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 30 2e 20 20 ersion 3.6.20.
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 2a 0a 2a 2a 20 40 28 23 29 20 ess..**.** @(#)
0ef0: 24 49 64 3a 20 73 71 6c 69 74 65 4c 69 6d 69 74 $Id: sqliteLimit
0f00: 2e 68 2c 76 20 31 2e 31 30 20 32 30 30 39 2f 30 .h,v 1.10 2009/0
0f10: 31 2f 31 30 20 31 36 3a 31 35 3a 30 39 20 64 61 1/10 16:15:09 da
0f20: 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a nielk1977 Exp $.
0f30: 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 */../*.** The ma
0f40: 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 ximum length of
0f50: 61 20 54 45 58 54 20 6f 72 20 42 4c 4f 42 20 69 a TEXT or BLOB i
0f60: 6e 20 62 79 74 65 73 2e 20 20 20 54 68 69 73 20 n bytes. This
0f70: 61 6c 73 6f 0a 2a 2a 20 6c 69 6d 69 74 73 20 74 also.** limits t
0f80: 68 65 20 73 69 7a 65 20 6f 66 20 61 20 72 6f 77 he size of a row
0f90: 20 69 6e 20 61 20 74 61 62 6c 65 20 6f 72 20 69 in a table or i
0fa0: 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ndex..**.** The
0fb0: 68 61 72 64 20 6c 69 6d 69 74 20 69 73 20 74 68 hard limit is th
0fc0: 65 20 61 62 69 6c 69 74 79 20 6f 66 20 61 20 33 e ability of a 3
0fd0: 32 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 2-bit signed int
0fe0: 65 67 65 72 0a 2a 2a 20 74 6f 20 63 6f 75 6e 74 eger.** to count
0ff0: 20 74 68 65 20 73 69 7a 65 3a 20 32 5e 33 31 2d the size: 2^31-
1000: 31 20 6f 72 20 32 31 34 37 34 38 33 36 34 37 2e 1 or 2147483647.
1010: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
1020: 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 0a 23 20 TE_MAX_LENGTH.#
1030: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 define SQLITE_MA
1040: 58 5f 4c 45 4e 47 54 48 20 31 30 30 30 30 30 30 X_LENGTH 1000000
1050: 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 000.#endif../*.*
1060: 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 6d 61 * This is the ma
1070: 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 0a ximum number of.
1080: 2a 2a 0a 2a 2a 20 20 20 20 2a 20 43 6f 6c 75 6d **.** * Colum
1090: 6e 73 20 69 6e 20 61 20 74 61 62 6c 65 0a 2a 2a ns in a table.**
10a0: 20 20 20 20 2a 20 43 6f 6c 75 6d 6e 73 20 69 6e * Columns in
10b0: 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 20 20 20 an index.**
10c0: 2a 20 43 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 76 * Columns in a v
10d0: 69 65 77 0a 2a 2a 20 20 20 20 2a 20 54 65 72 6d iew.** * Term
10e0: 73 20 69 6e 20 74 68 65 20 53 45 54 20 63 6c 61 s in the SET cla
10f0: 75 73 65 20 6f 66 20 61 6e 20 55 50 44 41 54 45 use of an UPDATE
1100: 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 20 20 statement.**
1110: 20 2a 20 54 65 72 6d 73 20 69 6e 20 74 68 65 20 * Terms in the
1120: 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 61 20 result set of a
1130: 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 SELECT statement
1140: 0a 2a 2a 20 20 20 20 2a 20 54 65 72 6d 73 20 69 .** * Terms i
1150: 6e 20 74 68 65 20 47 52 4f 55 50 20 42 59 20 6f n the GROUP BY o
1160: 72 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 r ORDER BY claus
1170: 65 73 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 es of a SELECT s
1180: 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 20 20 20 tatement..**
1190: 2a 20 54 65 72 6d 73 20 69 6e 20 74 68 65 20 56 * Terms in the V
11a0: 41 4c 55 45 53 20 63 6c 61 75 73 65 20 6f 66 20 ALUES clause of
11b0: 61 6e 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d an INSERT statem
11c0: 65 6e 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 68 61 ent.**.** The ha
11d0: 72 64 20 75 70 70 65 72 20 6c 69 6d 69 74 20 68 rd upper limit h
11e0: 65 72 65 20 69 73 20 33 32 36 37 36 2e 20 20 4d ere is 32676. M
11f0: 6f 73 74 20 64 61 74 61 62 61 73 65 20 70 65 6f ost database peo
1200: 70 6c 65 20 77 69 6c 6c 0a 2a 2a 20 74 65 6c 6c ple will.** tell
1210: 20 79 6f 75 20 74 68 61 74 20 69 6e 20 61 20 77 you that in a w
1220: 65 6c 6c 2d 6e 6f 72 6d 61 6c 69 7a 65 64 20 64 ell-normalized d
1230: 61 74 61 62 61 73 65 2c 20 79 6f 75 20 75 73 75 atabase, you usu
1240: 61 6c 6c 79 20 73 68 6f 75 6c 64 0a 2a 2a 20 6e ally should.** n
1250: 6f 74 20 68 61 76 65 20 6d 6f 72 65 20 74 68 61 ot have more tha
1260: 6e 20 61 20 64 6f 7a 65 6e 20 6f 72 20 73 6f 20 n a dozen or so
1270: 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 6e 79 20 74 columns in any t
1280: 61 62 6c 65 2e 20 20 41 6e 64 20 69 66 0a 2a 2a able. And if.**
1290: 20 74 68 61 74 20 69 73 20 74 68 65 20 63 61 73 that is the cas
12a0: 65 2c 20 74 68 65 72 65 20 69 73 20 6e 6f 20 70 e, there is no p
12b0: 6f 69 6e 74 20 69 6e 20 68 61 76 69 6e 67 20 6d oint in having m
12c0: 6f 72 65 20 74 68 61 6e 20 61 20 66 65 77 0a 2a ore than a few.*
12d0: 2a 20 64 6f 7a 65 6e 20 76 61 6c 75 65 73 20 69 * dozen values i
12e0: 6e 20 61 6e 79 20 6f 66 20 74 68 65 20 6f 74 68 n any of the oth
12f0: 65 72 20 73 69 74 75 61 74 69 6f 6e 73 20 64 65 er situations de
1300: 73 63 72 69 62 65 64 20 61 62 6f 76 65 2e 0a 2a scribed above..*
1310: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
1320: 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 23 20 64 65 _MAX_COLUMN.# de
1330: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f fine SQLITE_MAX_
1340: 43 4f 4c 55 4d 4e 20 32 30 30 30 0a 23 65 6e 64 COLUMN 2000.#end
1350: 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 if../*.** The ma
1360: 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 ximum length of
1370: 61 20 73 69 6e 67 6c 65 20 53 51 4c 20 73 74 61 a single SQL sta
1380: 74 65 6d 65 6e 74 20 69 6e 20 62 79 74 65 73 2e tement in bytes.
1390: 0a 2a 2a 0a 2a 2a 20 49 74 20 75 73 65 64 20 74 .**.** It used t
13a0: 6f 20 62 65 20 74 68 65 20 63 61 73 65 20 74 68 o be the case th
13b0: 61 74 20 73 65 74 74 69 6e 67 20 74 68 69 73 20 at setting this
13c0: 76 61 6c 75 65 20 74 6f 20 7a 65 72 6f 20 77 6f value to zero wo
13d0: 75 6c 64 0a 2a 2a 20 74 75 72 6e 20 74 68 65 20 uld.** turn the
13e0: 6c 69 6d 69 74 20 6f 66 66 2e 20 20 54 68 61 74 limit off. That
13f0: 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 74 72 is no longer tr
1400: 75 65 2e 20 20 49 74 20 69 73 20 6e 6f 74 20 70 ue. It is not p
1410: 6f 73 73 69 62 6c 65 0a 2a 2a 20 74 6f 20 74 75 ossible.** to tu
1420: 72 6e 20 74 68 69 73 20 6c 69 6d 69 74 20 6f 66 rn this limit of
1430: 66 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 f..*/.#ifndef SQ
1440: 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e LITE_MAX_SQL_LEN
1450: 47 54 48 0a 23 20 64 65 66 69 6e 65 20 53 51 4c GTH.# define SQL
1460: 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 ITE_MAX_SQL_LENG
1470: 54 48 20 31 30 30 30 30 30 30 30 30 30 0a 23 65 TH 1000000000.#e
1480: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ndif../*.** The
1490: 6d 61 78 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 maximum depth of
14a0: 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 an expression t
14b0: 72 65 65 2e 20 54 68 69 73 20 69 73 20 6c 69 6d ree. This is lim
14c0: 69 74 65 64 20 74 6f 20 0a 2a 2a 20 73 6f 6d 65 ited to .** some
14d0: 20 65 78 74 65 6e 74 20 62 79 20 53 51 4c 49 54 extent by SQLIT
14e0: 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 E_MAX_SQL_LENGTH
14f0: 2e 20 42 75 74 20 73 6f 6d 65 74 69 6d 65 20 79 . But sometime y
1500: 6f 75 20 6d 69 67 68 74 20 0a 2a 2a 20 77 61 6e ou might .** wan
1510: 74 20 74 6f 20 70 6c 61 63 65 20 6d 6f 72 65 20 t to place more
1520: 73 65 76 65 72 65 20 6c 69 6d 69 74 73 20 6f 6e severe limits on
1530: 20 74 68 65 20 63 6f 6d 70 6c 65 78 69 74 79 20 the complexity
1540: 6f 66 20 61 6e 20 0a 2a 2a 20 65 78 70 72 65 73 of an .** expres
1550: 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 61 sion..**.** A va
1560: 6c 75 65 20 6f 66 20 30 20 75 73 65 64 20 74 6f lue of 0 used to
1570: 20 6d 65 61 6e 20 74 68 61 74 20 74 68 65 20 6c mean that the l
1580: 69 6d 69 74 20 77 61 73 20 6e 6f 74 20 65 6e 66 imit was not enf
1590: 6f 72 63 65 64 2e 0a 2a 2a 20 42 75 74 20 74 68 orced..** But th
15a0: 61 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 at is no longer
15b0: 74 72 75 65 2e 20 20 54 68 65 20 6c 69 6d 69 74 true. The limit
15c0: 20 69 73 20 6e 6f 77 20 73 74 72 69 63 74 6c 79 is now strictly
15d0: 20 65 6e 66 6f 72 63 65 64 0a 2a 2a 20 61 74 20 enforced.** at
15e0: 61 6c 6c 20 74 69 6d 65 73 2e 0a 2a 2f 0a 23 69 all times..*/.#i
15f0: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 fndef SQLITE_MAX
1600: 5f 45 58 50 52 5f 44 45 50 54 48 0a 23 20 64 65 _EXPR_DEPTH.# de
1610: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f fine SQLITE_MAX_
1620: 45 58 50 52 5f 44 45 50 54 48 20 31 30 30 30 0a EXPR_DEPTH 1000.
1630: 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 #endif../*.** Th
1640: 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 e maximum number
1650: 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 61 20 63 of terms in a c
1660: 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 73 ompound SELECT s
1670: 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 54 68 65 tatement..** The
1680: 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 20 code generator
1690: 66 6f 72 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c for compound SEL
16a0: 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 64 ECT statements d
16b0: 6f 65 73 20 6f 6e 65 0a 2a 2a 20 6c 65 76 65 6c oes one.** level
16c0: 20 6f 66 20 72 65 63 75 72 73 69 6f 6e 20 66 6f of recursion fo
16d0: 72 20 65 61 63 68 20 74 65 72 6d 2e 20 20 41 20 r each term. A
16e0: 73 74 61 63 6b 20 6f 76 65 72 66 6c 6f 77 20 63 stack overflow c
16f0: 61 6e 20 72 65 73 75 6c 74 0a 2a 2a 20 69 66 20 an result.** if
1700: 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 the number of te
1710: 72 6d 73 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 rms is too large
1720: 2e 20 20 49 6e 20 70 72 61 63 74 69 63 65 2c 20 . In practice,
1730: 6d 6f 73 74 20 53 51 4c 0a 2a 2a 20 6e 65 76 65 most SQL.** neve
1740: 72 20 68 61 73 20 6d 6f 72 65 20 74 68 61 6e 20 r has more than
1750: 33 20 6f 72 20 34 20 74 65 72 6d 73 2e 20 20 55 3 or 4 terms. U
1760: 73 65 20 61 20 76 61 6c 75 65 20 6f 66 20 30 20 se a value of 0
1770: 74 6f 20 64 69 73 61 62 6c 65 0a 2a 2a 20 61 6e to disable.** an
1780: 79 20 6c 69 6d 69 74 20 6f 6e 20 74 68 65 20 6e y limit on the n
1790: 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69 umber of terms i
17a0: 6e 20 61 20 63 6f 6d 70 6f 75 6e 74 20 53 45 4c n a compount SEL
17b0: 45 43 54 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ECT..*/.#ifndef
17c0: 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4d 50 4f SQLITE_MAX_COMPO
17d0: 55 4e 44 5f 53 45 4c 45 43 54 0a 23 20 64 65 66 UND_SELECT.# def
17e0: 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 ine SQLITE_MAX_C
17f0: 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 35 OMPOUND_SELECT 5
1800: 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 00.#endif../*.**
1810: 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d The maximum num
1820: 62 65 72 20 6f 66 20 6f 70 63 6f 64 65 73 20 69 ber of opcodes i
1830: 6e 20 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d n a VDBE program
1840: 2e 0a 2a 2a 20 4e 6f 74 20 63 75 72 72 65 6e 74 ..** Not current
1850: 6c 79 20 65 6e 66 6f 72 63 65 64 2e 0a 2a 2f 0a ly enforced..*/.
1860: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d #ifndef SQLITE_M
1870: 41 58 5f 56 44 42 45 5f 4f 50 0a 23 20 64 65 66 AX_VDBE_OP.# def
1880: 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 ine SQLITE_MAX_V
1890: 44 42 45 5f 4f 50 20 32 35 30 30 30 0a 23 65 6e DBE_OP 25000.#en
18a0: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d dif../*.** The m
18b0: 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 aximum number of
18c0: 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 61 6e arguments to an
18d0: 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a SQL function..*
18e0: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
18f0: 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 _MAX_FUNCTION_AR
1900: 47 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 G.# define SQLIT
1910: 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41 E_MAX_FUNCTION_A
1920: 52 47 20 31 32 37 0a 23 65 6e 64 69 66 0a 0a 2f RG 127.#endif../
1930: 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d *.** The maximum
1940: 20 6e 75 6d 62 65 72 20 6f 66 20 69 6e 2d 6d 65 number of in-me
1950: 6d 6f 72 79 20 70 61 67 65 73 20 74 6f 20 75 73 mory pages to us
1960: 65 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 e for the main d
1970: 61 74 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 atabase.** table
1980: 20 61 6e 64 20 66 6f 72 20 74 65 6d 70 6f 72 61 and for tempora
1990: 72 79 20 74 61 62 6c 65 73 2e 20 20 54 68 65 20 ry tables. The
19a0: 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 SQLITE_DEFAULT_C
19b0: 41 43 48 45 5f 53 49 5a 45 0a 2a 2f 0a 23 69 66 ACHE_SIZE.*/.#if
19c0: 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 ndef SQLITE_DEFA
19d0: 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 0a 23 ULT_CACHE_SIZE.#
19e0: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 define SQLITE_D
19f0: 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a EFAULT_CACHE_SIZ
1a00: 45 20 20 32 30 30 30 0a 23 65 6e 64 69 66 0a 23 E 2000.#endif.#
1a10: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 ifndef SQLITE_DE
1a20: 46 41 55 4c 54 5f 54 45 4d 50 5f 43 41 43 48 45 FAULT_TEMP_CACHE
1a30: 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 _SIZE.# define S
1a40: 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 54 45 QLITE_DEFAULT_TE
1a50: 4d 50 5f 43 41 43 48 45 5f 53 49 5a 45 20 20 35 MP_CACHE_SIZE 5
1a60: 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 00.#endif../*.**
1a70: 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d The maximum num
1a80: 62 65 72 20 6f 66 20 61 74 74 61 63 68 65 64 20 ber of attached
1a90: 64 61 74 61 62 61 73 65 73 2e 20 20 54 68 69 73 databases. This
1aa0: 20 6d 75 73 74 20 62 65 20 62 65 74 77 65 65 6e must be between
1ab0: 20 30 0a 2a 2a 20 61 6e 64 20 33 30 2e 20 20 54 0.** and 30. T
1ac0: 68 65 20 75 70 70 65 72 20 62 6f 75 6e 64 20 6f he upper bound o
1ad0: 6e 20 33 30 20 69 73 20 62 65 63 61 75 73 65 20 n 30 is because
1ae0: 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 a 32-bit integer
1af0: 20 62 69 74 6d 61 70 0a 2a 2a 20 69 73 20 75 73 bitmap.** is us
1b00: 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 74 6f ed internally to
1b10: 20 74 72 61 63 6b 20 61 74 74 61 63 68 65 64 20 track attached
1b20: 64 61 74 61 62 61 73 65 73 2e 0a 2a 2f 0a 23 69 databases..*/.#i
1b30: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 fndef SQLITE_MAX
1b40: 5f 41 54 54 41 43 48 45 44 0a 23 20 64 65 66 69 _ATTACHED.# defi
1b50: 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 ne SQLITE_MAX_AT
1b60: 54 41 43 48 45 44 20 31 30 0a 23 65 6e 64 69 66 TACHED 10.#endif
1b70: 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 .../*.** The max
1b80: 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20 61 20 imum value of a
1b90: 3f 6e 6e 6e 20 77 69 6c 64 63 61 72 64 20 74 68 ?nnn wildcard th
1ba0: 61 74 20 74 68 65 20 70 61 72 73 65 72 20 77 69 at the parser wi
1bb0: 6c 6c 20 61 63 63 65 70 74 2e 0a 2a 2f 0a 23 69 ll accept..*/.#i
1bc0: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 fndef SQLITE_MAX
1bd0: 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 _VARIABLE_NUMBER
1be0: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
1bf0: 5f 4d 41 58 5f 56 41 52 49 41 42 4c 45 5f 4e 55 _MAX_VARIABLE_NU
1c00: 4d 42 45 52 20 39 39 39 0a 23 65 6e 64 69 66 0a MBER 999.#endif.
1c10: 0a 2f 2a 20 4d 61 78 69 6d 75 6d 20 70 61 67 65 ./* Maximum page
1c20: 20 73 69 7a 65 2e 20 20 54 68 65 20 75 70 70 65 size. The uppe
1c30: 72 20 62 6f 75 6e 64 20 6f 6e 20 74 68 69 73 20 r bound on this
1c40: 76 61 6c 75 65 20 69 73 20 33 32 37 36 38 2e 20 value is 32768.
1c50: 20 54 68 69 73 20 61 20 6c 69 6d 69 74 0a 2a 2a This a limit.**
1c60: 20 69 6d 70 6f 73 65 64 20 62 79 20 74 68 65 20 imposed by the
1c70: 6e 65 63 65 73 73 69 74 79 20 6f 66 20 73 74 6f necessity of sto
1c80: 72 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 69 ring the value i
1c90: 6e 20 61 20 32 2d 62 79 74 65 20 75 6e 73 69 67 n a 2-byte unsig
1ca0: 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 61 ned integer.** a
1cb0: 6e 64 20 74 68 65 20 66 61 63 74 20 74 68 61 74 nd the fact that
1cc0: 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 6d the page size m
1cd0: 75 73 74 20 62 65 20 61 20 70 6f 77 65 72 20 6f ust be a power o
1ce0: 66 20 32 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 f 2..**.** If th
1cf0: 69 73 20 6c 69 6d 69 74 20 69 73 20 63 68 61 6e is limit is chan
1d00: 67 65 64 2c 20 74 68 65 6e 20 74 68 65 20 63 6f ged, then the co
1d10: 6d 70 69 6c 65 64 20 6c 69 62 72 61 72 79 20 69 mpiled library i
1d20: 73 20 74 65 63 68 6e 69 63 61 6c 6c 79 0a 2a 2a s technically.**
1d30: 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 77 69 incompatible wi
1d40: 74 68 20 61 6e 20 53 51 4c 69 74 65 20 6c 69 62 th an SQLite lib
1d50: 72 61 72 79 20 63 6f 6d 70 69 6c 65 64 20 77 69 rary compiled wi
1d60: 74 68 20 61 20 64 69 66 66 65 72 65 6e 74 20 6c th a different l
1d70: 69 6d 69 74 2e 20 49 66 0a 2a 2a 20 61 20 70 72 imit. If.** a pr
1d80: 6f 63 65 73 73 20 6f 70 65 72 61 74 69 6e 67 20 ocess operating
1d90: 6f 6e 20 61 20 64 61 74 61 62 61 73 65 20 77 69 on a database wi
1da0: 74 68 20 61 20 70 61 67 65 2d 73 69 7a 65 20 6f th a page-size o
1db0: 66 20 36 35 35 33 36 20 62 79 74 65 73 20 0a 2a f 65536 bytes .*
1dc0: 2a 20 63 72 61 73 68 65 73 2c 20 74 68 65 6e 20 * crashes, then
1dd0: 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 53 an instance of S
1de0: 51 4c 69 74 65 20 63 6f 6d 70 69 6c 65 64 20 77 QLite compiled w
1df0: 69 74 68 20 74 68 65 20 64 65 66 61 75 6c 74 20 ith the default
1e00: 70 61 67 65 2d 73 69 7a 65 20 0a 2a 2a 20 6c 69 page-size .** li
1e10: 6d 69 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 mit will not be
1e20: 61 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b able to rollback
1e30: 20 74 68 65 20 61 62 6f 72 74 65 64 20 74 72 61 the aborted tra
1e40: 6e 73 61 63 74 69 6f 6e 2e 20 54 68 69 73 20 63 nsaction. This c
1e50: 6f 75 6c 64 0a 2a 2a 20 6c 65 61 64 20 74 6f 20 ould.** lead to
1e60: 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 database corrupt
1e70: 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ion..*/.#ifndef
1e80: 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f SQLITE_MAX_PAGE_
1e90: 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51 SIZE.# define SQ
1ea0: 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 LITE_MAX_PAGE_SI
1eb0: 5a 45 20 33 32 37 36 38 0a 23 65 6e 64 69 66 0a ZE 32768.#endif.
1ec0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 ../*.** The defa
1ed0: 75 6c 74 20 73 69 7a 65 20 6f 66 20 61 20 64 61 ult size of a da
1ee0: 74 61 62 61 73 65 20 70 61 67 65 2e 0a 2a 2f 0a tabase page..*/.
1ef0: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 #ifndef SQLITE_D
1f00: 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 EFAULT_PAGE_SIZE
1f10: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
1f20: 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 _DEFAULT_PAGE_SI
1f30: 5a 45 20 31 30 32 34 0a 23 65 6e 64 69 66 0a 23 ZE 1024.#endif.#
1f40: 69 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c if SQLITE_DEFAUL
1f50: 54 5f 50 41 47 45 5f 53 49 5a 45 3e 53 51 4c 49 T_PAGE_SIZE>SQLI
1f60: 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 TE_MAX_PAGE_SIZE
1f70: 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f .# undef SQLITE_
1f80: 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a DEFAULT_PAGE_SIZ
1f90: 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 E.# define SQLIT
1fa0: 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 E_DEFAULT_PAGE_S
1fb0: 49 5a 45 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 IZE SQLITE_MAX_P
1fc0: 41 47 45 5f 53 49 5a 45 0a 23 65 6e 64 69 66 0a AGE_SIZE.#endif.
1fd0: 0a 2f 2a 0a 2a 2a 20 4f 72 64 69 6e 61 72 69 6c ./*.** Ordinaril
1fe0: 79 2c 20 69 66 20 6e 6f 20 76 61 6c 75 65 20 69 y, if no value i
1ff0: 73 20 65 78 70 6c 69 63 69 74 6c 79 20 70 72 6f s explicitly pro
2000: 76 69 64 65 64 2c 20 53 51 4c 69 74 65 20 63 72 vided, SQLite cr
2010: 65 61 74 65 73 20 64 61 74 61 62 61 73 65 73 0a eates databases.
2020: 2a 2a 20 77 69 74 68 20 70 61 67 65 20 73 69 7a ** with page siz
2030: 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 e SQLITE_DEFAULT
2040: 5f 50 41 47 45 5f 53 49 5a 45 2e 20 48 6f 77 65 _PAGE_SIZE. Howe
2050: 76 65 72 2c 20 62 61 73 65 64 20 6f 6e 20 63 65 ver, based on ce
2060: 72 74 61 69 6e 0a 2a 2a 20 64 65 76 69 63 65 20 rtain.** device
2070: 63 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20 characteristics
2080: 28 73 65 63 74 6f 72 2d 73 69 7a 65 20 61 6e 64 (sector-size and
2090: 20 61 74 6f 6d 69 63 20 77 72 69 74 65 28 29 20 atomic write()
20a0: 73 75 70 70 6f 72 74 29 2c 0a 2a 2a 20 53 51 4c support),.** SQL
20b0: 69 74 65 20 6d 61 79 20 63 68 6f 6f 73 65 20 61 ite may choose a
20c0: 20 6c 61 72 67 65 72 20 76 61 6c 75 65 2e 20 54 larger value. T
20d0: 68 69 73 20 63 6f 6e 73 74 61 6e 74 20 69 73 20 his constant is
20e0: 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75 the maximum valu
20f0: 65 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c e.** SQLite will
2100: 20 63 68 6f 6f 73 65 20 6f 6e 20 69 74 73 20 6f choose on its o
2110: 77 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 wn..*/.#ifndef S
2120: 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c QLITE_MAX_DEFAUL
2130: 54 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20 64 65 T_PAGE_SIZE.# de
2140: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f fine SQLITE_MAX_
2150: 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a DEFAULT_PAGE_SIZ
2160: 45 20 38 31 39 32 0a 23 65 6e 64 69 66 0a 23 69 E 8192.#endif.#i
2170: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 f SQLITE_MAX_DEF
2180: 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3e 53 AULT_PAGE_SIZE>S
2190: 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 QLITE_MAX_PAGE_S
21a0: 49 5a 45 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 IZE.# undef SQLI
21b0: 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 TE_MAX_DEFAULT_P
21c0: 41 47 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e AGE_SIZE.# defin
21d0: 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 e SQLITE_MAX_DEF
21e0: 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 20 53 AULT_PAGE_SIZE S
21f0: 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 QLITE_MAX_PAGE_S
2200: 49 5a 45 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a IZE.#endif.../*.
2210: 2a 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 ** Maximum numbe
2220: 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 6f 6e r of pages in on
2230: 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
2240: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 72 .**.** This is r
2250: 65 61 6c 6c 79 20 6a 75 73 74 20 74 68 65 20 64 eally just the d
2260: 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 efault value for
2270: 20 74 68 65 20 6d 61 78 5f 70 61 67 65 5f 63 6f the max_page_co
2280: 75 6e 74 20 70 72 61 67 6d 61 2e 0a 2a 2a 20 54 unt pragma..** T
2290: 68 69 73 20 76 61 6c 75 65 20 63 61 6e 20 62 65 his value can be
22a0: 20 6c 6f 77 65 72 65 64 20 28 6f 72 20 72 61 69 lowered (or rai
22b0: 73 65 64 29 20 61 74 20 72 75 6e 2d 74 69 6d 65 sed) at run-time
22c0: 20 75 73 69 6e 67 20 74 68 61 74 20 74 68 65 0a using that the.
22d0: 2a 2a 20 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e ** max_page_coun
22e0: 74 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 6e t macro..*/.#ifn
22f0: 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 def SQLITE_MAX_P
2300: 41 47 45 5f 43 4f 55 4e 54 0a 23 20 64 65 66 69 AGE_COUNT.# defi
2310: 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 ne SQLITE_MAX_PA
2320: 47 45 5f 43 4f 55 4e 54 20 31 30 37 33 37 34 31 GE_COUNT 1073741
2330: 38 32 33 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 823.#endif../*.*
2340: 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 * Maximum length
2350: 20 28 69 6e 20 62 79 74 65 73 29 20 6f 66 20 74 (in bytes) of t
2360: 68 65 20 70 61 74 74 65 72 6e 20 69 6e 20 61 20 he pattern in a
2370: 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 0a 2a 2a 20 LIKE or GLOB.**
2380: 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 23 69 66 operator..*/.#if
2390: 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f ndef SQLITE_MAX_
23a0: 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e LIKE_PATTERN_LEN
23b0: 47 54 48 0a 23 20 64 65 66 69 6e 65 20 53 51 4c GTH.# define SQL
23c0: 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 ITE_MAX_LIKE_PAT
23d0: 54 45 52 4e 5f 4c 45 4e 47 54 48 20 35 30 30 30 TERN_LENGTH 5000
23e0: 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 0.#endif../*.**
23f0: 4d 61 78 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 Maximum depth of
2400: 20 72 65 63 75 72 73 69 6f 6e 20 66 6f 72 20 74 recursion for t
2410: 72 69 67 67 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 41 riggers..**.** A
2420: 20 76 61 6c 75 65 20 6f 66 20 31 20 6d 65 61 6e value of 1 mean
2430: 73 20 74 68 61 74 20 61 20 74 72 69 67 67 65 72 s that a trigger
2440: 20 70 72 6f 67 72 61 6d 20 77 69 6c 6c 20 6e 6f program will no
2450: 74 20 62 65 20 61 62 6c 65 20 74 6f 20 69 74 73 t be able to its
2460: 65 6c 66 0a 2a 2a 20 66 69 72 65 20 61 6e 79 20 elf.** fire any
2470: 74 72 69 67 67 65 72 73 2e 20 41 20 76 61 6c 75 triggers. A valu
2480: 65 20 6f 66 20 30 20 6d 65 61 6e 73 20 74 68 61 e of 0 means tha
2490: 74 20 6e 6f 20 74 72 69 67 67 65 72 20 70 72 6f t no trigger pro
24a0: 67 72 61 6d 73 20 61 74 20 61 6c 6c 20 0a 2a 2a grams at all .**
24b0: 20 6d 61 79 20 62 65 20 65 78 65 63 75 74 65 64 may be executed
24c0: 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
24d0: 49 54 45 5f 4d 41 58 5f 54 52 49 47 47 45 52 5f ITE_MAX_TRIGGER_
24e0: 44 45 50 54 48 0a 23 69 66 20 64 65 66 69 6e 65 DEPTH.#if define
24f0: 64 28 53 51 4c 49 54 45 5f 53 4d 41 4c 4c 5f 53 d(SQLITE_SMALL_S
2500: 54 41 43 4b 29 0a 23 20 64 65 66 69 6e 65 20 53 TACK).# define S
2510: 51 4c 49 54 45 5f 4d 41 58 5f 54 52 49 47 47 45 QLITE_MAX_TRIGGE
2520: 52 5f 44 45 50 54 48 20 31 30 0a 23 65 6c 73 65 R_DEPTH 10.#else
2530: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
2540: 5f 4d 41 58 5f 54 52 49 47 47 45 52 5f 44 45 50 _MAX_TRIGGER_DEP
2550: 54 48 20 31 30 30 30 0a 23 65 6e 64 69 66 0a 23 TH 1000.#endif.#
2560: 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a endif../********
2570: 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 ****** End of sq
2580: 6c 69 74 65 4c 69 6d 69 74 2e 68 20 2a 2a 2a 2a liteLimit.h ****
2590: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
25a0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
25b0: 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
25c0: 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e ****** Continuin
25d0: 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 g where we left
25e0: 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 off in sqliteInt
25f0: 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
2600: 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 20 44 69 73 61 62 *****/../* Disab
2610: 6c 65 20 6e 75 69 73 61 6e 63 65 20 77 61 72 6e le nuisance warn
2620: 69 6e 67 73 20 6f 6e 20 42 6f 72 6c 61 6e 64 20 ings on Borland
2630: 63 6f 6d 70 69 6c 65 72 73 20 2a 2f 0a 23 69 66 compilers */.#if
2640: 20 64 65 66 69 6e 65 64 28 5f 5f 42 4f 52 4c 41 defined(__BORLA
2650: 4e 44 43 5f 5f 29 0a 23 70 72 61 67 6d 61 20 77 NDC__).#pragma w
2660: 61 72 6e 20 2d 72 63 68 20 2f 2a 20 75 6e 72 65 arn -rch /* unre
2670: 61 63 68 61 62 6c 65 20 63 6f 64 65 20 2a 2f 0a achable code */.
2680: 23 70 72 61 67 6d 61 20 77 61 72 6e 20 2d 63 63 #pragma warn -cc
2690: 63 20 2f 2a 20 43 6f 6e 64 69 74 69 6f 6e 20 69 c /* Condition i
26a0: 73 20 61 6c 77 61 79 73 20 74 72 75 65 20 6f 72 s always true or
26b0: 20 66 61 6c 73 65 20 2a 2f 0a 23 70 72 61 67 6d false */.#pragm
26c0: 61 20 77 61 72 6e 20 2d 61 75 73 20 2f 2a 20 41 a warn -aus /* A
26d0: 73 73 69 67 6e 65 64 20 76 61 6c 75 65 20 69 73 ssigned value is
26e0: 20 6e 65 76 65 72 20 75 73 65 64 20 2a 2f 0a 23 never used */.#
26f0: 70 72 61 67 6d 61 20 77 61 72 6e 20 2d 63 73 75 pragma warn -csu
2700: 20 2f 2a 20 43 6f 6d 70 61 72 69 6e 67 20 73 69 /* Comparing si
2710: 67 6e 65 64 20 61 6e 64 20 75 6e 73 69 67 6e 65 gned and unsigne
2720: 64 20 2a 2f 0a 23 70 72 61 67 6d 61 20 77 61 72 d */.#pragma war
2730: 6e 20 2d 73 70 61 20 2f 2a 20 53 75 73 70 69 63 n -spa /* Suspic
2740: 69 6f 75 73 20 70 6f 69 6e 74 65 72 20 61 72 69 ious pointer ari
2750: 74 68 6d 65 74 69 63 20 2a 2f 0a 23 65 6e 64 69 thmetic */.#endi
2760: 66 0a 0a 2f 2a 20 4e 65 65 64 65 64 20 66 6f 72 f../* Needed for
2770: 20 76 61 72 69 6f 75 73 20 64 65 66 69 6e 69 74 various definit
2780: 69 6f 6e 73 2e 2e 2e 20 2a 2f 0a 23 69 66 6e 64 ions... */.#ifnd
2790: 65 66 20 5f 47 4e 55 5f 53 4f 55 52 43 45 0a 23 ef _GNU_SOURCE.#
27a0: 20 64 65 66 69 6e 65 20 5f 47 4e 55 5f 53 4f 55 define _GNU_SOU
27b0: 52 43 45 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a RCE.#endif../*.*
27c0: 2a 20 49 6e 63 6c 75 64 65 20 73 74 61 6e 64 61 * Include standa
27d0: 72 64 20 68 65 61 64 65 72 20 66 69 6c 65 73 20 rd header files
27e0: 61 73 20 6e 65 63 65 73 73 61 72 79 0a 2a 2f 0a as necessary.*/.
27f0: 23 69 66 64 65 66 20 48 41 56 45 5f 53 54 44 49 #ifdef HAVE_STDI
2800: 4e 54 5f 48 0a 23 69 6e 63 6c 75 64 65 20 3c 73 NT_H.#include <s
2810: 74 64 69 6e 74 2e 68 3e 0a 23 65 6e 64 69 66 0a tdint.h>.#endif.
2820: 23 69 66 64 65 66 20 48 41 56 45 5f 49 4e 54 54 #ifdef HAVE_INTT
2830: 59 50 45 53 5f 48 0a 23 69 6e 63 6c 75 64 65 20 YPES_H.#include
2840: 3c 69 6e 74 74 79 70 65 73 2e 68 3e 0a 23 65 6e <inttypes.h>.#en
2850: 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 53 51 4c dif..#define SQL
2860: 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45 ITE_INDEX_SAMPLE
2870: 53 20 31 30 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 S 10../*.** This
2880: 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64 20 74 macro is used t
2890: 6f 20 22 68 69 64 65 22 20 73 6f 6d 65 20 75 67 o "hide" some ug
28a0: 6c 69 6e 65 73 73 20 69 6e 20 63 61 73 74 69 6e liness in castin
28b0: 67 20 61 6e 20 69 6e 74 0a 2a 2a 20 76 61 6c 75 g an int.** valu
28c0: 65 20 74 6f 20 61 20 70 74 72 20 76 61 6c 75 65 e to a ptr value
28d0: 20 75 6e 64 65 72 20 74 68 65 20 4d 53 56 43 20 under the MSVC
28e0: 36 34 2d 62 69 74 20 63 6f 6d 70 69 6c 65 72 2e 64-bit compiler.
28f0: 20 20 20 43 61 73 74 69 6e 67 0a 2a 2a 20 6e 6f Casting.** no
2900: 6e 20 36 34 2d 62 69 74 20 76 61 6c 75 65 73 20 n 64-bit values
2910: 74 6f 20 70 74 72 20 74 79 70 65 73 20 72 65 73 to ptr types res
2920: 75 6c 74 73 20 69 6e 20 61 20 22 68 61 72 64 22 ults in a "hard"
2930: 20 65 72 72 6f 72 20 77 69 74 68 20 0a 2a 2a 20 error with .**
2940: 74 68 65 20 4d 53 56 43 20 36 34 2d 62 69 74 20 the MSVC 64-bit
2950: 63 6f 6d 70 69 6c 65 72 20 77 68 69 63 68 20 74 compiler which t
2960: 68 69 73 20 61 74 74 65 6d 70 74 73 20 74 6f 20 his attempts to
2970: 61 76 6f 69 64 2e 20 20 0a 2a 2a 0a 2a 2a 20 41 avoid. .**.** A
2980: 20 73 69 6d 70 6c 65 20 63 6f 6d 70 69 6c 65 72 simple compiler
2990: 20 70 72 61 67 6d 61 20 6f 72 20 63 61 73 74 69 pragma or casti
29a0: 6e 67 20 73 65 71 75 65 6e 63 65 20 63 6f 75 6c ng sequence coul
29b0: 64 20 6e 6f 74 20 62 65 20 66 6f 75 6e 64 0a 2a d not be found.*
29c0: 2a 20 74 6f 20 63 6f 72 72 65 63 74 20 74 68 69 * to correct thi
29d0: 73 20 69 6e 20 61 6c 6c 20 73 69 74 75 61 74 69 s in all situati
29e0: 6f 6e 73 2c 20 73 6f 20 74 68 69 73 20 6d 61 63 ons, so this mac
29f0: 72 6f 20 77 61 73 20 69 6e 74 72 6f 64 75 63 65 ro was introduce
2a00: 64 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 63 6f 75 6c d..**.** It coul
2a10: 64 20 62 65 20 61 72 67 75 65 64 20 74 68 61 74 d be argued that
2a20: 20 74 68 65 20 69 6e 74 70 74 72 5f 74 20 74 79 the intptr_t ty
2a30: 70 65 20 63 6f 75 6c 64 20 62 65 20 75 73 65 64 pe could be used
2a40: 20 69 6e 20 74 68 69 73 0a 2a 2a 20 63 61 73 65 in this.** case
2a50: 2c 20 62 75 74 20 74 68 61 74 20 74 79 70 65 20 , but that type
2a60: 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 is not available
2a70: 20 6f 6e 20 61 6c 6c 20 63 6f 6d 70 69 6c 65 72 on all compiler
2a80: 73 2c 20 6f 72 20 0a 2a 2a 20 72 65 71 75 69 72 s, or .** requir
2a90: 65 73 20 74 68 65 20 23 69 6e 63 6c 75 64 65 20 es the #include
2aa0: 6f 66 20 73 70 65 63 69 66 69 63 20 68 65 61 64 of specific head
2ab0: 65 72 73 20 77 68 69 63 68 20 64 69 66 66 65 72 ers which differ
2ac0: 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 70 6c 61 s between.** pla
2ad0: 74 66 6f 72 6d 73 2e 0a 2a 2a 0a 2a 2a 20 54 69 tforms..**.** Ti
2ae0: 63 6b 65 74 20 23 33 38 36 30 3a 20 20 54 68 65 cket #3860: The
2af0: 20 6c 6c 76 6d 2d 67 63 63 2d 34 2e 32 20 63 6f llvm-gcc-4.2 co
2b00: 6d 70 69 6c 65 72 20 66 72 6f 6d 20 41 70 70 6c mpiler from Appl
2b10: 65 20 63 68 6f 6b 65 73 20 6f 6e 0a 2a 2a 20 74 e chokes on.** t
2b20: 68 65 20 28 28 76 6f 69 64 2a 29 26 28 28 63 68 he ((void*)&((ch
2b30: 61 72 2a 29 30 29 5b 58 5d 29 20 63 6f 6e 73 74 ar*)0)[X]) const
2b40: 72 75 63 74 2e 20 20 42 75 74 20 4d 53 56 43 20 ruct. But MSVC
2b50: 63 68 6f 6b 65 73 20 6f 6e 20 28 28 76 6f 69 64 chokes on ((void
2b60: 2a 29 28 58 29 29 2e 0a 2a 2a 20 53 6f 20 77 65 *)(X))..** So we
2b70: 20 68 61 76 65 20 74 6f 20 64 65 66 69 6e 65 20 have to define
2b80: 74 68 65 20 6d 61 63 72 6f 73 20 69 6e 20 64 69 the macros in di
2b90: 66 66 65 72 65 6e 74 20 77 61 79 73 20 64 65 70 fferent ways dep
2ba0: 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 0a 2a 2a ending on the.**
2bb0: 20 63 6f 6d 70 69 6c 65 72 2e 0a 2a 2f 0a 23 69 compiler..*/.#i
2bc0: 66 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 f defined(__GNUC
2bd0: 5f 5f 29 0a 23 20 69 66 20 64 65 66 69 6e 65 64 __).# if defined
2be0: 28 48 41 56 45 5f 53 54 44 49 4e 54 5f 48 29 0a (HAVE_STDINT_H).
2bf0: 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 # define SQLIT
2c00: 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 58 29 20 E_INT_TO_PTR(X)
2c10: 20 28 28 76 6f 69 64 2a 29 28 69 6e 74 70 74 72 ((void*)(intptr
2c20: 5f 74 29 28 58 29 29 0a 23 20 20 20 64 65 66 69 _t)(X)).# defi
2c30: 6e 65 20 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f ne SQLITE_PTR_TO
2c40: 5f 49 4e 54 28 58 29 20 20 28 28 69 6e 74 29 28 _INT(X) ((int)(
2c50: 69 6e 74 70 74 72 5f 74 29 28 58 29 29 0a 23 20 intptr_t)(X)).#
2c60: 65 6c 73 65 0a 23 20 20 20 64 65 66 69 6e 65 20 else.# define
2c70: 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 SQLITE_INT_TO_PT
2c80: 52 28 58 29 20 20 28 28 76 6f 69 64 2a 29 28 58 R(X) ((void*)(X
2c90: 29 29 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51 )).# define SQ
2ca0: 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 LITE_PTR_TO_INT(
2cb0: 58 29 20 20 28 28 69 6e 74 29 28 58 29 29 0a 23 X) ((int)(X)).#
2cc0: 20 65 6e 64 69 66 0a 23 65 6c 73 65 0a 23 20 64 endif.#else.# d
2cd0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 efine SQLITE_INT
2ce0: 5f 54 4f 5f 50 54 52 28 58 29 20 20 20 28 28 76 _TO_PTR(X) ((v
2cf0: 6f 69 64 2a 29 26 28 28 63 68 61 72 2a 29 30 29 oid*)&((char*)0)
2d00: 5b 58 5d 29 0a 23 20 64 65 66 69 6e 65 20 53 51 [X]).# define SQ
2d10: 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 LITE_PTR_TO_INT(
2d20: 58 29 20 20 20 28 28 69 6e 74 29 28 28 28 63 68 X) ((int)(((ch
2d30: 61 72 2a 29 58 29 2d 28 63 68 61 72 2a 29 30 29 ar*)X)-(char*)0)
2d40: 29 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a ).#endif.../*.**
2d50: 20 54 68 65 20 53 51 4c 49 54 45 5f 54 48 52 45 The SQLITE_THRE
2d60: 41 44 53 41 46 45 20 6d 61 63 72 6f 20 6d 75 73 ADSAFE macro mus
2d70: 74 20 62 65 20 64 65 66 69 6e 65 64 20 61 73 20 t be defined as
2d80: 65 69 74 68 65 72 20 30 20 6f 72 20 31 2e 0a 2a either 0 or 1..*
2d90: 2a 20 4f 6c 64 65 72 20 76 65 72 73 69 6f 6e 73 * Older versions
2da0: 20 6f 66 20 53 51 4c 69 74 65 20 75 73 65 64 20 of SQLite used
2db0: 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 54 48 52 45 an optional THRE
2dc0: 41 44 53 41 46 45 20 6d 61 63 72 6f 2e 0a 2a 2a ADSAFE macro..**
2dd0: 20 57 65 20 73 75 70 70 6f 72 74 20 74 68 61 74 We support that
2de0: 20 66 6f 72 20 6c 65 67 61 63 79 0a 2a 2f 0a 23 for legacy.*/.#
2df0: 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 if !defined(SQLI
2e00: 54 45 5f 54 48 52 45 41 44 53 41 46 45 29 0a 23 TE_THREADSAFE).#
2e10: 69 66 20 64 65 66 69 6e 65 64 28 54 48 52 45 41 if defined(THREA
2e20: 44 53 41 46 45 29 0a 23 20 64 65 66 69 6e 65 20 DSAFE).# define
2e30: 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
2e40: 45 20 54 48 52 45 41 44 53 41 46 45 0a 23 65 6c E THREADSAFE.#el
2e50: 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 se.# define SQLI
2e60: 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 31 0a TE_THREADSAFE 1.
2e70: 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f #endif.#endif../
2e80: 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f *.** The SQLITE_
2e90: 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54 41 54 55 DEFAULT_MEMSTATU
2ea0: 53 20 6d 61 63 72 6f 20 6d 75 73 74 20 62 65 20 S macro must be
2eb0: 64 65 66 69 6e 65 64 20 61 73 20 65 69 74 68 65 defined as eithe
2ec0: 72 20 30 20 6f 72 20 31 2e 0a 2a 2a 20 49 74 20 r 0 or 1..** It
2ed0: 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65 74 68 determines wheth
2ee0: 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 66 65 er or not the fe
2ef0: 61 74 75 72 65 73 20 72 65 6c 61 74 65 64 20 74 atures related t
2f00: 6f 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 4f 4e o .** SQLITE_CON
2f10: 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 20 61 72 FIG_MEMSTATUS ar
2f20: 65 20 61 76 61 69 6c 61 62 6c 65 20 62 79 20 64 e available by d
2f30: 65 66 61 75 6c 74 20 6f 72 20 6e 6f 74 2e 20 54 efault or not. T
2f40: 68 69 73 20 76 61 6c 75 65 20 63 61 6e 0a 2a 2a his value can.**
2f50: 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e 20 61 be overridden a
2f60: 74 20 72 75 6e 74 69 6d 65 20 75 73 69 6e 67 20 t runtime using
2f70: 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 the sqlite3_conf
2f80: 69 67 28 29 20 41 50 49 2e 0a 2a 2f 0a 23 69 66 ig() API..*/.#if
2f90: 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
2fa0: 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54 41 54 _DEFAULT_MEMSTAT
2fb0: 55 53 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c US).# define SQL
2fc0: 49 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 ITE_DEFAULT_MEMS
2fd0: 54 41 54 55 53 20 31 0a 23 65 6e 64 69 66 0a 0a TATUS 1.#endif..
2fe0: 2f 2a 0a 2a 2a 20 45 78 61 63 74 6c 79 20 6f 6e /*.** Exactly on
2ff0: 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
3000: 6e 67 20 6d 61 63 72 6f 73 20 6d 75 73 74 20 62 ng macros must b
3010: 65 20 64 65 66 69 6e 65 64 20 69 6e 20 6f 72 64 e defined in ord
3020: 65 72 20 74 6f 0a 2a 2a 20 73 70 65 63 69 66 79 er to.** specify
3030: 20 77 68 69 63 68 20 6d 65 6d 6f 72 79 20 61 6c which memory al
3040: 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 location subsyst
3050: 65 6d 20 74 6f 20 75 73 65 2e 0a 2a 2a 0a 2a 2a em to use..**.**
3060: 20 20 20 20 20 53 51 4c 49 54 45 5f 53 59 53 54 SQLITE_SYST
3070: 45 4d 5f 4d 41 4c 4c 4f 43 20 20 20 20 20 20 20 EM_MALLOC
3080: 20 20 20 2f 2f 20 55 73 65 20 6e 6f 72 6d 61 6c // Use normal
3090: 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 29 system malloc()
30a0: 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4d .** SQLITE_M
30b0: 45 4d 44 45 42 55 47 20 20 20 20 20 20 20 20 20 EMDEBUG
30c0: 20 20 20 20 20 20 2f 2f 20 44 65 62 75 67 67 69 // Debuggi
30d0: 6e 67 20 76 65 72 73 69 6f 6e 20 6f 66 20 73 79 ng version of sy
30e0: 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 29 0a 2a 2a stem malloc().**
30f0: 20 20 20 20 20 53 51 4c 49 54 45 5f 4d 45 4d 4f SQLITE_MEMO
3100: 52 59 5f 53 49 5a 45 20 20 20 20 20 20 20 20 20 RY_SIZE
3110: 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61 6c 20 61 // internal a
3120: 6c 6c 6f 63 61 74 6f 72 20 23 31 0a 2a 2a 20 20 llocator #1.**
3130: 20 20 20 53 51 4c 49 54 45 5f 4d 4d 41 50 5f 48 SQLITE_MMAP_H
3140: 45 41 50 5f 53 49 5a 45 20 20 20 20 20 20 20 20 EAP_SIZE
3150: 20 2f 2f 20 69 6e 74 65 72 6e 61 6c 20 6d 6d 61 // internal mma
3160: 70 28 29 20 61 6c 6c 6f 63 61 74 6f 72 0a 2a 2a p() allocator.**
3170: 20 20 20 20 20 53 51 4c 49 54 45 5f 50 4f 57 32 SQLITE_POW2
3180: 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 20 20 20 20 _MEMORY_SIZE
3190: 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61 6c 20 70 // internal p
31a0: 6f 77 65 72 2d 6f 66 2d 74 77 6f 20 61 6c 6c 6f ower-of-two allo
31b0: 63 61 74 6f 72 0a 2a 2a 0a 2a 2a 20 49 66 20 6e cator.**.** If n
31c0: 6f 6e 65 20 6f 66 20 74 68 65 20 61 62 6f 76 65 one of the above
31d0: 20 61 72 65 20 64 65 66 69 6e 65 64 2c 20 74 68 are defined, th
31e0: 65 6e 20 73 65 74 20 53 51 4c 49 54 45 5f 53 59 en set SQLITE_SY
31f0: 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 61 73 0a 2a STEM_MALLOC as.*
3200: 2a 20 74 68 65 20 64 65 66 61 75 6c 74 2e 0a 2a * the default..*
3210: 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 /.#if defined(SQ
3220: 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c LITE_SYSTEM_MALL
3230: 4f 43 29 2b 64 65 66 69 6e 65 64 28 53 51 4c 49 OC)+defined(SQLI
3240: 54 45 5f 4d 45 4d 44 45 42 55 47 29 2b 5c 0a 20 TE_MEMDEBUG)+\.
3250: 20 20 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 defined(SQLIT
3260: 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 29 2b 64 E_MEMORY_SIZE)+d
3270: 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 4d efined(SQLITE_MM
3280: 41 50 5f 48 45 41 50 5f 53 49 5a 45 29 2b 5c 0a AP_HEAP_SIZE)+\.
3290: 20 20 20 20 64 65 66 69 6e 65 64 28 53 51 4c 49 defined(SQLI
32a0: 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59 5f 53 TE_POW2_MEMORY_S
32b0: 49 5a 45 29 3e 31 0a 23 20 65 72 72 6f 72 20 22 IZE)>1.# error "
32c0: 41 74 20 6d 6f 73 74 20 6f 6e 65 20 6f 66 20 74 At most one of t
32d0: 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6d he following com
32e0: 70 69 6c 65 2d 74 69 6d 65 20 63 6f 6e 66 69 67 pile-time config
32f0: 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 5c uration options\
3300: 0a 20 69 73 20 61 6c 6c 6f 77 73 3a 20 53 51 4c . is allows: SQL
3310: 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f ITE_SYSTEM_MALLO
3320: 43 2c 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 C, SQLITE_MEMDEB
3330: 55 47 2c 20 53 51 4c 49 54 45 5f 4d 45 4d 4f 52 UG, SQLITE_MEMOR
3340: 59 5f 53 49 5a 45 2c 5c 0a 20 53 51 4c 49 54 45 Y_SIZE,\. SQLITE
3350: 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a 45 2c _MMAP_HEAP_SIZE,
3360: 20 53 51 4c 49 54 45 5f 50 4f 57 32 5f 4d 45 4d SQLITE_POW2_MEM
3370: 4f 52 59 5f 53 49 5a 45 22 0a 23 65 6e 64 69 66 ORY_SIZE".#endif
3380: 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c .#if defined(SQL
3390: 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f ITE_SYSTEM_MALLO
33a0: 43 29 2b 64 65 66 69 6e 65 64 28 53 51 4c 49 54 C)+defined(SQLIT
33b0: 45 5f 4d 45 4d 44 45 42 55 47 29 2b 5c 0a 20 20 E_MEMDEBUG)+\.
33c0: 20 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 defined(SQLITE
33d0: 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 29 2b 64 65 _MEMORY_SIZE)+de
33e0: 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 4d 41 fined(SQLITE_MMA
33f0: 50 5f 48 45 41 50 5f 53 49 5a 45 29 2b 5c 0a 20 P_HEAP_SIZE)+\.
3400: 20 20 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 defined(SQLIT
3410: 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59 5f 53 49 E_POW2_MEMORY_SI
3420: 5a 45 29 3d 3d 30 0a 23 20 64 65 66 69 6e 65 20 ZE)==0.# define
3430: 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 SQLITE_SYSTEM_MA
3440: 4c 4c 4f 43 20 31 0a 23 65 6e 64 69 66 0a 0a 2f LLOC 1.#endif../
3450: 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4d *.** If SQLITE_M
3460: 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 ALLOC_SOFT_LIMIT
3470: 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 is not zero, th
3480: 65 6e 20 74 72 79 20 74 6f 20 6b 65 65 70 20 74 en try to keep t
3490: 68 65 0a 2a 2a 20 73 69 7a 65 73 20 6f 66 20 6d he.** sizes of m
34a0: 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
34b0: 73 20 62 65 6c 6f 77 20 74 68 69 73 20 76 61 6c s below this val
34c0: 75 65 20 77 68 65 72 65 20 70 6f 73 73 69 62 6c ue where possibl
34d0: 65 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e e..*/.#if !defin
34e0: 65 64 28 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 ed(SQLITE_MALLOC
34f0: 5f 53 4f 46 54 5f 4c 49 4d 49 54 29 0a 23 20 64 _SOFT_LIMIT).# d
3500: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 4c efine SQLITE_MAL
3510: 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 20 31 LOC_SOFT_LIMIT 1
3520: 30 32 34 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 024.#endif../*.*
3530: 2a 20 57 65 20 6e 65 65 64 20 74 6f 20 64 65 66 * We need to def
3540: 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 ine _XOPEN_SOURC
3550: 45 20 61 73 20 66 6f 6c 6c 6f 77 73 20 69 6e 20 E as follows in
3560: 6f 72 64 65 72 20 74 6f 20 65 6e 61 62 6c 65 0a order to enable.
3570: 2a 2a 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 ** recursive mut
3580: 65 78 65 73 20 6f 6e 20 6d 6f 73 74 20 55 6e 69 exes on most Uni
3590: 78 20 73 79 73 74 65 6d 73 2e 20 20 42 75 74 20 x systems. But
35a0: 4d 61 63 20 4f 53 20 58 20 69 73 20 64 69 66 66 Mac OS X is diff
35b0: 65 72 65 6e 74 2e 0a 2a 2a 20 54 68 65 20 5f 58 erent..** The _X
35c0: 4f 50 45 4e 5f 53 4f 55 52 43 45 20 64 65 66 69 OPEN_SOURCE defi
35d0: 6e 65 20 63 61 75 73 65 73 20 70 72 6f 62 6c 65 ne causes proble
35e0: 6d 73 20 66 6f 72 20 4d 61 63 20 4f 53 20 58 20 ms for Mac OS X
35f0: 77 65 20 61 72 65 20 74 6f 6c 64 2c 0a 2a 2a 20 we are told,.**
3600: 73 6f 20 69 74 20 69 73 20 6f 6d 69 74 74 65 64 so it is omitted
3610: 20 74 68 65 72 65 2e 20 20 53 65 65 20 74 69 63 there. See tic
3620: 6b 65 74 20 23 32 36 37 33 2e 0a 2a 2a 0a 2a 2a ket #2673..**.**
3630: 20 4c 61 74 65 72 20 77 65 20 6c 65 61 72 6e 20 Later we learn
3640: 74 68 61 74 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 that _XOPEN_SOUR
3650: 43 45 20 69 73 20 70 6f 6f 72 6c 79 20 6f 72 20 CE is poorly or
3660: 69 6e 63 6f 72 72 65 63 74 6c 79 0a 2a 2a 20 69 incorrectly.** i
3670: 6d 70 6c 65 6d 65 6e 74 65 64 20 6f 6e 20 73 6f mplemented on so
3680: 6d 65 20 73 79 73 74 65 6d 73 2e 20 20 53 6f 20 me systems. So
3690: 77 65 20 61 76 6f 69 64 20 64 65 66 69 6e 69 6e we avoid definin
36a0: 67 20 69 74 20 61 74 20 61 6c 6c 0a 2a 2a 20 69 g it at all.** i
36b0: 66 20 69 74 20 69 73 20 61 6c 72 65 61 64 79 20 f it is already
36c0: 64 65 66 69 6e 65 64 20 6f 72 20 69 66 20 69 74 defined or if it
36d0: 20 69 73 20 75 6e 6e 65 65 64 65 64 20 62 65 63 is unneeded bec
36e0: 61 75 73 65 20 77 65 20 61 72 65 0a 2a 2a 20 6e ause we are.** n
36f0: 6f 74 20 64 6f 69 6e 67 20 61 20 74 68 72 65 61 ot doing a threa
3700: 64 73 61 66 65 20 62 75 69 6c 64 2e 20 20 54 69 dsafe build. Ti
3710: 63 6b 65 74 20 23 32 36 38 31 2e 0a 2a 2a 0a 2a cket #2681..**.*
3720: 2a 20 53 65 65 20 61 6c 73 6f 20 74 69 63 6b 65 * See also ticke
3730: 74 20 23 32 37 34 31 2e 0a 2a 2f 0a 23 69 66 20 t #2741..*/.#if
3740: 21 64 65 66 69 6e 65 64 28 5f 58 4f 50 45 4e 5f !defined(_XOPEN_
3750: 53 4f 55 52 43 45 29 20 26 26 20 21 64 65 66 69 SOURCE) && !defi
3760: 6e 65 64 28 5f 5f 44 41 52 57 49 4e 5f 5f 29 20 ned(__DARWIN__)
3770: 26 26 20 21 64 65 66 69 6e 65 64 28 5f 5f 41 50 && !defined(__AP
3780: 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 PLE__) && SQLITE
3790: 5f 54 48 52 45 41 44 53 41 46 45 0a 23 20 20 64 _THREADSAFE.# d
37a0: 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 efine _XOPEN_SOU
37b0: 52 43 45 20 35 30 30 20 20 2f 2a 20 4e 65 65 64 RCE 500 /* Need
37c0: 65 64 20 74 6f 20 65 6e 61 62 6c 65 20 70 74 68 ed to enable pth
37d0: 72 65 61 64 20 72 65 63 75 72 73 69 76 65 20 6d read recursive m
37e0: 75 74 65 78 65 73 20 2a 2f 0a 23 65 6e 64 69 66 utexes */.#endif
37f0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54 43 4c 20 ../*.** The TCL
3800: 68 65 61 64 65 72 73 20 61 72 65 20 6f 6e 6c 79 headers are only
3810: 20 6e 65 65 64 65 64 20 77 68 65 6e 20 63 6f 6d needed when com
3820: 70 69 6c 69 6e 67 20 74 68 65 20 54 43 4c 20 62 piling the TCL b
3830: 69 6e 64 69 6e 67 73 2e 0a 2a 2f 0a 23 69 66 20 indings..*/.#if
3840: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 defined(SQLITE_T
3850: 43 4c 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 54 CL) || defined(T
3860: 43 4c 53 48 29 0a 23 20 69 6e 63 6c 75 64 65 20 CLSH).# include
3870: 3c 74 63 6c 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a <tcl.h>.#endif..
3880: 2f 2a 0a 2a 2a 20 4d 61 6e 79 20 70 65 6f 70 6c /*.** Many peopl
3890: 65 20 61 72 65 20 66 61 69 6c 69 6e 67 20 74 6f e are failing to
38a0: 20 73 65 74 20 2d 44 4e 44 45 42 55 47 3d 31 20 set -DNDEBUG=1
38b0: 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 53 when compiling S
38c0: 51 4c 69 74 65 2e 0a 2a 2a 20 53 65 74 74 69 6e QLite..** Settin
38d0: 67 20 4e 44 45 42 55 47 20 6d 61 6b 65 73 20 74 g NDEBUG makes t
38e0: 68 65 20 63 6f 64 65 20 73 6d 61 6c 6c 65 72 20 he code smaller
38f0: 61 6e 64 20 72 75 6e 20 66 61 73 74 65 72 2e 20 and run faster.
3900: 20 53 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e So the followin
3910: 67 0a 2a 2a 20 6c 69 6e 65 73 20 61 72 65 20 61 g.** lines are a
3920: 64 64 65 64 20 74 6f 20 61 75 74 6f 6d 61 74 69 dded to automati
3930: 63 61 6c 6c 79 20 73 65 74 20 4e 44 45 42 55 47 cally set NDEBUG
3940: 20 75 6e 6c 65 73 73 20 74 68 65 20 2d 44 53 51 unless the -DSQ
3950: 4c 49 54 45 5f 44 45 42 55 47 3d 31 0a 2a 2a 20 LITE_DEBUG=1.**
3960: 6f 70 74 69 6f 6e 20 69 73 20 73 65 74 2e 20 20 option is set.
3970: 54 68 75 73 20 4e 44 45 42 55 47 20 62 65 63 6f Thus NDEBUG beco
3980: 6d 65 73 20 61 6e 20 6f 70 74 2d 69 6e 20 72 61 mes an opt-in ra
3990: 74 68 65 72 20 74 68 61 6e 20 61 6e 20 6f 70 74 ther than an opt
39a0: 2d 6f 75 74 0a 2a 2a 20 66 65 61 74 75 72 65 2e -out.** feature.
39b0: 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 .*/.#if !defined
39c0: 28 4e 44 45 42 55 47 29 20 26 26 20 21 64 65 66 (NDEBUG) && !def
39d0: 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 ined(SQLITE_DEBU
39e0: 47 29 20 0a 23 20 64 65 66 69 6e 65 20 4e 44 45 G) .# define NDE
39f0: 42 55 47 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a BUG 1.#endif../*
3a00: 0a 2a 2a 20 54 68 65 20 74 65 73 74 63 61 73 65 .** The testcase
3a10: 28 29 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64 () macro is used
3a20: 20 74 6f 20 61 69 64 20 69 6e 20 63 6f 76 65 72 to aid in cover
3a30: 61 67 65 20 74 65 73 74 69 6e 67 2e 20 20 57 68 age testing. Wh
3a40: 65 6e 20 0a 2a 2a 20 64 6f 69 6e 67 20 63 6f 76 en .** doing cov
3a50: 65 72 61 67 65 20 74 65 73 74 69 6e 67 2c 20 74 erage testing, t
3a60: 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20 69 6e 73 he condition ins
3a70: 69 64 65 20 74 68 65 20 61 72 67 75 6d 65 6e 74 ide the argument
3a80: 20 74 6f 0a 2a 2a 20 74 65 73 74 63 61 73 65 28 to.** testcase(
3a90: 29 20 6d 75 73 74 20 62 65 20 65 76 61 6c 75 61 ) must be evalua
3aa0: 74 65 64 20 62 6f 74 68 20 74 72 75 65 20 61 6e ted both true an
3ab0: 64 20 66 61 6c 73 65 20 69 6e 20 6f 72 64 65 72 d false in order
3ac0: 20 74 6f 0a 2a 2a 20 67 65 74 20 66 75 6c 6c 20 to.** get full
3ad0: 62 72 61 6e 63 68 20 63 6f 76 65 72 61 67 65 2e branch coverage.
3ae0: 20 20 54 68 65 20 74 65 73 74 63 61 73 65 28 29 The testcase()
3af0: 20 6d 61 63 72 6f 20 69 73 20 69 6e 73 65 72 74 macro is insert
3b00: 65 64 0a 2a 2a 20 74 6f 20 68 65 6c 70 20 65 6e ed.** to help en
3b10: 73 75 72 65 20 61 64 65 71 75 61 74 65 20 74 65 sure adequate te
3b20: 73 74 20 63 6f 76 65 72 61 67 65 20 69 6e 20 70 st coverage in p
3b30: 6c 61 63 65 73 20 77 68 65 72 65 20 73 69 6d 70 laces where simp
3b40: 6c 65 0a 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e 2f le.** condition/
3b50: 64 65 63 69 73 69 6f 6e 20 63 6f 76 65 72 61 67 decision coverag
3b60: 65 20 69 73 20 69 6e 61 64 65 71 75 61 74 65 2e e is inadequate.
3b70: 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 For example, t
3b80: 65 73 74 63 61 73 65 28 29 0a 2a 2a 20 63 61 6e estcase().** can
3b90: 20 62 65 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 be used to make
3ba0: 20 73 75 72 65 20 62 6f 75 6e 64 61 72 79 20 76 sure boundary v
3bb0: 61 6c 75 65 73 20 61 72 65 20 74 65 73 74 65 64 alues are tested
3bc0: 2e 20 20 46 6f 72 0a 2a 2a 20 62 69 74 6d 61 73 . For.** bitmas
3bd0: 6b 20 74 65 73 74 73 2c 20 74 65 73 74 63 61 73 k tests, testcas
3be0: 65 28 29 20 63 61 6e 20 62 65 20 75 73 65 64 20 e() can be used
3bf0: 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 65 61 63 to make sure eac
3c00: 68 20 62 69 74 0a 2a 2a 20 69 73 20 73 69 67 6e h bit.** is sign
3c10: 69 66 69 63 61 6e 74 20 61 6e 64 20 75 73 65 64 ificant and used
3c20: 20 61 74 20 6c 65 61 73 74 20 6f 6e 63 65 2e 20 at least once.
3c30: 20 4f 6e 20 73 77 69 74 63 68 20 73 74 61 74 65 On switch state
3c40: 6d 65 6e 74 73 0a 2a 2a 20 77 68 65 72 65 20 6d ments.** where m
3c50: 75 6c 74 69 70 6c 65 20 63 61 73 65 73 20 67 6f ultiple cases go
3c60: 20 74 6f 20 74 68 65 20 73 61 6d 65 20 62 6c 6f to the same blo
3c70: 63 6b 20 6f 66 20 63 6f 64 65 2c 20 74 65 73 74 ck of code, test
3c80: 63 61 73 65 28 29 0a 2a 2a 20 63 61 6e 20 69 6e case().** can in
3c90: 73 75 72 65 20 74 68 61 74 20 61 6c 6c 20 63 61 sure that all ca
3ca0: 73 65 73 20 61 72 65 20 65 76 61 6c 75 61 74 65 ses are evaluate
3cb0: 64 2e 0a 2a 2a 0a 2a 2f 0a 23 69 66 64 65 66 20 d..**.*/.#ifdef
3cc0: 53 51 4c 49 54 45 5f 43 4f 56 45 52 41 47 45 5f SQLITE_COVERAGE_
3cd0: 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 TEST.SQLITE_PRIV
3ce0: 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
3cf0: 65 33 43 6f 76 65 72 61 67 65 28 69 6e 74 29 3b e3Coverage(int);
3d00: 0a 23 20 64 65 66 69 6e 65 20 74 65 73 74 63 61 .# define testca
3d10: 73 65 28 58 29 20 20 69 66 28 20 58 20 29 7b 20 se(X) if( X ){
3d20: 73 71 6c 69 74 65 33 43 6f 76 65 72 61 67 65 28 sqlite3Coverage(
3d30: 5f 5f 4c 49 4e 45 5f 5f 29 3b 20 7d 0a 23 65 6c __LINE__); }.#el
3d40: 73 65 0a 23 20 64 65 66 69 6e 65 20 74 65 73 74 se.# define test
3d50: 63 61 73 65 28 58 29 0a 23 65 6e 64 69 66 0a 0a case(X).#endif..
3d60: 2f 2a 0a 2a 2a 20 54 68 65 20 54 45 53 54 4f 4e /*.** The TESTON
3d70: 4c 59 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64 LY macro is used
3d80: 20 74 6f 20 65 6e 63 6c 6f 73 65 20 76 61 72 69 to enclose vari
3d90: 61 62 6c 65 20 64 65 63 6c 61 72 61 74 69 6f 6e able declaration
3da0: 73 20 6f 72 0a 2a 2a 20 6f 74 68 65 72 20 62 69 s or.** other bi
3db0: 74 73 20 6f 66 20 63 6f 64 65 20 74 68 61 74 20 ts of code that
3dc0: 61 72 65 20 6e 65 65 64 65 64 20 74 6f 20 73 75 are needed to su
3dd0: 70 70 6f 72 74 20 74 68 65 20 61 72 67 75 6d 65 pport the argume
3de0: 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 65 nts.** within te
3df0: 73 74 63 61 73 65 28 29 20 61 6e 64 20 61 73 73 stcase() and ass
3e00: 65 72 74 28 29 20 6d 61 63 72 6f 73 2e 0a 2a 2f ert() macros..*/
3e10: 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 .#if !defined(ND
3e20: 45 42 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 EBUG) || defined
3e30: 28 53 51 4c 49 54 45 5f 43 4f 56 45 52 41 47 45 (SQLITE_COVERAGE
3e40: 5f 54 45 53 54 29 0a 23 20 64 65 66 69 6e 65 20 _TEST).# define
3e50: 54 45 53 54 4f 4e 4c 59 28 58 29 20 20 58 0a 23 TESTONLY(X) X.#
3e60: 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 54 45 else.# define TE
3e70: 53 54 4f 4e 4c 59 28 58 29 0a 23 65 6e 64 69 66 STONLY(X).#endif
3e80: 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 ../*.** Sometime
3e90: 73 20 77 65 20 6e 65 65 64 20 61 20 73 6d 61 6c s we need a smal
3ea0: 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 63 6f 64 65 l amount of code
3eb0: 20 73 75 63 68 20 61 73 20 61 20 76 61 72 69 61 such as a varia
3ec0: 62 6c 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 ble initializati
3ed0: 6f 6e 0a 2a 2a 20 74 6f 20 73 65 74 75 70 20 66 on.** to setup f
3ee0: 6f 72 20 61 20 6c 61 74 65 72 20 61 73 73 65 72 or a later asser
3ef0: 74 28 29 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 t() statement.
3f00: 57 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 We do not want t
3f10: 68 69 73 20 63 6f 64 65 20 74 6f 0a 2a 2a 20 61 his code to.** a
3f20: 70 70 65 61 72 20 77 68 65 6e 20 61 73 73 65 72 ppear when asser
3f30: 74 28 29 20 69 73 20 64 69 73 61 62 6c 65 64 2e t() is disabled.
3f40: 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 The following
3f50: 6d 61 63 72 6f 20 69 73 20 74 68 65 72 65 66 6f macro is therefo
3f60: 72 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 63 6f re.** used to co
3f70: 6e 74 61 69 6e 20 74 68 61 74 20 73 65 74 75 70 ntain that setup
3f80: 20 63 6f 64 65 2e 20 20 54 68 65 20 22 56 56 41 code. The "VVA
3f90: 22 20 61 63 72 6f 6e 79 6d 20 73 74 61 6e 64 73 " acronym stands
3fa0: 20 66 6f 72 0a 2a 2a 20 22 56 65 72 69 66 69 63 for.** "Verific
3fb0: 61 74 69 6f 6e 2c 20 56 61 6c 69 64 61 74 69 6f ation, Validatio
3fc0: 6e 2c 20 61 6e 64 20 41 63 63 72 65 64 69 74 61 n, and Accredita
3fd0: 74 69 6f 6e 22 2e 20 20 49 6e 20 6f 74 68 65 72 tion". In other
3fe0: 20 77 6f 72 64 73 2c 20 74 68 65 0a 2a 2a 20 63 words, the.** c
3ff0: 6f 64 65 20 77 69 74 68 69 6e 20 56 56 41 5f 4f ode within VVA_O
4000: 4e 4c 59 28 29 20 77 69 6c 6c 20 6f 6e 6c 79 20 NLY() will only
4010: 72 75 6e 20 64 75 72 69 6e 67 20 76 65 72 69 66 run during verif
4020: 69 63 61 74 69 6f 6e 20 70 72 6f 63 65 73 73 65 ication processe
4030: 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44 s..*/.#ifndef ND
4040: 45 42 55 47 0a 23 20 64 65 66 69 6e 65 20 56 56 EBUG.# define VV
4050: 41 5f 4f 4e 4c 59 28 58 29 20 20 58 0a 23 65 6c A_ONLY(X) X.#el
4060: 73 65 0a 23 20 64 65 66 69 6e 65 20 56 56 41 5f se.# define VVA_
4070: 4f 4e 4c 59 28 58 29 0a 23 65 6e 64 69 66 0a 0a ONLY(X).#endif..
4080: 2f 2a 0a 2a 2a 20 54 68 65 20 41 4c 57 41 59 53 /*.** The ALWAYS
4090: 20 61 6e 64 20 4e 45 56 45 52 20 6d 61 63 72 6f and NEVER macro
40a0: 73 20 73 75 72 72 6f 75 6e 64 20 62 6f 6f 6c 65 s surround boole
40b0: 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 73 20 77 an expressions w
40c0: 68 69 63 68 20 0a 2a 2a 20 61 72 65 20 69 6e 74 hich .** are int
40d0: 65 6e 64 65 64 20 74 6f 20 61 6c 77 61 79 73 20 ended to always
40e0: 62 65 20 74 72 75 65 20 6f 72 20 66 61 6c 73 65 be true or false
40f0: 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 , respectively.
4100: 20 53 75 63 68 0a 2a 2a 20 65 78 70 72 65 73 73 Such.** express
4110: 69 6f 6e 73 20 63 6f 75 6c 64 20 62 65 20 6f 6d ions could be om
4120: 69 74 74 65 64 20 66 72 6f 6d 20 74 68 65 20 63 itted from the c
4130: 6f 64 65 20 63 6f 6d 70 6c 65 74 65 6c 79 2e 20 ode completely.
4140: 20 42 75 74 20 74 68 65 79 0a 2a 2a 20 61 72 65 But they.** are
4150: 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 61 20 66 included in a f
4160: 65 77 20 63 61 73 65 73 20 69 6e 20 6f 72 64 65 ew cases in orde
4170: 72 20 74 6f 20 65 6e 68 61 6e 63 65 20 74 68 65 r to enhance the
4180: 20 72 65 73 69 6c 69 65 6e 63 65 0a 2a 2a 20 6f resilience.** o
4190: 66 20 53 51 4c 69 74 65 20 74 6f 20 75 6e 65 78 f SQLite to unex
41a0: 70 65 63 74 65 64 20 62 65 68 61 76 69 6f 72 20 pected behavior
41b0: 2d 20 74 6f 20 6d 61 6b 65 20 74 68 65 20 63 6f - to make the co
41c0: 64 65 20 22 73 65 6c 66 2d 68 65 61 6c 69 6e 67 de "self-healing
41d0: 22 0a 2a 2a 20 6f 72 20 22 64 75 63 74 69 6c 65 ".** or "ductile
41e0: 22 20 72 61 74 68 65 72 20 74 68 61 6e 20 62 65 " rather than be
41f0: 69 6e 67 20 22 62 72 69 74 74 6c 65 22 20 61 6e ing "brittle" an
4200: 64 20 63 72 61 73 68 69 6e 67 20 61 74 20 74 68 d crashing at th
4210: 65 20 66 69 72 73 74 0a 2a 2a 20 68 69 6e 74 20 e first.** hint
4220: 6f 66 20 75 6e 70 6c 61 6e 6e 65 64 20 62 65 68 of unplanned beh
4230: 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 avior..**.** In
4240: 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 41 4c 57 other words, ALW
4250: 41 59 53 20 61 6e 64 20 4e 45 56 45 52 20 61 72 AYS and NEVER ar
4260: 65 20 61 64 64 65 64 20 66 6f 72 20 64 65 66 65 e added for defe
4270: 6e 73 69 76 65 20 63 6f 64 65 2e 0a 2a 2a 0a 2a nsive code..**.*
4280: 2a 20 57 68 65 6e 20 64 6f 69 6e 67 20 63 6f 76 * When doing cov
4290: 65 72 61 67 65 20 74 65 73 74 69 6e 67 20 41 4c erage testing AL
42a0: 57 41 59 53 20 61 6e 64 20 4e 45 56 45 52 20 61 WAYS and NEVER a
42b0: 72 65 20 68 61 72 64 2d 63 6f 64 65 64 20 74 6f re hard-coded to
42c0: 0a 2a 2a 20 62 65 20 74 72 75 65 20 61 6e 64 20 .** be true and
42d0: 66 61 6c 73 65 20 73 6f 20 74 68 61 74 20 74 68 false so that th
42e0: 65 20 75 6e 72 65 61 63 68 61 62 6c 65 20 63 6f e unreachable co
42f0: 64 65 20 74 68 65 6e 20 73 70 65 63 69 66 79 20 de then specify
4300: 77 69 6c 6c 0a 2a 2a 20 6e 6f 74 20 62 65 20 63 will.** not be c
4310: 6f 75 6e 74 65 64 20 61 73 20 75 6e 74 65 73 74 ounted as untest
4320: 65 64 20 63 6f 64 65 2e 0a 2a 2f 0a 23 69 66 20 ed code..*/.#if
4330: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 defined(SQLITE_C
4340: 4f 56 45 52 41 47 45 5f 54 45 53 54 29 0a 23 20 OVERAGE_TEST).#
4350: 64 65 66 69 6e 65 20 41 4c 57 41 59 53 28 58 29 define ALWAYS(X)
4360: 20 20 20 20 20 20 28 31 29 0a 23 20 64 65 66 69 (1).# defi
4370: 6e 65 20 4e 45 56 45 52 28 58 29 20 20 20 20 20 ne NEVER(X)
4380: 20 20 28 30 29 0a 23 65 6c 69 66 20 21 64 65 66 (0).#elif !def
4390: 69 6e 65 64 28 4e 44 45 42 55 47 29 0a 23 20 64 ined(NDEBUG).# d
43a0: 65 66 69 6e 65 20 41 4c 57 41 59 53 28 58 29 20 efine ALWAYS(X)
43b0: 20 20 20 20 20 28 28 58 29 3f 31 3a 28 61 73 73 ((X)?1:(ass
43c0: 65 72 74 28 30 29 2c 30 29 29 0a 23 20 64 65 66 ert(0),0)).# def
43d0: 69 6e 65 20 4e 45 56 45 52 28 58 29 20 20 20 20 ine NEVER(X)
43e0: 20 20 20 28 28 58 29 3f 28 61 73 73 65 72 74 28 ((X)?(assert(
43f0: 30 29 2c 31 29 3a 30 29 0a 23 65 6c 73 65 0a 23 0),1):0).#else.#
4400: 20 64 65 66 69 6e 65 20 41 4c 57 41 59 53 28 58 define ALWAYS(X
4410: 29 20 20 20 20 20 20 28 58 29 0a 23 20 64 65 66 ) (X).# def
4420: 69 6e 65 20 4e 45 56 45 52 28 58 29 20 20 20 20 ine NEVER(X)
4430: 20 20 20 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f (X).#endif../
4440: 2a 0a 2a 2a 20 54 68 65 20 6d 61 63 72 6f 20 75 *.** The macro u
4450: 6e 6c 69 6b 65 6c 79 28 29 20 69 73 20 61 20 68 nlikely() is a h
4460: 69 6e 74 20 74 68 61 74 20 73 75 72 72 6f 75 6e int that surroun
4470: 64 73 20 61 20 62 6f 6f 6c 65 61 6e 0a 2a 2a 20 ds a boolean.**
4480: 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 expression that
4490: 69 73 20 75 73 75 61 6c 6c 79 20 66 61 6c 73 65 is usually false
44a0: 2e 20 20 4d 61 63 72 6f 20 6c 69 6b 65 6c 79 28 . Macro likely(
44b0: 29 20 73 75 72 72 6f 75 6e 64 73 0a 2a 2a 20 61 ) surrounds.** a
44c0: 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65 73 73 boolean express
44d0: 69 6f 6e 20 74 68 61 74 20 69 73 20 75 73 75 61 ion that is usua
44e0: 6c 6c 79 20 74 72 75 65 2e 20 20 47 43 43 20 69 lly true. GCC i
44f0: 73 20 61 62 6c 65 20 74 6f 0a 2a 2a 20 75 73 65 s able to.** use
4500: 20 74 68 65 73 65 20 68 69 6e 74 73 20 74 6f 20 these hints to
4510: 67 65 6e 65 72 61 74 65 20 62 65 74 74 65 72 20 generate better
4520: 63 6f 64 65 2c 20 73 6f 6d 65 74 69 6d 65 73 2e code, sometimes.
4530: 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 .*/.#if defined(
4540: 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 30 0a 23 __GNUC__) && 0.#
4550: 20 64 65 66 69 6e 65 20 6c 69 6b 65 6c 79 28 58 define likely(X
4560: 29 20 20 20 20 5f 5f 62 75 69 6c 74 69 6e 5f 65 ) __builtin_e
4570: 78 70 65 63 74 28 28 58 29 2c 31 29 0a 23 20 64 xpect((X),1).# d
4580: 65 66 69 6e 65 20 75 6e 6c 69 6b 65 6c 79 28 58 efine unlikely(X
4590: 29 20 20 5f 5f 62 75 69 6c 74 69 6e 5f 65 78 70 ) __builtin_exp
45a0: 65 63 74 28 28 58 29 2c 30 29 0a 23 65 6c 73 65 ect((X),0).#else
45b0: 0a 23 20 64 65 66 69 6e 65 20 6c 69 6b 65 6c 79 .# define likely
45c0: 28 58 29 20 20 20 20 21 21 28 58 29 0a 23 20 64 (X) !!(X).# d
45d0: 65 66 69 6e 65 20 75 6e 6c 69 6b 65 6c 79 28 58 efine unlikely(X
45e0: 29 20 20 21 21 28 58 29 0a 23 65 6e 64 69 66 0a ) !!(X).#endif.
45f0: 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
4600: 20 49 6e 63 6c 75 64 65 20 73 71 6c 69 74 65 33 Include sqlite3
4610: 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 .h in the middle
4620: 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 of sqliteInt.h
4630: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
4640: 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
4650: 20 42 65 67 69 6e 20 66 69 6c 65 20 73 71 6c 69 Begin file sqli
4660: 74 65 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a te3.h **********
4670: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4680: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
4690: 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 ./*.** 2001 Sept
46a0: 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 ember 15.**.** T
46b0: 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
46c0: 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
46d0: 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
46e0: 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
46f0: 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
4700: 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
4710: 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
4720: 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
4730: 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
4740: 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
4750: 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
4760: 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
4770: 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
4780: 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
4790: 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
47a0: 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
47b0: 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
47c0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
47d0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
47e0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
47f0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4800: 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 *******.** This
4810: 68 65 61 64 65 72 20 66 69 6c 65 20 64 65 66 69 header file defi
4820: 6e 65 73 20 74 68 65 20 69 6e 74 65 72 66 61 63 nes the interfac
4830: 65 20 74 68 61 74 20 74 68 65 20 53 51 4c 69 74 e that the SQLit
4840: 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 70 72 65 e library.** pre
4850: 73 65 6e 74 73 20 74 6f 20 63 6c 69 65 6e 74 20 sents to client
4860: 70 72 6f 67 72 61 6d 73 2e 20 20 49 66 20 61 20 programs. If a
4870: 43 2d 66 75 6e 63 74 69 6f 6e 2c 20 73 74 72 75 C-function, stru
4880: 63 74 75 72 65 2c 20 64 61 74 61 74 79 70 65 2c cture, datatype,
4890: 0a 2a 2a 20 6f 72 20 63 6f 6e 73 74 61 6e 74 20 .** or constant
48a0: 64 65 66 69 6e 69 74 69 6f 6e 20 64 6f 65 73 20 definition does
48b0: 6e 6f 74 20 61 70 70 65 61 72 20 69 6e 20 74 68 not appear in th
48c0: 69 73 20 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 is file, then it
48d0: 20 69 73 0a 2a 2a 20 6e 6f 74 20 61 20 70 75 62 is.** not a pub
48e0: 6c 69 73 68 65 64 20 41 50 49 20 6f 66 20 53 51 lished API of SQ
48f0: 4c 69 74 65 2c 20 69 73 20 73 75 62 6a 65 63 74 Lite, is subject
4900: 20 74 6f 20 63 68 61 6e 67 65 20 77 69 74 68 6f to change witho
4910: 75 74 0a 2a 2a 20 6e 6f 74 69 63 65 2c 20 61 6e ut.** notice, an
4920: 64 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 d should not be
4930: 72 65 66 65 72 65 6e 63 65 64 20 62 79 20 70 72 referenced by pr
4940: 6f 67 72 61 6d 73 20 74 68 61 74 20 75 73 65 20 ograms that use
4950: 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 53 6f SQLite..**.** So
4960: 6d 65 20 6f 66 20 74 68 65 20 64 65 66 69 6e 69 me of the defini
4970: 74 69 6f 6e 73 20 74 68 61 74 20 61 72 65 20 69 tions that are i
4980: 6e 20 74 68 69 73 20 66 69 6c 65 20 61 72 65 20 n this file are
4990: 6d 61 72 6b 65 64 20 61 73 0a 2a 2a 20 22 65 78 marked as.** "ex
49a0: 70 65 72 69 6d 65 6e 74 61 6c 22 2e 20 20 45 78 perimental". Ex
49b0: 70 65 72 69 6d 65 6e 74 61 6c 20 69 6e 74 65 72 perimental inter
49c0: 66 61 63 65 73 20 61 72 65 20 6e 6f 72 6d 61 6c faces are normal
49d0: 6c 79 20 6e 65 77 0a 2a 2a 20 66 65 61 74 75 72 ly new.** featur
49e0: 65 73 20 72 65 63 65 6e 74 6c 79 20 61 64 64 65 es recently adde
49f0: 64 20 74 6f 20 53 51 4c 69 74 65 2e 20 20 57 65 d to SQLite. We
4a00: 20 64 6f 20 6e 6f 74 20 61 6e 74 69 63 69 70 61 do not anticipa
4a10: 74 65 20 63 68 61 6e 67 65 73 0a 2a 2a 20 74 6f te changes.** to
4a20: 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 69 6e experimental in
4a30: 74 65 72 66 61 63 65 73 20 62 75 74 20 72 65 73 terfaces but res
4a40: 65 72 76 65 20 74 68 65 20 72 69 67 68 74 20 74 erve the right t
4a50: 6f 20 6d 61 6b 65 20 6d 69 6e 6f 72 20 63 68 61 o make minor cha
4a60: 6e 67 65 73 0a 2a 2a 20 69 66 20 65 78 70 65 72 nges.** if exper
4a70: 69 65 6e 63 65 20 66 72 6f 6d 20 75 73 65 20 22 ience from use "
4a80: 69 6e 20 74 68 65 20 77 69 6c 64 22 20 73 75 67 in the wild" sug
4a90: 67 65 73 74 20 73 75 63 68 20 63 68 61 6e 67 65 gest such change
4aa0: 73 20 61 72 65 20 70 72 75 64 65 6e 74 2e 0a 2a s are prudent..*
4ab0: 2a 0a 2a 2a 20 54 68 65 20 6f 66 66 69 63 69 61 *.** The officia
4ac0: 6c 20 43 2d 6c 61 6e 67 75 61 67 65 20 41 50 49 l C-language API
4ad0: 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 documentation f
4ae0: 6f 72 20 53 51 4c 69 74 65 20 69 73 20 64 65 72 or SQLite is der
4af0: 69 76 65 64 0a 2a 2a 20 66 72 6f 6d 20 63 6f 6d ived.** from com
4b00: 6d 65 6e 74 73 20 69 6e 20 74 68 69 73 20 66 69 ments in this fi
4b10: 6c 65 2e 20 20 54 68 69 73 20 66 69 6c 65 20 69 le. This file i
4b20: 73 20 74 68 65 20 61 75 74 68 6f 72 69 74 61 74 s the authoritat
4b30: 69 76 65 20 73 6f 75 72 63 65 0a 2a 2a 20 6f 6e ive source.** on
4b40: 20 68 6f 77 20 53 51 4c 69 74 65 20 69 6e 74 65 how SQLite inte
4b50: 72 66 61 63 65 73 20 61 72 65 20 73 75 70 70 6f rfaces are suppo
4b60: 73 65 20 74 6f 20 6f 70 65 72 61 74 65 2e 0a 2a se to operate..*
4b70: 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 *.** The name of
4b80: 20 74 68 69 73 20 66 69 6c 65 20 75 6e 64 65 72 this file under
4b90: 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6d configuration m
4ba0: 61 6e 61 67 65 6d 65 6e 74 20 69 73 20 22 73 71 anagement is "sq
4bb0: 6c 69 74 65 2e 68 2e 69 6e 22 2e 0a 2a 2a 20 54 lite.h.in"..** T
4bc0: 68 65 20 6d 61 6b 65 66 69 6c 65 20 6d 61 6b 65 he makefile make
4bd0: 73 20 73 6f 6d 65 20 6d 69 6e 6f 72 20 63 68 61 s some minor cha
4be0: 6e 67 65 73 20 74 6f 20 74 68 69 73 20 66 69 6c nges to this fil
4bf0: 65 20 28 73 75 63 68 20 61 73 20 69 6e 73 65 72 e (such as inser
4c00: 74 69 6e 67 0a 2a 2a 20 74 68 65 20 76 65 72 73 ting.** the vers
4c10: 69 6f 6e 20 6e 75 6d 62 65 72 29 20 61 6e 64 20 ion number) and
4c20: 63 68 61 6e 67 65 73 20 69 74 73 20 6e 61 6d 65 changes its name
4c30: 20 74 6f 20 22 73 71 6c 69 74 65 33 2e 68 22 20 to "sqlite3.h"
4c40: 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 74 68 as.** part of th
4c50: 65 20 62 75 69 6c 64 20 70 72 6f 63 65 73 73 2e e build process.
4c60: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c .*/.#ifndef _SQL
4c70: 49 54 45 33 5f 48 5f 0a 23 64 65 66 69 6e 65 20 ITE3_H_.#define
4c80: 5f 53 51 4c 49 54 45 33 5f 48 5f 0a 23 69 6e 63 _SQLITE3_H_.#inc
4c90: 6c 75 64 65 20 3c 73 74 64 61 72 67 2e 68 3e 20 lude <stdarg.h>
4ca0: 20 20 20 20 2f 2a 20 4e 65 65 64 65 64 20 66 6f /* Needed fo
4cb0: 72 20 74 68 65 20 64 65 66 69 6e 69 74 69 6f 6e r the definition
4cc0: 20 6f 66 20 76 61 5f 6c 69 73 74 20 2a 2f 0a 0a of va_list */..
4cd0: 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 /*.** Make sure
4ce0: 77 65 20 63 61 6e 20 63 61 6c 6c 20 74 68 69 73 we can call this
4cf0: 20 73 74 75 66 66 20 66 72 6f 6d 20 43 2b 2b 2e stuff from C++.
4d00: 0a 2a 2f 0a 23 69 66 20 30 0a 65 78 74 65 72 6e .*/.#if 0.extern
4d10: 20 22 43 22 20 7b 0a 23 65 6e 64 69 66 0a 0a 0a "C" {.#endif...
4d20: 2f 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20 61 62 /*.** Add the ab
4d30: 69 6c 69 74 79 20 74 6f 20 6f 76 65 72 72 69 64 ility to overrid
4d40: 65 20 27 65 78 74 65 72 6e 27 0a 2a 2f 0a 23 69 e 'extern'.*/.#i
4d50: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 45 58 54 fndef SQLITE_EXT
4d60: 45 52 4e 0a 23 20 64 65 66 69 6e 65 20 53 51 4c ERN.# define SQL
4d70: 49 54 45 5f 45 58 54 45 52 4e 20 65 78 74 65 72 ITE_EXTERN exter
4d80: 6e 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 n.#endif..#ifnde
4d90: 66 20 53 51 4c 49 54 45 5f 41 50 49 0a 23 20 64 f SQLITE_API.# d
4da0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 50 49 efine SQLITE_API
4db0: 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 .#endif.../*.**
4dc0: 54 68 65 73 65 20 6e 6f 2d 6f 70 20 6d 61 63 72 These no-op macr
4dd0: 6f 73 20 61 72 65 20 75 73 65 64 20 69 6e 20 66 os are used in f
4de0: 72 6f 6e 74 20 6f 66 20 69 6e 74 65 72 66 61 63 ront of interfac
4df0: 65 73 20 74 6f 20 6d 61 72 6b 20 74 68 6f 73 65 es to mark those
4e00: 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 20 61 .** interfaces a
4e10: 73 20 65 69 74 68 65 72 20 64 65 70 72 65 63 61 s either depreca
4e20: 74 65 64 20 6f 72 20 65 78 70 65 72 69 6d 65 6e ted or experimen
4e30: 74 61 6c 2e 20 20 4e 65 77 20 61 70 70 6c 69 63 tal. New applic
4e40: 61 74 69 6f 6e 73 0a 2a 2a 20 73 68 6f 75 6c 64 ations.** should
4e50: 20 6e 6f 74 20 75 73 65 20 64 65 70 72 65 63 61 not use depreca
4e60: 74 65 64 20 69 6e 74 65 72 66 61 63 65 73 20 2d ted interfaces -
4e70: 20 74 68 65 79 20 61 72 65 20 73 75 70 70 6f 72 they are suppor
4e80: 74 20 66 6f 72 20 62 61 63 6b 77 61 72 64 73 0a t for backwards.
4e90: 2a 2a 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 ** compatibility
4ea0: 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61 74 only. Applicat
4eb0: 69 6f 6e 20 77 72 69 74 65 72 73 20 73 68 6f 75 ion writers shou
4ec0: 6c 64 20 62 65 20 61 77 61 72 65 20 74 68 61 74 ld be aware that
4ed0: 0a 2a 2a 20 65 78 70 65 72 69 6d 65 6e 74 61 6c .** experimental
4ee0: 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 20 interfaces are
4ef0: 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 subject to chang
4f00: 65 20 69 6e 20 70 6f 69 6e 74 20 72 65 6c 65 61 e in point relea
4f10: 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 ses..**.** These
4f20: 20 6d 61 63 72 6f 73 20 75 73 65 64 20 74 6f 20 macros used to
4f30: 72 65 73 6f 6c 76 65 20 74 6f 20 76 61 72 69 6f resolve to vario
4f40: 75 73 20 6b 69 6e 64 73 20 6f 66 20 63 6f 6d 70 us kinds of comp
4f50: 69 6c 65 72 20 6d 61 67 69 63 20 74 68 61 74 0a iler magic that.
4f60: 2a 2a 20 77 6f 75 6c 64 20 67 65 6e 65 72 61 74 ** would generat
4f70: 65 20 77 61 72 6e 69 6e 67 20 6d 65 73 73 61 67 e warning messag
4f80: 65 73 20 77 68 65 6e 20 74 68 65 79 20 77 65 72 es when they wer
4f90: 65 20 75 73 65 64 2e 20 20 42 75 74 20 74 68 61 e used. But tha
4fa0: 74 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 20 6d 61 t.** compiler ma
4fb0: 67 69 63 20 65 6e 64 65 64 20 75 70 20 67 65 6e gic ended up gen
4fc0: 65 72 61 74 69 6e 67 20 73 75 63 68 20 61 20 66 erating such a f
4fd0: 6c 75 72 72 79 20 6f 66 20 62 75 67 20 72 65 70 lurry of bug rep
4fe0: 6f 72 74 73 0a 2a 2a 20 74 68 61 74 20 77 65 20 orts.** that we
4ff0: 68 61 76 65 20 74 61 6b 65 6e 20 69 74 20 61 6c have taken it al
5000: 6c 20 6f 75 74 20 61 6e 64 20 67 6f 6e 65 20 62 l out and gone b
5010: 61 63 6b 20 74 6f 20 75 73 69 6e 67 20 73 69 6d ack to using sim
5020: 70 6c 65 0a 2a 2a 20 6e 6f 6f 70 20 6d 61 63 72 ple.** noop macr
5030: 6f 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 os..*/.#define S
5040: 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 44 QLITE_DEPRECATED
5050: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
5060: 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 0a 2f 2a EXPERIMENTAL../*
5070: 0a 2a 2a 20 45 6e 73 75 72 65 20 74 68 65 73 65 .** Ensure these
5080: 20 73 79 6d 62 6f 6c 73 20 77 65 72 65 20 6e 6f symbols were no
5090: 74 20 64 65 66 69 6e 65 64 20 62 79 20 73 6f 6d t defined by som
50a0: 65 20 70 72 65 76 69 6f 75 73 20 68 65 61 64 65 e previous heade
50b0: 72 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66 64 65 r file..*/.#ifde
50c0: 66 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e f SQLITE_VERSION
50d0: 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f .# undef SQLITE_
50e0: 56 45 52 53 49 4f 4e 0a 23 65 6e 64 69 66 0a 23 VERSION.#endif.#
50f0: 69 66 64 65 66 20 53 51 4c 49 54 45 5f 56 45 52 ifdef SQLITE_VER
5100: 53 49 4f 4e 5f 4e 55 4d 42 45 52 0a 23 20 75 6e SION_NUMBER.# un
5110: 64 65 66 20 53 51 4c 49 54 45 5f 56 45 52 53 49 def SQLITE_VERSI
5120: 4f 4e 5f 4e 55 4d 42 45 52 0a 23 65 6e 64 69 66 ON_NUMBER.#endif
5130: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
5140: 3a 20 43 6f 6d 70 69 6c 65 2d 54 69 6d 65 20 4c : Compile-Time L
5150: 69 62 72 61 72 79 20 56 65 72 73 69 6f 6e 20 4e ibrary Version N
5160: 75 6d 62 65 72 73 20 7b 48 31 30 30 31 30 7d 20 umbers {H10010}
5170: 3c 53 36 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S60100>.**.** T
5180: 68 65 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f he SQLITE_VERSIO
5190: 4e 20 61 6e 64 20 53 51 4c 49 54 45 5f 56 45 52 N and SQLITE_VER
51a0: 53 49 4f 4e 5f 4e 55 4d 42 45 52 20 23 64 65 66 SION_NUMBER #def
51b0: 69 6e 65 73 20 69 6e 0a 2a 2a 20 74 68 65 20 73 ines in.** the s
51c0: 71 6c 69 74 65 33 2e 68 20 66 69 6c 65 20 73 70 qlite3.h file sp
51d0: 65 63 69 66 79 20 74 68 65 20 76 65 72 73 69 6f ecify the versio
51e0: 6e 20 6f 66 20 53 51 4c 69 74 65 20 77 69 74 68 n of SQLite with
51f0: 20 77 68 69 63 68 0a 2a 2a 20 74 68 61 74 20 68 which.** that h
5200: 65 61 64 65 72 20 66 69 6c 65 20 69 73 20 61 73 eader file is as
5210: 73 6f 63 69 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 sociated..**.**
5220: 54 68 65 20 22 76 65 72 73 69 6f 6e 22 20 6f 66 The "version" of
5230: 20 53 51 4c 69 74 65 20 69 73 20 61 20 73 74 72 SQLite is a str
5240: 69 6e 67 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 ing of the form
5250: 22 57 2e 58 2e 59 22 20 6f 72 20 22 57 2e 58 2e "W.X.Y" or "W.X.
5260: 59 2e 5a 22 2e 0a 2a 2a 20 54 68 65 20 57 20 76 Y.Z"..** The W v
5270: 61 6c 75 65 20 69 73 20 6d 61 6a 6f 72 20 76 65 alue is major ve
5280: 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 61 6e 64 rsion number and
5290: 20 69 73 20 61 6c 77 61 79 73 20 33 20 69 6e 20 is always 3 in
52a0: 53 51 4c 69 74 65 33 2e 0a 2a 2a 20 54 68 65 20 SQLite3..** The
52b0: 57 20 76 61 6c 75 65 20 6f 6e 6c 79 20 63 68 61 W value only cha
52c0: 6e 67 65 73 20 77 68 65 6e 20 62 61 63 6b 77 61 nges when backwa
52d0: 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 rds compatibilit
52e0: 79 20 69 73 0a 2a 2a 20 62 72 6f 6b 65 6e 20 61 y is.** broken a
52f0: 6e 64 20 77 65 20 69 6e 74 65 6e 64 20 74 6f 20 nd we intend to
5300: 6e 65 76 65 72 20 62 72 65 61 6b 20 62 61 63 6b never break back
5310: 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c wards compatibil
5320: 69 74 79 2e 0a 2a 2a 20 54 68 65 20 58 20 76 61 ity..** The X va
5330: 6c 75 65 20 69 73 20 74 68 65 20 6d 69 6e 6f 72 lue is the minor
5340: 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 version number
5350: 61 6e 64 20 6f 6e 6c 79 20 63 68 61 6e 67 65 73 and only changes
5360: 20 77 68 65 6e 0a 2a 2a 20 74 68 65 72 65 20 61 when.** there a
5370: 72 65 20 6d 61 6a 6f 72 20 66 65 61 74 75 72 65 re major feature
5380: 20 65 6e 68 61 6e 63 65 6d 65 6e 74 73 20 74 68 enhancements th
5390: 61 74 20 61 72 65 20 66 6f 72 77 61 72 64 73 20 at are forwards
53a0: 63 6f 6d 70 61 74 69 62 6c 65 0a 2a 2a 20 62 75 compatible.** bu
53b0: 74 20 6e 6f 74 20 62 61 63 6b 77 61 72 64 73 20 t not backwards
53c0: 63 6f 6d 70 61 74 69 62 6c 65 2e 0a 2a 2a 20 54 compatible..** T
53d0: 68 65 20 59 20 76 61 6c 75 65 20 69 73 20 74 68 he Y value is th
53e0: 65 20 72 65 6c 65 61 73 65 20 6e 75 6d 62 65 72 e release number
53f0: 20 61 6e 64 20 69 73 20 69 6e 63 72 65 6d 65 6e and is incremen
5400: 74 65 64 20 77 69 74 68 0a 2a 2a 20 65 61 63 68 ted with.** each
5410: 20 72 65 6c 65 61 73 65 20 62 75 74 20 72 65 73 release but res
5420: 65 74 73 20 62 61 63 6b 20 74 6f 20 30 20 77 68 ets back to 0 wh
5430: 65 6e 65 76 65 72 20 58 20 69 73 20 69 6e 63 72 enever X is incr
5440: 65 6d 65 6e 74 65 64 2e 0a 2a 2a 20 54 68 65 20 emented..** The
5450: 5a 20 76 61 6c 75 65 20 6f 6e 6c 79 20 61 70 70 Z value only app
5460: 65 61 72 73 20 6f 6e 20 62 72 61 6e 63 68 20 72 ears on branch r
5470: 65 6c 65 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 eleases..**.** T
5480: 68 65 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f he SQLITE_VERSIO
5490: 4e 5f 4e 55 4d 42 45 52 20 69 73 20 61 6e 20 69 N_NUMBER is an i
54a0: 6e 74 65 67 65 72 20 74 68 61 74 20 69 73 20 63 nteger that is c
54b0: 6f 6d 70 75 74 65 64 20 61 73 0a 2a 2a 20 66 6f omputed as.** fo
54c0: 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c llows:.**.** <bl
54d0: 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a ockquote><pre>.*
54e0: 2a 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e * SQLITE_VERSION
54f0: 5f 4e 55 4d 42 45 52 20 3d 20 57 2a 31 30 30 30 _NUMBER = W*1000
5500: 30 30 30 20 2b 20 58 2a 31 30 30 30 20 2b 20 59 000 + X*1000 + Y
5510: 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 .** </pre></bloc
5520: 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 53 69 kquote>.**.** Si
5530: 6e 63 65 20 76 65 72 73 69 6f 6e 20 33 2e 36 2e nce version 3.6.
5540: 31 38 2c 20 53 51 4c 69 74 65 20 73 6f 75 72 63 18, SQLite sourc
5550: 65 20 63 6f 64 65 20 68 61 73 20 62 65 65 6e 20 e code has been
5560: 73 74 6f 72 65 64 20 69 6e 20 74 68 65 0a 2a 2a stored in the.**
5570: 20 3c 61 20 68 72 65 66 3d 22 68 74 74 70 3a 2f <a href="http:/
5580: 2f 77 77 77 2e 66 6f 73 73 69 6c 2d 73 63 6d 2e /www.fossil-scm.
5590: 6f 72 67 2f 22 3e 66 6f 73 73 69 6c 20 63 6f 6e org/">fossil con
55a0: 66 69 67 75 72 61 74 69 6f 6e 20 6d 61 6e 61 67 figuration manag
55b0: 65 6d 65 6e 74 0a 2a 2a 20 73 79 73 74 65 6d 3c ement.** system<
55c0: 2f 61 3e 2e 20 20 54 68 65 20 53 51 4c 49 54 45 /a>. The SQLITE
55d0: 5f 53 4f 55 52 43 45 5f 49 44 0a 2a 2a 20 6d 61 _SOURCE_ID.** ma
55e0: 63 72 6f 20 69 73 20 61 20 73 74 72 69 6e 67 20 cro is a string
55f0: 77 68 69 63 68 20 69 64 65 6e 74 69 66 69 65 73 which identifies
5600: 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 63 68 a particular ch
5610: 65 63 6b 2d 69 6e 20 6f 66 20 53 51 4c 69 74 65 eck-in of SQLite
5620: 0a 2a 2a 20 77 69 74 68 69 6e 20 69 74 73 20 63 .** within its c
5630: 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6d 61 6e onfiguration man
5640: 61 67 65 6d 65 6e 74 20 73 79 73 74 65 6d 2e 20 agement system.
5650: 20 54 68 65 20 73 74 72 69 6e 67 20 63 6f 6e 74 The string cont
5660: 61 69 6e 73 20 74 68 65 0a 2a 2a 20 64 61 74 65 ains the.** date
5670: 20 61 6e 64 20 74 69 6d 65 20 6f 66 20 74 68 65 and time of the
5680: 20 63 68 65 63 6b 2d 69 6e 20 28 55 54 43 29 20 check-in (UTC)
5690: 61 6e 64 20 61 6e 20 53 48 41 31 20 68 61 73 68 and an SHA1 hash
56a0: 20 6f 66 20 74 68 65 20 65 6e 74 69 72 65 0a 2a of the entire.*
56b0: 2a 20 73 6f 75 72 63 65 20 74 72 65 65 2e 0a 2a * source tree..*
56c0: 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b *.** See also: [
56d0: 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 sqlite3_libversi
56e0: 6f 6e 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 on()],.** [sqlit
56f0: 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 e3_libversion_nu
5700: 6d 62 65 72 28 29 5d 2c 20 5b 73 71 6c 69 74 65 mber()], [sqlite
5710: 33 5f 73 6f 75 72 63 65 69 64 28 29 5d 2c 0a 2a 3_sourceid()],.*
5720: 2a 20 5b 73 71 6c 69 74 65 5f 76 65 72 73 69 6f * [sqlite_versio
5730: 6e 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 n()] and [sqlite
5740: 5f 73 6f 75 72 63 65 5f 69 64 28 29 5d 2e 0a 2a _source_id()]..*
5750: 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
5760: 73 3a 20 5b 48 31 30 30 31 31 5d 20 5b 48 31 30 s: [H10011] [H10
5770: 30 31 34 5d 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 014].*/.#define
5780: 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 20 20 SQLITE_VERSION
5790: 20 20 20 20 20 20 22 33 2e 36 2e 32 30 22 0a 23 "3.6.20".#
57a0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 56 45 define SQLITE_VE
57b0: 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 20 33 30 30 RSION_NUMBER 300
57c0: 36 30 32 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 6020.#define SQL
57d0: 49 54 45 5f 53 4f 55 52 43 45 5f 49 44 20 20 20 ITE_SOURCE_ID
57e0: 20 20 20 22 32 30 30 39 2d 31 31 2d 30 34 20 31 "2009-11-04 1
57f0: 33 3a 33 30 3a 30 32 20 65 62 37 61 35 34 34 66 3:30:02 eb7a544f
5800: 65 34 39 64 31 36 32 36 62 61 63 65 63 66 65 35 e49d1626bacecfe5
5810: 33 64 64 63 30 33 66 65 30 38 32 65 33 32 34 33 3ddc03fe082e3243
5820: 22 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 "../*.** CAPI3RE
5830: 46 3a 20 52 75 6e 2d 54 69 6d 65 20 4c 69 62 72 F: Run-Time Libr
5840: 61 72 79 20 56 65 72 73 69 6f 6e 20 4e 75 6d 62 ary Version Numb
5850: 65 72 73 20 7b 48 31 30 30 32 30 7d 20 3c 53 36 ers {H10020} <S6
5860: 30 31 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 0100>.** KEYWORD
5870: 53 3a 20 73 71 6c 69 74 65 33 5f 76 65 72 73 69 S: sqlite3_versi
5880: 6f 6e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 69 on.**.** These i
5890: 6e 74 65 72 66 61 63 65 73 20 70 72 6f 76 69 64 nterfaces provid
58a0: 65 20 74 68 65 20 73 61 6d 65 20 69 6e 66 6f 72 e the same infor
58b0: 6d 61 74 69 6f 6e 20 61 73 20 74 68 65 20 5b 53 mation as the [S
58c0: 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5d 2c 0a QLITE_VERSION],.
58d0: 2a 2a 20 5b 53 51 4c 49 54 45 5f 56 45 52 53 49 ** [SQLITE_VERSI
58e0: 4f 4e 5f 4e 55 4d 42 45 52 5d 2c 20 61 6e 64 20 ON_NUMBER], and
58f0: 5b 53 51 4c 49 54 45 5f 53 4f 55 52 43 45 5f 49 [SQLITE_SOURCE_I
5900: 44 5d 20 23 64 65 66 69 6e 65 73 20 69 6e 20 74 D] #defines in t
5910: 68 65 20 68 65 61 64 65 72 2c 0a 2a 2a 20 62 75 he header,.** bu
5920: 74 20 61 72 65 20 61 73 73 6f 63 69 61 74 65 64 t are associated
5930: 20 77 69 74 68 20 74 68 65 20 6c 69 62 72 61 72 with the librar
5940: 79 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 y instead of the
5950: 20 68 65 61 64 65 72 20 66 69 6c 65 2e 20 20 43 header file. C
5960: 61 75 74 69 6f 75 73 0a 2a 2a 20 70 72 6f 67 72 autious.** progr
5970: 61 6d 6d 65 72 73 20 6d 69 67 68 74 20 69 6e 63 ammers might inc
5980: 6c 75 64 65 20 61 73 73 65 72 74 28 29 20 73 74 lude assert() st
5990: 61 74 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 69 atements in thei
59a0: 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 74 6f r application to
59b0: 0a 2a 2a 20 76 65 72 69 66 79 20 74 68 61 74 20 .** verify that
59c0: 76 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64 20 values returned
59d0: 62 79 20 74 68 65 73 65 20 69 6e 74 65 72 66 61 by these interfa
59e0: 63 65 73 20 6d 61 74 63 68 20 74 68 65 20 6d 61 ces match the ma
59f0: 63 72 6f 73 20 69 6e 0a 2a 2a 20 74 68 65 20 68 cros in.** the h
5a00: 65 61 64 65 72 2c 20 61 6e 64 20 74 68 75 73 20 eader, and thus
5a10: 69 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 20 insure that the
5a20: 61 70 70 6c 69 63 61 74 69 6f 6e 20 69 73 0a 2a application is.*
5a30: 2a 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 * compiled with
5a40: 6d 61 74 63 68 69 6e 67 20 6c 69 62 72 61 72 79 matching library
5a50: 20 61 6e 64 20 68 65 61 64 65 72 20 66 69 6c 65 and header file
5a60: 73 2e 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 s..**.** <blockq
5a70: 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 61 73 uote><pre>.** as
5a80: 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6c 69 sert( sqlite3_li
5a90: 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 bversion_number(
5aa0: 29 3d 3d 53 51 4c 49 54 45 5f 56 45 52 53 49 4f )==SQLITE_VERSIO
5ab0: 4e 5f 4e 55 4d 42 45 52 20 29 3b 0a 2a 2a 20 61 N_NUMBER );.** a
5ac0: 73 73 65 72 74 28 20 73 74 72 63 6d 70 28 73 71 ssert( strcmp(sq
5ad0: 6c 69 74 65 33 5f 73 6f 75 72 63 65 69 64 28 29 lite3_sourceid()
5ae0: 2c 53 51 4c 49 54 45 5f 53 4f 55 52 43 45 5f 49 ,SQLITE_SOURCE_I
5af0: 44 29 3d 3d 30 20 29 3b 0a 2a 2a 20 61 73 73 65 D)==0 );.** asse
5b00: 72 74 28 20 73 74 72 63 6d 70 28 73 71 6c 69 74 rt( strcmp(sqlit
5b10: 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 2c 53 51 e3_libversion,SQ
5b20: 4c 49 54 45 5f 56 45 52 53 49 4f 4e 29 3d 3d 30 LITE_VERSION)==0
5b30: 20 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 );.** </pre></b
5b40: 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a lockquote>.**.**
5b50: 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6c 69 62 The sqlite3_lib
5b60: 76 65 72 73 69 6f 6e 28 29 20 66 75 6e 63 74 69 version() functi
5b70: 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 on returns the s
5b80: 61 6d 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 ame information
5b90: 61 73 20 69 73 0a 2a 2a 20 69 6e 20 74 68 65 20 as is.** in the
5ba0: 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e 5b sqlite3_version[
5bb0: 5d 20 73 74 72 69 6e 67 20 63 6f 6e 73 74 61 6e ] string constan
5bc0: 74 2e 20 20 54 68 65 20 66 75 6e 63 74 69 6f 6e t. The function
5bd0: 20 69 73 20 70 72 6f 76 69 64 65 64 0a 2a 2a 20 is provided.**
5be0: 66 6f 72 20 75 73 65 20 69 6e 20 44 4c 4c 73 20 for use in DLLs
5bf0: 73 69 6e 63 65 20 44 4c 4c 20 75 73 65 72 73 20 since DLL users
5c00: 75 73 75 61 6c 6c 79 20 64 6f 20 6e 6f 74 20 68 usually do not h
5c10: 61 76 65 20 64 69 72 65 63 74 20 61 63 63 65 73 ave direct acces
5c20: 73 20 74 6f 20 73 74 72 69 6e 67 0a 2a 2a 20 63 s to string.** c
5c30: 6f 6e 73 74 61 6e 74 73 20 77 69 74 68 69 6e 20 onstants within
5c40: 74 68 65 20 44 4c 4c 2e 20 20 53 69 6d 69 6c 61 the DLL. Simila
5c50: 72 6c 79 2c 20 74 68 65 20 73 71 6c 69 74 65 33 rly, the sqlite3
5c60: 5f 73 6f 75 72 63 65 69 64 28 29 20 66 75 6e 63 _sourceid() func
5c70: 74 69 6f 6e 0a 2a 2a 20 72 65 74 75 72 6e 73 20 tion.** returns
5c80: 74 68 65 20 73 61 6d 65 20 69 6e 66 6f 72 6d 61 the same informa
5c90: 74 69 6f 6e 20 61 73 20 69 73 20 69 6e 20 74 68 tion as is in th
5ca0: 65 20 5b 53 51 4c 49 54 45 5f 53 4f 55 52 43 45 e [SQLITE_SOURCE
5cb0: 5f 49 44 5d 20 23 64 65 66 69 6e 65 20 6f 66 0a _ID] #define of.
5cc0: 2a 2a 20 74 68 65 20 68 65 61 64 65 72 20 66 69 ** the header fi
5cd0: 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c le..**.** See al
5ce0: 73 6f 3a 20 5b 73 71 6c 69 74 65 5f 76 65 72 73 so: [sqlite_vers
5cf0: 69 6f 6e 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 ion()] and [sqli
5d00: 74 65 5f 73 6f 75 72 63 65 5f 69 64 28 29 5d 2e te_source_id()].
5d10: 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
5d20: 6e 74 73 3a 20 5b 48 31 30 30 32 31 5d 20 5b 48 nts: [H10021] [H
5d30: 31 30 30 32 32 5d 20 5b 48 31 30 30 32 33 5d 0a 10022] [H10023].
5d40: 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f */.SQLITE_API co
5d50: 6e 73 74 20 63 68 61 72 20 73 71 6c 69 74 65 33 nst char sqlite3
5d60: 5f 76 65 72 73 69 6f 6e 5b 5d 20 3d 20 53 51 4c _version[] = SQL
5d70: 49 54 45 5f 56 45 52 53 49 4f 4e 3b 0a 53 51 4c ITE_VERSION;.SQL
5d80: 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 ITE_API const ch
5d90: 61 72 20 2a 73 71 6c 69 74 65 33 5f 6c 69 62 76 ar *sqlite3_libv
5da0: 65 72 73 69 6f 6e 28 76 6f 69 64 29 3b 0a 53 51 ersion(void);.SQ
5db0: 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 LITE_API const c
5dc0: 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 6f 75 har *sqlite3_sou
5dd0: 72 63 65 69 64 28 76 6f 69 64 29 3b 0a 53 51 4c rceid(void);.SQL
5de0: 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
5df0: 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e te3_libversion_n
5e00: 75 6d 62 65 72 28 76 6f 69 64 29 3b 0a 0a 2f 2a umber(void);../*
5e10: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54 65 .** CAPI3REF: Te
5e20: 73 74 20 54 6f 20 53 65 65 20 49 66 20 54 68 65 st To See If The
5e30: 20 4c 69 62 72 61 72 79 20 49 73 20 54 68 72 65 Library Is Thre
5e40: 61 64 73 61 66 65 20 7b 48 31 30 31 30 30 7d 20 adsafe {H10100}
5e50: 3c 53 36 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 53 <S60100>.**.** S
5e60: 51 4c 69 74 65 20 63 61 6e 20 62 65 20 63 6f 6d QLite can be com
5e70: 70 69 6c 65 64 20 77 69 74 68 20 6f 72 20 77 69 piled with or wi
5e80: 74 68 6f 75 74 20 6d 75 74 65 78 65 73 2e 20 20 thout mutexes.
5e90: 57 68 65 6e 0a 2a 2a 20 74 68 65 20 5b 53 51 4c When.** the [SQL
5ea0: 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 5d 20 ITE_THREADSAFE]
5eb0: 43 20 70 72 65 70 72 6f 63 65 73 73 6f 72 20 6d C preprocessor m
5ec0: 61 63 72 6f 20 69 73 20 31 20 6f 72 20 32 2c 20 acro is 1 or 2,
5ed0: 6d 75 74 65 78 65 73 0a 2a 2a 20 61 72 65 20 65 mutexes.** are e
5ee0: 6e 61 62 6c 65 64 20 61 6e 64 20 53 51 4c 69 74 nabled and SQLit
5ef0: 65 20 69 73 20 74 68 72 65 61 64 73 61 66 65 2e e is threadsafe.
5f00: 20 20 57 68 65 6e 20 74 68 65 0a 2a 2a 20 5b 53 When the.** [S
5f10: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
5f20: 5d 20 6d 61 63 72 6f 20 69 73 20 30 2c 20 0a 2a ] macro is 0, .*
5f30: 2a 20 74 68 65 20 6d 75 74 65 78 65 73 20 61 72 * the mutexes ar
5f40: 65 20 6f 6d 69 74 74 65 64 2e 20 20 57 69 74 68 e omitted. With
5f50: 6f 75 74 20 74 68 65 20 6d 75 74 65 78 65 73 2c out the mutexes,
5f60: 20 69 74 20 69 73 20 6e 6f 74 20 73 61 66 65 0a it is not safe.
5f70: 2a 2a 20 74 6f 20 75 73 65 20 53 51 4c 69 74 65 ** to use SQLite
5f80: 20 63 6f 6e 63 75 72 72 65 6e 74 6c 79 20 66 72 concurrently fr
5f90: 6f 6d 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 om more than one
5fa0: 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 45 thread..**.** E
5fb0: 6e 61 62 6c 69 6e 67 20 6d 75 74 65 78 65 73 20 nabling mutexes
5fc0: 69 6e 63 75 72 73 20 61 20 6d 65 61 73 75 72 61 incurs a measura
5fd0: 62 6c 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 ble performance
5fe0: 70 65 6e 61 6c 74 79 2e 0a 2a 2a 20 53 6f 20 69 penalty..** So i
5ff0: 66 20 73 70 65 65 64 20 69 73 20 6f 66 20 75 74 f speed is of ut
6000: 6d 6f 73 74 20 69 6d 70 6f 72 74 61 6e 63 65 2c most importance,
6010: 20 69 74 20 6d 61 6b 65 73 20 73 65 6e 73 65 20 it makes sense
6020: 74 6f 20 64 69 73 61 62 6c 65 0a 2a 2a 20 74 68 to disable.** th
6030: 65 20 6d 75 74 65 78 65 73 2e 20 20 42 75 74 20 e mutexes. But
6040: 66 6f 72 20 6d 61 78 69 6d 75 6d 20 73 61 66 65 for maximum safe
6050: 74 79 2c 20 6d 75 74 65 78 65 73 20 73 68 6f 75 ty, mutexes shou
6060: 6c 64 20 62 65 20 65 6e 61 62 6c 65 64 2e 0a 2a ld be enabled..*
6070: 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 62 65 * The default be
6080: 68 61 76 69 6f 72 20 69 73 20 66 6f 72 20 6d 75 havior is for mu
6090: 74 65 78 65 73 20 74 6f 20 62 65 20 65 6e 61 62 texes to be enab
60a0: 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 led..**.** This
60b0: 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 62 65 interface can be
60c0: 20 75 73 65 64 20 62 79 20 61 6e 20 61 70 70 6c used by an appl
60d0: 69 63 61 74 69 6f 6e 20 74 6f 20 6d 61 6b 65 20 ication to make
60e0: 73 75 72 65 20 74 68 61 74 20 74 68 65 0a 2a 2a sure that the.**
60f0: 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69 version of SQLi
6100: 74 65 20 74 68 61 74 20 69 74 20 69 73 20 6c 69 te that it is li
6110: 6e 6b 69 6e 67 20 61 67 61 69 6e 73 74 20 77 61 nking against wa
6120: 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a s compiled with.
6130: 2a 2a 20 74 68 65 20 64 65 73 69 72 65 64 20 73 ** the desired s
6140: 65 74 74 69 6e 67 20 6f 66 20 74 68 65 20 5b 53 etting of the [S
6150: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
6160: 5d 20 6d 61 63 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54 ] macro..**.** T
6170: 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 6f 6e his interface on
6180: 6c 79 20 72 65 70 6f 72 74 73 20 6f 6e 20 74 68 ly reports on th
6190: 65 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6d e compile-time m
61a0: 75 74 65 78 20 73 65 74 74 69 6e 67 0a 2a 2a 20 utex setting.**
61b0: 6f 66 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 54 of the [SQLITE_T
61c0: 48 52 45 41 44 53 41 46 45 5d 20 66 6c 61 67 2e HREADSAFE] flag.
61d0: 20 20 49 66 20 53 51 4c 69 74 65 20 69 73 20 63 If SQLite is c
61e0: 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a 2a 2a 20 ompiled with.**
61f0: 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
6200: 45 3d 31 20 74 68 65 6e 20 6d 75 74 65 78 65 73 E=1 then mutexes
6210: 20 61 72 65 20 65 6e 61 62 6c 65 64 20 62 79 20 are enabled by
6220: 64 65 66 61 75 6c 74 20 62 75 74 0a 2a 2a 20 63 default but.** c
6230: 61 6e 20 62 65 20 66 75 6c 6c 79 20 6f 72 20 70 an be fully or p
6240: 61 72 74 69 61 6c 6c 79 20 64 69 73 61 62 6c 65 artially disable
6250: 64 20 75 73 69 6e 67 20 61 20 63 61 6c 6c 20 74 d using a call t
6260: 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 o [sqlite3_confi
6270: 67 28 29 5d 0a 2a 2a 20 77 69 74 68 20 74 68 65 g()].** with the
6280: 20 76 65 72 62 73 20 5b 53 51 4c 49 54 45 5f 43 verbs [SQLITE_C
6290: 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 ONFIG_SINGLETHRE
62a0: 41 44 5d 2c 20 5b 53 51 4c 49 54 45 5f 43 4f 4e AD], [SQLITE_CON
62b0: 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 5d FIG_MULTITHREAD]
62c0: 2c 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49 54 45 5f ,.** or [SQLITE_
62d0: 43 4f 4e 46 49 47 5f 4d 55 54 45 58 5d 2e 20 20 CONFIG_MUTEX].
62e0: 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 The return value
62f0: 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f of this functio
6300: 6e 20 73 68 6f 77 73 0a 2a 2a 20 6f 6e 6c 79 20 n shows.** only
6310: 74 68 65 20 64 65 66 61 75 6c 74 20 63 6f 6d 70 the default comp
6320: 69 6c 65 2d 74 69 6d 65 20 73 65 74 74 69 6e 67 ile-time setting
6330: 2c 20 6e 6f 74 20 61 6e 79 20 72 75 6e 2d 74 69 , not any run-ti
6340: 6d 65 20 63 68 61 6e 67 65 73 0a 2a 2a 20 74 6f me changes.** to
6350: 20 74 68 61 74 20 73 65 74 74 69 6e 67 2e 0a 2a that setting..*
6360: 2a 0a 2a 2a 20 53 65 65 20 74 68 65 20 5b 74 68 *.** See the [th
6370: 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 64 6f reading mode] do
6380: 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 cumentation for
6390: 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 additional infor
63a0: 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 52 65 mation..**.** Re
63b0: 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30 quirements: [H10
63c0: 31 30 31 5d 20 5b 48 31 30 31 30 32 5d 0a 2a 2f 101] [H10102].*/
63d0: 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
63e0: 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64 73 61 sqlite3_threadsa
63f0: 66 65 28 76 6f 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a fe(void);../*.**
6400: 20 43 41 50 49 33 52 45 46 3a 20 44 61 74 61 62 CAPI3REF: Datab
6410: 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 48 ase Connection H
6420: 61 6e 64 6c 65 20 7b 48 31 32 30 30 30 7d 20 3c andle {H12000} <
6430: 53 34 30 32 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f S40200>.** KEYWO
6440: 52 44 53 3a 20 7b 64 61 74 61 62 61 73 65 20 63 RDS: {database c
6450: 6f 6e 6e 65 63 74 69 6f 6e 7d 20 7b 64 61 74 61 onnection} {data
6460: 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 base connections
6470: 7d 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 6f 70 65 }.**.** Each ope
6480: 6e 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 n SQLite databas
6490: 65 20 69 73 20 72 65 70 72 65 73 65 6e 74 65 64 e is represented
64a0: 20 62 79 20 61 20 70 6f 69 6e 74 65 72 20 74 6f by a pointer to
64b0: 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a an instance of.
64c0: 2a 2a 20 74 68 65 20 6f 70 61 71 75 65 20 73 74 ** the opaque st
64d0: 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 73 ructure named "s
64e0: 71 6c 69 74 65 33 22 2e 20 20 49 74 20 69 73 20 qlite3". It is
64f0: 75 73 65 66 75 6c 20 74 6f 20 74 68 69 6e 6b 20 useful to think
6500: 6f 66 20 61 6e 20 73 71 6c 69 74 65 33 0a 2a 2a of an sqlite3.**
6510: 20 70 6f 69 6e 74 65 72 20 61 73 20 61 6e 20 6f pointer as an o
6520: 62 6a 65 63 74 2e 20 20 54 68 65 20 5b 73 71 6c bject. The [sql
6530: 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 ite3_open()], [s
6540: 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d qlite3_open16()]
6550: 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 , and.** [sqlite
6560: 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 69 6e 74 3_open_v2()] int
6570: 65 72 66 61 63 65 73 20 61 72 65 20 69 74 73 20 erfaces are its
6580: 63 6f 6e 73 74 72 75 63 74 6f 72 73 2c 20 61 6e constructors, an
6590: 64 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 d [sqlite3_close
65a0: 28 29 5d 0a 2a 2a 20 69 73 20 69 74 73 20 64 65 ()].** is its de
65b0: 73 74 72 75 63 74 6f 72 2e 20 20 54 68 65 72 65 structor. There
65c0: 20 61 72 65 20 6d 61 6e 79 20 6f 74 68 65 72 20 are many other
65d0: 69 6e 74 65 72 66 61 63 65 73 20 28 73 75 63 68 interfaces (such
65e0: 20 61 73 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f as.** [sqlite3_
65f0: 70 72 65 70 61 72 65 5f 76 32 28 29 5d 2c 20 5b prepare_v2()], [
6600: 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 sqlite3_create_f
6610: 75 6e 63 74 69 6f 6e 28 29 5d 2c 20 61 6e 64 0a unction()], and.
6620: 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79 ** [sqlite3_busy
6630: 5f 74 69 6d 65 6f 75 74 28 29 5d 20 74 6f 20 6e _timeout()] to n
6640: 61 6d 65 20 62 75 74 20 74 68 72 65 65 29 20 74 ame but three) t
6650: 68 61 74 20 61 72 65 20 6d 65 74 68 6f 64 73 20 hat are methods
6660: 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 on an.** sqlite3
6670: 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 74 79 70 65 object..*/.type
6680: 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 def struct sqlit
6690: 65 33 20 73 71 6c 69 74 65 33 3b 0a 0a 2f 2a 0a e3 sqlite3;../*.
66a0: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 36 34 2d ** CAPI3REF: 64-
66b0: 42 69 74 20 49 6e 74 65 67 65 72 20 54 79 70 65 Bit Integer Type
66c0: 73 20 7b 48 31 30 32 30 30 7d 20 3c 53 31 30 31 s {H10200} <S101
66d0: 31 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 10>.** KEYWORDS:
66e0: 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 71 sqlite_int64 sq
66f0: 6c 69 74 65 5f 75 69 6e 74 36 34 0a 2a 2a 0a 2a lite_uint64.**.*
6700: 2a 20 42 65 63 61 75 73 65 20 74 68 65 72 65 20 * Because there
6710: 69 73 20 6e 6f 20 63 72 6f 73 73 2d 70 6c 61 74 is no cross-plat
6720: 66 6f 72 6d 20 77 61 79 20 74 6f 20 73 70 65 63 form way to spec
6730: 69 66 79 20 36 34 2d 62 69 74 20 69 6e 74 65 67 ify 64-bit integ
6740: 65 72 20 74 79 70 65 73 0a 2a 2a 20 53 51 4c 69 er types.** SQLi
6750: 74 65 20 69 6e 63 6c 75 64 65 73 20 74 79 70 65 te includes type
6760: 64 65 66 73 20 66 6f 72 20 36 34 2d 62 69 74 20 defs for 64-bit
6770: 73 69 67 6e 65 64 20 61 6e 64 20 75 6e 73 69 67 signed and unsig
6780: 6e 65 64 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a ned integers..**
6790: 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
67a0: 69 6e 74 36 34 20 61 6e 64 20 73 71 6c 69 74 65 int64 and sqlite
67b0: 33 5f 75 69 6e 74 36 34 20 61 72 65 20 74 68 65 3_uint64 are the
67c0: 20 70 72 65 66 65 72 72 65 64 20 74 79 70 65 20 preferred type
67d0: 64 65 66 69 6e 69 74 69 6f 6e 73 2e 0a 2a 2a 20 definitions..**
67e0: 54 68 65 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 The sqlite_int64
67f0: 20 61 6e 64 20 73 71 6c 69 74 65 5f 75 69 6e 74 and sqlite_uint
6800: 36 34 20 74 79 70 65 73 20 61 72 65 20 73 75 70 64 types are sup
6810: 70 6f 72 74 65 64 20 66 6f 72 20 62 61 63 6b 77 ported for backw
6820: 61 72 64 73 0a 2a 2a 20 63 6f 6d 70 61 74 69 62 ards.** compatib
6830: 69 6c 69 74 79 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a ility only..**.*
6840: 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 * Requirements:
6850: 5b 48 31 30 32 30 31 5d 20 5b 48 31 30 32 30 32 [H10201] [H10202
6860: 5d 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ].*/.#ifdef SQLI
6870: 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 0a 20 20 TE_INT64_TYPE.
6880: 74 79 70 65 64 65 66 20 53 51 4c 49 54 45 5f 49 typedef SQLITE_I
6890: 4e 54 36 34 5f 54 59 50 45 20 73 71 6c 69 74 65 NT64_TYPE sqlite
68a0: 5f 69 6e 74 36 34 3b 0a 20 20 74 79 70 65 64 65 _int64;. typede
68b0: 66 20 75 6e 73 69 67 6e 65 64 20 53 51 4c 49 54 f unsigned SQLIT
68c0: 45 5f 49 4e 54 36 34 5f 54 59 50 45 20 73 71 6c E_INT64_TYPE sql
68d0: 69 74 65 5f 75 69 6e 74 36 34 3b 0a 23 65 6c 69 ite_uint64;.#eli
68e0: 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 f defined(_MSC_V
68f0: 45 52 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f ER) || defined(_
6900: 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a 20 20 74 _BORLANDC__). t
6910: 79 70 65 64 65 66 20 5f 5f 69 6e 74 36 34 20 73 ypedef __int64 s
6920: 71 6c 69 74 65 5f 69 6e 74 36 34 3b 0a 20 20 74 qlite_int64;. t
6930: 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64 20 ypedef unsigned
6940: 5f 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 5f 75 __int64 sqlite_u
6950: 69 6e 74 36 34 3b 0a 23 65 6c 73 65 0a 20 20 74 int64;.#else. t
6960: 79 70 65 64 65 66 20 6c 6f 6e 67 20 6c 6f 6e 67 ypedef long long
6970: 20 69 6e 74 20 73 71 6c 69 74 65 5f 69 6e 74 36 int sqlite_int6
6980: 34 3b 0a 20 20 74 79 70 65 64 65 66 20 75 6e 73 4;. typedef uns
6990: 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 igned long long
69a0: 69 6e 74 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 int sqlite_uint6
69b0: 34 3b 0a 23 65 6e 64 69 66 0a 74 79 70 65 64 65 4;.#endif.typede
69c0: 66 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 f sqlite_int64 s
69d0: 71 6c 69 74 65 33 5f 69 6e 74 36 34 3b 0a 74 79 qlite3_int64;.ty
69e0: 70 65 64 65 66 20 73 71 6c 69 74 65 5f 75 69 6e pedef sqlite_uin
69f0: 74 36 34 20 73 71 6c 69 74 65 33 5f 75 69 6e 74 t64 sqlite3_uint
6a00: 36 34 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 63 6f 64;../*.** If co
6a10: 6d 70 69 6c 69 6e 67 20 66 6f 72 20 61 20 70 72 mpiling for a pr
6a20: 6f 63 65 73 73 6f 72 20 74 68 61 74 20 6c 61 63 ocessor that lac
6a30: 6b 73 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e ks floating poin
6a40: 74 20 73 75 70 70 6f 72 74 2c 0a 2a 2a 20 73 75 t support,.** su
6a50: 62 73 74 69 74 75 74 65 20 69 6e 74 65 67 65 72 bstitute integer
6a60: 20 66 6f 72 20 66 6c 6f 61 74 69 6e 67 2d 70 6f for floating-po
6a70: 69 6e 74 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 int..*/.#ifdef S
6a80: 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 QLITE_OMIT_FLOAT
6a90: 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 64 65 66 69 ING_POINT.# defi
6aa0: 6e 65 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 ne double sqlite
6ab0: 33 5f 69 6e 74 36 34 0a 23 65 6e 64 69 66 0a 0a 3_int64.#endif..
6ac0: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
6ad0: 43 6c 6f 73 69 6e 67 20 41 20 44 61 74 61 62 61 Closing A Databa
6ae0: 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 7b 48 se Connection {H
6af0: 31 32 30 31 30 7d 20 3c 53 33 30 31 30 30 3e 3c 12010} <S30100><
6b00: 53 34 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S40200>.**.** Th
6b10: 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 is routine is th
6b20: 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 6f 72 e destructor for
6b30: 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5d 20 6f the [sqlite3] o
6b40: 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 41 70 70 bject..**.** App
6b50: 6c 69 63 61 74 69 6f 6e 73 20 6d 75 73 74 20 5b lications must [
6b60: 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 sqlite3_finalize
6b70: 20 7c 20 66 69 6e 61 6c 69 7a 65 5d 20 61 6c 6c | finalize] all
6b80: 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 [prepared state
6b90: 6d 65 6e 74 73 5d 0a 2a 2a 20 61 6e 64 20 5b 73 ments].** and [s
6ba0: 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 qlite3_blob_clos
6bb0: 65 20 7c 20 63 6c 6f 73 65 5d 20 61 6c 6c 20 5b e | close] all [
6bc0: 42 4c 4f 42 20 68 61 6e 64 6c 65 73 5d 20 61 73 BLOB handles] as
6bd0: 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a 2a 2a sociated with.**
6be0: 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5d 20 6f the [sqlite3] o
6bf0: 62 6a 65 63 74 20 70 72 69 6f 72 20 74 6f 20 61 bject prior to a
6c00: 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 63 6c 6f ttempting to clo
6c10: 73 65 20 74 68 65 20 6f 62 6a 65 63 74 2e 0a 2a se the object..*
6c20: 2a 0a 2a 2a 20 49 66 20 5b 73 71 6c 69 74 65 33 *.** If [sqlite3
6c30: 5f 63 6c 6f 73 65 28 29 5d 20 69 73 20 69 6e 76 _close()] is inv
6c40: 6f 6b 65 64 20 77 68 69 6c 65 20 61 20 74 72 61 oked while a tra
6c50: 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e nsaction is open
6c60: 2c 0a 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 63 ,.** the transac
6c70: 74 69 6f 6e 20 69 73 20 61 75 74 6f 6d 61 74 69 tion is automati
6c80: 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 cally rolled bac
6c90: 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 20 70 k..**.** The C p
6ca0: 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c arameter to [sql
6cb0: 69 74 65 33 5f 63 6c 6f 73 65 28 43 29 5d 20 6d ite3_close(C)] m
6cc0: 75 73 74 20 62 65 20 65 69 74 68 65 72 20 61 20 ust be either a
6cd0: 4e 55 4c 4c 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 NULL.** pointer
6ce0: 6f 72 20 61 6e 20 5b 73 71 6c 69 74 65 33 5d 20 or an [sqlite3]
6cf0: 6f 62 6a 65 63 74 20 70 6f 69 6e 74 65 72 20 6f object pointer o
6d00: 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 btained.** from
6d10: 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d [sqlite3_open()]
6d20: 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 , [sqlite3_open1
6d30: 36 28 29 5d 2c 20 6f 72 0a 2a 2a 20 5b 73 71 6c 6()], or.** [sql
6d40: 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 2c ite3_open_v2()],
6d50: 20 61 6e 64 20 6e 6f 74 20 70 72 65 76 69 6f 75 and not previou
6d60: 73 6c 79 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a sly closed..**.*
6d70: 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
6d80: 2a 2a 20 5b 48 31 32 30 31 31 5d 20 5b 48 31 32 ** [H12011] [H12
6d90: 30 31 32 5d 20 5b 48 31 32 30 31 33 5d 20 5b 48 012] [H12013] [H
6da0: 31 32 30 31 34 5d 20 5b 48 31 32 30 31 35 5d 20 12014] [H12015]
6db0: 5b 48 31 32 30 31 39 5d 0a 2a 2f 0a 53 51 4c 49 [H12019].*/.SQLI
6dc0: 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
6dd0: 65 33 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33 e3_close(sqlite3
6de0: 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 *);../*.** The
6df0: 74 79 70 65 20 66 6f 72 20 61 20 63 61 6c 6c 62 type for a callb
6e00: 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a ack function..**
6e10: 20 54 68 69 73 20 69 73 20 6c 65 67 61 63 79 20 This is legacy
6e20: 61 6e 64 20 64 65 70 72 65 63 61 74 65 64 2e 20 and deprecated.
6e30: 20 49 74 20 69 73 20 69 6e 63 6c 75 64 65 64 20 It is included
6e40: 66 6f 72 20 68 69 73 74 6f 72 69 63 61 6c 0a 2a for historical.*
6e50: 2a 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 * compatibility
6e60: 61 6e 64 20 69 73 20 6e 6f 74 20 64 6f 63 75 6d and is not docum
6e70: 65 6e 74 65 64 2e 0a 2a 2f 0a 74 79 70 65 64 65 ented..*/.typede
6e80: 66 20 69 6e 74 20 28 2a 73 71 6c 69 74 65 33 5f f int (*sqlite3_
6e90: 63 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c callback)(void*,
6ea0: 69 6e 74 2c 63 68 61 72 2a 2a 2c 20 63 68 61 72 int,char**, char
6eb0: 2a 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 **);../*.** CAPI
6ec0: 33 52 45 46 3a 20 4f 6e 65 2d 53 74 65 70 20 51 3REF: One-Step Q
6ed0: 75 65 72 79 20 45 78 65 63 75 74 69 6f 6e 20 49 uery Execution I
6ee0: 6e 74 65 72 66 61 63 65 20 7b 48 31 32 31 30 30 nterface {H12100
6ef0: 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a } <S10000>.**.**
6f00: 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78 65 The sqlite3_exe
6f10: 63 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 c() interface is
6f20: 20 61 20 63 6f 6e 76 65 6e 69 65 6e 74 20 77 61 a convenient wa
6f30: 79 20 6f 66 20 72 75 6e 6e 69 6e 67 20 6f 6e 65 y of running one
6f40: 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 53 51 4c 20 or more.** SQL
6f50: 73 74 61 74 65 6d 65 6e 74 73 20 77 69 74 68 6f statements witho
6f60: 75 74 20 68 61 76 69 6e 67 20 74 6f 20 77 72 69 ut having to wri
6f70: 74 65 20 61 20 6c 6f 74 20 6f 66 20 43 20 63 6f te a lot of C co
6f80: 64 65 2e 20 20 54 68 65 20 55 54 46 2d 38 20 65 de. The UTF-8 e
6f90: 6e 63 6f 64 65 64 0a 2a 2a 20 53 51 4c 20 73 74 ncoded.** SQL st
6fa0: 61 74 65 6d 65 6e 74 73 20 61 72 65 20 70 61 73 atements are pas
6fb0: 73 65 64 20 69 6e 20 61 73 20 74 68 65 20 73 65 sed in as the se
6fc0: 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 cond parameter t
6fd0: 6f 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 o sqlite3_exec()
6fe0: 2e 0a 2a 2a 20 54 68 65 20 73 74 61 74 65 6d 65 ..** The stateme
6ff0: 6e 74 73 20 61 72 65 20 65 76 61 6c 75 61 74 65 nts are evaluate
7000: 64 20 6f 6e 65 20 62 79 20 6f 6e 65 20 75 6e 74 d one by one unt
7010: 69 6c 20 65 69 74 68 65 72 20 61 6e 20 65 72 72 il either an err
7020: 6f 72 20 6f 72 0a 2a 2a 20 61 6e 20 69 6e 74 65 or or.** an inte
7030: 72 72 75 70 74 20 69 73 20 65 6e 63 6f 75 6e 74 rrupt is encount
7040: 65 72 65 64 2c 20 6f 72 20 75 6e 74 69 6c 20 74 ered, or until t
7050: 68 65 79 20 61 72 65 20 61 6c 6c 20 64 6f 6e 65 hey are all done
7060: 2e 20 20 54 68 65 20 33 72 64 20 70 61 72 61 6d . The 3rd param
7070: 65 74 65 72 0a 2a 2a 20 69 73 20 61 6e 20 6f 70 eter.** is an op
7080: 74 69 6f 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 20 tional callback
7090: 74 68 61 74 20 69 73 20 69 6e 76 6f 6b 65 64 20 that is invoked
70a0: 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 72 6f once for each ro
70b0: 77 20 6f 66 20 61 6e 79 20 71 75 65 72 79 0a 2a w of any query.*
70c0: 2a 20 72 65 73 75 6c 74 73 20 70 72 6f 64 75 63 * results produc
70d0: 65 64 20 62 79 20 74 68 65 20 53 51 4c 20 73 74 ed by the SQL st
70e0: 61 74 65 6d 65 6e 74 73 2e 20 20 54 68 65 20 35 atements. The 5
70f0: 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 65 6c th parameter tel
7100: 6c 73 20 77 68 65 72 65 0a 2a 2a 20 74 6f 20 77 ls where.** to w
7110: 72 69 74 65 20 61 6e 79 20 65 72 72 6f 72 20 6d rite any error m
7120: 65 73 73 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 essages..**.** T
7130: 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 he error message
7140: 20 70 61 73 73 65 64 20 62 61 63 6b 20 74 68 72 passed back thr
7150: 6f 75 67 68 20 74 68 65 20 35 74 68 20 70 61 72 ough the 5th par
7160: 61 6d 65 74 65 72 20 69 73 20 68 65 6c 64 0a 2a ameter is held.*
7170: 2a 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 * in memory obta
7180: 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 ined from [sqlit
7190: 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 20 54 e3_malloc()]. T
71a0: 6f 20 61 76 6f 69 64 20 61 20 6d 65 6d 6f 72 79 o avoid a memory
71b0: 20 6c 65 61 6b 2c 0a 2a 2a 20 74 68 65 20 63 61 leak,.** the ca
71c0: 6c 6c 69 6e 67 20 61 70 70 6c 69 63 61 74 69 6f lling applicatio
71d0: 6e 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20 5b 73 n should call [s
71e0: 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 20 6f qlite3_free()] o
71f0: 6e 20 61 6e 79 20 65 72 72 6f 72 0a 2a 2a 20 6d n any error.** m
7200: 65 73 73 61 67 65 20 72 65 74 75 72 6e 65 64 20 essage returned
7210: 74 68 72 6f 75 67 68 20 74 68 65 20 35 74 68 20 through the 5th
7220: 70 61 72 61 6d 65 74 65 72 20 77 68 65 6e 20 69 parameter when i
7230: 74 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 75 t has finished u
7240: 73 69 6e 67 0a 2a 2a 20 74 68 65 20 65 72 72 6f sing.** the erro
7250: 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a r message..**.**
7260: 20 49 66 20 74 68 65 20 53 51 4c 20 73 74 61 74 If the SQL stat
7270: 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 32 6e 64 ement in the 2nd
7280: 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 4e 55 parameter is NU
7290: 4c 4c 20 6f 72 20 61 6e 20 65 6d 70 74 79 20 73 LL or an empty s
72a0: 74 72 69 6e 67 0a 2a 2a 20 6f 72 20 61 20 73 74 tring.** or a st
72b0: 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20 ring containing
72c0: 6f 6e 6c 79 20 77 68 69 74 65 73 70 61 63 65 20 only whitespace
72d0: 61 6e 64 20 63 6f 6d 6d 65 6e 74 73 2c 20 74 68 and comments, th
72e0: 65 6e 20 6e 6f 20 53 51 4c 0a 2a 2a 20 73 74 61 en no SQL.** sta
72f0: 74 65 6d 65 6e 74 73 20 61 72 65 20 65 76 61 6c tements are eval
7300: 75 61 74 65 64 20 61 6e 64 20 74 68 65 20 64 61 uated and the da
7310: 74 61 62 61 73 65 20 69 73 20 6e 6f 74 20 63 68 tabase is not ch
7320: 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 anged..**.** The
7330: 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 sqlite3_exec()
7340: 69 6e 74 65 72 66 61 63 65 20 69 73 20 69 6d 70 interface is imp
7350: 6c 65 6d 65 6e 74 65 64 20 69 6e 20 74 65 72 6d lemented in term
7360: 73 20 6f 66 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 s of.** [sqlite3
7370: 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 2c 20 _prepare_v2()],
7380: 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d [sqlite3_step()]
7390: 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 66 , and [sqlite3_f
73a0: 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a 20 54 inalize()]..** T
73b0: 68 65 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 he sqlite3_exec(
73c0: 29 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e ) routine does n
73d0: 6f 74 68 69 6e 67 20 74 6f 20 74 68 65 20 64 61 othing to the da
73e0: 74 61 62 61 73 65 20 74 68 61 74 20 63 61 6e 6e tabase that cann
73f0: 6f 74 20 62 65 20 64 6f 6e 65 0a 2a 2a 20 62 79 ot be done.** by
7400: 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 [sqlite3_prepar
7410: 65 5f 76 32 28 29 5d 2c 20 5b 73 71 6c 69 74 65 e_v2()], [sqlite
7420: 33 5f 73 74 65 70 28 29 5d 2c 20 61 6e 64 20 5b 3_step()], and [
7430: 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 sqlite3_finalize
7440: 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 ()]..**.** The f
7450: 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 74 irst parameter t
7460: 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 o [sqlite3_exec(
7470: 29 5d 20 6d 75 73 74 20 62 65 20 61 6e 20 76 61 )] must be an va
7480: 6c 69 64 20 61 6e 64 20 6f 70 65 6e 0a 2a 2a 20 lid and open.**
7490: 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
74a0: 74 69 6f 6e 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 tion]..**.** The
74b0: 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
74c0: 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 62 65 tion must not be
74d0: 20 63 6c 6f 73 65 64 20 77 68 69 6c 65 0a 2a 2a closed while.**
74e0: 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 [sqlite3_exec()
74f0: 5d 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a ] is running..**
7500: 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 .** The calling
7510: 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 function should
7520: 75 73 65 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 use [sqlite3_fre
7530: 65 28 29 5d 20 74 6f 20 66 72 65 65 0a 2a 2a 20 e()] to free.**
7540: 74 68 65 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 the memory that
7550: 2a 65 72 72 6d 73 67 20 69 73 20 6c 65 66 74 20 *errmsg is left
7560: 70 6f 69 6e 74 69 6e 67 20 61 74 20 6f 6e 63 65 pointing at once
7570: 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 the error.** me
7580: 73 73 61 67 65 20 69 73 20 6e 6f 20 6c 6f 6e 67 ssage is no long
7590: 65 72 20 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a 2a er needed..**.**
75a0: 20 54 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 The SQL stateme
75b0: 6e 74 20 74 65 78 74 20 69 6e 20 74 68 65 20 32 nt text in the 2
75c0: 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 nd parameter to
75d0: 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d [sqlite3_exec()]
75e0: 0a 2a 2a 20 6d 75 73 74 20 72 65 6d 61 69 6e 20 .** must remain
75f0: 75 6e 63 68 61 6e 67 65 64 20 77 68 69 6c 65 20 unchanged while
7600: 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d [sqlite3_exec()]
7610: 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a is running..**.
7620: 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
7630: 0a 2a 2a 20 5b 48 31 32 31 30 31 5d 20 5b 48 31 .** [H12101] [H1
7640: 32 31 30 32 5d 20 5b 48 31 32 31 30 34 5d 20 5b 2102] [H12104] [
7650: 48 31 32 31 30 35 5d 20 5b 48 31 32 31 30 37 5d H12105] [H12107]
7660: 20 5b 48 31 32 31 31 30 5d 20 5b 48 31 32 31 31 [H12110] [H1211
7670: 33 5d 20 5b 48 31 32 31 31 36 5d 0a 2a 2a 20 5b 3] [H12116].** [
7680: 48 31 32 31 31 39 5d 20 5b 48 31 32 31 32 32 5d H12119] [H12122]
7690: 20 5b 48 31 32 31 32 35 5d 20 5b 48 31 32 31 33 [H12125] [H1213
76a0: 31 5d 20 5b 48 31 32 31 33 34 5d 20 5b 48 31 32 1] [H12134] [H12
76b0: 31 33 37 5d 20 5b 48 31 32 31 33 38 5d 0a 2a 2f 137] [H12138].*/
76c0: 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
76d0: 73 71 6c 69 74 65 33 5f 65 78 65 63 28 0a 20 20 sqlite3_exec(.
76e0: 73 71 6c 69 74 65 33 2a 2c 20 20 20 20 20 20 20 sqlite3*,
76f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7700: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e /* An
7710: 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 2a open database *
7720: 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
7730: 73 71 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 sql,
7740: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
7750: 2a 20 53 51 4c 20 74 6f 20 62 65 20 65 76 61 6c * SQL to be eval
7760: 75 61 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 28 uated */. int (
7770: 2a 63 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a *callback)(void*
7780: 2c 69 6e 74 2c 63 68 61 72 2a 2a 2c 63 68 61 72 ,int,char**,char
7790: 2a 2a 29 2c 20 20 2f 2a 20 43 61 6c 6c 62 61 63 **), /* Callbac
77a0: 6b 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 k function */.
77b0: 76 6f 69 64 20 2a 2c 20 20 20 20 20 20 20 20 20 void *,
77c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77d0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 73 /* 1s
77e0: 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 63 61 t argument to ca
77f0: 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 63 68 61 72 llback */. char
7800: 20 2a 2a 65 72 72 6d 73 67 20 20 20 20 20 20 20 **errmsg
7810: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7820: 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 /* Error
7830: 6d 73 67 20 77 72 69 74 74 65 6e 20 68 65 72 65 msg written here
7840: 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 */.);../*.** CA
7850: 50 49 33 52 45 46 3a 20 52 65 73 75 6c 74 20 43 PI3REF: Result C
7860: 6f 64 65 73 20 7b 48 31 30 32 31 30 7d 20 3c 53 odes {H10210} <S
7870: 31 30 37 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 10700>.** KEYWOR
7880: 44 53 3a 20 53 51 4c 49 54 45 5f 4f 4b 20 7b 65 DS: SQLITE_OK {e
7890: 72 72 6f 72 20 63 6f 64 65 7d 20 7b 65 72 72 6f rror code} {erro
78a0: 72 20 63 6f 64 65 73 7d 0a 2a 2a 20 4b 45 59 57 r codes}.** KEYW
78b0: 4f 52 44 53 3a 20 7b 72 65 73 75 6c 74 20 63 6f ORDS: {result co
78c0: 64 65 7d 20 7b 72 65 73 75 6c 74 20 63 6f 64 65 de} {result code
78d0: 73 7d 0a 2a 2a 0a 2a 2a 20 4d 61 6e 79 20 53 51 s}.**.** Many SQ
78e0: 4c 69 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 72 Lite functions r
78f0: 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 eturn an integer
7900: 20 72 65 73 75 6c 74 20 63 6f 64 65 20 66 72 6f result code fro
7910: 6d 20 74 68 65 20 73 65 74 20 73 68 6f 77 6e 0a m the set shown.
7920: 2a 2a 20 68 65 72 65 20 69 6e 20 6f 72 64 65 72 ** here in order
7930: 20 74 6f 20 69 6e 64 69 63 61 74 65 73 20 73 75 to indicates su
7940: 63 63 65 73 73 20 6f 72 20 66 61 69 6c 75 72 65 ccess or failure
7950: 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 65 72 72 6f ..**.** New erro
7960: 72 20 63 6f 64 65 73 20 6d 61 79 20 62 65 20 61 r codes may be a
7970: 64 64 65 64 20 69 6e 20 66 75 74 75 72 65 20 76 dded in future v
7980: 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 ersions of SQLit
7990: 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 e..**.** See als
79a0: 6f 3a 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 o: [SQLITE_IOERR
79b0: 5f 52 45 41 44 20 7c 20 65 78 74 65 6e 64 65 64 _READ | extended
79c0: 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d 0a 2a result codes].*
79d0: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
79e0: 5f 4f 4b 20 20 20 20 20 20 20 20 20 20 20 30 20 _OK 0
79f0: 20 20 2f 2a 20 53 75 63 63 65 73 73 66 75 6c 20 /* Successful
7a00: 72 65 73 75 6c 74 20 2a 2f 0a 2f 2a 20 62 65 67 result */./* beg
7a10: 69 6e 6e 69 6e 67 2d 6f 66 2d 65 72 72 6f 72 2d inning-of-error-
7a20: 63 6f 64 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 codes */.#define
7a30: 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 20 20 SQLITE_ERROR
7a40: 20 20 20 20 20 31 20 20 20 2f 2a 20 53 51 4c 20 1 /* SQL
7a50: 65 72 72 6f 72 20 6f 72 20 6d 69 73 73 69 6e 67 error or missing
7a60: 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 23 64 65 database */.#de
7a70: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 fine SQLITE_INTE
7a80: 52 4e 41 4c 20 20 20 20 20 32 20 20 20 2f 2a 20 RNAL 2 /*
7a90: 49 6e 74 65 72 6e 61 6c 20 6c 6f 67 69 63 20 65 Internal logic e
7aa0: 72 72 6f 72 20 69 6e 20 53 51 4c 69 74 65 20 2a rror in SQLite *
7ab0: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
7ac0: 5f 50 45 52 4d 20 20 20 20 20 20 20 20 20 33 20 _PERM 3
7ad0: 20 20 2f 2a 20 41 63 63 65 73 73 20 70 65 72 6d /* Access perm
7ae0: 69 73 73 69 6f 6e 20 64 65 6e 69 65 64 20 2a 2f ission denied */
7af0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
7b00: 41 42 4f 52 54 20 20 20 20 20 20 20 20 34 20 20 ABORT 4
7b10: 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 72 6f 75 /* Callback rou
7b20: 74 69 6e 65 20 72 65 71 75 65 73 74 65 64 20 61 tine requested a
7b30: 6e 20 61 62 6f 72 74 20 2a 2f 0a 23 64 65 66 69 n abort */.#defi
7b40: 6e 65 20 53 51 4c 49 54 45 5f 42 55 53 59 20 20 ne SQLITE_BUSY
7b50: 20 20 20 20 20 20 20 35 20 20 20 2f 2a 20 54 68 5 /* Th
7b60: 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
7b70: 69 73 20 6c 6f 63 6b 65 64 20 2a 2f 0a 23 64 65 is locked */.#de
7b80: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b fine SQLITE_LOCK
7b90: 45 44 20 20 20 20 20 20 20 36 20 20 20 2f 2a 20 ED 6 /*
7ba0: 41 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 64 A table in the d
7bb0: 61 74 61 62 61 73 65 20 69 73 20 6c 6f 63 6b 65 atabase is locke
7bc0: 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c d */.#define SQL
7bd0: 49 54 45 5f 4e 4f 4d 45 4d 20 20 20 20 20 20 20 ITE_NOMEM
7be0: 20 37 20 20 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63 7 /* A malloc
7bf0: 28 29 20 66 61 69 6c 65 64 20 2a 2f 0a 23 64 65 () failed */.#de
7c00: 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 45 41 44 fine SQLITE_READ
7c10: 4f 4e 4c 59 20 20 20 20 20 38 20 20 20 2f 2a 20 ONLY 8 /*
7c20: 41 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65 Attempt to write
7c30: 20 61 20 72 65 61 64 6f 6e 6c 79 20 64 61 74 61 a readonly data
7c40: 62 61 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 base */.#define
7c50: 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 SQLITE_INTERRUPT
7c60: 20 20 20 20 39 20 20 20 2f 2a 20 4f 70 65 72 61 9 /* Opera
7c70: 74 69 6f 6e 20 74 65 72 6d 69 6e 61 74 65 64 20 tion terminated
7c80: 62 79 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 by sqlite3_inter
7c90: 72 75 70 74 28 29 2a 2f 0a 23 64 65 66 69 6e 65 rupt()*/.#define
7ca0: 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 20 20 SQLITE_IOERR
7cb0: 20 20 20 20 31 30 20 20 20 2f 2a 20 53 6f 6d 65 10 /* Some
7cc0: 20 6b 69 6e 64 20 6f 66 20 64 69 73 6b 20 49 2f kind of disk I/
7cd0: 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 O error occurred
7ce0: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
7cf0: 54 45 5f 43 4f 52 52 55 50 54 20 20 20 20 20 31 TE_CORRUPT 1
7d00: 31 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 1 /* The datab
7d10: 61 73 65 20 64 69 73 6b 20 69 6d 61 67 65 20 69 ase disk image i
7d20: 73 20 6d 61 6c 66 6f 72 6d 65 64 20 2a 2f 0a 23 s malformed */.#
7d30: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 4f define SQLITE_NO
7d40: 54 46 4f 55 4e 44 20 20 20 20 31 32 20 20 20 2f TFOUND 12 /
7d50: 2a 20 4e 4f 54 20 55 53 45 44 2e 20 54 61 62 6c * NOT USED. Tabl
7d60: 65 20 6f 72 20 72 65 63 6f 72 64 20 6e 6f 74 20 e or record not
7d70: 66 6f 75 6e 64 20 2a 2f 0a 23 64 65 66 69 6e 65 found */.#define
7d80: 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 20 20 20 SQLITE_FULL
7d90: 20 20 20 20 31 33 20 20 20 2f 2a 20 49 6e 73 65 13 /* Inse
7da0: 72 74 69 6f 6e 20 66 61 69 6c 65 64 20 62 65 63 rtion failed bec
7db0: 61 75 73 65 20 64 61 74 61 62 61 73 65 20 69 73 ause database is
7dc0: 20 66 75 6c 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 full */.#define
7dd0: 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e SQLITE_CANTOPEN
7de0: 20 20 20 20 31 34 20 20 20 2f 2a 20 55 6e 61 62 14 /* Unab
7df0: 6c 65 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 64 le to open the d
7e00: 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a atabase file */.
7e10: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 #define SQLITE_P
7e20: 52 4f 54 4f 43 4f 4c 20 20 20 20 31 35 20 20 20 ROTOCOL 15
7e30: 2f 2a 20 4e 4f 54 20 55 53 45 44 2e 20 44 61 74 /* NOT USED. Dat
7e40: 61 62 61 73 65 20 6c 6f 63 6b 20 70 72 6f 74 6f abase lock proto
7e50: 63 6f 6c 20 65 72 72 6f 72 20 2a 2f 0a 23 64 65 col error */.#de
7e60: 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 4d 50 54 fine SQLITE_EMPT
7e70: 59 20 20 20 20 20 20 20 31 36 20 20 20 2f 2a 20 Y 16 /*
7e80: 44 61 74 61 62 61 73 65 20 69 73 20 65 6d 70 74 Database is empt
7e90: 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c y */.#define SQL
7ea0: 49 54 45 5f 53 43 48 45 4d 41 20 20 20 20 20 20 ITE_SCHEMA
7eb0: 31 37 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 17 /* The data
7ec0: 62 61 73 65 20 73 63 68 65 6d 61 20 63 68 61 6e base schema chan
7ed0: 67 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ged */.#define S
7ee0: 51 4c 49 54 45 5f 54 4f 4f 42 49 47 20 20 20 20 QLITE_TOOBIG
7ef0: 20 20 31 38 20 20 20 2f 2a 20 53 74 72 69 6e 67 18 /* String
7f00: 20 6f 72 20 42 4c 4f 42 20 65 78 63 65 65 64 73 or BLOB exceeds
7f10: 20 73 69 7a 65 20 6c 69 6d 69 74 20 2a 2f 0a 23 size limit */.#
7f20: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f define SQLITE_CO
7f30: 4e 53 54 52 41 49 4e 54 20 20 31 39 20 20 20 2f NSTRAINT 19 /
7f40: 2a 20 41 62 6f 72 74 20 64 75 65 20 74 6f 20 63 * Abort due to c
7f50: 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 onstraint violat
7f60: 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ion */.#define S
7f70: 51 4c 49 54 45 5f 4d 49 53 4d 41 54 43 48 20 20 QLITE_MISMATCH
7f80: 20 20 32 30 20 20 20 2f 2a 20 44 61 74 61 20 74 20 /* Data t
7f90: 79 70 65 20 6d 69 73 6d 61 74 63 68 20 2a 2f 0a ype mismatch */.
7fa0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d #define SQLITE_M
7fb0: 49 53 55 53 45 20 20 20 20 20 20 32 31 20 20 20 ISUSE 21
7fc0: 2f 2a 20 4c 69 62 72 61 72 79 20 75 73 65 64 20 /* Library used
7fd0: 69 6e 63 6f 72 72 65 63 74 6c 79 20 2a 2f 0a 23 incorrectly */.#
7fe0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 4f define SQLITE_NO
7ff0: 4c 46 53 20 20 20 20 20 20 20 32 32 20 20 20 2f LFS 22 /
8000: 2a 20 55 73 65 73 20 4f 53 20 66 65 61 74 75 72 * Uses OS featur
8010: 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 65 64 es not supported
8020: 20 6f 6e 20 68 6f 73 74 20 2a 2f 0a 23 64 65 66 on host */.#def
8030: 69 6e 65 20 53 51 4c 49 54 45 5f 41 55 54 48 20 ine SQLITE_AUTH
8040: 20 20 20 20 20 20 20 32 33 20 20 20 2f 2a 20 41 23 /* A
8050: 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 64 65 6e uthorization den
8060: 69 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ied */.#define S
8070: 51 4c 49 54 45 5f 46 4f 52 4d 41 54 20 20 20 20 QLITE_FORMAT
8080: 20 20 32 34 20 20 20 2f 2a 20 41 75 78 69 6c 69 24 /* Auxili
8090: 61 72 79 20 64 61 74 61 62 61 73 65 20 66 6f 72 ary database for
80a0: 6d 61 74 20 65 72 72 6f 72 20 2a 2f 0a 23 64 65 mat error */.#de
80b0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 41 4e 47 fine SQLITE_RANG
80c0: 45 20 20 20 20 20 20 20 32 35 20 20 20 2f 2a 20 E 25 /*
80d0: 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 2nd parameter to
80e0: 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 20 6f 75 sqlite3_bind ou
80f0: 74 20 6f 66 20 72 61 6e 67 65 20 2a 2f 0a 23 64 t of range */.#d
8100: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 4f 54 efine SQLITE_NOT
8110: 41 44 42 20 20 20 20 20 20 32 36 20 20 20 2f 2a ADB 26 /*
8120: 20 46 69 6c 65 20 6f 70 65 6e 65 64 20 74 68 61 File opened tha
8130: 74 20 69 73 20 6e 6f 74 20 61 20 64 61 74 61 62 t is not a datab
8140: 61 73 65 20 66 69 6c 65 20 2a 2f 0a 23 64 65 66 ase file */.#def
8150: 69 6e 65 20 53 51 4c 49 54 45 5f 52 4f 57 20 20 ine SQLITE_ROW
8160: 20 20 20 20 20 20 20 31 30 30 20 20 2f 2a 20 73 100 /* s
8170: 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 68 61 qlite3_step() ha
8180: 73 20 61 6e 6f 74 68 65 72 20 72 6f 77 20 72 65 s another row re
8190: 61 64 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ady */.#define S
81a0: 51 4c 49 54 45 5f 44 4f 4e 45 20 20 20 20 20 20 QLITE_DONE
81b0: 20 20 31 30 31 20 20 2f 2a 20 73 71 6c 69 74 65 101 /* sqlite
81c0: 33 5f 73 74 65 70 28 29 20 68 61 73 20 66 69 6e 3_step() has fin
81d0: 69 73 68 65 64 20 65 78 65 63 75 74 69 6e 67 20 ished executing
81e0: 2a 2f 0a 2f 2a 20 65 6e 64 2d 6f 66 2d 65 72 72 */./* end-of-err
81f0: 6f 72 2d 63 6f 64 65 73 20 2a 2f 0a 0a 2f 2a 0a or-codes */../*.
8200: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 78 74 ** CAPI3REF: Ext
8210: 65 6e 64 65 64 20 52 65 73 75 6c 74 20 43 6f 64 ended Result Cod
8220: 65 73 20 7b 48 31 30 32 32 30 7d 20 3c 53 31 30 es {H10220} <S10
8230: 37 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 700>.** KEYWORDS
8240: 3a 20 7b 65 78 74 65 6e 64 65 64 20 65 72 72 6f : {extended erro
8250: 72 20 63 6f 64 65 7d 20 7b 65 78 74 65 6e 64 65 r code} {extende
8260: 64 20 65 72 72 6f 72 20 63 6f 64 65 73 7d 0a 2a d error codes}.*
8270: 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 65 78 74 * KEYWORDS: {ext
8280: 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 ended result cod
8290: 65 7d 20 7b 65 78 74 65 6e 64 65 64 20 72 65 73 e} {extended res
82a0: 75 6c 74 20 63 6f 64 65 73 7d 0a 2a 2a 0a 2a 2a ult codes}.**.**
82b0: 20 49 6e 20 69 74 73 20 64 65 66 61 75 6c 74 20 In its default
82c0: 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2c 20 53 configuration, S
82d0: 51 4c 69 74 65 20 41 50 49 20 72 6f 75 74 69 6e QLite API routin
82e0: 65 73 20 72 65 74 75 72 6e 20 6f 6e 65 20 6f 66 es return one of
82f0: 20 32 36 20 69 6e 74 65 67 65 72 0a 2a 2a 20 5b 26 integer.** [
8300: 53 51 4c 49 54 45 5f 4f 4b 20 7c 20 72 65 73 75 SQLITE_OK | resu
8310: 6c 74 20 63 6f 64 65 73 5d 2e 20 20 48 6f 77 65 lt codes]. Howe
8320: 76 65 72 2c 20 65 78 70 65 72 69 65 6e 63 65 20 ver, experience
8330: 68 61 73 20 73 68 6f 77 6e 20 74 68 61 74 20 6d has shown that m
8340: 61 6e 79 20 6f 66 0a 2a 2a 20 74 68 65 73 65 20 any of.** these
8350: 72 65 73 75 6c 74 20 63 6f 64 65 73 20 61 72 65 result codes are
8360: 20 74 6f 6f 20 63 6f 61 72 73 65 2d 67 72 61 69 too coarse-grai
8370: 6e 65 64 2e 20 20 54 68 65 79 20 64 6f 20 6e 6f ned. They do no
8380: 74 20 70 72 6f 76 69 64 65 20 61 73 0a 2a 2a 20 t provide as.**
8390: 6d 75 63 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e much information
83a0: 20 61 62 6f 75 74 20 70 72 6f 62 6c 65 6d 73 20 about problems
83b0: 61 73 20 70 72 6f 67 72 61 6d 6d 65 72 73 20 6d as programmers m
83c0: 69 67 68 74 20 6c 69 6b 65 2e 20 20 49 6e 20 61 ight like. In a
83d0: 6e 20 65 66 66 6f 72 74 20 74 6f 0a 2a 2a 20 61 n effort to.** a
83e0: 64 64 72 65 73 73 20 74 68 69 73 2c 20 6e 65 77 ddress this, new
83f0: 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 er versions of S
8400: 51 4c 69 74 65 20 28 76 65 72 73 69 6f 6e 20 33 QLite (version 3
8410: 2e 33 2e 38 20 61 6e 64 20 6c 61 74 65 72 29 20 .3.8 and later)
8420: 69 6e 63 6c 75 64 65 0a 2a 2a 20 73 75 70 70 6f include.** suppo
8430: 72 74 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 rt for additiona
8440: 6c 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 74 l result codes t
8450: 68 61 74 20 70 72 6f 76 69 64 65 20 6d 6f 72 65 hat provide more
8460: 20 64 65 74 61 69 6c 65 64 20 69 6e 66 6f 72 6d detailed inform
8470: 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 65 ation.** about e
8480: 72 72 6f 72 73 2e 20 54 68 65 20 65 78 74 65 6e rrors. The exten
8490: 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 ded result codes
84a0: 20 61 72 65 20 65 6e 61 62 6c 65 64 20 6f 72 20 are enabled or
84b0: 64 69 73 61 62 6c 65 64 0a 2a 2a 20 6f 6e 20 61 disabled.** on a
84c0: 20 70 65 72 20 64 61 74 61 62 61 73 65 20 63 6f per database co
84d0: 6e 6e 65 63 74 69 6f 6e 20 62 61 73 69 73 20 75 nnection basis u
84e0: 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 73 71 6c sing the.** [sql
84f0: 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65 ite3_extended_re
8500: 73 75 6c 74 5f 63 6f 64 65 73 28 29 5d 20 41 50 sult_codes()] AP
8510: 49 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 66 I..**.** Some of
8520: 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20 65 the available e
8530: 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 xtended result c
8540: 6f 64 65 73 20 61 72 65 20 6c 69 73 74 65 64 20 odes are listed
8550: 68 65 72 65 2e 0a 2a 2a 20 4f 6e 65 20 6d 61 79 here..** One may
8560: 20 65 78 70 65 63 74 20 74 68 65 20 6e 75 6d 62 expect the numb
8570: 65 72 20 6f 66 20 65 78 74 65 6e 64 65 64 20 72 er of extended r
8580: 65 73 75 6c 74 20 63 6f 64 65 73 20 77 69 6c 6c esult codes will
8590: 20 62 65 20 65 78 70 61 6e 64 0a 2a 2a 20 6f 76 be expand.** ov
85a0: 65 72 20 74 69 6d 65 2e 20 20 53 6f 66 74 77 61 er time. Softwa
85b0: 72 65 20 74 68 61 74 20 75 73 65 73 20 65 78 74 re that uses ext
85c0: 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 ended result cod
85d0: 65 73 20 73 68 6f 75 6c 64 20 65 78 70 65 63 74 es should expect
85e0: 0a 2a 2a 20 74 6f 20 73 65 65 20 6e 65 77 20 72 .** to see new r
85f0: 65 73 75 6c 74 20 63 6f 64 65 73 20 69 6e 20 66 esult codes in f
8600: 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 20 6f uture releases o
8610: 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 f SQLite..**.**
8620: 54 68 65 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 The SQLITE_OK re
8630: 73 75 6c 74 20 63 6f 64 65 20 77 69 6c 6c 20 6e sult code will n
8640: 65 76 65 72 20 62 65 20 65 78 74 65 6e 64 65 64 ever be extended
8650: 2e 20 20 49 74 20 77 69 6c 6c 20 61 6c 77 61 79 . It will alway
8660: 73 0a 2a 2a 20 62 65 20 65 78 61 63 74 6c 79 20 s.** be exactly
8670: 7a 65 72 6f 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 zero..*/.#define
8680: 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 SQLITE_IOERR_RE
8690: 41 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 AD
86a0: 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 (SQLITE_IOERR |
86b0: 28 31 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 (1<<8)).#define
86c0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f SQLITE_IOERR_SHO
86d0: 52 54 5f 52 45 41 44 20 20 20 20 20 20 20 20 28 RT_READ (
86e0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 SQLITE_IOERR | (
86f0: 32 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 2<<8)).#define S
8700: 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52 49 54 QLITE_IOERR_WRIT
8710: 45 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53 E (S
8720: 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 33 QLITE_IOERR | (3
8730: 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 <<8)).#define SQ
8740: 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 59 4e 43 LITE_IOERR_FSYNC
8750: 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 (SQ
8760: 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 34 3c LITE_IOERR | (4<
8770: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c <8)).#define SQL
8780: 49 54 45 5f 49 4f 45 52 52 5f 44 49 52 5f 46 53 ITE_IOERR_DIR_FS
8790: 59 4e 43 20 20 20 20 20 20 20 20 20 28 53 51 4c YNC (SQL
87a0: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 35 3c 3c ITE_IOERR | (5<<
87b0: 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 8)).#define SQLI
87c0: 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 TE_IOERR_TRUNCAT
87d0: 45 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 E (SQLI
87e0: 54 45 5f 49 4f 45 52 52 20 7c 20 28 36 3c 3c 38 TE_IOERR | (6<<8
87f0: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 )).#define SQLIT
8800: 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 20 20 20 E_IOERR_FSTAT
8810: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 (SQLIT
8820: 45 5f 49 4f 45 52 52 20 7c 20 28 37 3c 3c 38 29 E_IOERR | (7<<8)
8830: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 ).#define SQLITE
8840: 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 20 20 20 _IOERR_UNLOCK
8850: 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 (SQLITE
8860: 5f 49 4f 45 52 52 20 7c 20 28 38 3c 3c 38 29 29 _IOERR | (8<<8))
8870: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
8880: 49 4f 45 52 52 5f 52 44 4c 4f 43 4b 20 20 20 20 IOERR_RDLOCK
8890: 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f (SQLITE_
88a0: 49 4f 45 52 52 20 7c 20 28 39 3c 3c 38 29 29 0a IOERR | (9<<8)).
88b0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
88c0: 4f 45 52 52 5f 44 45 4c 45 54 45 20 20 20 20 20 OERR_DELETE
88d0: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 (SQLITE_I
88e0: 4f 45 52 52 20 7c 20 28 31 30 3c 3c 38 29 29 0a OERR | (10<<8)).
88f0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
8900: 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 20 20 20 OERR_BLOCKED
8910: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 (SQLITE_I
8920: 4f 45 52 52 20 7c 20 28 31 31 3c 3c 38 29 29 0a OERR | (11<<8)).
8930: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
8940: 4f 45 52 52 5f 4e 4f 4d 45 4d 20 20 20 20 20 20 OERR_NOMEM
8950: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 (SQLITE_I
8960: 4f 45 52 52 20 7c 20 28 31 32 3c 3c 38 29 29 0a OERR | (12<<8)).
8970: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
8980: 4f 45 52 52 5f 41 43 43 45 53 53 20 20 20 20 20 OERR_ACCESS
8990: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 (SQLITE_I
89a0: 4f 45 52 52 20 7c 20 28 31 33 3c 3c 38 29 29 0a OERR | (13<<8)).
89b0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
89c0: 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 OERR_CHECKRESERV
89d0: 45 44 4c 4f 43 4b 20 28 53 51 4c 49 54 45 5f 49 EDLOCK (SQLITE_I
89e0: 4f 45 52 52 20 7c 20 28 31 34 3c 3c 38 29 29 0a OERR | (14<<8)).
89f0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
8a00: 4f 45 52 52 5f 4c 4f 43 4b 20 20 20 20 20 20 20 OERR_LOCK
8a10: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 (SQLITE_I
8a20: 4f 45 52 52 20 7c 20 28 31 35 3c 3c 38 29 29 0a OERR | (15<<8)).
8a30: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
8a40: 4f 45 52 52 5f 43 4c 4f 53 45 20 20 20 20 20 20 OERR_CLOSE
8a50: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 (SQLITE_I
8a60: 4f 45 52 52 20 7c 20 28 31 36 3c 3c 38 29 29 0a OERR | (16<<8)).
8a70: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
8a80: 4f 45 52 52 5f 44 49 52 5f 43 4c 4f 53 45 20 20 OERR_DIR_CLOSE
8a90: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 (SQLITE_I
8aa0: 4f 45 52 52 20 7c 20 28 31 37 3c 3c 38 29 29 0a OERR | (17<<8)).
8ab0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c #define SQLITE_L
8ac0: 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48 OCKED_SHAREDCACH
8ad0: 45 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 4c E (SQLITE_L
8ae0: 4f 43 4b 45 44 20 7c 20 28 31 3c 3c 38 29 20 29 OCKED | (1<<8) )
8af0: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
8b00: 3a 20 46 6c 61 67 73 20 46 6f 72 20 46 69 6c 65 : Flags For File
8b10: 20 4f 70 65 6e 20 4f 70 65 72 61 74 69 6f 6e 73 Open Operations
8b20: 20 7b 48 31 30 32 33 30 7d 20 3c 48 31 31 31 32 {H10230} <H1112
8b30: 30 3e 20 3c 48 31 32 37 30 30 3e 0a 2a 2a 0a 2a 0> <H12700>.**.*
8b40: 2a 20 54 68 65 73 65 20 62 69 74 20 76 61 6c 75 * These bit valu
8b50: 65 73 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 es are intended
8b60: 66 6f 72 20 75 73 65 20 69 6e 20 74 68 65 0a 2a for use in the.*
8b70: 2a 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 * 3rd parameter
8b80: 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f to the [sqlite3_
8b90: 6f 70 65 6e 5f 76 32 28 29 5d 20 69 6e 74 65 72 open_v2()] inter
8ba0: 66 61 63 65 20 61 6e 64 0a 2a 2a 20 69 6e 20 74 face and.** in t
8bb0: 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 he 4th parameter
8bc0: 20 74 6f 20 74 68 65 20 78 4f 70 65 6e 20 6d 65 to the xOpen me
8bd0: 74 68 6f 64 20 6f 66 20 74 68 65 0a 2a 2a 20 5b thod of the.** [
8be0: 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a sqlite3_vfs] obj
8bf0: 65 63 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ect..*/.#define
8c00: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
8c10: 4f 4e 4c 59 20 20 20 20 20 20 20 20 20 30 78 30 ONLY 0x0
8c20: 30 30 30 30 30 30 31 20 20 2f 2a 20 4f 6b 20 66 0000001 /* Ok f
8c30: 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f or sqlite3_open_
8c40: 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 v2() */.#define
8c50: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
8c60: 57 52 49 54 45 20 20 20 20 20 20 20 20 30 78 30 WRITE 0x0
8c70: 30 30 30 30 30 30 32 20 20 2f 2a 20 4f 6b 20 66 0000002 /* Ok f
8c80: 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f or sqlite3_open_
8c90: 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 v2() */.#define
8ca0: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 SQLITE_OPEN_CREA
8cb0: 54 45 20 20 20 20 20 20 20 20 20 20 20 30 78 30 TE 0x0
8cc0: 30 30 30 30 30 30 34 20 20 2f 2a 20 4f 6b 20 66 0000004 /* Ok f
8cd0: 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f or sqlite3_open_
8ce0: 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 v2() */.#define
8cf0: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 SQLITE_OPEN_DELE
8d00: 54 45 4f 4e 43 4c 4f 53 45 20 20 20 20 30 78 30 TEONCLOSE 0x0
8d10: 30 30 30 30 30 30 38 20 20 2f 2a 20 56 46 53 20 0000008 /* VFS
8d20: 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 only */.#define
8d30: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c SQLITE_OPEN_EXCL
8d40: 55 53 49 56 45 20 20 20 20 20 20 20 20 30 78 30 USIVE 0x0
8d50: 30 30 30 30 30 31 30 20 20 2f 2a 20 56 46 53 20 0000010 /* VFS
8d60: 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 only */.#define
8d70: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e SQLITE_OPEN_MAIN
8d80: 5f 44 42 20 20 20 20 20 20 20 20 20 20 30 78 30 _DB 0x0
8d90: 30 30 30 30 31 30 30 20 20 2f 2a 20 56 46 53 20 0000100 /* VFS
8da0: 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 only */.#define
8db0: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 SQLITE_OPEN_TEMP
8dc0: 5f 44 42 20 20 20 20 20 20 20 20 20 20 30 78 30 _DB 0x0
8dd0: 30 30 30 30 32 30 30 20 20 2f 2a 20 56 46 53 20 0000200 /* VFS
8de0: 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 only */.#define
8df0: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 52 41 4e SQLITE_OPEN_TRAN
8e00: 53 49 45 4e 54 5f 44 42 20 20 20 20 20 30 78 30 SIENT_DB 0x0
8e10: 30 30 30 30 34 30 30 20 20 2f 2a 20 56 46 53 20 0000400 /* VFS
8e20: 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 only */.#define
8e30: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e SQLITE_OPEN_MAIN
8e40: 5f 4a 4f 55 52 4e 41 4c 20 20 20 20 20 30 78 30 _JOURNAL 0x0
8e50: 30 30 30 30 38 30 30 20 20 2f 2a 20 56 46 53 20 0000800 /* VFS
8e60: 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 only */.#define
8e70: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 SQLITE_OPEN_TEMP
8e80: 5f 4a 4f 55 52 4e 41 4c 20 20 20 20 20 30 78 30 _JOURNAL 0x0
8e90: 30 30 30 31 30 30 30 20 20 2f 2a 20 56 46 53 20 0001000 /* VFS
8ea0: 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 only */.#define
8eb0: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42 4a SQLITE_OPEN_SUBJ
8ec0: 4f 55 52 4e 41 4c 20 20 20 20 20 20 20 30 78 30 OURNAL 0x0
8ed0: 30 30 30 32 30 30 30 20 20 2f 2a 20 56 46 53 20 0002000 /* VFS
8ee0: 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 only */.#define
8ef0: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 SQLITE_OPEN_MAST
8f00: 45 52 5f 4a 4f 55 52 4e 41 4c 20 20 20 30 78 30 ER_JOURNAL 0x0
8f10: 30 30 30 34 30 30 30 20 20 2f 2a 20 56 46 53 20 0004000 /* VFS
8f20: 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 only */.#define
8f30: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 SQLITE_OPEN_NOMU
8f40: 54 45 58 20 20 20 20 20 20 20 20 20 20 30 78 30 TEX 0x0
8f50: 30 30 30 38 30 30 30 20 20 2f 2a 20 4f 6b 20 66 0008000 /* Ok f
8f60: 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f or sqlite3_open_
8f70: 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 v2() */.#define
8f80: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c SQLITE_OPEN_FULL
8f90: 4d 55 54 45 58 20 20 20 20 20 20 20 20 30 78 30 MUTEX 0x0
8fa0: 30 30 31 30 30 30 30 20 20 2f 2a 20 4f 6b 20 66 0010000 /* Ok f
8fb0: 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f or sqlite3_open_
8fc0: 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 v2() */.#define
8fd0: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41 52 SQLITE_OPEN_SHAR
8fe0: 45 44 43 41 43 48 45 20 20 20 20 20 20 30 78 30 EDCACHE 0x0
8ff0: 30 30 32 30 30 30 30 20 20 2f 2a 20 4f 6b 20 66 0020000 /* Ok f
9000: 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f or sqlite3_open_
9010: 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 v2() */.#define
9020: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 50 52 49 56 SQLITE_OPEN_PRIV
9030: 41 54 45 43 41 43 48 45 20 20 20 20 20 30 78 30 ATECACHE 0x0
9040: 30 30 34 30 30 30 30 20 20 2f 2a 20 4f 6b 20 66 0040000 /* Ok f
9050: 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f or sqlite3_open_
9060: 76 32 28 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 v2() */../*.** C
9070: 41 50 49 33 52 45 46 3a 20 44 65 76 69 63 65 20 API3REF: Device
9080: 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20 Characteristics
9090: 7b 48 31 30 32 34 30 7d 20 3c 48 31 31 31 32 30 {H10240} <H11120
90a0: 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 44 65 76 >.**.** The xDev
90b0: 69 63 65 43 61 70 61 62 69 6c 69 74 69 65 73 20 iceCapabilities
90c0: 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 5b 73 method of the [s
90d0: 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
90e0: 73 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 72 65 74 s].** object ret
90f0: 75 72 6e 73 20 61 6e 20 69 6e 74 65 67 65 72 20 urns an integer
9100: 77 68 69 63 68 20 69 73 20 61 20 76 65 63 74 6f which is a vecto
9110: 72 20 6f 66 20 74 68 65 20 74 68 65 73 65 0a 2a r of the these.*
9120: 2a 20 62 69 74 20 76 61 6c 75 65 73 20 65 78 70 * bit values exp
9130: 72 65 73 73 69 6e 67 20 49 2f 4f 20 63 68 61 72 ressing I/O char
9140: 61 63 74 65 72 69 73 74 69 63 73 20 6f 66 20 74 acteristics of t
9150: 68 65 20 6d 61 73 73 20 73 74 6f 72 61 67 65 0a he mass storage.
9160: 2a 2a 20 64 65 76 69 63 65 20 74 68 61 74 20 68 ** device that h
9170: 6f 6c 64 73 20 74 68 65 20 66 69 6c 65 20 74 68 olds the file th
9180: 61 74 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f at the [sqlite3_
9190: 69 6f 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 72 io_methods].** r
91a0: 65 66 65 72 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 efers to..**.**
91b0: 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 The SQLITE_IOCAP
91c0: 5f 41 54 4f 4d 49 43 20 70 72 6f 70 65 72 74 79 _ATOMIC property
91d0: 20 6d 65 61 6e 73 20 74 68 61 74 20 61 6c 6c 20 means that all
91e0: 77 72 69 74 65 73 20 6f 66 0a 2a 2a 20 61 6e 79 writes of.** any
91f0: 20 73 69 7a 65 20 61 72 65 20 61 74 6f 6d 69 63 size are atomic
9200: 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f . The SQLITE_IO
9210: 43 41 50 5f 41 54 4f 4d 49 43 6e 6e 6e 20 76 61 CAP_ATOMICnnn va
9220: 6c 75 65 73 0a 2a 2a 20 6d 65 61 6e 20 74 68 61 lues.** mean tha
9230: 74 20 77 72 69 74 65 73 20 6f 66 20 62 6c 6f 63 t writes of bloc
9240: 6b 73 20 74 68 61 74 20 61 72 65 20 6e 6e 6e 20 ks that are nnn
9250: 62 79 74 65 73 20 69 6e 20 73 69 7a 65 20 61 6e bytes in size an
9260: 64 0a 2a 2a 20 61 72 65 20 61 6c 69 67 6e 65 64 d.** are aligned
9270: 20 74 6f 20 61 6e 20 61 64 64 72 65 73 73 20 77 to an address w
9280: 68 69 63 68 20 69 73 20 61 6e 20 69 6e 74 65 67 hich is an integ
9290: 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66 0a 2a er multiple of.*
92a0: 2a 20 6e 6e 6e 20 61 72 65 20 61 74 6f 6d 69 63 * nnn are atomic
92b0: 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f . The SQLITE_IO
92c0: 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 20 CAP_SAFE_APPEND
92d0: 76 61 6c 75 65 20 6d 65 61 6e 73 0a 2a 2a 20 74 value means.** t
92e0: 68 61 74 20 77 68 65 6e 20 64 61 74 61 20 69 73 hat when data is
92f0: 20 61 70 70 65 6e 64 65 64 20 74 6f 20 61 20 66 appended to a f
9300: 69 6c 65 2c 20 74 68 65 20 64 61 74 61 20 69 73 ile, the data is
9310: 20 61 70 70 65 6e 64 65 64 0a 2a 2a 20 66 69 72 appended.** fir
9320: 73 74 20 74 68 65 6e 20 74 68 65 20 73 69 7a 65 st then the size
9330: 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 of the file is
9340: 65 78 74 65 6e 64 65 64 2c 20 6e 65 76 65 72 20 extended, never
9350: 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20 77 61 79 the other.** way
9360: 20 61 72 6f 75 6e 64 2e 20 20 54 68 65 20 53 51 around. The SQ
9370: 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 LITE_IOCAP_SEQUE
9380: 4e 54 49 41 4c 20 70 72 6f 70 65 72 74 79 20 6d NTIAL property m
9390: 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 69 6e 66 eans that.** inf
93a0: 6f 72 6d 61 74 69 6f 6e 20 69 73 20 77 72 69 74 ormation is writ
93b0: 74 65 6e 20 74 6f 20 64 69 73 6b 20 69 6e 20 74 ten to disk in t
93c0: 68 65 20 73 61 6d 65 20 6f 72 64 65 72 20 61 73 he same order as
93d0: 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 78 57 72 calls.** to xWr
93e0: 69 74 65 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e ite()..*/.#defin
93f0: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 e SQLITE_IOCAP_A
9400: 54 4f 4d 49 43 20 20 20 20 20 20 20 20 20 20 30 TOMIC 0
9410: 78 30 30 30 30 30 30 30 31 0a 23 64 65 66 69 6e x00000001.#defin
9420: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 e SQLITE_IOCAP_A
9430: 54 4f 4d 49 43 35 31 32 20 20 20 20 20 20 20 30 TOMIC512 0
9440: 78 30 30 30 30 30 30 30 32 0a 23 64 65 66 69 6e x00000002.#defin
9450: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 e SQLITE_IOCAP_A
9460: 54 4f 4d 49 43 31 4b 20 20 20 20 20 20 20 20 30 TOMIC1K 0
9470: 78 30 30 30 30 30 30 30 34 0a 23 64 65 66 69 6e x00000004.#defin
9480: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 e SQLITE_IOCAP_A
9490: 54 4f 4d 49 43 32 4b 20 20 20 20 20 20 20 20 30 TOMIC2K 0
94a0: 78 30 30 30 30 30 30 30 38 0a 23 64 65 66 69 6e x00000008.#defin
94b0: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 e SQLITE_IOCAP_A
94c0: 54 4f 4d 49 43 34 4b 20 20 20 20 20 20 20 20 30 TOMIC4K 0
94d0: 78 30 30 30 30 30 30 31 30 0a 23 64 65 66 69 6e x00000010.#defin
94e0: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 e SQLITE_IOCAP_A
94f0: 54 4f 4d 49 43 38 4b 20 20 20 20 20 20 20 20 30 TOMIC8K 0
9500: 78 30 30 30 30 30 30 32 30 0a 23 64 65 66 69 6e x00000020.#defin
9510: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 e SQLITE_IOCAP_A
9520: 54 4f 4d 49 43 31 36 4b 20 20 20 20 20 20 20 30 TOMIC16K 0
9530: 78 30 30 30 30 30 30 34 30 0a 23 64 65 66 69 6e x00000040.#defin
9540: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 e SQLITE_IOCAP_A
9550: 54 4f 4d 49 43 33 32 4b 20 20 20 20 20 20 20 30 TOMIC32K 0
9560: 78 30 30 30 30 30 30 38 30 0a 23 64 65 66 69 6e x00000080.#defin
9570: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 e SQLITE_IOCAP_A
9580: 54 4f 4d 49 43 36 34 4b 20 20 20 20 20 20 20 30 TOMIC64K 0
9590: 78 30 30 30 30 30 31 30 30 0a 23 64 65 66 69 6e x00000100.#defin
95a0: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 e SQLITE_IOCAP_S
95b0: 41 46 45 5f 41 50 50 45 4e 44 20 20 20 20 20 30 AFE_APPEND 0
95c0: 78 30 30 30 30 30 32 30 30 0a 23 64 65 66 69 6e x00000200.#defin
95d0: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 e SQLITE_IOCAP_S
95e0: 45 51 55 45 4e 54 49 41 4c 20 20 20 20 20 20 30 EQUENTIAL 0
95f0: 78 30 30 30 30 30 34 30 30 0a 0a 2f 2a 0a 2a 2a x00000400../*.**
9600: 20 43 41 50 49 33 52 45 46 3a 20 46 69 6c 65 20 CAPI3REF: File
9610: 4c 6f 63 6b 69 6e 67 20 4c 65 76 65 6c 73 20 7b Locking Levels {
9620: 48 31 30 32 35 30 7d 20 3c 48 31 31 31 32 30 3e H10250} <H11120>
9630: 20 3c 48 31 31 33 31 30 3e 0a 2a 2a 0a 2a 2a 20 <H11310>.**.**
9640: 53 51 4c 69 74 65 20 75 73 65 73 20 6f 6e 65 20 SQLite uses one
9650: 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 65 72 of these integer
9660: 20 76 61 6c 75 65 73 20 61 73 20 74 68 65 20 73 values as the s
9670: 65 63 6f 6e 64 0a 2a 2a 20 61 72 67 75 6d 65 6e econd.** argumen
9680: 74 20 74 6f 20 63 61 6c 6c 73 20 69 74 20 6d 61 t to calls it ma
9690: 6b 65 73 20 74 6f 20 74 68 65 20 78 4c 6f 63 6b kes to the xLock
96a0: 28 29 20 61 6e 64 20 78 55 6e 6c 6f 63 6b 28 29 () and xUnlock()
96b0: 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 66 20 61 methods.** of a
96c0: 6e 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 n [sqlite3_io_me
96d0: 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a thods] object..*
96e0: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
96f0: 5f 4c 4f 43 4b 5f 4e 4f 4e 45 20 20 20 20 20 20 _LOCK_NONE
9700: 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 0.#define SQ
9710: 4c 49 54 45 5f 4c 4f 43 4b 5f 53 48 41 52 45 44 LITE_LOCK_SHARED
9720: 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 1.#defin
9730: 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 52 45 e SQLITE_LOCK_RE
9740: 53 45 52 56 45 44 20 20 20 20 20 20 32 0a 23 64 SERVED 2.#d
9750: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 efine SQLITE_LOC
9760: 4b 5f 50 45 4e 44 49 4e 47 20 20 20 20 20 20 20 K_PENDING
9770: 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 3.#define SQLITE
9780: 5f 4c 4f 43 4b 5f 45 58 43 4c 55 53 49 56 45 20 _LOCK_EXCLUSIVE
9790: 20 20 20 20 34 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 4../*.** CAP
97a0: 49 33 52 45 46 3a 20 53 79 6e 63 68 72 6f 6e 69 I3REF: Synchroni
97b0: 7a 61 74 69 6f 6e 20 54 79 70 65 20 46 6c 61 67 zation Type Flag
97c0: 73 20 7b 48 31 30 32 36 30 7d 20 3c 48 31 31 31 s {H10260} <H111
97d0: 32 30 3e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 53 20>.**.** When S
97e0: 51 4c 69 74 65 20 69 6e 76 6f 6b 65 73 20 74 68 QLite invokes th
97f0: 65 20 78 53 79 6e 63 28 29 20 6d 65 74 68 6f 64 e xSync() method
9800: 20 6f 66 20 61 6e 0a 2a 2a 20 5b 73 71 6c 69 74 of an.** [sqlit
9810: 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f e3_io_methods] o
9820: 62 6a 65 63 74 20 69 74 20 75 73 65 73 20 61 20 bject it uses a
9830: 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 0a 2a combination of.*
9840: 2a 20 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 * these integer
9850: 76 61 6c 75 65 73 20 61 73 20 74 68 65 20 73 65 values as the se
9860: 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a cond argument..*
9870: 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 53 51 *.** When the SQ
9880: 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e LITE_SYNC_DATAON
9890: 4c 59 20 66 6c 61 67 20 69 73 20 75 73 65 64 2c LY flag is used,
98a0: 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 it means that t
98b0: 68 65 0a 2a 2a 20 73 79 6e 63 20 6f 70 65 72 61 he.** sync opera
98c0: 74 69 6f 6e 20 6f 6e 6c 79 20 6e 65 65 64 73 20 tion only needs
98d0: 74 6f 20 66 6c 75 73 68 20 64 61 74 61 20 74 6f to flush data to
98e0: 20 6d 61 73 73 20 73 74 6f 72 61 67 65 2e 20 20 mass storage.
98f0: 49 6e 6f 64 65 0a 2a 2a 20 69 6e 66 6f 72 6d 61 Inode.** informa
9900: 74 69 6f 6e 20 6e 65 65 64 20 6e 6f 74 20 62 65 tion need not be
9910: 20 66 6c 75 73 68 65 64 2e 20 49 66 20 74 68 65 flushed. If the
9920: 20 6c 6f 77 65 72 20 66 6f 75 72 20 62 69 74 73 lower four bits
9930: 20 6f 66 20 74 68 65 20 66 6c 61 67 0a 2a 2a 20 of the flag.**
9940: 65 71 75 61 6c 20 53 51 4c 49 54 45 5f 53 59 4e equal SQLITE_SYN
9950: 43 5f 4e 4f 52 4d 41 4c 2c 20 74 68 61 74 20 6d C_NORMAL, that m
9960: 65 61 6e 73 20 74 6f 20 75 73 65 20 6e 6f 72 6d eans to use norm
9970: 61 6c 20 66 73 79 6e 63 28 29 20 73 65 6d 61 6e al fsync() seman
9980: 74 69 63 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 tics..** If the
9990: 6c 6f 77 65 72 20 66 6f 75 72 20 62 69 74 73 20 lower four bits
99a0: 65 71 75 61 6c 20 53 51 4c 49 54 45 5f 53 59 4e equal SQLITE_SYN
99b0: 43 5f 46 55 4c 4c 2c 20 74 68 61 74 20 6d 65 61 C_FULL, that mea
99c0: 6e 73 0a 2a 2a 20 74 6f 20 75 73 65 20 4d 61 63 ns.** to use Mac
99d0: 20 4f 53 20 58 20 73 74 79 6c 65 20 66 75 6c 6c OS X style full
99e0: 73 79 6e 63 20 69 6e 73 74 65 61 64 20 6f 66 20 sync instead of
99f0: 66 73 79 6e 63 28 29 2e 0a 2a 2f 0a 23 64 65 66 fsync()..*/.#def
9a00: 69 6e 65 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f ine SQLITE_SYNC_
9a10: 4e 4f 52 4d 41 4c 20 20 20 20 20 20 20 20 30 78 NORMAL 0x
9a20: 30 30 30 30 32 0a 23 64 65 66 69 6e 65 20 53 51 00002.#define SQ
9a30: 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 20 20 LITE_SYNC_FULL
9a40: 20 20 20 20 20 20 20 20 30 78 30 30 30 30 33 0a 0x00003.
9a50: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 #define SQLITE_S
9a60: 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 20 20 20 20 YNC_DATAONLY
9a70: 20 20 30 78 30 30 30 31 30 0a 0a 2f 2a 0a 2a 2a 0x00010../*.**
9a80: 20 43 41 50 49 33 52 45 46 3a 20 4f 53 20 49 6e CAPI3REF: OS In
9a90: 74 65 72 66 61 63 65 20 4f 70 65 6e 20 46 69 6c terface Open Fil
9aa0: 65 20 48 61 6e 64 6c 65 20 7b 48 31 31 31 31 30 e Handle {H11110
9ab0: 7d 20 3c 53 32 30 31 31 30 3e 0a 2a 2a 0a 2a 2a } <S20110>.**.**
9ac0: 20 41 6e 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c An [sqlite3_fil
9ad0: 65 5d 20 6f 62 6a 65 63 74 20 72 65 70 72 65 73 e] object repres
9ae0: 65 6e 74 73 20 61 6e 20 6f 70 65 6e 20 66 69 6c ents an open fil
9af0: 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20 5b 73 71 e in the .** [sq
9b00: 6c 69 74 65 33 5f 76 66 73 20 7c 20 4f 53 20 69 lite3_vfs | OS i
9b10: 6e 74 65 72 66 61 63 65 20 6c 61 79 65 72 5d 2e nterface layer].
9b20: 20 20 49 6e 64 69 76 69 64 75 61 6c 20 4f 53 20 Individual OS
9b30: 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 69 6d 70 interface.** imp
9b40: 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69 6c lementations wil
9b50: 6c 0a 2a 2a 20 77 61 6e 74 20 74 6f 20 73 75 62 l.** want to sub
9b60: 63 6c 61 73 73 20 74 68 69 73 20 6f 62 6a 65 63 class this objec
9b70: 74 20 62 79 20 61 70 70 65 6e 64 69 6e 67 20 61 t by appending a
9b80: 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73 dditional fields
9b90: 0a 2a 2a 20 66 6f 72 20 74 68 65 69 72 20 6f 77 .** for their ow
9ba0: 6e 20 75 73 65 2e 20 20 54 68 65 20 70 4d 65 74 n use. The pMet
9bb0: 68 6f 64 73 20 65 6e 74 72 79 20 69 73 20 61 20 hods entry is a
9bc0: 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a pointer to an.**
9bd0: 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 [sqlite3_io_met
9be0: 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20 74 68 61 hods] object tha
9bf0: 74 20 64 65 66 69 6e 65 73 20 6d 65 74 68 6f 64 t defines method
9c00: 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 69 6e 67 s for performing
9c10: 0a 2a 2a 20 49 2f 4f 20 6f 70 65 72 61 74 69 6f .** I/O operatio
9c20: 6e 73 20 6f 6e 20 74 68 65 20 6f 70 65 6e 20 66 ns on the open f
9c30: 69 6c 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 ile..*/.typedef
9c40: 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 66 struct sqlite3_f
9c50: 69 6c 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 ile sqlite3_file
9c60: 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 ;.struct sqlite3
9c70: 5f 66 69 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20 _file {. const
9c80: 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 struct sqlite3_i
9c90: 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74 68 o_methods *pMeth
9ca0: 6f 64 73 3b 20 20 2f 2a 20 4d 65 74 68 6f 64 73 ods; /* Methods
9cb0: 20 66 6f 72 20 61 6e 20 6f 70 65 6e 20 66 69 6c for an open fil
9cc0: 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 e */.};../*.** C
9cd0: 41 50 49 33 52 45 46 3a 20 4f 53 20 49 6e 74 65 API3REF: OS Inte
9ce0: 72 66 61 63 65 20 46 69 6c 65 20 56 69 72 74 75 rface File Virtu
9cf0: 61 6c 20 4d 65 74 68 6f 64 73 20 4f 62 6a 65 63 al Methods Objec
9d00: 74 20 7b 48 31 31 31 32 30 7d 20 3c 53 32 30 31 t {H11120} <S201
9d10: 31 30 3e 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 10>.**.** Every
9d20: 66 69 6c 65 20 6f 70 65 6e 65 64 20 62 79 20 74 file opened by t
9d30: 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d he [sqlite3_vfs]
9d40: 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 70 6f xOpen method po
9d50: 70 75 6c 61 74 65 73 20 61 6e 0a 2a 2a 20 5b 73 pulates an.** [s
9d60: 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f 62 6a qlite3_file] obj
9d70: 65 63 74 20 28 6f 72 2c 20 6d 6f 72 65 20 63 6f ect (or, more co
9d80: 6d 6d 6f 6e 6c 79 2c 20 61 20 73 75 62 63 6c 61 mmonly, a subcla
9d90: 73 73 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71 ss of the.** [sq
9da0: 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f 62 6a 65 lite3_file] obje
9db0: 63 74 29 20 77 69 74 68 20 61 20 70 6f 69 6e 74 ct) with a point
9dc0: 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 er to an instanc
9dd0: 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 e of this object
9de0: 2e 0a 2a 2a 20 54 68 69 73 20 6f 62 6a 65 63 74 ..** This object
9df0: 20 64 65 66 69 6e 65 73 20 74 68 65 20 6d 65 74 defines the met
9e00: 68 6f 64 73 20 75 73 65 64 20 74 6f 20 70 65 72 hods used to per
9e10: 66 6f 72 6d 20 76 61 72 69 6f 75 73 20 6f 70 65 form various ope
9e20: 72 61 74 69 6f 6e 73 0a 2a 2a 20 61 67 61 69 6e rations.** again
9e30: 73 74 20 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 st the open file
9e40: 20 72 65 70 72 65 73 65 6e 74 65 64 20 62 79 20 represented by
9e50: 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c the [sqlite3_fil
9e60: 65 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a e] object..**.**
9e70: 20 49 66 20 74 68 65 20 78 4f 70 65 6e 20 6d 65 If the xOpen me
9e80: 74 68 6f 64 20 73 65 74 73 20 74 68 65 20 73 71 thod sets the sq
9e90: 6c 69 74 65 33 5f 66 69 6c 65 2e 70 4d 65 74 68 lite3_file.pMeth
9ea0: 6f 64 73 20 65 6c 65 6d 65 6e 74 20 0a 2a 2a 20 ods element .**
9eb0: 74 6f 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 70 6f to a non-NULL po
9ec0: 69 6e 74 65 72 2c 20 74 68 65 6e 20 74 68 65 20 inter, then the
9ed0: 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
9ee0: 64 73 2e 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 ds.xClose method
9ef0: 0a 2a 2a 20 6d 61 79 20 62 65 20 69 6e 76 6f 6b .** may be invok
9f00: 65 64 20 65 76 65 6e 20 69 66 20 74 68 65 20 78 ed even if the x
9f10: 4f 70 65 6e 20 72 65 70 6f 72 74 65 64 20 74 68 Open reported th
9f20: 61 74 20 69 74 20 66 61 69 6c 65 64 2e 20 20 54 at it failed. T
9f30: 68 65 0a 2a 2a 20 6f 6e 6c 79 20 77 61 79 20 74 he.** only way t
9f40: 6f 20 70 72 65 76 65 6e 74 20 61 20 63 61 6c 6c o prevent a call
9f50: 20 74 6f 20 78 43 6c 6f 73 65 20 66 6f 6c 6c 6f to xClose follo
9f60: 77 69 6e 67 20 61 20 66 61 69 6c 65 64 20 78 4f wing a failed xO
9f70: 70 65 6e 0a 2a 2a 20 69 73 20 66 6f 72 20 74 68 pen.** is for th
9f80: 65 20 78 4f 70 65 6e 20 74 6f 20 73 65 74 20 74 e xOpen to set t
9f90: 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2e he sqlite3_file.
9fa0: 70 4d 65 74 68 6f 64 73 20 65 6c 65 6d 65 6e 74 pMethods element
9fb0: 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 to NULL..**.**
9fc0: 54 68 65 20 66 6c 61 67 73 20 61 72 67 75 6d 65 The flags argume
9fd0: 6e 74 20 74 6f 20 78 53 79 6e 63 20 6d 61 79 20 nt to xSync may
9fe0: 62 65 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 be one of [SQLIT
9ff0: 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 5d 20 6f E_SYNC_NORMAL] o
a000: 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 53 59 4e r.** [SQLITE_SYN
a010: 43 5f 46 55 4c 4c 5d 2e 20 20 54 68 65 20 66 69 C_FULL]. The fi
a020: 72 73 74 20 63 68 6f 69 63 65 20 69 73 20 74 68 rst choice is th
a030: 65 20 6e 6f 72 6d 61 6c 20 66 73 79 6e 63 28 29 e normal fsync()
a040: 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 ..** The second
a050: 63 68 6f 69 63 65 20 69 73 20 61 20 4d 61 63 20 choice is a Mac
a060: 4f 53 20 58 20 73 74 79 6c 65 20 66 75 6c 6c 73 OS X style fulls
a070: 79 6e 63 2e 20 20 54 68 65 20 5b 53 51 4c 49 54 ync. The [SQLIT
a080: 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 5d E_SYNC_DATAONLY]
a090: 0a 2a 2a 20 66 6c 61 67 20 6d 61 79 20 62 65 20 .** flag may be
a0a0: 4f 52 65 64 20 69 6e 20 74 6f 20 69 6e 64 69 63 ORed in to indic
a0b0: 61 74 65 20 74 68 61 74 20 6f 6e 6c 79 20 74 68 ate that only th
a0c0: 65 20 64 61 74 61 20 6f 66 20 74 68 65 20 66 69 e data of the fi
a0d0: 6c 65 0a 2a 2a 20 61 6e 64 20 6e 6f 74 20 69 74 le.** and not it
a0e0: 73 20 69 6e 6f 64 65 20 6e 65 65 64 73 20 74 6f s inode needs to
a0f0: 20 62 65 20 73 79 6e 63 65 64 2e 0a 2a 2a 0a 2a be synced..**.*
a100: 2a 20 54 68 65 20 69 6e 74 65 67 65 72 20 76 61 * The integer va
a110: 6c 75 65 73 20 74 6f 20 78 4c 6f 63 6b 28 29 20 lues to xLock()
a120: 61 6e 64 20 78 55 6e 6c 6f 63 6b 28 29 20 61 72 and xUnlock() ar
a130: 65 20 6f 6e 65 20 6f 66 0a 2a 2a 20 3c 75 6c 3e e one of.** <ul>
a140: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 .** <li> [SQLITE
a150: 5f 4c 4f 43 4b 5f 4e 4f 4e 45 5d 2c 0a 2a 2a 20 _LOCK_NONE],.**
a160: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 <li> [SQLITE_LOC
a170: 4b 5f 53 48 41 52 45 44 5d 2c 0a 2a 2a 20 3c 6c K_SHARED],.** <l
a180: 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f i> [SQLITE_LOCK_
a190: 52 45 53 45 52 56 45 44 5d 2c 0a 2a 2a 20 3c 6c RESERVED],.** <l
a1a0: 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f i> [SQLITE_LOCK_
a1b0: 50 45 4e 44 49 4e 47 5d 2c 20 6f 72 0a 2a 2a 20 PENDING], or.**
a1c0: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 <li> [SQLITE_LOC
a1d0: 4b 5f 45 58 43 4c 55 53 49 56 45 5d 2e 0a 2a 2a K_EXCLUSIVE]..**
a1e0: 20 3c 2f 75 6c 3e 0a 2a 2a 20 78 4c 6f 63 6b 28 </ul>.** xLock(
a1f0: 29 20 69 6e 63 72 65 61 73 65 73 20 74 68 65 20 ) increases the
a200: 6c 6f 63 6b 2e 20 78 55 6e 6c 6f 63 6b 28 29 20 lock. xUnlock()
a210: 64 65 63 72 65 61 73 65 73 20 74 68 65 20 6c 6f decreases the lo
a220: 63 6b 2e 0a 2a 2a 20 54 68 65 20 78 43 68 65 63 ck..** The xChec
a230: 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 29 20 kReservedLock()
a240: 6d 65 74 68 6f 64 20 63 68 65 63 6b 73 20 77 68 method checks wh
a250: 65 74 68 65 72 20 61 6e 79 20 64 61 74 61 62 61 ether any databa
a260: 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 0a 2a se connection,.*
a270: 2a 20 65 69 74 68 65 72 20 69 6e 20 74 68 69 73 * either in this
a280: 20 70 72 6f 63 65 73 73 20 6f 72 20 69 6e 20 73 process or in s
a290: 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 ome other proces
a2a0: 73 2c 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 s, is holding a
a2b0: 52 45 53 45 52 56 45 44 2c 0a 2a 2a 20 50 45 4e RESERVED,.** PEN
a2c0: 44 49 4e 47 2c 20 6f 72 20 45 58 43 4c 55 53 49 DING, or EXCLUSI
a2d0: 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 VE lock on the f
a2e0: 69 6c 65 2e 20 20 49 74 20 72 65 74 75 72 6e 73 ile. It returns
a2f0: 20 74 72 75 65 0a 2a 2a 20 69 66 20 73 75 63 68 true.** if such
a300: 20 61 20 6c 6f 63 6b 20 65 78 69 73 74 73 20 61 a lock exists a
a310: 6e 64 20 66 61 6c 73 65 20 6f 74 68 65 72 77 69 nd false otherwi
a320: 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 46 se..**.** The xF
a330: 69 6c 65 43 6f 6e 74 72 6f 6c 28 29 20 6d 65 74 ileControl() met
a340: 68 6f 64 20 69 73 20 61 20 67 65 6e 65 72 69 63 hod is a generic
a350: 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 interface that
a360: 61 6c 6c 6f 77 73 20 63 75 73 74 6f 6d 0a 2a 2a allows custom.**
a370: 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 VFS implementat
a380: 69 6f 6e 73 20 74 6f 20 64 69 72 65 63 74 6c 79 ions to directly
a390: 20 63 6f 6e 74 72 6f 6c 20 61 6e 20 6f 70 65 6e control an open
a3a0: 20 66 69 6c 65 20 75 73 69 6e 67 20 74 68 65 0a file using the.
a3b0: 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 ** [sqlite3_file
a3c0: 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 69 6e 74 65 _control()] inte
a3d0: 72 66 61 63 65 2e 20 20 54 68 65 20 73 65 63 6f rface. The seco
a3e0: 6e 64 20 22 6f 70 22 20 61 72 67 75 6d 65 6e 74 nd "op" argument
a3f0: 20 69 73 20 61 6e 0a 2a 2a 20 69 6e 74 65 67 65 is an.** intege
a400: 72 20 6f 70 63 6f 64 65 2e 20 20 54 68 65 20 74 r opcode. The t
a410: 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69 73 hird argument is
a420: 20 61 20 67 65 6e 65 72 69 63 20 70 6f 69 6e 74 a generic point
a430: 65 72 20 69 6e 74 65 6e 64 65 64 20 74 6f 0a 2a er intended to.*
a440: 2a 20 70 6f 69 6e 74 20 74 6f 20 61 20 73 74 72 * point to a str
a450: 75 63 74 75 72 65 20 74 68 61 74 20 6d 61 79 20 ucture that may
a460: 63 6f 6e 74 61 69 6e 20 61 72 67 75 6d 65 6e 74 contain argument
a470: 73 20 6f 72 20 73 70 61 63 65 20 69 6e 20 77 68 s or space in wh
a480: 69 63 68 20 74 6f 0a 2a 2a 20 77 72 69 74 65 20 ich to.** write
a490: 72 65 74 75 72 6e 20 76 61 6c 75 65 73 2e 20 20 return values.
a4a0: 50 6f 74 65 6e 74 69 61 6c 20 75 73 65 73 20 66 Potential uses f
a4b0: 6f 72 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 or xFileControl(
a4c0: 29 20 6d 69 67 68 74 20 62 65 0a 2a 2a 20 66 75 ) might be.** fu
a4d0: 6e 63 74 69 6f 6e 73 20 74 6f 20 65 6e 61 62 6c nctions to enabl
a4e0: 65 20 62 6c 6f 63 6b 69 6e 67 20 6c 6f 63 6b 73 e blocking locks
a4f0: 20 77 69 74 68 20 74 69 6d 65 6f 75 74 73 2c 20 with timeouts,
a500: 74 6f 20 63 68 61 6e 67 65 20 74 68 65 0a 2a 2a to change the.**
a510: 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 locking strateg
a520: 79 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 74 y (for example t
a530: 6f 20 75 73 65 20 64 6f 74 2d 66 69 6c 65 20 6c o use dot-file l
a540: 6f 63 6b 73 29 2c 20 74 6f 20 69 6e 71 75 69 72 ocks), to inquir
a550: 65 0a 2a 2a 20 61 62 6f 75 74 20 74 68 65 20 73 e.** about the s
a560: 74 61 74 75 73 20 6f 66 20 61 20 6c 6f 63 6b 2c tatus of a lock,
a570: 20 6f 72 20 74 6f 20 62 72 65 61 6b 20 73 74 61 or to break sta
a580: 6c 65 20 6c 6f 63 6b 73 2e 20 20 54 68 65 20 53 le locks. The S
a590: 51 4c 69 74 65 0a 2a 2a 20 63 6f 72 65 20 72 65 QLite.** core re
a5a0: 73 65 72 76 65 73 20 61 6c 6c 20 6f 70 63 6f 64 serves all opcod
a5b0: 65 73 20 6c 65 73 73 20 74 68 61 6e 20 31 30 30 es less than 100
a5c0: 20 66 6f 72 20 69 74 73 20 6f 77 6e 20 75 73 65 for its own use
a5d0: 2e 0a 2a 2a 20 41 20 5b 53 51 4c 49 54 45 5f 46 ..** A [SQLITE_F
a5e0: 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 20 7c CNTL_LOCKSTATE |
a5f0: 20 6c 69 73 74 20 6f 66 20 6f 70 63 6f 64 65 73 list of opcodes
a600: 5d 20 6c 65 73 73 20 74 68 61 6e 20 31 30 30 20 ] less than 100
a610: 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a is available..**
a620: 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 Applications th
a630: 61 74 20 64 65 66 69 6e 65 20 61 20 63 75 73 74 at define a cust
a640: 6f 6d 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 om xFileControl
a650: 6d 65 74 68 6f 64 20 73 68 6f 75 6c 64 20 75 73 method should us
a660: 65 20 6f 70 63 6f 64 65 73 0a 2a 2a 20 67 72 65 e opcodes.** gre
a670: 61 74 65 72 20 74 68 61 6e 20 31 30 30 20 74 6f ater than 100 to
a680: 20 61 76 6f 69 64 20 63 6f 6e 66 6c 69 63 74 73 avoid conflicts
a690: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 53 65 63 ..**.** The xSec
a6a0: 74 6f 72 53 69 7a 65 28 29 20 6d 65 74 68 6f 64 torSize() method
a6b0: 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 65 63 returns the sec
a6c0: 74 6f 72 20 73 69 7a 65 20 6f 66 20 74 68 65 0a tor size of the.
a6d0: 2a 2a 20 64 65 76 69 63 65 20 74 68 61 74 20 75 ** device that u
a6e0: 6e 64 65 72 6c 69 65 73 20 74 68 65 20 66 69 6c nderlies the fil
a6f0: 65 2e 20 20 54 68 65 20 73 65 63 74 6f 72 20 73 e. The sector s
a700: 69 7a 65 20 69 73 20 74 68 65 0a 2a 2a 20 6d 69 ize is the.** mi
a710: 6e 69 6d 75 6d 20 77 72 69 74 65 20 74 68 61 74 nimum write that
a720: 20 63 61 6e 20 62 65 20 70 65 72 66 6f 72 6d 65 can be performe
a730: 64 20 77 69 74 68 6f 75 74 20 64 69 73 74 75 72 d without distur
a740: 62 69 6e 67 0a 2a 2a 20 6f 74 68 65 72 20 62 79 bing.** other by
a750: 74 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65 2e tes in the file.
a760: 20 20 54 68 65 20 78 44 65 76 69 63 65 43 68 61 The xDeviceCha
a770: 72 61 63 74 65 72 69 73 74 69 63 73 28 29 0a 2a racteristics().*
a780: 2a 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73 * method returns
a790: 20 61 20 62 69 74 20 76 65 63 74 6f 72 20 64 65 a bit vector de
a7a0: 73 63 72 69 62 69 6e 67 20 62 65 68 61 76 69 6f scribing behavio
a7b0: 72 73 20 6f 66 20 74 68 65 0a 2a 2a 20 75 6e 64 rs of the.** und
a7c0: 65 72 6c 79 69 6e 67 20 64 65 76 69 63 65 3a 0a erlying device:.
a7d0: 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c **.** <ul>.** <l
a7e0: 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 i> [SQLITE_IOCAP
a7f0: 5f 41 54 4f 4d 49 43 5d 0a 2a 2a 20 3c 6c 69 3e _ATOMIC].** <li>
a800: 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 [SQLITE_IOCAP_A
a810: 54 4f 4d 49 43 35 31 32 5d 0a 2a 2a 20 3c 6c 69 TOMIC512].** <li
a820: 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f > [SQLITE_IOCAP_
a830: 41 54 4f 4d 49 43 31 4b 5d 0a 2a 2a 20 3c 6c 69 ATOMIC1K].** <li
a840: 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f > [SQLITE_IOCAP_
a850: 41 54 4f 4d 49 43 32 4b 5d 0a 2a 2a 20 3c 6c 69 ATOMIC2K].** <li
a860: 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f > [SQLITE_IOCAP_
a870: 41 54 4f 4d 49 43 34 4b 5d 0a 2a 2a 20 3c 6c 69 ATOMIC4K].** <li
a880: 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f > [SQLITE_IOCAP_
a890: 41 54 4f 4d 49 43 38 4b 5d 0a 2a 2a 20 3c 6c 69 ATOMIC8K].** <li
a8a0: 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f > [SQLITE_IOCAP_
a8b0: 41 54 4f 4d 49 43 31 36 4b 5d 0a 2a 2a 20 3c 6c ATOMIC16K].** <l
a8c0: 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 i> [SQLITE_IOCAP
a8d0: 5f 41 54 4f 4d 49 43 33 32 4b 5d 0a 2a 2a 20 3c _ATOMIC32K].** <
a8e0: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 li> [SQLITE_IOCA
a8f0: 50 5f 41 54 4f 4d 49 43 36 34 4b 5d 0a 2a 2a 20 P_ATOMIC64K].**
a900: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 <li> [SQLITE_IOC
a910: 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 5d 0a AP_SAFE_APPEND].
a920: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f ** <li> [SQLITE_
a930: 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c IOCAP_SEQUENTIAL
a940: 5d 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a ].** </ul>.**.**
a950: 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 The SQLITE_IOCA
a960: 50 5f 41 54 4f 4d 49 43 20 70 72 6f 70 65 72 74 P_ATOMIC propert
a970: 79 20 6d 65 61 6e 73 20 74 68 61 74 20 61 6c 6c y means that all
a980: 20 77 72 69 74 65 73 20 6f 66 0a 2a 2a 20 61 6e writes of.** an
a990: 79 20 73 69 7a 65 20 61 72 65 20 61 74 6f 6d 69 y size are atomi
a9a0: 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49 c. The SQLITE_I
a9b0: 4f 43 41 50 5f 41 54 4f 4d 49 43 6e 6e 6e 20 76 OCAP_ATOMICnnn v
a9c0: 61 6c 75 65 73 0a 2a 2a 20 6d 65 61 6e 20 74 68 alues.** mean th
a9d0: 61 74 20 77 72 69 74 65 73 20 6f 66 20 62 6c 6f at writes of blo
a9e0: 63 6b 73 20 74 68 61 74 20 61 72 65 20 6e 6e 6e cks that are nnn
a9f0: 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 20 61 bytes in size a
aa00: 6e 64 0a 2a 2a 20 61 72 65 20 61 6c 69 67 6e 65 nd.** are aligne
aa10: 64 20 74 6f 20 61 6e 20 61 64 64 72 65 73 73 20 d to an address
aa20: 77 68 69 63 68 20 69 73 20 61 6e 20 69 6e 74 65 which is an inte
aa30: 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66 0a ger multiple of.
aa40: 2a 2a 20 6e 6e 6e 20 61 72 65 20 61 74 6f 6d 69 ** nnn are atomi
aa50: 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49 c. The SQLITE_I
aa60: 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 OCAP_SAFE_APPEND
aa70: 20 76 61 6c 75 65 20 6d 65 61 6e 73 0a 2a 2a 20 value means.**
aa80: 74 68 61 74 20 77 68 65 6e 20 64 61 74 61 20 69 that when data i
aa90: 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 61 20 s appended to a
aaa0: 66 69 6c 65 2c 20 74 68 65 20 64 61 74 61 20 69 file, the data i
aab0: 73 20 61 70 70 65 6e 64 65 64 0a 2a 2a 20 66 69 s appended.** fi
aac0: 72 73 74 20 74 68 65 6e 20 74 68 65 20 73 69 7a rst then the siz
aad0: 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 73 e of the file is
aae0: 20 65 78 74 65 6e 64 65 64 2c 20 6e 65 76 65 72 extended, never
aaf0: 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20 77 61 the other.** wa
ab00: 79 20 61 72 6f 75 6e 64 2e 20 20 54 68 65 20 53 y around. The S
ab10: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 QLITE_IOCAP_SEQU
ab20: 45 4e 54 49 41 4c 20 70 72 6f 70 65 72 74 79 20 ENTIAL property
ab30: 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 69 6e means that.** in
ab40: 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 77 72 69 formation is wri
ab50: 74 74 65 6e 20 74 6f 20 64 69 73 6b 20 69 6e 20 tten to disk in
ab60: 74 68 65 20 73 61 6d 65 20 6f 72 64 65 72 20 61 the same order a
ab70: 73 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 78 57 s calls.** to xW
ab80: 72 69 74 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 rite()..**.** If
ab90: 20 78 52 65 61 64 28 29 20 72 65 74 75 72 6e 73 xRead() returns
aba0: 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 SQLITE_IOERR_SH
abb0: 4f 52 54 5f 52 45 41 44 20 69 74 20 6d 75 73 74 ORT_READ it must
abc0: 20 61 6c 73 6f 20 66 69 6c 6c 0a 2a 2a 20 69 6e also fill.** in
abd0: 20 74 68 65 20 75 6e 72 65 61 64 20 70 6f 72 74 the unread port
abe0: 69 6f 6e 73 20 6f 66 20 74 68 65 20 62 75 66 66 ions of the buff
abf0: 65 72 20 77 69 74 68 20 7a 65 72 6f 73 2e 20 20 er with zeros.
ac00: 41 20 56 46 53 20 74 68 61 74 0a 2a 2a 20 66 61 A VFS that.** fa
ac10: 69 6c 73 20 74 6f 20 7a 65 72 6f 2d 66 69 6c 6c ils to zero-fill
ac20: 20 73 68 6f 72 74 20 72 65 61 64 73 20 6d 69 67 short reads mig
ac30: 68 74 20 73 65 65 6d 20 74 6f 20 77 6f 72 6b 2e ht seem to work.
ac40: 20 20 48 6f 77 65 76 65 72 2c 0a 2a 2a 20 66 61 However,.** fa
ac50: 69 6c 75 72 65 20 74 6f 20 7a 65 72 6f 2d 66 69 ilure to zero-fi
ac60: 6c 6c 20 73 68 6f 72 74 20 72 65 61 64 73 20 77 ll short reads w
ac70: 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79 20 6c ill eventually l
ac80: 65 61 64 20 74 6f 0a 2a 2a 20 64 61 74 61 62 61 ead to.** databa
ac90: 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a se corruption..*
aca0: 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
acb0: 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
acc0: 6f 64 73 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d ods sqlite3_io_m
acd0: 65 74 68 6f 64 73 3b 0a 73 74 72 75 63 74 20 73 ethods;.struct s
ace0: 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
acf0: 73 20 7b 0a 20 20 69 6e 74 20 69 56 65 72 73 69 s {. int iVersi
ad00: 6f 6e 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6c 6f on;. int (*xClo
ad10: 73 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 se)(sqlite3_file
ad20: 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52 65 61 *);. int (*xRea
ad30: 64 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a d)(sqlite3_file*
ad40: 2c 20 76 6f 69 64 2a 2c 20 69 6e 74 20 69 41 6d , void*, int iAm
ad50: 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 t, sqlite3_int64
ad60: 20 69 4f 66 73 74 29 3b 0a 20 20 69 6e 74 20 28 iOfst);. int (
ad70: 2a 78 57 72 69 74 65 29 28 73 71 6c 69 74 65 33 *xWrite)(sqlite3
ad80: 5f 66 69 6c 65 2a 2c 20 63 6f 6e 73 74 20 76 6f _file*, const vo
ad90: 69 64 2a 2c 20 69 6e 74 20 69 41 6d 74 2c 20 73 id*, int iAmt, s
ada0: 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4f 66 qlite3_int64 iOf
adb0: 73 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 54 72 st);. int (*xTr
adc0: 75 6e 63 61 74 65 29 28 73 71 6c 69 74 65 33 5f uncate)(sqlite3_
add0: 66 69 6c 65 2a 2c 20 73 71 6c 69 74 65 33 5f 69 file*, sqlite3_i
ade0: 6e 74 36 34 20 73 69 7a 65 29 3b 0a 20 20 69 6e nt64 size);. in
adf0: 74 20 28 2a 78 53 79 6e 63 29 28 73 71 6c 69 74 t (*xSync)(sqlit
ae00: 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 20 66 6c e3_file*, int fl
ae10: 61 67 73 29 3b 0a 20 20 69 6e 74 20 28 2a 78 46 ags);. int (*xF
ae20: 69 6c 65 53 69 7a 65 29 28 73 71 6c 69 74 65 33 ileSize)(sqlite3
ae30: 5f 66 69 6c 65 2a 2c 20 73 71 6c 69 74 65 33 5f _file*, sqlite3_
ae40: 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 3b 0a 20 int64 *pSize);.
ae50: 20 69 6e 74 20 28 2a 78 4c 6f 63 6b 29 28 73 71 int (*xLock)(sq
ae60: 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 lite3_file*, int
ae70: 29 3b 0a 20 20 69 6e 74 20 28 2a 78 55 6e 6c 6f );. int (*xUnlo
ae80: 63 6b 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ck)(sqlite3_file
ae90: 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 *, int);. int (
aea0: 2a 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c *xCheckReservedL
aeb0: 6f 63 6b 29 28 73 71 6c 69 74 65 33 5f 66 69 6c ock)(sqlite3_fil
aec0: 65 2a 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 e*, int *pResOut
aed0: 29 3b 0a 20 20 69 6e 74 20 28 2a 78 46 69 6c 65 );. int (*xFile
aee0: 43 6f 6e 74 72 6f 6c 29 28 73 71 6c 69 74 65 33 Control)(sqlite3
aef0: 5f 66 69 6c 65 2a 2c 20 69 6e 74 20 6f 70 2c 20 _file*, int op,
af00: 76 6f 69 64 20 2a 70 41 72 67 29 3b 0a 20 20 69 void *pArg);. i
af10: 6e 74 20 28 2a 78 53 65 63 74 6f 72 53 69 7a 65 nt (*xSectorSize
af20: 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 )(sqlite3_file*)
af30: 3b 0a 20 20 69 6e 74 20 28 2a 78 44 65 76 69 63 ;. int (*xDevic
af40: 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 eCharacteristics
af50: 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 )(sqlite3_file*)
af60: 3b 0a 20 20 2f 2a 20 41 64 64 69 74 69 6f 6e 61 ;. /* Additiona
af70: 6c 20 6d 65 74 68 6f 64 73 20 6d 61 79 20 62 65 l methods may be
af80: 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 65 added in future
af90: 20 72 65 6c 65 61 73 65 73 20 2a 2f 0a 7d 3b 0a releases */.};.
afa0: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
afb0: 20 53 74 61 6e 64 61 72 64 20 46 69 6c 65 20 43 Standard File C
afc0: 6f 6e 74 72 6f 6c 20 4f 70 63 6f 64 65 73 20 7b ontrol Opcodes {
afd0: 48 31 31 33 31 30 7d 20 3c 53 33 30 38 30 30 3e H11310} <S30800>
afe0: 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 69 6e 74 .**.** These int
aff0: 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 20 61 eger constants a
b000: 72 65 20 6f 70 63 6f 64 65 73 20 66 6f 72 20 74 re opcodes for t
b010: 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 he xFileControl
b020: 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 65 method.** of the
b030: 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 [sqlite3_io_met
b040: 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20 61 6e 64 hods] object and
b050: 20 66 6f 72 20 74 68 65 20 5b 73 71 6c 69 74 65 for the [sqlite
b060: 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 3_file_control()
b070: 5d 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 2e 0a ].** interface..
b080: 2a 2a 0a 2a 2a 20 54 68 65 20 5b 53 51 4c 49 54 **.** The [SQLIT
b090: 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 E_FCNTL_LOCKSTAT
b0a0: 45 5d 20 6f 70 63 6f 64 65 20 69 73 20 75 73 65 E] opcode is use
b0b0: 64 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67 2e d for debugging.
b0c0: 20 20 54 68 69 73 0a 2a 2a 20 6f 70 63 6f 64 65 This.** opcode
b0d0: 20 63 61 75 73 65 73 20 74 68 65 20 78 46 69 6c causes the xFil
b0e0: 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20 eControl method
b0f0: 74 6f 20 77 72 69 74 65 20 74 68 65 20 63 75 72 to write the cur
b100: 72 65 6e 74 20 73 74 61 74 65 20 6f 66 0a 2a 2a rent state of.**
b110: 20 74 68 65 20 6c 6f 63 6b 20 28 6f 6e 65 20 6f the lock (one o
b120: 66 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 4e f [SQLITE_LOCK_N
b130: 4f 4e 45 5d 2c 20 5b 53 51 4c 49 54 45 5f 4c 4f ONE], [SQLITE_LO
b140: 43 4b 5f 53 48 41 52 45 44 5d 2c 0a 2a 2a 20 5b CK_SHARED],.** [
b150: 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 52 45 53 45 SQLITE_LOCK_RESE
b160: 52 56 45 44 5d 2c 20 5b 53 51 4c 49 54 45 5f 4c RVED], [SQLITE_L
b170: 4f 43 4b 5f 50 45 4e 44 49 4e 47 5d 2c 20 6f 72 OCK_PENDING], or
b180: 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 45 58 [SQLITE_LOCK_EX
b190: 43 4c 55 53 49 56 45 5d 29 0a 2a 2a 20 69 6e 74 CLUSIVE]).** int
b1a0: 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68 61 o an integer tha
b1b0: 74 20 74 68 65 20 70 41 72 67 20 61 72 67 75 6d t the pArg argum
b1c0: 65 6e 74 20 70 6f 69 6e 74 73 20 74 6f 2e 20 54 ent points to. T
b1d0: 68 69 73 20 63 61 70 61 62 69 6c 69 74 79 0a 2a his capability.*
b1e0: 2a 20 69 73 20 75 73 65 64 20 64 75 72 69 6e 67 * is used during
b1f0: 20 74 65 73 74 69 6e 67 20 61 6e 64 20 6f 6e 6c testing and onl
b200: 79 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73 75 y needs to be su
b210: 70 70 6f 72 74 65 64 20 77 68 65 6e 20 53 51 4c pported when SQL
b220: 49 54 45 5f 54 45 53 54 0a 2a 2a 20 69 73 20 64 ITE_TEST.** is d
b230: 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 efined..*/.#defi
b240: 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f ne SQLITE_FCNTL_
b250: 4c 4f 43 4b 53 54 41 54 45 20 20 20 20 20 20 20 LOCKSTATE
b260: 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 1.#define SQLIT
b270: 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 E_GET_LOCKPROXYF
b280: 49 4c 45 20 20 20 20 20 20 32 0a 23 64 65 66 69 ILE 2.#defi
b290: 6e 65 20 53 51 4c 49 54 45 5f 53 45 54 5f 4c 4f ne SQLITE_SET_LO
b2a0: 43 4b 50 52 4f 58 59 46 49 4c 45 20 20 20 20 20 CKPROXYFILE
b2b0: 20 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 3.#define SQLIT
b2c0: 45 5f 4c 41 53 54 5f 45 52 52 4e 4f 20 20 20 20 E_LAST_ERRNO
b2d0: 20 20 20 20 20 20 20 20 20 34 0a 0a 2f 2a 0a 2a 4../*.*
b2e0: 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65 * CAPI3REF: Mute
b2f0: 78 20 48 61 6e 64 6c 65 20 7b 48 31 37 31 31 30 x Handle {H17110
b300: 7d 20 3c 53 32 30 31 33 30 3e 0a 2a 2a 0a 2a 2a } <S20130>.**.**
b310: 20 54 68 65 20 6d 75 74 65 78 20 6d 6f 64 75 6c The mutex modul
b320: 65 20 77 69 74 68 69 6e 20 53 51 4c 69 74 65 20 e within SQLite
b330: 64 65 66 69 6e 65 73 20 5b 73 71 6c 69 74 65 33 defines [sqlite3
b340: 5f 6d 75 74 65 78 5d 20 74 6f 20 62 65 20 61 6e _mutex] to be an
b350: 0a 2a 2a 20 61 62 73 74 72 61 63 74 20 74 79 70 .** abstract typ
b360: 65 20 66 6f 72 20 61 20 6d 75 74 65 78 20 6f 62 e for a mutex ob
b370: 6a 65 63 74 2e 20 20 54 68 65 20 53 51 4c 69 74 ject. The SQLit
b380: 65 20 63 6f 72 65 20 6e 65 76 65 72 20 6c 6f 6f e core never loo
b390: 6b 73 0a 2a 2a 20 61 74 20 74 68 65 20 69 6e 74 ks.** at the int
b3a0: 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61 ernal representa
b3b0: 74 69 6f 6e 20 6f 66 20 61 6e 20 5b 73 71 6c 69 tion of an [sqli
b3c0: 74 65 33 5f 6d 75 74 65 78 5d 2e 20 20 49 74 20 te3_mutex]. It
b3d0: 6f 6e 6c 79 0a 2a 2a 20 64 65 61 6c 73 20 77 69 only.** deals wi
b3e0: 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 th pointers to t
b3f0: 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 he [sqlite3_mute
b400: 78 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a x] object..**.**
b410: 20 4d 75 74 65 78 65 73 20 61 72 65 20 63 72 65 Mutexes are cre
b420: 61 74 65 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 ated using [sqli
b430: 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 te3_mutex_alloc(
b440: 29 5d 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 )]..*/.typedef s
b450: 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 truct sqlite3_mu
b460: 74 65 78 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 tex sqlite3_mute
b470: 78 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 x;../*.** CAPI3R
b480: 45 46 3a 20 4f 53 20 49 6e 74 65 72 66 61 63 65 EF: OS Interface
b490: 20 4f 62 6a 65 63 74 20 7b 48 31 31 31 34 30 7d Object {H11140}
b4a0: 20 3c 53 32 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S20100>.**.**
b4b0: 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 An instance of t
b4c0: 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6f he sqlite3_vfs o
b4d0: 62 6a 65 63 74 20 64 65 66 69 6e 65 73 20 74 68 bject defines th
b4e0: 65 20 69 6e 74 65 72 66 61 63 65 20 62 65 74 77 e interface betw
b4f0: 65 65 6e 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 een.** the SQLit
b500: 65 20 63 6f 72 65 20 61 6e 64 20 74 68 65 20 75 e core and the u
b510: 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 nderlying operat
b520: 69 6e 67 20 73 79 73 74 65 6d 2e 20 20 54 68 65 ing system. The
b530: 20 22 76 66 73 22 0a 2a 2a 20 69 6e 20 74 68 65 "vfs".** in the
b540: 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 6f 62 6a name of the obj
b550: 65 63 74 20 73 74 61 6e 64 73 20 66 6f 72 20 22 ect stands for "
b560: 76 69 72 74 75 61 6c 20 66 69 6c 65 20 73 79 73 virtual file sys
b570: 74 65 6d 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 tem"..**.** The
b580: 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 56 65 value of the iVe
b590: 72 73 69 6f 6e 20 66 69 65 6c 64 20 69 73 20 69 rsion field is i
b5a0: 6e 69 74 69 61 6c 6c 79 20 31 20 62 75 74 20 6d nitially 1 but m
b5b0: 61 79 20 62 65 20 6c 61 72 67 65 72 20 69 6e 0a ay be larger in.
b5c0: 2a 2a 20 66 75 74 75 72 65 20 76 65 72 73 69 6f ** future versio
b5d0: 6e 73 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 41 ns of SQLite. A
b5e0: 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73 dditional fields
b5f0: 20 6d 61 79 20 62 65 20 61 70 70 65 6e 64 65 64 may be appended
b600: 20 74 6f 20 74 68 69 73 0a 2a 2a 20 6f 62 6a 65 to this.** obje
b610: 63 74 20 77 68 65 6e 20 74 68 65 20 69 56 65 72 ct when the iVer
b620: 73 69 6f 6e 20 76 61 6c 75 65 20 69 73 20 69 6e sion value is in
b630: 63 72 65 61 73 65 64 2e 20 20 4e 6f 74 65 20 74 creased. Note t
b640: 68 61 74 20 74 68 65 20 73 74 72 75 63 74 75 72 hat the structur
b650: 65 0a 2a 2a 20 6f 66 20 74 68 65 20 73 71 6c 69 e.** of the sqli
b660: 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 20 63 te3_vfs object c
b670: 68 61 6e 67 65 73 20 69 6e 20 74 68 65 20 74 72 hanges in the tr
b680: 61 6e 73 61 63 74 69 6f 6e 20 62 65 74 77 65 65 ansaction betwee
b690: 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 76 65 72 73 n.** SQLite vers
b6a0: 69 6f 6e 20 33 2e 35 2e 39 20 61 6e 64 20 33 2e ion 3.5.9 and 3.
b6b0: 36 2e 30 20 61 6e 64 20 79 65 74 20 74 68 65 20 6.0 and yet the
b6c0: 69 56 65 72 73 69 6f 6e 20 66 69 65 6c 64 20 77 iVersion field w
b6d0: 61 73 20 6e 6f 74 0a 2a 2a 20 6d 6f 64 69 66 69 as not.** modifi
b6e0: 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 7a ed..**.** The sz
b6f0: 4f 73 46 69 6c 65 20 66 69 65 6c 64 20 69 73 20 OsFile field is
b700: 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
b710: 73 75 62 63 6c 61 73 73 65 64 20 5b 73 71 6c 69 subclassed [sqli
b720: 74 65 33 5f 66 69 6c 65 5d 0a 2a 2a 20 73 74 72 te3_file].** str
b730: 75 63 74 75 72 65 20 75 73 65 64 20 62 79 20 74 ucture used by t
b740: 68 69 73 20 56 46 53 2e 20 20 6d 78 50 61 74 68 his VFS. mxPath
b750: 6e 61 6d 65 20 69 73 20 74 68 65 20 6d 61 78 69 name is the maxi
b760: 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 0a 2a 2a mum length of.**
b770: 20 61 20 70 61 74 68 6e 61 6d 65 20 69 6e 20 74 a pathname in t
b780: 68 69 73 20 56 46 53 2e 0a 2a 2a 0a 2a 2a 20 52 his VFS..**.** R
b790: 65 67 69 73 74 65 72 65 64 20 73 71 6c 69 74 65 egistered sqlite
b7a0: 33 5f 76 66 73 20 6f 62 6a 65 63 74 73 20 61 72 3_vfs objects ar
b7b0: 65 20 6b 65 70 74 20 6f 6e 20 61 20 6c 69 6e 6b e kept on a link
b7c0: 65 64 20 6c 69 73 74 20 66 6f 72 6d 65 64 20 62 ed list formed b
b7d0: 79 0a 2a 2a 20 74 68 65 20 70 4e 65 78 74 20 70 y.** the pNext p
b7e0: 6f 69 6e 74 65 72 2e 20 20 54 68 65 20 5b 73 71 ointer. The [sq
b7f0: 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74 lite3_vfs_regist
b800: 65 72 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71 er()].** and [sq
b810: 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 lite3_vfs_unregi
b820: 73 74 65 72 28 29 5d 20 69 6e 74 65 72 66 61 63 ster()] interfac
b830: 65 73 20 6d 61 6e 61 67 65 20 74 68 69 73 20 6c es manage this l
b840: 69 73 74 0a 2a 2a 20 69 6e 20 61 20 74 68 72 65 ist.** in a thre
b850: 61 64 2d 73 61 66 65 20 77 61 79 2e 20 20 54 68 ad-safe way. Th
b860: 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 e [sqlite3_vfs_f
b870: 69 6e 64 28 29 5d 20 69 6e 74 65 72 66 61 63 65 ind()] interface
b880: 0a 2a 2a 20 73 65 61 72 63 68 65 73 20 74 68 65 .** searches the
b890: 20 6c 69 73 74 2e 20 20 4e 65 69 74 68 65 72 20 list. Neither
b8a0: 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 the application
b8b0: 63 6f 64 65 20 6e 6f 72 20 74 68 65 20 56 46 53 code nor the VFS
b8c0: 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 .** implementati
b8d0: 6f 6e 20 73 68 6f 75 6c 64 20 75 73 65 20 74 68 on should use th
b8e0: 65 20 70 4e 65 78 74 20 70 6f 69 6e 74 65 72 2e e pNext pointer.
b8f0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 4e 65 78 74 .**.** The pNext
b900: 20 66 69 65 6c 64 20 69 73 20 74 68 65 20 6f 6e field is the on
b910: 6c 79 20 66 69 65 6c 64 20 69 6e 20 74 68 65 20 ly field in the
b920: 73 71 6c 69 74 65 33 5f 76 66 73 0a 2a 2a 20 73 sqlite3_vfs.** s
b930: 74 72 75 63 74 75 72 65 20 74 68 61 74 20 53 51 tructure that SQ
b940: 4c 69 74 65 20 77 69 6c 6c 20 65 76 65 72 20 6d Lite will ever m
b950: 6f 64 69 66 79 2e 20 20 53 51 4c 69 74 65 20 77 odify. SQLite w
b960: 69 6c 6c 20 6f 6e 6c 79 20 61 63 63 65 73 73 0a ill only access.
b970: 2a 2a 20 6f 72 20 6d 6f 64 69 66 79 20 74 68 69 ** or modify thi
b980: 73 20 66 69 65 6c 64 20 77 68 69 6c 65 20 68 6f s field while ho
b990: 6c 64 69 6e 67 20 61 20 70 61 72 74 69 63 75 6c lding a particul
b9a0: 61 72 20 73 74 61 74 69 63 20 6d 75 74 65 78 2e ar static mutex.
b9b0: 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74 .** The applicat
b9c0: 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 ion should never
b9d0: 20 6d 6f 64 69 66 79 20 61 6e 79 74 68 69 6e 67 modify anything
b9e0: 20 77 69 74 68 69 6e 20 74 68 65 20 73 71 6c 69 within the sqli
b9f0: 74 65 33 5f 76 66 73 0a 2a 2a 20 6f 62 6a 65 63 te3_vfs.** objec
ba00: 74 20 6f 6e 63 65 20 74 68 65 20 6f 62 6a 65 63 t once the objec
ba10: 74 20 68 61 73 20 62 65 65 6e 20 72 65 67 69 73 t has been regis
ba20: 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 tered..**.** The
ba30: 20 7a 4e 61 6d 65 20 66 69 65 6c 64 20 68 6f 6c zName field hol
ba40: 64 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 ds the name of t
ba50: 68 65 20 56 46 53 20 6d 6f 64 75 6c 65 2e 20 20 he VFS module.
ba60: 54 68 65 20 6e 61 6d 65 20 6d 75 73 74 0a 2a 2a The name must.**
ba70: 20 62 65 20 75 6e 69 71 75 65 20 61 63 72 6f 73 be unique acros
ba80: 73 20 61 6c 6c 20 56 46 53 20 6d 6f 64 75 6c 65 s all VFS module
ba90: 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 s..**.** SQLite
baa0: 77 69 6c 6c 20 67 75 61 72 61 6e 74 65 65 20 74 will guarantee t
bab0: 68 61 74 20 74 68 65 20 7a 46 69 6c 65 6e 61 6d hat the zFilenam
bac0: 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 78 e parameter to x
bad0: 4f 70 65 6e 0a 2a 2a 20 69 73 20 65 69 74 68 65 Open.** is eithe
bae0: 72 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 r a NULL pointer
baf0: 20 6f 72 20 73 74 72 69 6e 67 20 6f 62 74 61 69 or string obtai
bb00: 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 78 46 75 6c ned.** from xFul
bb10: 6c 50 61 74 68 6e 61 6d 65 28 29 2e 20 20 53 51 lPathname(). SQ
bb20: 4c 69 74 65 20 66 75 72 74 68 65 72 20 67 75 61 Lite further gua
bb30: 72 61 6e 74 65 65 73 20 74 68 61 74 0a 2a 2a 20 rantees that.**
bb40: 74 68 65 20 73 74 72 69 6e 67 20 77 69 6c 6c 20 the string will
bb50: 62 65 20 76 61 6c 69 64 20 61 6e 64 20 75 6e 63 be valid and unc
bb60: 68 61 6e 67 65 64 20 75 6e 74 69 6c 20 78 43 6c hanged until xCl
bb70: 6f 73 65 28 29 20 69 73 0a 2a 2a 20 63 61 6c 6c ose() is.** call
bb80: 65 64 2e 20 42 65 63 61 75 73 65 20 6f 66 20 74 ed. Because of t
bb90: 68 65 20 70 72 65 76 69 6f 75 73 20 73 65 6e 74 he previous sent
bba0: 65 6e 63 65 2c 0a 2a 2a 20 74 68 65 20 5b 73 71 ence,.** the [sq
bbb0: 6c 69 74 65 33 5f 66 69 6c 65 5d 20 63 61 6e 20 lite3_file] can
bbc0: 73 61 66 65 6c 79 20 73 74 6f 72 65 20 61 20 70 safely store a p
bbd0: 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 2a 2a ointer to the.**
bbe0: 20 66 69 6c 65 6e 61 6d 65 20 69 66 20 69 74 20 filename if it
bbf0: 6e 65 65 64 73 20 74 6f 20 72 65 6d 65 6d 62 65 needs to remembe
bc00: 72 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 66 r the filename f
bc10: 6f 72 20 73 6f 6d 65 20 72 65 61 73 6f 6e 2e 0a or some reason..
bc20: 2a 2a 20 49 66 20 74 68 65 20 7a 46 69 6c 65 6e ** If the zFilen
bc30: 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20 69 73 ame parameter is
bc40: 20 78 4f 70 65 6e 20 69 73 20 61 20 4e 55 4c 4c xOpen is a NULL
bc50: 20 70 6f 69 6e 74 65 72 20 74 68 65 6e 20 78 4f pointer then xO
bc60: 70 65 6e 0a 2a 2a 20 6d 75 73 74 20 69 6e 76 65 pen.** must inve
bc70: 6e 74 20 69 74 73 20 6f 77 6e 20 74 65 6d 70 6f nt its own tempo
bc80: 72 61 72 79 20 6e 61 6d 65 20 66 6f 72 20 74 68 rary name for th
bc90: 65 20 66 69 6c 65 2e 20 20 57 68 65 6e 65 76 65 e file. Wheneve
bca0: 72 20 74 68 65 20 0a 2a 2a 20 78 46 69 6c 65 6e r the .** xFilen
bcb0: 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20 69 73 ame parameter is
bcc0: 20 4e 55 4c 4c 20 69 74 20 77 69 6c 6c 20 61 6c NULL it will al
bcd0: 73 6f 20 62 65 20 74 68 65 20 63 61 73 65 20 74 so be the case t
bce0: 68 61 74 20 74 68 65 0a 2a 2a 20 66 6c 61 67 73 hat the.** flags
bcf0: 20 70 61 72 61 6d 65 74 65 72 20 77 69 6c 6c 20 parameter will
bd00: 69 6e 63 6c 75 64 65 20 5b 53 51 4c 49 54 45 5f include [SQLITE_
bd10: 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f OPEN_DELETEONCLO
bd20: 53 45 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 SE]..**.** The f
bd30: 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20 74 6f lags argument to
bd40: 20 78 4f 70 65 6e 28 29 20 69 6e 63 6c 75 64 65 xOpen() include
bd50: 73 20 61 6c 6c 20 62 69 74 73 20 73 65 74 20 69 s all bits set i
bd60: 6e 0a 2a 2a 20 74 68 65 20 66 6c 61 67 73 20 61 n.** the flags a
bd70: 72 67 75 6d 65 6e 74 20 74 6f 20 5b 73 71 6c 69 rgument to [sqli
bd80: 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 2e 20 te3_open_v2()].
bd90: 20 4f 72 20 69 66 20 5b 73 71 6c 69 74 65 33 5f Or if [sqlite3_
bda0: 6f 70 65 6e 28 29 5d 0a 2a 2a 20 6f 72 20 5b 73 open()].** or [s
bdb0: 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d qlite3_open16()]
bdc0: 20 69 73 20 75 73 65 64 2c 20 74 68 65 6e 20 66 is used, then f
bdd0: 6c 61 67 73 20 69 6e 63 6c 75 64 65 73 20 61 74 lags includes at
bde0: 20 6c 65 61 73 74 0a 2a 2a 20 5b 53 51 4c 49 54 least.** [SQLIT
bdf0: 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 E_OPEN_READWRITE
be00: 5d 20 7c 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e ] | [SQLITE_OPEN
be10: 5f 43 52 45 41 54 45 5d 2e 20 0a 2a 2a 20 49 66 _CREATE]. .** If
be20: 20 78 4f 70 65 6e 28 29 20 6f 70 65 6e 73 20 61 xOpen() opens a
be30: 20 66 69 6c 65 20 72 65 61 64 2d 6f 6e 6c 79 20 file read-only
be40: 74 68 65 6e 20 69 74 20 73 65 74 73 20 2a 70 4f then it sets *pO
be50: 75 74 46 6c 61 67 73 20 74 6f 0a 2a 2a 20 69 6e utFlags to.** in
be60: 63 6c 75 64 65 20 5b 53 51 4c 49 54 45 5f 4f 50 clude [SQLITE_OP
be70: 45 4e 5f 52 45 41 44 4f 4e 4c 59 5d 2e 20 20 4f EN_READONLY]. O
be80: 74 68 65 72 20 62 69 74 73 20 69 6e 20 2a 70 4f ther bits in *pO
be90: 75 74 46 6c 61 67 73 20 6d 61 79 20 62 65 20 73 utFlags may be s
bea0: 65 74 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 et..**.** SQLite
beb0: 20 77 69 6c 6c 20 61 6c 73 6f 20 61 64 64 20 6f will also add o
bec0: 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ne of the follow
bed0: 69 6e 67 20 66 6c 61 67 73 20 74 6f 20 74 68 65 ing flags to the
bee0: 20 78 4f 70 65 6e 28 29 0a 2a 2a 20 63 61 6c 6c xOpen().** call
bef0: 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 , depending on t
bf00: 68 65 20 6f 62 6a 65 63 74 20 62 65 69 6e 67 20 he object being
bf10: 6f 70 65 6e 65 64 3a 0a 2a 2a 0a 2a 2a 20 3c 75 opened:.**.** <u
bf20: 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c l>.** <li> [SQL
bf30: 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 ITE_OPEN_MAIN_DB
bf40: 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 ].** <li> [SQLI
bf50: 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 TE_OPEN_MAIN_JOU
bf60: 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b RNAL].** <li> [
bf70: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 SQLITE_OPEN_TEMP
bf80: 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 _DB].** <li> [S
bf90: 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f QLITE_OPEN_TEMP_
bfa0: 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69 3e JOURNAL].** <li>
bfb0: 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 [SQLITE_OPEN_T
bfc0: 52 41 4e 53 49 45 4e 54 5f 44 42 5d 0a 2a 2a 20 RANSIENT_DB].**
bfd0: 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 <li> [SQLITE_OP
bfe0: 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 5d 0a 2a EN_SUBJOURNAL].*
bff0: 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f * <li> [SQLITE_
c000: 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 OPEN_MASTER_JOUR
c010: 4e 41 4c 5d 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a NAL].** </ul>.**
c020: 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 49 2f 4f .** The file I/O
c030: 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
c040: 63 61 6e 20 75 73 65 20 74 68 65 20 6f 62 6a 65 can use the obje
c050: 63 74 20 74 79 70 65 20 66 6c 61 67 73 20 74 6f ct type flags to
c060: 0a 2a 2a 20 63 68 61 6e 67 65 20 74 68 65 20 77 .** change the w
c070: 61 79 20 69 74 20 64 65 61 6c 73 20 77 69 74 68 ay it deals with
c080: 20 66 69 6c 65 73 2e 20 20 46 6f 72 20 65 78 61 files. For exa
c090: 6d 70 6c 65 2c 20 61 6e 20 61 70 70 6c 69 63 61 mple, an applica
c0a0: 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 20 64 6f 65 tion.** that doe
c0b0: 73 20 6e 6f 74 20 63 61 72 65 20 61 62 6f 75 74 s not care about
c0c0: 20 63 72 61 73 68 20 72 65 63 6f 76 65 72 79 20 crash recovery
c0d0: 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 6d 69 67 68 or rollback migh
c0e0: 74 20 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6f 70 t make.** the op
c0f0: 65 6e 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 en of a journal
c100: 66 69 6c 65 20 61 20 6e 6f 2d 6f 70 2e 20 20 57 file a no-op. W
c110: 72 69 74 65 73 20 74 6f 20 74 68 69 73 20 6a 6f rites to this jo
c120: 75 72 6e 61 6c 20 77 6f 75 6c 64 0a 2a 2a 20 61 urnal would.** a
c130: 6c 73 6f 20 62 65 20 6e 6f 2d 6f 70 73 2c 20 61 lso be no-ops, a
c140: 6e 64 20 61 6e 79 20 61 74 74 65 6d 70 74 20 74 nd any attempt t
c150: 6f 20 72 65 61 64 20 74 68 65 20 6a 6f 75 72 6e o read the journ
c160: 61 6c 20 77 6f 75 6c 64 20 72 65 74 75 72 6e 0a al would return.
c170: 2a 2a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 2e ** SQLITE_IOERR.
c180: 20 20 4f 72 20 74 68 65 20 69 6d 70 6c 65 6d 65 Or the impleme
c190: 6e 74 61 74 69 6f 6e 20 6d 69 67 68 74 20 72 65 ntation might re
c1a0: 63 6f 67 6e 69 7a 65 20 74 68 61 74 20 61 20 64 cognize that a d
c1b0: 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 atabase.** file
c1c0: 77 69 6c 6c 20 62 65 20 64 6f 69 6e 67 20 70 61 will be doing pa
c1d0: 67 65 2d 61 6c 69 67 6e 65 64 20 73 65 63 74 6f ge-aligned secto
c1e0: 72 20 72 65 61 64 73 20 61 6e 64 20 77 72 69 74 r reads and writ
c1f0: 65 73 20 69 6e 20 61 20 72 61 6e 64 6f 6d 0a 2a es in a random.*
c200: 2a 20 6f 72 64 65 72 20 61 6e 64 20 73 65 74 20 * order and set
c210: 75 70 20 69 74 73 20 49 2f 4f 20 73 75 62 73 79 up its I/O subsy
c220: 73 74 65 6d 20 61 63 63 6f 72 64 69 6e 67 6c 79 stem accordingly
c230: 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 6d ..**.** SQLite m
c240: 69 67 68 74 20 61 6c 73 6f 20 61 64 64 20 6f 6e ight also add on
c250: 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
c260: 6e 67 20 66 6c 61 67 73 20 74 6f 20 74 68 65 20 ng flags to the
c270: 78 4f 70 65 6e 20 6d 65 74 68 6f 64 3a 0a 2a 2a xOpen method:.**
c280: 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e .** <ul>.** <li>
c290: 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 [SQLITE_OPEN_DE
c2a0: 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 0a 2a 2a 20 LETEONCLOSE].**
c2b0: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4f 50 45 <li> [SQLITE_OPE
c2c0: 4e 5f 45 58 43 4c 55 53 49 56 45 5d 0a 2a 2a 20 N_EXCLUSIVE].**
c2d0: 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 </ul>.**.** The
c2e0: 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c [SQLITE_OPEN_DEL
c2f0: 45 54 45 4f 4e 43 4c 4f 53 45 5d 20 66 6c 61 67 ETEONCLOSE] flag
c300: 20 6d 65 61 6e 73 20 74 68 65 20 66 69 6c 65 20 means the file
c310: 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 64 65 6c should be.** del
c320: 65 74 65 64 20 77 68 65 6e 20 69 74 20 69 73 20 eted when it is
c330: 63 6c 6f 73 65 64 2e 20 20 54 68 65 20 5b 53 51 closed. The [SQ
c340: 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 LITE_OPEN_DELETE
c350: 4f 4e 43 4c 4f 53 45 5d 0a 2a 2a 20 77 69 6c 6c ONCLOSE].** will
c360: 20 62 65 20 73 65 74 20 66 6f 72 20 54 45 4d 50 be set for TEMP
c370: 20 20 64 61 74 61 62 61 73 65 73 2c 20 6a 6f 75 databases, jou
c380: 72 6e 61 6c 73 20 61 6e 64 20 66 6f 72 20 73 75 rnals and for su
c390: 62 6a 6f 75 72 6e 61 6c 73 2e 0a 2a 2a 0a 2a 2a bjournals..**.**
c3a0: 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 The [SQLITE_OPE
c3b0: 4e 5f 45 58 43 4c 55 53 49 56 45 5d 20 66 6c 61 N_EXCLUSIVE] fla
c3c0: 67 20 69 73 20 61 6c 77 61 79 73 20 75 73 65 64 g is always used
c3d0: 20 69 6e 20 63 6f 6e 6a 75 6e 63 74 69 6f 6e 0a in conjunction.
c3e0: 2a 2a 20 77 69 74 68 20 74 68 65 20 5b 53 51 4c ** with the [SQL
c3f0: 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 5d ITE_OPEN_CREATE]
c400: 20 66 6c 61 67 2c 20 77 68 69 63 68 20 61 72 65 flag, which are
c410: 20 62 6f 74 68 20 64 69 72 65 63 74 6c 79 0a 2a both directly.*
c420: 2a 20 61 6e 61 6c 6f 67 6f 75 73 20 74 6f 20 74 * analogous to t
c430: 68 65 20 4f 5f 45 58 43 4c 20 61 6e 64 20 4f 5f he O_EXCL and O_
c440: 43 52 45 41 54 20 66 6c 61 67 73 20 6f 66 20 74 CREAT flags of t
c450: 68 65 20 50 4f 53 49 58 20 6f 70 65 6e 28 29 0a he POSIX open().
c460: 2a 2a 20 41 50 49 2e 20 20 54 68 65 20 53 51 4c ** API. The SQL
c470: 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 ITE_OPEN_EXCLUSI
c480: 56 45 20 66 6c 61 67 2c 20 77 68 65 6e 20 70 61 VE flag, when pa
c490: 69 72 65 64 20 77 69 74 68 20 74 68 65 20 0a 2a ired with the .*
c4a0: 2a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 * SQLITE_OPEN_CR
c4b0: 45 41 54 45 2c 20 69 73 20 75 73 65 64 20 74 6f EATE, is used to
c4c0: 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 66 indicate that f
c4d0: 69 6c 65 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 ile should alway
c4e0: 73 0a 2a 2a 20 62 65 20 63 72 65 61 74 65 64 2c s.** be created,
c4f0: 20 61 6e 64 20 74 68 61 74 20 69 74 20 69 73 20 and that it is
c500: 61 6e 20 65 72 72 6f 72 20 69 66 20 69 74 20 61 an error if it a
c510: 6c 72 65 61 64 79 20 65 78 69 73 74 73 2e 0a 2a lready exists..*
c520: 2a 20 49 74 20 69 73 20 3c 69 3e 6e 6f 74 3c 2f * It is <i>not</
c530: 69 3e 20 75 73 65 64 20 74 6f 20 69 6e 64 69 63 i> used to indic
c540: 61 74 65 20 74 68 65 20 66 69 6c 65 20 73 68 6f ate the file sho
c550: 75 6c 64 20 62 65 20 6f 70 65 6e 65 64 20 0a 2a uld be opened .*
c560: 2a 20 66 6f 72 20 65 78 63 6c 75 73 69 76 65 20 * for exclusive
c570: 61 63 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 41 74 access..**.** At
c580: 20 6c 65 61 73 74 20 73 7a 4f 73 46 69 6c 65 20 least szOsFile
c590: 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 bytes of memory
c5a0: 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 are allocated by
c5b0: 20 53 51 4c 69 74 65 0a 2a 2a 20 74 6f 20 68 6f SQLite.** to ho
c5c0: 6c 64 20 74 68 65 20 20 5b 73 71 6c 69 74 65 33 ld the [sqlite3
c5d0: 5f 66 69 6c 65 5d 20 73 74 72 75 63 74 75 72 65 _file] structure
c5e0: 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 74 passed as the t
c5f0: 68 69 72 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 hird.** argument
c600: 20 74 6f 20 78 4f 70 65 6e 2e 20 20 54 68 65 20 to xOpen. The
c610: 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 64 6f 65 xOpen method doe
c620: 73 20 6e 6f 74 20 68 61 76 65 20 74 6f 0a 2a 2a s not have to.**
c630: 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 73 74 allocate the st
c640: 72 75 63 74 75 72 65 3b 20 69 74 20 73 68 6f 75 ructure; it shou
c650: 6c 64 20 6a 75 73 74 20 66 69 6c 6c 20 69 74 20 ld just fill it
c660: 69 6e 2e 20 20 4e 6f 74 65 20 74 68 61 74 0a 2a in. Note that.*
c670: 2a 20 74 68 65 20 78 4f 70 65 6e 20 6d 65 74 68 * the xOpen meth
c680: 6f 64 20 6d 75 73 74 20 73 65 74 20 74 68 65 20 od must set the
c690: 73 71 6c 69 74 65 33 5f 66 69 6c 65 2e 70 4d 65 sqlite3_file.pMe
c6a0: 74 68 6f 64 73 20 74 6f 20 65 69 74 68 65 72 0a thods to either.
c6b0: 2a 2a 20 61 20 76 61 6c 69 64 20 5b 73 71 6c 69 ** a valid [sqli
c6c0: 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 te3_io_methods]
c6d0: 6f 62 6a 65 63 74 20 6f 72 20 74 6f 20 4e 55 4c object or to NUL
c6e0: 4c 2e 20 20 78 4f 70 65 6e 20 6d 75 73 74 20 64 L. xOpen must d
c6f0: 6f 0a 2a 2a 20 74 68 69 73 20 65 76 65 6e 20 69 o.** this even i
c700: 66 20 74 68 65 20 6f 70 65 6e 20 66 61 69 6c 73 f the open fails
c710: 2e 20 20 53 51 4c 69 74 65 20 65 78 70 65 63 74 . SQLite expect
c720: 73 20 74 68 61 74 20 74 68 65 20 73 71 6c 69 74 s that the sqlit
c730: 65 33 5f 66 69 6c 65 2e 70 4d 65 74 68 6f 64 73 e3_file.pMethods
c740: 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 77 69 6c 6c .** element will
c750: 20 62 65 20 76 61 6c 69 64 20 61 66 74 65 72 20 be valid after
c760: 78 4f 70 65 6e 20 72 65 74 75 72 6e 73 20 72 65 xOpen returns re
c770: 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 gardless of the
c780: 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72 20 66 61 success.** or fa
c790: 69 6c 75 72 65 20 6f 66 20 74 68 65 20 78 4f 70 ilure of the xOp
c7a0: 65 6e 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 en call..**.** T
c7b0: 68 65 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e he flags argumen
c7c0: 74 20 74 6f 20 78 41 63 63 65 73 73 28 29 20 6d t to xAccess() m
c7d0: 61 79 20 62 65 20 5b 53 51 4c 49 54 45 5f 41 43 ay be [SQLITE_AC
c7e0: 43 45 53 53 5f 45 58 49 53 54 53 5d 0a 2a 2a 20 CESS_EXISTS].**
c7f0: 74 6f 20 74 65 73 74 20 66 6f 72 20 74 68 65 20 to test for the
c800: 65 78 69 73 74 65 6e 63 65 20 6f 66 20 61 20 66 existence of a f
c810: 69 6c 65 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f ile, or [SQLITE_
c820: 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 54 45 ACCESS_READWRITE
c830: 5d 20 74 6f 0a 2a 2a 20 74 65 73 74 20 77 68 65 ] to.** test whe
c840: 74 68 65 72 20 61 20 66 69 6c 65 20 69 73 20 72 ther a file is r
c850: 65 61 64 61 62 6c 65 20 61 6e 64 20 77 72 69 74 eadable and writ
c860: 61 62 6c 65 2c 20 6f 72 20 5b 53 51 4c 49 54 45 able, or [SQLITE
c870: 5f 41 43 43 45 53 53 5f 52 45 41 44 5d 0a 2a 2a _ACCESS_READ].**
c880: 20 74 6f 20 74 65 73 74 20 77 68 65 74 68 65 72 to test whether
c890: 20 61 20 66 69 6c 65 20 69 73 20 61 74 20 6c 65 a file is at le
c8a0: 61 73 74 20 72 65 61 64 61 62 6c 65 2e 20 20 20 ast readable.
c8b0: 54 68 65 20 66 69 6c 65 20 63 61 6e 20 62 65 20 The file can be
c8c0: 61 0a 2a 2a 20 64 69 72 65 63 74 6f 72 79 2e 0a a.** directory..
c8d0: 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c **.** SQLite wil
c8e0: 6c 20 61 6c 77 61 79 73 20 61 6c 6c 6f 63 61 74 l always allocat
c8f0: 65 20 61 74 20 6c 65 61 73 74 20 6d 78 50 61 74 e at least mxPat
c900: 68 6e 61 6d 65 2b 31 20 62 79 74 65 73 20 66 6f hname+1 bytes fo
c910: 72 20 74 68 65 0a 2a 2a 20 6f 75 74 70 75 74 20 r the.** output
c920: 62 75 66 66 65 72 20 78 46 75 6c 6c 50 61 74 68 buffer xFullPath
c930: 6e 61 6d 65 2e 20 20 54 68 65 20 65 78 61 63 74 name. The exact
c940: 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6f 75 74 size of the out
c950: 70 75 74 20 62 75 66 66 65 72 0a 2a 2a 20 69 73 put buffer.** is
c960: 20 61 6c 73 6f 20 70 61 73 73 65 64 20 61 73 20 also passed as
c970: 61 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 62 a parameter to b
c980: 6f 74 68 20 20 6d 65 74 68 6f 64 73 2e 20 49 66 oth methods. If
c990: 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 the output buff
c9a0: 65 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 6c 61 72 er.** is not lar
c9b0: 67 65 20 65 6e 6f 75 67 68 2c 20 5b 53 51 4c 49 ge enough, [SQLI
c9c0: 54 45 5f 43 41 4e 54 4f 50 45 4e 5d 20 73 68 6f TE_CANTOPEN] sho
c9d0: 75 6c 64 20 62 65 20 72 65 74 75 72 6e 65 64 2e uld be returned.
c9e0: 20 53 69 6e 63 65 20 74 68 69 73 20 69 73 0a 2a Since this is.*
c9f0: 2a 20 68 61 6e 64 6c 65 64 20 61 73 20 61 20 66 * handled as a f
ca00: 61 74 61 6c 20 65 72 72 6f 72 20 62 79 20 53 51 atal error by SQ
ca10: 4c 69 74 65 2c 20 76 66 73 20 69 6d 70 6c 65 6d Lite, vfs implem
ca20: 65 6e 74 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 entations should
ca30: 20 65 6e 64 65 61 76 6f 72 0a 2a 2a 20 74 6f 20 endeavor.** to
ca40: 70 72 65 76 65 6e 74 20 74 68 69 73 20 62 79 20 prevent this by
ca50: 73 65 74 74 69 6e 67 20 6d 78 50 61 74 68 6e 61 setting mxPathna
ca60: 6d 65 20 74 6f 20 61 20 73 75 66 66 69 63 69 65 me to a sufficie
ca70: 6e 74 6c 79 20 6c 61 72 67 65 20 76 61 6c 75 65 ntly large value
ca80: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 52 61 6e ..**.** The xRan
ca90: 64 6f 6d 6e 65 73 73 28 29 2c 20 78 53 6c 65 65 domness(), xSlee
caa0: 70 28 29 2c 20 61 6e 64 20 78 43 75 72 72 65 6e p(), and xCurren
cab0: 74 54 69 6d 65 28 29 20 69 6e 74 65 72 66 61 63 tTime() interfac
cac0: 65 73 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 73 74 es.** are not st
cad0: 72 69 63 74 6c 79 20 61 20 70 61 72 74 20 6f 66 rictly a part of
cae0: 20 74 68 65 20 66 69 6c 65 73 79 73 74 65 6d 2c the filesystem,
caf0: 20 62 75 74 20 74 68 65 79 20 61 72 65 0a 2a 2a but they are.**
cb00: 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 65 included in the
cb10: 20 56 46 53 20 73 74 72 75 63 74 75 72 65 20 66 VFS structure f
cb20: 6f 72 20 63 6f 6d 70 6c 65 74 65 6e 65 73 73 2e or completeness.
cb30: 0a 2a 2a 20 54 68 65 20 78 52 61 6e 64 6f 6d 6e .** The xRandomn
cb40: 65 73 73 28 29 20 66 75 6e 63 74 69 6f 6e 20 61 ess() function a
cb50: 74 74 65 6d 70 74 73 20 74 6f 20 72 65 74 75 72 ttempts to retur
cb60: 6e 20 6e 42 79 74 65 73 20 62 79 74 65 73 0a 2a n nBytes bytes.*
cb70: 2a 20 6f 66 20 67 6f 6f 64 2d 71 75 61 6c 69 74 * of good-qualit
cb80: 79 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e 74 y randomness int
cb90: 6f 20 7a 4f 75 74 2e 20 20 54 68 65 20 72 65 74 o zOut. The ret
cba0: 75 72 6e 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 urn value is.**
cbb0: 74 68 65 20 61 63 74 75 61 6c 20 6e 75 6d 62 65 the actual numbe
cbc0: 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 72 61 r of bytes of ra
cbd0: 6e 64 6f 6d 6e 65 73 73 20 6f 62 74 61 69 6e 65 ndomness obtaine
cbe0: 64 2e 0a 2a 2a 20 54 68 65 20 78 53 6c 65 65 70 d..** The xSleep
cbf0: 28 29 20 6d 65 74 68 6f 64 20 63 61 75 73 65 73 () method causes
cc00: 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 74 68 72 the calling thr
cc10: 65 61 64 20 74 6f 20 73 6c 65 65 70 20 66 6f 72 ead to sleep for
cc20: 20 61 74 0a 2a 2a 20 6c 65 61 73 74 20 74 68 65 at.** least the
cc30: 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f number of micro
cc40: 73 65 63 6f 6e 64 73 20 67 69 76 65 6e 2e 20 20 seconds given.
cc50: 54 68 65 20 78 43 75 72 72 65 6e 74 54 69 6d 65 The xCurrentTime
cc60: 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 72 65 74 ().** method ret
cc70: 75 72 6e 73 20 61 20 4a 75 6c 69 61 6e 20 44 61 urns a Julian Da
cc80: 79 20 4e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 y Number for the
cc90: 20 63 75 72 72 65 6e 74 20 64 61 74 65 20 61 6e current date an
cca0: 64 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2f 0a 74 79 d time..**.*/.ty
ccb0: 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c pedef struct sql
ccc0: 69 74 65 33 5f 76 66 73 20 73 71 6c 69 74 65 33 ite3_vfs sqlite3
ccd0: 5f 76 66 73 3b 0a 73 74 72 75 63 74 20 73 71 6c _vfs;.struct sql
cce0: 69 74 65 33 5f 76 66 73 20 7b 0a 20 20 69 6e 74 ite3_vfs {. int
ccf0: 20 69 56 65 72 73 69 6f 6e 3b 20 20 20 20 20 20 iVersion;
cd00: 20 20 20 20 20 20 2f 2a 20 53 74 72 75 63 74 75 /* Structu
cd10: 72 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 re version numbe
cd20: 72 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 4f 73 46 r */. int szOsF
cd30: 69 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ile;
cd40: 2f 2a 20 53 69 7a 65 20 6f 66 20 73 75 62 63 6c /* Size of subcl
cd50: 61 73 73 65 64 20 73 71 6c 69 74 65 33 5f 66 69 assed sqlite3_fi
cd60: 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 50 61 le */. int mxPa
cd70: 74 68 6e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 thname;
cd80: 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 66 69 6c 65 /* Maximum file
cd90: 20 70 61 74 68 6e 61 6d 65 20 6c 65 6e 67 74 68 pathname length
cda0: 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 */. sqlite3_vf
cdb0: 73 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 2f s *pNext; /
cdc0: 2a 20 4e 65 78 74 20 72 65 67 69 73 74 65 72 65 * Next registere
cdd0: 64 20 56 46 53 20 2a 2f 0a 20 20 63 6f 6e 73 74 d VFS */. const
cde0: 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 char *zName;
cdf0: 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 /* Name of t
ce00: 68 69 73 20 76 69 72 74 75 61 6c 20 66 69 6c 65 his virtual file
ce10: 20 73 79 73 74 65 6d 20 2a 2f 0a 20 20 76 6f 69 system */. voi
ce20: 64 20 2a 70 41 70 70 44 61 74 61 3b 20 20 20 20 d *pAppData;
ce30: 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
ce40: 20 74 6f 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d to application-
ce50: 73 70 65 63 69 66 69 63 20 64 61 74 61 20 2a 2f specific data */
ce60: 0a 20 20 69 6e 74 20 28 2a 78 4f 70 65 6e 29 28 . int (*xOpen)(
ce70: 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f sqlite3_vfs*, co
ce80: 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c nst char *zName,
ce90: 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 0a sqlite3_file*,.
cea0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 i
ceb0: 6e 74 20 66 6c 61 67 73 2c 20 69 6e 74 20 2a 70 nt flags, int *p
cec0: 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20 69 6e 74 OutFlags);. int
ced0: 20 28 2a 78 44 65 6c 65 74 65 29 28 73 71 6c 69 (*xDelete)(sqli
cee0: 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 te3_vfs*, const
cef0: 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 char *zName, int
cf00: 20 73 79 6e 63 44 69 72 29 3b 0a 20 20 69 6e 74 syncDir);. int
cf10: 20 28 2a 78 41 63 63 65 73 73 29 28 73 71 6c 69 (*xAccess)(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 4e 61 6d 65 2c 20 69 6e 74 char *zName, int
cf40: 20 66 6c 61 67 73 2c 20 69 6e 74 20 2a 70 52 65 flags, int *pRe
cf50: 73 4f 75 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 sOut);. int (*x
cf60: 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 28 73 71 FullPathname)(sq
cf70: 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 lite3_vfs*, cons
cf80: 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 t char *zName, i
cf90: 6e 74 20 6e 4f 75 74 2c 20 63 68 61 72 20 2a 7a nt nOut, char *z
cfa0: 4f 75 74 29 3b 0a 20 20 76 6f 69 64 20 2a 28 2a Out);. void *(*
cfb0: 78 44 6c 4f 70 65 6e 29 28 73 71 6c 69 74 65 33 xDlOpen)(sqlite3
cfc0: 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 _vfs*, const cha
cfd0: 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 r *zFilename);.
cfe0: 20 76 6f 69 64 20 28 2a 78 44 6c 45 72 72 6f 72 void (*xDlError
cff0: 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 )(sqlite3_vfs*,
d000: 69 6e 74 20 6e 42 79 74 65 2c 20 63 68 61 72 20 int nByte, char
d010: 2a 7a 45 72 72 4d 73 67 29 3b 0a 20 20 76 6f 69 *zErrMsg);. voi
d020: 64 20 28 2a 28 2a 78 44 6c 53 79 6d 29 28 73 71 d (*(*xDlSym)(sq
d030: 6c 69 74 65 33 5f 76 66 73 2a 2c 76 6f 69 64 2a lite3_vfs*,void*
d040: 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 , const char *zS
d050: 79 6d 62 6f 6c 29 29 28 76 6f 69 64 29 3b 0a 20 ymbol))(void);.
d060: 20 76 6f 69 64 20 28 2a 78 44 6c 43 6c 6f 73 65 void (*xDlClose
d070: 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 )(sqlite3_vfs*,
d080: 76 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 20 28 2a void*);. int (*
d090: 78 52 61 6e 64 6f 6d 6e 65 73 73 29 28 73 71 6c xRandomness)(sql
d0a0: 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 20 6e ite3_vfs*, int n
d0b0: 42 79 74 65 2c 20 63 68 61 72 20 2a 7a 4f 75 74 Byte, char *zOut
d0c0: 29 3b 0a 20 20 69 6e 74 20 28 2a 78 53 6c 65 65 );. int (*xSlee
d0d0: 70 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c p)(sqlite3_vfs*,
d0e0: 20 69 6e 74 20 6d 69 63 72 6f 73 65 63 6f 6e 64 int microsecond
d0f0: 73 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 75 72 s);. int (*xCur
d100: 72 65 6e 74 54 69 6d 65 29 28 73 71 6c 69 74 65 rentTime)(sqlite
d110: 33 5f 76 66 73 2a 2c 20 64 6f 75 62 6c 65 2a 29 3_vfs*, double*)
d120: 3b 0a 20 20 69 6e 74 20 28 2a 78 47 65 74 4c 61 ;. int (*xGetLa
d130: 73 74 45 72 72 6f 72 29 28 73 71 6c 69 74 65 33 stError)(sqlite3
d140: 5f 76 66 73 2a 2c 20 69 6e 74 2c 20 63 68 61 72 _vfs*, int, char
d150: 20 2a 29 3b 0a 20 20 2f 2a 20 4e 65 77 20 66 69 *);. /* New fi
d160: 65 6c 64 73 20 6d 61 79 20 62 65 20 61 70 70 65 elds may be appe
d170: 6e 64 65 64 20 69 6e 20 66 69 67 75 72 65 20 76 nded in figure v
d180: 65 72 73 69 6f 6e 73 2e 20 20 54 68 65 20 69 56 ersions. The iV
d190: 65 72 73 69 6f 6e 0a 20 20 2a 2a 20 76 61 6c 75 ersion. ** valu
d1a0: 65 20 77 69 6c 6c 20 69 6e 63 72 65 6d 65 6e 74 e will increment
d1b0: 20 77 68 65 6e 65 76 65 72 20 74 68 69 73 20 68 whenever this h
d1c0: 61 70 70 65 6e 73 2e 20 2a 2f 0a 7d 3b 0a 0a 2f appens. */.};../
d1d0: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 *.** CAPI3REF: F
d1e0: 6c 61 67 73 20 66 6f 72 20 74 68 65 20 78 41 63 lags for the xAc
d1f0: 63 65 73 73 20 56 46 53 20 6d 65 74 68 6f 64 20 cess VFS method
d200: 7b 48 31 31 31 39 30 7d 20 3c 48 31 31 31 34 30 {H11190} <H11140
d210: 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 69 6e >.**.** These in
d220: 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 20 teger constants
d230: 63 61 6e 20 62 65 20 75 73 65 64 20 61 73 20 74 can be used as t
d240: 68 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 he third paramet
d250: 65 72 20 74 6f 0a 2a 2a 20 74 68 65 20 78 41 63 er to.** the xAc
d260: 63 65 73 73 20 6d 65 74 68 6f 64 20 6f 66 20 61 cess method of a
d270: 6e 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 n [sqlite3_vfs]
d280: 6f 62 6a 65 63 74 2e 20 7b 45 4e 44 7d 20 20 54 object. {END} T
d290: 68 65 79 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a hey determine.**
d2a0: 20 77 68 61 74 20 6b 69 6e 64 20 6f 66 20 70 65 what kind of pe
d2b0: 72 6d 69 73 73 69 6f 6e 73 20 74 68 65 20 78 41 rmissions the xA
d2c0: 63 63 65 73 73 20 6d 65 74 68 6f 64 20 69 73 20 ccess method is
d2d0: 6c 6f 6f 6b 69 6e 67 20 66 6f 72 2e 0a 2a 2a 20 looking for..**
d2e0: 57 69 74 68 20 53 51 4c 49 54 45 5f 41 43 43 45 With SQLITE_ACCE
d2f0: 53 53 5f 45 58 49 53 54 53 2c 20 74 68 65 20 78 SS_EXISTS, the x
d300: 41 63 63 65 73 73 20 6d 65 74 68 6f 64 0a 2a 2a Access method.**
d310: 20 73 69 6d 70 6c 79 20 63 68 65 63 6b 73 20 77 simply checks w
d320: 68 65 74 68 65 72 20 74 68 65 20 66 69 6c 65 20 hether the file
d330: 65 78 69 73 74 73 2e 0a 2a 2a 20 57 69 74 68 20 exists..** With
d340: 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 SQLITE_ACCESS_RE
d350: 41 44 57 52 49 54 45 2c 20 74 68 65 20 78 41 63 ADWRITE, the xAc
d360: 63 65 73 73 20 6d 65 74 68 6f 64 0a 2a 2a 20 63 cess method.** c
d370: 68 65 63 6b 73 20 77 68 65 74 68 65 72 20 74 68 hecks whether th
d380: 65 20 66 69 6c 65 20 69 73 20 62 6f 74 68 20 72 e file is both r
d390: 65 61 64 61 62 6c 65 20 61 6e 64 20 77 72 69 74 eadable and writ
d3a0: 61 62 6c 65 2e 0a 2a 2a 20 57 69 74 68 20 53 51 able..** With SQ
d3b0: 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 LITE_ACCESS_READ
d3c0: 2c 20 74 68 65 20 78 41 63 63 65 73 73 20 6d 65 , the xAccess me
d3d0: 74 68 6f 64 0a 2a 2a 20 63 68 65 63 6b 73 20 77 thod.** checks w
d3e0: 68 65 74 68 65 72 20 74 68 65 20 66 69 6c 65 20 hether the file
d3f0: 69 73 20 72 65 61 64 61 62 6c 65 2e 0a 2a 2f 0a is readable..*/.
d400: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 #define SQLITE_A
d410: 43 43 45 53 53 5f 45 58 49 53 54 53 20 20 20 20 CCESS_EXISTS
d420: 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 0.#define SQLITE
d430: 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 54 _ACCESS_READWRIT
d440: 45 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 E 1.#define SQLI
d450: 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 20 20 TE_ACCESS_READ
d460: 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 2../*.** CAP
d470: 49 33 52 45 46 3a 20 49 6e 69 74 69 61 6c 69 7a I3REF: Initializ
d480: 65 20 54 68 65 20 53 51 4c 69 74 65 20 4c 69 62 e The SQLite Lib
d490: 72 61 72 79 20 7b 48 31 30 31 33 30 7d 20 3c 53 rary {H10130} <S
d4a0: 32 30 30 30 30 3e 3c 53 33 30 31 30 30 3e 0a 2a 20000><S30100>.*
d4b0: 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
d4c0: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 72 6f _initialize() ro
d4d0: 75 74 69 6e 65 20 69 6e 69 74 69 61 6c 69 7a 65 utine initialize
d4e0: 73 20 74 68 65 0a 2a 2a 20 53 51 4c 69 74 65 20 s the.** SQLite
d4f0: 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20 73 71 library. The sq
d500: 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 lite3_shutdown()
d510: 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 64 65 61 6c routine.** deal
d520: 6c 6f 63 61 74 65 73 20 61 6e 79 20 72 65 73 6f locates any reso
d530: 75 72 63 65 73 20 74 68 61 74 20 77 65 72 65 20 urces that were
d540: 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 73 71 6c allocated by sql
d550: 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
d560: 29 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 )..** This routi
d570: 6e 65 73 20 61 72 65 20 64 65 73 69 67 6e 65 64 nes are designed
d580: 20 74 6f 20 61 69 64 20 69 6e 20 70 72 6f 63 65 to aid in proce
d590: 73 73 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f ss initializatio
d5a0: 6e 20 61 6e 64 0a 2a 2a 20 73 68 75 74 64 6f 77 n and.** shutdow
d5b0: 6e 20 6f 6e 20 65 6d 62 65 64 64 65 64 20 73 79 n on embedded sy
d5c0: 73 74 65 6d 73 2e 20 20 57 6f 72 6b 73 74 61 74 stems. Workstat
d5d0: 69 6f 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 ion applications
d5e0: 20 75 73 69 6e 67 0a 2a 2a 20 53 51 4c 69 74 65 using.** SQLite
d5f0: 20 6e 6f 72 6d 61 6c 6c 79 20 64 6f 20 6e 6f 74 normally do not
d600: 20 6e 65 65 64 20 74 6f 20 69 6e 76 6f 6b 65 20 need to invoke
d610: 65 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 either of these
d620: 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 routines..**.**
d630: 41 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 A call to sqlite
d640: 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 3_initialize() i
d650: 73 20 61 6e 20 22 65 66 66 65 63 74 69 76 65 22 s an "effective"
d660: 20 63 61 6c 6c 20 69 66 20 69 74 20 69 73 0a 2a call if it is.*
d670: 2a 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 * the first time
d680: 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c sqlite3_initial
d690: 69 7a 65 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 ize() is invoked
d6a0: 20 64 75 72 69 6e 67 20 74 68 65 20 6c 69 66 65 during the life
d6b0: 74 69 6d 65 20 6f 66 0a 2a 2a 20 74 68 65 20 70 time of.** the p
d6c0: 72 6f 63 65 73 73 2c 20 6f 72 20 69 66 20 69 74 rocess, or if it
d6d0: 20 69 73 20 74 68 65 20 66 69 72 73 74 20 74 69 is the first ti
d6e0: 6d 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 me sqlite3_initi
d6f0: 61 6c 69 7a 65 28 29 20 69 73 20 69 6e 76 6f 6b alize() is invok
d700: 65 64 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 ed.** following
d710: 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 a call to sqlite
d720: 33 5f 73 68 75 74 64 6f 77 6e 28 29 2e 20 20 4f 3_shutdown(). O
d730: 6e 6c 79 20 61 6e 20 65 66 66 65 63 74 69 76 65 nly an effective
d740: 20 63 61 6c 6c 0a 2a 2a 20 6f 66 20 73 71 6c 69 call.** of sqli
d750: 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 te3_initialize()
d760: 20 64 6f 65 73 20 61 6e 79 20 69 6e 69 74 69 61 does any initia
d770: 6c 69 7a 61 74 69 6f 6e 2e 20 20 41 6c 6c 20 6f lization. All o
d780: 74 68 65 72 20 63 61 6c 6c 73 0a 2a 2a 20 61 72 ther calls.** ar
d790: 65 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70 e harmless no-op
d7a0: 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 s..**.** A call
d7b0: 74 6f 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 to sqlite3_shutd
d7c0: 6f 77 6e 28 29 20 69 73 20 61 6e 20 22 65 66 66 own() is an "eff
d7d0: 65 63 74 69 76 65 22 20 63 61 6c 6c 20 69 66 20 ective" call if
d7e0: 69 74 20 69 73 20 74 68 65 20 66 69 72 73 74 0a it is the first.
d7f0: 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 ** call to sqlit
d800: 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 20 73 69 e3_shutdown() si
d810: 6e 63 65 20 74 68 65 20 6c 61 73 74 20 73 71 6c nce the last sql
d820: 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
d830: 29 2e 20 20 4f 6e 6c 79 0a 2a 2a 20 61 6e 20 65 ). Only.** an e
d840: 66 66 65 63 74 69 76 65 20 63 61 6c 6c 20 74 6f ffective call to
d850: 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 sqlite3_shutdow
d860: 6e 28 29 20 64 6f 65 73 20 61 6e 79 20 64 65 69 n() does any dei
d870: 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2e 0a 2a nitialization..*
d880: 2a 20 41 6c 6c 20 6f 74 68 65 72 20 76 61 6c 69 * All other vali
d890: 64 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 d calls to sqlit
d8a0: 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 20 61 72 e3_shutdown() ar
d8b0: 65 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70 e harmless no-op
d8c0: 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c s..**.** The sql
d8d0: 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
d8e0: 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 74 ) interface is t
d8f0: 68 72 65 61 64 73 61 66 65 2c 20 62 75 74 20 73 hreadsafe, but s
d900: 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 qlite3_shutdown(
d910: 29 0a 2a 2a 20 69 73 20 6e 6f 74 2e 20 20 54 68 ).** is not. Th
d920: 65 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f e sqlite3_shutdo
d930: 77 6e 28 29 20 69 6e 74 65 72 66 61 63 65 20 6d wn() interface m
d940: 75 73 74 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c ust only be call
d950: 65 64 20 66 72 6f 6d 20 61 0a 2a 2a 20 73 69 6e ed from a.** sin
d960: 67 6c 65 20 74 68 72 65 61 64 2e 20 20 41 6c 6c gle thread. All
d970: 20 6f 70 65 6e 20 5b 64 61 74 61 62 61 73 65 20 open [database
d980: 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d 20 6d 75 73 connections] mus
d990: 74 20 62 65 20 63 6c 6f 73 65 64 20 61 6e 64 20 t be closed and
d9a0: 61 6c 6c 0a 2a 2a 20 6f 74 68 65 72 20 53 51 4c all.** other SQL
d9b0: 69 74 65 20 72 65 73 6f 75 72 63 65 73 20 6d 75 ite resources mu
d9c0: 73 74 20 62 65 20 64 65 61 6c 6c 6f 63 61 74 65 st be deallocate
d9d0: 64 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b d prior to invok
d9e0: 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73 ing.** sqlite3_s
d9f0: 68 75 74 64 6f 77 6e 28 29 2e 0a 2a 2a 0a 2a 2a hutdown()..**.**
da00: 20 41 6d 6f 6e 67 20 6f 74 68 65 72 20 74 68 69 Among other thi
da10: 6e 67 73 2c 20 73 71 6c 69 74 65 33 5f 69 6e 69 ngs, sqlite3_ini
da20: 74 69 61 6c 69 7a 65 28 29 20 77 69 6c 6c 20 69 tialize() will i
da30: 6e 76 6f 6b 65 0a 2a 2a 20 73 71 6c 69 74 65 33 nvoke.** sqlite3
da40: 5f 6f 73 5f 69 6e 69 74 28 29 2e 20 20 53 69 6d _os_init(). Sim
da50: 69 6c 61 72 6c 79 2c 20 73 71 6c 69 74 65 33 5f ilarly, sqlite3_
da60: 73 68 75 74 64 6f 77 6e 28 29 0a 2a 2a 20 77 69 shutdown().** wi
da70: 6c 6c 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 ll invoke sqlite
da80: 33 5f 6f 73 5f 65 6e 64 28 29 2e 0a 2a 2a 0a 2a 3_os_end()..**.*
da90: 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e * The sqlite3_in
daa0: 69 74 69 61 6c 69 7a 65 28 29 20 72 6f 75 74 69 itialize() routi
dab0: 6e 65 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 ne returns [SQLI
dac0: 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73 TE_OK] on succes
dad0: 73 2e 0a 2a 2a 20 49 66 20 66 6f 72 20 73 6f 6d s..** If for som
dae0: 65 20 72 65 61 73 6f 6e 2c 20 73 71 6c 69 74 65 e reason, sqlite
daf0: 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 3_initialize() i
db00: 73 20 75 6e 61 62 6c 65 20 74 6f 20 69 6e 69 74 s unable to init
db10: 69 61 6c 69 7a 65 0a 2a 2a 20 74 68 65 20 6c 69 ialize.** the li
db20: 62 72 61 72 79 20 28 70 65 72 68 61 70 73 20 69 brary (perhaps i
db30: 74 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 61 t is unable to a
db40: 6c 6c 6f 63 61 74 65 20 61 20 6e 65 65 64 65 64 llocate a needed
db50: 20 72 65 73 6f 75 72 63 65 20 73 75 63 68 0a 2a resource such.*
db60: 2a 20 61 73 20 61 20 6d 75 74 65 78 29 20 69 74 * as a mutex) it
db70: 20 72 65 74 75 72 6e 73 20 61 6e 20 5b 65 72 72 returns an [err
db80: 6f 72 20 63 6f 64 65 5d 20 6f 74 68 65 72 20 74 or code] other t
db90: 68 61 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e han [SQLITE_OK].
dba0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
dbb0: 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 e3_initialize()
dbc0: 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
dbd0: 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 62 79 20 d internally by
dbe0: 6d 61 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 53 51 many other.** SQ
dbf0: 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 73 20 Lite interfaces
dc00: 73 6f 20 74 68 61 74 20 61 6e 20 61 70 70 6c 69 so that an appli
dc10: 63 61 74 69 6f 6e 20 75 73 75 61 6c 6c 79 20 64 cation usually d
dc20: 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 0a oes not need to.
dc30: 2a 2a 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 ** invoke sqlite
dc40: 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 64 3_initialize() d
dc50: 69 72 65 63 74 6c 79 2e 20 20 46 6f 72 20 65 78 irectly. For ex
dc60: 61 6d 70 6c 65 2c 20 5b 73 71 6c 69 74 65 33 5f ample, [sqlite3_
dc70: 6f 70 65 6e 28 29 5d 0a 2a 2a 20 63 61 6c 6c 73 open()].** calls
dc80: 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c sqlite3_initial
dc90: 69 7a 65 28 29 20 73 6f 20 74 68 65 20 53 51 4c ize() so the SQL
dca0: 69 74 65 20 6c 69 62 72 61 72 79 20 77 69 6c 6c ite library will
dcb0: 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c be automaticall
dcc0: 79 0a 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 64 y.** initialized
dcd0: 20 77 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 6f when [sqlite3_o
dce0: 70 65 6e 28 29 5d 20 69 73 20 63 61 6c 6c 65 64 pen()] is called
dcf0: 20 69 66 20 69 74 20 68 61 73 20 6e 6f 74 20 62 if it has not b
dd00: 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 0a 2a 2a e initialized.**
dd10: 20 61 6c 72 65 61 64 79 2e 20 20 48 6f 77 65 76 already. Howev
dd20: 65 72 2c 20 69 66 20 53 51 4c 69 74 65 20 69 73 er, if SQLite is
dd30: 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 compiled with t
dd40: 68 65 20 5b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f he [SQLITE_OMIT_
dd50: 41 55 54 4f 49 4e 49 54 5d 0a 2a 2a 20 63 6f 6d AUTOINIT].** com
dd60: 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e pile-time option
dd70: 2c 20 74 68 65 6e 20 74 68 65 20 61 75 74 6f 6d , then the autom
dd80: 61 74 69 63 20 63 61 6c 6c 73 20 74 6f 20 73 71 atic calls to sq
dd90: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
dda0: 28 29 0a 2a 2a 20 61 72 65 20 6f 6d 69 74 74 65 ().** are omitte
ddb0: 64 20 61 6e 64 20 74 68 65 20 61 70 70 6c 69 63 d and the applic
ddc0: 61 74 69 6f 6e 20 6d 75 73 74 20 63 61 6c 6c 20 ation must call
ddd0: 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
dde0: 7a 65 28 29 20 64 69 72 65 63 74 6c 79 0a 2a 2a ze() directly.**
ddf0: 20 70 72 69 6f 72 20 74 6f 20 75 73 69 6e 67 20 prior to using
de00: 61 6e 79 20 6f 74 68 65 72 20 53 51 4c 69 74 65 any other SQLite
de10: 20 69 6e 74 65 72 66 61 63 65 2e 20 20 46 6f 72 interface. For
de20: 20 6d 61 78 69 6d 75 6d 20 70 6f 72 74 61 62 69 maximum portabi
de30: 6c 69 74 79 2c 0a 2a 2a 20 69 74 20 69 73 20 72 lity,.** it is r
de40: 65 63 6f 6d 6d 65 6e 64 65 64 20 74 68 61 74 20 ecommended that
de50: 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 61 6c 77 applications alw
de60: 61 79 73 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 ays invoke sqlit
de70: 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 0a e3_initialize().
de80: 2a 2a 20 64 69 72 65 63 74 6c 79 20 70 72 69 6f ** directly prio
de90: 72 20 74 6f 20 75 73 69 6e 67 20 61 6e 79 20 6f r to using any o
dea0: 74 68 65 72 20 53 51 4c 69 74 65 20 69 6e 74 65 ther SQLite inte
deb0: 72 66 61 63 65 2e 20 20 46 75 74 75 72 65 20 72 rface. Future r
dec0: 65 6c 65 61 73 65 73 0a 2a 2a 20 6f 66 20 53 51 eleases.** of SQ
ded0: 4c 69 74 65 20 6d 61 79 20 72 65 71 75 69 72 65 Lite may require
dee0: 20 74 68 69 73 2e 20 20 49 6e 20 6f 74 68 65 72 this. In other
def0: 20 77 6f 72 64 73 2c 20 74 68 65 20 62 65 68 61 words, the beha
df00: 76 69 6f 72 20 65 78 68 69 62 69 74 65 64 0a 2a vior exhibited.*
df10: 2a 20 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 * when SQLite is
df20: 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 5b compiled with [
df30: 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
df40: 49 4e 49 54 5d 20 6d 69 67 68 74 20 62 65 63 6f INIT] might beco
df50: 6d 65 20 74 68 65 0a 2a 2a 20 64 65 66 61 75 6c me the.** defaul
df60: 74 20 62 65 68 61 76 69 6f 72 20 69 6e 20 73 6f t behavior in so
df70: 6d 65 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 me future releas
df80: 65 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a e of SQLite..**.
df90: 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6f ** The sqlite3_o
dfa0: 73 5f 69 6e 69 74 28 29 20 72 6f 75 74 69 6e 65 s_init() routine
dfb0: 20 64 6f 65 73 20 6f 70 65 72 61 74 69 6e 67 2d does operating-
dfc0: 73 79 73 74 65 6d 20 73 70 65 63 69 66 69 63 0a system specific.
dfd0: 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f ** initializatio
dfe0: 6e 20 6f 66 20 74 68 65 20 53 51 4c 69 74 65 20 n of the SQLite
dff0: 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20 73 71 library. The sq
e000: 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 0a 2a lite3_os_end().*
e010: 2a 20 72 6f 75 74 69 6e 65 20 75 6e 64 6f 65 73 * routine undoes
e020: 20 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 73 the effect of s
e030: 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 qlite3_os_init()
e040: 2e 20 20 54 79 70 69 63 61 6c 20 74 61 73 6b 73 . Typical tasks
e050: 0a 2a 2a 20 70 65 72 66 6f 72 6d 65 64 20 62 79 .** performed by
e060: 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 these routines
e070: 69 6e 63 6c 75 64 65 20 61 6c 6c 6f 63 61 74 69 include allocati
e080: 6f 6e 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 69 on or deallocati
e090: 6f 6e 0a 2a 2a 20 6f 66 20 73 74 61 74 69 63 20 on.** of static
e0a0: 72 65 73 6f 75 72 63 65 73 2c 20 69 6e 69 74 69 resources, initi
e0b0: 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 67 6c 6f alization of glo
e0c0: 62 61 6c 20 76 61 72 69 61 62 6c 65 73 2c 0a 2a bal variables,.*
e0d0: 2a 20 73 65 74 74 69 6e 67 20 75 70 20 61 20 64 * setting up a d
e0e0: 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f efault [sqlite3_
e0f0: 76 66 73 5d 20 6d 6f 64 75 6c 65 2c 20 6f 72 20 vfs] module, or
e100: 73 65 74 74 69 6e 67 20 75 70 0a 2a 2a 20 61 20 setting up.** a
e110: 64 65 66 61 75 6c 74 20 63 6f 6e 66 69 67 75 72 default configur
e120: 61 74 69 6f 6e 20 75 73 69 6e 67 20 5b 73 71 6c ation using [sql
e130: 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 2e 0a ite3_config()]..
e140: 2a 2a 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 **.** The applic
e150: 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 65 76 ation should nev
e160: 65 72 20 69 6e 76 6f 6b 65 20 65 69 74 68 65 72 er invoke either
e170: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 sqlite3_os_init
e180: 28 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 ().** or sqlite3
e190: 5f 6f 73 5f 65 6e 64 28 29 20 64 69 72 65 63 74 _os_end() direct
e1a0: 6c 79 2e 20 20 54 68 65 20 61 70 70 6c 69 63 61 ly. The applica
e1b0: 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 tion should only
e1c0: 20 69 6e 76 6f 6b 65 0a 2a 2a 20 73 71 6c 69 74 invoke.** sqlit
e1d0: 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 e3_initialize()
e1e0: 61 6e 64 20 73 71 6c 69 74 65 33 5f 73 68 75 74 and sqlite3_shut
e1f0: 64 6f 77 6e 28 29 2e 20 20 54 68 65 20 73 71 6c down(). The sql
e200: 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 0a 2a ite3_os_init().*
e210: 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 63 * interface is c
e220: 61 6c 6c 65 64 20 61 75 74 6f 6d 61 74 69 63 61 alled automatica
e230: 6c 6c 79 20 62 79 20 73 71 6c 69 74 65 33 5f 69 lly by sqlite3_i
e240: 6e 69 74 69 61 6c 69 7a 65 28 29 20 61 6e 64 0a nitialize() and.
e250: 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e ** sqlite3_os_en
e260: 64 28 29 20 69 73 20 63 61 6c 6c 65 64 20 62 79 d() is called by
e270: 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 sqlite3_shutdow
e280: 6e 28 29 2e 20 20 41 70 70 72 6f 70 72 69 61 74 n(). Appropriat
e290: 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 e.** implementat
e2a0: 69 6f 6e 73 20 66 6f 72 20 73 71 6c 69 74 65 33 ions for sqlite3
e2b0: 5f 6f 73 5f 69 6e 69 74 28 29 20 61 6e 64 20 73 _os_init() and s
e2c0: 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 0a qlite3_os_end().
e2d0: 2a 2a 20 61 72 65 20 62 75 69 6c 74 20 69 6e 74 ** are built int
e2e0: 6f 20 53 51 4c 69 74 65 20 77 68 65 6e 20 69 74 o SQLite when it
e2f0: 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 66 6f 72 is compiled for
e300: 20 55 6e 69 78 2c 20 57 69 6e 64 6f 77 73 2c 20 Unix, Windows,
e310: 6f 72 20 4f 53 2f 32 2e 0a 2a 2a 20 57 68 65 6e or OS/2..** When
e320: 20 5b 63 75 73 74 6f 6d 20 62 75 69 6c 64 73 20 [custom builds
e330: 7c 20 62 75 69 6c 74 20 66 6f 72 20 6f 74 68 65 | built for othe
e340: 72 20 70 6c 61 74 66 6f 72 6d 73 5d 0a 2a 2a 20 r platforms].**
e350: 28 75 73 69 6e 67 20 74 68 65 20 5b 53 51 4c 49 (using the [SQLI
e360: 54 45 5f 4f 53 5f 4f 54 48 45 52 3d 31 5d 20 63 TE_OS_OTHER=1] c
e370: 6f 6d 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a 20 6f ompile-time.** o
e380: 70 74 69 6f 6e 29 20 74 68 65 20 61 70 70 6c 69 ption) the appli
e390: 63 61 74 69 6f 6e 20 6d 75 73 74 20 73 75 70 70 cation must supp
e3a0: 6c 79 20 61 20 73 75 69 74 61 62 6c 65 20 69 6d ly a suitable im
e3b0: 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 plementation for
e3c0: 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 .** sqlite3_os_i
e3d0: 6e 69 74 28 29 20 61 6e 64 20 73 71 6c 69 74 65 nit() and sqlite
e3e0: 33 5f 6f 73 5f 65 6e 64 28 29 2e 20 20 41 6e 20 3_os_end(). An
e3f0: 61 70 70 6c 69 63 61 74 69 6f 6e 2d 73 75 70 70 application-supp
e400: 6c 69 65 64 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e lied.** implemen
e410: 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 tation of sqlite
e420: 33 5f 6f 73 5f 69 6e 69 74 28 29 20 6f 72 20 73 3_os_init() or s
e430: 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 0a qlite3_os_end().
e440: 2a 2a 20 6d 75 73 74 20 72 65 74 75 72 6e 20 5b ** must return [
e450: 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75 SQLITE_OK] on su
e460: 63 63 65 73 73 20 61 6e 64 20 73 6f 6d 65 20 6f ccess and some o
e470: 74 68 65 72 20 5b 65 72 72 6f 72 20 63 6f 64 65 ther [error code
e480: 5d 20 75 70 6f 6e 0a 2a 2a 20 66 61 69 6c 75 72 ] upon.** failur
e490: 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 e..*/.SQLITE_API
e4a0: 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6e 69 int sqlite3_ini
e4b0: 74 69 61 6c 69 7a 65 28 76 6f 69 64 29 3b 0a 53 tialize(void);.S
e4c0: 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
e4d0: 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 76 lite3_shutdown(v
e4e0: 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 oid);.SQLITE_API
e4f0: 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f int sqlite3_os_
e500: 69 6e 69 74 28 76 6f 69 64 29 3b 0a 53 51 4c 49 init(void);.SQLI
e510: 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
e520: 65 33 5f 6f 73 5f 65 6e 64 28 76 6f 69 64 29 3b e3_os_end(void);
e530: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
e540: 3a 20 43 6f 6e 66 69 67 75 72 69 6e 67 20 54 68 : Configuring Th
e550: 65 20 53 51 4c 69 74 65 20 4c 69 62 72 61 72 79 e SQLite Library
e560: 20 7b 48 31 34 31 30 30 7d 20 3c 53 32 30 30 30 {H14100} <S2000
e570: 30 3e 3c 53 33 30 32 30 30 3e 0a 2a 2a 20 45 58 0><S30200>.** EX
e580: 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a PERIMENTAL.**.**
e590: 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6e The sqlite3_con
e5a0: 66 69 67 28 29 20 69 6e 74 65 72 66 61 63 65 20 fig() interface
e5b0: 69 73 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 is used to make
e5c0: 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 75 72 61 global configura
e5d0: 74 69 6f 6e 0a 2a 2a 20 63 68 61 6e 67 65 73 20 tion.** changes
e5e0: 74 6f 20 53 51 4c 69 74 65 20 69 6e 20 6f 72 64 to SQLite in ord
e5f0: 65 72 20 74 6f 20 74 75 6e 65 20 53 51 4c 69 74 er to tune SQLit
e600: 65 20 74 6f 20 74 68 65 20 73 70 65 63 69 66 69 e to the specifi
e610: 63 20 6e 65 65 64 73 20 6f 66 0a 2a 2a 20 74 68 c needs of.** th
e620: 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20 e application.
e630: 54 68 65 20 64 65 66 61 75 6c 74 20 63 6f 6e 66 The default conf
e640: 69 67 75 72 61 74 69 6f 6e 20 69 73 20 72 65 63 iguration is rec
e650: 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 20 6d 6f 73 ommended for mos
e660: 74 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e t.** application
e670: 73 20 61 6e 64 20 73 6f 20 74 68 69 73 20 72 6f s and so this ro
e680: 75 74 69 6e 65 20 69 73 20 75 73 75 61 6c 6c 79 utine is usually
e690: 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 2e 20 not necessary.
e6a0: 20 49 74 20 69 73 0a 2a 2a 20 70 72 6f 76 69 64 It is.** provid
e6b0: 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20 72 61 ed to support ra
e6c0: 72 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 re applications
e6d0: 77 69 74 68 20 75 6e 75 73 75 61 6c 20 6e 65 65 with unusual nee
e6e0: 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 ds..**.** The sq
e6f0: 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 69 lite3_config() i
e700: 6e 74 65 72 66 61 63 65 20 69 73 20 6e 6f 74 20 nterface is not
e710: 74 68 72 65 61 64 73 61 66 65 2e 20 20 54 68 65 threadsafe. The
e720: 20 61 70 70 6c 69 63 61 74 69 6f 6e 0a 2a 2a 20 application.**
e730: 6d 75 73 74 20 69 6e 73 75 72 65 20 74 68 61 74 must insure that
e740: 20 6e 6f 20 6f 74 68 65 72 20 53 51 4c 69 74 65 no other SQLite
e750: 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 20 interfaces are
e760: 69 6e 76 6f 6b 65 64 20 62 79 20 6f 74 68 65 72 invoked by other
e770: 0a 2a 2a 20 74 68 72 65 61 64 73 20 77 68 69 6c .** threads whil
e780: 65 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 e sqlite3_config
e790: 28 29 20 69 73 20 72 75 6e 6e 69 6e 67 2e 20 20 () is running.
e7a0: 46 75 72 74 68 65 72 6d 6f 72 65 2c 20 73 71 6c Furthermore, sql
e7b0: 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 0a 2a 2a ite3_config().**
e7c0: 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 69 6e 76 may only be inv
e7d0: 6f 6b 65 64 20 70 72 69 6f 72 20 74 6f 20 6c 69 oked prior to li
e7e0: 62 72 61 72 79 20 69 6e 69 74 69 61 6c 69 7a 61 brary initializa
e7f0: 74 69 6f 6e 20 75 73 69 6e 67 0a 2a 2a 20 5b 73 tion using.** [s
e800: 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
e810: 65 28 29 5d 20 6f 72 20 61 66 74 65 72 20 73 68 e()] or after sh
e820: 75 74 64 6f 77 6e 20 62 79 20 5b 73 71 6c 69 74 utdown by [sqlit
e830: 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d 2e 0a e3_shutdown()]..
e840: 2a 2a 20 4e 6f 74 65 2c 20 68 6f 77 65 76 65 72 ** Note, however
e850: 2c 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f 63 , that sqlite3_c
e860: 6f 6e 66 69 67 28 29 20 63 61 6e 20 62 65 20 63 onfig() can be c
e870: 61 6c 6c 65 64 20 61 73 20 70 61 72 74 20 6f 66 alled as part of
e880: 20 74 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e the.** implemen
e890: 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20 61 70 70 tation of an app
e8a0: 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 lication-defined
e8b0: 20 5b 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 [sqlite3_os_ini
e8c0: 74 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 t()]..**.** The
e8d0: 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 first argument t
e8e0: 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 o sqlite3_config
e8f0: 28 29 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 () is an integer
e900: 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 .** [SQLITE_CONF
e910: 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 20 IG_SINGLETHREAD
e920: 7c 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 | configuration
e930: 6f 70 74 69 6f 6e 5d 20 74 68 61 74 20 64 65 74 option] that det
e940: 65 72 6d 69 6e 65 73 0a 2a 2a 20 77 68 61 74 20 ermines.** what
e950: 70 72 6f 70 65 72 74 79 20 6f 66 20 53 51 4c 69 property of SQLi
e960: 74 65 20 69 73 20 74 6f 20 62 65 20 63 6f 6e 66 te is to be conf
e970: 69 67 75 72 65 64 2e 20 20 53 75 62 73 65 71 75 igured. Subsequ
e980: 65 6e 74 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a ent arguments.**
e990: 20 76 61 72 79 20 64 65 70 65 6e 64 69 6e 67 20 vary depending
e9a0: 6f 6e 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 43 on the [SQLITE_C
e9b0: 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 ONFIG_SINGLETHRE
e9c0: 41 44 20 7c 20 63 6f 6e 66 69 67 75 72 61 74 69 AD | configurati
e9d0: 6f 6e 20 6f 70 74 69 6f 6e 5d 0a 2a 2a 20 69 6e on option].** in
e9e0: 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d the first argum
e9f0: 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 ent..**.** When
ea00: 61 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 a configuration
ea10: 6f 70 74 69 6f 6e 20 69 73 20 73 65 74 2c 20 73 option is set, s
ea20: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 qlite3_config()
ea30: 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f returns [SQLITE_
ea40: 4f 4b 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 20 6f OK]..** If the o
ea50: 70 74 69 6f 6e 20 69 73 20 75 6e 6b 6e 6f 77 6e ption is unknown
ea60: 20 6f 72 20 53 51 4c 69 74 65 20 69 73 20 75 6e or SQLite is un
ea70: 61 62 6c 65 20 74 6f 20 73 65 74 20 74 68 65 20 able to set the
ea80: 6f 70 74 69 6f 6e 0a 2a 2a 20 74 68 65 6e 20 74 option.** then t
ea90: 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 his routine retu
eaa0: 72 6e 73 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 5b rns a non-zero [
eab0: 65 72 72 6f 72 20 63 6f 64 65 5d 2e 0a 2a 2a 0a error code]..**.
eac0: 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
ead0: 0a 2a 2a 20 5b 48 31 34 31 30 33 5d 20 5b 48 31 .** [H14103] [H1
eae0: 34 31 30 36 5d 20 5b 48 31 34 31 32 30 5d 20 5b 4106] [H14120] [
eaf0: 48 31 34 31 32 33 5d 20 5b 48 31 34 31 32 36 5d H14123] [H14126]
eb00: 20 5b 48 31 34 31 32 39 5d 20 5b 48 31 34 31 33 [H14129] [H1413
eb10: 32 5d 20 5b 48 31 34 31 33 35 5d 0a 2a 2a 20 5b 2] [H14135].** [
eb20: 48 31 34 31 33 38 5d 20 5b 48 31 34 31 34 31 5d H14138] [H14141]
eb30: 20 5b 48 31 34 31 34 34 5d 20 5b 48 31 34 31 34 [H14144] [H1414
eb40: 37 5d 20 5b 48 31 34 31 35 30 5d 20 5b 48 31 34 7] [H14150] [H14
eb50: 31 35 33 5d 20 5b 48 31 34 31 35 36 5d 20 5b 48 153] [H14156] [H
eb60: 31 34 31 35 39 5d 0a 2a 2a 20 5b 48 31 34 31 36 14159].** [H1416
eb70: 32 5d 20 5b 48 31 34 31 36 35 5d 20 5b 48 31 34 2] [H14165] [H14
eb80: 31 36 38 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 168].*/.SQLITE_A
eb90: 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 PI SQLITE_EXPERI
eba0: 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 MENTAL int sqlit
ebb0: 65 33 5f 63 6f 6e 66 69 67 28 69 6e 74 2c 20 2e e3_config(int, .
ebc0: 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 ..);../*.** CAPI
ebd0: 33 52 45 46 3a 20 43 6f 6e 66 69 67 75 72 65 20 3REF: Configure
ebe0: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
ebf0: 69 6f 6e 73 20 20 7b 48 31 34 32 30 30 7d 20 3c ions {H14200} <
ec00: 53 32 30 30 30 30 3e 0a 2a 2a 20 45 58 50 45 52 S20000>.** EXPER
ec10: 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 IMENTAL.**.** Th
ec20: 65 20 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e e sqlite3_db_con
ec30: 66 69 67 28 29 20 69 6e 74 65 72 66 61 63 65 20 fig() interface
ec40: 69 73 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 is used to make
ec50: 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 0a 2a 2a configuration.**
ec60: 20 63 68 61 6e 67 65 73 20 74 6f 20 61 20 5b 64 changes to a [d
ec70: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
ec80: 6f 6e 5d 2e 20 20 54 68 65 20 69 6e 74 65 72 66 on]. The interf
ec90: 61 63 65 20 69 73 20 73 69 6d 69 6c 61 72 20 74 ace is similar t
eca0: 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f o.** [sqlite3_co
ecb0: 6e 66 69 67 28 29 5d 20 65 78 63 65 70 74 20 74 nfig()] except t
ecc0: 68 61 74 20 74 68 65 20 63 68 61 6e 67 65 73 20 hat the changes
ecd0: 61 70 70 6c 79 20 74 6f 20 61 20 73 69 6e 67 6c apply to a singl
ece0: 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 e.** [database c
ecf0: 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 73 70 65 63 onnection] (spec
ed00: 69 66 69 65 64 20 69 6e 20 74 68 65 20 66 69 72 ified in the fir
ed10: 73 74 20 61 72 67 75 6d 65 6e 74 29 2e 20 20 54 st argument). T
ed20: 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 64 62 he.** sqlite3_db
ed30: 5f 63 6f 6e 66 69 67 28 29 20 69 6e 74 65 72 66 _config() interf
ed40: 61 63 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 ace can only be
ed50: 75 73 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 used immediately
ed60: 20 61 66 74 65 72 0a 2a 2a 20 74 68 65 20 64 61 after.** the da
ed70: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
ed80: 6e 20 69 73 20 63 72 65 61 74 65 64 20 75 73 69 n is created usi
ed90: 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e ng [sqlite3_open
eda0: 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 ()],.** [sqlite3
edb0: 5f 6f 70 65 6e 31 36 28 29 5d 2c 20 6f 72 20 5b _open16()], or [
edc0: 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 sqlite3_open_v2(
edd0: 29 5d 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 )]. .**.** The
ede0: 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
edf0: 74 6f 20 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f to sqlite3_db_co
ee00: 6e 66 69 67 28 44 2c 56 2c 2e 2e 2e 29 20 20 69 nfig(D,V,...) i
ee10: 73 20 74 68 65 0a 2a 2a 20 63 6f 6e 66 69 67 75 s the.** configu
ee20: 72 61 74 69 6f 6e 20 76 65 72 62 20 2d 20 61 6e ration verb - an
ee30: 20 69 6e 74 65 67 65 72 20 63 6f 64 65 20 74 68 integer code th
ee40: 61 74 20 69 6e 64 69 63 61 74 65 73 20 77 68 61 at indicates wha
ee50: 74 0a 2a 2a 20 61 73 70 65 63 74 20 6f 66 20 74 t.** aspect of t
ee60: 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e he [database con
ee70: 6e 65 63 74 69 6f 6e 5d 20 69 73 20 62 65 69 6e nection] is bein
ee80: 67 20 63 6f 6e 66 69 67 75 72 65 64 2e 0a 2a 2a g configured..**
ee90: 20 54 68 65 20 6f 6e 6c 79 20 63 68 6f 69 63 65 The only choice
eea0: 20 66 6f 72 20 74 68 69 73 20 76 61 6c 75 65 20 for this value
eeb0: 69 73 20 5b 53 51 4c 49 54 45 5f 44 42 43 4f 4e is [SQLITE_DBCON
eec0: 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 5d 2e 0a FIG_LOOKASIDE]..
eed0: 2a 2a 20 4e 65 77 20 76 65 72 62 73 20 61 72 65 ** New verbs are
eee0: 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20 61 64 likely to be ad
eef0: 64 65 64 20 69 6e 20 66 75 74 75 72 65 20 72 65 ded in future re
ef00: 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 leases of SQLite
ef10: 2e 0a 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 ..** Additional
ef20: 61 72 67 75 6d 65 6e 74 73 20 64 65 70 65 6e 64 arguments depend
ef30: 20 6f 6e 20 74 68 65 20 76 65 72 62 2e 0a 2a 2a on the verb..**
ef40: 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
ef50: 3a 0a 2a 2a 20 5b 48 31 34 32 30 33 5d 20 5b 48 :.** [H14203] [H
ef60: 31 34 32 30 36 5d 20 5b 48 31 34 32 30 39 5d 20 14206] [H14209]
ef70: 5b 48 31 34 32 31 32 5d 20 5b 48 31 34 32 31 35 [H14212] [H14215
ef80: 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
ef90: 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e SQLITE_EXPERIMEN
efa0: 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f TAL int sqlite3_
efb0: 64 62 5f 63 6f 6e 66 69 67 28 73 71 6c 69 74 65 db_config(sqlite
efc0: 33 2a 2c 20 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29 3*, int op, ...)
efd0: 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
efe0: 46 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 F: Memory Alloca
eff0: 74 69 6f 6e 20 52 6f 75 74 69 6e 65 73 20 7b 48 tion Routines {H
f000: 31 30 31 35 35 7d 20 3c 53 32 30 31 32 30 3e 0a 10155} <S20120>.
f010: 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a ** EXPERIMENTAL.
f020: 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 **.** An instanc
f030: 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 e of this object
f040: 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 defines the int
f050: 65 72 66 61 63 65 20 62 65 74 77 65 65 6e 20 53 erface between S
f060: 51 4c 69 74 65 0a 2a 2a 20 61 6e 64 20 6c 6f 77 QLite.** and low
f070: 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c -level memory al
f080: 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 location routine
f090: 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 62 s..**.** This ob
f0a0: 6a 65 63 74 20 69 73 20 75 73 65 64 20 69 6e 20 ject is used in
f0b0: 6f 6e 6c 79 20 6f 6e 65 20 70 6c 61 63 65 20 69 only one place i
f0c0: 6e 20 74 68 65 20 53 51 4c 69 74 65 20 69 6e 74 n the SQLite int
f0d0: 65 72 66 61 63 65 2e 0a 2a 2a 20 41 20 70 6f 69 erface..** A poi
f0e0: 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 nter to an insta
f0f0: 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 nce of this obje
f100: 63 74 20 69 73 20 74 68 65 20 61 72 67 75 6d 65 ct is the argume
f110: 6e 74 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 nt to.** [sqlite
f120: 33 5f 63 6f 6e 66 69 67 28 29 5d 20 77 68 65 6e 3_config()] when
f130: 20 74 68 65 20 63 6f 6e 66 69 67 75 72 61 74 69 the configurati
f140: 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 0a 2a 2a 20 on option is.**
f150: 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d [SQLITE_CONFIG_M
f160: 41 4c 4c 4f 43 5d 20 6f 72 20 5b 53 51 4c 49 54 ALLOC] or [SQLIT
f170: 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c 4c E_CONFIG_GETMALL
f180: 4f 43 5d 2e 20 20 0a 2a 2a 20 42 79 20 63 72 65 OC]. .** By cre
f190: 61 74 69 6e 67 20 61 6e 20 69 6e 73 74 61 6e 63 ating an instanc
f1a0: 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 e of this object
f1b0: 0a 2a 2a 20 61 6e 64 20 70 61 73 73 69 6e 67 20 .** and passing
f1c0: 69 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 it to [sqlite3_c
f1d0: 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43 onfig]([SQLITE_C
f1e0: 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 5d 29 0a 2a ONFIG_MALLOC]).*
f1f0: 2a 20 64 75 72 69 6e 67 20 63 6f 6e 66 69 67 75 * during configu
f200: 72 61 74 69 6f 6e 2c 20 61 6e 20 61 70 70 6c 69 ration, an appli
f210: 63 61 74 69 6f 6e 20 63 61 6e 20 73 70 65 63 69 cation can speci
f220: 66 79 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 fy an alternativ
f230: 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f e.** memory allo
f240: 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d cation subsystem
f250: 20 66 6f 72 20 53 51 4c 69 74 65 20 74 6f 20 75 for SQLite to u
f260: 73 65 20 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74 se for all of it
f270: 73 0a 2a 2a 20 64 79 6e 61 6d 69 63 20 6d 65 6d s.** dynamic mem
f280: 6f 72 79 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a ory needs..**.**
f290: 20 4e 6f 74 65 20 74 68 61 74 20 53 51 4c 69 74 Note that SQLit
f2a0: 65 20 63 6f 6d 65 73 20 77 69 74 68 20 73 65 76 e comes with sev
f2b0: 65 72 61 6c 20 5b 62 75 69 6c 74 2d 69 6e 20 6d eral [built-in m
f2c0: 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 73 emory allocators
f2d0: 5d 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 70 65 ].** that are pe
f2e0: 72 66 65 63 74 6c 79 20 61 64 65 71 75 61 74 65 rfectly adequate
f2f0: 20 66 6f 72 20 74 68 65 20 6f 76 65 72 77 68 65 for the overwhe
f300: 6c 6d 69 6e 67 20 6d 61 6a 6f 72 69 74 79 20 6f lming majority o
f310: 66 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a f applications.*
f320: 2a 20 61 6e 64 20 74 68 61 74 20 74 68 69 73 20 * and that this
f330: 6f 62 6a 65 63 74 20 69 73 20 6f 6e 6c 79 20 75 object is only u
f340: 73 65 66 75 6c 20 74 6f 20 61 20 74 69 6e 79 20 seful to a tiny
f350: 6d 69 6e 6f 72 69 74 79 20 6f 66 20 61 70 70 6c minority of appl
f360: 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 77 69 74 68 ications.** with
f370: 20 73 70 65 63 69 61 6c 69 7a 65 64 20 6d 65 6d specialized mem
f380: 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 ory allocation r
f390: 65 71 75 69 72 65 6d 65 6e 74 73 2e 20 20 54 68 equirements. Th
f3a0: 69 73 20 6f 62 6a 65 63 74 20 69 73 0a 2a 2a 20 is object is.**
f3b0: 61 6c 73 6f 20 75 73 65 64 20 64 75 72 69 6e 67 also used during
f3c0: 20 74 65 73 74 69 6e 67 20 6f 66 20 53 51 4c 69 testing of SQLi
f3d0: 74 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 te in order to s
f3e0: 70 65 63 69 66 79 20 61 6e 20 61 6c 74 65 72 6e pecify an altern
f3f0: 61 74 69 76 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 ative.** memory
f400: 61 6c 6c 6f 63 61 74 6f 72 20 74 68 61 74 20 73 allocator that s
f410: 69 6d 75 6c 61 74 65 73 20 6d 65 6d 6f 72 79 20 imulates memory
f420: 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 6f out-of-memory co
f430: 6e 64 69 74 69 6f 6e 73 20 69 6e 0a 2a 2a 20 6f nditions in.** o
f440: 72 64 65 72 20 74 6f 20 76 65 72 69 66 79 20 74 rder to verify t
f450: 68 61 74 20 53 51 4c 69 74 65 20 72 65 63 6f 76 hat SQLite recov
f460: 65 72 73 20 67 72 61 63 65 66 75 6c 6c 79 20 66 ers gracefully f
f470: 72 6f 6d 20 73 75 63 68 0a 2a 2a 20 63 6f 6e 64 rom such.** cond
f480: 69 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 itions..**.** Th
f490: 65 20 78 4d 61 6c 6c 6f 63 20 61 6e 64 20 78 46 e xMalloc and xF
f4a0: 72 65 65 20 6d 65 74 68 6f 64 73 20 6d 75 73 74 ree methods must
f4b0: 20 77 6f 72 6b 20 6c 69 6b 65 20 74 68 65 0a 2a work like the.*
f4c0: 2a 20 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 66 * malloc() and f
f4d0: 72 65 65 28 29 20 66 75 6e 63 74 69 6f 6e 73 20 ree() functions
f4e0: 66 72 6f 6d 20 74 68 65 20 73 74 61 6e 64 61 72 from the standar
f4f0: 64 20 43 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 20 d C library..**
f500: 54 68 65 20 78 52 65 61 6c 6c 6f 63 20 6d 65 74 The xRealloc met
f510: 68 6f 64 20 6d 75 73 74 20 77 6f 72 6b 20 6c 69 hod must work li
f520: 6b 65 20 72 65 61 6c 6c 6f 63 28 29 20 66 72 6f ke realloc() fro
f530: 6d 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 43 m the standard C
f540: 20 6c 69 62 72 61 72 79 0a 2a 2a 20 77 69 74 68 library.** with
f550: 20 74 68 65 20 65 78 63 65 70 74 69 6f 6e 20 74 the exception t
f560: 68 61 74 20 69 66 20 74 68 65 20 73 65 63 6f 6e hat if the secon
f570: 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 52 d argument to xR
f580: 65 61 6c 6c 6f 63 20 69 73 20 7a 65 72 6f 2c 0a ealloc is zero,.
f590: 2a 2a 20 78 52 65 61 6c 6c 6f 63 20 6d 75 73 74 ** xRealloc must
f5a0: 20 62 65 20 61 20 6e 6f 2d 6f 70 20 2d 20 69 74 be a no-op - it
f5b0: 20 6d 75 73 74 20 6e 6f 74 20 70 65 72 66 6f 72 must not perfor
f5c0: 6d 20 61 6e 79 20 61 6c 6c 6f 63 61 74 69 6f 6e m any allocation
f5d0: 20 6f 72 0a 2a 2a 20 64 65 61 6c 6c 6f 63 61 74 or.** deallocat
f5e0: 69 6f 6e 2e 20 20 53 51 4c 69 74 65 20 67 75 61 ion. SQLite gua
f5f0: 72 61 6e 74 65 65 64 73 20 74 68 61 74 20 74 68 ranteeds that th
f600: 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
f610: 74 20 74 6f 0a 2a 2a 20 78 52 65 61 6c 6c 6f 63 t to.** xRealloc
f620: 20 69 73 20 61 6c 77 61 79 73 20 61 20 76 61 6c is always a val
f630: 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 61 ue returned by a
f640: 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 78 prior call to x
f650: 52 6f 75 6e 64 75 70 2e 0a 2a 2a 20 41 6e 64 20 Roundup..** And
f660: 73 6f 20 69 6e 20 63 61 73 65 73 20 77 68 65 72 so in cases wher
f670: 65 20 78 52 6f 75 6e 64 75 70 20 61 6c 77 61 79 e xRoundup alway
f680: 73 20 72 65 74 75 72 6e 73 20 61 20 70 6f 73 69 s returns a posi
f690: 74 69 76 65 20 6e 75 6d 62 65 72 2c 0a 2a 2a 20 tive number,.**
f6a0: 78 52 65 61 6c 6c 6f 63 20 63 61 6e 20 70 65 72 xRealloc can per
f6b0: 66 6f 72 6d 20 65 78 61 63 74 6c 79 20 61 73 20 form exactly as
f6c0: 74 68 65 20 73 74 61 6e 64 61 72 64 20 6c 69 62 the standard lib
f6d0: 72 61 72 79 20 72 65 61 6c 6c 6f 63 28 29 20 61 rary realloc() a
f6e0: 6e 64 0a 2a 2a 20 73 74 69 6c 6c 20 62 65 20 69 nd.** still be i
f6f0: 6e 20 63 6f 6d 70 6c 69 61 6e 63 65 20 77 69 74 n compliance wit
f700: 68 20 74 68 69 73 20 73 70 65 63 69 66 69 63 61 h this specifica
f710: 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 78 53 69 7a tion..**.** xSiz
f720: 65 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 e should return
f730: 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 69 the allocated si
f740: 7a 65 20 6f 66 20 61 20 6d 65 6d 6f 72 79 20 61 ze of a memory a
f750: 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 70 72 65 llocation.** pre
f760: 76 69 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65 64 viously obtained
f770: 20 66 72 6f 6d 20 78 4d 61 6c 6c 6f 63 20 6f 72 from xMalloc or
f780: 20 78 52 65 61 6c 6c 6f 63 2e 20 20 54 68 65 20 xRealloc. The
f790: 61 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 0a 2a allocated size.*
f7a0: 2a 20 69 73 20 61 6c 77 61 79 73 20 61 74 20 6c * is always at l
f7b0: 65 61 73 74 20 61 73 20 62 69 67 20 61 73 20 74 east as big as t
f7c0: 68 65 20 72 65 71 75 65 73 74 65 64 20 73 69 7a he requested siz
f7d0: 65 20 62 75 74 20 6d 61 79 20 62 65 20 6c 61 72 e but may be lar
f7e0: 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 ger..**.** The x
f7f0: 52 6f 75 6e 64 75 70 20 6d 65 74 68 6f 64 20 72 Roundup method r
f800: 65 74 75 72 6e 73 20 77 68 61 74 20 77 6f 75 6c eturns what woul
f810: 64 20 62 65 20 74 68 65 20 61 6c 6c 6f 63 61 74 d be the allocat
f820: 65 64 20 73 69 7a 65 20 6f 66 0a 2a 2a 20 61 20 ed size of.** a
f830: 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
f840: 6e 20 67 69 76 65 6e 20 61 20 70 61 72 74 69 63 n given a partic
f850: 75 6c 61 72 20 72 65 71 75 65 73 74 65 64 20 73 ular requested s
f860: 69 7a 65 2e 20 20 4d 6f 73 74 20 6d 65 6d 6f 72 ize. Most memor
f870: 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 6f 72 73 20 y.** allocators
f880: 72 6f 75 6e 64 20 75 70 20 6d 65 6d 6f 72 79 20 round up memory
f890: 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 74 20 6c allocations at l
f8a0: 65 61 73 74 20 74 6f 20 74 68 65 20 6e 65 78 74 east to the next
f8b0: 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 6f 66 20 multiple.** of
f8c0: 38 2e 20 20 53 6f 6d 65 20 61 6c 6c 6f 63 61 74 8. Some allocat
f8d0: 6f 72 73 20 72 6f 75 6e 64 20 75 70 20 74 6f 20 ors round up to
f8e0: 61 20 6c 61 72 67 65 72 20 6d 75 6c 74 69 70 6c a larger multipl
f8f0: 65 20 6f 72 20 74 6f 20 61 20 70 6f 77 65 72 20 e or to a power
f900: 6f 66 20 32 2e 0a 2a 2a 20 45 76 65 72 79 20 6d of 2..** Every m
f910: 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
f920: 20 72 65 71 75 65 73 74 20 63 6f 6d 69 6e 67 20 request coming
f930: 69 6e 20 74 68 72 6f 75 67 68 20 5b 73 71 6c 69 in through [sqli
f940: 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 0a 2a 2a te3_malloc()].**
f950: 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 or [sqlite3_rea
f960: 6c 6c 6f 63 28 29 5d 20 66 69 72 73 74 20 63 61 lloc()] first ca
f970: 6c 6c 73 20 78 52 6f 75 6e 64 75 70 2e 20 20 49 lls xRoundup. I
f980: 66 20 78 52 6f 75 6e 64 75 70 20 72 65 74 75 72 f xRoundup retur
f990: 6e 73 20 30 2c 20 0a 2a 2a 20 74 68 61 74 20 63 ns 0, .** that c
f9a0: 61 75 73 65 73 20 74 68 65 20 63 6f 72 72 65 73 auses the corres
f9b0: 70 6f 6e 64 69 6e 67 20 6d 65 6d 6f 72 79 20 61 ponding memory a
f9c0: 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 66 61 69 llocation to fai
f9d0: 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 49 6e l..**.** The xIn
f9e0: 69 74 20 6d 65 74 68 6f 64 20 69 6e 69 74 69 61 it method initia
f9f0: 6c 69 7a 65 73 20 74 68 65 20 6d 65 6d 6f 72 79 lizes the memory
fa00: 20 61 6c 6c 6f 63 61 74 6f 72 2e 20 20 28 46 6f allocator. (Fo
fa10: 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 69 74 r example,.** it
fa20: 20 6d 69 67 68 74 20 61 6c 6c 6f 63 61 74 65 20 might allocate
fa30: 61 6e 79 20 72 65 71 75 69 72 65 20 6d 75 74 65 any require mute
fa40: 78 65 73 20 6f 72 20 69 6e 69 74 69 61 6c 69 7a xes or initializ
fa50: 65 20 69 6e 74 65 72 6e 61 6c 20 64 61 74 61 0a e internal data.
fa60: 2a 2a 20 73 74 72 75 63 74 75 72 65 73 2e 20 20 ** structures.
fa70: 54 68 65 20 78 53 68 75 74 64 6f 77 6e 20 6d 65 The xShutdown me
fa80: 74 68 6f 64 20 69 73 20 69 6e 76 6f 6b 65 64 20 thod is invoked
fa90: 28 69 6e 64 69 72 65 63 74 6c 79 29 20 62 79 0a (indirectly) by.
faa0: 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 ** [sqlite3_shut
fab0: 64 6f 77 6e 28 29 5d 20 61 6e 64 20 73 68 6f 75 down()] and shou
fac0: 6c 64 20 64 65 61 6c 6c 6f 63 61 74 65 20 61 6e ld deallocate an
fad0: 79 20 72 65 73 6f 75 72 63 65 73 20 61 63 71 75 y resources acqu
fae0: 69 72 65 64 0a 2a 2a 20 62 79 20 78 49 6e 69 74 ired.** by xInit
faf0: 2e 20 20 54 68 65 20 70 41 70 70 44 61 74 61 20 . The pAppData
fb00: 70 6f 69 6e 74 65 72 20 69 73 20 75 73 65 64 20 pointer is used
fb10: 61 73 20 74 68 65 20 6f 6e 6c 79 20 70 61 72 61 as the only para
fb20: 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 78 49 6e 69 meter to.** xIni
fb30: 74 20 61 6e 64 20 78 53 68 75 74 64 6f 77 6e 2e t and xShutdown.
fb40: 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 68 6f .**.** SQLite ho
fb50: 6c 64 73 20 74 68 65 20 5b 53 51 4c 49 54 45 5f lds the [SQLITE_
fb60: 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 MUTEX_STATIC_MAS
fb70: 54 45 52 5d 20 6d 75 74 65 78 20 77 68 65 6e 20 TER] mutex when
fb80: 69 74 20 69 6e 76 6f 6b 65 73 0a 2a 2a 20 74 68 it invokes.** th
fb90: 65 20 78 49 6e 69 74 20 6d 65 74 68 6f 64 2c 20 e xInit method,
fba0: 73 6f 20 74 68 65 20 78 49 6e 69 74 20 6d 65 74 so the xInit met
fbb0: 68 6f 64 20 6e 65 65 64 20 6e 6f 74 20 62 65 20 hod need not be
fbc0: 74 68 72 65 61 64 73 61 66 65 2e 20 20 54 68 65 threadsafe. The
fbd0: 0a 2a 2a 20 78 53 68 75 74 64 6f 77 6e 20 6d 65 .** xShutdown me
fbe0: 74 68 6f 64 20 69 73 20 6f 6e 6c 79 20 63 61 6c thod is only cal
fbf0: 6c 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 led from [sqlite
fc00: 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d 20 73 6f 3_shutdown()] so
fc10: 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 it does.** not
fc20: 6e 65 65 64 20 74 6f 20 62 65 20 74 68 72 65 61 need to be threa
fc30: 64 73 61 66 65 20 65 69 74 68 65 72 2e 20 20 46 dsafe either. F
fc40: 6f 72 20 61 6c 6c 20 6f 74 68 65 72 20 6d 65 74 or all other met
fc50: 68 6f 64 73 2c 20 53 51 4c 69 74 65 0a 2a 2a 20 hods, SQLite.**
fc60: 68 6f 6c 64 73 20 74 68 65 20 5b 53 51 4c 49 54 holds the [SQLIT
fc70: 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d E_MUTEX_STATIC_M
fc80: 45 4d 5d 20 6d 75 74 65 78 20 61 73 20 6c 6f 6e EM] mutex as lon
fc90: 67 20 61 73 20 74 68 65 0a 2a 2a 20 5b 53 51 4c g as the.** [SQL
fca0: 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 ITE_CONFIG_MEMST
fcb0: 41 54 55 53 5d 20 63 6f 6e 66 69 67 75 72 61 74 ATUS] configurat
fcc0: 69 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 20 74 75 ion option is tu
fcd0: 72 6e 65 64 20 6f 6e 20 28 77 68 69 63 68 0a 2a rned on (which.*
fce0: 2a 20 69 74 20 69 73 20 62 79 20 64 65 66 61 75 * it is by defau
fcf0: 6c 74 29 20 61 6e 64 20 73 6f 20 74 68 65 20 6d lt) and so the m
fd00: 65 74 68 6f 64 73 20 61 72 65 20 61 75 74 6f 6d ethods are autom
fd10: 61 74 69 63 61 6c 6c 79 20 73 65 72 69 61 6c 69 atically seriali
fd20: 7a 65 64 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c zed..** However,
fd30: 20 69 66 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 if [SQLITE_CONF
fd40: 49 47 5f 4d 45 4d 53 54 41 54 55 53 5d 20 69 73 IG_MEMSTATUS] is
fd50: 20 64 69 73 61 62 6c 65 64 2c 20 74 68 65 6e 20 disabled, then
fd60: 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20 6d 65 74 the other.** met
fd70: 68 6f 64 73 20 6d 75 73 74 20 62 65 20 74 68 72 hods must be thr
fd80: 65 61 64 73 61 66 65 20 6f 72 20 65 6c 73 65 20 eadsafe or else
fd90: 6d 61 6b 65 20 74 68 65 69 72 20 6f 77 6e 20 61 make their own a
fda0: 72 72 61 6e 67 65 6d 65 6e 74 73 20 66 6f 72 0a rrangements for.
fdb0: 2a 2a 20 73 65 72 69 61 6c 69 7a 61 74 69 6f 6e ** serialization
fdc0: 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 ..**.** SQLite w
fdd0: 69 6c 6c 20 6e 65 76 65 72 20 69 6e 76 6f 6b 65 ill never invoke
fde0: 20 78 49 6e 69 74 28 29 20 6d 6f 72 65 20 74 68 xInit() more th
fdf0: 61 6e 20 6f 6e 63 65 20 77 69 74 68 6f 75 74 20 an once without
fe00: 61 6e 20 69 6e 74 65 72 76 65 6e 69 6e 67 0a 2a an intervening.*
fe10: 2a 20 63 61 6c 6c 20 74 6f 20 78 53 68 75 74 64 * call to xShutd
fe20: 6f 77 6e 28 29 2e 0a 2a 2f 0a 74 79 70 65 64 65 own()..*/.typede
fe30: 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 f struct sqlite3
fe40: 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 73 71 6c _mem_methods sql
fe50: 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 ite3_mem_methods
fe60: 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 ;.struct sqlite3
fe70: 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 7b 0a 20 _mem_methods {.
fe80: 20 76 6f 69 64 20 2a 28 2a 78 4d 61 6c 6c 6f 63 void *(*xMalloc
fe90: 29 28 69 6e 74 29 3b 20 20 20 20 20 20 20 20 20 )(int);
fea0: 2f 2a 20 4d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 /* Memory alloca
feb0: 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f tion function */
fec0: 0a 20 20 76 6f 69 64 20 28 2a 78 46 72 65 65 29 . void (*xFree)
fed0: 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20 20 20 (void*);
fee0: 20 20 2f 2a 20 46 72 65 65 20 61 20 70 72 69 6f /* Free a prio
fef0: 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a r allocation */.
ff00: 20 20 76 6f 69 64 20 2a 28 2a 78 52 65 61 6c 6c void *(*xReall
ff10: 6f 63 29 28 76 6f 69 64 2a 2c 69 6e 74 29 3b 20 oc)(void*,int);
ff20: 20 2f 2a 20 52 65 73 69 7a 65 20 61 6e 20 61 6c /* Resize an al
ff30: 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e location */. in
ff40: 74 20 28 2a 78 53 69 7a 65 29 28 76 6f 69 64 2a t (*xSize)(void*
ff50: 29 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ); /*
ff60: 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 Return the size
ff70: 6f 66 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e of an allocation
ff80: 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 52 6f 75 */. int (*xRou
ff90: 6e 64 75 70 29 28 69 6e 74 29 3b 20 20 20 20 20 ndup)(int);
ffa0: 20 20 20 20 20 2f 2a 20 52 6f 75 6e 64 20 75 70 /* Round up
ffb0: 20 72 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f request size to
ffc0: 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 allocation size
ffd0: 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 49 6e 69 */. int (*xIni
ffe0: 74 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20 t)(void*);
fff0: 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 /* Initiali
10000 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c ze the memory al
10010 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20 20 76 6f 69 locator */. voi
10020 64 20 28 2a 78 53 68 75 74 64 6f 77 6e 29 28 76 d (*xShutdown)(v
10030 6f 69 64 2a 29 3b 20 20 20 20 20 20 2f 2a 20 44 oid*); /* D
10040 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 einitialize the
10050 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 memory allocator
10060 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 70 70 */. void *pApp
10070 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 Data;
10080 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 /* Argument
10090 20 74 6f 20 78 49 6e 69 74 28 29 20 61 6e 64 20 to xInit() and
100a0 78 53 68 75 74 64 6f 77 6e 28 29 20 2a 2f 0a 7d xShutdown() */.}
100b0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
100c0 46 3a 20 43 6f 6e 66 69 67 75 72 61 74 69 6f 6e F: Configuration
100d0 20 4f 70 74 69 6f 6e 73 20 7b 48 31 30 31 36 30 Options {H10160
100e0 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 20 45 58 } <S20000>.** EX
100f0 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a PERIMENTAL.**.**
10100 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 These constants
10110 20 61 72 65 20 74 68 65 20 61 76 61 69 6c 61 62 are the availab
10120 6c 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 66 69 le integer confi
10130 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 guration options
10140 20 74 68 61 74 0a 2a 2a 20 63 61 6e 20 62 65 20 that.** can be
10150 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69 passed as the fi
10160 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 rst argument to
10170 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e the [sqlite3_con
10180 66 69 67 28 29 5d 20 69 6e 74 65 72 66 61 63 65 fig()] interface
10190 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 63 6f 6e 66 ..**.** New conf
101a0 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e iguration option
101b0 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 s may be added i
101c0 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 n future release
101d0 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 s of SQLite..**
101e0 45 78 69 73 74 69 6e 67 20 63 6f 6e 66 69 67 75 Existing configu
101f0 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 6d ration options m
10200 69 67 68 74 20 62 65 20 64 69 73 63 6f 6e 74 69 ight be disconti
10210 6e 75 65 64 2e 20 20 41 70 70 6c 69 63 61 74 69 nued. Applicati
10220 6f 6e 73 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 68 ons.** should ch
10230 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20 63 eck the return c
10240 6f 64 65 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 ode from [sqlite
10250 33 5f 63 6f 6e 66 69 67 28 29 5d 20 74 6f 20 6d 3_config()] to m
10260 61 6b 65 20 73 75 72 65 20 74 68 61 74 0a 2a 2a ake sure that.**
10270 20 74 68 65 20 63 61 6c 6c 20 77 6f 72 6b 65 64 the call worked
10280 2e 20 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f . The [sqlite3_
10290 63 6f 6e 66 69 67 28 29 5d 20 69 6e 74 65 72 66 config()] interf
102a0 61 63 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 ace will return
102b0 61 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 5b 65 a.** non-zero [e
102c0 72 72 6f 72 20 63 6f 64 65 5d 20 69 66 20 61 20 rror code] if a
102d0 64 69 73 63 6f 6e 74 69 6e 75 65 64 20 6f 72 20 discontinued or
102e0 75 6e 73 75 70 70 6f 72 74 65 64 20 63 6f 6e 66 unsupported conf
102f0 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e iguration option
10300 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a .** is invoked..
10310 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 **.** <dl>.** <d
10320 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f t>SQLITE_CONFIG_
10330 53 49 4e 47 4c 45 54 48 52 45 41 44 3c 2f 64 74 SINGLETHREAD</dt
10340 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 72 65 20 61 >.** <dd>There a
10350 72 65 20 6e 6f 20 61 72 67 75 6d 65 6e 74 73 20 re no arguments
10360 74 6f 20 74 68 69 73 20 6f 70 74 69 6f 6e 2e 20 to this option.
10370 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 64 69 73 This option dis
10380 61 62 6c 65 73 0a 2a 2a 20 61 6c 6c 20 6d 75 74 ables.** all mut
10390 65 78 69 6e 67 20 61 6e 64 20 70 75 74 73 20 53 exing and puts S
103a0 51 4c 69 74 65 20 69 6e 74 6f 20 61 20 6d 6f 64 QLite into a mod
103b0 65 20 77 68 65 72 65 20 69 74 20 63 61 6e 20 6f e where it can o
103c0 6e 6c 79 20 62 65 20 75 73 65 64 0a 2a 2a 20 62 nly be used.** b
103d0 79 20 61 20 73 69 6e 67 6c 65 20 74 68 72 65 61 y a single threa
103e0 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 d.</dd>.**.** <d
103f0 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f t>SQLITE_CONFIG_
10400 4d 55 4c 54 49 54 48 52 45 41 44 3c 2f 64 74 3e MULTITHREAD</dt>
10410 0a 2a 2a 20 3c 64 64 3e 54 68 65 72 65 20 61 72 .** <dd>There ar
10420 65 20 6e 6f 20 61 72 67 75 6d 65 6e 74 73 20 74 e no arguments t
10430 6f 20 74 68 69 73 20 6f 70 74 69 6f 6e 2e 20 20 o this option.
10440 54 68 69 73 20 6f 70 74 69 6f 6e 20 64 69 73 61 This option disa
10450 62 6c 65 73 0a 2a 2a 20 6d 75 74 65 78 69 6e 67 bles.** mutexing
10460 20 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f on [database co
10470 6e 6e 65 63 74 69 6f 6e 5d 20 61 6e 64 20 5b 70 nnection] and [p
10480 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
10490 74 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 54 t] objects..** T
104a0 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 69 he application i
104b0 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f s responsible fo
104c0 72 20 73 65 72 69 61 6c 69 7a 69 6e 67 20 61 63 r serializing ac
104d0 63 65 73 73 20 74 6f 0a 2a 2a 20 5b 64 61 74 61 cess to.** [data
104e0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 base connections
104f0 5d 20 61 6e 64 20 5b 70 72 65 70 61 72 65 64 20 ] and [prepared
10500 73 74 61 74 65 6d 65 6e 74 73 5d 2e 20 20 42 75 statements]. Bu
10510 74 20 6f 74 68 65 72 20 6d 75 74 65 78 65 73 0a t other mutexes.
10520 2a 2a 20 61 72 65 20 65 6e 61 62 6c 65 64 20 73 ** are enabled s
10530 6f 20 74 68 61 74 20 53 51 4c 69 74 65 20 77 69 o that SQLite wi
10540 6c 6c 20 62 65 20 73 61 66 65 20 74 6f 20 75 73 ll be safe to us
10550 65 20 69 6e 20 61 20 6d 75 6c 74 69 2d 74 68 72 e in a multi-thr
10560 65 61 64 65 64 0a 2a 2a 20 65 6e 76 69 72 6f 6e eaded.** environ
10570 6d 65 6e 74 20 61 73 20 6c 6f 6e 67 20 61 73 20 ment as long as
10580 6e 6f 20 74 77 6f 20 74 68 72 65 61 64 73 20 61 no two threads a
10590 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20 74 68 ttempt to use th
105a0 65 20 73 61 6d 65 0a 2a 2a 20 5b 64 61 74 61 62 e same.** [datab
105b0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
105c0 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 at the same time
105d0 2e 20 20 53 65 65 20 74 68 65 20 5b 74 68 72 65 . See the [thre
105e0 61 64 69 6e 67 20 6d 6f 64 65 5d 0a 2a 2a 20 64 ading mode].** d
105f0 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 ocumentation for
10600 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f additional info
10610 72 6d 61 74 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a rmation.</dd>.**
10620 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 .** <dt>SQLITE_C
10630 4f 4e 46 49 47 5f 53 45 52 49 41 4c 49 5a 45 44 ONFIG_SERIALIZED
10640 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 </dt>.** <dd>The
10650 72 65 20 61 72 65 20 6e 6f 20 61 72 67 75 6d 65 re are no argume
10660 6e 74 73 20 74 6f 20 74 68 69 73 20 6f 70 74 69 nts to this opti
10670 6f 6e 2e 20 20 54 68 69 73 20 6f 70 74 69 6f 6e on. This option
10680 20 65 6e 61 62 6c 65 73 0a 2a 2a 20 61 6c 6c 20 enables.** all
10690 6d 75 74 65 78 65 73 20 69 6e 63 6c 75 64 69 6e mutexes includin
106a0 67 20 74 68 65 20 72 65 63 75 72 73 69 76 65 0a g the recursive.
106b0 2a 2a 20 6d 75 74 65 78 65 73 20 6f 6e 20 5b 64 ** mutexes on [d
106c0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
106d0 6f 6e 5d 20 61 6e 64 20 5b 70 72 65 70 61 72 65 on] and [prepare
106e0 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 6f 62 6a d statement] obj
106f0 65 63 74 73 2e 0a 2a 2a 20 49 6e 20 74 68 69 73 ects..** In this
10700 20 6d 6f 64 65 20 28 77 68 69 63 68 20 69 73 20 mode (which is
10710 74 68 65 20 64 65 66 61 75 6c 74 20 77 68 65 6e the default when
10720 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 SQLite is compi
10730 6c 65 64 20 77 69 74 68 0a 2a 2a 20 5b 53 51 4c led with.** [SQL
10740 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 31 ITE_THREADSAFE=1
10750 5d 29 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 ]) the SQLite li
10760 62 72 61 72 79 20 77 69 6c 6c 20 69 74 73 65 6c brary will itsel
10770 66 20 73 65 72 69 61 6c 69 7a 65 20 61 63 63 65 f serialize acce
10780 73 73 0a 2a 2a 20 74 6f 20 5b 64 61 74 61 62 61 ss.** to [databa
10790 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d 20 se connections]
107a0 61 6e 64 20 5b 70 72 65 70 61 72 65 64 20 73 74 and [prepared st
107b0 61 74 65 6d 65 6e 74 73 5d 20 73 6f 20 74 68 61 atements] so tha
107c0 74 20 74 68 65 0a 2a 2a 20 61 70 70 6c 69 63 61 t the.** applica
107d0 74 69 6f 6e 20 69 73 20 66 72 65 65 20 74 6f 20 tion is free to
107e0 75 73 65 20 74 68 65 20 73 61 6d 65 20 5b 64 61 use the same [da
107f0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
10800 6e 5d 20 6f 72 20 74 68 65 0a 2a 2a 20 73 61 6d n] or the.** sam
10810 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 e [prepared stat
10820 65 6d 65 6e 74 5d 20 69 6e 20 64 69 66 66 65 72 ement] in differ
10830 65 6e 74 20 74 68 72 65 61 64 73 20 61 74 20 74 ent threads at t
10840 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a 2a 2a he same time..**
10850 20 53 65 65 20 74 68 65 20 5b 74 68 72 65 61 64 See the [thread
10860 69 6e 67 20 6d 6f 64 65 5d 20 64 6f 63 75 6d 65 ing mode] docume
10870 6e 74 61 74 69 6f 6e 20 66 6f 72 20 61 64 64 69 ntation for addi
10880 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 tional informati
10890 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c on.</dd>.**.** <
108a0 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 dt>SQLITE_CONFIG
108b0 5f 4d 41 4c 4c 4f 43 3c 2f 64 74 3e 0a 2a 2a 20 _MALLOC</dt>.**
108c0 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 <dd>This option
108d0 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61 takes a single a
108e0 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73 rgument which is
108f0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e a pointer to an
10900 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 .** instance of
10910 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d the [sqlite3_mem
10920 5f 6d 65 74 68 6f 64 73 5d 20 73 74 72 75 63 74 _methods] struct
10930 75 72 65 2e 20 20 54 68 65 20 61 72 67 75 6d 65 ure. The argume
10940 6e 74 20 73 70 65 63 69 66 69 65 73 0a 2a 2a 20 nt specifies.**
10950 61 6c 74 65 72 6e 61 74 69 76 65 20 6c 6f 77 2d alternative low-
10960 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c level memory all
10970 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 ocation routines
10980 20 74 6f 20 62 65 20 75 73 65 64 20 69 6e 20 70 to be used in p
10990 6c 61 63 65 20 6f 66 0a 2a 2a 20 74 68 65 20 6d lace of.** the m
109a0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
109b0 20 72 6f 75 74 69 6e 65 73 20 62 75 69 6c 74 20 routines built
109c0 69 6e 74 6f 20 53 51 4c 69 74 65 2e 3c 2f 64 64 into SQLite.</dd
109d0 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 >.**.** <dt>SQLI
109e0 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c TE_CONFIG_GETMAL
109f0 4c 4f 43 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e LOC</dt>.** <dd>
10a00 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 This option take
10a10 73 20 61 20 73 69 6e 67 6c 65 20 61 72 67 75 6d s a single argum
10a20 65 6e 74 20 77 68 69 63 68 20 69 73 20 61 20 70 ent which is a p
10a30 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 ointer to an.**
10a40 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
10a50 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 [sqlite3_mem_met
10a60 68 6f 64 73 5d 20 73 74 72 75 63 74 75 72 65 2e hods] structure.
10a70 20 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d The [sqlite3_m
10a80 65 6d 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 73 em_methods].** s
10a90 74 72 75 63 74 75 72 65 20 69 73 20 66 69 6c 6c tructure is fill
10aa0 65 64 20 77 69 74 68 20 74 68 65 20 63 75 72 72 ed with the curr
10ab0 65 6e 74 6c 79 20 64 65 66 69 6e 65 64 20 6d 65 ently defined me
10ac0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
10ad0 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 20 54 68 69 routines..** Thi
10ae0 73 20 6f 70 74 69 6f 6e 20 63 61 6e 20 62 65 20 s option can be
10af0 75 73 65 64 20 74 6f 20 6f 76 65 72 6c 6f 61 64 used to overload
10b00 20 74 68 65 20 64 65 66 61 75 6c 74 20 6d 65 6d the default mem
10b10 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a ory allocation.*
10b20 2a 20 72 6f 75 74 69 6e 65 73 20 77 69 74 68 20 * routines with
10b30 61 20 77 72 61 70 70 65 72 20 74 68 61 74 20 73 a wrapper that s
10b40 69 6d 75 6c 61 74 69 6f 6e 73 20 6d 65 6d 6f 72 imulations memor
10b50 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 y allocation fai
10b60 6c 75 72 65 20 6f 72 0a 2a 2a 20 74 72 61 63 6b lure or.** track
10b70 73 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 2c 20 s memory usage,
10b80 66 6f 72 20 65 78 61 6d 70 6c 65 2e 3c 2f 64 64 for example.</dd
10b90 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 >.**.** <dt>SQLI
10ba0 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 TE_CONFIG_MEMSTA
10bb0 54 55 53 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e TUS</dt>.** <dd>
10bc0 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 This option take
10bd0 73 20 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e s single argumen
10be0 74 20 6f 66 20 74 79 70 65 20 69 6e 74 2c 20 69 t of type int, i
10bf0 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 61 20 nterpreted as a
10c00 0a 2a 2a 20 62 6f 6f 6c 65 61 6e 2c 20 77 68 69 .** boolean, whi
10c10 63 68 20 65 6e 61 62 6c 65 73 20 6f 72 20 64 69 ch enables or di
10c20 73 61 62 6c 65 73 20 74 68 65 20 63 6f 6c 6c 65 sables the colle
10c30 63 74 69 6f 6e 20 6f 66 20 6d 65 6d 6f 72 79 20 ction of memory
10c40 61 6c 6c 6f 63 61 74 69 6f 6e 20 0a 2a 2a 20 73 allocation .** s
10c50 74 61 74 69 73 74 69 63 73 2e 20 57 68 65 6e 20 tatistics. When
10c60 64 69 73 61 62 6c 65 64 2c 20 74 68 65 20 66 6f disabled, the fo
10c70 6c 6c 6f 77 69 6e 67 20 53 51 4c 69 74 65 20 69 llowing SQLite i
10c80 6e 74 65 72 66 61 63 65 73 20 62 65 63 6f 6d 65 nterfaces become
10c90 20 0a 2a 2a 20 6e 6f 6e 2d 6f 70 65 72 61 74 69 .** non-operati
10ca0 6f 6e 61 6c 3a 0a 2a 2a 20 20 20 3c 75 6c 3e 0a onal:.** <ul>.
10cb0 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73 71 6c 69 74 ** <li> [sqlit
10cc0 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 29 e3_memory_used()
10cd0 5d 0a 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73 71 6c ].** <li> [sql
10ce0 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 ite3_memory_high
10cf0 77 61 74 65 72 28 29 5d 0a 2a 2a 20 20 20 3c 6c water()].** <l
10d00 69 3e 20 5b 73 71 6c 69 74 65 33 5f 73 6f 66 74 i> [sqlite3_soft
10d10 5f 68 65 61 70 5f 6c 69 6d 69 74 28 29 5d 0a 2a _heap_limit()].*
10d20 2a 20 20 20 3c 6c 69 3e 20 5b 73 71 6c 69 74 65 * <li> [sqlite
10d30 33 5f 73 74 61 74 75 73 28 29 5d 0a 2a 2a 20 20 3_status()].**
10d40 20 3c 2f 75 6c 3e 0a 2a 2a 20 3c 2f 64 64 3e 0a </ul>.** </dd>.
10d50 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 **.** <dt>SQLITE
10d60 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 3c _CONFIG_SCRATCH<
10d70 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 /dt>.** <dd>This
10d80 20 6f 70 74 69 6f 6e 20 73 70 65 63 69 66 69 65 option specifie
10d90 73 20 61 20 73 74 61 74 69 63 20 6d 65 6d 6f 72 s a static memor
10da0 79 20 62 75 66 66 65 72 20 74 68 61 74 20 53 51 y buffer that SQ
10db0 4c 69 74 65 20 63 61 6e 20 75 73 65 20 66 6f 72 Lite can use for
10dc0 0a 2a 2a 20 73 63 72 61 74 63 68 20 6d 65 6d 6f .** scratch memo
10dd0 72 79 2e 20 20 54 68 65 72 65 20 61 72 65 20 74 ry. There are t
10de0 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 3a 20 hree arguments:
10df0 20 41 20 70 6f 69 6e 74 65 72 20 61 6e 20 38 2d A pointer an 8-
10e00 62 79 74 65 0a 2a 2a 20 61 6c 69 67 6e 65 64 20 byte.** aligned
10e10 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20 66 72 memory buffer fr
10e20 6f 6d 20 77 68 69 63 68 20 74 68 65 20 73 63 72 om which the scr
10e30 61 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 ach allocations
10e40 77 69 6c 6c 20 62 65 0a 2a 2a 20 64 72 61 77 6e will be.** drawn
10e50 2c 20 74 68 65 20 73 69 7a 65 20 6f 66 20 65 61 , the size of ea
10e60 63 68 20 73 63 72 61 74 63 68 20 61 6c 6c 6f 63 ch scratch alloc
10e70 61 74 69 6f 6e 20 28 73 7a 29 2c 0a 2a 2a 20 61 ation (sz),.** a
10e80 6e 64 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e nd the maximum n
10e90 75 6d 62 65 72 20 6f 66 20 73 63 72 61 74 63 68 umber of scratch
10ea0 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 28 4e 29 allocations (N)
10eb0 2e 20 20 54 68 65 20 73 7a 0a 2a 2a 20 61 72 67 . The sz.** arg
10ec0 75 6d 65 6e 74 20 6d 75 73 74 20 62 65 20 61 20 ument must be a
10ed0 6d 75 6c 74 69 70 6c 65 20 6f 66 20 31 36 2e 20 multiple of 16.
10ee0 54 68 65 20 73 7a 20 70 61 72 61 6d 65 74 65 72 The sz parameter
10ef0 20 73 68 6f 75 6c 64 20 62 65 20 61 20 66 65 77 should be a few
10f00 20 62 79 74 65 73 0a 2a 2a 20 6c 61 72 67 65 72 bytes.** larger
10f10 20 74 68 61 6e 20 74 68 65 20 61 63 74 75 61 6c than the actual
10f20 20 73 63 72 61 74 63 68 20 73 70 61 63 65 20 72 scratch space r
10f30 65 71 75 69 72 65 64 20 64 75 65 20 74 6f 20 69 equired due to i
10f40 6e 74 65 72 6e 61 6c 20 6f 76 65 72 68 65 61 64 nternal overhead
10f50 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 ..** The first a
10f60 72 67 75 6d 65 6e 74 20 73 68 6f 75 6c 64 20 70 rgument should p
10f70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 38 2d 62 ointer to an 8-b
10f80 79 74 65 20 61 6c 69 67 6e 65 64 20 62 75 66 66 yte aligned buff
10f90 65 72 0a 2a 2a 20 6f 66 20 61 74 20 6c 65 61 73 er.** of at leas
10fa0 74 20 73 7a 2a 4e 20 62 79 74 65 73 20 6f 66 20 t sz*N bytes of
10fb0 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 53 51 4c 69 74 memory..** SQLit
10fc0 65 20 77 69 6c 6c 20 75 73 65 20 6e 6f 20 6d 6f e will use no mo
10fd0 72 65 20 74 68 61 6e 20 6f 6e 65 20 73 63 72 61 re than one scra
10fe0 74 63 68 20 62 75 66 66 65 72 20 61 74 20 6f 6e tch buffer at on
10ff0 63 65 20 70 65 72 20 74 68 72 65 61 64 2c 20 73 ce per thread, s
11000 6f 0a 2a 2a 20 4e 20 73 68 6f 75 6c 64 20 62 65 o.** N should be
11010 20 73 65 74 20 74 6f 20 74 68 65 20 65 78 70 65 set to the expe
11020 63 74 65 64 20 6d 61 78 69 6d 75 6d 20 6e 75 6d cted maximum num
11030 62 65 72 20 6f 66 20 74 68 72 65 61 64 73 2e 20 ber of threads.
11040 20 54 68 65 20 73 7a 0a 2a 2a 20 70 61 72 61 6d The sz.** param
11050 65 74 65 72 20 73 68 6f 75 6c 64 20 62 65 20 36 eter should be 6
11060 20 74 69 6d 65 73 20 74 68 65 20 73 69 7a 65 20 times the size
11070 6f 66 20 74 68 65 20 6c 61 72 67 65 73 74 20 64 of the largest d
11080 61 74 61 62 61 73 65 20 70 61 67 65 20 73 69 7a atabase page siz
11090 65 2e 0a 2a 2a 20 53 63 72 61 74 63 68 20 62 75 e..** Scratch bu
110a0 66 66 65 72 73 20 61 72 65 20 75 73 65 64 20 61 ffers are used a
110b0 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 62 74 s part of the bt
110c0 72 65 65 20 62 61 6c 61 6e 63 65 20 6f 70 65 72 ree balance oper
110d0 61 74 69 6f 6e 2e 20 20 49 66 0a 2a 2a 20 54 68 ation. If.** Th
110e0 65 20 62 74 72 65 65 20 62 61 6c 61 6e 63 65 72 e btree balancer
110f0 20 6e 65 65 64 73 20 61 64 64 69 74 69 6f 6e 61 needs additiona
11100 6c 20 6d 65 6d 6f 72 79 20 62 65 79 6f 6e 64 20 l memory beyond
11110 77 68 61 74 20 69 73 20 70 72 6f 76 69 64 65 64 what is provided
11120 20 62 79 0a 2a 2a 20 73 63 72 61 74 63 68 20 62 by.** scratch b
11130 75 66 66 65 72 73 20 6f 72 20 69 66 20 6e 6f 20 uffers or if no
11140 73 63 72 61 74 63 68 20 62 75 66 66 65 72 20 73 scratch buffer s
11150 70 61 63 65 20 69 73 20 73 70 65 63 69 66 69 65 pace is specifie
11160 64 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 0a 2a d, then SQLite.*
11170 2a 20 67 6f 65 73 20 74 6f 20 5b 73 71 6c 69 74 * goes to [sqlit
11180 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 74 6f 20 e3_malloc()] to
11190 6f 62 74 61 69 6e 20 74 68 65 20 6d 65 6d 6f 72 obtain the memor
111a0 79 20 69 74 20 6e 65 65 64 73 2e 3c 2f 64 64 3e y it needs.</dd>
111b0 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 .**.** <dt>SQLIT
111c0 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 E_CONFIG_PAGECAC
111d0 48 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 HE</dt>.** <dd>T
111e0 68 69 73 20 6f 70 74 69 6f 6e 20 73 70 65 63 69 his option speci
111f0 66 69 65 73 20 61 20 73 74 61 74 69 63 20 6d 65 fies a static me
11200 6d 6f 72 79 20 62 75 66 66 65 72 20 74 68 61 74 mory buffer that
11210 20 53 51 4c 69 74 65 20 63 61 6e 20 75 73 65 20 SQLite can use
11220 66 6f 72 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 for.** the datab
11230 61 73 65 20 70 61 67 65 20 63 61 63 68 65 20 77 ase page cache w
11240 69 74 68 20 74 68 65 20 64 65 66 61 75 6c 74 20 ith the default
11250 70 61 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65 page cache imple
11260 6d 65 6e 61 74 69 6f 6e 2e 20 20 0a 2a 2a 20 54 menation. .** T
11270 68 69 73 20 63 6f 6e 66 69 67 75 72 61 74 69 6f his configuratio
11280 6e 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 n should not be
11290 75 73 65 64 20 69 66 20 61 6e 20 61 70 70 6c 69 used if an appli
112a0 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 20 70 61 cation-define pa
112b0 67 65 0a 2a 2a 20 63 61 63 68 65 20 69 6d 70 6c ge.** cache impl
112c0 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 6c 6f ementation is lo
112d0 61 64 65 64 20 75 73 69 6e 67 20 74 68 65 20 53 aded using the S
112e0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 QLITE_CONFIG_PCA
112f0 43 48 45 20 6f 70 74 69 6f 6e 2e 0a 2a 2a 20 54 CHE option..** T
11300 68 65 72 65 20 61 72 65 20 74 68 72 65 65 20 61 here are three a
11310 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 rguments to this
11320 20 6f 70 74 69 6f 6e 3a 20 41 20 70 6f 69 6e 74 option: A point
11330 65 72 20 74 6f 20 38 2d 62 79 74 65 20 61 6c 69 er to 8-byte ali
11340 67 6e 65 64 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 gned.** memory,
11350 74 68 65 20 73 69 7a 65 20 6f 66 20 65 61 63 68 the size of each
11360 20 70 61 67 65 20 62 75 66 66 65 72 20 28 73 7a page buffer (sz
11370 29 2c 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 ), and the numbe
11380 72 20 6f 66 20 70 61 67 65 73 20 28 4e 29 2e 0a r of pages (N)..
11390 2a 2a 20 54 68 65 20 73 7a 20 61 72 67 75 6d 65 ** The sz argume
113a0 6e 74 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65 nt should be the
113b0 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6c 61 72 size of the lar
113c0 67 65 73 74 20 64 61 74 61 62 61 73 65 20 70 61 gest database pa
113d0 67 65 0a 2a 2a 20 28 61 20 70 6f 77 65 72 20 6f ge.** (a power o
113e0 66 20 74 77 6f 20 62 65 74 77 65 65 6e 20 35 31 f two between 51
113f0 32 20 61 6e 64 20 33 32 37 36 38 29 20 70 6c 75 2 and 32768) plu
11400 73 20 61 20 6c 69 74 74 6c 65 20 65 78 74 72 61 s a little extra
11410 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 70 61 67 for each.** pag
11420 65 20 68 65 61 64 65 72 2e 20 20 54 68 65 20 70 e header. The p
11430 61 67 65 20 68 65 61 64 65 72 20 73 69 7a 65 20 age header size
11440 69 73 20 32 30 20 74 6f 20 34 30 20 62 79 74 65 is 20 to 40 byte
11450 73 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 0a 2a s depending on.*
11460 2a 20 74 68 65 20 68 6f 73 74 20 61 72 63 68 69 * the host archi
11470 74 65 63 74 75 72 65 2e 20 20 49 74 20 69 73 20 tecture. It is
11480 68 61 72 6d 6c 65 73 73 2c 20 61 70 61 72 74 20 harmless, apart
11490 66 72 6f 6d 20 74 68 65 20 77 61 73 74 65 64 20 from the wasted
114a0 6d 65 6d 6f 72 79 2c 0a 2a 2a 20 74 6f 20 6d 61 memory,.** to ma
114b0 6b 65 20 73 7a 20 61 20 6c 69 74 74 6c 65 20 74 ke sz a little t
114c0 6f 6f 20 6c 61 72 67 65 2e 20 20 54 68 65 20 66 oo large. The f
114d0 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 irst.** argument
114e0 20 73 68 6f 75 6c 64 20 70 6f 69 6e 74 20 74 6f should point to
114f0 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f an allocation o
11500 66 20 61 74 20 6c 65 61 73 74 20 73 7a 2a 4e 20 f at least sz*N
11510 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e bytes of memory.
11520 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 .** SQLite will
11530 75 73 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 70 use the memory p
11540 72 6f 76 69 64 65 64 20 62 79 20 74 68 65 20 66 rovided by the f
11550 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f irst argument to
11560 20 73 61 74 69 73 66 79 20 69 74 73 0a 2a 2a 20 satisfy its.**
11570 6d 65 6d 6f 72 79 20 6e 65 65 64 73 20 66 6f 72 memory needs for
11580 20 74 68 65 20 66 69 72 73 74 20 4e 20 70 61 67 the first N pag
11590 65 73 20 74 68 61 74 20 69 74 20 61 64 64 73 20 es that it adds
115a0 74 6f 20 63 61 63 68 65 2e 20 20 49 66 20 61 64 to cache. If ad
115b0 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 70 61 67 65 ditional.** page
115c0 20 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 69 73 cache memory is
115d0 20 6e 65 65 64 65 64 20 62 65 79 6f 6e 64 20 77 needed beyond w
115e0 68 61 74 20 69 73 20 70 72 6f 76 69 64 65 64 20 hat is provided
115f0 62 79 20 74 68 69 73 20 6f 70 74 69 6f 6e 2c 20 by this option,
11600 74 68 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 67 then.** SQLite g
11610 6f 65 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f oes to [sqlite3_
11620 6d 61 6c 6c 6f 63 28 29 5d 20 66 6f 72 20 74 68 malloc()] for th
11630 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 6f e additional sto
11640 72 61 67 65 20 73 70 61 63 65 2e 0a 2a 2a 20 54 rage space..** T
11650 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
11660 6e 20 6d 69 67 68 74 20 75 73 65 20 6f 6e 65 20 n might use one
11670 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 4e or more of the N
11680 20 62 75 66 66 65 72 73 20 74 6f 20 68 6f 6c 64 buffers to hold
11690 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 63 63 6f .** memory acco
116a0 75 6e 74 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 unting informati
116b0 6f 6e 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 on. The pointer
116c0 69 6e 20 74 68 65 20 66 69 72 73 74 20 61 72 67 in the first arg
116d0 75 6d 65 6e 74 20 6d 75 73 74 0a 2a 2a 20 62 65 ument must.** be
116e0 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 38 aligned to an 8
116f0 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 79 20 6f -byte boundary o
11700 72 20 73 75 62 73 65 71 75 65 6e 74 20 62 65 68 r subsequent beh
11710 61 76 69 6f 72 20 6f 66 20 53 51 4c 69 74 65 0a avior of SQLite.
11720 2a 2a 20 77 69 6c 6c 20 62 65 20 75 6e 64 65 66 ** will be undef
11730 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a ined.</dd>.**.**
11740 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 <dt>SQLITE_CONF
11750 49 47 5f 48 45 41 50 3c 2f 64 74 3e 0a 2a 2a 20 IG_HEAP</dt>.**
11760 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 <dd>This option
11770 73 70 65 63 69 66 69 65 73 20 61 20 73 74 61 74 specifies a stat
11780 69 63 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 ic memory buffer
11790 20 74 68 61 74 20 53 51 4c 69 74 65 20 77 69 6c that SQLite wil
117a0 6c 20 75 73 65 0a 2a 2a 20 66 6f 72 20 61 6c 6c l use.** for all
117b0 20 6f 66 20 69 74 73 20 64 79 6e 61 6d 69 63 20 of its dynamic
117c0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
117d0 6e 20 6e 65 65 64 73 20 62 65 79 6f 6e 64 20 74 n needs beyond t
117e0 68 6f 73 65 20 70 72 6f 76 69 64 65 64 0a 2a 2a hose provided.**
117f0 20 66 6f 72 20 62 79 20 5b 53 51 4c 49 54 45 5f for by [SQLITE_
11800 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 20 CONFIG_SCRATCH]
11810 61 6e 64 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 and [SQLITE_CONF
11820 49 47 5f 50 41 47 45 43 41 43 48 45 5d 2e 0a 2a IG_PAGECACHE]..*
11830 2a 20 54 68 65 72 65 20 61 72 65 20 74 68 72 65 * There are thre
11840 65 20 61 72 67 75 6d 65 6e 74 73 3a 20 41 6e 20 e arguments: An
11850 38 2d 62 79 74 65 20 61 6c 69 67 6e 65 64 20 70 8-byte aligned p
11860 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d 65 ointer to the me
11870 6d 6f 72 79 2c 0a 2a 2a 20 74 68 65 20 6e 75 6d mory,.** the num
11880 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 ber of bytes in
11890 74 68 65 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 the memory buffe
118a0 72 2c 20 61 6e 64 20 74 68 65 20 6d 69 6e 69 6d r, and the minim
118b0 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 um allocation si
118c0 7a 65 2e 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 ze..** If the fi
118d0 72 73 74 20 70 6f 69 6e 74 65 72 20 28 74 68 65 rst pointer (the
118e0 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 72 29 memory pointer)
118f0 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 53 is NULL, then S
11900 51 4c 69 74 65 20 72 65 76 65 72 74 73 0a 2a 2a QLite reverts.**
11910 20 74 6f 20 75 73 69 6e 67 20 69 74 73 20 64 65 to using its de
11920 66 61 75 6c 74 20 6d 65 6d 6f 72 79 20 61 6c 6c fault memory all
11930 6f 63 61 74 6f 72 20 28 74 68 65 20 73 79 73 74 ocator (the syst
11940 65 6d 20 6d 61 6c 6c 6f 63 28 29 20 69 6d 70 6c em malloc() impl
11950 65 6d 65 6e 74 61 74 69 6f 6e 29 2c 0a 2a 2a 20 ementation),.**
11960 75 6e 64 6f 69 6e 67 20 61 6e 79 20 70 72 69 6f undoing any prio
11970 72 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 r invocation of
11980 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d [SQLITE_CONFIG_M
11990 41 4c 4c 4f 43 5d 2e 20 20 49 66 20 74 68 65 0a ALLOC]. If the.
119a0 2a 2a 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 ** memory pointe
119b0 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 61 6e r is not NULL an
119c0 64 20 65 69 74 68 65 72 20 5b 53 51 4c 49 54 45 d either [SQLITE
119d0 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 5d _ENABLE_MEMSYS3]
119e0 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 or.** [SQLITE_E
119f0 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 5d 20 61 NABLE_MEMSYS5] a
11a00 72 65 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e re defined, then
11a10 20 74 68 65 20 61 6c 74 65 72 6e 61 74 69 76 65 the alternative
11a20 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 memory.** alloc
11a30 61 74 6f 72 20 69 73 20 65 6e 67 61 67 65 64 20 ator is engaged
11a40 74 6f 20 68 61 6e 64 6c 65 20 61 6c 6c 20 6f 66 to handle all of
11a50 20 53 51 4c 69 74 65 73 20 6d 65 6d 6f 72 79 20 SQLites memory
11a60 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e 65 65 64 73 allocation needs
11a70 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 ..** The first p
11a80 6f 69 6e 74 65 72 20 28 74 68 65 20 6d 65 6d 6f ointer (the memo
11a90 72 79 20 70 6f 69 6e 74 65 72 29 20 6d 75 73 74 ry pointer) must
11aa0 20 62 65 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 be aligned to a
11ab0 6e 20 38 2d 62 79 74 65 0a 2a 2a 20 62 6f 75 6e n 8-byte.** boun
11ac0 64 61 72 79 20 6f 72 20 73 75 62 73 65 71 75 65 dary or subseque
11ad0 6e 74 20 62 65 68 61 76 69 6f 72 20 6f 66 20 53 nt behavior of S
11ae0 51 4c 69 74 65 20 77 69 6c 6c 20 62 65 20 75 6e QLite will be un
11af0 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a defined.</dd>.**
11b00 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 .** <dt>SQLITE_C
11b10 4f 4e 46 49 47 5f 4d 55 54 45 58 3c 2f 64 74 3e ONFIG_MUTEX</dt>
11b20 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 .** <dd>This opt
11b30 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67 ion takes a sing
11b40 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 le argument whic
11b50 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 h is a pointer t
11b60 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 o an.** instance
11b70 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 of the [sqlite3
11b80 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 5d 20 _mutex_methods]
11b90 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 structure. The
11ba0 61 72 67 75 6d 65 6e 74 20 73 70 65 63 69 66 69 argument specifi
11bb0 65 73 0a 2a 2a 20 61 6c 74 65 72 6e 61 74 69 76 es.** alternativ
11bc0 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 75 74 65 e low-level mute
11bd0 78 20 72 6f 75 74 69 6e 65 73 20 74 6f 20 62 65 x routines to be
11be0 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65 0a 2a used in place.*
11bf0 2a 20 74 68 65 20 6d 75 74 65 78 20 72 6f 75 74 * the mutex rout
11c00 69 6e 65 73 20 62 75 69 6c 74 20 69 6e 74 6f 20 ines built into
11c10 53 51 4c 69 74 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a SQLite.</dd>.**.
11c20 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f ** <dt>SQLITE_CO
11c30 4e 46 49 47 5f 47 45 54 4d 55 54 45 58 3c 2f 64 NFIG_GETMUTEX</d
11c40 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f t>.** <dd>This o
11c50 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 ption takes a si
11c60 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 ngle argument wh
11c70 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 ich is a pointer
11c80 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e to an.** instan
11c90 63 65 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 ce of the [sqlit
11ca0 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 e3_mutex_methods
11cb0 5d 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 ] structure. Th
11cc0 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 75 e.** [sqlite3_mu
11cd0 74 65 78 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 tex_methods].**
11ce0 73 74 72 75 63 74 75 72 65 20 69 73 20 66 69 6c structure is fil
11cf0 6c 65 64 20 77 69 74 68 20 74 68 65 20 63 75 72 led with the cur
11d00 72 65 6e 74 6c 79 20 64 65 66 69 6e 65 64 20 6d rently defined m
11d10 75 74 65 78 20 72 6f 75 74 69 6e 65 73 2e 0a 2a utex routines..*
11d20 2a 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 63 61 * This option ca
11d30 6e 20 62 65 20 75 73 65 64 20 74 6f 20 6f 76 65 n be used to ove
11d40 72 6c 6f 61 64 20 74 68 65 20 64 65 66 61 75 6c rload the defaul
11d50 74 20 6d 75 74 65 78 20 61 6c 6c 6f 63 61 74 69 t mutex allocati
11d60 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 77 on.** routines w
11d70 69 74 68 20 61 20 77 72 61 70 70 65 72 20 75 73 ith a wrapper us
11d80 65 64 20 74 6f 20 74 72 61 63 6b 20 6d 75 74 65 ed to track mute
11d90 78 20 75 73 61 67 65 20 66 6f 72 20 70 65 72 66 x usage for perf
11da0 6f 72 6d 61 6e 63 65 0a 2a 2a 20 70 72 6f 66 69 ormance.** profi
11db0 6c 69 6e 67 20 6f 72 20 74 65 73 74 69 6e 67 2c ling or testing,
11dc0 20 66 6f 72 20 65 78 61 6d 70 6c 65 2e 3c 2f 64 for example.</d
11dd0 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c d>.**.** <dt>SQL
11de0 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 ITE_CONFIG_LOOKA
11df0 53 49 44 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 SIDE</dt>.** <dd
11e00 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b >This option tak
11e10 65 73 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 es two arguments
11e20 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 20 that determine
11e30 74 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20 6d the default.** m
11e40 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
11e50 20 6c 6f 6f 6b 61 73 69 64 65 20 6f 70 74 69 6d lookaside optim
11e60 69 7a 61 74 69 6f 6e 2e 20 20 54 68 65 20 66 69 ization. The fi
11e70 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 rst argument is
11e80 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 65 the.** size of e
11e90 61 63 68 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 ach lookaside bu
11ea0 66 66 65 72 20 73 6c 6f 74 20 61 6e 64 20 74 68 ffer slot and th
11eb0 65 20 73 65 63 6f 6e 64 20 69 73 20 74 68 65 20 e second is the
11ec0 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 73 6c 6f number of.** slo
11ed0 74 73 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20 ts allocated to
11ee0 65 61 63 68 20 64 61 74 61 62 61 73 65 20 63 6f each database co
11ef0 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 nnection. This
11f00 6f 70 74 69 6f 6e 20 73 65 74 73 20 74 68 65 0a option sets the.
11f10 2a 2a 20 3c 69 3e 64 65 66 61 75 6c 74 3c 2f 69 ** <i>default</i
11f20 3e 20 6c 6f 6f 6b 61 73 69 64 65 20 73 69 7a 65 > lookaside size
11f30 2e 20 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 44 . The [SQLITE_D
11f40 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 BCONFIG_LOOKASID
11f50 45 5d 0a 2a 2a 20 76 65 72 62 20 74 6f 20 5b 73 E].** verb to [s
11f60 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 qlite3_db_config
11f70 28 29 5d 20 63 61 6e 20 62 65 20 75 73 65 64 20 ()] can be used
11f80 74 6f 20 63 68 61 6e 67 65 20 74 68 65 20 6c 6f to change the lo
11f90 6f 6b 61 73 69 64 65 0a 2a 2a 20 63 6f 6e 66 69 okaside.** confi
11fa0 67 75 72 61 74 69 6f 6e 20 6f 6e 20 69 6e 64 69 guration on indi
11fb0 76 69 64 75 61 6c 20 63 6f 6e 6e 65 63 74 69 6f vidual connectio
11fc0 6e 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c ns.</dd>.**.** <
11fd0 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 dt>SQLITE_CONFIG
11fe0 5f 50 43 41 43 48 45 3c 2f 64 74 3e 0a 2a 2a 20 _PCACHE</dt>.**
11ff0 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 <dd>This option
12000 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61 takes a single a
12010 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73 rgument which is
12020 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a a pointer to.**
12030 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 70 63 61 an [sqlite3_pca
12040 63 68 65 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a che_methods] obj
12050 65 63 74 2e 20 20 54 68 69 73 20 6f 62 6a 65 63 ect. This objec
12060 74 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20 t specifies the
12070 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20 interface.** to
12080 61 20 63 75 73 74 6f 6d 20 70 61 67 65 20 63 61 a custom page ca
12090 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 che implementati
120a0 6f 6e 2e 20 20 53 51 4c 69 74 65 20 6d 61 6b 65 on. SQLite make
120b0 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a s a copy of the.
120c0 2a 2a 20 6f 62 6a 65 63 74 20 61 6e 64 20 75 73 ** object and us
120d0 65 73 20 69 74 20 66 6f 72 20 70 61 67 65 20 63 es it for page c
120e0 61 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f ache memory allo
120f0 63 61 74 69 6f 6e 73 2e 3c 2f 64 64 3e 0a 2a 2a cations.</dd>.**
12100 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 .** <dt>SQLITE_C
12110 4f 4e 46 49 47 5f 47 45 54 50 43 41 43 48 45 3c ONFIG_GETPCACHE<
12120 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 /dt>.** <dd>This
12130 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 option takes a
12140 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 single argument
12150 77 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 which is a point
12160 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 5b 73 71 6c er to an.** [sql
12170 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 ite3_pcache_meth
12180 6f 64 73 5d 20 6f 62 6a 65 63 74 2e 20 20 53 51 ods] object. SQ
12190 4c 69 74 65 20 63 6f 70 69 65 73 20 6f 66 20 74 Lite copies of t
121a0 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 70 61 he current.** pa
121b0 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 ge cache impleme
121c0 6e 74 61 74 69 6f 6e 20 69 6e 74 6f 20 74 68 61 ntation into tha
121d0 74 20 6f 62 6a 65 63 74 2e 3c 2f 64 64 3e 0a 2a t object.</dd>.*
121e0 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 *.** </dl>.*/.#d
121f0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e efine SQLITE_CON
12200 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 FIG_SINGLETHREAD
12210 20 20 31 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a 23 1 /* nil */.#
12220 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f define SQLITE_CO
12230 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 NFIG_MULTITHREAD
12240 20 20 20 32 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a 2 /* nil */.
12250 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 #define SQLITE_C
12260 4f 4e 46 49 47 5f 53 45 52 49 41 4c 49 5a 45 44 ONFIG_SERIALIZED
12270 20 20 20 20 33 20 20 2f 2a 20 6e 69 6c 20 2a 2f 3 /* nil */
12280 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
12290 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 20 20 20 CONFIG_MALLOC
122a0 20 20 20 20 20 34 20 20 2f 2a 20 73 71 6c 69 74 4 /* sqlit
122b0 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a 20 e3_mem_methods*
122c0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
122d0 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c 4c E_CONFIG_GETMALL
122e0 4f 43 20 20 20 20 20 35 20 20 2f 2a 20 73 71 6c OC 5 /* sql
122f0 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 ite3_mem_methods
12300 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c * */.#define SQL
12310 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 ITE_CONFIG_SCRAT
12320 43 48 20 20 20 20 20 20 20 36 20 20 2f 2a 20 76 CH 6 /* v
12330 6f 69 64 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 6e oid*, int sz, in
12340 74 20 4e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 t N */.#define S
12350 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 QLITE_CONFIG_PAG
12360 45 43 41 43 48 45 20 20 20 20 20 37 20 20 2f 2a ECACHE 7 /*
12370 20 76 6f 69 64 2a 2c 20 69 6e 74 20 73 7a 2c 20 void*, int sz,
12380 69 6e 74 20 4e 20 2a 2f 0a 23 64 65 66 69 6e 65 int N */.#define
12390 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 48 SQLITE_CONFIG_H
123a0 45 41 50 20 20 20 20 20 20 20 20 20 20 38 20 20 EAP 8
123b0 2f 2a 20 76 6f 69 64 2a 2c 20 69 6e 74 20 6e 42 /* void*, int nB
123c0 79 74 65 2c 20 69 6e 74 20 6d 69 6e 20 2a 2f 0a yte, int min */.
123d0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 #define SQLITE_C
123e0 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 20 ONFIG_MEMSTATUS
123f0 20 20 20 20 39 20 20 2f 2a 20 62 6f 6f 6c 65 61 9 /* boolea
12400 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c n */.#define SQL
12410 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 ITE_CONFIG_MUTEX
12420 20 20 20 20 20 20 20 20 31 30 20 20 2f 2a 20 73 10 /* s
12430 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 qlite3_mutex_met
12440 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 hods* */.#define
12450 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 SQLITE_CONFIG_G
12460 45 54 4d 55 54 45 58 20 20 20 20 20 31 31 20 20 ETMUTEX 11
12470 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 /* sqlite3_mutex
12480 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 2f 2a 20 _methods* */./*
12490 70 72 65 76 69 6f 75 73 6c 79 20 53 51 4c 49 54 previously SQLIT
124a0 45 5f 43 4f 4e 46 49 47 5f 43 48 55 4e 4b 41 4c E_CONFIG_CHUNKAL
124b0 4c 4f 43 20 31 32 20 77 68 69 63 68 20 69 73 20 LOC 12 which is
124c0 6e 6f 77 20 75 6e 75 73 65 64 2e 20 2a 2f 20 0a now unused. */ .
124d0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 #define SQLITE_C
124e0 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 20 ONFIG_LOOKASIDE
124f0 20 20 20 31 33 20 20 2f 2a 20 69 6e 74 20 69 6e 13 /* int in
12500 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c t */.#define SQL
12510 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 ITE_CONFIG_PCACH
12520 45 20 20 20 20 20 20 20 31 34 20 20 2f 2a 20 73 E 14 /* s
12530 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 qlite3_pcache_me
12540 74 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e thods* */.#defin
12550 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f e SQLITE_CONFIG_
12560 47 45 54 50 43 41 43 48 45 20 20 20 20 31 35 20 GETPCACHE 15
12570 20 2f 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63 /* sqlite3_pcac
12580 68 65 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 0a he_methods* */..
12590 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
125a0 43 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 4f 70 Configuration Op
125b0 74 69 6f 6e 73 20 7b 48 31 30 31 37 30 7d 20 3c tions {H10170} <
125c0 53 32 30 30 30 30 3e 0a 2a 2a 20 45 58 50 45 52 S20000>.** EXPER
125d0 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 IMENTAL.**.** Th
125e0 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 ese constants ar
125f0 65 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20 e the available
12600 69 6e 74 65 67 65 72 20 63 6f 6e 66 69 67 75 72 integer configur
12610 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 74 68 ation options th
12620 61 74 0a 2a 2a 20 63 61 6e 20 62 65 20 70 61 73 at.** can be pas
12630 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e sed as the secon
12640 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 d argument to th
12650 65 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f e [sqlite3_db_co
12660 6e 66 69 67 28 29 5d 20 69 6e 74 65 72 66 61 63 nfig()] interfac
12670 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 63 6f 6e e..**.** New con
12680 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f figuration optio
12690 6e 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 ns may be added
126a0 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 in future releas
126b0 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a es of SQLite..**
126c0 20 45 78 69 73 74 69 6e 67 20 63 6f 6e 66 69 67 Existing config
126d0 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 uration options
126e0 6d 69 67 68 74 20 62 65 20 64 69 73 63 6f 6e 74 might be discont
126f0 69 6e 75 65 64 2e 20 20 41 70 70 6c 69 63 61 74 inued. Applicat
12700 69 6f 6e 73 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 ions.** should c
12710 68 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20 heck the return
12720 63 6f 64 65 20 66 72 6f 6d 20 5b 73 71 6c 69 74 code from [sqlit
12730 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d 20 e3_db_config()]
12740 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 to make sure tha
12750 74 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 20 77 6f t.** the call wo
12760 72 6b 65 64 2e 20 20 54 68 65 20 5b 73 71 6c 69 rked. The [sqli
12770 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d te3_db_config()]
12780 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c 6c 20 interface will
12790 72 65 74 75 72 6e 20 61 0a 2a 2a 20 6e 6f 6e 2d return a.** non-
127a0 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64 65 zero [error code
127b0 5d 20 69 66 20 61 20 64 69 73 63 6f 6e 74 69 6e ] if a discontin
127c0 75 65 64 20 6f 72 20 75 6e 73 75 70 70 6f 72 74 ued or unsupport
127d0 65 64 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e ed configuration
127e0 20 6f 70 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 6e option.** is in
127f0 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c voked..**.** <dl
12800 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f >.** <dt>SQLITE_
12810 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 DBCONFIG_LOOKASI
12820 44 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 DE</dt>.** <dd>T
12830 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 his option takes
12840 20 74 68 72 65 65 20 61 64 64 69 74 69 6f 6e 61 three additiona
12850 6c 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61 74 l arguments that
12860 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 0a determine the .
12870 2a 2a 20 5b 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 ** [lookaside me
12880 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 mory allocator]
12890 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 66 6f configuration fo
128a0 72 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 r the [database
128b0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 0a 2a 2a 20 connection]..**
128c0 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 The first argume
128d0 6e 74 20 28 74 68 65 20 74 68 69 72 64 20 70 61 nt (the third pa
128e0 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 rameter to [sqli
128f0 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d te3_db_config()]
12900 20 69 73 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 is a.** pointer
12910 20 74 6f 20 61 6e 20 6d 65 6d 6f 72 79 20 62 75 to an memory bu
12920 66 66 65 72 20 74 6f 20 75 73 65 20 66 6f 72 20 ffer to use for
12930 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 lookaside memory
12940 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 ..** The first a
12950 72 67 75 6d 65 6e 74 20 6d 61 79 20 62 65 20 4e rgument may be N
12960 55 4c 4c 20 69 6e 20 77 68 69 63 68 20 63 61 73 ULL in which cas
12970 65 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c e SQLite will al
12980 6c 6f 63 61 74 65 20 74 68 65 0a 2a 2a 20 6c 6f locate the.** lo
12990 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 20 69 okaside buffer i
129a0 74 73 65 6c 66 20 75 73 69 6e 67 20 5b 73 71 6c tself using [sql
129b0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 ite3_malloc()].
129c0 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 The second argu
129d0 6d 65 6e 74 20 69 73 20 74 68 65 0a 2a 2a 20 73 ment is the.** s
129e0 69 7a 65 20 6f 66 20 65 61 63 68 20 6c 6f 6f 6b ize of each look
129f0 61 73 69 64 65 20 62 75 66 66 65 72 20 73 6c 6f aside buffer slo
12a00 74 20 61 6e 64 20 74 68 65 20 74 68 69 72 64 20 t and the third
12a10 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 argument is the
12a20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 73 6c 6f number of.** slo
12a30 74 73 2e 20 20 54 68 65 20 73 69 7a 65 20 6f 66 ts. The size of
12a40 20 74 68 65 20 62 75 66 66 65 72 20 69 6e 20 74 the buffer in t
12a50 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
12a60 74 20 6d 75 73 74 20 62 65 20 67 72 65 61 74 65 t must be greate
12a70 72 20 74 68 61 6e 0a 2a 2a 20 6f 72 20 65 71 75 r than.** or equ
12a80 61 6c 20 74 6f 20 74 68 65 20 70 72 6f 64 75 63 al to the produc
12a90 74 20 6f 66 20 74 68 65 20 73 65 63 6f 6e 64 20 t of the second
12aa0 61 6e 64 20 74 68 69 72 64 20 61 72 67 75 6d 65 and third argume
12ab0 6e 74 73 2e 20 20 54 68 65 20 62 75 66 66 65 72 nts. The buffer
12ac0 0a 2a 2a 20 6d 75 73 74 20 62 65 20 61 6c 69 67 .** must be alig
12ad0 6e 65 64 20 74 6f 20 61 6e 20 38 2d 62 79 74 65 ned to an 8-byte
12ae0 20 62 6f 75 6e 64 61 72 79 2e 20 20 49 66 20 74 boundary. If t
12af0 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
12b00 6e 74 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 6d nt is not.** a m
12b10 75 6c 74 69 70 6c 65 20 6f 66 20 38 2c 20 69 74 ultiple of 8, it
12b20 20 69 73 20 69 6e 74 65 72 6e 61 6c 6c 79 20 72 is internally r
12b30 6f 75 6e 64 65 64 20 64 6f 77 6e 20 74 6f 20 74 ounded down to t
12b40 68 65 20 6e 65 78 74 20 73 6d 61 6c 6c 65 72 0a he next smaller.
12b50 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 38 ** multiple of 8
12b60 2e 20 20 53 65 65 20 61 6c 73 6f 3a 20 5b 53 51 . See also: [SQ
12b70 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b LITE_CONFIG_LOOK
12b80 41 53 49 44 45 5d 3c 2f 64 64 3e 0a 2a 2a 0a 2a ASIDE]</dd>.**.*
12b90 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 65 66 69 * </dl>.*/.#defi
12ba0 6e 65 20 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46 ne SQLITE_DBCONF
12bb0 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 20 20 20 20 IG_LOOKASIDE
12bc0 31 30 30 31 20 20 2f 2a 20 76 6f 69 64 2a 20 69 1001 /* void* i
12bd0 6e 74 20 69 6e 74 20 2a 2f 0a 0a 0a 2f 2a 0a 2a nt int */.../*.*
12be0 2a 20 43 41 50 49 33 52 45 46 3a 20 45 6e 61 62 * CAPI3REF: Enab
12bf0 6c 65 20 4f 72 20 44 69 73 61 62 6c 65 20 45 78 le Or Disable Ex
12c00 74 65 6e 64 65 64 20 52 65 73 75 6c 74 20 43 6f tended Result Co
12c10 64 65 73 20 7b 48 31 32 32 30 30 7d 20 3c 53 31 des {H12200} <S1
12c20 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 0700>.**.** The
12c30 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 sqlite3_extended
12c40 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 28 29 20 _result_codes()
12c50 72 6f 75 74 69 6e 65 20 65 6e 61 62 6c 65 73 20 routine enables
12c60 6f 72 20 64 69 73 61 62 6c 65 73 20 74 68 65 0a or disables the.
12c70 2a 2a 20 5b 65 78 74 65 6e 64 65 64 20 72 65 73 ** [extended res
12c80 75 6c 74 20 63 6f 64 65 73 5d 20 66 65 61 74 75 ult codes] featu
12c90 72 65 20 6f 66 20 53 51 4c 69 74 65 2e 20 54 68 re of SQLite. Th
12ca0 65 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c e extended resul
12cb0 74 0a 2a 2a 20 63 6f 64 65 73 20 61 72 65 20 64 t.** codes are d
12cc0 69 73 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 isabled by defau
12cd0 6c 74 20 66 6f 72 20 68 69 73 74 6f 72 69 63 61 lt for historica
12ce0 6c 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 l compatibility
12cf0 63 6f 6e 73 69 64 65 72 61 74 69 6f 6e 73 2e 0a considerations..
12d00 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
12d10 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 30 31 5d 20 ts:.** [H12201]
12d20 5b 48 31 32 32 30 32 5d 0a 2a 2f 0a 53 51 4c 49 [H12202].*/.SQLI
12d30 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
12d40 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65 73 75 e3_extended_resu
12d50 6c 74 5f 63 6f 64 65 73 28 73 71 6c 69 74 65 33 lt_codes(sqlite3
12d60 2a 2c 20 69 6e 74 20 6f 6e 6f 66 66 29 3b 0a 0a *, int onoff);..
12d70 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
12d80 4c 61 73 74 20 49 6e 73 65 72 74 20 52 6f 77 69 Last Insert Rowi
12d90 64 20 7b 48 31 32 32 32 30 7d 20 3c 53 31 30 37 d {H12220} <S107
12da0 30 30 3e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 65 00>.**.** Each e
12db0 6e 74 72 79 20 69 6e 20 61 6e 20 53 51 4c 69 74 ntry in an SQLit
12dc0 65 20 74 61 62 6c 65 20 68 61 73 20 61 20 75 6e e table has a un
12dd0 69 71 75 65 20 36 34 2d 62 69 74 20 73 69 67 6e ique 64-bit sign
12de0 65 64 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6b 65 ed.** integer ke
12df0 79 20 63 61 6c 6c 65 64 20 74 68 65 20 5b 52 4f y called the [RO
12e00 57 49 44 20 7c 20 22 72 6f 77 69 64 22 5d 2e 20 WID | "rowid"].
12e10 54 68 65 20 72 6f 77 69 64 20 69 73 20 61 6c 77 The rowid is alw
12e20 61 79 73 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a ays available.**
12e30 20 61 73 20 61 6e 20 75 6e 64 65 63 6c 61 72 65 as an undeclare
12e40 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 52 d column named R
12e50 4f 57 49 44 2c 20 4f 49 44 2c 20 6f 72 20 5f 52 OWID, OID, or _R
12e60 4f 57 49 44 5f 20 61 73 20 6c 6f 6e 67 20 61 73 OWID_ as long as
12e70 20 74 68 6f 73 65 0a 2a 2a 20 6e 61 6d 65 73 20 those.** names
12e80 61 72 65 20 6e 6f 74 20 61 6c 73 6f 20 75 73 65 are not also use
12e90 64 20 62 79 20 65 78 70 6c 69 63 69 74 6c 79 20 d by explicitly
12ea0 64 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d 6e 73 declared columns
12eb0 2e 20 49 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c . If.** the tabl
12ec0 65 20 68 61 73 20 61 20 63 6f 6c 75 6d 6e 20 6f e has a column o
12ed0 66 20 74 79 70 65 20 5b 49 4e 54 45 47 45 52 20 f type [INTEGER
12ee0 50 52 49 4d 41 52 59 20 4b 45 59 5d 20 74 68 65 PRIMARY KEY] the
12ef0 6e 20 74 68 61 74 20 63 6f 6c 75 6d 6e 0a 2a 2a n that column.**
12f00 20 69 73 20 61 6e 6f 74 68 65 72 20 61 6c 69 61 is another alia
12f10 73 20 66 6f 72 20 74 68 65 20 72 6f 77 69 64 2e s for the rowid.
12f20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
12f30 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 ine returns the
12f40 5b 72 6f 77 69 64 5d 20 6f 66 20 74 68 65 20 6d [rowid] of the m
12f50 6f 73 74 20 72 65 63 65 6e 74 0a 2a 2a 20 73 75 ost recent.** su
12f60 63 63 65 73 73 66 75 6c 20 5b 49 4e 53 45 52 54 ccessful [INSERT
12f70 5d 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 ] into the datab
12f80 61 73 65 20 66 72 6f 6d 20 74 68 65 20 5b 64 61 ase from the [da
12f90 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
12fa0 6e 5d 0a 2a 2a 20 69 6e 20 74 68 65 20 66 69 72 n].** in the fir
12fb0 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 66 st argument. If
12fc0 20 6e 6f 20 73 75 63 63 65 73 73 66 75 6c 20 5b no successful [
12fd0 49 4e 53 45 52 54 5d 73 0a 2a 2a 20 68 61 76 65 INSERT]s.** have
12fe0 20 65 76 65 72 20 6f 63 63 75 72 72 65 64 20 6f ever occurred o
12ff0 6e 20 74 68 61 74 20 64 61 74 61 62 61 73 65 20 n that database
13000 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 7a 65 72 6f connection, zero
13010 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
13020 0a 2a 2a 20 49 66 20 61 6e 20 5b 49 4e 53 45 52 .** If an [INSER
13030 54 5d 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e T] occurs within
13040 20 61 20 74 72 69 67 67 65 72 2c 20 74 68 65 6e a trigger, then
13050 20 74 68 65 20 5b 72 6f 77 69 64 5d 20 6f 66 20 the [rowid] of
13060 74 68 65 20 69 6e 73 65 72 74 65 64 0a 2a 2a 20 the inserted.**
13070 72 6f 77 20 69 73 20 72 65 74 75 72 6e 65 64 20 row is returned
13080 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 by this routine
13090 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 74 as long as the t
130a0 72 69 67 67 65 72 20 69 73 20 72 75 6e 6e 69 6e rigger is runnin
130b0 67 2e 0a 2a 2a 20 42 75 74 20 6f 6e 63 65 20 74 g..** But once t
130c0 68 65 20 74 72 69 67 67 65 72 20 74 65 72 6d 69 he trigger termi
130d0 6e 61 74 65 73 2c 20 74 68 65 20 76 61 6c 75 65 nates, the value
130e0 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 69 returned by thi
130f0 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 76 s routine.** rev
13100 65 72 74 73 20 74 6f 20 74 68 65 20 6c 61 73 74 erts to the last
13110 20 76 61 6c 75 65 20 69 6e 73 65 72 74 65 64 20 value inserted
13120 62 65 66 6f 72 65 20 74 68 65 20 74 72 69 67 67 before the trigg
13130 65 72 20 66 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 er fired..**.**
13140 41 6e 20 5b 49 4e 53 45 52 54 5d 20 74 68 61 74 An [INSERT] that
13150 20 66 61 69 6c 73 20 64 75 65 20 74 6f 20 61 20 fails due to a
13160 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 constraint viola
13170 74 69 6f 6e 20 69 73 20 6e 6f 74 20 61 0a 2a 2a tion is not a.**
13180 20 73 75 63 63 65 73 73 66 75 6c 20 5b 49 4e 53 successful [INS
13190 45 52 54 5d 20 61 6e 64 20 64 6f 65 73 20 6e 6f ERT] and does no
131a0 74 20 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c t change the val
131b0 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 ue returned by t
131c0 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 2e 20 his.** routine.
131d0 20 54 68 75 73 20 49 4e 53 45 52 54 20 4f 52 20 Thus INSERT OR
131e0 46 41 49 4c 2c 20 49 4e 53 45 52 54 20 4f 52 20 FAIL, INSERT OR
131f0 49 47 4e 4f 52 45 2c 20 49 4e 53 45 52 54 20 4f IGNORE, INSERT O
13200 52 20 52 4f 4c 4c 42 41 43 4b 2c 0a 2a 2a 20 61 R ROLLBACK,.** a
13210 6e 64 20 49 4e 53 45 52 54 20 4f 52 20 41 42 4f nd INSERT OR ABO
13220 52 54 20 6d 61 6b 65 20 6e 6f 20 63 68 61 6e 67 RT make no chang
13230 65 73 20 74 6f 20 74 68 65 20 72 65 74 75 72 6e es to the return
13240 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 0a 2a value of this.*
13250 2a 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 74 * routine when t
13260 68 65 69 72 20 69 6e 73 65 72 74 69 6f 6e 20 66 heir insertion f
13270 61 69 6c 73 2e 20 20 57 68 65 6e 20 49 4e 53 45 ails. When INSE
13280 52 54 20 4f 52 20 52 45 50 4c 41 43 45 0a 2a 2a RT OR REPLACE.**
13290 20 65 6e 63 6f 75 6e 74 65 72 73 20 61 20 63 6f encounters a co
132a0 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 nstraint violati
132b0 6f 6e 2c 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 on, it does not
132c0 66 61 69 6c 2e 20 20 54 68 65 0a 2a 2a 20 49 4e fail. The.** IN
132d0 53 45 52 54 20 63 6f 6e 74 69 6e 75 65 73 20 74 SERT continues t
132e0 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 61 66 74 o completion aft
132f0 65 72 20 64 65 6c 65 74 69 6e 67 20 72 6f 77 73 er deleting rows
13300 20 74 68 61 74 20 63 61 75 73 65 64 0a 2a 2a 20 that caused.**
13310 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 70 the constraint p
13320 72 6f 62 6c 65 6d 20 73 6f 20 49 4e 53 45 52 54 roblem so INSERT
13330 20 4f 52 20 52 45 50 4c 41 43 45 20 77 69 6c 6c OR REPLACE will
13340 20 61 6c 77 61 79 73 20 63 68 61 6e 67 65 0a 2a always change.*
13350 2a 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c * the return val
13360 75 65 20 6f 66 20 74 68 69 73 20 69 6e 74 65 72 ue of this inter
13370 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 face..**.** For
13380 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 the purposes of
13390 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 61 6e this routine, an
133a0 20 5b 49 4e 53 45 52 54 5d 20 69 73 20 63 6f 6e [INSERT] is con
133b0 73 69 64 65 72 65 64 20 74 6f 0a 2a 2a 20 62 65 sidered to.** be
133c0 20 73 75 63 63 65 73 73 66 75 6c 20 65 76 65 6e successful even
133d0 20 69 66 20 69 74 20 69 73 20 73 75 62 73 65 71 if it is subseq
133e0 75 65 6e 74 6c 79 20 72 6f 6c 6c 65 64 20 62 61 uently rolled ba
133f0 63 6b 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 ck..**.** Requir
13400 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 ements:.** [H122
13410 32 31 5d 20 5b 48 31 32 32 32 33 5d 0a 2a 2a 0a 21] [H12223].**.
13420 2a 2a 20 49 66 20 61 20 73 65 70 61 72 61 74 65 ** If a separate
13430 20 74 68 72 65 61 64 20 70 65 72 66 6f 72 6d 73 thread performs
13440 20 61 20 6e 65 77 20 5b 49 4e 53 45 52 54 5d 20 a new [INSERT]
13450 6f 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 64 on the same.** d
13460 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
13470 6f 6e 20 77 68 69 6c 65 20 74 68 65 20 5b 73 71 on while the [sq
13480 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 lite3_last_inser
13490 74 5f 72 6f 77 69 64 28 29 5d 0a 2a 2a 20 66 75 t_rowid()].** fu
134a0 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e nction is runnin
134b0 67 20 61 6e 64 20 74 68 75 73 20 63 68 61 6e 67 g and thus chang
134c0 65 73 20 74 68 65 20 6c 61 73 74 20 69 6e 73 65 es the last inse
134d0 72 74 20 5b 72 6f 77 69 64 5d 2c 0a 2a 2a 20 74 rt [rowid],.** t
134e0 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 hen the value re
134f0 74 75 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74 turned by [sqlit
13500 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 e3_last_insert_r
13510 6f 77 69 64 28 29 5d 20 69 73 0a 2a 2a 20 75 6e owid()] is.** un
13520 70 72 65 64 69 63 74 61 62 6c 65 20 61 6e 64 20 predictable and
13530 6d 69 67 68 74 20 6e 6f 74 20 65 71 75 61 6c 20 might not equal
13540 65 69 74 68 65 72 20 74 68 65 20 6f 6c 64 20 6f either the old o
13550 72 20 74 68 65 20 6e 65 77 0a 2a 2a 20 6c 61 73 r the new.** las
13560 74 20 69 6e 73 65 72 74 20 5b 72 6f 77 69 64 5d t insert [rowid]
13570 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
13580 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 sqlite3_int64 sq
13590 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 lite3_last_inser
135a0 74 5f 72 6f 77 69 64 28 73 71 6c 69 74 65 33 2a t_rowid(sqlite3*
135b0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
135c0 45 46 3a 20 43 6f 75 6e 74 20 54 68 65 20 4e 75 EF: Count The Nu
135d0 6d 62 65 72 20 4f 66 20 52 6f 77 73 20 4d 6f 64 mber Of Rows Mod
135e0 69 66 69 65 64 20 7b 48 31 32 32 34 30 7d 20 3c ified {H12240} <
135f0 53 31 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S10600>.**.** Th
13600 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 is function retu
13610 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f rns the number o
13620 66 20 64 61 74 61 62 61 73 65 20 72 6f 77 73 20 f database rows
13630 74 68 61 74 20 77 65 72 65 20 63 68 61 6e 67 65 that were change
13640 64 0a 2a 2a 20 6f 72 20 69 6e 73 65 72 74 65 64 d.** or inserted
13650 20 6f 72 20 64 65 6c 65 74 65 64 20 62 79 20 74 or deleted by t
13660 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 he most recently
13670 20 63 6f 6d 70 6c 65 74 65 64 20 53 51 4c 20 73 completed SQL s
13680 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 6f 6e 20 74 tatement.** on t
13690 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e he [database con
136a0 6e 65 63 74 69 6f 6e 5d 20 73 70 65 63 69 66 69 nection] specifi
136b0 65 64 20 62 79 20 74 68 65 20 66 69 72 73 74 20 ed by the first
136c0 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 4f 6e parameter..** On
136d0 6c 79 20 63 68 61 6e 67 65 73 20 74 68 61 74 20 ly changes that
136e0 61 72 65 20 64 69 72 65 63 74 6c 79 20 73 70 65 are directly spe
136f0 63 69 66 69 65 64 20 62 79 20 74 68 65 20 5b 49 cified by the [I
13700 4e 53 45 52 54 5d 2c 20 5b 55 50 44 41 54 45 5d NSERT], [UPDATE]
13710 2c 0a 2a 2a 20 6f 72 20 5b 44 45 4c 45 54 45 5d ,.** or [DELETE]
13720 20 73 74 61 74 65 6d 65 6e 74 20 61 72 65 20 63 statement are c
13730 6f 75 6e 74 65 64 2e 20 20 41 75 78 69 6c 69 61 ounted. Auxilia
13740 72 79 20 63 68 61 6e 67 65 73 20 63 61 75 73 65 ry changes cause
13750 64 20 62 79 0a 2a 2a 20 74 72 69 67 67 65 72 73 d by.** triggers
13760 20 6f 72 20 5b 66 6f 72 65 69 67 6e 20 6b 65 79 or [foreign key
13770 20 61 63 74 69 6f 6e 73 5d 20 61 72 65 20 6e 6f actions] are no
13780 74 20 63 6f 75 6e 74 65 64 2e 20 55 73 65 20 74 t counted. Use t
13790 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 74 he.** [sqlite3_t
137a0 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d 20 otal_changes()]
137b0 66 75 6e 63 74 69 6f 6e 20 74 6f 20 66 69 6e 64 function to find
137c0 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 the total numbe
137d0 72 20 6f 66 20 63 68 61 6e 67 65 73 0a 2a 2a 20 r of changes.**
137e0 69 6e 63 6c 75 64 69 6e 67 20 63 68 61 6e 67 65 including change
137f0 73 20 63 61 75 73 65 64 20 62 79 20 74 72 69 67 s caused by trig
13800 67 65 72 73 20 61 6e 64 20 66 6f 72 65 69 67 6e gers and foreign
13810 20 6b 65 79 20 61 63 74 69 6f 6e 73 2e 0a 2a 2a key actions..**
13820 0a 2a 2a 20 43 68 61 6e 67 65 73 20 74 6f 20 61 .** Changes to a
13830 20 76 69 65 77 20 74 68 61 74 20 61 72 65 20 73 view that are s
13840 69 6d 75 6c 61 74 65 64 20 62 79 20 61 6e 20 5b imulated by an [
13850 49 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67 67 INSTEAD OF trigg
13860 65 72 5d 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 63 er].** are not c
13870 6f 75 6e 74 65 64 2e 20 20 4f 6e 6c 79 20 72 65 ounted. Only re
13880 61 6c 20 74 61 62 6c 65 20 63 68 61 6e 67 65 73 al table changes
13890 20 61 72 65 20 63 6f 75 6e 74 65 64 2e 0a 2a 2a are counted..**
138a0 0a 2a 2a 20 41 20 22 72 6f 77 20 63 68 61 6e 67 .** A "row chang
138b0 65 22 20 69 73 20 61 20 63 68 61 6e 67 65 20 74 e" is a change t
138c0 6f 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f o a single row o
138d0 66 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 f a single table
138e0 0a 2a 2a 20 63 61 75 73 65 64 20 62 79 20 61 6e .** caused by an
138f0 20 49 4e 53 45 52 54 2c 20 44 45 4c 45 54 45 2c INSERT, DELETE,
13900 20 6f 72 20 55 50 44 41 54 45 20 73 74 61 74 65 or UPDATE state
13910 6d 65 6e 74 2e 20 20 52 6f 77 73 20 74 68 61 74 ment. Rows that
13920 0a 2a 2a 20 61 72 65 20 63 68 61 6e 67 65 64 20 .** are changed
13930 61 73 20 73 69 64 65 20 65 66 66 65 63 74 73 20 as side effects
13940 6f 66 20 5b 52 45 50 4c 41 43 45 5d 20 63 6f 6e of [REPLACE] con
13950 73 74 72 61 69 6e 74 20 72 65 73 6f 6c 75 74 69 straint resoluti
13960 6f 6e 2c 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 2c on,.** rollback,
13970 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73 69 6e ABORT processin
13980 67 2c 20 5b 44 52 4f 50 20 54 41 42 4c 45 5d 2c g, [DROP TABLE],
13990 20 6f 72 20 62 79 20 61 6e 79 20 6f 74 68 65 72 or by any other
139a0 0a 2a 2a 20 6d 65 63 68 61 6e 69 73 6d 73 20 64 .** mechanisms d
139b0 6f 20 6e 6f 74 20 63 6f 75 6e 74 20 61 73 20 64 o not count as d
139c0 69 72 65 63 74 20 72 6f 77 20 63 68 61 6e 67 65 irect row change
139d0 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 22 74 72 69 67 s..**.** A "trig
139e0 67 65 72 20 63 6f 6e 74 65 78 74 22 20 69 73 20 ger context" is
139f0 61 20 73 63 6f 70 65 20 6f 66 20 65 78 65 63 75 a scope of execu
13a00 74 69 6f 6e 20 74 68 61 74 20 62 65 67 69 6e 73 tion that begins
13a10 20 61 6e 64 0a 2a 2a 20 65 6e 64 73 20 77 69 74 and.** ends wit
13a20 68 20 74 68 65 20 73 63 72 69 70 74 20 6f 66 20 h the script of
13a30 61 20 5b 43 52 45 41 54 45 20 54 52 49 47 47 45 a [CREATE TRIGGE
13a40 52 20 7c 20 74 72 69 67 67 65 72 5d 2e 20 0a 2a R | trigger]. .*
13a50 2a 20 4d 6f 73 74 20 53 51 4c 20 73 74 61 74 65 * Most SQL state
13a60 6d 65 6e 74 73 20 61 72 65 0a 2a 2a 20 65 76 61 ments are.** eva
13a70 6c 75 61 74 65 64 20 6f 75 74 73 69 64 65 20 6f luated outside o
13a80 66 20 61 6e 79 20 74 72 69 67 67 65 72 2e 20 20 f any trigger.
13a90 54 68 69 73 20 69 73 20 74 68 65 20 22 74 6f 70 This is the "top
13aa0 20 6c 65 76 65 6c 22 0a 2a 2a 20 74 72 69 67 67 level".** trigg
13ab0 65 72 20 63 6f 6e 74 65 78 74 2e 20 20 49 66 20 er context. If
13ac0 61 20 74 72 69 67 67 65 72 20 66 69 72 65 73 20 a trigger fires
13ad0 66 72 6f 6d 20 74 68 65 20 74 6f 70 20 6c 65 76 from the top lev
13ae0 65 6c 2c 20 61 0a 2a 2a 20 6e 65 77 20 74 72 69 el, a.** new tri
13af0 67 67 65 72 20 63 6f 6e 74 65 78 74 20 69 73 20 gger context is
13b00 65 6e 74 65 72 65 64 20 66 6f 72 20 74 68 65 20 entered for the
13b10 64 75 72 61 74 69 6f 6e 20 6f 66 20 74 68 61 74 duration of that
13b20 20 6f 6e 65 0a 2a 2a 20 74 72 69 67 67 65 72 2e one.** trigger.
13b30 20 20 53 75 62 74 72 69 67 67 65 72 73 20 63 72 Subtriggers cr
13b40 65 61 74 65 20 73 75 62 63 6f 6e 74 65 78 74 73 eate subcontexts
13b50 20 66 6f 72 20 74 68 65 69 72 20 64 75 72 61 74 for their durat
13b60 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 ion..**.** Calli
13b70 6e 67 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 ng [sqlite3_exec
13b80 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f ()] or [sqlite3_
13b90 73 74 65 70 28 29 5d 20 72 65 63 75 72 73 69 76 step()] recursiv
13ba0 65 6c 79 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 ely does.** not
13bb0 63 72 65 61 74 65 20 61 20 6e 65 77 20 74 72 69 create a new tri
13bc0 67 67 65 72 20 63 6f 6e 74 65 78 74 2e 0a 2a 2a gger context..**
13bd0 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
13be0 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 n returns the nu
13bf0 6d 62 65 72 20 6f 66 20 64 69 72 65 63 74 20 72 mber of direct r
13c00 6f 77 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68 ow changes in th
13c10 65 0a 2a 2a 20 6d 6f 73 74 20 72 65 63 65 6e 74 e.** most recent
13c20 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c INSERT, UPDATE,
13c30 20 6f 72 20 44 45 4c 45 54 45 20 73 74 61 74 65 or DELETE state
13c40 6d 65 6e 74 20 77 69 74 68 69 6e 20 74 68 65 20 ment within the
13c50 73 61 6d 65 0a 2a 2a 20 74 72 69 67 67 65 72 20 same.** trigger
13c60 63 6f 6e 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 54 context..**.** T
13c70 68 75 73 2c 20 77 68 65 6e 20 63 61 6c 6c 65 64 hus, when called
13c80 20 66 72 6f 6d 20 74 68 65 20 74 6f 70 20 6c 65 from the top le
13c90 76 65 6c 2c 20 74 68 69 73 20 66 75 6e 63 74 69 vel, this functi
13ca0 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a on returns the.*
13cb0 2a 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 6e * number of chan
13cc0 67 65 73 20 69 6e 20 74 68 65 20 6d 6f 73 74 20 ges in the most
13cd0 72 65 63 65 6e 74 20 49 4e 53 45 52 54 2c 20 55 recent INSERT, U
13ce0 50 44 41 54 45 2c 20 6f 72 20 44 45 4c 45 54 45 PDATE, or DELETE
13cf0 0a 2a 2a 20 74 68 61 74 20 61 6c 73 6f 20 6f 63 .** that also oc
13d00 63 75 72 72 65 64 20 61 74 20 74 68 65 20 74 6f curred at the to
13d10 70 20 6c 65 76 65 6c 2e 20 20 57 69 74 68 69 6e p level. Within
13d20 20 74 68 65 20 62 6f 64 79 20 6f 66 20 61 20 74 the body of a t
13d30 72 69 67 67 65 72 2c 0a 2a 2a 20 74 68 65 20 73 rigger,.** the s
13d40 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 qlite3_changes()
13d50 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 62 interface can b
13d60 65 20 63 61 6c 6c 65 64 20 74 6f 20 66 69 6e 64 e called to find
13d70 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a the number of.*
13d80 2a 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68 65 * changes in the
13d90 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 63 most recently c
13da0 6f 6d 70 6c 65 74 65 64 20 49 4e 53 45 52 54 2c ompleted INSERT,
13db0 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45 4c 45 UPDATE, or DELE
13dc0 54 45 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 TE.** statement
13dd0 77 69 74 68 69 6e 20 74 68 65 20 62 6f 64 79 20 within the body
13de0 6f 66 20 74 68 65 20 73 61 6d 65 20 74 72 69 67 of the same trig
13df0 67 65 72 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c ger..** However,
13e00 20 74 68 65 20 6e 75 6d 62 65 72 20 72 65 74 75 the number retu
13e10 72 6e 65 64 20 64 6f 65 73 20 6e 6f 74 20 69 6e rned does not in
13e20 63 6c 75 64 65 20 63 68 61 6e 67 65 73 0a 2a 2a clude changes.**
13e30 20 63 61 75 73 65 64 20 62 79 20 73 75 62 74 72 caused by subtr
13e40 69 67 67 65 72 73 20 73 69 6e 63 65 20 74 68 6f iggers since tho
13e50 73 65 20 68 61 76 65 20 74 68 65 69 72 20 6f 77 se have their ow
13e60 6e 20 63 6f 6e 74 65 78 74 2e 0a 2a 2a 0a 2a 2a n context..**.**
13e70 20 53 65 65 20 61 6c 73 6f 20 74 68 65 20 5b 73 See also the [s
13e80 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 qlite3_total_cha
13e90 6e 67 65 73 28 29 5d 20 69 6e 74 65 72 66 61 63 nges()] interfac
13ea0 65 20 61 6e 64 20 74 68 65 0a 2a 2a 20 5b 63 6f e and the.** [co
13eb0 75 6e 74 5f 63 68 61 6e 67 65 73 20 70 72 61 67 unt_changes prag
13ec0 6d 61 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 ma]..**.** Requi
13ed0 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 rements:.** [H12
13ee0 32 34 31 5d 20 5b 48 31 32 32 34 33 5d 0a 2a 2a 241] [H12243].**
13ef0 0a 2a 2a 20 49 66 20 61 20 73 65 70 61 72 61 74 .** If a separat
13f00 65 20 74 68 72 65 61 64 20 6d 61 6b 65 73 20 63 e thread makes c
13f10 68 61 6e 67 65 73 20 6f 6e 20 74 68 65 20 73 61 hanges on the sa
13f20 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e me database conn
13f30 65 63 74 69 6f 6e 0a 2a 2a 20 77 68 69 6c 65 20 ection.** while
13f40 5b 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 [sqlite3_changes
13f50 28 29 5d 20 69 73 20 72 75 6e 6e 69 6e 67 20 74 ()] is running t
13f60 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 hen the value re
13f70 74 75 72 6e 65 64 0a 2a 2a 20 69 73 20 75 6e 70 turned.** is unp
13f80 72 65 64 69 63 74 61 62 6c 65 20 61 6e 64 20 6e redictable and n
13f90 6f 74 20 6d 65 61 6e 69 6e 67 66 75 6c 2e 0a 2a ot meaningful..*
13fa0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
13fb0 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 sqlite3_changes
13fc0 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a (sqlite3*);../*.
13fd0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54 6f 74 ** CAPI3REF: Tot
13fe0 61 6c 20 4e 75 6d 62 65 72 20 4f 66 20 52 6f 77 al Number Of Row
13ff0 73 20 4d 6f 64 69 66 69 65 64 20 7b 48 31 32 32 s Modified {H122
14000 36 30 7d 20 3c 53 31 30 36 30 30 3e 0a 2a 2a 0a 60} <S10600>.**.
14010 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
14020 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d returns the num
14030 62 65 72 20 6f 66 20 72 6f 77 20 63 68 61 6e 67 ber of row chang
14040 65 73 20 63 61 75 73 65 64 20 62 79 20 5b 49 4e es caused by [IN
14050 53 45 52 54 5d 2c 0a 2a 2a 20 5b 55 50 44 41 54 SERT],.** [UPDAT
14060 45 5d 20 6f 72 20 5b 44 45 4c 45 54 45 5d 20 73 E] or [DELETE] s
14070 74 61 74 65 6d 65 6e 74 73 20 73 69 6e 63 65 20 tatements since
14080 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f the [database co
14090 6e 6e 65 63 74 69 6f 6e 5d 20 77 61 73 20 6f 70 nnection] was op
140a0 65 6e 65 64 2e 0a 2a 2a 20 54 68 65 20 63 6f 75 ened..** The cou
140b0 6e 74 20 69 6e 63 6c 75 64 65 73 20 61 6c 6c 20 nt includes all
140c0 63 68 61 6e 67 65 73 20 66 72 6f 6d 20 61 6c 6c changes from all
140d0 20 5b 43 52 45 41 54 45 20 54 52 49 47 47 45 52 [CREATE TRIGGER
140e0 20 7c 20 74 72 69 67 67 65 72 5d 20 0a 2a 2a 20 | trigger] .**
140f0 63 6f 6e 74 65 78 74 73 20 61 6e 64 20 63 68 61 contexts and cha
14100 6e 67 65 73 20 6d 61 64 65 20 62 79 20 5b 66 6f nges made by [fo
14110 72 65 69 67 6e 20 6b 65 79 20 61 63 74 69 6f 6e reign key action
14120 73 5d 2e 20 48 6f 77 65 76 65 72 2c 0a 2a 2a 20 s]. However,.**
14130 74 68 65 20 63 6f 75 6e 74 20 64 6f 65 73 20 6e the count does n
14140 6f 74 20 69 6e 63 6c 75 64 65 20 63 68 61 6e 67 ot include chang
14150 65 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 es used to imple
14160 6d 65 6e 74 20 5b 52 45 50 4c 41 43 45 5d 20 63 ment [REPLACE] c
14170 6f 6e 73 74 72 61 69 6e 74 73 2c 0a 2a 2a 20 64 onstraints,.** d
14180 6f 20 72 6f 6c 6c 62 61 63 6b 73 20 6f 72 20 41 o rollbacks or A
14190 42 4f 52 54 20 70 72 6f 63 65 73 73 69 6e 67 2c BORT processing,
141a0 20 6f 72 20 5b 44 52 4f 50 20 54 41 42 4c 45 5d or [DROP TABLE]
141b0 20 70 72 6f 63 65 73 73 69 6e 67 2e 20 20 54 68 processing. Th
141c0 65 0a 2a 2a 20 63 6f 75 6e 74 20 64 6f 65 73 20 e.** count does
141d0 6e 6f 74 20 69 6e 63 6c 75 64 65 20 72 6f 77 73 not include rows
141e0 20 6f 66 20 76 69 65 77 73 20 74 68 61 74 20 66 of views that f
141f0 69 72 65 20 61 6e 20 5b 49 4e 53 54 45 41 44 20 ire an [INSTEAD
14200 4f 46 20 74 72 69 67 67 65 72 5d 2c 0a 2a 2a 20 OF trigger],.**
14210 74 68 6f 75 67 68 20 69 66 20 74 68 65 20 49 4e though if the IN
14220 53 54 45 41 44 20 4f 46 20 74 72 69 67 67 65 72 STEAD OF trigger
14230 20 6d 61 6b 65 73 20 63 68 61 6e 67 65 73 20 6f makes changes o
14240 66 20 69 74 73 20 6f 77 6e 2c 20 74 68 6f 73 65 f its own, those
14250 20 63 68 61 6e 67 65 73 20 0a 2a 2a 20 61 72 65 changes .** are
14260 20 63 6f 75 6e 74 65 64 2e 0a 2a 2a 20 54 68 65 counted..** The
14270 20 63 68 61 6e 67 65 73 20 61 72 65 20 63 6f 75 changes are cou
14280 6e 74 65 64 20 61 73 20 73 6f 6f 6e 20 61 73 20 nted as soon as
14290 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 68 the statement th
142a0 61 74 20 6d 61 6b 65 73 20 74 68 65 6d 20 69 73 at makes them is
142b0 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 64 20 28 77 .** completed (w
142c0 68 65 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e hen the statemen
142d0 74 20 68 61 6e 64 6c 65 20 69 73 20 70 61 73 73 t handle is pass
142e0 65 64 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 ed to [sqlite3_r
142f0 65 73 65 74 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73 eset()] or.** [s
14300 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 qlite3_finalize(
14310 29 5d 29 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 )])..**.** See a
14320 6c 73 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 lso the [sqlite3
14330 5f 63 68 61 6e 67 65 73 28 29 5d 20 69 6e 74 65 _changes()] inte
14340 72 66 61 63 65 20 61 6e 64 20 74 68 65 0a 2a 2a rface and the.**
14350 20 5b 63 6f 75 6e 74 5f 63 68 61 6e 67 65 73 20 [count_changes
14360 70 72 61 67 6d 61 5d 2e 0a 2a 2a 0a 2a 2a 20 52 pragma]..**.** R
14370 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 equirements:.**
14380 5b 48 31 32 32 36 31 5d 20 5b 48 31 32 32 36 33 [H12261] [H12263
14390 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 73 65 70 ].**.** If a sep
143a0 61 72 61 74 65 20 74 68 72 65 61 64 20 6d 61 6b arate thread mak
143b0 65 73 20 63 68 61 6e 67 65 73 20 6f 6e 20 74 68 es changes on th
143c0 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 e same database
143d0 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 77 68 connection.** wh
143e0 69 6c 65 20 5b 73 71 6c 69 74 65 33 5f 74 6f 74 ile [sqlite3_tot
143f0 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d 20 69 73 al_changes()] is
14400 20 72 75 6e 6e 69 6e 67 20 74 68 65 6e 20 74 68 running then th
14410 65 20 76 61 6c 75 65 0a 2a 2a 20 72 65 74 75 72 e value.** retur
14420 6e 65 64 20 69 73 20 75 6e 70 72 65 64 69 63 74 ned is unpredict
14430 61 62 6c 65 20 61 6e 64 20 6e 6f 74 20 6d 65 61 able and not mea
14440 6e 69 6e 67 66 75 6c 2e 0a 2a 2f 0a 53 51 4c 49 ningful..*/.SQLI
14450 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
14460 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 e3_total_changes
14470 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a (sqlite3*);../*.
14480 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 49 6e 74 ** CAPI3REF: Int
14490 65 72 72 75 70 74 20 41 20 4c 6f 6e 67 2d 52 75 errupt A Long-Ru
144a0 6e 6e 69 6e 67 20 51 75 65 72 79 20 7b 48 31 32 nning Query {H12
144b0 32 37 30 7d 20 3c 53 33 30 35 30 30 3e 0a 2a 2a 270} <S30500>.**
144c0 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
144d0 6e 20 63 61 75 73 65 73 20 61 6e 79 20 70 65 6e n causes any pen
144e0 64 69 6e 67 20 64 61 74 61 62 61 73 65 20 6f 70 ding database op
144f0 65 72 61 74 69 6f 6e 20 74 6f 20 61 62 6f 72 74 eration to abort
14500 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 61 and.** return a
14510 74 20 69 74 73 20 65 61 72 6c 69 65 73 74 20 6f t its earliest o
14520 70 70 6f 72 74 75 6e 69 74 79 2e 20 54 68 69 73 pportunity. This
14530 20 72 6f 75 74 69 6e 65 20 69 73 20 74 79 70 69 routine is typi
14540 63 61 6c 6c 79 0a 2a 2a 20 63 61 6c 6c 65 64 20 cally.** called
14550 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 61 in response to a
14560 20 75 73 65 72 20 61 63 74 69 6f 6e 20 73 75 63 user action suc
14570 68 20 61 73 20 70 72 65 73 73 69 6e 67 20 22 43 h as pressing "C
14580 61 6e 63 65 6c 22 0a 2a 2a 20 6f 72 20 43 74 72 ancel".** or Ctr
14590 6c 2d 43 20 77 68 65 72 65 20 74 68 65 20 75 73 l-C where the us
145a0 65 72 20 77 61 6e 74 73 20 61 20 6c 6f 6e 67 20 er wants a long
145b0 71 75 65 72 79 20 6f 70 65 72 61 74 69 6f 6e 20 query operation
145c0 74 6f 20 68 61 6c 74 0a 2a 2a 20 69 6d 6d 65 64 to halt.** immed
145d0 69 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 49 74 iately..**.** It
145e0 20 69 73 20 73 61 66 65 20 74 6f 20 63 61 6c 6c is safe to call
145f0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66 72 this routine fr
14600 6f 6d 20 61 20 74 68 72 65 61 64 20 64 69 66 66 om a thread diff
14610 65 72 65 6e 74 20 66 72 6f 6d 20 74 68 65 0a 2a erent from the.*
14620 2a 20 74 68 72 65 61 64 20 74 68 61 74 20 69 73 * thread that is
14630 20 63 75 72 72 65 6e 74 6c 79 20 72 75 6e 6e 69 currently runni
14640 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ng the database
14650 6f 70 65 72 61 74 69 6f 6e 2e 20 20 42 75 74 20 operation. But
14660 69 74 0a 2a 2a 20 69 73 20 6e 6f 74 20 73 61 66 it.** is not saf
14670 65 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20 72 e to call this r
14680 6f 75 74 69 6e 65 20 77 69 74 68 20 61 20 5b 64 outine with a [d
14690 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
146a0 6f 6e 5d 20 74 68 61 74 0a 2a 2a 20 69 73 20 63 on] that.** is c
146b0 6c 6f 73 65 64 20 6f 72 20 6d 69 67 68 74 20 63 losed or might c
146c0 6c 6f 73 65 20 62 65 66 6f 72 65 20 73 71 6c 69 lose before sqli
146d0 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 te3_interrupt()
146e0 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 49 returns..**.** I
146f0 66 20 61 6e 20 53 51 4c 20 6f 70 65 72 61 74 69 f an SQL operati
14700 6f 6e 20 69 73 20 76 65 72 79 20 6e 65 61 72 6c on is very nearl
14710 79 20 66 69 6e 69 73 68 65 64 20 61 74 20 74 68 y finished at th
14720 65 20 74 69 6d 65 20 77 68 65 6e 0a 2a 2a 20 73 e time when.** s
14730 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 qlite3_interrupt
14740 28 29 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 () is called, th
14750 65 6e 20 69 74 20 6d 69 67 68 74 20 6e 6f 74 20 en it might not
14760 68 61 76 65 20 61 6e 20 6f 70 70 6f 72 74 75 6e have an opportun
14770 69 74 79 0a 2a 2a 20 74 6f 20 62 65 20 69 6e 74 ity.** to be int
14780 65 72 72 75 70 74 65 64 20 61 6e 64 20 6d 69 67 errupted and mig
14790 68 74 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 63 ht continue to c
147a0 6f 6d 70 6c 65 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a ompletion..**.**
147b0 20 41 6e 20 53 51 4c 20 6f 70 65 72 61 74 69 6f An SQL operatio
147c0 6e 20 74 68 61 74 20 69 73 20 69 6e 74 65 72 72 n that is interr
147d0 75 70 74 65 64 20 77 69 6c 6c 20 72 65 74 75 72 upted will retur
147e0 6e 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 n [SQLITE_INTERR
147f0 55 50 54 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 20 UPT]..** If the
14800 69 6e 74 65 72 72 75 70 74 65 64 20 53 51 4c 20 interrupted SQL
14810 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 61 6e 20 operation is an
14820 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c 20 INSERT, UPDATE,
14830 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20 74 68 61 or DELETE.** tha
14840 74 20 69 73 20 69 6e 73 69 64 65 20 61 6e 20 65 t is inside an e
14850 78 70 6c 69 63 69 74 20 74 72 61 6e 73 61 63 74 xplicit transact
14860 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 65 6e ion, then the en
14870 74 69 72 65 20 74 72 61 6e 73 61 63 74 69 6f 6e tire transaction
14880 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 72 6f 6c 6c .** will be roll
14890 65 64 20 62 61 63 6b 20 61 75 74 6f 6d 61 74 69 ed back automati
148a0 63 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 cally..**.** The
148b0 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 sqlite3_interru
148c0 70 74 28 44 29 20 63 61 6c 6c 20 69 73 20 69 6e pt(D) call is in
148d0 20 65 66 66 65 63 74 20 75 6e 74 69 6c 20 61 6c effect until al
148e0 6c 20 63 75 72 72 65 6e 74 6c 79 20 72 75 6e 6e l currently runn
148f0 69 6e 67 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 ing.** SQL state
14900 6d 65 6e 74 73 20 6f 6e 20 5b 64 61 74 61 62 61 ments on [databa
14910 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 se connection] D
14920 20 63 6f 6d 70 6c 65 74 65 2e 20 20 41 6e 79 20 complete. Any
14930 6e 65 77 20 53 51 4c 20 73 74 61 74 65 6d 65 6e new SQL statemen
14940 74 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 73 ts.** that are s
14950 74 61 72 74 65 64 20 61 66 74 65 72 20 74 68 65 tarted after the
14960 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 sqlite3_interru
14970 70 74 28 29 20 63 61 6c 6c 20 61 6e 64 20 62 65 pt() call and be
14980 66 6f 72 65 20 74 68 65 20 0a 2a 2a 20 72 75 6e fore the .** run
14990 6e 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 73 20 ning statements
149a0 72 65 61 63 68 65 73 20 7a 65 72 6f 20 61 72 65 reaches zero are
149b0 20 69 6e 74 65 72 72 75 70 74 65 64 20 61 73 20 interrupted as
149c0 69 66 20 74 68 65 79 20 68 61 64 20 62 65 65 6e if they had been
149d0 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 70 72 69 6f .** running prio
149e0 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 r to the sqlite3
149f0 5f 69 6e 74 65 72 72 75 70 74 28 29 20 63 61 6c _interrupt() cal
14a00 6c 2e 20 20 4e 65 77 20 53 51 4c 20 73 74 61 74 l. New SQL stat
14a10 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61 ements.** that a
14a20 72 65 20 73 74 61 72 74 65 64 20 61 66 74 65 72 re started after
14a30 20 74 68 65 20 72 75 6e 6e 69 6e 67 20 73 74 61 the running sta
14a40 74 65 6d 65 6e 74 20 63 6f 75 6e 74 20 72 65 61 tement count rea
14a50 63 68 65 73 20 7a 65 72 6f 20 61 72 65 0a 2a 2a ches zero are.**
14a60 20 6e 6f 74 20 65 66 66 65 63 74 65 64 20 62 79 not effected by
14a70 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 the sqlite3_int
14a80 65 72 72 75 70 74 28 29 2e 0a 2a 2a 20 41 20 63 errupt()..** A c
14a90 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 69 all to sqlite3_i
14aa0 6e 74 65 72 72 75 70 74 28 44 29 20 74 68 61 74 nterrupt(D) that
14ab0 20 6f 63 63 75 72 73 20 77 68 65 6e 20 74 68 65 occurs when the
14ac0 72 65 20 61 72 65 20 6e 6f 20 72 75 6e 6e 69 6e re are no runnin
14ad0 67 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 g.** SQL stateme
14ae0 6e 74 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 61 nts is a no-op a
14af0 6e 64 20 68 61 73 20 6e 6f 20 65 66 66 65 63 74 nd has no effect
14b00 20 6f 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e on SQL statemen
14b10 74 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 73 ts.** that are s
14b20 74 61 72 74 65 64 20 61 66 74 65 72 20 74 68 65 tarted after the
14b30 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 sqlite3_interru
14b40 70 74 28 29 20 63 61 6c 6c 20 72 65 74 75 72 6e pt() call return
14b50 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 s..**.** Require
14b60 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 37 ments:.** [H1227
14b70 31 5d 20 5b 48 31 32 32 37 32 5d 0a 2a 2a 0a 2a 1] [H12272].**.*
14b80 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 * If the databas
14b90 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 6c 6f e connection clo
14ba0 73 65 73 20 77 68 69 6c 65 20 5b 73 71 6c 69 74 ses while [sqlit
14bb0 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 5d 0a e3_interrupt()].
14bc0 2a 2a 20 69 73 20 72 75 6e 6e 69 6e 67 20 74 68 ** is running th
14bd0 65 6e 20 62 61 64 20 74 68 69 6e 67 73 20 77 69 en bad things wi
14be0 6c 6c 20 6c 69 6b 65 6c 79 20 68 61 70 70 65 6e ll likely happen
14bf0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
14c00 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 69 6e 74 void sqlite3_int
14c10 65 72 72 75 70 74 28 73 71 6c 69 74 65 33 2a 29 errupt(sqlite3*)
14c20 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
14c30 46 3a 20 44 65 74 65 72 6d 69 6e 65 20 49 66 20 F: Determine If
14c40 41 6e 20 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 An SQL Statement
14c50 20 49 73 20 43 6f 6d 70 6c 65 74 65 20 7b 48 31 Is Complete {H1
14c60 30 35 31 30 7d 20 3c 53 37 30 32 30 30 3e 0a 2a 0510} <S70200>.*
14c70 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 *.** These routi
14c80 6e 65 73 20 61 72 65 20 75 73 65 66 75 6c 20 64 nes are useful d
14c90 75 72 69 6e 67 20 63 6f 6d 6d 61 6e 64 2d 6c 69 uring command-li
14ca0 6e 65 20 69 6e 70 75 74 20 74 6f 20 64 65 74 65 ne input to dete
14cb0 72 6d 69 6e 65 20 69 66 20 74 68 65 0a 2a 2a 20 rmine if the.**
14cc0 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 currently entere
14cd0 64 20 74 65 78 74 20 73 65 65 6d 73 20 74 6f 20 d text seems to
14ce0 66 6f 72 6d 20 61 20 63 6f 6d 70 6c 65 74 65 20 form a complete
14cf0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 6f 72 SQL statement or
14d00 0a 2a 2a 20 69 66 20 61 64 64 69 74 69 6f 6e 61 .** if additiona
14d10 6c 20 69 6e 70 75 74 20 69 73 20 6e 65 65 64 65 l input is neede
14d20 64 20 62 65 66 6f 72 65 20 73 65 6e 64 69 6e 67 d before sending
14d30 20 74 68 65 20 74 65 78 74 20 69 6e 74 6f 0a 2a the text into.*
14d40 2a 20 53 51 4c 69 74 65 20 66 6f 72 20 70 61 72 * SQLite for par
14d50 73 69 6e 67 2e 20 20 54 68 65 73 65 20 72 6f 75 sing. These rou
14d60 74 69 6e 65 73 20 72 65 74 75 72 6e 20 31 20 69 tines return 1 i
14d70 66 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 f the input stri
14d80 6e 67 0a 2a 2a 20 61 70 70 65 61 72 73 20 74 6f ng.** appears to
14d90 20 62 65 20 61 20 63 6f 6d 70 6c 65 74 65 20 53 be a complete S
14da0 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 41 QL statement. A
14db0 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6a 75 statement is ju
14dc0 64 67 65 64 20 74 6f 20 62 65 0a 2a 2a 20 63 6f dged to be.** co
14dd0 6d 70 6c 65 74 65 20 69 66 20 69 74 20 65 6e 64 mplete if it end
14de0 73 20 77 69 74 68 20 61 20 73 65 6d 69 63 6f 6c s with a semicol
14df0 6f 6e 20 74 6f 6b 65 6e 20 61 6e 64 20 69 73 20 on token and is
14e00 6e 6f 74 20 61 20 70 72 65 66 69 78 20 6f 66 20 not a prefix of
14e10 61 0a 2a 2a 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 a.** well-formed
14e20 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20 CREATE TRIGGER
14e30 73 74 61 74 65 6d 65 6e 74 2e 20 20 53 65 6d 69 statement. Semi
14e40 63 6f 6c 6f 6e 73 20 74 68 61 74 20 61 72 65 20 colons that are
14e50 65 6d 62 65 64 64 65 64 20 77 69 74 68 69 6e 0a embedded within.
14e60 2a 2a 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61 ** string litera
14e70 6c 73 20 6f 72 20 71 75 6f 74 65 64 20 69 64 65 ls or quoted ide
14e80 6e 74 69 66 69 65 72 20 6e 61 6d 65 73 20 6f 72 ntifier names or
14e90 20 63 6f 6d 6d 65 6e 74 73 20 61 72 65 20 6e 6f comments are no
14ea0 74 0a 2a 2a 20 69 6e 64 65 70 65 6e 64 65 6e 74 t.** independent
14eb0 20 74 6f 6b 65 6e 73 20 28 74 68 65 79 20 61 72 tokens (they ar
14ec0 65 20 70 61 72 74 20 6f 66 20 74 68 65 20 74 6f e part of the to
14ed0 6b 65 6e 20 69 6e 20 77 68 69 63 68 20 74 68 65 ken in which the
14ee0 79 20 61 72 65 0a 2a 2a 20 65 6d 62 65 64 64 65 y are.** embedde
14ef0 64 29 20 61 6e 64 20 74 68 75 73 20 64 6f 20 6e d) and thus do n
14f00 6f 74 20 63 6f 75 6e 74 20 61 73 20 61 20 73 74 ot count as a st
14f10 61 74 65 6d 65 6e 74 20 74 65 72 6d 69 6e 61 74 atement terminat
14f20 6f 72 2e 20 20 57 68 69 74 65 73 70 61 63 65 0a or. Whitespace.
14f30 2a 2a 20 61 6e 64 20 63 6f 6d 6d 65 6e 74 73 20 ** and comments
14f40 74 68 61 74 20 66 6f 6c 6c 6f 77 20 74 68 65 20 that follow the
14f50 66 69 6e 61 6c 20 73 65 6d 69 63 6f 6c 6f 6e 20 final semicolon
14f60 61 72 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a are ignored..**.
14f70 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 ** These routine
14f80 73 20 72 65 74 75 72 6e 20 30 20 69 66 20 74 68 s return 0 if th
14f90 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 69 e statement is i
14fa0 6e 63 6f 6d 70 6c 65 74 65 2e 20 20 49 66 20 61 ncomplete. If a
14fb0 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 .** memory alloc
14fc0 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 74 68 65 ation fails, the
14fd0 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 n SQLITE_NOMEM i
14fe0 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a s returned..**.*
14ff0 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 * These routines
15000 20 64 6f 20 6e 6f 74 20 70 61 72 73 65 20 74 68 do not parse th
15010 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 e SQL statements
15020 20 74 68 75 73 0a 2a 2a 20 77 69 6c 6c 20 6e 6f thus.** will no
15030 74 20 64 65 74 65 63 74 20 73 79 6e 74 61 63 74 t detect syntact
15040 69 63 61 6c 6c 79 20 69 6e 63 6f 72 72 65 63 74 ically incorrect
15050 20 53 51 4c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 SQL..**.** If S
15060 51 4c 69 74 65 20 68 61 73 20 6e 6f 74 20 62 65 QLite has not be
15070 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 75 en initialized u
15080 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 69 6e sing [sqlite3_in
15090 69 74 69 61 6c 69 7a 65 28 29 5d 20 70 72 69 6f itialize()] prio
150a0 72 20 0a 2a 2a 20 74 6f 20 69 6e 76 6f 6b 69 6e r .** to invokin
150b0 67 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 g sqlite3_comple
150c0 74 65 31 36 28 29 20 74 68 65 6e 20 73 71 6c 69 te16() then sqli
150d0 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 te3_initialize()
150e0 20 69 73 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20 61 is invoked.** a
150f0 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 62 79 20 utomatically by
15100 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 sqlite3_complete
15110 31 36 28 29 2e 20 20 49 66 20 74 68 61 74 20 69 16(). If that i
15120 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 66 61 nitialization fa
15130 69 6c 73 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 ils,.** then the
15140 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72 return value fr
15150 6f 6d 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c om sqlite3_compl
15160 65 74 65 31 36 28 29 20 77 69 6c 6c 20 62 65 20 ete16() will be
15170 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 72 65 67 61 non-zero.** rega
15180 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65 rdless of whethe
15190 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 69 6e 70 r or not the inp
151a0 75 74 20 53 51 4c 20 69 73 20 63 6f 6d 70 6c 65 ut SQL is comple
151b0 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 te..**.** Requir
151c0 65 6d 65 6e 74 73 3a 20 5b 48 31 30 35 31 31 5d ements: [H10511]
151d0 20 5b 48 31 30 35 31 32 5d 0a 2a 2a 0a 2a 2a 20 [H10512].**.**
151e0 54 68 65 20 69 6e 70 75 74 20 74 6f 20 5b 73 71 The input to [sq
151f0 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 29 lite3_complete()
15200 5d 20 6d 75 73 74 20 62 65 20 61 20 7a 65 72 6f ] must be a zero
15210 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 55 -terminated.** U
15220 54 46 2d 38 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a TF-8 string..**.
15230 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 74 6f 20 ** The input to
15240 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 [sqlite3_complet
15250 65 31 36 28 29 5d 20 6d 75 73 74 20 62 65 20 61 e16()] must be a
15260 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 zero-terminated
15270 0a 2a 2a 20 55 54 46 2d 31 36 20 73 74 72 69 6e .** UTF-16 strin
15280 67 20 69 6e 20 6e 61 74 69 76 65 20 62 79 74 65 g in native byte
15290 20 6f 72 64 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 order..*/.SQLIT
152a0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
152b0 33 5f 63 6f 6d 70 6c 65 74 65 28 63 6f 6e 73 74 3_complete(const
152c0 20 63 68 61 72 20 2a 73 71 6c 29 3b 0a 53 51 4c char *sql);.SQL
152d0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
152e0 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 63 te3_complete16(c
152f0 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 29 3b onst void *sql);
15300 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
15310 3a 20 52 65 67 69 73 74 65 72 20 41 20 43 61 6c : Register A Cal
15320 6c 62 61 63 6b 20 54 6f 20 48 61 6e 64 6c 65 20 lback To Handle
15330 53 51 4c 49 54 45 5f 42 55 53 59 20 45 72 72 6f SQLITE_BUSY Erro
15340 72 73 20 7b 48 31 32 33 31 30 7d 20 3c 53 34 30 rs {H12310} <S40
15350 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 400>.**.** This
15360 72 6f 75 74 69 6e 65 20 73 65 74 73 20 61 20 63 routine sets a c
15370 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e allback function
15380 20 74 68 61 74 20 6d 69 67 68 74 20 62 65 20 69 that might be i
15390 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76 65 72 0a nvoked whenever.
153a0 2a 2a 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 ** an attempt is
153b0 20 6d 61 64 65 20 74 6f 20 6f 70 65 6e 20 61 20 made to open a
153c0 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 74 database table t
153d0 68 61 74 20 61 6e 6f 74 68 65 72 20 74 68 72 65 hat another thre
153e0 61 64 0a 2a 2a 20 6f 72 20 70 72 6f 63 65 73 73 ad.** or process
153f0 20 68 61 73 20 6c 6f 63 6b 65 64 2e 0a 2a 2a 0a has locked..**.
15400 2a 2a 20 49 66 20 74 68 65 20 62 75 73 79 20 63 ** If the busy c
15410 61 6c 6c 62 61 63 6b 20 69 73 20 4e 55 4c 4c 2c allback is NULL,
15420 20 74 68 65 6e 20 5b 53 51 4c 49 54 45 5f 42 55 then [SQLITE_BU
15430 53 59 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 49 SY] or [SQLITE_I
15440 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 0a 2a 2a OERR_BLOCKED].**
15450 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6d 6d is returned imm
15460 65 64 69 61 74 65 6c 79 20 75 70 6f 6e 20 65 6e ediately upon en
15470 63 6f 75 6e 74 65 72 69 6e 67 20 74 68 65 20 6c countering the l
15480 6f 63 6b 2e 20 49 66 20 74 68 65 20 62 75 73 79 ock. If the busy
15490 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73 20 callback.** is
154a0 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 not NULL, then t
154b0 68 65 20 63 61 6c 6c 62 61 63 6b 20 77 69 6c 6c he callback will
154c0 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 69 74 68 be invoked with
154d0 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 2e 0a two arguments..
154e0 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 **.** The first
154f0 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 argument to the
15500 68 61 6e 64 6c 65 72 20 69 73 20 61 20 63 6f 70 handler is a cop
15510 79 20 6f 66 20 74 68 65 20 76 6f 69 64 2a 20 70 y of the void* p
15520 6f 69 6e 74 65 72 20 77 68 69 63 68 0a 2a 2a 20 ointer which.**
15530 69 73 20 74 68 65 20 74 68 69 72 64 20 61 72 67 is the third arg
15540 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 ument to sqlite3
15550 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 29 2e _busy_handler().
15560 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 The second arg
15570 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 74 68 65 20 ument to.** the
15580 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62 61 63 6b handler callback
15590 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f is the number o
155a0 66 20 74 69 6d 65 73 20 74 68 61 74 20 74 68 65 f times that the
155b0 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 68 61 busy handler ha
155c0 73 0a 2a 2a 20 62 65 65 6e 20 69 6e 76 6f 6b 65 s.** been invoke
155d0 64 20 66 6f 72 20 74 68 69 73 20 6c 6f 63 6b 69 d for this locki
155e0 6e 67 20 65 76 65 6e 74 2e 20 20 49 66 20 74 68 ng event. If th
155f0 65 0a 2a 2a 20 62 75 73 79 20 63 61 6c 6c 62 61 e.** busy callba
15600 63 6b 20 72 65 74 75 72 6e 73 20 30 2c 20 74 68 ck returns 0, th
15610 65 6e 20 6e 6f 20 61 64 64 69 74 69 6f 6e 61 6c en no additional
15620 20 61 74 74 65 6d 70 74 73 20 61 72 65 20 6d 61 attempts are ma
15630 64 65 20 74 6f 0a 2a 2a 20 61 63 63 65 73 73 20 de to.** access
15640 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6e 64 the database and
15650 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6f [SQLITE_BUSY] o
15660 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f r [SQLITE_IOERR_
15670 42 4c 4f 43 4b 45 44 5d 20 69 73 20 72 65 74 75 BLOCKED] is retu
15680 72 6e 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 rned..** If the
15690 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 callback returns
156a0 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 non-zero, then
156b0 61 6e 6f 74 68 65 72 20 61 74 74 65 6d 70 74 0a another attempt.
156c0 2a 2a 20 69 73 20 6d 61 64 65 20 74 6f 20 6f 70 ** is made to op
156d0 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 en the database
156e0 66 6f 72 20 72 65 61 64 69 6e 67 20 61 6e 64 20 for reading and
156f0 74 68 65 20 63 79 63 6c 65 20 72 65 70 65 61 74 the cycle repeat
15700 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 65 s..**.** The pre
15710 73 65 6e 63 65 20 6f 66 20 61 20 62 75 73 79 20 sence of a busy
15720 68 61 6e 64 6c 65 72 20 64 6f 65 73 20 6e 6f 74 handler does not
15730 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 guarantee that
15740 69 74 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b it will be invok
15750 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 72 65 ed.** when there
15760 20 69 73 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74 is lock content
15770 69 6f 6e 2e 20 49 66 20 53 51 4c 69 74 65 20 64 ion. If SQLite d
15780 65 74 65 72 6d 69 6e 65 73 20 74 68 61 74 20 69 etermines that i
15790 6e 76 6f 6b 69 6e 67 20 74 68 65 20 62 75 73 79 nvoking the busy
157a0 0a 2a 2a 20 68 61 6e 64 6c 65 72 20 63 6f 75 6c .** handler coul
157b0 64 20 72 65 73 75 6c 74 20 69 6e 20 61 20 64 65 d result in a de
157c0 61 64 6c 6f 63 6b 2c 20 69 74 20 77 69 6c 6c 20 adlock, it will
157d0 67 6f 20 61 68 65 61 64 20 61 6e 64 20 72 65 74 go ahead and ret
157e0 75 72 6e 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 urn [SQLITE_BUSY
157f0 5d 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49 54 45 5f ].** or [SQLITE_
15800 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 20 69 IOERR_BLOCKED] i
15810 6e 73 74 65 61 64 20 6f 66 20 69 6e 76 6f 6b 69 nstead of invoki
15820 6e 67 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 ng the busy hand
15830 6c 65 72 2e 0a 2a 2a 20 43 6f 6e 73 69 64 65 72 ler..** Consider
15840 20 61 20 73 63 65 6e 61 72 69 6f 20 77 68 65 72 a scenario wher
15850 65 20 6f 6e 65 20 70 72 6f 63 65 73 73 20 69 73 e one process is
15860 20 68 6f 6c 64 69 6e 67 20 61 20 72 65 61 64 20 holding a read
15870 6c 6f 63 6b 20 74 68 61 74 0a 2a 2a 20 69 74 20 lock that.** it
15880 69 73 20 74 72 79 69 6e 67 20 74 6f 20 70 72 6f is trying to pro
15890 6d 6f 74 65 20 74 6f 20 61 20 72 65 73 65 72 76 mote to a reserv
158a0 65 64 20 6c 6f 63 6b 20 61 6e 64 0a 2a 2a 20 61 ed lock and.** a
158b0 20 73 65 63 6f 6e 64 20 70 72 6f 63 65 73 73 20 second process
158c0 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 72 65 73 is holding a res
158d0 65 72 76 65 64 20 6c 6f 63 6b 20 74 68 61 74 20 erved lock that
158e0 69 74 20 69 73 20 74 72 79 69 6e 67 0a 2a 2a 20 it is trying.**
158f0 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f 20 61 6e to promote to an
15900 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e exclusive lock.
15910 20 20 54 68 65 20 66 69 72 73 74 20 70 72 6f 63 The first proc
15920 65 73 73 20 63 61 6e 6e 6f 74 20 70 72 6f 63 65 ess cannot proce
15930 65 64 0a 2a 2a 20 62 65 63 61 75 73 65 20 69 74 ed.** because it
15940 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 79 20 74 is blocked by t
15950 68 65 20 73 65 63 6f 6e 64 20 61 6e 64 20 74 68 he second and th
15960 65 20 73 65 63 6f 6e 64 20 70 72 6f 63 65 73 73 e second process
15970 20 63 61 6e 6e 6f 74 0a 2a 2a 20 70 72 6f 63 65 cannot.** proce
15980 65 64 20 62 65 63 61 75 73 65 20 69 74 20 69 73 ed because it is
15990 20 62 6c 6f 63 6b 65 64 20 62 79 20 74 68 65 20 blocked by the
159a0 66 69 72 73 74 2e 20 20 49 66 20 62 6f 74 68 20 first. If both
159b0 70 72 6f 63 65 73 73 65 73 0a 2a 2a 20 69 6e 76 processes.** inv
159c0 6f 6b 65 20 74 68 65 20 62 75 73 79 20 68 61 6e oke the busy han
159d0 64 6c 65 72 73 2c 20 6e 65 69 74 68 65 72 20 77 dlers, neither w
159e0 69 6c 6c 20 6d 61 6b 65 20 61 6e 79 20 70 72 6f ill make any pro
159f0 67 72 65 73 73 2e 20 20 54 68 65 72 65 66 6f 72 gress. Therefor
15a00 65 2c 0a 2a 2a 20 53 51 4c 69 74 65 20 72 65 74 e,.** SQLite ret
15a10 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 42 55 53 urns [SQLITE_BUS
15a20 59 5d 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 Y] for the first
15a30 20 70 72 6f 63 65 73 73 2c 20 68 6f 70 69 6e 67 process, hoping
15a40 20 74 68 61 74 20 74 68 69 73 0a 2a 2a 20 77 69 that this.** wi
15a50 6c 6c 20 69 6e 64 75 63 65 20 74 68 65 20 66 69 ll induce the fi
15a60 72 73 74 20 70 72 6f 63 65 73 73 20 74 6f 20 72 rst process to r
15a70 65 6c 65 61 73 65 20 69 74 73 20 72 65 61 64 20 elease its read
15a80 6c 6f 63 6b 20 61 6e 64 20 61 6c 6c 6f 77 0a 2a lock and allow.*
15a90 2a 20 74 68 65 20 73 65 63 6f 6e 64 20 70 72 6f * the second pro
15aa0 63 65 73 73 20 74 6f 20 70 72 6f 63 65 65 64 2e cess to proceed.
15ab0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 .**.** The defau
15ac0 6c 74 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b lt busy callback
15ad0 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 is NULL..**.**
15ae0 54 68 65 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 The [SQLITE_BUSY
15af0 5d 20 65 72 72 6f 72 20 69 73 20 63 6f 6e 76 65 ] error is conve
15b00 72 74 65 64 20 74 6f 20 5b 53 51 4c 49 54 45 5f rted to [SQLITE_
15b10 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 0a 2a IOERR_BLOCKED].*
15b20 2a 20 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 * when SQLite is
15b30 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f in the middle o
15b40 66 20 61 20 6c 61 72 67 65 20 74 72 61 6e 73 61 f a large transa
15b50 63 74 69 6f 6e 20 77 68 65 72 65 20 61 6c 6c 20 ction where all
15b60 74 68 65 0a 2a 2a 20 63 68 61 6e 67 65 73 20 77 the.** changes w
15b70 69 6c 6c 20 6e 6f 74 20 66 69 74 20 69 6e 74 6f ill not fit into
15b80 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 the in-memory c
15b90 61 63 68 65 2e 20 20 53 51 4c 69 74 65 20 77 69 ache. SQLite wi
15ba0 6c 6c 0a 2a 2a 20 61 6c 72 65 61 64 79 20 68 6f ll.** already ho
15bb0 6c 64 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f ld a RESERVED lo
15bc0 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 ck on the databa
15bd0 73 65 20 66 69 6c 65 2c 20 62 75 74 20 69 74 20 se file, but it
15be0 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 70 72 6f 6d needs.** to prom
15bf0 6f 74 65 20 74 68 69 73 20 6c 6f 63 6b 20 74 6f ote this lock to
15c00 20 45 58 43 4c 55 53 49 56 45 20 73 6f 20 74 68 EXCLUSIVE so th
15c10 61 74 20 69 74 20 63 61 6e 20 73 70 69 6c 6c 20 at it can spill
15c20 63 61 63 68 65 0a 2a 2a 20 70 61 67 65 73 20 69 cache.** pages i
15c30 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 nto the database
15c40 20 66 69 6c 65 20 77 69 74 68 6f 75 74 20 68 61 file without ha
15c50 72 6d 20 74 6f 20 63 6f 6e 63 75 72 72 65 6e 74 rm to concurrent
15c60 0a 2a 2a 20 72 65 61 64 65 72 73 2e 20 20 49 66 .** readers. If
15c70 20 69 74 20 69 73 20 75 6e 61 62 6c 65 20 74 6f it is unable to
15c80 20 70 72 6f 6d 6f 74 65 20 74 68 65 20 6c 6f 63 promote the loc
15c90 6b 2c 20 74 68 65 6e 20 74 68 65 20 69 6e 2d 6d k, then the in-m
15ca0 65 6d 6f 72 79 0a 2a 2a 20 63 61 63 68 65 20 77 emory.** cache w
15cb0 69 6c 6c 20 62 65 20 6c 65 66 74 20 69 6e 20 61 ill be left in a
15cc0 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73 n inconsistent s
15cd0 74 61 74 65 20 61 6e 64 20 73 6f 20 74 68 65 20 tate and so the
15ce0 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 69 73 error.** code is
15cf0 20 70 72 6f 6d 6f 74 65 64 20 66 72 6f 6d 20 74 promoted from t
15d00 68 65 20 72 65 6c 61 74 69 76 65 6c 79 20 62 65 he relatively be
15d10 6e 69 67 6e 20 5b 53 51 4c 49 54 45 5f 42 55 53 nign [SQLITE_BUS
15d20 59 5d 20 74 6f 0a 2a 2a 20 74 68 65 20 6d 6f 72 Y] to.** the mor
15d30 65 20 73 65 76 65 72 65 20 5b 53 51 4c 49 54 45 e severe [SQLITE
15d40 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 2e _IOERR_BLOCKED].
15d50 20 20 54 68 69 73 20 65 72 72 6f 72 20 63 6f 64 This error cod
15d60 65 20 70 72 6f 6d 6f 74 69 6f 6e 0a 2a 2a 20 66 e promotion.** f
15d70 6f 72 63 65 73 20 61 6e 20 61 75 74 6f 6d 61 74 orces an automat
15d80 69 63 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 74 ic rollback of t
15d90 68 65 20 63 68 61 6e 67 65 73 2e 20 20 53 65 65 he changes. See
15da0 20 74 68 65 0a 2a 2a 20 3c 61 20 68 72 65 66 3d the.** <a href=
15db0 22 2f 63 76 73 74 72 61 63 2f 77 69 6b 69 3f 70 "/cvstrac/wiki?p
15dc0 3d 43 6f 72 72 75 70 74 69 6f 6e 46 6f 6c 6c 6f =CorruptionFollo
15dd0 77 69 6e 67 42 75 73 79 45 72 72 6f 72 22 3e 0a wingBusyError">.
15de0 2a 2a 20 43 6f 72 72 75 70 74 69 6f 6e 46 6f 6c ** CorruptionFol
15df0 6c 6f 77 69 6e 67 42 75 73 79 45 72 72 6f 72 3c lowingBusyError<
15e00 2f 61 3e 20 77 69 6b 69 20 70 61 67 65 20 66 6f /a> wiki page fo
15e10 72 20 61 20 64 69 73 63 75 73 73 69 6f 6e 20 6f r a discussion o
15e20 66 20 77 68 79 0a 2a 2a 20 74 68 69 73 20 69 73 f why.** this is
15e30 20 69 6d 70 6f 72 74 61 6e 74 2e 0a 2a 2a 0a 2a important..**.*
15e40 2a 20 54 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79 * There can only
15e50 20 62 65 20 61 20 73 69 6e 67 6c 65 20 62 75 73 be a single bus
15e60 79 20 68 61 6e 64 6c 65 72 20 64 65 66 69 6e 65 y handler define
15e70 64 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 5b 64 d for each.** [d
15e80 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
15e90 6f 6e 5d 2e 20 20 53 65 74 74 69 6e 67 20 61 20 on]. Setting a
15ea0 6e 65 77 20 62 75 73 79 20 68 61 6e 64 6c 65 72 new busy handler
15eb0 20 63 6c 65 61 72 73 20 61 6e 79 0a 2a 2a 20 70 clears any.** p
15ec0 72 65 76 69 6f 75 73 6c 79 20 73 65 74 20 68 61 reviously set ha
15ed0 6e 64 6c 65 72 2e 20 20 4e 6f 74 65 20 74 68 61 ndler. Note tha
15ee0 74 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 t calling [sqlit
15ef0 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 28 e3_busy_timeout(
15f00 29 5d 0a 2a 2a 20 77 69 6c 6c 20 61 6c 73 6f 20 )].** will also
15f10 73 65 74 20 6f 72 20 63 6c 65 61 72 20 74 68 65 set or clear the
15f20 20 62 75 73 79 20 68 61 6e 64 6c 65 72 2e 0a 2a busy handler..*
15f30 2a 0a 2a 2a 20 54 68 65 20 62 75 73 79 20 63 61 *.** The busy ca
15f40 6c 6c 62 61 63 6b 20 73 68 6f 75 6c 64 20 6e 6f llback should no
15f50 74 20 74 61 6b 65 20 61 6e 79 20 61 63 74 69 6f t take any actio
15f60 6e 73 20 77 68 69 63 68 20 6d 6f 64 69 66 79 20 ns which modify
15f70 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 the.** database
15f80 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 connection that
15f90 69 6e 76 6f 6b 65 64 20 74 68 65 20 62 75 73 79 invoked the busy
15fa0 20 68 61 6e 64 6c 65 72 2e 20 20 41 6e 79 20 73 handler. Any s
15fb0 75 63 68 20 61 63 74 69 6f 6e 73 0a 2a 2a 20 72 uch actions.** r
15fc0 65 73 75 6c 74 20 69 6e 20 75 6e 64 65 66 69 6e esult in undefin
15fd0 65 64 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 20 ed behavior..**
15fe0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
15ff0 3a 0a 2a 2a 20 5b 48 31 32 33 31 31 5d 20 5b 48 :.** [H12311] [H
16000 31 32 33 31 32 5d 20 5b 48 31 32 33 31 34 5d 20 12312] [H12314]
16010 5b 48 31 32 33 31 36 5d 20 5b 48 31 32 33 31 38 [H12316] [H12318
16020 5d 0a 2a 2a 0a 2a 2a 20 41 20 62 75 73 79 20 68 ].**.** A busy h
16030 61 6e 64 6c 65 72 20 6d 75 73 74 20 6e 6f 74 20 andler must not
16040 63 6c 6f 73 65 20 74 68 65 20 64 61 74 61 62 61 close the databa
16050 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a se connection.**
16060 20 6f 72 20 5b 70 72 65 70 61 72 65 64 20 73 74 or [prepared st
16070 61 74 65 6d 65 6e 74 5d 20 74 68 61 74 20 69 6e atement] that in
16080 76 6f 6b 65 64 20 74 68 65 20 62 75 73 79 20 68 voked the busy h
16090 61 6e 64 6c 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 andler..*/.SQLIT
160a0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
160b0 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 73 3_busy_handler(s
160c0 71 6c 69 74 65 33 2a 2c 20 69 6e 74 28 2a 29 28 qlite3*, int(*)(
160d0 76 6f 69 64 2a 2c 69 6e 74 29 2c 20 76 6f 69 64 void*,int), void
160e0 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 *);../*.** CAPI3
160f0 52 45 46 3a 20 53 65 74 20 41 20 42 75 73 79 20 REF: Set A Busy
16100 54 69 6d 65 6f 75 74 20 7b 48 31 32 33 34 30 7d Timeout {H12340}
16110 20 3c 53 34 30 34 31 30 3e 0a 2a 2a 0a 2a 2a 20 <S40410>.**.**
16120 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 This routine set
16130 73 20 61 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 s a [sqlite3_bus
16140 79 5f 68 61 6e 64 6c 65 72 20 7c 20 62 75 73 79 y_handler | busy
16150 20 68 61 6e 64 6c 65 72 5d 20 74 68 61 74 20 73 handler] that s
16160 6c 65 65 70 73 0a 2a 2a 20 66 6f 72 20 61 20 73 leeps.** for a s
16170 70 65 63 69 66 69 65 64 20 61 6d 6f 75 6e 74 20 pecified amount
16180 6f 66 20 74 69 6d 65 20 77 68 65 6e 20 61 20 74 of time when a t
16190 61 62 6c 65 20 69 73 20 6c 6f 63 6b 65 64 2e 20 able is locked.
161a0 20 54 68 65 20 68 61 6e 64 6c 65 72 0a 2a 2a 20 The handler.**
161b0 77 69 6c 6c 20 73 6c 65 65 70 20 6d 75 6c 74 69 will sleep multi
161c0 70 6c 65 20 74 69 6d 65 73 20 75 6e 74 69 6c 20 ple times until
161d0 61 74 20 6c 65 61 73 74 20 22 6d 73 22 20 6d 69 at least "ms" mi
161e0 6c 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c lliseconds of sl
161f0 65 65 70 69 6e 67 0a 2a 2a 20 68 61 76 65 20 61 eeping.** have a
16200 63 63 75 6d 75 6c 61 74 65 64 2e 20 7b 48 31 32 ccumulated. {H12
16210 33 34 33 7d 20 41 66 74 65 72 20 22 6d 73 22 20 343} After "ms"
16220 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20 milliseconds of
16230 73 6c 65 65 70 69 6e 67 2c 0a 2a 2a 20 74 68 65 sleeping,.** the
16240 20 68 61 6e 64 6c 65 72 20 72 65 74 75 72 6e 73 handler returns
16250 20 30 20 77 68 69 63 68 20 63 61 75 73 65 73 20 0 which causes
16260 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d [sqlite3_step()]
16270 20 74 6f 20 72 65 74 75 72 6e 0a 2a 2a 20 5b 53 to return.** [S
16280 51 4c 49 54 45 5f 42 55 53 59 5d 20 6f 72 20 5b QLITE_BUSY] or [
16290 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f SQLITE_IOERR_BLO
162a0 43 4b 45 44 5d 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c CKED]..**.** Cal
162b0 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e ling this routin
162c0 65 20 77 69 74 68 20 61 6e 20 61 72 67 75 6d 65 e with an argume
162d0 6e 74 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 nt less than or
162e0 65 71 75 61 6c 20 74 6f 20 7a 65 72 6f 0a 2a 2a equal to zero.**
162f0 20 74 75 72 6e 73 20 6f 66 66 20 61 6c 6c 20 62 turns off all b
16300 75 73 79 20 68 61 6e 64 6c 65 72 73 2e 0a 2a 2a usy handlers..**
16310 0a 2a 2a 20 54 68 65 72 65 20 63 61 6e 20 6f 6e .** There can on
16320 6c 79 20 62 65 20 61 20 73 69 6e 67 6c 65 20 62 ly be a single b
16330 75 73 79 20 68 61 6e 64 6c 65 72 20 66 6f 72 20 usy handler for
16340 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 a particular.**
16350 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
16360 74 69 6f 6e 5d 20 61 6e 79 20 61 6e 79 20 67 69 tion] any any gi
16370 76 65 6e 20 6d 6f 6d 65 6e 74 2e 20 20 49 66 20 ven moment. If
16380 61 6e 6f 74 68 65 72 20 62 75 73 79 20 68 61 6e another busy han
16390 64 6c 65 72 0a 2a 2a 20 77 61 73 20 64 65 66 69 dler.** was defi
163a0 6e 65 64 20 20 28 75 73 69 6e 67 20 5b 73 71 6c ned (using [sql
163b0 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 ite3_busy_handle
163c0 72 28 29 5d 29 20 70 72 69 6f 72 20 74 6f 20 63 r()]) prior to c
163d0 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 69 73 20 72 alling.** this r
163e0 6f 75 74 69 6e 65 2c 20 74 68 61 74 20 6f 74 68 outine, that oth
163f0 65 72 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 er busy handler
16400 69 73 20 63 6c 65 61 72 65 64 2e 0a 2a 2a 0a 2a is cleared..**.*
16410 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
16420 2a 2a 20 5b 48 31 32 33 34 31 5d 20 5b 48 31 32 ** [H12341] [H12
16430 33 34 33 5d 20 5b 48 31 32 33 34 34 5d 0a 2a 2f 343] [H12344].*/
16440 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
16450 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d sqlite3_busy_tim
16460 65 6f 75 74 28 73 71 6c 69 74 65 33 2a 2c 20 69 eout(sqlite3*, i
16470 6e 74 20 6d 73 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 nt ms);../*.** C
16480 41 50 49 33 52 45 46 3a 20 43 6f 6e 76 65 6e 69 API3REF: Conveni
16490 65 6e 63 65 20 52 6f 75 74 69 6e 65 73 20 46 6f ence Routines Fo
164a0 72 20 52 75 6e 6e 69 6e 67 20 51 75 65 72 69 65 r Running Querie
164b0 73 20 7b 48 31 32 33 37 30 7d 20 3c 53 31 30 30 s {H12370} <S100
164c0 30 30 3e 0a 2a 2a 0a 2a 2a 20 44 65 66 69 6e 69 00>.**.** Defini
164d0 74 69 6f 6e 3a 20 41 20 3c 62 3e 72 65 73 75 6c tion: A <b>resul
164e0 74 20 74 61 62 6c 65 3c 2f 62 3e 20 69 73 20 6d t table</b> is m
164f0 65 6d 6f 72 79 20 64 61 74 61 20 73 74 72 75 63 emory data struc
16500 74 75 72 65 20 63 72 65 61 74 65 64 20 62 79 20 ture created by
16510 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f the.** [sqlite3_
16520 67 65 74 5f 74 61 62 6c 65 28 29 5d 20 69 6e 74 get_table()] int
16530 65 72 66 61 63 65 2e 20 20 41 20 72 65 73 75 6c erface. A resul
16540 74 20 74 61 62 6c 65 20 72 65 63 6f 72 64 73 20 t table records
16550 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 20 the.** complete
16560 71 75 65 72 79 20 72 65 73 75 6c 74 73 20 66 72 query results fr
16570 6f 6d 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 71 om one or more q
16580 75 65 72 69 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 ueries..**.** Th
16590 65 20 74 61 62 6c 65 20 63 6f 6e 63 65 70 74 75 e table conceptu
165a0 61 6c 6c 79 20 68 61 73 20 61 20 6e 75 6d 62 65 ally has a numbe
165b0 72 20 6f 66 20 72 6f 77 73 20 61 6e 64 20 63 6f r of rows and co
165c0 6c 75 6d 6e 73 2e 20 20 42 75 74 0a 2a 2a 20 74 lumns. But.** t
165d0 68 65 73 65 20 6e 75 6d 62 65 72 73 20 61 72 65 hese numbers are
165e0 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68 65 not part of the
165f0 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 69 74 result table it
16600 73 65 6c 66 2e 20 20 54 68 65 73 65 0a 2a 2a 20 self. These.**
16610 6e 75 6d 62 65 72 73 20 61 72 65 20 6f 62 74 61 numbers are obta
16620 69 6e 65 64 20 73 65 70 61 72 61 74 65 6c 79 2e ined separately.
16630 20 20 4c 65 74 20 4e 20 62 65 20 74 68 65 20 6e Let N be the n
16640 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 0a 2a 2a umber of rows.**
16650 20 61 6e 64 20 4d 20 62 65 20 74 68 65 20 6e 75 and M be the nu
16660 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 2e mber of columns.
16670 0a 2a 2a 0a 2a 2a 20 41 20 72 65 73 75 6c 74 20 .**.** A result
16680 74 61 62 6c 65 20 69 73 20 61 6e 20 61 72 72 61 table is an arra
16690 79 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f y of pointers to
166a0 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 zero-terminated
166b0 20 55 54 46 2d 38 20 73 74 72 69 6e 67 73 2e 0a UTF-8 strings..
166c0 2a 2a 20 54 68 65 72 65 20 61 72 65 20 28 4e 2b ** There are (N+
166d0 31 29 2a 4d 20 65 6c 65 6d 65 6e 74 73 20 69 6e 1)*M elements in
166e0 20 74 68 65 20 61 72 72 61 79 2e 20 20 54 68 65 the array. The
166f0 20 66 69 72 73 74 20 4d 20 70 6f 69 6e 74 65 72 first M pointer
16700 73 20 70 6f 69 6e 74 0a 2a 2a 20 74 6f 20 7a 65 s point.** to ze
16710 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74 ro-terminated st
16720 72 69 6e 67 73 20 74 68 61 74 20 20 63 6f 6e 74 rings that cont
16730 61 69 6e 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 ain the names of
16740 20 74 68 65 20 63 6f 6c 75 6d 6e 73 2e 0a 2a 2a the columns..**
16750 20 54 68 65 20 72 65 6d 61 69 6e 69 6e 67 20 65 The remaining e
16760 6e 74 72 69 65 73 20 61 6c 6c 20 70 6f 69 6e 74 ntries all point
16770 20 74 6f 20 71 75 65 72 79 20 72 65 73 75 6c 74 to query result
16780 73 2e 20 20 4e 55 4c 4c 20 76 61 6c 75 65 73 20 s. NULL values
16790 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 4e 55 4c result.** in NUL
167a0 4c 20 70 6f 69 6e 74 65 72 73 2e 20 20 41 6c 6c L pointers. All
167b0 20 6f 74 68 65 72 20 76 61 6c 75 65 73 20 61 72 other values ar
167c0 65 20 69 6e 20 74 68 65 69 72 20 55 54 46 2d 38 e in their UTF-8
167d0 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 zero-terminated
167e0 0a 2a 2a 20 73 74 72 69 6e 67 20 72 65 70 72 65 .** string repre
167f0 73 65 6e 74 61 74 69 6f 6e 20 61 73 20 72 65 74 sentation as ret
16800 75 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74 65 urned by [sqlite
16810 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 5d 3_column_text()]
16820 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 73 75 6c 74 ..**.** A result
16830 20 74 61 62 6c 65 20 6d 69 67 68 74 20 63 6f 6e table might con
16840 73 69 73 74 20 6f 66 20 6f 6e 65 20 6f 72 20 6d sist of one or m
16850 6f 72 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 ore memory alloc
16860 61 74 69 6f 6e 73 2e 0a 2a 2a 20 49 74 20 69 73 ations..** It is
16870 20 6e 6f 74 20 73 61 66 65 20 74 6f 20 70 61 73 not safe to pas
16880 73 20 61 20 72 65 73 75 6c 74 20 74 61 62 6c 65 s a result table
16890 20 64 69 72 65 63 74 6c 79 20 74 6f 20 5b 73 71 directly to [sq
168a0 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e 0a 2a lite3_free()]..*
168b0 2a 20 41 20 72 65 73 75 6c 74 20 74 61 62 6c 65 * A result table
168c0 20 73 68 6f 75 6c 64 20 62 65 20 64 65 61 6c 6c should be deall
168d0 6f 63 61 74 65 64 20 75 73 69 6e 67 20 5b 73 71 ocated using [sq
168e0 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 lite3_free_table
168f0 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 61 6e ()]..**.** As an
16900 20 65 78 61 6d 70 6c 65 20 6f 66 20 74 68 65 20 example of the
16910 72 65 73 75 6c 74 20 74 61 62 6c 65 20 66 6f 72 result table for
16920 6d 61 74 2c 20 73 75 70 70 6f 73 65 20 61 20 71 mat, suppose a q
16930 75 65 72 79 20 72 65 73 75 6c 74 0a 2a 2a 20 69 uery result.** i
16940 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a s as follows:.**
16950 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e .** <blockquote>
16960 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20 20 20 20 <pre>.**
16970 4e 61 6d 65 20 20 20 20 20 20 20 20 7c 20 41 67 Name | Ag
16980 65 0a 2a 2a 20 20 20 20 20 20 20 20 2d 2d 2d 2d e.** ----
16990 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
169a0 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20 20 20 41 6c ---.** Al
169b0 69 63 65 20 20 20 20 20 20 20 7c 20 34 33 0a 2a ice | 43.*
169c0 2a 20 20 20 20 20 20 20 20 42 6f 62 20 20 20 20 * Bob
169d0 20 20 20 20 20 7c 20 32 38 0a 2a 2a 20 20 20 20 | 28.**
169e0 20 20 20 20 43 69 6e 64 79 20 20 20 20 20 20 20 Cindy
169f0 7c 20 32 31 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f | 21.** </pre></
16a00 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a blockquote>.**.*
16a10 2a 20 54 68 65 72 65 20 61 72 65 20 74 77 6f 20 * There are two
16a20 63 6f 6c 75 6d 6e 20 28 4d 3d 3d 32 29 20 61 6e column (M==2) an
16a30 64 20 74 68 72 65 65 20 72 6f 77 73 20 28 4e 3d d three rows (N=
16a40 3d 33 29 2e 20 20 54 68 75 73 20 74 68 65 0a 2a =3). Thus the.*
16a50 2a 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 68 * result table h
16a60 61 73 20 38 20 65 6e 74 72 69 65 73 2e 20 20 53 as 8 entries. S
16a70 75 70 70 6f 73 65 20 74 68 65 20 72 65 73 75 6c uppose the resul
16a80 74 20 74 61 62 6c 65 20 69 73 20 73 74 6f 72 65 t table is store
16a90 64 0a 2a 2a 20 69 6e 20 61 6e 20 61 72 72 61 79 d.** in an array
16aa0 20 6e 61 6d 65 73 20 61 7a 52 65 73 75 6c 74 2e names azResult.
16ab0 20 20 54 68 65 6e 20 61 7a 52 65 73 75 6c 74 20 Then azResult
16ac0 68 6f 6c 64 73 20 74 68 69 73 20 63 6f 6e 74 65 holds this conte
16ad0 6e 74 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b nt:.**.** <block
16ae0 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 quote><pre>.**
16af0 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 azResult&#
16b00 39 31 3b 30 5d 20 3d 20 22 4e 61 6d 65 22 3b 0a 91;0] = "Name";.
16b10 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 ** azResu
16b20 6c 74 26 23 39 31 3b 31 5d 20 3d 20 22 41 67 65 lt[1] = "Age
16b30 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 ";.** azR
16b40 65 73 75 6c 74 26 23 39 31 3b 32 5d 20 3d 20 22 esult[2] = "
16b50 41 6c 69 63 65 22 3b 0a 2a 2a 20 20 20 20 20 20 Alice";.**
16b60 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 33 azResult[3
16b70 5d 20 3d 20 22 34 33 22 3b 0a 2a 2a 20 20 20 20 ] = "43";.**
16b80 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31 azResult[
16b90 3b 34 5d 20 3d 20 22 42 6f 62 22 3b 0a 2a 2a 20 ;4] = "Bob";.**
16ba0 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 azResult&
16bb0 23 39 31 3b 35 5d 20 3d 20 22 32 38 22 3b 0a 2a #91;5] = "28";.*
16bc0 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c * azResul
16bd0 74 26 23 39 31 3b 36 5d 20 3d 20 22 43 69 6e 64 t[6] = "Cind
16be0 79 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a y";.** az
16bf0 52 65 73 75 6c 74 26 23 39 31 3b 37 5d 20 3d 20 Result[7] =
16c00 22 32 31 22 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c "21";.** </pre><
16c10 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a /blockquote>.**.
16c20 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 67 ** The sqlite3_g
16c30 65 74 5f 74 61 62 6c 65 28 29 20 66 75 6e 63 74 et_table() funct
16c40 69 6f 6e 20 65 76 61 6c 75 61 74 65 73 20 6f 6e ion evaluates on
16c50 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 73 65 6d e or more.** sem
16c60 69 63 6f 6c 6f 6e 2d 73 65 70 61 72 61 74 65 64 icolon-separated
16c70 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 SQL statements
16c80 69 6e 20 74 68 65 20 7a 65 72 6f 2d 74 65 72 6d in the zero-term
16c90 69 6e 61 74 65 64 20 55 54 46 2d 38 0a 2a 2a 20 inated UTF-8.**
16ca0 73 74 72 69 6e 67 20 6f 66 20 69 74 73 20 32 6e string of its 2n
16cb0 64 20 70 61 72 61 6d 65 74 65 72 2e 20 20 49 74 d parameter. It
16cc0 20 72 65 74 75 72 6e 73 20 61 20 72 65 73 75 6c returns a resul
16cd0 74 20 74 61 62 6c 65 20 74 6f 20 74 68 65 0a 2a t table to the.*
16ce0 2a 20 70 6f 69 6e 74 65 72 20 67 69 76 65 6e 20 * pointer given
16cf0 69 6e 20 69 74 73 20 33 72 64 20 70 61 72 61 6d in its 3rd param
16d00 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 eter..**.** Afte
16d10 72 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 r the calling fu
16d20 6e 63 74 69 6f 6e 20 68 61 73 20 66 69 6e 69 73 nction has finis
16d30 68 65 64 20 75 73 69 6e 67 20 74 68 65 20 72 65 hed using the re
16d40 73 75 6c 74 2c 20 69 74 20 73 68 6f 75 6c 64 0a sult, it should.
16d50 2a 2a 20 70 61 73 73 20 74 68 65 20 70 6f 69 6e ** pass the poin
16d60 74 65 72 20 74 6f 20 74 68 65 20 72 65 73 75 6c ter to the resul
16d70 74 20 74 61 62 6c 65 20 74 6f 20 73 71 6c 69 74 t table to sqlit
16d80 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 29 20 e3_free_table()
16d90 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 72 in order to.** r
16da0 65 6c 65 61 73 65 20 74 68 65 20 6d 65 6d 6f 72 elease the memor
16db0 79 20 74 68 61 74 20 77 61 73 20 6d 61 6c 6c 6f y that was mallo
16dc0 63 65 64 2e 20 20 42 65 63 61 75 73 65 20 6f 66 ced. Because of
16dd0 20 74 68 65 20 77 61 79 20 74 68 65 0a 2a 2a 20 the way the.**
16de0 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 [sqlite3_malloc(
16df0 29 5d 20 68 61 70 70 65 6e 73 20 77 69 74 68 69 )] happens withi
16e00 6e 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 n sqlite3_get_ta
16e10 62 6c 65 28 29 2c 20 74 68 65 20 63 61 6c 6c 69 ble(), the calli
16e20 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 6d ng.** function m
16e30 75 73 74 20 6e 6f 74 20 74 72 79 20 74 6f 20 63 ust not try to c
16e40 61 6c 6c 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 all [sqlite3_fre
16e50 65 28 29 5d 20 64 69 72 65 63 74 6c 79 2e 20 20 e()] directly.
16e60 4f 6e 6c 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 Only.** [sqlite3
16e70 5f 66 72 65 65 5f 74 61 62 6c 65 28 29 5d 20 69 _free_table()] i
16e80 73 20 61 62 6c 65 20 74 6f 20 72 65 6c 65 61 73 s able to releas
16e90 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 70 72 6f e the memory pro
16ea0 70 65 72 6c 79 20 61 6e 64 20 73 61 66 65 6c 79 perly and safely
16eb0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ..**.** The sqli
16ec0 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 te3_get_table()
16ed0 69 6e 74 65 72 66 61 63 65 20 69 73 20 69 6d 70 interface is imp
16ee0 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 20 77 72 lemented as a wr
16ef0 61 70 70 65 72 20 61 72 6f 75 6e 64 0a 2a 2a 20 apper around.**
16f00 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d [sqlite3_exec()]
16f10 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 67 . The sqlite3_g
16f20 65 74 5f 74 61 62 6c 65 28 29 20 72 6f 75 74 69 et_table() routi
16f30 6e 65 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 ne does not have
16f40 20 61 63 63 65 73 73 0a 2a 2a 20 74 6f 20 61 6e access.** to an
16f50 79 20 69 6e 74 65 72 6e 61 6c 20 64 61 74 61 20 y internal data
16f60 73 74 72 75 63 74 75 72 65 73 20 6f 66 20 53 51 structures of SQ
16f70 4c 69 74 65 2e 20 20 49 74 20 75 73 65 73 20 6f Lite. It uses o
16f80 6e 6c 79 20 74 68 65 20 70 75 62 6c 69 63 0a 2a nly the public.*
16f90 2a 20 69 6e 74 65 72 66 61 63 65 20 64 65 66 69 * interface defi
16fa0 6e 65 64 20 68 65 72 65 2e 20 20 41 73 20 61 20 ned here. As a
16fb0 63 6f 6e 73 65 71 75 65 6e 63 65 2c 20 65 72 72 consequence, err
16fc0 6f 72 73 20 74 68 61 74 20 6f 63 63 75 72 20 69 ors that occur i
16fd0 6e 20 74 68 65 0a 2a 2a 20 77 72 61 70 70 65 72 n the.** wrapper
16fe0 20 6c 61 79 65 72 20 6f 75 74 73 69 64 65 20 6f layer outside o
16ff0 66 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 5b f the internal [
17000 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20 sqlite3_exec()]
17010 63 61 6c 6c 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 call are not.**
17020 72 65 66 6c 65 63 74 65 64 20 69 6e 20 73 75 62 reflected in sub
17030 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f sequent calls to
17040 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 [sqlite3_errcod
17050 65 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 e()] or [sqlite3
17060 5f 65 72 72 6d 73 67 28 29 5d 2e 0a 2a 2a 0a 2a _errmsg()]..**.*
17070 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
17080 2a 2a 20 5b 48 31 32 33 37 31 5d 20 5b 48 31 32 ** [H12371] [H12
17090 33 37 33 5d 20 5b 48 31 32 33 37 34 5d 20 5b 48 373] [H12374] [H
170a0 31 32 33 37 36 5d 20 5b 48 31 32 33 37 39 5d 20 12376] [H12379]
170b0 5b 48 31 32 33 38 32 5d 0a 2a 2f 0a 53 51 4c 49 [H12382].*/.SQLI
170c0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
170d0 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 0a 20 20 e3_get_table(.
170e0 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 sqlite3 *db,
170f0 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f 70 65 6e /* An open
17100 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 63 database */. c
17110 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c onst char *zSql,
17120 20 20 20 20 20 2f 2a 20 53 51 4c 20 74 6f 20 62 /* SQL to b
17130 65 20 65 76 61 6c 75 61 74 65 64 20 2a 2f 0a 20 e evaluated */.
17140 20 63 68 61 72 20 2a 2a 2a 70 61 7a 52 65 73 75 char ***pazResu
17150 6c 74 2c 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 lt, /* Result
17160 73 20 6f 66 20 74 68 65 20 71 75 65 72 79 20 2a s of the query *
17170 2f 0a 20 20 69 6e 74 20 2a 70 6e 52 6f 77 2c 20 /. int *pnRow,
17180 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
17190 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20 72 6f ber of result ro
171a0 77 73 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 ws written here
171b0 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 6f 6c 75 */. int *pnColu
171c0 6d 6e 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 mn, /* Nu
171d0 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20 63 mber of result c
171e0 6f 6c 75 6d 6e 73 20 77 72 69 74 74 65 6e 20 68 olumns written h
171f0 65 72 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a ere */. char **
17200 70 7a 45 72 72 6d 73 67 20 20 20 20 20 20 20 2f pzErrmsg /
17210 2a 20 45 72 72 6f 72 20 6d 73 67 20 77 72 69 74 * Error msg writ
17220 74 65 6e 20 68 65 72 65 20 2a 2f 0a 29 3b 0a 53 ten here */.);.S
17230 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
17240 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c qlite3_free_tabl
17250 65 28 63 68 61 72 20 2a 2a 72 65 73 75 6c 74 29 e(char **result)
17260 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
17270 46 3a 20 46 6f 72 6d 61 74 74 65 64 20 53 74 72 F: Formatted Str
17280 69 6e 67 20 50 72 69 6e 74 69 6e 67 20 46 75 6e ing Printing Fun
17290 63 74 69 6f 6e 73 20 7b 48 31 37 34 30 30 7d 20 ctions {H17400}
172a0 3c 53 37 30 30 30 30 3e 3c 53 32 30 30 30 30 3e <S70000><S20000>
172b0 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 .**.** These rou
172c0 74 69 6e 65 73 20 61 72 65 20 77 6f 72 6b 2d 61 tines are work-a
172d0 6c 69 6b 65 73 20 6f 66 20 74 68 65 20 22 70 72 likes of the "pr
172e0 69 6e 74 66 28 29 22 20 66 61 6d 69 6c 79 20 6f intf()" family o
172f0 66 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 66 f functions.** f
17300 72 6f 6d 20 74 68 65 20 73 74 61 6e 64 61 72 64 rom the standard
17310 20 43 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a C library..**.*
17320 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 70 * The sqlite3_mp
17330 72 69 6e 74 66 28 29 20 61 6e 64 20 73 71 6c 69 rintf() and sqli
17340 74 65 33 5f 76 6d 70 72 69 6e 74 66 28 29 20 72 te3_vmprintf() r
17350 6f 75 74 69 6e 65 73 20 77 72 69 74 65 20 74 68 outines write th
17360 65 69 72 0a 2a 2a 20 72 65 73 75 6c 74 73 20 69 eir.** results i
17370 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 nto memory obtai
17380 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 ned from [sqlite
17390 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2a 20 3_malloc()]..**
173a0 54 68 65 20 73 74 72 69 6e 67 73 20 72 65 74 75 The strings retu
173b0 72 6e 65 64 20 62 79 20 74 68 65 73 65 20 74 77 rned by these tw
173c0 6f 20 72 6f 75 74 69 6e 65 73 20 73 68 6f 75 6c o routines shoul
173d0 64 20 62 65 0a 2a 2a 20 72 65 6c 65 61 73 65 64 d be.** released
173e0 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 by [sqlite3_fre
173f0 65 28 29 5d 2e 20 20 42 6f 74 68 20 72 6f 75 74 e()]. Both rout
17400 69 6e 65 73 20 72 65 74 75 72 6e 20 61 0a 2a 2a ines return a.**
17410 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 66 NULL pointer if
17420 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 [sqlite3_malloc
17430 28 29 5d 20 69 73 20 75 6e 61 62 6c 65 20 74 6f ()] is unable to
17440 20 61 6c 6c 6f 63 61 74 65 20 65 6e 6f 75 67 68 allocate enough
17450 0a 2a 2a 20 6d 65 6d 6f 72 79 20 74 6f 20 68 6f .** memory to ho
17460 6c 64 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 ld the resulting
17470 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 string..**.** I
17480 6e 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e n sqlite3_snprin
17490 74 66 28 29 20 72 6f 75 74 69 6e 65 20 69 73 20 tf() routine is
174a0 73 69 6d 69 6c 61 72 20 74 6f 20 22 73 6e 70 72 similar to "snpr
174b0 69 6e 74 66 28 29 22 20 66 72 6f 6d 0a 2a 2a 20 intf()" from.**
174c0 74 68 65 20 73 74 61 6e 64 61 72 64 20 43 20 6c the standard C l
174d0 69 62 72 61 72 79 2e 20 20 54 68 65 20 72 65 73 ibrary. The res
174e0 75 6c 74 20 69 73 20 77 72 69 74 74 65 6e 20 69 ult is written i
174f0 6e 74 6f 20 74 68 65 0a 2a 2a 20 62 75 66 66 65 nto the.** buffe
17500 72 20 73 75 70 70 6c 69 65 64 20 61 73 20 74 68 r supplied as th
17510 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 e second paramet
17520 65 72 20 77 68 6f 73 65 20 73 69 7a 65 20 69 73 er whose size is
17530 20 67 69 76 65 6e 20 62 79 0a 2a 2a 20 74 68 65 given by.** the
17540 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 first parameter
17550 2e 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 . Note that the
17560 6f 72 64 65 72 20 6f 66 20 74 68 65 0a 2a 2a 20 order of the.**
17570 66 69 72 73 74 20 74 77 6f 20 70 61 72 61 6d 65 first two parame
17580 74 65 72 73 20 69 73 20 72 65 76 65 72 73 65 64 ters is reversed
17590 20 66 72 6f 6d 20 73 6e 70 72 69 6e 74 66 28 29 from snprintf()
175a0 2e 20 20 54 68 69 73 20 69 73 20 61 6e 0a 2a 2a . This is an.**
175b0 20 68 69 73 74 6f 72 69 63 61 6c 20 61 63 63 69 historical acci
175c0 64 65 6e 74 20 74 68 61 74 20 63 61 6e 6e 6f 74 dent that cannot
175d0 20 62 65 20 66 69 78 65 64 20 77 69 74 68 6f 75 be fixed withou
175e0 74 20 62 72 65 61 6b 69 6e 67 0a 2a 2a 20 62 61 t breaking.** ba
175f0 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 ckwards compatib
17600 69 6c 69 74 79 2e 20 20 4e 6f 74 65 20 61 6c 73 ility. Note als
17610 6f 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f 73 o that sqlite3_s
17620 6e 70 72 69 6e 74 66 28 29 0a 2a 2a 20 72 65 74 nprintf().** ret
17630 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 urns a pointer t
17640 6f 20 69 74 73 20 62 75 66 66 65 72 20 69 6e 73 o its buffer ins
17650 74 65 61 64 20 6f 66 20 74 68 65 20 6e 75 6d 62 tead of the numb
17660 65 72 20 6f 66 0a 2a 2a 20 63 68 61 72 61 63 74 er of.** charact
17670 65 72 73 20 61 63 74 75 61 6c 6c 79 20 77 72 69 ers actually wri
17680 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 62 75 tten into the bu
17690 66 66 65 72 2e 20 20 57 65 20 61 64 6d 69 74 20 ffer. We admit
176a0 74 68 61 74 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 that.** the numb
176b0 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 er of characters
176c0 20 77 72 69 74 74 65 6e 20 77 6f 75 6c 64 20 62 written would b
176d0 65 20 61 20 6d 6f 72 65 20 75 73 65 66 75 6c 20 e a more useful
176e0 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 return.** value
176f0 62 75 74 20 77 65 20 63 61 6e 6e 6f 74 20 63 68 but we cannot ch
17700 61 6e 67 65 20 74 68 65 20 69 6d 70 6c 65 6d 65 ange the impleme
17710 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 ntation of sqlit
17720 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 0a 2a 2a e3_snprintf().**
17730 20 6e 6f 77 20 77 69 74 68 6f 75 74 20 62 72 65 now without bre
17740 61 6b 69 6e 67 20 63 6f 6d 70 61 74 69 62 69 6c aking compatibil
17750 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 6c 6f ity..**.** As lo
17760 6e 67 20 61 73 20 74 68 65 20 62 75 66 66 65 72 ng as the buffer
17770 20 73 69 7a 65 20 69 73 20 67 72 65 61 74 65 72 size is greater
17780 20 74 68 61 6e 20 7a 65 72 6f 2c 20 73 71 6c 69 than zero, sqli
17790 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 0a 2a te3_snprintf().*
177a0 2a 20 67 75 61 72 61 6e 74 65 65 73 20 74 68 61 * guarantees tha
177b0 74 20 74 68 65 20 62 75 66 66 65 72 20 69 73 20 t the buffer is
177c0 61 6c 77 61 79 73 20 7a 65 72 6f 2d 74 65 72 6d always zero-term
177d0 69 6e 61 74 65 64 2e 20 20 54 68 65 20 66 69 72 inated. The fir
177e0 73 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 st.** parameter
177f0 22 6e 22 20 69 73 20 74 68 65 20 74 6f 74 61 6c "n" is the total
17800 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 75 66 size of the buf
17810 66 65 72 2c 20 69 6e 63 6c 75 64 69 6e 67 20 73 fer, including s
17820 70 61 63 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 pace for.** the
17830 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 2e zero terminator.
17840 20 20 53 6f 20 74 68 65 20 6c 6f 6e 67 65 73 74 So the longest
17850 20 73 74 72 69 6e 67 20 74 68 61 74 20 63 61 6e string that can
17860 20 62 65 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 2a be completely.*
17870 2a 20 77 72 69 74 74 65 6e 20 77 69 6c 6c 20 62 * written will b
17880 65 20 6e 2d 31 20 63 68 61 72 61 63 74 65 72 73 e n-1 characters
17890 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f ..**.** These ro
178a0 75 74 69 6e 65 73 20 61 6c 6c 20 69 6d 70 6c 65 utines all imple
178b0 6d 65 6e 74 20 73 6f 6d 65 20 61 64 64 69 74 69 ment some additi
178c0 6f 6e 61 6c 20 66 6f 72 6d 61 74 74 69 6e 67 0a onal formatting.
178d0 2a 2a 20 6f 70 74 69 6f 6e 73 20 74 68 61 74 20 ** options that
178e0 61 72 65 20 75 73 65 66 75 6c 20 66 6f 72 20 63 are useful for c
178f0 6f 6e 73 74 72 75 63 74 69 6e 67 20 53 51 4c 20 onstructing SQL
17900 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 20 41 statements..** A
17910 6c 6c 20 6f 66 20 74 68 65 20 75 73 75 61 6c 20 ll of the usual
17920 70 72 69 6e 74 66 28 29 20 66 6f 72 6d 61 74 74 printf() formatt
17930 69 6e 67 20 6f 70 74 69 6f 6e 73 20 61 70 70 6c ing options appl
17940 79 2e 20 20 49 6e 20 61 64 64 69 74 69 6f 6e 2c y. In addition,
17950 20 74 68 65 72 65 0a 2a 2a 20 69 73 20 61 72 65 there.** is are
17960 20 22 25 71 22 2c 20 22 25 51 22 2c 20 61 6e 64 "%q", "%Q", and
17970 20 22 25 7a 22 20 6f 70 74 69 6f 6e 73 2e 0a 2a "%z" options..*
17980 2a 0a 2a 2a 20 54 68 65 20 25 71 20 6f 70 74 69 *.** The %q opti
17990 6f 6e 20 77 6f 72 6b 73 20 6c 69 6b 65 20 25 73 on works like %s
179a0 20 69 6e 20 74 68 61 74 20 69 74 20 73 75 62 73 in that it subs
179b0 74 69 74 75 74 65 73 20 61 20 6e 75 6c 6c 2d 74 titutes a null-t
179c0 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 73 74 72 erminated.** str
179d0 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 61 72 67 ing from the arg
179e0 75 6d 65 6e 74 20 6c 69 73 74 2e 20 20 42 75 74 ument list. But
179f0 20 25 71 20 61 6c 73 6f 20 64 6f 75 62 6c 65 73 %q also doubles
17a00 20 65 76 65 72 79 20 27 5c 27 27 20 63 68 61 72 every '\'' char
17a10 61 63 74 65 72 2e 0a 2a 2a 20 25 71 20 69 73 20 acter..** %q is
17a20 64 65 73 69 67 6e 65 64 20 66 6f 72 20 75 73 65 designed for use
17a30 20 69 6e 73 69 64 65 20 61 20 73 74 72 69 6e 67 inside a string
17a40 20 6c 69 74 65 72 61 6c 2e 20 20 42 79 20 64 6f literal. By do
17a50 75 62 6c 69 6e 67 20 65 61 63 68 20 27 5c 27 27 ubling each '\''
17a60 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 20 69 74 .** character it
17a70 20 65 73 63 61 70 65 73 20 74 68 61 74 20 63 68 escapes that ch
17a80 61 72 61 63 74 65 72 20 61 6e 64 20 61 6c 6c 6f aracter and allo
17a90 77 73 20 69 74 20 74 6f 20 62 65 20 69 6e 73 65 ws it to be inse
17aa0 72 74 65 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 rted into.** the
17ab0 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 46 string..**.** F
17ac0 6f 72 20 65 78 61 6d 70 6c 65 2c 20 61 73 73 75 or example, assu
17ad0 6d 65 20 74 68 65 20 73 74 72 69 6e 67 20 76 61 me the string va
17ae0 72 69 61 62 6c 65 20 7a 54 65 78 74 20 63 6f 6e riable zText con
17af0 74 61 69 6e 73 20 74 65 78 74 20 61 73 20 66 6f tains text as fo
17b00 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c llows:.**.** <bl
17b10 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a ockquote><pre>.*
17b20 2a 20 20 63 68 61 72 20 2a 7a 54 65 78 74 20 3d * char *zText =
17b30 20 22 49 74 27 73 20 61 20 68 61 70 70 79 20 64 "It's a happy d
17b40 61 79 21 22 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c ay!";.** </pre><
17b50 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a /blockquote>.**.
17b60 2a 2a 20 4f 6e 65 20 63 61 6e 20 75 73 65 20 74 ** One can use t
17b70 68 69 73 20 74 65 78 74 20 69 6e 20 61 6e 20 53 his text in an S
17b80 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61 73 20 QL statement as
17b90 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c follows:.**.** <
17ba0 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e blockquote><pre>
17bb0 0a 2a 2a 20 20 63 68 61 72 20 2a 7a 53 51 4c 20 .** char *zSQL
17bc0 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 = sqlite3_mprint
17bd0 66 28 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 f("INSERT INTO t
17be0 61 62 6c 65 20 56 41 4c 55 45 53 28 27 25 71 27 able VALUES('%q'
17bf0 29 22 2c 20 7a 54 65 78 74 29 3b 0a 2a 2a 20 20 )", zText);.**
17c00 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c sqlite3_exec(db,
17c10 20 7a 53 51 4c 2c 20 30 2c 20 30 2c 20 30 29 3b zSQL, 0, 0, 0);
17c20 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 .** sqlite3_fre
17c30 65 28 7a 53 51 4c 29 3b 0a 2a 2a 20 3c 2f 70 72 e(zSQL);.** </pr
17c40 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a e></blockquote>.
17c50 2a 2a 0a 2a 2a 20 42 65 63 61 75 73 65 20 74 68 **.** Because th
17c60 65 20 25 71 20 66 6f 72 6d 61 74 20 73 74 72 69 e %q format stri
17c70 6e 67 20 69 73 20 75 73 65 64 2c 20 74 68 65 20 ng is used, the
17c80 27 5c 27 27 20 63 68 61 72 61 63 74 65 72 20 69 '\'' character i
17c90 6e 20 7a 54 65 78 74 0a 2a 2a 20 69 73 20 65 73 n zText.** is es
17ca0 63 61 70 65 64 20 61 6e 64 20 74 68 65 20 53 51 caped and the SQ
17cb0 4c 20 67 65 6e 65 72 61 74 65 64 20 69 73 20 61 L generated is a
17cc0 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a s follows:.**.**
17cd0 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 <blockquote><pr
17ce0 65 3e 0a 2a 2a 20 20 49 4e 53 45 52 54 20 49 4e e>.** INSERT IN
17cf0 54 4f 20 74 61 62 6c 65 31 20 56 41 4c 55 45 53 TO table1 VALUES
17d00 28 27 49 74 27 27 73 20 61 20 68 61 70 70 79 20 ('It''s a happy
17d10 64 61 79 21 27 29 0a 2a 2a 20 3c 2f 70 72 65 3e day!').** </pre>
17d20 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a </blockquote>.**
17d30 0a 2a 2a 20 54 68 69 73 20 69 73 20 63 6f 72 72 .** This is corr
17d40 65 63 74 2e 20 20 48 61 64 20 77 65 20 75 73 65 ect. Had we use
17d50 64 20 25 73 20 69 6e 73 74 65 61 64 20 6f 66 20 d %s instead of
17d60 25 71 2c 20 74 68 65 20 67 65 6e 65 72 61 74 65 %q, the generate
17d70 64 20 53 51 4c 0a 2a 2a 20 77 6f 75 6c 64 20 68 d SQL.** would h
17d80 61 76 65 20 6c 6f 6f 6b 65 64 20 6c 69 6b 65 20 ave looked like
17d90 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f this:.**.** <blo
17da0 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a ckquote><pre>.**
17db0 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 61 INSERT INTO ta
17dc0 62 6c 65 31 20 56 41 4c 55 45 53 28 27 49 74 27 ble1 VALUES('It'
17dd0 73 20 61 20 68 61 70 70 79 20 64 61 79 21 27 29 s a happy day!')
17de0 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f ;.** </pre></blo
17df0 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 ckquote>.**.** T
17e00 68 69 73 20 73 65 63 6f 6e 64 20 65 78 61 6d 70 his second examp
17e10 6c 65 20 69 73 20 61 6e 20 53 51 4c 20 73 79 6e le is an SQL syn
17e20 74 61 78 20 65 72 72 6f 72 2e 20 20 41 73 20 61 tax error. As a
17e30 20 67 65 6e 65 72 61 6c 20 72 75 6c 65 20 79 6f general rule yo
17e40 75 20 73 68 6f 75 6c 64 0a 2a 2a 20 61 6c 77 61 u should.** alwa
17e50 79 73 20 75 73 65 20 25 71 20 69 6e 73 74 65 61 ys use %q instea
17e60 64 20 6f 66 20 25 73 20 77 68 65 6e 20 69 6e 73 d of %s when ins
17e70 65 72 74 69 6e 67 20 74 65 78 74 20 69 6e 74 6f erting text into
17e80 20 61 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61 a string litera
17e90 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 25 51 20 l..**.** The %Q
17ea0 6f 70 74 69 6f 6e 20 77 6f 72 6b 73 20 6c 69 6b option works lik
17eb0 65 20 25 71 20 65 78 63 65 70 74 20 69 74 20 61 e %q except it a
17ec0 6c 73 6f 20 61 64 64 73 20 73 69 6e 67 6c 65 20 lso adds single
17ed0 71 75 6f 74 65 73 20 61 72 6f 75 6e 64 0a 2a 2a quotes around.**
17ee0 20 74 68 65 20 6f 75 74 73 69 64 65 20 6f 66 20 the outside of
17ef0 74 68 65 20 74 6f 74 61 6c 20 73 74 72 69 6e 67 the total string
17f00 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c . Additionally,
17f10 20 69 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 if the paramete
17f20 72 20 69 6e 20 74 68 65 0a 2a 2a 20 61 72 67 75 r in the.** argu
17f30 6d 65 6e 74 20 6c 69 73 74 20 69 73 20 61 20 4e ment list is a N
17f40 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 25 51 20 ULL pointer, %Q
17f50 73 75 62 73 74 69 74 75 74 65 73 20 74 68 65 20 substitutes the
17f60 74 65 78 74 20 22 4e 55 4c 4c 22 20 28 77 69 74 text "NULL" (wit
17f70 68 6f 75 74 0a 2a 2a 20 73 69 6e 67 6c 65 20 71 hout.** single q
17f80 75 6f 74 65 73 29 20 69 6e 20 70 6c 61 63 65 20 uotes) in place
17f90 6f 66 20 74 68 65 20 25 51 20 6f 70 74 69 6f 6e of the %Q option
17fa0 2e 20 20 53 6f 2c 20 66 6f 72 20 65 78 61 6d 70 . So, for examp
17fb0 6c 65 2c 20 6f 6e 65 20 63 6f 75 6c 64 20 73 61 le, one could sa
17fc0 79 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 y:.**.** <blockq
17fd0 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 63 uote><pre>.** c
17fe0 68 61 72 20 2a 7a 53 51 4c 20 3d 20 73 71 6c 69 har *zSQL = sqli
17ff0 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 49 4e 53 te3_mprintf("INS
18000 45 52 54 20 49 4e 54 4f 20 74 61 62 6c 65 20 56 ERT INTO table V
18010 41 4c 55 45 53 28 25 51 29 22 2c 20 7a 54 65 78 ALUES(%Q)", zTex
18020 74 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f t);.** sqlite3_
18030 65 78 65 63 28 64 62 2c 20 7a 53 51 4c 2c 20 30 exec(db, zSQL, 0
18040 2c 20 30 2c 20 30 29 3b 0a 2a 2a 20 20 73 71 6c , 0, 0);.** sql
18050 69 74 65 33 5f 66 72 65 65 28 7a 53 51 4c 29 3b ite3_free(zSQL);
18060 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 .** </pre></bloc
18070 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 kquote>.**.** Th
18080 65 20 63 6f 64 65 20 61 62 6f 76 65 20 77 69 6c e code above wil
18090 6c 20 72 65 6e 64 65 72 20 61 20 63 6f 72 72 65 l render a corre
180a0 63 74 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 ct SQL statement
180b0 20 69 6e 20 74 68 65 20 7a 53 51 4c 0a 2a 2a 20 in the zSQL.**
180c0 76 61 72 69 61 62 6c 65 20 65 76 65 6e 20 69 66 variable even if
180d0 20 74 68 65 20 7a 54 65 78 74 20 76 61 72 69 61 the zText varia
180e0 62 6c 65 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f ble is a NULL po
180f0 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 inter..**.** The
18100 20 22 25 7a 22 20 66 6f 72 6d 61 74 74 69 6e 67 "%z" formatting
18110 20 6f 70 74 69 6f 6e 20 77 6f 72 6b 73 20 65 78 option works ex
18120 61 63 74 6c 79 20 6c 69 6b 65 20 22 25 73 22 20 actly like "%s"
18130 77 69 74 68 20 74 68 65 0a 2a 2a 20 61 64 64 69 with the.** addi
18140 74 69 6f 6e 20 74 68 61 74 20 61 66 74 65 72 20 tion that after
18150 74 68 65 20 73 74 72 69 6e 67 20 68 61 73 20 62 the string has b
18160 65 65 6e 20 72 65 61 64 20 61 6e 64 20 63 6f 70 een read and cop
18170 69 65 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 ied into.** the
18180 72 65 73 75 6c 74 2c 20 5b 73 71 6c 69 74 65 33 result, [sqlite3
18190 5f 66 72 65 65 28 29 5d 20 69 73 20 63 61 6c 6c _free()] is call
181a0 65 64 20 6f 6e 20 74 68 65 20 69 6e 70 75 74 20 ed on the input
181b0 73 74 72 69 6e 67 2e 20 7b 45 4e 44 7d 0a 2a 2a string. {END}.**
181c0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
181d0 3a 0a 2a 2a 20 5b 48 31 37 34 30 33 5d 20 5b 48 :.** [H17403] [H
181e0 31 37 34 30 36 5d 20 5b 48 31 37 34 30 37 5d 0a 17406] [H17407].
181f0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68 */.SQLITE_API ch
18200 61 72 20 2a 73 71 6c 69 74 65 33 5f 6d 70 72 69 ar *sqlite3_mpri
18210 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c ntf(const char*,
18220 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 ...);.SQLITE_API
18230 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 char *sqlite3_v
18240 6d 70 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 mprintf(const ch
18250 61 72 2a 2c 20 76 61 5f 6c 69 73 74 29 3b 0a 53 ar*, va_list);.S
18260 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a QLITE_API char *
18270 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
18280 28 69 6e 74 2c 63 68 61 72 2a 2c 63 6f 6e 73 74 (int,char*,const
18290 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 0a 2f char*, ...);../
182a0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4d *.** CAPI3REF: M
182b0 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 69 6f 6e emory Allocation
182c0 20 53 75 62 73 79 73 74 65 6d 20 7b 48 31 37 33 Subsystem {H173
182d0 30 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 00} <S20000>.**.
182e0 2a 2a 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f ** The SQLite co
182f0 72 65 20 20 75 73 65 73 20 74 68 65 73 65 20 74 re uses these t
18300 68 72 65 65 20 72 6f 75 74 69 6e 65 73 20 66 6f hree routines fo
18310 72 20 61 6c 6c 20 6f 66 20 69 74 73 20 6f 77 6e r all of its own
18320 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c 20 6d 65 6d .** internal mem
18330 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e ory allocation n
18340 65 65 64 73 2e 20 22 43 6f 72 65 22 20 69 6e 20 eeds. "Core" in
18350 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 65 6e the previous sen
18360 74 65 6e 63 65 0a 2a 2a 20 64 6f 65 73 20 6e 6f tence.** does no
18370 74 20 69 6e 63 6c 75 64 65 20 6f 70 65 72 61 74 t include operat
18380 69 6e 67 2d 73 79 73 74 65 6d 20 73 70 65 63 69 ing-system speci
18390 66 69 63 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e fic VFS implemen
183a0 74 61 74 69 6f 6e 2e 20 20 54 68 65 0a 2a 2a 20 tation. The.**
183b0 57 69 6e 64 6f 77 73 20 56 46 53 20 75 73 65 73 Windows VFS uses
183c0 20 6e 61 74 69 76 65 20 6d 61 6c 6c 6f 63 28 29 native malloc()
183d0 20 61 6e 64 20 66 72 65 65 28 29 20 66 6f 72 20 and free() for
183e0 73 6f 6d 65 20 6f 70 65 72 61 74 69 6f 6e 73 2e some operations.
183f0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
18400 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 72 6f 75 74 e3_malloc() rout
18410 69 6e 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f ine returns a po
18420 69 6e 74 65 72 20 74 6f 20 61 20 62 6c 6f 63 6b inter to a block
18430 0a 2a 2a 20 6f 66 20 6d 65 6d 6f 72 79 20 61 74 .** of memory at
18440 20 6c 65 61 73 74 20 4e 20 62 79 74 65 73 20 69 least N bytes i
18450 6e 20 6c 65 6e 67 74 68 2c 20 77 68 65 72 65 20 n length, where
18460 4e 20 69 73 20 74 68 65 20 70 61 72 61 6d 65 74 N is the paramet
18470 65 72 2e 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65 er..** If sqlite
18480 33 5f 6d 61 6c 6c 6f 63 28 29 20 69 73 20 75 6e 3_malloc() is un
18490 61 62 6c 65 20 74 6f 20 6f 62 74 61 69 6e 20 73 able to obtain s
184a0 75 66 66 69 63 69 65 6e 74 20 66 72 65 65 0a 2a ufficient free.*
184b0 2a 20 6d 65 6d 6f 72 79 2c 20 69 74 20 72 65 74 * memory, it ret
184c0 75 72 6e 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e urns a NULL poin
184d0 74 65 72 2e 20 20 49 66 20 74 68 65 20 70 61 72 ter. If the par
184e0 61 6d 65 74 65 72 20 4e 20 74 6f 0a 2a 2a 20 73 ameter N to.** s
184f0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 qlite3_malloc()
18500 69 73 20 7a 65 72 6f 20 6f 72 20 6e 65 67 61 74 is zero or negat
18510 69 76 65 20 74 68 65 6e 20 73 71 6c 69 74 65 33 ive then sqlite3
18520 5f 6d 61 6c 6c 6f 63 28 29 20 72 65 74 75 72 6e _malloc() return
18530 73 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f 69 6e s.** a NULL poin
18540 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 ter..**.** Calli
18550 6e 67 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 ng sqlite3_free(
18560 29 20 77 69 74 68 20 61 20 70 6f 69 6e 74 65 72 ) with a pointer
18570 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 74 75 previously retu
18580 72 6e 65 64 0a 2a 2a 20 62 79 20 73 71 6c 69 74 rned.** by sqlit
18590 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 73 e3_malloc() or s
185a0 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 qlite3_realloc()
185b0 20 72 65 6c 65 61 73 65 73 20 74 68 61 74 20 6d releases that m
185c0 65 6d 6f 72 79 20 73 6f 0a 2a 2a 20 74 68 61 74 emory so.** that
185d0 20 69 74 20 6d 69 67 68 74 20 62 65 20 72 65 75 it might be reu
185e0 73 65 64 2e 20 20 54 68 65 20 73 71 6c 69 74 65 sed. The sqlite
185f0 33 5f 66 72 65 65 28 29 20 72 6f 75 74 69 6e 65 3_free() routine
18600 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 20 69 is.** a no-op i
18610 66 20 69 73 20 63 61 6c 6c 65 64 20 77 69 74 68 f is called with
18620 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e a NULL pointer.
18630 20 20 50 61 73 73 69 6e 67 20 61 20 4e 55 4c 4c Passing a NULL
18640 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 73 pointer.** to s
18650 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20 69 73 qlite3_free() is
18660 20 68 61 72 6d 6c 65 73 73 2e 20 20 41 66 74 65 harmless. Afte
18670 72 20 62 65 69 6e 67 20 66 72 65 65 64 2c 20 6d r being freed, m
18680 65 6d 6f 72 79 0a 2a 2a 20 73 68 6f 75 6c 64 20 emory.** should
18690 6e 65 69 74 68 65 72 20 62 65 20 72 65 61 64 20 neither be read
186a0 6e 6f 72 20 77 72 69 74 74 65 6e 2e 20 20 45 76 nor written. Ev
186b0 65 6e 20 72 65 61 64 69 6e 67 20 70 72 65 76 69 en reading previ
186c0 6f 75 73 6c 79 20 66 72 65 65 64 0a 2a 2a 20 6d ously freed.** m
186d0 65 6d 6f 72 79 20 6d 69 67 68 74 20 72 65 73 75 emory might resu
186e0 6c 74 20 69 6e 20 61 20 73 65 67 6d 65 6e 74 61 lt in a segmenta
186f0 74 69 6f 6e 20 66 61 75 6c 74 20 6f 72 20 6f 74 tion fault or ot
18700 68 65 72 20 73 65 76 65 72 65 20 65 72 72 6f 72 her severe error
18710 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 63 6f 72 72 ..** Memory corr
18720 75 70 74 69 6f 6e 2c 20 61 20 73 65 67 6d 65 6e uption, a segmen
18730 74 61 74 69 6f 6e 20 66 61 75 6c 74 2c 20 6f 72 tation fault, or
18740 20 6f 74 68 65 72 20 73 65 76 65 72 65 20 65 72 other severe er
18750 72 6f 72 0a 2a 2a 20 6d 69 67 68 74 20 72 65 73 ror.** might res
18760 75 6c 74 20 69 66 20 73 71 6c 69 74 65 33 5f 66 ult if sqlite3_f
18770 72 65 65 28 29 20 69 73 20 63 61 6c 6c 65 64 20 ree() is called
18780 77 69 74 68 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 with a non-NULL
18790 70 6f 69 6e 74 65 72 20 74 68 61 74 0a 2a 2a 20 pointer that.**
187a0 77 61 73 20 6e 6f 74 20 6f 62 74 61 69 6e 65 64 was not obtained
187b0 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 from sqlite3_ma
187c0 6c 6c 6f 63 28 29 20 6f 72 20 73 71 6c 69 74 65 lloc() or sqlite
187d0 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 3_realloc()..**.
187e0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 ** The sqlite3_r
187f0 65 61 6c 6c 6f 63 28 29 20 69 6e 74 65 72 66 61 ealloc() interfa
18800 63 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 72 ce attempts to r
18810 65 73 69 7a 65 20 61 0a 2a 2a 20 70 72 69 6f 72 esize a.** prior
18820 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
18830 6f 6e 20 74 6f 20 62 65 20 61 74 20 6c 65 61 73 on to be at leas
18840 74 20 4e 20 62 79 74 65 73 2c 20 77 68 65 72 65 t N bytes, where
18850 20 4e 20 69 73 20 74 68 65 0a 2a 2a 20 73 65 63 N is the.** sec
18860 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 2e 20 20 ond parameter.
18870 54 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 The memory alloc
18880 61 74 69 6f 6e 20 74 6f 20 62 65 20 72 65 73 69 ation to be resi
18890 7a 65 64 20 69 73 20 74 68 65 20 66 69 72 73 74 zed is the first
188a0 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 2e 20 20 .** parameter.
188b0 49 66 20 74 68 65 20 66 69 72 73 74 20 70 61 72 If the first par
188c0 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 ameter to sqlite
188d0 33 5f 72 65 61 6c 6c 6f 63 28 29 0a 2a 2a 20 69 3_realloc().** i
188e0 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 s a NULL pointer
188f0 20 74 68 65 6e 20 69 74 73 20 62 65 68 61 76 69 then its behavi
18900 6f 72 20 69 73 20 69 64 65 6e 74 69 63 61 6c 20 or is identical
18910 74 6f 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 73 71 to calling.** sq
18920 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 4e 29 20 lite3_malloc(N)
18930 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 73 where N is the s
18940 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 econd parameter
18950 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c to sqlite3_reall
18960 6f 63 28 29 2e 0a 2a 2a 20 49 66 20 74 68 65 20 oc()..** If the
18970 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 second parameter
18980 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 61 6c to sqlite3_real
18990 6c 6f 63 28 29 20 69 73 20 7a 65 72 6f 20 6f 72 loc() is zero or
189a0 0a 2a 2a 20 6e 65 67 61 74 69 76 65 20 74 68 65 .** negative the
189b0 6e 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 n the behavior i
189c0 73 20 65 78 61 63 74 6c 79 20 74 68 65 20 73 61 s exactly the sa
189d0 6d 65 20 61 73 20 63 61 6c 6c 69 6e 67 0a 2a 2a me as calling.**
189e0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 50 29 sqlite3_free(P)
189f0 20 77 68 65 72 65 20 50 20 69 73 20 74 68 65 20 where P is the
18a00 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 first parameter
18a10 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c to sqlite3_reall
18a20 6f 63 28 29 2e 0a 2a 2a 20 73 71 6c 69 74 65 33 oc()..** sqlite3
18a30 5f 72 65 61 6c 6c 6f 63 28 29 20 72 65 74 75 72 _realloc() retur
18a40 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ns a pointer to
18a50 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 a memory allocat
18a60 69 6f 6e 0a 2a 2a 20 6f 66 20 61 74 20 6c 65 61 ion.** of at lea
18a70 73 74 20 4e 20 62 79 74 65 73 20 69 6e 20 73 69 st N bytes in si
18a80 7a 65 20 6f 72 20 4e 55 4c 4c 20 69 66 20 73 75 ze or NULL if su
18a90 66 66 69 63 69 65 6e 74 20 6d 65 6d 6f 72 79 20 fficient memory
18aa0 69 73 20 75 6e 61 76 61 69 6c 61 62 6c 65 2e 0a is unavailable..
18ab0 2a 2a 20 49 66 20 4d 20 69 73 20 74 68 65 20 73 ** If M is the s
18ac0 69 7a 65 20 6f 66 20 74 68 65 20 70 72 69 6f 72 ize of the prior
18ad0 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 74 68 65 allocation, the
18ae0 6e 20 6d 69 6e 28 4e 2c 4d 29 20 62 79 74 65 73 n min(N,M) bytes
18af0 0a 2a 2a 20 6f 66 20 74 68 65 20 70 72 69 6f 72 .** of the prior
18b00 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 72 65 20 allocation are
18b10 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65 20 copied into the
18b20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 62 75 66 beginning of buf
18b30 66 65 72 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 fer returned.**
18b40 62 79 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c by sqlite3_reall
18b50 6f 63 28 29 20 61 6e 64 20 74 68 65 20 70 72 69 oc() and the pri
18b60 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 or allocation is
18b70 20 66 72 65 65 64 2e 0a 2a 2a 20 49 66 20 73 71 freed..** If sq
18b80 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 lite3_realloc()
18b90 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2c 20 74 68 returns NULL, th
18ba0 65 6e 20 74 68 65 20 70 72 69 6f 72 20 61 6c 6c en the prior all
18bb0 6f 63 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 6e 6f ocation.** is no
18bc0 74 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 t freed..**.** T
18bd0 68 65 20 6d 65 6d 6f 72 79 20 72 65 74 75 72 6e he memory return
18be0 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 61 ed by sqlite3_ma
18bf0 6c 6c 6f 63 28 29 20 61 6e 64 20 73 71 6c 69 74 lloc() and sqlit
18c00 65 33 5f 72 65 61 6c 6c 6f 63 28 29 0a 2a 2a 20 e3_realloc().**
18c10 69 73 20 61 6c 77 61 79 73 20 61 6c 69 67 6e 65 is always aligne
18c20 64 20 74 6f 20 61 74 20 6c 65 61 73 74 20 61 6e d to at least an
18c30 20 38 20 62 79 74 65 20 62 6f 75 6e 64 61 72 79 8 byte boundary
18c40 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 . {END}.**.** Th
18c50 65 20 64 65 66 61 75 6c 74 20 69 6d 70 6c 65 6d e default implem
18c60 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 entation of the
18c70 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
18c80 6e 20 73 75 62 73 79 73 74 65 6d 20 75 73 65 73 n subsystem uses
18c90 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c 6f 63 28 29 .** the malloc()
18ca0 2c 20 72 65 61 6c 6c 6f 63 28 29 20 61 6e 64 20 , realloc() and
18cb0 66 72 65 65 28 29 20 70 72 6f 76 69 64 65 64 20 free() provided
18cc0 62 79 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 by the standard
18cd0 43 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 20 7b 48 C library..** {H
18ce0 31 37 33 38 32 7d 20 48 6f 77 65 76 65 72 2c 20 17382} However,
18cf0 69 66 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d if SQLite is com
18d00 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a piled with the.*
18d10 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 4f 52 59 5f * SQLITE_MEMORY_
18d20 53 49 5a 45 3d 3c 69 3e 4e 4e 4e 3c 2f 69 3e 20 SIZE=<i>NNN</i>
18d30 43 20 70 72 65 70 72 6f 63 65 73 73 6f 72 20 6d C preprocessor m
18d40 61 63 72 6f 20 28 77 68 65 72 65 20 3c 69 3e 4e acro (where <i>N
18d50 4e 4e 3c 2f 69 3e 0a 2a 2a 20 69 73 20 61 6e 20 NN</i>.** is an
18d60 69 6e 74 65 67 65 72 29 2c 20 74 68 65 6e 20 53 integer), then S
18d70 51 4c 69 74 65 20 63 72 65 61 74 65 20 61 20 73 QLite create a s
18d80 74 61 74 69 63 20 61 72 72 61 79 20 6f 66 20 61 tatic array of a
18d90 74 20 6c 65 61 73 74 0a 2a 2a 20 3c 69 3e 4e 4e t least.** <i>NN
18da0 4e 3c 2f 69 3e 20 62 79 74 65 73 20 69 6e 20 73 N</i> bytes in s
18db0 69 7a 65 20 61 6e 64 20 75 73 65 73 20 74 68 61 ize and uses tha
18dc0 74 20 61 72 72 61 79 20 66 6f 72 20 61 6c 6c 20 t array for all
18dd0 6f 66 20 69 74 73 20 64 79 6e 61 6d 69 63 0a 2a of its dynamic.*
18de0 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 * memory allocat
18df0 69 6f 6e 20 6e 65 65 64 73 2e 20 7b 45 4e 44 7d ion needs. {END}
18e00 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 6d 65 6d Additional mem
18e10 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 6f 70 ory allocator op
18e20 74 69 6f 6e 73 0a 2a 2a 20 6d 61 79 20 62 65 20 tions.** may be
18e30 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 65 20 added in future
18e40 72 65 6c 65 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 releases..**.**
18e50 49 6e 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f In SQLite versio
18e60 6e 20 33 2e 35 2e 30 20 61 6e 64 20 33 2e 35 2e n 3.5.0 and 3.5.
18e70 31 2c 20 69 74 20 77 61 73 20 70 6f 73 73 69 62 1, it was possib
18e80 6c 65 20 74 6f 20 64 65 66 69 6e 65 0a 2a 2a 20 le to define.**
18e90 74 68 65 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f the SQLITE_OMIT_
18ea0 4d 45 4d 4f 52 59 5f 41 4c 4c 4f 43 41 54 49 4f MEMORY_ALLOCATIO
18eb0 4e 20 77 68 69 63 68 20 77 6f 75 6c 64 20 63 61 N which would ca
18ec0 75 73 65 20 74 68 65 20 62 75 69 6c 74 2d 69 6e use the built-in
18ed0 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 .** implementati
18ee0 6f 6e 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 on of these rout
18ef0 69 6e 65 73 20 74 6f 20 62 65 20 6f 6d 69 74 74 ines to be omitt
18f00 65 64 2e 20 20 54 68 61 74 20 63 61 70 61 62 69 ed. That capabi
18f10 6c 69 74 79 0a 2a 2a 20 69 73 20 6e 6f 20 6c 6f lity.** is no lo
18f20 6e 67 65 72 20 70 72 6f 76 69 64 65 64 2e 20 20 nger provided.
18f30 4f 6e 6c 79 20 62 75 69 6c 74 2d 69 6e 20 6d 65 Only built-in me
18f40 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 73 20 mory allocators
18f50 63 61 6e 20 62 65 20 75 73 65 64 2e 0a 2a 2a 0a can be used..**.
18f60 2a 2a 20 54 68 65 20 57 69 6e 64 6f 77 73 20 4f ** The Windows O
18f70 53 20 69 6e 74 65 72 66 61 63 65 20 6c 61 79 65 S interface laye
18f80 72 20 63 61 6c 6c 73 0a 2a 2a 20 74 68 65 20 73 r calls.** the s
18f90 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 29 20 61 ystem malloc() a
18fa0 6e 64 20 66 72 65 65 28 29 20 64 69 72 65 63 74 nd free() direct
18fb0 6c 79 20 77 68 65 6e 20 63 6f 6e 76 65 72 74 69 ly when converti
18fc0 6e 67 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65 73 20 ng.** filenames
18fd0 62 65 74 77 65 65 6e 20 74 68 65 20 55 54 46 2d between the UTF-
18fe0 38 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 8 encoding used
18ff0 62 79 20 53 51 4c 69 74 65 0a 2a 2a 20 61 6e 64 by SQLite.** and
19000 20 77 68 61 74 65 76 65 72 20 66 69 6c 65 6e 61 whatever filena
19010 6d 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 75 me encoding is u
19020 73 65 64 20 62 79 20 74 68 65 20 70 61 72 74 69 sed by the parti
19030 63 75 6c 61 72 20 57 69 6e 64 6f 77 73 0a 2a 2a cular Windows.**
19040 20 69 6e 73 74 61 6c 6c 61 74 69 6f 6e 2e 20 20 installation.
19050 4d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f Memory allocatio
19060 6e 20 65 72 72 6f 72 73 20 61 72 65 20 64 65 74 n errors are det
19070 65 63 74 65 64 2c 20 62 75 74 0a 2a 2a 20 74 68 ected, but.** th
19080 65 79 20 61 72 65 20 72 65 70 6f 72 74 65 64 20 ey are reported
19090 62 61 63 6b 20 61 73 20 5b 53 51 4c 49 54 45 5f back as [SQLITE_
190a0 43 41 4e 54 4f 50 45 4e 5d 20 6f 72 0a 2a 2a 20 CANTOPEN] or.**
190b0 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5d 20 72 [SQLITE_IOERR] r
190c0 61 74 68 65 72 20 74 68 61 6e 20 5b 53 51 4c 49 ather than [SQLI
190d0 54 45 5f 4e 4f 4d 45 4d 5d 2e 0a 2a 2a 0a 2a 2a TE_NOMEM]..**.**
190e0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a Requirements:.*
190f0 2a 20 5b 48 31 37 33 30 33 5d 20 5b 48 31 37 33 * [H17303] [H173
19100 30 34 5d 20 5b 48 31 37 33 30 35 5d 20 5b 48 31 04] [H17305] [H1
19110 37 33 30 36 5d 20 5b 48 31 37 33 31 30 5d 20 5b 7306] [H17310] [
19120 48 31 37 33 31 32 5d 20 5b 48 31 37 33 31 35 5d H17312] [H17315]
19130 20 5b 48 31 37 33 31 38 5d 0a 2a 2a 20 5b 48 31 [H17318].** [H1
19140 37 33 32 31 5d 20 5b 48 31 37 33 32 32 5d 20 5b 7321] [H17322] [
19150 48 31 37 33 32 33 5d 0a 2a 2a 0a 2a 2a 20 54 68 H17323].**.** Th
19160 65 20 70 6f 69 6e 74 65 72 20 61 72 67 75 6d 65 e pointer argume
19170 6e 74 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f nts to [sqlite3_
19180 66 72 65 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c free()] and [sql
19190 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 0a ite3_realloc()].
191a0 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 ** must be eithe
191b0 72 20 4e 55 4c 4c 20 6f 72 20 65 6c 73 65 20 70 r NULL or else p
191c0 6f 69 6e 74 65 72 73 20 6f 62 74 61 69 6e 65 64 ointers obtained
191d0 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 0a 2a 2a from a prior.**
191e0 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 5b invocation of [
191f0 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 sqlite3_malloc()
19200 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 ] or [sqlite3_re
19210 61 6c 6c 6f 63 28 29 5d 20 74 68 61 74 20 68 61 alloc()] that ha
19220 76 65 0a 2a 2a 20 6e 6f 74 20 79 65 74 20 62 65 ve.** not yet be
19230 65 6e 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 0a en released..**.
19240 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 ** The applicati
19250 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 72 65 61 64 on must not read
19260 20 6f 72 20 77 72 69 74 65 20 61 6e 79 20 70 61 or write any pa
19270 72 74 20 6f 66 0a 2a 2a 20 61 20 62 6c 6f 63 6b rt of.** a block
19280 20 6f 66 20 6d 65 6d 6f 72 79 20 61 66 74 65 72 of memory after
19290 20 69 74 20 68 61 73 20 62 65 65 6e 20 72 65 6c it has been rel
192a0 65 61 73 65 64 20 75 73 69 6e 67 0a 2a 2a 20 5b eased using.** [
192b0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 20 sqlite3_free()]
192c0 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c or [sqlite3_real
192d0 6c 6f 63 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 loc()]..*/.SQLIT
192e0 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 E_API void *sqli
192f0 74 65 33 5f 6d 61 6c 6c 6f 63 28 69 6e 74 29 3b te3_malloc(int);
19300 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
19310 20 2a 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f *sqlite3_reallo
19320 63 28 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 53 c(void*, int);.S
19330 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
19340 71 6c 69 74 65 33 5f 66 72 65 65 28 76 6f 69 64 qlite3_free(void
19350 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 *);../*.** CAPI3
19360 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f REF: Memory Allo
19370 63 61 74 6f 72 20 53 74 61 74 69 73 74 69 63 73 cator Statistics
19380 20 7b 48 31 37 33 37 30 7d 20 3c 53 33 30 32 31 {H17370} <S3021
19390 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 0>.**.** SQLite
193a0 70 72 6f 76 69 64 65 73 20 74 68 65 73 65 20 74 provides these t
193b0 77 6f 20 69 6e 74 65 72 66 61 63 65 73 20 66 6f wo interfaces fo
193c0 72 20 72 65 70 6f 72 74 69 6e 67 20 6f 6e 20 74 r reporting on t
193d0 68 65 20 73 74 61 74 75 73 0a 2a 2a 20 6f 66 20 he status.** of
193e0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c the [sqlite3_mal
193f0 6c 6f 63 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 loc()], [sqlite3
19400 5f 66 72 65 65 28 29 5d 2c 20 61 6e 64 20 5b 73 _free()], and [s
19410 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 qlite3_realloc()
19420 5d 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 2c 20 77 ].** routines, w
19430 68 69 63 68 20 66 6f 72 6d 20 74 68 65 20 62 75 hich form the bu
19440 69 6c 74 2d 69 6e 20 6d 65 6d 6f 72 79 20 61 6c ilt-in memory al
19450 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 location subsyst
19460 65 6d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 em..**.** Requir
19470 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 33 ements:.** [H173
19480 37 31 5d 20 5b 48 31 37 33 37 33 5d 20 5b 48 31 71] [H17373] [H1
19490 37 33 37 34 5d 20 5b 48 31 37 33 37 35 5d 0a 2a 7374] [H17375].*
194a0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c /.SQLITE_API sql
194b0 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 ite3_int64 sqlit
194c0 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 76 e3_memory_used(v
194d0 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 oid);.SQLITE_API
194e0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 sqlite3_int64 s
194f0 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 qlite3_memory_hi
19500 67 68 77 61 74 65 72 28 69 6e 74 20 72 65 73 65 ghwater(int rese
19510 74 46 6c 61 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 tFlag);../*.** C
19520 41 50 49 33 52 45 46 3a 20 50 73 65 75 64 6f 2d API3REF: Pseudo-
19530 52 61 6e 64 6f 6d 20 4e 75 6d 62 65 72 20 47 65 Random Number Ge
19540 6e 65 72 61 74 6f 72 20 7b 48 31 37 33 39 30 7d nerator {H17390}
19550 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S20000>.**.**
19560 53 51 4c 69 74 65 20 63 6f 6e 74 61 69 6e 73 20 SQLite contains
19570 61 20 68 69 67 68 2d 71 75 61 6c 69 74 79 20 70 a high-quality p
19580 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20 6e 75 6d seudo-random num
19590 62 65 72 20 67 65 6e 65 72 61 74 6f 72 20 28 50 ber generator (P
195a0 52 4e 47 29 20 75 73 65 64 20 74 6f 0a 2a 2a 20 RNG) used to.**
195b0 73 65 6c 65 63 74 20 72 61 6e 64 6f 6d 20 5b 52 select random [R
195c0 4f 57 49 44 20 7c 20 52 4f 57 49 44 73 5d 20 77 OWID | ROWIDs] w
195d0 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 20 6e 65 hen inserting ne
195e0 77 20 72 65 63 6f 72 64 73 20 69 6e 74 6f 20 61 w records into a
195f0 20 74 61 62 6c 65 20 74 68 61 74 0a 2a 2a 20 61 table that.** a
19600 6c 72 65 61 64 79 20 75 73 65 73 20 74 68 65 20 lready uses the
19610 6c 61 72 67 65 73 74 20 70 6f 73 73 69 62 6c 65 largest possible
19620 20 5b 52 4f 57 49 44 5d 2e 20 20 54 68 65 20 50 [ROWID]. The P
19630 52 4e 47 20 69 73 20 61 6c 73 6f 20 75 73 65 64 RNG is also used
19640 20 66 6f 72 0a 2a 2a 20 74 68 65 20 62 75 69 6c for.** the buil
19650 64 2d 69 6e 20 72 61 6e 64 6f 6d 28 29 20 61 6e d-in random() an
19660 64 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 29 20 53 d randomblob() S
19670 51 4c 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 QL functions. T
19680 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 61 6c his interface al
19690 6c 6f 77 73 0a 2a 2a 20 61 70 70 6c 69 63 61 74 lows.** applicat
196a0 69 6f 6e 73 20 74 6f 20 61 63 63 65 73 73 20 74 ions to access t
196b0 68 65 20 73 61 6d 65 20 50 52 4e 47 20 66 6f 72 he same PRNG for
196c0 20 6f 74 68 65 72 20 70 75 72 70 6f 73 65 73 2e other purposes.
196d0 0a 2a 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f .**.** A call to
196e0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73 74 this routine st
196f0 6f 72 65 73 20 4e 20 62 79 74 65 73 20 6f 66 20 ores N bytes of
19700 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e 74 6f 20 randomness into
19710 62 75 66 66 65 72 20 50 2e 0a 2a 2a 0a 2a 2a 20 buffer P..**.**
19720 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 The first time t
19730 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 69 his routine is i
19740 6e 76 6f 6b 65 64 20 28 65 69 74 68 65 72 20 69 nvoked (either i
19750 6e 74 65 72 6e 61 6c 6c 79 20 6f 72 20 62 79 0a nternally or by.
19760 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 ** the applicati
19770 6f 6e 29 20 74 68 65 20 50 52 4e 47 20 69 73 20 on) the PRNG is
19780 73 65 65 64 65 64 20 75 73 69 6e 67 20 72 61 6e seeded using ran
19790 64 6f 6d 6e 65 73 73 20 6f 62 74 61 69 6e 65 64 domness obtained
197a0 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 78 52 61 .** from the xRa
197b0 6e 64 6f 6d 6e 65 73 73 20 6d 65 74 68 6f 64 20 ndomness method
197c0 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b of the default [
197d0 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a sqlite3_vfs] obj
197e0 65 63 74 2e 0a 2a 2a 20 4f 6e 20 61 6c 6c 20 73 ect..** On all s
197f0 75 62 73 65 71 75 65 6e 74 20 69 6e 76 6f 63 61 ubsequent invoca
19800 74 69 6f 6e 73 2c 20 74 68 65 20 70 73 65 75 64 tions, the pseud
19810 6f 2d 72 61 6e 64 6f 6d 6e 65 73 73 20 69 73 20 o-randomness is
19820 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20 69 6e 74 generated.** int
19830 65 72 6e 61 6c 6c 79 20 61 6e 64 20 77 69 74 68 ernally and with
19840 6f 75 74 20 72 65 63 6f 75 72 73 65 20 74 6f 20 out recourse to
19850 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 the [sqlite3_vfs
19860 5d 20 78 52 61 6e 64 6f 6d 6e 65 73 73 0a 2a 2a ] xRandomness.**
19870 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 52 method..**.** R
19880 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 equirements:.**
19890 5b 48 31 37 33 39 32 5d 0a 2a 2f 0a 53 51 4c 49 [H17392].*/.SQLI
198a0 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
198b0 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 69 te3_randomness(i
198c0 6e 74 20 4e 2c 20 76 6f 69 64 20 2a 50 29 3b 0a nt N, void *P);.
198d0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
198e0 20 43 6f 6d 70 69 6c 65 2d 54 69 6d 65 20 41 75 Compile-Time Au
198f0 74 68 6f 72 69 7a 61 74 69 6f 6e 20 43 61 6c 6c thorization Call
19900 62 61 63 6b 73 20 7b 48 31 32 35 30 30 7d 20 3c backs {H12500} <
19910 53 37 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S70100>.**.** Th
19920 69 73 20 72 6f 75 74 69 6e 65 20 72 65 67 69 73 is routine regis
19930 74 65 72 73 20 61 20 61 75 74 68 6f 72 69 7a 65 ters a authorize
19940 72 20 63 61 6c 6c 62 61 63 6b 20 77 69 74 68 20 r callback with
19950 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 a particular.**
19960 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
19970 74 69 6f 6e 5d 2c 20 73 75 70 70 6c 69 65 64 20 tion], supplied
19980 69 6e 20 74 68 65 20 66 69 72 73 74 20 61 72 67 in the first arg
19990 75 6d 65 6e 74 2e 0a 2a 2a 20 54 68 65 20 61 75 ument..** The au
199a0 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 thorizer callbac
199b0 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 73 20 k is invoked as
199c0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 61 SQL statements a
199d0 72 65 20 62 65 69 6e 67 20 63 6f 6d 70 69 6c 65 re being compile
199e0 64 0a 2a 2a 20 62 79 20 5b 73 71 6c 69 74 65 33 d.** by [sqlite3
199f0 5f 70 72 65 70 61 72 65 28 29 5d 20 6f 72 20 69 _prepare()] or i
19a00 74 73 20 76 61 72 69 61 6e 74 73 20 5b 73 71 6c ts variants [sql
19a10 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 ite3_prepare_v2(
19a20 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f )],.** [sqlite3_
19a30 70 72 65 70 61 72 65 31 36 28 29 5d 20 61 6e 64 prepare16()] and
19a40 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 [sqlite3_prepar
19a50 65 31 36 5f 76 32 28 29 5d 2e 20 20 41 74 20 76 e16_v2()]. At v
19a60 61 72 69 6f 75 73 0a 2a 2a 20 70 6f 69 6e 74 73 arious.** points
19a70 20 64 75 72 69 6e 67 20 74 68 65 20 63 6f 6d 70 during the comp
19a80 69 6c 61 74 69 6f 6e 20 70 72 6f 63 65 73 73 2c ilation process,
19a90 20 61 73 20 6c 6f 67 69 63 20 69 73 20 62 65 69 as logic is bei
19aa0 6e 67 20 63 72 65 61 74 65 64 0a 2a 2a 20 74 6f ng created.** to
19ab0 20 70 65 72 66 6f 72 6d 20 76 61 72 69 6f 75 73 perform various
19ac0 20 61 63 74 69 6f 6e 73 2c 20 74 68 65 20 61 75 actions, the au
19ad0 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 thorizer callbac
19ae0 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f 0a k is invoked to.
19af0 2a 2a 20 73 65 65 20 69 66 20 74 68 6f 73 65 20 ** see if those
19b00 61 63 74 69 6f 6e 73 20 61 72 65 20 61 6c 6c 6f actions are allo
19b10 77 65 64 2e 20 20 54 68 65 20 61 75 74 68 6f 72 wed. The author
19b20 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 73 68 izer callback sh
19b30 6f 75 6c 64 0a 2a 2a 20 72 65 74 75 72 6e 20 5b ould.** return [
19b40 53 51 4c 49 54 45 5f 4f 4b 5d 20 74 6f 20 61 6c SQLITE_OK] to al
19b50 6c 6f 77 20 74 68 65 20 61 63 74 69 6f 6e 2c 20 low the action,
19b60 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 20 [SQLITE_IGNORE]
19b70 74 6f 20 64 69 73 61 6c 6c 6f 77 20 74 68 65 0a to disallow the.
19b80 2a 2a 20 73 70 65 63 69 66 69 63 20 61 63 74 69 ** specific acti
19b90 6f 6e 20 62 75 74 20 61 6c 6c 6f 77 20 74 68 65 on but allow the
19ba0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 SQL statement t
19bb0 6f 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 62 65 o continue to be
19bc0 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 2c 20 6f 72 .** compiled, or
19bd0 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59 5d 20 74 [SQLITE_DENY] t
19be0 6f 20 63 61 75 73 65 20 74 68 65 20 65 6e 74 69 o cause the enti
19bf0 72 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 re SQL statement
19c00 20 74 6f 20 62 65 0a 2a 2a 20 72 65 6a 65 63 74 to be.** reject
19c10 65 64 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 ed with an error
19c20 2e 20 20 49 66 20 74 68 65 20 61 75 74 68 6f 72 . If the author
19c30 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 72 65 izer callback re
19c40 74 75 72 6e 73 0a 2a 2a 20 61 6e 79 20 76 61 6c turns.** any val
19c50 75 65 20 6f 74 68 65 72 20 74 68 61 6e 20 5b 53 ue other than [S
19c60 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 2c 20 5b QLITE_IGNORE], [
19c70 53 51 4c 49 54 45 5f 4f 4b 5d 2c 20 6f 72 20 5b SQLITE_OK], or [
19c80 53 51 4c 49 54 45 5f 44 45 4e 59 5d 0a 2a 2a 20 SQLITE_DENY].**
19c90 74 68 65 6e 20 74 68 65 20 5b 73 71 6c 69 74 65 then the [sqlite
19ca0 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 3_prepare_v2()]
19cb0 6f 72 20 65 71 75 69 76 61 6c 65 6e 74 20 63 61 or equivalent ca
19cc0 6c 6c 20 74 68 61 74 20 74 72 69 67 67 65 72 65 ll that triggere
19cd0 64 0a 2a 2a 20 74 68 65 20 61 75 74 68 6f 72 69 d.** the authori
19ce0 7a 65 72 20 77 69 6c 6c 20 66 61 69 6c 20 77 69 zer will fail wi
19cf0 74 68 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 th an error mess
19d00 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 age..**.** When
19d10 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 the callback ret
19d20 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d urns [SQLITE_OK]
19d30 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 65 , that means the
19d40 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20 72 65 operation.** re
19d50 71 75 65 73 74 65 64 20 69 73 20 6f 6b 2e 20 20 quested is ok.
19d60 57 68 65 6e 20 74 68 65 20 63 61 6c 6c 62 61 63 When the callbac
19d70 6b 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 k returns [SQLIT
19d80 45 5f 44 45 4e 59 5d 2c 20 74 68 65 0a 2a 2a 20 E_DENY], the.**
19d90 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
19da0 5f 76 32 28 29 5d 20 6f 72 20 65 71 75 69 76 61 _v2()] or equiva
19db0 6c 65 6e 74 20 63 61 6c 6c 20 74 68 61 74 20 74 lent call that t
19dc0 72 69 67 67 65 72 65 64 20 74 68 65 0a 2a 2a 20 riggered the.**
19dd0 61 75 74 68 6f 72 69 7a 65 72 20 77 69 6c 6c 20 authorizer will
19de0 66 61 69 6c 20 77 69 74 68 20 61 6e 20 65 72 72 fail with an err
19df0 6f 72 20 6d 65 73 73 61 67 65 20 65 78 70 6c 61 or message expla
19e00 69 6e 69 6e 67 20 74 68 61 74 0a 2a 2a 20 61 63 ining that.** ac
19e10 63 65 73 73 20 69 73 20 64 65 6e 69 65 64 2e 20 cess is denied.
19e20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 .**.** The first
19e30 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 parameter to th
19e40 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c e authorizer cal
19e50 6c 62 61 63 6b 20 69 73 20 61 20 63 6f 70 79 20 lback is a copy
19e60 6f 66 20 74 68 65 20 74 68 69 72 64 0a 2a 2a 20 of the third.**
19e70 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 parameter to the
19e80 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 sqlite3_set_aut
19e90 68 6f 72 69 7a 65 72 28 29 20 69 6e 74 65 72 66 horizer() interf
19ea0 61 63 65 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 ace. The second
19eb0 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 74 6f 20 parameter.** to
19ec0 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 the callback is
19ed0 61 6e 20 69 6e 74 65 67 65 72 20 5b 53 51 4c 49 an integer [SQLI
19ee0 54 45 5f 43 4f 50 59 20 7c 20 61 63 74 69 6f 6e TE_COPY | action
19ef0 20 63 6f 64 65 5d 20 74 68 61 74 20 73 70 65 63 code] that spec
19f00 69 66 69 65 73 0a 2a 2a 20 74 68 65 20 70 61 72 ifies.** the par
19f10 74 69 63 75 6c 61 72 20 61 63 74 69 6f 6e 20 74 ticular action t
19f20 6f 20 62 65 20 61 75 74 68 6f 72 69 7a 65 64 2e o be authorized.
19f30 20 54 68 65 20 74 68 69 72 64 20 74 68 72 6f 75 The third throu
19f40 67 68 20 73 69 78 74 68 20 70 61 72 61 6d 65 74 gh sixth paramet
19f50 65 72 73 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61 ers.** to the ca
19f60 6c 6c 62 61 63 6b 20 61 72 65 20 7a 65 72 6f 2d llback are zero-
19f70 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e terminated strin
19f80 67 73 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 20 gs that contain
19f90 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 64 65 additional.** de
19fa0 74 61 69 6c 73 20 61 62 6f 75 74 20 74 68 65 20 tails about the
19fb0 61 63 74 69 6f 6e 20 74 6f 20 62 65 20 61 75 74 action to be aut
19fc0 68 6f 72 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 horized..**.** I
19fd0 66 20 74 68 65 20 61 63 74 69 6f 6e 20 63 6f 64 f the action cod
19fe0 65 20 69 73 20 5b 53 51 4c 49 54 45 5f 52 45 41 e is [SQLITE_REA
19ff0 44 5d 0a 2a 2a 20 61 6e 64 20 74 68 65 20 63 61 D].** and the ca
1a000 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 5b llback returns [
1a010 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 20 74 SQLITE_IGNORE] t
1a020 68 65 6e 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70 hen the.** [prep
1a030 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 ared statement]
1a040 73 74 61 74 65 6d 65 6e 74 20 69 73 20 63 6f 6e statement is con
1a050 73 74 72 75 63 74 65 64 20 74 6f 20 73 75 62 73 structed to subs
1a060 74 69 74 75 74 65 0a 2a 2a 20 61 20 4e 55 4c 4c titute.** a NULL
1a070 20 76 61 6c 75 65 20 69 6e 20 70 6c 61 63 65 20 value in place
1a080 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6c of the table col
1a090 75 6d 6e 20 74 68 61 74 20 77 6f 75 6c 64 20 68 umn that would h
1a0a0 61 76 65 0a 2a 2a 20 62 65 65 6e 20 72 65 61 64 ave.** been read
1a0b0 20 69 66 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 if [SQLITE_OK]
1a0c0 68 61 64 20 62 65 65 6e 20 72 65 74 75 72 6e 65 had been returne
1a0d0 64 2e 20 20 54 68 65 20 5b 53 51 4c 49 54 45 5f d. The [SQLITE_
1a0e0 49 47 4e 4f 52 45 5d 0a 2a 2a 20 72 65 74 75 72 IGNORE].** retur
1a0f0 6e 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f n can be used to
1a100 20 64 65 6e 79 20 61 6e 20 75 6e 74 72 75 73 74 deny an untrust
1a110 65 64 20 75 73 65 72 20 61 63 63 65 73 73 20 74 ed user access t
1a120 6f 20 69 6e 64 69 76 69 64 75 61 6c 0a 2a 2a 20 o individual.**
1a130 63 6f 6c 75 6d 6e 73 20 6f 66 20 61 20 74 61 62 columns of a tab
1a140 6c 65 2e 0a 2a 2a 20 49 66 20 74 68 65 20 61 63 le..** If the ac
1a150 74 69 6f 6e 20 63 6f 64 65 20 69 73 20 5b 53 51 tion code is [SQ
1a160 4c 49 54 45 5f 44 45 4c 45 54 45 5d 20 61 6e 64 LITE_DELETE] and
1a170 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 the callback re
1a180 74 75 72 6e 73 0a 2a 2a 20 5b 53 51 4c 49 54 45 turns.** [SQLITE
1a190 5f 49 47 4e 4f 52 45 5d 20 74 68 65 6e 20 74 68 _IGNORE] then th
1a1a0 65 20 5b 44 45 4c 45 54 45 5d 20 6f 70 65 72 61 e [DELETE] opera
1a1b0 74 69 6f 6e 20 70 72 6f 63 65 65 64 73 20 62 75 tion proceeds bu
1a1c0 74 20 74 68 65 0a 2a 2a 20 5b 74 72 75 6e 63 61 t the.** [trunca
1a1d0 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 5d te optimization]
1a1e0 20 69 73 20 64 69 73 61 62 6c 65 64 20 61 6e 64 is disabled and
1a1f0 20 61 6c 6c 20 72 6f 77 73 20 61 72 65 20 64 65 all rows are de
1a200 6c 65 74 65 64 20 69 6e 64 69 76 69 64 75 61 6c leted individual
1a210 6c 79 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 75 74 ly..**.** An aut
1a220 68 6f 72 69 7a 65 72 20 69 73 20 75 73 65 64 20 horizer is used
1a230 77 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72 when [sqlite3_pr
1a240 65 70 61 72 65 20 7c 20 70 72 65 70 61 72 69 6e epare | preparin
1a250 67 5d 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d g].** SQL statem
1a260 65 6e 74 73 20 66 72 6f 6d 20 61 6e 20 75 6e 74 ents from an unt
1a270 72 75 73 74 65 64 20 73 6f 75 72 63 65 2c 20 74 rusted source, t
1a280 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 o ensure that th
1a290 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 e SQL statements
1a2a0 0a 2a 2a 20 64 6f 20 6e 6f 74 20 74 72 79 20 74 .** do not try t
1a2b0 6f 20 61 63 63 65 73 73 20 64 61 74 61 20 74 68 o access data th
1a2c0 65 79 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 ey are not allow
1a2d0 65 64 20 74 6f 20 73 65 65 2c 20 6f 72 20 74 68 ed to see, or th
1a2e0 61 74 20 74 68 65 79 20 64 6f 20 6e 6f 74 0a 2a at they do not.*
1a2f0 2a 20 74 72 79 20 74 6f 20 65 78 65 63 75 74 65 * try to execute
1a300 20 6d 61 6c 69 63 69 6f 75 73 20 73 74 61 74 65 malicious state
1a310 6d 65 6e 74 73 20 74 68 61 74 20 64 61 6d 61 67 ments that damag
1a320 65 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 e the database.
1a330 20 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c For.** example,
1a340 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 an application
1a350 6d 61 79 20 61 6c 6c 6f 77 20 61 20 75 73 65 72 may allow a user
1a360 20 74 6f 20 65 6e 74 65 72 20 61 72 62 69 74 72 to enter arbitr
1a370 61 72 79 0a 2a 2a 20 53 51 4c 20 71 75 65 72 69 ary.** SQL queri
1a380 65 73 20 66 6f 72 20 65 76 61 6c 75 61 74 69 6f es for evaluatio
1a390 6e 20 62 79 20 61 20 64 61 74 61 62 61 73 65 2e n by a database.
1a3a0 20 20 42 75 74 20 74 68 65 20 61 70 70 6c 69 63 But the applic
1a3b0 61 74 69 6f 6e 20 64 6f 65 73 0a 2a 2a 20 6e 6f ation does.** no
1a3c0 74 20 77 61 6e 74 20 74 68 65 20 75 73 65 72 20 t want the user
1a3d0 74 6f 20 62 65 20 61 62 6c 65 20 74 6f 20 6d 61 to be able to ma
1a3e0 6b 65 20 61 72 62 69 74 72 61 72 79 20 63 68 61 ke arbitrary cha
1a3f0 6e 67 65 73 20 74 6f 20 74 68 65 0a 2a 2a 20 64 nges to the.** d
1a400 61 74 61 62 61 73 65 2e 20 20 41 6e 20 61 75 74 atabase. An aut
1a410 68 6f 72 69 7a 65 72 20 63 6f 75 6c 64 20 74 68 horizer could th
1a420 65 6e 20 62 65 20 70 75 74 20 69 6e 20 70 6c 61 en be put in pla
1a430 63 65 20 77 68 69 6c 65 20 74 68 65 0a 2a 2a 20 ce while the.**
1a440 75 73 65 72 2d 65 6e 74 65 72 65 64 20 53 51 4c user-entered SQL
1a450 20 69 73 20 62 65 69 6e 67 20 5b 73 71 6c 69 74 is being [sqlit
1a460 65 33 5f 70 72 65 70 61 72 65 20 7c 20 70 72 65 e3_prepare | pre
1a470 70 61 72 65 64 5d 20 74 68 61 74 0a 2a 2a 20 64 pared] that.** d
1a480 69 73 61 6c 6c 6f 77 73 20 65 76 65 72 79 74 68 isallows everyth
1a490 69 6e 67 20 65 78 63 65 70 74 20 5b 53 45 4c 45 ing except [SELE
1a4a0 43 54 5d 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a CT] statements..
1a4b0 2a 2a 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69 6f **.** Applicatio
1a4c0 6e 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 ns that need to
1a4d0 70 72 6f 63 65 73 73 20 53 51 4c 20 66 72 6f 6d process SQL from
1a4e0 20 75 6e 74 72 75 73 74 65 64 20 73 6f 75 72 63 untrusted sourc
1a4f0 65 73 0a 2a 2a 20 6d 69 67 68 74 20 61 6c 73 6f es.** might also
1a500 20 63 6f 6e 73 69 64 65 72 20 6c 6f 77 65 72 69 consider loweri
1a510 6e 67 20 72 65 73 6f 75 72 63 65 20 6c 69 6d 69 ng resource limi
1a520 74 73 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 ts using [sqlite
1a530 33 5f 6c 69 6d 69 74 28 29 5d 0a 2a 2a 20 61 6e 3_limit()].** an
1a540 64 20 6c 69 6d 69 74 69 6e 67 20 64 61 74 61 62 d limiting datab
1a550 61 73 65 20 73 69 7a 65 20 75 73 69 6e 67 20 74 ase size using t
1a560 68 65 20 5b 6d 61 78 5f 70 61 67 65 5f 63 6f 75 he [max_page_cou
1a570 6e 74 5d 20 5b 50 52 41 47 4d 41 5d 0a 2a 2a 20 nt] [PRAGMA].**
1a580 69 6e 20 61 64 64 69 74 69 6f 6e 20 74 6f 20 75 in addition to u
1a590 73 69 6e 67 20 61 6e 20 61 75 74 68 6f 72 69 7a sing an authoriz
1a5a0 65 72 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 61 er..**.** Only a
1a5b0 20 73 69 6e 67 6c 65 20 61 75 74 68 6f 72 69 7a single authoriz
1a5c0 65 72 20 63 61 6e 20 62 65 20 69 6e 20 70 6c 61 er can be in pla
1a5d0 63 65 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65 ce on a database
1a5e0 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 61 connection.** a
1a5f0 74 20 61 20 74 69 6d 65 2e 20 20 45 61 63 68 20 t a time. Each
1a600 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f call to sqlite3_
1a610 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 20 6f set_authorizer o
1a620 76 65 72 72 69 64 65 73 20 74 68 65 0a 2a 2a 20 verrides the.**
1a630 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 2e 20 20 previous call.
1a640 44 69 73 61 62 6c 65 20 74 68 65 20 61 75 74 68 Disable the auth
1a650 6f 72 69 7a 65 72 20 62 79 20 69 6e 73 74 61 6c orizer by instal
1a660 6c 69 6e 67 20 61 20 4e 55 4c 4c 20 63 61 6c 6c ling a NULL call
1a670 62 61 63 6b 2e 0a 2a 2a 20 54 68 65 20 61 75 74 back..** The aut
1a680 68 6f 72 69 7a 65 72 20 69 73 20 64 69 73 61 62 horizer is disab
1a690 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 2e 0a led by default..
1a6a0 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
1a6b0 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 6d 75 izer callback mu
1a6c0 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68 69 st not do anythi
1a6d0 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f 64 ng that will mod
1a6e0 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 ify.** the datab
1a6f0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 ase connection t
1a700 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 hat invoked the
1a710 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 authorizer callb
1a720 61 63 6b 2e 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 ack..** Note tha
1a730 74 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 t [sqlite3_prepa
1a740 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b 73 71 re_v2()] and [sq
1a750 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 62 6f lite3_step()] bo
1a760 74 68 20 6d 6f 64 69 66 79 20 74 68 65 69 72 0a th modify their.
1a770 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e ** database conn
1a780 65 63 74 69 6f 6e 73 20 66 6f 72 20 74 68 65 20 ections for the
1a790 6d 65 61 6e 69 6e 67 20 6f 66 20 22 6d 6f 64 69 meaning of "modi
1a7a0 66 79 22 20 69 6e 20 74 68 69 73 20 70 61 72 61 fy" in this para
1a7b0 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 graph..**.** Whe
1a7c0 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 n [sqlite3_prepa
1a7d0 72 65 5f 76 32 28 29 5d 20 69 73 20 75 73 65 64 re_v2()] is used
1a7e0 20 74 6f 20 70 72 65 70 61 72 65 20 61 20 73 74 to prepare a st
1a7f0 61 74 65 6d 65 6e 74 2c 20 74 68 65 0a 2a 2a 20 atement, the.**
1a800 73 74 61 74 65 6d 65 6e 74 20 6d 69 67 68 74 20 statement might
1a810 62 65 20 72 65 2d 70 72 65 70 61 72 65 64 20 64 be re-prepared d
1a820 75 72 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 73 uring [sqlite3_s
1a830 74 65 70 28 29 5d 20 64 75 65 20 74 6f 20 61 20 tep()] due to a
1a840 0a 2a 2a 20 73 63 68 65 6d 61 20 63 68 61 6e 67 .** schema chang
1a850 65 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 20 61 e. Hence, the a
1a860 70 70 6c 69 63 61 74 69 6f 6e 20 73 68 6f 75 6c pplication shoul
1a870 64 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 d ensure that th
1a880 65 0a 2a 2a 20 63 6f 72 72 65 63 74 20 61 75 74 e.** correct aut
1a890 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b horizer callback
1a8a0 20 72 65 6d 61 69 6e 73 20 69 6e 20 70 6c 61 63 remains in plac
1a8b0 65 20 64 75 72 69 6e 67 20 74 68 65 20 5b 73 71 e during the [sq
1a8c0 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2e 0a 2a lite3_step()]..*
1a8d0 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 *.** Note that t
1a8e0 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 he authorizer ca
1a8f0 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 llback is invoke
1a900 64 20 6f 6e 6c 79 20 64 75 72 69 6e 67 0a 2a 2a d only during.**
1a910 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 [sqlite3_prepar
1a920 65 28 29 5d 20 6f 72 20 69 74 73 20 76 61 72 69 e()] or its vari
1a930 61 6e 74 73 2e 20 20 41 75 74 68 6f 72 69 7a 61 ants. Authoriza
1a940 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 2a 2a 20 70 tion is not.** p
1a950 65 72 66 6f 72 6d 65 64 20 64 75 72 69 6e 67 20 erformed during
1a960 73 74 61 74 65 6d 65 6e 74 20 65 76 61 6c 75 61 statement evalua
1a970 74 69 6f 6e 20 69 6e 20 5b 73 71 6c 69 74 65 33 tion in [sqlite3
1a980 5f 73 74 65 70 28 29 5d 2c 20 75 6e 6c 65 73 73 _step()], unless
1a990 0a 2a 2a 20 61 73 20 73 74 61 74 65 64 20 69 6e .** as stated in
1a9a0 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 70 61 the previous pa
1a9b0 72 61 67 72 61 70 68 2c 20 73 71 6c 69 74 65 33 ragraph, sqlite3
1a9c0 5f 73 74 65 70 28 29 20 69 6e 76 6f 6b 65 73 0a _step() invokes.
1a9d0 2a 2a 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 ** sqlite3_prepa
1a9e0 72 65 5f 76 32 28 29 20 74 6f 20 72 65 70 72 65 re_v2() to repre
1a9f0 70 61 72 65 20 61 20 73 74 61 74 65 6d 65 6e 74 pare a statement
1aa00 20 61 66 74 65 72 20 61 20 73 63 68 65 6d 61 20 after a schema
1aa10 63 68 61 6e 67 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 change..**.** Re
1aa20 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b quirements:.** [
1aa30 48 31 32 35 30 31 5d 20 5b 48 31 32 35 30 32 5d H12501] [H12502]
1aa40 20 5b 48 31 32 35 30 33 5d 20 5b 48 31 32 35 30 [H12503] [H1250
1aa50 34 5d 20 5b 48 31 32 35 30 35 5d 20 5b 48 31 32 4] [H12505] [H12
1aa60 35 30 36 5d 20 5b 48 31 32 35 30 37 5d 20 5b 48 506] [H12507] [H
1aa70 31 32 35 31 30 5d 0a 2a 2a 20 5b 48 31 32 35 31 12510].** [H1251
1aa80 31 5d 20 5b 48 31 32 35 31 32 5d 20 5b 48 31 32 1] [H12512] [H12
1aa90 35 32 30 5d 20 5b 48 31 32 35 32 31 5d 20 5b 48 520] [H12521] [H
1aaa0 31 32 35 32 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 12522].*/.SQLITE
1aab0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
1aac0 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 _set_authorizer(
1aad0 0a 20 20 73 71 6c 69 74 65 33 2a 2c 0a 20 20 69 . sqlite3*,. i
1aae0 6e 74 20 28 2a 78 41 75 74 68 29 28 76 6f 69 64 nt (*xAuth)(void
1aaf0 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 *,int,const char
1ab00 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f *,const char*,co
1ab10 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 nst char*,const
1ab20 63 68 61 72 2a 29 2c 0a 20 20 76 6f 69 64 20 2a char*),. void *
1ab30 70 55 73 65 72 44 61 74 61 0a 29 3b 0a 0a 2f 2a pUserData.);../*
1ab40 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 41 75 .** CAPI3REF: Au
1ab50 74 68 6f 72 69 7a 65 72 20 52 65 74 75 72 6e 20 thorizer Return
1ab60 43 6f 64 65 73 20 7b 48 31 32 35 39 30 7d 20 3c Codes {H12590} <
1ab70 48 31 32 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 H12500>.**.** Th
1ab80 65 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 e [sqlite3_set_a
1ab90 75 74 68 6f 72 69 7a 65 72 20 7c 20 61 75 74 68 uthorizer | auth
1aba0 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 orizer callback
1abb0 66 75 6e 63 74 69 6f 6e 5d 20 6d 75 73 74 0a 2a function] must.*
1abc0 2a 20 72 65 74 75 72 6e 20 65 69 74 68 65 72 20 * return either
1abd0 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 72 20 6f [SQLITE_OK] or o
1abe0 6e 65 20 6f 66 20 74 68 65 73 65 20 74 77 6f 20 ne of these two
1abf0 63 6f 6e 73 74 61 6e 74 73 20 69 6e 20 6f 72 64 constants in ord
1ac00 65 72 0a 2a 2a 20 74 6f 20 73 69 67 6e 61 6c 20 er.** to signal
1ac10 53 51 4c 69 74 65 20 77 68 65 74 68 65 72 20 6f SQLite whether o
1ac20 72 20 6e 6f 74 20 74 68 65 20 61 63 74 69 6f 6e r not the action
1ac30 20 69 73 20 70 65 72 6d 69 74 74 65 64 2e 20 20 is permitted.
1ac40 53 65 65 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 See the.** [sqli
1ac50 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a te3_set_authoriz
1ac60 65 72 20 7c 20 61 75 74 68 6f 72 69 7a 65 72 20 er | authorizer
1ac70 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 5d 20 66 documentation] f
1ac80 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a or additional.**
1ac90 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f information..*/
1aca0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
1acb0 44 45 4e 59 20 20 20 31 20 20 20 2f 2a 20 41 62 DENY 1 /* Ab
1acc0 6f 72 74 20 74 68 65 20 53 51 4c 20 73 74 61 74 ort the SQL stat
1acd0 65 6d 65 6e 74 20 77 69 74 68 20 61 6e 20 65 72 ement with an er
1ace0 72 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ror */.#define S
1acf0 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 32 20 20 QLITE_IGNORE 2
1ad00 20 2f 2a 20 44 6f 6e 27 74 20 61 6c 6c 6f 77 20 /* Don't allow
1ad10 61 63 63 65 73 73 2c 20 62 75 74 20 64 6f 6e 27 access, but don'
1ad20 74 20 67 65 6e 65 72 61 74 65 20 61 6e 20 65 72 t generate an er
1ad30 72 6f 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 ror */../*.** CA
1ad40 50 49 33 52 45 46 3a 20 41 75 74 68 6f 72 69 7a PI3REF: Authoriz
1ad50 65 72 20 41 63 74 69 6f 6e 20 43 6f 64 65 73 20 er Action Codes
1ad60 7b 48 31 32 35 35 30 7d 20 3c 48 31 32 35 30 30 {H12550} <H12500
1ad70 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c >.**.** The [sql
1ad80 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 ite3_set_authori
1ad90 7a 65 72 28 29 5d 20 69 6e 74 65 72 66 61 63 65 zer()] interface
1ada0 20 72 65 67 69 73 74 65 72 73 20 61 20 63 61 6c registers a cal
1adb0 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 0a 2a lback function.*
1adc0 2a 20 74 68 61 74 20 69 73 20 69 6e 76 6f 6b 65 * that is invoke
1add0 64 20 74 6f 20 61 75 74 68 6f 72 69 7a 65 20 63 d to authorize c
1ade0 65 72 74 61 69 6e 20 53 51 4c 20 73 74 61 74 65 ertain SQL state
1adf0 6d 65 6e 74 20 61 63 74 69 6f 6e 73 2e 20 20 54 ment actions. T
1ae00 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 70 61 72 he.** second par
1ae10 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 63 61 ameter to the ca
1ae20 6c 6c 62 61 63 6b 20 69 73 20 61 6e 20 69 6e 74 llback is an int
1ae30 65 67 65 72 20 63 6f 64 65 20 74 68 61 74 20 73 eger code that s
1ae40 70 65 63 69 66 69 65 73 0a 2a 2a 20 77 68 61 74 pecifies.** what
1ae50 20 61 63 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 action is being
1ae60 20 61 75 74 68 6f 72 69 7a 65 64 2e 20 20 54 68 authorized. Th
1ae70 65 73 65 20 61 72 65 20 74 68 65 20 69 6e 74 65 ese are the inte
1ae80 67 65 72 20 61 63 74 69 6f 6e 20 63 6f 64 65 73 ger action codes
1ae90 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 61 75 74 that.** the aut
1aea0 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b horizer callback
1aeb0 20 6d 61 79 20 62 65 20 70 61 73 73 65 64 2e 0a may be passed..
1aec0 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 61 63 74 69 **.** These acti
1aed0 6f 6e 20 63 6f 64 65 20 76 61 6c 75 65 73 20 73 on code values s
1aee0 69 67 6e 69 66 79 20 77 68 61 74 20 6b 69 6e 64 ignify what kind
1aef0 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 of operation is
1af00 20 74 6f 20 62 65 0a 2a 2a 20 61 75 74 68 6f 72 to be.** author
1af10 69 7a 65 64 2e 20 20 54 68 65 20 33 72 64 20 61 ized. The 3rd a
1af20 6e 64 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 nd 4th parameter
1af30 73 20 74 6f 20 74 68 65 20 61 75 74 68 6f 72 69 s to the authori
1af40 7a 61 74 69 6f 6e 0a 2a 2a 20 63 61 6c 6c 62 61 zation.** callba
1af50 63 6b 20 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c ck function will
1af60 20 62 65 20 70 61 72 61 6d 65 74 65 72 73 20 6f be parameters o
1af70 72 20 4e 55 4c 4c 20 64 65 70 65 6e 64 69 6e 67 r NULL depending
1af80 20 6f 6e 20 77 68 69 63 68 20 6f 66 20 74 68 65 on which of the
1af90 73 65 0a 2a 2a 20 63 6f 64 65 73 20 69 73 20 75 se.** codes is u
1afa0 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e sed as the secon
1afb0 64 20 70 61 72 61 6d 65 74 65 72 2e 20 20 54 68 d parameter. Th
1afc0 65 20 35 74 68 20 70 61 72 61 6d 65 74 65 72 20 e 5th parameter
1afd0 74 6f 20 74 68 65 0a 2a 2a 20 61 75 74 68 6f 72 to the.** author
1afe0 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 izer callback is
1aff0 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 the name of the
1b000 20 64 61 74 61 62 61 73 65 20 28 22 6d 61 69 6e database ("main
1b010 22 2c 20 22 74 65 6d 70 22 2c 0a 2a 2a 20 65 74 ", "temp",.** et
1b020 63 2e 29 20 69 66 20 61 70 70 6c 69 63 61 62 6c c.) if applicabl
1b030 65 2e 20 20 54 68 65 20 36 74 68 20 70 61 72 61 e. The 6th para
1b040 6d 65 74 65 72 20 74 6f 20 74 68 65 20 61 75 74 meter to the aut
1b050 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b horizer callback
1b060 0a 2a 2a 20 69 73 20 74 68 65 20 6e 61 6d 65 20 .** is the name
1b070 6f 66 20 74 68 65 20 69 6e 6e 65 72 2d 6d 6f 73 of the inner-mos
1b080 74 20 74 72 69 67 67 65 72 20 6f 72 20 76 69 65 t trigger or vie
1b090 77 20 74 68 61 74 20 69 73 20 72 65 73 70 6f 6e w that is respon
1b0a0 73 69 62 6c 65 20 66 6f 72 0a 2a 2a 20 74 68 65 sible for.** the
1b0b0 20 61 63 63 65 73 73 20 61 74 74 65 6d 70 74 20 access attempt
1b0c0 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 69 73 20 or NULL if this
1b0d0 61 63 63 65 73 73 20 61 74 74 65 6d 70 74 20 69 access attempt i
1b0e0 73 20 64 69 72 65 63 74 6c 79 20 66 72 6f 6d 0a s directly from.
1b0f0 2a 2a 20 74 6f 70 2d 6c 65 76 65 6c 20 53 51 4c ** top-level SQL
1b100 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 code..**.** Req
1b110 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
1b120 31 32 35 35 31 5d 20 5b 48 31 32 35 35 32 5d 20 12551] [H12552]
1b130 5b 48 31 32 35 35 33 5d 20 5b 48 31 32 35 35 34 [H12553] [H12554
1b140 5d 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ].*/./**********
1b150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b170 2a 20 33 72 64 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a * 3rd **********
1b180 2a 2a 20 34 74 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a ** 4th *********
1b190 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 **/.#define SQLI
1b1a0 54 45 5f 43 52 45 41 54 45 5f 49 4e 44 45 58 20 TE_CREATE_INDEX
1b1b0 20 20 20 20 20 20 20 20 20 31 20 20 20 2f 2a 20 1 /*
1b1c0 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 20 20 Index Name
1b1d0 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 Table Name
1b1e0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
1b1f0 45 5f 43 52 45 41 54 45 5f 54 41 42 4c 45 20 20 E_CREATE_TABLE
1b200 20 20 20 20 20 20 20 20 32 20 20 20 2f 2a 20 54 2 /* T
1b210 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e able Name N
1b220 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a ULL *
1b230 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
1b240 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 49 4e 44 _CREATE_TEMP_IND
1b250 45 58 20 20 20 20 20 33 20 20 20 2f 2a 20 49 6e EX 3 /* In
1b260 64 65 78 20 4e 61 6d 65 20 20 20 20 20 20 54 61 dex Name Ta
1b270 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f ble Name */
1b280 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
1b290 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 41 42 4c CREATE_TEMP_TABL
1b2a0 45 20 20 20 20 20 34 20 20 20 2f 2a 20 54 61 62 E 4 /* Tab
1b2b0 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c le Name NUL
1b2c0 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a L */.
1b2d0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 #define SQLITE_C
1b2e0 52 45 41 54 45 5f 54 45 4d 50 5f 54 52 49 47 47 REATE_TEMP_TRIGG
1b2f0 45 52 20 20 20 35 20 20 20 2f 2a 20 54 72 69 67 ER 5 /* Trig
1b300 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61 62 6c ger Name Tabl
1b310 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 e Name */.#
1b320 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 define SQLITE_CR
1b330 45 41 54 45 5f 54 45 4d 50 5f 56 49 45 57 20 20 EATE_TEMP_VIEW
1b340 20 20 20 20 36 20 20 20 2f 2a 20 56 69 65 77 20 6 /* View
1b350 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c 20 Name NULL
1b360 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 */.#d
1b370 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 efine SQLITE_CRE
1b380 41 54 45 5f 54 52 49 47 47 45 52 20 20 20 20 20 ATE_TRIGGER
1b390 20 20 20 37 20 20 20 2f 2a 20 54 72 69 67 67 65 7 /* Trigge
1b3a0 72 20 4e 61 6d 65 20 20 20 20 54 61 62 6c 65 20 r Name Table
1b3b0 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 Name */.#de
1b3c0 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 fine SQLITE_CREA
1b3d0 54 45 5f 56 49 45 57 20 20 20 20 20 20 20 20 20 TE_VIEW
1b3e0 20 20 38 20 20 20 2f 2a 20 56 69 65 77 20 4e 61 8 /* View Na
1b3f0 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 me NULL
1b400 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 */.#def
1b410 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 ine SQLITE_DELET
1b420 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 E
1b430 20 39 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 9 /* Table Na
1b440 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 me NULL
1b450 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 */.#defi
1b460 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 49 ne SQLITE_DROP_I
1b470 4e 44 45 58 20 20 20 20 20 20 20 20 20 20 20 31 NDEX 1
1b480 30 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 6d 0 /* Index Nam
1b490 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e 61 6d e Table Nam
1b4a0 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e e */.#defin
1b4b0 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 41 e SQLITE_DROP_TA
1b4c0 42 4c 45 20 20 20 20 20 20 20 20 20 20 20 31 31 BLE 11
1b4d0 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 /* Table Name
1b4e0 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 NULL
1b4f0 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 */.#define
1b500 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d SQLITE_DROP_TEM
1b510 50 5f 49 4e 44 45 58 20 20 20 20 20 20 31 32 20 P_INDEX 12
1b520 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 6d 65 20 /* Index Name
1b530 20 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 Table Name
1b540 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
1b550 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 SQLITE_DROP_TEMP
1b560 5f 54 41 42 4c 45 20 20 20 20 20 20 31 33 20 20 _TABLE 13
1b570 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 /* Table Name
1b580 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 NULL
1b590 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 */.#define S
1b5a0 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f QLITE_DROP_TEMP_
1b5b0 54 52 49 47 47 45 52 20 20 20 20 31 34 20 20 20 TRIGGER 14
1b5c0 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d 65 20 /* Trigger Name
1b5d0 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 Table Name
1b5e0 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 */.#define SQ
1b5f0 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 56 LITE_DROP_TEMP_V
1b600 49 45 57 20 20 20 20 20 20 20 31 35 20 20 20 2f IEW 15 /
1b610 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20 20 20 * View Name
1b620 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 NULL
1b630 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c */.#define SQL
1b640 49 54 45 5f 44 52 4f 50 5f 54 52 49 47 47 45 52 ITE_DROP_TRIGGER
1b650 20 20 20 20 20 20 20 20 20 31 36 20 20 20 2f 2a 16 /*
1b660 20 54 72 69 67 67 65 72 20 4e 61 6d 65 20 20 20 Trigger Name
1b670 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 Table Name
1b680 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
1b690 54 45 5f 44 52 4f 50 5f 56 49 45 57 20 20 20 20 TE_DROP_VIEW
1b6a0 20 20 20 20 20 20 20 20 31 37 20 20 20 2f 2a 20 17 /*
1b6b0 56 69 65 77 20 4e 61 6d 65 20 20 20 20 20 20 20 View Name
1b6c0 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 NULL
1b6d0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
1b6e0 45 5f 49 4e 53 45 52 54 20 20 20 20 20 20 20 20 E_INSERT
1b6f0 20 20 20 20 20 20 20 31 38 20 20 20 2f 2a 20 54 18 /* T
1b700 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e able Name N
1b710 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a ULL *
1b720 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
1b730 5f 50 52 41 47 4d 41 20 20 20 20 20 20 20 20 20 _PRAGMA
1b740 20 20 20 20 20 20 31 39 20 20 20 2f 2a 20 50 72 19 /* Pr
1b750 61 67 6d 61 20 4e 61 6d 65 20 20 20 20 20 31 73 agma Name 1s
1b760 74 20 61 72 67 20 6f 72 20 4e 55 4c 4c 20 2a 2f t arg or NULL */
1b770 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
1b780 52 45 41 44 20 20 20 20 20 20 20 20 20 20 20 20 READ
1b790 20 20 20 20 20 32 30 20 20 20 2f 2a 20 54 61 62 20 /* Tab
1b7a0 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 43 6f 6c le Name Col
1b7b0 75 6d 6e 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a umn Name */.
1b7c0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 #define SQLITE_S
1b7d0 45 4c 45 43 54 20 20 20 20 20 20 20 20 20 20 20 ELECT
1b7e0 20 20 20 20 32 31 20 20 20 2f 2a 20 4e 55 4c 4c 21 /* NULL
1b7f0 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c NULL
1b800 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 */.#
1b810 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 52 define SQLITE_TR
1b820 41 4e 53 41 43 54 49 4f 4e 20 20 20 20 20 20 20 ANSACTION
1b830 20 20 20 32 32 20 20 20 2f 2a 20 4f 70 65 72 61 22 /* Opera
1b840 74 69 6f 6e 20 20 20 20 20 20 20 4e 55 4c 4c 20 tion NULL
1b850 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 */.#d
1b860 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 50 44 efine SQLITE_UPD
1b870 41 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20 ATE
1b880 20 20 32 33 20 20 20 2f 2a 20 54 61 62 6c 65 20 23 /* Table
1b890 4e 61 6d 65 20 20 20 20 20 20 43 6f 6c 75 6d 6e Name Column
1b8a0 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a 23 64 65 Name */.#de
1b8b0 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 54 54 41 fine SQLITE_ATTA
1b8c0 43 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 CH
1b8d0 20 32 34 20 20 20 2f 2a 20 46 69 6c 65 6e 61 6d 24 /* Filenam
1b8e0 65 20 20 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 e NULL
1b8f0 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 */.#def
1b900 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 54 41 43 ine SQLITE_DETAC
1b910 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 H
1b920 32 35 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 25 /* Database
1b930 20 4e 61 6d 65 20 20 20 4e 55 4c 4c 20 20 20 20 Name NULL
1b940 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 */.#defi
1b950 6e 65 20 53 51 4c 49 54 45 5f 41 4c 54 45 52 5f ne SQLITE_ALTER_
1b960 54 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 32 TABLE 2
1b970 36 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6 /* Database
1b980 4e 61 6d 65 20 20 20 54 61 62 6c 65 20 4e 61 6d Name Table Nam
1b990 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e e */.#defin
1b9a0 65 20 53 51 4c 49 54 45 5f 52 45 49 4e 44 45 58 e SQLITE_REINDEX
1b9b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 37 27
1b9c0 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 6d 65 /* Index Name
1b9d0 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 NULL
1b9e0 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 */.#define
1b9f0 20 53 51 4c 49 54 45 5f 41 4e 41 4c 59 5a 45 20 SQLITE_ANALYZE
1ba00 20 20 20 20 20 20 20 20 20 20 20 20 20 32 38 20 28
1ba10 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 /* Table Name
1ba20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 NULL
1ba30 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
1ba40 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 56 54 SQLITE_CREATE_VT
1ba50 41 42 4c 45 20 20 20 20 20 20 20 20 32 39 20 20 ABLE 29
1ba60 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 /* Table Name
1ba70 20 20 20 20 4d 6f 64 75 6c 65 20 4e 61 6d 65 20 Module Name
1ba80 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 */.#define S
1ba90 51 4c 49 54 45 5f 44 52 4f 50 5f 56 54 41 42 4c QLITE_DROP_VTABL
1baa0 45 20 20 20 20 20 20 20 20 20 20 33 30 20 20 20 E 30
1bab0 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 /* Table Name
1bac0 20 20 20 4d 6f 64 75 6c 65 20 4e 61 6d 65 20 20 Module Name
1bad0 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 */.#define SQ
1bae0 4c 49 54 45 5f 46 55 4e 43 54 49 4f 4e 20 20 20 LITE_FUNCTION
1baf0 20 20 20 20 20 20 20 20 20 20 33 31 20 20 20 2f 31 /
1bb00 2a 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 * NULL
1bb10 20 20 46 75 6e 63 74 69 6f 6e 20 4e 61 6d 65 20 Function Name
1bb20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c */.#define SQL
1bb30 49 54 45 5f 53 41 56 45 50 4f 49 4e 54 20 20 20 ITE_SAVEPOINT
1bb40 20 20 20 20 20 20 20 20 20 33 32 20 20 20 2f 2a 32 /*
1bb50 20 4f 70 65 72 61 74 69 6f 6e 20 20 20 20 20 20 Operation
1bb60 20 53 61 76 65 70 6f 69 6e 74 20 4e 61 6d 65 20 Savepoint Name
1bb70 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
1bb80 54 45 5f 43 4f 50 59 20 20 20 20 20 20 20 20 20 TE_COPY
1bb90 20 20 20 20 20 20 20 20 20 30 20 20 20 2f 2a 20 0 /*
1bba0 4e 6f 20 6c 6f 6e 67 65 72 20 75 73 65 64 20 2a No longer used *
1bbb0 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 /../*.** CAPI3RE
1bbc0 46 3a 20 54 72 61 63 69 6e 67 20 41 6e 64 20 50 F: Tracing And P
1bbd0 72 6f 66 69 6c 69 6e 67 20 46 75 6e 63 74 69 6f rofiling Functio
1bbe0 6e 73 20 7b 48 31 32 32 38 30 7d 20 3c 53 36 30 ns {H12280} <S60
1bbf0 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 400>.** EXPERIME
1bc00 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 NTAL.**.** These
1bc10 20 72 6f 75 74 69 6e 65 73 20 72 65 67 69 73 74 routines regist
1bc20 65 72 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 er callback func
1bc30 74 69 6f 6e 73 20 74 68 61 74 20 63 61 6e 20 62 tions that can b
1bc40 65 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 74 72 e used for.** tr
1bc50 61 63 69 6e 67 20 61 6e 64 20 70 72 6f 66 69 6c acing and profil
1bc60 69 6e 67 20 74 68 65 20 65 78 65 63 75 74 69 6f ing the executio
1bc70 6e 20 6f 66 20 53 51 4c 20 73 74 61 74 65 6d 65 n of SQL stateme
1bc80 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 nts..**.** The c
1bc90 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e allback function
1bca0 20 72 65 67 69 73 74 65 72 65 64 20 62 79 20 73 registered by s
1bcb0 71 6c 69 74 65 33 5f 74 72 61 63 65 28 29 20 69 qlite3_trace() i
1bcc0 73 20 69 6e 76 6f 6b 65 64 20 61 74 0a 2a 2a 20 s invoked at.**
1bcd0 76 61 72 69 6f 75 73 20 74 69 6d 65 73 20 77 68 various times wh
1bce0 65 6e 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d en an SQL statem
1bcf0 65 6e 74 20 69 73 20 62 65 69 6e 67 20 72 75 6e ent is being run
1bd00 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 by [sqlite3_ste
1bd10 70 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 63 61 6c p()]..** The cal
1bd20 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 61 20 lback returns a
1bd30 55 54 46 2d 38 20 72 65 6e 64 65 72 69 6e 67 20 UTF-8 rendering
1bd40 6f 66 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 of the SQL state
1bd50 6d 65 6e 74 20 74 65 78 74 0a 2a 2a 20 61 73 20 ment text.** as
1bd60 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 66 69 the statement fi
1bd70 72 73 74 20 62 65 67 69 6e 73 20 65 78 65 63 75 rst begins execu
1bd80 74 69 6e 67 2e 20 20 41 64 64 69 74 69 6f 6e 61 ting. Additiona
1bd90 6c 20 63 61 6c 6c 62 61 63 6b 73 20 6f 63 63 75 l callbacks occu
1bda0 72 0a 2a 2a 20 61 73 20 65 61 63 68 20 74 72 69 r.** as each tri
1bdb0 67 67 65 72 65 64 20 73 75 62 70 72 6f 67 72 61 ggered subprogra
1bdc0 6d 20 69 73 20 65 6e 74 65 72 65 64 2e 20 20 54 m is entered. T
1bdd0 68 65 20 63 61 6c 6c 62 61 63 6b 73 20 66 6f 72 he callbacks for
1bde0 20 74 72 69 67 67 65 72 73 0a 2a 2a 20 63 6f 6e triggers.** con
1bdf0 74 61 69 6e 20 61 20 55 54 46 2d 38 20 53 51 4c tain a UTF-8 SQL
1be00 20 63 6f 6d 6d 65 6e 74 20 74 68 61 74 20 69 64 comment that id
1be10 65 6e 74 69 66 69 65 73 20 74 68 65 20 74 72 69 entifies the tri
1be20 67 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 gger..**.** The
1be30 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f callback functio
1be40 6e 20 72 65 67 69 73 74 65 72 65 64 20 62 79 20 n registered by
1be50 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65 28 sqlite3_profile(
1be60 29 20 69 73 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20 ) is invoked.**
1be70 61 73 20 65 61 63 68 20 53 51 4c 20 73 74 61 74 as each SQL stat
1be80 65 6d 65 6e 74 20 66 69 6e 69 73 68 65 73 2e 20 ement finishes.
1be90 20 54 68 65 20 70 72 6f 66 69 6c 65 20 63 61 6c The profile cal
1bea0 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e 73 0a 2a lback contains.*
1beb0 2a 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 73 * the original s
1bec0 74 61 74 65 6d 65 6e 74 20 74 65 78 74 20 61 6e tatement text an
1bed0 64 20 61 6e 20 65 73 74 69 6d 61 74 65 20 6f 66 d an estimate of
1bee0 20 77 61 6c 6c 2d 63 6c 6f 63 6b 20 74 69 6d 65 wall-clock time
1bef0 0a 2a 2a 20 6f 66 20 68 6f 77 20 6c 6f 6e 67 20 .** of how long
1bf00 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74 20 74 that statement t
1bf10 6f 6f 6b 20 74 6f 20 72 75 6e 2e 0a 2a 2a 0a 2a ook to run..**.*
1bf20 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
1bf30 2a 2a 20 5b 48 31 32 32 38 31 5d 20 5b 48 31 32 ** [H12281] [H12
1bf40 32 38 32 5d 20 5b 48 31 32 32 38 33 5d 20 5b 48 282] [H12283] [H
1bf50 31 32 32 38 34 5d 20 5b 48 31 32 32 38 35 5d 20 12284] [H12285]
1bf60 5b 48 31 32 32 38 37 5d 20 5b 48 31 32 32 38 38 [H12287] [H12288
1bf70 5d 20 5b 48 31 32 32 38 39 5d 0a 2a 2a 20 5b 48 ] [H12289].** [H
1bf80 31 32 32 39 30 5d 0a 2a 2f 0a 53 51 4c 49 54 45 12290].*/.SQLITE
1bf90 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 _API SQLITE_EXPE
1bfa0 52 49 4d 45 4e 54 41 4c 20 76 6f 69 64 20 2a 73 RIMENTAL void *s
1bfb0 71 6c 69 74 65 33 5f 74 72 61 63 65 28 73 71 6c qlite3_trace(sql
1bfc0 69 74 65 33 2a 2c 20 76 6f 69 64 28 2a 78 54 72 ite3*, void(*xTr
1bfd0 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 ace)(void*,const
1bfe0 20 63 68 61 72 2a 29 2c 20 76 6f 69 64 2a 29 3b char*), void*);
1bff0 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 .SQLITE_API SQLI
1c000 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 TE_EXPERIMENTAL
1c010 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 70 72 void *sqlite3_pr
1c020 6f 66 69 6c 65 28 73 71 6c 69 74 65 33 2a 2c 0a ofile(sqlite3*,.
1c030 20 20 20 76 6f 69 64 28 2a 78 50 72 6f 66 69 6c void(*xProfil
1c040 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 e)(void*,const c
1c050 68 61 72 2a 2c 73 71 6c 69 74 65 33 5f 75 69 6e har*,sqlite3_uin
1c060 74 36 34 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f t64), void*);../
1c070 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 51 *.** CAPI3REF: Q
1c080 75 65 72 79 20 50 72 6f 67 72 65 73 73 20 43 61 uery Progress Ca
1c090 6c 6c 62 61 63 6b 73 20 7b 48 31 32 39 31 30 7d llbacks {H12910}
1c0a0 20 3c 53 36 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S60400>.**.**
1c0b0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 6f 6e This routine con
1c0c0 66 69 67 75 72 65 73 20 61 20 63 61 6c 6c 62 61 figures a callba
1c0d0 63 6b 20 66 75 6e 63 74 69 6f 6e 20 2d 20 74 68 ck function - th
1c0e0 65 0a 2a 2a 20 70 72 6f 67 72 65 73 73 20 63 61 e.** progress ca
1c0f0 6c 6c 62 61 63 6b 20 2d 20 74 68 61 74 20 69 73 llback - that is
1c100 20 69 6e 76 6f 6b 65 64 20 70 65 72 69 6f 64 69 invoked periodi
1c110 63 61 6c 6c 79 20 64 75 72 69 6e 67 20 6c 6f 6e cally during lon
1c120 67 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 63 61 6c g.** running cal
1c130 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 ls to [sqlite3_e
1c140 78 65 63 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 xec()], [sqlite3
1c150 5f 73 74 65 70 28 29 5d 20 61 6e 64 0a 2a 2a 20 _step()] and.**
1c160 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 [sqlite3_get_tab
1c170 6c 65 28 29 5d 2e 20 20 41 6e 20 65 78 61 6d 70 le()]. An examp
1c180 6c 65 20 75 73 65 20 66 6f 72 20 74 68 69 73 0a le use for this.
1c190 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 ** interface is
1c1a0 74 6f 20 6b 65 65 70 20 61 20 47 55 49 20 75 70 to keep a GUI up
1c1b0 64 61 74 65 64 20 64 75 72 69 6e 67 20 61 20 6c dated during a l
1c1c0 61 72 67 65 20 71 75 65 72 79 2e 0a 2a 2a 0a 2a arge query..**.*
1c1d0 2a 20 49 66 20 74 68 65 20 70 72 6f 67 72 65 73 * If the progres
1c1e0 73 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 s callback retur
1c1f0 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 ns non-zero, the
1c200 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 0a 2a 2a operation is.**
1c210 20 69 6e 74 65 72 72 75 70 74 65 64 2e 20 20 54 interrupted. T
1c220 68 69 73 20 66 65 61 74 75 72 65 20 63 61 6e 20 his feature can
1c230 62 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 be used to imple
1c240 6d 65 6e 74 20 61 0a 2a 2a 20 22 43 61 6e 63 65 ment a.** "Cance
1c250 6c 22 20 62 75 74 74 6f 6e 20 6f 6e 20 61 20 47 l" button on a G
1c260 55 49 20 70 72 6f 67 72 65 73 73 20 64 69 61 6c UI progress dial
1c270 6f 67 20 62 6f 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 og box..**.** Th
1c280 65 20 70 72 6f 67 72 65 73 73 20 68 61 6e 64 6c e progress handl
1c290 65 72 20 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61 er must not do a
1c2a0 6e 79 74 68 69 6e 67 20 74 68 61 74 20 77 69 6c nything that wil
1c2b0 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20 l modify.** the
1c2c0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
1c2d0 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 ion that invoked
1c2e0 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 68 61 the progress ha
1c2f0 6e 64 6c 65 72 2e 0a 2a 2a 20 4e 6f 74 65 20 74 ndler..** Note t
1c300 68 61 74 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 hat [sqlite3_pre
1c310 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b pare_v2()] and [
1c320 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 sqlite3_step()]
1c330 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 68 65 69 both modify thei
1c340 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f r.** database co
1c350 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20 74 68 nnections for th
1c360 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22 6d 6f e meaning of "mo
1c370 64 69 66 79 22 20 69 6e 20 74 68 69 73 20 70 61 dify" in this pa
1c380 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 52 ragraph..**.** R
1c390 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 equirements:.**
1c3a0 5b 48 31 32 39 31 31 5d 20 5b 48 31 32 39 31 32 [H12911] [H12912
1c3b0 5d 20 5b 48 31 32 39 31 33 5d 20 5b 48 31 32 39 ] [H12913] [H129
1c3c0 31 34 5d 20 5b 48 31 32 39 31 35 5d 20 5b 48 31 14] [H12915] [H1
1c3d0 32 39 31 36 5d 20 5b 48 31 32 39 31 37 5d 20 5b 2916] [H12917] [
1c3e0 48 31 32 39 31 38 5d 0a 2a 2a 0a 2a 2f 0a 53 51 H12918].**.*/.SQ
1c3f0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
1c400 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73 5f 68 lite3_progress_h
1c410 61 6e 64 6c 65 72 28 73 71 6c 69 74 65 33 2a 2c andler(sqlite3*,
1c420 20 69 6e 74 2c 20 69 6e 74 28 2a 29 28 76 6f 69 int, int(*)(voi
1c430 64 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a d*), void*);../*
1c440 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 70 .** CAPI3REF: Op
1c450 65 6e 69 6e 67 20 41 20 4e 65 77 20 44 61 74 61 ening A New Data
1c460 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 base Connection
1c470 7b 48 31 32 37 30 30 7d 20 3c 53 34 30 32 30 30 {H12700} <S40200
1c480 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f >.**.** These ro
1c490 75 74 69 6e 65 73 20 6f 70 65 6e 20 61 6e 20 53 utines open an S
1c4a0 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 20 66 QLite database f
1c4b0 69 6c 65 20 77 68 6f 73 65 20 6e 61 6d 65 20 69 ile whose name i
1c4c0 73 20 67 69 76 65 6e 20 62 79 20 74 68 65 0a 2a s given by the.*
1c4d0 2a 20 66 69 6c 65 6e 61 6d 65 20 61 72 67 75 6d * filename argum
1c4e0 65 6e 74 2e 20 54 68 65 20 66 69 6c 65 6e 61 6d ent. The filenam
1c4f0 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 69 6e e argument is in
1c500 74 65 72 70 72 65 74 65 64 20 61 73 20 55 54 46 terpreted as UTF
1c510 2d 38 20 66 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 -8 for.** sqlite
1c520 33 5f 6f 70 65 6e 28 29 20 61 6e 64 20 73 71 6c 3_open() and sql
1c530 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 61 ite3_open_v2() a
1c540 6e 64 20 61 73 20 55 54 46 2d 31 36 20 69 6e 20 nd as UTF-16 in
1c550 74 68 65 20 6e 61 74 69 76 65 20 62 79 74 65 0a the native byte.
1c560 2a 2a 20 6f 72 64 65 72 20 66 6f 72 20 73 71 6c ** order for sql
1c570 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 2e 20 41 ite3_open16(). A
1c580 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
1c590 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 20 69 73 ction] handle is
1c5a0 20 75 73 75 61 6c 6c 79 0a 2a 2a 20 72 65 74 75 usually.** retu
1c5b0 72 6e 65 64 20 69 6e 20 2a 70 70 44 62 2c 20 65 rned in *ppDb, e
1c5c0 76 65 6e 20 69 66 20 61 6e 20 65 72 72 6f 72 20 ven if an error
1c5d0 6f 63 63 75 72 73 2e 20 20 54 68 65 20 6f 6e 6c occurs. The onl
1c5e0 79 20 65 78 63 65 70 74 69 6f 6e 20 69 73 20 74 y exception is t
1c5f0 68 61 74 0a 2a 2a 20 69 66 20 53 51 4c 69 74 65 hat.** if SQLite
1c600 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 61 6c is unable to al
1c610 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 74 6f locate memory to
1c620 20 68 6f 6c 64 20 74 68 65 20 5b 73 71 6c 69 74 hold the [sqlit
1c630 65 33 5d 20 6f 62 6a 65 63 74 2c 0a 2a 2a 20 61 e3] object,.** a
1c640 20 4e 55 4c 4c 20 77 69 6c 6c 20 62 65 20 77 72 NULL will be wr
1c650 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 70 44 62 itten into *ppDb
1c660 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20 70 6f instead of a po
1c670 69 6e 74 65 72 20 74 6f 20 74 68 65 20 5b 73 71 inter to the [sq
1c680 6c 69 74 65 33 5d 0a 2a 2a 20 6f 62 6a 65 63 74 lite3].** object
1c690 2e 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 . If the databas
1c6a0 65 20 69 73 20 6f 70 65 6e 65 64 20 28 61 6e 64 e is opened (and
1c6b0 2f 6f 72 20 63 72 65 61 74 65 64 29 20 73 75 63 /or created) suc
1c6c0 63 65 73 73 66 75 6c 6c 79 2c 20 74 68 65 6e 0a cessfully, then.
1c6d0 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 69 ** [SQLITE_OK] i
1c6e0 73 20 72 65 74 75 72 6e 65 64 2e 20 20 4f 74 68 s returned. Oth
1c6f0 65 72 77 69 73 65 20 61 6e 20 5b 65 72 72 6f 72 erwise an [error
1c700 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e code] is return
1c710 65 64 2e 20 20 54 68 65 0a 2a 2a 20 5b 73 71 6c ed. The.** [sql
1c720 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 20 6f ite3_errmsg()] o
1c730 72 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 r [sqlite3_errms
1c740 67 31 36 28 29 5d 20 72 6f 75 74 69 6e 65 73 20 g16()] routines
1c750 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 6f can be used to o
1c760 62 74 61 69 6e 0a 2a 2a 20 61 6e 20 45 6e 67 6c btain.** an Engl
1c770 69 73 68 20 6c 61 6e 67 75 61 67 65 20 64 65 73 ish language des
1c780 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 cription of the
1c790 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 error..**.** The
1c7a0 20 64 65 66 61 75 6c 74 20 65 6e 63 6f 64 69 6e default encodin
1c7b0 67 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 g for the databa
1c7c0 73 65 20 77 69 6c 6c 20 62 65 20 55 54 46 2d 38 se will be UTF-8
1c7d0 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f if.** sqlite3_o
1c7e0 70 65 6e 28 29 20 6f 72 20 73 71 6c 69 74 65 33 pen() or sqlite3
1c7f0 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 20 63 61 _open_v2() is ca
1c800 6c 6c 65 64 20 61 6e 64 0a 2a 2a 20 55 54 46 2d lled and.** UTF-
1c810 31 36 20 69 6e 20 74 68 65 20 6e 61 74 69 76 65 16 in the native
1c820 20 62 79 74 65 20 6f 72 64 65 72 20 69 66 20 73 byte order if s
1c830 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 20 qlite3_open16()
1c840 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 is used..**.** W
1c850 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 6e hether or not an
1c860 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 error occurs wh
1c870 65 6e 20 69 74 20 69 73 20 6f 70 65 6e 65 64 2c en it is opened,
1c880 20 72 65 73 6f 75 72 63 65 73 0a 2a 2a 20 61 73 resources.** as
1c890 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 sociated with th
1c8a0 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e e [database conn
1c8b0 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 20 73 ection] handle s
1c8c0 68 6f 75 6c 64 20 62 65 20 72 65 6c 65 61 73 65 hould be release
1c8d0 64 20 62 79 0a 2a 2a 20 70 61 73 73 69 6e 67 20 d by.** passing
1c8e0 69 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 it to [sqlite3_c
1c8f0 6c 6f 73 65 28 29 5d 20 77 68 65 6e 20 69 74 20 lose()] when it
1c900 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 72 65 71 is no longer req
1c910 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 uired..**.** The
1c920 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 sqlite3_open_v2
1c930 28 29 20 69 6e 74 65 72 66 61 63 65 20 77 6f 72 () interface wor
1c940 6b 73 20 6c 69 6b 65 20 73 71 6c 69 74 65 33 5f ks like sqlite3_
1c950 6f 70 65 6e 28 29 0a 2a 2a 20 65 78 63 65 70 74 open().** except
1c960 20 74 68 61 74 20 69 74 20 61 63 63 65 70 74 73 that it accepts
1c970 20 74 77 6f 20 61 64 64 69 74 69 6f 6e 61 6c 20 two additional
1c980 70 61 72 61 6d 65 74 65 72 73 20 66 6f 72 20 61 parameters for a
1c990 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6e 74 72 6f dditional contro
1c9a0 6c 0a 2a 2a 20 6f 76 65 72 20 74 68 65 20 6e 65 l.** over the ne
1c9b0 77 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 w database conne
1c9c0 63 74 69 6f 6e 2e 20 20 54 68 65 20 66 6c 61 67 ction. The flag
1c9d0 73 20 70 61 72 61 6d 65 74 65 72 20 63 61 6e 20 s parameter can
1c9e0 74 61 6b 65 20 6f 6e 65 20 6f 66 0a 2a 2a 20 74 take one of.** t
1c9f0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 72 he following thr
1ca00 65 65 20 76 61 6c 75 65 73 2c 20 6f 70 74 69 6f ee values, optio
1ca10 6e 61 6c 6c 79 20 63 6f 6d 62 69 6e 65 64 20 77 nally combined w
1ca20 69 74 68 20 74 68 65 20 0a 2a 2a 20 5b 53 51 4c ith the .** [SQL
1ca30 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58 ITE_OPEN_NOMUTEX
1ca40 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f ], [SQLITE_OPEN_
1ca50 46 55 4c 4c 4d 55 54 45 58 5d 2c 20 5b 53 51 4c FULLMUTEX], [SQL
1ca60 49 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 ITE_OPEN_SHAREDC
1ca70 41 43 48 45 5d 2c 0a 2a 2a 20 61 6e 64 2f 6f 72 ACHE],.** and/or
1ca80 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 50 52 [SQLITE_OPEN_PR
1ca90 49 56 41 54 45 43 41 43 48 45 5d 20 66 6c 61 67 IVATECACHE] flag
1caa0 73 3a 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a s:.**.** <dl>.**
1cab0 20 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 45 <dt>[SQLITE_OPE
1cac0 4e 5f 52 45 41 44 4f 4e 4c 59 5d 3c 2f 64 74 3e N_READONLY]</dt>
1cad0 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 64 61 74 61 .** <dd>The data
1cae0 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 69 base is opened i
1caf0 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 6f 64 65 n read-only mode
1cb00 2e 20 20 49 66 20 74 68 65 20 64 61 74 61 62 61 . If the databa
1cb10 73 65 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 61 se does not.** a
1cb20 6c 72 65 61 64 79 20 65 78 69 73 74 2c 20 61 6e lready exist, an
1cb30 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e error is return
1cb40 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c ed.</dd>.**.** <
1cb50 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f dt>[SQLITE_OPEN_
1cb60 52 45 41 44 57 52 49 54 45 5d 3c 2f 64 74 3e 0a READWRITE]</dt>.
1cb70 2a 2a 20 3c 64 64 3e 54 68 65 20 64 61 74 61 62 ** <dd>The datab
1cb80 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 66 6f ase is opened fo
1cb90 72 20 72 65 61 64 69 6e 67 20 61 6e 64 20 77 72 r reading and wr
1cba0 69 74 69 6e 67 20 69 66 20 70 6f 73 73 69 62 6c iting if possibl
1cbb0 65 2c 20 6f 72 20 72 65 61 64 69 6e 67 0a 2a 2a e, or reading.**
1cbc0 20 6f 6e 6c 79 20 69 66 20 74 68 65 20 66 69 6c only if the fil
1cbd0 65 20 69 73 20 77 72 69 74 65 20 70 72 6f 74 65 e is write prote
1cbe0 63 74 65 64 20 62 79 20 74 68 65 20 6f 70 65 72 cted by the oper
1cbf0 61 74 69 6e 67 20 73 79 73 74 65 6d 2e 20 20 49 ating system. I
1cc00 6e 20 65 69 74 68 65 72 0a 2a 2a 20 63 61 73 65 n either.** case
1cc10 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6d 75 the database mu
1cc20 73 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 st already exist
1cc30 2c 20 6f 74 68 65 72 77 69 73 65 20 61 6e 20 65 , otherwise an e
1cc40 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 rror is returned
1cc50 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 .</dd>.**.** <dt
1cc60 3e 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 >[SQLITE_OPEN_RE
1cc70 41 44 57 52 49 54 45 5d 20 7c 20 5b 53 51 4c 49 ADWRITE] | [SQLI
1cc80 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 5d 3c TE_OPEN_CREATE]<
1cc90 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 /dt>.** <dd>The
1cca0 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e database is open
1ccb0 65 64 20 66 6f 72 20 72 65 61 64 69 6e 67 20 61 ed for reading a
1ccc0 6e 64 20 77 72 69 74 69 6e 67 2c 20 61 6e 64 20 nd writing, and
1ccd0 69 73 20 63 72 65 61 74 65 73 20 69 74 20 69 66 is creates it if
1cce0 0a 2a 2a 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 .** it does not
1ccf0 61 6c 72 65 61 64 79 20 65 78 69 73 74 2e 20 54 already exist. T
1cd00 68 69 73 20 69 73 20 74 68 65 20 62 65 68 61 76 his is the behav
1cd10 69 6f 72 20 74 68 61 74 20 69 73 20 61 6c 77 61 ior that is alwa
1cd20 79 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 73 ys used for.** s
1cd30 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 61 6e qlite3_open() an
1cd40 64 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 d sqlite3_open16
1cd50 28 29 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c ().</dd>.** </dl
1cd60 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 33 >.**.** If the 3
1cd70 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 rd parameter to
1cd80 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 sqlite3_open_v2(
1cd90 29 20 69 73 20 6e 6f 74 20 6f 6e 65 20 6f 66 20 ) is not one of
1cda0 74 68 65 0a 2a 2a 20 63 6f 6d 62 69 6e 61 74 69 the.** combinati
1cdb0 6f 6e 73 20 73 68 6f 77 6e 20 61 62 6f 76 65 20 ons shown above
1cdc0 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 63 6f or one of the co
1cdd0 6d 62 69 6e 61 74 69 6f 6e 73 20 73 68 6f 77 6e mbinations shown
1cde0 20 61 62 6f 76 65 20 63 6f 6d 62 69 6e 65 64 0a above combined.
1cdf0 2a 2a 20 77 69 74 68 20 74 68 65 20 5b 53 51 4c ** with the [SQL
1ce00 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58 ITE_OPEN_NOMUTEX
1ce10 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f ], [SQLITE_OPEN_
1ce20 46 55 4c 4c 4d 55 54 45 58 5d 2c 0a 2a 2a 20 5b FULLMUTEX],.** [
1ce30 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41 52 SQLITE_OPEN_SHAR
1ce40 45 44 43 41 43 48 45 5d 20 61 6e 64 2f 6f 72 20 EDCACHE] and/or
1ce50 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41 [SQLITE_OPEN_SHA
1ce60 52 45 44 43 41 43 48 45 5d 20 66 6c 61 67 73 2c REDCACHE] flags,
1ce70 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 62 65 68 .** then the beh
1ce80 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e avior is undefin
1ce90 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ed..**.** If the
1cea0 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f [SQLITE_OPEN_NO
1ceb0 4d 55 54 45 58 5d 20 66 6c 61 67 20 69 73 20 73 MUTEX] flag is s
1cec0 65 74 2c 20 74 68 65 6e 20 74 68 65 20 64 61 74 et, then the dat
1ced0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
1cee0 0a 2a 2a 20 6f 70 65 6e 73 20 69 6e 20 74 68 65 .** opens in the
1cef0 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 20 5b 74 multi-thread [t
1cf00 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 61 hreading mode] a
1cf10 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 73 69 s long as the si
1cf20 6e 67 6c 65 2d 74 68 72 65 61 64 0a 2a 2a 20 6d ngle-thread.** m
1cf30 6f 64 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e ode has not been
1cf40 20 73 65 74 20 61 74 20 63 6f 6d 70 69 6c 65 2d set at compile-
1cf50 74 69 6d 65 20 6f 72 20 73 74 61 72 74 2d 74 69 time or start-ti
1cf60 6d 65 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 5b me. If the.** [
1cf70 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c SQLITE_OPEN_FULL
1cf80 4d 55 54 45 58 5d 20 66 6c 61 67 20 69 73 20 73 MUTEX] flag is s
1cf90 65 74 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 et then the data
1cfa0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
1cfb0 6f 70 65 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 opens.** in the
1cfc0 73 65 72 69 61 6c 69 7a 65 64 20 5b 74 68 72 65 serialized [thre
1cfd0 61 64 69 6e 67 20 6d 6f 64 65 5d 20 75 6e 6c 65 ading mode] unle
1cfe0 73 73 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 ss single-thread
1cff0 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 was.** previous
1d000 6c 79 20 73 65 6c 65 63 74 65 64 20 61 74 20 63 ly selected at c
1d010 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 72 20 73 ompile-time or s
1d020 74 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a 20 54 68 tart-time..** Th
1d030 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 e [SQLITE_OPEN_S
1d040 48 41 52 45 44 43 41 43 48 45 5d 20 66 6c 61 67 HAREDCACHE] flag
1d050 20 63 61 75 73 65 73 20 74 68 65 20 64 61 74 61 causes the data
1d060 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
1d070 74 6f 20 62 65 0a 2a 2a 20 65 6c 69 67 69 62 6c to be.** eligibl
1d080 65 20 74 6f 20 75 73 65 20 5b 73 68 61 72 65 64 e to use [shared
1d090 20 63 61 63 68 65 20 6d 6f 64 65 5d 2c 20 72 65 cache mode], re
1d0a0 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 gardless of whet
1d0b0 68 65 72 20 6f 72 20 6e 6f 74 20 73 68 61 72 65 her or not share
1d0c0 64 0a 2a 2a 20 63 61 63 68 65 20 69 73 20 65 6e d.** cache is en
1d0d0 61 62 6c 65 64 20 75 73 69 6e 67 20 5b 73 71 6c abled using [sql
1d0e0 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72 ite3_enable_shar
1d0f0 65 64 5f 63 61 63 68 65 28 29 5d 2e 20 20 54 68 ed_cache()]. Th
1d100 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 50 45 e.** [SQLITE_OPE
1d110 4e 5f 50 52 49 56 41 54 45 43 41 43 48 45 5d 20 N_PRIVATECACHE]
1d120 66 6c 61 67 20 63 61 75 73 65 73 20 74 68 65 20 flag causes the
1d130 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
1d140 69 6f 6e 20 74 6f 20 6e 6f 74 0a 2a 2a 20 70 61 ion to not.** pa
1d150 72 74 69 63 69 70 61 74 65 20 69 6e 20 5b 73 68 rticipate in [sh
1d160 61 72 65 64 20 63 61 63 68 65 20 6d 6f 64 65 5d ared cache mode]
1d170 20 65 76 65 6e 20 69 66 20 69 74 20 69 73 20 65 even if it is e
1d180 6e 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 nabled..**.** If
1d190 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 69 73 the filename is
1d1a0 20 22 3a 6d 65 6d 6f 72 79 3a 22 2c 20 74 68 65 ":memory:", the
1d1b0 6e 20 61 20 70 72 69 76 61 74 65 2c 20 74 65 6d n a private, tem
1d1c0 70 6f 72 61 72 79 20 69 6e 2d 6d 65 6d 6f 72 79 porary in-memory
1d1d0 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 69 73 20 database.** is
1d1e0 63 72 65 61 74 65 64 20 66 6f 72 20 74 68 65 20 created for the
1d1f0 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 69 connection. Thi
1d200 73 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 s in-memory data
1d210 62 61 73 65 20 77 69 6c 6c 20 76 61 6e 69 73 68 base will vanish
1d220 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 64 61 74 when.** the dat
1d230 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
1d240 20 69 73 20 63 6c 6f 73 65 64 2e 20 20 46 75 74 is closed. Fut
1d250 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 ure versions of
1d260 53 51 4c 69 74 65 20 6d 69 67 68 74 0a 2a 2a 20 SQLite might.**
1d270 6d 61 6b 65 20 75 73 65 20 6f 66 20 61 64 64 69 make use of addi
1d280 74 69 6f 6e 61 6c 20 73 70 65 63 69 61 6c 20 66 tional special f
1d290 69 6c 65 6e 61 6d 65 73 20 74 68 61 74 20 62 65 ilenames that be
1d2a0 67 69 6e 20 77 69 74 68 20 74 68 65 20 22 3a 22 gin with the ":"
1d2b0 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 49 character..** I
1d2c0 74 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64 t is recommended
1d2d0 20 74 68 61 74 20 77 68 65 6e 20 61 20 64 61 74 that when a dat
1d2e0 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 61 abase filename a
1d2f0 63 74 75 61 6c 6c 79 20 64 6f 65 73 20 62 65 67 ctually does beg
1d300 69 6e 20 77 69 74 68 0a 2a 2a 20 61 20 22 3a 22 in with.** a ":"
1d310 20 63 68 61 72 61 63 74 65 72 20 79 6f 75 20 73 character you s
1d320 68 6f 75 6c 64 20 70 72 65 66 69 78 20 74 68 65 hould prefix the
1d330 20 66 69 6c 65 6e 61 6d 65 20 77 69 74 68 20 61 filename with a
1d340 20 70 61 74 68 6e 61 6d 65 20 73 75 63 68 20 61 pathname such a
1d350 73 0a 2a 2a 20 22 2e 2f 22 20 74 6f 20 61 76 6f s.** "./" to avo
1d360 69 64 20 61 6d 62 69 67 75 69 74 79 2e 0a 2a 2a id ambiguity..**
1d370 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 6c 65 6e .** If the filen
1d380 61 6d 65 20 69 73 20 61 6e 20 65 6d 70 74 79 20 ame is an empty
1d390 73 74 72 69 6e 67 2c 20 74 68 65 6e 20 61 20 70 string, then a p
1d3a0 72 69 76 61 74 65 2c 20 74 65 6d 70 6f 72 61 72 rivate, temporar
1d3b0 79 0a 2a 2a 20 6f 6e 2d 64 69 73 6b 20 64 61 74 y.** on-disk dat
1d3c0 61 62 61 73 65 20 77 69 6c 6c 20 62 65 20 63 72 abase will be cr
1d3d0 65 61 74 65 64 2e 20 20 54 68 69 73 20 70 72 69 eated. This pri
1d3e0 76 61 74 65 20 64 61 74 61 62 61 73 65 20 77 69 vate database wi
1d3f0 6c 6c 20 62 65 0a 2a 2a 20 61 75 74 6f 6d 61 74 ll be.** automat
1d400 69 63 61 6c 6c 79 20 64 65 6c 65 74 65 64 20 61 ically deleted a
1d410 73 20 73 6f 6f 6e 20 61 73 20 74 68 65 20 64 61 s soon as the da
1d420 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
1d430 6e 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a n is closed..**.
1d440 2a 2a 20 54 68 65 20 66 6f 75 72 74 68 20 70 61 ** The fourth pa
1d450 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 rameter to sqlit
1d460 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 20 e3_open_v2() is
1d470 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a the name of the.
1d480 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d ** [sqlite3_vfs]
1d490 20 6f 62 6a 65 63 74 20 74 68 61 74 20 64 65 66 object that def
1d4a0 69 6e 65 73 20 74 68 65 20 6f 70 65 72 61 74 69 ines the operati
1d4b0 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66 ng system interf
1d4c0 61 63 65 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 ace that.** the
1d4d0 6e 65 77 20 64 61 74 61 62 61 73 65 20 63 6f 6e new database con
1d4e0 6e 65 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 75 nection should u
1d4f0 73 65 2e 20 20 49 66 20 74 68 65 20 66 6f 75 72 se. If the four
1d500 74 68 20 70 61 72 61 6d 65 74 65 72 20 69 73 0a th parameter is.
1d510 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 ** a NULL pointe
1d520 72 20 74 68 65 6e 20 74 68 65 20 64 65 66 61 75 r then the defau
1d530 6c 74 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d lt [sqlite3_vfs]
1d540 20 6f 62 6a 65 63 74 20 69 73 20 75 73 65 64 2e object is used.
1d550 0a 2a 2a 0a 2a 2a 20 3c 62 3e 4e 6f 74 65 20 74 .**.** <b>Note t
1d560 6f 20 57 69 6e 64 6f 77 73 20 75 73 65 72 73 3a o Windows users:
1d570 3c 2f 62 3e 20 20 54 68 65 20 65 6e 63 6f 64 69 </b> The encodi
1d580 6e 67 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 ng used for the
1d590 66 69 6c 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e filename argumen
1d5a0 74 0a 2a 2a 20 6f 66 20 73 71 6c 69 74 65 33 5f t.** of sqlite3_
1d5b0 6f 70 65 6e 28 29 20 61 6e 64 20 73 71 6c 69 74 open() and sqlit
1d5c0 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 6d 75 73 e3_open_v2() mus
1d5d0 74 20 62 65 20 55 54 46 2d 38 2c 20 6e 6f 74 20 t be UTF-8, not
1d5e0 77 68 61 74 65 76 65 72 0a 2a 2a 20 63 6f 64 65 whatever.** code
1d5f0 70 61 67 65 20 69 73 20 63 75 72 72 65 6e 74 6c page is currentl
1d600 79 20 64 65 66 69 6e 65 64 2e 20 20 46 69 6c 65 y defined. File
1d610 6e 61 6d 65 73 20 63 6f 6e 74 61 69 6e 69 6e 67 names containing
1d620 20 69 6e 74 65 72 6e 61 74 69 6f 6e 61 6c 0a 2a international.*
1d630 2a 20 63 68 61 72 61 63 74 65 72 73 20 6d 75 73 * characters mus
1d640 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 74 t be converted t
1d650 6f 20 55 54 46 2d 38 20 70 72 69 6f 72 20 74 6f o UTF-8 prior to
1d660 20 70 61 73 73 69 6e 67 20 74 68 65 6d 20 69 6e passing them in
1d670 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 70 to.** sqlite3_op
1d680 65 6e 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f en() or sqlite3_
1d690 6f 70 65 6e 5f 76 32 28 29 2e 0a 2a 2a 0a 2a 2a open_v2()..**.**
1d6a0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a Requirements:.*
1d6b0 2a 20 5b 48 31 32 37 30 31 5d 20 5b 48 31 32 37 * [H12701] [H127
1d6c0 30 32 5d 20 5b 48 31 32 37 30 33 5d 20 5b 48 31 02] [H12703] [H1
1d6d0 32 37 30 34 5d 20 5b 48 31 32 37 30 36 5d 20 5b 2704] [H12706] [
1d6e0 48 31 32 37 30 37 5d 20 5b 48 31 32 37 30 39 5d H12707] [H12709]
1d6f0 20 5b 48 31 32 37 31 31 5d 0a 2a 2a 20 5b 48 31 [H12711].** [H1
1d700 32 37 31 32 5d 20 5b 48 31 32 37 31 33 5d 20 5b 2712] [H12713] [
1d710 48 31 32 37 31 34 5d 20 5b 48 31 32 37 31 37 5d H12714] [H12717]
1d720 20 5b 48 31 32 37 31 39 5d 20 5b 48 31 32 37 32 [H12719] [H1272
1d730 31 5d 20 5b 48 31 32 37 32 33 5d 0a 2a 2f 0a 53 1] [H12723].*/.S
1d740 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1d750 6c 69 74 65 33 5f 6f 70 65 6e 28 0a 20 20 63 6f lite3_open(. co
1d760 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 6e 61 nst char *filena
1d770 6d 65 2c 20 20 20 2f 2a 20 44 61 74 61 62 61 73 me, /* Databas
1d780 65 20 66 69 6c 65 6e 61 6d 65 20 28 55 54 46 2d e filename (UTF-
1d790 38 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 8) */. sqlite3
1d7a0 2a 2a 70 70 44 62 20 20 20 20 20 20 20 20 20 20 **ppDb
1d7b0 2f 2a 20 4f 55 54 3a 20 53 51 4c 69 74 65 20 64 /* OUT: SQLite d
1d7c0 62 20 68 61 6e 64 6c 65 20 2a 2f 0a 29 3b 0a 53 b handle */.);.S
1d7d0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1d7e0 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 0a 20 20 lite3_open16(.
1d7f0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 66 69 6c 65 const void *file
1d800 6e 61 6d 65 2c 20 20 20 2f 2a 20 44 61 74 61 62 name, /* Datab
1d810 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 28 55 54 ase filename (UT
1d820 46 2d 31 36 29 20 2a 2f 0a 20 20 73 71 6c 69 74 F-16) */. sqlit
1d830 65 33 20 2a 2a 70 70 44 62 20 20 20 20 20 20 20 e3 **ppDb
1d840 20 20 20 2f 2a 20 4f 55 54 3a 20 53 51 4c 69 74 /* OUT: SQLit
1d850 65 20 64 62 20 68 61 6e 64 6c 65 20 2a 2f 0a 29 e db handle */.)
1d860 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
1d870 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 sqlite3_open_v2
1d880 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a (. const char *
1d890 66 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 44 filename, /* D
1d8a0 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 atabase filename
1d8b0 20 28 55 54 46 2d 38 29 20 2a 2f 0a 20 20 73 71 (UTF-8) */. sq
1d8c0 6c 69 74 65 33 20 2a 2a 70 70 44 62 2c 20 20 20 lite3 **ppDb,
1d8d0 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 51 /* OUT: SQ
1d8e0 4c 69 74 65 20 64 62 20 68 61 6e 64 6c 65 20 2a Lite db handle *
1d8f0 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 /. int flags,
1d900 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
1d910 6c 61 67 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 lags */. const
1d920 63 68 61 72 20 2a 7a 56 66 73 20 20 20 20 20 20 char *zVfs
1d930 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 56 46 53 /* Name of VFS
1d940 20 6d 6f 64 75 6c 65 20 74 6f 20 75 73 65 20 2a module to use *
1d950 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 /.);../*.** CAPI
1d960 33 52 45 46 3a 20 45 72 72 6f 72 20 43 6f 64 65 3REF: Error Code
1d970 73 20 41 6e 64 20 4d 65 73 73 61 67 65 73 20 7b s And Messages {
1d980 48 31 32 38 30 30 7d 20 3c 53 36 30 32 30 30 3e H12800} <S60200>
1d990 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
1d9a0 65 33 5f 65 72 72 63 6f 64 65 28 29 20 69 6e 74 e3_errcode() int
1d9b0 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 erface returns t
1d9c0 68 65 20 6e 75 6d 65 72 69 63 20 5b 72 65 73 75 he numeric [resu
1d9d0 6c 74 20 63 6f 64 65 5d 20 6f 72 0a 2a 2a 20 5b lt code] or.** [
1d9e0 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 extended result
1d9f0 63 6f 64 65 5d 20 66 6f 72 20 74 68 65 20 6d 6f code] for the mo
1da00 73 74 20 72 65 63 65 6e 74 20 66 61 69 6c 65 64 st recent failed
1da10 20 73 71 6c 69 74 65 33 5f 2a 20 41 50 49 20 63 sqlite3_* API c
1da20 61 6c 6c 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 all.** associate
1da30 64 20 77 69 74 68 20 61 20 5b 64 61 74 61 62 61 d with a [databa
1da40 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20 se connection].
1da50 49 66 20 61 20 70 72 69 6f 72 20 41 50 49 20 63 If a prior API c
1da60 61 6c 6c 20 66 61 69 6c 65 64 0a 2a 2a 20 62 75 all failed.** bu
1da70 74 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e t the most recen
1da80 74 20 41 50 49 20 63 61 6c 6c 20 73 75 63 63 65 t API call succe
1da90 65 64 65 64 2c 20 74 68 65 20 72 65 74 75 72 6e eded, the return
1daa0 20 76 61 6c 75 65 20 66 72 6f 6d 0a 2a 2a 20 73 value from.** s
1dab0 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 qlite3_errcode()
1dac0 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 20 20 is undefined.
1dad0 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78 74 65 The sqlite3_exte
1dae0 6e 64 65 64 5f 65 72 72 63 6f 64 65 28 29 0a 2a nded_errcode().*
1daf0 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 74 * interface is t
1db00 68 65 20 73 61 6d 65 20 65 78 63 65 70 74 20 74 he same except t
1db10 68 61 74 20 69 74 20 61 6c 77 61 79 73 20 72 65 hat it always re
1db20 74 75 72 6e 73 20 74 68 65 20 0a 2a 2a 20 5b 65 turns the .** [e
1db30 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 xtended result c
1db40 6f 64 65 5d 20 65 76 65 6e 20 77 68 65 6e 20 65 ode] even when e
1db50 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 xtended result c
1db60 6f 64 65 73 20 61 72 65 0a 2a 2a 20 64 69 73 61 odes are.** disa
1db70 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 bled..**.** The
1db80 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 sqlite3_errmsg()
1db90 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 65 72 72 and sqlite3_err
1dba0 6d 73 67 31 36 28 29 20 72 65 74 75 72 6e 20 45 msg16() return E
1dbb0 6e 67 6c 69 73 68 2d 6c 61 6e 67 75 61 67 65 0a nglish-language.
1dbc0 2a 2a 20 74 65 78 74 20 74 68 61 74 20 64 65 73 ** text that des
1dbd0 63 72 69 62 65 73 20 74 68 65 20 65 72 72 6f 72 cribes the error
1dbe0 2c 20 61 73 20 65 69 74 68 65 72 20 55 54 46 2d , as either UTF-
1dbf0 38 20 6f 72 20 55 54 46 2d 31 36 20 72 65 73 70 8 or UTF-16 resp
1dc00 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 4d 65 6d ectively..** Mem
1dc10 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 ory to hold the
1dc20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74 error message st
1dc30 72 69 6e 67 20 69 73 20 6d 61 6e 61 67 65 64 20 ring is managed
1dc40 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 2a 2a 20 54 internally..** T
1dc50 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64 he application d
1dc60 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 oes not need to
1dc70 77 6f 72 72 79 20 61 62 6f 75 74 20 66 72 65 65 worry about free
1dc80 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74 2e 0a ing the result..
1dc90 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65 20 ** However, the
1dca0 65 72 72 6f 72 20 73 74 72 69 6e 67 20 6d 69 67 error string mig
1dcb0 68 74 20 62 65 20 6f 76 65 72 77 72 69 74 74 65 ht be overwritte
1dcc0 6e 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 65 64 n or deallocated
1dcd0 20 62 79 0a 2a 2a 20 73 75 62 73 65 71 75 65 6e by.** subsequen
1dce0 74 20 63 61 6c 6c 73 20 74 6f 20 6f 74 68 65 72 t calls to other
1dcf0 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 SQLite interfac
1dd00 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a e functions..**.
1dd10 2a 2a 20 57 68 65 6e 20 74 68 65 20 73 65 72 69 ** When the seri
1dd20 61 6c 69 7a 65 64 20 5b 74 68 72 65 61 64 69 6e alized [threadin
1dd30 67 20 6d 6f 64 65 5d 20 69 73 20 69 6e 20 75 73 g mode] is in us
1dd40 65 2c 20 69 74 20 6d 69 67 68 74 20 62 65 20 74 e, it might be t
1dd50 68 65 0a 2a 2a 20 63 61 73 65 20 74 68 61 74 20 he.** case that
1dd60 61 20 73 65 63 6f 6e 64 20 65 72 72 6f 72 20 6f a second error o
1dd70 63 63 75 72 73 20 6f 6e 20 61 20 73 65 70 61 72 ccurs on a separ
1dd80 61 74 65 20 74 68 72 65 61 64 20 69 6e 20 62 65 ate thread in be
1dd90 74 77 65 65 6e 0a 2a 2a 20 74 68 65 20 74 69 6d tween.** the tim
1dda0 65 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 65 e of the first e
1ddb0 72 72 6f 72 20 61 6e 64 20 74 68 65 20 63 61 6c rror and the cal
1ddc0 6c 20 74 6f 20 74 68 65 73 65 20 69 6e 74 65 72 l to these inter
1ddd0 66 61 63 65 73 2e 0a 2a 2a 20 57 68 65 6e 20 74 faces..** When t
1dde0 68 61 74 20 68 61 70 70 65 6e 73 2c 20 74 68 65 hat happens, the
1ddf0 20 73 65 63 6f 6e 64 20 65 72 72 6f 72 20 77 69 second error wi
1de00 6c 6c 20 62 65 20 72 65 70 6f 72 74 65 64 20 73 ll be reported s
1de10 69 6e 63 65 20 74 68 65 73 65 0a 2a 2a 20 69 6e ince these.** in
1de20 74 65 72 66 61 63 65 73 20 61 6c 77 61 79 73 20 terfaces always
1de30 72 65 70 6f 72 74 20 74 68 65 20 6d 6f 73 74 20 report the most
1de40 72 65 63 65 6e 74 20 72 65 73 75 6c 74 2e 20 20 recent result.
1de50 54 6f 20 61 76 6f 69 64 0a 2a 2a 20 74 68 69 73 To avoid.** this
1de60 2c 20 65 61 63 68 20 74 68 72 65 61 64 20 63 61 , each thread ca
1de70 6e 20 6f 62 74 61 69 6e 20 65 78 63 6c 75 73 69 n obtain exclusi
1de80 76 65 20 75 73 65 20 6f 66 20 74 68 65 20 5b 64 ve use of the [d
1de90 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
1dea0 6f 6e 5d 20 44 0a 2a 2a 20 62 79 20 69 6e 76 6f on] D.** by invo
1deb0 6b 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6d 75 king [sqlite3_mu
1dec0 74 65 78 5f 65 6e 74 65 72 5d 28 5b 73 71 6c 69 tex_enter]([sqli
1ded0 74 65 33 5f 64 62 5f 6d 75 74 65 78 5d 28 44 29 te3_db_mutex](D)
1dee0 29 20 62 65 66 6f 72 65 20 62 65 67 69 6e 6e 69 ) before beginni
1def0 6e 67 0a 2a 2a 20 74 6f 20 75 73 65 20 44 20 61 ng.** to use D a
1df00 6e 64 20 69 6e 76 6f 6b 69 6e 67 20 5b 73 71 6c nd invoking [sql
1df10 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
1df20 5d 28 5b 73 71 6c 69 74 65 33 5f 64 62 5f 6d 75 ]([sqlite3_db_mu
1df30 74 65 78 5d 28 44 29 29 20 61 66 74 65 72 0a 2a tex](D)) after.*
1df40 2a 20 61 6c 6c 20 63 61 6c 6c 73 20 74 6f 20 74 * all calls to t
1df50 68 65 20 69 6e 74 65 72 66 61 63 65 73 20 6c 69 he interfaces li
1df60 73 74 65 64 20 68 65 72 65 20 61 72 65 20 63 6f sted here are co
1df70 6d 70 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 mpleted..**.** I
1df80 66 20 61 6e 20 69 6e 74 65 72 66 61 63 65 20 66 f an interface f
1df90 61 69 6c 73 20 77 69 74 68 20 53 51 4c 49 54 45 ails with SQLITE
1dfa0 5f 4d 49 53 55 53 45 2c 20 74 68 61 74 20 6d 65 _MISUSE, that me
1dfb0 61 6e 73 20 74 68 65 20 69 6e 74 65 72 66 61 63 ans the interfac
1dfc0 65 0a 2a 2a 20 77 61 73 20 69 6e 76 6f 6b 65 64 e.** was invoked
1dfd0 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 62 79 20 incorrectly by
1dfe0 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e the application.
1dff0 20 20 49 6e 20 74 68 61 74 20 63 61 73 65 2c 20 In that case,
1e000 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 the.** error cod
1e010 65 20 61 6e 64 20 6d 65 73 73 61 67 65 20 6d 61 e and message ma
1e020 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 y or may not be
1e030 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 set..**.** Requi
1e040 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 rements:.** [H12
1e050 38 30 31 5d 20 5b 48 31 32 38 30 32 5d 20 5b 48 801] [H12802] [H
1e060 31 32 38 30 33 5d 20 5b 48 31 32 38 30 37 5d 20 12803] [H12807]
1e070 5b 48 31 32 38 30 38 5d 20 5b 48 31 32 38 30 39 [H12808] [H12809
1e080 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
1e090 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 72 72 63 int sqlite3_errc
1e0a0 6f 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29 ode(sqlite3 *db)
1e0b0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
1e0c0 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 sqlite3_extende
1e0d0 64 5f 65 72 72 63 6f 64 65 28 73 71 6c 69 74 65 d_errcode(sqlite
1e0e0 33 20 2a 64 62 29 3b 0a 53 51 4c 49 54 45 5f 41 3 *db);.SQLITE_A
1e0f0 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 PI const char *s
1e100 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 73 71 qlite3_errmsg(sq
1e110 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f lite3*);.SQLITE_
1e120 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a API const void *
1e130 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 sqlite3_errmsg16
1e140 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a (sqlite3*);../*.
1e150 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 51 4c ** CAPI3REF: SQL
1e160 20 53 74 61 74 65 6d 65 6e 74 20 4f 62 6a 65 63 Statement Objec
1e170 74 20 7b 48 31 33 30 30 30 7d 20 3c 48 31 33 30 t {H13000} <H130
1e180 31 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 10>.** KEYWORDS:
1e190 20 7b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 {prepared state
1e1a0 6d 65 6e 74 7d 20 7b 70 72 65 70 61 72 65 64 20 ment} {prepared
1e1b0 73 74 61 74 65 6d 65 6e 74 73 7d 0a 2a 2a 0a 2a statements}.**.*
1e1c0 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 * An instance of
1e1d0 20 74 68 69 73 20 6f 62 6a 65 63 74 20 72 65 70 this object rep
1e1e0 72 65 73 65 6e 74 73 20 61 20 73 69 6e 67 6c 65 resents a single
1e1f0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a SQL statement..
1e200 2a 2a 20 54 68 69 73 20 6f 62 6a 65 63 74 20 69 ** This object i
1e210 73 20 76 61 72 69 6f 75 73 6c 79 20 6b 6e 6f 77 s variously know
1e220 6e 20 61 73 20 61 20 22 70 72 65 70 61 72 65 64 n as a "prepared
1e230 20 73 74 61 74 65 6d 65 6e 74 22 20 6f 72 20 61 statement" or a
1e240 0a 2a 2a 20 22 63 6f 6d 70 69 6c 65 64 20 53 51 .** "compiled SQ
1e250 4c 20 73 74 61 74 65 6d 65 6e 74 22 20 6f 72 20 L statement" or
1e260 73 69 6d 70 6c 79 20 61 73 20 61 20 22 73 74 61 simply as a "sta
1e270 74 65 6d 65 6e 74 22 2e 0a 2a 2a 0a 2a 2a 20 54 tement"..**.** T
1e280 68 65 20 6c 69 66 65 20 6f 66 20 61 20 73 74 61 he life of a sta
1e290 74 65 6d 65 6e 74 20 6f 62 6a 65 63 74 20 67 6f tement object go
1e2a0 65 73 20 73 6f 6d 65 74 68 69 6e 67 20 6c 69 6b es something lik
1e2b0 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 3c 6f e this:.**.** <o
1e2c0 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 43 72 65 61 74 l>.** <li> Creat
1e2d0 65 20 74 68 65 20 6f 62 6a 65 63 74 20 75 73 69 e the object usi
1e2e0 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 ng [sqlite3_prep
1e2f0 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 61 20 72 are_v2()] or a r
1e300 65 6c 61 74 65 64 0a 2a 2a 20 20 20 20 20 20 66 elated.** f
1e310 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 3c 6c 69 3e unction..** <li>
1e320 20 42 69 6e 64 20 76 61 6c 75 65 73 20 74 6f 20 Bind values to
1e330 5b 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 73 [host parameters
1e340 5d 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 ] using the sqli
1e350 74 65 33 5f 62 69 6e 64 5f 2a 28 29 0a 2a 2a 20 te3_bind_*().**
1e360 20 20 20 20 20 69 6e 74 65 72 66 61 63 65 73 2e interfaces.
1e370 0a 2a 2a 20 3c 6c 69 3e 20 52 75 6e 20 74 68 65 .** <li> Run the
1e380 20 53 51 4c 20 62 79 20 63 61 6c 6c 69 6e 67 20 SQL by calling
1e390 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d [sqlite3_step()]
1e3a0 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 69 6d one or more tim
1e3b0 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 52 65 73 65 es..** <li> Rese
1e3c0 74 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 t the statement
1e3d0 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 72 using [sqlite3_r
1e3e0 65 73 65 74 28 29 5d 20 74 68 65 6e 20 67 6f 20 eset()] then go
1e3f0 62 61 63 6b 0a 2a 2a 20 20 20 20 20 20 74 6f 20 back.** to
1e400 73 74 65 70 20 32 2e 20 20 44 6f 20 74 68 69 73 step 2. Do this
1e410 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 74 69 zero or more ti
1e420 6d 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 44 65 73 mes..** <li> Des
1e430 74 72 6f 79 20 74 68 65 20 6f 62 6a 65 63 74 20 troy the object
1e440 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 using [sqlite3_f
1e450 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a 20 3c inalize()]..** <
1e460 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a 20 52 65 66 65 72 /ol>.**.** Refer
1e470 20 74 6f 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f to documentatio
1e480 6e 20 6f 6e 20 69 6e 64 69 76 69 64 75 61 6c 20 n on individual
1e490 6d 65 74 68 6f 64 73 20 61 62 6f 76 65 20 66 6f methods above fo
1e4a0 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 r additional.**
1e4b0 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a information..*/.
1e4c0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 typedef struct s
1e4d0 71 6c 69 74 65 33 5f 73 74 6d 74 20 73 71 6c 69 qlite3_stmt sqli
1e4e0 74 65 33 5f 73 74 6d 74 3b 0a 0a 2f 2a 0a 2a 2a te3_stmt;../*.**
1e4f0 20 43 41 50 49 33 52 45 46 3a 20 52 75 6e 2d 74 CAPI3REF: Run-t
1e500 69 6d 65 20 4c 69 6d 69 74 73 20 7b 48 31 32 37 ime Limits {H127
1e510 36 30 7d 20 3c 53 32 30 36 30 30 3e 0a 2a 2a 0a 60} <S20600>.**.
1e520 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 ** This interfac
1e530 65 20 61 6c 6c 6f 77 73 20 74 68 65 20 73 69 7a e allows the siz
1e540 65 20 6f 66 20 76 61 72 69 6f 75 73 20 63 6f 6e e of various con
1e550 73 74 72 75 63 74 73 20 74 6f 20 62 65 20 6c 69 structs to be li
1e560 6d 69 74 65 64 0a 2a 2a 20 6f 6e 20 61 20 63 6f mited.** on a co
1e570 6e 6e 65 63 74 69 6f 6e 20 62 79 20 63 6f 6e 6e nnection by conn
1e580 65 63 74 69 6f 6e 20 62 61 73 69 73 2e 20 20 54 ection basis. T
1e590 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 he first paramet
1e5a0 65 72 20 69 73 20 74 68 65 0a 2a 2a 20 5b 64 61 er is the.** [da
1e5b0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
1e5c0 6e 5d 20 77 68 6f 73 65 20 6c 69 6d 69 74 20 69 n] whose limit i
1e5d0 73 20 74 6f 20 62 65 20 73 65 74 20 6f 72 20 71 s to be set or q
1e5e0 75 65 72 69 65 64 2e 20 20 54 68 65 0a 2a 2a 20 ueried. The.**
1e5f0 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 second parameter
1e600 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 20 5b is one of the [
1e610 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 69 65 73 limit categories
1e620 5d 20 74 68 61 74 20 64 65 66 69 6e 65 20 61 0a ] that define a.
1e630 2a 2a 20 63 6c 61 73 73 20 6f 66 20 63 6f 6e 73 ** class of cons
1e640 74 72 75 63 74 73 20 74 6f 20 62 65 20 73 69 7a tructs to be siz
1e650 65 20 6c 69 6d 69 74 65 64 2e 20 20 54 68 65 20 e limited. The
1e660 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 third parameter
1e670 69 73 20 74 68 65 0a 2a 2a 20 6e 65 77 20 6c 69 is the.** new li
1e680 6d 69 74 20 66 6f 72 20 74 68 61 74 20 63 6f 6e mit for that con
1e690 73 74 72 75 63 74 2e 20 20 54 68 65 20 66 75 6e struct. The fun
1e6a0 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 ction returns th
1e6b0 65 20 6f 6c 64 20 6c 69 6d 69 74 2e 0a 2a 2a 0a e old limit..**.
1e6c0 2a 2a 20 49 66 20 74 68 65 20 6e 65 77 20 6c 69 ** If the new li
1e6d0 6d 69 74 20 69 73 20 61 20 6e 65 67 61 74 69 76 mit is a negativ
1e6e0 65 20 6e 75 6d 62 65 72 2c 20 74 68 65 20 6c 69 e number, the li
1e6f0 6d 69 74 20 69 73 20 75 6e 63 68 61 6e 67 65 64 mit is unchanged
1e700 2e 0a 2a 2a 20 46 6f 72 20 74 68 65 20 6c 69 6d ..** For the lim
1e710 69 74 20 63 61 74 65 67 6f 72 79 20 6f 66 20 53 it category of S
1e720 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 58 59 5a 20 QLITE_LIMIT_XYZ
1e730 74 68 65 72 65 20 69 73 20 61 20 0a 2a 2a 20 5b there is a .** [
1e740 6c 69 6d 69 74 73 20 7c 20 68 61 72 64 20 75 70 limits | hard up
1e750 70 65 72 20 62 6f 75 6e 64 5d 0a 2a 2a 20 73 65 per bound].** se
1e760 74 20 62 79 20 61 20 63 6f 6d 70 69 6c 65 2d 74 t by a compile-t
1e770 69 6d 65 20 43 20 70 72 65 70 72 6f 63 65 73 73 ime C preprocess
1e780 6f 72 20 6d 61 63 72 6f 20 6e 61 6d 65 64 20 0a or macro named .
1e790 2a 2a 20 5b 6c 69 6d 69 74 73 20 7c 20 53 51 4c ** [limits | SQL
1e7a0 49 54 45 5f 4d 41 58 5f 58 59 5a 5d 2e 0a 2a 2a ITE_MAX_XYZ]..**
1e7b0 20 28 54 68 65 20 22 5f 4c 49 4d 49 54 5f 22 20 (The "_LIMIT_"
1e7c0 69 6e 20 74 68 65 20 6e 61 6d 65 20 69 73 20 63 in the name is c
1e7d0 68 61 6e 67 65 64 20 74 6f 20 22 5f 4d 41 58 5f hanged to "_MAX_
1e7e0 22 2e 29 0a 2a 2a 20 41 74 74 65 6d 70 74 73 20 ".).** Attempts
1e7f0 74 6f 20 69 6e 63 72 65 61 73 65 20 61 20 6c 69 to increase a li
1e800 6d 69 74 20 61 62 6f 76 65 20 69 74 73 20 68 61 mit above its ha
1e810 72 64 20 75 70 70 65 72 20 62 6f 75 6e 64 20 61 rd upper bound a
1e820 72 65 0a 2a 2a 20 73 69 6c 65 6e 74 6c 79 20 74 re.** silently t
1e830 72 75 6e 63 61 74 65 64 20 74 6f 20 74 68 65 20 runcated to the
1e840 68 61 72 64 20 75 70 70 65 72 20 6c 69 6d 69 74 hard upper limit
1e850 2e 0a 2a 2a 0a 2a 2a 20 52 75 6e 20 74 69 6d 65 ..**.** Run time
1e860 20 6c 69 6d 69 74 73 20 61 72 65 20 69 6e 74 65 limits are inte
1e870 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69 6e 20 nded for use in
1e880 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 applications tha
1e890 74 20 6d 61 6e 61 67 65 0a 2a 2a 20 62 6f 74 68 t manage.** both
1e8a0 20 74 68 65 69 72 20 6f 77 6e 20 69 6e 74 65 72 their own inter
1e8b0 6e 61 6c 20 64 61 74 61 62 61 73 65 20 61 6e 64 nal database and
1e8c0 20 61 6c 73 6f 20 64 61 74 61 62 61 73 65 73 20 also databases
1e8d0 74 68 61 74 20 61 72 65 20 63 6f 6e 74 72 6f 6c that are control
1e8e0 6c 65 64 0a 2a 2a 20 62 79 20 75 6e 74 72 75 73 led.** by untrus
1e8f0 74 65 64 20 65 78 74 65 72 6e 61 6c 20 73 6f 75 ted external sou
1e900 72 63 65 73 2e 20 20 41 6e 20 65 78 61 6d 70 6c rces. An exampl
1e910 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 69 e application mi
1e920 67 68 74 20 62 65 20 61 0a 2a 2a 20 77 65 62 20 ght be a.** web
1e930 62 72 6f 77 73 65 72 20 74 68 61 74 20 68 61 73 browser that has
1e940 20 69 74 73 20 6f 77 6e 20 64 61 74 61 62 61 73 its own databas
1e950 65 73 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 68 es for storing h
1e960 69 73 74 6f 72 79 20 61 6e 64 0a 2a 2a 20 73 65 istory and.** se
1e970 70 61 72 61 74 65 20 64 61 74 61 62 61 73 65 73 parate databases
1e980 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 4a controlled by J
1e990 61 76 61 53 63 72 69 70 74 20 61 70 70 6c 69 63 avaScript applic
1e9a0 61 74 69 6f 6e 73 20 64 6f 77 6e 6c 6f 61 64 65 ations downloade
1e9b0 64 0a 2a 2a 20 6f 66 66 20 74 68 65 20 49 6e 74 d.** off the Int
1e9c0 65 72 6e 65 74 2e 20 20 54 68 65 20 69 6e 74 65 ernet. The inte
1e9d0 72 6e 61 6c 20 64 61 74 61 62 61 73 65 73 20 63 rnal databases c
1e9e0 61 6e 20 62 65 20 67 69 76 65 6e 20 74 68 65 0a an be given the.
1e9f0 2a 2a 20 6c 61 72 67 65 2c 20 64 65 66 61 75 6c ** large, defaul
1ea00 74 20 6c 69 6d 69 74 73 2e 20 20 44 61 74 61 62 t limits. Datab
1ea10 61 73 65 73 20 6d 61 6e 61 67 65 64 20 62 79 20 ases managed by
1ea20 65 78 74 65 72 6e 61 6c 20 73 6f 75 72 63 65 73 external sources
1ea30 20 63 61 6e 0a 2a 2a 20 62 65 20 67 69 76 65 6e can.** be given
1ea40 20 6d 75 63 68 20 73 6d 61 6c 6c 65 72 20 6c 69 much smaller li
1ea50 6d 69 74 73 20 64 65 73 69 67 6e 65 64 20 74 6f mits designed to
1ea60 20 70 72 65 76 65 6e 74 20 61 20 64 65 6e 69 61 prevent a denia
1ea70 6c 20 6f 66 20 73 65 72 76 69 63 65 0a 2a 2a 20 l of service.**
1ea80 61 74 74 61 63 6b 2e 20 20 44 65 76 65 6c 6f 70 attack. Develop
1ea90 65 72 73 20 6d 69 67 68 74 20 61 6c 73 6f 20 77 ers might also w
1eaa0 61 6e 74 20 74 6f 20 75 73 65 20 74 68 65 20 5b ant to use the [
1eab0 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 sqlite3_set_auth
1eac0 6f 72 69 7a 65 72 28 29 5d 0a 2a 2a 20 69 6e 74 orizer()].** int
1ead0 65 72 66 61 63 65 20 74 6f 20 66 75 72 74 68 65 erface to furthe
1eae0 72 20 63 6f 6e 74 72 6f 6c 20 75 6e 74 72 75 73 r control untrus
1eaf0 74 65 64 20 53 51 4c 2e 20 20 54 68 65 20 73 69 ted SQL. The si
1eb00 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ze of the databa
1eb10 73 65 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 79 se.** created by
1eb20 20 61 6e 20 75 6e 74 72 75 73 74 65 64 20 73 63 an untrusted sc
1eb30 72 69 70 74 20 63 61 6e 20 62 65 20 63 6f 6e 74 ript can be cont
1eb40 61 69 6e 65 64 20 75 73 69 6e 67 20 74 68 65 0a ained using the.
1eb50 2a 2a 20 5b 6d 61 78 5f 70 61 67 65 5f 63 6f 75 ** [max_page_cou
1eb60 6e 74 5d 20 5b 50 52 41 47 4d 41 5d 2e 0a 2a 2a nt] [PRAGMA]..**
1eb70 0a 2a 2a 20 4e 65 77 20 72 75 6e 2d 74 69 6d 65 .** New run-time
1eb80 20 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 69 65 limit categorie
1eb90 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 s may be added i
1eba0 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 n future release
1ebb0 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 s..**.** Require
1ebc0 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 37 36 ments:.** [H1276
1ebd0 32 5d 20 5b 48 31 32 37 36 36 5d 20 5b 48 31 32 2] [H12766] [H12
1ebe0 37 36 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 769].*/.SQLITE_A
1ebf0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c PI int sqlite3_l
1ec00 69 6d 69 74 28 73 71 6c 69 74 65 33 2a 2c 20 69 imit(sqlite3*, i
1ec10 6e 74 20 69 64 2c 20 69 6e 74 20 6e 65 77 56 61 nt id, int newVa
1ec20 6c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 l);../*.** CAPI3
1ec30 52 45 46 3a 20 52 75 6e 2d 54 69 6d 65 20 4c 69 REF: Run-Time Li
1ec40 6d 69 74 20 43 61 74 65 67 6f 72 69 65 73 20 7b mit Categories {
1ec50 48 31 32 37 39 30 7d 20 3c 48 31 32 37 36 30 3e H12790} <H12760>
1ec60 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 6c .** KEYWORDS: {l
1ec70 69 6d 69 74 20 63 61 74 65 67 6f 72 79 7d 20 7b imit category} {
1ec80 2a 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 69 65 *limit categorie
1ec90 73 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 s}.**.** These c
1eca0 6f 6e 73 74 61 6e 74 73 20 64 65 66 69 6e 65 20 onstants define
1ecb0 76 61 72 69 6f 75 73 20 70 65 72 66 6f 72 6d 61 various performa
1ecc0 6e 63 65 20 6c 69 6d 69 74 73 0a 2a 2a 20 74 68 nce limits.** th
1ecd0 61 74 20 63 61 6e 20 62 65 20 6c 6f 77 65 72 65 at can be lowere
1ece0 64 20 61 74 20 72 75 6e 2d 74 69 6d 65 20 75 73 d at run-time us
1ecf0 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6c 69 6d ing [sqlite3_lim
1ed00 69 74 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 73 79 it()]..** The sy
1ed10 6e 6f 70 73 69 73 20 6f 66 20 74 68 65 20 6d 65 nopsis of the me
1ed20 61 6e 69 6e 67 73 20 6f 66 20 74 68 65 20 76 61 anings of the va
1ed30 72 69 6f 75 73 20 6c 69 6d 69 74 73 20 69 73 20 rious limits is
1ed40 73 68 6f 77 6e 20 62 65 6c 6f 77 2e 0a 2a 2a 20 shown below..**
1ed50 41 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 Additional infor
1ed60 6d 61 74 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 mation is availa
1ed70 62 6c 65 20 61 74 20 5b 6c 69 6d 69 74 73 20 7c ble at [limits |
1ed80 20 4c 69 6d 69 74 73 20 69 6e 20 53 51 4c 69 74 Limits in SQLit
1ed90 65 5d 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a e]..**.** <dl>.*
1eda0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d * <dt>SQLITE_LIM
1edb0 49 54 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e 0a 2a IT_LENGTH</dt>.*
1edc0 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 * <dd>The maximu
1edd0 6d 20 73 69 7a 65 20 6f 66 20 61 6e 79 20 73 74 m size of any st
1ede0 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20 6f 72 20 ring or BLOB or
1edf0 74 61 62 6c 65 20 72 6f 77 2e 3c 64 64 3e 0a 2a table row.<dd>.*
1ee00 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f *.** <dt>SQLITE_
1ee10 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47 54 48 LIMIT_SQL_LENGTH
1ee20 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 </dt>.** <dd>The
1ee30 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 maximum length
1ee40 6f 66 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d of an SQL statem
1ee50 65 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 ent.</dd>.**.**
1ee60 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 <dt>SQLITE_LIMIT
1ee70 5f 43 4f 4c 55 4d 4e 3c 2f 64 74 3e 0a 2a 2a 20 _COLUMN</dt>.**
1ee80 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 <dd>The maximum
1ee90 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e number of column
1eea0 73 20 69 6e 20 61 20 74 61 62 6c 65 20 64 65 66 s in a table def
1eeb0 69 6e 69 74 69 6f 6e 20 6f 72 20 69 6e 20 74 68 inition or in th
1eec0 65 0a 2a 2a 20 72 65 73 75 6c 74 20 73 65 74 20 e.** result set
1eed0 6f 66 20 61 20 5b 53 45 4c 45 43 54 5d 20 6f 72 of a [SELECT] or
1eee0 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d the maximum num
1eef0 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 ber of columns i
1ef00 6e 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 6f 72 n an index.** or
1ef10 20 69 6e 20 61 6e 20 4f 52 44 45 52 20 42 59 20 in an ORDER BY
1ef20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 or GROUP BY clau
1ef30 73 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c se.</dd>.**.** <
1ef40 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f dt>SQLITE_LIMIT_
1ef50 45 58 50 52 5f 44 45 50 54 48 3c 2f 64 74 3e 0a EXPR_DEPTH</dt>.
1ef60 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d ** <dd>The maxim
1ef70 75 6d 20 64 65 70 74 68 20 6f 66 20 74 68 65 20 um depth of the
1ef80 70 61 72 73 65 20 74 72 65 65 20 6f 6e 20 61 6e parse tree on an
1ef90 79 20 65 78 70 72 65 73 73 69 6f 6e 2e 3c 2f 64 y expression.</d
1efa0 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c d>.**.** <dt>SQL
1efb0 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d 50 4f 55 ITE_LIMIT_COMPOU
1efc0 4e 44 5f 53 45 4c 45 43 54 3c 2f 64 74 3e 0a 2a ND_SELECT</dt>.*
1efd0 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 * <dd>The maximu
1efe0 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d m number of term
1eff0 73 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 s in a compound
1f000 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 SELECT statement
1f010 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 .</dd>.**.** <dt
1f020 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 44 >SQLITE_LIMIT_VD
1f030 42 45 5f 4f 50 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 BE_OP</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 69 6e 73 74 72 75 63 74 mber of instruct
1f060 69 6f 6e 73 20 69 6e 20 61 20 76 69 72 74 75 61 ions in a virtua
1f070 6c 20 6d 61 63 68 69 6e 65 20 70 72 6f 67 72 61 l machine progra
1f080 6d 0a 2a 2a 20 75 73 65 64 20 74 6f 20 69 6d 70 m.** used to imp
1f090 6c 65 6d 65 6e 74 20 61 6e 20 53 51 4c 20 73 74 lement an SQL st
1f0a0 61 74 65 6d 65 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a atement.</dd>.**
1f0b0 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c .** <dt>SQLITE_L
1f0c0 49 4d 49 54 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 IMIT_FUNCTION_AR
1f0d0 47 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 G</dt>.** <dd>Th
1f0e0 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 e maximum number
1f0f0 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 6f 6e of arguments on
1f100 20 61 20 66 75 6e 63 74 69 6f 6e 2e 3c 2f 64 64 a function.</dd
1f110 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 >.**.** <dt>SQLI
1f120 54 45 5f 4c 49 4d 49 54 5f 41 54 54 41 43 48 45 TE_LIMIT_ATTACHE
1f130 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 D</dt>.** <dd>Th
1f140 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 e maximum number
1f150 20 6f 66 20 5b 41 54 54 41 43 48 20 7c 20 61 74 of [ATTACH | at
1f160 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73 tached databases
1f170 5d 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 ].</dd>.**.** <d
1f180 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c t>SQLITE_LIMIT_L
1f190 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 IKE_PATTERN_LENG
1f1a0 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 TH</dt>.** <dd>T
1f1b0 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 he maximum lengt
1f1c0 68 20 6f 66 20 74 68 65 20 70 61 74 74 65 72 6e h of the pattern
1f1d0 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 argument to the
1f1e0 20 5b 4c 49 4b 45 5d 20 6f 72 0a 2a 2a 20 5b 47 [LIKE] or.** [G
1f1f0 4c 4f 42 5d 20 6f 70 65 72 61 74 6f 72 73 2e 3c LOB] operators.<
1f200 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 /dd>.**.** <dt>S
1f210 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 QLITE_LIMIT_VARI
1f220 41 42 4c 45 5f 4e 55 4d 42 45 52 3c 2f 64 74 3e ABLE_NUMBER</dt>
1f230 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 .** <dd>The maxi
1f240 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 76 61 mum number of va
1f250 72 69 61 62 6c 65 73 20 69 6e 20 61 6e 20 53 51 riables in an SQ
1f260 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 L statement that
1f270 20 63 61 6e 0a 2a 2a 20 62 65 20 62 6f 75 6e 64 can.** be bound
1f280 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 .</dd>.**.** <dt
1f290 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 54 52 >SQLITE_LIMIT_TR
1f2a0 49 47 47 45 52 5f 44 45 50 54 48 3c 2f 64 74 3e IGGER_DEPTH</dt>
1f2b0 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 .** <dd>The maxi
1f2c0 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 72 65 63 mum depth of rec
1f2d0 75 72 73 69 6f 6e 20 66 6f 72 20 74 72 69 67 67 ursion for trigg
1f2e0 65 72 73 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 ers.</dd>.** </d
1f2f0 6c 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 l>.*/.#define SQ
1f300 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 LITE_LIMIT_LENGT
1f310 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 H
1f320 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 0.#define S
1f330 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f QLITE_LIMIT_SQL_
1f340 4c 45 4e 47 54 48 20 20 20 20 20 20 20 20 20 20 LENGTH
1f350 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 1.#define
1f360 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c SQLITE_LIMIT_COL
1f370 55 4d 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 UMN
1f380 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65 2.#define
1f390 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 45 58 SQLITE_LIMIT_EX
1f3a0 50 52 5f 44 45 50 54 48 20 20 20 20 20 20 20 20 PR_DEPTH
1f3b0 20 20 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e 3.#defin
1f3c0 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 e SQLITE_LIMIT_C
1f3d0 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 20 OMPOUND_SELECT
1f3e0 20 20 20 20 20 20 20 20 20 34 0a 23 64 65 66 69 4.#defi
1f3f0 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f ne SQLITE_LIMIT_
1f400 56 44 42 45 5f 4f 50 20 20 20 20 20 20 20 20 20 VDBE_OP
1f410 20 20 20 20 20 20 20 20 20 20 35 0a 23 64 65 66 5.#def
1f420 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 ine SQLITE_LIMIT
1f430 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 20 20 20 _FUNCTION_ARG
1f440 20 20 20 20 20 20 20 20 20 20 20 36 0a 23 64 65 6.#de
1f450 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 fine SQLITE_LIMI
1f460 54 5f 41 54 54 41 43 48 45 44 20 20 20 20 20 20 T_ATTACHED
1f470 20 20 20 20 20 20 20 20 20 20 20 20 37 0a 23 64 7.#d
1f480 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d efine SQLITE_LIM
1f490 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f IT_LIKE_PATTERN_
1f4a0 4c 45 4e 47 54 48 20 20 20 20 20 20 20 38 0a 23 LENGTH 8.#
1f4b0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 define SQLITE_LI
1f4c0 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d MIT_VARIABLE_NUM
1f4d0 42 45 52 20 20 20 20 20 20 20 20 20 20 20 39 0a BER 9.
1f4e0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c #define SQLITE_L
1f4f0 49 4d 49 54 5f 54 52 49 47 47 45 52 5f 44 45 50 IMIT_TRIGGER_DEP
1f500 54 48 20 20 20 20 20 20 20 20 20 20 20 20 31 30 TH 10
1f510 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
1f520 3a 20 43 6f 6d 70 69 6c 69 6e 67 20 41 6e 20 53 : Compiling An S
1f530 51 4c 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 31 QL Statement {H1
1f540 33 30 31 30 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 3010} <S10000>.*
1f550 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 53 51 4c * KEYWORDS: {SQL
1f560 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70 69 statement compi
1f570 6c 65 72 7d 0a 2a 2a 0a 2a 2a 20 54 6f 20 65 78 ler}.**.** To ex
1f580 65 63 75 74 65 20 61 6e 20 53 51 4c 20 71 75 65 ecute an SQL que
1f590 72 79 2c 20 69 74 20 6d 75 73 74 20 66 69 72 73 ry, it must firs
1f5a0 74 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 69 6e t be compiled in
1f5b0 74 6f 20 61 20 62 79 74 65 2d 63 6f 64 65 0a 2a to a byte-code.*
1f5c0 2a 20 70 72 6f 67 72 61 6d 20 75 73 69 6e 67 20 * program using
1f5d0 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 72 6f 75 one of these rou
1f5e0 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 tines..**.** The
1f5f0 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2c first argument,
1f600 20 22 64 62 22 2c 20 69 73 20 61 20 5b 64 61 74 "db", is a [dat
1f610 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
1f620 5d 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 ] obtained from
1f630 61 0a 2a 2a 20 70 72 69 6f 72 20 73 75 63 63 65 a.** prior succe
1f640 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 ssful call to [s
1f650 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 qlite3_open()],
1f660 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 [sqlite3_open_v2
1f670 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 ()] or.** [sqlit
1f680 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 20 20 54 e3_open16()]. T
1f690 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
1f6a0 65 63 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 ection must not
1f6b0 68 61 76 65 20 62 65 65 6e 20 63 6c 6f 73 65 64 have been closed
1f6c0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f ..**.** The seco
1f6d0 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 22 7a 53 nd argument, "zS
1f6e0 71 6c 22 2c 20 69 73 20 74 68 65 20 73 74 61 74 ql", is the stat
1f6f0 65 6d 65 6e 74 20 74 6f 20 62 65 20 63 6f 6d 70 ement to be comp
1f700 69 6c 65 64 2c 20 65 6e 63 6f 64 65 64 0a 2a 2a iled, encoded.**
1f710 20 61 73 20 65 69 74 68 65 72 20 55 54 46 2d 38 as either UTF-8
1f720 20 6f 72 20 55 54 46 2d 31 36 2e 20 20 54 68 65 or UTF-16. The
1f730 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 sqlite3_prepare
1f740 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 70 () and sqlite3_p
1f750 72 65 70 61 72 65 5f 76 32 28 29 0a 2a 2a 20 69 repare_v2().** i
1f760 6e 74 65 72 66 61 63 65 73 20 75 73 65 20 55 54 nterfaces use UT
1f770 46 2d 38 2c 20 61 6e 64 20 73 71 6c 69 74 65 33 F-8, and sqlite3
1f780 5f 70 72 65 70 61 72 65 31 36 28 29 20 61 6e 64 _prepare16() and
1f790 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 sqlite3_prepare
1f7a0 31 36 5f 76 32 28 29 0a 2a 2a 20 75 73 65 20 55 16_v2().** use U
1f7b0 54 46 2d 31 36 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 TF-16..**.** If
1f7c0 74 68 65 20 6e 42 79 74 65 20 61 72 67 75 6d 65 the nByte argume
1f7d0 6e 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 nt is less than
1f7e0 7a 65 72 6f 2c 20 74 68 65 6e 20 7a 53 71 6c 20 zero, then zSql
1f7f0 69 73 20 72 65 61 64 20 75 70 20 74 6f 20 74 68 is read up to th
1f800 65 0a 2a 2a 20 66 69 72 73 74 20 7a 65 72 6f 20 e.** first zero
1f810 74 65 72 6d 69 6e 61 74 6f 72 2e 20 49 66 20 6e terminator. If n
1f820 42 79 74 65 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 Byte is non-nega
1f830 74 69 76 65 2c 20 74 68 65 6e 20 69 74 20 69 73 tive, then it is
1f840 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 the maximum.**
1f850 6e 75 6d 62 65 72 20 6f 66 20 20 62 79 74 65 73 number of bytes
1f860 20 72 65 61 64 20 66 72 6f 6d 20 7a 53 71 6c 2e read from zSql.
1f870 20 20 57 68 65 6e 20 6e 42 79 74 65 20 69 73 20 When nByte is
1f880 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c 20 74 68 non-negative, th
1f890 65 0a 2a 2a 20 7a 53 71 6c 20 73 74 72 69 6e 67 e.** zSql string
1f8a0 20 65 6e 64 73 20 61 74 20 65 69 74 68 65 72 20 ends at either
1f8b0 74 68 65 20 66 69 72 73 74 20 27 5c 30 30 30 27 the first '\000'
1f8c0 20 6f 72 20 27 5c 75 30 30 30 30 27 20 63 68 61 or '\u0000' cha
1f8d0 72 61 63 74 65 72 20 6f 72 0a 2a 2a 20 74 68 65 racter or.** the
1f8e0 20 6e 42 79 74 65 2d 74 68 20 62 79 74 65 2c 20 nByte-th byte,
1f8f0 77 68 69 63 68 65 76 65 72 20 63 6f 6d 65 73 20 whichever comes
1f900 66 69 72 73 74 2e 20 49 66 20 74 68 65 20 63 61 first. If the ca
1f910 6c 6c 65 72 20 6b 6e 6f 77 73 0a 2a 2a 20 74 68 ller knows.** th
1f920 61 74 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 at the supplied
1f930 73 74 72 69 6e 67 20 69 73 20 6e 75 6c 2d 74 65 string is nul-te
1f940 72 6d 69 6e 61 74 65 64 2c 20 74 68 65 6e 20 74 rminated, then t
1f950 68 65 72 65 20 69 73 20 61 20 73 6d 61 6c 6c 0a here is a small.
1f960 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 61 ** performance a
1f970 64 76 61 6e 74 61 67 65 20 74 6f 20 62 65 20 67 dvantage to be g
1f980 61 69 6e 65 64 20 62 79 20 70 61 73 73 69 6e 67 ained by passing
1f990 20 61 6e 20 6e 42 79 74 65 20 70 61 72 61 6d 65 an nByte parame
1f9a0 74 65 72 20 74 68 61 74 0a 2a 2a 20 69 73 20 65 ter that.** is e
1f9b0 71 75 61 6c 20 74 6f 20 74 68 65 20 6e 75 6d 62 qual to the numb
1f9c0 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 er of bytes in t
1f9d0 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 he input string
1f9e0 3c 69 3e 69 6e 63 6c 75 64 69 6e 67 3c 2f 69 3e <i>including</i>
1f9f0 0a 2a 2a 20 74 68 65 20 6e 75 6c 2d 74 65 72 6d .** the nul-term
1fa00 69 6e 61 74 6f 72 20 62 79 74 65 73 2e 0a 2a 2a inator bytes..**
1fa10 0a 2a 2a 20 49 66 20 70 7a 54 61 69 6c 20 69 73 .** If pzTail is
1fa20 20 6e 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20 2a not NULL then *
1fa30 70 7a 54 61 69 6c 20 69 73 20 6d 61 64 65 20 74 pzTail is made t
1fa40 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 66 o point to the f
1fa50 69 72 73 74 20 62 79 74 65 0a 2a 2a 20 70 61 73 irst byte.** pas
1fa60 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 t the end of the
1fa70 20 66 69 72 73 74 20 53 51 4c 20 73 74 61 74 65 first SQL state
1fa80 6d 65 6e 74 20 69 6e 20 7a 53 71 6c 2e 20 20 54 ment in zSql. T
1fa90 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6f 6e hese routines on
1faa0 6c 79 0a 2a 2a 20 63 6f 6d 70 69 6c 65 20 74 68 ly.** compile th
1fab0 65 20 66 69 72 73 74 20 73 74 61 74 65 6d 65 6e e first statemen
1fac0 74 20 69 6e 20 7a 53 71 6c 2c 20 73 6f 20 2a 70 t in zSql, so *p
1fad0 7a 54 61 69 6c 20 69 73 20 6c 65 66 74 20 70 6f zTail is left po
1fae0 69 6e 74 69 6e 67 20 74 6f 0a 2a 2a 20 77 68 61 inting to.** wha
1faf0 74 20 72 65 6d 61 69 6e 73 20 75 6e 63 6f 6d 70 t remains uncomp
1fb00 69 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 2a 70 70 53 iled..**.** *ppS
1fb10 74 6d 74 20 69 73 20 6c 65 66 74 20 70 6f 69 6e tmt is left poin
1fb20 74 69 6e 67 20 74 6f 20 61 20 63 6f 6d 70 69 6c ting to a compil
1fb30 65 64 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 ed [prepared sta
1fb40 74 65 6d 65 6e 74 5d 20 74 68 61 74 20 63 61 6e tement] that can
1fb50 20 62 65 0a 2a 2a 20 65 78 65 63 75 74 65 64 20 be.** executed
1fb60 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 73 using [sqlite3_s
1fb70 74 65 70 28 29 5d 2e 20 20 49 66 20 74 68 65 72 tep()]. If ther
1fb80 65 20 69 73 20 61 6e 20 65 72 72 6f 72 2c 20 2a e is an error, *
1fb90 70 70 53 74 6d 74 20 69 73 20 73 65 74 0a 2a 2a ppStmt is set.**
1fba0 20 74 6f 20 4e 55 4c 4c 2e 20 20 49 66 20 74 68 to NULL. If th
1fbb0 65 20 69 6e 70 75 74 20 74 65 78 74 20 63 6f 6e e input text con
1fbc0 74 61 69 6e 73 20 6e 6f 20 53 51 4c 20 28 69 66 tains no SQL (if
1fbd0 20 74 68 65 20 69 6e 70 75 74 20 69 73 20 61 6e the input is an
1fbe0 20 65 6d 70 74 79 0a 2a 2a 20 73 74 72 69 6e 67 empty.** string
1fbf0 20 6f 72 20 61 20 63 6f 6d 6d 65 6e 74 29 20 74 or a comment) t
1fc00 68 65 6e 20 2a 70 70 53 74 6d 74 20 69 73 20 73 hen *ppStmt is s
1fc10 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 20 54 et to NULL..** T
1fc20 68 65 20 63 61 6c 6c 69 6e 67 20 70 72 6f 63 65 he calling proce
1fc30 64 75 72 65 20 69 73 20 72 65 73 70 6f 6e 73 69 dure is responsi
1fc40 62 6c 65 20 66 6f 72 20 64 65 6c 65 74 69 6e 67 ble for deleting
1fc50 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a the compiled.**
1fc60 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 75 SQL statement u
1fc70 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 69 sing [sqlite3_fi
1fc80 6e 61 6c 69 7a 65 28 29 5d 20 61 66 74 65 72 20 nalize()] after
1fc90 69 74 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 it has finished
1fca0 77 69 74 68 20 69 74 2e 0a 2a 2a 20 70 70 53 74 with it..** ppSt
1fcb0 6d 74 20 6d 61 79 20 6e 6f 74 20 62 65 20 4e 55 mt may not be NU
1fcc0 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 LL..**.** On suc
1fcd0 63 65 73 73 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b cess, [SQLITE_OK
1fce0 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 6f ] is returned, o
1fcf0 74 68 65 72 77 69 73 65 20 61 6e 20 5b 65 72 72 therwise an [err
1fd00 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 or code] is retu
1fd10 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 rned..**.** The
1fd20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f sqlite3_prepare_
1fd30 76 32 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 v2() and sqlite3
1fd40 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 20 _prepare16_v2()
1fd50 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 0a 2a interfaces are.*
1fd60 2a 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f * recommended fo
1fd70 72 20 61 6c 6c 20 6e 65 77 20 70 72 6f 67 72 61 r all new progra
1fd80 6d 73 2e 20 54 68 65 20 74 77 6f 20 6f 6c 64 65 ms. The two olde
1fd90 72 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 r interfaces are
1fda0 20 72 65 74 61 69 6e 65 64 0a 2a 2a 20 66 6f 72 retained.** for
1fdb0 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 backwards compa
1fdc0 74 69 62 69 6c 69 74 79 2c 20 62 75 74 20 74 68 tibility, but th
1fdd0 65 69 72 20 75 73 65 20 69 73 20 64 69 73 63 6f eir use is disco
1fde0 75 72 61 67 65 64 2e 0a 2a 2a 20 49 6e 20 74 68 uraged..** In th
1fdf0 65 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 e "v2" interface
1fe00 73 2c 20 74 68 65 20 70 72 65 70 61 72 65 64 20 s, the prepared
1fe10 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 68 61 statement.** tha
1fe20 74 20 69 73 20 72 65 74 75 72 6e 65 64 20 28 74 t is returned (t
1fe30 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 he [sqlite3_stmt
1fe40 5d 20 6f 62 6a 65 63 74 29 20 63 6f 6e 74 61 69 ] object) contai
1fe50 6e 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 ns a copy of the
1fe60 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c .** original SQL
1fe70 20 74 65 78 74 2e 20 54 68 69 73 20 63 61 75 73 text. This caus
1fe80 65 73 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f es the [sqlite3_
1fe90 73 74 65 70 28 29 5d 20 69 6e 74 65 72 66 61 63 step()] interfac
1fea0 65 20 74 6f 0a 2a 2a 20 62 65 68 61 76 65 20 61 e to.** behave a
1feb0 20 64 69 66 66 65 72 65 6e 74 6c 79 20 69 6e 20 differently in
1fec0 74 68 72 65 65 20 77 61 79 73 3a 0a 2a 2a 0a 2a three ways:.**.*
1fed0 2a 20 3c 6f 6c 3e 0a 2a 2a 20 3c 6c 69 3e 0a 2a * <ol>.** <li>.*
1fee0 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 * If the databas
1fef0 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73 e schema changes
1ff00 2c 20 69 6e 73 74 65 61 64 20 6f 66 20 72 65 74 , instead of ret
1ff10 75 72 6e 69 6e 67 20 5b 53 51 4c 49 54 45 5f 53 urning [SQLITE_S
1ff20 43 48 45 4d 41 5d 20 61 73 20 69 74 0a 2a 2a 20 CHEMA] as it.**
1ff30 61 6c 77 61 79 73 20 75 73 65 64 20 74 6f 20 64 always used to d
1ff40 6f 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 o, [sqlite3_step
1ff50 28 29 5d 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74 ()] will automat
1ff60 69 63 61 6c 6c 79 20 72 65 63 6f 6d 70 69 6c 65 ically recompile
1ff70 20 74 68 65 20 53 51 4c 0a 2a 2a 20 73 74 61 74 the SQL.** stat
1ff80 65 6d 65 6e 74 20 61 6e 64 20 74 72 79 20 74 6f ement and try to
1ff90 20 72 75 6e 20 69 74 20 61 67 61 69 6e 2e 20 20 run it again.
1ffa0 49 66 20 74 68 65 20 73 63 68 65 6d 61 20 68 61 If the schema ha
1ffb0 73 20 63 68 61 6e 67 65 64 20 69 6e 0a 2a 2a 20 s changed in.**
1ffc0 61 20 77 61 79 20 74 68 61 74 20 6d 61 6b 65 73 a way that makes
1ffd0 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6e the statement n
1ffe0 6f 20 6c 6f 6e 67 65 72 20 76 61 6c 69 64 2c 20 o longer valid,
1fff0 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d [sqlite3_step()]
20000 20 77 69 6c 6c 20 73 74 69 6c 6c 0a 2a 2a 20 72 will still.** r
20010 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 53 43 eturn [SQLITE_SC
20020 48 45 4d 41 5d 2e 20 20 42 75 74 20 75 6e 6c 69 HEMA]. But unli
20030 6b 65 20 74 68 65 20 6c 65 67 61 63 79 20 62 65 ke the legacy be
20040 68 61 76 69 6f 72 2c 20 5b 53 51 4c 49 54 45 5f havior, [SQLITE_
20050 53 43 48 45 4d 41 5d 20 69 73 0a 2a 2a 20 6e 6f SCHEMA] is.** no
20060 77 20 61 20 66 61 74 61 6c 20 65 72 72 6f 72 2e w a fatal error.
20070 20 20 43 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 Calling [sqlit
20080 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d e3_prepare_v2()]
20090 20 61 67 61 69 6e 20 77 69 6c 6c 20 6e 6f 74 20 again will not
200a0 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 65 72 72 6f make the.** erro
200b0 72 20 67 6f 20 61 77 61 79 2e 20 20 4e 6f 74 65 r go away. Note
200c0 3a 20 75 73 65 20 5b 73 71 6c 69 74 65 33 5f 65 : use [sqlite3_e
200d0 72 72 6d 73 67 28 29 5d 20 74 6f 20 66 69 6e 64 rrmsg()] to find
200e0 20 74 68 65 20 74 65 78 74 0a 2a 2a 20 6f 66 20 the text.** of
200f0 74 68 65 20 70 61 72 73 69 6e 67 20 65 72 72 6f the parsing erro
20100 72 20 74 68 61 74 20 72 65 73 75 6c 74 73 20 69 r that results i
20110 6e 20 61 6e 20 5b 53 51 4c 49 54 45 5f 53 43 48 n an [SQLITE_SCH
20120 45 4d 41 5d 20 72 65 74 75 72 6e 2e 0a 2a 2a 20 EMA] return..**
20130 3c 2f 6c 69 3e 0a 2a 2a 0a 2a 2a 20 3c 6c 69 3e </li>.**.** <li>
20140 0a 2a 2a 20 57 68 65 6e 20 61 6e 20 65 72 72 6f .** When an erro
20150 72 20 6f 63 63 75 72 73 2c 20 5b 73 71 6c 69 74 r occurs, [sqlit
20160 65 33 5f 73 74 65 70 28 29 5d 20 77 69 6c 6c 20 e3_step()] will
20170 72 65 74 75 72 6e 20 6f 6e 65 20 6f 66 20 74 68 return one of th
20180 65 20 64 65 74 61 69 6c 65 64 0a 2a 2a 20 5b 65 e detailed.** [e
20190 72 72 6f 72 20 63 6f 64 65 73 5d 20 6f 72 20 5b rror codes] or [
201a0 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 extended error c
201b0 6f 64 65 73 5d 2e 20 20 54 68 65 20 6c 65 67 61 odes]. The lega
201c0 63 79 20 62 65 68 61 76 69 6f 72 20 77 61 73 20 cy behavior was
201d0 74 68 61 74 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 that.** [sqlite3
201e0 5f 73 74 65 70 28 29 5d 20 77 6f 75 6c 64 20 6f _step()] would o
201f0 6e 6c 79 20 72 65 74 75 72 6e 20 61 20 67 65 6e nly return a gen
20200 65 72 69 63 20 5b 53 51 4c 49 54 45 5f 45 52 52 eric [SQLITE_ERR
20210 4f 52 5d 20 72 65 73 75 6c 74 20 63 6f 64 65 0a OR] result code.
20220 2a 2a 20 61 6e 64 20 79 6f 75 20 77 6f 75 6c 64 ** and you would
20230 20 68 61 76 65 20 74 6f 20 6d 61 6b 65 20 61 20 have to make a
20240 73 65 63 6f 6e 64 20 63 61 6c 6c 20 74 6f 20 5b second call to [
20250 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d sqlite3_reset()]
20260 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20 74 6f 20 in order.** to
20270 66 69 6e 64 20 74 68 65 20 75 6e 64 65 72 6c 79 find the underly
20280 69 6e 67 20 63 61 75 73 65 20 6f 66 20 74 68 65 ing cause of the
20290 20 70 72 6f 62 6c 65 6d 2e 20 57 69 74 68 20 74 problem. With t
202a0 68 65 20 22 76 32 22 20 70 72 65 70 61 72 65 0a he "v2" prepare.
202b0 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 2c 20 74 ** interfaces, t
202c0 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 72 65 he underlying re
202d0 61 73 6f 6e 20 66 6f 72 20 74 68 65 20 65 72 72 ason for the err
202e0 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 or is returned i
202f0 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 2a 2a 20 3c mmediately..** <
20300 2f 6c 69 3e 0a 2a 2a 0a 2a 2a 20 3c 6c 69 3e 0a /li>.**.** <li>.
20310 2a 2a 20 5e 49 66 20 74 68 65 20 76 61 6c 75 65 ** ^If the value
20320 20 6f 66 20 61 20 5b 70 61 72 61 6d 65 74 65 72 of a [parameter
20330 20 7c 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65 | host paramete
20340 72 5d 20 69 6e 20 74 68 65 20 57 48 45 52 45 20 r] in the WHERE
20350 63 6c 61 75 73 65 20 6d 69 67 68 74 0a 2a 2a 20 clause might.**
20360 63 68 61 6e 67 65 20 74 68 65 20 71 75 65 72 79 change the query
20370 20 70 6c 61 6e 20 66 6f 72 20 61 20 73 74 61 74 plan for a stat
20380 65 6d 65 6e 74 2c 20 74 68 65 6e 20 74 68 65 20 ement, then the
20390 73 74 61 74 65 6d 65 6e 74 20 6d 61 79 20 62 65 statement may be
203a0 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c .** automaticall
203b0 79 20 72 65 63 6f 6d 70 69 6c 65 64 20 28 61 73 y recompiled (as
203c0 20 69 66 20 74 68 65 72 65 20 68 61 64 20 62 65 if there had be
203d0 65 6e 20 61 20 73 63 68 65 6d 61 20 63 68 61 6e en a schema chan
203e0 67 65 29 20 6f 6e 20 74 68 65 20 66 69 72 73 74 ge) on the first
203f0 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 74 .** [sqlite3_st
20400 65 70 28 29 5d 20 63 61 6c 6c 20 66 6f 6c 6c 6f ep()] call follo
20410 77 69 6e 67 20 61 6e 79 20 63 68 61 6e 67 65 20 wing any change
20420 74 6f 20 74 68 65 20 0a 2a 2a 20 5b 73 71 6c 69 to the .** [sqli
20430 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 20 7c 20 te3_bind_text |
20440 62 69 6e 64 69 6e 67 73 5d 20 6f 66 20 74 68 65 bindings] of the
20450 20 5b 70 61 72 61 6d 65 74 65 72 5d 2e 20 0a 2a [parameter]. .*
20460 2a 20 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 6f 6c 3e * </li>.** </ol>
20470 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
20480 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 30 31 31 5d nts:.** [H13011]
20490 20 5b 48 31 33 30 31 32 5d 20 5b 48 31 33 30 31 [H13012] [H1301
204a0 33 5d 20 5b 48 31 33 30 31 34 5d 20 5b 48 31 33 3] [H13014] [H13
204b0 30 31 35 5d 20 5b 48 31 33 30 31 36 5d 20 5b 48 015] [H13016] [H
204c0 31 33 30 31 39 5d 20 5b 48 31 33 30 32 31 5d 0a 13019] [H13021].
204d0 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 **.*/.SQLITE_API
204e0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65 int sqlite3_pre
204f0 70 61 72 65 28 0a 20 20 73 71 6c 69 74 65 33 20 pare(. sqlite3
20500 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 *db,
20510 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 /* Database hand
20520 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 le */. const ch
20530 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 ar *zSql,
20540 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 /* SQL statement
20550 2c 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 , UTF-8 encoded
20560 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 */. int nByte,
20570 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
20580 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f Maximum length o
20590 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e f zSql in bytes.
205a0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 */. sqlite3_st
205b0 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a mt **ppStmt, /*
205c0 20 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 OUT: Statement
205d0 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 handle */. cons
205e0 74 20 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20 t char **pzTail
205f0 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e /* OUT: Poin
20600 74 65 72 20 74 6f 20 75 6e 75 73 65 64 20 70 6f ter to unused po
20610 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f rtion of zSql */
20620 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .);.SQLITE_API i
20630 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 nt sqlite3_prepa
20640 72 65 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33 re_v2(. sqlite3
20650 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 *db,
20660 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e /* Database han
20670 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 dle */. const c
20680 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 har *zSql,
20690 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e /* SQL statemen
206a0 74 2c 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 t, UTF-8 encoded
206b0 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c */. int nByte,
206c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
206d0 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 Maximum length
206e0 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 of zSql in bytes
206f0 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 . */. sqlite3_s
20700 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 2f tmt **ppStmt, /
20710 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e 74 * OUT: Statement
20720 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e handle */. con
20730 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c st char **pzTail
20740 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 /* OUT: Poi
20750 6e 74 65 72 20 74 6f 20 75 6e 75 73 65 64 20 70 nter to unused p
20760 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a ortion of zSql *
20770 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 /.);.SQLITE_API
20780 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70 int sqlite3_prep
20790 61 72 65 31 36 28 0a 20 20 73 71 6c 69 74 65 33 are16(. sqlite3
207a0 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 *db,
207b0 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e /* Database han
207c0 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 dle */. const v
207d0 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 oid *zSql,
207e0 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e /* SQL statemen
207f0 74 2c 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 t, UTF-16 encode
20800 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 d */. int nByte
20810 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
20820 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 * Maximum length
20830 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 of zSql in byte
20840 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f s. */. sqlite3_
20850 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 stmt **ppStmt,
20860 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e /* OUT: Statemen
20870 74 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f t handle */. co
20880 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69 nst void **pzTai
20890 6c 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f l /* OUT: Po
208a0 69 6e 74 65 72 20 74 6f 20 75 6e 75 73 65 64 20 inter to unused
208b0 70 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 portion of zSql
208c0 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 */.);.SQLITE_API
208d0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65 int sqlite3_pre
208e0 70 61 72 65 31 36 5f 76 32 28 0a 20 20 73 71 6c pare16_v2(. sql
208f0 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 ite3 *db,
20900 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 /* Database
20910 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e handle */. con
20920 73 74 20 76 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 st void *zSql,
20930 20 20 20 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 /* SQL stat
20940 65 6d 65 6e 74 2c 20 55 54 46 2d 31 36 20 65 6e ement, UTF-16 en
20950 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e coded */. int n
20960 42 79 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 Byte,
20970 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 /* Maximum le
20980 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 ngth of zSql in
20990 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 bytes. */. sqli
209a0 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d te3_stmt **ppStm
209b0 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61 74 t, /* OUT: Stat
209c0 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 2a 2f 0a ement handle */.
209d0 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2a 70 const void **p
209e0 7a 54 61 69 6c 20 20 20 20 20 2f 2a 20 4f 55 54 zTail /* OUT
209f0 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20 75 6e 75 : Pointer to unu
20a00 73 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 7a sed portion of z
20a10 53 71 6c 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a Sql */.);../*.**
20a20 20 43 41 50 49 33 52 45 46 3a 20 52 65 74 72 69 CAPI3REF: Retri
20a30 65 76 69 6e 67 20 53 74 61 74 65 6d 65 6e 74 20 eving Statement
20a40 53 51 4c 20 7b 48 31 33 31 30 30 7d 20 3c 48 31 SQL {H13100} <H1
20a50 33 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 3000>.**.** This
20a60 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 62 interface can b
20a70 65 20 75 73 65 64 20 74 6f 20 72 65 74 72 69 65 e used to retrie
20a80 76 65 20 61 20 73 61 76 65 64 20 63 6f 70 79 20 ve a saved copy
20a90 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 0a of the original.
20aa0 2a 2a 20 53 51 4c 20 74 65 78 74 20 75 73 65 64 ** SQL text used
20ab0 20 74 6f 20 63 72 65 61 74 65 20 61 20 5b 70 72 to create a [pr
20ac0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
20ad0 5d 20 69 66 20 74 68 61 74 20 73 74 61 74 65 6d ] if that statem
20ae0 65 6e 74 20 77 61 73 0a 2a 2a 20 63 6f 6d 70 69 ent was.** compi
20af0 6c 65 64 20 75 73 69 6e 67 20 65 69 74 68 65 72 led using either
20b00 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 [sqlite3_prepar
20b10 65 5f 76 32 28 29 5d 20 6f 72 20 5b 73 71 6c 69 e_v2()] or [sqli
20b20 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 te3_prepare16_v2
20b30 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 ()]..**.** Requi
20b40 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 rements:.** [H13
20b50 31 30 31 5d 20 5b 48 31 33 31 30 32 5d 20 5b 48 101] [H13102] [H
20b60 31 33 31 30 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 13103].*/.SQLITE
20b70 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 _API const char
20b80 2a 73 71 6c 69 74 65 33 5f 73 71 6c 28 73 71 6c *sqlite3_sql(sql
20b90 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
20ba0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
20bb0 45 46 3a 20 44 79 6e 61 6d 69 63 61 6c 6c 79 20 EF: Dynamically
20bc0 54 79 70 65 64 20 56 61 6c 75 65 20 4f 62 6a 65 Typed Value Obje
20bd0 63 74 20 7b 48 31 35 30 30 30 7d 20 3c 53 32 30 ct {H15000} <S20
20be0 32 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 200>.** KEYWORDS
20bf0 3a 20 7b 70 72 6f 74 65 63 74 65 64 20 73 71 6c : {protected sql
20c00 69 74 65 33 5f 76 61 6c 75 65 7d 20 7b 75 6e 70 ite3_value} {unp
20c10 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 rotected sqlite3
20c20 5f 76 61 6c 75 65 7d 0a 2a 2a 0a 2a 2a 20 53 51 _value}.**.** SQ
20c30 4c 69 74 65 20 75 73 65 73 20 74 68 65 20 73 71 Lite uses the sq
20c40 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 lite3_value obje
20c50 63 74 20 74 6f 20 72 65 70 72 65 73 65 6e 74 20 ct to represent
20c60 61 6c 6c 20 76 61 6c 75 65 73 0a 2a 2a 20 74 68 all values.** th
20c70 61 74 20 63 61 6e 20 62 65 20 73 74 6f 72 65 64 at can be stored
20c80 20 69 6e 20 61 20 64 61 74 61 62 61 73 65 20 74 in a database t
20c90 61 62 6c 65 2e 20 53 51 4c 69 74 65 20 75 73 65 able. SQLite use
20ca0 73 20 64 79 6e 61 6d 69 63 20 74 79 70 69 6e 67 s dynamic typing
20cb0 0a 2a 2a 20 66 6f 72 20 74 68 65 20 76 61 6c 75 .** for the valu
20cc0 65 73 20 69 74 20 73 74 6f 72 65 73 2e 20 56 61 es it stores. Va
20cd0 6c 75 65 73 20 73 74 6f 72 65 64 20 69 6e 20 73 lues stored in s
20ce0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a qlite3_value obj
20cf0 65 63 74 73 0a 2a 2a 20 63 61 6e 20 62 65 20 69 ects.** can be i
20d00 6e 74 65 67 65 72 73 2c 20 66 6c 6f 61 74 69 6e ntegers, floatin
20d10 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 73 2c 20 g point values,
20d20 73 74 72 69 6e 67 73 2c 20 42 4c 4f 42 73 2c 20 strings, BLOBs,
20d30 6f 72 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 41 or NULL..**.** A
20d40 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 n sqlite3_value
20d50 6f 62 6a 65 63 74 20 6d 61 79 20 62 65 20 65 69 object may be ei
20d60 74 68 65 72 20 22 70 72 6f 74 65 63 74 65 64 22 ther "protected"
20d70 20 6f 72 20 22 75 6e 70 72 6f 74 65 63 74 65 64 or "unprotected
20d80 22 2e 0a 2a 2a 20 53 6f 6d 65 20 69 6e 74 65 72 "..** Some inter
20d90 66 61 63 65 73 20 72 65 71 75 69 72 65 20 61 20 faces require a
20da0 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 protected sqlite
20db0 33 5f 76 61 6c 75 65 2e 20 20 4f 74 68 65 72 20 3_value. Other
20dc0 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 77 69 interfaces.** wi
20dd0 6c 6c 20 61 63 63 65 70 74 20 65 69 74 68 65 72 ll accept either
20de0 20 61 20 70 72 6f 74 65 63 74 65 64 20 6f 72 20 a protected or
20df0 61 6e 20 75 6e 70 72 6f 74 65 63 74 65 64 20 73 an unprotected s
20e00 71 6c 69 74 65 33 5f 76 61 6c 75 65 2e 0a 2a 2a qlite3_value..**
20e10 20 45 76 65 72 79 20 69 6e 74 65 72 66 61 63 65 Every interface
20e20 20 74 68 61 74 20 61 63 63 65 70 74 73 20 73 71 that accepts sq
20e30 6c 69 74 65 33 5f 76 61 6c 75 65 20 61 72 67 75 lite3_value argu
20e40 6d 65 6e 74 73 20 73 70 65 63 69 66 69 65 73 0a ments specifies.
20e50 2a 2a 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f ** whether or no
20e60 74 20 69 74 20 72 65 71 75 69 72 65 73 20 61 20 t it requires a
20e70 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 protected sqlite
20e80 33 5f 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 3_value..**.** T
20e90 68 65 20 74 65 72 6d 73 20 22 70 72 6f 74 65 63 he terms "protec
20ea0 74 65 64 22 20 61 6e 64 20 22 75 6e 70 72 6f 74 ted" and "unprot
20eb0 65 63 74 65 64 22 20 72 65 66 65 72 20 74 6f 20 ected" refer to
20ec0 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 0a 2a whether or not.*
20ed0 2a 20 61 20 6d 75 74 65 78 20 69 73 20 68 65 6c * a mutex is hel
20ee0 64 2e 20 20 41 20 69 6e 74 65 72 6e 61 6c 20 6d d. A internal m
20ef0 75 74 65 78 20 69 73 20 68 65 6c 64 20 66 6f 72 utex is held for
20f00 20 61 20 70 72 6f 74 65 63 74 65 64 0a 2a 2a 20 a protected.**
20f10 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 sqlite3_value ob
20f20 6a 65 63 74 20 62 75 74 20 6e 6f 20 6d 75 74 65 ject but no mute
20f30 78 20 69 73 20 68 65 6c 64 20 66 6f 72 20 61 6e x is held for an
20f40 20 75 6e 70 72 6f 74 65 63 74 65 64 0a 2a 2a 20 unprotected.**
20f50 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 sqlite3_value ob
20f60 6a 65 63 74 2e 20 20 49 66 20 53 51 4c 69 74 65 ject. If SQLite
20f70 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 74 6f 20 is compiled to
20f80 62 65 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 be single-thread
20f90 65 64 0a 2a 2a 20 28 77 69 74 68 20 5b 53 51 4c ed.** (with [SQL
20fa0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 30 ITE_THREADSAFE=0
20fb0 5d 20 61 6e 64 20 77 69 74 68 20 5b 73 71 6c 69 ] and with [sqli
20fc0 74 65 33 5f 74 68 72 65 61 64 73 61 66 65 28 29 te3_threadsafe()
20fd0 5d 20 72 65 74 75 72 6e 69 6e 67 20 30 29 0a 2a ] returning 0).*
20fe0 2a 20 6f 72 20 69 66 20 53 51 4c 69 74 65 20 69 * or if SQLite i
20ff0 73 20 72 75 6e 20 69 6e 20 6f 6e 65 20 6f 66 20 s run in one of
21000 72 65 64 75 63 65 64 20 6d 75 74 65 78 20 6d 6f reduced mutex mo
21010 64 65 73 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f des .** [SQLITE_
21020 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 CONFIG_SINGLETHR
21030 45 41 44 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f EAD] or [SQLITE_
21040 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 CONFIG_MULTITHRE
21050 41 44 5d 0a 2a 2a 20 74 68 65 6e 20 74 68 65 72 AD].** then ther
21060 65 20 69 73 20 6e 6f 20 64 69 73 74 69 6e 63 74 e is no distinct
21070 69 6f 6e 20 62 65 74 77 65 65 6e 20 70 72 6f 74 ion between prot
21080 65 63 74 65 64 20 61 6e 64 20 75 6e 70 72 6f 74 ected and unprot
21090 65 63 74 65 64 0a 2a 2a 20 73 71 6c 69 74 65 33 ected.** sqlite3
210a0 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 20 61 _value objects a
210b0 6e 64 20 74 68 65 79 20 63 61 6e 20 62 65 20 75 nd they can be u
210c0 73 65 64 20 69 6e 74 65 72 63 68 61 6e 67 65 61 sed interchangea
210d0 62 6c 79 2e 20 20 48 6f 77 65 76 65 72 2c 0a 2a bly. However,.*
210e0 2a 20 66 6f 72 20 6d 61 78 69 6d 75 6d 20 63 6f * for maximum co
210f0 64 65 20 70 6f 72 74 61 62 69 6c 69 74 79 20 69 de portability i
21100 74 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64 t is recommended
21110 20 74 68 61 74 20 61 70 70 6c 69 63 61 74 69 6f that applicatio
21120 6e 73 0a 2a 2a 20 73 74 69 6c 6c 20 6d 61 6b 65 ns.** still make
21130 20 74 68 65 20 64 69 73 74 69 6e 63 74 69 6f 6e the distinction
21140 20 62 65 74 77 65 65 6e 20 62 65 74 77 65 65 6e between between
21150 20 70 72 6f 74 65 63 74 65 64 20 61 6e 64 20 75 protected and u
21160 6e 70 72 6f 74 65 63 74 65 64 0a 2a 2a 20 73 71 nprotected.** sq
21170 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 lite3_value obje
21180 63 74 73 20 65 76 65 6e 20 77 68 65 6e 20 6e 6f cts even when no
21190 74 20 73 74 72 69 63 74 6c 79 20 72 65 71 75 69 t strictly requi
211a0 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 red..**.** The s
211b0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a qlite3_value obj
211c0 65 63 74 73 20 74 68 61 74 20 61 72 65 20 70 61 ects that are pa
211d0 73 73 65 64 20 61 73 20 70 61 72 61 6d 65 74 65 ssed as paramete
211e0 72 73 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 69 rs into the.** i
211f0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
21200 20 5b 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 [application-de
21210 66 69 6e 65 64 20 53 51 4c 20 66 75 6e 63 74 69 fined SQL functi
21220 6f 6e 73 5d 20 61 72 65 20 70 72 6f 74 65 63 74 ons] are protect
21230 65 64 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 ed..** The sqlit
21240 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 20 e3_value object
21250 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 5b returned by.** [
21260 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 sqlite3_column_v
21270 61 6c 75 65 28 29 5d 20 69 73 20 75 6e 70 72 6f alue()] is unpro
21280 74 65 63 74 65 64 2e 0a 2a 2a 20 55 6e 70 72 6f tected..** Unpro
21290 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 tected sqlite3_v
212a0 61 6c 75 65 20 6f 62 6a 65 63 74 73 20 6d 61 79 alue objects may
212b0 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 77 69 only be used wi
212c0 74 68 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72 th.** [sqlite3_r
212d0 65 73 75 6c 74 5f 76 61 6c 75 65 28 29 5d 20 61 esult_value()] a
212e0 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 nd [sqlite3_bind
212f0 5f 76 61 6c 75 65 28 29 5d 2e 0a 2a 2a 20 54 68 _value()]..** Th
21300 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 e [sqlite3_value
21310 5f 62 6c 6f 62 20 7c 20 73 71 6c 69 74 65 33 5f _blob | sqlite3_
21320 76 61 6c 75 65 5f 74 79 70 65 28 29 5d 20 66 61 value_type()] fa
21330 6d 69 6c 79 20 6f 66 0a 2a 2a 20 69 6e 74 65 72 mily of.** inter
21340 66 61 63 65 73 20 72 65 71 75 69 72 65 20 70 72 faces require pr
21350 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f otected sqlite3_
21360 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 2e 0a 2a value objects..*
21370 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
21380 20 4d 65 6d 20 73 71 6c 69 74 65 33 5f 76 61 6c Mem sqlite3_val
21390 75 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 ue;../*.** CAPI3
213a0 52 45 46 3a 20 53 51 4c 20 46 75 6e 63 74 69 6f REF: SQL Functio
213b0 6e 20 43 6f 6e 74 65 78 74 20 4f 62 6a 65 63 74 n Context Object
213c0 20 7b 48 31 36 30 30 31 7d 20 3c 53 32 30 32 30 {H16001} <S2020
213d0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 0>.**.** The con
213e0 74 65 78 74 20 69 6e 20 77 68 69 63 68 20 61 6e text in which an
213f0 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 65 78 SQL function ex
21400 65 63 75 74 65 73 20 69 73 20 73 74 6f 72 65 64 ecutes is stored
21410 20 69 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 74 65 in an.** sqlite
21420 33 5f 63 6f 6e 74 65 78 74 20 6f 62 6a 65 63 74 3_context object
21430 2e 20 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 . A pointer to
21440 61 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 an sqlite3_conte
21450 78 74 20 6f 62 6a 65 63 74 0a 2a 2a 20 69 73 20 xt object.** is
21460 61 6c 77 61 79 73 20 66 69 72 73 74 20 70 61 72 always first par
21470 61 6d 65 74 65 72 20 74 6f 20 5b 61 70 70 6c 69 ameter to [appli
21480 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 cation-defined S
21490 51 4c 20 66 75 6e 63 74 69 6f 6e 73 5d 2e 0a 2a QL functions]..*
214a0 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f * The applicatio
214b0 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66 75 n-defined SQL fu
214c0 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 nction implement
214d0 61 74 69 6f 6e 20 77 69 6c 6c 20 70 61 73 73 20 ation will pass
214e0 74 68 69 73 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 this.** pointer
214f0 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 63 61 6c through into cal
21500 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 ls to [sqlite3_r
21510 65 73 75 6c 74 5f 69 6e 74 20 7c 20 73 71 6c 69 esult_int | sqli
21520 74 65 33 5f 72 65 73 75 6c 74 28 29 5d 2c 0a 2a te3_result()],.*
21530 2a 20 5b 73 71 6c 69 74 65 33 5f 61 67 67 72 65 * [sqlite3_aggre
21540 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 29 5d 2c gate_context()],
21550 20 5b 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 [sqlite3_user_d
21560 61 74 61 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 ata()],.** [sqli
21570 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 te3_context_db_h
21580 61 6e 64 6c 65 28 29 5d 2c 20 5b 73 71 6c 69 74 andle()], [sqlit
21590 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28 29 e3_get_auxdata()
215a0 5d 2c 0a 2a 2a 20 61 6e 64 2f 6f 72 20 5b 73 71 ],.** and/or [sq
215b0 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 lite3_set_auxdat
215c0 61 28 29 5d 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 a()]..*/.typedef
215d0 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f struct sqlite3_
215e0 63 6f 6e 74 65 78 74 20 73 71 6c 69 74 65 33 5f context sqlite3_
215f0 63 6f 6e 74 65 78 74 3b 0a 0a 2f 2a 0a 2a 2a 20 context;../*.**
21600 43 41 50 49 33 52 45 46 3a 20 42 69 6e 64 69 6e CAPI3REF: Bindin
21610 67 20 56 61 6c 75 65 73 20 54 6f 20 50 72 65 70 g Values To Prep
21620 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 73 20 ared Statements
21630 7b 48 31 33 35 30 30 7d 20 3c 53 37 30 33 30 30 {H13500} <S70300
21640 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b >.** KEYWORDS: {
21650 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 7d 20 host parameter}
21660 7b 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 73 {host parameters
21670 7d 20 7b 68 6f 73 74 20 70 61 72 61 6d 65 74 65 } {host paramete
21680 72 20 6e 61 6d 65 7d 0a 2a 2a 20 4b 45 59 57 4f r name}.** KEYWO
21690 52 44 53 3a 20 7b 53 51 4c 20 70 61 72 61 6d 65 RDS: {SQL parame
216a0 74 65 72 7d 20 7b 53 51 4c 20 70 61 72 61 6d 65 ter} {SQL parame
216b0 74 65 72 73 7d 20 7b 70 61 72 61 6d 65 74 65 72 ters} {parameter
216c0 20 62 69 6e 64 69 6e 67 7d 0a 2a 2a 0a 2a 2a 20 binding}.**.**
216d0 49 6e 20 74 68 65 20 53 51 4c 20 73 74 72 69 6e In the SQL strin
216e0 67 73 20 69 6e 70 75 74 20 74 6f 20 5b 73 71 6c gs input to [sql
216f0 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 ite3_prepare_v2(
21700 29 5d 20 61 6e 64 20 69 74 73 20 76 61 72 69 61 )] and its varia
21710 6e 74 73 2c 0a 2a 2a 20 6c 69 74 65 72 61 6c 73 nts,.** literals
21720 20 6d 61 79 20 62 65 20 72 65 70 6c 61 63 65 64 may be replaced
21730 20 62 79 20 61 20 5b 70 61 72 61 6d 65 74 65 72 by a [parameter
21740 5d 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 6f ] that matches o
21750 6e 65 20 6f 66 20 66 6f 6c 6c 6f 77 69 6e 67 0a ne of following.
21760 2a 2a 20 74 65 6d 70 6c 61 74 65 73 3a 0a 2a 2a ** templates:.**
21770 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e .** <ul>.** <li>
21780 20 20 3f 0a 2a 2a 20 3c 6c 69 3e 20 20 3f 4e 4e ?.** <li> ?NN
21790 4e 0a 2a 2a 20 3c 6c 69 3e 20 20 3a 56 56 56 0a N.** <li> :VVV.
217a0 2a 2a 20 3c 6c 69 3e 20 20 40 56 56 56 0a 2a 2a ** <li> @VVV.**
217b0 20 3c 6c 69 3e 20 20 24 56 56 56 0a 2a 2a 20 3c <li> $VVV.** <
217c0 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 /ul>.**.** In th
217d0 65 20 74 65 6d 70 6c 61 74 65 73 20 61 62 6f 76 e templates abov
217e0 65 2c 20 4e 4e 4e 20 72 65 70 72 65 73 65 6e 74 e, NNN represent
217f0 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6c 69 74 s an integer lit
21800 65 72 61 6c 2c 0a 2a 2a 20 61 6e 64 20 56 56 56 eral,.** and VVV
21810 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20 61 represents an a
21820 6c 70 68 61 6e 75 6d 65 72 69 63 20 69 64 65 6e lphanumeric iden
21830 74 69 66 65 72 2e 20 20 54 68 65 20 76 61 6c 75 tifer. The valu
21840 65 73 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20 70 es of these.** p
21850 61 72 61 6d 65 74 65 72 73 20 28 61 6c 73 6f 20 arameters (also
21860 63 61 6c 6c 65 64 20 22 68 6f 73 74 20 70 61 72 called "host par
21870 61 6d 65 74 65 72 20 6e 61 6d 65 73 22 20 6f 72 ameter names" or
21880 20 22 53 51 4c 20 70 61 72 61 6d 65 74 65 72 73 "SQL parameters
21890 22 29 0a 2a 2a 20 63 61 6e 20 62 65 20 73 65 74 ").** can be set
218a0 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74 using the sqlit
218b0 65 33 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74 e3_bind_*() rout
218c0 69 6e 65 73 20 64 65 66 69 6e 65 64 20 68 65 72 ines defined her
218d0 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 e..**.** The fir
218e0 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 st argument to t
218f0 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f he sqlite3_bind_
21900 2a 28 29 20 72 6f 75 74 69 6e 65 73 20 69 73 20 *() routines is
21910 61 6c 77 61 79 73 0a 2a 2a 20 61 20 70 6f 69 6e always.** a poin
21920 74 65 72 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 ter to the [sqli
21930 74 65 33 5f 73 74 6d 74 5d 20 6f 62 6a 65 63 74 te3_stmt] object
21940 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 0a 2a returned from.*
21950 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 * [sqlite3_prepa
21960 72 65 5f 76 32 28 29 5d 20 6f 72 20 69 74 73 20 re_v2()] or its
21970 76 61 72 69 61 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 variants..**.**
21980 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d The second argum
21990 65 6e 74 20 69 73 20 74 68 65 20 69 6e 64 65 78 ent is the index
219a0 20 6f 66 20 74 68 65 20 53 51 4c 20 70 61 72 61 of the SQL para
219b0 6d 65 74 65 72 20 74 6f 20 62 65 20 73 65 74 2e meter to be set.
219c0 0a 2a 2a 20 54 68 65 20 6c 65 66 74 6d 6f 73 74 .** The leftmost
219d0 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72 20 68 SQL parameter h
219e0 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 31 as an index of 1
219f0 2e 20 20 57 68 65 6e 20 74 68 65 20 73 61 6d 65 . When the same
21a00 20 6e 61 6d 65 64 0a 2a 2a 20 53 51 4c 20 70 61 named.** SQL pa
21a10 72 61 6d 65 74 65 72 20 69 73 20 75 73 65 64 20 rameter is used
21a20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 more than once,
21a30 73 65 63 6f 6e 64 20 61 6e 64 20 73 75 62 73 65 second and subse
21a40 71 75 65 6e 74 0a 2a 2a 20 6f 63 63 75 72 72 65 quent.** occurre
21a50 6e 63 65 73 20 68 61 76 65 20 74 68 65 20 73 61 nces have the sa
21a60 6d 65 20 69 6e 64 65 78 20 61 73 20 74 68 65 20 me index as the
21a70 66 69 72 73 74 20 6f 63 63 75 72 72 65 6e 63 65 first occurrence
21a80 2e 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 66 ..** The index f
21a90 6f 72 20 6e 61 6d 65 64 20 70 61 72 61 6d 65 74 or named paramet
21aa0 65 72 73 20 63 61 6e 20 62 65 20 6c 6f 6f 6b 65 ers can be looke
21ab0 64 20 75 70 20 75 73 69 6e 67 20 74 68 65 0a 2a d up using the.*
21ac0 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f * [sqlite3_bind_
21ad0 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 parameter_index(
21ae0 29 5d 20 41 50 49 20 69 66 20 64 65 73 69 72 65 )] API if desire
21af0 64 2e 20 20 54 68 65 20 69 6e 64 65 78 0a 2a 2a d. The index.**
21b00 20 66 6f 72 20 22 3f 4e 4e 4e 22 20 70 61 72 61 for "?NNN" para
21b10 6d 65 74 65 72 73 20 69 73 20 74 68 65 20 76 61 meters is the va
21b20 6c 75 65 20 6f 66 20 4e 4e 4e 2e 0a 2a 2a 20 54 lue of NNN..** T
21b30 68 65 20 4e 4e 4e 20 76 61 6c 75 65 20 6d 75 73 he NNN value mus
21b40 74 20 62 65 20 62 65 74 77 65 65 6e 20 31 20 61 t be between 1 a
21b50 6e 64 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f nd the [sqlite3_
21b60 6c 69 6d 69 74 28 29 5d 0a 2a 2a 20 70 61 72 61 limit()].** para
21b70 6d 65 74 65 72 20 5b 53 51 4c 49 54 45 5f 4c 49 meter [SQLITE_LI
21b80 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d MIT_VARIABLE_NUM
21b90 42 45 52 5d 20 28 64 65 66 61 75 6c 74 20 76 61 BER] (default va
21ba0 6c 75 65 3a 20 39 39 39 29 2e 0a 2a 2a 0a 2a 2a lue: 999)..**.**
21bb0 20 54 68 65 20 74 68 69 72 64 20 61 72 67 75 6d The third argum
21bc0 65 6e 74 20 69 73 20 74 68 65 20 76 61 6c 75 65 ent is the value
21bd0 20 74 6f 20 62 69 6e 64 20 74 6f 20 74 68 65 20 to bind to the
21be0 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a parameter..**.**
21bf0 20 49 6e 20 74 68 6f 73 65 20 72 6f 75 74 69 6e In those routin
21c00 65 73 20 74 68 61 74 20 68 61 76 65 20 61 20 66 es that have a f
21c10 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74 2c 20 ourth argument,
21c20 69 74 73 20 76 61 6c 75 65 20 69 73 20 74 68 65 its value is the
21c30 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 .** number of by
21c40 74 65 73 20 69 6e 20 74 68 65 20 70 61 72 61 6d tes in the param
21c50 65 74 65 72 2e 20 20 54 6f 20 62 65 20 63 6c 65 eter. To be cle
21c60 61 72 3a 20 74 68 65 20 76 61 6c 75 65 20 69 73 ar: the value is
21c70 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f the.** number o
21c80 66 20 3c 75 3e 62 79 74 65 73 3c 2f 75 3e 20 69 f <u>bytes</u> i
21c90 6e 20 74 68 65 20 76 61 6c 75 65 2c 20 6e 6f 74 n the value, not
21ca0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 the number of c
21cb0 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 20 49 66 haracters..** If
21cc0 20 74 68 65 20 66 6f 75 72 74 68 20 70 61 72 61 the fourth para
21cd0 6d 65 74 65 72 20 69 73 20 6e 65 67 61 74 69 76 meter is negativ
21ce0 65 2c 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 e, the length of
21cf0 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 0a 2a the string is.*
21d00 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 * the number of
21d10 62 79 74 65 73 20 75 70 20 74 6f 20 74 68 65 20 bytes up to the
21d20 66 69 72 73 74 20 7a 65 72 6f 20 74 65 72 6d 69 first zero termi
21d30 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 nator..**.** The
21d40 20 66 69 66 74 68 20 61 72 67 75 6d 65 6e 74 20 fifth argument
21d50 74 6f 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f to sqlite3_bind_
21d60 62 6c 6f 62 28 29 2c 20 73 71 6c 69 74 65 33 5f blob(), sqlite3_
21d70 62 69 6e 64 5f 74 65 78 74 28 29 2c 20 61 6e 64 bind_text(), and
21d80 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 .** sqlite3_bind
21d90 5f 74 65 78 74 31 36 28 29 20 69 73 20 61 20 64 _text16() is a d
21da0 65 73 74 72 75 63 74 6f 72 20 75 73 65 64 20 74 estructor used t
21db0 6f 20 64 69 73 70 6f 73 65 20 6f 66 20 74 68 65 o dispose of the
21dc0 20 42 4c 4f 42 20 6f 72 0a 2a 2a 20 73 74 72 69 BLOB or.** stri
21dd0 6e 67 20 61 66 74 65 72 20 53 51 4c 69 74 65 20 ng after SQLite
21de0 68 61 73 20 66 69 6e 69 73 68 65 64 20 77 69 74 has finished wit
21df0 68 20 69 74 2e 20 49 66 20 74 68 65 20 66 69 66 h it. If the fif
21e00 74 68 20 61 72 67 75 6d 65 6e 74 20 69 73 0a 2a th argument is.*
21e10 2a 20 74 68 65 20 73 70 65 63 69 61 6c 20 76 61 * the special va
21e20 6c 75 65 20 5b 53 51 4c 49 54 45 5f 53 54 41 54 lue [SQLITE_STAT
21e30 49 43 5d 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 IC], then SQLite
21e40 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 assumes that th
21e50 65 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e e.** information
21e60 20 69 73 20 69 6e 20 73 74 61 74 69 63 2c 20 75 is in static, u
21e70 6e 6d 61 6e 61 67 65 64 20 73 70 61 63 65 20 61 nmanaged space a
21e80 6e 64 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 nd does not need
21e90 20 74 6f 20 62 65 20 66 72 65 65 64 2e 0a 2a 2a to be freed..**
21ea0 20 49 66 20 74 68 65 20 66 69 66 74 68 20 61 72 If the fifth ar
21eb0 67 75 6d 65 6e 74 20 68 61 73 20 74 68 65 20 76 gument has the v
21ec0 61 6c 75 65 20 5b 53 51 4c 49 54 45 5f 54 52 41 alue [SQLITE_TRA
21ed0 4e 53 49 45 4e 54 5d 2c 20 74 68 65 6e 0a 2a 2a NSIENT], then.**
21ee0 20 53 51 4c 69 74 65 20 6d 61 6b 65 73 20 69 74 SQLite makes it
21ef0 73 20 6f 77 6e 20 70 72 69 76 61 74 65 20 63 6f s own private co
21f00 70 79 20 6f 66 20 74 68 65 20 64 61 74 61 20 69 py of the data i
21f10 6d 6d 65 64 69 61 74 65 6c 79 2c 20 62 65 66 6f mmediately, befo
21f20 72 65 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 re.** the sqlite
21f30 33 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74 69 3_bind_*() routi
21f40 6e 65 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a ne returns..**.*
21f50 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62 69 * The sqlite3_bi
21f60 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 29 20 72 6f nd_zeroblob() ro
21f70 75 74 69 6e 65 20 62 69 6e 64 73 20 61 20 42 4c utine binds a BL
21f80 4f 42 20 6f 66 20 6c 65 6e 67 74 68 20 4e 20 74 OB of length N t
21f90 68 61 74 0a 2a 2a 20 69 73 20 66 69 6c 6c 65 64 hat.** is filled
21fa0 20 77 69 74 68 20 7a 65 72 6f 65 73 2e 20 20 41 with zeroes. A
21fb0 20 7a 65 72 6f 62 6c 6f 62 20 75 73 65 73 20 61 zeroblob uses a
21fc0 20 66 69 78 65 64 20 61 6d 6f 75 6e 74 20 6f 66 fixed amount of
21fd0 20 6d 65 6d 6f 72 79 0a 2a 2a 20 28 6a 75 73 74 memory.** (just
21fe0 20 61 6e 20 69 6e 74 65 67 65 72 20 74 6f 20 68 an integer to h
21ff0 6f 6c 64 20 69 74 73 20 73 69 7a 65 29 20 77 68 old its size) wh
22000 69 6c 65 20 69 74 20 69 73 20 62 65 69 6e 67 20 ile it is being
22010 70 72 6f 63 65 73 73 65 64 2e 0a 2a 2a 20 5a 65 processed..** Ze
22020 72 6f 62 6c 6f 62 73 20 61 72 65 20 69 6e 74 65 roblobs are inte
22030 6e 64 65 64 20 74 6f 20 73 65 72 76 65 20 61 73 nded to serve as
22040 20 70 6c 61 63 65 68 6f 6c 64 65 72 73 20 66 6f placeholders fo
22050 72 20 42 4c 4f 42 73 20 77 68 6f 73 65 0a 2a 2a r BLOBs whose.**
22060 20 63 6f 6e 74 65 6e 74 20 69 73 20 6c 61 74 65 content is late
22070 72 20 77 72 69 74 74 65 6e 20 75 73 69 6e 67 0a r written using.
22080 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 ** [sqlite3_blob
22090 5f 6f 70 65 6e 20 7c 20 69 6e 63 72 65 6d 65 6e _open | incremen
220a0 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 5d 20 72 6f tal BLOB I/O] ro
220b0 75 74 69 6e 65 73 2e 0a 2a 2a 20 41 20 6e 65 67 utines..** A neg
220c0 61 74 69 76 65 20 76 61 6c 75 65 20 66 6f 72 20 ative value for
220d0 74 68 65 20 7a 65 72 6f 62 6c 6f 62 20 72 65 73 the zeroblob res
220e0 75 6c 74 73 20 69 6e 20 61 20 7a 65 72 6f 2d 6c ults in a zero-l
220f0 65 6e 67 74 68 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a ength BLOB..**.*
22100 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62 69 * The sqlite3_bi
22110 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 20 nd_*() routines
22120 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 61 must be called a
22130 66 74 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 fter.** [sqlite3
22140 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 28 _prepare_v2()] (
22150 61 6e 64 20 69 74 73 20 76 61 72 69 61 6e 74 73 and its variants
22160 29 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 ) or [sqlite3_re
22170 73 65 74 28 29 5d 20 61 6e 64 0a 2a 2a 20 62 65 set()] and.** be
22180 66 6f 72 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 fore [sqlite3_st
22190 65 70 28 29 5d 2e 0a 2a 2a 20 42 69 6e 64 69 6e ep()]..** Bindin
221a0 67 73 20 61 72 65 20 6e 6f 74 20 63 6c 65 61 72 gs are not clear
221b0 65 64 20 62 79 20 74 68 65 20 5b 73 71 6c 69 74 ed by the [sqlit
221c0 65 33 5f 72 65 73 65 74 28 29 5d 20 72 6f 75 74 e3_reset()] rout
221d0 69 6e 65 2e 0a 2a 2a 20 55 6e 62 6f 75 6e 64 20 ine..** Unbound
221e0 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 69 parameters are i
221f0 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 4e 55 nterpreted as NU
22200 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 LL..**.** These
22210 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 routines return
22220 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 73 [SQLITE_OK] on s
22230 75 63 63 65 73 73 20 6f 72 20 61 6e 20 65 72 72 uccess or an err
22240 6f 72 20 63 6f 64 65 20 69 66 0a 2a 2a 20 61 6e or code if.** an
22250 79 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e ything goes wron
22260 67 2e 20 20 5b 53 51 4c 49 54 45 5f 52 41 4e 47 g. [SQLITE_RANG
22270 45 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 E] is returned i
22280 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 0a f the parameter.
22290 2a 2a 20 69 6e 64 65 78 20 69 73 20 6f 75 74 20 ** index is out
222a0 6f 66 20 72 61 6e 67 65 2e 20 20 5b 53 51 4c 49 of range. [SQLI
222b0 54 45 5f 4e 4f 4d 45 4d 5d 20 69 73 20 72 65 74 TE_NOMEM] is ret
222c0 75 72 6e 65 64 20 69 66 20 6d 61 6c 6c 6f 63 28 urned if malloc(
222d0 29 20 66 61 69 6c 73 2e 0a 2a 2a 20 5b 53 51 4c ) fails..** [SQL
222e0 49 54 45 5f 4d 49 53 55 53 45 5d 20 6d 69 67 68 ITE_MISUSE] migh
222f0 74 20 62 65 20 72 65 74 75 72 6e 65 64 20 69 66 t be returned if
22300 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 these routines
22310 61 72 65 20 63 61 6c 6c 65 64 20 6f 6e 20 61 0a are called on a.
22320 2a 2a 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 ** virtual machi
22330 6e 65 20 74 68 61 74 20 69 73 20 74 68 65 20 77 ne that is the w
22340 72 6f 6e 67 20 73 74 61 74 65 20 6f 72 20 77 68 rong state or wh
22350 69 63 68 20 68 61 73 20 61 6c 72 65 61 64 79 20 ich has already
22360 62 65 65 6e 20 66 69 6e 61 6c 69 7a 65 64 2e 0a been finalized..
22370 2a 2a 20 44 65 74 65 63 74 69 6f 6e 20 6f 66 20 ** Detection of
22380 6d 69 73 75 73 65 20 69 73 20 75 6e 72 65 6c 69 misuse is unreli
22390 61 62 6c 65 2e 20 20 41 70 70 6c 69 63 61 74 69 able. Applicati
223a0 6f 6e 73 20 73 68 6f 75 6c 64 20 6e 6f 74 20 64 ons should not d
223b0 65 70 65 6e 64 0a 2a 2a 20 6f 6e 20 53 51 4c 49 epend.** on SQLI
223c0 54 45 5f 4d 49 53 55 53 45 20 72 65 74 75 72 6e TE_MISUSE return
223d0 73 2e 20 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 s. SQLITE_MISUS
223e0 45 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f E is intended to
223f0 20 69 6e 64 69 63 61 74 65 20 61 0a 2a 2a 20 61 indicate a.** a
22400 20 6c 6f 67 69 63 20 65 72 72 6f 72 20 69 6e 20 logic error in
22410 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e the application.
22420 20 20 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e Future version
22430 73 20 6f 66 20 53 51 4c 69 74 65 20 6d 69 67 68 s of SQLite migh
22440 74 0a 2a 2a 20 70 61 6e 69 63 20 72 61 74 68 65 t.** panic rathe
22450 72 20 74 68 61 6e 20 72 65 74 75 72 6e 20 53 51 r than return SQ
22460 4c 49 54 45 5f 4d 49 53 55 53 45 2e 0a 2a 2a 0a LITE_MISUSE..**.
22470 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 ** See also: [sq
22480 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d lite3_bind_param
22490 65 74 65 72 5f 63 6f 75 6e 74 28 29 5d 2c 0a 2a eter_count()],.*
224a0 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f * [sqlite3_bind_
224b0 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28 29 parameter_name()
224c0 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f ], and [sqlite3_
224d0 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 bind_parameter_i
224e0 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 ndex()]..**.** R
224f0 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 equirements:.**
22500 5b 48 31 33 35 30 36 5d 20 5b 48 31 33 35 30 39 [H13506] [H13509
22510 5d 20 5b 48 31 33 35 31 32 5d 20 5b 48 31 33 35 ] [H13512] [H135
22520 31 35 5d 20 5b 48 31 33 35 31 38 5d 20 5b 48 31 15] [H13518] [H1
22530 33 35 32 31 5d 20 5b 48 31 33 35 32 34 5d 20 5b 3521] [H13524] [
22540 48 31 33 35 32 37 5d 0a 2a 2a 20 5b 48 31 33 35 H13527].** [H135
22550 33 30 5d 20 5b 48 31 33 35 33 33 5d 20 5b 48 31 30] [H13533] [H1
22560 33 35 33 36 5d 20 5b 48 31 33 35 33 39 5d 20 5b 3536] [H13539] [
22570 48 31 33 35 34 32 5d 20 5b 48 31 33 35 34 35 5d H13542] [H13545]
22580 20 5b 48 31 33 35 34 38 5d 20 5b 48 31 33 35 35 [H13548] [H1355
22590 31 5d 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 1].**.*/.SQLITE_
225a0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
225b0 62 69 6e 64 5f 62 6c 6f 62 28 73 71 6c 69 74 65 bind_blob(sqlite
225c0 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 3_stmt*, int, co
225d0 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 20 6e nst void*, int n
225e0 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 , void(*)(void*)
225f0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
22600 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64 t sqlite3_bind_d
22610 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 73 74 ouble(sqlite3_st
22620 6d 74 2a 2c 20 69 6e 74 2c 20 64 6f 75 62 6c 65 mt*, int, double
22630 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
22640 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 t sqlite3_bind_i
22650 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a nt(sqlite3_stmt*
22660 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c , int, int);.SQL
22670 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
22680 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 te3_bind_int64(s
22690 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e qlite3_stmt*, in
226a0 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 t, sqlite3_int64
226b0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
226c0 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e t sqlite3_bind_n
226d0 75 6c 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 ull(sqlite3_stmt
226e0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
226f0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
22700 62 69 6e 64 5f 74 65 78 74 28 73 71 6c 69 74 65 bind_text(sqlite
22710 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 3_stmt*, int, co
22720 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 20 6e nst char*, int n
22730 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 , void(*)(void*)
22740 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
22750 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 t sqlite3_bind_t
22760 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f 73 74 ext16(sqlite3_st
22770 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 mt*, int, const
22780 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 void*, int, void
22790 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c (*)(void*));.SQL
227a0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
227b0 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 73 te3_bind_value(s
227c0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e qlite3_stmt*, in
227d0 74 2c 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 t, const sqlite3
227e0 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 _value*);.SQLITE
227f0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
22800 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 73 _bind_zeroblob(s
22810 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e qlite3_stmt*, in
22820 74 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 0a 2a t, int n);../*.*
22830 2a 20 43 41 50 49 33 52 45 46 3a 20 4e 75 6d 62 * CAPI3REF: Numb
22840 65 72 20 4f 66 20 53 51 4c 20 50 61 72 61 6d 65 er Of SQL Parame
22850 74 65 72 73 20 7b 48 31 33 36 30 30 7d 20 3c 53 ters {H13600} <S
22860 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 70300>.**.** Thi
22870 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 65 s routine can be
22880 20 75 73 65 64 20 74 6f 20 66 69 6e 64 20 74 68 used to find th
22890 65 20 6e 75 6d 62 65 72 20 6f 66 20 5b 53 51 4c e number of [SQL
228a0 20 70 61 72 61 6d 65 74 65 72 73 5d 0a 2a 2a 20 parameters].**
228b0 69 6e 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 in a [prepared s
228c0 74 61 74 65 6d 65 6e 74 5d 2e 20 20 53 51 4c 20 tatement]. SQL
228d0 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 74 parameters are t
228e0 6f 6b 65 6e 73 20 6f 66 20 74 68 65 0a 2a 2a 20 okens of the.**
228f0 66 6f 72 6d 20 22 3f 22 2c 20 22 3f 4e 4e 4e 22 form "?", "?NNN"
22900 2c 20 22 3a 41 41 41 22 2c 20 22 24 41 41 41 22 , ":AAA", "$AAA"
22910 2c 20 6f 72 20 22 40 41 41 41 22 20 74 68 61 74 , or "@AAA" that
22920 20 73 65 72 76 65 20 61 73 0a 2a 2a 20 70 6c 61 serve as.** pla
22930 63 65 68 6f 6c 64 65 72 73 20 66 6f 72 20 76 61 ceholders for va
22940 6c 75 65 73 20 74 68 61 74 20 61 72 65 20 5b 73 lues that are [s
22950 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 qlite3_bind_blob
22960 20 7c 20 62 6f 75 6e 64 5d 0a 2a 2a 20 74 6f 20 | bound].** to
22970 74 68 65 20 70 61 72 61 6d 65 74 65 72 73 20 61 the parameters a
22980 74 20 61 20 6c 61 74 65 72 20 74 69 6d 65 2e 0a t a later time..
22990 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
229a0 6e 65 20 61 63 74 75 61 6c 6c 79 20 72 65 74 75 ne actually retu
229b0 72 6e 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 rns the index of
229c0 20 74 68 65 20 6c 61 72 67 65 73 74 20 28 72 69 the largest (ri
229d0 67 68 74 6d 6f 73 74 29 0a 2a 2a 20 70 61 72 61 ghtmost).** para
229e0 6d 65 74 65 72 2e 20 46 6f 72 20 61 6c 6c 20 66 meter. For all f
229f0 6f 72 6d 73 20 65 78 63 65 70 74 20 3f 4e 4e 4e orms except ?NNN
22a00 2c 20 74 68 69 73 20 77 69 6c 6c 20 63 6f 72 72 , this will corr
22a10 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 0a 2a 2a espond to the.**
22a20 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69 71 75 number of uniqu
22a30 65 20 70 61 72 61 6d 65 74 65 72 73 2e 20 20 49 e parameters. I
22a40 66 20 70 61 72 61 6d 65 74 65 72 73 20 6f 66 20 f parameters of
22a50 74 68 65 20 3f 4e 4e 4e 20 61 72 65 20 75 73 65 the ?NNN are use
22a60 64 2c 0a 2a 2a 20 74 68 65 72 65 20 6d 61 79 20 d,.** there may
22a70 62 65 20 67 61 70 73 20 69 6e 20 74 68 65 20 6c be gaps in the l
22a80 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 ist..**.** See a
22a90 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 62 69 lso: [sqlite3_bi
22aa0 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74 65 33 5f nd_blob|sqlite3_
22ab0 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c bind()],.** [sql
22ac0 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 ite3_bind_parame
22ad0 74 65 72 5f 6e 61 6d 65 28 29 5d 2c 20 61 6e 64 ter_name()], and
22ae0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e .** [sqlite3_bin
22af0 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 d_parameter_inde
22b00 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 x()]..**.** Requ
22b10 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
22b20 33 36 30 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 3601].*/.SQLITE_
22b30 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
22b40 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 bind_parameter_c
22b50 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d ount(sqlite3_stm
22b60 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 t*);../*.** CAPI
22b70 33 52 45 46 3a 20 4e 61 6d 65 20 4f 66 20 41 20 3REF: Name Of A
22b80 48 6f 73 74 20 50 61 72 61 6d 65 74 65 72 20 7b Host Parameter {
22b90 48 31 33 36 32 30 7d 20 3c 53 37 30 33 30 30 3e H13620} <S70300>
22ba0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
22bb0 69 6e 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f ine returns a po
22bc0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 61 6d inter to the nam
22bd0 65 20 6f 66 20 74 68 65 20 6e 2d 74 68 0a 2a 2a e of the n-th.**
22be0 20 5b 53 51 4c 20 70 61 72 61 6d 65 74 65 72 5d [SQL parameter]
22bf0 20 69 6e 20 61 20 5b 70 72 65 70 61 72 65 64 20 in a [prepared
22c00 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 53 statement]..** S
22c10 51 4c 20 70 61 72 61 6d 65 74 65 72 73 20 6f 66 QL parameters of
22c20 20 74 68 65 20 66 6f 72 6d 20 22 3f 4e 4e 4e 22 the form "?NNN"
22c30 20 6f 72 20 22 3a 41 41 41 22 20 6f 72 20 22 40 or ":AAA" or "@
22c40 41 41 41 22 20 6f 72 20 22 24 41 41 41 22 0a 2a AAA" or "$AAA".*
22c50 2a 20 68 61 76 65 20 61 20 6e 61 6d 65 20 77 68 * have a name wh
22c60 69 63 68 20 69 73 20 74 68 65 20 73 74 72 69 6e ich is the strin
22c70 67 20 22 3f 4e 4e 4e 22 20 6f 72 20 22 3a 41 41 g "?NNN" or ":AA
22c80 41 22 20 6f 72 20 22 40 41 41 41 22 20 6f 72 20 A" or "@AAA" or
22c90 22 24 41 41 41 22 0a 2a 2a 20 72 65 73 70 65 63 "$AAA".** respec
22ca0 74 69 76 65 6c 79 2e 0a 2a 2a 20 49 6e 20 6f 74 tively..** In ot
22cb0 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 69 her words, the i
22cc0 6e 69 74 69 61 6c 20 22 3a 22 20 6f 72 20 22 24 nitial ":" or "$
22cd0 22 20 6f 72 20 22 40 22 20 6f 72 20 22 3f 22 0a " or "@" or "?".
22ce0 2a 2a 20 69 73 20 69 6e 63 6c 75 64 65 64 20 61 ** is included a
22cf0 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 6e 61 s part of the na
22d00 6d 65 2e 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 me..** Parameter
22d10 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3f s of the form "?
22d20 22 20 77 69 74 68 6f 75 74 20 61 20 66 6f 6c 6c " without a foll
22d30 6f 77 69 6e 67 20 69 6e 74 65 67 65 72 20 68 61 owing integer ha
22d40 76 65 20 6e 6f 20 6e 61 6d 65 0a 2a 2a 20 61 6e ve no name.** an
22d50 64 20 61 72 65 20 61 6c 73 6f 20 72 65 66 65 72 d are also refer
22d60 72 65 64 20 74 6f 20 61 73 20 22 61 6e 6f 6e 79 red to as "anony
22d70 6d 6f 75 73 20 70 61 72 61 6d 65 74 65 72 73 22 mous parameters"
22d80 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 ..**.** The firs
22d90 74 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 t host parameter
22da0 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 has an index of
22db0 20 31 2c 20 6e 6f 74 20 30 2e 0a 2a 2a 0a 2a 2a 1, not 0..**.**
22dc0 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 6e 20 If the value n
22dd0 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 is out of range
22de0 6f 72 20 69 66 20 74 68 65 20 6e 2d 74 68 20 70 or if the n-th p
22df0 61 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 6e arameter is.** n
22e00 61 6d 65 6c 65 73 73 2c 20 74 68 65 6e 20 4e 55 ameless, then NU
22e10 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 LL is returned.
22e20 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 The returned st
22e30 72 69 6e 67 20 69 73 0a 2a 2a 20 61 6c 77 61 79 ring is.** alway
22e40 73 20 69 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64 s in UTF-8 encod
22e50 69 6e 67 20 65 76 65 6e 20 69 66 20 74 68 65 20 ing even if the
22e60 6e 61 6d 65 64 20 70 61 72 61 6d 65 74 65 72 20 named parameter
22e70 77 61 73 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 6c was.** originall
22e80 79 20 73 70 65 63 69 66 69 65 64 20 61 73 20 55 y specified as U
22e90 54 46 2d 31 36 20 69 6e 20 5b 73 71 6c 69 74 65 TF-16 in [sqlite
22ea0 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d 20 6f 3_prepare16()] o
22eb0 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 r.** [sqlite3_pr
22ec0 65 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 0a 2a epare16_v2()]..*
22ed0 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b *.** See also: [
22ee0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f sqlite3_bind_blo
22ef0 62 7c 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 b|sqlite3_bind()
22f00 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 ],.** [sqlite3_b
22f10 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f ind_parameter_co
22f20 75 6e 74 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b unt()], and.** [
22f30 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 sqlite3_bind_par
22f40 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e ameter_index()].
22f50 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
22f60 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 36 32 31 5d nts:.** [H13621]
22f70 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 .*/.SQLITE_API c
22f80 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 onst char *sqlit
22f90 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 e3_bind_paramete
22fa0 72 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 r_name(sqlite3_s
22fb0 74 6d 74 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 0a tmt*, int);../*.
22fc0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 49 6e 64 ** CAPI3REF: Ind
22fd0 65 78 20 4f 66 20 41 20 50 61 72 61 6d 65 74 65 ex Of A Paramete
22fe0 72 20 57 69 74 68 20 41 20 47 69 76 65 6e 20 4e r With A Given N
22ff0 61 6d 65 20 7b 48 31 33 36 34 30 7d 20 3c 53 37 ame {H13640} <S7
23000 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 0300>.**.** Retu
23010 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 rn the index of
23020 61 6e 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72 an SQL parameter
23030 20 67 69 76 65 6e 20 69 74 73 20 6e 61 6d 65 2e given its name.
23040 20 20 54 68 65 0a 2a 2a 20 69 6e 64 65 78 20 76 The.** index v
23050 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 alue returned is
23060 20 73 75 69 74 61 62 6c 65 20 66 6f 72 20 75 73 suitable for us
23070 65 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 0a e as the second.
23080 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 ** parameter to
23090 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c [sqlite3_bind_bl
230a0 6f 62 7c 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 ob|sqlite3_bind(
230b0 29 5d 2e 20 20 41 20 7a 65 72 6f 0a 2a 2a 20 69 )]. A zero.** i
230c0 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 6e 6f s returned if no
230d0 20 6d 61 74 63 68 69 6e 67 20 70 61 72 61 6d 65 matching parame
230e0 74 65 72 20 69 73 20 66 6f 75 6e 64 2e 20 20 54 ter is found. T
230f0 68 65 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 he parameter.**
23100 6e 61 6d 65 20 6d 75 73 74 20 62 65 20 67 69 76 name must be giv
23110 65 6e 20 69 6e 20 55 54 46 2d 38 20 65 76 65 6e en in UTF-8 even
23120 20 69 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c if the original
23130 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 77 61 statement.** wa
23140 73 20 70 72 65 70 61 72 65 64 20 66 72 6f 6d 20 s prepared from
23150 55 54 46 2d 31 36 20 74 65 78 74 20 75 73 69 6e UTF-16 text usin
23160 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 g [sqlite3_prepa
23170 72 65 31 36 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a re16_v2()]..**.*
23180 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c * See also: [sql
23190 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73 ite3_bind_blob|s
231a0 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d 2c 0a qlite3_bind()],.
231b0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 ** [sqlite3_bind
231c0 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 _parameter_count
231d0 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c ()], and.** [sql
231e0 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 ite3_bind_parame
231f0 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a 2a ter_index()]..**
23200 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
23210 3a 0a 2a 2a 20 5b 48 31 33 36 34 31 5d 0a 2a 2f :.** [H13641].*/
23220 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
23230 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 sqlite3_bind_par
23240 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 73 71 6c ameter_index(sql
23250 69 74 65 33 5f 73 74 6d 74 2a 2c 20 63 6f 6e 73 ite3_stmt*, cons
23260 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 3b 0a t char *zName);.
23270 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
23280 20 52 65 73 65 74 20 41 6c 6c 20 42 69 6e 64 69 Reset All Bindi
23290 6e 67 73 20 4f 6e 20 41 20 50 72 65 70 61 72 65 ngs On A Prepare
232a0 64 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 31 33 d Statement {H13
232b0 36 36 30 7d 20 3c 53 37 30 33 30 30 3e 0a 2a 2a 660} <S70300>.**
232c0 0a 2a 2a 20 43 6f 6e 74 72 61 72 79 20 74 6f 20 .** Contrary to
232d0 74 68 65 20 69 6e 74 75 69 74 69 6f 6e 20 6f 66 the intuition of
232e0 20 6d 61 6e 79 2c 20 5b 73 71 6c 69 74 65 33 5f many, [sqlite3_
232f0 72 65 73 65 74 28 29 5d 20 64 6f 65 73 20 6e 6f reset()] does no
23300 74 20 72 65 73 65 74 0a 2a 2a 20 74 68 65 20 5b t reset.** the [
23310 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f sqlite3_bind_blo
23320 62 20 7c 20 62 69 6e 64 69 6e 67 73 5d 20 6f 6e b | bindings] on
23330 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 a [prepared sta
23340 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 55 73 65 20 tement]..** Use
23350 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 this routine to
23360 72 65 73 65 74 20 61 6c 6c 20 68 6f 73 74 20 70 reset all host p
23370 61 72 61 6d 65 74 65 72 73 20 74 6f 20 4e 55 4c arameters to NUL
23380 4c 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 L..**.** Require
23390 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 36 36 ments:.** [H1366
233a0 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 1].*/.SQLITE_API
233b0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6c 65 int sqlite3_cle
233c0 61 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c 69 ar_bindings(sqli
233d0 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a te3_stmt*);../*.
233e0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4e 75 6d ** CAPI3REF: Num
233f0 62 65 72 20 4f 66 20 43 6f 6c 75 6d 6e 73 20 49 ber Of Columns I
23400 6e 20 41 20 52 65 73 75 6c 74 20 53 65 74 20 7b n A Result Set {
23410 48 31 33 37 31 30 7d 20 3c 53 31 30 37 30 30 3e H13710} <S10700>
23420 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 .**.** Return th
23430 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 e number of colu
23440 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c mns in the resul
23450 74 20 73 65 74 20 72 65 74 75 72 6e 65 64 20 62 t set returned b
23460 79 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 y the.** [prepar
23470 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 54 ed statement]. T
23480 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 his routine retu
23490 72 6e 73 20 30 20 69 66 20 70 53 74 6d 74 20 69 rns 0 if pStmt i
234a0 73 20 61 6e 20 53 51 4c 0a 2a 2a 20 73 74 61 74 s an SQL.** stat
234b0 65 6d 65 6e 74 20 74 68 61 74 20 64 6f 65 73 20 ement that does
234c0 6e 6f 74 20 72 65 74 75 72 6e 20 64 61 74 61 20 not return data
234d0 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 61 6e 20 (for example an
234e0 5b 55 50 44 41 54 45 5d 29 2e 0a 2a 2a 0a 2a 2a [UPDATE])..**.**
234f0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a Requirements:.*
23500 2a 20 5b 48 31 33 37 31 31 5d 0a 2a 2f 0a 53 51 * [H13711].*/.SQ
23510 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
23520 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e ite3_column_coun
23530 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a t(sqlite3_stmt *
23540 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 pStmt);../*.** C
23550 41 50 49 33 52 45 46 3a 20 43 6f 6c 75 6d 6e 20 API3REF: Column
23560 4e 61 6d 65 73 20 49 6e 20 41 20 52 65 73 75 6c Names In A Resul
23570 74 20 53 65 74 20 7b 48 31 33 37 32 30 7d 20 3c t Set {H13720} <
23580 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S10700>.**.** Th
23590 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65 74 ese routines ret
235a0 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 61 73 73 urn the name ass
235b0 69 67 6e 65 64 20 74 6f 20 61 20 70 61 72 74 69 igned to a parti
235c0 63 75 6c 61 72 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 cular column.**
235d0 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 in the result se
235e0 74 20 6f 66 20 61 20 5b 53 45 4c 45 43 54 5d 20 t of a [SELECT]
235f0 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 20 statement. The
23600 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e sqlite3_column_n
23610 61 6d 65 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 ame().** interfa
23620 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 ce returns a poi
23630 6e 74 65 72 20 74 6f 20 61 20 7a 65 72 6f 2d 74 nter to a zero-t
23640 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 erminated UTF-8
23650 73 74 72 69 6e 67 0a 2a 2a 20 61 6e 64 20 73 71 string.** and sq
23660 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d lite3_column_nam
23670 65 31 36 28 29 20 72 65 74 75 72 6e 73 20 61 20 e16() returns a
23680 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 7a 65 72 pointer to a zer
23690 6f 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 o-terminated.**
236a0 55 54 46 2d 31 36 20 73 74 72 69 6e 67 2e 20 20 UTF-16 string.
236b0 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 The first parame
236c0 74 65 72 20 69 73 20 74 68 65 20 5b 70 72 65 70 ter is the [prep
236d0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 0a ared statement].
236e0 2a 2a 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e ** that implemen
236f0 74 73 20 74 68 65 20 5b 53 45 4c 45 43 54 5d 20 ts the [SELECT]
23700 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 65 20 73 statement. The s
23710 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 econd parameter
23720 69 73 20 74 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e is the.** column
23730 20 6e 75 6d 62 65 72 2e 20 20 54 68 65 20 6c 65 number. The le
23740 66 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 69 73 ftmost column is
23750 20 6e 75 6d 62 65 72 20 30 2e 0a 2a 2a 0a 2a 2a number 0..**.**
23760 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 The returned st
23770 72 69 6e 67 20 70 6f 69 6e 74 65 72 20 69 73 20 ring pointer is
23780 76 61 6c 69 64 20 75 6e 74 69 6c 20 65 69 74 68 valid until eith
23790 65 72 20 74 68 65 20 5b 70 72 65 70 61 72 65 64 er the [prepared
237a0 20 73 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 69 statement].** i
237b0 73 20 64 65 73 74 72 6f 79 65 64 20 62 79 20 5b s destroyed by [
237c0 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 sqlite3_finalize
237d0 28 29 5d 20 6f 72 20 75 6e 74 69 6c 20 74 68 65 ()] or until the
237e0 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a next call to.**
237f0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
23800 6e 61 6d 65 28 29 20 6f 72 20 73 71 6c 69 74 65 name() or sqlite
23810 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 3_column_name16(
23820 29 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 63 6f ) on the same co
23830 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 lumn..**.** If s
23840 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 qlite3_malloc()
23850 66 61 69 6c 73 20 64 75 72 69 6e 67 20 74 68 65 fails during the
23860 20 70 72 6f 63 65 73 73 69 6e 67 20 6f 66 20 65 processing of e
23870 69 74 68 65 72 20 72 6f 75 74 69 6e 65 0a 2a 2a ither routine.**
23880 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 64 75 (for example du
23890 72 69 6e 67 20 61 20 63 6f 6e 76 65 72 73 69 6f ring a conversio
238a0 6e 20 66 72 6f 6d 20 55 54 46 2d 38 20 74 6f 20 n from UTF-8 to
238b0 55 54 46 2d 31 36 29 20 74 68 65 6e 20 61 0a 2a UTF-16) then a.*
238c0 2a 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 * NULL pointer i
238d0 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a s returned..**.*
238e0 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 * The name of a
238f0 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 result column is
23900 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 the value of th
23910 65 20 22 41 53 22 20 63 6c 61 75 73 65 20 66 6f e "AS" clause fo
23920 72 0a 2a 2a 20 74 68 61 74 20 63 6f 6c 75 6d 6e r.** that column
23930 2c 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e , if there is an
23940 20 41 53 20 63 6c 61 75 73 65 2e 20 20 49 66 20 AS clause. If
23950 74 68 65 72 65 20 69 73 20 6e 6f 20 41 53 20 63 there is no AS c
23960 6c 61 75 73 65 0a 2a 2a 20 74 68 65 6e 20 74 68 lause.** then th
23970 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f e name of the co
23980 6c 75 6d 6e 20 69 73 20 75 6e 73 70 65 63 69 66 lumn is unspecif
23990 69 65 64 20 61 6e 64 20 6d 61 79 20 63 68 61 6e ied and may chan
239a0 67 65 20 66 72 6f 6d 0a 2a 2a 20 6f 6e 65 20 72 ge from.** one r
239b0 65 6c 65 61 73 65 20 6f 66 20 53 51 4c 69 74 65 elease of SQLite
239c0 20 74 6f 20 74 68 65 20 6e 65 78 74 2e 0a 2a 2a to the next..**
239d0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
239e0 3a 0a 2a 2a 20 5b 48 31 33 37 32 31 5d 20 5b 48 :.** [H13721] [H
239f0 31 33 37 32 33 5d 20 5b 48 31 33 37 32 34 5d 20 13723] [H13724]
23a00 5b 48 31 33 37 32 35 5d 20 5b 48 31 33 37 32 36 [H13725] [H13726
23a10 5d 20 5b 48 31 33 37 32 37 5d 0a 2a 2f 0a 53 51 ] [H13727].*/.SQ
23a20 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 LITE_API const c
23a30 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c har *sqlite3_col
23a40 75 6d 6e 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 umn_name(sqlite3
23a50 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 4e 29 3b 0a _stmt*, int N);.
23a60 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
23a70 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 void *sqlite3_c
23a80 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 73 71 6c olumn_name16(sql
23a90 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 ite3_stmt*, int
23aa0 4e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 N);../*.** CAPI3
23ab0 52 45 46 3a 20 53 6f 75 72 63 65 20 4f 66 20 44 REF: Source Of D
23ac0 61 74 61 20 49 6e 20 41 20 51 75 65 72 79 20 52 ata In A Query R
23ad0 65 73 75 6c 74 20 7b 48 31 33 37 34 30 7d 20 3c esult {H13740} <
23ae0 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S10700>.**.** Th
23af0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 70 72 6f ese routines pro
23b00 76 69 64 65 20 61 20 6d 65 61 6e 73 20 74 6f 20 vide a means to
23b10 64 65 74 65 72 6d 69 6e 65 20 77 68 61 74 20 63 determine what c
23b20 6f 6c 75 6d 6e 20 6f 66 20 77 68 61 74 0a 2a 2a olumn of what.**
23b30 20 74 61 62 6c 65 20 69 6e 20 77 68 69 63 68 20 table in which
23b40 64 61 74 61 62 61 73 65 20 61 20 72 65 73 75 6c database a resul
23b50 74 20 6f 66 20 61 20 5b 53 45 4c 45 43 54 5d 20 t of a [SELECT]
23b60 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 65 73 20 statement comes
23b70 66 72 6f 6d 2e 0a 2a 2a 20 54 68 65 20 6e 61 6d from..** The nam
23b80 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
23b90 65 20 6f 72 20 74 61 62 6c 65 20 6f 72 20 63 6f e or table or co
23ba0 6c 75 6d 6e 20 63 61 6e 20 62 65 20 72 65 74 75 lumn can be retu
23bb0 72 6e 65 64 20 61 73 0a 2a 2a 20 65 69 74 68 65 rned as.** eithe
23bc0 72 20 61 20 55 54 46 2d 38 20 6f 72 20 55 54 46 r a UTF-8 or UTF
23bd0 2d 31 36 20 73 74 72 69 6e 67 2e 20 20 54 68 65 -16 string. The
23be0 20 5f 64 61 74 61 62 61 73 65 5f 20 72 6f 75 74 _database_ rout
23bf0 69 6e 65 73 20 72 65 74 75 72 6e 0a 2a 2a 20 74 ines return.** t
23c00 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 he database name
23c10 2c 20 74 68 65 20 5f 74 61 62 6c 65 5f 20 72 6f , the _table_ ro
23c20 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 74 68 utines return th
23c30 65 20 74 61 62 6c 65 20 6e 61 6d 65 2c 20 61 6e e table name, an
23c40 64 0a 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e 5f d.** the origin_
23c50 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e routines return
23c60 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 the column name
23c70 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 ..** The returne
23c80 64 20 73 74 72 69 6e 67 20 69 73 20 76 61 6c 69 d string is vali
23c90 64 20 75 6e 74 69 6c 20 74 68 65 20 5b 70 72 65 d until the [pre
23ca0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d pared statement]
23cb0 20 69 73 20 64 65 73 74 72 6f 79 65 64 0a 2a 2a is destroyed.**
23cc0 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f using [sqlite3_
23cd0 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f 72 20 75 finalize()] or u
23ce0 6e 74 69 6c 20 74 68 65 20 73 61 6d 65 20 69 6e ntil the same in
23cf0 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 72 65 71 formation is req
23d00 75 65 73 74 65 64 0a 2a 2a 20 61 67 61 69 6e 20 uested.** again
23d10 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 65 in a different e
23d20 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 ncoding..**.** T
23d30 68 65 20 6e 61 6d 65 73 20 72 65 74 75 72 6e 65 he names returne
23d40 64 20 61 72 65 20 74 68 65 20 6f 72 69 67 69 6e d are the origin
23d50 61 6c 20 75 6e 2d 61 6c 69 61 73 65 64 20 6e 61 al un-aliased na
23d60 6d 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 mes of the.** da
23d70 74 61 62 61 73 65 2c 20 74 61 62 6c 65 2c 20 61 tabase, table, a
23d80 6e 64 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a nd column..**.**
23d90 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d The first argum
23da0 65 6e 74 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f ent to the follo
23db0 77 69 6e 67 20 63 61 6c 6c 73 20 69 73 20 61 20 wing calls is a
23dc0 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d [prepared statem
23dd0 65 6e 74 5d 2e 0a 2a 2a 20 54 68 65 73 65 20 66 ent]..** These f
23de0 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 6e 20 unctions return
23df0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 information abou
23e00 74 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e t the Nth column
23e10 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 returned by.**
23e20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2c 20 77 the statement, w
23e30 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 73 65 here N is the se
23e40 63 6f 6e 64 20 66 75 6e 63 74 69 6f 6e 20 61 72 cond function ar
23e50 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 gument..**.** If
23e60 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 the Nth column
23e70 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 returned by the
23e80 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 6e 20 statement is an
23e90 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 0a 2a 2a expression or.**
23ea0 20 73 75 62 71 75 65 72 79 20 61 6e 64 20 69 73 subquery and is
23eb0 20 6e 6f 74 20 61 20 63 6f 6c 75 6d 6e 20 76 61 not a column va
23ec0 6c 75 65 2c 20 74 68 65 6e 20 61 6c 6c 20 6f 66 lue, then all of
23ed0 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 these functions
23ee0 20 72 65 74 75 72 6e 0a 2a 2a 20 4e 55 4c 4c 2e return.** NULL.
23ef0 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 20 These routine
23f00 6d 69 67 68 74 20 61 6c 73 6f 20 72 65 74 75 72 might also retur
23f10 6e 20 4e 55 4c 4c 20 69 66 20 61 20 6d 65 6d 6f n NULL if a memo
23f20 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 ry allocation er
23f30 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 20 ror.** occurs.
23f40 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 79 20 Otherwise, they
23f50 72 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 return the name
23f60 6f 66 20 74 68 65 20 61 74 74 61 63 68 65 64 20 of the attached
23f70 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65 0a database, table.
23f80 2a 2a 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 74 68 ** and column th
23f90 61 74 20 71 75 65 72 79 20 72 65 73 75 6c 74 20 at query result
23fa0 63 6f 6c 75 6d 6e 20 77 61 73 20 65 78 74 72 61 column was extra
23fb0 63 74 65 64 20 66 72 6f 6d 2e 0a 2a 2a 0a 2a 2a cted from..**.**
23fc0 20 41 73 20 77 69 74 68 20 61 6c 6c 20 6f 74 68 As with all oth
23fd0 65 72 20 53 51 4c 69 74 65 20 41 50 49 73 2c 20 er SQLite APIs,
23fe0 74 68 6f 73 65 20 70 6f 73 74 66 69 78 65 64 20 those postfixed
23ff0 77 69 74 68 20 22 31 36 22 20 72 65 74 75 72 6e with "16" return
24000 0a 2a 2a 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 .** UTF-16 encod
24010 65 64 20 73 74 72 69 6e 67 73 2c 20 74 68 65 20 ed strings, the
24020 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 other functions
24030 72 65 74 75 72 6e 20 55 54 46 2d 38 2e 20 7b 45 return UTF-8. {E
24040 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 ND}.**.** These
24050 41 50 49 73 20 61 72 65 20 6f 6e 6c 79 20 61 76 APIs are only av
24060 61 69 6c 61 62 6c 65 20 69 66 20 74 68 65 20 6c ailable if the l
24070 69 62 72 61 72 79 20 77 61 73 20 63 6f 6d 70 69 ibrary was compi
24080 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 led with the.**
24090 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 [SQLITE_ENABLE_C
240a0 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 5d 20 OLUMN_METADATA]
240b0 43 2d 70 72 65 70 72 6f 63 65 73 73 6f 72 20 73 C-preprocessor s
240c0 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64 2e 0a 2a ymbol defined..*
240d0 2a 0a 2a 2a 20 7b 41 31 33 37 35 31 7d 0a 2a 2a *.** {A13751}.**
240e0 20 49 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 If two or more
240f0 74 68 72 65 61 64 73 20 63 61 6c 6c 20 6f 6e 65 threads call one
24100 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65 73 or more of thes
24110 65 20 72 6f 75 74 69 6e 65 73 20 61 67 61 69 6e e routines again
24120 73 74 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 70 st the same.** p
24130 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
24140 74 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 61 74 20 t and column at
24150 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 74 68 the same time th
24160 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20 61 en the results a
24170 72 65 0a 2a 2a 20 75 6e 64 65 66 69 6e 65 64 2e re.** undefined.
24180 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
24190 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 37 34 31 5d nts:.** [H13741]
241a0 20 5b 48 31 33 37 34 32 5d 20 5b 48 31 33 37 34 [H13742] [H1374
241b0 33 5d 20 5b 48 31 33 37 34 34 5d 20 5b 48 31 33 3] [H13744] [H13
241c0 37 34 35 5d 20 5b 48 31 33 37 34 36 5d 20 5b 48 745] [H13746] [H
241d0 31 33 37 34 38 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 13748].**.** If
241e0 74 77 6f 20 6f 72 20 6d 6f 72 65 20 74 68 72 65 two or more thre
241f0 61 64 73 20 63 61 6c 6c 20 6f 6e 65 20 6f 72 20 ads call one or
24200 6d 6f 72 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 more.** [sqlite3
24210 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 _column_database
24220 5f 6e 61 6d 65 20 7c 20 63 6f 6c 75 6d 6e 20 6d _name | column m
24230 65 74 61 64 61 74 61 20 69 6e 74 65 72 66 61 63 etadata interfac
24240 65 73 5d 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 es].** for the s
24250 61 6d 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 ame [prepared st
24260 61 74 65 6d 65 6e 74 5d 20 61 6e 64 20 72 65 73 atement] and res
24270 75 6c 74 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 61 74 ult column.** at
24280 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 74 the same time t
24290 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20 hen the results
242a0 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a are undefined..*
242b0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e /.SQLITE_API con
242c0 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 st char *sqlite3
242d0 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 _column_database
242e0 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 _name(sqlite3_st
242f0 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 mt*,int);.SQLITE
24300 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 _API const void
24310 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f *sqlite3_column_
24320 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 28 database_name16(
24330 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e sqlite3_stmt*,in
24340 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 t);.SQLITE_API c
24350 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 onst char *sqlit
24360 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f e3_column_table_
24370 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d name(sqlite3_stm
24380 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t*,int);.SQLITE_
24390 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a API const void *
243a0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 sqlite3_column_t
243b0 61 62 6c 65 5f 6e 61 6d 65 31 36 28 73 71 6c 69 able_name16(sqli
243c0 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a te3_stmt*,int);.
243d0 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
243e0 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 char *sqlite3_c
243f0 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d olumn_origin_nam
24400 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c e(sqlite3_stmt*,
24410 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 int);.SQLITE_API
24420 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c const void *sql
24430 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 ite3_column_orig
24440 69 6e 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 in_name16(sqlite
24450 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 0a 2f 3_stmt*,int);../
24460 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 *.** CAPI3REF: D
24470 65 63 6c 61 72 65 64 20 44 61 74 61 74 79 70 65 eclared Datatype
24480 20 4f 66 20 41 20 51 75 65 72 79 20 52 65 73 75 Of A Query Resu
24490 6c 74 20 7b 48 31 33 37 36 30 7d 20 3c 53 31 30 lt {H13760} <S10
244a0 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 700>.**.** The f
244b0 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 69 irst parameter i
244c0 73 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 s a [prepared st
244d0 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 49 66 20 atement]..** If
244e0 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20 69 this statement i
244f0 73 20 61 20 5b 53 45 4c 45 43 54 5d 20 73 74 61 s a [SELECT] sta
24500 74 65 6d 65 6e 74 20 61 6e 64 20 74 68 65 20 4e tement and the N
24510 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 th column of the
24520 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 72 65 73 .** returned res
24530 75 6c 74 20 73 65 74 20 6f 66 20 74 68 61 74 20 ult set of that
24540 5b 53 45 4c 45 43 54 5d 20 69 73 20 61 20 74 61 [SELECT] is a ta
24550 62 6c 65 20 63 6f 6c 75 6d 6e 20 28 6e 6f 74 20 ble column (not
24560 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e an.** expression
24570 20 6f 72 20 73 75 62 71 75 65 72 79 29 20 74 68 or subquery) th
24580 65 6e 20 74 68 65 20 64 65 63 6c 61 72 65 64 20 en the declared
24590 74 79 70 65 20 6f 66 20 74 68 65 20 74 61 62 6c type of the tabl
245a0 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 69 73 20 72 e.** column is r
245b0 65 74 75 72 6e 65 64 2e 20 20 49 66 20 74 68 65 eturned. If the
245c0 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 Nth column of t
245d0 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 69 73 he result set is
245e0 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f an.** expressio
245f0 6e 20 6f 72 20 73 75 62 71 75 65 72 79 2c 20 74 n or subquery, t
24600 68 65 6e 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 hen a NULL point
24610 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a er is returned..
24620 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 ** The returned
24630 73 74 72 69 6e 67 20 69 73 20 61 6c 77 61 79 73 string is always
24640 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 2e 20 UTF-8 encoded.
24650 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 {END}.**.** For
24660 65 78 61 6d 70 6c 65 2c 20 67 69 76 65 6e 20 74 example, given t
24670 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 he database sche
24680 6d 61 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41 54 45 ma:.**.** CREATE
24690 20 54 41 42 4c 45 20 74 31 28 63 31 20 56 41 52 TABLE t1(c1 VAR
246a0 49 41 4e 54 29 3b 0a 2a 2a 0a 2a 2a 20 61 6e 64 IANT);.**.** and
246b0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
246c0 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 65 20 63 tatement to be c
246d0 6f 6d 70 69 6c 65 64 3a 0a 2a 2a 0a 2a 2a 20 53 ompiled:.**.** S
246e0 45 4c 45 43 54 20 63 31 20 2b 20 31 2c 20 63 31 ELECT c1 + 1, c1
246f0 20 46 52 4f 4d 20 74 31 3b 0a 2a 2a 0a 2a 2a 20 FROM t1;.**.**
24700 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 6f 75 this routine wou
24710 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20 73 74 ld return the st
24720 72 69 6e 67 20 22 56 41 52 49 41 4e 54 22 20 66 ring "VARIANT" f
24730 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20 72 65 or the second re
24740 73 75 6c 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 28 sult.** column (
24750 69 3d 3d 31 29 2c 20 61 6e 64 20 61 20 4e 55 4c i==1), and a NUL
24760 4c 20 70 6f 69 6e 74 65 72 20 66 6f 72 20 74 68 L pointer for th
24770 65 20 66 69 72 73 74 20 72 65 73 75 6c 74 20 63 e first result c
24780 6f 6c 75 6d 6e 20 28 69 3d 3d 30 29 2e 0a 2a 2a olumn (i==0)..**
24790 0a 2a 2a 20 53 51 4c 69 74 65 20 75 73 65 73 20 .** SQLite uses
247a0 64 79 6e 61 6d 69 63 20 72 75 6e 2d 74 69 6d 65 dynamic run-time
247b0 20 74 79 70 69 6e 67 2e 20 20 53 6f 20 6a 75 73 typing. So jus
247c0 74 20 62 65 63 61 75 73 65 20 61 20 63 6f 6c 75 t because a colu
247d0 6d 6e 0a 2a 2a 20 69 73 20 64 65 63 6c 61 72 65 mn.** is declare
247e0 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20 70 d to contain a p
247f0 61 72 74 69 63 75 6c 61 72 20 74 79 70 65 20 64 articular type d
24800 6f 65 73 20 6e 6f 74 20 6d 65 61 6e 20 74 68 61 oes not mean tha
24810 74 20 74 68 65 0a 2a 2a 20 64 61 74 61 20 73 74 t the.** data st
24820 6f 72 65 64 20 69 6e 20 74 68 61 74 20 63 6f 6c ored in that col
24830 75 6d 6e 20 69 73 20 6f 66 20 74 68 65 20 64 65 umn is of the de
24840 63 6c 61 72 65 64 20 74 79 70 65 2e 20 20 53 51 clared type. SQ
24850 4c 69 74 65 20 69 73 0a 2a 2a 20 73 74 72 6f 6e Lite is.** stron
24860 67 6c 79 20 74 79 70 65 64 2c 20 62 75 74 20 74 gly typed, but t
24870 68 65 20 74 79 70 69 6e 67 20 69 73 20 64 79 6e he typing is dyn
24880 61 6d 69 63 20 6e 6f 74 20 73 74 61 74 69 63 2e amic not static.
24890 20 20 54 79 70 65 0a 2a 2a 20 69 73 20 61 73 73 Type.** is ass
248a0 6f 63 69 61 74 65 64 20 77 69 74 68 20 69 6e 64 ociated with ind
248b0 69 76 69 64 75 61 6c 20 76 61 6c 75 65 73 2c 20 ividual values,
248c0 6e 6f 74 20 77 69 74 68 20 74 68 65 20 63 6f 6e not with the con
248d0 74 61 69 6e 65 72 73 0a 2a 2a 20 75 73 65 64 20 tainers.** used
248e0 74 6f 20 68 6f 6c 64 20 74 68 6f 73 65 20 76 61 to hold those va
248f0 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 lues..**.** Requ
24900 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
24910 33 37 36 31 5d 20 5b 48 31 33 37 36 32 5d 20 5b 3761] [H13762] [
24920 48 31 33 37 36 33 5d 0a 2a 2f 0a 53 51 4c 49 54 H13763].*/.SQLIT
24930 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 E_API const char
24940 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e *sqlite3_column
24950 5f 64 65 63 6c 74 79 70 65 28 73 71 6c 69 74 65 _decltype(sqlite
24960 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 3_stmt*,int);.SQ
24970 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 LITE_API const v
24980 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c oid *sqlite3_col
24990 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 28 73 umn_decltype16(s
249a0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 qlite3_stmt*,int
249b0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
249c0 45 46 3a 20 45 76 61 6c 75 61 74 65 20 41 6e 20 EF: Evaluate An
249d0 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 SQL Statement {H
249e0 31 33 32 30 30 7d 20 3c 53 31 30 30 30 30 3e 0a 13200} <S10000>.
249f0 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 61 20 5b 70 **.** After a [p
24a00 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
24a10 74 5d 20 68 61 73 20 62 65 65 6e 20 70 72 65 70 t] has been prep
24a20 61 72 65 64 20 75 73 69 6e 67 20 65 69 74 68 65 ared using eithe
24a30 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 r.** [sqlite3_pr
24a40 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 5b epare_v2()] or [
24a50 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 sqlite3_prepare1
24a60 36 5f 76 32 28 29 5d 20 6f 72 20 6f 6e 65 20 6f 6_v2()] or one o
24a70 66 20 74 68 65 20 6c 65 67 61 63 79 0a 2a 2a 20 f the legacy.**
24a80 69 6e 74 65 72 66 61 63 65 73 20 5b 73 71 6c 69 interfaces [sqli
24a90 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 6f te3_prepare()] o
24aa0 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 r [sqlite3_prepa
24ab0 72 65 31 36 28 29 5d 2c 20 74 68 69 73 20 66 75 re16()], this fu
24ac0 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 62 nction.** must b
24ad0 65 20 63 61 6c 6c 65 64 20 6f 6e 65 20 6f 72 20 e called one or
24ae0 6d 6f 72 65 20 74 69 6d 65 73 20 74 6f 20 65 76 more times to ev
24af0 61 6c 75 61 74 65 20 74 68 65 20 73 74 61 74 65 aluate the state
24b00 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ment..**.** The
24b10 64 65 74 61 69 6c 73 20 6f 66 20 74 68 65 20 62 details of the b
24b20 65 68 61 76 69 6f 72 20 6f 66 20 74 68 65 20 73 ehavior of the s
24b30 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 69 6e qlite3_step() in
24b40 74 65 72 66 61 63 65 20 64 65 70 65 6e 64 0a 2a terface depend.*
24b50 2a 20 6f 6e 20 77 68 65 74 68 65 72 20 74 68 65 * on whether the
24b60 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73 20 70 statement was p
24b70 72 65 70 61 72 65 64 20 75 73 69 6e 67 20 74 68 repared using th
24b80 65 20 6e 65 77 65 72 20 22 76 32 22 20 69 6e 74 e newer "v2" int
24b90 65 72 66 61 63 65 0a 2a 2a 20 5b 73 71 6c 69 74 erface.** [sqlit
24ba0 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d e3_prepare_v2()]
24bb0 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70 72 and [sqlite3_pr
24bc0 65 70 61 72 65 31 36 5f 76 32 28 29 5d 20 6f 72 epare16_v2()] or
24bd0 20 74 68 65 20 6f 6c 64 65 72 20 6c 65 67 61 63 the older legac
24be0 79 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 5b y.** interface [
24bf0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 sqlite3_prepare(
24c00 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f )] and [sqlite3_
24c10 70 72 65 70 61 72 65 31 36 28 29 5d 2e 20 20 54 prepare16()]. T
24c20 68 65 20 75 73 65 20 6f 66 20 74 68 65 0a 2a 2a he use of the.**
24c30 20 6e 65 77 20 22 76 32 22 20 69 6e 74 65 72 66 new "v2" interf
24c40 61 63 65 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 ace is recommend
24c50 65 64 20 66 6f 72 20 6e 65 77 20 61 70 70 6c 69 ed for new appli
24c60 63 61 74 69 6f 6e 73 20 62 75 74 20 74 68 65 20 cations but the
24c70 6c 65 67 61 63 79 0a 2a 2a 20 69 6e 74 65 72 66 legacy.** interf
24c80 61 63 65 20 77 69 6c 6c 20 63 6f 6e 74 69 6e 75 ace will continu
24c90 65 20 74 6f 20 62 65 20 73 75 70 70 6f 72 74 65 e to be supporte
24ca0 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 d..**.** In the
24cb0 6c 65 67 61 63 79 20 69 6e 74 65 72 66 61 63 65 legacy interface
24cc0 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c , the return val
24cd0 75 65 20 77 69 6c 6c 20 62 65 20 65 69 74 68 65 ue will be eithe
24ce0 72 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 2c r [SQLITE_BUSY],
24cf0 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 .** [SQLITE_DONE
24d00 5d 2c 20 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 2c ], [SQLITE_ROW],
24d10 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 2c [SQLITE_ERROR],
24d20 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4d 49 53 55 or [SQLITE_MISU
24d30 53 45 5d 2e 0a 2a 2a 20 57 69 74 68 20 74 68 65 SE]..** With the
24d40 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 2c "v2" interface,
24d50 20 61 6e 79 20 6f 66 20 74 68 65 20 6f 74 68 65 any of the othe
24d60 72 20 5b 72 65 73 75 6c 74 20 63 6f 64 65 73 5d r [result codes]
24d70 20 6f 72 0a 2a 2a 20 5b 65 78 74 65 6e 64 65 64 or.** [extended
24d80 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d 20 6d result codes] m
24d90 69 67 68 74 20 62 65 20 72 65 74 75 72 6e 65 64 ight be returned
24da0 20 61 73 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 as well..**.**
24db0 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6d 65 [SQLITE_BUSY] me
24dc0 61 6e 73 20 74 68 61 74 20 74 68 65 20 64 61 74 ans that the dat
24dd0 61 62 61 73 65 20 65 6e 67 69 6e 65 20 77 61 73 abase engine was
24de0 20 75 6e 61 62 6c 65 20 74 6f 20 61 63 71 75 69 unable to acqui
24df0 72 65 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 re the.** databa
24e00 73 65 20 6c 6f 63 6b 73 20 69 74 20 6e 65 65 64 se locks it need
24e10 73 20 74 6f 20 64 6f 20 69 74 73 20 6a 6f 62 2e s to do its job.
24e20 20 20 49 66 20 74 68 65 20 73 74 61 74 65 6d 65 If the stateme
24e30 6e 74 20 69 73 20 61 20 5b 43 4f 4d 4d 49 54 5d nt is a [COMMIT]
24e40 0a 2a 2a 20 6f 72 20 6f 63 63 75 72 73 20 6f 75 .** or occurs ou
24e50 74 73 69 64 65 20 6f 66 20 61 6e 20 65 78 70 6c tside of an expl
24e60 69 63 69 74 20 74 72 61 6e 73 61 63 74 69 6f 6e icit transaction
24e70 2c 20 74 68 65 6e 20 79 6f 75 20 63 61 6e 20 72 , then you can r
24e80 65 74 72 79 20 74 68 65 0a 2a 2a 20 73 74 61 74 etry the.** stat
24e90 65 6d 65 6e 74 2e 20 20 49 66 20 74 68 65 20 73 ement. If the s
24ea0 74 61 74 65 6d 65 6e 74 20 69 73 20 6e 6f 74 20 tatement is not
24eb0 61 20 5b 43 4f 4d 4d 49 54 5d 20 61 6e 64 20 6f a [COMMIT] and o
24ec0 63 63 75 72 73 20 77 69 74 68 69 6e 20 61 0a 2a ccurs within a.*
24ed0 2a 20 65 78 70 6c 69 63 69 74 20 74 72 61 6e 73 * explicit trans
24ee0 61 63 74 69 6f 6e 20 74 68 65 6e 20 79 6f 75 20 action then you
24ef0 73 68 6f 75 6c 64 20 72 6f 6c 6c 62 61 63 6b 20 should rollback
24f00 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
24f10 62 65 66 6f 72 65 0a 2a 2a 20 63 6f 6e 74 69 6e before.** contin
24f20 75 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c uing..**.** [SQL
24f30 49 54 45 5f 44 4f 4e 45 5d 20 6d 65 61 6e 73 20 ITE_DONE] means
24f40 74 68 61 74 20 74 68 65 20 73 74 61 74 65 6d 65 that the stateme
24f50 6e 74 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 nt has finished
24f60 65 78 65 63 75 74 69 6e 67 0a 2a 2a 20 73 75 63 executing.** suc
24f70 63 65 73 73 66 75 6c 6c 79 2e 20 20 73 71 6c 69 cessfully. sqli
24f80 74 65 33 5f 73 74 65 70 28 29 20 73 68 6f 75 6c te3_step() shoul
24f90 64 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 d not be called
24fa0 61 67 61 69 6e 20 6f 6e 20 74 68 69 73 20 76 69 again on this vi
24fb0 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 rtual.** machine
24fc0 20 77 69 74 68 6f 75 74 20 66 69 72 73 74 20 63 without first c
24fd0 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f alling [sqlite3_
24fe0 72 65 73 65 74 28 29 5d 20 74 6f 20 72 65 73 65 reset()] to rese
24ff0 74 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a t the virtual.**
25000 20 6d 61 63 68 69 6e 65 20 62 61 63 6b 20 74 6f machine back to
25010 20 69 74 73 20 69 6e 69 74 69 61 6c 20 73 74 61 its initial sta
25020 74 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 te..**.** If the
25030 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 62 SQL statement b
25040 65 69 6e 67 20 65 78 65 63 75 74 65 64 20 72 65 eing executed re
25050 74 75 72 6e 73 20 61 6e 79 20 64 61 74 61 2c 20 turns any data,
25060 74 68 65 6e 20 5b 53 51 4c 49 54 45 5f 52 4f 57 then [SQLITE_ROW
25070 5d 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 ].** is returned
25080 20 65 61 63 68 20 74 69 6d 65 20 61 20 6e 65 77 each time a new
25090 20 72 6f 77 20 6f 66 20 64 61 74 61 20 69 73 20 row of data is
250a0 72 65 61 64 79 20 66 6f 72 20 70 72 6f 63 65 73 ready for proces
250b0 73 69 6e 67 20 62 79 20 74 68 65 0a 2a 2a 20 63 sing by the.** c
250c0 61 6c 6c 65 72 2e 20 54 68 65 20 76 61 6c 75 65 aller. The value
250d0 73 20 6d 61 79 20 62 65 20 61 63 63 65 73 73 65 s may be accesse
250e0 64 20 75 73 69 6e 67 20 74 68 65 20 5b 63 6f 6c d using the [col
250f0 75 6d 6e 20 61 63 63 65 73 73 20 66 75 6e 63 74 umn access funct
25100 69 6f 6e 73 5d 2e 0a 2a 2a 20 73 71 6c 69 74 65 ions]..** sqlite
25110 33 5f 73 74 65 70 28 29 20 69 73 20 63 61 6c 6c 3_step() is call
25120 65 64 20 61 67 61 69 6e 20 74 6f 20 72 65 74 72 ed again to retr
25130 69 65 76 65 20 74 68 65 20 6e 65 78 74 20 72 6f ieve the next ro
25140 77 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a w of data..**.**
25150 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 [SQLITE_ERROR]
25160 6d 65 61 6e 73 20 74 68 61 74 20 61 20 72 75 6e means that a run
25170 2d 74 69 6d 65 20 65 72 72 6f 72 20 28 73 75 63 -time error (suc
25180 68 20 61 73 20 61 20 63 6f 6e 73 74 72 61 69 6e h as a constrain
25190 74 0a 2a 2a 20 76 69 6f 6c 61 74 69 6f 6e 29 20 t.** violation)
251a0 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20 20 73 has occurred. s
251b0 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 73 68 qlite3_step() sh
251c0 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 61 6c 6c ould not be call
251d0 65 64 20 61 67 61 69 6e 20 6f 6e 0a 2a 2a 20 74 ed again on.** t
251e0 68 65 20 56 4d 2e 20 4d 6f 72 65 20 69 6e 66 6f he VM. More info
251f0 72 6d 61 74 69 6f 6e 20 6d 61 79 20 62 65 20 66 rmation may be f
25200 6f 75 6e 64 20 62 79 20 63 61 6c 6c 69 6e 67 20 ound by calling
25210 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 [sqlite3_errmsg(
25220 29 5d 2e 0a 2a 2a 20 57 69 74 68 20 74 68 65 20 )]..** With the
25230 6c 65 67 61 63 79 20 69 6e 74 65 72 66 61 63 65 legacy interface
25240 2c 20 61 20 6d 6f 72 65 20 73 70 65 63 69 66 69 , a more specifi
25250 63 20 65 72 72 6f 72 20 63 6f 64 65 20 28 66 6f c error code (fo
25260 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 5b 53 r example,.** [S
25270 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 5d QLITE_INTERRUPT]
25280 2c 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 , [SQLITE_SCHEMA
25290 5d 2c 20 5b 53 51 4c 49 54 45 5f 43 4f 52 52 55 ], [SQLITE_CORRU
252a0 50 54 5d 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 PT], and so fort
252b0 68 29 0a 2a 2a 20 63 61 6e 20 62 65 20 6f 62 74 h).** can be obt
252c0 61 69 6e 65 64 20 62 79 20 63 61 6c 6c 69 6e 67 ained by calling
252d0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 [sqlite3_reset(
252e0 29 5d 20 6f 6e 20 74 68 65 0a 2a 2a 20 5b 70 72 )] on the.** [pr
252f0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
25300 5d 2e 20 20 49 6e 20 74 68 65 20 22 76 32 22 20 ]. In the "v2"
25310 69 6e 74 65 72 66 61 63 65 2c 0a 2a 2a 20 74 68 interface,.** th
25320 65 20 6d 6f 72 65 20 73 70 65 63 69 66 69 63 20 e more specific
25330 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 error code is re
25340 74 75 72 6e 65 64 20 64 69 72 65 63 74 6c 79 20 turned directly
25350 62 79 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 by sqlite3_step(
25360 29 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 54 45 )..**.** [SQLITE
25370 5f 4d 49 53 55 53 45 5d 20 6d 65 61 6e 73 20 74 _MISUSE] means t
25380 68 61 74 20 74 68 65 20 74 68 69 73 20 72 6f 75 hat the this rou
25390 74 69 6e 65 20 77 61 73 20 63 61 6c 6c 65 64 20 tine was called
253a0 69 6e 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e inappropriately.
253b0 0a 2a 2a 20 50 65 72 68 61 70 73 20 69 74 20 77 .** Perhaps it w
253c0 61 73 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20 5b as called on a [
253d0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
253e0 6e 74 5d 20 74 68 61 74 20 68 61 73 0a 2a 2a 20 nt] that has.**
253f0 61 6c 72 65 61 64 79 20 62 65 65 6e 20 5b 73 71 already been [sq
25400 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 20 7c lite3_finalize |
25410 20 66 69 6e 61 6c 69 7a 65 64 5d 20 6f 72 20 6f finalized] or o
25420 6e 20 6f 6e 65 20 74 68 61 74 20 68 61 64 0a 2a n one that had.*
25430 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 74 * previously ret
25440 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f 45 52 urned [SQLITE_ER
25450 52 4f 52 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f ROR] or [SQLITE_
25460 44 4f 4e 45 5d 2e 20 20 4f 72 20 69 74 20 63 6f DONE]. Or it co
25470 75 6c 64 0a 2a 2a 20 62 65 20 74 68 65 20 63 61 uld.** be the ca
25480 73 65 20 74 68 61 74 20 74 68 65 20 73 61 6d 65 se that the same
25490 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
254a0 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 75 73 tion is being us
254b0 65 64 20 62 79 20 74 77 6f 20 6f 72 0a 2a 2a 20 ed by two or.**
254c0 6d 6f 72 65 20 74 68 72 65 61 64 73 20 61 74 20 more threads at
254d0 74 68 65 20 73 61 6d 65 20 6d 6f 6d 65 6e 74 20 the same moment
254e0 69 6e 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 3c in time..**.** <
254f0 62 3e 47 6f 6f 66 79 20 49 6e 74 65 72 66 61 63 b>Goofy Interfac
25500 65 20 41 6c 65 72 74 3a 3c 2f 62 3e 20 49 6e 20 e Alert:</b> In
25510 74 68 65 20 6c 65 67 61 63 79 20 69 6e 74 65 72 the legacy inter
25520 66 61 63 65 2c 20 74 68 65 20 73 71 6c 69 74 65 face, the sqlite
25530 33 5f 73 74 65 70 28 29 0a 2a 2a 20 41 50 49 20 3_step().** API
25540 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 61 always returns a
25550 20 67 65 6e 65 72 69 63 20 65 72 72 6f 72 20 63 generic error c
25560 6f 64 65 2c 20 5b 53 51 4c 49 54 45 5f 45 52 52 ode, [SQLITE_ERR
25570 4f 52 5d 2c 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 OR], following a
25580 6e 79 0a 2a 2a 20 65 72 72 6f 72 20 6f 74 68 65 ny.** error othe
25590 72 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f 42 r than [SQLITE_B
255a0 55 53 59 5d 20 61 6e 64 20 5b 53 51 4c 49 54 45 USY] and [SQLITE
255b0 5f 4d 49 53 55 53 45 5d 2e 20 20 59 6f 75 20 6d _MISUSE]. You m
255c0 75 73 74 20 63 61 6c 6c 0a 2a 2a 20 5b 73 71 6c ust call.** [sql
255d0 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 72 ite3_reset()] or
255e0 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 [sqlite3_finali
255f0 7a 65 28 29 5d 20 69 6e 20 6f 72 64 65 72 20 74 ze()] in order t
25600 6f 20 66 69 6e 64 20 6f 6e 65 20 6f 66 20 74 68 o find one of th
25610 65 0a 2a 2a 20 73 70 65 63 69 66 69 63 20 5b 65 e.** specific [e
25620 72 72 6f 72 20 63 6f 64 65 73 5d 20 74 68 61 74 rror codes] that
25630 20 62 65 74 74 65 72 20 64 65 73 63 72 69 62 65 better describe
25640 73 20 74 68 65 20 65 72 72 6f 72 2e 0a 2a 2a 20 s the error..**
25650 57 65 20 61 64 6d 69 74 20 74 68 61 74 20 74 68 We admit that th
25660 69 73 20 69 73 20 61 20 67 6f 6f 66 79 20 64 65 is is a goofy de
25670 73 69 67 6e 2e 20 20 54 68 65 20 70 72 6f 62 6c sign. The probl
25680 65 6d 20 68 61 73 20 62 65 65 6e 20 66 69 78 65 em has been fixe
25690 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 22 76 d.** with the "v
256a0 32 22 20 69 6e 74 65 72 66 61 63 65 2e 20 20 49 2" interface. I
256b0 66 20 79 6f 75 20 70 72 65 70 61 72 65 20 61 6c f you prepare al
256c0 6c 20 6f 66 20 79 6f 75 72 20 53 51 4c 20 73 74 l of your SQL st
256d0 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 75 73 69 6e atements.** usin
256e0 67 20 65 69 74 68 65 72 20 5b 73 71 6c 69 74 65 g either [sqlite
256f0 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 3_prepare_v2()]
25700 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 or [sqlite3_prep
25710 61 72 65 31 36 5f 76 32 28 29 5d 20 69 6e 73 74 are16_v2()] inst
25720 65 61 64 0a 2a 2a 20 6f 66 20 74 68 65 20 6c 65 ead.** of the le
25730 67 61 63 79 20 5b 73 71 6c 69 74 65 33 5f 70 72 gacy [sqlite3_pr
25740 65 70 61 72 65 28 29 5d 20 61 6e 64 20 5b 73 71 epare()] and [sq
25750 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 28 lite3_prepare16(
25760 29 5d 20 69 6e 74 65 72 66 61 63 65 73 2c 0a 2a )] interfaces,.*
25770 2a 20 74 68 65 6e 20 74 68 65 20 6d 6f 72 65 20 * then the more
25780 73 70 65 63 69 66 69 63 20 5b 65 72 72 6f 72 20 specific [error
25790 63 6f 64 65 73 5d 20 61 72 65 20 72 65 74 75 72 codes] are retur
257a0 6e 65 64 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 ned directly.**
257b0 62 79 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 by sqlite3_step(
257c0 29 2e 20 20 54 68 65 20 75 73 65 20 6f 66 20 74 ). The use of t
257d0 68 65 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 he "v2" interfac
257e0 65 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64 e is recommended
257f0 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d ..**.** Requirem
25800 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 32 30 32 ents:.** [H13202
25810 5d 20 5b 48 31 35 33 30 34 5d 20 5b 48 31 35 33 ] [H15304] [H153
25820 30 36 5d 20 5b 48 31 35 33 30 38 5d 20 5b 48 31 06] [H15308] [H1
25830 35 33 31 30 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 5310].*/.SQLITE_
25840 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
25850 73 74 65 70 28 73 71 6c 69 74 65 33 5f 73 74 6d step(sqlite3_stm
25860 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 t*);../*.** CAPI
25870 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 6f 66 20 3REF: Number of
25880 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 72 65 73 columns in a res
25890 75 6c 74 20 73 65 74 20 7b 48 31 33 37 37 30 7d ult set {H13770}
258a0 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S10700>.**.**
258b0 52 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 Returns the numb
258c0 65 72 20 6f 66 20 76 61 6c 75 65 73 20 69 6e 20 er of values in
258d0 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 the current row
258e0 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 of the result se
258f0 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 t..**.** Require
25900 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 37 37 ments:.** [H1377
25910 31 5d 20 5b 48 31 33 37 37 32 5d 0a 2a 2f 0a 53 1] [H13772].*/.S
25920 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
25930 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74 lite3_data_count
25940 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
25950 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 Stmt);../*.** CA
25960 50 49 33 52 45 46 3a 20 46 75 6e 64 61 6d 65 6e PI3REF: Fundamen
25970 74 61 6c 20 44 61 74 61 74 79 70 65 73 20 7b 48 tal Datatypes {H
25980 31 30 32 36 35 7d 20 3c 53 31 30 31 31 30 3e 3c 10265} <S10110><
25990 53 31 30 31 32 30 3e 0a 2a 2a 20 4b 45 59 57 4f S10120>.** KEYWO
259a0 52 44 53 3a 20 53 51 4c 49 54 45 5f 54 45 58 54 RDS: SQLITE_TEXT
259b0 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 32 36 36 7d 20 .**.** {H10266}
259c0 45 76 65 72 79 20 76 61 6c 75 65 20 69 6e 20 53 Every value in S
259d0 51 4c 69 74 65 20 68 61 73 20 6f 6e 65 20 6f 66 QLite has one of
259e0 20 66 69 76 65 20 66 75 6e 64 61 6d 65 6e 74 61 five fundamenta
259f0 6c 20 64 61 74 61 74 79 70 65 73 3a 0a 2a 2a 0a l datatypes:.**.
25a00 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 ** <ul>.** <li>
25a10 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 64-bit signed in
25a20 74 65 67 65 72 0a 2a 2a 20 3c 6c 69 3e 20 36 34 teger.** <li> 64
25a30 2d 62 69 74 20 49 45 45 45 20 66 6c 6f 61 74 69 -bit IEEE floati
25a40 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72 0a ng point number.
25a50 2a 2a 20 3c 6c 69 3e 20 73 74 72 69 6e 67 0a 2a ** <li> string.*
25a60 2a 20 3c 6c 69 3e 20 42 4c 4f 42 0a 2a 2a 20 3c * <li> BLOB.** <
25a70 6c 69 3e 20 4e 55 4c 4c 0a 2a 2a 20 3c 2f 75 6c li> NULL.** </ul
25a80 3e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 > {END}.**.** Th
25a90 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 ese constants ar
25aa0 65 20 63 6f 64 65 73 20 66 6f 72 20 65 61 63 68 e codes for each
25ab0 20 6f 66 20 74 68 6f 73 65 20 74 79 70 65 73 2e of those types.
25ac0 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 .**.** Note that
25ad0 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45 58 54 the SQLITE_TEXT
25ae0 20 63 6f 6e 73 74 61 6e 74 20 77 61 73 20 61 6c constant was al
25af0 73 6f 20 75 73 65 64 20 69 6e 20 53 51 4c 69 74 so used in SQLit
25b00 65 20 76 65 72 73 69 6f 6e 20 32 0a 2a 2a 20 66 e version 2.** f
25b10 6f 72 20 61 20 63 6f 6d 70 6c 65 74 65 6c 79 20 or a completely
25b20 64 69 66 66 65 72 65 6e 74 20 6d 65 61 6e 69 6e different meanin
25b30 67 2e 20 20 53 6f 66 74 77 61 72 65 20 74 68 61 g. Software tha
25b40 74 20 6c 69 6e 6b 73 20 61 67 61 69 6e 73 74 20 t links against
25b50 62 6f 74 68 0a 2a 2a 20 53 51 4c 69 74 65 20 76 both.** SQLite v
25b60 65 72 73 69 6f 6e 20 32 20 61 6e 64 20 53 51 4c ersion 2 and SQL
25b70 69 74 65 20 76 65 72 73 69 6f 6e 20 33 20 73 68 ite version 3 sh
25b80 6f 75 6c 64 20 75 73 65 20 53 51 4c 49 54 45 33 ould use SQLITE3
25b90 5f 54 45 58 54 2c 20 6e 6f 74 0a 2a 2a 20 53 51 _TEXT, not.** SQ
25ba0 4c 49 54 45 5f 54 45 58 54 2e 0a 2a 2f 0a 23 64 LITE_TEXT..*/.#d
25bb0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 efine SQLITE_INT
25bc0 45 47 45 52 20 20 31 0a 23 64 65 66 69 6e 65 20 EGER 1.#define
25bd0 53 51 4c 49 54 45 5f 46 4c 4f 41 54 20 20 20 20 SQLITE_FLOAT
25be0 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 2.#define SQLITE
25bf0 5f 42 4c 4f 42 20 20 20 20 20 34 0a 23 64 65 66 _BLOB 4.#def
25c00 69 6e 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 ine SQLITE_NULL
25c10 20 20 20 20 35 0a 23 69 66 64 65 66 20 53 51 4c 5.#ifdef SQL
25c20 49 54 45 5f 54 45 58 54 0a 23 20 75 6e 64 65 66 ITE_TEXT.# undef
25c30 20 53 51 4c 49 54 45 5f 54 45 58 54 0a 23 65 6c SQLITE_TEXT.#el
25c40 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 se.# define SQLI
25c50 54 45 5f 54 45 58 54 20 20 20 20 20 33 0a 23 65 TE_TEXT 3.#e
25c60 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 53 51 4c ndif.#define SQL
25c70 49 54 45 33 5f 54 45 58 54 20 20 20 20 20 33 0a ITE3_TEXT 3.
25c80 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
25c90 20 52 65 73 75 6c 74 20 56 61 6c 75 65 73 20 46 Result Values F
25ca0 72 6f 6d 20 41 20 51 75 65 72 79 20 7b 48 31 33 rom A Query {H13
25cb0 38 30 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 800} <S10700>.**
25cc0 20 4b 45 59 57 4f 52 44 53 3a 20 7b 63 6f 6c 75 KEYWORDS: {colu
25cd0 6d 6e 20 61 63 63 65 73 73 20 66 75 6e 63 74 69 mn access functi
25ce0 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 ons}.**.** These
25cf0 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 6d 20 74 routines form t
25d00 68 65 20 22 72 65 73 75 6c 74 20 73 65 74 20 71 he "result set q
25d10 75 65 72 79 22 20 69 6e 74 65 72 66 61 63 65 2e uery" interface.
25d20 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 .**.** These rou
25d30 74 69 6e 65 73 20 72 65 74 75 72 6e 20 69 6e 66 tines return inf
25d40 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 ormation about a
25d50 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 6f single column o
25d60 66 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a f the current.**
25d70 20 72 65 73 75 6c 74 20 72 6f 77 20 6f 66 20 61 result row of a
25d80 20 71 75 65 72 79 2e 20 20 49 6e 20 65 76 65 72 query. In ever
25d90 79 20 63 61 73 65 20 74 68 65 20 66 69 72 73 74 y case the first
25da0 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 70 argument is a p
25db0 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 ointer.** to the
25dc0 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 [prepared state
25dd0 6d 65 6e 74 5d 20 74 68 61 74 20 69 73 20 62 65 ment] that is be
25de0 69 6e 67 20 65 76 61 6c 75 61 74 65 64 20 28 74 ing evaluated (t
25df0 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 he [sqlite3_stmt
25e00 2a 5d 0a 2a 2a 20 74 68 61 74 20 77 61 73 20 72 *].** that was r
25e10 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 5b 73 71 eturned from [sq
25e20 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
25e30 28 29 5d 20 6f 72 20 6f 6e 65 20 6f 66 20 69 74 ()] or one of it
25e40 73 20 76 61 72 69 61 6e 74 73 29 0a 2a 2a 20 61 s variants).** a
25e50 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 nd the second ar
25e60 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 69 6e gument is the in
25e70 64 65 78 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d dex of the colum
25e80 6e 20 66 6f 72 20 77 68 69 63 68 20 69 6e 66 6f n for which info
25e90 72 6d 61 74 69 6f 6e 0a 2a 2a 20 73 68 6f 75 6c rmation.** shoul
25ea0 64 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 20 d be returned.
25eb0 54 68 65 20 6c 65 66 74 6d 6f 73 74 20 63 6f 6c The leftmost col
25ec0 75 6d 6e 20 6f 66 20 74 68 65 20 72 65 73 75 6c umn of the resul
25ed0 74 20 73 65 74 20 68 61 73 20 74 68 65 20 69 6e t set has the in
25ee0 64 65 78 20 30 2e 0a 2a 2a 20 54 68 65 20 6e 75 dex 0..** The nu
25ef0 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 mber of columns
25f00 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 63 61 in the result ca
25f10 6e 20 62 65 20 64 65 74 65 72 6d 69 6e 65 64 20 n be determined
25f20 75 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 using.** [sqlite
25f30 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 29 3_column_count()
25f40 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 ]..**.** If the
25f50 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 64 6f SQL statement do
25f60 65 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 es not currently
25f70 20 70 6f 69 6e 74 20 74 6f 20 61 20 76 61 6c 69 point to a vali
25f80 64 20 72 6f 77 2c 20 6f 72 20 69 66 20 74 68 65 d row, or if the
25f90 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 .** column index
25fa0 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 is out of range
25fb0 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 , the result is
25fc0 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 54 68 undefined..** Th
25fd0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d 61 79 ese routines may
25fe0 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 only be called
25ff0 77 68 65 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 when the most re
26000 63 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 cent call to.**
26010 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d [sqlite3_step()]
26020 20 68 61 73 20 72 65 74 75 72 6e 65 64 20 5b 53 has returned [S
26030 51 4c 49 54 45 5f 52 4f 57 5d 20 61 6e 64 20 6e QLITE_ROW] and n
26040 65 69 74 68 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 either.** [sqlit
26050 65 33 5f 72 65 73 65 74 28 29 5d 20 6e 6f 72 20 e3_reset()] nor
26060 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a [sqlite3_finaliz
26070 65 28 29 5d 20 68 61 76 65 20 62 65 65 6e 20 63 e()] have been c
26080 61 6c 6c 65 64 20 73 75 62 73 65 71 75 65 6e 74 alled subsequent
26090 6c 79 2e 0a 2a 2a 20 49 66 20 61 6e 79 20 6f 66 ly..** If any of
260a0 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 these routines
260b0 61 72 65 20 63 61 6c 6c 65 64 20 61 66 74 65 72 are called after
260c0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 [sqlite3_reset(
260d0 29 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 )] or.** [sqlite
260e0 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f 72 3_finalize()] or
260f0 20 61 66 74 65 72 20 5b 73 71 6c 69 74 65 33 5f after [sqlite3_
26100 73 74 65 70 28 29 5d 20 68 61 73 20 72 65 74 75 step()] has retu
26110 72 6e 65 64 0a 2a 2a 20 73 6f 6d 65 74 68 69 6e rned.** somethin
26120 67 20 6f 74 68 65 72 20 74 68 61 6e 20 5b 53 51 g other than [SQ
26130 4c 49 54 45 5f 52 4f 57 5d 2c 20 74 68 65 20 72 LITE_ROW], the r
26140 65 73 75 6c 74 73 20 61 72 65 20 75 6e 64 65 66 esults are undef
26150 69 6e 65 64 2e 0a 2a 2a 20 49 66 20 5b 73 71 6c ined..** If [sql
26160 69 74 65 33 5f 73 74 65 70 28 29 5d 20 6f 72 20 ite3_step()] or
26170 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 [sqlite3_reset()
26180 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 66 69 ] or [sqlite3_fi
26190 6e 61 6c 69 7a 65 28 29 5d 0a 2a 2a 20 61 72 65 nalize()].** are
261a0 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 61 20 64 called from a d
261b0 69 66 66 65 72 65 6e 74 20 74 68 72 65 61 64 20 ifferent thread
261c0 77 68 69 6c 65 20 61 6e 79 20 6f 66 20 74 68 65 while any of the
261d0 73 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 61 se routines.** a
261e0 72 65 20 70 65 6e 64 69 6e 67 2c 20 74 68 65 6e re pending, then
261f0 20 74 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 the results are
26200 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a undefined..**.*
26210 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 6f * The sqlite3_co
26220 6c 75 6d 6e 5f 74 79 70 65 28 29 20 72 6f 75 74 lumn_type() rout
26230 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 0a ine returns the.
26240 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 47 ** [SQLITE_INTEG
26250 45 52 20 7c 20 64 61 74 61 74 79 70 65 20 63 6f ER | datatype co
26260 64 65 5d 20 66 6f 72 20 74 68 65 20 69 6e 69 74 de] for the init
26270 69 61 6c 20 64 61 74 61 20 74 79 70 65 0a 2a 2a ial data type.**
26280 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 63 of the result c
26290 6f 6c 75 6d 6e 2e 20 20 54 68 65 20 72 65 74 75 olumn. The retu
262a0 72 6e 65 64 20 76 61 6c 75 65 20 69 73 20 6f 6e rned value is on
262b0 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 49 4e 54 e of [SQLITE_INT
262c0 45 47 45 52 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 EGER],.** [SQLIT
262d0 45 5f 46 4c 4f 41 54 5d 2c 20 5b 53 51 4c 49 54 E_FLOAT], [SQLIT
262e0 45 5f 54 45 58 54 5d 2c 20 5b 53 51 4c 49 54 45 E_TEXT], [SQLITE
262f0 5f 42 4c 4f 42 5d 2c 20 6f 72 20 5b 53 51 4c 49 _BLOB], or [SQLI
26300 54 45 5f 4e 55 4c 4c 5d 2e 20 20 54 68 65 20 76 TE_NULL]. The v
26310 61 6c 75 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 alue.** returned
26320 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 by sqlite3_colu
26330 6d 6e 5f 74 79 70 65 28 29 20 69 73 20 6f 6e 6c mn_type() is onl
26340 79 20 6d 65 61 6e 69 6e 67 66 75 6c 20 69 66 20 y meaningful if
26350 6e 6f 20 74 79 70 65 0a 2a 2a 20 63 6f 6e 76 65 no type.** conve
26360 72 73 69 6f 6e 73 20 68 61 76 65 20 6f 63 63 75 rsions have occu
26370 72 72 65 64 20 61 73 20 64 65 73 63 72 69 62 65 rred as describe
26380 64 20 62 65 6c 6f 77 2e 20 20 41 66 74 65 72 20 d below. After
26390 61 20 74 79 70 65 20 63 6f 6e 76 65 72 73 69 6f a type conversio
263a0 6e 2c 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 n,.** the value
263b0 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 returned by sqli
263c0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 te3_column_type(
263d0 29 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 20 ) is undefined.
263e0 20 46 75 74 75 72 65 0a 2a 2a 20 76 65 72 73 69 Future.** versi
263f0 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 6d 61 ons of SQLite ma
26400 79 20 63 68 61 6e 67 65 20 74 68 65 20 62 65 68 y change the beh
26410 61 76 69 6f 72 20 6f 66 20 73 71 6c 69 74 65 33 avior of sqlite3
26420 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 0a 2a _column_type().*
26430 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 74 79 * following a ty
26440 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a 2a pe conversion..*
26450 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 73 75 *.** If the resu
26460 6c 74 20 69 73 20 61 20 42 4c 4f 42 20 6f 72 20 lt is a BLOB or
26470 55 54 46 2d 38 20 73 74 72 69 6e 67 20 74 68 65 UTF-8 string the
26480 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f n the sqlite3_co
26490 6c 75 6d 6e 5f 62 79 74 65 73 28 29 0a 2a 2a 20 lumn_bytes().**
264a0 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 routine 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 20 69 6e 20 74 68 61 74 20 42 4c 4f 42 tes in that BLOB
264d0 20 6f 72 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 49 or string..** I
264e0 66 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 f the result is
264f0 61 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 2c a UTF-16 string,
26500 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 63 6f then sqlite3_co
26510 6c 75 6d 6e 5f 62 79 74 65 73 28 29 20 63 6f 6e lumn_bytes() con
26520 76 65 72 74 73 0a 2a 2a 20 74 68 65 20 73 74 72 verts.** the str
26530 69 6e 67 20 74 6f 20 55 54 46 2d 38 20 61 6e 64 ing to UTF-8 and
26540 20 74 68 65 6e 20 72 65 74 75 72 6e 73 20 74 68 then returns th
26550 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 e number of byte
26560 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 73 s..** If the res
26570 75 6c 74 20 69 73 20 61 20 6e 75 6d 65 72 69 63 ult is a numeric
26580 20 76 61 6c 75 65 20 74 68 65 6e 20 73 71 6c 69 value then sqli
26590 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 te3_column_bytes
265a0 28 29 20 75 73 65 73 0a 2a 2a 20 5b 73 71 6c 69 () uses.** [sqli
265b0 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 5d 20 te3_snprintf()]
265c0 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 61 74 20 to convert that
265d0 76 61 6c 75 65 20 74 6f 20 61 20 55 54 46 2d 38 value to a UTF-8
265e0 20 73 74 72 69 6e 67 20 61 6e 64 20 72 65 74 75 string and retu
265f0 72 6e 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 rns.** the numbe
26600 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 r of bytes in th
26610 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 54 68 at string..** Th
26620 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 e value returned
26630 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 does not includ
26640 65 20 74 68 65 20 7a 65 72 6f 20 74 65 72 6d 69 e the zero termi
26650 6e 61 74 6f 72 20 61 74 20 74 68 65 20 65 6e 64 nator at the end
26660 0a 2a 2a 20 6f 66 20 74 68 65 20 73 74 72 69 6e .** of the strin
26670 67 2e 20 20 46 6f 72 20 63 6c 61 72 69 74 79 3a g. For clarity:
26680 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 the value retur
26690 6e 65 64 20 69 73 20 74 68 65 20 6e 75 6d 62 65 ned is the numbe
266a0 72 20 6f 66 0a 2a 2a 20 62 79 74 65 73 20 69 6e r of.** bytes in
266b0 20 74 68 65 20 73 74 72 69 6e 67 2c 20 6e 6f 74 the string, not
266c0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 the number of c
266d0 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a haracters..**.**
266e0 20 53 74 72 69 6e 67 73 20 72 65 74 75 72 6e 65 Strings returne
266f0 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c d by sqlite3_col
26700 75 6d 6e 5f 74 65 78 74 28 29 20 61 6e 64 20 73 umn_text() and s
26710 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 qlite3_column_te
26720 78 74 31 36 28 29 2c 0a 2a 2a 20 65 76 65 6e 20 xt16(),.** even
26730 65 6d 70 74 79 20 73 74 72 69 6e 67 73 2c 20 61 empty strings, a
26740 72 65 20 61 6c 77 61 79 73 20 7a 65 72 6f 20 74 re always zero t
26750 65 72 6d 69 6e 61 74 65 64 2e 20 20 54 68 65 20 erminated. The
26760 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 return.** value
26770 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 63 6f 6c from sqlite3_col
26780 75 6d 6e 5f 62 6c 6f 62 28 29 20 66 6f 72 20 61 umn_blob() for a
26790 20 7a 65 72 6f 2d 6c 65 6e 67 74 68 20 42 4c 4f zero-length BLO
267a0 42 20 69 73 20 61 6e 20 61 72 62 69 74 72 61 72 B is an arbitrar
267b0 79 0a 2a 2a 20 70 6f 69 6e 74 65 72 2c 20 70 6f y.** pointer, po
267c0 73 73 69 62 6c 79 20 65 76 65 6e 20 61 20 4e 55 ssibly even a NU
267d0 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a LL pointer..**.*
267e0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 6f * The sqlite3_co
267f0 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 20 72 lumn_bytes16() r
26800 6f 75 74 69 6e 65 20 69 73 20 73 69 6d 69 6c 61 outine is simila
26810 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c r to sqlite3_col
26820 75 6d 6e 5f 62 79 74 65 73 28 29 0a 2a 2a 20 62 umn_bytes().** b
26830 75 74 20 6c 65 61 76 65 73 20 74 68 65 20 72 65 ut leaves the re
26840 73 75 6c 74 20 69 6e 20 55 54 46 2d 31 36 20 69 sult in UTF-16 i
26850 6e 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 n native byte or
26860 64 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 55 der instead of U
26870 54 46 2d 38 2e 0a 2a 2a 20 54 68 65 20 7a 65 72 TF-8..** The zer
26880 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20 69 73 20 o terminator is
26890 6e 6f 74 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 not included in
268a0 74 68 69 73 20 63 6f 75 6e 74 2e 0a 2a 2a 0a 2a this count..**.*
268b0 2a 20 54 68 65 20 6f 62 6a 65 63 74 20 72 65 74 * The object ret
268c0 75 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74 65 urned by [sqlite
268d0 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 29 3_column_value()
268e0 5d 20 69 73 20 61 6e 0a 2a 2a 20 5b 75 6e 70 72 ] is an.** [unpr
268f0 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f otected sqlite3_
26900 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 2e 20 20 value] object.
26910 41 6e 20 75 6e 70 72 6f 74 65 63 74 65 64 20 73 An unprotected s
26920 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a qlite3_value obj
26930 65 63 74 0a 2a 2a 20 6d 61 79 20 6f 6e 6c 79 20 ect.** may only
26940 62 65 20 75 73 65 64 20 77 69 74 68 20 5b 73 71 be used with [sq
26950 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 lite3_bind_value
26960 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 ()] and [sqlite3
26970 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 29 5d _result_value()]
26980 2e 0a 2a 2a 20 49 66 20 74 68 65 20 5b 75 6e 70 ..** If the [unp
26990 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 rotected sqlite3
269a0 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 72 _value] object r
269b0 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 5b 73 eturned by.** [s
269c0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 qlite3_column_va
269d0 6c 75 65 28 29 5d 20 69 73 20 75 73 65 64 20 69 lue()] is used i
269e0 6e 20 61 6e 79 20 6f 74 68 65 72 20 77 61 79 2c n any other way,
269f0 20 69 6e 63 6c 75 64 69 6e 67 20 63 61 6c 6c 73 including calls
26a00 0a 2a 2a 20 74 6f 20 72 6f 75 74 69 6e 65 73 20 .** to routines
26a10 6c 69 6b 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 like [sqlite3_va
26a20 6c 75 65 5f 69 6e 74 28 29 5d 2c 20 5b 73 71 6c lue_int()], [sql
26a30 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 ite3_value_text(
26a40 29 5d 2c 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 )],.** or [sqlit
26a50 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 29 e3_value_bytes()
26a60 5d 2c 20 74 68 65 6e 20 74 68 65 20 62 65 68 61 ], then the beha
26a70 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 vior is undefine
26a80 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 d..**.** These r
26a90 6f 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74 20 outines attempt
26aa0 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 65 20 76 to convert the v
26ab0 61 6c 75 65 20 77 68 65 72 65 20 61 70 70 72 6f alue where appro
26ac0 70 72 69 61 74 65 2e 20 20 46 6f 72 0a 2a 2a 20 priate. For.**
26ad0 65 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65 20 example, if the
26ae0 69 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65 internal represe
26af0 6e 74 61 74 69 6f 6e 20 69 73 20 46 4c 4f 41 54 ntation is FLOAT
26b00 20 61 6e 64 20 61 20 74 65 78 74 20 72 65 73 75 and a text resu
26b10 6c 74 0a 2a 2a 20 69 73 20 72 65 71 75 65 73 74 lt.** is request
26b20 65 64 2c 20 5b 73 71 6c 69 74 65 33 5f 73 6e 70 ed, [sqlite3_snp
26b30 72 69 6e 74 66 28 29 5d 20 69 73 20 75 73 65 64 rintf()] is used
26b40 20 69 6e 74 65 72 6e 61 6c 6c 79 20 74 6f 20 70 internally to p
26b50 65 72 66 6f 72 6d 20 74 68 65 0a 2a 2a 20 63 6f erform the.** co
26b60 6e 76 65 72 73 69 6f 6e 20 61 75 74 6f 6d 61 74 nversion automat
26b70 69 63 61 6c 6c 79 2e 20 20 54 68 65 20 66 6f 6c ically. The fol
26b80 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 64 65 74 lowing table det
26b90 61 69 6c 73 20 74 68 65 20 63 6f 6e 76 65 72 73 ails the convers
26ba0 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 ions.** that are
26bb0 20 61 70 70 6c 69 65 64 3a 0a 2a 2a 0a 2a 2a 20 applied:.**.**
26bc0 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 20 <blockquote>.**
26bd0 3c 74 61 62 6c 65 20 62 6f 72 64 65 72 3d 22 31 <table border="1
26be0 22 3e 0a 2a 2a 20 3c 74 72 3e 3c 74 68 3e 20 49 ">.** <tr><th> I
26bf0 6e 74 65 72 6e 61 6c 3c 62 72 3e 54 79 70 65 20 nternal<br>Type
26c00 3c 74 68 3e 20 52 65 71 75 65 73 74 65 64 3c 62 <th> Requested<b
26c10 72 3e 54 79 70 65 20 3c 74 68 3e 20 20 43 6f 6e r>Type <th> Con
26c20 76 65 72 73 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74 version.**.** <t
26c30 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c 20 20 20 20 r><td> NULL
26c40 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 20 3c <td> INTEGER <
26c50 74 64 3e 20 52 65 73 75 6c 74 20 69 73 20 30 0a td> Result is 0.
26c60 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 4e 55 4c ** <tr><td> NUL
26c70 4c 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f 41 54 L <td> FLOAT
26c80 20 20 20 20 3c 74 64 3e 20 52 65 73 75 6c 74 20 <td> Result
26c90 69 73 20 30 2e 30 0a 2a 2a 20 3c 74 72 3e 3c 74 is 0.0.** <tr><t
26ca0 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64 3e d> NULL <td>
26cb0 20 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20 TEXT <td>
26cc0 52 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 20 70 Result is NULL p
26cd0 6f 69 6e 74 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 ointer.** <tr><t
26ce0 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64 3e d> NULL <td>
26cf0 20 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 BLOB <td>
26d00 52 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 20 70 Result is NULL p
26d10 6f 69 6e 74 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 ointer.** <tr><t
26d20 64 3e 20 49 4e 54 45 47 45 52 20 20 3c 74 64 3e d> INTEGER <td>
26d30 20 20 46 4c 4f 41 54 20 20 20 20 3c 74 64 3e 20 FLOAT <td>
26d40 43 6f 6e 76 65 72 74 20 66 72 6f 6d 20 69 6e 74 Convert from int
26d50 65 67 65 72 20 74 6f 20 66 6c 6f 61 74 0a 2a 2a eger to float.**
26d60 20 3c 74 72 3e 3c 74 64 3e 20 49 4e 54 45 47 45 <tr><td> INTEGE
26d70 52 20 20 3c 74 64 3e 20 20 20 54 45 58 54 20 20 R <td> TEXT
26d80 20 20 3c 74 64 3e 20 41 53 43 49 49 20 72 65 6e <td> ASCII ren
26d90 64 65 72 69 6e 67 20 6f 66 20 74 68 65 20 69 6e dering of the in
26da0 74 65 67 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 teger.** <tr><td
26db0 3e 20 49 4e 54 45 47 45 52 20 20 3c 74 64 3e 20 > INTEGER <td>
26dc0 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 53 BLOB <td> S
26dd0 61 6d 65 20 61 73 20 49 4e 54 45 47 45 52 2d 3e ame as INTEGER->
26de0 54 45 58 54 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e TEXT.** <tr><td>
26df0 20 20 46 4c 4f 41 54 20 20 20 3c 74 64 3e 20 49 FLOAT <td> I
26e00 4e 54 45 47 45 52 20 20 20 3c 74 64 3e 20 43 6f NTEGER <td> Co
26e10 6e 76 65 72 74 20 66 72 6f 6d 20 66 6c 6f 61 74 nvert from float
26e20 20 74 6f 20 69 6e 74 65 67 65 72 0a 2a 2a 20 3c to integer.** <
26e30 74 72 3e 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 tr><td> FLOAT
26e40 20 3c 74 64 3e 20 20 20 54 45 58 54 20 20 20 20 <td> TEXT
26e50 3c 74 64 3e 20 41 53 43 49 49 20 72 65 6e 64 65 <td> ASCII rende
26e60 72 69 6e 67 20 6f 66 20 74 68 65 20 66 6c 6f 61 ring of the floa
26e70 74 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 46 t.** <tr><td> F
26e80 4c 4f 41 54 20 20 20 3c 74 64 3e 20 20 20 42 4c LOAT <td> BL
26e90 4f 42 20 20 20 20 3c 74 64 3e 20 53 61 6d 65 20 OB <td> Same
26ea0 61 73 20 46 4c 4f 41 54 2d 3e 54 45 58 54 0a 2a as FLOAT->TEXT.*
26eb0 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 54 45 58 54 * <tr><td> TEXT
26ec0 20 20 20 20 3c 74 64 3e 20 49 4e 54 45 47 45 52 <td> INTEGER
26ed0 20 20 20 3c 74 64 3e 20 55 73 65 20 61 74 6f 69 <td> Use atoi
26ee0 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 ().** <tr><td>
26ef0 54 45 58 54 20 20 20 20 3c 74 64 3e 20 20 46 4c TEXT <td> FL
26f00 4f 41 54 20 20 20 20 3c 74 64 3e 20 55 73 65 20 OAT <td> Use
26f10 61 74 6f 66 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 atof().** <tr><t
26f20 64 3e 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e d> TEXT <td>
26f30 20 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 BLOB <td>
26f40 4e 6f 20 63 68 61 6e 67 65 0a 2a 2a 20 3c 74 72 No change.** <tr
26f50 3e 3c 74 64 3e 20 20 42 4c 4f 42 20 20 20 20 3c ><td> BLOB <
26f60 74 64 3e 20 49 4e 54 45 47 45 52 20 20 20 3c 74 td> INTEGER <t
26f70 64 3e 20 43 6f 6e 76 65 72 74 20 74 6f 20 54 45 d> Convert to TE
26f80 58 54 20 74 68 65 6e 20 75 73 65 20 61 74 6f 69 XT then use atoi
26f90 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 ().** <tr><td>
26fa0 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 20 46 4c BLOB <td> FL
26fb0 4f 41 54 20 20 20 20 3c 74 64 3e 20 43 6f 6e 76 OAT <td> Conv
26fc0 65 72 74 20 74 6f 20 54 45 58 54 20 74 68 65 6e ert to TEXT then
26fd0 20 75 73 65 20 61 74 6f 66 28 29 0a 2a 2a 20 3c use atof().** <
26fe0 74 72 3e 3c 74 64 3e 20 20 42 4c 4f 42 20 20 20 tr><td> BLOB
26ff0 20 3c 74 64 3e 20 20 20 54 45 58 54 20 20 20 20 <td> TEXT
27000 3c 74 64 3e 20 41 64 64 20 61 20 7a 65 72 6f 20 <td> Add a zero
27010 74 65 72 6d 69 6e 61 74 6f 72 20 69 66 20 6e 65 terminator if ne
27020 65 64 65 64 0a 2a 2a 20 3c 2f 74 61 62 6c 65 3e eded.** </table>
27030 0a 2a 2a 20 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 .** </blockquote
27040 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c >.**.** The tabl
27050 65 20 61 62 6f 76 65 20 6d 61 6b 65 73 20 72 65 e above makes re
27060 66 65 72 65 6e 63 65 20 74 6f 20 73 74 61 6e 64 ference to stand
27070 61 72 64 20 43 20 6c 69 62 72 61 72 79 20 66 75 ard C library fu
27080 6e 63 74 69 6f 6e 73 20 61 74 6f 69 28 29 0a 2a nctions atoi().*
27090 2a 20 61 6e 64 20 61 74 6f 66 28 29 2e 20 20 53 * and atof(). S
270a0 51 4c 69 74 65 20 64 6f 65 73 20 6e 6f 74 20 72 QLite does not r
270b0 65 61 6c 6c 79 20 75 73 65 20 74 68 65 73 65 20 eally use these
270c0 66 75 6e 63 74 69 6f 6e 73 2e 20 20 49 74 20 68 functions. It h
270d0 61 73 20 69 74 73 0a 2a 2a 20 6f 77 6e 20 65 71 as its.** own eq
270e0 75 69 76 61 6c 65 6e 74 20 69 6e 74 65 72 6e 61 uivalent interna
270f0 6c 20 72 6f 75 74 69 6e 65 73 2e 20 20 54 68 65 l routines. The
27100 20 61 74 6f 69 28 29 20 61 6e 64 20 61 74 6f 66 atoi() and atof
27110 28 29 20 6e 61 6d 65 73 20 61 72 65 0a 2a 2a 20 () names are.**
27120 75 73 65 64 20 69 6e 20 74 68 65 20 74 61 62 6c used in the tabl
27130 65 20 66 6f 72 20 62 72 65 76 69 74 79 20 61 6e e for brevity an
27140 64 20 62 65 63 61 75 73 65 20 74 68 65 79 20 61 d because they a
27150 72 65 20 66 61 6d 69 6c 69 61 72 20 74 6f 20 6d re familiar to m
27160 6f 73 74 0a 2a 2a 20 43 20 70 72 6f 67 72 61 6d ost.** C program
27170 6d 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 mers..**.** Note
27180 20 74 68 61 74 20 77 68 65 6e 20 74 79 70 65 20 that when type
27190 63 6f 6e 76 65 72 73 69 6f 6e 73 20 6f 63 63 75 conversions occu
271a0 72 2c 20 70 6f 69 6e 74 65 72 73 20 72 65 74 75 r, pointers retu
271b0 72 6e 65 64 20 62 79 20 70 72 69 6f 72 0a 2a 2a rned by prior.**
271c0 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 calls to sqlite
271d0 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 3_column_blob(),
271e0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
271f0 74 65 78 74 28 29 2c 20 61 6e 64 2f 6f 72 0a 2a text(), and/or.*
27200 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e * sqlite3_column
27210 5f 74 65 78 74 31 36 28 29 20 6d 61 79 20 62 65 _text16() may be
27220 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2a invalidated..**
27230 20 54 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e Type conversion
27240 73 20 61 6e 64 20 70 6f 69 6e 74 65 72 20 69 6e s and pointer in
27250 76 61 6c 69 64 61 74 69 6f 6e 73 20 6d 69 67 68 validations migh
27260 74 20 6f 63 63 75 72 0a 2a 2a 20 69 6e 20 74 68 t occur.** in th
27270 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 73 65 e following case
27280 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a s:.**.** <ul>.**
27290 20 3c 6c 69 3e 20 54 68 65 20 69 6e 69 74 69 61 <li> The initia
272a0 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 61 20 42 l content is a B
272b0 4c 4f 42 20 61 6e 64 20 73 71 6c 69 74 65 33 5f LOB and sqlite3_
272c0 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 6f 72 column_text() or
272d0 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 .** sqlite3
272e0 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 _column_text16()
272f0 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 41 20 7a is called. A z
27300 65 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 20 6d ero-terminator m
27310 69 67 68 74 0a 2a 2a 20 20 20 20 20 20 6e 65 65 ight.** nee
27320 64 20 74 6f 20 62 65 20 61 64 64 65 64 20 74 6f d to be added to
27330 20 74 68 65 20 73 74 72 69 6e 67 2e 3c 2f 6c 69 the string.</li
27340 3e 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 69 6e >.** <li> The in
27350 69 74 69 61 6c 20 63 6f 6e 74 65 6e 74 20 69 73 itial content is
27360 20 55 54 46 2d 38 20 74 65 78 74 20 61 6e 64 20 UTF-8 text and
27370 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 sqlite3_column_b
27380 79 74 65 73 31 36 28 29 20 6f 72 0a 2a 2a 20 20 ytes16() or.**
27390 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 sqlite3_colu
273a0 6d 6e 5f 74 65 78 74 31 36 28 29 20 69 73 20 63 mn_text16() is c
273b0 61 6c 6c 65 64 2e 20 20 54 68 65 20 63 6f 6e 74 alled. The cont
273c0 65 6e 74 20 6d 75 73 74 20 62 65 20 63 6f 6e 76 ent must be conv
273d0 65 72 74 65 64 0a 2a 2a 20 20 20 20 20 20 74 6f erted.** to
273e0 20 55 54 46 2d 31 36 2e 3c 2f 6c 69 3e 0a 2a 2a UTF-16.</li>.**
273f0 20 3c 6c 69 3e 20 54 68 65 20 69 6e 69 74 69 61 <li> The initia
27400 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 55 54 46 l content is UTF
27410 2d 31 36 20 74 65 78 74 20 61 6e 64 20 73 71 6c -16 text and sql
27420 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 ite3_column_byte
27430 73 28 29 20 6f 72 0a 2a 2a 20 20 20 20 20 20 73 s() or.** s
27440 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 qlite3_column_te
27450 78 74 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 20 xt() is called.
27460 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6d 75 73 The content mus
27470 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 0a 2a t be converted.*
27480 2a 20 20 20 20 20 20 74 6f 20 55 54 46 2d 38 2e * to UTF-8.
27490 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a </li>.** </ul>.*
274a0 2a 0a 2a 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 73 *.** Conversions
274b0 20 62 65 74 77 65 65 6e 20 55 54 46 2d 31 36 62 between UTF-16b
274c0 65 20 61 6e 64 20 55 54 46 2d 31 36 6c 65 20 61 e and UTF-16le a
274d0 72 65 20 61 6c 77 61 79 73 20 64 6f 6e 65 20 69 re always done i
274e0 6e 20 70 6c 61 63 65 20 61 6e 64 20 64 6f 0a 2a n place and do.*
274f0 2a 20 6e 6f 74 20 69 6e 76 61 6c 69 64 61 74 65 * not invalidate
27500 20 61 20 70 72 69 6f 72 20 70 6f 69 6e 74 65 72 a prior pointer
27510 2c 20 74 68 6f 75 67 68 20 6f 66 20 63 6f 75 72 , though of cour
27520 73 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f se the content o
27530 66 20 74 68 65 20 62 75 66 66 65 72 0a 2a 2a 20 f the buffer.**
27540 74 68 61 74 20 74 68 65 20 70 72 69 6f 72 20 70 that the prior p
27550 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 74 6f ointer points to
27560 20 77 69 6c 6c 20 68 61 76 65 20 62 65 65 6e 20 will have been
27570 6d 6f 64 69 66 69 65 64 2e 20 20 4f 74 68 65 72 modified. Other
27580 20 6b 69 6e 64 73 0a 2a 2a 20 6f 66 20 63 6f 6e kinds.** of con
27590 76 65 72 73 69 6f 6e 20 61 72 65 20 64 6f 6e 65 version are done
275a0 20 69 6e 20 70 6c 61 63 65 20 77 68 65 6e 20 69 in place when i
275b0 74 20 69 73 20 70 6f 73 73 69 62 6c 65 2c 20 62 t is possible, b
275c0 75 74 20 73 6f 6d 65 74 69 6d 65 73 20 74 68 65 ut sometimes the
275d0 79 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 70 6f 73 y.** are not pos
275e0 73 69 62 6c 65 20 61 6e 64 20 69 6e 20 74 68 6f sible and in tho
275f0 73 65 20 63 61 73 65 73 20 70 72 69 6f 72 20 70 se cases prior p
27600 6f 69 6e 74 65 72 73 20 61 72 65 20 69 6e 76 61 ointers are inva
27610 6c 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 lidated..**.** T
27620 68 65 20 73 61 66 65 73 74 20 61 6e 64 20 65 61 he safest and ea
27630 73 69 65 73 74 20 74 6f 20 72 65 6d 65 6d 62 65 siest to remembe
27640 72 20 70 6f 6c 69 63 79 20 69 73 20 74 6f 20 69 r policy is to i
27650 6e 76 6f 6b 65 20 74 68 65 73 65 20 72 6f 75 74 nvoke these rout
27660 69 6e 65 73 0a 2a 2a 20 69 6e 20 6f 6e 65 20 6f ines.** in one o
27670 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
27680 77 61 79 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e ways:.**.** <ul>
27690 0a 2a 2a 20 20 3c 6c 69 3e 73 71 6c 69 74 65 33 .** <li>sqlite3
276a0 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 66 _column_text() f
276b0 6f 6c 6c 6f 77 65 64 20 62 79 20 73 71 6c 69 74 ollowed by sqlit
276c0 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 e3_column_bytes(
276d0 29 3c 2f 6c 69 3e 0a 2a 2a 20 20 3c 6c 69 3e 73 )</li>.** <li>s
276e0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c qlite3_column_bl
276f0 6f 62 28 29 20 66 6f 6c 6c 6f 77 65 64 20 62 79 ob() followed by
27700 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
27710 62 79 74 65 73 28 29 3c 2f 6c 69 3e 0a 2a 2a 20 bytes()</li>.**
27720 20 3c 6c 69 3e 73 71 6c 69 74 65 33 5f 63 6f 6c <li>sqlite3_col
27730 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 6f 6c umn_text16() fol
27740 6c 6f 77 65 64 20 62 79 20 73 71 6c 69 74 65 33 lowed by sqlite3
27750 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 _column_bytes16(
27760 29 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a )</li>.** </ul>.
27770 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 **.** In other w
27780 6f 72 64 73 2c 20 79 6f 75 20 73 68 6f 75 6c 64 ords, you should
27790 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 63 6f call sqlite3_co
277a0 6c 75 6d 6e 5f 74 65 78 74 28 29 2c 0a 2a 2a 20 lumn_text(),.**
277b0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 sqlite3_column_b
277c0 6c 6f 62 28 29 2c 20 6f 72 20 73 71 6c 69 74 65 lob(), or sqlite
277d0 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 3_column_text16(
277e0 29 20 66 69 72 73 74 20 74 6f 20 66 6f 72 63 65 ) first to force
277f0 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69 the result.** i
27800 6e 74 6f 20 74 68 65 20 64 65 73 69 72 65 64 20 nto the desired
27810 66 6f 72 6d 61 74 2c 20 74 68 65 6e 20 69 6e 76 format, then inv
27820 6f 6b 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 oke sqlite3_colu
27830 6d 6e 5f 62 79 74 65 73 28 29 20 6f 72 0a 2a 2a mn_bytes() or.**
27840 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
27850 62 79 74 65 73 31 36 28 29 20 74 6f 20 66 69 6e bytes16() to fin
27860 64 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 d the size of th
27870 65 20 72 65 73 75 6c 74 2e 20 20 44 6f 20 6e 6f e result. Do no
27880 74 20 6d 69 78 20 63 61 6c 6c 73 0a 2a 2a 20 74 t mix calls.** t
27890 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e o sqlite3_column
278a0 5f 74 65 78 74 28 29 20 6f 72 20 73 71 6c 69 74 _text() or sqlit
278b0 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 e3_column_blob()
278c0 20 77 69 74 68 20 63 61 6c 6c 73 20 74 6f 0a 2a with calls to.*
278d0 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e * sqlite3_column
278e0 5f 62 79 74 65 73 31 36 28 29 2c 20 61 6e 64 20 _bytes16(), and
278f0 64 6f 20 6e 6f 74 20 6d 69 78 20 63 61 6c 6c 73 do not mix calls
27900 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 to sqlite3_colu
27910 6d 6e 5f 74 65 78 74 31 36 28 29 0a 2a 2a 20 77 mn_text16().** w
27920 69 74 68 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c ith calls to sql
27930 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 ite3_column_byte
27940 73 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 s()..**.** The p
27950 6f 69 6e 74 65 72 73 20 72 65 74 75 72 6e 65 64 ointers returned
27960 20 61 72 65 20 76 61 6c 69 64 20 75 6e 74 69 6c are valid until
27970 20 61 20 74 79 70 65 20 63 6f 6e 76 65 72 73 69 a type conversi
27980 6f 6e 20 6f 63 63 75 72 73 20 61 73 0a 2a 2a 20 on occurs as.**
27990 64 65 73 63 72 69 62 65 64 20 61 62 6f 76 65 2c described above,
279a0 20 6f 72 20 75 6e 74 69 6c 20 5b 73 71 6c 69 74 or until [sqlit
279b0 65 33 5f 73 74 65 70 28 29 5d 20 6f 72 20 5b 73 e3_step()] or [s
279c0 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 qlite3_reset()]
279d0 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 or.** [sqlite3_f
279e0 69 6e 61 6c 69 7a 65 28 29 5d 20 69 73 20 63 61 inalize()] is ca
279f0 6c 6c 65 64 2e 20 20 54 68 65 20 6d 65 6d 6f 72 lled. The memor
27a00 79 20 73 70 61 63 65 20 75 73 65 64 20 74 6f 20 y space used to
27a10 68 6f 6c 64 20 73 74 72 69 6e 67 73 0a 2a 2a 20 hold strings.**
27a20 61 6e 64 20 42 4c 4f 42 73 20 69 73 20 66 72 65 and BLOBs is fre
27a30 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 ed automatically
27a40 2e 20 20 44 6f 20 3c 62 3e 6e 6f 74 3c 2f 62 3e . Do <b>not</b>
27a50 20 70 61 73 73 20 74 68 65 20 70 6f 69 6e 74 65 pass the pointe
27a60 72 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 5b rs returned.** [
27a70 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 sqlite3_column_b
27a80 6c 6f 62 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 lob()], [sqlite3
27a90 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 5d 2c _column_text()],
27aa0 20 65 74 63 2e 20 69 6e 74 6f 0a 2a 2a 20 5b 73 etc. into.** [s
27ab0 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e 0a qlite3_free()]..
27ac0 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72 **.** If a memor
27ad0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 y allocation err
27ae0 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 or occurs during
27af0 20 74 68 65 20 65 76 61 6c 75 61 74 69 6f 6e 20 the evaluation
27b00 6f 66 20 61 6e 79 0a 2a 2a 20 6f 66 20 74 68 65 of any.** of the
27b10 73 65 20 72 6f 75 74 69 6e 65 73 2c 20 61 20 64 se routines, a d
27b20 65 66 61 75 6c 74 20 76 61 6c 75 65 20 69 73 20 efault value is
27b30 72 65 74 75 72 6e 65 64 2e 20 20 54 68 65 20 64 returned. The d
27b40 65 66 61 75 6c 74 20 76 61 6c 75 65 0a 2a 2a 20 efault value.**
27b50 69 73 20 65 69 74 68 65 72 20 74 68 65 20 69 6e is either the in
27b60 74 65 67 65 72 20 30 2c 20 74 68 65 20 66 6c 6f teger 0, the flo
27b70 61 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62 ating point numb
27b80 65 72 20 30 2e 30 2c 20 6f 72 20 61 20 4e 55 4c er 0.0, or a NUL
27b90 4c 0a 2a 2a 20 70 6f 69 6e 74 65 72 2e 20 20 53 L.** pointer. S
27ba0 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 ubsequent calls
27bb0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 to [sqlite3_errc
27bc0 6f 64 65 28 29 5d 20 77 69 6c 6c 20 72 65 74 75 ode()] will retu
27bd0 72 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4e 4f rn.** [SQLITE_NO
27be0 4d 45 4d 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 MEM]..**.** Requ
27bf0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
27c00 33 38 30 33 5d 20 5b 48 31 33 38 30 36 5d 20 5b 3803] [H13806] [
27c10 48 31 33 38 30 39 5d 20 5b 48 31 33 38 31 32 5d H13809] [H13812]
27c20 20 5b 48 31 33 38 31 35 5d 20 5b 48 31 33 38 31 [H13815] [H1381
27c30 38 5d 20 5b 48 31 33 38 32 31 5d 20 5b 48 31 33 8] [H13821] [H13
27c40 38 32 34 5d 0a 2a 2a 20 5b 48 31 33 38 32 37 5d 824].** [H13827]
27c50 20 5b 48 31 33 38 33 30 5d 0a 2a 2f 0a 53 51 4c [H13830].*/.SQL
27c60 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f ITE_API const vo
27c70 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 id *sqlite3_colu
27c80 6d 6e 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f mn_blob(sqlite3_
27c90 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 stmt*, int iCol)
27ca0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
27cb0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
27cc0 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f 73 74 bytes(sqlite3_st
27cd0 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a mt*, int iCol);.
27ce0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
27cf0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 qlite3_column_by
27d00 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f 73 74 tes16(sqlite3_st
27d10 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a mt*, int iCol);.
27d20 53 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62 6c SQLITE_API doubl
27d30 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e e sqlite3_column
27d40 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f _double(sqlite3_
27d50 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 stmt*, int iCol)
27d60 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
27d70 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
27d80 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 int(sqlite3_stmt
27d90 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 *, int iCol);.SQ
27da0 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 LITE_API sqlite3
27db0 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 63 _int64 sqlite3_c
27dc0 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 71 6c 69 olumn_int64(sqli
27dd0 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 te3_stmt*, int i
27de0 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 Col);.SQLITE_API
27df0 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 const unsigned
27e00 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f char *sqlite3_co
27e10 6c 75 6d 6e 5f 74 65 78 74 28 73 71 6c 69 74 65 lumn_text(sqlite
27e20 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 3_stmt*, int iCo
27e30 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 l);.SQLITE_API c
27e40 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 onst void *sqlit
27e50 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 e3_column_text16
27e60 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 (sqlite3_stmt*,
27e70 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 int iCol);.SQLIT
27e80 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
27e90 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 73 71 3_column_type(sq
27ea0 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 lite3_stmt*, int
27eb0 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 iCol);.SQLITE_A
27ec0 50 49 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 PI sqlite3_value
27ed0 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e *sqlite3_column
27ee0 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 73 _value(sqlite3_s
27ef0 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b tmt*, int iCol);
27f00 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
27f10 3a 20 44 65 73 74 72 6f 79 20 41 20 50 72 65 70 : Destroy A Prep
27f20 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20 4f ared Statement O
27f30 62 6a 65 63 74 20 7b 48 31 33 33 30 30 7d 20 3c bject {H13300} <
27f40 53 37 30 33 30 30 3e 3c 53 33 30 31 30 30 3e 0a S70300><S30100>.
27f50 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
27f60 33 5f 66 69 6e 61 6c 69 7a 65 28 29 20 66 75 6e 3_finalize() fun
27f70 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 ction is called
27f80 74 6f 20 64 65 6c 65 74 65 20 61 20 5b 70 72 65 to delete a [pre
27f90 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d pared statement]
27fa0 2e 0a 2a 2a 20 49 66 20 74 68 65 20 73 74 61 74 ..** If the stat
27fb0 65 6d 65 6e 74 20 77 61 73 20 65 78 65 63 75 74 ement was execut
27fc0 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 ed successfully
27fd0 6f 72 20 6e 6f 74 20 65 78 65 63 75 74 65 64 20 or not executed
27fe0 61 74 20 61 6c 6c 2c 20 74 68 65 6e 0a 2a 2a 20 at all, then.**
27ff0 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
28000 75 72 6e 65 64 2e 20 49 66 20 65 78 65 63 75 74 urned. If execut
28010 69 6f 6e 20 6f 66 20 74 68 65 20 73 74 61 74 65 ion of the state
28020 6d 65 6e 74 20 66 61 69 6c 65 64 20 74 68 65 6e ment failed then
28030 20 61 6e 0a 2a 2a 20 5b 65 72 72 6f 72 20 63 6f an.** [error co
28040 64 65 5d 20 6f 72 20 5b 65 78 74 65 6e 64 65 64 de] or [extended
28050 20 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 error code] is
28060 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 returned..**.**
28070 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e This routine can
28080 20 62 65 20 63 61 6c 6c 65 64 20 61 74 20 61 6e be called at an
28090 79 20 70 6f 69 6e 74 20 64 75 72 69 6e 67 20 74 y point during t
280a0 68 65 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 he execution of
280b0 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 the.** [prepared
280c0 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 49 66 statement]. If
280d0 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 the virtual mac
280e0 68 69 6e 65 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 hine has not.**
280f0 63 6f 6d 70 6c 65 74 65 64 20 65 78 65 63 75 74 completed execut
28100 69 6f 6e 20 77 68 65 6e 20 74 68 69 73 20 72 6f ion when this ro
28110 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c utine is called,
28120 20 74 68 61 74 20 69 73 20 6c 69 6b 65 0a 2a 2a that is like.**
28130 20 65 6e 63 6f 75 6e 74 65 72 69 6e 67 20 61 6e encountering an
28140 20 65 72 72 6f 72 20 6f 72 20 61 6e 20 5b 73 71 error or an [sq
28150 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 20 lite3_interrupt
28160 7c 20 69 6e 74 65 72 72 75 70 74 5d 2e 0a 2a 2a | interrupt]..**
28170 20 49 6e 63 6f 6d 70 6c 65 74 65 20 75 70 64 61 Incomplete upda
28180 74 65 73 20 6d 61 79 20 62 65 20 72 6f 6c 6c 65 tes may be rolle
28190 64 20 62 61 63 6b 20 61 6e 64 20 74 72 61 6e 73 d back and trans
281a0 61 63 74 69 6f 6e 73 20 63 61 6e 63 65 6c 65 64 actions canceled
281b0 2c 0a 2a 2a 20 64 65 70 65 6e 64 69 6e 67 20 6f ,.** depending o
281c0 6e 20 74 68 65 20 63 69 72 63 75 6d 73 74 61 6e n the circumstan
281d0 63 65 73 2c 20 61 6e 64 20 74 68 65 0a 2a 2a 20 ces, and the.**
281e0 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 72 65 74 [error code] ret
281f0 75 72 6e 65 64 20 77 69 6c 6c 20 62 65 20 5b 53 urned will be [S
28200 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a 2a QLITE_ABORT]..**
28210 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
28220 3a 0a 2a 2a 20 5b 48 31 31 33 30 32 5d 20 5b 48 :.** [H11302] [H
28230 31 31 33 30 34 5d 0a 2a 2f 0a 53 51 4c 49 54 45 11304].*/.SQLITE
28240 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
28250 5f 66 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 _finalize(sqlite
28260 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 3_stmt *pStmt);.
28270 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
28280 20 52 65 73 65 74 20 41 20 50 72 65 70 61 72 65 Reset A Prepare
28290 64 20 53 74 61 74 65 6d 65 6e 74 20 4f 62 6a 65 d Statement Obje
282a0 63 74 20 7b 48 31 33 33 33 30 7d 20 3c 53 37 30 ct {H13330} <S70
282b0 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 300>.**.** The s
282c0 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 20 66 qlite3_reset() f
282d0 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
282e0 64 20 74 6f 20 72 65 73 65 74 20 61 20 5b 70 72 d to reset a [pr
282f0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
28300 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 62 61 63 6b ].** object back
28310 20 74 6f 20 69 74 73 20 69 6e 69 74 69 61 6c 20 to its initial
28320 73 74 61 74 65 2c 20 72 65 61 64 79 20 74 6f 20 state, ready to
28330 62 65 20 72 65 2d 65 78 65 63 75 74 65 64 2e 0a be re-executed..
28340 2a 2a 20 41 6e 79 20 53 51 4c 20 73 74 61 74 65 ** Any SQL state
28350 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 73 20 74 ment variables t
28360 68 61 74 20 68 61 64 20 76 61 6c 75 65 73 20 62 hat had values b
28370 6f 75 6e 64 20 74 6f 20 74 68 65 6d 20 75 73 69 ound to them usi
28380 6e 67 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 ng.** the [sqlit
28390 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 7c 20 73 e3_bind_blob | s
283a0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 qlite3_bind_*()
283b0 41 50 49 5d 20 72 65 74 61 69 6e 20 74 68 65 69 API] retain thei
283c0 72 20 76 61 6c 75 65 73 2e 0a 2a 2a 20 55 73 65 r values..** Use
283d0 20 5b 73 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f [sqlite3_clear_
283e0 62 69 6e 64 69 6e 67 73 28 29 5d 20 74 6f 20 72 bindings()] to r
283f0 65 73 65 74 20 74 68 65 20 62 69 6e 64 69 6e 67 eset the binding
28400 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 33 32 s..**.** {H11332
28410 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 } The [sqlite3_r
28420 65 73 65 74 28 53 29 5d 20 69 6e 74 65 72 66 61 eset(S)] interfa
28430 63 65 20 72 65 73 65 74 73 20 74 68 65 20 5b 70 ce resets the [p
28440 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
28450 74 5d 20 53 0a 2a 2a 20 20 20 20 20 20 20 20 20 t] S.**
28460 20 62 61 63 6b 20 74 6f 20 74 68 65 20 62 65 67 back to the beg
28470 69 6e 6e 69 6e 67 20 6f 66 20 69 74 73 20 70 72 inning of its pr
28480 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 ogram..**.** {H1
28490 31 33 33 34 7d 20 49 66 20 74 68 65 20 6d 6f 73 1334} If the mos
284a0 74 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f t recent call to
284b0 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 [sqlite3_step(S
284c0 29 5d 20 66 6f 72 20 74 68 65 0a 2a 2a 20 20 20 )] for the.**
284d0 20 20 20 20 20 20 20 5b 70 72 65 70 61 72 65 64 [prepared
284e0 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 72 65 statement] S re
284f0 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f 52 turned [SQLITE_R
28500 4f 57 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 44 OW] or [SQLITE_D
28510 4f 4e 45 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 ONE],.**
28520 20 20 6f 72 20 69 66 20 5b 73 71 6c 69 74 65 33 or if [sqlite3
28530 5f 73 74 65 70 28 53 29 5d 20 68 61 73 20 6e 65 _step(S)] has ne
28540 76 65 72 20 62 65 66 6f 72 65 20 62 65 65 6e 20 ver before been
28550 63 61 6c 6c 65 64 20 6f 6e 20 53 2c 0a 2a 2a 20 called on S,.**
28560 20 20 20 20 20 20 20 20 20 74 68 65 6e 20 5b 73 then [s
28570 71 6c 69 74 65 33 5f 72 65 73 65 74 28 53 29 5d qlite3_reset(S)]
28580 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 returns [SQLITE
28590 5f 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 _OK]..**.** {H11
285a0 33 33 36 7d 20 49 66 20 74 68 65 20 6d 6f 73 74 336} If the most
285b0 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20 recent call to
285c0 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 29 [sqlite3_step(S)
285d0 5d 20 66 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20 ] for the.**
285e0 20 20 20 20 20 20 5b 70 72 65 70 61 72 65 64 20 [prepared
285f0 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 69 6e 64 statement] S ind
28600 69 63 61 74 65 64 20 61 6e 20 65 72 72 6f 72 2c icated an error,
28610 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 then.**
28620 20 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 [sqlite3_reset
28630 28 53 29 5d 20 72 65 74 75 72 6e 73 20 61 6e 20 (S)] returns an
28640 61 70 70 72 6f 70 72 69 61 74 65 20 5b 65 72 72 appropriate [err
28650 6f 72 20 63 6f 64 65 5d 2e 0a 2a 2a 0a 2a 2a 20 or code]..**.**
28660 7b 48 31 31 33 33 38 7d 20 54 68 65 20 5b 73 71 {H11338} The [sq
28670 6c 69 74 65 33 5f 72 65 73 65 74 28 53 29 5d 20 lite3_reset(S)]
28680 69 6e 74 65 72 66 61 63 65 20 64 6f 65 73 20 6e interface does n
28690 6f 74 20 63 68 61 6e 67 65 20 74 68 65 20 76 61 ot change the va
286a0 6c 75 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 lues.**
286b0 20 6f 66 20 61 6e 79 20 5b 73 71 6c 69 74 65 33 of any [sqlite3
286c0 5f 62 69 6e 64 5f 62 6c 6f 62 7c 62 69 6e 64 69 _bind_blob|bindi
286d0 6e 67 73 5d 20 6f 6e 20 74 68 65 20 5b 70 72 65 ngs] on the [pre
286e0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d pared statement]
286f0 20 53 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 S..*/.SQLITE_AP
28700 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72 65 I int sqlite3_re
28710 73 65 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 set(sqlite3_stmt
28720 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a *pStmt);../*.**
28730 20 43 41 50 49 33 52 45 46 3a 20 43 72 65 61 74 CAPI3REF: Creat
28740 65 20 4f 72 20 52 65 64 65 66 69 6e 65 20 53 51 e Or Redefine SQ
28750 4c 20 46 75 6e 63 74 69 6f 6e 73 20 7b 48 31 36 L Functions {H16
28760 31 30 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 100} <S20200>.**
28770 20 4b 45 59 57 4f 52 44 53 3a 20 7b 66 75 6e 63 KEYWORDS: {func
28780 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20 72 6f tion creation ro
28790 75 74 69 6e 65 73 7d 0a 2a 2a 20 4b 45 59 57 4f utines}.** KEYWO
287a0 52 44 53 3a 20 7b 61 70 70 6c 69 63 61 74 69 6f RDS: {applicatio
287b0 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66 75 n-defined SQL fu
287c0 6e 63 74 69 6f 6e 7d 0a 2a 2a 20 4b 45 59 57 4f nction}.** KEYWO
287d0 52 44 53 3a 20 7b 61 70 70 6c 69 63 61 74 69 6f RDS: {applicatio
287e0 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66 75 n-defined SQL fu
287f0 6e 63 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 54 nctions}.**.** T
28800 68 65 73 65 20 74 77 6f 20 66 75 6e 63 74 69 6f hese two functio
28810 6e 73 20 28 63 6f 6c 6c 65 63 74 69 76 65 6c 79 ns (collectively
28820 20 6b 6e 6f 77 6e 20 61 73 20 22 66 75 6e 63 74 known as "funct
28830 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20 72 6f 75 ion creation rou
28840 74 69 6e 65 73 22 29 0a 2a 2a 20 61 72 65 20 75 tines").** are u
28850 73 65 64 20 74 6f 20 61 64 64 20 53 51 4c 20 66 sed to add SQL f
28860 75 6e 63 74 69 6f 6e 73 20 6f 72 20 61 67 67 72 unctions or aggr
28870 65 67 61 74 65 73 20 6f 72 20 74 6f 20 72 65 64 egates or to red
28880 65 66 69 6e 65 20 74 68 65 20 62 65 68 61 76 69 efine the behavi
28890 6f 72 0a 2a 2a 20 6f 66 20 65 78 69 73 74 69 6e or.** of existin
288a0 67 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 g SQL functions
288b0 6f 72 20 61 67 67 72 65 67 61 74 65 73 2e 20 20 or aggregates.
288c0 54 68 65 20 6f 6e 6c 79 20 64 69 66 66 65 72 65 The only differe
288d0 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68 65 0a nce between the.
288e0 2a 2a 20 74 77 6f 20 69 73 20 74 68 61 74 20 74 ** two is that t
288f0 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 he second parame
28900 74 65 72 2c 20 74 68 65 20 6e 61 6d 65 20 6f 66 ter, the name of
28910 20 74 68 65 20 28 73 63 61 6c 61 72 29 20 66 75 the (scalar) fu
28920 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67 67 nction or.** agg
28930 72 65 67 61 74 65 2c 20 69 73 20 65 6e 63 6f 64 regate, is encod
28940 65 64 20 69 6e 20 55 54 46 2d 38 20 66 6f 72 20 ed in UTF-8 for
28950 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 sqlite3_create_f
28960 75 6e 63 74 69 6f 6e 28 29 20 61 6e 64 20 55 54 unction() and UT
28970 46 2d 31 36 0a 2a 2a 20 66 6f 72 20 73 71 6c 69 F-16.** for sqli
28980 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 te3_create_funct
28990 69 6f 6e 31 36 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 ion16()..**.** T
289a0 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 he first paramet
289b0 65 72 20 69 73 20 74 68 65 20 5b 64 61 74 61 62 er is the [datab
289c0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
289d0 74 6f 20 77 68 69 63 68 20 74 68 65 20 53 51 4c to which the SQL
289e0 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 .** function is
289f0 74 6f 20 62 65 20 61 64 64 65 64 2e 20 20 49 66 to be added. If
28a00 20 61 20 73 69 6e 67 6c 65 20 70 72 6f 67 72 61 a single progra
28a10 6d 20 75 73 65 73 20 6d 6f 72 65 20 74 68 61 6e m uses more than
28a20 20 6f 6e 65 20 64 61 74 61 62 61 73 65 0a 2a 2a one database.**
28a30 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 6e 74 65 connection inte
28a40 72 6e 61 6c 6c 79 2c 20 74 68 65 6e 20 53 51 4c rnally, then SQL
28a50 20 66 75 6e 63 74 69 6f 6e 73 20 6d 75 73 74 20 functions must
28a60 62 65 20 61 64 64 65 64 20 69 6e 64 69 76 69 64 be added individ
28a70 75 61 6c 6c 79 20 74 6f 0a 2a 2a 20 65 61 63 68 ually to.** each
28a80 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
28a90 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 tion..**.** The
28aa0 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 second parameter
28ab0 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 is the name of
28ac0 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e the SQL function
28ad0 20 74 6f 20 62 65 20 63 72 65 61 74 65 64 20 6f to be created o
28ae0 72 0a 2a 2a 20 72 65 64 65 66 69 6e 65 64 2e 20 r.** redefined.
28af0 20 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 The length of t
28b00 68 65 20 6e 61 6d 65 20 69 73 20 6c 69 6d 69 74 he name is limit
28b10 65 64 20 74 6f 20 32 35 35 20 62 79 74 65 73 2c ed to 255 bytes,
28b20 20 65 78 63 6c 75 73 69 76 65 20 6f 66 0a 2a 2a exclusive of.**
28b30 20 74 68 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e the zero-termin
28b40 61 74 6f 72 2e 20 20 4e 6f 74 65 20 74 68 61 74 ator. Note that
28b50 20 74 68 65 20 6e 61 6d 65 20 6c 65 6e 67 74 68 the name length
28b60 20 6c 69 6d 69 74 20 69 73 20 69 6e 20 62 79 74 limit is in byt
28b70 65 73 2c 20 6e 6f 74 0a 2a 2a 20 63 68 61 72 61 es, not.** chara
28b80 63 74 65 72 73 2e 20 20 41 6e 79 20 61 74 74 65 cters. Any atte
28b90 6d 70 74 20 74 6f 20 63 72 65 61 74 65 20 61 20 mpt to create a
28ba0 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 61 20 function with a
28bb0 6c 6f 6e 67 65 72 20 6e 61 6d 65 0a 2a 2a 20 77 longer name.** w
28bc0 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 5b 53 ill result in [S
28bd0 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 62 65 69 QLITE_ERROR] bei
28be0 6e 67 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a ng returned..**.
28bf0 2a 2a 20 54 68 65 20 74 68 69 72 64 20 70 61 72 ** The third par
28c00 61 6d 65 74 65 72 20 28 6e 41 72 67 29 0a 2a 2a ameter (nArg).**
28c10 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f is the number o
28c20 66 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61 74 f arguments that
28c30 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f the SQL functio
28c40 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61 74 n or.** aggregat
28c50 65 20 74 61 6b 65 73 2e 20 49 66 20 74 68 69 73 e takes. If this
28c60 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 2d 31 parameter is -1
28c70 2c 20 74 68 65 6e 20 74 68 65 20 53 51 4c 20 66 , then the SQL f
28c80 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67 unction or.** ag
28c90 67 72 65 67 61 74 65 20 6d 61 79 20 74 61 6b 65 gregate may take
28ca0 20 61 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20 61 any number of a
28cb0 72 67 75 6d 65 6e 74 73 20 62 65 74 77 65 65 6e rguments between
28cc0 20 30 20 61 6e 64 20 74 68 65 20 6c 69 6d 69 74 0 and the limit
28cd0 0a 2a 2a 20 73 65 74 20 62 79 20 5b 73 71 6c 69 .** set by [sqli
28ce0 74 65 33 5f 6c 69 6d 69 74 5d 28 5b 53 51 4c 49 te3_limit]([SQLI
28cf0 54 45 5f 4c 49 4d 49 54 5f 46 55 4e 43 54 49 4f TE_LIMIT_FUNCTIO
28d00 4e 5f 41 52 47 5d 29 2e 20 20 49 66 20 74 68 65 N_ARG]). If the
28d10 20 74 68 69 72 64 0a 2a 2a 20 70 61 72 61 6d 65 third.** parame
28d20 74 65 72 20 69 73 20 6c 65 73 73 20 74 68 61 6e ter is less than
28d30 20 2d 31 20 6f 72 20 67 72 65 61 74 65 72 20 74 -1 or greater t
28d40 68 61 6e 20 31 32 37 20 74 68 65 6e 20 74 68 65 han 127 then the
28d50 20 62 65 68 61 76 69 6f 72 20 69 73 0a 2a 2a 20 behavior is.**
28d60 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a undefined..**.**
28d70 20 54 68 65 20 66 6f 75 72 74 68 20 70 61 72 61 The fourth para
28d80 6d 65 74 65 72 2c 20 65 54 65 78 74 52 65 70 2c meter, eTextRep,
28d90 20 73 70 65 63 69 66 69 65 73 20 77 68 61 74 0a specifies what.
28da0 2a 2a 20 5b 53 51 4c 49 54 45 5f 55 54 46 38 20 ** [SQLITE_UTF8
28db0 7c 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 5d | text encoding]
28dc0 20 74 68 69 73 20 53 51 4c 20 66 75 6e 63 74 69 this SQL functi
28dd0 6f 6e 20 70 72 65 66 65 72 73 20 66 6f 72 0a 2a on prefers for.*
28de0 2a 20 69 74 73 20 70 61 72 61 6d 65 74 65 72 73 * its parameters
28df0 2e 20 20 41 6e 79 20 53 51 4c 20 66 75 6e 63 74 . Any SQL funct
28e00 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ion implementati
28e10 6f 6e 20 73 68 6f 75 6c 64 20 62 65 20 61 62 6c on should be abl
28e20 65 20 74 6f 20 77 6f 72 6b 0a 2a 2a 20 77 6f 72 e to work.** wor
28e30 6b 20 77 69 74 68 20 55 54 46 2d 38 2c 20 55 54 k with UTF-8, UT
28e40 46 2d 31 36 6c 65 2c 20 6f 72 20 55 54 46 2d 31 F-16le, or UTF-1
28e50 36 62 65 2e 20 20 42 75 74 20 73 6f 6d 65 20 69 6be. But some i
28e60 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6d mplementations m
28e70 61 79 20 62 65 0a 2a 2a 20 6d 6f 72 65 20 65 66 ay be.** more ef
28e80 66 69 63 69 65 6e 74 20 77 69 74 68 20 6f 6e 65 ficient with one
28e90 20 65 6e 63 6f 64 69 6e 67 20 74 68 61 6e 20 61 encoding than a
28ea0 6e 6f 74 68 65 72 2e 20 20 41 6e 20 61 70 70 6c nother. An appl
28eb0 69 63 61 74 69 6f 6e 20 6d 61 79 0a 2a 2a 20 69 ication may.** i
28ec0 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f 63 72 nvoke sqlite3_cr
28ed0 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 20 eate_function()
28ee0 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 or sqlite3_creat
28ef0 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 20 6d e_function16() m
28f00 75 6c 74 69 70 6c 65 0a 2a 2a 20 74 69 6d 65 73 ultiple.** times
28f10 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 66 with the same f
28f20 75 6e 63 74 69 6f 6e 20 62 75 74 20 77 69 74 68 unction but with
28f30 20 64 69 66 66 65 72 65 6e 74 20 76 61 6c 75 65 different value
28f40 73 20 6f 66 20 65 54 65 78 74 52 65 70 2e 0a 2a s of eTextRep..*
28f50 2a 20 57 68 65 6e 20 6d 75 6c 74 69 70 6c 65 20 * When multiple
28f60 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 implementations
28f70 6f 66 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63 of the same func
28f80 74 69 6f 6e 20 61 72 65 20 61 76 61 69 6c 61 62 tion are availab
28f90 6c 65 2c 20 53 51 4c 69 74 65 0a 2a 2a 20 77 69 le, SQLite.** wi
28fa0 6c 6c 20 70 69 63 6b 20 74 68 65 20 6f 6e 65 20 ll pick the one
28fb0 74 68 61 74 20 69 6e 76 6f 6c 76 65 73 20 74 68 that involves th
28fc0 65 20 6c 65 61 73 74 20 61 6d 6f 75 6e 74 20 6f e least amount o
28fd0 66 20 64 61 74 61 20 63 6f 6e 76 65 72 73 69 6f f data conversio
28fe0 6e 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 n..** If there i
28ff0 73 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 s only a single
29000 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 implementation w
29010 68 69 63 68 20 64 6f 65 73 20 6e 6f 74 20 63 61 hich does not ca
29020 72 65 20 77 68 61 74 20 74 65 78 74 0a 2a 2a 20 re what text.**
29030 65 6e 63 6f 64 69 6e 67 20 69 73 20 75 73 65 64 encoding is used
29040 2c 20 74 68 65 6e 20 74 68 65 20 66 6f 75 72 74 , then the fourt
29050 68 20 61 72 67 75 6d 65 6e 74 20 73 68 6f 75 6c h argument shoul
29060 64 20 62 65 20 5b 53 51 4c 49 54 45 5f 41 4e 59 d be [SQLITE_ANY
29070 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 66 ]..**.** The fif
29080 74 68 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 th parameter is
29090 61 6e 20 61 72 62 69 74 72 61 72 79 20 70 6f 69 an arbitrary poi
290a0 6e 74 65 72 2e 20 20 54 68 65 20 69 6d 70 6c 65 nter. The imple
290b0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 mentation of the
290c0 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e .** function can
290d0 20 67 61 69 6e 20 61 63 63 65 73 73 20 74 6f 20 gain access to
290e0 74 68 69 73 20 70 6f 69 6e 74 65 72 20 75 73 69 this pointer usi
290f0 6e 67 20 5b 73 71 6c 69 74 65 33 5f 75 73 65 72 ng [sqlite3_user
29100 5f 64 61 74 61 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 _data()]..**.**
29110 54 68 65 20 73 65 76 65 6e 74 68 2c 20 65 69 67 The seventh, eig
29120 68 74 68 20 61 6e 64 20 6e 69 6e 74 68 20 70 61 hth and ninth pa
29130 72 61 6d 65 74 65 72 73 2c 20 78 46 75 6e 63 2c rameters, xFunc,
29140 20 78 53 74 65 70 20 61 6e 64 20 78 46 69 6e 61 xStep and xFina
29150 6c 2c 20 61 72 65 0a 2a 2a 20 70 6f 69 6e 74 65 l, are.** pointe
29160 72 73 20 74 6f 20 43 2d 6c 61 6e 67 75 61 67 65 rs to C-language
29170 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 functions that
29180 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 implement the SQ
29190 4c 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a L function or.**
291a0 20 61 67 67 72 65 67 61 74 65 2e 20 41 20 73 63 aggregate. A sc
291b0 61 6c 61 72 20 53 51 4c 20 66 75 6e 63 74 69 6f alar SQL functio
291c0 6e 20 72 65 71 75 69 72 65 73 20 61 6e 20 69 6d n requires an im
291d0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
291e0 74 68 65 20 78 46 75 6e 63 0a 2a 2a 20 63 61 6c the xFunc.** cal
291f0 6c 62 61 63 6b 20 6f 6e 6c 79 2c 20 4e 55 4c 4c lback only, NULL
29200 20 70 6f 69 6e 74 65 72 73 20 73 68 6f 75 6c 64 pointers should
29210 20 62 65 20 70 61 73 73 65 64 20 61 73 20 74 68 be passed as th
29220 65 20 78 53 74 65 70 20 61 6e 64 20 78 46 69 6e e xStep and xFin
29230 61 6c 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 al.** parameters
29240 2e 20 41 6e 20 61 67 67 72 65 67 61 74 65 20 53 . An aggregate S
29250 51 4c 20 66 75 6e 63 74 69 6f 6e 20 72 65 71 75 QL function requ
29260 69 72 65 73 20 61 6e 20 69 6d 70 6c 65 6d 65 6e ires an implemen
29270 74 61 74 69 6f 6e 20 6f 66 20 78 53 74 65 70 0a tation of xStep.
29280 2a 2a 20 61 6e 64 20 78 46 69 6e 61 6c 20 61 6e ** and xFinal an
29290 64 20 4e 55 4c 4c 20 73 68 6f 75 6c 64 20 62 65 d NULL should be
292a0 20 70 61 73 73 65 64 20 66 6f 72 20 78 46 75 6e passed for xFun
292b0 63 2e 20 54 6f 20 64 65 6c 65 74 65 20 61 6e 20 c. To delete an
292c0 65 78 69 73 74 69 6e 67 0a 2a 2a 20 53 51 4c 20 existing.** SQL
292d0 66 75 6e 63 74 69 6f 6e 20 6f 72 20 61 67 67 72 function or aggr
292e0 65 67 61 74 65 2c 20 70 61 73 73 20 4e 55 4c 4c egate, pass NULL
292f0 20 66 6f 72 20 61 6c 6c 20 74 68 72 65 65 20 66 for all three f
29300 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b unction callback
29310 73 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 70 s..**.** It is p
29320 65 72 6d 69 74 74 65 64 20 74 6f 20 72 65 67 69 ermitted to regi
29330 73 74 65 72 20 6d 75 6c 74 69 70 6c 65 20 69 6d ster multiple im
29340 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 plementations of
29350 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 66 75 6e the same.** fun
29360 63 74 69 6f 6e 73 20 77 69 74 68 20 74 68 65 20 ctions with the
29370 73 61 6d 65 20 6e 61 6d 65 20 62 75 74 20 77 69 same name but wi
29380 74 68 20 65 69 74 68 65 72 20 64 69 66 66 65 72 th either differ
29390 69 6e 67 20 6e 75 6d 62 65 72 73 20 6f 66 0a 2a ing numbers of.*
293a0 2a 20 61 72 67 75 6d 65 6e 74 73 20 6f 72 20 64 * arguments or d
293b0 69 66 66 65 72 69 6e 67 20 70 72 65 66 65 72 72 iffering preferr
293c0 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 ed text encoding
293d0 73 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 s. SQLite will
293e0 75 73 65 0a 2a 2a 20 74 68 65 20 69 6d 70 6c 65 use.** the imple
293f0 6d 65 6e 74 61 74 69 6f 6e 20 74 68 61 74 20 6d mentation that m
29400 6f 73 74 20 63 6c 6f 73 65 6c 79 20 6d 61 74 63 ost closely matc
29410 68 65 73 20 74 68 65 20 77 61 79 20 69 6e 20 77 hes the way in w
29420 68 69 63 68 20 74 68 65 0a 2a 2a 20 53 51 4c 20 hich the.** SQL
29430 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 function is used
29440 2e 20 20 41 20 66 75 6e 63 74 69 6f 6e 20 69 6d . A function im
29450 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69 74 plementation wit
29460 68 20 61 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 h a non-negative
29470 0a 2a 2a 20 6e 41 72 67 20 70 61 72 61 6d 65 74 .** nArg paramet
29480 65 72 20 69 73 20 61 20 62 65 74 74 65 72 20 6d er is a better m
29490 61 74 63 68 20 74 68 61 6e 20 61 20 66 75 6e 63 atch than a func
294a0 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 tion implementat
294b0 69 6f 6e 20 77 69 74 68 0a 2a 2a 20 61 20 6e 65 ion with.** a ne
294c0 67 61 74 69 76 65 20 6e 41 72 67 2e 20 20 41 20 gative nArg. A
294d0 66 75 6e 63 74 69 6f 6e 20 77 68 65 72 65 20 74 function where t
294e0 68 65 20 70 72 65 66 65 72 72 65 64 20 74 65 78 he preferred tex
294f0 74 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 6d 61 t encoding.** ma
29500 74 63 68 65 73 20 74 68 65 20 64 61 74 61 62 61 tches the databa
29510 73 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 61 se encoding is a
29520 20 62 65 74 74 65 72 0a 2a 2a 20 6d 61 74 63 68 better.** match
29530 20 74 68 61 6e 20 61 20 66 75 6e 63 74 69 6f 6e than a function
29540 20 77 68 65 72 65 20 74 68 65 20 65 6e 63 6f 64 where the encod
29550 69 6e 67 20 69 73 20 64 69 66 66 65 72 65 6e 74 ing is different
29560 2e 20 20 0a 2a 2a 20 41 20 66 75 6e 63 74 69 6f . .** A functio
29570 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e 63 6f n where the enco
29580 64 69 6e 67 20 64 69 66 66 65 72 65 6e 63 65 20 ding difference
29590 69 73 20 62 65 74 77 65 65 6e 20 55 54 46 31 36 is between UTF16
295a0 6c 65 20 61 6e 64 20 55 54 46 31 36 62 65 0a 2a le and UTF16be.*
295b0 2a 20 69 73 20 61 20 63 6c 6f 73 65 72 20 6d 61 * is a closer ma
295c0 74 63 68 20 74 68 61 6e 20 61 20 66 75 6e 63 74 tch than a funct
295d0 69 6f 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e ion where the en
295e0 63 6f 64 69 6e 67 20 64 69 66 66 65 72 65 6e 63 coding differenc
295f0 65 20 69 73 0a 2a 2a 20 62 65 74 77 65 65 6e 20 e is.** between
29600 55 54 46 38 20 61 6e 64 20 55 54 46 31 36 2e 0a UTF8 and UTF16..
29610 2a 2a 0a 2a 2a 20 42 75 69 6c 74 2d 69 6e 20 66 **.** Built-in f
29620 75 6e 63 74 69 6f 6e 73 20 6d 61 79 20 62 65 20 unctions may be
29630 6f 76 65 72 6c 6f 61 64 65 64 20 62 79 20 6e 65 overloaded by ne
29640 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 w application-de
29650 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 2e fined functions.
29660 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 70 .** The first ap
29670 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 plication-define
29680 64 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 d function with
29690 61 20 67 69 76 65 6e 20 6e 61 6d 65 20 6f 76 65 a given name ove
296a0 72 72 69 64 65 73 20 61 6c 6c 0a 2a 2a 20 62 75 rrides all.** bu
296b0 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f 6e 73 ilt-in functions
296c0 20 69 6e 20 74 68 65 20 73 61 6d 65 20 5b 64 61 in the same [da
296d0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
296e0 6e 5d 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 n] with the same
296f0 20 6e 61 6d 65 2e 0a 2a 2a 20 53 75 62 73 65 71 name..** Subseq
29700 75 65 6e 74 20 61 70 70 6c 69 63 61 74 69 6f 6e uent application
29710 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f -defined functio
29720 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 6e ns of the same n
29730 61 6d 65 20 6f 6e 6c 79 20 6f 76 65 72 72 69 64 ame only overrid
29740 65 20 0a 2a 2a 20 70 72 69 6f 72 20 61 70 70 6c e .** prior appl
29750 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 ication-defined
29760 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 61 functions that a
29770 72 65 20 61 6e 20 65 78 61 63 74 20 6d 61 74 63 re an exact matc
29780 68 20 66 6f 72 20 74 68 65 0a 2a 2a 20 6e 75 6d h for the.** num
29790 62 65 72 20 6f 66 20 70 61 72 61 6d 65 74 65 72 ber of parameter
297a0 73 20 61 6e 64 20 70 72 65 66 65 72 72 65 64 20 s and preferred
297b0 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 encoding..**.**
297c0 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 An application-d
297d0 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 efined function
297e0 69 73 20 70 65 72 6d 69 74 74 65 64 20 74 6f 20 is permitted to
297f0 63 61 6c 6c 20 6f 74 68 65 72 0a 2a 2a 20 53 51 call other.** SQ
29800 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 73 2e Lite interfaces.
29810 20 20 48 6f 77 65 76 65 72 2c 20 73 75 63 68 20 However, such
29820 63 61 6c 6c 73 20 6d 75 73 74 20 6e 6f 74 0a 2a calls must not.*
29830 2a 20 63 6c 6f 73 65 20 74 68 65 20 64 61 74 61 * close the data
29840 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
29850 6e 6f 72 20 66 69 6e 61 6c 69 7a 65 20 6f 72 20 nor finalize or
29860 72 65 73 65 74 20 74 68 65 20 70 72 65 70 61 72 reset the prepar
29870 65 64 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 ed.** statement
29880 69 6e 20 77 68 69 63 68 20 74 68 65 20 66 75 6e in which the fun
29890 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67 ction is running
298a0 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d ..**.** Requirem
298b0 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 31 30 33 ents:.** [H16103
298c0 5d 20 5b 48 31 36 31 30 36 5d 20 5b 48 31 36 31 ] [H16106] [H161
298d0 30 39 5d 20 5b 48 31 36 31 31 32 5d 20 5b 48 31 09] [H16112] [H1
298e0 36 31 31 38 5d 20 5b 48 31 36 31 32 31 5d 20 5b 6118] [H16121] [
298f0 48 31 36 31 32 37 5d 0a 2a 2a 20 5b 48 31 36 31 H16127].** [H161
29900 33 30 5d 20 5b 48 31 36 31 33 33 5d 20 5b 48 31 30] [H16133] [H1
29910 36 31 33 36 5d 20 5b 48 31 36 31 33 39 5d 20 5b 6136] [H16139] [
29920 48 31 36 31 34 32 5d 0a 2a 2f 0a 53 51 4c 49 54 H16142].*/.SQLIT
29930 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
29940 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 3_create_functio
29950 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 n(. sqlite3 *db
29960 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ,. const char *
29970 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a 20 zFunctionName,.
29980 20 69 6e 74 20 6e 41 72 67 2c 0a 20 20 69 6e 74 int nArg,. int
29990 20 65 54 65 78 74 52 65 70 2c 0a 20 20 76 6f 69 eTextRep,. voi
299a0 64 20 2a 70 41 70 70 2c 0a 20 20 76 6f 69 64 20 d *pApp,. void
299b0 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 (*xFunc)(sqlite3
299c0 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 _context*,int,sq
299d0 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a lite3_value**),.
299e0 20 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 void (*xStep)(
299f0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a sqlite3_context*
29a00 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c ,int,sqlite3_val
29a10 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a ue**),. void (*
29a20 78 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f xFinal)(sqlite3_
29a30 63 6f 6e 74 65 78 74 2a 29 0a 29 3b 0a 53 51 4c context*).);.SQL
29a40 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
29a50 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 te3_create_funct
29a60 69 6f 6e 31 36 28 0a 20 20 73 71 6c 69 74 65 33 ion16(. sqlite3
29a70 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20 76 6f *db,. const vo
29a80 69 64 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d id *zFunctionNam
29a90 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67 2c 0a 20 e,. int nArg,.
29aa0 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 0a 20 int eTextRep,.
29ab0 20 76 6f 69 64 20 2a 70 41 70 70 2c 0a 20 20 76 void *pApp,. v
29ac0 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c oid (*xFunc)(sql
29ad0 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e ite3_context*,in
29ae0 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a t,sqlite3_value*
29af0 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 53 74 *),. void (*xSt
29b00 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 ep)(sqlite3_cont
29b10 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 ext*,int,sqlite3
29b20 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 _value**),. voi
29b30 64 20 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c 69 d (*xFinal)(sqli
29b40 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 0a 29 3b te3_context*).);
29b50 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
29b60 3a 20 54 65 78 74 20 45 6e 63 6f 64 69 6e 67 73 : Text Encodings
29b70 20 7b 48 31 30 32 36 37 7d 20 3c 53 35 30 32 30 {H10267} <S5020
29b80 30 3e 20 3c 48 31 36 31 30 30 3e 0a 2a 2a 0a 2a 0> <H16100>.**.*
29b90 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 * These constant
29ba0 20 64 65 66 69 6e 65 20 69 6e 74 65 67 65 72 20 define integer
29bb0 63 6f 64 65 73 20 74 68 61 74 20 72 65 70 72 65 codes that repre
29bc0 73 65 6e 74 20 74 68 65 20 76 61 72 69 6f 75 73 sent the various
29bd0 0a 2a 2a 20 74 65 78 74 20 65 6e 63 6f 64 69 6e .** text encodin
29be0 67 73 20 73 75 70 70 6f 72 74 65 64 20 62 79 20 gs supported by
29bf0 53 51 4c 69 74 65 2e 0a 2a 2f 0a 23 64 65 66 69 SQLite..*/.#defi
29c00 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 38 20 20 ne SQLITE_UTF8
29c10 20 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 1.#defi
29c20 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c ne SQLITE_UTF16L
29c30 45 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 E 2.#defi
29c40 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 ne SQLITE_UTF16B
29c50 45 20 20 20 20 20 20 20 20 33 0a 23 64 65 66 69 E 3.#defi
29c60 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 20 ne SQLITE_UTF16
29c70 20 20 20 20 20 20 20 20 20 34 20 20 20 20 2f 2a 4 /*
29c80 20 55 73 65 20 6e 61 74 69 76 65 20 62 79 74 65 Use native byte
29c90 20 6f 72 64 65 72 20 2a 2f 0a 23 64 65 66 69 6e order */.#defin
29ca0 65 20 53 51 4c 49 54 45 5f 41 4e 59 20 20 20 20 e SQLITE_ANY
29cb0 20 20 20 20 20 20 20 20 35 20 20 20 20 2f 2a 20 5 /*
29cc0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 sqlite3_create_f
29cd0 75 6e 63 74 69 6f 6e 20 6f 6e 6c 79 20 2a 2f 0a unction only */.
29ce0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 #define SQLITE_U
29cf0 54 46 31 36 5f 41 4c 49 47 4e 45 44 20 20 38 20 TF16_ALIGNED 8
29d00 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 63 72 /* sqlite3_cr
29d10 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 20 6f eate_collation o
29d20 6e 6c 79 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 nly */../*.** CA
29d30 50 49 33 52 45 46 3a 20 44 65 70 72 65 63 61 74 PI3REF: Deprecat
29d40 65 64 20 46 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 ed Functions.**
29d50 44 45 50 52 45 43 41 54 45 44 0a 2a 2a 0a 2a 2a DEPRECATED.**.**
29d60 20 54 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 These functions
29d70 20 61 72 65 20 5b 64 65 70 72 65 63 61 74 65 64 are [deprecated
29d80 5d 2e 20 20 49 6e 20 6f 72 64 65 72 20 74 6f 20 ]. In order to
29d90 6d 61 69 6e 74 61 69 6e 0a 2a 2a 20 62 61 63 6b maintain.** back
29da0 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c wards compatibil
29db0 69 74 79 20 77 69 74 68 20 6f 6c 64 65 72 20 63 ity with older c
29dc0 6f 64 65 2c 20 74 68 65 73 65 20 66 75 6e 63 74 ode, these funct
29dd0 69 6f 6e 73 20 63 6f 6e 74 69 6e 75 65 20 0a 2a ions continue .*
29de0 2a 20 74 6f 20 62 65 20 73 75 70 70 6f 72 74 65 * to be supporte
29df0 64 2e 20 20 48 6f 77 65 76 65 72 2c 20 6e 65 77 d. However, new
29e00 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 73 68 applications sh
29e10 6f 75 6c 64 20 61 76 6f 69 64 0a 2a 2a 20 74 68 ould avoid.** th
29e20 65 20 75 73 65 20 6f 66 20 74 68 65 73 65 20 66 e use of these f
29e30 75 6e 63 74 69 6f 6e 73 2e 20 20 54 6f 20 68 65 unctions. To he
29e40 6c 70 20 65 6e 63 6f 75 72 61 67 65 20 70 65 6f lp encourage peo
29e50 70 6c 65 20 74 6f 20 61 76 6f 69 64 0a 2a 2a 20 ple to avoid.**
29e60 75 73 69 6e 67 20 74 68 65 73 65 20 66 75 6e 63 using these func
29e70 74 69 6f 6e 73 2c 20 77 65 20 61 72 65 20 6e 6f tions, we are no
29e80 74 20 67 6f 69 6e 67 20 74 6f 20 74 65 6c 6c 20 t going to tell
29e90 79 6f 75 20 77 68 61 74 20 74 68 65 79 20 64 6f you what they do
29ea0 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
29eb0 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 ITE_OMIT_DEPRECA
29ec0 54 45 44 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 TED.SQLITE_API S
29ed0 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 44 QLITE_DEPRECATED
29ee0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 67 67 int sqlite3_agg
29ef0 72 65 67 61 74 65 5f 63 6f 75 6e 74 28 73 71 6c regate_count(sql
29f00 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a ite3_context*);.
29f10 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 SQLITE_API SQLIT
29f20 45 5f 44 45 50 52 45 43 41 54 45 44 20 69 6e 74 E_DEPRECATED int
29f30 20 73 71 6c 69 74 65 33 5f 65 78 70 69 72 65 64 sqlite3_expired
29f40 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b (sqlite3_stmt*);
29f50 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 .SQLITE_API SQLI
29f60 54 45 5f 44 45 50 52 45 43 41 54 45 44 20 69 6e TE_DEPRECATED in
29f70 74 20 73 71 6c 69 74 65 33 5f 74 72 61 6e 73 66 t sqlite3_transf
29f80 65 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c 69 er_bindings(sqli
29f90 74 65 33 5f 73 74 6d 74 2a 2c 20 73 71 6c 69 74 te3_stmt*, sqlit
29fa0 65 33 5f 73 74 6d 74 2a 29 3b 0a 53 51 4c 49 54 e3_stmt*);.SQLIT
29fb0 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 E_API SQLITE_DEP
29fc0 52 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69 RECATED int sqli
29fd0 74 65 33 5f 67 6c 6f 62 61 6c 5f 72 65 63 6f 76 te3_global_recov
29fe0 65 72 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 er(void);.SQLITE
29ff0 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 52 _API SQLITE_DEPR
2a000 45 43 41 54 45 44 20 76 6f 69 64 20 73 71 6c 69 ECATED void sqli
2a010 74 65 33 5f 74 68 72 65 61 64 5f 63 6c 65 61 6e te3_thread_clean
2a020 75 70 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 up(void);.SQLITE
2a030 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 52 _API SQLITE_DEPR
2a040 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69 74 ECATED int sqlit
2a050 65 33 5f 6d 65 6d 6f 72 79 5f 61 6c 61 72 6d 28 e3_memory_alarm(
2a060 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c 73 71 void(*)(void*,sq
2a070 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e 74 29 lite3_int64,int)
2a080 2c 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 5f 69 ,void*,sqlite3_i
2a090 6e 74 36 34 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f nt64);.#endif../
2a0a0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f *.** CAPI3REF: O
2a0b0 62 74 61 69 6e 69 6e 67 20 53 51 4c 20 46 75 6e btaining SQL Fun
2a0c0 63 74 69 6f 6e 20 50 61 72 61 6d 65 74 65 72 20 ction Parameter
2a0d0 56 61 6c 75 65 73 20 7b 48 31 35 31 30 30 7d 20 Values {H15100}
2a0e0 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S20200>.**.** T
2a0f0 68 65 20 43 2d 6c 61 6e 67 75 61 67 65 20 69 6d he C-language im
2a100 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
2a110 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e SQL functions an
2a120 64 20 61 67 67 72 65 67 61 74 65 73 20 75 73 65 d aggregates use
2a130 73 0a 2a 2a 20 74 68 69 73 20 73 65 74 20 6f 66 s.** this set of
2a140 20 69 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69 interface routi
2a150 6e 65 73 20 74 6f 20 61 63 63 65 73 73 20 74 68 nes to access th
2a160 65 20 70 61 72 61 6d 65 74 65 72 20 76 61 6c 75 e parameter valu
2a170 65 73 20 6f 6e 0a 2a 2a 20 74 68 65 20 66 75 6e es on.** the fun
2a180 63 74 69 6f 6e 20 6f 72 20 61 67 67 72 65 67 61 ction or aggrega
2a190 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 46 te..**.** The xF
2a1a0 75 6e 63 20 28 66 6f 72 20 73 63 61 6c 61 72 20 unc (for scalar
2a1b0 66 75 6e 63 74 69 6f 6e 73 29 20 6f 72 20 78 53 functions) or xS
2a1c0 74 65 70 20 28 66 6f 72 20 61 67 67 72 65 67 61 tep (for aggrega
2a1d0 74 65 73 29 20 70 61 72 61 6d 65 74 65 72 73 0a tes) parameters.
2a1e0 2a 2a 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 ** to [sqlite3_c
2a1f0 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 reate_function()
2a200 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 ] and [sqlite3_c
2a210 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 reate_function16
2a220 28 29 5d 0a 2a 2a 20 64 65 66 69 6e 65 20 63 61 ()].** define ca
2a230 6c 6c 62 61 63 6b 73 20 74 68 61 74 20 69 6d 70 llbacks that imp
2a240 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 4c 20 66 lement the SQL f
2a250 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 61 67 67 unctions and agg
2a260 72 65 67 61 74 65 73 2e 0a 2a 2a 20 54 68 65 20 regates..** The
2a270 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 4th parameter to
2a280 20 74 68 65 73 65 20 63 61 6c 6c 62 61 63 6b 73 these callbacks
2a290 20 69 73 20 61 6e 20 61 72 72 61 79 20 6f 66 20 is an array of
2a2a0 70 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20 5b pointers to.** [
2a2b0 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 protected sqlite
2a2c0 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 73 3_value] objects
2a2d0 2e 20 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20 . There is one
2a2e0 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 [sqlite3_value]
2a2f0 6f 62 6a 65 63 74 20 66 6f 72 0a 2a 2a 20 65 61 object for.** ea
2a300 63 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 ch parameter to
2a310 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e the SQL function
2a320 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 . These routine
2a330 73 20 61 72 65 20 75 73 65 64 20 74 6f 0a 2a 2a s are used to.**
2a340 20 65 78 74 72 61 63 74 20 76 61 6c 75 65 73 20 extract values
2a350 66 72 6f 6d 20 74 68 65 20 5b 73 71 6c 69 74 65 from the [sqlite
2a360 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 73 3_value] objects
2a370 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f ..**.** These ro
2a380 75 74 69 6e 65 73 20 77 6f 72 6b 20 6f 6e 6c 79 utines work only
2a390 20 77 69 74 68 20 5b 70 72 6f 74 65 63 74 65 64 with [protected
2a3a0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 sqlite3_value]
2a3b0 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 41 6e 79 20 objects..** Any
2a3c0 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20 74 attempt to use t
2a3d0 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6f 6e hese routines on
2a3e0 20 61 6e 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 an [unprotected
2a3f0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 0a sqlite3_value].
2a400 2a 2a 20 6f 62 6a 65 63 74 20 72 65 73 75 6c 74 ** object result
2a410 73 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 20 62 s in undefined b
2a420 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 ehavior..**.** T
2a430 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 77 6f hese routines wo
2a440 72 6b 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 rk just like the
2a450 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 5b corresponding [
2a460 63 6f 6c 75 6d 6e 20 61 63 63 65 73 73 20 66 75 column access fu
2a470 6e 63 74 69 6f 6e 73 5d 0a 2a 2a 20 65 78 63 65 nctions].** exce
2a480 70 74 20 74 68 61 74 20 20 74 68 65 73 65 20 72 pt that these r
2a490 6f 75 74 69 6e 65 73 20 74 61 6b 65 20 61 20 73 outines take a s
2a4a0 69 6e 67 6c 65 20 5b 70 72 6f 74 65 63 74 65 64 ingle [protected
2a4b0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 sqlite3_value]
2a4c0 6f 62 6a 65 63 74 0a 2a 2a 20 70 6f 69 6e 74 65 object.** pointe
2a4d0 72 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20 5b r instead of a [
2a4e0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 5d 20 70 sqlite3_stmt*] p
2a4f0 6f 69 6e 74 65 72 20 61 6e 64 20 61 6e 20 69 6e ointer and an in
2a500 74 65 67 65 72 20 63 6f 6c 75 6d 6e 20 6e 75 6d teger column num
2a510 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ber..**.** The s
2a520 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
2a530 74 31 36 28 29 20 69 6e 74 65 72 66 61 63 65 20 t16() interface
2a540 65 78 74 72 61 63 74 73 20 61 20 55 54 46 2d 31 extracts a UTF-1
2a550 36 20 73 74 72 69 6e 67 0a 2a 2a 20 69 6e 20 74 6 string.** in t
2a560 68 65 20 6e 61 74 69 76 65 20 62 79 74 65 2d 6f he native byte-o
2a570 72 64 65 72 20 6f 66 20 74 68 65 20 68 6f 73 74 rder of the host
2a580 20 6d 61 63 68 69 6e 65 2e 20 20 54 68 65 0a 2a machine. The.*
2a590 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f * sqlite3_value_
2a5a0 74 65 78 74 31 36 62 65 28 29 20 61 6e 64 20 73 text16be() and s
2a5b0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
2a5c0 74 31 36 6c 65 28 29 20 69 6e 74 65 72 66 61 63 t16le() interfac
2a5d0 65 73 0a 2a 2a 20 65 78 74 72 61 63 74 20 55 54 es.** extract UT
2a5e0 46 2d 31 36 20 73 74 72 69 6e 67 73 20 61 73 20 F-16 strings as
2a5f0 62 69 67 2d 65 6e 64 69 61 6e 20 61 6e 64 20 6c big-endian and l
2a600 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 72 65 73 ittle-endian res
2a610 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 0a 2a 2a pectively..**.**
2a620 20 54 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c The sqlite3_val
2a630 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 ue_numeric_type(
2a640 29 20 69 6e 74 65 72 66 61 63 65 20 61 74 74 65 ) interface atte
2a650 6d 70 74 73 20 74 6f 20 61 70 70 6c 79 0a 2a 2a mpts to apply.**
2a660 20 6e 75 6d 65 72 69 63 20 61 66 66 69 6e 69 74 numeric affinit
2a670 79 20 74 6f 20 74 68 65 20 76 61 6c 75 65 2e 20 y to the value.
2a680 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 This means that
2a690 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 0a 2a an attempt is.*
2a6a0 2a 20 6d 61 64 65 20 74 6f 20 63 6f 6e 76 65 72 * made to conver
2a6b0 74 20 74 68 65 20 76 61 6c 75 65 20 74 6f 20 61 t the value to a
2a6c0 6e 20 69 6e 74 65 67 65 72 20 6f 72 20 66 6c 6f n integer or flo
2a6d0 61 74 69 6e 67 20 70 6f 69 6e 74 2e 20 20 49 66 ating point. If
2a6e0 0a 2a 2a 20 73 75 63 68 20 61 20 63 6f 6e 76 65 .** such a conve
2a6f0 72 73 69 6f 6e 20 69 73 20 70 6f 73 73 69 62 6c rsion is possibl
2a700 65 20 77 69 74 68 6f 75 74 20 6c 6f 73 73 20 6f e without loss o
2a710 66 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 28 69 f information (i
2a720 6e 20 6f 74 68 65 72 0a 2a 2a 20 77 6f 72 64 73 n other.** words
2a730 2c 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 , if the value i
2a740 73 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 s a string that
2a750 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 6e 75 6d looks like a num
2a760 62 65 72 29 0a 2a 2a 20 74 68 65 6e 20 74 68 65 ber).** then the
2a770 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 conversion is p
2a780 65 72 66 6f 72 6d 65 64 2e 20 20 4f 74 68 65 72 erformed. Other
2a790 77 69 73 65 20 6e 6f 20 63 6f 6e 76 65 72 73 69 wise no conversi
2a7a0 6f 6e 20 6f 63 63 75 72 73 2e 0a 2a 2a 20 54 68 on occurs..** Th
2a7b0 65 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 e [SQLITE_INTEGE
2a7c0 52 20 7c 20 64 61 74 61 74 79 70 65 5d 20 61 66 R | datatype] af
2a7d0 74 65 72 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 ter conversion i
2a7e0 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a s returned..**.*
2a7f0 2a 20 50 6c 65 61 73 65 20 70 61 79 20 70 61 72 * Please pay par
2a800 74 69 63 75 6c 61 72 20 61 74 74 65 6e 74 69 6f ticular attentio
2a810 6e 20 74 6f 20 74 68 65 20 66 61 63 74 20 74 68 n to the fact th
2a820 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 72 at the pointer r
2a830 65 74 75 72 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 eturned.** from
2a840 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 [sqlite3_value_b
2a850 6c 6f 62 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 lob()], [sqlite3
2a860 5f 76 61 6c 75 65 5f 74 65 78 74 28 29 5d 2c 20 _value_text()],
2a870 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76 or.** [sqlite3_v
2a880 61 6c 75 65 5f 74 65 78 74 31 36 28 29 5d 20 63 alue_text16()] c
2a890 61 6e 20 62 65 20 69 6e 76 61 6c 69 64 61 74 65 an be invalidate
2a8a0 64 20 62 79 20 61 20 73 75 62 73 65 71 75 65 6e d by a subsequen
2a8b0 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 t call to.** [sq
2a8c0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 lite3_value_byte
2a8d0 73 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 76 s()], [sqlite3_v
2a8e0 61 6c 75 65 5f 62 79 74 65 73 31 36 28 29 5d 2c alue_bytes16()],
2a8f0 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f [sqlite3_value_
2a900 74 65 78 74 28 29 5d 2c 0a 2a 2a 20 6f 72 20 5b text()],.** or [
2a910 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
2a920 78 74 31 36 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 xt16()]..**.** T
2a930 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d 75 hese routines mu
2a940 73 74 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f st be called fro
2a950 6d 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 m the same threa
2a960 64 20 61 73 0a 2a 2a 20 74 68 65 20 53 51 4c 20 d as.** the SQL
2a970 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 73 75 function that su
2a980 70 70 6c 69 65 64 20 74 68 65 20 5b 73 71 6c 69 pplied the [sqli
2a990 74 65 33 5f 76 61 6c 75 65 2a 5d 20 70 61 72 61 te3_value*] para
2a9a0 6d 65 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 meters..**.** Re
2a9b0 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b quirements:.** [
2a9c0 48 31 35 31 30 33 5d 20 5b 48 31 35 31 30 36 5d H15103] [H15106]
2a9d0 20 5b 48 31 35 31 30 39 5d 20 5b 48 31 35 31 31 [H15109] [H1511
2a9e0 32 5d 20 5b 48 31 35 31 31 35 5d 20 5b 48 31 35 2] [H15115] [H15
2a9f0 31 31 38 5d 20 5b 48 31 35 31 32 31 5d 20 5b 48 118] [H15121] [H
2aa00 31 35 31 32 34 5d 0a 2a 2a 20 5b 48 31 35 31 32 15124].** [H1512
2aa10 37 5d 20 5b 48 31 35 31 33 30 5d 20 5b 48 31 35 7] [H15130] [H15
2aa20 31 33 33 5d 20 5b 48 31 35 31 33 36 5d 0a 2a 2f 133] [H15136].*/
2aa30 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
2aa40 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f t void *sqlite3_
2aa50 76 61 6c 75 65 5f 62 6c 6f 62 28 73 71 6c 69 74 value_blob(sqlit
2aa60 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 e3_value*);.SQLI
2aa70 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
2aa80 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 73 e3_value_bytes(s
2aa90 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a qlite3_value*);.
2aaa0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
2aab0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 qlite3_value_byt
2aac0 65 73 31 36 28 73 71 6c 69 74 65 33 5f 76 61 6c es16(sqlite3_val
2aad0 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 ue*);.SQLITE_API
2aae0 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 33 5f double sqlite3_
2aaf0 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 73 71 6c value_double(sql
2ab00 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 ite3_value*);.SQ
2ab10 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
2ab20 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 73 ite3_value_int(s
2ab30 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a qlite3_value*);.
2ab40 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 SQLITE_API sqlit
2ab50 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 e3_int64 sqlite3
2ab60 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 73 71 6c _value_int64(sql
2ab70 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 ite3_value*);.SQ
2ab80 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 75 LITE_API const u
2ab90 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 73 71 nsigned char *sq
2aba0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
2abb0 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 (sqlite3_value*)
2abc0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e ;.SQLITE_API con
2abd0 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 st void *sqlite3
2abe0 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 73 71 _value_text16(sq
2abf0 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 lite3_value*);.S
2ac00 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
2ac10 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 void *sqlite3_va
2ac20 6c 75 65 5f 74 65 78 74 31 36 6c 65 28 73 71 6c lue_text16le(sql
2ac30 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 ite3_value*);.SQ
2ac40 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 LITE_API const v
2ac50 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c oid *sqlite3_val
2ac60 75 65 5f 74 65 78 74 31 36 62 65 28 73 71 6c 69 ue_text16be(sqli
2ac70 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c te3_value*);.SQL
2ac80 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
2ac90 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 73 te3_value_type(s
2aca0 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a qlite3_value*);.
2acb0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
2acc0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d qlite3_value_num
2acd0 65 72 69 63 5f 74 79 70 65 28 73 71 6c 69 74 65 eric_type(sqlite
2ace0 33 5f 76 61 6c 75 65 2a 29 3b 0a 0a 2f 2a 0a 2a 3_value*);../*.*
2acf0 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 62 74 61 * CAPI3REF: Obta
2ad00 69 6e 20 41 67 67 72 65 67 61 74 65 20 46 75 6e in Aggregate Fun
2ad10 63 74 69 6f 6e 20 43 6f 6e 74 65 78 74 20 7b 48 ction Context {H
2ad20 31 36 32 31 30 7d 20 3c 53 32 30 32 30 30 3e 0a 16210} <S20200>.
2ad30 2a 2a 0a 2a 2a 20 54 68 65 20 69 6d 70 6c 65 6d **.** The implem
2ad40 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 67 67 72 entation of aggr
2ad50 65 67 61 74 65 20 53 51 4c 20 66 75 6e 63 74 69 egate SQL functi
2ad60 6f 6e 73 20 75 73 65 20 74 68 69 73 20 72 6f 75 ons use this rou
2ad70 74 69 6e 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 tine to allocate
2ad80 0a 2a 2a 20 61 20 73 74 72 75 63 74 75 72 65 20 .** a structure
2ad90 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 68 65 69 for storing thei
2ada0 72 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 r state..**.** T
2adb0 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 he first time th
2adc0 65 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 e sqlite3_aggreg
2add0 61 74 65 5f 63 6f 6e 74 65 78 74 28 29 20 72 6f ate_context() ro
2ade0 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 utine is called
2adf0 66 6f 72 20 61 0a 2a 2a 20 70 61 72 74 69 63 75 for a.** particu
2ae00 6c 61 72 20 61 67 67 72 65 67 61 74 65 2c 20 53 lar aggregate, S
2ae10 51 4c 69 74 65 20 61 6c 6c 6f 63 61 74 65 73 20 QLite allocates
2ae20 6e 42 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 nBytes of memory
2ae30 2c 20 7a 65 72 6f 65 73 20 6f 75 74 20 74 68 61 , zeroes out tha
2ae40 74 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 61 6e 64 t.** memory, and
2ae50 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 returns a point
2ae60 65 72 20 74 6f 20 69 74 2e 20 4f 6e 20 73 65 63 er to it. On sec
2ae70 6f 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 ond and subseque
2ae80 6e 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 nt calls to.** s
2ae90 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 qlite3_aggregate
2aea0 5f 63 6f 6e 74 65 78 74 28 29 20 66 6f 72 20 74 _context() for t
2aeb0 68 65 20 73 61 6d 65 20 61 67 67 72 65 67 61 74 he same aggregat
2aec0 65 20 66 75 6e 63 74 69 6f 6e 20 69 6e 64 65 78 e function index
2aed0 2c 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 62 75 ,.** the same bu
2aee0 66 66 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 ffer is returned
2aef0 2e 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 . The implementa
2af00 74 69 6f 6e 20 6f 66 20 74 68 65 20 61 67 67 72 tion of the aggr
2af10 65 67 61 74 65 20 63 61 6e 20 75 73 65 0a 2a 2a egate can use.**
2af20 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 62 75 the returned bu
2af30 66 66 65 72 20 74 6f 20 61 63 63 75 6d 75 6c 61 ffer to accumula
2af40 74 65 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 53 te data..**.** S
2af50 51 4c 69 74 65 20 61 75 74 6f 6d 61 74 69 63 61 QLite automatica
2af60 6c 6c 79 20 66 72 65 65 73 20 74 68 65 20 61 6c lly frees the al
2af70 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 20 77 located buffer w
2af80 68 65 6e 20 74 68 65 20 61 67 67 72 65 67 61 74 hen the aggregat
2af90 65 0a 2a 2a 20 71 75 65 72 79 20 63 6f 6e 63 6c e.** query concl
2afa0 75 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 udes..**.** The
2afb0 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 first parameter
2afc0 73 68 6f 75 6c 64 20 62 65 20 61 20 63 6f 70 79 should be a copy
2afd0 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 of the.** [sqli
2afe0 74 65 33 5f 63 6f 6e 74 65 78 74 20 7c 20 53 51 te3_context | SQ
2aff0 4c 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 65 L function conte
2b000 78 74 5d 20 74 68 61 74 20 69 73 20 74 68 65 20 xt] that is the
2b010 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 0a first parameter.
2b020 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 ** to the callba
2b030 63 6b 20 72 6f 75 74 69 6e 65 20 74 68 61 74 20 ck routine that
2b040 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 61 implements the a
2b050 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f ggregate functio
2b060 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f n..**.** This ro
2b070 75 74 69 6e 65 20 6d 75 73 74 20 62 65 20 63 61 utine must be ca
2b080 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 73 61 lled from the sa
2b090 6d 65 20 74 68 72 65 61 64 20 69 6e 20 77 68 69 me thread in whi
2b0a0 63 68 0a 2a 2a 20 74 68 65 20 61 67 67 72 65 67 ch.** the aggreg
2b0b0 61 74 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e ate SQL function
2b0c0 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a is running..**.
2b0d0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
2b0e0 0a 2a 2a 20 5b 48 31 36 32 31 31 5d 20 5b 48 31 .** [H16211] [H1
2b0f0 36 32 31 33 5d 20 5b 48 31 36 32 31 35 5d 20 5b 6213] [H16215] [
2b100 48 31 36 32 31 37 5d 0a 2a 2f 0a 53 51 4c 49 54 H16217].*/.SQLIT
2b110 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 E_API void *sqli
2b120 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f te3_aggregate_co
2b130 6e 74 65 78 74 28 73 71 6c 69 74 65 33 5f 63 6f ntext(sqlite3_co
2b140 6e 74 65 78 74 2a 2c 20 69 6e 74 20 6e 42 79 74 ntext*, int nByt
2b150 65 73 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 es);../*.** CAPI
2b160 33 52 45 46 3a 20 55 73 65 72 20 44 61 74 61 20 3REF: User Data
2b170 46 6f 72 20 46 75 6e 63 74 69 6f 6e 73 20 7b 48 For Functions {H
2b180 31 36 32 34 30 7d 20 3c 53 32 30 32 30 30 3e 0a 16240} <S20200>.
2b190 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
2b1a0 33 5f 75 73 65 72 5f 64 61 74 61 28 29 20 69 6e 3_user_data() in
2b1b0 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 terface returns
2b1c0 61 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65 a copy of.** the
2b1d0 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20 77 61 pointer that wa
2b1e0 73 20 74 68 65 20 70 55 73 65 72 44 61 74 61 20 s the pUserData
2b1f0 70 61 72 61 6d 65 74 65 72 20 28 74 68 65 20 35 parameter (the 5
2b200 74 68 20 70 61 72 61 6d 65 74 65 72 29 0a 2a 2a th parameter).**
2b210 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 of the [sqlite3
2b220 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e _create_function
2b230 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71 6c 69 ()].** and [sqli
2b240 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 te3_create_funct
2b250 69 6f 6e 31 36 28 29 5d 20 72 6f 75 74 69 6e 65 ion16()] routine
2b260 73 20 74 68 61 74 20 6f 72 69 67 69 6e 61 6c 6c s that originall
2b270 79 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 64 20 y.** registered
2b280 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 the application
2b290 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e defined function
2b2a0 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 . {END}.**.** Th
2b2b0 69 73 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 is routine must
2b2c0 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 be called from t
2b2d0 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 69 he same thread i
2b2e0 6e 20 77 68 69 63 68 0a 2a 2a 20 74 68 65 20 61 n which.** the a
2b2f0 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e pplication-defin
2b300 65 64 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 ed function is r
2b310 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 unning..**.** Re
2b320 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b quirements:.** [
2b330 48 31 36 32 34 33 5d 0a 2a 2f 0a 53 51 4c 49 54 H16243].*/.SQLIT
2b340 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 E_API void *sqli
2b350 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 73 71 te3_user_data(sq
2b360 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b lite3_context*);
2b370 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
2b380 3a 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 : Database Conne
2b390 63 74 69 6f 6e 20 46 6f 72 20 46 75 6e 63 74 69 ction For Functi
2b3a0 6f 6e 73 20 7b 48 31 36 32 35 30 7d 20 3c 53 36 ons {H16250} <S6
2b3b0 30 36 30 30 3e 3c 53 32 30 32 30 30 3e 0a 2a 2a 0600><S20200>.**
2b3c0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
2b3d0 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c context_db_handl
2b3e0 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 e() interface re
2b3f0 74 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66 0a turns a copy of.
2b400 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 ** the pointer t
2b410 6f 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 o the [database
2b420 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 74 68 65 connection] (the
2b430 20 31 73 74 20 70 61 72 61 6d 65 74 65 72 29 0a 1st parameter).
2b440 2a 2a 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 ** of the [sqlit
2b450 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 e3_create_functi
2b460 6f 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71 on()].** and [sq
2b470 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e lite3_create_fun
2b480 63 74 69 6f 6e 31 36 28 29 5d 20 72 6f 75 74 69 ction16()] routi
2b490 6e 65 73 20 74 68 61 74 20 6f 72 69 67 69 6e 61 nes that origina
2b4a0 6c 6c 79 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 lly.** registere
2b4b0 64 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f d the applicatio
2b4c0 6e 20 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 n defined functi
2b4d0 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 on..**.** Requir
2b4e0 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 32 ements:.** [H162
2b4f0 35 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 53].*/.SQLITE_AP
2b500 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 I sqlite3 *sqlit
2b510 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 e3_context_db_ha
2b520 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e ndle(sqlite3_con
2b530 74 65 78 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 text*);../*.** C
2b540 41 50 49 33 52 45 46 3a 20 46 75 6e 63 74 69 6f API3REF: Functio
2b550 6e 20 41 75 78 69 6c 69 61 72 79 20 44 61 74 61 n Auxiliary Data
2b560 20 7b 48 31 36 32 37 30 7d 20 3c 53 32 30 32 30 {H16270} <S2020
2b570 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 0>.**.** The fol
2b580 6c 6f 77 69 6e 67 20 74 77 6f 20 66 75 6e 63 74 lowing two funct
2b590 69 6f 6e 73 20 6d 61 79 20 62 65 20 75 73 65 64 ions may be used
2b5a0 20 62 79 20 73 63 61 6c 61 72 20 53 51 4c 20 66 by scalar SQL f
2b5b0 75 6e 63 74 69 6f 6e 73 20 74 6f 0a 2a 2a 20 61 unctions to.** a
2b5c0 73 73 6f 63 69 61 74 65 20 6d 65 74 61 64 61 74 ssociate metadat
2b5d0 61 20 77 69 74 68 20 61 72 67 75 6d 65 6e 74 20 a with argument
2b5e0 76 61 6c 75 65 73 2e 20 49 66 20 74 68 65 20 73 values. If the s
2b5f0 61 6d 65 20 76 61 6c 75 65 20 69 73 20 70 61 73 ame value is pas
2b600 73 65 64 20 74 6f 0a 2a 2a 20 6d 75 6c 74 69 70 sed to.** multip
2b610 6c 65 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6f le invocations o
2b620 66 20 74 68 65 20 73 61 6d 65 20 53 51 4c 20 66 f the same SQL f
2b630 75 6e 63 74 69 6f 6e 20 64 75 72 69 6e 67 20 71 unction during q
2b640 75 65 72 79 20 65 78 65 63 75 74 69 6f 6e 2c 20 uery execution,
2b650 75 6e 64 65 72 0a 2a 2a 20 73 6f 6d 65 20 63 69 under.** some ci
2b660 72 63 75 6d 73 74 61 6e 63 65 73 20 74 68 65 20 rcumstances the
2b670 61 73 73 6f 63 69 61 74 65 64 20 6d 65 74 61 64 associated metad
2b680 61 74 61 20 6d 61 79 20 62 65 20 70 72 65 73 65 ata may be prese
2b690 72 76 65 64 2e 20 54 68 69 73 20 6d 61 79 0a 2a rved. This may.*
2b6a0 2a 20 62 65 20 75 73 65 64 2c 20 66 6f 72 20 65 * be used, for e
2b6b0 78 61 6d 70 6c 65 2c 20 74 6f 20 61 64 64 20 61 xample, to add a
2b6c0 20 72 65 67 75 6c 61 72 2d 65 78 70 72 65 73 73 regular-express
2b6d0 69 6f 6e 20 6d 61 74 63 68 69 6e 67 20 73 63 61 ion matching sca
2b6e0 6c 61 72 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e lar.** function.
2b6f0 20 54 68 65 20 63 6f 6d 70 69 6c 65 64 20 76 65 The compiled ve
2b700 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 72 65 67 rsion of the reg
2b710 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e 20 ular expression
2b720 69 73 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a 20 is stored as.**
2b730 6d 65 74 61 64 61 74 61 20 61 73 73 6f 63 69 61 metadata associa
2b740 74 65 64 20 77 69 74 68 20 74 68 65 20 53 51 4c ted with the SQL
2b750 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 73 value passed as
2b760 20 74 68 65 20 72 65 67 75 6c 61 72 20 65 78 70 the regular exp
2b770 72 65 73 73 69 6f 6e 0a 2a 2a 20 70 61 74 74 65 ression.** patte
2b780 72 6e 2e 20 20 54 68 65 20 63 6f 6d 70 69 6c 65 rn. The compile
2b790 64 20 72 65 67 75 6c 61 72 20 65 78 70 72 65 73 d regular expres
2b7a0 73 69 6f 6e 20 63 61 6e 20 62 65 20 72 65 75 73 sion can be reus
2b7b0 65 64 20 6f 6e 20 6d 75 6c 74 69 70 6c 65 0a 2a ed on multiple.*
2b7c0 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6f 66 * invocations of
2b7d0 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63 74 69 the same functi
2b7e0 6f 6e 20 73 6f 20 74 68 61 74 20 74 68 65 20 6f on so that the o
2b7f0 72 69 67 69 6e 61 6c 20 70 61 74 74 65 72 6e 20 riginal pattern
2b800 73 74 72 69 6e 67 0a 2a 2a 20 64 6f 65 73 20 6e string.** does n
2b810 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 72 65 ot need to be re
2b820 63 6f 6d 70 69 6c 65 64 20 6f 6e 20 65 61 63 68 compiled on each
2b830 20 69 6e 76 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a invocation..**.
2b840 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 67 ** The sqlite3_g
2b850 65 74 5f 61 75 78 64 61 74 61 28 29 20 69 6e 74 et_auxdata() int
2b860 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 erface returns a
2b870 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
2b880 6d 65 74 61 64 61 74 61 0a 2a 2a 20 61 73 73 6f metadata.** asso
2b890 63 69 61 74 65 64 20 62 79 20 74 68 65 20 73 71 ciated by the sq
2b8a0 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 lite3_set_auxdat
2b8b0 61 28 29 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 a() function wit
2b8c0 68 20 74 68 65 20 4e 74 68 20 61 72 67 75 6d 65 h the Nth argume
2b8d0 6e 74 0a 2a 2a 20 76 61 6c 75 65 20 74 6f 20 74 nt.** value to t
2b8e0 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 he application-d
2b8f0 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 2e efined function.
2b900 20 49 66 20 6e 6f 20 6d 65 74 61 64 61 74 61 20 If no metadata
2b910 68 61 73 20 62 65 65 6e 20 65 76 65 72 0a 2a 2a has been ever.**
2b920 20 62 65 65 6e 20 73 65 74 20 66 6f 72 20 74 68 been set for th
2b930 65 20 4e 74 68 20 61 72 67 75 6d 65 6e 74 20 6f e Nth argument o
2b940 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2c 20 f the function,
2b950 6f 72 20 69 66 20 74 68 65 20 63 6f 72 72 65 73 or if the corres
2b960 70 6f 6e 64 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 ponding.** funct
2b970 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 68 61 ion parameter ha
2b980 73 20 63 68 61 6e 67 65 64 20 73 69 6e 63 65 20 s changed since
2b990 74 68 65 20 6d 65 74 61 2d 64 61 74 61 20 77 61 the meta-data wa
2b9a0 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65 6e 20 73 s set,.** then s
2b9b0 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 qlite3_get_auxda
2b9c0 74 61 28 29 20 72 65 74 75 72 6e 73 20 61 20 4e ta() returns a N
2b9d0 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a ULL pointer..**.
2b9e0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 73 ** The sqlite3_s
2b9f0 65 74 5f 61 75 78 64 61 74 61 28 29 20 69 6e 74 et_auxdata() int
2ba00 65 72 66 61 63 65 20 73 61 76 65 73 20 74 68 65 erface saves the
2ba10 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 70 6f 69 metadata.** poi
2ba20 6e 74 65 64 20 74 6f 20 62 79 20 69 74 73 20 33 nted to by its 3
2ba30 72 64 20 70 61 72 61 6d 65 74 65 72 20 61 73 20 rd parameter as
2ba40 74 68 65 20 6d 65 74 61 64 61 74 61 20 66 6f 72 the metadata for
2ba50 20 74 68 65 20 4e 2d 74 68 0a 2a 2a 20 61 72 67 the N-th.** arg
2ba60 75 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 70 70 ument of the app
2ba70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 lication-defined
2ba80 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53 75 62 73 function. Subs
2ba90 65 71 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c 73 20 equent.** calls
2baa0 74 6f 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 to sqlite3_get_a
2bab0 75 78 64 61 74 61 28 29 20 6d 69 67 68 74 20 72 uxdata() might r
2bac0 65 74 75 72 6e 20 74 68 69 73 20 64 61 74 61 2c eturn this data,
2bad0 20 69 66 20 69 74 20 68 61 73 0a 2a 2a 20 6e 6f if it has.** no
2bae0 74 20 62 65 65 6e 20 64 65 73 74 72 6f 79 65 64 t been destroyed
2baf0 2e 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 6e 6f ..** If it is no
2bb00 74 20 4e 55 4c 4c 2c 20 53 51 4c 69 74 65 20 77 t NULL, SQLite w
2bb10 69 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20 64 ill invoke the d
2bb20 65 73 74 72 75 63 74 6f 72 0a 2a 2a 20 66 75 6e estructor.** fun
2bb30 63 74 69 6f 6e 20 67 69 76 65 6e 20 62 79 20 74 ction given by t
2bb40 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 he 4th parameter
2bb50 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 65 74 5f to sqlite3_set_
2bb60 61 75 78 64 61 74 61 28 29 20 6f 6e 0a 2a 2a 20 auxdata() on.**
2bb70 74 68 65 20 6d 65 74 61 64 61 74 61 20 77 68 65 the metadata whe
2bb80 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 n the correspond
2bb90 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 70 61 72 ing function par
2bba0 61 6d 65 74 65 72 20 63 68 61 6e 67 65 73 0a 2a ameter changes.*
2bbb0 2a 20 6f 72 20 77 68 65 6e 20 74 68 65 20 53 51 * or when the SQ
2bbc0 4c 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70 L statement comp
2bbd0 6c 65 74 65 73 2c 20 77 68 69 63 68 65 76 65 72 letes, whichever
2bbe0 20 63 6f 6d 65 73 20 66 69 72 73 74 2e 0a 2a 2a comes first..**
2bbf0 0a 2a 2a 20 53 51 4c 69 74 65 20 69 73 20 66 72 .** SQLite is fr
2bc00 65 65 20 74 6f 20 63 61 6c 6c 20 74 68 65 20 64 ee to call the d
2bc10 65 73 74 72 75 63 74 6f 72 20 61 6e 64 20 64 72 estructor and dr
2bc20 6f 70 20 6d 65 74 61 64 61 74 61 20 6f 6e 20 61 op metadata on a
2bc30 6e 79 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 ny.** parameter
2bc40 6f 66 20 61 6e 79 20 66 75 6e 63 74 69 6f 6e 20 of any function
2bc50 61 74 20 61 6e 79 20 74 69 6d 65 2e 20 20 54 68 at any time. Th
2bc60 65 20 6f 6e 6c 79 20 67 75 61 72 61 6e 74 65 65 e only guarantee
2bc70 20 69 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 is that.** the
2bc80 64 65 73 74 72 75 63 74 6f 72 20 77 69 6c 6c 20 destructor will
2bc90 62 65 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 65 be called before
2bca0 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 69 73 the metadata is
2bcb0 20 64 72 6f 70 70 65 64 2e 0a 2a 2a 0a 2a 2a 20 dropped..**.**
2bcc0 49 6e 20 70 72 61 63 74 69 63 65 2c 20 6d 65 74 In practice, met
2bcd0 61 64 61 74 61 20 69 73 20 70 72 65 73 65 72 76 adata is preserv
2bce0 65 64 20 62 65 74 77 65 65 6e 20 66 75 6e 63 74 ed between funct
2bcf0 69 6f 6e 20 63 61 6c 6c 73 20 66 6f 72 0a 2a 2a ion calls for.**
2bd00 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68 61 expressions tha
2bd10 74 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 20 61 t are constant a
2bd20 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2e 20 t compile time.
2bd30 54 68 69 73 20 69 6e 63 6c 75 64 65 73 20 6c 69 This includes li
2bd40 74 65 72 61 6c 0a 2a 2a 20 76 61 6c 75 65 73 20 teral.** values
2bd50 61 6e 64 20 53 51 4c 20 76 61 72 69 61 62 6c 65 and SQL variable
2bd60 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 s..**.** These r
2bd70 6f 75 74 69 6e 65 73 20 6d 75 73 74 20 62 65 20 outines must be
2bd80 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 called from the
2bd90 73 61 6d 65 20 74 68 72 65 61 64 20 69 6e 20 77 same thread in w
2bda0 68 69 63 68 0a 2a 2a 20 74 68 65 20 53 51 4c 20 hich.** the SQL
2bdb0 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e function is runn
2bdc0 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 ing..**.** Requi
2bdd0 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 rements:.** [H16
2bde0 32 37 32 5d 20 5b 48 31 36 32 37 34 5d 20 5b 48 272] [H16274] [H
2bdf0 31 36 32 37 36 5d 20 5b 48 31 36 32 37 37 5d 20 16276] [H16277]
2be00 5b 48 31 36 32 37 38 5d 20 5b 48 31 36 32 37 39 [H16278] [H16279
2be10 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
2be20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 67 65 void *sqlite3_ge
2be30 74 5f 61 75 78 64 61 74 61 28 73 71 6c 69 74 65 t_auxdata(sqlite
2be40 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 20 3_context*, int
2be50 4e 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 N);.SQLITE_API v
2be60 6f 69 64 20 73 71 6c 69 74 65 33 5f 73 65 74 5f oid sqlite3_set_
2be70 61 75 78 64 61 74 61 28 73 71 6c 69 74 65 33 5f auxdata(sqlite3_
2be80 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 20 4e 2c context*, int N,
2be90 20 76 6f 69 64 2a 2c 20 76 6f 69 64 20 28 2a 29 void*, void (*)
2bea0 28 76 6f 69 64 2a 29 29 3b 0a 0a 0a 2f 2a 0a 2a (void*));.../*.*
2beb0 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 73 * CAPI3REF: Cons
2bec0 74 61 6e 74 73 20 44 65 66 69 6e 69 6e 67 20 53 tants Defining S
2bed0 70 65 63 69 61 6c 20 44 65 73 74 72 75 63 74 6f pecial Destructo
2bee0 72 20 42 65 68 61 76 69 6f 72 20 7b 48 31 30 32 r Behavior {H102
2bef0 38 30 7d 20 3c 53 33 30 31 30 30 3e 0a 2a 2a 0a 80} <S30100>.**.
2bf00 2a 2a 20 54 68 65 73 65 20 61 72 65 20 73 70 65 ** These are spe
2bf10 63 69 61 6c 20 76 61 6c 75 65 73 20 66 6f 72 20 cial values for
2bf20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 74 the destructor t
2bf30 68 61 74 20 69 73 20 70 61 73 73 65 64 20 69 6e hat is passed in
2bf40 20 61 73 20 74 68 65 0a 2a 2a 20 66 69 6e 61 6c as the.** final
2bf50 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 72 6f 75 argument to rou
2bf60 74 69 6e 65 73 20 6c 69 6b 65 20 5b 73 71 6c 69 tines like [sqli
2bf70 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 te3_result_blob(
2bf80 29 5d 2e 20 20 49 66 20 74 68 65 20 64 65 73 74 )]. If the dest
2bf90 72 75 63 74 6f 72 0a 2a 2a 20 61 72 67 75 6d 65 ructor.** argume
2bfa0 6e 74 20 69 73 20 53 51 4c 49 54 45 5f 53 54 41 nt is SQLITE_STA
2bfb0 54 49 43 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 TIC, it means th
2bfc0 61 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 70 at the content p
2bfd0 6f 69 6e 74 65 72 20 69 73 20 63 6f 6e 73 74 61 ointer is consta
2bfe0 6e 74 0a 2a 2a 20 61 6e 64 20 77 69 6c 6c 20 6e nt.** and will n
2bff0 65 76 65 72 20 63 68 61 6e 67 65 2e 20 20 49 74 ever change. It
2c000 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 does not need t
2c010 6f 20 62 65 20 64 65 73 74 72 6f 79 65 64 2e 20 o be destroyed.
2c020 20 54 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 54 The.** SQLITE_T
2c030 52 41 4e 53 49 45 4e 54 20 76 61 6c 75 65 20 6d RANSIENT value m
2c040 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 63 6f eans that the co
2c050 6e 74 65 6e 74 20 77 69 6c 6c 20 6c 69 6b 65 6c ntent will likel
2c060 79 20 63 68 61 6e 67 65 20 69 6e 0a 2a 2a 20 74 y change in.** t
2c070 68 65 20 6e 65 61 72 20 66 75 74 75 72 65 20 61 he near future a
2c080 6e 64 20 74 68 61 74 20 53 51 4c 69 74 65 20 73 nd that SQLite s
2c090 68 6f 75 6c 64 20 6d 61 6b 65 20 69 74 73 20 6f hould make its o
2c0a0 77 6e 20 70 72 69 76 61 74 65 20 63 6f 70 79 20 wn private copy
2c0b0 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e of.** the conten
2c0c0 74 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 t before returni
2c0d0 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 79 ng..**.** The ty
2c0e0 70 65 64 65 66 20 69 73 20 6e 65 63 65 73 73 61 pedef is necessa
2c0f0 72 79 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75 6e ry to work aroun
2c100 64 20 70 72 6f 62 6c 65 6d 73 20 69 6e 20 63 65 d problems in ce
2c110 72 74 61 69 6e 0a 2a 2a 20 43 2b 2b 20 63 6f 6d rtain.** C++ com
2c120 70 69 6c 65 72 73 2e 20 20 53 65 65 20 74 69 63 pilers. See tic
2c130 6b 65 74 20 23 32 31 39 31 2e 0a 2a 2f 0a 74 79 ket #2191..*/.ty
2c140 70 65 64 65 66 20 76 6f 69 64 20 28 2a 73 71 6c pedef void (*sql
2c150 69 74 65 33 5f 64 65 73 74 72 75 63 74 6f 72 5f ite3_destructor_
2c160 74 79 70 65 29 28 76 6f 69 64 2a 29 3b 0a 23 64 type)(void*);.#d
2c170 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 efine SQLITE_STA
2c180 54 49 43 20 20 20 20 20 20 28 28 73 71 6c 69 74 TIC ((sqlit
2c190 65 33 5f 64 65 73 74 72 75 63 74 6f 72 5f 74 79 e3_destructor_ty
2c1a0 70 65 29 30 29 0a 23 64 65 66 69 6e 65 20 53 51 pe)0).#define SQ
2c1b0 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 20 20 LITE_TRANSIENT
2c1c0 20 28 28 73 71 6c 69 74 65 33 5f 64 65 73 74 72 ((sqlite3_destr
2c1d0 75 63 74 6f 72 5f 74 79 70 65 29 2d 31 29 0a 0a uctor_type)-1)..
2c1e0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
2c1f0 53 65 74 74 69 6e 67 20 54 68 65 20 52 65 73 75 Setting The Resu
2c200 6c 74 20 4f 66 20 41 6e 20 53 51 4c 20 46 75 6e lt Of An SQL Fun
2c210 63 74 69 6f 6e 20 7b 48 31 36 34 30 30 7d 20 3c ction {H16400} <
2c220 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S20200>.**.** Th
2c230 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 ese routines are
2c240 20 75 73 65 64 20 62 79 20 74 68 65 20 78 46 75 used by the xFu
2c250 6e 63 20 6f 72 20 78 46 69 6e 61 6c 20 63 61 6c nc or xFinal cal
2c260 6c 62 61 63 6b 73 20 74 68 61 74 0a 2a 2a 20 69 lbacks that.** i
2c270 6d 70 6c 65 6d 65 6e 74 20 53 51 4c 20 66 75 6e mplement SQL fun
2c280 63 74 69 6f 6e 73 20 61 6e 64 20 61 67 67 72 65 ctions and aggre
2c290 67 61 74 65 73 2e 20 20 53 65 65 0a 2a 2a 20 5b gates. See.** [
2c2a0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 sqlite3_create_f
2c2b0 75 6e 63 74 69 6f 6e 28 29 5d 20 61 6e 64 20 5b unction()] and [
2c2c0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 sqlite3_create_f
2c2d0 75 6e 63 74 69 6f 6e 31 36 28 29 5d 0a 2a 2a 20 unction16()].**
2c2e0 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 for additional i
2c2f0 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a nformation..**.*
2c300 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69 6f 6e * These function
2c310 73 20 77 6f 72 6b 20 76 65 72 79 20 6d 75 63 68 s work very much
2c320 20 6c 69 6b 65 20 74 68 65 20 5b 70 61 72 61 6d like the [param
2c330 65 74 65 72 20 62 69 6e 64 69 6e 67 5d 20 66 61 eter binding] fa
2c340 6d 69 6c 79 20 6f 66 0a 2a 2a 20 66 75 6e 63 74 mily of.** funct
2c350 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 62 69 6e ions used to bin
2c360 64 20 76 61 6c 75 65 73 20 74 6f 20 68 6f 73 74 d values to host
2c370 20 70 61 72 61 6d 65 74 65 72 73 20 69 6e 20 70 parameters in p
2c380 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
2c390 74 73 2e 0a 2a 2a 20 52 65 66 65 72 20 74 6f 20 ts..** Refer to
2c3a0 74 68 65 20 5b 53 51 4c 20 70 61 72 61 6d 65 74 the [SQL paramet
2c3b0 65 72 5d 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f er] documentatio
2c3c0 6e 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c n for additional
2c3d0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a information..**
2c3e0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
2c3f0 72 65 73 75 6c 74 5f 62 6c 6f 62 28 29 20 69 6e result_blob() in
2c400 74 65 72 66 61 63 65 20 73 65 74 73 20 74 68 65 terface sets the
2c410 20 72 65 73 75 6c 74 20 66 72 6f 6d 0a 2a 2a 20 result from.**
2c420 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 an application-d
2c430 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 efined function
2c440 74 6f 20 62 65 20 74 68 65 20 42 4c 4f 42 20 77 to be the BLOB w
2c450 68 6f 73 65 20 63 6f 6e 74 65 6e 74 20 69 73 20 hose content is
2c460 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 62 79 pointed.** to by
2c470 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 the second para
2c480 6d 65 74 65 72 20 61 6e 64 20 77 68 69 63 68 20 meter and which
2c490 69 73 20 4e 20 62 79 74 65 73 20 6c 6f 6e 67 20 is N bytes long
2c4a0 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 0a 2a where N is the.*
2c4b0 2a 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 * third paramete
2c4c0 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c r..**.** The sql
2c4d0 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f ite3_result_zero
2c4e0 62 6c 6f 62 28 29 20 69 6e 74 65 72 66 61 63 65 blob() interface
2c4f0 73 20 73 65 74 20 74 68 65 20 72 65 73 75 6c 74 s set the result
2c500 20 6f 66 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 of.** the appli
2c510 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 cation-defined f
2c520 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 61 20 unction to be a
2c530 42 4c 4f 42 20 63 6f 6e 74 61 69 6e 69 6e 67 20 BLOB containing
2c540 61 6c 6c 20 7a 65 72 6f 0a 2a 2a 20 62 79 74 65 all zero.** byte
2c550 73 20 61 6e 64 20 4e 20 62 79 74 65 73 20 69 6e s and N bytes in
2c560 20 73 69 7a 65 2c 20 77 68 65 72 65 20 4e 20 69 size, where N i
2c570 73 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 s the value of t
2c580 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 he 2nd parameter
2c590 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ..**.** The sqli
2c5a0 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c te3_result_doubl
2c5b0 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65 e() interface se
2c5c0 74 73 20 74 68 65 20 72 65 73 75 6c 74 20 66 72 ts the result fr
2c5d0 6f 6d 0a 2a 2a 20 61 6e 20 61 70 70 6c 69 63 61 om.** an applica
2c5e0 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e tion-defined fun
2c5f0 63 74 69 6f 6e 20 74 6f 20 62 65 20 61 20 66 6c ction to be a fl
2c600 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c oating point val
2c610 75 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 ue specified.**
2c620 62 79 20 69 74 73 20 32 6e 64 20 61 72 67 75 6d by its 2nd argum
2c630 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ent..**.** The s
2c640 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
2c650 72 6f 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 ror() and sqlite
2c660 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 3_result_error16
2c670 28 29 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 () functions.**
2c680 63 61 75 73 65 20 74 68 65 20 69 6d 70 6c 65 6d cause the implem
2c690 65 6e 74 65 64 20 53 51 4c 20 66 75 6e 63 74 69 ented SQL functi
2c6a0 6f 6e 20 74 6f 20 74 68 72 6f 77 20 61 6e 20 65 on to throw an e
2c6b0 78 63 65 70 74 69 6f 6e 2e 0a 2a 2a 20 53 51 4c xception..** SQL
2c6c0 69 74 65 20 75 73 65 73 20 74 68 65 20 73 74 72 ite uses the str
2c6d0 69 6e 67 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 ing pointed to b
2c6e0 79 20 74 68 65 0a 2a 2a 20 32 6e 64 20 70 61 72 y the.** 2nd par
2c6f0 61 6d 65 74 65 72 20 6f 66 20 73 71 6c 69 74 65 ameter of sqlite
2c700 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 3_result_error()
2c710 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75 or sqlite3_resu
2c720 6c 74 5f 65 72 72 6f 72 31 36 28 29 0a 2a 2a 20 lt_error16().**
2c730 61 73 20 74 68 65 20 74 65 78 74 20 6f 66 20 61 as the text of a
2c740 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e n error message.
2c750 20 20 53 51 4c 69 74 65 20 69 6e 74 65 72 70 72 SQLite interpr
2c760 65 74 73 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a ets the error.**
2c770 20 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67 20 message string
2c780 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 72 65 73 from sqlite3_res
2c790 75 6c 74 5f 65 72 72 6f 72 28 29 20 61 73 20 55 ult_error() as U
2c7a0 54 46 2d 38 2e 20 53 51 4c 69 74 65 0a 2a 2a 20 TF-8. SQLite.**
2c7b0 69 6e 74 65 72 70 72 65 74 73 20 74 68 65 20 73 interprets the s
2c7c0 74 72 69 6e 67 20 66 72 6f 6d 20 73 71 6c 69 74 tring from sqlit
2c7d0 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 e3_result_error1
2c7e0 36 28 29 20 61 73 20 55 54 46 2d 31 36 20 69 6e 6() as UTF-16 in
2c7f0 20 6e 61 74 69 76 65 0a 2a 2a 20 62 79 74 65 20 native.** byte
2c800 6f 72 64 65 72 2e 20 20 49 66 20 74 68 65 20 74 order. If the t
2c810 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 hird parameter t
2c820 6f 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 o sqlite3_result
2c830 5f 65 72 72 6f 72 28 29 0a 2a 2a 20 6f 72 20 73 _error().** or s
2c840 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
2c850 72 6f 72 31 36 28 29 20 69 73 20 6e 65 67 61 74 ror16() is negat
2c860 69 76 65 20 74 68 65 6e 20 53 51 4c 69 74 65 20 ive then SQLite
2c870 74 61 6b 65 73 20 61 73 20 74 68 65 20 65 72 72 takes as the err
2c880 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20 61 6c or.** message al
2c890 6c 20 74 65 78 74 20 75 70 20 74 68 72 6f 75 67 l text up throug
2c8a0 68 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f h the first zero
2c8b0 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 49 character..** I
2c8c0 66 20 74 68 65 20 74 68 69 72 64 20 70 61 72 61 f the third para
2c8d0 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 meter to sqlite3
2c8e0 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 20 _result_error()
2c8f0 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 or.** sqlite3_re
2c900 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 69 sult_error16() i
2c910 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 20 74 s non-negative t
2c920 68 65 6e 20 53 51 4c 69 74 65 20 74 61 6b 65 73 hen SQLite takes
2c930 20 74 68 61 74 20 6d 61 6e 79 0a 2a 2a 20 62 79 that many.** by
2c940 74 65 73 20 28 6e 6f 74 20 63 68 61 72 61 63 74 tes (not charact
2c950 65 72 73 29 20 66 72 6f 6d 20 74 68 65 20 32 6e ers) from the 2n
2c960 64 20 70 61 72 61 6d 65 74 65 72 20 61 73 20 74 d parameter as t
2c970 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 he error message
2c980 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 ..** The sqlite3
2c990 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 20 _result_error()
2c9a0 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 and sqlite3_resu
2c9b0 6c 74 5f 65 72 72 6f 72 31 36 28 29 0a 2a 2a 20 lt_error16().**
2c9c0 72 6f 75 74 69 6e 65 73 20 6d 61 6b 65 20 61 20 routines make a
2c9d0 70 72 69 76 61 74 65 20 63 6f 70 79 20 6f 66 20 private copy of
2c9e0 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 the error messag
2c9f0 65 20 74 65 78 74 20 62 65 66 6f 72 65 0a 2a 2a e text before.**
2ca00 20 74 68 65 79 20 72 65 74 75 72 6e 2e 20 20 48 they return. H
2ca10 65 6e 63 65 2c 20 74 68 65 20 63 61 6c 6c 69 6e ence, the callin
2ca20 67 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e 20 64 g function can d
2ca30 65 61 6c 6c 6f 63 61 74 65 20 6f 72 0a 2a 2a 20 eallocate or.**
2ca40 6d 6f 64 69 66 79 20 74 68 65 20 74 65 78 74 20 modify the text
2ca50 61 66 74 65 72 20 74 68 65 79 20 72 65 74 75 72 after they retur
2ca60 6e 20 77 69 74 68 6f 75 74 20 68 61 72 6d 2e 0a n without harm..
2ca70 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 ** The sqlite3_r
2ca80 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 esult_error_code
2ca90 28 29 20 66 75 6e 63 74 69 6f 6e 20 63 68 61 6e () function chan
2caa0 67 65 73 20 74 68 65 20 65 72 72 6f 72 20 63 6f ges the error co
2cab0 64 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 62 de.** returned b
2cac0 79 20 53 51 4c 69 74 65 20 61 73 20 61 20 72 65 y SQLite as a re
2cad0 73 75 6c 74 20 6f 66 20 61 6e 20 65 72 72 6f 72 sult of an error
2cae0 20 69 6e 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 in a function.
2caf0 20 42 79 20 64 65 66 61 75 6c 74 2c 0a 2a 2a 20 By default,.**
2cb00 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 the error code i
2cb10 73 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 20 s SQLITE_ERROR.
2cb20 20 41 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 A subsequent ca
2cb30 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 ll to sqlite3_re
2cb40 73 75 6c 74 5f 65 72 72 6f 72 28 29 0a 2a 2a 20 sult_error().**
2cb50 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c or sqlite3_resul
2cb60 74 5f 65 72 72 6f 72 31 36 28 29 20 72 65 73 65 t_error16() rese
2cb70 74 73 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 ts the error cod
2cb80 65 20 74 6f 20 53 51 4c 49 54 45 5f 45 52 52 4f e to SQLITE_ERRO
2cb90 52 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c R..**.** The sql
2cba0 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 6f 6f 62 ite3_result_toob
2cbb0 69 67 28 29 20 69 6e 74 65 72 66 61 63 65 20 63 ig() interface c
2cbc0 61 75 73 65 73 20 53 51 4c 69 74 65 20 74 6f 20 auses SQLite to
2cbd0 74 68 72 6f 77 20 61 6e 20 65 72 72 6f 72 0a 2a throw an error.*
2cbe0 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 * indicating tha
2cbf0 74 20 61 20 73 74 72 69 6e 67 20 6f 72 20 42 4c t a string or BL
2cc00 4f 42 20 69 73 20 74 6f 20 6c 6f 6e 67 20 74 6f OB is to long to
2cc10 20 72 65 70 72 65 73 65 6e 74 2e 0a 2a 2a 0a 2a represent..**.*
2cc20 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 * The sqlite3_re
2cc30 73 75 6c 74 5f 6e 6f 6d 65 6d 28 29 20 69 6e 74 sult_nomem() int
2cc40 65 72 66 61 63 65 20 63 61 75 73 65 73 20 53 51 erface causes SQ
2cc50 4c 69 74 65 20 74 6f 20 74 68 72 6f 77 20 61 6e Lite to throw an
2cc60 20 65 72 72 6f 72 0a 2a 2a 20 69 6e 64 69 63 61 error.** indica
2cc70 74 69 6e 67 20 74 68 61 74 20 61 20 6d 65 6d 6f ting that a memo
2cc80 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 ry allocation fa
2cc90 69 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 iled..**.** The
2cca0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 sqlite3_result_i
2ccb0 6e 74 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 nt() interface s
2ccc0 65 74 73 20 74 68 65 20 72 65 74 75 72 6e 20 76 ets the return v
2ccd0 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65 20 61 alue.** of the a
2cce0 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e pplication-defin
2ccf0 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 ed function to b
2cd00 65 20 74 68 65 20 33 32 2d 62 69 74 20 73 69 67 e the 32-bit sig
2cd10 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 76 ned integer.** v
2cd20 61 6c 75 65 20 67 69 76 65 6e 20 69 6e 20 74 68 alue given in th
2cd30 65 20 32 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a e 2nd argument..
2cd40 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 ** The sqlite3_r
2cd50 65 73 75 6c 74 5f 69 6e 74 36 34 28 29 20 69 6e esult_int64() in
2cd60 74 65 72 66 61 63 65 20 73 65 74 73 20 74 68 65 terface sets the
2cd70 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a return value.**
2cd80 20 6f 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 of the applicat
2cd90 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 ion-defined func
2cda0 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 20 36 tion to be the 6
2cdb0 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 4-bit signed int
2cdc0 65 67 65 72 0a 2a 2a 20 76 61 6c 75 65 20 67 69 eger.** value gi
2cdd0 76 65 6e 20 69 6e 20 74 68 65 20 32 6e 64 20 61 ven in the 2nd a
2cde0 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 rgument..**.** T
2cdf0 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c he sqlite3_resul
2ce00 74 5f 6e 75 6c 6c 28 29 20 69 6e 74 65 72 66 61 t_null() interfa
2ce10 63 65 20 73 65 74 73 20 74 68 65 20 72 65 74 75 ce sets the retu
2ce20 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 rn value.** of t
2ce30 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 he application-d
2ce40 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 efined function
2ce50 74 6f 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a to be NULL..**.*
2ce60 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 * The sqlite3_re
2ce70 73 75 6c 74 5f 74 65 78 74 28 29 2c 20 73 71 6c sult_text(), sql
2ce80 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 ite3_result_text
2ce90 31 36 28 29 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 16(),.** sqlite3
2cea0 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 _result_text16le
2ceb0 28 29 2c 20 61 6e 64 20 73 71 6c 69 74 65 33 5f (), and sqlite3_
2cec0 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 28 result_text16be(
2ced0 29 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 ) interfaces.**
2cee0 73 65 74 20 74 68 65 20 72 65 74 75 72 6e 20 76 set the return v
2cef0 61 6c 75 65 20 6f 66 20 74 68 65 20 61 70 70 6c alue of the appl
2cf00 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 ication-defined
2cf10 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 0a 2a function to be.*
2cf20 2a 20 61 20 74 65 78 74 20 73 74 72 69 6e 67 20 * a text string
2cf30 77 68 69 63 68 20 69 73 20 72 65 70 72 65 73 65 which is represe
2cf40 6e 74 65 64 20 61 73 20 55 54 46 2d 38 2c 20 55 nted as UTF-8, U
2cf50 54 46 2d 31 36 20 6e 61 74 69 76 65 20 62 79 74 TF-16 native byt
2cf60 65 20 6f 72 64 65 72 2c 0a 2a 2a 20 55 54 46 2d e order,.** UTF-
2cf70 31 36 20 6c 69 74 74 6c 65 20 65 6e 64 69 61 6e 16 little endian
2cf80 2c 20 6f 72 20 55 54 46 2d 31 36 20 62 69 67 20 , or UTF-16 big
2cf90 65 6e 64 69 61 6e 2c 20 72 65 73 70 65 63 74 69 endian, respecti
2cfa0 76 65 6c 79 2e 0a 2a 2a 20 53 51 4c 69 74 65 20 vely..** SQLite
2cfb0 74 61 6b 65 73 20 74 68 65 20 74 65 78 74 20 72 takes the text r
2cfc0 65 73 75 6c 74 20 66 72 6f 6d 20 74 68 65 20 61 esult from the a
2cfd0 70 70 6c 69 63 61 74 69 6f 6e 20 66 72 6f 6d 0a pplication from.
2cfe0 2a 2a 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d ** the 2nd param
2cff0 65 74 65 72 20 6f 66 20 74 68 65 20 73 71 6c 69 eter of the sqli
2d000 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a te3_result_text*
2d010 20 69 6e 74 65 72 66 61 63 65 73 2e 0a 2a 2a 20 interfaces..**
2d020 49 66 20 74 68 65 20 33 72 64 20 70 61 72 61 6d If the 3rd param
2d030 65 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69 eter to the sqli
2d040 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a te3_result_text*
2d050 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 69 interfaces.** i
2d060 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e s negative, then
2d070 20 53 51 4c 69 74 65 20 74 61 6b 65 73 20 72 65 SQLite takes re
2d080 73 75 6c 74 20 74 65 78 74 20 66 72 6f 6d 20 74 sult text from t
2d090 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 he 2nd parameter
2d0a0 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 65 20 .** through the
2d0b0 66 69 72 73 74 20 7a 65 72 6f 20 63 68 61 72 61 first zero chara
2d0c0 63 74 65 72 2e 0a 2a 2a 20 49 66 20 74 68 65 20 cter..** If the
2d0d0 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 3rd parameter to
2d0e0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 the sqlite3_res
2d0f0 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 ult_text* interf
2d100 61 63 65 73 0a 2a 2a 20 69 73 20 6e 6f 6e 2d 6e aces.** is non-n
2d110 65 67 61 74 69 76 65 2c 20 74 68 65 6e 20 61 73 egative, then as
2d120 20 6d 61 6e 79 20 62 79 74 65 73 20 28 6e 6f 74 many bytes (not
2d130 20 63 68 61 72 61 63 74 65 72 73 29 20 6f 66 20 characters) of
2d140 74 68 65 20 74 65 78 74 0a 2a 2a 20 70 6f 69 6e the text.** poin
2d150 74 65 64 20 74 6f 20 62 79 20 74 68 65 20 32 6e ted to by the 2n
2d160 64 20 70 61 72 61 6d 65 74 65 72 20 61 72 65 20 d parameter are
2d170 74 61 6b 65 6e 20 61 73 20 74 68 65 20 61 70 70 taken as the app
2d180 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 lication-defined
2d190 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65 73 .** function res
2d1a0 75 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 34 ult..** If the 4
2d1b0 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 th parameter to
2d1c0 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 the sqlite3_resu
2d1d0 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 61 lt_text* interfa
2d1e0 63 65 73 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 ces.** or sqlite
2d1f0 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 69 73 3_result_blob is
2d200 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 70 6f 69 6e a non-NULL poin
2d210 74 65 72 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 ter, then SQLite
2d220 20 63 61 6c 6c 73 20 74 68 61 74 0a 2a 2a 20 66 calls that.** f
2d230 75 6e 63 74 69 6f 6e 20 61 73 20 74 68 65 20 64 unction as the d
2d240 65 73 74 72 75 63 74 6f 72 20 6f 6e 20 74 68 65 estructor on the
2d250 20 74 65 78 74 20 6f 72 20 42 4c 4f 42 20 72 65 text or BLOB re
2d260 73 75 6c 74 20 77 68 65 6e 20 69 74 20 68 61 73 sult when it has
2d270 0a 2a 2a 20 66 69 6e 69 73 68 65 64 20 75 73 69 .** finished usi
2d280 6e 67 20 74 68 61 74 20 72 65 73 75 6c 74 2e 0a ng that result..
2d290 2a 2a 20 49 66 20 74 68 65 20 34 74 68 20 70 61 ** If the 4th pa
2d2a0 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 rameter to the s
2d2b0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 qlite3_result_te
2d2c0 78 74 2a 20 69 6e 74 65 72 66 61 63 65 73 20 6f xt* interfaces o
2d2d0 72 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f r to.** sqlite3_
2d2e0 72 65 73 75 6c 74 5f 62 6c 6f 62 20 69 73 20 74 result_blob is t
2d2f0 68 65 20 73 70 65 63 69 61 6c 20 63 6f 6e 73 74 he special const
2d300 61 6e 74 20 53 51 4c 49 54 45 5f 53 54 41 54 49 ant SQLITE_STATI
2d310 43 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 0a 2a C, then SQLite.*
2d320 2a 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 * assumes that t
2d330 68 65 20 74 65 78 74 20 6f 72 20 42 4c 4f 42 20 he text or BLOB
2d340 72 65 73 75 6c 74 20 69 73 20 69 6e 20 63 6f 6e result is in con
2d350 73 74 61 6e 74 20 73 70 61 63 65 20 61 6e 64 20 stant space and
2d360 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 63 6f 70 79 does not.** copy
2d370 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 the content of
2d380 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 6e 6f the parameter no
2d390 72 20 63 61 6c 6c 20 61 20 64 65 73 74 72 75 63 r call a destruc
2d3a0 74 6f 72 20 6f 6e 20 74 68 65 20 63 6f 6e 74 65 tor on the conte
2d3b0 6e 74 0a 2a 2a 20 77 68 65 6e 20 69 74 20 68 61 nt.** when it ha
2d3c0 73 20 66 69 6e 69 73 68 65 64 20 75 73 69 6e 67 s finished using
2d3d0 20 74 68 61 74 20 72 65 73 75 6c 74 2e 0a 2a 2a that result..**
2d3e0 20 49 66 20 74 68 65 20 34 74 68 20 70 61 72 61 If the 4th para
2d3f0 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c meter to the sql
2d400 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 ite3_result_text
2d410 2a 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 * interfaces.**
2d420 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c or sqlite3_resul
2d430 74 5f 62 6c 6f 62 20 69 73 20 74 68 65 20 73 70 t_blob is the sp
2d440 65 63 69 61 6c 20 63 6f 6e 73 74 61 6e 74 20 53 ecial constant S
2d450 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 0a QLITE_TRANSIENT.
2d460 2a 2a 20 74 68 65 6e 20 53 51 4c 69 74 65 20 6d ** then SQLite m
2d470 61 6b 65 73 20 61 20 63 6f 70 79 20 6f 66 20 74 akes a copy of t
2d480 68 65 20 72 65 73 75 6c 74 20 69 6e 74 6f 20 73 he result into s
2d490 70 61 63 65 20 6f 62 74 61 69 6e 65 64 20 66 72 pace obtained fr
2d4a0 6f 6d 0a 2a 2a 20 66 72 6f 6d 20 5b 73 71 6c 69 om.** from [sqli
2d4b0 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 62 65 te3_malloc()] be
2d4c0 66 6f 72 65 20 69 74 20 72 65 74 75 72 6e 73 2e fore it returns.
2d4d0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
2d4e0 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 e3_result_value(
2d4f0 29 20 69 6e 74 65 72 66 61 63 65 20 73 65 74 73 ) interface sets
2d500 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a the result of.*
2d510 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f * the applicatio
2d520 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 n-defined functi
2d530 6f 6e 20 74 6f 20 62 65 20 61 20 63 6f 70 79 20 on to be a copy
2d540 74 68 65 0a 2a 2a 20 5b 75 6e 70 72 6f 74 65 63 the.** [unprotec
2d550 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ted sqlite3_valu
2d560 65 5d 20 6f 62 6a 65 63 74 20 73 70 65 63 69 66 e] object specif
2d570 69 65 64 20 62 79 20 74 68 65 20 32 6e 64 20 70 ied by the 2nd p
2d580 61 72 61 6d 65 74 65 72 2e 20 20 54 68 65 0a 2a arameter. The.*
2d590 2a 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 * sqlite3_result
2d5a0 5f 76 61 6c 75 65 28 29 20 69 6e 74 65 72 66 61 _value() interfa
2d5b0 63 65 20 6d 61 6b 65 73 20 61 20 63 6f 70 79 20 ce makes a copy
2d5c0 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f of the [sqlite3_
2d5d0 76 61 6c 75 65 5d 0a 2a 2a 20 73 6f 20 74 68 61 value].** so tha
2d5e0 74 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 t the [sqlite3_v
2d5f0 61 6c 75 65 5d 20 73 70 65 63 69 66 69 65 64 20 alue] specified
2d600 69 6e 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 in the parameter
2d610 20 6d 61 79 20 63 68 61 6e 67 65 20 6f 72 0a 2a may change or.*
2d620 2a 20 62 65 20 64 65 61 6c 6c 6f 63 61 74 65 64 * be deallocated
2d630 20 61 66 74 65 72 20 73 71 6c 69 74 65 33 5f 72 after sqlite3_r
2d640 65 73 75 6c 74 5f 76 61 6c 75 65 28 29 20 72 65 esult_value() re
2d650 74 75 72 6e 73 20 77 69 74 68 6f 75 74 20 68 61 turns without ha
2d660 72 6d 2e 0a 2a 2a 20 41 20 5b 70 72 6f 74 65 63 rm..** A [protec
2d670 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ted sqlite3_valu
2d680 65 5d 20 6f 62 6a 65 63 74 20 6d 61 79 20 61 6c e] object may al
2d690 77 61 79 73 20 62 65 20 75 73 65 64 20 77 68 65 ways be used whe
2d6a0 72 65 20 61 6e 0a 2a 2a 20 5b 75 6e 70 72 6f 74 re an.** [unprot
2d6b0 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 ected sqlite3_va
2d6c0 6c 75 65 5d 20 6f 62 6a 65 63 74 20 69 73 20 72 lue] object is r
2d6d0 65 71 75 69 72 65 64 2c 20 73 6f 20 65 69 74 68 equired, so eith
2d6e0 65 72 0a 2a 2a 20 6b 69 6e 64 20 6f 66 20 5b 73 er.** kind of [s
2d6f0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 qlite3_value] ob
2d700 6a 65 63 74 20 63 61 6e 20 62 65 20 75 73 65 64 ject can be used
2d710 20 77 69 74 68 20 74 68 69 73 20 69 6e 74 65 72 with this inter
2d720 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 face..**.** If t
2d730 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 hese routines ar
2d740 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 e called from wi
2d750 74 68 69 6e 20 74 68 65 20 64 69 66 66 65 72 65 thin the differe
2d760 6e 74 20 74 68 72 65 61 64 0a 2a 2a 20 74 68 61 nt thread.** tha
2d770 6e 20 74 68 65 20 6f 6e 65 20 63 6f 6e 74 61 69 n the one contai
2d780 6e 69 6e 67 20 74 68 65 20 61 70 70 6c 69 63 61 ning the applica
2d790 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e tion-defined fun
2d7a0 63 74 69 6f 6e 20 74 68 61 74 20 72 65 63 65 69 ction that recei
2d7b0 76 65 64 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 ved.** the [sqli
2d7c0 74 65 33 5f 63 6f 6e 74 65 78 74 5d 20 70 6f 69 te3_context] poi
2d7d0 6e 74 65 72 2c 20 74 68 65 20 72 65 73 75 6c 74 nter, the result
2d7e0 73 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e s are undefined.
2d7f0 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
2d800 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 34 30 33 5d nts:.** [H16403]
2d810 20 5b 48 31 36 34 30 36 5d 20 5b 48 31 36 34 30 [H16406] [H1640
2d820 39 5d 20 5b 48 31 36 34 31 32 5d 20 5b 48 31 36 9] [H16412] [H16
2d830 34 31 35 5d 20 5b 48 31 36 34 31 38 5d 20 5b 48 415] [H16418] [H
2d840 31 36 34 32 31 5d 20 5b 48 31 36 34 32 34 5d 0a 16421] [H16424].
2d850 2a 2a 20 5b 48 31 36 34 32 37 5d 20 5b 48 31 36 ** [H16427] [H16
2d860 34 33 30 5d 20 5b 48 31 36 34 33 33 5d 20 5b 48 430] [H16433] [H
2d870 31 36 34 33 36 5d 20 5b 48 31 36 34 33 39 5d 20 16436] [H16439]
2d880 5b 48 31 36 34 34 32 5d 20 5b 48 31 36 34 34 35 [H16442] [H16445
2d890 5d 20 5b 48 31 36 34 34 38 5d 0a 2a 2a 20 5b 48 ] [H16448].** [H
2d8a0 31 36 34 35 31 5d 20 5b 48 31 36 34 35 34 5d 20 16451] [H16454]
2d8b0 5b 48 31 36 34 35 37 5d 20 5b 48 31 36 34 36 30 [H16457] [H16460
2d8c0 5d 20 5b 48 31 36 34 36 33 5d 0a 2a 2f 0a 53 51 ] [H16463].*/.SQ
2d8d0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
2d8e0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f lite3_result_blo
2d8f0 62 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 b(sqlite3_contex
2d900 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c t*, const void*,
2d910 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f int, void(*)(vo
2d920 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 id*));.SQLITE_AP
2d930 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
2d940 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 73 71 6c esult_double(sql
2d950 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 64 ite3_context*, d
2d960 6f 75 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 41 ouble);.SQLITE_A
2d970 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
2d980 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 73 71 6c result_error(sql
2d990 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 ite3_context*, c
2d9a0 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 29 onst char*, int)
2d9b0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 ;.SQLITE_API voi
2d9c0 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 d sqlite3_result
2d9d0 5f 65 72 72 6f 72 31 36 28 73 71 6c 69 74 65 33 _error16(sqlite3
2d9e0 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 _context*, const
2d9f0 20 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 53 51 void*, int);.SQ
2da00 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
2da10 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 lite3_result_err
2da20 6f 72 5f 74 6f 6f 62 69 67 28 73 71 6c 69 74 65 or_toobig(sqlite
2da30 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 3_context*);.SQL
2da40 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
2da50 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f ite3_result_erro
2da60 72 5f 6e 6f 6d 65 6d 28 73 71 6c 69 74 65 33 5f r_nomem(sqlite3_
2da70 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 context*);.SQLIT
2da80 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
2da90 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f e3_result_error_
2daa0 63 6f 64 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e code(sqlite3_con
2dab0 74 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c text*, int);.SQL
2dac0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
2dad0 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 ite3_result_int(
2dae0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a sqlite3_context*
2daf0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 , int);.SQLITE_A
2db00 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
2db10 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 73 71 6c result_int64(sql
2db20 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 73 ite3_context*, s
2db30 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 53 qlite3_int64);.S
2db40 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
2db50 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 qlite3_result_nu
2db60 6c 6c 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 ll(sqlite3_conte
2db70 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 xt*);.SQLITE_API
2db80 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 void sqlite3_re
2db90 73 75 6c 74 5f 74 65 78 74 28 73 71 6c 69 74 65 sult_text(sqlite
2dba0 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 3_context*, cons
2dbb0 74 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 76 6f t char*, int, vo
2dbc0 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 id(*)(void*));.S
2dbd0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
2dbe0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 qlite3_result_te
2dbf0 78 74 31 36 28 73 71 6c 69 74 65 33 5f 63 6f 6e xt16(sqlite3_con
2dc00 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 text*, const voi
2dc10 64 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 d*, int, void(*)
2dc20 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 (void*));.SQLITE
2dc30 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
2dc40 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 3_result_text16l
2dc50 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 e(sqlite3_contex
2dc60 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c t*, const void*,
2dc70 20 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 int,void(*)(voi
2dc80 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 d*));.SQLITE_API
2dc90 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 void sqlite3_re
2dca0 73 75 6c 74 5f 74 65 78 74 31 36 62 65 28 73 71 sult_text16be(sq
2dcb0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 lite3_context*,
2dcc0 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 const void*, int
2dcd0 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 ,void(*)(void*))
2dce0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 ;.SQLITE_API voi
2dcf0 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 d sqlite3_result
2dd00 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 63 _value(sqlite3_c
2dd10 6f 6e 74 65 78 74 2a 2c 20 73 71 6c 69 74 65 33 ontext*, sqlite3
2dd20 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 _value*);.SQLITE
2dd30 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
2dd40 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 3_result_zeroblo
2dd50 62 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 b(sqlite3_contex
2dd60 74 2a 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 0a t*, int n);../*.
2dd70 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 66 ** CAPI3REF: Def
2dd80 69 6e 65 20 4e 65 77 20 43 6f 6c 6c 61 74 69 6e ine New Collatin
2dd90 67 20 53 65 71 75 65 6e 63 65 73 20 7b 48 31 36 g Sequences {H16
2dda0 36 30 30 7d 20 3c 53 32 30 33 30 30 3e 0a 2a 2a 600} <S20300>.**
2ddb0 0a 2a 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69 .** These functi
2ddc0 6f 6e 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 ons are used to
2ddd0 61 64 64 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f add new collatio
2dde0 6e 20 73 65 71 75 65 6e 63 65 73 20 74 6f 20 74 n sequences to t
2ddf0 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 he.** [database
2de00 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 65 63 connection] spec
2de10 69 66 69 65 64 20 61 73 20 74 68 65 20 66 69 72 ified as the fir
2de20 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a st argument..**.
2de30 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 ** The name of t
2de40 68 65 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f 6e he new collation
2de50 20 73 65 71 75 65 6e 63 65 20 69 73 20 73 70 65 sequence is spe
2de60 63 69 66 69 65 64 20 61 73 20 61 20 55 54 46 2d cified as a UTF-
2de70 38 20 73 74 72 69 6e 67 0a 2a 2a 20 66 6f 72 20 8 string.** for
2de80 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 sqlite3_create_c
2de90 6f 6c 6c 61 74 69 6f 6e 28 29 20 61 6e 64 20 73 ollation() and s
2dea0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f qlite3_create_co
2deb0 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 0a 2a 2a 20 llation_v2().**
2dec0 61 6e 64 20 61 20 55 54 46 2d 31 36 20 73 74 72 and a UTF-16 str
2ded0 69 6e 67 20 66 6f 72 20 73 71 6c 69 74 65 33 5f ing for sqlite3_
2dee0 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e create_collation
2def0 31 36 28 29 2e 20 49 6e 20 61 6c 6c 20 63 61 73 16(). In all cas
2df00 65 73 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20 69 es.** the name i
2df10 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 s passed as the
2df20 73 65 63 6f 6e 64 20 66 75 6e 63 74 69 6f 6e 20 second function
2df30 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 argument..**.**
2df40 54 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 The third argume
2df50 6e 74 20 6d 61 79 20 62 65 20 6f 6e 65 20 6f 66 nt may be one of
2df60 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 20 5b the constants [
2df70 53 51 4c 49 54 45 5f 55 54 46 38 5d 2c 0a 2a 2a SQLITE_UTF8],.**
2df80 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 [SQLITE_UTF16LE
2df90 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 55 54 ], or [SQLITE_UT
2dfa0 46 31 36 42 45 5d 2c 20 69 6e 64 69 63 61 74 69 F16BE], indicati
2dfb0 6e 67 20 74 68 61 74 20 74 68 65 20 75 73 65 72 ng that the user
2dfc0 2d 73 75 70 70 6c 69 65 64 0a 2a 2a 20 72 6f 75 -supplied.** rou
2dfd0 74 69 6e 65 20 65 78 70 65 63 74 73 20 74 6f 20 tine expects to
2dfe0 62 65 20 70 61 73 73 65 64 20 70 6f 69 6e 74 65 be passed pointe
2dff0 72 73 20 74 6f 20 73 74 72 69 6e 67 73 20 65 6e rs to strings en
2e000 63 6f 64 65 64 20 75 73 69 6e 67 20 55 54 46 2d coded using UTF-
2e010 38 2c 0a 2a 2a 20 55 54 46 2d 31 36 20 6c 69 74 8,.** UTF-16 lit
2e020 74 6c 65 2d 65 6e 64 69 61 6e 2c 20 6f 72 20 55 tle-endian, or U
2e030 54 46 2d 31 36 20 62 69 67 2d 65 6e 64 69 61 6e TF-16 big-endian
2e040 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 , respectively.
2e050 54 68 65 0a 2a 2a 20 74 68 69 72 64 20 61 72 67 The.** third arg
2e060 75 6d 65 6e 74 20 6d 69 67 68 74 20 61 6c 73 6f ument might also
2e070 20 62 65 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 be [SQLITE_UTF1
2e080 36 5d 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 6] to indicate t
2e090 68 61 74 20 74 68 65 20 72 6f 75 74 69 6e 65 0a hat the routine.
2e0a0 2a 2a 20 65 78 70 65 63 74 73 20 70 6f 69 6e 74 ** expects point
2e0b0 65 72 73 20 74 6f 20 62 65 20 55 54 46 2d 31 36 ers to be UTF-16
2e0c0 20 73 74 72 69 6e 67 73 20 69 6e 20 74 68 65 20 strings in the
2e0d0 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 native byte orde
2e0e0 72 2c 20 6f 72 20 74 68 65 0a 2a 2a 20 61 72 67 r, or the.** arg
2e0f0 75 6d 65 6e 74 20 63 61 6e 20 62 65 20 5b 53 51 ument can be [SQ
2e100 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e LITE_UTF16_ALIGN
2e110 45 44 5d 20 69 66 20 74 68 65 0a 2a 2a 20 74 68 ED] if the.** th
2e120 65 20 72 6f 75 74 69 6e 65 20 65 78 70 65 63 74 e routine expect
2e130 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 31 36 s pointers to 16
2e140 2d 62 69 74 20 77 6f 72 64 20 61 6c 69 67 6e 65 -bit word aligne
2e150 64 20 73 74 72 69 6e 67 73 0a 2a 2a 20 6f 66 20 d strings.** of
2e160 55 54 46 2d 31 36 20 69 6e 20 74 68 65 20 6e 61 UTF-16 in the na
2e170 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 2e tive byte order.
2e180 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 .**.** A pointer
2e190 20 74 6f 20 74 68 65 20 75 73 65 72 20 73 75 70 to the user sup
2e1a0 70 6c 69 65 64 20 72 6f 75 74 69 6e 65 20 6d 75 plied routine mu
2e1b0 73 74 20 62 65 20 70 61 73 73 65 64 20 61 73 20 st be passed as
2e1c0 74 68 65 20 66 69 66 74 68 0a 2a 2a 20 61 72 67 the fifth.** arg
2e1d0 75 6d 65 6e 74 2e 20 20 49 66 20 69 74 20 69 73 ument. If it is
2e1e0 20 4e 55 4c 4c 2c 20 74 68 69 73 20 69 73 20 74 NULL, this is t
2e1f0 68 65 20 73 61 6d 65 20 61 73 20 64 65 6c 65 74 he same as delet
2e200 69 6e 67 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f ing the collatio
2e210 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 28 73 n.** sequence (s
2e220 6f 20 74 68 61 74 20 53 51 4c 69 74 65 20 63 61 o that SQLite ca
2e230 6e 6e 6f 74 20 63 61 6c 6c 20 69 74 20 61 6e 79 nnot call it any
2e240 6d 6f 72 65 29 2e 0a 2a 2a 20 45 61 63 68 20 74 more)..** Each t
2e250 69 6d 65 20 74 68 65 20 61 70 70 6c 69 63 61 74 ime the applicat
2e260 69 6f 6e 20 73 75 70 70 6c 69 65 64 20 66 75 6e ion supplied fun
2e270 63 74 69 6f 6e 20 69 73 20 69 6e 76 6f 6b 65 64 ction is invoked
2e280 2c 20 69 74 20 69 73 20 70 61 73 73 65 64 0a 2a , it is passed.*
2e290 2a 20 61 73 20 69 74 73 20 66 69 72 73 74 20 70 * as its first p
2e2a0 61 72 61 6d 65 74 65 72 20 61 20 63 6f 70 79 20 arameter a copy
2e2b0 6f 66 20 74 68 65 20 76 6f 69 64 2a 20 70 61 73 of the void* pas
2e2c0 73 65 64 20 61 73 20 74 68 65 20 66 6f 75 72 74 sed as the fourt
2e2d0 68 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f h argument.** to
2e2e0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f sqlite3_create_
2e2f0 63 6f 6c 6c 61 74 69 6f 6e 28 29 20 6f 72 20 73 collation() or s
2e300 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f qlite3_create_co
2e310 6c 6c 61 74 69 6f 6e 31 36 28 29 2e 0a 2a 2a 0a llation16()..**.
2e320 2a 2a 20 54 68 65 20 72 65 6d 61 69 6e 69 6e 67 ** The remaining
2e330 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 arguments to th
2e340 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 73 75 e application-su
2e350 70 70 6c 69 65 64 20 72 6f 75 74 69 6e 65 20 61 pplied routine a
2e360 72 65 20 74 77 6f 20 73 74 72 69 6e 67 73 2c 0a re two strings,.
2e370 2a 2a 20 65 61 63 68 20 72 65 70 72 65 73 65 6e ** each represen
2e380 74 65 64 20 62 79 20 61 20 28 6c 65 6e 67 74 68 ted by a (length
2e390 2c 20 64 61 74 61 29 20 70 61 69 72 20 61 6e 64 , data) pair and
2e3a0 20 65 6e 63 6f 64 65 64 20 69 6e 20 74 68 65 20 encoded in the
2e3b0 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 74 68 61 74 encoding.** that
2e3c0 20 77 61 73 20 70 61 73 73 65 64 20 61 73 20 74 was passed as t
2e3d0 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e he third argumen
2e3e0 74 20 77 68 65 6e 20 74 68 65 20 63 6f 6c 6c 61 t when the colla
2e3f0 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 61 tion sequence wa
2e400 73 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 64 2e s.** registered.
2e410 20 7b 45 4e 44 7d 20 20 54 68 65 20 61 70 70 6c {END} The appl
2e420 69 63 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 ication defined
2e430 63 6f 6c 6c 61 74 69 6f 6e 20 72 6f 75 74 69 6e collation routin
2e440 65 20 73 68 6f 75 6c 64 0a 2a 2a 20 72 65 74 75 e should.** retu
2e450 72 6e 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 rn negative, zer
2e460 6f 20 6f 72 20 70 6f 73 69 74 69 76 65 20 69 66 o or positive if
2e470 20 74 68 65 20 66 69 72 73 74 20 73 74 72 69 6e the first strin
2e480 67 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 0a g is less than,.
2e490 2a 2a 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 ** equal to, or
2e4a0 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 greater than the
2e4b0 20 73 65 63 6f 6e 64 20 73 74 72 69 6e 67 2e 20 second string.
2e4c0 69 2e 65 2e 20 28 53 54 52 49 4e 47 31 20 2d 20 i.e. (STRING1 -
2e4d0 53 54 52 49 4e 47 32 29 2e 0a 2a 2a 0a 2a 2a 20 STRING2)..**.**
2e4e0 54 68 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 The sqlite3_crea
2e4f0 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 te_collation_v2(
2e500 29 20 77 6f 72 6b 73 20 6c 69 6b 65 20 73 71 6c ) works like sql
2e510 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c ite3_create_coll
2e520 61 74 69 6f 6e 28 29 0a 2a 2a 20 65 78 63 65 70 ation().** excep
2e530 74 20 74 68 61 74 20 69 74 20 74 61 6b 65 73 20 t that it takes
2e540 61 6e 20 65 78 74 72 61 20 61 72 67 75 6d 65 6e an extra argumen
2e550 74 20 77 68 69 63 68 20 69 73 20 61 20 64 65 73 t which is a des
2e560 74 72 75 63 74 6f 72 20 66 6f 72 0a 2a 2a 20 74 tructor for.** t
2e570 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 2e 20 20 54 he collation. T
2e580 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 69 73 he destructor is
2e590 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 called when the
2e5a0 20 63 6f 6c 6c 61 74 69 6f 6e 20 69 73 0a 2a 2a collation is.**
2e5b0 20 64 65 73 74 72 6f 79 65 64 20 61 6e 64 20 69 destroyed and i
2e5c0 73 20 70 61 73 73 65 64 20 61 20 63 6f 70 79 20 s passed a copy
2e5d0 6f 66 20 74 68 65 20 66 6f 75 72 74 68 20 70 61 of the fourth pa
2e5e0 72 61 6d 65 74 65 72 20 76 6f 69 64 2a 20 70 6f rameter void* po
2e5f0 69 6e 74 65 72 0a 2a 2a 20 6f 66 20 74 68 65 20 inter.** of the
2e600 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 sqlite3_create_c
2e610 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 2e 0a 2a ollation_v2()..*
2e620 2a 20 43 6f 6c 6c 61 74 69 6f 6e 73 20 61 72 65 * Collations are
2e630 20 64 65 73 74 72 6f 79 65 64 20 77 68 65 6e 20 destroyed when
2e640 74 68 65 79 20 61 72 65 20 6f 76 65 72 72 69 64 they are overrid
2e650 64 65 6e 20 62 79 20 6c 61 74 65 72 20 63 61 6c den by later cal
2e660 6c 73 20 74 6f 20 74 68 65 0a 2a 2a 20 63 6f 6c ls to the.** col
2e670 6c 61 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20 lation creation
2e680 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20 77 68 65 functions or whe
2e690 6e 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 n the [database
2e6a0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 73 20 63 connection] is c
2e6b0 6c 6f 73 65 64 0a 2a 2a 20 75 73 69 6e 67 20 5b losed.** using [
2e6c0 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 5d sqlite3_close()]
2e6d0 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f ..**.** See also
2e6e0 3a 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 6c : [sqlite3_coll
2e6f0 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29 5d 20 ation_needed()]
2e700 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c and [sqlite3_col
2e710 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 28 lation_needed16(
2e720 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 )]..**.** Requir
2e730 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 36 ements:.** [H166
2e740 30 33 5d 20 5b 48 31 36 36 30 34 5d 20 5b 48 31 03] [H16604] [H1
2e750 36 36 30 36 5d 20 5b 48 31 36 36 30 39 5d 20 5b 6606] [H16609] [
2e760 48 31 36 36 31 32 5d 20 5b 48 31 36 36 31 35 5d H16612] [H16615]
2e770 20 5b 48 31 36 36 31 38 5d 20 5b 48 31 36 36 32 [H16618] [H1662
2e780 31 5d 0a 2a 2a 20 5b 48 31 36 36 32 34 5d 20 5b 1].** [H16624] [
2e790 48 31 36 36 32 37 5d 20 5b 48 31 36 36 33 30 5d H16627] [H16630]
2e7a0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
2e7b0 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 nt sqlite3_creat
2e7c0 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20 73 e_collation(. s
2e7d0 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e 73 qlite3*, . cons
2e7e0 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a t char *zName, .
2e7f0 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 20 int eTextRep,
2e800 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 28 . void*,. int(
2e810 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a *xCompare)(void*
2e820 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a ,int,const void*
2e830 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a ,int,const void*
2e840 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 ).);.SQLITE_API
2e850 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 int sqlite3_crea
2e860 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 te_collation_v2(
2e870 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 . sqlite3*, .
2e880 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d const char *zNam
2e890 65 2c 20 0a 20 20 69 6e 74 20 65 54 65 78 74 52 e, . int eTextR
2e8a0 65 70 2c 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 ep, . void*,.
2e8b0 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 int(*xCompare)(v
2e8c0 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 oid*,int,const v
2e8d0 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 oid*,int,const v
2e8e0 6f 69 64 2a 29 2c 0a 20 20 76 6f 69 64 28 2a 78 oid*),. void(*x
2e8f0 44 65 73 74 72 6f 79 29 28 76 6f 69 64 2a 29 0a Destroy)(void*).
2e900 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
2e910 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 t sqlite3_create
2e920 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 0a 20 20 _collation16(.
2e930 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e sqlite3*, . con
2e940 73 74 20 76 6f 69 64 20 2a 7a 4e 61 6d 65 2c 0a st void *zName,.
2e950 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 20 int eTextRep,
2e960 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 28 . void*,. int(
2e970 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a *xCompare)(void*
2e980 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a ,int,const void*
2e990 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a ,int,const void*
2e9a0 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 ).);../*.** CAPI
2e9b0 33 52 45 46 3a 20 43 6f 6c 6c 61 74 69 6f 6e 20 3REF: Collation
2e9c0 4e 65 65 64 65 64 20 43 61 6c 6c 62 61 63 6b 73 Needed Callbacks
2e9d0 20 7b 48 31 36 37 30 30 7d 20 3c 53 32 30 33 30 {H16700} <S2030
2e9e0 30 3e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 0>.**.** To avoi
2e9f0 64 20 68 61 76 69 6e 67 20 74 6f 20 72 65 67 69 d having to regi
2ea00 73 74 65 72 20 61 6c 6c 20 63 6f 6c 6c 61 74 69 ster all collati
2ea10 6f 6e 20 73 65 71 75 65 6e 63 65 73 20 62 65 66 on sequences bef
2ea20 6f 72 65 20 61 20 64 61 74 61 62 61 73 65 0a 2a ore a database.*
2ea30 2a 20 63 61 6e 20 62 65 20 75 73 65 64 2c 20 61 * can be used, a
2ea40 20 73 69 6e 67 6c 65 20 63 61 6c 6c 62 61 63 6b single callback
2ea50 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 function may be
2ea60 20 72 65 67 69 73 74 65 72 65 64 20 77 69 74 68 registered with
2ea70 20 74 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 the.** [databas
2ea80 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 6f e connection] to
2ea90 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 65 be called whene
2eaa0 76 65 72 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 ver an undefined
2eab0 20 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 73 65 collation.** se
2eac0 71 75 65 6e 63 65 20 69 73 20 72 65 71 75 69 72 quence is requir
2ead0 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ed..**.** If the
2eae0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 67 function is reg
2eaf0 69 73 74 65 72 65 64 20 75 73 69 6e 67 20 74 68 istered using th
2eb00 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 e sqlite3_collat
2eb10 69 6f 6e 5f 6e 65 65 64 65 64 28 29 20 41 50 49 ion_needed() API
2eb20 2c 0a 2a 2a 20 74 68 65 6e 20 69 74 20 69 73 20 ,.** then it is
2eb30 70 61 73 73 65 64 20 74 68 65 20 6e 61 6d 65 73 passed the names
2eb40 20 6f 66 20 75 6e 64 65 66 69 6e 65 64 20 63 6f of undefined co
2eb50 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 llation sequence
2eb60 73 20 61 73 20 73 74 72 69 6e 67 73 0a 2a 2a 20 s as strings.**
2eb70 65 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 encoded in UTF-8
2eb80 2e 20 7b 48 31 36 37 30 33 7d 20 49 66 20 73 71 . {H16703} If sq
2eb90 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f lite3_collation_
2eba0 6e 65 65 64 65 64 31 36 28 29 20 69 73 20 75 73 needed16() is us
2ebb0 65 64 2c 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 73 ed,.** the names
2ebc0 20 61 72 65 20 70 61 73 73 65 64 20 61 73 20 55 are passed as U
2ebd0 54 46 2d 31 36 20 69 6e 20 6d 61 63 68 69 6e 65 TF-16 in machine
2ebe0 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 native byte ord
2ebf0 65 72 2e 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f er..** A call to
2ec00 20 65 69 74 68 65 72 20 66 75 6e 63 74 69 6f 6e either function
2ec10 20 72 65 70 6c 61 63 65 73 20 61 6e 79 20 65 78 replaces any ex
2ec20 69 73 74 69 6e 67 20 63 61 6c 6c 62 61 63 6b 2e isting callback.
2ec30 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 .**.** When the
2ec40 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f callback is invo
2ec50 6b 65 64 2c 20 74 68 65 20 66 69 72 73 74 20 61 ked, the first a
2ec60 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20 69 rgument passed i
2ec70 73 20 61 20 63 6f 70 79 0a 2a 2a 20 6f 66 20 74 s a copy.** of t
2ec80 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
2ec90 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f nt to sqlite3_co
2eca0 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29 llation_needed()
2ecb0 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 or.** sqlite3_c
2ecc0 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 ollation_needed1
2ecd0 36 28 29 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 6(). The second
2ece0 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 argument is the
2ecf0 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e database.** con
2ed00 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 74 68 nection. The th
2ed10 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 ird argument is
2ed20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 55 one of [SQLITE_U
2ed30 54 46 38 5d 2c 20 5b 53 51 4c 49 54 45 5f 55 54 TF8], [SQLITE_UT
2ed40 46 31 36 42 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 F16BE],.** or [S
2ed50 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 5d 2c 20 QLITE_UTF16LE],
2ed60 69 6e 64 69 63 61 74 69 6e 67 20 74 68 65 20 6d indicating the m
2ed70 6f 73 74 20 64 65 73 69 72 61 62 6c 65 20 66 6f ost desirable fo
2ed80 72 6d 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 rm of the collat
2ed90 69 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 ion.** sequence
2eda0 66 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 72 65 function require
2edb0 64 2e 20 20 54 68 65 20 66 6f 75 72 74 68 20 70 d. The fourth p
2edc0 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 arameter is the
2edd0 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20 72 name of the.** r
2ede0 65 71 75 69 72 65 64 20 63 6f 6c 6c 61 74 69 6f equired collatio
2edf0 6e 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2a 0a 2a n sequence..**.*
2ee00 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 * The callback f
2ee10 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 72 unction should r
2ee20 65 67 69 73 74 65 72 20 74 68 65 20 64 65 73 69 egister the desi
2ee30 72 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 75 73 red collation us
2ee40 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f ing.** [sqlite3_
2ee50 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e create_collation
2ee60 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 63 72 ()], [sqlite3_cr
2ee70 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 eate_collation16
2ee80 28 29 5d 2c 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 ()], or.** [sqli
2ee90 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 te3_create_colla
2eea0 74 69 6f 6e 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a tion_v2()]..**.*
2eeb0 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
2eec0 2a 2a 20 5b 48 31 36 37 30 32 5d 20 5b 48 31 36 ** [H16702] [H16
2eed0 37 30 34 5d 20 5b 48 31 36 37 30 36 5d 0a 2a 2f 704] [H16706].*/
2eee0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
2eef0 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f sqlite3_collatio
2ef00 6e 5f 6e 65 65 64 65 64 28 0a 20 20 73 71 6c 69 n_needed(. sqli
2ef10 74 65 33 2a 2c 20 0a 20 20 76 6f 69 64 2a 2c 20 te3*, . void*,
2ef20 0a 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a . void(*)(void*
2ef30 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54 ,sqlite3*,int eT
2ef40 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 63 68 61 extRep,const cha
2ef50 72 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 r*).);.SQLITE_AP
2ef60 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f I int sqlite3_co
2ef70 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 llation_needed16
2ef80 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 (. sqlite3*, .
2ef90 20 76 6f 69 64 2a 2c 0a 20 20 76 6f 69 64 28 2a void*,. void(*
2efa0 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a )(void*,sqlite3*
2efb0 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f ,int eTextRep,co
2efc0 6e 73 74 20 76 6f 69 64 2a 29 0a 29 3b 0a 0a 2f nst void*).);../
2efd0 2a 0a 2a 2a 20 53 70 65 63 69 66 79 20 74 68 65 *.** Specify the
2efe0 20 6b 65 79 20 66 6f 72 20 61 6e 20 65 6e 63 72 key for an encr
2eff0 79 70 74 65 64 20 64 61 74 61 62 61 73 65 2e 20 ypted database.
2f000 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 This routine sh
2f010 6f 75 6c 64 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 ould be.** calle
2f020 64 20 72 69 67 68 74 20 61 66 74 65 72 20 73 71 d right after sq
2f030 6c 69 74 65 33 5f 6f 70 65 6e 28 29 2e 0a 2a 2a lite3_open()..**
2f040 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 74 6f 20 .** The code to
2f050 69 6d 70 6c 65 6d 65 6e 74 20 74 68 69 73 20 41 implement this A
2f060 50 49 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 PI is not availa
2f070 62 6c 65 20 69 6e 20 74 68 65 20 70 75 62 6c 69 ble in the publi
2f080 63 20 72 65 6c 65 61 73 65 0a 2a 2a 20 6f 66 20 c release.** of
2f090 53 51 4c 69 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 SQLite..*/.SQLIT
2f0a0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
2f0b0 33 5f 6b 65 79 28 0a 20 20 73 71 6c 69 74 65 33 3_key(. sqlite3
2f0c0 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 *db,
2f0d0 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 /* Datab
2f0e0 61 73 65 20 74 6f 20 62 65 20 72 65 6b 65 79 65 ase to be rekeye
2f0f0 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 d */. const voi
2f100 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 d *pKey, int nKe
2f110 79 20 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 y /* The key
2f120 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 68 */.);../*.** Ch
2f130 61 6e 67 65 20 74 68 65 20 6b 65 79 20 6f 6e 20 ange the key on
2f140 61 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 an open database
2f150 2e 20 20 49 66 20 74 68 65 20 63 75 72 72 65 6e . If the curren
2f160 74 20 64 61 74 61 62 61 73 65 20 69 73 20 6e 6f t database is no
2f170 74 0a 2a 2a 20 65 6e 63 72 79 70 74 65 64 2c 20 t.** encrypted,
2f180 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c this routine wil
2f190 6c 20 65 6e 63 72 79 70 74 20 69 74 2e 20 20 49 l encrypt it. I
2f1a0 66 20 70 4e 65 77 3d 3d 30 20 6f 72 20 6e 4e 65 f pNew==0 or nNe
2f1b0 77 3d 3d 30 2c 20 74 68 65 0a 2a 2a 20 64 61 74 w==0, the.** dat
2f1c0 61 62 61 73 65 20 69 73 20 64 65 63 72 79 70 74 abase is decrypt
2f1d0 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f ed..**.** The co
2f1e0 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 de to implement
2f1f0 74 68 69 73 20 41 50 49 20 69 73 20 6e 6f 74 20 this API is not
2f200 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 available in the
2f210 20 70 75 62 6c 69 63 20 72 65 6c 65 61 73 65 0a public release.
2f220 2a 2a 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f ** of SQLite..*/
2f230 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
2f240 73 71 6c 69 74 65 33 5f 72 65 6b 65 79 28 0a 20 sqlite3_rekey(.
2f250 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 sqlite3 *db,
2f260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2f270 2f 2a 20 44 61 74 61 62 61 73 65 20 74 6f 20 62 /* Database to b
2f280 65 20 72 65 6b 65 79 65 64 20 2a 2f 0a 20 20 63 e rekeyed */. c
2f290 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c onst void *pKey,
2f2a0 20 69 6e 74 20 6e 4b 65 79 20 20 20 20 20 2f 2a int nKey /*
2f2b0 20 54 68 65 20 6e 65 77 20 6b 65 79 20 2a 2f 0a The new key */.
2f2c0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
2f2d0 45 46 3a 20 53 75 73 70 65 6e 64 20 45 78 65 63 EF: Suspend Exec
2f2e0 75 74 69 6f 6e 20 46 6f 72 20 41 20 53 68 6f 72 ution For A Shor
2f2f0 74 20 54 69 6d 65 20 7b 48 31 30 35 33 30 7d 20 t Time {H10530}
2f300 3c 53 34 30 34 31 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S40410>.**.** T
2f310 68 65 20 73 71 6c 69 74 65 33 5f 73 6c 65 65 70 he sqlite3_sleep
2f320 28 29 20 66 75 6e 63 74 69 6f 6e 20 63 61 75 73 () function caus
2f330 65 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 es the current t
2f340 68 72 65 61 64 20 74 6f 20 73 75 73 70 65 6e 64 hread to suspend
2f350 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 66 6f execution.** fo
2f360 72 20 61 74 20 6c 65 61 73 74 20 61 20 6e 75 6d r at least a num
2f370 62 65 72 20 6f 66 20 6d 69 6c 6c 69 73 65 63 6f ber of milliseco
2f380 6e 64 73 20 73 70 65 63 69 66 69 65 64 20 69 6e nds specified in
2f390 20 69 74 73 20 70 61 72 61 6d 65 74 65 72 2e 0a its parameter..
2f3a0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6f 70 65 **.** If the ope
2f3b0 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 64 6f rating system do
2f3c0 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 73 es not support s
2f3d0 6c 65 65 70 20 72 65 71 75 65 73 74 73 20 77 69 leep requests wi
2f3e0 74 68 0a 2a 2a 20 6d 69 6c 6c 69 73 65 63 6f 6e th.** millisecon
2f3f0 64 20 74 69 6d 65 20 72 65 73 6f 6c 75 74 69 6f d time resolutio
2f400 6e 2c 20 74 68 65 6e 20 74 68 65 20 74 69 6d 65 n, then the time
2f410 20 77 69 6c 6c 20 62 65 20 72 6f 75 6e 64 65 64 will be rounded
2f420 20 75 70 20 74 6f 0a 2a 2a 20 74 68 65 20 6e 65 up to.** the ne
2f430 61 72 65 73 74 20 73 65 63 6f 6e 64 2e 20 54 68 arest second. Th
2f440 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6c 6c e number of mill
2f450 69 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 iseconds of slee
2f460 70 20 61 63 74 75 61 6c 6c 79 0a 2a 2a 20 72 65 p actually.** re
2f470 71 75 65 73 74 65 64 20 66 72 6f 6d 20 74 68 65 quested from the
2f480 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 operating syste
2f490 6d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a m is returned..*
2f4a0 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6d 70 6c *.** SQLite impl
2f4b0 65 6d 65 6e 74 73 20 74 68 69 73 20 69 6e 74 65 ements this inte
2f4c0 72 66 61 63 65 20 62 79 20 63 61 6c 6c 69 6e 67 rface by calling
2f4d0 20 74 68 65 20 78 53 6c 65 65 70 28 29 0a 2a 2a the xSleep().**
2f4e0 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 64 method of the d
2f4f0 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f efault [sqlite3_
2f500 76 66 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a vfs] object..**.
2f510 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
2f520 20 5b 48 31 30 35 33 33 5d 20 5b 48 31 30 35 33 [H10533] [H1053
2f530 36 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 6].*/.SQLITE_API
2f540 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 6c 65 int sqlite3_sle
2f550 65 70 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 ep(int);../*.**
2f560 43 41 50 49 33 52 45 46 3a 20 4e 61 6d 65 20 4f CAPI3REF: Name O
2f570 66 20 54 68 65 20 46 6f 6c 64 65 72 20 48 6f 6c f The Folder Hol
2f580 64 69 6e 67 20 54 65 6d 70 6f 72 61 72 79 20 46 ding Temporary F
2f590 69 6c 65 73 20 7b 48 31 30 33 31 30 7d 20 3c 53 iles {H10310} <S
2f5a0 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 20000>.**.** If
2f5b0 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 61 72 69 this global vari
2f5c0 61 62 6c 65 20 69 73 20 6d 61 64 65 20 74 6f 20 able is made to
2f5d0 70 6f 69 6e 74 20 74 6f 20 61 20 73 74 72 69 6e point to a strin
2f5e0 67 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 74 68 g which is.** th
2f5f0 65 20 6e 61 6d 65 20 6f 66 20 61 20 66 6f 6c 64 e name of a fold
2f600 65 72 20 28 61 2e 6b 2e 61 2e 20 64 69 72 65 63 er (a.k.a. direc
2f610 74 6f 72 79 29 2c 20 74 68 65 6e 20 61 6c 6c 20 tory), then all
2f620 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 0a temporary files.
2f630 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20 53 51 ** created by SQ
2f640 4c 69 74 65 20 77 69 6c 6c 20 62 65 20 70 6c 61 Lite will be pla
2f650 63 65 64 20 69 6e 20 74 68 61 74 20 64 69 72 65 ced in that dire
2f660 63 74 6f 72 79 2e 20 20 49 66 20 74 68 69 73 20 ctory. If this
2f670 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69 73 20 61 variable.** is a
2f680 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 74 NULL pointer, t
2f690 68 65 6e 20 53 51 4c 69 74 65 20 70 65 72 66 6f hen SQLite perfo
2f6a0 72 6d 73 20 61 20 73 65 61 72 63 68 20 66 6f 72 rms a search for
2f6b0 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 0a an appropriate.
2f6c0 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c ** temporary fil
2f6d0 65 20 64 69 72 65 63 74 6f 72 79 2e 0a 2a 2a 0a e directory..**.
2f6e0 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 73 61 66 ** It is not saf
2f6f0 65 20 74 6f 20 72 65 61 64 20 6f 72 20 6d 6f 64 e to read or mod
2f700 69 66 79 20 74 68 69 73 20 76 61 72 69 61 62 6c ify this variabl
2f710 65 20 69 6e 20 6d 6f 72 65 20 74 68 61 6e 20 6f e in more than o
2f720 6e 65 0a 2a 2a 20 74 68 72 65 61 64 20 61 74 20 ne.** thread at
2f730 61 20 74 69 6d 65 2e 20 20 49 74 20 69 73 20 6e a time. It is n
2f740 6f 74 20 73 61 66 65 20 74 6f 20 72 65 61 64 20 ot safe to read
2f750 6f 72 20 6d 6f 64 69 66 79 20 74 68 69 73 20 76 or modify this v
2f760 61 72 69 61 62 6c 65 0a 2a 2a 20 69 66 20 61 20 ariable.** if a
2f770 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
2f780 74 69 6f 6e 5d 20 69 73 20 62 65 69 6e 67 20 75 tion] is being u
2f790 73 65 64 20 61 74 20 74 68 65 20 73 61 6d 65 20 sed at the same
2f7a0 74 69 6d 65 20 69 6e 20 61 20 73 65 70 61 72 61 time in a separa
2f7b0 74 65 0a 2a 2a 20 74 68 72 65 61 64 2e 0a 2a 2a te.** thread..**
2f7c0 20 49 74 20 69 73 20 69 6e 74 65 6e 64 65 64 20 It is intended
2f7d0 74 68 61 74 20 74 68 69 73 20 76 61 72 69 61 62 that this variab
2f7e0 6c 65 20 62 65 20 73 65 74 20 6f 6e 63 65 0a 2a le be set once.*
2f7f0 2a 20 61 73 20 70 61 72 74 20 6f 66 20 70 72 6f * as part of pro
2f800 63 65 73 73 20 69 6e 69 74 69 61 6c 69 7a 61 74 cess initializat
2f810 69 6f 6e 20 61 6e 64 20 62 65 66 6f 72 65 20 61 ion and before a
2f820 6e 79 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 ny SQLite interf
2f830 61 63 65 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 ace.** routines
2f840 68 61 76 65 20 62 65 65 6e 20 63 61 6c 6c 65 64 have been called
2f850 20 61 6e 64 20 74 68 61 74 20 74 68 69 73 20 76 and that this v
2f860 61 72 69 61 62 6c 65 20 72 65 6d 61 69 6e 20 75 ariable remain u
2f870 6e 63 68 61 6e 67 65 64 0a 2a 2a 20 74 68 65 72 nchanged.** ther
2f880 65 61 66 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 eafter..**.** Th
2f890 65 20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 e [temp_store_di
2f8a0 72 65 63 74 6f 72 79 20 70 72 61 67 6d 61 5d 20 rectory pragma]
2f8b0 6d 61 79 20 6d 6f 64 69 66 79 20 74 68 69 73 20 may modify this
2f8c0 76 61 72 69 61 62 6c 65 20 61 6e 64 20 63 61 75 variable and cau
2f8d0 73 65 0a 2a 2a 20 69 74 20 74 6f 20 70 6f 69 6e se.** it to poin
2f8e0 74 20 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 t to memory obta
2f8f0 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 ined from [sqlit
2f900 65 33 5f 6d 61 6c 6c 6f 63 5d 2e 20 20 46 75 72 e3_malloc]. Fur
2f910 74 68 65 72 6d 6f 72 65 2c 0a 2a 2a 20 74 68 65 thermore,.** the
2f920 20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 [temp_store_dir
2f930 65 63 74 6f 72 79 20 70 72 61 67 6d 61 5d 20 61 ectory pragma] a
2f940 6c 77 61 79 73 20 61 73 73 75 6d 65 73 20 74 68 lways assumes th
2f950 61 74 20 61 6e 79 20 73 74 72 69 6e 67 0a 2a 2a at any string.**
2f960 20 74 68 61 74 20 74 68 69 73 20 76 61 72 69 61 that this varia
2f970 62 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 69 73 ble points to is
2f980 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 20 held in memory
2f990 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 0a 2a obtained from .*
2f9a0 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f * [sqlite3_mallo
2f9b0 63 5d 20 61 6e 64 20 74 68 65 20 70 72 61 67 6d c] and the pragm
2f9c0 61 20 6d 61 79 20 61 74 74 65 6d 70 74 20 74 6f a may attempt to
2f9d0 20 66 72 65 65 20 74 68 61 74 20 6d 65 6d 6f 72 free that memor
2f9e0 79 0a 2a 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 y.** using [sqli
2f9f0 74 65 33 5f 66 72 65 65 5d 2e 0a 2a 2a 20 48 65 te3_free]..** He
2fa00 6e 63 65 2c 20 69 66 20 74 68 69 73 20 76 61 72 nce, if this var
2fa10 69 61 62 6c 65 20 69 73 20 6d 6f 64 69 66 69 65 iable is modifie
2fa20 64 20 64 69 72 65 63 74 6c 79 2c 20 65 69 74 68 d directly, eith
2fa30 65 72 20 69 74 20 73 68 6f 75 6c 64 20 62 65 0a er it should be.
2fa40 2a 2a 20 6d 61 64 65 20 4e 55 4c 4c 20 6f 72 20 ** made NULL or
2fa50 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f made to point to
2fa60 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 memory obtained
2fa70 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d from [sqlite3_m
2fa80 61 6c 6c 6f 63 5d 0a 2a 2a 20 6f 72 20 65 6c 73 alloc].** or els
2fa90 65 20 74 68 65 20 75 73 65 20 6f 66 20 74 68 65 e the use of the
2faa0 20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 [temp_store_dir
2fab0 65 63 74 6f 72 79 20 70 72 61 67 6d 61 5d 20 73 ectory pragma] s
2fac0 68 6f 75 6c 64 20 62 65 20 61 76 6f 69 64 65 64 hould be avoided
2fad0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
2fae0 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 74 65 char *sqlite3_te
2faf0 6d 70 5f 64 69 72 65 63 74 6f 72 79 3b 0a 0a 2f mp_directory;../
2fb00 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54 *.** CAPI3REF: T
2fb10 65 73 74 20 46 6f 72 20 41 75 74 6f 2d 43 6f 6d est For Auto-Com
2fb20 6d 69 74 20 4d 6f 64 65 20 7b 48 31 32 39 33 30 mit Mode {H12930
2fb30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 4b 45 } <S60200>.** KE
2fb40 59 57 4f 52 44 53 3a 20 7b 61 75 74 6f 63 6f 6d YWORDS: {autocom
2fb50 6d 69 74 20 6d 6f 64 65 7d 0a 2a 2a 0a 2a 2a 20 mit mode}.**.**
2fb60 54 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f The sqlite3_get_
2fb70 61 75 74 6f 63 6f 6d 6d 69 74 28 29 20 69 6e 74 autocommit() int
2fb80 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 6e erface returns n
2fb90 6f 6e 2d 7a 65 72 6f 20 6f 72 0a 2a 2a 20 7a 65 on-zero or.** ze
2fba0 72 6f 20 69 66 20 74 68 65 20 67 69 76 65 6e 20 ro if the given
2fbb0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
2fbc0 69 6f 6e 20 69 73 20 6f 72 20 69 73 20 6e 6f 74 ion is or is not
2fbd0 20 69 6e 20 61 75 74 6f 63 6f 6d 6d 69 74 20 6d in autocommit m
2fbe0 6f 64 65 2c 0a 2a 2a 20 72 65 73 70 65 63 74 69 ode,.** respecti
2fbf0 76 65 6c 79 2e 20 20 41 75 74 6f 63 6f 6d 6d 69 vely. Autocommi
2fc00 74 20 6d 6f 64 65 20 69 73 20 6f 6e 20 62 79 20 t mode is on by
2fc10 64 65 66 61 75 6c 74 2e 0a 2a 2a 20 41 75 74 6f default..** Auto
2fc20 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73 20 64 commit mode is d
2fc30 69 73 61 62 6c 65 64 20 62 79 20 61 20 5b 42 45 isabled by a [BE
2fc40 47 49 4e 5d 20 73 74 61 74 65 6d 65 6e 74 2e 0a GIN] statement..
2fc50 2a 2a 20 41 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f ** Autocommit mo
2fc60 64 65 20 69 73 20 72 65 2d 65 6e 61 62 6c 65 64 de is re-enabled
2fc70 20 62 79 20 61 20 5b 43 4f 4d 4d 49 54 5d 20 6f by a [COMMIT] o
2fc80 72 20 5b 52 4f 4c 4c 42 41 43 4b 5d 2e 0a 2a 2a r [ROLLBACK]..**
2fc90 0a 2a 2a 20 49 66 20 63 65 72 74 61 69 6e 20 6b .** If certain k
2fca0 69 6e 64 73 20 6f 66 20 65 72 72 6f 72 73 20 6f inds of errors o
2fcb0 63 63 75 72 20 6f 6e 20 61 20 73 74 61 74 65 6d ccur on a statem
2fcc0 65 6e 74 20 77 69 74 68 69 6e 20 61 20 6d 75 6c ent within a mul
2fcd0 74 69 2d 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 ti-statement.**
2fce0 74 72 61 6e 73 61 63 74 69 6f 6e 20 28 65 72 72 transaction (err
2fcf0 6f 72 73 20 69 6e 63 6c 75 64 69 6e 67 20 5b 53 ors including [S
2fd00 51 4c 49 54 45 5f 46 55 4c 4c 5d 2c 20 5b 53 51 QLITE_FULL], [SQ
2fd10 4c 49 54 45 5f 49 4f 45 52 52 5d 2c 0a 2a 2a 20 LITE_IOERR],.**
2fd20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2c 20 [SQLITE_NOMEM],
2fd30 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 2c 20 61 [SQLITE_BUSY], a
2fd40 6e 64 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 52 nd [SQLITE_INTER
2fd50 52 55 50 54 5d 29 20 74 68 65 6e 20 74 68 65 0a RUPT]) then the.
2fd60 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d ** transaction m
2fd70 69 67 68 74 20 62 65 20 72 6f 6c 6c 65 64 20 62 ight be rolled b
2fd80 61 63 6b 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c ack automaticall
2fd90 79 2e 20 20 54 68 65 20 6f 6e 6c 79 20 77 61 79 y. The only way
2fda0 20 74 6f 0a 2a 2a 20 66 69 6e 64 20 6f 75 74 20 to.** find out
2fdb0 77 68 65 74 68 65 72 20 53 51 4c 69 74 65 20 61 whether SQLite a
2fdc0 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72 6f 6c utomatically rol
2fdd0 6c 65 64 20 62 61 63 6b 20 74 68 65 20 74 72 61 led back the tra
2fde0 6e 73 61 63 74 69 6f 6e 20 61 66 74 65 72 0a 2a nsaction after.*
2fdf0 2a 20 61 6e 20 65 72 72 6f 72 20 69 73 20 74 6f * an error is to
2fe00 20 75 73 65 20 74 68 69 73 20 66 75 6e 63 74 69 use this functi
2fe10 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 6f on..**.** If ano
2fe20 74 68 65 72 20 74 68 72 65 61 64 20 63 68 61 6e ther thread chan
2fe30 67 65 73 20 74 68 65 20 61 75 74 6f 63 6f 6d 6d ges the autocomm
2fe40 69 74 20 73 74 61 74 75 73 20 6f 66 20 74 68 65 it status of the
2fe50 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e database.** con
2fe60 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 74 68 nection while th
2fe70 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 72 75 is routine is ru
2fe80 6e 6e 69 6e 67 2c 20 74 68 65 6e 20 74 68 65 20 nning, then the
2fe90 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 return value.**
2fea0 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a is undefined..**
2feb0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
2fec0 3a 20 5b 48 31 32 39 33 31 5d 20 5b 48 31 32 39 : [H12931] [H129
2fed0 33 32 5d 20 5b 48 31 32 39 33 33 5d 20 5b 48 31 32] [H12933] [H1
2fee0 32 39 33 34 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 2934].*/.SQLITE_
2fef0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
2ff00 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 28 73 get_autocommit(s
2ff10 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a qlite3*);../*.**
2ff20 20 43 41 50 49 33 52 45 46 3a 20 46 69 6e 64 20 CAPI3REF: Find
2ff30 54 68 65 20 44 61 74 61 62 61 73 65 20 48 61 6e The Database Han
2ff40 64 6c 65 20 4f 66 20 41 20 50 72 65 70 61 72 65 dle Of A Prepare
2ff50 64 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 31 33 d Statement {H13
2ff60 31 32 30 7d 20 3c 53 36 30 36 30 30 3e 0a 2a 2a 120} <S60600>.**
2ff70 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
2ff80 64 62 5f 68 61 6e 64 6c 65 20 69 6e 74 65 72 66 db_handle interf
2ff90 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 ace returns the
2ffa0 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
2ffb0 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 0a 2a 2a 20 tion] handle.**
2ffc0 74 6f 20 77 68 69 63 68 20 61 20 5b 70 72 65 70 to which a [prep
2ffd0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 ared statement]
2ffe0 62 65 6c 6f 6e 67 73 2e 20 20 54 68 65 20 5b 64 belongs. The [d
2fff0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
30000 6f 6e 5d 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 on].** returned
30010 62 79 20 73 71 6c 69 74 65 33 5f 64 62 5f 68 61 by sqlite3_db_ha
30020 6e 64 6c 65 20 69 73 20 74 68 65 20 73 61 6d 65 ndle is the same
30030 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
30040 63 74 69 6f 6e 5d 20 74 68 61 74 20 77 61 73 20 ction] that was
30050 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 the first argume
30060 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20 5b 73 71 nt.** to the [sq
30070 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
30080 28 29 5d 20 63 61 6c 6c 20 28 6f 72 20 69 74 73 ()] call (or its
30090 20 76 61 72 69 61 6e 74 73 29 20 74 68 61 74 20 variants) that
300a0 77 61 73 20 75 73 65 64 20 74 6f 0a 2a 2a 20 63 was used to.** c
300b0 72 65 61 74 65 20 74 68 65 20 73 74 61 74 65 6d reate the statem
300c0 65 6e 74 20 69 6e 20 74 68 65 20 66 69 72 73 74 ent in the first
300d0 20 70 6c 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 place..**.** Re
300e0 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 33 quirements: [H13
300f0 31 32 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 123].*/.SQLITE_A
30100 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 PI sqlite3 *sqli
30110 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 te3_db_handle(sq
30120 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a 2f lite3_stmt*);../
30130 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 *.** CAPI3REF: F
30140 69 6e 64 20 74 68 65 20 6e 65 78 74 20 70 72 65 ind the next pre
30150 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 pared statement
30160 7b 48 31 33 31 34 30 7d 20 3c 53 36 30 36 30 30 {H13140} <S60600
30170 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 >.**.** This int
30180 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 erface returns a
30190 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
301a0 6e 65 78 74 20 5b 70 72 65 70 61 72 65 64 20 73 next [prepared s
301b0 74 61 74 65 6d 65 6e 74 5d 20 61 66 74 65 72 0a tatement] after.
301c0 2a 2a 20 70 53 74 6d 74 20 61 73 73 6f 63 69 61 ** pStmt associa
301d0 74 65 64 20 77 69 74 68 20 74 68 65 20 5b 64 61 ted with the [da
301e0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
301f0 6e 5d 20 70 44 62 2e 20 20 49 66 20 70 53 74 6d n] pDb. If pStm
30200 74 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 65 t is NULL.** the
30210 6e 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 n this interface
30220 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 returns a point
30230 65 72 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 er to the first
30240 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
30250 6e 74 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 nt.** associated
30260 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61 with the databa
30270 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 44 se connection pD
30280 62 2e 20 20 49 66 20 6e 6f 20 70 72 65 70 61 72 b. If no prepar
30290 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 ed statement.**
302a0 73 61 74 69 73 66 69 65 73 20 74 68 65 20 63 6f satisfies the co
302b0 6e 64 69 74 69 6f 6e 73 20 6f 66 20 74 68 69 73 nditions of this
302c0 20 72 6f 75 74 69 6e 65 2c 20 69 74 20 72 65 74 routine, it ret
302d0 75 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a urns NULL..**.**
302e0 20 54 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 The [database c
302f0 6f 6e 6e 65 63 74 69 6f 6e 5d 20 70 6f 69 6e 74 onnection] point
30300 65 72 20 44 20 69 6e 20 61 20 63 61 6c 6c 20 74 er D in a call t
30310 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6e 65 o.** [sqlite3_ne
30320 78 74 5f 73 74 6d 74 28 44 2c 53 29 5d 20 6d 75 xt_stmt(D,S)] mu
30330 73 74 20 72 65 66 65 72 20 74 6f 20 61 6e 20 6f st refer to an o
30340 70 65 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 pen database.**
30350 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 6e 64 20 69 connection and i
30360 6e 20 70 61 72 74 69 63 75 6c 61 72 20 6d 75 73 n particular mus
30370 74 20 6e 6f 74 20 62 65 20 61 20 4e 55 4c 4c 20 t not be a NULL
30380 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 52 pointer..**.** R
30390 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 equirements: [H1
303a0 33 31 34 33 5d 20 5b 48 31 33 31 34 36 5d 20 5b 3143] [H13146] [
303b0 48 31 33 31 34 39 5d 20 5b 48 31 33 31 35 32 5d H13149] [H13152]
303c0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 .*/.SQLITE_API s
303d0 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 71 6c qlite3_stmt *sql
303e0 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 73 ite3_next_stmt(s
303f0 71 6c 69 74 65 33 20 2a 70 44 62 2c 20 73 71 6c qlite3 *pDb, sql
30400 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
30410 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
30420 45 46 3a 20 43 6f 6d 6d 69 74 20 41 6e 64 20 52 EF: Commit And R
30430 6f 6c 6c 62 61 63 6b 20 4e 6f 74 69 66 69 63 61 ollback Notifica
30440 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73 20 7b tion Callbacks {
30450 48 31 32 39 35 30 7d 20 3c 53 36 30 34 30 30 3e H12950} <S60400>
30460 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
30470 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 29 e3_commit_hook()
30480 20 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 interface regis
30490 74 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b 0a ters a callback.
304a0 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 ** function to b
304b0 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76 e invoked whenev
304c0 65 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e er a transaction
304d0 20 69 73 20 5b 43 4f 4d 4d 49 54 20 7c 20 63 6f is [COMMIT | co
304e0 6d 6d 69 74 74 65 64 5d 2e 0a 2a 2a 20 41 6e 79 mmitted]..** Any
304f0 20 63 61 6c 6c 62 61 63 6b 20 73 65 74 20 62 79 callback set by
30500 20 61 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c a previous call
30510 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6d 6d to sqlite3_comm
30520 69 74 5f 68 6f 6f 6b 28 29 0a 2a 2a 20 66 6f 72 it_hook().** for
30530 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 the same databa
30540 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 se connection is
30550 20 6f 76 65 72 72 69 64 64 65 6e 2e 0a 2a 2a 20 overridden..**
30560 54 68 65 20 73 71 6c 69 74 65 33 5f 72 6f 6c 6c The sqlite3_roll
30570 62 61 63 6b 5f 68 6f 6f 6b 28 29 20 69 6e 74 65 back_hook() inte
30580 72 66 61 63 65 20 72 65 67 69 73 74 65 72 73 20 rface registers
30590 61 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 66 75 a callback.** fu
305a0 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 69 6e 76 nction to be inv
305b0 6f 6b 65 64 20 77 68 65 6e 65 76 65 72 20 61 20 oked whenever a
305c0 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 5b transaction is [
305d0 52 4f 4c 4c 42 41 43 4b 20 7c 20 72 6f 6c 6c 65 ROLLBACK | rolle
305e0 64 20 62 61 63 6b 5d 2e 0a 2a 2a 20 41 6e 79 20 d back]..** Any
305f0 63 61 6c 6c 62 61 63 6b 20 73 65 74 20 62 79 20 callback set by
30600 61 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 20 a previous call
30610 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 to sqlite3_commi
30620 74 5f 68 6f 6f 6b 28 29 0a 2a 2a 20 66 6f 72 20 t_hook().** for
30630 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 the same databas
30640 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 e connection is
30650 6f 76 65 72 72 69 64 64 65 6e 2e 0a 2a 2a 20 54 overridden..** T
30660 68 65 20 70 41 72 67 20 61 72 67 75 6d 65 6e 74 he pArg argument
30670 20 69 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 is passed throu
30680 67 68 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 gh to the callba
30690 63 6b 2e 0a 2a 2a 20 49 66 20 74 68 65 20 63 61 ck..** If the ca
306a0 6c 6c 62 61 63 6b 20 6f 6e 20 61 20 63 6f 6d 6d llback on a comm
306b0 69 74 20 68 6f 6f 6b 20 66 75 6e 63 74 69 6f 6e it hook function
306c0 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 returns non-zer
306d0 6f 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 o,.** then the c
306e0 6f 6d 6d 69 74 20 69 73 20 63 6f 6e 76 65 72 74 ommit is convert
306f0 65 64 20 69 6e 74 6f 20 61 20 72 6f 6c 6c 62 61 ed into a rollba
30700 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 6f ck..**.** If ano
30710 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 20 77 61 ther function wa
30720 73 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 67 s previously reg
30730 69 73 74 65 72 65 64 2c 20 69 74 73 0a 2a 2a 20 istered, its.**
30740 70 41 72 67 20 76 61 6c 75 65 20 69 73 20 72 65 pArg value is re
30750 74 75 72 6e 65 64 2e 20 20 4f 74 68 65 72 77 69 turned. Otherwi
30760 73 65 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 se NULL is retur
30770 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 ned..**.** The c
30780 61 6c 6c 62 61 63 6b 20 69 6d 70 6c 65 6d 65 6e allback implemen
30790 74 61 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 tation must not
307a0 64 6f 20 61 6e 79 74 68 69 6e 67 20 74 68 61 74 do anything that
307b0 20 77 69 6c 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 will modify.**
307c0 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e the database con
307d0 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 69 6e 76 nection that inv
307e0 6f 6b 65 64 20 74 68 65 20 63 61 6c 6c 62 61 63 oked the callbac
307f0 6b 2e 20 20 41 6e 79 20 61 63 74 69 6f 6e 73 0a k. Any actions.
30800 2a 2a 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 65 ** to modify the
30810 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
30820 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 64 65 66 tion must be def
30830 65 72 72 65 64 20 75 6e 74 69 6c 20 61 66 74 65 erred until afte
30840 72 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74 r the.** complet
30850 69 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 ion of the [sqli
30860 74 65 33 5f 73 74 65 70 28 29 5d 20 63 61 6c 6c te3_step()] call
30870 20 74 68 61 74 20 74 72 69 67 67 65 72 65 64 20 that triggered
30880 74 68 65 20 63 6f 6d 6d 69 74 0a 2a 2a 20 6f 72 the commit.** or
30890 20 72 6f 6c 6c 62 61 63 6b 20 68 6f 6f 6b 20 69 rollback hook i
308a0 6e 20 74 68 65 20 66 69 72 73 74 20 70 6c 61 63 n the first plac
308b0 65 2e 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 e..** Note that
308c0 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
308d0 5f 76 32 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 _v2()] and [sqli
308e0 74 65 33 5f 73 74 65 70 28 29 5d 20 62 6f 74 68 te3_step()] both
308f0 20 6d 6f 64 69 66 79 20 74 68 65 69 72 0a 2a 2a modify their.**
30900 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
30910 74 69 6f 6e 73 20 66 6f 72 20 74 68 65 20 6d 65 tions for the me
30920 61 6e 69 6e 67 20 6f 66 20 22 6d 6f 64 69 66 79 aning of "modify
30930 22 20 69 6e 20 74 68 69 73 20 70 61 72 61 67 72 " in this paragr
30940 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 aph..**.** Regis
30950 74 65 72 69 6e 67 20 61 20 4e 55 4c 4c 20 66 75 tering a NULL fu
30960 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 20 nction disables
30970 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a the callback..**
30980 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 63 6f 6d .** When the com
30990 6d 69 74 20 68 6f 6f 6b 20 63 61 6c 6c 62 61 63 mit hook callbac
309a0 6b 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e k routine return
309b0 73 20 7a 65 72 6f 2c 20 74 68 65 20 5b 43 4f 4d s zero, the [COM
309c0 4d 49 54 5d 0a 2a 2a 20 6f 70 65 72 61 74 69 6f MIT].** operatio
309d0 6e 20 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f 20 n is allowed to
309e0 63 6f 6e 74 69 6e 75 65 20 6e 6f 72 6d 61 6c 6c continue normall
309f0 79 2e 20 20 49 66 20 74 68 65 20 63 6f 6d 6d 69 y. If the commi
30a00 74 20 68 6f 6f 6b 0a 2a 2a 20 72 65 74 75 72 6e t hook.** return
30a10 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e s non-zero, then
30a20 20 74 68 65 20 5b 43 4f 4d 4d 49 54 5d 20 69 73 the [COMMIT] is
30a30 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 converted into
30a40 61 20 5b 52 4f 4c 4c 42 41 43 4b 5d 2e 0a 2a 2a a [ROLLBACK]..**
30a50 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 68 6f The rollback ho
30a60 6f 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e ok is invoked on
30a70 20 61 20 72 6f 6c 6c 62 61 63 6b 20 74 68 61 74 a rollback that
30a80 20 72 65 73 75 6c 74 73 20 66 72 6f 6d 20 61 20 results from a
30a90 63 6f 6d 6d 69 74 0a 2a 2a 20 68 6f 6f 6b 20 72 commit.** hook r
30aa0 65 74 75 72 6e 69 6e 67 20 6e 6f 6e 2d 7a 65 72 eturning non-zer
30ab0 6f 2c 20 6a 75 73 74 20 61 73 20 69 74 20 77 6f o, just as it wo
30ac0 75 6c 64 20 62 65 20 77 69 74 68 20 61 6e 79 20 uld be with any
30ad0 6f 74 68 65 72 20 72 6f 6c 6c 62 61 63 6b 2e 0a other rollback..
30ae0 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70 75 **.** For the pu
30af0 72 70 6f 73 65 73 20 6f 66 20 74 68 69 73 20 41 rposes of this A
30b00 50 49 2c 20 61 20 74 72 61 6e 73 61 63 74 69 6f PI, a transactio
30b10 6e 20 69 73 20 73 61 69 64 20 74 6f 20 68 61 76 n is said to hav
30b20 65 20 62 65 65 6e 0a 2a 2a 20 72 6f 6c 6c 65 64 e been.** rolled
30b30 20 62 61 63 6b 20 69 66 20 61 6e 20 65 78 70 6c back if an expl
30b40 69 63 69 74 20 22 52 4f 4c 4c 42 41 43 4b 22 20 icit "ROLLBACK"
30b50 73 74 61 74 65 6d 65 6e 74 20 69 73 20 65 78 65 statement is exe
30b60 63 75 74 65 64 2c 20 6f 72 0a 2a 2a 20 61 6e 20 cuted, or.** an
30b70 65 72 72 6f 72 20 6f 72 20 63 6f 6e 73 74 72 61 error or constra
30b80 69 6e 74 20 63 61 75 73 65 73 20 61 6e 20 69 6d int causes an im
30b90 70 6c 69 63 69 74 20 72 6f 6c 6c 62 61 63 6b 20 plicit rollback
30ba0 74 6f 20 6f 63 63 75 72 2e 0a 2a 2a 20 54 68 65 to occur..** The
30bb0 20 72 6f 6c 6c 62 61 63 6b 20 63 61 6c 6c 62 61 rollback callba
30bc0 63 6b 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 ck is not invoke
30bd0 64 20 69 66 20 61 20 74 72 61 6e 73 61 63 74 69 d if a transacti
30be0 6f 6e 20 69 73 0a 2a 2a 20 61 75 74 6f 6d 61 74 on is.** automat
30bf0 69 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20 62 61 ically rolled ba
30c00 63 6b 20 62 65 63 61 75 73 65 20 74 68 65 20 64 ck because the d
30c10 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
30c20 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a on is closed..**
30c30 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 63 61 The rollback ca
30c40 6c 6c 62 61 63 6b 20 69 73 20 6e 6f 74 20 69 6e llback is not in
30c50 76 6f 6b 65 64 20 69 66 20 61 20 74 72 61 6e 73 voked if a trans
30c60 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 72 6f 6c action is.** rol
30c70 6c 65 64 20 62 61 63 6b 20 62 65 63 61 75 73 65 led back because
30c80 20 61 20 63 6f 6d 6d 69 74 20 63 61 6c 6c 62 61 a commit callba
30c90 63 6b 20 72 65 74 75 72 6e 65 64 20 6e 6f 6e 2d ck returned non-
30ca0 7a 65 72 6f 2e 0a 2a 2a 20 3c 74 6f 64 6f 3e 20 zero..** <todo>
30cb0 43 68 65 63 6b 20 6f 6e 20 74 68 69 73 20 3c 2f Check on this </
30cc0 74 6f 64 6f 3e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 todo>.**.** See
30cd0 61 6c 73 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 also the [sqlite
30ce0 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 29 5d 3_update_hook()]
30cf0 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a interface..**.*
30d00 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
30d10 2a 2a 20 5b 48 31 32 39 35 31 5d 20 5b 48 31 32 ** [H12951] [H12
30d20 39 35 32 5d 20 5b 48 31 32 39 35 33 5d 20 5b 48 952] [H12953] [H
30d30 31 32 39 35 34 5d 20 5b 48 31 32 39 35 35 5d 0a 12954] [H12955].
30d40 2a 2a 20 5b 48 31 32 39 36 31 5d 20 5b 48 31 32 ** [H12961] [H12
30d50 39 36 32 5d 20 5b 48 31 32 39 36 33 5d 20 5b 48 962] [H12963] [H
30d60 31 32 39 36 34 5d 0a 2a 2f 0a 53 51 4c 49 54 45 12964].*/.SQLITE
30d70 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 _API void *sqlit
30d80 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 73 e3_commit_hook(s
30d90 71 6c 69 74 65 33 2a 2c 20 69 6e 74 28 2a 29 28 qlite3*, int(*)(
30da0 76 6f 69 64 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a void*), void*);.
30db0 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
30dc0 2a 73 71 6c 69 74 65 33 5f 72 6f 6c 6c 62 61 63 *sqlite3_rollbac
30dd0 6b 5f 68 6f 6f 6b 28 73 71 6c 69 74 65 33 2a 2c k_hook(sqlite3*,
30de0 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 20 2a 29 void(*)(void *)
30df0 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a , void*);../*.**
30e00 20 43 41 50 49 33 52 45 46 3a 20 44 61 74 61 20 CAPI3REF: Data
30e10 43 68 61 6e 67 65 20 4e 6f 74 69 66 69 63 61 74 Change Notificat
30e20 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73 20 7b 48 ion Callbacks {H
30e30 31 32 39 37 30 7d 20 3c 53 36 30 34 30 30 3e 0a 12970} <S60400>.
30e40 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
30e50 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 29 20 3_update_hook()
30e60 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 74 interface regist
30e70 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b 20 66 ers a callback f
30e80 75 6e 63 74 69 6f 6e 0a 2a 2a 20 77 69 74 68 20 unction.** with
30e90 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f the [database co
30ea0 6e 6e 65 63 74 69 6f 6e 5d 20 69 64 65 6e 74 69 nnection] identi
30eb0 66 69 65 64 20 62 79 20 74 68 65 20 66 69 72 73 fied by the firs
30ec0 74 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f t argument.** to
30ed0 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e be invoked when
30ee0 65 76 65 72 20 61 20 72 6f 77 20 69 73 20 75 70 ever a row is up
30ef0 64 61 74 65 64 2c 20 69 6e 73 65 72 74 65 64 20 dated, inserted
30f00 6f 72 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 20 41 or deleted..** A
30f10 6e 79 20 63 61 6c 6c 62 61 63 6b 20 73 65 74 20 ny callback set
30f20 62 79 20 61 20 70 72 65 76 69 6f 75 73 20 63 61 by a previous ca
30f30 6c 6c 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 ll to this funct
30f40 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 ion.** for the s
30f50 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e ame database con
30f60 6e 65 63 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 nection is overr
30f70 69 64 64 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 idden..**.** The
30f80 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
30f90 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
30fa0 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 6f the function to
30fb0 20 69 6e 76 6f 6b 65 20 77 68 65 6e 20 61 0a 2a invoke when a.*
30fc0 2a 20 72 6f 77 20 69 73 20 75 70 64 61 74 65 64 * row is updated
30fd0 2c 20 69 6e 73 65 72 74 65 64 20 6f 72 20 64 65 , inserted or de
30fe0 6c 65 74 65 64 2e 0a 2a 2a 20 54 68 65 20 66 69 leted..** The fi
30ff0 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 rst argument to
31000 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 the callback is
31010 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 74 68 a copy of the th
31020 69 72 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 ird argument.**
31030 74 6f 20 73 71 6c 69 74 65 33 5f 75 70 64 61 74 to sqlite3_updat
31040 65 5f 68 6f 6f 6b 28 29 2e 0a 2a 2a 20 54 68 65 e_hook()..** The
31050 20 73 65 63 6f 6e 64 20 63 61 6c 6c 62 61 63 6b second callback
31060 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f 6e 65 argument is one
31070 20 6f 66 20 5b 53 51 4c 49 54 45 5f 49 4e 53 45 of [SQLITE_INSE
31080 52 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 44 45 4c RT], [SQLITE_DEL
31090 45 54 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51 4c ETE],.** or [SQL
310a0 49 54 45 5f 55 50 44 41 54 45 5d 2c 20 64 65 70 ITE_UPDATE], dep
310b0 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 6f 70 ending on the op
310c0 65 72 61 74 69 6f 6e 20 74 68 61 74 20 63 61 75 eration that cau
310d0 73 65 64 20 74 68 65 20 63 61 6c 6c 62 61 63 6b sed the callback
310e0 0a 2a 2a 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 .** to be invoke
310f0 64 2e 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20 d..** The third
31100 61 6e 64 20 66 6f 75 72 74 68 20 61 72 67 75 6d and fourth argum
31110 65 6e 74 73 20 74 6f 20 74 68 65 20 63 61 6c 6c ents to the call
31120 62 61 63 6b 20 63 6f 6e 74 61 69 6e 20 70 6f 69 back contain poi
31130 6e 74 65 72 73 20 74 6f 20 74 68 65 0a 2a 2a 20 nters to the.**
31140 64 61 74 61 62 61 73 65 20 61 6e 64 20 74 61 62 database and tab
31150 6c 65 20 6e 61 6d 65 20 63 6f 6e 74 61 69 6e 69 le name containi
31160 6e 67 20 74 68 65 20 61 66 66 65 63 74 65 64 20 ng the affected
31170 72 6f 77 2e 0a 2a 2a 20 54 68 65 20 66 69 6e 61 row..** The fina
31180 6c 20 63 61 6c 6c 62 61 63 6b 20 70 61 72 61 6d l callback param
31190 65 74 65 72 20 69 73 20 74 68 65 20 5b 72 6f 77 eter is the [row
311a0 69 64 5d 20 6f 66 20 74 68 65 20 72 6f 77 2e 0a id] of the row..
311b0 2a 2a 20 49 6e 20 74 68 65 20 63 61 73 65 20 6f ** In the case o
311c0 66 20 61 6e 20 75 70 64 61 74 65 2c 20 74 68 69 f an update, thi
311d0 73 20 69 73 20 74 68 65 20 5b 72 6f 77 69 64 5d s is the [rowid]
311e0 20 61 66 74 65 72 20 74 68 65 20 75 70 64 61 74 after the updat
311f0 65 20 74 61 6b 65 73 20 70 6c 61 63 65 2e 0a 2a e takes place..*
31200 2a 0a 2a 2a 20 54 68 65 20 75 70 64 61 74 65 20 *.** The update
31210 68 6f 6f 6b 20 69 73 20 6e 6f 74 20 69 6e 76 6f hook is not invo
31220 6b 65 64 20 77 68 65 6e 20 69 6e 74 65 72 6e 61 ked when interna
31230 6c 20 73 79 73 74 65 6d 20 74 61 62 6c 65 73 20 l system tables
31240 61 72 65 0a 2a 2a 20 6d 6f 64 69 66 69 65 64 20 are.** modified
31250 28 69 2e 65 2e 20 73 71 6c 69 74 65 5f 6d 61 73 (i.e. sqlite_mas
31260 74 65 72 20 61 6e 64 20 73 71 6c 69 74 65 5f 73 ter and sqlite_s
31270 65 71 75 65 6e 63 65 29 2e 0a 2a 2a 0a 2a 2a 20 equence)..**.**
31280 49 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 69 In the current i
31290 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 74 mplementation, t
312a0 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 0a 2a he update hook.*
312b0 2a 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 * is not invoked
312c0 20 77 68 65 6e 20 64 75 70 6c 69 63 61 74 69 6f when duplicatio
312d0 6e 20 72 6f 77 73 20 61 72 65 20 64 65 6c 65 74 n rows are delet
312e0 65 64 20 62 65 63 61 75 73 65 20 6f 66 20 61 6e ed because of an
312f0 0a 2a 2a 20 5b 4f 4e 20 43 4f 4e 46 4c 49 43 54 .** [ON CONFLICT
31300 20 7c 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 52 | ON CONFLICT R
31310 45 50 4c 41 43 45 5d 20 63 6c 61 75 73 65 2e 20 EPLACE] clause.
31320 20 4e 6f 72 20 69 73 20 74 68 65 20 75 70 64 61 Nor is the upda
31330 74 65 20 68 6f 6f 6b 0a 2a 2a 20 69 6e 76 6f 6b te hook.** invok
31340 65 64 20 77 68 65 6e 20 72 6f 77 73 20 61 72 65 ed when rows are
31350 20 64 65 6c 65 74 65 64 20 75 73 69 6e 67 20 74 deleted using t
31360 68 65 20 5b 74 72 75 6e 63 61 74 65 20 6f 70 74 he [truncate opt
31370 69 6d 69 7a 61 74 69 6f 6e 5d 2e 0a 2a 2a 20 54 imization]..** T
31380 68 65 20 65 78 63 65 70 74 69 6f 6e 73 20 64 65 he exceptions de
31390 66 69 6e 65 64 20 69 6e 20 74 68 69 73 20 70 61 fined in this pa
313a0 72 61 67 72 61 70 68 20 6d 69 67 68 74 20 63 68 ragraph might ch
313b0 61 6e 67 65 20 69 6e 20 61 20 66 75 74 75 72 65 ange in a future
313c0 0a 2a 2a 20 72 65 6c 65 61 73 65 20 6f 66 20 53 .** release of S
313d0 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 QLite..**.** The
313e0 20 75 70 64 61 74 65 20 68 6f 6f 6b 20 69 6d 70 update hook imp
313f0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 75 73 74 lementation must
31400 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68 69 6e 67 not do anything
31410 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f 64 69 66 that will modif
31420 79 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 y.** the databas
31430 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 e connection tha
31440 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 75 70 t invoked the up
31450 64 61 74 65 20 68 6f 6f 6b 2e 20 20 41 6e 79 20 date hook. Any
31460 61 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20 6d 6f actions.** to mo
31470 64 69 66 79 20 74 68 65 20 64 61 74 61 62 61 73 dify the databas
31480 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 73 e connection mus
31490 74 20 62 65 20 64 65 66 65 72 72 65 64 20 75 6e t be deferred un
314a0 74 69 6c 20 61 66 74 65 72 20 74 68 65 0a 2a 2a til after the.**
314b0 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 66 20 74 completion of t
314c0 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 he [sqlite3_step
314d0 28 29 5d 20 63 61 6c 6c 20 74 68 61 74 20 74 72 ()] call that tr
314e0 69 67 67 65 72 65 64 20 74 68 65 20 75 70 64 61 iggered the upda
314f0 74 65 20 68 6f 6f 6b 2e 0a 2a 2a 20 4e 6f 74 65 te hook..** Note
31500 20 74 68 61 74 20 5b 73 71 6c 69 74 65 33 5f 70 that [sqlite3_p
31510 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 repare_v2()] and
31520 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 [sqlite3_step()
31530 5d 20 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 68 ] both modify th
31540 65 69 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 eir.** database
31550 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20 connections for
31560 74 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22 the meaning of "
31570 6d 6f 64 69 66 79 22 20 69 6e 20 74 68 69 73 20 modify" in this
31580 70 61 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a paragraph..**.**
31590 20 49 66 20 61 6e 6f 74 68 65 72 20 66 75 6e 63 If another func
315a0 74 69 6f 6e 20 77 61 73 20 70 72 65 76 69 6f 75 tion was previou
315b0 73 6c 79 20 72 65 67 69 73 74 65 72 65 64 2c 20 sly registered,
315c0 69 74 73 20 70 41 72 67 20 76 61 6c 75 65 0a 2a its pArg value.*
315d0 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 * is returned.
315e0 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 20 69 Otherwise NULL i
315f0 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a s returned..**.*
31600 2a 20 53 65 65 20 61 6c 73 6f 20 74 68 65 20 5b * See also the [
31610 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 sqlite3_commit_h
31620 6f 6f 6b 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 ook()] and [sqli
31630 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f te3_rollback_hoo
31640 6b 28 29 5d 0a 2a 2a 20 69 6e 74 65 72 66 61 63 k()].** interfac
31650 65 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 es..**.** Requir
31660 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 39 ements:.** [H129
31670 37 31 5d 20 5b 48 31 32 39 37 33 5d 20 5b 48 31 71] [H12973] [H1
31680 32 39 37 35 5d 20 5b 48 31 32 39 37 37 5d 20 5b 2975] [H12977] [
31690 48 31 32 39 37 39 5d 20 5b 48 31 32 39 38 31 5d H12979] [H12981]
316a0 20 5b 48 31 32 39 38 33 5d 20 5b 48 31 32 39 38 [H12983] [H1298
316b0 36 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 6].*/.SQLITE_API
316c0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 75 void *sqlite3_u
316d0 70 64 61 74 65 5f 68 6f 6f 6b 28 0a 20 20 73 71 pdate_hook(. sq
316e0 6c 69 74 65 33 2a 2c 20 0a 20 20 76 6f 69 64 28 lite3*, . void(
316f0 2a 29 28 76 6f 69 64 20 2a 2c 69 6e 74 20 2c 63 *)(void *,int ,c
31700 68 61 72 20 63 6f 6e 73 74 20 2a 2c 63 68 61 72 har const *,char
31710 20 63 6f 6e 73 74 20 2a 2c 73 71 6c 69 74 65 33 const *,sqlite3
31720 5f 69 6e 74 36 34 29 2c 0a 20 20 76 6f 69 64 2a _int64),. void*
31730 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 .);../*.** CAPI3
31740 52 45 46 3a 20 45 6e 61 62 6c 65 20 4f 72 20 44 REF: Enable Or D
31750 69 73 61 62 6c 65 20 53 68 61 72 65 64 20 50 61 isable Shared Pa
31760 67 65 72 20 43 61 63 68 65 20 7b 48 31 30 33 33 ger Cache {H1033
31770 30 7d 20 3c 53 33 30 39 30 30 3e 0a 2a 2a 20 4b 0} <S30900>.** K
31780 45 59 57 4f 52 44 53 3a 20 7b 73 68 61 72 65 64 EYWORDS: {shared
31790 20 63 61 63 68 65 7d 0a 2a 2a 0a 2a 2a 20 54 68 cache}.**.** Th
317a0 69 73 20 72 6f 75 74 69 6e 65 20 65 6e 61 62 6c is routine enabl
317b0 65 73 20 6f 72 20 64 69 73 61 62 6c 65 73 20 74 es or disables t
317c0 68 65 20 73 68 61 72 69 6e 67 20 6f 66 20 74 68 he sharing of th
317d0 65 20 64 61 74 61 62 61 73 65 20 63 61 63 68 65 e database cache
317e0 0a 2a 2a 20 61 6e 64 20 73 63 68 65 6d 61 20 64 .** and schema d
317f0 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 62 ata structures b
31800 65 74 77 65 65 6e 20 5b 64 61 74 61 62 61 73 65 etween [database
31810 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 7c 20 63 6f connection | co
31820 6e 6e 65 63 74 69 6f 6e 73 5d 0a 2a 2a 20 74 6f nnections].** to
31830 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 the same databa
31840 73 65 2e 20 53 68 61 72 69 6e 67 20 69 73 20 65 se. Sharing is e
31850 6e 61 62 6c 65 64 20 69 66 20 74 68 65 20 61 72 nabled if the ar
31860 67 75 6d 65 6e 74 20 69 73 20 74 72 75 65 0a 2a gument is true.*
31870 2a 20 61 6e 64 20 64 69 73 61 62 6c 65 64 20 69 * and disabled i
31880 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69 f the argument i
31890 73 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 43 s false..**.** C
318a0 61 63 68 65 20 73 68 61 72 69 6e 67 20 69 73 20 ache sharing is
318b0 65 6e 61 62 6c 65 64 20 61 6e 64 20 64 69 73 61 enabled and disa
318c0 62 6c 65 64 20 66 6f 72 20 61 6e 20 65 6e 74 69 bled for an enti
318d0 72 65 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 20 54 re process..** T
318e0 68 69 73 20 69 73 20 61 20 63 68 61 6e 67 65 20 his is a change
318f0 61 73 20 6f 66 20 53 51 4c 69 74 65 20 76 65 72 as of SQLite ver
31900 73 69 6f 6e 20 33 2e 35 2e 30 2e 20 49 6e 20 70 sion 3.5.0. In p
31910 72 69 6f 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 rior versions of
31920 20 53 51 4c 69 74 65 2c 0a 2a 2a 20 73 68 61 72 SQLite,.** shar
31930 69 6e 67 20 77 61 73 20 65 6e 61 62 6c 65 64 20 ing was enabled
31940 6f 72 20 64 69 73 61 62 6c 65 64 20 66 6f 72 20 or disabled for
31950 65 61 63 68 20 74 68 72 65 61 64 20 73 65 70 61 each thread sepa
31960 72 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 rately..**.** Th
31970 65 20 63 61 63 68 65 20 73 68 61 72 69 6e 67 20 e cache sharing
31980 6d 6f 64 65 20 73 65 74 20 62 79 20 74 68 69 73 mode set by this
31990 20 69 6e 74 65 72 66 61 63 65 20 65 66 66 65 63 interface effec
319a0 74 73 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e ts all subsequen
319b0 74 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 5b 73 t.** calls to [s
319c0 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 qlite3_open()],
319d0 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 [sqlite3_open_v2
319e0 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 ()], and [sqlite
319f0 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 0a 2a 2a 20 3_open16()]..**
31a00 45 78 69 73 74 69 6e 67 20 64 61 74 61 62 61 73 Existing databas
31a10 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 63 6f e connections co
31a20 6e 74 69 6e 75 65 20 75 73 65 20 74 68 65 20 73 ntinue use the s
31a30 68 61 72 69 6e 67 20 6d 6f 64 65 0a 2a 2a 20 74 haring mode.** t
31a40 68 61 74 20 77 61 73 20 69 6e 20 65 66 66 65 63 hat was in effec
31a50 74 20 61 74 20 74 68 65 20 74 69 6d 65 20 74 68 t at the time th
31a60 65 79 20 77 65 72 65 20 6f 70 65 6e 65 64 2e 0a ey were opened..
31a70 2a 2a 0a 2a 2a 20 56 69 72 74 75 61 6c 20 74 61 **.** Virtual ta
31a80 62 6c 65 73 20 63 61 6e 6e 6f 74 20 62 65 20 75 bles cannot be u
31a90 73 65 64 20 77 69 74 68 20 61 20 73 68 61 72 65 sed with a share
31aa0 64 20 63 61 63 68 65 2e 20 20 57 68 65 6e 20 73 d cache. When s
31ab0 68 61 72 65 64 0a 2a 2a 20 63 61 63 68 65 20 69 hared.** cache i
31ac0 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 20 5b s enabled, the [
31ad0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d sqlite3_create_m
31ae0 6f 64 75 6c 65 28 29 5d 20 41 50 49 20 75 73 65 odule()] API use
31af0 64 20 74 6f 20 72 65 67 69 73 74 65 72 0a 2a 2a d to register.**
31b00 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 virtual tables
31b10 77 69 6c 6c 20 61 6c 77 61 79 73 20 72 65 74 75 will always retu
31b20 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a rn an error..**.
31b30 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
31b40 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f returns [SQLITE_
31b50 4f 4b 5d 20 69 66 20 73 68 61 72 65 64 20 63 61 OK] if shared ca
31b60 63 68 65 20 77 61 73 20 65 6e 61 62 6c 65 64 20 che was enabled
31b70 6f 72 20 64 69 73 61 62 6c 65 64 0a 2a 2a 20 73 or disabled.** s
31b80 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 20 41 6e uccessfully. An
31b90 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 [error code] is
31ba0 20 72 65 74 75 72 6e 65 64 20 6f 74 68 65 72 77 returned otherw
31bb0 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 53 68 61 72 65 ise..**.** Share
31bc0 64 20 63 61 63 68 65 20 69 73 20 64 69 73 61 62 d cache is disab
31bd0 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 2e 20 led by default.
31be0 42 75 74 20 74 68 69 73 20 6d 69 67 68 74 20 63 But this might c
31bf0 68 61 6e 67 65 20 69 6e 0a 2a 2a 20 66 75 74 75 hange in.** futu
31c00 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53 re releases of S
31c10 51 4c 69 74 65 2e 20 20 41 70 70 6c 69 63 61 74 QLite. Applicat
31c20 69 6f 6e 73 20 74 68 61 74 20 63 61 72 65 20 61 ions that care a
31c30 62 6f 75 74 20 73 68 61 72 65 64 0a 2a 2a 20 63 bout shared.** c
31c40 61 63 68 65 20 73 65 74 74 69 6e 67 20 73 68 6f ache setting sho
31c50 75 6c 64 20 73 65 74 20 69 74 20 65 78 70 6c 69 uld set it expli
31c60 63 69 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 citly..**.** See
31c70 20 41 6c 73 6f 3a 20 20 5b 53 51 4c 69 74 65 20 Also: [SQLite
31c80 53 68 61 72 65 64 2d 43 61 63 68 65 20 4d 6f 64 Shared-Cache Mod
31c90 65 5d 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 e].**.** Require
31ca0 6d 65 6e 74 73 3a 20 5b 48 31 30 33 33 31 5d 20 ments: [H10331]
31cb0 5b 48 31 30 33 33 36 5d 20 5b 48 31 30 33 33 37 [H10336] [H10337
31cc0 5d 20 5b 48 31 30 33 33 39 5d 0a 2a 2f 0a 53 51 ] [H10339].*/.SQ
31cd0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
31ce0 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72 ite3_enable_shar
31cf0 65 64 5f 63 61 63 68 65 28 69 6e 74 29 3b 0a 0a ed_cache(int);..
31d00 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
31d10 41 74 74 65 6d 70 74 20 54 6f 20 46 72 65 65 20 Attempt To Free
31d20 48 65 61 70 20 4d 65 6d 6f 72 79 20 7b 48 31 37 Heap Memory {H17
31d30 33 34 30 7d 20 3c 53 33 30 32 32 30 3e 0a 2a 2a 340} <S30220>.**
31d40 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
31d50 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 release_memory()
31d60 20 69 6e 74 65 72 66 61 63 65 20 61 74 74 65 6d interface attem
31d70 70 74 73 20 74 6f 20 66 72 65 65 20 4e 20 62 79 pts to free N by
31d80 74 65 73 0a 2a 2a 20 6f 66 20 68 65 61 70 20 6d tes.** of heap m
31d90 65 6d 6f 72 79 20 62 79 20 64 65 61 6c 6c 6f 63 emory by dealloc
31da0 61 74 69 6e 67 20 6e 6f 6e 2d 65 73 73 65 6e 74 ating non-essent
31db0 69 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 ial memory alloc
31dc0 61 74 69 6f 6e 73 0a 2a 2a 20 68 65 6c 64 20 62 ations.** held b
31dd0 79 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6c y the database l
31de0 69 62 72 61 72 79 2e 20 7b 45 4e 44 7d 20 20 4d ibrary. {END} M
31df0 65 6d 6f 72 79 20 75 73 65 64 20 74 6f 20 63 61 emory used to ca
31e00 63 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 che database.**
31e10 70 61 67 65 73 20 74 6f 20 69 6d 70 72 6f 76 65 pages to improve
31e20 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 69 73 20 performance is
31e30 61 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20 6e 6f an example of no
31e40 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d 6f n-essential memo
31e50 72 79 2e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 ry..** sqlite3_r
31e60 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 elease_memory()
31e70 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 returns the numb
31e80 65 72 20 6f 66 20 62 79 74 65 73 20 61 63 74 75 er of bytes actu
31e90 61 6c 6c 79 20 66 72 65 65 64 2c 0a 2a 2a 20 77 ally freed,.** w
31ea0 68 69 63 68 20 6d 69 67 68 74 20 62 65 20 6d 6f hich might be mo
31eb0 72 65 20 6f 72 20 6c 65 73 73 20 74 68 61 6e 20 re or less than
31ec0 74 68 65 20 61 6d 6f 75 6e 74 20 72 65 71 75 65 the amount reque
31ed0 73 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 sted..**.** Requ
31ee0 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 37 33 34 irements: [H1734
31ef0 31 5d 20 5b 48 31 37 33 34 32 5d 0a 2a 2f 0a 53 1] [H17342].*/.S
31f00 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
31f10 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 lite3_release_me
31f20 6d 6f 72 79 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a mory(int);../*.*
31f30 2a 20 43 41 50 49 33 52 45 46 3a 20 49 6d 70 6f * CAPI3REF: Impo
31f40 73 65 20 41 20 4c 69 6d 69 74 20 4f 6e 20 48 65 se A Limit On He
31f50 61 70 20 53 69 7a 65 20 7b 48 31 37 33 35 30 7d ap Size {H17350}
31f60 20 3c 53 33 30 32 32 30 3e 0a 2a 2a 0a 2a 2a 20 <S30220>.**.**
31f70 54 68 65 20 73 71 6c 69 74 65 33 5f 73 6f 66 74 The sqlite3_soft
31f80 5f 68 65 61 70 5f 6c 69 6d 69 74 28 29 20 69 6e _heap_limit() in
31f90 74 65 72 66 61 63 65 20 70 6c 61 63 65 73 20 61 terface places a
31fa0 20 22 73 6f 66 74 22 20 6c 69 6d 69 74 0a 2a 2a "soft" limit.**
31fb0 20 6f 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f on the amount o
31fc0 66 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 74 68 f heap memory th
31fd0 61 74 20 6d 61 79 20 62 65 20 61 6c 6c 6f 63 61 at may be alloca
31fe0 74 65 64 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a ted by SQLite..*
31ff0 2a 20 49 66 20 61 6e 20 69 6e 74 65 72 6e 61 6c * If an internal
32000 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 72 allocation is r
32010 65 71 75 65 73 74 65 64 20 74 68 61 74 20 77 6f equested that wo
32020 75 6c 64 20 65 78 63 65 65 64 20 74 68 65 0a 2a uld exceed the.*
32030 2a 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 * soft heap limi
32040 74 2c 20 5b 73 71 6c 69 74 65 33 5f 72 65 6c 65 t, [sqlite3_rele
32050 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d 20 69 73 ase_memory()] is
32060 20 69 6e 76 6f 6b 65 64 20 6f 6e 65 20 6f 72 0a invoked one or.
32070 2a 2a 20 6d 6f 72 65 20 74 69 6d 65 73 20 74 6f ** more times to
32080 20 66 72 65 65 20 75 70 20 73 6f 6d 65 20 73 70 free up some sp
32090 61 63 65 20 62 65 66 6f 72 65 20 74 68 65 20 61 ace before the a
320a0 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 70 65 72 llocation is per
320b0 66 6f 72 6d 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 formed..**.** Th
320c0 65 20 6c 69 6d 69 74 20 69 73 20 63 61 6c 6c 65 e limit is calle
320d0 64 20 22 73 6f 66 74 22 2c 20 62 65 63 61 75 73 d "soft", becaus
320e0 65 20 69 66 20 5b 73 71 6c 69 74 65 33 5f 72 65 e if [sqlite3_re
320f0 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d 0a lease_memory()].
32100 2a 2a 20 63 61 6e 6e 6f 74 20 66 72 65 65 20 73 ** cannot free s
32110 75 66 66 69 63 69 65 6e 74 20 6d 65 6d 6f 72 79 ufficient memory
32120 20 74 6f 20 70 72 65 76 65 6e 74 20 74 68 65 20 to prevent the
32130 6c 69 6d 69 74 20 66 72 6f 6d 20 62 65 69 6e 67 limit from being
32140 20 65 78 63 65 65 64 65 64 2c 0a 2a 2a 20 74 68 exceeded,.** th
32150 65 20 6d 65 6d 6f 72 79 20 69 73 20 61 6c 6c 6f e memory is allo
32160 63 61 74 65 64 20 61 6e 79 77 61 79 20 61 6e 64 cated anyway and
32170 20 74 68 65 20 63 75 72 72 65 6e 74 20 6f 70 65 the current ope
32180 72 61 74 69 6f 6e 20 70 72 6f 63 65 65 64 73 2e ration proceeds.
32190 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 67 61 74 69 76 .**.** A negativ
321a0 65 20 6f 72 20 7a 65 72 6f 20 76 61 6c 75 65 20 e or zero value
321b0 66 6f 72 20 4e 20 6d 65 61 6e 73 20 74 68 61 74 for N means that
321c0 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73 6f 66 there is no sof
321d0 74 20 68 65 61 70 20 6c 69 6d 69 74 20 61 6e 64 t heap limit and
321e0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72 65 6c .** [sqlite3_rel
321f0 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d 20 77 ease_memory()] w
32200 69 6c 6c 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c ill only be call
32210 65 64 20 77 68 65 6e 20 6d 65 6d 6f 72 79 20 69 ed when memory i
32220 73 20 65 78 68 61 75 73 74 65 64 2e 0a 2a 2a 20 s exhausted..**
32230 54 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 The default valu
32240 65 20 66 6f 72 20 74 68 65 20 73 6f 66 74 20 68 e for the soft h
32250 65 61 70 20 6c 69 6d 69 74 20 69 73 20 7a 65 72 eap limit is zer
32260 6f 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 o..**.** SQLite
32270 6d 61 6b 65 73 20 61 20 62 65 73 74 20 65 66 66 makes a best eff
32280 6f 72 74 20 74 6f 20 68 6f 6e 6f 72 20 74 68 65 ort to honor the
32290 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 soft heap limit
322a0 2e 0a 2a 2a 20 42 75 74 20 69 66 20 74 68 65 20 ..** But if the
322b0 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 20 soft heap limit
322c0 63 61 6e 6e 6f 74 20 62 65 20 68 6f 6e 6f 72 65 cannot be honore
322d0 64 2c 20 65 78 65 63 75 74 69 6f 6e 20 77 69 6c d, execution wil
322e0 6c 0a 2a 2a 20 63 6f 6e 74 69 6e 75 65 20 77 69 l.** continue wi
322f0 74 68 6f 75 74 20 65 72 72 6f 72 20 6f 72 20 6e thout error or n
32300 6f 74 69 66 69 63 61 74 69 6f 6e 2e 20 20 54 68 otification. Th
32310 69 73 20 69 73 20 77 68 79 20 74 68 65 20 6c 69 is is why the li
32320 6d 69 74 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 mit is.** called
32330 20 61 20 22 73 6f 66 74 22 20 6c 69 6d 69 74 2e a "soft" limit.
32340 20 20 49 74 20 69 73 20 61 64 76 69 73 6f 72 79 It is advisory
32350 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50 72 69 only..**.** Pri
32360 6f 72 20 74 6f 20 53 51 4c 69 74 65 20 76 65 72 or to SQLite ver
32370 73 69 6f 6e 20 33 2e 35 2e 30 2c 20 74 68 69 73 sion 3.5.0, this
32380 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 63 6f routine only co
32390 6e 73 74 72 61 69 6e 65 64 20 74 68 65 20 6d 65 nstrained the me
323a0 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 mory.** allocate
323b0 64 20 62 79 20 61 20 73 69 6e 67 6c 65 20 74 68 d by a single th
323c0 72 65 61 64 20 2d 20 74 68 65 20 73 61 6d 65 20 read - the same
323d0 74 68 72 65 61 64 20 69 6e 20 77 68 69 63 68 20 thread in which
323e0 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 this routine.**
323f0 72 75 6e 73 2e 20 20 42 65 67 69 6e 6e 69 6e 67 runs. Beginning
32400 20 77 69 74 68 20 53 51 4c 69 74 65 20 76 65 72 with SQLite ver
32410 73 69 6f 6e 20 33 2e 35 2e 30 2c 20 74 68 65 20 sion 3.5.0, the
32420 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 20 soft heap limit
32430 69 73 0a 2a 2a 20 61 70 70 6c 69 65 64 20 74 6f is.** applied to
32440 20 61 6c 6c 20 74 68 72 65 61 64 73 2e 20 54 68 all threads. Th
32450 65 20 76 61 6c 75 65 20 73 70 65 63 69 66 69 65 e value specifie
32460 64 20 66 6f 72 20 74 68 65 20 73 6f 66 74 20 68 d for the soft h
32470 65 61 70 20 6c 69 6d 69 74 0a 2a 2a 20 69 73 20 eap limit.** is
32480 61 6e 20 75 70 70 65 72 20 62 6f 75 6e 64 20 6f an upper bound o
32490 6e 20 74 68 65 20 74 6f 74 61 6c 20 6d 65 6d 6f n the total memo
324a0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 6f ry allocation fo
324b0 72 20 61 6c 6c 20 74 68 72 65 61 64 73 2e 20 49 r all threads. I
324c0 6e 0a 2a 2a 20 76 65 72 73 69 6f 6e 20 33 2e 35 n.** version 3.5
324d0 2e 30 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d .0 there is no m
324e0 65 63 68 61 6e 69 73 6d 20 66 6f 72 20 6c 69 6d echanism for lim
324f0 69 74 69 6e 67 20 74 68 65 20 68 65 61 70 20 75 iting the heap u
32500 73 61 67 65 20 66 6f 72 0a 2a 2a 20 69 6e 64 69 sage for.** indi
32510 76 69 64 75 61 6c 20 74 68 72 65 61 64 73 2e 0a vidual threads..
32520 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
32530 74 73 3a 0a 2a 2a 20 5b 48 31 36 33 35 31 5d 20 ts:.** [H16351]
32540 5b 48 31 36 33 35 32 5d 20 5b 48 31 36 33 35 33 [H16352] [H16353
32550 5d 20 5b 48 31 36 33 35 34 5d 20 5b 48 31 36 33 ] [H16354] [H163
32560 35 35 5d 20 5b 48 31 36 33 35 38 5d 0a 2a 2f 0a 55] [H16358].*/.
32570 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
32580 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 sqlite3_soft_hea
32590 70 5f 6c 69 6d 69 74 28 69 6e 74 29 3b 0a 0a 2f p_limit(int);../
325a0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 *.** CAPI3REF: E
325b0 78 74 72 61 63 74 20 4d 65 74 61 64 61 74 61 20 xtract Metadata
325c0 41 62 6f 75 74 20 41 20 43 6f 6c 75 6d 6e 20 4f About A Column O
325d0 66 20 41 20 54 61 62 6c 65 20 7b 48 31 32 38 35 f A Table {H1285
325e0 30 7d 20 3c 53 36 30 33 30 30 3e 0a 2a 2a 0a 2a 0} <S60300>.**.*
325f0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 * This routine r
32600 65 74 75 72 6e 73 20 6d 65 74 61 64 61 74 61 20 eturns metadata
32610 61 62 6f 75 74 20 61 20 73 70 65 63 69 66 69 63 about a specific
32620 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 73 70 65 column of a spe
32630 63 69 66 69 63 0a 2a 2a 20 64 61 74 61 62 61 73 cific.** databas
32640 65 20 74 61 62 6c 65 20 61 63 63 65 73 73 69 62 e table accessib
32650 6c 65 20 75 73 69 6e 67 20 74 68 65 20 5b 64 61 le using the [da
32660 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
32670 6e 5d 20 68 61 6e 64 6c 65 0a 2a 2a 20 70 61 73 n] handle.** pas
32680 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74 sed as the first
32690 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 function argume
326a0 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f nt..**.** The co
326b0 6c 75 6d 6e 20 69 73 20 69 64 65 6e 74 69 66 69 lumn is identifi
326c0 65 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64 ed by the second
326d0 2c 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72 , third and four
326e0 74 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f th parameters to
326f0 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f .** this functio
32700 6e 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61 n. The second pa
32710 72 61 6d 65 74 65 72 20 69 73 20 65 69 74 68 65 rameter is eithe
32720 72 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 r the name of th
32730 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 28 69 e database.** (i
32740 2e 65 2e 20 22 6d 61 69 6e 22 2c 20 22 74 65 6d .e. "main", "tem
32750 70 22 20 6f 72 20 61 6e 20 61 74 74 61 63 68 65 p" or an attache
32760 64 20 64 61 74 61 62 61 73 65 29 20 63 6f 6e 74 d database) cont
32770 61 69 6e 69 6e 67 20 74 68 65 20 73 70 65 63 69 aining the speci
32780 66 69 65 64 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 fied.** table or
32790 20 4e 55 4c 4c 2e 20 49 66 20 69 74 20 69 73 20 NULL. If it is
327a0 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 6c 6c 20 61 NULL, then all a
327b0 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 ttached database
327c0 73 20 61 72 65 20 73 65 61 72 63 68 65 64 0a 2a s are searched.*
327d0 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 * for the table
327e0 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 61 using the same a
327f0 6c 67 6f 72 69 74 68 6d 20 75 73 65 64 20 62 79 lgorithm used by
32800 20 74 68 65 20 64 61 74 61 62 61 73 65 20 65 6e the database en
32810 67 69 6e 65 20 74 6f 0a 2a 2a 20 72 65 73 6f 6c gine to.** resol
32820 76 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20 74 ve unqualified t
32830 61 62 6c 65 20 72 65 66 65 72 65 6e 63 65 73 2e able references.
32840 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 .**.** The third
32850 20 61 6e 64 20 66 6f 75 72 74 68 20 70 61 72 61 and fourth para
32860 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73 20 66 meters to this f
32870 75 6e 63 74 69 6f 6e 20 61 72 65 20 74 68 65 20 unction are the
32880 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e table and column
32890 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 .** name of the
328a0 64 65 73 69 72 65 64 20 63 6f 6c 75 6d 6e 2c 20 desired column,
328b0 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 4e 65 respectively. Ne
328c0 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 70 ither of these p
328d0 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 6d 61 79 arameters.** may
328e0 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 be NULL..**.**
328f0 4d 65 74 61 64 61 74 61 20 69 73 20 72 65 74 75 Metadata is retu
32900 72 6e 65 64 20 62 79 20 77 72 69 74 69 6e 67 20 rned by writing
32910 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 6c 6f to the memory lo
32920 63 61 74 69 6f 6e 73 20 70 61 73 73 65 64 20 61 cations passed a
32930 73 20 74 68 65 20 35 74 68 0a 2a 2a 20 61 6e 64 s the 5th.** and
32940 20 73 75 62 73 65 71 75 65 6e 74 20 70 61 72 61 subsequent para
32950 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73 20 66 meters to this f
32960 75 6e 63 74 69 6f 6e 2e 20 41 6e 79 20 6f 66 20 unction. Any of
32970 74 68 65 73 65 20 61 72 67 75 6d 65 6e 74 73 20 these arguments
32980 6d 61 79 20 62 65 0a 2a 2a 20 4e 55 4c 4c 2c 20 may be.** NULL,
32990 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68 in which case th
329a0 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 e corresponding
329b0 65 6c 65 6d 65 6e 74 20 6f 66 20 6d 65 74 61 64 element of metad
329c0 61 74 61 20 69 73 20 6f 6d 69 74 74 65 64 2e 0a ata is omitted..
329d0 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 **.** <blockquot
329e0 65 3e 0a 2a 2a 20 3c 74 61 62 6c 65 20 62 6f 72 e>.** <table bor
329f0 64 65 72 3d 22 31 22 3e 0a 2a 2a 20 3c 74 72 3e der="1">.** <tr>
32a00 3c 74 68 3e 20 50 61 72 61 6d 65 74 65 72 20 3c <th> Parameter <
32a10 74 68 3e 20 4f 75 74 70 75 74 3c 62 72 3e 54 79 th> Output<br>Ty
32a20 70 65 20 3c 74 68 3e 20 20 44 65 73 63 72 69 70 pe <th> Descrip
32a30 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74 72 3e 3c tion.**.** <tr><
32a40 74 64 3e 20 35 74 68 20 3c 74 64 3e 20 63 6f 6e td> 5th <td> con
32a50 73 74 20 63 68 61 72 2a 20 3c 74 64 3e 20 44 61 st char* <td> Da
32a60 74 61 20 74 79 70 65 0a 2a 2a 20 3c 74 72 3e 3c ta type.** <tr><
32a70 74 64 3e 20 36 74 68 20 3c 74 64 3e 20 63 6f 6e td> 6th <td> con
32a80 73 74 20 63 68 61 72 2a 20 3c 74 64 3e 20 4e 61 st char* <td> Na
32a90 6d 65 20 6f 66 20 64 65 66 61 75 6c 74 20 63 6f me of default co
32aa0 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 llation sequence
32ab0 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 37 74 68 .** <tr><td> 7th
32ac0 20 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20 <td> int
32ad0 20 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63 <td> True if c
32ae0 6f 6c 75 6d 6e 20 68 61 73 20 61 20 4e 4f 54 20 olumn has a NOT
32af0 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 0a NULL constraint.
32b00 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 38 74 68 20 ** <tr><td> 8th
32b10 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20 20 <td> int
32b20 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63 6f <td> True if co
32b30 6c 75 6d 6e 20 69 73 20 70 61 72 74 20 6f 66 20 lumn is part of
32b40 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 0a the PRIMARY KEY.
32b50 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 39 74 68 20 ** <tr><td> 9th
32b60 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20 20 <td> int
32b70 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63 6f <td> True if co
32b80 6c 75 6d 6e 20 69 73 20 5b 41 55 54 4f 49 4e 43 lumn is [AUTOINC
32b90 52 45 4d 45 4e 54 5d 0a 2a 2a 20 3c 2f 74 61 62 REMENT].** </tab
32ba0 6c 65 3e 0a 2a 2a 20 3c 2f 62 6c 6f 63 6b 71 75 le>.** </blockqu
32bb0 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d ote>.**.** The m
32bc0 65 6d 6f 72 79 20 70 6f 69 6e 74 65 64 20 74 6f emory pointed to
32bd0 20 62 79 20 74 68 65 20 63 68 61 72 61 63 74 65 by the characte
32be0 72 20 70 6f 69 6e 74 65 72 73 20 72 65 74 75 72 r pointers retur
32bf0 6e 65 64 20 66 6f 72 20 74 68 65 0a 2a 2a 20 64 ned for the.** d
32c00 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20 eclaration type
32c10 61 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 and collation se
32c20 71 75 65 6e 63 65 20 69 73 20 76 61 6c 69 64 20 quence is valid
32c30 6f 6e 6c 79 20 75 6e 74 69 6c 20 74 68 65 20 6e only until the n
32c40 65 78 74 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 61 ext.** call to a
32c50 6e 79 20 53 51 4c 69 74 65 20 41 50 49 20 66 75 ny SQLite API fu
32c60 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 nction..**.** If
32c70 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 74 the specified t
32c80 61 62 6c 65 20 69 73 20 61 63 74 75 61 6c 6c 79 able is actually
32c90 20 61 20 76 69 65 77 2c 20 61 6e 20 5b 65 72 72 a view, an [err
32ca0 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 or code] is retu
32cb0 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 rned..**.** If t
32cc0 68 65 20 73 70 65 63 69 66 69 65 64 20 63 6f 6c he specified col
32cd0 75 6d 6e 20 69 73 20 22 72 6f 77 69 64 22 2c 20 umn is "rowid",
32ce0 22 6f 69 64 22 20 6f 72 20 22 5f 72 6f 77 69 64 "oid" or "_rowid
32cf0 5f 22 20 61 6e 64 20 61 6e 0a 2a 2a 20 5b 49 4e _" and an.** [IN
32d00 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE
32d10 59 5d 20 63 6f 6c 75 6d 6e 20 68 61 73 20 62 65 Y] column has be
32d20 65 6e 20 65 78 70 6c 69 63 69 74 6c 79 20 64 65 en explicitly de
32d30 63 6c 61 72 65 64 2c 20 74 68 65 6e 20 74 68 65 clared, then the
32d40 20 6f 75 74 70 75 74 0a 2a 2a 20 70 61 72 61 6d output.** param
32d50 65 74 65 72 73 20 61 72 65 20 73 65 74 20 66 6f eters are set fo
32d60 72 20 74 68 65 20 65 78 70 6c 69 63 69 74 6c 79 r the explicitly
32d70 20 64 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d 6e declared column
32d80 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f . If there is no
32d90 0a 2a 2a 20 65 78 70 6c 69 63 69 74 6c 79 20 64 .** explicitly d
32da0 65 63 6c 61 72 65 64 20 5b 49 4e 54 45 47 45 52 eclared [INTEGER
32db0 20 50 52 49 4d 41 52 59 20 4b 45 59 5d 20 63 6f PRIMARY KEY] co
32dc0 6c 75 6d 6e 2c 20 74 68 65 6e 20 74 68 65 20 6f lumn, then the o
32dd0 75 74 70 75 74 0a 2a 2a 20 70 61 72 61 6d 65 74 utput.** paramet
32de0 65 72 73 20 61 72 65 20 73 65 74 20 61 73 20 66 ers are set as f
32df0 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 70 ollows:.**.** <p
32e00 72 65 3e 0a 2a 2a 20 20 20 20 20 64 61 74 61 20 re>.** data
32e10 74 79 70 65 3a 20 22 49 4e 54 45 47 45 52 22 0a type: "INTEGER".
32e20 2a 2a 20 20 20 20 20 63 6f 6c 6c 61 74 69 6f 6e ** collation
32e30 20 73 65 71 75 65 6e 63 65 3a 20 22 42 49 4e 41 sequence: "BINA
32e40 52 59 22 0a 2a 2a 20 20 20 20 20 6e 6f 74 20 6e RY".** not n
32e50 75 6c 6c 3a 20 30 0a 2a 2a 20 20 20 20 20 70 72 ull: 0.** pr
32e60 69 6d 61 72 79 20 6b 65 79 3a 20 31 0a 2a 2a 20 imary key: 1.**
32e70 20 20 20 20 61 75 74 6f 20 69 6e 63 72 65 6d 65 auto increme
32e80 6e 74 3a 20 30 0a 2a 2a 20 3c 2f 70 72 65 3e 0a nt: 0.** </pre>.
32e90 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 **.** This funct
32ea0 69 6f 6e 20 6d 61 79 20 6c 6f 61 64 20 6f 6e 65 ion may load one
32eb0 20 6f 72 20 6d 6f 72 65 20 73 63 68 65 6d 61 73 or more schemas
32ec0 20 66 72 6f 6d 20 64 61 74 61 62 61 73 65 20 66 from database f
32ed0 69 6c 65 73 2e 20 49 66 20 61 6e 0a 2a 2a 20 65 iles. If an.** e
32ee0 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 rror occurs duri
32ef0 6e 67 20 74 68 69 73 20 70 72 6f 63 65 73 73 2c ng this process,
32f00 20 6f 72 20 69 66 20 74 68 65 20 72 65 71 75 65 or if the reque
32f10 73 74 65 64 20 74 61 62 6c 65 20 6f 72 20 63 6f sted table or co
32f20 6c 75 6d 6e 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 lumn.** cannot b
32f30 65 20 66 6f 75 6e 64 2c 20 61 6e 20 5b 65 72 72 e found, an [err
32f40 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 or code] is retu
32f50 72 6e 65 64 20 61 6e 64 20 61 6e 20 65 72 72 6f rned and an erro
32f60 72 20 6d 65 73 73 61 67 65 20 6c 65 66 74 0a 2a r message left.*
32f70 2a 20 69 6e 20 74 68 65 20 5b 64 61 74 61 62 61 * in the [databa
32f80 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 se connection] (
32f90 74 6f 20 62 65 20 72 65 74 72 69 65 76 65 64 20 to be retrieved
32fa0 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 65 72 using sqlite3_er
32fb0 72 6d 73 67 28 29 29 2e 0a 2a 2a 0a 2a 2a 20 54 rmsg())..**.** T
32fc0 68 69 73 20 41 50 49 20 69 73 20 6f 6e 6c 79 20 his API is only
32fd0 61 76 61 69 6c 61 62 6c 65 20 69 66 20 74 68 65 available if the
32fe0 20 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f 6d library was com
32ff0 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a piled with the.*
33000 2a 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 * [SQLITE_ENABLE
33010 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 _COLUMN_METADATA
33020 5d 20 43 2d 70 72 65 70 72 6f 63 65 73 73 6f 72 ] C-preprocessor
33030 20 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64 2e symbol defined.
33040 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
33050 6e 74 20 73 71 6c 69 74 65 33 5f 74 61 62 6c 65 nt sqlite3_table
33060 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61 _column_metadata
33070 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c (. sqlite3 *db,
33080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
33090 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 /* Connection ha
330a0 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 ndle */. const
330b0 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c 20 20 char *zDbName,
330c0 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 /* Databas
330d0 65 20 6e 61 6d 65 20 6f 72 20 4e 55 4c 4c 20 2a e name or NULL *
330e0 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
330f0 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 20 20 20 20 zTableName,
33100 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65 20 2a 2f /* Table name */
33110 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
33120 43 6f 6c 75 6d 6e 4e 61 6d 65 2c 20 20 20 20 2f ColumnName, /
33130 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a 2f * Column name */
33140 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 2a . char const **
33150 70 7a 44 61 74 61 54 79 70 65 2c 20 20 20 20 2f pzDataType, /
33160 2a 20 4f 55 54 50 55 54 3a 20 44 65 63 6c 61 72 * OUTPUT: Declar
33170 65 64 20 64 61 74 61 20 74 79 70 65 20 2a 2f 0a ed data type */.
33180 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 2a 70 char const **p
33190 7a 43 6f 6c 6c 53 65 71 2c 20 20 20 20 20 2f 2a zCollSeq, /*
331a0 20 4f 55 54 50 55 54 3a 20 43 6f 6c 6c 61 74 69 OUTPUT: Collati
331b0 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 on sequence name
331c0 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4e 6f 74 4e */. int *pNotN
331d0 75 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 ull,
331e0 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75 /* OUTPUT: Tru
331f0 65 20 69 66 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f e if NOT NULL co
33200 6e 73 74 72 61 69 6e 74 20 65 78 69 73 74 73 20 nstraint exists
33210 2a 2f 0a 20 20 69 6e 74 20 2a 70 50 72 69 6d 61 */. int *pPrima
33220 72 79 4b 65 79 2c 20 20 20 20 20 20 20 20 20 20 ryKey,
33230 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75 65 /* OUTPUT: True
33240 20 69 66 20 63 6f 6c 75 6d 6e 20 70 61 72 74 20 if column part
33250 6f 66 20 50 4b 20 2a 2f 0a 20 20 69 6e 74 20 2a of PK */. int *
33260 70 41 75 74 6f 69 6e 63 20 20 20 20 20 20 20 20 pAutoinc
33270 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 /* OUTPUT
33280 3a 20 54 72 75 65 20 69 66 20 63 6f 6c 75 6d 6e : True if column
33290 20 69 73 20 61 75 74 6f 2d 69 6e 63 72 65 6d 65 is auto-increme
332a0 6e 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 nt */.);../*.**
332b0 43 41 50 49 33 52 45 46 3a 20 4c 6f 61 64 20 41 CAPI3REF: Load A
332c0 6e 20 45 78 74 65 6e 73 69 6f 6e 20 7b 48 31 32 n Extension {H12
332d0 36 30 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 600} <S20500>.**
332e0 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 .** This interfa
332f0 63 65 20 6c 6f 61 64 73 20 61 6e 20 53 51 4c 69 ce loads an SQLi
33300 74 65 20 65 78 74 65 6e 73 69 6f 6e 20 6c 69 62 te extension lib
33310 72 61 72 79 20 66 72 6f 6d 20 74 68 65 20 6e 61 rary from the na
33320 6d 65 64 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 med file..**.**
33330 7b 48 31 32 36 30 31 7d 20 54 68 65 20 73 71 6c {H12601} The sql
33340 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 ite3_load_extens
33350 69 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 65 20 ion() interface
33360 61 74 74 65 6d 70 74 73 20 74 6f 20 6c 6f 61 64 attempts to load
33370 20 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 an.**
33380 53 51 4c 69 74 65 20 65 78 74 65 6e 73 69 6f 6e SQLite extension
33390 20 6c 69 62 72 61 72 79 20 63 6f 6e 74 61 69 6e library contain
333a0 65 64 20 69 6e 20 74 68 65 20 66 69 6c 65 20 7a ed in the file z
333b0 46 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 File..**.** {H12
333c0 36 30 32 7d 20 54 68 65 20 65 6e 74 72 79 20 70 602} The entry p
333d0 6f 69 6e 74 20 69 73 20 7a 50 72 6f 63 2e 0a 2a oint is zProc..*
333e0 2a 0a 2a 2a 20 7b 48 31 32 36 30 33 7d 20 7a 50 *.** {H12603} zP
333f0 72 6f 63 20 6d 61 79 20 62 65 20 30 2c 20 69 6e roc may be 0, in
33400 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65 20 which case the
33410 6e 61 6d 65 20 6f 66 20 74 68 65 20 65 6e 74 72 name of the entr
33420 79 20 70 6f 69 6e 74 0a 2a 2a 20 20 20 20 20 20 y point.**
33430 20 20 20 20 64 65 66 61 75 6c 74 73 20 74 6f 20 defaults to
33440 22 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69 "sqlite3_extensi
33450 6f 6e 5f 69 6e 69 74 22 2e 0a 2a 2a 0a 2a 2a 20 on_init"..**.**
33460 7b 48 31 32 36 30 34 7d 20 54 68 65 20 73 71 6c {H12604} The sql
33470 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 ite3_load_extens
33480 69 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 65 20 ion() interface
33490 73 68 61 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 shall return.**
334a0 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 45 [SQLITE
334b0 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73 73 20 _OK] on success
334c0 61 6e 64 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f and [SQLITE_ERRO
334d0 52 5d 20 69 66 20 73 6f 6d 65 74 68 69 6e 67 20 R] if something
334e0 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a goes wrong..**.*
334f0 2a 20 7b 48 31 32 36 30 35 7d 20 49 66 20 61 6e * {H12605} If an
33500 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61 6e error occurs an
33510 64 20 70 7a 45 72 72 4d 73 67 20 69 73 20 6e 6f d pzErrMsg is no
33520 74 20 30 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a t 0, then the.**
33530 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 [sqlit
33540 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f e3_load_extensio
33550 6e 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 73 n()] interface s
33560 68 61 6c 6c 20 61 74 74 65 6d 70 74 20 74 6f 0a hall attempt to.
33570 2a 2a 20 20 20 20 20 20 20 20 20 20 66 69 6c 6c ** fill
33580 20 2a 70 7a 45 72 72 4d 73 67 20 77 69 74 68 20 *pzErrMsg with
33590 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 65 error message te
335a0 78 74 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d xt stored in mem
335b0 6f 72 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ory.**
335c0 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 obtained from [s
335d0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d qlite3_malloc()]
335e0 2e 20 7b 45 4e 44 7d 20 20 54 68 65 20 63 61 6c . {END} The cal
335f0 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a ling function.**
33600 20 20 20 20 20 20 20 20 20 20 73 68 6f 75 6c 64 should
33610 20 66 72 65 65 20 74 68 69 73 20 6d 65 6d 6f 72 free this memor
33620 79 20 62 79 20 63 61 6c 6c 69 6e 67 20 5b 73 71 y by calling [sq
33630 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e 0a 2a lite3_free()]..*
33640 2a 0a 2a 2a 20 7b 48 31 32 36 30 36 7d 20 45 78 *.** {H12606} Ex
33650 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 tension loading
33660 6d 75 73 74 20 62 65 20 65 6e 61 62 6c 65 64 20 must be enabled
33670 75 73 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 using.**
33680 20 20 5b 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c [sqlite3_enabl
33690 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e e_load_extension
336a0 28 29 5d 20 70 72 69 6f 72 20 74 6f 20 63 61 6c ()] prior to cal
336b0 6c 69 6e 67 20 74 68 69 73 20 41 50 49 2c 0a 2a ling this API,.*
336c0 2a 20 20 20 20 20 20 20 20 20 20 6f 74 68 65 72 * other
336d0 77 69 73 65 20 61 6e 20 65 72 72 6f 72 20 77 69 wise an error wi
336e0 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 2e 0a ll be returned..
336f0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
33700 74 20 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 t sqlite3_load_e
33710 78 74 65 6e 73 69 6f 6e 28 0a 20 20 73 71 6c 69 xtension(. sqli
33720 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 te3 *db,
33730 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 65 78 /* Load the ex
33740 74 65 6e 73 69 6f 6e 20 69 6e 74 6f 20 74 68 69 tension into thi
33750 73 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 s database conne
33760 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 ction */. const
33770 20 63 68 61 72 20 2a 7a 46 69 6c 65 2c 20 20 20 char *zFile,
33780 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 /* Name of the
33790 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 20 63 shared library c
337a0 6f 6e 74 61 69 6e 69 6e 67 20 65 78 74 65 6e 73 ontaining extens
337b0 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ion */. const c
337c0 68 61 72 20 2a 7a 50 72 6f 63 2c 20 20 20 20 2f har *zProc, /
337d0 2a 20 45 6e 74 72 79 20 70 6f 69 6e 74 2e 20 20 * Entry point.
337e0 44 65 72 69 76 65 64 20 66 72 6f 6d 20 7a 46 69 Derived from zFi
337f0 6c 65 20 69 66 20 30 20 2a 2f 0a 20 20 63 68 61 le if 0 */. cha
33800 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20 20 20 r **pzErrMsg
33810 20 20 20 2f 2a 20 50 75 74 20 65 72 72 6f 72 20 /* Put error
33820 6d 65 73 73 61 67 65 20 68 65 72 65 20 69 66 20 message here if
33830 6e 6f 74 20 30 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a not 0 */.);../*.
33840 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 6e 61 ** CAPI3REF: Ena
33850 62 6c 65 20 4f 72 20 44 69 73 61 62 6c 65 20 45 ble Or Disable E
33860 78 74 65 6e 73 69 6f 6e 20 4c 6f 61 64 69 6e 67 xtension Loading
33870 20 7b 48 31 32 36 32 30 7d 20 3c 53 32 30 35 30 {H12620} <S2050
33880 30 3e 0a 2a 2a 0a 2a 2a 20 53 6f 20 61 73 20 6e 0>.**.** So as n
33890 6f 74 20 74 6f 20 6f 70 65 6e 20 73 65 63 75 72 ot to open secur
338a0 69 74 79 20 68 6f 6c 65 73 20 69 6e 20 6f 6c 64 ity holes in old
338b0 65 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 er applications
338c0 74 68 61 74 20 61 72 65 0a 2a 2a 20 75 6e 70 72 that are.** unpr
338d0 65 70 61 72 65 64 20 74 6f 20 64 65 61 6c 20 77 epared to deal w
338e0 69 74 68 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f ith extension lo
338f0 61 64 69 6e 67 2c 20 61 6e 64 20 61 73 20 61 20 ading, and as a
33900 6d 65 61 6e 73 20 6f 66 20 64 69 73 61 62 6c 69 means of disabli
33910 6e 67 0a 2a 2a 20 65 78 74 65 6e 73 69 6f 6e 20 ng.** extension
33920 6c 6f 61 64 69 6e 67 20 77 68 69 6c 65 20 65 76 loading while ev
33930 61 6c 75 61 74 69 6e 67 20 75 73 65 72 2d 65 6e aluating user-en
33940 74 65 72 65 64 20 53 51 4c 2c 20 74 68 65 20 66 tered SQL, the f
33950 6f 6c 6c 6f 77 69 6e 67 20 41 50 49 0a 2a 2a 20 ollowing API.**
33960 69 73 20 70 72 6f 76 69 64 65 64 20 74 6f 20 74 is provided to t
33970 75 72 6e 20 74 68 65 20 5b 73 71 6c 69 74 65 33 urn the [sqlite3
33980 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 _load_extension(
33990 29 5d 20 6d 65 63 68 61 6e 69 73 6d 20 6f 6e 20 )] mechanism on
339a0 61 6e 64 20 6f 66 66 2e 0a 2a 2a 0a 2a 2a 20 45 and off..**.** E
339b0 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 xtension loading
339c0 20 69 73 20 6f 66 66 20 62 79 20 64 65 66 61 75 is off by defau
339d0 6c 74 2e 20 53 65 65 20 74 69 63 6b 65 74 20 23 lt. See ticket #
339e0 31 38 36 33 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 1863..**.** {H12
339f0 36 32 31 7d 20 43 61 6c 6c 20 74 68 65 20 73 71 621} Call the sq
33a00 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 lite3_enable_loa
33a10 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 72 6f d_extension() ro
33a20 75 74 69 6e 65 20 77 69 74 68 20 6f 6e 6f 66 66 utine with onoff
33a30 3d 3d 31 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ==1.**
33a40 74 6f 20 74 75 72 6e 20 65 78 74 65 6e 73 69 6f to turn extensio
33a50 6e 20 6c 6f 61 64 69 6e 67 20 6f 6e 20 61 6e 64 n loading on and
33a60 20 63 61 6c 6c 20 69 74 20 77 69 74 68 20 6f 6e call it with on
33a70 6f 66 66 3d 3d 30 20 74 6f 20 74 75 72 6e 0a 2a off==0 to turn.*
33a80 2a 20 20 20 20 20 20 20 20 20 20 69 74 20 62 61 * it ba
33a90 63 6b 20 6f 66 66 20 61 67 61 69 6e 2e 0a 2a 2a ck off again..**
33aa0 0a 2a 2a 20 7b 48 31 32 36 32 32 7d 20 45 78 74 .** {H12622} Ext
33ab0 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 69 ension loading i
33ac0 73 20 6f 66 66 20 62 79 20 64 65 66 61 75 6c 74 s off by default
33ad0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
33ae0 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 int sqlite3_enab
33af0 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f le_load_extensio
33b00 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 n(sqlite3 *db, i
33b10 6e 74 20 6f 6e 6f 66 66 29 3b 0a 0a 2f 2a 0a 2a nt onoff);../*.*
33b20 2a 20 43 41 50 49 33 52 45 46 3a 20 41 75 74 6f * CAPI3REF: Auto
33b30 6d 61 74 69 63 61 6c 6c 79 20 4c 6f 61 64 20 41 matically Load A
33b40 6e 20 45 78 74 65 6e 73 69 6f 6e 73 20 7b 48 31 n Extensions {H1
33b50 32 36 34 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2640} <S20500>.*
33b60 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 63 61 *.** This API ca
33b70 6e 20 62 65 20 69 6e 76 6f 6b 65 64 20 61 74 20 n be invoked at
33b80 70 72 6f 67 72 61 6d 20 73 74 61 72 74 75 70 20 program startup
33b90 69 6e 20 6f 72 64 65 72 20 74 6f 20 72 65 67 69 in order to regi
33ba0 73 74 65 72 0a 2a 2a 20 6f 6e 65 20 6f 72 20 6d ster.** one or m
33bb0 6f 72 65 20 73 74 61 74 69 63 61 6c 6c 79 20 6c ore statically l
33bc0 69 6e 6b 65 64 20 65 78 74 65 6e 73 69 6f 6e 73 inked extensions
33bd0 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 61 76 that will be av
33be0 61 69 6c 61 62 6c 65 0a 2a 2a 20 74 6f 20 61 6c ailable.** to al
33bf0 6c 20 6e 65 77 20 5b 64 61 74 61 62 61 73 65 20 l new [database
33c00 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d 2e 20 7b 45 connections]. {E
33c10 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 ND}.**.** This r
33c20 6f 75 74 69 6e 65 20 73 74 6f 72 65 73 20 61 20 outine stores a
33c30 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 65 pointer to the e
33c40 78 74 65 6e 73 69 6f 6e 20 69 6e 20 61 6e 20 61 xtension in an a
33c50 72 72 61 79 20 74 68 61 74 20 69 73 0a 2a 2a 20 rray that is.**
33c60 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 obtained from [s
33c70 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d qlite3_malloc()]
33c80 2e 20 20 49 66 20 79 6f 75 20 72 75 6e 20 61 20 . If you run a
33c90 6d 65 6d 6f 72 79 20 6c 65 61 6b 20 63 68 65 63 memory leak chec
33ca0 6b 65 72 0a 2a 2a 20 6f 6e 20 79 6f 75 72 20 70 ker.** on your p
33cb0 72 6f 67 72 61 6d 20 61 6e 64 20 69 74 20 72 65 rogram and it re
33cc0 70 6f 72 74 73 20 61 20 6c 65 61 6b 20 62 65 63 ports a leak bec
33cd0 61 75 73 65 20 6f 66 20 74 68 69 73 20 61 72 72 ause of this arr
33ce0 61 79 2c 20 69 6e 76 6f 6b 65 0a 2a 2a 20 5b 73 ay, invoke.** [s
33cf0 71 6c 69 74 65 33 5f 72 65 73 65 74 5f 61 75 74 qlite3_reset_aut
33d00 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 70 o_extension()] p
33d10 72 69 6f 72 20 74 6f 20 73 68 75 74 64 6f 77 6e rior to shutdown
33d20 20 74 6f 20 66 72 65 65 20 74 68 65 20 6d 65 6d to free the mem
33d30 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 ory..**.** {H126
33d40 34 31 7d 20 54 68 69 73 20 66 75 6e 63 74 69 6f 41} This functio
33d50 6e 20 72 65 67 69 73 74 65 72 73 20 61 6e 20 65 n registers an e
33d60 78 74 65 6e 73 69 6f 6e 20 65 6e 74 72 79 20 70 xtension entry p
33d70 6f 69 6e 74 20 74 68 61 74 20 69 73 0a 2a 2a 20 oint that is.**
33d80 20 20 20 20 20 20 20 20 20 61 75 74 6f 6d 61 74 automat
33d90 69 63 61 6c 6c 79 20 69 6e 76 6f 6b 65 64 20 77 ically invoked w
33da0 68 65 6e 65 76 65 72 20 61 20 6e 65 77 20 5b 64 henever a new [d
33db0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
33dc0 6f 6e 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 on].**
33dd0 69 73 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 20 is opened using
33de0 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d [sqlite3_open()]
33df0 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 , [sqlite3_open1
33e00 36 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 6()],.**
33e10 20 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 6f 70 or [sqlite3_op
33e20 65 6e 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 en_v2()]..**.**
33e30 7b 48 31 32 36 34 32 7d 20 44 75 70 6c 69 63 61 {H12642} Duplica
33e40 74 65 20 65 78 74 65 6e 73 69 6f 6e 73 20 61 72 te extensions ar
33e50 65 20 64 65 74 65 63 74 65 64 20 73 6f 20 63 61 e detected so ca
33e60 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 lling this routi
33e70 6e 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6d ne.** m
33e80 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 77 69 ultiple times wi
33e90 74 68 20 74 68 65 20 73 61 6d 65 20 65 78 74 65 th the same exte
33ea0 6e 73 69 6f 6e 20 69 73 20 68 61 72 6d 6c 65 73 nsion is harmles
33eb0 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34 33 s..**.** {H12643
33ec0 7d 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 } This routine s
33ed0 74 6f 72 65 73 20 61 20 70 6f 69 6e 74 65 72 20 tores a pointer
33ee0 74 6f 20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e to the extension
33ef0 20 69 6e 20 61 6e 20 61 72 72 61 79 0a 2a 2a 20 in an array.**
33f00 20 20 20 20 20 20 20 20 20 74 68 61 74 20 69 73 that is
33f10 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b obtained from [
33f20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 sqlite3_malloc()
33f30 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34 34 ]..**.** {H12644
33f40 7d 20 41 75 74 6f 6d 61 74 69 63 20 65 78 74 65 } Automatic exte
33f50 6e 73 69 6f 6e 73 20 61 70 70 6c 79 20 61 63 72 nsions apply acr
33f60 6f 73 73 20 61 6c 6c 20 74 68 72 65 61 64 73 2e oss all threads.
33f70 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
33f80 6e 74 20 73 71 6c 69 74 65 33 5f 61 75 74 6f 5f nt sqlite3_auto_
33f90 65 78 74 65 6e 73 69 6f 6e 28 76 6f 69 64 20 28 extension(void (
33fa0 2a 78 45 6e 74 72 79 50 6f 69 6e 74 29 28 76 6f *xEntryPoint)(vo
33fb0 69 64 29 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 id));../*.** CAP
33fc0 49 33 52 45 46 3a 20 52 65 73 65 74 20 41 75 74 I3REF: Reset Aut
33fd0 6f 6d 61 74 69 63 20 45 78 74 65 6e 73 69 6f 6e omatic Extension
33fe0 20 4c 6f 61 64 69 6e 67 20 7b 48 31 32 36 36 30 Loading {H12660
33ff0 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 0a 2a 2a } <S20500>.**.**
34000 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 This function d
34010 69 73 61 62 6c 65 73 20 61 6c 6c 20 70 72 65 76 isables all prev
34020 69 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72 65 iously registere
34030 64 20 61 75 74 6f 6d 61 74 69 63 0a 2a 2a 20 65 d automatic.** e
34040 78 74 65 6e 73 69 6f 6e 73 2e 20 7b 45 4e 44 7d xtensions. {END}
34050 20 20 49 74 20 75 6e 64 6f 65 73 20 74 68 65 20 It undoes the
34060 65 66 66 65 63 74 20 6f 66 20 61 6c 6c 20 70 72 effect of all pr
34070 69 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f ior.** [sqlite3_
34080 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 29 auto_extension()
34090 5d 20 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 7b ] calls..**.** {
340a0 48 31 32 36 36 31 7d 20 54 68 69 73 20 66 75 6e H12661} This fun
340b0 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 20 61 ction disables a
340c0 6c 6c 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 ll previously re
340d0 67 69 73 74 65 72 65 64 0a 2a 2a 20 20 20 20 20 gistered.**
340e0 20 20 20 20 20 61 75 74 6f 6d 61 74 69 63 20 65 automatic e
340f0 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a xtensions..**.**
34100 20 7b 48 31 32 36 36 32 7d 20 54 68 69 73 20 66 {H12662} This f
34110 75 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 unction disables
34120 20 61 75 74 6f 6d 61 74 69 63 20 65 78 74 65 6e automatic exten
34130 73 69 6f 6e 73 20 69 6e 20 61 6c 6c 20 74 68 72 sions in all thr
34140 65 61 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f eads..*/.SQLITE_
34150 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
34160 5f 72 65 73 65 74 5f 61 75 74 6f 5f 65 78 74 65 _reset_auto_exte
34170 6e 73 69 6f 6e 28 76 6f 69 64 29 3b 0a 0a 2f 2a nsion(void);../*
34180 0a 2a 2a 2a 2a 2a 2a 20 45 58 50 45 52 49 4d 45 .****** EXPERIME
34190 4e 54 41 4c 20 2d 20 73 75 62 6a 65 63 74 20 74 NTAL - subject t
341a0 6f 20 63 68 61 6e 67 65 20 77 69 74 68 6f 75 74 o change without
341b0 20 6e 6f 74 69 63 65 20 2a 2a 2a 2a 2a 2a 2a 2a notice ********
341c0 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 ******.**.** The
341d0 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68 interface to th
341e0 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 e virtual-table
341f0 6d 65 63 68 61 6e 69 73 6d 20 69 73 20 63 75 72 mechanism is cur
34200 72 65 6e 74 6c 79 20 63 6f 6e 73 69 64 65 72 65 rently considere
34210 64 0a 2a 2a 20 74 6f 20 62 65 20 65 78 70 65 72 d.** to be exper
34220 69 6d 65 6e 74 61 6c 2e 20 20 54 68 65 20 69 6e imental. The in
34230 74 65 72 66 61 63 65 20 6d 69 67 68 74 20 63 68 terface might ch
34240 61 6e 67 65 20 69 6e 20 69 6e 63 6f 6d 70 61 74 ange in incompat
34250 69 62 6c 65 20 77 61 79 73 2e 0a 2a 2a 20 49 66 ible ways..** If
34260 20 74 68 69 73 20 69 73 20 61 20 70 72 6f 62 6c this is a probl
34270 65 6d 20 66 6f 72 20 79 6f 75 2c 20 64 6f 20 6e em for you, do n
34280 6f 74 20 75 73 65 20 74 68 65 20 69 6e 74 65 72 ot use the inter
34290 66 61 63 65 20 61 74 20 74 68 69 73 20 74 69 6d face at this tim
342a0 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 e..**.** When th
342b0 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 e virtual-table
342c0 6d 65 63 68 61 6e 69 73 6d 20 73 74 61 62 69 6c mechanism stabil
342d0 69 7a 65 73 2c 20 77 65 20 77 69 6c 6c 20 64 65 izes, we will de
342e0 63 6c 61 72 65 20 74 68 65 0a 2a 2a 20 69 6e 74 clare the.** int
342f0 65 72 66 61 63 65 20 66 69 78 65 64 2c 20 73 75 erface fixed, su
34300 70 70 6f 72 74 20 69 74 20 69 6e 64 65 66 69 6e pport it indefin
34310 69 74 65 6c 79 2c 20 61 6e 64 20 72 65 6d 6f 76 itely, and remov
34320 65 20 74 68 69 73 20 63 6f 6d 6d 65 6e 74 2e 0a e this comment..
34330 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63 74 */../*.** Struct
34340 75 72 65 73 20 75 73 65 64 20 62 79 20 74 68 65 ures used by the
34350 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 virtual table i
34360 6e 74 65 72 66 61 63 65 0a 2a 2f 0a 74 79 70 65 nterface.*/.type
34370 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 def struct sqlit
34380 65 33 5f 76 74 61 62 20 73 71 6c 69 74 65 33 5f e3_vtab sqlite3_
34390 76 74 61 62 3b 0a 74 79 70 65 64 65 66 20 73 74 vtab;.typedef st
343a0 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 ruct sqlite3_ind
343b0 65 78 5f 69 6e 66 6f 20 73 71 6c 69 74 65 33 5f ex_info sqlite3_
343c0 69 6e 64 65 78 5f 69 6e 66 6f 3b 0a 74 79 70 65 index_info;.type
343d0 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 def struct sqlit
343e0 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 73 e3_vtab_cursor s
343f0 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 qlite3_vtab_curs
34400 6f 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 or;.typedef stru
34410 63 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c ct sqlite3_modul
34420 65 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 e sqlite3_module
34430 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
34440 46 3a 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 F: Virtual Table
34450 20 4f 62 6a 65 63 74 20 7b 48 31 38 30 30 30 7d Object {H18000}
34460 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b 45 59 <S20400>.** KEY
34470 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 33 5f 6d WORDS: sqlite3_m
34480 6f 64 75 6c 65 20 7b 76 69 72 74 75 61 6c 20 74 odule {virtual t
34490 61 62 6c 65 20 6d 6f 64 75 6c 65 7d 0a 2a 2a 20 able module}.**
344a0 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a EXPERIMENTAL.**.
344b0 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72 ** This structur
344c0 65 2c 20 73 6f 6d 65 74 69 6d 65 73 20 63 61 6c e, sometimes cal
344d0 6c 65 64 20 61 20 61 20 22 76 69 72 74 75 61 6c led a a "virtual
344e0 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 22 2c 20 table module",
344f0 0a 2a 2a 20 64 65 66 69 6e 65 73 20 74 68 65 20 .** defines the
34500 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
34510 66 20 61 20 5b 76 69 72 74 75 61 6c 20 74 61 62 f a [virtual tab
34520 6c 65 73 5d 2e 20 20 0a 2a 2a 20 54 68 69 73 20 les]. .** This
34530 73 74 72 75 63 74 75 72 65 20 63 6f 6e 73 69 73 structure consis
34540 74 73 20 6d 6f 73 74 6c 79 20 6f 66 20 6d 65 74 ts mostly of met
34550 68 6f 64 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 hods for the mod
34560 75 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 69 72 ule..**.** A vir
34570 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c tual table modul
34580 65 20 69 73 20 63 72 65 61 74 65 64 20 62 79 20 e is created by
34590 66 69 6c 6c 69 6e 67 20 69 6e 20 61 20 70 65 72 filling in a per
345a0 73 69 73 74 65 6e 74 0a 2a 2a 20 69 6e 73 74 61 sistent.** insta
345b0 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 nce of this stru
345c0 63 74 75 72 65 20 61 6e 64 20 70 61 73 73 69 6e cture and passin
345d0 67 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 g a pointer to t
345e0 68 61 74 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 hat instance.**
345f0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 to [sqlite3_crea
34600 74 65 5f 6d 6f 64 75 6c 65 28 29 5d 20 6f 72 20 te_module()] or
34610 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f [sqlite3_create_
34620 6d 6f 64 75 6c 65 5f 76 32 28 29 5d 2e 0a 2a 2a module_v2()]..**
34630 20 54 68 65 20 72 65 67 69 73 74 72 61 74 69 6f The registratio
34640 6e 20 72 65 6d 61 69 6e 73 20 76 61 6c 69 64 20 n remains valid
34650 75 6e 74 69 6c 20 69 74 20 69 73 20 72 65 70 6c until it is repl
34660 61 63 65 64 20 62 79 20 61 20 64 69 66 66 65 72 aced by a differ
34670 65 6e 74 0a 2a 2a 20 6d 6f 64 75 6c 65 20 6f 72 ent.** module or
34680 20 75 6e 74 69 6c 20 74 68 65 20 5b 64 61 74 61 until the [data
34690 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d base connection]
346a0 20 63 6c 6f 73 65 73 2e 20 20 54 68 65 20 63 6f closes. The co
346b0 6e 74 65 6e 74 0a 2a 2a 20 6f 66 20 74 68 69 73 ntent.** of this
346c0 20 73 74 72 75 63 74 75 72 65 20 6d 75 73 74 20 structure must
346d0 6e 6f 74 20 63 68 61 6e 67 65 20 77 68 69 6c 65 not change while
346e0 20 69 74 20 69 73 20 72 65 67 69 73 74 65 72 65 it is registere
346f0 64 20 77 69 74 68 0a 2a 2a 20 61 6e 79 20 64 61 d with.** any da
34700 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
34710 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c n..*/.struct sql
34720 69 74 65 33 5f 6d 6f 64 75 6c 65 20 7b 0a 20 20 ite3_module {.
34730 69 6e 74 20 69 56 65 72 73 69 6f 6e 3b 0a 20 20 int iVersion;.
34740 69 6e 74 20 28 2a 78 43 72 65 61 74 65 29 28 73 int (*xCreate)(s
34750 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 20 2a 70 qlite3*, void *p
34760 41 75 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 Aux,.
34770 20 20 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f int argc, co
34780 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a nst char *const*
34790 61 72 67 76 2c 0a 20 20 20 20 20 20 20 20 20 20 argv,.
347a0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 sqlite3_vta
347b0 62 20 2a 2a 70 70 56 54 61 62 2c 20 63 68 61 72 b **ppVTab, char
347c0 2a 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6f **);. int (*xCo
347d0 6e 6e 65 63 74 29 28 73 71 6c 69 74 65 33 2a 2c nnect)(sqlite3*,
347e0 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20 20 20 void *pAux,.
347f0 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 int
34800 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 argc, const char
34810 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 20 *const*argv,.
34820 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c sql
34830 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56 54 ite3_vtab **ppVT
34840 61 62 2c 20 63 68 61 72 2a 2a 29 3b 0a 20 20 69 ab, char**);. i
34850 6e 74 20 28 2a 78 42 65 73 74 49 6e 64 65 78 29 nt (*xBestIndex)
34860 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 (sqlite3_vtab *p
34870 56 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 69 6e VTab, sqlite3_in
34880 64 65 78 5f 69 6e 66 6f 2a 29 3b 0a 20 20 69 6e dex_info*);. in
34890 74 20 28 2a 78 44 69 73 63 6f 6e 6e 65 63 74 29 t (*xDisconnect)
348a0 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 (sqlite3_vtab *p
348b0 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78 VTab);. int (*x
348c0 44 65 73 74 72 6f 79 29 28 73 71 6c 69 74 65 33 Destroy)(sqlite3
348d0 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 _vtab *pVTab);.
348e0 20 69 6e 74 20 28 2a 78 4f 70 65 6e 29 28 73 71 int (*xOpen)(sq
348f0 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 lite3_vtab *pVTa
34900 62 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f b, sqlite3_vtab_
34910 63 75 72 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f cursor **ppCurso
34920 72 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6c 6f r);. int (*xClo
34930 73 65 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 se)(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 46 69 6c 74 65 72 29 28 73 71 6c 69 (*xFilter)(sqli
34960 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a te3_vtab_cursor*
34970 2c 20 69 6e 74 20 69 64 78 4e 75 6d 2c 20 63 6f , int idxNum, co
34980 6e 73 74 20 63 68 61 72 20 2a 69 64 78 53 74 72 nst char *idxStr
34990 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
349a0 20 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 int argc, sqli
349b0 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 te3_value **argv
349c0 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4e 65 78 74 );. int (*xNext
349d0 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 )(sqlite3_vtab_c
349e0 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 20 28 ursor*);. int (
349f0 2a 78 45 6f 66 29 28 73 71 6c 69 74 65 33 5f 76 *xEof)(sqlite3_v
34a00 74 61 62 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20 tab_cursor*);.
34a10 69 6e 74 20 28 2a 78 43 6f 6c 75 6d 6e 29 28 73 int (*xColumn)(s
34a20 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 qlite3_vtab_curs
34a30 6f 72 2a 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e or*, sqlite3_con
34a40 74 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 text*, int);. i
34a50 6e 74 20 28 2a 78 52 6f 77 69 64 29 28 73 71 6c nt (*xRowid)(sql
34a60 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 ite3_vtab_cursor
34a70 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 *, sqlite3_int64
34a80 20 2a 70 52 6f 77 69 64 29 3b 0a 20 20 69 6e 74 *pRowid);. int
34a90 20 28 2a 78 55 70 64 61 74 65 29 28 73 71 6c 69 (*xUpdate)(sqli
34aa0 74 65 33 5f 76 74 61 62 20 2a 2c 20 69 6e 74 2c te3_vtab *, int,
34ab0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
34ac0 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 *, sqlite3_int64
34ad0 20 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 42 65 *);. int (*xBe
34ae0 67 69 6e 29 28 73 71 6c 69 74 65 33 5f 76 74 61 gin)(sqlite3_vta
34af0 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 b *pVTab);. int
34b00 20 28 2a 78 53 79 6e 63 29 28 73 71 6c 69 74 65 (*xSync)(sqlite
34b10 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 3_vtab *pVTab);.
34b20 20 20 69 6e 74 20 28 2a 78 43 6f 6d 6d 69 74 29 int (*xCommit)
34b30 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 (sqlite3_vtab *p
34b40 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78 VTab);. int (*x
34b50 52 6f 6c 6c 62 61 63 6b 29 28 73 71 6c 69 74 65 Rollback)(sqlite
34b60 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 3_vtab *pVTab);.
34b70 20 20 69 6e 74 20 28 2a 78 46 69 6e 64 46 75 6e int (*xFindFun
34b80 63 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 5f 76 ction)(sqlite3_v
34b90 74 61 62 20 2a 70 56 74 61 62 2c 20 69 6e 74 20 tab *pVtab, int
34ba0 6e 41 72 67 2c 20 63 6f 6e 73 74 20 63 68 61 72 nArg, const char
34bb0 20 2a 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 *zName,.
34bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
34bd0 76 6f 69 64 20 28 2a 2a 70 78 46 75 6e 63 29 28 void (**pxFunc)(
34be0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a sqlite3_context*
34bf0 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c ,int,sqlite3_val
34c00 75 65 2a 2a 29 2c 0a 20 20 20 20 20 20 20 20 20 ue**),.
34c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 76 6f vo
34c20 69 64 20 2a 2a 70 70 41 72 67 29 3b 0a 20 20 69 id **ppArg);. i
34c30 6e 74 20 28 2a 78 52 65 6e 61 6d 65 29 28 73 71 nt (*xRename)(sq
34c40 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 lite3_vtab *pVta
34c50 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a b, const char *z
34c60 4e 65 77 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 New);.};../*.**
34c70 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 75 61 CAPI3REF: Virtua
34c80 6c 20 54 61 62 6c 65 20 49 6e 64 65 78 69 6e 67 l Table Indexing
34c90 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 7b 48 31 Information {H1
34ca0 38 31 30 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 8100} <S20400>.*
34cb0 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 * KEYWORDS: sqli
34cc0 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 0a 2a te3_index_info.*
34cd0 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a * EXPERIMENTAL.*
34ce0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
34cf0 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 _index_info stru
34d00 63 74 75 72 65 20 61 6e 64 20 69 74 73 20 73 75 cture and its su
34d10 62 73 74 72 75 63 74 75 72 65 73 20 69 73 20 75 bstructures is u
34d20 73 65 64 20 74 6f 0a 2a 2a 20 70 61 73 73 20 69 sed to.** pass i
34d30 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 74 6f 20 nformation into
34d40 61 6e 64 20 72 65 63 65 69 76 65 20 74 68 65 20 and receive the
34d50 72 65 70 6c 79 20 66 72 6f 6d 20 74 68 65 20 5b reply from the [
34d60 78 42 65 73 74 49 6e 64 65 78 5d 0a 2a 2a 20 6d xBestIndex].** m
34d70 65 74 68 6f 64 20 6f 66 20 61 20 5b 76 69 72 74 ethod of a [virt
34d80 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 ual table module
34d90 5d 2e 20 20 54 68 65 20 66 69 65 6c 64 73 20 75 ]. The fields u
34da0 6e 64 65 72 20 2a 2a 49 6e 70 75 74 73 2a 2a 20 nder **Inputs**
34db0 61 72 65 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 are the.** input
34dc0 73 20 74 6f 20 78 42 65 73 74 49 6e 64 65 78 20 s to xBestIndex
34dd0 61 6e 64 20 61 72 65 20 72 65 61 64 2d 6f 6e 6c and are read-onl
34de0 79 2e 20 20 78 42 65 73 74 49 6e 64 65 78 20 69 y. xBestIndex i
34df0 6e 73 65 72 74 73 20 69 74 73 0a 2a 2a 20 72 65 nserts its.** re
34e00 73 75 6c 74 73 20 69 6e 74 6f 20 74 68 65 20 2a sults into the *
34e10 2a 4f 75 74 70 75 74 73 2a 2a 20 66 69 65 6c 64 *Outputs** field
34e20 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 43 6f s..**.** The aCo
34e30 6e 73 74 72 61 69 6e 74 5b 5d 20 61 72 72 61 79 nstraint[] array
34e40 20 72 65 63 6f 72 64 73 20 57 48 45 52 45 20 63 records WHERE c
34e50 6c 61 75 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 lause constraint
34e60 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a s of the form:.*
34e70 2a 0a 2a 2a 20 3c 70 72 65 3e 63 6f 6c 75 6d 6e *.** <pre>column
34e80 20 4f 50 20 65 78 70 72 3c 2f 70 72 65 3e 0a 2a OP expr</pre>.*
34e90 2a 0a 2a 2a 20 77 68 65 72 65 20 4f 50 20 69 73 *.** where OP is
34ea0 20 3d 2c 20 26 6c 74 3b 2c 20 26 6c 74 3b 3d 2c =, <, <=,
34eb0 20 26 67 74 3b 2c 20 6f 72 20 26 67 74 3b 3d 2e >, or >=.
34ec0 20 20 54 68 65 20 70 61 72 74 69 63 75 6c 61 72 The particular
34ed0 20 6f 70 65 72 61 74 6f 72 20 69 73 0a 2a 2a 20 operator is.**
34ee0 73 74 6f 72 65 64 20 69 6e 20 61 43 6f 6e 73 74 stored in aConst
34ef0 72 61 69 6e 74 5b 5d 2e 6f 70 2e 20 20 54 68 65 raint[].op. The
34f00 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 63 6f index of the co
34f10 6c 75 6d 6e 20 69 73 20 73 74 6f 72 65 64 20 69 lumn is stored i
34f20 6e 0a 2a 2a 20 61 43 6f 6e 73 74 72 61 69 6e 74 n.** aConstraint
34f30 5b 5d 2e 69 43 6f 6c 75 6d 6e 2e 20 20 61 43 6f [].iColumn. aCo
34f40 6e 73 74 72 61 69 6e 74 5b 5d 2e 75 73 61 62 6c nstraint[].usabl
34f50 65 20 69 73 20 54 52 55 45 20 69 66 20 74 68 65 e is TRUE if the
34f60 0a 2a 2a 20 65 78 70 72 20 6f 6e 20 74 68 65 20 .** expr on the
34f70 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 right-hand side
34f80 63 61 6e 20 62 65 20 65 76 61 6c 75 61 74 65 64 can be evaluated
34f90 20 28 61 6e 64 20 74 68 75 73 20 74 68 65 20 63 (and thus the c
34fa0 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 69 73 20 onstraint.** is
34fb0 75 73 61 62 6c 65 29 20 61 6e 64 20 66 61 6c 73 usable) and fals
34fc0 65 20 69 66 20 69 74 20 63 61 6e 6e 6f 74 2e 0a e if it cannot..
34fd0 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70 74 69 6d 69 **.** The optimi
34fe0 7a 65 72 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c zer automaticall
34ff0 79 20 69 6e 76 65 72 74 73 20 74 65 72 6d 73 20 y inverts terms
35000 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 65 78 70 of the form "exp
35010 72 20 4f 50 20 63 6f 6c 75 6d 6e 22 0a 2a 2a 20 r OP column".**
35020 61 6e 64 20 6d 61 6b 65 73 20 6f 74 68 65 72 20 and makes other
35030 73 69 6d 70 6c 69 66 69 63 61 74 69 6f 6e 73 20 simplifications
35040 74 6f 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 to the WHERE cla
35050 75 73 65 20 69 6e 20 61 6e 20 61 74 74 65 6d 70 use in an attemp
35060 74 20 74 6f 0a 2a 2a 20 67 65 74 20 61 73 20 6d t to.** get as m
35070 61 6e 79 20 57 48 45 52 45 20 63 6c 61 75 73 65 any WHERE clause
35080 20 74 65 72 6d 73 20 69 6e 74 6f 20 74 68 65 20 terms into the
35090 66 6f 72 6d 20 73 68 6f 77 6e 20 61 62 6f 76 65 form shown above
350a0 20 61 73 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a as possible..**
350b0 20 54 68 65 20 61 43 6f 6e 73 74 72 61 69 6e 74 The aConstraint
350c0 5b 5d 20 61 72 72 61 79 20 6f 6e 6c 79 20 72 65 [] array only re
350d0 70 6f 72 74 73 20 57 48 45 52 45 20 63 6c 61 75 ports WHERE clau
350e0 73 65 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20 se terms in the
350f0 63 6f 72 72 65 63 74 0a 2a 2a 20 66 6f 72 6d 20 correct.** form
35100 74 68 61 74 20 72 65 66 65 72 20 74 6f 20 74 68 that refer to th
35110 65 20 70 61 72 74 69 63 75 6c 61 72 20 76 69 72 e particular vir
35120 74 75 61 6c 20 74 61 62 6c 65 20 62 65 69 6e 67 tual table being
35130 20 71 75 65 72 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 queried..**.**
35140 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 Information abou
35150 74 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 t the ORDER BY c
35160 6c 61 75 73 65 20 69 73 20 73 74 6f 72 65 64 20 lause is stored
35170 69 6e 20 61 4f 72 64 65 72 42 79 5b 5d 2e 0a 2a in aOrderBy[]..*
35180 2a 20 45 61 63 68 20 74 65 72 6d 20 6f 66 20 61 * Each term of a
35190 4f 72 64 65 72 42 79 20 72 65 63 6f 72 64 73 20 OrderBy records
351a0 61 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 a column of the
351b0 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 2e ORDER BY clause.
351c0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 78 42 65 73 .**.** The [xBes
351d0 74 49 6e 64 65 78 5d 20 6d 65 74 68 6f 64 20 6d tIndex] method m
351e0 75 73 74 20 66 69 6c 6c 20 61 43 6f 6e 73 74 72 ust fill aConstr
351f0 61 69 6e 74 55 73 61 67 65 5b 5d 20 77 69 74 68 aintUsage[] with
35200 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 information.**
35210 61 62 6f 75 74 20 77 68 61 74 20 70 61 72 61 6d about what param
35220 65 74 65 72 73 20 74 6f 20 70 61 73 73 20 74 6f eters to pass to
35230 20 78 46 69 6c 74 65 72 2e 20 20 49 66 20 61 72 xFilter. If ar
35240 67 76 49 6e 64 65 78 3e 30 20 74 68 65 6e 0a 2a gvIndex>0 then.*
35250 2a 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64 * the right-hand
35260 20 73 69 64 65 20 6f 66 20 74 68 65 20 63 6f 72 side of the cor
35270 72 65 73 70 6f 6e 64 69 6e 67 20 61 43 6f 6e 73 responding aCons
35280 74 72 61 69 6e 74 5b 5d 20 69 73 20 65 76 61 6c traint[] is eval
35290 75 61 74 65 64 0a 2a 2a 20 61 6e 64 20 62 65 63 uated.** and bec
352a0 6f 6d 65 73 20 74 68 65 20 61 72 67 76 49 6e 64 omes the argvInd
352b0 65 78 2d 74 68 20 65 6e 74 72 79 20 69 6e 20 61 ex-th entry in a
352c0 72 67 76 2e 20 20 49 66 20 61 43 6f 6e 73 74 72 rgv. If aConstr
352d0 61 69 6e 74 55 73 61 67 65 5b 5d 2e 6f 6d 69 74 aintUsage[].omit
352e0 0a 2a 2a 20 69 73 20 74 72 75 65 2c 20 74 68 65 .** is true, the
352f0 6e 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 n the constraint
35300 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 62 is assumed to b
35310 65 20 66 75 6c 6c 79 20 68 61 6e 64 6c 65 64 20 e fully handled
35320 62 79 20 74 68 65 0a 2a 2a 20 76 69 72 74 75 61 by the.** virtua
35330 6c 20 74 61 62 6c 65 20 61 6e 64 20 69 73 20 6e l table and is n
35340 6f 74 20 63 68 65 63 6b 65 64 20 61 67 61 69 6e ot checked again
35350 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a by SQLite..**.*
35360 2a 20 54 68 65 20 69 64 78 4e 75 6d 20 61 6e 64 * The idxNum and
35370 20 69 64 78 50 74 72 20 76 61 6c 75 65 73 20 61 idxPtr values a
35380 72 65 20 72 65 63 6f 72 64 65 64 20 61 6e 64 20 re recorded and
35390 70 61 73 73 65 64 20 69 6e 74 6f 20 74 68 65 0a passed into the.
353a0 2a 2a 20 5b 78 46 69 6c 74 65 72 5d 20 6d 65 74 ** [xFilter] met
353b0 68 6f 64 2e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 hod..** [sqlite3
353c0 5f 66 72 65 65 28 29 5d 20 69 73 20 75 73 65 64 _free()] is used
353d0 20 74 6f 20 66 72 65 65 20 69 64 78 50 74 72 20 to free idxPtr
353e0 69 66 20 61 6e 64 20 6f 6e 6c 79 20 69 66 66 0a if and only iff.
353f0 2a 2a 20 6e 65 65 64 54 6f 46 72 65 65 49 64 78 ** needToFreeIdx
35400 50 74 72 20 69 73 20 74 72 75 65 2e 0a 2a 2a 0a Ptr is true..**.
35410 2a 2a 20 54 68 65 20 6f 72 64 65 72 42 79 43 6f ** The orderByCo
35420 6e 73 75 6d 65 64 20 6d 65 61 6e 73 20 74 68 61 nsumed means tha
35430 74 20 6f 75 74 70 75 74 20 66 72 6f 6d 20 5b 78 t output from [x
35440 46 69 6c 74 65 72 5d 2f 5b 78 4e 65 78 74 5d 20 Filter]/[xNext]
35450 77 69 6c 6c 20 6f 63 63 75 72 20 69 6e 0a 2a 2a will occur in.**
35460 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f 72 64 the correct ord
35470 65 72 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 er to satisfy th
35480 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 e ORDER BY claus
35490 65 20 73 6f 20 74 68 61 74 20 6e 6f 20 73 65 70 e so that no sep
354a0 61 72 61 74 65 0a 2a 2a 20 73 6f 72 74 69 6e 67 arate.** sorting
354b0 20 73 74 65 70 20 69 73 20 72 65 71 75 69 72 65 step is require
354c0 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 73 74 d..**.** The est
354d0 69 6d 61 74 65 64 43 6f 73 74 20 76 61 6c 75 65 imatedCost value
354e0 20 69 73 20 61 6e 20 65 73 74 69 6d 61 74 65 20 is an estimate
354f0 6f 66 20 74 68 65 20 63 6f 73 74 20 6f 66 20 64 of the cost of d
35500 6f 69 6e 67 20 74 68 65 0a 2a 2a 20 70 61 72 74 oing the.** part
35510 69 63 75 6c 61 72 20 6c 6f 6f 6b 75 70 2e 20 20 icular lookup.
35520 41 20 66 75 6c 6c 20 73 63 61 6e 20 6f 66 20 61 A full scan of a
35530 20 74 61 62 6c 65 20 77 69 74 68 20 4e 20 65 6e table with N en
35540 74 72 69 65 73 20 73 68 6f 75 6c 64 20 68 61 76 tries should hav
35550 65 0a 2a 2a 20 61 20 63 6f 73 74 20 6f 66 20 4e e.** a cost of N
35560 2e 20 20 41 20 62 69 6e 61 72 79 20 73 65 61 72 . A binary sear
35570 63 68 20 6f 66 20 61 20 74 61 62 6c 65 20 6f 66 ch of a table of
35580 20 4e 20 65 6e 74 72 69 65 73 20 73 68 6f 75 6c N entries shoul
35590 64 20 68 61 76 65 20 61 0a 2a 2a 20 63 6f 73 74 d have a.** cost
355a0 20 6f 66 20 61 70 70 72 6f 78 69 6d 61 74 65 6c of approximatel
355b0 79 20 6c 6f 67 28 4e 29 2e 0a 2a 2f 0a 73 74 72 y log(N)..*/.str
355c0 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 uct sqlite3_inde
355d0 78 5f 69 6e 66 6f 20 7b 0a 20 20 2f 2a 20 49 6e x_info {. /* In
355e0 70 75 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 puts */. int nC
355f0 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20 20 onstraint;
35600 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
35610 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 43 6f f entries in aCo
35620 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 73 74 nstraint */. st
35630 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 ruct sqlite3_ind
35640 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20 7b 0a ex_constraint {.
35650 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e int iColumn
35660 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
35670 2a 20 43 6f 6c 75 6d 6e 20 6f 6e 20 6c 65 66 74 * Column on left
35680 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 63 6f -hand side of co
35690 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 20 20 nstraint */.
356a0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6f unsigned char o
356b0 70 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f p; /* Co
356c0 6e 73 74 72 61 69 6e 74 20 6f 70 65 72 61 74 6f nstraint operato
356d0 72 20 2a 2f 0a 20 20 20 20 20 75 6e 73 69 67 6e r */. unsign
356e0 65 64 20 63 68 61 72 20 75 73 61 62 6c 65 3b 20 ed char usable;
356f0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 /* True if t
35700 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 his constraint i
35710 73 20 75 73 61 62 6c 65 20 2a 2f 0a 20 20 20 20 s usable */.
35720 20 69 6e 74 20 69 54 65 72 6d 4f 66 66 73 65 74 int iTermOffset
35730 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 ; /* Us
35740 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 2d 20 ed internally -
35750 78 42 65 73 74 49 6e 64 65 78 20 73 68 6f 75 6c xBestIndex shoul
35760 64 20 69 67 6e 6f 72 65 20 2a 2f 0a 20 20 7d 20 d ignore */. }
35770 2a 61 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20 *aConstraint;
35780 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c /* Tabl
35790 65 20 6f 66 20 57 48 45 52 45 20 63 6c 61 75 73 e of WHERE claus
357a0 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f e constraints */
357b0 0a 20 20 69 6e 74 20 6e 4f 72 64 65 72 42 79 3b . int nOrderBy;
357c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
357d0 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 Number of terms
357e0 20 69 6e 20 74 68 65 20 4f 52 44 45 52 20 42 59 in the ORDER BY
357f0 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74 72 clause */. str
35800 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 uct sqlite3_inde
35810 78 5f 6f 72 64 65 72 62 79 20 7b 0a 20 20 20 20 x_orderby {.
35820 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 int iColumn;
35830 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f /* Co
35840 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 lumn number */.
35850 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 unsigned cha
35860 72 20 64 65 73 63 3b 20 20 20 20 20 20 20 2f 2a r desc; /*
35870 20 54 72 75 65 20 66 6f 72 20 44 45 53 43 2e 20 True for DESC.
35880 20 46 61 6c 73 65 20 66 6f 72 20 41 53 43 2e 20 False for ASC.
35890 2a 2f 0a 20 20 7d 20 2a 61 4f 72 64 65 72 42 79 */. } *aOrderBy
358a0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
358b0 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 /* The ORDER BY
358c0 63 6c 61 75 73 65 20 2a 2f 0a 20 20 2f 2a 20 4f clause */. /* O
358d0 75 74 70 75 74 73 20 2a 2f 0a 20 20 73 74 72 75 utputs */. stru
358e0 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 ct sqlite3_index
358f0 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61 67 _constraint_usag
35900 65 20 7b 0a 20 20 20 20 69 6e 74 20 61 72 67 76 e {. int argv
35910 49 6e 64 65 78 3b 20 20 20 20 20 20 20 20 20 20 Index;
35920 20 2f 2a 20 69 66 20 3e 30 2c 20 63 6f 6e 73 74 /* if >0, const
35930 72 61 69 6e 74 20 69 73 20 70 61 72 74 20 6f 66 raint is part of
35940 20 61 72 67 76 20 74 6f 20 78 46 69 6c 74 65 72 argv to xFilter
35950 20 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 */. unsigned
35960 20 63 68 61 72 20 6f 6d 69 74 3b 20 20 20 20 20 char omit;
35970 20 2f 2a 20 44 6f 20 6e 6f 74 20 63 6f 64 65 20 /* Do not code
35980 61 20 74 65 73 74 20 66 6f 72 20 74 68 69 73 20 a test for this
35990 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 constraint */.
359a0 7d 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 } *aConstraintUs
359b0 61 67 65 3b 0a 20 20 69 6e 74 20 69 64 78 4e 75 age;. int idxNu
359c0 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 m;
359d0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 75 73 65 64 /* Number used
359e0 20 74 6f 20 69 64 65 6e 74 69 66 79 20 74 68 65 to identify the
359f0 20 69 6e 64 65 78 20 2a 2f 0a 20 20 63 68 61 72 index */. char
35a00 20 2a 69 64 78 53 74 72 3b 20 20 20 20 20 20 20 *idxStr;
35a10 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 /* String
35a20 2c 20 70 6f 73 73 69 62 6c 79 20 6f 62 74 61 69 , possibly obtai
35a30 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 ned from sqlite3
35a40 5f 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 20 69 6e 74 _malloc */. int
35a50 20 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53 74 needToFreeIdxSt
35a60 72 3b 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20 r; /* Free
35a70 69 64 78 53 74 72 20 75 73 69 6e 67 20 73 71 6c idxStr using sql
35a80 69 74 65 33 5f 66 72 65 65 28 29 20 69 66 20 74 ite3_free() if t
35a90 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 64 rue */. int ord
35aa0 65 72 42 79 43 6f 6e 73 75 6d 65 64 3b 20 20 20 erByConsumed;
35ab0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6f /* True if o
35ac0 75 74 70 75 74 20 69 73 20 61 6c 72 65 61 64 79 utput is already
35ad0 20 6f 72 64 65 72 65 64 20 2a 2f 0a 20 20 64 6f ordered */. do
35ae0 75 62 6c 65 20 65 73 74 69 6d 61 74 65 64 43 6f uble estimatedCo
35af0 73 74 3b 20 20 20 20 20 20 2f 2a 20 45 73 74 69 st; /* Esti
35b00 6d 61 74 65 64 20 63 6f 73 74 20 6f 66 20 75 73 mated cost of us
35b10 69 6e 67 20 74 68 69 73 20 69 6e 64 65 78 20 2a ing this index *
35b20 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c /.};.#define SQL
35b30 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 ITE_INDEX_CONSTR
35b40 41 49 4e 54 5f 45 51 20 20 20 20 32 0a 23 64 65 AINT_EQ 2.#de
35b50 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 fine SQLITE_INDE
35b60 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47 54 20 X_CONSTRAINT_GT
35b70 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53 51 4c 4.#define SQL
35b80 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 ITE_INDEX_CONSTR
35b90 41 49 4e 54 5f 4c 45 20 20 20 20 38 0a 23 64 65 AINT_LE 8.#de
35ba0 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 fine SQLITE_INDE
35bb0 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 54 20 X_CONSTRAINT_LT
35bc0 20 20 20 31 36 0a 23 64 65 66 69 6e 65 20 53 51 16.#define SQ
35bd0 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 LITE_INDEX_CONST
35be0 52 41 49 4e 54 5f 47 45 20 20 20 20 33 32 0a 23 RAINT_GE 32.#
35bf0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e define SQLITE_IN
35c00 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4d DEX_CONSTRAINT_M
35c10 41 54 43 48 20 36 34 0a 0a 2f 2a 0a 2a 2a 20 43 ATCH 64../*.** C
35c20 41 50 49 33 52 45 46 3a 20 52 65 67 69 73 74 65 API3REF: Registe
35c30 72 20 41 20 56 69 72 74 75 61 6c 20 54 61 62 6c r A Virtual Tabl
35c40 65 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e Implementation
35c50 20 7b 48 31 38 32 30 30 7d 20 3c 53 32 30 34 30 {H18200} <S2040
35c60 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 0>.** EXPERIMENT
35c70 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f AL.**.** This ro
35c80 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 74 6f utine is used to
35c90 20 72 65 67 69 73 74 65 72 20 61 20 6e 65 77 20 register a new
35ca0 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d [virtual table m
35cb0 6f 64 75 6c 65 5d 20 6e 61 6d 65 2e 0a 2a 2a 20 odule] name..**
35cc0 4d 6f 64 75 6c 65 20 6e 61 6d 65 73 20 6d 75 73 Module names mus
35cd0 74 20 62 65 20 72 65 67 69 73 74 65 72 65 64 20 t be registered
35ce0 62 65 66 6f 72 65 0a 2a 2a 20 63 72 65 61 74 69 before.** creati
35cf0 6e 67 20 61 20 6e 65 77 20 5b 76 69 72 74 75 61 ng a new [virtua
35d00 6c 20 74 61 62 6c 65 5d 20 75 73 69 6e 67 20 74 l table] using t
35d10 68 65 20 6d 6f 64 75 6c 65 2c 20 6f 72 20 62 65 he module, or be
35d20 66 6f 72 65 20 75 73 69 6e 67 20 61 0a 2a 2a 20 fore using a.**
35d30 70 72 65 65 78 69 73 74 69 6e 67 20 5b 76 69 72 preexisting [vir
35d40 74 75 61 6c 20 74 61 62 6c 65 5d 20 66 6f 72 20 tual table] for
35d50 74 68 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a the module..**.*
35d60 2a 20 54 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d * The module nam
35d70 65 20 69 73 20 72 65 67 69 73 74 65 72 65 64 20 e is registered
35d80 6f 6e 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 on the [database
35d90 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 65 connection] spe
35da0 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 74 68 65 cified.** by the
35db0 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 first parameter
35dc0 2e 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 . The name of t
35dd0 68 65 20 6d 6f 64 75 6c 65 20 69 73 20 67 69 76 he module is giv
35de0 65 6e 20 62 79 20 74 68 65 20 0a 2a 2a 20 73 65 en by the .** se
35df0 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 2e 20 cond parameter.
35e00 20 54 68 65 20 74 68 69 72 64 20 70 61 72 61 6d The third param
35e10 65 74 65 72 20 69 73 20 61 20 70 6f 69 6e 74 65 eter is a pointe
35e20 72 20 74 6f 0a 2a 2a 20 74 68 65 20 69 6d 70 6c r to.** the impl
35e30 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 ementation of th
35e40 65 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 e [virtual table
35e50 20 6d 6f 64 75 6c 65 5d 2e 20 20 20 54 68 65 20 module]. The
35e60 66 6f 75 72 74 68 0a 2a 2a 20 70 61 72 61 6d 65 fourth.** parame
35e70 74 65 72 20 69 73 20 61 6e 20 61 72 62 69 74 72 ter is an arbitr
35e80 61 72 79 20 63 6c 69 65 6e 74 20 64 61 74 61 20 ary client data
35e90 70 6f 69 6e 74 65 72 20 74 68 61 74 20 69 73 20 pointer that is
35ea0 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 0a 2a passed through.*
35eb0 2a 20 69 6e 74 6f 20 74 68 65 20 5b 78 43 72 65 * into the [xCre
35ec0 61 74 65 5d 20 61 6e 64 20 5b 78 43 6f 6e 6e 65 ate] and [xConne
35ed0 63 74 5d 20 6d 65 74 68 6f 64 73 20 6f 66 20 74 ct] methods of t
35ee0 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 he virtual table
35ef0 20 6d 6f 64 75 6c 65 0a 2a 2a 20 77 68 65 6e 20 module.** when
35f00 61 20 6e 65 77 20 76 69 72 74 75 61 6c 20 74 61 a new virtual ta
35f10 62 6c 65 20 69 73 20 62 65 20 62 65 69 6e 67 20 ble is be being
35f20 63 72 65 61 74 65 64 20 6f 72 20 72 65 69 6e 69 created or reini
35f30 74 69 61 6c 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 tialized..**.**
35f40 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 68 This interface h
35f50 61 73 20 65 78 61 63 74 6c 79 20 74 68 65 20 73 as exactly the s
35f60 61 6d 65 20 65 66 66 65 63 74 20 61 73 20 63 61 ame effect as ca
35f70 6c 6c 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 lling.** [sqlite
35f80 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 3_create_module_
35f90 76 32 28 29 5d 20 77 69 74 68 20 61 20 4e 55 4c v2()] with a NUL
35fa0 4c 20 63 6c 69 65 6e 74 20 64 61 74 61 20 64 65 L client data de
35fb0 73 74 72 75 63 74 6f 72 2e 0a 2a 2f 0a 53 51 4c structor..*/.SQL
35fc0 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 ITE_API SQLITE_E
35fd0 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 XPERIMENTAL int
35fe0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d sqlite3_create_m
35ff0 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 odule(. sqlite3
36000 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 *db,
36010 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 63 6f /* SQLite co
36020 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 72 65 67 69 nnection to regi
36030 73 74 65 72 20 6d 6f 64 75 6c 65 20 77 69 74 68 ster module with
36040 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
36050 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 *zName,
36060 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 /* Name of the
36070 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 module */. cons
36080 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 t sqlite3_module
36090 20 2a 70 2c 20 20 20 2f 2a 20 4d 65 74 68 6f 64 *p, /* Method
360a0 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 6c 65 s for the module
360b0 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6c 69 */. void *pCli
360c0 65 6e 74 44 61 74 61 20 20 20 20 20 20 20 20 20 entData
360d0 20 2f 2a 20 43 6c 69 65 6e 74 20 64 61 74 61 20 /* Client data
360e0 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f 6e for xCreate/xCon
360f0 6e 65 63 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a nect */.);../*.*
36100 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 67 69 * CAPI3REF: Regi
36110 73 74 65 72 20 41 20 56 69 72 74 75 61 6c 20 54 ster A Virtual T
36120 61 62 6c 65 20 49 6d 70 6c 65 6d 65 6e 74 61 74 able Implementat
36130 69 6f 6e 20 7b 48 31 38 32 31 30 7d 20 3c 53 32 ion {H18210} <S2
36140 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 0400>.** EXPERIM
36150 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ENTAL.**.** This
36160 20 72 6f 75 74 69 6e 65 20 69 73 20 69 64 65 6e routine is iden
36170 74 69 63 61 6c 20 74 6f 20 74 68 65 20 5b 73 71 tical to the [sq
36180 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 lite3_create_mod
36190 75 6c 65 28 29 5d 20 6d 65 74 68 6f 64 2c 0a 2a ule()] method,.*
361a0 2a 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74 * except that it
361b0 20 68 61 73 20 61 6e 20 65 78 74 72 61 20 70 61 has an extra pa
361c0 72 61 6d 65 74 65 72 20 74 6f 20 73 70 65 63 69 rameter to speci
361d0 66 79 20 0a 2a 2a 20 61 20 64 65 73 74 72 75 63 fy .** a destruc
361e0 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 tor function for
361f0 20 74 68 65 20 63 6c 69 65 6e 74 20 64 61 74 61 the client data
36200 20 70 6f 69 6e 74 65 72 2e 20 20 53 51 4c 69 74 pointer. SQLit
36210 65 20 77 69 6c 6c 0a 2a 2a 20 69 6e 76 6f 6b 65 e will.** invoke
36220 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 the destructor
36230 66 75 6e 63 74 69 6f 6e 20 28 69 66 20 69 74 20 function (if it
36240 69 73 20 6e 6f 74 20 4e 55 4c 4c 29 20 77 68 65 is not NULL) whe
36250 6e 20 53 51 4c 69 74 65 0a 2a 2a 20 6e 6f 20 6c n SQLite.** no l
36260 6f 6e 67 65 72 20 6e 65 65 64 73 20 74 68 65 20 onger needs the
36270 70 43 6c 69 65 6e 74 44 61 74 61 20 70 6f 69 6e pClientData poin
36280 74 65 72 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 ter. .*/.SQLITE
36290 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 _API SQLITE_EXPE
362a0 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c RIMENTAL int sql
362b0 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 ite3_create_modu
362c0 6c 65 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33 le_v2(. sqlite3
362d0 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 *db,
362e0 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 63 6f /* SQLite co
362f0 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 72 65 67 69 nnection to regi
36300 73 74 65 72 20 6d 6f 64 75 6c 65 20 77 69 74 68 ster module with
36310 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
36320 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 *zName,
36330 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 /* Name of the
36340 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 module */. cons
36350 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 t sqlite3_module
36360 20 2a 70 2c 20 20 20 2f 2a 20 4d 65 74 68 6f 64 *p, /* Method
36370 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 6c 65 s for the module
36380 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6c 69 */. void *pCli
36390 65 6e 74 44 61 74 61 2c 20 20 20 20 20 20 20 20 entData,
363a0 20 2f 2a 20 43 6c 69 65 6e 74 20 64 61 74 61 20 /* Client data
363b0 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f 6e for xCreate/xCon
363c0 6e 65 63 74 20 2a 2f 0a 20 20 76 6f 69 64 28 2a nect */. void(*
363d0 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64 2a 29 xDestroy)(void*)
363e0 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 64 /* Module d
363f0 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74 69 estructor functi
36400 6f 6e 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 on */.);../*.**
36410 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 75 61 CAPI3REF: Virtua
36420 6c 20 54 61 62 6c 65 20 49 6e 73 74 61 6e 63 65 l Table Instance
36430 20 4f 62 6a 65 63 74 20 7b 48 31 38 30 31 30 7d Object {H18010}
36440 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b 45 59 <S20400>.** KEY
36450 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 33 5f 76 WORDS: sqlite3_v
36460 74 61 62 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e tab.** EXPERIMEN
36470 54 41 4c 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 TAL.**.** Every
36480 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d [virtual table m
36490 6f 64 75 6c 65 5d 20 69 6d 70 6c 65 6d 65 6e 74 odule] implement
364a0 61 74 69 6f 6e 20 75 73 65 73 20 61 20 73 75 62 ation uses a sub
364b0 63 6c 61 73 73 0a 2a 2a 20 6f 66 20 74 68 65 20 class.** of the
364c0 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
364d0 75 72 65 20 74 6f 20 64 65 73 63 72 69 62 65 20 ure to describe
364e0 61 20 70 61 72 74 69 63 75 6c 61 72 20 69 6e 73 a particular ins
364f0 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 65 20 tance.** of the
36500 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 5d 2e [virtual table].
36510 20 20 45 61 63 68 20 73 75 62 63 6c 61 73 73 20 Each subclass
36520 77 69 6c 6c 0a 2a 2a 20 62 65 20 74 61 69 6c 6f will.** be tailo
36530 72 65 64 20 74 6f 20 74 68 65 20 73 70 65 63 69 red to the speci
36540 66 69 63 20 6e 65 65 64 73 20 6f 66 20 74 68 65 fic needs of the
36550 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e module implemen
36560 74 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 70 tation..** The p
36570 75 72 70 6f 73 65 20 6f 66 20 74 68 69 73 20 73 urpose of this s
36580 75 70 65 72 63 6c 61 73 73 20 69 73 20 74 6f 20 uperclass is to
36590 64 65 66 69 6e 65 20 63 65 72 74 61 69 6e 20 66 define certain f
365a0 69 65 6c 64 73 20 74 68 61 74 20 61 72 65 0a 2a ields that are.*
365b0 2a 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 * common to all
365c0 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 module implement
365d0 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 56 69 ations..**.** Vi
365e0 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6d 65 74 rtual tables met
365f0 68 6f 64 73 20 63 61 6e 20 73 65 74 20 61 6e 20 hods can set an
36600 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 62 79 error message by
36610 20 61 73 73 69 67 6e 69 6e 67 20 61 0a 2a 2a 20 assigning a.**
36620 73 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 string obtained
36630 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 70 from [sqlite3_mp
36640 72 69 6e 74 66 28 29 5d 20 74 6f 20 7a 45 72 72 rintf()] to zErr
36650 4d 73 67 2e 20 20 54 68 65 20 6d 65 74 68 6f 64 Msg. The method
36660 20 73 68 6f 75 6c 64 0a 2a 2a 20 74 61 6b 65 20 should.** take
36670 63 61 72 65 20 74 68 61 74 20 61 6e 79 20 70 72 care that any pr
36680 69 6f 72 20 73 74 72 69 6e 67 20 69 73 20 66 72 ior string is fr
36690 65 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f eed by a call to
366a0 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 [sqlite3_free()
366b0 5d 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 61 73 ].** prior to as
366c0 73 69 67 6e 69 6e 67 20 61 20 6e 65 77 20 73 74 signing a new st
366d0 72 69 6e 67 20 74 6f 20 7a 45 72 72 4d 73 67 2e ring to zErrMsg.
366e0 20 20 41 66 74 65 72 20 74 68 65 20 65 72 72 6f After the erro
366f0 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 73 20 r message.** is
36700 64 65 6c 69 76 65 72 65 64 20 75 70 20 74 6f 20 delivered up to
36710 74 68 65 20 63 6c 69 65 6e 74 20 61 70 70 6c 69 the client appli
36720 63 61 74 69 6f 6e 2c 20 74 68 65 20 73 74 72 69 cation, the stri
36730 6e 67 20 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d ng will be autom
36740 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 66 72 65 65 atically.** free
36750 64 20 62 79 20 73 71 6c 69 74 65 33 5f 66 72 65 d by sqlite3_fre
36760 65 28 29 20 61 6e 64 20 74 68 65 20 7a 45 72 72 e() and the zErr
36770 4d 73 67 20 66 69 65 6c 64 20 77 69 6c 6c 20 62 Msg field will b
36780 65 20 7a 65 72 6f 65 64 2e 0a 2a 2f 0a 73 74 72 e zeroed..*/.str
36790 75 63 74 20 73 71 6c 69 74 65 33 5f 76 74 61 62 uct sqlite3_vtab
367a0 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 {. const sqlit
367b0 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 e3_module *pModu
367c0 6c 65 3b 20 20 2f 2a 20 54 68 65 20 6d 6f 64 75 le; /* The modu
367d0 6c 65 20 66 6f 72 20 74 68 69 73 20 76 69 72 74 le for this virt
367e0 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 ual table */. i
367f0 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 nt nRef;
36800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
36810 2a 20 4e 4f 20 4c 4f 4e 47 45 52 20 55 53 45 44 * NO LONGER USED
36820 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 */. char *zErr
36830 4d 73 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 Msg;
36840 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d /* Error m
36850 65 73 73 61 67 65 20 66 72 6f 6d 20 73 71 6c 69 essage from sqli
36860 74 65 33 5f 6d 70 72 69 6e 74 66 28 29 20 2a 2f te3_mprintf() */
36870 0a 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 . /* Virtual ta
36880 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ble implementati
36890 6f 6e 73 20 77 69 6c 6c 20 74 79 70 69 63 61 6c ons will typical
368a0 6c 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 ly add additiona
368b0 6c 20 66 69 65 6c 64 73 20 2a 2f 0a 7d 3b 0a 0a l fields */.};..
368c0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
368d0 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 43 75 Virtual Table Cu
368e0 72 73 6f 72 20 4f 62 6a 65 63 74 20 20 7b 48 31 rsor Object {H1
368f0 38 30 32 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 8020} <S20400>.*
36900 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 * KEYWORDS: sqli
36910 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 te3_vtab_cursor
36920 7b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63 {virtual table c
36930 75 72 73 6f 72 7d 0a 2a 2a 20 45 58 50 45 52 49 ursor}.** EXPERI
36940 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 45 76 65 MENTAL.**.** Eve
36950 72 79 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c ry [virtual tabl
36960 65 20 6d 6f 64 75 6c 65 5d 20 69 6d 70 6c 65 6d e module] implem
36970 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 61 20 entation uses a
36980 73 75 62 63 6c 61 73 73 20 6f 66 20 74 68 65 0a subclass of the.
36990 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 ** following str
369a0 75 63 74 75 72 65 20 74 6f 20 64 65 73 63 72 69 ucture to descri
369b0 62 65 20 63 75 72 73 6f 72 73 20 74 68 61 74 20 be cursors that
369c0 70 6f 69 6e 74 20 69 6e 74 6f 20 74 68 65 0a 2a point into the.*
369d0 2a 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 * [virtual table
369e0 5d 20 61 6e 64 20 61 72 65 20 75 73 65 64 0a 2a ] and are used.*
369f0 2a 20 74 6f 20 6c 6f 6f 70 20 74 68 72 6f 75 67 * to loop throug
36a00 68 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 h the virtual ta
36a10 62 6c 65 2e 20 20 43 75 72 73 6f 72 73 20 61 72 ble. Cursors ar
36a20 65 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 e created using
36a30 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f the.** [sqlite3_
36a40 6d 6f 64 75 6c 65 2e 78 4f 70 65 6e 20 7c 20 78 module.xOpen | x
36a50 4f 70 65 6e 5d 20 6d 65 74 68 6f 64 20 6f 66 20 Open] method of
36a60 74 68 65 20 6d 6f 64 75 6c 65 20 61 6e 64 20 61 the module and a
36a70 72 65 20 64 65 73 74 72 6f 79 65 64 0a 2a 2a 20 re destroyed.**
36a80 62 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f by the [sqlite3_
36a90 6d 6f 64 75 6c 65 2e 78 43 6c 6f 73 65 20 7c 20 module.xClose |
36aa0 78 43 6c 6f 73 65 5d 20 6d 65 74 68 6f 64 2e 20 xClose] method.
36ab0 20 43 75 73 73 6f 72 73 20 61 72 65 20 75 73 65 Cussors are use
36ac0 64 0a 2a 2a 20 62 79 20 74 68 65 20 5b 78 46 69 d.** by the [xFi
36ad0 6c 74 65 72 5d 2c 20 5b 78 4e 65 78 74 5d 2c 20 lter], [xNext],
36ae0 5b 78 45 6f 66 5d 2c 20 5b 78 43 6f 6c 75 6d 6e [xEof], [xColumn
36af0 5d 2c 20 61 6e 64 20 5b 78 52 6f 77 69 64 5d 20 ], and [xRowid]
36b00 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 66 20 74 68 methods.** of th
36b10 65 20 6d 6f 64 75 6c 65 2e 20 20 45 61 63 68 20 e module. Each
36b20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 module implement
36b30 61 74 69 6f 6e 20 77 69 6c 6c 20 64 65 66 69 6e ation will defin
36b40 65 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 e.** the content
36b50 20 6f 66 20 61 20 63 75 72 73 6f 72 20 73 74 72 of a cursor str
36b60 75 63 74 75 72 65 20 74 6f 20 73 75 69 74 20 69 ucture to suit i
36b70 74 73 20 6f 77 6e 20 6e 65 65 64 73 2e 0a 2a 2a ts own needs..**
36b80 0a 2a 2a 20 54 68 69 73 20 73 75 70 65 72 63 6c .** This supercl
36b90 61 73 73 20 65 78 69 73 74 73 20 69 6e 20 6f 72 ass exists in or
36ba0 64 65 72 20 74 6f 20 64 65 66 69 6e 65 20 66 69 der to define fi
36bb0 65 6c 64 73 20 6f 66 20 74 68 65 20 63 75 72 73 elds of the curs
36bc0 6f 72 20 74 68 61 74 0a 2a 2a 20 61 72 65 20 63 or that.** are c
36bd0 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 69 6d 70 ommon to all imp
36be0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f lementations..*/
36bf0 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f .struct sqlite3_
36c00 76 74 61 62 5f 63 75 72 73 6f 72 20 7b 0a 20 20 vtab_cursor {.
36c10 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 sqlite3_vtab *pV
36c20 74 61 62 3b 20 20 20 20 20 20 2f 2a 20 56 69 72 tab; /* Vir
36c30 74 75 61 6c 20 74 61 62 6c 65 20 6f 66 20 74 68 tual table of th
36c40 69 73 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 2f is cursor */. /
36c50 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20 * Virtual table
36c60 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 implementations
36c70 77 69 6c 6c 20 74 79 70 69 63 61 6c 6c 79 20 61 will typically a
36c80 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 69 dd additional fi
36c90 65 6c 64 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a elds */.};../*.*
36ca0 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 63 6c * CAPI3REF: Decl
36cb0 61 72 65 20 54 68 65 20 53 63 68 65 6d 61 20 4f are The Schema O
36cc0 66 20 41 20 56 69 72 74 75 61 6c 20 54 61 62 6c f A Virtual Tabl
36cd0 65 20 7b 48 31 38 32 38 30 7d 20 3c 53 32 30 34 e {H18280} <S204
36ce0 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 00>.** EXPERIMEN
36cf0 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 78 TAL.**.** The [x
36d00 43 72 65 61 74 65 5d 20 61 6e 64 20 5b 78 43 6f Create] and [xCo
36d10 6e 6e 65 63 74 5d 20 6d 65 74 68 6f 64 73 20 6f nnect] methods o
36d20 66 20 61 0a 2a 2a 20 5b 76 69 72 74 75 61 6c 20 f a.** [virtual
36d30 74 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 20 63 61 table module] ca
36d40 6c 6c 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 ll this interfac
36d50 65 0a 2a 2a 20 74 6f 20 64 65 63 6c 61 72 65 20 e.** to declare
36d60 74 68 65 20 66 6f 72 6d 61 74 20 28 74 68 65 20 the format (the
36d70 6e 61 6d 65 73 20 61 6e 64 20 64 61 74 61 74 79 names and dataty
36d80 70 65 73 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d pes of the colum
36d90 6e 73 29 20 6f 66 0a 2a 2a 20 74 68 65 20 76 69 ns) of.** the vi
36da0 72 74 75 61 6c 20 74 61 62 6c 65 73 20 74 68 65 rtual tables the
36db0 79 20 69 6d 70 6c 65 6d 65 6e 74 2e 0a 2a 2f 0a y implement..*/.
36dc0 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 SQLITE_API SQLIT
36dd0 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 E_EXPERIMENTAL i
36de0 6e 74 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 nt sqlite3_decla
36df0 72 65 5f 76 74 61 62 28 73 71 6c 69 74 65 33 2a re_vtab(sqlite3*
36e00 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 , const char *zS
36e10 51 4c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 QL);../*.** CAPI
36e20 33 52 45 46 3a 20 4f 76 65 72 6c 6f 61 64 20 41 3REF: Overload A
36e30 20 46 75 6e 63 74 69 6f 6e 20 46 6f 72 20 41 20 Function For A
36e40 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 7b 48 Virtual Table {H
36e50 31 38 33 30 30 7d 20 3c 53 32 30 34 30 30 3e 0a 18300} <S20400>.
36e60 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a ** EXPERIMENTAL.
36e70 2a 2a 0a 2a 2a 20 56 69 72 74 75 61 6c 20 74 61 **.** Virtual ta
36e80 62 6c 65 73 20 63 61 6e 20 70 72 6f 76 69 64 65 bles can provide
36e90 20 61 6c 74 65 72 6e 61 74 69 76 65 20 69 6d 70 alternative imp
36ea0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 lementations of
36eb0 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 75 73 69 functions.** usi
36ec0 6e 67 20 74 68 65 20 5b 78 46 69 6e 64 46 75 6e ng the [xFindFun
36ed0 63 74 69 6f 6e 5d 20 6d 65 74 68 6f 64 20 6f 66 ction] method of
36ee0 20 74 68 65 20 5b 76 69 72 74 75 61 6c 20 74 61 the [virtual ta
36ef0 62 6c 65 20 6d 6f 64 75 6c 65 5d 2e 20 20 0a 2a ble module]. .*
36f00 2a 20 42 75 74 20 67 6c 6f 62 61 6c 20 76 65 72 * But global ver
36f10 73 69 6f 6e 73 20 6f 66 20 74 68 6f 73 65 20 66 sions of those f
36f20 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 6d 75 73 74 unctions.** must
36f30 20 65 78 69 73 74 20 69 6e 20 6f 72 64 65 72 20 exist in order
36f40 74 6f 20 62 65 20 6f 76 65 72 6c 6f 61 64 65 64 to be overloaded
36f50 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 ..**.** This API
36f60 20 6d 61 6b 65 73 20 73 75 72 65 20 61 20 67 6c makes sure a gl
36f70 6f 62 61 6c 20 76 65 72 73 69 6f 6e 20 6f 66 20 obal version of
36f80 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 a function with
36f90 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 a particular.**
36fa0 6e 61 6d 65 20 61 6e 64 20 6e 75 6d 62 65 72 20 name and number
36fb0 6f 66 20 70 61 72 61 6d 65 74 65 72 73 20 65 78 of parameters ex
36fc0 69 73 74 73 2e 20 20 49 66 20 6e 6f 20 73 75 63 ists. If no suc
36fd0 68 20 66 75 6e 63 74 69 6f 6e 20 65 78 69 73 74 h function exist
36fe0 73 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68 69 73 s.** before this
36ff0 20 41 50 49 20 69 73 20 63 61 6c 6c 65 64 2c 20 API is called,
37000 61 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e 20 69 a new function i
37010 73 20 63 72 65 61 74 65 64 2e 20 20 54 68 65 20 s created. The
37020 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a implementation.*
37030 2a 20 6f 66 20 74 68 65 20 6e 65 77 20 66 75 6e * of the new fun
37040 63 74 69 6f 6e 20 61 6c 77 61 79 73 20 63 61 75 ction always cau
37050 73 65 73 20 61 6e 20 65 78 63 65 70 74 69 6f 6e ses an exception
37060 20 74 6f 20 62 65 20 74 68 72 6f 77 6e 2e 20 20 to be thrown.
37070 53 6f 0a 2a 2a 20 74 68 65 20 6e 65 77 20 66 75 So.** the new fu
37080 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 67 6f nction is not go
37090 6f 64 20 66 6f 72 20 61 6e 79 74 68 69 6e 67 20 od for anything
370a0 62 79 20 69 74 73 65 6c 66 2e 20 20 49 74 73 20 by itself. Its
370b0 6f 6e 6c 79 0a 2a 2a 20 70 75 72 70 6f 73 65 20 only.** purpose
370c0 69 73 20 74 6f 20 62 65 20 61 20 70 6c 61 63 65 is to be a place
370d0 68 6f 6c 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 holder function
370e0 74 68 61 74 20 63 61 6e 20 62 65 20 6f 76 65 72 that can be over
370f0 6c 6f 61 64 65 64 0a 2a 2a 20 62 79 20 61 20 5b loaded.** by a [
37100 76 69 72 74 75 61 6c 20 74 61 62 6c 65 5d 2e 0a virtual table]..
37110 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 */.SQLITE_API SQ
37120 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 LITE_EXPERIMENTA
37130 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 76 L int sqlite3_ov
37140 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 erload_function(
37150 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 sqlite3*, const
37160 63 68 61 72 20 2a 7a 46 75 6e 63 4e 61 6d 65 2c char *zFuncName,
37170 20 69 6e 74 20 6e 41 72 67 29 3b 0a 0a 2f 2a 0a int nArg);../*.
37180 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 ** The interface
37190 20 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c 2d to the virtual-
371a0 74 61 62 6c 65 20 6d 65 63 68 61 6e 69 73 6d 20 table mechanism
371b0 64 65 66 69 6e 65 64 20 61 62 6f 76 65 20 28 62 defined above (b
371c0 61 63 6b 20 75 70 0a 2a 2a 20 74 6f 20 61 20 63 ack up.** to a c
371d0 6f 6d 6d 65 6e 74 20 72 65 6d 61 72 6b 61 62 6c omment remarkabl
371e0 79 20 73 69 6d 69 6c 61 72 20 74 6f 20 74 68 69 y similar to thi
371f0 73 20 6f 6e 65 29 20 69 73 20 63 75 72 72 65 6e s one) is curren
37200 74 6c 79 20 63 6f 6e 73 69 64 65 72 65 64 0a 2a tly considered.*
37210 2a 20 74 6f 20 62 65 20 65 78 70 65 72 69 6d 65 * to be experime
37220 6e 74 61 6c 2e 20 20 54 68 65 20 69 6e 74 65 72 ntal. The inter
37230 66 61 63 65 20 6d 69 67 68 74 20 63 68 61 6e 67 face might chang
37240 65 20 69 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c e in incompatibl
37250 65 20 77 61 79 73 2e 0a 2a 2a 20 49 66 20 74 68 e ways..** If th
37260 69 73 20 69 73 20 61 20 70 72 6f 62 6c 65 6d 20 is is a problem
37270 66 6f 72 20 79 6f 75 2c 20 64 6f 20 6e 6f 74 20 for you, do not
37280 75 73 65 20 74 68 65 20 69 6e 74 65 72 66 61 63 use the interfac
37290 65 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e 0a e at this time..
372a0 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 76 **.** When the v
372b0 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 65 63 irtual-table mec
372c0 68 61 6e 69 73 6d 20 73 74 61 62 69 6c 69 7a 65 hanism stabilize
372d0 73 2c 20 77 65 20 77 69 6c 6c 20 64 65 63 6c 61 s, we will decla
372e0 72 65 20 74 68 65 0a 2a 2a 20 69 6e 74 65 72 66 re the.** interf
372f0 61 63 65 20 66 69 78 65 64 2c 20 73 75 70 70 6f ace fixed, suppo
37300 72 74 20 69 74 20 69 6e 64 65 66 69 6e 69 74 65 rt it indefinite
37310 6c 79 2c 20 61 6e 64 20 72 65 6d 6f 76 65 20 74 ly, and remove t
37320 68 69 73 20 63 6f 6d 6d 65 6e 74 2e 0a 2a 2a 0a his comment..**.
37330 2a 2a 2a 2a 2a 2a 20 45 58 50 45 52 49 4d 45 4e ****** EXPERIMEN
37340 54 41 4c 20 2d 20 73 75 62 6a 65 63 74 20 74 6f TAL - subject to
37350 20 63 68 61 6e 67 65 20 77 69 74 68 6f 75 74 20 change without
37360 6e 6f 74 69 63 65 20 2a 2a 2a 2a 2a 2a 2a 2a 2a notice *********
37370 2a 2a 2a 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 *****.*/../*.**
37380 43 41 50 49 33 52 45 46 3a 20 41 20 48 61 6e 64 CAPI3REF: A Hand
37390 6c 65 20 54 6f 20 41 6e 20 4f 70 65 6e 20 42 4c le To An Open BL
373a0 4f 42 20 7b 48 31 37 38 30 30 7d 20 3c 53 33 30 OB {H17800} <S30
373b0 32 33 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 230>.** KEYWORDS
373c0 3a 20 7b 42 4c 4f 42 20 68 61 6e 64 6c 65 7d 20 : {BLOB handle}
373d0 7b 42 4c 4f 42 20 68 61 6e 64 6c 65 73 7d 0a 2a {BLOB handles}.*
373e0 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 *.** An instance
373f0 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 of this object
37400 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20 6f 70 represents an op
37410 65 6e 20 42 4c 4f 42 20 6f 6e 20 77 68 69 63 68 en BLOB on which
37420 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f .** [sqlite3_blo
37430 62 5f 6f 70 65 6e 20 7c 20 69 6e 63 72 65 6d 65 b_open | increme
37440 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 5d 20 63 ntal BLOB I/O] c
37450 61 6e 20 62 65 20 70 65 72 66 6f 72 6d 65 64 2e an be performed.
37460 0a 2a 2a 20 4f 62 6a 65 63 74 73 20 6f 66 20 74 .** Objects of t
37470 68 69 73 20 74 79 70 65 20 61 72 65 20 63 72 65 his type are cre
37480 61 74 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 ated by [sqlite3
37490 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 0a 2a 2a _blob_open()].**
374a0 20 61 6e 64 20 64 65 73 74 72 6f 79 65 64 20 62 and destroyed b
374b0 79 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f y [sqlite3_blob_
374c0 63 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 20 54 68 65 close()]..** The
374d0 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 [sqlite3_blob_r
374e0 65 61 64 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 ead()] and [sqli
374f0 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28 29 te3_blob_write()
37500 5d 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 ] interfaces.**
37510 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 72 can be used to r
37520 65 61 64 20 6f 72 20 77 72 69 74 65 20 73 6d 61 ead or write sma
37530 6c 6c 20 73 75 62 73 65 63 74 69 6f 6e 73 20 6f ll subsections o
37540 66 20 74 68 65 20 42 4c 4f 42 2e 0a 2a 2a 20 54 f the BLOB..** T
37550 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 he [sqlite3_blob
37560 5f 62 79 74 65 73 28 29 5d 20 69 6e 74 65 72 66 _bytes()] interf
37570 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 ace returns the
37580 73 69 7a 65 20 6f 66 20 74 68 65 20 42 4c 4f 42 size of the BLOB
37590 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a 74 79 in bytes..*/.ty
375a0 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c pedef struct sql
375b0 69 74 65 33 5f 62 6c 6f 62 20 73 71 6c 69 74 65 ite3_blob sqlite
375c0 33 5f 62 6c 6f 62 3b 0a 0a 2f 2a 0a 2a 2a 20 43 3_blob;../*.** C
375d0 41 50 49 33 52 45 46 3a 20 4f 70 65 6e 20 41 20 API3REF: Open A
375e0 42 4c 4f 42 20 46 6f 72 20 49 6e 63 72 65 6d 65 BLOB For Increme
375f0 6e 74 61 6c 20 49 2f 4f 20 7b 48 31 37 38 31 30 ntal I/O {H17810
37600 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a } <S30230>.**.**
37610 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 73 This interfaces
37620 20 6f 70 65 6e 73 20 61 20 5b 42 4c 4f 42 20 68 opens a [BLOB h
37630 61 6e 64 6c 65 20 7c 20 68 61 6e 64 6c 65 5d 20 andle | handle]
37640 74 6f 20 74 68 65 20 42 4c 4f 42 20 6c 6f 63 61 to the BLOB loca
37650 74 65 64 0a 2a 2a 20 69 6e 20 72 6f 77 20 69 52 ted.** in row iR
37660 6f 77 2c 20 63 6f 6c 75 6d 6e 20 7a 43 6f 6c 75 ow, column zColu
37670 6d 6e 2c 20 74 61 62 6c 65 20 7a 54 61 62 6c 65 mn, table zTable
37680 20 69 6e 20 64 61 74 61 62 61 73 65 20 7a 44 62 in database zDb
37690 3b 0a 2a 2a 20 69 6e 20 6f 74 68 65 72 20 77 6f ;.** in other wo
376a0 72 64 73 2c 20 74 68 65 20 73 61 6d 65 20 42 4c rds, the same BL
376b0 4f 42 20 74 68 61 74 20 77 6f 75 6c 64 20 62 65 OB that would be
376c0 20 73 65 6c 65 63 74 65 64 20 62 79 3a 0a 2a 2a selected by:.**
376d0 0a 2a 2a 20 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 .** <pre>.**
376e0 20 53 45 4c 45 43 54 20 7a 43 6f 6c 75 6d 6e 20 SELECT zColumn
376f0 46 52 4f 4d 20 7a 44 62 2e 7a 54 61 62 6c 65 20 FROM zDb.zTable
37700 57 48 45 52 45 20 5b 72 6f 77 69 64 5d 20 3d 20 WHERE [rowid] =
37710 69 52 6f 77 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 20 iRow;.** </pre>
37720 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 {END}.**.** If t
37730 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 he flags paramet
37740 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 er is non-zero,
37750 74 68 65 6e 20 74 68 65 20 42 4c 4f 42 20 69 73 then the BLOB is
37760 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 opened for read
37770 0a 2a 2a 20 61 6e 64 20 77 72 69 74 65 20 61 63 .** and write ac
37780 63 65 73 73 2e 20 49 66 20 69 74 20 69 73 20 7a cess. If it is z
37790 65 72 6f 2c 20 74 68 65 20 42 4c 4f 42 20 69 73 ero, the BLOB is
377a0 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 opened for read
377b0 20 61 63 63 65 73 73 2e 0a 2a 2a 20 49 74 20 69 access..** It i
377c0 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 s not possible t
377d0 6f 20 6f 70 65 6e 20 61 20 63 6f 6c 75 6d 6e 20 o open a column
377e0 74 68 61 74 20 69 73 20 70 61 72 74 20 6f 66 20 that is part of
377f0 61 6e 20 69 6e 64 65 78 20 6f 72 20 70 72 69 6d an index or prim
37800 61 72 79 20 0a 2a 2a 20 6b 65 79 20 66 6f 72 20 ary .** key for
37810 77 72 69 74 69 6e 67 2e 20 5e 49 66 20 5b 66 6f writing. ^If [fo
37820 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 reign key constr
37830 61 69 6e 74 73 5d 20 61 72 65 20 65 6e 61 62 6c aints] are enabl
37840 65 64 2c 20 69 74 20 69 73 20 0a 2a 2a 20 6e 6f ed, it is .** no
37850 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 6f 70 t possible to op
37860 65 6e 20 61 20 63 6f 6c 75 6d 6e 20 74 68 61 74 en a column that
37870 20 69 73 20 70 61 72 74 20 6f 66 20 61 20 5b 63 is part of a [c
37880 68 69 6c 64 20 6b 65 79 5d 20 66 6f 72 20 77 72 hild key] for wr
37890 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 iting..**.** Not
378a0 65 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62 e that the datab
378b0 61 73 65 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20 ase name is not
378c0 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 74 68 61 the filename tha
378d0 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 t contains.** th
378e0 65 20 64 61 74 61 62 61 73 65 20 62 75 74 20 72 e database but r
378f0 61 74 68 65 72 20 74 68 65 20 73 79 6d 62 6f 6c ather the symbol
37900 69 63 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 ic name of the d
37910 61 74 61 62 61 73 65 20 74 68 61 74 0a 2a 2a 20 atabase that.**
37920 69 73 20 61 73 73 69 67 6e 65 64 20 77 68 65 6e is assigned when
37930 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 the database is
37940 20 63 6f 6e 6e 65 63 74 65 64 20 75 73 69 6e 67 connected using
37950 20 5b 41 54 54 41 43 48 5d 2e 0a 2a 2a 20 46 6f [ATTACH]..** Fo
37960 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 r the main datab
37970 61 73 65 20 66 69 6c 65 2c 20 74 68 65 20 64 61 ase file, the da
37980 74 61 62 61 73 65 20 6e 61 6d 65 20 69 73 20 22 tabase name is "
37990 6d 61 69 6e 22 2e 0a 2a 2a 20 46 6f 72 20 54 45 main"..** For TE
379a0 4d 50 20 74 61 62 6c 65 73 2c 20 74 68 65 20 64 MP tables, the d
379b0 61 74 61 62 61 73 65 20 6e 61 6d 65 20 69 73 20 atabase name is
379c0 22 74 65 6d 70 22 2e 0a 2a 2a 0a 2a 2a 20 4f 6e "temp"..**.** On
379d0 20 73 75 63 63 65 73 73 2c 20 5b 53 51 4c 49 54 success, [SQLIT
379e0 45 5f 4f 4b 5d 20 69 73 20 72 65 74 75 72 6e 65 E_OK] is returne
379f0 64 20 61 6e 64 20 74 68 65 20 6e 65 77 20 5b 42 d and the new [B
37a00 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 69 73 20 77 LOB handle] is w
37a10 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20 2a 70 70 ritten.** to *pp
37a20 42 6c 6f 62 2e 20 4f 74 68 65 72 77 69 73 65 20 Blob. Otherwise
37a30 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 an [error code]
37a40 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 is returned and
37a50 2a 70 70 42 6c 6f 62 20 69 73 20 73 65 74 0a 2a *ppBlob is set.*
37a60 2a 20 74 6f 20 62 65 20 61 20 6e 75 6c 6c 20 70 * to be a null p
37a70 6f 69 6e 74 65 72 2e 0a 2a 2a 20 54 68 69 73 20 ointer..** This
37a80 66 75 6e 63 74 69 6f 6e 20 73 65 74 73 20 74 68 function sets th
37a90 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e e [database conn
37aa0 65 63 74 69 6f 6e 5d 20 65 72 72 6f 72 20 63 6f ection] error co
37ab0 64 65 20 61 6e 64 20 6d 65 73 73 61 67 65 0a 2a de and message.*
37ac0 2a 20 61 63 63 65 73 73 69 62 6c 65 20 76 69 61 * accessible via
37ad0 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 [sqlite3_errcod
37ae0 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 e()] and [sqlite
37af0 33 5f 65 72 72 6d 73 67 28 29 5d 20 61 6e 64 20 3_errmsg()] and
37b00 72 65 6c 61 74 65 64 0a 2a 2a 20 66 75 6e 63 74 related.** funct
37b10 69 6f 6e 73 2e 20 20 4e 6f 74 65 20 74 68 61 74 ions. Note that
37b20 20 74 68 65 20 2a 70 70 42 6c 6f 62 20 76 61 72 the *ppBlob var
37b30 69 61 62 6c 65 20 69 73 20 61 6c 77 61 79 73 20 iable is always
37b40 69 6e 69 74 69 61 6c 69 7a 65 64 20 69 6e 20 61 initialized in a
37b50 0a 2a 2a 20 77 61 79 20 74 68 61 74 20 6d 61 6b .** way that mak
37b60 65 73 20 69 74 20 73 61 66 65 20 74 6f 20 69 6e es it safe to in
37b70 76 6f 6b 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c voke [sqlite3_bl
37b80 6f 62 5f 63 6c 6f 73 65 28 29 5d 20 6f 6e 20 2a ob_close()] on *
37b90 70 70 42 6c 6f 62 0a 2a 2a 20 72 65 67 61 72 64 ppBlob.** regard
37ba0 6c 65 73 73 20 6f 66 20 74 68 65 20 73 75 63 63 less of the succ
37bb0 65 73 73 20 6f 72 20 66 61 69 6c 75 72 65 20 6f ess or failure o
37bc0 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a f this routine..
37bd0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 6f 77 **.** If the row
37be0 20 74 68 61 74 20 61 20 42 4c 4f 42 20 68 61 6e that a BLOB han
37bf0 64 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 69 73 dle points to is
37c00 20 6d 6f 64 69 66 69 65 64 20 62 79 20 61 6e 0a modified by an.
37c10 2a 2a 20 5b 55 50 44 41 54 45 5d 2c 20 5b 44 45 ** [UPDATE], [DE
37c20 4c 45 54 45 5d 2c 20 6f 72 20 62 79 20 5b 4f 4e LETE], or by [ON
37c30 20 43 4f 4e 46 4c 49 43 54 5d 20 73 69 64 65 2d CONFLICT] side-
37c40 65 66 66 65 63 74 73 0a 2a 2a 20 74 68 65 6e 20 effects.** then
37c50 74 68 65 20 42 4c 4f 42 20 68 61 6e 64 6c 65 20 the BLOB handle
37c60 69 73 20 6d 61 72 6b 65 64 20 61 73 20 22 65 78 is marked as "ex
37c70 70 69 72 65 64 22 2e 0a 2a 2a 20 54 68 69 73 20 pired"..** This
37c80 69 73 20 74 72 75 65 20 69 66 20 61 6e 79 20 63 is true if any c
37c90 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 6f 77 olumn of the row
37ca0 20 69 73 20 63 68 61 6e 67 65 64 2c 20 65 76 65 is changed, eve
37cb0 6e 20 61 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f 74 n a column.** ot
37cc0 68 65 72 20 74 68 61 6e 20 74 68 65 20 6f 6e 65 her than the one
37cd0 20 74 68 65 20 42 4c 4f 42 20 68 61 6e 64 6c 65 the BLOB handle
37ce0 20 69 73 20 6f 70 65 6e 20 6f 6e 2e 0a 2a 2a 20 is open on..**
37cf0 43 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 Calls to [sqlite
37d00 33 5f 62 6c 6f 62 5f 72 65 61 64 28 29 5d 20 61 3_blob_read()] a
37d10 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 nd [sqlite3_blob
37d20 5f 77 72 69 74 65 28 29 5d 20 66 6f 72 0a 2a 2a _write()] for.**
37d30 20 61 20 65 78 70 69 72 65 64 20 42 4c 4f 42 20 a expired BLOB
37d40 68 61 6e 64 6c 65 20 66 61 69 6c 20 77 69 74 68 handle fail with
37d50 20 61 6e 20 72 65 74 75 72 6e 20 63 6f 64 65 20 an return code
37d60 6f 66 20 5b 53 51 4c 49 54 45 5f 41 42 4f 52 54 of [SQLITE_ABORT
37d70 5d 2e 0a 2a 2a 20 43 68 61 6e 67 65 73 20 77 72 ]..** Changes wr
37d80 69 74 74 65 6e 20 69 6e 74 6f 20 61 20 42 4c 4f itten into a BLO
37d90 42 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 42 B prior to the B
37da0 4c 4f 42 20 65 78 70 69 72 69 6e 67 20 61 72 65 LOB expiring are
37db0 20 6e 6f 74 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b not.** rollback
37dc0 20 62 79 20 74 68 65 20 65 78 70 69 72 61 74 69 by the expirati
37dd0 6f 6e 20 6f 66 20 74 68 65 20 42 4c 4f 42 2e 20 on of the BLOB.
37de0 20 53 75 63 68 20 63 68 61 6e 67 65 73 20 77 69 Such changes wi
37df0 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79 0a 2a 2a ll eventually.**
37e00 20 63 6f 6d 6d 69 74 20 69 66 20 74 68 65 20 74 commit if the t
37e10 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 6e 74 69 ransaction conti
37e20 6e 75 65 73 20 74 6f 20 63 6f 6d 70 6c 65 74 69 nues to completi
37e30 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 on..**.** Use th
37e40 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f e [sqlite3_blob_
37e50 62 79 74 65 73 28 29 5d 20 69 6e 74 65 72 66 61 bytes()] interfa
37e60 63 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 ce to determine
37e70 74 68 65 20 73 69 7a 65 20 6f 66 0a 2a 2a 20 74 the size of.** t
37e80 68 65 20 6f 70 65 6e 65 64 20 62 6c 6f 62 2e 20 he opened blob.
37e90 20 54 68 65 20 73 69 7a 65 20 6f 66 20 61 20 62 The size of a b
37ea0 6c 6f 62 20 6d 61 79 20 6e 6f 74 20 62 65 20 63 lob may not be c
37eb0 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 0a 2a hanged by this.*
37ec0 2a 20 69 6e 74 65 72 66 61 63 65 2e 20 20 55 73 * interface. Us
37ed0 65 20 74 68 65 20 5b 55 50 44 41 54 45 5d 20 53 e the [UPDATE] S
37ee0 51 4c 20 63 6f 6d 6d 61 6e 64 20 74 6f 20 63 68 QL command to ch
37ef0 61 6e 67 65 20 74 68 65 20 73 69 7a 65 20 6f 66 ange the size of
37f00 20 61 0a 2a 2a 20 62 6c 6f 62 2e 0a 2a 2a 0a 2a a.** blob..**.*
37f10 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 * The [sqlite3_b
37f20 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 29 5d 20 ind_zeroblob()]
37f30 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 and [sqlite3_res
37f40 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 29 5d 20 ult_zeroblob()]
37f50 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 61 6e interfaces.** an
37f60 64 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 5b d the built-in [
37f70 7a 65 72 6f 62 6c 6f 62 5d 20 53 51 4c 20 66 75 zeroblob] SQL fu
37f80 6e 63 74 69 6f 6e 20 63 61 6e 20 62 65 20 75 73 nction can be us
37f90 65 64 2c 20 69 66 20 64 65 73 69 72 65 64 2c 0a ed, if desired,.
37fa0 2a 2a 20 74 6f 20 63 72 65 61 74 65 20 61 6e 20 ** to create an
37fb0 65 6d 70 74 79 2c 20 7a 65 72 6f 2d 66 69 6c 6c empty, zero-fill
37fc0 65 64 20 62 6c 6f 62 20 69 6e 20 77 68 69 63 68 ed blob in which
37fd0 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69 74 to read or writ
37fe0 65 20 75 73 69 6e 67 0a 2a 2a 20 74 68 69 73 20 e using.** this
37ff0 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a interface..**.**
38000 20 54 6f 20 61 76 6f 69 64 20 61 20 72 65 73 6f To avoid a reso
38010 75 72 63 65 20 6c 65 61 6b 2c 20 65 76 65 72 79 urce leak, every
38020 20 6f 70 65 6e 20 5b 42 4c 4f 42 20 68 61 6e 64 open [BLOB hand
38030 6c 65 5d 20 73 68 6f 75 6c 64 20 65 76 65 6e 74 le] should event
38040 75 61 6c 6c 79 0a 2a 2a 20 62 65 20 72 65 6c 65 ually.** be rele
38050 61 73 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 ased by a call t
38060 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f o [sqlite3_blob_
38070 63 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 close()]..**.**
38080 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a Requirements:.**
38090 20 5b 48 31 37 38 31 33 5d 20 5b 48 31 37 38 31 [H17813] [H1781
380a0 34 5d 20 5b 48 31 37 38 31 36 5d 20 5b 48 31 37 4] [H17816] [H17
380b0 38 31 39 5d 20 5b 48 31 37 38 32 31 5d 20 5b 48 819] [H17821] [H
380c0 31 37 38 32 34 5d 0a 2a 2f 0a 53 51 4c 49 54 45 17824].*/.SQLITE
380d0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
380e0 5f 62 6c 6f 62 5f 6f 70 65 6e 28 0a 20 20 73 71 _blob_open(. sq
380f0 6c 69 74 65 33 2a 2c 0a 20 20 63 6f 6e 73 74 20 lite3*,. const
38100 63 68 61 72 20 2a 7a 44 62 2c 0a 20 20 63 6f 6e char *zDb,. con
38110 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 2c st char *zTable,
38120 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
38130 43 6f 6c 75 6d 6e 2c 0a 20 20 73 71 6c 69 74 65 Column,. sqlite
38140 33 5f 69 6e 74 36 34 20 69 52 6f 77 2c 0a 20 20 3_int64 iRow,.
38150 69 6e 74 20 66 6c 61 67 73 2c 0a 20 20 73 71 6c int flags,. sql
38160 69 74 65 33 5f 62 6c 6f 62 20 2a 2a 70 70 42 6c ite3_blob **ppBl
38170 6f 62 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 ob.);../*.** CAP
38180 49 33 52 45 46 3a 20 43 6c 6f 73 65 20 41 20 42 I3REF: Close A B
38190 4c 4f 42 20 48 61 6e 64 6c 65 20 7b 48 31 37 38 LOB Handle {H178
381a0 33 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 30} <S30230>.**.
381b0 2a 2a 20 43 6c 6f 73 65 73 20 61 6e 20 6f 70 65 ** Closes an ope
381c0 6e 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 2e n [BLOB handle].
381d0 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 69 6e 67 20 61 .**.** Closing a
381e0 20 42 4c 4f 42 20 73 68 61 6c 6c 20 63 61 75 73 BLOB shall caus
381f0 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 e the current tr
38200 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20 63 6f 6d ansaction to com
38210 6d 69 74 0a 2a 2a 20 69 66 20 74 68 65 72 65 20 mit.** if there
38220 61 72 65 20 6e 6f 20 6f 74 68 65 72 20 42 4c 4f are no other BLO
38230 42 73 2c 20 6e 6f 20 70 65 6e 64 69 6e 67 20 70 Bs, no pending p
38240 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
38250 74 73 2c 20 61 6e 64 20 74 68 65 0a 2a 2a 20 64 ts, and the.** d
38260 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
38270 6f 6e 20 69 73 20 69 6e 20 5b 61 75 74 6f 63 6f on is in [autoco
38280 6d 6d 69 74 20 6d 6f 64 65 5d 2e 0a 2a 2a 20 49 mmit mode]..** I
38290 66 20 61 6e 79 20 77 72 69 74 65 73 20 77 65 72 f any writes wer
382a0 65 20 6d 61 64 65 20 74 6f 20 74 68 65 20 42 4c e made to the BL
382b0 4f 42 2c 20 74 68 65 79 20 6d 69 67 68 74 20 62 OB, they might b
382c0 65 20 68 65 6c 64 20 69 6e 20 63 61 63 68 65 0a e held in cache.
382d0 2a 2a 20 75 6e 74 69 6c 20 74 68 65 20 63 6c 6f ** until the clo
382e0 73 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 66 20 se operation if
382f0 74 68 65 79 20 77 69 6c 6c 20 66 69 74 2e 0a 2a they will fit..*
38300 2a 0a 2a 2a 20 43 6c 6f 73 69 6e 67 20 74 68 65 *.** Closing the
38310 20 42 4c 4f 42 20 6f 66 74 65 6e 20 66 6f 72 63 BLOB often forc
38320 65 73 20 74 68 65 20 63 68 61 6e 67 65 73 0a 2a es the changes.*
38330 2a 20 6f 75 74 20 74 6f 20 64 69 73 6b 20 61 6e * out to disk an
38340 64 20 73 6f 20 69 66 20 61 6e 79 20 49 2f 4f 20 d so if any I/O
38350 65 72 72 6f 72 73 20 6f 63 63 75 72 2c 20 74 68 errors occur, th
38360 65 79 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 6f ey will likely o
38370 63 63 75 72 0a 2a 2a 20 61 74 20 74 68 65 20 74 ccur.** at the t
38380 69 6d 65 20 77 68 65 6e 20 74 68 65 20 42 4c 4f ime when the BLO
38390 42 20 69 73 20 63 6c 6f 73 65 64 2e 20 20 41 6e B is closed. An
383a0 79 20 65 72 72 6f 72 73 20 74 68 61 74 20 6f 63 y errors that oc
383b0 63 75 72 20 64 75 72 69 6e 67 0a 2a 2a 20 63 6c cur during.** cl
383c0 6f 73 69 6e 67 20 61 72 65 20 72 65 70 6f 72 74 osing are report
383d0 65 64 20 61 73 20 61 20 6e 6f 6e 2d 7a 65 72 6f ed as a non-zero
383e0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 2e 0a 2a return value..*
383f0 2a 0a 2a 2a 20 54 68 65 20 42 4c 4f 42 20 69 73 *.** The BLOB is
38400 20 63 6c 6f 73 65 64 20 75 6e 63 6f 6e 64 69 74 closed uncondit
38410 69 6f 6e 61 6c 6c 79 2e 20 20 45 76 65 6e 20 69 ionally. Even i
38420 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 f this routine r
38430 65 74 75 72 6e 73 0a 2a 2a 20 61 6e 20 65 72 72 eturns.** an err
38440 6f 72 20 63 6f 64 65 2c 20 74 68 65 20 42 4c 4f or code, the BLO
38450 42 20 69 73 20 73 74 69 6c 6c 20 63 6c 6f 73 65 B is still close
38460 64 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e 67 d..**.** Calling
38470 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 this routine wi
38480 74 68 20 61 20 6e 75 6c 6c 20 70 6f 69 6e 74 65 th a null pointe
38490 72 20 28 77 68 69 63 68 20 61 73 20 77 6f 75 6c r (which as woul
384a0 64 20 62 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a d be returned.**
384b0 20 62 79 20 66 61 69 6c 65 64 20 63 61 6c 6c 20 by failed call
384c0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 to [sqlite3_blob
384d0 5f 6f 70 65 6e 28 29 5d 29 20 69 73 20 61 20 68 _open()]) is a h
384e0 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70 2e 0a 2a armless no-op..*
384f0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
38500 73 3a 0a 2a 2a 20 5b 48 31 37 38 33 33 5d 20 5b s:.** [H17833] [
38510 48 31 37 38 33 36 5d 20 5b 48 31 37 38 33 39 5d H17836] [H17839]
38520 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
38530 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f nt sqlite3_blob_
38540 63 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 62 6c close(sqlite3_bl
38550 6f 62 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 ob *);../*.** CA
38560 50 49 33 52 45 46 3a 20 52 65 74 75 72 6e 20 54 PI3REF: Return T
38570 68 65 20 53 69 7a 65 20 4f 66 20 41 6e 20 4f 70 he Size Of An Op
38580 65 6e 20 42 4c 4f 42 20 7b 48 31 37 38 34 30 7d en BLOB {H17840}
38590 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 <S30230>.**.**
385a0 52 65 74 75 72 6e 73 20 74 68 65 20 73 69 7a 65 Returns the size
385b0 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68 65 in bytes of the
385c0 20 42 4c 4f 42 20 61 63 63 65 73 73 69 62 6c 65 BLOB accessible
385d0 20 76 69 61 20 74 68 65 20 0a 2a 2a 20 73 75 63 via the .** suc
385e0 63 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e 65 64 cessfully opened
385f0 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 69 [BLOB handle] i
38600 6e 20 69 74 73 20 6f 6e 6c 79 20 61 72 67 75 6d n its only argum
38610 65 6e 74 2e 20 20 54 68 65 0a 2a 2a 20 69 6e 63 ent. The.** inc
38620 72 65 6d 65 6e 74 61 6c 20 62 6c 6f 62 20 49 2f remental blob I/
38630 4f 20 72 6f 75 74 69 6e 65 73 20 63 61 6e 20 6f O routines can o
38640 6e 6c 79 20 72 65 61 64 20 6f 72 20 6f 76 65 72 nly read or over
38650 77 72 69 74 69 6e 67 20 65 78 69 73 74 69 6e 67 writing existing
38660 0a 2a 2a 20 62 6c 6f 62 20 63 6f 6e 74 65 6e 74 .** blob content
38670 3b 20 74 68 65 79 20 63 61 6e 6e 6f 74 20 63 68 ; they cannot ch
38680 61 6e 67 65 20 74 68 65 20 73 69 7a 65 20 6f 66 ange the size of
38690 20 61 20 62 6c 6f 62 2e 0a 2a 2a 0a 2a 2a 20 54 a blob..**.** T
386a0 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 his routine only
386b0 20 77 6f 72 6b 73 20 6f 6e 20 61 20 5b 42 4c 4f works on a [BLO
386c0 42 20 68 61 6e 64 6c 65 5d 20 77 68 69 63 68 20 B handle] which
386d0 68 61 73 20 62 65 65 6e 20 63 72 65 61 74 65 64 has been created
386e0 0a 2a 2a 20 62 79 20 61 20 70 72 69 6f 72 20 73 .** by a prior s
386f0 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 uccessful call t
38700 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f o [sqlite3_blob_
38710 6f 70 65 6e 28 29 5d 20 61 6e 64 20 77 68 69 63 open()] and whic
38720 68 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 62 65 65 h has not.** bee
38730 6e 20 63 6c 6f 73 65 64 20 62 79 20 5b 73 71 6c n closed by [sql
38740 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 ite3_blob_close(
38750 29 5d 2e 20 20 50 61 73 73 69 6e 67 20 61 6e 79 )]. Passing any
38760 20 6f 74 68 65 72 20 70 6f 69 6e 74 65 72 20 69 other pointer i
38770 6e 0a 2a 2a 20 74 6f 20 74 68 69 73 20 72 6f 75 n.** to this rou
38780 74 69 6e 65 20 72 65 73 75 6c 74 73 20 69 6e 20 tine results in
38790 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 20 70 72 undefined and pr
387a0 6f 62 61 62 6c 79 20 75 6e 64 65 73 69 72 61 62 obably undesirab
387b0 6c 65 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a le behavior..**.
387c0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
387d0 0a 2a 2a 20 5b 48 31 37 38 34 33 5d 0a 2a 2f 0a .** [H17843].*/.
387e0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
387f0 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65 qlite3_blob_byte
38800 73 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a s(sqlite3_blob *
38810 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
38820 45 46 3a 20 52 65 61 64 20 44 61 74 61 20 46 72 EF: Read Data Fr
38830 6f 6d 20 41 20 42 4c 4f 42 20 49 6e 63 72 65 6d om A BLOB Increm
38840 65 6e 74 61 6c 6c 79 20 7b 48 31 37 38 35 30 7d entally {H17850}
38850 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 <S30230>.**.**
38860 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
38870 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 64 61 used to read da
38880 74 61 20 66 72 6f 6d 20 61 6e 20 6f 70 65 6e 20 ta from an open
38890 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 69 6e [BLOB handle] in
388a0 74 6f 20 61 0a 2a 2a 20 63 61 6c 6c 65 72 2d 73 to a.** caller-s
388b0 75 70 70 6c 69 65 64 20 62 75 66 66 65 72 2e 20 upplied buffer.
388c0 4e 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 N bytes of data
388d0 61 72 65 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 are copied into
388e0 62 75 66 66 65 72 20 5a 0a 2a 2a 20 66 72 6f 6d buffer Z.** from
388f0 20 74 68 65 20 6f 70 65 6e 20 42 4c 4f 42 2c 20 the open BLOB,
38900 73 74 61 72 74 69 6e 67 20 61 74 20 6f 66 66 73 starting at offs
38910 65 74 20 69 4f 66 66 73 65 74 2e 0a 2a 2a 0a 2a et iOffset..**.*
38920 2a 20 49 66 20 6f 66 66 73 65 74 20 69 4f 66 66 * If offset iOff
38930 73 65 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e set is less than
38940 20 4e 20 62 79 74 65 73 20 66 72 6f 6d 20 74 68 N bytes from th
38950 65 20 65 6e 64 20 6f 66 20 74 68 65 20 42 4c 4f e end of the BLO
38960 42 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 52 B,.** [SQLITE_ER
38970 52 4f 52 5d 20 69 73 20 72 65 74 75 72 6e 65 64 ROR] is returned
38980 20 61 6e 64 20 6e 6f 20 64 61 74 61 20 69 73 20 and no data is
38990 72 65 61 64 2e 20 20 49 66 20 4e 20 6f 72 20 69 read. If N or i
389a0 4f 66 66 73 65 74 20 69 73 0a 2a 2a 20 6c 65 73 Offset is.** les
389b0 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 5b 53 51 s than zero, [SQ
389c0 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 20 72 LITE_ERROR] is r
389d0 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64 eturned and no d
389e0 61 74 61 20 69 73 20 72 65 61 64 2e 0a 2a 2a 20 ata is read..**
389f0 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 The size of the
38a00 62 6c 6f 62 20 28 61 6e 64 20 68 65 6e 63 65 20 blob (and hence
38a10 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75 the maximum valu
38a20 65 20 6f 66 20 4e 2b 69 4f 66 66 73 65 74 29 0a e of N+iOffset).
38a30 2a 2a 20 63 61 6e 20 62 65 20 64 65 74 65 72 6d ** can be determ
38a40 69 6e 65 64 20 75 73 69 6e 67 20 74 68 65 20 5b ined using the [
38a50 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 sqlite3_blob_byt
38a60 65 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e es()] interface.
38a70 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 74 74 65 6d 70 .**.** An attemp
38a80 74 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 61 t to read from a
38a90 6e 20 65 78 70 69 72 65 64 20 5b 42 4c 4f 42 20 n expired [BLOB
38aa0 68 61 6e 64 6c 65 5d 20 66 61 69 6c 73 20 77 69 handle] fails wi
38ab0 74 68 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 63 th an.** error c
38ac0 6f 64 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 41 ode of [SQLITE_A
38ad0 42 4f 52 54 5d 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 BORT]..**.** On
38ae0 73 75 63 63 65 73 73 2c 20 53 51 4c 49 54 45 5f success, SQLITE_
38af0 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a OK is returned..
38b00 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e ** Otherwise, an
38b10 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f 72 [error code] or
38b20 20 61 6e 20 5b 65 78 74 65 6e 64 65 64 20 65 72 an [extended er
38b30 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 ror code] is ret
38b40 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 urned..**.** Thi
38b50 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 s routine only w
38b60 6f 72 6b 73 20 6f 6e 20 61 20 5b 42 4c 4f 42 20 orks on a [BLOB
38b70 68 61 6e 64 6c 65 5d 20 77 68 69 63 68 20 68 61 handle] which ha
38b80 73 20 62 65 65 6e 20 63 72 65 61 74 65 64 0a 2a s been created.*
38b90 2a 20 62 79 20 61 20 70 72 69 6f 72 20 73 75 63 * by a prior suc
38ba0 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 cessful call to
38bb0 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 [sqlite3_blob_op
38bc0 65 6e 28 29 5d 20 61 6e 64 20 77 68 69 63 68 20 en()] and which
38bd0 68 61 73 20 6e 6f 74 0a 2a 2a 20 62 65 65 6e 20 has not.** been
38be0 63 6c 6f 73 65 64 20 62 79 20 5b 73 71 6c 69 74 closed by [sqlit
38bf0 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 5d e3_blob_close()]
38c00 2e 20 20 50 61 73 73 69 6e 67 20 61 6e 79 20 6f . Passing any o
38c10 74 68 65 72 20 70 6f 69 6e 74 65 72 20 69 6e 0a ther pointer in.
38c20 2a 2a 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 ** to this routi
38c30 6e 65 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e ne results in un
38c40 64 65 66 69 6e 65 64 20 61 6e 64 20 70 72 6f 62 defined and prob
38c50 61 62 6c 79 20 75 6e 64 65 73 69 72 61 62 6c 65 ably undesirable
38c60 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a behavior..**.**
38c70 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 See also: [sqli
38c80 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28 29 te3_blob_write()
38c90 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 ]..**.** Require
38ca0 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 38 35 ments:.** [H1785
38cb0 33 5d 20 5b 48 31 37 38 35 36 5d 20 5b 48 31 37 3] [H17856] [H17
38cc0 38 35 39 5d 20 5b 48 31 37 38 36 32 5d 20 5b 48 859] [H17862] [H
38cd0 31 37 38 36 33 5d 20 5b 48 31 37 38 36 35 5d 20 17863] [H17865]
38ce0 5b 48 31 37 38 36 38 5d 0a 2a 2f 0a 53 51 4c 49 [H17868].*/.SQLI
38cf0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
38d00 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 73 71 6c e3_blob_read(sql
38d10 69 74 65 33 5f 62 6c 6f 62 20 2a 2c 20 76 6f 69 ite3_blob *, voi
38d20 64 20 2a 5a 2c 20 69 6e 74 20 4e 2c 20 69 6e 74 d *Z, int N, int
38d30 20 69 4f 66 66 73 65 74 29 3b 0a 0a 2f 2a 0a 2a iOffset);../*.*
38d40 2a 20 43 41 50 49 33 52 45 46 3a 20 57 72 69 74 * CAPI3REF: Writ
38d50 65 20 44 61 74 61 20 49 6e 74 6f 20 41 20 42 4c e Data Into A BL
38d60 4f 42 20 49 6e 63 72 65 6d 65 6e 74 61 6c 6c 79 OB Incrementally
38d70 20 7b 48 31 37 38 37 30 7d 20 3c 53 33 30 32 33 {H17870} <S3023
38d80 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 0>.**.** This fu
38d90 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 nction is used t
38da0 6f 20 77 72 69 74 65 20 64 61 74 61 20 69 6e 74 o write data int
38db0 6f 20 61 6e 20 6f 70 65 6e 20 5b 42 4c 4f 42 20 o an open [BLOB
38dc0 68 61 6e 64 6c 65 5d 20 66 72 6f 6d 20 61 0a 2a handle] from a.*
38dd0 2a 20 63 61 6c 6c 65 72 2d 73 75 70 70 6c 69 65 * caller-supplie
38de0 64 20 62 75 66 66 65 72 2e 20 4e 20 62 79 74 65 d buffer. N byte
38df0 73 20 6f 66 20 64 61 74 61 20 61 72 65 20 63 6f s of data are co
38e00 70 69 65 64 20 66 72 6f 6d 20 74 68 65 20 62 75 pied from the bu
38e10 66 66 65 72 20 5a 0a 2a 2a 20 69 6e 74 6f 20 74 ffer Z.** into t
38e20 68 65 20 6f 70 65 6e 20 42 4c 4f 42 2c 20 73 74 he open BLOB, st
38e30 61 72 74 69 6e 67 20 61 74 20 6f 66 66 73 65 74 arting at offset
38e40 20 69 4f 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 iOffset..**.**
38e50 49 66 20 74 68 65 20 5b 42 4c 4f 42 20 68 61 6e If the [BLOB han
38e60 64 6c 65 5d 20 70 61 73 73 65 64 20 61 73 20 74 dle] passed as t
38e70 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
38e80 74 20 77 61 73 20 6e 6f 74 20 6f 70 65 6e 65 64 t was not opened
38e90 20 66 6f 72 0a 2a 2a 20 77 72 69 74 69 6e 67 20 for.** writing
38ea0 28 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d (the flags param
38eb0 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 eter to [sqlite3
38ec0 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 20 77 61 _blob_open()] wa
38ed0 73 20 7a 65 72 6f 29 2c 0a 2a 2a 20 74 68 69 73 s zero),.** this
38ee0 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
38ef0 73 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e s [SQLITE_READON
38f00 4c 59 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 LY]..**.** This
38f10 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6f 6e 6c function may onl
38f20 79 20 6d 6f 64 69 66 79 20 74 68 65 20 63 6f 6e y modify the con
38f30 74 65 6e 74 73 20 6f 66 20 74 68 65 20 42 4c 4f tents of the BLO
38f40 42 3b 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 B; it is.** not
38f50 70 6f 73 73 69 62 6c 65 20 74 6f 20 69 6e 63 72 possible to incr
38f60 65 61 73 65 20 74 68 65 20 73 69 7a 65 20 6f 66 ease the size of
38f70 20 61 20 42 4c 4f 42 20 75 73 69 6e 67 20 74 68 a BLOB using th
38f80 69 73 20 41 50 49 2e 0a 2a 2a 20 49 66 20 6f 66 is API..** If of
38f90 66 73 65 74 20 69 4f 66 66 73 65 74 20 69 73 20 fset iOffset is
38fa0 6c 65 73 73 20 74 68 61 6e 20 4e 20 62 79 74 65 less than N byte
38fb0 73 20 66 72 6f 6d 20 74 68 65 20 65 6e 64 20 6f s from the end o
38fc0 66 20 74 68 65 20 42 4c 4f 42 2c 0a 2a 2a 20 5b f the BLOB,.** [
38fd0 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 SQLITE_ERROR] is
38fe0 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f returned and no
38ff0 20 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e data is written
39000 2e 20 20 49 66 20 4e 20 69 73 0a 2a 2a 20 6c 65 . If N is.** le
39010 73 73 20 74 68 61 6e 20 7a 65 72 6f 20 5b 53 51 ss than zero [SQ
39020 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 20 72 LITE_ERROR] is r
39030 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64 eturned and no d
39040 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 2e 0a ata is written..
39050 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74 ** The size of t
39060 68 65 20 42 4c 4f 42 20 28 61 6e 64 20 68 65 6e he BLOB (and hen
39070 63 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 ce the maximum v
39080 61 6c 75 65 20 6f 66 20 4e 2b 69 4f 66 66 73 65 alue of N+iOffse
39090 74 29 0a 2a 2a 20 63 61 6e 20 62 65 20 64 65 74 t).** can be det
390a0 65 72 6d 69 6e 65 64 20 75 73 69 6e 67 20 74 68 ermined using th
390b0 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f e [sqlite3_blob_
390c0 62 79 74 65 73 28 29 5d 20 69 6e 74 65 72 66 61 bytes()] interfa
390d0 63 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 74 74 ce..**.** An att
390e0 65 6d 70 74 20 74 6f 20 77 72 69 74 65 20 74 6f empt to write to
390f0 20 61 6e 20 65 78 70 69 72 65 64 20 5b 42 4c 4f an expired [BLO
39100 42 20 68 61 6e 64 6c 65 5d 20 66 61 69 6c 73 20 B handle] fails
39110 77 69 74 68 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 with an.** error
39120 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49 54 45 code of [SQLITE
39130 5f 41 42 4f 52 54 5d 2e 20 20 57 72 69 74 65 73 _ABORT]. Writes
39140 20 74 6f 20 74 68 65 20 42 4c 4f 42 20 74 68 61 to the BLOB tha
39150 74 20 6f 63 63 75 72 72 65 64 0a 2a 2a 20 62 65 t occurred.** be
39160 66 6f 72 65 20 74 68 65 20 5b 42 4c 4f 42 20 68 fore the [BLOB h
39170 61 6e 64 6c 65 5d 20 65 78 70 69 72 65 64 20 61 andle] expired a
39180 72 65 20 6e 6f 74 20 72 6f 6c 6c 65 64 20 62 61 re not rolled ba
39190 63 6b 20 62 79 20 74 68 65 0a 2a 2a 20 65 78 70 ck by the.** exp
391a0 69 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 68 iration of the h
391b0 61 6e 64 6c 65 2c 20 74 68 6f 75 67 68 20 6f 66 andle, though of
391c0 20 63 6f 75 72 73 65 20 74 68 6f 73 65 20 63 68 course those ch
391d0 61 6e 67 65 73 20 6d 69 67 68 74 0a 2a 2a 20 68 anges might.** h
391e0 61 76 65 20 62 65 65 6e 20 6f 76 65 72 77 72 69 ave been overwri
391f0 74 74 65 6e 20 62 79 20 74 68 65 20 73 74 61 74 tten by the stat
39200 65 6d 65 6e 74 20 74 68 61 74 20 65 78 70 69 72 ement that expir
39210 65 64 20 74 68 65 20 42 4c 4f 42 20 68 61 6e 64 ed the BLOB hand
39220 6c 65 0a 2a 2a 20 6f 72 20 62 79 20 6f 74 68 65 le.** or by othe
39230 72 20 69 6e 64 65 70 65 6e 64 65 6e 74 20 73 74 r independent st
39240 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 atements..**.**
39250 4f 6e 20 73 75 63 63 65 73 73 2c 20 53 51 4c 49 On success, SQLI
39260 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
39270 64 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c d..** Otherwise,
39280 20 61 6e 20 20 5b 65 72 72 6f 72 20 63 6f 64 65 an [error code
39290 5d 20 6f 72 20 61 6e 20 5b 65 78 74 65 6e 64 65 ] or an [extende
392a0 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 d error code] is
392b0 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a returned..**.**
392c0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e This routine on
392d0 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 61 20 5b 42 ly works on a [B
392e0 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 77 68 69 63 LOB handle] whic
392f0 68 20 68 61 73 20 62 65 65 6e 20 63 72 65 61 74 h has been creat
39300 65 64 0a 2a 2a 20 62 79 20 61 20 70 72 69 6f 72 ed.** by a prior
39310 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c successful call
39320 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f to [sqlite3_blo
39330 62 5f 6f 70 65 6e 28 29 5d 20 61 6e 64 20 77 68 b_open()] and wh
39340 69 63 68 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 62 ich has not.** b
39350 65 65 6e 20 63 6c 6f 73 65 64 20 62 79 20 5b 73 een closed by [s
39360 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 qlite3_blob_clos
39370 65 28 29 5d 2e 20 20 50 61 73 73 69 6e 67 20 61 e()]. Passing a
39380 6e 79 20 6f 74 68 65 72 20 70 6f 69 6e 74 65 72 ny other pointer
39390 20 69 6e 0a 2a 2a 20 74 6f 20 74 68 69 73 20 72 in.** to this r
393a0 6f 75 74 69 6e 65 20 72 65 73 75 6c 74 73 20 69 outine results i
393b0 6e 20 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 20 n undefined and
393c0 70 72 6f 62 61 62 6c 79 20 75 6e 64 65 73 69 72 probably undesir
393d0 61 62 6c 65 20 62 65 68 61 76 69 6f 72 2e 0a 2a able behavior..*
393e0 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b *.** See also: [
393f0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 sqlite3_blob_rea
39400 64 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 d()]..**.** Requ
39410 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
39420 37 38 37 33 5d 20 5b 48 31 37 38 37 34 5d 20 5b 7873] [H17874] [
39430 48 31 37 38 37 35 5d 20 5b 48 31 37 38 37 36 5d H17875] [H17876]
39440 20 5b 48 31 37 38 37 37 5d 20 5b 48 31 37 38 37 [H17877] [H1787
39450 39 5d 20 5b 48 31 37 38 38 32 5d 20 5b 48 31 37 9] [H17882] [H17
39460 38 38 35 5d 0a 2a 2a 20 5b 48 31 37 38 38 38 5d 885].** [H17888]
39470 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
39480 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f nt sqlite3_blob_
39490 77 72 69 74 65 28 73 71 6c 69 74 65 33 5f 62 6c write(sqlite3_bl
394a0 6f 62 20 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 ob *, const void
394b0 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 *z, int n, int
394c0 69 4f 66 66 73 65 74 29 3b 0a 0a 2f 2a 0a 2a 2a iOffset);../*.**
394d0 20 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 75 CAPI3REF: Virtu
394e0 61 6c 20 46 69 6c 65 20 53 79 73 74 65 6d 20 4f al File System O
394f0 62 6a 65 63 74 73 20 7b 48 31 31 32 30 30 7d 20 bjects {H11200}
39500 3c 53 32 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 41 <S20100>.**.** A
39510 20 76 69 72 74 75 61 6c 20 66 69 6c 65 73 79 73 virtual filesys
39520 74 65 6d 20 28 56 46 53 29 20 69 73 20 61 6e 20 tem (VFS) is an
39530 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 [sqlite3_vfs] ob
39540 6a 65 63 74 0a 2a 2a 20 74 68 61 74 20 53 51 4c ject.** that SQL
39550 69 74 65 20 75 73 65 73 20 74 6f 20 69 6e 74 65 ite uses to inte
39560 72 61 63 74 0a 2a 2a 20 77 69 74 68 20 74 68 65 ract.** with the
39570 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 underlying oper
39580 61 74 69 6e 67 20 73 79 73 74 65 6d 2e 20 20 4d ating system. M
39590 6f 73 74 20 53 51 4c 69 74 65 20 62 75 69 6c 64 ost SQLite build
395a0 73 20 63 6f 6d 65 20 77 69 74 68 20 61 0a 2a 2a s come with a.**
395b0 20 73 69 6e 67 6c 65 20 64 65 66 61 75 6c 74 20 single default
395c0 56 46 53 20 74 68 61 74 20 69 73 20 61 70 70 72 VFS that is appr
395d0 6f 70 72 69 61 74 65 20 66 6f 72 20 74 68 65 20 opriate for the
395e0 68 6f 73 74 20 63 6f 6d 70 75 74 65 72 2e 0a 2a host computer..*
395f0 2a 20 4e 65 77 20 56 46 53 65 73 20 63 61 6e 20 * New VFSes can
39600 62 65 20 72 65 67 69 73 74 65 72 65 64 20 61 6e be registered an
39610 64 20 65 78 69 73 74 69 6e 67 20 56 46 53 65 73 d existing VFSes
39620 20 63 61 6e 20 62 65 20 75 6e 72 65 67 69 73 74 can be unregist
39630 65 72 65 64 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c ered..** The fol
39640 6c 6f 77 69 6e 67 20 69 6e 74 65 72 66 61 63 65 lowing interface
39650 73 20 61 72 65 20 70 72 6f 76 69 64 65 64 2e 0a s are provided..
39660 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
39670 33 5f 76 66 73 5f 66 69 6e 64 28 29 20 69 6e 74 3_vfs_find() int
39680 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 erface returns a
39690 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 56 46 pointer to a VF
396a0 53 20 67 69 76 65 6e 20 69 74 73 20 6e 61 6d 65 S given its name
396b0 2e 0a 2a 2a 20 4e 61 6d 65 73 20 61 72 65 20 63 ..** Names are c
396c0 61 73 65 20 73 65 6e 73 69 74 69 76 65 2e 0a 2a ase sensitive..*
396d0 2a 20 4e 61 6d 65 73 20 61 72 65 20 7a 65 72 6f * Names are zero
396e0 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d -terminated UTF-
396f0 38 20 73 74 72 69 6e 67 73 2e 0a 2a 2a 20 49 66 8 strings..** If
39700 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 there is no mat
39710 63 68 2c 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 ch, a NULL point
39720 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a er is returned..
39730 2a 2a 20 49 66 20 7a 56 66 73 4e 61 6d 65 20 69 ** If zVfsName i
39740 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 s NULL then the
39750 64 65 66 61 75 6c 74 20 56 46 53 20 69 73 20 72 default VFS is r
39760 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 4e eturned..**.** N
39770 65 77 20 56 46 53 65 73 20 61 72 65 20 72 65 67 ew VFSes are reg
39780 69 73 74 65 72 65 64 20 77 69 74 68 20 73 71 6c istered with sql
39790 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65 ite3_vfs_registe
397a0 72 28 29 2e 0a 2a 2a 20 45 61 63 68 20 6e 65 77 r()..** Each new
397b0 20 56 46 53 20 62 65 63 6f 6d 65 73 20 74 68 65 VFS becomes the
397c0 20 64 65 66 61 75 6c 74 20 56 46 53 20 69 66 20 default VFS if
397d0 74 68 65 20 6d 61 6b 65 44 66 6c 74 20 66 6c 61 the makeDflt fla
397e0 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20 54 68 65 g is set..** The
397f0 20 73 61 6d 65 20 56 46 53 20 63 61 6e 20 62 65 same VFS can be
39800 20 72 65 67 69 73 74 65 72 65 64 20 6d 75 6c 74 registered mult
39810 69 70 6c 65 20 74 69 6d 65 73 20 77 69 74 68 6f iple times witho
39820 75 74 20 69 6e 6a 75 72 79 2e 0a 2a 2a 20 54 6f ut injury..** To
39830 20 6d 61 6b 65 20 61 6e 20 65 78 69 73 74 69 6e make an existin
39840 67 20 56 46 53 20 69 6e 74 6f 20 74 68 65 20 64 g VFS into the d
39850 65 66 61 75 6c 74 20 56 46 53 2c 20 72 65 67 69 efault VFS, regi
39860 73 74 65 72 20 69 74 20 61 67 61 69 6e 0a 2a 2a ster it again.**
39870 20 77 69 74 68 20 74 68 65 20 6d 61 6b 65 44 66 with the makeDf
39880 6c 74 20 66 6c 61 67 20 73 65 74 2e 20 20 49 66 lt flag set. If
39890 20 74 77 6f 20 64 69 66 66 65 72 65 6e 74 20 56 two different V
398a0 46 53 65 73 20 77 69 74 68 20 74 68 65 0a 2a 2a FSes with the.**
398b0 20 73 61 6d 65 20 6e 61 6d 65 20 61 72 65 20 72 same name are r
398c0 65 67 69 73 74 65 72 65 64 2c 20 74 68 65 20 62 egistered, the b
398d0 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 ehavior is undef
398e0 69 6e 65 64 2e 20 20 49 66 20 61 0a 2a 2a 20 56 ined. If a.** V
398f0 46 53 20 69 73 20 72 65 67 69 73 74 65 72 65 64 FS is registered
39900 20 77 69 74 68 20 61 20 6e 61 6d 65 20 74 68 61 with a name tha
39910 74 20 69 73 20 4e 55 4c 4c 20 6f 72 20 61 6e 20 t is NULL or an
39920 65 6d 70 74 79 20 73 74 72 69 6e 67 2c 0a 2a 2a empty string,.**
39930 20 74 68 65 6e 20 74 68 65 20 62 65 68 61 76 69 then the behavi
39940 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e or is undefined.
39950 0a 2a 2a 0a 2a 2a 20 55 6e 72 65 67 69 73 74 65 .**.** Unregiste
39960 72 20 61 20 56 46 53 20 77 69 74 68 20 74 68 65 r a VFS with the
39970 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 sqlite3_vfs_unr
39980 65 67 69 73 74 65 72 28 29 20 69 6e 74 65 72 66 egister() interf
39990 61 63 65 2e 0a 2a 2a 20 49 66 20 74 68 65 20 64 ace..** If the d
399a0 65 66 61 75 6c 74 20 56 46 53 20 69 73 20 75 6e efault VFS is un
399b0 72 65 67 69 73 74 65 72 65 64 2c 20 61 6e 6f 74 registered, anot
399c0 68 65 72 20 56 46 53 20 69 73 20 63 68 6f 73 65 her VFS is chose
399d0 6e 20 61 73 0a 2a 2a 20 74 68 65 20 64 65 66 61 n as.** the defa
399e0 75 6c 74 2e 20 20 54 68 65 20 63 68 6f 69 63 65 ult. The choice
399f0 20 66 6f 72 20 74 68 65 20 6e 65 77 20 56 46 53 for the new VFS
39a00 20 69 73 20 61 72 62 69 74 72 61 72 79 2e 0a 2a is arbitrary..*
39a10 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
39a20 73 3a 0a 2a 2a 20 5b 48 31 31 32 30 33 5d 20 5b s:.** [H11203] [
39a30 48 31 31 32 30 36 5d 20 5b 48 31 31 32 30 39 5d H11206] [H11209]
39a40 20 5b 48 31 31 32 31 32 5d 20 5b 48 31 31 32 31 [H11212] [H1121
39a50 35 5d 20 5b 48 31 31 32 31 38 5d 0a 2a 2f 0a 53 5] [H11218].*/.S
39a60 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 QLITE_API sqlite
39a70 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 5f 76 3_vfs *sqlite3_v
39a80 66 73 5f 66 69 6e 64 28 63 6f 6e 73 74 20 63 68 fs_find(const ch
39a90 61 72 20 2a 7a 56 66 73 4e 61 6d 65 29 3b 0a 53 ar *zVfsName);.S
39aa0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
39ab0 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74 lite3_vfs_regist
39ac0 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c er(sqlite3_vfs*,
39ad0 20 69 6e 74 20 6d 61 6b 65 44 66 6c 74 29 3b 0a int makeDflt);.
39ae0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
39af0 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 qlite3_vfs_unreg
39b00 69 73 74 65 72 28 73 71 6c 69 74 65 33 5f 76 66 ister(sqlite3_vf
39b10 73 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 s*);../*.** CAPI
39b20 33 52 45 46 3a 20 4d 75 74 65 78 65 73 20 7b 48 3REF: Mutexes {H
39b30 31 37 30 30 30 7d 20 3c 53 32 30 30 30 30 3e 0a 17000} <S20000>.
39b40 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 65 **.** The SQLite
39b50 20 63 6f 72 65 20 75 73 65 73 20 74 68 65 73 65 core uses these
39b60 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 74 68 routines for th
39b70 72 65 61 64 0a 2a 2a 20 73 79 6e 63 68 72 6f 6e read.** synchron
39b80 69 7a 61 74 69 6f 6e 2e 20 54 68 6f 75 67 68 20 ization. Though
39b90 74 68 65 79 20 61 72 65 20 69 6e 74 65 6e 64 65 they are intende
39ba0 64 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a d for internal.*
39bb0 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 2c * use by SQLite,
39bc0 20 63 6f 64 65 20 74 68 61 74 20 6c 69 6e 6b 73 code that links
39bd0 20 61 67 61 69 6e 73 74 20 53 51 4c 69 74 65 20 against SQLite
39be0 69 73 0a 2a 2a 20 70 65 72 6d 69 74 74 65 64 20 is.** permitted
39bf0 74 6f 20 75 73 65 20 61 6e 79 20 6f 66 20 74 68 to use any of th
39c00 65 73 65 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a ese routines..**
39c10 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 65 20 73 .** The SQLite s
39c20 6f 75 72 63 65 20 63 6f 64 65 20 63 6f 6e 74 61 ource code conta
39c30 69 6e 73 20 6d 75 6c 74 69 70 6c 65 20 69 6d 70 ins multiple imp
39c40 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a 20 lementations.**
39c50 6f 66 20 74 68 65 73 65 20 6d 75 74 65 78 20 72 of these mutex r
39c60 6f 75 74 69 6e 65 73 2e 20 20 41 6e 20 61 70 70 outines. An app
39c70 72 6f 70 72 69 61 74 65 20 69 6d 70 6c 65 6d 65 ropriate impleme
39c80 6e 74 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 65 ntation.** is se
39c90 6c 65 63 74 65 64 20 61 75 74 6f 6d 61 74 69 63 lected automatic
39ca0 61 6c 6c 79 20 61 74 20 63 6f 6d 70 69 6c 65 2d ally at compile-
39cb0 74 69 6d 65 2e 20 20 54 68 65 20 66 6f 6c 6c 6f time. The follo
39cc0 77 69 6e 67 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e wing.** implemen
39cd0 74 61 74 69 6f 6e 73 20 61 72 65 20 61 76 61 69 tations are avai
39ce0 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 53 51 4c lable in the SQL
39cf0 69 74 65 20 63 6f 72 65 3a 0a 2a 2a 0a 2a 2a 20 ite core:.**.**
39d00 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 <ul>.** <li> S
39d10 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 0a QLITE_MUTEX_OS2.
39d20 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54 45 ** <li> SQLITE
39d30 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 44 0a 2a _MUTEX_PTHREAD.*
39d40 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54 45 5f * <li> SQLITE_
39d50 4d 55 54 45 58 5f 57 33 32 0a 2a 2a 20 3c 6c 69 MUTEX_W32.** <li
39d60 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 > SQLITE_MUTEX
39d70 5f 4e 4f 4f 50 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a _NOOP.** </ul>.*
39d80 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f *.** The SQLITE_
39d90 4d 55 54 45 58 5f 4e 4f 4f 50 20 69 6d 70 6c 65 MUTEX_NOOP imple
39da0 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 20 73 mentation is a s
39db0 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 0a 2a et of routines.*
39dc0 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 20 72 * that does no r
39dd0 65 61 6c 20 6c 6f 63 6b 69 6e 67 20 61 6e 64 20 eal locking and
39de0 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 is appropriate f
39df0 6f 72 20 75 73 65 20 69 6e 0a 2a 2a 20 61 20 73 or use in.** a s
39e00 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20 61 ingle-threaded a
39e10 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20 54 68 65 pplication. The
39e20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 SQLITE_MUTEX_OS
39e30 32 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 2,.** SQLITE_MUT
39e40 45 58 5f 50 54 48 52 45 41 44 2c 20 61 6e 64 20 EX_PTHREAD, and
39e50 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 32 SQLITE_MUTEX_W32
39e60 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
39e70 0a 2a 2a 20 61 72 65 20 61 70 70 72 6f 70 72 69 .** are appropri
39e80 61 74 65 20 66 6f 72 20 75 73 65 20 6f 6e 20 4f ate for use on O
39e90 53 2f 32 2c 20 55 6e 69 78 2c 20 61 6e 64 20 57 S/2, Unix, and W
39ea0 69 6e 64 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 indows..**.** If
39eb0 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 SQLite is compi
39ec0 6c 65 64 20 77 69 74 68 20 74 68 65 20 53 51 4c led with the SQL
39ed0 49 54 45 5f 4d 55 54 45 58 5f 41 50 50 44 45 46 ITE_MUTEX_APPDEF
39ee0 20 70 72 65 70 72 6f 63 65 73 73 6f 72 0a 2a 2a preprocessor.**
39ef0 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 64 20 28 macro defined (
39f00 77 69 74 68 20 22 2d 44 53 51 4c 49 54 45 5f 4d with "-DSQLITE_M
39f10 55 54 45 58 5f 41 50 50 44 45 46 3d 31 22 29 2c UTEX_APPDEF=1"),
39f20 20 74 68 65 6e 20 6e 6f 20 6d 75 74 65 78 0a 2a then no mutex.*
39f30 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * implementation
39f40 20 69 73 20 69 6e 63 6c 75 64 65 64 20 77 69 74 is included wit
39f50 68 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20 49 h the library. I
39f60 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 0a n this case the.
39f70 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d ** application m
39f80 75 73 74 20 73 75 70 70 6c 79 20 61 20 63 75 73 ust supply a cus
39f90 74 6f 6d 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d tom mutex implem
39fa0 65 6e 74 61 74 69 6f 6e 20 75 73 69 6e 67 20 74 entation using t
39fb0 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f he.** [SQLITE_CO
39fc0 4e 46 49 47 5f 4d 55 54 45 58 5d 20 6f 70 74 69 NFIG_MUTEX] opti
39fd0 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 on of the sqlite
39fe0 33 5f 63 6f 6e 66 69 67 28 29 20 66 75 6e 63 74 3_config() funct
39ff0 69 6f 6e 0a 2a 2a 20 62 65 66 6f 72 65 20 63 61 ion.** before ca
3a000 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 69 6e lling sqlite3_in
3a010 69 74 69 61 6c 69 7a 65 28 29 20 6f 72 20 61 6e itialize() or an
3a020 79 20 6f 74 68 65 72 20 70 75 62 6c 69 63 20 73 y other public s
3a030 71 6c 69 74 65 33 5f 0a 2a 2a 20 66 75 6e 63 74 qlite3_.** funct
3a040 69 6f 6e 20 74 68 61 74 20 63 61 6c 6c 73 20 73 ion that calls s
3a050 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
3a060 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 e()..**.** {H170
3a070 31 31 7d 20 54 68 65 20 73 71 6c 69 74 65 33 5f 11} The sqlite3_
3a080 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 6f mutex_alloc() ro
3a090 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20 utine allocates
3a0a0 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 61 a new.** mutex a
3a0b0 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 nd returns a poi
3a0c0 6e 74 65 72 20 74 6f 20 69 74 2e 20 7b 48 31 37 nter to it. {H17
3a0d0 30 31 32 7d 20 49 66 20 69 74 20 72 65 74 75 72 012} If it retur
3a0e0 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 ns NULL.** that
3a0f0 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74 means that a mut
3a100 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 ex could not be
3a110 61 6c 6c 6f 63 61 74 65 64 2e 20 7b 48 31 37 30 allocated. {H170
3a120 31 33 7d 20 53 51 4c 69 74 65 0a 2a 2a 20 77 69 13} SQLite.** wi
3a130 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 20 73 74 ll unwind its st
3a140 61 63 6b 20 61 6e 64 20 72 65 74 75 72 6e 20 61 ack and return a
3a150 6e 20 65 72 72 6f 72 2e 20 7b 48 31 37 30 31 34 n error. {H17014
3a160 7d 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a } The argument.*
3a170 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 * to sqlite3_mut
3a180 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e ex_alloc() is on
3a190 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 e of these integ
3a1a0 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a er constants:.**
3a1b0 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e .** <ul>.** <li>
3a1c0 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 SQLITE_MUTEX_F
3a1d0 41 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c AST.** <li> SQL
3a1e0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
3a1f0 49 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c IVE.** <li> SQL
3a200 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
3a210 5f 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c 69 3e 20 _MASTER.** <li>
3a220 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 SQLITE_MUTEX_ST
3a230 41 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e ATIC_MEM.** <li>
3a240 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 SQLITE_MUTEX_S
3a250 54 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c TATIC_MEM2.** <l
3a260 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 i> SQLITE_MUTEX
3a270 5f 53 54 41 54 49 43 5f 50 52 4e 47 0a 2a 2a 20 _STATIC_PRNG.**
3a280 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
3a290 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 0a 2a 2a EX_STATIC_LRU.**
3a2a0 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 <li> SQLITE_MU
3a2b0 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 0a TEX_STATIC_LRU2.
3a2c0 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 7b ** </ul>.**.** {
3a2d0 48 31 37 30 31 35 7d 20 54 68 65 20 66 69 72 73 H17015} The firs
3a2e0 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20 t two constants
3a2f0 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 75 cause sqlite3_mu
3a300 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63 tex_alloc() to c
3a310 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d reate.** a new m
3a320 75 74 65 78 2e 20 20 54 68 65 20 6e 65 77 20 6d utex. The new m
3a330 75 74 65 78 20 69 73 20 72 65 63 75 72 73 69 76 utex is recursiv
3a340 65 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 e when SQLITE_MU
3a350 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a TEX_RECURSIVE.**
3a360 20 69 73 20 75 73 65 64 20 62 75 74 20 6e 6f 74 is used but not
3a370 20 6e 65 63 65 73 73 61 72 69 6c 79 20 73 6f 20 necessarily so
3a380 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 when SQLITE_MUTE
3a390 58 5f 46 41 53 54 20 69 73 20 75 73 65 64 2e 20 X_FAST is used.
3a3a0 7b 45 4e 44 7d 0a 2a 2a 20 54 68 65 20 6d 75 74 {END}.** The mut
3a3b0 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ex implementatio
3a3c0 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 n does not need
3a3d0 74 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e to make a distin
3a3e0 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e ction.** between
3a3f0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 SQLITE_MUTEX_RE
3a400 43 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49 CURSIVE and SQLI
3a410 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66 TE_MUTEX_FAST if
3a420 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 it does.** not
3a430 77 61 6e 74 20 74 6f 2e 20 20 7b 48 31 37 30 31 want to. {H1701
3a440 36 7d 20 42 75 74 20 53 51 4c 69 74 65 20 77 69 6} But SQLite wi
3a450 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 74 20 ll only request
3a460 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 a recursive mute
3a470 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77 68 x in.** cases wh
3a480 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20 6e 65 ere it really ne
3a490 65 64 73 20 6f 6e 65 2e 20 20 7b 45 4e 44 7d 20 eds one. {END}
3a4a0 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d If a faster non-
3a4b0 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 0a recursive mutex.
3a4c0 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** implementatio
3a4d0 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 6f n is available o
3a4e0 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74 66 n the host platf
3a4f0 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 73 orm, the mutex s
3a500 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 ubsystem.** migh
3a510 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 61 20 t return such a
3a520 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 mutex in respons
3a530 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45 e to SQLITE_MUTE
3a540 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 7b 48 X_FAST..**.** {H
3a550 31 37 30 31 37 7d 20 54 68 65 20 6f 74 68 65 72 17017} The other
3a560 20 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d 65 74 allowed paramet
3a570 65 72 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d ers to sqlite3_m
3a580 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65 61 63 utex_alloc() eac
3a590 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f h return.** a po
3a5a0 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74 69 inter to a stati
3a5b0 63 20 70 72 65 65 78 69 73 74 69 6e 67 20 6d 75 c preexisting mu
3a5c0 74 65 78 2e 20 7b 45 4e 44 7d 20 20 53 69 78 20 tex. {END} Six
3a5d0 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 61 static mutexes a
3a5e0 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 20 74 68 re.** used by th
3a5f0 65 20 63 75 72 72 65 6e 74 20 76 65 72 73 69 6f e current versio
3a600 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 46 75 n of SQLite. Fu
3a610 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 ture versions of
3a620 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 79 20 61 SQLite.** may a
3a630 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 dd additional st
3a640 61 74 69 63 20 6d 75 74 65 78 65 73 2e 20 20 53 atic mutexes. S
3a650 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 61 72 tatic mutexes ar
3a660 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a e for internal.*
3a670 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 20 * use by SQLite
3a680 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61 74 69 only. Applicati
3a690 6f 6e 73 20 74 68 61 74 20 75 73 65 20 53 51 4c ons that use SQL
3a6a0 69 74 65 20 6d 75 74 65 78 65 73 20 73 68 6f 75 ite mutexes shou
3a6b0 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 74 ld.** use only t
3a6c0 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 he dynamic mutex
3a6d0 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 53 es returned by S
3a6e0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 QLITE_MUTEX_FAST
3a6f0 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 or.** SQLITE_MU
3a700 54 45 58 5f 52 45 43 55 52 53 49 56 45 2e 0a 2a TEX_RECURSIVE..*
3a710 2a 0a 2a 2a 20 7b 48 31 37 30 31 38 7d 20 4e 6f *.** {H17018} No
3a720 74 65 20 74 68 61 74 20 69 66 20 6f 6e 65 20 6f te that if one o
3a730 66 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 f the dynamic mu
3a740 74 65 78 20 70 61 72 61 6d 65 74 65 72 73 20 28 tex parameters (
3a750 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
3a760 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4d T.** or SQLITE_M
3a770 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 20 UTEX_RECURSIVE)
3a780 69 73 20 75 73 65 64 20 74 68 65 6e 20 73 71 6c is used then sql
3a790 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
3a7a0 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 ().** returns a
3a7b0 64 69 66 66 65 72 65 6e 74 20 6d 75 74 65 78 20 different mutex
3a7c0 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e 20 20 on every call.
3a7d0 7b 48 31 37 30 33 34 7d 20 42 75 74 20 66 6f 72 {H17034} But for
3a7e0 20 74 68 65 20 73 74 61 74 69 63 0a 2a 2a 20 6d the static.** m
3a7f0 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 65 20 utex types, the
3a800 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 72 65 same mutex is re
3a810 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 79 20 turned on every
3a820 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a 2a call that has.**
3a830 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20 6e the same type n
3a840 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 umber..**.** {H1
3a850 37 30 31 39 7d 20 54 68 65 20 73 71 6c 69 74 65 7019} The sqlite
3a860 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 29 20 72 3_mutex_free() r
3a870 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 outine deallocat
3a880 65 73 20 61 20 70 72 65 76 69 6f 75 73 6c 79 0a es a previously.
3a890 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 64 79 6e ** allocated dyn
3a8a0 61 6d 69 63 20 6d 75 74 65 78 2e 20 7b 48 31 37 amic mutex. {H17
3a8b0 30 32 30 7d 20 53 51 4c 69 74 65 20 69 73 20 63 020} SQLite is c
3a8c0 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c 6c 6f areful to deallo
3a8d0 63 61 74 65 20 65 76 65 72 79 0a 2a 2a 20 64 79 cate every.** dy
3a8e0 6e 61 6d 69 63 20 6d 75 74 65 78 20 74 68 61 74 namic mutex that
3a8f0 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e 20 7b it allocates. {
3a900 41 31 37 30 32 31 7d 20 54 68 65 20 64 79 6e 61 A17021} The dyna
3a910 6d 69 63 20 6d 75 74 65 78 65 73 20 6d 75 73 74 mic mutexes must
3a920 20 6e 6f 74 20 62 65 20 69 6e 0a 2a 2a 20 75 73 not be in.** us
3a930 65 20 77 68 65 6e 20 74 68 65 79 20 61 72 65 20 e when they are
3a940 64 65 61 6c 6c 6f 63 61 74 65 64 2e 20 7b 41 31 deallocated. {A1
3a950 37 30 32 32 7d 20 41 74 74 65 6d 70 74 69 6e 67 7022} Attempting
3a960 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20 61 to deallocate a
3a970 20 73 74 61 74 69 63 0a 2a 2a 20 6d 75 74 65 78 static.** mutex
3a980 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64 65 results in unde
3a990 66 69 6e 65 64 20 62 65 68 61 76 69 6f 72 2e 20 fined behavior.
3a9a0 7b 48 31 37 30 32 33 7d 20 53 51 4c 69 74 65 20 {H17023} SQLite
3a9b0 6e 65 76 65 72 20 64 65 61 6c 6c 6f 63 61 74 65 never deallocate
3a9c0 73 0a 2a 2a 20 61 20 73 74 61 74 69 63 20 6d 75 s.** a static mu
3a9d0 74 65 78 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a tex. {END}.**.**
3a9e0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 The sqlite3_mut
3a9f0 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73 ex_enter() and s
3aa00 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 qlite3_mutex_try
3aa10 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65 () routines atte
3aa20 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 mpt.** to enter
3aa30 61 20 6d 75 74 65 78 2e 20 7b 48 31 37 30 32 34 a mutex. {H17024
3aa40 7d 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 } If another thr
3aa50 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20 77 ead is already w
3aa60 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78 2c ithin the mutex,
3aa70 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 .** sqlite3_mute
3aa80 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 62 x_enter() will b
3aa90 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 33 lock and sqlite3
3aaa0 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69 6c _mutex_try() wil
3aab0 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 l return.** SQLI
3aac0 54 45 5f 42 55 53 59 2e 20 7b 48 31 37 30 32 35 TE_BUSY. {H17025
3aad0 7d 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d } The sqlite3_m
3aae0 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72 utex_try() inter
3aaf0 66 61 63 65 20 72 65 74 75 72 6e 73 20 5b 53 51 face returns [SQ
3ab00 4c 49 54 45 5f 4f 4b 5d 0a 2a 2a 20 75 70 6f 6e LITE_OK].** upon
3ab10 20 73 75 63 63 65 73 73 66 75 6c 20 65 6e 74 72 successful entr
3ab20 79 2e 20 20 7b 48 31 37 30 32 36 7d 20 4d 75 74 y. {H17026} Mut
3ab30 65 78 65 73 20 63 72 65 61 74 65 64 20 75 73 69 exes created usi
3ab40 6e 67 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 ng.** SQLITE_MUT
3ab50 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e EX_RECURSIVE can
3ab60 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 be entered mult
3ab70 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74 68 iple times by th
3ab80 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 0a 2a e same thread..*
3ab90 2a 20 7b 48 31 37 30 32 37 7d 20 49 6e 20 73 75 * {H17027} In su
3aba0 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a 2a ch cases the,.**
3abb0 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 65 mutex must be e
3abc0 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20 6e xited an equal n
3abd0 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 62 umber of times b
3abe0 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74 68 efore another th
3abf0 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 read.** can ente
3ac00 72 2e 20 20 7b 41 31 37 30 32 38 7d 20 49 66 20 r. {A17028} If
3ac10 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 the same thread
3ac20 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61 tries to enter a
3ac30 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 6b 69 6e 64 ny other.** kind
3ac40 20 6f 66 20 6d 75 74 65 78 20 6d 6f 72 65 20 74 of mutex more t
3ac50 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 han once, the be
3ac60 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 havior is undefi
3ac70 6e 65 64 2e 0a 2a 2a 20 7b 48 31 37 30 32 39 7d ned..** {H17029}
3ac80 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 SQLite will nev
3ac90 65 72 20 65 78 68 69 62 69 74 0a 2a 2a 20 73 75 er exhibit.** su
3aca0 63 68 20 62 65 68 61 76 69 6f 72 20 69 6e 20 69 ch behavior in i
3acb0 74 73 20 6f 77 6e 20 75 73 65 20 6f 66 20 6d 75 ts own use of mu
3acc0 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d texes..**.** Som
3acd0 65 20 73 79 73 74 65 6d 73 20 28 66 6f 72 20 65 e systems (for e
3ace0 78 61 6d 70 6c 65 2c 20 57 69 6e 64 6f 77 73 20 xample, Windows
3acf0 39 35 29 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 95) do not suppo
3ad00 72 74 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e rt the operation
3ad10 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 .** implemented
3ad20 62 79 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 by sqlite3_mutex
3ad30 5f 74 72 79 28 29 2e 20 20 4f 6e 20 74 68 6f 73 _try(). On thos
3ad40 65 20 73 79 73 74 65 6d 73 2c 20 73 71 6c 69 74 e systems, sqlit
3ad50 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 0a 2a e3_mutex_try().*
3ad60 2a 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 72 65 * will always re
3ad70 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 turn SQLITE_BUSY
3ad80 2e 20 20 7b 48 31 37 30 33 30 7d 20 54 68 65 20 . {H17030} The
3ad90 53 51 4c 69 74 65 20 63 6f 72 65 20 6f 6e 6c 79 SQLite core only
3ada0 20 65 76 65 72 20 75 73 65 73 0a 2a 2a 20 73 71 ever uses.** sq
3adb0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 lite3_mutex_try(
3adc0 29 20 61 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 ) as an optimiza
3add0 74 69 6f 6e 20 73 6f 20 74 68 69 73 20 69 73 20 tion so this is
3ade0 61 63 63 65 70 74 61 62 6c 65 20 62 65 68 61 76 acceptable behav
3adf0 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 ior..**.** {H170
3ae00 33 31 7d 20 54 68 65 20 73 71 6c 69 74 65 33 5f 31} The sqlite3_
3ae10 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72 6f mutex_leave() ro
3ae20 75 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d 75 utine exits a mu
3ae30 74 65 78 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 tex that was.**
3ae40 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 previously enter
3ae50 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 ed by the same t
3ae60 68 72 65 61 64 2e 20 20 7b 41 31 37 30 33 32 7d hread. {A17032}
3ae70 20 54 68 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a The behavior.**
3ae80 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 69 66 is undefined if
3ae90 20 74 68 65 20 6d 75 74 65 78 20 69 73 20 6e 6f the mutex is no
3aea0 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 t currently ente
3aeb0 72 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 63 61 red by the.** ca
3aec0 6c 6c 69 6e 67 20 74 68 72 65 61 64 20 6f 72 20 lling thread or
3aed0 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 is not currently
3aee0 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 7b 48 31 allocated. {H1
3aef0 37 30 33 33 7d 20 53 51 4c 69 74 65 20 77 69 6c 7033} SQLite wil
3af00 6c 0a 2a 2a 20 6e 65 76 65 72 20 64 6f 20 65 69 l.** never do ei
3af10 74 68 65 72 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a ther. {END}.**.*
3af20 2a 20 49 66 20 74 68 65 20 61 72 67 75 6d 65 6e * If the argumen
3af30 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 t to sqlite3_mut
3af40 65 78 5f 65 6e 74 65 72 28 29 2c 20 73 71 6c 69 ex_enter(), sqli
3af50 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 2c te3_mutex_try(),
3af60 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d or.** sqlite3_m
3af70 75 74 65 78 5f 6c 65 61 76 65 28 29 20 69 73 20 utex_leave() is
3af80 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 a NULL pointer,
3af90 74 68 65 6e 20 61 6c 6c 20 74 68 72 65 65 20 72 then all three r
3afa0 6f 75 74 69 6e 65 73 0a 2a 2a 20 62 65 68 61 76 outines.** behav
3afb0 65 20 61 73 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a e as no-ops..**.
3afc0 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 ** See also: [sq
3afd0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
3afe0 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 ()] and [sqlite3
3aff0 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 _mutex_notheld()
3b000 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 ]..*/.SQLITE_API
3b010 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a sqlite3_mutex *
3b020 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c sqlite3_mutex_al
3b030 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 loc(int);.SQLITE
3b040 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
3b050 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 73 71 6c 3_mutex_free(sql
3b060 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 53 51 ite3_mutex*);.SQ
3b070 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
3b080 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
3b090 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a r(sqlite3_mutex*
3b0a0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
3b0b0 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f t sqlite3_mutex_
3b0c0 74 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 try(sqlite3_mute
3b0d0 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 x*);.SQLITE_API
3b0e0 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 void sqlite3_mut
3b0f0 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 33 ex_leave(sqlite3
3b100 5f 6d 75 74 65 78 2a 29 3b 0a 0a 2f 2a 0a 2a 2a _mutex*);../*.**
3b110 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65 78 CAPI3REF: Mutex
3b120 20 4d 65 74 68 6f 64 73 20 4f 62 6a 65 63 74 20 Methods Object
3b130 7b 48 31 37 31 32 30 7d 20 3c 53 32 30 31 33 30 {H17120} <S20130
3b140 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 >.** EXPERIMENTA
3b150 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 L.**.** An insta
3b160 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 nce of this stru
3b170 63 74 75 72 65 20 64 65 66 69 6e 65 73 20 74 68 cture defines th
3b180 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75 74 e low-level rout
3b190 69 6e 65 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20 ines.** used to
3b1a0 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 75 73 65 allocate and use
3b1b0 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 mutexes..**.**
3b1c0 55 73 75 61 6c 6c 79 2c 20 74 68 65 20 64 65 66 Usually, the def
3b1d0 61 75 6c 74 20 6d 75 74 65 78 20 69 6d 70 6c 65 ault mutex imple
3b1e0 6d 65 6e 74 61 74 69 6f 6e 73 20 70 72 6f 76 69 mentations provi
3b1f0 64 65 64 20 62 79 20 53 51 4c 69 74 65 20 61 72 ded by SQLite ar
3b200 65 0a 2a 2a 20 73 75 66 66 69 63 69 65 6e 74 2c e.** sufficient,
3b210 20 68 6f 77 65 76 65 72 20 74 68 65 20 75 73 65 however the use
3b220 72 20 68 61 73 20 74 68 65 20 6f 70 74 69 6f 6e r has the option
3b230 20 6f 66 20 73 75 62 73 74 69 74 75 74 69 6e 67 of substituting
3b240 20 61 20 63 75 73 74 6f 6d 0a 2a 2a 20 69 6d 70 a custom.** imp
3b250 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 lementation for
3b260 73 70 65 63 69 61 6c 69 7a 65 64 20 64 65 70 6c specialized depl
3b270 6f 79 6d 65 6e 74 73 20 6f 72 20 73 79 73 74 65 oyments or syste
3b280 6d 73 20 66 6f 72 20 77 68 69 63 68 20 53 51 4c ms for which SQL
3b290 69 74 65 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 ite.** does not
3b2a0 70 72 6f 76 69 64 65 20 61 20 73 75 69 74 61 62 provide a suitab
3b2b0 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f le implementatio
3b2c0 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c n. In this case,
3b2d0 20 74 68 65 20 75 73 65 72 0a 2a 2a 20 63 72 65 the user.** cre
3b2e0 61 74 65 73 20 61 6e 64 20 70 6f 70 75 6c 61 74 ates and populat
3b2f0 65 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f es an instance o
3b300 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 f this structure
3b310 20 74 6f 20 70 61 73 73 0a 2a 2a 20 74 6f 20 73 to pass.** to s
3b320 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 qlite3_config()
3b330 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20 5b along with the [
3b340 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 SQLITE_CONFIG_MU
3b350 54 45 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a 2a 20 TEX] option..**
3b360 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 61 6e Additionally, an
3b370 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 instance of thi
3b380 73 20 73 74 72 75 63 74 75 72 65 20 63 61 6e 20 s structure can
3b390 62 65 20 75 73 65 64 20 61 73 20 61 6e 0a 2a 2a be used as an.**
3b3a0 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c 65 output variable
3b3b0 20 77 68 65 6e 20 71 75 65 72 79 69 6e 67 20 74 when querying t
3b3c0 68 65 20 73 79 73 74 65 6d 20 66 6f 72 20 74 68 he system for th
3b3d0 65 20 63 75 72 72 65 6e 74 20 6d 75 74 65 78 0a e current mutex.
3b3e0 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** implementatio
3b3f0 6e 2c 20 75 73 69 6e 67 20 74 68 65 20 5b 53 51 n, using the [SQ
3b400 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d LITE_CONFIG_GETM
3b410 55 54 45 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a 2a UTEX] option..**
3b420 0a 2a 2a 20 54 68 65 20 78 4d 75 74 65 78 49 6e .** The xMutexIn
3b430 69 74 20 6d 65 74 68 6f 64 20 64 65 66 69 6e 65 it method define
3b440 64 20 62 79 20 74 68 69 73 20 73 74 72 75 63 74 d by this struct
3b450 75 72 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 ure is invoked a
3b460 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 73 79 73 s.** part of sys
3b470 74 65 6d 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 tem initializati
3b480 6f 6e 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 on by the sqlite
3b490 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 66 3_initialize() f
3b4a0 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 7b 48 31 37 unction..** {H17
3b4b0 30 30 31 7d 20 54 68 65 20 78 4d 75 74 65 78 49 001} The xMutexI
3b4c0 6e 69 74 20 72 6f 75 74 69 6e 65 20 73 68 61 6c nit routine shal
3b4d0 6c 20 62 65 20 63 61 6c 6c 65 64 20 62 79 20 53 l be called by S
3b4e0 51 4c 69 74 65 20 6f 6e 63 65 20 66 6f 72 20 65 QLite once for e
3b4f0 61 63 68 0a 2a 2a 20 65 66 66 65 63 74 69 76 65 ach.** effective
3b500 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 call to [sqlite
3b510 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d 2e 3_initialize()].
3b520 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 4d 75 74 65 .**.** The xMute
3b530 78 45 6e 64 20 6d 65 74 68 6f 64 20 64 65 66 69 xEnd method defi
3b540 6e 65 64 20 62 79 20 74 68 69 73 20 73 74 72 75 ned by this stru
3b550 63 74 75 72 65 20 69 73 20 69 6e 76 6f 6b 65 64 cture is invoked
3b560 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 73 as.** part of s
3b570 79 73 74 65 6d 20 73 68 75 74 64 6f 77 6e 20 62 ystem shutdown b
3b580 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 68 y the sqlite3_sh
3b590 75 74 64 6f 77 6e 28 29 20 66 75 6e 63 74 69 6f utdown() functio
3b5a0 6e 2e 20 54 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d n. The.** implem
3b5b0 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 entation of this
3b5c0 20 6d 65 74 68 6f 64 20 69 73 20 65 78 70 65 63 method is expec
3b5d0 74 65 64 20 74 6f 20 72 65 6c 65 61 73 65 20 61 ted to release a
3b5e0 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e 67 0a 2a ll outstanding.*
3b5f0 2a 20 72 65 73 6f 75 72 63 65 73 20 6f 62 74 61 * resources obta
3b600 69 6e 65 64 20 62 79 20 74 68 65 20 6d 75 74 65 ined by the mute
3b610 78 20 6d 65 74 68 6f 64 73 20 69 6d 70 6c 65 6d x methods implem
3b620 65 6e 74 61 74 69 6f 6e 2c 20 65 73 70 65 63 69 entation, especi
3b630 61 6c 6c 79 0a 2a 2a 20 74 68 6f 73 65 20 6f 62 ally.** those ob
3b640 74 61 69 6e 65 64 20 62 79 20 74 68 65 20 78 4d tained by the xM
3b650 75 74 65 78 49 6e 69 74 20 6d 65 74 68 6f 64 2e utexInit method.
3b660 20 7b 48 31 37 30 30 33 7d 20 54 68 65 20 78 4d {H17003} The xM
3b670 75 74 65 78 45 6e 64 28 29 0a 2a 2a 20 69 6e 74 utexEnd().** int
3b680 65 72 66 61 63 65 20 73 68 61 6c 6c 20 62 65 20 erface shall be
3b690 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 66 6f 72 invoked once for
3b6a0 20 65 61 63 68 20 63 61 6c 6c 20 74 6f 20 5b 73 each call to [s
3b6b0 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 qlite3_shutdown(
3b6c0 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 )]..**.** The re
3b6d0 6d 61 69 6e 69 6e 67 20 73 65 76 65 6e 20 6d 65 maining seven me
3b6e0 74 68 6f 64 73 20 64 65 66 69 6e 65 64 20 62 79 thods defined by
3b6f0 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 this structure
3b700 28 78 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a 2a 2a (xMutexAlloc,.**
3b710 20 78 4d 75 74 65 78 46 72 65 65 2c 20 78 4d 75 xMutexFree, xMu
3b720 74 65 78 45 6e 74 65 72 2c 20 78 4d 75 74 65 78 texEnter, xMutex
3b730 54 72 79 2c 20 78 4d 75 74 65 78 4c 65 61 76 65 Try, xMutexLeave
3b740 2c 20 78 4d 75 74 65 78 48 65 6c 64 20 61 6e 64 , xMutexHeld and
3b750 0a 2a 2a 20 78 4d 75 74 65 78 4e 6f 74 68 65 6c .** xMutexNothel
3b760 64 29 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 d) implement the
3b770 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 74 65 72 following inter
3b780 66 61 63 65 73 20 28 72 65 73 70 65 63 74 69 76 faces (respectiv
3b790 65 6c 79 29 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e ely):.**.** <ul>
3b7a0 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c .** <li> [sql
3b7b0 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
3b7c0 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c ()] </li>.** <
3b7d0 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 li> [sqlite3_mu
3b7e0 74 65 78 5f 66 72 65 65 28 29 5d 20 3c 2f 6c 69 tex_free()] </li
3b7f0 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 >.** <li> [sq
3b800 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
3b810 72 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 r()] </li>.**
3b820 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d <li> [sqlite3_m
3b830 75 74 65 78 5f 74 72 79 28 29 5d 20 3c 2f 6c 69 utex_try()] </li
3b840 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 >.** <li> [sq
3b850 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
3b860 65 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 e()] </li>.**
3b870 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d <li> [sqlite3_m
3b880 75 74 65 78 5f 68 65 6c 64 28 29 5d 20 3c 2f 6c utex_held()] </l
3b890 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 i>.** <li> [s
3b8a0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 qlite3_mutex_not
3b8b0 68 65 6c 64 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a held()] </li>.**
3b8c0 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 </ul>.**.** The
3b8d0 20 6f 6e 6c 79 20 64 69 66 66 65 72 65 6e 63 65 only difference
3b8e0 20 69 73 20 74 68 61 74 20 74 68 65 20 70 75 62 is that the pub
3b8f0 6c 69 63 20 73 71 6c 69 74 65 33 5f 58 58 58 20 lic sqlite3_XXX
3b900 66 75 6e 63 74 69 6f 6e 73 20 65 6e 75 6d 65 72 functions enumer
3b910 61 74 65 64 0a 2a 2a 20 61 62 6f 76 65 20 73 69 ated.** above si
3b920 6c 65 6e 74 6c 79 20 69 67 6e 6f 72 65 20 61 6e lently ignore an
3b930 79 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 74 68 y invocations th
3b940 61 74 20 70 61 73 73 20 61 20 4e 55 4c 4c 20 70 at pass a NULL p
3b950 6f 69 6e 74 65 72 20 69 6e 73 74 65 61 64 0a 2a ointer instead.*
3b960 2a 20 6f 66 20 61 20 76 61 6c 69 64 20 6d 75 74 * of a valid mut
3b970 65 78 20 68 61 6e 64 6c 65 2e 20 54 68 65 20 69 ex handle. The i
3b980 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f mplementations o
3b990 66 20 74 68 65 20 6d 65 74 68 6f 64 73 20 64 65 f the methods de
3b9a0 66 69 6e 65 64 0a 2a 2a 20 62 79 20 74 68 69 73 fined.** by this
3b9b0 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 6e structure are n
3b9c0 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 68 ot required to h
3b9d0 61 6e 64 6c 65 20 74 68 69 73 20 63 61 73 65 2c andle this case,
3b9e0 20 74 68 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20 the results.**
3b9f0 6f 66 20 70 61 73 73 69 6e 67 20 61 20 4e 55 4c of passing a NUL
3ba00 4c 20 70 6f 69 6e 74 65 72 20 69 6e 73 74 65 61 L pointer instea
3ba10 64 20 6f 66 20 61 20 76 61 6c 69 64 20 6d 75 74 d of a valid mut
3ba20 65 78 20 68 61 6e 64 6c 65 20 61 72 65 20 75 6e ex handle are un
3ba30 64 65 66 69 6e 65 64 0a 2a 2a 20 28 69 2e 65 2e defined.** (i.e.
3ba40 20 69 74 20 69 73 20 61 63 63 65 70 74 61 62 6c it is acceptabl
3ba50 65 20 74 6f 20 70 72 6f 76 69 64 65 20 61 6e 20 e to provide an
3ba60 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 implementation t
3ba70 68 61 74 20 73 65 67 66 61 75 6c 74 73 20 69 66 hat segfaults if
3ba80 0a 2a 2a 20 69 74 20 69 73 20 70 61 73 73 65 64 .** it is passed
3ba90 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 29 a NULL pointer)
3baa0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 4d 75 74 ..**.** The xMut
3bab0 65 78 49 6e 69 74 28 29 20 6d 65 74 68 6f 64 20 exInit() method
3bac0 6d 75 73 74 20 62 65 20 74 68 72 65 61 64 73 61 must be threadsa
3bad0 66 65 2e 20 20 49 74 20 6d 75 73 74 20 62 65 20 fe. It must be
3bae0 68 61 72 6d 6c 65 73 73 20 74 6f 0a 2a 2a 20 69 harmless to.** i
3baf0 6e 76 6f 6b 65 20 78 4d 75 74 65 78 49 6e 69 74 nvoke xMutexInit
3bb00 28 29 20 6d 75 74 69 70 6c 65 20 74 69 6d 65 73 () mutiple times
3bb10 20 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65 within the same
3bb20 20 70 72 6f 63 65 73 73 20 61 6e 64 20 77 69 74 process and wit
3bb30 68 6f 75 74 0a 2a 2a 20 69 6e 74 65 72 76 65 6e hout.** interven
3bb40 69 6e 67 20 63 61 6c 6c 73 20 74 6f 20 78 4d 75 ing calls to xMu
3bb50 74 65 78 45 6e 64 28 29 2e 20 20 53 65 63 6f 6e texEnd(). Secon
3bb60 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 6e 74 d and subsequent
3bb70 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 78 4d 75 calls to.** xMu
3bb80 74 65 78 49 6e 69 74 28 29 20 6d 75 73 74 20 62 texInit() must b
3bb90 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 e no-ops..**.**
3bba0 78 4d 75 74 65 78 49 6e 69 74 28 29 20 6d 75 73 xMutexInit() mus
3bbb0 74 20 6e 6f 74 20 75 73 65 20 53 51 4c 69 74 65 t not use SQLite
3bbc0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
3bbd0 6f 6e 20 28 5b 73 71 6c 69 74 65 33 5f 6d 61 6c on ([sqlite3_mal
3bbe0 6c 6f 63 28 29 5d 0a 2a 2a 20 61 6e 64 20 69 74 loc()].** and it
3bbf0 73 20 61 73 73 6f 63 69 61 74 65 73 29 2e 20 20 s associates).
3bc00 53 69 6d 69 6c 61 72 6c 79 2c 20 78 4d 75 74 65 Similarly, xMute
3bc10 78 41 6c 6c 6f 63 28 29 20 6d 75 73 74 20 6e 6f xAlloc() must no
3bc20 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d 65 6d t use SQLite mem
3bc30 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f ory.** allocatio
3bc40 6e 20 66 6f 72 20 61 20 73 74 61 74 69 63 20 6d n for a static m
3bc50 75 74 65 78 2e 20 20 48 6f 77 65 76 65 72 20 78 utex. However x
3bc60 4d 75 74 65 78 41 6c 6c 6f 63 28 29 20 6d 61 79 MutexAlloc() may
3bc70 20 75 73 65 20 53 51 4c 69 74 65 0a 2a 2a 20 6d use SQLite.** m
3bc80 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
3bc90 20 66 6f 72 20 61 20 66 61 73 74 20 6f 72 20 72 for a fast or r
3bca0 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 2e 0a ecursive mutex..
3bcb0 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c **.** SQLite wil
3bcc0 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20 78 4d 75 l invoke the xMu
3bcd0 74 65 78 45 6e 64 28 29 20 6d 65 74 68 6f 64 20 texEnd() method
3bce0 77 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 73 68 when [sqlite3_sh
3bcf0 75 74 64 6f 77 6e 28 29 5d 20 69 73 0a 2a 2a 20 utdown()] is.**
3bd00 63 61 6c 6c 65 64 2c 20 62 75 74 20 6f 6e 6c 79 called, but only
3bd10 20 69 66 20 74 68 65 20 70 72 69 6f 72 20 63 61 if the prior ca
3bd20 6c 6c 20 74 6f 20 78 4d 75 74 65 78 49 6e 69 74 ll to xMutexInit
3bd30 20 72 65 74 75 72 6e 65 64 20 53 51 4c 49 54 45 returned SQLITE
3bd40 5f 4f 4b 2e 0a 2a 2a 20 49 66 20 78 4d 75 74 65 _OK..** If xMute
3bd50 78 49 6e 69 74 20 66 61 69 6c 73 20 69 6e 20 61 xInit fails in a
3bd60 6e 79 20 77 61 79 2c 20 69 74 20 69 73 20 65 78 ny way, it is ex
3bd70 70 65 63 74 65 64 20 74 6f 20 63 6c 65 61 6e 20 pected to clean
3bd80 75 70 20 61 66 74 65 72 20 69 74 73 65 6c 66 0a up after itself.
3bd90 2a 2a 20 70 72 69 6f 72 20 74 6f 20 72 65 74 75 ** prior to retu
3bda0 72 6e 69 6e 67 2e 0a 2a 2f 0a 74 79 70 65 64 65 rning..*/.typede
3bdb0 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 f struct sqlite3
3bdc0 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 _mutex_methods s
3bdd0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 qlite3_mutex_met
3bde0 68 6f 64 73 3b 0a 73 74 72 75 63 74 20 73 71 6c hods;.struct sql
3bdf0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f ite3_mutex_metho
3be00 64 73 20 7b 0a 20 20 69 6e 74 20 28 2a 78 4d 75 ds {. int (*xMu
3be10 74 65 78 49 6e 69 74 29 28 76 6f 69 64 29 3b 0a texInit)(void);.
3be20 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 45 6e int (*xMutexEn
3be30 64 29 28 76 6f 69 64 29 3b 0a 20 20 73 71 6c 69 d)(void);. sqli
3be40 74 65 33 5f 6d 75 74 65 78 20 2a 28 2a 78 4d 75 te3_mutex *(*xMu
3be50 74 65 78 41 6c 6c 6f 63 29 28 69 6e 74 29 3b 0a texAlloc)(int);.
3be60 20 20 76 6f 69 64 20 28 2a 78 4d 75 74 65 78 46 void (*xMutexF
3be70 72 65 65 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 ree)(sqlite3_mut
3be80 65 78 20 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a ex *);. void (*
3be90 78 4d 75 74 65 78 45 6e 74 65 72 29 28 73 71 6c xMutexEnter)(sql
3bea0 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 ite3_mutex *);.
3beb0 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 54 72 79 int (*xMutexTry
3bec0 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 )(sqlite3_mutex
3bed0 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 4d 75 *);. void (*xMu
3bee0 74 65 78 4c 65 61 76 65 29 28 73 71 6c 69 74 65 texLeave)(sqlite
3bef0 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 20 69 6e 3_mutex *);. in
3bf00 74 20 28 2a 78 4d 75 74 65 78 48 65 6c 64 29 28 t (*xMutexHeld)(
3bf10 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 sqlite3_mutex *)
3bf20 3b 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 ;. int (*xMutex
3bf30 4e 6f 74 68 65 6c 64 29 28 73 71 6c 69 74 65 33 Notheld)(sqlite3
3bf40 5f 6d 75 74 65 78 20 2a 29 3b 0a 7d 3b 0a 0a 2f _mutex *);.};../
3bf50 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4d *.** CAPI3REF: M
3bf60 75 74 65 78 20 56 65 72 69 66 69 63 61 74 69 6f utex Verificatio
3bf70 6e 20 52 6f 75 74 69 6e 65 73 20 7b 48 31 37 30 n Routines {H170
3bf80 38 30 7d 20 3c 53 32 30 31 33 30 3e 20 3c 53 33 80} <S20130> <S3
3bf90 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 0800>.**.** The
3bfa0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
3bfb0 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 ld() and sqlite3
3bfc0 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 _mutex_notheld()
3bfd0 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 61 72 65 routines.** are
3bfe0 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 intended for us
3bff0 65 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 e inside assert(
3c000 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 7b 48 ) statements. {H
3c010 31 37 30 38 31 7d 20 54 68 65 20 53 51 4c 69 74 17081} The SQLit
3c020 65 20 63 6f 72 65 0a 2a 2a 20 6e 65 76 65 72 20 e core.** never
3c030 75 73 65 73 20 74 68 65 73 65 20 72 6f 75 74 69 uses these routi
3c040 6e 65 73 20 65 78 63 65 70 74 20 69 6e 73 69 64 nes except insid
3c050 65 20 61 6e 20 61 73 73 65 72 74 28 29 20 61 6e e an assert() an
3c060 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a d applications.*
3c070 2a 20 61 72 65 20 61 64 76 69 73 65 64 20 74 6f * are advised to
3c080 20 66 6f 6c 6c 6f 77 20 74 68 65 20 6c 65 61 64 follow the lead
3c090 20 6f 66 20 74 68 65 20 63 6f 72 65 2e 20 20 7b of the core. {
3c0a0 48 31 37 30 38 32 7d 20 54 68 65 20 63 6f 72 65 H17082} The core
3c0b0 20 6f 6e 6c 79 0a 2a 2a 20 70 72 6f 76 69 64 65 only.** provide
3c0c0 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e s implementation
3c0d0 73 20 66 6f 72 20 74 68 65 73 65 20 72 6f 75 74 s for these rout
3c0e0 69 6e 65 73 20 77 68 65 6e 20 69 74 20 69 73 20 ines when it is
3c0f0 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 77 69 74 68 compiled.** with
3c100 20 74 68 65 20 53 51 4c 49 54 45 5f 44 45 42 55 the SQLITE_DEBU
3c110 47 20 66 6c 61 67 2e 20 20 7b 41 31 37 30 38 37 G flag. {A17087
3c120 7d 20 45 78 74 65 72 6e 61 6c 20 6d 75 74 65 78 } External mutex
3c130 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
3c140 0a 2a 2a 20 61 72 65 20 6f 6e 6c 79 20 72 65 71 .** are only req
3c150 75 69 72 65 64 20 74 6f 20 70 72 6f 76 69 64 65 uired to provide
3c160 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 these routines
3c170 69 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 if SQLITE_DEBUG
3c180 69 73 0a 2a 2a 20 64 65 66 69 6e 65 64 20 61 6e is.** defined an
3c190 64 20 69 66 20 4e 44 45 42 55 47 20 69 73 20 6e d if NDEBUG is n
3c1a0 6f 74 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a ot defined..**.*
3c1b0 2a 20 7b 48 31 37 30 38 33 7d 20 54 68 65 73 65 * {H17083} These
3c1c0 20 72 6f 75 74 69 6e 65 73 20 73 68 6f 75 6c 64 routines should
3c1d0 20 72 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 return true if
3c1e0 74 68 65 20 6d 75 74 65 78 20 69 6e 20 74 68 65 the mutex in the
3c1f0 69 72 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 ir argument.** i
3c200 73 20 68 65 6c 64 20 6f 72 20 6e 6f 74 20 68 65 s held or not he
3c210 6c 64 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 ld, respectively
3c220 2c 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 , by the calling
3c230 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 7b thread..**.** {
3c240 58 31 37 30 38 34 7d 20 54 68 65 20 69 6d 70 6c X17084} The impl
3c250 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 6e 6f ementation is no
3c260 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 70 72 t required to pr
3c270 6f 76 69 64 65 64 20 76 65 72 73 69 6f 6e 73 20 ovided versions
3c280 6f 66 20 74 68 65 73 65 0a 2a 2a 20 72 6f 75 74 of these.** rout
3c290 69 6e 65 73 20 74 68 61 74 20 61 63 74 75 61 6c ines that actual
3c2a0 6c 79 20 77 6f 72 6b 2e 20 49 66 20 74 68 65 20 ly work. If the
3c2b0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 implementation d
3c2c0 6f 65 73 20 6e 6f 74 20 70 72 6f 76 69 64 65 20 oes not provide
3c2d0 77 6f 72 6b 69 6e 67 0a 2a 2a 20 76 65 72 73 69 working.** versi
3c2e0 6f 6e 73 20 6f 66 20 74 68 65 73 65 20 72 6f 75 ons of these rou
3c2f0 74 69 6e 65 73 2c 20 69 74 20 73 68 6f 75 6c 64 tines, it should
3c300 20 61 74 20 6c 65 61 73 74 20 70 72 6f 76 69 64 at least provid
3c310 65 20 73 74 75 62 73 20 74 68 61 74 20 61 6c 77 e stubs that alw
3c320 61 79 73 0a 2a 2a 20 72 65 74 75 72 6e 20 74 72 ays.** return tr
3c330 75 65 20 73 6f 20 74 68 61 74 20 6f 6e 65 20 64 ue so that one d
3c340 6f 65 73 20 6e 6f 74 20 67 65 74 20 73 70 75 72 oes not get spur
3c350 69 6f 75 73 20 61 73 73 65 72 74 69 6f 6e 20 66 ious assertion f
3c360 61 69 6c 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b ailures..**.** {
3c370 48 31 37 30 38 35 7d 20 49 66 20 74 68 65 20 61 H17085} If the a
3c380 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 rgument to sqlit
3c390 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 e3_mutex_held()
3c3a0 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 is a NULL pointe
3c3b0 72 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 72 6f r then.** the ro
3c3c0 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 72 65 74 utine should ret
3c3d0 75 72 6e 20 31 2e 20 20 7b 45 4e 44 7d 20 54 68 urn 1. {END} Th
3c3e0 69 73 20 73 65 65 6d 73 20 63 6f 75 6e 74 65 72 is seems counter
3c3f0 2d 69 6e 74 75 69 74 69 76 65 20 73 69 6e 63 65 -intuitive since
3c400 0a 2a 2a 20 63 6c 65 61 72 6c 79 20 74 68 65 20 .** clearly the
3c410 6d 75 74 65 78 20 63 61 6e 6e 6f 74 20 62 65 20 mutex cannot be
3c420 68 65 6c 64 20 69 66 20 69 74 20 64 6f 65 73 20 held if it does
3c430 6e 6f 74 20 65 78 69 73 74 2e 20 20 42 75 74 20 not exist. But
3c440 74 68 65 0a 2a 2a 20 74 68 65 20 72 65 61 73 6f the.** the reaso
3c450 6e 20 74 68 65 20 6d 75 74 65 78 20 64 6f 65 73 n the mutex does
3c460 20 6e 6f 74 20 65 78 69 73 74 20 69 73 20 62 65 not exist is be
3c470 63 61 75 73 65 20 74 68 65 20 62 75 69 6c 64 20 cause the build
3c480 69 73 20 6e 6f 74 0a 2a 2a 20 75 73 69 6e 67 20 is not.** using
3c490 6d 75 74 65 78 65 73 2e 20 20 41 6e 64 20 77 65 mutexes. And we
3c4a0 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 68 65 do not want the
3c4b0 20 61 73 73 65 72 74 28 29 20 63 6f 6e 74 61 69 assert() contai
3c4c0 6e 69 6e 67 20 74 68 65 0a 2a 2a 20 63 61 6c 6c ning the.** call
3c4d0 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 to sqlite3_mute
3c4e0 78 5f 68 65 6c 64 28 29 20 74 6f 20 66 61 69 6c x_held() to fail
3c4f0 2c 20 73 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 , so a non-zero
3c500 72 65 74 75 72 6e 20 69 73 0a 2a 2a 20 74 68 65 return is.** the
3c510 20 61 70 70 72 6f 70 72 69 61 74 65 20 74 68 69 appropriate thi
3c520 6e 67 20 74 6f 20 64 6f 2e 20 20 7b 48 31 37 30 ng to do. {H170
3c530 38 36 7d 20 54 68 65 20 73 71 6c 69 74 65 33 5f 86} The sqlite3_
3c540 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 0a mutex_notheld().
3c550 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 73 68 6f ** interface sho
3c560 75 6c 64 20 61 6c 73 6f 20 72 65 74 75 72 6e 20 uld also return
3c570 31 20 77 68 65 6e 20 67 69 76 65 6e 20 61 20 4e 1 when given a N
3c580 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a ULL pointer..*/.
3c590 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
3c5a0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
3c5b0 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a d(sqlite3_mutex*
3c5c0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
3c5d0 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f t sqlite3_mutex_
3c5e0 6e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f notheld(sqlite3_
3c5f0 6d 75 74 65 78 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 mutex*);../*.**
3c600 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65 78 20 CAPI3REF: Mutex
3c610 54 79 70 65 73 20 7b 48 31 37 30 30 31 7d 20 3c Types {H17001} <
3c620 48 31 37 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 H17000>.**.** Th
3c630 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 e [sqlite3_mutex
3c640 5f 61 6c 6c 6f 63 28 29 5d 20 69 6e 74 65 72 66 _alloc()] interf
3c650 61 63 65 20 74 61 6b 65 73 20 61 20 73 69 6e 67 ace takes a sing
3c660 6c 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 77 le argument.** w
3c670 68 69 63 68 20 69 73 20 6f 6e 65 20 6f 66 20 74 hich is one of t
3c680 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e hese integer con
3c690 73 74 61 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 stants..**.** Th
3c6a0 65 20 73 65 74 20 6f 66 20 73 74 61 74 69 63 20 e set of static
3c6b0 6d 75 74 65 78 65 73 20 6d 61 79 20 63 68 61 6e mutexes may chan
3c6c0 67 65 20 66 72 6f 6d 20 6f 6e 65 20 53 51 4c 69 ge from one SQLi
3c6d0 74 65 20 72 65 6c 65 61 73 65 20 74 6f 20 74 68 te release to th
3c6e0 65 0a 2a 2a 20 6e 65 78 74 2e 20 20 41 70 70 6c e.** next. Appl
3c6f0 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6f 76 ications that ov
3c700 65 72 72 69 64 65 20 74 68 65 20 62 75 69 6c 74 erride the built
3c710 2d 69 6e 20 6d 75 74 65 78 20 6c 6f 67 69 63 20 -in mutex logic
3c720 6d 75 73 74 20 62 65 0a 2a 2a 20 70 72 65 70 61 must be.** prepa
3c730 72 65 64 20 74 6f 20 61 63 63 6f 6d 6d 6f 64 61 red to accommoda
3c740 74 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 te additional st
3c750 61 74 69 63 20 6d 75 74 65 78 65 73 2e 0a 2a 2f atic mutexes..*/
3c760 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
3c770 4d 55 54 45 58 5f 46 41 53 54 20 20 20 20 20 20 MUTEX_FAST
3c780 20 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 0.#define
3c790 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 SQLITE_MUTEX_RE
3c7a0 43 55 52 53 49 56 45 20 20 20 20 20 20 20 20 31 CURSIVE 1
3c7b0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
3c7c0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 MUTEX_STATIC_MAS
3c7d0 54 45 52 20 20 20 20 32 0a 23 64 65 66 69 6e 65 TER 2.#define
3c7e0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 SQLITE_MUTEX_ST
3c7f0 41 54 49 43 5f 4d 45 4d 20 20 20 20 20 20 20 33 ATIC_MEM 3
3c800 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c /* sqlite3_mal
3c810 6c 6f 63 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 loc() */.#define
3c820 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 SQLITE_MUTEX_ST
3c830 41 54 49 43 5f 4d 45 4d 32 20 20 20 20 20 20 34 ATIC_MEM2 4
3c840 20 20 2f 2a 20 4e 4f 54 20 55 53 45 44 20 2a 2f /* NOT USED */
3c850 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
3c860 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4f 50 45 MUTEX_STATIC_OPE
3c870 4e 20 20 20 20 20 20 34 20 20 2f 2a 20 73 71 6c N 4 /* sql
3c880 69 74 65 33 42 74 72 65 65 4f 70 65 6e 28 29 20 ite3BtreeOpen()
3c890 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
3c8a0 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 50 E_MUTEX_STATIC_P
3c8b0 52 4e 47 20 20 20 20 20 20 35 20 20 2f 2a 20 73 RNG 5 /* s
3c8c0 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 28 29 20 qlite3_random()
3c8d0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
3c8e0 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c E_MUTEX_STATIC_L
3c8f0 52 55 20 20 20 20 20 20 20 36 20 20 2f 2a 20 6c RU 6 /* l
3c900 72 75 20 70 61 67 65 20 6c 69 73 74 20 2a 2f 0a ru page list */.
3c910 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d #define SQLITE_M
3c920 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 UTEX_STATIC_LRU2
3c930 20 20 20 20 20 20 37 20 20 2f 2a 20 6c 72 75 20 7 /* lru
3c940 70 61 67 65 20 6c 69 73 74 20 2a 2f 0a 0a 2f 2a page list */../*
3c950 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 .** CAPI3REF: Re
3c960 74 72 69 65 76 65 20 74 68 65 20 6d 75 74 65 78 trieve the mutex
3c970 20 66 6f 72 20 61 20 64 61 74 61 62 61 73 65 20 for a database
3c980 63 6f 6e 6e 65 63 74 69 6f 6e 20 7b 48 31 37 30 connection {H170
3c990 30 32 7d 20 3c 48 31 37 30 30 30 3e 0a 2a 2a 0a 02} <H17000>.**.
3c9a0 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 ** This interfac
3c9b0 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e e returns a poin
3c9c0 74 65 72 20 74 68 65 20 5b 73 71 6c 69 74 65 33 ter the [sqlite3
3c9d0 5f 6d 75 74 65 78 5d 20 6f 62 6a 65 63 74 20 74 _mutex] object t
3c9e0 68 61 74 20 0a 2a 2a 20 73 65 72 69 61 6c 69 7a hat .** serializ
3c9f0 65 73 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 es access to the
3ca00 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
3ca10 63 74 69 6f 6e 5d 20 67 69 76 65 6e 20 69 6e 20 ction] given in
3ca20 74 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 the argument.**
3ca30 77 68 65 6e 20 74 68 65 20 5b 74 68 72 65 61 64 when the [thread
3ca40 69 6e 67 20 6d 6f 64 65 5d 20 69 73 20 53 65 72 ing mode] is Ser
3ca50 69 61 6c 69 7a 65 64 2e 0a 2a 2a 20 49 66 20 74 ialized..** If t
3ca60 68 65 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f he [threading mo
3ca70 64 65 5d 20 69 73 20 53 69 6e 67 6c 65 2d 74 68 de] is Single-th
3ca80 72 65 61 64 20 6f 72 20 4d 75 6c 74 69 2d 74 68 read or Multi-th
3ca90 72 65 61 64 20 74 68 65 6e 20 74 68 69 73 0a 2a read then this.*
3caa0 2a 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e * routine return
3cab0 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 s a NULL pointer
3cac0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
3cad0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 sqlite3_mutex *s
3cae0 71 6c 69 74 65 33 5f 64 62 5f 6d 75 74 65 78 28 qlite3_db_mutex(
3caf0 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a sqlite3*);../*.*
3cb00 2a 20 43 41 50 49 33 52 45 46 3a 20 4c 6f 77 2d * CAPI3REF: Low-
3cb10 4c 65 76 65 6c 20 43 6f 6e 74 72 6f 6c 20 4f 66 Level Control Of
3cb20 20 44 61 74 61 62 61 73 65 20 46 69 6c 65 73 20 Database Files
3cb30 7b 48 31 31 33 30 30 7d 20 3c 53 33 30 38 30 30 {H11300} <S30800
3cb40 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 30 31 7d >.**.** {H11301}
3cb50 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 66 69 The [sqlite3_fi
3cb60 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 69 6e le_control()] in
3cb70 74 65 72 66 61 63 65 20 6d 61 6b 65 73 20 61 20 terface makes a
3cb80 64 69 72 65 63 74 20 63 61 6c 6c 20 74 6f 20 74 direct call to t
3cb90 68 65 0a 2a 2a 20 78 46 69 6c 65 43 6f 6e 74 72 he.** xFileContr
3cba0 6f 6c 20 6d 65 74 68 6f 64 20 66 6f 72 20 74 68 ol method for th
3cbb0 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 e [sqlite3_io_me
3cbc0 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20 61 73 thods] object as
3cbd0 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 sociated.** with
3cbe0 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 a particular da
3cbf0 74 61 62 61 73 65 20 69 64 65 6e 74 69 66 69 65 tabase identifie
3cc00 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64 20 d by the second
3cc10 61 72 67 75 6d 65 6e 74 2e 20 7b 48 31 31 33 30 argument. {H1130
3cc20 32 7d 20 54 68 65 0a 2a 2a 20 6e 61 6d 65 20 6f 2} The.** name o
3cc30 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 f the database i
3cc40 73 20 74 68 65 20 6e 61 6d 65 20 61 73 73 69 67 s the name assig
3cc50 6e 65 64 20 74 6f 20 74 68 65 20 64 61 74 61 62 ned to the datab
3cc60 61 73 65 20 62 79 20 74 68 65 0a 2a 2a 20 3c 61 ase by the.** <a
3cc70 20 68 72 65 66 3d 22 6c 61 6e 67 5f 61 74 74 61 href="lang_atta
3cc80 63 68 2e 68 74 6d 6c 22 3e 41 54 54 41 43 48 3c ch.html">ATTACH<
3cc90 2f 61 3e 20 53 51 4c 20 63 6f 6d 6d 61 6e 64 20 /a> SQL command
3cca0 74 68 61 74 20 6f 70 65 6e 65 64 20 74 68 65 0a that opened the.
3ccb0 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 7b 48 31 ** database. {H1
3ccc0 31 33 30 33 7d 20 54 6f 20 63 6f 6e 74 72 6f 6c 1303} To control
3ccd0 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 the main databa
3cce0 73 65 20 66 69 6c 65 2c 20 75 73 65 20 74 68 65 se file, use the
3ccf0 20 6e 61 6d 65 20 22 6d 61 69 6e 22 0a 2a 2a 20 name "main".**
3cd00 6f 72 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 or a NULL pointe
3cd10 72 2e 20 7b 48 31 31 33 30 34 7d 20 54 68 65 20 r. {H11304} The
3cd20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72 74 68 third and fourth
3cd30 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74 parameters to t
3cd40 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 61 his routine.** a
3cd50 72 65 20 70 61 73 73 65 64 20 64 69 72 65 63 74 re passed direct
3cd60 6c 79 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 ly through to th
3cd70 65 20 73 65 63 6f 6e 64 20 61 6e 64 20 74 68 69 e second and thi
3cd80 72 64 20 70 61 72 61 6d 65 74 65 72 73 20 6f 66 rd parameters of
3cd90 0a 2a 2a 20 74 68 65 20 78 46 69 6c 65 43 6f 6e .** the xFileCon
3cda0 74 72 6f 6c 20 6d 65 74 68 6f 64 2e 20 20 7b 48 trol method. {H
3cdb0 31 31 33 30 35 7d 20 54 68 65 20 72 65 74 75 72 11305} The retur
3cdc0 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 78 n value of the x
3cdd0 46 69 6c 65 43 6f 6e 74 72 6f 6c 0a 2a 2a 20 6d FileControl.** m
3cde0 65 74 68 6f 64 20 62 65 63 6f 6d 65 73 20 74 68 ethod becomes th
3cdf0 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f e return value o
3ce00 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a f this routine..
3ce10 2a 2a 0a 2a 2a 20 7b 48 31 31 33 30 36 7d 20 49 **.** {H11306} I
3ce20 66 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 f the second par
3ce30 61 6d 65 74 65 72 20 28 7a 44 62 4e 61 6d 65 29 ameter (zDbName)
3ce40 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 does not match
3ce50 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 6e 79 0a the name of any.
3ce60 2a 2a 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 ** open database
3ce70 20 66 69 6c 65 2c 20 74 68 65 6e 20 53 51 4c 49 file, then SQLI
3ce80 54 45 5f 45 52 52 4f 52 20 69 73 20 72 65 74 75 TE_ERROR is retu
3ce90 72 6e 65 64 2e 20 7b 48 31 31 33 30 37 7d 20 54 rned. {H11307} T
3cea0 68 69 73 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 his error.** cod
3ceb0 65 20 69 73 20 6e 6f 74 20 72 65 6d 65 6d 62 65 e is not remembe
3cec0 72 65 64 20 61 6e 64 20 77 69 6c 6c 20 6e 6f 74 red and will not
3ced0 20 62 65 20 72 65 63 61 6c 6c 65 64 20 62 79 20 be recalled by
3cee0 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 [sqlite3_errcode
3cef0 28 29 5d 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 ()].** or [sqlit
3cf00 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e 20 7b 41 e3_errmsg()]. {A
3cf10 31 31 33 30 38 7d 20 54 68 65 20 75 6e 64 65 72 11308} The under
3cf20 6c 79 69 6e 67 20 78 46 69 6c 65 43 6f 6e 74 72 lying xFileContr
3cf30 6f 6c 20 6d 65 74 68 6f 64 20 6d 69 67 68 74 0a ol method might.
3cf40 2a 2a 20 61 6c 73 6f 20 72 65 74 75 72 6e 20 53 ** also return S
3cf50 51 4c 49 54 45 5f 45 52 52 4f 52 2e 20 20 7b 41 QLITE_ERROR. {A
3cf60 31 31 33 30 39 7d 20 54 68 65 72 65 20 69 73 20 11309} There is
3cf70 6e 6f 20 77 61 79 20 74 6f 20 64 69 73 74 69 6e no way to distin
3cf80 67 75 69 73 68 20 62 65 74 77 65 65 6e 0a 2a 2a guish between.**
3cf90 20 61 6e 20 69 6e 63 6f 72 72 65 63 74 20 7a 44 an incorrect zD
3cfa0 62 4e 61 6d 65 20 61 6e 64 20 61 6e 20 53 51 4c bName and an SQL
3cfb0 49 54 45 5f 45 52 52 4f 52 20 72 65 74 75 72 6e ITE_ERROR return
3cfc0 20 66 72 6f 6d 20 74 68 65 20 75 6e 64 65 72 6c from the underl
3cfd0 79 69 6e 67 0a 2a 2a 20 78 46 69 6c 65 43 6f 6e ying.** xFileCon
3cfe0 74 72 6f 6c 20 6d 65 74 68 6f 64 2e 20 7b 45 4e trol method. {EN
3cff0 44 7d 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 D}.**.** See als
3d000 6f 3a 20 5b 53 51 4c 49 54 45 5f 46 43 4e 54 4c o: [SQLITE_FCNTL
3d010 5f 4c 4f 43 4b 53 54 41 54 45 5d 0a 2a 2f 0a 53 _LOCKSTATE].*/.S
3d020 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
3d030 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 lite3_file_contr
3d040 6f 6c 28 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e ol(sqlite3*, con
3d050 73 74 20 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65 st char *zDbName
3d060 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 2a 29 , int op, void*)
3d070 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
3d080 46 3a 20 54 65 73 74 69 6e 67 20 49 6e 74 65 72 F: Testing Inter
3d090 66 61 63 65 20 7b 48 31 31 34 30 30 7d 20 3c 53 face {H11400} <S
3d0a0 33 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 30800>.**.** The
3d0b0 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f sqlite3_test_co
3d0c0 6e 74 72 6f 6c 28 29 20 69 6e 74 65 72 66 61 63 ntrol() interfac
3d0d0 65 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 61 e is used to rea
3d0e0 64 20 6f 75 74 20 69 6e 74 65 72 6e 61 6c 0a 2a d out internal.*
3d0f0 2a 20 73 74 61 74 65 20 6f 66 20 53 51 4c 69 74 * state of SQLit
3d100 65 20 61 6e 64 20 74 6f 20 69 6e 6a 65 63 74 20 e and to inject
3d110 66 61 75 6c 74 73 20 69 6e 74 6f 20 53 51 4c 69 faults into SQLi
3d120 74 65 20 66 6f 72 20 74 65 73 74 69 6e 67 0a 2a te for testing.*
3d130 2a 20 70 75 72 70 6f 73 65 73 2e 20 20 54 68 65 * purposes. The
3d140 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 first parameter
3d150 20 69 73 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e is an operation
3d160 20 63 6f 64 65 20 74 68 61 74 20 64 65 74 65 72 code that deter
3d170 6d 69 6e 65 73 0a 2a 2a 20 74 68 65 20 6e 75 6d mines.** the num
3d180 62 65 72 2c 20 6d 65 61 6e 69 6e 67 2c 20 61 6e ber, meaning, an
3d190 64 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 61 d operation of a
3d1a0 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 20 70 61 ll subsequent pa
3d1b0 72 61 6d 65 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 rameters..**.**
3d1c0 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 69 This interface i
3d1d0 73 20 6e 6f 74 20 66 6f 72 20 75 73 65 20 62 79 s not for use by
3d1e0 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 2e 20 20 applications.
3d1f0 49 74 20 65 78 69 73 74 73 20 73 6f 6c 65 6c 79 It exists solely
3d200 0a 2a 2a 20 66 6f 72 20 76 65 72 69 66 79 69 6e .** for verifyin
3d210 67 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f 70 g the correct op
3d220 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 53 eration of the S
3d230 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 20 20 QLite library.
3d240 44 65 70 65 6e 64 69 6e 67 0a 2a 2a 20 6f 6e 20 Depending.** on
3d250 68 6f 77 20 74 68 65 20 53 51 4c 69 74 65 20 6c how the SQLite l
3d260 69 62 72 61 72 79 20 69 73 20 63 6f 6d 70 69 6c ibrary is compil
3d270 65 64 2c 20 74 68 69 73 20 69 6e 74 65 72 66 61 ed, this interfa
3d280 63 65 20 6d 69 67 68 74 20 6e 6f 74 20 65 78 69 ce might not exi
3d290 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 st..**.** The de
3d2a0 74 61 69 6c 73 20 6f 66 20 74 68 65 20 6f 70 65 tails of the ope
3d2b0 72 61 74 69 6f 6e 20 63 6f 64 65 73 2c 20 74 68 ration codes, th
3d2c0 65 69 72 20 6d 65 61 6e 69 6e 67 73 2c 20 74 68 eir meanings, th
3d2d0 65 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 e parameters.**
3d2e0 74 68 65 79 20 74 61 6b 65 2c 20 61 6e 64 20 77 they take, and w
3d2f0 68 61 74 20 74 68 65 79 20 64 6f 20 61 72 65 20 hat they do are
3d300 61 6c 6c 20 73 75 62 6a 65 63 74 20 74 6f 20 63 all subject to c
3d310 68 61 6e 67 65 20 77 69 74 68 6f 75 74 20 6e 6f hange without no
3d320 74 69 63 65 2e 0a 2a 2a 20 55 6e 6c 69 6b 65 20 tice..** Unlike
3d330 6d 6f 73 74 20 6f 66 20 74 68 65 20 53 51 4c 69 most of the SQLi
3d340 74 65 20 41 50 49 2c 20 74 68 69 73 20 66 75 6e te API, this fun
3d350 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 67 75 61 ction is not gua
3d360 72 61 6e 74 65 65 64 20 74 6f 0a 2a 2a 20 6f 70 ranteed to.** op
3d370 65 72 61 74 65 20 63 6f 6e 73 69 73 74 65 6e 74 erate consistent
3d380 6c 79 20 66 72 6f 6d 20 6f 6e 65 20 72 65 6c 65 ly from one rele
3d390 61 73 65 20 74 6f 20 74 68 65 20 6e 65 78 74 2e ase to the next.
3d3a0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
3d3b0 6e 74 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f nt sqlite3_test_
3d3c0 63 6f 6e 74 72 6f 6c 28 69 6e 74 20 6f 70 2c 20 control(int op,
3d3d0 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 ...);../*.** CAP
3d3e0 49 33 52 45 46 3a 20 54 65 73 74 69 6e 67 20 49 I3REF: Testing I
3d3f0 6e 74 65 72 66 61 63 65 20 4f 70 65 72 61 74 69 nterface Operati
3d400 6f 6e 20 43 6f 64 65 73 20 7b 48 31 31 34 31 30 on Codes {H11410
3d410 7d 20 3c 48 31 31 34 30 30 3e 0a 2a 2a 0a 2a 2a } <H11400>.**.**
3d420 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 These constants
3d430 20 61 72 65 20 74 68 65 20 76 61 6c 69 64 20 6f are the valid o
3d440 70 65 72 61 74 69 6f 6e 20 63 6f 64 65 20 70 61 peration code pa
3d450 72 61 6d 65 74 65 72 73 20 75 73 65 64 0a 2a 2a rameters used.**
3d460 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 as the first ar
3d470 67 75 6d 65 6e 74 20 74 6f 20 5b 73 71 6c 69 74 gument to [sqlit
3d480 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 e3_test_control(
3d490 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 )]..**.** These
3d4a0 70 61 72 61 6d 65 74 65 72 73 20 61 6e 64 20 74 parameters and t
3d4b0 68 65 69 72 20 6d 65 61 6e 69 6e 67 73 20 61 72 heir meanings ar
3d4c0 65 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 e subject to cha
3d4d0 6e 67 65 0a 2a 2a 20 77 69 74 68 6f 75 74 20 6e nge.** without n
3d4e0 6f 74 69 63 65 2e 20 20 54 68 65 73 65 20 76 61 otice. These va
3d4f0 6c 75 65 73 20 61 72 65 20 66 6f 72 20 74 65 73 lues are for tes
3d500 74 69 6e 67 20 70 75 72 70 6f 73 65 73 20 6f 6e ting purposes on
3d510 6c 79 2e 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69 ly..** Applicati
3d520 6f 6e 73 20 73 68 6f 75 6c 64 20 6e 6f 74 20 75 ons should not u
3d530 73 65 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20 se any of these
3d540 70 61 72 61 6d 65 74 65 72 73 20 6f 72 20 74 68 parameters or th
3d550 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 74 65 e.** [sqlite3_te
3d560 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 69 6e st_control()] in
3d570 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 23 64 65 66 terface..*/.#def
3d580 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 ine SQLITE_TESTC
3d590 54 52 4c 5f 50 52 4e 47 5f 53 41 56 45 20 20 20 TRL_PRNG_SAVE
3d5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 35 0a 23 5.#
3d5b0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 define SQLITE_TE
3d5c0 53 54 43 54 52 4c 5f 50 52 4e 47 5f 52 45 53 54 STCTRL_PRNG_REST
3d5d0 4f 52 45 20 20 20 20 20 20 20 20 20 20 20 20 20 ORE
3d5e0 36 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 6.#define SQLITE
3d5f0 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f 52 _TESTCTRL_PRNG_R
3d600 45 53 45 54 20 20 20 20 20 20 20 20 20 20 20 20 ESET
3d610 20 20 20 37 0a 23 64 65 66 69 6e 65 20 53 51 4c 7.#define SQL
3d620 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 42 49 54 ITE_TESTCTRL_BIT
3d630 56 45 43 5f 54 45 53 54 20 20 20 20 20 20 20 20 VEC_TEST
3d640 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e 65 20 8.#define
3d650 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f SQLITE_TESTCTRL_
3d660 46 41 55 4c 54 5f 49 4e 53 54 41 4c 4c 20 20 20 FAULT_INSTALL
3d670 20 20 20 20 20 20 20 20 20 39 0a 23 64 65 66 69 9.#defi
3d680 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 ne SQLITE_TESTCT
3d690 52 4c 5f 42 45 4e 49 47 4e 5f 4d 41 4c 4c 4f 43 RL_BENIGN_MALLOC
3d6a0 5f 48 4f 4f 4b 53 20 20 20 20 20 31 30 0a 23 64 _HOOKS 10.#d
3d6b0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 efine SQLITE_TES
3d6c0 54 43 54 52 4c 5f 50 45 4e 44 49 4e 47 5f 42 59 TCTRL_PENDING_BY
3d6d0 54 45 20 20 20 20 20 20 20 20 20 20 20 20 31 31 TE 11
3d6e0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
3d6f0 54 45 53 54 43 54 52 4c 5f 41 53 53 45 52 54 20 TESTCTRL_ASSERT
3d700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
3d710 20 31 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 12.#define SQLI
3d720 54 45 5f 54 45 53 54 43 54 52 4c 5f 41 4c 57 41 TE_TESTCTRL_ALWA
3d730 59 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 YS
3d740 20 20 20 20 31 33 0a 23 64 65 66 69 6e 65 20 53 13.#define S
3d750 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 52 QLITE_TESTCTRL_R
3d760 45 53 45 52 56 45 20 20 20 20 20 20 20 20 20 20 ESERVE
3d770 20 20 20 20 20 20 20 31 34 0a 0a 2f 2a 0a 2a 2a 14../*.**
3d780 20 43 41 50 49 33 52 45 46 3a 20 53 51 4c 69 74 CAPI3REF: SQLit
3d790 65 20 52 75 6e 74 69 6d 65 20 53 74 61 74 75 73 e Runtime Status
3d7a0 20 7b 48 31 37 32 30 30 7d 20 3c 53 36 30 32 30 {H17200} <S6020
3d7b0 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 0>.** EXPERIMENT
3d7c0 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e AL.**.** This in
3d7d0 74 65 72 66 61 63 65 20 69 73 20 75 73 65 64 20 terface is used
3d7e0 74 6f 20 72 65 74 72 69 65 76 65 20 72 75 6e 74 to retrieve runt
3d7f0 69 6d 65 20 73 74 61 74 75 73 20 69 6e 66 6f 72 ime status infor
3d800 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 mation.** about
3d810 74 68 65 20 70 72 65 66 6f 72 6d 61 6e 63 65 20 the preformance
3d820 6f 66 20 53 51 4c 69 74 65 2c 20 61 6e 64 20 6f of SQLite, and o
3d830 70 74 69 6f 6e 61 6c 6c 79 20 74 6f 20 72 65 73 ptionally to res
3d840 65 74 20 76 61 72 69 6f 75 73 0a 2a 2a 20 68 69 et various.** hi
3d850 67 68 77 61 74 65 72 20 6d 61 72 6b 73 2e 20 20 ghwater marks.
3d860 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 The first argume
3d870 6e 74 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 nt is an integer
3d880 20 63 6f 64 65 20 66 6f 72 0a 2a 2a 20 74 68 65 code for.** the
3d890 20 73 70 65 63 69 66 69 63 20 70 61 72 61 6d 65 specific parame
3d8a0 74 65 72 20 74 6f 20 6d 65 61 73 75 72 65 2e 20 ter to measure.
3d8b0 20 52 65 63 6f 67 6e 69 7a 65 64 20 69 6e 74 65 Recognized inte
3d8c0 67 65 72 20 63 6f 64 65 73 0a 2a 2a 20 61 72 65 ger codes.** are
3d8d0 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 5b 53 51 of the form [SQ
3d8e0 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f LITE_STATUS_MEMO
3d8f0 52 59 5f 55 53 45 44 20 7c 20 53 51 4c 49 54 45 RY_USED | SQLITE
3d900 5f 53 54 41 54 55 53 5f 2e 2e 2e 5d 2e 0a 2a 2a _STATUS_...]..**
3d910 20 54 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c The current val
3d920 75 65 20 6f 66 20 74 68 65 20 70 61 72 61 6d 65 ue of the parame
3d930 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 ter is returned
3d940 69 6e 74 6f 20 2a 70 43 75 72 72 65 6e 74 2e 0a into *pCurrent..
3d950 2a 2a 20 54 68 65 20 68 69 67 68 65 73 74 20 72 ** The highest r
3d960 65 63 6f 72 64 65 64 20 76 61 6c 75 65 20 69 73 ecorded value is
3d970 20 72 65 74 75 72 6e 65 64 20 69 6e 20 2a 70 48 returned in *pH
3d980 69 67 68 77 61 74 65 72 2e 20 20 49 66 20 74 68 ighwater. If th
3d990 65 0a 2a 2a 20 72 65 73 65 74 46 6c 61 67 20 69 e.** resetFlag i
3d9a0 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 s true, then the
3d9b0 20 68 69 67 68 65 73 74 20 72 65 63 6f 72 64 20 highest record
3d9c0 76 61 6c 75 65 20 69 73 20 72 65 73 65 74 20 61 value is reset a
3d9d0 66 74 65 72 0a 2a 2a 20 2a 70 48 69 67 68 77 61 fter.** *pHighwa
3d9e0 74 65 72 20 69 73 20 77 72 69 74 74 65 6e 2e 20 ter is written.
3d9f0 53 6f 6d 65 20 70 61 72 61 6d 65 74 65 72 73 20 Some parameters
3da00 64 6f 20 6e 6f 74 20 72 65 63 6f 72 64 20 74 68 do not record th
3da10 65 20 68 69 67 68 65 73 74 0a 2a 2a 20 76 61 6c e highest.** val
3da20 75 65 2e 20 20 46 6f 72 20 74 68 6f 73 65 20 70 ue. For those p
3da30 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 6e 6f 74 arameters.** not
3da40 68 69 6e 67 20 69 73 20 77 72 69 74 74 65 6e 20 hing is written
3da50 69 6e 74 6f 20 2a 70 48 69 67 68 77 61 74 65 72 into *pHighwater
3da60 20 61 6e 64 20 74 68 65 20 72 65 73 65 74 46 6c and the resetFl
3da70 61 67 20 69 73 20 69 67 6e 6f 72 65 64 2e 0a 2a ag is ignored..*
3da80 2a 20 4f 74 68 65 72 20 70 61 72 61 6d 65 74 65 * Other paramete
3da90 72 73 20 72 65 63 6f 72 64 20 6f 6e 6c 79 20 74 rs record only t
3daa0 68 65 20 68 69 67 68 77 61 74 65 72 20 6d 61 72 he highwater mar
3dab0 6b 20 61 6e 64 20 6e 6f 74 20 74 68 65 20 63 75 k and not the cu
3dac0 72 72 65 6e 74 0a 2a 2a 20 76 61 6c 75 65 2e 20 rrent.** value.
3dad0 20 46 6f 72 20 74 68 65 73 65 20 6c 61 74 74 65 For these latte
3dae0 72 20 70 61 72 61 6d 65 74 65 72 73 20 6e 6f 74 r parameters not
3daf0 68 69 6e 67 20 69 73 20 77 72 69 74 74 65 6e 20 hing is written
3db00 69 6e 74 6f 20 2a 70 43 75 72 72 65 6e 74 2e 0a into *pCurrent..
3db10 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
3db20 6e 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 ne returns SQLIT
3db30 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 E_OK on success
3db40 61 6e 64 20 61 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a and a non-zero.*
3db50 2a 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f * [error code] o
3db60 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a 2a n failure..**.**
3db70 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
3db80 20 74 68 72 65 61 64 73 61 66 65 20 62 75 74 20 threadsafe but
3db90 69 73 20 6e 6f 74 20 61 74 6f 6d 69 63 2e 20 20 is not atomic.
3dba0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e This routine can
3dbb0 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 64 20 77 68 be.** called wh
3dbc0 69 6c 65 20 6f 74 68 65 72 20 74 68 72 65 61 64 ile other thread
3dbd0 73 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 74 68 s are running th
3dbe0 65 20 73 61 6d 65 20 6f 72 20 64 69 66 66 65 72 e same or differ
3dbf0 65 6e 74 20 53 51 4c 69 74 65 0a 2a 2a 20 69 6e ent SQLite.** in
3dc00 74 65 72 66 61 63 65 73 2e 20 20 48 6f 77 65 76 terfaces. Howev
3dc10 65 72 20 74 68 65 20 76 61 6c 75 65 73 20 72 65 er the values re
3dc20 74 75 72 6e 65 64 20 69 6e 20 2a 70 43 75 72 72 turned in *pCurr
3dc30 65 6e 74 20 61 6e 64 0a 2a 2a 20 2a 70 48 69 67 ent and.** *pHig
3dc40 68 77 61 74 65 72 20 72 65 66 6c 65 63 74 20 74 hwater reflect t
3dc50 68 65 20 73 74 61 74 75 73 20 6f 66 20 53 51 4c he status of SQL
3dc60 69 74 65 20 61 74 20 64 69 66 66 65 72 65 6e 74 ite at different
3dc70 20 70 6f 69 6e 74 73 20 69 6e 20 74 69 6d 65 0a points in time.
3dc80 2a 2a 20 61 6e 64 20 69 74 20 69 73 20 70 6f 73 ** and it is pos
3dc90 73 69 62 6c 65 20 74 68 61 74 20 61 6e 6f 74 68 sible that anoth
3dca0 65 72 20 74 68 72 65 61 64 20 6d 69 67 68 74 20 er thread might
3dcb0 63 68 61 6e 67 65 20 74 68 65 20 70 61 72 61 6d change the param
3dcc0 65 74 65 72 0a 2a 2a 20 69 6e 20 62 65 74 77 65 eter.** in betwe
3dcd0 65 6e 20 74 68 65 20 74 69 6d 65 73 20 77 68 65 en the times whe
3dce0 6e 20 2a 70 43 75 72 72 65 6e 74 20 61 6e 64 20 n *pCurrent and
3dcf0 2a 70 48 69 67 68 77 61 74 65 72 20 61 72 65 20 *pHighwater are
3dd00 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 53 written..**.** S
3dd10 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 ee also: [sqlite
3dd20 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 0a 2a 3_db_status()].*
3dd30 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c /.SQLITE_API SQL
3dd40 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c ITE_EXPERIMENTAL
3dd50 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 61 int sqlite3_sta
3dd60 74 75 73 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20 tus(int op, int
3dd70 2a 70 43 75 72 72 65 6e 74 2c 20 69 6e 74 20 2a *pCurrent, int *
3dd80 70 48 69 67 68 77 61 74 65 72 2c 20 69 6e 74 20 pHighwater, int
3dd90 72 65 73 65 74 46 6c 61 67 29 3b 0a 0a 0a 2f 2a resetFlag);.../*
3dda0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 74 .** CAPI3REF: St
3ddb0 61 74 75 73 20 50 61 72 61 6d 65 74 65 72 73 20 atus Parameters
3ddc0 7b 48 31 37 32 35 30 7d 20 3c 48 31 37 32 30 30 {H17250} <H17200
3ddd0 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 >.** EXPERIMENTA
3dde0 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 69 6e L.**.** These in
3ddf0 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 20 teger constants
3de00 64 65 73 69 67 6e 61 74 65 20 76 61 72 69 6f 75 designate variou
3de10 73 20 72 75 6e 2d 74 69 6d 65 20 73 74 61 74 75 s run-time statu
3de20 73 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 s parameters.**
3de30 74 68 61 74 20 63 61 6e 20 62 65 20 72 65 74 75 that can be retu
3de40 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 rned by [sqlite3
3de50 5f 73 74 61 74 75 73 28 29 5d 2e 0a 2a 2a 0a 2a _status()]..**.*
3de60 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 * <dl>.** <dt>SQ
3de70 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f LITE_STATUS_MEMO
3de80 52 59 5f 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20 RY_USED</dt>.**
3de90 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 <dd>This paramet
3dea0 65 72 20 69 73 20 74 68 65 20 63 75 72 72 65 6e er is the curren
3deb0 74 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f t amount of memo
3dec0 72 79 20 63 68 65 63 6b 65 64 20 6f 75 74 0a 2a ry checked out.*
3ded0 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 * using [sqlite3
3dee0 5f 6d 61 6c 6c 6f 63 28 29 5d 2c 20 65 69 74 68 _malloc()], eith
3def0 65 72 20 64 69 72 65 63 74 6c 79 20 6f 72 20 69 er directly or i
3df00 6e 64 69 72 65 63 74 6c 79 2e 20 20 54 68 65 0a ndirectly. The.
3df10 2a 2a 20 66 69 67 75 72 65 20 69 6e 63 6c 75 64 ** figure includ
3df20 65 73 20 63 61 6c 6c 73 20 6d 61 64 65 20 74 6f es calls made to
3df30 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 [sqlite3_malloc
3df40 28 29 5d 20 62 79 20 74 68 65 20 61 70 70 6c 69 ()] by the appli
3df50 63 61 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 69 6e cation.** and in
3df60 74 65 72 6e 61 6c 20 6d 65 6d 6f 72 79 20 75 73 ternal memory us
3df70 61 67 65 20 62 79 20 74 68 65 20 53 51 4c 69 74 age by the SQLit
3df80 65 20 6c 69 62 72 61 72 79 2e 20 20 53 63 72 61 e library. Scra
3df90 74 63 68 20 6d 65 6d 6f 72 79 0a 2a 2a 20 63 6f tch memory.** co
3dfa0 6e 74 72 6f 6c 6c 65 64 20 62 79 20 5b 53 51 4c ntrolled by [SQL
3dfb0 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 ITE_CONFIG_SCRAT
3dfc0 43 48 5d 20 61 6e 64 20 61 75 78 69 6c 69 61 72 CH] and auxiliar
3dfd0 79 20 70 61 67 65 2d 63 61 63 68 65 0a 2a 2a 20 y page-cache.**
3dfe0 6d 65 6d 6f 72 79 20 63 6f 6e 74 72 6f 6c 6c 65 memory controlle
3dff0 64 20 62 79 20 5b 53 51 4c 49 54 45 5f 43 4f 4e d by [SQLITE_CON
3e000 46 49 47 5f 50 41 47 45 43 41 43 48 45 5d 20 69 FIG_PAGECACHE] i
3e010 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 64 20 69 s not included i
3e020 6e 0a 2a 2a 20 74 68 69 73 20 70 61 72 61 6d 65 n.** this parame
3e030 74 65 72 2e 20 20 54 68 65 20 61 6d 6f 75 6e 74 ter. The amount
3e040 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 65 returned is the
3e050 20 73 75 6d 20 6f 66 20 74 68 65 20 61 6c 6c 6f sum of the allo
3e060 63 61 74 69 6f 6e 0a 2a 2a 20 73 69 7a 65 73 20 cation.** sizes
3e070 61 73 20 72 65 70 6f 72 74 65 64 20 62 79 20 74 as reported by t
3e080 68 65 20 78 53 69 7a 65 20 6d 65 74 68 6f 64 20 he xSize method
3e090 69 6e 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 5f in [sqlite3_mem_
3e0a0 6d 65 74 68 6f 64 73 5d 2e 3c 2f 64 64 3e 0a 2a methods].</dd>.*
3e0b0 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f *.** <dt>SQLITE_
3e0c0 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 STATUS_MALLOC_SI
3e0d0 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 ZE</dt>.** <dd>T
3e0e0 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 his parameter re
3e0f0 63 6f 72 64 73 20 74 68 65 20 6c 61 72 67 65 73 cords the larges
3e100 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 t memory allocat
3e110 69 6f 6e 20 72 65 71 75 65 73 74 0a 2a 2a 20 68 ion request.** h
3e120 61 6e 64 65 64 20 74 6f 20 5b 73 71 6c 69 74 65 anded to [sqlite
3e130 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20 5b 3_malloc()] or [
3e140 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 sqlite3_realloc(
3e150 29 5d 20 28 6f 72 20 74 68 65 69 72 0a 2a 2a 20 )] (or their.**
3e160 69 6e 74 65 72 6e 61 6c 20 65 71 75 69 76 61 6c internal equival
3e170 65 6e 74 73 29 2e 20 20 4f 6e 6c 79 20 74 68 65 ents). Only the
3e180 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
3e190 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48 69 67 68 in the.** *pHigh
3e1a0 77 61 74 65 72 20 70 61 72 61 6d 65 74 65 72 20 water parameter
3e1b0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 to [sqlite3_stat
3e1c0 75 73 28 29 5d 20 69 73 20 6f 66 20 69 6e 74 65 us()] is of inte
3e1d0 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68 65 20 76 rest. .** The v
3e1e0 61 6c 75 65 20 77 72 69 74 74 65 6e 20 69 6e 74 alue written int
3e1f0 6f 20 74 68 65 20 2a 70 43 75 72 72 65 6e 74 20 o the *pCurrent
3e200 70 61 72 61 6d 65 74 65 72 20 69 73 20 75 6e 64 parameter is und
3e210 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a efined.</dd>.**.
3e220 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 ** <dt>SQLITE_ST
3e230 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55 ATUS_PAGECACHE_U
3e240 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e SED</dt>.** <dd>
3e250 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 This parameter r
3e260 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 eturns the numbe
3e270 72 20 6f 66 20 70 61 67 65 73 20 75 73 65 64 20 r of pages used
3e280 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 70 out of the.** [p
3e290 61 67 65 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 agecache memory
3e2a0 61 6c 6c 6f 63 61 74 6f 72 5d 20 74 68 61 74 20 allocator] that
3e2b0 77 61 73 20 63 6f 6e 66 69 67 75 72 65 64 20 75 was configured u
3e2c0 73 69 6e 67 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 sing .** [SQLITE
3e2d0 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 _CONFIG_PAGECACH
3e2e0 45 5d 2e 20 20 54 68 65 0a 2a 2a 20 76 61 6c 75 E]. The.** valu
3e2f0 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 69 6e e returned is in
3e300 20 70 61 67 65 73 2c 20 6e 6f 74 20 69 6e 20 62 pages, not in b
3e310 79 74 65 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a ytes.</dd>.**.**
3e320 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 <dt>SQLITE_STAT
3e330 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f 56 45 US_PAGECACHE_OVE
3e340 52 46 4c 4f 57 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 RFLOW</dt>.** <d
3e350 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 d>This parameter
3e360 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d returns the num
3e370 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 ber of bytes of
3e380 70 61 67 65 20 63 61 63 68 65 0a 2a 2a 20 61 6c page cache.** al
3e390 6c 6f 63 61 74 69 6f 6e 20 77 68 69 63 68 20 63 location which c
3e3a0 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 74 61 74 ould not be stat
3e3b0 69 73 66 69 65 64 20 62 79 20 74 68 65 20 5b 53 isfied by the [S
3e3c0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 QLITE_CONFIG_PAG
3e3d0 45 43 41 43 48 45 5d 0a 2a 2a 20 62 75 66 66 65 ECACHE].** buffe
3e3e0 72 20 61 6e 64 20 77 68 65 72 65 20 66 6f 72 63 r and where forc
3e3f0 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 74 ed to overflow t
3e400 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f o [sqlite3_mallo
3e410 63 28 29 5d 2e 20 20 54 68 65 0a 2a 2a 20 72 65 c()]. The.** re
3e420 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 6e 63 turned value inc
3e430 6c 75 64 65 73 20 61 6c 6c 6f 63 61 74 69 6f 6e ludes allocation
3e440 73 20 74 68 61 74 20 6f 76 65 72 66 6c 6f 77 65 s that overflowe
3e450 64 20 62 65 63 61 75 73 65 20 74 68 65 79 0a 2a d because they.*
3e460 2a 20 77 68 65 72 65 20 74 6f 6f 20 6c 61 72 67 * where too larg
3e470 65 20 28 74 68 65 79 20 77 65 72 65 20 6c 61 72 e (they were lar
3e480 67 65 72 20 74 68 61 6e 20 74 68 65 20 22 73 7a ger than the "sz
3e490 22 20 70 61 72 61 6d 65 74 65 72 20 74 6f 0a 2a " parameter to.*
3e4a0 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 * [SQLITE_CONFIG
3e4b0 5f 50 41 47 45 43 41 43 48 45 5d 29 20 61 6e 64 _PAGECACHE]) and
3e4c0 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 74 68 61 allocations tha
3e4d0 74 20 6f 76 65 72 66 6c 6f 77 65 64 20 62 65 63 t overflowed bec
3e4e0 61 75 73 65 0a 2a 2a 20 6e 6f 20 73 70 61 63 65 ause.** no space
3e4f0 20 77 61 73 20 6c 65 66 74 20 69 6e 20 74 68 65 was left in the
3e500 20 70 61 67 65 20 63 61 63 68 65 2e 3c 2f 64 64 page cache.</dd
3e510 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 >.**.** <dt>SQLI
3e520 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 TE_STATUS_PAGECA
3e530 43 48 45 5f 53 49 5a 45 3c 2f 64 74 3e 0a 2a 2a CHE_SIZE</dt>.**
3e540 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 <dd>This parame
3e550 74 65 72 20 72 65 63 6f 72 64 73 20 74 68 65 20 ter records the
3e560 6c 61 72 67 65 73 74 20 6d 65 6d 6f 72 79 20 61 largest memory a
3e570 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 73 llocation reques
3e580 74 0a 2a 2a 20 68 61 6e 64 65 64 20 74 6f 20 5b t.** handed to [
3e590 70 61 67 65 63 61 63 68 65 20 6d 65 6d 6f 72 79 pagecache memory
3e5a0 20 61 6c 6c 6f 63 61 74 6f 72 5d 2e 20 20 4f 6e allocator]. On
3e5b0 6c 79 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 ly the value ret
3e5c0 75 72 6e 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 urned in the.**
3e5d0 2a 70 48 69 67 68 77 61 74 65 72 20 70 61 72 61 *pHighwater para
3e5e0 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 meter to [sqlite
3e5f0 33 5f 73 74 61 74 75 73 28 29 5d 20 69 73 20 6f 3_status()] is o
3e600 66 20 69 6e 74 65 72 65 73 74 2e 20 20 0a 2a 2a f interest. .**
3e610 20 54 68 65 20 76 61 6c 75 65 20 77 72 69 74 74 The value writt
3e620 65 6e 20 69 6e 74 6f 20 74 68 65 20 2a 70 43 75 en into the *pCu
3e630 72 72 65 6e 74 20 70 61 72 61 6d 65 74 65 72 20 rrent parameter
3e640 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 is undefined.</d
3e650 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c d>.**.** <dt>SQL
3e660 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 ITE_STATUS_SCRAT
3e670 43 48 5f 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20 CH_USED</dt>.**
3e680 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 <dd>This paramet
3e690 65 72 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e er returns the n
3e6a0 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 umber of allocat
3e6b0 69 6f 6e 73 20 75 73 65 64 20 6f 75 74 20 6f 66 ions used out of
3e6c0 20 74 68 65 0a 2a 2a 20 5b 73 63 72 61 74 63 68 the.** [scratch
3e6d0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f memory allocato
3e6e0 72 5d 20 63 6f 6e 66 69 67 75 72 65 64 20 75 73 r] configured us
3e6f0 69 6e 67 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 ing.** [SQLITE_C
3e700 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 2e 20 ONFIG_SCRATCH].
3e710 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 The value retur
3e720 6e 65 64 20 69 73 20 69 6e 20 61 6c 6c 6f 63 61 ned is in alloca
3e730 74 69 6f 6e 73 2c 20 6e 6f 74 0a 2a 2a 20 69 6e tions, not.** in
3e740 20 62 79 74 65 73 2e 20 20 53 69 6e 63 65 20 61 bytes. Since a
3e750 20 73 69 6e 67 6c 65 20 74 68 72 65 61 64 20 6d single thread m
3e760 61 79 20 6f 6e 6c 79 20 68 61 76 65 20 6f 6e 65 ay only have one
3e770 20 73 63 72 61 74 63 68 20 61 6c 6c 6f 63 61 74 scratch allocat
3e780 69 6f 6e 0a 2a 2a 20 6f 75 74 73 74 61 6e 64 69 ion.** outstandi
3e790 6e 67 20 61 74 20 74 69 6d 65 2c 20 74 68 69 73 ng at time, this
3e7a0 20 70 61 72 61 6d 65 74 65 72 20 61 6c 73 6f 20 parameter also
3e7b0 72 65 70 6f 72 74 73 20 74 68 65 20 6e 75 6d 62 reports the numb
3e7c0 65 72 20 6f 66 20 74 68 72 65 61 64 73 0a 2a 2a er of threads.**
3e7d0 20 75 73 69 6e 67 20 73 63 72 61 74 63 68 20 6d using scratch m
3e7e0 65 6d 6f 72 79 20 61 74 20 74 68 65 20 73 61 6d emory at the sam
3e7f0 65 20 74 69 6d 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a e time.</dd>.**.
3e800 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 ** <dt>SQLITE_ST
3e810 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 45 ATUS_SCRATCH_OVE
3e820 52 46 4c 4f 57 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 RFLOW</dt>.** <d
3e830 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 d>This parameter
3e840 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d returns the num
3e850 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 ber of bytes of
3e860 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 0a 2a scratch memory.*
3e870 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 68 69 * allocation whi
3e880 63 68 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 ch could not be
3e890 73 74 61 74 69 73 66 69 65 64 20 62 79 20 74 68 statisfied by th
3e8a0 65 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 e [SQLITE_CONFIG
3e8b0 5f 53 43 52 41 54 43 48 5d 0a 2a 2a 20 62 75 66 _SCRATCH].** buf
3e8c0 66 65 72 20 61 6e 64 20 77 68 65 72 65 20 66 6f fer and where fo
3e8d0 72 63 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f 77 rced to overflow
3e8e0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c to [sqlite3_mal
3e8f0 6c 6f 63 28 29 5d 2e 20 20 54 68 65 20 76 61 6c loc()]. The val
3e900 75 65 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 ues.** returned
3e910 69 6e 63 6c 75 64 65 20 6f 76 65 72 66 6c 6f 77 include overflow
3e920 73 20 62 65 63 61 75 73 65 20 74 68 65 20 72 65 s because the re
3e930 71 75 65 73 74 65 64 20 61 6c 6c 6f 63 61 74 69 quested allocati
3e940 6f 6e 20 77 61 73 20 74 6f 6f 0a 2a 2a 20 6c 61 on was too.** la
3e950 72 67 65 72 20 28 74 68 61 74 20 69 73 2c 20 62 rger (that is, b
3e960 65 63 61 75 73 65 20 74 68 65 20 72 65 71 75 65 ecause the reque
3e970 73 74 65 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 sted allocation
3e980 77 61 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 was larger than
3e990 74 68 65 0a 2a 2a 20 22 73 7a 22 20 70 61 72 61 the.** "sz" para
3e9a0 6d 65 74 65 72 20 74 6f 20 5b 53 51 4c 49 54 45 meter to [SQLITE
3e9b0 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d _CONFIG_SCRATCH]
3e9c0 29 20 61 6e 64 20 62 65 63 61 75 73 65 20 6e 6f ) and because no
3e9d0 20 73 63 72 61 74 63 68 20 62 75 66 66 65 72 0a scratch buffer.
3e9e0 2a 2a 20 73 6c 6f 74 73 20 77 65 72 65 20 61 76 ** slots were av
3e9f0 61 69 6c 61 62 6c 65 2e 0a 2a 2a 20 3c 2f 64 64 ailable..** </dd
3ea00 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 >.**.** <dt>SQLI
3ea10 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 TE_STATUS_SCRATC
3ea20 48 5f 53 49 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c H_SIZE</dt>.** <
3ea30 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 dd>This paramete
3ea40 72 20 72 65 63 6f 72 64 73 20 74 68 65 20 6c 61 r records the la
3ea50 72 67 65 73 74 20 6d 65 6d 6f 72 79 20 61 6c 6c rgest memory all
3ea60 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 73 74 0a ocation request.
3ea70 2a 2a 20 68 61 6e 64 65 64 20 74 6f 20 5b 73 63 ** handed to [sc
3ea80 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 61 6c 6c ratch memory all
3ea90 6f 63 61 74 6f 72 5d 2e 20 20 4f 6e 6c 79 20 74 ocator]. Only t
3eaa0 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 he value returne
3eab0 64 20 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48 69 d in the.** *pHi
3eac0 67 68 77 61 74 65 72 20 70 61 72 61 6d 65 74 65 ghwater paramete
3ead0 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 r to [sqlite3_st
3eae0 61 74 75 73 28 29 5d 20 69 73 20 6f 66 20 69 6e atus()] is of in
3eaf0 74 65 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68 65 terest. .** The
3eb00 20 76 61 6c 75 65 20 77 72 69 74 74 65 6e 20 69 value written i
3eb10 6e 74 6f 20 74 68 65 20 2a 70 43 75 72 72 65 6e nto the *pCurren
3eb20 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 75 t parameter is u
3eb30 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a ndefined.</dd>.*
3eb40 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f *.** <dt>SQLITE_
3eb50 53 54 41 54 55 53 5f 50 41 52 53 45 52 5f 53 54 STATUS_PARSER_ST
3eb60 41 43 4b 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e ACK</dt>.** <dd>
3eb70 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 This parameter r
3eb80 65 63 6f 72 64 73 20 74 68 65 20 64 65 65 70 65 ecords the deepe
3eb90 73 74 20 70 61 72 73 65 72 20 73 74 61 63 6b 2e st parser stack.
3eba0 20 20 49 74 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 It is only.**
3ebb0 6d 65 61 6e 69 6e 67 66 75 6c 20 69 66 20 53 51 meaningful if SQ
3ebc0 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 Lite is compiled
3ebd0 20 77 69 74 68 20 5b 59 59 54 52 41 43 4b 4d 41 with [YYTRACKMA
3ebe0 58 53 54 41 43 4b 44 45 50 54 48 5d 2e 3c 2f 64 XSTACKDEPTH].</d
3ebf0 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2a 0a 2a d>.** </dl>.**.*
3ec00 2a 20 4e 65 77 20 73 74 61 74 75 73 20 70 61 72 * New status par
3ec10 61 6d 65 74 65 72 73 20 6d 61 79 20 62 65 20 61 ameters may be a
3ec20 64 64 65 64 20 66 72 6f 6d 20 74 69 6d 65 20 74 dded from time t
3ec30 6f 20 74 69 6d 65 2e 0a 2a 2f 0a 23 64 65 66 69 o time..*/.#defi
3ec40 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 ne SQLITE_STATUS
3ec50 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 20 20 20 20 _MEMORY_USED
3ec60 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 0.#define
3ec70 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 SQLITE_STATUS_PA
3ec80 47 45 43 41 43 48 45 5f 55 53 45 44 20 20 20 20 GECACHE_USED
3ec90 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 1.#define SQL
3eca0 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 ITE_STATUS_PAGEC
3ecb0 41 43 48 45 5f 4f 56 45 52 46 4c 4f 57 20 20 20 ACHE_OVERFLOW
3ecc0 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 2.#define SQLITE
3ecd0 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f _STATUS_SCRATCH_
3ece0 55 53 45 44 20 20 20 20 20 20 20 20 20 33 0a 23 USED 3.#
3ecf0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 define SQLITE_ST
3ed00 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 45 ATUS_SCRATCH_OVE
3ed10 52 46 4c 4f 57 20 20 20 20 20 34 0a 23 64 65 66 RFLOW 4.#def
3ed20 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 ine SQLITE_STATU
3ed30 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 20 20 20 S_MALLOC_SIZE
3ed40 20 20 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65 5.#define
3ed50 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 SQLITE_STATUS_P
3ed60 41 52 53 45 52 5f 53 54 41 43 4b 20 20 20 20 20 ARSER_STACK
3ed70 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20 53 51 6.#define SQ
3ed80 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 LITE_STATUS_PAGE
3ed90 43 41 43 48 45 5f 53 49 5a 45 20 20 20 20 20 20 CACHE_SIZE
3eda0 20 37 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 7.#define SQLIT
3edb0 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 E_STATUS_SCRATCH
3edc0 5f 53 49 5a 45 20 20 20 20 20 20 20 20 20 38 0a _SIZE 8.
3edd0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
3ede0 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 Database Connec
3edf0 74 69 6f 6e 20 53 74 61 74 75 73 20 7b 48 31 37 tion Status {H17
3ee00 35 30 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 500} <S60200>.**
3ee10 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a EXPERIMENTAL.**
3ee20 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 .** This interfa
3ee30 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 ce is used to re
3ee40 74 72 69 65 76 65 20 72 75 6e 74 69 6d 65 20 73 trieve runtime s
3ee50 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 69 6f tatus informatio
3ee60 6e 20 0a 2a 2a 20 61 62 6f 75 74 20 61 20 73 69 n .** about a si
3ee70 6e 67 6c 65 20 5b 64 61 74 61 62 61 73 65 20 63 ngle [database c
3ee80 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20 20 54 68 65 onnection]. The
3ee90 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 first argument
3eea0 69 73 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 is the.** databa
3eeb0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62 se connection ob
3eec0 6a 65 63 74 20 74 6f 20 62 65 20 69 6e 74 65 72 ject to be inter
3eed0 72 6f 67 61 74 65 64 2e 20 20 54 68 65 20 73 65 rogated. The se
3eee0 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a cond argument.**
3eef0 20 69 73 20 74 68 65 20 70 61 72 61 6d 65 74 65 is the paramete
3ef00 72 20 74 6f 20 69 6e 74 65 72 72 6f 67 61 74 65 r to interrogate
3ef10 2e 20 20 43 75 72 72 65 6e 74 6c 79 2c 20 74 68 . Currently, th
3ef20 65 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 76 e only allowed v
3ef30 61 6c 75 65 0a 2a 2a 20 66 6f 72 20 74 68 65 20 alue.** for the
3ef40 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 second parameter
3ef50 20 69 73 20 5b 53 51 4c 49 54 45 5f 44 42 53 54 is [SQLITE_DBST
3ef60 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55 ATUS_LOOKASIDE_U
3ef70 53 45 44 5d 2e 0a 2a 2a 20 41 64 64 69 74 69 6f SED]..** Additio
3ef80 6e 61 6c 20 6f 70 74 69 6f 6e 73 20 77 69 6c 6c nal options will
3ef90 20 6c 69 6b 65 6c 79 20 61 70 70 65 61 72 20 69 likely appear i
3efa0 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 n future release
3efb0 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a s of SQLite..**.
3efc0 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20 76 ** The current v
3efd0 61 6c 75 65 20 6f 66 20 74 68 65 20 72 65 71 75 alue of the requ
3efe0 65 73 74 65 64 20 70 61 72 61 6d 65 74 65 72 20 ested parameter
3eff0 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 is written into
3f000 2a 70 43 75 72 0a 2a 2a 20 61 6e 64 20 74 68 65 *pCur.** and the
3f010 20 68 69 67 68 65 73 74 20 69 6e 73 74 61 6e 74 highest instant
3f020 61 6e 65 6f 75 73 20 76 61 6c 75 65 20 69 73 20 aneous value is
3f030 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 48 written into *pH
3f040 69 77 74 72 2e 20 20 49 66 0a 2a 2a 20 74 68 65 iwtr. If.** the
3f050 20 72 65 73 65 74 46 6c 67 20 69 73 20 74 72 75 resetFlg is tru
3f060 65 2c 20 74 68 65 6e 20 74 68 65 20 68 69 67 68 e, then the high
3f070 65 73 74 20 69 6e 73 74 61 6e 74 61 6e 65 6f 75 est instantaneou
3f080 73 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 72 65 s value is.** re
3f090 73 65 74 20 62 61 63 6b 20 64 6f 77 6e 20 74 6f set back down to
3f0a0 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c the current val
3f0b0 75 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c ue..**.** See al
3f0c0 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 so: [sqlite3_sta
3f0d0 74 75 73 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 tus()] and [sqli
3f0e0 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73 28 te3_stmt_status(
3f0f0 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 )]..*/.SQLITE_AP
3f100 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d I SQLITE_EXPERIM
3f110 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 ENTAL int sqlite
3f120 33 5f 64 62 5f 73 74 61 74 75 73 28 73 71 6c 69 3_db_status(sqli
3f130 74 65 33 2a 2c 20 69 6e 74 20 6f 70 2c 20 69 6e te3*, int op, in
3f140 74 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 48 t *pCur, int *pH
3f150 69 77 74 72 2c 20 69 6e 74 20 72 65 73 65 74 46 iwtr, int resetF
3f160 6c 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 lg);../*.** CAPI
3f170 33 52 45 46 3a 20 53 74 61 74 75 73 20 50 61 72 3REF: Status Par
3f180 61 6d 65 74 65 72 73 20 66 6f 72 20 64 61 74 61 ameters for data
3f190 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 base connections
3f1a0 20 7b 48 31 37 35 32 30 7d 20 3c 48 31 37 35 30 {H17520} <H1750
3f1b0 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 0>.** EXPERIMENT
3f1c0 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 AL.**.** These c
3f1d0 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 74 68 65 onstants are the
3f1e0 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 74 65 67 available integ
3f1f0 65 72 20 22 76 65 72 62 73 22 20 74 68 61 74 20 er "verbs" that
3f200 63 61 6e 20 62 65 20 70 61 73 73 65 64 20 61 73 can be passed as
3f210 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 20 61 .** the second a
3f220 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 5b rgument to the [
3f230 73 71 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 75 sqlite3_db_statu
3f240 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a s()] interface..
3f250 2a 2a 0a 2a 2a 20 4e 65 77 20 76 65 72 62 73 20 **.** New verbs
3f260 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 6e 20 may be added in
3f270 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 20 future releases
3f280 6f 66 20 53 51 4c 69 74 65 2e 20 45 78 69 73 74 of SQLite. Exist
3f290 69 6e 67 20 76 65 72 62 73 0a 2a 2a 20 6d 69 67 ing verbs.** mig
3f2a0 68 74 20 62 65 20 64 69 73 63 6f 6e 74 69 6e 75 ht be discontinu
3f2b0 65 64 2e 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 ed. Applications
3f2c0 20 73 68 6f 75 6c 64 20 63 68 65 63 6b 20 74 68 should check th
3f2d0 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 66 72 e return code fr
3f2e0 6f 6d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 64 om.** [sqlite3_d
3f2f0 62 5f 73 74 61 74 75 73 28 29 5d 20 74 6f 20 6d b_status()] to m
3f300 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68 ake sure that th
3f310 65 20 63 61 6c 6c 20 77 6f 72 6b 65 64 2e 0a 2a e call worked..*
3f320 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 64 * The [sqlite3_d
3f330 62 5f 73 74 61 74 75 73 28 29 5d 20 69 6e 74 65 b_status()] inte
3f340 72 66 61 63 65 20 77 69 6c 6c 20 72 65 74 75 72 rface will retur
3f350 6e 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 65 72 72 n a non-zero err
3f360 6f 72 20 63 6f 64 65 0a 2a 2a 20 69 66 20 61 20 or code.** if a
3f370 64 69 73 63 6f 6e 74 69 6e 75 65 64 20 6f 72 20 discontinued or
3f380 75 6e 73 75 70 70 6f 72 74 65 64 20 76 65 72 62 unsupported verb
3f390 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a is invoked..**.
3f3a0 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 ** <dl>.** <dt>S
3f3b0 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 4c QLITE_DBSTATUS_L
3f3c0 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 3c 2f 64 OOKASIDE_USED</d
3f3d0 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 t>.** <dd>This p
3f3e0 61 72 61 6d 65 74 65 72 20 72 65 74 75 72 6e 73 arameter returns
3f3f0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c the number of l
3f400 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 ookaside memory
3f410 73 6c 6f 74 73 20 63 75 72 72 65 6e 74 6c 79 0a slots currently.
3f420 2a 2a 20 63 68 65 63 6b 65 64 20 6f 75 74 2e 3c ** checked out.<
3f430 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f /dd>.** </dl>.*/
3f440 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
3f450 44 42 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 DBSTATUS_LOOKASI
3f460 44 45 5f 55 53 45 44 20 20 20 20 20 30 0a 0a 0a DE_USED 0...
3f470 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
3f480 50 72 65 70 61 72 65 64 20 53 74 61 74 65 6d 65 Prepared Stateme
3f490 6e 74 20 53 74 61 74 75 73 20 7b 48 31 37 35 35 nt Status {H1755
3f4a0 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 45 0} <S60200>.** E
3f4b0 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a XPERIMENTAL.**.*
3f4c0 2a 20 45 61 63 68 20 70 72 65 70 61 72 65 64 20 * Each prepared
3f4d0 73 74 61 74 65 6d 65 6e 74 20 6d 61 69 6e 74 61 statement mainta
3f4e0 69 6e 73 20 76 61 72 69 6f 75 73 0a 2a 2a 20 5b ins various.** [
3f4f0 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 SQLITE_STMTSTATU
3f500 53 5f 53 4f 52 54 20 7c 20 63 6f 75 6e 74 65 72 S_SORT | counter
3f510 73 5d 20 74 68 61 74 20 6d 65 61 73 75 72 65 20 s] that measure
3f520 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 the number.** of
3f530 20 74 69 6d 65 73 20 69 74 20 68 61 73 20 70 65 times it has pe
3f540 72 66 6f 72 6d 65 64 20 73 70 65 63 69 66 69 63 rformed specific
3f550 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20 54 68 operations. Th
3f560 65 73 65 20 63 6f 75 6e 74 65 72 73 20 63 61 6e ese counters can
3f570 0a 2a 2a 20 62 65 20 75 73 65 64 20 74 6f 20 6d .** be used to m
3f580 6f 6e 69 74 6f 72 20 74 68 65 20 70 65 72 66 6f onitor the perfo
3f590 72 6d 61 6e 63 65 20 63 68 61 72 61 63 74 65 72 rmance character
3f5a0 69 73 74 69 63 73 20 6f 66 20 74 68 65 20 70 72 istics of the pr
3f5b0 65 70 61 72 65 64 0a 2a 2a 20 73 74 61 74 65 6d epared.** statem
3f5c0 65 6e 74 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 ents. For examp
3f5d0 6c 65 2c 20 69 66 20 74 68 65 20 6e 75 6d 62 65 le, if the numbe
3f5e0 72 20 6f 66 20 74 61 62 6c 65 20 73 74 65 70 73 r of table steps
3f5f0 20 67 72 65 61 74 6c 79 20 65 78 63 65 65 64 73 greatly exceeds
3f600 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f .** the number o
3f610 66 20 74 61 62 6c 65 20 73 65 61 72 63 68 65 73 f table searches
3f620 20 6f 72 20 72 65 73 75 6c 74 20 72 6f 77 73 2c or result rows,
3f630 20 74 68 61 74 20 77 6f 75 6c 64 20 74 65 6e 64 that would tend
3f640 20 74 6f 20 69 6e 64 69 63 61 74 65 0a 2a 2a 20 to indicate.**
3f650 74 68 61 74 20 74 68 65 20 70 72 65 70 61 72 65 that the prepare
3f660 64 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 75 d statement is u
3f670 73 69 6e 67 20 61 20 66 75 6c 6c 20 74 61 62 6c sing a full tabl
3f680 65 20 73 63 61 6e 20 72 61 74 68 65 72 20 74 68 e scan rather th
3f690 61 6e 0a 2a 2a 20 61 6e 20 69 6e 64 65 78 2e 20 an.** an index.
3f6a0 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 .**.** This int
3f6b0 65 72 66 61 63 65 20 69 73 20 75 73 65 64 20 74 erface is used t
3f6c0 6f 20 72 65 74 72 69 65 76 65 20 61 6e 64 20 72 o retrieve and r
3f6d0 65 73 65 74 20 63 6f 75 6e 74 65 72 20 76 61 6c eset counter val
3f6e0 75 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 20 5b 70 ues from.** a [p
3f6f0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
3f700 74 5d 2e 20 20 54 68 65 20 66 69 72 73 74 20 61 t]. The first a
3f710 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 70 rgument is the p
3f720 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
3f730 74 0a 2a 2a 20 6f 62 6a 65 63 74 20 74 6f 20 62 t.** object to b
3f740 65 20 69 6e 74 65 72 72 6f 67 61 74 65 64 2e 20 e interrogated.
3f750 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 The second argu
3f760 6d 65 6e 74 0a 2a 2a 20 69 73 20 61 6e 20 69 6e ment.** is an in
3f770 74 65 67 65 72 20 63 6f 64 65 20 66 6f 72 20 61 teger code for a
3f780 20 73 70 65 63 69 66 69 63 20 5b 53 51 4c 49 54 specific [SQLIT
3f790 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 53 4f 52 E_STMTSTATUS_SOR
3f7a0 54 20 7c 20 63 6f 75 6e 74 65 72 5d 0a 2a 2a 20 T | counter].**
3f7b0 74 6f 20 62 65 20 69 6e 74 65 72 72 6f 67 61 74 to be interrogat
3f7c0 65 64 2e 20 0a 2a 2a 20 54 68 65 20 63 75 72 72 ed. .** The curr
3f7d0 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65 ent value of the
3f7e0 20 72 65 71 75 65 73 74 65 64 20 63 6f 75 6e 74 requested count
3f7f0 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a er is returned..
3f800 2a 2a 20 49 66 20 74 68 65 20 72 65 73 65 74 46 ** If the resetF
3f810 6c 67 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e lg is true, then
3f820 20 74 68 65 20 63 6f 75 6e 74 65 72 20 69 73 20 the counter is
3f830 72 65 73 65 74 20 74 6f 20 7a 65 72 6f 20 61 66 reset to zero af
3f840 74 65 72 20 74 68 69 73 0a 2a 2a 20 69 6e 74 65 ter this.** inte
3f850 72 66 61 63 65 20 63 61 6c 6c 20 72 65 74 75 72 rface call retur
3f860 6e 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c ns..**.** See al
3f870 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 so: [sqlite3_sta
3f880 74 75 73 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 tus()] and [sqli
3f890 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d te3_db_status()]
3f8a0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
3f8b0 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e SQLITE_EXPERIMEN
3f8c0 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f TAL int sqlite3_
3f8d0 73 74 6d 74 5f 73 74 61 74 75 73 28 73 71 6c 69 stmt_status(sqli
3f8e0 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 6f te3_stmt*, int o
3f8f0 70 2c 69 6e 74 20 72 65 73 65 74 46 6c 67 29 3b p,int resetFlg);
3f900 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
3f910 3a 20 53 74 61 74 75 73 20 50 61 72 61 6d 65 74 : Status Paramet
3f920 65 72 73 20 66 6f 72 20 70 72 65 70 61 72 65 64 ers for prepared
3f930 20 73 74 61 74 65 6d 65 6e 74 73 20 7b 48 31 37 statements {H17
3f940 35 37 30 7d 20 3c 48 31 37 35 35 30 3e 0a 2a 2a 570} <H17550>.**
3f950 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a EXPERIMENTAL.**
3f960 0a 2a 2a 20 54 68 65 73 65 20 70 72 65 70 72 6f .** These prepro
3f970 63 65 73 73 6f 72 20 6d 61 63 72 6f 73 20 64 65 cessor macros de
3f980 66 69 6e 65 20 69 6e 74 65 67 65 72 20 63 6f 64 fine integer cod
3f990 65 73 20 74 68 61 74 20 6e 61 6d 65 20 63 6f 75 es that name cou
3f9a0 6e 74 65 72 0a 2a 2a 20 76 61 6c 75 65 73 20 61 nter.** values a
3f9b0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
3f9c0 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 he [sqlite3_stmt
3f9d0 5f 73 74 61 74 75 73 28 29 5d 20 69 6e 74 65 72 _status()] inter
3f9e0 66 61 63 65 2e 0a 2a 2a 20 54 68 65 20 6d 65 61 face..** The mea
3f9f0 6e 69 6e 67 73 20 6f 66 20 74 68 65 20 76 61 72 nings of the var
3fa00 69 6f 75 73 20 63 6f 75 6e 74 65 72 73 20 61 72 ious counters ar
3fa10 65 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a e as follows:.**
3fa20 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e .** <dl>.** <dt>
3fa30 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 SQLITE_STMTSTATU
3fa40 53 5f 46 55 4c 4c 53 43 41 4e 5f 53 54 45 50 3c S_FULLSCAN_STEP<
3fa50 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 /dt>.** <dd>This
3fa60 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f is the number o
3fa70 66 20 74 69 6d 65 73 20 74 68 61 74 20 53 51 4c f times that SQL
3fa80 69 74 65 20 68 61 73 20 73 74 65 70 70 65 64 20 ite has stepped
3fa90 66 6f 72 77 61 72 64 20 69 6e 0a 2a 2a 20 61 20 forward in.** a
3faa0 74 61 62 6c 65 20 61 73 20 70 61 72 74 20 6f 66 table as part of
3fab0 20 61 20 66 75 6c 6c 20 74 61 62 6c 65 20 73 63 a full table sc
3fac0 61 6e 2e 20 20 4c 61 72 67 65 20 6e 75 6d 62 65 an. Large numbe
3fad0 72 73 20 66 6f 72 20 74 68 69 73 20 63 6f 75 6e rs for this coun
3fae0 74 65 72 0a 2a 2a 20 6d 61 79 20 69 6e 64 69 63 ter.** may indic
3faf0 61 74 65 20 6f 70 70 6f 72 74 75 6e 69 74 69 65 ate opportunitie
3fb00 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 s for performanc
3fb10 65 20 69 6d 70 72 6f 76 65 6d 65 6e 74 20 74 68 e improvement th
3fb20 72 6f 75 67 68 20 0a 2a 2a 20 63 61 72 65 66 75 rough .** carefu
3fb30 6c 20 75 73 65 20 6f 66 20 69 6e 64 69 63 65 73 l use of indices
3fb40 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 .</dd>.**.** <dt
3fb50 3e 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 >SQLITE_STMTSTAT
3fb60 55 53 5f 53 4f 52 54 3c 2f 64 74 3e 0a 2a 2a 20 US_SORT</dt>.**
3fb70 3c 64 64 3e 54 68 69 73 20 69 73 20 74 68 65 20 <dd>This is the
3fb80 6e 75 6d 62 65 72 20 6f 66 20 73 6f 72 74 20 6f number of sort o
3fb90 70 65 72 61 74 69 6f 6e 73 20 74 68 61 74 20 68 perations that h
3fba0 61 76 65 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a ave occurred..**
3fbb0 20 41 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 A non-zero valu
3fbc0 65 20 69 6e 20 74 68 69 73 20 63 6f 75 6e 74 65 e in this counte
3fbd0 72 20 6d 61 79 20 69 6e 64 69 63 61 74 65 20 61 r may indicate a
3fbe0 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 6f n opportunity to
3fbf0 0a 2a 2a 20 69 6d 70 72 6f 76 65 6d 65 6e 74 20 .** improvement
3fc00 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 68 72 6f performance thro
3fc10 75 67 68 20 63 61 72 65 66 75 6c 20 75 73 65 20 ugh careful use
3fc20 6f 66 20 69 6e 64 69 63 65 73 2e 3c 2f 64 64 3e of indices.</dd>
3fc30 0a 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a .**.** </dl>.*/.
3fc40 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 #define SQLITE_S
3fc50 54 4d 54 53 54 41 54 55 53 5f 46 55 4c 4c 53 43 TMTSTATUS_FULLSC
3fc60 41 4e 5f 53 54 45 50 20 20 20 20 20 31 0a 23 64 AN_STEP 1.#d
3fc70 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 4d efine SQLITE_STM
3fc80 54 53 54 41 54 55 53 5f 53 4f 52 54 20 20 20 20 TSTATUS_SORT
3fc90 20 20 20 20 20 20 20 20 20 20 32 0a 0a 2f 2a 0a 2../*.
3fca0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 75 73 ** CAPI3REF: Cus
3fcb0 74 6f 6d 20 50 61 67 65 20 43 61 63 68 65 20 4f tom Page Cache O
3fcc0 62 6a 65 63 74 0a 2a 2a 20 45 58 50 45 52 49 4d bject.** EXPERIM
3fcd0 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ENTAL.**.** The
3fce0 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 74 sqlite3_pcache t
3fcf0 79 70 65 20 69 73 20 6f 70 61 71 75 65 2e 20 20 ype is opaque.
3fd00 49 74 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 It is implemente
3fd10 64 20 62 79 0a 2a 2a 20 74 68 65 20 70 6c 75 67 d by.** the plug
3fd20 67 61 62 6c 65 20 6d 6f 64 75 6c 65 2e 20 20 54 gable module. T
3fd30 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 68 he SQLite core h
3fd40 61 73 20 6e 6f 20 6b 6e 6f 77 6c 65 64 67 65 20 as no knowledge
3fd50 6f 66 0a 2a 2a 20 69 74 73 20 73 69 7a 65 20 6f of.** its size o
3fd60 72 20 69 6e 74 65 72 6e 61 6c 20 73 74 72 75 63 r internal struc
3fd70 74 75 72 65 20 61 6e 64 20 6e 65 76 65 72 20 64 ture and never d
3fd80 65 61 6c 73 20 77 69 74 68 20 74 68 65 0a 2a 2a eals with the.**
3fd90 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 sqlite3_pcache
3fda0 6f 62 6a 65 63 74 20 65 78 63 65 70 74 20 62 79 object except by
3fdb0 20 68 6f 6c 64 69 6e 67 20 61 6e 64 20 70 61 73 holding and pas
3fdc0 73 69 6e 67 20 70 6f 69 6e 74 65 72 73 0a 2a 2a sing pointers.**
3fdd0 20 74 6f 20 74 68 65 20 6f 62 6a 65 63 74 2e 0a to the object..
3fde0 2a 2a 0a 2a 2a 20 53 65 65 20 5b 73 71 6c 69 74 **.** See [sqlit
3fdf0 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 e3_pcache_method
3fe00 73 5d 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 s] for additiona
3fe10 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a l information..*
3fe20 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
3fe30 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 sqlite3_pcache
3fe40 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 3b 0a sqlite3_pcache;.
3fe50 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
3fe60 20 41 70 70 6c 69 63 61 74 69 6f 6e 20 44 65 66 Application Def
3fe70 69 6e 65 64 20 50 61 67 65 20 43 61 63 68 65 2e ined Page Cache.
3fe80 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 70 .** KEYWORDS: {p
3fe90 61 67 65 20 63 61 63 68 65 7d 0a 2a 2a 20 45 58 age cache}.** EX
3fea0 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a PERIMENTAL.**.**
3feb0 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f The [sqlite3_co
3fec0 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43 4f nfig]([SQLITE_CO
3fed0 4e 46 49 47 5f 50 43 41 43 48 45 5d 2c 20 2e 2e NFIG_PCACHE], ..
3fee0 2e 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e .) interface can
3fef0 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 61 6e 20 .** register an
3ff00 61 6c 74 65 72 6e 61 74 69 76 65 20 70 61 67 65 alternative page
3ff10 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 cache implement
3ff20 61 74 69 6f 6e 20 62 79 20 70 61 73 73 69 6e 67 ation by passing
3ff30 20 69 6e 20 61 6e 20 0a 2a 2a 20 69 6e 73 74 61 in an .** insta
3ff40 6e 63 65 20 6f 66 20 74 68 65 20 73 71 6c 69 74 nce of the sqlit
3ff50 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 e3_pcache_method
3ff60 73 20 73 74 72 75 63 74 75 72 65 2e 20 54 68 65 s structure. The
3ff70 20 6d 61 6a 6f 72 69 74 79 20 6f 66 20 74 68 65 majority of the
3ff80 20 0a 2a 2a 20 68 65 61 70 20 6d 65 6d 6f 72 79 .** heap memory
3ff90 20 75 73 65 64 20 62 79 20 53 51 4c 69 74 65 20 used by SQLite
3ffa0 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 70 is used by the p
3ffb0 61 67 65 20 63 61 63 68 65 20 74 6f 20 63 61 63 age cache to cac
3ffc0 68 65 20 64 61 74 61 20 72 65 61 64 20 0a 2a 2a he data read .**
3ffd0 20 66 72 6f 6d 2c 20 6f 72 20 72 65 61 64 79 20 from, or ready
3ffe0 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f to be written to
3fff0 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 , the database f
40000 69 6c 65 2e 20 42 79 20 69 6d 70 6c 65 6d 65 6e ile. By implemen
40010 74 69 6e 67 20 61 20 0a 2a 2a 20 63 75 73 74 6f ting a .** custo
40020 6d 20 70 61 67 65 20 63 61 63 68 65 20 75 73 69 m page cache usi
40030 6e 67 20 74 68 69 73 20 41 50 49 2c 20 61 6e 20 ng this API, an
40040 61 70 70 6c 69 63 61 74 69 6f 6e 20 63 61 6e 20 application can
40050 63 6f 6e 74 72 6f 6c 20 6d 6f 72 65 20 0a 2a 2a control more .**
40060 20 70 72 65 63 69 73 65 6c 79 20 74 68 65 20 61 precisely the a
40070 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 mount of memory
40080 63 6f 6e 73 75 6d 65 64 20 62 79 20 53 51 4c 69 consumed by SQLi
40090 74 65 2c 20 74 68 65 20 77 61 79 20 69 6e 20 77 te, the way in w
400a0 68 69 63 68 20 0a 2a 2a 20 74 68 61 74 20 6d 65 hich .** that me
400b0 6d 6f 72 79 20 69 73 20 61 6c 6c 6f 63 61 74 65 mory is allocate
400c0 64 20 61 6e 64 20 72 65 6c 65 61 73 65 64 2c 20 d and released,
400d0 61 6e 64 20 74 68 65 20 70 6f 6c 69 63 69 65 73 and the policies
400e0 20 75 73 65 64 20 74 6f 20 0a 2a 2a 20 64 65 74 used to .** det
400f0 65 72 6d 69 6e 65 20 65 78 61 63 74 6c 79 20 77 ermine exactly w
40100 68 69 63 68 20 70 61 72 74 73 20 6f 66 20 61 20 hich parts of a
40110 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 72 database file ar
40120 65 20 63 61 63 68 65 64 20 61 6e 64 20 66 6f 72 e cached and for
40130 20 0a 2a 2a 20 68 6f 77 20 6c 6f 6e 67 2e 0a 2a .** how long..*
40140 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 *.** The content
40150 73 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 s of the sqlite3
40160 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 20 _pcache_methods
40170 73 74 72 75 63 74 75 72 65 20 61 72 65 20 63 6f structure are co
40180 70 69 65 64 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e pied to an.** in
40190 74 65 72 6e 61 6c 20 62 75 66 66 65 72 20 62 79 ternal buffer by
401a0 20 53 51 4c 69 74 65 20 77 69 74 68 69 6e 20 74 SQLite within t
401b0 68 65 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 he call to [sqli
401c0 74 65 33 5f 63 6f 6e 66 69 67 5d 2e 20 20 48 65 te3_config]. He
401d0 6e 63 65 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 nce.** the appli
401e0 63 61 74 69 6f 6e 20 6d 61 79 20 64 69 73 63 61 cation may disca
401f0 72 64 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 rd the parameter
40200 20 61 66 74 65 72 20 74 68 65 20 63 61 6c 6c 20 after the call
40210 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 to.** [sqlite3_c
40220 6f 6e 66 69 67 28 29 5d 20 72 65 74 75 72 6e 73 onfig()] returns
40230 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 49 6e 69 ..**.** The xIni
40240 74 28 29 20 6d 65 74 68 6f 64 20 69 73 20 63 61 t() method is ca
40250 6c 6c 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 lled once for ea
40260 63 68 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 ch call to [sqli
40270 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 te3_initialize()
40280 5d 0a 2a 2a 20 28 75 73 75 61 6c 6c 79 20 6f 6e ].** (usually on
40290 6c 79 20 6f 6e 63 65 20 64 75 72 69 6e 67 20 74 ly once during t
402a0 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 20 74 he lifetime of t
402b0 68 65 20 70 72 6f 63 65 73 73 29 2e 20 49 74 20 he process). It
402c0 69 73 20 70 61 73 73 65 64 0a 2a 2a 20 61 20 63 is passed.** a c
402d0 6f 70 79 20 6f 66 20 74 68 65 20 73 71 6c 69 74 opy of the sqlit
402e0 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 e3_pcache_method
402f0 73 2e 70 41 72 67 20 76 61 6c 75 65 2e 20 49 74 s.pArg value. It
40300 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 can be used to
40310 73 65 74 0a 2a 2a 20 75 70 20 67 6c 6f 62 61 6c set.** up global
40320 20 73 74 72 75 63 74 75 72 65 73 20 61 6e 64 20 structures and
40330 6d 75 74 65 78 65 73 20 72 65 71 75 69 72 65 64 mutexes required
40340 20 62 79 20 74 68 65 20 63 75 73 74 6f 6d 20 70 by the custom p
40350 61 67 65 20 63 61 63 68 65 20 0a 2a 2a 20 69 6d age cache .** im
40360 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 0a 2a plementation. .*
40370 2a 0a 2a 2a 20 54 68 65 20 78 53 68 75 74 64 6f *.** The xShutdo
40380 77 6e 28 29 20 6d 65 74 68 6f 64 20 69 73 20 63 wn() method is c
40390 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 alled from withi
403a0 6e 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 64 n [sqlite3_shutd
403b0 6f 77 6e 28 29 5d 2c 20 0a 2a 2a 20 69 66 20 74 own()], .** if t
403c0 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 69 he application i
403d0 6e 76 6f 6b 65 73 20 74 68 69 73 20 41 50 49 2e nvokes this API.
403e0 20 49 74 20 63 61 6e 20 62 65 20 75 73 65 64 20 It can be used
403f0 74 6f 20 63 6c 65 61 6e 20 75 70 20 0a 2a 2a 20 to clean up .**
40400 61 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 any outstanding
40410 72 65 73 6f 75 72 63 65 73 20 62 65 66 6f 72 65 resources before
40420 20 70 72 6f 63 65 73 73 20 73 68 75 74 64 6f 77 process shutdow
40430 6e 2c 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a n, if required..
40440 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 68 6f 6c **.** SQLite hol
40450 64 73 20 61 20 5b 53 51 4c 49 54 45 5f 4d 55 54 ds a [SQLITE_MUT
40460 45 58 5f 52 45 43 55 52 53 49 56 45 5d 20 6d 75 EX_RECURSIVE] mu
40470 74 65 78 20 77 68 65 6e 20 69 74 20 69 6e 76 6f tex when it invo
40480 6b 65 73 0a 2a 2a 20 74 68 65 20 78 49 6e 69 74 kes.** the xInit
40490 20 6d 65 74 68 6f 64 2c 20 73 6f 20 74 68 65 20 method, so the
404a0 78 49 6e 69 74 20 6d 65 74 68 6f 64 20 6e 65 65 xInit method nee
404b0 64 20 6e 6f 74 20 62 65 20 74 68 72 65 61 64 73 d not be threads
404c0 61 66 65 2e 20 20 54 68 65 0a 2a 2a 20 78 53 68 afe. The.** xSh
404d0 75 74 64 6f 77 6e 20 6d 65 74 68 6f 64 20 69 73 utdown method is
404e0 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72 6f only called fro
404f0 6d 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 64 m [sqlite3_shutd
40500 6f 77 6e 28 29 5d 20 73 6f 20 69 74 20 64 6f 65 own()] so it doe
40510 73 0a 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 74 6f s.** not need to
40520 20 62 65 20 74 68 72 65 61 64 73 61 66 65 20 65 be threadsafe e
40530 69 74 68 65 72 2e 20 20 41 6c 6c 20 6f 74 68 65 ither. All othe
40540 72 20 6d 65 74 68 6f 64 73 20 6d 75 73 74 20 62 r methods must b
40550 65 20 74 68 72 65 61 64 73 61 66 65 0a 2a 2a 20 e threadsafe.**
40560 69 6e 20 6d 75 6c 74 69 74 68 72 65 61 64 65 64 in multithreaded
40570 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 2e 0a 2a applications..*
40580 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c *.** SQLite will
40590 20 6e 65 76 65 72 20 69 6e 76 6f 6b 65 20 78 49 never invoke xI
405a0 6e 69 74 28 29 20 6d 6f 72 65 20 74 68 61 6e 20 nit() more than
405b0 6f 6e 63 65 20 77 69 74 68 6f 75 74 20 61 6e 20 once without an
405c0 69 6e 74 65 72 76 65 6e 69 6e 67 0a 2a 2a 20 63 intervening.** c
405d0 61 6c 6c 20 74 6f 20 78 53 68 75 74 64 6f 77 6e all to xShutdown
405e0 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 43 ()..**.** The xC
405f0 72 65 61 74 65 28 29 20 6d 65 74 68 6f 64 20 69 reate() method i
40600 73 20 75 73 65 64 20 74 6f 20 63 6f 6e 73 74 72 s used to constr
40610 75 63 74 20 61 20 6e 65 77 20 63 61 63 68 65 20 uct a new cache
40620 69 6e 73 74 61 6e 63 65 2e 20 20 53 51 4c 69 74 instance. SQLit
40630 65 0a 2a 2a 20 77 69 6c 6c 20 74 79 70 69 63 61 e.** will typica
40640 6c 6c 79 20 63 72 65 61 74 65 20 6f 6e 65 20 63 lly create one c
40650 61 63 68 65 20 69 6e 73 74 61 6e 63 65 20 66 6f ache instance fo
40660 72 20 65 61 63 68 20 6f 70 65 6e 20 64 61 74 61 r each open data
40670 62 61 73 65 20 66 69 6c 65 2c 0a 2a 2a 20 74 68 base file,.** th
40680 6f 75 67 68 20 74 68 69 73 20 69 73 20 6e 6f 74 ough this is not
40690 20 67 75 61 72 61 6e 74 65 65 64 2e 20 54 68 65 guaranteed. The
406a0 0a 2a 2a 20 66 69 72 73 74 20 70 61 72 61 6d 65 .** first parame
406b0 74 65 72 2c 20 73 7a 50 61 67 65 2c 20 69 73 20 ter, szPage, is
406c0 74 68 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 the size in byte
406d0 73 20 6f 66 20 74 68 65 20 70 61 67 65 73 20 74 s of the pages t
406e0 68 61 74 20 6d 75 73 74 0a 2a 2a 20 62 65 20 61 hat must.** be a
406f0 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 65 20 llocated by the
40700 63 61 63 68 65 2e 20 20 73 7a 50 61 67 65 20 77 cache. szPage w
40710 69 6c 6c 20 6e 6f 74 20 62 65 20 61 20 70 6f 77 ill not be a pow
40720 65 72 20 6f 66 20 74 77 6f 2e 20 20 73 7a 50 61 er of two. szPa
40730 67 65 0a 2a 2a 20 77 69 6c 6c 20 74 68 65 20 70 ge.** will the p
40740 61 67 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 age size of the
40750 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 68 database file th
40760 61 74 20 69 73 20 74 6f 20 62 65 20 63 61 63 68 at is to be cach
40770 65 64 20 70 6c 75 73 20 61 6e 0a 2a 2a 20 69 6e ed plus an.** in
40780 63 72 65 6d 65 6e 74 20 28 68 65 72 65 20 63 61 crement (here ca
40790 6c 6c 65 64 20 22 52 22 29 20 6f 66 20 61 62 6f lled "R") of abo
407a0 75 74 20 31 30 30 20 6f 72 20 32 30 30 2e 20 20 ut 100 or 200.
407b0 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 73 65 20 SQLite will use
407c0 74 68 65 0a 2a 2a 20 65 78 74 72 61 20 52 20 62 the.** extra R b
407d0 79 74 65 73 20 6f 6e 20 65 61 63 68 20 70 61 67 ytes on each pag
407e0 65 20 74 6f 20 73 74 6f 72 65 20 6d 65 74 61 64 e to store metad
407f0 61 74 61 20 61 62 6f 75 74 20 74 68 65 20 75 6e ata about the un
40800 64 65 72 6c 79 69 6e 67 0a 2a 2a 20 64 61 74 61 derlying.** data
40810 62 61 73 65 20 70 61 67 65 20 6f 6e 20 64 69 73 base page on dis
40820 6b 2e 20 20 54 68 65 20 76 61 6c 75 65 20 6f 66 k. The value of
40830 20 52 20 64 65 70 65 6e 64 73 0a 2a 2a 20 6f 6e R depends.** on
40840 20 74 68 65 20 53 51 4c 69 74 65 20 76 65 72 73 the SQLite vers
40850 69 6f 6e 2c 20 74 68 65 20 74 61 72 67 65 74 20 ion, the target
40860 70 6c 61 74 66 6f 72 6d 2c 20 61 6e 64 20 68 6f platform, and ho
40870 77 20 53 51 4c 69 74 65 20 77 61 73 20 63 6f 6d w SQLite was com
40880 70 69 6c 65 64 2e 0a 2a 2a 20 52 20 69 73 20 63 piled..** R is c
40890 6f 6e 73 74 61 6e 74 20 66 6f 72 20 61 20 70 61 onstant for a pa
408a0 72 74 69 63 75 6c 61 72 20 62 75 69 6c 64 20 6f rticular build o
408b0 66 20 53 51 4c 69 74 65 2e 20 20 54 68 65 20 73 f SQLite. The s
408c0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 econd argument t
408d0 6f 0a 2a 2a 20 78 43 72 65 61 74 65 28 29 2c 20 o.** xCreate(),
408e0 62 50 75 72 67 65 61 62 6c 65 2c 20 69 73 20 74 bPurgeable, is t
408f0 72 75 65 20 69 66 20 74 68 65 20 63 61 63 68 65 rue if the cache
40900 20 62 65 69 6e 67 20 63 72 65 61 74 65 64 20 77 being created w
40910 69 6c 6c 0a 2a 2a 20 62 65 20 75 73 65 64 20 74 ill.** be used t
40920 6f 20 63 61 63 68 65 20 64 61 74 61 62 61 73 65 o cache database
40930 20 70 61 67 65 73 20 6f 66 20 61 20 66 69 6c 65 pages of a file
40940 20 73 74 6f 72 65 64 20 6f 6e 20 64 69 73 6b 2c stored on disk,
40950 20 6f 72 0a 2a 2a 20 66 61 6c 73 65 20 69 66 20 or.** false if
40960 69 74 20 69 73 20 75 73 65 64 20 66 6f 72 20 61 it is used for a
40970 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 n in-memory data
40980 62 61 73 65 2e 20 54 68 65 20 63 61 63 68 65 20 base. The cache
40990 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a implementation.*
409a0 2a 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 * does not have
409b0 74 6f 20 64 6f 20 61 6e 79 74 68 69 6e 67 20 73 to do anything s
409c0 70 65 63 69 61 6c 20 62 61 73 65 64 20 77 69 74 pecial based wit
409d0 68 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 62 h the value of b
409e0 50 75 72 67 65 61 62 6c 65 3b 0a 2a 2a 20 69 74 Purgeable;.** it
409f0 20 69 73 20 70 75 72 65 6c 79 20 61 64 76 69 73 is purely advis
40a00 6f 72 79 2e 20 20 4f 6e 20 61 20 63 61 63 68 65 ory. On a cache
40a10 20 77 68 65 72 65 20 62 50 75 72 67 65 61 62 6c where bPurgeabl
40a20 65 20 69 73 20 66 61 6c 73 65 2c 20 53 51 4c 69 e is false, SQLi
40a30 74 65 20 77 69 6c 6c 0a 2a 2a 20 6e 65 76 65 72 te will.** never
40a40 20 69 6e 76 6f 6b 65 20 78 55 6e 70 69 6e 28 29 invoke xUnpin()
40a50 20 65 78 63 65 70 74 20 74 6f 20 64 65 6c 69 62 except to delib
40a60 65 72 61 74 65 6c 79 20 64 65 6c 65 74 65 20 61 erately delete a
40a70 20 70 61 67 65 2e 0a 2a 2a 20 49 6e 20 6f 74 68 page..** In oth
40a80 65 72 20 77 6f 72 64 73 2c 20 61 20 63 61 63 68 er words, a cach
40a90 65 20 63 72 65 61 74 65 64 20 77 69 74 68 20 62 e created with b
40aa0 50 75 72 67 65 61 62 6c 65 20 73 65 74 20 74 6f Purgeable set to
40ab0 20 66 61 6c 73 65 20 77 69 6c 6c 0a 2a 2a 20 6e false will.** n
40ac0 65 76 65 72 20 63 6f 6e 74 61 69 6e 20 61 6e 79 ever contain any
40ad0 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 2e unpinned pages.
40ae0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 43 61 63 68 .**.** The xCach
40af0 65 73 69 7a 65 28 29 20 6d 65 74 68 6f 64 20 6d esize() method m
40b00 61 79 20 62 65 20 63 61 6c 6c 65 64 20 61 74 20 ay be called at
40b10 61 6e 79 20 74 69 6d 65 20 62 79 20 53 51 4c 69 any time by SQLi
40b20 74 65 20 74 6f 20 73 65 74 20 74 68 65 0a 2a 2a te to set the.**
40b30 20 73 75 67 67 65 73 74 65 64 20 6d 61 78 69 6d suggested maxim
40b40 75 6d 20 63 61 63 68 65 2d 73 69 7a 65 20 28 6e um cache-size (n
40b50 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 73 umber of pages s
40b60 74 6f 72 65 64 20 62 79 29 20 74 68 65 20 63 61 tored by) the ca
40b70 63 68 65 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 che.** instance
40b80 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69 passed as the fi
40b90 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 54 68 rst argument. Th
40ba0 69 73 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 is is the value
40bb0 63 6f 6e 66 69 67 75 72 65 64 20 75 73 69 6e 67 configured using
40bc0 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20 22 .** the SQLite "
40bd0 5b 50 52 41 47 4d 41 20 63 61 63 68 65 5f 73 69 [PRAGMA cache_si
40be0 7a 65 5d 22 20 63 6f 6d 6d 61 6e 64 2e 20 41 73 ze]" command. As
40bf0 20 77 69 74 68 20 74 68 65 20 62 50 75 72 67 65 with the bPurge
40c00 61 62 6c 65 20 70 61 72 61 6d 65 74 65 72 2c 0a able parameter,.
40c10 2a 2a 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 ** the implement
40c20 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20 72 65 71 ation is not req
40c30 75 69 72 65 64 20 74 6f 20 64 6f 20 61 6e 79 74 uired to do anyt
40c40 68 69 6e 67 20 77 69 74 68 20 74 68 69 73 0a 2a hing with this.*
40c50 2a 20 76 61 6c 75 65 3b 20 69 74 20 69 73 20 61 * value; it is a
40c60 64 76 69 73 6f 72 79 20 6f 6e 6c 79 2e 0a 2a 2a dvisory only..**
40c70 0a 2a 2a 20 54 68 65 20 78 50 61 67 65 63 6f 75 .** The xPagecou
40c80 6e 74 28 29 20 6d 65 74 68 6f 64 20 73 68 6f 75 nt() method shou
40c90 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75 ld return the nu
40ca0 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 63 75 mber of pages cu
40cb0 72 72 65 6e 74 6c 79 0a 2a 2a 20 73 74 6f 72 65 rrently.** store
40cc0 64 20 69 6e 20 74 68 65 20 63 61 63 68 65 2e 0a d in the cache..
40cd0 2a 2a 20 0a 2a 2a 20 54 68 65 20 78 46 65 74 63 ** .** The xFetc
40ce0 68 28 29 20 6d 65 74 68 6f 64 20 69 73 20 75 73 h() method is us
40cf0 65 64 20 74 6f 20 66 65 74 63 68 20 61 20 70 61 ed to fetch a pa
40d00 67 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 ge and return a
40d10 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 0a pointer to it. .
40d20 2a 2a 20 41 20 27 70 61 67 65 27 2c 20 69 6e 20 ** A 'page', in
40d30 74 68 69 73 20 63 6f 6e 74 65 78 74 2c 20 69 73 this context, is
40d40 20 61 20 62 75 66 66 65 72 20 6f 66 20 73 7a 50 a buffer of szP
40d50 61 67 65 20 62 79 74 65 73 20 61 6c 69 67 6e 65 age bytes aligne
40d60 64 20 61 74 20 61 6e 0a 2a 2a 20 38 2d 62 79 74 d at an.** 8-byt
40d70 65 20 62 6f 75 6e 64 61 72 79 2e 20 54 68 65 20 e boundary. The
40d80 70 61 67 65 20 74 6f 20 62 65 20 66 65 74 63 68 page to be fetch
40d90 65 64 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 ed is determined
40da0 20 62 79 20 74 68 65 20 6b 65 79 2e 20 54 68 65 by the key. The
40db0 0a 2a 2a 20 6d 69 6d 69 6d 75 6d 20 6b 65 79 20 .** mimimum key
40dc0 76 61 6c 75 65 20 69 73 20 31 2e 20 41 66 74 65 value is 1. Afte
40dd0 72 20 69 74 20 68 61 73 20 62 65 65 6e 20 72 65 r it has been re
40de0 74 72 69 65 76 65 64 20 75 73 69 6e 67 20 78 46 trieved using xF
40df0 65 74 63 68 2c 20 74 68 65 20 70 61 67 65 20 0a etch, the page .
40e00 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 ** is considered
40e10 20 74 6f 20 62 65 20 22 70 69 6e 6e 65 64 22 2e to be "pinned".
40e20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 .**.** If the re
40e30 71 75 65 73 74 65 64 20 70 61 67 65 20 69 73 20 quested page is
40e40 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 70 already in the p
40e50 61 67 65 20 63 61 63 68 65 2c 20 74 68 65 6e 20 age cache, then
40e60 74 68 65 20 70 61 67 65 20 63 61 63 68 65 0a 2a the page cache.*
40e70 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * implementation
40e80 20 6d 75 73 74 20 72 65 74 75 72 6e 20 61 20 70 must return a p
40e90 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61 ointer to the pa
40ea0 67 65 20 62 75 66 66 65 72 20 77 69 74 68 20 69 ge buffer with i
40eb0 74 73 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 69 6e ts content.** in
40ec0 74 61 63 74 2e 20 20 49 66 20 74 68 65 20 72 65 tact. If the re
40ed0 71 75 65 73 74 65 64 20 70 61 67 65 20 69 73 20 quested page is
40ee0 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 6e 20 74 not already in t
40ef0 68 65 20 63 61 63 68 65 2c 20 74 68 65 6e 20 74 he cache, then t
40f00 68 65 0a 2a 2a 20 62 65 68 61 76 69 6f 72 20 6f he.** behavior o
40f10 66 20 74 68 65 20 63 61 63 68 65 20 69 6d 70 6c f the cache impl
40f20 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 64 65 ementation is de
40f30 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 termined by the
40f40 76 61 6c 75 65 20 6f 66 20 74 68 65 0a 2a 2a 20 value of the.**
40f50 63 72 65 61 74 65 46 6c 61 67 20 70 61 72 61 6d createFlag param
40f60 65 74 65 72 20 70 61 73 73 65 64 20 74 6f 20 78 eter passed to x
40f70 46 65 74 63 68 2c 20 61 63 63 6f 72 64 69 6e 67 Fetch, according
40f80 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e to the followin
40f90 67 20 74 61 62 6c 65 3a 0a 2a 2a 0a 2a 2a 20 3c g table:.**.** <
40fa0 74 61 62 6c 65 20 62 6f 72 64 65 72 3d 31 20 77 table border=1 w
40fb0 69 64 74 68 3d 38 35 25 20 61 6c 69 67 6e 3d 63 idth=85% align=c
40fc0 65 6e 74 65 72 3e 0a 2a 2a 20 3c 74 72 3e 3c 74 enter>.** <tr><t
40fd0 68 3e 20 63 72 65 61 74 65 46 6c 61 67 20 3c 74 h> createFlag <t
40fe0 68 3e 20 42 65 68 61 76 69 6f 75 72 20 77 68 65 h> Behaviour whe
40ff0 6e 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c n page is not al
41000 72 65 61 64 79 20 69 6e 20 63 61 63 68 65 0a 2a ready in cache.*
41010 2a 20 3c 74 72 3e 3c 74 64 3e 20 30 20 3c 74 64 * <tr><td> 0 <td
41020 3e 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 > Do not allocat
41030 65 20 61 20 6e 65 77 20 70 61 67 65 2e 20 20 52 e a new page. R
41040 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a 20 3c eturn NULL..** <
41050 74 72 3e 3c 74 64 3e 20 31 20 3c 74 64 3e 20 41 tr><td> 1 <td> A
41060 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 61 llocate a new pa
41070 67 65 20 69 66 20 69 74 20 65 61 73 79 20 61 6e ge if it easy an
41080 64 20 63 6f 6e 76 65 6e 69 65 6e 74 20 74 6f 20 d convenient to
41090 64 6f 20 73 6f 2e 0a 2a 2a 20 20 20 20 20 20 20 do so..**
410a0 20 20 20 20 20 20 20 20 20 20 4f 74 68 65 72 77 Otherw
410b0 69 73 65 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e ise return NULL.
410c0 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 32 20 3c .** <tr><td> 2 <
410d0 74 64 3e 20 4d 61 6b 65 20 65 76 65 72 79 20 65 td> Make every e
410e0 66 66 6f 72 74 20 74 6f 20 61 6c 6c 6f 63 61 74 ffort to allocat
410f0 65 20 61 20 6e 65 77 20 70 61 67 65 2e 20 20 4f e a new page. O
41100 6e 6c 79 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 nly return.**
41110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 NU
41120 4c 4c 20 69 66 20 61 6c 6c 6f 63 61 74 69 6e 67 LL if allocating
41130 20 61 20 6e 65 77 20 70 61 67 65 20 69 73 20 65 a new page is e
41140 66 66 65 63 74 69 76 65 6c 79 20 69 6d 70 6f 73 ffectively impos
41150 73 69 62 6c 65 2e 0a 2a 2a 20 3c 2f 74 61 62 6c sible..** </tabl
41160 65 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 e>.**.** SQLite
41170 77 69 6c 6c 20 6e 6f 72 6d 61 6c 6c 79 20 69 6e will normally in
41180 76 6f 6b 65 20 78 46 65 74 63 68 28 29 20 77 69 voke xFetch() wi
41190 74 68 20 61 20 63 72 65 61 74 65 46 6c 61 67 20 th a createFlag
411a0 6f 66 20 30 20 6f 72 20 31 2e 20 20 49 66 0a 2a of 0 or 1. If.*
411b0 2a 20 61 20 63 61 6c 6c 20 74 6f 20 78 46 65 74 * a call to xFet
411c0 63 68 28 29 20 77 69 74 68 20 63 72 65 61 74 65 ch() with create
411d0 46 6c 61 67 3d 3d 31 20 72 65 74 75 72 6e 73 20 Flag==1 returns
411e0 4e 55 4c 4c 2c 20 74 68 65 6e 20 53 51 4c 69 74 NULL, then SQLit
411f0 65 20 77 69 6c 6c 0a 2a 2a 20 61 74 74 65 6d 70 e will.** attemp
41200 74 20 74 6f 20 75 6e 70 69 6e 20 6f 6e 65 20 6f t to unpin one o
41210 72 20 6d 6f 72 65 20 63 61 63 68 65 20 70 61 67 r more cache pag
41220 65 73 20 62 79 20 73 70 69 6c 6c 69 6e 67 20 74 es by spilling t
41230 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a he content of.**
41240 20 70 69 6e 6e 65 64 20 70 61 67 65 73 20 74 6f pinned pages to
41250 20 64 69 73 6b 20 61 6e 64 20 73 79 6e 63 68 69 disk and synchi
41260 6e 67 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 ng the operating
41270 20 73 79 73 74 65 6d 20 64 69 73 6b 20 63 61 63 system disk cac
41280 68 65 2e 20 41 66 74 65 72 0a 2a 2a 20 61 74 74 he. After.** att
41290 65 6d 70 74 69 6e 67 20 74 6f 20 75 6e 70 69 6e empting to unpin
412a0 20 70 61 67 65 73 2c 20 74 68 65 20 78 46 65 74 pages, the xFet
412b0 63 68 28 29 20 6d 65 74 68 6f 64 20 77 69 6c 6c ch() method will
412c0 20 62 65 20 69 6e 76 6f 6b 65 64 20 61 67 61 69 be invoked agai
412d0 6e 20 77 69 74 68 0a 2a 2a 20 61 20 63 72 65 61 n with.** a crea
412e0 74 65 46 6c 61 67 20 6f 66 20 32 2e 0a 2a 2a 0a teFlag of 2..**.
412f0 2a 2a 20 78 55 6e 70 69 6e 28 29 20 69 73 20 63 ** xUnpin() is c
41300 61 6c 6c 65 64 20 62 79 20 53 51 4c 69 74 65 20 alled by SQLite
41310 77 69 74 68 20 61 20 70 6f 69 6e 74 65 72 20 74 with a pointer t
41320 6f 20 61 20 63 75 72 72 65 6e 74 6c 79 20 70 69 o a currently pi
41330 6e 6e 65 64 20 70 61 67 65 0a 2a 2a 20 61 73 20 nned page.** as
41340 69 74 73 20 73 65 63 6f 6e 64 20 61 72 67 75 6d its second argum
41350 65 6e 74 2e 20 49 66 20 74 68 65 20 74 68 69 72 ent. If the thir
41360 64 20 70 61 72 61 6d 65 74 65 72 2c 20 64 69 73 d parameter, dis
41370 63 61 72 64 2c 20 69 73 20 6e 6f 6e 2d 7a 65 72 card, is non-zer
41380 6f 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 70 o,.** then the p
41390 61 67 65 20 73 68 6f 75 6c 64 20 62 65 20 65 76 age should be ev
413a0 69 63 74 65 64 20 66 72 6f 6d 20 74 68 65 20 63 icted from the c
413b0 61 63 68 65 2e 20 49 6e 20 74 68 69 73 20 63 61 ache. In this ca
413c0 73 65 20 53 51 4c 69 74 65 20 0a 2a 2a 20 61 73 se SQLite .** as
413d0 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 20 6e sumes that the n
413e0 65 78 74 20 74 69 6d 65 20 74 68 65 20 70 61 67 ext time the pag
413f0 65 20 69 73 20 72 65 74 72 69 65 76 65 64 20 66 e is retrieved f
41400 72 6f 6d 20 74 68 65 20 63 61 63 68 65 20 75 73 rom the cache us
41410 69 6e 67 0a 2a 2a 20 74 68 65 20 78 46 65 74 63 ing.** the xFetc
41420 68 28 29 20 6d 65 74 68 6f 64 2c 20 69 74 20 77 h() method, it w
41430 69 6c 6c 20 62 65 20 7a 65 72 6f 65 64 2e 20 49 ill be zeroed. I
41440 66 20 74 68 65 20 64 69 73 63 61 72 64 20 70 61 f the discard pa
41450 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 7a 65 rameter is.** ze
41460 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 70 61 67 ro, then the pag
41470 65 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 e is considered
41480 74 6f 20 62 65 20 75 6e 70 69 6e 6e 65 64 2e 20 to be unpinned.
41490 54 68 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d The cache implem
414a0 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6d 61 79 20 entation.** may
414b0 63 68 6f 6f 73 65 20 74 6f 20 65 76 69 63 74 20 choose to evict
414c0 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 61 unpinned pages a
414d0 74 20 61 6e 79 20 74 69 6d 65 2e 0a 2a 2a 0a 2a t any time..**.*
414e0 2a 20 54 68 65 20 63 61 63 68 65 20 69 73 20 6e * The cache is n
414f0 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 70 ot required to p
41500 65 72 66 6f 72 6d 20 61 6e 79 20 72 65 66 65 72 erform any refer
41510 65 6e 63 65 20 63 6f 75 6e 74 69 6e 67 2e 20 41 ence counting. A
41520 20 73 69 6e 67 6c 65 20 0a 2a 2a 20 63 61 6c 6c single .** call
41530 20 74 6f 20 78 55 6e 70 69 6e 28 29 20 75 6e 70 to xUnpin() unp
41540 69 6e 73 20 74 68 65 20 70 61 67 65 20 72 65 67 ins the page reg
41550 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 6e ardless of the n
41560 75 6d 62 65 72 20 6f 66 20 70 72 69 6f 72 20 63 umber of prior c
41570 61 6c 6c 73 20 0a 2a 2a 20 74 6f 20 78 46 65 74 alls .** to xFet
41580 63 68 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ch()..**.** The
41590 78 52 65 6b 65 79 28 29 20 6d 65 74 68 6f 64 20 xRekey() method
415a0 69 73 20 75 73 65 64 20 74 6f 20 63 68 61 6e 67 is used to chang
415b0 65 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 20 e the key value
415c0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
415d0 74 68 65 0a 2a 2a 20 70 61 67 65 20 70 61 73 73 the.** page pass
415e0 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 ed as the second
415f0 20 61 72 67 75 6d 65 6e 74 20 66 72 6f 6d 20 6f argument from o
41600 6c 64 4b 65 79 20 74 6f 20 6e 65 77 4b 65 79 2e ldKey to newKey.
41610 20 49 66 20 74 68 65 20 63 61 63 68 65 0a 2a 2a If the cache.**
41620 20 70 72 65 76 69 6f 75 73 6c 79 20 63 6f 6e 74 previously cont
41630 61 69 6e 73 20 61 6e 20 65 6e 74 72 79 20 61 73 ains an entry as
41640 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 6e 65 sociated with ne
41650 77 4b 65 79 2c 20 69 74 20 73 68 6f 75 6c 64 20 wKey, it should
41660 62 65 0a 2a 2a 20 64 69 73 63 61 72 64 65 64 2e be.** discarded.
41670 20 41 6e 79 20 70 72 69 6f 72 20 63 61 63 68 65 Any prior cache
41680 20 65 6e 74 72 79 20 61 73 73 6f 63 69 61 74 65 entry associate
41690 64 20 77 69 74 68 20 6e 65 77 4b 65 79 20 69 73 d with newKey is
416a0 20 67 75 61 72 61 6e 74 65 65 64 20 6e 6f 74 0a guaranteed not.
416b0 2a 2a 20 74 6f 20 62 65 20 70 69 6e 6e 65 64 2e ** to be pinned.
416c0 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 53 51 4c 69 .**.** When SQLi
416d0 74 65 20 63 61 6c 6c 73 20 74 68 65 20 78 54 72 te calls the xTr
416e0 75 6e 63 61 74 65 28 29 20 6d 65 74 68 6f 64 2c uncate() method,
416f0 20 74 68 65 20 63 61 63 68 65 20 6d 75 73 74 20 the cache must
41700 64 69 73 63 61 72 64 20 61 6c 6c 0a 2a 2a 20 65 discard all.** e
41710 78 69 73 74 69 6e 67 20 63 61 63 68 65 20 65 6e xisting cache en
41720 74 72 69 65 73 20 77 69 74 68 20 70 61 67 65 20 tries with page
41730 6e 75 6d 62 65 72 73 20 28 6b 65 79 73 29 20 67 numbers (keys) g
41740 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 reater than or e
41750 71 75 61 6c 0a 2a 2a 20 74 6f 20 74 68 65 20 76 qual.** to the v
41760 61 6c 75 65 20 6f 66 20 74 68 65 20 69 4c 69 6d alue of the iLim
41770 69 74 20 70 61 72 61 6d 65 74 65 72 20 70 61 73 it parameter pas
41780 73 65 64 20 74 6f 20 78 54 72 75 6e 63 61 74 65 sed to xTruncate
41790 28 29 2e 20 49 66 20 61 6e 79 0a 2a 2a 20 6f 66 (). If any.** of
417a0 20 74 68 65 73 65 20 70 61 67 65 73 20 61 72 65 these pages are
417b0 20 70 69 6e 6e 65 64 2c 20 74 68 65 79 20 61 72 pinned, they ar
417c0 65 20 69 6d 70 6c 69 63 69 74 6c 79 20 75 6e 70 e implicitly unp
417d0 69 6e 6e 65 64 2c 20 6d 65 61 6e 69 6e 67 20 74 inned, meaning t
417e0 68 61 74 0a 2a 2a 20 74 68 65 79 20 63 61 6e 20 hat.** they can
417f0 62 65 20 73 61 66 65 6c 79 20 64 69 73 63 61 72 be safely discar
41800 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 ded..**.** The x
41810 44 65 73 74 72 6f 79 28 29 20 6d 65 74 68 6f 64 Destroy() method
41820 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 6c 65 is used to dele
41830 74 65 20 61 20 63 61 63 68 65 20 61 6c 6c 6f 63 te a cache alloc
41840 61 74 65 64 20 62 79 20 78 43 72 65 61 74 65 28 ated by xCreate(
41850 29 2e 0a 2a 2a 20 41 6c 6c 20 72 65 73 6f 75 72 )..** All resour
41860 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 ces associated w
41870 69 74 68 20 74 68 65 20 73 70 65 63 69 66 69 65 ith the specifie
41880 64 20 63 61 63 68 65 20 73 68 6f 75 6c 64 20 62 d cache should b
41890 65 20 66 72 65 65 64 2e 20 41 66 74 65 72 0a 2a e freed. After.*
418a0 2a 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 78 44 * calling the xD
418b0 65 73 74 72 6f 79 28 29 20 6d 65 74 68 6f 64 2c estroy() method,
418c0 20 53 51 4c 69 74 65 20 63 6f 6e 73 69 64 65 72 SQLite consider
418d0 73 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 70 s the [sqlite3_p
418e0 63 61 63 68 65 2a 5d 0a 2a 2a 20 68 61 6e 64 6c cache*].** handl
418f0 65 20 69 6e 76 61 6c 69 64 2c 20 61 6e 64 20 77 e invalid, and w
41900 69 6c 6c 20 6e 6f 74 20 75 73 65 20 69 74 20 77 ill not use it w
41910 69 74 68 20 61 6e 79 20 6f 74 68 65 72 20 73 71 ith any other sq
41920 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 lite3_pcache_met
41930 68 6f 64 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e hods.** function
41940 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 s..*/.typedef st
41950 72 75 63 74 20 73 71 6c 69 74 65 33 5f 70 63 61 ruct sqlite3_pca
41960 63 68 65 5f 6d 65 74 68 6f 64 73 20 73 71 6c 69 che_methods sqli
41970 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f te3_pcache_metho
41980 64 73 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 74 ds;.struct sqlit
41990 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 e3_pcache_method
419a0 73 20 7b 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 s {. void *pArg
419b0 3b 0a 20 20 69 6e 74 20 28 2a 78 49 6e 69 74 29 ;. int (*xInit)
419c0 28 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 (void*);. void
419d0 28 2a 78 53 68 75 74 64 6f 77 6e 29 28 76 6f 69 (*xShutdown)(voi
419e0 64 2a 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 70 d*);. sqlite3_p
419f0 63 61 63 68 65 20 2a 28 2a 78 43 72 65 61 74 65 cache *(*xCreate
41a00 29 28 69 6e 74 20 73 7a 50 61 67 65 2c 20 69 6e )(int szPage, in
41a10 74 20 62 50 75 72 67 65 61 62 6c 65 29 3b 0a 20 t bPurgeable);.
41a20 20 76 6f 69 64 20 28 2a 78 43 61 63 68 65 73 69 void (*xCachesi
41a30 7a 65 29 28 73 71 6c 69 74 65 33 5f 70 63 61 63 ze)(sqlite3_pcac
41a40 68 65 2a 2c 20 69 6e 74 20 6e 43 61 63 68 65 73 he*, int nCaches
41a50 69 7a 65 29 3b 0a 20 20 69 6e 74 20 28 2a 78 50 ize);. int (*xP
41a60 61 67 65 63 6f 75 6e 74 29 28 73 71 6c 69 74 65 agecount)(sqlite
41a70 33 5f 70 63 61 63 68 65 2a 29 3b 0a 20 20 76 6f 3_pcache*);. vo
41a80 69 64 20 2a 28 2a 78 46 65 74 63 68 29 28 73 71 id *(*xFetch)(sq
41a90 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 2c 20 75 lite3_pcache*, u
41aa0 6e 73 69 67 6e 65 64 20 6b 65 79 2c 20 69 6e 74 nsigned key, int
41ab0 20 63 72 65 61 74 65 46 6c 61 67 29 3b 0a 20 20 createFlag);.
41ac0 76 6f 69 64 20 28 2a 78 55 6e 70 69 6e 29 28 73 void (*xUnpin)(s
41ad0 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 2c 20 qlite3_pcache*,
41ae0 76 6f 69 64 2a 2c 20 69 6e 74 20 64 69 73 63 61 void*, int disca
41af0 72 64 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 52 rd);. void (*xR
41b00 65 6b 65 79 29 28 73 71 6c 69 74 65 33 5f 70 63 ekey)(sqlite3_pc
41b10 61 63 68 65 2a 2c 20 76 6f 69 64 2a 2c 20 75 6e ache*, void*, un
41b20 73 69 67 6e 65 64 20 6f 6c 64 4b 65 79 2c 20 75 signed oldKey, u
41b30 6e 73 69 67 6e 65 64 20 6e 65 77 4b 65 79 29 3b nsigned newKey);
41b40 0a 20 20 76 6f 69 64 20 28 2a 78 54 72 75 6e 63 . void (*xTrunc
41b50 61 74 65 29 28 73 71 6c 69 74 65 33 5f 70 63 61 ate)(sqlite3_pca
41b60 63 68 65 2a 2c 20 75 6e 73 69 67 6e 65 64 20 69 che*, unsigned i
41b70 4c 69 6d 69 74 29 3b 0a 20 20 76 6f 69 64 20 28 Limit);. void (
41b80 2a 78 44 65 73 74 72 6f 79 29 28 73 71 6c 69 74 *xDestroy)(sqlit
41b90 65 33 5f 70 63 61 63 68 65 2a 29 3b 0a 7d 3b 0a e3_pcache*);.};.
41ba0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
41bb0 20 4f 6e 6c 69 6e 65 20 42 61 63 6b 75 70 20 4f Online Backup O
41bc0 62 6a 65 63 74 0a 2a 2a 20 45 58 50 45 52 49 4d bject.** EXPERIM
41bd0 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ENTAL.**.** The
41be0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 6f sqlite3_backup o
41bf0 62 6a 65 63 74 20 72 65 63 6f 72 64 73 20 73 74 bject records st
41c00 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 ate information
41c10 61 62 6f 75 74 20 61 6e 20 6f 6e 67 6f 69 6e 67 about an ongoing
41c20 0a 2a 2a 20 6f 6e 6c 69 6e 65 20 62 61 63 6b 75 .** online backu
41c30 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 54 68 p operation. Th
41c40 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 e sqlite3_backup
41c50 20 6f 62 6a 65 63 74 20 69 73 20 63 72 65 61 74 object is creat
41c60 65 64 20 62 79 0a 2a 2a 20 61 20 63 61 6c 6c 20 ed by.** a call
41c70 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b to [sqlite3_back
41c80 75 70 5f 69 6e 69 74 28 29 5d 20 61 6e 64 20 69 up_init()] and i
41c90 73 20 64 65 73 74 72 6f 79 65 64 20 62 79 20 61 s destroyed by a
41ca0 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c call to.** [sql
41cb0 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 ite3_backup_fini
41cc0 73 68 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 sh()]..**.** See
41cd0 20 41 6c 73 6f 3a 20 5b 55 73 69 6e 67 20 74 68 Also: [Using th
41ce0 65 20 53 51 4c 69 74 65 20 4f 6e 6c 69 6e 65 20 e SQLite Online
41cf0 42 61 63 6b 75 70 20 41 50 49 5d 0a 2a 2f 0a 74 Backup API].*/.t
41d00 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 ypedef struct sq
41d10 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 73 71 6c lite3_backup sql
41d20 69 74 65 33 5f 62 61 63 6b 75 70 3b 0a 0a 2f 2a ite3_backup;../*
41d30 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 6e .** CAPI3REF: On
41d40 6c 69 6e 65 20 42 61 63 6b 75 70 20 41 50 49 2e line Backup API.
41d50 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c .** EXPERIMENTAL
41d60 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 .**.** This API
41d70 69 73 20 75 73 65 64 20 74 6f 20 6f 76 65 72 77 is used to overw
41d80 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 rite the content
41d90 73 20 6f 66 20 6f 6e 65 20 64 61 74 61 62 61 73 s of one databas
41da0 65 20 77 69 74 68 20 74 68 61 74 0a 2a 2a 20 6f e with that.** o
41db0 66 20 61 6e 6f 74 68 65 72 2e 20 49 74 20 69 73 f another. It is
41dc0 20 75 73 65 66 75 6c 20 65 69 74 68 65 72 20 66 useful either f
41dd0 6f 72 20 63 72 65 61 74 69 6e 67 20 62 61 63 6b or creating back
41de0 75 70 73 20 6f 66 20 64 61 74 61 62 61 73 65 73 ups of databases
41df0 20 6f 72 0a 2a 2a 20 66 6f 72 20 63 6f 70 79 69 or.** for copyi
41e00 6e 67 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 ng in-memory dat
41e10 61 62 61 73 65 73 20 74 6f 20 6f 72 20 66 72 6f abases to or fro
41e20 6d 20 70 65 72 73 69 73 74 65 6e 74 20 66 69 6c m persistent fil
41e30 65 73 2e 20 0a 2a 2a 0a 2a 2a 20 53 65 65 20 41 es. .**.** See A
41e40 6c 73 6f 3a 20 5b 55 73 69 6e 67 20 74 68 65 20 lso: [Using the
41e50 53 51 4c 69 74 65 20 4f 6e 6c 69 6e 65 20 42 61 SQLite Online Ba
41e60 63 6b 75 70 20 41 50 49 5d 0a 2a 2a 0a 2a 2a 20 ckup API].**.**
41e70 45 78 63 6c 75 73 69 76 65 20 61 63 63 65 73 73 Exclusive access
41e80 20 69 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 is required to
41e90 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 the destination
41ea0 64 61 74 61 62 61 73 65 20 66 6f 72 20 74 68 65 database for the
41eb0 20 0a 2a 2a 20 64 75 72 61 74 69 6f 6e 20 6f 66 .** duration of
41ec0 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20 the operation.
41ed0 48 6f 77 65 76 65 72 20 74 68 65 20 73 6f 75 72 However the sour
41ee0 63 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6f ce database is o
41ef0 6e 6c 79 0a 2a 2a 20 72 65 61 64 2d 6c 6f 63 6b nly.** read-lock
41f00 65 64 20 77 68 69 6c 65 20 69 74 20 69 73 20 61 ed while it is a
41f10 63 74 75 61 6c 6c 79 20 62 65 69 6e 67 20 72 65 ctually being re
41f20 61 64 2c 20 69 74 20 69 73 20 6e 6f 74 20 6c 6f ad, it is not lo
41f30 63 6b 65 64 0a 2a 2a 20 63 6f 6e 74 69 6e 75 6f cked.** continuo
41f40 75 73 6c 79 20 66 6f 72 20 74 68 65 20 65 6e 74 usly for the ent
41f50 69 72 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20 54 ire operation. T
41f60 68 75 73 2c 20 74 68 65 20 62 61 63 6b 75 70 20 hus, the backup
41f70 6d 61 79 20 62 65 0a 2a 2a 20 70 65 72 66 6f 72 may be.** perfor
41f80 6d 65 64 20 6f 6e 20 61 20 6c 69 76 65 20 64 61 med on a live da
41f90 74 61 62 61 73 65 20 77 69 74 68 6f 75 74 20 70 tabase without p
41fa0 72 65 76 65 6e 74 69 6e 67 20 6f 74 68 65 72 20 reventing other
41fb0 75 73 65 72 73 20 66 72 6f 6d 0a 2a 2a 20 77 72 users from.** wr
41fc0 69 74 69 6e 67 20 74 6f 20 74 68 65 20 64 61 74 iting to the dat
41fd0 61 62 61 73 65 20 66 6f 72 20 61 6e 20 65 78 74 abase for an ext
41fe0 65 6e 64 65 64 20 70 65 72 69 6f 64 20 6f 66 20 ended period of
41ff0 74 69 6d 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 6f 20 time..** .** To
42000 70 65 72 66 6f 72 6d 20 61 20 62 61 63 6b 75 70 perform a backup
42010 20 6f 70 65 72 61 74 69 6f 6e 3a 20 0a 2a 2a 20 operation: .**
42020 20 20 3c 6f 6c 3e 0a 2a 2a 20 20 20 20 20 3c 6c <ol>.** <l
42030 69 3e 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 i><b>sqlite3_bac
42040 6b 75 70 5f 69 6e 69 74 28 29 3c 2f 62 3e 20 69 kup_init()</b> i
42050 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 74 6f s called once to
42060 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 0a initialize the.
42070 2a 2a 20 20 20 20 20 20 20 20 20 62 61 63 6b 75 ** backu
42080 70 2c 20 0a 2a 2a 20 20 20 20 20 3c 6c 69 3e 3c p, .** <li><
42090 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 b>sqlite3_backup
420a0 5f 73 74 65 70 28 29 3c 2f 62 3e 20 69 73 20 63 _step()</b> is c
420b0 61 6c 6c 65 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 alled one or mor
420c0 65 20 74 69 6d 65 73 20 74 6f 20 74 72 61 6e 73 e times to trans
420d0 66 65 72 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 fer .**
420e0 74 68 65 20 64 61 74 61 20 62 65 74 77 65 65 6e the data between
420f0 20 74 68 65 20 74 77 6f 20 64 61 74 61 62 61 73 the two databas
42100 65 73 2c 20 61 6e 64 20 66 69 6e 61 6c 6c 79 0a es, and finally.
42110 2a 2a 20 20 20 20 20 3c 6c 69 3e 3c 62 3e 73 71 ** <li><b>sq
42120 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e lite3_backup_fin
42130 69 73 68 28 29 3c 2f 62 3e 20 69 73 20 63 61 6c ish()</b> is cal
42140 6c 65 64 20 74 6f 20 72 65 6c 65 61 73 65 20 61 led to release a
42150 6c 6c 20 72 65 73 6f 75 72 63 65 73 20 0a 2a 2a ll resources .**
42160 20 20 20 20 20 20 20 20 20 61 73 73 6f 63 69 61 associa
42170 74 65 64 20 77 69 74 68 20 74 68 65 20 62 61 63 ted with the bac
42180 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20 0a kup operation. .
42190 2a 2a 20 20 20 3c 2f 6f 6c 3e 0a 2a 2a 20 54 68 ** </ol>.** Th
421a0 65 72 65 20 73 68 6f 75 6c 64 20 62 65 20 65 78 ere should be ex
421b0 61 63 74 6c 79 20 6f 6e 65 20 63 61 6c 6c 20 74 actly one call t
421c0 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 o sqlite3_backup
421d0 5f 66 69 6e 69 73 68 28 29 20 66 6f 72 20 65 61 _finish() for ea
421e0 63 68 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c ch.** successful
421f0 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
42200 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 2e 0a _backup_init()..
42210 2a 2a 0a 2a 2a 20 3c 62 3e 73 71 6c 69 74 65 33 **.** <b>sqlite3
42220 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 3c 2f _backup_init()</
42230 62 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 b>.**.** The fir
42240 73 74 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 st two arguments
42250 20 70 61 73 73 65 64 20 74 6f 20 5b 73 71 6c 69 passed to [sqli
42260 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 te3_backup_init(
42270 29 5d 20 61 72 65 20 74 68 65 20 64 61 74 61 62 )] are the datab
42280 61 73 65 0a 2a 2a 20 68 61 6e 64 6c 65 20 61 73 ase.** handle as
42290 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 sociated with th
422a0 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 e destination da
422b0 74 61 62 61 73 65 20 61 6e 64 20 74 68 65 20 64 tabase and the d
422c0 61 74 61 62 61 73 65 20 6e 61 6d 65 20 0a 2a 2a atabase name .**
422d0 20 75 73 65 64 20 74 6f 20 61 74 74 61 63 68 20 used to attach
422e0 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 the destination
422f0 64 61 74 61 62 61 73 65 20 74 6f 20 74 68 65 20 database to the
42300 68 61 6e 64 6c 65 2e 20 54 68 65 20 64 61 74 61 handle. The data
42310 62 61 73 65 20 6e 61 6d 65 0a 2a 2a 20 69 73 20 base name.** is
42320 22 6d 61 69 6e 22 20 66 6f 72 20 74 68 65 20 6d "main" for the m
42330 61 69 6e 20 64 61 74 61 62 61 73 65 2c 20 22 74 ain database, "t
42340 65 6d 70 22 20 66 6f 72 20 74 68 65 20 74 65 6d emp" for the tem
42350 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65 2c porary database,
42360 20 6f 72 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20 or.** the name
42370 73 70 65 63 69 66 69 65 64 20 61 73 20 70 61 72 specified as par
42380 74 20 6f 66 20 74 68 65 20 5b 41 54 54 41 43 48 t of the [ATTACH
42390 5d 20 73 74 61 74 65 6d 65 6e 74 20 69 66 20 74 ] statement if t
423a0 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 he destination i
423b0 73 0a 2a 2a 20 61 6e 20 61 74 74 61 63 68 65 64 s.** an attached
423c0 20 64 61 74 61 62 61 73 65 2e 20 54 68 65 20 74 database. The t
423d0 68 69 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20 hird and fourth
423e0 61 72 67 75 6d 65 6e 74 73 20 70 61 73 73 65 64 arguments passed
423f0 20 74 6f 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f to .** sqlite3_
42400 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 20 69 64 backup_init() id
42410 65 6e 74 69 66 79 20 74 68 65 20 5b 64 61 74 61 entify the [data
42420 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d base connection]
42430 0a 2a 2a 20 61 6e 64 20 64 61 74 61 62 61 73 65 .** and database
42440 20 6e 61 6d 65 20 75 73 65 64 0a 2a 2a 20 74 6f name used.** to
42450 20 61 63 63 65 73 73 20 74 68 65 20 73 6f 75 72 access the sour
42460 63 65 20 64 61 74 61 62 61 73 65 2e 20 54 68 65 ce database. The
42470 20 76 61 6c 75 65 73 20 70 61 73 73 65 64 20 66 values passed f
42480 6f 72 20 74 68 65 20 73 6f 75 72 63 65 20 61 6e or the source an
42490 64 20 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f d .** destinatio
424a0 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e n [database conn
424b0 65 63 74 69 6f 6e 5d 20 70 61 72 61 6d 65 74 65 ection] paramete
424c0 72 73 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 74 rs must not be t
424d0 68 65 20 73 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 49 he same..**.** I
424e0 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
424f0 73 20 77 69 74 68 69 6e 20 73 71 6c 69 74 65 33 s within sqlite3
42500 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 2c 20 _backup_init(),
42510 74 68 65 6e 20 4e 55 4c 4c 20 69 73 20 72 65 74 then NULL is ret
42520 75 72 6e 65 64 0a 2a 2a 20 61 6e 64 20 61 6e 20 urned.** and an
42530 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 65 error code and e
42540 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77 72 69 rror message wri
42550 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 5b 64 tten into the [d
42560 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
42570 6f 6e 5d 20 0a 2a 2a 20 70 61 73 73 65 64 20 61 on] .** passed a
42580 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 s the first argu
42590 6d 65 6e 74 2e 20 54 68 65 79 20 6d 61 79 20 62 ment. They may b
425a0 65 20 72 65 74 72 69 65 76 65 64 20 75 73 69 6e e retrieved usin
425b0 67 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 g the.** [sqlite
425c0 33 5f 65 72 72 63 6f 64 65 28 29 5d 2c 20 5b 73 3_errcode()], [s
425d0 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d qlite3_errmsg()]
425e0 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 65 , and [sqlite3_e
425f0 72 72 6d 73 67 31 36 28 29 5d 20 66 75 6e 63 74 rrmsg16()] funct
42600 69 6f 6e 73 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 ions..** Otherwi
42610 73 65 2c 20 69 66 20 73 75 63 63 65 73 73 66 75 se, if successfu
42620 6c 2c 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 l, a pointer to
42630 61 6e 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b an [sqlite3_back
42640 75 70 5d 20 6f 62 6a 65 63 74 20 69 73 0a 2a 2a up] object is.**
42650 20 72 65 74 75 72 6e 65 64 2e 20 54 68 69 73 20 returned. This
42660 70 6f 69 6e 74 65 72 20 6d 61 79 20 62 65 20 75 pointer may be u
42670 73 65 64 20 77 69 74 68 20 74 68 65 20 73 71 6c sed with the sql
42680 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 ite3_backup_step
42690 28 29 20 61 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 () and.** sqlite
426a0 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 3_backup_finish(
426b0 29 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20 70 ) functions to p
426c0 65 72 66 6f 72 6d 20 74 68 65 20 73 70 65 63 69 erform the speci
426d0 66 69 65 64 20 62 61 63 6b 75 70 20 0a 2a 2a 20 fied backup .**
426e0 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a operation..**.**
426f0 20 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b <b>sqlite3_back
42700 75 70 5f 73 74 65 70 28 29 3c 2f 62 3e 0a 2a 2a up_step()</b>.**
42710 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 5b 73 71 .** Function [sq
42720 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 lite3_backup_ste
42730 70 28 29 5d 20 69 73 20 75 73 65 64 20 74 6f 20 p()] is used to
42740 63 6f 70 79 20 75 70 20 74 6f 20 6e 50 61 67 65 copy up to nPage
42750 20 70 61 67 65 73 20 62 65 74 77 65 65 6e 20 0a pages between .
42760 2a 2a 20 74 68 65 20 73 6f 75 72 63 65 20 61 6e ** the source an
42770 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 d destination da
42780 74 61 62 61 73 65 73 2c 20 77 68 65 72 65 20 6e tabases, where n
42790 50 61 67 65 20 69 73 20 74 68 65 20 76 61 6c 75 Page is the valu
427a0 65 20 6f 66 20 74 68 65 20 0a 2a 2a 20 73 65 63 e of the .** sec
427b0 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 70 61 ond parameter pa
427c0 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33 5f ssed to sqlite3_
427d0 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 20 49 backup_step(). I
427e0 66 20 6e 50 61 67 65 20 69 73 20 61 20 6e 65 67 f nPage is a neg
427f0 61 74 69 76 65 0a 2a 2a 20 76 61 6c 75 65 2c 20 ative.** value,
42800 61 6c 6c 20 72 65 6d 61 69 6e 69 6e 67 20 73 6f all remaining so
42810 75 72 63 65 20 70 61 67 65 73 20 61 72 65 20 63 urce pages are c
42820 6f 70 69 65 64 2e 20 49 66 20 74 68 65 20 72 65 opied. If the re
42830 71 75 69 72 65 64 20 70 61 67 65 73 20 61 72 65 quired pages are
42840 20 0a 2a 2a 20 73 75 63 63 65 73 66 75 6c 6c 79 .** succesfully
42850 20 63 6f 70 69 65 64 2c 20 62 75 74 20 74 68 65 copied, but the
42860 72 65 20 61 72 65 20 73 74 69 6c 6c 20 6d 6f 72 re are still mor
42870 65 20 70 61 67 65 73 20 74 6f 20 63 6f 70 79 20 e pages to copy
42880 62 65 66 6f 72 65 20 74 68 65 20 0a 2a 2a 20 62 before the .** b
42890 61 63 6b 75 70 20 69 73 20 63 6f 6d 70 6c 65 74 ackup is complet
428a0 65 2c 20 69 74 20 72 65 74 75 72 6e 73 20 5b 53 e, it returns [S
428b0 51 4c 49 54 45 5f 4f 4b 5d 2e 20 49 66 20 6e 6f QLITE_OK]. If no
428c0 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64 20 61 error occured a
428d0 6e 64 20 74 68 65 72 65 20 0a 2a 2a 20 61 72 65 nd there .** are
428e0 20 6e 6f 20 6d 6f 72 65 20 70 61 67 65 73 20 74 no more pages t
428f0 6f 20 63 6f 70 79 2c 20 74 68 65 6e 20 5b 53 51 o copy, then [SQ
42900 4c 49 54 45 5f 44 4f 4e 45 5d 20 69 73 20 72 65 LITE_DONE] is re
42910 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 65 72 turned. If an er
42920 72 6f 72 20 0a 2a 2a 20 6f 63 63 75 72 73 2c 20 ror .** occurs,
42930 74 68 65 6e 20 61 6e 20 53 51 4c 69 74 65 20 65 then an SQLite e
42940 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 rror code is ret
42950 75 72 6e 65 64 2e 20 41 73 20 77 65 6c 6c 20 61 urned. As well a
42960 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 61 6e s [SQLITE_OK] an
42970 64 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 4f 4e d.** [SQLITE_DON
42980 45 5d 2c 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 E], a call to sq
42990 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 lite3_backup_ste
429a0 70 28 29 20 6d 61 79 20 72 65 74 75 72 6e 20 5b p() may return [
429b0 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5d SQLITE_READONLY]
429c0 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d ,.** [SQLITE_NOM
429d0 45 4d 5d 2c 20 5b 53 51 4c 49 54 45 5f 42 55 53 EM], [SQLITE_BUS
429e0 59 5d 2c 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b Y], [SQLITE_LOCK
429f0 45 44 5d 2c 20 6f 72 20 61 6e 0a 2a 2a 20 5b 53 ED], or an.** [S
42a00 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 QLITE_IOERR_ACCE
42a10 53 53 20 7c 20 53 51 4c 49 54 45 5f 49 4f 45 52 SS | SQLITE_IOER
42a20 52 5f 58 58 58 5d 20 65 78 74 65 6e 64 65 64 20 R_XXX] extended
42a30 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a 0a 2a error code..**.*
42a40 2a 20 41 73 20 77 65 6c 6c 20 61 73 20 74 68 65 * As well as the
42a50 20 63 61 73 65 20 77 68 65 72 65 20 74 68 65 20 case where the
42a60 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 destination data
42a70 62 61 73 65 20 66 69 6c 65 20 77 61 73 20 6f 70 base file was op
42a80 65 6e 65 64 20 66 6f 72 0a 2a 2a 20 72 65 61 64 ened for.** read
42a90 2d 6f 6e 6c 79 20 61 63 63 65 73 73 2c 20 73 71 -only access, sq
42aa0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 lite3_backup_ste
42ab0 70 28 29 20 6d 61 79 20 72 65 74 75 72 6e 20 5b p() may return [
42ac0 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5d SQLITE_READONLY]
42ad0 20 69 66 0a 2a 2a 20 74 68 65 20 64 65 73 74 69 if.** the desti
42ae0 6e 61 74 69 6f 6e 20 69 73 20 61 6e 20 69 6e 2d nation is an in-
42af0 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 memory database
42b00 77 69 74 68 20 61 20 64 69 66 66 65 72 65 6e 74 with a different
42b10 20 70 61 67 65 20 73 69 7a 65 0a 2a 2a 20 66 72 page size.** fr
42b20 6f 6d 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 om the source da
42b30 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 tabase..**.** If
42b40 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
42b50 73 74 65 70 28 29 20 63 61 6e 6e 6f 74 20 6f 62 step() cannot ob
42b60 74 61 69 6e 20 61 20 72 65 71 75 69 72 65 64 20 tain a required
42b70 66 69 6c 65 2d 73 79 73 74 65 6d 20 6c 6f 63 6b file-system lock
42b80 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 5b 73 , then.** the [s
42b90 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 qlite3_busy_hand
42ba0 6c 65 72 20 7c 20 62 75 73 79 2d 68 61 6e 64 6c ler | busy-handl
42bb0 65 72 20 66 75 6e 63 74 69 6f 6e 5d 0a 2a 2a 20 er function].**
42bc0 69 73 20 69 6e 76 6f 6b 65 64 20 28 69 66 20 6f is invoked (if o
42bd0 6e 65 20 69 73 20 73 70 65 63 69 66 69 65 64 29 ne is specified)
42be0 2e 20 49 66 20 74 68 65 20 0a 2a 2a 20 62 75 73 . If the .** bus
42bf0 79 2d 68 61 6e 64 6c 65 72 20 72 65 74 75 72 6e y-handler return
42c00 73 20 6e 6f 6e 2d 7a 65 72 6f 20 62 65 66 6f 72 s non-zero befor
42c10 65 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 61 76 e the lock is av
42c20 61 69 6c 61 62 6c 65 2c 20 74 68 65 6e 20 0a 2a ailable, then .*
42c30 2a 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 * [SQLITE_BUSY]
42c40 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 is returned to t
42c50 68 65 20 63 61 6c 6c 65 72 2e 20 49 6e 20 74 68 he caller. In th
42c60 69 73 20 63 61 73 65 20 74 68 65 20 63 61 6c 6c is case the call
42c70 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 to.** sqlite3_b
42c80 61 63 6b 75 70 5f 73 74 65 70 28 29 20 63 61 6e ackup_step() can
42c90 20 62 65 20 72 65 74 72 69 65 64 20 6c 61 74 65 be retried late
42ca0 72 2e 20 49 66 20 74 68 65 20 73 6f 75 72 63 65 r. If the source
42cb0 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f .** [database co
42cc0 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 69 73 20 nnection].** is
42cd0 62 65 69 6e 67 20 75 73 65 64 20 74 6f 20 77 72 being used to wr
42ce0 69 74 65 20 74 6f 20 74 68 65 20 73 6f 75 72 63 ite to the sourc
42cf0 65 20 64 61 74 61 62 61 73 65 20 77 68 65 6e 20 e database when
42d00 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 sqlite3_backup_s
42d10 74 65 70 28 29 0a 2a 2a 20 69 73 20 63 61 6c 6c tep().** is call
42d20 65 64 2c 20 74 68 65 6e 20 5b 53 51 4c 49 54 45 ed, then [SQLITE
42d30 5f 4c 4f 43 4b 45 44 5d 20 69 73 20 72 65 74 75 _LOCKED] is retu
42d40 72 6e 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 rned immediately
42d50 2e 20 41 67 61 69 6e 2c 20 69 6e 20 74 68 69 73 . Again, in this
42d60 0a 2a 2a 20 63 61 73 65 20 74 68 65 20 63 61 6c .** case the cal
42d70 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 l to sqlite3_bac
42d80 6b 75 70 5f 73 74 65 70 28 29 20 63 61 6e 20 62 kup_step() can b
42d90 65 20 72 65 74 72 69 65 64 20 6c 61 74 65 72 20 e retried later
42da0 6f 6e 2e 20 49 66 0a 2a 2a 20 5b 53 51 4c 49 54 on. If.** [SQLIT
42db0 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 53 20 7c E_IOERR_ACCESS |
42dc0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 58 58 SQLITE_IOERR_XX
42dd0 58 5d 2c 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 X], [SQLITE_NOME
42de0 4d 5d 2c 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54 M], or.** [SQLIT
42df0 45 5f 52 45 41 44 4f 4e 4c 59 5d 20 69 73 20 72 E_READONLY] is r
42e00 65 74 75 72 6e 65 64 2c 20 74 68 65 6e 20 0a 2a eturned, then .*
42e10 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20 70 6f * there is no po
42e20 69 6e 74 20 69 6e 20 72 65 74 72 79 69 6e 67 20 int in retrying
42e30 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 the call to sqli
42e40 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 te3_backup_step(
42e50 29 2e 20 54 68 65 73 65 20 0a 2a 2a 20 65 72 72 ). These .** err
42e60 6f 72 73 20 61 72 65 20 63 6f 6e 73 69 64 65 72 ors are consider
42e70 65 64 20 66 61 74 61 6c 2e 20 41 74 20 74 68 69 ed fatal. At thi
42e80 73 20 70 6f 69 6e 74 20 74 68 65 20 61 70 70 6c s point the appl
42e90 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 61 63 63 ication must acc
42ea0 65 70 74 20 0a 2a 2a 20 74 68 61 74 20 74 68 65 ept .** that the
42eb0 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f backup operatio
42ec0 6e 20 68 61 73 20 66 61 69 6c 65 64 20 61 6e 64 n has failed and
42ed0 20 70 61 73 73 20 74 68 65 20 62 61 63 6b 75 70 pass the backup
42ee0 20 6f 70 65 72 61 74 69 6f 6e 20 68 61 6e 64 6c operation handl
42ef0 65 20 0a 2a 2a 20 74 6f 20 74 68 65 20 73 71 6c e .** to the sql
42f00 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 ite3_backup_fini
42f10 73 68 28 29 20 74 6f 20 72 65 6c 65 61 73 65 20 sh() to release
42f20 61 73 73 6f 63 69 61 74 65 64 20 72 65 73 6f 75 associated resou
42f30 72 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 6c 6c rces..**.** Foll
42f40 6f 77 69 6e 67 20 74 68 65 20 66 69 72 73 74 20 owing the first
42f50 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f call to sqlite3_
42f60 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2c 20 61 backup_step(), a
42f70 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b n exclusive lock
42f80 20 69 73 0a 2a 2a 20 6f 62 74 61 69 6e 65 64 20 is.** obtained
42f90 6f 6e 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 on the destinati
42fa0 6f 6e 20 66 69 6c 65 2e 20 49 74 20 69 73 20 6e on file. It is n
42fb0 6f 74 20 72 65 6c 65 61 73 65 64 20 75 6e 74 69 ot released unti
42fc0 6c 20 65 69 74 68 65 72 20 0a 2a 2a 20 73 71 6c l either .** sql
42fd0 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 ite3_backup_fini
42fe0 73 68 28 29 20 69 73 20 63 61 6c 6c 65 64 20 6f sh() is called o
42ff0 72 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 r the backup ope
43000 72 61 74 69 6f 6e 20 69 73 20 63 6f 6d 70 6c 65 ration is comple
43010 74 65 20 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74 te .** and sqlit
43020 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 e3_backup_step()
43030 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 returns [SQLITE
43040 5f 44 4f 4e 45 5d 2e 20 41 64 64 69 74 69 6f 6e _DONE]. Addition
43050 61 6c 6c 79 2c 20 65 61 63 68 20 74 69 6d 65 20 ally, each time
43060 0a 2a 2a 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 .** a call to sq
43070 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 lite3_backup_ste
43080 70 28 29 20 69 73 20 6d 61 64 65 20 61 20 5b 73 p() is made a [s
43090 68 61 72 65 64 20 6c 6f 63 6b 5d 20 69 73 20 6f hared lock] is o
430a0 62 74 61 69 6e 65 64 20 6f 6e 0a 2a 2a 20 74 68 btained on.** th
430b0 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 e source databas
430c0 65 20 66 69 6c 65 2e 20 54 68 69 73 20 6c 6f 63 e file. This loc
430d0 6b 20 69 73 20 72 65 6c 65 61 73 65 64 20 62 65 k is released be
430e0 66 6f 72 65 20 74 68 65 0a 2a 2a 20 73 71 6c 69 fore the.** sqli
430f0 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 te3_backup_step(
43100 29 20 63 61 6c 6c 20 72 65 74 75 72 6e 73 2e 20 ) call returns.
43110 42 65 63 61 75 73 65 20 74 68 65 20 73 6f 75 72 Because the sour
43120 63 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6e ce database is n
43130 6f 74 0a 2a 2a 20 6c 6f 63 6b 65 64 20 62 65 74 ot.** locked bet
43140 77 65 65 6e 20 63 61 6c 6c 73 20 74 6f 20 73 71 ween calls to sq
43150 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 lite3_backup_ste
43160 70 28 29 2c 20 69 74 20 6d 61 79 20 62 65 20 6d p(), it may be m
43170 6f 64 69 66 69 65 64 20 6d 69 64 2d 77 61 79 0a odified mid-way.
43180 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 65 20 62 ** through the b
43190 61 63 6b 75 70 20 70 72 6f 63 65 64 75 72 65 2e ackup procedure.
431a0 20 49 66 20 74 68 65 20 73 6f 75 72 63 65 20 64 If the source d
431b0 61 74 61 62 61 73 65 20 69 73 20 6d 6f 64 69 66 atabase is modif
431c0 69 65 64 20 62 79 20 61 6e 0a 2a 2a 20 65 78 74 ied by an.** ext
431d0 65 72 6e 61 6c 20 70 72 6f 63 65 73 73 20 6f 72 ernal process or
431e0 20 76 69 61 20 61 20 64 61 74 61 62 61 73 65 20 via a database
431f0 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 74 68 65 72 connection other
43200 20 74 68 61 6e 20 74 68 65 20 6f 6e 65 20 62 65 than the one be
43210 69 6e 67 0a 2a 2a 20 75 73 65 64 20 62 79 20 74 ing.** used by t
43220 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 he backup operat
43230 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 62 61 ion, then the ba
43240 63 6b 75 70 20 77 69 6c 6c 20 62 65 20 74 72 61 ckup will be tra
43250 6e 73 70 61 72 65 6e 74 6c 79 0a 2a 2a 20 72 65 nsparently.** re
43260 73 74 61 72 74 65 64 20 62 79 20 74 68 65 20 6e started by the n
43270 65 78 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 ext call to sqli
43280 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 te3_backup_step(
43290 29 2e 20 49 66 20 74 68 65 20 73 6f 75 72 63 65 ). If the source
432a0 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 69 73 .** database is
432b0 20 6d 6f 64 69 66 69 65 64 20 62 79 20 74 68 65 modified by the
432c0 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 using the same
432d0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
432e0 69 6f 6e 20 61 73 20 69 73 20 75 73 65 64 0a 2a ion as is used.*
432f0 2a 20 62 79 20 74 68 65 20 62 61 63 6b 75 70 20 * by the backup
43300 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 6e 20 operation, then
43310 74 68 65 20 62 61 63 6b 75 70 20 64 61 74 61 62 the backup datab
43320 61 73 65 20 69 73 20 74 72 61 6e 73 70 61 72 65 ase is transpare
43330 6e 74 6c 79 20 0a 2a 2a 20 75 70 64 61 74 65 64 ntly .** updated
43340 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d at the same tim
43350 65 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 73 71 6c 69 e..**.** <b>sqli
43360 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 te3_backup_finis
43370 68 28 29 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 4f 6e h()</b>.**.** On
43380 63 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 ce sqlite3_backu
43390 70 5f 73 74 65 70 28 29 20 68 61 73 20 72 65 74 p_step() has ret
433a0 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f 44 4f urned [SQLITE_DO
433b0 4e 45 5d 2c 20 6f 72 20 77 68 65 6e 20 74 68 65 NE], or when the
433c0 20 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e .** application
433d0 20 77 69 73 68 65 73 20 74 6f 20 61 62 61 6e 64 wishes to aband
433e0 6f 6e 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70 on the backup op
433f0 65 72 61 74 69 6f 6e 2c 20 74 68 65 20 5b 73 71 eration, the [sq
43400 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d 0a 2a 2a lite3_backup].**
43410 20 6f 62 6a 65 63 74 20 73 68 6f 75 6c 64 20 62 object should b
43420 65 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 e passed to sqli
43430 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 te3_backup_finis
43440 68 28 29 2e 20 54 68 69 73 20 72 65 6c 65 61 73 h(). This releas
43450 65 73 20 61 6c 6c 0a 2a 2a 20 72 65 73 6f 75 72 es all.** resour
43460 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 ces associated w
43470 69 74 68 20 74 68 65 20 62 61 63 6b 75 70 20 6f ith the backup o
43480 70 65 72 61 74 69 6f 6e 2e 20 49 66 20 73 71 6c peration. If sql
43490 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 ite3_backup_step
434a0 28 29 0a 2a 2a 20 68 61 73 20 6e 6f 74 20 79 65 ().** has not ye
434b0 74 20 72 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 t returned [SQLI
434c0 54 45 5f 44 4f 4e 45 5d 2c 20 74 68 65 6e 20 61 TE_DONE], then a
434d0 6e 79 20 61 63 74 69 76 65 20 77 72 69 74 65 2d ny active write-
434e0 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 transaction on t
434f0 68 65 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f he.** destinatio
43500 6e 20 64 61 74 61 62 61 73 65 20 69 73 20 72 6f n database is ro
43510 6c 6c 65 64 20 62 61 63 6b 2e 20 54 68 65 20 5b lled back. The [
43520 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d 20 sqlite3_backup]
43530 6f 62 6a 65 63 74 20 69 73 20 69 6e 76 61 6c 69 object is invali
43540 64 0a 2a 2a 20 61 6e 64 20 6d 61 79 20 6e 6f 74 d.** and may not
43550 20 62 65 20 75 73 65 64 20 66 6f 6c 6c 6f 77 69 be used followi
43560 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c ng a call to sql
43570 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 ite3_backup_fini
43580 73 68 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 sh()..**.** The
43590 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 value returned b
435a0 79 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 y sqlite3_backup
435b0 5f 66 69 6e 69 73 68 20 69 73 20 5b 53 51 4c 49 _finish is [SQLI
435c0 54 45 5f 4f 4b 5d 20 69 66 20 6e 6f 20 65 72 72 TE_OK] if no err
435d0 6f 72 0a 2a 2a 20 6f 63 63 75 72 72 65 64 2c 20 or.** occurred,
435e0 72 65 67 61 72 64 6c 65 73 73 20 6f 72 20 77 68 regardless or wh
435f0 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 73 71 6c ether or not sql
43600 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 ite3_backup_step
43610 28 29 20 77 61 73 20 63 61 6c 6c 65 64 0a 2a 2a () was called.**
43620 20 61 20 73 75 66 66 69 63 69 65 6e 74 20 6e 75 a sufficient nu
43630 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 6f mber of times to
43640 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 62 61 complete the ba
43650 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20 ckup operation.
43660 4f 72 2c 20 69 66 0a 2a 2a 20 61 6e 20 6f 75 74 Or, if.** an out
43670 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 6f 6e 64 69 -of-memory condi
43680 74 69 6f 6e 20 6f 72 20 49 4f 20 65 72 72 6f 72 tion or IO error
43690 20 6f 63 63 75 72 65 64 20 64 75 72 69 6e 67 20 occured during
436a0 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c a call to.** sql
436b0 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 ite3_backup_step
436c0 28 29 20 74 68 65 6e 20 5b 53 51 4c 49 54 45 5f () then [SQLITE_
436d0 4e 4f 4d 45 4d 5d 20 6f 72 20 61 6e 0a 2a 2a 20 NOMEM] or an.**
436e0 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 [SQLITE_IOERR_AC
436f0 43 45 53 53 20 7c 20 53 51 4c 49 54 45 5f 49 4f CESS | SQLITE_IO
43700 45 52 52 5f 58 58 58 5d 20 65 72 72 6f 72 20 63 ERR_XXX] error c
43710 6f 64 65 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e ode.** is return
43720 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 ed. In this case
43730 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 the error code
43740 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 and an error mes
43750 73 61 67 65 20 61 72 65 0a 2a 2a 20 77 72 69 74 sage are.** writ
43760 74 65 6e 20 74 6f 20 74 68 65 20 64 65 73 74 69 ten to the desti
43770 6e 61 74 69 6f 6e 20 5b 64 61 74 61 62 61 73 65 nation [database
43780 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 0a 2a 2a connection]..**
43790 0a 2a 2a 20 41 20 72 65 74 75 72 6e 20 6f 66 20 .** A return of
437a0 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6f 72 [SQLITE_BUSY] or
437b0 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5d [SQLITE_LOCKED]
437c0 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 62 61 from sqlite3_ba
437d0 63 6b 75 70 5f 73 74 65 70 28 29 20 69 73 0a 2a ckup_step() is.*
437e0 2a 20 6e 6f 74 20 61 20 70 65 72 6d 61 6e 65 6e * not a permanen
437f0 74 20 65 72 72 6f 72 20 61 6e 64 20 64 6f 65 73 t error and does
43800 20 6e 6f 74 20 61 66 66 65 63 74 20 74 68 65 20 not affect the
43810 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 0a return value of.
43820 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 ** sqlite3_backu
43830 70 5f 66 69 6e 69 73 68 28 29 2e 0a 2a 2a 0a 2a p_finish()..**.*
43840 2a 20 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 * <b>sqlite3_bac
43850 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 2c kup_remaining(),
43860 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
43870 70 61 67 65 63 6f 75 6e 74 28 29 3c 2f 62 3e 0a pagecount()</b>.
43880 2a 2a 0a 2a 2a 20 45 61 63 68 20 63 61 6c 6c 20 **.** Each call
43890 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 to sqlite3_backu
438a0 70 5f 73 74 65 70 28 29 20 73 65 74 73 20 74 77 p_step() sets tw
438b0 6f 20 76 61 6c 75 65 73 20 73 74 6f 72 65 64 20 o values stored
438c0 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20 62 79 internally.** by
438d0 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 an [sqlite3_bac
438e0 6b 75 70 5d 20 6f 62 6a 65 63 74 2e 20 54 68 65 kup] object. The
438f0 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
43900 20 73 74 69 6c 6c 20 74 6f 20 62 65 20 62 61 63 still to be bac
43910 6b 65 64 0a 2a 2a 20 75 70 2c 20 77 68 69 63 68 ked.** up, which
43920 20 6d 61 79 20 62 65 20 71 75 65 72 69 65 64 20 may be queried
43930 62 79 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 by sqlite3_backu
43940 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 2c 20 61 p_remaining(), a
43950 6e 64 20 74 68 65 20 74 6f 74 61 6c 0a 2a 2a 20 nd the total.**
43960 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 number of pages
43970 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 in the source da
43980 74 61 62 61 73 65 20 66 69 6c 65 2c 20 77 68 69 tabase file, whi
43990 63 68 20 6d 61 79 20 62 65 20 71 75 65 72 69 65 ch may be querie
439a0 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f d by.** sqlite3_
439b0 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 backup_pagecount
439c0 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 ()..**.** The va
439d0 6c 75 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 lues returned by
439e0 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 these functions
439f0 20 61 72 65 20 6f 6e 6c 79 20 75 70 64 61 74 65 are only update
43a00 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f d by.** sqlite3_
43a10 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 20 49 backup_step(). I
43a20 66 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 f the source dat
43a30 61 62 61 73 65 20 69 73 20 6d 6f 64 69 66 69 65 abase is modifie
43a40 64 20 64 75 72 69 6e 67 20 61 20 62 61 63 6b 75 d during a backu
43a50 70 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 2c 20 p.** operation,
43a60 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 73 20 then the values
43a70 61 72 65 20 6e 6f 74 20 75 70 64 61 74 65 64 20 are not updated
43a80 74 6f 20 61 63 63 6f 75 6e 74 20 66 6f 72 20 61 to account for a
43a90 6e 79 20 65 78 74 72 61 0a 2a 2a 20 70 61 67 65 ny extra.** page
43aa0 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 62 s that need to b
43ab0 65 20 75 70 64 61 74 65 64 20 6f 72 20 74 68 65 e updated or the
43ac0 20 73 69 7a 65 20 6f 66 20 74 68 65 20 73 6f 75 size of the sou
43ad0 72 63 65 20 64 61 74 61 62 61 73 65 20 66 69 6c rce database fil
43ae0 65 0a 2a 2a 20 63 68 61 6e 67 69 6e 67 2e 0a 2a e.** changing..*
43af0 2a 0a 2a 2a 20 3c 62 3e 43 6f 6e 63 75 72 72 65 *.** <b>Concurre
43b00 6e 74 20 55 73 61 67 65 20 6f 66 20 44 61 74 61 nt Usage of Data
43b10 62 61 73 65 20 48 61 6e 64 6c 65 73 3c 2f 62 3e base Handles</b>
43b20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 6f 75 72 63 .**.** The sourc
43b30 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e e [database conn
43b40 65 63 74 69 6f 6e 5d 20 6d 61 79 20 62 65 20 75 ection] may be u
43b50 73 65 64 20 62 79 20 74 68 65 20 61 70 70 6c 69 sed by the appli
43b60 63 61 74 69 6f 6e 20 66 6f 72 20 6f 74 68 65 72 cation for other
43b70 0a 2a 2a 20 70 75 72 70 6f 73 65 73 20 77 68 69 .** purposes whi
43b80 6c 65 20 61 20 62 61 63 6b 75 70 20 6f 70 65 72 le a backup oper
43b90 61 74 69 6f 6e 20 69 73 20 75 6e 64 65 72 77 61 ation is underwa
43ba0 79 20 6f 72 20 62 65 69 6e 67 20 69 6e 69 74 69 y or being initi
43bb0 61 6c 69 7a 65 64 2e 0a 2a 2a 20 49 66 20 53 51 alized..** If SQ
43bc0 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 Lite is compiled
43bd0 20 61 6e 64 20 63 6f 6e 66 69 67 75 72 65 64 20 and configured
43be0 74 6f 20 73 75 70 70 6f 72 74 20 74 68 72 65 61 to support threa
43bf0 64 73 61 66 65 20 64 61 74 61 62 61 73 65 0a 2a dsafe database.*
43c00 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2c 20 74 * connections, t
43c10 68 65 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64 hen the source d
43c20 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
43c30 6f 6e 20 6d 61 79 20 62 65 20 75 73 65 64 20 63 on may be used c
43c40 6f 6e 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 66 oncurrently.** f
43c50 72 6f 6d 20 77 69 74 68 69 6e 20 6f 74 68 65 72 rom within other
43c60 20 74 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 threads..**.**
43c70 48 6f 77 65 76 65 72 2c 20 74 68 65 20 61 70 70 However, the app
43c80 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 67 75 lication must gu
43c90 61 72 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 arantee that the
43ca0 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 destination dat
43cb0 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 abase.** connect
43cc0 69 6f 6e 20 68 61 6e 64 6c 65 20 69 73 20 6e 6f ion handle is no
43cd0 74 20 70 61 73 73 65 64 20 74 6f 20 61 6e 79 20 t passed to any
43ce0 6f 74 68 65 72 20 41 50 49 20 28 62 79 20 61 6e other API (by an
43cf0 79 20 74 68 72 65 61 64 29 20 61 66 74 65 72 20 y thread) after
43d00 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b .** sqlite3_back
43d10 75 70 5f 69 6e 69 74 28 29 20 69 73 20 63 61 6c up_init() is cal
43d20 6c 65 64 20 61 6e 64 20 62 65 66 6f 72 65 20 74 led and before t
43d30 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 he corresponding
43d40 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 call to.** sqli
43d50 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 te3_backup_finis
43d60 68 28 29 2e 20 55 6e 66 6f 72 74 75 6e 61 74 65 h(). Unfortunate
43d70 6c 79 20 53 51 4c 69 74 65 20 64 6f 65 73 20 6e ly SQLite does n
43d80 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 63 68 65 ot currently che
43d90 63 6b 0a 2a 2a 20 66 6f 72 20 74 68 69 73 2c 20 ck.** for this,
43da0 69 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 if the applicati
43db0 6f 6e 20 64 6f 65 73 20 75 73 65 20 74 68 65 20 on does use the
43dc0 64 65 73 74 69 6e 61 74 69 6f 6e 20 5b 64 61 74 destination [dat
43dd0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
43de0 5d 0a 2a 2a 20 66 6f 72 20 73 6f 6d 65 20 6f 74 ].** for some ot
43df0 68 65 72 20 70 75 72 70 6f 73 65 20 64 75 72 69 her purpose duri
43e00 6e 67 20 61 20 62 61 63 6b 75 70 20 6f 70 65 72 ng a backup oper
43e10 61 74 69 6f 6e 2c 20 74 68 69 6e 67 73 20 6d 61 ation, things ma
43e20 79 20 61 70 70 65 61 72 20 74 6f 0a 2a 2a 20 77 y appear to.** w
43e30 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 20 62 75 ork correctly bu
43e40 74 20 69 6e 20 66 61 63 74 20 62 65 20 73 75 62 t in fact be sub
43e50 74 6c 79 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 69 tly malfunctioni
43e60 6e 67 2e 20 20 55 73 65 20 6f 66 20 74 68 65 0a ng. Use of the.
43e70 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 ** destination d
43e80 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
43e90 6f 6e 20 77 68 69 6c 65 20 61 20 62 61 63 6b 75 on while a backu
43ea0 70 20 69 73 20 69 6e 20 70 72 6f 67 72 65 73 73 p is in progress
43eb0 20 6d 69 67 68 74 0a 2a 2a 20 61 6c 73 6f 20 63 might.** also c
43ec0 61 75 73 65 20 61 20 6d 75 74 65 78 20 64 65 61 ause a mutex dea
43ed0 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 46 75 72 dlock..**.** Fur
43ee0 74 68 65 72 6d 6f 72 65 2c 20 69 66 20 72 75 6e thermore, if run
43ef0 6e 69 6e 67 20 69 6e 20 5b 73 68 61 72 65 64 20 ning in [shared
43f00 63 61 63 68 65 20 6d 6f 64 65 5d 2c 20 74 68 65 cache mode], the
43f10 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 application mus
43f20 74 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65 20 74 t.** guarantee t
43f30 68 61 74 20 74 68 65 20 73 68 61 72 65 64 20 63 hat the shared c
43f40 61 63 68 65 20 75 73 65 64 20 62 79 20 74 68 65 ache used by the
43f50 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 destination dat
43f60 61 62 61 73 65 0a 2a 2a 20 69 73 20 6e 6f 74 20 abase.** is not
43f70 61 63 63 65 73 73 65 64 20 77 68 69 6c 65 20 74 accessed while t
43f80 68 65 20 62 61 63 6b 75 70 20 69 73 20 72 75 6e he backup is run
43f90 6e 69 6e 67 2e 20 49 6e 20 70 72 61 63 74 69 63 ning. In practic
43fa0 65 20 74 68 69 73 20 6d 65 61 6e 73 0a 2a 2a 20 e this means.**
43fb0 74 68 61 74 20 74 68 65 20 61 70 70 6c 69 63 61 that the applica
43fc0 74 69 6f 6e 20 6d 75 73 74 20 67 75 61 72 61 6e tion must guaran
43fd0 74 65 65 20 74 68 61 74 20 74 68 65 20 66 69 6c tee that the fil
43fe0 65 2d 73 79 73 74 65 6d 20 66 69 6c 65 20 62 65 e-system file be
43ff0 69 6e 67 20 0a 2a 2a 20 62 61 63 6b 65 64 20 75 ing .** backed u
44000 70 20 74 6f 20 69 73 20 6e 6f 74 20 61 63 63 65 p to is not acce
44010 73 73 65 64 20 62 79 20 61 6e 79 20 63 6f 6e 6e ssed by any conn
44020 65 63 74 69 6f 6e 20 77 69 74 68 69 6e 20 74 68 ection within th
44030 65 20 70 72 6f 63 65 73 73 2c 0a 2a 2a 20 6e 6f e process,.** no
44040 74 20 6a 75 73 74 20 74 68 65 20 73 70 65 63 69 t just the speci
44050 66 69 63 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 fic connection t
44060 68 61 74 20 77 61 73 20 70 61 73 73 65 64 20 74 hat was passed t
44070 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 o sqlite3_backup
44080 5f 69 6e 69 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 _init()..**.** T
44090 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b he [sqlite3_back
440a0 75 70 5d 20 6f 62 6a 65 63 74 20 69 74 73 65 6c up] object itsel
440b0 66 20 69 73 20 70 61 72 74 69 61 6c 6c 79 20 74 f is partially t
440c0 68 72 65 61 64 73 61 66 65 2e 20 4d 75 6c 74 69 hreadsafe. Multi
440d0 70 6c 65 20 0a 2a 2a 20 74 68 72 65 61 64 73 20 ple .** threads
440e0 6d 61 79 20 73 61 66 65 6c 79 20 6d 61 6b 65 20 may safely make
440f0 6d 75 6c 74 69 70 6c 65 20 63 6f 6e 63 75 72 72 multiple concurr
44100 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c ent calls to sql
44110 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 ite3_backup_step
44120 28 29 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 ()..** However,
44130 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b the sqlite3_back
44140 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 20 61 up_remaining() a
44150 6e 64 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 nd sqlite3_backu
44160 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 0a 2a 2a p_pagecount().**
44170 20 41 50 49 73 20 61 72 65 20 6e 6f 74 20 73 74 APIs are not st
44180 72 69 63 74 6c 79 20 73 70 65 61 6b 69 6e 67 20 rictly speaking
44190 74 68 72 65 61 64 73 61 66 65 2e 20 49 66 20 74 threadsafe. If t
441a0 68 65 79 20 61 72 65 20 69 6e 76 6f 6b 65 64 20 hey are invoked
441b0 61 74 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 74 at the.** same t
441c0 69 6d 65 20 61 73 20 61 6e 6f 74 68 65 72 20 74 ime as another t
441d0 68 72 65 61 64 20 69 73 20 69 6e 76 6f 6b 69 6e hread is invokin
441e0 67 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 g sqlite3_backup
441f0 5f 73 74 65 70 28 29 20 69 74 20 69 73 0a 2a 2a _step() it is.**
44200 20 70 6f 73 73 69 62 6c 65 20 74 68 61 74 20 74 possible that t
44210 68 65 79 20 72 65 74 75 72 6e 20 69 6e 76 61 6c hey return inval
44220 69 64 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a 53 51 id values..*/.SQ
44230 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 LITE_API sqlite3
44240 5f 62 61 63 6b 75 70 20 2a 73 71 6c 69 74 65 33 _backup *sqlite3
44250 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 0a 20 20 _backup_init(.
44260 73 71 6c 69 74 65 33 20 2a 70 44 65 73 74 2c 20 sqlite3 *pDest,
44270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44280 20 20 20 20 20 20 20 2f 2a 20 44 65 73 74 69 6e /* Destin
44290 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 68 ation database h
442a0 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 andle */. const
442b0 20 63 68 61 72 20 2a 7a 44 65 73 74 4e 61 6d 65 char *zDestName
442c0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
442d0 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f 6e /* Destination
442e0 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 2a database name *
442f0 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 53 6f /. sqlite3 *pSo
44300 75 72 63 65 2c 20 20 20 20 20 20 20 20 20 20 20 urce,
44310 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f /* So
44320 75 72 63 65 20 64 61 74 61 62 61 73 65 20 68 61 urce database ha
44330 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 ndle */. const
44340 63 68 61 72 20 2a 7a 53 6f 75 72 63 65 4e 61 6d char *zSourceNam
44350 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e
44360 20 2f 2a 20 53 6f 75 72 63 65 20 64 61 74 61 62 /* Source datab
44370 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 29 3b 0a 53 ase name */.);.S
44380 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
44390 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 lite3_backup_ste
443a0 70 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 p(sqlite3_backup
443b0 20 2a 70 2c 20 69 6e 74 20 6e 50 61 67 65 29 3b *p, int nPage);
443c0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
443d0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 sqlite3_backup_f
443e0 69 6e 69 73 68 28 73 71 6c 69 74 65 33 5f 62 61 inish(sqlite3_ba
443f0 63 6b 75 70 20 2a 70 29 3b 0a 53 51 4c 49 54 45 ckup *p);.SQLITE
44400 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
44410 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e _backup_remainin
44420 67 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 g(sqlite3_backup
44430 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 *p);.SQLITE_API
44440 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61 63 int sqlite3_bac
44450 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 73 71 kup_pagecount(sq
44460 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 29 lite3_backup *p)
44470 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
44480 46 3a 20 55 6e 6c 6f 63 6b 20 4e 6f 74 69 66 69 F: Unlock Notifi
44490 63 61 74 69 6f 6e 0a 2a 2a 20 45 58 50 45 52 49 cation.** EXPERI
444a0 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 57 68 65 MENTAL.**.** Whe
444b0 6e 20 72 75 6e 6e 69 6e 67 20 69 6e 20 73 68 61 n running in sha
444c0 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 2c 20 red-cache mode,
444d0 61 20 64 61 74 61 62 61 73 65 20 6f 70 65 72 61 a database opera
444e0 74 69 6f 6e 20 6d 61 79 20 66 61 69 6c 20 77 69 tion may fail wi
444f0 74 68 0a 2a 2a 20 61 6e 20 5b 53 51 4c 49 54 45 th.** an [SQLITE
44500 5f 4c 4f 43 4b 45 44 5d 20 65 72 72 6f 72 20 69 _LOCKED] error i
44510 66 20 74 68 65 20 72 65 71 75 69 72 65 64 20 6c f the required l
44520 6f 63 6b 73 20 6f 6e 20 74 68 65 20 73 68 61 72 ocks on the shar
44530 65 64 2d 63 61 63 68 65 20 6f 72 0a 2a 2a 20 69 ed-cache or.** i
44540 6e 64 69 76 69 64 75 61 6c 20 74 61 62 6c 65 73 ndividual tables
44550 20 77 69 74 68 69 6e 20 74 68 65 20 73 68 61 72 within the shar
44560 65 64 2d 63 61 63 68 65 20 63 61 6e 6e 6f 74 20 ed-cache cannot
44570 62 65 20 6f 62 74 61 69 6e 65 64 2e 20 53 65 65 be obtained. See
44580 0a 2a 2a 20 5b 53 51 4c 69 74 65 20 53 68 61 72 .** [SQLite Shar
44590 65 64 2d 43 61 63 68 65 20 4d 6f 64 65 5d 20 66 ed-Cache Mode] f
445a0 6f 72 20 61 20 64 65 73 63 72 69 70 74 69 6f 6e or a description
445b0 20 6f 66 20 73 68 61 72 65 64 2d 63 61 63 68 65 of shared-cache
445c0 20 6c 6f 63 6b 69 6e 67 2e 20 0a 2a 2a 20 54 68 locking. .** Th
445d0 69 73 20 41 50 49 20 6d 61 79 20 62 65 20 75 73 is API may be us
445e0 65 64 20 74 6f 20 72 65 67 69 73 74 65 72 20 61 ed to register a
445f0 20 63 61 6c 6c 62 61 63 6b 20 74 68 61 74 20 53 callback that S
44600 51 4c 69 74 65 20 77 69 6c 6c 20 69 6e 76 6f 6b QLite will invok
44610 65 20 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 63 e .** when the c
44620 6f 6e 6e 65 63 74 69 6f 6e 20 63 75 72 72 65 6e onnection curren
44630 74 6c 79 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 tly holding the
44640 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 20 72 65 required lock re
44650 6c 69 6e 71 75 69 73 68 65 73 20 69 74 2e 0a 2a linquishes it..*
44660 2a 20 54 68 69 73 20 41 50 49 20 69 73 20 6f 6e * This API is on
44670 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 66 20 ly available if
44680 74 68 65 20 6c 69 62 72 61 72 79 20 77 61 73 20 the library was
44690 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 compiled with th
446a0 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 4e 41 e.** [SQLITE_ENA
446b0 42 4c 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 BLE_UNLOCK_NOTIF
446c0 59 5d 20 43 2d 70 72 65 70 72 6f 63 65 73 73 6f Y] C-preprocesso
446d0 72 20 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64 r symbol defined
446e0 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 41 6c 73 6f ..**.** See Also
446f0 3a 20 5b 55 73 69 6e 67 20 74 68 65 20 53 51 4c : [Using the SQL
44700 69 74 65 20 55 6e 6c 6f 63 6b 20 4e 6f 74 69 66 ite Unlock Notif
44710 69 63 61 74 69 6f 6e 20 46 65 61 74 75 72 65 5d ication Feature]
44720 2e 0a 2a 2a 0a 2a 2a 20 53 68 61 72 65 64 2d 63 ..**.** Shared-c
44730 61 63 68 65 20 6c 6f 63 6b 73 20 61 72 65 20 72 ache locks are r
44740 65 6c 65 61 73 65 64 20 77 68 65 6e 20 61 20 64 eleased when a d
44750 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
44760 6f 6e 20 63 6f 6e 63 6c 75 64 65 73 0a 2a 2a 20 on concludes.**
44770 69 74 73 20 63 75 72 72 65 6e 74 20 74 72 61 6e its current tran
44780 73 61 63 74 69 6f 6e 2c 20 65 69 74 68 65 72 20 saction, either
44790 62 79 20 63 6f 6d 6d 69 74 74 69 6e 67 20 69 74 by committing it
447a0 20 6f 72 20 72 6f 6c 6c 69 6e 67 20 69 74 20 62 or rolling it b
447b0 61 63 6b 2e 20 0a 2a 2a 0a 2a 2a 20 57 68 65 6e ack. .**.** When
447c0 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 28 6b a connection (k
447d0 6e 6f 77 6e 20 61 73 20 74 68 65 20 62 6c 6f 63 nown as the bloc
447e0 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 29 20 ked connection)
447f0 66 61 69 6c 73 20 74 6f 20 6f 62 74 61 69 6e 20 fails to obtain
44800 61 0a 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 68 a.** shared-cach
44810 65 20 6c 6f 63 6b 20 61 6e 64 20 53 51 4c 49 54 e lock and SQLIT
44820 45 5f 4c 4f 43 4b 45 44 20 69 73 20 72 65 74 75 E_LOCKED is retu
44830 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c rned to the call
44840 65 72 2c 20 74 68 65 0a 2a 2a 20 69 64 65 6e 74 er, the.** ident
44850 69 74 79 20 6f 66 20 74 68 65 20 64 61 74 61 62 ity of the datab
44860 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 28 ase connection (
44870 74 68 65 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e the blocking con
44880 6e 65 63 74 69 6f 6e 29 20 74 68 61 74 0a 2a 2a nection) that.**
44890 20 68 61 73 20 6c 6f 63 6b 65 64 20 74 68 65 20 has locked the
448a0 72 65 71 75 69 72 65 64 20 72 65 73 6f 75 72 63 required resourc
448b0 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 74 65 e is stored inte
448c0 72 6e 61 6c 6c 79 2e 20 41 66 74 65 72 20 61 6e rnally. After an
448d0 20 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e .** application
448e0 20 72 65 63 65 69 76 65 73 20 61 6e 20 53 51 4c receives an SQL
448f0 49 54 45 5f 4c 4f 43 4b 45 44 20 65 72 72 6f 72 ITE_LOCKED error
44900 2c 20 69 74 20 6d 61 79 20 63 61 6c 6c 20 74 68 , it may call th
44910 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 75 6e 6c e.** sqlite3_unl
44920 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 6d 65 74 ock_notify() met
44930 68 6f 64 20 77 69 74 68 20 74 68 65 20 62 6c 6f hod with the blo
44940 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 cked connection
44950 68 61 6e 64 6c 65 20 61 73 20 0a 2a 2a 20 74 68 handle as .** th
44960 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
44970 20 74 6f 20 72 65 67 69 73 74 65 72 20 66 6f 72 to register for
44980 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 68 61 74 a callback that
44990 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 will be invoked
449a0 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 62 6c 6f .** when the blo
449b0 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e cking connection
449c0 73 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 s current transa
449d0 63 74 69 6f 6e 20 69 73 20 63 6f 6e 63 6c 75 64 ction is conclud
449e0 65 64 2e 20 54 68 65 0a 2a 2a 20 63 61 6c 6c 62 ed. The.** callb
449f0 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 66 ack is invoked f
44a00 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 20 5b rom within the [
44a10 73 71 6c 69 74 65 33 5f 73 74 65 70 5d 20 6f 72 sqlite3_step] or
44a20 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 5d [sqlite3_close]
44a30 0a 2a 2a 20 63 61 6c 6c 20 74 68 61 74 20 63 6f .** call that co
44a40 6e 63 6c 75 64 65 73 20 74 68 65 20 62 6c 6f 63 ncludes the bloc
44a50 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 king connections
44a60 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a transaction..**
44a70 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65 33 5f 75 .** If sqlite3_u
44a80 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 69 nlock_notify() i
44a90 73 20 63 61 6c 6c 65 64 20 69 6e 20 61 20 6d 75 s called in a mu
44aa0 6c 74 69 2d 74 68 72 65 61 64 65 64 20 61 70 70 lti-threaded app
44ab0 6c 69 63 61 74 69 6f 6e 2c 0a 2a 2a 20 74 68 65 lication,.** the
44ac0 72 65 20 69 73 20 61 20 63 68 61 6e 63 65 20 74 re is a chance t
44ad0 68 61 74 20 74 68 65 20 62 6c 6f 63 6b 69 6e 67 hat the blocking
44ae0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 6c 6c connection will
44af0 20 68 61 76 65 20 61 6c 72 65 61 64 79 0a 2a 2a have already.**
44b00 20 63 6f 6e 63 6c 75 64 65 64 20 69 74 73 20 74 concluded its t
44b10 72 61 6e 73 61 63 74 69 6f 6e 20 62 79 20 74 68 ransaction by th
44b20 65 20 74 69 6d 65 20 73 71 6c 69 74 65 33 5f 75 e time sqlite3_u
44b30 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 69 nlock_notify() i
44b40 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 20 49 66 s invoked..** If
44b50 20 74 68 69 73 20 68 61 70 70 65 6e 73 2c 20 74 this happens, t
44b60 68 65 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 hen the specifie
44b70 64 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e d callback is in
44b80 76 6f 6b 65 64 20 69 6d 6d 65 64 69 61 74 65 6c voked immediatel
44b90 79 2c 0a 2a 2a 20 66 72 6f 6d 20 77 69 74 68 69 y,.** from withi
44ba0 6e 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 n the call to sq
44bb0 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 lite3_unlock_not
44bc0 69 66 79 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 ify()..**.** If
44bd0 74 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e the blocked conn
44be0 65 63 74 69 6f 6e 20 69 73 20 61 74 74 65 6d 70 ection is attemp
44bf0 74 69 6e 67 20 74 6f 20 6f 62 74 61 69 6e 20 61 ting to obtain a
44c00 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 61 write-lock on a
44c10 0a 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 68 65 .** shared-cache
44c20 20 74 61 62 6c 65 2c 20 61 6e 64 20 6d 6f 72 65 table, and more
44c30 20 74 68 61 6e 20 6f 6e 65 20 6f 74 68 65 72 20 than one other
44c40 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 75 72 72 65 connection curre
44c50 6e 74 6c 79 20 68 6f 6c 64 73 0a 2a 2a 20 61 20 ntly holds.** a
44c60 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 read-lock on the
44c70 20 73 61 6d 65 20 74 61 62 6c 65 2c 20 74 68 65 same table, the
44c80 6e 20 53 51 4c 69 74 65 20 61 72 62 69 74 72 61 n SQLite arbitra
44c90 72 69 6c 79 20 73 65 6c 65 63 74 73 20 6f 6e 65 rily selects one
44ca0 20 6f 66 20 0a 2a 2a 20 74 68 65 20 6f 74 68 65 of .** the othe
44cb0 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 6f r connections to
44cc0 20 75 73 65 20 61 73 20 74 68 65 20 62 6c 6f 63 use as the bloc
44cd0 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e king connection.
44ce0 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 61 79 .**.** There may
44cf0 20 62 65 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20 be at most one
44d00 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 unlock-notify ca
44d10 6c 6c 62 61 63 6b 20 72 65 67 69 73 74 65 72 65 llback registere
44d20 64 20 62 79 20 61 20 0a 2a 2a 20 62 6c 6f 63 6b d by a .** block
44d30 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 49 ed connection. I
44d40 66 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b f sqlite3_unlock
44d50 5f 6e 6f 74 69 66 79 28 29 20 69 73 20 63 61 6c _notify() is cal
44d60 6c 65 64 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 led when the.**
44d70 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 blocked connecti
44d80 6f 6e 20 61 6c 72 65 61 64 79 20 68 61 73 20 61 on already has a
44d90 20 72 65 67 69 73 74 65 72 65 64 20 75 6e 6c 6f registered unlo
44da0 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 ck-notify callba
44db0 63 6b 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 ck,.** then the
44dc0 6e 65 77 20 63 61 6c 6c 62 61 63 6b 20 72 65 70 new callback rep
44dd0 6c 61 63 65 73 20 74 68 65 20 6f 6c 64 2e 20 49 laces the old. I
44de0 66 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b f sqlite3_unlock
44df0 5f 6e 6f 74 69 66 79 28 29 20 69 73 0a 2a 2a 20 _notify() is.**
44e00 63 61 6c 6c 65 64 20 77 69 74 68 20 61 20 4e 55 called with a NU
44e10 4c 4c 20 70 6f 69 6e 74 65 72 20 61 73 20 69 74 LL pointer as it
44e20 73 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e s second argumen
44e30 74 2c 20 74 68 65 6e 20 61 6e 79 20 65 78 69 73 t, then any exis
44e40 74 69 6e 67 0a 2a 2a 20 75 6e 6c 6f 63 6b 2d 6e ting.** unlock-n
44e50 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 69 otify callback i
44e60 73 20 63 61 6e 63 65 6c 6c 65 64 2e 20 54 68 65 s cancelled. The
44e70 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 blocked connect
44e80 69 6f 6e 73 20 0a 2a 2a 20 75 6e 6c 6f 63 6b 2d ions .** unlock-
44e90 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 notify callback
44ea0 6d 61 79 20 61 6c 73 6f 20 62 65 20 63 61 6e 63 may also be canc
44eb0 65 6c 65 64 20 62 79 20 63 6c 6f 73 69 6e 67 20 eled by closing
44ec0 74 68 65 20 62 6c 6f 63 6b 65 64 0a 2a 2a 20 63 the blocked.** c
44ed0 6f 6e 6e 65 63 74 69 6f 6e 20 75 73 69 6e 67 20 onnection using
44ee0 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 [sqlite3_close()
44ef0 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 75 6e 6c ]..**.** The unl
44f00 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 ock-notify callb
44f10 61 63 6b 20 69 73 20 6e 6f 74 20 72 65 65 6e 74 ack is not reent
44f20 72 61 6e 74 2e 20 49 66 20 61 6e 20 61 70 70 6c rant. If an appl
44f30 69 63 61 74 69 6f 6e 20 69 6e 76 6f 6b 65 73 0a ication invokes.
44f40 2a 2a 20 61 6e 79 20 73 71 6c 69 74 65 33 5f 78 ** any sqlite3_x
44f50 78 78 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 73 xx API functions
44f60 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 6e 20 from within an
44f70 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 unlock-notify ca
44f80 6c 6c 62 61 63 6b 2c 20 61 0a 2a 2a 20 63 72 61 llback, a.** cra
44f90 73 68 20 6f 72 20 64 65 61 64 6c 6f 63 6b 20 6d sh or deadlock m
44fa0 61 79 20 62 65 20 74 68 65 20 72 65 73 75 6c 74 ay be the result
44fb0 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 65 73 73 20 64 ..**.** Unless d
44fc0 65 61 64 6c 6f 63 6b 20 69 73 20 64 65 74 65 63 eadlock is detec
44fd0 74 65 64 20 28 73 65 65 20 62 65 6c 6f 77 29 2c ted (see below),
44fe0 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f sqlite3_unlock_
44ff0 6e 6f 74 69 66 79 28 29 20 61 6c 77 61 79 73 0a notify() always.
45000 2a 2a 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 ** returns SQLIT
45010 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 43 E_OK..**.** <b>C
45020 61 6c 6c 62 61 63 6b 20 49 6e 76 6f 63 61 74 69 allback Invocati
45030 6f 6e 20 44 65 74 61 69 6c 73 3c 2f 62 3e 0a 2a on Details</b>.*
45040 2a 0a 2a 2a 20 57 68 65 6e 20 61 6e 20 75 6e 6c *.** When an unl
45050 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 ock-notify callb
45060 61 63 6b 20 69 73 20 72 65 67 69 73 74 65 72 65 ack is registere
45070 64 2c 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 d, the applicati
45080 6f 6e 20 70 72 6f 76 69 64 65 73 20 61 20 0a 2a on provides a .*
45090 2a 20 73 69 6e 67 6c 65 20 76 6f 69 64 2a 20 70 * single void* p
450a0 6f 69 6e 74 65 72 20 74 68 61 74 20 69 73 20 70 ointer that is p
450b0 61 73 73 65 64 20 74 6f 20 74 68 65 20 63 61 6c assed to the cal
450c0 6c 62 61 63 6b 20 77 68 65 6e 20 69 74 20 69 73 lback when it is
450d0 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 20 48 6f 77 invoked..** How
450e0 65 76 65 72 2c 20 74 68 65 20 73 69 67 6e 61 74 ever, the signat
450f0 75 72 65 20 6f 66 20 74 68 65 20 63 61 6c 6c 62 ure of the callb
45100 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 61 6c 6c ack function all
45110 6f 77 73 20 53 51 4c 69 74 65 20 74 6f 20 70 61 ows SQLite to pa
45120 73 73 0a 2a 2a 20 69 74 20 61 6e 20 61 72 72 61 ss.** it an arra
45130 79 20 6f 66 20 76 6f 69 64 2a 20 63 6f 6e 74 65 y of void* conte
45140 78 74 20 70 6f 69 6e 74 65 72 73 2e 20 54 68 65 xt pointers. The
45150 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 first argument
45160 70 61 73 73 65 64 20 74 6f 0a 2a 2a 20 61 6e 20 passed to.** an
45170 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 unlock-notify ca
45180 6c 6c 62 61 63 6b 20 69 73 20 61 20 70 6f 69 6e llback is a poin
45190 74 65 72 20 74 6f 20 61 6e 20 61 72 72 61 79 20 ter to an array
451a0 6f 66 20 76 6f 69 64 2a 20 70 6f 69 6e 74 65 72 of void* pointer
451b0 73 2c 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73 65 s,.** and the se
451c0 63 6f 6e 64 20 69 73 20 74 68 65 20 6e 75 6d 62 cond is the numb
451d0 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e er of entries in
451e0 20 74 68 65 20 61 72 72 61 79 2e 0a 2a 2a 0a 2a the array..**.*
451f0 2a 20 57 68 65 6e 20 61 20 62 6c 6f 63 6b 69 6e * When a blockin
45200 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 72 g connections tr
45210 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6e ansaction is con
45220 63 6c 75 64 65 64 2c 20 74 68 65 72 65 20 6d 61 cluded, there ma
45230 79 20 62 65 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 y be.** more tha
45240 6e 20 6f 6e 65 20 62 6c 6f 63 6b 65 64 20 63 6f n one blocked co
45250 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 68 61 nnection that ha
45260 73 20 72 65 67 69 73 74 65 72 65 64 20 66 6f 72 s registered for
45270 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 an unlock-notif
45280 79 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 2e 20 49 y.** callback. I
45290 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 73 75 f two or more su
452a0 63 68 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 ch blocked conne
452b0 63 74 69 6f 6e 73 20 68 61 76 65 20 73 70 65 63 ctions have spec
452c0 69 66 69 65 64 20 74 68 65 0a 2a 2a 20 73 61 6d ified the.** sam
452d0 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 e callback funct
452e0 69 6f 6e 2c 20 74 68 65 6e 20 69 6e 73 74 65 61 ion, then instea
452f0 64 20 6f 66 20 69 6e 76 6f 6b 69 6e 67 20 74 68 d of invoking th
45300 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 e callback funct
45310 69 6f 6e 0a 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 ion.** multiple
45320 74 69 6d 65 73 2c 20 69 74 20 69 73 20 69 6e 76 times, it is inv
45330 6f 6b 65 64 20 6f 6e 63 65 20 77 69 74 68 20 74 oked once with t
45340 68 65 20 73 65 74 20 6f 66 20 76 6f 69 64 2a 20 he set of void*
45350 63 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 73 context pointers
45360 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 62 79 .** specified by
45370 20 74 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e the blocked con
45380 6e 65 63 74 69 6f 6e 73 20 62 75 6e 64 6c 65 64 nections bundled
45390 20 74 6f 67 65 74 68 65 72 20 69 6e 74 6f 20 61 together into a
453a0 6e 20 61 72 72 61 79 2e 0a 2a 2a 20 54 68 69 73 n array..** This
453b0 20 67 69 76 65 73 20 74 68 65 20 61 70 70 6c 69 gives the appli
453c0 63 61 74 69 6f 6e 20 61 6e 20 6f 70 70 6f 72 74 cation an opport
453d0 75 6e 69 74 79 20 74 6f 20 70 72 69 6f 72 69 74 unity to priorit
453e0 69 7a 65 20 61 6e 79 20 61 63 74 69 6f 6e 73 20 ize any actions
453f0 0a 2a 2a 20 72 65 6c 61 74 65 64 20 74 6f 20 74 .** related to t
45400 68 65 20 73 65 74 20 6f 66 20 75 6e 62 6c 6f 63 he set of unbloc
45410 6b 65 64 20 64 61 74 61 62 61 73 65 20 63 6f 6e ked database con
45420 6e 65 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 nections..**.**
45430 3c 62 3e 44 65 61 64 6c 6f 63 6b 20 44 65 74 65 <b>Deadlock Dete
45440 63 74 69 6f 6e 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 ction</b>.**.**
45450 41 73 73 75 6d 69 6e 67 20 74 68 61 74 20 61 66 Assuming that af
45460 74 65 72 20 72 65 67 69 73 74 65 72 69 6e 67 20 ter registering
45470 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f for an unlock-no
45480 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 61 20 tify callback a
45490 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 77 61 69 .** database wai
454a0 74 73 20 66 6f 72 20 74 68 65 20 63 61 6c 6c 62 ts for the callb
454b0 61 63 6b 20 74 6f 20 62 65 20 69 73 73 75 65 64 ack to be issued
454c0 20 62 65 66 6f 72 65 20 74 61 6b 69 6e 67 20 61 before taking a
454d0 6e 79 20 66 75 72 74 68 65 72 0a 2a 2a 20 61 63 ny further.** ac
454e0 74 69 6f 6e 20 28 61 20 72 65 61 73 6f 6e 61 62 tion (a reasonab
454f0 6c 65 20 61 73 73 75 6d 70 74 69 6f 6e 29 2c 20 le assumption),
45500 74 68 65 6e 20 75 73 69 6e 67 20 74 68 69 73 20 then using this
45510 41 50 49 20 6d 61 79 20 63 61 75 73 65 20 74 68 API may cause th
45520 65 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e e.** application
45530 20 74 6f 20 64 65 61 64 6c 6f 63 6b 2e 20 46 6f to deadlock. Fo
45540 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 63 6f r example, if co
45550 6e 6e 65 63 74 69 6f 6e 20 58 20 69 73 20 77 61 nnection X is wa
45560 69 74 69 6e 67 20 66 6f 72 0a 2a 2a 20 63 6f 6e iting for.** con
45570 6e 65 63 74 69 6f 6e 20 59 27 73 20 74 72 61 6e nection Y's tran
45580 73 61 63 74 69 6f 6e 20 74 6f 20 62 65 20 63 6f saction to be co
45590 6e 63 6c 75 64 65 64 2c 20 61 6e 64 20 73 69 6d ncluded, and sim
455a0 69 6c 61 72 6c 79 20 63 6f 6e 6e 65 63 74 69 6f ilarly connectio
455b0 6e 0a 2a 2a 20 59 20 69 73 20 77 61 69 74 69 6e n.** Y is waitin
455c0 67 20 6f 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 g on connection
455d0 58 27 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c X's transaction,
455e0 20 74 68 65 6e 20 6e 65 69 74 68 65 72 20 63 6f then neither co
455f0 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 77 69 6c 6c nnection.** will
45600 20 70 72 6f 63 65 65 64 20 61 6e 64 20 74 68 65 proceed and the
45610 20 73 79 73 74 65 6d 20 6d 61 79 20 72 65 6d 61 system may rema
45620 69 6e 20 64 65 61 64 6c 6f 63 6b 65 64 20 69 6e in deadlocked in
45630 64 65 66 69 6e 69 74 65 6c 79 2e 0a 2a 2a 0a 2a definitely..**.*
45640 2a 20 54 6f 20 61 76 6f 69 64 20 74 68 69 73 20 * To avoid this
45650 73 63 65 6e 61 72 69 6f 2c 20 74 68 65 20 73 71 scenario, the sq
45660 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 lite3_unlock_not
45670 69 66 79 28 29 20 70 65 72 66 6f 72 6d 73 20 64 ify() performs d
45680 65 61 64 6c 6f 63 6b 0a 2a 2a 20 64 65 74 65 63 eadlock.** detec
45690 74 69 6f 6e 2e 20 49 66 20 61 20 67 69 76 65 6e tion. If a given
456a0 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
456b0 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 _unlock_notify()
456c0 20 77 6f 75 6c 64 20 70 75 74 20 74 68 65 0a 2a would put the.*
456d0 2a 20 73 79 73 74 65 6d 20 69 6e 20 61 20 64 65 * system in a de
456e0 61 64 6c 6f 63 6b 65 64 20 73 74 61 74 65 2c 20 adlocked state,
456f0 74 68 65 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b then SQLITE_LOCK
45700 45 44 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 ED is returned a
45710 6e 64 20 6e 6f 0a 2a 2a 20 75 6e 6c 6f 63 6b 2d nd no.** unlock-
45720 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 notify callback
45730 69 73 20 72 65 67 69 73 74 65 72 65 64 2e 20 54 is registered. T
45740 68 65 20 73 79 73 74 65 6d 20 69 73 20 73 61 69 he system is sai
45750 64 20 74 6f 20 62 65 20 69 6e 0a 2a 2a 20 61 20 d to be in.** a
45760 64 65 61 64 6c 6f 63 6b 65 64 20 73 74 61 74 65 deadlocked state
45770 20 69 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 41 if connection A
45780 20 68 61 73 20 72 65 67 69 73 74 65 72 65 64 20 has registered
45790 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f for an unlock-no
457a0 74 69 66 79 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b tify.** callback
457b0 20 6f 6e 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 on the conclusi
457c0 6f 6e 20 6f 66 20 63 6f 6e 6e 65 63 74 69 6f 6e on of connection
457d0 20 42 27 73 20 74 72 61 6e 73 61 63 74 69 6f 6e B's transaction
457e0 2c 20 61 6e 64 20 63 6f 6e 6e 65 63 74 69 6f 6e , and connection
457f0 0a 2a 2a 20 42 20 68 61 73 20 69 74 73 65 6c 66 .** B has itself
45800 20 72 65 67 69 73 74 65 72 65 64 20 66 6f 72 20 registered for
45810 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 an unlock-notify
45820 20 63 61 6c 6c 62 61 63 6b 20 77 68 65 6e 20 63 callback when c
45830 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 41 27 73 onnection.** A's
45840 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
45850 63 6f 6e 63 6c 75 64 65 64 2e 20 49 6e 64 69 72 concluded. Indir
45860 65 63 74 20 64 65 61 64 6c 6f 63 6b 20 69 73 20 ect deadlock is
45870 61 6c 73 6f 20 64 65 74 65 63 74 65 64 2c 20 73 also detected, s
45880 6f 0a 2a 2a 20 74 68 65 20 73 79 73 74 65 6d 20 o.** the system
45890 69 73 20 61 6c 73 6f 20 63 6f 6e 73 69 64 65 72 is also consider
458a0 65 64 20 74 6f 20 62 65 20 64 65 61 64 6c 6f 63 ed to be deadloc
458b0 6b 65 64 20 69 66 20 63 6f 6e 6e 65 63 74 69 6f ked if connectio
458c0 6e 20 42 20 68 61 73 0a 2a 2a 20 72 65 67 69 73 n B has.** regis
458d0 74 65 72 65 64 20 66 6f 72 20 61 6e 20 75 6e 6c tered for an unl
458e0 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 ock-notify callb
458f0 61 63 6b 20 6f 6e 20 74 68 65 20 63 6f 6e 63 6c ack on the concl
45900 75 73 69 6f 6e 20 6f 66 20 63 6f 6e 6e 65 63 74 usion of connect
45910 69 6f 6e 0a 2a 2a 20 43 27 73 20 74 72 61 6e 73 ion.** C's trans
45920 61 63 74 69 6f 6e 2c 20 77 68 65 72 65 20 63 6f action, where co
45930 6e 6e 65 63 74 69 6f 6e 20 43 20 69 73 20 77 61 nnection C is wa
45940 69 74 69 6e 67 20 6f 6e 20 63 6f 6e 6e 65 63 74 iting on connect
45950 69 6f 6e 20 41 2e 20 41 6e 79 0a 2a 2a 20 6e 75 ion A. Any.** nu
45960 6d 62 65 72 20 6f 66 20 6c 65 76 65 6c 73 20 6f mber of levels o
45970 66 20 69 6e 64 69 72 65 63 74 69 6f 6e 20 61 72 f indirection ar
45980 65 20 61 6c 6c 6f 77 65 64 2e 0a 2a 2a 0a 2a 2a e allowed..**.**
45990 20 3c 62 3e 54 68 65 20 22 44 52 4f 50 20 54 41 <b>The "DROP TA
459a0 42 4c 45 22 20 45 78 63 65 70 74 69 6f 6e 3c 2f BLE" Exception</
459b0 62 3e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 b>.**.** When a
459c0 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 call to [sqlite3
459d0 5f 73 74 65 70 28 29 5d 20 72 65 74 75 72 6e 73 _step()] returns
459e0 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2c 20 SQLITE_LOCKED,
459f0 69 74 20 69 73 20 61 6c 6d 6f 73 74 20 0a 2a 2a it is almost .**
45a00 20 61 6c 77 61 79 73 20 61 70 70 72 6f 70 72 69 always appropri
45a10 61 74 65 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69 ate to call sqli
45a20 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 te3_unlock_notif
45a30 79 28 29 2e 20 54 68 65 72 65 20 69 73 20 68 6f y(). There is ho
45a40 77 65 76 65 72 2c 0a 2a 2a 20 6f 6e 65 20 65 78 wever,.** one ex
45a50 63 65 70 74 69 6f 6e 2e 20 57 68 65 6e 20 65 78 ception. When ex
45a60 65 63 75 74 69 6e 67 20 61 20 22 44 52 4f 50 20 ecuting a "DROP
45a70 54 41 42 4c 45 22 20 6f 72 20 22 44 52 4f 50 20 TABLE" or "DROP
45a80 49 4e 44 45 58 22 20 73 74 61 74 65 6d 65 6e 74 INDEX" statement
45a90 2c 0a 2a 2a 20 53 51 4c 69 74 65 20 63 68 65 63 ,.** SQLite chec
45aa0 6b 73 20 69 66 20 74 68 65 72 65 20 61 72 65 20 ks if there are
45ab0 61 6e 79 20 63 75 72 72 65 6e 74 6c 79 20 65 78 any currently ex
45ac0 65 63 75 74 69 6e 67 20 53 45 4c 45 43 54 20 73 ecuting SELECT s
45ad0 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 tatements.** tha
45ae0 74 20 62 65 6c 6f 6e 67 20 74 6f 20 74 68 65 20 t belong to the
45af0 73 61 6d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e same connection.
45b00 20 49 66 20 74 68 65 72 65 20 61 72 65 2c 20 53 If there are, S
45b10 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 73 0a QLITE_LOCKED is.
45b20 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 49 6e 20 ** returned. In
45b30 74 68 69 73 20 63 61 73 65 20 74 68 65 72 65 20 this case there
45b40 69 73 20 6e 6f 20 22 62 6c 6f 63 6b 69 6e 67 20 is no "blocking
45b50 63 6f 6e 6e 65 63 74 69 6f 6e 22 2c 20 73 6f 20 connection", so
45b60 69 6e 76 6f 6b 69 6e 67 0a 2a 2a 20 73 71 6c 69 invoking.** sqli
45b70 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 te3_unlock_notif
45b80 79 28 29 20 72 65 73 75 6c 74 73 20 69 6e 20 74 y() results in t
45b90 68 65 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 he unlock-notify
45ba0 20 63 61 6c 6c 62 61 63 6b 20 62 65 69 6e 67 0a callback being.
45bb0 2a 2a 20 69 6e 76 6f 6b 65 64 20 69 6d 6d 65 64 ** invoked immed
45bc0 69 61 74 65 6c 79 2e 20 49 66 20 74 68 65 20 61 iately. If the a
45bd0 70 70 6c 69 63 61 74 69 6f 6e 20 74 68 65 6e 20 pplication then
45be0 72 65 2d 61 74 74 65 6d 70 74 73 20 74 68 65 20 re-attempts the
45bf0 22 44 52 4f 50 20 54 41 42 4c 45 22 0a 2a 2a 20 "DROP TABLE".**
45c00 6f 72 20 22 44 52 4f 50 20 49 4e 44 45 58 22 20 or "DROP INDEX"
45c10 71 75 65 72 79 2c 20 61 6e 20 69 6e 66 69 6e 69 query, an infini
45c20 74 65 20 6c 6f 6f 70 20 6d 69 67 68 74 20 62 65 te loop might be
45c30 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a the result..**.
45c40 2a 2a 20 4f 6e 65 20 77 61 79 20 61 72 6f 75 6e ** One way aroun
45c50 64 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 69 d this problem i
45c60 73 20 74 6f 20 63 68 65 63 6b 20 74 68 65 20 65 s to check the e
45c70 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f xtended error co
45c80 64 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 62 de returned.** b
45c90 79 20 61 6e 20 73 71 6c 69 74 65 33 5f 73 74 65 y an sqlite3_ste
45ca0 70 28 29 20 63 61 6c 6c 2e 20 49 66 20 74 68 65 p() call. If the
45cb0 72 65 20 69 73 20 61 20 62 6c 6f 63 6b 69 6e 67 re is a blocking
45cc0 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 74 68 65 connection, the
45cd0 6e 20 74 68 65 0a 2a 2a 20 65 78 74 65 6e 64 65 n the.** extende
45ce0 64 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 d error code is
45cf0 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4c 4f set to SQLITE_LO
45d00 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48 45 CKED_SHAREDCACHE
45d10 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 6e 0a . Otherwise, in.
45d20 2a 2a 20 74 68 65 20 73 70 65 63 69 61 6c 20 22 ** the special "
45d30 44 52 4f 50 20 54 41 42 4c 45 2f 49 4e 44 45 58 DROP TABLE/INDEX
45d40 22 20 63 61 73 65 2c 20 74 68 65 20 65 78 74 65 " case, the exte
45d50 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 20 nded error code
45d60 69 73 20 6a 75 73 74 20 0a 2a 2a 20 53 51 4c 49 is just .** SQLI
45d70 54 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2f 0a 53 51 TE_LOCKED..*/.SQ
45d80 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
45d90 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 ite3_unlock_noti
45da0 66 79 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 fy(. sqlite3 *p
45db0 42 6c 6f 63 6b 65 64 2c 20 20 20 20 20 20 20 20 Blocked,
45dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45dd0 20 20 2f 2a 20 57 61 69 74 69 6e 67 20 63 6f 6e /* Waiting con
45de0 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 nection */. voi
45df0 64 20 28 2a 78 4e 6f 74 69 66 79 29 28 76 6f 69 d (*xNotify)(voi
45e00 64 20 2a 2a 61 70 41 72 67 2c 20 69 6e 74 20 6e d **apArg, int n
45e10 41 72 67 29 2c 20 20 20 20 2f 2a 20 43 61 6c 6c Arg), /* Call
45e20 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 74 6f back function to
45e30 20 69 6e 76 6f 6b 65 20 2a 2f 0a 20 20 76 6f 69 invoke */. voi
45e40 64 20 2a 70 4e 6f 74 69 66 79 41 72 67 20 20 20 d *pNotifyArg
45e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45e60 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 /* Argu
45e70 6d 65 6e 74 20 74 6f 20 70 61 73 73 20 74 6f 20 ment to pass to
45e80 78 4e 6f 74 69 66 79 20 2a 2f 0a 29 3b 0a 0a 0a xNotify */.);...
45e90 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
45ea0 53 74 72 69 6e 67 20 43 6f 6d 70 61 72 69 73 6f String Compariso
45eb0 6e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 n.** EXPERIMENTA
45ec0 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c L.**.** The [sql
45ed0 69 74 65 33 5f 73 74 72 6e 69 63 6d 70 28 29 5d ite3_strnicmp()]
45ee0 20 41 50 49 20 61 6c 6c 6f 77 73 20 61 70 70 6c API allows appl
45ef0 69 63 61 74 69 6f 6e 73 20 61 6e 64 20 65 78 74 ications and ext
45f00 65 6e 73 69 6f 6e 73 20 74 6f 0a 2a 2a 20 63 6f ensions to.** co
45f10 6d 70 61 72 65 20 74 68 65 20 63 6f 6e 74 65 6e mpare the conten
45f20 74 73 20 6f 66 20 74 77 6f 20 62 75 66 66 65 72 ts of two buffer
45f30 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20 55 54 46 s containing UTF
45f40 2d 38 20 73 74 72 69 6e 67 73 20 69 6e 20 61 0a -8 strings in a.
45f50 2a 2a 20 63 61 73 65 2d 69 6e 64 65 6e 64 65 6e ** case-indenden
45f60 74 20 66 61 73 68 69 6f 6e 2c 20 75 73 69 6e 67 t fashion, using
45f70 20 74 68 65 20 73 61 6d 65 20 64 65 66 69 6e 69 the same defini
45f80 74 69 6f 6e 20 6f 66 20 63 61 73 65 20 69 6e 64 tion of case ind
45f90 65 70 65 6e 64 65 6e 63 65 20 0a 2a 2a 20 74 68 ependence .** th
45fa0 61 74 20 53 51 4c 69 74 65 20 75 73 65 73 20 69 at SQLite uses i
45fb0 6e 74 65 72 6e 61 6c 6c 79 20 77 68 65 6e 20 63 nternally when c
45fc0 6f 6d 70 61 72 69 6e 67 20 69 64 65 6e 74 69 66 omparing identif
45fd0 69 65 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f iers..*/.SQLITE_
45fe0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
45ff0 73 74 72 6e 69 63 6d 70 28 63 6f 6e 73 74 20 63 strnicmp(const c
46000 68 61 72 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 har *, const cha
46010 72 20 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a r *, int);../*.*
46020 2a 20 55 6e 64 6f 20 74 68 65 20 68 61 63 6b 20 * Undo the hack
46030 74 68 61 74 20 63 6f 6e 76 65 72 74 73 20 66 6c that converts fl
46040 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 74 79 70 oating point typ
46050 65 73 20 74 6f 20 69 6e 74 65 67 65 72 20 66 6f es to integer fo
46060 72 0a 2a 2a 20 62 75 69 6c 64 73 20 6f 6e 20 70 r.** builds on p
46070 72 6f 63 65 73 73 6f 72 73 20 77 69 74 68 6f 75 rocessors withou
46080 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 t floating point
46090 20 73 75 70 70 6f 72 74 2e 0a 2a 2f 0a 23 69 66 support..*/.#if
460a0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
460b0 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 23 FLOATING_POINT.#
460c0 20 75 6e 64 65 66 20 64 6f 75 62 6c 65 0a 23 65 undef double.#e
460d0 6e 64 69 66 0a 0a 23 69 66 20 30 0a 7d 20 20 2f ndif..#if 0.} /
460e0 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 27 65 78 * End of the 'ex
460f0 74 65 72 6e 20 22 43 22 27 20 62 6c 6f 63 6b 20 tern "C"' block
46100 2a 2f 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 */.#endif.#endif
46110 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .../************
46120 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69 74 65 ** End of sqlite
46130 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 3.h ************
46140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46160 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
46170 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 ** Continuing wh
46180 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 ere we left off
46190 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a in sqliteInt.h *
461a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
461b0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
461c0 2a 2a 20 49 6e 63 6c 75 64 65 20 68 61 73 68 2e ** Include hash.
461d0 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 h in the middle
461e0 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a of sqliteInt.h *
461f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46200 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
46210 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68 61 ** Begin file ha
46220 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a sh.h ***********
46230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46250 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 */./*.** 2001 Se
46260 70 74 65 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a 2a ptember 22.**.**
46270 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
46280 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
46290 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
462a0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
462b0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
462c0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
462d0 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
462e0 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
462f0 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
46300 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
46310 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
46320 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
46330 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
46340 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
46350 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
46360 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
46370 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
46380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
463a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
463b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
463c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 *********.** Thi
463d0 73 20 69 73 20 74 68 65 20 68 65 61 64 65 72 20 s is the header
463e0 66 69 6c 65 20 66 6f 72 20 74 68 65 20 67 65 6e file for the gen
463f0 65 72 69 63 20 68 61 73 68 2d 74 61 62 6c 65 20 eric hash-table
46400 69 6d 70 6c 65 6d 65 6e 61 74 69 6f 6e 0a 2a 2a implemenation.**
46410 20 75 73 65 64 20 69 6e 20 53 51 4c 69 74 65 2e used in SQLite.
46420 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 61 73 68 .**.** $Id: hash
46430 2e 68 2c 76 20 31 2e 31 35 20 32 30 30 39 2f 30 .h,v 1.15 2009/0
46440 35 2f 30 32 20 31 33 3a 32 39 3a 33 38 20 64 72 5/02 13:29:38 dr
46450 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 h Exp $.*/.#ifnd
46460 65 66 20 5f 53 51 4c 49 54 45 5f 48 41 53 48 5f ef _SQLITE_HASH_
46470 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 H_.#define _SQLI
46480 54 45 5f 48 41 53 48 5f 48 5f 0a 0a 2f 2a 20 46 TE_HASH_H_../* F
46490 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 orward declarati
464a0 6f 6e 73 20 6f 66 20 73 74 72 75 63 74 75 72 65 ons of structure
464b0 73 2e 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 s. */.typedef st
464c0 72 75 63 74 20 48 61 73 68 20 48 61 73 68 3b 0a ruct Hash Hash;.
464d0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 48 typedef struct H
464e0 61 73 68 45 6c 65 6d 20 48 61 73 68 45 6c 65 6d ashElem HashElem
464f0 3b 0a 0a 2f 2a 20 41 20 63 6f 6d 70 6c 65 74 65 ;../* A complete
46500 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 20 61 hash table is a
46510 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
46520 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 e following stru
46530 63 74 75 72 65 2e 0a 2a 2a 20 54 68 65 20 69 6e cture..** The in
46540 74 65 72 6e 61 6c 73 20 6f 66 20 74 68 69 73 20 ternals of this
46550 73 74 72 75 63 74 75 72 65 20 61 72 65 20 69 6e structure are in
46560 74 65 6e 64 65 64 20 74 6f 20 62 65 20 6f 70 61 tended to be opa
46570 71 75 65 20 2d 2d 20 63 6c 69 65 6e 74 0a 2a 2a que -- client.**
46580 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 6e 6f 74 code should not
46590 20 61 74 74 65 6d 70 74 20 74 6f 20 61 63 63 65 attempt to acce
465a0 73 73 20 6f 72 20 6d 6f 64 69 66 79 20 74 68 65 ss or modify the
465b0 20 66 69 65 6c 64 73 20 6f 66 20 74 68 69 73 20 fields of this
465c0 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 64 69 72 structure.** dir
465d0 65 63 74 6c 79 2e 20 20 43 68 61 6e 67 65 20 74 ectly. Change t
465e0 68 69 73 20 73 74 72 75 63 74 75 72 65 20 6f 6e his structure on
465f0 6c 79 20 62 79 20 75 73 69 6e 67 20 74 68 65 20 ly by using the
46600 72 6f 75 74 69 6e 65 73 20 62 65 6c 6f 77 2e 0a routines below..
46610 2a 2a 20 48 6f 77 65 76 65 72 2c 20 73 6f 6d 65 ** However, some
46620 20 6f 66 20 74 68 65 20 22 70 72 6f 63 65 64 75 of the "procedu
46630 72 65 73 22 20 61 6e 64 20 22 66 75 6e 63 74 69 res" and "functi
46640 6f 6e 73 22 20 66 6f 72 20 6d 6f 64 69 66 79 69 ons" for modifyi
46650 6e 67 20 61 6e 64 0a 2a 2a 20 61 63 63 65 73 73 ng and.** access
46660 69 6e 67 20 74 68 69 73 20 73 74 72 75 63 74 75 ing this structu
46670 72 65 20 61 72 65 20 72 65 61 6c 6c 79 20 6d 61 re are really ma
46680 63 72 6f 73 2c 20 73 6f 20 77 65 20 63 61 6e 27 cros, so we can'
46690 74 20 72 65 61 6c 6c 79 20 6d 61 6b 65 0a 2a 2a t really make.**
466a0 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 this structure
466b0 6f 70 61 71 75 65 2e 0a 2a 2a 0a 2a 2a 20 41 6c opaque..**.** Al
466c0 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 l elements of th
466d0 65 20 68 61 73 68 20 74 61 62 6c 65 20 61 72 65 e hash table are
466e0 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 64 6f 75 on a single dou
466f0 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 2e bly-linked list.
46700 0a 2a 2a 20 48 61 73 68 2e 66 69 72 73 74 20 70 .** Hash.first p
46710 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 68 65 61 oints to the hea
46720 64 20 6f 66 20 74 68 69 73 20 6c 69 73 74 2e 0a d of this list..
46730 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 **.** There are
46740 48 61 73 68 2e 68 74 73 69 7a 65 20 62 75 63 6b Hash.htsize buck
46750 65 74 73 2e 20 20 45 61 63 68 20 62 75 63 6b 65 ets. Each bucke
46760 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 73 70 t points to a sp
46770 6f 74 20 69 6e 0a 2a 2a 20 74 68 65 20 67 6c 6f ot in.** the glo
46780 62 61 6c 20 64 6f 75 62 6c 79 2d 6c 69 6e 6b 65 bal doubly-linke
46790 64 20 6c 69 73 74 2e 20 20 54 68 65 20 63 6f 6e d list. The con
467a0 74 65 6e 74 73 20 6f 66 20 74 68 65 20 62 75 63 tents of the buc
467b0 6b 65 74 20 61 72 65 20 74 68 65 0a 2a 2a 20 65 ket are the.** e
467c0 6c 65 6d 65 6e 74 20 70 6f 69 6e 74 65 64 20 74 lement pointed t
467d0 6f 20 70 6c 75 73 20 74 68 65 20 6e 65 78 74 20 o plus the next
467e0 5f 68 74 2e 63 6f 75 6e 74 2d 31 20 65 6c 65 6d _ht.count-1 elem
467f0 65 6e 74 73 20 69 6e 20 74 68 65 20 6c 69 73 74 ents in the list
46800 2e 0a 2a 2a 0a 2a 2a 20 48 61 73 68 2e 68 74 73 ..**.** Hash.hts
46810 69 7a 65 20 61 6e 64 20 48 61 73 68 2e 68 74 20 ize and Hash.ht
46820 6d 61 79 20 62 65 20 7a 65 72 6f 2e 20 20 49 6e may be zero. In
46830 20 74 68 61 74 20 63 61 73 65 20 6c 6f 6f 6b 75 that case looku
46840 70 20 69 73 20 64 6f 6e 65 0a 2a 2a 20 62 79 20 p is done.** by
46850 61 20 6c 69 6e 65 61 72 20 73 65 61 72 63 68 20 a linear search
46860 6f 66 20 74 68 65 20 67 6c 6f 62 61 6c 20 6c 69 of the global li
46870 73 74 2e 20 20 46 6f 72 20 73 6d 61 6c 6c 20 74 st. For small t
46880 61 62 6c 65 73 2c 20 74 68 65 20 0a 2a 2a 20 48 ables, the .** H
46890 61 73 68 2e 68 74 20 74 61 62 6c 65 20 69 73 20 ash.ht table is
468a0 6e 65 76 65 72 20 61 6c 6c 6f 63 61 74 65 64 20 never allocated
468b0 62 65 63 61 75 73 65 20 69 66 20 74 68 65 72 65 because if there
468c0 20 61 72 65 20 66 65 77 20 65 6c 65 6d 65 6e 74 are few element
468d0 73 0a 2a 2a 20 69 6e 20 74 68 65 20 74 61 62 6c s.** in the tabl
468e0 65 2c 20 69 74 20 69 73 20 66 61 73 74 65 72 20 e, it is faster
468f0 74 6f 20 64 6f 20 61 20 6c 69 6e 65 61 72 20 73 to do a linear s
46900 65 61 72 63 68 20 74 68 61 6e 20 74 6f 20 6d 61 earch than to ma
46910 6e 61 67 65 0a 2a 2a 20 74 68 65 20 68 61 73 68 nage.** the hash
46920 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 table..*/.struc
46930 74 20 48 61 73 68 20 7b 0a 20 20 75 6e 73 69 67 t Hash {. unsig
46940 6e 65 64 20 69 6e 74 20 68 74 73 69 7a 65 3b 20 ned int htsize;
46950 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
46960 66 20 62 75 63 6b 65 74 73 20 69 6e 20 74 68 65 f buckets in the
46970 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 hash table */.
46980 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 63 6f unsigned int co
46990 75 6e 74 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 unt; /* Nu
469a0 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 mber of entries
469b0 69 6e 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f in this table */
469c0 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 66 69 72 . HashElem *fir
469d0 73 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 st; /*
469e0 54 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e The first elemen
469f0 74 20 6f 66 20 74 68 65 20 61 72 72 61 79 20 2a t of the array *
46a00 2f 0a 20 20 73 74 72 75 63 74 20 5f 68 74 20 7b /. struct _ht {
46a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
46a20 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 the hash table
46a30 2a 2f 0a 20 20 20 20 69 6e 74 20 63 6f 75 6e 74 */. int count
46a40 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
46a50 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 /* Number of e
46a60 6e 74 72 69 65 73 20 77 69 74 68 20 74 68 69 73 ntries with this
46a70 20 68 61 73 68 20 2a 2f 0a 20 20 20 20 48 61 73 hash */. Has
46a80 68 45 6c 65 6d 20 2a 63 68 61 69 6e 3b 20 20 20 hElem *chain;
46a90 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 /* Point
46aa0 65 72 20 74 6f 20 66 69 72 73 74 20 65 6e 74 72 er to first entr
46ab0 79 20 77 69 74 68 20 74 68 69 73 20 68 61 73 68 y with this hash
46ac0 20 2a 2f 0a 20 20 7d 20 2a 68 74 3b 0a 7d 3b 0a */. } *ht;.};.
46ad0 0a 2f 2a 20 45 61 63 68 20 65 6c 65 6d 65 6e 74 ./* Each element
46ae0 20 69 6e 20 74 68 65 20 68 61 73 68 20 74 61 62 in the hash tab
46af0 6c 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 le is an instanc
46b00 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
46b10 6e 67 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 ng .** structure
46b20 2e 20 20 41 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 . All elements
46b30 61 72 65 20 73 74 6f 72 65 64 20 6f 6e 20 61 20 are stored on a
46b40 73 69 6e 67 6c 65 20 64 6f 75 62 6c 79 2d 6c 69 single doubly-li
46b50 6e 6b 65 64 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a nked list..**.**
46b60 20 41 67 61 69 6e 2c 20 74 68 69 73 20 73 74 72 Again, this str
46b70 75 63 74 75 72 65 20 69 73 20 69 6e 74 65 6e 64 ucture is intend
46b80 65 64 20 74 6f 20 62 65 20 6f 70 61 71 75 65 2c ed to be opaque,
46b90 20 62 75 74 20 69 74 20 63 61 6e 27 74 20 72 65 but it can't re
46ba0 61 6c 6c 79 0a 2a 2a 20 62 65 20 6f 70 61 71 75 ally.** be opaqu
46bb0 65 20 62 65 63 61 75 73 65 20 69 74 20 69 73 20 e because it is
46bc0 75 73 65 64 20 62 79 20 6d 61 63 72 6f 73 2e 0a used by macros..
46bd0 2a 2f 0a 73 74 72 75 63 74 20 48 61 73 68 45 6c */.struct HashEl
46be0 65 6d 20 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 em {. HashElem
46bf0 2a 6e 65 78 74 2c 20 2a 70 72 65 76 3b 20 20 20 *next, *prev;
46c00 20 20 20 20 2f 2a 20 4e 65 78 74 20 61 6e 64 20 /* Next and
46c10 70 72 65 76 69 6f 75 73 20 65 6c 65 6d 65 6e 74 previous element
46c20 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 2a s in the table *
46c30 2f 0a 20 20 76 6f 69 64 20 2a 64 61 74 61 3b 20 /. void *data;
46c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
46c50 20 2f 2a 20 44 61 74 61 20 61 73 73 6f 63 69 61 /* Data associa
46c60 74 65 64 20 77 69 74 68 20 74 68 69 73 20 65 6c ted with this el
46c70 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 ement */. const
46c80 20 63 68 61 72 20 2a 70 4b 65 79 3b 20 69 6e 74 char *pKey; int
46c90 20 6e 4b 65 79 3b 20 20 2f 2a 20 4b 65 79 20 61 nKey; /* Key a
46ca0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
46cb0 68 69 73 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 7d his element */.}
46cc0 3b 0a 0a 2f 2a 0a 2a 2a 20 41 63 63 65 73 73 20 ;../*.** Access
46cd0 72 6f 75 74 69 6e 65 73 2e 20 20 54 6f 20 64 65 routines. To de
46ce0 6c 65 74 65 2c 20 69 6e 73 65 72 74 20 61 20 4e lete, insert a N
46cf0 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a ULL pointer..*/.
46d00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
46d10 6f 69 64 20 73 71 6c 69 74 65 33 48 61 73 68 49 oid sqlite3HashI
46d20 6e 69 74 28 48 61 73 68 2a 29 3b 0a 53 51 4c 49 nit(Hash*);.SQLI
46d30 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
46d40 2a 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 *sqlite3HashInse
46d50 72 74 28 48 61 73 68 2a 2c 20 63 6f 6e 73 74 20 rt(Hash*, const
46d60 63 68 61 72 20 2a 70 4b 65 79 2c 20 69 6e 74 20 char *pKey, int
46d70 6e 4b 65 79 2c 20 76 6f 69 64 20 2a 70 44 61 74 nKey, void *pDat
46d80 61 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 a);.SQLITE_PRIVA
46d90 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
46da0 48 61 73 68 46 69 6e 64 28 63 6f 6e 73 74 20 48 HashFind(const H
46db0 61 73 68 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 ash*, const char
46dc0 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 *pKey, int nKey
46dd0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
46de0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 48 61 E void sqlite3Ha
46df0 73 68 43 6c 65 61 72 28 48 61 73 68 2a 29 3b 0a shClear(Hash*);.
46e00 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f ./*.** Macros fo
46e10 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 61 r looping over a
46e20 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 61 ll elements of a
46e30 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54 68 hash table. Th
46e40 65 20 69 64 69 6f 6d 20 69 73 0a 2a 2a 20 6c 69 e idiom is.** li
46e50 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 ke this:.**.**
46e60 20 48 61 73 68 20 68 3b 0a 2a 2a 20 20 20 48 61 Hash h;.** Ha
46e70 73 68 45 6c 65 6d 20 2a 70 3b 0a 2a 2a 20 20 20 shElem *p;.**
46e80 2e 2e 2e 0a 2a 2a 20 20 20 66 6f 72 28 70 3d 73 ....** for(p=s
46e90 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 26 qliteHashFirst(&
46ea0 68 29 3b 20 70 3b 20 70 3d 73 71 6c 69 74 65 48 h); p; p=sqliteH
46eb0 61 73 68 4e 65 78 74 28 70 29 29 7b 0a 2a 2a 20 ashNext(p)){.**
46ec0 20 20 20 20 53 6f 6d 65 53 74 72 75 63 74 75 72 SomeStructur
46ed0 65 20 2a 70 44 61 74 61 20 3d 20 73 71 6c 69 74 e *pData = sqlit
46ee0 65 48 61 73 68 44 61 74 61 28 70 29 3b 0a 2a 2a eHashData(p);.**
46ef0 20 20 20 20 20 2f 2f 20 64 6f 20 73 6f 6d 65 74 // do somet
46f00 68 69 6e 67 20 77 69 74 68 20 70 44 61 74 61 0a hing with pData.
46f10 2a 2a 20 20 20 7d 0a 2a 2f 0a 23 64 65 66 69 6e ** }.*/.#defin
46f20 65 20 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 e sqliteHashFirs
46f30 74 28 48 29 20 20 28 28 48 29 2d 3e 66 69 72 73 t(H) ((H)->firs
46f40 74 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 t).#define sqlit
46f50 65 48 61 73 68 4e 65 78 74 28 45 29 20 20 20 28 eHashNext(E) (
46f60 28 45 29 2d 3e 6e 65 78 74 29 0a 23 64 65 66 69 (E)->next).#defi
46f70 6e 65 20 73 71 6c 69 74 65 48 61 73 68 44 61 74 ne sqliteHashDat
46f80 61 28 45 29 20 20 20 28 28 45 29 2d 3e 64 61 74 a(E) ((E)->dat
46f90 61 29 0a 2f 2a 20 23 64 65 66 69 6e 65 20 73 71 a)./* #define sq
46fa0 6c 69 74 65 48 61 73 68 4b 65 79 28 45 29 20 20 liteHashKey(E)
46fb0 20 20 28 28 45 29 2d 3e 70 4b 65 79 29 20 2f 2f ((E)->pKey) //
46fc0 20 4e 4f 54 20 55 53 45 44 20 2a 2f 0a 2f 2a 20 NOT USED */./*
46fd0 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 #define sqliteHa
46fe0 73 68 4b 65 79 73 69 7a 65 28 45 29 20 28 28 45 shKeysize(E) ((E
46ff0 29 2d 3e 6e 4b 65 79 29 20 20 2f 2f 20 4e 4f 54 )->nKey) // NOT
47000 20 55 53 45 44 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 USED */../*.**
47010 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 Number of entrie
47020 73 20 69 6e 20 61 20 68 61 73 68 20 74 61 62 6c s in a hash tabl
47030 65 0a 2a 2f 0a 2f 2a 20 23 64 65 66 69 6e 65 20 e.*/./* #define
47040 73 71 6c 69 74 65 48 61 73 68 43 6f 75 6e 74 28 sqliteHashCount(
47050 48 29 20 20 28 28 48 29 2d 3e 63 6f 75 6e 74 29 H) ((H)->count)
47060 20 2f 2f 20 4e 4f 54 20 55 53 45 44 20 2a 2f 0a // NOT USED */.
47070 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 53 51 4c 49 .#endif /* _SQLI
47080 54 45 5f 48 41 53 48 5f 48 5f 20 2a 2f 0a 0a 2f TE_HASH_H_ */../
47090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
470a0 6e 64 20 6f 66 20 68 61 73 68 2e 68 20 2a 2a 2a nd of hash.h ***
470b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
470c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
470d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
470e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 ************** C
470f0 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 ontinuing where
47100 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 we left off in s
47110 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a qliteInt.h *****
47120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
47130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 ************** I
47140 6e 63 6c 75 64 65 20 70 61 72 73 65 2e 68 20 69 nclude parse.h i
47150 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 n the middle of
47160 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a sqliteInt.h ****
47170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
47180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
47190 65 67 69 6e 20 66 69 6c 65 20 70 61 72 73 65 2e egin file parse.
471a0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
471b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
471c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 *************/.#
471d0 64 65 66 69 6e 65 20 54 4b 5f 53 45 4d 49 20 20 define TK_SEMI
471e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
471f0 20 20 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 1.#def
47200 69 6e 65 20 54 4b 5f 45 58 50 4c 41 49 4e 20 20 ine TK_EXPLAIN
47210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47220 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65 2.#define
47230 20 54 4b 5f 51 55 45 52 59 20 20 20 20 20 20 20 TK_QUERY
47240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47250 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20 54 4b 3.#define TK
47260 5f 50 4c 41 4e 20 20 20 20 20 20 20 20 20 20 20 _PLAN
47270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47280 20 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 45 4.#define TK_BE
47290 47 49 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 GIN
472a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 0a 5.
472b0 23 64 65 66 69 6e 65 20 54 4b 5f 54 52 41 4e 53 #define TK_TRANS
472c0 41 43 54 49 4f 4e 20 20 20 20 20 20 20 20 20 20 ACTION
472d0 20 20 20 20 20 20 20 20 20 20 20 36 0a 23 64 65 6.#de
472e0 66 69 6e 65 20 54 4b 5f 44 45 46 45 52 52 45 44 fine TK_DEFERRED
472f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47300 20 20 20 20 20 20 20 20 37 0a 23 64 65 66 69 6e 7.#defin
47310 65 20 54 4b 5f 49 4d 4d 45 44 49 41 54 45 20 20 e TK_IMMEDIATE
47320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47330 20 20 20 20 20 38 0a 23 64 65 66 69 6e 65 20 54 8.#define T
47340 4b 5f 45 58 43 4c 55 53 49 56 45 20 20 20 20 20 K_EXCLUSIVE
47350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47360 20 20 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 9.#define TK_C
47370 4f 4d 4d 49 54 20 20 20 20 20 20 20 20 20 20 20 OMMIT
47380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 10
47390 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 4e 44 20 .#define TK_END
473a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
473b0 20 20 20 20 20 20 20 20 20 20 20 31 31 0a 23 64 11.#d
473c0 65 66 69 6e 65 20 54 4b 5f 52 4f 4c 4c 42 41 43 efine TK_ROLLBAC
473d0 4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 K
473e0 20 20 20 20 20 20 20 20 31 32 0a 23 64 65 66 69 12.#defi
473f0 6e 65 20 54 4b 5f 53 41 56 45 50 4f 49 4e 54 20 ne TK_SAVEPOINT
47400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47410 20 20 20 20 20 31 33 0a 23 64 65 66 69 6e 65 20 13.#define
47420 54 4b 5f 52 45 4c 45 41 53 45 20 20 20 20 20 20 TK_RELEASE
47430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47440 20 20 31 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 14.#define TK_
47450 54 4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 TO
47460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
47470 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 41 42 5.#define TK_TAB
47480 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 LE
47490 20 20 20 20 20 20 20 20 20 20 20 20 31 36 0a 23 16.#
474a0 64 65 66 69 6e 65 20 54 4b 5f 43 52 45 41 54 45 define TK_CREATE
474b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
474c0 20 20 20 20 20 20 20 20 20 31 37 0a 23 64 65 66 17.#def
474d0 69 6e 65 20 54 4b 5f 49 46 20 20 20 20 20 20 20 ine TK_IF
474e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
474f0 20 20 20 20 20 20 31 38 0a 23 64 65 66 69 6e 65 18.#define
47500 20 54 4b 5f 4e 4f 54 20 20 20 20 20 20 20 20 20 TK_NOT
47510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47520 20 20 20 31 39 0a 23 64 65 66 69 6e 65 20 54 4b 19.#define TK
47530 5f 45 58 49 53 54 53 20 20 20 20 20 20 20 20 20 _EXISTS
47540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47550 32 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 45 20.#define TK_TE
47560 4d 50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 MP
47570 20 20 20 20 20 20 20 20 20 20 20 20 20 32 31 0a 21.
47580 23 64 65 66 69 6e 65 20 54 4b 5f 4c 50 20 20 20 #define TK_LP
47590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
475a0 20 20 20 20 20 20 20 20 20 20 32 32 0a 23 64 65 22.#de
475b0 66 69 6e 65 20 54 4b 5f 52 50 20 20 20 20 20 20 fine TK_RP
475c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
475d0 20 20 20 20 20 20 20 32 33 0a 23 64 65 66 69 6e 23.#defin
475e0 65 20 54 4b 5f 41 53 20 20 20 20 20 20 20 20 20 e TK_AS
475f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47600 20 20 20 20 32 34 0a 23 64 65 66 69 6e 65 20 54 24.#define T
47610 4b 5f 43 4f 4d 4d 41 20 20 20 20 20 20 20 20 20 K_COMMA
47620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47630 20 32 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 25.#define TK_I
47640 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 D
47650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 36 26
47660 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 44 45 .#define TK_INDE
47670 58 45 44 20 20 20 20 20 20 20 20 20 20 20 20 20 XED
47680 20 20 20 20 20 20 20 20 20 20 20 32 37 0a 23 64 27.#d
47690 65 66 69 6e 65 20 54 4b 5f 41 42 4f 52 54 20 20 efine TK_ABORT
476a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
476b0 20 20 20 20 20 20 20 20 32 38 0a 23 64 65 66 69 28.#defi
476c0 6e 65 20 54 4b 5f 41 43 54 49 4f 4e 20 20 20 20 ne TK_ACTION
476d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
476e0 20 20 20 20 20 32 39 0a 23 64 65 66 69 6e 65 20 29.#define
476f0 54 4b 5f 41 46 54 45 52 20 20 20 20 20 20 20 20 TK_AFTER
47700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47710 20 20 33 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 30.#define TK_
47720 41 4e 41 4c 59 5a 45 20 20 20 20 20 20 20 20 20 ANALYZE
47730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 3
47740 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 53 43 1.#define TK_ASC
47750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47760 20 20 20 20 20 20 20 20 20 20 20 20 33 32 0a 23 32.#
47770 64 65 66 69 6e 65 20 54 4b 5f 41 54 54 41 43 48 define TK_ATTACH
47780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47790 20 20 20 20 20 20 20 20 20 33 33 0a 23 64 65 66 33.#def
477a0 69 6e 65 20 54 4b 5f 42 45 46 4f 52 45 20 20 20 ine TK_BEFORE
477b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
477c0 20 20 20 20 20 20 33 34 0a 23 64 65 66 69 6e 65 34.#define
477d0 20 54 4b 5f 42 59 20 20 20 20 20 20 20 20 20 20 TK_BY
477e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
477f0 20 20 20 33 35 0a 23 64 65 66 69 6e 65 20 54 4b 35.#define TK
47800 5f 43 41 53 43 41 44 45 20 20 20 20 20 20 20 20 _CASCADE
47810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47820 33 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 41 36.#define TK_CA
47830 53 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ST
47840 20 20 20 20 20 20 20 20 20 20 20 20 20 33 37 0a 37.
47850 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4c 55 4d #define TK_COLUM
47860 4e 4b 57 20 20 20 20 20 20 20 20 20 20 20 20 20 NKW
47870 20 20 20 20 20 20 20 20 20 20 33 38 0a 23 64 65 38.#de
47880 66 69 6e 65 20 54 4b 5f 43 4f 4e 46 4c 49 43 54 fine TK_CONFLICT
47890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
478a0 20 20 20 20 20 20 20 33 39 0a 23 64 65 66 69 6e 39.#defin
478b0 65 20 54 4b 5f 44 41 54 41 42 41 53 45 20 20 20 e TK_DATABASE
478c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
478d0 20 20 20 20 34 30 0a 23 64 65 66 69 6e 65 20 54 40.#define T
478e0 4b 5f 44 45 53 43 20 20 20 20 20 20 20 20 20 20 K_DESC
478f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47900 20 34 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 41.#define TK_D
47910 45 54 41 43 48 20 20 20 20 20 20 20 20 20 20 20 ETACH
47920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 32 42
47930 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 41 43 48 .#define TK_EACH
47940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47950 20 20 20 20 20 20 20 20 20 20 20 34 33 0a 23 64 43.#d
47960 65 66 69 6e 65 20 54 4b 5f 46 41 49 4c 20 20 20 efine TK_FAIL
47970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47980 20 20 20 20 20 20 20 20 34 34 0a 23 64 65 66 69 44.#defi
47990 6e 65 20 54 4b 5f 46 4f 52 20 20 20 20 20 20 20 ne TK_FOR
479a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
479b0 20 20 20 20 20 34 35 0a 23 64 65 66 69 6e 65 20 45.#define
479c0 54 4b 5f 49 47 4e 4f 52 45 20 20 20 20 20 20 20 TK_IGNORE
479d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
479e0 20 20 34 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46.#define TK_
479f0 49 4e 49 54 49 41 4c 4c 59 20 20 20 20 20 20 20 INITIALLY
47a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 4
47a10 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 53 7.#define TK_INS
47a20 54 45 41 44 20 20 20 20 20 20 20 20 20 20 20 20 TEAD
47a30 20 20 20 20 20 20 20 20 20 20 20 20 34 38 0a 23 48.#
47a40 64 65 66 69 6e 65 20 54 4b 5f 4c 49 4b 45 5f 4b define TK_LIKE_K
47a50 57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 W
47a60 20 20 20 20 20 20 20 20 20 34 39 0a 23 64 65 66 49.#def
47a70 69 6e 65 20 54 4b 5f 4d 41 54 43 48 20 20 20 20 ine TK_MATCH
47a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47a90 20 20 20 20 20 20 35 30 0a 23 64 65 66 69 6e 65 50.#define
47aa0 20 54 4b 5f 4e 4f 20 20 20 20 20 20 20 20 20 20 TK_NO
47ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47ac0 20 20 20 35 31 0a 23 64 65 66 69 6e 65 20 54 4b 51.#define TK
47ad0 5f 4b 45 59 20 20 20 20 20 20 20 20 20 20 20 20 _KEY
47ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47af0 35 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 46 52.#define TK_OF
47b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47b10 20 20 20 20 20 20 20 20 20 20 20 20 20 35 33 0a 53.
47b20 23 64 65 66 69 6e 65 20 54 4b 5f 4f 46 46 53 45 #define TK_OFFSE
47b30 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 T
47b40 20 20 20 20 20 20 20 20 20 20 35 34 0a 23 64 65 54.#de
47b50 66 69 6e 65 20 54 4b 5f 50 52 41 47 4d 41 20 20 fine TK_PRAGMA
47b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47b70 20 20 20 20 20 20 20 35 35 0a 23 64 65 66 69 6e 55.#defin
47b80 65 20 54 4b 5f 52 41 49 53 45 20 20 20 20 20 20 e TK_RAISE
47b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47ba0 20 20 20 20 35 36 0a 23 64 65 66 69 6e 65 20 54 56.#define T
47bb0 4b 5f 52 45 50 4c 41 43 45 20 20 20 20 20 20 20 K_REPLACE
47bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47bd0 20 35 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 57.#define TK_R
47be0 45 53 54 52 49 43 54 20 20 20 20 20 20 20 20 20 ESTRICT
47bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 38 58
47c00 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 4f 57 20 .#define TK_ROW
47c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47c20 20 20 20 20 20 20 20 20 20 20 20 35 39 0a 23 64 59.#d
47c30 65 66 69 6e 65 20 54 4b 5f 54 52 49 47 47 45 52 efine TK_TRIGGER
47c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47c50 20 20 20 20 20 20 20 20 36 30 0a 23 64 65 66 69 60.#defi
47c60 6e 65 20 54 4b 5f 56 41 43 55 55 4d 20 20 20 20 ne TK_VACUUM
47c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47c80 20 20 20 20 20 36 31 0a 23 64 65 66 69 6e 65 20 61.#define
47c90 54 4b 5f 56 49 45 57 20 20 20 20 20 20 20 20 20 TK_VIEW
47ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47cb0 20 20 36 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 62.#define TK_
47cc0 56 49 52 54 55 41 4c 20 20 20 20 20 20 20 20 20 VIRTUAL
47cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 6
47ce0 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 49 3.#define TK_REI
47cf0 4e 44 45 58 20 20 20 20 20 20 20 20 20 20 20 20 NDEX
47d00 20 20 20 20 20 20 20 20 20 20 20 20 36 34 0a 23 64.#
47d10 64 65 66 69 6e 65 20 54 4b 5f 52 45 4e 41 4d 45 define TK_RENAME
47d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47d30 20 20 20 20 20 20 20 20 20 36 35 0a 23 64 65 66 65.#def
47d40 69 6e 65 20 54 4b 5f 43 54 49 4d 45 5f 4b 57 20 ine TK_CTIME_KW
47d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47d60 20 20 20 20 20 20 36 36 0a 23 64 65 66 69 6e 65 66.#define
47d70 20 54 4b 5f 41 4e 59 20 20 20 20 20 20 20 20 20 TK_ANY
47d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47d90 20 20 20 36 37 0a 23 64 65 66 69 6e 65 20 54 4b 67.#define TK
47da0 5f 4f 52 20 20 20 20 20 20 20 20 20 20 20 20 20 _OR
47db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47dc0 36 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 4e 68.#define TK_AN
47dd0 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 D
47de0 20 20 20 20 20 20 20 20 20 20 20 20 20 36 39 0a 69.
47df0 23 64 65 66 69 6e 65 20 54 4b 5f 49 53 20 20 20 #define TK_IS
47e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47e10 20 20 20 20 20 20 20 20 20 20 37 30 0a 23 64 65 70.#de
47e20 66 69 6e 65 20 54 4b 5f 42 45 54 57 45 45 4e 20 fine TK_BETWEEN
47e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47e40 20 20 20 20 20 20 20 37 31 0a 23 64 65 66 69 6e 71.#defin
47e50 65 20 54 4b 5f 49 4e 20 20 20 20 20 20 20 20 20 e TK_IN
47e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47e70 20 20 20 20 37 32 0a 23 64 65 66 69 6e 65 20 54 72.#define T
47e80 4b 5f 49 53 4e 55 4c 4c 20 20 20 20 20 20 20 20 K_ISNULL
47e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47ea0 20 37 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e 73.#define TK_N
47eb0 4f 54 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 OTNULL
47ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 34 74
47ed0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e 45 20 20 .#define TK_NE
47ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47ef0 20 20 20 20 20 20 20 20 20 20 20 37 35 0a 23 64 75.#d
47f00 65 66 69 6e 65 20 54 4b 5f 45 51 20 20 20 20 20 efine TK_EQ
47f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47f20 20 20 20 20 20 20 20 20 37 36 0a 23 64 65 66 69 76.#defi
47f30 6e 65 20 54 4b 5f 47 54 20 20 20 20 20 20 20 20 ne TK_GT
47f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47f50 20 20 20 20 20 37 37 0a 23 64 65 66 69 6e 65 20 77.#define
47f60 54 4b 5f 4c 45 20 20 20 20 20 20 20 20 20 20 20 TK_LE
47f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47f80 20 20 37 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 78.#define TK_
47f90 4c 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 LT
47fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 7
47fb0 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 47 45 20 9.#define TK_GE
47fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47fd0 20 20 20 20 20 20 20 20 20 20 20 20 38 30 0a 23 80.#
47fe0 64 65 66 69 6e 65 20 54 4b 5f 45 53 43 41 50 45 define TK_ESCAPE
47ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48000 20 20 20 20 20 20 20 20 20 38 31 0a 23 64 65 66 81.#def
48010 69 6e 65 20 54 4b 5f 42 49 54 41 4e 44 20 20 20 ine TK_BITAND
48020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48030 20 20 20 20 20 20 38 32 0a 23 64 65 66 69 6e 65 82.#define
48040 20 54 4b 5f 42 49 54 4f 52 20 20 20 20 20 20 20 TK_BITOR
48050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48060 20 20 20 38 33 0a 23 64 65 66 69 6e 65 20 54 4b 83.#define TK
48070 5f 4c 53 48 49 46 54 20 20 20 20 20 20 20 20 20 _LSHIFT
48080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48090 38 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 53 84.#define TK_RS
480a0 48 49 46 54 20 20 20 20 20 20 20 20 20 20 20 20 HIFT
480b0 20 20 20 20 20 20 20 20 20 20 20 20 20 38 35 0a 85.
480c0 23 64 65 66 69 6e 65 20 54 4b 5f 50 4c 55 53 20 #define TK_PLUS
480d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
480e0 20 20 20 20 20 20 20 20 20 20 38 36 0a 23 64 65 86.#de
480f0 66 69 6e 65 20 54 4b 5f 4d 49 4e 55 53 20 20 20 fine TK_MINUS
48100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48110 20 20 20 20 20 20 20 38 37 0a 23 64 65 66 69 6e 87.#defin
48120 65 20 54 4b 5f 53 54 41 52 20 20 20 20 20 20 20 e TK_STAR
48130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48140 20 20 20 20 38 38 0a 23 64 65 66 69 6e 65 20 54 88.#define T
48150 4b 5f 53 4c 41 53 48 20 20 20 20 20 20 20 20 20 K_SLASH
48160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48170 20 38 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 89.#define TK_R
48180 45 4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 EM
48190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 30 90
481a0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4e 43 .#define TK_CONC
481b0 41 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 AT
481c0 20 20 20 20 20 20 20 20 20 20 20 39 31 0a 23 64 91.#d
481d0 65 66 69 6e 65 20 54 4b 5f 43 4f 4c 4c 41 54 45 efine TK_COLLATE
481e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
481f0 20 20 20 20 20 20 20 20 39 32 0a 23 64 65 66 69 92.#defi
48200 6e 65 20 54 4b 5f 42 49 54 4e 4f 54 20 20 20 20 ne TK_BITNOT
48210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48220 20 20 20 20 20 39 33 0a 23 64 65 66 69 6e 65 20 93.#define
48230 54 4b 5f 53 54 52 49 4e 47 20 20 20 20 20 20 20 TK_STRING
48240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48250 20 20 39 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 94.#define TK_
48260 4a 4f 49 4e 5f 4b 57 20 20 20 20 20 20 20 20 20 JOIN_KW
48270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 9
48280 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4e 5.#define TK_CON
48290 53 54 52 41 49 4e 54 20 20 20 20 20 20 20 20 20 STRAINT
482a0 20 20 20 20 20 20 20 20 20 20 20 20 39 36 0a 23 96.#
482b0 64 65 66 69 6e 65 20 54 4b 5f 44 45 46 41 55 4c define TK_DEFAUL
482c0 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 T
482d0 20 20 20 20 20 20 20 20 20 39 37 0a 23 64 65 66 97.#def
482e0 69 6e 65 20 54 4b 5f 4e 55 4c 4c 20 20 20 20 20 ine TK_NULL
482f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48300 20 20 20 20 20 20 39 38 0a 23 64 65 66 69 6e 65 98.#define
48310 20 54 4b 5f 50 52 49 4d 41 52 59 20 20 20 20 20 TK_PRIMARY
48320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48330 20 20 20 39 39 0a 23 64 65 66 69 6e 65 20 54 4b 99.#define TK
48340 5f 55 4e 49 51 55 45 20 20 20 20 20 20 20 20 20 _UNIQUE
48350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48360 31 30 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 100.#define TK_C
48370 48 45 43 4b 20 20 20 20 20 20 20 20 20 20 20 20 HECK
48380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 10
48390 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 46 1.#define TK_REF
483a0 45 52 45 4e 43 45 53 20 20 20 20 20 20 20 20 20 ERENCES
483b0 20 20 20 20 20 20 20 20 20 20 20 20 31 30 32 0a 102.
483c0 23 64 65 66 69 6e 65 20 54 4b 5f 41 55 54 4f 49 #define TK_AUTOI
483d0 4e 43 52 20 20 20 20 20 20 20 20 20 20 20 20 20 NCR
483e0 20 20 20 20 20 20 20 20 20 20 31 30 33 0a 23 64 103.#d
483f0 65 66 69 6e 65 20 54 4b 5f 4f 4e 20 20 20 20 20 efine TK_ON
48400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48410 20 20 20 20 20 20 20 20 31 30 34 0a 23 64 65 66 104.#def
48420 69 6e 65 20 54 4b 5f 44 45 4c 45 54 45 20 20 20 ine TK_DELETE
48430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48440 20 20 20 20 20 20 31 30 35 0a 23 64 65 66 69 6e 105.#defin
48450 65 20 54 4b 5f 55 50 44 41 54 45 20 20 20 20 20 e TK_UPDATE
48460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48470 20 20 20 20 31 30 36 0a 23 64 65 66 69 6e 65 20 106.#define
48480 54 4b 5f 53 45 54 20 20 20 20 20 20 20 20 20 20 TK_SET
48490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
484a0 20 20 31 30 37 0a 23 64 65 66 69 6e 65 20 54 4b 107.#define TK
484b0 5f 44 45 46 45 52 52 41 42 4c 45 20 20 20 20 20 _DEFERRABLE
484c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
484d0 31 30 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 108.#define TK_F
484e0 4f 52 45 49 47 4e 20 20 20 20 20 20 20 20 20 20 OREIGN
484f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 10
48500 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 52 4f 9.#define TK_DRO
48510 50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 P
48520 20 20 20 20 20 20 20 20 20 20 20 20 31 31 30 0a 110.
48530 23 64 65 66 69 6e 65 20 54 4b 5f 55 4e 49 4f 4e #define TK_UNION
48540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48550 20 20 20 20 20 20 20 20 20 20 31 31 31 0a 23 64 111.#d
48560 65 66 69 6e 65 20 54 4b 5f 41 4c 4c 20 20 20 20 efine TK_ALL
48570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48580 20 20 20 20 20 20 20 20 31 31 32 0a 23 64 65 66 112.#def
48590 69 6e 65 20 54 4b 5f 45 58 43 45 50 54 20 20 20 ine TK_EXCEPT
485a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
485b0 20 20 20 20 20 20 31 31 33 0a 23 64 65 66 69 6e 113.#defin
485c0 65 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 20 20 e TK_INTERSECT
485d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
485e0 20 20 20 20 31 31 34 0a 23 64 65 66 69 6e 65 20 114.#define
485f0 54 4b 5f 53 45 4c 45 43 54 20 20 20 20 20 20 20 TK_SELECT
48600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48610 20 20 31 31 35 0a 23 64 65 66 69 6e 65 20 54 4b 115.#define TK
48620 5f 44 49 53 54 49 4e 43 54 20 20 20 20 20 20 20 _DISTINCT
48630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48640 31 31 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 116.#define TK_D
48650 4f 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 OT
48660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 11
48670 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 52 4f 7.#define TK_FRO
48680 4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 M
48690 20 20 20 20 20 20 20 20 20 20 20 20 31 31 38 0a 118.
486a0 23 64 65 66 69 6e 65 20 54 4b 5f 4a 4f 49 4e 20 #define TK_JOIN
486b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
486c0 20 20 20 20 20 20 20 20 20 20 31 31 39 0a 23 64 119.#d
486d0 65 66 69 6e 65 20 54 4b 5f 55 53 49 4e 47 20 20 efine TK_USING
486e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
486f0 20 20 20 20 20 20 20 20 31 32 30 0a 23 64 65 66 120.#def
48700 69 6e 65 20 54 4b 5f 4f 52 44 45 52 20 20 20 20 ine TK_ORDER
48710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48720 20 20 20 20 20 20 31 32 31 0a 23 64 65 66 69 6e 121.#defin
48730 65 20 54 4b 5f 47 52 4f 55 50 20 20 20 20 20 20 e TK_GROUP
48740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48750 20 20 20 20 31 32 32 0a 23 64 65 66 69 6e 65 20 122.#define
48760 54 4b 5f 48 41 56 49 4e 47 20 20 20 20 20 20 20 TK_HAVING
48770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48780 20 20 31 32 33 0a 23 64 65 66 69 6e 65 20 54 4b 123.#define TK
48790 5f 4c 49 4d 49 54 20 20 20 20 20 20 20 20 20 20 _LIMIT
487a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
487b0 31 32 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 57 124.#define TK_W
487c0 48 45 52 45 20 20 20 20 20 20 20 20 20 20 20 20 HERE
487d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 12
487e0 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 54 5.#define TK_INT
487f0 4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 O
48800 20 20 20 20 20 20 20 20 20 20 20 20 31 32 36 0a 126.
48810 23 64 65 66 69 6e 65 20 54 4b 5f 56 41 4c 55 45 #define TK_VALUE
48820 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 S
48830 20 20 20 20 20 20 20 20 20 20 31 32 37 0a 23 64 127.#d
48840 65 66 69 6e 65 20 54 4b 5f 49 4e 53 45 52 54 20 efine TK_INSERT
48850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48860 20 20 20 20 20 20 20 20 31 32 38 0a 23 64 65 66 128.#def
48870 69 6e 65 20 54 4b 5f 49 4e 54 45 47 45 52 20 20 ine TK_INTEGER
48880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48890 20 20 20 20 20 20 31 32 39 0a 23 64 65 66 69 6e 129.#defin
488a0 65 20 54 4b 5f 46 4c 4f 41 54 20 20 20 20 20 20 e TK_FLOAT
488b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
488c0 20 20 20 20 31 33 30 0a 23 64 65 66 69 6e 65 20 130.#define
488d0 54 4b 5f 42 4c 4f 42 20 20 20 20 20 20 20 20 20 TK_BLOB
488e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
488f0 20 20 31 33 31 0a 23 64 65 66 69 6e 65 20 54 4b 131.#define TK
48900 5f 52 45 47 49 53 54 45 52 20 20 20 20 20 20 20 _REGISTER
48910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48920 31 33 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56 132.#define TK_V
48930 41 52 49 41 42 4c 45 20 20 20 20 20 20 20 20 20 ARIABLE
48940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 13
48950 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 41 53 3.#define TK_CAS
48960 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 E
48970 20 20 20 20 20 20 20 20 20 20 20 20 31 33 34 0a 134.
48980 23 64 65 66 69 6e 65 20 54 4b 5f 57 48 45 4e 20 #define TK_WHEN
48990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
489a0 20 20 20 20 20 20 20 20 20 20 31 33 35 0a 23 64 135.#d
489b0 65 66 69 6e 65 20 54 4b 5f 54 48 45 4e 20 20 20 efine TK_THEN
489c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
489d0 20 20 20 20 20 20 20 20 31 33 36 0a 23 64 65 66 136.#def
489e0 69 6e 65 20 54 4b 5f 45 4c 53 45 20 20 20 20 20 ine TK_ELSE
489f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48a00 20 20 20 20 20 20 31 33 37 0a 23 64 65 66 69 6e 137.#defin
48a10 65 20 54 4b 5f 49 4e 44 45 58 20 20 20 20 20 20 e TK_INDEX
48a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48a30 20 20 20 20 31 33 38 0a 23 64 65 66 69 6e 65 20 138.#define
48a40 54 4b 5f 41 4c 54 45 52 20 20 20 20 20 20 20 20 TK_ALTER
48a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48a60 20 20 31 33 39 0a 23 64 65 66 69 6e 65 20 54 4b 139.#define TK
48a70 5f 41 44 44 20 20 20 20 20 20 20 20 20 20 20 20 _ADD
48a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48a90 31 34 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 140.#define TK_T
48aa0 4f 5f 54 45 58 54 20 20 20 20 20 20 20 20 20 20 O_TEXT
48ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 14
48ac0 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 1.#define TK_TO_
48ad0 42 4c 4f 42 20 20 20 20 20 20 20 20 20 20 20 20 BLOB
48ae0 20 20 20 20 20 20 20 20 20 20 20 20 31 34 32 0a 142.
48af0 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 4e 55 #define TK_TO_NU
48b00 4d 45 52 49 43 20 20 20 20 20 20 20 20 20 20 20 MERIC
48b10 20 20 20 20 20 20 20 20 20 20 31 34 33 0a 23 64 143.#d
48b20 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 49 4e 54 20 efine TK_TO_INT
48b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48b40 20 20 20 20 20 20 20 20 31 34 34 0a 23 64 65 66 144.#def
48b50 69 6e 65 20 54 4b 5f 54 4f 5f 52 45 41 4c 20 20 ine TK_TO_REAL
48b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48b70 20 20 20 20 20 20 31 34 35 0a 23 64 65 66 69 6e 145.#defin
48b80 65 20 54 4b 5f 49 53 4e 4f 54 20 20 20 20 20 20 e TK_ISNOT
48b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48ba0 20 20 20 20 31 34 36 0a 23 64 65 66 69 6e 65 20 146.#define
48bb0 54 4b 5f 45 4e 44 5f 4f 46 5f 46 49 4c 45 20 20 TK_END_OF_FILE
48bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48bd0 20 20 31 34 37 0a 23 64 65 66 69 6e 65 20 54 4b 147.#define TK
48be0 5f 49 4c 4c 45 47 41 4c 20 20 20 20 20 20 20 20 _ILLEGAL
48bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48c00 31 34 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 148.#define TK_S
48c10 50 41 43 45 20 20 20 20 20 20 20 20 20 20 20 20 PACE
48c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 14
48c30 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 4e 43 9.#define TK_UNC
48c40 4c 4f 53 45 44 5f 53 54 52 49 4e 47 20 20 20 20 LOSED_STRING
48c50 20 20 20 20 20 20 20 20 20 20 20 20 31 35 30 0a 150.
48c60 23 64 65 66 69 6e 65 20 54 4b 5f 46 55 4e 43 54 #define TK_FUNCT
48c70 49 4f 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 ION
48c80 20 20 20 20 20 20 20 20 20 20 31 35 31 0a 23 64 151.#d
48c90 65 66 69 6e 65 20 54 4b 5f 43 4f 4c 55 4d 4e 20 efine TK_COLUMN
48ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48cb0 20 20 20 20 20 20 20 20 31 35 32 0a 23 64 65 66 152.#def
48cc0 69 6e 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 ine TK_AGG_FUNCT
48cd0 49 4f 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 ION
48ce0 20 20 20 20 20 20 31 35 33 0a 23 64 65 66 69 6e 153.#defin
48cf0 65 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 e TK_AGG_COLUMN
48d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48d10 20 20 20 20 31 35 34 0a 23 64 65 66 69 6e 65 20 154.#define
48d20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 20 20 20 TK_CONST_FUNC
48d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48d40 20 20 31 35 35 0a 23 64 65 66 69 6e 65 20 54 4b 155.#define TK
48d50 5f 55 4d 49 4e 55 53 20 20 20 20 20 20 20 20 20 _UMINUS
48d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48d70 31 35 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 156.#define TK_U
48d80 50 4c 55 53 20 20 20 20 20 20 20 20 20 20 20 20 PLUS
48d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35 15
48da0 37 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 7../************
48db0 2a 2a 20 45 6e 64 20 6f 66 20 70 61 72 73 65 2e ** End of parse.
48dc0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
48dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
48de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
48df0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
48e00 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 ** Continuing wh
48e10 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 ere we left off
48e20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a in sqliteInt.h *
48e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
48e40 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 */.#include <std
48e50 69 6f 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c io.h>.#include <
48e60 73 74 64 6c 69 62 2e 68 3e 0a 23 69 6e 63 6c 75 stdlib.h>.#inclu
48e70 64 65 20 3c 73 74 72 69 6e 67 2e 68 3e 0a 23 69 de <string.h>.#i
48e80 6e 63 6c 75 64 65 20 3c 61 73 73 65 72 74 2e 68 nclude <assert.h
48e90 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 64 >.#include <stdd
48ea0 65 66 2e 68 3e 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 ef.h>../*.** If
48eb0 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20 61 20 compiling for a
48ec0 70 72 6f 63 65 73 73 6f 72 20 74 68 61 74 20 6c processor that l
48ed0 61 63 6b 73 20 66 6c 6f 61 74 69 6e 67 20 70 6f acks floating po
48ee0 69 6e 74 20 73 75 70 70 6f 72 74 2c 0a 2a 2a 20 int support,.**
48ef0 73 75 62 73 74 69 74 75 74 65 20 69 6e 74 65 67 substitute integ
48f00 65 72 20 66 6f 72 20 66 6c 6f 61 74 69 6e 67 2d er for floating-
48f10 70 6f 69 6e 74 0a 2a 2f 0a 23 69 66 64 65 66 20 point.*/.#ifdef
48f20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 SQLITE_OMIT_FLOA
48f30 54 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 64 65 66 TING_POINT.# def
48f40 69 6e 65 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 ine double sqlit
48f50 65 5f 69 6e 74 36 34 0a 23 20 64 65 66 69 6e 65 e_int64.# define
48f60 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 LONGDOUBLE_TYPE
48f70 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 0a 23 20 sqlite_int64.#
48f80 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 42 49 ifndef SQLITE_BI
48f90 47 5f 44 42 4c 0a 23 20 20 20 64 65 66 69 6e 65 G_DBL.# define
48fa0 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c 20 SQLITE_BIG_DBL
48fb0 28 28 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 (((sqlite3_int64
48fc0 29 31 29 3c 3c 35 30 29 0a 23 20 65 6e 64 69 66 )1)<<50).# endif
48fd0 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
48fe0 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 _OMIT_DATETIME_F
48ff0 55 4e 43 53 20 31 0a 23 20 64 65 66 69 6e 65 20 UNCS 1.# define
49000 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 SQLITE_OMIT_TRAC
49010 45 20 31 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 E 1.# undef SQLI
49020 54 45 5f 4d 49 58 45 44 5f 45 4e 44 49 41 4e 5f TE_MIXED_ENDIAN_
49030 36 34 42 49 54 5f 46 4c 4f 41 54 0a 23 20 75 6e 64BIT_FLOAT.# un
49040 64 65 66 20 53 51 4c 49 54 45 5f 48 41 56 45 5f def SQLITE_HAVE_
49050 49 53 4e 41 4e 0a 23 65 6e 64 69 66 0a 23 69 66 ISNAN.#endif.#if
49060 6e 64 65 66 20 53 51 4c 49 54 45 5f 42 49 47 5f ndef SQLITE_BIG_
49070 44 42 4c 0a 23 20 64 65 66 69 6e 65 20 53 51 4c DBL.# define SQL
49080 49 54 45 5f 42 49 47 5f 44 42 4c 20 28 31 65 39 ITE_BIG_DBL (1e9
49090 39 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 9).#endif../*.**
490a0 20 4f 4d 49 54 5f 54 45 4d 50 44 42 20 69 73 20 OMIT_TEMPDB is
490b0 73 65 74 20 74 6f 20 31 20 69 66 20 53 51 4c 49 set to 1 if SQLI
490c0 54 45 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 20 69 TE_OMIT_TEMPDB i
490d0 73 20 64 65 66 69 6e 65 64 2c 20 6f 72 20 30 0a s defined, or 0.
490e0 2a 2a 20 61 66 74 65 72 77 61 72 64 2e 20 48 61 ** afterward. Ha
490f0 76 69 6e 67 20 74 68 69 73 20 6d 61 63 72 6f 20 ving this macro
49100 61 6c 6c 6f 77 73 20 75 73 20 74 6f 20 63 61 75 allows us to cau
49110 73 65 20 74 68 65 20 43 20 63 6f 6d 70 69 6c 65 se the C compile
49120 72 20 0a 2a 2a 20 74 6f 20 6f 6d 69 74 20 63 6f r .** to omit co
49130 64 65 20 75 73 65 64 20 62 79 20 54 45 4d 50 20 de used by TEMP
49140 74 61 62 6c 65 73 20 77 69 74 68 6f 75 74 20 6d tables without m
49150 65 73 73 79 20 23 69 66 6e 64 65 66 20 73 74 61 essy #ifndef sta
49160 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 23 69 66 64 tements..*/.#ifd
49170 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 ef SQLITE_OMIT_T
49180 45 4d 50 44 42 0a 23 64 65 66 69 6e 65 20 4f 4d EMPDB.#define OM
49190 49 54 5f 54 45 4d 50 44 42 20 31 0a 23 65 6c 73 IT_TEMPDB 1.#els
491a0 65 0a 23 64 65 66 69 6e 65 20 4f 4d 49 54 5f 54 e.#define OMIT_T
491b0 45 4d 50 44 42 20 30 0a 23 65 6e 64 69 66 0a 0a EMPDB 0.#endif..
491c0 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f 6c /*.** If the fol
491d0 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 20 69 73 20 lowing macro is
491e0 73 65 74 20 74 6f 20 31 2c 20 74 68 65 6e 20 4e set to 1, then N
491f0 55 4c 4c 20 76 61 6c 75 65 73 20 61 72 65 20 63 ULL values are c
49200 6f 6e 73 69 64 65 72 65 64 0a 2a 2a 20 64 69 73 onsidered.** dis
49210 74 69 6e 63 74 20 77 68 65 6e 20 64 65 74 65 72 tinct when deter
49220 6d 69 6e 69 6e 67 20 77 68 65 74 68 65 72 20 6f mining whether o
49230 72 20 6e 6f 74 20 74 77 6f 20 65 6e 74 72 69 65 r not two entrie
49240 73 20 61 72 65 20 74 68 65 20 73 61 6d 65 0a 2a s are the same.*
49250 2a 20 69 6e 20 61 20 55 4e 49 51 55 45 20 69 6e * in a UNIQUE in
49260 64 65 78 2e 20 20 54 68 69 73 20 69 73 20 74 68 dex. This is th
49270 65 20 77 61 79 20 50 6f 73 74 67 72 65 53 51 4c e way PostgreSQL
49280 2c 20 4f 72 61 63 6c 65 2c 20 44 42 32 2c 20 4d , Oracle, DB2, M
49290 79 53 51 4c 2c 0a 2a 2a 20 4f 43 45 4c 4f 54 2c ySQL,.** OCELOT,
492a0 20 61 6e 64 20 46 69 72 65 62 69 72 64 20 61 6c and Firebird al
492b0 6c 20 77 6f 72 6b 2e 20 20 54 68 65 20 53 51 4c l work. The SQL
492c0 39 32 20 73 70 65 63 20 65 78 70 6c 69 63 69 74 92 spec explicit
492d0 6c 79 20 73 61 79 73 20 74 68 69 73 0a 2a 2a 20 ly says this.**
492e0 69 73 20 74 68 65 20 77 61 79 20 74 68 69 6e 67 is the way thing
492f0 73 20 61 72 65 20 73 75 70 70 6f 73 65 20 74 6f s are suppose to
49300 20 77 6f 72 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 work..**.** If
49310 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 the following ma
49320 63 72 6f 20 69 73 20 73 65 74 20 74 6f 20 30 2c cro is set to 0,
49330 20 74 68 65 20 4e 55 4c 4c 73 20 61 72 65 20 69 the NULLs are i
49340 6e 64 69 73 74 69 6e 63 74 20 66 6f 72 0a 2a 2a ndistinct for.**
49350 20 61 20 55 4e 49 51 55 45 20 69 6e 64 65 78 2e a UNIQUE index.
49360 20 20 49 6e 20 74 68 69 73 20 6d 6f 64 65 2c 20 In this mode,
49370 79 6f 75 20 63 61 6e 20 6f 6e 6c 79 20 68 61 76 you can only hav
49380 65 20 61 20 73 69 6e 67 6c 65 20 4e 55 4c 4c 20 e a single NULL
49390 65 6e 74 72 79 0a 2a 2a 20 66 6f 72 20 61 20 63 entry.** for a c
493a0 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 65 64 20 55 olumn declared U
493b0 4e 49 51 55 45 2e 20 20 54 68 69 73 20 69 73 20 NIQUE. This is
493c0 74 68 65 20 77 61 79 20 49 6e 66 6f 72 6d 69 78 the way Informix
493d0 20 61 6e 64 20 53 51 4c 20 53 65 72 76 65 72 0a and SQL Server.
493e0 2a 2a 20 77 6f 72 6b 2e 0a 2a 2f 0a 23 64 65 66 ** work..*/.#def
493f0 69 6e 65 20 4e 55 4c 4c 5f 44 49 53 54 49 4e 43 ine NULL_DISTINC
49400 54 5f 46 4f 52 5f 55 4e 49 51 55 45 20 31 0a 0a T_FOR_UNIQUE 1..
49410 2f 2a 0a 2a 2a 20 54 68 65 20 22 66 69 6c 65 20 /*.** The "file
49420 66 6f 72 6d 61 74 22 20 6e 75 6d 62 65 72 20 69 format" number i
49430 73 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68 61 s an integer tha
49440 74 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 t is incremented
49450 20 77 68 65 6e 65 76 65 72 0a 2a 2a 20 74 68 65 whenever.** the
49460 20 56 44 42 45 2d 6c 65 76 65 6c 20 66 69 6c 65 VDBE-level file
49470 20 66 6f 72 6d 61 74 20 63 68 61 6e 67 65 73 2e format changes.
49480 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 The following
49490 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 20 74 68 macros define th
494a0 65 0a 2a 2a 20 74 68 65 20 64 65 66 61 75 6c 74 e.** the default
494b0 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 66 6f 72 file format for
494c0 20 6e 65 77 20 64 61 74 61 62 61 73 65 73 20 61 new databases a
494d0 6e 64 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 66 nd the maximum f
494e0 69 6c 65 20 66 6f 72 6d 61 74 0a 2a 2a 20 74 68 ile format.** th
494f0 61 74 20 74 68 65 20 6c 69 62 72 61 72 79 20 63 at the library c
49500 61 6e 20 72 65 61 64 2e 0a 2a 2f 0a 23 64 65 66 an read..*/.#def
49510 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46 ine SQLITE_MAX_F
49520 49 4c 45 5f 46 4f 52 4d 41 54 20 34 0a 23 69 66 ILE_FORMAT 4.#if
49530 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 ndef SQLITE_DEFA
49540 55 4c 54 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 0a ULT_FILE_FORMAT.
49550 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
49560 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 46 4f 52 DEFAULT_FILE_FOR
49570 4d 41 54 20 31 0a 23 65 6e 64 69 66 0a 0a 23 69 MAT 1.#endif..#i
49580 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 fndef SQLITE_DEF
49590 41 55 4c 54 5f 52 45 43 55 52 53 49 56 45 5f 54 AULT_RECURSIVE_T
495a0 52 49 47 47 45 52 53 0a 23 20 64 65 66 69 6e 65 RIGGERS.# define
495b0 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
495c0 52 45 43 55 52 53 49 56 45 5f 54 52 49 47 47 45 RECURSIVE_TRIGGE
495d0 52 53 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a RS 0.#endif../*.
495e0 2a 2a 20 50 72 6f 76 69 64 65 20 61 20 64 65 66 ** Provide a def
495f0 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 53 ault value for S
49600 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 QLITE_TEMP_STORE
49610 20 69 6e 20 63 61 73 65 20 69 74 20 69 73 20 6e in case it is n
49620 6f 74 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 ot specified.**
49630 6f 6e 20 74 68 65 20 63 6f 6d 6d 61 6e 64 2d 6c on the command-l
49640 69 6e 65 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 ine.*/.#ifndef S
49650 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 QLITE_TEMP_STORE
49660 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
49670 5f 54 45 4d 50 5f 53 54 4f 52 45 20 31 0a 23 65 _TEMP_STORE 1.#e
49680 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 43 43 20 ndif../*.** GCC
49690 64 6f 65 73 20 6e 6f 74 20 64 65 66 69 6e 65 20 does not define
496a0 74 68 65 20 6f 66 66 73 65 74 6f 66 28 29 20 6d the offsetof() m
496b0 61 63 72 6f 20 73 6f 20 77 65 27 6c 6c 20 68 61 acro so we'll ha
496c0 76 65 20 74 6f 20 64 6f 20 69 74 0a 2a 2a 20 6f ve to do it.** o
496d0 75 72 73 65 6c 76 65 73 2e 0a 2a 2f 0a 23 69 66 urselves..*/.#if
496e0 6e 64 65 66 20 6f 66 66 73 65 74 6f 66 0a 23 64 ndef offsetof.#d
496f0 65 66 69 6e 65 20 6f 66 66 73 65 74 6f 66 28 53 efine offsetof(S
49700 54 52 55 43 54 55 52 45 2c 46 49 45 4c 44 29 20 TRUCTURE,FIELD)
49710 28 28 69 6e 74 29 28 28 63 68 61 72 2a 29 26 28 ((int)((char*)&(
49720 28 53 54 52 55 43 54 55 52 45 2a 29 30 29 2d 3e (STRUCTURE*)0)->
49730 46 49 45 4c 44 29 29 0a 23 65 6e 64 69 66 0a 0a FIELD)).#endif..
49740 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 /*.** Check to s
49750 65 65 20 69 66 20 74 68 69 73 20 6d 61 63 68 69 ee if this machi
49760 6e 65 20 75 73 65 73 20 45 42 43 44 49 43 2e 20 ne uses EBCDIC.
49770 20 28 59 65 73 2c 20 62 65 6c 69 65 76 65 20 69 (Yes, believe i
49780 74 20 6f 72 0a 2a 2a 20 6e 6f 74 2c 20 74 68 65 t or.** not, the
49790 72 65 20 61 72 65 20 73 74 69 6c 6c 20 6d 61 63 re are still mac
497a0 68 69 6e 65 73 20 6f 75 74 20 74 68 65 72 65 20 hines out there
497b0 74 68 61 74 20 75 73 65 20 45 42 43 44 49 43 2e that use EBCDIC.
497c0 29 0a 2a 2f 0a 23 69 66 20 27 41 27 20 3d 3d 20 ).*/.#if 'A' ==
497d0 27 5c 33 30 31 27 0a 23 20 64 65 66 69 6e 65 20 '\301'.# define
497e0 53 51 4c 49 54 45 5f 45 42 43 44 49 43 20 31 0a SQLITE_EBCDIC 1.
497f0 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 #else.# define S
49800 51 4c 49 54 45 5f 41 53 43 49 49 20 31 0a 23 65 QLITE_ASCII 1.#e
49810 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 ndif../*.** Inte
49820 67 65 72 73 20 6f 66 20 6b 6e 6f 77 6e 20 73 69 gers of known si
49830 7a 65 73 2e 20 20 54 68 65 73 65 20 74 79 70 65 zes. These type
49840 64 65 66 73 20 6d 69 67 68 74 20 63 68 61 6e 67 defs might chang
49850 65 20 66 6f 72 20 61 72 63 68 69 74 65 63 74 75 e for architectu
49860 72 65 73 0a 2a 2a 20 77 68 65 72 65 20 74 68 65 res.** where the
49870 20 73 69 7a 65 73 20 76 65 72 79 2e 20 20 50 72 sizes very. Pr
49880 65 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f eprocessor macro
49890 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 20 s are available
498a0 73 6f 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 74 so that the.** t
498b0 79 70 65 73 20 63 61 6e 20 62 65 20 63 6f 6e 76 ypes can be conv
498c0 65 6e 69 65 6e 74 6c 79 20 72 65 64 65 66 69 6e eniently redefin
498d0 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 79 ed at compile-ty
498e0 70 65 2e 20 20 4c 69 6b 65 20 74 68 69 73 3a 0a pe. Like this:.
498f0 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 63 63 **.** cc
49900 20 27 2d 44 55 49 4e 54 50 54 52 5f 54 59 50 45 '-DUINTPTR_TYPE
49910 3d 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 27 20 =long long int'
49920 2e 2e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 55 ....*/.#ifndef U
49930 49 4e 54 33 32 5f 54 59 50 45 0a 23 20 69 66 64 INT32_TYPE.# ifd
49940 65 66 20 48 41 56 45 5f 55 49 4e 54 33 32 5f 54 ef HAVE_UINT32_T
49950 0a 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54 33 .# define UINT3
49960 32 5f 54 59 50 45 20 75 69 6e 74 33 32 5f 74 0a 2_TYPE uint32_t.
49970 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 # else.# define
49980 20 55 49 4e 54 33 32 5f 54 59 50 45 20 75 6e 73 UINT32_TYPE uns
49990 69 67 6e 65 64 20 69 6e 74 0a 23 20 65 6e 64 69 igned int.# endi
499a0 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 f.#endif.#ifndef
499b0 20 55 49 4e 54 31 36 5f 54 59 50 45 0a 23 20 69 UINT16_TYPE.# i
499c0 66 64 65 66 20 48 41 56 45 5f 55 49 4e 54 31 36 fdef HAVE_UINT16
499d0 5f 54 0a 23 20 20 64 65 66 69 6e 65 20 55 49 4e _T.# define UIN
499e0 54 31 36 5f 54 59 50 45 20 75 69 6e 74 31 36 5f T16_TYPE uint16_
499f0 74 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 t.# else.# defi
49a00 6e 65 20 55 49 4e 54 31 36 5f 54 59 50 45 20 75 ne UINT16_TYPE u
49a10 6e 73 69 67 6e 65 64 20 73 68 6f 72 74 20 69 6e nsigned short in
49a20 74 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 t.# endif.#endif
49a30 0a 23 69 66 6e 64 65 66 20 49 4e 54 31 36 5f 54 .#ifndef INT16_T
49a40 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 56 45 YPE.# ifdef HAVE
49a50 5f 49 4e 54 31 36 5f 54 0a 23 20 20 64 65 66 69 _INT16_T.# defi
49a60 6e 65 20 49 4e 54 31 36 5f 54 59 50 45 20 69 6e ne INT16_TYPE in
49a70 74 31 36 5f 74 0a 23 20 65 6c 73 65 0a 23 20 20 t16_t.# else.#
49a80 64 65 66 69 6e 65 20 49 4e 54 31 36 5f 54 59 50 define INT16_TYP
49a90 45 20 73 68 6f 72 74 20 69 6e 74 0a 23 20 65 6e E short int.# en
49aa0 64 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 dif.#endif.#ifnd
49ab0 65 66 20 55 49 4e 54 38 5f 54 59 50 45 0a 23 20 ef UINT8_TYPE.#
49ac0 69 66 64 65 66 20 48 41 56 45 5f 55 49 4e 54 38 ifdef HAVE_UINT8
49ad0 5f 54 0a 23 20 20 64 65 66 69 6e 65 20 55 49 4e _T.# define UIN
49ae0 54 38 5f 54 59 50 45 20 75 69 6e 74 38 5f 74 0a T8_TYPE uint8_t.
49af0 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 # else.# define
49b00 20 55 49 4e 54 38 5f 54 59 50 45 20 75 6e 73 69 UINT8_TYPE unsi
49b10 67 6e 65 64 20 63 68 61 72 0a 23 20 65 6e 64 69 gned char.# endi
49b20 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 f.#endif.#ifndef
49b30 20 49 4e 54 38 5f 54 59 50 45 0a 23 20 69 66 64 INT8_TYPE.# ifd
49b40 65 66 20 48 41 56 45 5f 49 4e 54 38 5f 54 0a 23 ef HAVE_INT8_T.#
49b50 20 20 64 65 66 69 6e 65 20 49 4e 54 38 5f 54 59 define INT8_TY
49b60 50 45 20 69 6e 74 38 5f 74 0a 23 20 65 6c 73 65 PE int8_t.# else
49b70 0a 23 20 20 64 65 66 69 6e 65 20 49 4e 54 38 5f .# define INT8_
49b80 54 59 50 45 20 73 69 67 6e 65 64 20 63 68 61 72 TYPE signed char
49b90 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a .# endif.#endif.
49ba0 23 69 66 6e 64 65 66 20 4c 4f 4e 47 44 4f 55 42 #ifndef LONGDOUB
49bb0 4c 45 5f 54 59 50 45 0a 23 20 64 65 66 69 6e 65 LE_TYPE.# define
49bc0 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 LONGDOUBLE_TYPE
49bd0 20 6c 6f 6e 67 20 64 6f 75 62 6c 65 0a 23 65 6e long double.#en
49be0 64 69 66 0a 74 79 70 65 64 65 66 20 73 71 6c 69 dif.typedef sqli
49bf0 74 65 5f 69 6e 74 36 34 20 69 36 34 3b 20 20 20 te_int64 i64;
49c00 20 20 20 20 20 20 20 2f 2a 20 38 2d 62 79 74 65 /* 8-byte
49c10 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 signed integer
49c20 2a 2f 0a 74 79 70 65 64 65 66 20 73 71 6c 69 74 */.typedef sqlit
49c30 65 5f 75 69 6e 74 36 34 20 75 36 34 3b 20 20 20 e_uint64 u64;
49c40 20 20 20 20 20 20 2f 2a 20 38 2d 62 79 74 65 20 /* 8-byte
49c50 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 unsigned integer
49c60 20 2a 2f 0a 74 79 70 65 64 65 66 20 55 49 4e 54 */.typedef UINT
49c70 33 32 5f 54 59 50 45 20 75 33 32 3b 20 20 20 20 32_TYPE u32;
49c80 20 20 20 20 20 20 20 2f 2a 20 34 2d 62 79 74 65 /* 4-byte
49c90 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 unsigned intege
49ca0 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 55 49 4e r */.typedef UIN
49cb0 54 31 36 5f 54 59 50 45 20 75 31 36 3b 20 20 20 T16_TYPE u16;
49cc0 20 20 20 20 20 20 20 20 2f 2a 20 32 2d 62 79 74 /* 2-byt
49cd0 65 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 e unsigned integ
49ce0 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 49 4e er */.typedef IN
49cf0 54 31 36 5f 54 59 50 45 20 69 31 36 3b 20 20 20 T16_TYPE i16;
49d00 20 20 20 20 20 20 20 20 20 2f 2a 20 32 2d 62 79 /* 2-by
49d10 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 te signed intege
49d20 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 55 49 4e r */.typedef UIN
49d30 54 38 5f 54 59 50 45 20 75 38 3b 20 20 20 20 20 T8_TYPE u8;
49d40 20 20 20 20 20 20 20 20 2f 2a 20 31 2d 62 79 74 /* 1-byt
49d50 65 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 e unsigned integ
49d60 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 49 4e er */.typedef IN
49d70 54 38 5f 54 59 50 45 20 69 38 3b 20 20 20 20 20 T8_TYPE i8;
49d80 20 20 20 20 20 20 20 20 20 2f 2a 20 31 2d 62 79 /* 1-by
49d90 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 te signed intege
49da0 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 49 r */../*.** SQLI
49db0 54 45 5f 4d 41 58 5f 55 33 32 20 69 73 20 61 20 TE_MAX_U32 is a
49dc0 75 36 34 20 63 6f 6e 73 74 61 6e 74 20 74 68 61 u64 constant tha
49dd0 74 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d t is the maximum
49de0 20 75 36 34 20 76 61 6c 75 65 0a 2a 2a 20 74 68 u64 value.** th
49df0 61 74 20 63 61 6e 20 62 65 20 73 74 6f 72 65 64 at can be stored
49e00 20 69 6e 20 61 20 75 33 32 20 77 69 74 68 6f 75 in a u32 withou
49e10 74 20 6c 6f 73 73 20 6f 66 20 64 61 74 61 2e 20 t loss of data.
49e20 20 54 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 73 The value.** is
49e30 20 30 78 30 30 30 30 30 30 30 30 66 66 66 66 66 0x00000000fffff
49e40 66 66 66 2e 20 20 42 75 74 20 62 65 63 61 75 73 fff. But becaus
49e50 65 20 6f 66 20 71 75 69 72 6b 73 20 6f 66 20 73 e of quirks of s
49e60 6f 6d 65 20 63 6f 6d 70 69 6c 65 72 73 2c 20 77 ome compilers, w
49e70 65 0a 2a 2a 20 68 61 76 65 20 74 6f 20 73 70 65 e.** have to spe
49e80 63 69 66 79 20 74 68 65 20 76 61 6c 75 65 20 69 cify the value i
49e90 6e 20 74 68 65 20 6c 65 73 73 20 69 6e 74 75 69 n the less intui
49ea0 74 69 76 65 20 6d 61 6e 6e 65 72 20 73 68 6f 77 tive manner show
49eb0 6e 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 n:.*/.#define SQ
49ec0 4c 49 54 45 5f 4d 41 58 5f 55 33 32 20 20 28 28 LITE_MAX_U32 ((
49ed0 28 28 75 36 34 29 31 29 3c 3c 33 32 29 2d 31 29 ((u64)1)<<32)-1)
49ee0 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 74 ../*.** Macros t
49ef0 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65 74 o determine whet
49f00 68 65 72 20 74 68 65 20 6d 61 63 68 69 6e 65 20 her the machine
49f10 69 73 20 62 69 67 20 6f 72 20 6c 69 74 74 6c 65 is big or little
49f20 20 65 6e 64 69 61 6e 2c 0a 2a 2a 20 65 76 61 6c endian,.** eval
49f30 75 61 74 65 64 20 61 74 20 72 75 6e 74 69 6d 65 uated at runtime
49f40 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
49f50 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 0a TE_AMALGAMATION.
49f60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
49f70 6f 6e 73 74 20 69 6e 74 20 73 71 6c 69 74 65 33 onst int sqlite3
49f80 6f 6e 65 20 3d 20 31 3b 0a 23 65 6c 73 65 0a 53 one = 1;.#else.S
49f90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f QLITE_PRIVATE co
49fa0 6e 73 74 20 69 6e 74 20 73 71 6c 69 74 65 33 6f nst int sqlite3o
49fb0 6e 65 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 64 ne;.#endif.#if d
49fc0 65 66 69 6e 65 64 28 69 33 38 36 29 20 7c 7c 20 efined(i386) ||
49fd0 64 65 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f defined(__i386__
49fe0 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f ) || defined(_M_
49ff0 49 58 38 36 29 5c 0a 20 20 20 20 20 20 20 20 20 IX86)\.
4a000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4a010 20 20 20 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f || defined(_
4a020 5f 78 38 36 5f 36 34 29 20 7c 7c 20 64 65 66 69 _x86_64) || defi
4a030 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 0a ned(__x86_64__).
4a040 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
4a050 42 49 47 45 4e 44 49 41 4e 20 20 20 20 30 0a 23 BIGENDIAN 0.#
4a060 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c define SQLITE_L
4a070 49 54 54 4c 45 45 4e 44 49 41 4e 20 31 0a 23 20 ITTLEENDIAN 1.#
4a080 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 define SQLITE_UT
4a090 46 31 36 4e 41 54 49 56 45 20 20 53 51 4c 49 54 F16NATIVE SQLIT
4a0a0 45 5f 55 54 46 31 36 4c 45 0a 23 65 6c 73 65 0a E_UTF16LE.#else.
4a0b0 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
4a0c0 42 49 47 45 4e 44 49 41 4e 20 20 20 20 28 2a 28 BIGENDIAN (*(
4a0d0 63 68 61 72 20 2a 29 28 26 73 71 6c 69 74 65 33 char *)(&sqlite3
4a0e0 6f 6e 65 29 3d 3d 30 29 0a 23 20 64 65 66 69 6e one)==0).# defin
4a0f0 65 20 53 51 4c 49 54 45 5f 4c 49 54 54 4c 45 45 e SQLITE_LITTLEE
4a100 4e 44 49 41 4e 20 28 2a 28 63 68 61 72 20 2a 29 NDIAN (*(char *)
4a110 28 26 73 71 6c 69 74 65 33 6f 6e 65 29 3d 3d 31 (&sqlite3one)==1
4a120 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 ).# define SQLIT
4a130 45 5f 55 54 46 31 36 4e 41 54 49 56 45 20 28 53 E_UTF16NATIVE (S
4a140 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e 3f QLITE_BIGENDIAN?
4a150 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 3a 53 SQLITE_UTF16BE:S
4a160 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 29 0a 23 QLITE_UTF16LE).#
4a170 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e endif../*.** Con
4a180 73 74 61 6e 74 73 20 66 6f 72 20 74 68 65 20 6c stants for the l
4a190 61 72 67 65 73 74 20 61 6e 64 20 73 6d 61 6c 6c argest and small
4a1a0 65 73 74 20 70 6f 73 73 69 62 6c 65 20 36 34 2d est possible 64-
4a1b0 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 bit signed integ
4a1c0 65 72 73 2e 0a 2a 2a 20 54 68 65 73 65 20 6d 61 ers..** These ma
4a1d0 63 72 6f 73 20 61 72 65 20 64 65 73 69 67 6e 65 cros are designe
4a1e0 64 20 74 6f 20 77 6f 72 6b 20 63 6f 72 72 65 63 d to work correc
4a1f0 74 6c 79 20 6f 6e 20 62 6f 74 68 20 33 32 2d 62 tly on both 32-b
4a200 69 74 20 61 6e 64 20 36 34 2d 62 69 74 0a 2a 2a it and 64-bit.**
4a210 20 63 6f 6d 70 69 6c 65 72 73 2e 0a 2a 2f 0a 23 compilers..*/.#
4a220 64 65 66 69 6e 65 20 4c 41 52 47 45 53 54 5f 49 define LARGEST_I
4a230 4e 54 36 34 20 20 28 30 78 66 66 66 66 66 66 66 NT64 (0xfffffff
4a240 66 7c 28 28 28 69 36 34 29 30 78 37 66 66 66 66 f|(((i64)0x7ffff
4a250 66 66 66 29 3c 3c 33 32 29 29 0a 23 64 65 66 69 fff)<<32)).#defi
4a260 6e 65 20 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 ne SMALLEST_INT6
4a270 34 20 28 28 28 69 36 34 29 2d 31 29 20 2d 20 4c 4 (((i64)-1) - L
4a280 41 52 47 45 53 54 5f 49 4e 54 36 34 29 0a 0a 2f ARGEST_INT64)../
4a290 2a 20 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 * .** Round up a
4a2a0 20 6e 75 6d 62 65 72 20 74 6f 20 74 68 65 20 6e number to the n
4a2b0 65 78 74 20 6c 61 72 67 65 72 20 6d 75 6c 74 69 ext larger multi
4a2c0 70 6c 65 20 6f 66 20 38 2e 20 20 54 68 69 73 20 ple of 8. This
4a2d0 69 73 20 75 73 65 64 0a 2a 2a 20 74 6f 20 66 6f is used.** to fo
4a2e0 72 63 65 20 38 2d 62 79 74 65 20 61 6c 69 67 6e rce 8-byte align
4a2f0 6d 65 6e 74 20 6f 6e 20 36 34 2d 62 69 74 20 61 ment on 64-bit a
4a300 72 63 68 69 74 65 63 74 75 72 65 73 2e 0a 2a 2f rchitectures..*/
4a310 0a 23 64 65 66 69 6e 65 20 52 4f 55 4e 44 38 28 .#define ROUND8(
4a320 78 29 20 20 20 20 20 28 28 28 78 29 2b 37 29 26 x) (((x)+7)&
4a330 7e 37 29 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 ~7)../*.** Round
4a340 20 64 6f 77 6e 20 74 6f 20 74 68 65 20 6e 65 61 down to the nea
4a350 72 65 73 74 20 6d 75 6c 74 69 70 6c 65 20 6f 66 rest multiple of
4a360 20 38 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52 4f 8.*/.#define RO
4a370 55 4e 44 44 4f 57 4e 38 28 78 29 20 28 28 78 29 UNDDOWN8(x) ((x)
4a380 26 7e 37 29 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 65 &~7)../*.** Asse
4a390 72 74 20 74 68 61 74 20 74 68 65 20 70 6f 69 6e rt that the poin
4a3a0 74 65 72 20 58 20 69 73 20 61 6c 69 67 6e 65 64 ter X is aligned
4a3b0 20 74 6f 20 61 6e 20 38 2d 62 79 74 65 20 62 6f to an 8-byte bo
4a3c0 75 6e 64 61 72 79 2e 0a 2a 2f 0a 23 64 65 66 69 undary..*/.#defi
4a3d0 6e 65 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c ne EIGHT_BYTE_AL
4a3e0 49 47 4e 4d 45 4e 54 28 58 29 20 20 20 28 28 28 IGNMENT(X) (((
4a3f0 28 63 68 61 72 2a 29 28 58 29 20 2d 20 28 63 68 (char*)(X) - (ch
4a400 61 72 2a 29 30 29 26 37 29 3d 3d 30 29 0a 0a 0a ar*)0)&7)==0)...
4a410 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 /*.** An instanc
4a420 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
4a430 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 73 20 ng structure is
4a440 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 used to store th
4a450 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 0a 2a e busy-handler.*
4a460 2a 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61 * callback for a
4a470 20 67 69 76 65 6e 20 73 71 6c 69 74 65 20 68 61 given sqlite ha
4a480 6e 64 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 ndle. .**.** The
4a490 20 73 71 6c 69 74 65 2e 62 75 73 79 48 61 6e 64 sqlite.busyHand
4a4a0 6c 65 72 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 ler member of th
4a4b0 65 20 73 71 6c 69 74 65 20 73 74 72 75 63 74 20 e sqlite struct
4a4c0 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 62 75 73 contains the bus
4a4d0 79 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 66 6f y.** callback fo
4a4e0 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 r the database h
4a4f0 61 6e 64 6c 65 2e 20 45 61 63 68 20 70 61 67 65 andle. Each page
4a500 72 20 6f 70 65 6e 65 64 20 76 69 61 20 74 68 65 r opened via the
4a510 20 73 71 6c 69 74 65 0a 2a 2a 20 68 61 6e 64 6c sqlite.** handl
4a520 65 20 69 73 20 70 61 73 73 65 64 20 61 20 70 6f e is passed a po
4a530 69 6e 74 65 72 20 74 6f 20 73 71 6c 69 74 65 2e inter to sqlite.
4a540 62 75 73 79 48 61 6e 64 6c 65 72 2e 20 54 68 65 busyHandler. The
4a550 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 0a 2a 2a busy-handler.**
4a560 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 63 75 72 callback is cur
4a570 72 65 6e 74 6c 79 20 69 6e 76 6f 6b 65 64 20 6f rently invoked o
4a580 6e 6c 79 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 nly from within
4a590 70 61 67 65 72 2e 63 2e 0a 2a 2f 0a 74 79 70 65 pager.c..*/.type
4a5a0 64 65 66 20 73 74 72 75 63 74 20 42 75 73 79 48 def struct BusyH
4a5b0 61 6e 64 6c 65 72 20 42 75 73 79 48 61 6e 64 6c andler BusyHandl
4a5c0 65 72 3b 0a 73 74 72 75 63 74 20 42 75 73 79 48 er;.struct BusyH
4a5d0 61 6e 64 6c 65 72 20 7b 0a 20 20 69 6e 74 20 28 andler {. int (
4a5e0 2a 78 46 75 6e 63 29 28 76 6f 69 64 20 2a 2c 69 *xFunc)(void *,i
4a5f0 6e 74 29 3b 20 20 2f 2a 20 54 68 65 20 62 75 73 nt); /* The bus
4a600 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 y callback */.
4a610 76 6f 69 64 20 2a 70 41 72 67 3b 20 20 20 20 20 void *pArg;
4a620 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 /* Fi
4a630 72 73 74 20 61 72 67 20 74 6f 20 62 75 73 79 20 rst arg to busy
4a640 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 6e callback */. in
4a650 74 20 6e 42 75 73 79 3b 20 20 20 20 20 20 20 20 t nBusy;
4a660 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 63 72 /* Incr
4a670 65 6d 65 6e 74 65 64 20 77 69 74 68 20 65 61 63 emented with eac
4a680 68 20 62 75 73 79 20 63 61 6c 6c 20 2a 2f 0a 7d h busy call */.}
4a690 3b 0a 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 20 6f 66 ;../*.** Name of
4a6a0 20 74 68 65 20 6d 61 73 74 65 72 20 64 61 74 61 the master data
4a6b0 62 61 73 65 20 74 61 62 6c 65 2e 20 20 54 68 65 base table. The
4a6c0 20 6d 61 73 74 65 72 20 64 61 74 61 62 61 73 65 master database
4a6d0 20 74 61 62 6c 65 0a 2a 2a 20 69 73 20 61 20 73 table.** is a s
4a6e0 70 65 63 69 61 6c 20 74 61 62 6c 65 20 74 68 61 pecial table tha
4a6f0 74 20 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d 65 t holds the name
4a700 73 20 61 6e 64 20 61 74 74 72 69 62 75 74 65 73 s and attributes
4a710 20 6f 66 20 61 6c 6c 0a 2a 2a 20 75 73 65 72 20 of all.** user
4a720 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 63 tables and indic
4a730 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d es..*/.#define M
4a740 41 53 54 45 52 5f 4e 41 4d 45 20 20 20 20 20 20 ASTER_NAME
4a750 20 22 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 22 "sqlite_master"
4a760 0a 23 64 65 66 69 6e 65 20 54 45 4d 50 5f 4d 41 .#define TEMP_MA
4a770 53 54 45 52 5f 4e 41 4d 45 20 20 22 73 71 6c 69 STER_NAME "sqli
4a780 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 22 0a te_temp_master".
4a790 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f 74 2d ./*.** The root-
4a7a0 70 61 67 65 20 6f 66 20 74 68 65 20 6d 61 73 74 page of the mast
4a7b0 65 72 20 64 61 74 61 62 61 73 65 20 74 61 62 6c er database tabl
4a7c0 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 41 e..*/.#define MA
4a7d0 53 54 45 52 5f 52 4f 4f 54 20 20 20 20 20 20 20 STER_ROOT
4a7e0 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 1../*.** The nam
4a7f0 65 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61 20 e of the schema
4a800 74 61 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e table..*/.#defin
4a810 65 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 78 e SCHEMA_TABLE(x
4a820 29 20 20 28 28 21 4f 4d 49 54 5f 54 45 4d 50 44 ) ((!OMIT_TEMPD
4a830 42 29 26 26 28 78 3d 3d 31 29 3f 54 45 4d 50 5f B)&&(x==1)?TEMP_
4a840 4d 41 53 54 45 52 5f 4e 41 4d 45 3a 4d 41 53 54 MASTER_NAME:MAST
4a850 45 52 5f 4e 41 4d 45 29 0a 0a 2f 2a 0a 2a 2a 20 ER_NAME)../*.**
4a860 41 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 6d 61 A convenience ma
4a870 63 72 6f 20 74 68 61 74 20 72 65 74 75 72 6e 73 cro that returns
4a880 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 the number of e
4a890 6c 65 6d 65 6e 74 73 20 69 6e 0a 2a 2a 20 61 6e lements in.** an
4a8a0 20 61 72 72 61 79 2e 0a 2a 2f 0a 23 64 65 66 69 array..*/.#defi
4a8b0 6e 65 20 41 72 72 61 79 53 69 7a 65 28 58 29 20 ne ArraySize(X)
4a8c0 20 20 20 28 28 69 6e 74 29 28 73 69 7a 65 6f 66 ((int)(sizeof
4a8d0 28 58 29 2f 73 69 7a 65 6f 66 28 58 5b 30 5d 29 (X)/sizeof(X[0])
4a8e0 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f ))../*.** The fo
4a8f0 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 20 61 73 llowing value as
4a900 20 61 20 64 65 73 74 72 75 63 74 6f 72 20 6d 65 a destructor me
4a910 61 6e 73 20 74 6f 20 75 73 65 20 73 71 6c 69 74 ans to use sqlit
4a920 65 33 44 62 46 72 65 65 28 29 2e 0a 2a 2a 20 54 e3DbFree()..** T
4a930 68 69 73 20 69 73 20 61 6e 20 69 6e 74 65 72 6e his is an intern
4a940 61 6c 20 65 78 74 65 6e 73 69 6f 6e 20 74 6f 20 al extension to
4a950 53 51 4c 49 54 45 5f 53 54 41 54 49 43 20 61 6e SQLITE_STATIC an
4a960 64 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 d SQLITE_TRANSIE
4a970 4e 54 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 NT..*/.#define S
4a980 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 20 20 20 QLITE_DYNAMIC
4a990 28 28 73 71 6c 69 74 65 33 5f 64 65 73 74 72 75 ((sqlite3_destru
4a9a0 63 74 6f 72 5f 74 79 70 65 29 73 71 6c 69 74 65 ctor_type)sqlite
4a9b0 33 44 62 46 72 65 65 29 0a 0a 2f 2a 0a 2a 2a 20 3DbFree)../*.**
4a9c0 57 68 65 6e 20 53 51 4c 49 54 45 5f 4f 4d 49 54 When SQLITE_OMIT
4a9d0 5f 57 53 44 20 69 73 20 64 65 66 69 6e 65 64 2c _WSD is defined,
4a9e0 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 it means that t
4a9f0 68 65 20 74 61 72 67 65 74 20 70 6c 61 74 66 6f he target platfo
4aa00 72 6d 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 73 rm does.** not s
4aa10 75 70 70 6f 72 74 20 57 72 69 74 61 62 6c 65 20 upport Writable
4aa20 53 74 61 74 69 63 20 44 61 74 61 20 28 57 53 44 Static Data (WSD
4aa30 29 20 73 75 63 68 20 61 73 20 67 6c 6f 62 61 6c ) such as global
4aa40 20 61 6e 64 20 73 74 61 74 69 63 20 76 61 72 69 and static vari
4aa50 61 62 6c 65 73 2e 0a 2a 2a 20 41 6c 6c 20 76 61 ables..** All va
4aa60 72 69 61 62 6c 65 73 20 6d 75 73 74 20 65 69 74 riables must eit
4aa70 68 65 72 20 62 65 20 6f 6e 20 74 68 65 20 73 74 her be on the st
4aa80 61 63 6b 20 6f 72 20 64 79 6e 61 6d 69 63 61 6c ack or dynamical
4aa90 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 66 72 6f ly allocated fro
4aaa0 6d 0a 2a 2a 20 74 68 65 20 68 65 61 70 2e 20 20 m.** the heap.
4aab0 57 68 65 6e 20 57 53 44 20 69 73 20 75 6e 73 75 When WSD is unsu
4aac0 70 70 6f 72 74 65 64 2c 20 74 68 65 20 76 61 72 pported, the var
4aad0 69 61 62 6c 65 20 64 65 63 6c 61 72 61 74 69 6f iable declaratio
4aae0 6e 73 20 73 63 61 74 74 65 72 65 64 0a 2a 2a 20 ns scattered.**
4aaf0 74 68 72 6f 75 67 68 6f 75 74 20 74 68 65 20 53 throughout the S
4ab00 51 4c 69 74 65 20 63 6f 64 65 20 6d 75 73 74 20 QLite code must
4ab10 62 65 63 6f 6d 65 20 63 6f 6e 73 74 61 6e 74 73 become constants
4ab20 20 69 6e 73 74 65 61 64 2e 20 20 54 68 65 20 53 instead. The S
4ab30 51 4c 49 54 45 5f 57 53 44 0a 2a 2a 20 6d 61 63 QLITE_WSD.** mac
4ab40 72 6f 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 ro is used for t
4ab50 68 69 73 20 70 75 72 70 6f 73 65 2e 20 20 41 6e his purpose. An
4ab60 64 20 69 6e 73 74 65 61 64 20 6f 66 20 72 65 66 d instead of ref
4ab70 65 72 65 6e 63 69 6e 67 20 74 68 65 20 76 61 72 erencing the var
4ab80 69 61 62 6c 65 0a 2a 2a 20 64 69 72 65 63 74 6c iable.** directl
4ab90 79 2c 20 77 65 20 75 73 65 20 69 74 73 20 63 6f y, we use its co
4aba0 6e 73 74 61 6e 74 20 61 73 20 61 20 6b 65 79 20 nstant as a key
4abb0 74 6f 20 6c 6f 6f 6b 75 70 20 74 68 65 20 72 75 to lookup the ru
4abc0 6e 2d 74 69 6d 65 20 61 6c 6c 6f 63 61 74 65 64 n-time allocated
4abd0 0a 2a 2a 20 62 75 66 66 65 72 20 74 68 61 74 20 .** buffer that
4abe0 68 6f 6c 64 73 20 72 65 61 6c 20 76 61 72 69 61 holds real varia
4abf0 62 6c 65 2e 20 20 54 68 65 20 63 6f 6e 73 74 61 ble. The consta
4ac00 6e 74 20 69 73 20 61 6c 73 6f 20 74 68 65 20 69 nt is also the i
4ac10 6e 69 74 69 61 6c 69 7a 65 72 0a 2a 2a 20 66 6f nitializer.** fo
4ac20 72 20 74 68 65 20 72 75 6e 2d 74 69 6d 65 20 61 r the run-time a
4ac30 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 2e llocated buffer.
4ac40 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 75 73 .**.** In the us
4ac50 75 61 6c 20 63 61 73 65 20 77 68 65 72 65 20 57 ual case where W
4ac60 53 44 20 69 73 20 73 75 70 70 6f 72 74 65 64 2c SD is supported,
4ac70 20 74 68 65 20 53 51 4c 49 54 45 5f 57 53 44 20 the SQLITE_WSD
4ac80 61 6e 64 20 47 4c 4f 42 41 4c 0a 2a 2a 20 6d 61 and GLOBAL.** ma
4ac90 63 72 6f 73 20 62 65 63 6f 6d 65 20 6e 6f 2d 6f cros become no-o
4aca0 70 73 20 61 6e 64 20 68 61 76 65 20 7a 65 72 6f ps and have zero
4acb0 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 69 6d 70 performance imp
4acc0 61 63 74 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 act..*/.#ifdef S
4acd0 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 20 QLITE_OMIT_WSD.
4ace0 20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f #define SQLITE_
4acf0 57 53 44 20 63 6f 6e 73 74 0a 20 20 23 64 65 66 WSD const. #def
4ad00 69 6e 65 20 47 4c 4f 42 41 4c 28 74 2c 76 29 20 ine GLOBAL(t,v)
4ad10 28 2a 28 74 2a 29 73 71 6c 69 74 65 33 5f 77 73 (*(t*)sqlite3_ws
4ad20 64 5f 66 69 6e 64 28 28 76 6f 69 64 2a 29 26 28 d_find((void*)&(
4ad30 76 29 2c 20 73 69 7a 65 6f 66 28 76 29 29 29 0a v), sizeof(v))).
4ad40 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 #define sqlite
4ad50 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 20 47 4c 3GlobalConfig GL
4ad60 4f 42 41 4c 28 73 74 72 75 63 74 20 53 71 6c 69 OBAL(struct Sqli
4ad70 74 65 33 43 6f 6e 66 69 67 2c 20 73 71 6c 69 74 te3Config, sqlit
4ad80 65 33 43 6f 6e 66 69 67 29 0a 53 51 4c 49 54 45 e3Config).SQLITE
4ad90 5f 41 50 49 20 20 20 69 6e 74 20 73 71 6c 69 74 _API int sqlit
4ada0 65 33 5f 77 73 64 5f 69 6e 69 74 28 69 6e 74 20 e3_wsd_init(int
4adb0 4e 2c 20 69 6e 74 20 4a 29 3b 0a 53 51 4c 49 54 N, int J);.SQLIT
4adc0 45 5f 41 50 49 20 20 20 76 6f 69 64 20 2a 73 71 E_API void *sq
4add0 6c 69 74 65 33 5f 77 73 64 5f 66 69 6e 64 28 76 lite3_wsd_find(v
4ade0 6f 69 64 20 2a 4b 2c 20 69 6e 74 20 4c 29 3b 0a oid *K, int L);.
4adf0 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 #else. #define
4ae00 53 51 4c 49 54 45 5f 57 53 44 20 0a 20 20 23 64 SQLITE_WSD . #d
4ae10 65 66 69 6e 65 20 47 4c 4f 42 41 4c 28 74 2c 76 efine GLOBAL(t,v
4ae20 29 20 76 0a 20 20 23 64 65 66 69 6e 65 20 73 71 ) v. #define sq
4ae30 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
4ae40 67 20 73 71 6c 69 74 65 33 43 6f 6e 66 69 67 0a g sqlite3Config.
4ae50 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 #endif../*.** Th
4ae60 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 e following macr
4ae70 6f 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 73 os are used to s
4ae80 75 70 70 72 65 73 73 20 63 6f 6d 70 69 6c 65 72 uppress compiler
4ae90 20 77 61 72 6e 69 6e 67 73 20 61 6e 64 20 74 6f warnings and to
4aea0 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 63 6c 65 61 .** make it clea
4aeb0 72 20 74 6f 20 68 75 6d 61 6e 20 72 65 61 64 65 r to human reade
4aec0 72 73 20 77 68 65 6e 20 61 20 66 75 6e 63 74 69 rs when a functi
4aed0 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 on parameter is
4aee0 64 65 6c 69 62 65 72 61 74 65 6c 79 20 0a 2a 2a deliberately .**
4aef0 20 6c 65 66 74 20 75 6e 75 73 65 64 20 77 69 74 left unused wit
4af00 68 69 6e 20 74 68 65 20 62 6f 64 79 20 6f 66 20 hin the body of
4af10 61 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 69 73 a function. This
4af20 20 75 73 75 61 6c 6c 79 20 68 61 70 70 65 6e 73 usually happens
4af30 20 77 68 65 6e 0a 2a 2a 20 61 20 66 75 6e 63 74 when.** a funct
4af40 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 76 69 ion is called vi
4af50 61 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 a a function poi
4af60 6e 74 65 72 2e 20 46 6f 72 20 65 78 61 6d 70 6c nter. For exampl
4af70 65 20 74 68 65 20 0a 2a 2a 20 69 6d 70 6c 65 6d e the .** implem
4af80 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20 53 entation of an S
4af90 51 4c 20 61 67 67 72 65 67 61 74 65 20 73 74 65 QL aggregate ste
4afa0 70 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20 6e p callback may n
4afb0 6f 74 20 75 73 65 20 74 68 65 0a 2a 2a 20 70 61 ot use the.** pa
4afc0 72 61 6d 65 74 65 72 20 69 6e 64 69 63 61 74 69 rameter indicati
4afd0 6e 67 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ng the number of
4afe0 20 61 72 67 75 6d 65 6e 74 73 20 70 61 73 73 65 arguments passe
4aff0 64 20 74 6f 20 74 68 65 20 61 67 67 72 65 67 61 d to the aggrega
4b000 74 65 2c 0a 2a 2a 20 69 66 20 69 74 20 6b 6e 6f te,.** if it kno
4b010 77 73 20 74 68 61 74 20 74 68 69 73 20 69 73 20 ws that this is
4b020 65 6e 66 6f 72 63 65 64 20 65 6c 73 65 77 68 65 enforced elsewhe
4b030 72 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 re..**.** When a
4b040 20 66 75 6e 63 74 69 6f 6e 20 70 61 72 61 6d 65 function parame
4b050 74 65 72 20 69 73 20 6e 6f 74 20 75 73 65 64 20 ter is not used
4b060 61 74 20 61 6c 6c 20 77 69 74 68 69 6e 20 74 68 at all within th
4b070 65 20 62 6f 64 79 20 6f 66 20 61 20 66 75 6e 63 e body of a func
4b080 74 69 6f 6e 2c 0a 2a 2a 20 69 74 20 69 73 20 67 tion,.** it is g
4b090 65 6e 65 72 61 6c 6c 79 20 6e 61 6d 65 64 20 22 enerally named "
4b0a0 4e 6f 74 55 73 65 64 22 20 6f 72 20 22 4e 6f 74 NotUsed" or "Not
4b0b0 55 73 65 64 32 22 20 74 6f 20 6d 61 6b 65 20 74 Used2" to make t
4b0c0 68 69 6e 67 73 20 65 76 65 6e 20 63 6c 65 61 72 hings even clear
4b0d0 65 72 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 er..** However,
4b0e0 74 68 65 73 65 20 6d 61 63 72 6f 73 20 6d 61 79 these macros may
4b0f0 20 61 6c 73 6f 20 62 65 20 75 73 65 64 20 74 6f also be used to
4b100 20 73 75 70 70 72 65 73 73 20 77 61 72 6e 69 6e suppress warnin
4b110 67 73 20 72 65 6c 61 74 65 64 20 74 6f 0a 2a 2a gs related to.**
4b120 20 70 61 72 61 6d 65 74 65 72 73 20 74 68 61 74 parameters that
4b130 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20 may or may not
4b140 62 65 20 75 73 65 64 20 64 65 70 65 6e 64 69 6e be used dependin
4b150 67 20 6f 6e 20 63 6f 6d 70 69 6c 61 74 69 6f 6e g on compilation
4b160 20 6f 70 74 69 6f 6e 73 2e 0a 2a 2a 20 46 6f 72 options..** For
4b170 20 65 78 61 6d 70 6c 65 20 74 68 6f 73 65 20 70 example those p
4b180 61 72 61 6d 65 74 65 72 73 20 6f 6e 6c 79 20 75 arameters only u
4b190 73 65 64 20 69 6e 20 61 73 73 65 72 74 28 29 20 sed in assert()
4b1a0 73 74 61 74 65 6d 65 6e 74 73 2e 20 49 6e 20 74 statements. In t
4b1b0 68 65 73 65 0a 2a 2a 20 63 61 73 65 73 20 74 68 hese.** cases th
4b1c0 65 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 e parameters are
4b1d0 20 6e 61 6d 65 64 20 61 73 20 70 65 72 20 74 68 named as per th
4b1e0 65 20 75 73 75 61 6c 20 63 6f 6e 76 65 6e 74 69 e usual conventi
4b1f0 6f 6e 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ons..*/.#define
4b200 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
4b210 28 78 29 20 28 76 6f 69 64 29 28 78 29 0a 23 64 (x) (void)(x).#d
4b220 65 66 69 6e 65 20 55 4e 55 53 45 44 5f 50 41 52 efine UNUSED_PAR
4b230 41 4d 45 54 45 52 32 28 78 2c 79 29 20 55 4e 55 AMETER2(x,y) UNU
4b240 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 78 29 SED_PARAMETER(x)
4b250 2c 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 ,UNUSED_PARAMETE
4b260 52 28 79 29 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 R(y)../*.** Forw
4b270 61 72 64 20 72 65 66 65 72 65 6e 63 65 73 20 74 ard references t
4b280 6f 20 73 74 72 75 63 74 75 72 65 73 0a 2a 2f 0a o structures.*/.
4b290 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 41 typedef struct A
4b2a0 67 67 49 6e 66 6f 20 41 67 67 49 6e 66 6f 3b 0a ggInfo AggInfo;.
4b2b0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 41 typedef struct A
4b2c0 75 74 68 43 6f 6e 74 65 78 74 20 41 75 74 68 43 uthContext AuthC
4b2d0 6f 6e 74 65 78 74 3b 0a 74 79 70 65 64 65 66 20 ontext;.typedef
4b2e0 73 74 72 75 63 74 20 41 75 74 6f 69 6e 63 49 6e struct AutoincIn
4b2f0 66 6f 20 41 75 74 6f 69 6e 63 49 6e 66 6f 3b 0a fo AutoincInfo;.
4b300 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 42 typedef struct B
4b310 69 74 76 65 63 20 42 69 74 76 65 63 3b 0a 74 79 itvec Bitvec;.ty
4b320 70 65 64 65 66 20 73 74 72 75 63 74 20 52 6f 77 pedef struct Row
4b330 53 65 74 20 52 6f 77 53 65 74 3b 0a 74 79 70 65 Set RowSet;.type
4b340 64 65 66 20 73 74 72 75 63 74 20 43 6f 6c 6c 53 def struct CollS
4b350 65 71 20 43 6f 6c 6c 53 65 71 3b 0a 74 79 70 65 eq CollSeq;.type
4b360 64 65 66 20 73 74 72 75 63 74 20 43 6f 6c 75 6d def struct Colum
4b370 6e 20 43 6f 6c 75 6d 6e 3b 0a 74 79 70 65 64 65 n Column;.typede
4b380 66 20 73 74 72 75 63 74 20 44 62 20 44 62 3b 0a f struct Db Db;.
4b390 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 typedef struct S
4b3a0 63 68 65 6d 61 20 53 63 68 65 6d 61 3b 0a 74 79 chema Schema;.ty
4b3b0 70 65 64 65 66 20 73 74 72 75 63 74 20 45 78 70 pedef struct Exp
4b3c0 72 20 45 78 70 72 3b 0a 74 79 70 65 64 65 66 20 r Expr;.typedef
4b3d0 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 20 struct ExprList
4b3e0 45 78 70 72 4c 69 73 74 3b 0a 74 79 70 65 64 65 ExprList;.typede
4b3f0 66 20 73 74 72 75 63 74 20 45 78 70 72 53 70 61 f struct ExprSpa
4b400 6e 20 45 78 70 72 53 70 61 6e 3b 0a 74 79 70 65 n ExprSpan;.type
4b410 64 65 66 20 73 74 72 75 63 74 20 46 4b 65 79 20 def struct FKey
4b420 46 4b 65 79 3b 0a 74 79 70 65 64 65 66 20 73 74 FKey;.typedef st
4b430 72 75 63 74 20 46 75 6e 63 44 65 66 20 46 75 6e ruct FuncDef Fun
4b440 63 44 65 66 3b 0a 74 79 70 65 64 65 66 20 73 74 cDef;.typedef st
4b450 72 75 63 74 20 46 75 6e 63 44 65 66 48 61 73 68 ruct FuncDefHash
4b460 20 46 75 6e 63 44 65 66 48 61 73 68 3b 0a 74 79 FuncDefHash;.ty
4b470 70 65 64 65 66 20 73 74 72 75 63 74 20 49 64 4c pedef struct IdL
4b480 69 73 74 20 49 64 4c 69 73 74 3b 0a 74 79 70 65 ist IdList;.type
4b490 64 65 66 20 73 74 72 75 63 74 20 49 6e 64 65 78 def struct Index
4b4a0 20 49 6e 64 65 78 3b 0a 74 79 70 65 64 65 66 20 Index;.typedef
4b4b0 73 74 72 75 63 74 20 49 6e 64 65 78 53 61 6d 70 struct IndexSamp
4b4c0 6c 65 20 49 6e 64 65 78 53 61 6d 70 6c 65 3b 0a le IndexSample;.
4b4d0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4b typedef struct K
4b4e0 65 79 43 6c 61 73 73 20 4b 65 79 43 6c 61 73 73 eyClass KeyClass
4b4f0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
4b500 20 4b 65 79 49 6e 66 6f 20 4b 65 79 49 6e 66 6f KeyInfo KeyInfo
4b510 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
4b520 20 4c 6f 6f 6b 61 73 69 64 65 20 4c 6f 6f 6b 61 Lookaside Looka
4b530 73 69 64 65 3b 0a 74 79 70 65 64 65 66 20 73 74 side;.typedef st
4b540 72 75 63 74 20 4c 6f 6f 6b 61 73 69 64 65 53 6c ruct LookasideSl
4b550 6f 74 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 ot LookasideSlot
4b560 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
4b570 20 4d 6f 64 75 6c 65 20 4d 6f 64 75 6c 65 3b 0a Module Module;.
4b580 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4e typedef struct N
4b590 61 6d 65 43 6f 6e 74 65 78 74 20 4e 61 6d 65 43 ameContext NameC
4b5a0 6f 6e 74 65 78 74 3b 0a 74 79 70 65 64 65 66 20 ontext;.typedef
4b5b0 73 74 72 75 63 74 20 50 61 72 73 65 20 50 61 72 struct Parse Par
4b5c0 73 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 se;.typedef stru
4b5d0 63 74 20 53 61 76 65 70 6f 69 6e 74 20 53 61 76 ct Savepoint Sav
4b5e0 65 70 6f 69 6e 74 3b 0a 74 79 70 65 64 65 66 20 epoint;.typedef
4b5f0 73 74 72 75 63 74 20 53 65 6c 65 63 74 20 53 65 struct Select Se
4b600 6c 65 63 74 3b 0a 74 79 70 65 64 65 66 20 73 74 lect;.typedef st
4b610 72 75 63 74 20 53 72 63 4c 69 73 74 20 53 72 63 ruct SrcList Src
4b620 4c 69 73 74 3b 0a 74 79 70 65 64 65 66 20 73 74 List;.typedef st
4b630 72 75 63 74 20 53 74 72 41 63 63 75 6d 20 53 74 ruct StrAccum St
4b640 72 41 63 63 75 6d 3b 0a 74 79 70 65 64 65 66 20 rAccum;.typedef
4b650 73 74 72 75 63 74 20 54 61 62 6c 65 20 54 61 62 struct Table Tab
4b660 6c 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 le;.typedef stru
4b670 63 74 20 54 61 62 6c 65 4c 6f 63 6b 20 54 61 62 ct TableLock Tab
4b680 6c 65 4c 6f 63 6b 3b 0a 74 79 70 65 64 65 66 20 leLock;.typedef
4b690 73 74 72 75 63 74 20 54 6f 6b 65 6e 20 54 6f 6b struct Token Tok
4b6a0 65 6e 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 en;.typedef stru
4b6b0 63 74 20 54 72 69 67 67 65 72 50 72 67 20 54 72 ct TriggerPrg Tr
4b6c0 69 67 67 65 72 50 72 67 3b 0a 74 79 70 65 64 65 iggerPrg;.typede
4b6d0 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72 f struct Trigger
4b6e0 53 74 65 70 20 54 72 69 67 67 65 72 53 74 65 70 Step TriggerStep
4b6f0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
4b700 20 54 72 69 67 67 65 72 20 54 72 69 67 67 65 72 Trigger Trigger
4b710 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
4b720 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 UnpackedRecord
4b730 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 3b 0a UnpackedRecord;.
4b740 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 56 typedef struct V
4b750 54 61 62 6c 65 20 56 54 61 62 6c 65 3b 0a 74 79 Table VTable;.ty
4b760 70 65 64 65 66 20 73 74 72 75 63 74 20 57 61 6c pedef struct Wal
4b770 6b 65 72 20 57 61 6c 6b 65 72 3b 0a 74 79 70 65 ker Walker;.type
4b780 64 65 66 20 73 74 72 75 63 74 20 57 68 65 72 65 def struct Where
4b790 50 6c 61 6e 20 57 68 65 72 65 50 6c 61 6e 3b 0a Plan WherePlan;.
4b7a0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 57 typedef struct W
4b7b0 68 65 72 65 49 6e 66 6f 20 57 68 65 72 65 49 6e hereInfo WhereIn
4b7c0 66 6f 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 fo;.typedef stru
4b7d0 63 74 20 57 68 65 72 65 4c 65 76 65 6c 20 57 68 ct WhereLevel Wh
4b7e0 65 72 65 4c 65 76 65 6c 3b 0a 0a 2f 2a 0a 2a 2a ereLevel;../*.**
4b7f0 20 44 65 66 65 72 20 73 6f 75 72 63 69 6e 67 20 Defer sourcing
4b800 76 64 62 65 2e 68 20 61 6e 64 20 62 74 72 65 65 vdbe.h and btree
4b810 2e 68 20 75 6e 74 69 6c 20 61 66 74 65 72 20 74 .h until after t
4b820 68 65 20 22 75 38 22 20 61 6e 64 20 0a 2a 2a 20 he "u8" and .**
4b830 22 42 75 73 79 48 61 6e 64 6c 65 72 22 20 74 79 "BusyHandler" ty
4b840 70 65 64 65 66 73 2e 20 76 64 62 65 2e 68 20 61 pedefs. vdbe.h a
4b850 6c 73 6f 20 72 65 71 75 69 72 65 73 20 61 20 66 lso requires a f
4b860 65 77 20 6f 66 20 74 68 65 20 6f 70 61 71 75 65 ew of the opaque
4b870 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 79 70 65 .** pointer type
4b880 73 20 28 69 2e 65 2e 20 46 75 6e 63 44 65 66 29 s (i.e. FuncDef)
4b890 20 64 65 66 69 6e 65 64 20 61 62 6f 76 65 2e 0a defined above..
4b8a0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
4b8b0 2a 2a 20 49 6e 63 6c 75 64 65 20 62 74 72 65 65 ** Include btree
4b8c0 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 .h in the middle
4b8d0 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 of sqliteInt.h
4b8e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4b8f0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
4b900 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 62 74 ** Begin file bt
4b910 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ree.h **********
4b920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4b930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4b940 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 */./*.** 2001 Se
4b950 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a ptember 15.**.**
4b960 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
4b970 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
4b980 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
4b990 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
4b9a0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
4b9b0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
4b9c0 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
4b9d0 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
4b9e0 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
4b9f0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
4ba00 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
4ba10 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
4ba20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
4ba30 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
4ba40 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
4ba50 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
4ba60 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
4ba70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4ba80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4ba90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4baa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4bab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 *********.** Thi
4bac0 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 65 s header file de
4bad0 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72 66 fines the interf
4bae0 61 63 65 20 74 68 61 74 20 74 68 65 20 73 71 6c ace that the sql
4baf0 69 74 65 20 42 2d 54 72 65 65 20 66 69 6c 65 0a ite B-Tree file.
4bb00 2a 2a 20 73 75 62 73 79 73 74 65 6d 2e 20 20 53 ** subsystem. S
4bb10 65 65 20 63 6f 6d 6d 65 6e 74 73 20 69 6e 20 74 ee comments in t
4bb20 68 65 20 73 6f 75 72 63 65 20 63 6f 64 65 20 66 he source code f
4bb30 6f 72 20 61 20 64 65 74 61 69 6c 65 64 20 64 65 or a detailed de
4bb40 73 63 72 69 70 74 69 6f 6e 0a 2a 2a 20 6f 66 20 scription.** of
4bb50 77 68 61 74 20 65 61 63 68 20 69 6e 74 65 72 66 what each interf
4bb60 61 63 65 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 ace routine does
4bb70 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 ..**.** @(#) $Id
4bb80 3a 20 62 74 72 65 65 2e 68 2c 76 20 31 2e 31 32 : btree.h,v 1.12
4bb90 30 20 32 30 30 39 2f 30 37 2f 32 32 20 30 30 3a 0 2009/07/22 00:
4bba0 33 35 3a 32 34 20 64 72 68 20 45 78 70 20 24 0a 35:24 drh Exp $.
4bbb0 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 42 54 52 45 */.#ifndef _BTRE
4bbc0 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 42 54 E_H_.#define _BT
4bbd0 52 45 45 5f 48 5f 0a 0a 2f 2a 20 54 4f 44 4f 3a REE_H_../* TODO:
4bbe0 20 54 68 69 73 20 64 65 66 69 6e 69 74 69 6f 6e This definition
4bbf0 20 69 73 20 6a 75 73 74 20 69 6e 63 6c 75 64 65 is just include
4bc00 64 20 73 6f 20 6f 74 68 65 72 20 6d 6f 64 75 6c d so other modul
4bc10 65 73 20 63 6f 6d 70 69 6c 65 2e 20 49 74 0a 2a es compile. It.*
4bc20 2a 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 * needs to be re
4bc30 76 69 73 69 74 65 64 2e 0a 2a 2f 0a 23 64 65 66 visited..*/.#def
4bc40 69 6e 65 20 53 51 4c 49 54 45 5f 4e 5f 42 54 52 ine SQLITE_N_BTR
4bc50 45 45 5f 4d 45 54 41 20 31 30 0a 0a 2f 2a 0a 2a EE_META 10../*.*
4bc60 2a 20 49 66 20 64 65 66 69 6e 65 64 20 61 73 20 * If defined as
4bc70 6e 6f 6e 2d 7a 65 72 6f 2c 20 61 75 74 6f 2d 76 non-zero, auto-v
4bc80 61 63 75 75 6d 20 69 73 20 65 6e 61 62 6c 65 64 acuum is enabled
4bc90 20 62 79 20 64 65 66 61 75 6c 74 2e 20 4f 74 68 by default. Oth
4bca0 65 72 77 69 73 65 0a 2a 2a 20 69 74 20 6d 75 73 erwise.** it mus
4bcb0 74 20 62 65 20 74 75 72 6e 65 64 20 6f 6e 20 66 t be turned on f
4bcc0 6f 72 20 65 61 63 68 20 64 61 74 61 62 61 73 65 or each database
4bcd0 20 75 73 69 6e 67 20 22 50 52 41 47 4d 41 20 61 using "PRAGMA a
4bce0 75 74 6f 5f 76 61 63 75 75 6d 20 3d 20 31 22 2e uto_vacuum = 1".
4bcf0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
4bd00 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 TE_DEFAULT_AUTOV
4bd10 41 43 55 55 4d 0a 20 20 23 64 65 66 69 6e 65 20 ACUUM. #define
4bd20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 SQLITE_DEFAULT_A
4bd30 55 54 4f 56 41 43 55 55 4d 20 30 0a 23 65 6e 64 UTOVACUUM 0.#end
4bd40 69 66 0a 0a 23 64 65 66 69 6e 65 20 42 54 52 45 if..#define BTRE
4bd50 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e 4f 4e E_AUTOVACUUM_NON
4bd60 45 20 30 20 20 20 20 20 20 20 20 2f 2a 20 44 6f E 0 /* Do
4bd70 20 6e 6f 74 20 64 6f 20 61 75 74 6f 2d 76 61 63 not do auto-vac
4bd80 75 75 6d 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 uum */.#define B
4bd90 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f TREE_AUTOVACUUM_
4bda0 46 55 4c 4c 20 31 20 20 20 20 20 20 20 20 2f 2a FULL 1 /*
4bdb0 20 44 6f 20 66 75 6c 6c 20 61 75 74 6f 2d 76 61 Do full auto-va
4bdc0 63 75 75 6d 20 2a 2f 0a 23 64 65 66 69 6e 65 20 cuum */.#define
4bdd0 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d BTREE_AUTOVACUUM
4bde0 5f 49 4e 43 52 20 32 20 20 20 20 20 20 20 20 2f _INCR 2 /
4bdf0 2a 20 49 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 * Incremental va
4be00 63 75 75 6d 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46 cuum */../*.** F
4be10 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 orward declarati
4be20 6f 6e 73 20 6f 66 20 73 74 72 75 63 74 75 72 65 ons of structure
4be30 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
4be40 63 74 20 42 74 72 65 65 20 42 74 72 65 65 3b 0a ct Btree Btree;.
4be50 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 42 typedef struct B
4be60 74 43 75 72 73 6f 72 20 42 74 43 75 72 73 6f 72 tCursor BtCursor
4be70 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
4be80 20 42 74 53 68 61 72 65 64 20 42 74 53 68 61 72 BtShared BtShar
4be90 65 64 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 ed;.typedef stru
4bea0 63 74 20 42 74 72 65 65 4d 75 74 65 78 41 72 72 ct BtreeMutexArr
4beb0 61 79 20 42 74 72 65 65 4d 75 74 65 78 41 72 72 ay BtreeMutexArr
4bec0 61 79 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ay;../*.** This
4bed0 73 74 72 75 63 74 75 72 65 20 72 65 63 6f 72 64 structure record
4bee0 73 20 61 6c 6c 20 6f 66 20 74 68 65 20 42 74 72 s all of the Btr
4bef0 65 65 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f ees that need to
4bf00 20 68 6f 6c 64 0a 2a 2a 20 61 20 6d 75 74 65 78 hold.** a mutex
4bf10 20 62 65 66 6f 72 65 20 77 65 20 65 6e 74 65 72 before we enter
4bf20 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 sqlite3VdbeExec
4bf30 28 29 2e 20 20 54 68 65 20 42 74 72 65 65 73 20 (). The Btrees
4bf40 61 72 65 0a 2a 2a 20 61 72 65 20 70 6c 61 63 65 are.** are place
4bf50 64 20 69 6e 20 61 42 74 72 65 65 5b 5d 20 69 6e d in aBtree[] in
4bf60 20 6f 72 64 65 72 20 6f 66 20 61 42 74 72 65 65 order of aBtree
4bf70 5b 5d 2d 3e 70 42 74 2e 20 20 54 68 61 74 20 77 []->pBt. That w
4bf80 61 79 2c 0a 2a 2a 20 77 65 20 63 61 6e 20 61 6c ay,.** we can al
4bf90 77 61 79 73 20 6c 6f 63 6b 20 61 6e 64 20 75 6e ways lock and un
4bfa0 6c 6f 63 6b 20 74 68 65 6d 20 61 6c 6c 20 71 75 lock them all qu
4bfb0 69 63 6b 6c 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 ickly..*/.struct
4bfc0 20 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 BtreeMutexArray
4bfd0 20 7b 0a 20 20 69 6e 74 20 6e 4d 75 74 65 78 3b {. int nMutex;
4bfe0 0a 20 20 42 74 72 65 65 20 2a 61 42 74 72 65 65 . Btree *aBtree
4bff0 5b 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 [SQLITE_MAX_ATTA
4c000 43 48 45 44 2b 31 5d 3b 0a 7d 3b 0a 0a 0a 53 51 CHED+1];.};...SQ
4c010 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
4c020 20 73 71 6c 69 74 65 33 42 74 72 65 65 4f 70 65 sqlite3BtreeOpe
4c030 6e 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 n(. const char
4c040 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a *zFilename, /*
4c050 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73 Name of databas
4c060 65 20 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a e file to open *
4c070 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c /. sqlite3 *db,
4c080 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
4c090 41 73 73 6f 63 69 61 74 65 64 20 64 61 74 61 62 Associated datab
4c0a0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a ase connection *
4c0b0 2f 0a 20 20 42 74 72 65 65 20 2a 2a 70 70 42 74 /. Btree **ppBt
4c0c0 72 65 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 ree, /*
4c0d0 52 65 74 75 72 6e 20 6f 70 65 6e 20 42 74 72 65 Return open Btre
4c0e0 65 2a 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 e* here */. int
4c0f0 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 flags,
4c100 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 2a /* Flags *
4c110 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73 /. int vfsFlags
4c120 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
4c130 46 6c 61 67 73 20 70 61 73 73 65 64 20 74 68 72 Flags passed thr
4c140 6f 75 67 68 20 74 6f 20 56 46 53 20 6f 70 65 6e ough to VFS open
4c150 20 2a 2f 0a 29 3b 0a 0a 2f 2a 20 54 68 65 20 66 */.);../* The f
4c160 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 74 lags parameter t
4c170 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4f 70 o sqlite3BtreeOp
4c180 65 6e 20 63 61 6e 20 62 65 20 74 68 65 20 62 69 en can be the bi
4c190 74 77 69 73 65 20 6f 72 20 6f 66 20 74 68 65 0a twise or of the.
4c1a0 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c ** following val
4c1b0 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a ues..**.** NOTE:
4c1c0 20 20 54 68 65 73 65 20 76 61 6c 75 65 73 20 6d These values m
4c1d0 75 73 74 20 6d 61 74 63 68 20 74 68 65 20 63 6f ust match the co
4c1e0 72 72 65 73 70 6f 6e 64 69 6e 67 20 50 41 47 45 rresponding PAGE
4c1f0 52 5f 20 76 61 6c 75 65 73 20 69 6e 0a 2a 2a 20 R_ values in.**
4c200 70 61 67 65 72 2e 68 2e 0a 2a 2f 0a 23 64 65 66 pager.h..*/.#def
4c210 69 6e 65 20 42 54 52 45 45 5f 4f 4d 49 54 5f 4a ine BTREE_OMIT_J
4c220 4f 55 52 4e 41 4c 20 20 31 20 20 2f 2a 20 44 6f OURNAL 1 /* Do
4c230 20 6e 6f 74 20 75 73 65 20 6a 6f 75 72 6e 61 6c not use journal
4c240 2e 20 20 4e 6f 20 61 72 67 75 6d 65 6e 74 20 2a . No argument *
4c250 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f /.#define BTREE_
4c260 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20 20 20 32 20 NO_READLOCK 2
4c270 20 2f 2a 20 4f 6d 69 74 20 72 65 61 64 6c 6f 63 /* Omit readloc
4c280 6b 73 20 6f 6e 20 72 65 61 64 6f 6e 6c 79 20 66 ks on readonly f
4c290 69 6c 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 iles */.#define
4c2a0 42 54 52 45 45 5f 4d 45 4d 4f 52 59 20 20 20 20 BTREE_MEMORY
4c2b0 20 20 20 20 34 20 20 2f 2a 20 49 6e 2d 6d 65 6d 4 /* In-mem
4c2c0 6f 72 79 20 44 42 2e 20 20 4e 6f 20 61 72 67 75 ory DB. No argu
4c2d0 6d 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ment */.#define
4c2e0 42 54 52 45 45 5f 52 45 41 44 4f 4e 4c 59 20 20 BTREE_READONLY
4c2f0 20 20 20 20 38 20 20 2f 2a 20 4f 70 65 6e 20 74 8 /* Open t
4c300 68 65 20 64 61 74 61 62 61 73 65 20 69 6e 20 72 he database in r
4c310 65 61 64 2d 6f 6e 6c 79 20 6d 6f 64 65 20 2a 2f ead-only mode */
4c320 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 52 .#define BTREE_R
4c330 45 41 44 57 52 49 54 45 20 20 20 20 31 36 20 20 EADWRITE 16
4c340 2f 2a 20 4f 70 65 6e 20 66 6f 72 20 62 6f 74 68 /* Open for both
4c350 20 72 65 61 64 69 6e 67 20 61 6e 64 20 77 72 69 reading and wri
4c360 74 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ting */.#define
4c370 42 54 52 45 45 5f 43 52 45 41 54 45 20 20 20 20 BTREE_CREATE
4c380 20 20 20 33 32 20 20 2f 2a 20 43 72 65 61 74 65 32 /* Create
4c390 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 66 the database if
4c3a0 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 it does not exi
4c3b0 73 74 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 st */..SQLITE_PR
4c3c0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
4c3d0 33 42 74 72 65 65 43 6c 6f 73 65 28 42 74 72 65 3BtreeClose(Btre
4c3e0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
4c3f0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
4c400 74 72 65 65 53 65 74 43 61 63 68 65 53 69 7a 65 treeSetCacheSize
4c410 28 42 74 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 (Btree*,int);.SQ
4c420 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
4c430 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 sqlite3BtreeSet
4c440 53 61 66 65 74 79 4c 65 76 65 6c 28 42 74 72 65 SafetyLevel(Btre
4c450 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c e*,int,int);.SQL
4c460 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
4c470 73 71 6c 69 74 65 33 42 74 72 65 65 53 79 6e 63 sqlite3BtreeSync
4c480 44 69 73 61 62 6c 65 64 28 42 74 72 65 65 2a 29 Disabled(Btree*)
4c490 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4c4a0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
4c4b0 65 53 65 74 50 61 67 65 53 69 7a 65 28 42 74 72 eSetPageSize(Btr
4c4c0 65 65 20 2a 70 2c 20 69 6e 74 20 6e 50 61 67 65 ee *p, int nPage
4c4d0 73 69 7a 65 2c 20 69 6e 74 20 6e 52 65 73 65 72 size, int nReser
4c4e0 76 65 2c 20 69 6e 74 20 65 46 69 78 29 3b 0a 53 ve, int eFix);.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 47 65 t sqlite3BtreeGe
4c510 74 50 61 67 65 53 69 7a 65 28 42 74 72 65 65 2a tPageSize(Btree*
4c520 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4c530 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
4c540 65 65 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 42 eeMaxPageCount(B
4c550 74 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 tree*,int);.SQLI
4c560 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
4c570 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 52 65 qlite3BtreeGetRe
4c580 73 65 72 76 65 28 42 74 72 65 65 2a 29 3b 0a 53 serve(Btree*);.S
4c590 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4c5a0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 t sqlite3BtreeSe
4c5b0 74 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72 65 tAutoVacuum(Btre
4c5c0 65 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 e *, int);.SQLIT
4c5d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
4c5e0 6c 69 74 65 33 42 74 72 65 65 47 65 74 41 75 74 lite3BtreeGetAut
4c5f0 6f 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a 29 oVacuum(Btree *)
4c600 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4c610 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
4c620 65 42 65 67 69 6e 54 72 61 6e 73 28 42 74 72 65 eBeginTrans(Btre
4c630 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f e*,int);.SQLITE_
4c640 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
4c650 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 te3BtreeCommitPh
4c660 61 73 65 4f 6e 65 28 42 74 72 65 65 2a 2c 20 63 aseOne(Btree*, c
4c670 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 onst char *zMast
4c680 65 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 er);.SQLITE_PRIV
4c690 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
4c6a0 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 treeCommitPhaseT
4c6b0 77 6f 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 wo(Btree*);.SQLI
4c6c0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
4c6d0 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 qlite3BtreeCommi
4c6e0 74 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 t(Btree*);.SQLIT
4c6f0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
4c700 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 lite3BtreeRollba
4c710 63 6b 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 ck(Btree*);.SQLI
4c720 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
4c730 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e qlite3BtreeBegin
4c740 53 74 6d 74 28 42 74 72 65 65 2a 2c 69 6e 74 29 Stmt(Btree*,int)
4c750 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4c760 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
4c770 65 43 72 65 61 74 65 54 61 62 6c 65 28 42 74 72 eCreateTable(Btr
4c780 65 65 2a 2c 20 69 6e 74 2a 2c 20 69 6e 74 20 66 ee*, int*, int f
4c790 6c 61 67 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 lags);.SQLITE_PR
4c7a0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
4c7b0 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 3BtreeIsInTrans(
4c7c0 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f Btree*);.SQLITE_
4c7d0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
4c7e0 74 65 33 42 74 72 65 65 49 73 49 6e 52 65 61 64 te3BtreeIsInRead
4c7f0 54 72 61 6e 73 28 42 74 72 65 65 2a 29 3b 0a 53 Trans(Btree*);.S
4c800 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4c810 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 t sqlite3BtreeIs
4c820 49 6e 42 61 63 6b 75 70 28 42 74 72 65 65 2a 29 InBackup(Btree*)
4c830 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4c840 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 void *sqlite3Bt
4c850 72 65 65 53 63 68 65 6d 61 28 42 74 72 65 65 20 reeSchema(Btree
4c860 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 *, int, void(*)(
4c870 76 6f 69 64 20 2a 29 29 3b 0a 53 51 4c 49 54 45 void *));.SQLITE
4c880 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
4c890 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 4c ite3BtreeSchemaL
4c8a0 6f 63 6b 65 64 28 42 74 72 65 65 20 2a 70 42 74 ocked(Btree *pBt
4c8b0 72 65 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ree);.SQLITE_PRI
4c8c0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
4c8d0 42 74 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28 42 BtreeLockTable(B
4c8e0 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20 69 6e tree *pBtree, in
4c8f0 74 20 69 54 61 62 2c 20 75 38 20 69 73 57 72 69 t iTab, u8 isWri
4c900 74 65 4c 6f 63 6b 29 3b 0a 53 51 4c 49 54 45 5f teLock);.SQLITE_
4c910 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
4c920 74 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e te3BtreeSavepoin
4c930 74 28 42 74 72 65 65 20 2a 2c 20 69 6e 74 2c 20 t(Btree *, int,
4c940 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 int);..SQLITE_PR
4c950 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 IVATE const char
4c960 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 *sqlite3BtreeGe
4c970 74 46 69 6c 65 6e 61 6d 65 28 42 74 72 65 65 20 tFilename(Btree
4c980 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
4c990 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 TE const char *s
4c9a0 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4a 6f qlite3BtreeGetJo
4c9b0 75 72 6e 61 6c 6e 61 6d 65 28 42 74 72 65 65 20 urnalname(Btree
4c9c0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
4c9d0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
4c9e0 72 65 65 43 6f 70 79 46 69 6c 65 28 42 74 72 65 reeCopyFile(Btre
4c9f0 65 20 2a 2c 20 42 74 72 65 65 20 2a 29 3b 0a 0a e *, Btree *);..
4ca00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4ca10 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 nt sqlite3BtreeI
4ca20 6e 63 72 56 61 63 75 75 6d 28 42 74 72 65 65 20 ncrVacuum(Btree
4ca30 2a 29 3b 0a 0a 2f 2a 20 54 68 65 20 66 6c 61 67 *);../* The flag
4ca40 73 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 s parameter to s
4ca50 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74 qlite3BtreeCreat
4ca60 65 54 61 62 6c 65 20 63 61 6e 20 62 65 20 74 68 eTable can be th
4ca70 65 20 62 69 74 77 69 73 65 20 4f 52 0a 2a 2a 20 e bitwise OR.**
4ca80 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
4ca90 20 66 6c 61 67 73 3a 0a 2a 2f 0a 23 64 65 66 69 flags:.*/.#defi
4caa0 6e 65 20 42 54 52 45 45 5f 49 4e 54 4b 45 59 20 ne BTREE_INTKEY
4cab0 20 20 20 20 31 20 20 20 20 2f 2a 20 54 61 62 6c 1 /* Tabl
4cac0 65 20 68 61 73 20 6f 6e 6c 79 20 36 34 2d 62 69 e has only 64-bi
4cad0 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 t signed integer
4cae0 20 6b 65 79 73 20 2a 2f 0a 23 64 65 66 69 6e 65 keys */.#define
4caf0 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 20 BTREE_ZERODATA
4cb00 20 20 32 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 2 /* Table
4cb10 68 61 73 20 6b 65 79 73 20 6f 6e 6c 79 20 2d 20 has keys only -
4cb20 6e 6f 20 64 61 74 61 20 2a 2f 0a 23 64 65 66 69 no data */.#defi
4cb30 6e 65 20 42 54 52 45 45 5f 4c 45 41 46 44 41 54 ne BTREE_LEAFDAT
4cb40 41 20 20 20 34 20 20 20 20 2f 2a 20 44 61 74 61 A 4 /* Data
4cb50 20 73 74 6f 72 65 64 20 69 6e 20 6c 65 61 76 65 stored in leave
4cb60 73 20 6f 6e 6c 79 2e 20 20 49 6d 70 6c 69 65 73 s only. Implies
4cb70 20 49 4e 54 4b 45 59 20 2a 2f 0a 0a 53 51 4c 49 INTKEY */..SQLI
4cb80 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
4cb90 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70 54 qlite3BtreeDropT
4cba0 61 62 6c 65 28 42 74 72 65 65 2a 2c 20 69 6e 74 able(Btree*, int
4cbb0 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f , int*);.SQLITE_
4cbc0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
4cbd0 74 65 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 te3BtreeClearTab
4cbe0 6c 65 28 42 74 72 65 65 2a 2c 20 69 6e 74 2c 20 le(Btree*, int,
4cbf0 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int*);.SQLITE_PR
4cc00 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
4cc10 65 33 42 74 72 65 65 54 72 69 70 41 6c 6c 43 75 e3BtreeTripAllCu
4cc20 72 73 6f 72 73 28 42 74 72 65 65 2a 2c 20 69 6e rsors(Btree*, in
4cc30 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 t);..SQLITE_PRIV
4cc40 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
4cc50 42 74 72 65 65 47 65 74 4d 65 74 61 28 42 74 72 BtreeGetMeta(Btr
4cc60 65 65 20 2a 70 42 74 72 65 65 2c 20 69 6e 74 20 ee *pBtree, int
4cc70 69 64 78 2c 20 75 33 32 20 2a 70 56 61 6c 75 65 idx, u32 *pValue
4cc80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4cc90 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
4cca0 65 65 55 70 64 61 74 65 4d 65 74 61 28 42 74 72 eeUpdateMeta(Btr
4ccb0 65 65 2a 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 ee*, int idx, u3
4ccc0 32 20 76 61 6c 75 65 29 3b 0a 0a 2f 2a 0a 2a 2a 2 value);../*.**
4ccd0 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 The second para
4cce0 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 meter to sqlite3
4ccf0 42 74 72 65 65 47 65 74 4d 65 74 61 20 6f 72 20 BtreeGetMeta or
4cd00 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 sqlite3BtreeUpda
4cd10 74 65 4d 65 74 61 0a 2a 2a 20 73 68 6f 75 6c 64 teMeta.** should
4cd20 20 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 be one of the f
4cd30 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 73 2e ollowing values.
4cd40 20 54 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c The integer val
4cd50 75 65 73 20 61 72 65 20 61 73 73 69 67 6e 65 64 ues are assigned
4cd60 20 0a 2a 2a 20 74 6f 20 63 6f 6e 73 74 61 6e 74 .** to constant
4cd70 73 20 73 6f 20 74 68 61 74 20 74 68 65 20 6f 66 s so that the of
4cd80 66 73 65 74 20 6f 66 20 74 68 65 20 63 6f 72 72 fset of the corr
4cd90 65 73 70 6f 6e 64 69 6e 67 20 66 69 65 6c 64 20 esponding field
4cda0 69 6e 20 61 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 in an.** SQLite
4cdb0 64 61 74 61 62 61 73 65 20 68 65 61 64 65 72 20 database header
4cdc0 6d 61 79 20 62 65 20 66 6f 75 6e 64 20 75 73 69 may be found usi
4cdd0 6e 67 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ng the following
4cde0 20 66 6f 72 6d 75 6c 61 3a 0a 2a 2a 0a 2a 2a 20 formula:.**.**
4cdf0 20 20 6f 66 66 73 65 74 20 3d 20 33 36 20 2b 20 offset = 36 +
4ce00 28 69 64 78 20 2a 20 34 29 0a 2a 2a 0a 2a 2a 20 (idx * 4).**.**
4ce10 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 For example, the
4ce20 20 66 72 65 65 2d 70 61 67 65 2d 63 6f 75 6e 74 free-page-count
4ce30 20 66 69 65 6c 64 20 69 73 20 6c 6f 63 61 74 65 field is locate
4ce40 64 20 61 74 20 62 79 74 65 20 6f 66 66 73 65 74 d at byte offset
4ce50 20 33 36 20 6f 66 0a 2a 2a 20 74 68 65 20 64 61 36 of.** the da
4ce60 74 61 62 61 73 65 20 66 69 6c 65 20 68 65 61 64 tabase file head
4ce70 65 72 2e 20 54 68 65 20 69 6e 63 72 2d 76 61 63 er. The incr-vac
4ce80 75 75 6d 2d 66 6c 61 67 20 66 69 65 6c 64 20 69 uum-flag field i
4ce90 73 20 6c 6f 63 61 74 65 64 20 61 74 0a 2a 2a 20 s located at.**
4cea0 62 79 74 65 20 6f 66 66 73 65 74 20 36 34 20 28 byte offset 64 (
4ceb0 3d 3d 20 33 36 2b 34 2a 37 29 2e 0a 2a 2f 0a 23 == 36+4*7)..*/.#
4cec0 64 65 66 69 6e 65 20 42 54 52 45 45 5f 46 52 45 define BTREE_FRE
4ced0 45 5f 50 41 47 45 5f 43 4f 55 4e 54 20 20 20 20 E_PAGE_COUNT
4cee0 20 30 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 0.#define BTREE
4cef0 5f 53 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e 20 _SCHEMA_VERSION
4cf00 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 42 1.#define B
4cf10 54 52 45 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 TREE_FILE_FORMAT
4cf20 20 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 2.#defi
4cf30 6e 65 20 42 54 52 45 45 5f 44 45 46 41 55 4c 54 ne BTREE_DEFAULT
4cf40 5f 43 41 43 48 45 5f 53 49 5a 45 20 20 33 0a 23 _CACHE_SIZE 3.#
4cf50 64 65 66 69 6e 65 20 42 54 52 45 45 5f 4c 41 52 define BTREE_LAR
4cf60 47 45 53 54 5f 52 4f 4f 54 5f 50 41 47 45 20 20 GEST_ROOT_PAGE
4cf70 20 34 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 4.#define BTREE
4cf80 5f 54 45 58 54 5f 45 4e 43 4f 44 49 4e 47 20 20 _TEXT_ENCODING
4cf90 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 42 5.#define B
4cfa0 54 52 45 45 5f 55 53 45 52 5f 56 45 52 53 49 4f TREE_USER_VERSIO
4cfb0 4e 20 20 20 20 20 20 20 20 36 0a 23 64 65 66 69 N 6.#defi
4cfc0 6e 65 20 42 54 52 45 45 5f 49 4e 43 52 5f 56 41 ne BTREE_INCR_VA
4cfd0 43 55 55 4d 20 20 20 20 20 20 20 20 20 37 0a 0a CUUM 7..
4cfe0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4cff0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
4d000 75 72 73 6f 72 28 0a 20 20 42 74 72 65 65 2a 2c ursor(. Btree*,
4d010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
4d030 20 42 54 72 65 65 20 63 6f 6e 74 61 69 6e 69 6e BTree containin
4d040 67 20 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 g table to open
4d050 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c */. int iTable,
4d060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d070 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 /* Ind
4d080 65 78 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 20 ex of root page
4d090 2a 2f 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 2c */. int wrFlag,
4d0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d0b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 66 /* 1 f
4d0c0 6f 72 20 77 72 69 74 69 6e 67 2e 20 20 30 20 66 or writing. 0 f
4d0d0 6f 72 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a or read-only */.
4d0e0 20 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f struct KeyInfo
4d0f0 2a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *,
4d100 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 /* First
4d110 61 72 67 75 6d 65 6e 74 20 74 6f 20 63 6f 6d 70 argument to comp
4d120 61 72 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a are function */.
4d130 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 BtCursor *pCur
4d140 73 6f 72 20 20 20 20 20 20 20 20 20 20 20 20 20 sor
4d150 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 /* Space
4d160 74 6f 20 77 72 69 74 65 20 63 75 72 73 6f 72 20 to write cursor
4d170 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 29 3b 0a structure */.);.
4d180 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4d190 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
4d1a0 75 72 73 6f 72 53 69 7a 65 28 76 6f 69 64 29 3b ursorSize(void);
4d1b0 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ..SQLITE_PRIVATE
4d1c0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
4d1d0 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 42 74 43 eCloseCursor(BtC
4d1e0 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f ursor*);.SQLITE_
4d1f0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
4d200 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e te3BtreeMovetoUn
4d210 70 61 63 6b 65 64 28 0a 20 20 42 74 43 75 72 73 packed(. BtCurs
4d220 6f 72 2a 2c 0a 20 20 55 6e 70 61 63 6b 65 64 52 or*,. UnpackedR
4d230 65 63 6f 72 64 20 2a 70 55 6e 4b 65 79 2c 0a 20 ecord *pUnKey,.
4d240 20 69 36 34 20 69 6e 74 4b 65 79 2c 0a 20 20 69 i64 intKey,. i
4d250 6e 74 20 62 69 61 73 2c 0a 20 20 69 6e 74 20 2a nt bias,. int *
4d260 70 52 65 73 0a 29 3b 0a 53 51 4c 49 54 45 5f 50 pRes.);.SQLITE_P
4d270 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
4d280 65 33 42 74 72 65 65 43 75 72 73 6f 72 48 61 73 e3BtreeCursorHas
4d290 4d 6f 76 65 64 28 42 74 43 75 72 73 6f 72 2a 2c Moved(BtCursor*,
4d2a0 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 int*);.SQLITE_P
4d2b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
4d2c0 65 33 42 74 72 65 65 44 65 6c 65 74 65 28 42 74 e3BtreeDelete(Bt
4d2d0 43 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 Cursor*);.SQLITE
4d2e0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
4d2f0 69 74 65 33 42 74 72 65 65 49 6e 73 65 72 74 28 ite3BtreeInsert(
4d300 42 74 43 75 72 73 6f 72 2a 2c 20 63 6f 6e 73 74 BtCursor*, const
4d310 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 36 34 void *pKey, i64
4d320 20 6e 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20 nKey,.
4d330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d340 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 76 const v
4d350 6f 69 64 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 oid *pData, int
4d360 6e 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 nData,.
4d370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d380 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 5a 65 int nZe
4d390 72 6f 2c 20 69 6e 74 20 62 69 61 73 2c 20 69 6e ro, int bias, in
4d3a0 74 20 73 65 65 6b 52 65 73 75 6c 74 29 3b 0a 53 t seekResult);.S
4d3b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4d3c0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 69 t sqlite3BtreeFi
4d3d0 72 73 74 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 rst(BtCursor*, i
4d3e0 6e 74 20 2a 70 52 65 73 29 3b 0a 53 51 4c 49 54 nt *pRes);.SQLIT
4d3f0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
4d400 6c 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 42 lite3BtreeLast(B
4d410 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 tCursor*, int *p
4d420 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 Res);.SQLITE_PRI
4d430 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
4d440 42 74 72 65 65 4e 65 78 74 28 42 74 43 75 72 73 BtreeNext(BtCurs
4d450 6f 72 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b or*, int *pRes);
4d460 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4d470 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
4d480 45 6f 66 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a Eof(BtCursor*);.
4d490 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4d4a0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 nt sqlite3BtreeP
4d4b0 72 65 76 69 6f 75 73 28 42 74 43 75 72 73 6f 72 revious(BtCursor
4d4c0 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53 *, int *pRes);.S
4d4d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4d4e0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 t sqlite3BtreeKe
4d4f0 79 53 69 7a 65 28 42 74 43 75 72 73 6f 72 2a 2c ySize(BtCursor*,
4d500 20 69 36 34 20 2a 70 53 69 7a 65 29 3b 0a 53 51 i64 *pSize);.SQ
4d510 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
4d520 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 sqlite3BtreeKey
4d530 28 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 20 (BtCursor*, u32
4d540 6f 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74 2c offset, u32 amt,
4d550 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f void*);.SQLITE_
4d560 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f PRIVATE const vo
4d570 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 id *sqlite3Btree
4d580 4b 65 79 46 65 74 63 68 28 42 74 43 75 72 73 6f KeyFetch(BtCurso
4d590 72 2a 2c 20 69 6e 74 20 2a 70 41 6d 74 29 3b 0a r*, int *pAmt);.
4d5a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
4d5b0 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 onst void *sqlit
4d5c0 65 33 42 74 72 65 65 44 61 74 61 46 65 74 63 68 e3BtreeDataFetch
4d5d0 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 (BtCursor*, int
4d5e0 2a 70 41 6d 74 29 3b 0a 53 51 4c 49 54 45 5f 50 *pAmt);.SQLITE_P
4d5f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
4d600 65 33 42 74 72 65 65 44 61 74 61 53 69 7a 65 28 e3BtreeDataSize(
4d610 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 20 2a BtCursor*, u32 *
4d620 70 53 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 pSize);.SQLITE_P
4d630 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
4d640 65 33 42 74 72 65 65 44 61 74 61 28 42 74 43 75 e3BtreeData(BtCu
4d650 72 73 6f 72 2a 2c 20 75 33 32 20 6f 66 66 73 65 rsor*, u32 offse
4d660 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 t, u32 amt, void
4d670 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
4d680 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
4d690 74 72 65 65 53 65 74 43 61 63 68 65 64 52 6f 77 treeSetCachedRow
4d6a0 69 64 28 42 74 43 75 72 73 6f 72 2a 2c 20 73 71 id(BtCursor*, sq
4d6b0 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 53 51 lite3_int64);.SQ
4d6c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c LITE_PRIVATE sql
4d6d0 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 ite3_int64 sqlit
4d6e0 65 33 42 74 72 65 65 47 65 74 43 61 63 68 65 64 e3BtreeGetCached
4d6f0 52 6f 77 69 64 28 42 74 43 75 72 73 6f 72 2a 29 Rowid(BtCursor*)
4d700 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ;..SQLITE_PRIVAT
4d710 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 E char *sqlite3B
4d720 74 72 65 65 49 6e 74 65 67 72 69 74 79 43 68 65 treeIntegrityChe
4d730 63 6b 28 42 74 72 65 65 2a 2c 20 69 6e 74 20 2a ck(Btree*, int *
4d740 61 52 6f 6f 74 2c 20 69 6e 74 20 6e 52 6f 6f 74 aRoot, int nRoot
4d750 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53 51 , int, int*);.SQ
4d760 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 74 72 LITE_PRIVATE str
4d770 75 63 74 20 50 61 67 65 72 20 2a 73 71 6c 69 74 uct Pager *sqlit
4d780 65 33 42 74 72 65 65 50 61 67 65 72 28 42 74 72 e3BtreePager(Btr
4d790 65 65 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 ee*);..SQLITE_PR
4d7a0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
4d7b0 33 42 74 72 65 65 50 75 74 44 61 74 61 28 42 74 3BtreePutData(Bt
4d7c0 43 75 72 73 6f 72 2a 2c 20 75 33 32 20 6f 66 66 Cursor*, u32 off
4d7d0 73 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f set, u32 amt, vo
4d7e0 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 id*);.SQLITE_PRI
4d7f0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
4d800 33 42 74 72 65 65 43 61 63 68 65 4f 76 65 72 66 3BtreeCacheOverf
4d810 6c 6f 77 28 42 74 43 75 72 73 6f 72 20 2a 29 3b low(BtCursor *);
4d820 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4d830 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
4d840 65 43 6c 65 61 72 43 75 72 73 6f 72 28 42 74 43 eClearCursor(BtC
4d850 75 72 73 6f 72 20 2a 29 3b 0a 0a 23 69 66 6e 64 ursor *);..#ifnd
4d860 65 66 20 4e 44 45 42 55 47 0a 53 51 4c 49 54 45 ef NDEBUG.SQLITE
4d870 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
4d880 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 49 ite3BtreeCursorI
4d890 73 56 61 6c 69 64 28 42 74 43 75 72 73 6f 72 2a sValid(BtCursor*
4d8a0 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 );.#endif..#ifnd
4d8b0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 ef SQLITE_OMIT_B
4d8c0 54 52 45 45 43 4f 55 4e 54 0a 53 51 4c 49 54 45 TREECOUNT.SQLITE
4d8d0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
4d8e0 69 74 65 33 42 74 72 65 65 43 6f 75 6e 74 28 42 ite3BtreeCount(B
4d8f0 74 43 75 72 73 6f 72 20 2a 2c 20 69 36 34 20 2a tCursor *, i64 *
4d900 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 );.#endif..#ifde
4d910 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 f SQLITE_TEST.SQ
4d920 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
4d930 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 sqlite3BtreeCur
4d940 73 6f 72 49 6e 66 6f 28 42 74 43 75 72 73 6f 72 sorInfo(BtCursor
4d950 2a 2c 20 69 6e 74 2a 2c 20 69 6e 74 29 3b 0a 53 *, int*, int);.S
4d960 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
4d970 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 id sqlite3BtreeC
4d980 75 72 73 6f 72 4c 69 73 74 28 42 74 72 65 65 2a ursorList(Btree*
4d990 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a );.#endif../*.**
4d9a0 20 49 66 20 77 65 20 61 72 65 20 6e 6f 74 20 75 If we are not u
4d9b0 73 69 6e 67 20 73 68 61 72 65 64 20 63 61 63 68 sing shared cach
4d9c0 65 2c 20 74 68 65 6e 20 74 68 65 72 65 20 69 73 e, then there is
4d9d0 20 6e 6f 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 75 no need to.** u
4d9e0 73 65 20 6d 75 74 65 78 65 73 20 74 6f 20 61 63 se mutexes to ac
4d9f0 63 65 73 73 20 74 68 65 20 42 74 53 68 61 72 65 cess the BtShare
4da00 64 20 73 74 72 75 63 74 75 72 65 73 2e 20 20 53 d structures. S
4da10 6f 20 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 45 6e o make the.** En
4da20 74 65 72 20 61 6e 64 20 4c 65 61 76 65 20 70 72 ter and Leave pr
4da30 6f 63 65 64 75 72 65 73 20 6e 6f 2d 6f 70 73 2e ocedures no-ops.
4da40 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
4da50 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 TE_OMIT_SHARED_C
4da60 41 43 48 45 0a 53 51 4c 49 54 45 5f 50 52 49 56 ACHE.SQLITE_PRIV
4da70 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
4da80 65 33 42 74 72 65 65 45 6e 74 65 72 28 42 74 72 e3BtreeEnter(Btr
4da90 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ee*);.SQLITE_PRI
4daa0 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
4dab0 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c te3BtreeEnterAll
4dac0 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23 65 6c 73 (sqlite3*);.#els
4dad0 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 e.# define sqlit
4dae0 65 33 42 74 72 65 65 45 6e 74 65 72 28 58 29 20 e3BtreeEnter(X)
4daf0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
4db00 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 58 3BtreeEnterAll(X
4db10 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 21 64 ).#endif..#if !d
4db20 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
4db30 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 29 IT_SHARED_CACHE)
4db40 20 26 26 20 53 51 4c 49 54 45 5f 54 48 52 45 41 && SQLITE_THREA
4db50 44 53 41 46 45 0a 53 51 4c 49 54 45 5f 50 52 49 DSAFE.SQLITE_PRI
4db60 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
4db70 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 42 74 te3BtreeLeave(Bt
4db80 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ree*);.SQLITE_PR
4db90 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
4dba0 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 43 75 ite3BtreeEnterCu
4dbb0 72 73 6f 72 28 42 74 43 75 72 73 6f 72 2a 29 3b rsor(BtCursor*);
4dbc0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4dbd0 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 void sqlite3Bt
4dbe0 72 65 65 4c 65 61 76 65 43 75 72 73 6f 72 28 42 reeLeaveCursor(B
4dbf0 74 43 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 tCursor*);.SQLIT
4dc00 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 E_PRIVATE void
4dc10 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
4dc20 76 65 41 6c 6c 28 73 71 6c 69 74 65 33 2a 29 3b veAll(sqlite3*);
4dc30 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4dc40 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 void sqlite3Bt
4dc50 72 65 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74 reeMutexArrayEnt
4dc60 65 72 28 42 74 72 65 65 4d 75 74 65 78 41 72 72 er(BtreeMutexArr
4dc70 61 79 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ay*);.SQLITE_PRI
4dc80 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
4dc90 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 te3BtreeMutexArr
4dca0 61 79 4c 65 61 76 65 28 42 74 72 65 65 4d 75 74 ayLeave(BtreeMut
4dcb0 65 78 41 72 72 61 79 2a 29 3b 0a 53 51 4c 49 54 exArray*);.SQLIT
4dcc0 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 E_PRIVATE void
4dcd0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 sqlite3BtreeMut
4dce0 65 78 41 72 72 61 79 49 6e 73 65 72 74 28 42 74 exArrayInsert(Bt
4dcf0 72 65 65 4d 75 74 65 78 41 72 72 61 79 2a 2c 20 reeMutexArray*,
4dd00 42 74 72 65 65 2a 29 3b 0a 23 69 66 6e 64 65 66 Btree*);.#ifndef
4dd10 20 4e 44 45 42 55 47 0a 20 20 2f 2a 20 54 68 65 NDEBUG. /* The
4dd20 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 se routines are
4dd30 75 73 65 64 20 69 6e 73 69 64 65 20 61 73 73 65 used inside asse
4dd40 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 rt() statements
4dd50 6f 6e 6c 79 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f only. */.SQLITE_
4dd60 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 PRIVATE int sq
4dd70 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d lite3BtreeHoldsM
4dd80 75 74 65 78 28 42 74 72 65 65 2a 29 3b 0a 53 51 utex(Btree*);.SQ
4dd90 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 LITE_PRIVATE i
4dda0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 nt sqlite3BtreeH
4ddb0 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 73 oldsAllMutexes(s
4ddc0 71 6c 69 74 65 33 2a 29 3b 0a 23 65 6e 64 69 66 qlite3*);.#endif
4ddd0 0a 23 65 6c 73 65 0a 0a 23 20 64 65 66 69 6e 65 .#else..# define
4dde0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
4ddf0 76 65 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 ve(X).# define s
4de00 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
4de10 43 75 72 73 6f 72 28 58 29 0a 23 20 64 65 66 69 Cursor(X).# defi
4de20 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c ne sqlite3BtreeL
4de30 65 61 76 65 43 75 72 73 6f 72 28 58 29 0a 23 20 eaveCursor(X).#
4de40 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 define sqlite3Bt
4de50 72 65 65 4c 65 61 76 65 41 6c 6c 28 58 29 0a 23 reeLeaveAll(X).#
4de60 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 define sqlite3B
4de70 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 45 6e treeMutexArrayEn
4de80 74 65 72 28 58 29 0a 23 20 64 65 66 69 6e 65 20 ter(X).# define
4de90 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 sqlite3BtreeMute
4dea0 78 41 72 72 61 79 4c 65 61 76 65 28 58 29 0a 23 xArrayLeave(X).#
4deb0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 define sqlite3B
4dec0 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 49 6e treeMutexArrayIn
4ded0 73 65 72 74 28 58 2c 59 29 0a 0a 23 20 64 65 66 sert(X,Y)..# def
4dee0 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 ine sqlite3Btree
4def0 48 6f 6c 64 73 4d 75 74 65 78 28 58 29 20 31 0a HoldsMutex(X) 1.
4df00 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
4df10 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 BtreeHoldsAllMut
4df20 65 78 65 73 28 58 29 20 31 0a 23 65 6e 64 69 66 exes(X) 1.#endif
4df30 0a 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 42 54 ...#endif /* _BT
4df40 52 45 45 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a REE_H_ */../****
4df50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
4df60 66 20 62 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a f btree.h ******
4df70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4df80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4df90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
4dfa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 ********** Conti
4dfb0 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c nuing where we l
4dfc0 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 eft off in sqlit
4dfd0 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a eInt.h *********
4dfe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
4dff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 ********** Inclu
4e000 64 65 20 76 64 62 65 2e 68 20 69 6e 20 74 68 65 de vdbe.h in the
4e010 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 middle of sqlit
4e020 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a eInt.h *********
4e030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
4e040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
4e050 20 66 69 6c 65 20 76 64 62 65 2e 68 20 2a 2a 2a file vdbe.h ***
4e060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
4e090 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 2001 September
4e0a0 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 15.**.** The aut
4e0b0 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
4e0c0 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
4e0d0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
4e0e0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
4e0f0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
4e100 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
4e110 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
4e120 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
4e130 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
4e140 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
4e150 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
4e160 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
4e170 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
4e180 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
4e190 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
4e1a0 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
4e1b0 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
4e1c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e1d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e1e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e1f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e200 2a 0a 2a 2a 20 48 65 61 64 65 72 20 66 69 6c 65 *.** Header file
4e210 20 66 6f 72 20 74 68 65 20 56 69 72 74 75 61 6c for the Virtual
4e220 20 44 61 74 61 42 61 73 65 20 45 6e 67 69 6e 65 DataBase Engine
4e230 20 28 56 44 42 45 29 0a 2a 2a 0a 2a 2a 20 54 68 (VDBE).**.** Th
4e240 69 73 20 68 65 61 64 65 72 20 64 65 66 69 6e 65 is header define
4e250 73 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 s the interface
4e260 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c 20 64 to the virtual d
4e270 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65 0a 2a atabase engine.*
4e280 2a 20 6f 72 20 56 44 42 45 2e 20 20 54 68 65 20 * or VDBE. The
4e290 56 44 42 45 20 69 6d 70 6c 65 6d 65 6e 74 73 20 VDBE implements
4e2a0 61 6e 20 61 62 73 74 72 61 63 74 20 6d 61 63 68 an abstract mach
4e2b0 69 6e 65 20 74 68 61 74 20 72 75 6e 73 20 61 0a ine that runs a.
4e2c0 2a 2a 20 73 69 6d 70 6c 65 20 70 72 6f 67 72 61 ** simple progra
4e2d0 6d 20 74 6f 20 61 63 63 65 73 73 20 61 6e 64 20 m to access and
4e2e0 6d 6f 64 69 66 79 20 74 68 65 20 75 6e 64 65 72 modify the under
4e2f0 6c 79 69 6e 67 20 64 61 74 61 62 61 73 65 2e 0a lying database..
4e300 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 2e **.** $Id: vdbe.
4e310 68 2c 76 20 31 2e 31 34 32 20 32 30 30 39 2f 30 h,v 1.142 2009/0
4e320 37 2f 32 34 20 31 37 3a 35 38 3a 35 33 20 64 61 7/24 17:58:53 da
4e330 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a nielk1977 Exp $.
4e340 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 */.#ifndef _SQLI
4e350 54 45 5f 56 44 42 45 5f 48 5f 0a 23 64 65 66 69 TE_VDBE_H_.#defi
4e360 6e 65 20 5f 53 51 4c 49 54 45 5f 56 44 42 45 5f ne _SQLITE_VDBE_
4e370 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 69 6e 67 H_../*.** A sing
4e380 6c 65 20 56 44 42 45 20 69 73 20 61 6e 20 6f 70 le VDBE is an op
4e390 61 71 75 65 20 73 74 72 75 63 74 75 72 65 20 6e aque structure n
4e3a0 61 6d 65 64 20 22 56 64 62 65 22 2e 20 20 4f 6e amed "Vdbe". On
4e3b0 6c 79 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 69 ly routines.** i
4e3c0 6e 20 74 68 65 20 73 6f 75 72 63 65 20 66 69 6c n the source fil
4e3d0 65 20 73 71 6c 69 74 65 56 64 62 65 2e 63 20 61 e sqliteVdbe.c a
4e3e0 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 73 65 re allowed to se
4e3f0 65 20 74 68 65 20 69 6e 73 69 64 65 73 0a 2a 2a e the insides.**
4e400 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 of this structu
4e410 72 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 re..*/.typedef s
4e420 74 72 75 63 74 20 56 64 62 65 20 56 64 62 65 3b truct Vdbe Vdbe;
4e430 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 ../*.** The name
4e440 73 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 s of the followi
4e450 6e 67 20 74 79 70 65 73 20 64 65 63 6c 61 72 65 ng types declare
4e460 64 20 69 6e 20 76 64 62 65 49 6e 74 2e 68 20 61 d in vdbeInt.h a
4e470 72 65 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 66 re required.** f
4e480 6f 72 20 74 68 65 20 56 64 62 65 4f 70 20 64 65 or the VdbeOp de
4e490 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 finition..*/.typ
4e4a0 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62 65 edef struct Vdbe
4e4b0 46 75 6e 63 20 56 64 62 65 46 75 6e 63 3b 0a 74 Func VdbeFunc;.t
4e4c0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4d 65 ypedef struct Me
4e4d0 6d 20 4d 65 6d 3b 0a 74 79 70 65 64 65 66 20 73 m Mem;.typedef s
4e4e0 74 72 75 63 74 20 53 75 62 50 72 6f 67 72 61 6d truct SubProgram
4e4f0 20 53 75 62 50 72 6f 67 72 61 6d 3b 0a 0a 2f 2a SubProgram;../*
4e500 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 69 6e 73 .** A single ins
4e510 74 72 75 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 truction of the
4e520 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 virtual machine
4e530 68 61 73 20 61 6e 20 6f 70 63 6f 64 65 0a 2a 2a has an opcode.**
4e540 20 61 6e 64 20 61 73 20 6d 61 6e 79 20 61 73 20 and as many as
4e550 74 68 72 65 65 20 6f 70 65 72 61 6e 64 73 2e 20 three operands.
4e560 20 54 68 65 20 69 6e 73 74 72 75 63 74 69 6f 6e The instruction
4e570 20 69 73 20 72 65 63 6f 72 64 65 64 0a 2a 2a 20 is recorded.**
4e580 61 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f as an instance o
4e590 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
4e5a0 73 74 72 75 63 74 75 72 65 3a 0a 2a 2f 0a 73 74 structure:.*/.st
4e5b0 72 75 63 74 20 56 64 62 65 4f 70 20 7b 0a 20 20 ruct VdbeOp {.
4e5c0 75 38 20 6f 70 63 6f 64 65 3b 20 20 20 20 20 20 u8 opcode;
4e5d0 20 20 20 20 2f 2a 20 57 68 61 74 20 6f 70 65 72 /* What oper
4e5e0 61 74 69 6f 6e 20 74 6f 20 70 65 72 66 6f 72 6d ation to perform
4e5f0 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63 68 61 */. signed cha
4e600 72 20 70 34 74 79 70 65 3b 20 2f 2a 20 4f 6e 65 r p4type; /* One
4e610 20 6f 66 20 74 68 65 20 50 34 5f 78 78 78 20 63 of the P4_xxx c
4e620 6f 6e 73 74 61 6e 74 73 20 66 6f 72 20 70 34 20 onstants for p4
4e630 2a 2f 0a 20 20 75 38 20 6f 70 66 6c 61 67 73 3b */. u8 opflags;
4e640 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 /* Not
4e650 63 75 72 72 65 6e 74 6c 79 20 75 73 65 64 20 2a currently used *
4e660 2f 0a 20 20 75 38 20 70 35 3b 20 20 20 20 20 20 /. u8 p5;
4e670 20 20 20 20 20 20 20 20 2f 2a 20 46 69 66 74 68 /* Fifth
4e680 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 6e parameter is an
4e690 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 61 63 unsigned charac
4e6a0 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 31 3b ter */. int p1;
4e6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
4e6c0 46 69 72 73 74 20 6f 70 65 72 61 6e 64 20 2a 2f First operand */
4e6d0 0a 20 20 69 6e 74 20 70 32 3b 20 20 20 20 20 20 . int p2;
4e6e0 20 20 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 /* Second
4e6f0 20 70 61 72 61 6d 65 74 65 72 20 28 6f 66 74 65 parameter (ofte
4e700 6e 20 74 68 65 20 6a 75 6d 70 20 64 65 73 74 69 n the jump desti
4e710 6e 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 69 6e 74 nation) */. int
4e720 20 70 33 3b 20 20 20 20 20 20 20 20 20 20 20 20 p3;
4e730 20 2f 2a 20 54 68 65 20 74 68 69 72 64 20 70 61 /* The third pa
4e740 72 61 6d 65 74 65 72 20 2a 2f 0a 20 20 75 6e 69 rameter */. uni
4e750 6f 6e 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 on {
4e760 20 2f 2a 20 66 6f 75 72 74 68 20 70 61 72 61 6d /* fourth param
4e770 65 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 eter */. int
4e780 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
4e790 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 61 /* Integer va
4e7a0 6c 75 65 20 69 66 20 70 34 74 79 70 65 3d 3d 50 lue if p4type==P
4e7b0 34 5f 49 4e 54 33 32 20 2a 2f 0a 20 20 20 20 76 4_INT32 */. v
4e7c0 6f 69 64 20 2a 70 3b 20 20 20 20 20 20 20 20 20 oid *p;
4e7d0 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 69 63 /* Generic
4e7e0 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 20 20 pointer */.
4e7f0 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 char *z;
4e800 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
4e810 72 20 74 6f 20 64 61 74 61 20 66 6f 72 20 73 74 r to data for st
4e820 72 69 6e 67 20 28 63 68 61 72 20 61 72 72 61 79 ring (char array
4e830 29 20 74 79 70 65 73 20 2a 2f 0a 20 20 20 20 69 ) types */. i
4e840 36 34 20 2a 70 49 36 34 3b 20 20 20 20 20 20 20 64 *pI64;
4e850 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 /* Used wh
4e860 65 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 5f en p4type is P4_
4e870 49 4e 54 36 34 20 2a 2f 0a 20 20 20 20 64 6f 75 INT64 */. dou
4e880 62 6c 65 20 2a 70 52 65 61 6c 3b 20 20 20 20 20 ble *pReal;
4e890 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e /* Used when
4e8a0 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 52 45 p4type is P4_RE
4e8b0 41 4c 20 2a 2f 0a 20 20 20 20 46 75 6e 63 44 65 AL */. FuncDe
4e8c0 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20 f *pFunc;
4e8d0 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34 /* Used when p4
4e8e0 74 79 70 65 20 69 73 20 50 34 5f 46 55 4e 43 44 type is P4_FUNCD
4e8f0 45 46 20 2a 2f 0a 20 20 20 20 56 64 62 65 46 75 EF */. VdbeFu
4e900 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 3b 20 20 nc *pVdbeFunc;
4e910 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34 /* Used when p4
4e920 74 79 70 65 20 69 73 20 50 34 5f 56 44 42 45 46 type is P4_VDBEF
4e930 55 4e 43 20 2a 2f 0a 20 20 20 20 43 6f 6c 6c 53 UNC */. CollS
4e940 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20 eq *pColl;
4e950 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 /* Used when p
4e960 34 74 79 70 65 20 69 73 20 50 34 5f 43 4f 4c 4c 4type is P4_COLL
4e970 53 45 51 20 2a 2f 0a 20 20 20 20 4d 65 6d 20 2a SEQ */. Mem *
4e980 70 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 pMem;
4e990 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 /* Used when p
4e9a0 34 74 79 70 65 20 69 73 20 50 34 5f 4d 45 4d 20 4type is P4_MEM
4e9b0 2a 2f 0a 20 20 20 20 56 54 61 62 6c 65 20 2a 70 */. VTable *p
4e9c0 56 74 61 62 3b 20 20 20 20 20 20 20 20 20 2f 2a Vtab; /*
4e9d0 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 Used when p4typ
4e9e0 65 20 69 73 20 50 34 5f 56 54 41 42 20 2a 2f 0a e is P4_VTAB */.
4e9f0 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 KeyInfo *pKe
4ea00 79 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20 55 73 yInfo; /* Us
4ea10 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 ed when p4type i
4ea20 73 20 50 34 5f 4b 45 59 49 4e 46 4f 20 2a 2f 0a s P4_KEYINFO */.
4ea30 20 20 20 20 69 6e 74 20 2a 61 69 3b 20 20 20 20 int *ai;
4ea40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 /* Us
4ea50 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 ed when p4type i
4ea60 73 20 50 34 5f 49 4e 54 41 52 52 41 59 20 2a 2f s P4_INTARRAY */
4ea70 0a 20 20 20 20 53 75 62 50 72 6f 67 72 61 6d 20 . SubProgram
4ea80 2a 70 50 72 6f 67 72 61 6d 3b 20 20 2f 2a 20 55 *pProgram; /* U
4ea90 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 sed when p4type
4eaa0 69 73 20 50 34 5f 53 55 42 50 52 4f 47 52 41 4d is P4_SUBPROGRAM
4eab0 20 2a 2f 0a 20 20 7d 20 70 34 3b 0a 23 69 66 64 */. } p4;.#ifd
4eac0 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
4ead0 20 20 63 68 61 72 20 2a 7a 43 6f 6d 6d 65 6e 74 char *zComment
4eae0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f ; /* Co
4eaf0 6d 6d 65 6e 74 20 74 6f 20 69 6d 70 72 6f 76 65 mment to improve
4eb00 20 72 65 61 64 61 62 69 6c 69 74 79 20 2a 2f 0a readability */.
4eb10 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 56 44 #endif.#ifdef VD
4eb20 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 69 6e 74 BE_PROFILE. int
4eb30 20 63 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 cnt;
4eb40 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
4eb50 6f 66 20 74 69 6d 65 73 20 74 68 69 73 20 69 6e of times this in
4eb60 73 74 72 75 63 74 69 6f 6e 20 77 61 73 20 65 78 struction was ex
4eb70 65 63 75 74 65 64 20 2a 2f 0a 20 20 75 36 34 20 ecuted */. u64
4eb80 63 79 63 6c 65 73 3b 20 20 20 20 20 20 20 20 20 cycles;
4eb90 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 74 69 /* Total ti
4eba0 6d 65 20 73 70 65 6e 74 20 65 78 65 63 75 74 69 me spent executi
4ebb0 6e 67 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 ng this instruct
4ebc0 69 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b ion */.#endif.};
4ebd0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4ebe0 56 64 62 65 4f 70 20 56 64 62 65 4f 70 3b 0a 0a VdbeOp VdbeOp;..
4ebf0 0a 2f 2a 0a 2a 2a 20 41 20 73 75 62 2d 72 6f 75 ./*.** A sub-rou
4ec00 74 69 6e 65 20 75 73 65 64 20 74 6f 20 69 6d 70 tine used to imp
4ec10 6c 65 6d 65 6e 74 20 61 20 74 72 69 67 67 65 72 lement a trigger
4ec20 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 73 74 72 program..*/.str
4ec30 75 63 74 20 53 75 62 50 72 6f 67 72 61 6d 20 7b uct SubProgram {
4ec40 0a 20 20 56 64 62 65 4f 70 20 2a 61 4f 70 3b 20 . VdbeOp *aOp;
4ec50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ec60 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 6f 70 63 /* Array of opc
4ec70 6f 64 65 73 20 66 6f 72 20 73 75 62 2d 70 72 6f odes for sub-pro
4ec80 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f gram */. int nO
4ec90 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p;
4eca0 20 20 20 20 20 20 20 20 2f 2a 20 45 6c 65 6d 65 /* Eleme
4ecb0 6e 74 73 20 69 6e 20 61 4f 70 5b 5d 20 2a 2f 0a nts in aOp[] */.
4ecc0 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 20 int nMem;
4ecd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ece0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d /* Number of mem
4ecf0 6f 72 79 20 63 65 6c 6c 73 20 72 65 71 75 69 72 ory cells requir
4ed00 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 73 72 ed */. int nCsr
4ed10 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
4ed20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
4ed30 6f 66 20 63 75 72 73 6f 72 73 20 72 65 71 75 69 of cursors requi
4ed40 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 red */. int nRe
4ed50 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 f;
4ed60 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
4ed70 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 of pointers to
4ed80 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a this structure *
4ed90 2f 0a 20 20 76 6f 69 64 20 2a 74 6f 6b 65 6e 3b /. void *token;
4eda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4edb0 20 20 2f 2a 20 69 64 20 74 68 61 74 20 6d 61 79 /* id that may
4edc0 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 63 75 be used to recu
4edd0 72 73 69 76 65 20 74 72 69 67 67 65 72 73 20 2a rsive triggers *
4ede0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 6d /.};../*.** A sm
4edf0 61 6c 6c 65 72 20 76 65 72 73 69 6f 6e 20 6f 66 aller version of
4ee00 20 56 64 62 65 4f 70 20 75 73 65 64 20 66 6f 72 VdbeOp used for
4ee10 20 74 68 65 20 56 64 62 65 41 64 64 4f 70 4c 69 the VdbeAddOpLi
4ee20 73 74 28 29 20 66 75 6e 63 74 69 6f 6e 20 62 65 st() function be
4ee30 63 61 75 73 65 0a 2a 2a 20 69 74 20 74 61 6b 65 cause.** it take
4ee40 73 20 75 70 20 6c 65 73 73 20 73 70 61 63 65 2e s up less space.
4ee50 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65 4f .*/.struct VdbeO
4ee60 70 4c 69 73 74 20 7b 0a 20 20 75 38 20 6f 70 63 pList {. u8 opc
4ee70 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ode; /*
4ee80 20 57 68 61 74 20 6f 70 65 72 61 74 69 6f 6e 20 What operation
4ee90 74 6f 20 70 65 72 66 6f 72 6d 20 2a 2f 0a 20 20 to perform */.
4eea0 73 69 67 6e 65 64 20 63 68 61 72 20 70 31 3b 20 signed char p1;
4eeb0 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6f 70 65 /* First ope
4eec0 72 61 6e 64 20 2a 2f 0a 20 20 73 69 67 6e 65 64 rand */. signed
4eed0 20 63 68 61 72 20 70 32 3b 20 20 20 20 20 2f 2a char p2; /*
4eee0 20 53 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 Second paramete
4eef0 72 20 28 6f 66 74 65 6e 20 74 68 65 20 6a 75 6d r (often the jum
4ef00 70 20 64 65 73 74 69 6e 61 74 69 6f 6e 29 20 2a p destination) *
4ef10 2f 0a 20 20 73 69 67 6e 65 64 20 63 68 61 72 20 /. signed char
4ef20 70 33 3b 20 20 20 20 20 2f 2a 20 54 68 69 72 64 p3; /* Third
4ef30 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 7d 3b parameter */.};
4ef40 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4ef50 56 64 62 65 4f 70 4c 69 73 74 20 56 64 62 65 4f VdbeOpList VdbeO
4ef60 70 4c 69 73 74 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c pList;../*.** Al
4ef70 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 6f 66 20 lowed values of
4ef80 56 64 62 65 4f 70 2e 70 34 74 79 70 65 0a 2a 2f VdbeOp.p4type.*/
4ef90 0a 23 64 65 66 69 6e 65 20 50 34 5f 4e 4f 54 55 .#define P4_NOTU
4efa0 53 45 44 20 20 20 20 30 20 20 20 2f 2a 20 54 68 SED 0 /* Th
4efb0 65 20 50 34 20 70 61 72 61 6d 65 74 65 72 20 69 e P4 parameter i
4efc0 73 20 6e 6f 74 20 75 73 65 64 20 2a 2f 0a 23 64 s not used */.#d
4efd0 65 66 69 6e 65 20 50 34 5f 44 59 4e 41 4d 49 43 efine P4_DYNAMIC
4efe0 20 20 28 2d 31 29 20 20 2f 2a 20 50 6f 69 6e 74 (-1) /* Point
4eff0 65 72 20 74 6f 20 61 20 73 74 72 69 6e 67 20 6f er to a string o
4f000 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c btained from sql
4f010 69 74 65 4d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 23 iteMalloc() */.#
4f020 64 65 66 69 6e 65 20 50 34 5f 53 54 41 54 49 43 define P4_STATIC
4f030 20 20 20 28 2d 32 29 20 20 2f 2a 20 50 6f 69 6e (-2) /* Poin
4f040 74 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 ter to a static
4f050 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e string */.#defin
4f060 65 20 50 34 5f 43 4f 4c 4c 53 45 51 20 20 28 2d e P4_COLLSEQ (-
4f070 34 29 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70 4) /* P4 is a p
4f080 6f 69 6e 74 65 72 20 74 6f 20 61 20 43 6f 6c 6c ointer to a Coll
4f090 53 65 71 20 73 74 72 75 63 74 75 72 65 20 2a 2f Seq structure */
4f0a0 0a 23 64 65 66 69 6e 65 20 50 34 5f 46 55 4e 43 .#define P4_FUNC
4f0b0 44 45 46 20 20 28 2d 35 29 20 20 2f 2a 20 50 34 DEF (-5) /* P4
4f0c0 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
4f0d0 20 61 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 a FuncDef struc
4f0e0 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ture */.#define
4f0f0 50 34 5f 4b 45 59 49 4e 46 4f 20 20 28 2d 36 29 P4_KEYINFO (-6)
4f100 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 /* P4 is a poi
4f110 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e 66 nter to a KeyInf
4f120 6f 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 o structure */.#
4f130 64 65 66 69 6e 65 20 50 34 5f 56 44 42 45 46 55 define P4_VDBEFU
4f140 4e 43 20 28 2d 37 29 20 20 2f 2a 20 50 34 20 69 NC (-7) /* P4 i
4f150 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
4f160 20 56 64 62 65 46 75 6e 63 20 73 74 72 75 63 74 VdbeFunc struct
4f170 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 ure */.#define P
4f180 34 5f 4d 45 4d 20 20 20 20 20 20 28 2d 38 29 20 4_MEM (-8)
4f190 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e /* P4 is a poin
4f1a0 74 65 72 20 74 6f 20 61 20 4d 65 6d 2a 20 20 20 ter to a Mem*
4f1b0 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 64 structure */.#d
4f1c0 65 66 69 6e 65 20 50 34 5f 54 52 41 4e 53 49 45 efine P4_TRANSIE
4f1d0 4e 54 20 28 2d 39 29 20 2f 2a 20 50 34 20 69 73 NT (-9) /* P4 is
4f1e0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
4f1f0 74 72 61 6e 73 69 65 6e 74 20 73 74 72 69 6e 67 transient string
4f200 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 56 */.#define P4_V
4f210 54 41 42 20 20 20 20 20 28 2d 31 30 29 20 2f 2a TAB (-10) /*
4f220 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 P4 is a pointer
4f230 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 to an sqlite3_v
4f240 74 61 62 20 73 74 72 75 63 74 75 72 65 20 2a 2f tab structure */
4f250 0a 23 64 65 66 69 6e 65 20 50 34 5f 4d 50 52 49 .#define P4_MPRI
4f260 4e 54 46 20 20 28 2d 31 31 29 20 2f 2a 20 50 34 NTF (-11) /* P4
4f270 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f 62 74 is a string obt
4f280 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 ained from sqlit
4f290 65 33 5f 6d 70 72 69 6e 74 66 28 29 20 2a 2f 0a e3_mprintf() */.
4f2a0 23 64 65 66 69 6e 65 20 50 34 5f 52 45 41 4c 20 #define P4_REAL
4f2b0 20 20 20 20 28 2d 31 32 29 20 2f 2a 20 50 34 20 (-12) /* P4
4f2c0 69 73 20 61 20 36 34 2d 62 69 74 20 66 6c 6f 61 is a 64-bit floa
4f2d0 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 ting point value
4f2e0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 49 */.#define P4_I
4f2f0 4e 54 36 34 20 20 20 20 28 2d 31 33 29 20 2f 2a NT64 (-13) /*
4f300 20 50 34 20 69 73 20 61 20 36 34 2d 62 69 74 20 P4 is a 64-bit
4f310 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a signed integer *
4f320 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 49 4e 54 /.#define P4_INT
4f330 33 32 20 20 20 20 28 2d 31 34 29 20 2f 2a 20 50 32 (-14) /* P
4f340 34 20 69 73 20 61 20 33 32 2d 62 69 74 20 73 69 4 is a 32-bit si
4f350 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a gned integer */.
4f360 23 64 65 66 69 6e 65 20 50 34 5f 49 4e 54 41 52 #define P4_INTAR
4f370 52 41 59 20 28 2d 31 35 29 20 2f 2a 20 50 34 20 RAY (-15) /* P4
4f380 69 73 20 61 20 76 65 63 74 6f 72 20 6f 66 20 33 is a vector of 3
4f390 32 2d 62 69 74 20 69 6e 74 65 67 65 72 73 20 2a 2-bit integers *
4f3a0 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 53 55 42 /.#define P4_SUB
4f3b0 50 52 4f 47 52 41 4d 20 20 28 2d 31 38 29 20 2f PROGRAM (-18) /
4f3c0 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 * P4 is a pointe
4f3d0 72 20 74 6f 20 61 20 53 75 62 50 72 6f 67 72 61 r to a SubProgra
4f3e0 6d 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 0a m structure */..
4f3f0 2f 2a 20 57 68 65 6e 20 61 64 64 69 6e 67 20 61 /* When adding a
4f400 20 50 34 20 61 72 67 75 6d 65 6e 74 20 75 73 69 P4 argument usi
4f410 6e 67 20 50 34 5f 4b 45 59 49 4e 46 4f 2c 20 61 ng P4_KEYINFO, a
4f420 20 63 6f 70 79 20 6f 66 20 74 68 65 20 4b 65 79 copy of the Key
4f430 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 0a 2a Info structure.*
4f440 2a 20 69 73 20 6d 61 64 65 2e 20 20 54 68 61 74 * is made. That
4f450 20 63 6f 70 79 20 69 73 20 66 72 65 65 64 20 77 copy is freed w
4f460 68 65 6e 20 74 68 65 20 56 64 62 65 20 69 73 20 hen the Vdbe is
4f470 66 69 6e 61 6c 69 7a 65 64 2e 20 20 42 75 74 20 finalized. But
4f480 69 66 20 74 68 65 0a 2a 2a 20 61 72 67 75 6d 65 if the.** argume
4f490 6e 74 20 69 73 20 50 34 5f 4b 45 59 49 4e 46 4f nt is P4_KEYINFO
4f4a0 5f 48 41 4e 44 4f 46 46 2c 20 74 68 65 20 70 61 _HANDOFF, the pa
4f4b0 73 73 65 64 20 69 6e 20 70 6f 69 6e 74 65 72 20 ssed in pointer
4f4c0 69 73 20 75 73 65 64 2e 20 20 49 74 20 73 74 69 is used. It sti
4f4d0 6c 6c 0a 2a 2a 20 67 65 74 73 20 66 72 65 65 64 ll.** gets freed
4f4e0 20 77 68 65 6e 20 74 68 65 20 56 64 62 65 20 69 when the Vdbe i
4f4f0 73 20 66 69 6e 61 6c 69 7a 65 64 20 73 6f 20 69 s finalized so i
4f500 74 20 73 74 69 6c 6c 20 73 68 6f 75 6c 64 20 62 t still should b
4f510 65 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 e obtained.** fr
4f520 6f 6d 20 61 20 73 69 6e 67 6c 65 20 73 71 6c 69 om a single sqli
4f530 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20 42 75 74 teMalloc(). But
4f540 20 6e 6f 20 63 6f 70 79 20 69 73 20 6d 61 64 65 no copy is made
4f550 20 61 6e 64 20 74 68 65 20 63 61 6c 6c 69 6e 67 and the calling
4f560 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f .** function sho
4f570 75 6c 64 20 2a 6e 6f 74 2a 20 74 72 79 20 74 6f uld *not* try to
4f580 20 66 72 65 65 20 74 68 65 20 4b 65 79 49 6e 66 free the KeyInf
4f590 6f 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 o..*/.#define P4
4f5a0 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 _KEYINFO_HANDOFF
4f5b0 20 28 2d 31 36 29 0a 23 64 65 66 69 6e 65 20 50 (-16).#define P
4f5c0 34 5f 4b 45 59 49 4e 46 4f 5f 53 54 41 54 49 43 4_KEYINFO_STATIC
4f5d0 20 20 28 2d 31 37 29 0a 0a 2f 2a 0a 2a 2a 20 54 (-17)../*.** T
4f5e0 68 65 20 56 64 62 65 2e 61 43 6f 6c 4e 61 6d 65 he Vdbe.aColName
4f5f0 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e 73 20 array contains
4f600 35 6e 20 4d 65 6d 20 73 74 72 75 63 74 75 72 65 5n Mem structure
4f610 73 2c 20 77 68 65 72 65 20 6e 20 69 73 20 74 68 s, where n is th
4f620 65 20 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 e .** number of
4f630 63 6f 6c 75 6d 6e 73 20 6f 66 20 64 61 74 61 20 columns of data
4f640 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 returned by the
4f650 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 23 64 statement..*/.#d
4f660 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 41 efine COLNAME_NA
4f670 4d 45 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 ME 0.#define
4f680 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c 54 59 50 COLNAME_DECLTYP
4f690 45 20 31 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e E 1.#define COLN
4f6a0 41 4d 45 5f 44 41 54 41 42 41 53 45 20 32 0a 23 AME_DATABASE 2.#
4f6b0 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 54 define COLNAME_T
4f6c0 41 42 4c 45 20 20 20 20 33 0a 23 64 65 66 69 6e ABLE 3.#defin
4f6d0 65 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e e COLNAME_COLUMN
4f6e0 20 20 20 34 0a 23 69 66 64 65 66 20 53 51 4c 49 4.#ifdef SQLI
4f6f0 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e TE_ENABLE_COLUMN
4f700 5f 4d 45 54 41 44 41 54 41 0a 23 20 64 65 66 69 _METADATA.# defi
4f710 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20 20 20 20 ne COLNAME_N
4f720 20 20 20 20 35 20 20 20 20 20 20 2f 2a 20 4e 75 5 /* Nu
4f730 6d 62 65 72 20 6f 66 20 43 4f 4c 4e 41 4d 45 5f mber of COLNAME_
4f740 78 78 78 20 73 79 6d 62 6f 6c 73 20 2a 2f 0a 23 xxx symbols */.#
4f750 65 6c 73 65 0a 23 20 69 66 64 65 66 20 53 51 4c else.# ifdef SQL
4f760 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 ITE_OMIT_DECLTYP
4f770 45 0a 23 20 20 20 64 65 66 69 6e 65 20 43 4f 4c E.# define COL
4f780 4e 41 4d 45 5f 4e 20 20 20 20 20 20 31 20 20 20 NAME_N 1
4f790 20 20 20 2f 2a 20 53 74 6f 72 65 20 6f 6e 6c 79 /* Store only
4f7a0 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0a 23 20 65 the name */.# e
4f7b0 6c 73 65 0a 23 20 20 20 64 65 66 69 6e 65 20 43 lse.# define C
4f7c0 4f 4c 4e 41 4d 45 5f 4e 20 20 20 20 20 20 32 20 OLNAME_N 2
4f7d0 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74 68 /* Store th
4f7e0 65 20 6e 61 6d 65 20 61 6e 64 20 64 65 63 6c 74 e name and declt
4f7f0 79 70 65 20 2a 2f 0a 23 20 65 6e 64 69 66 0a 23 ype */.# endif.#
4f800 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
4f810 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f following macro
4f820 20 63 6f 6e 76 65 72 74 73 20 61 20 72 65 6c 61 converts a rela
4f830 74 69 76 65 20 61 64 64 72 65 73 73 20 69 6e 20 tive address in
4f840 74 68 65 20 70 32 20 66 69 65 6c 64 0a 2a 2a 20 the p2 field.**
4f850 6f 66 20 61 20 56 64 62 65 4f 70 20 73 74 72 75 of a VdbeOp stru
4f860 63 74 75 72 65 20 69 6e 74 6f 20 61 20 6e 65 67 cture into a neg
4f870 61 74 69 76 65 20 6e 75 6d 62 65 72 20 73 6f 20 ative number so
4f880 74 68 61 74 20 0a 2a 2a 20 73 71 6c 69 74 65 33 that .** sqlite3
4f890 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 29 20 VdbeAddOpList()
4f8a0 6b 6e 6f 77 73 20 74 68 61 74 20 74 68 65 20 61 knows that the a
4f8b0 64 64 72 65 73 73 20 69 73 20 72 65 6c 61 74 69 ddress is relati
4f8c0 76 65 2e 20 20 43 61 6c 6c 69 6e 67 0a 2a 2a 20 ve. Calling.**
4f8d0 74 68 65 20 6d 61 63 72 6f 20 61 67 61 69 6e 20 the macro again
4f8e0 72 65 73 74 6f 72 65 73 20 74 68 65 20 61 64 64 restores the add
4f8f0 72 65 73 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 ress..*/.#define
4f900 20 41 44 44 52 28 58 29 20 20 28 2d 31 2d 28 58 ADDR(X) (-1-(X
4f910 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 ))../*.** The ma
4f920 6b 65 66 69 6c 65 20 73 63 61 6e 73 20 74 68 65 kefile scans the
4f930 20 76 64 62 65 2e 63 20 73 6f 75 72 63 65 20 66 vdbe.c source f
4f940 69 6c 65 20 61 6e 64 20 63 72 65 61 74 65 73 20 ile and creates
4f950 74 68 65 20 22 6f 70 63 6f 64 65 73 2e 68 22 0a the "opcodes.h".
4f960 2a 2a 20 68 65 61 64 65 72 20 66 69 6c 65 20 74 ** header file t
4f970 68 61 74 20 64 65 66 69 6e 65 73 20 61 20 6e 75 hat defines a nu
4f980 6d 62 65 72 20 66 6f 72 20 65 61 63 68 20 6f 70 mber for each op
4f990 63 6f 64 65 20 75 73 65 64 20 62 79 20 74 68 65 code used by the
4f9a0 20 56 44 42 45 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a VDBE..*/./*****
4f9b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 ********* Includ
4f9c0 65 20 6f 70 63 6f 64 65 73 2e 68 20 69 6e 20 74 e opcodes.h in t
4f9d0 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 76 64 62 he middle of vdb
4f9e0 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e.h ************
4f9f0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
4fa00 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
4fa10 66 69 6c 65 20 6f 70 63 6f 64 65 73 2e 68 20 2a file opcodes.h *
4fa20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4fa30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4fa40 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 41 75 74 ********/./* Aut
4fa50 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e 65 72 omatically gener
4fa60 61 74 65 64 2e 20 20 44 6f 20 6e 6f 74 20 65 64 ated. Do not ed
4fa70 69 74 20 2a 2f 0a 2f 2a 20 53 65 65 20 74 68 65 it */./* See the
4fa80 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 6b 20 73 mkopcodeh.awk s
4fa90 63 72 69 70 74 20 66 6f 72 20 64 65 74 61 69 6c cript for detail
4faa0 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f s */.#define OP_
4fab0 47 6f 74 6f 20 20 20 20 20 20 20 20 20 20 20 20 Goto
4fac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fad0 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 4f 1.#define O
4fae0 50 5f 47 6f 73 75 62 20 20 20 20 20 20 20 20 20 P_Gosub
4faf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fb00 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65 2.#define
4fb10 20 4f 50 5f 52 65 74 75 72 6e 20 20 20 20 20 20 OP_Return
4fb20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fb30 20 20 20 20 20 20 20 20 20 33 0a 23 64 65 66 69 3.#defi
4fb40 6e 65 20 4f 50 5f 59 69 65 6c 64 20 20 20 20 20 ne OP_Yield
4fb50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fb60 20 20 20 20 20 20 20 20 20 20 20 34 0a 23 64 65 4.#de
4fb70 66 69 6e 65 20 4f 50 5f 48 61 6c 74 49 66 4e 75 fine OP_HaltIfNu
4fb80 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ll
4fb90 20 20 20 20 20 20 20 20 20 20 20 20 20 35 0a 23 5.#
4fba0 64 65 66 69 6e 65 20 4f 50 5f 48 61 6c 74 20 20 define OP_Halt
4fbb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fbc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 6
4fbd0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 74 65 .#define OP_Inte
4fbe0 67 65 72 20 20 20 20 20 20 20 20 20 20 20 20 20 ger
4fbf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fc00 20 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 7.#define OP_In
4fc10 74 36 34 20 20 20 20 20 20 20 20 20 20 20 20 20 t64
4fc20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fc30 20 20 20 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 8.#define OP_
4fc40 52 65 61 6c 20 20 20 20 20 20 20 20 20 20 20 20 Real
4fc50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fc60 20 20 20 31 33 30 20 20 20 2f 2a 20 73 61 6d 65 130 /* same
4fc70 20 61 73 20 54 4b 5f 46 4c 4f 41 54 20 20 20 20 as TK_FLOAT
4fc80 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 74 */.#define OP_St
4fc90 72 69 6e 67 38 20 20 20 20 20 20 20 20 20 20 20 ring8
4fca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fcb0 20 20 39 34 20 20 20 2f 2a 20 73 61 6d 65 20 61 94 /* same a
4fcc0 73 20 54 4b 5f 53 54 52 49 4e 47 20 20 20 2a 2f s TK_STRING */
4fcd0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 74 72 69 .#define OP_Stri
4fce0 6e 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ng
4fcf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fd00 20 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 75 9.#define OP_Nu
4fd10 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ll
4fd20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fd30 20 20 31 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 10.#define OP_
4fd40 42 6c 6f 62 20 20 20 20 20 20 20 20 20 20 20 20 Blob
4fd50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fd60 20 20 20 20 31 31 0a 23 64 65 66 69 6e 65 20 4f 11.#define O
4fd70 50 5f 56 61 72 69 61 62 6c 65 20 20 20 20 20 20 P_Variable
4fd80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fd90 20 20 20 20 20 20 31 32 0a 23 64 65 66 69 6e 65 12.#define
4fda0 20 4f 50 5f 4d 6f 76 65 20 20 20 20 20 20 20 20 OP_Move
4fdb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fdc0 20 20 20 20 20 20 20 20 31 33 0a 23 64 65 66 69 13.#defi
4fdd0 6e 65 20 4f 50 5f 43 6f 70 79 20 20 20 20 20 20 ne OP_Copy
4fde0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fdf0 20 20 20 20 20 20 20 20 20 20 31 34 0a 23 64 65 14.#de
4fe00 66 69 6e 65 20 4f 50 5f 53 43 6f 70 79 20 20 20 fine OP_SCopy
4fe10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fe20 20 20 20 20 20 20 20 20 20 20 20 20 31 35 0a 23 15.#
4fe30 64 65 66 69 6e 65 20 4f 50 5f 52 65 73 75 6c 74 define OP_Result
4fe40 52 6f 77 20 20 20 20 20 20 20 20 20 20 20 20 20 Row
4fe50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 36 16
4fe60 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6e 63 .#define OP_Conc
4fe70 61 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 at
4fe80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fe90 39 31 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 91 /* same as
4fea0 54 4b 5f 43 4f 4e 43 41 54 20 20 20 2a 2f 0a 23 TK_CONCAT */.#
4feb0 64 65 66 69 6e 65 20 4f 50 5f 41 64 64 20 20 20 define OP_Add
4fec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 36 86
4fee0 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
4fef0 5f 50 4c 55 53 20 20 20 20 20 2a 2f 0a 23 64 65 _PLUS */.#de
4ff00 66 69 6e 65 20 4f 50 5f 53 75 62 74 72 61 63 74 fine OP_Subtract
4ff10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ff20 20 20 20 20 20 20 20 20 20 20 20 20 38 37 20 20 87
4ff30 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4d /* same as TK_M
4ff40 49 4e 55 53 20 20 20 20 2a 2f 0a 23 64 65 66 69 INUS */.#defi
4ff50 6e 65 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 20 20 ne OP_Multiply
4ff60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ff70 20 20 20 20 20 20 20 20 20 20 38 38 20 20 20 2f 88 /
4ff80 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 54 41 * same as TK_STA
4ff90 52 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 R */.#define
4ffa0 20 4f 50 5f 44 69 76 69 64 65 20 20 20 20 20 20 OP_Divide
4ffb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ffc0 20 20 20 20 20 20 20 20 38 39 20 20 20 2f 2a 20 89 /*
4ffd0 73 61 6d 65 20 61 73 20 54 4b 5f 53 4c 41 53 48 same as TK_SLASH
4ffe0 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f */.#define O
4fff0 50 5f 52 65 6d 61 69 6e 64 65 72 20 20 20 20 20 P_Remainder
50000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50010 20 20 20 20 20 20 39 30 20 20 20 2f 2a 20 73 61 90 /* sa
50020 6d 65 20 61 73 20 54 4b 5f 52 45 4d 20 20 20 20 me as TK_REM
50030 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f */.#define OP_
50040 43 6f 6c 6c 53 65 71 20 20 20 20 20 20 20 20 20 CollSeq
50050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50060 20 20 20 20 31 37 0a 23 64 65 66 69 6e 65 20 4f 17.#define O
50070 50 5f 46 75 6e 63 74 69 6f 6e 20 20 20 20 20 20 P_Function
50080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50090 20 20 20 20 20 20 31 38 0a 23 64 65 66 69 6e 65 18.#define
500a0 20 4f 50 5f 42 69 74 41 6e 64 20 20 20 20 20 20 OP_BitAnd
500b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
500c0 20 20 20 20 20 20 20 20 38 32 20 20 20 2f 2a 20 82 /*
500d0 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 41 4e same as TK_BITAN
500e0 44 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f D */.#define O
500f0 50 5f 42 69 74 4f 72 20 20 20 20 20 20 20 20 20 P_BitOr
50100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50110 20 20 20 20 20 20 38 33 20 20 20 2f 2a 20 73 61 83 /* sa
50120 6d 65 20 61 73 20 54 4b 5f 42 49 54 4f 52 20 20 me as TK_BITOR
50130 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f */.#define OP_
50140 53 68 69 66 74 4c 65 66 74 20 20 20 20 20 20 20 ShiftLeft
50150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50160 20 20 20 20 38 34 20 20 20 2f 2a 20 73 61 6d 65 84 /* same
50170 20 61 73 20 54 4b 5f 4c 53 48 49 46 54 20 20 20 as TK_LSHIFT
50180 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 68 */.#define OP_Sh
50190 69 66 74 52 69 67 68 74 20 20 20 20 20 20 20 20 iftRight
501a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
501b0 20 20 38 35 20 20 20 2f 2a 20 73 61 6d 65 20 61 85 /* same a
501c0 73 20 54 4b 5f 52 53 48 49 46 54 20 20 20 2a 2f s TK_RSHIFT */
501d0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 64 64 49 .#define OP_AddI
501e0 6d 6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 mm
501f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50200 32 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 75 20.#define OP_Mu
50210 73 74 42 65 49 6e 74 20 20 20 20 20 20 20 20 20 stBeInt
50220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50230 20 20 32 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 21.#define OP_
50240 52 65 61 6c 41 66 66 69 6e 69 74 79 20 20 20 20 RealAffinity
50250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50260 20 20 20 20 32 32 0a 23 64 65 66 69 6e 65 20 4f 22.#define O
50270 50 5f 54 6f 54 65 78 74 20 20 20 20 20 20 20 20 P_ToText
50280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50290 20 20 20 20 20 31 34 31 20 20 20 2f 2a 20 73 61 141 /* sa
502a0 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 54 45 58 54 me as TK_TO_TEXT
502b0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f */.#define OP_
502c0 54 6f 42 6c 6f 62 20 20 20 20 20 20 20 20 20 20 ToBlob
502d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
502e0 20 20 20 31 34 32 20 20 20 2f 2a 20 73 61 6d 65 142 /* same
502f0 20 61 73 20 54 4b 5f 54 4f 5f 42 4c 4f 42 20 20 as TK_TO_BLOB
50300 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f */.#define OP_To
50310 4e 75 6d 65 72 69 63 20 20 20 20 20 20 20 20 20 Numeric
50320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50330 20 31 34 33 20 20 20 2f 2a 20 73 61 6d 65 20 61 143 /* same a
50340 73 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43 2a s TK_TO_NUMERIC*
50350 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f 49 /.#define OP_ToI
50360 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 nt
50370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50380 31 34 34 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 144 /* same as
50390 20 54 4b 5f 54 4f 5f 49 4e 54 20 20 20 2a 2f 0a TK_TO_INT */.
503a0 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f 52 65 61 #define OP_ToRea
503b0 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 l
503c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 14
503d0 35 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 5 /* same as T
503e0 4b 5f 54 4f 5f 52 45 41 4c 20 20 2a 2f 0a 23 64 K_TO_REAL */.#d
503f0 65 66 69 6e 65 20 4f 50 5f 45 71 20 20 20 20 20 efine OP_Eq
50400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50410 20 20 20 20 20 20 20 20 20 20 20 20 20 37 36 20 76
50420 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
50430 45 51 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 EQ */.#def
50440 69 6e 65 20 4f 50 5f 4e 65 20 20 20 20 20 20 20 ine OP_Ne
50450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50460 20 20 20 20 20 20 20 20 20 20 20 37 35 20 20 20 75
50470 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 45 /* same as TK_NE
50480 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e */.#defin
50490 65 20 4f 50 5f 4c 74 20 20 20 20 20 20 20 20 20 e OP_Lt
504a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
504b0 20 20 20 20 20 20 20 20 20 37 39 20 20 20 2f 2a 79 /*
504c0 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 54 20 20 same as TK_LT
504d0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
504e0 4f 50 5f 4c 65 20 20 20 20 20 20 20 20 20 20 20 OP_Le
504f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50500 20 20 20 20 20 20 20 37 38 20 20 20 2f 2a 20 73 78 /* s
50510 61 6d 65 20 61 73 20 54 4b 5f 4c 45 20 20 20 20 ame as TK_LE
50520 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 */.#define OP
50530 5f 47 74 20 20 20 20 20 20 20 20 20 20 20 20 20 _Gt
50540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50550 20 20 20 20 20 37 37 20 20 20 2f 2a 20 73 61 6d 77 /* sam
50560 65 20 61 73 20 54 4b 5f 47 54 20 20 20 20 20 20 e as TK_GT
50570 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 47 */.#define OP_G
50580 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e
50590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
505a0 20 20 20 38 30 20 20 20 2f 2a 20 73 61 6d 65 20 80 /* same
505b0 61 73 20 54 4b 5f 47 45 20 20 20 20 20 20 20 2a as TK_GE *
505c0 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 65 72 /.#define OP_Per
505d0 6d 75 74 61 74 69 6f 6e 20 20 20 20 20 20 20 20 mutation
505e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
505f0 20 32 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 23.#define OP_C
50600 6f 6d 70 61 72 65 20 20 20 20 20 20 20 20 20 20 ompare
50610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50620 20 20 20 32 34 0a 23 64 65 66 69 6e 65 20 4f 50 24.#define OP
50630 5f 4a 75 6d 70 20 20 20 20 20 20 20 20 20 20 20 _Jump
50640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50650 20 20 20 20 20 32 35 0a 23 64 65 66 69 6e 65 20 25.#define
50660 4f 50 5f 41 6e 64 20 20 20 20 20 20 20 20 20 20 OP_And
50670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50680 20 20 20 20 20 20 20 36 39 20 20 20 2f 2a 20 73 69 /* s
50690 61 6d 65 20 61 73 20 54 4b 5f 41 4e 44 20 20 20 ame as TK_AND
506a0 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 */.#define OP
506b0 5f 4f 72 20 20 20 20 20 20 20 20 20 20 20 20 20 _Or
506c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
506d0 20 20 20 20 20 36 38 20 20 20 2f 2a 20 73 61 6d 68 /* sam
506e0 65 20 61 73 20 54 4b 5f 4f 52 20 20 20 20 20 20 e as TK_OR
506f0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e */.#define OP_N
50700 6f 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ot
50710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50720 20 20 20 31 39 20 20 20 2f 2a 20 73 61 6d 65 20 19 /* same
50730 61 73 20 54 4b 5f 4e 4f 54 20 20 20 20 20 20 2a as TK_NOT *
50740 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 42 69 74 /.#define OP_Bit
50750 4e 6f 74 20 20 20 20 20 20 20 20 20 20 20 20 20 Not
50760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50770 20 39 33 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 93 /* same as
50780 20 54 4b 5f 42 49 54 4e 4f 54 20 20 20 2a 2f 0a TK_BITNOT */.
50790 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 20 20 20 #define OP_If
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 20 20 20 20 20 20 20 20 32 2
507c0 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 4e 6.#define OP_IfN
507d0 6f 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ot
507e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
507f0 20 32 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 27.#define OP_I
50800 73 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20 sNull
50810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50820 20 20 20 37 33 20 20 20 2f 2a 20 73 61 6d 65 20 73 /* same
50830 61 73 20 54 4b 5f 49 53 4e 55 4c 4c 20 20 20 2a as TK_ISNULL *
50840 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 /.#define OP_Not
50850 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 Null
50860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50870 20 37 34 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 74 /* same as
50880 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 20 2a 2f 0a TK_NOTNULL */.
50890 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6c 75 6d #define OP_Colum
508a0 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n
508b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2
508c0 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 66 66 8.#define OP_Aff
508d0 69 6e 69 74 79 20 20 20 20 20 20 20 20 20 20 20 inity
508e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
508f0 20 32 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 29.#define OP_M
50900 61 6b 65 52 65 63 6f 72 64 20 20 20 20 20 20 20 akeRecord
50910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50920 20 20 20 33 30 0a 23 64 65 66 69 6e 65 20 4f 50 30.#define OP
50930 5f 43 6f 75 6e 74 20 20 20 20 20 20 20 20 20 20 _Count
50940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50950 20 20 20 20 20 33 31 0a 23 64 65 66 69 6e 65 20 31.#define
50960 4f 50 5f 53 61 76 65 70 6f 69 6e 74 20 20 20 20 OP_Savepoint
50970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50980 20 20 20 20 20 20 20 33 32 0a 23 64 65 66 69 6e 32.#defin
50990 65 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 20 e OP_AutoCommit
509a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
509b0 20 20 20 20 20 20 20 20 20 33 33 0a 23 64 65 66 33.#def
509c0 69 6e 65 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 ine OP_Transacti
509d0 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 on
509e0 20 20 20 20 20 20 20 20 20 20 20 33 34 0a 23 64 34.#d
509f0 65 66 69 6e 65 20 4f 50 5f 52 65 61 64 43 6f 6f efine OP_ReadCoo
50a00 6b 69 65 20 20 20 20 20 20 20 20 20 20 20 20 20 kie
50a10 20 20 20 20 20 20 20 20 20 20 20 20 20 33 35 0a 35.
50a20 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 74 43 6f #define OP_SetCo
50a30 6f 6b 69 65 20 20 20 20 20 20 20 20 20 20 20 20 okie
50a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 3
50a50 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 65 72 6.#define OP_Ver
50a60 69 66 79 43 6f 6f 6b 69 65 20 20 20 20 20 20 20 ifyCookie
50a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50a80 20 33 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 37.#define OP_O
50a90 70 65 6e 52 65 61 64 20 20 20 20 20 20 20 20 20 penRead
50aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50ab0 20 20 20 33 38 0a 23 64 65 66 69 6e 65 20 4f 50 38.#define OP
50ac0 5f 4f 70 65 6e 57 72 69 74 65 20 20 20 20 20 20 _OpenWrite
50ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50ae0 20 20 20 20 20 33 39 0a 23 64 65 66 69 6e 65 20 39.#define
50af0 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c OP_OpenEphemeral
50b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50b10 20 20 20 20 20 20 20 34 30 0a 23 64 65 66 69 6e 40.#defin
50b20 65 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64 6f 20 e OP_OpenPseudo
50b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50b40 20 20 20 20 20 20 20 20 20 34 31 0a 23 64 65 66 41.#def
50b50 69 6e 65 20 4f 50 5f 43 6c 6f 73 65 20 20 20 20 ine OP_Close
50b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50b70 20 20 20 20 20 20 20 20 20 20 20 34 32 0a 23 64 42.#d
50b80 65 66 69 6e 65 20 4f 50 5f 53 65 65 6b 4c 74 20 efine OP_SeekLt
50b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 34 33 0a 43.
50bb0 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 65 6b 4c #define OP_SeekL
50bc0 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e
50bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 4
50be0 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 65 4.#define OP_See
50bf0 6b 47 65 20 20 20 20 20 20 20 20 20 20 20 20 20 kGe
50c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50c10 20 34 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 45.#define OP_S
50c20 65 65 6b 47 74 20 20 20 20 20 20 20 20 20 20 20 eekGt
50c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50c40 20 20 20 34 36 0a 23 64 65 66 69 6e 65 20 4f 50 46.#define OP
50c50 5f 53 65 65 6b 20 20 20 20 20 20 20 20 20 20 20 _Seek
50c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50c70 20 20 20 20 20 34 37 0a 23 64 65 66 69 6e 65 20 47.#define
50c80 4f 50 5f 4e 6f 74 46 6f 75 6e 64 20 20 20 20 20 OP_NotFound
50c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50ca0 20 20 20 20 20 20 20 34 38 0a 23 64 65 66 69 6e 48.#defin
50cb0 65 20 4f 50 5f 46 6f 75 6e 64 20 20 20 20 20 20 e OP_Found
50cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50cd0 20 20 20 20 20 20 20 20 20 34 39 0a 23 64 65 66 49.#def
50ce0 69 6e 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 20 ine OP_IsUnique
50cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50d00 20 20 20 20 20 20 20 20 20 20 20 35 30 0a 23 64 50.#d
50d10 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 45 78 69 73 efine OP_NotExis
50d20 74 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ts
50d30 20 20 20 20 20 20 20 20 20 20 20 20 20 35 31 0a 51.
50d40 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 71 75 65 #define OP_Seque
50d50 6e 63 65 20 20 20 20 20 20 20 20 20 20 20 20 20 nce
50d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 5
50d70 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 65 77 2.#define OP_New
50d80 52 6f 77 69 64 20 20 20 20 20 20 20 20 20 20 20 Rowid
50d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50da0 20 35 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 53.#define OP_I
50db0 6e 73 65 72 74 20 20 20 20 20 20 20 20 20 20 20 nsert
50dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50dd0 20 20 20 35 34 0a 23 64 65 66 69 6e 65 20 4f 50 54.#define OP
50de0 5f 49 6e 73 65 72 74 49 6e 74 20 20 20 20 20 20 _InsertInt
50df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50e00 20 20 20 20 20 35 35 0a 23 64 65 66 69 6e 65 20 55.#define
50e10 4f 50 5f 44 65 6c 65 74 65 20 20 20 20 20 20 20 OP_Delete
50e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50e30 20 20 20 20 20 20 20 35 36 0a 23 64 65 66 69 6e 56.#defin
50e40 65 20 4f 50 5f 52 65 73 65 74 43 6f 75 6e 74 20 e OP_ResetCount
50e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50e60 20 20 20 20 20 20 20 20 20 35 37 0a 23 64 65 66 57.#def
50e70 69 6e 65 20 4f 50 5f 52 6f 77 4b 65 79 20 20 20 ine OP_RowKey
50e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50e90 20 20 20 20 20 20 20 20 20 20 20 35 38 0a 23 64 58.#d
50ea0 65 66 69 6e 65 20 4f 50 5f 52 6f 77 44 61 74 61 efine OP_RowData
50eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 35 39 0a 59.
50ed0 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77 69 64 #define OP_Rowid
50ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 6
50f00 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 75 6c 0.#define OP_Nul
50f10 6c 52 6f 77 20 20 20 20 20 20 20 20 20 20 20 20 lRow
50f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50f30 20 36 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4c 61.#define OP_L
50f40 61 73 74 20 20 20 20 20 20 20 20 20 20 20 20 20 ast
50f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50f60 20 20 20 36 32 0a 23 64 65 66 69 6e 65 20 4f 50 62.#define OP
50f70 5f 53 6f 72 74 20 20 20 20 20 20 20 20 20 20 20 _Sort
50f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50f90 20 20 20 20 20 36 33 0a 23 64 65 66 69 6e 65 20 63.#define
50fa0 4f 50 5f 52 65 77 69 6e 64 20 20 20 20 20 20 20 OP_Rewind
50fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50fc0 20 20 20 20 20 20 20 36 34 0a 23 64 65 66 69 6e 64.#defin
50fd0 65 20 4f 50 5f 50 72 65 76 20 20 20 20 20 20 20 e OP_Prev
50fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50ff0 20 20 20 20 20 20 20 20 20 36 35 0a 23 64 65 66 65.#def
51000 69 6e 65 20 4f 50 5f 4e 65 78 74 20 20 20 20 20 ine OP_Next
51010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51020 20 20 20 20 20 20 20 20 20 20 20 36 36 0a 23 64 66.#d
51030 65 66 69 6e 65 20 4f 50 5f 49 64 78 49 6e 73 65 efine OP_IdxInse
51040 72 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 rt
51050 20 20 20 20 20 20 20 20 20 20 20 20 20 36 37 0a 67.
51060 23 64 65 66 69 6e 65 20 4f 50 5f 49 64 78 44 65 #define OP_IdxDe
51070 6c 65 74 65 20 20 20 20 20 20 20 20 20 20 20 20 lete
51080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 7
51090 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 64 78 0.#define OP_Idx
510a0 52 6f 77 69 64 20 20 20 20 20 20 20 20 20 20 20 Rowid
510b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
510c0 20 37 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 71.#define OP_I
510d0 64 78 4c 54 20 20 20 20 20 20 20 20 20 20 20 20 dxLT
510e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
510f0 20 20 20 37 32 0a 23 64 65 66 69 6e 65 20 4f 50 72.#define OP
51100 5f 49 64 78 47 45 20 20 20 20 20 20 20 20 20 20 _IdxGE
51110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51120 20 20 20 20 20 38 31 0a 23 64 65 66 69 6e 65 20 81.#define
51130 4f 50 5f 44 65 73 74 72 6f 79 20 20 20 20 20 20 OP_Destroy
51140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51150 20 20 20 20 20 20 20 39 32 0a 23 64 65 66 69 6e 92.#defin
51160 65 20 4f 50 5f 43 6c 65 61 72 20 20 20 20 20 20 e OP_Clear
51170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51180 20 20 20 20 20 20 20 20 20 39 35 0a 23 64 65 66 95.#def
51190 69 6e 65 20 4f 50 5f 43 72 65 61 74 65 49 6e 64 ine OP_CreateInd
511a0 65 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ex
511b0 20 20 20 20 20 20 20 20 20 20 20 39 36 0a 23 64 96.#d
511c0 65 66 69 6e 65 20 4f 50 5f 43 72 65 61 74 65 54 efine OP_CreateT
511d0 61 62 6c 65 20 20 20 20 20 20 20 20 20 20 20 20 able
511e0 20 20 20 20 20 20 20 20 20 20 20 20 20 39 37 0a 97.
511f0 23 64 65 66 69 6e 65 20 4f 50 5f 50 61 72 73 65 #define OP_Parse
51200 53 63 68 65 6d 61 20 20 20 20 20 20 20 20 20 20 Schema
51210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 9
51220 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4c 6f 61 8.#define OP_Loa
51230 64 41 6e 61 6c 79 73 69 73 20 20 20 20 20 20 20 dAnalysis
51240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51250 20 39 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 99.#define OP_D
51260 72 6f 70 54 61 62 6c 65 20 20 20 20 20 20 20 20 ropTable
51270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51280 20 20 31 30 30 0a 23 64 65 66 69 6e 65 20 4f 50 100.#define OP
51290 5f 44 72 6f 70 49 6e 64 65 78 20 20 20 20 20 20 _DropIndex
512a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
512b0 20 20 20 20 31 30 31 0a 23 64 65 66 69 6e 65 20 101.#define
512c0 4f 50 5f 44 72 6f 70 54 72 69 67 67 65 72 20 20 OP_DropTrigger
512d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
512e0 20 20 20 20 20 20 31 30 32 0a 23 64 65 66 69 6e 102.#defin
512f0 65 20 4f 50 5f 49 6e 74 65 67 72 69 74 79 43 6b e OP_IntegrityCk
51300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51310 20 20 20 20 20 20 20 20 31 30 33 0a 23 64 65 66 103.#def
51320 69 6e 65 20 4f 50 5f 52 6f 77 53 65 74 41 64 64 ine OP_RowSetAdd
51330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51340 20 20 20 20 20 20 20 20 20 20 31 30 34 0a 23 64 104.#d
51350 65 66 69 6e 65 20 4f 50 5f 52 6f 77 53 65 74 52 efine OP_RowSetR
51360 65 61 64 20 20 20 20 20 20 20 20 20 20 20 20 20 ead
51370 20 20 20 20 20 20 20 20 20 20 20 20 31 30 35 0a 105.
51380 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77 53 65 #define OP_RowSe
51390 74 54 65 73 74 20 20 20 20 20 20 20 20 20 20 20 tTest
513a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 10
513b0 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 72 6f 6.#define OP_Pro
513c0 67 72 61 6d 20 20 20 20 20 20 20 20 20 20 20 20 gram
513d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
513e0 31 30 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 107.#define OP_P
513f0 61 72 61 6d 20 20 20 20 20 20 20 20 20 20 20 20 aram
51400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51410 20 20 31 30 38 0a 23 64 65 66 69 6e 65 20 4f 50 108.#define OP
51420 5f 46 6b 43 6f 75 6e 74 65 72 20 20 20 20 20 20 _FkCounter
51430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51440 20 20 20 20 31 30 39 0a 23 64 65 66 69 6e 65 20 109.#define
51450 4f 50 5f 46 6b 49 66 5a 65 72 6f 20 20 20 20 20 OP_FkIfZero
51460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51470 20 20 20 20 20 20 31 31 30 0a 23 64 65 66 69 6e 110.#defin
51480 65 20 4f 50 5f 4d 65 6d 4d 61 78 20 20 20 20 20 e OP_MemMax
51490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
514a0 20 20 20 20 20 20 20 20 31 31 31 0a 23 64 65 66 111.#def
514b0 69 6e 65 20 4f 50 5f 49 66 50 6f 73 20 20 20 20 ine OP_IfPos
514c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
514d0 20 20 20 20 20 20 20 20 20 20 31 31 32 0a 23 64 112.#d
514e0 65 66 69 6e 65 20 4f 50 5f 49 66 4e 65 67 20 20 efine OP_IfNeg
514f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51500 20 20 20 20 20 20 20 20 20 20 20 20 31 31 33 0a 113.
51510 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 5a 65 72 #define OP_IfZer
51520 6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 o
51530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 11
51540 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 67 67 4.#define OP_Agg
51550 53 74 65 70 20 20 20 20 20 20 20 20 20 20 20 20 Step
51560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51570 31 31 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 115.#define OP_A
51580 67 67 46 69 6e 61 6c 20 20 20 20 20 20 20 20 20 ggFinal
51590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
515a0 20 20 31 31 36 0a 23 64 65 66 69 6e 65 20 4f 50 116.#define OP
515b0 5f 56 61 63 75 75 6d 20 20 20 20 20 20 20 20 20 _Vacuum
515c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
515d0 20 20 20 20 31 31 37 0a 23 64 65 66 69 6e 65 20 117.#define
515e0 4f 50 5f 49 6e 63 72 56 61 63 75 75 6d 20 20 20 OP_IncrVacuum
515f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51600 20 20 20 20 20 20 31 31 38 0a 23 64 65 66 69 6e 118.#defin
51610 65 20 4f 50 5f 45 78 70 69 72 65 20 20 20 20 20 e OP_Expire
51620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51630 20 20 20 20 20 20 20 20 31 31 39 0a 23 64 65 66 119.#def
51640 69 6e 65 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b ine OP_TableLock
51650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51660 20 20 20 20 20 20 20 20 20 20 31 32 30 0a 23 64 120.#d
51670 65 66 69 6e 65 20 4f 50 5f 56 42 65 67 69 6e 20 efine OP_VBegin
51680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51690 20 20 20 20 20 20 20 20 20 20 20 20 31 32 31 0a 121.
516a0 23 64 65 66 69 6e 65 20 4f 50 5f 56 43 72 65 61 #define OP_VCrea
516b0 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 te
516c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 12
516d0 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 44 65 2.#define OP_VDe
516e0 73 74 72 6f 79 20 20 20 20 20 20 20 20 20 20 20 stroy
516f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51700 31 32 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 123.#define OP_V
51710 4f 70 65 6e 20 20 20 20 20 20 20 20 20 20 20 20 Open
51720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51730 20 20 31 32 34 0a 23 64 65 66 69 6e 65 20 4f 50 124.#define OP
51740 5f 56 46 69 6c 74 65 72 20 20 20 20 20 20 20 20 _VFilter
51750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51760 20 20 20 20 31 32 35 0a 23 64 65 66 69 6e 65 20 125.#define
51770 4f 50 5f 56 43 6f 6c 75 6d 6e 20 20 20 20 20 20 OP_VColumn
51780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51790 20 20 20 20 20 20 31 32 36 0a 23 64 65 66 69 6e 126.#defin
517a0 65 20 4f 50 5f 56 4e 65 78 74 20 20 20 20 20 20 e OP_VNext
517b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
517c0 20 20 20 20 20 20 20 20 31 32 37 0a 23 64 65 66 127.#def
517d0 69 6e 65 20 4f 50 5f 56 52 65 6e 61 6d 65 20 20 ine OP_VRename
517e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
517f0 20 20 20 20 20 20 20 20 20 20 31 32 38 0a 23 64 128.#d
51800 65 66 69 6e 65 20 4f 50 5f 56 55 70 64 61 74 65 efine OP_VUpdate
51810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51820 20 20 20 20 20 20 20 20 20 20 20 20 31 32 39 0a 129.
51830 23 64 65 66 69 6e 65 20 4f 50 5f 50 61 67 65 63 #define OP_Pagec
51840 6f 75 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 ount
51850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 13
51860 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 72 61 1.#define OP_Tra
51870 63 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ce
51880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51890 31 33 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 132.#define OP_N
518a0 6f 6f 70 20 20 20 20 20 20 20 20 20 20 20 20 20 oop
518b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
518c0 20 20 31 33 33 0a 23 64 65 66 69 6e 65 20 4f 50 133.#define OP
518d0 5f 45 78 70 6c 61 69 6e 20 20 20 20 20 20 20 20 _Explain
518e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
518f0 20 20 20 20 31 33 34 0a 0a 2f 2a 20 54 68 65 20 134../* The
51900 66 6f 6c 6c 6f 77 69 6e 67 20 6f 70 63 6f 64 65 following opcode
51910 20 76 61 6c 75 65 73 20 61 72 65 20 6e 65 76 65 values are neve
51920 72 20 75 73 65 64 20 2a 2f 0a 23 64 65 66 69 6e r used */.#defin
51930 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 35 e OP_NotUsed_135
51940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51950 20 20 20 20 20 20 20 20 31 33 35 0a 23 64 65 66 135.#def
51960 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 ine OP_NotUsed_1
51970 33 36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36
51980 20 20 20 20 20 20 20 20 20 20 31 33 36 0a 23 64 136.#d
51990 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 efine OP_NotUsed
519a0 5f 31 33 37 20 20 20 20 20 20 20 20 20 20 20 20 _137
519b0 20 20 20 20 20 20 20 20 20 20 20 20 31 33 37 0a 137.
519c0 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 #define OP_NotUs
519d0 65 64 5f 31 33 38 20 20 20 20 20 20 20 20 20 20 ed_138
519e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 13
519f0 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 8.#define OP_Not
51a00 55 73 65 64 5f 31 33 39 20 20 20 20 20 20 20 20 Used_139
51a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51a20 31 33 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 139.#define OP_N
51a30 6f 74 55 73 65 64 5f 31 34 30 20 20 20 20 20 20 otUsed_140
51a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51a50 20 20 31 34 30 0a 0a 0a 2f 2a 20 50 72 6f 70 65 140.../* Prope
51a60 72 74 69 65 73 20 73 75 63 68 20 61 73 20 22 6f rties such as "o
51a70 75 74 32 22 20 6f 72 20 22 6a 75 6d 70 22 20 74 ut2" or "jump" t
51a80 68 61 74 20 61 72 65 20 73 70 65 63 69 66 69 65 hat are specifie
51a90 64 20 69 6e 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 73 d in.** comments
51aa0 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 22 following the "
51ab0 63 61 73 65 22 20 66 6f 72 20 65 61 63 68 20 6f case" for each o
51ac0 70 63 6f 64 65 20 69 6e 20 74 68 65 20 76 64 62 pcode in the vdb
51ad0 65 2e 63 0a 2a 2a 20 61 72 65 20 65 6e 63 6f 64 e.c.** are encod
51ae0 65 64 20 69 6e 74 6f 20 62 69 74 76 65 63 74 6f ed into bitvecto
51af0 72 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a rs as follows:.*
51b00 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f /.#define OPFLG_
51b10 4a 55 4d 50 20 20 20 20 20 20 20 20 20 20 20 20 JUMP
51b20 30 78 30 30 30 31 20 20 2f 2a 20 6a 75 6d 70 3a 0x0001 /* jump:
51b30 20 20 50 32 20 68 6f 6c 64 73 20 6a 6d 70 20 74 P2 holds jmp t
51b40 61 72 67 65 74 20 2a 2f 0a 23 64 65 66 69 6e 65 arget */.#define
51b50 20 4f 50 46 4c 47 5f 4f 55 54 32 5f 50 52 45 52 OPFLG_OUT2_PRER
51b60 45 4c 45 41 53 45 20 30 78 30 30 30 32 20 20 2f ELEASE 0x0002 /
51b70 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 * out2-prereleas
51b80 65 3a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 e: */.#define OP
51b90 46 4c 47 5f 49 4e 31 20 20 20 20 20 20 20 20 20 FLG_IN1
51ba0 20 20 20 20 30 78 30 30 30 34 20 20 2f 2a 20 69 0x0004 /* i
51bb0 6e 31 3a 20 20 20 50 31 20 69 73 20 61 6e 20 69 n1: P1 is an i
51bc0 6e 70 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 nput */.#define
51bd0 4f 50 46 4c 47 5f 49 4e 32 20 20 20 20 20 20 20 OPFLG_IN2
51be0 20 20 20 20 20 20 30 78 30 30 30 38 20 20 2f 2a 0x0008 /*
51bf0 20 69 6e 32 3a 20 20 20 50 32 20 69 73 20 61 6e in2: P2 is an
51c00 20 69 6e 70 75 74 20 2a 2f 0a 23 64 65 66 69 6e input */.#defin
51c10 65 20 4f 50 46 4c 47 5f 49 4e 33 20 20 20 20 20 e OPFLG_IN3
51c20 20 20 20 20 20 20 20 20 30 78 30 30 31 30 20 20 0x0010
51c30 2f 2a 20 69 6e 33 3a 20 20 20 50 33 20 69 73 20 /* in3: P3 is
51c40 61 6e 20 69 6e 70 75 74 20 2a 2f 0a 23 64 65 66 an input */.#def
51c50 69 6e 65 20 4f 50 46 4c 47 5f 4f 55 54 33 20 20 ine OPFLG_OUT3
51c60 20 20 20 20 20 20 20 20 20 20 30 78 30 30 32 30 0x0020
51c70 20 20 2f 2a 20 6f 75 74 33 3a 20 20 50 33 20 69 /* out3: P3 i
51c80 73 20 61 6e 20 6f 75 74 70 75 74 20 2a 2f 0a 23 s an output */.#
51c90 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 49 define OPFLG_INI
51ca0 54 49 41 4c 49 5a 45 52 20 7b 5c 0a 2f 2a 20 20 TIALIZER {\./*
51cb0 20 30 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 31 0 */ 0x00, 0x01
51cc0 2c 20 30 78 30 31 2c 20 30 78 30 34 2c 20 30 78 , 0x01, 0x04, 0x
51cd0 30 34 2c 20 30 78 31 30 2c 20 30 78 30 30 2c 20 04, 0x10, 0x00,
51ce0 30 78 30 32 2c 5c 0a 2f 2a 20 20 20 38 20 2a 2f 0x02,\./* 8 */
51cf0 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 0x02, 0x02, 0x0
51d00 32 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 2, 0x02, 0x00, 0
51d10 78 30 30 2c 20 30 78 30 34 2c 20 30 78 30 34 2c x00, 0x04, 0x04,
51d20 5c 0a 2f 2a 20 20 31 36 20 2a 2f 20 30 78 30 30 \./* 16 */ 0x00
51d30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
51d40 30 34 2c 20 30 78 30 34 2c 20 30 78 30 35 2c 20 04, 0x04, 0x05,
51d50 30 78 30 34 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 0x04, 0x00,\./*
51d60 20 32 34 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 24 */ 0x00, 0x0
51d70 31 2c 20 30 78 30 35 2c 20 30 78 30 35 2c 20 30 1, 0x05, 0x05, 0
51d80 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
51d90 20 30 78 30 32 2c 5c 0a 2f 2a 20 20 33 32 20 2a 0x02,\./* 32 *
51da0 2f 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 / 0x00, 0x00, 0x
51db0 30 30 2c 20 30 78 30 32 2c 20 30 78 31 30 2c 20 00, 0x02, 0x10,
51dc0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
51dd0 2c 5c 0a 2f 2a 20 20 34 30 20 2a 2f 20 30 78 30 ,\./* 40 */ 0x0
51de0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
51df0 78 31 31 2c 20 30 78 31 31 2c 20 30 78 31 31 2c x11, 0x11, 0x11,
51e00 20 30 78 31 31 2c 20 30 78 30 38 2c 5c 0a 2f 2a 0x11, 0x08,\./*
51e10 20 20 34 38 20 2a 2f 20 30 78 31 31 2c 20 30 78 48 */ 0x11, 0x
51e20 31 31 2c 20 30 78 31 31 2c 20 30 78 31 31 2c 20 11, 0x11, 0x11,
51e30 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 30 0x02, 0x02, 0x00
51e40 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 35 36 20 , 0x00,\./* 56
51e50 2a 2f 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 */ 0x00, 0x00, 0
51e60 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 32 2c x00, 0x00, 0x02,
51e70 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 0x00, 0x01, 0x0
51e80 31 2c 5c 0a 2f 2a 20 20 36 34 20 2a 2f 20 30 78 1,\./* 64 */ 0x
51e90 30 31 2c 20 30 78 30 31 2c 20 30 78 30 31 2c 20 01, 0x01, 0x01,
51ea0 30 78 30 38 2c 20 30 78 32 63 2c 20 30 78 32 63 0x08, 0x2c, 0x2c
51eb0 2c 20 30 78 30 30 2c 20 30 78 30 32 2c 5c 0a 2f , 0x00, 0x02,\./
51ec0 2a 20 20 37 32 20 2a 2f 20 30 78 31 31 2c 20 30 * 72 */ 0x11, 0
51ed0 78 30 35 2c 20 30 78 30 35 2c 20 30 78 31 35 2c x05, 0x05, 0x15,
51ee0 20 30 78 31 35 2c 20 30 78 31 35 2c 20 30 78 31 0x15, 0x15, 0x1
51ef0 35 2c 20 30 78 31 35 2c 5c 0a 2f 2a 20 20 38 30 5, 0x15,\./* 80
51f00 20 2a 2f 20 30 78 31 35 2c 20 30 78 31 31 2c 20 */ 0x15, 0x11,
51f10 30 78 32 63 2c 20 30 78 32 63 2c 20 30 78 32 63 0x2c, 0x2c, 0x2c
51f20 2c 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30 78 , 0x2c, 0x2c, 0x
51f30 32 63 2c 5c 0a 2f 2a 20 20 38 38 20 2a 2f 20 30 2c,\./* 88 */ 0
51f40 78 32 63 2c 20 30 78 32 63 2c 20 30 78 32 63 2c x2c, 0x2c, 0x2c,
51f50 20 30 78 32 63 2c 20 30 78 30 32 2c 20 30 78 30 0x2c, 0x02, 0x0
51f60 34 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 5c 0a 4, 0x02, 0x00,\.
51f70 2f 2a 20 20 39 36 20 2a 2f 20 30 78 30 32 2c 20 /* 96 */ 0x02,
51f80 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30 30 0x02, 0x00, 0x00
51f90 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
51fa0 30 30 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 31 30 00, 0x00,\./* 10
51fb0 34 20 2a 2f 20 30 78 30 38 2c 20 30 78 32 31 2c 4 */ 0x08, 0x21,
51fc0 20 30 78 31 35 2c 20 30 78 30 31 2c 20 30 78 30 0x15, 0x01, 0x0
51fd0 32 2c 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 2, 0x00, 0x01, 0
51fe0 78 30 38 2c 5c 0a 2f 2a 20 31 31 32 20 2a 2f 20 x08,\./* 112 */
51ff0 30 78 30 35 2c 20 30 78 30 35 2c 20 30 78 30 35 0x05, 0x05, 0x05
52000 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
52010 30 30 2c 20 30 78 30 31 2c 20 30 78 30 30 2c 5c 00, 0x01, 0x00,\
52020 0a 2f 2a 20 31 32 30 20 2a 2f 20 30 78 30 30 2c ./* 120 */ 0x00,
52030 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
52040 30 2c 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 0, 0x00, 0x01, 0
52050 78 30 30 2c 20 30 78 30 31 2c 5c 0a 2f 2a 20 31 x00, 0x01,\./* 1
52060 32 38 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 30 28 */ 0x00, 0x00
52070 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 , 0x02, 0x02, 0x
52080 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
52090 30 78 30 30 2c 5c 0a 2f 2a 20 31 33 36 20 2a 2f 0x00,\./* 136 */
520a0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
520b0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
520c0 78 30 34 2c 20 30 78 30 34 2c 20 30 78 30 34 2c x04, 0x04, 0x04,
520d0 5c 0a 2f 2a 20 31 34 34 20 2a 2f 20 30 78 30 34 \./* 144 */ 0x04
520e0 2c 20 30 78 30 34 2c 7d 0a 0a 2f 2a 2a 2a 2a 2a , 0x04,}../*****
520f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
52100 20 6f 70 63 6f 64 65 73 2e 68 20 2a 2a 2a 2a 2a opcodes.h *****
52110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52130 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
52140 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e ********* Contin
52150 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 uing where we le
52160 66 74 20 6f 66 66 20 69 6e 20 76 64 62 65 2e 68 ft off in vdbe.h
52170 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
52180 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a ********/../*.**
52190 20 50 72 6f 74 6f 74 79 70 65 73 20 66 6f 72 20 Prototypes for
521a0 74 68 65 20 56 44 42 45 20 69 6e 74 65 72 66 61 the VDBE interfa
521b0 63 65 2e 20 20 53 65 65 20 63 6f 6d 6d 65 6e 74 ce. See comment
521c0 73 20 6f 6e 20 74 68 65 20 69 6d 70 6c 65 6d 65 s on the impleme
521d0 6e 74 61 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 61 ntation.** for a
521e0 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 description of
521f0 77 68 61 74 20 65 61 63 68 20 6f 66 20 74 68 65 what each of the
52200 73 65 20 72 6f 75 74 69 6e 65 73 20 64 6f 65 73 se routines does
52210 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
52220 41 54 45 20 56 64 62 65 20 2a 73 71 6c 69 74 65 ATE Vdbe *sqlite
52230 33 56 64 62 65 43 72 65 61 74 65 28 73 71 6c 69 3VdbeCreate(sqli
52240 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 te3*);.SQLITE_PR
52250 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
52260 33 56 64 62 65 41 64 64 4f 70 30 28 56 64 62 65 3VdbeAddOp0(Vdbe
52270 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 *,int);.SQLITE_P
52280 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
52290 65 33 56 64 62 65 41 64 64 4f 70 31 28 56 64 62 e3VdbeAddOp1(Vdb
522a0 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c e*,int,int);.SQL
522b0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
522c0 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
522d0 32 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 2(Vdbe*,int,int,
522e0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
522f0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
52300 56 64 62 65 41 64 64 4f 70 33 28 56 64 62 65 2a VdbeAddOp3(Vdbe*
52310 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 ,int,int,int,int
52320 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
52330 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
52340 65 41 64 64 4f 70 34 28 56 64 62 65 2a 2c 69 6e eAddOp4(Vdbe*,in
52350 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 63 6f t,int,int,int,co
52360 6e 73 74 20 63 68 61 72 20 2a 7a 50 34 2c 69 6e nst char *zP4,in
52370 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
52380 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
52390 62 65 41 64 64 4f 70 4c 69 73 74 28 56 64 62 65 beAddOpList(Vdbe
523a0 2a 2c 20 69 6e 74 20 6e 4f 70 2c 20 56 64 62 65 *, int nOp, Vdbe
523b0 4f 70 4c 69 73 74 20 63 6f 6e 73 74 20 2a 61 4f OpList const *aO
523c0 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 p);.SQLITE_PRIVA
523d0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
523e0 64 62 65 43 68 61 6e 67 65 50 31 28 56 64 62 65 dbeChangeP1(Vdbe
523f0 2a 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 *, int addr, int
52400 20 50 31 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 P1);.SQLITE_PRI
52410 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
52420 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 56 64 3VdbeChangeP2(Vd
52430 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 be*, int addr, i
52440 6e 74 20 50 32 29 3b 0a 53 51 4c 49 54 45 5f 50 nt P2);.SQLITE_P
52450 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
52460 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 te3VdbeChangeP3(
52470 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c Vdbe*, int addr,
52480 20 69 6e 74 20 50 33 29 3b 0a 53 51 4c 49 54 45 int P3);.SQLITE
52490 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
524a0 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 lite3VdbeChangeP
524b0 35 28 56 64 62 65 2a 2c 20 75 38 20 50 35 29 3b 5(Vdbe*, u8 P5);
524c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
524d0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
524e0 4a 75 6d 70 48 65 72 65 28 56 64 62 65 2a 2c 20 JumpHere(Vdbe*,
524f0 69 6e 74 20 61 64 64 72 29 3b 0a 53 51 4c 49 54 int addr);.SQLIT
52500 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
52510 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
52520 54 6f 4e 6f 6f 70 28 56 64 62 65 2a 2c 20 69 6e ToNoop(Vdbe*, in
52530 74 20 61 64 64 72 2c 20 69 6e 74 20 4e 29 3b 0a t addr, int N);.
52540 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
52550 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 oid sqlite3VdbeC
52560 68 61 6e 67 65 50 34 28 56 64 62 65 2a 2c 20 69 hangeP4(Vdbe*, i
52570 6e 74 20 61 64 64 72 2c 20 63 6f 6e 73 74 20 63 nt addr, const c
52580 68 61 72 20 2a 7a 50 34 2c 20 69 6e 74 20 4e 29 har *zP4, int N)
52590 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
525a0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
525b0 65 55 73 65 73 42 74 72 65 65 28 56 64 62 65 2a eUsesBtree(Vdbe*
525c0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
525d0 52 49 56 41 54 45 20 56 64 62 65 4f 70 20 2a 73 RIVATE VdbeOp *s
525e0 71 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 qlite3VdbeGetOp(
525f0 56 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c Vdbe*, int);.SQL
52600 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
52610 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c sqlite3VdbeMakeL
52620 61 62 65 6c 28 56 64 62 65 2a 29 3b 0a 53 51 4c abel(Vdbe*);.SQL
52630 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
52640 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 sqlite3VdbeDele
52650 74 65 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 te(Vdbe*);.SQLIT
52660 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
52670 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 qlite3VdbeMakeRe
52680 61 64 79 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e ady(Vdbe*,int,in
52690 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e t,int,int,int,in
526a0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
526b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
526c0 62 65 46 69 6e 61 6c 69 7a 65 28 56 64 62 65 2a beFinalize(Vdbe*
526d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
526e0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
526f0 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 56 beResolveLabel(V
52700 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 dbe*, int);.SQLI
52710 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
52720 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e qlite3VdbeCurren
52730 74 41 64 64 72 28 56 64 62 65 2a 29 3b 0a 23 69 tAddr(Vdbe*);.#i
52740 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
52750 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 G.SQLITE_PRIVATE
52760 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 int sqlite3Vd
52770 62 65 41 73 73 65 72 74 4d 61 79 41 62 6f 72 74 beAssertMayAbort
52780 28 56 64 62 65 20 2a 2c 20 69 6e 74 29 3b 0a 53 (Vdbe *, int);.S
52790 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
527a0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
527b0 54 72 61 63 65 28 56 64 62 65 2a 2c 46 49 4c 45 Trace(Vdbe*,FILE
527c0 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 *);.#endif.SQLIT
527d0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
527e0 71 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 53 qlite3VdbeResetS
527f0 74 65 70 52 65 73 75 6c 74 28 56 64 62 65 2a 29 tepResult(Vdbe*)
52800 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
52810 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
52820 52 65 73 65 74 28 56 64 62 65 2a 29 3b 0a 53 51 Reset(Vdbe*);.SQ
52830 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
52840 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 d sqlite3VdbeSet
52850 4e 75 6d 43 6f 6c 73 28 56 64 62 65 2a 2c 69 6e NumCols(Vdbe*,in
52860 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
52870 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
52880 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 56 64 62 beSetColName(Vdb
52890 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 63 6f e*, int, int, co
528a0 6e 73 74 20 63 68 61 72 20 2a 2c 20 76 6f 69 64 nst char *, void
528b0 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c (*)(void*));.SQL
528c0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
528d0 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f 75 6e sqlite3VdbeCoun
528e0 74 43 68 61 6e 67 65 73 28 56 64 62 65 2a 29 3b tChanges(Vdbe*);
528f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
52900 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33 sqlite3 *sqlite3
52910 56 64 62 65 44 62 28 56 64 62 65 2a 29 3b 0a 53 VdbeDb(Vdbe*);.S
52920 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
52930 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 id sqlite3VdbeSe
52940 74 53 71 6c 28 56 64 62 65 2a 2c 20 63 6f 6e 73 tSql(Vdbe*, cons
52950 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e t char *z, int n
52960 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
52970 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
52980 74 65 33 56 64 62 65 53 77 61 70 28 56 64 62 65 te3VdbeSwap(Vdbe
52990 2a 2c 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 *,Vdbe*);.SQLITE
529a0 5f 50 52 49 56 41 54 45 20 56 64 62 65 4f 70 20 _PRIVATE VdbeOp
529b0 2a 73 71 6c 69 74 65 33 56 64 62 65 54 61 6b 65 *sqlite3VdbeTake
529c0 4f 70 41 72 72 61 79 28 56 64 62 65 2a 2c 20 69 OpArray(Vdbe*, i
529d0 6e 74 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 nt*, int*);.SQLI
529e0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
529f0 73 71 6c 69 74 65 33 56 64 62 65 50 72 6f 67 72 sqlite3VdbeProgr
52a00 61 6d 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 amDelete(sqlite3
52a10 20 2a 2c 20 53 75 62 50 72 6f 67 72 61 6d 20 2a *, SubProgram *
52a20 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
52a30 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 76 RIVATE sqlite3_v
52a40 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 56 64 62 alue *sqlite3Vdb
52a50 65 47 65 74 56 61 6c 75 65 28 56 64 62 65 2a 2c eGetValue(Vdbe*,
52a60 20 69 6e 74 2c 20 75 38 29 3b 0a 53 51 4c 49 54 int, u8);.SQLIT
52a70 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
52a80 71 6c 69 74 65 33 56 64 62 65 53 65 74 56 61 72 qlite3VdbeSetVar
52a90 6d 61 73 6b 28 56 64 62 65 2a 2c 20 69 6e 74 29 mask(Vdbe*, int)
52aa0 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ;..SQLITE_PRIVAT
52ab0 45 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 E UnpackedRecord
52ac0 20 2a 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 *sqlite3VdbeRec
52ad0 6f 72 64 55 6e 70 61 63 6b 28 4b 65 79 49 6e 66 ordUnpack(KeyInf
52ae0 6f 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 o*,int,const voi
52af0 64 2a 2c 63 68 61 72 2a 2c 69 6e 74 29 3b 0a 53 d*,char*,int);.S
52b00 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
52b10 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 id sqlite3VdbeDe
52b20 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f leteUnpackedReco
52b30 72 64 28 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 rd(UnpackedRecor
52b40 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 d*);.SQLITE_PRIV
52b50 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
52b60 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 dbeRecordCompare
52b70 28 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a (int,const void*
52b80 2c 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 2a ,UnpackedRecord*
52b90 29 3b 0a 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 );...#ifndef NDE
52ba0 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 BUG.SQLITE_PRIVA
52bb0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
52bc0 33 56 64 62 65 43 6f 6d 6d 65 6e 74 28 56 64 62 3VdbeComment(Vdb
52bd0 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c e*, const char*,
52be0 20 2e 2e 2e 29 3b 0a 23 20 64 65 66 69 6e 65 20 ...);.# define
52bf0 56 64 62 65 43 6f 6d 6d 65 6e 74 28 58 29 20 20 VdbeComment(X)
52c00 73 71 6c 69 74 65 33 56 64 62 65 43 6f 6d 6d 65 sqlite3VdbeComme
52c10 6e 74 20 58 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt X.SQLITE_PRIV
52c20 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
52c30 65 33 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e e3VdbeNoopCommen
52c40 74 28 56 64 62 65 2a 2c 20 63 6f 6e 73 74 20 63 t(Vdbe*, const c
52c50 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 23 20 64 65 har*, ...);.# de
52c60 66 69 6e 65 20 56 64 62 65 4e 6f 6f 70 43 6f 6d fine VdbeNoopCom
52c70 6d 65 6e 74 28 58 29 20 20 73 71 6c 69 74 65 33 ment(X) sqlite3
52c80 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 20 VdbeNoopComment
52c90 58 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 X.#else.# define
52ca0 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 58 29 0a VdbeComment(X).
52cb0 23 20 64 65 66 69 6e 65 20 56 64 62 65 4e 6f 6f # define VdbeNoo
52cc0 70 43 6f 6d 6d 65 6e 74 28 58 29 0a 23 65 6e 64 pComment(X).#end
52cd0 69 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a if..#endif../***
52ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
52cf0 6f 66 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a of vdbe.h ******
52d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
52d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 *********** Cont
52d40 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 inuing where we
52d50 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 left off in sqli
52d60 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a teInt.h ********
52d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
52d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c *********** Incl
52d90 75 64 65 20 70 61 67 65 72 2e 68 20 69 6e 20 74 ude pager.h in t
52da0 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c he middle of sql
52db0 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a iteInt.h *******
52dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
52dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
52de0 6e 20 66 69 6c 65 20 70 61 67 65 72 2e 68 20 2a n file pager.h *
52df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
52e20 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 * 2001 September
52e30 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 15.**.** The au
52e40 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
52e50 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
52e60 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
52e70 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
52e80 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
52e90 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
52ea0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
52eb0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
52ec0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
52ed0 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
52ee0 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
52ef0 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
52f00 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
52f10 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
52f20 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
52f30 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
52f40 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
52f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52f60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52f90 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 **.** This heade
52fa0 72 20 66 69 6c 65 20 64 65 66 69 6e 65 73 20 74 r file defines t
52fb0 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 he interface tha
52fc0 74 20 74 68 65 20 73 71 6c 69 74 65 20 70 61 67 t the sqlite pag
52fd0 65 20 63 61 63 68 65 0a 2a 2a 20 73 75 62 73 79 e cache.** subsy
52fe0 73 74 65 6d 2e 20 20 54 68 65 20 70 61 67 65 20 stem. The page
52ff0 63 61 63 68 65 20 73 75 62 73 79 73 74 65 6d 20 cache subsystem
53000 72 65 61 64 73 20 61 6e 64 20 77 72 69 74 65 73 reads and writes
53010 20 61 20 66 69 6c 65 20 61 20 70 61 67 65 0a 2a a file a page.*
53020 2a 20 61 74 20 61 20 74 69 6d 65 20 61 6e 64 20 * at a time and
53030 70 72 6f 76 69 64 65 73 20 61 20 6a 6f 75 72 6e provides a journ
53040 61 6c 20 66 6f 72 20 72 6f 6c 6c 62 61 63 6b 2e al for rollback.
53050 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a .**.** @(#) $Id:
53060 20 70 61 67 65 72 2e 68 2c 76 20 31 2e 31 30 34 pager.h,v 1.104
53070 20 32 30 30 39 2f 30 37 2f 32 34 20 31 39 3a 30 2009/07/24 19:0
53080 31 3a 31 39 20 64 72 68 20 45 78 70 20 24 0a 2a 1:19 drh Exp $.*
53090 2f 0a 0a 23 69 66 6e 64 65 66 20 5f 50 41 47 45 /..#ifndef _PAGE
530a0 52 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 50 41 R_H_.#define _PA
530b0 47 45 52 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 44 65 GER_H_../*.** De
530c0 66 61 75 6c 74 20 6d 61 78 69 6d 75 6d 20 73 69 fault maximum si
530d0 7a 65 20 66 6f 72 20 70 65 72 73 69 73 74 65 6e ze for persisten
530e0 74 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e t journal files.
530f0 20 41 20 6e 65 67 61 74 69 76 65 20 0a 2a 2a 20 A negative .**
53100 76 61 6c 75 65 20 6d 65 61 6e 73 20 6e 6f 20 6c value means no l
53110 69 6d 69 74 2e 20 54 68 69 73 20 76 61 6c 75 65 imit. This value
53120 20 6d 61 79 20 62 65 20 6f 76 65 72 72 69 64 64 may be overridd
53130 65 6e 20 75 73 69 6e 67 20 74 68 65 20 0a 2a 2a en using the .**
53140 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 sqlite3PagerJou
53150 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28 29 20 rnalSizeLimit()
53160 41 50 49 2e 20 53 65 65 20 61 6c 73 6f 20 22 50 API. See also "P
53170 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 73 69 RAGMA journal_si
53180 7a 65 5f 6c 69 6d 69 74 22 2e 0a 2a 2f 0a 23 69 ze_limit"..*/.#i
53190 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 fndef SQLITE_DEF
531a0 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a AULT_JOURNAL_SIZ
531b0 45 5f 4c 49 4d 49 54 0a 20 20 23 64 65 66 69 6e E_LIMIT. #defin
531c0 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 e SQLITE_DEFAULT
531d0 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a 45 5f 4c 49 _JOURNAL_SIZE_LI
531e0 4d 49 54 20 2d 31 0a 23 65 6e 64 69 66 0a 0a 2f MIT -1.#endif../
531f0 2a 0a 2a 2a 20 54 68 65 20 74 79 70 65 20 75 73 *.** The type us
53200 65 64 20 74 6f 20 72 65 70 72 65 73 65 6e 74 20 ed to represent
53210 61 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 20 a page number.
53220 54 68 65 20 66 69 72 73 74 20 70 61 67 65 20 69 The first page i
53230 6e 20 61 20 66 69 6c 65 0a 2a 2a 20 69 73 20 63 n a file.** is c
53240 61 6c 6c 65 64 20 70 61 67 65 20 31 2e 20 20 30 alled page 1. 0
53250 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 70 72 is used to repr
53260 65 73 65 6e 74 20 22 6e 6f 74 20 61 20 70 61 67 esent "not a pag
53270 65 22 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 75 e"..*/.typedef u
53280 33 32 20 50 67 6e 6f 3b 0a 0a 2f 2a 0a 2a 2a 20 32 Pgno;../*.**
53290 45 61 63 68 20 6f 70 65 6e 20 66 69 6c 65 20 69 Each open file i
532a0 73 20 6d 61 6e 61 67 65 64 20 62 79 20 61 20 73 s managed by a s
532b0 65 70 61 72 61 74 65 20 69 6e 73 74 61 6e 63 65 eparate instance
532c0 20 6f 66 20 74 68 65 20 22 50 61 67 65 72 22 20 of the "Pager"
532d0 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79 structure..*/.ty
532e0 70 65 64 65 66 20 73 74 72 75 63 74 20 50 61 67 pedef struct Pag
532f0 65 72 20 50 61 67 65 72 3b 0a 0a 2f 2a 0a 2a 2a er Pager;../*.**
53300 20 48 61 6e 64 6c 65 20 74 79 70 65 20 66 6f 72 Handle type for
53310 20 70 61 67 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 pages..*/.typed
53320 65 66 20 73 74 72 75 63 74 20 50 67 48 64 72 20 ef struct PgHdr
53330 44 62 50 61 67 65 3b 0a 0a 2f 2a 0a 2a 2a 20 50 DbPage;../*.** P
53340 61 67 65 20 6e 75 6d 62 65 72 20 50 41 47 45 52 age number PAGER
53350 5f 4d 4a 5f 50 47 4e 4f 20 69 73 20 6e 65 76 65 _MJ_PGNO is neve
53360 72 20 75 73 65 64 20 69 6e 20 61 6e 20 53 51 4c r used in an SQL
53370 69 74 65 20 64 61 74 61 62 61 73 65 20 28 69 74 ite database (it
53380 20 69 73 0a 2a 2a 20 72 65 73 65 72 76 65 64 20 is.** reserved
53390 66 6f 72 20 77 6f 72 6b 69 6e 67 20 61 72 6f 75 for working arou
533a0 6e 64 20 61 20 77 69 6e 64 6f 77 73 2f 70 6f 73 nd a windows/pos
533b0 69 78 20 69 6e 63 6f 6d 70 61 74 69 62 69 6c 69 ix incompatibili
533c0 74 79 29 2e 20 49 74 20 69 73 0a 2a 2a 20 75 73 ty). It is.** us
533d0 65 64 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 ed in the journa
533e0 6c 20 74 6f 20 73 69 67 6e 69 66 79 20 74 68 61 l to signify tha
533f0 74 20 74 68 65 20 72 65 6d 61 69 6e 64 65 72 20 t the remainder
53400 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 of the journal f
53410 69 6c 65 20 0a 2a 2a 20 69 73 20 64 65 76 6f 74 ile .** is devot
53420 65 64 20 74 6f 20 73 74 6f 72 69 6e 67 20 61 20 ed to storing a
53430 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e master journal n
53440 61 6d 65 20 2d 20 74 68 65 72 65 20 61 72 65 20 ame - there are
53450 6e 6f 20 6d 6f 72 65 20 70 61 67 65 73 20 74 6f no more pages to
53460 0a 2a 2a 20 72 6f 6c 6c 20 62 61 63 6b 2e 20 53 .** roll back. S
53470 65 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 ee comments for
53480 66 75 6e 63 74 69 6f 6e 20 77 72 69 74 65 4d 61 function writeMa
53490 73 74 65 72 4a 6f 75 72 6e 61 6c 28 29 20 69 6e sterJournal() in
534a0 20 70 61 67 65 72 2e 63 20 0a 2a 2a 20 66 6f 72 pager.c .** for
534b0 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 64 65 details..*/.#de
534c0 66 69 6e 65 20 50 41 47 45 52 5f 4d 4a 5f 50 47 fine PAGER_MJ_PG
534d0 4e 4f 28 78 29 20 28 28 50 67 6e 6f 29 28 28 50 NO(x) ((Pgno)((P
534e0 45 4e 44 49 4e 47 5f 42 59 54 45 2f 28 28 78 29 ENDING_BYTE/((x)
534f0 2d 3e 70 61 67 65 53 69 7a 65 29 29 2b 31 29 29 ->pageSize))+1))
53500 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 ../*.** Allowed
53510 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 66 values for the f
53520 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 74 lags parameter t
53530 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 o sqlite3PagerOp
53540 65 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 en()..**.** NOTE
53550 3a 20 54 68 65 73 65 20 76 61 6c 75 65 73 20 6d : These values m
53560 75 73 74 20 6d 61 74 63 68 20 74 68 65 20 63 6f ust match the co
53570 72 72 65 73 70 6f 6e 64 69 6e 67 20 42 54 52 45 rresponding BTRE
53580 45 5f 20 76 61 6c 75 65 73 20 69 6e 20 62 74 72 E_ values in btr
53590 65 65 2e 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 ee.h..*/.#define
535a0 20 50 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 PAGER_OMIT_JOUR
535b0 4e 41 4c 20 20 30 78 30 30 30 31 20 20 20 20 2f NAL 0x0001 /
535c0 2a 20 44 6f 20 6e 6f 74 20 75 73 65 20 61 20 72 * Do not use a r
535d0 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 ollback journal
535e0 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 */.#define PAGER
535f0 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20 20 20 30 _NO_READLOCK 0
53600 78 30 30 30 32 20 20 20 20 2f 2a 20 4f 6d 69 74 x0002 /* Omit
53610 20 72 65 61 64 6c 6f 63 6b 73 20 6f 6e 20 72 65 readlocks on re
53620 61 64 6f 6e 6c 79 20 66 69 6c 65 73 20 2a 2f 0a adonly files */.
53630 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20 76 61 6c ./*.** Valid val
53640 75 65 73 20 66 6f 72 20 74 68 65 20 73 65 63 6f ues for the seco
53650 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 nd argument to s
53660 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 qlite3PagerLocki
53670 6e 67 4d 6f 64 65 28 29 2e 0a 2a 2f 0a 23 64 65 ngMode()..*/.#de
53680 66 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b 49 fine PAGER_LOCKI
53690 4e 47 4d 4f 44 45 5f 51 55 45 52 59 20 20 20 20 NGMODE_QUERY
536a0 20 20 2d 31 0a 23 64 65 66 69 6e 65 20 50 41 47 -1.#define PAG
536b0 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e ER_LOCKINGMODE_N
536c0 4f 52 4d 41 4c 20 20 20 20 20 20 30 0a 23 64 65 ORMAL 0.#de
536d0 66 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b 49 fine PAGER_LOCKI
536e0 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 NGMODE_EXCLUSIVE
536f0 20 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 1../*.** Vali
53700 64 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 d values for the
53710 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
53720 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 to sqlite3Pager
53730 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 29 2e 0a 2a JournalMode()..*
53740 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f /.#define PAGER_
53750 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 JOURNALMODE_QUER
53760 59 20 20 20 20 20 20 2d 31 0a 23 64 65 66 69 6e Y -1.#defin
53770 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d e PAGER_JOURNALM
53780 4f 44 45 5f 44 45 4c 45 54 45 20 20 20 20 20 20 ODE_DELETE
53790 30 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 79 0 /* Commit by
537a0 20 64 65 6c 65 74 69 6e 67 20 6a 6f 75 72 6e 61 deleting journa
537b0 6c 20 66 69 6c 65 20 2a 2f 0a 23 64 65 66 69 6e l file */.#defin
537c0 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d e PAGER_JOURNALM
537d0 4f 44 45 5f 50 45 52 53 49 53 54 20 20 20 20 20 ODE_PERSIST
537e0 31 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 79 1 /* Commit by
537f0 20 7a 65 72 6f 69 6e 67 20 6a 6f 75 72 6e 61 6c zeroing journal
53800 20 68 65 61 64 65 72 20 2a 2f 0a 23 64 65 66 69 header */.#defi
53810 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c ne PAGER_JOURNAL
53820 4d 4f 44 45 5f 4f 46 46 20 20 20 20 20 20 20 20 MODE_OFF
53830 20 32 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c 20 2 /* Journal
53840 6f 6d 69 74 74 65 64 2e 20 20 2a 2f 0a 23 64 65 omitted. */.#de
53850 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e fine PAGER_JOURN
53860 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 20 ALMODE_TRUNCATE
53870 20 20 20 33 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 3 /* Commit
53880 20 62 79 20 74 72 75 6e 63 61 74 69 6e 67 20 6a by truncating j
53890 6f 75 72 6e 61 6c 20 2a 2f 0a 23 64 65 66 69 6e ournal */.#defin
538a0 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d e PAGER_JOURNALM
538b0 4f 44 45 5f 4d 45 4d 4f 52 59 20 20 20 20 20 20 ODE_MEMORY
538c0 34 20 20 20 2f 2a 20 49 6e 2d 6d 65 6d 6f 72 79 4 /* In-memory
538d0 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f journal file */
538e0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 6d 61 ../*.** The rema
538f0 69 6e 64 65 72 20 6f 66 20 74 68 69 73 20 66 69 inder of this fi
53900 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 le contains the
53910 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20 declarations of
53920 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a the functions.**
53930 20 74 68 61 74 20 6d 61 6b 65 20 75 70 20 74 68 that make up th
53940 65 20 50 61 67 65 72 20 73 75 62 2d 73 79 73 74 e Pager sub-syst
53950 65 6d 20 41 50 49 2e 20 53 65 65 20 73 6f 75 72 em API. See sour
53960 63 65 20 63 6f 64 65 20 63 6f 6d 6d 65 6e 74 73 ce code comments
53970 20 66 6f 72 20 0a 2a 2a 20 61 20 64 65 74 61 69 for .** a detai
53980 6c 65 64 20 64 65 73 63 72 69 70 74 69 6f 6e 20 led description
53990 6f 66 20 65 61 63 68 20 72 6f 75 74 69 6e 65 2e of each routine.
539a0 0a 2a 2f 0a 0a 2f 2a 20 4f 70 65 6e 20 61 6e 64 .*/../* Open and
539b0 20 63 6c 6f 73 65 20 61 20 50 61 67 65 72 20 63 close a Pager c
539c0 6f 6e 6e 65 63 74 69 6f 6e 2e 20 2a 2f 20 0a 53 onnection. */ .S
539d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
539e0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 t sqlite3PagerOp
539f0 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 en(. sqlite3_vf
53a00 73 2a 2c 0a 20 20 50 61 67 65 72 20 2a 2a 70 70 s*,. Pager **pp
53a10 50 61 67 65 72 2c 0a 20 20 63 6f 6e 73 74 20 63 Pager,. const c
53a20 68 61 72 2a 2c 0a 20 20 69 6e 74 2c 0a 20 20 69 har*,. int,. i
53a30 6e 74 2c 0a 20 20 69 6e 74 2c 0a 20 20 76 6f 69 nt,. int,. voi
53a40 64 28 2a 29 28 44 62 50 61 67 65 2a 29 0a 29 3b d(*)(DbPage*).);
53a50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
53a60 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
53a70 43 6c 6f 73 65 28 50 61 67 65 72 20 2a 70 50 61 Close(Pager *pPa
53a80 67 65 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ger);.SQLITE_PRI
53a90 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
53aa0 50 61 67 65 72 52 65 61 64 46 69 6c 65 68 65 61 PagerReadFilehea
53ab0 64 65 72 28 50 61 67 65 72 2a 2c 20 69 6e 74 2c der(Pager*, int,
53ac0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 unsigned char*)
53ad0 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20 ;../* Functions
53ae0 75 73 65 64 20 74 6f 20 63 6f 6e 66 69 67 75 72 used to configur
53af0 65 20 61 20 50 61 67 65 72 20 6f 62 6a 65 63 74 e a Pager object
53b00 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 . */.SQLITE_PRIV
53b10 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
53b20 50 61 67 65 72 53 65 74 42 75 73 79 68 61 6e 64 PagerSetBusyhand
53b30 6c 65 72 28 50 61 67 65 72 2a 2c 20 69 6e 74 28 ler(Pager*, int(
53b40 2a 29 28 76 6f 69 64 20 2a 29 2c 20 76 6f 69 64 *)(void *), void
53b50 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
53b60 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
53b70 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 agerSetPagesize(
53b80 50 61 67 65 72 2a 2c 20 75 31 36 2a 2c 20 69 6e Pager*, u16*, in
53b90 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
53ba0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
53bb0 67 65 72 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 gerMaxPageCount(
53bc0 50 61 67 65 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 Pager*, int);.SQ
53bd0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
53be0 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 d sqlite3PagerSe
53bf0 74 43 61 63 68 65 73 69 7a 65 28 50 61 67 65 72 tCachesize(Pager
53c00 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
53c10 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
53c20 69 74 65 33 50 61 67 65 72 53 65 74 53 61 66 65 ite3PagerSetSafe
53c30 74 79 4c 65 76 65 6c 28 50 61 67 65 72 2a 2c 69 tyLevel(Pager*,i
53c40 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f nt,int);.SQLITE_
53c50 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
53c60 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d te3PagerLockingM
53c70 6f 64 65 28 50 61 67 65 72 20 2a 2c 20 69 6e 74 ode(Pager *, int
53c80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
53c90 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
53ca0 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50 61 erJournalMode(Pa
53cb0 67 65 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c ger *, int);.SQL
53cc0 49 54 45 5f 50 52 49 56 41 54 45 20 69 36 34 20 ITE_PRIVATE i64
53cd0 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 sqlite3PagerJour
53ce0 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28 50 61 67 nalSizeLimit(Pag
53cf0 65 72 20 2a 2c 20 69 36 34 29 3b 0a 53 51 4c 49 er *, i64);.SQLI
53d00 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 TE_PRIVATE sqlit
53d10 65 33 5f 62 61 63 6b 75 70 20 2a 2a 73 71 6c 69 e3_backup **sqli
53d20 74 65 33 50 61 67 65 72 42 61 63 6b 75 70 50 74 te3PagerBackupPt
53d30 72 28 50 61 67 65 72 2a 29 3b 0a 0a 2f 2a 20 46 r(Pager*);../* F
53d40 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 6f unctions used to
53d50 20 6f 62 74 61 69 6e 20 61 6e 64 20 72 65 6c 65 obtain and rele
53d60 61 73 65 20 70 61 67 65 20 72 65 66 65 72 65 6e ase page referen
53d70 63 65 73 2e 20 2a 2f 20 0a 53 51 4c 49 54 45 5f ces. */ .SQLITE_
53d80 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
53d90 74 65 33 50 61 67 65 72 41 63 71 75 69 72 65 28 te3PagerAcquire(
53da0 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 Pager *pPager, P
53db0 67 6e 6f 20 70 67 6e 6f 2c 20 44 62 50 61 67 65 gno pgno, DbPage
53dc0 20 2a 2a 70 70 50 61 67 65 2c 20 69 6e 74 20 63 **ppPage, int c
53dd0 6c 72 46 6c 61 67 29 3b 0a 23 64 65 66 69 6e 65 lrFlag);.#define
53de0 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 sqlite3PagerGet
53df0 28 41 2c 42 2c 43 29 20 73 71 6c 69 74 65 33 50 (A,B,C) sqlite3P
53e00 61 67 65 72 41 63 71 75 69 72 65 28 41 2c 42 2c agerAcquire(A,B,
53e10 43 2c 30 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 C,0).SQLITE_PRIV
53e20 41 54 45 20 44 62 50 61 67 65 20 2a 73 71 6c 69 ATE DbPage *sqli
53e30 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 50 te3PagerLookup(P
53e40 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 ager *pPager, Pg
53e50 6e 6f 20 70 67 6e 6f 29 3b 0a 53 51 4c 49 54 45 no pgno);.SQLITE
53e60 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
53e70 6c 69 74 65 33 50 61 67 65 72 52 65 66 28 44 62 lite3PagerRef(Db
53e80 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Page*);.SQLITE_P
53e90 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
53ea0 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 44 62 te3PagerUnref(Db
53eb0 50 61 67 65 2a 29 3b 0a 0a 2f 2a 20 4f 70 65 72 Page*);../* Oper
53ec0 61 74 69 6f 6e 73 20 6f 6e 20 70 61 67 65 20 72 ations on page r
53ed0 65 66 65 72 65 6e 63 65 73 2e 20 2a 2f 0a 53 51 eferences. */.SQ
53ee0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
53ef0 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
53f00 74 65 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c te(DbPage*);.SQL
53f10 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
53f20 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e sqlite3PagerDon
53f30 74 57 72 69 74 65 28 44 62 50 61 67 65 2a 29 3b tWrite(DbPage*);
53f40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
53f50 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
53f60 4d 6f 76 65 70 61 67 65 28 50 61 67 65 72 2a 2c Movepage(Pager*,
53f70 44 62 50 61 67 65 2a 2c 50 67 6e 6f 2c 69 6e 74 DbPage*,Pgno,int
53f80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
53f90 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
53fa0 65 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28 44 erPageRefcount(D
53fb0 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f bPage*);.SQLITE_
53fc0 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
53fd0 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 lite3PagerGetDat
53fe0 61 28 44 62 50 61 67 65 20 2a 29 3b 20 0a 53 51 a(DbPage *); .SQ
53ff0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
54000 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 47 d *sqlite3PagerG
54010 65 74 45 78 74 72 61 28 44 62 50 61 67 65 20 2a etExtra(DbPage *
54020 29 3b 20 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e ); ../* Function
54030 73 20 75 73 65 64 20 74 6f 20 6d 61 6e 61 67 65 s used to manage
54040 20 70 61 67 65 72 20 74 72 61 6e 73 61 63 74 69 pager transacti
54050 6f 6e 73 20 61 6e 64 20 73 61 76 65 70 6f 69 6e ons and savepoin
54060 74 73 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ts. */.SQLITE_PR
54070 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
54080 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 3PagerPagecount(
54090 50 61 67 65 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 Pager*, int*);.S
540a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
540b0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 42 65 t sqlite3PagerBe
540c0 67 69 6e 28 50 61 67 65 72 2a 2c 20 69 6e 74 20 gin(Pager*, int
540d0 65 78 46 6c 61 67 2c 20 69 6e 74 29 3b 0a 53 51 exFlag, int);.SQ
540e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
540f0 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d sqlite3PagerCom
54100 6d 69 74 50 68 61 73 65 4f 6e 65 28 50 61 67 65 mitPhaseOne(Page
54110 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a r*,const char *z
54120 4d 61 73 74 65 72 2c 20 69 6e 74 29 3b 0a 53 51 Master, int);.SQ
54130 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
54140 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 79 6e sqlite3PagerSyn
54150 63 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 c(Pager *pPager)
54160 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
54170 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
54180 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 rCommitPhaseTwo(
54190 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f Pager*);.SQLITE_
541a0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
541b0 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b te3PagerRollback
541c0 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 (Pager*);.SQLITE
541d0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
541e0 69 74 65 33 50 61 67 65 72 4f 70 65 6e 53 61 76 ite3PagerOpenSav
541f0 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 50 epoint(Pager *pP
54200 61 67 65 72 2c 20 69 6e 74 20 6e 29 3b 0a 53 51 ager, int n);.SQ
54210 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
54220 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 61 76 sqlite3PagerSav
54230 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 50 epoint(Pager *pP
54240 61 67 65 72 2c 20 69 6e 74 20 6f 70 2c 20 69 6e ager, int op, in
54250 74 20 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a 53 t iSavepoint);.S
54260 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
54270 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 68 t sqlite3PagerSh
54280 61 72 65 64 4c 6f 63 6b 28 50 61 67 65 72 20 2a aredLock(Pager *
54290 70 50 61 67 65 72 29 3b 0a 0a 2f 2a 20 46 75 6e pPager);../* Fun
542a0 63 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 71 ctions used to q
542b0 75 65 72 79 20 70 61 67 65 72 20 73 74 61 74 65 uery pager state
542c0 20 61 6e 64 20 63 6f 6e 66 69 67 75 72 61 74 69 and configurati
542d0 6f 6e 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 on. */.SQLITE_PR
542e0 49 56 41 54 45 20 75 38 20 73 71 6c 69 74 65 33 IVATE u8 sqlite3
542f0 50 61 67 65 72 49 73 72 65 61 64 6f 6e 6c 79 28 PagerIsreadonly(
54300 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f Pager*);.SQLITE_
54310 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
54320 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 te3PagerRefcount
54330 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 (Pager*);.SQLITE
54340 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 _PRIVATE const c
54350 68 61 72 20 2a 73 71 6c 69 74 65 33 50 61 67 65 har *sqlite3Page
54360 72 46 69 6c 65 6e 61 6d 65 28 50 61 67 65 72 2a rFilename(Pager*
54370 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
54380 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f E const sqlite3_
54390 76 66 73 20 2a 73 71 6c 69 74 65 33 50 61 67 65 vfs *sqlite3Page
543a0 72 56 66 73 28 50 61 67 65 72 2a 29 3b 0a 53 51 rVfs(Pager*);.SQ
543b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c LITE_PRIVATE sql
543c0 69 74 65 33 5f 66 69 6c 65 20 2a 73 71 6c 69 74 ite3_file *sqlit
543d0 65 33 50 61 67 65 72 46 69 6c 65 28 50 61 67 65 e3PagerFile(Page
543e0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
543f0 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ATE const char *
54400 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 sqlite3PagerJour
54410 6e 61 6c 6e 61 6d 65 28 50 61 67 65 72 2a 29 3b nalname(Pager*);
54420 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
54430 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
54440 4e 6f 73 79 6e 63 28 50 61 67 65 72 2a 29 3b 0a Nosync(Pager*);.
54450 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
54460 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 oid *sqlite3Page
54470 72 54 65 6d 70 53 70 61 63 65 28 50 61 67 65 72 rTempSpace(Pager
54480 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
54490 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
544a0 67 65 72 49 73 4d 65 6d 64 62 28 50 61 67 65 72 gerIsMemdb(Pager
544b0 2a 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e *);../* Function
544c0 73 20 75 73 65 64 20 74 6f 20 74 72 75 6e 63 61 s used to trunca
544d0 74 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 te the database
544e0 66 69 6c 65 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f file. */.SQLITE_
544f0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
54500 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 ite3PagerTruncat
54510 65 49 6d 61 67 65 28 50 61 67 65 72 2a 2c 50 67 eImage(Pager*,Pg
54520 6e 6f 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f no);../* Functio
54530 6e 73 20 74 6f 20 73 75 70 70 6f 72 74 20 74 65 ns to support te
54540 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67 sting and debugg
54550 69 6e 67 2e 20 2a 2f 0a 23 69 66 20 21 64 65 66 ing. */.#if !def
54560 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 ined(NDEBUG) ||
54570 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 defined(SQLITE_T
54580 45 53 54 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 EST).SQLITE_PRIV
54590 41 54 45 20 20 20 50 67 6e 6f 20 73 71 6c 69 74 ATE Pgno sqlit
545a0 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65 e3PagerPagenumbe
545b0 72 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 r(DbPage*);.SQLI
545c0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 TE_PRIVATE int
545d0 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 sqlite3PagerIsw
545e0 72 69 74 65 61 62 6c 65 28 44 62 50 61 67 65 2a riteable(DbPage*
545f0 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 );.#endif.#ifdef
54600 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
54610 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e ITE_PRIVATE in
54620 74 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 53 t *sqlite3PagerS
54630 74 61 74 73 28 50 61 67 65 72 2a 29 3b 0a 53 51 tats(Pager*);.SQ
54640 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 LITE_PRIVATE v
54650 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 oid sqlite3Pager
54660 52 65 66 64 75 6d 70 28 50 61 67 65 72 2a 29 3b Refdump(Pager*);
54670 0a 20 20 76 6f 69 64 20 64 69 73 61 62 6c 65 5f . void disable_
54680 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 simulated_io_err
54690 6f 72 73 28 76 6f 69 64 29 3b 0a 20 20 76 6f 69 ors(void);. voi
546a0 64 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 d enable_simulat
546b0 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69 ed_io_errors(voi
546c0 64 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 d);.#else.# defi
546d0 6e 65 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c ne disable_simul
546e0 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 ated_io_errors()
546f0 0a 23 20 64 65 66 69 6e 65 20 65 6e 61 62 6c 65 .# define enable
54700 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 _simulated_io_er
54710 72 6f 72 73 28 29 0a 23 65 6e 64 69 66 0a 0a 23 rors().#endif..#
54720 65 6e 64 69 66 20 2f 2a 20 5f 50 41 47 45 52 5f endif /* _PAGER_
54730 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a H_ */../********
54740 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 ****** End of pa
54750 67 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ger.h **********
54760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54780 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
54790 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e ****** Continuin
547a0 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 g where we left
547b0 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 off in sqliteInt
547c0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
547d0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
547e0 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 70 ****** Include p
547f0 63 61 63 68 65 2e 68 20 69 6e 20 74 68 65 20 6d cache.h in the m
54800 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 iddle of sqliteI
54810 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a nt.h ***********
54820 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
54830 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
54840 65 20 70 63 61 63 68 65 2e 68 20 2a 2a 2a 2a 2a e pcache.h *****
54850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54870 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
54880 38 20 41 75 67 75 73 74 20 30 35 0a 2a 2a 0a 2a 8 August 05.**.*
54890 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
548a0 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
548b0 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
548c0 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
548d0 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
548e0 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
548f0 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
54900 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
54910 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
54920 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
54930 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
54940 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
54950 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
54960 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
54970 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
54980 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
54990 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
549a0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
549b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
549c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
549d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
549e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 **********.** Th
549f0 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 is header file d
54a00 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72 efines the inter
54a10 66 61 63 65 20 74 68 61 74 20 74 68 65 20 73 71 face that the sq
54a20 6c 69 74 65 20 70 61 67 65 20 63 61 63 68 65 0a lite page cache.
54a30 2a 2a 20 73 75 62 73 79 73 74 65 6d 2e 20 0a 2a ** subsystem. .*
54a40 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 70 *.** @(#) $Id: p
54a50 63 61 63 68 65 2e 68 2c 76 20 31 2e 32 30 20 32 cache.h,v 1.20 2
54a60 30 30 39 2f 30 37 2f 32 35 20 31 31 3a 34 36 3a 009/07/25 11:46:
54a70 34 39 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 49 danielk1977 E
54a80 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 xp $.*/..#ifndef
54a90 20 5f 50 43 41 43 48 45 5f 48 5f 0a 0a 74 79 70 _PCACHE_H_..typ
54aa0 65 64 65 66 20 73 74 72 75 63 74 20 50 67 48 64 edef struct PgHd
54ab0 72 20 50 67 48 64 72 3b 0a 74 79 70 65 64 65 66 r PgHdr;.typedef
54ac0 20 73 74 72 75 63 74 20 50 43 61 63 68 65 20 50 struct PCache P
54ad0 43 61 63 68 65 3b 0a 0a 2f 2a 0a 2a 2a 20 45 76 Cache;../*.** Ev
54ae0 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20 ery page in the
54af0 63 61 63 68 65 20 69 73 20 63 6f 6e 74 72 6f 6c cache is control
54b00 6c 65 64 20 62 79 20 61 6e 20 69 6e 73 74 61 6e led by an instan
54b10 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
54b20 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 ing.** structure
54b30 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 67 48 64 ..*/.struct PgHd
54b40 72 20 7b 0a 20 20 76 6f 69 64 20 2a 70 44 61 74 r {. void *pDat
54b50 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 a;
54b60 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 /* Content
54b70 6f 66 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a of this page */.
54b80 20 20 76 6f 69 64 20 2a 70 45 78 74 72 61 3b 20 void *pExtra;
54b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54ba0 20 2f 2a 20 45 78 74 72 61 20 63 6f 6e 74 65 6e /* Extra conten
54bb0 74 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 44 t */. PgHdr *pD
54bc0 69 72 74 79 3b 20 20 20 20 20 20 20 20 20 20 20 irty;
54bd0 20 20 20 20 20 20 2f 2a 20 54 72 61 6e 73 69 65 /* Transie
54be0 6e 74 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 nt list of dirty
54bf0 20 70 61 67 65 73 20 2a 2f 0a 20 20 50 67 6e 6f pages */. Pgno
54c00 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 pgno;
54c10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
54c20 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 ge number for th
54c30 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 50 61 67 is page */. Pag
54c40 65 72 20 2a 70 50 61 67 65 72 3b 20 20 20 20 20 er *pPager;
54c50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
54c60 68 65 20 70 61 67 65 72 20 74 68 69 73 20 70 61 he pager this pa
54c70 67 65 20 69 73 20 70 61 72 74 20 6f 66 20 2a 2f ge is part of */
54c80 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 .#ifdef SQLITE_C
54c90 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 75 33 32 HECK_PAGES. u32
54ca0 20 70 61 67 65 48 61 73 68 3b 20 20 20 20 20 20 pageHash;
54cb0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 /* H
54cc0 61 73 68 20 6f 66 20 70 61 67 65 20 63 6f 6e 74 ash of page cont
54cd0 65 6e 74 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 ent */.#endif.
54ce0 75 31 36 20 66 6c 61 67 73 3b 20 20 20 20 20 20 u16 flags;
54cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
54d00 2a 20 50 47 48 44 52 20 66 6c 61 67 73 20 64 65 * PGHDR flags de
54d10 66 69 6e 65 64 20 62 65 6c 6f 77 20 2a 2f 0a 0a fined below */..
54d20 20 20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /*************
54d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20 2a 2a 20 45 *********. ** E
54d70 6c 65 6d 65 6e 74 73 20 61 62 6f 76 65 20 61 72 lements above ar
54d80 65 20 70 75 62 6c 69 63 2e 20 20 41 6c 6c 20 74 e public. All t
54d90 68 61 74 20 66 6f 6c 6c 6f 77 73 20 69 73 20 70 hat follows is p
54da0 72 69 76 61 74 65 20 74 6f 20 70 63 61 63 68 65 rivate to pcache
54db0 2e 63 0a 20 20 2a 2a 20 61 6e 64 20 73 68 6f 75 .c. ** and shou
54dc0 6c 64 20 6e 6f 74 20 62 65 20 61 63 63 65 73 73 ld not be access
54dd0 65 64 20 62 79 20 6f 74 68 65 72 20 6d 6f 64 75 ed by other modu
54de0 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 31 36 20 les.. */. i16
54df0 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 nRef;
54e00 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
54e10 6d 62 65 72 20 6f 66 20 75 73 65 72 73 20 6f 66 mber of users of
54e20 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 this page */.
54e30 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 3b 20 PCache *pCache;
54e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
54e50 2a 20 43 61 63 68 65 20 74 68 61 74 20 6f 77 6e * Cache that own
54e60 73 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 0a s this page */..
54e70 20 20 50 67 48 64 72 20 2a 70 44 69 72 74 79 4e PgHdr *pDirtyN
54e80 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 ext;
54e90 20 2f 2a 20 4e 65 78 74 20 65 6c 65 6d 65 6e 74 /* Next element
54ea0 20 69 6e 20 6c 69 73 74 20 6f 66 20 64 69 72 74 in list of dirt
54eb0 79 20 70 61 67 65 73 20 2a 2f 0a 20 20 50 67 48 y pages */. PgH
54ec0 64 72 20 2a 70 44 69 72 74 79 50 72 65 76 3b 20 dr *pDirtyPrev;
54ed0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
54ee0 72 65 76 69 6f 75 73 20 65 6c 65 6d 65 6e 74 20 revious element
54ef0 69 6e 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 in list of dirty
54f00 20 70 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a pages */.};../*
54f10 20 42 69 74 20 76 61 6c 75 65 73 20 66 6f 72 20 Bit values for
54f20 50 67 48 64 72 2e 66 6c 61 67 73 20 2a 2f 0a 23 PgHdr.flags */.#
54f30 64 65 66 69 6e 65 20 50 47 48 44 52 5f 44 49 52 define PGHDR_DIR
54f40 54 59 20 20 20 20 20 20 20 20 20 20 20 20 20 30 TY 0
54f50 78 30 30 32 20 20 2f 2a 20 50 61 67 65 20 68 61 x002 /* Page ha
54f60 73 20 63 68 61 6e 67 65 64 20 2a 2f 0a 23 64 65 s changed */.#de
54f70 66 69 6e 65 20 50 47 48 44 52 5f 4e 45 45 44 5f fine PGHDR_NEED_
54f80 53 59 4e 43 20 20 20 20 20 20 20 20 20 30 78 30 SYNC 0x0
54f90 30 34 20 20 2f 2a 20 46 73 79 6e 63 20 74 68 65 04 /* Fsync the
54fa0 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 rollback journa
54fb0 6c 20 62 65 66 6f 72 65 0a 20 20 20 20 20 20 20 l before.
54fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54fe0 2a 2a 20 77 72 69 74 69 6e 67 20 74 68 69 73 20 ** writing this
54ff0 70 61 67 65 20 74 6f 20 74 68 65 20 64 61 74 61 page to the data
55000 62 61 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 base */.#define
55010 50 47 48 44 52 5f 4e 45 45 44 5f 52 45 41 44 20 PGHDR_NEED_READ
55020 20 20 20 20 20 20 20 20 30 78 30 30 38 20 20 2f 0x008 /
55030 2a 20 43 6f 6e 74 65 6e 74 20 69 73 20 75 6e 72 * Content is unr
55040 65 61 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 ead */.#define P
55050 47 48 44 52 5f 52 45 55 53 45 5f 55 4e 4c 49 4b GHDR_REUSE_UNLIK
55060 45 4c 59 20 20 20 20 30 78 30 31 30 20 20 2f 2a ELY 0x010 /*
55070 20 41 20 68 69 6e 74 20 74 68 61 74 20 72 65 75 A hint that reu
55080 73 65 20 69 73 20 75 6e 6c 69 6b 65 6c 79 20 2a se is unlikely *
55090 2f 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52 5f /.#define PGHDR_
550a0 44 4f 4e 54 5f 57 52 49 54 45 20 20 20 20 20 20 DONT_WRITE
550b0 20 20 30 78 30 32 30 20 20 2f 2a 20 44 6f 20 6e 0x020 /* Do n
550c0 6f 74 20 77 72 69 74 65 20 63 6f 6e 74 65 6e 74 ot write content
550d0 20 74 6f 20 64 69 73 6b 20 2a 2f 0a 0a 2f 2a 20 to disk */../*
550e0 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 73 Initialize and s
550f0 68 75 74 64 6f 77 6e 20 74 68 65 20 70 61 67 65 hutdown the page
55100 20 63 61 63 68 65 20 73 75 62 73 79 73 74 65 6d cache subsystem
55110 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */.SQLITE_PRIVA
55120 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 TE int sqlite3Pc
55130 61 63 68 65 49 6e 69 74 69 61 6c 69 7a 65 28 76 acheInitialize(v
55140 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 oid);.SQLITE_PRI
55150 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
55160 33 50 63 61 63 68 65 53 68 75 74 64 6f 77 6e 28 3PcacheShutdown(
55170 76 6f 69 64 29 3b 0a 0a 2f 2a 20 50 61 67 65 20 void);../* Page
55180 63 61 63 68 65 20 62 75 66 66 65 72 20 6d 61 6e cache buffer man
55190 61 67 65 6d 65 6e 74 3a 0a 2a 2a 20 54 68 65 73 agement:.** Thes
551a0 65 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 e routines imple
551b0 6d 65 6e 74 20 53 51 4c 49 54 45 5f 43 4f 4e 46 ment SQLITE_CONF
551c0 49 47 5f 50 41 47 45 43 41 43 48 45 2e 0a 2a 2f IG_PAGECACHE..*/
551d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
551e0 76 6f 69 64 20 73 71 6c 69 74 65 33 50 43 61 63 void sqlite3PCac
551f0 68 65 42 75 66 66 65 72 53 65 74 75 70 28 76 6f heBufferSetup(vo
55200 69 64 20 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 6e id *, int sz, in
55210 74 20 6e 29 3b 0a 0a 2f 2a 20 43 72 65 61 74 65 t n);../* Create
55220 20 61 20 6e 65 77 20 70 61 67 65 72 20 63 61 63 a new pager cac
55230 68 65 2e 0a 2a 2a 20 55 6e 64 65 72 20 6d 65 6d he..** Under mem
55240 6f 72 79 20 73 74 72 65 73 73 2c 20 69 6e 76 6f ory stress, invo
55250 6b 65 20 78 53 74 72 65 73 73 20 74 6f 20 74 72 ke xStress to tr
55260 79 20 74 6f 20 6d 61 6b 65 20 70 61 67 65 73 20 y to make pages
55270 63 6c 65 61 6e 2e 0a 2a 2a 20 4f 6e 6c 79 20 63 clean..** Only c
55280 6c 65 61 6e 20 61 6e 64 20 75 6e 70 69 6e 6e 65 lean and unpinne
55290 64 20 70 61 67 65 73 20 63 61 6e 20 62 65 20 72 d pages can be r
552a0 65 63 6c 61 69 6d 65 64 2e 0a 2a 2f 0a 53 51 4c eclaimed..*/.SQL
552b0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
552c0 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4f 70 sqlite3PcacheOp
552d0 65 6e 28 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 en(. int szPage
552e0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
552f0 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
55300 65 76 65 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 every page */.
55310 69 6e 74 20 73 7a 45 78 74 72 61 2c 20 20 20 20 int szExtra,
55320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
55330 2a 20 45 78 74 72 61 20 73 70 61 63 65 20 61 73 * Extra space as
55340 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 65 61 sociated with ea
55350 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 ch page */. int
55360 20 62 50 75 72 67 65 61 62 6c 65 2c 20 20 20 20 bPurgeable,
55370 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
55380 72 75 65 20 69 66 20 70 61 67 65 73 20 61 72 65 rue if pages are
55390 20 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74 6f 72 on backing stor
553a0 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 74 e */. int (*xSt
553b0 72 65 73 73 29 28 76 6f 69 64 2a 2c 20 50 67 48 ress)(void*, PgH
553c0 64 72 2a 29 2c 20 2f 2a 20 43 61 6c 6c 20 74 6f dr*), /* Call to
553d0 20 74 72 79 20 74 6f 20 6d 61 6b 65 20 70 61 67 try to make pag
553e0 65 73 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 6f es clean */. vo
553f0 69 64 20 2a 70 53 74 72 65 73 73 2c 20 20 20 20 id *pStress,
55400 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
55410 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 Argument to xStr
55420 65 73 73 20 2a 2f 0a 20 20 50 43 61 63 68 65 20 ess */. PCache
55430 2a 70 54 6f 49 6e 69 74 20 20 20 20 20 20 20 20 *pToInit
55440 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 61 6c /* Preal
55450 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20 66 6f located space fo
55460 72 20 74 68 65 20 50 43 61 63 68 65 20 2a 2f 0a r the PCache */.
55470 29 3b 0a 0a 2f 2a 20 4d 6f 64 69 66 79 20 74 68 );../* Modify th
55480 65 20 70 61 67 65 2d 73 69 7a 65 20 61 66 74 65 e page-size afte
55490 72 20 74 68 65 20 63 61 63 68 65 20 68 61 73 20 r the cache has
554a0 62 65 65 6e 20 63 72 65 61 74 65 64 2e 20 2a 2f been created. */
554b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
554c0 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
554d0 68 65 53 65 74 50 61 67 65 53 69 7a 65 28 50 43 heSetPageSize(PC
554e0 61 63 68 65 20 2a 2c 20 69 6e 74 29 3b 0a 0a 2f ache *, int);../
554f0 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a * Return the siz
55500 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 61 20 e in bytes of a
55510 50 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 20 PCache object.
55520 55 73 65 64 20 74 6f 20 70 72 65 61 6c 6c 6f 63 Used to prealloc
55530 61 74 65 0a 2a 2a 20 73 74 6f 72 61 67 65 20 73 ate.** storage s
55540 70 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f pace..*/.SQLITE_
55550 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
55560 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 76 6f te3PcacheSize(vo
55570 69 64 29 3b 0a 0a 2f 2a 20 4f 6e 65 20 72 65 6c id);../* One rel
55580 65 61 73 65 20 70 65 72 20 73 75 63 63 65 73 73 ease per success
55590 66 75 6c 20 66 65 74 63 68 2e 20 20 50 61 67 65 ful fetch. Page
555a0 20 69 73 20 70 69 6e 6e 65 64 20 75 6e 74 69 6c is pinned until
555b0 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 20 52 65 released..** Re
555c0 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 65 64 2e ference counted.
555d0 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 .*/.SQLITE_PRIV
555e0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
555f0 63 61 63 68 65 46 65 74 63 68 28 50 43 61 63 68 cacheFetch(PCach
55600 65 2a 2c 20 50 67 6e 6f 2c 20 69 6e 74 20 63 72 e*, Pgno, int cr
55610 65 61 74 65 46 6c 61 67 2c 20 50 67 48 64 72 2a eateFlag, PgHdr*
55620 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
55630 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
55640 63 61 63 68 65 52 65 6c 65 61 73 65 28 50 67 48 cacheRelease(PgH
55650 64 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 dr*);..SQLITE_PR
55660 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
55670 65 33 50 63 61 63 68 65 44 72 6f 70 28 50 67 48 e3PcacheDrop(PgH
55680 64 72 2a 29 3b 20 20 20 20 20 20 20 20 20 2f 2a dr*); /*
55690 20 52 65 6d 6f 76 65 20 70 61 67 65 20 66 72 6f Remove page fro
556a0 6d 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 m cache */.SQLIT
556b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
556c0 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 qlite3PcacheMake
556d0 44 69 72 74 79 28 50 67 48 64 72 2a 29 3b 20 20 Dirty(PgHdr*);
556e0 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 70 /* Make sure p
556f0 61 67 65 20 69 73 20 6d 61 72 6b 65 64 20 64 69 age is marked di
55700 72 74 79 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 rty */.SQLITE_PR
55710 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
55720 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 e3PcacheMakeClea
55730 6e 28 50 67 48 64 72 2a 29 3b 20 20 20 20 2f 2a n(PgHdr*); /*
55740 20 4d 61 72 6b 20 61 20 73 69 6e 67 6c 65 20 70 Mark a single p
55750 61 67 65 20 61 73 20 63 6c 65 61 6e 20 2a 2f 0a age as clean */.
55760 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
55770 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
55780 65 43 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68 65 eCleanAll(PCache
55790 2a 29 3b 20 20 20 20 2f 2a 20 4d 61 72 6b 20 61 *); /* Mark a
557a0 6c 6c 20 64 69 72 74 79 20 6c 69 73 74 20 70 61 ll dirty list pa
557b0 67 65 73 20 61 73 20 63 6c 65 61 6e 20 2a 2f 0a ges as clean */.
557c0 0a 2f 2a 20 43 68 61 6e 67 65 20 61 20 70 61 67 ./* Change a pag
557d0 65 20 6e 75 6d 62 65 72 2e 20 20 55 73 65 64 20 e number. Used
557e0 62 79 20 69 6e 63 72 2d 76 61 63 75 75 6d 2e 20 by incr-vacuum.
557f0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
55800 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 E void sqlite3Pc
55810 61 63 68 65 4d 6f 76 65 28 50 67 48 64 72 2a 2c acheMove(PgHdr*,
55820 20 50 67 6e 6f 29 3b 0a 0a 2f 2a 20 52 65 6d 6f Pgno);../* Remo
55830 76 65 20 61 6c 6c 20 70 61 67 65 73 20 77 69 74 ve all pages wit
55840 68 20 70 67 6e 6f 3e 78 2e 20 20 52 65 73 65 74 h pgno>x. Reset
55850 20 74 68 65 20 63 61 63 68 65 20 69 66 20 78 3d the cache if x=
55860 3d 30 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 =0 */.SQLITE_PRI
55870 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
55880 33 50 63 61 63 68 65 54 72 75 6e 63 61 74 65 28 3PcacheTruncate(
55890 50 43 61 63 68 65 2a 2c 20 50 67 6e 6f 20 78 29 PCache*, Pgno x)
558a0 3b 0a 0a 2f 2a 20 47 65 74 20 61 20 6c 69 73 74 ;../* Get a list
558b0 20 6f 66 20 61 6c 6c 20 64 69 72 74 79 20 70 61 of all dirty pa
558c0 67 65 73 20 69 6e 20 74 68 65 20 63 61 63 68 65 ges in the cache
558d0 2c 20 73 6f 72 74 65 64 20 62 79 20 70 61 67 65 , sorted by page
558e0 20 6e 75 6d 62 65 72 20 2a 2f 0a 53 51 4c 49 54 number */.SQLIT
558f0 45 5f 50 52 49 56 41 54 45 20 50 67 48 64 72 20 E_PRIVATE PgHdr
55900 2a 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 69 *sqlite3PcacheDi
55910 72 74 79 4c 69 73 74 28 50 43 61 63 68 65 2a 29 rtyList(PCache*)
55920 3b 0a 0a 2f 2a 20 52 65 73 65 74 20 61 6e 64 20 ;../* Reset and
55930 63 6c 6f 73 65 20 74 68 65 20 63 61 63 68 65 20 close the cache
55940 6f 62 6a 65 63 74 20 2a 2f 0a 53 51 4c 49 54 45 object */.SQLITE
55950 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
55960 6c 69 74 65 33 50 63 61 63 68 65 43 6c 6f 73 65 lite3PcacheClose
55970 28 50 43 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20 43 (PCache*);../* C
55980 6c 65 61 72 20 66 6c 61 67 73 20 66 72 6f 6d 20 lear flags from
55990 70 61 67 65 73 20 6f 66 20 74 68 65 20 70 61 67 pages of the pag
559a0 65 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 e cache */.SQLIT
559b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
559c0 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 qlite3PcacheClea
559d0 72 53 79 6e 63 46 6c 61 67 73 28 50 43 61 63 68 rSyncFlags(PCach
559e0 65 20 2a 29 3b 0a 0a 2f 2a 20 44 69 73 63 61 72 e *);../* Discar
559f0 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f d the contents o
55a00 66 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a 53 f the cache */.S
55a10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
55a20 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
55a30 43 6c 65 61 72 28 50 43 61 63 68 65 2a 29 3b 0a Clear(PCache*);.
55a40 0a 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74 ./* Return the t
55a50 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 6f otal number of o
55a60 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 20 utstanding page
55a70 72 65 66 65 72 65 6e 63 65 73 20 2a 2f 0a 53 51 references */.SQ
55a80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
55a90 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 sqlite3PcacheRe
55aa0 66 43 6f 75 6e 74 28 50 43 61 63 68 65 2a 29 3b fCount(PCache*);
55ab0 0a 0a 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 ../* Increment t
55ac0 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 he reference cou
55ad0 6e 74 20 6f 66 20 61 6e 20 65 78 69 73 74 69 6e nt of an existin
55ae0 67 20 70 61 67 65 20 2a 2f 0a 53 51 4c 49 54 45 g page */.SQLITE
55af0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
55b00 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 28 50 lite3PcacheRef(P
55b10 67 48 64 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f gHdr*);..SQLITE_
55b20 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
55b30 74 65 33 50 63 61 63 68 65 50 61 67 65 52 65 66 te3PcachePageRef
55b40 63 6f 75 6e 74 28 50 67 48 64 72 2a 29 3b 0a 0a count(PgHdr*);..
55b50 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f /* Return the to
55b60 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 tal number of pa
55b70 67 65 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 ges stored in th
55b80 65 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 e cache */.SQLIT
55b90 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
55ba0 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 65 63 lite3PcachePagec
55bb0 6f 75 6e 74 28 50 43 61 63 68 65 2a 29 3b 0a 0a ount(PCache*);..
55bc0 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 #if defined(SQLI
55bd0 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 29 20 TE_CHECK_PAGES)
55be0 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 || defined(SQLIT
55bf0 45 5f 44 45 42 55 47 29 0a 2f 2a 20 49 74 65 72 E_DEBUG)./* Iter
55c00 61 74 65 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 ate through all
55c10 64 69 72 74 79 20 70 61 67 65 73 20 63 75 72 72 dirty pages curr
55c20 65 6e 74 6c 79 20 73 74 6f 72 65 64 20 69 6e 20 ently stored in
55c30 74 68 65 20 63 61 63 68 65 2e 20 54 68 69 73 0a the cache. This.
55c40 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 ** interface is
55c50 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 only available i
55c60 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 f SQLITE_CHECK_P
55c70 41 47 45 53 20 69 73 20 64 65 66 69 6e 65 64 20 AGES is defined
55c80 77 68 65 6e 20 74 68 65 20 0a 2a 2a 20 6c 69 62 when the .** lib
55c90 72 61 72 79 20 69 73 20 62 75 69 6c 74 2e 0a 2a rary is built..*
55ca0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
55cb0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 void sqlite3Pca
55cc0 63 68 65 49 74 65 72 61 74 65 44 69 72 74 79 28 cheIterateDirty(
55cd0 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20 PCache *pCache,
55ce0 76 6f 69 64 20 28 2a 78 49 74 65 72 29 28 50 67 void (*xIter)(Pg
55cf0 48 64 72 20 2a 29 29 3b 0a 23 65 6e 64 69 66 0a Hdr *));.#endif.
55d00 0a 2f 2a 20 53 65 74 20 61 6e 64 20 67 65 74 20 ./* Set and get
55d10 74 68 65 20 73 75 67 67 65 73 74 65 64 20 63 61 the suggested ca
55d20 63 68 65 2d 73 69 7a 65 20 66 6f 72 20 74 68 65 che-size for the
55d30 20 73 70 65 63 69 66 69 65 64 20 70 61 67 65 72 specified pager
55d40 2d 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 -cache..**.** If
55d50 20 6e 6f 20 67 6c 6f 62 61 6c 20 6d 61 78 69 6d no global maxim
55d60 75 6d 20 69 73 20 63 6f 6e 66 69 67 75 72 65 64 um is configured
55d70 2c 20 74 68 65 6e 20 74 68 65 20 73 79 73 74 65 , then the syste
55d80 6d 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6c 69 m attempts to li
55d90 6d 69 74 0a 2a 2a 20 74 68 65 20 74 6f 74 61 6c mit.** the total
55da0 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
55db0 20 63 61 63 68 65 64 20 62 79 20 70 75 72 67 65 cached by purge
55dc0 61 62 6c 65 20 70 61 67 65 72 2d 63 61 63 68 65 able pager-cache
55dd0 73 20 74 6f 20 74 68 65 20 73 75 6d 0a 2a 2a 20 s to the sum.**
55de0 6f 66 20 74 68 65 20 73 75 67 67 65 73 74 65 64 of the suggested
55df0 20 63 61 63 68 65 2d 73 69 7a 65 73 2e 0a 2a 2f cache-sizes..*/
55e00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
55e10 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
55e20 68 65 53 65 74 43 61 63 68 65 73 69 7a 65 28 50 heSetCachesize(P
55e30 43 61 63 68 65 20 2a 2c 20 69 6e 74 29 3b 0a 23 Cache *, int);.#
55e40 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
55e50 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 T.SQLITE_PRIVATE
55e60 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 int sqlite3Pcac
55e70 68 65 47 65 74 43 61 63 68 65 73 69 7a 65 28 50 heGetCachesize(P
55e80 43 61 63 68 65 20 2a 29 3b 0a 23 65 6e 64 69 66 Cache *);.#endif
55e90 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
55ea0 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 ENABLE_MEMORY_MA
55eb0 4e 41 47 45 4d 45 4e 54 0a 2f 2a 20 54 72 79 20 NAGEMENT./* Try
55ec0 74 6f 20 72 65 74 75 72 6e 20 6d 65 6d 6f 72 79 to return memory
55ed0 20 75 73 65 64 20 62 79 20 74 68 65 20 70 63 61 used by the pca
55ee0 63 68 65 20 6d 6f 64 75 6c 65 20 74 6f 20 74 68 che module to th
55ef0 65 20 6d 61 69 6e 20 6d 65 6d 6f 72 79 20 68 65 e main memory he
55f00 61 70 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 ap */.SQLITE_PRI
55f10 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
55f20 50 63 61 63 68 65 52 65 6c 65 61 73 65 4d 65 6d PcacheReleaseMem
55f30 6f 72 79 28 69 6e 74 29 3b 0a 23 65 6e 64 69 66 ory(int);.#endif
55f40 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
55f50 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 TEST.SQLITE_PRIV
55f60 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
55f70 50 63 61 63 68 65 53 74 61 74 73 28 69 6e 74 2a PcacheStats(int*
55f80 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29 ,int*,int*,int*)
55f90 3b 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 ;.#endif..SQLITE
55fa0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
55fb0 6c 69 74 65 33 50 43 61 63 68 65 53 65 74 44 65 lite3PCacheSetDe
55fc0 66 61 75 6c 74 28 76 6f 69 64 29 3b 0a 0a 23 65 fault(void);..#e
55fd0 6e 64 69 66 20 2f 2a 20 5f 50 43 41 43 48 45 5f ndif /* _PCACHE_
55fe0 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a H_ */../********
55ff0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 63 ****** End of pc
56000 61 63 68 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a ache.h *********
56010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56030 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
56040 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e ****** Continuin
56050 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 g where we left
56060 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 off in sqliteInt
56070 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
56080 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a *****/../*******
56090 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 ******* Include
560a0 6f 73 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 os.h in the midd
560b0 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e le of sqliteInt.
560c0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
560d0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
560e0 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
560f0 6c 65 20 6f 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a le os.h ********
56100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56120 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
56130 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 36 0a 01 September 16.
56140 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
56150 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
56160 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
56170 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
56180 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
56190 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
561a0 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
561b0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
561c0 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
561d0 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
561e0 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
561f0 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
56200 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
56210 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
56220 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
56230 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
56240 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
56250 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
56260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
562a0 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 ***.**.** This h
562b0 65 61 64 65 72 20 66 69 6c 65 20 28 74 6f 67 65 eader file (toge
562c0 74 68 65 72 20 77 69 74 68 20 69 73 20 63 6f 6d ther with is com
562d0 70 61 6e 69 6f 6e 20 43 20 73 6f 75 72 63 65 2d panion C source-
562e0 63 6f 64 65 20 66 69 6c 65 0a 2a 2a 20 22 6f 73 code file.** "os
562f0 2e 63 22 29 20 61 74 74 65 6d 70 74 20 74 6f 20 .c") attempt to
56300 61 62 73 74 72 61 63 74 20 74 68 65 20 75 6e 64 abstract the und
56310 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e erlying operatin
56320 67 20 73 79 73 74 65 6d 20 73 6f 20 74 68 61 74 g system so that
56330 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20 6c .** the SQLite l
56340 69 62 72 61 72 79 20 77 69 6c 6c 20 77 6f 72 6b ibrary will work
56350 20 6f 6e 20 62 6f 74 68 20 50 4f 53 49 58 20 61 on both POSIX a
56360 6e 64 20 77 69 6e 64 6f 77 73 20 73 79 73 74 65 nd windows syste
56370 6d 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 ms..**.** This h
56380 65 61 64 65 72 20 66 69 6c 65 20 69 73 20 23 69 eader file is #i
56390 6e 63 6c 75 64 65 2d 65 64 20 62 79 20 73 71 6c nclude-ed by sql
563a0 69 74 65 49 6e 74 2e 68 20 61 6e 64 20 74 68 75 iteInt.h and thu
563b0 73 20 65 6e 64 73 20 75 70 0a 2a 2a 20 62 65 69 s ends up.** bei
563c0 6e 67 20 69 6e 63 6c 75 64 65 64 20 62 79 20 65 ng included by e
563d0 76 65 72 79 20 73 6f 75 72 63 65 20 66 69 6c 65 very source file
563e0 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 2e ..**.** $Id: os.
563f0 68 2c 76 20 31 2e 31 30 38 20 32 30 30 39 2f 30 h,v 1.108 2009/0
56400 32 2f 30 35 20 31 36 3a 33 31 3a 34 36 20 64 72 2/05 16:31:46 dr
56410 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 h Exp $.*/.#ifnd
56420 65 66 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 48 5f ef _SQLITE_OS_H_
56430 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 .#define _SQLITE
56440 5f 4f 53 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 46 69 _OS_H_../*.** Fi
56450 67 75 72 65 20 6f 75 74 20 69 66 20 77 65 20 61 gure out if we a
56460 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 re dealing with
56470 55 6e 69 78 2c 20 57 69 6e 64 6f 77 73 2c 20 6f Unix, Windows, o
56480 72 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 r some other.**
56490 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d operating system
564a0 2e 20 20 41 66 74 65 72 20 74 68 65 20 66 6f 6c . After the fol
564b0 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 6f 66 20 lowing block of
564c0 70 72 65 70 72 6f 63 65 73 73 20 6d 61 63 72 6f preprocess macro
564d0 73 2c 0a 2a 2a 20 61 6c 6c 20 6f 66 20 53 51 4c s,.** all of SQL
564e0 49 54 45 5f 4f 53 5f 55 4e 49 58 2c 20 53 51 4c ITE_OS_UNIX, SQL
564f0 49 54 45 5f 4f 53 5f 57 49 4e 2c 20 53 51 4c 49 ITE_OS_WIN, SQLI
56500 54 45 5f 4f 53 5f 4f 53 32 2c 20 61 6e 64 20 53 TE_OS_OS2, and S
56510 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 20 0a QLITE_OS_OTHER .
56520 2a 2a 20 77 69 6c 6c 20 64 65 66 69 6e 65 64 20 ** will defined
56530 74 6f 20 65 69 74 68 65 72 20 31 20 6f 72 20 30 to either 1 or 0
56540 2e 20 20 4f 6e 65 20 6f 66 20 74 68 65 20 66 6f . One of the fo
56550 75 72 20 77 69 6c 6c 20 62 65 20 31 2e 20 20 54 ur will be 1. T
56560 68 65 20 6f 74 68 65 72 20 0a 2a 2a 20 74 68 72 he other .** thr
56570 65 65 20 77 69 6c 6c 20 62 65 20 30 2e 0a 2a 2f ee will be 0..*/
56580 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c .#if defined(SQL
56590 49 54 45 5f 4f 53 5f 4f 54 48 45 52 29 0a 23 20 ITE_OS_OTHER).#
565a0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 if SQLITE_OS_OTH
565b0 45 52 3d 3d 31 0a 23 20 20 20 75 6e 64 65 66 20 ER==1.# undef
565c0 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 0a 23 SQLITE_OS_UNIX.#
565d0 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 define SQLITE
565e0 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 20 75 _OS_UNIX 0.# u
565f0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 ndef SQLITE_OS_W
56600 49 4e 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51 IN.# define SQ
56610 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 LITE_OS_WIN 0.#
56620 20 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f undef SQLITE_O
56630 53 5f 4f 53 32 0a 23 20 20 20 64 65 66 69 6e 65 S_OS2.# define
56640 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 30 SQLITE_OS_OS2 0
56650 0a 23 20 65 6c 73 65 0a 23 20 20 20 75 6e 64 65 .# else.# unde
56660 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 f SQLITE_OS_OTHE
56670 52 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 R.# endif.#endif
56680 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
56690 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 29 20 26 26 LITE_OS_UNIX) &&
566a0 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
566b0 5f 4f 53 5f 4f 54 48 45 52 29 0a 23 20 64 65 66 _OS_OTHER).# def
566c0 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 ine SQLITE_OS_OT
566d0 48 45 52 20 30 0a 23 20 69 66 6e 64 65 66 20 53 HER 0.# ifndef S
566e0 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 QLITE_OS_WIN.#
566f0 20 69 66 20 64 65 66 69 6e 65 64 28 5f 57 49 4e if defined(_WIN
56700 33 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 57 32) || defined(W
56710 49 4e 33 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 IN32) || defined
56720 28 5f 5f 43 59 47 57 49 4e 5f 5f 29 20 7c 7c 20 (__CYGWIN__) ||
56730 64 65 66 69 6e 65 64 28 5f 5f 4d 49 4e 47 57 33 defined(__MINGW3
56740 32 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 2__) || defined(
56750 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a 23 20 __BORLANDC__).#
56760 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 define SQLIT
56770 45 5f 4f 53 5f 57 49 4e 20 31 0a 23 20 20 20 20 E_OS_WIN 1.#
56780 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f define SQLITE_O
56790 53 5f 55 4e 49 58 20 30 0a 23 20 20 20 20 20 64 S_UNIX 0.# d
567a0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f efine SQLITE_OS_
567b0 4f 53 32 20 30 0a 23 20 20 20 65 6c 69 66 20 64 OS2 0.# elif d
567c0 65 66 69 6e 65 64 28 5f 5f 45 4d 58 5f 5f 29 20 efined(__EMX__)
567d0 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4f 53 32 29 || defined(_OS2)
567e0 20 7c 7c 20 64 65 66 69 6e 65 64 28 4f 53 32 29 || defined(OS2)
567f0 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4f 53 32 || defined(_OS2
56800 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f _) || defined(__
56810 4f 53 32 5f 5f 29 0a 23 20 20 20 20 20 64 65 66 OS2__).# def
56820 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 ine SQLITE_OS_WI
56830 4e 20 30 0a 23 20 20 20 20 20 64 65 66 69 6e 65 N 0.# define
56840 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 SQLITE_OS_UNIX
56850 30 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 53 0.# define S
56860 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 31 0a 23 QLITE_OS_OS2 1.#
56870 20 20 20 65 6c 73 65 0a 23 20 20 20 20 20 64 65 else.# de
56880 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 fine SQLITE_OS_W
56890 49 4e 20 30 0a 23 20 20 20 20 20 64 65 66 69 6e IN 0.# defin
568a0 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 e SQLITE_OS_UNIX
568b0 20 31 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 1.# define
568c0 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 30 0a SQLITE_OS_OS2 0.
568d0 23 20 20 65 6e 64 69 66 0a 23 20 65 6c 73 65 0a # endif.# else.
568e0 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 # define SQLITE
568f0 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 64 65 _OS_UNIX 0.# de
56900 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f fine SQLITE_OS_O
56910 53 32 20 30 0a 23 20 65 6e 64 69 66 0a 23 65 6c S2 0.# endif.#el
56920 73 65 0a 23 20 69 66 6e 64 65 66 20 53 51 4c 49 se.# ifndef SQLI
56930 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 64 65 66 TE_OS_WIN.# def
56940 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 ine SQLITE_OS_WI
56950 4e 20 30 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 N 0.# endif.#end
56960 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d if../*.** Determ
56970 69 6e 65 20 69 66 20 77 65 20 61 72 65 20 64 65 ine if we are de
56980 61 6c 69 6e 67 20 77 69 74 68 20 57 69 6e 64 6f aling with Windo
56990 77 73 43 45 20 2d 20 77 68 69 63 68 20 68 61 73 wsCE - which has
569a0 20 61 20 6d 75 63 68 0a 2a 2a 20 72 65 64 75 63 a much.** reduc
569b0 65 64 20 41 50 49 2e 0a 2a 2f 0a 23 69 66 20 64 ed API..*/.#if d
569c0 65 66 69 6e 65 64 28 5f 57 49 4e 33 32 5f 57 43 efined(_WIN32_WC
569d0 45 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 E).# define SQLI
569e0 54 45 5f 4f 53 5f 57 49 4e 43 45 20 31 0a 23 65 TE_OS_WINCE 1.#e
569f0 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c lse.# define SQL
56a00 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 30 0a 23 ITE_OS_WINCE 0.#
56a10 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 endif.../*.** De
56a20 66 69 6e 65 20 74 68 65 20 6d 61 78 69 6d 75 6d fine the maximum
56a30 20 73 69 7a 65 20 6f 66 20 61 20 74 65 6d 70 6f size of a tempo
56a40 72 61 72 79 20 66 69 6c 65 6e 61 6d 65 0a 2a 2f rary filename.*/
56a50 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 .#if SQLITE_OS_W
56a60 49 4e 0a 23 20 69 6e 63 6c 75 64 65 20 3c 77 69 IN.# include <wi
56a70 6e 64 6f 77 73 2e 68 3e 0a 23 20 64 65 66 69 6e ndows.h>.# defin
56a80 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d e SQLITE_TEMPNAM
56a90 45 5f 53 49 5a 45 20 28 4d 41 58 5f 50 41 54 48 E_SIZE (MAX_PATH
56aa0 2b 35 30 29 0a 23 65 6c 69 66 20 53 51 4c 49 54 +50).#elif SQLIT
56ab0 45 5f 4f 53 5f 4f 53 32 0a 23 20 69 66 20 28 5f E_OS_OS2.# if (_
56ac0 5f 47 4e 55 43 5f 5f 20 3e 20 33 20 7c 7c 20 5f _GNUC__ > 3 || _
56ad0 5f 47 4e 55 43 5f 5f 20 3d 3d 20 33 20 26 26 20 _GNUC__ == 3 &&
56ae0 5f 5f 47 4e 55 43 5f 4d 49 4e 4f 52 5f 5f 20 3e __GNUC_MINOR__ >
56af0 3d 20 33 29 20 26 26 20 64 65 66 69 6e 65 64 28 = 3) && defined(
56b00 4f 53 32 5f 48 49 47 48 5f 4d 45 4d 4f 52 59 29 OS2_HIGH_MEMORY)
56b10 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 6f 73 32 .# include <os2
56b20 73 61 66 65 2e 68 3e 20 2f 2a 20 68 61 73 20 74 safe.h> /* has t
56b30 6f 20 62 65 20 69 6e 63 6c 75 64 65 64 20 62 65 o be included be
56b40 66 6f 72 65 20 6f 73 32 2e 68 20 66 6f 72 20 6c fore os2.h for l
56b50 69 6e 6b 69 6e 67 20 74 6f 20 77 6f 72 6b 20 2a inking to work *
56b60 2f 0a 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69 /.# endif.# defi
56b70 6e 65 20 49 4e 43 4c 5f 44 4f 53 44 41 54 45 54 ne INCL_DOSDATET
56b80 49 4d 45 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 IME.# define INC
56b90 4c 5f 44 4f 53 46 49 4c 45 4d 47 52 0a 23 20 64 L_DOSFILEMGR.# d
56ba0 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 45 52 efine INCL_DOSER
56bb0 52 4f 52 53 0a 23 20 64 65 66 69 6e 65 20 49 4e RORS.# define IN
56bc0 43 4c 5f 44 4f 53 4d 49 53 43 0a 23 20 64 65 66 CL_DOSMISC.# def
56bd0 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 50 52 4f 43 ine INCL_DOSPROC
56be0 45 53 53 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 ESS.# define INC
56bf0 4c 5f 44 4f 53 4d 4f 44 55 4c 45 4d 47 52 0a 23 L_DOSMODULEMGR.#
56c00 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 define INCL_DOS
56c10 53 45 4d 41 50 48 4f 52 45 53 0a 23 20 69 6e 63 SEMAPHORES.# inc
56c20 6c 75 64 65 20 3c 6f 73 32 2e 68 3e 0a 23 20 69 lude <os2.h>.# i
56c30 6e 63 6c 75 64 65 20 3c 75 63 6f 6e 76 2e 68 3e nclude <uconv.h>
56c40 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
56c50 5f 54 45 4d 50 4e 41 4d 45 5f 53 49 5a 45 20 28 _TEMPNAME_SIZE (
56c60 43 43 48 4d 41 58 50 41 54 48 43 4f 4d 50 29 0a CCHMAXPATHCOMP).
56c70 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 #else.# define S
56c80 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d 45 5f 53 QLITE_TEMPNAME_S
56c90 49 5a 45 20 32 30 30 0a 23 65 6e 64 69 66 0a 0a IZE 200.#endif..
56ca0 2f 2a 20 49 66 20 74 68 65 20 53 45 54 5f 46 55 /* If the SET_FU
56cb0 4c 4c 53 59 4e 43 20 6d 61 63 72 6f 20 69 73 20 LLSYNC macro is
56cc0 6e 6f 74 20 64 65 66 69 6e 65 64 20 61 62 6f 76 not defined abov
56cd0 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20 69 74 0a e, then make it.
56ce0 2a 2a 20 61 20 6e 6f 2d 6f 70 0a 2a 2f 0a 23 69 ** a no-op.*/.#i
56cf0 66 6e 64 65 66 20 53 45 54 5f 46 55 4c 4c 53 59 fndef SET_FULLSY
56d00 4e 43 0a 23 20 64 65 66 69 6e 65 20 53 45 54 5f NC.# define SET_
56d10 46 55 4c 4c 53 59 4e 43 28 78 2c 79 29 0a 23 65 FULLSYNC(x,y).#e
56d20 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ndif../*.** The
56d30 64 65 66 61 75 6c 74 20 73 69 7a 65 20 6f 66 20 default size of
56d40 61 20 64 69 73 6b 20 73 65 63 74 6f 72 0a 2a 2f a disk sector.*/
56d50 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
56d60 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 DEFAULT_SECTOR_S
56d70 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c IZE.# define SQL
56d80 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 ITE_DEFAULT_SECT
56d90 4f 52 5f 53 49 5a 45 20 35 31 32 0a 23 65 6e 64 OR_SIZE 512.#end
56da0 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 6d 70 6f 72 if../*.** Tempor
56db0 61 72 79 20 66 69 6c 65 73 20 61 72 65 20 6e 61 ary files are na
56dc0 6d 65 64 20 73 74 61 72 74 69 6e 67 20 77 69 74 med starting wit
56dd0 68 20 74 68 69 73 20 70 72 65 66 69 78 20 66 6f h this prefix fo
56de0 6c 6c 6f 77 65 64 20 62 79 20 31 36 20 72 61 6e llowed by 16 ran
56df0 64 6f 6d 0a 2a 2a 20 61 6c 70 68 61 6e 75 6d 65 dom.** alphanume
56e00 72 69 63 20 63 68 61 72 61 63 74 65 72 73 2c 20 ric characters,
56e10 61 6e 64 20 6e 6f 20 66 69 6c 65 20 65 78 74 65 and no file exte
56e20 6e 73 69 6f 6e 2e 20 54 68 65 79 20 61 72 65 20 nsion. They are
56e30 73 74 6f 72 65 64 20 69 6e 20 74 68 65 0a 2a 2a stored in the.**
56e40 20 4f 53 27 73 20 73 74 61 6e 64 61 72 64 20 74 OS's standard t
56e50 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 64 69 emporary file di
56e60 72 65 63 74 6f 72 79 2c 20 61 6e 64 20 61 72 65 rectory, and are
56e70 20 64 65 6c 65 74 65 64 20 70 72 69 6f 72 20 74 deleted prior t
56e80 6f 20 65 78 69 74 2e 0a 2a 2a 20 49 66 20 73 71 o exit..** If sq
56e90 6c 69 74 65 20 69 73 20 62 65 69 6e 67 20 65 6d lite is being em
56ea0 62 65 64 64 65 64 20 69 6e 20 61 6e 6f 74 68 65 bedded in anothe
56eb0 72 20 70 72 6f 67 72 61 6d 2c 20 79 6f 75 20 6d r program, you m
56ec0 61 79 20 77 69 73 68 20 74 6f 20 63 68 61 6e 67 ay wish to chang
56ed0 65 20 74 68 65 0a 2a 2a 20 70 72 65 66 69 78 20 e the.** prefix
56ee0 74 6f 20 72 65 66 6c 65 63 74 20 79 6f 75 72 20 to reflect your
56ef0 70 72 6f 67 72 61 6d 27 73 20 6e 61 6d 65 2c 20 program's name,
56f00 73 6f 20 74 68 61 74 20 69 66 20 79 6f 75 72 20 so that if your
56f10 70 72 6f 67 72 61 6d 20 65 78 69 74 73 0a 2a 2a program exits.**
56f20 20 70 72 65 6d 61 74 75 72 65 6c 79 2c 20 6f 6c prematurely, ol
56f30 64 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 d temporary file
56f40 73 20 63 61 6e 20 62 65 20 65 61 73 69 6c 79 20 s can be easily
56f50 69 64 65 6e 74 69 66 69 65 64 2e 20 54 68 69 73 identified. This
56f60 20 63 61 6e 20 62 65 20 64 6f 6e 65 0a 2a 2a 20 can be done.**
56f70 75 73 69 6e 67 20 2d 44 53 51 4c 49 54 45 5f 54 using -DSQLITE_T
56f80 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 3d EMP_FILE_PREFIX=
56f90 6d 79 70 72 65 66 69 78 5f 20 6f 6e 20 74 68 65 myprefix_ on the
56fa0 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e compiler comman
56fb0 64 20 6c 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 32 30 d line..**.** 20
56fc0 30 36 2d 31 30 2d 33 31 3a 20 20 54 68 65 20 64 06-10-31: The d
56fd0 65 66 61 75 6c 74 20 70 72 65 66 69 78 20 75 73 efault prefix us
56fe0 65 64 20 74 6f 20 62 65 20 22 73 71 6c 69 74 65 ed to be "sqlite
56ff0 5f 22 2e 20 20 42 75 74 20 74 68 65 6e 0a 2a 2a _". But then.**
57000 20 4d 63 61 66 65 65 20 73 74 61 72 74 65 64 20 Mcafee started
57010 75 73 69 6e 67 20 53 51 4c 69 74 65 20 69 6e 20 using SQLite in
57020 74 68 65 69 72 20 61 6e 74 69 2d 76 69 72 75 73 their anti-virus
57030 20 70 72 6f 64 75 63 74 20 61 6e 64 20 69 74 0a product and it.
57040 2a 2a 20 73 74 61 72 74 65 64 20 70 75 74 74 69 ** started putti
57050 6e 67 20 66 69 6c 65 73 20 77 69 74 68 20 74 68 ng files with th
57060 65 20 22 73 71 6c 69 74 65 22 20 6e 61 6d 65 20 e "sqlite" name
57070 69 6e 20 74 68 65 20 63 3a 2f 74 65 6d 70 20 66 in the c:/temp f
57080 6f 6c 64 65 72 2e 0a 2a 2a 20 54 68 69 73 20 61 older..** This a
57090 6e 6e 6f 79 65 64 20 6d 61 6e 79 20 77 69 6e 64 nnoyed many wind
570a0 6f 77 73 20 75 73 65 72 73 2e 20 20 54 68 6f 73 ows users. Thos
570b0 65 20 75 73 65 72 73 20 77 6f 75 6c 64 20 74 68 e users would th
570c0 65 6e 20 64 6f 20 61 20 0a 2a 2a 20 47 6f 6f 67 en do a .** Goog
570d0 6c 65 20 73 65 61 72 63 68 20 66 6f 72 20 22 73 le search for "s
570e0 71 6c 69 74 65 22 2c 20 66 69 6e 64 20 74 68 65 qlite", find the
570f0 20 74 65 6c 65 70 68 6f 6e 65 20 6e 75 6d 62 65 telephone numbe
57100 72 73 20 6f 66 20 74 68 65 0a 2a 2a 20 64 65 76 rs of the.** dev
57110 65 6c 6f 70 65 72 73 20 61 6e 64 20 63 61 6c 6c elopers and call
57120 20 74 6f 20 77 61 6b 65 20 74 68 65 6d 20 75 70 to wake them up
57130 20 61 74 20 6e 69 67 68 74 20 61 6e 64 20 63 6f at night and co
57140 6d 70 6c 61 69 6e 2e 0a 2a 2a 20 46 6f 72 20 74 mplain..** For t
57150 68 69 73 20 72 65 61 73 6f 6e 2c 20 74 68 65 20 his reason, the
57160 64 65 66 61 75 6c 74 20 6e 61 6d 65 20 70 72 65 default name pre
57170 66 69 78 20 69 73 20 63 68 61 6e 67 65 64 20 74 fix is changed t
57180 6f 20 62 65 20 22 73 71 6c 69 74 65 22 20 0a 2a o be "sqlite" .*
57190 2a 20 73 70 65 6c 6c 65 64 20 62 61 63 6b 77 61 * spelled backwa
571a0 72 64 73 2e 20 20 53 6f 20 74 68 65 20 74 65 6d rds. So the tem
571b0 70 20 66 69 6c 65 73 20 61 72 65 20 73 74 69 6c p files are stil
571c0 6c 20 69 64 65 6e 74 69 66 69 65 64 2c 20 62 75 l identified, bu
571d0 74 0a 2a 2a 20 61 6e 79 62 6f 64 79 20 73 6d 61 t.** anybody sma
571e0 72 74 20 65 6e 6f 75 67 68 20 74 6f 20 66 69 67 rt enough to fig
571f0 75 72 65 20 6f 75 74 20 74 68 65 20 63 6f 64 65 ure out the code
57200 20 69 73 20 61 6c 73 6f 20 6c 69 6b 65 6c 79 20 is also likely
57210 73 6d 61 72 74 0a 2a 2a 20 65 6e 6f 75 67 68 20 smart.** enough
57220 74 6f 20 6b 6e 6f 77 20 74 68 61 74 20 63 61 6c to know that cal
57230 6c 69 6e 67 20 74 68 65 20 64 65 76 65 6c 6f 70 ling the develop
57240 65 72 20 77 69 6c 6c 20 6e 6f 74 20 68 65 6c 70 er will not help
57250 20 67 65 74 20 72 69 64 0a 2a 2a 20 6f 66 20 74 get rid.** of t
57260 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66 6e he file..*/.#ifn
57270 64 65 66 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f def SQLITE_TEMP_
57280 46 49 4c 45 5f 50 52 45 46 49 58 0a 23 20 64 65 FILE_PREFIX.# de
57290 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 fine SQLITE_TEMP
572a0 5f 46 49 4c 45 5f 50 52 45 46 49 58 20 22 65 74 _FILE_PREFIX "et
572b0 69 6c 71 73 5f 22 0a 23 65 6e 64 69 66 0a 0a 2f ilqs_".#endif../
572c0 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
572d0 6e 67 20 76 61 6c 75 65 73 20 6d 61 79 20 62 65 ng values may be
572e0 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 passed as the s
572f0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 econd argument t
57300 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73 4c 6f o.** sqlite3OsLo
57310 63 6b 28 29 2e 20 54 68 65 20 76 61 72 69 6f 75 ck(). The variou
57320 73 20 6c 6f 63 6b 73 20 65 78 68 69 62 69 74 20 s locks exhibit
57330 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 the following se
57340 6d 61 6e 74 69 63 73 3a 0a 2a 2a 0a 2a 2a 20 53 mantics:.**.** S
57350 48 41 52 45 44 3a 20 20 20 20 41 6e 79 20 6e 75 HARED: Any nu
57360 6d 62 65 72 20 6f 66 20 70 72 6f 63 65 73 73 65 mber of processe
57370 73 20 6d 61 79 20 68 6f 6c 64 20 61 20 53 48 41 s may hold a SHA
57380 52 45 44 20 6c 6f 63 6b 20 73 69 6d 75 6c 74 61 RED lock simulta
57390 6e 65 6f 75 73 6c 79 2e 0a 2a 2a 20 52 45 53 45 neously..** RESE
573a0 52 56 45 44 3a 20 20 41 20 73 69 6e 67 6c 65 20 RVED: A single
573b0 70 72 6f 63 65 73 73 20 6d 61 79 20 68 6f 6c 64 process may hold
573c0 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b a RESERVED lock
573d0 20 6f 6e 20 61 20 66 69 6c 65 20 61 74 0a 2a 2a on a file at.**
573e0 20 20 20 20 20 20 20 20 20 20 20 20 61 6e 79 20 any
573f0 74 69 6d 65 2e 20 4f 74 68 65 72 20 70 72 6f 63 time. Other proc
57400 65 73 73 65 73 20 6d 61 79 20 68 6f 6c 64 20 61 esses may hold a
57410 6e 64 20 6f 62 74 61 69 6e 20 6e 65 77 20 53 48 nd obtain new SH
57420 41 52 45 44 20 6c 6f 63 6b 73 2e 0a 2a 2a 20 50 ARED locks..** P
57430 45 4e 44 49 4e 47 3a 20 20 20 41 20 73 69 6e 67 ENDING: A sing
57440 6c 65 20 70 72 6f 63 65 73 73 20 6d 61 79 20 68 le process may h
57450 6f 6c 64 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f old a PENDING lo
57460 63 6b 20 6f 6e 20 61 20 66 69 6c 65 20 61 74 0a ck on a file at.
57470 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 61 6e ** an
57480 79 20 6f 6e 65 20 74 69 6d 65 2e 20 45 78 69 73 y one time. Exis
57490 74 69 6e 67 20 53 48 41 52 45 44 20 6c 6f 63 6b ting SHARED lock
574a0 73 20 6d 61 79 20 70 65 72 73 69 73 74 2c 20 62 s may persist, b
574b0 75 74 20 6e 6f 20 6e 65 77 0a 2a 2a 20 20 20 20 ut no new.**
574c0 20 20 20 20 20 20 20 20 53 48 41 52 45 44 20 6c SHARED l
574d0 6f 63 6b 73 20 6d 61 79 20 62 65 20 6f 62 74 61 ocks may be obta
574e0 69 6e 65 64 20 62 79 20 6f 74 68 65 72 20 70 72 ined by other pr
574f0 6f 63 65 73 73 65 73 2e 0a 2a 2a 20 45 58 43 4c ocesses..** EXCL
57500 55 53 49 56 45 3a 20 41 6e 20 45 58 43 4c 55 53 USIVE: An EXCLUS
57510 49 56 45 20 6c 6f 63 6b 20 70 72 65 63 6c 75 64 IVE lock preclud
57520 65 73 20 61 6c 6c 20 6f 74 68 65 72 20 6c 6f 63 es all other loc
57530 6b 73 2e 0a 2a 2a 0a 2a 2a 20 50 45 4e 44 49 4e ks..**.** PENDIN
57540 47 5f 4c 4f 43 4b 20 6d 61 79 20 6e 6f 74 20 62 G_LOCK may not b
57550 65 20 70 61 73 73 65 64 20 64 69 72 65 63 74 6c e passed directl
57560 79 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 4c 6f y to sqlite3OsLo
57570 63 6b 28 29 2e 20 49 6e 73 74 65 61 64 2c 20 61 ck(). Instead, a
57580 0a 2a 2a 20 70 72 6f 63 65 73 73 20 74 68 61 74 .** process that
57590 20 72 65 71 75 65 73 74 73 20 61 6e 20 45 58 43 requests an EXC
575a0 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6d 61 79 20 LUSIVE lock may
575b0 61 63 74 75 61 6c 6c 79 20 6f 62 74 61 69 6e 20 actually obtain
575c0 61 20 50 45 4e 44 49 4e 47 0a 2a 2a 20 6c 6f 63 a PENDING.** loc
575d0 6b 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 k. This can be u
575e0 70 67 72 61 64 65 64 20 74 6f 20 61 6e 20 45 58 pgraded to an EX
575f0 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 62 79 20 CLUSIVE lock by
57600 61 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c a subsequent cal
57610 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 4f l to.** sqlite3O
57620 73 4c 6f 63 6b 28 29 2e 0a 2a 2f 0a 23 64 65 66 sLock()..*/.#def
57630 69 6e 65 20 4e 4f 5f 4c 4f 43 4b 20 20 20 20 20 ine NO_LOCK
57640 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 48 0.#define SH
57650 41 52 45 44 5f 4c 4f 43 4b 20 20 20 20 20 31 0a ARED_LOCK 1.
57660 23 64 65 66 69 6e 65 20 52 45 53 45 52 56 45 44 #define RESERVED
57670 5f 4c 4f 43 4b 20 20 20 32 0a 23 64 65 66 69 6e _LOCK 2.#defin
57680 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 20 e PENDING_LOCK
57690 20 20 33 0a 23 64 65 66 69 6e 65 20 45 58 43 4c 3.#define EXCL
576a0 55 53 49 56 45 5f 4c 4f 43 4b 20 20 34 0a 0a 2f USIVE_LOCK 4../
576b0 2a 0a 2a 2a 20 46 69 6c 65 20 4c 6f 63 6b 69 6e *.** File Lockin
576c0 67 20 4e 6f 74 65 73 3a 20 20 28 4d 6f 73 74 6c g Notes: (Mostl
576d0 79 20 61 62 6f 75 74 20 77 69 6e 64 6f 77 73 20 y about windows
576e0 62 75 74 20 61 6c 73 6f 20 73 6f 6d 65 20 69 6e but also some in
576f0 66 6f 20 66 6f 72 20 55 6e 69 78 29 0a 2a 2a 0a fo for Unix).**.
57700 2a 2a 20 57 65 20 63 61 6e 6e 6f 74 20 75 73 65 ** We cannot use
57710 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 6f 72 LockFileEx() or
57720 20 55 6e 6c 6f 63 6b 46 69 6c 65 45 78 28 29 20 UnlockFileEx()
57730 6f 6e 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20 62 on Win95/98/ME b
57740 65 63 61 75 73 65 0a 2a 2a 20 74 68 6f 73 65 20 ecause.** those
57750 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 6e 6f functions are no
57760 74 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 53 6f t available. So
57770 20 77 65 20 75 73 65 20 6f 6e 6c 79 20 4c 6f 63 we use only Loc
57780 6b 46 69 6c 65 28 29 20 61 6e 64 0a 2a 2a 20 55 kFile() and.** U
57790 6e 6c 6f 63 6b 46 69 6c 65 28 29 2e 0a 2a 2a 0a nlockFile()..**.
577a0 2a 2a 20 4c 6f 63 6b 46 69 6c 65 28 29 20 70 72 ** LockFile() pr
577b0 65 76 65 6e 74 73 20 6e 6f 74 20 6a 75 73 74 20 events not just
577c0 77 72 69 74 69 6e 67 20 62 75 74 20 61 6c 73 6f writing but also
577d0 20 72 65 61 64 69 6e 67 20 62 79 20 6f 74 68 65 reading by othe
577e0 72 20 70 72 6f 63 65 73 73 65 73 2e 0a 2a 2a 20 r processes..**
577f0 41 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 69 73 A SHARED_LOCK is
57800 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f 63 obtained by loc
57810 6b 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 72 61 king a single ra
57820 6e 64 6f 6d 6c 79 2d 63 68 6f 73 65 6e 20 0a 2a ndomly-chosen .*
57830 2a 20 62 79 74 65 20 6f 75 74 20 6f 66 20 61 20 * byte out of a
57840 73 70 65 63 69 66 69 63 20 72 61 6e 67 65 20 6f specific range o
57850 66 20 62 79 74 65 73 2e 20 54 68 65 20 6c 6f 63 f bytes. The loc
57860 6b 20 62 79 74 65 20 69 73 20 6f 62 74 61 69 6e k byte is obtain
57870 65 64 20 61 74 20 0a 2a 2a 20 72 61 6e 64 6f 6d ed at .** random
57880 20 73 6f 20 74 77 6f 20 73 65 70 61 72 61 74 65 so two separate
57890 20 72 65 61 64 65 72 73 20 63 61 6e 20 70 72 6f readers can pro
578a0 62 61 62 6c 79 20 61 63 63 65 73 73 20 74 68 65 bably access the
578b0 20 66 69 6c 65 20 61 74 20 74 68 65 20 0a 2a 2a file at the .**
578c0 20 73 61 6d 65 20 74 69 6d 65 2c 20 75 6e 6c 65 same time, unle
578d0 73 73 20 74 68 65 79 20 61 72 65 20 75 6e 6c 75 ss they are unlu
578e0 63 6b 79 20 61 6e 64 20 63 68 6f 6f 73 65 20 74 cky and choose t
578f0 68 65 20 73 61 6d 65 20 6c 6f 63 6b 20 62 79 74 he same lock byt
57900 65 2e 0a 2a 2a 20 41 6e 20 45 58 43 4c 55 53 49 e..** An EXCLUSI
57910 56 45 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 61 69 VE_LOCK is obtai
57920 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67 20 61 ned by locking a
57930 6c 6c 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 ll bytes in the
57940 72 61 6e 67 65 2e 0a 2a 2a 20 54 68 65 72 65 20 range..** There
57950 63 61 6e 20 6f 6e 6c 79 20 62 65 20 6f 6e 65 20 can only be one
57960 77 72 69 74 65 72 2e 20 20 41 20 52 45 53 45 52 writer. A RESER
57970 56 45 44 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 61 VED_LOCK is obta
57980 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67 0a ined by locking.
57990 2a 2a 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 ** a single byte
579a0 20 6f 66 20 74 68 65 20 66 69 6c 65 20 74 68 61 of the file tha
579b0 74 20 69 73 20 64 65 73 69 67 6e 61 74 65 64 20 t is designated
579c0 61 73 20 74 68 65 20 72 65 73 65 72 76 65 64 20 as the reserved
579d0 6c 6f 63 6b 20 62 79 74 65 2e 0a 2a 2a 20 41 20 lock byte..** A
579e0 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 69 73 20 PENDING_LOCK is
579f0 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f 63 6b obtained by lock
57a00 69 6e 67 20 61 20 64 65 73 69 67 6e 61 74 65 64 ing a designated
57a10 20 62 79 74 65 20 64 69 66 66 65 72 65 6e 74 20 byte different
57a20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 52 45 53 45 from.** the RESE
57a30 52 56 45 44 5f 4c 4f 43 4b 20 62 79 74 65 2e 0a RVED_LOCK byte..
57a40 2a 2a 0a 2a 2a 20 4f 6e 20 57 69 6e 4e 54 2f 32 **.** On WinNT/2
57a50 4b 2f 58 50 20 73 79 73 74 65 6d 73 2c 20 4c 6f K/XP systems, Lo
57a60 63 6b 46 69 6c 65 45 78 28 29 20 61 6e 64 20 55 ckFileEx() and U
57a70 6e 6c 6f 63 6b 46 69 6c 65 45 78 28 29 20 61 72 nlockFileEx() ar
57a80 65 20 61 76 61 69 6c 61 62 6c 65 2c 0a 2a 2a 20 e available,.**
57a90 77 68 69 63 68 20 6d 65 61 6e 73 20 77 65 20 63 which means we c
57aa0 61 6e 20 75 73 65 20 72 65 61 64 65 72 2f 77 72 an use reader/wr
57ab0 69 74 65 72 20 6c 6f 63 6b 73 2e 20 20 57 68 65 iter locks. Whe
57ac0 6e 20 72 65 61 64 65 72 2f 77 72 69 74 65 72 20 n reader/writer
57ad0 6c 6f 63 6b 73 0a 2a 2a 20 61 72 65 20 75 73 65 locks.** are use
57ae0 64 2c 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 70 d, the lock is p
57af0 6c 61 63 65 64 20 6f 6e 20 74 68 65 20 73 61 6d laced on the sam
57b00 65 20 72 61 6e 67 65 20 6f 66 20 62 79 74 65 73 e range of bytes
57b10 20 74 68 61 74 20 69 73 20 75 73 65 64 0a 2a 2a that is used.**
57b20 20 66 6f 72 20 70 72 6f 62 61 62 69 6c 69 73 74 for probabilist
57b30 69 63 20 6c 6f 63 6b 69 6e 67 20 69 6e 20 57 69 ic locking in Wi
57b40 6e 39 35 2f 39 38 2f 4d 45 2e 20 20 48 65 6e 63 n95/98/ME. Henc
57b50 65 2c 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 e, the locking s
57b60 63 68 65 6d 65 0a 2a 2a 20 77 69 6c 6c 20 73 75 cheme.** will su
57b70 70 70 6f 72 74 20 74 77 6f 20 6f 72 20 6d 6f 72 pport two or mor
57b80 65 20 57 69 6e 39 35 20 72 65 61 64 65 72 73 20 e Win95 readers
57b90 6f 72 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 57 or two or more W
57ba0 69 6e 4e 54 20 72 65 61 64 65 72 73 2e 0a 2a 2a inNT readers..**
57bb0 20 42 75 74 20 61 20 73 69 6e 67 6c 65 20 57 69 But a single Wi
57bc0 6e 39 35 20 72 65 61 64 65 72 20 77 69 6c 6c 20 n95 reader will
57bd0 6c 6f 63 6b 20 6f 75 74 20 61 6c 6c 20 57 69 6e lock out all Win
57be0 4e 54 20 72 65 61 64 65 72 73 20 61 6e 64 20 61 NT readers and a
57bf0 20 73 69 6e 67 6c 65 0a 2a 2a 20 57 69 6e 4e 54 single.** WinNT
57c00 20 72 65 61 64 65 72 20 77 69 6c 6c 20 6c 6f 63 reader will loc
57c10 6b 20 6f 75 74 20 61 6c 6c 20 6f 74 68 65 72 20 k out all other
57c20 57 69 6e 39 35 20 72 65 61 64 65 72 73 2e 0a 2a Win95 readers..*
57c30 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
57c40 6e 67 20 23 64 65 66 69 6e 65 73 20 73 70 65 63 ng #defines spec
57c50 69 66 79 20 74 68 65 20 72 61 6e 67 65 20 6f 66 ify the range of
57c60 20 62 79 74 65 73 20 75 73 65 64 20 66 6f 72 20 bytes used for
57c70 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 53 48 41 52 locking..** SHAR
57c80 45 44 5f 53 49 5a 45 20 69 73 20 74 68 65 20 6e ED_SIZE is the n
57c90 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 umber of bytes a
57ca0 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 vailable in the
57cb0 70 6f 6f 6c 20 66 72 6f 6d 20 77 68 69 63 68 0a pool from which.
57cc0 2a 2a 20 61 20 72 61 6e 64 6f 6d 20 62 79 74 65 ** a random byte
57cd0 20 69 73 20 73 65 6c 65 63 74 65 64 20 66 6f 72 is selected for
57ce0 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e 20 a shared lock.
57cf0 20 54 68 65 20 70 6f 6f 6c 20 6f 66 20 62 79 74 The pool of byt
57d00 65 73 20 66 6f 72 0a 2a 2a 20 73 68 61 72 65 64 es for.** shared
57d10 20 6c 6f 63 6b 73 20 62 65 67 69 6e 73 20 61 74 locks begins at
57d20 20 53 48 41 52 45 44 5f 46 49 52 53 54 2e 20 0a SHARED_FIRST. .
57d30 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 6d 65 20 6c **.** The same l
57d40 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79 20 ocking strategy
57d50 61 6e 64 0a 2a 2a 20 62 79 74 65 20 72 61 6e 67 and.** byte rang
57d60 65 73 20 61 72 65 20 75 73 65 64 20 66 6f 72 20 es are used for
57d70 55 6e 69 78 2e 20 20 54 68 69 73 20 6c 65 61 76 Unix. This leav
57d80 65 73 20 6f 70 65 6e 20 74 68 65 20 70 6f 73 73 es open the poss
57d90 69 62 6c 69 74 79 20 6f 66 20 68 61 76 69 6e 67 iblity of having
57da0 0a 2a 2a 20 63 6c 69 65 6e 74 73 20 6f 6e 20 77 .** clients on w
57db0 69 6e 39 35 2c 20 77 69 6e 4e 54 2c 20 61 6e 64 in95, winNT, and
57dc0 20 75 6e 69 78 20 61 6c 6c 20 74 61 6c 6b 69 6e unix all talkin
57dd0 67 20 74 6f 20 74 68 65 20 73 61 6d 65 20 73 68 g to the same sh
57de0 61 72 65 64 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 ared file.** and
57df0 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 63 6f 72 all locking cor
57e00 72 65 63 74 6c 79 2e 20 20 54 6f 20 64 6f 20 73 rectly. To do s
57e10 6f 20 77 6f 75 6c 64 20 72 65 71 75 69 72 65 20 o would require
57e20 74 68 61 74 20 73 61 6d 62 61 20 28 6f 72 20 77 that samba (or w
57e30 68 61 74 65 76 65 72 0a 2a 2a 20 74 6f 6f 6c 20 hatever.** tool
57e40 69 73 20 62 65 69 6e 67 20 75 73 65 64 20 66 6f is being used fo
57e50 72 20 66 69 6c 65 20 73 68 61 72 69 6e 67 29 20 r file sharing)
57e60 69 6d 70 6c 65 6d 65 6e 74 73 20 6c 6f 63 6b 73 implements locks
57e70 20 63 6f 72 72 65 63 74 6c 79 20 62 65 74 77 65 correctly betwe
57e80 65 6e 0a 2a 2a 20 77 69 6e 64 6f 77 73 20 61 6e en.** windows an
57e90 64 20 75 6e 69 78 2e 20 20 49 27 6d 20 67 75 65 d unix. I'm gue
57ea0 73 73 69 6e 67 20 74 68 61 74 20 69 73 6e 27 74 ssing that isn't
57eb0 20 6c 69 6b 65 6c 79 20 74 6f 20 68 61 70 70 65 likely to happe
57ec0 6e 2c 20 62 75 74 20 62 79 0a 2a 2a 20 75 73 69 n, but by.** usi
57ed0 6e 67 20 74 68 65 20 73 61 6d 65 20 6c 6f 63 6b ng the same lock
57ee0 69 6e 67 20 72 61 6e 67 65 20 77 65 20 61 72 65 ing range we are
57ef0 20 61 74 20 6c 65 61 73 74 20 6f 70 65 6e 20 74 at least open t
57f00 6f 20 74 68 65 20 70 6f 73 73 69 62 69 6c 69 74 o the possibilit
57f10 79 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 63 6b 69 6e 67 y..**.** Locking
57f20 20 69 6e 20 77 69 6e 64 6f 77 73 20 69 73 20 6d in windows is m
57f30 61 6e 64 69 74 6f 72 79 2e 20 20 46 6f 72 20 74 anditory. For t
57f40 68 69 73 20 72 65 61 73 6f 6e 2c 20 77 65 20 63 his reason, we c
57f50 61 6e 6e 6f 74 20 73 74 6f 72 65 0a 2a 2a 20 61 annot store.** a
57f60 63 74 75 61 6c 20 64 61 74 61 20 69 6e 20 74 68 ctual data in th
57f70 65 20 62 79 74 65 73 20 75 73 65 64 20 66 6f 72 e bytes used for
57f80 20 6c 6f 63 6b 69 6e 67 2e 20 20 54 68 65 20 70 locking. The p
57f90 61 67 65 72 20 6e 65 76 65 72 20 61 6c 6c 6f 63 ager never alloc
57fa0 61 74 65 73 0a 2a 2a 20 74 68 65 20 70 61 67 65 ates.** the page
57fb0 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 6c 6f s involved in lo
57fc0 63 6b 69 6e 67 20 74 68 65 72 65 66 6f 72 65 2e cking therefore.
57fd0 20 20 53 48 41 52 45 44 5f 53 49 5a 45 20 69 73 SHARED_SIZE is
57fe0 20 73 65 6c 65 63 74 65 64 20 73 6f 0a 2a 2a 20 selected so.**
57ff0 74 68 61 74 20 61 6c 6c 20 6c 6f 63 6b 73 20 77 that all locks w
58000 69 6c 6c 20 66 69 74 20 6f 6e 20 61 20 73 69 6e ill fit on a sin
58010 67 6c 65 20 70 61 67 65 20 65 76 65 6e 20 61 74 gle page even at
58020 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 70 61 67 the minimum pag
58030 65 20 73 69 7a 65 2e 0a 2a 2a 20 50 45 4e 44 49 e size..** PENDI
58040 4e 47 5f 42 59 54 45 20 64 65 66 69 6e 65 73 20 NG_BYTE defines
58050 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 the beginning of
58060 20 74 68 65 20 6c 6f 63 6b 73 2e 20 20 42 79 20 the locks. By
58070 64 65 66 61 75 6c 74 20 50 45 4e 44 49 4e 47 5f default PENDING_
58080 42 59 54 45 0a 2a 2a 20 69 73 20 73 65 74 20 68 BYTE.** is set h
58090 69 67 68 20 73 6f 20 74 68 61 74 20 77 65 20 64 igh so that we d
580a0 6f 6e 27 74 20 68 61 76 65 20 74 6f 20 61 6c 6c on't have to all
580b0 6f 63 61 74 65 20 61 6e 20 75 6e 75 73 65 64 20 ocate an unused
580c0 70 61 67 65 20 65 78 63 65 70 74 0a 2a 2a 20 66 page except.** f
580d0 6f 72 20 76 65 72 79 20 6c 61 72 67 65 20 64 61 or very large da
580e0 74 61 62 61 73 65 73 2e 20 20 42 75 74 20 6f 6e tabases. But on
580f0 65 20 73 68 6f 75 6c 64 20 74 65 73 74 20 74 68 e should test th
58100 65 20 70 61 67 65 20 73 6b 69 70 70 69 6e 67 20 e page skipping
58110 6c 6f 67 69 63 20 0a 2a 2a 20 62 79 20 73 65 74 logic .** by set
58120 74 69 6e 67 20 50 45 4e 44 49 4e 47 5f 42 59 54 ting PENDING_BYT
58130 45 20 6c 6f 77 20 61 6e 64 20 72 75 6e 6e 69 6e E low and runnin
58140 67 20 74 68 65 20 65 6e 74 69 72 65 20 72 65 67 g the entire reg
58150 72 65 73 73 69 6f 6e 20 73 75 69 74 65 2e 0a 2a ression suite..*
58160 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 68 *.** Changing th
58170 65 20 76 61 6c 75 65 20 6f 66 20 50 45 4e 44 49 e value of PENDI
58180 4e 47 5f 42 59 54 45 20 72 65 73 75 6c 74 73 20 NG_BYTE results
58190 69 6e 20 61 20 73 75 62 74 6c 79 20 69 6e 63 6f in a subtly inco
581a0 6d 70 61 74 69 62 6c 65 0a 2a 2a 20 66 69 6c 65 mpatible.** file
581b0 20 66 6f 72 6d 61 74 2e 20 20 44 65 70 65 6e 64 format. Depend
581c0 69 6e 67 20 6f 6e 20 68 6f 77 20 69 74 20 69 73 ing on how it is
581d0 20 63 68 61 6e 67 65 64 2c 20 79 6f 75 20 6d 69 changed, you mi
581e0 67 68 74 20 6e 6f 74 20 6e 6f 74 69 63 65 0a 2a ght not notice.*
581f0 2a 20 74 68 65 20 69 6e 63 6f 6d 70 61 74 69 62 * the incompatib
58200 69 6c 69 74 79 20 72 69 67 68 74 20 61 77 61 79 ility right away
58210 2c 20 65 76 65 6e 20 72 75 6e 6e 69 6e 67 20 61 , even running a
58220 20 66 75 6c 6c 20 72 65 67 72 65 73 73 69 6f 6e full regression
58230 20 74 65 73 74 2e 0a 2a 2a 20 54 68 65 20 64 65 test..** The de
58240 66 61 75 6c 74 20 6c 6f 63 61 74 69 6f 6e 20 6f fault location o
58250 66 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 69 f PENDING_BYTE i
58260 73 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 s the first byte
58270 20 70 61 73 74 20 74 68 65 0a 2a 2a 20 31 47 42 past the.** 1GB
58280 20 62 6f 75 6e 64 61 72 79 2e 0a 2a 2a 0a 2a 2f boundary..**.*/
58290 0a 23 64 65 66 69 6e 65 20 50 45 4e 44 49 4e 47 .#define PENDING
582a0 5f 42 59 54 45 20 20 20 20 20 20 73 71 6c 69 74 _BYTE sqlit
582b0 65 33 50 65 6e 64 69 6e 67 42 79 74 65 0a 23 64 e3PendingByte.#d
582c0 65 66 69 6e 65 20 52 45 53 45 52 56 45 44 5f 42 efine RESERVED_B
582d0 59 54 45 20 20 20 20 20 28 50 45 4e 44 49 4e 47 YTE (PENDING
582e0 5f 42 59 54 45 2b 31 29 0a 23 64 65 66 69 6e 65 _BYTE+1).#define
582f0 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 20 20 SHARED_FIRST
58300 20 20 20 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 (PENDING_BYTE
58310 2b 32 29 0a 23 64 65 66 69 6e 65 20 53 48 41 52 +2).#define SHAR
58320 45 44 5f 53 49 5a 45 20 20 20 20 20 20 20 35 31 ED_SIZE 51
58330 30 0a 0a 2f 2a 0a 2a 2a 20 57 72 61 70 70 65 72 0../*.** Wrapper
58340 20 61 72 6f 75 6e 64 20 4f 53 20 73 70 65 63 69 around OS speci
58350 66 69 63 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 fic sqlite3_os_i
58360 6e 69 74 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a nit() function..
58370 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
58380 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 49 E int sqlite3OsI
58390 6e 69 74 28 76 6f 69 64 29 3b 0a 0a 2f 2a 20 0a nit(void);../* .
583a0 2a 2a 20 46 75 6e 63 74 69 6f 6e 73 20 66 6f 72 ** Functions for
583b0 20 61 63 63 65 73 73 69 6e 67 20 73 71 6c 69 74 accessing sqlit
583c0 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 20 e3_file methods
583d0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
583e0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
583f0 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 Close(sqlite3_fi
58400 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 le*);.SQLITE_PRI
58410 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
58420 4f 73 52 65 61 64 28 73 71 6c 69 74 65 33 5f 66 OsRead(sqlite3_f
58430 69 6c 65 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e 74 ile*, void*, int
58440 20 61 6d 74 2c 20 69 36 34 20 6f 66 66 73 65 74 amt, i64 offset
58450 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
58460 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 57 E int sqlite3OsW
58470 72 69 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c rite(sqlite3_fil
58480 65 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c e*, const void*,
58490 20 69 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f 66 int amt, i64 of
584a0 66 73 65 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 fset);.SQLITE_PR
584b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
584c0 33 4f 73 54 72 75 6e 63 61 74 65 28 73 71 6c 69 3OsTruncate(sqli
584d0 74 65 33 5f 66 69 6c 65 2a 2c 20 69 36 34 20 73 te3_file*, i64 s
584e0 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ize);.SQLITE_PRI
584f0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
58500 4f 73 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 OsSync(sqlite3_f
58510 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 ile*, int);.SQLI
58520 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
58530 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 qlite3OsFileSize
58540 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 (sqlite3_file*,
58550 69 36 34 20 2a 70 53 69 7a 65 29 3b 0a 53 51 4c i64 *pSize);.SQL
58560 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
58570 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 73 71 sqlite3OsLock(sq
58580 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 lite3_file*, int
58590 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
585a0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 55 E int sqlite3OsU
585b0 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 nlock(sqlite3_fi
585c0 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 le*, int);.SQLIT
585d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
585e0 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 65 73 65 lite3OsCheckRese
585f0 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 rvedLock(sqlite3
58600 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a _file *id, int *
58610 70 52 65 73 4f 75 74 29 3b 0a 53 51 4c 49 54 45 pResOut);.SQLITE
58620 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
58630 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f ite3OsFileContro
58640 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c l(sqlite3_file*,
58650 69 6e 74 2c 76 6f 69 64 2a 29 3b 0a 23 64 65 66 int,void*);.#def
58660 69 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c ine SQLITE_FCNTL
58670 5f 44 42 5f 55 4e 43 48 41 4e 47 45 44 20 30 78 _DB_UNCHANGED 0x
58680 63 61 30 39 33 66 61 30 0a 53 51 4c 49 54 45 5f ca093fa0.SQLITE_
58690 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
586a0 74 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 te3OsSectorSize(
586b0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
586c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
586d0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 44 E int sqlite3OsD
586e0 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 eviceCharacteris
586f0 74 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c tics(sqlite3_fil
58700 65 20 2a 69 64 29 3b 0a 0a 2f 2a 20 0a 2a 2a 20 e *id);../* .**
58710 46 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 61 63 Functions for ac
58720 63 65 73 73 69 6e 67 20 73 71 6c 69 74 65 33 5f cessing sqlite3_
58730 76 66 73 20 6d 65 74 68 6f 64 73 20 0a 2a 2f 0a vfs methods .*/.
58740 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
58750 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e nt sqlite3OsOpen
58760 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 (sqlite3_vfs *,
58770 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 73 71 const char *, sq
58780 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 lite3_file*, int
58790 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 45 , int *);.SQLITE
587a0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
587b0 69 74 65 33 4f 73 44 65 6c 65 74 65 28 73 71 6c ite3OsDelete(sql
587c0 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 ite3_vfs *, cons
587d0 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b 0a t char *, int);.
587e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
587f0 6e 74 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 nt sqlite3OsAcce
58800 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a ss(sqlite3_vfs *
58810 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 , const char *,
58820 69 6e 74 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 int, int *pResOu
58830 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
58840 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
58850 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 73 71 6c FullPathname(sql
58860 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 ite3_vfs *, cons
58870 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 63 t char *, int, c
58880 68 61 72 20 2a 29 3b 0a 23 69 66 6e 64 65 66 20 har *);.#ifndef
58890 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 SQLITE_OMIT_LOAD
588a0 5f 45 58 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 _EXTENSION.SQLIT
588b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a E_PRIVATE void *
588c0 73 71 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28 sqlite3OsDlOpen(
588d0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 sqlite3_vfs *, c
588e0 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 onst char *);.SQ
588f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
58900 64 20 73 71 6c 69 74 65 33 4f 73 44 6c 45 72 72 d sqlite3OsDlErr
58910 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a or(sqlite3_vfs *
58920 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a 29 3b 0a , int, char *);.
58930 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
58940 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 4f 73 44 oid (*sqlite3OsD
58950 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 lSym(sqlite3_vfs
58960 20 2a 2c 20 76 6f 69 64 20 2a 2c 20 63 6f 6e 73 *, void *, cons
58970 74 20 63 68 61 72 20 2a 29 29 28 76 6f 69 64 29 t char *))(void)
58980 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
58990 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 73 44 void sqlite3OsD
589a0 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 lClose(sqlite3_v
589b0 66 73 20 2a 2c 20 76 6f 69 64 20 2a 29 3b 0a 23 fs *, void *);.#
589c0 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
589d0 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 OMIT_LOAD_EXTENS
589e0 49 4f 4e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ION */.SQLITE_PR
589f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
58a00 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 3OsRandomness(sq
58a10 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 69 6e 74 lite3_vfs *, int
58a20 2c 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 , char *);.SQLIT
58a30 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
58a40 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 73 71 6c lite3OsSleep(sql
58a50 69 74 65 33 5f 76 66 73 20 2a 2c 20 69 6e 74 29 ite3_vfs *, int)
58a60 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
58a70 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 75 int sqlite3OsCu
58a80 72 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74 65 rrentTime(sqlite
58a90 33 5f 76 66 73 20 2a 2c 20 64 6f 75 62 6c 65 2a 3_vfs *, double*
58aa0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 6e );../*.** Conven
58ab0 69 65 6e 63 65 20 66 75 6e 63 74 69 6f 6e 73 20 ience functions
58ac0 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 6e 64 20 for opening and
58ad0 63 6c 6f 73 69 6e 67 20 66 69 6c 65 73 20 75 73 closing files us
58ae0 69 6e 67 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f ing .** sqlite3_
58af0 6d 61 6c 6c 6f 63 28 29 20 74 6f 20 6f 62 74 61 malloc() to obta
58b00 69 6e 20 73 70 61 63 65 20 66 6f 72 20 74 68 65 in space for the
58b10 20 66 69 6c 65 2d 68 61 6e 64 6c 65 20 73 74 72 file-handle str
58b20 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 ucture..*/.SQLIT
58b30 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
58b40 6c 69 74 65 33 4f 73 4f 70 65 6e 4d 61 6c 6c 6f lite3OsOpenMallo
58b50 63 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c c(sqlite3_vfs *,
58b60 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 73 const char *, s
58b70 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2a 2c 20 qlite3_file **,
58b80 69 6e 74 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 int,int*);.SQLIT
58b90 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
58ba0 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 lite3OsCloseFree
58bb0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 (sqlite3_file *)
58bc0 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 53 51 ;..#endif /* _SQ
58bd0 4c 49 54 45 5f 4f 53 5f 48 5f 20 2a 2f 0a 0a 2f LITE_OS_H_ */../
58be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
58bf0 6e 64 20 6f 66 20 6f 73 2e 68 20 2a 2a 2a 2a 2a nd of os.h *****
58c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
58c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 ************** C
58c40 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 ontinuing where
58c50 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 we left off in s
58c60 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a qliteInt.h *****
58c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
58c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 ************** I
58c90 6e 63 6c 75 64 65 20 6d 75 74 65 78 2e 68 20 69 nclude mutex.h i
58ca0 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 n the middle of
58cb0 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a sqliteInt.h ****
58cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
58cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
58ce0 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 2e egin file mutex.
58cf0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
58d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
58d20 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 *.** 2007 August
58d30 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 28.**.** The au
58d40 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
58d50 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
58d60 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
58d70 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
58d80 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
58d90 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
58da0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
58db0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
58dc0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
58dd0 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
58de0 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
58df0 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
58e00 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
58e10 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
58e20 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
58e30 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
58e40 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
58e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58e90 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 **.**.** This fi
58ea0 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 le contains the
58eb0 63 6f 6d 6d 6f 6e 20 68 65 61 64 65 72 20 66 6f common header fo
58ec0 72 20 61 6c 6c 20 6d 75 74 65 78 20 69 6d 70 6c r all mutex impl
58ed0 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 20 ementations..**
58ee0 54 68 65 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 The sqliteInt.h
58ef0 68 65 61 64 65 72 20 23 69 6e 63 6c 75 64 65 73 header #includes
58f00 20 74 68 69 73 20 66 69 6c 65 20 73 6f 20 74 68 this file so th
58f10 61 74 20 69 74 20 69 73 20 61 76 61 69 6c 61 62 at it is availab
58f20 6c 65 0a 2a 2a 20 74 6f 20 61 6c 6c 20 73 6f 75 le.** to all sou
58f30 72 63 65 20 66 69 6c 65 73 2e 20 20 57 65 20 62 rce files. We b
58f40 72 65 61 6b 20 69 74 20 6f 75 74 20 69 6e 20 61 reak it out in a
58f50 6e 20 65 66 66 6f 72 74 20 74 6f 20 6b 65 65 70 n effort to keep
58f60 20 74 68 65 20 63 6f 64 65 0a 2a 2a 20 62 65 74 the code.** bet
58f70 74 65 72 20 6f 72 67 61 6e 69 7a 65 64 2e 0a 2a ter organized..*
58f80 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 20 73 6f 75 72 *.** NOTE: sour
58f90 63 65 20 66 69 6c 65 73 20 73 68 6f 75 6c 64 20 ce files should
58fa0 2a 6e 6f 74 2a 20 23 69 6e 63 6c 75 64 65 20 74 *not* #include t
58fb0 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 his header file
58fc0 64 69 72 65 63 74 6c 79 2e 0a 2a 2a 20 53 6f 75 directly..** Sou
58fd0 72 63 65 20 66 69 6c 65 73 20 73 68 6f 75 6c 64 rce files should
58fe0 20 23 69 6e 63 6c 75 64 65 20 74 68 65 20 73 71 #include the sq
58ff0 6c 69 74 65 49 6e 74 2e 68 20 66 69 6c 65 20 61 liteInt.h file a
59000 6e 64 20 6c 65 74 20 74 68 61 74 20 66 69 6c 65 nd let that file
59010 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 74 68 69 73 .** include this
59020 20 6f 6e 65 20 69 6e 64 69 72 65 63 74 6c 79 2e one indirectly.
59030 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65 .**.** $Id: mute
59040 78 2e 68 2c 76 20 31 2e 39 20 32 30 30 38 2f 31 x.h,v 1.9 2008/1
59050 30 2f 30 37 20 31 35 3a 32 35 3a 34 38 20 64 72 0/07 15:25:48 dr
59060 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a h Exp $.*/.../*.
59070 2a 2a 20 46 69 67 75 72 65 20 6f 75 74 20 77 68 ** Figure out wh
59080 61 74 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 at version of th
59090 65 20 63 6f 64 65 20 74 6f 20 75 73 65 2e 20 20 e code to use.
590a0 54 68 65 20 63 68 6f 69 63 65 73 20 61 72 65 0a The choices are.
590b0 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d **.** SQLITE_M
590c0 55 54 45 58 5f 4f 4d 49 54 20 20 20 20 20 20 20 UTEX_OMIT
590d0 20 20 4e 6f 20 6d 75 74 65 78 20 6c 6f 67 69 63 No mutex logic
590e0 2e 20 20 4e 6f 74 20 65 76 65 6e 20 73 74 75 62 . Not even stub
590f0 73 2e 20 20 54 68 65 0a 2a 2a 20 20 20 20 20 20 s. The.**
59100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59110 20 20 20 20 20 20 20 6d 75 74 65 78 65 73 20 69 mutexes i
59120 6d 70 6c 65 6d 65 6e 74 69 6f 6e 20 63 61 6e 6e mplemention cann
59130 6f 74 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e ot be overridden
59140 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
59150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59160 61 74 20 73 74 61 72 74 2d 74 69 6d 65 2e 0a 2a at start-time..*
59170 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 *.** SQLITE_MU
59180 54 45 58 5f 4e 4f 4f 50 20 20 20 20 20 20 20 20 TEX_NOOP
59190 20 46 6f 72 20 73 69 6e 67 6c 65 2d 74 68 72 65 For single-thre
591a0 61 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e aded application
591b0 73 2e 20 20 4e 6f 0a 2a 2a 20 20 20 20 20 20 20 s. No.**
591c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
591d0 20 20 20 20 20 20 6d 75 74 75 61 6c 20 65 78 63 mutual exc
591e0 6c 75 73 69 6f 6e 20 69 73 20 70 72 6f 76 69 64 lusion is provid
591f0 65 64 2e 20 20 42 75 74 20 74 68 69 73 0a 2a 2a ed. But this.**
59200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59210 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6d 70 imp
59220 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 63 61 6e 20 lementation can
59230 62 65 20 6f 76 65 72 72 69 64 64 65 6e 20 61 74 be overridden at
59240 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
59250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59260 73 74 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a 0a 2a start-time..**.*
59270 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 * SQLITE_MUTEX
59280 5f 50 54 48 52 45 41 44 53 20 20 20 20 20 46 6f _PTHREADS Fo
59290 72 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 64 r multi-threaded
592a0 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 6f 6e applications on
592b0 20 55 6e 69 78 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 Unix..**.** S
592c0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 32 20 QLITE_MUTEX_W32
592d0 20 20 20 20 20 20 20 20 20 46 6f 72 20 6d 75 6c For mul
592e0 74 69 2d 74 68 72 65 61 64 65 64 20 61 70 70 6c ti-threaded appl
592f0 69 63 61 74 69 6f 6e 73 20 6f 6e 20 57 69 6e 33 ications on Win3
59300 32 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 2..**.** SQLIT
59310 45 5f 4d 55 54 45 58 5f 4f 53 32 20 20 20 20 20 E_MUTEX_OS2
59320 20 20 20 20 20 46 6f 72 20 6d 75 6c 74 69 2d 74 For multi-t
59330 68 72 65 61 64 65 64 20 61 70 70 6c 69 63 61 74 hreaded applicat
59340 69 6f 6e 73 20 6f 6e 20 4f 53 2f 32 2e 0a 2a 2f ions on OS/2..*/
59350 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 54 48 52 .#if !SQLITE_THR
59360 45 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e 65 EADSAFE.# define
59370 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d SQLITE_MUTEX_OM
59380 49 54 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 IT.#endif.#if SQ
59390 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 LITE_THREADSAFE
593a0 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 && !defined(SQLI
593b0 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 0a 23 TE_MUTEX_NOOP).#
593c0 20 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 55 if SQLITE_OS_U
593d0 4e 49 58 0a 23 20 20 20 20 64 65 66 69 6e 65 20 NIX.# define
593e0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 SQLITE_MUTEX_PTH
593f0 52 45 41 44 53 0a 23 20 20 65 6c 69 66 20 53 51 READS.# elif SQ
59400 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 20 LITE_OS_WIN.#
59410 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d define SQLITE_M
59420 55 54 45 58 5f 57 33 32 0a 23 20 20 65 6c 69 66 UTEX_W32.# elif
59430 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a 23 SQLITE_OS_OS2.#
59440 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 define SQLIT
59450 45 5f 4d 55 54 45 58 5f 4f 53 32 0a 23 20 20 65 E_MUTEX_OS2.# e
59460 6c 73 65 0a 23 20 20 20 20 64 65 66 69 6e 65 20 lse.# define
59470 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f SQLITE_MUTEX_NOO
59480 50 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 P.# endif.#endi
59490 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 f..#ifdef SQLITE
594a0 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 2f 2a 0a 2a _MUTEX_OMIT./*.*
594b0 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 6e * If this is a n
594c0 6f 2d 6f 70 20 69 6d 70 6c 65 6d 65 6e 74 61 74 o-op implementat
594d0 69 6f 6e 2c 20 69 6d 70 6c 65 6d 65 6e 74 20 65 ion, implement e
594e0 76 65 72 79 74 68 69 6e 67 20 61 73 20 6d 61 63 verything as mac
594f0 72 6f 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ros..*/.#define
59500 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c sqlite3_mutex_al
59510 6c 6f 63 28 58 29 20 20 20 20 28 28 73 71 6c 69 loc(X) ((sqli
59520 74 65 33 5f 6d 75 74 65 78 2a 29 38 29 0a 23 64 te3_mutex*)8).#d
59530 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 efine sqlite3_mu
59540 74 65 78 5f 66 72 65 65 28 58 29 0a 23 64 65 66 tex_free(X).#def
59550 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ine sqlite3_mute
59560 78 5f 65 6e 74 65 72 28 58 29 0a 23 64 65 66 69 x_enter(X).#defi
59570 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ne sqlite3_mutex
59580 5f 74 72 79 28 58 29 20 20 20 20 20 20 53 51 4c _try(X) SQL
59590 49 54 45 5f 4f 4b 0a 23 64 65 66 69 6e 65 20 73 ITE_OK.#define s
595a0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
595b0 76 65 28 58 29 0a 23 64 65 66 69 6e 65 20 73 71 ve(X).#define sq
595c0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
595d0 28 58 29 20 20 20 20 20 31 0a 23 64 65 66 69 6e (X) 1.#defin
595e0 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f e sqlite3_mutex_
595f0 6e 6f 74 68 65 6c 64 28 58 29 20 20 31 0a 23 64 notheld(X) 1.#d
59600 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4d 75 74 efine sqlite3Mut
59610 65 78 41 6c 6c 6f 63 28 58 29 20 20 20 20 20 20 exAlloc(X)
59620 28 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a ((sqlite3_mutex*
59630 29 38 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 )8).#define sqli
59640 74 65 33 4d 75 74 65 78 49 6e 69 74 28 29 20 20 te3MutexInit()
59650 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 0a SQLITE_OK.
59660 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4d #define sqlite3M
59670 75 74 65 78 45 6e 64 28 29 0a 23 65 6e 64 69 66 utexEnd().#endif
59680 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49 /* defined(SQLI
59690 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 29 20 2a TE_MUTEX_OMIT) *
596a0 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /../************
596b0 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 2e ** End of mutex.
596c0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
596d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
596e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
596f0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
59700 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 ** Continuing wh
59710 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 ere we left off
59720 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a in sqliteInt.h *
59730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
59740 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 */.../*.** Each
59750 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f database file to
59760 20 62 65 20 61 63 63 65 73 73 65 64 20 62 79 20 be accessed by
59770 74 68 65 20 73 79 73 74 65 6d 20 69 73 20 61 6e the system is an
59780 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 instance.** of
59790 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
597a0 72 75 63 74 75 72 65 2e 20 20 54 68 65 72 65 20 ructure. There
597b0 61 72 65 20 6e 6f 72 6d 61 6c 6c 79 20 74 77 6f are normally two
597c0 20 6f 66 20 74 68 65 73 65 20 73 74 72 75 63 74 of these struct
597d0 75 72 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73 ures.** in the s
597e0 71 6c 69 74 65 2e 61 44 62 5b 5d 20 61 72 72 61 qlite.aDb[] arra
597f0 79 2e 20 20 61 44 62 5b 30 5d 20 69 73 20 74 68 y. aDb[0] is th
59800 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 e main database
59810 66 69 6c 65 20 61 6e 64 0a 2a 2a 20 61 44 62 5b file and.** aDb[
59820 31 5d 20 69 73 20 74 68 65 20 64 61 74 61 62 61 1] is the databa
59830 73 65 20 66 69 6c 65 20 75 73 65 64 20 74 6f 20 se file used to
59840 68 6f 6c 64 20 74 65 6d 70 6f 72 61 72 79 20 74 hold temporary t
59850 61 62 6c 65 73 2e 20 20 41 64 64 69 74 69 6f 6e ables. Addition
59860 61 6c 0a 2a 2a 20 64 61 74 61 62 61 73 65 73 20 al.** databases
59870 6d 61 79 20 62 65 20 61 74 74 61 63 68 65 64 2e may be attached.
59880 0a 2a 2f 0a 73 74 72 75 63 74 20 44 62 20 7b 0a .*/.struct Db {.
59890 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 char *zName;
598a0 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
598b0 66 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 f this database
598c0 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b */. Btree *pBt;
598d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
598e0 20 42 2a 54 72 65 65 20 73 74 72 75 63 74 75 72 B*Tree structur
598f0 65 20 66 6f 72 20 74 68 69 73 20 64 61 74 61 62 e for this datab
59900 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 ase file */. u8
59910 20 69 6e 54 72 61 6e 73 3b 20 20 20 20 20 20 20 inTrans;
59920 20 20 20 2f 2a 20 30 3a 20 6e 6f 74 20 77 72 69 /* 0: not wri
59930 74 61 62 6c 65 2e 20 20 31 3a 20 54 72 61 6e 73 table. 1: Trans
59940 61 63 74 69 6f 6e 2e 20 20 32 3a 20 43 68 65 63 action. 2: Chec
59950 6b 70 6f 69 6e 74 20 2a 2f 0a 20 20 75 38 20 73 kpoint */. u8 s
59960 61 66 65 74 79 5f 6c 65 76 65 6c 3b 20 20 20 20 afety_level;
59970 20 2f 2a 20 48 6f 77 20 61 67 67 72 65 73 73 69 /* How aggressi
59980 76 65 20 61 74 20 73 79 6e 63 69 6e 67 20 64 61 ve at syncing da
59990 74 61 20 74 6f 20 64 69 73 6b 20 2a 2f 0a 20 20 ta to disk */.
599a0 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b Schema *pSchema;
599b0 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
599c0 74 6f 20 64 61 74 61 62 61 73 65 20 73 63 68 65 to database sche
599d0 6d 61 20 28 70 6f 73 73 69 62 6c 79 20 73 68 61 ma (possibly sha
599e0 72 65 64 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a red) */.};../*.*
599f0 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 * An instance of
59a00 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
59a10 74 72 75 63 74 75 72 65 20 73 74 6f 72 65 73 20 tructure stores
59a20 61 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d a database schem
59a30 61 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 a..**.** If ther
59a40 65 20 61 72 65 20 6e 6f 20 76 69 72 74 75 61 6c e are no virtual
59a50 20 74 61 62 6c 65 73 20 63 6f 6e 66 69 67 75 72 tables configur
59a60 65 64 20 69 6e 20 74 68 69 73 20 73 63 68 65 6d ed in this schem
59a70 61 2c 20 74 68 65 0a 2a 2a 20 53 63 68 65 6d 61 a, the.** Schema
59a80 2e 64 62 20 76 61 72 69 61 62 6c 65 20 69 73 20 .db variable is
59a90 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 20 41 66 74 set to NULL. Aft
59aa0 65 72 20 74 68 65 20 66 69 72 73 74 20 76 69 72 er the first vir
59ab0 74 75 61 6c 20 74 61 62 6c 65 0a 2a 2a 20 68 61 tual table.** ha
59ac0 73 20 62 65 65 6e 20 61 64 64 65 64 2c 20 69 74 s been added, it
59ad0 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 is set to point
59ae0 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
59af0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 0a 2a 2a 20 connection .**
59b00 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 74 used to create t
59b10 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 4f he connection. O
59b20 6e 63 65 20 61 20 76 69 72 74 75 61 6c 20 74 61 nce a virtual ta
59b30 62 6c 65 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 ble has been.**
59b40 61 64 64 65 64 20 74 6f 20 74 68 65 20 53 63 68 added to the Sch
59b50 65 6d 61 20 73 74 72 75 63 74 75 72 65 20 61 6e ema structure an
59b60 64 20 74 68 65 20 53 63 68 65 6d 61 2e 64 62 20 d the Schema.db
59b70 76 61 72 69 61 62 6c 65 20 70 6f 70 75 6c 61 74 variable populat
59b80 65 64 2c 20 0a 2a 2a 20 6f 6e 6c 79 20 74 68 61 ed, .** only tha
59b90 74 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 t database conne
59ba0 63 74 69 6f 6e 20 6d 61 79 20 75 73 65 20 74 68 ction may use th
59bb0 65 20 53 63 68 65 6d 61 20 74 6f 20 70 72 65 70 e Schema to prep
59bc0 61 72 65 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e are .** statemen
59bd0 74 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 63 ts..*/.struct Sc
59be0 68 65 6d 61 20 7b 0a 20 20 69 6e 74 20 73 63 68 hema {. int sch
59bf0 65 6d 61 5f 63 6f 6f 6b 69 65 3b 20 20 20 2f 2a ema_cookie; /*
59c00 20 44 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 Database schema
59c10 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 version number
59c20 66 6f 72 20 74 68 69 73 20 66 69 6c 65 20 2a 2f for this file */
59c30 0a 20 20 48 61 73 68 20 74 62 6c 48 61 73 68 3b . Hash tblHash;
59c40 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 74 /* All t
59c50 61 62 6c 65 73 20 69 6e 64 65 78 65 64 20 62 79 ables indexed by
59c60 20 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73 68 20 name */. Hash
59c70 69 64 78 48 61 73 68 3b 20 20 20 20 20 20 20 20 idxHash;
59c80 2f 2a 20 41 6c 6c 20 28 6e 61 6d 65 64 29 20 69 /* All (named) i
59c90 6e 64 69 63 65 73 20 69 6e 64 65 78 65 64 20 62 ndices indexed b
59ca0 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73 68 y name */. Hash
59cb0 20 74 72 69 67 48 61 73 68 3b 20 20 20 20 20 20 trigHash;
59cc0 20 2f 2a 20 41 6c 6c 20 74 72 69 67 67 65 72 73 /* All triggers
59cd0 20 69 6e 64 65 78 65 64 20 62 79 20 6e 61 6d 65 indexed by name
59ce0 20 2a 2f 0a 20 20 48 61 73 68 20 66 6b 65 79 48 */. Hash fkeyH
59cf0 61 73 68 3b 20 20 20 20 20 20 20 2f 2a 20 41 6c ash; /* Al
59d00 6c 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 62 l foreign keys b
59d10 79 20 72 65 66 65 72 65 6e 63 65 64 20 74 61 62 y referenced tab
59d20 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 54 61 62 le name */. Tab
59d30 6c 65 20 2a 70 53 65 71 54 61 62 3b 20 20 20 20 le *pSeqTab;
59d40 20 20 2f 2a 20 54 68 65 20 73 71 6c 69 74 65 5f /* The sqlite_
59d50 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20 75 sequence table u
59d60 73 65 64 20 62 79 20 41 55 54 4f 49 4e 43 52 45 sed by AUTOINCRE
59d70 4d 45 4e 54 20 2a 2f 0a 20 20 75 38 20 66 69 6c MENT */. u8 fil
59d80 65 5f 66 6f 72 6d 61 74 3b 20 20 20 20 20 20 2f e_format; /
59d90 2a 20 53 63 68 65 6d 61 20 66 6f 72 6d 61 74 20 * Schema format
59da0 76 65 72 73 69 6f 6e 20 66 6f 72 20 74 68 69 73 version for this
59db0 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 65 6e file */. u8 en
59dc0 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
59dd0 2f 2a 20 54 65 78 74 20 65 6e 63 6f 64 69 6e 67 /* Text encoding
59de0 20 75 73 65 64 20 62 79 20 74 68 69 73 20 64 61 used by this da
59df0 74 61 62 61 73 65 20 2a 2f 0a 20 20 75 31 36 20 tabase */. u16
59e00 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 flags;
59e10 20 2f 2a 20 46 6c 61 67 73 20 61 73 73 6f 63 69 /* Flags associ
59e20 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20 73 ated with this s
59e30 63 68 65 6d 61 20 2a 2f 0a 20 20 69 6e 74 20 63 chema */. int c
59e40 61 63 68 65 5f 73 69 7a 65 3b 20 20 20 20 20 20 ache_size;
59e50 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 /* Number of pag
59e60 65 73 20 74 6f 20 75 73 65 20 69 6e 20 74 68 65 es to use in the
59e70 20 63 61 63 68 65 20 2a 2f 0a 23 69 66 6e 64 65 cache */.#ifnde
59e80 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 f SQLITE_OMIT_VI
59e90 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 73 71 6c RTUALTABLE. sql
59ea0 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 ite3 *db;
59eb0 20 20 2f 2a 20 22 4f 77 6e 65 72 22 20 63 6f 6e /* "Owner" con
59ec0 6e 65 63 74 69 6f 6e 2e 20 53 65 65 20 63 6f 6d nection. See com
59ed0 6d 65 6e 74 20 61 62 6f 76 65 20 2a 2f 0a 23 65 ment above */.#e
59ee0 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 ndif.};../*.** T
59ef0 68 65 73 65 20 6d 61 63 72 6f 73 20 63 61 6e 20 hese macros can
59f00 62 65 20 75 73 65 64 20 74 6f 20 74 65 73 74 2c be used to test,
59f10 20 73 65 74 2c 20 6f 72 20 63 6c 65 61 72 20 62 set, or clear b
59f20 69 74 73 20 69 6e 20 74 68 65 20 0a 2a 2a 20 44 its in the .** D
59f30 62 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a b.flags field..*
59f40 2f 0a 23 64 65 66 69 6e 65 20 44 62 48 61 73 50 /.#define DbHasP
59f50 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 29 20 20 roperty(D,I,P)
59f60 20 20 20 28 28 28 44 29 2d 3e 61 44 62 5b 49 5d (((D)->aDb[I]
59f70 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 26 .pSchema->flags&
59f80 28 50 29 29 3d 3d 28 50 29 29 0a 23 64 65 66 69 (P))==(P)).#defi
59f90 6e 65 20 44 62 48 61 73 41 6e 79 50 72 6f 70 65 ne DbHasAnyPrope
59fa0 72 74 79 28 44 2c 49 2c 50 29 20 20 28 28 28 44 rty(D,I,P) (((D
59fb0 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d )->aDb[I].pSchem
59fc0 61 2d 3e 66 6c 61 67 73 26 28 50 29 29 21 3d 30 a->flags&(P))!=0
59fd0 29 0a 23 64 65 66 69 6e 65 20 44 62 53 65 74 50 ).#define DbSetP
59fe0 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 29 20 20 roperty(D,I,P)
59ff0 20 20 20 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 (D)->aDb[I].p
5a000 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 7c 3d 28 Schema->flags|=(
5a010 50 29 0a 23 64 65 66 69 6e 65 20 44 62 43 6c 65 P).#define DbCle
5a020 61 72 50 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 arProperty(D,I,P
5a030 29 20 20 20 28 44 29 2d 3e 61 44 62 5b 49 5d 2e ) (D)->aDb[I].
5a040 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 26 3d pSchema->flags&=
5a050 7e 28 50 29 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f ~(P)../*.** Allo
5a060 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 74 wed values for t
5a070 68 65 20 44 42 2e 66 6c 61 67 73 20 66 69 65 6c he DB.flags fiel
5a080 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 44 42 5f d..**.** The DB_
5a090 53 63 68 65 6d 61 4c 6f 61 64 65 64 20 66 6c 61 SchemaLoaded fla
5a0a0 67 20 69 73 20 73 65 74 20 61 66 74 65 72 20 74 g is set after t
5a0b0 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 he database sche
5a0c0 6d 61 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 72 ma has been.** r
5a0d0 65 61 64 20 69 6e 74 6f 20 69 6e 74 65 72 6e 61 ead into interna
5a0e0 6c 20 68 61 73 68 20 74 61 62 6c 65 73 2e 0a 2a l hash tables..*
5a0f0 2a 0a 2a 2a 20 44 42 5f 55 6e 72 65 73 65 74 56 *.** DB_UnresetV
5a100 69 65 77 73 20 6d 65 61 6e 73 20 74 68 61 74 20 iews means that
5a110 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 76 69 65 77 one or more view
5a120 73 20 68 61 76 65 20 63 6f 6c 75 6d 6e 20 6e 61 s have column na
5a130 6d 65 73 20 74 68 61 74 0a 2a 2a 20 68 61 76 65 mes that.** have
5a140 20 62 65 65 6e 20 66 69 6c 6c 65 64 20 6f 75 74 been filled out
5a150 2e 20 20 49 66 20 74 68 65 20 73 63 68 65 6d 61 . If the schema
5a160 20 63 68 61 6e 67 65 73 2c 20 74 68 65 73 65 20 changes, these
5a170 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6d 69 67 column names mig
5a180 68 74 0a 2a 2a 20 63 68 61 6e 67 65 73 20 61 6e ht.** changes an
5a190 64 20 73 6f 20 74 68 65 20 76 69 65 77 20 77 69 d so the view wi
5a1a0 6c 6c 20 6e 65 65 64 20 74 6f 20 62 65 20 72 65 ll need to be re
5a1b0 73 65 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 set..*/.#define
5a1c0 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 20 DB_SchemaLoaded
5a1d0 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 54 68 0x0001 /* Th
5a1e0 65 20 73 63 68 65 6d 61 20 68 61 73 20 62 65 65 e schema has bee
5a1f0 6e 20 6c 6f 61 64 65 64 20 2a 2f 0a 23 64 65 66 n loaded */.#def
5a200 69 6e 65 20 44 42 5f 55 6e 72 65 73 65 74 56 69 ine DB_UnresetVi
5a210 65 77 73 20 20 20 20 30 78 30 30 30 32 20 20 2f ews 0x0002 /
5a220 2a 20 53 6f 6d 65 20 76 69 65 77 73 20 68 61 76 * Some views hav
5a230 65 20 64 65 66 69 6e 65 64 20 63 6f 6c 75 6d 6e e defined column
5a240 20 6e 61 6d 65 73 20 2a 2f 0a 23 64 65 66 69 6e names */.#defin
5a250 65 20 44 42 5f 45 6d 70 74 79 20 20 20 20 20 20 e DB_Empty
5a260 20 20 20 20 20 30 78 30 30 30 34 20 20 2f 2a 20 0x0004 /*
5a270 54 68 65 20 66 69 6c 65 20 69 73 20 65 6d 70 74 The file is empt
5a280 79 20 28 6c 65 6e 67 74 68 20 30 20 62 79 74 65 y (length 0 byte
5a290 73 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 s) */../*.** The
5a2a0 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 66 66 65 number of diffe
5a2b0 72 65 6e 74 20 6b 69 6e 64 73 20 6f 66 20 74 68 rent kinds of th
5a2c0 69 6e 67 73 20 74 68 61 74 20 63 61 6e 20 62 65 ings that can be
5a2d0 20 6c 69 6d 69 74 65 64 0a 2a 2a 20 75 73 69 6e limited.** usin
5a2e0 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6c 69 g the sqlite3_li
5a2f0 6d 69 74 28 29 20 69 6e 74 65 72 66 61 63 65 2e mit() interface.
5a300 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
5a310 54 45 5f 4e 5f 4c 49 4d 49 54 20 28 53 51 4c 49 TE_N_LIMIT (SQLI
5a320 54 45 5f 4c 49 4d 49 54 5f 54 52 49 47 47 45 52 TE_LIMIT_TRIGGER
5a330 5f 44 45 50 54 48 2b 31 29 0a 0a 2f 2a 0a 2a 2a _DEPTH+1)../*.**
5a340 20 4c 6f 6f 6b 61 73 69 64 65 20 6d 61 6c 6c 6f Lookaside mallo
5a350 63 20 69 73 20 61 20 73 65 74 20 6f 66 20 66 69 c is a set of fi
5a360 78 65 64 2d 73 69 7a 65 20 62 75 66 66 65 72 73 xed-size buffers
5a370 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 73 65 that can be use
5a380 64 0a 2a 2a 20 74 6f 20 73 61 74 69 73 66 79 20 d.** to satisfy
5a390 73 6d 61 6c 6c 20 74 72 61 6e 73 69 65 6e 74 20 small transient
5a3a0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
5a3b0 6e 20 72 65 71 75 65 73 74 73 20 66 6f 72 20 6f n requests for o
5a3c0 62 6a 65 63 74 73 0a 2a 2a 20 61 73 73 6f 63 69 bjects.** associ
5a3d0 61 74 65 64 20 77 69 74 68 20 61 20 70 61 72 74 ated with a part
5a3e0 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 icular database
5a3f0 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 connection. The
5a400 20 75 73 65 20 6f 66 0a 2a 2a 20 6c 6f 6f 6b 61 use of.** looka
5a410 73 69 64 65 20 6d 61 6c 6c 6f 63 20 70 72 6f 76 side malloc prov
5a420 69 64 65 73 20 61 20 73 69 67 6e 69 66 69 63 61 ides a significa
5a430 6e 74 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 65 nt performance e
5a440 6e 68 61 6e 63 65 6d 65 6e 74 0a 2a 2a 20 28 61 nhancement.** (a
5a450 70 70 72 6f 78 20 31 30 25 29 20 62 79 20 61 76 pprox 10%) by av
5a460 6f 69 64 69 6e 67 20 6e 75 6d 65 72 6f 75 73 20 oiding numerous
5a470 6d 61 6c 6c 6f 63 2f 66 72 65 65 20 72 65 71 75 malloc/free requ
5a480 65 73 74 73 20 77 68 69 6c 65 20 70 61 72 73 69 ests while parsi
5a490 6e 67 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d ng.** SQL statem
5a4a0 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ents..**.** The
5a4b0 4c 6f 6f 6b 61 73 69 64 65 20 73 74 72 75 63 74 Lookaside struct
5a4c0 75 72 65 20 68 6f 6c 64 73 20 63 6f 6e 66 69 67 ure holds config
5a4d0 75 72 61 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 uration informat
5a4e0 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 0a 2a 2a ion about the.**
5a4f0 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 61 6c 6c 6f lookaside mallo
5a500 63 20 73 75 62 73 79 73 74 65 6d 2e 20 20 45 61 c subsystem. Ea
5a510 63 68 20 61 76 61 69 6c 61 62 6c 65 20 6d 65 6d ch available mem
5a520 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 ory allocation i
5a530 6e 0a 2a 2a 20 74 68 65 20 6c 6f 6f 6b 61 73 69 n.** the lookasi
5a540 64 65 20 73 75 62 73 79 73 74 65 6d 20 69 73 20 de subsystem is
5a550 73 74 6f 72 65 64 20 6f 6e 20 61 20 6c 69 6e 6b stored on a link
5a560 65 64 20 6c 69 73 74 20 6f 66 20 4c 6f 6f 6b 61 ed list of Looka
5a570 73 69 64 65 53 6c 6f 74 0a 2a 2a 20 6f 62 6a 65 sideSlot.** obje
5a580 63 74 73 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 6f 6b 61 cts..**.** Looka
5a590 73 69 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 side allocations
5a5a0 20 61 72 65 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 are only allowe
5a5b0 64 20 66 6f 72 20 6f 62 6a 65 63 74 73 20 74 68 d for objects th
5a5c0 61 74 20 61 72 65 20 61 73 73 6f 63 69 61 74 65 at are associate
5a5d0 64 0a 2a 2a 20 77 69 74 68 20 61 20 70 61 72 74 d.** with a part
5a5e0 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 icular database
5a5f0 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 48 65 6e connection. Hen
5a600 63 65 2c 20 73 63 68 65 6d 61 20 69 6e 66 6f 72 ce, schema infor
5a610 6d 61 74 69 6f 6e 20 63 61 6e 6e 6f 74 0a 2a 2a mation cannot.**
5a620 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 6c 6f be stored in lo
5a630 6f 6b 61 73 69 64 65 20 62 65 63 61 75 73 65 20 okaside because
5a640 69 6e 20 73 68 61 72 65 64 20 63 61 63 68 65 20 in shared cache
5a650 6d 6f 64 65 20 74 68 65 20 73 63 68 65 6d 61 20 mode the schema
5a660 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 69 information.** i
5a670 73 20 73 68 61 72 65 64 20 62 79 20 6d 75 6c 74 s shared by mult
5a680 69 70 6c 65 20 64 61 74 61 62 61 73 65 20 63 6f iple database co
5a690 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 54 68 65 72 nnections. Ther
5a6a0 65 66 6f 72 65 2c 20 77 68 69 6c 65 20 70 61 72 efore, while par
5a6b0 73 69 6e 67 0a 2a 2a 20 73 63 68 65 6d 61 20 69 sing.** schema i
5a6c0 6e 66 6f 72 6d 61 74 69 6f 6e 2c 20 74 68 65 20 nformation, the
5a6d0 4c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c Lookaside.bEnabl
5a6e0 65 64 20 66 6c 61 67 20 69 73 20 63 6c 65 61 72 ed flag is clear
5a6f0 65 64 20 73 6f 20 74 68 61 74 0a 2a 2a 20 6c 6f ed so that.** lo
5a700 6f 6b 61 73 69 64 65 20 61 6c 6c 6f 63 61 74 69 okaside allocati
5a710 6f 6e 73 20 61 72 65 20 6e 6f 74 20 75 73 65 64 ons are not used
5a720 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 20 74 68 to construct th
5a730 65 20 73 63 68 65 6d 61 20 6f 62 6a 65 63 74 73 e schema objects
5a740 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4c 6f 6f 6b ..*/.struct Look
5a750 61 73 69 64 65 20 7b 0a 20 20 75 31 36 20 73 7a aside {. u16 sz
5a760 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
5a770 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 /* Size of eac
5a780 68 20 62 75 66 66 65 72 20 69 6e 20 62 79 74 65 h buffer in byte
5a790 73 20 2a 2f 0a 20 20 75 38 20 62 45 6e 61 62 6c s */. u8 bEnabl
5a7a0 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f ed; /
5a7b0 2a 20 46 61 6c 73 65 20 74 6f 20 64 69 73 61 62 * False to disab
5a7c0 6c 65 20 6e 65 77 20 6c 6f 6f 6b 61 73 69 64 65 le new lookaside
5a7d0 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a allocations */.
5a7e0 20 20 75 38 20 62 4d 61 6c 6c 6f 63 65 64 3b 20 u8 bMalloced;
5a7f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
5a800 65 20 69 66 20 70 53 74 61 72 74 20 6f 62 74 61 e if pStart obta
5a810 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 ined from sqlite
5a820 33 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 20 20 3_malloc() */.
5a830 69 6e 74 20 6e 4f 75 74 3b 20 20 20 20 20 20 20 int nOut;
5a840 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
5a850 72 20 6f 66 20 62 75 66 66 65 72 73 20 63 75 72 r of buffers cur
5a860 72 65 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 6f rently checked o
5a870 75 74 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 4f 75 ut */. int mxOu
5a880 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t;
5a890 2f 2a 20 48 69 67 68 77 61 74 65 72 20 6d 61 72 /* Highwater mar
5a8a0 6b 20 66 6f 72 20 6e 4f 75 74 20 2a 2f 0a 20 20 k for nOut */.
5a8b0 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 LookasideSlot *p
5a8c0 46 72 65 65 3b 20 20 20 2f 2a 20 4c 69 73 74 20 Free; /* List
5a8d0 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 62 75 66 of available buf
5a8e0 66 65 72 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a fers */. void *
5a8f0 70 53 74 61 72 74 3b 20 20 20 20 20 20 20 20 20 pStart;
5a900 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 /* First byte
5a910 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 6d 65 6d of available mem
5a920 6f 72 79 20 73 70 61 63 65 20 2a 2f 0a 20 20 76 ory space */. v
5a930 6f 69 64 20 2a 70 45 6e 64 3b 20 20 20 20 20 20 oid *pEnd;
5a940 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 /* First
5a950 62 79 74 65 20 70 61 73 74 20 65 6e 64 20 6f 66 byte past end of
5a960 20 61 76 61 69 6c 61 62 6c 65 20 73 70 61 63 65 available space
5a970 20 2a 2f 0a 7d 3b 0a 73 74 72 75 63 74 20 4c 6f */.};.struct Lo
5a980 6f 6b 61 73 69 64 65 53 6c 6f 74 20 7b 0a 20 20 okasideSlot {.
5a990 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 LookasideSlot *p
5a9a0 4e 65 78 74 3b 20 20 20 20 2f 2a 20 4e 65 78 74 Next; /* Next
5a9b0 20 62 75 66 66 65 72 20 69 6e 20 74 68 65 20 6c buffer in the l
5a9c0 69 73 74 20 6f 66 20 66 72 65 65 20 62 75 66 66 ist of free buff
5a9d0 65 72 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ers */.};../*.**
5a9e0 20 41 20 68 61 73 68 20 74 61 62 6c 65 20 66 6f A hash table fo
5a9f0 72 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e r function defin
5aa00 69 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 48 61 itions..**.** Ha
5aa10 73 68 20 65 61 63 68 20 46 75 6e 63 44 65 66 20 sh each FuncDef
5aa20 73 74 72 75 63 74 75 72 65 20 69 6e 74 6f 20 6f structure into o
5aa30 6e 65 20 6f 66 20 74 68 65 20 46 75 6e 63 44 65 ne of the FuncDe
5aa40 66 48 61 73 68 2e 61 5b 5d 20 73 6c 6f 74 73 2e fHash.a[] slots.
5aa50 0a 2a 2a 20 43 6f 6c 6c 69 73 69 6f 6e 73 20 61 .** Collisions a
5aa60 72 65 20 6f 6e 20 74 68 65 20 46 75 6e 63 44 65 re on the FuncDe
5aa70 66 2e 70 48 61 73 68 20 63 68 61 69 6e 2e 0a 2a f.pHash chain..*
5aa80 2f 0a 73 74 72 75 63 74 20 46 75 6e 63 44 65 66 /.struct FuncDef
5aa90 48 61 73 68 20 7b 0a 20 20 46 75 6e 63 44 65 66 Hash {. FuncDef
5aaa0 20 2a 61 5b 32 33 5d 3b 20 20 20 20 20 20 20 2f *a[23]; /
5aab0 2a 20 48 61 73 68 20 74 61 62 6c 65 20 66 6f 72 * Hash table for
5aac0 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 7d 3b functions */.};
5aad0 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 64 61 74 ../*.** Each dat
5aae0 61 62 61 73 65 20 69 73 20 61 6e 20 69 6e 73 74 abase is an inst
5aaf0 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c ance of the foll
5ab00 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e owing structure.
5ab10 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
5ab20 65 2e 6c 61 73 74 52 6f 77 69 64 20 72 65 63 6f e.lastRowid reco
5ab30 72 64 73 20 74 68 65 20 6c 61 73 74 20 69 6e 73 rds the last ins
5ab40 65 72 74 20 72 6f 77 69 64 20 67 65 6e 65 72 61 ert rowid genera
5ab50 74 65 64 20 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 ted by an.** ins
5ab60 65 72 74 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 ert statement.
5ab70 49 6e 73 65 72 74 73 20 6f 6e 20 76 69 65 77 73 Inserts on views
5ab80 20 64 6f 20 6e 6f 74 20 61 66 66 65 63 74 20 69 do not affect i
5ab90 74 73 20 76 61 6c 75 65 2e 20 20 45 61 63 68 0a ts value. Each.
5aba0 2a 2a 20 74 72 69 67 67 65 72 20 68 61 73 20 69 ** trigger has i
5abb0 74 73 20 6f 77 6e 20 63 6f 6e 74 65 78 74 2c 20 ts own context,
5abc0 73 6f 20 74 68 61 74 20 6c 61 73 74 52 6f 77 69 so that lastRowi
5abd0 64 20 63 61 6e 20 62 65 20 75 70 64 61 74 65 64 d can be updated
5abe0 20 69 6e 73 69 64 65 0a 2a 2a 20 74 72 69 67 67 inside.** trigg
5abf0 65 72 73 20 61 73 20 75 73 75 61 6c 2e 20 20 54 ers as usual. T
5ac00 68 65 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 he previous valu
5ac10 65 20 77 69 6c 6c 20 62 65 20 72 65 73 74 6f 72 e will be restor
5ac20 65 64 20 6f 6e 63 65 20 74 68 65 20 74 72 69 67 ed once the trig
5ac30 67 65 72 0a 2a 2a 20 65 78 69 74 73 2e 20 20 55 ger.** exits. U
5ac40 70 6f 6e 20 65 6e 74 65 72 69 6e 67 20 61 20 62 pon entering a b
5ac50 65 66 6f 72 65 20 6f 72 20 69 6e 73 74 65 61 64 efore or instead
5ac60 20 6f 66 20 74 72 69 67 67 65 72 2c 20 6c 61 73 of trigger, las
5ac70 74 52 6f 77 69 64 20 69 73 20 6e 6f 0a 2a 2a 20 tRowid is no.**
5ac80 6c 6f 6e 67 65 72 20 28 73 69 6e 63 65 20 61 66 longer (since af
5ac90 74 65 72 20 76 65 72 73 69 6f 6e 20 32 2e 38 2e ter version 2.8.
5aca0 31 32 29 20 72 65 73 65 74 20 74 6f 20 2d 31 2e 12) reset to -1.
5acb0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
5acc0 65 2e 6e 43 68 61 6e 67 65 20 64 6f 65 73 20 6e e.nChange does n
5acd0 6f 74 20 63 6f 75 6e 74 20 63 68 61 6e 67 65 73 ot count changes
5ace0 20 77 69 74 68 69 6e 20 74 72 69 67 67 65 72 73 within triggers
5acf0 20 61 6e 64 20 6b 65 65 70 73 20 6e 6f 0a 2a 2a and keeps no.**
5ad00 20 63 6f 6e 74 65 78 74 2e 20 20 49 74 20 69 73 context. It is
5ad10 20 72 65 73 65 74 20 61 74 20 73 74 61 72 74 20 reset at start
5ad20 6f 66 20 73 71 6c 69 74 65 33 5f 65 78 65 63 2e of sqlite3_exec.
5ad30 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 6c .** The sqlite.l
5ad40 73 43 68 61 6e 67 65 20 72 65 70 72 65 73 65 6e sChange represen
5ad50 74 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ts the number of
5ad60 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20 62 79 changes made by
5ad70 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20 69 6e 73 the last.** ins
5ad80 65 72 74 2c 20 75 70 64 61 74 65 2c 20 6f 72 20 ert, update, or
5ad90 64 65 6c 65 74 65 20 73 74 61 74 65 6d 65 6e 74 delete statement
5ada0 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20 63 6f . It remains co
5adb0 6e 73 74 61 6e 74 20 74 68 72 6f 75 67 68 6f 75 nstant throughou
5adc0 74 20 74 68 65 0a 2a 2a 20 6c 65 6e 67 74 68 20 t the.** length
5add0 6f 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20 61 of a statement a
5ade0 6e 64 20 69 73 20 74 68 65 6e 20 75 70 64 61 74 nd is then updat
5adf0 65 64 20 62 79 20 4f 50 5f 53 65 74 43 6f 75 6e ed by OP_SetCoun
5ae00 74 73 2e 20 20 49 74 20 6b 65 65 70 73 20 61 0a ts. It keeps a.
5ae10 2a 2a 20 63 6f 6e 74 65 78 74 20 73 74 61 63 6b ** context stack
5ae20 20 6a 75 73 74 20 6c 69 6b 65 20 6c 61 73 74 52 just like lastR
5ae30 6f 77 69 64 20 73 6f 20 74 68 61 74 20 74 68 65 owid so that the
5ae40 20 63 6f 75 6e 74 20 6f 66 20 63 68 61 6e 67 65 count of change
5ae50 73 0a 2a 2a 20 77 69 74 68 69 6e 20 61 20 74 72 s.** within a tr
5ae60 69 67 67 65 72 20 69 73 20 6e 6f 74 20 73 65 65 igger is not see
5ae70 6e 20 6f 75 74 73 69 64 65 20 74 68 65 20 74 72 n outside the tr
5ae80 69 67 67 65 72 2e 20 20 43 68 61 6e 67 65 73 20 igger. Changes
5ae90 74 6f 20 76 69 65 77 73 20 64 6f 20 6e 6f 74 0a to views do not.
5aea0 2a 2a 20 61 66 66 65 63 74 20 74 68 65 20 76 61 ** affect the va
5aeb0 6c 75 65 20 6f 66 20 6c 73 43 68 61 6e 67 65 2e lue of lsChange.
5aec0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 63 .** The sqlite.c
5aed0 73 43 68 61 6e 67 65 20 6b 65 65 70 73 20 74 72 sChange keeps tr
5aee0 61 63 6b 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 ack of the numbe
5aef0 72 20 6f 66 20 63 75 72 72 65 6e 74 20 63 68 61 r of current cha
5af00 6e 67 65 73 20 28 73 69 6e 63 65 0a 2a 2a 20 74 nges (since.** t
5af10 68 65 20 6c 61 73 74 20 73 74 61 74 65 6d 65 6e he last statemen
5af20 74 29 20 61 6e 64 20 69 73 20 75 73 65 64 20 74 t) and is used t
5af30 6f 20 75 70 64 61 74 65 20 73 71 6c 69 74 65 5f o update sqlite_
5af40 6c 73 43 68 61 6e 67 65 2e 0a 2a 2a 0a 2a 2a 20 lsChange..**.**
5af50 54 68 65 20 6d 65 6d 62 65 72 20 76 61 72 69 61 The member varia
5af60 62 6c 65 73 20 73 71 6c 69 74 65 2e 65 72 72 43 bles sqlite.errC
5af70 6f 64 65 2c 20 73 71 6c 69 74 65 2e 7a 45 72 72 ode, sqlite.zErr
5af80 4d 73 67 20 61 6e 64 20 73 71 6c 69 74 65 2e 7a Msg and sqlite.z
5af90 45 72 72 4d 73 67 31 36 0a 2a 2a 20 73 74 6f 72 ErrMsg16.** stor
5afa0 65 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e e the most recen
5afb0 74 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 t error code and
5afc0 2c 20 69 66 20 61 70 70 6c 69 63 61 62 6c 65 2c , if applicable,
5afd0 20 73 74 72 69 6e 67 2e 20 54 68 65 0a 2a 2a 20 string. The.**
5afe0 69 6e 74 65 72 6e 61 6c 20 66 75 6e 63 74 69 6f internal functio
5aff0 6e 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 29 n sqlite3Error()
5b000 20 69 73 20 75 73 65 64 20 74 6f 20 73 65 74 20 is used to set
5b010 74 68 65 73 65 20 76 61 72 69 61 62 6c 65 73 0a these variables.
5b020 2a 2a 20 63 6f 6e 73 69 73 74 65 6e 74 6c 79 2e ** consistently.
5b030 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 .*/.struct sqlit
5b040 65 33 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 e3 {. sqlite3_v
5b050 66 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20 fs *pVfs;
5b060 20 20 20 20 20 2f 2a 20 4f 53 20 49 6e 74 65 72 /* OS Inter
5b070 66 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 face */. int nD
5b080 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 b;
5b090 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
5b0a0 72 20 6f 66 20 62 61 63 6b 65 6e 64 73 20 63 75 r of backends cu
5b0b0 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 20 2a rrently in use *
5b0c0 2f 0a 20 20 44 62 20 2a 61 44 62 3b 20 20 20 20 /. Db *aDb;
5b0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b0e0 20 20 2f 2a 20 41 6c 6c 20 62 61 63 6b 65 6e 64 /* All backend
5b0f0 73 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 s */. int flags
5b100 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
5b110 20 20 20 20 20 2f 2a 20 4d 69 73 63 65 6c 6c 61 /* Miscella
5b120 6e 65 6f 75 73 20 66 6c 61 67 73 2e 20 53 65 65 neous flags. See
5b130 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74 20 below */. int
5b140 6f 70 65 6e 46 6c 61 67 73 3b 20 20 20 20 20 20 openFlags;
5b150 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 /* Fla
5b160 67 73 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c gs passed to sql
5b170 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29 ite3_vfs.xOpen()
5b180 20 2a 2f 0a 20 20 69 6e 74 20 65 72 72 43 6f 64 */. int errCod
5b190 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
5b1a0 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 /* Most rece
5b1b0 6e 74 20 65 72 72 6f 72 20 63 6f 64 65 20 28 53 nt error code (S
5b1c0 51 4c 49 54 45 5f 2a 29 20 2a 2f 0a 20 20 69 6e QLITE_*) */. in
5b1d0 74 20 65 72 72 4d 61 73 6b 3b 20 20 20 20 20 20 t errMask;
5b1e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 26 /* &
5b1f0 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 77 69 result codes wi
5b200 74 68 20 74 68 69 73 20 62 65 66 6f 72 65 20 72 th this before r
5b210 65 74 75 72 6e 69 6e 67 20 2a 2f 0a 20 20 75 38 eturning */. u8
5b220 20 61 75 74 6f 43 6f 6d 6d 69 74 3b 20 20 20 20 autoCommit;
5b230 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
5b240 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 he auto-commit f
5b250 6c 61 67 2e 20 2a 2f 0a 20 20 75 38 20 74 65 6d lag. */. u8 tem
5b260 70 5f 73 74 6f 72 65 3b 20 20 20 20 20 20 20 20 p_store;
5b270 20 20 20 20 20 20 20 20 2f 2a 20 31 3a 20 66 69 /* 1: fi
5b280 6c 65 20 32 3a 20 6d 65 6d 6f 72 79 20 30 3a 20 le 2: memory 0:
5b290 64 65 66 61 75 6c 74 20 2a 2f 0a 20 20 75 38 20 default */. u8
5b2a0 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3b 20 20 20 mallocFailed;
5b2b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
5b2c0 75 65 20 69 66 20 77 65 20 68 61 76 65 20 73 65 ue if we have se
5b2d0 65 6e 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c en a malloc fail
5b2e0 75 72 65 20 2a 2f 0a 20 20 75 38 20 64 66 6c 74 ure */. u8 dflt
5b2f0 4c 6f 63 6b 4d 6f 64 65 3b 20 20 20 20 20 20 20 LockMode;
5b300 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c /* Defaul
5b310 74 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 66 t locking-mode f
5b320 6f 72 20 61 74 74 61 63 68 65 64 20 64 62 73 20 or attached dbs
5b330 2a 2f 0a 20 20 75 38 20 64 66 6c 74 4a 6f 75 72 */. u8 dfltJour
5b340 6e 61 6c 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 nalMode;
5b350 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 6a 6f /* Default jo
5b360 75 72 6e 61 6c 20 6d 6f 64 65 20 66 6f 72 20 61 urnal mode for a
5b370 74 74 61 63 68 65 64 20 64 62 73 20 2a 2f 0a 20 ttached dbs */.
5b380 20 73 69 67 6e 65 64 20 63 68 61 72 20 6e 65 78 signed char nex
5b390 74 41 75 74 6f 76 61 63 3b 20 20 20 20 20 20 2f tAutovac; /
5b3a0 2a 20 41 75 74 6f 76 61 63 20 73 65 74 74 69 6e * Autovac settin
5b3b0 67 20 61 66 74 65 72 20 56 41 43 55 55 4d 20 69 g after VACUUM i
5b3c0 66 20 3e 3d 30 20 2a 2f 0a 20 20 69 6e 74 20 6e f >=0 */. int n
5b3d0 65 78 74 50 61 67 65 73 69 7a 65 3b 20 20 20 20 extPagesize;
5b3e0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
5b3f0 73 69 7a 65 20 61 66 74 65 72 20 56 41 43 55 55 size after VACUU
5b400 4d 20 69 66 20 3e 30 20 2a 2f 0a 20 20 69 6e 74 M if >0 */. int
5b410 20 6e 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 nTable;
5b420 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
5b430 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20 69 mber of tables i
5b440 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a n the database *
5b450 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 44 66 /. CollSeq *pDf
5b460 6c 74 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20 ltColl;
5b470 20 20 2f 2a 20 54 68 65 20 64 65 66 61 75 6c 74 /* The default
5b480 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 collating seque
5b490 6e 63 65 20 28 42 49 4e 41 52 59 29 20 2a 2f 0a nce (BINARY) */.
5b4a0 20 20 69 36 34 20 6c 61 73 74 52 6f 77 69 64 3b i64 lastRowid;
5b4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b4c0 2f 2a 20 52 4f 57 49 44 20 6f 66 20 6d 6f 73 74 /* ROWID of most
5b4d0 20 72 65 63 65 6e 74 20 69 6e 73 65 72 74 20 28 recent insert (
5b4e0 73 65 65 20 61 62 6f 76 65 29 20 2a 2f 0a 20 20 see above) */.
5b4f0 75 33 32 20 6d 61 67 69 63 3b 20 20 20 20 20 20 u32 magic;
5b500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
5b510 20 4d 61 67 69 63 20 6e 75 6d 62 65 72 20 66 6f Magic number fo
5b520 72 20 64 65 74 65 63 74 20 6c 69 62 72 61 72 79 r detect library
5b530 20 6d 69 73 75 73 65 20 2a 2f 0a 20 20 69 6e 74 misuse */. int
5b540 20 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20 20 nChange;
5b550 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 /* Va
5b560 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 lue returned by
5b570 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 sqlite3_changes(
5b580 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f 74 61 ) */. int nTota
5b590 6c 43 68 61 6e 67 65 3b 20 20 20 20 20 20 20 20 lChange;
5b5a0 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 65 /* Value re
5b5b0 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 turned by sqlite
5b5c0 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 3_total_changes(
5b5d0 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d ) */. sqlite3_m
5b5e0 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 20 utex *mutex;
5b5f0 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 /* Connecti
5b600 6f 6e 20 6d 75 74 65 78 20 2a 2f 0a 20 20 69 6e on mutex */. in
5b610 74 20 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f t aLimit[SQLITE_
5b620 4e 5f 4c 49 4d 49 54 5d 3b 20 20 20 2f 2a 20 4c N_LIMIT]; /* L
5b630 69 6d 69 74 73 20 2a 2f 0a 20 20 73 74 72 75 63 imits */. struc
5b640 74 20 73 71 6c 69 74 65 33 49 6e 69 74 49 6e 66 t sqlite3InitInf
5b650 6f 20 7b 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f o { /* Info
5b660 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 64 75 72 rmation used dur
5b670 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 ing initializati
5b680 6f 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 44 on */. int iD
5b690 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 b;
5b6a0 20 20 20 20 20 20 2f 2a 20 57 68 65 6e 20 62 61 /* When ba
5b6b0 63 6b 20 69 73 20 62 65 69 6e 67 20 69 6e 69 74 ck is being init
5b6c0 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 20 20 69 ialized */. i
5b6d0 6e 74 20 6e 65 77 54 6e 75 6d 3b 20 20 20 20 20 nt newTnum;
5b6e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f /* Ro
5b6f0 6f 74 70 61 67 65 20 6f 66 20 74 61 62 6c 65 20 otpage of table
5b700 62 65 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 65 being initialize
5b710 64 20 2a 2f 0a 20 20 20 20 75 38 20 62 75 73 79 d */. u8 busy
5b720 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
5b730 20 20 20 20 20 2f 2a 20 54 52 55 45 20 69 66 20 /* TRUE if
5b740 63 75 72 72 65 6e 74 6c 79 20 69 6e 69 74 69 61 currently initia
5b750 6c 69 7a 69 6e 67 20 2a 2f 0a 20 20 20 20 75 38 lizing */. u8
5b760 20 6f 72 70 68 61 6e 54 72 69 67 67 65 72 3b 20 orphanTrigger;
5b770 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 /* Las
5b780 74 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6f t statement is o
5b790 72 70 68 61 6e 65 64 20 54 45 4d 50 20 74 72 69 rphaned TEMP tri
5b7a0 67 67 65 72 20 2a 2f 0a 20 20 7d 20 69 6e 69 74 gger */. } init
5b7b0 3b 0a 20 20 69 6e 74 20 6e 45 78 74 65 6e 73 69 ;. int nExtensi
5b7c0 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 on;
5b7d0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c /* Number of l
5b7e0 6f 61 64 65 64 20 65 78 74 65 6e 73 69 6f 6e 73 oaded extensions
5b7f0 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 2a 61 45 78 */. void **aEx
5b800 74 65 6e 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 tension;
5b810 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 /* Array of
5b820 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 20 68 shared library h
5b830 61 6e 64 6c 65 73 20 2a 2f 0a 20 20 73 74 72 75 andles */. stru
5b840 63 74 20 56 64 62 65 20 2a 70 56 64 62 65 3b 20 ct Vdbe *pVdbe;
5b850 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 /* Lis
5b860 74 20 6f 66 20 61 63 74 69 76 65 20 76 69 72 74 t of active virt
5b870 75 61 6c 20 6d 61 63 68 69 6e 65 73 20 2a 2f 0a ual machines */.
5b880 20 20 69 6e 74 20 61 63 74 69 76 65 56 64 62 65 int activeVdbe
5b890 43 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 Cnt;
5b8a0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 56 44 42 /* Number of VDB
5b8b0 45 73 20 63 75 72 72 65 6e 74 6c 79 20 65 78 65 Es currently exe
5b8c0 63 75 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 cuting */. int
5b8d0 77 72 69 74 65 56 64 62 65 43 6e 74 3b 20 20 20 writeVdbeCnt;
5b8e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
5b8f0 62 65 72 20 6f 66 20 61 63 74 69 76 65 20 56 44 ber of active VD
5b900 42 45 73 20 74 68 61 74 20 61 72 65 20 77 72 69 BEs that are wri
5b910 74 69 6e 67 20 2a 2f 0a 20 20 76 6f 69 64 20 28 ting */. void (
5b920 2a 78 54 72 61 63 65 29 28 76 6f 69 64 2a 2c 63 *xTrace)(void*,c
5b930 6f 6e 73 74 20 63 68 61 72 2a 29 3b 20 20 20 20 onst char*);
5b940 20 20 20 20 2f 2a 20 54 72 61 63 65 20 66 75 6e /* Trace fun
5b950 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 ction */. void
5b960 2a 70 54 72 61 63 65 41 72 67 3b 20 20 20 20 20 *pTraceArg;
5b970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b980 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 /* Argument
5b990 20 74 6f 20 74 68 65 20 74 72 61 63 65 20 66 75 to the trace fu
5b9a0 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 nction */. void
5b9b0 20 28 2a 78 50 72 6f 66 69 6c 65 29 28 76 6f 69 (*xProfile)(voi
5b9c0 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 d*,const char*,u
5b9d0 36 34 29 3b 20 20 2f 2a 20 50 72 6f 66 69 6c 69 64); /* Profili
5b9e0 6e 67 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 ng function */.
5b9f0 20 76 6f 69 64 20 2a 70 50 72 6f 66 69 6c 65 41 void *pProfileA
5ba00 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rg;
5ba10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 /* Ar
5ba20 67 75 6d 65 6e 74 20 74 6f 20 70 72 6f 66 69 6c gument to profil
5ba30 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 e function */.
5ba40 76 6f 69 64 20 2a 70 43 6f 6d 6d 69 74 41 72 67 void *pCommitArg
5ba50 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
5ba60 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f /* Argument to
5ba70 20 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b xCommitCallback
5ba80 28 29 20 2a 2f 20 20 20 0a 20 20 69 6e 74 20 28 () */ . int (
5ba90 2a 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b *xCommitCallback
5baa0 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 2f 2a 20 )(void*); /*
5bab0 49 6e 76 6f 6b 65 64 20 61 74 20 65 76 65 72 79 Invoked at every
5bac0 20 63 6f 6d 6d 69 74 2e 20 2a 2f 0a 20 20 76 6f commit. */. vo
5bad0 69 64 20 2a 70 52 6f 6c 6c 62 61 63 6b 41 72 67 id *pRollbackArg
5bae0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
5baf0 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 /* Argument to x
5bb00 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63 6b RollbackCallback
5bb10 28 29 20 2a 2f 20 20 20 0a 20 20 76 6f 69 64 20 () */ . void
5bb20 28 2a 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 (*xRollbackCallb
5bb30 61 63 6b 29 28 76 6f 69 64 2a 29 3b 20 2f 2a 20 ack)(void*); /*
5bb40 49 6e 76 6f 6b 65 64 20 61 74 20 65 76 65 72 79 Invoked at every
5bb50 20 63 6f 6d 6d 69 74 2e 20 2a 2f 0a 20 20 76 6f commit. */. vo
5bb60 69 64 20 2a 70 55 70 64 61 74 65 41 72 67 3b 0a id *pUpdateArg;.
5bb70 20 20 76 6f 69 64 20 28 2a 78 55 70 64 61 74 65 void (*xUpdate
5bb80 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c Callback)(void*,
5bb90 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a int, const char*
5bba0 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71 6c ,const char*,sql
5bbb0 69 74 65 5f 69 6e 74 36 34 29 3b 0a 20 20 76 6f ite_int64);. vo
5bbc0 69 64 28 2a 78 43 6f 6c 6c 4e 65 65 64 65 64 29 id(*xCollNeeded)
5bbd0 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c (void*,sqlite3*,
5bbe0 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f 6e int eTextRep,con
5bbf0 73 74 20 63 68 61 72 2a 29 3b 0a 20 20 76 6f 69 st char*);. voi
5bc00 64 28 2a 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 d(*xCollNeeded16
5bc10 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a )(void*,sqlite3*
5bc20 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f ,int eTextRep,co
5bc30 6e 73 74 20 76 6f 69 64 2a 29 3b 0a 20 20 76 6f nst void*);. vo
5bc40 69 64 20 2a 70 43 6f 6c 6c 4e 65 65 64 65 64 41 id *pCollNeededA
5bc50 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 rg;. sqlite3_va
5bc60 6c 75 65 20 2a 70 45 72 72 3b 20 20 20 20 20 20 lue *pErr;
5bc70 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 /* Most rece
5bc80 6e 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 nt error message
5bc90 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 */. char *zErr
5bca0 4d 73 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 Msg;
5bcb0 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 /* Most rece
5bcc0 6e 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 nt error message
5bcd0 20 28 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 29 (UTF-8 encoded)
5bce0 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 */. char *zErr
5bcf0 4d 73 67 31 36 3b 20 20 20 20 20 20 20 20 20 20 Msg16;
5bd00 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 /* Most rece
5bd10 6e 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 nt error message
5bd20 20 28 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 (UTF-16 encoded
5bd30 29 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 ) */. union {.
5bd40 20 20 20 76 6f 6c 61 74 69 6c 65 20 69 6e 74 20 volatile int
5bd50 69 73 49 6e 74 65 72 72 75 70 74 65 64 3b 20 2f isInterrupted; /
5bd60 2a 20 54 72 75 65 20 69 66 20 73 71 6c 69 74 65 * True if sqlite
5bd70 33 5f 69 6e 74 65 72 72 75 70 74 20 68 61 73 20 3_interrupt has
5bd80 62 65 65 6e 20 63 61 6c 6c 65 64 20 2a 2f 0a 20 been called */.
5bd90 20 20 20 64 6f 75 62 6c 65 20 6e 6f 74 55 73 65 double notUse
5bda0 64 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f d1; /
5bdb0 2a 20 53 70 61 63 65 72 20 2a 2f 0a 20 20 7d 20 * Spacer */. }
5bdc0 75 31 3b 0a 20 20 4c 6f 6f 6b 61 73 69 64 65 20 u1;. Lookaside
5bdd0 6c 6f 6f 6b 61 73 69 64 65 3b 20 20 20 20 20 20 lookaside;
5bde0 20 20 20 20 2f 2a 20 4c 6f 6f 6b 61 73 69 64 65 /* Lookaside
5bdf0 20 6d 61 6c 6c 6f 63 20 63 6f 6e 66 69 67 75 72 malloc configur
5be00 61 74 69 6f 6e 20 2a 2f 0a 23 69 66 6e 64 65 66 ation */.#ifndef
5be10 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
5be20 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 69 6e 74 HORIZATION. int
5be30 20 28 2a 78 41 75 74 68 29 28 76 6f 69 64 2a 2c (*xAuth)(void*,
5be40 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c int,const char*,
5be50 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 const char*,cons
5be60 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 t char*,const ch
5be70 61 72 2a 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ar*);.
5be80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5be90 20 20 20 20 20 20 2f 2a 20 41 63 63 65 73 73 20 /* Access
5bea0 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75 authorization fu
5beb0 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 nction */. void
5bec0 20 2a 70 41 75 74 68 41 72 67 3b 20 20 20 20 20 *pAuthArg;
5bed0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 73 74 /* 1st
5bee0 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 argument to the
5bef0 20 61 63 63 65 73 73 20 61 75 74 68 20 66 75 6e access auth fun
5bf00 63 74 69 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a ction */.#endif.
5bf10 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
5bf20 4d 49 54 5f 50 52 4f 47 52 45 53 53 5f 43 41 4c MIT_PROGRESS_CAL
5bf30 4c 42 41 43 4b 0a 20 20 69 6e 74 20 28 2a 78 50 LBACK. int (*xP
5bf40 72 6f 67 72 65 73 73 29 28 76 6f 69 64 20 2a 29 rogress)(void *)
5bf50 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 70 72 6f ; /* The pro
5bf60 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 2a gress callback *
5bf70 2f 0a 20 20 76 6f 69 64 20 2a 70 50 72 6f 67 72 /. void *pProgr
5bf80 65 73 73 41 72 67 3b 20 20 20 20 20 20 20 20 20 essArg;
5bf90 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f /* Argument to
5bfa0 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 the progress ca
5bfb0 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 llback */. int
5bfc0 6e 50 72 6f 67 72 65 73 73 4f 70 73 3b 20 20 20 nProgressOps;
5bfd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
5bfe0 62 65 72 20 6f 66 20 6f 70 63 6f 64 65 73 20 66 ber of opcodes f
5bff0 6f 72 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c or progress call
5c000 62 61 63 6b 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 back */.#endif.#
5c010 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
5c020 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a IT_VIRTUALTABLE.
5c030 20 20 48 61 73 68 20 61 4d 6f 64 75 6c 65 3b 20 Hash aModule;
5c040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5c050 2f 2a 20 70 6f 70 75 6c 61 74 65 64 20 62 79 20 /* populated by
5c060 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d sqlite3_create_m
5c070 6f 64 75 6c 65 28 29 20 2a 2f 0a 20 20 54 61 62 odule() */. Tab
5c080 6c 65 20 2a 70 56 54 61 62 3b 20 20 20 20 20 20 le *pVTab;
5c090 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 76 74 /* vt
5c0a0 61 62 20 77 69 74 68 20 61 63 74 69 76 65 20 43 ab with active C
5c0b0 6f 6e 6e 65 63 74 2f 43 72 65 61 74 65 20 6d 65 onnect/Create me
5c0c0 74 68 6f 64 20 2a 2f 0a 20 20 56 54 61 62 6c 65 thod */. VTable
5c0d0 20 2a 2a 61 56 54 72 61 6e 73 3b 20 20 20 20 20 **aVTrans;
5c0e0 20 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75 /* Virtu
5c0f0 61 6c 20 74 61 62 6c 65 73 20 77 69 74 68 20 6f al tables with o
5c100 70 65 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 pen transactions
5c110 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 54 72 61 6e */. int nVTran
5c120 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s;
5c130 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 /* Allocated
5c140 20 73 69 7a 65 20 6f 66 20 61 56 54 72 61 6e 73 size of aVTrans
5c150 20 2a 2f 0a 20 20 56 54 61 62 6c 65 20 2a 70 44 */. VTable *pD
5c160 69 73 63 6f 6e 6e 65 63 74 3b 20 20 20 20 2f 2a isconnect; /*
5c170 20 44 69 73 63 6f 6e 6e 65 63 74 20 74 68 65 73 Disconnect thes
5c180 65 20 69 6e 20 6e 65 78 74 20 73 71 6c 69 74 65 e in next sqlite
5c190 33 5f 70 72 65 70 61 72 65 28 29 20 2a 2f 0a 23 3_prepare() */.#
5c1a0 65 6e 64 69 66 0a 20 20 46 75 6e 63 44 65 66 48 endif. FuncDefH
5c1b0 61 73 68 20 61 46 75 6e 63 3b 20 20 20 20 20 20 ash aFunc;
5c1c0 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 74 61 /* Hash ta
5c1d0 62 6c 65 20 6f 66 20 63 6f 6e 6e 65 63 74 69 6f ble of connectio
5c1e0 6e 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20 n functions */.
5c1f0 20 48 61 73 68 20 61 43 6f 6c 6c 53 65 71 3b 20 Hash aCollSeq;
5c200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
5c210 2a 20 41 6c 6c 20 63 6f 6c 6c 61 74 69 6e 67 20 * All collating
5c220 73 65 71 75 65 6e 63 65 73 20 2a 2f 0a 20 20 42 sequences */. B
5c230 75 73 79 48 61 6e 64 6c 65 72 20 62 75 73 79 48 usyHandler busyH
5c240 61 6e 64 6c 65 72 3b 20 20 20 20 20 20 2f 2a 20 andler; /*
5c250 42 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f Busy callback */
5c260 0a 20 20 69 6e 74 20 62 75 73 79 54 69 6d 65 6f . int busyTimeo
5c270 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ut;
5c280 20 2f 2a 20 42 75 73 79 20 68 61 6e 64 6c 65 72 /* Busy handler
5c290 20 74 69 6d 65 6f 75 74 2c 20 69 6e 20 6d 73 65 timeout, in mse
5c2a0 63 20 2a 2f 0a 20 20 44 62 20 61 44 62 53 74 61 c */. Db aDbSta
5c2b0 74 69 63 5b 32 5d 3b 20 20 20 20 20 20 20 20 20 tic[2];
5c2c0 20 20 20 20 20 2f 2a 20 53 74 61 74 69 63 20 73 /* Static s
5c2d0 70 61 63 65 20 66 6f 72 20 74 68 65 20 32 20 64 pace for the 2 d
5c2e0 65 66 61 75 6c 74 20 62 61 63 6b 65 6e 64 73 20 efault backends
5c2f0 2a 2f 0a 20 20 53 61 76 65 70 6f 69 6e 74 20 2a */. Savepoint *
5c300 70 53 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 20 pSavepoint;
5c310 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 63 /* List of ac
5c320 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20 tive savepoints
5c330 2a 2f 0a 20 20 69 6e 74 20 6e 53 61 76 65 70 6f */. int nSavepo
5c340 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 int;
5c350 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
5c360 6e 6f 6e 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 non-transaction
5c370 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 20 20 savepoints */.
5c380 69 6e 74 20 6e 53 74 61 74 65 6d 65 6e 74 3b 20 int nStatement;
5c390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
5c3a0 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 Number of neste
5c3b0 64 20 73 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e d statement-tran
5c3c0 73 61 63 74 69 6f 6e 73 20 20 2a 2f 0a 20 20 75 sactions */. u
5c3d0 38 20 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 8 isTransactionS
5c3e0 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 2f 2a 20 avepoint; /*
5c3f0 54 72 75 65 20 69 66 20 74 68 65 20 6f 75 74 65 True if the oute
5c400 72 6d 6f 73 74 20 73 61 76 65 70 6f 69 6e 74 20 rmost savepoint
5c410 69 73 20 61 20 54 53 20 2a 2f 0a 20 20 69 36 34 is a TS */. i64
5c420 20 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 3b 20 nDeferredCons;
5c430 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 /* Ne
5c440 74 20 64 65 66 65 72 72 65 64 20 63 6f 6e 73 74 t deferred const
5c450 72 61 69 6e 74 73 20 74 68 69 73 20 74 72 61 6e raints this tran
5c460 73 61 63 74 69 6f 6e 2e 20 2a 2f 0a 0a 23 69 66 saction. */..#if
5c470 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c def SQLITE_ENABL
5c480 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 0a E_UNLOCK_NOTIFY.
5c490 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 /* The followi
5c4a0 6e 67 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 ng variables are
5c4b0 20 61 6c 6c 20 70 72 6f 74 65 63 74 65 64 20 62 all protected b
5c4c0 79 20 74 68 65 20 53 54 41 54 49 43 5f 4d 41 53 y the STATIC_MAS
5c4d0 54 45 52 20 0a 20 20 2a 2a 20 6d 75 74 65 78 2c TER . ** mutex,
5c4e0 20 6e 6f 74 20 62 79 20 73 71 6c 69 74 65 33 2e not by sqlite3.
5c4f0 6d 75 74 65 78 2e 20 54 68 65 79 20 61 72 65 20 mutex. They are
5c500 75 73 65 64 20 62 79 20 63 6f 64 65 20 69 6e 20 used by code in
5c510 6e 6f 74 69 66 79 2e 63 2e 20 0a 20 20 2a 2a 0a notify.c. . **.
5c520 20 20 2a 2a 20 57 68 65 6e 20 58 2e 70 55 6e 6c ** When X.pUnl
5c530 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 59 ockConnection==Y
5c540 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 , that means tha
5c550 74 20 58 20 69 73 20 77 61 69 74 69 6e 67 20 66 t X is waiting f
5c560 6f 72 20 59 20 74 6f 0a 20 20 2a 2a 20 75 6e 6c or Y to. ** unl
5c570 6f 63 6b 20 73 6f 20 74 68 61 74 20 69 74 20 63 ock so that it c
5c580 61 6e 20 70 72 6f 63 65 65 64 2e 0a 20 20 2a 2a an proceed.. **
5c590 0a 20 20 2a 2a 20 57 68 65 6e 20 58 2e 70 42 6c . ** When X.pBl
5c5a0 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e ockingConnection
5c5b0 3d 3d 59 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 ==Y, that means
5c5c0 74 68 61 74 20 73 6f 6d 65 74 68 69 6e 67 20 74 that something t
5c5d0 68 61 74 20 58 20 74 72 69 65 64 0a 20 20 2a 2a hat X tried. **
5c5e0 20 74 72 69 65 64 20 74 6f 20 64 6f 20 72 65 63 tried to do rec
5c5f0 65 6e 74 6c 79 20 66 61 69 6c 65 64 20 77 69 74 ently failed wit
5c600 68 20 61 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b h an SQLITE_LOCK
5c610 45 44 20 65 72 72 6f 72 20 64 75 65 20 74 6f 20 ED error due to
5c620 6c 6f 63 6b 73 0a 20 20 2a 2a 20 68 65 6c 64 20 locks. ** held
5c630 62 79 20 59 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c by Y.. */. sql
5c640 69 74 65 33 20 2a 70 42 6c 6f 63 6b 69 6e 67 43 ite3 *pBlockingC
5c650 6f 6e 6e 65 63 74 69 6f 6e 3b 20 2f 2a 20 43 6f onnection; /* Co
5c660 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 63 61 nnection that ca
5c670 75 73 65 64 20 53 51 4c 49 54 45 5f 4c 4f 43 4b used SQLITE_LOCK
5c680 45 44 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 ED */. sqlite3
5c690 2a 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 *pUnlockConnecti
5c6a0 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a on; /*
5c6b0 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 77 Connection to w
5c6c0 61 74 63 68 20 66 6f 72 20 75 6e 6c 6f 63 6b 20 atch for unlock
5c6d0 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 55 6e 6c 6f */. void *pUnlo
5c6e0 63 6b 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 ckArg;
5c6f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 /* Ar
5c700 67 75 6d 65 6e 74 20 74 6f 20 78 55 6e 6c 6f 63 gument to xUnloc
5c710 6b 4e 6f 74 69 66 79 20 2a 2f 0a 20 20 76 6f 69 kNotify */. voi
5c720 64 20 28 2a 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 d (*xUnlockNotif
5c730 79 29 28 76 6f 69 64 20 2a 2a 2c 20 69 6e 74 29 y)(void **, int)
5c740 3b 20 20 2f 2a 20 55 6e 6c 6f 63 6b 20 6e 6f 74 ; /* Unlock not
5c750 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a ify callback */.
5c760 20 20 73 71 6c 69 74 65 33 20 2a 70 4e 65 78 74 sqlite3 *pNext
5c770 42 6c 6f 63 6b 65 64 3b 20 20 20 20 20 20 20 20 Blocked;
5c780 2f 2a 20 4e 65 78 74 20 69 6e 20 6c 69 73 74 20 /* Next in list
5c790 6f 66 20 61 6c 6c 20 62 6c 6f 63 6b 65 64 20 63 of all blocked c
5c7a0 6f 6e 6e 65 63 74 69 6f 6e 73 20 2a 2f 0a 23 65 onnections */.#e
5c7b0 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 ndif.};../*.** A
5c7c0 20 6d 61 63 72 6f 20 74 6f 20 64 69 73 63 6f 76 macro to discov
5c7d0 65 72 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 er the encoding
5c7e0 6f 66 20 61 20 64 61 74 61 62 61 73 65 2e 0a 2a of a database..*
5c7f0 2f 0a 23 64 65 66 69 6e 65 20 45 4e 43 28 64 62 /.#define ENC(db
5c800 29 20 28 28 64 62 29 2d 3e 61 44 62 5b 30 5d 2e ) ((db)->aDb[0].
5c810 70 53 63 68 65 6d 61 2d 3e 65 6e 63 29 0a 0a 2f pSchema->enc)../
5c820 2a 0a 2a 2a 20 50 6f 73 73 69 62 6c 65 20 76 61 *.** Possible va
5c830 6c 75 65 73 20 66 6f 72 20 74 68 65 20 73 71 6c lues for the sql
5c840 69 74 65 2e 66 6c 61 67 73 20 61 6e 64 20 6f 72 ite.flags and or
5c850 20 44 62 2e 66 6c 61 67 73 20 66 69 65 6c 64 73 Db.flags fields
5c860 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 71 6c 69 74 ..**.** On sqlit
5c870 65 2e 66 6c 61 67 73 2c 20 74 68 65 20 53 51 4c e.flags, the SQL
5c880 49 54 45 5f 49 6e 54 72 61 6e 73 20 76 61 6c 75 ITE_InTrans valu
5c890 65 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 e means that we
5c8a0 68 61 76 65 0a 2a 2a 20 65 78 65 63 75 74 65 64 have.** executed
5c8b0 20 61 20 42 45 47 49 4e 2e 20 20 4f 6e 20 44 62 a BEGIN. On Db
5c8c0 2e 66 6c 61 67 73 2c 20 53 51 4c 49 54 45 5f 49 .flags, SQLITE_I
5c8d0 6e 54 72 61 6e 73 20 6d 65 61 6e 73 20 61 20 73 nTrans means a s
5c8e0 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e tatement.** tran
5c8f0 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 saction is activ
5c900 65 20 6f 6e 20 74 68 61 74 20 70 61 72 74 69 63 e on that partic
5c910 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 66 69 ular database fi
5c920 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 le..*/.#define S
5c930 51 4c 49 54 45 5f 56 64 62 65 54 72 61 63 65 20 QLITE_VdbeTrace
5c940 20 20 20 20 20 30 78 30 30 30 30 30 30 30 31 20 0x00000001
5c950 20 2f 2a 20 54 72 75 65 20 74 6f 20 74 72 61 63 /* True to trac
5c960 65 20 56 44 42 45 20 65 78 65 63 75 74 69 6f 6e e VDBE execution
5c970 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
5c980 54 45 5f 49 6e 54 72 61 6e 73 20 20 20 20 20 20 TE_InTrans
5c990 20 20 30 78 30 30 30 30 30 30 30 38 20 20 2f 2a 0x00000008 /*
5c9a0 20 54 72 75 65 20 69 66 20 69 6e 20 61 20 74 72 True if in a tr
5c9b0 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 ansaction */.#de
5c9c0 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 6e 74 65 fine SQLITE_Inte
5c9d0 72 6e 43 68 61 6e 67 65 73 20 20 30 78 30 30 30 rnChanges 0x000
5c9e0 30 30 30 31 30 20 20 2f 2a 20 55 6e 63 6f 6d 6d 00010 /* Uncomm
5c9f0 69 74 74 65 64 20 48 61 73 68 20 74 61 62 6c 65 itted Hash table
5ca00 20 63 68 61 6e 67 65 73 20 2a 2f 0a 23 64 65 66 changes */.#def
5ca10 69 6e 65 20 53 51 4c 49 54 45 5f 46 75 6c 6c 43 ine SQLITE_FullC
5ca20 6f 6c 4e 61 6d 65 73 20 20 20 30 78 30 30 30 30 olNames 0x0000
5ca30 30 30 32 30 20 20 2f 2a 20 53 68 6f 77 20 66 75 0020 /* Show fu
5ca40 6c 6c 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 ll column names
5ca50 6f 6e 20 53 45 4c 45 43 54 20 2a 2f 0a 23 64 65 on SELECT */.#de
5ca60 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 68 6f 72 fine SQLITE_Shor
5ca70 74 43 6f 6c 4e 61 6d 65 73 20 20 30 78 30 30 30 tColNames 0x000
5ca80 30 30 30 34 30 20 20 2f 2a 20 53 68 6f 77 20 73 00040 /* Show s
5ca90 68 6f 72 74 20 63 6f 6c 75 6d 6e 73 20 6e 61 6d hort columns nam
5caa0 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 es */.#define SQ
5cab0 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 20 LITE_CountRows
5cac0 20 20 20 20 30 78 30 30 30 30 30 30 38 30 20 20 0x00000080
5cad0 2f 2a 20 43 6f 75 6e 74 20 72 6f 77 73 20 63 68 /* Count rows ch
5cae0 61 6e 67 65 64 20 62 79 20 49 4e 53 45 52 54 2c anged by INSERT,
5caf0 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 */.
5cb00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5cb10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
5cb20 20 20 20 44 45 4c 45 54 45 2c 20 6f 72 20 55 50 DELETE, or UP
5cb30 44 41 54 45 20 61 6e 64 20 72 65 74 75 72 6e 20 DATE and return
5cb40 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 */.
5cb50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5cb60 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
5cb70 20 20 74 68 65 20 63 6f 75 6e 74 20 75 73 69 6e the count usin
5cb80 67 20 61 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f g a callback. */
5cb90 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
5cba0 4e 75 6c 6c 43 61 6c 6c 62 61 63 6b 20 20 20 30 NullCallback 0
5cbb0 78 30 30 30 30 30 31 30 30 20 20 2f 2a 20 49 6e x00000100 /* In
5cbc0 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62 61 63 voke the callbac
5cbd0 6b 20 6f 6e 63 65 20 69 66 20 74 68 65 20 2a 2f k once if the */
5cbe0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
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 2f 2a 20 20 20 /*
5cc10 72 65 73 75 6c 74 20 73 65 74 20 69 73 20 65 6d result set is em
5cc20 70 74 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 pty */.#define S
5cc30 51 4c 49 54 45 5f 53 71 6c 54 72 61 63 65 20 20 QLITE_SqlTrace
5cc40 20 20 20 20 20 30 78 30 30 30 30 30 32 30 30 20 0x00000200
5cc50 20 2f 2a 20 44 65 62 75 67 20 70 72 69 6e 74 20 /* Debug print
5cc60 53 51 4c 20 61 73 20 69 74 20 65 78 65 63 75 74 SQL as it execut
5cc70 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 es */.#define SQ
5cc80 4c 49 54 45 5f 56 64 62 65 4c 69 73 74 69 6e 67 LITE_VdbeListing
5cc90 20 20 20 20 30 78 30 30 30 30 30 34 30 30 20 20 0x00000400
5cca0 2f 2a 20 44 65 62 75 67 20 6c 69 73 74 69 6e 67 /* Debug listing
5ccb0 73 20 6f 66 20 56 44 42 45 20 70 72 6f 67 72 61 s of VDBE progra
5ccc0 6d 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 ms */.#define SQ
5ccd0 4c 49 54 45 5f 57 72 69 74 65 53 63 68 65 6d 61 LITE_WriteSchema
5cce0 20 20 20 20 30 78 30 30 30 30 30 38 30 30 20 20 0x00000800
5ccf0 2f 2a 20 4f 4b 20 74 6f 20 75 70 64 61 74 65 20 /* OK to update
5cd00 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 2a 2f SQLITE_MASTER */
5cd10 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
5cd20 4e 6f 52 65 61 64 6c 6f 63 6b 20 20 20 20 20 30 NoReadlock 0
5cd30 78 30 30 30 30 31 30 30 30 20 20 2f 2a 20 52 65 x00001000 /* Re
5cd40 61 64 6c 6f 63 6b 73 20 61 72 65 20 6f 6d 69 74 adlocks are omit
5cd50 74 65 64 20 77 68 65 6e 20 0a 20 20 20 20 20 20 ted when .
5cd60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5cd70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5cd80 20 20 20 20 2a 2a 20 61 63 63 65 73 73 69 6e 67 ** accessing
5cd90 20 72 65 61 64 2d 6f 6e 6c 79 20 64 61 74 61 62 read-only datab
5cda0 61 73 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ases */.#define
5cdb0 53 51 4c 49 54 45 5f 49 67 6e 6f 72 65 43 68 65 SQLITE_IgnoreChe
5cdc0 63 6b 73 20 20 20 30 78 30 30 30 30 32 30 30 30 cks 0x00002000
5cdd0 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 65 6e 66 6f /* Do not enfo
5cde0 72 63 65 20 63 68 65 63 6b 20 63 6f 6e 73 74 72 rce check constr
5cdf0 61 69 6e 74 73 20 2a 2f 0a 23 64 65 66 69 6e 65 aints */.#define
5ce00 20 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f SQLITE_ReadUnco
5ce10 6d 6d 69 74 74 65 64 20 30 78 30 30 30 30 34 30 mmitted 0x000040
5ce20 30 30 20 2f 2a 20 46 6f 72 20 73 68 61 72 65 64 00 /* For shared
5ce30 2d 63 61 63 68 65 20 6d 6f 64 65 20 2a 2f 0a 23 -cache mode */.#
5ce40 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 65 define SQLITE_Le
5ce50 67 61 63 79 46 69 6c 65 46 6d 74 20 20 30 78 30 gacyFileFmt 0x0
5ce60 30 30 30 38 30 30 30 20 20 2f 2a 20 43 72 65 61 0008000 /* Crea
5ce70 74 65 20 6e 65 77 20 64 61 74 61 62 61 73 65 73 te new databases
5ce80 20 69 6e 20 66 6f 72 6d 61 74 20 31 20 2a 2f 0a in format 1 */.
5ce90 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 #define SQLITE_F
5cea0 75 6c 6c 46 53 79 6e 63 20 20 20 20 20 20 30 78 ullFSync 0x
5ceb0 30 30 30 31 30 30 30 30 20 20 2f 2a 20 55 73 65 00010000 /* Use
5cec0 20 66 75 6c 6c 20 66 73 79 6e 63 20 6f 6e 20 74 full fsync on t
5ced0 68 65 20 62 61 63 6b 65 6e 64 20 2a 2f 0a 23 64 he backend */.#d
5cee0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 6f 61 efine SQLITE_Loa
5cef0 64 45 78 74 65 6e 73 69 6f 6e 20 20 30 78 30 30 dExtension 0x00
5cf00 30 32 30 30 30 30 20 20 2f 2a 20 45 6e 61 62 6c 020000 /* Enabl
5cf10 65 20 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e e load_extension
5cf20 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 53 51 4c */..#define SQL
5cf30 49 54 45 5f 52 65 63 6f 76 65 72 79 4d 6f 64 65 ITE_RecoveryMode
5cf40 20 20 20 30 78 30 30 30 34 30 30 30 30 20 20 2f 0x00040000 /
5cf50 2a 20 49 67 6e 6f 72 65 20 73 63 68 65 6d 61 20 * Ignore schema
5cf60 65 72 72 6f 72 73 20 2a 2f 0a 23 64 65 66 69 6e errors */.#defin
5cf70 65 20 53 51 4c 49 54 45 5f 52 65 76 65 72 73 65 e SQLITE_Reverse
5cf80 4f 72 64 65 72 20 20 20 30 78 30 30 31 30 30 30 Order 0x001000
5cf90 30 30 20 20 2f 2a 20 52 65 76 65 72 73 65 20 75 00 /* Reverse u
5cfa0 6e 6f 72 64 65 72 65 64 20 53 45 4c 45 43 54 73 nordered SELECTs
5cfb0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
5cfc0 54 45 5f 52 65 63 54 72 69 67 67 65 72 73 20 20 TE_RecTriggers
5cfd0 20 20 30 78 30 30 32 30 30 30 30 30 20 20 2f 2a 0x00200000 /*
5cfe0 20 45 6e 61 62 6c 65 20 72 65 63 75 72 73 69 76 Enable recursiv
5cff0 65 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 23 64 e triggers */.#d
5d000 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 6f 72 efine SQLITE_For
5d010 65 69 67 6e 4b 65 79 73 20 20 20 20 30 78 30 30 eignKeys 0x00
5d020 34 30 30 30 30 30 20 20 2f 2a 20 45 6e 66 6f 72 400000 /* Enfor
5d030 63 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 ce foreign key c
5d040 6f 6e 73 74 72 61 69 6e 74 73 20 20 2a 2f 0a 0a onstraints */..
5d050 2f 2a 0a 2a 2a 20 50 6f 73 73 69 62 6c 65 20 76 /*.** Possible v
5d060 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 73 71 alues for the sq
5d070 6c 69 74 65 2e 6d 61 67 69 63 20 66 69 65 6c 64 lite.magic field
5d080 2e 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 73 ..** The numbers
5d090 20 61 72 65 20 6f 62 74 61 69 6e 65 64 20 61 74 are obtained at
5d0a0 20 72 61 6e 64 6f 6d 20 61 6e 64 20 68 61 76 65 random and have
5d0b0 20 6e 6f 20 73 70 65 63 69 61 6c 20 6d 65 61 6e no special mean
5d0c0 69 6e 67 2c 20 6f 74 68 65 72 0a 2a 2a 20 74 68 ing, other.** th
5d0d0 61 6e 20 62 65 69 6e 67 20 64 69 73 74 69 6e 63 an being distinc
5d0e0 74 20 66 72 6f 6d 20 6f 6e 65 20 61 6e 6f 74 68 t from one anoth
5d0f0 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 er..*/.#define S
5d100 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e QLITE_MAGIC_OPEN
5d110 20 20 20 20 20 30 78 61 30 32 39 61 36 39 37 20 0xa029a697
5d120 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69 73 20 /* Database is
5d130 6f 70 65 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 open */.#define
5d140 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 43 4c 4f SQLITE_MAGIC_CLO
5d150 53 45 44 20 20 20 30 78 39 66 33 63 32 64 33 33 SED 0x9f3c2d33
5d160 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69 73 /* Database is
5d170 20 63 6c 6f 73 65 64 20 2a 2f 0a 23 64 65 66 69 closed */.#defi
5d180 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f ne SQLITE_MAGIC_
5d190 53 49 43 4b 20 20 20 20 20 30 78 34 62 37 37 31 SICK 0x4b771
5d1a0 32 39 30 20 20 2f 2a 20 45 72 72 6f 72 20 61 6e 290 /* Error an
5d1b0 64 20 61 77 61 69 74 69 6e 67 20 63 6c 6f 73 65 d awaiting close
5d1c0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
5d1d0 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 20 20 TE_MAGIC_BUSY
5d1e0 20 20 30 78 66 30 33 62 37 39 30 36 20 20 2f 2a 0xf03b7906 /*
5d1f0 20 44 61 74 61 62 61 73 65 20 63 75 72 72 65 6e Database curren
5d200 74 6c 79 20 69 6e 20 75 73 65 20 2a 2f 0a 23 64 tly in use */.#d
5d210 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47 efine SQLITE_MAG
5d220 49 43 5f 45 52 52 4f 52 20 20 20 20 30 78 62 35 IC_ERROR 0xb5
5d230 33 35 37 39 33 30 20 20 2f 2a 20 41 6e 20 53 51 357930 /* An SQ
5d240 4c 49 54 45 5f 4d 49 53 55 53 45 20 65 72 72 6f LITE_MISUSE erro
5d250 72 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a 0a 2f r occurred */../
5d260 2a 0a 2a 2a 20 45 61 63 68 20 53 51 4c 20 66 75 *.** Each SQL fu
5d270 6e 63 74 69 6f 6e 20 69 73 20 64 65 66 69 6e 65 nction is define
5d280 64 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 d by an instance
5d290 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
5d2a0 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 g.** structure.
5d2b0 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 A pointer to th
5d2c0 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 is structure is
5d2d0 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 73 71 stored in the sq
5d2e0 6c 69 74 65 2e 61 46 75 6e 63 0a 2a 2a 20 68 61 lite.aFunc.** ha
5d2f0 73 68 20 74 61 62 6c 65 2e 20 20 57 68 65 6e 20 sh table. When
5d300 6d 75 6c 74 69 70 6c 65 20 66 75 6e 63 74 69 6f multiple functio
5d310 6e 73 20 68 61 76 65 20 74 68 65 20 73 61 6d 65 ns have the same
5d320 20 6e 61 6d 65 2c 20 74 68 65 20 68 61 73 68 20 name, the hash
5d330 74 61 62 6c 65 0a 2a 2a 20 70 6f 69 6e 74 73 20 table.** points
5d340 74 6f 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 to a linked list
5d350 20 6f 66 20 74 68 65 73 65 20 73 74 72 75 63 74 of these struct
5d360 75 72 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 ures..*/.struct
5d370 46 75 6e 63 44 65 66 20 7b 0a 20 20 69 31 36 20 FuncDef {. i16
5d380 6e 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 nArg;
5d390 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 /* Number of ar
5d3a0 67 75 6d 65 6e 74 73 2e 20 20 2d 31 20 6d 65 61 guments. -1 mea
5d3b0 6e 73 20 75 6e 6c 69 6d 69 74 65 64 20 2a 2f 0a ns unlimited */.
5d3c0 20 20 75 38 20 69 50 72 65 66 45 6e 63 3b 20 20 u8 iPrefEnc;
5d3d0 20 20 20 20 20 20 20 2f 2a 20 50 72 65 66 65 72 /* Prefer
5d3e0 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69 6e red text encodin
5d3f0 67 20 28 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 g (SQLITE_UTF8,
5d400 31 36 4c 45 2c 20 31 36 42 45 29 20 2a 2f 0a 20 16LE, 16BE) */.
5d410 20 75 38 20 66 6c 61 67 73 3b 20 20 20 20 20 20 u8 flags;
5d420 20 20 20 20 20 20 2f 2a 20 53 6f 6d 65 20 63 6f /* Some co
5d430 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 53 51 4c mbination of SQL
5d440 49 54 45 5f 46 55 4e 43 5f 2a 20 2a 2f 0a 20 20 ITE_FUNC_* */.
5d450 76 6f 69 64 20 2a 70 55 73 65 72 44 61 74 61 3b void *pUserData;
5d460 20 20 20 20 20 2f 2a 20 55 73 65 72 20 64 61 74 /* User dat
5d470 61 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20 a parameter */.
5d480 20 46 75 6e 63 44 65 66 20 2a 70 4e 65 78 74 3b FuncDef *pNext;
5d490 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 66 75 /* Next fu
5d4a0 6e 63 74 69 6f 6e 20 77 69 74 68 20 73 61 6d 65 nction with same
5d4b0 20 6e 61 6d 65 20 2a 2f 0a 20 20 76 6f 69 64 20 name */. void
5d4c0 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 (*xFunc)(sqlite3
5d4d0 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 _context*,int,sq
5d4e0 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 3b 20 lite3_value**);
5d4f0 2f 2a 20 52 65 67 75 6c 61 72 20 66 75 6e 63 74 /* Regular funct
5d500 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a ion */. void (*
5d510 78 53 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63 xStep)(sqlite3_c
5d520 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 ontext*,int,sqli
5d530 74 65 33 5f 76 61 6c 75 65 2a 2a 29 3b 20 2f 2a te3_value**); /*
5d540 20 41 67 67 72 65 67 61 74 65 20 73 74 65 70 20 Aggregate step
5d550 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 6e */. void (*xFin
5d560 61 6c 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 63 alize)(sqlite3_c
5d570 6f 6e 74 65 78 74 2a 29 3b 20 20 20 20 20 20 20 ontext*);
5d580 20 20 20 20 20 20 20 20 20 2f 2a 20 41 67 67 72 /* Aggr
5d590 65 67 61 74 65 20 66 69 6e 61 6c 69 7a 65 72 20 egate finalizer
5d5a0 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 */. char *zName
5d5b0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c ; /* SQL
5d5c0 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 66 75 6e name of the fun
5d5d0 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20 46 75 6e 63 ction. */. Func
5d5e0 44 65 66 20 2a 70 48 61 73 68 3b 20 20 20 20 20 Def *pHash;
5d5f0 20 2f 2a 20 4e 65 78 74 20 77 69 74 68 20 61 20 /* Next with a
5d600 64 69 66 66 65 72 65 6e 74 20 6e 61 6d 65 20 62 different name b
5d610 75 74 20 74 68 65 20 73 61 6d 65 20 68 61 73 68 ut the same hash
5d620 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 6f */.};../*.** Po
5d630 73 73 69 62 6c 65 20 76 61 6c 75 65 73 20 66 6f ssible values fo
5d640 72 20 46 75 6e 63 44 65 66 2e 66 6c 61 67 73 0a r FuncDef.flags.
5d650 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
5d660 45 5f 46 55 4e 43 5f 4c 49 4b 45 20 20 20 20 20 E_FUNC_LIKE
5d670 30 78 30 31 20 2f 2a 20 43 61 6e 64 69 64 61 74 0x01 /* Candidat
5d680 65 20 66 6f 72 20 74 68 65 20 4c 49 4b 45 20 6f e for the LIKE o
5d690 70 74 69 6d 69 7a 61 74 69 6f 6e 20 2a 2f 0a 23 ptimization */.#
5d6a0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 define SQLITE_FU
5d6b0 4e 43 5f 43 41 53 45 20 20 20 20 20 30 78 30 32 NC_CASE 0x02
5d6c0 20 2f 2a 20 43 61 73 65 2d 73 65 6e 73 69 74 69 /* Case-sensiti
5d6d0 76 65 20 4c 49 4b 45 2d 74 79 70 65 20 66 75 6e ve LIKE-type fun
5d6e0 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 ction */.#define
5d6f0 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 45 50 48 SQLITE_FUNC_EPH
5d700 45 4d 20 20 20 20 30 78 30 34 20 2f 2a 20 45 70 EM 0x04 /* Ep
5d710 68 65 6d 65 72 61 6c 2e 20 20 44 65 6c 65 74 65 hemeral. Delete
5d720 20 77 69 74 68 20 56 44 42 45 20 2a 2f 0a 23 64 with VDBE */.#d
5d730 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e efine SQLITE_FUN
5d740 43 5f 4e 45 45 44 43 4f 4c 4c 20 30 78 30 38 20 C_NEEDCOLL 0x08
5d750 2f 2a 20 73 71 6c 69 74 65 33 47 65 74 46 75 6e /* sqlite3GetFun
5d760 63 43 6f 6c 6c 53 65 71 28 29 20 6d 69 67 68 74 cCollSeq() might
5d770 20 62 65 20 63 61 6c 6c 65 64 20 2a 2f 0a 23 64 be called */.#d
5d780 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e efine SQLITE_FUN
5d790 43 5f 50 52 49 56 41 54 45 20 20 30 78 31 30 20 C_PRIVATE 0x10
5d7a0 2f 2a 20 41 6c 6c 6f 77 65 64 20 66 6f 72 20 69 /* Allowed for i
5d7b0 6e 74 65 72 6e 61 6c 20 75 73 65 20 6f 6e 6c 79 nternal use only
5d7c0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
5d7d0 54 45 5f 46 55 4e 43 5f 43 4f 55 4e 54 20 20 20 TE_FUNC_COUNT
5d7e0 20 30 78 32 30 20 2f 2a 20 42 75 69 6c 74 2d 69 0x20 /* Built-i
5d7f0 6e 20 63 6f 75 6e 74 28 2a 29 20 61 67 67 72 65 n count(*) aggre
5d800 67 61 74 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 gate */../*.** T
5d810 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 72 he following thr
5d820 65 65 20 6d 61 63 72 6f 73 2c 20 46 55 4e 43 54 ee macros, FUNCT
5d830 49 4f 4e 28 29 2c 20 4c 49 4b 45 46 55 4e 43 28 ION(), LIKEFUNC(
5d840 29 20 61 6e 64 20 41 47 47 52 45 47 41 54 45 28 ) and AGGREGATE(
5d850 29 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 74 6f ) are.** used to
5d860 20 63 72 65 61 74 65 20 74 68 65 20 69 6e 69 74 create the init
5d870 69 61 6c 69 7a 65 72 73 20 66 6f 72 20 74 68 65 ializers for the
5d880 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 75 FuncDef structu
5d890 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 46 55 4e res..**.** FUN
5d8a0 43 54 49 4f 4e 28 7a 4e 61 6d 65 2c 20 6e 41 72 CTION(zName, nAr
5d8b0 67 2c 20 69 41 72 67 2c 20 62 4e 43 2c 20 78 46 g, iArg, bNC, xF
5d8c0 75 6e 63 29 0a 2a 2a 20 20 20 20 20 55 73 65 64 unc).** Used
5d8d0 20 74 6f 20 63 72 65 61 74 65 20 61 20 73 63 61 to create a sca
5d8e0 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 lar function def
5d8f0 69 6e 69 74 69 6f 6e 20 6f 66 20 61 20 66 75 6e inition of a fun
5d900 63 74 69 6f 6e 20 7a 4e 61 6d 65 20 0a 2a 2a 20 ction zName .**
5d910 20 20 20 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 implemented
5d920 62 79 20 43 20 66 75 6e 63 74 69 6f 6e 20 78 46 by C function xF
5d930 75 6e 63 20 74 68 61 74 20 61 63 63 65 70 74 73 unc that accepts
5d940 20 6e 41 72 67 20 61 72 67 75 6d 65 6e 74 73 2e nArg arguments.
5d950 20 54 68 65 0a 2a 2a 20 20 20 20 20 76 61 6c 75 The.** valu
5d960 65 20 70 61 73 73 65 64 20 61 73 20 69 41 72 67 e passed as iArg
5d970 20 69 73 20 63 61 73 74 20 74 6f 20 61 20 28 76 is cast to a (v
5d980 6f 69 64 2a 29 20 61 6e 64 20 6d 61 64 65 20 61 oid*) and made a
5d990 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 20 20 20 20 vailable.**
5d9a0 61 73 20 74 68 65 20 75 73 65 72 2d 64 61 74 61 as the user-data
5d9b0 20 28 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 (sqlite3_user_d
5d9c0 61 74 61 28 29 29 20 66 6f 72 20 74 68 65 20 66 ata()) for the f
5d9d0 75 6e 63 74 69 6f 6e 2e 20 49 66 20 0a 2a 2a 20 unction. If .**
5d9e0 20 20 20 20 61 72 67 75 6d 65 6e 74 20 62 4e 43 argument bNC
5d9f0 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 is true, then t
5da00 68 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e he SQLITE_FUNC_N
5da10 45 45 44 43 4f 4c 4c 20 66 6c 61 67 20 69 73 20 EEDCOLL flag is
5da20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 41 47 47 set..**.** AGG
5da30 52 45 47 41 54 45 28 7a 4e 61 6d 65 2c 20 6e 41 REGATE(zName, nA
5da40 72 67 2c 20 69 41 72 67 2c 20 62 4e 43 2c 20 78 rg, iArg, bNC, x
5da50 53 74 65 70 2c 20 78 46 69 6e 61 6c 29 0a 2a 2a Step, xFinal).**
5da60 20 20 20 20 20 55 73 65 64 20 74 6f 20 63 72 65 Used to cre
5da70 61 74 65 20 61 6e 20 61 67 67 72 65 67 61 74 65 ate an aggregate
5da80 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 function defini
5da90 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 65 64 tion implemented
5daa0 20 62 79 0a 2a 2a 20 20 20 20 20 74 68 65 20 43 by.** the C
5dab0 20 66 75 6e 63 74 69 6f 6e 73 20 78 53 74 65 70 functions xStep
5dac0 20 61 6e 64 20 78 46 69 6e 61 6c 2e 20 54 68 65 and xFinal. The
5dad0 20 66 69 72 73 74 20 66 6f 75 72 20 70 61 72 61 first four para
5dae0 6d 65 74 65 72 73 0a 2a 2a 20 20 20 20 20 61 72 meters.** ar
5daf0 65 20 69 6e 74 65 72 70 72 65 74 65 64 20 69 6e e interpreted in
5db00 20 74 68 65 20 73 61 6d 65 20 77 61 79 20 61 73 the same way as
5db10 20 74 68 65 20 66 69 72 73 74 20 34 20 70 61 72 the first 4 par
5db20 61 6d 65 74 65 72 73 20 74 6f 0a 2a 2a 20 20 20 ameters to.**
5db30 20 20 46 55 4e 43 54 49 4f 4e 28 29 2e 0a 2a 2a FUNCTION()..**
5db40 0a 2a 2a 20 20 20 4c 49 4b 45 46 55 4e 43 28 7a .** LIKEFUNC(z
5db50 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 70 41 72 67 Name, nArg, pArg
5db60 2c 20 66 6c 61 67 73 29 0a 2a 2a 20 20 20 20 20 , flags).**
5db70 55 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 Used to create a
5db80 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e scalar function
5db90 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 61 definition of a
5dba0 20 66 75 6e 63 74 69 6f 6e 20 7a 4e 61 6d 65 20 function zName
5dbb0 0a 2a 2a 20 20 20 20 20 74 68 61 74 20 61 63 63 .** that acc
5dbc0 65 70 74 73 20 6e 41 72 67 20 61 72 67 75 6d 65 epts nArg argume
5dbd0 6e 74 73 20 61 6e 64 20 69 73 20 69 6d 70 6c 65 nts and is imple
5dbe0 6d 65 6e 74 65 64 20 62 79 20 61 20 63 61 6c 6c mented by a call
5dbf0 20 74 6f 20 43 20 0a 2a 2a 20 20 20 20 20 66 75 to C .** fu
5dc00 6e 63 74 69 6f 6e 20 6c 69 6b 65 46 75 6e 63 2e nction likeFunc.
5dc10 20 41 72 67 75 6d 65 6e 74 20 70 41 72 67 20 69 Argument pArg i
5dc20 73 20 63 61 73 74 20 74 6f 20 61 20 28 76 6f 69 s cast to a (voi
5dc30 64 20 2a 29 20 61 6e 64 20 6d 61 64 65 0a 2a 2a d *) and made.**
5dc40 20 20 20 20 20 61 76 61 69 6c 61 62 6c 65 20 61 available a
5dc50 73 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 75 s the function u
5dc60 73 65 72 2d 64 61 74 61 20 28 73 71 6c 69 74 65 ser-data (sqlite
5dc70 33 5f 75 73 65 72 5f 64 61 74 61 28 29 29 2e 20 3_user_data()).
5dc80 54 68 65 0a 2a 2a 20 20 20 20 20 46 75 6e 63 44 The.** FuncD
5dc90 65 66 2e 66 6c 61 67 73 20 76 61 72 69 61 62 6c ef.flags variabl
5dca0 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 e is set to the
5dcb0 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 73 20 value passed as
5dcc0 74 68 65 20 66 6c 61 67 73 0a 2a 2a 20 20 20 20 the flags.**
5dcd0 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a 23 parameter..*/.#
5dce0 64 65 66 69 6e 65 20 46 55 4e 43 54 49 4f 4e 28 define FUNCTION(
5dcf0 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 69 41 72 zName, nArg, iAr
5dd00 67 2c 20 62 4e 43 2c 20 78 46 75 6e 63 29 20 5c g, bNC, xFunc) \
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 62 4e 43 2a 53 51 4c 49 54 _UTF8, bNC*SQLIT
5dd30 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 2c E_FUNC_NEEDCOLL,
5dd40 20 5c 0a 20 20 20 53 51 4c 49 54 45 5f 49 4e 54 \. SQLITE_INT
5dd50 5f 54 4f 5f 50 54 52 28 69 41 72 67 29 2c 20 30 _TO_PTR(iArg), 0
5dd60 2c 20 78 46 75 6e 63 2c 20 30 2c 20 30 2c 20 23 , xFunc, 0, 0, #
5dd70 7a 4e 61 6d 65 2c 20 30 7d 0a 23 64 65 66 69 6e zName, 0}.#defin
5dd80 65 20 53 54 52 5f 46 55 4e 43 54 49 4f 4e 28 7a e STR_FUNCTION(z
5dd90 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 70 41 72 67 Name, nArg, pArg
5dda0 2c 20 62 4e 43 2c 20 78 46 75 6e 63 29 20 5c 0a , bNC, xFunc) \.
5ddb0 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f {nArg, SQLITE_
5ddc0 55 54 46 38 2c 20 62 4e 43 2a 53 51 4c 49 54 45 UTF8, bNC*SQLITE
5ddd0 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 2c 20 _FUNC_NEEDCOLL,
5dde0 5c 0a 20 20 20 70 41 72 67 2c 20 30 2c 20 78 46 \. pArg, 0, xF
5ddf0 75 6e 63 2c 20 30 2c 20 30 2c 20 23 7a 4e 61 6d unc, 0, 0, #zNam
5de00 65 2c 20 30 7d 0a 23 64 65 66 69 6e 65 20 4c 49 e, 0}.#define LI
5de10 4b 45 46 55 4e 43 28 7a 4e 61 6d 65 2c 20 6e 41 KEFUNC(zName, nA
5de20 72 67 2c 20 61 72 67 2c 20 66 6c 61 67 73 29 20 rg, arg, flags)
5de30 5c 0a 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54 \. {nArg, SQLIT
5de40 45 5f 55 54 46 38 2c 20 66 6c 61 67 73 2c 20 28 E_UTF8, flags, (
5de50 76 6f 69 64 20 2a 29 61 72 67 2c 20 30 2c 20 6c void *)arg, 0, l
5de60 69 6b 65 46 75 6e 63 2c 20 30 2c 20 30 2c 20 23 ikeFunc, 0, 0, #
5de70 7a 4e 61 6d 65 2c 20 30 7d 0a 23 64 65 66 69 6e zName, 0}.#defin
5de80 65 20 41 47 47 52 45 47 41 54 45 28 7a 4e 61 6d e AGGREGATE(zNam
5de90 65 2c 20 6e 41 72 67 2c 20 61 72 67 2c 20 6e 63 e, nArg, arg, nc
5dea0 2c 20 78 53 74 65 70 2c 20 78 46 69 6e 61 6c 29 , xStep, xFinal)
5deb0 20 5c 0a 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49 \. {nArg, SQLI
5dec0 54 45 5f 55 54 46 38 2c 20 6e 63 2a 53 51 4c 49 TE_UTF8, nc*SQLI
5ded0 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c TE_FUNC_NEEDCOLL
5dee0 2c 20 5c 0a 20 20 20 53 51 4c 49 54 45 5f 49 4e , \. SQLITE_IN
5def0 54 5f 54 4f 5f 50 54 52 28 61 72 67 29 2c 20 30 T_TO_PTR(arg), 0
5df00 2c 20 30 2c 20 78 53 74 65 70 2c 78 46 69 6e 61 , 0, xStep,xFina
5df10 6c 2c 23 7a 4e 61 6d 65 2c 30 7d 0a 0a 2f 2a 0a l,#zName,0}../*.
5df20 2a 2a 20 41 6c 6c 20 63 75 72 72 65 6e 74 20 73 ** All current s
5df30 61 76 65 70 6f 69 6e 74 73 20 61 72 65 20 73 74 avepoints are st
5df40 6f 72 65 64 20 69 6e 20 61 20 6c 69 6e 6b 65 64 ored in a linked
5df50 20 6c 69 73 74 20 73 74 61 72 74 69 6e 67 20 61 list starting a
5df60 74 0a 2a 2a 20 73 71 6c 69 74 65 33 2e 70 53 61 t.** sqlite3.pSa
5df70 76 65 70 6f 69 6e 74 2e 20 54 68 65 20 66 69 72 vepoint. The fir
5df80 73 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20 74 68 st element in th
5df90 65 20 6c 69 73 74 20 69 73 20 74 68 65 20 6d 6f e list is the mo
5dfa0 73 74 20 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 6f st recently.** o
5dfb0 70 65 6e 65 64 20 73 61 76 65 70 6f 69 6e 74 2e pened savepoint.
5dfc0 20 53 61 76 65 70 6f 69 6e 74 73 20 61 72 65 20 Savepoints are
5dfd0 61 64 64 65 64 20 74 6f 20 74 68 65 20 6c 69 73 added to the lis
5dfe0 74 20 62 79 20 74 68 65 20 76 64 62 65 0a 2a 2a t by the vdbe.**
5dff0 20 4f 50 5f 53 61 76 65 70 6f 69 6e 74 20 69 6e OP_Savepoint in
5e000 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 struction..*/.st
5e010 72 75 63 74 20 53 61 76 65 70 6f 69 6e 74 20 7b ruct Savepoint {
5e020 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 . char *zName;
5e030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5e040 20 20 20 20 20 20 20 2f 2a 20 53 61 76 65 70 6f /* Savepo
5e050 69 6e 74 20 6e 61 6d 65 20 28 6e 75 6c 2d 74 65 int name (nul-te
5e060 72 6d 69 6e 61 74 65 64 29 20 2a 2f 0a 20 20 69 rminated) */. i
5e070 36 34 20 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 64 nDeferredCons
5e080 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
5e090 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
5e0a0 64 65 66 65 72 72 65 64 20 66 6b 20 76 69 6f 6c deferred fk viol
5e0b0 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 53 61 76 65 ations */. Save
5e0c0 70 6f 69 6e 74 20 2a 70 4e 65 78 74 3b 20 20 20 point *pNext;
5e0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5e0e0 2f 2a 20 50 61 72 65 6e 74 20 73 61 76 65 70 6f /* Parent savepo
5e0f0 69 6e 74 20 28 69 66 20 61 6e 79 29 20 2a 2f 0a int (if any) */.
5e100 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f };../*.** The fo
5e110 6c 6c 6f 77 69 6e 67 20 61 72 65 20 75 73 65 64 llowing are used
5e120 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70 as the second p
5e130 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 arameter to sqli
5e140 74 65 33 53 61 76 65 70 6f 69 6e 74 28 29 2c 0a te3Savepoint(),.
5e150 2a 2a 20 61 6e 64 20 61 73 20 74 68 65 20 50 31 ** and as the P1
5e160 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 argument to the
5e170 20 4f 50 5f 53 61 76 65 70 6f 69 6e 74 20 69 6e OP_Savepoint in
5e180 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 struction..*/.#d
5e190 65 66 69 6e 65 20 53 41 56 45 50 4f 49 4e 54 5f efine SAVEPOINT_
5e1a0 42 45 47 49 4e 20 20 20 20 20 20 30 0a 23 64 65 BEGIN 0.#de
5e1b0 66 69 6e 65 20 53 41 56 45 50 4f 49 4e 54 5f 52 fine SAVEPOINT_R
5e1c0 45 4c 45 41 53 45 20 20 20 20 31 0a 23 64 65 66 ELEASE 1.#def
5e1d0 69 6e 65 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f ine SAVEPOINT_RO
5e1e0 4c 4c 42 41 43 4b 20 20 20 32 0a 0a 0a 2f 2a 0a LLBACK 2.../*.
5e1f0 2a 2a 20 45 61 63 68 20 53 51 4c 69 74 65 20 6d ** Each SQLite m
5e200 6f 64 75 6c 65 20 28 76 69 72 74 75 61 6c 20 74 odule (virtual t
5e210 61 62 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e 29 able definition)
5e220 20 69 73 20 64 65 66 69 6e 65 64 20 62 79 20 61 is defined by a
5e230 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 n.** instance of
5e240 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
5e250 74 72 75 63 74 75 72 65 2c 20 73 74 6f 72 65 64 tructure, stored
5e260 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 2e in the sqlite3.
5e270 61 4d 6f 64 75 6c 65 0a 2a 2a 20 68 61 73 68 20 aModule.** hash
5e280 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 table..*/.struct
5e290 20 4d 6f 64 75 6c 65 20 7b 0a 20 20 63 6f 6e 73 Module {. cons
5e2a0 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 t sqlite3_module
5e2b0 20 2a 70 4d 6f 64 75 6c 65 3b 20 20 20 20 20 20 *pModule;
5e2c0 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 70 6f 69 /* Callback poi
5e2d0 6e 74 65 72 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 nters */. const
5e2e0 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 char *zName;
5e2f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5e300 2f 2a 20 4e 61 6d 65 20 70 61 73 73 65 64 20 74 /* Name passed t
5e310 6f 20 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 o create_module(
5e320 29 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 ) */. void *pAu
5e330 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 x;
5e340 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 /* p
5e350 41 75 78 20 70 61 73 73 65 64 20 74 6f 20 63 72 Aux passed to cr
5e360 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20 2a 2f eate_module() */
5e370 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 73 74 72 . void (*xDestr
5e380 6f 79 29 28 76 6f 69 64 20 2a 29 3b 20 20 20 20 oy)(void *);
5e390 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c /* Modul
5e3a0 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e e destructor fun
5e3b0 63 74 69 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a ction */.};../*.
5e3c0 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 ** information a
5e3d0 62 6f 75 74 20 65 61 63 68 20 63 6f 6c 75 6d 6e bout each column
5e3e0 20 6f 66 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 of an SQL table
5e3f0 20 69 73 20 68 65 6c 64 20 69 6e 20 61 6e 20 69 is held in an i
5e400 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 nstance.** of th
5e410 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f is structure..*/
5e420 0a 73 74 72 75 63 74 20 43 6f 6c 75 6d 6e 20 7b .struct Column {
5e430 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 . char *zName;
5e440 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 /* Name of t
5e450 68 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 his column */.
5e460 45 78 70 72 20 2a 70 44 66 6c 74 3b 20 20 20 20 Expr *pDflt;
5e470 20 2f 2a 20 44 65 66 61 75 6c 74 20 76 61 6c 75 /* Default valu
5e480 65 20 6f 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e e of this column
5e490 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 44 66 6c */. char *zDfl
5e4a0 74 3b 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e t; /* Origin
5e4b0 61 6c 20 74 65 78 74 20 6f 66 20 74 68 65 20 64 al text of the d
5e4c0 65 66 61 75 6c 74 20 76 61 6c 75 65 20 2a 2f 0a efault value */.
5e4d0 20 20 63 68 61 72 20 2a 7a 54 79 70 65 3b 20 20 char *zType;
5e4e0 20 20 20 2f 2a 20 44 61 74 61 20 74 79 70 65 20 /* Data type
5e4f0 66 6f 72 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 for this column
5e500 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c */. char *zColl
5e510 3b 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 ; /* Collati
5e520 6e 67 20 73 65 71 75 65 6e 63 65 2e 20 20 49 66 ng sequence. If
5e530 20 4e 55 4c 4c 2c 20 75 73 65 20 74 68 65 20 64 NULL, use the d
5e540 65 66 61 75 6c 74 20 2a 2f 0a 20 20 75 38 20 6e efault */. u8 n
5e550 6f 74 4e 75 6c 6c 3b 20 20 20 20 20 20 2f 2a 20 otNull; /*
5e560 54 72 75 65 20 69 66 20 74 68 65 72 65 20 69 73 True if there is
5e570 20 61 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 a NOT NULL cons
5e580 74 72 61 69 6e 74 20 2a 2f 0a 20 20 75 38 20 69 traint */. u8 i
5e590 73 50 72 69 6d 4b 65 79 3b 20 20 20 20 2f 2a 20 sPrimKey; /*
5e5a0 54 72 75 65 20 69 66 20 74 68 69 73 20 63 6f 6c True if this col
5e5b0 75 6d 6e 20 69 73 20 70 61 72 74 20 6f 66 20 74 umn is part of t
5e5c0 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 2a he PRIMARY KEY *
5e5d0 2f 0a 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 /. char affinit
5e5e0 79 3b 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 y; /* One of t
5e5f0 68 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 2e 2e he SQLITE_AFF_..
5e600 2e 20 76 61 6c 75 65 73 20 2a 2f 0a 23 69 66 6e . values */.#ifn
5e610 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
5e620 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 75 VIRTUALTABLE. u
5e630 38 20 69 73 48 69 64 64 65 6e 3b 20 20 20 20 20 8 isHidden;
5e640 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 /* True if this
5e650 63 6f 6c 75 6d 6e 20 69 73 20 27 68 69 64 64 65 column is 'hidde
5e660 6e 27 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a n' */.#endif.};.
5e670 0a 2f 2a 0a 2a 2a 20 41 20 22 43 6f 6c 6c 61 74 ./*.** A "Collat
5e680 69 6e 67 20 53 65 71 75 65 6e 63 65 22 20 69 73 ing Sequence" is
5e690 20 64 65 66 69 6e 65 64 20 62 79 20 61 6e 20 69 defined by an i
5e6a0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
5e6b0 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 ollowing.** stru
5e6c0 63 74 75 72 65 2e 20 43 6f 6e 63 65 70 74 75 61 cture. Conceptua
5e6d0 6c 6c 79 2c 20 61 20 63 6f 6c 6c 61 74 69 6e 67 lly, a collating
5e6e0 20 73 65 71 75 65 6e 63 65 20 63 6f 6e 73 69 73 sequence consis
5e6f0 74 73 20 6f 66 20 61 20 6e 61 6d 65 20 61 6e 64 ts of a name and
5e700 0a 2a 2a 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e .** a comparison
5e710 20 72 6f 75 74 69 6e 65 20 74 68 61 74 20 64 65 routine that de
5e720 66 69 6e 65 73 20 74 68 65 20 6f 72 64 65 72 20 fines the order
5e730 6f 66 20 74 68 61 74 20 73 65 71 75 65 6e 63 65 of that sequence
5e740 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 61 ..**.** There ma
5e750 79 20 74 77 6f 20 73 65 70 61 72 61 74 65 20 69 y two separate i
5e760 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f mplementations o
5e770 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 f the collation
5e780 66 75 6e 63 74 69 6f 6e 2c 20 6f 6e 65 0a 2a 2a function, one.**
5e790 20 74 68 61 74 20 70 72 6f 63 65 73 73 65 73 20 that processes
5e7a0 74 65 78 74 20 69 6e 20 55 54 46 2d 38 20 65 6e text in UTF-8 en
5e7b0 63 6f 64 69 6e 67 20 28 43 6f 6c 6c 53 65 71 2e coding (CollSeq.
5e7c0 78 43 6d 70 29 20 61 6e 64 20 61 6e 6f 74 68 65 xCmp) and anothe
5e7d0 72 20 74 68 61 74 0a 2a 2a 20 70 72 6f 63 65 73 r that.** proces
5e7e0 73 65 73 20 74 65 78 74 20 65 6e 63 6f 64 65 64 ses text encoded
5e7f0 20 69 6e 20 55 54 46 2d 31 36 20 28 43 6f 6c 6c in UTF-16 (Coll
5e800 53 65 71 2e 78 43 6d 70 31 36 29 2c 20 75 73 69 Seq.xCmp16), usi
5e810 6e 67 20 74 68 65 20 6d 61 63 68 69 6e 65 0a 2a ng the machine.*
5e820 2a 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 * native byte or
5e830 64 65 72 2e 20 57 68 65 6e 20 61 20 63 6f 6c 6c der. When a coll
5e840 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 69 ation sequence i
5e850 73 20 69 6e 76 6f 6b 65 64 2c 20 53 51 4c 69 74 s invoked, SQLit
5e860 65 20 73 65 6c 65 63 74 73 0a 2a 2a 20 74 68 65 e selects.** the
5e870 20 76 65 72 73 69 6f 6e 20 74 68 61 74 20 77 69 version that wi
5e880 6c 6c 20 72 65 71 75 69 72 65 20 74 68 65 20 6c ll require the l
5e890 65 61 73 74 20 65 78 70 65 6e 73 69 76 65 20 65 east expensive e
5e8a0 6e 63 6f 64 69 6e 67 0a 2a 2a 20 74 72 61 6e 73 ncoding.** trans
5e8b0 6c 61 74 69 6f 6e 73 2c 20 69 66 20 61 6e 79 2e lations, if any.
5e8c0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 6f 6c 6c 53 .**.** The CollS
5e8d0 65 71 2e 70 55 73 65 72 20 6d 65 6d 62 65 72 20 eq.pUser member
5e8e0 76 61 72 69 61 62 6c 65 20 69 73 20 61 6e 20 65 variable is an e
5e8f0 78 74 72 61 20 70 61 72 61 6d 65 74 65 72 20 74 xtra parameter t
5e900 68 61 74 20 70 61 73 73 65 64 20 69 6e 0a 2a 2a hat passed in.**
5e910 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 as the first ar
5e920 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 55 54 gument to the UT
5e930 46 2d 38 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 F-8 comparison f
5e940 75 6e 63 74 69 6f 6e 2c 20 78 43 6d 70 2e 0a 2a unction, xCmp..*
5e950 2a 20 43 6f 6c 6c 53 65 71 2e 70 55 73 65 72 31 * CollSeq.pUser1
5e960 36 20 69 73 20 74 68 65 20 65 71 75 69 76 61 6c 6 is the equival
5e970 65 6e 74 20 66 6f 72 20 74 68 65 20 55 54 46 2d ent for the UTF-
5e980 31 36 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 16 comparison fu
5e990 6e 63 74 69 6f 6e 2c 0a 2a 2a 20 78 43 6d 70 31 nction,.** xCmp1
5e9a0 36 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 62 6f 74 68 6..**.** If both
5e9b0 20 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 20 61 6e CollSeq.xCmp an
5e9c0 64 20 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 31 36 d CollSeq.xCmp16
5e9d0 20 61 72 65 20 4e 55 4c 4c 2c 20 69 74 20 6d 65 are NULL, it me
5e9e0 61 6e 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 ans that the.**
5e9f0 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e collating sequen
5ea00 63 65 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e ce is undefined.
5ea10 20 20 49 6e 64 69 63 65 73 20 62 75 69 6c 74 20 Indices built
5ea20 6f 6e 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 0a on an undefined.
5ea30 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 ** collating seq
5ea40 75 65 6e 63 65 20 6d 61 79 20 6e 6f 74 20 62 65 uence may not be
5ea50 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e read or written
5ea60 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 43 6f 6c 6c ..*/.struct Coll
5ea70 53 65 71 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e Seq {. char *zN
5ea80 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ame; /*
5ea90 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c Name of the col
5eaa0 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 2c lating sequence,
5eab0 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a UTF-8 encoded *
5eac0 2f 0a 20 20 75 38 20 65 6e 63 3b 20 20 20 20 20 /. u8 enc;
5ead0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 /* Tex
5eae0 74 20 65 6e 63 6f 64 69 6e 67 20 68 61 6e 64 6c t encoding handl
5eaf0 65 64 20 62 79 20 78 43 6d 70 28 29 20 2a 2f 0a ed by xCmp() */.
5eb00 20 20 75 38 20 74 79 70 65 3b 20 20 20 20 20 20 u8 type;
5eb10 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f /* One o
5eb20 66 20 74 68 65 20 53 51 4c 49 54 45 5f 43 4f 4c f the SQLITE_COL
5eb30 4c 5f 2e 2e 2e 20 76 61 6c 75 65 73 20 62 65 6c L_... values bel
5eb40 6f 77 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 55 ow */. void *pU
5eb50 73 65 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ser; /*
5eb60 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 First argument
5eb70 74 6f 20 78 43 6d 70 28 29 20 2a 2f 0a 20 20 69 to xCmp() */. i
5eb80 6e 74 20 28 2a 78 43 6d 70 29 28 76 6f 69 64 2a nt (*xCmp)(void*
5eb90 2c 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 ,int, const void
5eba0 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f *, int, const vo
5ebb0 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 id*);. void (*x
5ebc0 44 65 6c 29 28 76 6f 69 64 2a 29 3b 20 20 2f 2a Del)(void*); /*
5ebd0 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20 Destructor for
5ebe0 70 55 73 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a pUser */.};../*.
5ebf0 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 ** Allowed value
5ec00 73 20 6f 66 20 43 6f 6c 6c 53 65 71 2e 74 79 70 s of CollSeq.typ
5ec10 65 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 e:.*/.#define SQ
5ec20 4c 49 54 45 5f 43 4f 4c 4c 5f 42 49 4e 41 52 59 LITE_COLL_BINARY
5ec30 20 20 31 20 20 2f 2a 20 54 68 65 20 64 65 66 61 1 /* The defa
5ec40 75 6c 74 20 6d 65 6d 63 6d 70 28 29 20 63 6f 6c ult memcmp() col
5ec50 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 lating sequence
5ec60 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
5ec70 45 5f 43 4f 4c 4c 5f 4e 4f 43 41 53 45 20 20 32 E_COLL_NOCASE 2
5ec80 20 20 2f 2a 20 54 68 65 20 62 75 69 6c 74 2d 69 /* The built-i
5ec90 6e 20 4e 4f 43 41 53 45 20 63 6f 6c 6c 61 74 69 n NOCASE collati
5eca0 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 23 ng sequence */.#
5ecb0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f define SQLITE_CO
5ecc0 4c 4c 5f 52 45 56 45 52 53 45 20 33 20 20 2f 2a LL_REVERSE 3 /*
5ecd0 20 54 68 65 20 62 75 69 6c 74 2d 69 6e 20 52 45 The built-in RE
5ece0 56 45 52 53 45 20 63 6f 6c 6c 61 74 69 6e 67 20 VERSE collating
5ecf0 73 65 71 75 65 6e 63 65 20 2a 2f 0a 23 64 65 66 sequence */.#def
5ed00 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f ine SQLITE_COLL_
5ed10 55 53 45 52 20 20 20 20 30 20 20 2f 2a 20 41 6e USER 0 /* An
5ed20 79 20 6f 74 68 65 72 20 75 73 65 72 2d 64 65 66 y other user-def
5ed30 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6e 67 20 73 ined collating s
5ed40 65 71 75 65 6e 63 65 20 2a 2f 0a 0a 2f 2a 0a 2a equence */../*.*
5ed50 2a 20 41 20 73 6f 72 74 20 6f 72 64 65 72 20 63 * A sort order c
5ed60 61 6e 20 62 65 20 65 69 74 68 65 72 20 41 53 43 an be either ASC
5ed70 20 6f 72 20 44 45 53 43 2e 0a 2a 2f 0a 23 64 65 or DESC..*/.#de
5ed80 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 4f 5f 41 fine SQLITE_SO_A
5ed90 53 43 20 20 20 20 20 20 20 30 20 20 2f 2a 20 53 SC 0 /* S
5eda0 6f 72 74 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 ort in ascending
5edb0 20 6f 72 64 65 72 20 2a 2f 0a 23 64 65 66 69 6e order */.#defin
5edc0 65 20 53 51 4c 49 54 45 5f 53 4f 5f 44 45 53 43 e SQLITE_SO_DESC
5edd0 20 20 20 20 20 20 31 20 20 2f 2a 20 53 6f 72 74 1 /* Sort
5ede0 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 in ascending or
5edf0 64 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f der */../*.** Co
5ee00 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 74 79 lumn affinity ty
5ee10 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 pes..**.** These
5ee20 20 75 73 65 64 20 74 6f 20 68 61 76 65 20 6d 6e used to have mn
5ee30 65 6d 6f 6e 69 63 20 6e 61 6d 65 20 6c 69 6b 65 emonic name like
5ee40 20 27 69 27 20 66 6f 72 20 53 51 4c 49 54 45 5f 'i' for SQLITE_
5ee50 41 46 46 5f 49 4e 54 45 47 45 52 20 61 6e 64 0a AFF_INTEGER and.
5ee60 2a 2a 20 27 74 27 20 66 6f 72 20 53 51 4c 49 54 ** 't' for SQLIT
5ee70 45 5f 41 46 46 5f 54 45 58 54 2e 20 20 42 75 74 E_AFF_TEXT. But
5ee80 20 77 65 20 63 61 6e 20 73 61 76 65 20 61 20 6c we can save a l
5ee90 69 74 74 6c 65 20 73 70 61 63 65 20 61 6e 64 20 ittle space and
5eea0 69 6d 70 72 6f 76 65 0a 2a 2a 20 74 68 65 20 73 improve.** the s
5eeb0 70 65 65 64 20 61 20 6c 69 74 74 6c 65 20 62 79 peed a little by
5eec0 20 6e 75 6d 62 65 72 69 6e 67 20 74 68 65 20 76 numbering the v
5eed0 61 6c 75 65 73 20 63 6f 6e 73 65 63 75 74 69 76 alues consecutiv
5eee0 65 6c 79 2e 20 20 0a 2a 2a 0a 2a 2a 20 42 75 74 ely. .**.** But
5eef0 20 72 61 74 68 65 72 20 74 68 61 6e 20 73 74 61 rather than sta
5ef00 72 74 20 77 69 74 68 20 30 20 6f 72 20 31 2c 20 rt with 0 or 1,
5ef10 77 65 20 62 65 67 69 6e 20 77 69 74 68 20 27 61 we begin with 'a
5ef20 27 2e 20 20 54 68 61 74 20 77 61 79 2c 0a 2a 2a '. That way,.**
5ef30 20 77 68 65 6e 20 6d 75 6c 74 69 70 6c 65 20 61 when multiple a
5ef40 66 66 69 6e 69 74 79 20 74 79 70 65 73 20 61 72 ffinity types ar
5ef50 65 20 63 6f 6e 63 61 74 65 6e 61 74 65 64 20 69 e concatenated i
5ef60 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 61 6e 64 nto a string and
5ef70 0a 2a 2a 20 75 73 65 64 20 61 73 20 74 68 65 20 .** used as the
5ef80 50 34 20 6f 70 65 72 61 6e 64 2c 20 74 68 65 79 P4 operand, they
5ef90 20 77 69 6c 6c 20 62 65 20 6d 6f 72 65 20 72 65 will be more re
5efa0 61 64 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f adable..**.** No
5efb0 74 65 20 61 6c 73 6f 20 74 68 61 74 20 74 68 65 te also that the
5efc0 20 6e 75 6d 65 72 69 63 20 74 79 70 65 73 20 61 numeric types a
5efd0 72 65 20 67 72 6f 75 70 65 64 20 74 6f 67 65 74 re grouped toget
5efe0 68 65 72 20 73 6f 20 74 68 61 74 20 74 65 73 74 her so that test
5eff0 69 6e 67 0a 2a 2a 20 66 6f 72 20 61 20 6e 75 6d ing.** for a num
5f000 65 72 69 63 20 74 79 70 65 20 69 73 20 61 20 73 eric type is a s
5f010 69 6e 67 6c 65 20 63 6f 6d 70 61 72 69 73 6f 6e ingle comparison
5f020 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c ..*/.#define SQL
5f030 49 54 45 5f 41 46 46 5f 54 45 58 54 20 20 20 20 ITE_AFF_TEXT
5f040 20 27 61 27 0a 23 64 65 66 69 6e 65 20 53 51 4c 'a'.#define SQL
5f050 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 20 20 20 ITE_AFF_NONE
5f060 20 27 62 27 0a 23 64 65 66 69 6e 65 20 53 51 4c 'b'.#define SQL
5f070 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 ITE_AFF_NUMERIC
5f080 20 27 63 27 0a 23 64 65 66 69 6e 65 20 53 51 4c 'c'.#define SQL
5f090 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 ITE_AFF_INTEGER
5f0a0 20 27 64 27 0a 23 64 65 66 69 6e 65 20 53 51 4c 'd'.#define SQL
5f0b0 49 54 45 5f 41 46 46 5f 52 45 41 4c 20 20 20 20 ITE_AFF_REAL
5f0c0 20 27 65 27 0a 0a 23 64 65 66 69 6e 65 20 73 71 'e'..#define sq
5f0d0 6c 69 74 65 33 49 73 4e 75 6d 65 72 69 63 41 66 lite3IsNumericAf
5f0e0 66 69 6e 69 74 79 28 58 29 20 20 28 28 58 29 3e finity(X) ((X)>
5f0f0 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 =SQLITE_AFF_NUME
5f100 52 49 43 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 RIC)../*.** The
5f110 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b 20 SQLITE_AFF_MASK
5f120 76 61 6c 75 65 73 20 6d 61 73 6b 73 20 6f 66 66 values masks off
5f130 20 74 68 65 20 73 69 67 6e 69 66 69 63 61 6e 74 the significant
5f140 20 62 69 74 73 20 6f 66 20 61 6e 0a 2a 2a 20 61 bits of an.** a
5f150 66 66 69 6e 69 74 79 20 76 61 6c 75 65 2e 20 0a ffinity value. .
5f160 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
5f170 45 5f 41 46 46 5f 4d 41 53 4b 20 20 20 20 20 30 E_AFF_MASK 0
5f180 78 36 37 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 69 74 x67../*.** Addit
5f190 69 6f 6e 61 6c 20 62 69 74 20 76 61 6c 75 65 73 ional bit values
5f1a0 20 74 68 61 74 20 63 61 6e 20 62 65 20 4f 52 65 that can be ORe
5f1b0 64 20 77 69 74 68 20 61 6e 20 61 66 66 69 6e 69 d with an affini
5f1c0 74 79 20 77 69 74 68 6f 75 74 0a 2a 2a 20 63 68 ty without.** ch
5f1d0 61 6e 67 69 6e 67 20 74 68 65 20 61 66 66 69 6e anging the affin
5f1e0 69 74 79 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ity..*/.#define
5f1f0 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c SQLITE_JUMPIFNUL
5f200 4c 20 20 20 30 78 30 38 20 20 2f 2a 20 6a 75 6d L 0x08 /* jum
5f210 70 73 20 69 66 20 65 69 74 68 65 72 20 6f 70 65 ps if either ope
5f220 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 2a 2f 0a rand is NULL */.
5f230 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 #define SQLITE_S
5f240 54 4f 52 45 50 32 20 20 20 20 20 20 30 78 31 30 TOREP2 0x10
5f250 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c /* Store resul
5f260 74 20 69 6e 20 72 65 67 5b 50 32 5d 20 72 61 74 t in reg[P2] rat
5f270 68 65 72 20 74 68 61 6e 20 6a 75 6d 70 20 2a 2f her than jump */
5f280 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
5f290 4e 55 4c 4c 45 51 20 20 20 20 20 20 20 30 78 38 NULLEQ 0x8
5f2a0 30 20 20 2f 2a 20 4e 55 4c 4c 3d 4e 55 4c 4c 20 0 /* NULL=NULL
5f2b0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 6f 62 6a */../*.** An obj
5f2c0 65 63 74 20 6f 66 20 74 68 69 73 20 74 79 70 65 ect of this type
5f2d0 20 69 73 20 63 72 65 61 74 65 64 20 66 6f 72 20 is created for
5f2e0 65 61 63 68 20 76 69 72 74 75 61 6c 20 74 61 62 each virtual tab
5f2f0 6c 65 20 70 72 65 73 65 6e 74 20 69 6e 0a 2a 2a le present in.**
5f300 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 the database sc
5f310 68 65 6d 61 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 hema. .**.** If
5f320 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 the database sch
5f330 65 6d 61 20 69 73 20 73 68 61 72 65 64 2c 20 74 ema is shared, t
5f340 68 65 6e 20 74 68 65 72 65 20 69 73 20 6f 6e 65 hen there is one
5f350 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 instance of thi
5f360 73 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 66 s.** structure f
5f370 6f 72 20 65 61 63 68 20 64 61 74 61 62 61 73 65 or each database
5f380 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 28 73 71 6c connection (sql
5f390 69 74 65 33 2a 29 20 74 68 61 74 20 75 73 65 73 ite3*) that uses
5f3a0 20 74 68 65 20 73 68 61 72 65 64 0a 2a 2a 20 73 the shared.** s
5f3b0 63 68 65 6d 61 2e 20 54 68 69 73 20 69 73 20 62 chema. This is b
5f3c0 65 63 61 75 73 65 20 65 61 63 68 20 64 61 74 61 ecause each data
5f3d0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
5f3e0 72 65 71 75 69 72 65 73 20 69 74 73 20 6f 77 6e requires its own
5f3f0 20 75 6e 69 71 75 65 0a 2a 2a 20 69 6e 73 74 61 unique.** insta
5f400 6e 63 65 20 6f 66 20 74 68 65 20 73 71 6c 69 74 nce of the sqlit
5f410 65 33 5f 76 74 61 62 2a 20 68 61 6e 64 6c 65 20 e3_vtab* handle
5f420 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 used to access t
5f430 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 he virtual table
5f440 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 .** implementat
5f450 69 6f 6e 2e 20 73 71 6c 69 74 65 33 5f 76 74 61 ion. sqlite3_vta
5f460 62 2a 20 68 61 6e 64 6c 65 73 20 63 61 6e 20 6e b* handles can n
5f470 6f 74 20 62 65 20 73 68 61 72 65 64 20 62 65 74 ot be shared bet
5f480 77 65 65 6e 20 0a 2a 2a 20 64 61 74 61 62 61 73 ween .** databas
5f490 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2c 20 65 e connections, e
5f4a0 76 65 6e 20 77 68 65 6e 20 74 68 65 20 72 65 73 ven when the res
5f4b0 74 20 6f 66 20 74 68 65 20 69 6e 2d 6d 65 6d 6f t of the in-memo
5f4c0 72 79 20 64 61 74 61 62 61 73 65 20 0a 2a 2a 20 ry database .**
5f4d0 73 63 68 65 6d 61 20 69 73 20 73 68 61 72 65 64 schema is shared
5f4e0 2c 20 61 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 , as the impleme
5f4f0 6e 74 61 74 69 6f 6e 20 6f 66 74 65 6e 20 73 74 ntation often st
5f500 6f 72 65 73 20 74 68 65 20 64 61 74 61 62 61 73 ores the databas
5f510 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 e.** connection
5f520 68 61 6e 64 6c 65 20 70 61 73 73 65 64 20 74 6f handle passed to
5f530 20 69 74 20 76 69 61 20 74 68 65 20 78 43 6f 6e it via the xCon
5f540 6e 65 63 74 28 29 20 6f 72 20 78 43 72 65 61 74 nect() or xCreat
5f550 65 28 29 20 6d 65 74 68 6f 64 0a 2a 2a 20 64 75 e() method.** du
5f560 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 ring initializat
5f570 69 6f 6e 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 20 ion internally.
5f580 54 68 69 73 20 64 61 74 61 62 61 73 65 20 63 6f This database co
5f590 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 nnection handle
5f5a0 6d 61 79 0a 2a 2a 20 74 68 65 6e 20 75 73 65 64 may.** then used
5f5b0 20 62 79 20 74 68 65 20 76 69 72 74 75 61 6c 20 by the virtual
5f5c0 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 table implementa
5f5d0 74 69 6f 6e 20 74 6f 20 61 63 63 65 73 73 20 72 tion to access r
5f5e0 65 61 6c 20 74 61 62 6c 65 73 20 0a 2a 2a 20 77 eal tables .** w
5f5f0 69 74 68 69 6e 20 74 68 65 20 64 61 74 61 62 61 ithin the databa
5f600 73 65 2e 20 53 6f 20 74 68 61 74 20 74 68 65 79 se. So that they
5f610 20 61 70 70 65 61 72 20 61 73 20 70 61 72 74 20 appear as part
5f620 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 73 20 0a of the callers .
5f630 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 ** transaction,
5f640 74 68 65 73 65 20 61 63 63 65 73 73 65 73 20 6e these accesses n
5f650 65 65 64 20 74 6f 20 62 65 20 6d 61 64 65 20 76 eed to be made v
5f660 69 61 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 ia the same data
5f670 62 61 73 65 20 0a 2a 2a 20 63 6f 6e 6e 65 63 74 base .** connect
5f680 69 6f 6e 20 61 73 20 74 68 61 74 20 75 73 65 64 ion as that used
5f690 20 74 6f 20 65 78 65 63 75 74 65 20 53 51 4c 20 to execute SQL
5f6a0 6f 70 65 72 61 74 69 6f 6e 73 20 6f 6e 20 74 68 operations on th
5f6b0 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e e virtual table.
5f6c0 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 56 54 61 62 6c .**.** All VTabl
5f6d0 65 20 6f 62 6a 65 63 74 73 20 74 68 61 74 20 63 e objects that c
5f6e0 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20 61 20 73 orrespond to a s
5f6f0 69 6e 67 6c 65 20 74 61 62 6c 65 20 69 6e 20 61 ingle table in a
5f700 20 73 68 61 72 65 64 0a 2a 2a 20 64 61 74 61 62 shared.** datab
5f710 61 73 65 20 73 63 68 65 6d 61 20 61 72 65 20 69 ase schema are i
5f720 6e 69 74 69 61 6c 6c 79 20 73 74 6f 72 65 64 20 nitially stored
5f730 69 6e 20 61 20 6c 69 6e 6b 65 64 2d 6c 69 73 74 in a linked-list
5f740 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 0a 2a pointed to by.*
5f750 2a 20 74 68 65 20 54 61 62 6c 65 2e 70 56 54 61 * the Table.pVTa
5f760 62 6c 65 20 6d 65 6d 62 65 72 20 76 61 72 69 61 ble member varia
5f770 62 6c 65 20 6f 66 20 74 68 65 20 63 6f 72 72 65 ble of the corre
5f780 73 70 6f 6e 64 69 6e 67 20 54 61 62 6c 65 20 6f sponding Table o
5f790 62 6a 65 63 74 2e 0a 2a 2a 20 57 68 65 6e 20 61 bject..** When a
5f7a0 6e 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 n sqlite3_prepar
5f7b0 65 28 29 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 e() operation is
5f7c0 20 72 65 71 75 69 72 65 64 20 74 6f 20 61 63 63 required to acc
5f7d0 65 73 73 20 74 68 65 20 76 69 72 74 75 61 6c 0a ess the virtual.
5f7e0 2a 2a 20 74 61 62 6c 65 2c 20 69 74 20 73 65 61 ** table, it sea
5f7f0 72 63 68 65 73 20 74 68 65 20 6c 69 73 74 20 66 rches the list f
5f800 6f 72 20 74 68 65 20 56 54 61 62 6c 65 20 74 68 or the VTable th
5f810 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 at corresponds t
5f820 6f 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 o the.** databas
5f830 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 6f 69 e connection doi
5f840 6e 67 20 74 68 65 20 70 72 65 70 61 72 69 6e 67 ng the preparing
5f850 20 73 6f 20 61 73 20 74 6f 20 75 73 65 20 74 68 so as to use th
5f860 65 20 63 6f 72 72 65 63 74 0a 2a 2a 20 73 71 6c e correct.** sql
5f870 69 74 65 33 5f 76 74 61 62 2a 20 68 61 6e 64 6c ite3_vtab* handl
5f880 65 20 69 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 e in the compile
5f890 64 20 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 57 d query..**.** W
5f8a0 68 65 6e 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 hen an in-memory
5f8b0 20 54 61 62 6c 65 20 6f 62 6a 65 63 74 20 69 73 Table object is
5f8c0 20 64 65 6c 65 74 65 64 20 28 66 6f 72 20 65 78 deleted (for ex
5f8d0 61 6d 70 6c 65 20 77 68 65 6e 20 74 68 65 0a 2a ample when the.*
5f8e0 2a 20 73 63 68 65 6d 61 20 69 73 20 62 65 69 6e * schema is bein
5f8f0 67 20 72 65 6c 6f 61 64 65 64 20 66 6f 72 20 73 g reloaded for s
5f900 6f 6d 65 20 72 65 61 73 6f 6e 29 2c 20 74 68 65 ome reason), the
5f910 20 56 54 61 62 6c 65 20 6f 62 6a 65 63 74 73 20 VTable objects
5f920 61 72 65 20 6e 6f 74 20 0a 2a 2a 20 64 65 6c 65 are not .** dele
5f930 74 65 64 20 61 6e 64 20 74 68 65 20 73 71 6c 69 ted and the sqli
5f940 74 65 33 5f 76 74 61 62 2a 20 68 61 6e 64 6c 65 te3_vtab* handle
5f950 73 20 61 72 65 20 6e 6f 74 20 78 44 69 73 63 6f s are not xDisco
5f960 6e 6e 65 63 74 28 29 65 64 20 0a 2a 2a 20 69 6d nnect()ed .** im
5f970 6d 65 64 69 61 74 65 6c 79 2e 20 49 6e 73 74 65 mediately. Inste
5f980 61 64 2c 20 74 68 65 79 20 61 72 65 20 6d 6f 76 ad, they are mov
5f990 65 64 20 66 72 6f 6d 20 74 68 65 20 54 61 62 6c ed from the Tabl
5f9a0 65 2e 70 56 54 61 62 6c 65 20 6c 69 73 74 20 74 e.pVTable list t
5f9b0 6f 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 6c 69 6e o.** another lin
5f9c0 6b 65 64 20 6c 69 73 74 20 68 65 61 64 65 64 20 ked list headed
5f9d0 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 2e 70 by the sqlite3.p
5f9e0 44 69 73 63 6f 6e 6e 65 63 74 20 6d 65 6d 62 65 Disconnect membe
5f9f0 72 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 72 72 r of the.** corr
5fa00 65 73 70 6f 6e 64 69 6e 67 20 73 71 6c 69 74 65 esponding sqlite
5fa10 33 20 73 74 72 75 63 74 75 72 65 2e 20 54 68 65 3 structure. The
5fa20 79 20 61 72 65 20 74 68 65 6e 20 64 65 6c 65 74 y are then delet
5fa30 65 64 2f 78 44 69 73 63 6f 6e 6e 65 63 74 65 64 ed/xDisconnected
5fa40 20 0a 2a 2a 20 6e 65 78 74 20 74 69 6d 65 20 61 .** next time a
5fa50 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 70 72 statement is pr
5fa60 65 70 61 72 65 64 20 75 73 69 6e 67 20 73 61 69 epared using sai
5fa70 64 20 73 71 6c 69 74 65 33 2a 2e 20 54 68 69 73 d sqlite3*. This
5fa80 20 69 73 20 64 6f 6e 65 0a 2a 2a 20 74 6f 20 61 is done.** to a
5fa90 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b 20 69 73 void deadlock is
5faa0 73 75 65 73 20 69 6e 76 6f 6c 76 69 6e 67 20 6d sues involving m
5fab0 75 6c 74 69 70 6c 65 20 73 71 6c 69 74 65 33 2e ultiple sqlite3.
5fac0 6d 75 74 65 78 20 6d 75 74 65 78 65 73 2e 0a 2a mutex mutexes..*
5fad0 2a 20 52 65 66 65 72 20 74 6f 20 63 6f 6d 6d 65 * Refer to comme
5fae0 6e 74 73 20 61 62 6f 76 65 20 66 75 6e 63 74 69 nts above functi
5faf0 6f 6e 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e on sqlite3VtabUn
5fb00 6c 6f 63 6b 4c 69 73 74 28 29 20 66 6f 72 20 61 lockList() for a
5fb10 6e 0a 2a 2a 20 65 78 70 6c 61 6e 61 74 69 6f 6e n.** explanation
5fb20 20 61 73 20 74 6f 20 77 68 79 20 69 74 20 69 73 as to why it is
5fb30 20 73 61 66 65 20 74 6f 20 61 64 64 20 61 6e 20 safe to add an
5fb40 65 6e 74 72 79 20 74 6f 20 61 6e 20 73 71 6c 69 entry to an sqli
5fb50 74 65 33 2e 70 44 69 73 63 6f 6e 6e 65 63 74 0a te3.pDisconnect.
5fb60 2a 2a 20 6c 69 73 74 20 77 69 74 68 6f 75 74 20 ** list without
5fb70 68 6f 6c 64 69 6e 67 20 74 68 65 20 63 6f 72 72 holding the corr
5fb80 65 73 70 6f 6e 64 69 6e 67 20 73 71 6c 69 74 65 esponding sqlite
5fb90 33 2e 6d 75 74 65 78 20 6d 75 74 65 78 2e 0a 2a 3.mutex mutex..*
5fba0 2a 0a 2a 2a 20 54 68 65 20 6d 65 6d 6f 72 79 20 *.** The memory
5fbb0 66 6f 72 20 6f 62 6a 65 63 74 73 20 6f 66 20 74 for objects of t
5fbc0 68 69 73 20 74 79 70 65 20 69 73 20 61 6c 77 61 his type is alwa
5fbd0 79 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 ys allocated by
5fbe0 0a 2a 2a 20 73 71 6c 69 74 65 33 44 62 4d 61 6c .** sqlite3DbMal
5fbf0 6c 6f 63 28 29 2c 20 75 73 69 6e 67 20 74 68 65 loc(), using the
5fc00 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 connection hand
5fc10 6c 65 20 73 74 6f 72 65 64 20 69 6e 20 56 54 61 le stored in VTa
5fc20 62 6c 65 2e 64 62 20 61 73 20 0a 2a 2a 20 74 68 ble.db as .** th
5fc30 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
5fc40 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56 54 61 62 ..*/.struct VTab
5fc50 6c 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a le {. sqlite3 *
5fc60 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 db;
5fc70 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e /* Database con
5fc80 6e 65 63 74 69 6f 6e 20 61 73 73 6f 63 69 61 74 nection associat
5fc90 65 64 20 77 69 74 68 20 74 68 69 73 20 74 61 62 ed with this tab
5fca0 6c 65 20 2a 2f 0a 20 20 4d 6f 64 75 6c 65 20 2a le */. Module *
5fcb0 70 4d 6f 64 3b 20 20 20 20 20 20 20 20 20 20 20 pMod;
5fcc0 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
5fcd0 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 module implement
5fce0 61 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 ation */. sqlit
5fcf0 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 20 e3_vtab *pVtab;
5fd00 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
5fd10 74 6f 20 76 74 61 62 20 69 6e 73 74 61 6e 63 65 to vtab instance
5fd20 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 */. int nRef;
5fd30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5fd40 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 /* Number of poi
5fd50 6e 74 65 72 73 20 74 6f 20 74 68 69 73 20 73 74 nters to this st
5fd60 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 56 54 61 ructure */. VTa
5fd70 62 6c 65 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 ble *pNext;
5fd80 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 69 /* Next i
5fd90 6e 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 28 73 n linked list (s
5fda0 65 65 20 61 62 6f 76 65 29 20 2a 2f 0a 7d 3b 0a ee above) */.};.
5fdb0 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 51 4c 20 ./*.** Each SQL
5fdc0 74 61 62 6c 65 20 69 73 20 72 65 70 72 65 73 65 table is represe
5fdd0 6e 74 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 62 nted in memory b
5fde0 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 y an instance of
5fdf0 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e the.** followin
5fe00 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a g structure..**.
5fe10 2a 2a 20 54 61 62 6c 65 2e 7a 4e 61 6d 65 20 69 ** Table.zName i
5fe20 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 s the name of th
5fe30 65 20 74 61 62 6c 65 2e 20 20 54 68 65 20 63 61 e table. The ca
5fe40 73 65 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e se of the origin
5fe50 61 6c 0a 2a 2a 20 43 52 45 41 54 45 20 54 41 42 al.** CREATE TAB
5fe60 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 LE statement is
5fe70 73 74 6f 72 65 64 2c 20 62 75 74 20 63 61 73 65 stored, but case
5fe80 20 69 73 20 6e 6f 74 20 73 69 67 6e 69 66 69 63 is not signific
5fe90 61 6e 74 20 66 6f 72 0a 2a 2a 20 63 6f 6d 70 61 ant for.** compa
5fea0 72 69 73 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 61 risons..**.** Ta
5feb0 62 6c 65 2e 6e 43 6f 6c 20 69 73 20 74 68 65 20 ble.nCol is the
5fec0 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e number of column
5fed0 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65 2e s in this table.
5fee0 20 20 54 61 62 6c 65 2e 61 43 6f 6c 20 69 73 20 Table.aCol is
5fef0 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 a.** pointer to
5ff00 61 6e 20 61 72 72 61 79 20 6f 66 20 43 6f 6c 75 an array of Colu
5ff10 6d 6e 20 73 74 72 75 63 74 75 72 65 73 2c 20 6f mn structures, o
5ff20 6e 65 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 ne for each colu
5ff30 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 mn..**.** If the
5ff40 20 74 61 62 6c 65 20 68 61 73 20 61 6e 20 49 4e table has an IN
5ff50 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE
5ff60 59 2c 20 74 68 65 6e 20 54 61 62 6c 65 2e 69 50 Y, then Table.iP
5ff70 4b 65 79 20 69 73 20 74 68 65 20 69 6e 64 65 78 Key is the index
5ff80 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d of.** the colum
5ff90 6e 20 74 68 61 74 20 69 73 20 74 68 61 74 20 6b n that is that k
5ffa0 65 79 2e 20 20 20 4f 74 68 65 72 77 69 73 65 20 ey. Otherwise
5ffb0 54 61 62 6c 65 2e 69 50 4b 65 79 20 69 73 20 6e Table.iPKey is n
5ffc0 65 67 61 74 69 76 65 2e 20 20 4e 6f 74 65 0a 2a egative. Note.*
5ffd0 2a 20 74 68 61 74 20 74 68 65 20 64 61 74 61 74 * that the datat
5ffe0 79 70 65 20 6f 66 20 74 68 65 20 50 52 49 4d 41 ype of the PRIMA
5fff0 52 59 20 4b 45 59 20 6d 75 73 74 20 62 65 20 49 RY KEY must be I
60000 4e 54 45 47 45 52 20 66 6f 72 20 74 68 69 73 20 NTEGER for this
60010 66 69 65 6c 64 20 74 6f 0a 2a 2a 20 62 65 20 73 field to.** be s
60020 65 74 2e 20 20 41 6e 20 49 4e 54 45 47 45 52 20 et. An INTEGER
60030 50 52 49 4d 41 52 59 20 4b 45 59 20 69 73 20 75 PRIMARY KEY is u
60040 73 65 64 20 61 73 20 74 68 65 20 72 6f 77 69 64 sed as the rowid
60050 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 6f 66 for each row of
60060 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 2e 20 20 .** the table.
60070 49 66 20 61 20 74 61 62 6c 65 20 68 61 73 20 6e If a table has n
60080 6f 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 o INTEGER PRIMAR
60090 59 20 4b 45 59 2c 20 74 68 65 6e 20 61 20 72 61 Y KEY, then a ra
600a0 6e 64 6f 6d 20 72 6f 77 69 64 0a 2a 2a 20 69 73 ndom rowid.** is
600b0 20 67 65 6e 65 72 61 74 65 64 20 66 6f 72 20 65 generated for e
600c0 61 63 68 20 72 6f 77 20 6f 66 20 74 68 65 20 74 ach row of the t
600d0 61 62 6c 65 2e 20 20 54 46 5f 48 61 73 50 72 69 able. TF_HasPri
600e0 6d 61 72 79 4b 65 79 20 69 73 20 73 65 74 20 69 maryKey is set i
600f0 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 68 f.** the table h
60100 61 73 20 61 6e 79 20 50 52 49 4d 41 52 59 20 4b as any PRIMARY K
60110 45 59 2c 20 49 4e 54 45 47 45 52 20 6f 72 20 6f EY, INTEGER or o
60120 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 therwise..**.**
60130 54 61 62 6c 65 2e 74 6e 75 6d 20 69 73 20 74 68 Table.tnum is th
60140 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66 6f e page number fo
60150 72 20 74 68 65 20 72 6f 6f 74 20 42 54 72 65 65 r the root BTree
60160 20 70 61 67 65 20 6f 66 20 74 68 65 20 74 61 62 page of the tab
60170 6c 65 20 69 6e 20 74 68 65 0a 2a 2a 20 64 61 74 le in the.** dat
60180 61 62 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20 abase file. If
60190 54 61 62 6c 65 2e 69 44 62 20 69 73 20 74 68 65 Table.iDb is the
601a0 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 index of the da
601b0 74 61 62 61 73 65 20 74 61 62 6c 65 20 62 61 63 tabase table bac
601c0 6b 65 6e 64 0a 2a 2a 20 69 6e 20 73 71 6c 69 74 kend.** in sqlit
601d0 65 2e 61 44 62 5b 5d 2e 20 20 30 20 69 73 20 66 e.aDb[]. 0 is f
601e0 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 or the main data
601f0 62 61 73 65 20 61 6e 64 20 31 20 69 73 20 66 6f base and 1 is fo
60200 72 20 74 68 65 20 66 69 6c 65 20 74 68 61 74 0a r the file that.
60210 2a 2a 20 68 6f 6c 64 73 20 74 65 6d 70 6f 72 61 ** holds tempora
60220 72 79 20 74 61 62 6c 65 73 20 61 6e 64 20 69 6e ry tables and in
60230 64 69 63 65 73 2e 20 20 49 66 20 54 46 5f 45 70 dices. If TF_Ep
60240 68 65 6d 65 72 61 6c 20 69 73 20 73 65 74 0a 2a hemeral is set.*
60250 2a 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c 65 * then the table
60260 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 20 is stored in a
60270 66 69 6c 65 20 74 68 61 74 20 69 73 20 61 75 74 file that is aut
60280 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65 6c 65 74 omatically delet
60290 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 56 ed.** when the V
602a0 44 42 45 20 63 75 72 73 6f 72 20 74 6f 20 74 68 DBE cursor to th
602b0 65 20 74 61 62 6c 65 20 69 73 20 63 6c 6f 73 65 e table is close
602c0 64 2e 20 20 49 6e 20 74 68 69 73 20 63 61 73 65 d. In this case
602d0 20 54 61 62 6c 65 2e 74 6e 75 6d 20 0a 2a 2a 20 Table.tnum .**
602e0 72 65 66 65 72 73 20 56 44 42 45 20 63 75 72 73 refers VDBE curs
602f0 6f 72 20 6e 75 6d 62 65 72 20 74 68 61 74 20 68 or number that h
60300 6f 6c 64 73 20 74 68 65 20 74 61 62 6c 65 20 6f olds the table o
60310 70 65 6e 2c 20 6e 6f 74 20 74 6f 20 74 68 65 20 pen, not to the
60320 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 6e 75 6d root.** page num
60330 62 65 72 2e 20 20 54 72 61 6e 73 69 65 6e 74 20 ber. Transient
60340 74 61 62 6c 65 73 20 61 72 65 20 75 73 65 64 20 tables are used
60350 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 73 75 to hold the resu
60360 6c 74 73 20 6f 66 20 61 0a 2a 2a 20 73 75 62 2d lts of a.** sub-
60370 71 75 65 72 79 20 74 68 61 74 20 61 70 70 65 61 query that appea
60380 72 73 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20 rs instead of a
60390 72 65 61 6c 20 74 61 62 6c 65 20 6e 61 6d 65 20 real table name
603a0 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 in the FROM clau
603b0 73 65 20 0a 2a 2a 20 6f 66 20 61 20 53 45 4c 45 se .** of a SELE
603c0 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f CT statement..*/
603d0 0a 73 74 72 75 63 74 20 54 61 62 6c 65 20 7b 0a .struct Table {.
603e0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 4d 65 6d sqlite3 *dbMem
603f0 3b 20 20 20 20 20 20 2f 2a 20 44 42 20 63 6f 6e ; /* DB con
60400 6e 65 63 74 69 6f 6e 20 75 73 65 64 20 66 6f 72 nection used for
60410 20 6c 6f 6f 6b 61 73 69 64 65 20 61 6c 6c 6f 63 lookaside alloc
60420 61 74 69 6f 6e 73 2e 20 2a 2f 0a 20 20 63 68 61 ations. */. cha
60430 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 r *zName;
60440 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 /* Name of the
60450 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 2a table or view *
60460 2f 0a 20 20 69 6e 74 20 69 50 4b 65 79 3b 20 20 /. int iPKey;
60470 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 6e /* If n
60480 6f 74 20 6e 65 67 61 74 69 76 65 2c 20 75 73 65 ot negative, use
60490 20 61 43 6f 6c 5b 69 50 4b 65 79 5d 20 61 73 20 aCol[iPKey] as
604a0 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 the primary key
604b0 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 */. int nCol;
604c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
604d0 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 ber of columns i
604e0 6e 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a n this table */.
604f0 20 20 43 6f 6c 75 6d 6e 20 2a 61 43 6f 6c 3b 20 Column *aCol;
60500 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d /* Inform
60510 61 74 69 6f 6e 20 61 62 6f 75 74 20 65 61 63 68 ation about each
60520 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 49 6e 64 column */. Ind
60530 65 78 20 2a 70 49 6e 64 65 78 3b 20 20 20 20 20 ex *pIndex;
60540 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 53 51 4c /* List of SQL
60550 20 69 6e 64 65 78 65 73 20 6f 6e 20 74 68 69 73 indexes on this
60560 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 69 6e 74 table. */. int
60570 20 74 6e 75 6d 3b 20 20 20 20 20 20 20 20 20 20 tnum;
60580 20 20 2f 2a 20 52 6f 6f 74 20 42 54 72 65 65 20 /* Root BTree
60590 6e 6f 64 65 20 66 6f 72 20 74 68 69 73 20 74 61 node for this ta
605a0 62 6c 65 20 28 73 65 65 20 6e 6f 74 65 20 61 62 ble (see note ab
605b0 6f 76 65 29 20 2a 2f 0a 20 20 53 65 6c 65 63 74 ove) */. Select
605c0 20 2a 70 53 65 6c 65 63 74 3b 20 20 20 20 20 2f *pSelect; /
605d0 2a 20 4e 55 4c 4c 20 66 6f 72 20 74 61 62 6c 65 * NULL for table
605e0 73 2e 20 20 50 6f 69 6e 74 73 20 74 6f 20 64 65 s. Points to de
605f0 66 69 6e 69 74 69 6f 6e 20 69 66 20 61 20 76 69 finition if a vi
60600 65 77 2e 20 2a 2f 0a 20 20 75 31 36 20 6e 52 65 ew. */. u16 nRe
60610 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a f; /*
60620 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74 Number of point
60630 65 72 73 20 74 6f 20 74 68 69 73 20 54 61 62 6c ers to this Tabl
60640 65 20 2a 2f 0a 20 20 75 38 20 74 61 62 46 6c 61 e */. u8 tabFla
60650 67 73 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d gs; /* M
60660 61 73 6b 20 6f 66 20 54 46 5f 2a 20 76 61 6c 75 ask of TF_* valu
60670 65 73 20 2a 2f 0a 20 20 75 38 20 6b 65 79 43 6f es */. u8 keyCo
60680 6e 66 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 nf; /*
60690 57 68 61 74 20 74 6f 20 64 6f 20 69 6e 20 63 61 What to do in ca
606a0 73 65 20 6f 66 20 75 6e 69 71 75 65 6e 65 73 73 se of uniqueness
606b0 20 63 6f 6e 66 6c 69 63 74 20 6f 6e 20 69 50 4b conflict on iPK
606c0 65 79 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 46 ey */. FKey *pF
606d0 4b 65 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 Key; /*
606e0 4c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 61 Linked list of a
606f0 6c 6c 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 ll foreign keys
60700 69 6e 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f in this table */
60710 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 41 66 66 . char *zColAff
60720 3b 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e ; /* Strin
60730 67 20 64 65 66 69 6e 69 6e 67 20 74 68 65 20 61 g defining the a
60740 66 66 69 6e 69 74 79 20 6f 66 20 65 61 63 68 20 ffinity of each
60750 63 6f 6c 75 6d 6e 20 2a 2f 0a 23 69 66 6e 64 65 column */.#ifnde
60760 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 f SQLITE_OMIT_CH
60770 45 43 4b 0a 20 20 45 78 70 72 20 2a 70 43 68 65 ECK. Expr *pChe
60780 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 ck; /* Th
60790 65 20 41 4e 44 20 6f 66 20 61 6c 6c 20 43 48 45 e AND of all CHE
607a0 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a CK constraints *
607b0 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 /.#endif.#ifndef
607c0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 SQLITE_OMIT_ALT
607d0 45 52 54 41 42 4c 45 0a 20 20 69 6e 74 20 61 64 ERTABLE. int ad
607e0 64 43 6f 6c 4f 66 66 73 65 74 3b 20 20 20 20 2f dColOffset; /
607f0 2a 20 4f 66 66 73 65 74 20 69 6e 20 43 52 45 41 * Offset in CREA
60800 54 45 20 54 41 42 4c 45 20 73 74 6d 74 20 74 6f TE TABLE stmt to
60810 20 61 64 64 20 61 20 6e 65 77 20 63 6f 6c 75 6d add a new colum
60820 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e n */.#endif.#ifn
60830 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
60840 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 56 VIRTUALTABLE. V
60850 54 61 62 6c 65 20 2a 70 56 54 61 62 6c 65 3b 20 Table *pVTable;
60860 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 56 /* List of V
60870 54 61 62 6c 65 20 6f 62 6a 65 63 74 73 2e 20 2a Table objects. *
60880 2f 0a 20 20 69 6e 74 20 6e 4d 6f 64 75 6c 65 41 /. int nModuleA
60890 72 67 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 rg; /* Numb
608a0 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 er of arguments
608b0 74 6f 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f to the module */
608c0 0a 20 20 63 68 61 72 20 2a 2a 61 7a 4d 6f 64 75 . char **azModu
608d0 6c 65 41 72 67 3b 20 20 2f 2a 20 54 65 78 74 20 leArg; /* Text
608e0 6f 66 20 61 6c 6c 20 6d 6f 64 75 6c 65 20 61 72 of all module ar
608f0 67 73 2e 20 5b 30 5d 20 69 73 20 6d 6f 64 75 6c gs. [0] is modul
60900 65 20 6e 61 6d 65 20 2a 2f 0a 23 65 6e 64 69 66 e name */.#endif
60910 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 . Trigger *pTri
60920 67 67 65 72 3b 20 20 20 2f 2a 20 4c 69 73 74 20 gger; /* List
60930 6f 66 20 74 72 69 67 67 65 72 73 20 73 74 6f 72 of triggers stor
60940 65 64 20 69 6e 20 70 53 63 68 65 6d 61 20 2a 2f ed in pSchema */
60950 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 . Schema *pSche
60960 6d 61 3b 20 20 20 20 20 2f 2a 20 53 63 68 65 6d ma; /* Schem
60970 61 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 a that contains
60980 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 this table */.
60990 54 61 62 6c 65 20 2a 70 4e 65 78 74 5a 6f 6d 62 Table *pNextZomb
609a0 69 65 3b 20 20 2f 2a 20 4e 65 78 74 20 6f 6e 20 ie; /* Next on
609b0 74 68 65 20 50 61 72 73 65 2e 70 5a 6f 6d 62 69 the Parse.pZombi
609c0 65 54 61 62 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a eTab list */.};.
609d0 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 ./*.** Allowed v
609e0 61 6c 75 65 73 20 66 6f 72 20 54 61 62 65 2e 74 alues for Tabe.t
609f0 61 62 46 6c 61 67 73 2e 0a 2a 2f 0a 23 64 65 66 abFlags..*/.#def
60a00 69 6e 65 20 54 46 5f 52 65 61 64 6f 6e 6c 79 20 ine TF_Readonly
60a10 20 20 20 20 20 20 20 30 78 30 31 20 20 20 20 2f 0x01 /
60a20 2a 20 52 65 61 64 2d 6f 6e 6c 79 20 73 79 73 74 * Read-only syst
60a30 65 6d 20 74 61 62 6c 65 20 2a 2f 0a 23 64 65 66 em table */.#def
60a40 69 6e 65 20 54 46 5f 45 70 68 65 6d 65 72 61 6c ine TF_Ephemeral
60a50 20 20 20 20 20 20 20 30 78 30 32 20 20 20 20 2f 0x02 /
60a60 2a 20 41 6e 20 65 70 68 65 6d 65 72 61 6c 20 74 * An ephemeral t
60a70 61 62 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 able */.#define
60a80 54 46 5f 48 61 73 50 72 69 6d 61 72 79 4b 65 79 TF_HasPrimaryKey
60a90 20 20 20 30 78 30 34 20 20 20 20 2f 2a 20 54 61 0x04 /* Ta
60aa0 62 6c 65 20 68 61 73 20 61 20 70 72 69 6d 61 72 ble has a primar
60ab0 79 20 6b 65 79 20 2a 2f 0a 23 64 65 66 69 6e 65 y key */.#define
60ac0 20 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e TF_Autoincremen
60ad0 74 20 20 20 30 78 30 38 20 20 20 20 2f 2a 20 49 t 0x08 /* I
60ae0 6e 74 65 67 65 72 20 70 72 69 6d 61 72 79 20 6b nteger primary k
60af0 65 79 20 69 73 20 61 75 74 6f 69 6e 63 72 65 6d ey is autoincrem
60b00 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 ent */.#define T
60b10 46 5f 56 69 72 74 75 61 6c 20 20 20 20 20 20 20 F_Virtual
60b20 20 20 30 78 31 30 20 20 20 20 2f 2a 20 49 73 20 0x10 /* Is
60b30 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 a virtual table
60b40 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f 4e 65 */.#define TF_Ne
60b50 65 64 4d 65 74 61 64 61 74 61 20 20 20 20 30 78 edMetadata 0x
60b60 32 30 20 20 20 20 2f 2a 20 61 43 6f 6c 5b 5d 2e 20 /* aCol[].
60b70 7a 54 79 70 65 20 61 6e 64 20 61 43 6f 6c 5b 5d zType and aCol[]
60b80 2e 70 43 6f 6c 6c 20 6d 69 73 73 69 6e 67 20 2a .pColl missing *
60b90 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20 /..../*.** Test
60ba0 74 6f 20 73 65 65 20 77 68 65 74 68 65 72 20 6f to see whether o
60bb0 72 20 6e 6f 74 20 61 20 74 61 62 6c 65 20 69 73 r not a table is
60bc0 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 a virtual table
60bd0 2e 20 20 54 68 69 73 20 69 73 0a 2a 2a 20 64 6f . This is.** do
60be0 6e 65 20 61 73 20 61 20 6d 61 63 72 6f 20 73 6f ne as a macro so
60bf0 20 74 68 61 74 20 69 74 20 77 69 6c 6c 20 62 65 that it will be
60c00 20 6f 70 74 69 6d 69 7a 65 64 20 6f 75 74 20 77 optimized out w
60c10 68 65 6e 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74 hen virtual.** t
60c20 61 62 6c 65 20 73 75 70 70 6f 72 74 20 69 73 20 able support is
60c30 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20 74 68 65 omitted from the
60c40 20 62 75 69 6c 64 2e 0a 2a 2f 0a 23 69 66 6e 64 build..*/.#ifnd
60c50 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ef SQLITE_OMIT_V
60c60 49 52 54 55 41 4c 54 41 42 4c 45 0a 23 20 20 64 IRTUALTABLE.# d
60c70 65 66 69 6e 65 20 49 73 56 69 72 74 75 61 6c 28 efine IsVirtual(
60c80 58 29 20 20 20 20 20 20 28 28 28 58 29 2d 3e 74 X) (((X)->t
60c90 61 62 46 6c 61 67 73 20 26 20 54 46 5f 56 69 72 abFlags & TF_Vir
60ca0 74 75 61 6c 29 21 3d 30 29 0a 23 20 20 64 65 66 tual)!=0).# def
60cb0 69 6e 65 20 49 73 48 69 64 64 65 6e 43 6f 6c 75 ine IsHiddenColu
60cc0 6d 6e 28 58 29 20 28 28 58 29 2d 3e 69 73 48 69 mn(X) ((X)->isHi
60cd0 64 64 65 6e 29 0a 23 65 6c 73 65 0a 23 20 20 64 dden).#else.# d
60ce0 65 66 69 6e 65 20 49 73 56 69 72 74 75 61 6c 28 efine IsVirtual(
60cf0 58 29 20 20 20 20 20 20 30 0a 23 20 20 64 65 66 X) 0.# def
60d00 69 6e 65 20 49 73 48 69 64 64 65 6e 43 6f 6c 75 ine IsHiddenColu
60d10 6d 6e 28 58 29 20 30 0a 23 65 6e 64 69 66 0a 0a mn(X) 0.#endif..
60d20 2f 2a 0a 2a 2a 20 45 61 63 68 20 66 6f 72 65 69 /*.** Each forei
60d30 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e gn key constrain
60d40 74 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 t is an instance
60d50 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
60d60 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a g structure..**.
60d70 2a 2a 20 41 20 66 6f 72 65 69 67 6e 20 6b 65 79 ** A foreign key
60d80 20 69 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 is associated w
60d90 69 74 68 20 74 77 6f 20 74 61 62 6c 65 73 2e 20 ith two tables.
60da0 20 54 68 65 20 22 66 72 6f 6d 22 20 74 61 62 6c The "from" tabl
60db0 65 20 69 73 0a 2a 2a 20 74 68 65 20 74 61 62 6c e is.** the tabl
60dc0 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 e that contains
60dd0 74 68 65 20 52 45 46 45 52 45 4e 43 45 53 20 63 the REFERENCES c
60de0 6c 61 75 73 65 20 74 68 61 74 20 63 72 65 61 74 lause that creat
60df0 65 73 20 74 68 65 20 66 6f 72 65 69 67 6e 0a 2a es the foreign.*
60e00 2a 20 6b 65 79 2e 20 20 54 68 65 20 22 74 6f 22 * key. The "to"
60e10 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 74 61 table is the ta
60e20 62 6c 65 20 74 68 61 74 20 69 73 20 6e 61 6d 65 ble that is name
60e30 64 20 69 6e 20 74 68 65 20 52 45 46 45 52 45 4e d in the REFEREN
60e40 43 45 53 20 63 6c 61 75 73 65 2e 0a 2a 2a 20 43 CES clause..** C
60e50 6f 6e 73 69 64 65 72 20 74 68 69 73 20 65 78 61 onsider this exa
60e60 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 mple:.**.**
60e70 43 52 45 41 54 45 20 54 41 42 4c 45 20 65 78 31 CREATE TABLE ex1
60e80 28 0a 2a 2a 20 20 20 20 20 20 20 61 20 49 4e 54 (.** a INT
60e90 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY
60ea0 2c 0a 2a 2a 20 20 20 20 20 20 20 62 20 49 4e 54 ,.** b INT
60eb0 45 47 45 52 20 43 4f 4e 53 54 52 41 49 4e 54 20 EGER CONSTRAINT
60ec0 66 6b 31 20 52 45 46 45 52 45 4e 43 45 53 20 65 fk1 REFERENCES e
60ed0 78 32 28 78 29 0a 2a 2a 20 20 20 20 20 29 3b 0a x2(x).** );.
60ee0 2a 2a 0a 2a 2a 20 46 6f 72 20 66 6f 72 65 69 67 **.** For foreig
60ef0 6e 20 6b 65 79 20 22 66 6b 31 22 2c 20 74 68 65 n key "fk1", the
60f00 20 66 72 6f 6d 2d 74 61 62 6c 65 20 69 73 20 22 from-table is "
60f10 65 78 31 22 20 61 6e 64 20 74 68 65 20 74 6f 2d ex1" and the to-
60f20 74 61 62 6c 65 20 69 73 20 22 65 78 32 22 2e 0a table is "ex2"..
60f30 2a 2a 0a 2a 2a 20 45 61 63 68 20 52 45 46 45 52 **.** Each REFER
60f40 45 4e 43 45 53 20 63 6c 61 75 73 65 20 67 65 6e ENCES clause gen
60f50 65 72 61 74 65 73 20 61 6e 20 69 6e 73 74 61 6e erates an instan
60f60 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
60f70 69 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2a ing structure.**
60f80 20 77 68 69 63 68 20 69 73 20 61 74 74 61 63 68 which is attach
60f90 65 64 20 74 6f 20 74 68 65 20 66 72 6f 6d 2d 74 ed to the from-t
60fa0 61 62 6c 65 2e 20 20 54 68 65 20 74 6f 2d 74 61 able. The to-ta
60fb0 62 6c 65 20 6e 65 65 64 20 6e 6f 74 20 65 78 69 ble need not exi
60fc0 73 74 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 66 st when.** the f
60fd0 72 6f 6d 2d 74 61 62 6c 65 20 69 73 20 63 72 65 rom-table is cre
60fe0 61 74 65 64 2e 20 20 54 68 65 20 65 78 69 73 74 ated. The exist
60ff0 65 6e 63 65 20 6f 66 20 74 68 65 20 74 6f 2d 74 ence of the to-t
61000 61 62 6c 65 20 69 73 20 6e 6f 74 20 63 68 65 63 able is not chec
61010 6b 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 ked..*/.struct F
61020 4b 65 79 20 7b 0a 20 20 54 61 62 6c 65 20 2a 70 Key {. Table *p
61030 46 72 6f 6d 3b 20 20 20 20 20 2f 2a 20 54 61 62 From; /* Tab
61040 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 le containing th
61050 65 20 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61 e REFERENCES cla
61060 75 73 65 20 28 61 6b 61 3a 20 43 68 69 6c 64 29 use (aka: Child)
61070 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 4e 65 78 */. FKey *pNex
61080 74 46 72 6f 6d 3b 20 20 2f 2a 20 4e 65 78 74 20 tFrom; /* Next
61090 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 6e 20 70 foreign key in p
610a0 46 72 6f 6d 20 2a 2f 0a 20 20 63 68 61 72 20 2a From */. char *
610b0 7a 54 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e zTo; /* N
610c0 61 6d 65 20 6f 66 20 74 61 62 6c 65 20 74 68 61 ame of table tha
610d0 74 20 74 68 65 20 6b 65 79 20 70 6f 69 6e 74 73 t the key points
610e0 20 74 6f 20 28 61 6b 61 3a 20 50 61 72 65 6e 74 to (aka: Parent
610f0 29 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 4e 65 ) */. FKey *pNe
61100 78 74 54 6f 3b 20 20 20 20 2f 2a 20 4e 65 78 74 xtTo; /* Next
61110 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20 foreign key on
61120 74 61 62 6c 65 20 6e 61 6d 65 64 20 7a 54 6f 20 table named zTo
61130 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 50 72 65 76 */. FKey *pPrev
61140 54 6f 3b 20 20 20 20 2f 2a 20 50 72 65 76 69 6f To; /* Previo
61150 75 73 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f us foreign key o
61160 6e 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 7a 54 n table named zT
61170 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b o */. int nCol;
61180 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
61190 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e er of columns in
611a0 20 74 68 69 73 20 6b 65 79 20 2a 2f 0a 20 20 2f this key */. /
611b0 2a 20 45 56 3a 20 52 2d 33 30 33 32 33 2d 32 31 * EV: R-30323-21
611c0 39 31 37 20 2a 2f 0a 20 20 75 38 20 69 73 44 65 917 */. u8 isDe
611d0 66 65 72 72 65 64 3b 20 20 20 20 2f 2a 20 54 72 ferred; /* Tr
611e0 75 65 20 69 66 20 63 6f 6e 73 74 72 61 69 6e 74 ue if constraint
611f0 20 63 68 65 63 6b 69 6e 67 20 69 73 20 64 65 66 checking is def
61200 65 72 72 65 64 20 74 69 6c 6c 20 43 4f 4d 4d 49 erred till COMMI
61210 54 20 2a 2f 0a 20 20 75 38 20 61 41 63 74 69 6f T */. u8 aActio
61220 6e 5b 32 5d 3b 20 20 20 20 20 20 20 20 20 20 2f n[2]; /
61230 2a 20 4f 4e 20 44 45 4c 45 54 45 20 61 6e 64 20 * ON DELETE and
61240 4f 4e 20 55 50 44 41 54 45 20 61 63 74 69 6f 6e ON UPDATE action
61250 73 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 20 s, respectively
61260 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 61 70 */. Trigger *ap
61270 54 72 69 67 67 65 72 5b 32 5d 3b 20 20 2f 2a 20 Trigger[2]; /*
61280 54 72 69 67 67 65 72 73 20 66 6f 72 20 61 41 63 Triggers for aAc
61290 74 69 6f 6e 5b 5d 20 61 63 74 69 6f 6e 73 20 2a tion[] actions *
612a0 2f 0a 20 20 73 74 72 75 63 74 20 73 43 6f 6c 4d /. struct sColM
612b0 61 70 20 7b 20 20 2f 2a 20 4d 61 70 70 69 6e 67 ap { /* Mapping
612c0 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 70 of columns in p
612d0 46 72 6f 6d 20 74 6f 20 63 6f 6c 75 6d 6e 73 20 From to columns
612e0 69 6e 20 7a 54 6f 20 2a 2f 0a 20 20 20 20 69 6e in zTo */. in
612f0 74 20 69 46 72 6f 6d 3b 20 20 20 20 20 20 20 20 t iFrom;
61300 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c /* Index of col
61310 75 6d 6e 20 69 6e 20 70 46 72 6f 6d 20 2a 2f 0a umn in pFrom */.
61320 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 3b 20 char *zCol;
61330 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
61340 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 7a 54 6f 2e f column in zTo.
61350 20 20 49 66 20 30 20 75 73 65 20 50 52 49 4d 41 If 0 use PRIMA
61360 52 59 20 4b 45 59 20 2a 2f 0a 20 20 7d 20 61 43 RY KEY */. } aC
61370 6f 6c 5b 31 5d 3b 20 20 20 20 20 20 20 20 2f 2a ol[1]; /*
61380 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72 20 65 One entry for e
61390 61 63 68 20 6f 66 20 6e 43 6f 6c 20 63 6f 6c 75 ach of nCol colu
613a0 6d 6e 20 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a mn s */.};../*.*
613b0 2a 20 53 51 4c 69 74 65 20 73 75 70 70 6f 72 74 * SQLite support
613c0 73 20 6d 61 6e 79 20 64 69 66 66 65 72 65 6e 74 s many different
613d0 20 77 61 79 73 20 74 6f 20 72 65 73 6f 6c 76 65 ways to resolve
613e0 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a a constraint.**
613f0 20 65 72 72 6f 72 2e 20 20 52 4f 4c 4c 42 41 43 error. ROLLBAC
61400 4b 20 70 72 6f 63 65 73 73 69 6e 67 20 6d 65 61 K processing mea
61410 6e 73 20 74 68 61 74 20 61 20 63 6f 6e 73 74 72 ns that a constr
61420 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 0a 2a aint violation.*
61430 2a 20 63 61 75 73 65 73 20 74 68 65 20 6f 70 65 * causes the ope
61440 72 61 74 69 6f 6e 20 69 6e 20 70 72 6f 63 65 73 ration in proces
61450 73 20 74 6f 20 66 61 69 6c 20 61 6e 64 20 66 6f s to fail and fo
61460 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 r the current tr
61470 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20 ansaction.** to
61480 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 be rolled back.
61490 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73 69 6e ABORT processin
614a0 67 20 6d 65 61 6e 73 20 74 68 65 20 6f 70 65 72 g means the oper
614b0 61 74 69 6f 6e 20 69 6e 20 70 72 6f 63 65 73 73 ation in process
614c0 0a 2a 2a 20 66 61 69 6c 73 20 61 6e 64 20 61 6e .** fails and an
614d0 79 20 70 72 69 6f 72 20 63 68 61 6e 67 65 73 20 y prior changes
614e0 66 72 6f 6d 20 74 68 61 74 20 6f 6e 65 20 6f 70 from that one op
614f0 65 72 61 74 69 6f 6e 20 61 72 65 20 62 61 63 6b eration are back
61500 65 64 20 6f 75 74 2c 0a 2a 2a 20 62 75 74 20 74 ed out,.** but t
61510 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 he transaction i
61520 73 20 6e 6f 74 20 72 6f 6c 6c 65 64 20 62 61 63 s not rolled bac
61530 6b 2e 20 20 46 41 49 4c 20 70 72 6f 63 65 73 73 k. FAIL process
61540 69 6e 67 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a ing means that.*
61550 2a 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 * the operation
61560 69 6e 20 70 72 6f 67 72 65 73 73 20 73 74 6f 70 in progress stop
61570 73 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 6e s and returns an
61580 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 20 42 75 error code. Bu
61590 74 20 70 72 69 6f 72 0a 2a 2a 20 63 68 61 6e 67 t prior.** chang
615a0 65 73 20 64 75 65 20 74 6f 20 74 68 65 20 73 61 es due to the sa
615b0 6d 65 20 6f 70 65 72 61 74 69 6f 6e 20 61 72 65 me operation are
615c0 20 6e 6f 74 20 62 61 63 6b 65 64 20 6f 75 74 20 not backed out
615d0 61 6e 64 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 0a and no rollback.
615e0 2a 2a 20 6f 63 63 75 72 73 2e 20 20 49 47 4e 4f ** occurs. IGNO
615f0 52 45 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 RE means that th
61600 65 20 70 61 72 74 69 63 75 6c 61 72 20 72 6f 77 e particular row
61610 20 74 68 61 74 20 63 61 75 73 65 64 20 74 68 65 that caused the
61620 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65 constraint.** e
61630 72 72 6f 72 20 69 73 20 6e 6f 74 20 69 6e 73 65 rror is not inse
61640 72 74 65 64 20 6f 72 20 75 70 64 61 74 65 64 2e rted or updated.
61650 20 20 50 72 6f 63 65 73 73 69 6e 67 20 63 6f 6e Processing con
61660 74 69 6e 75 65 73 20 61 6e 64 20 6e 6f 20 65 72 tinues and no er
61670 72 6f 72 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e ror.** is return
61680 65 64 2e 20 20 52 45 50 4c 41 43 45 20 6d 65 61 ed. REPLACE mea
61690 6e 73 20 74 68 61 74 20 70 72 65 65 78 69 73 74 ns that preexist
616a0 69 6e 67 20 64 61 74 61 62 61 73 65 20 72 6f 77 ing database row
616b0 73 20 74 68 61 74 20 63 61 75 73 65 64 0a 2a 2a s that caused.**
616c0 20 61 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 a UNIQUE constr
616d0 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 20 61 aint violation a
616e0 72 65 20 72 65 6d 6f 76 65 64 20 73 6f 20 74 68 re removed so th
616f0 61 74 20 74 68 65 20 6e 65 77 20 69 6e 73 65 72 at the new inser
61700 74 20 6f 72 0a 2a 2a 20 75 70 64 61 74 65 20 63 t or.** update c
61710 61 6e 20 70 72 6f 63 65 65 64 2e 20 20 50 72 6f an proceed. Pro
61720 63 65 73 73 69 6e 67 20 63 6f 6e 74 69 6e 75 65 cessing continue
61730 73 20 61 6e 64 20 6e 6f 20 65 72 72 6f 72 20 69 s and no error i
61740 73 20 72 65 70 6f 72 74 65 64 2e 0a 2a 2a 0a 2a s reported..**.*
61750 2a 20 52 45 53 54 52 49 43 54 2c 20 53 45 54 4e * RESTRICT, SETN
61760 55 4c 4c 2c 20 61 6e 64 20 43 41 53 43 41 44 45 ULL, and CASCADE
61770 20 61 63 74 69 6f 6e 73 20 61 70 70 6c 79 20 6f actions apply o
61780 6e 6c 79 20 74 6f 20 66 6f 72 65 69 67 6e 20 6b nly to foreign k
61790 65 79 73 2e 0a 2a 2a 20 52 45 53 54 52 49 43 54 eys..** RESTRICT
617a0 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 is the same as
617b0 41 42 4f 52 54 20 66 6f 72 20 49 4d 4d 45 44 49 ABORT for IMMEDI
617c0 41 54 45 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 ATE foreign keys
617d0 20 61 6e 64 20 74 68 65 0a 2a 2a 20 73 61 6d 65 and the.** same
617e0 20 61 73 20 52 4f 4c 4c 42 41 43 4b 20 66 6f 72 as ROLLBACK for
617f0 20 44 45 46 45 52 52 45 44 20 6b 65 79 73 2e 20 DEFERRED keys.
61800 20 53 45 54 4e 55 4c 4c 20 6d 65 61 6e 73 20 74 SETNULL means t
61810 68 61 74 20 74 68 65 20 66 6f 72 65 69 67 6e 0a hat the foreign.
61820 2a 2a 20 6b 65 79 20 69 73 20 73 65 74 20 74 6f ** key is set to
61830 20 4e 55 4c 4c 2e 20 20 43 41 53 43 41 44 45 20 NULL. CASCADE
61840 6d 65 61 6e 73 20 74 68 61 74 20 61 20 44 45 4c means that a DEL
61850 45 54 45 20 6f 72 20 55 50 44 41 54 45 20 6f 66 ETE or UPDATE of
61860 20 74 68 65 0a 2a 2a 20 72 65 66 65 72 65 6e 63 the.** referenc
61870 65 64 20 74 61 62 6c 65 20 72 6f 77 20 69 73 20 ed table row is
61880 70 72 6f 70 61 67 61 74 65 64 20 69 6e 74 6f 20 propagated into
61890 74 68 65 20 72 6f 77 20 74 68 61 74 20 68 6f 6c the row that hol
618a0 64 73 20 74 68 65 0a 2a 2a 20 66 6f 72 65 69 67 ds the.** foreig
618b0 6e 20 6b 65 79 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 n key..** .** Th
618c0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 79 6d 62 e following symb
618d0 6f 6c 69 63 20 76 61 6c 75 65 73 20 61 72 65 20 olic values are
618e0 75 73 65 64 20 74 6f 20 72 65 63 6f 72 64 20 77 used to record w
618f0 68 69 63 68 20 74 79 70 65 0a 2a 2a 20 6f 66 20 hich type.** of
61900 61 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65 2e 0a action to take..
61910 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 4e 6f */.#define OE_No
61920 6e 65 20 20 20 20 20 30 20 20 20 2f 2a 20 54 68 ne 0 /* Th
61930 65 72 65 20 69 73 20 6e 6f 20 63 6f 6e 73 74 72 ere is no constr
61940 61 69 6e 74 20 74 6f 20 63 68 65 63 6b 20 2a 2f aint to check */
61950 0a 23 64 65 66 69 6e 65 20 4f 45 5f 52 6f 6c 6c .#define OE_Roll
61960 62 61 63 6b 20 31 20 20 20 2f 2a 20 46 61 69 6c back 1 /* Fail
61970 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 61 the operation a
61980 6e 64 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 nd rollback the
61990 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 transaction */.#
619a0 64 65 66 69 6e 65 20 4f 45 5f 41 62 6f 72 74 20 define OE_Abort
619b0 20 20 20 32 20 20 20 2f 2a 20 42 61 63 6b 20 6f 2 /* Back o
619c0 75 74 20 63 68 61 6e 67 65 73 20 62 75 74 20 64 ut changes but d
619d0 6f 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 72 o no rollback tr
619e0 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 ansaction */.#de
619f0 66 69 6e 65 20 4f 45 5f 46 61 69 6c 20 20 20 20 fine OE_Fail
61a00 20 33 20 20 20 2f 2a 20 53 74 6f 70 20 74 68 65 3 /* Stop the
61a10 20 6f 70 65 72 61 74 69 6f 6e 20 62 75 74 20 6c operation but l
61a20 65 61 76 65 20 61 6c 6c 20 70 72 69 6f 72 20 63 eave all prior c
61a30 68 61 6e 67 65 73 20 2a 2f 0a 23 64 65 66 69 6e hanges */.#defin
61a40 65 20 4f 45 5f 49 67 6e 6f 72 65 20 20 20 34 20 e OE_Ignore 4
61a50 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 68 65 20 /* Ignore the
61a60 65 72 72 6f 72 2e 20 44 6f 20 6e 6f 74 20 64 6f error. Do not do
61a70 20 74 68 65 20 49 4e 53 45 52 54 20 6f 72 20 55 the INSERT or U
61a80 50 44 41 54 45 20 2a 2f 0a 23 64 65 66 69 6e 65 PDATE */.#define
61a90 20 4f 45 5f 52 65 70 6c 61 63 65 20 20 35 20 20 OE_Replace 5
61aa0 20 2f 2a 20 44 65 6c 65 74 65 20 65 78 69 73 74 /* Delete exist
61ab0 69 6e 67 20 72 65 63 6f 72 64 2c 20 74 68 65 6e ing record, then
61ac0 20 64 6f 20 49 4e 53 45 52 54 20 6f 72 20 55 50 do INSERT or UP
61ad0 44 41 54 45 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 DATE */..#define
61ae0 20 4f 45 5f 52 65 73 74 72 69 63 74 20 36 20 20 OE_Restrict 6
61af0 20 2f 2a 20 4f 45 5f 41 62 6f 72 74 20 66 6f 72 /* OE_Abort for
61b00 20 49 4d 4d 45 44 49 41 54 45 2c 20 4f 45 5f 52 IMMEDIATE, OE_R
61b10 6f 6c 6c 62 61 63 6b 20 66 6f 72 20 44 45 46 45 ollback for DEFE
61b20 52 52 45 44 20 2a 2f 0a 23 64 65 66 69 6e 65 20 RRED */.#define
61b30 4f 45 5f 53 65 74 4e 75 6c 6c 20 20 37 20 20 20 OE_SetNull 7
61b40 2f 2a 20 53 65 74 20 74 68 65 20 66 6f 72 65 69 /* Set the forei
61b50 67 6e 20 6b 65 79 20 76 61 6c 75 65 20 74 6f 20 gn key value to
61b60 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 NULL */.#define
61b70 4f 45 5f 53 65 74 44 66 6c 74 20 20 38 20 20 20 OE_SetDflt 8
61b80 2f 2a 20 53 65 74 20 74 68 65 20 66 6f 72 65 69 /* Set the forei
61b90 67 6e 20 6b 65 79 20 76 61 6c 75 65 20 74 6f 20 gn key value to
61ba0 69 74 73 20 64 65 66 61 75 6c 74 20 2a 2f 0a 23 its default */.#
61bb0 64 65 66 69 6e 65 20 4f 45 5f 43 61 73 63 61 64 define OE_Cascad
61bc0 65 20 20 39 20 20 20 2f 2a 20 43 61 73 63 61 64 e 9 /* Cascad
61bd0 65 20 74 68 65 20 63 68 61 6e 67 65 73 20 2a 2f e the changes */
61be0 0a 0a 23 64 65 66 69 6e 65 20 4f 45 5f 44 65 66 ..#define OE_Def
61bf0 61 75 6c 74 20 20 39 39 20 20 2f 2a 20 44 6f 20 ault 99 /* Do
61c00 77 68 61 74 65 76 65 72 20 74 68 65 20 64 65 66 whatever the def
61c10 61 75 6c 74 20 61 63 74 69 6f 6e 20 69 73 20 2a ault action is *
61c20 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 /.../*.** An ins
61c30 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
61c40 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
61c50 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68 is passed as th
61c60 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d e first.** argum
61c70 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 56 64 ent to sqlite3Vd
61c80 62 65 4b 65 79 43 6f 6d 70 61 72 65 20 61 6e 64 beKeyCompare and
61c90 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6e 74 is used to cont
61ca0 72 6f 6c 20 74 68 65 20 0a 2a 2a 20 63 6f 6d 70 rol the .** comp
61cb0 61 72 69 73 6f 6e 20 6f 66 20 74 68 65 20 74 77 arison of the tw
61cc0 6f 20 69 6e 64 65 78 20 6b 65 79 73 2e 0a 2a 2f o index keys..*/
61cd0 0a 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 .struct KeyInfo
61ce0 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b {. sqlite3 *db;
61cf0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 /* The d
61d00 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
61d10 6f 6e 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20 on */. u8 enc;
61d20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
61d30 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 2d 20 6f ext encoding - o
61d40 6e 65 20 6f 66 20 74 68 65 20 54 45 58 54 5f 55 ne of the TEXT_U
61d50 74 66 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 tf* values */.
61d60 75 31 36 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 u16 nField;
61d70 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
61d80 20 65 6e 74 72 69 65 73 20 69 6e 20 61 43 6f 6c entries in aCol
61d90 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 53 6f l[] */. u8 *aSo
61da0 72 74 4f 72 64 65 72 3b 20 20 20 20 20 2f 2a 20 rtOrder; /*
61db0 49 66 20 64 65 66 69 6e 65 64 20 61 6e 20 61 53 If defined an aS
61dc0 6f 72 74 4f 72 64 65 72 5b 69 5d 20 69 73 20 74 ortOrder[i] is t
61dd0 72 75 65 2c 20 73 6f 72 74 20 44 45 53 43 20 2a rue, sort DESC *
61de0 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 61 43 6f /. CollSeq *aCo
61df0 6c 6c 5b 31 5d 3b 20 20 2f 2a 20 43 6f 6c 6c 61 ll[1]; /* Colla
61e00 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 66 6f ting sequence fo
61e10 72 20 65 61 63 68 20 74 65 72 6d 20 6f 66 20 74 r each term of t
61e20 68 65 20 6b 65 79 20 2a 2f 0a 7d 3b 0a 0a 2f 2a he key */.};../*
61e30 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 .** An instance
61e40 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
61e50 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 structure holds
61e60 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f information abo
61e70 75 74 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 69 ut a.** single i
61e80 6e 64 65 78 20 72 65 63 6f 72 64 20 74 68 61 74 ndex record that
61e90 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 has already bee
61ea0 6e 20 70 61 72 73 65 64 20 6f 75 74 20 69 6e 74 n parsed out int
61eb0 6f 20 69 6e 64 69 76 69 64 75 61 6c 0a 2a 2a 20 o individual.**
61ec0 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 values..**.** A
61ed0 72 65 63 6f 72 64 20 69 73 20 61 6e 20 6f 62 6a record is an obj
61ee0 65 63 74 20 74 68 61 74 20 63 6f 6e 74 61 69 6e ect that contain
61ef0 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 66 69 s one or more fi
61f00 65 6c 64 73 20 6f 66 20 64 61 74 61 2e 0a 2a 2a elds of data..**
61f10 20 52 65 63 6f 72 64 73 20 61 72 65 20 75 73 65 Records are use
61f20 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 63 d to store the c
61f30 6f 6e 74 65 6e 74 20 6f 66 20 61 20 74 61 62 6c ontent of a tabl
61f40 65 20 72 6f 77 20 61 6e 64 20 74 6f 20 73 74 6f e row and to sto
61f50 72 65 0a 2a 2a 20 74 68 65 20 6b 65 79 20 6f 66 re.** the key of
61f60 20 61 6e 20 69 6e 64 65 78 2e 20 20 41 20 62 6c an index. A bl
61f70 6f 62 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61 ob encoding of a
61f80 20 72 65 63 6f 72 64 20 69 73 20 63 72 65 61 74 record is creat
61f90 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 4f 50 5f ed by.** the OP_
61fa0 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 MakeRecord opcod
61fb0 65 20 6f 66 20 74 68 65 20 56 44 42 45 20 61 6e e of the VDBE an
61fc0 64 20 69 73 20 64 69 73 61 73 73 65 6d 62 6c 65 d is disassemble
61fd0 64 20 62 79 20 74 68 65 0a 2a 2a 20 4f 50 5f 43 d by the.** OP_C
61fe0 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 2e 0a 2a 2a olumn opcode..**
61ff0 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 .** This structu
62000 72 65 20 68 6f 6c 64 73 20 61 20 72 65 63 6f 72 re holds a recor
62010 64 20 74 68 61 74 20 68 61 73 20 61 6c 72 65 61 d that has alrea
62020 64 79 20 62 65 65 6e 20 64 69 73 61 73 73 65 6d dy been disassem
62030 62 6c 65 64 0a 2a 2a 20 69 6e 74 6f 20 69 74 73 bled.** into its
62040 20 63 6f 6e 73 74 69 74 75 65 6e 74 20 66 69 65 constituent fie
62050 6c 64 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 55 lds..*/.struct U
62060 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 7b 0a npackedRecord {.
62070 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 KeyInfo *pKeyI
62080 6e 66 6f 3b 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 nfo; /* Collati
62090 6f 6e 20 61 6e 64 20 73 6f 72 74 2d 6f 72 64 65 on and sort-orde
620a0 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f r information */
620b0 0a 20 20 75 31 36 20 6e 46 69 65 6c 64 3b 20 20 . u16 nField;
620c0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
620d0 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 of entries in a
620e0 70 4d 65 6d 5b 5d 20 2a 2f 0a 20 20 75 31 36 20 pMem[] */. u16
620f0 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 flags;
62100 2f 2a 20 42 6f 6f 6c 65 61 6e 20 73 65 74 74 69 /* Boolean setti
62110 6e 67 73 2e 20 20 55 4e 50 41 43 4b 45 44 5f 2e ngs. UNPACKED_.
62120 2e 2e 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 36 .. below */. i6
62130 34 20 72 6f 77 69 64 3b 20 20 20 20 20 20 20 20 4 rowid;
62140 20 20 2f 2a 20 55 73 65 64 20 62 79 20 55 4e 50 /* Used by UNP
62150 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41 ACKED_PREFIX_SEA
62160 52 43 48 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 4d RCH */. Mem *aM
62170 65 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 em; /*
62180 56 61 6c 75 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a Values */.};../*
62190 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 .** Allowed valu
621a0 65 73 20 6f 66 20 55 6e 70 61 63 6b 65 64 52 65 es of UnpackedRe
621b0 63 6f 72 64 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64 cord.flags.*/.#d
621c0 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 4e efine UNPACKED_N
621d0 45 45 44 5f 46 52 45 45 20 20 20 20 20 30 78 30 EED_FREE 0x0
621e0 30 30 31 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 69 001 /* Memory i
621f0 73 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4d 61 s from sqlite3Ma
62200 6c 6c 6f 63 28 29 20 2a 2f 0a 23 64 65 66 69 6e lloc() */.#defin
62210 65 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f e UNPACKED_NEED_
62220 44 45 53 54 52 4f 59 20 20 30 78 30 30 30 32 20 DESTROY 0x0002
62230 20 2f 2a 20 61 70 4d 65 6d 5b 5d 73 20 73 68 6f /* apMem[]s sho
62240 75 6c 64 20 61 6c 6c 20 62 65 20 64 65 73 74 72 uld all be destr
62250 6f 79 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 oyed */.#define
62260 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f UNPACKED_IGNORE_
62270 52 4f 57 49 44 20 20 30 78 30 30 30 34 20 20 2f ROWID 0x0004 /
62280 2a 20 49 67 6e 6f 72 65 20 74 72 61 69 6c 69 6e * Ignore trailin
62290 67 20 72 6f 77 69 64 20 6f 6e 20 6b 65 79 31 20 g rowid on key1
622a0 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 41 43 */.#define UNPAC
622b0 4b 45 44 5f 49 4e 43 52 4b 45 59 20 20 20 20 20 KED_INCRKEY
622c0 20 20 30 78 30 30 30 38 20 20 2f 2a 20 4d 61 6b 0x0008 /* Mak
622d0 65 20 74 68 69 73 20 6b 65 79 20 61 6e 20 65 70 e this key an ep
622e0 73 69 6c 6f 6e 20 6c 61 72 67 65 72 20 2a 2f 0a silon larger */.
622f0 23 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 #define UNPACKED
62300 5f 50 52 45 46 49 58 5f 4d 41 54 43 48 20 20 30 _PREFIX_MATCH 0
62310 78 30 30 31 30 20 20 2f 2a 20 41 20 70 72 65 66 x0010 /* A pref
62320 69 78 20 6d 61 74 63 68 20 69 73 20 63 6f 6e 73 ix match is cons
62330 69 64 65 72 65 64 20 4f 4b 20 2a 2f 0a 23 64 65 idered OK */.#de
62340 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 50 52 fine UNPACKED_PR
62350 45 46 49 58 5f 53 45 41 52 43 48 20 30 78 30 30 EFIX_SEARCH 0x00
62360 32 30 20 20 2f 2a 20 41 20 70 72 65 66 69 78 20 20 /* A prefix
62370 6d 61 74 63 68 20 69 73 20 63 6f 6e 73 69 64 65 match is conside
62380 72 65 64 20 4f 4b 20 2a 2f 0a 0a 2f 2a 0a 2a 2a red OK */../*.**
62390 20 45 61 63 68 20 53 51 4c 20 69 6e 64 65 78 20 Each SQL index
623a0 69 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 69 is represented i
623b0 6e 20 6d 65 6d 6f 72 79 20 62 79 20 61 6e 0a 2a n memory by an.*
623c0 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 * instance of th
623d0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 e following stru
623e0 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 cture..**.** The
623f0 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 columns of the
62400 74 61 62 6c 65 20 74 68 61 74 20 61 72 65 20 74 table that are t
62410 6f 20 62 65 20 69 6e 64 65 78 65 64 20 61 72 65 o be indexed are
62420 20 64 65 73 63 72 69 62 65 64 0a 2a 2a 20 62 79 described.** by
62430 20 74 68 65 20 61 69 43 6f 6c 75 6d 6e 5b 5d 20 the aiColumn[]
62440 66 69 65 6c 64 20 6f 66 20 74 68 69 73 20 73 74 field of this st
62450 72 75 63 74 75 72 65 2e 20 20 46 6f 72 20 65 78 ructure. For ex
62460 61 6d 70 6c 65 2c 20 73 75 70 70 6f 73 65 0a 2a ample, suppose.*
62470 2a 20 77 65 20 68 61 76 65 20 74 68 65 20 66 6f * we have the fo
62480 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 61 6e llowing table an
62490 64 20 69 6e 64 65 78 3a 0a 2a 2a 0a 2a 2a 20 20 d index:.**.**
624a0 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 CREATE TABLE
624b0 45 78 31 28 63 31 20 69 6e 74 2c 20 63 32 20 69 Ex1(c1 int, c2 i
624c0 6e 74 2c 20 63 33 20 74 65 78 74 29 3b 0a 2a 2a nt, c3 text);.**
624d0 20 20 20 20 20 43 52 45 41 54 45 20 49 4e 44 45 CREATE INDE
624e0 58 20 45 78 32 20 4f 4e 20 45 78 31 28 63 33 2c X Ex2 ON Ex1(c3,
624f0 63 31 29 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 c1);.**.** In th
62500 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 e Table structur
62510 65 20 64 65 73 63 72 69 62 69 6e 67 20 45 78 31 e describing Ex1
62520 2c 20 6e 43 6f 6c 3d 3d 33 20 62 65 63 61 75 73 , nCol==3 becaus
62530 65 20 74 68 65 72 65 20 61 72 65 0a 2a 2a 20 74 e there are.** t
62540 68 72 65 65 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 hree columns in
62550 74 68 65 20 74 61 62 6c 65 2e 20 20 49 6e 20 74 the table. In t
62560 68 65 20 49 6e 64 65 78 20 73 74 72 75 63 74 75 he Index structu
62570 72 65 20 64 65 73 63 72 69 62 69 6e 67 0a 2a 2a re describing.**
62580 20 45 78 32 2c 20 6e 43 6f 6c 75 6d 6e 3d 3d 32 Ex2, nColumn==2
62590 20 73 69 6e 63 65 20 32 20 6f 66 20 74 68 65 20 since 2 of the
625a0 33 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 45 78 31 3 columns of Ex1
625b0 20 61 72 65 20 69 6e 64 65 78 65 64 2e 0a 2a 2a are indexed..**
625c0 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 61 69 The value of ai
625d0 43 6f 6c 75 6d 6e 20 69 73 20 7b 32 2c 20 30 7d Column is {2, 0}
625e0 2e 20 20 61 69 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d . aiColumn[0]==
625f0 32 20 62 65 63 61 75 73 65 20 74 68 65 20 0a 2a 2 because the .*
62600 2a 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e 20 74 * first column t
62610 6f 20 62 65 20 69 6e 64 65 78 65 64 20 28 63 33 o be indexed (c3
62620 29 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 6f ) has an index o
62630 66 20 32 20 69 6e 20 45 78 31 2e 61 43 6f 6c 5b f 2 in Ex1.aCol[
62640 5d 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 ]..** The second
62650 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 69 6e column to be in
62660 64 65 78 65 64 20 28 63 31 29 20 68 61 73 20 61 dexed (c1) has a
62670 6e 20 69 6e 64 65 78 20 6f 66 20 30 20 69 6e 0a n index of 0 in.
62680 2a 2a 20 45 78 31 2e 61 43 6f 6c 5b 5d 2c 20 68 ** Ex1.aCol[], h
62690 65 6e 63 65 20 45 78 32 2e 61 69 43 6f 6c 75 6d ence Ex2.aiColum
626a0 6e 5b 31 5d 3d 3d 30 2e 0a 2a 2a 0a 2a 2a 20 54 n[1]==0..**.** T
626b0 68 65 20 49 6e 64 65 78 2e 6f 6e 45 72 72 6f 72 he Index.onError
626c0 20 66 69 65 6c 64 20 64 65 74 65 72 6d 69 6e 65 field determine
626d0 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 s whether or not
626e0 20 74 68 65 20 69 6e 64 65 78 65 64 20 63 6f 6c the indexed col
626f0 75 6d 6e 73 0a 2a 2a 20 6d 75 73 74 20 62 65 20 umns.** must be
62700 75 6e 69 71 75 65 20 61 6e 64 20 77 68 61 74 20 unique and what
62710 74 6f 20 64 6f 20 69 66 20 74 68 65 79 20 61 72 to do if they ar
62720 65 20 6e 6f 74 2e 20 20 57 68 65 6e 20 49 6e 64 e not. When Ind
62730 65 78 2e 6f 6e 45 72 72 6f 72 3d 4f 45 5f 4e 6f ex.onError=OE_No
62740 6e 65 2c 0a 2a 2a 20 69 74 20 6d 65 61 6e 73 20 ne,.** it means
62750 74 68 69 73 20 69 73 20 6e 6f 74 20 61 20 75 6e this is not a un
62760 69 71 75 65 20 69 6e 64 65 78 2e 20 20 4f 74 68 ique index. Oth
62770 65 72 77 69 73 65 20 69 74 20 69 73 20 61 20 75 erwise it is a u
62780 6e 69 71 75 65 20 69 6e 64 65 78 0a 2a 2a 20 61 nique index.** a
62790 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 nd the value of
627a0 49 6e 64 65 78 2e 6f 6e 45 72 72 6f 72 20 69 6e Index.onError in
627b0 64 69 63 61 74 65 20 74 68 65 20 77 68 69 63 68 dicate the which
627c0 20 63 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75 conflict resolu
627d0 74 69 6f 6e 20 0a 2a 2a 20 61 6c 67 6f 72 69 74 tion .** algorit
627e0 68 6d 20 74 6f 20 65 6d 70 6c 6f 79 20 77 68 65 hm to employ whe
627f0 6e 65 76 65 72 20 61 6e 20 61 74 74 65 6d 70 74 never an attempt
62800 20 69 73 20 6d 61 64 65 20 74 6f 20 69 6e 73 65 is made to inse
62810 72 74 20 61 20 6e 6f 6e 2d 75 6e 69 71 75 65 0a rt a non-unique.
62820 2a 2a 20 65 6c 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 ** element..*/.s
62830 74 72 75 63 74 20 49 6e 64 65 78 20 7b 0a 20 20 truct Index {.
62840 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 char *zName;
62850 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69 73 /* Name of this
62860 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 index */. int
62870 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 2f 2a 20 nColumn; /*
62880 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e Number of column
62890 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 75 s in the table u
628a0 73 65 64 20 62 79 20 74 68 69 73 20 69 6e 64 65 sed by this inde
628b0 78 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 69 43 6f x */. int *aiCo
628c0 6c 75 6d 6e 3b 20 20 20 2f 2a 20 57 68 69 63 68 lumn; /* Which
628d0 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20 75 73 65 columns are use
628e0 64 20 62 79 20 74 68 69 73 20 69 6e 64 65 78 2e d by this index.
628f0 20 20 31 73 74 20 69 73 20 30 20 2a 2f 0a 20 20 1st is 0 */.
62900 75 6e 73 69 67 6e 65 64 20 2a 61 69 52 6f 77 45 unsigned *aiRowE
62910 73 74 3b 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 st; /* Result of
62920 20 41 4e 41 4c 59 5a 45 3a 20 45 73 74 2e 20 72 ANALYZE: Est. r
62930 6f 77 73 20 73 65 6c 65 63 74 65 64 20 62 79 20 ows selected by
62940 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 each column */.
62950 20 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 3b 20 Table *pTable;
62960 20 20 2f 2a 20 54 68 65 20 53 51 4c 20 74 61 62 /* The SQL tab
62970 6c 65 20 62 65 69 6e 67 20 69 6e 64 65 78 65 64 le being indexed
62980 20 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d 3b 20 */. int tnum;
62990 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 63 /* Page c
629a0 6f 6e 74 61 69 6e 69 6e 67 20 72 6f 6f 74 20 6f ontaining root o
629b0 66 20 74 68 69 73 20 69 6e 64 65 78 20 69 6e 20 f this index in
629c0 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f database file */
629d0 0a 20 20 75 38 20 6f 6e 45 72 72 6f 72 3b 20 20 . u8 onError;
629e0 20 20 20 20 2f 2a 20 4f 45 5f 41 62 6f 72 74 2c /* OE_Abort,
629f0 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 4f 45 5f 52 OE_Ignore, OE_R
62a00 65 70 6c 61 63 65 2c 20 6f 72 20 4f 45 5f 4e 6f eplace, or OE_No
62a10 6e 65 20 2a 2f 0a 20 20 75 38 20 61 75 74 6f 49 ne */. u8 autoI
62a20 6e 64 65 78 3b 20 20 20 20 2f 2a 20 54 72 75 65 ndex; /* True
62a30 20 69 66 20 69 73 20 61 75 74 6f 6d 61 74 69 63 if is automatic
62a40 61 6c 6c 79 20 63 72 65 61 74 65 64 20 28 65 78 ally created (ex
62a50 3a 20 62 79 20 55 4e 49 51 55 45 29 20 2a 2f 0a : by UNIQUE) */.
62a60 20 20 63 68 61 72 20 2a 7a 43 6f 6c 41 66 66 3b char *zColAff;
62a70 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 64 65 66 /* String def
62a80 69 6e 69 6e 67 20 74 68 65 20 61 66 66 69 6e 69 ining the affini
62a90 74 79 20 6f 66 20 65 61 63 68 20 63 6f 6c 75 6d ty of each colum
62aa0 6e 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 4e n */. Index *pN
62ab0 65 78 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 6e ext; /* The n
62ac0 65 78 74 20 69 6e 64 65 78 20 61 73 73 6f 63 69 ext index associ
62ad0 61 74 65 64 20 77 69 74 68 20 74 68 65 20 73 61 ated with the sa
62ae0 6d 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 53 63 me table */. Sc
62af0 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b 20 2f hema *pSchema; /
62b00 2a 20 53 63 68 65 6d 61 20 63 6f 6e 74 61 69 6e * Schema contain
62b10 69 6e 67 20 74 68 69 73 20 69 6e 64 65 78 20 2a ing this index *
62b20 2f 0a 20 20 75 38 20 2a 61 53 6f 72 74 4f 72 64 /. u8 *aSortOrd
62b30 65 72 3b 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 er; /* Array of
62b40 20 73 69 7a 65 20 49 6e 64 65 78 2e 6e 43 6f 6c size Index.nCol
62b50 75 6d 6e 2e 20 54 72 75 65 3d 3d 44 45 53 43 2c umn. True==DESC,
62b60 20 46 61 6c 73 65 3d 3d 41 53 43 20 2a 2f 0a 20 False==ASC */.
62b70 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c 6c 3b 20 char **azColl;
62b80 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 63 6f /* Array of co
62b90 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 llation sequence
62ba0 20 6e 61 6d 65 73 20 66 6f 72 20 69 6e 64 65 78 names for index
62bb0 20 2a 2f 0a 20 20 49 6e 64 65 78 53 61 6d 70 6c */. IndexSampl
62bc0 65 20 2a 61 53 61 6d 70 6c 65 3b 20 20 20 20 2f e *aSample; /
62bd0 2a 20 41 72 72 61 79 20 6f 66 20 53 51 4c 49 54 * Array of SQLIT
62be0 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53 20 E_INDEX_SAMPLES
62bf0 73 61 6d 70 6c 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f samples */.};../
62c00 2a 0a 2a 2a 20 45 61 63 68 20 73 61 6d 70 6c 65 *.** Each sample
62c10 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 73 stored in the s
62c20 71 6c 69 74 65 5f 73 74 61 74 32 20 74 61 62 6c qlite_stat2 tabl
62c30 65 20 69 73 20 72 65 70 72 65 73 65 6e 74 65 64 e is represented
62c40 20 69 6e 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 75 in memory .** u
62c50 73 69 6e 67 20 61 20 73 74 72 75 63 74 75 72 65 sing a structure
62c60 20 6f 66 20 74 68 69 73 20 74 79 70 65 2e 0a 2a of this type..*
62c70 2f 0a 73 74 72 75 63 74 20 49 6e 64 65 78 53 61 /.struct IndexSa
62c80 6d 70 6c 65 20 7b 0a 20 20 75 6e 69 6f 6e 20 7b mple {. union {
62c90 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b 20 20 20 . char *z;
62ca0 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 66 /* Value if
62cb0 20 65 54 79 70 65 20 69 73 20 53 51 4c 49 54 45 eType is SQLITE
62cc0 5f 54 45 58 54 20 6f 72 20 53 51 4c 49 54 45 5f _TEXT or SQLITE_
62cd0 42 4c 4f 42 20 2a 2f 0a 20 20 20 20 64 6f 75 62 BLOB */. doub
62ce0 6c 65 20 72 3b 20 20 20 20 20 20 20 2f 2a 20 56 le r; /* V
62cf0 61 6c 75 65 20 69 66 20 65 54 79 70 65 20 69 73 alue if eType is
62d00 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 20 6f 72 SQLITE_FLOAT or
62d10 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 SQLITE_INTEGER
62d20 2a 2f 0a 20 20 7d 20 75 3b 0a 20 20 75 38 20 65 */. } u;. u8 e
62d30 54 79 70 65 3b 20 20 20 20 20 20 20 20 20 2f 2a Type; /*
62d40 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c 20 53 51 SQLITE_NULL, SQ
62d50 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 2e 2e 2e LITE_INTEGER ...
62d60 20 65 74 63 2e 20 2a 2f 0a 20 20 75 38 20 6e 42 etc. */. u8 nB
62d70 79 74 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 yte; /*
62d80 53 69 7a 65 20 69 6e 20 62 79 74 65 20 6f 66 20 Size in byte of
62d90 74 65 78 74 20 6f 72 20 62 6c 6f 62 2e 20 2a 2f text or blob. */
62da0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 .};../*.** Each
62db0 74 6f 6b 65 6e 20 63 6f 6d 69 6e 67 20 6f 75 74 token coming out
62dc0 20 6f 66 20 74 68 65 20 6c 65 78 65 72 20 69 73 of the lexer is
62dd0 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a an instance of.
62de0 2a 2a 20 74 68 69 73 20 73 74 72 75 63 74 75 72 ** this structur
62df0 65 2e 20 20 54 6f 6b 65 6e 73 20 61 72 65 20 61 e. Tokens are a
62e00 6c 73 6f 20 75 73 65 64 20 61 73 20 70 61 72 74 lso used as part
62e10 20 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f of an expressio
62e20 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 69 66 n..**.** Note if
62e30 20 54 6f 6b 65 6e 2e 7a 3d 3d 30 20 74 68 65 6e Token.z==0 then
62e40 20 54 6f 6b 65 6e 2e 64 79 6e 20 61 6e 64 20 54 Token.dyn and T
62e50 6f 6b 65 6e 2e 6e 20 61 72 65 20 75 6e 64 65 66 oken.n are undef
62e60 69 6e 65 64 20 61 6e 64 0a 2a 2a 20 6d 61 79 20 ined and.** may
62e70 63 6f 6e 74 61 69 6e 20 72 61 6e 64 6f 6d 20 76 contain random v
62e80 61 6c 75 65 73 2e 20 20 44 6f 20 6e 6f 74 20 6d alues. Do not m
62e90 61 6b 65 20 61 6e 79 20 61 73 73 75 6d 70 74 69 ake any assumpti
62ea0 6f 6e 73 20 61 62 6f 75 74 20 54 6f 6b 65 6e 2e ons about Token.
62eb0 64 79 6e 0a 2a 2a 20 61 6e 64 20 54 6f 6b 65 6e dyn.** and Token
62ec0 2e 6e 20 77 68 65 6e 20 54 6f 6b 65 6e 2e 7a 3d .n when Token.z=
62ed0 3d 30 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 54 6f =0..*/.struct To
62ee0 6b 65 6e 20 7b 0a 20 20 63 6f 6e 73 74 20 63 68 ken {. const ch
62ef0 61 72 20 2a 7a 3b 20 20 20 20 20 2f 2a 20 54 65 ar *z; /* Te
62f00 78 74 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 2e xt of the token.
62f10 20 20 4e 6f 74 20 4e 55 4c 4c 2d 74 65 72 6d 69 Not NULL-termi
62f20 6e 61 74 65 64 21 20 2a 2f 0a 20 20 75 6e 73 69 nated! */. unsi
62f30 67 6e 65 64 20 69 6e 74 20 6e 3b 20 20 20 20 2f gned int n; /
62f40 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 * Number of char
62f50 61 63 74 65 72 73 20 69 6e 20 74 68 69 73 20 74 acters in this t
62f60 6f 6b 65 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a oken */.};../*.*
62f70 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 * An instance of
62f80 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 this structure
62f90 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61 contains informa
62fa0 74 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 67 tion needed to g
62fb0 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 enerate.** code
62fc0 66 6f 72 20 61 20 53 45 4c 45 43 54 20 74 68 61 for a SELECT tha
62fd0 74 20 63 6f 6e 74 61 69 6e 73 20 61 67 67 72 65 t contains aggre
62fe0 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a gate functions..
62ff0 2a 2a 0a 2a 2a 20 49 66 20 45 78 70 72 2e 6f 70 **.** If Expr.op
63000 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 ==TK_AGG_COLUMN
63010 6f 72 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 or TK_AGG_FUNCTI
63020 4f 4e 20 74 68 65 6e 20 45 78 70 72 2e 70 41 67 ON then Expr.pAg
63030 67 49 6e 66 6f 20 69 73 20 61 0a 2a 2a 20 70 6f gInfo is a.** po
63040 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 74 inter to this st
63050 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 45 78 ructure. The Ex
63060 70 72 2e 69 43 6f 6c 75 6d 6e 20 66 69 65 6c 64 pr.iColumn field
63070 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 69 6e is the index in
63080 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e 61 43 6f 6c .** AggInfo.aCol
63090 5b 5d 20 6f 72 20 41 67 67 49 6e 66 6f 2e 61 46 [] or AggInfo.aF
630a0 75 6e 63 5b 5d 20 6f 66 20 69 6e 66 6f 72 6d 61 unc[] of informa
630b0 74 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 67 tion needed to g
630c0 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 enerate.** code
630d0 66 6f 72 20 74 68 61 74 20 6e 6f 64 65 2e 0a 2a for that node..*
630e0 2a 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e 70 47 72 *.** AggInfo.pGr
630f0 6f 75 70 42 79 20 61 6e 64 20 41 67 67 49 6e 66 oupBy and AggInf
63100 6f 2e 61 46 75 6e 63 2e 70 45 78 70 72 20 70 6f o.aFunc.pExpr po
63110 69 6e 74 20 74 6f 20 66 69 65 6c 64 73 20 77 69 int to fields wi
63120 74 68 69 6e 20 74 68 65 0a 2a 2a 20 6f 72 69 67 thin the.** orig
63130 69 6e 61 6c 20 53 65 6c 65 63 74 20 73 74 72 75 inal Select stru
63140 63 74 75 72 65 20 74 68 61 74 20 64 65 73 63 72 cture that descr
63150 69 62 65 73 20 74 68 65 20 53 45 4c 45 43 54 20 ibes the SELECT
63160 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 73 statement. Thes
63170 65 0a 2a 2a 20 66 69 65 6c 64 73 20 64 6f 20 6e e.** fields do n
63180 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 66 72 ot need to be fr
63190 65 65 64 20 77 68 65 6e 20 64 65 61 6c 6c 6f 63 eed when dealloc
631a0 61 74 69 6e 67 20 74 68 65 20 41 67 67 49 6e 66 ating the AggInf
631b0 6f 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a o structure..*/.
631c0 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 20 7b struct AggInfo {
631d0 0a 20 20 75 38 20 64 69 72 65 63 74 4d 6f 64 65 . u8 directMode
631e0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69 ; /* Di
631f0 72 65 63 74 20 72 65 6e 64 65 72 69 6e 67 20 6d rect rendering m
63200 6f 64 65 20 6d 65 61 6e 73 20 74 61 6b 65 20 64 ode means take d
63210 61 74 61 20 64 69 72 65 63 74 6c 79 0a 20 20 20 ata directly.
63220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63230 20 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 73 ** from s
63240 6f 75 72 63 65 20 74 61 62 6c 65 73 20 72 61 74 ource tables rat
63250 68 65 72 20 74 68 61 6e 20 66 72 6f 6d 20 61 63 her than from ac
63260 63 75 6d 75 6c 61 74 6f 72 73 20 2a 2f 0a 20 20 cumulators */.
63270 75 38 20 75 73 65 53 6f 72 74 69 6e 67 49 64 78 u8 useSortingIdx
63280 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 20 64 69 ; /* In di
63290 72 65 63 74 20 6d 6f 64 65 2c 20 72 65 66 65 72 rect mode, refer
632a0 65 6e 63 65 20 74 68 65 20 73 6f 72 74 69 6e 67 ence the sorting
632b0 20 69 6e 64 65 78 20 72 61 74 68 65 72 0a 20 20 index rather.
632c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
632d0 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 6e 20 ** than
632e0 74 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c 65 the source table
632f0 20 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74 69 6e */. int sortin
63300 67 49 64 78 3b 20 20 20 20 20 20 20 20 20 2f 2a gIdx; /*
63310 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f Cursor number o
63320 66 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69 6e f the sorting in
63330 64 65 78 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 dex */. ExprLis
63340 74 20 2a 70 47 72 6f 75 70 42 79 3b 20 20 20 20 t *pGroupBy;
63350 20 2f 2a 20 54 68 65 20 67 72 6f 75 70 20 62 79 /* The group by
63360 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 clause */. int
63370 20 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e 3b nSortingColumn;
63380 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
63390 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 f columns in the
633a0 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 2a sorting index *
633b0 2f 0a 20 20 73 74 72 75 63 74 20 41 67 67 49 6e /. struct AggIn
633c0 66 6f 5f 63 6f 6c 20 7b 20 20 20 20 2f 2a 20 46 fo_col { /* F
633d0 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 75 or each column u
633e0 73 65 64 20 69 6e 20 73 6f 75 72 63 65 20 74 61 sed in source ta
633f0 62 6c 65 73 20 2a 2f 0a 20 20 20 20 54 61 62 6c bles */. Tabl
63400 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 e *pTab;
63410 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 74 /* Source t
63420 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 able */. int
63430 69 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 iTable;
63440 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e /* Cursor n
63450 75 6d 62 65 72 20 6f 66 20 74 68 65 20 73 6f 75 umber of the sou
63460 72 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 rce table */.
63470 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 int iColumn;
63480 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c /* Col
63490 75 6d 6e 20 6e 75 6d 62 65 72 20 77 69 74 68 69 umn number withi
634a0 6e 20 74 68 65 20 73 6f 75 72 63 65 20 74 61 62 n the source tab
634b0 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 53 le */. int iS
634c0 6f 72 74 65 72 43 6f 6c 75 6d 6e 3b 20 20 20 20 orterColumn;
634d0 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d /* Column num
634e0 62 65 72 20 69 6e 20 74 68 65 20 73 6f 72 74 69 ber in the sorti
634f0 6e 67 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 ng index */.
63500 69 6e 74 20 69 4d 65 6d 3b 20 20 20 20 20 20 20 int iMem;
63510 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f /* Memo
63520 72 79 20 6c 6f 63 61 74 69 6f 6e 20 74 68 61 74 ry location that
63530 20 61 63 74 73 20 61 73 20 61 63 63 75 6d 75 6c acts as accumul
63540 61 74 6f 72 20 2a 2f 0a 20 20 20 20 45 78 70 72 ator */. Expr
63550 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 *pExpr;
63560 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 72 69 67 /* The orig
63570 69 6e 61 6c 20 65 78 70 72 65 73 73 69 6f 6e 20 inal expression
63580 2a 2f 0a 20 20 7d 20 2a 61 43 6f 6c 3b 0a 20 20 */. } *aCol;.
63590 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 int nColumn;
635a0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
635b0 72 20 6f 66 20 75 73 65 64 20 65 6e 74 72 69 65 r of used entrie
635c0 73 20 69 6e 20 61 43 6f 6c 5b 5d 20 2a 2f 0a 20 s in aCol[] */.
635d0 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 41 6c 6c 6f int nColumnAllo
635e0 63 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 c; /* Numb
635f0 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f er of slots allo
63600 63 61 74 65 64 20 66 6f 72 20 61 43 6f 6c 5b 5d cated for aCol[]
63610 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 63 63 75 6d */. int nAccum
63620 75 6c 61 74 6f 72 3b 20 20 20 20 20 20 20 2f 2a ulator; /*
63630 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d Number of colum
63640 6e 73 20 74 68 61 74 20 73 68 6f 77 20 74 68 72 ns that show thr
63650 6f 75 67 68 20 74 6f 20 74 68 65 20 6f 75 74 70 ough to the outp
63660 75 74 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20 ut..
63670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a **
63680 20 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6c 75 Additional colu
63690 6d 6e 73 20 61 72 65 20 75 73 65 64 20 6f 6e 6c mns are used onl
636a0 79 20 61 73 20 70 61 72 61 6d 65 74 65 72 73 20 y as parameters
636b0 74 6f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 to.
636c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 **
636d0 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 aggregate functi
636e0 6f 6e 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 ons */. struct
636f0 41 67 67 49 6e 66 6f 5f 66 75 6e 63 20 7b 20 20 AggInfo_func {
63700 20 2f 2a 20 46 6f 72 20 65 61 63 68 20 61 67 67 /* For each agg
63710 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 regate function
63720 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78 */. Expr *pEx
63730 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 pr;
63740 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 65 6e /* Expression en
63750 63 6f 64 69 6e 67 20 74 68 65 20 66 75 6e 63 74 coding the funct
63760 69 6f 6e 20 2a 2f 0a 20 20 20 20 46 75 6e 63 44 ion */. FuncD
63770 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 ef *pFunc;
63780 20 20 20 20 2f 2a 20 54 68 65 20 61 67 67 72 65 /* The aggre
63790 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 69 6d gate function im
637a0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a plementation */.
637b0 20 20 20 20 69 6e 74 20 69 4d 65 6d 3b 20 20 20 int iMem;
637c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
637d0 4d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 Memory location
637e0 74 68 61 74 20 61 63 74 73 20 61 73 20 61 63 63 that acts as acc
637f0 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 20 20 umulator */.
63800 69 6e 74 20 69 44 69 73 74 69 6e 63 74 3b 20 20 int iDistinct;
63810 20 20 20 20 20 20 20 20 20 2f 2a 20 45 70 68 65 /* Ephe
63820 6d 65 72 61 6c 20 74 61 62 6c 65 20 75 73 65 64 meral table used
63830 20 74 6f 20 65 6e 66 6f 72 63 65 20 44 49 53 54 to enforce DIST
63840 49 4e 43 54 20 2a 2f 0a 20 20 7d 20 2a 61 46 75 INCT */. } *aFu
63850 6e 63 3b 0a 20 20 69 6e 74 20 6e 46 75 6e 63 3b nc;. int nFunc;
63860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
63870 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 Number of entri
63880 65 73 20 69 6e 20 61 46 75 6e 63 5b 5d 20 2a 2f es in aFunc[] */
63890 0a 20 20 69 6e 74 20 6e 46 75 6e 63 41 6c 6c 6f . int nFuncAllo
638a0 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 c; /* Nu
638b0 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c mber of slots al
638c0 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 46 75 6e located for aFun
638d0 63 5b 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a c[] */.};../*.**
638e0 20 54 68 65 20 64 61 74 61 74 79 70 65 20 79 6e The datatype yn
638f0 56 61 72 20 69 73 20 61 20 73 69 67 6e 65 64 20 Var is a signed
63900 69 6e 74 65 67 65 72 2c 20 65 69 74 68 65 72 20 integer, either
63910 31 36 2d 62 69 74 20 6f 72 20 33 32 2d 62 69 74 16-bit or 32-bit
63920 2e 0a 2a 2a 20 55 73 75 61 6c 6c 79 20 69 74 20 ..** Usually it
63930 69 73 20 31 36 2d 62 69 74 73 2e 20 20 42 75 74 is 16-bits. But
63940 20 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 if SQLITE_MAX_V
63950 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 20 69 ARIABLE_NUMBER i
63960 73 20 67 72 65 61 74 65 72 0a 2a 2a 20 74 68 61 s greater.** tha
63970 6e 20 33 32 37 36 37 20 77 65 20 68 61 76 65 20 n 32767 we have
63980 74 6f 20 6d 61 6b 65 20 69 74 20 33 32 2d 62 69 to make it 32-bi
63990 74 2e 20 20 31 36 2d 62 69 74 20 69 73 20 70 72 t. 16-bit is pr
639a0 65 66 65 72 72 65 64 20 62 65 63 61 75 73 65 0a eferred because.
639b0 2a 2a 20 69 74 20 75 73 65 73 20 6c 65 73 73 20 ** it uses less
639c0 6d 65 6d 6f 72 79 20 69 6e 20 74 68 65 20 45 78 memory in the Ex
639d0 70 72 20 6f 62 6a 65 63 74 2c 20 77 68 69 63 68 pr object, which
639e0 20 69 73 20 61 20 62 69 67 20 6d 65 6d 6f 72 79 is a big memory
639f0 20 75 73 65 72 0a 2a 2a 20 69 6e 20 73 79 73 74 user.** in syst
63a00 65 6d 73 20 77 69 74 68 20 6c 6f 74 73 20 6f 66 ems with lots of
63a10 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d prepared statem
63a20 65 6e 74 73 2e 20 20 41 6e 64 20 66 65 77 20 61 ents. And few a
63a30 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 6e pplications.** n
63a40 65 65 64 20 6d 6f 72 65 20 74 68 61 6e 20 61 62 eed more than ab
63a50 6f 75 74 20 31 30 20 6f 72 20 32 30 20 76 61 72 out 10 or 20 var
63a60 69 61 62 6c 65 73 2e 20 20 42 75 74 20 73 6f 6d iables. But som
63a70 65 20 65 78 74 72 65 6d 65 20 75 73 65 72 73 20 e extreme users
63a80 77 61 6e 74 0a 2a 2a 20 74 6f 20 68 61 76 65 20 want.** to have
63a90 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
63aa0 6e 74 73 20 77 69 74 68 20 6f 76 65 72 20 33 32 nts with over 32
63ab0 37 36 37 20 76 61 72 69 61 62 6c 65 73 2c 20 61 767 variables, a
63ac0 6e 64 20 66 6f 72 20 74 68 65 6d 0a 2a 2a 20 74 nd for them.** t
63ad0 68 65 20 6f 70 74 69 6f 6e 20 69 73 20 61 76 61 he option is ava
63ae0 69 6c 61 62 6c 65 20 28 61 74 20 63 6f 6d 70 69 ilable (at compi
63af0 6c 65 2d 74 69 6d 65 29 2e 0a 2a 2f 0a 23 69 66 le-time)..*/.#if
63b00 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49 SQLITE_MAX_VARI
63b10 41 42 4c 45 5f 4e 55 4d 42 45 52 3c 3d 33 32 37 ABLE_NUMBER<=327
63b20 36 37 0a 74 79 70 65 64 65 66 20 69 36 34 20 79 67.typedef i64 y
63b30 6e 56 61 72 3b 0a 23 65 6c 73 65 0a 74 79 70 65 nVar;.#else.type
63b40 64 65 66 20 69 6e 74 20 79 6e 56 61 72 3b 0a 23 def int ynVar;.#
63b50 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 endif../*.** Eac
63b60 68 20 6e 6f 64 65 20 6f 66 20 61 6e 20 65 78 70 h node of an exp
63b70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 20 70 ression in the p
63b80 61 72 73 65 20 74 72 65 65 20 69 73 20 61 6e 20 arse tree is an
63b90 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 instance.** of t
63ba0 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a his structure..*
63bb0 2a 0a 2a 2a 20 45 78 70 72 2e 6f 70 20 69 73 20 *.** Expr.op is
63bc0 74 68 65 20 6f 70 63 6f 64 65 2e 20 54 68 65 20 the opcode. The
63bd0 69 6e 74 65 67 65 72 20 70 61 72 73 65 72 20 74 integer parser t
63be0 6f 6b 65 6e 20 63 6f 64 65 73 20 61 72 65 20 72 oken codes are r
63bf0 65 75 73 65 64 0a 2a 2a 20 61 73 20 6f 70 63 6f eused.** as opco
63c00 64 65 73 20 68 65 72 65 2e 20 46 6f 72 20 65 78 des here. For ex
63c10 61 6d 70 6c 65 2c 20 74 68 65 20 70 61 72 73 65 ample, the parse
63c20 72 20 64 65 66 69 6e 65 73 20 54 4b 5f 47 45 20 r defines TK_GE
63c30 74 6f 20 62 65 20 61 6e 20 69 6e 74 65 67 65 72 to be an integer
63c40 0a 2a 2a 20 63 6f 64 65 20 72 65 70 72 65 73 65 .** code represe
63c50 6e 74 69 6e 67 20 74 68 65 20 22 3e 3d 22 20 6f nting the ">=" o
63c60 70 65 72 61 74 6f 72 2e 20 54 68 69 73 20 73 61 perator. This sa
63c70 6d 65 20 69 6e 74 65 67 65 72 20 63 6f 64 65 20 me integer code
63c80 69 73 20 72 65 75 73 65 64 0a 2a 2a 20 74 6f 20 is reused.** to
63c90 72 65 70 72 65 73 65 6e 74 20 74 68 65 20 67 72 represent the gr
63ca0 65 61 74 65 72 2d 74 68 61 6e 2d 6f 72 2d 65 71 eater-than-or-eq
63cb0 75 61 6c 2d 74 6f 20 6f 70 65 72 61 74 6f 72 20 ual-to operator
63cc0 69 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f in the expressio
63cd0 6e 0a 2a 2a 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a n.** tree..**.**
63ce0 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 If the expressi
63cf0 6f 6e 20 69 73 20 61 6e 20 53 51 4c 20 6c 69 74 on is an SQL lit
63d00 65 72 61 6c 20 28 54 4b 5f 49 4e 54 45 47 45 52 eral (TK_INTEGER
63d10 2c 20 54 4b 5f 46 4c 4f 41 54 2c 20 54 4b 5f 42 , TK_FLOAT, TK_B
63d20 4c 4f 42 2c 20 0a 2a 2a 20 6f 72 20 54 4b 5f 53 LOB, .** or TK_S
63d30 54 52 49 4e 47 29 2c 20 74 68 65 6e 20 45 78 70 TRING), then Exp
63d40 72 2e 74 6f 6b 65 6e 20 63 6f 6e 74 61 69 6e 73 r.token contains
63d50 20 74 68 65 20 74 65 78 74 20 6f 66 20 74 68 65 the text of the
63d60 20 53 51 4c 20 6c 69 74 65 72 61 6c 2e 20 49 66 SQL literal. If
63d70 0a 2a 2a 20 74 68 65 20 65 78 70 72 65 73 73 69 .** the expressi
63d80 6f 6e 20 69 73 20 61 20 76 61 72 69 61 62 6c 65 on is a variable
63d90 20 28 54 4b 5f 56 41 52 49 41 42 4c 45 29 2c 20 (TK_VARIABLE),
63da0 74 68 65 6e 20 45 78 70 72 2e 74 6f 6b 65 6e 20 then Expr.token
63db0 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 0a 2a 2a contains the .**
63dc0 20 76 61 72 69 61 62 6c 65 20 6e 61 6d 65 2e 20 variable name.
63dd0 46 69 6e 61 6c 6c 79 2c 20 69 66 20 74 68 65 20 Finally, if the
63de0 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 6e expression is an
63df0 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 28 54 SQL function (T
63e00 4b 5f 46 55 4e 43 54 49 4f 4e 29 2c 0a 2a 2a 20 K_FUNCTION),.**
63e10 74 68 65 6e 20 45 78 70 72 2e 74 6f 6b 65 6e 20 then Expr.token
63e20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6e 61 6d contains the nam
63e30 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f e of the functio
63e40 6e 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 70 52 n..**.** Expr.pR
63e50 69 67 68 74 20 61 6e 64 20 45 78 70 72 2e 70 4c ight and Expr.pL
63e60 65 66 74 20 61 72 65 20 74 68 65 20 6c 65 66 74 eft are the left
63e70 20 61 6e 64 20 72 69 67 68 74 20 73 75 62 65 78 and right subex
63e80 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 61 0a 2a pressions of a.*
63e90 2a 20 62 69 6e 61 72 79 20 6f 70 65 72 61 74 6f * binary operato
63ea0 72 2e 20 45 69 74 68 65 72 20 6f 72 20 62 6f 74 r. Either or bot
63eb0 68 20 6d 61 79 20 62 65 20 4e 55 4c 4c 2e 0a 2a h may be NULL..*
63ec0 2a 0a 2a 2a 20 45 78 70 72 2e 78 2e 70 4c 69 73 *.** Expr.x.pLis
63ed0 74 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20 61 t is a list of a
63ee0 72 67 75 6d 65 6e 74 73 20 69 66 20 74 68 65 20 rguments if the
63ef0 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 6e expression is an
63f00 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2c 0a 2a SQL function,.*
63f10 2a 20 61 20 43 41 53 45 20 65 78 70 72 65 73 73 * a CASE express
63f20 69 6f 6e 20 6f 72 20 61 6e 20 49 4e 20 65 78 70 ion or an IN exp
63f30 72 65 73 73 69 6f 6e 20 6f 66 20 74 68 65 20 66 ression of the f
63f40 6f 72 6d 20 22 3c 6c 68 73 3e 20 49 4e 20 28 3c orm "<lhs> IN (<
63f50 79 3e 2c 20 3c 7a 3e 2e 2e 2e 29 22 2e 0a 2a 2a y>, <z>...)"..**
63f60 20 45 78 70 72 2e 78 2e 70 53 65 6c 65 63 74 20 Expr.x.pSelect
63f70 69 73 20 75 73 65 64 20 69 66 20 74 68 65 20 65 is used if the e
63f80 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 73 xpression is a s
63f90 75 62 2d 73 65 6c 65 63 74 20 6f 72 20 61 6e 20 ub-select or an
63fa0 65 78 70 72 65 73 73 69 6f 6e 20 6f 66 0a 2a 2a expression of.**
63fb0 20 74 68 65 20 66 6f 72 6d 20 22 3c 6c 68 73 3e the form "<lhs>
63fc0 20 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29 IN (SELECT ...)
63fd0 22 2e 20 49 66 20 74 68 65 20 45 50 5f 78 49 73 ". If the EP_xIs
63fe0 53 65 6c 65 63 74 20 62 69 74 20 69 73 20 73 65 Select bit is se
63ff0 74 20 69 6e 20 74 68 65 0a 2a 2a 20 45 78 70 72 t in the.** Expr
64000 2e 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74 68 65 .flags mask, the
64010 6e 20 45 78 70 72 2e 78 2e 70 53 65 6c 65 63 74 n Expr.x.pSelect
64020 20 69 73 20 76 61 6c 69 64 2e 20 4f 74 68 65 72 is valid. Other
64030 77 69 73 65 2c 20 45 78 70 72 2e 78 2e 70 4c 69 wise, Expr.x.pLi
64040 73 74 20 69 73 20 0a 2a 2a 20 76 61 6c 69 64 2e st is .** valid.
64050 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 70 72 65 73 .**.** An expres
64060 73 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f 72 6d sion of the form
64070 20 49 44 20 6f 72 20 49 44 2e 49 44 20 72 65 66 ID or ID.ID ref
64080 65 72 73 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 ers to a column
64090 69 6e 20 61 20 74 61 62 6c 65 2e 0a 2a 2a 20 46 in a table..** F
640a0 6f 72 20 73 75 63 68 20 65 78 70 72 65 73 73 69 or such expressi
640b0 6f 6e 73 2c 20 45 78 70 72 2e 6f 70 20 69 73 20 ons, Expr.op is
640c0 73 65 74 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e set to TK_COLUMN
640d0 20 61 6e 64 20 45 78 70 72 2e 69 54 61 62 6c 65 and Expr.iTable
640e0 20 69 73 0a 2a 2a 20 74 68 65 20 69 6e 74 65 67 is.** the integ
640f0 65 72 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 er cursor number
64100 20 6f 66 20 61 20 56 44 42 45 20 63 75 72 73 6f of a VDBE curso
64110 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 r pointing to th
64120 61 74 20 74 61 62 6c 65 20 61 6e 64 0a 2a 2a 20 at table and.**
64130 45 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 69 73 20 Expr.iColumn is
64140 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 the column numbe
64150 72 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 r for the specif
64160 69 63 20 63 6f 6c 75 6d 6e 2e 20 20 49 66 20 74 ic column. If t
64170 68 65 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e he.** expression
64180 20 69 73 20 75 73 65 64 20 61 73 20 61 20 72 65 is used as a re
64190 73 75 6c 74 20 69 6e 20 61 6e 20 61 67 67 72 65 sult in an aggre
641a0 67 61 74 65 20 53 45 4c 45 43 54 2c 20 74 68 65 gate SELECT, the
641b0 6e 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 69 n the.** value i
641c0 73 20 61 6c 73 6f 20 73 74 6f 72 65 64 20 69 6e s also stored in
641d0 20 74 68 65 20 45 78 70 72 2e 69 41 67 67 20 63 the Expr.iAgg c
641e0 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 61 67 67 olumn in the agg
641f0 72 65 67 61 74 65 20 73 6f 20 74 68 61 74 0a 2a regate so that.*
64200 2a 20 69 74 20 63 61 6e 20 62 65 20 61 63 63 65 * it can be acce
64210 73 73 65 64 20 61 66 74 65 72 20 61 6c 6c 20 61 ssed after all a
64220 67 67 72 65 67 61 74 65 73 20 61 72 65 20 63 6f ggregates are co
64230 6d 70 75 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 mputed..**.** If
64240 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
64250 69 73 20 61 6e 20 75 6e 62 6f 75 6e 64 20 76 61 is an unbound va
64260 72 69 61 62 6c 65 20 6d 61 72 6b 65 72 20 28 61 riable marker (a
64270 20 71 75 65 73 74 69 6f 6e 20 6d 61 72 6b 20 0a question mark .
64280 2a 2a 20 63 68 61 72 61 63 74 65 72 20 27 3f 27 ** character '?'
64290 20 69 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c in the original
642a0 20 53 51 4c 29 20 74 68 65 6e 20 74 68 65 20 45 SQL) then the E
642b0 78 70 72 2e 69 54 61 62 6c 65 20 68 6f 6c 64 73 xpr.iTable holds
642c0 20 74 68 65 20 69 6e 64 65 78 20 0a 2a 2a 20 6e the index .** n
642d0 75 6d 62 65 72 20 66 6f 72 20 74 68 61 74 20 76 umber for that v
642e0 61 72 69 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 ariable..**.** I
642f0 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e f the expression
64300 20 69 73 20 61 20 73 75 62 71 75 65 72 79 20 74 is a subquery t
64310 68 65 6e 20 45 78 70 72 2e 69 43 6f 6c 75 6d 6e hen Expr.iColumn
64320 20 68 6f 6c 64 73 20 61 6e 20 69 6e 74 65 67 65 holds an intege
64330 72 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 6e 75 r.** register nu
64340 6d 62 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 mber containing
64350 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 the result of th
64360 65 20 73 75 62 71 75 65 72 79 2e 20 20 49 66 20 e subquery. If
64370 74 68 65 0a 2a 2a 20 73 75 62 71 75 65 72 79 20 the.** subquery
64380 67 69 76 65 73 20 61 20 63 6f 6e 73 74 61 6e 74 gives a constant
64390 20 72 65 73 75 6c 74 2c 20 74 68 65 6e 20 69 54 result, then iT
643a0 61 62 6c 65 20 69 73 20 2d 31 2e 20 20 49 66 20 able is -1. If
643b0 74 68 65 20 73 75 62 71 75 65 72 79 0a 2a 2a 20 the subquery.**
643c0 67 69 76 65 73 20 61 20 64 69 66 66 65 72 65 6e gives a differen
643d0 74 20 61 6e 73 77 65 72 20 61 74 20 64 69 66 66 t answer at diff
643e0 65 72 65 6e 74 20 74 69 6d 65 73 20 64 75 72 69 erent times duri
643f0 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 70 72 6f ng statement pro
64400 63 65 73 73 69 6e 67 0a 2a 2a 20 74 68 65 6e 20 cessing.** then
64410 69 54 61 62 6c 65 20 69 73 20 74 68 65 20 61 64 iTable is the ad
64420 64 72 65 73 73 20 6f 66 20 61 20 73 75 62 72 6f dress of a subro
64430 75 74 69 6e 65 20 74 68 61 74 20 63 6f 6d 70 75 utine that compu
64440 74 65 73 20 74 68 65 20 73 75 62 71 75 65 72 79 tes the subquery
64450 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 45 ..**.** If the E
64460 78 70 72 20 69 73 20 6f 66 20 74 79 70 65 20 4f xpr is of type O
64470 50 5f 43 6f 6c 75 6d 6e 2c 20 61 6e 64 20 74 68 P_Column, and th
64480 65 20 74 61 62 6c 65 20 69 74 20 69 73 20 73 65 e table it is se
64490 6c 65 63 74 69 6e 67 20 66 72 6f 6d 0a 2a 2a 20 lecting from.**
644a0 69 73 20 61 20 64 69 73 6b 20 74 61 62 6c 65 20 is a disk table
644b0 6f 72 20 74 68 65 20 22 6f 6c 64 2e 2a 22 20 70 or the "old.*" p
644c0 73 65 75 64 6f 2d 74 61 62 6c 65 2c 20 74 68 65 seudo-table, the
644d0 6e 20 70 54 61 62 20 70 6f 69 6e 74 73 20 74 6f n pTab points to
644e0 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73 70 6f the.** correspo
644f0 6e 64 69 6e 67 20 74 61 62 6c 65 20 64 65 66 69 nding table defi
64500 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 4c nition..**.** AL
64510 4c 4f 43 41 54 49 4f 4e 20 4e 4f 54 45 53 3a 0a LOCATION NOTES:.
64520 2a 2a 0a 2a 2a 20 45 78 70 72 20 6f 62 6a 65 63 **.** Expr objec
64530 74 73 20 63 61 6e 20 75 73 65 20 61 20 6c 6f 74 ts can use a lot
64540 20 6f 66 20 6d 65 6d 6f 72 79 20 73 70 61 63 65 of memory space
64550 20 69 6e 20 64 61 74 61 62 61 73 65 20 73 63 68 in database sch
64560 65 6d 61 2e 20 20 54 6f 0a 2a 2a 20 68 65 6c 70 ema. To.** help
64570 20 72 65 64 75 63 65 20 6d 65 6d 6f 72 79 20 72 reduce memory r
64580 65 71 75 69 72 65 6d 65 6e 74 73 2c 20 73 6f 6d equirements, som
64590 65 74 69 6d 65 73 20 61 6e 20 45 78 70 72 20 6f etimes an Expr o
645a0 62 6a 65 63 74 20 77 69 6c 6c 20 62 65 0a 2a 2a bject will be.**
645b0 20 74 72 75 6e 63 61 74 65 64 2e 20 20 41 6e 64 truncated. And
645c0 20 74 6f 20 72 65 64 75 63 65 20 74 68 65 20 6e to reduce the n
645d0 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 umber of memory
645e0 61 6c 6c 6f 63 61 74 69 6f 6e 73 2c 20 73 6f 6d allocations, som
645f0 65 74 69 6d 65 73 0a 2a 2a 20 74 77 6f 20 6f 72 etimes.** two or
64600 20 6d 6f 72 65 20 45 78 70 72 20 6f 62 6a 65 63 more Expr objec
64610 74 73 20 77 69 6c 6c 20 62 65 20 73 74 6f 72 65 ts will be store
64620 64 20 69 6e 20 61 20 73 69 6e 67 6c 65 20 6d 65 d in a single me
64630 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c mory allocation,
64640 0a 2a 2a 20 74 6f 67 65 74 68 65 72 20 77 69 74 .** together wit
64650 68 20 45 78 70 72 2e 7a 54 6f 6b 65 6e 20 73 74 h Expr.zToken st
64660 72 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 rings..**.** If
64670 74 68 65 20 45 50 5f 52 65 64 75 63 65 64 20 61 the EP_Reduced a
64680 6e 64 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 nd EP_TokenOnly
64690 66 6c 61 67 73 20 61 72 65 20 73 65 74 20 77 68 flags are set wh
646a0 65 6e 0a 2a 2a 20 61 6e 20 45 78 70 72 20 6f 62 en.** an Expr ob
646b0 6a 65 63 74 20 69 73 20 74 72 75 6e 63 61 74 65 ject is truncate
646c0 64 2e 20 20 57 68 65 6e 20 45 50 5f 52 65 64 75 d. When EP_Redu
646d0 63 65 64 20 69 73 20 73 65 74 2c 20 74 68 65 6e ced is set, then
646e0 20 61 6c 6c 0a 2a 2a 20 74 68 65 20 63 68 69 6c all.** the chil
646f0 64 20 45 78 70 72 20 6f 62 6a 65 63 74 73 20 69 d Expr objects i
64700 6e 20 74 68 65 20 45 78 70 72 2e 70 4c 65 66 74 n the Expr.pLeft
64710 20 61 6e 64 20 45 78 70 72 2e 70 52 69 67 68 74 and Expr.pRight
64720 20 73 75 62 74 72 65 65 73 0a 2a 2a 20 61 72 65 subtrees.** are
64730 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 contained withi
64740 6e 20 74 68 65 20 73 61 6d 65 20 6d 65 6d 6f 72 n the same memor
64750 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 4e y allocation. N
64760 6f 74 65 2c 20 68 6f 77 65 76 65 72 2c 20 74 68 ote, however, th
64770 61 74 0a 2a 2a 20 74 68 65 20 73 75 62 74 72 65 at.** the subtre
64780 65 73 20 69 6e 20 45 78 70 72 2e 78 2e 70 4c 69 es in Expr.x.pLi
64790 73 74 20 6f 72 20 45 78 70 72 2e 78 2e 70 53 65 st or Expr.x.pSe
647a0 6c 65 63 74 20 61 72 65 20 61 6c 77 61 79 73 20 lect are always
647b0 73 65 70 61 72 61 74 65 6c 79 0a 2a 2a 20 61 6c separately.** al
647c0 6c 6f 63 61 74 65 64 2c 20 72 65 67 61 72 64 6c located, regardl
647d0 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 6f ess of whether o
647e0 72 20 6e 6f 74 20 45 50 5f 52 65 64 75 63 65 64 r not EP_Reduced
647f0 20 69 73 20 73 65 74 2e 0a 2a 2f 0a 73 74 72 75 is set..*/.stru
64800 63 74 20 45 78 70 72 20 7b 0a 20 20 75 38 20 6f ct Expr {. u8 o
64810 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p;
64820 20 20 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 /* Operation
64830 70 65 72 66 6f 72 6d 65 64 20 62 79 20 74 68 69 performed by thi
64840 73 20 6e 6f 64 65 20 2a 2f 0a 20 20 63 68 61 72 s node */. char
64850 20 61 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20 affinity;
64860 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 /* The affini
64870 74 79 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e ty of the column
64880 20 6f 72 20 30 20 69 66 20 6e 6f 74 20 61 20 63 or 0 if not a c
64890 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 75 31 36 20 66 olumn */. u16 f
648a0 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 lags;
648b0 20 20 2f 2a 20 56 61 72 69 6f 75 73 20 66 6c 61 /* Various fla
648c0 67 73 2e 20 20 45 50 5f 2a 20 53 65 65 20 62 65 gs. EP_* See be
648d0 6c 6f 77 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b low */. union {
648e0 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 6f 6b 65 . char *zToke
648f0 6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 n; /* T
64900 6f 6b 65 6e 20 76 61 6c 75 65 2e 20 5a 65 72 6f oken value. Zero
64910 20 74 65 72 6d 69 6e 61 74 65 64 20 61 6e 64 20 terminated and
64920 64 65 71 75 6f 74 65 64 20 2a 2f 0a 20 20 20 20 dequoted */.
64930 69 6e 74 20 69 56 61 6c 75 65 3b 20 20 20 20 20 int iValue;
64940 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 /* Intege
64950 72 20 76 61 6c 75 65 20 69 66 20 45 50 5f 49 6e r value if EP_In
64960 74 56 61 6c 75 65 20 2a 2f 0a 20 20 7d 20 75 3b tValue */. } u;
64970 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 45 50 .. /* If the EP
64980 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 66 6c 61 67 20 _TokenOnly flag
64990 69 73 20 73 65 74 20 69 6e 20 74 68 65 20 45 78 is set in the Ex
649a0 70 72 2e 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74 pr.flags mask, t
649b0 68 65 6e 20 6e 6f 0a 20 20 2a 2a 20 73 70 61 63 hen no. ** spac
649c0 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 e is allocated f
649d0 6f 72 20 74 68 65 20 66 69 65 6c 64 73 20 62 65 or the fields be
649e0 6c 6f 77 20 74 68 69 73 20 70 6f 69 6e 74 2e 20 low this point.
649f0 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 0a 20 20 An attempt to.
64a00 2a 2a 20 61 63 63 65 73 73 20 74 68 65 6d 20 77 ** access them w
64a10 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 61 20 ill result in a
64a20 73 65 67 66 61 75 6c 74 20 6f 72 20 6d 61 6c 66 segfault or malf
64a30 75 6e 63 74 69 6f 6e 2e 20 0a 20 20 2a 2a 2a 2a unction. . ****
64a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64a80 2a 2f 0a 0a 20 20 45 78 70 72 20 2a 70 4c 65 66 */.. Expr *pLef
64a90 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 t; /*
64aa0 4c 65 66 74 20 73 75 62 6e 6f 64 65 20 2a 2f 0a Left subnode */.
64ab0 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 3b 20 Expr *pRight;
64ac0 20 20 20 20 20 20 20 20 20 2f 2a 20 52 69 67 68 /* Righ
64ad0 74 20 73 75 62 6e 6f 64 65 20 2a 2f 0a 20 20 75 t subnode */. u
64ae0 6e 69 6f 6e 20 7b 0a 20 20 20 20 45 78 70 72 4c nion {. ExprL
64af0 69 73 74 20 2a 70 4c 69 73 74 3b 20 20 20 20 20 ist *pList;
64b00 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 61 72 67 75 /* Function argu
64b10 6d 65 6e 74 73 20 6f 72 20 69 6e 20 22 3c 65 78 ments or in "<ex
64b20 70 72 3e 20 49 4e 20 28 3c 65 78 70 72 2d 6c 69 pr> IN (<expr-li
64b30 73 74 29 22 20 2a 2f 0a 20 20 20 20 53 65 6c 65 st)" */. Sele
64b40 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 20 20 20 ct *pSelect;
64b50 20 2f 2a 20 55 73 65 64 20 66 6f 72 20 73 75 62 /* Used for sub
64b60 2d 73 65 6c 65 63 74 73 20 61 6e 64 20 22 3c 65 -selects and "<e
64b70 78 70 72 3e 20 49 4e 20 28 3c 73 65 6c 65 63 74 xpr> IN (<select
64b80 3e 29 22 20 2a 2f 0a 20 20 7d 20 78 3b 0a 20 20 >)" */. } x;.
64b90 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 CollSeq *pColl;
64ba0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f /* The co
64bb0 6c 6c 61 74 69 6f 6e 20 74 79 70 65 20 6f 66 20 llation type of
64bc0 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 72 20 30 20 the column or 0
64bd0 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 */.. /* If the
64be0 45 50 5f 52 65 64 75 63 65 64 20 66 6c 61 67 20 EP_Reduced flag
64bf0 69 73 20 73 65 74 20 69 6e 20 74 68 65 20 45 78 is set in the Ex
64c00 70 72 2e 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74 pr.flags mask, t
64c10 68 65 6e 20 6e 6f 0a 20 20 2a 2a 20 73 70 61 63 hen no. ** spac
64c20 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 e is allocated f
64c30 6f 72 20 74 68 65 20 66 69 65 6c 64 73 20 62 65 or the fields be
64c40 6c 6f 77 20 74 68 69 73 20 70 6f 69 6e 74 2e 20 low this point.
64c50 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 0a 20 20 An attempt to.
64c60 2a 2a 20 61 63 63 65 73 73 20 74 68 65 6d 20 77 ** access them w
64c70 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 61 20 ill result in a
64c80 73 65 67 66 61 75 6c 74 20 6f 72 20 6d 61 6c 66 segfault or malf
64c90 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2a 2a 2a 2a unction.. *****
64ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64ce0 2f 0a 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 3b /.. int iTable;
64cf0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
64d00 4b 5f 43 4f 4c 55 4d 4e 3a 20 63 75 72 73 6f 72 K_COLUMN: cursor
64d10 20 6e 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 number of table
64d20 20 68 6f 6c 64 69 6e 67 20 63 6f 6c 75 6d 6e 0a holding column.
64d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64d40 20 20 20 20 20 20 20 20 20 2a 2a 20 54 4b 5f 52 ** TK_R
64d50 45 47 49 53 54 45 52 3a 20 72 65 67 69 73 74 65 EGISTER: registe
64d60 72 20 6e 75 6d 62 65 72 0a 20 20 20 20 20 20 20 r number.
64d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64d80 20 20 2a 2a 20 54 4b 5f 54 52 49 47 47 45 52 3a ** TK_TRIGGER:
64d90 20 31 20 2d 3e 20 6e 65 77 2c 20 30 20 2d 3e 20 1 -> new, 0 ->
64da0 6f 6c 64 20 2a 2f 0a 20 20 79 6e 56 61 72 20 69 old */. ynVar i
64db0 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 Column;
64dc0 2f 2a 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 63 6f /* TK_COLUMN: co
64dd0 6c 75 6d 6e 20 69 6e 64 65 78 2e 20 20 2d 31 20 lumn index. -1
64de0 66 6f 72 20 72 6f 77 69 64 2e 0a 20 20 20 20 20 for rowid..
64df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64e00 20 20 20 20 2a 2a 20 54 4b 5f 56 41 52 49 41 42 ** TK_VARIAB
64e10 4c 45 3a 20 76 61 72 69 61 62 6c 65 20 6e 75 6d LE: variable num
64e20 62 65 72 20 28 61 6c 77 61 79 73 20 3e 3d 20 31 ber (always >= 1
64e30 29 2e 20 2a 2f 0a 20 20 69 31 36 20 69 41 67 67 ). */. i16 iAgg
64e40 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
64e50 2a 20 57 68 69 63 68 20 65 6e 74 72 79 20 69 6e * Which entry in
64e60 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b pAggInfo->aCol[
64e70 5d 20 6f 72 20 2d 3e 61 46 75 6e 63 5b 5d 20 2a ] or ->aFunc[] *
64e80 2f 0a 20 20 69 31 36 20 69 52 69 67 68 74 4a 6f /. i16 iRightJo
64e90 69 6e 54 61 62 6c 65 3b 20 20 20 2f 2a 20 49 66 inTable; /* If
64ea0 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 2c 20 74 68 EP_FromJoin, th
64eb0 65 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f 66 e right table of
64ec0 20 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a 20 20 75 the join */. u
64ed0 38 20 66 6c 61 67 73 32 3b 20 20 20 20 20 20 20 8 flags2;
64ee0 20 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 /* Second
64ef0 73 65 74 20 6f 66 20 66 6c 61 67 73 2e 20 20 45 set of flags. E
64f00 50 32 5f 2e 2e 2e 20 2a 2f 0a 20 20 75 38 20 6f P2_... */. u8 o
64f10 70 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 p2;
64f20 20 20 20 2f 2a 20 49 66 20 61 20 54 4b 5f 52 45 /* If a TK_RE
64f30 47 49 53 54 45 52 2c 20 74 68 65 20 6f 72 69 67 GISTER, the orig
64f40 69 6e 61 6c 20 76 61 6c 75 65 20 6f 66 20 45 78 inal value of Ex
64f50 70 72 2e 6f 70 20 2a 2f 0a 20 20 41 67 67 49 6e pr.op */. AggIn
64f60 66 6f 20 2a 70 41 67 67 49 6e 66 6f 3b 20 20 20 fo *pAggInfo;
64f70 20 20 2f 2a 20 55 73 65 64 20 62 79 20 54 4b 5f /* Used by TK_
64f80 41 47 47 5f 43 4f 4c 55 4d 4e 20 61 6e 64 20 54 AGG_COLUMN and T
64f90 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 2a K_AGG_FUNCTION *
64fa0 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b /. Table *pTab;
64fb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 /* Ta
64fc0 62 6c 65 20 66 6f 72 20 54 4b 5f 43 4f 4c 55 4d ble for TK_COLUM
64fd0 4e 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 20 2a N expressions. *
64fe0 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 /.#if SQLITE_MAX
64ff0 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 20 20 _EXPR_DEPTH>0.
65000 69 6e 74 20 6e 48 65 69 67 68 74 3b 20 20 20 20 int nHeight;
65010 20 20 20 20 20 20 20 2f 2a 20 48 65 69 67 68 74 /* Height
65020 20 6f 66 20 74 68 65 20 74 72 65 65 20 68 65 61 of the tree hea
65030 64 65 64 20 62 79 20 74 68 69 73 20 6e 6f 64 65 ded by this node
65040 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f */.#endif.};../
65050 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
65060 6e 67 20 61 72 65 20 74 68 65 20 6d 65 61 6e 69 ng are the meani
65070 6e 67 73 20 6f 66 20 62 69 74 73 20 69 6e 20 74 ngs of bits in t
65080 68 65 20 45 78 70 72 2e 66 6c 61 67 73 20 66 69 he Expr.flags fi
65090 65 6c 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 eld..*/.#define
650a0 45 50 5f 46 72 6f 6d 4a 6f 69 6e 20 20 20 30 78 EP_FromJoin 0x
650b0 30 30 30 31 20 20 2f 2a 20 4f 72 69 67 69 6e 61 0001 /* Origina
650c0 74 65 64 20 69 6e 20 4f 4e 20 6f 72 20 55 53 49 ted in ON or USI
650d0 4e 47 20 63 6c 61 75 73 65 20 6f 66 20 61 20 6a NG clause of a j
650e0 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 oin */.#define E
650f0 50 5f 41 67 67 20 20 20 20 20 20 20 20 30 78 30 P_Agg 0x0
65100 30 30 32 20 20 2f 2a 20 43 6f 6e 74 61 69 6e 73 002 /* Contains
65110 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 61 67 67 one or more agg
65120 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 regate functions
65130 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 52 */.#define EP_R
65140 65 73 6f 6c 76 65 64 20 20 20 30 78 30 30 30 34 esolved 0x0004
65150 20 20 2f 2a 20 49 44 73 20 68 61 76 65 20 62 65 /* IDs have be
65160 65 6e 20 72 65 73 6f 6c 76 65 64 20 74 6f 20 43 en resolved to C
65170 4f 4c 55 4d 4e 73 20 2a 2f 0a 23 64 65 66 69 6e OLUMNs */.#defin
65180 65 20 45 50 5f 45 72 72 6f 72 20 20 20 20 20 20 e EP_Error
65190 30 78 30 30 30 38 20 20 2f 2a 20 45 78 70 72 65 0x0008 /* Expre
651a0 73 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 6f ssion contains o
651b0 6e 65 20 6f 72 20 6d 6f 72 65 20 65 72 72 6f 72 ne or more error
651c0 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f s */.#define EP_
651d0 44 69 73 74 69 6e 63 74 20 20 20 30 78 30 30 31 Distinct 0x001
651e0 30 20 20 2f 2a 20 41 67 67 72 65 67 61 74 65 20 0 /* Aggregate
651f0 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 44 49 function with DI
65200 53 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 2a STINCT keyword *
65210 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 56 61 72 /.#define EP_Var
65220 53 65 6c 65 63 74 20 20 30 78 30 30 32 30 20 20 Select 0x0020
65230 2f 2a 20 70 53 65 6c 65 63 74 20 69 73 20 63 6f /* pSelect is co
65240 72 72 65 6c 61 74 65 64 2c 20 6e 6f 74 20 63 6f rrelated, not co
65250 6e 73 74 61 6e 74 20 2a 2f 0a 23 64 65 66 69 6e nstant */.#defin
65260 65 20 45 50 5f 44 62 6c 51 75 6f 74 65 64 20 20 e EP_DblQuoted
65270 30 78 30 30 34 30 20 20 2f 2a 20 74 6f 6b 65 6e 0x0040 /* token
65280 2e 7a 20 77 61 73 20 6f 72 69 67 69 6e 61 6c 6c .z was originall
65290 79 20 69 6e 20 22 2e 2e 2e 22 20 2a 2f 0a 23 64 y in "..." */.#d
652a0 65 66 69 6e 65 20 45 50 5f 49 6e 66 69 78 46 75 efine EP_InfixFu
652b0 6e 63 20 20 30 78 30 30 38 30 20 20 2f 2a 20 54 nc 0x0080 /* T
652c0 72 75 65 20 66 6f 72 20 61 6e 20 69 6e 66 69 78 rue for an infix
652d0 20 66 75 6e 63 74 69 6f 6e 3a 20 4c 49 4b 45 2c function: LIKE,
652e0 20 47 4c 4f 42 2c 20 65 74 63 20 2a 2f 0a 23 64 GLOB, etc */.#d
652f0 65 66 69 6e 65 20 45 50 5f 45 78 70 43 6f 6c 6c efine EP_ExpColl
65300 61 74 65 20 30 78 30 31 30 30 20 20 2f 2a 20 43 ate 0x0100 /* C
65310 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
65320 65 20 73 70 65 63 69 66 69 65 64 20 65 78 70 6c e specified expl
65330 69 63 69 74 6c 79 20 2a 2f 0a 23 64 65 66 69 6e icitly */.#defin
65340 65 20 45 50 5f 41 6e 79 41 66 66 20 20 20 20 20 e EP_AnyAff
65350 30 78 30 32 30 30 20 20 2f 2a 20 43 61 6e 20 74 0x0200 /* Can t
65360 61 6b 65 20 61 20 63 61 63 68 65 64 20 63 6f 6c ake a cached col
65370 75 6d 6e 20 6f 66 20 61 6e 79 20 61 66 66 69 6e umn of any affin
65380 69 74 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 ity */.#define E
65390 50 5f 46 69 78 65 64 44 65 73 74 20 20 30 78 30 P_FixedDest 0x0
653a0 34 30 30 20 20 2f 2a 20 52 65 73 75 6c 74 20 6e 400 /* Result n
653b0 65 65 64 65 64 20 69 6e 20 61 20 73 70 65 63 69 eeded in a speci
653c0 66 69 63 20 72 65 67 69 73 74 65 72 20 2a 2f 0a fic register */.
653d0 23 64 65 66 69 6e 65 20 45 50 5f 49 6e 74 56 61 #define EP_IntVa
653e0 6c 75 65 20 20 20 30 78 30 38 30 30 20 20 2f 2a lue 0x0800 /*
653f0 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20 63 Integer value c
65400 6f 6e 74 61 69 6e 65 64 20 69 6e 20 75 2e 69 56 ontained in u.iV
65410 61 6c 75 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 alue */.#define
65420 45 50 5f 78 49 73 53 65 6c 65 63 74 20 20 30 78 EP_xIsSelect 0x
65430 31 30 30 30 20 20 2f 2a 20 78 2e 70 53 65 6c 65 1000 /* x.pSele
65440 63 74 20 69 73 20 76 61 6c 69 64 20 28 6f 74 68 ct is valid (oth
65450 65 72 77 69 73 65 20 78 2e 70 4c 69 73 74 20 69 erwise x.pList i
65460 73 29 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 45 s) */..#define E
65470 50 5f 52 65 64 75 63 65 64 20 20 20 20 30 78 32 P_Reduced 0x2
65480 30 30 30 20 20 2f 2a 20 45 78 70 72 20 73 74 72 000 /* Expr str
65490 75 63 74 20 69 73 20 45 58 50 52 5f 52 45 44 55 uct is EXPR_REDU
654a0 43 45 44 53 49 5a 45 20 62 79 74 65 73 20 6f 6e CEDSIZE bytes on
654b0 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 ly */.#define EP
654c0 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 20 30 78 34 30 _TokenOnly 0x40
654d0 30 30 20 20 2f 2a 20 45 78 70 72 20 73 74 72 75 00 /* Expr stru
654e0 63 74 20 69 73 20 45 58 50 52 5f 54 4f 4b 45 4e ct is EXPR_TOKEN
654f0 4f 4e 4c 59 53 49 5a 45 20 62 79 74 65 73 20 6f ONLYSIZE bytes o
65500 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 nly */.#define E
65510 50 5f 53 74 61 74 69 63 20 20 20 20 20 30 78 38 P_Static 0x8
65520 30 30 30 20 20 2f 2a 20 48 65 6c 64 20 69 6e 20 000 /* Held in
65530 6d 65 6d 6f 72 79 20 6e 6f 74 20 6f 62 74 61 69 memory not obtai
65540 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 ned from malloc(
65550 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ) */../*.** The
65560 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 68 following are th
65570 65 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20 62 69 e meanings of bi
65580 74 73 20 69 6e 20 74 68 65 20 45 78 70 72 2e 66 ts in the Expr.f
65590 6c 61 67 73 32 20 66 69 65 6c 64 2e 0a 2a 2f 0a lags2 field..*/.
655a0 23 64 65 66 69 6e 65 20 45 50 32 5f 4d 61 6c 6c #define EP2_Mall
655b0 6f 63 65 64 54 6f 6b 65 6e 20 20 30 78 30 30 30 ocedToken 0x000
655c0 31 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 73 71 1 /* Need to sq
655d0 6c 69 74 65 33 44 62 46 72 65 65 28 29 20 45 78 lite3DbFree() Ex
655e0 70 72 2e 7a 54 6f 6b 65 6e 20 2a 2f 0a 23 64 65 pr.zToken */.#de
655f0 66 69 6e 65 20 45 50 32 5f 49 72 72 65 64 75 63 fine EP2_Irreduc
65600 69 62 6c 65 20 20 20 20 30 78 30 30 30 32 20 20 ible 0x0002
65610 2f 2a 20 43 61 6e 6e 6f 74 20 45 58 50 52 44 55 /* Cannot EXPRDU
65620 50 5f 52 45 44 55 43 45 20 74 68 69 73 20 45 78 P_REDUCE this Ex
65630 70 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 pr */../*.** The
65640 20 70 73 65 75 64 6f 2d 72 6f 75 74 69 6e 65 20 pseudo-routine
65650 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 49 72 sqlite3ExprSetIr
65660 72 65 64 75 63 69 62 6c 65 20 73 65 74 73 20 74 reducible sets t
65670 68 65 20 45 50 32 5f 49 72 72 65 64 75 63 69 62 he EP2_Irreducib
65680 6c 65 0a 2a 2a 20 66 6c 61 67 20 6f 6e 20 61 6e le.** flag on an
65690 20 65 78 70 72 65 73 73 69 6f 6e 20 73 74 72 75 expression stru
656a0 63 74 75 72 65 2e 20 20 54 68 69 73 20 66 6c 61 cture. This fla
656b0 67 20 69 73 20 75 73 65 64 20 66 6f 72 20 56 56 g is used for VV
656c0 26 41 20 6f 6e 6c 79 2e 20 20 54 68 65 0a 2a 2a &A only. The.**
656d0 20 72 6f 75 74 69 6e 65 20 69 73 20 69 6d 70 6c routine is impl
656e0 65 6d 65 6e 74 65 64 20 61 73 20 61 20 6d 61 63 emented as a mac
656f0 72 6f 20 74 68 61 74 20 6f 6e 6c 79 20 77 6f 72 ro that only wor
65700 6b 73 20 77 68 65 6e 20 69 6e 20 64 65 62 75 67 ks when in debug
65710 67 69 6e 67 20 6d 6f 64 65 2c 0a 2a 2a 20 73 6f ging mode,.** so
65720 20 61 73 20 6e 6f 74 20 74 6f 20 62 75 72 64 65 as not to burde
65730 6e 20 70 72 6f 64 75 63 74 69 6f 6e 20 63 6f 64 n production cod
65740 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c e..*/.#ifdef SQL
65750 49 54 45 5f 44 45 42 55 47 0a 23 20 64 65 66 69 ITE_DEBUG.# defi
65760 6e 65 20 45 78 70 72 53 65 74 49 72 72 65 64 75 ne ExprSetIrredu
65770 63 69 62 6c 65 28 58 29 20 20 28 58 29 2d 3e 66 cible(X) (X)->f
65780 6c 61 67 73 32 20 7c 3d 20 45 50 32 5f 49 72 72 lags2 |= EP2_Irr
65790 65 64 75 63 69 62 6c 65 0a 23 65 6c 73 65 0a 23 educible.#else.#
657a0 20 64 65 66 69 6e 65 20 45 78 70 72 53 65 74 49 define ExprSetI
657b0 72 72 65 64 75 63 69 62 6c 65 28 58 29 0a 23 65 rreducible(X).#e
657c0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 ndif../*.** Thes
657d0 65 20 6d 61 63 72 6f 73 20 63 61 6e 20 62 65 20 e macros can be
657e0 75 73 65 64 20 74 6f 20 74 65 73 74 2c 20 73 65 used to test, se
657f0 74 2c 20 6f 72 20 63 6c 65 61 72 20 62 69 74 73 t, or clear bits
65800 20 69 6e 20 74 68 65 20 0a 2a 2a 20 45 78 70 72 in the .** Expr
65810 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f .flags field..*/
65820 0a 23 64 65 66 69 6e 65 20 45 78 70 72 48 61 73 .#define ExprHas
65830 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20 20 Property(E,P)
65840 20 20 28 28 28 45 29 2d 3e 66 6c 61 67 73 26 28 (((E)->flags&(
65850 50 29 29 3d 3d 28 50 29 29 0a 23 64 65 66 69 6e P))==(P)).#defin
65860 65 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 e ExprHasAnyProp
65870 65 72 74 79 28 45 2c 50 29 20 20 28 28 28 45 29 erty(E,P) (((E)
65880 2d 3e 66 6c 61 67 73 26 28 50 29 29 21 3d 30 29 ->flags&(P))!=0)
65890 0a 23 64 65 66 69 6e 65 20 45 78 70 72 53 65 74 .#define ExprSet
658a0 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20 20 Property(E,P)
658b0 20 20 28 45 29 2d 3e 66 6c 61 67 73 7c 3d 28 50 (E)->flags|=(P
658c0 29 0a 23 64 65 66 69 6e 65 20 45 78 70 72 43 6c ).#define ExprCl
658d0 65 61 72 50 72 6f 70 65 72 74 79 28 45 2c 50 29 earProperty(E,P)
658e0 20 20 20 28 45 29 2d 3e 66 6c 61 67 73 26 3d 7e (E)->flags&=~
658f0 28 50 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f (P)../*.** Macro
65900 73 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 s to determine t
65910 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 he number of byt
65920 65 73 20 72 65 71 75 69 72 65 64 20 62 79 20 61 es required by a
65930 20 6e 6f 72 6d 61 6c 20 45 78 70 72 20 0a 2a 2a normal Expr .**
65940 20 73 74 72 75 63 74 2c 20 61 6e 20 45 78 70 72 struct, an Expr
65950 20 73 74 72 75 63 74 20 77 69 74 68 20 74 68 65 struct with the
65960 20 45 50 5f 52 65 64 75 63 65 64 20 66 6c 61 67 EP_Reduced flag
65970 20 73 65 74 20 69 6e 20 45 78 70 72 2e 66 6c 61 set in Expr.fla
65980 67 73 20 0a 2a 2a 20 61 6e 64 20 61 6e 20 45 78 gs .** and an Ex
65990 70 72 20 73 74 72 75 63 74 20 77 69 74 68 20 74 pr struct with t
659a0 68 65 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 he EP_TokenOnly
659b0 66 6c 61 67 20 73 65 74 2e 0a 2a 2f 0a 23 64 65 flag set..*/.#de
659c0 66 69 6e 65 20 45 58 50 52 5f 46 55 4c 4c 53 49 fine EXPR_FULLSI
659d0 5a 45 20 20 20 20 20 20 20 20 20 20 20 73 69 7a ZE siz
659e0 65 6f 66 28 45 78 70 72 29 20 20 20 20 20 20 20 eof(Expr)
659f0 20 20 20 20 2f 2a 20 46 75 6c 6c 20 73 69 7a 65 /* Full size
65a00 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58 50 52 */.#define EXPR
65a10 5f 52 45 44 55 43 45 44 53 49 5a 45 20 20 20 20 _REDUCEDSIZE
65a20 20 20 20 20 6f 66 66 73 65 74 6f 66 28 45 78 70 offsetof(Exp
65a30 72 2c 69 54 61 62 6c 65 29 20 20 2f 2a 20 43 6f r,iTable) /* Co
65a40 6d 6d 6f 6e 20 66 65 61 74 75 72 65 73 20 2a 2f mmon features */
65a50 0a 23 64 65 66 69 6e 65 20 45 58 50 52 5f 54 4f .#define EXPR_TO
65a60 4b 45 4e 4f 4e 4c 59 53 49 5a 45 20 20 20 20 20 KENONLYSIZE
65a70 20 6f 66 66 73 65 74 6f 66 28 45 78 70 72 2c 70 offsetof(Expr,p
65a80 4c 65 66 74 29 20 20 20 2f 2a 20 46 65 77 65 72 Left) /* Fewer
65a90 20 66 65 61 74 75 72 65 73 20 2a 2f 0a 0a 2f 2a features */../*
65aa0 0a 2a 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64 .** Flags passed
65ab0 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 45 to the sqlite3E
65ac0 78 70 72 44 75 70 28 29 20 66 75 6e 63 74 69 6f xprDup() functio
65ad0 6e 2e 20 53 65 65 20 74 68 65 20 68 65 61 64 65 n. See the heade
65ae0 72 20 63 6f 6d 6d 65 6e 74 20 0a 2a 2a 20 61 62 r comment .** ab
65af0 6f 76 65 20 73 71 6c 69 74 65 33 45 78 70 72 44 ove sqlite3ExprD
65b00 75 70 28 29 20 66 6f 72 20 64 65 74 61 69 6c 73 up() for details
65b10 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58 50 ..*/.#define EXP
65b20 52 44 55 50 5f 52 45 44 55 43 45 20 20 20 20 20 RDUP_REDUCE
65b30 20 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 55 0x0001 /* U
65b40 73 65 64 20 72 65 64 75 63 65 64 2d 73 69 7a 65 sed reduced-size
65b50 20 45 78 70 72 20 6e 6f 64 65 73 20 2a 2f 0a 0a Expr nodes */..
65b60 2f 2a 0a 2a 2a 20 41 20 6c 69 73 74 20 6f 66 20 /*.** A list of
65b70 65 78 70 72 65 73 73 69 6f 6e 73 2e 20 20 45 61 expressions. Ea
65b80 63 68 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 61 ch expression ma
65b90 79 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 68 61 76 y optionally hav
65ba0 65 20 61 0a 2a 2a 20 6e 61 6d 65 2e 20 20 41 6e e a.** name. An
65bb0 20 65 78 70 72 2f 6e 61 6d 65 20 63 6f 6d 62 69 expr/name combi
65bc0 6e 61 74 69 6f 6e 20 63 61 6e 20 62 65 20 75 73 nation can be us
65bd0 65 64 20 69 6e 20 73 65 76 65 72 61 6c 20 77 61 ed in several wa
65be0 79 73 2c 20 73 75 63 68 0a 2a 2a 20 61 73 20 74 ys, such.** as t
65bf0 68 65 20 6c 69 73 74 20 6f 66 20 22 65 78 70 72 he list of "expr
65c00 20 41 53 20 49 44 22 20 66 69 65 6c 64 73 20 66 AS ID" fields f
65c10 6f 6c 6c 6f 77 69 6e 67 20 61 20 22 53 45 4c 45 ollowing a "SELE
65c20 43 54 22 20 6f 72 20 69 6e 20 74 68 65 0a 2a 2a CT" or in the.**
65c30 20 6c 69 73 74 20 6f 66 20 22 49 44 20 3d 20 65 list of "ID = e
65c40 78 70 72 22 20 69 74 65 6d 73 20 69 6e 20 61 6e xpr" items in an
65c50 20 55 50 44 41 54 45 2e 20 20 41 20 6c 69 73 74 UPDATE. A list
65c60 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 of expressions
65c70 63 61 6e 0a 2a 2a 20 61 6c 73 6f 20 62 65 20 75 can.** also be u
65c80 73 65 64 20 61 73 20 74 68 65 20 61 72 67 75 6d sed as the argum
65c90 65 6e 74 20 74 6f 20 61 20 66 75 6e 63 74 69 6f ent to a functio
65ca0 6e 2c 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 n, in which case
65cb0 20 74 68 65 20 61 2e 7a 4e 61 6d 65 0a 2a 2a 20 the a.zName.**
65cc0 66 69 65 6c 64 20 69 73 20 6e 6f 74 20 75 73 65 field is not use
65cd0 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 45 78 70 d..*/.struct Exp
65ce0 72 4c 69 73 74 20 7b 0a 20 20 69 6e 74 20 6e 45 rList {. int nE
65cf0 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 xpr;
65d00 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 78 /* Number of ex
65d10 70 72 65 73 73 69 6f 6e 73 20 6f 6e 20 74 68 65 pressions on the
65d20 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e list */. int n
65d30 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 20 Alloc;
65d40 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 /* Number of e
65d50 6e 74 72 69 65 73 20 61 6c 6c 6f 63 61 74 65 64 ntries allocated
65d60 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74 20 below */. int
65d70 69 45 43 75 72 73 6f 72 3b 20 20 20 20 20 20 20 iECursor;
65d80 20 20 20 2f 2a 20 56 44 42 45 20 43 75 72 73 6f /* VDBE Curso
65d90 72 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 r associated wit
65da0 68 20 74 68 69 73 20 45 78 70 72 4c 69 73 74 20 h this ExprList
65db0 2a 2f 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 */. struct Expr
65dc0 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20 20 20 List_item {.
65dd0 45 78 70 72 20 2a 70 45 78 70 72 3b 20 20 20 20 Expr *pExpr;
65de0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 69 /* The li
65df0 73 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e st of expression
65e00 73 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a s */. char *z
65e10 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 Name;
65e20 2f 2a 20 54 6f 6b 65 6e 20 61 73 73 6f 63 69 61 /* Token associa
65e30 74 65 64 20 77 69 74 68 20 74 68 69 73 20 65 78 ted with this ex
65e40 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 pression */.
65e50 63 68 61 72 20 2a 7a 53 70 61 6e 3b 20 20 20 20 char *zSpan;
65e60 20 20 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e /* Origin
65e70 61 6c 20 74 65 78 74 20 6f 66 20 74 68 65 20 65 al text of the e
65e80 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 xpression */.
65e90 20 75 38 20 73 6f 72 74 4f 72 64 65 72 3b 20 20 u8 sortOrder;
65ea0 20 20 20 20 20 20 20 20 2f 2a 20 31 20 66 6f 72 /* 1 for
65eb0 20 44 45 53 43 20 6f 72 20 30 20 66 6f 72 20 41 DESC or 0 for A
65ec0 53 43 20 2a 2f 0a 20 20 20 20 75 38 20 64 6f 6e SC */. u8 don
65ed0 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
65ee0 20 2f 2a 20 41 20 66 6c 61 67 20 74 6f 20 69 6e /* A flag to in
65ef0 64 69 63 61 74 65 20 77 68 65 6e 20 70 72 6f 63 dicate when proc
65f00 65 73 73 69 6e 67 20 69 73 20 66 69 6e 69 73 68 essing is finish
65f10 65 64 20 2a 2f 0a 20 20 20 20 75 31 36 20 69 43 ed */. u16 iC
65f20 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ol;
65f30 20 2f 2a 20 46 6f 72 20 4f 52 44 45 52 20 42 59 /* For ORDER BY
65f40 2c 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 , column number
65f50 69 6e 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f in result set */
65f60 0a 20 20 20 20 75 31 36 20 69 41 6c 69 61 73 3b . u16 iAlias;
65f70 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
65f80 6e 64 65 78 20 69 6e 74 6f 20 50 61 72 73 65 2e ndex into Parse.
65f90 61 41 6c 69 61 73 5b 5d 20 66 6f 72 20 7a 4e 61 aAlias[] for zNa
65fa0 6d 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b 20 20 20 me */. } *a;
65fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
65fc0 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72 20 * One entry for
65fd0 65 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e 20 each expression
65fe0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 */.};../*.** An
65ff0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 instance of this
66000 20 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73 structure is us
66010 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 ed by the parser
66020 20 74 6f 20 72 65 63 6f 72 64 20 62 6f 74 68 0a to record both.
66030 2a 2a 20 74 68 65 20 70 61 72 73 65 20 74 72 65 ** the parse tre
66040 65 20 66 6f 72 20 61 6e 20 65 78 70 72 65 73 73 e for an express
66050 69 6f 6e 20 61 6e 64 20 74 68 65 20 73 70 61 6e ion and the span
66060 20 6f 66 20 69 6e 70 75 74 20 74 65 78 74 20 66 of input text f
66070 6f 72 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 or an.** express
66080 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 45 ion..*/.struct E
66090 78 70 72 53 70 61 6e 20 7b 0a 20 20 45 78 70 72 xprSpan {. Expr
660a0 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 *pExpr;
660b0 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 /* The express
660c0 69 6f 6e 20 70 61 72 73 65 20 74 72 65 65 20 2a ion parse tree *
660d0 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
660e0 7a 53 74 61 72 74 3b 20 20 20 2f 2a 20 46 69 72 zStart; /* Fir
660f0 73 74 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 st character of
66100 69 6e 70 75 74 20 74 65 78 74 20 2a 2f 0a 20 20 input text */.
66110 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 6e 64 const char *zEnd
66120 3b 20 20 20 20 20 2f 2a 20 4f 6e 65 20 63 68 61 ; /* One cha
66130 72 61 63 74 65 72 20 70 61 73 74 20 74 68 65 20 racter past the
66140 65 6e 64 20 6f 66 20 69 6e 70 75 74 20 74 65 78 end of input tex
66150 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 t */.};../*.** A
66160 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
66170 69 73 20 73 74 72 75 63 74 75 72 65 20 63 61 6e is structure can
66180 20 68 6f 6c 64 20 61 20 73 69 6d 70 6c 65 20 6c hold a simple l
66190 69 73 74 20 6f 66 20 69 64 65 6e 74 69 66 69 65 ist of identifie
661a0 72 73 2c 0a 2a 2a 20 73 75 63 68 20 61 73 20 74 rs,.** such as t
661b0 68 65 20 6c 69 73 74 20 22 61 2c 62 2c 63 22 20 he list "a,b,c"
661c0 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 in the following
661d0 20 73 74 61 74 65 6d 65 6e 74 73 3a 0a 2a 2a 0a statements:.**.
661e0 2a 2a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 ** INSERT I
661f0 4e 54 4f 20 74 28 61 2c 62 2c 63 29 20 56 41 4c NTO t(a,b,c) VAL
66200 55 45 53 20 2e 2e 2e 3b 0a 2a 2a 20 20 20 20 20 UES ...;.**
66210 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 69 64 CREATE INDEX id
66220 78 20 4f 4e 20 74 28 61 2c 62 2c 63 29 3b 0a 2a x ON t(a,b,c);.*
66230 2a 20 20 20 20 20 20 43 52 45 41 54 45 20 54 52 * CREATE TR
66240 49 47 47 45 52 20 74 72 69 67 20 42 45 46 4f 52 IGGER trig BEFOR
66250 45 20 55 50 44 41 54 45 20 4f 4e 20 74 28 61 2c E UPDATE ON t(a,
66260 62 2c 63 29 20 2e 2e 2e 3b 0a 2a 2a 0a 2a 2a 20 b,c) ...;.**.**
66270 54 68 65 20 49 64 4c 69 73 74 2e 61 2e 69 64 78 The IdList.a.idx
66280 20 66 69 65 6c 64 20 69 73 20 75 73 65 64 20 77 field is used w
66290 68 65 6e 20 74 68 65 20 49 64 4c 69 73 74 20 72 hen the IdList r
662a0 65 70 72 65 73 65 6e 74 73 20 74 68 65 20 6c 69 epresents the li
662b0 73 74 20 6f 66 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 st of.** column
662c0 6e 61 6d 65 73 20 61 66 74 65 72 20 61 20 74 61 names after a ta
662d0 62 6c 65 20 6e 61 6d 65 20 69 6e 20 61 6e 20 49 ble name in an I
662e0 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 2e NSERT statement.
662f0 20 20 49 6e 20 74 68 65 20 73 74 61 74 65 6d 65 In the stateme
66300 6e 74 0a 2a 2a 0a 2a 2a 20 20 20 20 20 49 4e 53 nt.**.** INS
66310 45 52 54 20 49 4e 54 4f 20 74 28 61 2c 62 2c 63 ERT INTO t(a,b,c
66320 29 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 22 ) ....**.** If "
66330 61 22 20 69 73 20 74 68 65 20 6b 2d 74 68 20 63 a" is the k-th c
66340 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 22 olumn of table "
66350 74 22 2c 20 74 68 65 6e 20 49 64 4c 69 73 74 2e t", then IdList.
66360 61 5b 30 5d 2e 69 64 78 3d 3d 6b 2e 0a 2a 2f 0a a[0].idx==k..*/.
66370 73 74 72 75 63 74 20 49 64 4c 69 73 74 20 7b 0a struct IdList {.
66380 20 20 73 74 72 75 63 74 20 49 64 4c 69 73 74 5f struct IdList_
66390 69 74 65 6d 20 7b 0a 20 20 20 20 63 68 61 72 20 item {. char
663a0 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20 *zName; /*
663b0 4e 61 6d 65 20 6f 66 20 74 68 65 20 69 64 65 6e Name of the iden
663c0 74 69 66 69 65 72 20 2a 2f 0a 20 20 20 20 69 6e tifier */. in
663d0 74 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 20 t idx;
663e0 2f 2a 20 49 6e 64 65 78 20 69 6e 20 73 6f 6d 65 /* Index in some
663f0 20 54 61 62 6c 65 2e 61 43 6f 6c 5b 5d 20 6f 66 Table.aCol[] of
66400 20 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 a column named
66410 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b zName */. } *a;
66420 0a 20 20 69 6e 74 20 6e 49 64 3b 20 20 20 20 20 . int nId;
66430 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
66440 20 69 64 65 6e 74 69 66 69 65 72 73 20 6f 6e 20 identifiers on
66450 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e the list */. in
66460 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 2f t nAlloc; /
66470 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 * Number of entr
66480 69 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f ies allocated fo
66490 72 20 61 5b 5d 20 62 65 6c 6f 77 20 2a 2f 0a 7d r a[] below */.}
664a0 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 62 69 74 ;../*.** The bit
664b0 6d 61 73 6b 20 64 61 74 61 74 79 70 65 20 64 65 mask datatype de
664c0 66 69 6e 65 64 20 62 65 6c 6f 77 20 69 73 20 75 fined below is u
664d0 73 65 64 20 66 6f 72 20 76 61 72 69 6f 75 73 20 sed for various
664e0 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 2e 0a 2a optimizations..*
664f0 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 68 *.** Changing th
66500 69 73 20 66 72 6f 6d 20 61 20 36 34 2d 62 69 74 is from a 64-bit
66510 20 74 6f 20 61 20 33 32 2d 62 69 74 20 74 79 70 to a 32-bit typ
66520 65 20 6c 69 6d 69 74 73 20 74 68 65 20 6e 75 6d e limits the num
66530 62 65 72 20 6f 66 0a 2a 2a 20 74 61 62 6c 65 73 ber of.** tables
66540 20 69 6e 20 61 20 6a 6f 69 6e 20 74 6f 20 33 32 in a join to 32
66550 20 69 6e 73 74 65 61 64 20 6f 66 20 36 34 2e 20 instead of 64.
66560 20 42 75 74 20 69 74 20 61 6c 73 6f 20 72 65 64 But it also red
66570 75 63 65 73 20 74 68 65 20 73 69 7a 65 0a 2a 2a uces the size.**
66580 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 of the library
66590 62 79 20 37 33 38 20 62 79 74 65 73 20 6f 6e 20 by 738 bytes on
665a0 69 78 38 36 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 ix86..*/.typedef
665b0 20 75 36 34 20 42 69 74 6d 61 73 6b 3b 0a 0a 2f u64 Bitmask;../
665c0 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 *.** The number
665d0 6f 66 20 62 69 74 73 20 69 6e 20 61 20 42 69 74 of bits in a Bit
665e0 6d 61 73 6b 2e 20 20 22 42 4d 53 22 20 6d 65 61 mask. "BMS" mea
665f0 6e 73 20 22 42 69 74 4d 61 73 6b 20 53 69 7a 65 ns "BitMask Size
66600 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 4d "..*/.#define BM
66610 53 20 20 28 28 69 6e 74 29 28 73 69 7a 65 6f 66 S ((int)(sizeof
66620 28 42 69 74 6d 61 73 6b 29 2a 38 29 29 0a 0a 2f (Bitmask)*8))../
66630 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
66640 6e 67 20 73 74 72 75 63 74 75 72 65 20 64 65 73 ng structure des
66650 63 72 69 62 65 73 20 74 68 65 20 46 52 4f 4d 20 cribes the FROM
66660 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45 clause of a SELE
66670 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a CT statement..**
66680 20 45 61 63 68 20 74 61 62 6c 65 20 6f 72 20 73 Each table or s
66690 75 62 71 75 65 72 79 20 69 6e 20 74 68 65 20 46 ubquery in the F
666a0 52 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 61 20 ROM clause is a
666b0 73 65 70 61 72 61 74 65 20 65 6c 65 6d 65 6e 74 separate element
666c0 20 6f 66 0a 2a 2a 20 74 68 65 20 53 72 63 4c 69 of.** the SrcLi
666d0 73 74 2e 61 5b 5d 20 61 72 72 61 79 2e 0a 2a 2a st.a[] array..**
666e0 0a 2a 2a 20 57 69 74 68 20 74 68 65 20 61 64 64 .** With the add
666f0 69 74 69 6f 6e 20 6f 66 20 6d 75 6c 74 69 70 6c ition of multipl
66700 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f e database suppo
66710 72 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e rt, the followin
66720 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 63 g structure.** c
66730 61 6e 20 61 6c 73 6f 20 62 65 20 75 73 65 64 20 an also be used
66740 74 6f 20 64 65 73 63 72 69 62 65 20 61 20 70 61 to describe a pa
66750 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65 20 73 rticular table s
66760 75 63 68 20 61 73 20 74 68 65 20 74 61 62 6c 65 uch as the table
66770 20 74 68 61 74 0a 2a 2a 20 69 73 20 6d 6f 64 69 that.** is modi
66780 66 69 65 64 20 62 79 20 61 6e 20 49 4e 53 45 52 fied by an INSER
66790 54 2c 20 44 45 4c 45 54 45 2c 20 6f 72 20 55 50 T, DELETE, or UP
667a0 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 DATE statement.
667b0 20 49 6e 20 73 74 61 6e 64 61 72 64 20 53 51 4c In standard SQL
667c0 2c 0a 2a 2a 20 73 75 63 68 20 61 20 74 61 62 6c ,.** such a tabl
667d0 65 20 6d 75 73 74 20 62 65 20 61 20 73 69 6d 70 e must be a simp
667e0 6c 65 20 6e 61 6d 65 3a 20 49 44 2e 20 20 42 75 le name: ID. Bu
667f0 74 20 69 6e 20 53 51 4c 69 74 65 2c 20 74 68 65 t in SQLite, the
66800 20 74 61 62 6c 65 20 63 61 6e 0a 2a 2a 20 6e 6f table can.** no
66810 77 20 62 65 20 69 64 65 6e 74 69 66 69 65 64 20 w be identified
66820 62 79 20 61 20 64 61 74 61 62 61 73 65 20 6e 61 by a database na
66830 6d 65 2c 20 61 20 64 6f 74 2c 20 74 68 65 6e 20 me, a dot, then
66840 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 3a 20 the table name:
66850 49 44 2e 49 44 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ID.ID..**.** The
66860 20 6a 6f 69 6e 74 79 70 65 20 73 74 61 72 74 73 jointype starts
66870 20 6f 75 74 20 73 68 6f 77 69 6e 67 20 74 68 65 out showing the
66880 20 6a 6f 69 6e 20 74 79 70 65 20 62 65 74 77 65 join type betwe
66890 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 en the current t
668a0 61 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 65 20 able.** and the
668b0 6e 65 78 74 20 74 61 62 6c 65 20 6f 6e 20 74 68 next table on th
668c0 65 20 6c 69 73 74 2e 20 20 54 68 65 20 70 61 72 e list. The par
668d0 73 65 72 20 62 75 69 6c 64 73 20 74 68 65 20 6c ser builds the l
668e0 69 73 74 20 74 68 69 73 20 77 61 79 2e 0a 2a 2a ist this way..**
668f0 20 42 75 74 20 73 71 6c 69 74 65 33 53 72 63 4c But sqlite3SrcL
66900 69 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 70 65 istShiftJoinType
66910 28 29 20 6c 61 74 65 72 20 73 68 69 66 74 73 20 () later shifts
66920 74 68 65 20 6a 6f 69 6e 74 79 70 65 73 20 73 6f the jointypes so
66930 20 74 68 61 74 20 65 61 63 68 0a 2a 2a 20 6a 6f that each.** jo
66940 69 6e 74 79 70 65 20 65 78 70 72 65 73 73 65 73 intype expresses
66950 20 74 68 65 20 6a 6f 69 6e 20 62 65 74 77 65 65 the join betwee
66960 6e 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 n the table and
66970 74 68 65 20 70 72 65 76 69 6f 75 73 20 74 61 62 the previous tab
66980 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 72 le..*/.struct Sr
66990 63 4c 69 73 74 20 7b 0a 20 20 69 31 36 20 6e 53 cList {. i16 nS
669a0 72 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 rc; /* Nu
669b0 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20 6f mber of tables o
669c0 72 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20 r subqueries in
669d0 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 the FROM clause
669e0 2a 2f 0a 20 20 69 31 36 20 6e 41 6c 6c 6f 63 3b */. i16 nAlloc;
669f0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
66a00 6f 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63 of entries alloc
66a10 61 74 65 64 20 69 6e 20 61 5b 5d 20 62 65 6c 6f ated in a[] belo
66a20 77 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 w */. struct Sr
66a30 63 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20 20 cList_item {.
66a40 20 63 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65 char *zDatabase
66a50 3b 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 ; /* Name of da
66a60 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 74 tabase holding t
66a70 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 his table */.
66a80 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 char *zName;
66a90 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 /* Name of th
66aa0 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 63 e table */. c
66ab0 68 61 72 20 2a 7a 41 6c 69 61 73 3b 20 20 20 20 har *zAlias;
66ac0 20 2f 2a 20 54 68 65 20 22 42 22 20 70 61 72 74 /* The "B" part
66ad0 20 6f 66 20 61 20 22 41 20 41 53 20 42 22 20 70 of a "A AS B" p
66ae0 68 72 61 73 65 2e 20 20 7a 4e 61 6d 65 20 69 73 hrase. zName is
66af0 20 74 68 65 20 22 41 22 20 2a 2f 0a 20 20 20 20 the "A" */.
66b00 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 Table *pTab;
66b10 20 20 2f 2a 20 41 6e 20 53 51 4c 20 74 61 62 6c /* An SQL tabl
66b20 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 e corresponding
66b30 74 6f 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 to zName */.
66b40 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b Select *pSelect;
66b50 20 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 74 /* A SELECT st
66b60 61 74 65 6d 65 6e 74 20 75 73 65 64 20 69 6e 20 atement used in
66b70 70 6c 61 63 65 20 6f 66 20 61 20 74 61 62 6c 65 place of a table
66b80 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 75 38 20 name */. u8
66b90 69 73 50 6f 70 75 6c 61 74 65 64 3b 20 20 20 2f isPopulated; /
66ba0 2a 20 54 65 6d 70 6f 72 61 72 79 20 74 61 62 6c * Temporary tabl
66bb0 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 e associated wit
66bc0 68 20 53 45 4c 45 43 54 20 69 73 20 70 6f 70 75 h SELECT is popu
66bd0 6c 61 74 65 64 20 2a 2f 0a 20 20 20 20 75 38 20 lated */. u8
66be0 6a 6f 69 6e 74 79 70 65 3b 20 20 20 20 20 20 2f jointype; /
66bf0 2a 20 54 79 70 65 20 6f 66 20 6a 6f 69 6e 20 62 * Type of join b
66c00 65 74 77 65 65 6e 20 74 68 69 73 20 61 62 6c 65 etween this able
66c10 20 61 6e 64 20 74 68 65 20 70 72 65 76 69 6f 75 and the previou
66c20 73 20 2a 2f 0a 20 20 20 20 75 38 20 6e 6f 74 49 s */. u8 notI
66c30 6e 64 65 78 65 64 3b 20 20 20 20 2f 2a 20 54 72 ndexed; /* Tr
66c40 75 65 20 69 66 20 74 68 65 72 65 20 69 73 20 61 ue if there is a
66c50 20 4e 4f 54 20 49 4e 44 45 58 45 44 20 63 6c 61 NOT INDEXED cla
66c60 75 73 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 use */. int i
66c70 43 75 72 73 6f 72 3b 20 20 20 20 20 20 2f 2a 20 Cursor; /*
66c80 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 The VDBE cursor
66c90 6e 75 6d 62 65 72 20 75 73 65 64 20 74 6f 20 61 number used to a
66ca0 63 63 65 73 73 20 74 68 69 73 20 74 61 62 6c 65 ccess this table
66cb0 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 4f */. Expr *pO
66cc0 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 n; /* The
66cd0 20 4f 4e 20 63 6c 61 75 73 65 20 6f 66 20 61 20 ON clause of a
66ce0 6a 6f 69 6e 20 2a 2f 0a 20 20 20 20 49 64 4c 69 join */. IdLi
66cf0 73 74 20 2a 70 55 73 69 6e 67 3b 20 20 20 2f 2a st *pUsing; /*
66d00 20 54 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73 The USING claus
66d10 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 20 e of a join */.
66d20 20 20 20 42 69 74 6d 61 73 6b 20 63 6f 6c 55 73 Bitmask colUs
66d30 65 64 3b 20 20 2f 2a 20 42 69 74 20 4e 20 28 31 ed; /* Bit N (1
66d40 3c 3c 4e 29 20 73 65 74 20 69 66 20 63 6f 6c 75 <<N) set if colu
66d50 6d 6e 20 4e 20 6f 66 20 70 54 61 62 20 69 73 20 mn N of pTab is
66d60 75 73 65 64 20 2a 2f 0a 20 20 20 20 63 68 61 72 used */. char
66d70 20 2a 7a 49 6e 64 65 78 3b 20 20 20 20 20 2f 2a *zIndex; /*
66d80 20 49 64 65 6e 74 69 66 69 65 72 20 66 72 6f 6d Identifier from
66d90 20 22 49 4e 44 45 58 45 44 20 42 59 20 3c 7a 49 "INDEXED BY <zI
66da0 6e 64 65 78 3e 22 20 63 6c 61 75 73 65 20 2a 2f ndex>" clause */
66db0 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 . Index *pInd
66dc0 65 78 3b 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 ex; /* Index
66dd0 73 74 72 75 63 74 75 72 65 20 63 6f 72 72 65 73 structure corres
66de0 70 6f 6e 64 69 6e 67 20 74 6f 20 7a 49 6e 64 65 ponding to zInde
66df0 78 2c 20 69 66 20 61 6e 79 20 2a 2f 0a 20 20 7d x, if any */. }
66e00 20 61 5b 31 5d 3b 20 20 20 20 20 20 20 20 20 20 a[1];
66e10 20 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20 /* One entry
66e20 66 6f 72 20 65 61 63 68 20 69 64 65 6e 74 69 66 for each identif
66e30 69 65 72 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 ier on the list
66e40 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 */.};../*.** Per
66e50 6d 69 74 74 65 64 20 76 61 6c 75 65 73 20 6f 66 mitted values of
66e60 20 74 68 65 20 53 72 63 4c 69 73 74 2e 61 2e 6a the SrcList.a.j
66e70 6f 69 6e 74 79 70 65 20 66 69 65 6c 64 0a 2a 2f ointype field.*/
66e80 0a 23 64 65 66 69 6e 65 20 4a 54 5f 49 4e 4e 45 .#define JT_INNE
66e90 52 20 20 20 20 20 30 78 30 30 30 31 20 20 20 20 R 0x0001
66ea0 2f 2a 20 41 6e 79 20 6b 69 6e 64 20 6f 66 20 69 /* Any kind of i
66eb0 6e 6e 65 72 20 6f 72 20 63 72 6f 73 73 20 6a 6f nner or cross jo
66ec0 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 in */.#define JT
66ed0 5f 43 52 4f 53 53 20 20 20 20 20 30 78 30 30 30 _CROSS 0x000
66ee0 32 20 20 20 20 2f 2a 20 45 78 70 6c 69 63 69 74 2 /* Explicit
66ef0 20 75 73 65 20 6f 66 20 74 68 65 20 43 52 4f 53 use of the CROS
66f00 53 20 6b 65 79 77 6f 72 64 20 2a 2f 0a 23 64 65 S keyword */.#de
66f10 66 69 6e 65 20 4a 54 5f 4e 41 54 55 52 41 4c 20 fine JT_NATURAL
66f20 20 20 30 78 30 30 30 34 20 20 20 20 2f 2a 20 54 0x0004 /* T
66f30 72 75 65 20 66 6f 72 20 61 20 22 6e 61 74 75 72 rue for a "natur
66f40 61 6c 22 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 al" join */.#def
66f50 69 6e 65 20 4a 54 5f 4c 45 46 54 20 20 20 20 20 ine JT_LEFT
66f60 20 30 78 30 30 30 38 20 20 20 20 2f 2a 20 4c 65 0x0008 /* Le
66f70 66 74 20 6f 75 74 65 72 20 6a 6f 69 6e 20 2a 2f ft outer join */
66f80 0a 23 64 65 66 69 6e 65 20 4a 54 5f 52 49 47 48 .#define JT_RIGH
66f90 54 20 20 20 20 20 30 78 30 30 31 30 20 20 20 20 T 0x0010
66fa0 2f 2a 20 52 69 67 68 74 20 6f 75 74 65 72 20 6a /* Right outer j
66fb0 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a oin */.#define J
66fc0 54 5f 4f 55 54 45 52 20 20 20 20 20 30 78 30 30 T_OUTER 0x00
66fd0 32 30 20 20 20 20 2f 2a 20 54 68 65 20 22 4f 55 20 /* The "OU
66fe0 54 45 52 22 20 6b 65 79 77 6f 72 64 20 69 73 20 TER" keyword is
66ff0 70 72 65 73 65 6e 74 20 2a 2f 0a 23 64 65 66 69 present */.#defi
67000 6e 65 20 4a 54 5f 45 52 52 4f 52 20 20 20 20 20 ne JT_ERROR
67010 30 78 30 30 34 30 20 20 20 20 2f 2a 20 75 6e 6b 0x0040 /* unk
67020 6e 6f 77 6e 20 6f 72 20 75 6e 73 75 70 70 6f 72 nown or unsuppor
67030 74 65 64 20 6a 6f 69 6e 20 74 79 70 65 20 2a 2f ted join type */
67040 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 57 68 65 72 65 .../*.** A Where
67050 50 6c 61 6e 20 6f 62 6a 65 63 74 20 68 6f 6c 64 Plan object hold
67060 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 s information th
67070 61 74 20 64 65 73 63 72 69 62 65 73 20 61 20 6c at describes a l
67080 6f 6f 6b 75 70 0a 2a 2a 20 73 74 72 61 74 65 67 ookup.** strateg
67090 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 62 y..**.** This ob
670a0 6a 65 63 74 20 69 73 20 69 6e 74 65 6e 64 65 64 ject is intended
670b0 20 74 6f 20 62 65 20 6f 70 61 71 75 65 20 6f 75 to be opaque ou
670c0 74 73 69 64 65 20 6f 66 20 74 68 65 20 77 68 65 tside of the whe
670d0 72 65 2e 63 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 20 re.c module..**
670e0 49 74 20 69 73 20 69 6e 63 6c 75 64 65 64 20 68 It is included h
670f0 65 72 65 20 6f 6e 6c 79 20 73 6f 20 74 68 61 74 ere only so that
67100 20 74 68 61 74 20 63 6f 6d 70 69 6c 65 72 20 77 that compiler w
67110 69 6c 6c 20 6b 6e 6f 77 20 68 6f 77 20 62 69 67 ill know how big
67120 20 69 74 0a 2a 2a 20 69 73 2e 20 20 4e 6f 6e 65 it.** is. None
67130 20 6f 66 20 74 68 65 20 66 69 65 6c 64 73 20 69 of the fields i
67140 6e 20 74 68 69 73 20 6f 62 6a 65 63 74 20 73 68 n this object sh
67150 6f 75 6c 64 20 62 65 20 75 73 65 64 20 6f 75 74 ould be used out
67160 73 69 64 65 20 6f 66 0a 2a 2a 20 74 68 65 20 77 side of.** the w
67170 68 65 72 65 2e 63 20 6d 6f 64 75 6c 65 2e 0a 2a here.c module..*
67180 2a 0a 2a 2a 20 57 69 74 68 69 6e 20 74 68 65 20 *.** Within the
67190 75 6e 69 6f 6e 2c 20 70 49 64 78 20 69 73 20 6f union, pIdx is o
671a0 6e 6c 79 20 75 73 65 64 20 77 68 65 6e 20 77 73 nly used when ws
671b0 46 6c 61 67 73 26 57 48 45 52 45 5f 49 4e 44 45 Flags&WHERE_INDE
671c0 58 45 44 20 69 73 20 74 72 75 65 2e 0a 2a 2a 20 XED is true..**
671d0 70 54 65 72 6d 20 69 73 20 6f 6e 6c 79 20 75 73 pTerm is only us
671e0 65 64 20 77 68 65 6e 20 77 73 46 6c 61 67 73 26 ed when wsFlags&
671f0 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52 20 69 WHERE_MULTI_OR i
67200 73 20 74 72 75 65 2e 20 20 41 6e 64 20 70 56 74 s true. And pVt
67210 61 62 49 64 78 0a 2a 2a 20 69 73 20 6f 6e 6c 79 abIdx.** is only
67220 20 75 73 65 64 20 77 68 65 6e 20 77 73 46 6c 61 used when wsFla
67230 67 73 26 57 48 45 52 45 5f 56 49 52 54 55 41 4c gs&WHERE_VIRTUAL
67240 54 41 42 4c 45 20 69 73 20 74 72 75 65 2e 20 20 TABLE is true.
67250 49 74 20 69 73 20 6e 65 76 65 72 20 74 68 65 0a It is never the.
67260 2a 2a 20 63 61 73 65 20 74 68 61 74 20 6d 6f 72 ** case that mor
67270 65 20 74 68 61 6e 20 6f 6e 65 20 6f 66 20 74 68 e than one of th
67280 65 73 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 69 ese conditions i
67290 73 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 72 75 63 s true..*/.struc
672a0 74 20 57 68 65 72 65 50 6c 61 6e 20 7b 0a 20 20 t WherePlan {.
672b0 75 33 32 20 77 73 46 6c 61 67 73 3b 20 20 20 20 u32 wsFlags;
672c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
672d0 2a 20 57 48 45 52 45 5f 2a 20 66 6c 61 67 73 20 * WHERE_* flags
672e0 74 68 61 74 20 64 65 73 63 72 69 62 65 20 74 68 that describe th
672f0 65 20 73 74 72 61 74 65 67 79 20 2a 2f 0a 20 20 e strategy */.
67300 75 33 32 20 6e 45 71 3b 20 20 20 20 20 20 20 20 u32 nEq;
67310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
67320 2a 20 4e 75 6d 62 65 72 20 6f 66 20 3d 3d 20 63 * Number of == c
67330 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 onstraints */.
67340 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 49 6e 64 65 union {. Inde
67350 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20 x *pIdx;
67360 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
67370 64 65 78 20 77 68 65 6e 20 57 48 45 52 45 5f 49 dex when WHERE_I
67380 4e 44 45 58 45 44 20 69 73 20 74 72 75 65 20 2a NDEXED is true *
67390 2f 0a 20 20 20 20 73 74 72 75 63 74 20 57 68 65 /. struct Whe
673a0 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20 reTerm *pTerm;
673b0 20 20 20 20 20 2f 2a 20 57 48 45 52 45 20 63 6c /* WHERE cl
673c0 61 75 73 65 20 74 65 72 6d 20 66 6f 72 20 4f 52 ause term for OR
673d0 2d 73 65 61 72 63 68 20 2a 2f 0a 20 20 20 20 73 -search */. s
673e0 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 qlite3_index_inf
673f0 6f 20 2a 70 56 74 61 62 49 64 78 3b 20 20 2f 2a o *pVtabIdx; /*
67400 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 Virtual table i
67410 6e 64 65 78 20 74 6f 20 75 73 65 20 2a 2f 0a 20 ndex to use */.
67420 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 } u;.};../*.**
67430 46 6f 72 20 65 61 63 68 20 6e 65 73 74 65 64 20 For each nested
67440 6c 6f 6f 70 20 69 6e 20 61 20 57 48 45 52 45 20 loop in a WHERE
67450 63 6c 61 75 73 65 20 69 6d 70 6c 65 6d 65 6e 74 clause implement
67460 61 74 69 6f 6e 2c 20 74 68 65 20 57 68 65 72 65 ation, the Where
67470 49 6e 66 6f 0a 2a 2a 20 73 74 72 75 63 74 75 72 Info.** structur
67480 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e e contains a sin
67490 67 6c 65 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 gle instance of
674a0 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 20 this structure.
674b0 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 0a This structure.
674c0 2a 2a 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 ** is intended t
674d0 6f 20 62 65 20 70 72 69 76 61 74 65 20 74 68 65 o be private the
674e0 20 74 68 65 20 77 68 65 72 65 2e 63 20 6d 6f 64 the where.c mod
674f0 75 6c 65 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e ule and should n
67500 6f 74 20 62 65 0a 2a 2a 20 61 63 63 65 73 73 20 ot be.** access
67510 6f 72 20 6d 6f 64 69 66 69 65 64 20 62 79 20 6f or modified by o
67520 74 68 65 72 20 6d 6f 64 75 6c 65 73 2e 0a 2a 2a ther modules..**
67530 0a 2a 2a 20 54 68 65 20 70 49 64 78 49 6e 66 6f .** The pIdxInfo
67540 20 66 69 65 6c 64 20 69 73 20 75 73 65 64 20 74 field is used t
67550 6f 20 68 65 6c 70 20 70 69 63 6b 20 74 68 65 20 o help pick the
67560 62 65 73 74 20 69 6e 64 65 78 20 6f 6e 20 61 0a best index on a.
67570 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 ** virtual table
67580 2e 20 20 54 68 65 20 70 49 64 78 49 6e 66 6f 20 . The pIdxInfo
67590 70 6f 69 6e 74 65 72 20 63 6f 6e 74 61 69 6e 73 pointer contains
675a0 20 69 6e 64 65 78 69 6e 67 0a 2a 2a 20 69 6e 66 indexing.** inf
675b0 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 ormation for the
675c0 20 69 2d 74 68 20 74 61 62 6c 65 20 69 6e 20 74 i-th table in t
675d0 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 62 he FROM clause b
675e0 65 66 6f 72 65 20 72 65 6f 72 64 65 72 69 6e 67 efore reordering
675f0 2e 0a 2a 2a 20 41 6c 6c 20 74 68 65 20 70 49 64 ..** All the pId
67600 78 49 6e 66 6f 20 70 6f 69 6e 74 65 72 73 20 61 xInfo pointers a
67610 72 65 20 66 72 65 65 64 20 62 79 20 77 68 65 72 re freed by wher
67620 65 49 6e 66 6f 46 72 65 65 28 29 20 69 6e 20 77 eInfoFree() in w
67630 68 65 72 65 2e 63 2e 0a 2a 2a 20 41 6c 6c 20 6f here.c..** All o
67640 74 68 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e ther information
67650 20 69 6e 20 74 68 65 20 69 2d 74 68 20 57 68 65 in the i-th Whe
67660 72 65 4c 65 76 65 6c 20 6f 62 6a 65 63 74 20 66 reLevel object f
67670 6f 72 20 74 68 65 20 69 2d 74 68 20 74 61 62 6c or the i-th tabl
67680 65 0a 2a 2a 20 61 66 74 65 72 20 46 52 4f 4d 20 e.** after FROM
67690 63 6c 61 75 73 65 20 6f 72 64 65 72 69 6e 67 2e clause ordering.
676a0 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72 65 .*/.struct Where
676b0 4c 65 76 65 6c 20 7b 0a 20 20 57 68 65 72 65 50 Level {. WhereP
676c0 6c 61 6e 20 70 6c 61 6e 3b 20 20 20 20 20 20 20 lan plan;
676d0 2f 2a 20 71 75 65 72 79 20 70 6c 61 6e 20 66 6f /* query plan fo
676e0 72 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 6f r this element o
676f0 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 f the FROM claus
67700 65 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 65 66 74 e */. int iLeft
67710 4a 6f 69 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20 Join; /*
67720 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 Memory cell used
67730 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 4c 45 to implement LE
67740 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20 2a 2f FT OUTER JOIN */
67750 0a 20 20 69 6e 74 20 69 54 61 62 43 75 72 3b 20 . int iTabCur;
67760 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
67770 56 44 42 45 20 63 75 72 73 6f 72 20 75 73 65 64 VDBE cursor used
67780 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 74 to access the t
67790 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 49 able */. int iI
677a0 64 78 43 75 72 3b 20 20 20 20 20 20 20 20 20 20 dxCur;
677b0 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73 /* The VDBE curs
677c0 6f 72 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 or used to acces
677d0 73 20 70 49 64 78 20 2a 2f 0a 20 20 69 6e 74 20 s pIdx */. int
677e0 61 64 64 72 42 72 6b 3b 20 20 20 20 20 20 20 20 addrBrk;
677f0 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 /* Jump here t
67800 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 o break out of t
67810 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 he loop */. int
67820 20 61 64 64 72 4e 78 74 3b 20 20 20 20 20 20 20 addrNxt;
67830 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 /* Jump here
67840 74 6f 20 73 74 61 72 74 20 74 68 65 20 6e 65 78 to start the nex
67850 74 20 49 4e 20 63 6f 6d 62 69 6e 61 74 69 6f 6e t IN combination
67860 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 43 6f */. int addrCo
67870 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4a nt; /* J
67880 75 6d 70 20 68 65 72 65 20 74 6f 20 63 6f 6e 74 ump here to cont
67890 69 6e 75 65 20 77 69 74 68 20 74 68 65 20 6e 65 inue with the ne
678a0 78 74 20 6c 6f 6f 70 20 63 79 63 6c 65 20 2a 2f xt loop cycle */
678b0 0a 20 20 69 6e 74 20 61 64 64 72 46 69 72 73 74 . int addrFirst
678c0 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 ; /* Firs
678d0 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66 t instruction of
678e0 20 69 6e 74 65 72 69 6f 72 20 6f 66 20 74 68 65 interior of the
678f0 20 6c 6f 6f 70 20 2a 2f 0a 20 20 75 38 20 69 46 loop */. u8 iF
67900 72 6f 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 rom;
67910 20 2f 2a 20 57 68 69 63 68 20 65 6e 74 72 79 20 /* Which entry
67920 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 in the FROM clau
67930 73 65 20 2a 2f 0a 20 20 75 38 20 6f 70 2c 20 70 se */. u8 op, p
67940 35 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 5; /*
67950 20 4f 70 63 6f 64 65 20 61 6e 64 20 50 35 20 6f Opcode and P5 o
67960 66 20 74 68 65 20 6f 70 63 6f 64 65 20 74 68 61 f the opcode tha
67970 74 20 65 6e 64 73 20 74 68 65 20 6c 6f 6f 70 20 t ends the loop
67980 2a 2f 0a 20 20 69 6e 74 20 70 31 2c 20 70 32 3b */. int p1, p2;
67990 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 /* Op
679a0 65 72 61 6e 64 73 20 6f 66 20 74 68 65 20 6f 70 erands of the op
679b0 63 6f 64 65 20 75 73 65 64 20 74 6f 20 65 6e 64 code used to end
679c0 73 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 s the loop */.
679d0 75 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20 20 20 union {
679e0 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 /* Informa
679f0 74 69 6f 6e 20 74 68 61 74 20 64 65 70 65 6e 64 tion that depend
67a00 73 20 6f 6e 20 70 6c 61 6e 2e 77 73 46 6c 61 67 s on plan.wsFlag
67a10 73 20 2a 2f 0a 20 20 20 20 73 74 72 75 63 74 20 s */. struct
67a20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 49 6e 3b {. int nIn;
67a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
67a40 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 Number of entri
67a50 65 73 20 69 6e 20 61 49 6e 4c 6f 6f 70 5b 5d 20 es in aInLoop[]
67a60 2a 2f 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 */. struct
67a70 49 6e 4c 6f 6f 70 20 7b 0a 20 20 20 20 20 20 20 InLoop {.
67a80 20 69 6e 74 20 69 43 75 72 3b 20 20 20 20 20 20 int iCur;
67a90 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 /* The V
67aa0 44 42 45 20 63 75 72 73 6f 72 20 75 73 65 64 20 DBE cursor used
67ab0 62 79 20 74 68 69 73 20 49 4e 20 6f 70 65 72 61 by this IN opera
67ac0 74 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 tor */. i
67ad0 6e 74 20 61 64 64 72 49 6e 54 6f 70 3b 20 20 20 nt addrInTop;
67ae0 20 20 20 20 20 20 2f 2a 20 54 6f 70 20 6f 66 20 /* Top of
67af0 74 68 65 20 49 4e 20 6c 6f 6f 70 20 2a 2f 0a 20 the IN loop */.
67b00 20 20 20 20 20 7d 20 2a 61 49 6e 4c 6f 6f 70 3b } *aInLoop;
67b10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
67b20 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 formation about
67b30 65 61 63 68 20 6e 65 73 74 65 64 20 49 4e 20 6f each nested IN o
67b40 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 7d perator */. }
67b50 20 69 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 in;
67b60 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 /* Used whe
67b70 6e 20 70 6c 61 6e 2e 77 73 46 6c 61 67 73 26 57 n plan.wsFlags&W
67b80 48 45 52 45 5f 49 4e 5f 41 42 4c 45 20 2a 2f 0a HERE_IN_ABLE */.
67b90 20 20 7d 20 75 3b 0a 0a 20 20 2f 2a 20 54 68 65 } u;.. /* The
67ba0 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 6c 64 following field
67bb0 20 69 73 20 72 65 61 6c 6c 79 20 6e 6f 74 20 70 is really not p
67bc0 61 72 74 20 6f 66 20 74 68 65 20 63 75 72 72 65 art of the curre
67bd0 6e 74 20 6c 65 76 65 6c 2e 20 20 42 75 74 0a 20 nt level. But.
67be0 20 2a 2a 20 77 65 20 6e 65 65 64 20 61 20 70 6c ** we need a pl
67bf0 61 63 65 20 74 6f 20 63 61 63 68 65 20 76 69 72 ace to cache vir
67c00 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 64 65 78 tual table index
67c10 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 information for
67c20 20 65 61 63 68 0a 20 20 2a 2a 20 76 69 72 74 75 each. ** virtu
67c30 61 6c 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 al table in the
67c40 46 52 4f 4d 20 63 6c 61 75 73 65 20 61 6e 64 20 FROM clause and
67c50 74 68 65 20 57 68 65 72 65 4c 65 76 65 6c 20 73 the WhereLevel s
67c60 74 72 75 63 74 75 72 65 20 69 73 0a 20 20 2a 2a tructure is. **
67c70 20 61 20 63 6f 6e 76 65 6e 69 65 6e 74 20 70 6c a convenient pl
67c80 61 63 65 20 73 69 6e 63 65 20 74 68 65 72 65 20 ace since there
67c90 69 73 20 6f 6e 65 20 57 68 65 72 65 4c 65 76 65 is one WhereLeve
67ca0 6c 20 66 6f 72 20 65 61 63 68 20 46 52 4f 4d 20 l for each FROM
67cb0 63 6c 61 75 73 65 0a 20 20 2a 2a 20 65 6c 65 6d clause. ** elem
67cc0 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 ent.. */. sqli
67cd0 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a te3_index_info *
67ce0 70 49 64 78 49 6e 66 6f 3b 20 20 2f 2a 20 49 6e pIdxInfo; /* In
67cf0 64 65 78 20 69 6e 66 6f 20 66 6f 72 20 6e 2d 74 dex info for n-t
67d00 68 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20 2a h source table *
67d10 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6c 61 67 /.};../*.** Flag
67d20 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f s appropriate fo
67d30 72 20 74 68 65 20 77 63 74 72 6c 46 6c 61 67 73 r the wctrlFlags
67d40 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 73 71 parameter of sq
67d50 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 lite3WhereBegin(
67d60 29 0a 2a 2a 20 61 6e 64 20 74 68 65 20 57 68 65 ).** and the Whe
67d70 72 65 49 6e 66 6f 2e 77 63 74 72 6c 46 6c 61 67 reInfo.wctrlFlag
67d80 73 20 6d 65 6d 62 65 72 2e 0a 2a 2f 0a 23 64 65 s member..*/.#de
67d90 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 52 fine WHERE_ORDER
67da0 42 59 5f 4e 4f 52 4d 41 4c 20 20 20 30 78 30 30 BY_NORMAL 0x00
67db0 30 30 20 2f 2a 20 4e 6f 2d 6f 70 20 2a 2f 0a 23 00 /* No-op */.#
67dc0 64 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 define WHERE_ORD
67dd0 45 52 42 59 5f 4d 49 4e 20 20 20 20 20 20 30 78 ERBY_MIN 0x
67de0 30 30 30 31 20 2f 2a 20 4f 52 44 45 52 20 42 59 0001 /* ORDER BY
67df0 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 processing for
67e00 6d 69 6e 28 29 20 66 75 6e 63 20 2a 2f 0a 23 64 min() func */.#d
67e10 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 efine WHERE_ORDE
67e20 52 42 59 5f 4d 41 58 20 20 20 20 20 20 30 78 30 RBY_MAX 0x0
67e30 30 30 32 20 2f 2a 20 4f 52 44 45 52 20 42 59 20 002 /* ORDER BY
67e40 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 6d processing for m
67e50 61 78 28 29 20 66 75 6e 63 20 2a 2f 0a 23 64 65 ax() func */.#de
67e60 66 69 6e 65 20 57 48 45 52 45 5f 4f 4e 45 50 41 fine WHERE_ONEPA
67e70 53 53 5f 44 45 53 49 52 45 44 20 20 30 78 30 30 SS_DESIRED 0x00
67e80 30 34 20 2f 2a 20 57 61 6e 74 20 74 6f 20 64 6f 04 /* Want to do
67e90 20 6f 6e 65 2d 70 61 73 73 20 55 50 44 41 54 45 one-pass UPDATE
67ea0 2f 44 45 4c 45 54 45 20 2a 2f 0a 23 64 65 66 69 /DELETE */.#defi
67eb0 6e 65 20 57 48 45 52 45 5f 44 55 50 4c 49 43 41 ne WHERE_DUPLICA
67ec0 54 45 53 5f 4f 4b 20 20 20 20 30 78 30 30 30 38 TES_OK 0x0008
67ed0 20 2f 2a 20 4f 6b 20 74 6f 20 72 65 74 75 72 6e /* Ok to return
67ee0 20 61 20 72 6f 77 20 6d 6f 72 65 20 74 68 61 6e a row more than
67ef0 20 6f 6e 63 65 20 2a 2f 0a 23 64 65 66 69 6e 65 once */.#define
67f00 20 57 48 45 52 45 5f 4f 4d 49 54 5f 4f 50 45 4e WHERE_OMIT_OPEN
67f10 20 20 20 20 20 20 20 20 30 78 30 30 31 30 20 2f 0x0010 /
67f20 2a 20 54 61 62 6c 65 20 63 75 72 73 6f 72 20 61 * Table cursor a
67f30 72 65 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 20 re already open
67f40 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 */.#define WHERE
67f50 5f 4f 4d 49 54 5f 43 4c 4f 53 45 20 20 20 20 20 _OMIT_CLOSE
67f60 20 20 30 78 30 30 32 30 20 2f 2a 20 4f 6d 69 74 0x0020 /* Omit
67f70 20 63 6c 6f 73 65 20 6f 66 20 74 61 62 6c 65 20 close of table
67f80 26 20 69 6e 64 65 78 20 63 75 72 73 6f 72 73 20 & index cursors
67f90 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 */.#define WHERE
67fa0 5f 46 4f 52 43 45 5f 54 41 42 4c 45 20 20 20 20 _FORCE_TABLE
67fb0 20 20 30 78 30 30 34 30 20 2f 2a 20 44 6f 20 6e 0x0040 /* Do n
67fc0 6f 74 20 75 73 65 20 61 6e 20 69 6e 64 65 78 2d ot use an index-
67fd0 6f 6e 6c 79 20 73 65 61 72 63 68 20 2a 2f 0a 0a only search */..
67fe0 2f 2a 0a 2a 2a 20 54 68 65 20 57 48 45 52 45 20 /*.** The WHERE
67ff0 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e clause processin
68000 67 20 72 6f 75 74 69 6e 65 20 68 61 73 20 74 77 g routine has tw
68010 6f 20 68 61 6c 76 65 73 2e 20 20 54 68 65 0a 2a o halves. The.*
68020 2a 20 66 69 72 73 74 20 70 61 72 74 20 64 6f 65 * first part doe
68030 73 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 s the start of t
68040 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 61 6e he WHERE loop an
68050 64 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 d the second.**
68060 68 61 6c 66 20 64 6f 65 73 20 74 68 65 20 74 61 half does the ta
68070 69 6c 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 il of the WHERE
68080 6c 6f 6f 70 2e 20 20 41 6e 20 69 6e 73 74 61 6e loop. An instan
68090 63 65 20 6f 66 0a 2a 2a 20 74 68 69 73 20 73 74 ce of.** this st
680a0 72 75 63 74 75 72 65 20 69 73 20 72 65 74 75 72 ructure is retur
680b0 6e 65 64 20 62 79 20 74 68 65 20 66 69 72 73 74 ned by the first
680c0 20 68 61 6c 66 20 61 6e 64 20 70 61 73 73 65 64 half and passed
680d0 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 73 65 63 .** into the sec
680e0 6f 6e 64 20 68 61 6c 66 20 74 6f 20 67 69 76 65 ond half to give
680f0 20 73 6f 6d 65 20 63 6f 6e 74 69 6e 75 69 74 79 some continuity
68100 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72 ..*/.struct Wher
68110 65 49 6e 66 6f 20 7b 0a 20 20 50 61 72 73 65 20 eInfo {. Parse
68120 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 2f *pParse; /
68130 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f * Parsing and co
68140 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f de generating co
68150 6e 74 65 78 74 20 2a 2f 0a 20 20 75 31 36 20 77 ntext */. u16 w
68160 63 74 72 6c 46 6c 61 67 73 3b 20 20 20 20 20 20 ctrlFlags;
68170 2f 2a 20 46 6c 61 67 73 20 6f 72 69 67 69 6e 61 /* Flags origina
68180 6c 6c 79 20 70 61 73 73 65 64 20 74 6f 20 73 71 lly passed to sq
68190 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 lite3WhereBegin(
681a0 29 20 2a 2f 0a 20 20 75 38 20 6f 6b 4f 6e 65 50 ) */. u8 okOneP
681b0 61 73 73 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f ass; /* O
681c0 6b 20 74 6f 20 75 73 65 20 6f 6e 65 2d 70 61 73 k to use one-pas
681d0 73 20 61 6c 67 6f 72 69 74 68 6d 20 66 6f 72 20 s algorithm for
681e0 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45 UPDATE or DELETE
681f0 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 */. SrcList *p
68200 54 61 62 4c 69 73 74 3b 20 20 20 20 20 20 20 20 TabList;
68210 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 /* List of
68220 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 6a 6f tables in the jo
68230 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 54 6f 70 in */. int iTop
68240 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
68250 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 65 /* The ve
68260 72 79 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 ry beginning of
68270 74 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 2a the WHERE loop *
68280 2f 0a 20 20 69 6e 74 20 69 43 6f 6e 74 69 6e 75 /. int iContinu
68290 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
682a0 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 /* Jump here
682b0 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 to continue with
682c0 20 6e 65 78 74 20 72 65 63 6f 72 64 20 2a 2f 0a next record */.
682d0 20 20 69 6e 74 20 69 42 72 65 61 6b 3b 20 20 20 int iBreak;
682e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
682f0 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f /* Jump here to
68300 20 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 68 break out of th
68310 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 e loop */. int
68320 6e 4c 65 76 65 6c 3b 20 20 20 20 20 20 20 20 20 nLevel;
68330 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
68340 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 64 20 6c mber of nested l
68350 6f 6f 70 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 oop */. struct
68360 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 WhereClause *pWC
68370 3b 20 20 20 20 20 20 20 2f 2a 20 44 65 63 6f 6d ; /* Decom
68380 70 6f 73 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 position of the
68390 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a WHERE clause */.
683a0 20 20 57 68 65 72 65 4c 65 76 65 6c 20 61 5b 31 WhereLevel a[1
683b0 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ];
683c0 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 /* Information
683d0 61 62 6f 75 74 20 65 61 63 68 20 6e 65 73 74 20 about each nest
683e0 6c 6f 6f 70 20 69 6e 20 57 48 45 52 45 20 2a 2f loop in WHERE */
683f0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e 61 6d .};../*.** A Nam
68400 65 43 6f 6e 74 65 78 74 20 64 65 66 69 6e 65 73 eContext defines
68410 20 61 20 63 6f 6e 74 65 78 74 20 69 6e 20 77 68 a context in wh
68420 69 63 68 20 74 6f 20 72 65 73 6f 6c 76 65 20 74 ich to resolve t
68430 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 0a able and column.
68440 2a 2a 20 6e 61 6d 65 73 2e 20 20 54 68 65 20 63 ** names. The c
68450 6f 6e 74 65 78 74 20 63 6f 6e 73 69 73 74 73 20 ontext consists
68460 6f 66 20 61 20 6c 69 73 74 20 6f 66 20 74 61 62 of a list of tab
68470 6c 65 73 20 28 74 68 65 20 70 53 72 63 4c 69 73 les (the pSrcLis
68480 74 29 20 66 69 65 6c 64 20 61 6e 64 0a 2a 2a 20 t) field and.**
68490 61 20 6c 69 73 74 20 6f 66 20 6e 61 6d 65 64 20 a list of named
684a0 65 78 70 72 65 73 73 69 6f 6e 20 28 70 45 4c 69 expression (pELi
684b0 73 74 29 2e 20 20 54 68 65 20 6e 61 6d 65 64 20 st). The named
684c0 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 expression list
684d0 6d 61 79 0a 2a 2a 20 62 65 20 4e 55 4c 4c 2e 20 may.** be NULL.
684e0 20 54 68 65 20 70 53 72 63 20 63 6f 72 72 65 73 The pSrc corres
684f0 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 46 52 4f ponds to the FRO
68500 4d 20 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45 M clause of a SE
68510 4c 45 43 54 20 6f 72 0a 2a 2a 20 74 6f 20 74 68 LECT or.** to th
68520 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 6f 70 e table being op
68530 65 72 61 74 65 64 20 6f 6e 20 62 79 20 49 4e 53 erated on by INS
68540 45 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 ERT, UPDATE, or
68550 44 45 4c 45 54 45 2e 20 20 54 68 65 0a 2a 2a 20 DELETE. The.**
68560 70 45 4c 69 73 74 20 63 6f 72 72 65 73 70 6f 6e pEList correspon
68570 64 73 20 74 6f 20 74 68 65 20 72 65 73 75 6c 74 ds to the result
68580 20 73 65 74 20 6f 66 20 61 20 53 45 4c 45 43 54 set of a SELECT
68590 20 61 6e 64 20 69 73 20 4e 55 4c 4c 20 66 6f 72 and is NULL for
685a0 0a 2a 2a 20 6f 74 68 65 72 20 73 74 61 74 65 6d .** other statem
685b0 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 4e 61 6d 65 ents..**.** Name
685c0 43 6f 6e 74 65 78 74 73 20 63 61 6e 20 62 65 20 Contexts can be
685d0 6e 65 73 74 65 64 2e 20 20 57 68 65 6e 20 72 65 nested. When re
685e0 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 2c 20 74 solving names, t
685f0 68 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 0a 2a he inner-most .*
68600 2a 20 63 6f 6e 74 65 78 74 20 69 73 20 73 65 61 * context is sea
68610 72 63 68 65 64 20 66 69 72 73 74 2e 20 20 49 66 rched first. If
68620 20 6e 6f 20 6d 61 74 63 68 20 69 73 20 66 6f 75 no match is fou
68630 6e 64 2c 20 74 68 65 20 6e 65 78 74 20 6f 75 74 nd, the next out
68640 65 72 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 69 73 er.** context is
68650 20 63 68 65 63 6b 65 64 2e 20 20 49 66 20 74 68 checked. If th
68660 65 72 65 20 69 73 20 73 74 69 6c 6c 20 6e 6f 20 ere is still no
68670 6d 61 74 63 68 2c 20 74 68 65 20 6e 65 78 74 20 match, the next
68680 63 6f 6e 74 65 78 74 0a 2a 2a 20 69 73 20 63 68 context.** is ch
68690 65 63 6b 65 64 2e 20 20 54 68 69 73 20 70 72 6f ecked. This pro
686a0 63 65 73 73 20 63 6f 6e 74 69 6e 75 65 73 20 75 cess continues u
686b0 6e 74 69 6c 20 65 69 74 68 65 72 20 61 20 6d 61 ntil either a ma
686c0 74 63 68 20 69 73 20 66 6f 75 6e 64 0a 2a 2a 20 tch is found.**
686d0 6f 72 20 61 6c 6c 20 63 6f 6e 74 65 78 74 73 20 or all contexts
686e0 61 72 65 20 63 68 65 63 6b 2e 20 20 57 68 65 6e are check. When
686f0 20 61 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e a match is foun
68700 64 2c 20 74 68 65 20 6e 52 65 66 20 6d 65 6d 62 d, the nRef memb
68710 65 72 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6e er of.** the con
68720 74 65 78 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 text containing
68730 74 68 65 20 6d 61 74 63 68 20 69 73 20 69 6e 63 the match is inc
68740 72 65 6d 65 6e 74 65 64 2e 20 0a 2a 2a 0a 2a 2a remented. .**.**
68750 20 45 61 63 68 20 73 75 62 71 75 65 72 79 20 67 Each subquery g
68760 65 74 73 20 61 20 6e 65 77 20 4e 61 6d 65 43 6f ets a new NameCo
68770 6e 74 65 78 74 2e 20 20 54 68 65 20 70 4e 65 78 ntext. The pNex
68780 74 20 66 69 65 6c 64 20 70 6f 69 6e 74 73 20 74 t field points t
68790 6f 20 74 68 65 0a 2a 2a 20 4e 61 6d 65 43 6f 6e o the.** NameCon
687a0 74 65 78 74 20 69 6e 20 74 68 65 20 70 61 72 65 text in the pare
687b0 6e 74 20 71 75 65 72 79 2e 20 20 54 68 75 73 20 nt query. Thus
687c0 74 68 65 20 70 72 6f 63 65 73 73 20 6f 66 20 73 the process of s
687d0 63 61 6e 6e 69 6e 67 20 74 68 65 0a 2a 2a 20 4e canning the.** N
687e0 61 6d 65 43 6f 6e 74 65 78 74 20 6c 69 73 74 20 ameContext list
687f0 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 73 corresponds to s
68800 65 61 72 63 68 69 6e 67 20 74 68 72 6f 75 67 68 earching through
68810 20 73 75 63 63 65 73 73 69 76 65 6c 79 20 6f 75 successively ou
68820 74 65 72 0a 2a 2a 20 73 75 62 71 75 65 72 69 65 ter.** subquerie
68830 73 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 s looking for a
68840 6d 61 74 63 68 2e 0a 2a 2f 0a 73 74 72 75 63 74 match..*/.struct
68850 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 7b 0a 20 NameContext {.
68860 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 Parse *pParse;
68870 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 /* The par
68880 73 65 72 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 ser */. SrcList
68890 20 2a 70 53 72 63 4c 69 73 74 3b 20 20 20 2f 2a *pSrcList; /*
688a0 20 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 61 62 One or more tab
688b0 6c 65 73 20 75 73 65 64 20 74 6f 20 72 65 73 6f les used to reso
688c0 6c 76 65 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 45 lve names */. E
688d0 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b xprList *pEList;
688e0 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 /* Optional
688f0 6c 69 73 74 20 6f 66 20 6e 61 6d 65 64 20 65 78 list of named ex
68900 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 20 69 pressions */. i
68910 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 nt nRef;
68920 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
68930 20 6e 61 6d 65 73 20 72 65 73 6f 6c 76 65 64 20 names resolved
68940 62 79 20 74 68 69 73 20 63 6f 6e 74 65 78 74 20 by this context
68950 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 3b 20 20 */. int nErr;
68960 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
68970 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20 65 6e ber of errors en
68980 63 6f 75 6e 74 65 72 65 64 20 77 68 69 6c 65 20 countered while
68990 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20 resolving names
689a0 2a 2f 0a 20 20 75 38 20 61 6c 6c 6f 77 41 67 67 */. u8 allowAgg
689b0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 67 67 ; /* Agg
689c0 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 regate functions
689d0 20 61 6c 6c 6f 77 65 64 20 68 65 72 65 20 2a 2f allowed here */
689e0 0a 20 20 75 38 20 68 61 73 41 67 67 3b 20 20 20 . u8 hasAgg;
689f0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
68a00 69 66 20 61 67 67 72 65 67 61 74 65 73 20 61 72 if aggregates ar
68a10 65 20 73 65 65 6e 20 2a 2f 0a 20 20 75 38 20 69 e seen */. u8 i
68a20 73 43 68 65 63 6b 3b 20 20 20 20 20 20 20 20 20 sCheck;
68a30 20 2f 2a 20 54 72 75 65 20 69 66 20 72 65 73 6f /* True if reso
68a40 6c 76 69 6e 67 20 6e 61 6d 65 73 20 69 6e 20 61 lving names in a
68a50 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e CHECK constrain
68a60 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 65 70 74 t */. int nDept
68a70 68 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 h; /* D
68a80 65 70 74 68 20 6f 66 20 73 75 62 71 75 65 72 79 epth of subquery
68a90 20 72 65 63 75 72 73 69 6f 6e 2e 20 31 20 66 6f recursion. 1 fo
68aa0 72 20 6e 6f 20 72 65 63 75 72 73 69 6f 6e 20 2a r no recursion *
68ab0 2f 0a 20 20 41 67 67 49 6e 66 6f 20 2a 70 41 67 /. AggInfo *pAg
68ac0 67 49 6e 66 6f 3b 20 20 20 2f 2a 20 49 6e 66 6f gInfo; /* Info
68ad0 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 67 rmation about ag
68ae0 67 72 65 67 61 74 65 73 20 61 74 20 74 68 69 73 gregates at this
68af0 20 6c 65 76 65 6c 20 2a 2f 0a 20 20 4e 61 6d 65 level */. Name
68b00 43 6f 6e 74 65 78 74 20 2a 70 4e 65 78 74 3b 20 Context *pNext;
68b10 20 2f 2a 20 4e 65 78 74 20 6f 75 74 65 72 20 6e /* Next outer n
68b20 61 6d 65 20 63 6f 6e 74 65 78 74 2e 20 20 4e 55 ame context. NU
68b30 4c 4c 20 66 6f 72 20 6f 75 74 65 72 6d 6f 73 74 LL for outermost
68b40 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e */.};../*.** An
68b50 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
68b60 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
68b70 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 6c ture contains al
68b80 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a l information.**
68b90 20 6e 65 65 64 65 64 20 74 6f 20 67 65 6e 65 72 needed to gener
68ba0 61 74 65 20 63 6f 64 65 20 66 6f 72 20 61 20 73 ate code for a s
68bb0 69 6e 67 6c 65 20 53 45 4c 45 43 54 20 73 74 61 ingle SELECT sta
68bc0 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 6e 4c tement..**.** nL
68bd0 69 6d 69 74 20 69 73 20 73 65 74 20 74 6f 20 2d imit is set to -
68be0 31 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 1 if there is no
68bf0 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e 20 20 LIMIT clause.
68c00 6e 4f 66 66 73 65 74 20 69 73 20 73 65 74 20 74 nOffset is set t
68c10 6f 20 30 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 o 0..** If there
68c20 20 69 73 20 61 20 4c 49 4d 49 54 20 63 6c 61 75 is a LIMIT clau
68c30 73 65 2c 20 74 68 65 20 70 61 72 73 65 72 20 73 se, the parser s
68c40 65 74 73 20 6e 4c 69 6d 69 74 20 74 6f 20 74 68 ets nLimit to th
68c50 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 0a 2a e value of the.*
68c60 2a 20 6c 69 6d 69 74 20 61 6e 64 20 6e 4f 66 66 * limit and nOff
68c70 73 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 set to the value
68c80 20 6f 66 20 74 68 65 20 6f 66 66 73 65 74 20 28 of the offset (
68c90 6f 72 20 30 20 69 66 20 74 68 65 72 65 20 69 73 or 0 if there is
68ca0 20 6e 6f 74 0a 2a 2a 20 6f 66 66 73 65 74 29 2e not.** offset).
68cb0 20 20 42 75 74 20 6c 61 74 65 72 20 6f 6e 2c 20 But later on,
68cc0 6e 4c 69 6d 69 74 20 61 6e 64 20 6e 4f 66 66 73 nLimit and nOffs
68cd0 65 74 20 62 65 63 6f 6d 65 20 74 68 65 20 6d 65 et become the me
68ce0 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 0a 2a mory locations.*
68cf0 2a 20 69 6e 20 74 68 65 20 56 44 42 45 20 74 68 * in the VDBE th
68d00 61 74 20 72 65 63 6f 72 64 20 74 68 65 20 6c 69 at record the li
68d10 6d 69 74 20 61 6e 64 20 6f 66 66 73 65 74 20 63 mit and offset c
68d20 6f 75 6e 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 61 ounters..**.** a
68d30 64 64 72 4f 70 65 6e 45 70 68 6d 5b 5d 20 65 6e ddrOpenEphm[] en
68d40 74 72 69 65 73 20 63 6f 6e 74 61 69 6e 20 74 68 tries contain th
68d50 65 20 61 64 64 72 65 73 73 20 6f 66 20 4f 50 5f e address of OP_
68d60 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 6f 70 OpenEphemeral op
68d70 63 6f 64 65 73 2e 0a 2a 2a 20 54 68 65 73 65 20 codes..** These
68d80 61 64 64 72 65 73 73 65 73 20 6d 75 73 74 20 62 addresses must b
68d90 65 20 73 74 6f 72 65 64 20 73 6f 20 74 68 61 74 e stored so that
68da0 20 77 65 20 63 61 6e 20 67 6f 20 62 61 63 6b 20 we can go back
68db0 61 6e 64 20 66 69 6c 6c 20 69 6e 0a 2a 2a 20 74 and fill in.** t
68dc0 68 65 20 50 34 5f 4b 45 59 49 4e 46 4f 20 61 6e he P4_KEYINFO an
68dd0 64 20 50 32 20 70 61 72 61 6d 65 74 65 72 73 20 d P2 parameters
68de0 6c 61 74 65 72 2e 20 20 4e 65 69 74 68 65 72 20 later. Neither
68df0 74 68 65 20 4b 65 79 49 6e 66 6f 20 6e 6f 72 0a the KeyInfo nor.
68e00 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ** the number of
68e10 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 50 32 20 63 columns in P2 c
68e20 61 6e 20 62 65 20 63 6f 6d 70 75 74 65 64 20 61 an be computed a
68e30 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 0a t the same time.
68e40 2a 2a 20 61 73 20 74 68 65 20 4f 50 5f 4f 70 65 ** as the OP_Ope
68e50 6e 45 70 68 6d 20 69 6e 73 74 72 75 63 74 69 6f nEphm instructio
68e60 6e 20 69 73 20 63 6f 64 65 64 20 62 65 63 61 75 n is coded becau
68e70 73 65 20 6e 6f 74 0a 2a 2a 20 65 6e 6f 75 67 68 se not.** enough
68e80 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f information abo
68e90 75 74 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 ut the compound
68ea0 71 75 65 72 79 20 69 73 20 6b 6e 6f 77 6e 20 61 query is known a
68eb0 74 20 74 68 61 74 20 70 6f 69 6e 74 2e 0a 2a 2a t that point..**
68ec0 20 54 68 65 20 4b 65 79 49 6e 66 6f 20 66 6f 72 The KeyInfo for
68ed0 20 61 64 64 72 4f 70 65 6e 54 72 61 6e 5b 30 5d addrOpenTran[0]
68ee0 20 61 6e 64 20 5b 31 5d 20 63 6f 6e 74 61 69 6e and [1] contain
68ef0 73 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 s collating sequ
68f00 65 6e 63 65 73 0a 2a 2a 20 66 6f 72 20 74 68 65 ences.** for the
68f10 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20 54 68 result set. Th
68f20 65 20 4b 65 79 49 6e 66 6f 20 66 6f 72 20 61 64 e KeyInfo for ad
68f30 64 72 4f 70 65 6e 54 72 61 6e 5b 32 5d 20 63 6f drOpenTran[2] co
68f40 6e 74 61 69 6e 73 20 63 6f 6c 6c 61 74 69 6e 67 ntains collating
68f50 0a 2a 2a 20 73 65 71 75 65 6e 63 65 73 20 66 6f .** sequences fo
68f60 72 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 r the ORDER BY c
68f70 6c 61 75 73 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 lause..*/.struct
68f80 20 53 65 6c 65 63 74 20 7b 0a 20 20 45 78 70 72 Select {. Expr
68f90 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 20 List *pEList;
68fa0 20 20 20 2f 2a 20 54 68 65 20 66 69 65 6c 64 73 /* The fields
68fb0 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 2a of the result *
68fc0 2f 0a 20 20 75 38 20 6f 70 3b 20 20 20 20 20 20 /. u8 op;
68fd0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e /* On
68fe0 65 20 6f 66 3a 20 54 4b 5f 55 4e 49 4f 4e 20 54 e of: TK_UNION T
68ff0 4b 5f 41 4c 4c 20 54 4b 5f 49 4e 54 45 52 53 45 K_ALL TK_INTERSE
69000 43 54 20 54 4b 5f 45 58 43 45 50 54 20 2a 2f 0a CT TK_EXCEPT */.
69010 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 3b char affinity;
69020 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 /* Make
69030 52 65 63 6f 72 64 20 77 69 74 68 20 74 68 69 73 Record with this
69040 20 61 66 66 69 6e 69 74 79 20 66 6f 72 20 53 52 affinity for SR
69050 54 5f 53 65 74 20 2a 2f 0a 20 20 75 31 36 20 73 T_Set */. u16 s
69060 65 6c 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 elFlags;
69070 20 20 2f 2a 20 56 61 72 69 6f 75 73 20 53 46 5f /* Various SF_
69080 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 53 72 * values */. Sr
69090 63 4c 69 73 74 20 2a 70 53 72 63 3b 20 20 20 20 cList *pSrc;
690a0 20 20 20 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d /* The FROM
690b0 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 clause */. Exp
690c0 72 20 2a 70 57 68 65 72 65 3b 20 20 20 20 20 20 r *pWhere;
690d0 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 /* The WHERE
690e0 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 clause */. Exp
690f0 72 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79 3b rList *pGroupBy;
69100 20 20 20 20 2f 2a 20 54 68 65 20 47 52 4f 55 50 /* The GROUP
69110 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 BY clause */.
69120 45 78 70 72 20 2a 70 48 61 76 69 6e 67 3b 20 20 Expr *pHaving;
69130 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 48 41 /* The HA
69140 56 49 4e 47 20 63 6c 61 75 73 65 20 2a 2f 0a 20 VING clause */.
69150 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 ExprList *pOrde
69160 72 42 79 3b 20 20 20 20 2f 2a 20 54 68 65 20 4f rBy; /* The O
69170 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a RDER BY clause *
69180 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 50 72 69 /. Select *pPri
69190 6f 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 72 or; /* Pr
691a0 69 6f 72 20 73 65 6c 65 63 74 20 69 6e 20 61 20 ior select in a
691b0 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 20 compound select
691c0 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 53 statement */. S
691d0 65 6c 65 63 74 20 2a 70 4e 65 78 74 3b 20 20 20 elect *pNext;
691e0 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 73 65 /* Next se
691f0 6c 65 63 74 20 74 6f 20 74 68 65 20 6c 65 66 74 lect to the left
69200 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 2a in a compound *
69210 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 52 69 67 /. Select *pRig
69220 68 74 6d 6f 73 74 3b 20 20 20 20 2f 2a 20 52 69 htmost; /* Ri
69230 67 68 74 2d 6d 6f 73 74 20 73 65 6c 65 63 74 20 ght-most select
69240 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 in a compound se
69250 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20 2a lect statement *
69260 2f 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 /. Expr *pLimit
69270 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 49 ; /* LI
69280 4d 49 54 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 MIT expression.
69290 4e 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f 74 20 75 NULL means not u
692a0 73 65 64 2e 20 2a 2f 0a 20 20 45 78 70 72 20 2a sed. */. Expr *
692b0 70 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 pOffset;
692c0 20 2f 2a 20 4f 46 46 53 45 54 20 65 78 70 72 65 /* OFFSET expre
692d0 73 73 69 6f 6e 2e 20 4e 55 4c 4c 20 6d 65 61 6e ssion. NULL mean
692e0 73 20 6e 6f 74 20 75 73 65 64 2e 20 2a 2f 0a 20 s not used. */.
692f0 20 69 6e 74 20 69 4c 69 6d 69 74 2c 20 69 4f 66 int iLimit, iOf
69300 66 73 65 74 3b 20 20 20 2f 2a 20 4d 65 6d 6f 72 fset; /* Memor
69310 79 20 72 65 67 69 73 74 65 72 73 20 68 6f 6c 64 y registers hold
69320 69 6e 67 20 4c 49 4d 49 54 20 26 20 4f 46 46 53 ing LIMIT & OFFS
69330 45 54 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 ET counters */.
69340 20 69 6e 74 20 61 64 64 72 4f 70 65 6e 45 70 68 int addrOpenEph
69350 6d 5b 33 5d 3b 20 20 20 2f 2a 20 4f 50 5f 4f 70 m[3]; /* OP_Op
69360 65 6e 45 70 68 65 6d 20 6f 70 63 6f 64 65 73 20 enEphem opcodes
69370 72 65 6c 61 74 65 64 20 74 6f 20 74 68 69 73 20 related to this
69380 73 65 6c 65 63 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a select */.};../*
69390 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 .** Allowed valu
693a0 65 73 20 66 6f 72 20 53 65 6c 65 63 74 2e 73 65 es for Select.se
693b0 6c 46 6c 61 67 73 2e 20 20 54 68 65 20 22 53 46 lFlags. The "SF
693c0 22 20 70 72 65 66 69 78 20 73 74 61 6e 64 73 20 " prefix stands
693d0 66 6f 72 0a 2a 2a 20 22 53 65 6c 65 63 74 20 46 for.** "Select F
693e0 6c 61 67 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 lag"..*/.#define
693f0 20 53 46 5f 44 69 73 74 69 6e 63 74 20 20 20 20 SF_Distinct
69400 20 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 4f 0x0001 /* O
69410 75 74 70 75 74 20 73 68 6f 75 6c 64 20 62 65 20 utput should be
69420 44 49 53 54 49 4e 43 54 20 2a 2f 0a 23 64 65 66 DISTINCT */.#def
69430 69 6e 65 20 53 46 5f 52 65 73 6f 6c 76 65 64 20 ine SF_Resolved
69440 20 20 20 20 20 20 20 30 78 30 30 30 32 20 20 2f 0x0002 /
69450 2a 20 49 64 65 6e 74 69 66 69 65 72 73 20 68 61 * Identifiers ha
69460 76 65 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 ve been resolved
69470 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f 41 */.#define SF_A
69480 67 67 72 65 67 61 74 65 20 20 20 20 20 20 20 30 ggregate 0
69490 78 30 30 30 34 20 20 2f 2a 20 43 6f 6e 74 61 69 x0004 /* Contai
694a0 6e 73 20 61 67 67 72 65 67 61 74 65 20 66 75 6e ns aggregate fun
694b0 63 74 69 6f 6e 73 20 2a 2f 0a 23 64 65 66 69 6e ctions */.#defin
694c0 65 20 53 46 5f 55 73 65 73 45 70 68 65 6d 65 72 e SF_UsesEphemer
694d0 61 6c 20 20 20 30 78 30 30 30 38 20 20 2f 2a 20 al 0x0008 /*
694e0 55 73 65 73 20 74 68 65 20 4f 70 65 6e 45 70 68 Uses the OpenEph
694f0 65 6d 65 72 61 6c 20 6f 70 63 6f 64 65 20 2a 2f emeral opcode */
69500 0a 23 64 65 66 69 6e 65 20 53 46 5f 45 78 70 61 .#define SF_Expa
69510 6e 64 65 64 20 20 20 20 20 20 20 20 30 78 30 30 nded 0x00
69520 31 30 20 20 2f 2a 20 73 71 6c 69 74 65 33 53 65 10 /* sqlite3Se
69530 6c 65 63 74 45 78 70 61 6e 64 28 29 20 63 61 6c lectExpand() cal
69540 6c 65 64 20 6f 6e 20 74 68 69 73 20 2a 2f 0a 23 led on this */.#
69550 64 65 66 69 6e 65 20 53 46 5f 48 61 73 54 79 70 define SF_HasTyp
69560 65 49 6e 66 6f 20 20 20 20 20 30 78 30 30 32 30 eInfo 0x0020
69570 20 20 2f 2a 20 46 52 4f 4d 20 73 75 62 71 75 65 /* FROM subque
69580 72 69 65 73 20 68 61 76 65 20 54 61 62 6c 65 20 ries have Table
69590 6d 65 74 61 64 61 74 61 20 2a 2f 0a 0a 0a 2f 2a metadata */.../*
695a0 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 73 20 .** The results
695b0 6f 66 20 61 20 73 65 6c 65 63 74 20 63 61 6e 20 of a select can
695c0 62 65 20 64 69 73 74 72 69 62 75 74 65 64 20 69 be distributed i
695d0 6e 20 73 65 76 65 72 61 6c 20 77 61 79 73 2e 20 n several ways.
695e0 20 54 68 65 0a 2a 2a 20 22 53 52 54 22 20 70 72 The.** "SRT" pr
695f0 65 66 69 78 20 6d 65 61 6e 73 20 22 53 45 4c 45 efix means "SELE
69600 43 54 20 52 65 73 75 6c 74 20 54 79 70 65 22 2e CT Result Type".
69610 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f .*/.#define SRT_
69620 55 6e 69 6f 6e 20 20 20 20 20 20 20 20 31 20 20 Union 1
69630 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20 /* Store result
69640 61 73 20 6b 65 79 73 20 69 6e 20 61 6e 20 69 6e as keys in an in
69650 64 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 dex */.#define S
69660 52 54 5f 45 78 63 65 70 74 20 20 20 20 20 20 20 RT_Except
69670 32 20 20 2f 2a 20 52 65 6d 6f 76 65 20 72 65 73 2 /* Remove res
69680 75 6c 74 20 66 72 6f 6d 20 61 20 55 4e 49 4f 4e ult from a UNION
69690 20 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69 6e index */.#defin
696a0 65 20 53 52 54 5f 45 78 69 73 74 73 20 20 20 20 e SRT_Exists
696b0 20 20 20 33 20 20 2f 2a 20 53 74 6f 72 65 20 31 3 /* Store 1
696c0 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 20 69 if the result i
696d0 73 20 6e 6f 74 20 65 6d 70 74 79 20 2a 2f 0a 23 s not empty */.#
696e0 64 65 66 69 6e 65 20 53 52 54 5f 44 69 73 63 61 define SRT_Disca
696f0 72 64 20 20 20 20 20 20 34 20 20 2f 2a 20 44 6f rd 4 /* Do
69700 20 6e 6f 74 20 73 61 76 65 20 74 68 65 20 72 65 not save the re
69710 73 75 6c 74 73 20 61 6e 79 77 68 65 72 65 20 2a sults anywhere *
69720 2f 0a 0a 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 /../* The ORDER
69730 42 59 20 63 6c 61 75 73 65 20 69 73 20 69 67 6e BY clause is ign
69740 6f 72 65 64 20 66 6f 72 20 61 6c 6c 20 6f 66 20 ored for all of
69750 74 68 65 20 61 62 6f 76 65 20 2a 2f 0a 23 64 65 the above */.#de
69760 66 69 6e 65 20 49 67 6e 6f 72 61 62 6c 65 4f 72 fine IgnorableOr
69770 64 65 72 62 79 28 58 29 20 28 28 58 2d 3e 65 44 derby(X) ((X->eD
69780 65 73 74 29 3c 3d 53 52 54 5f 44 69 73 63 61 72 est)<=SRT_Discar
69790 64 29 0a 0a 23 64 65 66 69 6e 65 20 53 52 54 5f d)..#define SRT_
697a0 4f 75 74 70 75 74 20 20 20 20 20 20 20 35 20 20 Output 5
697b0 2f 2a 20 4f 75 74 70 75 74 20 65 61 63 68 20 72 /* Output each r
697c0 6f 77 20 6f 66 20 72 65 73 75 6c 74 20 2a 2f 0a ow of result */.
697d0 23 64 65 66 69 6e 65 20 53 52 54 5f 4d 65 6d 20 #define SRT_Mem
697e0 20 20 20 20 20 20 20 20 20 36 20 20 2f 2a 20 53 6 /* S
697f0 74 6f 72 65 20 72 65 73 75 6c 74 20 69 6e 20 61 tore result in a
69800 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 2a 2f 0a memory cell */.
69810 23 64 65 66 69 6e 65 20 53 52 54 5f 53 65 74 20 #define SRT_Set
69820 20 20 20 20 20 20 20 20 20 37 20 20 2f 2a 20 53 7 /* S
69830 74 6f 72 65 20 72 65 73 75 6c 74 73 20 61 73 20 tore results as
69840 6b 65 79 73 20 69 6e 20 61 6e 20 69 6e 64 65 78 keys in an index
69850 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f */.#define SRT_
69860 54 61 62 6c 65 20 20 20 20 20 20 20 20 38 20 20 Table 8
69870 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20 /* Store result
69880 61 73 20 64 61 74 61 20 77 69 74 68 20 61 6e 20 as data with an
69890 61 75 74 6f 6d 61 74 69 63 20 72 6f 77 69 64 20 automatic rowid
698a0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 45 */.#define SRT_E
698b0 70 68 65 6d 54 61 62 20 20 20 20 20 39 20 20 2f phemTab 9 /
698c0 2a 20 43 72 65 61 74 65 20 74 72 61 6e 73 69 65 * Create transie
698d0 6e 74 20 74 61 62 20 61 6e 64 20 73 74 6f 72 65 nt tab and store
698e0 20 6c 69 6b 65 20 53 52 54 5f 54 61 62 6c 65 20 like SRT_Table
698f0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 43 */.#define SRT_C
69900 6f 72 6f 75 74 69 6e 65 20 20 20 31 30 20 20 2f oroutine 10 /
69910 2a 20 47 65 6e 65 72 61 74 65 20 61 20 73 69 6e * Generate a sin
69920 67 6c 65 20 72 6f 77 20 6f 66 20 72 65 73 75 6c gle row of resul
69930 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 74 t */../*.** A st
69940 72 75 63 74 75 72 65 20 75 73 65 64 20 74 6f 20 ructure used to
69950 63 75 73 74 6f 6d 69 7a 65 20 74 68 65 20 62 65 customize the be
69960 68 61 76 69 6f 72 20 6f 66 20 73 71 6c 69 74 65 havior of sqlite
69970 33 53 65 6c 65 63 74 28 29 2e 20 53 65 65 0a 2a 3Select(). See.*
69980 2a 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65 * comments above
69990 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 29 sqlite3Select()
699a0 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f for details..*/
699b0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
699c0 53 65 6c 65 63 74 44 65 73 74 20 53 65 6c 65 63 SelectDest Selec
699d0 74 44 65 73 74 3b 0a 73 74 72 75 63 74 20 53 65 tDest;.struct Se
699e0 6c 65 63 74 44 65 73 74 20 7b 0a 20 20 75 38 20 lectDest {. u8
699f0 65 44 65 73 74 3b 20 20 20 20 20 20 20 20 20 2f eDest; /
69a00 2a 20 48 6f 77 20 74 6f 20 64 69 73 70 6f 73 65 * How to dispose
69a10 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 73 20 of the results
69a20 2a 2f 0a 20 20 75 38 20 61 66 66 69 6e 69 74 79 */. u8 affinity
69a30 3b 20 20 20 20 20 20 2f 2a 20 41 66 66 69 6e 69 ; /* Affini
69a40 74 79 20 75 73 65 64 20 77 68 65 6e 20 65 44 65 ty used when eDe
69a50 73 74 3d 3d 53 52 54 5f 53 65 74 20 2a 2f 0a 20 st==SRT_Set */.
69a60 20 69 6e 74 20 69 50 61 72 6d 3b 20 20 20 20 20 int iParm;
69a70 20 20 20 2f 2a 20 41 20 70 61 72 61 6d 65 74 65 /* A paramete
69a80 72 20 75 73 65 64 20 62 79 20 74 68 65 20 65 44 r used by the eD
69a90 65 73 74 20 64 69 73 70 6f 73 61 6c 20 6d 65 74 est disposal met
69aa0 68 6f 64 20 2a 2f 0a 20 20 69 6e 74 20 69 4d 65 hod */. int iMe
69ab0 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61 m; /* Ba
69ac0 73 65 20 72 65 67 69 73 74 65 72 20 77 68 65 72 se register wher
69ad0 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 77 72 e results are wr
69ae0 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e itten */. int n
69af0 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 Mem; /*
69b00 4e 75 6d 62 65 72 20 6f 66 20 72 65 67 69 73 74 Number of regist
69b10 65 72 73 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f ers allocated */
69b20 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 44 75 72 69 6e .};../*.** Durin
69b30 67 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6f g code generatio
69b40 6e 20 6f 66 20 73 74 61 74 65 6d 65 6e 74 73 20 n of statements
69b50 74 68 61 74 20 64 6f 20 69 6e 73 65 72 74 73 20 that do inserts
69b60 69 6e 74 6f 20 41 55 54 4f 49 4e 43 52 45 4d 45 into AUTOINCREME
69b70 4e 54 20 0a 2a 2a 20 74 61 62 6c 65 73 2c 20 74 NT .** tables, t
69b80 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 66 he following inf
69b90 6f 72 6d 61 74 69 6f 6e 20 69 73 20 61 74 74 61 ormation is atta
69ba0 63 68 65 64 20 74 6f 20 74 68 65 20 54 61 62 6c ched to the Tabl
69bb0 65 2e 75 2e 61 75 74 6f 49 6e 63 2e 70 0a 2a 2a e.u.autoInc.p.**
69bc0 20 70 6f 69 6e 74 65 72 20 6f 66 20 65 61 63 68 pointer of each
69bd0 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 74 autoincrement t
69be0 61 62 6c 65 20 74 6f 20 72 65 63 6f 72 64 20 73 able to record s
69bf0 6f 6d 65 20 73 69 64 65 20 69 6e 66 6f 72 6d 61 ome side informa
69c00 74 69 6f 6e 20 74 68 61 74 0a 2a 2a 20 74 68 65 tion that.** the
69c10 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 20 code generator
69c20 6e 65 65 64 73 2e 20 20 57 65 20 68 61 76 65 20 needs. We have
69c30 74 6f 20 6b 65 65 70 20 70 65 72 2d 74 61 62 6c to keep per-tabl
69c40 65 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 0a e autoincrement.
69c50 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 ** information i
69c60 6e 20 63 61 73 65 20 69 6e 73 65 72 74 73 20 61 n case inserts a
69c70 72 65 20 64 6f 77 6e 20 77 69 74 68 69 6e 20 74 re down within t
69c80 72 69 67 67 65 72 73 2e 20 20 54 72 69 67 67 65 riggers. Trigge
69c90 72 73 20 64 6f 20 6e 6f 74 0a 2a 2a 20 6e 6f 72 rs do not.** nor
69ca0 6d 61 6c 6c 79 20 63 6f 6f 72 64 69 6e 61 74 65 mally coordinate
69cb0 20 74 68 65 69 72 20 61 63 74 69 76 69 74 69 65 their activitie
69cc0 73 2c 20 62 75 74 20 77 65 20 64 6f 20 6e 65 65 s, but we do nee
69cd0 64 20 74 6f 20 63 6f 6f 72 64 69 6e 61 74 65 20 d to coordinate
69ce0 74 68 65 0a 2a 2a 20 6c 6f 61 64 69 6e 67 20 61 the.** loading a
69cf0 6e 64 20 73 61 76 69 6e 67 20 6f 66 20 61 75 74 nd saving of aut
69d00 6f 69 6e 63 72 65 6d 65 6e 74 20 69 6e 66 6f 72 oincrement infor
69d10 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 mation..*/.struc
69d20 74 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 7b 0a t AutoincInfo {.
69d30 20 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 2a 70 AutoincInfo *p
69d40 4e 65 78 74 3b 20 20 20 2f 2a 20 4e 65 78 74 20 Next; /* Next
69d50 69 6e 66 6f 20 62 6c 6f 63 6b 20 69 6e 20 61 20 info block in a
69d60 6c 69 73 74 20 6f 66 20 74 68 65 6d 20 61 6c 6c list of them all
69d70 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 */. Table *pTa
69d80 62 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 b; /* T
69d90 61 62 6c 65 20 74 68 69 73 20 69 6e 66 6f 20 62 able this info b
69da0 6c 6f 63 6b 20 72 65 66 65 72 73 20 74 6f 20 2a lock refers to *
69db0 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 /. int iDb;
69dc0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 /* Ind
69dd0 65 78 20 69 6e 20 73 71 6c 69 74 65 33 2e 61 44 ex in sqlite3.aD
69de0 62 5b 5d 20 6f 66 20 64 61 74 61 62 61 73 65 20 b[] of database
69df0 68 6f 6c 64 69 6e 67 20 70 54 61 62 20 2a 2f 0a holding pTab */.
69e00 20 20 69 6e 74 20 72 65 67 43 74 72 3b 20 20 20 int regCtr;
69e10 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 /* Memor
69e20 79 20 72 65 67 69 73 74 65 72 20 68 6f 6c 64 69 y register holdi
69e30 6e 67 20 74 68 65 20 72 6f 77 69 64 20 63 6f 75 ng the rowid cou
69e40 6e 74 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a nter */.};../*.*
69e50 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 63 6f * Size of the co
69e60 6c 75 6d 6e 20 63 61 63 68 65 0a 2a 2f 0a 23 69 lumn cache.*/.#i
69e70 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4e 5f 43 fndef SQLITE_N_C
69e80 4f 4c 43 41 43 48 45 0a 23 20 64 65 66 69 6e 65 OLCACHE.# define
69e90 20 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 SQLITE_N_COLCAC
69ea0 48 45 20 31 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a HE 10.#endif../*
69eb0 0a 2a 2a 20 41 74 20 6c 65 61 73 74 20 6f 6e 65 .** At least one
69ec0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
69ed0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
69ee0 74 75 72 65 20 69 73 20 63 72 65 61 74 65 64 20 ture is created
69ef0 66 6f 72 20 65 61 63 68 20 0a 2a 2a 20 74 72 69 for each .** tri
69f00 67 67 65 72 20 74 68 61 74 20 6d 61 79 20 62 65 gger that may be
69f10 20 66 69 72 65 64 20 77 68 69 6c 65 20 70 61 72 fired while par
69f20 73 69 6e 67 20 61 6e 20 49 4e 53 45 52 54 2c 20 sing an INSERT,
69f30 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45 UPDATE or DELETE
69f40 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20 41 .** statement. A
69f50 6c 6c 20 73 75 63 68 20 6f 62 6a 65 63 74 73 20 ll such objects
69f60 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 are stored in th
69f70 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 68 65 e linked list he
69f80 61 64 65 64 20 61 74 0a 2a 2a 20 50 61 72 73 65 aded at.** Parse
69f90 2e 70 54 72 69 67 67 65 72 50 72 67 20 61 6e 64 .pTriggerPrg and
69fa0 20 64 65 6c 65 74 65 64 20 6f 6e 63 65 20 73 74 deleted once st
69fb0 61 74 65 6d 65 6e 74 20 63 6f 6d 70 69 6c 61 74 atement compilat
69fc0 69 6f 6e 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 ion has been.**
69fd0 63 6f 6d 70 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a completed..**.**
69fe0 20 41 20 56 64 62 65 20 73 75 62 2d 70 72 6f 67 A Vdbe sub-prog
69ff0 72 61 6d 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 ram that impleme
6a000 6e 74 73 20 74 68 65 20 62 6f 64 79 20 61 6e 64 nts the body and
6a010 20 57 48 45 4e 20 63 6c 61 75 73 65 20 6f 66 20 WHEN clause of
6a020 74 72 69 67 67 65 72 0a 2a 2a 20 54 72 69 67 67 trigger.** Trigg
6a030 65 72 50 72 67 2e 70 54 72 69 67 67 65 72 2c 20 erPrg.pTrigger,
6a040 61 73 73 75 6d 69 6e 67 20 61 20 64 65 66 61 75 assuming a defau
6a050 6c 74 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63 lt ON CONFLICT c
6a060 6c 61 75 73 65 20 6f 66 0a 2a 2a 20 54 72 69 67 lause of.** Trig
6a070 67 65 72 50 72 67 2e 6f 72 63 6f 6e 66 2c 20 69 gerPrg.orconf, i
6a080 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 s stored in the
6a090 54 72 69 67 67 65 72 50 72 67 2e 70 50 72 6f 67 TriggerPrg.pProg
6a0a0 72 61 6d 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2a ram variable..**
6a0b0 20 54 68 65 20 50 61 72 73 65 2e 70 54 72 69 67 The Parse.pTrig
6a0c0 67 65 72 50 72 67 20 6c 69 73 74 20 6e 65 76 65 gerPrg list neve
6a0d0 72 20 63 6f 6e 74 61 69 6e 73 20 74 77 6f 20 65 r contains two e
6a0e0 6e 74 72 69 65 73 20 77 69 74 68 20 74 68 65 20 ntries with the
6a0f0 73 61 6d 65 0a 2a 2a 20 76 61 6c 75 65 73 20 66 same.** values f
6a100 6f 72 20 62 6f 74 68 20 70 54 72 69 67 67 65 72 or both pTrigger
6a110 20 61 6e 64 20 6f 72 63 6f 6e 66 2e 0a 2a 2a 0a and orconf..**.
6a120 2a 2a 20 54 68 65 20 54 72 69 67 67 65 72 50 72 ** The TriggerPr
6a130 67 2e 6f 6c 64 6d 61 73 6b 20 76 61 72 69 61 62 g.oldmask variab
6a140 6c 65 20 69 73 20 73 65 74 20 74 6f 20 61 20 6d le is set to a m
6a150 61 73 6b 20 6f 66 20 6f 6c 64 2e 2a 20 63 6f 6c ask of old.* col
6a160 75 6d 6e 73 0a 2a 2a 20 61 63 63 65 73 73 65 64 umns.** accessed
6a170 20 28 6f 72 20 73 65 74 20 74 6f 20 30 20 66 6f (or set to 0 fo
6a180 72 20 74 72 69 67 67 65 72 73 20 66 69 72 65 64 r triggers fired
6a190 20 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 as a result of
6a1a0 49 4e 53 45 52 54 20 0a 2a 2a 20 73 74 61 74 65 INSERT .** state
6a1b0 6d 65 6e 74 73 29 2e 0a 2a 2f 0a 73 74 72 75 63 ments)..*/.struc
6a1c0 74 20 54 72 69 67 67 65 72 50 72 67 20 7b 0a 20 t TriggerPrg {.
6a1d0 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 Trigger *pTrigg
6a1e0 65 72 3b 20 20 20 20 20 20 2f 2a 20 54 72 69 67 er; /* Trig
6a1f0 67 65 72 20 74 68 69 73 20 70 72 6f 67 72 61 6d ger this program
6a200 20 77 61 73 20 63 6f 64 65 64 20 66 72 6f 6d 20 was coded from
6a210 2a 2f 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66 3b */. int orconf;
6a220 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
6a230 44 65 66 61 75 6c 74 20 4f 4e 20 43 4f 4e 46 4c Default ON CONFL
6a240 49 43 54 20 70 6f 6c 69 63 79 20 2a 2f 0a 20 20 ICT policy */.
6a250 53 75 62 50 72 6f 67 72 61 6d 20 2a 70 50 72 6f SubProgram *pPro
6a260 67 72 61 6d 3b 20 20 20 2f 2a 20 50 72 6f 67 72 gram; /* Progr
6a270 61 6d 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 am implementing
6a280 70 54 72 69 67 67 65 72 2f 6f 72 63 6f 6e 66 20 pTrigger/orconf
6a290 2a 2f 0a 20 20 75 33 32 20 6f 6c 64 6d 61 73 6b */. u32 oldmask
6a2a0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
6a2b0 4d 61 73 6b 20 6f 66 20 6f 6c 64 2e 2a 20 63 6f Mask of old.* co
6a2c0 6c 75 6d 6e 73 20 61 63 63 65 73 73 65 64 20 2a lumns accessed *
6a2d0 2f 0a 20 20 54 72 69 67 67 65 72 50 72 67 20 2a /. TriggerPrg *
6a2e0 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e pNext; /* N
6a2f0 65 78 74 20 65 6e 74 72 79 20 69 6e 20 50 61 72 ext entry in Par
6a300 73 65 2e 70 54 72 69 67 67 65 72 50 72 67 20 6c se.pTriggerPrg l
6a310 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ist */.};../*.**
6a320 20 41 6e 20 53 51 4c 20 70 61 72 73 65 72 20 63 An SQL parser c
6a330 6f 6e 74 65 78 74 2e 20 20 41 20 63 6f 70 79 20 ontext. A copy
6a340 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 of this structur
6a350 65 20 69 73 20 70 61 73 73 65 64 20 74 68 72 6f e is passed thro
6a360 75 67 68 0a 2a 2a 20 74 68 65 20 70 61 72 73 65 ugh.** the parse
6a370 72 20 61 6e 64 20 64 6f 77 6e 20 69 6e 74 6f 20 r and down into
6a380 61 6c 6c 20 74 68 65 20 70 61 72 73 65 72 20 61 all the parser a
6a390 63 74 69 6f 6e 20 72 6f 75 74 69 6e 65 20 69 6e ction routine in
6a3a0 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63 61 72 order to.** car
6a3b0 72 79 20 61 72 6f 75 6e 64 20 69 6e 66 6f 72 6d ry around inform
6a3c0 61 74 69 6f 6e 20 74 68 61 74 20 69 73 20 67 6c ation that is gl
6a3d0 6f 62 61 6c 20 74 6f 20 74 68 65 20 65 6e 74 69 obal to the enti
6a3e0 72 65 20 70 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 re parse..**.**
6a3f0 54 68 65 20 73 74 72 75 63 74 75 72 65 20 69 73 The structure is
6a400 20 64 69 76 69 64 65 64 20 69 6e 74 6f 20 74 77 divided into tw
6a410 6f 20 70 61 72 74 73 2e 20 20 57 68 65 6e 20 74 o parts. When t
6a420 68 65 20 70 61 72 73 65 72 20 61 6e 64 20 63 6f he parser and co
6a430 64 65 0a 2a 2a 20 67 65 6e 65 72 61 74 65 20 63 de.** generate c
6a440 61 6c 6c 20 74 68 65 6d 73 65 6c 76 65 73 20 72 all themselves r
6a450 65 63 75 72 73 69 76 65 6c 79 2c 20 74 68 65 20 ecursively, the
6a460 66 69 72 73 74 20 70 61 72 74 20 6f 66 20 74 68 first part of th
6a470 65 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 e structure.** i
6a480 73 20 63 6f 6e 73 74 61 6e 74 20 62 75 74 20 74 s constant but t
6a490 68 65 20 73 65 63 6f 6e 64 20 70 61 72 74 20 69 he second part i
6a4a0 73 20 72 65 73 65 74 20 61 74 20 74 68 65 20 62 s reset at the b
6a4b0 65 67 69 6e 6e 69 6e 67 20 61 6e 64 20 65 6e 64 eginning and end
6a4c0 20 6f 66 0a 2a 2a 20 65 61 63 68 20 72 65 63 75 of.** each recu
6a4d0 72 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 rsion..**.** The
6a4e0 20 6e 54 61 62 6c 65 4c 6f 63 6b 20 61 6e 64 20 nTableLock and
6a4f0 61 54 61 62 6c 65 4c 6f 63 6b 20 76 61 72 69 61 aTableLock varia
6a500 62 6c 65 73 20 61 72 65 20 6f 6e 6c 79 20 75 73 bles are only us
6a510 65 64 20 69 66 20 74 68 65 20 73 68 61 72 65 64 ed if the shared
6a520 2d 63 61 63 68 65 20 0a 2a 2a 20 66 65 61 74 75 -cache .** featu
6a530 72 65 20 69 73 20 65 6e 61 62 6c 65 64 20 28 69 re is enabled (i
6a540 66 20 73 71 6c 69 74 65 33 54 73 64 28 29 2d 3e f sqlite3Tsd()->
6a550 75 73 65 53 68 61 72 65 64 44 61 74 61 20 69 73 useSharedData is
6a560 20 74 72 75 65 29 2e 20 54 68 65 79 20 61 72 65 true). They are
6a570 0a 2a 2a 20 75 73 65 64 20 74 6f 20 73 74 6f 72 .** used to stor
6a580 65 20 74 68 65 20 73 65 74 20 6f 66 20 74 61 62 e the set of tab
6a590 6c 65 2d 6c 6f 63 6b 73 20 72 65 71 75 69 72 65 le-locks require
6a5a0 64 20 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 d by the stateme
6a5b0 6e 74 20 62 65 69 6e 67 0a 2a 2a 20 63 6f 6d 70 nt being.** comp
6a5c0 69 6c 65 64 2e 20 46 75 6e 63 74 69 6f 6e 20 73 iled. Function s
6a5d0 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 qlite3TableLock(
6a5e0 29 20 69 73 20 75 73 65 64 20 74 6f 20 61 64 64 ) is used to add
6a5f0 20 65 6e 74 72 69 65 73 20 74 6f 20 74 68 65 0a entries to the.
6a600 2a 2a 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 72 75 ** list..*/.stru
6a610 63 74 20 50 61 72 73 65 20 7b 0a 20 20 73 71 6c ct Parse {. sql
6a620 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 ite3 *db;
6a630 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64 61 /* The main da
6a640 74 61 62 61 73 65 20 73 74 72 75 63 74 75 72 65 tabase structure
6a650 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 */. int rc;
6a660 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
6a670 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 65 turn code from e
6a680 78 65 63 75 74 69 6f 6e 20 2a 2f 0a 20 20 63 68 xecution */. ch
6a690 61 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 ar *zErrMsg;
6a6a0 20 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20 6d /* An error m
6a6b0 65 73 73 61 67 65 20 2a 2f 0a 20 20 56 64 62 65 essage */. Vdbe
6a6c0 20 2a 70 56 64 62 65 3b 20 20 20 20 20 20 20 20 *pVdbe;
6a6d0 20 2f 2a 20 41 6e 20 65 6e 67 69 6e 65 20 66 6f /* An engine fo
6a6e0 72 20 65 78 65 63 75 74 69 6e 67 20 64 61 74 61 r executing data
6a6f0 62 61 73 65 20 62 79 74 65 63 6f 64 65 20 2a 2f base bytecode */
6a700 0a 20 20 75 38 20 63 6f 6c 4e 61 6d 65 73 53 65 . u8 colNamesSe
6a710 74 3b 20 20 20 20 20 20 2f 2a 20 54 52 55 45 20 t; /* TRUE
6a720 61 66 74 65 72 20 4f 50 5f 43 6f 6c 75 6d 6e 4e after OP_ColumnN
6a730 61 6d 65 20 68 61 73 20 62 65 65 6e 20 69 73 73 ame has been iss
6a740 75 65 64 20 74 6f 20 70 56 64 62 65 20 2a 2f 0a ued to pVdbe */.
6a750 20 20 75 38 20 6e 61 6d 65 43 6c 61 73 68 3b 20 u8 nameClash;
6a760 20 20 20 20 20 20 20 2f 2a 20 41 20 70 65 72 6d /* A perm
6a770 61 6e 65 6e 74 20 74 61 62 6c 65 20 6e 61 6d 65 anent table name
6a780 20 63 6c 61 73 68 65 73 20 77 69 74 68 20 74 65 clashes with te
6a790 6d 70 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f mp table name */
6a7a0 0a 20 20 75 38 20 63 68 65 63 6b 53 63 68 65 6d . u8 checkSchem
6a7b0 61 3b 20 20 20 20 20 20 2f 2a 20 43 61 75 73 65 a; /* Cause
6a7c0 73 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 s schema cookie
6a7d0 63 68 65 63 6b 20 61 66 74 65 72 20 61 6e 20 65 check after an e
6a7e0 72 72 6f 72 20 2a 2f 0a 20 20 75 38 20 6e 65 73 rror */. u8 nes
6a7f0 74 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f ted; /
6a800 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 * Number of nest
6a810 65 64 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 20 ed calls to the
6a820 70 61 72 73 65 72 2f 63 6f 64 65 20 67 65 6e 65 parser/code gene
6a830 72 61 74 6f 72 20 2a 2f 0a 20 20 75 38 20 70 61 rator */. u8 pa
6a840 72 73 65 45 72 72 6f 72 3b 20 20 20 20 20 20 20 rseError;
6a850 2f 2a 20 54 72 75 65 20 61 66 74 65 72 20 61 20 /* True after a
6a860 70 61 72 73 69 6e 67 20 65 72 72 6f 72 2e 20 20 parsing error.
6a870 54 69 63 6b 65 74 20 23 31 37 39 34 20 2a 2f 0a Ticket #1794 */.
6a880 20 20 75 38 20 6e 54 65 6d 70 52 65 67 3b 20 20 u8 nTempReg;
6a890 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
6a8a0 20 6f 66 20 74 65 6d 70 6f 72 61 72 79 20 72 65 of temporary re
6a8b0 67 69 73 74 65 72 73 20 69 6e 20 61 54 65 6d 70 gisters in aTemp
6a8c0 52 65 67 5b 5d 20 2a 2f 0a 20 20 75 38 20 6e 54 Reg[] */. u8 nT
6a8d0 65 6d 70 49 6e 55 73 65 3b 20 20 20 20 20 20 20 empInUse;
6a8e0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 54 65 /* Number of aTe
6a8f0 6d 70 52 65 67 5b 5d 20 63 75 72 72 65 6e 74 6c mpReg[] currentl
6a900 79 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f y checked out */
6a910 0a 20 20 69 6e 74 20 61 54 65 6d 70 52 65 67 5b . int aTempReg[
6a920 38 5d 3b 20 20 20 20 20 2f 2a 20 48 6f 6c 64 69 8]; /* Holdi
6a930 6e 67 20 61 72 65 61 20 66 6f 72 20 74 65 6d 70 ng area for temp
6a940 6f 72 61 72 79 20 72 65 67 69 73 74 65 72 73 20 orary registers
6a950 2a 2f 0a 20 20 69 6e 74 20 6e 52 61 6e 67 65 52 */. int nRangeR
6a960 65 67 3b 20 20 20 20 20 20 20 2f 2a 20 53 69 7a eg; /* Siz
6a970 65 20 6f 66 20 74 68 65 20 74 65 6d 70 6f 72 61 e of the tempora
6a980 72 79 20 72 65 67 69 73 74 65 72 20 62 6c 6f 63 ry register bloc
6a990 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 52 61 6e 67 k */. int iRang
6a9a0 65 52 65 67 3b 20 20 20 20 20 20 20 2f 2a 20 46 eReg; /* F
6a9b0 69 72 73 74 20 72 65 67 69 73 74 65 72 20 69 6e irst register in
6a9c0 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 temporary regis
6a9d0 74 65 72 20 62 6c 6f 63 6b 20 2a 2f 0a 20 20 69 ter block */. i
6a9e0 6e 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 20 nt nErr;
6a9f0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
6aa00 20 65 72 72 6f 72 73 20 73 65 65 6e 20 2a 2f 0a errors seen */.
6aa10 20 20 69 6e 74 20 6e 54 61 62 3b 20 20 20 20 20 int nTab;
6aa20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
6aa30 20 6f 66 20 70 72 65 76 69 6f 75 73 6c 79 20 61 of previously a
6aa40 6c 6c 6f 63 61 74 65 64 20 56 44 42 45 20 63 75 llocated VDBE cu
6aa50 72 73 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e rsors */. int n
6aa60 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 Mem;
6aa70 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d /* Number of mem
6aa80 6f 72 79 20 63 65 6c 6c 73 20 75 73 65 64 20 73 ory cells used s
6aa90 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 6e o far */. int n
6aaa0 53 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 Set;
6aab0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 65 74 /* Number of set
6aac0 73 20 75 73 65 64 20 73 6f 20 66 61 72 20 2a 2f s used so far */
6aad0 0a 20 20 69 6e 74 20 63 6b 42 61 73 65 3b 20 20 . int ckBase;
6aae0 20 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20 /* Base
6aaf0 72 65 67 69 73 74 65 72 20 6f 66 20 64 61 74 61 register of data
6ab00 20 64 75 72 69 6e 67 20 63 68 65 63 6b 20 63 6f during check co
6ab10 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 69 nstraints */. i
6ab20 6e 74 20 69 43 61 63 68 65 4c 65 76 65 6c 3b 20 nt iCacheLevel;
6ab30 20 20 20 20 2f 2a 20 43 6f 6c 43 61 63 68 65 20 /* ColCache
6ab40 76 61 6c 69 64 20 77 68 65 6e 20 61 43 6f 6c 43 valid when aColC
6ab50 61 63 68 65 5b 5d 2e 69 4c 65 76 65 6c 3c 3d 69 ache[].iLevel<=i
6ab60 43 61 63 68 65 4c 65 76 65 6c 20 2a 2f 0a 20 20 CacheLevel */.
6ab70 69 6e 74 20 69 43 61 63 68 65 43 6e 74 3b 20 20 int iCacheCnt;
6ab80 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 20 /* Counter
6ab90 75 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65 used to generate
6aba0 20 61 43 6f 6c 43 61 63 68 65 5b 5d 2e 6c 72 75 aColCache[].lru
6abb0 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 38 20 values */. u8
6abc0 6e 43 6f 6c 43 61 63 68 65 3b 20 20 20 20 20 20 nColCache;
6abd0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 /* Number of e
6abe0 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 63 6f ntries in the co
6abf0 6c 75 6d 6e 20 63 61 63 68 65 20 2a 2f 0a 20 20 lumn cache */.
6ac00 75 38 20 69 43 6f 6c 43 61 63 68 65 3b 20 20 20 u8 iColCache;
6ac10 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 65 6e 74 /* Next ent
6ac20 72 79 20 6f 66 20 74 68 65 20 63 61 63 68 65 20 ry of the cache
6ac30 74 6f 20 72 65 70 6c 61 63 65 20 2a 2f 0a 20 20 to replace */.
6ac40 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 struct yColCache
6ac50 20 7b 0a 20 20 20 20 69 6e 74 20 69 54 61 62 6c {. int iTabl
6ac60 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 e; /*
6ac70 54 61 62 6c 65 20 63 75 72 73 6f 72 20 6e 75 6d Table cursor num
6ac80 62 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 ber */. int i
6ac90 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 Column;
6aca0 20 2f 2a 20 54 61 62 6c 65 20 63 6f 6c 75 6d 6e /* Table column
6acb0 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 75 number */. u
6acc0 38 20 61 66 66 43 68 61 6e 67 65 3b 20 20 20 20 8 affChange;
6acd0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
6ace0 74 68 69 73 20 72 65 67 69 73 74 65 72 20 68 61 this register ha
6acf0 73 20 68 61 64 20 61 6e 20 61 66 66 69 6e 69 74 s had an affinit
6ad00 79 20 63 68 61 6e 67 65 20 2a 2f 0a 20 20 20 20 y change */.
6ad10 75 38 20 74 65 6d 70 52 65 67 3b 20 20 20 20 20 u8 tempReg;
6ad20 20 20 20 20 20 20 2f 2a 20 69 52 65 67 20 69 73 /* iReg is
6ad30 20 61 20 74 65 6d 70 20 72 65 67 69 73 74 65 72 a temp register
6ad40 20 74 68 61 74 20 6e 65 65 64 73 20 74 6f 20 62 that needs to b
6ad50 65 20 66 72 65 65 64 20 2a 2f 0a 20 20 20 20 69 e freed */. i
6ad60 6e 74 20 69 4c 65 76 65 6c 3b 20 20 20 20 20 20 nt iLevel;
6ad70 20 20 20 20 20 2f 2a 20 4e 65 73 74 69 6e 67 20 /* Nesting
6ad80 6c 65 76 65 6c 20 2a 2f 0a 20 20 20 20 69 6e 74 level */. int
6ad90 20 69 52 65 67 3b 20 20 20 20 20 20 20 20 20 20 iReg;
6ada0 20 20 20 2f 2a 20 52 65 67 20 77 69 74 68 20 76 /* Reg with v
6adb0 61 6c 75 65 20 6f 66 20 74 68 69 73 20 63 6f 6c alue of this col
6adc0 75 6d 6e 2e 20 30 20 6d 65 61 6e 73 20 6e 6f 6e umn. 0 means non
6add0 65 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 72 e. */. int lr
6ade0 75 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 u;
6adf0 2f 2a 20 4c 65 61 73 74 20 72 65 63 65 6e 74 6c /* Least recentl
6ae00 79 20 75 73 65 64 20 65 6e 74 72 79 20 68 61 73 y used entry has
6ae10 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 76 61 the smallest va
6ae20 6c 75 65 20 2a 2f 0a 20 20 7d 20 61 43 6f 6c 43 lue */. } aColC
6ae30 61 63 68 65 5b 53 51 4c 49 54 45 5f 4e 5f 43 4f ache[SQLITE_N_CO
6ae40 4c 43 41 43 48 45 5d 3b 20 20 2f 2a 20 4f 6e 65 LCACHE]; /* One
6ae50 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e for each column
6ae60 20 63 61 63 68 65 20 65 6e 74 72 79 20 2a 2f 0a cache entry */.
6ae70 20 20 75 33 32 20 77 72 69 74 65 4d 61 73 6b 3b u32 writeMask;
6ae80 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74 20 /* Start
6ae90 61 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 a write transact
6aea0 69 6f 6e 20 6f 6e 20 74 68 65 73 65 20 64 61 74 ion on these dat
6aeb0 61 62 61 73 65 73 20 2a 2f 0a 20 20 75 33 32 20 abases */. u32
6aec0 63 6f 6f 6b 69 65 4d 61 73 6b 3b 20 20 20 20 20 cookieMask;
6aed0 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 73 /* Bitmask of s
6aee0 63 68 65 6d 61 20 76 65 72 69 66 69 65 64 20 64 chema verified d
6aef0 61 74 61 62 61 73 65 73 20 2a 2f 0a 20 20 75 38 atabases */. u8
6af00 20 69 73 4d 75 6c 74 69 57 72 69 74 65 3b 20 20 isMultiWrite;
6af10 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 73 74 /* True if st
6af20 61 74 65 6d 65 6e 74 20 6d 61 79 20 61 66 66 65 atement may affe
6af30 63 74 2f 69 6e 73 65 72 74 20 6d 75 6c 74 69 70 ct/insert multip
6af40 6c 65 20 72 6f 77 73 20 2a 2f 0a 20 20 75 38 20 le rows */. u8
6af50 6d 61 79 41 62 6f 72 74 3b 20 20 20 20 20 20 20 mayAbort;
6af60 20 20 2f 2a 20 54 72 75 65 20 69 66 20 73 74 61 /* True if sta
6af70 74 65 6d 65 6e 74 20 6d 61 79 20 74 68 72 6f 77 tement may throw
6af80 20 61 6e 20 41 42 4f 52 54 20 65 78 63 65 70 74 an ABORT except
6af90 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f ion */. int coo
6afa0 6b 69 65 47 6f 74 6f 3b 20 20 20 20 20 20 2f 2a kieGoto; /*
6afb0 20 41 64 64 72 65 73 73 20 6f 66 20 4f 50 5f 47 Address of OP_G
6afc0 6f 74 6f 20 74 6f 20 63 6f 6f 6b 69 65 20 76 65 oto to cookie ve
6afd0 72 69 66 69 65 72 20 73 75 62 72 6f 75 74 69 6e rifier subroutin
6afe0 65 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f 6b 69 e */. int cooki
6aff0 65 56 61 6c 75 65 5b 53 51 4c 49 54 45 5f 4d 41 eValue[SQLITE_MA
6b000 58 5f 41 54 54 41 43 48 45 44 2b 32 5d 3b 20 20 X_ATTACHED+2];
6b010 2f 2a 20 56 61 6c 75 65 73 20 6f 66 20 63 6f 6f /* Values of coo
6b020 6b 69 65 73 20 74 6f 20 76 65 72 69 66 79 20 2a kies to verify *
6b030 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
6b040 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
6b050 48 45 0a 20 20 69 6e 74 20 6e 54 61 62 6c 65 4c HE. int nTableL
6b060 6f 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e ock; /* N
6b070 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 69 umber of locks i
6b080 6e 20 61 54 61 62 6c 65 4c 6f 63 6b 20 2a 2f 0a n aTableLock */.
6b090 20 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 61 54 61 TableLock *aTa
6b0a0 62 6c 65 4c 6f 63 6b 3b 20 2f 2a 20 52 65 71 75 bleLock; /* Requ
6b0b0 69 72 65 64 20 74 61 62 6c 65 20 6c 6f 63 6b 73 ired table locks
6b0c0 20 66 6f 72 20 73 68 61 72 65 64 2d 63 61 63 68 for shared-cach
6b0d0 65 20 6d 6f 64 65 20 2a 2f 0a 23 65 6e 64 69 66 e mode */.#endif
6b0e0 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64 3b . int regRowid;
6b0f0 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 /* Regis
6b100 74 65 72 20 68 6f 6c 64 69 6e 67 20 72 6f 77 69 ter holding rowi
6b110 64 20 6f 66 20 43 52 45 41 54 45 20 54 41 42 4c d of CREATE TABL
6b120 45 20 65 6e 74 72 79 20 2a 2f 0a 20 20 69 6e 74 E entry */. int
6b130 20 72 65 67 52 6f 6f 74 3b 20 20 20 20 20 20 20 regRoot;
6b140 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f /* Register ho
6b150 6c 64 69 6e 67 20 72 6f 6f 74 20 70 61 67 65 20 lding root page
6b160 6e 75 6d 62 65 72 20 66 6f 72 20 6e 65 77 20 6f number for new o
6b170 62 6a 65 63 74 73 20 2a 2f 0a 20 20 41 75 74 6f bjects */. Auto
6b180 69 6e 63 49 6e 66 6f 20 2a 70 41 69 6e 63 3b 20 incInfo *pAinc;
6b190 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 /* Information
6b1a0 61 62 6f 75 74 20 41 55 54 4f 49 4e 43 52 45 4d about AUTOINCREM
6b1b0 45 4e 54 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a ENT counters */.
6b1c0 20 20 69 6e 74 20 6e 4d 61 78 41 72 67 3b 20 20 int nMaxArg;
6b1d0 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 20 61 72 /* Max ar
6b1e0 67 73 20 70 61 73 73 65 64 20 74 6f 20 75 73 65 gs passed to use
6b1f0 72 20 66 75 6e 63 74 69 6f 6e 20 62 79 20 73 75 r function by su
6b200 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 0a 20 20 b-program */..
6b210 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 /* Information u
6b220 73 65 64 20 77 68 69 6c 65 20 63 6f 64 69 6e 67 sed while coding
6b230 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d trigger program
6b240 73 2e 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 s. */. Parse *p
6b250 54 6f 70 6c 65 76 65 6c 3b 20 20 20 20 2f 2a 20 Toplevel; /*
6b260 50 61 72 73 65 20 73 74 72 75 63 74 75 72 65 20 Parse structure
6b270 66 6f 72 20 6d 61 69 6e 20 70 72 6f 67 72 61 6d for main program
6b280 20 28 6f 72 20 4e 55 4c 4c 29 20 2a 2f 0a 20 20 (or NULL) */.
6b290 54 61 62 6c 65 20 2a 70 54 72 69 67 67 65 72 54 Table *pTriggerT
6b2a0 61 62 3b 20 20 2f 2a 20 54 61 62 6c 65 20 74 72 ab; /* Table tr
6b2b0 69 67 67 65 72 73 20 61 72 65 20 62 65 69 6e 67 iggers are being
6b2c0 20 63 6f 64 65 64 20 66 6f 72 20 2a 2f 0a 20 20 coded for */.
6b2d0 75 33 32 20 6f 6c 64 6d 61 73 6b 3b 20 20 20 20 u32 oldmask;
6b2e0 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 /* Mask of
6b2f0 6f 6c 64 2e 2a 20 63 6f 6c 75 6d 6e 73 20 72 65 old.* columns re
6b300 66 65 72 65 6e 63 65 64 20 2a 2f 0a 20 20 75 38 ferenced */. u8
6b310 20 65 54 72 69 67 67 65 72 4f 70 3b 20 20 20 20 eTriggerOp;
6b320 20 20 20 2f 2a 20 54 4b 5f 55 50 44 41 54 45 2c /* TK_UPDATE,
6b330 20 54 4b 5f 49 4e 53 45 52 54 20 6f 72 20 54 4b TK_INSERT or TK
6b340 5f 44 45 4c 45 54 45 20 2a 2f 0a 20 20 75 38 20 _DELETE */. u8
6b350 65 4f 72 63 6f 6e 66 3b 20 20 20 20 20 20 20 20 eOrconf;
6b360 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 4f 4e 20 /* Default ON
6b370 43 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79 20 CONFLICT policy
6b380 66 6f 72 20 74 72 69 67 67 65 72 20 73 74 65 70 for trigger step
6b390 73 20 2a 2f 0a 20 20 75 38 20 64 69 73 61 62 6c s */. u8 disabl
6b3a0 65 54 72 69 67 67 65 72 73 3b 20 20 2f 2a 20 54 eTriggers; /* T
6b3b0 72 75 65 20 74 6f 20 64 69 73 61 62 6c 65 20 74 rue to disable t
6b3c0 72 69 67 67 65 72 73 20 2a 2f 0a 0a 20 20 2f 2a riggers */.. /*
6b3d0 20 41 62 6f 76 65 20 69 73 20 63 6f 6e 73 74 61 Above is consta
6b3e0 6e 74 20 62 65 74 77 65 65 6e 20 72 65 63 75 72 nt between recur
6b3f0 73 69 6f 6e 73 2e 20 20 42 65 6c 6f 77 20 69 73 sions. Below is
6b400 20 72 65 73 65 74 20 62 65 66 6f 72 65 20 61 6e reset before an
6b410 64 20 61 66 74 65 72 0a 20 20 2a 2a 20 65 61 63 d after. ** eac
6b420 68 20 72 65 63 75 72 73 69 6f 6e 20 2a 2f 0a 0a h recursion */..
6b430 20 20 69 6e 74 20 6e 56 61 72 3b 20 20 20 20 20 int nVar;
6b440 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
6b450 20 6f 66 20 27 3f 27 20 76 61 72 69 61 62 6c 65 of '?' variable
6b460 73 20 73 65 65 6e 20 69 6e 20 74 68 65 20 53 51 s seen in the SQ
6b470 4c 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e L so far */. in
6b480 74 20 6e 56 61 72 45 78 70 72 3b 20 20 20 20 20 t nVarExpr;
6b490 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
6b4a0 75 73 65 64 20 73 6c 6f 74 73 20 69 6e 20 61 70 used slots in ap
6b4b0 56 61 72 45 78 70 72 5b 5d 20 2a 2f 0a 20 20 69 VarExpr[] */. i
6b4c0 6e 74 20 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 nt nVarExprAlloc
6b4d0 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 ; /* Number of
6b4e0 20 61 6c 6c 6f 63 61 74 65 64 20 73 6c 6f 74 73 allocated slots
6b4f0 20 69 6e 20 61 70 56 61 72 45 78 70 72 5b 5d 20 in apVarExpr[]
6b500 2a 2f 0a 20 20 45 78 70 72 20 2a 2a 61 70 56 61 */. Expr **apVa
6b510 72 45 78 70 72 3b 20 20 20 20 2f 2a 20 50 6f 69 rExpr; /* Poi
6b520 6e 74 65 72 73 20 74 6f 20 3a 61 61 61 20 61 6e nters to :aaa an
6b530 64 20 24 61 61 61 61 20 77 69 6c 64 63 61 72 64 d $aaaa wildcard
6b540 20 65 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a expressions */.
6b550 20 20 56 64 62 65 20 2a 70 52 65 70 72 65 70 61 Vdbe *pReprepa
6b560 72 65 3b 20 20 20 20 2f 2a 20 56 4d 20 62 65 69 re; /* VM bei
6b570 6e 67 20 72 65 70 72 65 70 61 72 65 64 20 28 73 ng reprepared (s
6b580 71 6c 69 74 65 33 52 65 70 72 65 70 61 72 65 28 qlite3Reprepare(
6b590 29 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 69 )) */. int nAli
6b5a0 61 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 as; /*
6b5b0 4e 75 6d 62 65 72 20 6f 66 20 61 6c 69 61 73 65 Number of aliase
6b5c0 64 20 72 65 73 75 6c 74 20 73 65 74 20 63 6f 6c d result set col
6b5d0 75 6d 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 umns */. int nA
6b5e0 6c 69 61 73 41 6c 6c 6f 63 3b 20 20 20 20 20 2f liasAlloc; /
6b5f0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f * Number of allo
6b600 63 61 74 65 64 20 73 6c 6f 74 73 20 66 6f 72 20 cated slots for
6b610 61 41 6c 69 61 73 5b 5d 20 2a 2f 0a 20 20 69 6e aAlias[] */. in
6b620 74 20 2a 61 41 6c 69 61 73 3b 20 20 20 20 20 20 t *aAlias;
6b630 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 75 /* Register u
6b640 73 65 64 20 74 6f 20 68 6f 6c 64 20 61 6c 69 61 sed to hold alia
6b650 73 65 64 20 72 65 73 75 6c 74 20 2a 2f 0a 20 20 sed result */.
6b660 75 38 20 65 78 70 6c 61 69 6e 3b 20 20 20 20 20 u8 explain;
6b670 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
6b680 74 68 65 20 45 58 50 4c 41 49 4e 20 66 6c 61 67 the EXPLAIN flag
6b690 20 69 73 20 66 6f 75 6e 64 20 6f 6e 20 74 68 65 is found on the
6b6a0 20 71 75 65 72 79 20 2a 2f 0a 20 20 54 6f 6b 65 query */. Toke
6b6b0 6e 20 73 4e 61 6d 65 54 6f 6b 65 6e 3b 20 20 20 n sNameToken;
6b6c0 20 2f 2a 20 54 6f 6b 65 6e 20 77 69 74 68 20 75 /* Token with u
6b6d0 6e 71 75 61 6c 69 66 69 65 64 20 73 63 68 65 6d nqualified schem
6b6e0 61 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f a object name */
6b6f0 0a 20 20 54 6f 6b 65 6e 20 73 4c 61 73 74 54 6f . Token sLastTo
6b700 6b 65 6e 3b 20 20 20 20 2f 2a 20 54 68 65 20 6c ken; /* The l
6b710 61 73 74 20 74 6f 6b 65 6e 20 70 61 72 73 65 64 ast token parsed
6b720 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
6b730 20 2a 7a 54 61 69 6c 3b 20 20 20 2f 2a 20 41 6c *zTail; /* Al
6b740 6c 20 53 51 4c 20 74 65 78 74 20 70 61 73 74 20 l SQL text past
6b750 74 68 65 20 6c 61 73 74 20 73 65 6d 69 63 6f 6c the last semicol
6b760 6f 6e 20 70 61 72 73 65 64 20 2a 2f 0a 20 20 54 on parsed */. T
6b770 61 62 6c 65 20 2a 70 4e 65 77 54 61 62 6c 65 3b able *pNewTable;
6b780 20 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 62 /* A table b
6b790 65 69 6e 67 20 63 6f 6e 73 74 72 75 63 74 65 64 eing constructed
6b7a0 20 62 79 20 43 52 45 41 54 45 20 54 41 42 4c 45 by CREATE TABLE
6b7b0 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70 */. Trigger *p
6b7c0 4e 65 77 54 72 69 67 67 65 72 3b 20 20 20 20 20 NewTrigger;
6b7d0 2f 2a 20 54 72 69 67 67 65 72 20 75 6e 64 65 72 /* Trigger under
6b7e0 20 63 6f 6e 73 74 72 75 63 74 20 62 79 20 61 20 construct by a
6b7f0 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20 2a CREATE TRIGGER *
6b800 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
6b810 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b 20 2f 2a zAuthContext; /*
6b820 20 54 68 65 20 36 74 68 20 70 61 72 61 6d 65 74 The 6th paramet
6b830 65 72 20 74 6f 20 64 62 2d 3e 78 41 75 74 68 20 er to db->xAuth
6b840 63 61 6c 6c 62 61 63 6b 73 20 2a 2f 0a 23 69 66 callbacks */.#if
6b850 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
6b860 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 _VIRTUALTABLE.
6b870 54 6f 6b 65 6e 20 73 41 72 67 3b 20 20 20 20 20 Token sArg;
6b880 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f /* Co
6b890 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 61 mplete text of a
6b8a0 20 6d 6f 64 75 6c 65 20 61 72 67 75 6d 65 6e 74 module argument
6b8b0 20 2a 2f 0a 20 20 75 38 20 64 65 63 6c 61 72 65 */. u8 declare
6b8c0 56 74 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 Vtab;
6b8d0 20 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 73 69 /* True if insi
6b8e0 64 65 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 de sqlite3_decla
6b8f0 72 65 5f 76 74 61 62 28 29 20 2a 2f 0a 20 20 69 re_vtab() */. i
6b900 6e 74 20 6e 56 74 61 62 4c 6f 63 6b 3b 20 20 20 nt nVtabLock;
6b910 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
6b920 62 65 72 20 6f 66 20 76 69 72 74 75 61 6c 20 74 ber of virtual t
6b930 61 62 6c 65 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f ables to lock */
6b940 0a 20 20 54 61 62 6c 65 20 2a 2a 61 70 56 74 61 . Table **apVta
6b950 62 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a bLock; /*
6b960 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 69 72 74 Pointer to virt
6b970 75 61 6c 20 74 61 62 6c 65 73 20 6e 65 65 64 69 ual tables needi
6b980 6e 67 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 23 65 ng locking */.#e
6b990 6e 64 69 66 0a 20 20 69 6e 74 20 6e 48 65 69 67 ndif. int nHeig
6b9a0 68 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f ht; /
6b9b0 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 74 72 65 * Expression tre
6b9c0 65 20 68 65 69 67 68 74 20 6f 66 20 63 75 72 72 e height of curr
6b9d0 65 6e 74 20 73 75 62 2d 73 65 6c 65 63 74 20 2a ent sub-select *
6b9e0 2f 0a 20 20 54 61 62 6c 65 20 2a 70 5a 6f 6d 62 /. Table *pZomb
6b9f0 69 65 54 61 62 3b 20 20 20 20 20 20 2f 2a 20 4c ieTab; /* L
6ba00 69 73 74 20 6f 66 20 54 61 62 6c 65 20 6f 62 6a ist of Table obj
6ba10 65 63 74 73 20 74 6f 20 64 65 6c 65 74 65 20 61 ects to delete a
6ba20 66 74 65 72 20 63 6f 64 65 20 67 65 6e 20 2a 2f fter code gen */
6ba30 0a 20 20 54 72 69 67 67 65 72 50 72 67 20 2a 70 . TriggerPrg *p
6ba40 54 72 69 67 67 65 72 50 72 67 3b 20 20 20 20 2f TriggerPrg; /
6ba50 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 * Linked list of
6ba60 20 63 6f 64 65 64 20 74 72 69 67 67 65 72 73 20 coded triggers
6ba70 2a 2f 0a 7d 3b 0a 0a 23 69 66 64 65 66 20 53 51 */.};..#ifdef SQ
6ba80 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
6ba90 4c 54 41 42 4c 45 0a 20 20 23 64 65 66 69 6e 65 LTABLE. #define
6baa0 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 IN_DECLARE_VTAB
6bab0 20 30 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 0.#else. #defi
6bac0 6e 65 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 ne IN_DECLARE_VT
6bad0 41 42 20 28 70 50 61 72 73 65 2d 3e 64 65 63 6c AB (pParse->decl
6bae0 61 72 65 56 74 61 62 29 0a 23 65 6e 64 69 66 0a areVtab).#endif.
6baf0 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e ./*.** An instan
6bb00 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
6bb10 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 63 61 ing structure ca
6bb20 6e 20 62 65 20 64 65 63 6c 61 72 65 64 20 6f 6e n be declared on
6bb30 20 61 20 73 74 61 63 6b 20 61 6e 64 20 75 73 65 a stack and use
6bb40 64 0a 2a 2a 20 74 6f 20 73 61 76 65 20 74 68 65 d.** to save the
6bb50 20 50 61 72 73 65 2e 7a 41 75 74 68 43 6f 6e 74 Parse.zAuthCont
6bb60 65 78 74 20 76 61 6c 75 65 20 73 6f 20 74 68 61 ext value so tha
6bb70 74 20 69 74 20 63 61 6e 20 62 65 20 72 65 73 74 t it can be rest
6bb80 6f 72 65 64 20 6c 61 74 65 72 2e 0a 2a 2f 0a 73 ored later..*/.s
6bb90 74 72 75 63 74 20 41 75 74 68 43 6f 6e 74 65 78 truct AuthContex
6bba0 74 20 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 t {. const char
6bbb0 20 2a 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b 20 *zAuthContext;
6bbc0 20 20 2f 2a 20 50 75 74 20 73 61 76 65 64 20 50 /* Put saved P
6bbd0 61 72 73 65 2e 7a 41 75 74 68 43 6f 6e 74 65 78 arse.zAuthContex
6bbe0 74 20 68 65 72 65 20 2a 2f 0a 20 20 50 61 72 73 t here */. Pars
6bbf0 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 e *pParse;
6bc00 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50 /* The P
6bc10 61 72 73 65 20 73 74 72 75 63 74 75 72 65 20 2a arse structure *
6bc20 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 42 69 74 66 /.};../*.** Bitf
6bc30 69 65 6c 64 20 66 6c 61 67 73 20 66 6f 72 20 50 ield flags for P
6bc40 35 20 76 61 6c 75 65 20 69 6e 20 4f 50 5f 49 6e 5 value in OP_In
6bc50 73 65 72 74 20 61 6e 64 20 4f 50 5f 44 65 6c 65 sert and OP_Dele
6bc60 74 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 te.*/.#define OP
6bc70 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 20 20 20 FLAG_NCHANGE
6bc80 20 20 20 30 78 30 31 20 20 20 20 2f 2a 20 53 65 0x01 /* Se
6bc90 74 20 74 6f 20 75 70 64 61 74 65 20 64 62 2d 3e t to update db->
6bca0 6e 43 68 61 6e 67 65 20 2a 2f 0a 23 64 65 66 69 nChange */.#defi
6bcb0 6e 65 20 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f ne OPFLAG_LASTRO
6bcc0 57 49 44 20 20 20 20 20 30 78 30 32 20 20 20 20 WID 0x02
6bcd0 2f 2a 20 53 65 74 20 74 6f 20 75 70 64 61 74 65 /* Set to update
6bce0 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20 2a db->lastRowid *
6bcf0 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 /.#define OPFLAG
6bd00 5f 49 53 55 50 44 41 54 45 20 20 20 20 20 20 30 _ISUPDATE 0
6bd10 78 30 34 20 20 20 20 2f 2a 20 54 68 69 73 20 4f x04 /* This O
6bd20 50 5f 49 6e 73 65 72 74 20 69 73 20 61 6e 20 73 P_Insert is an s
6bd30 71 6c 20 55 50 44 41 54 45 20 2a 2f 0a 23 64 65 ql UPDATE */.#de
6bd40 66 69 6e 65 20 4f 50 46 4c 41 47 5f 41 50 50 45 fine OPFLAG_APPE
6bd50 4e 44 20 20 20 20 20 20 20 20 30 78 30 38 20 20 ND 0x08
6bd60 20 20 2f 2a 20 54 68 69 73 20 69 73 20 6c 69 6b /* This is lik
6bd70 65 6c 79 20 74 6f 20 62 65 20 61 6e 20 61 70 70 ely to be an app
6bd80 65 6e 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f end */.#define O
6bd90 50 46 4c 41 47 5f 55 53 45 53 45 45 4b 52 45 53 PFLAG_USESEEKRES
6bda0 55 4c 54 20 30 78 31 30 20 20 20 20 2f 2a 20 54 ULT 0x10 /* T
6bdb0 72 79 20 74 6f 20 61 76 6f 69 64 20 61 20 73 65 ry to avoid a se
6bdc0 65 6b 20 69 6e 20 42 74 72 65 65 49 6e 73 65 72 ek in BtreeInser
6bdd0 74 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f t() */.#define O
6bde0 50 46 4c 41 47 5f 43 4c 45 41 52 43 41 43 48 45 PFLAG_CLEARCACHE
6bdf0 20 20 20 20 30 78 32 30 20 20 20 20 2f 2a 20 43 0x20 /* C
6be00 6c 65 61 72 20 70 73 65 75 64 6f 2d 74 61 62 6c lear pseudo-tabl
6be10 65 20 63 61 63 68 65 20 69 6e 20 4f 50 5f 43 6f e cache in OP_Co
6be20 6c 75 6d 6e 20 2a 2f 0a 0a 2f 2a 0a 20 2a 20 45 lumn */../*. * E
6be30 61 63 68 20 74 72 69 67 67 65 72 20 70 72 65 73 ach trigger pres
6be40 65 6e 74 20 69 6e 20 74 68 65 20 64 61 74 61 62 ent in the datab
6be50 61 73 65 20 73 63 68 65 6d 61 20 69 73 20 73 74 ase schema is st
6be60 6f 72 65 64 20 61 73 20 61 6e 20 69 6e 73 74 61 ored as an insta
6be70 6e 63 65 20 6f 66 0a 20 2a 20 73 74 72 75 63 74 nce of. * struct
6be80 20 54 72 69 67 67 65 72 2e 20 0a 20 2a 0a 20 2a Trigger. . *. *
6be90 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 69 6e 73 Pointers to ins
6bea0 74 61 6e 63 65 73 20 6f 66 20 73 74 72 75 63 74 tances of struct
6beb0 20 54 72 69 67 67 65 72 20 61 72 65 20 73 74 6f Trigger are sto
6bec0 72 65 64 20 69 6e 20 74 77 6f 20 77 61 79 73 2e red in two ways.
6bed0 0a 20 2a 20 31 2e 20 49 6e 20 74 68 65 20 22 74 . * 1. In the "t
6bee0 72 69 67 48 61 73 68 22 20 68 61 73 68 20 74 61 rigHash" hash ta
6bef0 62 6c 65 20 28 70 61 72 74 20 6f 66 20 74 68 65 ble (part of the
6bf00 20 73 71 6c 69 74 65 33 2a 20 74 68 61 74 20 72 sqlite3* that r
6bf10 65 70 72 65 73 65 6e 74 73 20 74 68 65 20 0a 20 epresents the .
6bf20 2a 20 20 20 20 64 61 74 61 62 61 73 65 29 2e 20 * database).
6bf30 54 68 69 73 20 61 6c 6c 6f 77 73 20 54 72 69 67 This allows Trig
6bf40 67 65 72 20 73 74 72 75 63 74 75 72 65 73 20 74 ger structures t
6bf50 6f 20 62 65 20 72 65 74 72 69 65 76 65 64 20 62 o be retrieved b
6bf60 79 20 6e 61 6d 65 2e 0a 20 2a 20 32 2e 20 41 6c y name.. * 2. Al
6bf70 6c 20 74 72 69 67 67 65 72 73 20 61 73 73 6f 63 l triggers assoc
6bf80 69 61 74 65 64 20 77 69 74 68 20 61 20 73 69 6e iated with a sin
6bf90 67 6c 65 20 74 61 62 6c 65 20 66 6f 72 6d 20 61 gle table form a
6bfa0 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2c 20 75 73 linked list, us
6bfb0 69 6e 67 20 74 68 65 0a 20 2a 20 20 20 20 70 4e ing the. * pN
6bfc0 65 78 74 20 6d 65 6d 62 65 72 20 6f 66 20 73 74 ext member of st
6bfd0 72 75 63 74 20 54 72 69 67 67 65 72 2e 20 41 20 ruct Trigger. A
6bfe0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66 pointer to the f
6bff0 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 irst element of
6c000 74 68 65 0a 20 2a 20 20 20 20 6c 69 6e 6b 65 64 the. * linked
6c010 20 6c 69 73 74 20 69 73 20 73 74 6f 72 65 64 20 list is stored
6c020 61 73 20 74 68 65 20 22 70 54 72 69 67 67 65 72 as the "pTrigger
6c030 22 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20 " member of the
6c040 61 73 73 6f 63 69 61 74 65 64 0a 20 2a 20 20 20 associated. *
6c050 20 73 74 72 75 63 74 20 54 61 62 6c 65 2e 0a 20 struct Table..
6c060 2a 0a 20 2a 20 54 68 65 20 22 73 74 65 70 5f 6c *. * The "step_l
6c070 69 73 74 22 20 6d 65 6d 62 65 72 20 70 6f 69 6e ist" member poin
6c080 74 73 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 ts to the first
6c090 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20 6c 69 6e element of a lin
6c0a0 6b 65 64 20 6c 69 73 74 0a 20 2a 20 63 6f 6e 74 ked list. * cont
6c0b0 61 69 6e 69 6e 67 20 74 68 65 20 53 51 4c 20 73 aining the SQL s
6c0c0 74 61 74 65 6d 65 6e 74 73 20 73 70 65 63 69 66 tatements specif
6c0d0 69 65 64 20 61 73 20 74 68 65 20 74 72 69 67 67 ied as the trigg
6c0e0 65 72 20 70 72 6f 67 72 61 6d 2e 0a 20 2a 2f 0a er program.. */.
6c0f0 73 74 72 75 63 74 20 54 72 69 67 67 65 72 20 7b struct Trigger {
6c100 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 . char *zName;
6c110 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
6c120 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 72 e name of the tr
6c130 69 67 67 65 72 20 20 20 20 20 20 20 20 20 20 20 igger
6c140 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a */.
6c150 20 20 63 68 61 72 20 2a 74 61 62 6c 65 3b 20 20 char *table;
6c160 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
6c170 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 74 table or view t
6c180 6f 20 77 68 69 63 68 20 74 68 65 20 74 72 69 67 o which the trig
6c190 67 65 72 20 61 70 70 6c 69 65 73 20 2a 2f 0a 20 ger applies */.
6c1a0 20 75 38 20 6f 70 3b 20 20 20 20 20 20 20 20 20 u8 op;
6c1b0 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 /* One
6c1c0 6f 66 20 54 4b 5f 44 45 4c 45 54 45 2c 20 54 4b of TK_DELETE, TK
6c1d0 5f 55 50 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 _UPDATE, TK_INSE
6c1e0 52 54 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 RT */.
6c1f0 75 38 20 74 72 5f 74 6d 3b 20 20 20 20 20 20 20 u8 tr_tm;
6c200 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f /* One o
6c210 66 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 f TRIGGER_BEFORE
6c220 2c 20 54 52 49 47 47 45 52 5f 41 46 54 45 52 20 , TRIGGER_AFTER
6c230 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 6e */. Expr *pWhen
6c240 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
6c250 54 68 65 20 57 48 45 4e 20 63 6c 61 75 73 65 20 The WHEN clause
6c260 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f of the expressio
6c270 6e 20 28 6d 61 79 20 62 65 20 4e 55 4c 4c 29 20 n (may be NULL)
6c280 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70 43 6f */. IdList *pCo
6c290 6c 75 6d 6e 73 3b 20 20 20 20 20 20 20 2f 2a 20 lumns; /*
6c2a0 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 55 50 If this is an UP
6c2b0 44 41 54 45 20 4f 46 20 3c 63 6f 6c 75 6d 6e 2d DATE OF <column-
6c2c0 6c 69 73 74 3e 20 74 72 69 67 67 65 72 2c 0a 20 list> trigger,.
6c2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6c2e0 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 the
6c2f0 3c 63 6f 6c 75 6d 6e 2d 6c 69 73 74 3e 20 69 73 <column-list> is
6c300 20 73 74 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a stored here */.
6c310 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d Schema *pSchem
6c320 61 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 63 68 a; /* Sch
6c330 65 6d 61 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 ema containing t
6c340 68 65 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 he trigger */.
6c350 53 63 68 65 6d 61 20 2a 70 54 61 62 53 63 68 65 Schema *pTabSche
6c360 6d 61 3b 20 20 20 20 20 2f 2a 20 53 63 68 65 6d ma; /* Schem
6c370 61 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 a containing the
6c380 20 74 61 62 6c 65 20 2a 2f 0a 20 20 54 72 69 67 table */. Trig
6c390 67 65 72 53 74 65 70 20 2a 73 74 65 70 5f 6c 69 gerStep *step_li
6c3a0 73 74 3b 20 2f 2a 20 4c 69 6e 6b 20 6c 69 73 74 st; /* Link list
6c3b0 20 6f 66 20 74 72 69 67 67 65 72 20 70 72 6f 67 of trigger prog
6c3c0 72 61 6d 20 73 74 65 70 73 20 20 20 20 20 20 20 ram steps
6c3d0 20 20 20 20 20 20 2a 2f 0a 20 20 54 72 69 67 67 */. Trigg
6c3e0 65 72 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 er *pNext;
6c3f0 20 20 20 2f 2a 20 4e 65 78 74 20 74 72 69 67 67 /* Next trigg
6c400 65 72 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 er associated wi
6c410 74 68 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a th the table */.
6c420 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 74 72 69 67 };../*.** A trig
6c430 67 65 72 20 69 73 20 65 69 74 68 65 72 20 61 20 ger is either a
6c440 42 45 46 4f 52 45 20 6f 72 20 61 6e 20 41 46 54 BEFORE or an AFT
6c450 45 52 20 74 72 69 67 67 65 72 2e 20 20 54 68 65 ER trigger. The
6c460 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e 73 74 following const
6c470 61 6e 74 73 0a 2a 2a 20 64 65 74 65 72 6d 69 6e ants.** determin
6c480 65 20 77 68 69 63 68 2e 20 0a 2a 2a 0a 2a 2a 20 e which. .**.**
6c490 49 66 20 74 68 65 72 65 20 61 72 65 20 6d 75 6c If there are mul
6c4a0 74 69 70 6c 65 20 74 72 69 67 67 65 72 73 2c 20 tiple triggers,
6c4b0 79 6f 75 20 6d 69 67 68 74 20 6f 66 20 73 6f 6d you might of som
6c4c0 65 20 42 45 46 4f 52 45 20 61 6e 64 20 73 6f 6d e BEFORE and som
6c4d0 65 20 41 46 54 45 52 2e 0a 2a 2a 20 49 6e 20 74 e AFTER..** In t
6c4e0 68 61 74 20 63 61 73 65 73 2c 20 74 68 65 20 63 hat cases, the c
6c4f0 6f 6e 73 74 61 6e 74 73 20 62 65 6c 6f 77 20 63 onstants below c
6c500 61 6e 20 62 65 20 4f 52 65 64 20 74 6f 67 65 74 an be ORed toget
6c510 68 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 her..*/.#define
6c520 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 20 20 TRIGGER_BEFORE
6c530 31 0a 23 64 65 66 69 6e 65 20 54 52 49 47 47 45 1.#define TRIGGE
6c540 52 5f 41 46 54 45 52 20 20 20 32 0a 0a 2f 2a 0a R_AFTER 2../*.
6c550 20 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f * An instance o
6c560 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72 f struct Trigger
6c570 53 74 65 70 20 69 73 20 75 73 65 64 20 74 6f 20 Step is used to
6c580 73 74 6f 72 65 20 61 20 73 69 6e 67 6c 65 20 53 store a single S
6c590 51 4c 20 73 74 61 74 65 6d 65 6e 74 0a 20 2a 20 QL statement. *
6c5a0 74 68 61 74 20 69 73 20 61 20 70 61 72 74 20 6f that is a part o
6c5b0 66 20 61 20 74 72 69 67 67 65 72 2d 70 72 6f 67 f a trigger-prog
6c5c0 72 61 6d 2e 20 0a 20 2a 0a 20 2a 20 49 6e 73 74 ram. . *. * Inst
6c5d0 61 6e 63 65 73 20 6f 66 20 73 74 72 75 63 74 20 ances of struct
6c5e0 54 72 69 67 67 65 72 53 74 65 70 20 61 72 65 20 TriggerStep are
6c5f0 73 74 6f 72 65 64 20 69 6e 20 61 20 73 69 6e 67 stored in a sing
6c600 6c 79 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 28 ly linked list (
6c610 6c 69 6e 6b 65 64 0a 20 2a 20 75 73 69 6e 67 20 linked. * using
6c620 74 68 65 20 22 70 4e 65 78 74 22 20 6d 65 6d 62 the "pNext" memb
6c630 65 72 29 20 72 65 66 65 72 65 6e 63 65 64 20 62 er) referenced b
6c640 79 20 74 68 65 20 22 73 74 65 70 5f 6c 69 73 74 y the "step_list
6c650 22 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20 " member of the
6c660 0a 20 2a 20 61 73 73 6f 63 69 61 74 65 64 20 73 . * associated s
6c670 74 72 75 63 74 20 54 72 69 67 67 65 72 20 69 6e truct Trigger in
6c680 73 74 61 6e 63 65 2e 20 54 68 65 20 66 69 72 73 stance. The firs
6c690 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 t element of the
6c6a0 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 69 73 0a linked list is.
6c6b0 20 2a 20 74 68 65 20 66 69 72 73 74 20 73 74 65 * the first ste
6c6c0 70 20 6f 66 20 74 68 65 20 74 72 69 67 67 65 72 p of the trigger
6c6d0 2d 70 72 6f 67 72 61 6d 2e 0a 20 2a 20 0a 20 2a -program.. * . *
6c6e0 20 54 68 65 20 22 6f 70 22 20 6d 65 6d 62 65 72 The "op" member
6c6f0 20 69 6e 64 69 63 61 74 65 73 20 77 68 65 74 68 indicates wheth
6c700 65 72 20 74 68 69 73 20 69 73 20 61 20 22 44 45 er this is a "DE
6c710 4c 45 54 45 22 2c 20 22 49 4e 53 45 52 54 22 2c LETE", "INSERT",
6c720 20 22 55 50 44 41 54 45 22 20 6f 72 0a 20 2a 20 "UPDATE" or. *
6c730 22 53 45 4c 45 43 54 22 20 73 74 61 74 65 6d 65 "SELECT" stateme
6c740 6e 74 2e 20 54 68 65 20 6d 65 61 6e 69 6e 67 73 nt. The meanings
6c750 20 6f 66 20 74 68 65 20 6f 74 68 65 72 20 6d 65 of the other me
6c760 6d 62 65 72 73 20 69 73 20 64 65 74 65 72 6d 69 mbers is determi
6c770 6e 65 64 20 62 79 20 74 68 65 20 0a 20 2a 20 76 ned by the . * v
6c780 61 6c 75 65 20 6f 66 20 22 6f 70 22 20 61 73 20 alue of "op" as
6c790 66 6f 6c 6c 6f 77 73 3a 0a 20 2a 0a 20 2a 20 28 follows:. *. * (
6c7a0 6f 70 20 3d 3d 20 54 4b 5f 49 4e 53 45 52 54 29 op == TK_INSERT)
6c7b0 0a 20 2a 20 6f 72 63 6f 6e 66 20 20 20 20 2d 3e . * orconf ->
6c7c0 20 73 74 6f 72 65 73 20 74 68 65 20 4f 4e 20 43 stores the ON C
6c7d0 4f 4e 46 4c 49 43 54 20 61 6c 67 6f 72 69 74 68 ONFLICT algorith
6c7e0 6d 0a 20 2a 20 70 53 65 6c 65 63 74 20 20 20 2d m. * pSelect -
6c7f0 3e 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 > If this is an
6c800 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 INSERT INTO ...
6c810 53 45 4c 45 43 54 20 2e 2e 2e 20 73 74 61 74 65 SELECT ... state
6c820 6d 65 6e 74 2c 20 74 68 65 6e 0a 20 2a 20 20 20 ment, then. *
6c830 20 20 20 20 20 20 20 20 20 20 20 74 68 69 73 20 this
6c840 73 74 6f 72 65 73 20 61 20 70 6f 69 6e 74 65 72 stores a pointer
6c850 20 74 6f 20 74 68 65 20 53 45 4c 45 43 54 20 73 to the SELECT s
6c860 74 61 74 65 6d 65 6e 74 2e 20 4f 74 68 65 72 77 tatement. Otherw
6c870 69 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 74 61 72 ise NULL.. * tar
6c880 67 65 74 20 20 20 20 2d 3e 20 41 20 74 6f 6b 65 get -> A toke
6c890 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 71 75 n holding the qu
6c8a0 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 74 68 65 oted name of the
6c8b0 20 74 61 62 6c 65 20 74 6f 20 69 6e 73 65 72 74 table to insert
6c8c0 20 69 6e 74 6f 2e 0a 20 2a 20 70 45 78 70 72 4c into.. * pExprL
6c8d0 69 73 74 20 2d 3e 20 49 66 20 74 68 69 73 20 69 ist -> If this i
6c8e0 73 20 61 6e 20 49 4e 53 45 52 54 20 49 4e 54 4f s an INSERT INTO
6c8f0 20 2e 2e 2e 20 56 41 4c 55 45 53 20 2e 2e 2e 20 ... VALUES ...
6c900 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 0a statement, then.
6c910 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
6c920 74 68 69 73 20 73 74 6f 72 65 73 20 76 61 6c 75 this stores valu
6c930 65 73 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 es to be inserte
6c940 64 2e 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c d. Otherwise NUL
6c950 4c 2e 0a 20 2a 20 70 49 64 4c 69 73 74 20 20 20 L.. * pIdList
6c960 2d 3e 20 49 66 20 74 68 69 73 20 69 73 20 61 6e -> If this is an
6c970 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e 2e INSERT INTO ...
6c980 20 28 3c 63 6f 6c 75 6d 6e 2d 6e 61 6d 65 73 3e (<column-names>
6c990 29 20 56 41 4c 55 45 53 20 2e 2e 2e 20 0a 20 2a ) VALUES ... . *
6c9a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74 st
6c9b0 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 74 68 atement, then th
6c9c0 69 73 20 73 74 6f 72 65 73 20 74 68 65 20 63 6f is stores the co
6c9d0 6c 75 6d 6e 2d 6e 61 6d 65 73 20 74 6f 20 62 65 lumn-names to be
6c9e0 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 . *
6c9f0 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 2e 0a inserted into..
6ca00 20 2a 0a 20 2a 20 28 6f 70 20 3d 3d 20 54 4b 5f *. * (op == TK_
6ca10 44 45 4c 45 54 45 29 0a 20 2a 20 74 61 72 67 65 DELETE). * targe
6ca20 74 20 20 20 20 2d 3e 20 41 20 74 6f 6b 65 6e 20 t -> A token
6ca30 68 6f 6c 64 69 6e 67 20 74 68 65 20 71 75 6f 74 holding the quot
6ca40 65 64 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 ed name of the t
6ca50 61 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 66 able to delete f
6ca60 72 6f 6d 2e 0a 20 2a 20 70 57 68 65 72 65 20 20 rom.. * pWhere
6ca70 20 20 2d 3e 20 54 68 65 20 57 48 45 52 45 20 63 -> The WHERE c
6ca80 6c 61 75 73 65 20 6f 66 20 74 68 65 20 44 45 4c lause of the DEL
6ca90 45 54 45 20 73 74 61 74 65 6d 65 6e 74 20 69 66 ETE statement if
6caa0 20 6f 6e 65 20 69 73 20 73 70 65 63 69 66 69 65 one is specifie
6cab0 64 2e 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 d.. *
6cac0 20 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c Otherwise NUL
6cad0 4c 2e 0a 20 2a 20 0a 20 2a 20 28 6f 70 20 3d 3d L.. * . * (op ==
6cae0 20 54 4b 5f 55 50 44 41 54 45 29 0a 20 2a 20 74 TK_UPDATE). * t
6caf0 61 72 67 65 74 20 20 20 20 2d 3e 20 41 20 74 6f arget -> A to
6cb00 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 ken holding the
6cb10 71 75 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 74 quoted name of t
6cb20 68 65 20 74 61 62 6c 65 20 74 6f 20 75 70 64 61 he table to upda
6cb30 74 65 20 72 6f 77 73 20 6f 66 2e 0a 20 2a 20 70 te rows of.. * p
6cb40 57 68 65 72 65 20 20 20 20 2d 3e 20 54 68 65 20 Where -> The
6cb50 57 48 45 52 45 20 63 6c 61 75 73 65 20 6f 66 20 WHERE clause of
6cb60 74 68 65 20 55 50 44 41 54 45 20 73 74 61 74 65 the UPDATE state
6cb70 6d 65 6e 74 20 69 66 20 6f 6e 65 20 69 73 20 73 ment if one is s
6cb80 70 65 63 69 66 69 65 64 2e 0a 20 2a 20 20 20 20 pecified.. *
6cb90 20 20 20 20 20 20 20 20 20 20 4f 74 68 65 72 77 Otherw
6cba0 69 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 70 45 78 ise NULL.. * pEx
6cbb0 70 72 4c 69 73 74 20 2d 3e 20 41 20 6c 69 73 74 prList -> A list
6cbc0 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 of the columns
6cbd0 74 6f 20 75 70 64 61 74 65 20 61 6e 64 20 74 68 to update and th
6cbe0 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 6f e expressions to
6cbf0 20 75 70 64 61 74 65 0a 20 2a 20 20 20 20 20 20 update. *
6cc00 20 20 20 20 20 20 20 20 74 68 65 6d 20 74 6f 2e them to.
6cc10 20 53 65 65 20 73 71 6c 69 74 65 33 55 70 64 61 See sqlite3Upda
6cc20 74 65 28 29 20 64 6f 63 75 6d 65 6e 74 61 74 69 te() documentati
6cc30 6f 6e 20 6f 66 20 22 70 43 68 61 6e 67 65 73 22 on of "pChanges"
6cc40 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 . *
6cc50 20 61 72 67 75 6d 65 6e 74 2e 0a 20 2a 20 0a 20 argument.. * .
6cc60 2a 2f 0a 73 74 72 75 63 74 20 54 72 69 67 67 65 */.struct Trigge
6cc70 72 53 74 65 70 20 7b 0a 20 20 75 38 20 6f 70 3b rStep {. u8 op;
6cc80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
6cc90 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 44 45 4c 45 * One of TK_DELE
6cca0 54 45 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 54 TE, TK_UPDATE, T
6ccb0 4b 5f 49 4e 53 45 52 54 2c 20 54 4b 5f 53 45 4c K_INSERT, TK_SEL
6ccc0 45 43 54 20 2a 2f 0a 20 20 75 38 20 6f 72 63 6f ECT */. u8 orco
6ccd0 6e 66 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a nf; /*
6cce0 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 65 74 63 OE_Rollback etc
6ccf0 2e 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a . */. Trigger *
6cd00 70 54 72 69 67 3b 20 20 20 20 20 20 2f 2a 20 54 pTrig; /* T
6cd10 68 65 20 74 72 69 67 67 65 72 20 74 68 61 74 20 he trigger that
6cd20 74 68 69 73 20 73 74 65 70 20 69 73 20 61 20 70 this step is a p
6cd30 61 72 74 20 6f 66 20 2a 2f 0a 20 20 53 65 6c 65 art of */. Sele
6cd40 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 20 20 20 ct *pSelect;
6cd50 20 2f 2a 20 53 45 4c 45 43 54 20 73 74 61 74 6d /* SELECT statm
6cd60 65 6e 74 20 6f 72 20 52 48 53 20 6f 66 20 49 4e ent or RHS of IN
6cd70 53 45 52 54 20 49 4e 54 4f 20 2e 2e 20 53 45 4c SERT INTO .. SEL
6cd80 45 43 54 20 2e 2e 2e 20 2a 2f 0a 20 20 54 6f 6b ECT ... */. Tok
6cd90 65 6e 20 74 61 72 67 65 74 3b 20 20 20 20 20 20 en target;
6cda0 20 20 2f 2a 20 54 61 72 67 65 74 20 74 61 62 6c /* Target tabl
6cdb0 65 20 66 6f 72 20 44 45 4c 45 54 45 2c 20 55 50 e for DELETE, UP
6cdc0 44 41 54 45 2c 20 49 4e 53 45 52 54 20 2a 2f 0a DATE, INSERT */.
6cdd0 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 3b 20 Expr *pWhere;
6cde0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 /* The WH
6cdf0 45 52 45 20 63 6c 61 75 73 65 20 66 6f 72 20 44 ERE clause for D
6ce00 45 4c 45 54 45 20 6f 72 20 55 50 44 41 54 45 20 ELETE or UPDATE
6ce10 73 74 65 70 73 20 2a 2f 0a 20 20 45 78 70 72 4c steps */. ExprL
6ce20 69 73 74 20 2a 70 45 78 70 72 4c 69 73 74 3b 20 ist *pExprList;
6ce30 2f 2a 20 53 45 54 20 63 6c 61 75 73 65 20 66 6f /* SET clause fo
6ce40 72 20 55 50 44 41 54 45 2e 20 20 56 41 4c 55 45 r UPDATE. VALUE
6ce50 53 20 63 6c 61 75 73 65 20 66 6f 72 20 49 4e 53 S clause for INS
6ce60 45 52 54 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20 ERT */. IdList
6ce70 2a 70 49 64 4c 69 73 74 3b 20 20 20 20 20 2f 2a *pIdList; /*
6ce80 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 6f Column names fo
6ce90 72 20 49 4e 53 45 52 54 20 2a 2f 0a 20 20 54 72 r INSERT */. Tr
6cea0 69 67 67 65 72 53 74 65 70 20 2a 70 4e 65 78 74 iggerStep *pNext
6ceb0 3b 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 74 68 ; /* Next in th
6cec0 65 20 6c 69 6e 6b 2d 6c 69 73 74 20 2a 2f 0a 20 e link-list */.
6ced0 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 4c TriggerStep *pL
6cee0 61 73 74 3b 20 20 2f 2a 20 4c 61 73 74 20 65 6c ast; /* Last el
6cef0 65 6d 65 6e 74 20 69 6e 20 6c 69 6e 6b 2d 6c 69 ement in link-li
6cf00 73 74 2e 20 56 61 6c 69 64 20 66 6f 72 20 31 73 st. Valid for 1s
6cf10 74 20 65 6c 65 6d 20 6f 6e 6c 79 20 2a 2f 0a 7d t elem only */.}
6cf20 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c ;../*.** The fol
6cf30 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
6cf40 20 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d contains inform
6cf50 61 74 69 6f 6e 20 75 73 65 64 20 62 79 20 74 68 ation used by th
6cf60 65 20 73 71 6c 69 74 65 46 69 78 2e 2e 2e 0a 2a e sqliteFix....*
6cf70 2a 20 72 6f 75 74 69 6e 65 73 20 61 73 20 74 68 * routines as th
6cf80 65 79 20 77 61 6c 6b 20 74 68 65 20 70 61 72 73 ey walk the pars
6cf90 65 20 74 72 65 65 20 74 6f 20 6d 61 6b 65 20 64 e tree to make d
6cfa0 61 74 61 62 61 73 65 20 72 65 66 65 72 65 6e 63 atabase referenc
6cfb0 65 73 0a 2a 2a 20 65 78 70 6c 69 63 69 74 2e 20 es.** explicit.
6cfc0 20 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 .*/.typedef str
6cfd0 75 63 74 20 44 62 46 69 78 65 72 20 44 62 46 69 uct DbFixer DbFi
6cfe0 78 65 72 3b 0a 73 74 72 75 63 74 20 44 62 46 69 xer;.struct DbFi
6cff0 78 65 72 20 7b 0a 20 20 50 61 72 73 65 20 2a 70 xer {. Parse *p
6d000 50 61 72 73 65 3b 20 20 20 20 20 20 2f 2a 20 54 Parse; /* T
6d010 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 he parsing conte
6d020 78 74 2e 20 20 45 72 72 6f 72 20 6d 65 73 73 61 xt. Error messa
6d030 67 65 73 20 77 72 69 74 74 65 6e 20 68 65 72 65 ges written here
6d040 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
6d050 20 2a 7a 44 62 3b 20 20 20 20 2f 2a 20 4d 61 6b *zDb; /* Mak
6d060 65 20 73 75 72 65 20 61 6c 6c 20 6f 62 6a 65 63 e sure all objec
6d070 74 73 20 61 72 65 20 63 6f 6e 74 61 69 6e 65 64 ts are contained
6d080 20 69 6e 20 74 68 69 73 20 64 61 74 61 62 61 73 in this databas
6d090 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 e */. const cha
6d0a0 72 20 2a 7a 54 79 70 65 3b 20 20 2f 2a 20 54 79 r *zType; /* Ty
6d0b0 70 65 20 6f 66 20 74 68 65 20 63 6f 6e 74 61 69 pe of the contai
6d0c0 6e 65 72 20 2d 20 75 73 65 64 20 66 6f 72 20 65 ner - used for e
6d0d0 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f rror messages */
6d0e0 0a 20 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a . const Token *
6d0f0 70 4e 61 6d 65 3b 20 2f 2a 20 4e 61 6d 65 20 6f pName; /* Name o
6d100 66 20 74 68 65 20 63 6f 6e 74 61 69 6e 65 72 20 f the container
6d110 2d 20 75 73 65 64 20 66 6f 72 20 65 72 72 6f 72 - used for error
6d120 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a 7d 3b 0a messages */.};.
6d130 0a 2f 2a 0a 2a 2a 20 41 6e 20 6f 62 6a 65 63 74 ./*.** An object
6d140 65 64 20 75 73 65 64 20 74 6f 20 61 63 63 75 6d ed used to accum
6d150 75 6c 61 74 65 20 74 68 65 20 74 65 78 74 20 6f ulate the text o
6d160 66 20 61 20 73 74 72 69 6e 67 20 77 68 65 72 65 f a string where
6d170 20 77 65 0a 2a 2a 20 64 6f 20 6e 6f 74 20 6e 65 we.** do not ne
6d180 63 65 73 73 61 72 69 6c 79 20 6b 6e 6f 77 20 68 cessarily know h
6d190 6f 77 20 62 69 67 20 74 68 65 20 73 74 72 69 6e ow big the strin
6d1a0 67 20 77 69 6c 6c 20 62 65 20 69 6e 20 74 68 65 g will be in the
6d1b0 20 65 6e 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 end..*/.struct
6d1c0 53 74 72 41 63 63 75 6d 20 7b 0a 20 20 73 71 6c StrAccum {. sql
6d1d0 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 ite3 *db;
6d1e0 20 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 64 61 /* Optional da
6d1f0 74 61 62 61 73 65 20 66 6f 72 20 6c 6f 6f 6b 61 tabase for looka
6d200 73 69 64 65 2e 20 20 43 61 6e 20 62 65 20 4e 55 side. Can be NU
6d210 4c 4c 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 42 LL */. char *zB
6d220 61 73 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 ase; /*
6d230 41 20 62 61 73 65 20 61 6c 6c 6f 63 61 74 69 6f A base allocatio
6d240 6e 2e 20 20 4e 6f 74 20 66 72 6f 6d 20 6d 61 6c n. Not from mal
6d250 6c 6f 63 2e 20 2a 2f 0a 20 20 63 68 61 72 20 2a loc. */. char *
6d260 7a 54 65 78 74 3b 20 20 20 20 20 20 20 20 20 2f zText; /
6d270 2a 20 54 68 65 20 73 74 72 69 6e 67 20 63 6f 6c * The string col
6d280 6c 65 63 74 65 64 20 73 6f 20 66 61 72 20 2a 2f lected so far */
6d290 0a 20 20 69 6e 74 20 20 6e 43 68 61 72 3b 20 20 . int nChar;
6d2a0 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 /* Lengt
6d2b0 68 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 20 h of the string
6d2c0 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 so far */. int
6d2d0 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 nAlloc;
6d2e0 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 73 70 /* Amount of sp
6d2f0 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e ace allocated in
6d300 20 7a 54 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 zText */. int
6d310 20 6d 78 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 mxAlloc;
6d320 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 6c 6c 6f /* Maximum allo
6d330 77 65 64 20 73 74 72 69 6e 67 20 6c 65 6e 67 74 wed string lengt
6d340 68 20 2a 2f 0a 20 20 75 38 20 20 20 6d 61 6c 6c h */. u8 mall
6d350 6f 63 46 61 69 6c 65 64 3b 20 20 20 2f 2a 20 42 ocFailed; /* B
6d360 65 63 6f 6d 65 73 20 74 72 75 65 20 69 66 20 61 ecomes true if a
6d370 6e 79 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 ny memory alloca
6d380 74 69 6f 6e 20 66 61 69 6c 73 20 2a 2f 0a 20 20 tion fails */.
6d390 75 38 20 20 20 75 73 65 4d 61 6c 6c 6f 63 3b 20 u8 useMalloc;
6d3a0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
6d3b0 7a 54 65 78 74 20 69 73 20 65 6e 6c 61 72 67 65 zText is enlarge
6d3c0 61 62 6c 65 20 75 73 69 6e 67 20 72 65 61 6c 6c able using reall
6d3d0 6f 63 20 2a 2f 0a 20 20 75 38 20 20 20 74 6f 6f oc */. u8 too
6d3e0 42 69 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 Big; /*
6d3f0 42 65 63 6f 6d 65 73 20 74 72 75 65 20 69 66 20 Becomes true if
6d400 73 74 72 69 6e 67 20 73 69 7a 65 20 65 78 63 65 string size exce
6d410 65 64 73 20 6c 69 6d 69 74 73 20 2a 2f 0a 7d 3b eds limits */.};
6d420 0a 0a 2f 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 ../*.** A pointe
6d430 72 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 r to this struct
6d440 75 72 65 20 69 73 20 75 73 65 64 20 74 6f 20 63 ure is used to c
6d450 6f 6d 6d 75 6e 69 63 61 74 65 20 69 6e 66 6f 72 ommunicate infor
6d460 6d 61 74 69 6f 6e 0a 2a 2a 20 66 72 6f 6d 20 73 mation.** from s
6d470 71 6c 69 74 65 33 49 6e 69 74 20 61 6e 64 20 4f qlite3Init and O
6d480 50 5f 50 61 72 73 65 53 63 68 65 6d 61 20 69 6e P_ParseSchema in
6d490 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 49 6e to the sqlite3In
6d4a0 69 74 43 61 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 74 itCallback..*/.t
6d4b0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 7b 0a ypedef struct {.
6d4c0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 sqlite3 *db;
6d4d0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 /* The dat
6d4e0 61 62 61 73 65 20 62 65 69 6e 67 20 69 6e 69 74 abase being init
6d4f0 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 ialized */. int
6d500 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 iDb;
6d510 20 2f 2a 20 30 20 66 6f 72 20 6d 61 69 6e 20 64 /* 0 for main d
6d520 61 74 61 62 61 73 65 2e 20 20 31 20 66 6f 72 20 atabase. 1 for
6d530 54 45 4d 50 2c 20 32 2e 2e 20 66 6f 72 20 41 54 TEMP, 2.. for AT
6d540 54 41 43 48 65 64 20 2a 2f 0a 20 20 63 68 61 72 TACHed */. char
6d550 20 2a 2a 70 7a 45 72 72 4d 73 67 3b 20 20 20 20 **pzErrMsg;
6d560 2f 2a 20 45 72 72 6f 72 20 6d 65 73 73 61 67 65 /* Error message
6d570 20 73 74 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a stored here */.
6d580 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 int rc;
6d590 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 /* Result
6d5a0 63 6f 64 65 20 73 74 6f 72 65 64 20 68 65 72 65 code stored here
6d5b0 20 2a 2f 0a 7d 20 49 6e 69 74 44 61 74 61 3b 0a */.} InitData;.
6d5c0 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63 74 75 72 65 ./*.** Structure
6d5d0 20 63 6f 6e 74 61 69 6e 69 6e 67 20 67 6c 6f 62 containing glob
6d5e0 61 6c 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e al configuration
6d5f0 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 53 51 data for the SQ
6d600 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2a Lite library..**
6d610 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 .** This structu
6d620 72 65 20 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 73 re also contains
6d630 20 73 6f 6d 65 20 73 74 61 74 65 20 69 6e 66 6f some state info
6d640 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 rmation..*/.stru
6d650 63 74 20 53 71 6c 69 74 65 33 43 6f 6e 66 69 67 ct Sqlite3Config
6d660 20 7b 0a 20 20 69 6e 74 20 62 4d 65 6d 73 74 61 {. int bMemsta
6d670 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t;
6d680 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 /* True t
6d690 6f 20 65 6e 61 62 6c 65 20 6d 65 6d 6f 72 79 20 o enable memory
6d6a0 73 74 61 74 75 73 20 2a 2f 0a 20 20 69 6e 74 20 status */. int
6d6b0 62 43 6f 72 65 4d 75 74 65 78 3b 20 20 20 20 20 bCoreMutex;
6d6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
6d6d0 20 54 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 True to enable
6d6e0 63 6f 72 65 20 6d 75 74 65 78 69 6e 67 20 2a 2f core mutexing */
6d6f0 0a 20 20 69 6e 74 20 62 46 75 6c 6c 4d 75 74 65 . int bFullMute
6d700 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 x;
6d710 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 /* True to
6d720 65 6e 61 62 6c 65 20 66 75 6c 6c 20 6d 75 74 65 enable full mute
6d730 78 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 xing */. int mx
6d740 53 74 72 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 Strlen;
6d750 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d /* M
6d760 61 78 69 6d 75 6d 20 73 74 72 69 6e 67 20 6c 65 aximum string le
6d770 6e 67 74 68 20 2a 2f 0a 20 20 69 6e 74 20 73 7a ngth */. int sz
6d780 4c 6f 6f 6b 61 73 69 64 65 3b 20 20 20 20 20 20 Lookaside;
6d790 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 /* D
6d7a0 65 66 61 75 6c 74 20 6c 6f 6f 6b 61 73 69 64 65 efault lookaside
6d7b0 20 62 75 66 66 65 72 20 73 69 7a 65 20 2a 2f 0a buffer size */.
6d7c0 20 20 69 6e 74 20 6e 4c 6f 6f 6b 61 73 69 64 65 int nLookaside
6d7d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
6d7e0 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 6c /* Default l
6d7f0 6f 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 20 ookaside buffer
6d800 63 6f 75 6e 74 20 2a 2f 0a 20 20 73 71 6c 69 74 count */. sqlit
6d810 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6d e3_mem_methods m
6d820 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
6d830 4c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 Low-level memory
6d840 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 74 65 allocation inte
6d850 72 66 61 63 65 20 2a 2f 0a 20 20 73 71 6c 69 74 rface */. sqlit
6d860 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 e3_mutex_methods
6d870 20 6d 75 74 65 78 3b 20 20 20 20 20 20 2f 2a 20 mutex; /*
6d880 4c 6f 77 2d 6c 65 76 65 6c 20 6d 75 74 65 78 20 Low-level mutex
6d890 69 6e 74 65 72 66 61 63 65 20 2a 2f 0a 20 20 73 interface */. s
6d8a0 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 qlite3_pcache_me
6d8b0 74 68 6f 64 73 20 70 63 61 63 68 65 3b 20 20 20 thods pcache;
6d8c0 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 70 61 /* Low-level pa
6d8d0 67 65 2d 63 61 63 68 65 20 69 6e 74 65 72 66 61 ge-cache interfa
6d8e0 63 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 48 ce */. void *pH
6d8f0 65 61 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 eap;
6d900 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 65 61 /* Hea
6d910 70 20 73 74 6f 72 61 67 65 20 73 70 61 63 65 20 p storage space
6d920 2a 2f 0a 20 20 69 6e 74 20 6e 48 65 61 70 3b 20 */. int nHeap;
6d930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d940 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
6d950 66 20 70 48 65 61 70 5b 5d 20 2a 2f 0a 20 20 69 f pHeap[] */. i
6d960 6e 74 20 6d 6e 52 65 71 2c 20 6d 78 52 65 71 3b nt mnReq, mxReq;
6d970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d980 20 2f 2a 20 4d 69 6e 20 61 6e 64 20 6d 61 78 20 /* Min and max
6d990 68 65 61 70 20 72 65 71 75 65 73 74 73 20 73 69 heap requests si
6d9a0 7a 65 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 zes */. void *p
6d9b0 53 63 72 61 74 63 68 3b 20 20 20 20 20 20 20 20 Scratch;
6d9c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 63 /* Sc
6d9d0 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 2a 2f 0a ratch memory */.
6d9e0 20 20 69 6e 74 20 73 7a 53 63 72 61 74 63 68 3b int szScratch;
6d9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6da00 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 /* Size of e
6da10 61 63 68 20 73 63 72 61 74 63 68 20 62 75 66 66 ach scratch buff
6da20 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 63 72 er */. int nScr
6da30 61 74 63 68 3b 20 20 20 20 20 20 20 20 20 20 20 atch;
6da40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
6da50 62 65 72 20 6f 66 20 73 63 72 61 74 63 68 20 62 ber of scratch b
6da60 75 66 66 65 72 73 20 2a 2f 0a 20 20 76 6f 69 64 uffers */. void
6da70 20 2a 70 50 61 67 65 3b 20 20 20 20 20 20 20 20 *pPage;
6da80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
6da90 20 50 61 67 65 20 63 61 63 68 65 20 6d 65 6d 6f Page cache memo
6daa0 72 79 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 50 61 ry */. int szPa
6dab0 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ge;
6dac0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a /* Siz
6dad0 65 20 6f 66 20 65 61 63 68 20 70 61 67 65 20 69 e of each page i
6dae0 6e 20 70 50 61 67 65 5b 5d 20 2a 2f 0a 20 20 69 n pPage[] */. i
6daf0 6e 74 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20 nt nPage;
6db00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6db10 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 /* Number of pa
6db20 67 65 73 20 69 6e 20 70 50 61 67 65 5b 5d 20 2a ges in pPage[] *
6db30 2f 0a 20 20 69 6e 74 20 6d 78 50 61 72 73 65 72 /. int mxParser
6db40 53 74 61 63 6b 3b 20 20 20 20 20 20 20 20 20 20 Stack;
6db50 20 20 20 20 20 20 2f 2a 20 6d 61 78 69 6d 75 6d /* maximum
6db60 20 64 65 70 74 68 20 6f 66 20 74 68 65 20 70 61 depth of the pa
6db70 72 73 65 72 20 73 74 61 63 6b 20 2a 2f 0a 20 20 rser stack */.
6db80 69 6e 74 20 73 68 61 72 65 64 43 61 63 68 65 45 int sharedCacheE
6db90 6e 61 62 6c 65 64 3b 20 20 20 20 20 20 20 20 20 nabled;
6dba0 20 20 2f 2a 20 74 72 75 65 20 69 66 20 73 68 61 /* true if sha
6dbb0 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 20 65 red-cache mode e
6dbc0 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 2f 2a 20 54 nabled */. /* T
6dbd0 68 65 20 61 62 6f 76 65 20 6d 69 67 68 74 20 62 he above might b
6dbe0 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f e initialized to
6dbf0 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 54 68 65 20 non-zero. The
6dc00 66 6f 6c 6c 6f 77 69 6e 67 20 6e 65 65 64 20 74 following need t
6dc10 6f 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20 69 6e o always. ** in
6dc20 69 74 69 61 6c 6c 79 20 62 65 20 7a 65 72 6f 2c itially be zero,
6dc30 20 68 6f 77 65 76 65 72 2e 20 2a 2f 0a 20 20 69 however. */. i
6dc40 6e 74 20 69 73 49 6e 69 74 3b 20 20 20 20 20 20 nt isInit;
6dc50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6dc60 20 2f 2a 20 54 72 75 65 20 61 66 74 65 72 20 69 /* True after i
6dc70 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 68 61 nitialization ha
6dc80 73 20 66 69 6e 69 73 68 65 64 20 2a 2f 0a 20 20 s finished */.
6dc90 69 6e 74 20 69 6e 50 72 6f 67 72 65 73 73 3b 20 int inProgress;
6dca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6dcb0 20 20 2f 2a 20 54 72 75 65 20 77 68 69 6c 65 20 /* True while
6dcc0 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 69 initialization i
6dcd0 6e 20 70 72 6f 67 72 65 73 73 20 2a 2f 0a 20 20 n progress */.
6dce0 69 6e 74 20 69 73 4d 75 74 65 78 49 6e 69 74 3b int isMutexInit;
6dcf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6dd00 20 20 2f 2a 20 54 72 75 65 20 61 66 74 65 72 20 /* True after
6dd10 6d 75 74 65 78 65 73 20 61 72 65 20 69 6e 69 74 mutexes are init
6dd20 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 ialized */. int
6dd30 20 69 73 4d 61 6c 6c 6f 63 49 6e 69 74 3b 20 20 isMallocInit;
6dd40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
6dd50 2a 20 54 72 75 65 20 61 66 74 65 72 20 6d 61 6c * True after mal
6dd60 6c 6f 63 20 69 73 20 69 6e 69 74 69 61 6c 69 7a loc is initializ
6dd70 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 73 50 43 ed */. int isPC
6dd80 61 63 68 65 49 6e 69 74 3b 20 20 20 20 20 20 20 acheInit;
6dd90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
6dda0 65 20 61 66 74 65 72 20 6d 61 6c 6c 6f 63 20 69 e after malloc i
6ddb0 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f s initialized */
6ddc0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
6ddd0 20 2a 70 49 6e 69 74 4d 75 74 65 78 3b 20 20 20 *pInitMutex;
6dde0 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 75 73 /* Mutex us
6ddf0 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 69 6e ed by sqlite3_in
6de00 69 74 69 61 6c 69 7a 65 28 29 20 2a 2f 0a 20 20 itialize() */.
6de10 69 6e 74 20 6e 52 65 66 49 6e 69 74 4d 75 74 65 int nRefInitMute
6de20 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 x;
6de30 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 /* Number of u
6de40 73 65 72 73 20 6f 66 20 70 49 6e 69 74 4d 75 74 sers of pInitMut
6de50 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 ex */.};../*.**
6de60 43 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 20 Context pointer
6de70 70 61 73 73 65 64 20 64 6f 77 6e 20 74 68 72 6f passed down thro
6de80 75 67 68 20 74 68 65 20 74 72 65 65 2d 77 61 6c ugh the tree-wal
6de90 6b 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 61 6c k..*/.struct Wal
6dea0 6b 65 72 20 7b 0a 20 20 69 6e 74 20 28 2a 78 45 ker {. int (*xE
6deb0 78 70 72 43 61 6c 6c 62 61 63 6b 29 28 57 61 6c xprCallback)(Wal
6dec0 6b 65 72 2a 2c 20 45 78 70 72 2a 29 3b 20 20 20 ker*, Expr*);
6ded0 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 6f /* Callback fo
6dee0 72 20 65 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f r expressions */
6def0 0a 20 20 69 6e 74 20 28 2a 78 53 65 6c 65 63 74 . int (*xSelect
6df00 43 61 6c 6c 62 61 63 6b 29 28 57 61 6c 6b 65 72 Callback)(Walker
6df10 2a 2c 53 65 6c 65 63 74 2a 29 3b 20 20 2f 2a 20 *,Select*); /*
6df20 43 61 6c 6c 62 61 63 6b 20 66 6f 72 20 53 45 4c Callback for SEL
6df30 45 43 54 73 20 2a 2f 0a 20 20 50 61 72 73 65 20 ECTs */. Parse
6df40 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 20 *pParse;
6df50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6df60 20 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f /* Parser co
6df70 6e 74 65 78 74 2e 20 20 2a 2f 0a 20 20 75 6e 69 ntext. */. uni
6df80 6f 6e 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 on {
6df90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6dfa0 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 /* Extra
6dfb0 64 61 74 61 20 66 6f 72 20 63 61 6c 6c 62 61 63 data for callbac
6dfc0 6b 20 2a 2f 0a 20 20 20 20 4e 61 6d 65 43 6f 6e k */. NameCon
6dfd0 74 65 78 74 20 2a 70 4e 43 3b 20 20 20 20 20 20 text *pNC;
6dfe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6dff0 20 20 20 20 2f 2a 20 4e 61 6d 69 6e 67 20 63 6f /* Naming co
6e000 6e 74 65 78 74 20 2a 2f 0a 20 20 20 20 69 6e 74 ntext */. int
6e010 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
6e020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6e030 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 /* Integ
6e040 65 72 20 76 61 6c 75 65 20 2a 2f 0a 20 20 7d 20 er value */. }
6e050 75 3b 0a 7d 3b 0a 0a 2f 2a 20 46 6f 72 77 61 72 u;.};../* Forwar
6e060 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 2a d declarations *
6e070 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
6e080 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b int sqlite3Walk
6e090 45 78 70 72 28 57 61 6c 6b 65 72 2a 2c 20 45 78 Expr(Walker*, Ex
6e0a0 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 pr*);.SQLITE_PRI
6e0b0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
6e0c0 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 57 61 6c WalkExprList(Wal
6e0d0 6b 65 72 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29 ker*, ExprList*)
6e0e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6e0f0 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b int sqlite3Walk
6e100 53 65 6c 65 63 74 28 57 61 6c 6b 65 72 2a 2c 20 Select(Walker*,
6e110 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 Select*);.SQLITE
6e120 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
6e130 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 45 78 ite3WalkSelectEx
6e140 70 72 28 57 61 6c 6b 65 72 2a 2c 20 53 65 6c 65 pr(Walker*, Sele
6e150 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ct*);.SQLITE_PRI
6e160 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
6e170 57 61 6c 6b 53 65 6c 65 63 74 46 72 6f 6d 28 57 WalkSelectFrom(W
6e180 61 6c 6b 65 72 2a 2c 20 53 65 6c 65 63 74 2a 29 alker*, Select*)
6e190 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 ;../*.** Return
6e1a0 63 6f 64 65 20 66 72 6f 6d 20 74 68 65 20 70 61 code from the pa
6e1b0 72 73 65 2d 74 72 65 65 20 77 61 6c 6b 69 6e 67 rse-tree walking
6e1c0 20 70 72 69 6d 69 74 69 76 65 73 20 61 6e 64 20 primitives and
6e1d0 74 68 65 69 72 0a 2a 2a 20 63 61 6c 6c 62 61 63 their.** callbac
6e1e0 6b 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 57 ks..*/.#define W
6e1f0 52 43 5f 43 6f 6e 74 69 6e 75 65 20 20 20 20 30 RC_Continue 0
6e200 20 20 20 2f 2a 20 43 6f 6e 74 69 6e 75 65 20 64 /* Continue d
6e210 6f 77 6e 20 69 6e 74 6f 20 63 68 69 6c 64 72 65 own into childre
6e220 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 52 43 n */.#define WRC
6e230 5f 50 72 75 6e 65 20 20 20 20 20 20 20 31 20 20 _Prune 1
6e240 20 2f 2a 20 4f 6d 69 74 20 63 68 69 6c 64 72 65 /* Omit childre
6e250 6e 20 62 75 74 20 63 6f 6e 74 69 6e 75 65 20 77 n but continue w
6e260 61 6c 6b 69 6e 67 20 73 69 62 6c 69 6e 67 73 20 alking siblings
6e270 2a 2f 0a 23 64 65 66 69 6e 65 20 57 52 43 5f 41 */.#define WRC_A
6e280 62 6f 72 74 20 20 20 20 20 20 20 32 20 20 20 2f bort 2 /
6e290 2a 20 41 62 61 6e 64 6f 6e 20 74 68 65 20 74 72 * Abandon the tr
6e2a0 65 65 20 77 61 6c 6b 20 2a 2f 0a 0a 2f 2a 0a 2a ee walk */../*.*
6e2b0 2a 20 41 73 73 75 6d 69 6e 67 20 7a 49 6e 20 70 * Assuming zIn p
6e2c0 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 66 69 72 oints to the fir
6e2d0 73 74 20 62 79 74 65 20 6f 66 20 61 20 55 54 46 st byte of a UTF
6e2e0 2d 38 20 63 68 61 72 61 63 74 65 72 2c 0a 2a 2a -8 character,.**
6e2f0 20 61 64 76 61 6e 63 65 20 7a 49 6e 20 74 6f 20 advance zIn to
6e300 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 66 69 72 point to the fir
6e310 73 74 20 62 79 74 65 20 6f 66 20 74 68 65 20 6e st byte of the n
6e320 65 78 74 20 55 54 46 2d 38 20 63 68 61 72 61 63 ext UTF-8 charac
6e330 74 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ter..*/.#define
6e340 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 SQLITE_SKIP_UTF8
6e350 28 7a 49 6e 29 20 7b 20 20 20 20 20 20 20 20 20 (zIn) {
6e360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
6e370 0a 20 20 69 66 28 20 28 2a 28 7a 49 6e 2b 2b 29 . if( (*(zIn++)
6e380 29 3e 3d 30 78 63 30 20 29 7b 20 20 20 20 20 20 )>=0xc0 ){
6e390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6e3a0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 77 68 \. wh
6e3b0 69 6c 65 28 20 28 2a 7a 49 6e 20 26 20 30 78 63 ile( (*zIn & 0xc
6e3c0 30 29 3d 3d 30 78 38 30 20 29 7b 20 7a 49 6e 2b 0)==0x80 ){ zIn+
6e3d0 2b 3b 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 +; }
6e3e0 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20 \. }
6e3f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6e400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6e410 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 2f \.}../
6e420 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f *.** The SQLITE_
6e430 43 4f 52 52 55 50 54 5f 42 4b 50 54 20 6d 61 63 CORRUPT_BKPT mac
6e440 72 6f 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 ro can be either
6e450 20 61 20 63 6f 6e 73 74 61 6e 74 20 28 66 6f 72 a constant (for
6e460 20 70 72 6f 64 75 63 74 69 6f 6e 0a 2a 2a 20 62 production.** b
6e470 75 69 6c 64 73 29 20 6f 72 20 61 20 66 75 6e 63 uilds) or a func
6e480 74 69 6f 6e 20 63 61 6c 6c 20 28 66 6f 72 20 64 tion call (for d
6e490 65 62 75 67 67 69 6e 67 29 2e 20 20 49 66 20 69 ebugging). If i
6e4a0 74 20 69 73 20 61 20 66 75 6e 63 74 69 6f 6e 20 t is a function
6e4b0 63 61 6c 6c 2c 0a 2a 2a 20 69 74 20 61 6c 6c 6f call,.** it allo
6e4c0 77 73 20 74 68 65 20 6f 70 65 72 61 74 6f 72 20 ws the operator
6e4d0 74 6f 20 73 65 74 20 61 20 62 72 65 61 6b 70 6f to set a breakpo
6e4e0 69 6e 74 20 61 74 20 74 68 65 20 73 70 6f 74 20 int at the spot
6e4f0 77 68 65 72 65 20 64 61 74 61 62 61 73 65 0a 2a where database.*
6e500 2a 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 20 * corruption is
6e510 66 69 72 73 74 20 64 65 74 65 63 74 65 64 2e 0a first detected..
6e520 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
6e530 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 _DEBUG.SQLITE_PR
6e540 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 IVATE int sqli
6e550 74 65 33 43 6f 72 72 75 70 74 28 76 6f 69 64 29 te3Corrupt(void)
6e560 3b 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 ;.# define SQLIT
6e570 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 20 73 E_CORRUPT_BKPT s
6e580 71 6c 69 74 65 33 43 6f 72 72 75 70 74 28 29 0a qlite3Corrupt().
6e590 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 #else.# define S
6e5a0 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
6e5b0 50 54 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 PT SQLITE_CORRUP
6e5c0 54 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 T.#endif../*.**
6e5d0 54 68 65 20 63 74 79 70 65 2e 68 20 68 65 61 64 The ctype.h head
6e5e0 65 72 20 69 73 20 6e 65 65 64 65 64 20 66 6f 72 er is needed for
6e5f0 20 6e 6f 6e 2d 41 53 43 49 49 20 73 79 73 74 65 non-ASCII syste
6e600 6d 73 2e 20 20 49 74 20 69 73 20 61 6c 73 6f 0a ms. It is also.
6e610 2a 2a 20 6e 65 65 64 65 64 20 62 79 20 46 54 53 ** needed by FTS
6e620 33 20 77 68 65 6e 20 46 54 53 33 20 69 73 20 69 3 when FTS3 is i
6e630 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 65 20 61 ncluded in the a
6e640 6d 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a malgamation..*/.
6e650 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c #if !defined(SQL
6e660 49 54 45 5f 41 53 43 49 49 29 20 7c 7c 20 5c 0a ITE_ASCII) || \.
6e670 20 20 20 20 28 64 65 66 69 6e 65 64 28 53 51 4c (defined(SQL
6e680 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 ITE_ENABLE_FTS3)
6e690 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 && defined(SQLI
6e6a0 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 29 TE_AMALGAMATION)
6e6b0 29 0a 23 20 69 6e 63 6c 75 64 65 20 3c 63 74 79 ).# include <cty
6e6c0 70 65 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a 2f 2a pe.h>.#endif../*
6e6d0 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
6e6e0 67 20 6d 61 63 72 6f 73 20 6d 69 6d 69 63 20 74 g macros mimic t
6e6f0 68 65 20 73 74 61 6e 64 61 72 64 20 6c 69 62 72 he standard libr
6e700 61 72 79 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f ary functions to
6e710 75 70 70 65 72 28 29 2c 0a 2a 2a 20 69 73 73 70 upper(),.** issp
6e720 61 63 65 28 29 2c 20 69 73 61 6c 6e 75 6d 28 29 ace(), isalnum()
6e730 2c 20 69 73 64 69 67 69 74 28 29 20 61 6e 64 20 , isdigit() and
6e740 69 73 78 64 69 67 69 74 28 29 2c 20 72 65 73 70 isxdigit(), resp
6e750 65 63 74 69 76 65 6c 79 2e 20 54 68 65 0a 2a 2a ectively. The.**
6e760 20 73 71 6c 69 74 65 20 76 65 72 73 69 6f 6e 73 sqlite versions
6e770 20 6f 6e 6c 79 20 77 6f 72 6b 20 66 6f 72 20 41 only work for A
6e780 53 43 49 49 20 63 68 61 72 61 63 74 65 72 73 2c SCII characters,
6e790 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 6c regardless of l
6e7a0 6f 63 61 6c 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 ocale..*/.#ifdef
6e7b0 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 23 20 SQLITE_ASCII.#
6e7c0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 6f define sqlite3To
6e7d0 75 70 70 65 72 28 78 29 20 20 28 28 78 29 26 7e upper(x) ((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 32 30 29 29 0a 23 20 64 65 (x)]&0x20)).# de
6e810 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 73 70 fine sqlite3Issp
6e820 61 63 65 28 78 29 20 20 20 28 73 71 6c 69 74 65 ace(x) (sqlite
6e830 33 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 3CtypeMap[(unsig
6e840 6e 65 64 20 63 68 61 72 29 28 78 29 5d 26 30 78 ned char)(x)]&0x
6e850 30 31 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 01).# define sql
6e860 69 74 65 33 49 73 61 6c 6e 75 6d 28 78 29 20 20 ite3Isalnum(x)
6e870 20 28 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 (sqlite3CtypeMa
6e880 70 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 p[(unsigned char
6e890 29 28 78 29 5d 26 30 78 30 36 29 0a 23 20 64 65 )(x)]&0x06).# de
6e8a0 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 61 6c fine sqlite3Isal
6e8b0 70 68 61 28 78 29 20 20 20 28 73 71 6c 69 74 65 pha(x) (sqlite
6e8c0 33 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 3CtypeMap[(unsig
6e8d0 6e 65 64 20 63 68 61 72 29 28 78 29 5d 26 30 78 ned char)(x)]&0x
6e8e0 30 32 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 02).# define sql
6e8f0 69 74 65 33 49 73 64 69 67 69 74 28 78 29 20 20 ite3Isdigit(x)
6e900 20 28 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 (sqlite3CtypeMa
6e910 70 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 p[(unsigned char
6e920 29 28 78 29 5d 26 30 78 30 34 29 0a 23 20 64 65 )(x)]&0x04).# de
6e930 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 78 64 fine sqlite3Isxd
6e940 69 67 69 74 28 78 29 20 20 28 73 71 6c 69 74 65 igit(x) (sqlite
6e950 33 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 3CtypeMap[(unsig
6e960 6e 65 64 20 63 68 61 72 29 28 78 29 5d 26 30 78 ned char)(x)]&0x
6e970 30 38 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 08).# define sql
6e980 69 74 65 33 54 6f 6c 6f 77 65 72 28 78 29 20 20 ite3Tolower(x)
6e990 20 28 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f (sqlite3UpperTo
6e9a0 4c 6f 77 65 72 5b 28 75 6e 73 69 67 6e 65 64 20 Lower[(unsigned
6e9b0 63 68 61 72 29 28 78 29 5d 29 0a 23 65 6c 73 65 char)(x)]).#else
6e9c0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
6e9d0 33 54 6f 75 70 70 65 72 28 78 29 20 20 20 74 6f 3Toupper(x) to
6e9e0 75 70 70 65 72 28 28 75 6e 73 69 67 6e 65 64 20 upper((unsigned
6e9f0 63 68 61 72 29 28 78 29 29 0a 23 20 64 65 66 69 char)(x)).# defi
6ea00 6e 65 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 ne sqlite3Isspac
6ea10 65 28 78 29 20 20 20 69 73 73 70 61 63 65 28 28 e(x) isspace((
6ea20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 unsigned char)(x
6ea30 29 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 )).# define sqli
6ea40 74 65 33 49 73 61 6c 6e 75 6d 28 78 29 20 20 20 te3Isalnum(x)
6ea50 69 73 61 6c 6e 75 6d 28 28 75 6e 73 69 67 6e 65 isalnum((unsigne
6ea60 64 20 63 68 61 72 29 28 78 29 29 0a 23 20 64 65 d char)(x)).# de
6ea70 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 61 6c fine sqlite3Isal
6ea80 70 68 61 28 78 29 20 20 20 69 73 61 6c 70 68 61 pha(x) isalpha
6ea90 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 ((unsigned char)
6eaa0 28 78 29 29 0a 23 20 64 65 66 69 6e 65 20 73 71 (x)).# define sq
6eab0 6c 69 74 65 33 49 73 64 69 67 69 74 28 78 29 20 lite3Isdigit(x)
6eac0 20 20 69 73 64 69 67 69 74 28 28 75 6e 73 69 67 isdigit((unsig
6ead0 6e 65 64 20 63 68 61 72 29 28 78 29 29 0a 23 20 ned char)(x)).#
6eae0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 define sqlite3Is
6eaf0 78 64 69 67 69 74 28 78 29 20 20 69 73 78 64 69 xdigit(x) isxdi
6eb00 67 69 74 28 28 75 6e 73 69 67 6e 65 64 20 63 68 git((unsigned ch
6eb10 61 72 29 28 78 29 29 0a 23 20 64 65 66 69 6e 65 ar)(x)).# define
6eb20 20 73 71 6c 69 74 65 33 54 6f 6c 6f 77 65 72 28 sqlite3Tolower(
6eb30 78 29 20 20 20 74 6f 6c 6f 77 65 72 28 28 75 6e x) tolower((un
6eb40 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 29 signed char)(x))
6eb50 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 .#endif../*.** I
6eb60 6e 74 65 72 6e 61 6c 20 66 75 6e 63 74 69 6f 6e nternal function
6eb70 20 70 72 6f 74 6f 74 79 70 65 73 0a 2a 2f 0a 53 prototypes.*/.S
6eb80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
6eb90 74 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 t sqlite3StrICmp
6eba0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 63 (const char *, c
6ebb0 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 onst char *);.SQ
6ebc0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
6ebd0 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 sqlite3IsNumber
6ebe0 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e (const char*, in
6ebf0 74 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45 5f t*, u8);.SQLITE_
6ec00 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6ec10 74 65 33 53 74 72 6c 65 6e 33 30 28 63 6f 6e 73 te3Strlen30(cons
6ec20 74 20 63 68 61 72 2a 29 3b 0a 23 64 65 66 69 6e t char*);.#defin
6ec30 65 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d e sqlite3StrNICm
6ec40 70 20 73 71 6c 69 74 65 33 5f 73 74 72 6e 69 63 p sqlite3_strnic
6ec50 6d 70 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 mp..SQLITE_PRIVA
6ec60 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 61 TE int sqlite3Ma
6ec70 6c 6c 6f 63 49 6e 69 74 28 76 6f 69 64 29 3b 0a llocInit(void);.
6ec80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6ec90 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f oid sqlite3Mallo
6eca0 63 45 6e 64 28 76 6f 69 64 29 3b 0a 53 51 4c 49 cEnd(void);.SQLI
6ecb0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6ecc0 2a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 69 *sqlite3Malloc(i
6ecd0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
6ece0 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 ATE void *sqlite
6ecf0 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 69 6e 74 29 3MallocZero(int)
6ed00 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6ed10 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 void *sqlite3Db
6ed20 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 71 6c 69 74 MallocZero(sqlit
6ed30 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 e3*, int);.SQLIT
6ed40 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a E_PRIVATE void *
6ed50 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 sqlite3DbMallocR
6ed60 61 77 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 aw(sqlite3*, int
6ed70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6ed80 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 44 E char *sqlite3D
6ed90 62 53 74 72 44 75 70 28 73 71 6c 69 74 65 33 2a bStrDup(sqlite3*
6eda0 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 ,const char*);.S
6edb0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 QLITE_PRIVATE ch
6edc0 61 72 20 2a 73 71 6c 69 74 65 33 44 62 53 74 72 ar *sqlite3DbStr
6edd0 4e 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 63 6f NDup(sqlite3*,co
6ede0 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 29 3b nst char*, int);
6edf0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6ee00 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 52 65 61 void *sqlite3Rea
6ee10 6c 6c 6f 63 28 76 6f 69 64 2a 2c 20 69 6e 74 29 lloc(void*, int)
6ee20 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6ee30 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 void *sqlite3Db
6ee40 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 73 71 ReallocOrFree(sq
6ee50 6c 69 74 65 33 20 2a 2c 20 76 6f 69 64 20 2a 2c lite3 *, void *,
6ee60 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
6ee70 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
6ee80 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 73 71 6c te3DbRealloc(sql
6ee90 69 74 65 33 20 2a 2c 20 76 6f 69 64 20 2a 2c 20 ite3 *, void *,
6eea0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
6eeb0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6eec0 33 44 62 46 72 65 65 28 73 71 6c 69 74 65 33 2a 3DbFree(sqlite3*
6eed0 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 , void*);.SQLITE
6eee0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
6eef0 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 76 ite3MallocSize(v
6ef00 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 oid*);.SQLITE_PR
6ef10 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
6ef20 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 73 71 3DbMallocSize(sq
6ef30 6c 69 74 65 33 2a 2c 20 76 6f 69 64 2a 29 3b 0a lite3*, void*);.
6ef40 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6ef50 6f 69 64 20 2a 73 71 6c 69 74 65 33 53 63 72 61 oid *sqlite3Scra
6ef60 74 63 68 4d 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a tchMalloc(int);.
6ef70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6ef80 6f 69 64 20 73 71 6c 69 74 65 33 53 63 72 61 74 oid sqlite3Scrat
6ef90 63 68 46 72 65 65 28 76 6f 69 64 2a 29 3b 0a 53 chFree(void*);.S
6efa0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6efb0 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 4d id *sqlite3PageM
6efc0 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 alloc(int);.SQLI
6efd0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6efe0 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 sqlite3PageFree(
6eff0 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 void*);.SQLITE_P
6f000 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6f010 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 te3MemSetDefault
6f020 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 (void);.SQLITE_P
6f030 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6f040 74 65 33 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 te3BenignMallocH
6f050 6f 6f 6b 73 28 76 6f 69 64 20 28 2a 29 28 76 6f ooks(void (*)(vo
6f060 69 64 29 2c 20 76 6f 69 64 20 28 2a 29 28 76 6f id), void (*)(vo
6f070 69 64 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 id));.SQLITE_PRI
6f080 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
6f090 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 76 6f 69 64 MemoryAlarm(void
6f0a0 20 28 2a 29 28 76 6f 69 64 2a 2c 20 73 71 6c 69 (*)(void*, sqli
6f0b0 74 65 33 5f 69 6e 74 36 34 2c 20 69 6e 74 29 2c te3_int64, int),
6f0c0 20 76 6f 69 64 2a 2c 20 73 71 6c 69 74 65 33 5f void*, sqlite3_
6f0d0 69 6e 74 36 34 29 3b 0a 0a 2f 2a 0a 2a 2a 20 4f int64);../*.** O
6f0e0 6e 20 73 79 73 74 65 6d 73 20 77 69 74 68 20 61 n systems with a
6f0f0 6d 70 6c 65 20 73 74 61 63 6b 20 73 70 61 63 65 mple stack space
6f100 20 61 6e 64 20 74 68 61 74 20 73 75 70 70 6f 72 and that suppor
6f110 74 20 61 6c 6c 6f 63 61 28 29 2c 20 6d 61 6b 65 t alloca(), make
6f120 0a 2a 2a 20 75 73 65 20 6f 66 20 61 6c 6c 6f 63 .** use of alloc
6f130 61 28 29 20 74 6f 20 6f 62 74 61 69 6e 20 73 70 a() to obtain sp
6f140 61 63 65 20 66 6f 72 20 6c 61 72 67 65 20 61 75 ace for large au
6f150 74 6f 6d 61 74 69 63 20 6f 62 6a 65 63 74 73 2e tomatic objects.
6f160 20 20 42 79 20 64 65 66 61 75 6c 74 2c 0a 2a 2a By default,.**
6f170 20 6f 62 74 61 69 6e 20 73 70 61 63 65 20 66 72 obtain space fr
6f180 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a om malloc()..**.
6f190 2a 2a 20 54 68 65 20 61 6c 6c 6f 63 61 28 29 20 ** The alloca()
6f1a0 72 6f 75 74 69 6e 65 20 6e 65 76 65 72 20 72 65 routine never re
6f1b0 74 75 72 6e 73 20 4e 55 4c 4c 2e 20 20 54 68 69 turns NULL. Thi
6f1c0 73 20 77 69 6c 6c 20 63 61 75 73 65 20 63 6f 64 s will cause cod
6f1d0 65 20 70 61 74 68 73 0a 2a 2a 20 74 68 61 74 20 e paths.** that
6f1e0 64 65 61 6c 20 77 69 74 68 20 73 71 6c 69 74 65 deal with sqlite
6f1f0 33 53 74 61 63 6b 41 6c 6c 6f 63 28 29 20 66 61 3StackAlloc() fa
6f200 69 6c 75 72 65 73 20 74 6f 20 62 65 20 75 6e 72 ilures to be unr
6f210 65 61 63 68 61 62 6c 65 2e 0a 2a 2f 0a 23 69 66 eachable..*/.#if
6f220 64 65 66 20 53 51 4c 49 54 45 5f 55 53 45 5f 41 def SQLITE_USE_A
6f230 4c 4c 4f 43 41 0a 23 20 64 65 66 69 6e 65 20 73 LLOCA.# define s
6f240 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 qlite3StackAlloc
6f250 52 61 77 28 44 2c 4e 29 20 20 20 61 6c 6c 6f 63 Raw(D,N) alloc
6f260 61 28 4e 29 0a 23 20 64 65 66 69 6e 65 20 73 71 a(N).# define sq
6f270 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 5a lite3StackAllocZ
6f280 65 72 6f 28 44 2c 4e 29 20 20 6d 65 6d 73 65 74 ero(D,N) memset
6f290 28 61 6c 6c 6f 63 61 28 4e 29 2c 20 30 2c 20 4e (alloca(N), 0, N
6f2a0 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 ).# define sqlit
6f2b0 65 33 53 74 61 63 6b 46 72 65 65 28 44 2c 50 29 e3StackFree(D,P)
6f2c0 20 20 20 20 20 20 20 0a 23 65 6c 73 65 0a 23 20 .#else.#
6f2d0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 74 define sqlite3St
6f2e0 61 63 6b 41 6c 6c 6f 63 52 61 77 28 44 2c 4e 29 ackAllocRaw(D,N)
6f2f0 20 20 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c sqlite3DbMall
6f300 6f 63 52 61 77 28 44 2c 4e 29 0a 23 20 64 65 66 ocRaw(D,N).# def
6f310 69 6e 65 20 73 71 6c 69 74 65 33 53 74 61 63 6b ine sqlite3Stack
6f320 41 6c 6c 6f 63 5a 65 72 6f 28 44 2c 4e 29 20 20 AllocZero(D,N)
6f330 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a sqlite3DbMallocZ
6f340 65 72 6f 28 44 2c 4e 29 0a 23 20 64 65 66 69 6e ero(D,N).# defin
6f350 65 20 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 e sqlite3StackFr
6f360 65 65 28 44 2c 50 29 20 20 20 20 20 20 20 73 71 ee(D,P) sq
6f370 6c 69 74 65 33 44 62 46 72 65 65 28 44 2c 50 29 lite3DbFree(D,P)
6f380 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 .#endif..#ifdef
6f390 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 SQLITE_ENABLE_ME
6f3a0 4d 53 59 53 33 0a 53 51 4c 49 54 45 5f 50 52 49 MSYS3.SQLITE_PRI
6f3b0 56 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 VATE const sqlit
6f3c0 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 2a e3_mem_methods *
6f3d0 73 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d sqlite3MemGetMem
6f3e0 73 79 73 33 28 76 6f 69 64 29 3b 0a 23 65 6e 64 sys3(void);.#end
6f3f0 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 if.#ifdef SQLITE
6f400 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 0a _ENABLE_MEMSYS5.
6f410 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
6f420 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d onst sqlite3_mem
6f430 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 _methods *sqlite
6f440 33 4d 65 6d 47 65 74 4d 65 6d 73 79 73 35 28 76 3MemGetMemsys5(v
6f450 6f 69 64 29 3b 0a 23 65 6e 64 69 66 0a 0a 0a 23 oid);.#endif...#
6f460 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 ifndef SQLITE_MU
6f470 54 45 58 5f 4f 4d 49 54 0a 53 51 4c 49 54 45 5f TEX_OMIT.SQLITE_
6f480 50 52 49 56 41 54 45 20 20 20 73 71 6c 69 74 65 PRIVATE sqlite
6f490 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 3_mutex_methods
6f4a0 2a 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d *sqlite3DefaultM
6f4b0 75 74 65 78 28 76 6f 69 64 29 3b 0a 53 51 4c 49 utex(void);.SQLI
6f4c0 54 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c TE_PRIVATE sql
6f4d0 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 ite3_mutex *sqli
6f4e0 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e te3MutexAlloc(in
6f4f0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
6f500 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 TE int sqlite3
6f510 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29 3b MutexInit(void);
6f520 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6f530 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 75 74 int sqlite3Mut
6f540 65 78 45 6e 64 28 76 6f 69 64 29 3b 0a 23 65 6e exEnd(void);.#en
6f550 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 dif..SQLITE_PRIV
6f560 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 ATE int sqlite3S
6f570 74 61 74 75 73 56 61 6c 75 65 28 69 6e 74 29 3b tatusValue(int);
6f580 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6f590 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 74 void sqlite3Stat
6f5a0 75 73 41 64 64 28 69 6e 74 2c 20 69 6e 74 29 3b usAdd(int, int);
6f5b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6f5c0 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 74 void sqlite3Stat
6f5d0 75 73 53 65 74 28 69 6e 74 2c 20 69 6e 74 29 3b usSet(int, int);
6f5e0 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ..SQLITE_PRIVATE
6f5f0 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4e 61 int sqlite3IsNa
6f600 4e 28 64 6f 75 62 6c 65 29 3b 0a 0a 53 51 4c 49 N(double);..SQLI
6f610 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6f620 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 sqlite3VXPrintf(
6f630 53 74 72 41 63 63 75 6d 2a 2c 20 69 6e 74 2c 20 StrAccum*, int,
6f640 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 76 61 5f const char*, va_
6f650 6c 69 73 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 list);.SQLITE_PR
6f660 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 IVATE char *sqli
6f670 74 65 33 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 te3MPrintf(sqlit
6f680 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c e3*,const char*,
6f690 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ...);.SQLITE_PR
6f6a0 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 IVATE char *sqli
6f6b0 74 65 33 56 4d 50 72 69 6e 74 66 28 73 71 6c 69 te3VMPrintf(sqli
6f6c0 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a te3*,const char*
6f6d0 2c 20 76 61 5f 6c 69 73 74 29 3b 0a 53 51 4c 49 , va_list);.SQLI
6f6e0 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 TE_PRIVATE char
6f6f0 2a 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 *sqlite3MAppendf
6f700 28 73 71 6c 69 74 65 33 2a 2c 63 68 61 72 2a 2c (sqlite3*,char*,
6f710 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 const char*,...)
6f720 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 ;.#if defined(SQ
6f730 4c 49 54 45 5f 54 45 53 54 29 20 7c 7c 20 64 65 LITE_TEST) || de
6f740 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 fined(SQLITE_DEB
6f750 55 47 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 UG).SQLITE_PRIVA
6f760 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
6f770 33 44 65 62 75 67 50 72 69 6e 74 66 28 63 6f 6e 3DebugPrintf(con
6f780 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a st char*, ...);.
6f790 23 65 6e 64 69 66 0a 23 69 66 20 64 65 66 69 6e #endif.#if defin
6f7a0 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a ed(SQLITE_TEST).
6f7b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
6f7c0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 54 65 void *sqlite3Te
6f7d0 73 74 54 65 78 74 54 6f 50 74 72 28 63 6f 6e 73 stTextToPtr(cons
6f7e0 74 20 63 68 61 72 2a 29 3b 0a 23 65 6e 64 69 66 t char*);.#endif
6f7f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6f800 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 74 53 void sqlite3SetS
6f810 74 72 69 6e 67 28 63 68 61 72 20 2a 2a 2c 20 73 tring(char **, s
6f820 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 qlite3*, const c
6f830 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 har*, ...);.SQLI
6f840 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6f850 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
6f860 50 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68 Parse*, const ch
6f870 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 ar*, ...);.SQLIT
6f880 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6f890 71 6c 69 74 65 33 45 72 72 6f 72 43 6c 65 61 72 qlite3ErrorClear
6f8a0 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 (Parse*);.SQLITE
6f8b0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
6f8c0 69 74 65 33 44 65 71 75 6f 74 65 28 63 68 61 72 ite3Dequote(char
6f8d0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6f8e0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4b 65 TE int sqlite3Ke
6f8f0 79 77 6f 72 64 43 6f 64 65 28 63 6f 6e 73 74 20 ywordCode(const
6f900 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 2c 20 unsigned char*,
6f910 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
6f920 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
6f930 52 75 6e 50 61 72 73 65 72 28 50 61 72 73 65 2a RunParser(Parse*
6f940 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 , const char*, c
6f950 68 61 72 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f har **);.SQLITE_
6f960 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6f970 69 74 65 33 46 69 6e 69 73 68 43 6f 64 69 6e 67 ite3FinishCoding
6f980 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 (Parse*);.SQLITE
6f990 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
6f9a0 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 50 ite3GetTempReg(P
6f9b0 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 arse*);.SQLITE_P
6f9c0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6f9d0 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 te3ReleaseTempRe
6f9e0 67 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b 0a 53 g(Parse*,int);.S
6f9f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
6fa00 74 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 t sqlite3GetTemp
6fa10 52 61 6e 67 65 28 50 61 72 73 65 2a 2c 69 6e 74 Range(Parse*,int
6fa20 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6fa30 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 E void sqlite3Re
6fa40 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 50 leaseTempRange(P
6fa50 61 72 73 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a arse*,int,int);.
6fa60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 SQLITE_PRIVATE E
6fa70 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 xpr *sqlite3Expr
6fa80 41 6c 6c 6f 63 28 73 71 6c 69 74 65 33 2a 2c 69 Alloc(sqlite3*,i
6fa90 6e 74 2c 63 6f 6e 73 74 20 54 6f 6b 65 6e 2a 2c nt,const Token*,
6faa0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
6fab0 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 VATE Expr *sqlit
6fac0 65 33 45 78 70 72 28 73 71 6c 69 74 65 33 2a 2c e3Expr(sqlite3*,
6fad0 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 int,const char*)
6fae0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6faf0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
6fb00 72 41 74 74 61 63 68 53 75 62 74 72 65 65 73 28 rAttachSubtrees(
6fb10 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c 45 sqlite3*,Expr*,E
6fb20 78 70 72 2a 2c 45 78 70 72 2a 29 3b 0a 53 51 4c xpr*,Expr*);.SQL
6fb30 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 ITE_PRIVATE Expr
6fb40 20 2a 73 71 6c 69 74 65 33 50 45 78 70 72 28 50 *sqlite3PExpr(P
6fb50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 45 78 70 72 arse*, int, Expr
6fb60 2a 2c 20 45 78 70 72 2a 2c 20 63 6f 6e 73 74 20 *, Expr*, const
6fb70 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f Token*);.SQLITE_
6fb80 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 PRIVATE Expr *sq
6fb90 6c 69 74 65 33 45 78 70 72 41 6e 64 28 73 71 6c lite3ExprAnd(sql
6fba0 69 74 65 33 2a 2c 45 78 70 72 2a 2c 20 45 78 70 ite3*,Expr*, Exp
6fbb0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
6fbc0 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 ATE Expr *sqlite
6fbd0 33 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 50 61 3ExprFunction(Pa
6fbe0 72 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 20 rse*,ExprList*,
6fbf0 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f Token*);.SQLITE_
6fc00 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6fc10 69 74 65 33 45 78 70 72 41 73 73 69 67 6e 56 61 ite3ExprAssignVa
6fc20 72 4e 75 6d 62 65 72 28 50 61 72 73 65 2a 2c 20 rNumber(Parse*,
6fc30 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Expr*);.SQLITE_P
6fc40 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6fc50 74 65 33 45 78 70 72 43 6c 65 61 72 28 73 71 6c te3ExprClear(sql
6fc60 69 74 65 33 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 ite3*, Expr*);.S
6fc70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6fc80 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 id sqlite3ExprDe
6fc90 6c 65 74 65 28 73 71 6c 69 74 65 33 2a 2c 20 45 lete(sqlite3*, E
6fca0 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 xpr*);.SQLITE_PR
6fcb0 49 56 41 54 45 20 45 78 70 72 4c 69 73 74 20 2a IVATE ExprList *
6fcc0 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 sqlite3ExprListA
6fcd0 70 70 65 6e 64 28 50 61 72 73 65 2a 2c 45 78 70 ppend(Parse*,Exp
6fce0 72 4c 69 73 74 2a 2c 45 78 70 72 2a 29 3b 0a 53 rList*,Expr*);.S
6fcf0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6fd00 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 id sqlite3ExprLi
6fd10 73 74 53 65 74 4e 61 6d 65 28 50 61 72 73 65 2a stSetName(Parse*
6fd20 2c 45 78 70 72 4c 69 73 74 2a 2c 54 6f 6b 65 6e ,ExprList*,Token
6fd30 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 *,int);.SQLITE_P
6fd40 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6fd50 74 65 33 45 78 70 72 4c 69 73 74 53 65 74 53 70 te3ExprListSetSp
6fd60 61 6e 28 50 61 72 73 65 2a 2c 45 78 70 72 4c 69 an(Parse*,ExprLi
6fd70 73 74 2a 2c 45 78 70 72 53 70 61 6e 2a 29 3b 0a st*,ExprSpan*);.
6fd80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6fd90 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 4c oid sqlite3ExprL
6fda0 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 istDelete(sqlite
6fdb0 33 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b 0a 3*, ExprList*);.
6fdc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
6fdd0 6e 74 20 73 71 6c 69 74 65 33 49 6e 69 74 28 73 nt sqlite3Init(s
6fde0 71 6c 69 74 65 33 2a 2c 20 63 68 61 72 2a 2a 29 qlite3*, char**)
6fdf0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6fe00 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 69 74 int sqlite3Init
6fe10 43 61 6c 6c 62 61 63 6b 28 76 6f 69 64 2a 2c 20 Callback(void*,
6fe20 69 6e 74 2c 20 63 68 61 72 2a 2a 2c 20 63 68 61 int, char**, cha
6fe30 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 r**);.SQLITE_PRI
6fe40 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6fe50 33 50 72 61 67 6d 61 28 50 61 72 73 65 2a 2c 54 3Pragma(Parse*,T
6fe60 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b oken*,Token*,Tok
6fe70 65 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 en*,int);.SQLITE
6fe80 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6fe90 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e lite3ResetIntern
6fea0 61 6c 53 63 68 65 6d 61 28 73 71 6c 69 74 65 33 alSchema(sqlite3
6feb0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
6fec0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6fed0 69 74 65 33 42 65 67 69 6e 50 61 72 73 65 28 50 ite3BeginParse(P
6fee0 61 72 73 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 arse*,int);.SQLI
6fef0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6ff00 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 49 6e 74 sqlite3CommitInt
6ff10 65 72 6e 61 6c 43 68 61 6e 67 65 73 28 73 71 6c ernalChanges(sql
6ff20 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ite3*);.SQLITE_P
6ff30 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 RIVATE Table *sq
6ff40 6c 69 74 65 33 52 65 73 75 6c 74 53 65 74 4f 66 lite3ResultSetOf
6ff50 53 65 6c 65 63 74 28 50 61 72 73 65 2a 2c 53 65 Select(Parse*,Se
6ff60 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 lect*);.SQLITE_P
6ff70 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6ff80 74 65 33 4f 70 65 6e 4d 61 73 74 65 72 54 61 62 te3OpenMasterTab
6ff90 6c 65 28 50 61 72 73 65 20 2a 2c 20 69 6e 74 29 le(Parse *, int)
6ffa0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6ffb0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 void sqlite3Sta
6ffc0 72 74 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 54 rtTable(Parse*,T
6ffd0 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e 74 oken*,Token*,int
6ffe0 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 ,int,int,int);.S
6fff0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
70000 69 64 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c id sqlite3AddCol
70010 75 6d 6e 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e umn(Parse*,Token
70020 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
70030 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 TE void sqlite3A
70040 64 64 4e 6f 74 4e 75 6c 6c 28 50 61 72 73 65 2a ddNotNull(Parse*
70050 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
70060 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
70070 74 65 33 41 64 64 50 72 69 6d 61 72 79 4b 65 79 te3AddPrimaryKey
70080 28 50 61 72 73 65 2a 2c 20 45 78 70 72 4c 69 73 (Parse*, ExprLis
70090 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e t*, int, int, in
700a0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
700b0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 TE void sqlite3A
700c0 64 64 43 68 65 63 6b 43 6f 6e 73 74 72 61 69 6e ddCheckConstrain
700d0 74 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 t(Parse*, Expr*)
700e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
700f0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 void sqlite3Add
70100 43 6f 6c 75 6d 6e 54 79 70 65 28 50 61 72 73 65 ColumnType(Parse
70110 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 *,Token*);.SQLIT
70120 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
70130 71 6c 69 74 65 33 41 64 64 44 65 66 61 75 6c 74 qlite3AddDefault
70140 56 61 6c 75 65 28 50 61 72 73 65 2a 2c 45 78 70 Value(Parse*,Exp
70150 72 53 70 61 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f rSpan*);.SQLITE_
70160 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
70170 69 74 65 33 41 64 64 43 6f 6c 6c 61 74 65 54 79 ite3AddCollateTy
70180 70 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e pe(Parse*, Token
70190 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
701a0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
701b0 6e 64 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 54 ndTable(Parse*,T
701c0 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 65 6c oken*,Token*,Sel
701d0 65 63 74 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 ect*);..SQLITE_P
701e0 52 49 56 41 54 45 20 42 69 74 76 65 63 20 2a 73 RIVATE Bitvec *s
701f0 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61 qlite3BitvecCrea
70200 74 65 28 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f te(u32);.SQLITE_
70210 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
70220 74 65 33 42 69 74 76 65 63 54 65 73 74 28 42 69 te3BitvecTest(Bi
70230 74 76 65 63 2a 2c 20 75 33 32 29 3b 0a 53 51 4c tvec*, u32);.SQL
70240 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
70250 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 sqlite3BitvecSet
70260 28 42 69 74 76 65 63 2a 2c 20 75 33 32 29 3b 0a (Bitvec*, u32);.
70270 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
70280 6f 69 64 20 73 71 6c 69 74 65 33 42 69 74 76 65 oid sqlite3Bitve
70290 63 43 6c 65 61 72 28 42 69 74 76 65 63 2a 2c 20 cClear(Bitvec*,
702a0 75 33 32 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c u32, void*);.SQL
702b0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
702c0 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 sqlite3BitvecDe
702d0 73 74 72 6f 79 28 42 69 74 76 65 63 2a 29 3b 0a stroy(Bitvec*);.
702e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 SQLITE_PRIVATE u
702f0 33 32 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 32 sqlite3Bitvec
70300 53 69 7a 65 28 42 69 74 76 65 63 2a 29 3b 0a 53 Size(Bitvec*);.S
70310 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
70320 74 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 42 t sqlite3BitvecB
70330 75 69 6c 74 69 6e 54 65 73 74 28 69 6e 74 2c 69 uiltinTest(int,i
70340 6e 74 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 nt*);..SQLITE_PR
70350 49 56 41 54 45 20 52 6f 77 53 65 74 20 2a 73 71 IVATE RowSet *sq
70360 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 69 74 28 lite3RowSetInit(
70370 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 2a 2c sqlite3*, void*,
70380 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 29 3b 0a unsigned int);.
70390 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
703a0 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 77 53 65 oid sqlite3RowSe
703b0 74 43 6c 65 61 72 28 52 6f 77 53 65 74 2a 29 3b tClear(RowSet*);
703c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
703d0 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 77 53 void sqlite3RowS
703e0 65 74 49 6e 73 65 72 74 28 52 6f 77 53 65 74 2a etInsert(RowSet*
703f0 2c 20 69 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 , i64);.SQLITE_P
70400 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
70410 65 33 52 6f 77 53 65 74 54 65 73 74 28 52 6f 77 e3RowSetTest(Row
70420 53 65 74 2a 2c 20 75 38 20 69 42 61 74 63 68 2c Set*, u8 iBatch,
70430 20 69 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 i64);.SQLITE_PR
70440 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
70450 33 52 6f 77 53 65 74 4e 65 78 74 28 52 6f 77 53 3RowSetNext(RowS
70460 65 74 2a 2c 20 69 36 34 2a 29 3b 0a 0a 53 51 4c et*, i64*);..SQL
70470 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
70480 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 56 69 sqlite3CreateVi
70490 65 77 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a ew(Parse*,Token*
704a0 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 ,Token*,Token*,S
704b0 65 6c 65 63 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b elect*,int,int);
704c0 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 ..#if !defined(S
704d0 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 QLITE_OMIT_VIEW)
704e0 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c || !defined(SQL
704f0 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
70500 54 41 42 4c 45 29 0a 53 51 4c 49 54 45 5f 50 52 TABLE).SQLITE_PR
70510 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 IVATE int sqli
70520 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e te3ViewGetColumn
70530 4e 61 6d 65 73 28 50 61 72 73 65 2a 2c 54 61 62 Names(Parse*,Tab
70540 6c 65 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 le*);.#else.# de
70550 66 69 6e 65 20 73 71 6c 69 74 65 33 56 69 65 77 fine sqlite3View
70560 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 41 GetColumnNames(A
70570 2c 42 29 20 30 0a 23 65 6e 64 69 66 0a 0a 53 51 ,B) 0.#endif..SQ
70580 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
70590 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54 61 62 d sqlite3DropTab
705a0 6c 65 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 le(Parse*, SrcLi
705b0 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a st*, int, int);.
705c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
705d0 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 oid sqlite3Delet
705e0 65 54 61 62 6c 65 28 54 61 62 6c 65 2a 29 3b 0a eTable(Table*);.
705f0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
70600 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e MIT_AUTOINCREMEN
70610 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 T.SQLITE_PRIVATE
70620 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 void sqlite3A
70630 75 74 6f 69 6e 63 72 65 6d 65 6e 74 42 65 67 69 utoincrementBegi
70640 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 n(Parse *pParse)
70650 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
70660 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 void sqlite3A
70670 75 74 6f 69 6e 63 72 65 6d 65 6e 74 45 6e 64 28 utoincrementEnd(
70680 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 3b 0a Parse *pParse);.
70690 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 #else.# define s
706a0 71 6c 69 74 65 33 41 75 74 6f 69 6e 63 72 65 6d qlite3Autoincrem
706b0 65 6e 74 42 65 67 69 6e 28 58 29 0a 23 20 64 65 entBegin(X).# de
706c0 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74 6f fine sqlite3Auto
706d0 69 6e 63 72 65 6d 65 6e 74 45 6e 64 28 58 29 0a incrementEnd(X).
706e0 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 #endif.SQLITE_PR
706f0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
70700 65 33 49 6e 73 65 72 74 28 50 61 72 73 65 2a 2c e3Insert(Parse*,
70710 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 72 4c SrcList*, ExprL
70720 69 73 74 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 49 ist*, Select*, I
70730 64 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 dList*, int);.SQ
70740 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
70750 64 20 2a 73 71 6c 69 74 65 33 41 72 72 61 79 41 d *sqlite3ArrayA
70760 6c 6c 6f 63 61 74 65 28 73 71 6c 69 74 65 33 2a llocate(sqlite3*
70770 2c 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 2c 69 ,void*,int,int,i
70780 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29 3b 0a nt*,int*,int*);.
70790 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49 SQLITE_PRIVATE I
707a0 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 49 64 dList *sqlite3Id
707b0 4c 69 73 74 41 70 70 65 6e 64 28 73 71 6c 69 74 ListAppend(sqlit
707c0 65 33 2a 2c 20 49 64 4c 69 73 74 2a 2c 20 54 6f e3*, IdList*, To
707d0 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ken*);.SQLITE_PR
707e0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
707f0 33 49 64 4c 69 73 74 49 6e 64 65 78 28 49 64 4c 3IdListIndex(IdL
70800 69 73 74 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a ist*,const char*
70810 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
70820 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 E SrcList *sqlit
70830 65 33 53 72 63 4c 69 73 74 45 6e 6c 61 72 67 65 e3SrcListEnlarge
70840 28 73 71 6c 69 74 65 33 2a 2c 20 53 72 63 4c 69 (sqlite3*, SrcLi
70850 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a st*, int, int);.
70860 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 SQLITE_PRIVATE S
70870 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53 rcList *sqlite3S
70880 72 63 4c 69 73 74 41 70 70 65 6e 64 28 73 71 6c rcListAppend(sql
70890 69 74 65 33 2a 2c 20 53 72 63 4c 69 73 74 2a 2c ite3*, SrcList*,
708a0 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 Token*, Token*)
708b0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
708c0 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 SrcList *sqlite
708d0 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 46 72 3SrcListAppendFr
708e0 6f 6d 54 65 72 6d 28 50 61 72 73 65 2a 2c 20 53 omTerm(Parse*, S
708f0 72 63 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 2c rcList*, Token*,
70900 20 54 6f 6b 65 6e 2a 2c 0a 20 20 20 20 20 20 20 Token*,.
70910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
70920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 T
70930 6f 6b 65 6e 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 oken*, Select*,
70940 45 78 70 72 2a 2c 20 49 64 4c 69 73 74 2a 29 3b Expr*, IdList*);
70950 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
70960 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c void sqlite3SrcL
70970 69 73 74 49 6e 64 65 78 65 64 42 79 28 50 61 72 istIndexedBy(Par
70980 73 65 20 2a 2c 20 53 72 63 4c 69 73 74 20 2a 2c se *, SrcList *,
70990 20 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c 49 54 Token *);.SQLIT
709a0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
709b0 6c 69 74 65 33 49 6e 64 65 78 65 64 42 79 4c 6f lite3IndexedByLo
709c0 6f 6b 75 70 28 50 61 72 73 65 20 2a 2c 20 73 74 okup(Parse *, st
709d0 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 ruct SrcList_ite
709e0 6d 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 m *);.SQLITE_PRI
709f0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
70a00 33 53 72 63 4c 69 73 74 53 68 69 66 74 4a 6f 69 3SrcListShiftJoi
70a10 6e 54 79 70 65 28 53 72 63 4c 69 73 74 2a 29 3b nType(SrcList*);
70a20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
70a30 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c void sqlite3SrcL
70a40 69 73 74 41 73 73 69 67 6e 43 75 72 73 6f 72 73 istAssignCursors
70a50 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 (Parse*, SrcList
70a60 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
70a70 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 TE void sqlite3I
70a80 64 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 dListDelete(sqli
70a90 74 65 33 2a 2c 20 49 64 4c 69 73 74 2a 29 3b 0a te3*, IdList*);.
70aa0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
70ab0 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 oid sqlite3SrcLi
70ac0 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 stDelete(sqlite3
70ad0 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 *, SrcList*);.SQ
70ae0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49 6e 64 LITE_PRIVATE Ind
70af0 65 78 20 2a 73 71 6c 69 74 65 33 43 72 65 61 74 ex *sqlite3Creat
70b00 65 49 6e 64 65 78 28 50 61 72 73 65 2a 2c 54 6f eIndex(Parse*,To
70b10 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 72 63 4c ken*,Token*,SrcL
70b20 69 73 74 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 69 ist*,ExprList*,i
70b30 6e 74 2c 54 6f 6b 65 6e 2a 2c 0a 20 20 20 20 20 nt,Token*,.
70b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
70b50 20 20 20 54 6f 6b 65 6e 2a 2c 20 69 6e 74 2c 20 Token*, int,
70b60 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
70b70 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
70b80 33 44 72 6f 70 49 6e 64 65 78 28 50 61 72 73 65 3DropIndex(Parse
70b90 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74 *, SrcList*, int
70ba0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
70bb0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 65 6c E int sqlite3Sel
70bc0 65 63 74 28 50 61 72 73 65 2a 2c 20 53 65 6c 65 ect(Parse*, Sele
70bd0 63 74 2a 2c 20 53 65 6c 65 63 74 44 65 73 74 2a ct*, SelectDest*
70be0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
70bf0 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 65 E Select *sqlite
70c00 33 53 65 6c 65 63 74 4e 65 77 28 50 61 72 73 65 3SelectNew(Parse
70c10 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 53 72 63 4c *,ExprList*,SrcL
70c20 69 73 74 2a 2c 45 78 70 72 2a 2c 45 78 70 72 4c ist*,Expr*,ExprL
70c30 69 73 74 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 ist*,.
70c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 45 E
70c50 78 70 72 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 69 xpr*,ExprList*,i
70c60 6e 74 2c 45 78 70 72 2a 2c 45 78 70 72 2a 29 3b nt,Expr*,Expr*);
70c70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
70c80 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c 65 void sqlite3Sele
70c90 63 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 ctDelete(sqlite3
70ca0 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c *, Select*);.SQL
70cb0 49 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c ITE_PRIVATE Tabl
70cc0 65 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 e *sqlite3SrcLis
70cd0 74 4c 6f 6f 6b 75 70 28 50 61 72 73 65 2a 2c 20 tLookup(Parse*,
70ce0 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 SrcList*);.SQLIT
70cf0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
70d00 6c 69 74 65 33 49 73 52 65 61 64 4f 6e 6c 79 28 lite3IsReadOnly(
70d10 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 Parse*, Table*,
70d20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
70d30 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
70d40 33 4f 70 65 6e 54 61 62 6c 65 28 50 61 72 73 65 3OpenTable(Parse
70d50 2a 2c 20 69 6e 74 20 69 43 75 72 2c 20 69 6e 74 *, int iCur, int
70d60 20 69 44 62 2c 20 54 61 62 6c 65 2a 2c 20 69 6e iDb, Table*, in
70d70 74 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 t);.#if defined(
70d80 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 50 SQLITE_ENABLE_UP
70d90 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d 49 DATE_DELETE_LIMI
70da0 54 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 T) && !defined(S
70db0 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 QLITE_OMIT_SUBQU
70dc0 45 52 59 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 ERY).SQLITE_PRIV
70dd0 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 ATE Expr *sqlite
70de0 33 4c 69 6d 69 74 57 68 65 72 65 28 50 61 72 73 3LimitWhere(Pars
70df0 65 20 2a 2c 20 53 72 63 4c 69 73 74 20 2a 2c 20 e *, SrcList *,
70e00 45 78 70 72 20 2a 2c 20 45 78 70 72 4c 69 73 74 Expr *, ExprList
70e10 20 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78 70 72 *, Expr *, Expr
70e20 20 2a 2c 20 63 68 61 72 20 2a 29 3b 0a 23 65 6e *, char *);.#en
70e30 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 dif.SQLITE_PRIVA
70e40 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 TE void sqlite3D
70e50 65 6c 65 74 65 46 72 6f 6d 28 50 61 72 73 65 2a eleteFrom(Parse*
70e60 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 72 , SrcList*, Expr
70e70 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
70e80 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 TE void sqlite3U
70e90 70 64 61 74 65 28 50 61 72 73 65 2a 2c 20 53 72 pdate(Parse*, Sr
70ea0 63 4c 69 73 74 2a 2c 20 45 78 70 72 4c 69 73 74 cList*, ExprList
70eb0 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a *, Expr*, int);.
70ec0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 57 SQLITE_PRIVATE W
70ed0 68 65 72 65 49 6e 66 6f 20 2a 73 71 6c 69 74 65 hereInfo *sqlite
70ee0 33 57 68 65 72 65 42 65 67 69 6e 28 50 61 72 73 3WhereBegin(Pars
70ef0 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 e*, SrcList*, Ex
70f00 70 72 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2a 2c pr*, ExprList**,
70f10 20 75 31 36 29 3b 0a 53 51 4c 49 54 45 5f 50 52 u16);.SQLITE_PR
70f20 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
70f30 65 33 57 68 65 72 65 45 6e 64 28 57 68 65 72 65 e3WhereEnd(Where
70f40 49 6e 66 6f 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Info*);.SQLITE_P
70f50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
70f60 65 33 45 78 70 72 43 6f 64 65 47 65 74 43 6f 6c e3ExprCodeGetCol
70f70 75 6d 6e 28 50 61 72 73 65 2a 2c 20 54 61 62 6c umn(Parse*, Tabl
70f80 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e e*, int, int, in
70f90 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t, int);.SQLITE_
70fa0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
70fb0 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65 ite3ExprCodeMove
70fc0 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e (Parse*, int, in
70fd0 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t, int);.SQLITE_
70fe0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
70ff0 69 74 65 33 45 78 70 72 43 6f 64 65 43 6f 70 79 ite3ExprCodeCopy
71000 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e (Parse*, int, in
71010 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t, int);.SQLITE_
71020 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
71030 69 74 65 33 45 78 70 72 43 61 63 68 65 53 74 6f ite3ExprCacheSto
71040 72 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 re(Parse*, int,
71050 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 int, int);.SQLIT
71060 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
71070 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 qlite3ExprCacheP
71080 75 73 68 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c ush(Parse*);.SQL
71090 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
710a0 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 sqlite3ExprCach
710b0 65 50 6f 70 28 50 61 72 73 65 2a 2c 20 69 6e 74 ePop(Parse*, int
710c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
710d0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 E void sqlite3Ex
710e0 70 72 43 61 63 68 65 52 65 6d 6f 76 65 28 50 61 prCacheRemove(Pa
710f0 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 rse*, int);.SQLI
71100 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
71110 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 sqlite3ExprCache
71120 43 6c 65 61 72 28 50 61 72 73 65 2a 29 3b 0a 53 Clear(Parse*);.S
71130 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
71140 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 id sqlite3ExprCa
71150 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 cheAffinityChang
71160 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 e(Parse*, int, i
71170 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
71180 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
71190 45 78 70 72 48 61 72 64 43 6f 70 79 28 50 61 72 ExprHardCopy(Par
711a0 73 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 se*,int,int);.SQ
711b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
711c0 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
711d0 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 (Parse*, Expr*,
711e0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
711f0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
71200 45 78 70 72 43 6f 64 65 54 65 6d 70 28 50 61 72 ExprCodeTemp(Par
71210 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 2a se*, Expr*, int*
71220 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
71230 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 E int sqlite3Exp
71240 72 43 6f 64 65 54 61 72 67 65 74 28 50 61 72 73 rCodeTarget(Pars
71250 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b e*, Expr*, int);
71260 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
71270 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 int sqlite3ExprC
71280 6f 64 65 41 6e 64 43 61 63 68 65 28 50 61 72 73 odeAndCache(Pars
71290 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b e*, Expr*, int);
712a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
712b0 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
712c0 43 6f 64 65 43 6f 6e 73 74 61 6e 74 73 28 50 61 CodeConstants(Pa
712d0 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 rse*, Expr*);.SQ
712e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
712f0 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
71300 45 78 70 72 4c 69 73 74 28 50 61 72 73 65 2a 2c ExprList(Parse*,
71310 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c ExprList*, int,
71320 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
71330 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
71340 65 33 45 78 70 72 49 66 54 72 75 65 28 50 61 72 e3ExprIfTrue(Par
71350 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 2c se*, Expr*, int,
71360 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
71370 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
71380 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 50 61 e3ExprIfFalse(Pa
71390 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 rse*, Expr*, int
713a0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
713b0 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 RIVATE Table *sq
713c0 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 73 lite3FindTable(s
713d0 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 qlite3*,const ch
713e0 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a ar*, const char*
713f0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
71400 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 E Table *sqlite3
71410 4c 6f 63 61 74 65 54 61 62 6c 65 28 50 61 72 73 LocateTable(Pars
71420 65 2a 2c 69 6e 74 20 69 73 56 69 65 77 2c 63 6f e*,int isView,co
71430 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 nst char*, const
71440 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f char*);.SQLITE_
71450 50 52 49 56 41 54 45 20 49 6e 64 65 78 20 2a 73 PRIVATE Index *s
71460 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 qlite3FindIndex(
71470 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 sqlite3*,const c
71480 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 har*, const char
71490 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
714a0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 TE void sqlite3U
714b0 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 61 nlinkAndDeleteTa
714c0 62 6c 65 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 ble(sqlite3*,int
714d0 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 ,const char*);.S
714e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
714f0 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b id sqlite3Unlink
71500 41 6e 64 44 65 6c 65 74 65 49 6e 64 65 78 28 73 AndDeleteIndex(s
71510 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 qlite3*,int,cons
71520 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 t char*);.SQLITE
71530 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
71540 6c 69 74 65 33 56 61 63 75 75 6d 28 50 61 72 73 lite3Vacuum(Pars
71550 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
71560 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 ATE int sqlite3R
71570 75 6e 56 61 63 75 75 6d 28 63 68 61 72 2a 2a 2c unVacuum(char**,
71580 20 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 sqlite3*);.SQLI
71590 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 TE_PRIVATE char
715a0 2a 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d *sqlite3NameFrom
715b0 54 6f 6b 65 6e 28 73 71 6c 69 74 65 33 2a 2c 20 Token(sqlite3*,
715c0 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f Token*);.SQLITE_
715d0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
715e0 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 45 te3ExprCompare(E
715f0 78 70 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 xpr*, Expr*);.SQ
71600 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
71610 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 d sqlite3ExprAna
71620 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73 28 4e lyzeAggregates(N
71630 61 6d 65 43 6f 6e 74 65 78 74 2a 2c 20 45 78 70 ameContext*, Exp
71640 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
71650 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
71660 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c 69 ExprAnalyzeAggLi
71670 73 74 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 2c st(NameContext*,
71680 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 ExprList*);.SQLI
71690 54 45 5f 50 52 49 56 41 54 45 20 56 64 62 65 20 TE_PRIVATE Vdbe
716a0 2a 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 *sqlite3GetVdbe(
716b0 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f Parse*);.SQLITE_
716c0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
716d0 69 74 65 33 50 72 6e 67 53 61 76 65 53 74 61 74 ite3PrngSaveStat
716e0 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f e(void);.SQLITE_
716f0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
71700 69 74 65 33 50 72 6e 67 52 65 73 74 6f 72 65 53 ite3PrngRestoreS
71710 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49 tate(void);.SQLI
71720 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
71730 73 71 6c 69 74 65 33 50 72 6e 67 52 65 73 65 74 sqlite3PrngReset
71740 53 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c State(void);.SQL
71750 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
71760 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b sqlite3Rollback
71770 41 6c 6c 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 All(sqlite3*);.S
71780 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
71790 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 id sqlite3CodeVe
717a0 72 69 66 79 53 63 68 65 6d 61 28 50 61 72 73 65 rifySchema(Parse
717b0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
717c0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
717d0 69 74 65 33 42 65 67 69 6e 54 72 61 6e 73 61 63 ite3BeginTransac
717e0 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20 69 6e 74 tion(Parse*, int
717f0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
71800 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f E void sqlite3Co
71810 6d 6d 69 74 54 72 61 6e 73 61 63 74 69 6f 6e 28 mmitTransaction(
71820 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f Parse*);.SQLITE_
71830 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
71840 69 74 65 33 52 6f 6c 6c 62 61 63 6b 54 72 61 6e ite3RollbackTran
71850 73 61 63 74 69 6f 6e 28 50 61 72 73 65 2a 29 3b saction(Parse*);
71860 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
71870 76 6f 69 64 20 73 71 6c 69 74 65 33 53 61 76 65 void sqlite3Save
71880 70 6f 69 6e 74 28 50 61 72 73 65 2a 2c 20 69 6e point(Parse*, in
71890 74 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 t, Token*);.SQLI
718a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
718b0 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 76 65 sqlite3CloseSave
718c0 70 6f 69 6e 74 73 28 73 71 6c 69 74 65 33 20 2a points(sqlite3 *
718d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
718e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 E int sqlite3Exp
718f0 72 49 73 43 6f 6e 73 74 61 6e 74 28 45 78 70 72 rIsConstant(Expr
71900 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
71910 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 TE int sqlite3Ex
71920 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a prIsConstantNotJ
71930 6f 69 6e 28 45 78 70 72 2a 29 3b 0a 53 51 4c 49 oin(Expr*);.SQLI
71940 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
71950 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 qlite3ExprIsCons
71960 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28 45 tantOrFunction(E
71970 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 xpr*);.SQLITE_PR
71980 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
71990 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 45 3ExprIsInteger(E
719a0 78 70 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c xpr*, int*);.SQL
719b0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
719c0 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28 63 sqlite3IsRowid(c
719d0 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c onst char*);.SQL
719e0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
719f0 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 sqlite3Generate
71a00 52 6f 77 44 65 6c 65 74 65 28 50 61 72 73 65 2a RowDelete(Parse*
71a10 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 , Table*, int, i
71a20 6e 74 2c 20 69 6e 74 2c 20 54 72 69 67 67 65 72 nt, int, Trigger
71a30 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 *, int);.SQLITE
71a40 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
71a50 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77 lite3GenerateRow
71a60 49 6e 64 65 78 44 65 6c 65 74 65 28 50 61 72 73 IndexDelete(Pars
71a70 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c e*, Table*, int,
71a80 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 int*);.SQLITE_P
71a90 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
71aa0 65 33 47 65 6e 65 72 61 74 65 49 6e 64 65 78 4b e3GenerateIndexK
71ab0 65 79 28 50 61 72 73 65 2a 2c 20 49 6e 64 65 78 ey(Parse*, Index
71ac0 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 *, int, int, int
71ad0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
71ae0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 47 65 E void sqlite3Ge
71af0 6e 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 nerateConstraint
71b00 43 68 65 63 6b 73 28 50 61 72 73 65 2a 2c 54 61 Checks(Parse*,Ta
71b10 62 6c 65 2a 2c 69 6e 74 2c 69 6e 74 2c 0a 20 20 ble*,int,int,.
71b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71b40 20 20 20 69 6e 74 2a 2c 69 6e 74 2c 69 6e 74 2c int*,int,int,
71b50 69 6e 74 2c 69 6e 74 2c 69 6e 74 2a 29 3b 0a 53 int,int,int*);.S
71b60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
71b70 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 70 6c 65 id sqlite3Comple
71b80 74 65 49 6e 73 65 72 74 69 6f 6e 28 50 61 72 73 teInsertion(Pars
71b90 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c e*, Table*, int,
71ba0 20 69 6e 74 2c 20 69 6e 74 2a 2c 20 69 6e 74 2c int, int*, int,
71bb0 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 int, int);.SQLI
71bc0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
71bd0 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 41 qlite3OpenTableA
71be0 6e 64 49 6e 64 69 63 65 73 28 50 61 72 73 65 2a ndIndices(Parse*
71bf0 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 , Table*, int, i
71c00 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
71c10 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
71c20 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 BeginWriteOperat
71c30 69 6f 6e 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c ion(Parse*, int,
71c40 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
71c50 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
71c60 65 33 4d 75 6c 74 69 57 72 69 74 65 28 50 61 72 e3MultiWrite(Par
71c70 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 se*);.SQLITE_PRI
71c80 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
71c90 33 4d 61 79 41 62 6f 72 74 28 50 61 72 73 65 2a 3MayAbort(Parse*
71ca0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
71cb0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 48 61 E void sqlite3Ha
71cc0 6c 74 43 6f 6e 73 74 72 61 69 6e 74 28 50 61 72 ltConstraint(Par
71cd0 73 65 2a 2c 20 69 6e 74 2c 20 63 68 61 72 2a 2c se*, int, char*,
71ce0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
71cf0 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 IVATE Expr *sqli
71d00 74 65 33 45 78 70 72 44 75 70 28 73 71 6c 69 74 te3ExprDup(sqlit
71d10 65 33 2a 2c 45 78 70 72 2a 2c 69 6e 74 29 3b 0a e3*,Expr*,int);.
71d20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 SQLITE_PRIVATE E
71d30 78 70 72 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 xprList *sqlite3
71d40 45 78 70 72 4c 69 73 74 44 75 70 28 73 71 6c 69 ExprListDup(sqli
71d50 74 65 33 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 69 te3*,ExprList*,i
71d60 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
71d70 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c ATE SrcList *sql
71d80 69 74 65 33 53 72 63 4c 69 73 74 44 75 70 28 73 ite3SrcListDup(s
71d90 71 6c 69 74 65 33 2a 2c 53 72 63 4c 69 73 74 2a qlite3*,SrcList*
71da0 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ,int);.SQLITE_PR
71db0 49 56 41 54 45 20 49 64 4c 69 73 74 20 2a 73 71 IVATE IdList *sq
71dc0 6c 69 74 65 33 49 64 4c 69 73 74 44 75 70 28 73 lite3IdListDup(s
71dd0 71 6c 69 74 65 33 2a 2c 49 64 4c 69 73 74 2a 29 qlite3*,IdList*)
71de0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
71df0 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 65 33 Select *sqlite3
71e00 53 65 6c 65 63 74 44 75 70 28 73 71 6c 69 74 65 SelectDup(sqlite
71e10 33 2a 2c 53 65 6c 65 63 74 2a 2c 69 6e 74 29 3b 3*,Select*,int);
71e20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
71e30 76 6f 69 64 20 73 71 6c 69 74 65 33 46 75 6e 63 void sqlite3Func
71e40 44 65 66 49 6e 73 65 72 74 28 46 75 6e 63 44 65 DefInsert(FuncDe
71e50 66 48 61 73 68 2a 2c 20 46 75 6e 63 44 65 66 2a fHash*, FuncDef*
71e60 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
71e70 45 20 46 75 6e 63 44 65 66 20 2a 73 71 6c 69 74 E FuncDef *sqlit
71e80 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 73 e3FindFunction(s
71e90 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 qlite3*,const ch
71ea0 61 72 2a 2c 69 6e 74 2c 69 6e 74 2c 75 38 2c 69 ar*,int,int,u8,i
71eb0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
71ec0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
71ed0 52 65 67 69 73 74 65 72 42 75 69 6c 74 69 6e 46 RegisterBuiltinF
71ee0 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 unctions(sqlite3
71ef0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
71f00 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 TE void sqlite3R
71f10 65 67 69 73 74 65 72 44 61 74 65 54 69 6d 65 46 egisterDateTimeF
71f20 75 6e 63 74 69 6f 6e 73 28 76 6f 69 64 29 3b 0a unctions(void);.
71f30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
71f40 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 oid sqlite3Regis
71f50 74 65 72 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f terGlobalFunctio
71f60 6e 73 28 76 6f 69 64 29 3b 0a 23 69 66 64 65 66 ns(void);.#ifdef
71f70 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 SQLITE_DEBUG.SQ
71f80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 LITE_PRIVATE i
71f90 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 nt sqlite3Safety
71fa0 4f 6e 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 On(sqlite3*);.SQ
71fb0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 LITE_PRIVATE i
71fc0 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 nt sqlite3Safety
71fd0 4f 66 66 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23 Off(sqlite3*);.#
71fe0 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71 else.# define sq
71ff0 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 41 29 lite3SafetyOn(A)
72000 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 0.# define sqli
72010 74 65 33 53 61 66 65 74 79 4f 66 66 28 41 29 20 te3SafetyOff(A)
72020 30 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 0.#endif.SQLITE_
72030 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
72040 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 4f 6b te3SafetyCheckOk
72050 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 (sqlite3*);.SQLI
72060 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
72070 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63 qlite3SafetyChec
72080 6b 53 69 63 6b 4f 72 4f 6b 28 73 71 6c 69 74 65 kSickOrOk(sqlite
72090 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 3*);.SQLITE_PRIV
720a0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
720b0 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 50 61 72 ChangeCookie(Par
720c0 73 65 2a 2c 20 69 6e 74 29 3b 0a 0a 23 69 66 20 se*, int);..#if
720d0 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
720e0 4f 4d 49 54 5f 56 49 45 57 29 20 26 26 20 21 64 OMIT_VIEW) && !d
720f0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
72100 49 54 5f 54 52 49 47 47 45 52 29 0a 53 51 4c 49 IT_TRIGGER).SQLI
72110 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
72120 73 71 6c 69 74 65 33 4d 61 74 65 72 69 61 6c 69 sqlite3Materiali
72130 7a 65 56 69 65 77 28 50 61 72 73 65 2a 2c 20 54 zeView(Parse*, T
72140 61 62 6c 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e able*, Expr*, in
72150 74 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e t);.#endif..#ifn
72160 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
72170 54 52 49 47 47 45 52 0a 53 51 4c 49 54 45 5f 50 TRIGGER.SQLITE_P
72180 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
72190 6c 69 74 65 33 42 65 67 69 6e 54 72 69 67 67 65 lite3BeginTrigge
721a0 72 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a r(Parse*, Token*
721b0 2c 54 6f 6b 65 6e 2a 2c 69 6e 74 2c 69 6e 74 2c ,Token*,int,int,
721c0 49 64 4c 69 73 74 2a 2c 53 72 63 4c 69 73 74 2a IdList*,SrcList*
721d0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
721e0 20 20 20 20 20 20 20 20 20 20 20 20 20 45 78 70 Exp
721f0 72 2a 2c 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 r*,int, int);.SQ
72200 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 LITE_PRIVATE v
72210 6f 69 64 20 73 71 6c 69 74 65 33 46 69 6e 69 73 oid sqlite3Finis
72220 68 54 72 69 67 67 65 72 28 50 61 72 73 65 2a 2c hTrigger(Parse*,
72230 20 54 72 69 67 67 65 72 53 74 65 70 2a 2c 20 54 TriggerStep*, T
72240 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 oken*);.SQLITE_P
72250 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
72260 6c 69 74 65 33 44 72 6f 70 54 72 69 67 67 65 72 lite3DropTrigger
72270 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 (Parse*, SrcList
72280 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
72290 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 PRIVATE void s
722a0 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67 67 65 qlite3DropTrigge
722b0 72 50 74 72 28 50 61 72 73 65 2a 2c 20 54 72 69 rPtr(Parse*, Tri
722c0 67 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 gger*);.SQLITE_P
722d0 52 49 56 41 54 45 20 20 20 54 72 69 67 67 65 72 RIVATE Trigger
722e0 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 *sqlite3Trigger
722f0 73 45 78 69 73 74 28 50 61 72 73 65 20 2a 2c 20 sExist(Parse *,
72300 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 45 78 70 Table*, int, Exp
72310 72 4c 69 73 74 2a 2c 20 69 6e 74 20 2a 70 4d 61 rList*, int *pMa
72320 73 6b 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 sk);.SQLITE_PRIV
72330 41 54 45 20 20 20 54 72 69 67 67 65 72 20 2a 73 ATE Trigger *s
72340 71 6c 69 74 65 33 54 72 69 67 67 65 72 4c 69 73 qlite3TriggerLis
72350 74 28 50 61 72 73 65 20 2a 2c 20 54 61 62 6c 65 t(Parse *, Table
72360 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
72370 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
72380 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 e3CodeRowTrigger
72390 28 50 61 72 73 65 2a 2c 20 54 72 69 67 67 65 72 (Parse*, Trigger
723a0 20 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69 73 *, int, ExprLis
723b0 74 2a 2c 20 69 6e 74 2c 20 54 61 62 6c 65 20 2a t*, int, Table *
723c0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
723d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e in
723e0 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 t, int, int);.SQ
723f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 LITE_PRIVATE v
72400 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 52 oid sqlite3CodeR
72410 6f 77 54 72 69 67 67 65 72 44 69 72 65 63 74 28 owTriggerDirect(
72420 50 61 72 73 65 20 2a 2c 20 54 72 69 67 67 65 72 Parse *, Trigger
72430 20 2a 2c 20 54 61 62 6c 65 20 2a 2c 20 69 6e 74 *, Table *, int
72440 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 20 20 76 , int, int);. v
72450 6f 69 64 20 73 71 6c 69 74 65 56 69 65 77 54 72 oid sqliteViewTr
72460 69 67 67 65 72 73 28 50 61 72 73 65 2a 2c 20 54 iggers(Parse*, T
72470 61 62 6c 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e able*, Expr*, in
72480 74 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53 t, ExprList*);.S
72490 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
724a0 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 void sqlite3Dele
724b0 74 65 54 72 69 67 67 65 72 53 74 65 70 28 73 71 teTriggerStep(sq
724c0 6c 69 74 65 33 2a 2c 20 54 72 69 67 67 65 72 53 lite3*, TriggerS
724d0 74 65 70 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 tep*);.SQLITE_PR
724e0 49 56 41 54 45 20 20 20 54 72 69 67 67 65 72 53 IVATE TriggerS
724f0 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72 69 67 tep *sqlite3Trig
72500 67 65 72 53 65 6c 65 63 74 53 74 65 70 28 73 71 gerSelectStep(sq
72510 6c 69 74 65 33 2a 2c 53 65 6c 65 63 74 2a 29 3b lite3*,Select*);
72520 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
72530 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 TriggerStep *s
72540 71 6c 69 74 65 33 54 72 69 67 67 65 72 49 6e 73 qlite3TriggerIns
72550 65 72 74 53 74 65 70 28 73 71 6c 69 74 65 33 2a ertStep(sqlite3*
72560 2c 54 6f 6b 65 6e 2a 2c 20 49 64 4c 69 73 74 2a ,Token*, IdList*
72570 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
72580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
72590 20 20 20 20 20 20 20 20 20 20 45 78 70 72 4c 69 ExprLi
725a0 73 74 2a 2c 53 65 6c 65 63 74 2a 2c 75 38 29 3b st*,Select*,u8);
725b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
725c0 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 TriggerStep *s
725d0 71 6c 69 74 65 33 54 72 69 67 67 65 72 55 70 64 qlite3TriggerUpd
725e0 61 74 65 53 74 65 70 28 73 71 6c 69 74 65 33 2a ateStep(sqlite3*
725f0 2c 54 6f 6b 65 6e 2a 2c 45 78 70 72 4c 69 73 74 ,Token*,ExprList
72600 2a 2c 20 45 78 70 72 2a 2c 20 75 38 29 3b 0a 53 *, Expr*, u8);.S
72610 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
72620 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 71 6c TriggerStep *sql
72630 69 74 65 33 54 72 69 67 67 65 72 44 65 6c 65 74 ite3TriggerDelet
72640 65 53 74 65 70 28 73 71 6c 69 74 65 33 2a 2c 54 eStep(sqlite3*,T
72650 6f 6b 65 6e 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 oken*, Expr*);.S
72660 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
72670 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 void sqlite3Dele
72680 74 65 54 72 69 67 67 65 72 28 73 71 6c 69 74 65 teTrigger(sqlite
72690 33 2a 2c 20 54 72 69 67 67 65 72 2a 29 3b 0a 53 3*, Trigger*);.S
726a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
726b0 76 6f 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 void sqlite3Unli
726c0 6e 6b 41 6e 64 44 65 6c 65 74 65 54 72 69 67 67 nkAndDeleteTrigg
726d0 65 72 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c er(sqlite3*,int,
726e0 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 const char*);.SQ
726f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 75 LITE_PRIVATE u
72700 33 32 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 32 sqlite3Trigge
72710 72 4f 6c 64 6d 61 73 6b 28 50 61 72 73 65 2a 2c rOldmask(Parse*,
72720 54 72 69 67 67 65 72 2a 2c 45 78 70 72 4c 69 73 Trigger*,ExprLis
72730 74 2a 2c 54 61 62 6c 65 2a 2c 69 6e 74 29 3b 0a t*,Table*,int);.
72740 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
72750 50 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 70 29 ParseToplevel(p)
72760 20 28 28 70 29 2d 3e 70 54 6f 70 6c 65 76 65 6c ((p)->pToplevel
72770 20 3f 20 28 70 29 2d 3e 70 54 6f 70 6c 65 76 65 ? (p)->pTopleve
72780 6c 20 3a 20 28 70 29 29 0a 23 65 6c 73 65 0a 23 l : (p)).#else.#
72790 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 define sqlite3T
727a0 72 69 67 67 65 72 73 45 78 69 73 74 28 42 2c 43 riggersExist(B,C
727b0 2c 44 2c 45 2c 46 29 20 30 0a 23 20 64 65 66 69 ,D,E,F) 0.# defi
727c0 6e 65 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 ne sqlite3Delete
727d0 54 72 69 67 67 65 72 28 41 2c 42 29 0a 23 20 64 Trigger(A,B).# d
727e0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 44 72 6f efine sqlite3Dro
727f0 70 54 72 69 67 67 65 72 50 74 72 28 41 2c 42 29 pTriggerPtr(A,B)
72800 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
72810 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 3UnlinkAndDelete
72820 54 72 69 67 67 65 72 28 41 2c 42 2c 43 29 0a 23 Trigger(A,B,C).#
72830 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43 define sqlite3C
72840 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28 41 2c odeRowTrigger(A,
72850 42 2c 43 2c 44 2c 45 2c 46 2c 47 2c 48 2c 49 29 B,C,D,E,F,G,H,I)
72860 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
72870 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 44 3CodeRowTriggerD
72880 69 72 65 63 74 28 41 2c 42 2c 43 2c 44 2c 45 2c irect(A,B,C,D,E,
72890 46 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 F).# define sqli
728a0 74 65 33 54 72 69 67 67 65 72 4c 69 73 74 28 58 te3TriggerList(X
728b0 2c 20 59 29 20 30 0a 23 20 64 65 66 69 6e 65 20 , Y) 0.# define
728c0 73 71 6c 69 74 65 33 50 61 72 73 65 54 6f 70 6c sqlite3ParseTopl
728d0 65 76 65 6c 28 70 29 20 70 0a 23 20 64 65 66 69 evel(p) p.# defi
728e0 6e 65 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 ne sqlite3Trigge
728f0 72 4f 6c 64 6d 61 73 6b 28 41 2c 42 2c 43 2c 44 rOldmask(A,B,C,D
72900 2c 45 29 20 30 0a 23 65 6e 64 69 66 0a 0a 53 51 ,E) 0.#endif..SQ
72910 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
72920 20 73 71 6c 69 74 65 33 4a 6f 69 6e 54 79 70 65 sqlite3JoinType
72930 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c (Parse*, Token*,
72940 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 Token*, Token*)
72950 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
72960 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65 void sqlite3Cre
72970 61 74 65 46 6f 72 65 69 67 6e 4b 65 79 28 50 61 ateForeignKey(Pa
72980 72 73 65 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c rse*, ExprList*,
72990 20 54 6f 6b 65 6e 2a 2c 20 45 78 70 72 4c 69 73 Token*, ExprLis
729a0 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 t*, int);.SQLITE
729b0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
729c0 6c 69 74 65 33 44 65 66 65 72 46 6f 72 65 69 67 lite3DeferForeig
729d0 6e 4b 65 79 28 50 61 72 73 65 2a 2c 20 69 6e 74 nKey(Parse*, int
729e0 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 );.#ifndef SQLIT
729f0 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 E_OMIT_AUTHORIZA
72a00 54 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 TION.SQLITE_PRIV
72a10 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
72a20 65 33 41 75 74 68 52 65 61 64 28 50 61 72 73 65 e3AuthRead(Parse
72a30 2a 2c 45 78 70 72 2a 2c 53 63 68 65 6d 61 2a 2c *,Expr*,Schema*,
72a40 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 SrcList*);.SQLIT
72a50 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 E_PRIVATE int
72a60 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b sqlite3AuthCheck
72a70 28 50 61 72 73 65 2a 2c 69 6e 74 2c 20 63 6f 6e (Parse*,int, con
72a80 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 st char*, const
72a90 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 char*, const cha
72aa0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
72ab0 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
72ac0 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 75 73 e3AuthContextPus
72ad0 68 28 50 61 72 73 65 2a 2c 20 41 75 74 68 43 6f h(Parse*, AuthCo
72ae0 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 63 68 ntext*, const ch
72af0 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ar*);.SQLITE_PRI
72b00 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
72b10 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 6f te3AuthContextPo
72b20 70 28 41 75 74 68 43 6f 6e 74 65 78 74 2a 29 3b p(AuthContext*);
72b30 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
72b40 20 20 69 6e 74 20 73 71 6c 69 74 65 33 41 75 74 int sqlite3Aut
72b50 68 52 65 61 64 43 6f 6c 28 50 61 72 73 65 2a 2c hReadCol(Parse*,
72b60 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 63 const char *, c
72b70 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 onst char *, int
72b80 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e );.#else.# defin
72b90 65 20 73 71 6c 69 74 65 33 41 75 74 68 52 65 61 e sqlite3AuthRea
72ba0 64 28 61 2c 62 2c 63 2c 64 29 0a 23 20 64 65 66 d(a,b,c,d).# def
72bb0 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74 68 43 ine sqlite3AuthC
72bc0 68 65 63 6b 28 61 2c 62 2c 63 2c 64 2c 65 29 20 heck(a,b,c,d,e)
72bd0 20 20 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 20 64 SQLITE_OK.# d
72be0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74 efine sqlite3Aut
72bf0 68 43 6f 6e 74 65 78 74 50 75 73 68 28 61 2c 62 hContextPush(a,b
72c00 2c 63 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c ,c).# define sql
72c10 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 ite3AuthContextP
72c20 6f 70 28 61 29 20 20 28 28 76 6f 69 64 29 28 61 op(a) ((void)(a
72c30 29 29 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 )).#endif.SQLITE
72c40 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
72c50 6c 69 74 65 33 41 74 74 61 63 68 28 50 61 72 73 lite3Attach(Pars
72c60 65 2a 2c 20 45 78 70 72 2a 2c 20 45 78 70 72 2a e*, Expr*, Expr*
72c70 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 , Expr*);.SQLITE
72c80 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
72c90 6c 69 74 65 33 44 65 74 61 63 68 28 50 61 72 73 lite3Detach(Pars
72ca0 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 e*, Expr*);.SQLI
72cb0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
72cc0 71 6c 69 74 65 33 42 74 72 65 65 46 61 63 74 6f qlite3BtreeFacto
72cd0 72 79 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 ry(sqlite3 *db,
72ce0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c const char *zFil
72cf0 65 6e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 ename,.
72d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e in
72d10 74 20 6f 6d 69 74 4a 6f 75 72 6e 61 6c 2c 20 69 t omitJournal, i
72d20 6e 74 20 6e 43 61 63 68 65 2c 20 69 6e 74 20 66 nt nCache, int f
72d30 6c 61 67 73 2c 20 42 74 72 65 65 20 2a 2a 70 70 lags, Btree **pp
72d40 42 74 72 65 65 29 3b 0a 53 51 4c 49 54 45 5f 50 Btree);.SQLITE_P
72d50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
72d60 65 33 46 69 78 49 6e 69 74 28 44 62 46 69 78 65 e3FixInit(DbFixe
72d70 72 2a 2c 20 50 61 72 73 65 2a 2c 20 69 6e 74 2c r*, Parse*, int,
72d80 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f const char*, co
72d90 6e 73 74 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c nst Token*);.SQL
72da0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
72db0 73 71 6c 69 74 65 33 46 69 78 53 72 63 4c 69 73 sqlite3FixSrcLis
72dc0 74 28 44 62 46 69 78 65 72 2a 2c 20 53 72 63 4c t(DbFixer*, SrcL
72dd0 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ist*);.SQLITE_PR
72de0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
72df0 33 46 69 78 53 65 6c 65 63 74 28 44 62 46 69 78 3FixSelect(DbFix
72e00 65 72 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 er*, Select*);.S
72e10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
72e20 74 20 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 t sqlite3FixExpr
72e30 28 44 62 46 69 78 65 72 2a 2c 20 45 78 70 72 2a (DbFixer*, Expr*
72e40 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
72e50 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 E int sqlite3Fix
72e60 45 78 70 72 4c 69 73 74 28 44 62 46 69 78 65 72 ExprList(DbFixer
72e70 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53 *, ExprList*);.S
72e80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
72e90 74 20 73 71 6c 69 74 65 33 46 69 78 54 72 69 67 t sqlite3FixTrig
72ea0 67 65 72 53 74 65 70 28 44 62 46 69 78 65 72 2a gerStep(DbFixer*
72eb0 2c 20 54 72 69 67 67 65 72 53 74 65 70 2a 29 3b , TriggerStep*);
72ec0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
72ed0 69 6e 74 20 73 71 6c 69 74 65 33 41 74 6f 46 28 int sqlite3AtoF(
72ee0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 64 const char *z, d
72ef0 6f 75 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f ouble*);.SQLITE_
72f00 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
72f10 74 65 33 47 65 74 49 6e 74 33 32 28 63 6f 6e 73 te3GetInt32(cons
72f20 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2a 29 3b t char *, int*);
72f30 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
72f40 69 6e 74 20 73 71 6c 69 74 65 33 46 69 74 73 49 int sqlite3FitsI
72f50 6e 36 34 42 69 74 73 28 63 6f 6e 73 74 20 63 68 n64Bits(const ch
72f60 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 ar *, int);.SQLI
72f70 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
72f80 71 6c 69 74 65 33 55 74 66 31 36 42 79 74 65 4c qlite3Utf16ByteL
72f90 65 6e 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 en(const void *p
72fa0 44 61 74 61 2c 20 69 6e 74 20 6e 43 68 61 72 29 Data, int nChar)
72fb0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
72fc0 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38 int sqlite3Utf8
72fd0 43 68 61 72 4c 65 6e 28 63 6f 6e 73 74 20 63 68 CharLen(const ch
72fe0 61 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e ar *pData, int n
72ff0 42 79 74 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Byte);.SQLITE_PR
73000 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
73010 33 55 74 66 38 52 65 61 64 28 63 6f 6e 73 74 20 3Utf8Read(const
73020 75 38 2a 2c 20 63 6f 6e 73 74 20 75 38 2a 2a 29 u8*, const u8**)
73030 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 ;../*.** Routine
73040 73 20 74 6f 20 72 65 61 64 20 61 6e 64 20 77 72 s to read and wr
73050 69 74 65 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e ite variable-len
73060 67 74 68 20 69 6e 74 65 67 65 72 73 2e 20 20 54 gth integers. T
73070 68 65 73 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 hese used to.**
73080 62 65 20 64 65 66 69 6e 65 64 20 6c 6f 63 61 6c be defined local
73090 6c 79 2c 20 62 75 74 20 6e 6f 77 20 77 65 20 75 ly, but now we u
730a0 73 65 20 74 68 65 20 76 61 72 69 6e 74 20 72 6f se the varint ro
730b0 75 74 69 6e 65 73 20 69 6e 20 74 68 65 20 75 74 utines in the ut
730c0 69 6c 2e 63 0a 2a 2a 20 66 69 6c 65 2e 20 20 43 il.c.** file. C
730d0 6f 64 65 20 73 68 6f 75 6c 64 20 75 73 65 20 74 ode should use t
730e0 68 65 20 4d 41 43 52 4f 20 66 6f 72 6d 73 20 62 he MACRO forms b
730f0 65 6c 6f 77 2c 20 61 73 20 74 68 65 20 56 61 72 elow, as the Var
73100 69 6e 74 33 32 20 76 65 72 73 69 6f 6e 73 0a 2a int32 versions.*
73110 2a 20 61 72 65 20 63 6f 64 65 64 20 74 6f 20 61 * are coded to a
73120 73 73 75 6d 65 20 74 68 65 20 73 69 6e 67 6c 65 ssume the single
73130 20 62 79 74 65 20 63 61 73 65 20 69 73 20 61 6c byte case is al
73140 72 65 61 64 79 20 68 61 6e 64 6c 65 64 20 28 77 ready handled (w
73150 68 69 63 68 20 0a 2a 2a 20 74 68 65 20 4d 41 43 hich .** the MAC
73160 52 4f 20 66 6f 72 6d 20 64 6f 65 73 29 2e 0a 2a RO form does)..*
73170 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
73180 20 69 6e 74 20 73 71 6c 69 74 65 33 50 75 74 56 int sqlite3PutV
73190 61 72 69 6e 74 28 75 6e 73 69 67 6e 65 64 20 63 arint(unsigned c
731a0 68 61 72 2a 2c 20 75 36 34 29 3b 0a 53 51 4c 49 har*, u64);.SQLI
731b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
731c0 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 33 qlite3PutVarint3
731d0 32 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 2(unsigned char*
731e0 2c 20 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50 , u32);.SQLITE_P
731f0 52 49 56 41 54 45 20 75 38 20 73 71 6c 69 74 65 RIVATE u8 sqlite
73200 33 47 65 74 56 61 72 69 6e 74 28 63 6f 6e 73 74 3GetVarint(const
73210 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
73220 2c 20 75 36 34 20 2a 29 3b 0a 53 51 4c 49 54 45 , u64 *);.SQLITE
73230 5f 50 52 49 56 41 54 45 20 75 38 20 73 71 6c 69 _PRIVATE u8 sqli
73240 74 65 33 47 65 74 56 61 72 69 6e 74 33 32 28 63 te3GetVarint32(c
73250 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
73260 61 72 20 2a 2c 20 75 33 32 20 2a 29 3b 0a 53 51 ar *, u32 *);.SQ
73270 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
73280 20 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 sqlite3VarintLe
73290 6e 28 75 36 34 20 76 29 3b 0a 0a 2f 2a 0a 2a 2a n(u64 v);../*.**
732a0 20 54 68 65 20 68 65 61 64 65 72 20 6f 66 20 61 The header of a
732b0 20 72 65 63 6f 72 64 20 63 6f 6e 73 69 73 74 73 record consists
732c0 20 6f 66 20 61 20 73 65 71 75 65 6e 63 65 20 76 of a sequence v
732d0 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 ariable-length i
732e0 6e 74 65 67 65 72 73 2e 0a 2a 2a 20 54 68 65 73 ntegers..** Thes
732f0 65 20 69 6e 74 65 67 65 72 73 20 61 72 65 20 61 e integers are a
73300 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20 73 6d 61 lmost always sma
73310 6c 6c 20 61 6e 64 20 61 72 65 20 65 6e 63 6f 64 ll and are encod
73320 65 64 20 61 73 20 61 20 73 69 6e 67 6c 65 20 62 ed as a single b
73330 79 74 65 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c yte..** The foll
73340 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 74 61 6b owing macros tak
73350 65 20 61 64 76 61 6e 74 61 67 65 20 74 68 69 73 e advantage this
73360 20 66 61 63 74 20 74 6f 20 70 72 6f 76 69 64 65 fact to provide
73370 20 61 20 66 61 73 74 20 65 6e 63 6f 64 65 0a 2a a fast encode.*
73380 2a 20 61 6e 64 20 64 65 63 6f 64 65 20 6f 66 20 * and decode of
73390 74 68 65 20 69 6e 74 65 67 65 72 73 20 69 6e 20 the integers in
733a0 61 20 72 65 63 6f 72 64 20 68 65 61 64 65 72 2e a record header.
733b0 20 20 49 74 20 69 73 20 66 61 73 74 65 72 20 66 It is faster f
733c0 6f 72 20 74 68 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a or the common.**
733d0 20 63 61 73 65 20 77 68 65 72 65 20 74 68 65 20 case where the
733e0 69 6e 74 65 67 65 72 20 69 73 20 61 20 73 69 6e integer is a sin
733f0 67 6c 65 20 62 79 74 65 2e 20 20 49 74 20 69 73 gle byte. It is
73400 20 61 20 6c 69 74 74 6c 65 20 73 6c 6f 77 65 72 a little slower
73410 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 69 6e 74 when the.** int
73420 65 67 65 72 20 69 73 20 74 77 6f 20 6f 72 20 6d eger is two or m
73430 6f 72 65 20 62 79 74 65 73 2e 20 20 42 75 74 20 ore bytes. But
73440 6f 76 65 72 61 6c 6c 20 69 74 20 69 73 20 66 61 overall it is fa
73450 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ster..**.** The
73460 66 6f 6c 6c 6f 77 69 6e 67 20 65 78 70 72 65 73 following expres
73470 73 69 6f 6e 73 20 61 72 65 20 65 71 75 69 76 61 sions are equiva
73480 6c 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 lent:.**.**
73490 78 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 61 x = sqlite3GetVa
734a0 72 69 6e 74 33 32 28 20 41 2c 20 26 42 20 29 3b rint32( A, &B );
734b0 0a 2a 2a 20 20 20 20 20 78 20 3d 20 73 71 6c 69 .** x = sqli
734c0 74 65 33 50 75 74 56 61 72 69 6e 74 33 32 28 20 te3PutVarint32(
734d0 41 2c 20 42 20 29 3b 0a 2a 2a 0a 2a 2a 20 20 20 A, B );.**.**
734e0 20 20 78 20 3d 20 67 65 74 56 61 72 69 6e 74 33 x = getVarint3
734f0 32 28 20 41 2c 20 42 20 29 3b 0a 2a 2a 20 20 20 2( A, B );.**
73500 20 20 78 20 3d 20 70 75 74 56 61 72 69 6e 74 33 x = putVarint3
73510 32 28 20 41 2c 20 42 20 29 3b 0a 2a 2a 0a 2a 2f 2( A, B );.**.*/
73520 0a 23 64 65 66 69 6e 65 20 67 65 74 56 61 72 69 .#define getVari
73530 6e 74 33 32 28 41 2c 42 29 20 20 28 75 38 29 28 nt32(A,B) (u8)(
73540 28 2a 28 41 29 3c 28 75 38 29 30 78 38 30 29 20 (*(A)<(u8)0x80)
73550 3f 20 28 28 42 29 20 3d 20 28 75 33 32 29 2a 28 ? ((B) = (u32)*(
73560 41 29 29 2c 31 20 3a 20 73 71 6c 69 74 65 33 47 A)),1 : sqlite3G
73570 65 74 56 61 72 69 6e 74 33 32 28 28 41 29 2c 20 etVarint32((A),
73580 28 75 33 32 20 2a 29 26 28 42 29 29 29 0a 23 64 (u32 *)&(B))).#d
73590 65 66 69 6e 65 20 70 75 74 56 61 72 69 6e 74 33 efine putVarint3
735a0 32 28 41 2c 42 29 20 20 28 75 38 29 28 28 28 75 2(A,B) (u8)(((u
735b0 33 32 29 28 42 29 3c 28 75 33 32 29 30 78 38 30 32)(B)<(u32)0x80
735c0 29 20 3f 20 28 2a 28 41 29 20 3d 20 28 75 6e 73 ) ? (*(A) = (uns
735d0 69 67 6e 65 64 20 63 68 61 72 29 28 42 29 29 2c igned char)(B)),
735e0 31 20 3a 20 73 71 6c 69 74 65 33 50 75 74 56 61 1 : sqlite3PutVa
735f0 72 69 6e 74 33 32 28 28 41 29 2c 20 28 42 29 29 rint32((A), (B))
73600 29 0a 23 64 65 66 69 6e 65 20 67 65 74 56 61 72 ).#define getVar
73610 69 6e 74 20 20 20 20 73 71 6c 69 74 65 33 47 65 int sqlite3Ge
73620 74 56 61 72 69 6e 74 0a 23 64 65 66 69 6e 65 20 tVarint.#define
73630 70 75 74 56 61 72 69 6e 74 20 20 20 20 73 71 6c putVarint sql
73640 69 74 65 33 50 75 74 56 61 72 69 6e 74 0a 0a 0a ite3PutVarint...
73650 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
73660 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 onst char *sqlit
73670 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 53 e3IndexAffinityS
73680 74 72 28 56 64 62 65 20 2a 2c 20 49 6e 64 65 78 tr(Vdbe *, Index
73690 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
736a0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
736b0 54 61 62 6c 65 41 66 66 69 6e 69 74 79 53 74 72 TableAffinityStr
736c0 28 56 64 62 65 20 2a 2c 20 54 61 62 6c 65 20 2a (Vdbe *, Table *
736d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
736e0 45 20 63 68 61 72 20 73 71 6c 69 74 65 33 43 6f E char sqlite3Co
736f0 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 45 78 mpareAffinity(Ex
73700 70 72 20 2a 70 45 78 70 72 2c 20 63 68 61 72 20 pr *pExpr, char
73710 61 66 66 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52 aff2);.SQLITE_PR
73720 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
73730 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 4f 6b 3IndexAffinityOk
73740 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68 (Expr *pExpr, ch
73750 61 72 20 69 64 78 5f 61 66 66 69 6e 69 74 79 29 ar idx_affinity)
73760 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
73770 20 63 68 61 72 20 73 71 6c 69 74 65 33 45 78 70 char sqlite3Exp
73780 72 41 66 66 69 6e 69 74 79 28 45 78 70 72 20 2a rAffinity(Expr *
73790 70 45 78 70 72 29 3b 0a 53 51 4c 49 54 45 5f 50 pExpr);.SQLITE_P
737a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
737b0 65 33 41 74 6f 69 36 34 28 63 6f 6e 73 74 20 63 e3Atoi64(const c
737c0 68 61 72 2a 2c 20 69 36 34 2a 29 3b 0a 53 51 4c har*, i64*);.SQL
737d0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
737e0 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 73 71 sqlite3Error(sq
737f0 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 63 6f 6e lite3*, int, con
73800 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 53 st char*,...);.S
73810 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
73820 69 64 20 2a 73 71 6c 69 74 65 33 48 65 78 54 6f id *sqlite3HexTo
73830 42 6c 6f 62 28 73 71 6c 69 74 65 33 2a 2c 20 63 Blob(sqlite3*, c
73840 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e onst char *z, in
73850 74 20 6e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 t n);.SQLITE_PRI
73860 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
73870 54 77 6f 50 61 72 74 4e 61 6d 65 28 50 61 72 73 TwoPartName(Pars
73880 65 20 2a 2c 20 54 6f 6b 65 6e 20 2a 2c 20 54 6f e *, Token *, To
73890 6b 65 6e 20 2a 2c 20 54 6f 6b 65 6e 20 2a 2a 29 ken *, Token **)
738a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
738b0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c const char *sql
738c0 69 74 65 33 45 72 72 53 74 72 28 69 6e 74 29 3b ite3ErrStr(int);
738d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
738e0 69 6e 74 20 73 71 6c 69 74 65 33 52 65 61 64 53 int sqlite3ReadS
738f0 63 68 65 6d 61 28 50 61 72 73 65 20 2a 70 50 61 chema(Parse *pPa
73900 72 73 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 rse);.SQLITE_PRI
73910 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 VATE CollSeq *sq
73920 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 lite3FindCollSeq
73930 28 73 71 6c 69 74 65 33 2a 2c 75 38 20 65 6e 63 (sqlite3*,u8 enc
73940 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e , const char*,in
73950 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
73960 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 TE CollSeq *sqli
73970 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 te3LocateCollSeq
73980 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
73990 63 6f 6e 73 74 20 63 68 61 72 2a 7a 4e 61 6d 65 const char*zName
739a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
739b0 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 E CollSeq *sqlit
739c0 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 50 61 e3ExprCollSeq(Pa
739d0 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 rse *pParse, Exp
739e0 72 20 2a 70 45 78 70 72 29 3b 0a 53 51 4c 49 54 r *pExpr);.SQLIT
739f0 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a E_PRIVATE Expr *
73a00 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 43 6f sqlite3ExprSetCo
73a10 6c 6c 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 ll(Parse *pParse
73a20 2c 20 45 78 70 72 20 2a 2c 20 54 6f 6b 65 6e 20 , Expr *, Token
73a30 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
73a40 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 68 TE int sqlite3Ch
73a50 65 63 6b 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 eckCollSeq(Parse
73a60 20 2a 2c 20 43 6f 6c 6c 53 65 71 20 2a 29 3b 0a *, CollSeq *);.
73a70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
73a80 6e 74 20 73 71 6c 69 74 65 33 43 68 65 63 6b 4f nt sqlite3CheckO
73a90 62 6a 65 63 74 4e 61 6d 65 28 50 61 72 73 65 20 bjectName(Parse
73aa0 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 *, const char *)
73ab0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
73ac0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
73ad0 65 53 65 74 43 68 61 6e 67 65 73 28 73 71 6c 69 eSetChanges(sqli
73ae0 74 65 33 20 2a 2c 20 69 6e 74 29 3b 0a 0a 53 51 te3 *, int);..SQ
73af0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e LITE_PRIVATE con
73b00 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 st void *sqlite3
73b10 56 61 6c 75 65 54 65 78 74 28 73 71 6c 69 74 65 ValueText(sqlite
73b20 33 5f 76 61 6c 75 65 2a 2c 20 75 38 29 3b 0a 53 3_value*, u8);.S
73b30 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
73b40 74 20 73 71 6c 69 74 65 33 56 61 6c 75 65 42 79 t sqlite3ValueBy
73b50 74 65 73 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 tes(sqlite3_valu
73b60 65 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45 5f e*, u8);.SQLITE_
73b70 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
73b80 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 ite3ValueSetStr(
73b90 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2c 20 sqlite3_value*,
73ba0 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 int, const void
73bb0 2a 2c 75 38 2c 20 0a 20 20 20 20 20 20 20 20 20 *,u8, .
73bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 76 v
73bd0 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a oid(*)(void*));.
73be0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
73bf0 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65 oid sqlite3Value
73c00 46 72 65 65 28 73 71 6c 69 74 65 33 5f 76 61 6c Free(sqlite3_val
73c10 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ue*);.SQLITE_PRI
73c20 56 41 54 45 20 73 71 6c 69 74 65 33 5f 76 61 6c VATE sqlite3_val
73c30 75 65 20 2a 73 71 6c 69 74 65 33 56 61 6c 75 65 ue *sqlite3Value
73c40 4e 65 77 28 73 71 6c 69 74 65 33 20 2a 29 3b 0a New(sqlite3 *);.
73c50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
73c60 68 61 72 20 2a 73 71 6c 69 74 65 33 55 74 66 31 har *sqlite3Utf1
73c70 36 74 6f 38 28 73 71 6c 69 74 65 33 20 2a 2c 20 6to8(sqlite3 *,
73c80 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 const void*, int
73c90 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 );.#ifdef SQLITE
73ca0 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32 0a 53 51 _ENABLE_STAT2.SQ
73cb0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 LITE_PRIVATE cha
73cc0 72 20 2a 73 71 6c 69 74 65 33 55 74 66 38 74 6f r *sqlite3Utf8to
73cd0 31 36 28 73 71 6c 69 74 65 33 20 2a 2c 20 75 38 16(sqlite3 *, u8
73ce0 2c 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 69 , char *, int, i
73cf0 6e 74 20 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 nt *);.#endif.SQ
73d00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
73d10 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 6f sqlite3ValueFro
73d20 6d 45 78 70 72 28 73 71 6c 69 74 65 33 20 2a 2c mExpr(sqlite3 *,
73d30 20 45 78 70 72 20 2a 2c 20 75 38 2c 20 75 38 2c Expr *, u8, u8,
73d40 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
73d50 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
73d60 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
73d70 61 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 69 74 alueApplyAffinit
73d80 79 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 y(sqlite3_value
73d90 2a 2c 20 75 38 2c 20 75 38 29 3b 0a 23 69 66 6e *, u8, u8);.#ifn
73da0 64 65 66 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 def SQLITE_AMALG
73db0 41 4d 41 54 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 AMATION.SQLITE_P
73dc0 52 49 56 41 54 45 20 63 6f 6e 73 74 20 75 6e 73 RIVATE const uns
73dd0 69 67 6e 65 64 20 63 68 61 72 20 73 71 6c 69 74 igned char sqlit
73de0 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 5d e3UpperToLower[]
73df0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
73e00 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 const unsigned
73e10 63 68 61 72 20 73 71 6c 69 74 65 33 43 74 79 70 char sqlite3Ctyp
73e20 65 4d 61 70 5b 5d 3b 0a 53 51 4c 49 54 45 5f 50 eMap[];.SQLITE_P
73e30 52 49 56 41 54 45 20 53 51 4c 49 54 45 5f 57 53 RIVATE SQLITE_WS
73e40 44 20 73 74 72 75 63 74 20 53 71 6c 69 74 65 33 D struct Sqlite3
73e50 43 6f 6e 66 69 67 20 73 71 6c 69 74 65 33 43 6f Config sqlite3Co
73e60 6e 66 69 67 3b 0a 53 51 4c 49 54 45 5f 50 52 49 nfig;.SQLITE_PRI
73e70 56 41 54 45 20 53 51 4c 49 54 45 5f 57 53 44 20 VATE SQLITE_WSD
73e80 46 75 6e 63 44 65 66 48 61 73 68 20 73 71 6c 69 FuncDefHash sqli
73e90 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f te3GlobalFunctio
73ea0 6e 73 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ns;.SQLITE_PRIVA
73eb0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 65 TE int sqlite3Pe
73ec0 6e 64 69 6e 67 42 79 74 65 3b 0a 23 65 6e 64 69 ndingByte;.#endi
73ed0 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 f.SQLITE_PRIVATE
73ee0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 6f void sqlite3Roo
73ef0 74 50 61 67 65 4d 6f 76 65 64 28 44 62 2a 2c 20 tPageMoved(Db*,
73f00 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 int, int);.SQLIT
73f10 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
73f20 71 6c 69 74 65 33 52 65 69 6e 64 65 78 28 50 61 qlite3Reindex(Pa
73f30 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f rse*, Token*, To
73f40 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ken*);.SQLITE_PR
73f50 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
73f60 65 33 41 6c 74 65 72 46 75 6e 63 74 69 6f 6e 73 e3AlterFunctions
73f70 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 (sqlite3*);.SQLI
73f80 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
73f90 73 71 6c 69 74 65 33 41 6c 74 65 72 52 65 6e 61 sqlite3AlterRena
73fa0 6d 65 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 20 meTable(Parse*,
73fb0 53 72 63 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a SrcList*, Token*
73fc0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
73fd0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 E int sqlite3Get
73fe0 54 6f 6b 65 6e 28 63 6f 6e 73 74 20 75 6e 73 69 Token(const unsi
73ff0 67 6e 65 64 20 63 68 61 72 20 2a 2c 20 69 6e 74 gned char *, int
74000 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
74010 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
74020 4e 65 73 74 65 64 50 61 72 73 65 28 50 61 72 73 NestedParse(Pars
74030 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c e*, const char*,
74040 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ...);.SQLITE_PR
74050 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
74060 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64 e3ExpirePrepared
74070 53 74 61 74 65 6d 65 6e 74 73 28 73 71 6c 69 74 Statements(sqlit
74080 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 e3*);.SQLITE_PRI
74090 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
740a0 43 6f 64 65 53 75 62 73 65 6c 65 63 74 28 50 61 CodeSubselect(Pa
740b0 72 73 65 20 2a 2c 20 45 78 70 72 20 2a 2c 20 69 rse *, Expr *, i
740c0 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 nt, int);.SQLITE
740d0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
740e0 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65 70 28 lite3SelectPrep(
740f0 50 61 72 73 65 2a 2c 20 53 65 6c 65 63 74 2a 2c Parse*, Select*,
74100 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 29 3b 0a NameContext*);.
74110 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
74120 6e 74 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 nt sqlite3Resolv
74130 65 45 78 70 72 4e 61 6d 65 73 28 4e 61 6d 65 43 eExprNames(NameC
74140 6f 6e 74 65 78 74 2a 2c 20 45 78 70 72 2a 29 3b ontext*, Expr*);
74150 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
74160 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 73 6f void sqlite3Reso
74170 6c 76 65 53 65 6c 65 63 74 4e 61 6d 65 73 28 50 lveSelectNames(P
74180 61 72 73 65 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 arse*, Select*,
74190 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 29 3b 0a 53 NameContext*);.S
741a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
741b0 74 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 t sqlite3Resolve
741c0 4f 72 64 65 72 47 72 6f 75 70 42 79 28 50 61 72 OrderGroupBy(Par
741d0 73 65 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 45 78 se*, Select*, Ex
741e0 70 72 4c 69 73 74 2a 2c 20 63 6f 6e 73 74 20 63 prList*, const c
741f0 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 har*);.SQLITE_PR
74200 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
74210 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28 e3ColumnDefault(
74220 56 64 62 65 20 2a 2c 20 54 61 62 6c 65 20 2a 2c Vdbe *, Table *,
74230 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 int, int);.SQLI
74240 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
74250 73 71 6c 69 74 65 33 41 6c 74 65 72 46 69 6e 69 sqlite3AlterFini
74260 73 68 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 shAddColumn(Pars
74270 65 20 2a 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a 53 e *, Token *);.S
74280 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
74290 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 42 id sqlite3AlterB
742a0 65 67 69 6e 41 64 64 43 6f 6c 75 6d 6e 28 50 61 eginAddColumn(Pa
742b0 72 73 65 20 2a 2c 20 53 72 63 4c 69 73 74 20 2a rse *, SrcList *
742c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
742d0 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 E CollSeq *sqlit
742e0 65 33 47 65 74 43 6f 6c 6c 53 65 71 28 73 71 6c e3GetCollSeq(sql
742f0 69 74 65 33 2a 2c 20 75 38 2c 20 43 6f 6c 6c 53 ite3*, u8, CollS
74300 65 71 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 eq *, const char
74310 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
74320 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 33 41 TE char sqlite3A
74330 66 66 69 6e 69 74 79 54 79 70 65 28 63 6f 6e 73 ffinityType(cons
74340 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 t char*);.SQLITE
74350 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
74360 6c 69 74 65 33 41 6e 61 6c 79 7a 65 28 50 61 72 lite3Analyze(Par
74370 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b se*, Token*, Tok
74380 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 en*);.SQLITE_PRI
74390 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
743a0 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 InvokeBusyHandle
743b0 72 28 42 75 73 79 48 61 6e 64 6c 65 72 2a 29 3b r(BusyHandler*);
743c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
743d0 69 6e 74 20 73 71 6c 69 74 65 33 46 69 6e 64 44 int sqlite3FindD
743e0 62 28 73 71 6c 69 74 65 33 2a 2c 20 54 6f 6b 65 b(sqlite3*, Toke
743f0 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 n*);.SQLITE_PRIV
74400 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 ATE int sqlite3F
74410 69 6e 64 44 62 4e 61 6d 65 28 73 71 6c 69 74 65 indDbName(sqlite
74420 33 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 3 *, const char
74430 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
74440 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 6e TE int sqlite3An
74450 61 6c 79 73 69 73 4c 6f 61 64 28 73 71 6c 69 74 alysisLoad(sqlit
74460 65 33 2a 2c 69 6e 74 20 69 44 42 29 3b 0a 53 51 e3*,int iDB);.SQ
74470 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
74480 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 49 d sqlite3DeleteI
74490 6e 64 65 78 53 61 6d 70 6c 65 73 28 49 6e 64 65 ndexSamples(Inde
744a0 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 x*);.SQLITE_PRIV
744b0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
744c0 44 65 66 61 75 6c 74 52 6f 77 45 73 74 28 49 6e DefaultRowEst(In
744d0 64 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 dex*);.SQLITE_PR
744e0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
744f0 65 33 52 65 67 69 73 74 65 72 4c 69 6b 65 46 75 e3RegisterLikeFu
74500 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 2a nctions(sqlite3*
74510 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
74520 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
74530 65 33 49 73 4c 69 6b 65 46 75 6e 63 74 69 6f 6e e3IsLikeFunction
74540 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c (sqlite3*,Expr*,
74550 69 6e 74 2a 2c 63 68 61 72 2a 29 3b 0a 53 51 4c int*,char*);.SQL
74560 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
74570 20 73 71 6c 69 74 65 33 4d 69 6e 69 6d 75 6d 46 sqlite3MinimumF
74580 69 6c 65 46 6f 72 6d 61 74 28 50 61 72 73 65 2a ileFormat(Parse*
74590 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c , int, int);.SQL
745a0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
745b0 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 46 72 sqlite3SchemaFr
745c0 65 65 28 76 6f 69 64 20 2a 29 3b 0a 53 51 4c 49 ee(void *);.SQLI
745d0 54 45 5f 50 52 49 56 41 54 45 20 53 63 68 65 6d TE_PRIVATE Schem
745e0 61 20 2a 73 71 6c 69 74 65 33 53 63 68 65 6d 61 a *sqlite3Schema
745f0 47 65 74 28 73 71 6c 69 74 65 33 20 2a 2c 20 42 Get(sqlite3 *, B
74600 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f tree *);.SQLITE_
74610 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
74620 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 te3SchemaToIndex
74630 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 63 (sqlite3 *db, Sc
74640 68 65 6d 61 20 2a 29 3b 0a 53 51 4c 49 54 45 5f hema *);.SQLITE_
74650 50 52 49 56 41 54 45 20 4b 65 79 49 6e 66 6f 20 PRIVATE KeyInfo
74660 2a 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 *sqlite3IndexKey
74670 69 6e 66 6f 28 50 61 72 73 65 20 2a 2c 20 49 6e info(Parse *, In
74680 64 65 78 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 dex *);.SQLITE_P
74690 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
746a0 65 33 43 72 65 61 74 65 46 75 6e 63 28 73 71 6c e3CreateFunc(sql
746b0 69 74 65 33 20 2a 2c 20 63 6f 6e 73 74 20 63 68 ite3 *, const ch
746c0 61 72 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 ar *, int, int,
746d0 76 6f 69 64 20 2a 2c 20 0a 20 20 76 6f 69 64 20 void *, . void
746e0 28 2a 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 (*)(sqlite3_cont
746f0 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 ext*,int,sqlite3
74700 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a 20 20 76 6f _value **),. vo
74710 69 64 20 28 2a 29 28 73 71 6c 69 74 65 33 5f 63 id (*)(sqlite3_c
74720 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 ontext*,int,sqli
74730 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 20 76 te3_value **), v
74740 6f 69 64 20 28 2a 29 28 73 71 6c 69 74 65 33 5f oid (*)(sqlite3_
74750 63 6f 6e 74 65 78 74 2a 29 29 3b 0a 53 51 4c 49 context*));.SQLI
74760 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
74770 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 73 71 qlite3ApiExit(sq
74780 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 29 3b lite3 *db, int);
74790 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
747a0 69 6e 74 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 int sqlite3OpenT
747b0 65 6d 70 44 61 74 61 62 61 73 65 28 50 61 72 73 empDatabase(Pars
747c0 65 20 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 e *);..SQLITE_PR
747d0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
747e0 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 53 e3StrAccumInit(S
747f0 74 72 41 63 63 75 6d 2a 2c 20 63 68 61 72 2a 2c trAccum*, char*,
74800 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 int, int);.SQLI
74810 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
74820 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 sqlite3StrAccumA
74830 70 70 65 6e 64 28 53 74 72 41 63 63 75 6d 2a 2c ppend(StrAccum*,
74840 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 29 const char*,int)
74850 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
74860 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 53 74 char *sqlite3St
74870 72 41 63 63 75 6d 46 69 6e 69 73 68 28 53 74 72 rAccumFinish(Str
74880 41 63 63 75 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f Accum*);.SQLITE_
74890 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
748a0 69 74 65 33 53 74 72 41 63 63 75 6d 52 65 73 65 ite3StrAccumRese
748b0 74 28 53 74 72 41 63 63 75 6d 2a 29 3b 0a 53 51 t(StrAccum*);.SQ
748c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
748d0 64 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 d sqlite3SelectD
748e0 65 73 74 49 6e 69 74 28 53 65 6c 65 63 74 44 65 estInit(SelectDe
748f0 73 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 st*,int,int);.SQ
74900 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 LITE_PRIVATE Exp
74910 72 20 2a 73 71 6c 69 74 65 33 43 72 65 61 74 65 r *sqlite3Create
74920 43 6f 6c 75 6d 6e 45 78 70 72 28 73 71 6c 69 74 ColumnExpr(sqlit
74930 65 33 20 2a 2c 20 53 72 63 4c 69 73 74 20 2a 2c e3 *, SrcList *,
74940 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 0a 53 51 4c int, int);..SQL
74950 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
74960 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 52 65 sqlite3BackupRe
74970 73 74 61 72 74 28 73 71 6c 69 74 65 33 5f 62 61 start(sqlite3_ba
74980 63 6b 75 70 20 2a 29 3b 0a 53 51 4c 49 54 45 5f ckup *);.SQLITE_
74990 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
749a0 69 74 65 33 42 61 63 6b 75 70 55 70 64 61 74 65 ite3BackupUpdate
749b0 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 (sqlite3_backup
749c0 2a 2c 20 50 67 6e 6f 2c 20 63 6f 6e 73 74 20 75 *, Pgno, const u
749d0 38 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 8 *);../*.** The
749e0 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68 interface to th
749f0 65 20 4c 45 4d 4f 4e 2d 67 65 6e 65 72 61 74 65 e LEMON-generate
74a00 64 20 70 61 72 73 65 72 0a 2a 2f 0a 53 51 4c 49 d parser.*/.SQLI
74a10 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
74a20 2a 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 6c *sqlite3ParserAl
74a30 6c 6f 63 28 76 6f 69 64 2a 28 2a 29 28 73 69 7a loc(void*(*)(siz
74a40 65 5f 74 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 e_t));.SQLITE_PR
74a50 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
74a60 65 33 50 61 72 73 65 72 46 72 65 65 28 76 6f 69 e3ParserFree(voi
74a70 64 2a 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 d*, void(*)(void
74a80 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *));.SQLITE_PRIV
74a90 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
74aa0 50 61 72 73 65 72 28 76 6f 69 64 2a 2c 20 69 6e Parser(void*, in
74ab0 74 2c 20 54 6f 6b 65 6e 2c 20 50 61 72 73 65 2a t, Token, Parse*
74ac0 29 3b 0a 23 69 66 64 65 66 20 59 59 54 52 41 43 );.#ifdef YYTRAC
74ad0 4b 4d 41 58 53 54 41 43 4b 44 45 50 54 48 0a 53 KMAXSTACKDEPTH.S
74ae0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
74af0 69 6e 74 20 73 71 6c 69 74 65 33 50 61 72 73 65 int sqlite3Parse
74b00 72 53 74 61 63 6b 50 65 61 6b 28 76 6f 69 64 2a rStackPeak(void*
74b10 29 3b 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 );.#endif..SQLIT
74b20 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
74b30 71 6c 69 74 65 33 41 75 74 6f 4c 6f 61 64 45 78 qlite3AutoLoadEx
74b40 74 65 6e 73 69 6f 6e 73 28 73 71 6c 69 74 65 33 tensions(sqlite3
74b50 2a 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 *);.#ifndef SQLI
74b60 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 TE_OMIT_LOAD_EXT
74b70 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 ENSION.SQLITE_PR
74b80 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
74b90 69 74 65 33 43 6c 6f 73 65 45 78 74 65 6e 73 69 ite3CloseExtensi
74ba0 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23 ons(sqlite3*);.#
74bb0 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71 else.# define sq
74bc0 6c 69 74 65 33 43 6c 6f 73 65 45 78 74 65 6e 73 lite3CloseExtens
74bd0 69 6f 6e 73 28 58 29 0a 23 65 6e 64 69 66 0a 0a ions(X).#endif..
74be0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
74bf0 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 MIT_SHARED_CACHE
74c00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
74c10 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 54 61 void sqlite3Ta
74c20 62 6c 65 4c 6f 63 6b 28 50 61 72 73 65 20 2a 2c bleLock(Parse *,
74c30 20 69 6e 74 2c 20 69 6e 74 2c 20 75 38 2c 20 63 int, int, u8, c
74c40 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 23 65 onst char *);.#e
74c50 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 lse. #define sq
74c60 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 76 lite3TableLock(v
74c70 2c 77 2c 78 2c 79 2c 7a 29 0a 23 65 6e 64 69 66 ,w,x,y,z).#endif
74c80 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
74c90 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 TEST.SQLITE_PRIV
74ca0 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 ATE int sqlite
74cb0 33 55 74 66 38 54 6f 38 28 75 6e 73 69 67 6e 65 3Utf8To8(unsigne
74cc0 64 20 63 68 61 72 2a 29 3b 0a 23 65 6e 64 69 66 d char*);.#endif
74cd0 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
74ce0 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
74cf0 45 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 E.# define sqli
74d00 74 65 33 56 74 61 62 43 6c 65 61 72 28 59 29 0a te3VtabClear(Y).
74d10 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 # define sqlite
74d20 33 56 74 61 62 53 79 6e 63 28 58 2c 59 29 20 53 3VtabSync(X,Y) S
74d30 51 4c 49 54 45 5f 4f 4b 0a 23 20 20 64 65 66 69 QLITE_OK.# defi
74d40 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62 52 6f ne sqlite3VtabRo
74d50 6c 6c 62 61 63 6b 28 58 29 0a 23 20 20 64 65 66 llback(X).# def
74d60 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62 43 ine sqlite3VtabC
74d70 6f 6d 6d 69 74 28 58 29 0a 23 20 20 64 65 66 69 ommit(X).# defi
74d80 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62 49 6e ne sqlite3VtabIn
74d90 53 79 6e 63 28 64 62 29 20 30 0a 23 20 20 64 65 Sync(db) 0.# de
74da0 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62 fine sqlite3Vtab
74db0 4c 6f 63 6b 28 58 29 20 0a 23 20 20 64 65 66 69 Lock(X) .# defi
74dc0 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e ne sqlite3VtabUn
74dd0 6c 6f 63 6b 28 58 29 0a 23 20 20 64 65 66 69 6e lock(X).# defin
74de0 65 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c e sqlite3VtabUnl
74df0 6f 63 6b 4c 69 73 74 28 58 29 0a 23 65 6c 73 65 ockList(X).#else
74e00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
74e10 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 void sqlite3V
74e20 74 61 62 43 6c 65 61 72 28 54 61 62 6c 65 2a 29 tabClear(Table*)
74e30 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
74e40 20 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56 int sqlite3V
74e50 74 61 62 53 79 6e 63 28 73 71 6c 69 74 65 33 20 tabSync(sqlite3
74e60 2a 64 62 2c 20 63 68 61 72 20 2a 2a 29 3b 0a 53 *db, char **);.S
74e70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
74e80 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 int sqlite3Vtab
74e90 52 6f 6c 6c 62 61 63 6b 28 73 71 6c 69 74 65 33 Rollback(sqlite3
74ea0 20 2a 64 62 29 3b 0a 53 51 4c 49 54 45 5f 50 52 *db);.SQLITE_PR
74eb0 49 56 41 54 45 20 20 20 20 69 6e 74 20 73 71 6c IVATE int sql
74ec0 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 74 28 73 ite3VtabCommit(s
74ed0 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c qlite3 *db);.SQL
74ee0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 76 ITE_PRIVATE v
74ef0 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 4c oid sqlite3VtabL
74f00 6f 63 6b 28 56 54 61 62 6c 65 20 2a 29 3b 0a 53 ock(VTable *);.S
74f10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
74f20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 void sqlite3Vta
74f30 62 55 6e 6c 6f 63 6b 28 56 54 61 62 6c 65 20 2a bUnlock(VTable *
74f40 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
74f50 45 20 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 E void sqlite
74f60 33 56 74 61 62 55 6e 6c 6f 63 6b 4c 69 73 74 28 3VtabUnlockList(
74f70 73 71 6c 69 74 65 33 2a 29 3b 0a 23 20 20 64 65 sqlite3*);.# de
74f80 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62 fine sqlite3Vtab
74f90 49 6e 53 79 6e 63 28 64 62 29 20 28 28 64 62 29 InSync(db) ((db)
74fa0 2d 3e 6e 56 54 72 61 6e 73 3e 30 20 26 26 20 28 ->nVTrans>0 && (
74fb0 64 62 29 2d 3e 61 56 54 72 61 6e 73 3d 3d 30 29 db)->aVTrans==0)
74fc0 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 .#endif.SQLITE_P
74fd0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
74fe0 74 65 33 56 74 61 62 4d 61 6b 65 57 72 69 74 61 te3VtabMakeWrita
74ff0 62 6c 65 28 50 61 72 73 65 2a 2c 54 61 62 6c 65 ble(Parse*,Table
75000 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
75010 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
75020 74 61 62 42 65 67 69 6e 50 61 72 73 65 28 50 61 tabBeginParse(Pa
75030 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f rse*, Token*, To
75040 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 ken*, Token*);.S
75050 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
75060 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 46 69 id sqlite3VtabFi
75070 6e 69 73 68 50 61 72 73 65 28 50 61 72 73 65 2a nishParse(Parse*
75080 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 , Token*);.SQLIT
75090 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
750a0 71 6c 69 74 65 33 56 74 61 62 41 72 67 49 6e 69 qlite3VtabArgIni
750b0 74 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 t(Parse*);.SQLIT
750c0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
750d0 71 6c 69 74 65 33 56 74 61 62 41 72 67 45 78 74 qlite3VtabArgExt
750e0 65 6e 64 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 end(Parse*, Toke
750f0 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 n*);.SQLITE_PRIV
75100 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
75110 74 61 62 43 61 6c 6c 43 72 65 61 74 65 28 73 71 tabCallCreate(sq
75120 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 63 6f 6e lite3*, int, con
75130 73 74 20 63 68 61 72 20 2a 2c 20 63 68 61 72 20 st char *, char
75140 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 **);.SQLITE_PRIV
75150 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
75160 74 61 62 43 61 6c 6c 43 6f 6e 6e 65 63 74 28 50 tabCallConnect(P
75170 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 29 3b 0a arse*, Table*);.
75180 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
75190 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 61 nt sqlite3VtabCa
751a0 6c 6c 44 65 73 74 72 6f 79 28 73 71 6c 69 74 65 llDestroy(sqlite
751b0 33 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 3*, int, const c
751c0 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 har *);.SQLITE_P
751d0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
751e0 65 33 56 74 61 62 42 65 67 69 6e 28 73 71 6c 69 e3VtabBegin(sqli
751f0 74 65 33 20 2a 2c 20 56 54 61 62 6c 65 20 2a 29 te3 *, VTable *)
75200 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
75210 20 46 75 6e 63 44 65 66 20 2a 73 71 6c 69 74 65 FuncDef *sqlite
75220 33 56 74 61 62 4f 76 65 72 6c 6f 61 64 46 75 6e 3VtabOverloadFun
75230 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 20 2a 2c ction(sqlite3 *,
75240 46 75 6e 63 44 65 66 2a 2c 20 69 6e 74 20 6e 41 FuncDef*, int nA
75250 72 67 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 rg, Expr*);.SQLI
75260 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
75270 73 71 6c 69 74 65 33 49 6e 76 61 6c 69 64 46 75 sqlite3InvalidFu
75280 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 5f 63 nction(sqlite3_c
75290 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 ontext*,int,sqli
752a0 74 65 33 5f 76 61 6c 75 65 2a 2a 29 3b 0a 53 51 te3_value**);.SQ
752b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
752c0 20 73 71 6c 69 74 65 33 54 72 61 6e 73 66 65 72 sqlite3Transfer
752d0 42 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 65 33 Bindings(sqlite3
752e0 5f 73 74 6d 74 20 2a 2c 20 73 71 6c 69 74 65 33 _stmt *, sqlite3
752f0 5f 73 74 6d 74 20 2a 29 3b 0a 53 51 4c 49 54 45 _stmt *);.SQLITE
75300 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
75310 69 74 65 33 52 65 70 72 65 70 61 72 65 28 56 64 ite3Reprepare(Vd
75320 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 be*);.SQLITE_PRI
75330 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
75340 33 45 78 70 72 4c 69 73 74 43 68 65 63 6b 4c 65 3ExprListCheckLe
75350 6e 67 74 68 28 50 61 72 73 65 2a 2c 20 45 78 70 ngth(Parse*, Exp
75360 72 4c 69 73 74 2a 2c 20 63 6f 6e 73 74 20 63 68 rList*, const ch
75370 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ar*);.SQLITE_PRI
75380 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 VATE CollSeq *sq
75390 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d 70 61 lite3BinaryCompa
753a0 72 65 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 reCollSeq(Parse
753b0 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78 70 72 20 *, Expr *, Expr
753c0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
753d0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 54 65 TE int sqlite3Te
753e0 6d 70 49 6e 4d 65 6d 6f 72 79 28 63 6f 6e 73 74 mpInMemory(const
753f0 20 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 sqlite3*);.SQLI
75400 54 45 5f 50 52 49 56 41 54 45 20 56 54 61 62 6c TE_PRIVATE VTabl
75410 65 20 2a 73 71 6c 69 74 65 33 47 65 74 56 54 61 e *sqlite3GetVTa
75420 62 6c 65 28 73 71 6c 69 74 65 33 2a 2c 20 54 61 ble(sqlite3*, Ta
75430 62 6c 65 2a 29 3b 0a 0a 2f 2a 20 44 65 63 6c 61 ble*);../* Decla
75440 72 61 74 69 6f 6e 73 20 66 6f 72 20 66 75 6e 63 rations for func
75450 74 69 6f 6e 73 20 69 6e 20 66 6b 65 79 2e 63 2e tions in fkey.c.
75460 20 41 6c 6c 20 6f 66 20 74 68 65 73 65 20 61 72 All of these ar
75470 65 20 72 65 70 6c 61 63 65 64 20 62 79 0a 2a 2a e replaced by.**
75480 20 6e 6f 2d 6f 70 20 6d 61 63 72 6f 73 20 69 66 no-op macros if
75490 20 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 OMIT_FOREIGN_KE
754a0 59 20 69 73 20 64 65 66 69 6e 65 64 2e 20 49 6e Y is defined. In
754b0 20 74 68 69 73 20 63 61 73 65 20 6e 6f 20 66 6f this case no fo
754c0 72 65 69 67 6e 0a 2a 2a 20 6b 65 79 20 66 75 6e reign.** key fun
754d0 63 74 69 6f 6e 61 6c 69 74 79 20 69 73 20 61 76 ctionality is av
754e0 61 69 6c 61 62 6c 65 2e 20 49 66 20 4f 4d 49 54 ailable. If OMIT
754f0 5f 54 52 49 47 47 45 52 20 69 73 20 64 65 66 69 _TRIGGER is defi
75500 6e 65 64 20 62 75 74 0a 2a 2a 20 4f 4d 49 54 5f ned but.** OMIT_
75510 46 4f 52 45 49 47 4e 5f 4b 45 59 20 69 73 20 6e FOREIGN_KEY is n
75520 6f 74 2c 20 6f 6e 6c 79 20 73 6f 6d 65 20 6f 66 ot, only some of
75530 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 the functions a
75540 72 65 20 6e 6f 2d 6f 70 65 64 2e 20 49 6e 0a 2a re no-oped. In.*
75550 2a 20 74 68 69 73 20 63 61 73 65 20 66 6f 72 65 * this case fore
75560 69 67 6e 20 6b 65 79 73 20 61 72 65 20 70 61 72 ign keys are par
75570 73 65 64 2c 20 62 75 74 20 6e 6f 20 6f 74 68 65 sed, but no othe
75580 72 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 20 r functionality
75590 69 73 20 0a 2a 2a 20 70 72 6f 76 69 64 65 64 20 is .** provided
755a0 28 65 6e 66 6f 72 63 65 6d 65 6e 74 20 6f 66 20 (enforcement of
755b0 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 72 FK constraints r
755c0 65 71 75 69 72 65 73 20 74 68 65 20 74 72 69 67 equires the trig
755d0 67 65 72 73 20 73 75 62 2d 73 79 73 74 65 6d 29 gers sub-system)
755e0 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 ..*/.#if !define
755f0 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f d(SQLITE_OMIT_FO
75600 52 45 49 47 4e 5f 4b 45 59 29 20 26 26 20 21 64 REIGN_KEY) && !d
75610 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
75620 49 54 5f 54 52 49 47 47 45 52 29 0a 53 51 4c 49 IT_TRIGGER).SQLI
75630 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
75640 64 20 73 71 6c 69 74 65 33 46 6b 43 68 65 63 6b d sqlite3FkCheck
75650 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c (Parse*, Table*,
75660 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 int, int);.SQLI
75670 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
75680 64 20 73 71 6c 69 74 65 33 46 6b 44 72 6f 70 54 d sqlite3FkDropT
75690 61 62 6c 65 28 50 61 72 73 65 2a 2c 20 53 72 63 able(Parse*, Src
756a0 4c 69 73 74 20 2a 2c 20 54 61 62 6c 65 2a 29 3b List *, Table*);
756b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
756c0 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 6b void sqlite3Fk
756d0 41 63 74 69 6f 6e 73 28 50 61 72 73 65 2a 2c 20 Actions(Parse*,
756e0 54 61 62 6c 65 2a 2c 20 45 78 70 72 4c 69 73 74 Table*, ExprList
756f0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
75700 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 PRIVATE int sq
75710 6c 69 74 65 33 46 6b 52 65 71 75 69 72 65 64 28 lite3FkRequired(
75720 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 Parse*, Table*,
75730 69 6e 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 int*, int);.SQLI
75740 54 45 5f 50 52 49 56 41 54 45 20 20 20 75 33 32 TE_PRIVATE u32
75750 20 73 71 6c 69 74 65 33 46 6b 4f 6c 64 6d 61 73 sqlite3FkOldmas
75760 6b 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a k(Parse*, Table*
75770 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
75780 45 20 20 20 46 4b 65 79 20 2a 73 71 6c 69 74 65 E FKey *sqlite
75790 33 46 6b 52 65 66 65 72 65 6e 63 65 73 28 54 61 3FkReferences(Ta
757a0 62 6c 65 20 2a 29 3b 0a 23 65 6c 73 65 0a 20 20 ble *);.#else.
757b0 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 46 #define sqlite3F
757c0 6b 41 63 74 69 6f 6e 73 28 61 2c 62 2c 63 2c 64 kActions(a,b,c,d
757d0 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 ). #define sqli
757e0 74 65 33 46 6b 43 68 65 63 6b 28 61 2c 62 2c 63 te3FkCheck(a,b,c
757f0 2c 64 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71 ,d). #define sq
75800 6c 69 74 65 33 46 6b 44 72 6f 70 54 61 62 6c 65 lite3FkDropTable
75810 28 61 2c 62 2c 63 29 0a 20 20 23 64 65 66 69 6e (a,b,c). #defin
75820 65 20 73 71 6c 69 74 65 33 46 6b 4f 6c 64 6d 61 e sqlite3FkOldma
75830 73 6b 28 61 2c 62 29 20 20 20 20 20 20 30 0a 20 sk(a,b) 0.
75840 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 #define sqlite3
75850 46 6b 52 65 71 75 69 72 65 64 28 61 2c 62 2c 63 FkRequired(a,b,c
75860 2c 64 29 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 ,d) 0.#endif.#if
75870 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
75880 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 53 51 4c _FOREIGN_KEY.SQL
75890 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
758a0 69 64 20 73 71 6c 69 74 65 33 46 6b 44 65 6c 65 id sqlite3FkDele
758b0 74 65 28 54 61 62 6c 65 2a 29 3b 0a 23 65 6c 73 te(Table*);.#els
758c0 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 e. #define sqli
758d0 74 65 33 46 6b 44 65 6c 65 74 65 28 61 29 0a 23 te3FkDelete(a).#
758e0 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 76 endif.../*.** Av
758f0 61 69 6c 61 62 6c 65 20 66 61 75 6c 74 20 69 6e ailable fault in
75900 6a 65 63 74 6f 72 73 2e 20 20 53 68 6f 75 6c 64 jectors. Should
75910 20 62 65 20 6e 75 6d 62 65 72 65 64 20 62 65 67 be numbered beg
75920 69 6e 6e 69 6e 67 20 77 69 74 68 20 30 2e 0a 2a inning with 0..*
75930 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
75940 5f 46 41 55 4c 54 49 4e 4a 45 43 54 4f 52 5f 4d _FAULTINJECTOR_M
75950 41 4c 4c 4f 43 20 20 20 20 20 30 0a 23 64 65 66 ALLOC 0.#def
75960 69 6e 65 20 53 51 4c 49 54 45 5f 46 41 55 4c 54 ine SQLITE_FAULT
75970 49 4e 4a 45 43 54 4f 52 5f 43 4f 55 4e 54 20 20 INJECTOR_COUNT
75980 20 20 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 1../*.** The
75990 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68 interface to th
759a0 65 20 63 6f 64 65 20 69 6e 20 66 61 75 6c 74 2e e code in fault.
759b0 63 20 75 73 65 64 20 66 6f 72 20 69 64 65 6e 74 c used for ident
759c0 69 66 79 69 6e 67 20 22 62 65 6e 69 67 6e 22 0a ifying "benign".
759d0 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 ** malloc failur
759e0 65 73 2e 20 54 68 69 73 20 69 73 20 6f 6e 6c 79 es. This is only
759f0 20 70 72 65 73 65 6e 74 20 69 66 20 53 51 4c 49 present if SQLI
75a00 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f TE_OMIT_BUILTIN_
75a10 54 45 53 54 0a 2a 2a 20 69 73 20 6e 6f 74 20 64 TEST.** is not d
75a20 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 efined..*/.#ifnd
75a30 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 ef SQLITE_OMIT_B
75a40 55 49 4c 54 49 4e 5f 54 45 53 54 0a 53 51 4c 49 UILTIN_TEST.SQLI
75a50 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
75a60 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 d sqlite3BeginBe
75a70 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 29 nignMalloc(void)
75a80 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
75a90 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 void sqlite3E
75aa0 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76 ndBenignMalloc(v
75ab0 6f 69 64 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 oid);.#else. #d
75ac0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 65 67 efine sqlite3Beg
75ad0 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 inBenignMalloc()
75ae0 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 . #define sqlit
75af0 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f e3EndBenignMallo
75b00 63 28 29 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 c().#endif..#def
75b10 69 6e 65 20 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 ine IN_INDEX_ROW
75b20 49 44 20 20 20 20 20 20 20 20 20 20 20 31 0a 23 ID 1.#
75b30 64 65 66 69 6e 65 20 49 4e 5f 49 4e 44 45 58 5f define IN_INDEX_
75b40 45 50 48 20 20 20 20 20 20 20 20 20 20 20 20 20 EPH
75b50 32 0a 23 64 65 66 69 6e 65 20 49 4e 5f 49 4e 44 2.#define IN_IND
75b60 45 58 5f 49 4e 44 45 58 20 20 20 20 20 20 20 20 EX_INDEX
75b70 20 20 20 33 0a 53 51 4c 49 54 45 5f 50 52 49 56 3.SQLITE_PRIV
75b80 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 ATE int sqlite3F
75b90 69 6e 64 49 6e 49 6e 64 65 78 28 50 61 72 73 65 indInIndex(Parse
75ba0 20 2a 2c 20 45 78 70 72 20 2a 2c 20 69 6e 74 2a *, Expr *, int*
75bb0 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 );..#ifdef SQLIT
75bc0 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f E_ENABLE_ATOMIC_
75bd0 57 52 49 54 45 0a 53 51 4c 49 54 45 5f 50 52 49 WRITE.SQLITE_PRI
75be0 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 VATE int sqlit
75bf0 65 33 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 73 71 e3JournalOpen(sq
75c00 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e lite3_vfs *, con
75c10 73 74 20 63 68 61 72 20 2a 2c 20 73 71 6c 69 74 st char *, sqlit
75c20 65 33 5f 66 69 6c 65 20 2a 2c 20 69 6e 74 2c 20 e3_file *, int,
75c30 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
75c40 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 VATE int sqlit
75c50 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 73 71 e3JournalSize(sq
75c60 6c 69 74 65 33 5f 76 66 73 20 2a 29 3b 0a 53 51 lite3_vfs *);.SQ
75c70 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 LITE_PRIVATE i
75c80 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 nt sqlite3Journa
75c90 6c 43 72 65 61 74 65 28 73 71 6c 69 74 65 33 5f lCreate(sqlite3_
75ca0 66 69 6c 65 20 2a 29 3b 0a 23 65 6c 73 65 0a 20 file *);.#else.
75cb0 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 #define sqlite3
75cc0 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 70 56 66 73 JournalSize(pVfs
75cd0 29 20 28 28 70 56 66 73 29 2d 3e 73 7a 4f 73 46 ) ((pVfs)->szOsF
75ce0 69 6c 65 29 0a 23 65 6e 64 69 66 0a 0a 53 51 4c ile).#endif..SQL
75cf0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
75d00 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e sqlite3MemJourn
75d10 61 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 66 alOpen(sqlite3_f
75d20 69 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ile *);.SQLITE_P
75d30 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
75d40 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 e3MemJournalSize
75d50 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 (void);.SQLITE_P
75d60 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
75d70 65 33 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 73 e3IsMemJournal(s
75d80 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0a qlite3_file *);.
75d90 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f .#if SQLITE_MAX_
75da0 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 53 51 4c EXPR_DEPTH>0.SQL
75db0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
75dc0 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 id sqlite3ExprSe
75dd0 74 48 65 69 67 68 74 28 50 61 72 73 65 20 2a 70 tHeight(Parse *p
75de0 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 29 3b Parse, Expr *p);
75df0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
75e00 20 20 69 6e 74 20 73 71 6c 69 74 65 33 53 65 6c int sqlite3Sel
75e10 65 63 74 45 78 70 72 48 65 69 67 68 74 28 53 65 ectExprHeight(Se
75e20 6c 65 63 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f lect *);.SQLITE_
75e30 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 PRIVATE int sq
75e40 6c 69 74 65 33 45 78 70 72 43 68 65 63 6b 48 65 lite3ExprCheckHe
75e50 69 67 68 74 28 50 61 72 73 65 2a 2c 20 69 6e 74 ight(Parse*, int
75e60 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 );.#else. #defi
75e70 6e 65 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 ne sqlite3ExprSe
75e80 74 48 65 69 67 68 74 28 78 2c 79 29 0a 20 20 23 tHeight(x,y). #
75e90 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 65 define sqlite3Se
75ea0 6c 65 63 74 45 78 70 72 48 65 69 67 68 74 28 78 lectExprHeight(x
75eb0 29 20 30 0a 20 20 23 64 65 66 69 6e 65 20 73 71 ) 0. #define sq
75ec0 6c 69 74 65 33 45 78 70 72 43 68 65 63 6b 48 65 lite3ExprCheckHe
75ed0 69 67 68 74 28 78 2c 79 29 0a 23 65 6e 64 69 66 ight(x,y).#endif
75ee0 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ..SQLITE_PRIVATE
75ef0 20 75 33 32 20 73 71 6c 69 74 65 33 47 65 74 34 u32 sqlite3Get4
75f00 62 79 74 65 28 63 6f 6e 73 74 20 75 38 2a 29 3b byte(const u8*);
75f10 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
75f20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 75 74 34 void sqlite3Put4
75f30 62 79 74 65 28 75 38 2a 2c 20 75 33 32 29 3b 0a byte(u8*, u32);.
75f40 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 .#ifdef SQLITE_E
75f50 4e 41 42 4c 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 NABLE_UNLOCK_NOT
75f60 49 46 59 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 IFY.SQLITE_PRIVA
75f70 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
75f80 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 3ConnectionBlock
75f90 65 64 28 73 71 6c 69 74 65 33 20 2a 2c 20 73 71 ed(sqlite3 *, sq
75fa0 6c 69 74 65 33 20 2a 29 3b 0a 53 51 4c 49 54 45 lite3 *);.SQLITE
75fb0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
75fc0 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f sqlite3Connectio
75fd0 6e 55 6e 6c 6f 63 6b 65 64 28 73 71 6c 69 74 65 nUnlocked(sqlite
75fe0 33 20 2a 64 62 29 3b 0a 53 51 4c 49 54 45 5f 50 3 *db);.SQLITE_P
75ff0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
76000 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 43 lite3ConnectionC
76010 6c 6f 73 65 64 28 73 71 6c 69 74 65 33 20 2a 64 losed(sqlite3 *d
76020 62 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 b);.#else. #def
76030 69 6e 65 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 ine sqlite3Conne
76040 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 78 2c 79 ctionBlocked(x,y
76050 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 ). #define sqli
76060 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c te3ConnectionUnl
76070 6f 63 6b 65 64 28 78 29 0a 20 20 23 64 65 66 69 ocked(x). #defi
76080 6e 65 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 ne sqlite3Connec
76090 74 69 6f 6e 43 6c 6f 73 65 64 28 78 29 0a 23 65 tionClosed(x).#e
760a0 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c ndif..#ifdef SQL
760b0 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 ITE_DEBUG.SQLITE
760c0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
760d0 73 71 6c 69 74 65 33 50 61 72 73 65 72 54 72 61 sqlite3ParserTra
760e0 63 65 28 46 49 4c 45 2a 2c 20 63 68 61 72 20 2a ce(FILE*, char *
760f0 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a );.#endif../*.**
76100 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f 45 If the SQLITE_E
76110 4e 41 42 4c 45 20 49 4f 54 52 41 43 45 20 65 78 NABLE IOTRACE ex
76120 69 73 74 73 20 74 68 65 6e 20 74 68 65 20 67 6c ists then the gl
76130 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 0a 2a 2a obal variable.**
76140 20 73 71 6c 69 74 65 33 49 6f 54 72 61 63 65 20 sqlite3IoTrace
76150 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
76160 61 20 70 72 69 6e 74 66 2d 6c 69 6b 65 20 72 6f a printf-like ro
76170 75 74 69 6e 65 20 75 73 65 64 20 74 6f 0a 2a 2a utine used to.**
76180 20 70 72 69 6e 74 20 49 2f 4f 20 74 72 61 63 69 print I/O traci
76190 6e 67 20 6d 65 73 73 61 67 65 73 2e 20 0a 2a 2f ng messages. .*/
761a0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 .#ifdef SQLITE_E
761b0 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 0a 23 20 NABLE_IOTRACE.#
761c0 64 65 66 69 6e 65 20 49 4f 54 52 41 43 45 28 41 define IOTRACE(A
761d0 29 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 6f ) if( sqlite3Io
761e0 54 72 61 63 65 20 29 7b 20 73 71 6c 69 74 65 33 Trace ){ sqlite3
761f0 49 6f 54 72 61 63 65 20 41 3b 20 7d 0a 53 51 4c IoTrace A; }.SQL
76200 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
76210 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 49 4f id sqlite3VdbeIO
76220 54 72 61 63 65 53 71 6c 28 56 64 62 65 2a 29 3b TraceSql(Vdbe*);
76230 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
76240 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 49 6f void (*sqlite3Io
76250 54 72 61 63 65 29 28 63 6f 6e 73 74 20 63 68 61 Trace)(const cha
76260 72 2a 2c 2e 2e 2e 29 3b 0a 23 65 6c 73 65 0a 23 r*,...);.#else.#
76270 20 64 65 66 69 6e 65 20 49 4f 54 52 41 43 45 28 define IOTRACE(
76280 41 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 A).# define sqli
76290 74 65 33 56 64 62 65 49 4f 54 72 61 63 65 53 71 te3VdbeIOTraceSq
762a0 6c 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e l(X).#endif..#en
762b0 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a dif../**********
762c0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69 **** End of sqli
762d0 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a teInt.h ********
762e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
762f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
76300 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
76310 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
76320 67 6c 6f 62 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a global.c *******
76330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
76340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
76350 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 ***/./*.** 2008
76360 4a 75 6e 65 20 31 33 0a 2a 2a 0a 2a 2a 20 54 68 June 13.**.** Th
76370 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
76380 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
76390 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
763a0 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
763b0 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
763c0 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
763d0 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
763e0 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
763f0 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
76400 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
76410 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
76420 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
76430 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
76440 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
76450 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
76460 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
76470 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
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 2a 2a 2a 2a ****************
764b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
764c0 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 ******.**.** Thi
764d0 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
764e0 64 65 66 69 6e 69 74 69 6f 6e 73 20 6f 66 20 67 definitions of g
764f0 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 73 20 lobal variables
76500 61 6e 64 20 63 6f 6e 74 61 6e 74 73 2e 0a 2a 2f and contants..*/
76510 0a 0a 0a 2f 2a 20 41 6e 20 61 72 72 61 79 20 74 .../* An array t
76520 6f 20 6d 61 70 20 61 6c 6c 20 75 70 70 65 72 2d o map all upper-
76530 63 61 73 65 20 63 68 61 72 61 63 74 65 72 73 20 case characters
76540 69 6e 74 6f 20 74 68 65 69 72 20 63 6f 72 72 65 into their corre
76550 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 6c 6f 77 65 sponding.** lowe
76560 72 2d 63 61 73 65 20 63 68 61 72 61 63 74 65 72 r-case character
76570 2e 20 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 . .**.** SQLite
76580 6f 6e 6c 79 20 63 6f 6e 73 69 64 65 72 73 20 55 only considers U
76590 53 2d 41 53 43 49 49 20 28 6f 72 20 45 42 43 44 S-ASCII (or EBCD
765a0 49 43 29 20 63 68 61 72 61 63 74 65 72 73 2e 20 IC) characters.
765b0 20 57 65 20 64 6f 20 6e 6f 74 0a 2a 2a 20 68 61 We do not.** ha
765c0 6e 64 6c 65 20 63 61 73 65 20 63 6f 6e 76 65 72 ndle case conver
765d0 73 69 6f 6e 73 20 66 6f 72 20 74 68 65 20 55 54 sions for the UT
765e0 46 20 63 68 61 72 61 63 74 65 72 20 73 65 74 20 F character set
765f0 73 69 6e 63 65 20 74 68 65 20 74 61 62 6c 65 73 since the tables
76600 0a 2a 2a 20 69 6e 76 6f 6c 76 65 64 20 61 72 65 .** involved are
76610 20 6e 65 61 72 6c 79 20 61 73 20 62 69 67 20 6f nearly as big o
76620 72 20 62 69 67 67 65 72 20 74 68 61 6e 20 53 51 r bigger than SQ
76630 4c 69 74 65 20 69 74 73 65 6c 66 2e 0a 2a 2f 0a Lite itself..*/.
76640 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
76650 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
76660 61 72 20 73 71 6c 69 74 65 33 55 70 70 65 72 54 ar sqlite3UpperT
76670 6f 4c 6f 77 65 72 5b 5d 20 3d 20 7b 0a 23 69 66 oLower[] = {.#if
76680 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43 49 49 def SQLITE_ASCII
76690 0a 20 20 20 20 20 20 30 2c 20 20 31 2c 20 20 32 . 0, 1, 2
766a0 2c 20 20 33 2c 20 20 34 2c 20 20 35 2c 20 20 36 , 3, 4, 5, 6
766b0 2c 20 20 37 2c 20 20 38 2c 20 20 39 2c 20 31 30 , 7, 8, 9, 10
766c0 2c 20 31 31 2c 20 31 32 2c 20 31 33 2c 20 31 34 , 11, 12, 13, 14
766d0 2c 20 31 35 2c 20 31 36 2c 20 31 37 2c 0a 20 20 , 15, 16, 17,.
766e0 20 20 20 31 38 2c 20 31 39 2c 20 32 30 2c 20 32 18, 19, 20, 2
766f0 31 2c 20 32 32 2c 20 32 33 2c 20 32 34 2c 20 32 1, 22, 23, 24, 2
76700 35 2c 20 32 36 2c 20 32 37 2c 20 32 38 2c 20 32 5, 26, 27, 28, 2
76710 39 2c 20 33 30 2c 20 33 31 2c 20 33 32 2c 20 33 9, 30, 31, 32, 3
76720 33 2c 20 33 34 2c 20 33 35 2c 0a 20 20 20 20 20 3, 34, 35,.
76730 33 36 2c 20 33 37 2c 20 33 38 2c 20 33 39 2c 20 36, 37, 38, 39,
76740 34 30 2c 20 34 31 2c 20 34 32 2c 20 34 33 2c 20 40, 41, 42, 43,
76750 34 34 2c 20 34 35 2c 20 34 36 2c 20 34 37 2c 20 44, 45, 46, 47,
76760 34 38 2c 20 34 39 2c 20 35 30 2c 20 35 31 2c 20 48, 49, 50, 51,
76770 35 32 2c 20 35 33 2c 0a 20 20 20 20 20 35 34 2c 52, 53,. 54,
76780 20 35 35 2c 20 35 36 2c 20 35 37 2c 20 35 38 2c 55, 56, 57, 58,
76790 20 35 39 2c 20 36 30 2c 20 36 31 2c 20 36 32 2c 59, 60, 61, 62,
767a0 20 36 33 2c 20 36 34 2c 20 39 37 2c 20 39 38 2c 63, 64, 97, 98,
767b0 20 39 39 2c 31 30 30 2c 31 30 31 2c 31 30 32 2c 99,100,101,102,
767c0 31 30 33 2c 0a 20 20 20 20 31 30 34 2c 31 30 35 103,. 104,105
767d0 2c 31 30 36 2c 31 30 37 2c 31 30 38 2c 31 30 39 ,106,107,108,109
767e0 2c 31 31 30 2c 31 31 31 2c 31 31 32 2c 31 31 33 ,110,111,112,113
767f0 2c 31 31 34 2c 31 31 35 2c 31 31 36 2c 31 31 37 ,114,115,116,117
76800 2c 31 31 38 2c 31 31 39 2c 31 32 30 2c 31 32 31 ,118,119,120,121
76810 2c 0a 20 20 20 20 31 32 32 2c 20 39 31 2c 20 39 ,. 122, 91, 9
76820 32 2c 20 39 33 2c 20 39 34 2c 20 39 35 2c 20 39 2, 93, 94, 95, 9
76830 36 2c 20 39 37 2c 20 39 38 2c 20 39 39 2c 31 30 6, 97, 98, 99,10
76840 30 2c 31 30 31 2c 31 30 32 2c 31 30 33 2c 31 30 0,101,102,103,10
76850 34 2c 31 30 35 2c 31 30 36 2c 31 30 37 2c 0a 20 4,105,106,107,.
76860 20 20 20 31 30 38 2c 31 30 39 2c 31 31 30 2c 31 108,109,110,1
76870 31 31 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c 31 11,112,113,114,1
76880 31 35 2c 31 31 36 2c 31 31 37 2c 31 31 38 2c 31 15,116,117,118,1
76890 31 39 2c 31 32 30 2c 31 32 31 2c 31 32 32 2c 31 19,120,121,122,1
768a0 32 33 2c 31 32 34 2c 31 32 35 2c 0a 20 20 20 20 23,124,125,.
768b0 31 32 36 2c 31 32 37 2c 31 32 38 2c 31 32 39 2c 126,127,128,129,
768c0 31 33 30 2c 31 33 31 2c 31 33 32 2c 31 33 33 2c 130,131,132,133,
768d0 31 33 34 2c 31 33 35 2c 31 33 36 2c 31 33 37 2c 134,135,136,137,
768e0 31 33 38 2c 31 33 39 2c 31 34 30 2c 31 34 31 2c 138,139,140,141,
768f0 31 34 32 2c 31 34 33 2c 0a 20 20 20 20 31 34 34 142,143,. 144
76900 2c 31 34 35 2c 31 34 36 2c 31 34 37 2c 31 34 38 ,145,146,147,148
76910 2c 31 34 39 2c 31 35 30 2c 31 35 31 2c 31 35 32 ,149,150,151,152
76920 2c 31 35 33 2c 31 35 34 2c 31 35 35 2c 31 35 36 ,153,154,155,156
76930 2c 31 35 37 2c 31 35 38 2c 31 35 39 2c 31 36 30 ,157,158,159,160
76940 2c 31 36 31 2c 0a 20 20 20 20 31 36 32 2c 31 36 ,161,. 162,16
76950 33 2c 31 36 34 2c 31 36 35 2c 31 36 36 2c 31 36 3,164,165,166,16
76960 37 2c 31 36 38 2c 31 36 39 2c 31 37 30 2c 31 37 7,168,169,170,17
76970 31 2c 31 37 32 2c 31 37 33 2c 31 37 34 2c 31 37 1,172,173,174,17
76980 35 2c 31 37 36 2c 31 37 37 2c 31 37 38 2c 31 37 5,176,177,178,17
76990 39 2c 0a 20 20 20 20 31 38 30 2c 31 38 31 2c 31 9,. 180,181,1
769a0 38 32 2c 31 38 33 2c 31 38 34 2c 31 38 35 2c 31 82,183,184,185,1
769b0 38 36 2c 31 38 37 2c 31 38 38 2c 31 38 39 2c 31 86,187,188,189,1
769c0 39 30 2c 31 39 31 2c 31 39 32 2c 31 39 33 2c 31 90,191,192,193,1
769d0 39 34 2c 31 39 35 2c 31 39 36 2c 31 39 37 2c 0a 94,195,196,197,.
769e0 20 20 20 20 31 39 38 2c 31 39 39 2c 32 30 30 2c 198,199,200,
769f0 32 30 31 2c 32 30 32 2c 32 30 33 2c 32 30 34 2c 201,202,203,204,
76a00 32 30 35 2c 32 30 36 2c 32 30 37 2c 32 30 38 2c 205,206,207,208,
76a10 32 30 39 2c 32 31 30 2c 32 31 31 2c 32 31 32 2c 209,210,211,212,
76a20 32 31 33 2c 32 31 34 2c 32 31 35 2c 0a 20 20 20 213,214,215,.
76a30 20 32 31 36 2c 32 31 37 2c 32 31 38 2c 32 31 39 216,217,218,219
76a40 2c 32 32 30 2c 32 32 31 2c 32 32 32 2c 32 32 33 ,220,221,222,223
76a50 2c 32 32 34 2c 32 32 35 2c 32 32 36 2c 32 32 37 ,224,225,226,227
76a60 2c 32 32 38 2c 32 32 39 2c 32 33 30 2c 32 33 31 ,228,229,230,231
76a70 2c 32 33 32 2c 32 33 33 2c 0a 20 20 20 20 32 33 ,232,233,. 23
76a80 34 2c 32 33 35 2c 32 33 36 2c 32 33 37 2c 32 33 4,235,236,237,23
76a90 38 2c 32 33 39 2c 32 34 30 2c 32 34 31 2c 32 34 8,239,240,241,24
76aa0 32 2c 32 34 33 2c 32 34 34 2c 32 34 35 2c 32 34 2,243,244,245,24
76ab0 36 2c 32 34 37 2c 32 34 38 2c 32 34 39 2c 32 35 6,247,248,249,25
76ac0 30 2c 32 35 31 2c 0a 20 20 20 20 32 35 32 2c 32 0,251,. 252,2
76ad0 35 33 2c 32 35 34 2c 32 35 35 0a 23 65 6e 64 69 53,254,255.#endi
76ae0 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f f.#ifdef SQLITE_
76af0 45 42 43 44 49 43 0a 20 20 20 20 20 20 30 2c 20 EBCDIC. 0,
76b00 20 31 2c 20 20 32 2c 20 20 33 2c 20 20 34 2c 20 1, 2, 3, 4,
76b10 20 35 2c 20 20 36 2c 20 20 37 2c 20 20 38 2c 20 5, 6, 7, 8,
76b20 20 39 2c 20 31 30 2c 20 31 31 2c 20 31 32 2c 20 9, 10, 11, 12,
76b30 31 33 2c 20 31 34 2c 20 31 35 2c 20 2f 2a 20 30 13, 14, 15, /* 0
76b40 78 20 2a 2f 0a 20 20 20 20 20 31 36 2c 20 31 37 x */. 16, 17
76b50 2c 20 31 38 2c 20 31 39 2c 20 32 30 2c 20 32 31 , 18, 19, 20, 21
76b60 2c 20 32 32 2c 20 32 33 2c 20 32 34 2c 20 32 35 , 22, 23, 24, 25
76b70 2c 20 32 36 2c 20 32 37 2c 20 32 38 2c 20 32 39 , 26, 27, 28, 29
76b80 2c 20 33 30 2c 20 33 31 2c 20 2f 2a 20 31 78 20 , 30, 31, /* 1x
76b90 2a 2f 0a 20 20 20 20 20 33 32 2c 20 33 33 2c 20 */. 32, 33,
76ba0 33 34 2c 20 33 35 2c 20 33 36 2c 20 33 37 2c 20 34, 35, 36, 37,
76bb0 33 38 2c 20 33 39 2c 20 34 30 2c 20 34 31 2c 20 38, 39, 40, 41,
76bc0 34 32 2c 20 34 33 2c 20 34 34 2c 20 34 35 2c 20 42, 43, 44, 45,
76bd0 34 36 2c 20 34 37 2c 20 2f 2a 20 32 78 20 2a 2f 46, 47, /* 2x */
76be0 0a 20 20 20 20 20 34 38 2c 20 34 39 2c 20 35 30 . 48, 49, 50
76bf0 2c 20 35 31 2c 20 35 32 2c 20 35 33 2c 20 35 34 , 51, 52, 53, 54
76c00 2c 20 35 35 2c 20 35 36 2c 20 35 37 2c 20 35 38 , 55, 56, 57, 58
76c10 2c 20 35 39 2c 20 36 30 2c 20 36 31 2c 20 36 32 , 59, 60, 61, 62
76c20 2c 20 36 33 2c 20 2f 2a 20 33 78 20 2a 2f 0a 20 , 63, /* 3x */.
76c30 20 20 20 20 36 34 2c 20 36 35 2c 20 36 36 2c 20 64, 65, 66,
76c40 36 37 2c 20 36 38 2c 20 36 39 2c 20 37 30 2c 20 67, 68, 69, 70,
76c50 37 31 2c 20 37 32 2c 20 37 33 2c 20 37 34 2c 20 71, 72, 73, 74,
76c60 37 35 2c 20 37 36 2c 20 37 37 2c 20 37 38 2c 20 75, 76, 77, 78,
76c70 37 39 2c 20 2f 2a 20 34 78 20 2a 2f 0a 20 20 20 79, /* 4x */.
76c80 20 20 38 30 2c 20 38 31 2c 20 38 32 2c 20 38 33 80, 81, 82, 83
76c90 2c 20 38 34 2c 20 38 35 2c 20 38 36 2c 20 38 37 , 84, 85, 86, 87
76ca0 2c 20 38 38 2c 20 38 39 2c 20 39 30 2c 20 39 31 , 88, 89, 90, 91
76cb0 2c 20 39 32 2c 20 39 33 2c 20 39 34 2c 20 39 35 , 92, 93, 94, 95
76cc0 2c 20 2f 2a 20 35 78 20 2a 2f 0a 20 20 20 20 20 , /* 5x */.
76cd0 39 36 2c 20 39 37 2c 20 36 36 2c 20 36 37 2c 20 96, 97, 66, 67,
76ce0 36 38 2c 20 36 39 2c 20 37 30 2c 20 37 31 2c 20 68, 69, 70, 71,
76cf0 37 32 2c 20 37 33 2c 31 30 36 2c 31 30 37 2c 31 72, 73,106,107,1
76d00 30 38 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 20 08,109,110,111,
76d10 2f 2a 20 36 78 20 2a 2f 0a 20 20 20 20 31 31 32 /* 6x */. 112
76d20 2c 20 38 31 2c 20 38 32 2c 20 38 33 2c 20 38 34 , 81, 82, 83, 84
76d30 2c 20 38 35 2c 20 38 36 2c 20 38 37 2c 20 38 38 , 85, 86, 87, 88
76d40 2c 20 38 39 2c 31 32 32 2c 31 32 33 2c 31 32 34 , 89,122,123,124
76d50 2c 31 32 35 2c 31 32 36 2c 31 32 37 2c 20 2f 2a ,125,126,127, /*
76d60 20 37 78 20 2a 2f 0a 20 20 20 20 31 32 38 2c 31 7x */. 128,1
76d70 32 39 2c 31 33 30 2c 31 33 31 2c 31 33 32 2c 31 29,130,131,132,1
76d80 33 33 2c 31 33 34 2c 31 33 35 2c 31 33 36 2c 31 33,134,135,136,1
76d90 33 37 2c 31 33 38 2c 31 33 39 2c 31 34 30 2c 31 37,138,139,140,1
76da0 34 31 2c 31 34 32 2c 31 34 33 2c 20 2f 2a 20 38 41,142,143, /* 8
76db0 78 20 2a 2f 0a 20 20 20 20 31 34 34 2c 31 34 35 x */. 144,145
76dc0 2c 31 34 36 2c 31 34 37 2c 31 34 38 2c 31 34 39 ,146,147,148,149
76dd0 2c 31 35 30 2c 31 35 31 2c 31 35 32 2c 31 35 33 ,150,151,152,153
76de0 2c 31 35 34 2c 31 35 35 2c 31 35 36 2c 31 35 37 ,154,155,156,157
76df0 2c 31 35 36 2c 31 35 39 2c 20 2f 2a 20 39 78 20 ,156,159, /* 9x
76e00 2a 2f 0a 20 20 20 20 31 36 30 2c 31 36 31 2c 31 */. 160,161,1
76e10 36 32 2c 31 36 33 2c 31 36 34 2c 31 36 35 2c 31 62,163,164,165,1
76e20 36 36 2c 31 36 37 2c 31 36 38 2c 31 36 39 2c 31 66,167,168,169,1
76e30 37 30 2c 31 37 31 2c 31 34 30 2c 31 34 31 2c 31 70,171,140,141,1
76e40 34 32 2c 31 37 35 2c 20 2f 2a 20 41 78 20 2a 2f 42,175, /* Ax */
76e50 0a 20 20 20 20 31 37 36 2c 31 37 37 2c 31 37 38 . 176,177,178
76e60 2c 31 37 39 2c 31 38 30 2c 31 38 31 2c 31 38 32 ,179,180,181,182
76e70 2c 31 38 33 2c 31 38 34 2c 31 38 35 2c 31 38 36 ,183,184,185,186
76e80 2c 31 38 37 2c 31 38 38 2c 31 38 39 2c 31 39 30 ,187,188,189,190
76e90 2c 31 39 31 2c 20 2f 2a 20 42 78 20 2a 2f 0a 20 ,191, /* Bx */.
76ea0 20 20 20 31 39 32 2c 31 32 39 2c 31 33 30 2c 31 192,129,130,1
76eb0 33 31 2c 31 33 32 2c 31 33 33 2c 31 33 34 2c 31 31,132,133,134,1
76ec0 33 35 2c 31 33 36 2c 31 33 37 2c 32 30 32 2c 32 35,136,137,202,2
76ed0 30 33 2c 32 30 34 2c 32 30 35 2c 32 30 36 2c 32 03,204,205,206,2
76ee0 30 37 2c 20 2f 2a 20 43 78 20 2a 2f 0a 20 20 20 07, /* Cx */.
76ef0 20 32 30 38 2c 31 34 35 2c 31 34 36 2c 31 34 37 208,145,146,147
76f00 2c 31 34 38 2c 31 34 39 2c 31 35 30 2c 31 35 31 ,148,149,150,151
76f10 2c 31 35 32 2c 31 35 33 2c 32 31 38 2c 32 31 39 ,152,153,218,219
76f20 2c 32 32 30 2c 32 32 31 2c 32 32 32 2c 32 32 33 ,220,221,222,223
76f30 2c 20 2f 2a 20 44 78 20 2a 2f 0a 20 20 20 20 32 , /* Dx */. 2
76f40 32 34 2c 32 32 35 2c 31 36 32 2c 31 36 33 2c 31 24,225,162,163,1
76f50 36 34 2c 31 36 35 2c 31 36 36 2c 31 36 37 2c 31 64,165,166,167,1
76f60 36 38 2c 31 36 39 2c 32 33 32 2c 32 30 33 2c 32 68,169,232,203,2
76f70 30 34 2c 32 30 35 2c 32 30 36 2c 32 30 37 2c 20 04,205,206,207,
76f80 2f 2a 20 45 78 20 2a 2f 0a 20 20 20 20 32 33 39 /* Ex */. 239
76f90 2c 32 34 30 2c 32 34 31 2c 32 34 32 2c 32 34 33 ,240,241,242,243
76fa0 2c 32 34 34 2c 32 34 35 2c 32 34 36 2c 32 34 37 ,244,245,246,247
76fb0 2c 32 34 38 2c 32 34 39 2c 32 31 39 2c 32 32 30 ,248,249,219,220
76fc0 2c 32 32 31 2c 32 32 32 2c 32 35 35 2c 20 2f 2a ,221,222,255, /*
76fd0 20 46 78 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b Fx */.#endif.};
76fe0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ../*.** The foll
76ff0 6f 77 69 6e 67 20 32 35 36 20 62 79 74 65 20 6c owing 256 byte l
77000 6f 6f 6b 75 70 20 74 61 62 6c 65 20 69 73 20 75 ookup table is u
77010 73 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20 53 sed to support S
77020 51 4c 69 74 65 73 20 62 75 69 6c 74 2d 69 6e 0a QLites built-in.
77030 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 73 20 74 ** equivalents t
77040 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 o the following
77050 73 74 61 6e 64 61 72 64 20 6c 69 62 72 61 72 79 standard library
77060 20 66 75 6e 63 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a functions:.**.*
77070 2a 20 20 20 69 73 73 70 61 63 65 28 29 20 20 20 * isspace()
77080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77090 20 20 20 20 20 30 78 30 31 0a 2a 2a 20 20 20 69 0x01.** i
770a0 73 61 6c 70 68 61 28 29 20 20 20 20 20 20 20 20 salpha()
770b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
770c0 30 78 30 32 0a 2a 2a 20 20 20 69 73 64 69 67 69 0x02.** isdigi
770d0 74 28 29 20 20 20 20 20 20 20 20 20 20 20 20 20 t()
770e0 20 20 20 20 20 20 20 20 20 20 20 30 78 30 34 0a 0x04.
770f0 2a 2a 20 20 20 69 73 61 6c 6e 75 6d 28 29 20 20 ** isalnum()
77100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77110 20 20 20 20 20 20 30 78 30 36 0a 2a 2a 20 20 20 0x06.**
77120 69 73 78 64 69 67 69 74 28 29 20 20 20 20 20 20 isxdigit()
77130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77140 20 30 78 30 38 0a 2a 2a 20 20 20 74 6f 75 70 70 0x08.** toupp
77150 65 72 28 29 20 20 20 20 20 20 20 20 20 20 20 20 er()
77160 20 20 20 20 20 20 20 20 20 20 20 20 30 78 32 30 0x20
77170 0a 2a 2a 0a 2a 2a 20 42 69 74 20 30 78 32 30 20 .**.** Bit 0x20
77180 69 73 20 73 65 74 20 69 66 20 74 68 65 20 6d 61 is set if the ma
77190 70 70 65 64 20 63 68 61 72 61 63 74 65 72 20 72 pped character r
771a0 65 71 75 69 72 65 73 20 74 72 61 6e 73 6c 61 74 equires translat
771b0 69 6f 6e 20 74 6f 20 75 70 70 65 72 0a 2a 2a 20 ion to upper.**
771c0 63 61 73 65 2e 20 69 2e 65 2e 20 69 66 20 74 68 case. i.e. if th
771d0 65 20 63 68 61 72 61 63 74 65 72 20 69 73 20 61 e character is a
771e0 20 6c 6f 77 65 72 2d 63 61 73 65 20 41 53 43 49 lower-case ASCI
771f0 49 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 I character..**
77200 49 66 20 78 20 69 73 20 61 20 6c 6f 77 65 72 2d If x is a lower-
77210 63 61 73 65 20 41 53 43 49 49 20 63 68 61 72 61 case ASCII chara
77220 63 74 65 72 2c 20 74 68 65 6e 20 69 74 73 20 75 cter, then its u
77230 70 70 65 72 2d 63 61 73 65 20 65 71 75 69 76 61 pper-case equiva
77240 6c 65 6e 74 0a 2a 2a 20 69 73 20 28 78 20 2d 20 lent.** is (x -
77250 30 78 32 30 29 2e 20 54 68 65 72 65 66 6f 72 65 0x20). Therefore
77260 20 74 6f 75 70 70 65 72 28 29 20 63 61 6e 20 62 toupper() can b
77270 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 e implemented as
77280 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 78 20 26 20 7e :.**.** (x & ~
77290 28 6d 61 70 5b 78 5d 26 30 78 32 30 29 29 0a 2a (map[x]&0x20)).*
772a0 2a 0a 2a 2a 20 53 74 61 6e 64 61 72 64 20 66 75 *.** Standard fu
772b0 6e 63 74 69 6f 6e 20 74 6f 6c 6f 77 65 72 28 29 nction tolower()
772c0 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 is implemented
772d0 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 using the sqlite
772e0 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 5d 0a 3UpperToLower[].
772f0 2a 2a 20 61 72 72 61 79 2e 20 74 6f 6c 6f 77 65 ** array. tolowe
77300 72 28 29 20 69 73 20 75 73 65 64 20 6d 6f 72 65 r() is used more
77310 20 6f 66 74 65 6e 20 74 68 61 6e 20 74 6f 75 70 often than toup
77320 70 65 72 28 29 20 62 79 20 53 51 4c 69 74 65 2e per() by SQLite.
77330 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 27 73 20 .**.** SQLite's
77340 76 65 72 73 69 6f 6e 73 20 61 72 65 20 69 64 65 versions are ide
77350 6e 74 69 63 61 6c 20 74 6f 20 74 68 65 20 73 74 ntical to the st
77360 61 6e 64 61 72 64 20 76 65 72 73 69 6f 6e 73 20 andard versions
77370 61 73 73 75 6d 69 6e 67 20 61 0a 2a 2a 20 6c 6f assuming a.** lo
77380 63 61 6c 65 20 6f 66 20 22 43 22 2e 20 54 68 65 cale of "C". The
77390 79 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 y are implemente
773a0 64 20 61 73 20 6d 61 63 72 6f 73 20 69 6e 20 73 d as macros in s
773b0 71 6c 69 74 65 49 6e 74 2e 68 2e 0a 2a 2f 0a 23 qliteInt.h..*/.#
773c0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43 ifdef SQLITE_ASC
773d0 49 49 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 II.SQLITE_PRIVAT
773e0 45 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 E const unsigned
773f0 20 63 68 61 72 20 73 71 6c 69 74 65 33 43 74 79 char sqlite3Cty
77400 70 65 4d 61 70 5b 32 35 36 5d 20 3d 20 7b 0a 20 peMap[256] = {.
77410 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
77420 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
77430 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
77440 20 20 2f 2a 20 30 30 2e 2e 30 37 20 20 20 20 2e /* 00..07 .
77450 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 ....... */. 0x0
77460 30 2c 20 30 78 30 31 2c 20 30 78 30 31 2c 20 30 0, 0x01, 0x01, 0
77470 78 30 31 2c 20 30 78 30 31 2c 20 30 78 30 31 2c x01, 0x01, 0x01,
77480 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 0x00, 0x00, /*
77490 20 30 38 2e 2e 30 66 20 20 20 20 2e 2e 2e 2e 2e 08..0f .....
774a0 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 ... */. 0x00, 0
774b0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
774c0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
774d0 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 31 30 2e 0, 0x00, /* 10.
774e0 2e 31 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 .17 ........
774f0 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c */. 0x00, 0x00,
77500 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
77510 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
77520 78 30 30 2c 20 20 2f 2a 20 31 38 2e 2e 31 66 20 x00, /* 18..1f
77530 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 ........ */.
77540 20 30 78 30 31 2c 20 30 78 30 30 2c 20 30 78 30 0x01, 0x00, 0x0
77550 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
77560 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
77570 20 20 2f 2a 20 32 30 2e 2e 32 37 20 20 20 20 20 /* 20..27
77580 21 22 23 24 25 26 27 20 2a 2f 0a 20 20 30 78 30 !"#$%&' */. 0x0
77590 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
775a0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
775b0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 0x00, 0x00, /*
775c0 20 32 38 2e 2e 32 66 20 20 20 20 28 29 2a 2b 2c 28..2f ()*+,
775d0 2d 2e 2f 20 2a 2f 0a 20 20 30 78 30 63 2c 20 30 -./ */. 0x0c, 0
775e0 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 63 2c x0c, 0x0c, 0x0c,
775f0 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 0x0c, 0x0c, 0x0
77600 63 2c 20 30 78 30 63 2c 20 20 2f 2a 20 33 30 2e c, 0x0c, /* 30.
77610 2e 33 37 20 20 20 20 30 31 32 33 34 35 36 37 20 .37 01234567
77620 2a 2f 0a 20 20 30 78 30 63 2c 20 30 78 30 63 2c */. 0x0c, 0x0c,
77630 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
77640 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
77650 78 30 30 2c 20 20 2f 2a 20 33 38 2e 2e 33 66 20 x00, /* 38..3f
77660 20 20 20 38 39 3a 3b 3c 3d 3e 3f 20 2a 2f 0a 0a 89:;<=>? */..
77670 20 20 30 78 30 30 2c 20 30 78 30 61 2c 20 30 78 0x00, 0x0a, 0x
77680 30 61 2c 20 30 78 30 61 2c 20 30 78 30 61 2c 20 0a, 0x0a, 0x0a,
77690 30 78 30 61 2c 20 30 78 30 61 2c 20 30 78 30 32 0x0a, 0x0a, 0x02
776a0 2c 20 20 2f 2a 20 34 30 2e 2e 34 37 20 20 20 20 , /* 40..47
776b0 40 41 42 43 44 45 46 47 20 2a 2f 0a 20 20 30 78 @ABCDEFG */. 0x
776c0 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 02, 0x02, 0x02,
776d0 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 0x02, 0x02, 0x02
776e0 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 20 2f , 0x02, 0x02, /
776f0 2a 20 34 38 2e 2e 34 66 20 20 20 20 48 49 4a 4b * 48..4f HIJK
77700 4c 4d 4e 4f 20 2a 2f 0a 20 20 30 78 30 32 2c 20 LMNO */. 0x02,
77710 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 0x02, 0x02, 0x02
77720 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 , 0x02, 0x02, 0x
77730 30 32 2c 20 30 78 30 32 2c 20 20 2f 2a 20 35 30 02, 0x02, /* 50
77740 2e 2e 35 37 20 20 20 20 50 51 52 53 54 55 56 57 ..57 PQRSTUVW
77750 20 2a 2f 0a 20 20 30 78 30 32 2c 20 30 78 30 32 */. 0x02, 0x02
77760 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 , 0x02, 0x00, 0x
77770 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
77780 30 78 30 30 2c 20 20 2f 2a 20 35 38 2e 2e 35 66 0x00, /* 58..5f
77790 20 20 20 20 58 59 5a 5b 5c 5d 5e 5f 20 2a 2f 0a XYZ[\]^_ */.
777a0 20 20 30 78 30 30 2c 20 30 78 32 61 2c 20 30 78 0x00, 0x2a, 0x
777b0 32 61 2c 20 30 78 32 61 2c 20 30 78 32 61 2c 20 2a, 0x2a, 0x2a,
777c0 30 78 32 61 2c 20 30 78 32 61 2c 20 30 78 32 32 0x2a, 0x2a, 0x22
777d0 2c 20 20 2f 2a 20 36 30 2e 2e 36 37 20 20 20 20 , /* 60..67
777e0 60 61 62 63 64 65 66 67 20 2a 2f 0a 20 20 30 78 `abcdefg */. 0x
777f0 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 22, 0x22, 0x22,
77800 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 0x22, 0x22, 0x22
77810 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 20 2f , 0x22, 0x22, /
77820 2a 20 36 38 2e 2e 36 66 20 20 20 20 68 69 6a 6b * 68..6f hijk
77830 6c 6d 6e 6f 20 2a 2f 0a 20 20 30 78 32 32 2c 20 lmno */. 0x22,
77840 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 0x22, 0x22, 0x22
77850 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 , 0x22, 0x22, 0x
77860 32 32 2c 20 30 78 32 32 2c 20 20 2f 2a 20 37 30 22, 0x22, /* 70
77870 2e 2e 37 37 20 20 20 20 70 71 72 73 74 75 76 77 ..77 pqrstuvw
77880 20 2a 2f 0a 20 20 30 78 32 32 2c 20 30 78 32 32 */. 0x22, 0x22
77890 2c 20 30 78 32 32 2c 20 30 78 30 30 2c 20 30 78 , 0x22, 0x00, 0x
778a0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
778b0 30 78 30 30 2c 20 20 2f 2a 20 37 38 2e 2e 37 66 0x00, /* 78..7f
778c0 20 20 20 20 78 79 7a 7b 7c 7d 7e 2e 20 2a 2f 0a xyz{|}~. */.
778d0 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 . 0x00, 0x00, 0
778e0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
778f0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
77900 30 2c 20 20 2f 2a 20 38 30 2e 2e 38 37 20 20 20 0, /* 80..87
77910 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 ........ */. 0
77920 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
77930 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
77940 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 0, 0x00, 0x00,
77950 2f 2a 20 38 38 2e 2e 38 66 20 20 20 20 2e 2e 2e /* 88..8f ...
77960 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c ..... */. 0x00,
77970 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
77980 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
77990 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 39 x00, 0x00, /* 9
779a0 30 2e 2e 39 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 0..97 .......
779b0 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 . */. 0x00, 0x0
779c0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
779d0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
779e0 20 30 78 30 30 2c 20 20 2f 2a 20 39 38 2e 2e 39 0x00, /* 98..9
779f0 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f f ........ */
77a00 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 . 0x00, 0x00, 0
77a10 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
77a20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
77a30 30 2c 20 20 2f 2a 20 61 30 2e 2e 61 37 20 20 20 0, /* a0..a7
77a40 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 ........ */. 0
77a50 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
77a60 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
77a70 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 0, 0x00, 0x00,
77a80 2f 2a 20 61 38 2e 2e 61 66 20 20 20 20 2e 2e 2e /* a8..af ...
77a90 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c ..... */. 0x00,
77aa0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
77ab0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
77ac0 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 62 x00, 0x00, /* b
77ad0 30 2e 2e 62 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 0..b7 .......
77ae0 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 . */. 0x00, 0x0
77af0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
77b00 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
77b10 20 30 78 30 30 2c 20 20 2f 2a 20 62 38 2e 2e 62 0x00, /* b8..b
77b20 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f f ........ */
77b30 0a 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 .. 0x00, 0x00,
77b40 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
77b50 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
77b60 30 30 2c 20 20 2f 2a 20 63 30 2e 2e 63 37 20 20 00, /* c0..c7
77b70 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 ........ */.
77b80 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
77b90 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
77ba0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
77bb0 20 2f 2a 20 63 38 2e 2e 63 66 20 20 20 20 2e 2e /* c8..cf ..
77bc0 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 ...... */. 0x00
77bd0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
77be0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
77bf0 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 0x00, 0x00, /*
77c00 64 30 2e 2e 64 37 20 20 20 20 2e 2e 2e 2e 2e 2e d0..d7 ......
77c10 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 .. */. 0x00, 0x
77c20 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
77c30 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
77c40 2c 20 30 78 30 30 2c 20 20 2f 2a 20 64 38 2e 2e , 0x00, /* d8..
77c50 64 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a df ........ *
77c60 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 /. 0x00, 0x00,
77c70 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
77c80 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
77c90 30 30 2c 20 20 2f 2a 20 65 30 2e 2e 65 37 20 20 00, /* e0..e7
77ca0 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 ........ */.
77cb0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
77cc0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
77cd0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
77ce0 20 2f 2a 20 65 38 2e 2e 65 66 20 20 20 20 2e 2e /* e8..ef ..
77cf0 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 ...... */. 0x00
77d00 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
77d10 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
77d20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 0x00, 0x00, /*
77d30 66 30 2e 2e 66 37 20 20 20 20 2e 2e 2e 2e 2e 2e f0..f7 ......
77d40 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 .. */. 0x00, 0x
77d50 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
77d60 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
77d70 2c 20 30 78 30 30 20 20 20 2f 2a 20 66 38 2e 2e , 0x00 /* f8..
77d80 66 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a ff ........ *
77d90 2f 0a 7d 3b 0a 23 65 6e 64 69 66 0a 0a 0a 0a 2f /.};.#endif..../
77da0 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
77db0 6e 67 20 73 69 6e 67 6c 65 74 6f 6e 20 63 6f 6e ng singleton con
77dc0 74 61 69 6e 73 20 74 68 65 20 67 6c 6f 62 61 6c tains the global
77dd0 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 66 configuration f
77de0 6f 72 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 or.** the SQLite
77df0 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 53 51 4c library..*/.SQL
77e00 49 54 45 5f 50 52 49 56 41 54 45 20 53 51 4c 49 ITE_PRIVATE SQLI
77e10 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 53 71 TE_WSD struct Sq
77e20 6c 69 74 65 33 43 6f 6e 66 69 67 20 73 71 6c 69 lite3Config sqli
77e30 74 65 33 43 6f 6e 66 69 67 20 3d 20 7b 0a 20 20 te3Config = {.
77e40 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
77e50 4d 45 4d 53 54 41 54 55 53 2c 20 20 2f 2a 20 62 MEMSTATUS, /* b
77e60 4d 65 6d 73 74 61 74 20 2a 2f 0a 20 20 20 31 2c Memstat */. 1,
77e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77e80 20 20 20 20 20 20 20 20 20 2f 2a 20 62 43 6f 72 /* bCor
77e90 65 4d 75 74 65 78 20 2a 2f 0a 20 20 20 53 51 4c eMutex */. SQL
77ea0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d ITE_THREADSAFE==
77eb0 31 2c 20 20 20 20 20 20 2f 2a 20 62 46 75 6c 6c 1, /* bFull
77ec0 4d 75 74 65 78 20 2a 2f 0a 20 20 20 30 78 37 66 Mutex */. 0x7f
77ed0 66 66 66 66 66 65 2c 20 20 20 20 20 20 20 20 20 fffffe,
77ee0 20 20 20 20 20 20 20 2f 2a 20 6d 78 53 74 72 6c /* mxStrl
77ef0 65 6e 20 2a 2f 0a 20 20 20 31 30 30 2c 20 20 20 en */. 100,
77f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77f10 20 20 20 20 2f 2a 20 73 7a 4c 6f 6f 6b 61 73 69 /* szLookasi
77f20 64 65 20 2a 2f 0a 20 20 20 35 30 30 2c 20 20 20 de */. 500,
77f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77f40 20 20 20 20 2f 2a 20 6e 4c 6f 6f 6b 61 73 69 64 /* nLookasid
77f50 65 20 2a 2f 0a 20 20 20 7b 30 2c 30 2c 30 2c 30 e */. {0,0,0,0
77f60 2c 30 2c 30 2c 30 2c 30 7d 2c 20 20 20 20 20 20 ,0,0,0,0},
77f70 20 20 20 2f 2a 20 6d 20 2a 2f 0a 20 20 20 7b 30 /* m */. {0
77f80 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 ,0,0,0,0,0,0,0,0
77f90 7d 2c 20 20 20 20 20 20 20 2f 2a 20 6d 75 74 65 }, /* mute
77fa0 78 20 2a 2f 0a 20 20 20 7b 30 2c 30 2c 30 2c 30 x */. {0,0,0,0
77fb0 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 7d 2c ,0,0,0,0,0,0,0},
77fc0 20 20 20 2f 2a 20 70 63 61 63 68 65 20 2a 2f 0a /* pcache */.
77fd0 20 20 20 28 76 6f 69 64 2a 29 30 2c 20 20 20 20 (void*)0,
77fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
77ff0 20 70 48 65 61 70 20 2a 2f 0a 20 20 20 30 2c 20 pHeap */. 0,
78000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78010 20 20 20 20 20 20 20 20 2f 2a 20 6e 48 65 61 70 /* nHeap
78020 20 2a 2f 0a 20 20 20 30 2c 20 30 2c 20 20 20 20 */. 0, 0,
78030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78040 20 20 2f 2a 20 6d 6e 48 65 61 70 2c 20 6d 78 48 /* mnHeap, mxH
78050 65 61 70 20 2a 2f 0a 20 20 20 28 76 6f 69 64 2a eap */. (void*
78060 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 )0,
78070 20 20 20 20 20 2f 2a 20 70 53 63 72 61 74 63 68 /* pScratch
78080 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 */. 0,
78090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
780a0 20 20 2f 2a 20 73 7a 53 63 72 61 74 63 68 20 2a /* szScratch *
780b0 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 /. 0,
780c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
780d0 2f 2a 20 6e 53 63 72 61 74 63 68 20 2a 2f 0a 20 /* nScratch */.
780e0 20 20 28 76 6f 69 64 2a 29 30 2c 20 20 20 20 20 (void*)0,
780f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
78100 70 50 61 67 65 20 2a 2f 0a 20 20 20 30 2c 20 20 pPage */. 0,
78110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78120 20 20 20 20 20 20 20 2f 2a 20 73 7a 50 61 67 65 /* szPage
78130 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 */. 0,
78140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78150 20 20 2f 2a 20 6e 50 61 67 65 20 2a 2f 0a 20 20 /* nPage */.
78160 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
78170 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d /* m
78180 78 50 61 72 73 65 72 53 74 61 63 6b 20 2a 2f 0a xParserStack */.
78190 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 0,
781a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
781b0 20 73 68 61 72 65 64 43 61 63 68 65 45 6e 61 62 sharedCacheEnab
781c0 6c 65 64 20 2a 2f 0a 20 20 20 2f 2a 20 41 6c 6c led */. /* All
781d0 20 74 68 65 20 72 65 73 74 20 73 68 6f 75 6c 64 the rest should
781e0 20 61 6c 77 61 79 73 20 62 65 20 69 6e 69 74 69 always be initi
781f0 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72 6f 20 2a alized to zero *
78200 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 /. 0,
78210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78220 2f 2a 20 69 73 49 6e 69 74 20 2a 2f 0a 20 20 20 /* isInit */.
78230 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
78240 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e /* in
78250 50 72 6f 67 72 65 73 73 20 2a 2f 0a 20 20 20 30 Progress */. 0
78260 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
78270 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 73 4d /* isM
78280 75 74 65 78 49 6e 69 74 20 2a 2f 0a 20 20 20 30 utexInit */. 0
78290 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
782a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 73 4d /* isM
782b0 61 6c 6c 6f 63 49 6e 69 74 20 2a 2f 0a 20 20 20 allocInit */.
782c0 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
782d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 73 /* is
782e0 50 43 61 63 68 65 49 6e 69 74 20 2a 2f 0a 20 20 PCacheInit */.
782f0 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
78300 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 /* p
78310 49 6e 69 74 4d 75 74 65 78 20 2a 2f 0a 20 20 20 InitMutex */.
78320 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
78330 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 52 /* nR
78340 65 66 49 6e 69 74 4d 75 74 65 78 20 2a 2f 0a 7d efInitMutex */.}
78350 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 48 61 73 68 20 74 ;.../*.** Hash t
78360 61 62 6c 65 20 66 6f 72 20 67 6c 6f 62 61 6c 20 able for global
78370 66 75 6e 63 74 69 6f 6e 73 20 2d 20 66 75 6e 63 functions - func
78380 74 69 6f 6e 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 tions common to
78390 61 6c 6c 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 all.** database
783a0 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 41 66 connections. Af
783b0 74 65 72 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 ter initializati
783c0 6f 6e 2c 20 74 68 69 73 20 74 61 62 6c 65 20 69 on, this table i
783d0 73 0a 2a 2a 20 72 65 61 64 2d 6f 6e 6c 79 2e 0a s.** read-only..
783e0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
783f0 45 20 53 51 4c 49 54 45 5f 57 53 44 20 46 75 6e E SQLITE_WSD Fun
78400 63 44 65 66 48 61 73 68 20 73 71 6c 69 74 65 33 cDefHash sqlite3
78410 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 3b GlobalFunctions;
78420 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 ../*.** The valu
78430 65 20 6f 66 20 74 68 65 20 22 70 65 6e 64 69 6e e of the "pendin
78440 67 22 20 62 79 74 65 20 6d 75 73 74 20 62 65 20 g" byte must be
78450 30 78 34 30 30 30 30 30 30 30 20 28 31 20 62 79 0x40000000 (1 by
78460 74 65 20 70 61 73 74 20 74 68 65 0a 2a 2a 20 31 te past the.** 1
78470 2d 67 69 62 61 62 79 74 65 20 62 6f 75 6e 64 61 -gibabyte bounda
78480 72 79 29 20 69 6e 20 61 20 63 6f 6d 70 61 74 69 ry) in a compati
78490 62 6c 65 20 64 61 74 61 62 61 73 65 2e 20 20 53 ble database. S
784a0 51 4c 69 74 65 20 6e 65 76 65 72 20 75 73 65 73 QLite never uses
784b0 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 .** the database
784c0 20 70 61 67 65 20 74 68 61 74 20 63 6f 6e 74 61 page that conta
784d0 69 6e 73 20 74 68 65 20 70 65 6e 64 69 6e 67 20 ins the pending
784e0 62 79 74 65 2e 20 20 49 74 20 6e 65 76 65 72 20 byte. It never
784f0 61 74 74 65 6d 70 74 73 0a 2a 2a 20 74 6f 20 72 attempts.** to r
78500 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 68 61 ead or write tha
78510 74 20 70 61 67 65 2e 20 20 54 68 65 20 70 65 6e t page. The pen
78520 64 69 6e 67 20 62 79 74 65 20 70 61 67 65 20 69 ding byte page i
78530 73 20 73 65 74 20 61 73 73 69 67 6e 0a 2a 2a 20 s set assign.**
78540 66 6f 72 20 75 73 65 20 62 79 20 74 68 65 20 56 for use by the V
78550 46 53 20 6c 61 79 65 72 73 20 61 73 20 73 70 61 FS layers as spa
78560 63 65 20 66 6f 72 20 6d 61 6e 61 67 69 6e 67 20 ce for managing
78570 66 69 6c 65 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a file locks..**.*
78580 2a 20 44 75 72 69 6e 67 20 74 65 73 74 69 6e 67 * During testing
78590 2c 20 69 74 20 69 73 20 6f 66 74 65 6e 20 64 65 , it is often de
785a0 73 69 72 61 62 6c 65 20 74 6f 20 6d 6f 76 65 20 sirable to move
785b0 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 the pending byte
785c0 20 74 6f 0a 2a 2a 20 61 20 64 69 66 66 65 72 65 to.** a differe
785d0 6e 74 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74 nt position in t
785e0 68 65 20 66 69 6c 65 2e 20 20 54 68 69 73 20 61 he file. This a
785f0 6c 6c 6f 77 73 20 63 6f 64 65 20 74 68 61 74 20 llows code that
78600 68 61 73 20 74 6f 0a 2a 2a 20 64 65 61 6c 20 77 has to.** deal w
78610 69 74 68 20 74 68 65 20 70 65 6e 64 69 6e 67 20 ith the pending
78620 62 79 74 65 20 74 6f 20 72 75 6e 20 6f 6e 20 66 byte to run on f
78630 69 6c 65 73 20 74 68 61 74 20 61 72 65 20 6d 75 iles that are mu
78640 63 68 20 73 6d 61 6c 6c 65 72 0a 2a 2a 20 74 68 ch smaller.** th
78650 61 6e 20 31 20 47 69 42 2e 20 20 54 68 65 20 73 an 1 GiB. The s
78660 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 qlite3_test_cont
78670 72 6f 6c 28 29 20 69 6e 74 65 72 66 61 63 65 20 rol() interface
78680 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 0a 2a can be used to.*
78690 2a 20 6d 6f 76 65 20 74 68 65 20 70 65 6e 64 69 * move the pendi
786a0 6e 67 20 62 79 74 65 2e 0a 2a 2a 0a 2a 2a 20 49 ng byte..**.** I
786b0 4d 50 4f 52 54 41 4e 54 3a 20 20 43 68 61 6e 67 MPORTANT: Chang
786c0 69 6e 67 20 74 68 65 20 70 65 6e 64 69 6e 67 20 ing the pending
786d0 62 79 74 65 20 74 6f 20 61 6e 79 20 76 61 6c 75 byte to any valu
786e0 65 20 6f 74 68 65 72 20 74 68 61 6e 0a 2a 2a 20 e other than.**
786f0 30 78 34 30 30 30 30 30 30 30 20 72 65 73 75 6c 0x40000000 resul
78700 74 73 20 69 6e 20 61 6e 20 69 6e 63 6f 6d 70 61 ts in an incompa
78710 74 69 62 6c 65 20 64 61 74 61 62 61 73 65 20 66 tible database f
78720 69 6c 65 20 66 6f 72 6d 61 74 21 0a 2a 2a 20 43 ile format!.** C
78730 68 61 6e 67 69 6e 67 20 74 68 65 20 70 65 6e 64 hanging the pend
78740 69 6e 67 20 62 79 74 65 20 64 75 72 69 6e 67 20 ing byte during
78750 6f 70 65 72 61 74 69 6e 67 20 72 65 73 75 6c 74 operating result
78760 73 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 0a 2a s in undefined.*
78770 2a 20 61 6e 64 20 64 69 6c 65 74 65 72 69 6f 75 * and dileteriou
78780 73 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2f 0a 53 s behavior..*/.S
78790 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
787a0 74 20 73 71 6c 69 74 65 33 50 65 6e 64 69 6e 67 t sqlite3Pending
787b0 42 79 74 65 20 3d 20 30 78 34 30 30 30 30 30 30 Byte = 0x4000000
787c0 30 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0;../***********
787d0 2a 2a 2a 20 45 6e 64 20 6f 66 20 67 6c 6f 62 61 *** End of globa
787e0 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a l.c ************
787f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78810 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
78820 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73 *** Begin file s
78830 74 61 74 75 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a tatus.c ********
78840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78860 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4a **/./*.** 2008 J
78870 75 6e 65 20 31 38 0a 2a 2a 0a 2a 2a 20 54 68 65 une 18.**.** The
78880 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
78890 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
788a0 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
788b0 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
788c0 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
788d0 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
788e0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
788f0 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
78900 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
78910 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
78920 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
78930 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
78940 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
78950 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
78960 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
78970 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
78980 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
78990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
789a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
789b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
789c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
789d0 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 *****.**.** This
789e0 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e module implemen
789f0 74 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 ts the sqlite3_s
78a00 74 61 74 75 73 28 29 20 69 6e 74 65 72 66 61 63 tatus() interfac
78a10 65 20 61 6e 64 20 72 65 6c 61 74 65 64 0a 2a 2a e and related.**
78a20 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 2e 0a functionality..
78a30 2a 2a 0a 2a 2a 20 24 49 64 3a 20 73 74 61 74 75 **.** $Id: statu
78a40 73 2e 63 2c 76 20 31 2e 39 20 32 30 30 38 2f 30 s.c,v 1.9 2008/0
78a50 39 2f 30 32 20 30 30 3a 35 32 3a 35 32 20 64 72 9/02 00:52:52 dr
78a60 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a h Exp $.*/../*.*
78a70 2a 20 56 61 72 69 61 62 6c 65 73 20 69 6e 20 77 * Variables in w
78a80 68 69 63 68 20 74 6f 20 72 65 63 6f 72 64 20 73 hich to record s
78a90 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 69 6f tatus informatio
78aa0 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 n..*/.typedef st
78ab0 72 75 63 74 20 73 71 6c 69 74 65 33 53 74 61 74 ruct sqlite3Stat
78ac0 54 79 70 65 20 73 71 6c 69 74 65 33 53 74 61 74 Type sqlite3Stat
78ad0 54 79 70 65 3b 0a 73 74 61 74 69 63 20 53 51 4c Type;.static SQL
78ae0 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 73 ITE_WSD struct s
78af0 71 6c 69 74 65 33 53 74 61 74 54 79 70 65 20 7b qlite3StatType {
78b00 0a 20 20 69 6e 74 20 6e 6f 77 56 61 6c 75 65 5b . int nowValue[
78b10 39 5d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 9]; /* C
78b20 75 72 72 65 6e 74 20 76 61 6c 75 65 20 2a 2f 0a urrent value */.
78b30 20 20 69 6e 74 20 6d 78 56 61 6c 75 65 5b 39 5d int mxValue[9]
78b40 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 ; /* Ma
78b50 78 69 6d 75 6d 20 76 61 6c 75 65 20 2a 2f 0a 7d ximum value */.}
78b60 20 73 71 6c 69 74 65 33 53 74 61 74 20 3d 20 7b sqlite3Stat = {
78b70 20 7b 30 2c 7d 2c 20 7b 30 2c 7d 20 7d 3b 0a 0a {0,}, {0,} };..
78b80 0a 2f 2a 20 54 68 65 20 22 77 73 64 53 74 61 74 ./* The "wsdStat
78b90 22 20 6d 61 63 72 6f 20 77 69 6c 6c 20 72 65 73 " macro will res
78ba0 6f 6c 76 65 20 74 6f 20 74 68 65 20 73 74 61 74 olve to the stat
78bb0 75 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a us information.*
78bc0 2a 20 73 74 61 74 65 20 76 65 63 74 6f 72 2e 20 * state vector.
78bd0 20 49 66 20 77 72 69 74 61 62 6c 65 20 73 74 61 If writable sta
78be0 74 69 63 20 64 61 74 61 20 69 73 20 75 6e 73 75 tic data is unsu
78bf0 70 70 6f 72 74 65 64 20 6f 6e 20 74 68 65 20 74 pported on the t
78c00 61 72 67 65 74 2c 0a 2a 2a 20 77 65 20 68 61 76 arget,.** we hav
78c10 65 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 e to locate the
78c20 73 74 61 74 65 20 76 65 63 74 6f 72 20 61 74 20 state vector at
78c30 72 75 6e 2d 74 69 6d 65 2e 20 20 49 6e 20 74 68 run-time. In th
78c40 65 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a e more common.**
78c50 20 63 61 73 65 20 77 68 65 72 65 20 77 72 69 74 case where writ
78c60 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 74 61 able static data
78c70 20 69 73 20 73 75 70 70 6f 72 74 65 64 2c 20 77 is supported, w
78c80 73 64 53 74 61 74 20 63 61 6e 20 72 65 66 65 72 sdStat can refer
78c90 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 74 6f 20 directly.** to
78ca0 74 68 65 20 22 73 71 6c 69 74 65 33 53 74 61 74 the "sqlite3Stat
78cb0 22 20 73 74 61 74 65 20 76 65 63 74 6f 72 20 64 " state vector d
78cc0 65 63 6c 61 72 65 64 20 61 62 6f 76 65 2e 0a 2a eclared above..*
78cd0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
78ce0 4f 4d 49 54 5f 57 53 44 0a 23 20 64 65 66 69 6e OMIT_WSD.# defin
78cf0 65 20 77 73 64 53 74 61 74 49 6e 69 74 20 20 73 e wsdStatInit s
78d00 71 6c 69 74 65 33 53 74 61 74 54 79 70 65 20 2a qlite3StatType *
78d10 78 20 3d 20 26 47 4c 4f 42 41 4c 28 73 71 6c 69 x = &GLOBAL(sqli
78d20 74 65 33 53 74 61 74 54 79 70 65 2c 73 71 6c 69 te3StatType,sqli
78d30 74 65 33 53 74 61 74 29 0a 23 20 64 65 66 69 6e te3Stat).# defin
78d40 65 20 77 73 64 53 74 61 74 20 78 5b 30 5d 0a 23 e wsdStat x[0].#
78d50 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 77 73 else.# define ws
78d60 64 53 74 61 74 49 6e 69 74 0a 23 20 64 65 66 69 dStatInit.# defi
78d70 6e 65 20 77 73 64 53 74 61 74 20 73 71 6c 69 74 ne wsdStat sqlit
78d80 65 33 53 74 61 74 0a 23 65 6e 64 69 66 0a 0a 2f e3Stat.#endif../
78d90 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
78da0 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 current value of
78db0 20 61 20 73 74 61 74 75 73 20 70 61 72 61 6d 65 a status parame
78dc0 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ter..*/.SQLITE_P
78dd0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
78de0 65 33 53 74 61 74 75 73 56 61 6c 75 65 28 69 6e e3StatusValue(in
78df0 74 20 6f 70 29 7b 0a 20 20 77 73 64 53 74 61 74 t op){. wsdStat
78e00 49 6e 69 74 3b 0a 20 20 61 73 73 65 72 74 28 20 Init;. assert(
78e10 6f 70 3e 3d 30 20 26 26 20 6f 70 3c 41 72 72 61 op>=0 && op<Arra
78e20 79 53 69 7a 65 28 77 73 64 53 74 61 74 2e 6e 6f ySize(wsdStat.no
78e30 77 56 61 6c 75 65 29 20 29 3b 0a 20 20 72 65 74 wValue) );. ret
78e40 75 72 6e 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 urn wsdStat.nowV
78e50 61 6c 75 65 5b 6f 70 5d 3b 0a 7d 0a 0a 2f 2a 0a alue[op];.}../*.
78e60 2a 2a 20 41 64 64 20 4e 20 74 6f 20 74 68 65 20 ** Add N to the
78e70 76 61 6c 75 65 20 6f 66 20 61 20 73 74 61 74 75 value of a statu
78e80 73 20 72 65 63 6f 72 64 2e 20 20 49 74 20 69 73 s record. It is
78e90 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 assumed that th
78ea0 65 0a 2a 2a 20 63 61 6c 6c 65 72 20 68 6f 6c 64 e.** caller hold
78eb0 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 6c 6f s appropriate lo
78ec0 63 6b 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 cks..*/.SQLITE_P
78ed0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
78ee0 74 65 33 53 74 61 74 75 73 41 64 64 28 69 6e 74 te3StatusAdd(int
78ef0 20 6f 70 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 77 op, int N){. w
78f00 73 64 53 74 61 74 49 6e 69 74 3b 0a 20 20 61 73 sdStatInit;. as
78f10 73 65 72 74 28 20 6f 70 3e 3d 30 20 26 26 20 6f sert( op>=0 && o
78f20 70 3c 41 72 72 61 79 53 69 7a 65 28 77 73 64 53 p<ArraySize(wsdS
78f30 74 61 74 2e 6e 6f 77 56 61 6c 75 65 29 20 29 3b tat.nowValue) );
78f40 0a 20 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 . wsdStat.nowVa
78f50 6c 75 65 5b 6f 70 5d 20 2b 3d 20 4e 3b 0a 20 20 lue[op] += N;.
78f60 69 66 28 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 if( wsdStat.nowV
78f70 61 6c 75 65 5b 6f 70 5d 3e 77 73 64 53 74 61 74 alue[op]>wsdStat
78f80 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 29 7b 0a .mxValue[op] ){.
78f90 20 20 20 20 77 73 64 53 74 61 74 2e 6d 78 56 61 wsdStat.mxVa
78fa0 6c 75 65 5b 6f 70 5d 20 3d 20 77 73 64 53 74 61 lue[op] = wsdSta
78fb0 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a t.nowValue[op];.
78fc0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 }.}../*.** Set
78fd0 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20 the value of a
78fe0 73 74 61 74 75 73 20 74 6f 20 58 2e 0a 2a 2f 0a status to X..*/.
78ff0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
79000 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 74 75 oid sqlite3Statu
79010 73 53 65 74 28 69 6e 74 20 6f 70 2c 20 69 6e 74 sSet(int op, int
79020 20 58 29 7b 0a 20 20 77 73 64 53 74 61 74 49 6e X){. wsdStatIn
79030 69 74 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 it;. assert( op
79040 3e 3d 30 20 26 26 20 6f 70 3c 41 72 72 61 79 53 >=0 && op<ArrayS
79050 69 7a 65 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 ize(wsdStat.nowV
79060 61 6c 75 65 29 20 29 3b 0a 20 20 77 73 64 53 74 alue) );. wsdSt
79070 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 20 at.nowValue[op]
79080 3d 20 58 3b 0a 20 20 69 66 28 20 77 73 64 53 74 = X;. if( wsdSt
79090 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3e at.nowValue[op]>
790a0 77 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b wsdStat.mxValue[
790b0 6f 70 5d 20 29 7b 0a 20 20 20 20 77 73 64 53 74 op] ){. wsdSt
790c0 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 3d at.mxValue[op] =
790d0 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 wsdStat.nowValu
790e0 65 5b 6f 70 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a e[op];. }.}../*
790f0 0a 2a 2a 20 51 75 65 72 79 20 73 74 61 74 75 73 .** Query status
79100 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a information..**
79110 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 .** This impleme
79120 6e 74 61 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 ntation assumes
79130 74 68 61 74 20 72 65 61 64 69 6e 67 20 6f 72 20 that reading or
79140 77 72 69 74 69 6e 67 20 61 6e 20 61 6c 69 67 6e writing an align
79150 65 64 0a 2a 2a 20 33 32 2d 62 69 74 20 69 6e 74 ed.** 32-bit int
79160 65 67 65 72 20 69 73 20 61 6e 20 61 74 6f 6d 69 eger is an atomi
79170 63 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 49 66 c operation. If
79180 20 74 68 61 74 20 61 73 73 75 6d 70 74 69 6f 6e that assumption
79190 20 69 73 20 6e 6f 74 20 74 72 75 65 2c 0a 2a 2a is not true,.**
791a0 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 then this routi
791b0 6e 65 20 69 73 20 6e 6f 74 20 74 68 72 65 61 64 ne is not thread
791c0 73 61 66 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f safe..*/.SQLITE_
791d0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
791e0 73 74 61 74 75 73 28 69 6e 74 20 6f 70 2c 20 69 status(int op, i
791f0 6e 74 20 2a 70 43 75 72 72 65 6e 74 2c 20 69 6e nt *pCurrent, in
79200 74 20 2a 70 48 69 67 68 77 61 74 65 72 2c 20 69 t *pHighwater, i
79210 6e 74 20 72 65 73 65 74 46 6c 61 67 29 7b 0a 20 nt resetFlag){.
79220 20 77 73 64 53 74 61 74 49 6e 69 74 3b 0a 20 20 wsdStatInit;.
79230 69 66 28 20 6f 70 3c 30 20 7c 7c 20 6f 70 3e 3d if( op<0 || op>=
79240 41 72 72 61 79 53 69 7a 65 28 77 73 64 53 74 61 ArraySize(wsdSta
79250 74 2e 6e 6f 77 56 61 6c 75 65 29 20 29 7b 0a 20 t.nowValue) ){.
79260 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
79270 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 2a _MISUSE;. }. *
79280 70 43 75 72 72 65 6e 74 20 3d 20 77 73 64 53 74 pCurrent = wsdSt
79290 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b at.nowValue[op];
792a0 0a 20 20 2a 70 48 69 67 68 77 61 74 65 72 20 3d . *pHighwater =
792b0 20 77 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 wsdStat.mxValue
792c0 5b 6f 70 5d 3b 0a 20 20 69 66 28 20 72 65 73 65 [op];. if( rese
792d0 74 46 6c 61 67 20 29 7b 0a 20 20 20 20 77 73 64 tFlag ){. wsd
792e0 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d Stat.mxValue[op]
792f0 20 3d 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 = wsdStat.nowVa
79300 6c 75 65 5b 6f 70 5d 3b 0a 20 20 7d 0a 20 20 72 lue[op];. }. r
79310 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
79320 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 .}../*.** Query
79330 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 69 status informati
79340 6f 6e 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 on for a single
79350 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
79360 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ion.*/.SQLITE_AP
79370 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 62 I int sqlite3_db
79380 5f 73 74 61 74 75 73 28 0a 20 20 73 71 6c 69 74 _status(. sqlit
79390 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 e3 *db,
793a0 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 /* The database
793b0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 68 6f 73 connection whos
793c0 65 20 73 74 61 74 75 73 20 69 73 20 64 65 73 69 e status is desi
793d0 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c red */. int op,
793e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
793f0 2a 20 53 74 61 74 75 73 20 76 65 72 62 20 2a 2f * Status verb */
79400 0a 20 20 69 6e 74 20 2a 70 43 75 72 72 65 6e 74 . int *pCurrent
79410 2c 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 , /* Writ
79420 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 e current value
79430 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 here */. int *p
79440 48 69 67 68 77 61 74 65 72 2c 20 20 20 20 20 20 Highwater,
79450 2f 2a 20 57 72 69 74 65 20 68 69 67 68 2d 77 61 /* Write high-wa
79460 74 65 72 20 6d 61 72 6b 20 68 65 72 65 20 2a 2f ter mark here */
79470 0a 20 20 69 6e 74 20 72 65 73 65 74 46 6c 61 67 . int resetFlag
79480 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 65 /* Rese
79490 74 20 68 69 67 68 2d 77 61 74 65 72 20 6d 61 72 t high-water mar
794a0 6b 20 69 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a k if true */.){.
794b0 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a switch( op ){.
794c0 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
794d0 44 42 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 DBSTATUS_LOOKASI
794e0 44 45 5f 55 53 45 44 3a 20 7b 0a 20 20 20 20 20 DE_USED: {.
794f0 20 2a 70 43 75 72 72 65 6e 74 20 3d 20 64 62 2d *pCurrent = db-
79500 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3b >lookaside.nOut;
79510 0a 20 20 20 20 20 20 2a 70 48 69 67 68 77 61 74 . *pHighwat
79520 65 72 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 er = db->lookasi
79530 64 65 2e 6d 78 4f 75 74 3b 0a 20 20 20 20 20 20 de.mxOut;.
79540 69 66 28 20 72 65 73 65 74 46 6c 61 67 20 29 7b if( resetFlag ){
79550 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f . db->loo
79560 6b 61 73 69 64 65 2e 6d 78 4f 75 74 20 3d 20 64 kaside.mxOut = d
79570 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 b->lookaside.nOu
79580 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 t;. }.
79590 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
795a0 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 default: {.
795b0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
795c0 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 _ERROR;. }.
795d0 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 }. return SQLIT
795e0 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a E_OK;.}../******
795f0 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
79600 73 74 61 74 75 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a status.c *******
79610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79630 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
79640 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
79650 69 6c 65 20 64 61 74 65 2e 63 20 2a 2a 2a 2a 2a ile date.c *****
79660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79680 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
79690 30 30 33 20 4f 63 74 6f 62 65 72 20 33 31 0a 2a 003 October 31.*
796a0 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
796b0 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
796c0 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
796d0 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
796e0 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
796f0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
79700 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
79710 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
79720 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
79730 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
79740 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
79750 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
79760 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
79770 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
79780 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
79790 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
797a0 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
797b0 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
797c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
797d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
797e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
797f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
79800 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
79810 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 ins the C functi
79820 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 ons that impleme
79830 6e 74 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65 nt date and time
79840 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f .** functions fo
79850 72 20 53 51 4c 69 74 65 2e 20 20 0a 2a 2a 0a 2a r SQLite. .**.*
79860 2a 20 54 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 * There is only
79870 6f 6e 65 20 65 78 70 6f 72 74 65 64 20 73 79 6d one exported sym
79880 62 6f 6c 20 69 6e 20 74 68 69 73 20 66 69 6c 65 bol in this file
79890 20 2d 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 0a - the function.
798a0 2a 2a 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 ** sqlite3Regist
798b0 65 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69 erDateTimeFuncti
798c0 6f 6e 73 28 29 20 66 6f 75 6e 64 20 61 74 20 74 ons() found at t
798d0 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20 74 68 65 he bottom of the
798e0 20 66 69 6c 65 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 file..** All ot
798f0 68 65 72 20 63 6f 64 65 20 68 61 73 20 66 69 6c her code has fil
79900 65 20 73 63 6f 70 65 2e 0a 2a 2a 0a 2a 2a 20 24 e scope..**.** $
79910 49 64 3a 20 64 61 74 65 2e 63 2c 76 20 31 2e 31 Id: date.c,v 1.1
79920 30 37 20 32 30 30 39 2f 30 35 2f 30 33 20 32 30 07 2009/05/03 20
79930 3a 32 33 3a 35 33 20 64 72 68 20 45 78 70 20 24 :23:53 drh Exp $
79940 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 70 72 .**.** SQLite pr
79950 6f 63 65 73 73 65 73 20 61 6c 6c 20 74 69 6d 65 ocesses all time
79960 73 20 61 6e 64 20 64 61 74 65 73 20 61 73 20 4a s and dates as J
79970 75 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 ulian Day number
79980 73 2e 20 20 54 68 65 0a 2a 2a 20 64 61 74 65 73 s. The.** dates
79990 20 61 6e 64 20 74 69 6d 65 73 20 61 72 65 20 73 and times are s
799a0 74 6f 72 65 64 20 61 73 20 74 68 65 20 6e 75 6d tored as the num
799b0 62 65 72 20 6f 66 20 64 61 79 73 20 73 69 6e 63 ber of days sinc
799c0 65 20 6e 6f 6f 6e 0a 2a 2a 20 69 6e 20 47 72 65 e noon.** in Gre
799d0 65 6e 77 69 63 68 20 6f 6e 20 4e 6f 76 65 6d 62 enwich on Novemb
799e0 65 72 20 32 34 2c 20 34 37 31 34 20 42 2e 43 2e er 24, 4714 B.C.
799f0 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 according to th
79a00 65 20 47 72 65 67 6f 72 69 61 6e 0a 2a 2a 20 63 e Gregorian.** c
79a10 61 6c 65 6e 64 61 72 20 73 79 73 74 65 6d 2e 20 alendar system.
79a20 0a 2a 2a 0a 2a 2a 20 31 39 37 30 2d 30 31 2d 30 .**.** 1970-01-0
79a30 31 20 30 30 3a 30 30 3a 30 30 20 69 73 20 4a 44 1 00:00:00 is JD
79a40 20 32 34 34 30 35 38 37 2e 35 0a 2a 2a 20 32 30 2440587.5.** 20
79a50 30 30 2d 30 31 2d 30 31 20 30 30 3a 30 30 3a 30 00-01-01 00:00:0
79a60 30 20 69 73 20 4a 44 20 32 34 35 31 35 34 34 2e 0 is JD 2451544.
79a70 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70 5.**.** This imp
79a80 6c 65 6d 65 6e 74 69 6f 6e 20 72 65 71 75 69 72 lemention requir
79a90 65 73 20 79 65 61 72 73 20 74 6f 20 62 65 20 65 es years to be e
79aa0 78 70 72 65 73 73 65 64 20 61 73 20 61 20 34 2d xpressed as a 4-
79ab0 64 69 67 69 74 20 6e 75 6d 62 65 72 0a 2a 2a 20 digit number.**
79ac0 77 68 69 63 68 20 6d 65 61 6e 73 20 74 68 61 74 which means that
79ad0 20 6f 6e 6c 79 20 64 61 74 65 73 20 62 65 74 77 only dates betw
79ae0 65 65 6e 20 30 30 30 30 2d 30 31 2d 30 31 20 61 een 0000-01-01 a
79af0 6e 64 20 39 39 39 39 2d 31 32 2d 33 31 20 63 61 nd 9999-12-31 ca
79b00 6e 0a 2a 2a 20 62 65 20 72 65 70 72 65 73 65 6e n.** be represen
79b10 74 65 64 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 ted, even though
79b20 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 julian day numb
79b30 65 72 73 20 61 6c 6c 6f 77 20 61 20 6d 75 63 68 ers allow a much
79b40 20 77 69 64 65 72 0a 2a 2a 20 72 61 6e 67 65 20 wider.** range
79b50 6f 66 20 64 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 of dates..**.**
79b60 54 68 65 20 47 72 65 67 6f 72 69 61 6e 20 63 61 The Gregorian ca
79b70 6c 65 6e 64 61 72 20 73 79 73 74 65 6d 20 69 73 lendar system is
79b80 20 75 73 65 64 20 66 6f 72 20 61 6c 6c 20 64 61 used for all da
79b90 74 65 73 20 61 6e 64 20 74 69 6d 65 73 2c 0a 2a tes and times,.*
79ba0 2a 20 65 76 65 6e 20 74 68 6f 73 65 20 74 68 61 * even those tha
79bb0 74 20 70 72 65 64 61 74 65 20 74 68 65 20 47 72 t predate the Gr
79bc0 65 67 6f 72 69 61 6e 20 63 61 6c 65 6e 64 61 72 egorian calendar
79bd0 2e 20 20 48 69 73 74 6f 72 69 61 6e 73 20 75 73 . Historians us
79be0 75 61 6c 6c 79 0a 2a 2a 20 75 73 65 20 74 68 65 ually.** use the
79bf0 20 4a 75 6c 69 61 6e 20 63 61 6c 65 6e 64 61 72 Julian calendar
79c00 20 66 6f 72 20 64 61 74 65 73 20 70 72 69 6f 72 for dates prior
79c10 20 74 6f 20 31 35 38 32 2d 31 30 2d 31 35 20 61 to 1582-10-15 a
79c20 6e 64 20 66 6f 72 20 73 6f 6d 65 0a 2a 2a 20 64 nd for some.** d
79c30 61 74 65 73 20 61 66 74 65 72 77 61 72 64 73 2c ates afterwards,
79c40 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 6c 6f depending on lo
79c50 63 61 6c 65 2e 20 20 42 65 77 61 72 65 20 6f 66 cale. Beware of
79c60 20 74 68 69 73 20 64 69 66 66 65 72 65 6e 63 65 this difference
79c70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 76 ..**.** The conv
79c80 65 72 73 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d ersion algorithm
79c90 73 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 s are implemente
79ca0 64 20 62 61 73 65 64 20 6f 6e 20 64 65 73 63 72 d based on descr
79cb0 69 70 74 69 6f 6e 73 0a 2a 2a 20 69 6e 20 74 68 iptions.** in th
79cc0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 65 78 74 e following text
79cd0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 4a 65 61 :.**.** Jea
79ce0 6e 20 4d 65 65 75 73 0a 2a 2a 20 20 20 20 20 20 n Meeus.**
79cf0 41 73 74 72 6f 6e 6f 6d 69 63 61 6c 20 41 6c 67 Astronomical Alg
79d00 6f 72 69 74 68 6d 73 2c 20 32 6e 64 20 45 64 69 orithms, 2nd Edi
79d10 74 69 6f 6e 2c 20 31 39 39 38 0a 2a 2a 20 20 20 tion, 1998.**
79d20 20 20 20 49 53 42 4d 20 30 2d 39 34 33 33 39 36 ISBM 0-943396
79d30 2d 36 31 2d 31 0a 2a 2a 20 20 20 20 20 20 57 69 -61-1.** Wi
79d40 6c 6c 6d 61 6e 6e 2d 42 65 6c 6c 2c 20 49 6e 63 llmann-Bell, Inc
79d50 0a 2a 2a 20 20 20 20 20 20 52 69 63 68 6d 6f 6e .** Richmon
79d60 64 2c 20 56 69 72 67 69 6e 69 61 20 28 55 53 41 d, Virginia (USA
79d70 29 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 74 ).*/.#include <t
79d80 69 6d 65 2e 68 3e 0a 0a 23 69 66 6e 64 65 66 20 ime.h>..#ifndef
79d90 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 SQLITE_OMIT_DATE
79da0 54 49 4d 45 5f 46 55 4e 43 53 0a 0a 2f 2a 0a 2a TIME_FUNCS../*.*
79db0 2a 20 4f 6e 20 72 65 63 65 6e 74 20 57 69 6e 64 * On recent Wind
79dc0 6f 77 73 20 70 6c 61 74 66 6f 72 6d 73 2c 20 74 ows platforms, t
79dd0 68 65 20 6c 6f 63 61 6c 74 69 6d 65 5f 73 28 29 he localtime_s()
79de0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 76 61 function is ava
79df0 69 6c 61 62 6c 65 0a 2a 2a 20 61 73 20 70 61 72 ilable.** as par
79e00 74 20 6f 66 20 74 68 65 20 22 53 65 63 75 72 65 t of the "Secure
79e10 20 43 52 54 22 2e 20 49 74 20 69 73 20 65 73 73 CRT". It is ess
79e20 65 6e 74 69 61 6c 6c 79 20 65 71 75 69 76 61 6c entially equival
79e30 65 6e 74 20 74 6f 20 0a 2a 2a 20 6c 6f 63 61 6c ent to .** local
79e40 74 69 6d 65 5f 72 28 29 20 61 76 61 69 6c 61 62 time_r() availab
79e50 6c 65 20 75 6e 64 65 72 20 6d 6f 73 74 20 50 4f le under most PO
79e60 53 49 58 20 70 6c 61 74 66 6f 72 6d 73 2c 20 65 SIX platforms, e
79e70 78 63 65 70 74 20 74 68 61 74 20 74 68 65 20 0a xcept that the .
79e80 2a 2a 20 6f 72 64 65 72 20 6f 66 20 74 68 65 20 ** order of the
79e90 70 61 72 61 6d 65 74 65 72 73 20 69 73 20 72 65 parameters is re
79ea0 76 65 72 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 versed..**.** Se
79eb0 65 20 68 74 74 70 3a 2f 2f 6d 73 64 6e 2e 6d 69 e http://msdn.mi
79ec0 63 72 6f 73 6f 66 74 2e 63 6f 6d 2f 65 6e 2d 75 crosoft.com/en-u
79ed0 73 2f 6c 69 62 72 61 72 79 2f 61 34 34 32 78 33 s/library/a442x3
79ee0 79 65 28 56 53 2e 38 30 29 2e 61 73 70 78 2e 0a ye(VS.80).aspx..
79ef0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 75 73 65 **.** If the use
79f00 72 20 68 61 73 20 6e 6f 74 20 69 6e 64 69 63 61 r has not indica
79f10 74 65 64 20 74 6f 20 75 73 65 20 6c 6f 63 61 6c ted to use local
79f20 74 69 6d 65 5f 72 28 29 20 6f 72 20 6c 6f 63 61 time_r() or loca
79f30 6c 74 69 6d 65 5f 73 28 29 0a 2a 2a 20 61 6c 72 ltime_s().** alr
79f40 65 61 64 79 2c 20 63 68 65 63 6b 20 66 6f 72 20 eady, check for
79f50 61 6e 20 4d 53 56 43 20 62 75 69 6c 64 20 65 6e an MSVC build en
79f60 76 69 72 6f 6e 6d 65 6e 74 20 74 68 61 74 20 70 vironment that p
79f70 72 6f 76 69 64 65 73 20 0a 2a 2a 20 6c 6f 63 61 rovides .** loca
79f80 6c 74 69 6d 65 5f 73 28 29 2e 0a 2a 2f 0a 23 69 ltime_s()..*/.#i
79f90 66 20 21 64 65 66 69 6e 65 64 28 48 41 56 45 5f f !defined(HAVE_
79fa0 4c 4f 43 41 4c 54 49 4d 45 5f 52 29 20 26 26 20 LOCALTIME_R) &&
79fb0 21 64 65 66 69 6e 65 64 28 48 41 56 45 5f 4c 4f !defined(HAVE_LO
79fc0 43 41 4c 54 49 4d 45 5f 53 29 20 26 26 20 5c 0a CALTIME_S) && \.
79fd0 20 20 20 20 20 64 65 66 69 6e 65 64 28 5f 4d 53 defined(_MS
79fe0 43 5f 56 45 52 29 20 26 26 20 64 65 66 69 6e 65 C_VER) && define
79ff0 64 28 5f 43 52 54 5f 49 4e 53 45 43 55 52 45 5f d(_CRT_INSECURE_
7a000 44 45 50 52 45 43 41 54 45 29 0a 23 64 65 66 69 DEPRECATE).#defi
7a010 6e 65 20 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d ne HAVE_LOCALTIM
7a020 45 5f 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a E_S 1.#endif../*
7a030 0a 2a 2a 20 41 20 73 74 72 75 63 74 75 72 65 20 .** A structure
7a040 66 6f 72 20 68 6f 6c 64 69 6e 67 20 61 20 73 69 for holding a si
7a050 6e 67 6c 65 20 64 61 74 65 20 61 6e 64 20 74 69 ngle date and ti
7a060 6d 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 me..*/.typedef s
7a070 74 72 75 63 74 20 44 61 74 65 54 69 6d 65 20 44 truct DateTime D
7a080 61 74 65 54 69 6d 65 3b 0a 73 74 72 75 63 74 20 ateTime;.struct
7a090 44 61 74 65 54 69 6d 65 20 7b 0a 20 20 73 71 6c DateTime {. sql
7a0a0 69 74 65 33 5f 69 6e 74 36 34 20 69 4a 44 3b 20 ite3_int64 iJD;
7a0b0 2f 2a 20 54 68 65 20 6a 75 6c 69 61 6e 20 64 61 /* The julian da
7a0c0 79 20 6e 75 6d 62 65 72 20 74 69 6d 65 73 20 38 y number times 8
7a0d0 36 34 30 30 30 30 30 20 2a 2f 0a 20 20 69 6e 74 6400000 */. int
7a0e0 20 59 2c 20 4d 2c 20 44 3b 20 20 20 20 20 20 20 Y, M, D;
7a0f0 2f 2a 20 59 65 61 72 2c 20 6d 6f 6e 74 68 2c 20 /* Year, month,
7a100 61 6e 64 20 64 61 79 20 2a 2f 0a 20 20 69 6e 74 and day */. int
7a110 20 68 2c 20 6d 3b 20 20 20 20 20 20 20 20 20 20 h, m;
7a120 2f 2a 20 48 6f 75 72 20 61 6e 64 20 6d 69 6e 75 /* Hour and minu
7a130 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 74 7a 3b tes */. int tz;
7a140 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
7a150 69 6d 65 7a 6f 6e 65 20 6f 66 66 73 65 74 20 69 imezone offset i
7a160 6e 20 6d 69 6e 75 74 65 73 20 2a 2f 0a 20 20 64 n minutes */. d
7a170 6f 75 62 6c 65 20 73 3b 20 20 20 20 20 20 20 20 ouble s;
7a180 20 20 2f 2a 20 53 65 63 6f 6e 64 73 20 2a 2f 0a /* Seconds */.
7a190 20 20 63 68 61 72 20 76 61 6c 69 64 59 4d 44 3b char validYMD;
7a1a0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 28 31 29 /* True (1)
7a1b0 20 69 66 20 59 2c 4d 2c 44 20 61 72 65 20 76 61 if Y,M,D are va
7a1c0 6c 69 64 20 2a 2f 0a 20 20 63 68 61 72 20 76 61 lid */. char va
7a1d0 6c 69 64 48 4d 53 3b 20 20 20 20 20 2f 2a 20 54 lidHMS; /* T
7a1e0 72 75 65 20 28 31 29 20 69 66 20 68 2c 6d 2c 73 rue (1) if h,m,s
7a1f0 20 61 72 65 20 76 61 6c 69 64 20 2a 2f 0a 20 20 are valid */.
7a200 63 68 61 72 20 76 61 6c 69 64 4a 44 3b 20 20 20 char validJD;
7a210 20 20 20 2f 2a 20 54 72 75 65 20 28 31 29 20 69 /* True (1) i
7a220 66 20 69 4a 44 20 69 73 20 76 61 6c 69 64 20 2a f iJD is valid *
7a230 2f 0a 20 20 63 68 61 72 20 76 61 6c 69 64 54 5a /. char validTZ
7a240 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 28 ; /* True (
7a250 31 29 20 69 66 20 74 7a 20 69 73 20 76 61 6c 69 1) if tz is vali
7a260 64 20 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 d */.};.../*.**
7a270 43 6f 6e 76 65 72 74 20 7a 44 61 74 65 20 69 6e Convert zDate in
7a280 74 6f 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 69 to one or more i
7a290 6e 74 65 67 65 72 73 2e 20 20 41 64 64 69 74 69 ntegers. Additi
7a2a0 6f 6e 61 6c 20 61 72 67 75 6d 65 6e 74 73 0a 2a onal arguments.*
7a2b0 2a 20 63 6f 6d 65 20 69 6e 20 67 72 6f 75 70 73 * come in groups
7a2c0 20 6f 66 20 35 20 61 73 20 66 6f 6c 6c 6f 77 73 of 5 as follows
7a2d0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4e 20 :.**.** N
7a2e0 20 20 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 number of
7a2f0 64 69 67 69 74 73 20 69 6e 20 74 68 65 20 69 6e digits in the in
7a300 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 6d teger.** m
7a310 69 6e 20 20 20 20 20 6d 69 6e 69 6d 75 6d 20 61 in minimum a
7a320 6c 6c 6f 77 65 64 20 76 61 6c 75 65 20 6f 66 20 llowed value of
7a330 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 the integer.**
7a340 20 20 20 20 20 6d 61 78 20 20 20 20 20 6d 61 78 max max
7a350 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 76 61 6c imum allowed val
7a360 75 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 ue of the intege
7a370 72 0a 2a 2a 20 20 20 20 20 20 20 6e 65 78 74 43 r.** nextC
7a380 20 20 20 66 69 72 73 74 20 63 68 61 72 61 63 74 first charact
7a390 65 72 20 61 66 74 65 72 20 74 68 65 20 69 6e 74 er after the int
7a3a0 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 70 56 eger.** pV
7a3b0 61 6c 20 20 20 20 77 68 65 72 65 20 74 6f 20 77 al where to w
7a3c0 72 69 74 65 20 74 68 65 20 69 6e 74 65 67 65 72 rite the integer
7a3d0 73 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 43 s value..**.** C
7a3e0 6f 6e 76 65 72 73 69 6f 6e 73 20 63 6f 6e 74 69 onversions conti
7a3f0 6e 75 65 20 75 6e 74 69 6c 20 6f 6e 65 20 77 69 nue until one wi
7a400 74 68 20 6e 65 78 74 43 3d 3d 30 20 69 73 20 65 th nextC==0 is e
7a410 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2a 20 54 ncountered..** T
7a420 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 he function retu
7a430 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f rns the number o
7a440 66 20 73 75 63 63 65 73 73 66 75 6c 20 63 6f 6e f successful con
7a450 76 65 72 73 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 versions..*/.sta
7a460 74 69 63 20 69 6e 74 20 67 65 74 44 69 67 69 74 tic int getDigit
7a470 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 s(const char *zD
7a480 61 74 65 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f ate, ...){. va_
7a490 6c 69 73 74 20 61 70 3b 0a 20 20 69 6e 74 20 76 list ap;. int v
7a4a0 61 6c 3b 0a 20 20 69 6e 74 20 4e 3b 0a 20 20 69 al;. int N;. i
7a4b0 6e 74 20 6d 69 6e 3b 0a 20 20 69 6e 74 20 6d 61 nt min;. int ma
7a4c0 78 3b 0a 20 20 69 6e 74 20 6e 65 78 74 43 3b 0a x;. int nextC;.
7a4d0 20 20 69 6e 74 20 2a 70 56 61 6c 3b 0a 20 20 69 int *pVal;. i
7a4e0 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 76 61 nt cnt = 0;. va
7a4f0 5f 73 74 61 72 74 28 61 70 2c 20 7a 44 61 74 65 _start(ap, zDate
7a500 29 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 4e 20 3d );. do{. N =
7a510 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 va_arg(ap, int)
7a520 3b 0a 20 20 20 20 6d 69 6e 20 3d 20 76 61 5f 61 ;. min = va_a
7a530 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 rg(ap, int);.
7a540 20 6d 61 78 20 3d 20 76 61 5f 61 72 67 28 61 70 max = va_arg(ap
7a550 2c 20 69 6e 74 29 3b 0a 20 20 20 20 6e 65 78 74 , int);. next
7a560 43 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 C = va_arg(ap, i
7a570 6e 74 29 3b 0a 20 20 20 20 70 56 61 6c 20 3d 20 nt);. pVal =
7a580 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 2a 29 va_arg(ap, int*)
7a590 3b 0a 20 20 20 20 76 61 6c 20 3d 20 30 3b 0a 20 ;. val = 0;.
7a5a0 20 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 29 7b while( N-- ){
7a5b0 0a 20 20 20 20 20 20 69 66 28 20 21 73 71 6c 69 . if( !sqli
7a5c0 74 65 33 49 73 64 69 67 69 74 28 2a 7a 44 61 74 te3Isdigit(*zDat
7a5d0 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f e) ){. go
7a5e0 74 6f 20 65 6e 64 5f 67 65 74 44 69 67 69 74 73 to end_getDigits
7a5f0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
7a600 76 61 6c 20 3d 20 76 61 6c 2a 31 30 20 2b 20 2a val = val*10 + *
7a610 7a 44 61 74 65 20 2d 20 27 30 27 3b 0a 20 20 20 zDate - '0';.
7a620 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 zDate++;.
7a630 7d 0a 20 20 20 20 69 66 28 20 76 61 6c 3c 6d 69 }. if( val<mi
7a640 6e 20 7c 7c 20 76 61 6c 3e 6d 61 78 20 7c 7c 20 n || val>max ||
7a650 28 6e 65 78 74 43 21 3d 30 20 26 26 20 6e 65 78 (nextC!=0 && nex
7a660 74 43 21 3d 2a 7a 44 61 74 65 29 20 29 7b 0a 20 tC!=*zDate) ){.
7a670 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 67 65 goto end_ge
7a680 74 44 69 67 69 74 73 3b 0a 20 20 20 20 7d 0a 20 tDigits;. }.
7a690 20 20 20 2a 70 56 61 6c 20 3d 20 76 61 6c 3b 0a *pVal = val;.
7a6a0 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 zDate++;.
7a6b0 20 63 6e 74 2b 2b 3b 0a 20 20 7d 77 68 69 6c 65 cnt++;. }while
7a6c0 28 20 6e 65 78 74 43 20 29 3b 0a 65 6e 64 5f 67 ( nextC );.end_g
7a6d0 65 74 44 69 67 69 74 73 3a 0a 20 20 76 61 5f 65 etDigits:. va_e
7a6e0 6e 64 28 61 70 29 3b 0a 20 20 72 65 74 75 72 6e nd(ap);. return
7a6f0 20 63 6e 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 cnt;.}../*.** R
7a700 65 61 64 20 74 65 78 74 20 66 72 6f 6d 20 7a 5b ead text from z[
7a710 5d 20 61 6e 64 20 63 6f 6e 76 65 72 74 20 69 6e ] and convert in
7a720 74 6f 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f to a floating po
7a730 69 6e 74 20 6e 75 6d 62 65 72 2e 20 20 52 65 74 int number. Ret
7a740 75 72 6e 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 urn.** the numbe
7a750 72 20 6f 66 20 64 69 67 69 74 73 20 63 6f 6e 76 r of digits conv
7a760 65 72 74 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e erted..*/.#defin
7a770 65 20 67 65 74 56 61 6c 75 65 20 73 71 6c 69 74 e getValue sqlit
7a780 65 33 41 74 6f 46 0a 0a 2f 2a 0a 2a 2a 20 50 61 e3AtoF../*.** Pa
7a790 72 73 65 20 61 20 74 69 6d 65 7a 6f 6e 65 20 65 rse a timezone e
7a7a0 78 74 65 6e 73 69 6f 6e 20 6f 6e 20 74 68 65 20 xtension on the
7a7b0 65 6e 64 20 6f 66 20 61 20 64 61 74 65 2d 74 69 end of a date-ti
7a7c0 6d 65 2e 0a 2a 2a 20 54 68 65 20 65 78 74 65 6e me..** The exten
7a7d0 73 69 6f 6e 20 69 73 20 6f 66 20 74 68 65 20 66 sion is of the f
7a7e0 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 orm:.**.**
7a7f0 20 20 28 2b 2f 2d 29 48 48 3a 4d 4d 0a 2a 2a 0a (+/-)HH:MM.**.
7a800 2a 2a 20 4f 72 20 74 68 65 20 22 7a 75 6c 75 22 ** Or the "zulu"
7a810 20 6e 6f 74 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a notation:.**.**
7a820 20 20 20 20 20 20 20 20 5a 0a 2a 2a 0a 2a 2a 20 Z.**.**
7a830 49 66 20 74 68 65 20 70 61 72 73 65 20 69 73 20 If the parse is
7a840 73 75 63 63 65 73 73 66 75 6c 2c 20 77 72 69 74 successful, writ
7a850 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 e the number of
7a860 6d 69 6e 75 74 65 73 0a 2a 2a 20 6f 66 20 63 68 minutes.** of ch
7a870 61 6e 67 65 20 69 6e 20 70 2d 3e 74 7a 20 61 6e ange in p->tz an
7a880 64 20 72 65 74 75 72 6e 20 30 2e 20 20 49 66 20 d return 0. If
7a890 61 20 70 61 72 73 65 72 20 65 72 72 6f 72 20 6f a parser error o
7a8a0 63 63 75 72 73 2c 0a 2a 2a 20 72 65 74 75 72 6e ccurs,.** return
7a8b0 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a non-zero..**.**
7a8c0 20 41 20 6d 69 73 73 69 6e 67 20 73 70 65 63 69 A missing speci
7a8d0 66 69 65 72 20 69 73 20 6e 6f 74 20 63 6f 6e 73 fier is not cons
7a8e0 69 64 65 72 65 64 20 61 6e 20 65 72 72 6f 72 2e idered an error.
7a8f0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
7a900 61 72 73 65 54 69 6d 65 7a 6f 6e 65 28 63 6f 6e arseTimezone(con
7a910 73 74 20 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 st char *zDate,
7a920 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 DateTime *p){.
7a930 69 6e 74 20 73 67 6e 20 3d 20 30 3b 0a 20 20 69 int sgn = 0;. i
7a940 6e 74 20 6e 48 72 2c 20 6e 4d 6e 3b 0a 20 20 69 nt nHr, nMn;. i
7a950 6e 74 20 63 3b 0a 20 20 77 68 69 6c 65 28 20 73 nt c;. while( s
7a960 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a qlite3Isspace(*z
7a970 44 61 74 65 29 20 29 7b 20 7a 44 61 74 65 2b 2b Date) ){ zDate++
7a980 3b 20 7d 0a 20 20 70 2d 3e 74 7a 20 3d 20 30 3b ; }. p->tz = 0;
7a990 0a 20 20 63 20 3d 20 2a 7a 44 61 74 65 3b 0a 20 . c = *zDate;.
7a9a0 20 69 66 28 20 63 3d 3d 27 2d 27 20 29 7b 0a 20 if( c=='-' ){.
7a9b0 20 20 20 73 67 6e 20 3d 20 2d 31 3b 0a 20 20 7d sgn = -1;. }
7a9c0 65 6c 73 65 20 69 66 28 20 63 3d 3d 27 2b 27 20 else if( c=='+'
7a9d0 29 7b 0a 20 20 20 20 73 67 6e 20 3d 20 2b 31 3b ){. sgn = +1;
7a9e0 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d . }else if( c==
7a9f0 27 5a 27 20 7c 7c 20 63 3d 3d 27 7a 27 20 29 7b 'Z' || c=='z' ){
7aa00 0a 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 . zDate++;.
7aa10 20 20 67 6f 74 6f 20 7a 75 6c 75 5f 74 69 6d 65 goto zulu_time
7aa20 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 ;. }else{. r
7aa30 65 74 75 72 6e 20 63 21 3d 30 3b 0a 20 20 7d 0a eturn c!=0;. }.
7aa40 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 69 66 28 zDate++;. if(
7aa50 20 67 65 74 44 69 67 69 74 73 28 7a 44 61 74 65 getDigits(zDate
7aa60 2c 20 32 2c 20 30 2c 20 31 34 2c 20 27 3a 27 2c , 2, 0, 14, ':',
7aa70 20 26 6e 48 72 2c 20 32 2c 20 30 2c 20 35 39 2c &nHr, 2, 0, 59,
7aa80 20 30 2c 20 26 6e 4d 6e 29 21 3d 32 20 29 7b 0a 0, &nMn)!=2 ){.
7aa90 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 return 1;.
7aaa0 7d 0a 20 20 7a 44 61 74 65 20 2b 3d 20 35 3b 0a }. zDate += 5;.
7aab0 20 20 70 2d 3e 74 7a 20 3d 20 73 67 6e 2a 28 6e p->tz = sgn*(n
7aac0 4d 6e 20 2b 20 6e 48 72 2a 36 30 29 3b 0a 7a 75 Mn + nHr*60);.zu
7aad0 6c 75 5f 74 69 6d 65 3a 0a 20 20 77 68 69 6c 65 lu_time:. while
7aae0 28 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 ( sqlite3Isspace
7aaf0 28 2a 7a 44 61 74 65 29 20 29 7b 20 7a 44 61 74 (*zDate) ){ zDat
7ab00 65 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 e++; }. return
7ab10 2a 7a 44 61 74 65 21 3d 30 3b 0a 7d 0a 0a 2f 2a *zDate!=0;.}../*
7ab20 0a 2a 2a 20 50 61 72 73 65 20 74 69 6d 65 73 20 .** Parse times
7ab30 6f 66 20 74 68 65 20 66 6f 72 6d 20 48 48 3a 4d of the form HH:M
7ab40 4d 20 6f 72 20 48 48 3a 4d 4d 3a 53 53 20 6f 72 M or HH:MM:SS or
7ab50 20 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 46 2e 0a HH:MM:SS.FFFF..
7ab60 2a 2a 20 54 68 65 20 48 48 2c 20 4d 4d 2c 20 61 ** The HH, MM, a
7ab70 6e 64 20 53 53 20 6d 75 73 74 20 65 61 63 68 20 nd SS must each
7ab80 62 65 20 65 78 61 63 74 6c 79 20 32 20 64 69 67 be exactly 2 dig
7ab90 69 74 73 2e 20 20 54 68 65 0a 2a 2a 20 66 72 61 its. The.** fra
7aba0 63 74 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 73 20 ctional seconds
7abb0 46 46 46 46 20 63 61 6e 20 62 65 20 6f 6e 65 20 FFFF can be one
7abc0 6f 72 20 6d 6f 72 65 20 64 69 67 69 74 73 2e 0a or more digits..
7abd0 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 31 20 69 **.** Return 1 i
7abe0 66 20 74 68 65 72 65 20 69 73 20 61 20 70 61 72 f there is a par
7abf0 73 69 6e 67 20 65 72 72 6f 72 20 61 6e 64 20 30 sing error and 0
7ac00 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2f 0a on success..*/.
7ac10 73 74 61 74 69 63 20 69 6e 74 20 70 61 72 73 65 static int parse
7ac20 48 68 4d 6d 53 73 28 63 6f 6e 73 74 20 63 68 61 HhMmSs(const cha
7ac30 72 20 2a 7a 44 61 74 65 2c 20 44 61 74 65 54 69 r *zDate, DateTi
7ac40 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 68 2c me *p){. int h,
7ac50 20 6d 2c 20 73 3b 0a 20 20 64 6f 75 62 6c 65 20 m, s;. double
7ac60 6d 73 20 3d 20 30 2e 30 3b 0a 20 20 69 66 28 20 ms = 0.0;. if(
7ac70 67 65 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c getDigits(zDate,
7ac80 20 32 2c 20 30 2c 20 32 34 2c 20 27 3a 27 2c 20 2, 0, 24, ':',
7ac90 26 68 2c 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c &h, 2, 0, 59, 0,
7aca0 20 26 6d 29 21 3d 32 20 29 7b 0a 20 20 20 20 72 &m)!=2 ){. r
7acb0 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a eturn 1;. }. z
7acc0 44 61 74 65 20 2b 3d 20 35 3b 0a 20 20 69 66 28 Date += 5;. if(
7acd0 20 2a 7a 44 61 74 65 3d 3d 27 3a 27 20 29 7b 0a *zDate==':' ){.
7ace0 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 zDate++;.
7acf0 20 69 66 28 20 67 65 74 44 69 67 69 74 73 28 7a if( getDigits(z
7ad00 44 61 74 65 2c 20 32 2c 20 30 2c 20 35 39 2c 20 Date, 2, 0, 59,
7ad10 30 2c 20 26 73 29 21 3d 31 20 29 7b 0a 20 20 20 0, &s)!=1 ){.
7ad20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 return 1;.
7ad30 20 7d 0a 20 20 20 20 7a 44 61 74 65 20 2b 3d 20 }. zDate +=
7ad40 32 3b 0a 20 20 20 20 69 66 28 20 2a 7a 44 61 74 2;. if( *zDat
7ad50 65 3d 3d 27 2e 27 20 26 26 20 73 71 6c 69 74 65 e=='.' && sqlite
7ad60 33 49 73 64 69 67 69 74 28 7a 44 61 74 65 5b 31 3Isdigit(zDate[1
7ad70 5d 29 20 29 7b 0a 20 20 20 20 20 20 64 6f 75 62 ]) ){. doub
7ad80 6c 65 20 72 53 63 61 6c 65 20 3d 20 31 2e 30 3b le rScale = 1.0;
7ad90 0a 20 20 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a . zDate++;.
7ada0 20 20 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c while( sql
7adb0 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 44 61 ite3Isdigit(*zDa
7adc0 74 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 6d te) ){. m
7add0 73 20 3d 20 6d 73 2a 31 30 2e 30 20 2b 20 2a 7a s = ms*10.0 + *z
7ade0 44 61 74 65 20 2d 20 27 30 27 3b 0a 20 20 20 20 Date - '0';.
7adf0 20 20 20 20 72 53 63 61 6c 65 20 2a 3d 20 31 30 rScale *= 10
7ae00 2e 30 3b 0a 20 20 20 20 20 20 20 20 7a 44 61 74 .0;. zDat
7ae10 65 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 e++;. }.
7ae20 20 20 20 6d 73 20 2f 3d 20 72 53 63 61 6c 65 3b ms /= rScale;
7ae30 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a . }. }else{.
7ae40 20 20 20 20 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 s = 0;. }.
7ae50 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b p->validJD = 0;
7ae60 0a 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d . p->validHMS =
7ae70 20 31 3b 0a 20 20 70 2d 3e 68 20 3d 20 68 3b 0a 1;. p->h = h;.
7ae80 20 20 70 2d 3e 6d 20 3d 20 6d 3b 0a 20 20 70 2d p->m = m;. p-
7ae90 3e 73 20 3d 20 73 20 2b 20 6d 73 3b 0a 20 20 69 >s = s + ms;. i
7aea0 66 28 20 70 61 72 73 65 54 69 6d 65 7a 6f 6e 65 f( parseTimezone
7aeb0 28 7a 44 61 74 65 2c 20 70 29 20 29 20 72 65 74 (zDate, p) ) ret
7aec0 75 72 6e 20 31 3b 0a 20 20 70 2d 3e 76 61 6c 69 urn 1;. p->vali
7aed0 64 54 5a 20 3d 20 28 70 2d 3e 74 7a 21 3d 30 29 dTZ = (p->tz!=0)
7aee0 3f 31 3a 30 3b 0a 20 20 72 65 74 75 72 6e 20 30 ?1:0;. return 0
7aef0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 ;.}../*.** Conve
7af00 72 74 20 66 72 6f 6d 20 59 59 59 59 2d 4d 4d 2d rt from YYYY-MM-
7af10 44 44 20 48 48 3a 4d 4d 3a 53 53 20 74 6f 20 6a DD HH:MM:SS to j
7af20 75 6c 69 61 6e 20 64 61 79 2e 20 20 57 65 20 61 ulian day. We a
7af30 6c 77 61 79 73 20 61 73 73 75 6d 65 0a 2a 2a 20 lways assume.**
7af40 74 68 61 74 20 74 68 65 20 59 59 59 59 2d 4d 4d that the YYYY-MM
7af50 2d 44 44 20 69 73 20 61 63 63 6f 72 64 69 6e 67 -DD is according
7af60 20 74 6f 20 74 68 65 20 47 72 65 67 6f 72 69 61 to the Gregoria
7af70 6e 20 63 61 6c 65 6e 64 61 72 2e 0a 2a 2a 0a 2a n calendar..**.*
7af80 2a 20 52 65 66 65 72 65 6e 63 65 3a 20 20 4d 65 * Reference: Me
7af90 65 75 73 20 70 61 67 65 20 36 31 0a 2a 2f 0a 73 eus page 61.*/.s
7afa0 74 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 tatic void compu
7afb0 74 65 4a 44 28 44 61 74 65 54 69 6d 65 20 2a 70 teJD(DateTime *p
7afc0 29 7b 0a 20 20 69 6e 74 20 59 2c 20 4d 2c 20 44 ){. int Y, M, D
7afd0 2c 20 41 2c 20 42 2c 20 58 31 2c 20 58 32 3b 0a , A, B, X1, X2;.
7afe0 0a 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 4a . if( p->validJ
7aff0 44 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 D ) return;. if
7b000 28 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 29 7b ( p->validYMD ){
7b010 0a 20 20 20 20 59 20 3d 20 70 2d 3e 59 3b 0a 20 . Y = p->Y;.
7b020 20 20 20 4d 20 3d 20 70 2d 3e 4d 3b 0a 20 20 20 M = p->M;.
7b030 20 44 20 3d 20 70 2d 3e 44 3b 0a 20 20 7d 65 6c D = p->D;. }el
7b040 73 65 7b 0a 20 20 20 20 59 20 3d 20 32 30 30 30 se{. Y = 2000
7b050 3b 20 20 2f 2a 20 49 66 20 6e 6f 20 59 4d 44 20 ; /* If no YMD
7b060 73 70 65 63 69 66 69 65 64 2c 20 61 73 73 75 6d specified, assum
7b070 65 20 32 30 30 30 2d 4a 61 6e 2d 30 31 20 2a 2f e 2000-Jan-01 */
7b080 0a 20 20 20 20 4d 20 3d 20 31 3b 0a 20 20 20 20 . M = 1;.
7b090 44 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 D = 1;. }. if(
7b0a0 20 4d 3c 3d 32 20 29 7b 0a 20 20 20 20 59 2d 2d M<=2 ){. Y--
7b0b0 3b 0a 20 20 20 20 4d 20 2b 3d 20 31 32 3b 0a 20 ;. M += 12;.
7b0c0 20 7d 0a 20 20 41 20 3d 20 59 2f 31 30 30 3b 0a }. A = Y/100;.
7b0d0 20 20 42 20 3d 20 32 20 2d 20 41 20 2b 20 28 41 B = 2 - A + (A
7b0e0 2f 34 29 3b 0a 20 20 58 31 20 3d 20 33 36 35 32 /4);. X1 = 3652
7b0f0 35 2a 28 59 2b 34 37 31 36 29 2f 31 30 30 3b 0a 5*(Y+4716)/100;.
7b100 20 20 58 32 20 3d 20 33 30 36 30 30 31 2a 28 4d X2 = 306001*(M
7b110 2b 31 29 2f 31 30 30 30 30 3b 0a 20 20 70 2d 3e +1)/10000;. p->
7b120 69 4a 44 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 iJD = (sqlite3_i
7b130 6e 74 36 34 29 28 28 58 31 20 2b 20 58 32 20 2b nt64)((X1 + X2 +
7b140 20 44 20 2b 20 42 20 2d 20 31 35 32 34 2e 35 20 D + B - 1524.5
7b150 29 20 2a 20 38 36 34 30 30 30 30 30 29 3b 0a 20 ) * 86400000);.
7b160 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b p->validJD = 1;
7b170 0a 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 48 . if( p->validH
7b180 4d 53 20 29 7b 0a 20 20 20 20 70 2d 3e 69 4a 44 MS ){. p->iJD
7b190 20 2b 3d 20 70 2d 3e 68 2a 33 36 30 30 30 30 30 += p->h*3600000
7b1a0 20 2b 20 70 2d 3e 6d 2a 36 30 30 30 30 20 2b 20 + p->m*60000 +
7b1b0 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 (sqlite3_int64)(
7b1c0 70 2d 3e 73 2a 31 30 30 30 29 3b 0a 20 20 20 20 p->s*1000);.
7b1d0 69 66 28 20 70 2d 3e 76 61 6c 69 64 54 5a 20 29 if( p->validTZ )
7b1e0 7b 0a 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2d {. p->iJD -
7b1f0 3d 20 70 2d 3e 74 7a 2a 36 30 30 30 30 3b 0a 20 = p->tz*60000;.
7b200 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44 p->validYMD
7b210 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 = 0;. p->v
7b220 61 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20 20 alidHMS = 0;.
7b230 20 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 p->validTZ =
7b240 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0;. }. }.}..
7b250 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 64 61 74 65 /*.** Parse date
7b260 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a s of the form.**
7b270 0a 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d .** YYYY-MM-
7b280 44 44 20 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 0a DD HH:MM:SS.FFF.
7b290 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 ** YYYY-MM-D
7b2a0 44 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2a 20 20 20 D HH:MM:SS.**
7b2b0 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a YYYY-MM-DD HH:
7b2c0 4d 4d 0a 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d MM.** YYYY-M
7b2d0 4d 2d 44 44 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 M-DD.**.** Write
7b2e0 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 74 6f the result into
7b2f0 20 74 68 65 20 44 61 74 65 54 69 6d 65 20 73 74 the DateTime st
7b300 72 75 63 74 75 72 65 20 61 6e 64 20 72 65 74 75 ructure and retu
7b310 72 6e 20 30 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 rn 0.** on succe
7b320 73 73 20 61 6e 64 20 31 20 69 66 20 74 68 65 20 ss and 1 if the
7b330 69 6e 70 75 74 20 73 74 72 69 6e 67 20 69 73 20 input string is
7b340 6e 6f 74 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 not a well-forme
7b350 64 0a 2a 2a 20 64 61 74 65 2e 0a 2a 2f 0a 73 74 d.** date..*/.st
7b360 61 74 69 63 20 69 6e 74 20 70 61 72 73 65 59 79 atic int parseYy
7b370 79 79 4d 6d 44 64 28 63 6f 6e 73 74 20 63 68 61 yyMmDd(const cha
7b380 72 20 2a 7a 44 61 74 65 2c 20 44 61 74 65 54 69 r *zDate, DateTi
7b390 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 59 2c me *p){. int Y,
7b3a0 20 4d 2c 20 44 2c 20 6e 65 67 3b 0a 0a 20 20 69 M, D, neg;.. i
7b3b0 66 28 20 7a 44 61 74 65 5b 30 5d 3d 3d 27 2d 27 f( zDate[0]=='-'
7b3c0 20 29 7b 0a 20 20 20 20 7a 44 61 74 65 2b 2b 3b ){. zDate++;
7b3d0 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20 . neg = 1;.
7b3e0 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 65 67 20 3d }else{. neg =
7b3f0 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67 65 0;. }. if( ge
7b400 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c 34 2c tDigits(zDate,4,
7b410 30 2c 39 39 39 39 2c 27 2d 27 2c 26 59 2c 32 2c 0,9999,'-',&Y,2,
7b420 31 2c 31 32 2c 27 2d 27 2c 26 4d 2c 32 2c 31 2c 1,12,'-',&M,2,1,
7b430 33 31 2c 30 2c 26 44 29 21 3d 33 20 29 7b 0a 20 31,0,&D)!=3 ){.
7b440 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d return 1;. }
7b450 0a 20 20 7a 44 61 74 65 20 2b 3d 20 31 30 3b 0a . zDate += 10;.
7b460 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 while( sqlite3
7b470 49 73 73 70 61 63 65 28 2a 7a 44 61 74 65 29 20 Isspace(*zDate)
7b480 7c 7c 20 27 54 27 3d 3d 2a 28 75 38 2a 29 7a 44 || 'T'==*(u8*)zD
7b490 61 74 65 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 ate ){ zDate++;
7b4a0 7d 0a 20 20 69 66 28 20 70 61 72 73 65 48 68 4d }. if( parseHhM
7b4b0 6d 53 73 28 7a 44 61 74 65 2c 20 70 29 3d 3d 30 mSs(zDate, p)==0
7b4c0 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 67 6f ){. /* We go
7b4d0 74 20 74 68 65 20 74 69 6d 65 20 2a 2f 0a 20 20 t the time */.
7b4e0 7d 65 6c 73 65 20 69 66 28 20 2a 7a 44 61 74 65 }else if( *zDate
7b4f0 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e 76 61 ==0 ){. p->va
7b500 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20 7d 65 lidHMS = 0;. }e
7b510 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 lse{. return
7b520 31 3b 0a 20 20 7d 0a 20 20 70 2d 3e 76 61 6c 69 1;. }. p->vali
7b530 64 4a 44 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 dJD = 0;. p->va
7b540 6c 69 64 59 4d 44 20 3d 20 31 3b 0a 20 20 70 2d lidYMD = 1;. p-
7b550 3e 59 20 3d 20 6e 65 67 20 3f 20 2d 59 20 3a 20 >Y = neg ? -Y :
7b560 59 3b 0a 20 20 70 2d 3e 4d 20 3d 20 4d 3b 0a 20 Y;. p->M = M;.
7b570 20 70 2d 3e 44 20 3d 20 44 3b 0a 20 20 69 66 28 p->D = D;. if(
7b580 20 70 2d 3e 76 61 6c 69 64 54 5a 20 29 7b 0a 20 p->validTZ ){.
7b590 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b computeJD(p);
7b5a0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b . }. return 0;
7b5b0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 .}../*.** Set th
7b5c0 65 20 74 69 6d 65 20 74 6f 20 74 68 65 20 63 75 e time to the cu
7b5d0 72 72 65 6e 74 20 74 69 6d 65 20 72 65 70 6f 72 rrent time repor
7b5e0 74 65 64 20 62 79 20 74 68 65 20 56 46 53 0a 2a ted by the VFS.*
7b5f0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 /.static void se
7b600 74 44 61 74 65 54 69 6d 65 54 6f 43 75 72 72 65 tDateTimeToCurre
7b610 6e 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 nt(sqlite3_conte
7b620 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 44 61 74 xt *context, Dat
7b630 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 64 6f 75 eTime *p){. dou
7b640 62 6c 65 20 72 3b 0a 20 20 73 71 6c 69 74 65 33 ble r;. sqlite3
7b650 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 *db = sqlite3_c
7b660 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 ontext_db_handle
7b670 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c (context);. sql
7b680 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d ite3OsCurrentTim
7b690 65 28 64 62 2d 3e 70 56 66 73 2c 20 26 72 29 3b e(db->pVfs, &r);
7b6a0 0a 20 20 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c . p->iJD = (sql
7b6b0 69 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 38 36 ite3_int64)(r*86
7b6c0 34 30 30 30 30 30 2e 30 20 2b 20 30 2e 35 29 3b 400000.0 + 0.5);
7b6d0 0a 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 . p->validJD =
7b6e0 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 1;.}../*.** Atte
7b6f0 6d 70 74 20 74 6f 20 70 61 72 73 65 20 74 68 65 mpt to parse the
7b700 20 67 69 76 65 6e 20 73 74 72 69 6e 67 20 69 6e given string in
7b710 74 6f 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 to a Julian Day
7b720 4e 75 6d 62 65 72 2e 20 20 52 65 74 75 72 6e 0a Number. Return.
7b730 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ** the number of
7b740 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 errors..**.** T
7b750 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 he following are
7b760 20 61 63 63 65 70 74 61 62 6c 65 20 66 6f 72 6d acceptable form
7b770 73 20 66 6f 72 20 74 68 65 20 69 6e 70 75 74 20 s for the input
7b780 73 74 72 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 string:.**.**
7b790 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 YYYY-MM-DD HH
7b7a0 3a 4d 4d 3a 53 53 2e 46 46 46 20 20 2b 2f 2d 48 :MM:SS.FFF +/-H
7b7b0 48 3a 4d 4d 0a 2a 2a 20 20 20 20 20 20 44 44 44 H:MM.** DDD
7b7c0 44 2e 44 44 20 0a 2a 2a 20 20 20 20 20 20 6e 6f D.DD .** no
7b7d0 77 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 66 w.**.** In the f
7b7e0 69 72 73 74 20 66 6f 72 6d 2c 20 74 68 65 20 2b irst form, the +
7b7f0 2f 2d 48 48 3a 4d 4d 20 69 73 20 61 6c 77 61 79 /-HH:MM is alway
7b800 73 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 54 68 65 s optional. The
7b810 20 66 72 61 63 74 69 6f 6e 61 6c 0a 2a 2a 20 73 fractional.** s
7b820 65 63 6f 6e 64 73 20 65 78 74 65 6e 73 69 6f 6e econds extension
7b830 20 28 74 68 65 20 22 2e 46 46 46 22 29 20 69 73 (the ".FFF") is
7b840 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 54 68 65 20 optional. The
7b850 73 65 63 6f 6e 64 73 20 70 6f 72 74 69 6f 6e 0a seconds portion.
7b860 2a 2a 20 28 22 3a 53 53 2e 46 46 46 22 29 20 69 ** (":SS.FFF") i
7b870 73 20 6f 70 74 69 6f 6e 2e 20 20 54 68 65 20 79 s option. The y
7b880 65 61 72 20 61 6e 64 20 64 61 74 65 20 63 61 6e ear and date can
7b890 20 62 65 20 6f 6d 69 74 74 65 64 20 61 73 20 6c be omitted as l
7b8a0 6f 6e 67 0a 2a 2a 20 61 73 20 74 68 65 72 65 20 ong.** as there
7b8b0 69 73 20 61 20 74 69 6d 65 20 73 74 72 69 6e 67 is a time string
7b8c0 2e 20 20 54 68 65 20 74 69 6d 65 20 73 74 72 69 . The time stri
7b8d0 6e 67 20 63 61 6e 20 62 65 20 6f 6d 69 74 74 65 ng can be omitte
7b8e0 64 20 61 73 20 6c 6f 6e 67 0a 2a 2a 20 61 73 20 d as long.** as
7b8f0 74 68 65 72 65 20 69 73 20 61 20 79 65 61 72 20 there is a year
7b900 61 6e 64 20 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 and date..*/.sta
7b910 74 69 63 20 69 6e 74 20 70 61 72 73 65 44 61 74 tic int parseDat
7b920 65 4f 72 54 69 6d 65 28 0a 20 20 73 71 6c 69 74 eOrTime(. sqlit
7b930 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 e3_context *cont
7b940 65 78 74 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 ext, . const ch
7b950 61 72 20 2a 7a 44 61 74 65 2c 20 0a 20 20 44 61 ar *zDate, . Da
7b960 74 65 54 69 6d 65 20 2a 70 0a 29 7b 0a 20 20 69 teTime *p.){. i
7b970 6e 74 20 69 73 52 65 61 6c 4e 75 6d 3b 20 20 20 nt isRealNum;
7b980 20 2f 2a 20 52 65 74 75 72 6e 20 66 72 6f 6d 20 /* Return from
7b990 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28 sqlite3IsNumber(
7b9a0 29 2e 20 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0a ). Not used */.
7b9b0 20 20 69 66 28 20 70 61 72 73 65 59 79 79 79 4d if( parseYyyyM
7b9c0 6d 44 64 28 7a 44 61 74 65 2c 70 29 3d 3d 30 20 mDd(zDate,p)==0
7b9d0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
7b9e0 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 61 72 . }else if( par
7b9f0 73 65 48 68 4d 6d 53 73 28 7a 44 61 74 65 2c 20 seHhMmSs(zDate,
7ba00 70 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 p)==0 ){. ret
7ba10 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 urn 0;. }else i
7ba20 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d f( sqlite3StrICm
7ba30 70 28 7a 44 61 74 65 2c 22 6e 6f 77 22 29 3d 3d p(zDate,"now")==
7ba40 30 29 7b 0a 20 20 20 20 73 65 74 44 61 74 65 54 0){. setDateT
7ba50 69 6d 65 54 6f 43 75 72 72 65 6e 74 28 63 6f 6e imeToCurrent(con
7ba60 74 65 78 74 2c 20 70 29 3b 0a 20 20 20 20 72 65 text, p);. re
7ba70 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 turn 0;. }else
7ba80 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e 75 6d if( sqlite3IsNum
7ba90 62 65 72 28 7a 44 61 74 65 2c 20 26 69 73 52 65 ber(zDate, &isRe
7baa0 61 6c 4e 75 6d 2c 20 53 51 4c 49 54 45 5f 55 54 alNum, SQLITE_UT
7bab0 46 38 29 20 29 7b 0a 20 20 20 20 64 6f 75 62 6c F8) ){. doubl
7bac0 65 20 72 3b 0a 20 20 20 20 67 65 74 56 61 6c 75 e r;. getValu
7bad0 65 28 7a 44 61 74 65 2c 20 26 72 29 3b 0a 20 20 e(zDate, &r);.
7bae0 20 20 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c 69 p->iJD = (sqli
7baf0 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 38 36 34 te3_int64)(r*864
7bb00 30 30 30 30 30 2e 30 20 2b 20 30 2e 35 29 3b 0a 00000.0 + 0.5);.
7bb10 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d p->validJD =
7bb20 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 1;. return 0
7bb30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 ;. }. return 1
7bb40 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 ;.}../*.** Compu
7bb50 74 65 20 74 68 65 20 59 65 61 72 2c 20 4d 6f 6e te the Year, Mon
7bb60 74 68 2c 20 61 6e 64 20 44 61 79 20 66 72 6f 6d th, and Day from
7bb70 20 74 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 the julian day
7bb80 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 number..*/.stati
7bb90 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 65 59 4d c void computeYM
7bba0 44 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a D(DateTime *p){.
7bbb0 20 20 69 6e 74 20 5a 2c 20 41 2c 20 42 2c 20 43 int Z, A, B, C
7bbc0 2c 20 44 2c 20 45 2c 20 58 31 3b 0a 20 20 69 66 , D, E, X1;. if
7bbd0 28 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 29 20 ( p->validYMD )
7bbe0 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 21 70 return;. if( !p
7bbf0 2d 3e 76 61 6c 69 64 4a 44 20 29 7b 0a 20 20 20 ->validJD ){.
7bc00 20 70 2d 3e 59 20 3d 20 32 30 30 30 3b 0a 20 20 p->Y = 2000;.
7bc10 20 20 70 2d 3e 4d 20 3d 20 31 3b 0a 20 20 20 20 p->M = 1;.
7bc20 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 p->D = 1;. }els
7bc30 65 7b 0a 20 20 20 20 5a 20 3d 20 28 69 6e 74 29 e{. Z = (int)
7bc40 28 28 70 2d 3e 69 4a 44 20 2b 20 34 33 32 30 30 ((p->iJD + 43200
7bc50 30 30 30 29 2f 38 36 34 30 30 30 30 30 29 3b 0a 000)/86400000);.
7bc60 20 20 20 20 41 20 3d 20 28 69 6e 74 29 28 28 5a A = (int)((Z
7bc70 20 2d 20 31 38 36 37 32 31 36 2e 32 35 29 2f 33 - 1867216.25)/3
7bc80 36 35 32 34 2e 32 35 29 3b 0a 20 20 20 20 41 20 6524.25);. A
7bc90 3d 20 5a 20 2b 20 31 20 2b 20 41 20 2d 20 28 41 = Z + 1 + A - (A
7bca0 2f 34 29 3b 0a 20 20 20 20 42 20 3d 20 41 20 2b /4);. B = A +
7bcb0 20 31 35 32 34 3b 0a 20 20 20 20 43 20 3d 20 28 1524;. C = (
7bcc0 69 6e 74 29 28 28 42 20 2d 20 31 32 32 2e 31 29 int)((B - 122.1)
7bcd0 2f 33 36 35 2e 32 35 29 3b 0a 20 20 20 20 44 20 /365.25);. D
7bce0 3d 20 28 33 36 35 32 35 2a 43 29 2f 31 30 30 3b = (36525*C)/100;
7bcf0 0a 20 20 20 20 45 20 3d 20 28 69 6e 74 29 28 28 . E = (int)((
7bd00 42 2d 44 29 2f 33 30 2e 36 30 30 31 29 3b 0a 20 B-D)/30.6001);.
7bd10 20 20 20 58 31 20 3d 20 28 69 6e 74 29 28 33 30 X1 = (int)(30
7bd20 2e 36 30 30 31 2a 45 29 3b 0a 20 20 20 20 70 2d .6001*E);. p-
7bd30 3e 44 20 3d 20 42 20 2d 20 44 20 2d 20 58 31 3b >D = B - D - X1;
7bd40 0a 20 20 20 20 70 2d 3e 4d 20 3d 20 45 3c 31 34 . p->M = E<14
7bd50 20 3f 20 45 2d 31 20 3a 20 45 2d 31 33 3b 0a 20 ? E-1 : E-13;.
7bd60 20 20 20 70 2d 3e 59 20 3d 20 70 2d 3e 4d 3e 32 p->Y = p->M>2
7bd70 20 3f 20 43 20 2d 20 34 37 31 36 20 3a 20 43 20 ? C - 4716 : C
7bd80 2d 20 34 37 31 35 3b 0a 20 20 7d 0a 20 20 70 2d - 4715;. }. p-
7bd90 3e 76 61 6c 69 64 59 4d 44 20 3d 20 31 3b 0a 7d >validYMD = 1;.}
7bda0 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 ../*.** Compute
7bdb0 74 68 65 20 48 6f 75 72 2c 20 4d 69 6e 75 74 65 the Hour, Minute
7bdc0 2c 20 61 6e 64 20 53 65 63 6f 6e 64 73 20 66 72 , and Seconds fr
7bdd0 6f 6d 20 74 68 65 20 6a 75 6c 69 61 6e 20 64 61 om the julian da
7bde0 79 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 y number..*/.sta
7bdf0 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 65 tic void compute
7be00 48 4d 53 28 44 61 74 65 54 69 6d 65 20 2a 70 29 HMS(DateTime *p)
7be10 7b 0a 20 20 69 6e 74 20 73 3b 0a 20 20 69 66 28 {. int s;. if(
7be20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 29 20 72 p->validHMS ) r
7be30 65 74 75 72 6e 3b 0a 20 20 63 6f 6d 70 75 74 65 eturn;. compute
7be40 4a 44 28 70 29 3b 0a 20 20 73 20 3d 20 28 69 6e JD(p);. s = (in
7be50 74 29 28 28 70 2d 3e 69 4a 44 20 2b 20 34 33 32 t)((p->iJD + 432
7be60 30 30 30 30 30 29 20 25 20 38 36 34 30 30 30 30 00000) % 8640000
7be70 30 29 3b 0a 20 20 70 2d 3e 73 20 3d 20 73 2f 31 0);. p->s = s/1
7be80 30 30 30 2e 30 3b 0a 20 20 73 20 3d 20 28 69 6e 000.0;. s = (in
7be90 74 29 70 2d 3e 73 3b 0a 20 20 70 2d 3e 73 20 2d t)p->s;. p->s -
7bea0 3d 20 73 3b 0a 20 20 70 2d 3e 68 20 3d 20 73 2f = s;. p->h = s/
7beb0 33 36 30 30 3b 0a 20 20 73 20 2d 3d 20 70 2d 3e 3600;. s -= p->
7bec0 68 2a 33 36 30 30 3b 0a 20 20 70 2d 3e 6d 20 3d h*3600;. p->m =
7bed0 20 73 2f 36 30 3b 0a 20 20 70 2d 3e 73 20 2b 3d s/60;. p->s +=
7bee0 20 73 20 2d 20 70 2d 3e 6d 2a 36 30 3b 0a 20 20 s - p->m*60;.
7bef0 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b p->validHMS = 1;
7bf00 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 .}../*.** Comput
7bf10 65 20 62 6f 74 68 20 59 4d 44 20 61 6e 64 20 48 e both YMD and H
7bf20 4d 53 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 MS.*/.static voi
7bf30 64 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 d computeYMD_HMS
7bf40 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 (DateTime *p){.
7bf50 20 63 6f 6d 70 75 74 65 59 4d 44 28 70 29 3b 0a computeYMD(p);.
7bf60 20 20 63 6f 6d 70 75 74 65 48 4d 53 28 70 29 3b computeHMS(p);
7bf70 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 .}../*.** Clear
7bf80 74 68 65 20 59 4d 44 20 61 6e 64 20 48 4d 53 20 the YMD and HMS
7bf90 61 6e 64 20 74 68 65 20 54 5a 0a 2a 2f 0a 73 74 and the TZ.*/.st
7bfa0 61 74 69 63 20 76 6f 69 64 20 63 6c 65 61 72 59 atic void clearY
7bfb0 4d 44 5f 48 4d 53 5f 54 5a 28 44 61 74 65 54 69 MD_HMS_TZ(DateTi
7bfc0 6d 65 20 2a 70 29 7b 0a 20 20 70 2d 3e 76 61 6c me *p){. p->val
7bfd0 69 64 59 4d 44 20 3d 20 30 3b 0a 20 20 70 2d 3e idYMD = 0;. p->
7bfe0 76 61 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20 validHMS = 0;.
7bff0 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a p->validTZ = 0;.
7c000 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }..#ifndef SQLIT
7c010 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d 45 E_OMIT_LOCALTIME
7c020 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 ./*.** Compute t
7c030 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 28 69 he difference (i
7c040 6e 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 29 0a n milliseconds).
7c050 2a 2a 20 62 65 74 77 65 65 6e 20 6c 6f 63 61 6c ** between local
7c060 74 69 6d 65 20 61 6e 64 20 55 54 43 20 28 61 2e time and UTC (a.
7c070 6b 2e 61 2e 20 47 4d 54 29 0a 2a 2a 20 66 6f 72 k.a. GMT).** for
7c080 20 74 68 65 20 74 69 6d 65 20 76 61 6c 75 65 20 the time value
7c090 70 20 77 68 65 72 65 20 70 20 69 73 20 69 6e 20 p where p is in
7c0a0 55 54 43 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 UTC..*/.static s
7c0b0 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6c 6f 63 qlite3_int64 loc
7c0c0 61 6c 74 69 6d 65 4f 66 66 73 65 74 28 44 61 74 altimeOffset(Dat
7c0d0 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 44 61 74 eTime *p){. Dat
7c0e0 65 54 69 6d 65 20 78 2c 20 79 3b 0a 20 20 74 69 eTime x, y;. ti
7c0f0 6d 65 5f 74 20 74 3b 0a 20 20 78 20 3d 20 2a 70 me_t t;. x = *p
7c100 3b 0a 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 ;. computeYMD_H
7c110 4d 53 28 26 78 29 3b 0a 20 20 69 66 28 20 78 2e MS(&x);. if( x.
7c120 59 3c 31 39 37 31 20 7c 7c 20 78 2e 59 3e 3d 32 Y<1971 || x.Y>=2
7c130 30 33 38 20 29 7b 0a 20 20 20 20 78 2e 59 20 3d 038 ){. x.Y =
7c140 20 32 30 30 30 3b 0a 20 20 20 20 78 2e 4d 20 3d 2000;. x.M =
7c150 20 31 3b 0a 20 20 20 20 78 2e 44 20 3d 20 31 3b 1;. x.D = 1;
7c160 0a 20 20 20 20 78 2e 68 20 3d 20 30 3b 0a 20 20 . x.h = 0;.
7c170 20 20 78 2e 6d 20 3d 20 30 3b 0a 20 20 20 20 78 x.m = 0;. x
7c180 2e 73 20 3d 20 30 2e 30 3b 0a 20 20 7d 20 65 6c .s = 0.0;. } el
7c190 73 65 20 7b 0a 20 20 20 20 69 6e 74 20 73 20 3d se {. int s =
7c1a0 20 28 69 6e 74 29 28 78 2e 73 20 2b 20 30 2e 35 (int)(x.s + 0.5
7c1b0 29 3b 0a 20 20 20 20 78 2e 73 20 3d 20 73 3b 0a );. x.s = s;.
7c1c0 20 20 7d 0a 20 20 78 2e 74 7a 20 3d 20 30 3b 0a }. x.tz = 0;.
7c1d0 20 20 78 2e 76 61 6c 69 64 4a 44 20 3d 20 30 3b x.validJD = 0;
7c1e0 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 78 29 . computeJD(&x)
7c1f0 3b 0a 20 20 74 20 3d 20 28 74 69 6d 65 5f 74 29 ;. t = (time_t)
7c200 28 78 2e 69 4a 44 2f 31 30 30 30 20 2d 20 32 31 (x.iJD/1000 - 21
7c210 30 38 36 36 37 36 2a 28 69 36 34 29 31 30 30 30 086676*(i64)1000
7c220 30 29 3b 0a 23 69 66 64 65 66 20 48 41 56 45 5f 0);.#ifdef HAVE_
7c230 4c 4f 43 41 4c 54 49 4d 45 5f 52 0a 20 20 7b 0a LOCALTIME_R. {.
7c240 20 20 20 20 73 74 72 75 63 74 20 74 6d 20 73 4c struct tm sL
7c250 6f 63 61 6c 3b 0a 20 20 20 20 6c 6f 63 61 6c 74 ocal;. localt
7c260 69 6d 65 5f 72 28 26 74 2c 20 26 73 4c 6f 63 61 ime_r(&t, &sLoca
7c270 6c 29 3b 0a 20 20 20 20 79 2e 59 20 3d 20 73 4c l);. y.Y = sL
7c280 6f 63 61 6c 2e 74 6d 5f 79 65 61 72 20 2b 20 31 ocal.tm_year + 1
7c290 39 30 30 3b 0a 20 20 20 20 79 2e 4d 20 3d 20 73 900;. y.M = s
7c2a0 4c 6f 63 61 6c 2e 74 6d 5f 6d 6f 6e 20 2b 20 31 Local.tm_mon + 1
7c2b0 3b 0a 20 20 20 20 79 2e 44 20 3d 20 73 4c 6f 63 ;. y.D = sLoc
7c2c0 61 6c 2e 74 6d 5f 6d 64 61 79 3b 0a 20 20 20 20 al.tm_mday;.
7c2d0 79 2e 68 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f y.h = sLocal.tm_
7c2e0 68 6f 75 72 3b 0a 20 20 20 20 79 2e 6d 20 3d 20 hour;. y.m =
7c2f0 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 69 6e 3b 0a 20 sLocal.tm_min;.
7c300 20 20 20 79 2e 73 20 3d 20 73 4c 6f 63 61 6c 2e y.s = sLocal.
7c310 74 6d 5f 73 65 63 3b 0a 20 20 7d 0a 23 65 6c 69 tm_sec;. }.#eli
7c320 66 20 64 65 66 69 6e 65 64 28 48 41 56 45 5f 4c f defined(HAVE_L
7c330 4f 43 41 4c 54 49 4d 45 5f 53 29 20 26 26 20 48 OCALTIME_S) && H
7c340 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f 53 0a AVE_LOCALTIME_S.
7c350 20 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 74 {. struct t
7c360 6d 20 73 4c 6f 63 61 6c 3b 0a 20 20 20 20 6c 6f m sLocal;. lo
7c370 63 61 6c 74 69 6d 65 5f 73 28 26 73 4c 6f 63 61 caltime_s(&sLoca
7c380 6c 2c 20 26 74 29 3b 0a 20 20 20 20 79 2e 59 20 l, &t);. y.Y
7c390 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 79 65 61 72 = sLocal.tm_year
7c3a0 20 2b 20 31 39 30 30 3b 0a 20 20 20 20 79 2e 4d + 1900;. y.M
7c3b0 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 6f 6e = sLocal.tm_mon
7c3c0 20 2b 20 31 3b 0a 20 20 20 20 79 2e 44 20 3d 20 + 1;. y.D =
7c3d0 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 64 61 79 3b 0a sLocal.tm_mday;.
7c3e0 20 20 20 20 79 2e 68 20 3d 20 73 4c 6f 63 61 6c y.h = sLocal
7c3f0 2e 74 6d 5f 68 6f 75 72 3b 0a 20 20 20 20 79 2e .tm_hour;. y.
7c400 6d 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 69 m = sLocal.tm_mi
7c410 6e 3b 0a 20 20 20 20 79 2e 73 20 3d 20 73 4c 6f n;. y.s = sLo
7c420 63 61 6c 2e 74 6d 5f 73 65 63 3b 0a 20 20 7d 0a cal.tm_sec;. }.
7c430 23 65 6c 73 65 0a 20 20 7b 0a 20 20 20 20 73 74 #else. {. st
7c440 72 75 63 74 20 74 6d 20 2a 70 54 6d 3b 0a 20 20 ruct tm *pTm;.
7c450 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
7c460 65 6e 74 65 72 28 73 71 6c 69 74 65 33 4d 75 74 enter(sqlite3Mut
7c470 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d exAlloc(SQLITE_M
7c480 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 UTEX_STATIC_MAST
7c490 45 52 29 29 3b 0a 20 20 20 20 70 54 6d 20 3d 20 ER));. pTm =
7c4a0 6c 6f 63 61 6c 74 69 6d 65 28 26 74 29 3b 0a 20 localtime(&t);.
7c4b0 20 20 20 79 2e 59 20 3d 20 70 54 6d 2d 3e 74 6d y.Y = pTm->tm
7c4c0 5f 79 65 61 72 20 2b 20 31 39 30 30 3b 0a 20 20 _year + 1900;.
7c4d0 20 20 79 2e 4d 20 3d 20 70 54 6d 2d 3e 74 6d 5f y.M = pTm->tm_
7c4e0 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20 20 79 2e 44 mon + 1;. y.D
7c4f0 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d 64 61 79 3b = pTm->tm_mday;
7c500 0a 20 20 20 20 79 2e 68 20 3d 20 70 54 6d 2d 3e . y.h = pTm->
7c510 74 6d 5f 68 6f 75 72 3b 0a 20 20 20 20 79 2e 6d tm_hour;. y.m
7c520 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d 69 6e 3b 0a = pTm->tm_min;.
7c530 20 20 20 20 79 2e 73 20 3d 20 70 54 6d 2d 3e 74 y.s = pTm->t
7c540 6d 5f 73 65 63 3b 0a 20 20 20 20 73 71 6c 69 74 m_sec;. sqlit
7c550 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 e3_mutex_leave(s
7c560 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 qlite3MutexAlloc
7c570 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 (SQLITE_MUTEX_ST
7c580 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 20 ATIC_MASTER));.
7c590 20 7d 0a 23 65 6e 64 69 66 0a 20 20 79 2e 76 61 }.#endif. y.va
7c5a0 6c 69 64 59 4d 44 20 3d 20 31 3b 0a 20 20 79 2e lidYMD = 1;. y.
7c5b0 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 20 20 validHMS = 1;.
7c5c0 79 2e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 y.validJD = 0;.
7c5d0 20 79 2e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a y.validTZ = 0;.
7c5e0 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 79 29 3b computeJD(&y);
7c5f0 0a 20 20 72 65 74 75 72 6e 20 79 2e 69 4a 44 20 . return y.iJD
7c600 2d 20 78 2e 69 4a 44 3b 0a 7d 0a 23 65 6e 64 69 - x.iJD;.}.#endi
7c610 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
7c620 5f 4c 4f 43 41 4c 54 49 4d 45 20 2a 2f 0a 0a 2f _LOCALTIME */../
7c630 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 20 61 20 6d *.** Process a m
7c640 6f 64 69 66 69 65 72 20 74 6f 20 61 20 64 61 74 odifier to a dat
7c650 65 2d 74 69 6d 65 20 73 74 61 6d 70 2e 20 20 54 e-time stamp. T
7c660 68 65 20 6d 6f 64 69 66 69 65 72 73 20 61 72 65 he modifiers are
7c670 0a 2a 2a 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a .** as follows:.
7c680 2a 2a 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 64 61 **.** NNN da
7c690 79 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 68 6f ys.** NNN ho
7c6a0 75 72 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 6d urs.** NNN m
7c6b0 69 6e 75 74 65 73 0a 2a 2a 20 20 20 20 20 4e 4e inutes.** NN
7c6c0 4e 2e 4e 4e 4e 4e 20 73 65 63 6f 6e 64 73 0a 2a N.NNNN seconds.*
7c6d0 2a 20 20 20 20 20 4e 4e 4e 20 6d 6f 6e 74 68 73 * NNN months
7c6e0 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 79 65 61 72 .** NNN year
7c6f0 73 0a 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f s.** start o
7c700 66 20 6d 6f 6e 74 68 0a 2a 2a 20 20 20 20 20 73 f month.** s
7c710 74 61 72 74 20 6f 66 20 79 65 61 72 0a 2a 2a 20 tart of year.**
7c720 20 20 20 20 73 74 61 72 74 20 6f 66 20 77 65 65 start of wee
7c730 6b 0a 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f k.** start o
7c740 66 20 64 61 79 0a 2a 2a 20 20 20 20 20 77 65 65 f day.** wee
7c750 6b 64 61 79 20 4e 0a 2a 2a 20 20 20 20 20 75 6e kday N.** un
7c760 69 78 65 70 6f 63 68 0a 2a 2a 20 20 20 20 20 6c ixepoch.** l
7c770 6f 63 61 6c 74 69 6d 65 0a 2a 2a 20 20 20 20 20 ocaltime.**
7c780 75 74 63 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e utc.**.** Return
7c790 20 30 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 0 on success an
7c7a0 64 20 31 20 69 66 20 74 68 65 72 65 20 69 73 20 d 1 if there is
7c7b0 61 6e 79 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f any kind of erro
7c7c0 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 r..*/.static int
7c7d0 20 70 61 72 73 65 4d 6f 64 69 66 69 65 72 28 63 parseModifier(c
7c7e0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 6f 64 2c onst char *zMod,
7c7f0 20 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 DateTime *p){.
7c800 20 69 6e 74 20 72 63 20 3d 20 31 3b 0a 20 20 69 int rc = 1;. i
7c810 6e 74 20 6e 3b 0a 20 20 64 6f 75 62 6c 65 20 72 nt n;. double r
7c820 3b 0a 20 20 63 68 61 72 20 2a 7a 2c 20 7a 42 75 ;. char *z, zBu
7c830 66 5b 33 30 5d 3b 0a 20 20 7a 20 3d 20 7a 42 75 f[30];. z = zBu
7c840 66 3b 0a 20 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c f;. for(n=0; n<
7c850 41 72 72 61 79 53 69 7a 65 28 7a 42 75 66 29 2d ArraySize(zBuf)-
7c860 31 20 26 26 20 7a 4d 6f 64 5b 6e 5d 3b 20 6e 2b 1 && zMod[n]; n+
7c870 2b 29 7b 0a 20 20 20 20 7a 5b 6e 5d 20 3d 20 28 +){. z[n] = (
7c880 63 68 61 72 29 73 71 6c 69 74 65 33 55 70 70 65 char)sqlite3Uppe
7c890 72 54 6f 4c 6f 77 65 72 5b 28 75 38 29 7a 4d 6f rToLower[(u8)zMo
7c8a0 64 5b 6e 5d 5d 3b 0a 20 20 7d 0a 20 20 7a 5b 6e d[n]];. }. z[n
7c8b0 5d 20 3d 20 30 3b 0a 20 20 73 77 69 74 63 68 28 ] = 0;. switch(
7c8c0 20 7a 5b 30 5d 20 29 7b 0a 23 69 66 6e 64 65 66 z[0] ){.#ifndef
7c8d0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 43 SQLITE_OMIT_LOC
7c8e0 41 4c 54 49 4d 45 0a 20 20 20 20 63 61 73 65 20 ALTIME. case
7c8f0 27 6c 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 'l': {. /*
7c900 20 20 20 6c 6f 63 61 6c 74 69 6d 65 0a 20 20 20 localtime.
7c910 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 41 **. ** A
7c920 73 73 75 6d 69 6e 67 20 74 68 65 20 63 75 72 72 ssuming the curr
7c930 65 6e 74 20 74 69 6d 65 20 76 61 6c 75 65 20 69 ent time value i
7c940 73 20 55 54 43 20 28 61 2e 6b 2e 61 2e 20 47 4d s UTC (a.k.a. GM
7c950 54 29 2c 20 73 68 69 66 74 20 69 74 20 74 6f 0a T), shift it to.
7c960 20 20 20 20 20 20 2a 2a 20 73 68 6f 77 20 6c 6f ** show lo
7c970 63 61 6c 20 74 69 6d 65 2e 0a 20 20 20 20 20 20 cal time..
7c980 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 74 72 */. if( str
7c990 63 6d 70 28 7a 2c 20 22 6c 6f 63 61 6c 74 69 6d cmp(z, "localtim
7c9a0 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 e")==0 ){.
7c9b0 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a computeJD(p);.
7c9c0 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b p->iJD +
7c9d0 3d 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65 = localtimeOffse
7c9e0 74 28 70 29 3b 0a 20 20 20 20 20 20 20 20 63 6c t(p);. cl
7c9f0 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 earYMD_HMS_TZ(p)
7ca00 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 ;. rc = 0
7ca10 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
7ca20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e break;. }.#en
7ca30 64 69 66 0a 20 20 20 20 63 61 73 65 20 27 75 27 dif. case 'u'
7ca40 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 0a 20 20 20 : {. /*.
7ca50 20 20 20 2a 2a 20 20 20 20 75 6e 69 78 65 70 6f ** unixepo
7ca60 63 68 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 ch. **.
7ca70 20 20 2a 2a 20 54 72 65 61 74 20 74 68 65 20 63 ** Treat the c
7ca80 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 urrent value of
7ca90 70 2d 3e 69 4a 44 20 61 73 20 74 68 65 20 6e 75 p->iJD as the nu
7caa0 6d 62 65 72 20 6f 66 0a 20 20 20 20 20 20 2a 2a mber of. **
7cab0 20 73 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 31 seconds since 1
7cac0 39 37 30 2e 20 20 43 6f 6e 76 65 72 74 20 74 6f 970. Convert to
7cad0 20 61 20 72 65 61 6c 20 6a 75 6c 69 61 6e 20 64 a real julian d
7cae0 61 79 20 6e 75 6d 62 65 72 2e 0a 20 20 20 20 20 ay number..
7caf0 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 74 */. if( st
7cb00 72 63 6d 70 28 7a 2c 20 22 75 6e 69 78 65 70 6f rcmp(z, "unixepo
7cb10 63 68 22 29 3d 3d 30 20 26 26 20 70 2d 3e 76 61 ch")==0 && p->va
7cb20 6c 69 64 4a 44 20 29 7b 0a 20 20 20 20 20 20 20 lidJD ){.
7cb30 20 70 2d 3e 69 4a 44 20 3d 20 28 70 2d 3e 69 4a p->iJD = (p->iJ
7cb40 44 20 2b 20 34 33 32 30 30 29 2f 38 36 34 30 30 D + 43200)/86400
7cb50 20 2b 20 32 31 30 38 36 36 37 36 2a 28 69 36 34 + 21086676*(i64
7cb60 29 31 30 30 30 30 30 30 30 3b 0a 20 20 20 20 20 )10000000;.
7cb70 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f clearYMD_HMS_
7cb80 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 TZ(p);. r
7cb90 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 23 c = 0;. }.#
7cba0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
7cbb0 49 54 5f 4c 4f 43 41 4c 54 49 4d 45 0a 20 20 20 IT_LOCALTIME.
7cbc0 20 20 20 65 6c 73 65 20 69 66 28 20 73 74 72 63 else if( strc
7cbd0 6d 70 28 7a 2c 20 22 75 74 63 22 29 3d 3d 30 20 mp(z, "utc")==0
7cbe0 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
7cbf0 65 33 5f 69 6e 74 36 34 20 63 31 3b 0a 20 20 20 e3_int64 c1;.
7cc00 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 computeJD(p
7cc10 29 3b 0a 20 20 20 20 20 20 20 20 63 31 20 3d 20 );. c1 =
7cc20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65 74 28 localtimeOffset(
7cc30 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 p);. p->i
7cc40 4a 44 20 2d 3d 20 63 31 3b 0a 20 20 20 20 20 20 JD -= c1;.
7cc50 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 clearYMD_HMS_T
7cc60 5a 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d Z(p);. p-
7cc70 3e 69 4a 44 20 2b 3d 20 63 31 20 2d 20 6c 6f 63 >iJD += c1 - loc
7cc80 61 6c 74 69 6d 65 4f 66 66 73 65 74 28 70 29 3b altimeOffset(p);
7cc90 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b . rc = 0;
7cca0 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a . }.#endif.
7ccb0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
7ccc0 20 7d 0a 20 20 20 20 63 61 73 65 20 27 77 27 3a }. case 'w':
7ccd0 20 7b 0a 20 20 20 20 20 20 2f 2a 0a 20 20 20 20 {. /*.
7cce0 20 20 2a 2a 20 20 20 20 77 65 65 6b 64 61 79 20 ** weekday
7ccf0 4e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 N. **.
7cd00 20 2a 2a 20 4d 6f 76 65 20 74 68 65 20 64 61 74 ** Move the dat
7cd10 65 20 74 6f 20 74 68 65 20 73 61 6d 65 20 74 69 e to the same ti
7cd20 6d 65 20 6f 6e 20 74 68 65 20 6e 65 78 74 20 6f me on the next o
7cd30 63 63 75 72 72 65 6e 63 65 20 6f 66 0a 20 20 20 ccurrence of.
7cd40 20 20 20 2a 2a 20 77 65 65 6b 64 61 79 20 4e 20 ** weekday N
7cd50 77 68 65 72 65 20 30 3d 3d 53 75 6e 64 61 79 2c where 0==Sunday,
7cd60 20 31 3d 3d 4d 6f 6e 64 61 79 2c 20 61 6e 64 20 1==Monday, and
7cd70 73 6f 20 66 6f 72 74 68 2e 20 20 49 66 20 74 68 so forth. If th
7cd80 65 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 65 20 e. ** date
7cd90 69 73 20 61 6c 72 65 61 64 79 20 6f 6e 20 74 68 is already on th
7cda0 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 77 65 e appropriate we
7cdb0 65 6b 64 61 79 2c 20 74 68 69 73 20 69 73 20 61 ekday, this is a
7cdc0 20 6e 6f 2d 6f 70 2e 0a 20 20 20 20 20 20 2a 2f no-op.. */
7cdd0 0a 20 20 20 20 20 20 69 66 28 20 73 74 72 6e 63 . if( strnc
7cde0 6d 70 28 7a 2c 20 22 77 65 65 6b 64 61 79 20 22 mp(z, "weekday "
7cdf0 2c 20 38 29 3d 3d 30 20 26 26 20 67 65 74 56 61 , 8)==0 && getVa
7ce00 6c 75 65 28 26 7a 5b 38 5d 2c 26 72 29 3e 30 0a lue(&z[8],&r)>0.
7ce10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7ce20 20 26 26 20 28 6e 3d 28 69 6e 74 29 72 29 3d 3d && (n=(int)r)==
7ce30 72 20 26 26 20 6e 3e 3d 30 20 26 26 20 72 3c 37 r && n>=0 && r<7
7ce40 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
7ce50 74 65 33 5f 69 6e 74 36 34 20 5a 3b 0a 20 20 20 te3_int64 Z;.
7ce60 20 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f computeYMD_
7ce70 48 4d 53 28 70 29 3b 0a 20 20 20 20 20 20 20 20 HMS(p);.
7ce80 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a p->validTZ = 0;.
7ce90 20 20 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 p->valid
7cea0 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 JD = 0;.
7ceb0 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 computeJD(p);.
7cec0 20 20 20 20 20 20 5a 20 3d 20 28 28 70 2d 3e 69 Z = ((p->i
7ced0 4a 44 20 2b 20 31 32 39 36 30 30 30 30 30 29 2f JD + 129600000)/
7cee0 38 36 34 30 30 30 30 30 29 20 25 20 37 3b 0a 20 86400000) % 7;.
7cef0 20 20 20 20 20 20 20 69 66 28 20 5a 3e 6e 20 29 if( Z>n )
7cf00 20 5a 20 2d 3d 20 37 3b 0a 20 20 20 20 20 20 20 Z -= 7;.
7cf10 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 6e 20 2d 20 p->iJD += (n -
7cf20 5a 29 2a 38 36 34 30 30 30 30 30 3b 0a 20 20 20 Z)*86400000;.
7cf30 20 20 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d clearYMD_HM
7cf40 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 20 S_TZ(p);.
7cf50 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d rc = 0;. }
7cf60 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
7cf70 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 73 27 }. case 's'
7cf80 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 0a 20 20 20 : {. /*.
7cf90 20 20 20 2a 2a 20 20 20 20 73 74 61 72 74 20 6f ** start o
7cfa0 66 20 54 54 54 54 54 0a 20 20 20 20 20 20 2a 2a f TTTTT. **
7cfb0 0a 20 20 20 20 20 20 2a 2a 20 4d 6f 76 65 20 74 . ** Move t
7cfc0 68 65 20 64 61 74 65 20 62 61 63 6b 77 61 72 64 he date backward
7cfd0 73 20 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 s to the beginni
7cfe0 6e 67 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e ng of the curren
7cff0 74 20 64 61 79 2c 0a 20 20 20 20 20 20 2a 2a 20 t day,. **
7d000 6f 72 20 6d 6f 6e 74 68 20 6f 72 20 79 65 61 72 or month or year
7d010 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
7d020 20 69 66 28 20 73 74 72 6e 63 6d 70 28 7a 2c 20 if( strncmp(z,
7d030 22 73 74 61 72 74 20 6f 66 20 22 2c 20 39 29 21 "start of ", 9)!
7d040 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 =0 ) break;.
7d050 20 20 7a 20 2b 3d 20 39 3b 0a 20 20 20 20 20 20 z += 9;.
7d060 63 6f 6d 70 75 74 65 59 4d 44 28 70 29 3b 0a 20 computeYMD(p);.
7d070 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 p->validHMS
7d080 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 2d 3e 68 = 1;. p->h
7d090 20 3d 20 70 2d 3e 6d 20 3d 20 30 3b 0a 20 20 20 = p->m = 0;.
7d0a0 20 20 20 70 2d 3e 73 20 3d 20 30 2e 30 3b 0a 20 p->s = 0.0;.
7d0b0 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 p->validTZ
7d0c0 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 = 0;. p->va
7d0d0 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 lidJD = 0;.
7d0e0 20 69 66 28 20 73 74 72 63 6d 70 28 7a 2c 22 6d if( strcmp(z,"m
7d0f0 6f 6e 74 68 22 29 3d 3d 30 20 29 7b 0a 20 20 20 onth")==0 ){.
7d100 20 20 20 20 20 70 2d 3e 44 20 3d 20 31 3b 0a 20 p->D = 1;.
7d110 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 rc = 0;.
7d120 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 }else if( s
7d130 74 72 63 6d 70 28 7a 2c 22 79 65 61 72 22 29 3d trcmp(z,"year")=
7d140 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f =0 ){. co
7d150 6d 70 75 74 65 59 4d 44 28 70 29 3b 0a 20 20 20 mputeYMD(p);.
7d160 20 20 20 20 20 70 2d 3e 4d 20 3d 20 31 3b 0a 20 p->M = 1;.
7d170 20 20 20 20 20 20 20 70 2d 3e 44 20 3d 20 31 3b p->D = 1;
7d180 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b . rc = 0;
7d190 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 . }else if(
7d1a0 20 73 74 72 63 6d 70 28 7a 2c 22 64 61 79 22 29 strcmp(z,"day")
7d1b0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 ==0 ){. r
7d1c0 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 c = 0;. }.
7d1d0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
7d1e0 7d 0a 20 20 20 20 63 61 73 65 20 27 2b 27 3a 0a }. case '+':.
7d1f0 20 20 20 20 63 61 73 65 20 27 2d 27 3a 0a 20 20 case '-':.
7d200 20 20 63 61 73 65 20 27 30 27 3a 0a 20 20 20 20 case '0':.
7d210 63 61 73 65 20 27 31 27 3a 0a 20 20 20 20 63 61 case '1':. ca
7d220 73 65 20 27 32 27 3a 0a 20 20 20 20 63 61 73 65 se '2':. case
7d230 20 27 33 27 3a 0a 20 20 20 20 63 61 73 65 20 27 '3':. case '
7d240 34 27 3a 0a 20 20 20 20 63 61 73 65 20 27 35 27 4':. case '5'
7d250 3a 0a 20 20 20 20 63 61 73 65 20 27 36 27 3a 0a :. case '6':.
7d260 20 20 20 20 63 61 73 65 20 27 37 27 3a 0a 20 20 case '7':.
7d270 20 20 63 61 73 65 20 27 38 27 3a 0a 20 20 20 20 case '8':.
7d280 63 61 73 65 20 27 39 27 3a 20 7b 0a 20 20 20 20 case '9': {.
7d290 20 20 64 6f 75 62 6c 65 20 72 52 6f 75 6e 64 65 double rRounde
7d2a0 72 3b 0a 20 20 20 20 20 20 6e 20 3d 20 67 65 74 r;. n = get
7d2b0 56 61 6c 75 65 28 7a 2c 20 26 72 29 3b 0a 20 20 Value(z, &r);.
7d2c0 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e 3d 31 assert( n>=1
7d2d0 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b );. if( z[
7d2e0 6e 5d 3d 3d 27 3a 27 20 29 7b 0a 20 20 20 20 20 n]==':' ){.
7d2f0 20 20 20 2f 2a 20 41 20 6d 6f 64 69 66 69 65 72 /* A modifier
7d300 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 28 2b 7c of the form (+|
7d310 2d 29 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 20 61 -)HH:MM:SS.FFF a
7d320 64 64 73 20 28 6f 72 20 73 75 62 74 72 61 63 74 dds (or subtract
7d330 73 29 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a s) the. *
7d340 2a 20 73 70 65 63 69 66 69 65 64 20 6e 75 6d 62 * specified numb
7d350 65 72 20 6f 66 20 68 6f 75 72 73 2c 20 6d 69 6e er of hours, min
7d360 75 74 65 73 2c 20 73 65 63 6f 6e 64 73 2c 20 61 utes, seconds, a
7d370 6e 64 20 66 72 61 63 74 69 6f 6e 61 6c 20 73 65 nd fractional se
7d380 63 6f 6e 64 73 0a 20 20 20 20 20 20 20 20 2a 2a conds. **
7d390 20 74 6f 20 74 68 65 20 74 69 6d 65 2e 20 20 54 to the time. T
7d3a0 68 65 20 22 2e 46 46 46 22 20 6d 61 79 20 62 65 he ".FFF" may be
7d3b0 20 6f 6d 69 74 74 65 64 2e 20 20 54 68 65 20 22 omitted. The "
7d3c0 3a 53 53 2e 46 46 46 22 20 6d 61 79 20 62 65 0a :SS.FFF" may be.
7d3d0 20 20 20 20 20 20 20 20 2a 2a 20 6f 6d 69 74 74 ** omitt
7d3e0 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 ed.. */.
7d3f0 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 const cha
7d400 72 20 2a 7a 32 20 3d 20 7a 3b 0a 20 20 20 20 20 r *z2 = z;.
7d410 20 20 20 44 61 74 65 54 69 6d 65 20 74 78 3b 0a DateTime tx;.
7d420 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
7d430 69 6e 74 36 34 20 64 61 79 3b 0a 20 20 20 20 20 int64 day;.
7d440 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 49 if( !sqlite3I
7d450 73 64 69 67 69 74 28 2a 7a 32 29 20 29 20 7a 32 sdigit(*z2) ) z2
7d460 2b 2b 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 ++;. mems
7d470 65 74 28 26 74 78 2c 20 30 2c 20 73 69 7a 65 6f et(&tx, 0, sizeo
7d480 66 28 74 78 29 29 3b 0a 20 20 20 20 20 20 20 20 f(tx));.
7d490 69 66 28 20 70 61 72 73 65 48 68 4d 6d 53 73 28 if( parseHhMmSs(
7d4a0 7a 32 2c 20 26 74 78 29 20 29 20 62 72 65 61 6b z2, &tx) ) break
7d4b0 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 ;. comput
7d4c0 65 4a 44 28 26 74 78 29 3b 0a 20 20 20 20 20 20 eJD(&tx);.
7d4d0 20 20 74 78 2e 69 4a 44 20 2d 3d 20 34 33 32 30 tx.iJD -= 4320
7d4e0 30 30 30 30 3b 0a 20 20 20 20 20 20 20 20 64 61 0000;. da
7d4f0 79 20 3d 20 74 78 2e 69 4a 44 2f 38 36 34 30 30 y = tx.iJD/86400
7d500 30 30 30 3b 0a 20 20 20 20 20 20 20 20 74 78 2e 000;. tx.
7d510 69 4a 44 20 2d 3d 20 64 61 79 2a 38 36 34 30 30 iJD -= day*86400
7d520 30 30 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 000;. if(
7d530 20 7a 5b 30 5d 3d 3d 27 2d 27 20 29 20 74 78 2e z[0]=='-' ) tx.
7d540 69 4a 44 20 3d 20 2d 74 78 2e 69 4a 44 3b 0a 20 iJD = -tx.iJD;.
7d550 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 computeJD
7d560 28 70 29 3b 0a 20 20 20 20 20 20 20 20 63 6c 65 (p);. cle
7d570 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b arYMD_HMS_TZ(p);
7d580 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 . p->iJD
7d590 2b 3d 20 74 78 2e 69 4a 44 3b 0a 20 20 20 20 20 += tx.iJD;.
7d5a0 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 rc = 0;.
7d5b0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
7d5c0 7d 0a 20 20 20 20 20 20 7a 20 2b 3d 20 6e 3b 0a }. z += n;.
7d5d0 20 20 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c while( sql
7d5e0 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a 29 20 ite3Isspace(*z)
7d5f0 29 20 7a 2b 2b 3b 0a 20 20 20 20 20 20 6e 20 3d ) z++;. n =
7d600 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
7d610 28 7a 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e (z);. if( n
7d620 3e 31 30 20 7c 7c 20 6e 3c 33 20 29 20 62 72 65 >10 || n<3 ) bre
7d630 61 6b 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b ak;. if( z[
7d640 6e 2d 31 5d 3d 3d 27 73 27 20 29 7b 20 7a 5b 6e n-1]=='s' ){ z[n
7d650 2d 31 5d 20 3d 20 30 3b 20 6e 2d 2d 3b 20 7d 0a -1] = 0; n--; }.
7d660 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 computeJD(
7d670 70 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 30 p);. rc = 0
7d680 3b 0a 20 20 20 20 20 20 72 52 6f 75 6e 64 65 72 ;. rRounder
7d690 20 3d 20 72 3c 30 20 3f 20 2d 30 2e 35 20 3a 20 = r<0 ? -0.5 :
7d6a0 2b 30 2e 35 3b 0a 20 20 20 20 20 20 69 66 28 20 +0.5;. if(
7d6b0 6e 3d 3d 33 20 26 26 20 73 74 72 63 6d 70 28 7a n==3 && strcmp(z
7d6c0 2c 22 64 61 79 22 29 3d 3d 30 20 29 7b 0a 20 20 ,"day")==0 ){.
7d6d0 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 p->iJD +=
7d6e0 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 (sqlite3_int64)(
7d6f0 72 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 20 72 r*86400000.0 + r
7d700 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 20 20 Rounder);.
7d710 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 34 20 26 }else if( n==4 &
7d720 26 20 73 74 72 63 6d 70 28 7a 2c 22 68 6f 75 72 & strcmp(z,"hour
7d730 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ")==0 ){.
7d740 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 p->iJD += (sqli
7d750 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 28 38 36 te3_int64)(r*(86
7d760 34 30 30 30 30 30 2e 30 2f 32 34 2e 30 29 20 2b 400000.0/24.0) +
7d770 20 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 rRounder);.
7d780 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 36 }else if( n==6
7d790 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 6d 69 && strcmp(z,"mi
7d7a0 6e 75 74 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 nute")==0 ){.
7d7b0 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 p->iJD += (
7d7c0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72 sqlite3_int64)(r
7d7d0 2a 28 38 36 34 30 30 30 30 30 2e 30 2f 28 32 34 *(86400000.0/(24
7d7e0 2e 30 2a 36 30 2e 30 29 29 20 2b 20 72 52 6f 75 .0*60.0)) + rRou
7d7f0 6e 64 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c nder);. }el
7d800 73 65 20 69 66 28 20 6e 3d 3d 36 20 26 26 20 73 se if( n==6 && s
7d810 74 72 63 6d 70 28 7a 2c 22 73 65 63 6f 6e 64 22 trcmp(z,"second"
7d820 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )==0 ){.
7d830 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 74 p->iJD += (sqlit
7d840 65 33 5f 69 6e 74 36 34 29 28 72 2a 28 38 36 34 e3_int64)(r*(864
7d850 30 30 30 30 30 2e 30 2f 28 32 34 2e 30 2a 36 30 00000.0/(24.0*60
7d860 2e 30 2a 36 30 2e 30 29 29 20 2b 20 72 52 6f 75 .0*60.0)) + rRou
7d870 6e 64 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c nder);. }el
7d880 73 65 20 69 66 28 20 6e 3d 3d 35 20 26 26 20 73 se if( n==5 && s
7d890 74 72 63 6d 70 28 7a 2c 22 6d 6f 6e 74 68 22 29 trcmp(z,"month")
7d8a0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 ==0 ){. i
7d8b0 6e 74 20 78 2c 20 79 3b 0a 20 20 20 20 20 20 20 nt x, y;.
7d8c0 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 computeYMD_HMS(
7d8d0 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 4d p);. p->M
7d8e0 20 2b 3d 20 28 69 6e 74 29 72 3b 0a 20 20 20 20 += (int)r;.
7d8f0 20 20 20 20 78 20 3d 20 70 2d 3e 4d 3e 30 20 3f x = p->M>0 ?
7d900 20 28 70 2d 3e 4d 2d 31 29 2f 31 32 20 3a 20 28 (p->M-1)/12 : (
7d910 70 2d 3e 4d 2d 31 32 29 2f 31 32 3b 0a 20 20 20 p->M-12)/12;.
7d920 20 20 20 20 20 70 2d 3e 59 20 2b 3d 20 78 3b 0a p->Y += x;.
7d930 20 20 20 20 20 20 20 20 70 2d 3e 4d 20 2d 3d 20 p->M -=
7d940 78 2a 31 32 3b 0a 20 20 20 20 20 20 20 20 70 2d x*12;. p-
7d950 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 >validJD = 0;.
7d960 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 computeJD(
7d970 70 29 3b 0a 20 20 20 20 20 20 20 20 79 20 3d 20 p);. y =
7d980 28 69 6e 74 29 72 3b 0a 20 20 20 20 20 20 20 20 (int)r;.
7d990 69 66 28 20 79 21 3d 72 20 29 7b 0a 20 20 20 20 if( y!=r ){.
7d9a0 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 p->iJD +=
7d9b0 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 (sqlite3_int64)(
7d9c0 28 72 20 2d 20 79 29 2a 33 30 2e 30 2a 38 36 34 (r - y)*30.0*864
7d9d0 30 30 30 30 30 2e 30 20 2b 20 72 52 6f 75 6e 64 00000.0 + rRound
7d9e0 65 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 er);. }.
7d9f0 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e }else if( n
7da00 3d 3d 34 20 26 26 20 73 74 72 63 6d 70 28 7a 2c ==4 && strcmp(z,
7da10 22 79 65 61 72 22 29 3d 3d 30 20 29 7b 0a 20 20 "year")==0 ){.
7da20 20 20 20 20 20 20 69 6e 74 20 79 20 3d 20 28 69 int y = (i
7da30 6e 74 29 72 3b 0a 20 20 20 20 20 20 20 20 63 6f nt)r;. co
7da40 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 70 29 3b mputeYMD_HMS(p);
7da50 0a 20 20 20 20 20 20 20 20 70 2d 3e 59 20 2b 3d . p->Y +=
7da60 20 79 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 y;. p->v
7da70 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 alidJD = 0;.
7da80 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 computeJD(p)
7da90 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 79 21 ;. if( y!
7daa0 3d 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =r ){.
7dab0 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 74 p->iJD += (sqlit
7dac0 65 33 5f 69 6e 74 36 34 29 28 28 72 20 2d 20 79 e3_int64)((r - y
7dad0 29 2a 33 36 35 2e 30 2a 38 36 34 30 30 30 30 30 )*365.0*86400000
7dae0 2e 30 20 2b 20 72 52 6f 75 6e 64 65 72 29 3b 0a .0 + rRounder);.
7daf0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
7db00 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 }else{. r
7db10 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 c = 1;. }.
7db20 20 20 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d clearYMD_HM
7db30 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 62 S_TZ(p);. b
7db40 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
7db50 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 default: {.
7db60 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
7db70 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
7db80 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 20 ../*.** Process
7db90 74 69 6d 65 20 66 75 6e 63 74 69 6f 6e 20 61 72 time function ar
7dba0 67 75 6d 65 6e 74 73 2e 20 20 61 72 67 76 5b 30 guments. argv[0
7dbb0 5d 20 69 73 20 61 20 64 61 74 65 2d 74 69 6d 65 ] is a date-time
7dbc0 20 73 74 61 6d 70 2e 0a 2a 2a 20 61 72 67 76 5b stamp..** argv[
7dbd0 31 5d 20 61 6e 64 20 66 6f 6c 6c 6f 77 69 6e 67 1] and following
7dbe0 20 61 72 65 20 6d 6f 64 69 66 69 65 72 73 2e 20 are modifiers.
7dbf0 20 50 61 72 73 65 20 74 68 65 6d 20 61 6c 6c 20 Parse them all
7dc00 61 6e 64 20 77 72 69 74 65 0a 2a 2a 20 74 68 65 and write.** the
7dc10 20 72 65 73 75 6c 74 69 6e 67 20 74 69 6d 65 20 resulting time
7dc20 69 6e 74 6f 20 74 68 65 20 44 61 74 65 54 69 6d into the DateTim
7dc30 65 20 73 74 72 75 63 74 75 72 65 20 70 2e 20 20 e structure p.
7dc40 52 65 74 75 72 6e 20 30 0a 2a 2a 20 6f 6e 20 73 Return 0.** on s
7dc50 75 63 63 65 73 73 20 61 6e 64 20 31 20 69 66 20 uccess and 1 if
7dc60 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 65 72 there are any er
7dc70 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 rors..**.** If t
7dc80 68 65 72 65 20 61 72 65 20 7a 65 72 6f 20 70 61 here are zero pa
7dc90 72 61 6d 65 74 65 72 73 20 28 69 66 20 65 76 65 rameters (if eve
7dca0 6e 20 61 72 67 76 5b 30 5d 20 69 73 20 75 6e 64 n argv[0] is und
7dcb0 65 66 69 6e 65 64 29 0a 2a 2a 20 74 68 65 6e 20 efined).** then
7dcc0 61 73 73 75 6d 65 20 61 20 64 65 66 61 75 6c 74 assume a default
7dcd0 20 76 61 6c 75 65 20 6f 66 20 22 6e 6f 77 22 20 value of "now"
7dce0 66 6f 72 20 61 72 67 76 5b 30 5d 2e 0a 2a 2f 0a for argv[0]..*/.
7dcf0 73 74 61 74 69 63 20 69 6e 74 20 69 73 44 61 74 static int isDat
7dd00 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e e(. sqlite3_con
7dd10 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a text *context, .
7dd20 20 20 69 6e 74 20 61 72 67 63 2c 20 0a 20 20 73 int argc, . s
7dd30 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 qlite3_value **a
7dd40 72 67 76 2c 20 0a 20 20 44 61 74 65 54 69 6d 65 rgv, . DateTime
7dd50 20 2a 70 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a *p.){. int i;.
7dd60 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 const unsigned
7dd70 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69 6e 74 20 char *z;. int
7dd80 65 54 79 70 65 3b 0a 20 20 6d 65 6d 73 65 74 28 eType;. memset(
7dd90 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 29 p, 0, sizeof(*p)
7dda0 29 3b 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 30 );. if( argc==0
7ddb0 20 29 7b 0a 20 20 20 20 73 65 74 44 61 74 65 54 ){. setDateT
7ddc0 69 6d 65 54 6f 43 75 72 72 65 6e 74 28 63 6f 6e imeToCurrent(con
7ddd0 74 65 78 74 2c 20 70 29 3b 0a 20 20 7d 65 6c 73 text, p);. }els
7dde0 65 20 69 66 28 20 28 65 54 79 70 65 20 3d 20 73 e if( (eType = s
7ddf0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 qlite3_value_typ
7de00 65 28 61 72 67 76 5b 30 5d 29 29 3d 3d 53 51 4c e(argv[0]))==SQL
7de10 49 54 45 5f 46 4c 4f 41 54 0a 20 20 20 20 20 20 ITE_FLOAT.
7de20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 ||
7de30 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 49 4e eType==SQLITE_IN
7de40 54 45 47 45 52 20 29 7b 0a 20 20 20 20 70 2d 3e TEGER ){. p->
7de50 69 4a 44 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 iJD = (sqlite3_i
7de60 6e 74 36 34 29 28 73 71 6c 69 74 65 33 5f 76 61 nt64)(sqlite3_va
7de70 6c 75 65 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b lue_double(argv[
7de80 30 5d 29 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 0])*86400000.0 +
7de90 20 30 2e 35 29 3b 0a 20 20 20 20 70 2d 3e 76 61 0.5);. p->va
7dea0 6c 69 64 4a 44 20 3d 20 31 3b 0a 20 20 7d 65 6c lidJD = 1;. }el
7deb0 73 65 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 se{. z = sqli
7dec0 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 te3_value_text(a
7ded0 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 69 66 28 rgv[0]);. if(
7dee0 20 21 7a 20 7c 7c 20 70 61 72 73 65 44 61 74 65 !z || parseDate
7def0 4f 72 54 69 6d 65 28 63 6f 6e 74 65 78 74 2c 20 OrTime(context,
7df00 28 63 68 61 72 2a 29 7a 2c 20 70 29 20 29 7b 0a (char*)z, p) ){.
7df10 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a return 1;.
7df20 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 }. }. for(
7df30 69 3d 31 3b 20 69 3c 61 72 67 63 3b 20 69 2b 2b i=1; i<argc; i++
7df40 29 7b 0a 20 20 20 20 69 66 28 20 28 7a 20 3d 20 ){. if( (z =
7df50 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
7df60 78 74 28 61 72 67 76 5b 69 5d 29 29 3d 3d 30 20 xt(argv[i]))==0
7df70 7c 7c 20 70 61 72 73 65 4d 6f 64 69 66 69 65 72 || parseModifier
7df80 28 28 63 68 61 72 2a 29 7a 2c 20 70 29 20 29 7b ((char*)z, p) ){
7df90 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b . return 1;
7dfa0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
7dfb0 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a urn 0;.}.../*.**
7dfc0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 The following r
7dfd0 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e outines implemen
7dfe0 74 20 74 68 65 20 76 61 72 69 6f 75 73 20 64 61 t the various da
7dff0 74 65 20 61 6e 64 20 74 69 6d 65 20 66 75 6e 63 te and time func
7e000 74 69 6f 6e 73 0a 2a 2a 20 6f 66 20 53 51 4c 69 tions.** of SQLi
7e010 74 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 20 20 te..*/../*.**
7e020 20 6a 75 6c 69 61 6e 64 61 79 28 20 54 49 4d 45 julianday( TIME
7e030 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 STRING, MOD, MOD
7e040 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 , ...).**.** Ret
7e050 75 72 6e 20 74 68 65 20 6a 75 6c 69 61 6e 20 64 urn the julian d
7e060 61 79 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 ay number of the
7e070 20 64 61 74 65 20 73 70 65 63 69 66 69 65 64 20 date specified
7e080 69 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 73 in the arguments
7e090 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
7e0a0 6a 75 6c 69 61 6e 64 61 79 46 75 6e 63 28 0a 20 juliandayFunc(.
7e0b0 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
7e0c0 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 *context,. int
7e0d0 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 argc,. sqlite3
7e0e0 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b _value **argv.){
7e0f0 0a 20 20 44 61 74 65 54 69 6d 65 20 78 3b 0a 20 . DateTime x;.
7e100 20 69 66 28 20 69 73 44 61 74 65 28 63 6f 6e 74 if( isDate(cont
7e110 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c ext, argc, argv,
7e120 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 63 &x)==0 ){. c
7e130 6f 6d 70 75 74 65 4a 44 28 26 78 29 3b 0a 20 20 omputeJD(&x);.
7e140 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 sqlite3_result
7e150 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c _double(context,
7e160 20 78 2e 69 4a 44 2f 38 36 34 30 30 30 30 30 2e x.iJD/86400000.
7e170 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 0);. }.}../*.**
7e180 20 20 20 20 64 61 74 65 74 69 6d 65 28 20 54 49 datetime( TI
7e190 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d MESTRING, MOD, M
7e1a0 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 OD, ...).**.** R
7e1b0 65 74 75 72 6e 20 59 59 59 59 2d 4d 4d 2d 44 44 eturn YYYY-MM-DD
7e1c0 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2f 0a 73 74 61 HH:MM:SS.*/.sta
7e1d0 74 69 63 20 76 6f 69 64 20 64 61 74 65 74 69 6d tic void datetim
7e1e0 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 eFunc(. sqlite3
7e1f0 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
7e200 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 t,. int argc,.
7e210 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
7e220 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 *argv.){. DateT
7e230 69 6d 65 20 78 3b 0a 20 20 69 66 28 20 69 73 44 ime x;. if( isD
7e240 61 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 ate(context, arg
7e250 63 2c 20 61 72 67 76 2c 20 26 78 29 3d 3d 30 20 c, argv, &x)==0
7e260 29 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66 ){. char zBuf
7e270 5b 31 30 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 75 [100];. compu
7e280 74 65 59 4d 44 5f 48 4d 53 28 26 78 29 3b 0a 20 teYMD_HMS(&x);.
7e290 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
7e2a0 6e 74 66 28 73 69 7a 65 6f 66 28 7a 42 75 66 29 ntf(sizeof(zBuf)
7e2b0 2c 20 7a 42 75 66 2c 20 22 25 30 34 64 2d 25 30 , zBuf, "%04d-%0
7e2c0 32 64 2d 25 30 32 64 20 25 30 32 64 3a 25 30 32 2d-%02d %02d:%02
7e2d0 64 3a 25 30 32 64 22 2c 0a 20 20 20 20 20 20 20 d:%02d",.
7e2e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 78 2e x.
7e2f0 59 2c 20 78 2e 4d 2c 20 78 2e 44 2c 20 78 2e 68 Y, x.M, x.D, x.h
7e300 2c 20 78 2e 6d 2c 20 28 69 6e 74 29 28 78 2e 73 , x.m, (int)(x.s
7e310 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ));. sqlite3_
7e320 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 result_text(cont
7e330 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53 ext, zBuf, -1, S
7e340 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 QLITE_TRANSIENT)
7e350 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 ;. }.}../*.**
7e360 20 20 74 69 6d 65 28 20 54 49 4d 45 53 54 52 49 time( TIMESTRI
7e370 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e NG, MOD, MOD, ..
7e380 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 .).**.** Return
7e390 48 48 3a 4d 4d 3a 53 53 0a 2a 2f 0a 73 74 61 74 HH:MM:SS.*/.stat
7e3a0 69 63 20 76 6f 69 64 20 74 69 6d 65 46 75 6e 63 ic void timeFunc
7e3b0 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 (. sqlite3_cont
7e3c0 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 ext *context,.
7e3d0 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 int argc,. sqli
7e3e0 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 te3_value **argv
7e3f0 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78 .){. DateTime x
7e400 3b 0a 20 20 69 66 28 20 69 73 44 61 74 65 28 63 ;. if( isDate(c
7e410 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72 ontext, argc, ar
7e420 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20 gv, &x)==0 ){.
7e430 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d char zBuf[100]
7e440 3b 0a 20 20 20 20 63 6f 6d 70 75 74 65 48 4d 53 ;. computeHMS
7e450 28 26 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 (&x);. sqlite
7e460 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 3_snprintf(sizeo
7e470 66 28 7a 42 75 66 29 2c 20 7a 42 75 66 2c 20 22 f(zBuf), zBuf, "
7e480 25 30 32 64 3a 25 30 32 64 3a 25 30 32 64 22 2c %02d:%02d:%02d",
7e490 20 78 2e 68 2c 20 78 2e 6d 2c 20 28 69 6e 74 29 x.h, x.m, (int)
7e4a0 78 2e 73 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 x.s);. sqlite
7e4b0 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 3_result_text(co
7e4c0 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c ntext, zBuf, -1,
7e4d0 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e SQLITE_TRANSIEN
7e4e0 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a T);. }.}../*.**
7e4f0 20 20 20 20 64 61 74 65 28 20 54 49 4d 45 53 54 date( TIMEST
7e500 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 RING, MOD, MOD,
7e510 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 ...).**.** Retur
7e520 6e 20 59 59 59 59 2d 4d 4d 2d 44 44 0a 2a 2f 0a n YYYY-MM-DD.*/.
7e530 73 74 61 74 69 63 20 76 6f 69 64 20 64 61 74 65 static void date
7e540 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f Func(. sqlite3_
7e550 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
7e560 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 ,. int argc,.
7e570 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
7e580 61 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 argv.){. DateTi
7e590 6d 65 20 78 3b 0a 20 20 69 66 28 20 69 73 44 61 me x;. if( isDa
7e5a0 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 te(context, argc
7e5b0 2c 20 61 72 67 76 2c 20 26 78 29 3d 3d 30 20 29 , argv, &x)==0 )
7e5c0 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b {. char zBuf[
7e5d0 31 30 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 75 74 100];. comput
7e5e0 65 59 4d 44 28 26 78 29 3b 0a 20 20 20 20 73 71 eYMD(&x);. sq
7e5f0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 lite3_snprintf(s
7e600 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 7a 42 75 izeof(zBuf), zBu
7e610 66 2c 20 22 25 30 34 64 2d 25 30 32 64 2d 25 30 f, "%04d-%02d-%0
7e620 32 64 22 2c 20 78 2e 59 2c 20 78 2e 4d 2c 20 78 2d", x.Y, x.M, x
7e630 2e 44 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 .D);. sqlite3
7e640 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e _result_text(con
7e650 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 text, zBuf, -1,
7e660 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 SQLITE_TRANSIENT
7e670 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
7e680 20 20 20 73 74 72 66 74 69 6d 65 28 20 46 4f 52 strftime( FOR
7e690 4d 41 54 2c 20 54 49 4d 45 53 54 52 49 4e 47 2c MAT, TIMESTRING,
7e6a0 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a MOD, MOD, ...).
7e6b0 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 73 **.** Return a s
7e6c0 74 72 69 6e 67 20 64 65 73 63 72 69 62 65 64 20 tring described
7e6d0 62 79 20 46 4f 52 4d 41 54 2e 20 20 43 6f 6e 76 by FORMAT. Conv
7e6e0 65 72 73 69 6f 6e 73 20 61 73 20 66 6f 6c 6c 6f ersions as follo
7e6f0 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 25 64 20 20 ws:.**.** %d
7e700 64 61 79 20 6f 66 20 6d 6f 6e 74 68 0a 2a 2a 20 day of month.**
7e710 20 20 25 66 20 20 2a 2a 20 66 72 61 63 74 69 6f %f ** fractio
7e720 6e 61 6c 20 73 65 63 6f 6e 64 73 20 20 53 53 2e nal seconds SS.
7e730 53 53 53 0a 2a 2a 20 20 20 25 48 20 20 68 6f 75 SSS.** %H hou
7e740 72 20 30 30 2d 32 34 0a 2a 2a 20 20 20 25 6a 20 r 00-24.** %j
7e750 20 64 61 79 20 6f 66 20 79 65 61 72 20 30 30 30 day of year 000
7e760 2d 33 36 36 0a 2a 2a 20 20 20 25 4a 20 20 2a 2a -366.** %J **
7e770 20 4a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 Julian day numb
7e780 65 72 0a 2a 2a 20 20 20 25 6d 20 20 6d 6f 6e 74 er.** %m mont
7e790 68 20 30 31 2d 31 32 0a 2a 2a 20 20 20 25 4d 20 h 01-12.** %M
7e7a0 20 6d 69 6e 75 74 65 20 30 30 2d 35 39 0a 2a 2a minute 00-59.**
7e7b0 20 20 20 25 73 20 20 73 65 63 6f 6e 64 73 20 73 %s seconds s
7e7c0 69 6e 63 65 20 31 39 37 30 2d 30 31 2d 30 31 0a ince 1970-01-01.
7e7d0 2a 2a 20 20 20 25 53 20 20 73 65 63 6f 6e 64 73 ** %S seconds
7e7e0 20 30 30 2d 35 39 0a 2a 2a 20 20 20 25 77 20 20 00-59.** %w
7e7f0 64 61 79 20 6f 66 20 77 65 65 6b 20 30 2d 36 20 day of week 0-6
7e800 20 73 75 6e 64 61 79 3d 3d 30 0a 2a 2a 20 20 20 sunday==0.**
7e810 25 57 20 20 77 65 65 6b 20 6f 66 20 79 65 61 72 %W week of year
7e820 20 30 30 2d 35 33 0a 2a 2a 20 20 20 25 59 20 20 00-53.** %Y
7e830 79 65 61 72 20 30 30 30 30 2d 39 39 39 39 0a 2a year 0000-9999.*
7e840 2a 20 20 20 25 25 20 20 25 0a 2a 2f 0a 73 74 61 * %% %.*/.sta
7e850 74 69 63 20 76 6f 69 64 20 73 74 72 66 74 69 6d tic void strftim
7e860 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 eFunc(. sqlite3
7e870 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
7e880 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 t,. int argc,.
7e890 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
7e8a0 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 *argv.){. DateT
7e8b0 69 6d 65 20 78 3b 0a 20 20 75 36 34 20 6e 3b 0a ime x;. u64 n;.
7e8c0 20 20 73 69 7a 65 5f 74 20 69 2c 6a 3b 0a 20 20 size_t i,j;.
7e8d0 63 68 61 72 20 2a 7a 3b 0a 20 20 73 71 6c 69 74 char *z;. sqlit
7e8e0 65 33 20 2a 64 62 3b 0a 20 20 63 6f 6e 73 74 20 e3 *db;. const
7e8f0 63 68 61 72 20 2a 7a 46 6d 74 20 3d 20 28 63 6f char *zFmt = (co
7e900 6e 73 74 20 63 68 61 72 2a 29 73 71 6c 69 74 65 nst char*)sqlite
7e910 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 3_value_text(arg
7e920 76 5b 30 5d 29 3b 0a 20 20 63 68 61 72 20 7a 42 v[0]);. char zB
7e930 75 66 5b 31 30 30 5d 3b 0a 20 20 69 66 28 20 7a uf[100];. if( z
7e940 46 6d 74 3d 3d 30 20 7c 7c 20 69 73 44 61 74 65 Fmt==0 || isDate
7e950 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2d 31 (context, argc-1
7e960 2c 20 61 72 67 76 2b 31 2c 20 26 78 29 20 29 20 , argv+1, &x) )
7e970 72 65 74 75 72 6e 3b 0a 20 20 64 62 20 3d 20 73 return;. db = s
7e980 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 qlite3_context_d
7e990 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 b_handle(context
7e9a0 29 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 6e 3d );. for(i=0, n=
7e9b0 31 3b 20 7a 46 6d 74 5b 69 5d 3b 20 69 2b 2b 2c 1; zFmt[i]; i++,
7e9c0 20 6e 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a n++){. if( z
7e9d0 46 6d 74 5b 69 5d 3d 3d 27 25 27 20 29 7b 0a 20 Fmt[i]=='%' ){.
7e9e0 20 20 20 20 20 73 77 69 74 63 68 28 20 7a 46 6d switch( zFm
7e9f0 74 5b 69 2b 31 5d 20 29 7b 0a 20 20 20 20 20 20 t[i+1] ){.
7ea00 20 20 63 61 73 65 20 27 64 27 3a 0a 20 20 20 20 case 'd':.
7ea10 20 20 20 20 63 61 73 65 20 27 48 27 3a 0a 20 20 case 'H':.
7ea20 20 20 20 20 20 20 63 61 73 65 20 27 6d 27 3a 0a case 'm':.
7ea30 20 20 20 20 20 20 20 20 63 61 73 65 20 27 4d 27 case 'M'
7ea40 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 :. case '
7ea50 53 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 S':. case
7ea60 20 27 57 27 3a 0a 20 20 20 20 20 20 20 20 20 20 'W':.
7ea70 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 2f n++;. /
7ea80 2a 20 66 61 6c 6c 20 74 68 72 75 20 2a 2f 0a 20 * fall thru */.
7ea90 20 20 20 20 20 20 20 63 61 73 65 20 27 77 27 3a case 'w':
7eaa0 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 25 . case '%
7eab0 27 3a 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 ':. bre
7eac0 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 ak;. case
7ead0 20 27 66 27 3a 0a 20 20 20 20 20 20 20 20 20 20 'f':.
7eae0 6e 20 2b 3d 20 38 3b 0a 20 20 20 20 20 20 20 20 n += 8;.
7eaf0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
7eb00 20 63 61 73 65 20 27 6a 27 3a 0a 20 20 20 20 20 case 'j':.
7eb10 20 20 20 20 20 6e 20 2b 3d 20 33 3b 0a 20 20 20 n += 3;.
7eb20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
7eb30 20 20 20 20 20 20 63 61 73 65 20 27 59 27 3a 0a case 'Y':.
7eb40 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 38 n += 8
7eb50 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 ;. brea
7eb60 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 k;. case
7eb70 27 73 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73 's':. cas
7eb80 65 20 27 4a 27 3a 0a 20 20 20 20 20 20 20 20 20 e 'J':.
7eb90 20 6e 20 2b 3d 20 35 30 3b 0a 20 20 20 20 20 20 n += 50;.
7eba0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
7ebb0 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 default:.
7ebc0 20 20 20 20 20 20 72 65 74 75 72 6e 3b 20 20 2f return; /
7ebd0 2a 20 45 52 52 4f 52 2e 20 20 72 65 74 75 72 6e * ERROR. return
7ebe0 20 61 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 a NULL */.
7ebf0 20 7d 0a 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 }. i++;.
7ec00 20 20 7d 0a 20 20 7d 0a 20 20 74 65 73 74 63 61 }. }. testca
7ec10 73 65 28 20 6e 3d 3d 73 69 7a 65 6f 66 28 7a 42 se( n==sizeof(zB
7ec20 75 66 29 2d 31 20 29 3b 0a 20 20 74 65 73 74 63 uf)-1 );. testc
7ec30 61 73 65 28 20 6e 3d 3d 73 69 7a 65 6f 66 28 7a ase( n==sizeof(z
7ec40 42 75 66 29 20 29 3b 0a 20 20 74 65 73 74 63 61 Buf) );. testca
7ec50 73 65 28 20 6e 3d 3d 28 75 36 34 29 64 62 2d 3e se( n==(u64)db->
7ec60 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 aLimit[SQLITE_LI
7ec70 4d 49 54 5f 4c 45 4e 47 54 48 5d 2b 31 20 29 3b MIT_LENGTH]+1 );
7ec80 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 3d 3d . testcase( n==
7ec90 28 75 36 34 29 64 62 2d 3e 61 4c 69 6d 69 74 5b (u64)db->aLimit[
7eca0 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e SQLITE_LIMIT_LEN
7ecb0 47 54 48 5d 20 29 3b 0a 20 20 69 66 28 20 6e 3c GTH] );. if( n<
7ecc0 73 69 7a 65 6f 66 28 7a 42 75 66 29 20 29 7b 0a sizeof(zBuf) ){.
7ecd0 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 z = zBuf;.
7ece0 7d 65 6c 73 65 20 69 66 28 20 6e 3e 28 75 36 34 }else if( n>(u64
7ecf0 29 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 )db->aLimit[SQLI
7ed00 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d TE_LIMIT_LENGTH]
7ed10 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
7ed20 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f result_error_too
7ed30 62 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 big(context);.
7ed40 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73 return;. }els
7ed50 65 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 e{. z = sqlit
7ed60 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 e3DbMallocRaw(db
7ed70 2c 20 28 69 6e 74 29 6e 29 3b 0a 20 20 20 20 69 , (int)n);. i
7ed80 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 20 f( z==0 ){.
7ed90 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
7eda0 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 error_nomem(cont
7edb0 65 78 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75 ext);. retu
7edc0 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 rn;. }. }.
7edd0 63 6f 6d 70 75 74 65 4a 44 28 26 78 29 3b 0a 20 computeJD(&x);.
7ede0 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 computeYMD_HMS(
7edf0 26 78 29 3b 0a 20 20 66 6f 72 28 69 3d 6a 3d 30 &x);. for(i=j=0
7ee00 3b 20 7a 46 6d 74 5b 69 5d 3b 20 69 2b 2b 29 7b ; zFmt[i]; i++){
7ee10 0a 20 20 20 20 69 66 28 20 7a 46 6d 74 5b 69 5d . if( zFmt[i]
7ee20 21 3d 27 25 27 20 29 7b 0a 20 20 20 20 20 20 7a !='%' ){. z
7ee30 5b 6a 2b 2b 5d 20 3d 20 7a 46 6d 74 5b 69 5d 3b [j++] = zFmt[i];
7ee40 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
7ee50 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 73 77 69 i++;. swi
7ee60 74 63 68 28 20 7a 46 6d 74 5b 69 5d 20 29 7b 0a tch( zFmt[i] ){.
7ee70 20 20 20 20 20 20 20 20 63 61 73 65 20 27 64 27 case 'd'
7ee80 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 : sqlite3_snpri
7ee90 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 ntf(3, &z[j],"%0
7eea0 32 64 22 2c 78 2e 44 29 3b 20 6a 2b 3d 32 3b 20 2d",x.D); j+=2;
7eeb0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 break;. c
7eec0 61 73 65 20 27 66 27 3a 20 7b 0a 20 20 20 20 20 ase 'f': {.
7eed0 20 20 20 20 20 64 6f 75 62 6c 65 20 73 20 3d 20 double s =
7eee0 78 2e 73 3b 0a 20 20 20 20 20 20 20 20 20 20 69 x.s;. i
7eef0 66 28 20 73 3e 35 39 2e 39 39 39 20 29 20 73 20 f( s>59.999 ) s
7ef00 3d 20 35 39 2e 39 39 39 3b 0a 20 20 20 20 20 20 = 59.999;.
7ef10 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
7ef20 69 6e 74 66 28 37 2c 20 26 7a 5b 6a 5d 2c 22 25 intf(7, &z[j],"%
7ef30 30 36 2e 33 66 22 2c 20 73 29 3b 0a 20 20 20 20 06.3f", s);.
7ef40 20 20 20 20 20 20 6a 20 2b 3d 20 73 71 6c 69 74 j += sqlit
7ef50 65 33 53 74 72 6c 65 6e 33 30 28 26 7a 5b 6a 5d e3Strlen30(&z[j]
7ef60 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 );. bre
7ef70 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ak;. }.
7ef80 20 20 20 20 20 20 63 61 73 65 20 27 48 27 3a 20 case 'H':
7ef90 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
7efa0 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 f(3, &z[j],"%02d
7efb0 22 2c 78 2e 68 29 3b 20 6a 2b 3d 32 3b 20 62 72 ",x.h); j+=2; br
7efc0 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 eak;. cas
7efd0 65 20 27 57 27 3a 20 2f 2a 20 46 61 6c 6c 20 74 e 'W': /* Fall t
7efe0 68 72 75 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 hru */. c
7eff0 61 73 65 20 27 6a 27 3a 20 7b 0a 20 20 20 20 20 ase 'j': {.
7f000 20 20 20 20 20 69 6e 74 20 6e 44 61 79 3b 20 20 int nDay;
7f010 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
7f020 6d 62 65 72 20 6f 66 20 64 61 79 73 20 73 69 6e mber of days sin
7f030 63 65 20 31 73 74 20 64 61 79 20 6f 66 20 79 65 ce 1st day of ye
7f040 61 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 ar */.
7f050 44 61 74 65 54 69 6d 65 20 79 20 3d 20 78 3b 0a DateTime y = x;.
7f060 20 20 20 20 20 20 20 20 20 20 79 2e 76 61 6c 69 y.vali
7f070 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 dJD = 0;.
7f080 20 20 20 79 2e 4d 20 3d 20 31 3b 0a 20 20 20 20 y.M = 1;.
7f090 20 20 20 20 20 20 79 2e 44 20 3d 20 31 3b 0a 20 y.D = 1;.
7f0a0 20 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 compute
7f0b0 4a 44 28 26 79 29 3b 0a 20 20 20 20 20 20 20 20 JD(&y);.
7f0c0 20 20 6e 44 61 79 20 3d 20 28 69 6e 74 29 28 28 nDay = (int)((
7f0d0 78 2e 69 4a 44 2d 79 2e 69 4a 44 2b 34 33 32 30 x.iJD-y.iJD+4320
7f0e0 30 30 30 30 29 2f 38 36 34 30 30 30 30 30 29 3b 0000)/86400000);
7f0f0 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a . if( z
7f100 46 6d 74 5b 69 5d 3d 3d 27 57 27 20 29 7b 0a 20 Fmt[i]=='W' ){.
7f110 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 77 int w
7f120 64 3b 20 20 20 2f 2a 20 30 3d 4d 6f 6e 64 61 79 d; /* 0=Monday
7f130 2c 20 31 3d 54 75 65 73 64 61 79 2c 20 2e 2e 2e , 1=Tuesday, ...
7f140 20 36 3d 53 75 6e 64 61 79 20 2a 2f 0a 20 20 20 6=Sunday */.
7f150 20 20 20 20 20 20 20 20 20 77 64 20 3d 20 28 69 wd = (i
7f160 6e 74 29 28 28 28 78 2e 69 4a 44 2b 34 33 32 30 nt)(((x.iJD+4320
7f170 30 30 30 30 29 2f 38 36 34 30 30 30 30 30 29 25 0000)/86400000)%
7f180 37 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7);.
7f190 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
7f1a0 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 (3, &z[j],"%02d"
7f1b0 2c 28 6e 44 61 79 2b 37 2d 77 64 29 2f 37 29 3b ,(nDay+7-wd)/7);
7f1c0 0a 20 20 20 20 20 20 20 20 20 20 20 20 6a 20 2b . j +
7f1d0 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 20 20 7d = 2;. }
7f1e0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
7f1f0 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
7f200 74 66 28 34 2c 20 26 7a 5b 6a 5d 2c 22 25 30 33 tf(4, &z[j],"%03
7f210 64 22 2c 6e 44 61 79 2b 31 29 3b 0a 20 20 20 20 d",nDay+1);.
7f220 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 33 3b 0a j += 3;.
7f230 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
7f240 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
7f250 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63 }. c
7f260 61 73 65 20 27 4a 27 3a 20 7b 0a 20 20 20 20 20 ase 'J': {.
7f270 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 sqlite3_snp
7f280 72 69 6e 74 66 28 32 30 2c 20 26 7a 5b 6a 5d 2c rintf(20, &z[j],
7f290 22 25 2e 31 36 67 22 2c 78 2e 69 4a 44 2f 38 36 "%.16g",x.iJD/86
7f2a0 34 30 30 30 30 30 2e 30 29 3b 0a 20 20 20 20 20 400000.0);.
7f2b0 20 20 20 20 20 6a 2b 3d 73 71 6c 69 74 65 33 53 j+=sqlite3S
7f2c0 74 72 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a trlen30(&z[j]);.
7f2d0 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b break;
7f2e0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
7f2f0 20 20 20 63 61 73 65 20 27 6d 27 3a 20 20 73 71 case 'm': sq
7f300 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 lite3_snprintf(3
7f310 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 78 , &z[j],"%02d",x
7f320 2e 4d 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b .M); j+=2; break
7f330 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 ;. case '
7f340 4d 27 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 M': sqlite3_snp
7f350 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 rintf(3, &z[j],"
7f360 25 30 32 64 22 2c 78 2e 6d 29 3b 20 6a 2b 3d 32 %02d",x.m); j+=2
7f370 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 ; break;.
7f380 20 63 61 73 65 20 27 73 27 3a 20 7b 0a 20 20 20 case 's': {.
7f390 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 sqlite3_s
7f3a0 6e 70 72 69 6e 74 66 28 33 30 2c 26 7a 5b 6a 5d nprintf(30,&z[j]
7f3b0 2c 22 25 6c 6c 64 22 2c 0a 20 20 20 20 20 20 20 ,"%lld",.
7f3c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7f3d0 20 20 20 20 28 69 36 34 29 28 78 2e 69 4a 44 2f (i64)(x.iJD/
7f3e0 31 30 30 30 20 2d 20 32 31 30 38 36 36 37 36 2a 1000 - 21086676*
7f3f0 28 69 36 34 29 31 30 30 30 30 29 29 3b 0a 20 20 (i64)10000));.
7f400 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 73 71 6c j += sql
7f410 69 74 65 33 53 74 72 6c 65 6e 33 30 28 26 7a 5b ite3Strlen30(&z[
7f420 6a 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 j]);. b
7f430 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a reak;. }.
7f440 20 20 20 20 20 20 20 20 63 61 73 65 20 27 53 27 case 'S'
7f450 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 : sqlite3_snpri
7f460 6e 74 66 28 33 2c 26 7a 5b 6a 5d 2c 22 25 30 32 ntf(3,&z[j],"%02
7f470 64 22 2c 28 69 6e 74 29 78 2e 73 29 3b 20 6a 2b d",(int)x.s); j+
7f480 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 =2; break;.
7f490 20 20 20 63 61 73 65 20 27 77 27 3a 20 7b 0a 20 case 'w': {.
7f4a0 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 z[j++]
7f4b0 3d 20 28 63 68 61 72 29 28 28 28 78 2e 69 4a 44 = (char)(((x.iJD
7f4c0 2b 31 32 39 36 30 30 30 30 30 29 2f 38 36 34 30 +129600000)/8640
7f4d0 30 30 30 30 29 20 25 20 37 29 20 2b 20 27 30 27 0000) % 7) + '0'
7f4e0 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 ;. brea
7f4f0 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 k;. }.
7f500 20 20 20 20 20 63 61 73 65 20 27 59 27 3a 20 7b case 'Y': {
7f510 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
7f520 65 33 5f 73 6e 70 72 69 6e 74 66 28 35 2c 26 7a e3_snprintf(5,&z
7f530 5b 6a 5d 2c 22 25 30 34 64 22 2c 78 2e 59 29 3b [j],"%04d",x.Y);
7f540 20 6a 2b 3d 73 71 6c 69 74 65 33 53 74 72 6c 65 j+=sqlite3Strle
7f550 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20 n30(&z[j]);.
7f560 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
7f570 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64 }. d
7f580 65 66 61 75 6c 74 3a 20 20 20 7a 5b 6a 2b 2b 5d efault: z[j++]
7f590 20 3d 20 27 25 27 3b 20 62 72 65 61 6b 3b 0a 20 = '%'; break;.
7f5a0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
7f5b0 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 73 . z[j] = 0;. s
7f5c0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 qlite3_result_te
7f5d0 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 2c 20 2d xt(context, z, -
7f5e0 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 1,.
7f5f0 20 20 20 20 20 20 20 20 20 7a 3d 3d 7a 42 75 66 z==zBuf
7f600 20 3f 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 ? SQLITE_TRANSI
7f610 45 4e 54 20 3a 20 53 51 4c 49 54 45 5f 44 59 4e ENT : SQLITE_DYN
7f620 41 4d 49 43 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 AMIC);.}../*.**
7f630 63 75 72 72 65 6e 74 5f 74 69 6d 65 28 29 0a 2a current_time().*
7f640 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
7f650 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 on returns the s
7f660 61 6d 65 20 76 61 6c 75 65 20 61 73 20 74 69 6d ame value as tim
7f670 65 28 27 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61 e('now')..*/.sta
7f680 74 69 63 20 76 6f 69 64 20 63 74 69 6d 65 46 75 tic void ctimeFu
7f690 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f nc(. sqlite3_co
7f6a0 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a ntext *context,.
7f6b0 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 int NotUsed,.
7f6c0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
7f6d0 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 55 *NotUsed2.){. U
7f6e0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 NUSED_PARAMETER2
7f6f0 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 (NotUsed, NotUse
7f700 64 32 29 3b 0a 20 20 74 69 6d 65 46 75 6e 63 28 d2);. timeFunc(
7f710 63 6f 6e 74 65 78 74 2c 20 30 2c 20 30 29 3b 0a context, 0, 0);.
7f720 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75 72 72 65 6e 74 }../*.** current
7f730 5f 64 61 74 65 28 29 0a 2a 2a 0a 2a 2a 20 54 68 _date().**.** Th
7f740 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 is function retu
7f750 72 6e 73 20 74 68 65 20 73 61 6d 65 20 76 61 6c rns the same val
7f760 75 65 20 61 73 20 64 61 74 65 28 27 6e 6f 77 27 ue as date('now'
7f770 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 )..*/.static voi
7f780 64 20 63 64 61 74 65 46 75 6e 63 28 0a 20 20 73 d cdateFunc(. s
7f790 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
7f7a0 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e context,. int N
7f7b0 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 otUsed,. sqlite
7f7c0 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 3_value **NotUse
7f7d0 64 32 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 d2.){. UNUSED_P
7f7e0 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 ARAMETER2(NotUse
7f7f0 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 d, NotUsed2);.
7f800 64 61 74 65 46 75 6e 63 28 63 6f 6e 74 65 78 74 dateFunc(context
7f810 2c 20 30 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a , 0, 0);.}../*.*
7f820 2a 20 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 74 * current_timest
7f830 61 6d 70 28 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 amp().**.** This
7f840 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
7f850 73 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 s the same value
7f860 20 61 73 20 64 61 74 65 74 69 6d 65 28 27 6e 6f as datetime('no
7f870 77 27 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 w')..*/.static v
7f880 6f 69 64 20 63 74 69 6d 65 73 74 61 6d 70 46 75 oid ctimestampFu
7f890 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f nc(. sqlite3_co
7f8a0 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a ntext *context,.
7f8b0 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 int NotUsed,.
7f8c0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
7f8d0 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 55 *NotUsed2.){. U
7f8e0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 NUSED_PARAMETER2
7f8f0 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 (NotUsed, NotUse
7f900 64 32 29 3b 0a 20 20 64 61 74 65 74 69 6d 65 46 d2);. datetimeF
7f910 75 6e 63 28 63 6f 6e 74 65 78 74 2c 20 30 2c 20 unc(context, 0,
7f920 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 0);.}.#endif /*
7f930 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
7f940 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 55 OMIT_DATETIME_FU
7f950 4e 43 53 29 20 2a 2f 0a 0a 23 69 66 64 65 66 20 NCS) */..#ifdef
7f960 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 SQLITE_OMIT_DATE
7f970 54 49 4d 45 5f 46 55 4e 43 53 0a 2f 2a 0a 2a 2a TIME_FUNCS./*.**
7f980 20 49 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 If the library
7f990 69 73 20 63 6f 6d 70 69 6c 65 64 20 74 6f 20 6f is compiled to o
7f9a0 6d 69 74 20 74 68 65 20 66 75 6c 6c 2d 73 63 61 mit the full-sca
7f9b0 6c 65 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65 le date and time
7f9c0 0a 2a 2a 20 68 61 6e 64 6c 69 6e 67 20 28 74 6f .** handling (to
7f9d0 20 67 65 74 20 61 20 73 6d 61 6c 6c 65 72 20 62 get a smaller b
7f9e0 69 6e 61 72 79 29 2c 20 74 68 65 20 66 6f 6c 6c inary), the foll
7f9f0 6f 77 69 6e 67 20 6d 69 6e 69 6d 61 6c 20 76 65 owing minimal ve
7fa00 72 73 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 rsion.** of the
7fa10 66 75 6e 63 74 69 6f 6e 73 20 63 75 72 72 65 6e functions curren
7fa20 74 5f 74 69 6d 65 28 29 2c 20 63 75 72 72 65 6e t_time(), curren
7fa30 74 5f 64 61 74 65 28 29 20 61 6e 64 20 63 75 72 t_date() and cur
7fa40 72 65 6e 74 5f 74 69 6d 65 73 74 61 6d 70 28 29 rent_timestamp()
7fa50 0a 2a 2a 20 61 72 65 20 69 6e 63 6c 75 64 65 64 .** are included
7fa60 20 69 6e 73 74 65 61 64 2e 20 54 68 69 73 20 69 instead. This i
7fa70 73 20 74 6f 20 73 75 70 70 6f 72 74 20 63 6f 6c s to support col
7fa80 75 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 umn declarations
7fa90 20 74 68 61 74 0a 2a 2a 20 69 6e 63 6c 75 64 65 that.** include
7faa0 20 22 44 45 46 41 55 4c 54 20 43 55 52 52 45 4e "DEFAULT CURREN
7fab0 54 5f 54 49 4d 45 22 20 65 74 63 2e 0a 2a 2a 0a T_TIME" etc..**.
7fac0 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
7fad0 20 75 73 65 73 20 74 68 65 20 43 2d 6c 69 62 72 uses the C-libr
7fae0 61 72 79 20 66 75 6e 63 74 69 6f 6e 73 20 74 69 ary functions ti
7faf0 6d 65 28 29 2c 20 67 6d 74 69 6d 65 28 29 0a 2a me(), gmtime().*
7fb00 2a 20 61 6e 64 20 73 74 72 66 74 69 6d 65 28 29 * and strftime()
7fb10 2e 20 54 68 65 20 66 6f 72 6d 61 74 20 73 74 72 . The format str
7fb20 69 6e 67 20 74 6f 20 70 61 73 73 20 74 6f 20 73 ing to pass to s
7fb30 74 72 66 74 69 6d 65 28 29 20 69 73 20 73 75 70 trftime() is sup
7fb40 70 6c 69 65 64 0a 2a 2a 20 61 73 20 74 68 65 20 plied.** as the
7fb50 75 73 65 72 2d 64 61 74 61 20 66 6f 72 20 74 68 user-data for th
7fb60 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 e function..*/.s
7fb70 74 61 74 69 63 20 76 6f 69 64 20 63 75 72 72 65 tatic void curre
7fb80 6e 74 54 69 6d 65 46 75 6e 63 28 0a 20 20 73 71 ntTimeFunc(. sq
7fb90 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 lite3_context *c
7fba0 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 ontext,. int ar
7fbb0 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 gc,. sqlite3_va
7fbc0 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 lue **argv.){.
7fbd0 74 69 6d 65 5f 74 20 74 3b 0a 20 20 63 68 61 72 time_t t;. char
7fbe0 20 2a 7a 46 6f 72 6d 61 74 20 3d 20 28 63 68 61 *zFormat = (cha
7fbf0 72 20 2a 29 73 71 6c 69 74 65 33 5f 75 73 65 72 r *)sqlite3_user
7fc00 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29 3b 0a _data(context);.
7fc10 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 sqlite3 *db;.
7fc20 20 64 6f 75 62 6c 65 20 72 54 3b 0a 20 20 63 68 double rT;. ch
7fc30 61 72 20 7a 42 75 66 5b 32 30 5d 3b 0a 0a 20 20 ar zBuf[20];..
7fc40 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
7fc50 28 61 72 67 63 29 3b 0a 20 20 55 4e 55 53 45 44 (argc);. UNUSED
7fc60 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 76 29 _PARAMETER(argv)
7fc70 3b 0a 0a 20 20 64 62 20 3d 20 73 71 6c 69 74 65 ;.. db = sqlite
7fc80 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 3_context_db_han
7fc90 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 dle(context);.
7fca0 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 sqlite3OsCurrent
7fcb0 54 69 6d 65 28 64 62 2d 3e 70 56 66 73 2c 20 26 Time(db->pVfs, &
7fcc0 72 54 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c rT);.#ifndef SQL
7fcd0 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e ITE_OMIT_FLOATIN
7fce0 47 5f 50 4f 49 4e 54 0a 20 20 74 20 3d 20 38 36 G_POINT. t = 86
7fcf0 34 30 30 2e 30 2a 28 72 54 20 2d 20 32 34 34 30 400.0*(rT - 2440
7fd00 35 38 37 2e 35 29 20 2b 20 30 2e 35 3b 0a 23 65 587.5) + 0.5;.#e
7fd10 6c 73 65 0a 20 20 2f 2a 20 77 69 74 68 6f 75 74 lse. /* without
7fd20 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 floating point
7fd30 73 75 70 70 6f 72 74 2c 20 72 54 20 77 69 6c 6c support, rT will
7fd40 20 68 61 76 65 0a 20 20 2a 2a 20 61 6c 72 65 61 have. ** alrea
7fd50 64 79 20 6c 6f 73 74 20 66 72 61 63 74 69 6f 6e dy lost fraction
7fd60 61 6c 20 64 61 79 20 70 72 65 63 69 73 69 6f 6e al day precision
7fd70 2e 0a 20 20 2a 2f 0a 20 20 74 20 3d 20 38 36 34 .. */. t = 864
7fd80 30 30 20 2a 20 28 72 54 20 2d 20 32 34 34 30 35 00 * (rT - 24405
7fd90 38 37 29 20 2d 20 34 33 32 30 30 3b 0a 23 65 6e 87) - 43200;.#en
7fda0 64 69 66 0a 23 69 66 64 65 66 20 48 41 56 45 5f dif.#ifdef HAVE_
7fdb0 47 4d 54 49 4d 45 5f 52 0a 20 20 7b 0a 20 20 20 GMTIME_R. {.
7fdc0 20 73 74 72 75 63 74 20 74 6d 20 73 4e 6f 77 3b struct tm sNow;
7fdd0 0a 20 20 20 20 67 6d 74 69 6d 65 5f 72 28 26 74 . gmtime_r(&t
7fde0 2c 20 26 73 4e 6f 77 29 3b 0a 20 20 20 20 73 74 , &sNow);. st
7fdf0 72 66 74 69 6d 65 28 7a 42 75 66 2c 20 32 30 2c rftime(zBuf, 20,
7fe00 20 7a 46 6f 72 6d 61 74 2c 20 26 73 4e 6f 77 29 zFormat, &sNow)
7fe10 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 7b 0a ;. }.#else. {.
7fe20 20 20 20 20 73 74 72 75 63 74 20 74 6d 20 2a 70 struct tm *p
7fe30 54 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f Tm;. sqlite3_
7fe40 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 mutex_enter(sqli
7fe50 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 te3MutexAlloc(SQ
7fe60 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
7fe70 43 5f 4d 41 53 54 45 52 29 29 3b 0a 20 20 20 20 C_MASTER));.
7fe80 70 54 6d 20 3d 20 67 6d 74 69 6d 65 28 26 74 29 pTm = gmtime(&t)
7fe90 3b 0a 20 20 20 20 73 74 72 66 74 69 6d 65 28 7a ;. strftime(z
7fea0 42 75 66 2c 20 32 30 2c 20 7a 46 6f 72 6d 61 74 Buf, 20, zFormat
7feb0 2c 20 70 54 6d 29 3b 0a 20 20 20 20 73 71 6c 69 , pTm);. sqli
7fec0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
7fed0 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f sqlite3MutexAllo
7fee0 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 c(SQLITE_MUTEX_S
7fef0 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a TATIC_MASTER));.
7ff00 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71 }.#endif.. sq
7ff10 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
7ff20 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c t(context, zBuf,
7ff30 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e -1, SQLITE_TRAN
7ff40 53 49 45 4e 54 29 3b 0a 7d 0a 23 65 6e 64 69 66 SIENT);.}.#endif
7ff50 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
7ff60 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 ction registered
7ff70 20 61 6c 6c 20 6f 66 20 74 68 65 20 61 62 6f 76 all of the abov
7ff80 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 61 73 e C functions as
7ff90 20 53 51 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e SQL.** function
7ffa0 73 2e 20 20 54 68 69 73 20 73 68 6f 75 6c 64 20 s. This should
7ffb0 62 65 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 be the only rout
7ffc0 69 6e 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 ine in this file
7ffd0 20 77 69 74 68 0a 2a 2a 20 65 78 74 65 72 6e 61 with.** externa
7ffe0 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2f 0a 53 51 l linkage..*/.SQ
7fff0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
80000 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 d sqlite3Registe
80010 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f rDateTimeFunctio
80020 6e 73 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 ns(void){. stat
80030 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 46 75 ic SQLITE_WSD Fu
80040 6e 63 44 65 66 20 61 44 61 74 65 54 69 6d 65 46 ncDef aDateTimeF
80050 75 6e 63 73 5b 5d 20 3d 20 7b 0a 23 69 66 6e 64 uncs[] = {.#ifnd
80060 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 ef SQLITE_OMIT_D
80070 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 0a 20 20 ATETIME_FUNCS.
80080 20 20 46 55 4e 43 54 49 4f 4e 28 6a 75 6c 69 61 FUNCTION(julia
80090 6e 64 61 79 2c 20 20 20 20 20 20 20 20 2d 31 2c nday, -1,
800a0 20 30 2c 20 30 2c 20 6a 75 6c 69 61 6e 64 61 79 0, 0, julianday
800b0 46 75 6e 63 20 29 2c 0a 20 20 20 20 46 55 4e 43 Func ),. FUNC
800c0 54 49 4f 4e 28 64 61 74 65 2c 20 20 20 20 20 20 TION(date,
800d0 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c -1, 0, 0,
800e0 20 64 61 74 65 46 75 6e 63 20 20 20 20 20 20 29 dateFunc )
800f0 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 74 ,. FUNCTION(t
80100 69 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ime,
80110 20 2d 31 2c 20 30 2c 20 30 2c 20 74 69 6d 65 46 -1, 0, 0, timeF
80120 75 6e 63 20 20 20 20 20 20 29 2c 0a 20 20 20 20 unc ),.
80130 46 55 4e 43 54 49 4f 4e 28 64 61 74 65 74 69 6d FUNCTION(datetim
80140 65 2c 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30 e, -1, 0
80150 2c 20 30 2c 20 64 61 74 65 74 69 6d 65 46 75 6e , 0, datetimeFun
80160 63 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 c ),. FUNCTI
80170 4f 4e 28 73 74 72 66 74 69 6d 65 2c 20 20 20 20 ON(strftime,
80180 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 73 -1, 0, 0, s
80190 74 72 66 74 69 6d 65 46 75 6e 63 20 20 29 2c 0a trftimeFunc ),.
801a0 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 75 72 FUNCTION(cur
801b0 72 65 6e 74 5f 74 69 6d 65 2c 20 20 20 20 20 20 rent_time,
801c0 30 2c 20 30 2c 20 30 2c 20 63 74 69 6d 65 46 75 0, 0, 0, ctimeFu
801d0 6e 63 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 nc ),. FU
801e0 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 74 NCTION(current_t
801f0 69 6d 65 73 74 61 6d 70 2c 20 30 2c 20 30 2c 20 imestamp, 0, 0,
80200 30 2c 20 63 74 69 6d 65 73 74 61 6d 70 46 75 6e 0, ctimestampFun
80210 63 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e c),. FUNCTION
80220 28 63 75 72 72 65 6e 74 5f 64 61 74 65 2c 20 20 (current_date,
80230 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 63 64 61 0, 0, 0, cda
80240 74 65 46 75 6e 63 20 20 20 20 20 29 2c 0a 23 65 teFunc ),.#e
80250 6c 73 65 0a 20 20 20 20 53 54 52 5f 46 55 4e 43 lse. STR_FUNC
80260 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 74 69 6d TION(current_tim
80270 65 2c 20 20 20 20 20 20 30 2c 20 22 25 48 3a 25 e, 0, "%H:%
80280 4d 3a 25 53 22 2c 20 20 20 20 20 20 20 20 20 20 M:%S",
80290 30 2c 20 63 75 72 72 65 6e 74 54 69 6d 65 46 75 0, currentTimeFu
802a0 6e 63 29 2c 0a 20 20 20 20 53 54 52 5f 46 55 4e nc),. STR_FUN
802b0 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 74 69 CTION(current_ti
802c0 6d 65 73 74 61 6d 70 2c 20 30 2c 20 22 25 59 2d mestamp, 0, "%Y-
802d0 25 6d 2d 25 64 22 2c 20 20 20 20 20 20 20 20 20 %m-%d",
802e0 20 30 2c 20 63 75 72 72 65 6e 74 54 69 6d 65 46 0, currentTimeF
802f0 75 6e 63 29 2c 0a 20 20 20 20 53 54 52 5f 46 55 unc),. STR_FU
80300 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 64 NCTION(current_d
80310 61 74 65 2c 20 20 20 20 20 20 30 2c 20 22 25 59 ate, 0, "%Y
80320 2d 25 6d 2d 25 64 20 25 48 3a 25 4d 3a 25 53 22 -%m-%d %H:%M:%S"
80330 2c 20 30 2c 20 63 75 72 72 65 6e 74 54 69 6d 65 , 0, currentTime
80340 46 75 6e 63 29 2c 0a 23 65 6e 64 69 66 0a 20 20 Func),.#endif.
80350 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 46 75 };. int i;. Fu
80360 6e 63 44 65 66 48 61 73 68 20 2a 70 48 61 73 68 ncDefHash *pHash
80370 20 3d 20 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44 = &GLOBAL(FuncD
80380 65 66 48 61 73 68 2c 20 73 71 6c 69 74 65 33 47 efHash, sqlite3G
80390 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 29 3b lobalFunctions);
803a0 0a 20 20 46 75 6e 63 44 65 66 20 2a 61 46 75 6e . FuncDef *aFun
803b0 63 20 3d 20 28 46 75 6e 63 44 65 66 2a 29 26 47 c = (FuncDef*)&G
803c0 4c 4f 42 41 4c 28 46 75 6e 63 44 65 66 2c 20 61 LOBAL(FuncDef, a
803d0 44 61 74 65 54 69 6d 65 46 75 6e 63 73 29 3b 0a DateTimeFuncs);.
803e0 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 . for(i=0; i<Ar
803f0 72 61 79 53 69 7a 65 28 61 44 61 74 65 54 69 6d raySize(aDateTim
80400 65 46 75 6e 63 73 29 3b 20 69 2b 2b 29 7b 0a 20 eFuncs); i++){.
80410 20 20 20 73 71 6c 69 74 65 33 46 75 6e 63 44 65 sqlite3FuncDe
80420 66 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 26 fInsert(pHash, &
80430 61 46 75 6e 63 5b 69 5d 29 3b 0a 20 20 7d 0a 7d aFunc[i]);. }.}
80440 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
80450 2a 20 45 6e 64 20 6f 66 20 64 61 74 65 2e 63 20 * End of date.c
80460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80490 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
804a0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 2e * Begin file os.
804b0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
804c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
804d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
804e0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20 4e 6f 76 /./*.** 2005 Nov
804f0 65 6d 62 65 72 20 32 39 0a 2a 2a 0a 2a 2a 20 54 ember 29.**.** T
80500 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
80510 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
80520 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
80530 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
80540 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
80550 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
80560 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
80570 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
80580 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
80590 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
805a0 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
805b0 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
805c0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
805d0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
805e0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
805f0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
80600 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
80610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
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 0a 2a 2a 0a ************.**.
80660 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
80670 74 61 69 6e 73 20 4f 53 20 69 6e 74 65 72 66 61 tains OS interfa
80680 63 65 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 ce code that is
80690 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 0a 2a 2a common to all.**
806a0 20 61 72 63 68 69 74 65 63 74 75 72 65 73 2e 0a architectures..
806b0 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 2e 63 2c **.** $Id: os.c,
806c0 76 20 31 2e 31 32 37 20 32 30 30 39 2f 30 37 2f v 1.127 2009/07/
806d0 32 37 20 31 31 3a 34 31 3a 32 31 20 64 61 6e 69 27 11:41:21 dani
806e0 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f elk1977 Exp $.*/
806f0 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 .#define _SQLITE
80700 5f 4f 53 5f 43 5f 20 31 0a 23 75 6e 64 65 66 20 _OS_C_ 1.#undef
80710 5f 53 51 4c 49 54 45 5f 4f 53 5f 43 5f 0a 0a 2f _SQLITE_OS_C_../
80720 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 *.** The default
80730 20 53 51 4c 69 74 65 20 73 71 6c 69 74 65 33 5f SQLite sqlite3_
80740 76 66 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 vfs implementati
80750 6f 6e 73 20 64 6f 20 6e 6f 74 20 61 6c 6c 6f 63 ons do not alloc
80760 61 74 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 28 61 ate.** memory (a
80770 63 74 75 61 6c 6c 79 2c 20 6f 73 5f 75 6e 69 78 ctually, os_unix
80780 2e 63 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 73 .c allocates a s
80790 6d 61 6c 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 6d mall amount of m
807a0 65 6d 6f 72 79 0a 2a 2a 20 66 72 6f 6d 20 77 69 emory.** from wi
807b0 74 68 69 6e 20 4f 73 4f 70 65 6e 28 29 29 2c 20 thin OsOpen()),
807c0 62 75 74 20 73 6f 6d 65 20 74 68 69 72 64 2d 70 but some third-p
807d0 61 72 74 79 20 69 6d 70 6c 65 6d 65 6e 74 61 74 arty implementat
807e0 69 6f 6e 73 20 6d 61 79 2e 0a 2a 2a 20 53 6f 20 ions may..** So
807f0 77 65 20 74 65 73 74 20 74 68 65 20 65 66 66 65 we test the effe
80800 63 74 73 20 6f 66 20 61 20 6d 61 6c 6c 6f 63 28 cts of a malloc(
80810 29 20 66 61 69 6c 69 6e 67 20 61 6e 64 20 74 68 ) failing and th
80820 65 20 73 71 6c 69 74 65 33 4f 73 58 58 58 28 29 e sqlite3OsXXX()
80830 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 .** function ret
80840 75 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f 49 4f urning SQLITE_IO
80850 45 52 52 5f 4e 4f 4d 45 4d 20 75 73 69 6e 67 20 ERR_NOMEM using
80860 74 68 65 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 the DO_OS_MALLOC
80870 5f 54 45 53 54 20 6d 61 63 72 6f 2e 0a 2a 2a 0a _TEST macro..**.
80880 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
80890 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 69 functions are i
808a0 6e 73 74 72 75 6d 65 6e 74 65 64 20 66 6f 72 20 nstrumented for
808b0 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 malloc() failure
808c0 20 0a 2a 2a 20 74 65 73 74 69 6e 67 3a 0a 2a 2a .** testing:.**
808d0 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f .** sqlite3O
808e0 73 4f 70 65 6e 28 29 0a 2a 2a 20 20 20 20 20 73 sOpen().** s
808f0 71 6c 69 74 65 33 4f 73 52 65 61 64 28 29 0a 2a qlite3OsRead().*
80900 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 57 * sqlite3OsW
80910 72 69 74 65 28 29 0a 2a 2a 20 20 20 20 20 73 71 rite().** sq
80920 6c 69 74 65 33 4f 73 53 79 6e 63 28 29 0a 2a 2a lite3OsSync().**
80930 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4c 6f sqlite3OsLo
80940 63 6b 28 29 0a 2a 2a 0a 2a 2f 0a 23 69 66 20 64 ck().**.*/.#if d
80950 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 efined(SQLITE_TE
80960 53 54 29 20 26 26 20 28 53 51 4c 49 54 45 5f 4f ST) && (SQLITE_O
80970 53 5f 57 49 4e 3d 3d 30 29 0a 20 20 23 64 65 66 S_WIN==0). #def
80980 69 6e 65 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 ine DO_OS_MALLOC
80990 5f 54 45 53 54 28 78 29 20 69 66 20 28 21 78 20 _TEST(x) if (!x
809a0 7c 7c 20 21 73 71 6c 69 74 65 33 49 73 4d 65 6d || !sqlite3IsMem
809b0 4a 6f 75 72 6e 61 6c 28 78 29 29 20 7b 20 20 20 Journal(x)) {
809c0 20 20 5c 0a 20 20 20 20 76 6f 69 64 20 2a 70 54 \. void *pT
809d0 73 74 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 stAlloc = sqlite
809e0 33 4d 61 6c 6c 6f 63 28 31 30 29 3b 20 20 20 20 3Malloc(10);
809f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
80a00 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69 \. i
80a10 66 20 28 21 70 54 73 74 41 6c 6c 6f 63 29 20 72 f (!pTstAlloc) r
80a20 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
80a30 52 52 5f 4e 4f 4d 45 4d 3b 20 20 20 20 20 20 20 RR_NOMEM;
80a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
80a50 5c 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 \. sqlite3_fr
80a60 65 65 28 70 54 73 74 41 6c 6c 6f 63 29 3b 20 20 ee(pTstAlloc);
80a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
80a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
80a90 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 23 65 6c \. }.#el
80aa0 73 65 0a 20 20 23 64 65 66 69 6e 65 20 44 4f 5f se. #define DO_
80ab0 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 78 OS_MALLOC_TEST(x
80ac0 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ).#endif../*.**
80ad0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f The following ro
80ae0 75 74 69 6e 65 73 20 61 72 65 20 63 6f 6e 76 65 utines are conve
80af0 6e 69 65 6e 63 65 20 77 72 61 70 70 65 72 73 20 nience wrappers
80b00 61 72 6f 75 6e 64 20 6d 65 74 68 6f 64 73 0a 2a around methods.*
80b10 2a 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 * of the sqlite3
80b20 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e 20 20 54 _file object. T
80b30 68 69 73 20 69 73 20 6d 6f 73 74 6c 79 20 6a 75 his is mostly ju
80b40 73 74 20 73 79 6e 74 61 63 74 69 63 20 73 75 67 st syntactic sug
80b50 61 72 2e 20 41 6c 6c 0a 2a 2a 20 6f 66 20 74 68 ar. All.** of th
80b60 69 73 20 77 6f 75 6c 64 20 62 65 20 63 6f 6d 70 is would be comp
80b70 6c 65 74 65 6c 79 20 61 75 74 6f 6d 61 74 69 63 letely automatic
80b80 20 69 66 20 53 51 4c 69 74 65 20 77 65 72 65 20 if SQLite were
80b90 63 6f 64 65 64 20 75 73 69 6e 67 0a 2a 2a 20 43 coded using.** C
80ba0 2b 2b 20 69 6e 73 74 65 61 64 20 6f 66 20 70 6c ++ instead of pl
80bb0 61 69 6e 20 6f 6c 64 20 43 2e 0a 2a 2f 0a 53 51 ain old C..*/.SQ
80bc0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
80bd0 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 sqlite3OsClose(
80be0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 49 sqlite3_file *pI
80bf0 64 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 d){. int rc = S
80c00 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 QLITE_OK;. if(
80c10 70 49 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b pId->pMethods ){
80c20 0a 20 20 20 20 72 63 20 3d 20 70 49 64 2d 3e 70 . rc = pId->p
80c30 4d 65 74 68 6f 64 73 2d 3e 78 43 6c 6f 73 65 28 Methods->xClose(
80c40 70 49 64 29 3b 0a 20 20 20 20 70 49 64 2d 3e 70 pId);. pId->p
80c50 4d 65 74 68 6f 64 73 20 3d 20 30 3b 0a 20 20 7d Methods = 0;. }
80c60 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
80c70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
80c80 6e 74 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 nt sqlite3OsRead
80c90 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
80ca0 64 2c 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 d, void *pBuf, i
80cb0 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f 66 66 73 nt amt, i64 offs
80cc0 65 74 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c et){. DO_OS_MAL
80cd0 4c 4f 43 5f 54 45 53 54 28 69 64 29 3b 0a 20 20 LOC_TEST(id);.
80ce0 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 return id->pMeth
80cf0 6f 64 73 2d 3e 78 52 65 61 64 28 69 64 2c 20 70 ods->xRead(id, p
80d00 42 75 66 2c 20 61 6d 74 2c 20 6f 66 66 73 65 74 Buf, amt, offset
80d10 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 );.}.SQLITE_PRIV
80d20 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
80d30 73 57 72 69 74 65 28 73 71 6c 69 74 65 33 5f 66 sWrite(sqlite3_f
80d40 69 6c 65 20 2a 69 64 2c 20 63 6f 6e 73 74 20 76 ile *id, const v
80d50 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 61 oid *pBuf, int a
80d60 6d 74 2c 20 69 36 34 20 6f 66 66 73 65 74 29 7b mt, i64 offset){
80d70 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f . DO_OS_MALLOC_
80d80 54 45 53 54 28 69 64 29 3b 0a 20 20 72 65 74 75 TEST(id);. retu
80d90 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d rn id->pMethods-
80da0 3e 78 57 72 69 74 65 28 69 64 2c 20 70 42 75 66 >xWrite(id, pBuf
80db0 2c 20 61 6d 74 2c 20 6f 66 66 73 65 74 29 3b 0a , amt, offset);.
80dc0 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
80dd0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 54 72 int sqlite3OsTr
80de0 75 6e 63 61 74 65 28 73 71 6c 69 74 65 33 5f 66 uncate(sqlite3_f
80df0 69 6c 65 20 2a 69 64 2c 20 69 36 34 20 73 69 7a ile *id, i64 siz
80e00 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d e){. return id-
80e10 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 54 72 75 6e >pMethods->xTrun
80e20 63 61 74 65 28 69 64 2c 20 73 69 7a 65 29 3b 0a cate(id, size);.
80e30 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
80e40 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 79 int sqlite3OsSy
80e50 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 nc(sqlite3_file
80e60 2a 69 64 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b *id, int flags){
80e70 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f . DO_OS_MALLOC_
80e80 54 45 53 54 28 69 64 29 3b 0a 20 20 72 65 74 75 TEST(id);. retu
80e90 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d rn id->pMethods-
80ea0 3e 78 53 79 6e 63 28 69 64 2c 20 66 6c 61 67 73 >xSync(id, flags
80eb0 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 );.}.SQLITE_PRIV
80ec0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
80ed0 73 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 sFileSize(sqlite
80ee0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 34 20 3_file *id, i64
80ef0 2a 70 53 69 7a 65 29 7b 0a 20 20 44 4f 5f 4f 53 *pSize){. DO_OS
80f00 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 69 64 29 _MALLOC_TEST(id)
80f10 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 ;. return id->p
80f20 4d 65 74 68 6f 64 73 2d 3e 78 46 69 6c 65 53 69 Methods->xFileSi
80f30 7a 65 28 69 64 2c 20 70 53 69 7a 65 29 3b 0a 7d ze(id, pSize);.}
80f40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
80f50 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 int sqlite3OsLoc
80f60 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a k(sqlite3_file *
80f70 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 54 79 70 65 id, int lockType
80f80 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f ){. DO_OS_MALLO
80f90 43 5f 54 45 53 54 28 69 64 29 3b 0a 20 20 72 65 C_TEST(id);. re
80fa0 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 turn id->pMethod
80fb0 73 2d 3e 78 4c 6f 63 6b 28 69 64 2c 20 6c 6f 63 s->xLock(id, loc
80fc0 6b 54 79 70 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 kType);.}.SQLITE
80fd0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
80fe0 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 73 71 6c ite3OsUnlock(sql
80ff0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
81000 6e 74 20 6c 6f 63 6b 54 79 70 65 29 7b 0a 20 20 nt lockType){.
81010 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 return id->pMeth
81020 6f 64 73 2d 3e 78 55 6e 6c 6f 63 6b 28 69 64 2c ods->xUnlock(id,
81030 20 6c 6f 63 6b 54 79 70 65 29 3b 0a 7d 0a 53 51 lockType);.}.SQ
81040 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
81050 20 73 71 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 sqlite3OsCheckR
81060 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 eservedLock(sqli
81070 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e te3_file *id, in
81080 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 44 t *pResOut){. D
81090 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 O_OS_MALLOC_TEST
810a0 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 (id);. return i
810b0 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 43 68 d->pMethods->xCh
810c0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 eckReservedLock(
810d0 69 64 2c 20 70 52 65 73 4f 75 74 29 3b 0a 7d 0a id, pResOut);.}.
810e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
810f0 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 nt sqlite3OsFile
81100 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f Control(sqlite3_
81110 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f 70 file *id, int op
81120 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 , void *pArg){.
81130 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 return id->pMet
81140 68 6f 64 73 2d 3e 78 46 69 6c 65 43 6f 6e 74 72 hods->xFileContr
81150 6f 6c 28 69 64 2c 20 6f 70 2c 20 70 41 72 67 29 ol(id, op, pArg)
81160 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
81170 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
81180 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c 69 74 SectorSize(sqlit
81190 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 e3_file *id){.
811a0 69 6e 74 20 28 2a 78 53 65 63 74 6f 72 53 69 7a int (*xSectorSiz
811b0 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a e)(sqlite3_file*
811c0 29 20 3d 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 ) = id->pMethods
811d0 2d 3e 78 53 65 63 74 6f 72 53 69 7a 65 3b 0a 20 ->xSectorSize;.
811e0 20 72 65 74 75 72 6e 20 28 78 53 65 63 74 6f 72 return (xSector
811f0 53 69 7a 65 20 3f 20 78 53 65 63 74 6f 72 53 69 Size ? xSectorSi
81200 7a 65 28 69 64 29 20 3a 20 53 51 4c 49 54 45 5f ze(id) : SQLITE_
81210 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 DEFAULT_SECTOR_S
81220 49 5a 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 IZE);.}.SQLITE_P
81230 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
81240 65 33 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 e3OsDeviceCharac
81250 74 65 72 69 73 74 69 63 73 28 73 71 6c 69 74 65 teristics(sqlite
81260 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 72 3_file *id){. r
81270 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f eturn id->pMetho
81280 64 73 2d 3e 78 44 65 76 69 63 65 43 68 61 72 61 ds->xDeviceChara
81290 63 74 65 72 69 73 74 69 63 73 28 69 64 29 3b 0a cteristics(id);.
812a0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 }../*.** The nex
812b0 74 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 74 69 t group of routi
812c0 6e 65 73 20 61 72 65 20 63 6f 6e 76 65 6e 69 65 nes are convenie
812d0 6e 63 65 20 77 72 61 70 70 65 72 73 20 61 72 6f nce wrappers aro
812e0 75 6e 64 20 74 68 65 0a 2a 2a 20 56 46 53 20 6d und the.** VFS m
812f0 65 74 68 6f 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 ethods..*/.SQLIT
81300 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
81310 6c 69 74 65 33 4f 73 4f 70 65 6e 28 0a 20 20 73 lite3OsOpen(. s
81320 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
81330 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 , . const char
81340 2a 7a 50 61 74 68 2c 20 0a 20 20 73 71 6c 69 74 *zPath, . sqlit
81350 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c 20 e3_file *pFile,
81360 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 0a 20 . int flags, .
81370 20 69 6e 74 20 2a 70 46 6c 61 67 73 4f 75 74 0a int *pFlagsOut.
81380 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 44 ){. int rc;. D
81390 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 O_OS_MALLOC_TEST
813a0 28 30 29 3b 0a 20 20 2f 2a 20 30 78 37 66 31 66 (0);. /* 0x7f1f
813b0 20 69 73 20 61 20 6d 61 73 6b 20 6f 66 20 53 51 is a mask of SQ
813c0 4c 49 54 45 5f 4f 50 45 4e 5f 20 66 6c 61 67 73 LITE_OPEN_ flags
813d0 20 74 68 61 74 20 61 72 65 20 76 61 6c 69 64 20 that are valid
813e0 74 6f 20 62 65 20 70 61 73 73 65 64 0a 20 20 2a to be passed. *
813f0 2a 20 64 6f 77 6e 20 69 6e 74 6f 20 74 68 65 20 * down into the
81400 56 46 53 20 6c 61 79 65 72 2e 20 20 53 6f 6d 65 VFS layer. Some
81410 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 20 66 6c SQLITE_OPEN_ fl
81420 61 67 73 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 ags (for example
81430 2c 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 4f 50 ,. ** SQLITE_OP
81440 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 20 6f 72 20 EN_FULLMUTEX or
81450 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41 52 SQLITE_OPEN_SHAR
81460 45 44 43 41 43 48 45 29 20 61 72 65 20 62 6c 6f EDCACHE) are blo
81470 63 6b 65 64 20 62 65 66 6f 72 65 0a 20 20 2a 2a cked before. **
81480 20 72 65 61 63 68 69 6e 67 20 74 68 65 20 56 46 reaching the VF
81490 53 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 70 56 66 S. */. rc = pVf
814a0 73 2d 3e 78 4f 70 65 6e 28 70 56 66 73 2c 20 7a s->xOpen(pVfs, z
814b0 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 66 6c 61 Path, pFile, fla
814c0 67 73 20 26 20 30 78 37 66 31 66 2c 20 70 46 6c gs & 0x7f1f, pFl
814d0 61 67 73 4f 75 74 29 3b 0a 20 20 61 73 73 65 72 agsOut);. asser
814e0 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b t( rc==SQLITE_OK
814f0 20 7c 7c 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 || pFile->pMeth
81500 6f 64 73 3d 3d 30 20 29 3b 0a 20 20 72 65 74 75 ods==0 );. retu
81510 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f rn rc;.}.SQLITE_
81520 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
81530 74 65 33 4f 73 44 65 6c 65 74 65 28 73 71 6c 69 te3OsDelete(sqli
81540 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 63 te3_vfs *pVfs, c
81550 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 onst char *zPath
81560 2c 20 69 6e 74 20 64 69 72 53 79 6e 63 29 7b 0a , int dirSync){.
81570 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 return pVfs->x
81580 44 65 6c 65 74 65 28 70 56 66 73 2c 20 7a 50 61 Delete(pVfs, zPa
81590 74 68 2c 20 64 69 72 53 79 6e 63 29 3b 0a 7d 0a th, dirSync);.}.
815a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
815b0 6e 74 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 nt sqlite3OsAcce
815c0 73 73 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 ss(. sqlite3_vf
815d0 73 20 2a 70 56 66 73 2c 20 0a 20 20 63 6f 6e 73 s *pVfs, . cons
815e0 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 0a t char *zPath, .
815f0 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 0a 20 20 int flags, .
81600 69 6e 74 20 2a 70 52 65 73 4f 75 74 0a 29 7b 0a int *pResOut.){.
81610 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 DO_OS_MALLOC_T
81620 45 53 54 28 30 29 3b 0a 20 20 72 65 74 75 72 6e EST(0);. return
81630 20 70 56 66 73 2d 3e 78 41 63 63 65 73 73 28 70 pVfs->xAccess(p
81640 56 66 73 2c 20 7a 50 61 74 68 2c 20 66 6c 61 67 Vfs, zPath, flag
81650 73 2c 20 70 52 65 73 4f 75 74 29 3b 0a 7d 0a 53 s, pResOut);.}.S
81660 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
81670 74 20 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50 t sqlite3OsFullP
81680 61 74 68 6e 61 6d 65 28 0a 20 20 73 71 6c 69 74 athname(. sqlit
81690 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 e3_vfs *pVfs, .
816a0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 const char *zPa
816b0 74 68 2c 20 0a 20 20 69 6e 74 20 6e 50 61 74 68 th, . int nPath
816c0 4f 75 74 2c 20 0a 20 20 63 68 61 72 20 2a 7a 50 Out, . char *zP
816d0 61 74 68 4f 75 74 0a 29 7b 0a 20 20 72 65 74 75 athOut.){. retu
816e0 72 6e 20 70 56 66 73 2d 3e 78 46 75 6c 6c 50 61 rn pVfs->xFullPa
816f0 74 68 6e 61 6d 65 28 70 56 66 73 2c 20 7a 50 61 thname(pVfs, zPa
81700 74 68 2c 20 6e 50 61 74 68 4f 75 74 2c 20 7a 50 th, nPathOut, zP
81710 61 74 68 4f 75 74 29 3b 0a 7d 0a 23 69 66 6e 64 athOut);.}.#ifnd
81720 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c ef SQLITE_OMIT_L
81730 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 53 51 OAD_EXTENSION.SQ
81740 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
81750 64 20 2a 73 71 6c 69 74 65 33 4f 73 44 6c 4f 70 d *sqlite3OsDlOp
81760 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a en(sqlite3_vfs *
81770 70 56 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 pVfs, const char
81780 20 2a 7a 50 61 74 68 29 7b 0a 20 20 72 65 74 75 *zPath){. retu
81790 72 6e 20 70 56 66 73 2d 3e 78 44 6c 4f 70 65 6e rn pVfs->xDlOpen
817a0 28 70 56 66 73 2c 20 7a 50 61 74 68 29 3b 0a 7d (pVfs, zPath);.}
817b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
817c0 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 73 44 6c void sqlite3OsDl
817d0 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 Error(sqlite3_vf
817e0 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 79 s *pVfs, int nBy
817f0 74 65 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f 75 te, char *zBufOu
81800 74 29 7b 0a 20 20 70 56 66 73 2d 3e 78 44 6c 45 t){. pVfs->xDlE
81810 72 72 6f 72 28 70 56 66 73 2c 20 6e 42 79 74 65 rror(pVfs, nByte
81820 2c 20 7a 42 75 66 4f 75 74 29 3b 0a 7d 0a 53 51 , zBufOut);.}.SQ
81830 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
81840 64 20 28 2a 73 71 6c 69 74 65 33 4f 73 44 6c 53 d (*sqlite3OsDlS
81850 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a ym(sqlite3_vfs *
81860 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 64 6c pVfs, void *pHdl
81870 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a e, const char *z
81880 53 79 6d 29 29 28 76 6f 69 64 29 7b 0a 20 20 72 Sym))(void){. r
81890 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 44 6c 53 eturn pVfs->xDlS
818a0 79 6d 28 70 56 66 73 2c 20 70 48 64 6c 65 2c 20 ym(pVfs, pHdle,
818b0 7a 53 79 6d 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f zSym);.}.SQLITE_
818c0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
818d0 69 74 65 33 4f 73 44 6c 43 6c 6f 73 65 28 73 71 ite3OsDlClose(sq
818e0 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
818f0 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65 29 7b void *pHandle){
81900 0a 20 20 70 56 66 73 2d 3e 78 44 6c 43 6c 6f 73 . pVfs->xDlClos
81910 65 28 70 56 66 73 2c 20 70 48 61 6e 64 6c 65 29 e(pVfs, pHandle)
81920 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
81930 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 LITE_OMIT_LOAD_E
81940 58 54 45 4e 53 49 4f 4e 20 2a 2f 0a 53 51 4c 49 XTENSION */.SQLI
81950 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
81960 71 6c 69 74 65 33 4f 73 52 61 6e 64 6f 6d 6e 65 qlite3OsRandomne
81970 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a ss(sqlite3_vfs *
81980 70 56 66 73 2c 20 69 6e 74 20 6e 42 79 74 65 2c pVfs, int nByte,
81990 20 63 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b char *zBufOut){
819a0 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e . return pVfs->
819b0 78 52 61 6e 64 6f 6d 6e 65 73 73 28 70 56 66 73 xRandomness(pVfs
819c0 2c 20 6e 42 79 74 65 2c 20 7a 42 75 66 4f 75 74 , nByte, zBufOut
819d0 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 );.}.SQLITE_PRIV
819e0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
819f0 73 53 6c 65 65 70 28 73 71 6c 69 74 65 33 5f 76 sSleep(sqlite3_v
81a00 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 4d fs *pVfs, int nM
81a10 69 63 72 6f 29 7b 0a 20 20 72 65 74 75 72 6e 20 icro){. return
81a20 70 56 66 73 2d 3e 78 53 6c 65 65 70 28 70 56 66 pVfs->xSleep(pVf
81a30 73 2c 20 6e 4d 69 63 72 6f 29 3b 0a 7d 0a 53 51 s, nMicro);.}.SQ
81a40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
81a50 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e sqlite3OsCurren
81a60 74 54 69 6d 65 28 73 71 6c 69 74 65 33 5f 76 66 tTime(sqlite3_vf
81a70 73 20 2a 70 56 66 73 2c 20 64 6f 75 62 6c 65 20 s *pVfs, double
81a80 2a 70 54 69 6d 65 4f 75 74 29 7b 0a 20 20 72 65 *pTimeOut){. re
81a90 74 75 72 6e 20 70 56 66 73 2d 3e 78 43 75 72 72 turn pVfs->xCurr
81aa0 65 6e 74 54 69 6d 65 28 70 56 66 73 2c 20 70 54 entTime(pVfs, pT
81ab0 69 6d 65 4f 75 74 29 3b 0a 7d 0a 0a 53 51 4c 49 imeOut);.}..SQLI
81ac0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
81ad0 71 6c 69 74 65 33 4f 73 4f 70 65 6e 4d 61 6c 6c qlite3OsOpenMall
81ae0 6f 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 oc(. sqlite3_vf
81af0 73 20 2a 70 56 66 73 2c 20 0a 20 20 63 6f 6e 73 s *pVfs, . cons
81b00 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 2c 20 0a t char *zFile, .
81b10 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a sqlite3_file *
81b20 2a 70 70 46 69 6c 65 2c 20 0a 20 20 69 6e 74 20 *ppFile, . int
81b30 66 6c 61 67 73 2c 0a 20 20 69 6e 74 20 2a 70 4f flags,. int *pO
81b40 75 74 46 6c 61 67 73 0a 29 7b 0a 20 20 69 6e 74 utFlags.){. int
81b50 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d rc = SQLITE_NOM
81b60 45 4d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 EM;. sqlite3_fi
81b70 6c 65 20 2a 70 46 69 6c 65 3b 0a 20 20 70 46 69 le *pFile;. pFi
81b80 6c 65 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 69 le = (sqlite3_fi
81b90 6c 65 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c le *)sqlite3Mall
81ba0 6f 63 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c oc(pVfs->szOsFil
81bb0 65 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 20 e);. if( pFile
81bc0 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 ){. rc = sqli
81bd0 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 te3OsOpen(pVfs,
81be0 7a 46 69 6c 65 2c 20 70 46 69 6c 65 2c 20 66 6c zFile, pFile, fl
81bf0 61 67 73 2c 20 70 4f 75 74 46 6c 61 67 73 29 3b ags, pOutFlags);
81c00 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
81c10 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
81c20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69 sqlite3_free(pFi
81c30 6c 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a le);. }else{.
81c40 20 20 20 20 20 20 2a 70 70 46 69 6c 65 20 3d 20 *ppFile =
81c50 70 46 69 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 7d pFile;. }. }
81c60 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
81c70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
81c80 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 nt sqlite3OsClos
81c90 65 46 72 65 65 28 73 71 6c 69 74 65 33 5f 66 69 eFree(sqlite3_fi
81ca0 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e le *pFile){. in
81cb0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
81cc0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c ;. assert( pFil
81cd0 65 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 e );. rc = sqli
81ce0 74 65 33 4f 73 43 6c 6f 73 65 28 70 46 69 6c 65 te3OsClose(pFile
81cf0 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 );. sqlite3_fre
81d00 65 28 70 46 69 6c 65 29 3b 0a 20 20 72 65 74 75 e(pFile);. retu
81d10 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
81d20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
81d30 20 61 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e a wrapper aroun
81d40 64 20 74 68 65 20 4f 53 20 73 70 65 63 69 66 69 d the OS specifi
81d50 63 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e c implementation
81d60 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f of.** sqlite3_o
81d70 73 5f 69 6e 69 74 28 29 2e 20 54 68 65 20 70 75 s_init(). The pu
81d80 72 70 6f 73 65 20 6f 66 20 74 68 65 20 77 72 61 rpose of the wra
81d90 70 70 65 72 20 69 73 20 74 6f 20 70 72 6f 76 69 pper is to provi
81da0 64 65 20 74 68 65 0a 2a 2a 20 61 62 69 6c 69 74 de the.** abilit
81db0 79 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 20 y to simulate a
81dc0 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 2c 20 malloc failure,
81dd0 73 6f 20 74 68 61 74 20 74 68 65 20 68 61 6e 64 so that the hand
81de0 6c 69 6e 67 20 6f 66 20 61 6e 0a 2a 2a 20 65 72 ling of an.** er
81df0 72 6f 72 20 69 6e 20 73 71 6c 69 74 65 33 5f 6f ror in sqlite3_o
81e00 73 5f 69 6e 69 74 28 29 20 62 79 20 74 68 65 20 s_init() by the
81e10 75 70 70 65 72 20 6c 61 79 65 72 73 20 63 61 6e upper layers can
81e20 20 62 65 20 74 65 73 74 65 64 2e 0a 2a 2f 0a 53 be tested..*/.S
81e30 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
81e40 74 20 73 71 6c 69 74 65 33 4f 73 49 6e 69 74 28 t sqlite3OsInit(
81e50 76 6f 69 64 29 7b 0a 20 20 76 6f 69 64 20 2a 70 void){. void *p
81e60 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f = sqlite3_mallo
81e70 63 28 31 30 29 3b 0a 20 20 69 66 28 20 70 3d 3d c(10);. if( p==
81e80 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 0 ) return SQLIT
81e90 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 73 71 6c 69 74 E_NOMEM;. sqlit
81ea0 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 72 65 e3_free(p);. re
81eb0 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 6f 73 5f turn sqlite3_os_
81ec0 69 6e 69 74 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a init();.}../*.**
81ed0 20 54 68 65 20 6c 69 73 74 20 6f 66 20 61 6c 6c The list of all
81ee0 20 72 65 67 69 73 74 65 72 65 64 20 56 46 53 20 registered VFS
81ef0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e implementations.
81f00 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 .*/.static sqlit
81f10 65 33 5f 76 66 73 20 2a 20 53 51 4c 49 54 45 5f e3_vfs * SQLITE_
81f20 57 53 44 20 76 66 73 4c 69 73 74 20 3d 20 30 3b WSD vfsList = 0;
81f30 0a 23 64 65 66 69 6e 65 20 76 66 73 4c 69 73 74 .#define vfsList
81f40 20 47 4c 4f 42 41 4c 28 73 71 6c 69 74 65 33 5f GLOBAL(sqlite3_
81f50 76 66 73 20 2a 2c 20 76 66 73 4c 69 73 74 29 0a vfs *, vfsList).
81f60 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 61 20 ./*.** Locate a
81f70 56 46 53 20 62 79 20 6e 61 6d 65 2e 20 20 49 66 VFS by name. If
81f80 20 6e 6f 20 6e 61 6d 65 20 69 73 20 67 69 76 65 no name is give
81f90 6e 2c 20 73 69 6d 70 6c 79 20 72 65 74 75 72 6e n, simply return
81fa0 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 56 46 the.** first VF
81fb0 53 20 6f 6e 20 74 68 65 20 6c 69 73 74 2e 0a 2a S on the list..*
81fc0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c /.SQLITE_API sql
81fd0 69 74 65 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 ite3_vfs *sqlite
81fe0 33 5f 76 66 73 5f 66 69 6e 64 28 63 6f 6e 73 74 3_vfs_find(const
81ff0 20 63 68 61 72 20 2a 7a 56 66 73 29 7b 0a 20 20 char *zVfs){.
82000 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
82010 73 20 3d 20 30 3b 0a 23 69 66 20 53 51 4c 49 54 s = 0;.#if SQLIT
82020 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 73 E_THREADSAFE. s
82030 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 qlite3_mutex *mu
82040 74 65 78 3b 0a 23 65 6e 64 69 66 0a 23 69 66 6e tex;.#endif.#ifn
82050 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
82060 41 55 54 4f 49 4e 49 54 0a 20 20 69 6e 74 20 72 AUTOINIT. int r
82070 63 20 3d 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 c = sqlite3_init
82080 69 61 6c 69 7a 65 28 29 3b 0a 20 20 69 66 28 20 ialize();. if(
82090 72 63 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 rc ) return 0;.#
820a0 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 endif.#if SQLITE
820b0 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 6d 75 _THREADSAFE. mu
820c0 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 tex = sqlite3Mut
820d0 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d exAlloc(SQLITE_M
820e0 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 UTEX_STATIC_MAST
820f0 45 52 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 ER);.#endif. sq
82100 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
82110 72 28 6d 75 74 65 78 29 3b 0a 20 20 66 6f 72 28 r(mutex);. for(
82120 70 56 66 73 20 3d 20 76 66 73 4c 69 73 74 3b 20 pVfs = vfsList;
82130 70 56 66 73 3b 20 70 56 66 73 3d 70 56 66 73 2d pVfs; pVfs=pVfs-
82140 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 >pNext){. if(
82150 20 7a 56 66 73 3d 3d 30 20 29 20 62 72 65 61 6b zVfs==0 ) break
82160 3b 0a 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 ;. if( strcmp
82170 28 7a 56 66 73 2c 20 70 56 66 73 2d 3e 7a 4e 61 (zVfs, pVfs->zNa
82180 6d 65 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a me)==0 ) break;.
82190 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 }. sqlite3_mu
821a0 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 tex_leave(mutex)
821b0 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 3b ;. return pVfs;
821c0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b .}../*.** Unlink
821d0 20 61 20 56 46 53 20 66 72 6f 6d 20 74 68 65 20 a VFS from the
821e0 6c 69 6e 6b 65 64 20 6c 69 73 74 0a 2a 2f 0a 73 linked list.*/.s
821f0 74 61 74 69 63 20 76 6f 69 64 20 76 66 73 55 6e tatic void vfsUn
82200 6c 69 6e 6b 28 73 71 6c 69 74 65 33 5f 76 66 73 link(sqlite3_vfs
82210 20 2a 70 56 66 73 29 7b 0a 20 20 61 73 73 65 72 *pVfs){. asser
82220 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
82230 5f 68 65 6c 64 28 73 71 6c 69 74 65 33 4d 75 74 _held(sqlite3Mut
82240 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d exAlloc(SQLITE_M
82250 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 UTEX_STATIC_MAST
82260 45 52 29 29 20 29 3b 0a 20 20 69 66 28 20 70 56 ER)) );. if( pV
82270 66 73 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 fs==0 ){. /*
82280 4e 6f 2d 6f 70 20 2a 2f 0a 20 20 7d 65 6c 73 65 No-op */. }else
82290 20 69 66 28 20 76 66 73 4c 69 73 74 3d 3d 70 56 if( vfsList==pV
822a0 66 73 20 29 7b 0a 20 20 20 20 76 66 73 4c 69 73 fs ){. vfsLis
822b0 74 20 3d 20 70 56 66 73 2d 3e 70 4e 65 78 74 3b t = pVfs->pNext;
822c0 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 76 66 73 . }else if( vfs
822d0 4c 69 73 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 List ){. sqli
822e0 74 65 33 5f 76 66 73 20 2a 70 20 3d 20 76 66 73 te3_vfs *p = vfs
822f0 4c 69 73 74 3b 0a 20 20 20 20 77 68 69 6c 65 28 List;. while(
82300 20 70 2d 3e 70 4e 65 78 74 20 26 26 20 70 2d 3e p->pNext && p->
82310 70 4e 65 78 74 21 3d 70 56 66 73 20 29 7b 0a 20 pNext!=pVfs ){.
82320 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 4e 65 78 p = p->pNex
82330 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 t;. }. if(
82340 20 70 2d 3e 70 4e 65 78 74 3d 3d 70 56 66 73 20 p->pNext==pVfs
82350 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 ){. p->pNex
82360 74 20 3d 20 70 56 66 73 2d 3e 70 4e 65 78 74 3b t = pVfs->pNext;
82370 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a . }. }.}../*
82380 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 56 .** Register a V
82390 46 53 20 77 69 74 68 20 74 68 65 20 73 79 73 74 FS with the syst
823a0 65 6d 2e 20 20 49 74 20 69 73 20 68 61 72 6d 6c em. It is harml
823b0 65 73 73 20 74 6f 20 72 65 67 69 73 74 65 72 20 ess to register
823c0 74 68 65 20 73 61 6d 65 0a 2a 2a 20 56 46 53 20 the same.** VFS
823d0 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 2e 20 multiple times.
823e0 20 54 68 65 20 6e 65 77 20 56 46 53 20 62 65 63 The new VFS bec
823f0 6f 6d 65 73 20 74 68 65 20 64 65 66 61 75 6c 74 omes the default
82400 20 69 66 20 6d 61 6b 65 44 66 6c 74 20 69 73 0a if makeDflt is.
82410 2a 2a 20 74 72 75 65 2e 0a 2a 2f 0a 53 51 4c 49 ** true..*/.SQLI
82420 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
82430 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 e3_vfs_register(
82440 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
82450 73 2c 20 69 6e 74 20 6d 61 6b 65 44 66 6c 74 29 s, int makeDflt)
82460 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 {. sqlite3_mute
82470 78 20 2a 6d 75 74 65 78 20 3d 20 30 3b 0a 23 69 x *mutex = 0;.#i
82480 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
82490 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 6e 74 T_AUTOINIT. int
824a0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 69 6e rc = sqlite3_in
824b0 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20 69 66 itialize();. if
824c0 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 ( rc ) return rc
824d0 3b 0a 23 65 6e 64 69 66 0a 20 20 6d 75 74 65 78 ;.#endif. mutex
824e0 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 = sqlite3MutexA
824f0 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 lloc(SQLITE_MUTE
82500 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 X_STATIC_MASTER)
82510 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ;. sqlite3_mute
82520 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a x_enter(mutex);.
82530 20 20 76 66 73 55 6e 6c 69 6e 6b 28 70 56 66 73 vfsUnlink(pVfs
82540 29 3b 0a 20 20 69 66 28 20 6d 61 6b 65 44 66 6c );. if( makeDfl
82550 74 20 7c 7c 20 76 66 73 4c 69 73 74 3d 3d 30 20 t || vfsList==0
82560 29 7b 0a 20 20 20 20 70 56 66 73 2d 3e 70 4e 65 ){. pVfs->pNe
82570 78 74 20 3d 20 76 66 73 4c 69 73 74 3b 0a 20 20 xt = vfsList;.
82580 20 20 76 66 73 4c 69 73 74 20 3d 20 70 56 66 73 vfsList = pVfs
82590 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
825a0 56 66 73 2d 3e 70 4e 65 78 74 20 3d 20 76 66 73 Vfs->pNext = vfs
825b0 4c 69 73 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 List->pNext;.
825c0 20 76 66 73 4c 69 73 74 2d 3e 70 4e 65 78 74 20 vfsList->pNext
825d0 3d 20 70 56 66 73 3b 0a 20 20 7d 0a 20 20 61 73 = pVfs;. }. as
825e0 73 65 72 74 28 76 66 73 4c 69 73 74 29 3b 0a 20 sert(vfsList);.
825f0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
82600 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 eave(mutex);. r
82610 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
82620 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 72 65 67 69 .}../*.** Unregi
82630 73 74 65 72 20 61 20 56 46 53 20 73 6f 20 74 68 ster a VFS so th
82640 61 74 20 69 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 at it is no long
82650 65 72 20 61 63 63 65 73 73 69 62 6c 65 2e 0a 2a er accessible..*
82660 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
82670 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 sqlite3_vfs_unr
82680 65 67 69 73 74 65 72 28 73 71 6c 69 74 65 33 5f egister(sqlite3_
82690 76 66 73 20 2a 70 56 66 73 29 7b 0a 23 69 66 20 vfs *pVfs){.#if
826a0 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
826b0 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 E. sqlite3_mute
826c0 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 x *mutex = sqlit
826d0 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c e3MutexAlloc(SQL
826e0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
826f0 5f 4d 41 53 54 45 52 29 3b 0a 23 65 6e 64 69 66 _MASTER);.#endif
82700 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
82710 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 _enter(mutex);.
82720 20 76 66 73 55 6e 6c 69 6e 6b 28 70 56 66 73 29 vfsUnlink(pVfs)
82730 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ;. sqlite3_mute
82740 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a x_leave(mutex);.
82750 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
82760 4f 4b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a OK;.}../********
82770 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 ****** End of os
82780 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
82790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
827a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
827b0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
827c0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
827d0 65 20 66 61 75 6c 74 2e 63 20 2a 2a 2a 2a 2a 2a e fault.c ******
827e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
827f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82800 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
82810 38 20 4a 61 6e 20 32 32 0a 2a 2a 0a 2a 2a 20 54 8 Jan 22.**.** T
82820 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
82830 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
82840 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
82850 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
82860 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
82870 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
82880 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
82890 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
828a0 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
828b0 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
828c0 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
828d0 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
828e0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
828f0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
82900 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
82910 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
82920 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
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 2a 2a 2a 2a 2a 2a ****************
82960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82970 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 24 49 *******.**.** $I
82980 64 3a 20 66 61 75 6c 74 2e 63 2c 76 20 31 2e 31 d: fault.c,v 1.1
82990 31 20 32 30 30 38 2f 30 39 2f 30 32 20 30 30 3a 1 2008/09/02 00:
829a0 35 32 3a 35 32 20 64 72 68 20 45 78 70 20 24 0a 52:52 drh Exp $.
829b0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 */../*.** This f
829c0 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 ile contains cod
829d0 65 20 74 6f 20 73 75 70 70 6f 72 74 20 74 68 65 e to support the
829e0 20 63 6f 6e 63 65 70 74 20 6f 66 20 22 62 65 6e concept of "ben
829f0 69 67 6e 22 20 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 ign" .** malloc
82a00 66 61 69 6c 75 72 65 73 20 28 77 68 65 6e 20 74 failures (when t
82a10 68 65 20 78 4d 61 6c 6c 6f 63 28 29 20 6f 72 20 he xMalloc() or
82a20 78 52 65 61 6c 6c 6f 63 28 29 20 6d 65 74 68 6f xRealloc() metho
82a30 64 20 6f 66 20 74 68 65 0a 2a 2a 20 73 71 6c 69 d of the.** sqli
82a40 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 te3_mem_methods
82a50 73 74 72 75 63 74 75 72 65 20 66 61 69 6c 73 20 structure fails
82a60 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 62 6c to allocate a bl
82a70 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a ock of memory.**
82a80 20 61 6e 64 20 72 65 74 75 72 6e 73 20 30 29 2e and returns 0).
82a90 20 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 6d 61 6c .**.** Most mal
82aa0 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 61 72 65 loc failures are
82ab0 20 6e 6f 6e 2d 62 65 6e 69 67 6e 2e 20 41 66 74 non-benign. Aft
82ac0 65 72 20 74 68 65 79 20 6f 63 63 75 72 2c 20 53 er they occur, S
82ad0 51 4c 69 74 65 0a 2a 2a 20 61 62 61 6e 64 6f 6e QLite.** abandon
82ae0 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 6f 70 s the current op
82af0 65 72 61 74 69 6f 6e 20 61 6e 64 20 72 65 74 75 eration and retu
82b00 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 rns an error cod
82b10 65 20 28 75 73 75 61 6c 6c 79 0a 2a 2a 20 53 51 e (usually.** SQ
82b20 4c 49 54 45 5f 4e 4f 4d 45 4d 29 20 74 6f 20 74 LITE_NOMEM) to t
82b30 68 65 20 75 73 65 72 2e 20 48 6f 77 65 76 65 72 he user. However
82b40 2c 20 73 6f 6d 65 74 69 6d 65 73 20 61 20 66 61 , sometimes a fa
82b50 75 6c 74 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 ult is not neces
82b60 73 61 72 69 6c 79 0a 2a 2a 20 66 61 74 61 6c 2e sarily.** fatal.
82b70 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 For example, if
82b80 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 20 a malloc fails
82b90 77 68 69 6c 65 20 72 65 73 69 7a 69 6e 67 20 61 while resizing a
82ba0 20 68 61 73 68 20 74 61 62 6c 65 2c 20 74 68 69 hash table, thi
82bb0 73 20 0a 2a 2a 20 69 73 20 63 6f 6d 70 6c 65 74 s .** is complet
82bc0 65 6c 79 20 72 65 63 6f 76 65 72 61 62 6c 65 20 ely recoverable
82bd0 73 69 6d 70 6c 79 20 62 79 20 6e 6f 74 20 63 61 simply by not ca
82be0 72 72 79 69 6e 67 20 6f 75 74 20 74 68 65 20 72 rrying out the r
82bf0 65 73 69 7a 65 2e 20 54 68 65 20 0a 2a 2a 20 68 esize. The .** h
82c00 61 73 68 20 74 61 62 6c 65 20 77 69 6c 6c 20 63 ash table will c
82c10 6f 6e 74 69 6e 75 65 20 74 6f 20 66 75 6e 63 74 ontinue to funct
82c20 69 6f 6e 20 6e 6f 72 6d 61 6c 6c 79 2e 20 20 53 ion normally. S
82c30 6f 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 o a malloc failu
82c40 72 65 20 0a 2a 2a 20 64 75 72 69 6e 67 20 61 20 re .** during a
82c50 68 61 73 68 20 74 61 62 6c 65 20 72 65 73 69 7a hash table resiz
82c60 65 20 69 73 20 61 20 62 65 6e 69 67 6e 20 66 61 e is a benign fa
82c70 75 6c 74 2e 0a 2a 2f 0a 0a 0a 23 69 66 6e 64 65 ult..*/...#ifnde
82c80 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 f SQLITE_OMIT_BU
82c90 49 4c 54 49 4e 5f 54 45 53 54 0a 0a 2f 2a 0a 2a ILTIN_TEST../*.*
82ca0 2a 20 47 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c * Global variabl
82cb0 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 es..*/.typedef s
82cc0 74 72 75 63 74 20 42 65 6e 69 67 6e 4d 61 6c 6c truct BenignMall
82cd0 6f 63 48 6f 6f 6b 73 20 42 65 6e 69 67 6e 4d 61 ocHooks BenignMa
82ce0 6c 6c 6f 63 48 6f 6f 6b 73 3b 0a 73 74 61 74 69 llocHooks;.stati
82cf0 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 c SQLITE_WSD str
82d00 75 63 74 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 uct BenignMalloc
82d10 48 6f 6f 6b 73 20 7b 0a 20 20 76 6f 69 64 20 28 Hooks {. void (
82d20 2a 78 42 65 6e 69 67 6e 42 65 67 69 6e 29 28 76 *xBenignBegin)(v
82d30 6f 69 64 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 oid);. void (*x
82d40 42 65 6e 69 67 6e 45 6e 64 29 28 76 6f 69 64 29 BenignEnd)(void)
82d50 3b 0a 7d 20 73 71 6c 69 74 65 33 48 6f 6f 6b 73 ;.} sqlite3Hooks
82d60 20 3d 20 7b 20 30 2c 20 30 20 7d 3b 0a 0a 2f 2a = { 0, 0 };../*
82d70 20 54 68 65 20 22 77 73 64 48 6f 6f 6b 73 22 20 The "wsdHooks"
82d80 6d 61 63 72 6f 20 77 69 6c 6c 20 72 65 73 6f 6c macro will resol
82d90 76 65 20 74 6f 20 74 68 65 20 61 70 70 72 6f 70 ve to the approp
82da0 72 69 61 74 65 20 42 65 6e 69 67 6e 4d 61 6c 6c riate BenignMall
82db0 6f 63 48 6f 6f 6b 73 0a 2a 2a 20 73 74 72 75 63 ocHooks.** struc
82dc0 74 75 72 65 2e 20 20 49 66 20 77 72 69 74 61 62 ture. If writab
82dd0 6c 65 20 73 74 61 74 69 63 20 64 61 74 61 20 69 le static data i
82de0 73 20 75 6e 73 75 70 70 6f 72 74 65 64 20 6f 6e s unsupported on
82df0 20 74 68 65 20 74 61 72 67 65 74 2c 0a 2a 2a 20 the target,.**
82e00 77 65 20 68 61 76 65 20 74 6f 20 6c 6f 63 61 74 we have to locat
82e10 65 20 74 68 65 20 73 74 61 74 65 20 76 65 63 74 e the state vect
82e20 6f 72 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20 or at run-time.
82e30 20 49 6e 20 74 68 65 20 6d 6f 72 65 20 63 6f 6d In the more com
82e40 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 77 68 65 72 mon.** case wher
82e50 65 20 77 72 69 74 61 62 6c 65 20 73 74 61 74 69 e writable stati
82e60 63 20 64 61 74 61 20 69 73 20 73 75 70 70 6f 72 c data is suppor
82e70 74 65 64 2c 20 77 73 64 48 6f 6f 6b 73 20 63 61 ted, wsdHooks ca
82e80 6e 20 72 65 66 65 72 20 64 69 72 65 63 74 6c 79 n refer directly
82e90 0a 2a 2a 20 74 6f 20 74 68 65 20 22 73 71 6c 69 .** to the "sqli
82ea0 74 65 33 48 6f 6f 6b 73 22 20 73 74 61 74 65 20 te3Hooks" state
82eb0 76 65 63 74 6f 72 20 64 65 63 6c 61 72 65 64 20 vector declared
82ec0 61 62 6f 76 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 above..*/.#ifdef
82ed0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 SQLITE_OMIT_WSD
82ee0 0a 23 20 64 65 66 69 6e 65 20 77 73 64 48 6f 6f .# define wsdHoo
82ef0 6b 73 49 6e 69 74 20 5c 0a 20 20 42 65 6e 69 67 ksInit \. Benig
82f00 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 20 2a 78 20 nMallocHooks *x
82f10 3d 20 26 47 4c 4f 42 41 4c 28 42 65 6e 69 67 6e = &GLOBAL(Benign
82f20 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 2c 73 71 6c 69 MallocHooks,sqli
82f30 74 65 33 48 6f 6f 6b 73 29 0a 23 20 64 65 66 69 te3Hooks).# defi
82f40 6e 65 20 77 73 64 48 6f 6f 6b 73 20 78 5b 30 5d ne wsdHooks x[0]
82f50 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
82f60 77 73 64 48 6f 6f 6b 73 49 6e 69 74 0a 23 20 64 wsdHooksInit.# d
82f70 65 66 69 6e 65 20 77 73 64 48 6f 6f 6b 73 20 73 efine wsdHooks s
82f80 71 6c 69 74 65 33 48 6f 6f 6b 73 0a 23 65 6e 64 qlite3Hooks.#end
82f90 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 if.../*.** Regis
82fa0 74 65 72 20 68 6f 6f 6b 73 20 74 6f 20 63 61 6c ter hooks to cal
82fb0 6c 20 77 68 65 6e 20 73 71 6c 69 74 65 33 42 65 l when sqlite3Be
82fc0 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 ginBenignMalloc(
82fd0 29 20 61 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33 ) and.** sqlite3
82fe0 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 EndBenignMalloc(
82ff0 29 20 61 72 65 20 63 61 6c 6c 65 64 2c 20 72 65 ) are called, re
83000 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2f 0a 53 spectively..*/.S
83010 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
83020 69 64 20 73 71 6c 69 74 65 33 42 65 6e 69 67 6e id sqlite3Benign
83030 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 28 0a 20 20 76 MallocHooks(. v
83040 6f 69 64 20 28 2a 78 42 65 6e 69 67 6e 42 65 67 oid (*xBenignBeg
83050 69 6e 29 28 76 6f 69 64 29 2c 0a 20 20 76 6f 69 in)(void),. voi
83060 64 20 28 2a 78 42 65 6e 69 67 6e 45 6e 64 29 28 d (*xBenignEnd)(
83070 76 6f 69 64 29 0a 29 7b 0a 20 20 77 73 64 48 6f void).){. wsdHo
83080 6f 6b 73 49 6e 69 74 3b 0a 20 20 77 73 64 48 6f oksInit;. wsdHo
83090 6f 6b 73 2e 78 42 65 6e 69 67 6e 42 65 67 69 6e oks.xBenignBegin
830a0 20 3d 20 78 42 65 6e 69 67 6e 42 65 67 69 6e 3b = xBenignBegin;
830b0 0a 20 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e . wsdHooks.xBen
830c0 69 67 6e 45 6e 64 20 3d 20 78 42 65 6e 69 67 6e ignEnd = xBenign
830d0 45 6e 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 End;.}../*.** Th
830e0 69 73 20 28 73 71 6c 69 74 65 33 45 6e 64 42 65 is (sqlite3EndBe
830f0 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 29 20 69 73 nignMalloc()) is
83100 20 63 61 6c 6c 65 64 20 62 79 20 53 51 4c 69 74 called by SQLit
83110 65 20 63 6f 64 65 20 74 6f 20 69 6e 64 69 63 61 e code to indica
83120 74 65 20 74 68 61 74 0a 2a 2a 20 73 75 62 73 65 te that.** subse
83130 71 75 65 6e 74 20 6d 61 6c 6c 6f 63 20 66 61 69 quent malloc fai
83140 6c 75 72 65 73 20 61 72 65 20 62 65 6e 69 67 6e lures are benign
83150 2e 20 41 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 . A call to sqli
83160 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c te3EndBenignMall
83170 6f 63 28 29 0a 2a 2a 20 69 6e 64 69 63 61 74 65 oc().** indicate
83180 73 20 74 68 61 74 20 73 75 62 73 65 71 75 65 6e s that subsequen
83190 74 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 t malloc failure
831a0 73 20 61 72 65 20 6e 6f 6e 2d 62 65 6e 69 67 6e s are non-benign
831b0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
831c0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
831d0 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f BeginBenignMallo
831e0 63 28 76 6f 69 64 29 7b 0a 20 20 77 73 64 48 6f c(void){. wsdHo
831f0 6f 6b 73 49 6e 69 74 3b 0a 20 20 69 66 28 20 77 oksInit;. if( w
83200 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 42 sdHooks.xBenignB
83210 65 67 69 6e 20 29 7b 0a 20 20 20 20 77 73 64 48 egin ){. wsdH
83220 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 42 65 67 69 ooks.xBenignBegi
83230 6e 28 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 n();. }.}.SQLIT
83240 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
83250 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d qlite3EndBenignM
83260 61 6c 6c 6f 63 28 76 6f 69 64 29 7b 0a 20 20 77 alloc(void){. w
83270 73 64 48 6f 6f 6b 73 49 6e 69 74 3b 0a 20 20 69 sdHooksInit;. i
83280 66 28 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e f( wsdHooks.xBen
83290 69 67 6e 45 6e 64 20 29 7b 0a 20 20 20 20 77 73 ignEnd ){. ws
832a0 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 45 6e dHooks.xBenignEn
832b0 64 28 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 65 6e 64 d();. }.}..#end
832c0 69 66 20 20 20 2f 2a 20 23 69 66 6e 64 65 66 20 if /* #ifndef
832d0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c SQLITE_OMIT_BUIL
832e0 54 49 4e 5f 54 45 53 54 20 2a 2f 0a 0a 2f 2a 2a TIN_TEST */../**
832f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
83300 20 6f 66 20 66 61 75 6c 74 2e 63 20 2a 2a 2a 2a of fault.c ****
83310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
83340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
83350 69 6e 20 66 69 6c 65 20 6d 65 6d 30 2e 63 20 2a in file mem0.c *
83360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
83390 2a 2a 20 32 30 30 38 20 4f 63 74 6f 62 65 72 20 ** 2008 October
833a0 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 28.**.** The aut
833b0 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
833c0 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
833d0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
833e0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
833f0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
83400 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
83410 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
83420 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
83430 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
83440 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
83450 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
83460 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
83470 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
83480 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
83490 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
834a0 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
834b0 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
834c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
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 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83500 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c *.**.** This fil
83510 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 6e 6f 2d e contains a no-
83520 6f 70 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 op memory alloca
83530 74 69 6f 6e 20 64 72 69 76 65 72 73 20 66 6f 72 tion drivers for
83540 20 75 73 65 20 77 68 65 6e 0a 2a 2a 20 53 51 4c use when.** SQL
83550 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c 4c 4f 43 20 ITE_ZERO_MALLOC
83560 69 73 20 64 65 66 69 6e 65 64 2e 20 20 54 68 65 is defined. The
83570 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 72 69 76 allocation driv
83580 65 72 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 0a ers implemented.
83590 2a 2a 20 68 65 72 65 20 61 6c 77 61 79 73 20 66 ** here always f
835a0 61 69 6c 2e 20 20 53 51 4c 69 74 65 20 77 69 6c ail. SQLite wil
835b0 6c 20 6e 6f 74 20 6f 70 65 72 61 74 65 20 77 69 l not operate wi
835c0 74 68 20 74 68 65 73 65 20 64 72 69 76 65 72 73 th these drivers
835d0 2e 20 20 54 68 65 73 65 0a 2a 2a 20 61 72 65 20 . These.** are
835e0 6d 65 72 65 6c 79 20 70 6c 61 63 65 68 6f 6c 64 merely placehold
835f0 65 72 73 2e 20 20 52 65 61 6c 20 64 72 69 76 65 ers. Real drive
83600 72 73 20 6d 75 73 74 20 62 65 20 73 75 62 73 74 rs must be subst
83610 69 74 75 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 ituted using.**
83620 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 sqlite3_config()
83630 20 62 65 66 6f 72 65 20 53 51 4c 69 74 65 20 77 before SQLite w
83640 69 6c 6c 20 6f 70 65 72 61 74 65 2e 0a 2a 2a 0a ill operate..**.
83650 2a 2a 20 24 49 64 3a 20 6d 65 6d 30 2e 63 2c 76 ** $Id: mem0.c,v
83660 20 31 2e 31 20 32 30 30 38 2f 31 30 2f 32 38 20 1.1 2008/10/28
83670 31 38 3a 35 38 3a 32 30 20 64 72 68 20 45 78 70 18:58:20 drh Exp
83680 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 $.*/../*.** Thi
83690 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 s version of the
836a0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f memory allocato
836b0 72 20 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 r is the default
836c0 2e 20 20 49 74 20 69 73 0a 2a 2a 20 75 73 65 64 . It is.** used
836d0 20 77 68 65 6e 20 6e 6f 20 6f 74 68 65 72 20 6d when no other m
836e0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 emory allocator
836f0 69 73 20 73 70 65 63 69 66 69 65 64 20 75 73 69 is specified usi
83700 6e 67 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 0a ng compile-time.
83710 2a 2a 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 23 69 ** macros..*/.#i
83720 66 64 65 66 20 53 51 4c 49 54 45 5f 5a 45 52 4f fdef SQLITE_ZERO
83730 5f 4d 41 4c 4c 4f 43 0a 0a 2f 2a 0a 2a 2a 20 4e _MALLOC../*.** N
83740 6f 2d 6f 70 20 76 65 72 73 69 6f 6e 73 20 6f 66 o-op versions of
83750 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f all memory allo
83760 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 0a cation routines.
83770 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a */.static void *
83780 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 sqlite3MemMalloc
83790 28 69 6e 74 20 6e 42 79 74 65 29 7b 20 72 65 74 (int nByte){ ret
837a0 75 72 6e 20 30 3b 20 7d 0a 73 74 61 74 69 63 20 urn 0; }.static
837b0 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 46 void sqlite3MemF
837c0 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 ree(void *pPrior
837d0 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 73 74 61 ){ return; }.sta
837e0 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 tic void *sqlite
837f0 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 76 6f 69 64 3MemRealloc(void
83800 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e 42 *pPrior, int nB
83810 79 74 65 29 7b 20 72 65 74 75 72 6e 20 30 3b 20 yte){ return 0;
83820 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c }.static int sql
83830 69 74 65 33 4d 65 6d 53 69 7a 65 28 76 6f 69 64 ite3MemSize(void
83840 20 2a 70 50 72 69 6f 72 29 7b 20 72 65 74 75 72 *pPrior){ retur
83850 6e 20 30 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e n 0; }.static in
83860 74 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e t sqlite3MemRoun
83870 64 75 70 28 69 6e 74 20 6e 29 7b 20 72 65 74 75 dup(int n){ retu
83880 72 6e 20 6e 3b 20 7d 0a 73 74 61 74 69 63 20 69 rn n; }.static i
83890 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 nt sqlite3MemIni
838a0 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 t(void *NotUsed)
838b0 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f { return SQLITE_
838c0 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 OK; }.static voi
838d0 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 d sqlite3MemShut
838e0 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73 down(void *NotUs
838f0 65 64 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 0a ed){ return; }..
83900 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
83910 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 72 ne is the only r
83920 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20 66 outine in this f
83930 69 6c 65 20 77 69 74 68 20 65 78 74 65 72 6e 61 ile with externa
83940 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a 2a 2a l linkage..**.**
83950 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 6c 6f Populate the lo
83960 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 w-level memory a
83970 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 llocation functi
83980 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a 2a on pointers in.*
83990 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 * sqlite3GlobalC
839a0 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 6f 69 onfig.m with poi
839b0 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72 6f 75 nters to the rou
839c0 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69 tines in this fi
839d0 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 le..*/.SQLITE_PR
839e0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
839f0 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28 e3MemSetDefault(
83a00 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 void){. static
83a10 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 const sqlite3_me
83a20 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66 61 75 6c m_methods defaul
83a30 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 tMethods = {.
83a40 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c sqlite3MemMall
83a50 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 oc,. sqlite3
83a60 4d 65 6d 46 72 65 65 2c 0a 20 20 20 20 20 73 71 MemFree,. sq
83a70 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 2c lite3MemRealloc,
83a80 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d . sqlite3Mem
83a90 53 69 7a 65 2c 0a 20 20 20 20 20 73 71 6c 69 74 Size,. sqlit
83aa0 65 33 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a 20 20 e3MemRoundup,.
83ab0 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 sqlite3MemIni
83ac0 74 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d t,. sqlite3M
83ad0 65 6d 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 20 emShutdown,.
83ae0 20 30 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 0. };. sqlite
83af0 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 3_config(SQLITE_
83b00 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c 20 26 CONFIG_MALLOC, &
83b10 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 29 3b defaultMethods);
83b20 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 .}..#endif /* SQ
83b30 4c 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c 4c 4f 43 LITE_ZERO_MALLOC
83b40 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../**********
83b50 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 6d 30 **** End of mem0
83b60 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
83b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83b90 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
83ba0 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
83bb0 6d 65 6d 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a mem1.c *********
83bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83be0 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 ***/./*.** 2007
83bf0 41 75 67 75 73 74 20 31 34 0a 2a 2a 0a 2a 2a 20 August 14.**.**
83c00 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
83c10 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
83c20 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
83c30 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
83c40 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
83c50 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
83c60 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
83c70 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
83c80 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
83c90 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
83ca0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
83cb0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
83cc0 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
83cd0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
83ce0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
83cf0 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
83d00 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
83d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83d50 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 ********.**.** T
83d60 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e his file contain
83d70 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f s low-level memo
83d80 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 72 ry allocation dr
83d90 69 76 65 72 73 20 66 6f 72 20 77 68 65 6e 0a 2a ivers for when.*
83da0 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 73 * SQLite will us
83db0 65 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 43 e the standard C
83dc0 2d 6c 69 62 72 61 72 79 20 6d 61 6c 6c 6f 63 2f -library malloc/
83dd0 72 65 61 6c 6c 6f 63 2f 66 72 65 65 20 69 6e 74 realloc/free int
83de0 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20 6f 62 74 erface.** to obt
83df0 61 69 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20 69 ain the memory i
83e00 74 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 t needs..**.** T
83e10 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e his file contain
83e20 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e s implementation
83e30 73 20 6f 66 20 74 68 65 20 6c 6f 77 2d 6c 65 76 s of the low-lev
83e40 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 el memory alloca
83e50 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 tion.** routines
83e60 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 specified in th
83e70 65 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 e sqlite3_mem_me
83e80 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a thods object..**
83e90 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d 31 2e 63 2c .** $Id: mem1.c,
83ea0 76 20 31 2e 33 30 20 32 30 30 39 2f 30 33 2f 32 v 1.30 2009/03/2
83eb0 33 20 30 34 3a 33 33 3a 33 33 20 64 61 6e 69 65 3 04:33:33 danie
83ec0 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a lk1977 Exp $.*/.
83ed0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 ./*.** This vers
83ee0 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 ion of the memor
83ef0 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 74 y allocator is t
83f00 68 65 20 64 65 66 61 75 6c 74 2e 20 20 49 74 20 he default. It
83f10 69 73 0a 2a 2a 20 75 73 65 64 20 77 68 65 6e 20 is.** used when
83f20 6e 6f 20 6f 74 68 65 72 20 6d 65 6d 6f 72 79 20 no other memory
83f30 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 73 70 65 allocator is spe
83f40 63 69 66 69 65 64 20 75 73 69 6e 67 20 63 6f 6d cified using com
83f50 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a 20 6d 61 63 pile-time.** mac
83f60 72 6f 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 ros..*/.#ifdef S
83f70 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c QLITE_SYSTEM_MAL
83f80 4c 4f 43 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 LOC../*.** Like
83f90 6d 61 6c 6c 6f 63 28 29 2c 20 62 75 74 20 72 65 malloc(), but re
83fa0 6d 65 6d 62 65 72 20 74 68 65 20 73 69 7a 65 20 member the size
83fb0 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f of the allocatio
83fc0 6e 0a 2a 2a 20 73 6f 20 74 68 61 74 20 77 65 20 n.** so that we
83fd0 63 61 6e 20 66 69 6e 64 20 69 74 20 6c 61 74 65 can find it late
83fe0 72 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4d r using sqlite3M
83ff0 65 6d 53 69 7a 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 emSize()..**.**
84000 46 6f 72 20 74 68 69 73 20 6c 6f 77 2d 6c 65 76 For this low-lev
84010 65 6c 20 72 6f 75 74 69 6e 65 2c 20 77 65 20 61 el routine, we a
84020 72 65 20 67 75 61 72 61 6e 74 65 65 64 20 74 68 re guaranteed th
84030 61 74 20 6e 42 79 74 65 3e 30 20 62 65 63 61 75 at nByte>0 becau
84040 73 65 0a 2a 2a 20 63 61 73 65 73 20 6f 66 20 6e se.** cases of n
84050 42 79 74 65 3c 3d 30 20 77 69 6c 6c 20 62 65 20 Byte<=0 will be
84060 69 6e 74 65 72 63 65 70 74 65 64 20 61 6e 64 20 intercepted and
84070 64 65 61 6c 74 20 77 69 74 68 20 62 79 20 68 69 dealt with by hi
84080 67 68 65 72 20 6c 65 76 65 6c 0a 2a 2a 20 72 6f gher level.** ro
84090 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 utines..*/.stati
840a0 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d c void *sqlite3M
840b0 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 emMalloc(int nBy
840c0 74 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 te){. sqlite3_i
840d0 6e 74 36 34 20 2a 70 3b 0a 20 20 61 73 73 65 72 nt64 *p;. asser
840e0 74 28 20 6e 42 79 74 65 3e 30 20 29 3b 0a 20 20 t( nByte>0 );.
840f0 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44 38 28 6e nByte = ROUND8(n
84100 42 79 74 65 29 3b 0a 20 20 70 20 3d 20 6d 61 6c Byte);. p = mal
84110 6c 6f 63 28 20 6e 42 79 74 65 2b 38 20 29 3b 0a loc( nByte+8 );.
84120 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70 if( p ){. p
84130 5b 30 5d 20 3d 20 6e 42 79 74 65 3b 0a 20 20 20 [0] = nByte;.
84140 20 70 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 p++;. }. retu
84150 72 6e 20 28 76 6f 69 64 20 2a 29 70 3b 0a 7d 0a rn (void *)p;.}.
84160 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 66 72 65 65 ./*.** Like free
84170 28 29 20 62 75 74 20 77 6f 72 6b 73 20 66 6f 72 () but works for
84180 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 6f 62 74 allocations obt
84190 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 ained from sqlit
841a0 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 29 0a 2a 2a e3MemMalloc().**
841b0 20 6f 72 20 73 71 6c 69 74 65 33 4d 65 6d 52 65 or sqlite3MemRe
841c0 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 alloc()..**.** F
841d0 6f 72 20 74 68 69 73 20 6c 6f 77 2d 6c 65 76 65 or this low-leve
841e0 6c 20 72 6f 75 74 69 6e 65 2c 20 77 65 20 61 6c l routine, we al
841f0 72 65 61 64 79 20 6b 6e 6f 77 20 74 68 61 74 20 ready know that
84200 70 50 72 69 6f 72 21 3d 30 20 73 69 6e 63 65 0a pPrior!=0 since.
84210 2a 2a 20 63 61 73 65 73 20 77 68 65 72 65 20 70 ** cases where p
84220 50 72 69 6f 72 3d 3d 30 20 77 69 6c 6c 20 68 61 Prior==0 will ha
84230 76 65 20 62 65 65 6e 20 69 6e 74 65 63 65 70 74 ve been intecept
84240 65 64 20 61 6e 64 20 64 65 61 6c 74 20 77 69 74 ed and dealt wit
84250 68 0a 2a 2a 20 62 79 20 68 69 67 68 65 72 2d 6c h.** by higher-l
84260 65 76 65 6c 20 72 6f 75 74 69 6e 65 73 2e 0a 2a evel routines..*
84270 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 /.static void sq
84280 6c 69 74 65 33 4d 65 6d 46 72 65 65 28 76 6f 69 lite3MemFree(voi
84290 64 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 73 71 d *pPrior){. sq
842a0 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 20 3d lite3_int64 *p =
842b0 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a (sqlite3_int64*
842c0 29 70 50 72 69 6f 72 3b 0a 20 20 61 73 73 65 72 )pPrior;. asser
842d0 74 28 20 70 50 72 69 6f 72 21 3d 30 20 29 3b 0a t( pPrior!=0 );.
842e0 20 20 70 2d 2d 3b 0a 20 20 66 72 65 65 28 70 29 p--;. free(p)
842f0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 ;.}../*.** Like
84300 72 65 61 6c 6c 6f 63 28 29 2e 20 20 52 65 73 69 realloc(). Resi
84310 7a 65 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e ze an allocation
84320 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 62 74 61 previously obta
84330 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c ined from.** sql
84340 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 29 2e ite3MemMalloc().
84350 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 69 73 20 .**.** For this
84360 6c 6f 77 2d 6c 65 76 65 6c 20 69 6e 74 65 72 66 low-level interf
84370 61 63 65 2c 20 77 65 20 6b 6e 6f 77 20 74 68 61 ace, we know tha
84380 74 20 70 50 72 69 6f 72 21 3d 30 2e 20 20 43 61 t pPrior!=0. Ca
84390 73 65 73 20 77 68 65 72 65 0a 2a 2a 20 70 50 72 ses where.** pPr
843a0 69 6f 72 3d 3d 30 20 77 68 69 6c 65 20 68 61 76 ior==0 while hav
843b0 65 20 62 65 65 6e 20 69 6e 74 65 72 63 65 70 74 e been intercept
843c0 65 64 20 62 79 20 68 69 67 68 65 72 2d 6c 65 76 ed by higher-lev
843d0 65 6c 20 72 6f 75 74 69 6e 65 20 61 6e 64 0a 2a el routine and.*
843e0 2a 20 72 65 64 69 72 65 63 74 65 64 20 74 6f 20 * redirected to
843f0 78 4d 61 6c 6c 6f 63 2e 20 20 53 69 6d 69 6c 61 xMalloc. Simila
84400 72 6c 79 2c 20 77 65 20 6b 6e 6f 77 20 74 68 61 rly, we know tha
84410 74 20 6e 42 79 74 65 3e 30 20 62 65 63 61 75 73 t nByte>0 becaus
84420 65 73 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 72 es.** cases wher
84430 65 20 6e 42 79 74 65 3c 3d 30 20 77 69 6c 6c 20 e nByte<=0 will
84440 68 61 76 65 20 62 65 65 6e 20 69 6e 74 65 72 63 have been interc
84450 65 70 74 65 64 20 62 79 20 68 69 67 68 65 72 2d epted by higher-
84460 6c 65 76 65 6c 0a 2a 2a 20 72 6f 75 74 69 6e 65 level.** routine
84470 73 20 61 6e 64 20 72 65 64 69 72 65 63 74 65 64 s and redirected
84480 20 74 6f 20 78 46 72 65 65 2e 0a 2a 2f 0a 73 74 to xFree..*/.st
84490 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 atic void *sqlit
844a0 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 76 6f 69 e3MemRealloc(voi
844b0 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e d *pPrior, int n
844c0 42 79 74 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 Byte){. sqlite3
844d0 5f 69 6e 74 36 34 20 2a 70 20 3d 20 28 73 71 6c _int64 *p = (sql
844e0 69 74 65 33 5f 69 6e 74 36 34 2a 29 70 50 72 69 ite3_int64*)pPri
844f0 6f 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 or;. assert( pP
84500 72 69 6f 72 21 3d 30 20 26 26 20 6e 42 79 74 65 rior!=0 && nByte
84510 3e 30 20 29 3b 0a 20 20 6e 42 79 74 65 20 3d 20 >0 );. nByte =
84520 52 4f 55 4e 44 38 28 6e 42 79 74 65 29 3b 0a 20 ROUND8(nByte);.
84530 20 70 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e p = (sqlite3_in
84540 74 36 34 2a 29 70 50 72 69 6f 72 3b 0a 20 20 70 t64*)pPrior;. p
84550 2d 2d 3b 0a 20 20 70 20 3d 20 72 65 61 6c 6c 6f --;. p = reallo
84560 63 28 70 2c 20 6e 42 79 74 65 2b 38 20 29 3b 0a c(p, nByte+8 );.
84570 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70 if( p ){. p
84580 5b 30 5d 20 3d 20 6e 42 79 74 65 3b 0a 20 20 20 [0] = nByte;.
84590 20 70 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 p++;. }. retu
845a0 72 6e 20 28 76 6f 69 64 2a 29 70 3b 0a 7d 0a 0a rn (void*)p;.}..
845b0 2f 2a 0a 2a 2a 20 52 65 70 6f 72 74 20 74 68 65 /*.** Report the
845c0 20 61 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 allocated size
845d0 6f 66 20 61 20 70 72 69 6f 72 20 72 65 74 75 72 of a prior retur
845e0 6e 20 66 72 6f 6d 20 78 4d 61 6c 6c 6f 63 28 29 n from xMalloc()
845f0 0a 2a 2a 20 6f 72 20 78 52 65 61 6c 6c 6f 63 28 .** or xRealloc(
84600 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 )..*/.static int
84610 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 28 sqlite3MemSize(
84620 76 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a 20 void *pPrior){.
84630 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a sqlite3_int64 *
84640 70 3b 0a 20 20 69 66 28 20 70 50 72 69 6f 72 3d p;. if( pPrior=
84650 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 =0 ) return 0;.
84660 20 70 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e p = (sqlite3_in
84670 74 36 34 2a 29 70 50 72 69 6f 72 3b 0a 20 20 70 t64*)pPrior;. p
84680 2d 2d 3b 0a 20 20 72 65 74 75 72 6e 20 28 69 6e --;. return (in
84690 74 29 70 5b 30 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a t)p[0];.}../*.**
846a0 20 52 6f 75 6e 64 20 75 70 20 61 20 72 65 71 75 Round up a requ
846b0 65 73 74 20 73 69 7a 65 20 74 6f 20 74 68 65 20 est size to the
846c0 6e 65 78 74 20 76 61 6c 69 64 20 61 6c 6c 6f 63 next valid alloc
846d0 61 74 69 6f 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 ation size..*/.s
846e0 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 tatic int sqlite
846f0 33 4d 65 6d 52 6f 75 6e 64 75 70 28 69 6e 74 20 3MemRoundup(int
84700 6e 29 7b 0a 20 20 72 65 74 75 72 6e 20 52 4f 55 n){. return ROU
84710 4e 44 38 28 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ND8(n);.}../*.**
84720 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73 Initialize this
84730 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 module..*/.stat
84740 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 ic int sqlite3Me
84750 6d 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 mInit(void *NotU
84760 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 sed){. UNUSED_P
84770 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 ARAMETER(NotUsed
84780 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
84790 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
847a0 44 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 Deinitialize thi
847b0 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 s module..*/.sta
847c0 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 tic void sqlite3
847d0 4d 65 6d 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 MemShutdown(void
847e0 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e *NotUsed){. UN
847f0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e USED_PARAMETER(N
84800 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 otUsed);. retur
84810 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 n;.}../*.** This
84820 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 routine is the
84830 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 only routine in
84840 74 68 69 73 20 66 69 6c 65 20 77 69 74 68 20 65 this file with e
84850 78 74 65 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e xternal linkage.
84860 0a 2a 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 20 .**.** Populate
84870 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 the low-level me
84880 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
84890 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 function pointer
848a0 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 47 s in.** sqlite3G
848b0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 lobalConfig.m wi
848c0 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 th pointers to t
848d0 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 he routines in t
848e0 68 69 73 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c his file..*/.SQL
848f0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
84900 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 sqlite3MemSetDe
84910 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a 20 20 73 fault(void){. s
84920 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 tatic const sqli
84930 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 te3_mem_methods
84940 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 20 3d defaultMethods =
84950 20 7b 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d {. sqlite3M
84960 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 emMalloc,. s
84970 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 2c 0a 20 qlite3MemFree,.
84980 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 65 sqlite3MemRe
84990 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 alloc,. sqli
849a0 74 65 33 4d 65 6d 53 69 7a 65 2c 0a 20 20 20 20 te3MemSize,.
849b0 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64 sqlite3MemRound
849c0 75 70 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 up,. sqlite3
849d0 4d 65 6d 49 6e 69 74 2c 0a 20 20 20 20 20 73 71 MemInit,. sq
849e0 6c 69 74 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e lite3MemShutdown
849f0 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 ,. 0. };.
84a00 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 sqlite3_config(S
84a10 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c QLITE_CONFIG_MAL
84a20 4c 4f 43 2c 20 26 64 65 66 61 75 6c 74 4d 65 74 LOC, &defaultMet
84a30 68 6f 64 73 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 hods);.}..#endif
84a40 20 2f 2a 20 53 51 4c 49 54 45 5f 53 59 53 54 45 /* SQLITE_SYSTE
84a50 4d 5f 4d 41 4c 4c 4f 43 20 2a 2f 0a 0a 2f 2a 2a M_MALLOC */../**
84a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
84a70 20 6f 66 20 6d 65 6d 31 2e 63 20 2a 2a 2a 2a 2a of mem1.c *****
84a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
84ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
84ac0 69 6e 20 66 69 6c 65 20 6d 65 6d 32 2e 63 20 2a in file mem2.c *
84ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
84b00 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 31 ** 2007 August 1
84b10 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 5.**.** The auth
84b20 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
84b30 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
84b40 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
84b50 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
84b60 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
84b70 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
84b80 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
84b90 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
84ba0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
84bb0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
84bc0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
84bd0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
84be0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
84bf0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
84c00 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
84c10 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
84c20 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
84c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84c70 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
84c80 20 63 6f 6e 74 61 69 6e 73 20 6c 6f 77 2d 6c 65 contains low-le
84c90 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 vel memory alloc
84ca0 61 74 69 6f 6e 20 64 72 69 76 65 72 73 20 66 6f ation drivers fo
84cb0 72 20 77 68 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 r when.** SQLite
84cc0 20 77 69 6c 6c 20 75 73 65 20 74 68 65 20 73 74 will use the st
84cd0 61 6e 64 61 72 64 20 43 2d 6c 69 62 72 61 72 79 andard C-library
84ce0 20 6d 61 6c 6c 6f 63 2f 72 65 61 6c 6c 6f 63 2f malloc/realloc/
84cf0 66 72 65 65 20 69 6e 74 65 72 66 61 63 65 0a 2a free interface.*
84d00 2a 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 * to obtain the
84d10 6d 65 6d 6f 72 79 20 69 74 20 6e 65 65 64 73 20 memory it needs
84d20 77 68 69 6c 65 20 61 64 64 69 6e 67 20 6c 6f 74 while adding lot
84d30 73 20 6f 66 20 61 64 64 69 74 69 6f 6e 61 6c 20 s of additional
84d40 64 65 62 75 67 67 69 6e 67 0a 2a 2a 20 69 6e 66 debugging.** inf
84d50 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 65 61 63 68 ormation to each
84d60 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 6f allocation in o
84d70 72 64 65 72 20 74 6f 20 68 65 6c 70 20 64 65 74 rder to help det
84d80 65 63 74 20 61 6e 64 20 66 69 78 20 6d 65 6d 6f ect and fix memo
84d90 72 79 0a 2a 2a 20 6c 65 61 6b 73 20 61 6e 64 20 ry.** leaks and
84da0 6d 65 6d 6f 72 79 20 75 73 61 67 65 20 65 72 72 memory usage err
84db0 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ors..**.** This
84dc0 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6d file contains im
84dd0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 plementations of
84de0 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d the low-level m
84df0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
84e00 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 73 70 65 .** routines spe
84e10 63 69 66 69 65 64 20 69 6e 20 74 68 65 20 73 71 cified in the sq
84e20 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 lite3_mem_method
84e30 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 s object..**.**
84e40 24 49 64 3a 20 6d 65 6d 32 2e 63 2c 76 20 31 2e $Id: mem2.c,v 1.
84e50 34 35 20 32 30 30 39 2f 30 33 2f 32 33 20 30 34 45 2009/03/23 04
84e60 3a 33 33 3a 33 33 20 64 61 6e 69 65 6c 6b 31 39 :33:33 danielk19
84e70 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 77 Exp $.*/../*.
84e80 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 ** This version
84e90 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c of the memory al
84ea0 6c 6f 63 61 74 6f 72 20 69 73 20 75 73 65 64 20 locator is used
84eb0 6f 6e 6c 79 20 69 66 20 74 68 65 0a 2a 2a 20 53 only if the.** S
84ec0 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 20 6d QLITE_MEMDEBUG m
84ed0 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64 0a acro is defined.
84ee0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
84ef0 5f 4d 45 4d 44 45 42 55 47 0a 0a 2f 2a 0a 2a 2a _MEMDEBUG../*.**
84f00 20 54 68 65 20 62 61 63 6b 74 72 61 63 65 20 66 The backtrace f
84f10 75 6e 63 74 69 6f 6e 61 6c 69 74 79 20 69 73 20 unctionality is
84f20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 77 only available w
84f30 69 74 68 20 47 4c 49 42 43 0a 2a 2f 0a 23 69 66 ith GLIBC.*/.#if
84f40 64 65 66 20 5f 5f 47 4c 49 42 43 5f 5f 0a 20 20 def __GLIBC__.
84f50 65 78 74 65 72 6e 20 69 6e 74 20 62 61 63 6b 74 extern int backt
84f60 72 61 63 65 28 76 6f 69 64 2a 2a 2c 69 6e 74 29 race(void**,int)
84f70 3b 0a 20 20 65 78 74 65 72 6e 20 76 6f 69 64 20 ;. extern void
84f80 62 61 63 6b 74 72 61 63 65 5f 73 79 6d 62 6f 6c backtrace_symbol
84f90 73 5f 66 64 28 76 6f 69 64 2a 63 6f 6e 73 74 2a s_fd(void*const*
84fa0 2c 69 6e 74 2c 69 6e 74 29 3b 0a 23 65 6c 73 65 ,int,int);.#else
84fb0 0a 23 20 64 65 66 69 6e 65 20 62 61 63 6b 74 72 .# define backtr
84fc0 61 63 65 28 41 2c 42 29 20 31 0a 23 20 64 65 66 ace(A,B) 1.# def
84fd0 69 6e 65 20 62 61 63 6b 74 72 61 63 65 5f 73 79 ine backtrace_sy
84fe0 6d 62 6f 6c 73 5f 66 64 28 41 2c 42 2c 43 29 0a mbols_fd(A,B,C).
84ff0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 61 #endif../*.** Ea
85000 63 68 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 ch memory alloca
85010 74 69 6f 6e 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 tion looks like
85020 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 2d 2d 2d this:.**.** ---
85030 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
85040 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
85050 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
85060 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
85070 2d 2d 2d 2d 2d 0a 2a 2a 20 20 7c 20 54 69 74 6c -----.** | Titl
85080 65 20 7c 20 20 62 61 63 6b 74 72 61 63 65 20 70 e | backtrace p
85090 6f 69 6e 74 65 72 73 20 7c 20 20 4d 65 6d 42 6c ointers | MemBl
850a0 6f 63 6b 48 64 72 20 7c 20 20 61 6c 6c 6f 63 61 ockHdr | alloca
850b0 74 69 6f 6e 20 7c 20 20 45 6e 64 47 75 61 72 64 tion | EndGuard
850c0 20 7c 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d |.** ---------
850d0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
850e0 2d 2d 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 0a ---------------.
85110 2a 2a 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 **.** The applic
85120 61 74 69 6f 6e 20 63 6f 64 65 20 73 65 65 73 20 ation code sees
85130 6f 6e 6c 79 20 61 20 70 6f 69 6e 74 65 72 20 74 only a pointer t
85140 6f 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e o the allocation
85150 2e 20 20 57 65 20 68 61 76 65 0a 2a 2a 20 74 6f . We have.** to
85160 20 62 61 63 6b 20 75 70 20 66 72 6f 6d 20 74 68 back up from th
85170 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 6f 69 e allocation poi
85180 6e 74 65 72 20 74 6f 20 66 69 6e 64 20 74 68 65 nter to find the
85190 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2e 20 20 54 MemBlockHdr. T
851a0 68 65 0a 2a 2a 20 4d 65 6d 42 6c 6f 63 6b 48 64 he.** MemBlockHd
851b0 72 20 74 65 6c 6c 73 20 75 73 20 74 68 65 20 73 r tells us the s
851c0 69 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 ize of the alloc
851d0 61 74 69 6f 6e 20 61 6e 64 20 74 68 65 20 6e 75 ation and the nu
851e0 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 61 63 6b 74 mber of.** backt
851f0 72 61 63 65 20 70 6f 69 6e 74 65 72 73 2e 20 20 race pointers.
85200 54 68 65 72 65 20 69 73 20 61 6c 73 6f 20 61 20 There is also a
85210 67 75 61 72 64 20 77 6f 72 64 20 61 74 20 74 68 guard word at th
85220 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 2a 2a 20 e end of the.**
85230 4d 65 6d 42 6c 6f 63 6b 48 64 72 2e 0a 2a 2f 0a MemBlockHdr..*/.
85240 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 struct MemBlockH
85250 64 72 20 7b 0a 20 20 69 36 34 20 69 53 69 7a 65 dr {. i64 iSize
85260 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
85270 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
85280 7a 65 20 6f 66 20 74 68 69 73 20 61 6c 6c 6f 63 ze of this alloc
85290 61 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 72 75 63 ation */. struc
852a0 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 t MemBlockHdr *p
852b0 4e 65 78 74 2c 20 2a 70 50 72 65 76 3b 20 20 2f Next, *pPrev; /
852c0 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 * Linked list of
852d0 20 61 6c 6c 20 75 6e 66 72 65 65 64 20 6d 65 6d all unfreed mem
852e0 6f 72 79 20 2a 2f 0a 20 20 63 68 61 72 20 6e 42 ory */. char nB
852f0 61 63 6b 74 72 61 63 65 3b 20 20 20 20 20 20 20 acktrace;
85300 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
85310 4e 75 6d 62 65 72 20 6f 66 20 62 61 63 6b 74 72 Number of backtr
85320 61 63 65 73 20 6f 6e 20 74 68 69 73 20 61 6c 6c aces on this all
85330 6f 63 20 2a 2f 0a 20 20 63 68 61 72 20 6e 42 61 oc */. char nBa
85340 63 6b 74 72 61 63 65 53 6c 6f 74 73 3b 20 20 20 cktraceSlots;
85350 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
85360 76 61 69 6c 61 62 6c 65 20 62 61 63 6b 74 72 61 vailable backtra
85370 63 65 20 73 6c 6f 74 73 20 2a 2f 0a 20 20 73 68 ce slots */. sh
85380 6f 72 74 20 6e 54 69 74 6c 65 3b 20 20 20 20 20 ort nTitle;
85390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
853a0 20 20 2f 2a 20 42 79 74 65 73 20 6f 66 20 74 69 /* Bytes of ti
853b0 74 6c 65 3b 20 69 6e 63 6c 75 64 65 73 20 27 5c tle; includes '\
853c0 30 27 20 2a 2f 0a 20 20 69 6e 74 20 69 46 6f 72 0' */. int iFor
853d0 65 47 75 61 72 64 3b 20 20 20 20 20 20 20 20 20 eGuard;
853e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47 /* G
853f0 75 61 72 64 20 77 6f 72 64 20 66 6f 72 20 73 61 uard word for sa
85400 6e 69 74 79 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a nity */.};../*.*
85410 2a 20 47 75 61 72 64 20 77 6f 72 64 73 0a 2a 2f * Guard words.*/
85420 0a 23 64 65 66 69 6e 65 20 46 4f 52 45 47 55 41 .#define FOREGUA
85430 52 44 20 30 78 38 30 46 35 45 31 35 33 0a 23 64 RD 0x80F5E153.#d
85440 65 66 69 6e 65 20 52 45 41 52 47 55 41 52 44 20 efine REARGUARD
85450 30 78 45 34 36 37 36 42 35 33 0a 0a 2f 2a 0a 2a 0xE4676B53../*.*
85460 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 61 6c 6c * Number of mall
85470 6f 63 20 73 69 7a 65 20 69 6e 63 72 65 6d 65 6e oc size incremen
85480 74 73 20 74 6f 20 74 72 61 63 6b 2e 0a 2a 2f 0a ts to track..*/.
85490 23 64 65 66 69 6e 65 20 4e 43 53 49 5a 45 20 20 #define NCSIZE
854a0 31 30 30 30 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 1000../*.** All
854b0 6f 66 20 74 68 65 20 73 74 61 74 69 63 20 76 61 of the static va
854c0 72 69 61 62 6c 65 73 20 75 73 65 64 20 62 79 20 riables used by
854d0 74 68 69 73 20 6d 6f 64 75 6c 65 20 61 72 65 20 this module are
854e0 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a 20 69 6e 74 collected.** int
854f0 6f 20 61 20 73 69 6e 67 6c 65 20 73 74 72 75 63 o a single struc
85500 74 75 72 65 20 6e 61 6d 65 64 20 22 6d 65 6d 22 ture named "mem"
85510 2e 20 20 54 68 69 73 20 69 73 20 74 6f 20 6b 65 . This is to ke
85520 65 70 20 74 68 65 0a 2a 2a 20 73 74 61 74 69 63 ep the.** static
85530 20 76 61 72 69 61 62 6c 65 73 20 6f 72 67 61 6e variables organ
85540 69 7a 65 64 20 61 6e 64 20 74 6f 20 72 65 64 75 ized and to redu
85550 63 65 20 6e 61 6d 65 73 70 61 63 65 20 70 6f 6c ce namespace pol
85560 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68 65 6e 20 74 lution.** when t
85570 68 69 73 20 6d 6f 64 75 6c 65 20 69 73 20 63 6f his module is co
85580 6d 62 69 6e 65 64 20 77 69 74 68 20 6f 74 68 65 mbined with othe
85590 72 20 69 6e 20 74 68 65 20 61 6d 61 6c 67 61 6d r in the amalgam
855a0 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 ation..*/.static
855b0 20 73 74 72 75 63 74 20 7b 0a 20 20 0a 20 20 2f struct {. . /
855c0 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 20 74 6f 20 *. ** Mutex to
855d0 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 20 74 control access t
855e0 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c o the memory all
855f0 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 ocation subsyste
85600 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 m.. */. sqlite
85610 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 0a 3_mutex *mutex;.
85620 0a 20 20 2f 2a 0a 20 20 2a 2a 20 48 65 61 64 20 . /*. ** Head
85630 61 6e 64 20 74 61 69 6c 20 6f 66 20 61 20 6c 69 and tail of a li
85640 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c nked list of all
85650 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 6c 6c outstanding all
85660 6f 63 61 74 69 6f 6e 73 0a 20 20 2a 2f 0a 20 20 ocations. */.
85670 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 struct MemBlockH
85680 64 72 20 2a 70 46 69 72 73 74 3b 0a 20 20 73 74 dr *pFirst;. st
85690 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 ruct MemBlockHdr
856a0 20 2a 70 4c 61 73 74 3b 0a 20 20 0a 20 20 2f 2a *pLast;. . /*
856b0 0a 20 20 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 . ** The number
856c0 20 6f 66 20 6c 65 76 65 6c 73 20 6f 66 20 62 61 of levels of ba
856d0 63 6b 74 72 61 63 65 20 74 6f 20 73 61 76 65 20 cktrace to save
856e0 69 6e 20 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f in new allocatio
856f0 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 6e ns.. */. int n
85700 42 61 63 6b 74 72 61 63 65 3b 0a 20 20 76 6f 69 Backtrace;. voi
85710 64 20 28 2a 78 42 61 63 6b 74 72 61 63 65 29 28 d (*xBacktrace)(
85720 69 6e 74 2c 20 69 6e 74 2c 20 76 6f 69 64 20 2a int, int, void *
85730 2a 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 *);.. /*. ** T
85740 69 74 6c 65 20 74 65 78 74 20 74 6f 20 69 6e 73 itle text to ins
85750 65 72 74 20 69 6e 20 66 72 6f 6e 74 20 6f 66 20 ert in front of
85760 65 61 63 68 20 62 6c 6f 63 6b 0a 20 20 2a 2f 0a each block. */.
85770 20 20 69 6e 74 20 6e 54 69 74 6c 65 3b 20 20 20 int nTitle;
85780 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f 66 /* Bytes of
85790 20 7a 54 69 74 6c 65 20 74 6f 20 73 61 76 65 2e zTitle to save.
857a0 20 20 49 6e 63 6c 75 64 65 73 20 27 5c 30 27 20 Includes '\0'
857b0 61 6e 64 20 70 61 64 64 69 6e 67 20 2a 2f 0a 20 and padding */.
857c0 20 63 68 61 72 20 7a 54 69 74 6c 65 5b 31 30 30 char zTitle[100
857d0 5d 3b 20 20 2f 2a 20 54 68 65 20 74 69 74 6c 65 ]; /* The title
857e0 20 74 65 78 74 20 2a 2f 0a 0a 20 20 2f 2a 20 0a text */.. /* .
857f0 20 20 2a 2a 20 73 71 6c 69 74 65 33 4d 61 6c 6c ** sqlite3Mall
85800 6f 63 44 69 73 61 6c 6c 6f 77 28 29 20 69 6e 63 ocDisallow() inc
85810 72 65 6d 65 6e 74 73 20 74 68 65 20 66 6f 6c 6c rements the foll
85820 6f 77 69 6e 67 20 63 6f 75 6e 74 65 72 2e 0a 20 owing counter..
85830 20 2a 2a 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f ** sqlite3Mallo
85840 63 41 6c 6c 6f 77 28 29 20 64 65 63 72 65 6d 65 cAllow() decreme
85850 6e 74 73 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 nts it.. */. i
85860 6e 74 20 64 69 73 61 6c 6c 6f 77 3b 20 2f 2a 20 nt disallow; /*
85870 44 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 6d 65 6d Do not allow mem
85880 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a ory allocation *
85890 2f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 47 61 74 /.. /*. ** Gat
858a0 68 65 72 20 73 74 61 74 69 73 74 69 63 73 20 6f her statistics o
858b0 6e 20 74 68 65 20 73 69 7a 65 73 20 6f 66 20 6d n the sizes of m
858c0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
858d0 73 2e 0a 20 20 2a 2a 20 6e 41 6c 6c 6f 63 5b 69 s.. ** nAlloc[i
858e0 5d 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 ] is the number
858f0 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 of allocation at
85900 74 65 6d 70 74 73 20 6f 66 20 69 2a 38 0a 20 20 tempts of i*8.
85910 2a 2a 20 62 79 74 65 73 2e 20 20 69 3d 3d 4e 43 ** bytes. i==NC
85920 53 49 5a 45 20 69 73 20 74 68 65 20 6e 75 6d 62 SIZE is the numb
85930 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e er of allocation
85940 20 61 74 74 65 6d 70 74 73 20 66 6f 72 0a 20 20 attempts for.
85950 2a 2a 20 73 69 7a 65 73 20 6d 6f 72 65 20 74 68 ** sizes more th
85960 61 6e 20 4e 43 53 49 5a 45 2a 38 20 62 79 74 65 an NCSIZE*8 byte
85970 73 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 s.. */. int nA
85980 6c 6c 6f 63 5b 4e 43 53 49 5a 45 5d 3b 20 20 20 lloc[NCSIZE];
85990 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 /* Total numb
859a0 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e er of allocation
859b0 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 75 72 72 s */. int nCurr
859c0 65 6e 74 5b 4e 43 53 49 5a 45 5d 3b 20 20 20 20 ent[NCSIZE];
859d0 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75 6d 62 65 /* Current numbe
859e0 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 r of allocations
859f0 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 43 75 72 72 */. int mxCurr
85a00 65 6e 74 5b 4e 43 53 49 5a 45 5d 3b 20 20 20 2f ent[NCSIZE]; /
85a10 2a 20 48 69 67 68 77 61 74 65 72 20 6d 61 72 6b * Highwater mark
85a20 20 66 6f 72 20 6e 43 75 72 72 65 6e 74 20 2a 2f for nCurrent */
85a30 0a 0a 7d 20 6d 65 6d 3b 0a 0a 0a 2f 2a 0a 2a 2a ..} mem;.../*.**
85a40 20 41 64 6a 75 73 74 20 6d 65 6d 6f 72 79 20 75 Adjust memory u
85a50 73 61 67 65 20 73 74 61 74 69 73 74 69 63 73 0a sage statistics.
85a60 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 */.static void a
85a70 64 6a 75 73 74 53 74 61 74 73 28 69 6e 74 20 69 djustStats(int i
85a80 53 69 7a 65 2c 20 69 6e 74 20 69 6e 63 72 65 6d Size, int increm
85a90 65 6e 74 29 7b 0a 20 20 69 6e 74 20 69 20 3d 20 ent){. int i =
85aa0 52 4f 55 4e 44 38 28 69 53 69 7a 65 29 2f 38 3b ROUND8(iSize)/8;
85ab0 0a 20 20 69 66 28 20 69 3e 4e 43 53 49 5a 45 2d . if( i>NCSIZE-
85ac0 31 20 29 7b 0a 20 20 20 20 69 20 3d 20 4e 43 53 1 ){. i = NCS
85ad0 49 5a 45 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 69 IZE - 1;. }. i
85ae0 66 28 20 69 6e 63 72 65 6d 65 6e 74 3e 30 20 29 f( increment>0 )
85af0 7b 0a 20 20 20 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 {. mem.nAlloc
85b00 5b 69 5d 2b 2b 3b 0a 20 20 20 20 6d 65 6d 2e 6e [i]++;. mem.n
85b10 43 75 72 72 65 6e 74 5b 69 5d 2b 2b 3b 0a 20 20 Current[i]++;.
85b20 20 20 69 66 28 20 6d 65 6d 2e 6e 43 75 72 72 65 if( mem.nCurre
85b30 6e 74 5b 69 5d 3e 6d 65 6d 2e 6d 78 43 75 72 72 nt[i]>mem.mxCurr
85b40 65 6e 74 5b 69 5d 20 29 7b 0a 20 20 20 20 20 20 ent[i] ){.
85b50 6d 65 6d 2e 6d 78 43 75 72 72 65 6e 74 5b 69 5d mem.mxCurrent[i]
85b60 20 3d 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b = mem.nCurrent[
85b70 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 i];. }. }els
85b80 65 7b 0a 20 20 20 20 6d 65 6d 2e 6e 43 75 72 72 e{. mem.nCurr
85b90 65 6e 74 5b 69 5d 2d 2d 3b 0a 20 20 20 20 61 73 ent[i]--;. as
85ba0 73 65 72 74 28 20 6d 65 6d 2e 6e 43 75 72 72 65 sert( mem.nCurre
85bb0 6e 74 5b 69 5d 3e 3d 30 20 29 3b 0a 20 20 7d 0a nt[i]>=0 );. }.
85bc0 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 }../*.** Given a
85bd0 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 66 69 n allocation, fi
85be0 6e 64 20 74 68 65 20 4d 65 6d 42 6c 6f 63 6b 48 nd the MemBlockH
85bf0 64 72 20 66 6f 72 20 74 68 61 74 20 61 6c 6c 6f dr for that allo
85c00 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 cation..**.** Th
85c10 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b is routine check
85c20 73 20 74 68 65 20 67 75 61 72 64 73 20 61 74 20 s the guards at
85c30 65 69 74 68 65 72 20 65 6e 64 20 6f 66 20 74 68 either end of th
85c40 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 6e 64 e allocation and
85c50 0a 2a 2a 20 69 66 20 74 68 65 79 20 61 72 65 20 .** if they are
85c60 69 6e 63 6f 72 72 65 63 74 20 69 74 20 61 73 73 incorrect it ass
85c70 65 72 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 erts..*/.static
85c80 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 struct MemBlockH
85c90 64 72 20 2a 73 71 6c 69 74 65 33 4d 65 6d 73 79 dr *sqlite3Memsy
85ca0 73 47 65 74 48 65 61 64 65 72 28 76 6f 69 64 20 sGetHeader(void
85cb0 2a 70 41 6c 6c 6f 63 61 74 69 6f 6e 29 7b 0a 20 *pAllocation){.
85cc0 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b struct MemBlock
85cd0 48 64 72 20 2a 70 3b 0a 20 20 69 6e 74 20 2a 70 Hdr *p;. int *p
85ce0 49 6e 74 3b 0a 20 20 75 38 20 2a 70 55 38 3b 0a Int;. u8 *pU8;.
85cf0 20 20 69 6e 74 20 6e 52 65 73 65 72 76 65 3b 0a int nReserve;.
85d00 0a 20 20 70 20 3d 20 28 73 74 72 75 63 74 20 4d . p = (struct M
85d10 65 6d 42 6c 6f 63 6b 48 64 72 2a 29 70 41 6c 6c emBlockHdr*)pAll
85d20 6f 63 61 74 69 6f 6e 3b 0a 20 20 70 2d 2d 3b 0a ocation;. p--;.
85d30 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 46 6f assert( p->iFo
85d40 72 65 47 75 61 72 64 3d 3d 28 69 6e 74 29 46 4f reGuard==(int)FO
85d50 52 45 47 55 41 52 44 20 29 3b 0a 20 20 6e 52 65 REGUARD );. nRe
85d60 73 65 72 76 65 20 3d 20 52 4f 55 4e 44 38 28 70 serve = ROUND8(p
85d70 2d 3e 69 53 69 7a 65 29 3b 0a 20 20 70 49 6e 74 ->iSize);. pInt
85d80 20 3d 20 28 69 6e 74 2a 29 70 41 6c 6c 6f 63 61 = (int*)pAlloca
85d90 74 69 6f 6e 3b 0a 20 20 70 55 38 20 3d 20 28 75 tion;. pU8 = (u
85da0 38 2a 29 70 41 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 8*)pAllocation;.
85db0 20 20 61 73 73 65 72 74 28 20 70 49 6e 74 5b 6e assert( pInt[n
85dc0 52 65 73 65 72 76 65 2f 73 69 7a 65 6f 66 28 69 Reserve/sizeof(i
85dd0 6e 74 29 5d 3d 3d 28 69 6e 74 29 52 45 41 52 47 nt)]==(int)REARG
85de0 55 41 52 44 20 29 3b 0a 20 20 2f 2a 20 54 68 69 UARD );. /* Thi
85df0 73 20 63 68 65 63 6b 73 20 61 6e 79 20 6f 66 20 s checks any of
85e00 74 68 65 20 22 65 78 74 72 61 22 20 62 79 74 65 the "extra" byte
85e10 73 20 61 6c 6c 6f 63 61 74 65 64 20 64 75 65 0a s allocated due.
85e20 20 20 2a 2a 20 74 6f 20 72 6f 75 6e 64 69 6e 67 ** to rounding
85e30 20 75 70 20 74 6f 20 61 6e 20 38 20 62 79 74 65 up to an 8 byte
85e40 20 62 6f 75 6e 64 61 72 79 20 74 6f 20 65 6e 73 boundary to ens
85e50 75 72 65 20 0a 20 20 2a 2a 20 74 68 65 79 20 68 ure . ** they h
85e60 61 76 65 6e 27 74 20 62 65 65 6e 20 6f 76 65 72 aven't been over
85e70 77 72 69 74 74 65 6e 2e 0a 20 20 2a 2f 0a 20 20 written.. */.
85e80 77 68 69 6c 65 28 20 6e 52 65 73 65 72 76 65 2d while( nReserve-
85e90 2d 20 3e 20 70 2d 3e 69 53 69 7a 65 20 29 20 61 - > p->iSize ) a
85ea0 73 73 65 72 74 28 20 70 55 38 5b 6e 52 65 73 65 ssert( pU8[nRese
85eb0 72 76 65 5d 3d 3d 30 78 36 35 20 29 3b 0a 20 20 rve]==0x65 );.
85ec0 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a return p;.}../*.
85ed0 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
85ee0 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 63 75 mber of bytes cu
85ef0 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 rrently allocate
85f00 64 20 61 74 20 61 64 64 72 65 73 73 20 70 2e 0a d at address p..
85f10 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 */.static int sq
85f20 6c 69 74 65 33 4d 65 6d 53 69 7a 65 28 76 6f 69 lite3MemSize(voi
85f30 64 20 2a 70 29 7b 0a 20 20 73 74 72 75 63 74 20 d *p){. struct
85f40 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 MemBlockHdr *pHd
85f50 72 3b 0a 20 20 69 66 28 20 21 70 20 29 7b 0a 20 r;. if( !p ){.
85f60 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
85f70 0a 20 20 70 48 64 72 20 3d 20 73 71 6c 69 74 65 . pHdr = sqlite
85f80 33 4d 65 6d 73 79 73 47 65 74 48 65 61 64 65 72 3MemsysGetHeader
85f90 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 48 (p);. return pH
85fa0 64 72 2d 3e 69 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a dr->iSize;.}../*
85fb0 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 .** Initialize t
85fc0 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 he memory alloca
85fd0 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a tion subsystem..
85fe0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 */.static int sq
85ff0 6c 69 74 65 33 4d 65 6d 49 6e 69 74 28 76 6f 69 lite3MemInit(voi
86000 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 d *NotUsed){. U
86010 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
86020 4e 6f 74 55 73 65 64 29 3b 0a 20 20 61 73 73 65 NotUsed);. asse
86030 72 74 28 20 28 73 69 7a 65 6f 66 28 73 74 72 75 rt( (sizeof(stru
86040 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 29 26 ct MemBlockHdr)&
86050 37 29 20 3d 3d 20 30 20 29 3b 0a 20 20 69 66 28 7) == 0 );. if(
86060 20 21 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 !sqlite3GlobalC
86070 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 onfig.bMemstat )
86080 7b 0a 20 20 20 20 2f 2a 20 49 66 20 6d 65 6d 6f {. /* If memo
86090 72 79 20 73 74 61 74 75 73 20 69 73 20 65 6e 61 ry status is ena
860a0 62 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 20 6d bled, then the m
860b0 61 6c 6c 6f 63 2e 63 20 77 72 61 70 70 65 72 20 alloc.c wrapper
860c0 77 69 6c 6c 20 61 6c 72 65 61 64 79 0a 20 20 20 will already.
860d0 20 2a 2a 20 68 6f 6c 64 20 74 68 65 20 53 54 41 ** hold the STA
860e0 54 49 43 5f 4d 45 4d 20 6d 75 74 65 78 20 77 68 TIC_MEM mutex wh
860f0 65 6e 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 en the routines
86100 68 65 72 65 20 61 72 65 20 69 6e 76 6f 6b 65 64 here are invoked
86110 2e 20 2a 2f 0a 20 20 20 20 6d 65 6d 2e 6d 75 74 . */. mem.mut
86120 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 ex = sqlite3Mute
86130 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 xAlloc(SQLITE_MU
86140 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 29 3b TEX_STATIC_MEM);
86150 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
86160 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
86170 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 * Deinitialize t
86180 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 he memory alloca
86190 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a tion subsystem..
861a0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 */.static void s
861b0 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 64 6f 77 qlite3MemShutdow
861c0 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 n(void *NotUsed)
861d0 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d {. UNUSED_PARAM
861e0 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 ETER(NotUsed);.
861f0 20 6d 65 6d 2e 6d 75 74 65 78 20 3d 20 30 3b 0a mem.mutex = 0;.
86200 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 }../*.** Round u
86210 70 20 61 20 72 65 71 75 65 73 74 20 73 69 7a 65 p a request size
86220 20 74 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c to the next val
86230 69 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 id allocation si
86240 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ze..*/.static in
86250 74 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e t sqlite3MemRoun
86260 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20 20 72 65 dup(int n){. re
86270 74 75 72 6e 20 52 4f 55 4e 44 38 28 6e 29 3b 0a turn ROUND8(n);.
86280 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 }../*.** Allocat
86290 65 20 6e 42 79 74 65 20 62 79 74 65 73 20 6f 66 e nByte bytes of
862a0 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 memory..*/.stat
862b0 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 ic void *sqlite3
862c0 4d 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 MemMalloc(int nB
862d0 79 74 65 29 7b 0a 20 20 73 74 72 75 63 74 20 4d yte){. struct M
862e0 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72 emBlockHdr *pHdr
862f0 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 42 74 3b 0a ;. void **pBt;.
86300 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69 6e 74 char *z;. int
86310 20 2a 70 49 6e 74 3b 0a 20 20 76 6f 69 64 20 2a *pInt;. void *
86320 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20 74 6f 74 p = 0;. int tot
86330 61 6c 53 69 7a 65 3b 0a 20 20 69 6e 74 20 6e 52 alSize;. int nR
86340 65 73 65 72 76 65 3b 0a 20 20 73 71 6c 69 74 65 eserve;. sqlite
86350 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 3_mutex_enter(me
86360 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 61 73 73 65 m.mutex);. asse
86370 72 74 28 20 6d 65 6d 2e 64 69 73 61 6c 6c 6f 77 rt( mem.disallow
86380 3d 3d 30 20 29 3b 0a 20 20 6e 52 65 73 65 72 76 ==0 );. nReserv
86390 65 20 3d 20 52 4f 55 4e 44 38 28 6e 42 79 74 65 e = ROUND8(nByte
863a0 29 3b 0a 20 20 74 6f 74 61 6c 53 69 7a 65 20 3d );. totalSize =
863b0 20 6e 52 65 73 65 72 76 65 20 2b 20 73 69 7a 65 nReserve + size
863c0 6f 66 28 2a 70 48 64 72 29 20 2b 20 73 69 7a 65 of(*pHdr) + size
863d0 6f 66 28 69 6e 74 29 20 2b 0a 20 20 20 20 20 20 of(int) +.
863e0 20 20 20 20 20 20 20 20 20 6d 65 6d 2e 6e 42 61 mem.nBa
863f0 63 6b 74 72 61 63 65 2a 73 69 7a 65 6f 66 28 76 cktrace*sizeof(v
86400 6f 69 64 2a 29 20 2b 20 6d 65 6d 2e 6e 54 69 74 oid*) + mem.nTit
86410 6c 65 3b 0a 20 20 70 20 3d 20 6d 61 6c 6c 6f 63 le;. p = malloc
86420 28 74 6f 74 61 6c 53 69 7a 65 29 3b 0a 20 20 69 (totalSize);. i
86430 66 28 20 70 20 29 7b 0a 20 20 20 20 7a 20 3d 20 f( p ){. z =
86440 70 3b 0a 20 20 20 20 70 42 74 20 3d 20 28 76 6f p;. pBt = (vo
86450 69 64 2a 2a 29 26 7a 5b 6d 65 6d 2e 6e 54 69 74 id**)&z[mem.nTit
86460 6c 65 5d 3b 0a 20 20 20 20 70 48 64 72 20 3d 20 le];. pHdr =
86470 28 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b (struct MemBlock
86480 48 64 72 2a 29 26 70 42 74 5b 6d 65 6d 2e 6e 42 Hdr*)&pBt[mem.nB
86490 61 63 6b 74 72 61 63 65 5d 3b 0a 20 20 20 20 70 acktrace];. p
864a0 48 64 72 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a Hdr->pNext = 0;.
864b0 20 20 20 20 70 48 64 72 2d 3e 70 50 72 65 76 20 pHdr->pPrev
864c0 3d 20 6d 65 6d 2e 70 4c 61 73 74 3b 0a 20 20 20 = mem.pLast;.
864d0 20 69 66 28 20 6d 65 6d 2e 70 4c 61 73 74 20 29 if( mem.pLast )
864e0 7b 0a 20 20 20 20 20 20 6d 65 6d 2e 70 4c 61 73 {. mem.pLas
864f0 74 2d 3e 70 4e 65 78 74 20 3d 20 70 48 64 72 3b t->pNext = pHdr;
86500 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
86510 20 20 6d 65 6d 2e 70 46 69 72 73 74 20 3d 20 70 mem.pFirst = p
86520 48 64 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d Hdr;. }. m
86530 65 6d 2e 70 4c 61 73 74 20 3d 20 70 48 64 72 3b em.pLast = pHdr;
86540 0a 20 20 20 20 70 48 64 72 2d 3e 69 46 6f 72 65 . pHdr->iFore
86550 47 75 61 72 64 20 3d 20 46 4f 52 45 47 55 41 52 Guard = FOREGUAR
86560 44 3b 0a 20 20 20 20 70 48 64 72 2d 3e 6e 42 61 D;. pHdr->nBa
86570 63 6b 74 72 61 63 65 53 6c 6f 74 73 20 3d 20 6d cktraceSlots = m
86580 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 3b 0a 20 em.nBacktrace;.
86590 20 20 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 20 pHdr->nTitle
865a0 3d 20 6d 65 6d 2e 6e 54 69 74 6c 65 3b 0a 20 20 = mem.nTitle;.
865b0 20 20 69 66 28 20 6d 65 6d 2e 6e 42 61 63 6b 74 if( mem.nBackt
865c0 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20 76 6f race ){. vo
865d0 69 64 20 2a 61 41 64 64 72 5b 34 30 5d 3b 0a 20 id *aAddr[40];.
865e0 20 20 20 20 20 70 48 64 72 2d 3e 6e 42 61 63 6b pHdr->nBack
865f0 74 72 61 63 65 20 3d 20 62 61 63 6b 74 72 61 63 trace = backtrac
86600 65 28 61 41 64 64 72 2c 20 6d 65 6d 2e 6e 42 61 e(aAddr, mem.nBa
86610 63 6b 74 72 61 63 65 2b 31 29 2d 31 3b 0a 20 20 cktrace+1)-1;.
86620 20 20 20 20 6d 65 6d 63 70 79 28 70 42 74 2c 20 memcpy(pBt,
86630 26 61 41 64 64 72 5b 31 5d 2c 20 70 48 64 72 2d &aAddr[1], pHdr-
86640 3e 6e 42 61 63 6b 74 72 61 63 65 2a 73 69 7a 65 >nBacktrace*size
86650 6f 66 28 76 6f 69 64 2a 29 29 3b 0a 20 20 20 20 of(void*));.
86660 20 20 61 73 73 65 72 74 28 70 42 74 5b 30 5d 29 assert(pBt[0])
86670 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d 2e ;. if( mem.
86680 78 42 61 63 6b 74 72 61 63 65 20 29 7b 0a 20 20 xBacktrace ){.
86690 20 20 20 20 20 20 6d 65 6d 2e 78 42 61 63 6b 74 mem.xBackt
866a0 72 61 63 65 28 6e 42 79 74 65 2c 20 70 48 64 72 race(nByte, pHdr
866b0 2d 3e 6e 42 61 63 6b 74 72 61 63 65 2d 31 2c 20 ->nBacktrace-1,
866c0 26 61 41 64 64 72 5b 31 5d 29 3b 0a 20 20 20 20 &aAddr[1]);.
866d0 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
866e0 20 20 20 20 20 70 48 64 72 2d 3e 6e 42 61 63 6b pHdr->nBack
866f0 74 72 61 63 65 20 3d 20 30 3b 0a 20 20 20 20 7d trace = 0;. }
86700 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 6e 54 69 . if( mem.nTi
86710 74 6c 65 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d tle ){. mem
86720 63 70 79 28 7a 2c 20 6d 65 6d 2e 7a 54 69 74 6c cpy(z, mem.zTitl
86730 65 2c 20 6d 65 6d 2e 6e 54 69 74 6c 65 29 3b 0a e, mem.nTitle);.
86740 20 20 20 20 7d 0a 20 20 20 20 70 48 64 72 2d 3e }. pHdr->
86750 69 53 69 7a 65 20 3d 20 6e 42 79 74 65 3b 0a 20 iSize = nByte;.
86760 20 20 20 61 64 6a 75 73 74 53 74 61 74 73 28 6e adjustStats(n
86770 42 79 74 65 2c 20 2b 31 29 3b 0a 20 20 20 20 70 Byte, +1);. p
86780 49 6e 74 20 3d 20 28 69 6e 74 2a 29 26 70 48 64 Int = (int*)&pHd
86790 72 5b 31 5d 3b 0a 20 20 20 20 70 49 6e 74 5b 6e r[1];. pInt[n
867a0 52 65 73 65 72 76 65 2f 73 69 7a 65 6f 66 28 69 Reserve/sizeof(i
867b0 6e 74 29 5d 20 3d 20 52 45 41 52 47 55 41 52 44 nt)] = REARGUARD
867c0 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 49 6e ;. memset(pIn
867d0 74 2c 20 30 78 36 35 2c 20 6e 52 65 73 65 72 76 t, 0x65, nReserv
867e0 65 29 3b 0a 20 20 20 20 70 20 3d 20 28 76 6f 69 e);. p = (voi
867f0 64 2a 29 70 49 6e 74 3b 0a 20 20 7d 0a 20 20 73 d*)pInt;. }. s
86800 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
86810 76 65 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 ve(mem.mutex);.
86820 20 72 65 74 75 72 6e 20 70 3b 20 0a 7d 0a 0a 2f return p; .}../
86830 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f 72 79 *.** Free memory
86840 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
86850 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 28 sqlite3MemFree(
86860 76 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a 20 void *pPrior){.
86870 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b struct MemBlock
86880 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 76 6f 69 Hdr *pHdr;. voi
86890 64 20 2a 2a 70 42 74 3b 0a 20 20 63 68 61 72 20 d **pBt;. char
868a0 2a 7a 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 *z;. assert( sq
868b0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
868c0 67 2e 62 4d 65 6d 73 74 61 74 20 7c 7c 20 6d 65 g.bMemstat || me
868d0 6d 2e 6d 75 74 65 78 21 3d 30 20 29 3b 0a 20 20 m.mutex!=0 );.
868e0 70 48 64 72 20 3d 20 73 71 6c 69 74 65 33 4d 65 pHdr = sqlite3Me
868f0 6d 73 79 73 47 65 74 48 65 61 64 65 72 28 70 50 msysGetHeader(pP
86900 72 69 6f 72 29 3b 0a 20 20 70 42 74 20 3d 20 28 rior);. pBt = (
86910 76 6f 69 64 2a 2a 29 70 48 64 72 3b 0a 20 20 70 void**)pHdr;. p
86920 42 74 20 2d 3d 20 70 48 64 72 2d 3e 6e 42 61 63 Bt -= pHdr->nBac
86930 6b 74 72 61 63 65 53 6c 6f 74 73 3b 0a 20 20 73 ktraceSlots;. s
86940 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
86950 65 72 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 er(mem.mutex);.
86960 20 69 66 28 20 70 48 64 72 2d 3e 70 50 72 65 76 if( pHdr->pPrev
86970 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
86980 70 48 64 72 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 pHdr->pPrev->pNe
86990 78 74 3d 3d 70 48 64 72 20 29 3b 0a 20 20 20 20 xt==pHdr );.
869a0 70 48 64 72 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 pHdr->pPrev->pNe
869b0 78 74 20 3d 20 70 48 64 72 2d 3e 70 4e 65 78 74 xt = pHdr->pNext
869c0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 ;. }else{. a
869d0 73 73 65 72 74 28 20 6d 65 6d 2e 70 46 69 72 73 ssert( mem.pFirs
869e0 74 3d 3d 70 48 64 72 20 29 3b 0a 20 20 20 20 6d t==pHdr );. m
869f0 65 6d 2e 70 46 69 72 73 74 20 3d 20 70 48 64 72 em.pFirst = pHdr
86a00 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 ->pNext;. }. i
86a10 66 28 20 70 48 64 72 2d 3e 70 4e 65 78 74 20 29 f( pHdr->pNext )
86a20 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 48 {. assert( pH
86a30 64 72 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 dr->pNext->pPrev
86a40 3d 3d 70 48 64 72 20 29 3b 0a 20 20 20 20 70 48 ==pHdr );. pH
86a50 64 72 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 dr->pNext->pPrev
86a60 20 3d 20 70 48 64 72 2d 3e 70 50 72 65 76 3b 0a = pHdr->pPrev;.
86a70 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 }else{. ass
86a80 65 72 74 28 20 6d 65 6d 2e 70 4c 61 73 74 3d 3d ert( mem.pLast==
86a90 70 48 64 72 20 29 3b 0a 20 20 20 20 6d 65 6d 2e pHdr );. mem.
86aa0 70 4c 61 73 74 20 3d 20 70 48 64 72 2d 3e 70 50 pLast = pHdr->pP
86ab0 72 65 76 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 28 rev;. }. z = (
86ac0 63 68 61 72 2a 29 70 42 74 3b 0a 20 20 7a 20 2d char*)pBt;. z -
86ad0 3d 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 3b 0a = pHdr->nTitle;.
86ae0 20 20 61 64 6a 75 73 74 53 74 61 74 73 28 70 48 adjustStats(pH
86af0 64 72 2d 3e 69 53 69 7a 65 2c 20 2d 31 29 3b 0a dr->iSize, -1);.
86b00 20 20 6d 65 6d 73 65 74 28 7a 2c 20 30 78 32 62 memset(z, 0x2b
86b10 2c 20 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 2a , sizeof(void*)*
86b20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 pHdr->nBacktrace
86b30 53 6c 6f 74 73 20 2b 20 73 69 7a 65 6f 66 28 2a Slots + sizeof(*
86b40 70 48 64 72 29 20 2b 0a 20 20 20 20 20 20 20 20 pHdr) +.
86b50 20 20 20 20 20 20 20 20 20 20 70 48 64 72 2d 3e pHdr->
86b60 69 53 69 7a 65 20 2b 20 73 69 7a 65 6f 66 28 69 iSize + sizeof(i
86b70 6e 74 29 20 2b 20 70 48 64 72 2d 3e 6e 54 69 74 nt) + pHdr->nTit
86b80 6c 65 29 3b 0a 20 20 66 72 65 65 28 7a 29 3b 0a le);. free(z);.
86b90 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
86ba0 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 74 65 78 29 leave(mem.mutex)
86bb0 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 ; .}../*.** Cha
86bc0 6e 67 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 nge the size of
86bd0 61 6e 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f an existing memo
86be0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a ry allocation..*
86bf0 2a 0a 2a 2a 20 46 6f 72 20 74 68 69 73 20 64 65 *.** For this de
86c00 62 75 67 67 69 6e 67 20 69 6d 70 6c 65 6d 65 6e bugging implemen
86c10 74 61 74 69 6f 6e 2c 20 77 65 20 2a 61 6c 77 61 tation, we *alwa
86c20 79 73 2a 20 6d 61 6b 65 20 61 20 63 6f 70 79 20 ys* make a copy
86c30 6f 66 20 74 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 of the.** alloca
86c40 74 69 6f 6e 20 69 6e 74 6f 20 61 20 6e 65 77 20 tion into a new
86c50 70 6c 61 63 65 20 69 6e 20 6d 65 6d 6f 72 79 2e place in memory.
86c60 20 20 49 6e 20 74 68 69 73 20 77 61 79 2c 20 69 In this way, i
86c70 66 20 74 68 65 20 0a 2a 2a 20 68 69 67 68 65 72 f the .** higher
86c80 20 6c 65 76 65 6c 20 63 6f 64 65 20 69 73 20 75 level code is u
86c90 73 69 6e 67 20 70 6f 69 6e 74 65 72 20 74 6f 20 sing pointer to
86ca0 74 68 65 20 6f 6c 64 20 61 6c 6c 6f 63 61 74 69 the old allocati
86cb0 6f 6e 2c 20 69 74 20 69 73 20 0a 2a 2a 20 6d 75 on, it is .** mu
86cc0 63 68 20 6d 6f 72 65 20 6c 69 6b 65 6c 79 20 74 ch more likely t
86cd0 6f 20 62 72 65 61 6b 20 61 6e 64 20 77 65 20 61 o break and we a
86ce0 72 65 20 6d 75 63 68 20 6d 6f 72 65 20 6c 69 6b re much more lik
86cf0 69 6e 67 20 74 6f 20 66 69 6e 64 0a 2a 2a 20 74 ing to find.** t
86d00 68 65 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 he error..*/.sta
86d10 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 tic void *sqlite
86d20 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 76 6f 69 64 3MemRealloc(void
86d30 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e 42 *pPrior, int nB
86d40 79 74 65 29 7b 0a 20 20 73 74 72 75 63 74 20 4d yte){. struct M
86d50 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 4f 6c 64 emBlockHdr *pOld
86d60 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 70 4e 65 Hdr;. void *pNe
86d70 77 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d w;. assert( mem
86d80 2e 64 69 73 61 6c 6c 6f 77 3d 3d 30 20 29 3b 0a .disallow==0 );.
86d90 20 20 70 4f 6c 64 48 64 72 20 3d 20 73 71 6c 69 pOldHdr = sqli
86da0 74 65 33 4d 65 6d 73 79 73 47 65 74 48 65 61 64 te3MemsysGetHead
86db0 65 72 28 70 50 72 69 6f 72 29 3b 0a 20 20 70 4e er(pPrior);. pN
86dc0 65 77 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 4d ew = sqlite3MemM
86dd0 61 6c 6c 6f 63 28 6e 42 79 74 65 29 3b 0a 20 20 alloc(nByte);.
86de0 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 if( pNew ){.
86df0 6d 65 6d 63 70 79 28 70 4e 65 77 2c 20 70 50 72 memcpy(pNew, pPr
86e00 69 6f 72 2c 20 6e 42 79 74 65 3c 70 4f 6c 64 48 ior, nByte<pOldH
86e10 64 72 2d 3e 69 53 69 7a 65 20 3f 20 6e 42 79 74 dr->iSize ? nByt
86e20 65 20 3a 20 70 4f 6c 64 48 64 72 2d 3e 69 53 69 e : pOldHdr->iSi
86e30 7a 65 29 3b 0a 20 20 20 20 69 66 28 20 6e 42 79 ze);. if( nBy
86e40 74 65 3e 70 4f 6c 64 48 64 72 2d 3e 69 53 69 7a te>pOldHdr->iSiz
86e50 65 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 e ){. memse
86e60 74 28 26 28 28 63 68 61 72 2a 29 70 4e 65 77 29 t(&((char*)pNew)
86e70 5b 70 4f 6c 64 48 64 72 2d 3e 69 53 69 7a 65 5d [pOldHdr->iSize]
86e80 2c 20 30 78 32 62 2c 20 6e 42 79 74 65 20 2d 20 , 0x2b, nByte -
86e90 70 4f 6c 64 48 64 72 2d 3e 69 53 69 7a 65 29 3b pOldHdr->iSize);
86ea0 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
86eb0 65 33 4d 65 6d 46 72 65 65 28 70 50 72 69 6f 72 e3MemFree(pPrior
86ec0 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
86ed0 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 pNew;.}../*.** P
86ee0 6f 70 75 6c 61 74 65 20 74 68 65 20 6c 6f 77 2d opulate the low-
86ef0 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c level memory all
86f00 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e ocation function
86f10 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a 2a 2a 20 pointers in.**
86f20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
86f30 66 69 67 2e 6d 20 77 69 74 68 20 70 6f 69 6e 74 fig.m with point
86f40 65 72 73 20 74 6f 20 74 68 65 20 72 6f 75 74 69 ers to the routi
86f50 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65 nes in this file
86f60 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
86f70 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
86f80 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28 76 6f MemSetDefault(vo
86f90 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f id){. static co
86fa0 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f nst sqlite3_mem_
86fb0 6d 65 74 68 6f 64 73 20 64 65 66 61 75 6c 74 4d methods defaultM
86fc0 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 20 20 ethods = {.
86fd0 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 sqlite3MemMalloc
86fe0 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 ,. sqlite3Me
86ff0 6d 46 72 65 65 2c 0a 20 20 20 20 20 73 71 6c 69 mFree,. sqli
87000 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 2c 0a 20 te3MemRealloc,.
87010 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 sqlite3MemSi
87020 7a 65 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 ze,. sqlite3
87030 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a 20 20 20 20 MemRoundup,.
87040 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74 2c sqlite3MemInit,
87050 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d . sqlite3Mem
87060 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 20 20 30 Shutdown,. 0
87070 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f . };. sqlite3_
87080 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 4f config(SQLITE_CO
87090 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c 20 26 64 65 NFIG_MALLOC, &de
870a0 66 61 75 6c 74 4d 65 74 68 6f 64 73 29 3b 0a 7d faultMethods);.}
870b0 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 ../*.** Set the
870c0 6e 75 6d 62 65 72 20 6f 66 20 62 61 63 6b 74 72 number of backtr
870d0 61 63 65 20 6c 65 76 65 6c 73 20 6b 65 70 74 20 ace levels kept
870e0 66 6f 72 20 65 61 63 68 20 61 6c 6c 6f 63 61 74 for each allocat
870f0 69 6f 6e 2e 0a 2a 2a 20 41 20 76 61 6c 75 65 20 ion..** A value
87100 6f 66 20 7a 65 72 6f 20 74 75 72 6e 73 20 6f 66 of zero turns of
87110 66 20 62 61 63 6b 74 72 61 63 69 6e 67 2e 20 20 f backtracing.
87120 54 68 65 20 6e 75 6d 62 65 72 20 69 73 20 61 6c The number is al
87130 77 61 79 73 20 72 6f 75 6e 64 65 64 0a 2a 2a 20 ways rounded.**
87140 75 70 20 74 6f 20 61 20 6d 75 6c 74 69 70 6c 65 up to a multiple
87150 20 6f 66 20 32 2e 0a 2a 2f 0a 53 51 4c 49 54 45 of 2..*/.SQLITE
87160 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
87170 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 42 61 63 lite3MemdebugBac
87180 6b 74 72 61 63 65 28 69 6e 74 20 64 65 70 74 68 ktrace(int depth
87190 29 7b 0a 20 20 69 66 28 20 64 65 70 74 68 3c 30 ){. if( depth<0
871a0 20 29 7b 20 64 65 70 74 68 20 3d 20 30 3b 20 7d ){ depth = 0; }
871b0 0a 20 20 69 66 28 20 64 65 70 74 68 3e 32 30 20 . if( depth>20
871c0 29 7b 20 64 65 70 74 68 20 3d 20 32 30 3b 20 7d ){ depth = 20; }
871d0 0a 20 20 64 65 70 74 68 20 3d 20 28 64 65 70 74 . depth = (dept
871e0 68 2b 31 29 26 30 78 66 65 3b 0a 20 20 6d 65 6d h+1)&0xfe;. mem
871f0 2e 6e 42 61 63 6b 74 72 61 63 65 20 3d 20 64 65 .nBacktrace = de
87200 70 74 68 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 pth;.}..SQLITE_P
87210 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
87220 74 65 33 4d 65 6d 64 65 62 75 67 42 61 63 6b 74 te3MemdebugBackt
87230 72 61 63 65 43 61 6c 6c 62 61 63 6b 28 76 6f 69 raceCallback(voi
87240 64 20 28 2a 78 42 61 63 6b 74 72 61 63 65 29 28 d (*xBacktrace)(
87250 69 6e 74 2c 20 69 6e 74 2c 20 76 6f 69 64 20 2a int, int, void *
87260 2a 29 29 7b 0a 20 20 6d 65 6d 2e 78 42 61 63 6b *)){. mem.xBack
87270 74 72 61 63 65 20 3d 20 78 42 61 63 6b 74 72 61 trace = xBacktra
87280 63 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 ce;.}../*.** Set
87290 20 74 68 65 20 74 69 74 6c 65 20 73 74 72 69 6e the title strin
872a0 67 20 66 6f 72 20 73 75 62 73 65 71 75 65 6e 74 g for subsequent
872b0 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 2a 2f allocations..*/
872c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
872d0 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 64 void sqlite3Memd
872e0 65 62 75 67 53 65 74 74 69 74 6c 65 28 63 6f 6e ebugSettitle(con
872f0 73 74 20 63 68 61 72 20 2a 7a 54 69 74 6c 65 29 st char *zTitle)
87300 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 {. unsigned int
87310 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c n = sqlite3Strl
87320 65 6e 33 30 28 7a 54 69 74 6c 65 29 20 2b 20 31 en30(zTitle) + 1
87330 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ;. sqlite3_mute
87340 78 5f 65 6e 74 65 72 28 6d 65 6d 2e 6d 75 74 65 x_enter(mem.mute
87350 78 29 3b 0a 20 20 69 66 28 20 6e 3e 3d 73 69 7a x);. if( n>=siz
87360 65 6f 66 28 6d 65 6d 2e 7a 54 69 74 6c 65 29 20 eof(mem.zTitle)
87370 29 20 6e 20 3d 20 73 69 7a 65 6f 66 28 6d 65 6d ) n = sizeof(mem
87380 2e 7a 54 69 74 6c 65 29 2d 31 3b 0a 20 20 6d 65 .zTitle)-1;. me
87390 6d 63 70 79 28 6d 65 6d 2e 7a 54 69 74 6c 65 2c mcpy(mem.zTitle,
873a0 20 7a 54 69 74 6c 65 2c 20 6e 29 3b 0a 20 20 6d zTitle, n);. m
873b0 65 6d 2e 7a 54 69 74 6c 65 5b 6e 5d 20 3d 20 30 em.zTitle[n] = 0
873c0 3b 0a 20 20 6d 65 6d 2e 6e 54 69 74 6c 65 20 3d ;. mem.nTitle =
873d0 20 52 4f 55 4e 44 38 28 6e 29 3b 0a 20 20 73 71 ROUND8(n);. sq
873e0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
873f0 65 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 7d 0a e(mem.mutex);.}.
87400 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
87410 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 64 void sqlite3Memd
87420 65 62 75 67 53 79 6e 63 28 29 7b 0a 20 20 73 74 ebugSync(){. st
87430 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 ruct MemBlockHdr
87440 20 2a 70 48 64 72 3b 0a 20 20 66 6f 72 28 70 48 *pHdr;. for(pH
87450 64 72 3d 6d 65 6d 2e 70 46 69 72 73 74 3b 20 70 dr=mem.pFirst; p
87460 48 64 72 3b 20 70 48 64 72 3d 70 48 64 72 2d 3e Hdr; pHdr=pHdr->
87470 70 4e 65 78 74 29 7b 0a 20 20 20 20 76 6f 69 64 pNext){. void
87480 20 2a 2a 70 42 74 20 3d 20 28 76 6f 69 64 2a 2a **pBt = (void**
87490 29 70 48 64 72 3b 0a 20 20 20 20 70 42 74 20 2d )pHdr;. pBt -
874a0 3d 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 = pHdr->nBacktra
874b0 63 65 53 6c 6f 74 73 3b 0a 20 20 20 20 6d 65 6d ceSlots;. mem
874c0 2e 78 42 61 63 6b 74 72 61 63 65 28 70 48 64 72 .xBacktrace(pHdr
874d0 2d 3e 69 53 69 7a 65 2c 20 70 48 64 72 2d 3e 6e ->iSize, pHdr->n
874e0 42 61 63 6b 74 72 61 63 65 2d 31 2c 20 26 70 42 Backtrace-1, &pB
874f0 74 5b 31 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a t[1]);. }.}../*
87500 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 66 69 6c .** Open the fil
87510 65 20 69 6e 64 69 63 61 74 65 64 20 61 6e 64 20 e indicated and
87520 77 72 69 74 65 20 61 20 6c 6f 67 20 6f 66 20 61 write a log of a
87530 6c 6c 20 75 6e 66 72 65 65 64 20 6d 65 6d 6f 72 ll unfreed memor
87540 79 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e y .** allocation
87550 73 20 69 6e 74 6f 20 74 68 61 74 20 6c 6f 67 2e s into that log.
87560 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
87570 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d TE void sqlite3M
87580 65 6d 64 65 62 75 67 44 75 6d 70 28 63 6f 6e 73 emdebugDump(cons
87590 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d t char *zFilenam
875a0 65 29 7b 0a 20 20 46 49 4c 45 20 2a 6f 75 74 3b e){. FILE *out;
875b0 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f . struct MemBlo
875c0 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 76 ckHdr *pHdr;. v
875d0 6f 69 64 20 2a 2a 70 42 74 3b 0a 20 20 69 6e 74 oid **pBt;. int
875e0 20 69 3b 0a 20 20 6f 75 74 20 3d 20 66 6f 70 65 i;. out = fope
875f0 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 22 77 22 n(zFilename, "w"
87600 29 3b 0a 20 20 69 66 28 20 6f 75 74 3d 3d 30 20 );. if( out==0
87610 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 73 ){. fprintf(s
87620 74 64 65 72 72 2c 20 22 2a 2a 20 55 6e 61 62 6c tderr, "** Unabl
87630 65 20 74 6f 20 6f 75 74 70 75 74 20 6d 65 6d 6f e to output memo
87640 72 79 20 64 65 62 75 67 20 6f 75 74 70 75 74 20 ry debug output
87650 6c 6f 67 3a 20 25 73 20 2a 2a 5c 6e 22 2c 0a 20 log: %s **\n",.
87660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
87670 20 20 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 zFilename);.
87680 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 return;. }.
87690 20 66 6f 72 28 70 48 64 72 3d 6d 65 6d 2e 70 46 for(pHdr=mem.pF
876a0 69 72 73 74 3b 20 70 48 64 72 3b 20 70 48 64 72 irst; pHdr; pHdr
876b0 3d 70 48 64 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 =pHdr->pNext){.
876c0 20 20 20 63 68 61 72 20 2a 7a 20 3d 20 28 63 68 char *z = (ch
876d0 61 72 2a 29 70 48 64 72 3b 0a 20 20 20 20 7a 20 ar*)pHdr;. z
876e0 2d 3d 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 -= pHdr->nBacktr
876f0 61 63 65 53 6c 6f 74 73 2a 73 69 7a 65 6f 66 28 aceSlots*sizeof(
87700 76 6f 69 64 2a 29 20 2b 20 70 48 64 72 2d 3e 6e void*) + pHdr->n
87710 54 69 74 6c 65 3b 0a 20 20 20 20 66 70 72 69 6e Title;. fprin
87720 74 66 28 6f 75 74 2c 20 22 2a 2a 2a 2a 20 25 6c tf(out, "**** %l
87730 6c 64 20 62 79 74 65 73 20 61 74 20 25 70 20 66 ld bytes at %p f
87740 72 6f 6d 20 25 73 20 2a 2a 2a 2a 5c 6e 22 2c 20 rom %s ****\n",
87750 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 48 64 . pHd
87760 72 2d 3e 69 53 69 7a 65 2c 20 26 70 48 64 72 5b r->iSize, &pHdr[
87770 31 5d 2c 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 1], pHdr->nTitle
87780 20 3f 20 7a 20 3a 20 22 3f 3f 3f 22 29 3b 0a 20 ? z : "???");.
87790 20 20 20 69 66 28 20 70 48 64 72 2d 3e 6e 42 61 if( pHdr->nBa
877a0 63 6b 74 72 61 63 65 20 29 7b 0a 20 20 20 20 20 cktrace ){.
877b0 20 66 66 6c 75 73 68 28 6f 75 74 29 3b 0a 20 20 fflush(out);.
877c0 20 20 20 20 70 42 74 20 3d 20 28 76 6f 69 64 2a pBt = (void*
877d0 2a 29 70 48 64 72 3b 0a 20 20 20 20 20 20 70 42 *)pHdr;. pB
877e0 74 20 2d 3d 20 70 48 64 72 2d 3e 6e 42 61 63 6b t -= pHdr->nBack
877f0 74 72 61 63 65 53 6c 6f 74 73 3b 0a 20 20 20 20 traceSlots;.
87800 20 20 62 61 63 6b 74 72 61 63 65 5f 73 79 6d 62 backtrace_symb
87810 6f 6c 73 5f 66 64 28 70 42 74 2c 20 70 48 64 72 ols_fd(pBt, pHdr
87820 2d 3e 6e 42 61 63 6b 74 72 61 63 65 2c 20 66 69 ->nBacktrace, fi
87830 6c 65 6e 6f 28 6f 75 74 29 29 3b 0a 20 20 20 20 leno(out));.
87840 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
87850 5c 6e 22 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a \n");. }. }.
87860 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
87870 43 4f 55 4e 54 53 3a 5c 6e 22 29 3b 0a 20 20 66 COUNTS:\n");. f
87880 6f 72 28 69 3d 30 3b 20 69 3c 4e 43 53 49 5a 45 or(i=0; i<NCSIZE
87890 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 -1; i++){. if
878a0 28 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 20 ( mem.nAlloc[i]
878b0 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 ){. fprintf
878c0 28 6f 75 74 2c 20 22 20 20 20 25 35 64 3a 20 25 (out, " %5d: %
878d0 31 30 64 20 25 31 30 64 20 25 31 30 64 5c 6e 22 10d %10d %10d\n"
878e0 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 , . i
878f0 2a 38 2c 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 69 *8, mem.nAlloc[i
87900 5d 2c 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b ], mem.nCurrent[
87910 69 5d 2c 20 6d 65 6d 2e 6d 78 43 75 72 72 65 6e i], mem.mxCurren
87920 74 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d t[i]);. }. }
87930 0a 20 20 69 66 28 20 6d 65 6d 2e 6e 41 6c 6c 6f . if( mem.nAllo
87940 63 5b 4e 43 53 49 5a 45 2d 31 5d 20 29 7b 0a 20 c[NCSIZE-1] ){.
87950 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 fprintf(out,
87960 22 20 20 20 25 35 64 3a 20 25 31 30 64 20 25 31 " %5d: %10d %1
87970 30 64 20 25 31 30 64 5c 6e 22 2c 0a 20 20 20 20 0d %10d\n",.
87980 20 20 20 20 20 20 20 20 20 4e 43 53 49 5a 45 2a NCSIZE*
87990 38 2d 38 2c 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 8-8, mem.nAlloc[
879a0 4e 43 53 49 5a 45 2d 31 5d 2c 0a 20 20 20 20 20 NCSIZE-1],.
879b0 20 20 20 20 20 20 20 20 6d 65 6d 2e 6e 43 75 72 mem.nCur
879c0 72 65 6e 74 5b 4e 43 53 49 5a 45 2d 31 5d 2c 20 rent[NCSIZE-1],
879d0 6d 65 6d 2e 6d 78 43 75 72 72 65 6e 74 5b 4e 43 mem.mxCurrent[NC
879e0 53 49 5a 45 2d 31 5d 29 3b 0a 20 20 7d 0a 20 20 SIZE-1]);. }.
879f0 66 63 6c 6f 73 65 28 6f 75 74 29 3b 0a 7d 0a 0a fclose(out);.}..
87a00 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
87a10 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 number of times
87a20 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f sqlite3MemMallo
87a30 63 28 29 20 68 61 73 20 62 65 65 6e 20 63 61 6c c() has been cal
87a40 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 led..*/.SQLITE_P
87a50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
87a60 65 33 4d 65 6d 64 65 62 75 67 4d 61 6c 6c 6f 63 e3MemdebugMalloc
87a70 43 6f 75 6e 74 28 29 7b 0a 20 20 69 6e 74 20 69 Count(){. int i
87a80 3b 0a 20 20 69 6e 74 20 6e 54 6f 74 61 6c 20 3d ;. int nTotal =
87a90 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 0;. for(i=0; i
87aa0 3c 4e 43 53 49 5a 45 3b 20 69 2b 2b 29 7b 0a 20 <NCSIZE; i++){.
87ab0 20 20 20 6e 54 6f 74 61 6c 20 2b 3d 20 6d 65 6d nTotal += mem
87ac0 2e 6e 41 6c 6c 6f 63 5b 69 5d 3b 0a 20 20 7d 0a .nAlloc[i];. }.
87ad0 20 20 72 65 74 75 72 6e 20 6e 54 6f 74 61 6c 3b return nTotal;
87ae0 0a 7d 0a 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 .}...#endif /* S
87af0 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 20 2a QLITE_MEMDEBUG *
87b00 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /../************
87b10 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 6d 32 2e 63 ** End of mem2.c
87b20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
87b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87b50 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
87b60 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 ** Begin file me
87b70 6d 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a m3.c ***********
87b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87ba0 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4f 63 */./*.** 2007 Oc
87bb0 74 6f 62 65 72 20 31 34 0a 2a 2a 0a 2a 2a 20 54 tober 14.**.** T
87bc0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
87bd0 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
87be0 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
87bf0 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
87c00 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
87c10 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
87c20 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
87c30 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
87c40 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
87c50 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
87c60 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
87c70 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
87c80 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
87c90 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
87ca0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
87cb0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
87cc0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
87cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87d10 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 *******.** This
87d20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 file contains th
87d30 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 e C functions th
87d40 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20 6d at implement a m
87d50 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 emory.** allocat
87d60 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 66 6f ion subsystem fo
87d70 72 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 2e r use by SQLite.
87d80 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 .**.** This ver
87d90 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f sion of the memo
87da0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 ry allocation su
87db0 62 73 79 73 74 65 6d 20 6f 6d 69 74 73 20 61 6c bsystem omits al
87dc0 6c 0a 2a 2a 20 75 73 65 20 6f 66 20 6d 61 6c 6c l.** use of mall
87dd0 6f 63 28 29 2e 20 54 68 65 20 53 51 4c 69 74 65 oc(). The SQLite
87de0 20 75 73 65 72 20 73 75 70 70 6c 69 65 73 20 61 user supplies a
87df0 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 block of memory
87e00 0a 2a 2a 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 .** before calli
87e10 6e 67 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 ng sqlite3_initi
87e20 61 6c 69 7a 65 28 29 20 66 72 6f 6d 20 77 68 69 alize() from whi
87e30 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a 2a ch allocations.*
87e40 2a 20 61 72 65 20 6d 61 64 65 20 61 6e 64 20 72 * are made and r
87e50 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 78 eturned by the x
87e60 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 78 52 65 Malloc() and xRe
87e70 61 6c 6c 6f 63 28 29 20 0a 2a 2a 20 69 6d 70 6c alloc() .** impl
87e80 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 20 4f 6e 63 ementations. Onc
87e90 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 e sqlite3_initia
87ea0 6c 69 7a 65 28 29 20 68 61 73 20 62 65 65 6e 20 lize() has been
87eb0 63 61 6c 6c 65 64 2c 0a 2a 2a 20 74 68 65 20 61 called,.** the a
87ec0 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 mount of memory
87ed0 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 53 51 4c available to SQL
87ee0 69 74 65 20 69 73 20 66 69 78 65 64 20 61 6e 64 ite is fixed and
87ef0 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 65 20 63 68 cannot.** be ch
87f00 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 anged..**.** Thi
87f10 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 s version of the
87f20 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
87f30 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 69 73 20 on subsystem is
87f40 69 6e 63 6c 75 64 65 64 0a 2a 2a 20 69 6e 20 74 included.** in t
87f50 68 65 20 62 75 69 6c 64 20 6f 6e 6c 79 20 69 66 he build only if
87f60 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d SQLITE_ENABLE_M
87f70 45 4d 53 59 53 33 20 69 73 20 64 65 66 69 6e 65 EMSYS3 is define
87f80 64 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 65 d..**.** $Id: me
87f90 6d 33 2e 63 2c 76 20 31 2e 32 35 20 32 30 30 38 m3.c,v 1.25 2008
87fa0 2f 31 31 2f 31 39 20 31 36 3a 35 32 3a 34 34 20 /11/19 16:52:44
87fb0 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 danielk1977 Exp
87fc0 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 $.*/../*.** This
87fd0 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 version of the
87fe0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 memory allocator
87ff0 20 69 73 20 6f 6e 6c 79 20 62 75 69 6c 74 20 69 is only built i
88000 6e 74 6f 20 74 68 65 20 6c 69 62 72 61 72 79 0a nto the library.
88010 2a 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ** SQLITE_ENABLE
88020 5f 4d 45 4d 53 59 53 33 20 69 73 20 64 65 66 69 _MEMSYS3 is defi
88030 6e 65 64 2e 20 44 65 66 69 6e 69 6e 67 20 74 68 ned. Defining th
88040 69 73 20 73 79 6d 62 6f 6c 20 64 6f 65 73 20 6e is symbol does n
88050 6f 74 0a 2a 2a 20 6d 65 61 6e 20 74 68 61 74 20 ot.** mean that
88060 74 68 65 20 6c 69 62 72 61 72 79 20 77 69 6c 6c the library will
88070 20 75 73 65 20 61 20 6d 65 6d 6f 72 79 2d 70 6f use a memory-po
88080 6f 6c 20 62 79 20 64 65 66 61 75 6c 74 2c 20 6a ol by default, j
88090 75 73 74 20 74 68 61 74 0a 2a 2a 20 69 74 20 69 ust that.** it i
880a0 73 20 61 76 61 69 6c 61 62 6c 65 2e 20 54 68 65 s available. The
880b0 20 6d 65 6d 70 6f 6f 6c 20 61 6c 6c 6f 63 61 74 mempool allocat
880c0 6f 72 20 69 73 20 61 63 74 69 76 61 74 65 64 20 or is activated
880d0 62 79 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 73 71 by calling.** sq
880e0 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 2e 0a lite3_config()..
880f0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
88100 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 0a _ENABLE_MEMSYS3.
88110 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 73 ./*.** Maximum s
88120 69 7a 65 20 28 69 6e 20 4d 65 6d 33 42 6c 6f 63 ize (in Mem3Bloc
88130 6b 73 29 20 6f 66 20 61 20 22 73 6d 61 6c 6c 22 ks) of a "small"
88140 20 63 68 75 6e 6b 2e 0a 2a 2f 0a 23 64 65 66 69 chunk..*/.#defi
88150 6e 65 20 4d 58 5f 53 4d 41 4c 4c 20 31 30 0a 0a ne MX_SMALL 10..
88160 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 72 20 6f 66 ./*.** Number of
88170 20 66 72 65 65 6c 69 73 74 20 68 61 73 68 20 73 freelist hash s
88180 6c 6f 74 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 lots.*/.#define
88190 4e 5f 48 41 53 48 20 20 36 31 0a 0a 2f 2a 0a 2a N_HASH 61../*.*
881a0 2a 20 41 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 * A memory alloc
881b0 61 74 69 6f 6e 20 28 61 6c 73 6f 20 63 61 6c 6c ation (also call
881c0 65 64 20 61 20 22 63 68 75 6e 6b 22 29 20 63 6f ed a "chunk") co
881d0 6e 73 69 73 74 73 20 6f 66 20 74 77 6f 20 6f 72 nsists of two or
881e0 20 0a 2a 2a 20 6d 6f 72 65 20 62 6c 6f 63 6b 73 .** more blocks
881f0 20 77 68 65 72 65 20 65 61 63 68 20 62 6c 6f 63 where each bloc
88200 6b 20 69 73 20 38 20 62 79 74 65 73 2e 20 20 54 k is 8 bytes. T
88210 68 65 20 66 69 72 73 74 20 38 20 62 79 74 65 73 he first 8 bytes
88220 20 61 72 65 20 0a 2a 2a 20 61 20 68 65 61 64 65 are .** a heade
88230 72 20 74 68 61 74 20 69 73 20 6e 6f 74 20 72 65 r that is not re
88240 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 75 73 turned to the us
88250 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 63 68 75 6e er..**.** A chun
88260 6b 20 69 73 20 74 77 6f 20 6f 72 20 6d 6f 72 65 k is two or more
88270 20 62 6c 6f 63 6b 73 20 74 68 61 74 20 69 73 20 blocks that is
88280 65 69 74 68 65 72 20 63 68 65 63 6b 65 64 20 6f either checked o
88290 75 74 20 6f 72 0a 2a 2a 20 66 72 65 65 2e 20 20 ut or.** free.
882a0 54 68 65 20 66 69 72 73 74 20 62 6c 6f 63 6b 20 The first block
882b0 68 61 73 20 66 6f 72 6d 61 74 20 75 2e 68 64 72 has format u.hdr
882c0 2e 20 20 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 . u.hdr.size4x
882d0 69 73 20 34 20 74 69 6d 65 73 20 74 68 65 0a 2a is 4 times the.*
882e0 2a 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61 6c * size of the al
882f0 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 62 6c 6f 63 location in bloc
88300 6b 73 20 69 66 20 74 68 65 20 61 6c 6c 6f 63 61 ks if the alloca
88310 74 69 6f 6e 20 69 73 20 66 72 65 65 2e 0a 2a 2a tion is free..**
88320 20 54 68 65 20 75 2e 68 64 72 2e 73 69 7a 65 34 The u.hdr.size4
88330 78 26 31 20 62 69 74 20 69 73 20 74 72 75 65 20 x&1 bit is true
88340 69 66 20 74 68 65 20 63 68 75 6e 6b 20 69 73 20 if the chunk is
88350 63 68 65 63 6b 65 64 20 6f 75 74 20 61 6e 64 0a checked out and.
88360 2a 2a 20 66 61 6c 73 65 20 69 66 20 74 68 65 20 ** false if the
88370 63 68 75 6e 6b 20 69 73 20 6f 6e 20 74 68 65 20 chunk is on the
88380 66 72 65 65 6c 69 73 74 2e 20 20 54 68 65 20 75 freelist. The u
88390 2e 68 64 72 2e 73 69 7a 65 34 78 26 32 20 62 69 .hdr.size4x&2 bi
883a0 74 0a 2a 2a 20 69 73 20 74 72 75 65 20 69 66 20 t.** is true if
883b0 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 68 75 the previous 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 20 66 61 6c 73 65 20 69 66 20 74 t and false if t
883e0 68 65 0a 2a 2a 20 70 72 65 76 69 6f 75 73 20 63 he.** previous c
883f0 68 75 6e 6b 20 69 73 20 66 72 65 65 2e 20 20 54 hunk is free. T
88400 68 65 20 75 2e 68 64 72 2e 70 72 65 76 53 69 7a he u.hdr.prevSiz
88410 65 20 66 69 65 6c 64 20 69 73 20 74 68 65 20 73 e field is the s
88420 69 7a 65 20 6f 66 0a 2a 2a 20 74 68 65 20 70 72 ize of.** the pr
88430 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 6e 20 evious chunk in
88440 62 6c 6f 63 6b 73 20 69 66 20 74 68 65 20 70 72 blocks if the pr
88450 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 73 20 evious chunk is
88460 6f 6e 20 74 68 65 0a 2a 2a 20 66 72 65 65 6c 69 on the.** freeli
88470 73 74 2e 20 49 66 20 74 68 65 20 70 72 65 76 69 st. If the previ
88480 6f 75 73 20 63 68 75 6e 6b 20 69 73 20 63 68 65 ous chunk is che
88490 63 6b 65 64 20 6f 75 74 2c 20 74 68 65 6e 0a 2a cked out, then.*
884a0 2a 20 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 * u.hdr.prevSize
884b0 20 63 61 6e 20 62 65 20 70 61 72 74 20 6f 66 20 can be part of
884c0 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 61 the data for tha
884d0 74 20 63 68 75 6e 6b 20 61 6e 64 20 73 68 6f 75 t chunk and shou
884e0 6c 64 0a 2a 2a 20 6e 6f 74 20 62 65 20 72 65 61 ld.** not be rea
884f0 64 20 6f 72 20 77 72 69 74 74 65 6e 2e 0a 2a 2a d or written..**
88500 0a 2a 2a 20 57 65 20 6f 66 74 65 6e 20 69 64 65 .** We often ide
88510 6e 74 69 66 79 20 61 20 63 68 75 6e 6b 20 62 79 ntify a chunk by
88520 20 69 74 73 20 69 6e 64 65 78 20 69 6e 20 6d 65 its index in me
88530 6d 33 2e 61 50 6f 6f 6c 5b 5d 2e 20 20 57 68 65 m3.aPool[]. Whe
88540 6e 0a 2a 2a 20 74 68 69 73 20 69 73 20 64 6f 6e n.** this is don
88550 65 2c 20 74 68 65 20 63 68 75 6e 6b 20 69 6e 64 e, the chunk ind
88560 65 78 20 72 65 66 65 72 73 20 74 6f 20 74 68 65 ex refers to the
88570 20 73 65 63 6f 6e 64 20 62 6c 6f 63 6b 20 6f 66 second block of
88580 0a 2a 2a 20 74 68 65 20 63 68 75 6e 6b 2e 20 20 .** the chunk.
88590 49 6e 20 74 68 69 73 20 77 61 79 2c 20 74 68 65 In this way, the
885a0 20 66 69 72 73 74 20 63 68 75 6e 6b 20 68 61 73 first chunk has
885b0 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 31 2e 0a an index of 1..
885c0 2a 2a 20 41 20 63 68 75 6e 6b 20 69 6e 64 65 78 ** A chunk index
885d0 20 6f 66 20 30 20 6d 65 61 6e 73 20 22 6e 6f 20 of 0 means "no
885e0 73 75 63 68 20 63 68 75 6e 6b 22 20 61 6e 64 20 such chunk" and
885f0 69 73 20 74 68 65 20 65 71 75 69 76 61 6c 65 6e is the equivalen
88600 74 0a 2a 2a 20 6f 66 20 61 20 4e 55 4c 4c 20 70 t.** of a NULL p
88610 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 ointer..**.** Th
88620 65 20 73 65 63 6f 6e 64 20 62 6c 6f 63 6b 20 6f e second block o
88630 66 20 66 72 65 65 20 63 68 75 6e 6b 73 20 69 73 f free chunks is
88640 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 75 2e 6c of the form u.l
88650 69 73 74 2e 20 20 54 68 65 0a 2a 2a 20 74 77 6f ist. The.** two
88660 20 66 69 65 6c 64 73 20 66 6f 72 6d 20 61 20 64 fields form a d
88670 6f 75 62 6c 65 2d 6c 69 6e 6b 65 64 20 6c 69 73 ouble-linked lis
88680 74 20 6f 66 20 63 68 75 6e 6b 73 20 6f 66 20 72 t of chunks of r
88690 65 6c 61 74 65 64 20 73 69 7a 65 73 2e 0a 2a 2a elated sizes..**
886a0 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 Pointers to the
886b0 20 68 65 61 64 20 6f 66 20 74 68 65 20 6c 69 73 head of the lis
886c0 74 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 t are stored in
886d0 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 5d 20 0a mem3.aiSmall[] .
886e0 2a 2a 20 66 6f 72 20 73 6d 61 6c 6c 65 72 20 63 ** for smaller c
886f0 68 75 6e 6b 73 20 61 6e 64 20 6d 65 6d 33 2e 61 hunks and mem3.a
88700 69 48 61 73 68 5b 5d 20 66 6f 72 20 6c 61 72 67 iHash[] for larg
88710 65 72 20 63 68 75 6e 6b 73 2e 0a 2a 2a 0a 2a 2a er chunks..**.**
88720 20 54 68 65 20 73 65 63 6f 6e 64 20 62 6c 6f 63 The second bloc
88730 6b 20 6f 66 20 61 20 63 68 75 6e 6b 20 69 73 20 k of a chunk is
88740 75 73 65 72 20 64 61 74 61 20 69 66 20 74 68 65 user data if the
88750 20 63 68 75 6e 6b 20 69 73 20 63 68 65 63 6b 65 chunk is checke
88760 64 20 0a 2a 2a 20 6f 75 74 2e 20 20 49 66 20 61 d .** out. If a
88770 20 63 68 75 6e 6b 20 69 73 20 63 68 65 63 6b 65 chunk is checke
88780 64 20 6f 75 74 2c 20 74 68 65 20 75 73 65 72 20 d out, the user
88790 64 61 74 61 20 6d 61 79 20 65 78 74 65 6e 64 20 data may extend
887a0 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 75 2e 68 64 into.** the u.hd
887b0 72 2e 70 72 65 76 53 69 7a 65 20 76 61 6c 75 65 r.prevSize value
887c0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
887d0 67 20 63 68 75 6e 6b 2e 0a 2a 2f 0a 74 79 70 65 g chunk..*/.type
887e0 64 65 66 20 73 74 72 75 63 74 20 4d 65 6d 33 42 def struct Mem3B
887f0 6c 6f 63 6b 20 4d 65 6d 33 42 6c 6f 63 6b 3b 0a lock Mem3Block;.
88800 73 74 72 75 63 74 20 4d 65 6d 33 42 6c 6f 63 6b struct Mem3Block
88810 20 7b 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 {. union {.
88820 20 73 74 72 75 63 74 20 7b 0a 20 20 20 20 20 20 struct {.
88830 75 33 32 20 70 72 65 76 53 69 7a 65 3b 20 20 20 u32 prevSize;
88840 2f 2a 20 53 69 7a 65 20 6f 66 20 70 72 65 76 69 /* Size of previ
88850 6f 75 73 20 63 68 75 6e 6b 20 69 6e 20 4d 65 6d ous chunk in Mem
88860 33 42 6c 6f 63 6b 20 65 6c 65 6d 65 6e 74 73 20 3Block elements
88870 2a 2f 0a 20 20 20 20 20 20 75 33 32 20 73 69 7a */. u32 siz
88880 65 34 78 3b 20 20 20 20 20 2f 2a 20 34 78 20 74 e4x; /* 4x t
88890 68 65 20 73 69 7a 65 20 6f 66 20 63 75 72 72 65 he size of curre
888a0 6e 74 20 63 68 75 6e 6b 20 69 6e 20 4d 65 6d 33 nt chunk in Mem3
888b0 42 6c 6f 63 6b 20 65 6c 65 6d 65 6e 74 73 20 2a Block elements *
888c0 2f 0a 20 20 20 20 7d 20 68 64 72 3b 0a 20 20 20 /. } hdr;.
888d0 20 73 74 72 75 63 74 20 7b 0a 20 20 20 20 20 20 struct {.
888e0 75 33 32 20 6e 65 78 74 3b 20 20 20 20 20 20 20 u32 next;
888f0 2f 2a 20 49 6e 64 65 78 20 69 6e 20 6d 65 6d 33 /* Index in mem3
88900 2e 61 50 6f 6f 6c 5b 5d 20 6f 66 20 6e 65 78 74 .aPool[] of next
88910 20 66 72 65 65 20 63 68 75 6e 6b 20 2a 2f 0a 20 free chunk */.
88920 20 20 20 20 20 75 33 32 20 70 72 65 76 3b 20 20 u32 prev;
88930 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e /* Index in
88940 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 5d 20 6f 66 mem3.aPool[] of
88950 20 70 72 65 76 69 6f 75 73 20 66 72 65 65 20 63 previous free c
88960 68 75 6e 6b 20 2a 2f 0a 20 20 20 20 7d 20 6c 69 hunk */. } li
88970 73 74 3b 0a 20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f st;. } u;.};../
88980 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 *.** All of the
88990 73 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 static variables
889a0 20 75 73 65 64 20 62 79 20 74 68 69 73 20 6d 6f used by this mo
889b0 64 75 6c 65 20 61 72 65 20 63 6f 6c 6c 65 63 74 dule are collect
889c0 65 64 0a 2a 2a 20 69 6e 74 6f 20 61 20 73 69 6e ed.** into a sin
889d0 67 6c 65 20 73 74 72 75 63 74 75 72 65 20 6e 61 gle structure na
889e0 6d 65 64 20 22 6d 65 6d 33 22 2e 20 20 54 68 69 med "mem3". Thi
889f0 73 20 69 73 20 74 6f 20 6b 65 65 70 20 74 68 65 s is to keep the
88a00 0a 2a 2a 20 73 74 61 74 69 63 20 76 61 72 69 61 .** static varia
88a10 62 6c 65 73 20 6f 72 67 61 6e 69 7a 65 64 20 61 bles organized a
88a20 6e 64 20 74 6f 20 72 65 64 75 63 65 20 6e 61 6d nd to reduce nam
88a30 65 73 70 61 63 65 20 70 6f 6c 6c 75 74 69 6f 6e espace pollution
88a40 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 6d 6f .** when this mo
88a50 64 75 6c 65 20 69 73 20 63 6f 6d 62 69 6e 65 64 dule is combined
88a60 20 77 69 74 68 20 6f 74 68 65 72 20 69 6e 20 74 with other in t
88a70 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 2e he amalgamation.
88a80 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54 .*/.static SQLIT
88a90 45 5f 57 53 44 20 73 74 72 75 63 74 20 4d 65 6d E_WSD struct Mem
88aa0 33 47 6c 6f 62 61 6c 20 7b 0a 20 20 2f 2a 0a 20 3Global {. /*.
88ab0 20 2a 2a 20 4d 65 6d 6f 72 79 20 61 76 61 69 6c ** Memory avail
88ac0 61 62 6c 65 20 66 6f 72 20 61 6c 6c 6f 63 61 74 able for allocat
88ad0 69 6f 6e 2e 20 6e 50 6f 6f 6c 20 69 73 20 74 68 ion. nPool is th
88ae0 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61 72 e size of the ar
88af0 72 61 79 0a 20 20 2a 2a 20 28 69 6e 20 4d 65 6d ray. ** (in Mem
88b00 33 42 6c 6f 63 6b 73 29 20 70 6f 69 6e 74 65 64 3Blocks) pointed
88b10 20 74 6f 20 62 79 20 61 50 6f 6f 6c 20 6c 65 73 to by aPool les
88b20 73 20 32 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20 s 2.. */. u32
88b30 6e 50 6f 6f 6c 3b 0a 20 20 4d 65 6d 33 42 6c 6f nPool;. Mem3Blo
88b40 63 6b 20 2a 61 50 6f 6f 6c 3b 0a 0a 20 20 2f 2a ck *aPool;.. /*
88b50 0a 20 20 2a 2a 20 54 72 75 65 20 69 66 20 77 65 . ** True if we
88b60 20 61 72 65 20 65 76 61 6c 75 61 74 69 6e 67 20 are evaluating
88b70 61 6e 20 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 an out-of-memory
88b80 20 63 61 6c 6c 62 61 63 6b 2e 0a 20 20 2a 2f 0a callback.. */.
88b90 20 20 69 6e 74 20 61 6c 61 72 6d 42 75 73 79 3b int alarmBusy;
88ba0 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 75 . . /*. ** Mu
88bb0 74 65 78 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 tex to control a
88bc0 63 63 65 73 73 20 74 6f 20 74 68 65 20 6d 65 6d ccess to the mem
88bd0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 ory allocation s
88be0 75 62 73 79 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20 ubsystem.. */.
88bf0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a sqlite3_mutex *
88c00 6d 75 74 65 78 3b 0a 20 20 0a 20 20 2f 2a 0a 20 mutex;. . /*.
88c10 20 2a 2a 20 54 68 65 20 6d 69 6e 69 6d 75 6d 20 ** The minimum
88c20 61 6d 6f 75 6e 74 20 6f 66 20 66 72 65 65 20 73 amount of free s
88c30 70 61 63 65 20 74 68 61 74 20 77 65 20 68 61 76 pace that we hav
88c40 65 20 73 65 65 6e 2e 0a 20 20 2a 2f 0a 20 20 75 e seen.. */. u
88c50 33 32 20 6d 6e 4d 61 73 74 65 72 3b 0a 0a 20 20 32 mnMaster;..
88c60 2f 2a 0a 20 20 2a 2a 20 69 4d 61 73 74 65 72 20 /*. ** iMaster
88c70 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 is the index of
88c80 74 68 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b the master chunk
88c90 2e 20 20 4d 6f 73 74 20 6e 65 77 20 61 6c 6c 6f . Most new allo
88ca0 63 61 74 69 6f 6e 73 0a 20 20 2a 2a 20 6f 63 63 cations. ** occ
88cb0 75 72 20 6f 66 66 20 6f 66 20 74 68 69 73 20 63 ur off of this c
88cc0 68 75 6e 6b 2e 20 20 73 7a 4d 61 73 74 65 72 20 hunk. szMaster
88cd0 69 73 20 74 68 65 20 73 69 7a 65 20 28 69 6e 20 is the size (in
88ce0 4d 65 6d 33 42 6c 6f 63 6b 73 29 0a 20 20 2a 2a Mem3Blocks). **
88cf0 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 of the current
88d00 6d 61 73 74 65 72 2e 20 20 69 4d 61 73 74 65 72 master. iMaster
88d10 20 69 73 20 30 20 69 66 20 74 68 65 72 65 20 69 is 0 if there i
88d20 73 20 6e 6f 74 20 6d 61 73 74 65 72 20 63 68 75 s not master chu
88d30 6e 6b 2e 0a 20 20 2a 2a 20 54 68 65 20 6d 61 73 nk.. ** The mas
88d40 74 65 72 20 63 68 75 6e 6b 20 69 73 20 6e 6f 74 ter chunk is not
88d50 20 69 6e 20 65 69 74 68 65 72 20 74 68 65 20 61 in either the a
88d60 69 48 61 73 68 5b 5d 20 6f 72 20 61 69 53 6d 61 iHash[] or aiSma
88d70 6c 6c 5b 5d 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 ll[].. */. u32
88d80 20 69 4d 61 73 74 65 72 3b 0a 20 20 75 33 32 20 iMaster;. u32
88d90 73 7a 4d 61 73 74 65 72 3b 0a 0a 20 20 2f 2a 0a szMaster;.. /*.
88da0 20 20 2a 2a 20 41 72 72 61 79 20 6f 66 20 6c 69 ** Array of li
88db0 73 74 73 20 6f 66 20 66 72 65 65 20 62 6c 6f 63 sts of free bloc
88dc0 6b 73 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 ks according to
88dd0 74 68 65 20 62 6c 6f 63 6b 20 73 69 7a 65 20 0a the block size .
88de0 20 20 2a 2a 20 66 6f 72 20 73 6d 61 6c 6c 65 72 ** for smaller
88df0 20 63 68 75 6e 6b 73 2c 20 6f 72 20 61 20 68 61 chunks, or a ha
88e00 73 68 20 6f 6e 20 74 68 65 20 62 6c 6f 63 6b 20 sh on the block
88e10 73 69 7a 65 20 66 6f 72 20 6c 61 72 67 65 72 0a size for larger.
88e20 20 20 2a 2a 20 63 68 75 6e 6b 73 2e 0a 20 20 2a ** chunks.. *
88e30 2f 0a 20 20 75 33 32 20 61 69 53 6d 61 6c 6c 5b /. u32 aiSmall[
88e40 4d 58 5f 53 4d 41 4c 4c 2d 31 5d 3b 20 20 20 2f MX_SMALL-1]; /
88e50 2a 20 46 6f 72 20 73 69 7a 65 73 20 32 20 74 68 * For sizes 2 th
88e60 72 6f 75 67 68 20 4d 58 5f 53 4d 41 4c 4c 2c 20 rough MX_SMALL,
88e70 69 6e 63 6c 75 73 69 76 65 20 2a 2f 0a 20 20 75 inclusive */. u
88e80 33 32 20 61 69 48 61 73 68 5b 4e 5f 48 41 53 48 32 aiHash[N_HASH
88e90 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 ]; /* For
88ea0 20 73 69 7a 65 73 20 4d 58 5f 53 4d 41 4c 4c 2b sizes MX_SMALL+
88eb0 31 20 61 6e 64 20 6c 61 72 67 65 72 20 2a 2f 0a 1 and larger */.
88ec0 7d 20 6d 65 6d 33 20 3d 20 7b 20 39 37 35 33 35 } mem3 = { 97535
88ed0 35 37 35 20 7d 3b 0a 0a 23 64 65 66 69 6e 65 20 575 };..#define
88ee0 6d 65 6d 33 20 47 4c 4f 42 41 4c 28 73 74 72 75 mem3 GLOBAL(stru
88ef0 63 74 20 4d 65 6d 33 47 6c 6f 62 61 6c 2c 20 6d ct Mem3Global, m
88f00 65 6d 33 29 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 em3)../*.** Unli
88f10 6e 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 nk the chunk at
88f20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 20 66 72 mem3.aPool[i] fr
88f30 6f 6d 20 6c 69 73 74 20 69 74 20 69 73 20 63 75 om list it is cu
88f40 72 72 65 6e 74 6c 79 0a 2a 2a 20 6f 6e 2e 20 20 rrently.** on.
88f50 2a 70 52 6f 6f 74 20 69 73 20 74 68 65 20 6c 69 *pRoot is the li
88f60 73 74 20 74 68 61 74 20 69 20 69 73 20 61 20 6d st that i is a m
88f70 65 6d 62 65 72 20 6f 66 2e 0a 2a 2f 0a 73 74 61 ember of..*/.sta
88f80 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 tic void memsys3
88f90 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 75 UnlinkFromList(u
88fa0 33 32 20 69 2c 20 75 33 32 20 2a 70 52 6f 6f 74 32 i, u32 *pRoot
88fb0 29 7b 0a 20 20 75 33 32 20 6e 65 78 74 20 3d 20 ){. u32 next =
88fc0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e mem3.aPool[i].u.
88fd0 6c 69 73 74 2e 6e 65 78 74 3b 0a 20 20 75 33 32 list.next;. u32
88fe0 20 70 72 65 76 20 3d 20 6d 65 6d 33 2e 61 50 6f prev = mem3.aPo
88ff0 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 ol[i].u.list.pre
89000 76 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c v;. assert( sql
89010 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
89020 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 mem3.mutex) );.
89030 20 69 66 28 20 70 72 65 76 3d 3d 30 20 29 7b 0a if( prev==0 ){.
89040 20 20 20 20 2a 70 52 6f 6f 74 20 3d 20 6e 65 78 *pRoot = nex
89050 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 t;. }else{.
89060 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 5d mem3.aPool[prev]
89070 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 20 3d 20 6e .u.list.next = n
89080 65 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e ext;. }. if( n
89090 65 78 74 20 29 7b 0a 20 20 20 20 6d 65 6d 33 2e ext ){. mem3.
890a0 61 50 6f 6f 6c 5b 6e 65 78 74 5d 2e 75 2e 6c 69 aPool[next].u.li
890b0 73 74 2e 70 72 65 76 20 3d 20 70 72 65 76 3b 0a st.prev = prev;.
890c0 20 20 7d 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c }. mem3.aPool
890d0 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 20 [i].u.list.next
890e0 3d 20 30 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f = 0;. mem3.aPoo
890f0 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 76 l[i].u.list.prev
89100 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 = 0;.}../*.** U
89110 6e 6c 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b 20 nlink the chunk
89120 61 74 20 69 6e 64 65 78 20 69 20 66 72 6f 6d 20 at index i from
89130 0a 2a 2a 20 77 68 61 74 65 76 65 72 20 6c 69 73 .** whatever lis
89140 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 t is currently a
89150 20 6d 65 6d 62 65 72 20 6f 66 2e 0a 2a 2f 0a 73 member of..*/.s
89160 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 tatic void memsy
89170 73 33 55 6e 6c 69 6e 6b 28 75 33 32 20 69 29 7b s3Unlink(u32 i){
89180 0a 20 20 75 33 32 20 73 69 7a 65 2c 20 68 61 73 . u32 size, has
89190 68 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c h;. assert( sql
891a0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
891b0 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 mem3.mutex) );.
891c0 20 61 73 73 65 72 74 28 20 28 6d 65 6d 33 2e 61 assert( (mem3.a
891d0 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e Pool[i-1].u.hdr.
891e0 73 69 7a 65 34 78 20 26 20 31 29 3d 3d 30 20 29 size4x & 1)==0 )
891f0 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 31 ;. assert( i>=1
89200 20 29 3b 0a 20 20 73 69 7a 65 20 3d 20 6d 65 6d );. size = mem
89210 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 3.aPool[i-1].u.h
89220 64 72 2e 73 69 7a 65 34 78 2f 34 3b 0a 20 20 61 dr.size4x/4;. a
89230 73 73 65 72 74 28 20 73 69 7a 65 3d 3d 6d 65 6d ssert( size==mem
89240 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 3.aPool[i+size-1
89250 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 ].u.hdr.prevSize
89260 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 );. assert( si
89270 7a 65 3e 3d 32 20 29 3b 0a 20 20 69 66 28 20 73 ze>=2 );. if( s
89280 69 7a 65 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 ize <= MX_SMALL
89290 29 7b 0a 20 20 20 20 6d 65 6d 73 79 73 33 55 6e ){. memsys3Un
892a0 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20 linkFromList(i,
892b0 26 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 73 69 &mem3.aiSmall[si
892c0 7a 65 2d 32 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b ze-2]);. }else{
892d0 0a 20 20 20 20 68 61 73 68 20 3d 20 73 69 7a 65 . hash = size
892e0 20 25 20 4e 5f 48 41 53 48 3b 0a 20 20 20 20 6d % N_HASH;. m
892f0 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d emsys3UnlinkFrom
89300 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69 List(i, &mem3.ai
89310 48 61 73 68 5b 68 61 73 68 5d 29 3b 0a 20 20 7d Hash[hash]);. }
89320 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6e 6b 20 74 .}../*.** Link t
89330 68 65 20 63 68 75 6e 6b 20 61 74 20 6d 65 6d 33 he chunk at mem3
89340 2e 61 50 6f 6f 6c 5b 69 5d 20 73 6f 20 74 68 61 .aPool[i] so tha
89350 74 20 69 73 20 6f 6e 20 74 68 65 20 6c 69 73 74 t is on the list
89360 20 72 6f 6f 74 65 64 0a 2a 2a 20 61 74 20 2a 70 rooted.** at *p
89370 52 6f 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 Root..*/.static
89380 76 6f 69 64 20 6d 65 6d 73 79 73 33 4c 69 6e 6b void memsys3Link
89390 49 6e 74 6f 4c 69 73 74 28 75 33 32 20 69 2c 20 IntoList(u32 i,
893a0 75 33 32 20 2a 70 52 6f 6f 74 29 7b 0a 20 20 61 u32 *pRoot){. a
893b0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
893c0 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d utex_held(mem3.m
893d0 75 74 65 78 29 20 29 3b 0a 20 20 6d 65 6d 33 2e utex) );. mem3.
893e0 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e aPool[i].u.list.
893f0 6e 65 78 74 20 3d 20 2a 70 52 6f 6f 74 3b 0a 20 next = *pRoot;.
89400 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 mem3.aPool[i].u
89410 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 30 3b 0a .list.prev = 0;.
89420 20 20 69 66 28 20 2a 70 52 6f 6f 74 20 29 7b 0a if( *pRoot ){.
89430 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 2a mem3.aPool[*
89440 70 52 6f 6f 74 5d 2e 75 2e 6c 69 73 74 2e 70 72 pRoot].u.list.pr
89450 65 76 20 3d 20 69 3b 0a 20 20 7d 0a 20 20 2a 70 ev = i;. }. *p
89460 52 6f 6f 74 20 3d 20 69 3b 0a 7d 0a 0a 2f 2a 0a Root = i;.}../*.
89470 2a 2a 20 4c 69 6e 6b 20 74 68 65 20 63 68 75 6e ** Link the chun
89480 6b 20 61 74 20 69 6e 64 65 78 20 69 20 69 6e 74 k at index i int
89490 6f 20 65 69 74 68 65 72 20 74 68 65 20 61 70 70 o either the app
894a0 72 6f 70 72 69 61 74 65 0a 2a 2a 20 73 6d 61 6c ropriate.** smal
894b0 6c 20 63 68 75 6e 6b 20 6c 69 73 74 2c 20 6f 72 l chunk list, or
894c0 20 69 6e 74 6f 20 74 68 65 20 6c 61 72 67 65 20 into the large
894d0 63 68 75 6e 6b 20 68 61 73 68 20 74 61 62 6c 65 chunk hash table
894e0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
894f0 20 6d 65 6d 73 79 73 33 4c 69 6e 6b 28 75 33 32 memsys3Link(u32
89500 20 69 29 7b 0a 20 20 75 33 32 20 73 69 7a 65 2c i){. u32 size,
89510 20 68 61 73 68 3b 0a 20 20 61 73 73 65 72 74 28 hash;. assert(
89520 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
89530 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 eld(mem3.mutex)
89540 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d );. assert( i>=
89550 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 1 );. assert( (
89560 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e mem3.aPool[i-1].
89570 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20 31 u.hdr.size4x & 1
89580 29 3d 3d 30 20 29 3b 0a 20 20 73 69 7a 65 20 3d )==0 );. size =
89590 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d mem3.aPool[i-1]
895a0 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 3b .u.hdr.size4x/4;
895b0 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 3d . assert( size=
895c0 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 =mem3.aPool[i+si
895d0 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 ze-1].u.hdr.prev
895e0 53 69 7a 65 20 29 3b 0a 20 20 61 73 73 65 72 74 Size );. assert
895f0 28 20 73 69 7a 65 3e 3d 32 20 29 3b 0a 20 20 69 ( size>=2 );. i
89600 66 28 20 73 69 7a 65 20 3c 3d 20 4d 58 5f 53 4d f( size <= MX_SM
89610 41 4c 4c 20 29 7b 0a 20 20 20 20 6d 65 6d 73 79 ALL ){. memsy
89620 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74 28 69 s3LinkIntoList(i
89630 2c 20 26 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b , &mem3.aiSmall[
89640 73 69 7a 65 2d 32 5d 29 3b 0a 20 20 7d 65 6c 73 size-2]);. }els
89650 65 7b 0a 20 20 20 20 68 61 73 68 20 3d 20 73 69 e{. hash = si
89660 7a 65 20 25 20 4e 5f 48 41 53 48 3b 0a 20 20 20 ze % N_HASH;.
89670 20 6d 65 6d 73 79 73 33 4c 69 6e 6b 49 6e 74 6f memsys3LinkInto
89680 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69 List(i, &mem3.ai
89690 48 61 73 68 5b 68 61 73 68 5d 29 3b 0a 20 20 7d Hash[hash]);. }
896a0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 .}../*.** If the
896b0 20 53 54 41 54 49 43 5f 4d 45 4d 20 6d 75 74 65 STATIC_MEM mute
896c0 78 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 x is not already
896d0 20 68 65 6c 64 2c 20 6f 62 74 61 69 6e 20 69 74 held, obtain it
896e0 20 6e 6f 77 2e 20 54 68 65 20 6d 75 74 65 78 0a now. The mutex.
896f0 2a 2a 20 77 69 6c 6c 20 61 6c 72 65 61 64 79 20 ** will already
89700 62 65 20 68 65 6c 64 20 28 6f 62 74 61 69 6e 65 be held (obtaine
89710 64 20 62 79 20 63 6f 64 65 20 69 6e 20 6d 61 6c d by code in mal
89720 6c 6f 63 2e 63 29 20 69 66 0a 2a 2a 20 73 71 6c loc.c) if.** sql
89730 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
89740 2e 62 4d 65 6d 53 74 61 74 20 69 73 20 74 72 75 .bMemStat is tru
89750 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 e..*/.static voi
89760 64 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 76 d memsys3Enter(v
89770 6f 69 64 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 oid){. if( sqli
89780 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
89790 62 4d 65 6d 73 74 61 74 3d 3d 30 20 26 26 20 6d bMemstat==0 && m
897a0 65 6d 33 2e 6d 75 74 65 78 3d 3d 30 20 29 7b 0a em3.mutex==0 ){.
897b0 20 20 20 20 6d 65 6d 33 2e 6d 75 74 65 78 20 3d mem3.mutex =
897c0 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c sqlite3MutexAll
897d0 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f oc(SQLITE_MUTEX_
897e0 53 54 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d STATIC_MEM);. }
897f0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
89800 5f 65 6e 74 65 72 28 6d 65 6d 33 2e 6d 75 74 65 _enter(mem3.mute
89810 78 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 x);.}.static voi
89820 64 20 6d 65 6d 73 79 73 33 4c 65 61 76 65 28 76 d memsys3Leave(v
89830 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f oid){. sqlite3_
89840 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 33 mutex_leave(mem3
89850 2e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a .mutex);.}../*.*
89860 2a 20 43 61 6c 6c 65 64 20 77 68 65 6e 20 77 65 * Called when we
89870 20 61 72 65 20 75 6e 61 62 6c 65 20 74 6f 20 73 are unable to s
89880 61 74 69 73 66 79 20 61 6e 20 61 6c 6c 6f 63 61 atisfy an alloca
89890 74 69 6f 6e 20 6f 66 20 6e 42 79 74 65 73 2e 0a tion of nBytes..
898a0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d */.static void m
898b0 65 6d 73 79 73 33 4f 75 74 4f 66 4d 65 6d 6f 72 emsys3OutOfMemor
898c0 79 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 y(int nByte){.
898d0 69 66 28 20 21 6d 65 6d 33 2e 61 6c 61 72 6d 42 if( !mem3.alarmB
898e0 75 73 79 20 29 7b 0a 20 20 20 20 6d 65 6d 33 2e usy ){. mem3.
898f0 61 6c 61 72 6d 42 75 73 79 20 3d 20 31 3b 0a 20 alarmBusy = 1;.
89900 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 assert( sqlit
89910 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 e3_mutex_held(me
89920 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 m3.mutex) );.
89930 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
89940 65 61 76 65 28 6d 65 6d 33 2e 6d 75 74 65 78 29 eave(mem3.mutex)
89950 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 ;. sqlite3_re
89960 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 6e 42 79 lease_memory(nBy
89970 74 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 te);. sqlite3
89980 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d _mutex_enter(mem
89990 33 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 6d 65 3.mutex);. me
899a0 6d 33 2e 61 6c 61 72 6d 42 75 73 79 20 3d 20 30 m3.alarmBusy = 0
899b0 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 ;. }.}.../*.**
899c0 43 68 75 6e 6b 20 69 20 69 73 20 61 20 66 72 65 Chunk i is a fre
899d0 65 20 63 68 75 6e 6b 20 74 68 61 74 20 68 61 73 e chunk that has
899e0 20 62 65 65 6e 20 75 6e 6c 69 6e 6b 65 64 2e 20 been unlinked.
899f0 20 41 64 6a 75 73 74 20 69 74 73 20 0a 2a 2a 20 Adjust its .**
89a00 73 69 7a 65 20 70 61 72 61 6d 65 74 65 72 73 20 size parameters
89a10 66 6f 72 20 63 68 65 63 6b 2d 6f 75 74 20 61 6e for check-out an
89a20 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 d return a point
89a30 65 72 20 74 6f 20 74 68 65 20 0a 2a 2a 20 75 73 er to the .** us
89a40 65 72 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 er portion of th
89a50 65 20 63 68 75 6e 6b 2e 0a 2a 2f 0a 73 74 61 74 e chunk..*/.stat
89a60 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 33 ic void *memsys3
89a70 43 68 65 63 6b 6f 75 74 28 75 33 32 20 69 2c 20 Checkout(u32 i,
89a80 75 33 32 20 6e 42 6c 6f 63 6b 29 7b 0a 20 20 75 u32 nBlock){. u
89a90 33 32 20 78 3b 0a 20 20 61 73 73 65 72 74 28 20 32 x;. assert(
89aa0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
89ab0 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 ld(mem3.mutex) )
89ac0 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 31 ;. assert( i>=1
89ad0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 );. assert( me
89ae0 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e m3.aPool[i-1].u.
89af0 68 64 72 2e 73 69 7a 65 34 78 2f 34 3d 3d 6e 42 hdr.size4x/4==nB
89b00 6c 6f 63 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 lock );. assert
89b10 28 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 6e ( mem3.aPool[i+n
89b20 42 6c 6f 63 6b 2d 31 5d 2e 75 2e 68 64 72 2e 70 Block-1].u.hdr.p
89b30 72 65 76 53 69 7a 65 3d 3d 6e 42 6c 6f 63 6b 20 revSize==nBlock
89b40 29 3b 0a 20 20 78 20 3d 20 6d 65 6d 33 2e 61 50 );. x = mem3.aP
89b50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 ool[i-1].u.hdr.s
89b60 69 7a 65 34 78 3b 0a 20 20 6d 65 6d 33 2e 61 50 ize4x;. mem3.aP
89b70 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 ool[i-1].u.hdr.s
89b80 69 7a 65 34 78 20 3d 20 6e 42 6c 6f 63 6b 2a 34 ize4x = nBlock*4
89b90 20 7c 20 31 20 7c 20 28 78 26 32 29 3b 0a 20 20 | 1 | (x&2);.
89ba0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 6e 42 6c mem3.aPool[i+nBl
89bb0 6f 63 6b 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 ock-1].u.hdr.pre
89bc0 76 53 69 7a 65 20 3d 20 6e 42 6c 6f 63 6b 3b 0a vSize = nBlock;.
89bd0 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 6e mem3.aPool[i+n
89be0 42 6c 6f 63 6b 2d 31 5d 2e 75 2e 68 64 72 2e 73 Block-1].u.hdr.s
89bf0 69 7a 65 34 78 20 7c 3d 20 32 3b 0a 20 20 72 65 ize4x |= 2;. re
89c00 74 75 72 6e 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c turn &mem3.aPool
89c10 5b 69 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 [i];.}../*.** Ca
89c20 72 76 65 20 61 20 70 69 65 63 65 20 6f 66 66 20 rve a piece off
89c30 6f 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 of the end of th
89c40 65 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 66 e mem3.iMaster f
89c50 72 65 65 20 63 68 75 6e 6b 2e 0a 2a 2a 20 52 65 ree chunk..** Re
89c60 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 turn a pointer t
89c70 6f 20 74 68 65 20 6e 65 77 20 61 6c 6c 6f 63 61 o the new alloca
89c80 74 69 6f 6e 2e 20 20 4f 72 2c 20 69 66 20 74 68 tion. Or, if th
89c90 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 0a 2a e master chunk.*
89ca0 2a 20 69 73 20 6e 6f 74 20 6c 61 72 67 65 20 65 * is not large e
89cb0 6e 6f 75 67 68 2c 20 72 65 74 75 72 6e 20 30 2e nough, return 0.
89cc0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
89cd0 2a 6d 65 6d 73 79 73 33 46 72 6f 6d 4d 61 73 74 *memsys3FromMast
89ce0 65 72 28 75 33 32 20 6e 42 6c 6f 63 6b 29 7b 0a er(u32 nBlock){.
89cf0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
89d00 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 3_mutex_held(mem
89d10 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 3.mutex) );. as
89d20 73 65 72 74 28 20 6d 65 6d 33 2e 73 7a 4d 61 73 sert( mem3.szMas
89d30 74 65 72 3e 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 20 ter>=nBlock );.
89d40 20 69 66 28 20 6e 42 6c 6f 63 6b 3e 3d 6d 65 6d if( nBlock>=mem
89d50 33 2e 73 7a 4d 61 73 74 65 72 2d 31 20 29 7b 0a 3.szMaster-1 ){.
89d60 20 20 20 20 2f 2a 20 55 73 65 20 74 68 65 20 65 /* Use the e
89d70 6e 74 69 72 65 20 6d 61 73 74 65 72 20 2a 2f 0a ntire master */.
89d80 20 20 20 20 76 6f 69 64 20 2a 70 20 3d 20 6d 65 void *p = me
89d90 6d 73 79 73 33 43 68 65 63 6b 6f 75 74 28 6d 65 msys3Checkout(me
89da0 6d 33 2e 69 4d 61 73 74 65 72 2c 20 6d 65 6d 33 m3.iMaster, mem3
89db0 2e 73 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 .szMaster);.
89dc0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20 30 mem3.iMaster = 0
89dd0 3b 0a 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 ;. mem3.szMas
89de0 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 6d 65 6d ter = 0;. mem
89df0 33 2e 6d 6e 4d 61 73 74 65 72 20 3d 20 30 3b 0a 3.mnMaster = 0;.
89e00 20 20 20 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 return p;.
89e10 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 53 70 }else{. /* Sp
89e20 6c 69 74 20 74 68 65 20 6d 61 73 74 65 72 20 62 lit the master b
89e30 6c 6f 63 6b 2e 20 20 52 65 74 75 72 6e 20 74 68 lock. Return th
89e40 65 20 74 61 69 6c 2e 20 2a 2f 0a 20 20 20 20 75 e tail. */. u
89e50 33 32 20 6e 65 77 69 2c 20 78 3b 0a 20 20 20 20 32 newi, x;.
89e60 6e 65 77 69 20 3d 20 6d 65 6d 33 2e 69 4d 61 73 newi = mem3.iMas
89e70 74 65 72 20 2b 20 6d 65 6d 33 2e 73 7a 4d 61 73 ter + mem3.szMas
89e80 74 65 72 20 2d 20 6e 42 6c 6f 63 6b 3b 0a 20 20 ter - nBlock;.
89e90 20 20 61 73 73 65 72 74 28 20 6e 65 77 69 20 3e assert( newi >
89ea0 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 31 20 mem3.iMaster+1
89eb0 29 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f );. mem3.aPoo
89ec0 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d l[mem3.iMaster+m
89ed0 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e em3.szMaster-1].
89ee0 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 3d u.hdr.prevSize =
89ef0 20 6e 42 6c 6f 63 6b 3b 0a 20 20 20 20 6d 65 6d nBlock;. mem
89f00 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 3.aPool[mem3.iMa
89f10 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 ster+mem3.szMast
89f20 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 er-1].u.hdr.size
89f30 34 78 20 7c 3d 20 32 3b 0a 20 20 20 20 6d 65 6d 4x |= 2;. mem
89f40 33 2e 61 50 6f 6f 6c 5b 6e 65 77 69 2d 31 5d 2e 3.aPool[newi-1].
89f50 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 6e u.hdr.size4x = n
89f60 42 6c 6f 63 6b 2a 34 20 2b 20 31 3b 0a 20 20 20 Block*4 + 1;.
89f70 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 2d mem3.szMaster -
89f80 3d 20 6e 42 6c 6f 63 6b 3b 0a 20 20 20 20 6d 65 = nBlock;. me
89f90 6d 33 2e 61 50 6f 6f 6c 5b 6e 65 77 69 2d 31 5d m3.aPool[newi-1]
89fa0 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 .u.hdr.prevSize
89fb0 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3b = mem3.szMaster;
89fc0 0a 20 20 20 20 78 20 3d 20 6d 65 6d 33 2e 61 50 . x = mem3.aP
89fd0 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 ool[mem3.iMaster
89fe0 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
89ff0 20 26 20 32 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 & 2;. mem3.a
8a000 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 Pool[mem3.iMaste
8a010 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 r-1].u.hdr.size4
8a020 78 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 x = mem3.szMaste
8a030 72 2a 34 20 7c 20 78 3b 0a 20 20 20 20 69 66 28 r*4 | x;. if(
8a040 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 3c mem3.szMaster <
8a050 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 20 29 mem3.mnMaster )
8a060 7b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 6d 6e 4d {. mem3.mnM
8a070 61 73 74 65 72 20 3d 20 6d 65 6d 33 2e 73 7a 4d aster = mem3.szM
8a080 61 73 74 65 72 3b 0a 20 20 20 20 7d 0a 20 20 20 aster;. }.
8a090 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 26 return (void*)&
8a0a0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e 65 77 69 5d mem3.aPool[newi]
8a0b0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 2a ;. }.}../*.** *
8a0c0 70 52 6f 6f 74 20 69 73 20 74 68 65 20 68 65 61 pRoot is the hea
8a0d0 64 20 6f 66 20 61 20 6c 69 73 74 20 6f 66 20 66 d of a list of f
8a0e0 72 65 65 20 63 68 75 6e 6b 73 20 6f 66 20 74 68 ree chunks of th
8a0f0 65 20 73 61 6d 65 20 73 69 7a 65 0a 2a 2a 20 6f e same size.** o
8a100 72 20 73 61 6d 65 20 73 69 7a 65 20 68 61 73 68 r same size hash
8a110 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 . In other word
8a120 73 2c 20 2a 70 52 6f 6f 74 20 69 73 20 61 6e 20 s, *pRoot is an
8a130 65 6e 74 72 79 20 69 6e 20 65 69 74 68 65 72 0a entry in either.
8a140 2a 2a 20 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b ** mem3.aiSmall[
8a150 5d 20 6f 72 20 6d 65 6d 33 2e 61 69 48 61 73 68 ] or mem3.aiHash
8a160 5b 5d 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 []. .**.** This
8a170 20 72 6f 75 74 69 6e 65 20 65 78 61 6d 69 6e 65 routine examine
8a180 73 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 6f 6e s all entries on
8a190 20 74 68 65 20 67 69 76 65 6e 20 6c 69 73 74 20 the given list
8a1a0 61 6e 64 20 74 72 69 65 73 0a 2a 2a 20 74 6f 20 and tries.** to
8a1b0 63 6f 61 6c 65 73 63 65 20 65 61 63 68 20 65 6e coalesce each en
8a1c0 74 72 69 65 73 20 77 69 74 68 20 61 64 6a 61 63 tries with adjac
8a1d0 65 6e 74 20 66 72 65 65 20 63 68 75 6e 6b 73 2e ent free chunks.
8a1e0 20 20 0a 2a 2a 0a 2a 2a 20 49 66 20 69 74 20 73 .**.** If it s
8a1f0 65 65 73 20 61 20 63 68 75 6e 6b 20 74 68 61 74 ees a chunk that
8a200 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 is larger than
8a210 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2c 20 69 74 mem3.iMaster, it
8a220 20 72 65 70 6c 61 63 65 73 20 0a 2a 2a 20 74 68 replaces .** th
8a230 65 20 63 75 72 72 65 6e 74 20 6d 65 6d 33 2e 69 e current mem3.i
8a240 4d 61 73 74 65 72 20 77 69 74 68 20 74 68 65 20 Master with the
8a250 6e 65 77 20 6c 61 72 67 65 72 20 63 68 75 6e 6b new larger chunk
8a260 2e 20 20 49 6e 20 6f 72 64 65 72 20 66 6f 72 0a . In order for.
8a270 2a 2a 20 74 68 69 73 20 6d 65 6d 33 2e 69 4d 61 ** this mem3.iMa
8a280 73 74 65 72 20 72 65 70 6c 61 63 65 6d 65 6e 74 ster replacement
8a290 20 74 6f 20 77 6f 72 6b 2c 20 74 68 65 20 6d 61 to work, the ma
8a2a0 73 74 65 72 20 63 68 75 6e 6b 20 6d 75 73 74 20 ster chunk must
8a2b0 62 65 0a 2a 2a 20 6c 69 6e 6b 65 64 20 69 6e 74 be.** linked int
8a2c0 6f 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 o the hash table
8a2d0 73 2e 20 20 54 68 61 74 20 69 73 20 6e 6f 74 20 s. That is not
8a2e0 74 68 65 20 6e 6f 72 6d 61 6c 20 73 74 61 74 65 the normal state
8a2f0 20 6f 66 0a 2a 2a 20 61 66 66 61 69 72 73 2c 20 of.** affairs,
8a300 6f 66 20 63 6f 75 72 73 65 2e 20 20 54 68 65 20 of course. The
8a310 63 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 20 calling routine
8a320 6d 75 73 74 20 6c 69 6e 6b 20 74 68 65 20 6d 61 must link the ma
8a330 73 74 65 72 0a 2a 2a 20 63 68 75 6e 6b 20 62 65 ster.** chunk be
8a340 66 6f 72 65 20 69 6e 76 6f 6b 69 6e 67 20 74 68 fore invoking th
8a350 69 73 20 72 6f 75 74 69 6e 65 2c 20 74 68 65 6e is routine, then
8a360 20 6d 75 73 74 20 75 6e 6c 69 6e 6b 20 74 68 65 must unlink the
8a370 20 28 70 6f 73 73 69 62 6c 79 0a 2a 2a 20 63 68 (possibly.** ch
8a380 61 6e 67 65 64 29 20 6d 61 73 74 65 72 20 63 68 anged) master ch
8a390 75 6e 6b 20 6f 6e 63 65 20 74 68 69 73 20 72 6f unk once this ro
8a3a0 75 74 69 6e 65 20 68 61 73 20 66 69 6e 69 73 68 utine has finish
8a3b0 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ed..*/.static vo
8a3c0 69 64 20 6d 65 6d 73 79 73 33 4d 65 72 67 65 28 id memsys3Merge(
8a3d0 75 33 32 20 2a 70 52 6f 6f 74 29 7b 0a 20 20 75 u32 *pRoot){. u
8a3e0 33 32 20 69 4e 65 78 74 2c 20 70 72 65 76 2c 20 32 iNext, prev,
8a3f0 73 69 7a 65 2c 20 69 2c 20 78 3b 0a 0a 20 20 61 size, i, x;.. a
8a400 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
8a410 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d utex_held(mem3.m
8a420 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 utex) );. for(i
8a430 3d 2a 70 52 6f 6f 74 3b 20 69 3e 30 3b 20 69 3d =*pRoot; i>0; i=
8a440 69 4e 65 78 74 29 7b 0a 20 20 20 20 69 4e 65 78 iNext){. iNex
8a450 74 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 t = mem3.aPool[i
8a460 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 3b 0a 20 ].u.list.next;.
8a470 20 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 size = mem3.a
8a480 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e Pool[i-1].u.hdr.
8a490 73 69 7a 65 34 78 3b 0a 20 20 20 20 61 73 73 65 size4x;. asse
8a4a0 72 74 28 20 28 73 69 7a 65 26 31 29 3d 3d 30 20 rt( (size&1)==0
8a4b0 29 3b 0a 20 20 20 20 69 66 28 20 28 73 69 7a 65 );. if( (size
8a4c0 26 32 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 &2)==0 ){.
8a4d0 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f memsys3UnlinkFro
8a4e0 6d 4c 69 73 74 28 69 2c 20 70 52 6f 6f 74 29 3b mList(i, pRoot);
8a4f0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 . assert( i
8a500 20 3e 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d > mem3.aPool[i-
8a510 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 1].u.hdr.prevSiz
8a520 65 20 29 3b 0a 20 20 20 20 20 20 70 72 65 76 20 e );. prev
8a530 3d 20 69 20 2d 20 6d 65 6d 33 2e 61 50 6f 6f 6c = i - mem3.aPool
8a540 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 [i-1].u.hdr.prev
8a550 53 69 7a 65 3b 0a 20 20 20 20 20 20 69 66 28 20 Size;. if(
8a560 70 72 65 76 3d 3d 69 4e 65 78 74 20 29 7b 0a 20 prev==iNext ){.
8a570 20 20 20 20 20 20 20 69 4e 65 78 74 20 3d 20 6d iNext = m
8a580 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 5d 2e em3.aPool[prev].
8a590 75 2e 6c 69 73 74 2e 6e 65 78 74 3b 0a 20 20 20 u.list.next;.
8a5a0 20 20 20 7d 0a 20 20 20 20 20 20 6d 65 6d 73 79 }. memsy
8a5b0 73 33 55 6e 6c 69 6e 6b 28 70 72 65 76 29 3b 0a s3Unlink(prev);.
8a5c0 20 20 20 20 20 20 73 69 7a 65 20 3d 20 69 20 2b size = i +
8a5d0 20 73 69 7a 65 2f 34 20 2d 20 70 72 65 76 3b 0a size/4 - prev;.
8a5e0 20 20 20 20 20 20 78 20 3d 20 6d 65 6d 33 2e 61 x = mem3.a
8a5f0 50 6f 6f 6c 5b 70 72 65 76 2d 31 5d 2e 75 2e 68 Pool[prev-1].u.h
8a600 64 72 2e 73 69 7a 65 34 78 20 26 20 32 3b 0a 20 dr.size4x & 2;.
8a610 20 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b mem3.aPool[
8a620 70 72 65 76 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 prev-1].u.hdr.si
8a630 7a 65 34 78 20 3d 20 73 69 7a 65 2a 34 20 7c 20 ze4x = size*4 |
8a640 78 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50 x;. mem3.aP
8a650 6f 6f 6c 5b 70 72 65 76 2b 73 69 7a 65 2d 31 5d ool[prev+size-1]
8a660 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 .u.hdr.prevSize
8a670 3d 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 = size;. me
8a680 6d 73 79 73 33 4c 69 6e 6b 28 70 72 65 76 29 3b msys3Link(prev);
8a690 0a 20 20 20 20 20 20 69 20 3d 20 70 72 65 76 3b . i = prev;
8a6a0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
8a6b0 20 20 73 69 7a 65 20 2f 3d 20 34 3b 0a 20 20 20 size /= 4;.
8a6c0 20 7d 0a 20 20 20 20 69 66 28 20 73 69 7a 65 3e }. if( size>
8a6d0 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 29 7b mem3.szMaster ){
8a6e0 0a 20 20 20 20 20 20 6d 65 6d 33 2e 69 4d 61 73 . mem3.iMas
8a6f0 74 65 72 20 3d 20 69 3b 0a 20 20 20 20 20 20 6d ter = i;. m
8a700 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 3d 20 73 em3.szMaster = s
8a710 69 7a 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d ize;. }. }.}
8a720 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 ../*.** Return a
8a730 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 block of memory
8a740 20 6f 66 20 61 74 20 6c 65 61 73 74 20 6e 42 79 of at least nBy
8a750 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 20 tes in size..**
8a760 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 75 Return NULL if u
8a770 6e 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 nable..**.** Thi
8a780 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d s function assum
8a790 65 73 20 74 68 61 74 20 74 68 65 20 6e 65 63 65 es that the nece
8a7a0 73 73 61 72 79 20 6d 75 74 65 78 65 73 2c 20 69 ssary mutexes, i
8a7b0 66 20 61 6e 79 2c 20 61 72 65 0a 2a 2a 20 61 6c f any, are.** al
8a7c0 72 65 61 64 79 20 68 65 6c 64 20 62 79 20 74 68 ready held by th
8a7d0 65 20 63 61 6c 6c 65 72 2e 20 48 65 6e 63 65 20 e caller. Hence
8a7e0 22 55 6e 73 61 66 65 22 2e 0a 2a 2f 0a 73 74 61 "Unsafe"..*/.sta
8a7f0 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 tic void *memsys
8a800 33 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 69 6e 3MallocUnsafe(in
8a810 74 20 6e 42 79 74 65 29 7b 0a 20 20 75 33 32 20 t nByte){. u32
8a820 69 3b 0a 20 20 75 33 32 20 6e 42 6c 6f 63 6b 3b i;. u32 nBlock;
8a830 0a 20 20 75 33 32 20 74 6f 46 72 65 65 3b 0a 0a . u32 toFree;..
8a840 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
8a850 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 3_mutex_held(mem
8a860 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 3.mutex) );. as
8a870 73 65 72 74 28 20 73 69 7a 65 6f 66 28 4d 65 6d sert( sizeof(Mem
8a880 33 42 6c 6f 63 6b 29 3d 3d 38 20 29 3b 0a 20 20 3Block)==8 );.
8a890 69 66 28 20 6e 42 79 74 65 3c 3d 31 32 20 29 7b if( nByte<=12 ){
8a8a0 0a 20 20 20 20 6e 42 6c 6f 63 6b 20 3d 20 32 3b . nBlock = 2;
8a8b0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 42 . }else{. nB
8a8c0 6c 6f 63 6b 20 3d 20 28 6e 42 79 74 65 20 2b 20 lock = (nByte +
8a8d0 31 31 29 2f 38 3b 0a 20 20 7d 0a 20 20 61 73 73 11)/8;. }. ass
8a8e0 65 72 74 28 20 6e 42 6c 6f 63 6b 3e 3d 32 20 29 ert( nBlock>=2 )
8a8f0 3b 0a 0a 20 20 2f 2a 20 53 54 45 50 20 31 3a 0a ;.. /* STEP 1:.
8a900 20 20 2a 2a 20 4c 6f 6f 6b 20 66 6f 72 20 61 6e ** Look for an
8a910 20 65 6e 74 72 79 20 6f 66 20 74 68 65 20 63 6f entry of the co
8a920 72 72 65 63 74 20 73 69 7a 65 20 69 6e 20 65 69 rrect size in ei
8a930 74 68 65 72 20 74 68 65 20 73 6d 61 6c 6c 0a 20 ther the small.
8a940 20 2a 2a 20 63 68 75 6e 6b 20 74 61 62 6c 65 20 ** chunk table
8a950 6f 72 20 69 6e 20 74 68 65 20 6c 61 72 67 65 20 or in the large
8a960 63 68 75 6e 6b 20 68 61 73 68 20 74 61 62 6c 65 chunk hash table
8a970 2e 20 20 54 68 69 73 20 69 73 0a 20 20 2a 2a 20 . This is. **
8a980 73 75 63 63 65 73 73 66 75 6c 20 6d 6f 73 74 20 successful most
8a990 6f 66 20 74 68 65 20 74 69 6d 65 20 28 61 62 6f of the time (abo
8a9a0 75 74 20 39 20 74 69 6d 65 73 20 6f 75 74 20 6f ut 9 times out o
8a9b0 66 20 31 30 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 f 10).. */. if
8a9c0 28 20 6e 42 6c 6f 63 6b 20 3c 3d 20 4d 58 5f 53 ( nBlock <= MX_S
8a9d0 4d 41 4c 4c 20 29 7b 0a 20 20 20 20 69 20 3d 20 MALL ){. i =
8a9e0 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 6e 42 6c mem3.aiSmall[nBl
8a9f0 6f 63 6b 2d 32 5d 3b 0a 20 20 20 20 69 66 28 20 ock-2];. if(
8aa00 69 3e 30 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d i>0 ){. mem
8aa10 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 sys3UnlinkFromLi
8aa20 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69 53 6d st(i, &mem3.aiSm
8aa30 61 6c 6c 5b 6e 42 6c 6f 63 6b 2d 32 5d 29 3b 0a all[nBlock-2]);.
8aa40 20 20 20 20 20 20 72 65 74 75 72 6e 20 6d 65 6d return mem
8aa50 73 79 73 33 43 68 65 63 6b 6f 75 74 28 69 2c 20 sys3Checkout(i,
8aa60 6e 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 7d 0a 20 nBlock);. }.
8aa70 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 }else{. int
8aa80 68 61 73 68 20 3d 20 6e 42 6c 6f 63 6b 20 25 20 hash = nBlock %
8aa90 4e 5f 48 41 53 48 3b 0a 20 20 20 20 66 6f 72 28 N_HASH;. for(
8aaa0 69 3d 6d 65 6d 33 2e 61 69 48 61 73 68 5b 68 61 i=mem3.aiHash[ha
8aab0 73 68 5d 3b 20 69 3e 30 3b 20 69 3d 6d 65 6d 33 sh]; i>0; i=mem3
8aac0 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 .aPool[i].u.list
8aad0 2e 6e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 66 .next){. if
8aae0 28 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 ( mem3.aPool[i-1
8aaf0 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 ].u.hdr.size4x/4
8ab00 3d 3d 6e 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 ==nBlock ){.
8ab10 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e memsys3Unlin
8ab20 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65 kFromList(i, &me
8ab30 6d 33 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 29 m3.aiHash[hash])
8ab40 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e ;. return
8ab50 20 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f 75 74 memsys3Checkout
8ab60 28 69 2c 20 6e 42 6c 6f 63 6b 29 3b 0a 20 20 20 (i, nBlock);.
8ab70 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a }. }. }..
8ab80 20 20 2f 2a 20 53 54 45 50 20 32 3a 0a 20 20 2a /* STEP 2:. *
8ab90 2a 20 54 72 79 20 74 6f 20 73 61 74 69 73 66 79 * Try to satisfy
8aba0 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 the allocation
8abb0 62 79 20 63 61 72 76 69 6e 67 20 61 20 70 69 65 by carving a pie
8abc0 63 65 20 6f 66 66 20 6f 66 20 74 68 65 20 65 6e ce off of the en
8abd0 64 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 6d 61 d. ** of the ma
8abe0 73 74 65 72 20 63 68 75 6e 6b 2e 20 20 54 68 69 ster chunk. Thi
8abf0 73 20 73 74 65 70 20 75 73 75 61 6c 6c 79 20 77 s step usually w
8ac00 6f 72 6b 73 20 69 66 20 73 74 65 70 20 31 20 66 orks if step 1 f
8ac10 61 69 6c 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 ails.. */. if(
8ac20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3e 3d mem3.szMaster>=
8ac30 6e 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 72 65 nBlock ){. re
8ac40 74 75 72 6e 20 6d 65 6d 73 79 73 33 46 72 6f 6d turn memsys3From
8ac50 4d 61 73 74 65 72 28 6e 42 6c 6f 63 6b 29 3b 0a Master(nBlock);.
8ac60 20 20 7d 0a 0a 0a 20 20 2f 2a 20 53 54 45 50 20 }... /* STEP
8ac70 33 3a 20 20 0a 20 20 2a 2a 20 4c 6f 6f 70 20 74 3: . ** Loop t
8ac80 68 72 6f 75 67 68 20 74 68 65 20 65 6e 74 69 72 hrough the entir
8ac90 65 20 6d 65 6d 6f 72 79 20 70 6f 6f 6c 2e 20 20 e memory pool.
8aca0 43 6f 61 6c 65 73 63 65 20 61 64 6a 61 63 65 6e Coalesce adjacen
8acb0 74 20 66 72 65 65 0a 20 20 2a 2a 20 63 68 75 6e t free. ** chun
8acc0 6b 73 2e 20 20 52 65 63 6f 6d 70 75 74 65 20 74 ks. Recompute t
8acd0 68 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20 he master chunk
8ace0 61 73 20 74 68 65 20 6c 61 72 67 65 73 74 20 66 as the largest f
8acf0 72 65 65 20 63 68 75 6e 6b 2e 0a 20 20 2a 2a 20 ree chunk.. **
8ad00 54 68 65 6e 20 74 72 79 20 61 67 61 69 6e 20 74 Then try again t
8ad10 6f 20 73 61 74 69 73 66 79 20 74 68 65 20 61 6c o satisfy the al
8ad20 6c 6f 63 61 74 69 6f 6e 20 62 79 20 63 61 72 76 location by carv
8ad30 69 6e 67 20 61 20 70 69 65 63 65 20 6f 66 66 0a ing a piece off.
8ad40 20 20 2a 2a 20 6f 66 20 74 68 65 20 65 6e 64 20 ** of the end
8ad50 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 63 68 of the master ch
8ad60 75 6e 6b 2e 20 20 54 68 69 73 20 73 74 65 70 20 unk. This step
8ad70 68 61 70 70 65 6e 73 20 76 65 72 79 0a 20 20 2a happens very. *
8ad80 2a 20 72 61 72 65 6c 79 20 28 77 65 20 68 6f 70 * rarely (we hop
8ad90 65 21 29 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 74 e!). */. for(t
8ada0 6f 46 72 65 65 3d 6e 42 6c 6f 63 6b 2a 31 36 3b oFree=nBlock*16;
8adb0 20 74 6f 46 72 65 65 3c 28 6d 65 6d 33 2e 6e 50 toFree<(mem3.nP
8adc0 6f 6f 6c 2a 31 36 29 3b 20 74 6f 46 72 65 65 20 ool*16); toFree
8add0 2a 3d 20 32 29 7b 0a 20 20 20 20 6d 65 6d 73 79 *= 2){. memsy
8ade0 73 33 4f 75 74 4f 66 4d 65 6d 6f 72 79 28 74 6f s3OutOfMemory(to
8adf0 46 72 65 65 29 3b 0a 20 20 20 20 69 66 28 20 6d Free);. if( m
8ae00 65 6d 33 2e 69 4d 61 73 74 65 72 20 29 7b 0a 20 em3.iMaster ){.
8ae10 20 20 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b memsys3Link
8ae20 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 29 3b 0a (mem3.iMaster);.
8ae30 20 20 20 20 20 20 6d 65 6d 33 2e 69 4d 61 73 74 mem3.iMast
8ae40 65 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 6d 65 er = 0;. me
8ae50 6d 33 2e 73 7a 4d 61 73 74 65 72 20 3d 20 30 3b m3.szMaster = 0;
8ae60 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 . }. for(i
8ae70 3d 30 3b 20 69 3c 4e 5f 48 41 53 48 3b 20 69 2b =0; i<N_HASH; i+
8ae80 2b 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 +){. memsys
8ae90 33 4d 65 72 67 65 28 26 6d 65 6d 33 2e 61 69 48 3Merge(&mem3.aiH
8aea0 61 73 68 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 ash[i]);. }.
8aeb0 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4d 58 for(i=0; i<MX
8aec0 5f 53 4d 41 4c 4c 2d 31 3b 20 69 2b 2b 29 7b 0a _SMALL-1; i++){.
8aed0 20 20 20 20 20 20 6d 65 6d 73 79 73 33 4d 65 72 memsys3Mer
8aee0 67 65 28 26 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c ge(&mem3.aiSmall
8aef0 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 [i]);. }.
8af00 69 66 28 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 if( mem3.szMaste
8af10 72 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 r ){. memsy
8af20 73 33 55 6e 6c 69 6e 6b 28 6d 65 6d 33 2e 69 4d s3Unlink(mem3.iM
8af30 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 69 66 aster);. if
8af40 28 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3e ( mem3.szMaster>
8af50 3d 6e 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 =nBlock ){.
8af60 20 20 20 72 65 74 75 72 6e 20 6d 65 6d 73 79 73 return memsys
8af70 33 46 72 6f 6d 4d 61 73 74 65 72 28 6e 42 6c 6f 3FromMaster(nBlo
8af80 63 6b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ck);. }.
8af90 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 }. }.. /* If
8afa0 6e 6f 6e 65 20 6f 66 20 74 68 65 20 61 62 6f 76 none of the abov
8afb0 65 20 77 6f 72 6b 65 64 2c 20 74 68 65 6e 20 77 e worked, then w
8afc0 65 20 66 61 69 6c 2e 20 2a 2f 0a 20 20 72 65 74 e fail. */. ret
8afd0 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn 0;.}../*.**
8afe0 46 72 65 65 20 61 6e 20 6f 75 74 73 74 61 6e 64 Free an outstand
8aff0 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 ing memory alloc
8b000 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 ation..**.** Thi
8b010 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d s function assum
8b020 65 73 20 74 68 61 74 20 74 68 65 20 6e 65 63 65 es that the nece
8b030 73 73 61 72 79 20 6d 75 74 65 78 65 73 2c 20 69 ssary mutexes, i
8b040 66 20 61 6e 79 2c 20 61 72 65 0a 2a 2a 20 61 6c f any, are.** al
8b050 72 65 61 64 79 20 68 65 6c 64 20 62 79 20 74 68 ready held by th
8b060 65 20 63 61 6c 6c 65 72 2e 20 48 65 6e 63 65 20 e caller. Hence
8b070 22 55 6e 73 61 66 65 22 2e 0a 2a 2f 0a 76 6f 69 "Unsafe"..*/.voi
8b080 64 20 6d 65 6d 73 79 73 33 46 72 65 65 55 6e 73 d memsys3FreeUns
8b090 61 66 65 28 76 6f 69 64 20 2a 70 4f 6c 64 29 7b afe(void *pOld){
8b0a0 0a 20 20 4d 65 6d 33 42 6c 6f 63 6b 20 2a 70 20 . Mem3Block *p
8b0b0 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b 2a 29 70 4f = (Mem3Block*)pO
8b0c0 6c 64 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 75 ld;. int i;. u
8b0d0 33 32 20 73 69 7a 65 2c 20 78 3b 0a 20 20 61 73 32 size, x;. as
8b0e0 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
8b0f0 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 tex_held(mem3.mu
8b100 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
8b110 28 20 70 3e 6d 65 6d 33 2e 61 50 6f 6f 6c 20 26 ( p>mem3.aPool &
8b120 26 20 70 3c 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b & p<&mem3.aPool[
8b130 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 20 29 3b 0a 20 mem3.nPool] );.
8b140 20 69 20 3d 20 70 20 2d 20 6d 65 6d 33 2e 61 50 i = p - mem3.aP
8b150 6f 6f 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 28 ool;. assert( (
8b160 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e mem3.aPool[i-1].
8b170 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 31 29 3d u.hdr.size4x&1)=
8b180 3d 31 20 29 3b 0a 20 20 73 69 7a 65 20 3d 20 6d =1 );. size = m
8b190 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 em3.aPool[i-1].u
8b1a0 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 3b 0a 20 .hdr.size4x/4;.
8b1b0 20 61 73 73 65 72 74 28 20 69 2b 73 69 7a 65 3c assert( i+size<
8b1c0 3d 6d 65 6d 33 2e 6e 50 6f 6f 6c 2b 31 20 29 3b =mem3.nPool+1 );
8b1d0 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d . mem3.aPool[i-
8b1e0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 1].u.hdr.size4x
8b1f0 26 3d 20 7e 31 3b 0a 20 20 6d 65 6d 33 2e 61 50 &= ~1;. mem3.aP
8b200 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e ool[i+size-1].u.
8b210 68 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 73 hdr.prevSize = s
8b220 69 7a 65 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f ize;. mem3.aPoo
8b230 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 l[i+size-1].u.hd
8b240 72 2e 73 69 7a 65 34 78 20 26 3d 20 7e 32 3b 0a r.size4x &= ~2;.
8b250 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b 28 69 29 memsys3Link(i)
8b260 3b 0a 0a 20 20 2f 2a 20 54 72 79 20 74 6f 20 65 ;.. /* Try to e
8b270 78 70 61 6e 64 20 74 68 65 20 6d 61 73 74 65 72 xpand the master
8b280 20 75 73 69 6e 67 20 74 68 65 20 6e 65 77 6c 79 using the newly
8b290 20 66 72 65 65 64 20 63 68 75 6e 6b 20 2a 2f 0a freed chunk */.
8b2a0 20 20 69 66 28 20 6d 65 6d 33 2e 69 4d 61 73 74 if( mem3.iMast
8b2b0 65 72 20 29 7b 0a 20 20 20 20 77 68 69 6c 65 28 er ){. while(
8b2c0 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d (mem3.aPool[mem
8b2d0 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 3.iMaster-1].u.h
8b2e0 64 72 2e 73 69 7a 65 34 78 26 32 29 3d 3d 30 20 dr.size4x&2)==0
8b2f0 29 7b 0a 20 20 20 20 20 20 73 69 7a 65 20 3d 20 ){. size =
8b300 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e mem3.aPool[mem3.
8b310 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 iMaster-1].u.hdr
8b320 2e 70 72 65 76 53 69 7a 65 3b 0a 20 20 20 20 20 .prevSize;.
8b330 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 2d 3d mem3.iMaster -=
8b340 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d size;. mem
8b350 33 2e 73 7a 4d 61 73 74 65 72 20 2b 3d 20 73 69 3.szMaster += si
8b360 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 ze;. memsys
8b370 33 55 6e 6c 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 3Unlink(mem3.iMa
8b380 73 74 65 72 29 3b 0a 20 20 20 20 20 20 78 20 3d ster);. x =
8b390 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 mem3.aPool[mem3
8b3a0 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 .iMaster-1].u.hd
8b3b0 72 2e 73 69 7a 65 34 78 20 26 20 32 3b 0a 20 20 r.size4x & 2;.
8b3c0 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d mem3.aPool[m
8b3d0 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 em3.iMaster-1].u
8b3e0 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 6d 65 .hdr.size4x = me
8b3f0 6d 33 2e 73 7a 4d 61 73 74 65 72 2a 34 20 7c 20 m3.szMaster*4 |
8b400 78 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50 x;. mem3.aP
8b410 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 ool[mem3.iMaster
8b420 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31 +mem3.szMaster-1
8b430 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 ].u.hdr.prevSize
8b440 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 = mem3.szMaster
8b450 3b 0a 20 20 20 20 7d 0a 20 20 20 20 78 20 3d 20 ;. }. x =
8b460 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e mem3.aPool[mem3.
8b470 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 iMaster-1].u.hdr
8b480 2e 73 69 7a 65 34 78 20 26 20 32 3b 0a 20 20 20 .size4x & 2;.
8b490 20 77 68 69 6c 65 28 20 28 6d 65 6d 33 2e 61 50 while( (mem3.aP
8b4a0 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 ool[mem3.iMaster
8b4b0 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31 +mem3.szMaster-1
8b4c0 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 31 ].u.hdr.size4x&1
8b4d0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6d 65 )==0 ){. me
8b4e0 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 6d 65 6d 33 msys3Unlink(mem3
8b4f0 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a .iMaster+mem3.sz
8b500 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 6d Master);. m
8b510 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 2b 3d 20 em3.szMaster +=
8b520 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e mem3.aPool[mem3.
8b530 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d iMaster+mem3.szM
8b540 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 aster-1].u.hdr.s
8b550 69 7a 65 34 78 2f 34 3b 0a 20 20 20 20 20 20 6d ize4x/4;. m
8b560 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 em3.aPool[mem3.i
8b570 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e Master-1].u.hdr.
8b580 73 69 7a 65 34 78 20 3d 20 6d 65 6d 33 2e 73 7a size4x = mem3.sz
8b590 4d 61 73 74 65 72 2a 34 20 7c 20 78 3b 0a 20 20 Master*4 | x;.
8b5a0 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d mem3.aPool[m
8b5b0 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 em3.iMaster+mem3
8b5c0 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 .szMaster-1].u.h
8b5d0 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d 65 dr.prevSize = me
8b5e0 6d 33 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 m3.szMaster;.
8b5f0 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
8b600 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 Return the size
8b610 6f 66 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e of an outstandin
8b620 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 6e g allocation, in
8b630 20 62 79 74 65 73 2e 20 20 54 68 65 0a 2a 2a 20 bytes. The.**
8b640 73 69 7a 65 20 72 65 74 75 72 6e 65 64 20 6f 6d size returned om
8b650 69 74 73 20 74 68 65 20 38 2d 62 79 74 65 20 68 its the 8-byte h
8b660 65 61 64 65 72 20 6f 76 65 72 68 65 61 64 2e 20 eader overhead.
8b670 20 54 68 69 73 20 6f 6e 6c 79 0a 2a 2a 20 77 6f This only.** wo
8b680 72 6b 73 20 66 6f 72 20 63 68 75 6e 6b 73 20 74 rks for chunks t
8b690 68 61 74 20 61 72 65 20 63 75 72 72 65 6e 74 6c hat are currentl
8b6a0 79 20 63 68 65 63 6b 65 64 20 6f 75 74 2e 0a 2a y checked out..*
8b6b0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d /.static int mem
8b6c0 73 79 73 33 53 69 7a 65 28 76 6f 69 64 20 2a 70 sys3Size(void *p
8b6d0 29 7b 0a 20 20 4d 65 6d 33 42 6c 6f 63 6b 20 2a ){. Mem3Block *
8b6e0 70 42 6c 6f 63 6b 3b 0a 20 20 69 66 28 20 70 3d pBlock;. if( p=
8b6f0 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 =0 ) return 0;.
8b700 20 70 42 6c 6f 63 6b 20 3d 20 28 4d 65 6d 33 42 pBlock = (Mem3B
8b710 6c 6f 63 6b 2a 29 70 3b 0a 20 20 61 73 73 65 72 lock*)p;. asser
8b720 74 28 20 28 70 42 6c 6f 63 6b 5b 2d 31 5d 2e 75 t( (pBlock[-1].u
8b730 2e 68 64 72 2e 73 69 7a 65 34 78 26 31 29 21 3d .hdr.size4x&1)!=
8b740 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 0 );. return (p
8b750 42 6c 6f 63 6b 5b 2d 31 5d 2e 75 2e 68 64 72 2e Block[-1].u.hdr.
8b760 73 69 7a 65 34 78 26 7e 33 29 2a 32 20 2d 20 34 size4x&~3)*2 - 4
8b770 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 ;.}../*.** Round
8b780 20 75 70 20 61 20 72 65 71 75 65 73 74 20 73 69 up a request si
8b790 7a 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20 76 ze to the next v
8b7a0 61 6c 69 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 alid allocation
8b7b0 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 size..*/.static
8b7c0 69 6e 74 20 6d 65 6d 73 79 73 33 52 6f 75 6e 64 int memsys3Round
8b7d0 75 70 28 69 6e 74 20 6e 29 7b 0a 20 20 69 66 28 up(int n){. if(
8b7e0 20 6e 3c 3d 31 32 20 29 7b 0a 20 20 20 20 72 65 n<=12 ){. re
8b7f0 74 75 72 6e 20 31 32 3b 0a 20 20 7d 65 6c 73 65 turn 12;. }else
8b800 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 28 6e {. return ((n
8b810 2b 31 31 29 26 7e 37 29 20 2d 20 34 3b 0a 20 20 +11)&~7) - 4;.
8b820 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 }.}../*.** Alloc
8b830 61 74 65 20 6e 42 79 74 65 73 20 6f 66 20 6d 65 ate nBytes of me
8b840 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 mory..*/.static
8b850 76 6f 69 64 20 2a 6d 65 6d 73 79 73 33 4d 61 6c void *memsys3Mal
8b860 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 73 29 7b loc(int nBytes){
8b870 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 . sqlite3_int64
8b880 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 6e *p;. assert( n
8b890 42 79 74 65 73 3e 30 20 29 3b 20 20 20 20 20 20 Bytes>0 );
8b8a0 20 20 20 20 2f 2a 20 6d 61 6c 6c 6f 63 2e 63 20 /* malloc.c
8b8b0 66 69 6c 74 65 72 73 20 6f 75 74 20 30 20 62 79 filters out 0 by
8b8c0 74 65 20 72 65 71 75 65 73 74 73 20 2a 2f 0a 20 te requests */.
8b8d0 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 29 3b memsys3Enter();
8b8e0 0a 20 20 70 20 3d 20 6d 65 6d 73 79 73 33 4d 61 . p = memsys3Ma
8b8f0 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 74 65 llocUnsafe(nByte
8b900 73 29 3b 0a 20 20 6d 65 6d 73 79 73 33 4c 65 61 s);. memsys3Lea
8b910 76 65 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 ve();. return (
8b920 76 6f 69 64 2a 29 70 3b 20 0a 7d 0a 0a 2f 2a 0a void*)p; .}../*.
8b930 2a 2a 20 46 72 65 65 20 6d 65 6d 6f 72 79 2e 0a ** Free memory..
8b940 2a 2f 0a 76 6f 69 64 20 6d 65 6d 73 79 73 33 46 */.void memsys3F
8b950 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 ree(void *pPrior
8b960 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 50 72 ){. assert( pPr
8b970 69 6f 72 20 29 3b 0a 20 20 6d 65 6d 73 79 73 33 ior );. memsys3
8b980 45 6e 74 65 72 28 29 3b 0a 20 20 6d 65 6d 73 79 Enter();. memsy
8b990 73 33 46 72 65 65 55 6e 73 61 66 65 28 70 50 72 s3FreeUnsafe(pPr
8b9a0 69 6f 72 29 3b 0a 20 20 6d 65 6d 73 79 73 33 4c ior);. memsys3L
8b9b0 65 61 76 65 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a eave();.}../*.**
8b9c0 20 43 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65 Change the size
8b9d0 20 6f 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20 of an existing
8b9e0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
8b9f0 6e 0a 2a 2f 0a 76 6f 69 64 20 2a 6d 65 6d 73 79 n.*/.void *memsy
8ba00 73 33 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a s3Realloc(void *
8ba10 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 pPrior, int nByt
8ba20 65 73 29 7b 0a 20 20 69 6e 74 20 6e 4f 6c 64 3b es){. int nOld;
8ba30 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 69 66 . void *p;. if
8ba40 28 20 70 50 72 69 6f 72 3d 3d 30 20 29 7b 0a 20 ( pPrior==0 ){.
8ba50 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 return sqlite
8ba60 33 5f 6d 61 6c 6c 6f 63 28 6e 42 79 74 65 73 29 3_malloc(nBytes)
8ba70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 42 79 74 ;. }. if( nByt
8ba80 65 73 3c 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c es<=0 ){. sql
8ba90 69 74 65 33 5f 66 72 65 65 28 70 50 72 69 6f 72 ite3_free(pPrior
8baa0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b );. return 0;
8bab0 0a 20 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 6d 65 . }. nOld = me
8bac0 6d 73 79 73 33 53 69 7a 65 28 70 50 72 69 6f 72 msys3Size(pPrior
8bad0 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3c );. if( nBytes<
8bae0 3d 6e 4f 6c 64 20 26 26 20 6e 42 79 74 65 73 3e =nOld && nBytes>
8baf0 3d 6e 4f 6c 64 2d 31 32 38 20 29 7b 0a 20 20 20 =nOld-128 ){.
8bb00 20 72 65 74 75 72 6e 20 70 50 72 69 6f 72 3b 0a return pPrior;.
8bb10 20 20 7d 0a 20 20 6d 65 6d 73 79 73 33 45 6e 74 }. memsys3Ent
8bb20 65 72 28 29 3b 0a 20 20 70 20 3d 20 6d 65 6d 73 er();. p = mems
8bb30 79 73 33 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 ys3MallocUnsafe(
8bb40 6e 42 79 74 65 73 29 3b 0a 20 20 69 66 28 20 70 nBytes);. if( p
8bb50 20 29 7b 0a 20 20 20 20 69 66 28 20 6e 4f 6c 64 ){. if( nOld
8bb60 3c 6e 42 79 74 65 73 20 29 7b 0a 20 20 20 20 20 <nBytes ){.
8bb70 20 6d 65 6d 63 70 79 28 70 2c 20 70 50 72 69 6f memcpy(p, pPrio
8bb80 72 2c 20 6e 4f 6c 64 29 3b 0a 20 20 20 20 7d 65 r, nOld);. }e
8bb90 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 lse{. memcp
8bba0 79 28 70 2c 20 70 50 72 69 6f 72 2c 20 6e 42 79 y(p, pPrior, nBy
8bbb0 74 65 73 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 tes);. }.
8bbc0 6d 65 6d 73 79 73 33 46 72 65 65 55 6e 73 61 66 memsys3FreeUnsaf
8bbd0 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 7d 0a 20 e(pPrior);. }.
8bbe0 20 6d 65 6d 73 79 73 33 4c 65 61 76 65 28 29 3b memsys3Leave();
8bbf0 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a . return p;.}..
8bc00 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 /*.** Initialize
8bc10 20 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f this module..*/
8bc20 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 .static int mems
8bc30 79 73 33 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f ys3Init(void *No
8bc40 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 tUsed){. UNUSED
8bc50 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 _PARAMETER(NotUs
8bc60 65 64 29 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 ed);. if( !sqli
8bc70 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
8bc80 70 48 65 61 70 20 29 7b 0a 20 20 20 20 72 65 74 pHeap ){. ret
8bc90 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 urn SQLITE_ERROR
8bca0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 6f 72 ;. }.. /* Stor
8bcb0 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 e a pointer to t
8bcc0 68 65 20 6d 65 6d 6f 72 79 20 62 6c 6f 63 6b 20 he memory block
8bcd0 69 6e 20 67 6c 6f 62 61 6c 20 73 74 72 75 63 74 in global struct
8bce0 75 72 65 20 6d 65 6d 33 2e 20 2a 2f 0a 20 20 61 ure mem3. */. a
8bcf0 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 4d 65 ssert( sizeof(Me
8bd00 6d 33 42 6c 6f 63 6b 29 3d 3d 38 20 29 3b 0a 20 m3Block)==8 );.
8bd10 20 6d 65 6d 33 2e 61 50 6f 6f 6c 20 3d 20 28 4d mem3.aPool = (M
8bd20 65 6d 33 42 6c 6f 63 6b 20 2a 29 73 71 6c 69 74 em3Block *)sqlit
8bd30 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
8bd40 48 65 61 70 3b 0a 20 20 6d 65 6d 33 2e 6e 50 6f Heap;. mem3.nPo
8bd50 6f 6c 20 3d 20 28 73 71 6c 69 74 65 33 47 6c 6f ol = (sqlite3Glo
8bd60 62 61 6c 43 6f 6e 66 69 67 2e 6e 48 65 61 70 20 balConfig.nHeap
8bd70 2f 20 73 69 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f / sizeof(Mem3Blo
8bd80 63 6b 29 29 20 2d 20 32 3b 0a 0a 20 20 2f 2a 20 ck)) - 2;.. /*
8bd90 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d Initialize the m
8bda0 61 73 74 65 72 20 62 6c 6f 63 6b 2e 20 2a 2f 0a aster block. */.
8bdb0 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 mem3.szMaster
8bdc0 3d 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 3b 0a 20 20 = mem3.nPool;.
8bdd0 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 20 3d 20 mem3.mnMaster =
8bde0 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 mem3.szMaster;.
8bdf0 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20 mem3.iMaster =
8be00 31 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 1;. mem3.aPool[
8be10 30 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 0].u.hdr.size4x
8be20 3d 20 28 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 = (mem3.szMaster
8be30 3c 3c 32 29 20 2b 20 32 3b 0a 20 20 6d 65 6d 33 <<2) + 2;. mem3
8be40 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f .aPool[mem3.nPoo
8be50 6c 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a l].u.hdr.prevSiz
8be60 65 20 3d 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 3b 0a e = mem3.nPool;.
8be70 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d mem3.aPool[mem
8be80 33 2e 6e 50 6f 6f 6c 5d 2e 75 2e 68 64 72 2e 73 3.nPool].u.hdr.s
8be90 69 7a 65 34 78 20 3d 20 31 3b 0a 0a 20 20 72 65 ize4x = 1;.. re
8bea0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
8beb0 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 69 }../*.** Deiniti
8bec0 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c alize this modul
8bed0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 e..*/.static voi
8bee0 64 20 6d 65 6d 73 79 73 33 53 68 75 74 64 6f 77 d memsys3Shutdow
8bef0 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 n(void *NotUsed)
8bf00 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d {. UNUSED_PARAM
8bf10 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 ETER(NotUsed);.
8bf20 20 6d 65 6d 33 2e 6d 75 74 65 78 20 3d 20 30 3b mem3.mutex = 0;
8bf30 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 0a 0a . return;.}....
8bf40 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 66 /*.** Open the f
8bf50 69 6c 65 20 69 6e 64 69 63 61 74 65 64 20 61 6e ile indicated an
8bf60 64 20 77 72 69 74 65 20 61 20 6c 6f 67 20 6f 66 d write a log of
8bf70 20 61 6c 6c 20 75 6e 66 72 65 65 64 20 6d 65 6d all unfreed mem
8bf80 6f 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 ory .** allocati
8bf90 6f 6e 73 20 69 6e 74 6f 20 74 68 61 74 20 6c 6f ons into that lo
8bfa0 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 g..*/.SQLITE_PRI
8bfb0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
8bfc0 33 4d 65 6d 73 79 73 33 44 75 6d 70 28 63 6f 6e 3Memsys3Dump(con
8bfd0 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 st char *zFilena
8bfe0 6d 65 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 me){.#ifdef SQLI
8bff0 54 45 5f 44 45 42 55 47 0a 20 20 46 49 4c 45 20 TE_DEBUG. FILE
8c000 2a 6f 75 74 3b 0a 20 20 75 33 32 20 69 2c 20 6a *out;. u32 i, j
8c010 3b 0a 20 20 75 33 32 20 73 69 7a 65 3b 0a 20 20 ;. u32 size;.
8c020 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 if( zFilename==0
8c030 20 7c 7c 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d || zFilename[0]
8c040 3d 3d 30 20 29 7b 0a 20 20 20 20 6f 75 74 20 3d ==0 ){. out =
8c050 20 73 74 64 6f 75 74 3b 0a 20 20 7d 65 6c 73 65 stdout;. }else
8c060 7b 0a 20 20 20 20 6f 75 74 20 3d 20 66 6f 70 65 {. out = fope
8c070 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 22 77 22 n(zFilename, "w"
8c080 29 3b 0a 20 20 20 20 69 66 28 20 6f 75 74 3d 3d );. if( out==
8c090 30 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 0 ){. fprin
8c0a0 74 66 28 73 74 64 65 72 72 2c 20 22 2a 2a 20 55 tf(stderr, "** U
8c0b0 6e 61 62 6c 65 20 74 6f 20 6f 75 74 70 75 74 20 nable to output
8c0c0 6d 65 6d 6f 72 79 20 64 65 62 75 67 20 6f 75 74 memory debug out
8c0d0 70 75 74 20 6c 6f 67 3a 20 25 73 20 2a 2a 5c 6e put log: %s **\n
8c0e0 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ",.
8c0f0 20 20 20 20 20 20 20 20 20 7a 46 69 6c 65 6e 61 zFilena
8c100 6d 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 me);. retur
8c110 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6d n;. }. }. m
8c120 65 6d 73 79 73 33 45 6e 74 65 72 28 29 3b 0a 20 emsys3Enter();.
8c130 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 43 fprintf(out, "C
8c140 48 55 4e 4b 53 3a 5c 6e 22 29 3b 0a 20 20 66 6f HUNKS:\n");. fo
8c150 72 28 69 3d 31 3b 20 69 3c 3d 6d 65 6d 33 2e 6e r(i=1; i<=mem3.n
8c160 50 6f 6f 6c 3b 20 69 2b 3d 73 69 7a 65 2f 34 29 Pool; i+=size/4)
8c170 7b 0a 20 20 20 20 73 69 7a 65 20 3d 20 6d 65 6d {. size = mem
8c180 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 3.aPool[i-1].u.h
8c190 64 72 2e 73 69 7a 65 34 78 3b 0a 20 20 20 20 69 dr.size4x;. i
8c1a0 66 28 20 73 69 7a 65 2f 34 3c 3d 31 20 29 7b 0a f( size/4<=1 ){.
8c1b0 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 fprintf(ou
8c1c0 74 2c 20 22 25 70 20 73 69 7a 65 20 65 72 72 6f t, "%p size erro
8c1d0 72 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f r\n", &mem3.aPoo
8c1e0 6c 5b 69 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 l[i]);. ass
8c1f0 65 72 74 28 20 30 20 29 3b 0a 20 20 20 20 20 20 ert( 0 );.
8c200 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
8c210 20 69 66 28 20 28 73 69 7a 65 26 31 29 3d 3d 30 if( (size&1)==0
8c220 20 26 26 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 && mem3.aPool[i
8c230 2b 73 69 7a 65 2f 34 2d 31 5d 2e 75 2e 68 64 72 +size/4-1].u.hdr
8c240 2e 70 72 65 76 53 69 7a 65 21 3d 73 69 7a 65 2f .prevSize!=size/
8c250 34 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 4 ){. fprin
8c260 74 66 28 6f 75 74 2c 20 22 25 70 20 74 61 69 6c tf(out, "%p tail
8c270 20 73 69 7a 65 20 64 6f 65 73 20 6e 6f 74 20 6d size does not m
8c280 61 74 63 68 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 atch\n", &mem3.a
8c290 50 6f 6f 6c 5b 69 5d 29 3b 0a 20 20 20 20 20 20 Pool[i]);.
8c2a0 61 73 73 65 72 74 28 20 30 20 29 3b 0a 20 20 20 assert( 0 );.
8c2b0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
8c2c0 20 20 20 20 69 66 28 20 28 28 6d 65 6d 33 2e 61 if( ((mem3.a
8c2d0 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2f 34 2d 31 5d Pool[i+size/4-1]
8c2e0 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 32 29 .u.hdr.size4x&2)
8c2f0 3e 3e 31 29 21 3d 28 73 69 7a 65 26 31 29 20 29 >>1)!=(size&1) )
8c300 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 {. fprintf(
8c310 6f 75 74 2c 20 22 25 70 20 74 61 69 6c 20 63 68 out, "%p tail ch
8c320 65 63 6b 6f 75 74 20 62 69 74 20 69 73 20 69 6e eckout bit is in
8c330 63 6f 72 72 65 63 74 5c 6e 22 2c 20 26 6d 65 6d correct\n", &mem
8c340 33 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0a 20 20 20 3.aPool[i]);.
8c350 20 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a assert( 0 );.
8c360 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
8c370 20 7d 0a 20 20 20 20 69 66 28 20 73 69 7a 65 26 }. if( size&
8c380 31 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 1 ){. fprin
8c390 74 66 28 6f 75 74 2c 20 22 25 70 20 25 36 64 20 tf(out, "%p %6d
8c3a0 62 79 74 65 73 20 63 68 65 63 6b 65 64 20 6f 75 bytes checked ou
8c3b0 74 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f t\n", &mem3.aPoo
8c3c0 6c 5b 69 5d 2c 20 28 73 69 7a 65 2f 34 29 2a 38 l[i], (size/4)*8
8c3d0 2d 38 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a -8);. }else{.
8c3e0 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 fprintf(ou
8c3f0 74 2c 20 22 25 70 20 25 36 64 20 62 79 74 65 73 t, "%p %6d bytes
8c400 20 66 72 65 65 25 73 5c 6e 22 2c 20 26 6d 65 6d free%s\n", &mem
8c410 33 2e 61 50 6f 6f 6c 5b 69 5d 2c 20 28 73 69 7a 3.aPool[i], (siz
8c420 65 2f 34 29 2a 38 2d 38 2c 0a 20 20 20 20 20 20 e/4)*8-8,.
8c430 20 20 20 20 20 20 20 20 20 20 20 20 69 3d 3d 6d i==m
8c440 65 6d 33 2e 69 4d 61 73 74 65 72 20 3f 20 22 20 em3.iMaster ? "
8c450 2a 2a 6d 61 73 74 65 72 2a 2a 22 20 3a 20 22 22 **master**" : ""
8c460 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 );. }. }. f
8c470 6f 72 28 69 3d 30 3b 20 69 3c 4d 58 5f 53 4d 41 or(i=0; i<MX_SMA
8c480 4c 4c 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 LL-1; i++){.
8c490 69 66 28 20 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c if( mem3.aiSmall
8c4a0 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 [i]==0 ) continu
8c4b0 65 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f e;. fprintf(o
8c4c0 75 74 2c 20 22 73 6d 61 6c 6c 28 25 32 64 29 3a ut, "small(%2d):
8c4d0 22 2c 20 69 29 3b 0a 20 20 20 20 66 6f 72 28 6a ", i);. for(j
8c4e0 20 3d 20 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b = mem3.aiSmall[
8c4f0 69 5d 3b 20 6a 3e 30 3b 20 6a 3d 6d 65 6d 33 2e i]; j>0; j=mem3.
8c500 61 50 6f 6f 6c 5b 6a 5d 2e 75 2e 6c 69 73 74 2e aPool[j].u.list.
8c510 6e 65 78 74 29 7b 0a 20 20 20 20 20 20 66 70 72 next){. fpr
8c520 69 6e 74 66 28 6f 75 74 2c 20 22 20 25 70 28 25 intf(out, " %p(%
8c530 64 29 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c d)", &mem3.aPool
8c540 5b 6a 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 [j],.
8c550 20 20 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a (mem3.aPool[j
8c560 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
8c570 2f 34 29 2a 38 2d 38 29 3b 0a 20 20 20 20 7d 0a /4)*8-8);. }.
8c580 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c fprintf(out,
8c590 20 22 5c 6e 22 29 3b 20 0a 20 20 7d 0a 20 20 66 "\n"); . }. f
8c5a0 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f 48 41 53 48 or(i=0; i<N_HASH
8c5b0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 ; i++){. if(
8c5c0 6d 65 6d 33 2e 61 69 48 61 73 68 5b 69 5d 3d 3d mem3.aiHash[i]==
8c5d0 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 0 ) continue;.
8c5e0 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
8c5f0 68 61 73 68 28 25 32 64 29 3a 22 2c 20 69 29 3b hash(%2d):", i);
8c600 0a 20 20 20 20 66 6f 72 28 6a 20 3d 20 6d 65 6d . for(j = mem
8c610 33 2e 61 69 48 61 73 68 5b 69 5d 3b 20 6a 3e 30 3.aiHash[i]; j>0
8c620 3b 20 6a 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a ; j=mem3.aPool[j
8c630 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 29 7b 0a ].u.list.next){.
8c640 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 fprintf(ou
8c650 74 2c 20 22 20 25 70 28 25 64 29 22 2c 20 26 6d t, " %p(%d)", &m
8c660 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2c 0a 20 20 em3.aPool[j],.
8c670 20 20 20 20 20 20 20 20 20 20 20 20 28 6d 65 6d (mem
8c680 33 2e 61 50 6f 6f 6c 5b 6a 2d 31 5d 2e 75 2e 68 3.aPool[j-1].u.h
8c690 64 72 2e 73 69 7a 65 34 78 2f 34 29 2a 38 2d 38 dr.size4x/4)*8-8
8c6a0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 70 72 );. }. fpr
8c6b0 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b intf(out, "\n");
8c6c0 20 0a 20 20 7d 0a 20 20 66 70 72 69 6e 74 66 28 . }. fprintf(
8c6d0 6f 75 74 2c 20 22 6d 61 73 74 65 72 3d 25 64 5c out, "master=%d\
8c6e0 6e 22 2c 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 n", mem3.iMaster
8c6f0 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 );. fprintf(out
8c700 2c 20 22 6e 6f 77 55 73 65 64 3d 25 64 5c 6e 22 , "nowUsed=%d\n"
8c710 2c 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a 38 20 2d , mem3.nPool*8 -
8c720 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2a 38 mem3.szMaster*8
8c730 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 );. fprintf(out
8c740 2c 20 22 6d 78 55 73 65 64 3d 25 64 5c 6e 22 2c , "mxUsed=%d\n",
8c750 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a 38 20 2d 20 mem3.nPool*8 -
8c760 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 2a 38 29 mem3.mnMaster*8)
8c770 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ;. sqlite3_mute
8c780 78 5f 6c 65 61 76 65 28 6d 65 6d 33 2e 6d 75 74 x_leave(mem3.mut
8c790 65 78 29 3b 0a 20 20 69 66 28 20 6f 75 74 3d 3d ex);. if( out==
8c7a0 73 74 64 6f 75 74 20 29 7b 0a 20 20 20 20 66 66 stdout ){. ff
8c7b0 6c 75 73 68 28 73 74 64 6f 75 74 29 3b 0a 20 20 lush(stdout);.
8c7c0 7d 65 6c 73 65 7b 0a 20 20 20 20 66 63 6c 6f 73 }else{. fclos
8c7d0 65 28 6f 75 74 29 3b 0a 20 20 7d 0a 23 65 6c 73 e(out);. }.#els
8c7e0 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d e. UNUSED_PARAM
8c7f0 45 54 45 52 28 7a 46 69 6c 65 6e 61 6d 65 29 3b ETER(zFilename);
8c800 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a .#endif.}../*.**
8c810 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
8c820 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e the only routin
8c830 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 77 e in this file w
8c840 69 74 68 20 65 78 74 65 72 6e 61 6c 20 0a 2a 2a ith external .**
8c850 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 linkage..**.**
8c860 50 6f 70 75 6c 61 74 65 20 74 68 65 20 6c 6f 77 Populate the low
8c870 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c -level memory al
8c880 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f location functio
8c890 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a 2a 2a n pointers in.**
8c8a0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
8c8b0 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 6f 69 6e nfig.m with poin
8c8c0 74 65 72 73 20 74 6f 20 74 68 65 20 72 6f 75 74 ters to the rout
8c8d0 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69 6c ines in this fil
8c8e0 65 2e 20 54 68 65 0a 2a 2a 20 61 72 67 75 6d 65 e. The.** argume
8c8f0 6e 74 73 20 73 70 65 63 69 66 79 20 74 68 65 20 nts specify the
8c900 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 block of memory
8c910 74 6f 20 6d 61 6e 61 67 65 2e 0a 2a 2a 0a 2a 2a to manage..**.**
8c920 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
8c930 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 62 79 20 only called by
8c940 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 sqlite3_config()
8c950 2c 20 61 6e 64 20 74 68 65 72 65 66 6f 72 65 0a , and therefore.
8c960 2a 2a 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 ** is not requir
8c970 65 64 20 74 6f 20 62 65 20 74 68 72 65 61 64 73 ed to be threads
8c980 61 66 65 20 28 69 74 20 69 73 20 6e 6f 74 29 2e afe (it is not).
8c990 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
8c9a0 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 TE const sqlite3
8c9b0 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 2a 73 71 _mem_methods *sq
8c9c0 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73 79 lite3MemGetMemsy
8c9d0 73 33 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 s3(void){. stat
8c9e0 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 ic const sqlite3
8c9f0 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6d 65 6d _mem_methods mem
8ca00 70 6f 6f 6c 4d 65 74 68 6f 64 73 20 3d 20 7b 0a poolMethods = {.
8ca10 20 20 20 20 20 6d 65 6d 73 79 73 33 4d 61 6c 6c memsys3Mall
8ca20 6f 63 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 oc,. memsys3
8ca30 46 72 65 65 2c 0a 20 20 20 20 20 6d 65 6d 73 79 Free,. memsy
8ca40 73 33 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 s3Realloc,.
8ca50 6d 65 6d 73 79 73 33 53 69 7a 65 2c 0a 20 20 20 memsys3Size,.
8ca60 20 20 6d 65 6d 73 79 73 33 52 6f 75 6e 64 75 70 memsys3Roundup
8ca70 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 49 6e ,. memsys3In
8ca80 69 74 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 it,. memsys3
8ca90 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 20 20 30 Shutdown,. 0
8caa0 0a 20 20 7d 3b 0a 20 20 72 65 74 75 72 6e 20 26 . };. return &
8cab0 6d 65 6d 70 6f 6f 6c 4d 65 74 68 6f 64 73 3b 0a mempoolMethods;.
8cac0 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c }..#endif /* SQL
8cad0 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 ITE_ENABLE_MEMSY
8cae0 53 33 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a S3 */../********
8caf0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 ****** End of me
8cb00 6d 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a m3.c ***********
8cb10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8cb20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8cb30 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
8cb40 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
8cb50 65 20 6d 65 6d 35 2e 63 20 2a 2a 2a 2a 2a 2a 2a e mem5.c *******
8cb60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8cb70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8cb80 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
8cb90 37 20 4f 63 74 6f 62 65 72 20 31 34 0a 2a 2a 0a 7 October 14.**.
8cba0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
8cbb0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
8cbc0 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
8cbd0 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
8cbe0 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
8cbf0 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
8cc00 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
8cc10 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
8cc20 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
8cc30 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
8cc40 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
8cc50 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
8cc60 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
8cc70 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
8cc80 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
8cc90 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
8cca0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
8ccb0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
8ccc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8ccd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8cce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8ccf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 ***********.** T
8cd00 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e his file contain
8cd10 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e s the C function
8cd20 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 s that implement
8cd30 20 61 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c a memory.** all
8cd40 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 ocation subsyste
8cd50 6d 20 66 6f 72 20 75 73 65 20 62 79 20 53 51 4c m for use by SQL
8cd60 69 74 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ite. .**.** This
8cd70 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 version of the
8cd80 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
8cd90 6e 20 73 75 62 73 79 73 74 65 6d 20 6f 6d 69 74 n subsystem omit
8cda0 73 20 61 6c 6c 0a 2a 2a 20 75 73 65 20 6f 66 20 s all.** use of
8cdb0 6d 61 6c 6c 6f 63 28 29 2e 20 54 68 65 20 61 70 malloc(). The ap
8cdc0 70 6c 69 63 61 74 69 6f 6e 20 67 69 76 65 73 20 plication gives
8cdd0 53 51 4c 69 74 65 20 61 20 62 6c 6f 63 6b 20 6f SQLite a block o
8cde0 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 65 66 6f f memory.** befo
8cdf0 72 65 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 re calling sqlit
8ce00 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 e3_initialize()
8ce10 66 72 6f 6d 20 77 68 69 63 68 20 61 6c 6c 6f 63 from which alloc
8ce20 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 6d 61 ations.** are ma
8ce30 64 65 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20 de and returned
8ce40 62 79 20 74 68 65 20 78 4d 61 6c 6c 6f 63 28 29 by the xMalloc()
8ce50 20 61 6e 64 20 78 52 65 61 6c 6c 6f 63 28 29 20 and xRealloc()
8ce60 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 .** implementati
8ce70 6f 6e 73 2e 20 4f 6e 63 65 20 73 71 6c 69 74 65 ons. Once sqlite
8ce80 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 68 3_initialize() h
8ce90 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c 0a as been called,.
8cea0 2a 2a 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 ** the amount of
8ceb0 20 6d 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c memory availabl
8cec0 65 20 74 6f 20 53 51 4c 69 74 65 20 69 73 20 66 e to SQLite is f
8ced0 69 78 65 64 20 61 6e 64 20 63 61 6e 6e 6f 74 0a ixed and cannot.
8cee0 2a 2a 20 62 65 20 63 68 61 6e 67 65 64 2e 0a 2a ** be changed..*
8cef0 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f *.** This versio
8cf00 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 n of the memory
8cf10 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 allocation subsy
8cf20 73 74 65 6d 20 69 73 20 69 6e 63 6c 75 64 65 64 stem is included
8cf30 0a 2a 2a 20 69 6e 20 74 68 65 20 62 75 69 6c 64 .** in the build
8cf40 20 6f 6e 6c 79 20 69 66 20 53 51 4c 49 54 45 5f only if SQLITE_
8cf50 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 20 69 ENABLE_MEMSYS5 i
8cf60 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a s defined..**.**
8cf70 20 54 68 69 73 20 6d 65 6d 6f 72 79 20 61 6c 6c This memory all
8cf80 6f 63 61 74 6f 72 20 75 73 65 73 20 74 68 65 20 ocator uses the
8cf90 66 6f 6c 6c 6f 77 69 6e 67 20 61 6c 67 6f 72 69 following algori
8cfa0 74 68 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 thm:.**.** 1.
8cfb0 20 41 6c 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f All memory allo
8cfc0 63 61 74 69 6f 6e 73 20 73 69 7a 65 73 20 61 72 cations sizes ar
8cfd0 65 20 72 6f 75 6e 64 65 64 20 75 70 20 74 6f 20 e rounded up to
8cfe0 61 20 70 6f 77 65 72 20 6f 66 20 32 2e 0a 2a 2a a power of 2..**
8cff0 0a 2a 2a 20 20 20 32 2e 20 20 49 66 20 74 77 6f .** 2. If two
8d000 20 61 64 6a 61 63 65 6e 74 20 66 72 65 65 20 62 adjacent free b
8d010 6c 6f 63 6b 73 20 61 72 65 20 74 68 65 20 68 61 locks are the ha
8d020 6c 76 65 73 20 6f 66 20 61 20 6c 61 72 67 65 72 lves of a larger
8d030 20 62 6c 6f 63 6b 2c 0a 2a 2a 20 20 20 20 20 20 block,.**
8d040 20 74 68 65 6e 20 74 68 65 20 74 77 6f 20 62 6c then the two bl
8d050 6f 63 6b 73 20 61 72 65 20 63 6f 61 6c 65 73 65 ocks are coalese
8d060 64 20 69 6e 74 6f 20 74 68 65 20 73 69 6e 67 6c d into the singl
8d070 65 20 6c 61 72 67 65 72 20 62 6c 6f 63 6b 2e 0a e larger block..
8d080 2a 2a 0a 2a 2a 20 20 20 33 2e 20 20 4e 65 77 20 **.** 3. New
8d090 6d 65 6d 6f 72 79 20 69 73 20 61 6c 6c 6f 63 61 memory is alloca
8d0a0 74 65 64 20 66 72 6f 6d 20 74 68 65 20 66 69 72 ted from the fir
8d0b0 73 74 20 61 76 61 69 6c 61 62 6c 65 20 66 72 65 st available fre
8d0c0 65 20 62 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 e block..**.** T
8d0d0 68 69 73 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 his algorithm is
8d0e0 20 64 65 73 63 72 69 62 65 64 20 69 6e 3a 20 4a described in: J
8d0f0 2e 20 4d 2e 20 52 6f 62 73 6f 6e 2e 20 22 42 6f . M. Robson. "Bo
8d100 75 6e 64 73 20 66 6f 72 20 53 6f 6d 65 20 46 75 unds for Some Fu
8d110 6e 63 74 69 6f 6e 73 0a 2a 2a 20 43 6f 6e 63 65 nctions.** Conce
8d120 72 6e 69 6e 67 20 44 79 6e 61 6d 69 63 20 53 74 rning Dynamic St
8d130 6f 72 61 67 65 20 41 6c 6c 6f 63 61 74 69 6f 6e orage Allocation
8d140 22 2e 20 4a 6f 75 72 6e 61 6c 20 6f 66 20 74 68 ". Journal of th
8d150 65 20 41 73 73 6f 63 69 61 74 69 6f 6e 20 66 6f e Association fo
8d160 72 0a 2a 2a 20 43 6f 6d 70 75 74 69 6e 67 20 4d r.** Computing M
8d170 61 63 68 69 6e 65 72 79 2c 20 56 6f 6c 75 6d 65 achinery, Volume
8d180 20 32 31 2c 20 4e 75 6d 62 65 72 20 38 2c 20 4a 21, Number 8, J
8d190 75 6c 79 20 31 39 37 34 2c 20 70 61 67 65 73 20 uly 1974, pages
8d1a0 34 39 31 2d 34 39 39 2e 0a 2a 2a 20 0a 2a 2a 20 491-499..** .**
8d1b0 4c 65 74 20 6e 20 62 65 20 74 68 65 20 73 69 7a Let n be the siz
8d1c0 65 20 6f 66 20 74 68 65 20 6c 61 72 67 65 73 74 e of the largest
8d1d0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 69 76 69 allocation divi
8d1e0 64 65 64 20 62 79 20 74 68 65 20 6d 69 6e 69 6d ded by the minim
8d1f0 75 6d 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e um.** allocation
8d200 20 73 69 7a 65 20 28 61 66 74 65 72 20 72 6f 75 size (after rou
8d210 6e 64 69 6e 67 20 61 6c 6c 20 73 69 7a 65 73 20 nding all sizes
8d220 75 70 20 74 6f 20 61 20 70 6f 77 65 72 20 6f 66 up to a power of
8d230 20 32 2e 29 20 20 4c 65 74 20 4d 0a 2a 2a 20 62 2.) Let M.** b
8d240 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6d e the maximum am
8d250 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 65 ount of memory e
8d260 76 65 72 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 ver outstanding
8d270 61 74 20 6f 6e 65 20 74 69 6d 65 2e 20 20 4c 65 at one time. Le
8d280 74 0a 2a 2a 20 4e 20 62 65 20 74 68 65 20 74 6f t.** N be the to
8d290 74 61 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 tal amount of me
8d2a0 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 66 mory available f
8d2b0 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 or allocation.
8d2c0 52 6f 62 73 6f 6e 0a 2a 2a 20 70 72 6f 76 65 64 Robson.** proved
8d2d0 20 74 68 61 74 20 74 68 69 73 20 6d 65 6d 6f 72 that this memor
8d2e0 79 20 61 6c 6c 6f 63 61 74 6f 72 20 77 69 6c 6c y allocator will
8d2f0 20 6e 65 76 65 72 20 62 72 65 61 6b 64 6f 77 6e never breakdown
8d300 20 64 75 65 20 74 6f 20 0a 2a 2a 20 66 72 61 67 due to .** frag
8d310 6d 65 6e 74 61 74 69 6f 6e 20 61 73 20 6c 6f 6e mentation as lon
8d320 67 20 61 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 g as the followi
8d330 6e 67 20 63 6f 6e 73 74 72 61 69 6e 74 20 68 6f ng constraint ho
8d340 6c 64 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 lds:.**.**
8d350 4e 20 3e 3d 20 20 4d 2a 28 31 20 2b 20 6c 6f 67 N >= M*(1 + log
8d360 32 28 6e 29 2f 32 29 20 2d 20 6e 20 2b 20 31 0a 2(n)/2) - n + 1.
8d370 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
8d380 33 5f 73 74 61 74 75 73 28 29 20 6c 6f 67 69 63 3_status() logic
8d390 20 74 72 61 63 6b 73 20 74 68 65 20 6d 61 78 69 tracks the maxi
8d3a0 6d 75 6d 20 76 61 6c 75 65 73 20 6f 66 20 6e 20 mum values of n
8d3b0 61 6e 64 20 4d 20 73 6f 0a 2a 2a 20 74 68 61 74 and M so.** that
8d3c0 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 an application
8d3d0 63 61 6e 2c 20 61 74 20 61 6e 79 20 74 69 6d 65 can, at any time
8d3e0 2c 20 76 65 72 69 66 79 20 74 68 69 73 20 63 6f , verify this co
8d3f0 6e 73 74 72 61 69 6e 74 2e 0a 2a 2f 0a 0a 2f 2a nstraint..*/../*
8d400 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e .** This version
8d410 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 of the memory a
8d420 6c 6c 6f 63 61 74 6f 72 20 69 73 20 75 73 65 64 llocator is used
8d430 20 6f 6e 6c 79 20 77 68 65 6e 20 0a 2a 2a 20 53 only when .** S
8d440 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d QLITE_ENABLE_MEM
8d450 53 59 53 35 20 69 73 20 64 65 66 69 6e 65 64 2e SYS5 is defined.
8d460 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
8d470 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 E_ENABLE_MEMSYS5
8d480 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 69 6e 69 6d 75 ../*.** A minimu
8d490 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 m allocation is
8d4a0 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 an instance of t
8d4b0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 he following str
8d4c0 75 63 74 75 72 65 2e 0a 2a 2a 20 4c 61 72 67 65 ucture..** Large
8d4d0 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 72 r allocations ar
8d4e0 65 20 61 6e 20 61 72 72 61 79 20 6f 66 20 74 68 e an array of th
8d4f0 65 73 65 20 73 74 72 75 63 74 75 72 65 73 20 77 ese structures w
8d500 68 65 72 65 20 74 68 65 0a 2a 2a 20 73 69 7a 65 here the.** size
8d510 20 6f 66 20 74 68 65 20 61 72 72 61 79 20 69 73 of the array is
8d520 20 61 20 70 6f 77 65 72 20 6f 66 20 32 2e 0a 2a a power of 2..*
8d530 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 *.** The size of
8d540 20 74 68 69 73 20 6f 62 6a 65 63 74 20 6d 75 73 this object mus
8d550 74 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 t be a power of
8d560 74 77 6f 2e 20 20 54 68 61 74 20 66 61 63 74 20 two. That fact
8d570 69 73 0a 2a 2a 20 76 65 72 69 66 69 65 64 20 69 is.** verified i
8d580 6e 20 6d 65 6d 73 79 73 35 49 6e 69 74 28 29 2e n memsys5Init().
8d590 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
8d5a0 63 74 20 4d 65 6d 35 4c 69 6e 6b 20 4d 65 6d 35 ct Mem5Link Mem5
8d5b0 4c 69 6e 6b 3b 0a 73 74 72 75 63 74 20 4d 65 6d Link;.struct Mem
8d5c0 35 4c 69 6e 6b 20 7b 0a 20 20 69 6e 74 20 6e 65 5Link {. int ne
8d5d0 78 74 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 xt; /* Ind
8d5e0 65 78 20 6f 66 20 6e 65 78 74 20 66 72 65 65 20 ex of next free
8d5f0 63 68 75 6e 6b 20 2a 2f 0a 20 20 69 6e 74 20 70 chunk */. int p
8d600 72 65 76 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e rev; /* In
8d610 64 65 78 20 6f 66 20 70 72 65 76 69 6f 75 73 20 dex of previous
8d620 66 72 65 65 20 63 68 75 6e 6b 20 2a 2f 0a 7d 3b free chunk */.};
8d630 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 ../*.** Maximum
8d640 73 69 7a 65 20 6f 66 20 61 6e 79 20 61 6c 6c 6f size of any allo
8d650 63 61 74 69 6f 6e 20 69 73 20 28 28 31 3c 3c 4c cation is ((1<<L
8d660 4f 47 4d 41 58 29 2a 6d 65 6d 35 2e 73 7a 41 74 OGMAX)*mem5.szAt
8d670 6f 6d 29 2e 20 53 69 6e 63 65 0a 2a 2a 20 6d 65 om). Since.** me
8d680 6d 35 2e 73 7a 41 74 6f 6d 20 69 73 20 61 6c 77 m5.szAtom is alw
8d690 61 79 73 20 61 74 20 6c 65 61 73 74 20 38 20 61 ays at least 8 a
8d6a0 6e 64 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 nd 32-bit intege
8d6b0 72 73 20 61 72 65 20 75 73 65 64 2c 0a 2a 2a 20 rs are used,.**
8d6c0 69 74 20 69 73 20 6e 6f 74 20 61 63 74 75 61 6c it is not actual
8d6d0 6c 79 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 72 ly possible to r
8d6e0 65 61 63 68 20 74 68 69 73 20 6c 69 6d 69 74 2e each this limit.
8d6f0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4c 4f 47 4d .*/.#define LOGM
8d700 41 58 20 33 30 0a 0a 2f 2a 0a 2a 2a 20 4d 61 73 AX 30../*.** Mas
8d710 6b 73 20 75 73 65 64 20 66 6f 72 20 6d 65 6d 35 ks used for mem5
8d720 2e 61 43 74 72 6c 5b 5d 20 65 6c 65 6d 65 6e 74 .aCtrl[] element
8d730 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 54 s..*/.#define CT
8d740 52 4c 5f 4c 4f 47 53 49 5a 45 20 20 30 78 31 66 RL_LOGSIZE 0x1f
8d750 20 20 20 20 2f 2a 20 4c 6f 67 32 20 53 69 7a 65 /* Log2 Size
8d760 20 6f 66 20 74 68 69 73 20 62 6c 6f 63 6b 20 2a of this block *
8d770 2f 0a 23 64 65 66 69 6e 65 20 43 54 52 4c 5f 46 /.#define CTRL_F
8d780 52 45 45 20 20 20 20 20 30 78 32 30 20 20 20 20 REE 0x20
8d790 2f 2a 20 54 72 75 65 20 69 66 20 6e 6f 74 20 63 /* True if not c
8d7a0 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a 0a 2f hecked out */../
8d7b0 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 *.** All of the
8d7c0 73 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 static variables
8d7d0 20 75 73 65 64 20 62 79 20 74 68 69 73 20 6d 6f used by this mo
8d7e0 64 75 6c 65 20 61 72 65 20 63 6f 6c 6c 65 63 74 dule are collect
8d7f0 65 64 0a 2a 2a 20 69 6e 74 6f 20 61 20 73 69 6e ed.** into a sin
8d800 67 6c 65 20 73 74 72 75 63 74 75 72 65 20 6e 61 gle structure na
8d810 6d 65 64 20 22 6d 65 6d 35 22 2e 20 20 54 68 69 med "mem5". Thi
8d820 73 20 69 73 20 74 6f 20 6b 65 65 70 20 74 68 65 s is to keep the
8d830 0a 2a 2a 20 73 74 61 74 69 63 20 76 61 72 69 61 .** static varia
8d840 62 6c 65 73 20 6f 72 67 61 6e 69 7a 65 64 20 61 bles organized a
8d850 6e 64 20 74 6f 20 72 65 64 75 63 65 20 6e 61 6d nd to reduce nam
8d860 65 73 70 61 63 65 20 70 6f 6c 6c 75 74 69 6f 6e espace pollution
8d870 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 6d 6f .** when this mo
8d880 64 75 6c 65 20 69 73 20 63 6f 6d 62 69 6e 65 64 dule is combined
8d890 20 77 69 74 68 20 6f 74 68 65 72 20 69 6e 20 74 with other in t
8d8a0 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 2e he amalgamation.
8d8b0 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54 .*/.static SQLIT
8d8c0 45 5f 57 53 44 20 73 74 72 75 63 74 20 4d 65 6d E_WSD struct Mem
8d8d0 35 47 6c 6f 62 61 6c 20 7b 0a 20 20 2f 2a 0a 20 5Global {. /*.
8d8e0 20 2a 2a 20 4d 65 6d 6f 72 79 20 61 76 61 69 6c ** Memory avail
8d8f0 61 62 6c 65 20 66 6f 72 20 61 6c 6c 6f 63 61 74 able for allocat
8d900 69 6f 6e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 73 ion. */. int s
8d910 7a 41 74 6f 6d 3b 20 20 20 20 20 20 2f 2a 20 53 zAtom; /* S
8d920 6d 61 6c 6c 65 73 74 20 70 6f 73 73 69 62 6c 65 mallest possible
8d930 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 62 allocation in b
8d940 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 ytes */. int nB
8d950 6c 6f 63 6b 3b 20 20 20 20 20 20 2f 2a 20 4e 75 lock; /* Nu
8d960 6d 62 65 72 20 6f 66 20 73 7a 41 74 6f 6d 20 73 mber of szAtom s
8d970 69 7a 65 64 20 62 6c 6f 63 6b 73 20 69 6e 20 7a ized blocks in z
8d980 50 6f 6f 6c 20 2a 2f 0a 20 20 75 38 20 2a 7a 50 Pool */. u8 *zP
8d990 6f 6f 6c 3b 20 20 20 20 20 20 20 2f 2a 20 4d 65 ool; /* Me
8d9a0 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 74 mory available t
8d9b0 6f 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 2a o be allocated *
8d9c0 2f 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d /. . /*. ** M
8d9d0 75 74 65 78 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 utex to control
8d9e0 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 6d 65 access to the me
8d9f0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
8da00 73 75 62 73 79 73 74 65 6d 2e 0a 20 20 2a 2f 0a subsystem.. */.
8da10 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 sqlite3_mutex
8da20 2a 6d 75 74 65 78 3b 0a 0a 20 20 2f 2a 0a 20 20 *mutex;.. /*.
8da30 2a 2a 20 50 65 72 66 6f 72 6d 61 6e 63 65 20 73 ** Performance s
8da40 74 61 74 69 73 74 69 63 73 0a 20 20 2a 2f 0a 20 tatistics. */.
8da50 20 75 36 34 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 u64 nAlloc;
8da60 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 /* Total nu
8da70 6d 62 65 72 20 6f 66 20 63 61 6c 6c 73 20 74 6f mber of calls to
8da80 20 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 20 75 36 34 malloc */. u64
8da90 20 74 6f 74 61 6c 41 6c 6c 6f 63 3b 20 20 20 20 totalAlloc;
8daa0 20 2f 2a 20 54 6f 74 61 6c 20 6f 66 20 61 6c 6c /* Total of all
8dab0 20 6d 61 6c 6c 6f 63 20 63 61 6c 6c 73 20 2d 20 malloc calls -
8dac0 69 6e 63 6c 75 64 65 73 20 69 6e 74 65 72 6e 61 includes interna
8dad0 6c 20 66 72 61 67 20 2a 2f 0a 20 20 75 36 34 20 l frag */. u64
8dae0 74 6f 74 61 6c 45 78 63 65 73 73 3b 20 20 20 20 totalExcess;
8daf0 2f 2a 20 54 6f 74 61 6c 20 69 6e 74 65 72 6e 61 /* Total interna
8db00 6c 20 66 72 61 67 6d 65 6e 74 61 74 69 6f 6e 20 l fragmentation
8db10 2a 2f 0a 20 20 75 33 32 20 63 75 72 72 65 6e 74 */. u32 current
8db20 4f 75 74 3b 20 20 20 20 20 2f 2a 20 43 75 72 72 Out; /* Curr
8db30 65 6e 74 20 63 68 65 63 6b 6f 75 74 2c 20 69 6e ent checkout, in
8db40 63 6c 75 64 69 6e 67 20 69 6e 74 65 72 6e 61 6c cluding internal
8db50 20 66 72 61 67 6d 65 6e 74 61 74 69 6f 6e 20 2a fragmentation *
8db60 2f 0a 20 20 75 33 32 20 63 75 72 72 65 6e 74 43 /. u32 currentC
8db70 6f 75 6e 74 3b 20 20 20 2f 2a 20 43 75 72 72 65 ount; /* Curre
8db80 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 73 nt number of dis
8db90 74 69 6e 63 74 20 63 68 65 63 6b 6f 75 74 73 20 tinct checkouts
8dba0 2a 2f 0a 20 20 75 33 32 20 6d 61 78 4f 75 74 3b */. u32 maxOut;
8dbb0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 /* Maxi
8dbc0 6d 75 6d 20 69 6e 73 74 61 6e 74 61 6e 65 6f 75 mum instantaneou
8dbd0 73 20 63 75 72 72 65 6e 74 4f 75 74 20 2a 2f 0a s currentOut */.
8dbe0 20 20 75 33 32 20 6d 61 78 43 6f 75 6e 74 3b 20 u32 maxCount;
8dbf0 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d /* Maximum
8dc00 20 69 6e 73 74 61 6e 74 61 6e 65 6f 75 73 20 63 instantaneous c
8dc10 75 72 72 65 6e 74 43 6f 75 6e 74 20 2a 2f 0a 20 urrentCount */.
8dc20 20 75 33 32 20 6d 61 78 52 65 71 75 65 73 74 3b u32 maxRequest;
8dc30 20 20 20 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 /* Largest
8dc40 61 6c 6c 6f 63 61 74 69 6f 6e 20 28 65 78 63 6c allocation (excl
8dc50 75 73 69 76 65 20 6f 66 20 69 6e 74 65 72 6e 61 usive of interna
8dc60 6c 20 66 72 61 67 29 20 2a 2f 0a 20 20 0a 20 20 l frag) */. .
8dc70 2f 2a 0a 20 20 2a 2a 20 4c 69 73 74 73 20 6f 66 /*. ** Lists of
8dc80 20 66 72 65 65 20 62 6c 6f 63 6b 73 2e 20 20 61 free blocks. a
8dc90 69 46 72 65 65 6c 69 73 74 5b 30 5d 20 69 73 20 iFreelist[0] is
8dca0 61 20 6c 69 73 74 20 6f 66 20 66 72 65 65 20 62 a list of free b
8dcb0 6c 6f 63 6b 73 20 6f 66 0a 20 20 2a 2a 20 73 69 locks of. ** si
8dcc0 7a 65 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 2e 20 ze mem5.szAtom.
8dcd0 20 61 69 46 72 65 65 6c 69 73 74 5b 31 5d 20 68 aiFreelist[1] h
8dce0 6f 6c 64 73 20 62 6c 6f 63 6b 73 20 6f 66 20 73 olds blocks of s
8dcf0 69 7a 65 20 73 7a 41 74 6f 6d 2a 32 2e 0a 20 20 ize szAtom*2..
8dd00 2a 2a 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e ** and so forth.
8dd10 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 61 69 46 72 . */. int aiFr
8dd20 65 65 6c 69 73 74 5b 4c 4f 47 4d 41 58 2b 31 5d eelist[LOGMAX+1]
8dd30 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 53 70 61 ;.. /*. ** Spa
8dd40 63 65 20 66 6f 72 20 74 72 61 63 6b 69 6e 67 20 ce for tracking
8dd50 77 68 69 63 68 20 62 6c 6f 63 6b 73 20 61 72 65 which blocks are
8dd60 20 63 68 65 63 6b 65 64 20 6f 75 74 20 61 6e 64 checked out and
8dd70 20 74 68 65 20 73 69 7a 65 0a 20 20 2a 2a 20 6f the size. ** o
8dd80 66 20 65 61 63 68 20 62 6c 6f 63 6b 2e 20 20 4f f each block. O
8dd90 6e 65 20 62 79 74 65 20 70 65 72 20 62 6c 6f 63 ne byte per bloc
8dda0 6b 2e 0a 20 20 2a 2f 0a 20 20 75 38 20 2a 61 43 k.. */. u8 *aC
8ddb0 74 72 6c 3b 0a 0a 7d 20 6d 65 6d 35 20 3d 20 7b trl;..} mem5 = {
8ddc0 20 30 20 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 63 63 0 };../*.** Acc
8ddd0 65 73 73 20 74 68 65 20 73 74 61 74 69 63 20 76 ess the static v
8dde0 61 72 69 61 62 6c 65 20 74 68 72 6f 75 67 68 20 ariable through
8ddf0 61 20 6d 61 63 72 6f 20 66 6f 72 20 53 51 4c 49 a macro for SQLI
8de00 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 2a 2f 0a 23 TE_OMIT_WSD.*/.#
8de10 64 65 66 69 6e 65 20 6d 65 6d 35 20 47 4c 4f 42 define mem5 GLOB
8de20 41 4c 28 73 74 72 75 63 74 20 4d 65 6d 35 47 6c AL(struct Mem5Gl
8de30 6f 62 61 6c 2c 20 6d 65 6d 35 29 0a 0a 2f 2a 0a obal, mem5)../*.
8de40 2a 2a 20 41 73 73 75 6d 69 6e 67 20 6d 65 6d 35 ** Assuming mem5
8de50 2e 7a 50 6f 6f 6c 20 69 73 20 64 69 76 69 64 65 .zPool is divide
8de60 64 20 75 70 20 69 6e 74 6f 20 61 6e 20 61 72 72 d up into an arr
8de70 61 79 20 6f 66 20 4d 65 6d 35 4c 69 6e 6b 0a 2a ay of Mem5Link.*
8de80 2a 20 73 74 72 75 63 74 75 72 65 73 2c 20 72 65 * structures, re
8de90 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 turn a pointer t
8dea0 6f 20 74 68 65 20 69 64 78 2d 74 68 20 73 75 63 o the idx-th suc
8deb0 68 20 6c 69 6b 2e 0a 2a 2f 0a 23 64 65 66 69 6e h lik..*/.#defin
8dec0 65 20 4d 45 4d 35 4c 49 4e 4b 28 69 64 78 29 20 e MEM5LINK(idx)
8ded0 28 28 4d 65 6d 35 4c 69 6e 6b 20 2a 29 28 26 6d ((Mem5Link *)(&m
8dee0 65 6d 35 2e 7a 50 6f 6f 6c 5b 28 69 64 78 29 2a em5.zPool[(idx)*
8def0 6d 65 6d 35 2e 73 7a 41 74 6f 6d 5d 29 29 0a 0a mem5.szAtom]))..
8df00 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 /*.** Unlink the
8df10 20 63 68 75 6e 6b 20 61 74 20 6d 65 6d 35 2e 61 chunk at mem5.a
8df20 50 6f 6f 6c 5b 69 5d 20 66 72 6f 6d 20 6c 69 73 Pool[i] from lis
8df30 74 20 69 74 20 69 73 20 63 75 72 72 65 6e 74 6c t it is currentl
8df40 79 0a 2a 2a 20 6f 6e 2e 20 20 49 74 20 73 68 6f y.** on. It sho
8df50 75 6c 64 20 62 65 20 66 6f 75 6e 64 20 6f 6e 20 uld be found on
8df60 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b mem5.aiFreelist[
8df70 69 4c 6f 67 73 69 7a 65 5d 2e 0a 2a 2f 0a 73 74 iLogsize]..*/.st
8df80 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 atic void memsys
8df90 35 55 6e 6c 69 6e 6b 28 69 6e 74 20 69 2c 20 69 5Unlink(int i, i
8dfa0 6e 74 20 69 4c 6f 67 73 69 7a 65 29 7b 0a 20 20 nt iLogsize){.
8dfb0 69 6e 74 20 6e 65 78 74 2c 20 70 72 65 76 3b 0a int next, prev;.
8dfc0 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 assert( i>=0 &
8dfd0 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 & i<mem5.nBlock
8dfe0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 4c 6f );. assert( iLo
8dff0 67 73 69 7a 65 3e 3d 30 20 26 26 20 69 4c 6f 67 gsize>=0 && iLog
8e000 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 20 29 3b 0a size<=LOGMAX );.
8e010 20 20 61 73 73 65 72 74 28 20 28 6d 65 6d 35 2e assert( (mem5.
8e020 61 43 74 72 6c 5b 69 5d 20 26 20 43 54 52 4c 5f aCtrl[i] & CTRL_
8e030 4c 4f 47 53 49 5a 45 29 3d 3d 69 4c 6f 67 73 69 LOGSIZE)==iLogsi
8e040 7a 65 20 29 3b 0a 0a 20 20 6e 65 78 74 20 3d 20 ze );.. next =
8e050 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 6e 65 78 MEM5LINK(i)->nex
8e060 74 3b 0a 20 20 70 72 65 76 20 3d 20 4d 45 4d 35 t;. prev = MEM5
8e070 4c 49 4e 4b 28 69 29 2d 3e 70 72 65 76 3b 0a 20 LINK(i)->prev;.
8e080 20 69 66 28 20 70 72 65 76 3c 30 20 29 7b 0a 20 if( prev<0 ){.
8e090 20 20 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 mem5.aiFreeli
8e0a0 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 20 3d 20 6e st[iLogsize] = n
8e0b0 65 78 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ext;. }else{.
8e0c0 20 20 4d 45 4d 35 4c 49 4e 4b 28 70 72 65 76 29 MEM5LINK(prev)
8e0d0 2d 3e 6e 65 78 74 20 3d 20 6e 65 78 74 3b 0a 20 ->next = next;.
8e0e0 20 7d 0a 20 20 69 66 28 20 6e 65 78 74 3e 3d 30 }. if( next>=0
8e0f0 20 29 7b 0a 20 20 20 20 4d 45 4d 35 4c 49 4e 4b ){. MEM5LINK
8e100 28 6e 65 78 74 29 2d 3e 70 72 65 76 20 3d 20 70 (next)->prev = p
8e110 72 65 76 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a rev;. }.}../*.*
8e120 2a 20 4c 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b * Link the chunk
8e130 20 61 74 20 6d 65 6d 35 2e 61 50 6f 6f 6c 5b 69 at mem5.aPool[i
8e140 5d 20 73 6f 20 74 68 61 74 20 69 73 20 6f 6e 20 ] so that is on
8e150 74 68 65 20 69 4c 6f 67 73 69 7a 65 0a 2a 2a 20 the iLogsize.**
8e160 66 72 65 65 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 free list..*/.st
8e170 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 atic void memsys
8e180 35 4c 69 6e 6b 28 69 6e 74 20 69 2c 20 69 6e 74 5Link(int i, int
8e190 20 69 4c 6f 67 73 69 7a 65 29 7b 0a 20 20 69 6e iLogsize){. in
8e1a0 74 20 78 3b 0a 20 20 61 73 73 65 72 74 28 20 73 t x;. assert( s
8e1b0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
8e1c0 64 28 6d 65 6d 35 2e 6d 75 74 65 78 29 20 29 3b d(mem5.mutex) );
8e1d0 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 . assert( i>=0
8e1e0 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b && i<mem5.nBlock
8e1f0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 4c );. assert( iL
8e200 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20 69 4c 6f ogsize>=0 && iLo
8e210 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 20 29 3b gsize<=LOGMAX );
8e220 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65 6d 35 . assert( (mem5
8e230 2e 61 43 74 72 6c 5b 69 5d 20 26 20 43 54 52 4c .aCtrl[i] & CTRL
8e240 5f 4c 4f 47 53 49 5a 45 29 3d 3d 69 4c 6f 67 73 _LOGSIZE)==iLogs
8e250 69 7a 65 20 29 3b 0a 0a 20 20 78 20 3d 20 4d 45 ize );.. x = ME
8e260 4d 35 4c 49 4e 4b 28 69 29 2d 3e 6e 65 78 74 20 M5LINK(i)->next
8e270 3d 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 = mem5.aiFreelis
8e280 74 5b 69 4c 6f 67 73 69 7a 65 5d 3b 0a 20 20 4d t[iLogsize];. M
8e290 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 70 72 65 76 EM5LINK(i)->prev
8e2a0 20 3d 20 2d 31 3b 0a 20 20 69 66 28 20 78 3e 3d = -1;. if( x>=
8e2b0 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 0 ){. assert(
8e2c0 20 78 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 x<mem5.nBlock )
8e2d0 3b 0a 20 20 20 20 4d 45 4d 35 4c 49 4e 4b 28 78 ;. MEM5LINK(x
8e2e0 29 2d 3e 70 72 65 76 20 3d 20 69 3b 0a 20 20 7d )->prev = i;. }
8e2f0 0a 20 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 . mem5.aiFreeli
8e300 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 20 3d 20 69 st[iLogsize] = i
8e310 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 ;.}../*.** If th
8e320 65 20 53 54 41 54 49 43 5f 4d 45 4d 20 6d 75 74 e STATIC_MEM mut
8e330 65 78 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 ex is not alread
8e340 79 20 68 65 6c 64 2c 20 6f 62 74 61 69 6e 20 69 y held, obtain i
8e350 74 20 6e 6f 77 2e 20 54 68 65 20 6d 75 74 65 78 t now. The mutex
8e360 0a 2a 2a 20 77 69 6c 6c 20 61 6c 72 65 61 64 79 .** will already
8e370 20 62 65 20 68 65 6c 64 20 28 6f 62 74 61 69 6e be held (obtain
8e380 65 64 20 62 79 20 63 6f 64 65 20 69 6e 20 6d 61 ed by code in ma
8e390 6c 6c 6f 63 2e 63 29 20 69 66 0a 2a 2a 20 73 71 lloc.c) if.** sq
8e3a0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
8e3b0 67 2e 62 4d 65 6d 53 74 61 74 20 69 73 20 74 72 g.bMemStat is tr
8e3c0 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ue..*/.static vo
8e3d0 69 64 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 id memsys5Enter(
8e3e0 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 void){. sqlite3
8e3f0 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d _mutex_enter(mem
8e400 35 2e 6d 75 74 65 78 29 3b 0a 7d 0a 73 74 61 74 5.mutex);.}.stat
8e410 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 4c ic void memsys5L
8e420 65 61 76 65 28 76 6f 69 64 29 7b 0a 20 20 73 71 eave(void){. sq
8e430 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
8e440 65 28 6d 65 6d 35 2e 6d 75 74 65 78 29 3b 0a 7d e(mem5.mutex);.}
8e450 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
8e460 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 6f 75 he size of an ou
8e470 74 73 74 61 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 tstanding alloca
8e480 74 69 6f 6e 2c 20 69 6e 20 62 79 74 65 73 2e 20 tion, in bytes.
8e490 20 54 68 65 0a 2a 2a 20 73 69 7a 65 20 72 65 74 The.** size ret
8e4a0 75 72 6e 65 64 20 6f 6d 69 74 73 20 74 68 65 20 urned omits the
8e4b0 38 2d 62 79 74 65 20 68 65 61 64 65 72 20 6f 76 8-byte header ov
8e4c0 65 72 68 65 61 64 2e 20 20 54 68 69 73 20 6f 6e erhead. This on
8e4d0 6c 79 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 ly.** works for
8e4e0 63 68 75 6e 6b 73 20 74 68 61 74 20 61 72 65 20 chunks that are
8e4f0 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 65 currently checke
8e500 64 20 6f 75 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 d out..*/.static
8e510 20 69 6e 74 20 6d 65 6d 73 79 73 35 53 69 7a 65 int memsys5Size
8e520 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 69 6e 74 (void *p){. int
8e530 20 69 53 69 7a 65 20 3d 20 30 3b 0a 20 20 69 66 iSize = 0;. if
8e540 28 20 70 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 ( p ){. int i
8e550 20 3d 20 28 28 75 38 20 2a 29 70 2d 6d 65 6d 35 = ((u8 *)p-mem5
8e560 2e 7a 50 6f 6f 6c 29 2f 6d 65 6d 35 2e 73 7a 41 .zPool)/mem5.szA
8e570 74 6f 6d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 tom;. assert(
8e580 20 69 3e 3d 30 20 26 26 20 69 3c 6d 65 6d 35 2e i>=0 && i<mem5.
8e590 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 20 20 69 53 nBlock );. iS
8e5a0 69 7a 65 20 3d 20 6d 65 6d 35 2e 73 7a 41 74 6f ize = mem5.szAto
8e5b0 6d 20 2a 20 28 31 20 3c 3c 20 28 6d 65 6d 35 2e m * (1 << (mem5.
8e5c0 61 43 74 72 6c 5b 69 5d 26 43 54 52 4c 5f 4c 4f aCtrl[i]&CTRL_LO
8e5d0 47 53 49 5a 45 29 29 3b 0a 20 20 7d 0a 20 20 72 GSIZE));. }. r
8e5e0 65 74 75 72 6e 20 69 53 69 7a 65 3b 0a 7d 0a 0a eturn iSize;.}..
8e5f0 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 66 /*.** Find the f
8e600 69 72 73 74 20 65 6e 74 72 79 20 6f 6e 20 74 68 irst entry on th
8e610 65 20 66 72 65 65 6c 69 73 74 20 69 4c 6f 67 73 e freelist iLogs
8e620 69 7a 65 2e 20 20 55 6e 6c 69 6e 6b 20 74 68 61 ize. Unlink tha
8e630 74 0a 2a 2a 20 65 6e 74 72 79 20 61 6e 64 20 72 t.** entry and r
8e640 65 74 75 72 6e 20 69 74 73 20 69 6e 64 65 78 2e eturn its index.
8e650 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 .*/.static int
8e660 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 46 69 72 memsys5UnlinkFir
8e670 73 74 28 69 6e 74 20 69 4c 6f 67 73 69 7a 65 29 st(int iLogsize)
8e680 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 {. int i;. int
8e690 20 69 46 69 72 73 74 3b 0a 0a 20 20 61 73 73 65 iFirst;.. asse
8e6a0 72 74 28 20 69 4c 6f 67 73 69 7a 65 3e 3d 30 20 rt( iLogsize>=0
8e6b0 26 26 20 69 4c 6f 67 73 69 7a 65 3c 3d 4c 4f 47 && iLogsize<=LOG
8e6c0 4d 41 58 20 29 3b 0a 20 20 69 20 3d 20 69 46 69 MAX );. i = iFi
8e6d0 72 73 74 20 3d 20 6d 65 6d 35 2e 61 69 46 72 65 rst = mem5.aiFre
8e6e0 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 3b elist[iLogsize];
8e6f0 0a 20 20 61 73 73 65 72 74 28 20 69 46 69 72 73 . assert( iFirs
8e700 74 3e 3d 30 20 29 3b 0a 20 20 77 68 69 6c 65 28 t>=0 );. while(
8e710 20 69 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 i>0 ){. if(
8e720 69 3c 69 46 69 72 73 74 20 29 20 69 46 69 72 73 i<iFirst ) iFirs
8e730 74 20 3d 20 69 3b 0a 20 20 20 20 69 20 3d 20 4d t = i;. i = M
8e740 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 6e 65 78 74 EM5LINK(i)->next
8e750 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 35 55 ;. }. memsys5U
8e760 6e 6c 69 6e 6b 28 69 46 69 72 73 74 2c 20 69 4c nlink(iFirst, iL
8e770 6f 67 73 69 7a 65 29 3b 0a 20 20 72 65 74 75 72 ogsize);. retur
8e780 6e 20 69 46 69 72 73 74 3b 0a 7d 0a 0a 2f 2a 0a n iFirst;.}../*.
8e790 2a 2a 20 52 65 74 75 72 6e 20 61 20 62 6c 6f 63 ** Return a bloc
8e7a0 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 6f 66 20 61 k of memory of a
8e7b0 74 20 6c 65 61 73 74 20 6e 42 79 74 65 73 20 69 t least nBytes i
8e7c0 6e 20 73 69 7a 65 2e 0a 2a 2a 20 52 65 74 75 72 n size..** Retur
8e7d0 6e 20 4e 55 4c 4c 20 69 66 20 75 6e 61 62 6c 65 n NULL if unable
8e7e0 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 . Return NULL i
8e7f0 66 20 6e 42 79 74 65 73 3d 3d 30 2e 0a 2a 2a 0a f nBytes==0..**.
8e800 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 67 75 ** The caller gu
8e810 61 72 61 6e 74 65 65 73 20 74 68 61 74 20 6e 42 arantees that nB
8e820 79 74 65 20 70 6f 73 69 74 69 76 65 2e 0a 2a 2a yte positive..**
8e830 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 68 .** The caller h
8e840 61 73 20 6f 62 74 61 69 6e 65 64 20 61 20 6d 75 as obtained a mu
8e850 74 65 78 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 tex prior to inv
8e860 6f 6b 69 6e 67 20 74 68 69 73 0a 2a 2a 20 72 6f oking this.** ro
8e870 75 74 69 6e 65 20 73 6f 20 74 68 65 72 65 20 69 utine so there i
8e880 73 20 6e 65 76 65 72 20 61 6e 79 20 63 68 61 6e s never any chan
8e890 63 65 20 74 68 61 74 20 74 77 6f 20 6f 72 20 6d ce that two or m
8e8a0 6f 72 65 0a 2a 2a 20 74 68 72 65 61 64 73 20 63 ore.** threads c
8e8b0 61 6e 20 62 65 20 69 6e 20 74 68 69 73 20 72 6f an be in this ro
8e8c0 75 74 69 6e 65 20 61 74 20 74 68 65 20 73 61 6d utine at the sam
8e8d0 65 20 74 69 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 e time..*/.stati
8e8e0 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 35 4d c void *memsys5M
8e8f0 61 6c 6c 6f 63 55 6e 73 61 66 65 28 69 6e 74 20 allocUnsafe(int
8e900 6e 42 79 74 65 29 7b 0a 20 20 69 6e 74 20 69 3b nByte){. int i;
8e910 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
8e920 64 65 78 20 6f 66 20 61 20 6d 65 6d 35 2e 61 50 dex of a mem5.aP
8e930 6f 6f 6c 5b 5d 20 73 6c 6f 74 20 2a 2f 0a 20 20 ool[] slot */.
8e940 69 6e 74 20 69 42 69 6e 3b 20 20 20 20 20 20 20 int iBin;
8e950 20 2f 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20 6d /* Index into m
8e960 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 5d em5.aiFreelist[]
8e970 20 2a 2f 0a 20 20 69 6e 74 20 69 46 75 6c 6c 53 */. int iFullS
8e980 7a 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f z; /* Size o
8e990 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 f allocation rou
8e9a0 6e 64 65 64 20 75 70 20 74 6f 20 70 6f 77 65 72 nded up to power
8e9b0 20 6f 66 20 32 20 2a 2f 0a 20 20 69 6e 74 20 69 of 2 */. int i
8e9c0 4c 6f 67 73 69 7a 65 3b 20 20 20 20 2f 2a 20 4c Logsize; /* L
8e9d0 6f 67 32 20 6f 66 20 69 46 75 6c 6c 53 7a 2f 50 og2 of iFullSz/P
8e9e0 4f 57 32 5f 4d 49 4e 20 2a 2f 0a 0a 20 20 2f 2a OW2_MIN */.. /*
8e9f0 20 6e 42 79 74 65 20 6d 75 73 74 20 62 65 20 61 nByte must be a
8ea00 20 70 6f 73 69 74 69 76 65 20 2a 2f 0a 20 20 61 positive */. a
8ea10 73 73 65 72 74 28 20 6e 42 79 74 65 3e 30 20 29 ssert( nByte>0 )
8ea20 3b 0a 0a 20 20 2f 2a 20 4b 65 65 70 20 74 72 61 ;.. /* Keep tra
8ea30 63 6b 20 6f 66 20 74 68 65 20 6d 61 78 69 6d 75 ck of the maximu
8ea40 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 m allocation req
8ea50 75 65 73 74 2e 20 20 45 76 65 6e 20 75 6e 66 75 uest. Even unfu
8ea60 6c 66 69 6c 6c 65 64 0a 20 20 2a 2a 20 72 65 71 lfilled. ** req
8ea70 75 65 73 74 73 20 61 72 65 20 63 6f 75 6e 74 65 uests are counte
8ea80 64 20 2a 2f 0a 20 20 69 66 28 20 28 75 33 32 29 d */. if( (u32)
8ea90 6e 42 79 74 65 3e 6d 65 6d 35 2e 6d 61 78 52 65 nByte>mem5.maxRe
8eaa0 71 75 65 73 74 20 29 7b 0a 20 20 20 20 6d 65 6d quest ){. mem
8eab0 35 2e 6d 61 78 52 65 71 75 65 73 74 20 3d 20 6e 5.maxRequest = n
8eac0 42 79 74 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 Byte;. }.. /*
8ead0 41 62 6f 72 74 20 69 66 20 74 68 65 20 72 65 71 Abort if the req
8eae0 75 65 73 74 65 64 20 61 6c 6c 6f 63 61 74 69 6f uested allocatio
8eaf0 6e 20 73 69 7a 65 20 69 73 20 6c 61 72 67 65 72 n size is larger
8eb00 20 74 68 61 6e 20 74 68 65 20 6c 61 72 67 65 73 than the larges
8eb10 74 0a 20 20 2a 2a 20 70 6f 77 65 72 20 6f 66 20 t. ** power of
8eb20 74 77 6f 20 74 68 61 74 20 77 65 20 63 61 6e 20 two that we can
8eb30 72 65 70 72 65 73 65 6e 74 20 75 73 69 6e 67 20 represent using
8eb40 33 32 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 32-bit signed in
8eb50 74 65 67 65 72 73 2e 0a 20 20 2a 2f 0a 20 20 69 tegers.. */. i
8eb60 66 28 20 6e 42 79 74 65 20 3e 20 30 78 34 30 30 f( nByte > 0x400
8eb70 30 30 30 30 30 20 29 7b 0a 20 20 20 20 72 65 74 00000 ){. ret
8eb80 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a urn 0;. }.. /*
8eb90 20 52 6f 75 6e 64 20 6e 42 79 74 65 20 75 70 20 Round nByte up
8eba0 74 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69 to the next vali
8ebb0 64 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 20 2a d power of two *
8ebc0 2f 0a 20 20 66 6f 72 28 69 46 75 6c 6c 53 7a 3d /. for(iFullSz=
8ebd0 6d 65 6d 35 2e 73 7a 41 74 6f 6d 2c 20 69 4c 6f mem5.szAtom, iLo
8ebe0 67 73 69 7a 65 3d 30 3b 20 69 46 75 6c 6c 53 7a gsize=0; iFullSz
8ebf0 3c 6e 42 79 74 65 3b 20 69 46 75 6c 6c 53 7a 20 <nByte; iFullSz
8ec00 2a 3d 20 32 2c 20 69 4c 6f 67 73 69 7a 65 2b 2b *= 2, iLogsize++
8ec10 29 7b 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 ){}.. /* Make s
8ec20 75 72 65 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c ure mem5.aiFreel
8ec30 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 20 63 6f ist[iLogsize] co
8ec40 6e 74 61 69 6e 73 20 61 74 20 6c 65 61 73 74 20 ntains at least
8ec50 6f 6e 65 20 66 72 65 65 0a 20 20 2a 2a 20 62 6c one free. ** bl
8ec60 6f 63 6b 2e 20 20 49 66 20 6e 6f 74 2c 20 74 68 ock. If not, th
8ec70 65 6e 20 73 70 6c 69 74 20 61 20 62 6c 6f 63 6b en split a block
8ec80 20 6f 66 20 74 68 65 20 6e 65 78 74 20 6c 61 72 of the next lar
8ec90 67 65 72 20 70 6f 77 65 72 20 6f 66 0a 20 20 2a ger power of. *
8eca0 2a 20 74 77 6f 20 69 6e 20 6f 72 64 65 72 20 74 * two in order t
8ecb0 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 66 o create a new f
8ecc0 72 65 65 20 62 6c 6f 63 6b 20 6f 66 20 73 69 7a ree block of siz
8ecd0 65 20 69 4c 6f 67 73 69 7a 65 2e 0a 20 20 2a 2f e iLogsize.. */
8ece0 0a 20 20 66 6f 72 28 69 42 69 6e 3d 69 4c 6f 67 . for(iBin=iLog
8ecf0 73 69 7a 65 3b 20 6d 65 6d 35 2e 61 69 46 72 65 size; mem5.aiFre
8ed00 65 6c 69 73 74 5b 69 42 69 6e 5d 3c 30 20 26 26 elist[iBin]<0 &&
8ed10 20 69 42 69 6e 3c 3d 4c 4f 47 4d 41 58 3b 20 69 iBin<=LOGMAX; i
8ed20 42 69 6e 2b 2b 29 7b 7d 0a 20 20 69 66 28 20 69 Bin++){}. if( i
8ed30 42 69 6e 3e 4c 4f 47 4d 41 58 20 29 20 72 65 74 Bin>LOGMAX ) ret
8ed40 75 72 6e 20 30 3b 0a 20 20 69 20 3d 20 6d 65 6d urn 0;. i = mem
8ed50 73 79 73 35 55 6e 6c 69 6e 6b 46 69 72 73 74 28 sys5UnlinkFirst(
8ed60 69 42 69 6e 29 3b 0a 20 20 77 68 69 6c 65 28 20 iBin);. while(
8ed70 69 42 69 6e 3e 69 4c 6f 67 73 69 7a 65 20 29 7b iBin>iLogsize ){
8ed80 0a 20 20 20 20 69 6e 74 20 6e 65 77 53 69 7a 65 . int newSize
8ed90 3b 0a 0a 20 20 20 20 69 42 69 6e 2d 2d 3b 0a 20 ;.. iBin--;.
8eda0 20 20 20 6e 65 77 53 69 7a 65 20 3d 20 31 20 3c newSize = 1 <
8edb0 3c 20 69 42 69 6e 3b 0a 20 20 20 20 6d 65 6d 35 < iBin;. mem5
8edc0 2e 61 43 74 72 6c 5b 69 2b 6e 65 77 53 69 7a 65 .aCtrl[i+newSize
8edd0 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c 20 ] = CTRL_FREE |
8ede0 69 42 69 6e 3b 0a 20 20 20 20 6d 65 6d 73 79 73 iBin;. memsys
8edf0 35 4c 69 6e 6b 28 69 2b 6e 65 77 53 69 7a 65 2c 5Link(i+newSize,
8ee00 20 69 42 69 6e 29 3b 0a 20 20 7d 0a 20 20 6d 65 iBin);. }. me
8ee10 6d 35 2e 61 43 74 72 6c 5b 69 5d 20 3d 20 69 4c m5.aCtrl[i] = iL
8ee20 6f 67 73 69 7a 65 3b 0a 0a 20 20 2f 2a 20 55 70 ogsize;.. /* Up
8ee30 64 61 74 65 20 61 6c 6c 6f 63 61 74 6f 72 20 70 date allocator p
8ee40 65 72 66 6f 72 6d 61 6e 63 65 20 73 74 61 74 69 erformance stati
8ee50 73 74 69 63 73 2e 20 2a 2f 0a 20 20 6d 65 6d 35 stics. */. mem5
8ee60 2e 6e 41 6c 6c 6f 63 2b 2b 3b 0a 20 20 6d 65 6d .nAlloc++;. mem
8ee70 35 2e 74 6f 74 61 6c 41 6c 6c 6f 63 20 2b 3d 20 5.totalAlloc +=
8ee80 69 46 75 6c 6c 53 7a 3b 0a 20 20 6d 65 6d 35 2e iFullSz;. mem5.
8ee90 74 6f 74 61 6c 45 78 63 65 73 73 20 2b 3d 20 69 totalExcess += i
8eea0 46 75 6c 6c 53 7a 20 2d 20 6e 42 79 74 65 3b 0a FullSz - nByte;.
8eeb0 20 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f mem5.currentCo
8eec0 75 6e 74 2b 2b 3b 0a 20 20 6d 65 6d 35 2e 63 75 unt++;. mem5.cu
8eed0 72 72 65 6e 74 4f 75 74 20 2b 3d 20 69 46 75 6c rrentOut += iFul
8eee0 6c 53 7a 3b 0a 20 20 69 66 28 20 6d 65 6d 35 2e lSz;. if( mem5.
8eef0 6d 61 78 43 6f 75 6e 74 3c 6d 65 6d 35 2e 63 75 maxCount<mem5.cu
8ef00 72 72 65 6e 74 43 6f 75 6e 74 20 29 20 6d 65 6d rrentCount ) mem
8ef10 35 2e 6d 61 78 43 6f 75 6e 74 20 3d 20 6d 65 6d 5.maxCount = mem
8ef20 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 3b 0a 5.currentCount;.
8ef30 20 20 69 66 28 20 6d 65 6d 35 2e 6d 61 78 4f 75 if( mem5.maxOu
8ef40 74 3c 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 t<mem5.currentOu
8ef50 74 20 29 20 6d 65 6d 35 2e 6d 61 78 4f 75 74 20 t ) mem5.maxOut
8ef60 3d 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 = mem5.currentOu
8ef70 74 3b 0a 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20 t;.. /* Return
8ef80 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
8ef90 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 allocated memor
8efa0 79 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 y. */. return (
8efb0 76 6f 69 64 2a 29 26 6d 65 6d 35 2e 7a 50 6f 6f void*)&mem5.zPoo
8efc0 6c 5b 69 2a 6d 65 6d 35 2e 73 7a 41 74 6f 6d 5d l[i*mem5.szAtom]
8efd0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 ;.}../*.** Free
8efe0 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6d an outstanding m
8eff0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
8f000 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
8f010 20 6d 65 6d 73 79 73 35 46 72 65 65 55 6e 73 61 memsys5FreeUnsa
8f020 66 65 28 76 6f 69 64 20 2a 70 4f 6c 64 29 7b 0a fe(void *pOld){.
8f030 20 20 75 33 32 20 73 69 7a 65 2c 20 69 4c 6f 67 u32 size, iLog
8f040 73 69 7a 65 3b 0a 20 20 69 6e 74 20 69 42 6c 6f size;. int iBlo
8f050 63 6b 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 69 42 ck;.. /* Set iB
8f060 6c 6f 63 6b 20 74 6f 20 74 68 65 20 69 6e 64 65 lock to the inde
8f070 78 20 6f 66 20 74 68 65 20 62 6c 6f 63 6b 20 70 x of the block p
8f080 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 4f 6c ointed to by pOl
8f090 64 20 69 6e 20 0a 20 20 2a 2a 20 74 68 65 20 61 d in . ** the a
8f0a0 72 72 61 79 20 6f 66 20 6d 65 6d 35 2e 73 7a 41 rray of mem5.szA
8f0b0 74 6f 6d 20 62 79 74 65 20 62 6c 6f 63 6b 73 20 tom byte blocks
8f0c0 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 6d 65 pointed to by me
8f0d0 6d 35 2e 7a 50 6f 6f 6c 2e 0a 20 20 2a 2f 0a 20 m5.zPool.. */.
8f0e0 20 69 42 6c 6f 63 6b 20 3d 20 28 28 75 38 20 2a iBlock = ((u8 *
8f0f0 29 70 4f 6c 64 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c )pOld-mem5.zPool
8f100 29 2f 6d 65 6d 35 2e 73 7a 41 74 6f 6d 3b 0a 0a )/mem5.szAtom;..
8f110 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 /* Check that
8f120 74 68 65 20 70 6f 69 6e 74 65 72 20 70 4f 6c 64 the pointer pOld
8f130 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 76 61 6c points to a val
8f140 69 64 2c 20 6e 6f 6e 2d 66 72 65 65 20 62 6c 6f id, non-free blo
8f150 63 6b 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 ck. */. assert(
8f160 20 69 42 6c 6f 63 6b 3e 3d 30 20 26 26 20 69 42 iBlock>=0 && iB
8f170 6c 6f 63 6b 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b lock<mem5.nBlock
8f180 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 28 );. assert( ((
8f190 75 38 20 2a 29 70 4f 6c 64 2d 6d 65 6d 35 2e 7a u8 *)pOld-mem5.z
8f1a0 50 6f 6f 6c 29 25 6d 65 6d 35 2e 73 7a 41 74 6f Pool)%mem5.szAto
8f1b0 6d 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 m==0 );. assert
8f1c0 28 20 28 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 ( (mem5.aCtrl[iB
8f1d0 6c 6f 63 6b 5d 20 26 20 43 54 52 4c 5f 46 52 45 lock] & CTRL_FRE
8f1e0 45 29 3d 3d 30 20 29 3b 0a 0a 20 20 69 4c 6f 67 E)==0 );.. iLog
8f1f0 73 69 7a 65 20 3d 20 6d 65 6d 35 2e 61 43 74 72 size = mem5.aCtr
8f200 6c 5b 69 42 6c 6f 63 6b 5d 20 26 20 43 54 52 4c l[iBlock] & CTRL
8f210 5f 4c 4f 47 53 49 5a 45 3b 0a 20 20 73 69 7a 65 _LOGSIZE;. size
8f220 20 3d 20 31 3c 3c 69 4c 6f 67 73 69 7a 65 3b 0a = 1<<iLogsize;.
8f230 20 20 61 73 73 65 72 74 28 20 69 42 6c 6f 63 6b assert( iBlock
8f240 2b 73 69 7a 65 2d 31 3c 28 75 33 32 29 6d 65 6d +size-1<(u32)mem
8f250 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 0a 20 20 6d 5.nBlock );.. m
8f260 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b em5.aCtrl[iBlock
8f270 5d 20 7c 3d 20 43 54 52 4c 5f 46 52 45 45 3b 0a ] |= CTRL_FREE;.
8f280 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c mem5.aCtrl[iBl
8f290 6f 63 6b 2b 73 69 7a 65 2d 31 5d 20 7c 3d 20 43 ock+size-1] |= C
8f2a0 54 52 4c 5f 46 52 45 45 3b 0a 20 20 61 73 73 65 TRL_FREE;. asse
8f2b0 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 rt( mem5.current
8f2c0 43 6f 75 6e 74 3e 30 20 29 3b 0a 20 20 61 73 73 Count>0 );. ass
8f2d0 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 65 6e ert( mem5.curren
8f2e0 74 4f 75 74 3e 3d 28 73 69 7a 65 2a 6d 65 6d 35 tOut>=(size*mem5
8f2f0 2e 73 7a 41 74 6f 6d 29 20 29 3b 0a 20 20 6d 65 .szAtom) );. me
8f300 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 2d m5.currentCount-
8f310 2d 3b 0a 20 20 6d 65 6d 35 2e 63 75 72 72 65 6e -;. mem5.curren
8f320 74 4f 75 74 20 2d 3d 20 73 69 7a 65 2a 6d 65 6d tOut -= size*mem
8f330 35 2e 73 7a 41 74 6f 6d 3b 0a 20 20 61 73 73 65 5.szAtom;. asse
8f340 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 rt( mem5.current
8f350 4f 75 74 3e 30 20 7c 7c 20 6d 65 6d 35 2e 63 75 Out>0 || mem5.cu
8f360 72 72 65 6e 74 43 6f 75 6e 74 3d 3d 30 20 29 3b rrentCount==0 );
8f370 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d 35 2e . assert( mem5.
8f380 63 75 72 72 65 6e 74 43 6f 75 6e 74 3e 30 20 7c currentCount>0 |
8f390 7c 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 | mem5.currentOu
8f3a0 74 3d 3d 30 20 29 3b 0a 0a 20 20 6d 65 6d 35 2e t==0 );.. mem5.
8f3b0 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 3d 20 aCtrl[iBlock] =
8f3c0 43 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 CTRL_FREE | iLog
8f3d0 73 69 7a 65 3b 0a 20 20 77 68 69 6c 65 28 20 41 size;. while( A
8f3e0 4c 57 41 59 53 28 69 4c 6f 67 73 69 7a 65 3c 4c LWAYS(iLogsize<L
8f3f0 4f 47 4d 41 58 29 20 29 7b 0a 20 20 20 20 69 6e OGMAX) ){. in
8f400 74 20 69 42 75 64 64 79 3b 0a 20 20 20 20 69 66 t iBuddy;. if
8f410 28 20 28 69 42 6c 6f 63 6b 3e 3e 69 4c 6f 67 73 ( (iBlock>>iLogs
8f420 69 7a 65 29 20 26 20 31 20 29 7b 0a 20 20 20 20 ize) & 1 ){.
8f430 20 20 69 42 75 64 64 79 20 3d 20 69 42 6c 6f 63 iBuddy = iBloc
8f440 6b 20 2d 20 73 69 7a 65 3b 0a 20 20 20 20 7d 65 k - size;. }e
8f450 6c 73 65 7b 0a 20 20 20 20 20 20 69 42 75 64 64 lse{. iBudd
8f460 79 20 3d 20 69 42 6c 6f 63 6b 20 2b 20 73 69 7a y = iBlock + siz
8f470 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 e;. }. ass
8f480 65 72 74 28 20 69 42 75 64 64 79 3e 3d 30 20 29 ert( iBuddy>=0 )
8f490 3b 0a 20 20 20 20 69 66 28 20 28 69 42 75 64 64 ;. if( (iBudd
8f4a0 79 2b 28 31 3c 3c 69 4c 6f 67 73 69 7a 65 29 29 y+(1<<iLogsize))
8f4b0 3e 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 20 62 >mem5.nBlock ) b
8f4c0 72 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 6d 65 reak;. if( me
8f4d0 6d 35 2e 61 43 74 72 6c 5b 69 42 75 64 64 79 5d m5.aCtrl[iBuddy]
8f4e0 21 3d 28 43 54 52 4c 5f 46 52 45 45 20 7c 20 69 !=(CTRL_FREE | i
8f4f0 4c 6f 67 73 69 7a 65 29 20 29 20 62 72 65 61 6b Logsize) ) break
8f500 3b 0a 20 20 20 20 6d 65 6d 73 79 73 35 55 6e 6c ;. memsys5Unl
8f510 69 6e 6b 28 69 42 75 64 64 79 2c 20 69 4c 6f 67 ink(iBuddy, iLog
8f520 73 69 7a 65 29 3b 0a 20 20 20 20 69 4c 6f 67 73 size);. iLogs
8f530 69 7a 65 2b 2b 3b 0a 20 20 20 20 69 66 28 20 69 ize++;. if( i
8f540 42 75 64 64 79 3c 69 42 6c 6f 63 6b 20 29 7b 0a Buddy<iBlock ){.
8f550 20 20 20 20 20 20 6d 65 6d 35 2e 61 43 74 72 6c mem5.aCtrl
8f560 5b 69 42 75 64 64 79 5d 20 3d 20 43 54 52 4c 5f [iBuddy] = CTRL_
8f570 46 52 45 45 20 7c 20 69 4c 6f 67 73 69 7a 65 3b FREE | iLogsize;
8f580 0a 20 20 20 20 20 20 6d 65 6d 35 2e 61 43 74 72 . mem5.aCtr
8f590 6c 5b 69 42 6c 6f 63 6b 5d 20 3d 20 30 3b 0a 20 l[iBlock] = 0;.
8f5a0 20 20 20 20 20 69 42 6c 6f 63 6b 20 3d 20 69 42 iBlock = iB
8f5b0 75 64 64 79 3b 0a 20 20 20 20 7d 65 6c 73 65 7b uddy;. }else{
8f5c0 0a 20 20 20 20 20 20 6d 65 6d 35 2e 61 43 74 72 . mem5.aCtr
8f5d0 6c 5b 69 42 6c 6f 63 6b 5d 20 3d 20 43 54 52 4c l[iBlock] = CTRL
8f5e0 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73 69 7a 65 _FREE | iLogsize
8f5f0 3b 0a 20 20 20 20 20 20 6d 65 6d 35 2e 61 43 74 ;. mem5.aCt
8f600 72 6c 5b 69 42 75 64 64 79 5d 20 3d 20 30 3b 0a rl[iBuddy] = 0;.
8f610 20 20 20 20 7d 0a 20 20 20 20 73 69 7a 65 20 2a }. size *
8f620 3d 20 32 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 = 2;. }. memsy
8f630 73 35 4c 69 6e 6b 28 69 42 6c 6f 63 6b 2c 20 69 s5Link(iBlock, i
8f640 4c 6f 67 73 69 7a 65 29 3b 0a 7d 0a 0a 2f 2a 0a Logsize);.}../*.
8f650 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 ** Allocate nByt
8f660 65 73 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2f 0a es of memory.*/.
8f670 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d static void *mem
8f680 73 79 73 35 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e sys5Malloc(int n
8f690 42 79 74 65 73 29 7b 0a 20 20 73 71 6c 69 74 65 Bytes){. sqlite
8f6a0 33 5f 69 6e 74 36 34 20 2a 70 20 3d 20 30 3b 0a 3_int64 *p = 0;.
8f6b0 20 20 69 66 28 20 6e 42 79 74 65 73 3e 30 20 29 if( nBytes>0 )
8f6c0 7b 0a 20 20 20 20 6d 65 6d 73 79 73 35 45 6e 74 {. memsys5Ent
8f6d0 65 72 28 29 3b 0a 20 20 20 20 70 20 3d 20 6d 65 er();. p = me
8f6e0 6d 73 79 73 35 4d 61 6c 6c 6f 63 55 6e 73 61 66 msys5MallocUnsaf
8f6f0 65 28 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 6d e(nBytes);. m
8f700 65 6d 73 79 73 35 4c 65 61 76 65 28 29 3b 0a 20 emsys5Leave();.
8f710 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 }. return (voi
8f720 64 2a 29 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 d*)p; .}../*.**
8f730 46 72 65 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a Free memory..**.
8f740 2a 2a 20 54 68 65 20 6f 75 74 65 72 20 6c 61 79 ** The outer lay
8f750 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 er memory alloca
8f760 74 6f 72 20 70 72 65 76 65 6e 74 73 20 74 68 69 tor prevents thi
8f770 73 20 72 6f 75 74 69 6e 65 20 66 72 6f 6d 0a 2a s routine from.*
8f780 2a 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 20 77 * being called w
8f790 69 74 68 20 70 50 72 69 6f 72 3d 3d 30 2e 0a 2a ith pPrior==0..*
8f7a0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 /.static void me
8f7b0 6d 73 79 73 35 46 72 65 65 28 76 6f 69 64 20 2a msys5Free(void *
8f7c0 70 50 72 69 6f 72 29 7b 0a 20 20 61 73 73 65 72 pPrior){. asser
8f7d0 74 28 20 70 50 72 69 6f 72 21 3d 30 20 29 3b 0a t( pPrior!=0 );.
8f7e0 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29 memsys5Enter()
8f7f0 3b 0a 20 20 6d 65 6d 73 79 73 35 46 72 65 65 55 ;. memsys5FreeU
8f800 6e 73 61 66 65 28 70 50 72 69 6f 72 29 3b 0a 20 nsafe(pPrior);.
8f810 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 29 3b memsys5Leave();
8f820 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e .}../*.** Chan
8f830 67 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 ge the size of a
8f840 6e 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72 n existing memor
8f850 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a y allocation..**
8f860 0a 2a 2a 20 54 68 65 20 6f 75 74 65 72 20 6c 61 .** The outer la
8f870 79 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 yer memory alloc
8f880 61 74 6f 72 20 70 72 65 76 65 6e 74 73 20 74 68 ator prevents th
8f890 69 73 20 72 6f 75 74 69 6e 65 20 66 72 6f 6d 0a is routine from.
8f8a0 2a 2a 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 20 ** being called
8f8b0 77 69 74 68 20 70 50 72 69 6f 72 3d 3d 30 2e 20 with pPrior==0.
8f8c0 20 0a 2a 2a 0a 2a 2a 20 6e 42 79 74 65 73 20 69 .**.** nBytes i
8f8d0 73 20 61 6c 77 61 79 73 20 61 20 76 61 6c 75 65 s always a value
8f8e0 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 61 obtained from a
8f8f0 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 0a 2a prior call to.*
8f900 2a 20 6d 65 6d 73 79 73 35 52 6f 75 6e 64 28 29 * memsys5Round()
8f910 2e 20 20 48 65 6e 63 65 20 6e 42 79 74 65 73 20 . Hence nBytes
8f920 69 73 20 61 6c 77 61 79 73 20 61 20 6e 6f 6e 2d is always a non-
8f930 6e 65 67 61 74 69 76 65 20 70 6f 77 65 72 0a 2a negative power.*
8f940 2a 20 6f 66 20 74 77 6f 2e 20 20 49 66 20 6e 42 * of two. If nB
8f950 79 74 65 73 3d 3d 30 20 74 68 61 74 20 6d 65 61 ytes==0 that mea
8f960 6e 73 20 74 68 61 74 20 61 6e 20 6f 76 65 72 73 ns that an overs
8f970 69 7a 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a ize allocation.*
8f980 2a 20 28 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e * (an allocation
8f990 20 6c 61 72 67 65 72 20 74 68 61 6e 20 30 78 34 larger than 0x4
8f9a0 30 30 30 30 30 30 30 29 20 77 61 73 20 72 65 71 0000000) was req
8f9b0 75 65 73 74 65 64 20 61 6e 64 20 74 68 69 73 0a uested and this.
8f9c0 2a 2a 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c ** routine shoul
8f9d0 64 20 72 65 74 75 72 6e 20 30 20 77 69 74 68 6f d return 0 witho
8f9e0 75 74 20 66 72 65 65 69 6e 67 20 70 50 72 69 6f ut freeing pPrio
8f9f0 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 r..*/.static voi
8fa00 64 20 2a 6d 65 6d 73 79 73 35 52 65 61 6c 6c 6f d *memsys5Reallo
8fa10 63 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20 c(void *pPrior,
8fa20 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 69 int nBytes){. i
8fa30 6e 74 20 6e 4f 6c 64 3b 0a 20 20 76 6f 69 64 20 nt nOld;. void
8fa40 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 *p;. assert( pP
8fa50 72 69 6f 72 21 3d 30 20 29 3b 0a 20 20 61 73 73 rior!=0 );. ass
8fa60 65 72 74 28 20 28 6e 42 79 74 65 73 26 28 6e 42 ert( (nBytes&(nB
8fa70 79 74 65 73 2d 31 29 29 3d 3d 30 20 29 3b 0a 20 ytes-1))==0 );.
8fa80 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 73 3e assert( nBytes>
8fa90 3d 30 20 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 =0 );. if( nByt
8faa0 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 es==0 ){. ret
8fab0 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 4f 6c urn 0;. }. nOl
8fac0 64 20 3d 20 6d 65 6d 73 79 73 35 53 69 7a 65 28 d = memsys5Size(
8fad0 70 50 72 69 6f 72 29 3b 0a 20 20 69 66 28 20 6e pPrior);. if( n
8fae0 42 79 74 65 73 3c 3d 6e 4f 6c 64 20 29 7b 0a 20 Bytes<=nOld ){.
8faf0 20 20 20 72 65 74 75 72 6e 20 70 50 72 69 6f 72 return pPrior
8fb00 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 35 45 ;. }. memsys5E
8fb10 6e 74 65 72 28 29 3b 0a 20 20 70 20 3d 20 6d 65 nter();. p = me
8fb20 6d 73 79 73 35 4d 61 6c 6c 6f 63 55 6e 73 61 66 msys5MallocUnsaf
8fb30 65 28 6e 42 79 74 65 73 29 3b 0a 20 20 69 66 28 e(nBytes);. if(
8fb40 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 p ){. memcpy
8fb50 28 70 2c 20 70 50 72 69 6f 72 2c 20 6e 4f 6c 64 (p, pPrior, nOld
8fb60 29 3b 0a 20 20 20 20 6d 65 6d 73 79 73 35 46 72 );. memsys5Fr
8fb70 65 65 55 6e 73 61 66 65 28 70 50 72 69 6f 72 29 eeUnsafe(pPrior)
8fb80 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 35 4c ;. }. memsys5L
8fb90 65 61 76 65 28 29 3b 0a 20 20 72 65 74 75 72 6e eave();. return
8fba0 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 p;.}../*.** Rou
8fbb0 6e 64 20 75 70 20 61 20 72 65 71 75 65 73 74 20 nd up a request
8fbc0 73 69 7a 65 20 74 6f 20 74 68 65 20 6e 65 78 74 size to the next
8fbd0 20 76 61 6c 69 64 20 61 6c 6c 6f 63 61 74 69 6f valid allocatio
8fbe0 6e 20 73 69 7a 65 2e 20 20 49 66 0a 2a 2a 20 74 n size. If.** t
8fbf0 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 he allocation is
8fc00 20 74 6f 6f 20 6c 61 72 67 65 20 74 6f 20 62 65 too large to be
8fc10 20 68 61 6e 64 6c 65 64 20 62 79 20 74 68 69 73 handled by this
8fc20 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 79 73 74 allocation syst
8fc30 65 6d 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e em,.** return 0.
8fc40 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 61 6c 6c 6f 63 .**.** All alloc
8fc50 61 74 69 6f 6e 73 20 6d 75 73 74 20 62 65 20 61 ations must be a
8fc60 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 20 61 6e power of two an
8fc70 64 20 6d 75 73 74 20 62 65 20 65 78 70 72 65 73 d must be expres
8fc80 73 65 64 20 62 79 20 61 0a 2a 2a 20 33 32 2d 62 sed by a.** 32-b
8fc90 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 it signed intege
8fca0 72 2e 20 20 48 65 6e 63 65 20 74 68 65 20 6c 61 r. Hence the la
8fcb0 72 67 65 73 74 20 61 6c 6c 6f 63 61 74 69 6f 6e rgest allocation
8fcc0 20 69 73 20 30 78 34 30 30 30 30 30 30 30 0a 2a is 0x40000000.*
8fcd0 2a 20 6f 72 20 31 30 37 33 37 34 31 38 32 34 20 * or 1073741824
8fce0 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 bytes..*/.static
8fcf0 20 69 6e 74 20 6d 65 6d 73 79 73 35 52 6f 75 6e int memsys5Roun
8fd00 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20 20 69 6e dup(int n){. in
8fd10 74 20 69 46 75 6c 6c 53 7a 3b 0a 20 20 69 66 28 t iFullSz;. if(
8fd20 20 6e 20 3e 20 30 78 34 30 30 30 30 30 30 30 20 n > 0x40000000
8fd30 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 66 6f ) return 0;. fo
8fd40 72 28 69 46 75 6c 6c 53 7a 3d 6d 65 6d 35 2e 73 r(iFullSz=mem5.s
8fd50 7a 41 74 6f 6d 3b 20 69 46 75 6c 6c 53 7a 3c 6e zAtom; iFullSz<n
8fd60 3b 20 69 46 75 6c 6c 53 7a 20 2a 3d 20 32 29 3b ; iFullSz *= 2);
8fd70 0a 20 20 72 65 74 75 72 6e 20 69 46 75 6c 6c 53 . return iFullS
8fd80 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 z;.}../*.** Retu
8fd90 72 6e 20 74 68 65 20 63 65 69 6c 69 6e 67 20 6f rn the ceiling o
8fda0 66 20 74 68 65 20 6c 6f 67 61 72 69 74 68 6d 20 f the logarithm
8fdb0 62 61 73 65 20 32 20 6f 66 20 69 56 61 6c 75 65 base 2 of iValue
8fdc0 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 73 ..**.** Examples
8fdd0 3a 20 20 20 6d 65 6d 73 79 73 35 4c 6f 67 28 31 : memsys5Log(1
8fde0 29 20 2d 3e 20 30 0a 2a 2a 20 20 20 20 20 20 20 ) -> 0.**
8fdf0 20 20 20 20 20 20 6d 65 6d 73 79 73 35 4c 6f 67 memsys5Log
8fe00 28 32 29 20 2d 3e 20 31 0a 2a 2a 20 20 20 20 20 (2) -> 1.**
8fe10 20 20 20 20 20 20 20 20 6d 65 6d 73 79 73 35 4c memsys5L
8fe20 6f 67 28 34 29 20 2d 3e 20 32 0a 2a 2a 20 20 20 og(4) -> 2.**
8fe30 20 20 20 20 20 20 20 20 20 20 6d 65 6d 73 79 73 memsys
8fe40 35 4c 6f 67 28 35 29 20 2d 3e 20 33 0a 2a 2a 20 5Log(5) -> 3.**
8fe50 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 73 mems
8fe60 79 73 35 4c 6f 67 28 38 29 20 2d 3e 20 33 0a 2a ys5Log(8) -> 3.*
8fe70 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 * me
8fe80 6d 73 79 73 35 4c 6f 67 28 39 29 20 2d 3e 20 34 msys5Log(9) -> 4
8fe90 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d .*/.static int m
8fea0 65 6d 73 79 73 35 4c 6f 67 28 69 6e 74 20 69 56 emsys5Log(int iV
8feb0 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 69 4c 6f alue){. int iLo
8fec0 67 3b 0a 20 20 66 6f 72 28 69 4c 6f 67 3d 30 3b g;. for(iLog=0;
8fed0 20 28 31 3c 3c 69 4c 6f 67 29 3c 69 56 61 6c 75 (1<<iLog)<iValu
8fee0 65 3b 20 69 4c 6f 67 2b 2b 29 3b 0a 20 20 72 65 e; iLog++);. re
8fef0 74 75 72 6e 20 69 4c 6f 67 3b 0a 7d 0a 0a 2f 2a turn iLog;.}../*
8ff00 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 .** Initialize t
8ff10 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 he memory alloca
8ff20 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 tor..**.** This
8ff30 72 6f 75 74 69 6e 65 20 69 73 20 6e 6f 74 20 74 routine is not t
8ff40 68 72 65 61 64 73 61 66 65 2e 20 20 54 68 65 20 hreadsafe. The
8ff50 63 61 6c 6c 65 72 20 6d 75 73 74 20 62 65 20 68 caller must be h
8ff60 6f 6c 64 69 6e 67 20 61 20 6d 75 74 65 78 0a 2a olding a mutex.*
8ff70 2a 20 74 6f 20 70 72 65 76 65 6e 74 20 6d 75 6c * to prevent mul
8ff80 74 69 70 6c 65 20 74 68 72 65 61 64 73 20 66 72 tiple threads fr
8ff90 6f 6d 20 65 6e 74 65 72 69 6e 67 20 61 74 20 74 om entering at t
8ffa0 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a 2a 2f he same time..*/
8ffb0 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 .static int mems
8ffc0 79 73 35 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f ys5Init(void *No
8ffd0 74 55 73 65 64 29 7b 0a 20 20 69 6e 74 20 69 69 tUsed){. int ii
8ffe0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
8fff0 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a Loop counter */.
90000 20 20 69 6e 74 20 6e 42 79 74 65 3b 20 20 20 20 int nByte;
90010 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
90020 66 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 f bytes of memor
90030 79 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 74 y available to t
90040 68 69 73 20 61 6c 6c 6f 63 61 74 6f 72 20 2a 2f his allocator */
90050 0a 20 20 75 38 20 2a 7a 42 79 74 65 3b 20 20 20 . u8 *zByte;
90060 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 /* Memory
90070 75 73 61 62 6c 65 20 62 79 20 74 68 69 73 20 61 usable by this a
90080 6c 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20 20 69 6e llocator */. in
90090 74 20 6e 4d 69 6e 4c 6f 67 3b 20 20 20 20 20 20 t nMinLog;
900a0 20 2f 2a 20 4c 6f 67 20 62 61 73 65 20 32 20 6f /* Log base 2 o
900b0 66 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 63 61 f minimum alloca
900c0 74 69 6f 6e 20 73 69 7a 65 20 69 6e 20 62 79 74 tion size in byt
900d0 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 66 66 es */. int iOff
900e0 73 65 74 3b 20 20 20 20 20 20 20 2f 2a 20 41 6e set; /* An
900f0 20 6f 66 66 73 65 74 20 69 6e 74 6f 20 6d 65 6d offset into mem
90100 35 2e 61 43 74 72 6c 5b 5d 20 2a 2f 0a 0a 20 20 5.aCtrl[] */..
90110 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
90120 28 4e 6f 74 55 73 65 64 29 3b 0a 0a 20 20 2f 2a (NotUsed);.. /*
90130 20 46 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 For the purpose
90140 73 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e s of this routin
90150 65 2c 20 64 69 73 61 62 6c 65 20 74 68 65 20 6d e, disable the m
90160 75 74 65 78 20 2a 2f 0a 20 20 6d 65 6d 35 2e 6d utex */. mem5.m
90170 75 74 65 78 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 utex = 0;.. /*
90180 54 68 65 20 73 69 7a 65 20 6f 66 20 61 20 4d 65 The size of a Me
90190 6d 35 4c 69 6e 6b 20 6f 62 6a 65 63 74 20 6d 75 m5Link object mu
901a0 73 74 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 st be a power of
901b0 20 74 77 6f 2e 20 20 56 65 72 69 66 79 20 74 68 two. Verify th
901c0 61 74 0a 20 20 2a 2a 20 74 68 69 73 20 69 73 20 at. ** this is
901d0 63 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 case.. */. ass
901e0 65 72 74 28 20 28 73 69 7a 65 6f 66 28 4d 65 6d ert( (sizeof(Mem
901f0 35 4c 69 6e 6b 29 26 28 73 69 7a 65 6f 66 28 4d 5Link)&(sizeof(M
90200 65 6d 35 4c 69 6e 6b 29 2d 31 29 29 3d 3d 30 20 em5Link)-1))==0
90210 29 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 73 71 );.. nByte = sq
90220 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
90230 67 2e 6e 48 65 61 70 3b 0a 20 20 7a 42 79 74 65 g.nHeap;. zByte
90240 20 3d 20 28 75 38 2a 29 73 71 6c 69 74 65 33 47 = (u8*)sqlite3G
90250 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 lobalConfig.pHea
90260 70 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 42 79 p;. assert( zBy
90270 74 65 21 3d 30 20 29 3b 20 20 2f 2a 20 73 71 6c te!=0 ); /* sql
90280 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 64 6f ite3_config() do
90290 65 73 20 6e 6f 74 20 61 6c 6c 6f 77 20 6f 74 68 es not allow oth
902a0 65 72 77 69 73 65 20 2a 2f 0a 0a 20 20 6e 4d 69 erwise */.. nMi
902b0 6e 4c 6f 67 20 3d 20 6d 65 6d 73 79 73 35 4c 6f nLog = memsys5Lo
902c0 67 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 g(sqlite3GlobalC
902d0 6f 6e 66 69 67 2e 6d 6e 52 65 71 29 3b 0a 20 20 onfig.mnReq);.
902e0 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 3d 20 28 31 mem5.szAtom = (1
902f0 3c 3c 6e 4d 69 6e 4c 6f 67 29 3b 0a 20 20 77 68 <<nMinLog);. wh
90300 69 6c 65 28 20 28 69 6e 74 29 73 69 7a 65 6f 66 ile( (int)sizeof
90310 28 4d 65 6d 35 4c 69 6e 6b 29 3e 6d 65 6d 35 2e (Mem5Link)>mem5.
90320 73 7a 41 74 6f 6d 20 29 7b 0a 20 20 20 20 6d 65 szAtom ){. me
90330 6d 35 2e 73 7a 41 74 6f 6d 20 3d 20 6d 65 6d 35 m5.szAtom = mem5
90340 2e 73 7a 41 74 6f 6d 20 3c 3c 20 31 3b 0a 20 20 .szAtom << 1;.
90350 7d 0a 0a 20 20 6d 65 6d 35 2e 6e 42 6c 6f 63 6b }.. mem5.nBlock
90360 20 3d 20 28 6e 42 79 74 65 20 2f 20 28 6d 65 6d = (nByte / (mem
90370 35 2e 73 7a 41 74 6f 6d 2b 73 69 7a 65 6f 66 28 5.szAtom+sizeof(
90380 75 38 29 29 29 3b 0a 20 20 6d 65 6d 35 2e 7a 50 u8)));. mem5.zP
90390 6f 6f 6c 20 3d 20 7a 42 79 74 65 3b 0a 20 20 6d ool = zByte;. m
903a0 65 6d 35 2e 61 43 74 72 6c 20 3d 20 28 75 38 20 em5.aCtrl = (u8
903b0 2a 29 26 6d 65 6d 35 2e 7a 50 6f 6f 6c 5b 6d 65 *)&mem5.zPool[me
903c0 6d 35 2e 6e 42 6c 6f 63 6b 2a 6d 65 6d 35 2e 73 m5.nBlock*mem5.s
903d0 7a 41 74 6f 6d 5d 3b 0a 0a 20 20 66 6f 72 28 69 zAtom];.. for(i
903e0 69 3d 30 3b 20 69 69 3c 3d 4c 4f 47 4d 41 58 3b i=0; ii<=LOGMAX;
903f0 20 69 69 2b 2b 29 7b 0a 20 20 20 20 6d 65 6d 35 ii++){. mem5
90400 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 69 5d 20 .aiFreelist[ii]
90410 3d 20 2d 31 3b 0a 20 20 7d 0a 0a 20 20 69 4f 66 = -1;. }.. iOf
90420 66 73 65 74 20 3d 20 30 3b 0a 20 20 66 6f 72 28 fset = 0;. for(
90430 69 69 3d 4c 4f 47 4d 41 58 3b 20 69 69 3e 3d 30 ii=LOGMAX; ii>=0
90440 3b 20 69 69 2d 2d 29 7b 0a 20 20 20 20 69 6e 74 ; ii--){. int
90450 20 6e 41 6c 6c 6f 63 20 3d 20 28 31 3c 3c 69 69 nAlloc = (1<<ii
90460 29 3b 0a 20 20 20 20 69 66 28 20 28 69 4f 66 66 );. if( (iOff
90470 73 65 74 2b 6e 41 6c 6c 6f 63 29 3c 3d 6d 65 6d set+nAlloc)<=mem
90480 35 2e 6e 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 5.nBlock ){.
90490 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 4f 66 mem5.aCtrl[iOf
904a0 66 73 65 74 5d 20 3d 20 69 69 20 7c 20 43 54 52 fset] = ii | CTR
904b0 4c 5f 46 52 45 45 3b 0a 20 20 20 20 20 20 6d 65 L_FREE;. me
904c0 6d 73 79 73 35 4c 69 6e 6b 28 69 4f 66 66 73 65 msys5Link(iOffse
904d0 74 2c 20 69 69 29 3b 0a 20 20 20 20 20 20 69 4f t, ii);. iO
904e0 66 66 73 65 74 20 2b 3d 20 6e 41 6c 6c 6f 63 3b ffset += nAlloc;
904f0 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 . }. asser
90500 74 28 28 69 4f 66 66 73 65 74 2b 6e 41 6c 6c 6f t((iOffset+nAllo
90510 63 29 3e 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 29 3b c)>mem5.nBlock);
90520 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 20 . }.. /* If a
90530 6d 75 74 65 78 20 69 73 20 72 65 71 75 69 72 65 mutex is require
90540 64 20 66 6f 72 20 6e 6f 72 6d 61 6c 20 6f 70 65 d for normal ope
90550 72 61 74 69 6f 6e 2c 20 61 6c 6c 6f 63 61 74 65 ration, allocate
90560 20 6f 6e 65 20 2a 2f 0a 20 20 69 66 28 20 73 71 one */. if( sq
90570 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
90580 67 2e 62 4d 65 6d 73 74 61 74 3d 3d 30 20 29 7b g.bMemstat==0 ){
90590 0a 20 20 20 20 6d 65 6d 35 2e 6d 75 74 65 78 20 . mem5.mutex
905a0 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c = sqlite3MutexAl
905b0 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 loc(SQLITE_MUTEX
905c0 5f 53 54 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 20 _STATIC_MEM);.
905d0 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }.. return SQLI
905e0 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
905f0 44 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 Deinitialize thi
90600 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 s module..*/.sta
90610 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 tic void memsys5
90620 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e Shutdown(void *N
90630 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 otUsed){. UNUSE
90640 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 D_PARAMETER(NotU
90650 73 65 64 29 3b 0a 20 20 6d 65 6d 35 2e 6d 75 74 sed);. mem5.mut
90660 65 78 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e ex = 0;. return
90670 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 ;.}..#ifdef SQLI
90680 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 4f 70 TE_TEST./*.** Op
90690 65 6e 20 74 68 65 20 66 69 6c 65 20 69 6e 64 69 en the file indi
906a0 63 61 74 65 64 20 61 6e 64 20 77 72 69 74 65 20 cated and write
906b0 61 20 6c 6f 67 20 6f 66 20 61 6c 6c 20 75 6e 66 a log of all unf
906c0 72 65 65 64 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 reed memory .**
906d0 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 69 6e 74 6f allocations into
906e0 20 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a 53 51 that log..*/.SQ
906f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
90700 64 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 35 d sqlite3Memsys5
90710 44 75 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 Dump(const char
90720 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 46 *zFilename){. F
90730 49 4c 45 20 2a 6f 75 74 3b 0a 20 20 69 6e 74 20 ILE *out;. int
90740 69 2c 20 6a 2c 20 6e 3b 0a 20 20 69 6e 74 20 6e i, j, n;. int n
90750 4d 69 6e 4c 6f 67 3b 0a 0a 20 20 69 66 28 20 7a MinLog;.. if( z
90760 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 7c 7c 20 7a Filename==0 || z
90770 46 69 6c 65 6e 61 6d 65 5b 30 5d 3d 3d 30 20 29 Filename[0]==0 )
90780 7b 0a 20 20 20 20 6f 75 74 20 3d 20 73 74 64 6f {. out = stdo
90790 75 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ut;. }else{.
907a0 20 6f 75 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69 out = fopen(zFi
907b0 6c 65 6e 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20 lename, "w");.
907c0 20 20 69 66 28 20 6f 75 74 3d 3d 30 20 29 7b 0a if( out==0 ){.
907d0 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 fprintf(st
907e0 64 65 72 72 2c 20 22 2a 2a 20 55 6e 61 62 6c 65 derr, "** Unable
907f0 20 74 6f 20 6f 75 74 70 75 74 20 6d 65 6d 6f 72 to output memor
90800 79 20 64 65 62 75 67 20 6f 75 74 70 75 74 20 6c y debug output l
90810 6f 67 3a 20 25 73 20 2a 2a 5c 6e 22 2c 0a 20 20 og: %s **\n",.
90820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
90830 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a zFilename);.
90840 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 return;.
90850 20 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 }. }. memsys
90860 35 45 6e 74 65 72 28 29 3b 0a 20 20 6e 4d 69 6e 5Enter();. nMin
90870 4c 6f 67 20 3d 20 6d 65 6d 73 79 73 35 4c 6f 67 Log = memsys5Log
90880 28 6d 65 6d 35 2e 73 7a 41 74 6f 6d 29 3b 0a 20 (mem5.szAtom);.
90890 20 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 4c 4f 47 for(i=0; i<=LOG
908a0 4d 41 58 20 26 26 20 69 2b 6e 4d 69 6e 4c 6f 67 MAX && i+nMinLog
908b0 3c 33 32 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 66 <32; i++){. f
908c0 6f 72 28 6e 3d 30 2c 20 6a 3d 6d 65 6d 35 2e 61 or(n=0, j=mem5.a
908d0 69 46 72 65 65 6c 69 73 74 5b 69 5d 3b 20 6a 3e iFreelist[i]; j>
908e0 3d 30 3b 20 6a 20 3d 20 4d 45 4d 35 4c 49 4e 4b =0; j = MEM5LINK
908f0 28 6a 29 2d 3e 6e 65 78 74 2c 20 6e 2b 2b 29 7b (j)->next, n++){
90900 7d 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 }. fprintf(ou
90910 74 2c 20 22 66 72 65 65 6c 69 73 74 20 69 74 65 t, "freelist ite
90920 6d 73 20 6f 66 20 73 69 7a 65 20 25 64 3a 20 25 ms of size %d: %
90930 64 5c 6e 22 2c 20 6d 65 6d 35 2e 73 7a 41 74 6f d\n", mem5.szAto
90940 6d 20 3c 3c 20 69 2c 20 6e 29 3b 0a 20 20 7d 0a m << i, n);. }.
90950 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
90960 6d 65 6d 35 2e 6e 41 6c 6c 6f 63 20 20 20 20 20 mem5.nAlloc
90970 20 20 3d 20 25 6c 6c 75 5c 6e 22 2c 20 6d 65 6d = %llu\n", mem
90980 35 2e 6e 41 6c 6c 6f 63 29 3b 0a 20 20 66 70 72 5.nAlloc);. fpr
90990 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e intf(out, "mem5.
909a0 74 6f 74 61 6c 41 6c 6c 6f 63 20 20 20 3d 20 25 totalAlloc = %
909b0 6c 6c 75 5c 6e 22 2c 20 6d 65 6d 35 2e 74 6f 74 llu\n", mem5.tot
909c0 61 6c 41 6c 6c 6f 63 29 3b 0a 20 20 66 70 72 69 alAlloc);. fpri
909d0 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 74 ntf(out, "mem5.t
909e0 6f 74 61 6c 45 78 63 65 73 73 20 20 3d 20 25 6c otalExcess = %l
909f0 6c 75 5c 6e 22 2c 20 6d 65 6d 35 2e 74 6f 74 61 lu\n", mem5.tota
90a00 6c 45 78 63 65 73 73 29 3b 0a 20 20 66 70 72 69 lExcess);. fpri
90a10 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 63 ntf(out, "mem5.c
90a20 75 72 72 65 6e 74 4f 75 74 20 20 20 3d 20 25 75 urrentOut = %u
90a30 5c 6e 22 2c 20 6d 65 6d 35 2e 63 75 72 72 65 6e \n", mem5.curren
90a40 74 4f 75 74 29 3b 0a 20 20 66 70 72 69 6e 74 66 tOut);. fprintf
90a50 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 63 75 72 72 (out, "mem5.curr
90a60 65 6e 74 43 6f 75 6e 74 20 3d 20 25 75 5c 6e 22 entCount = %u\n"
90a70 2c 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f , mem5.currentCo
90a80 75 6e 74 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 unt);. fprintf(
90a90 6f 75 74 2c 20 22 6d 65 6d 35 2e 6d 61 78 4f 75 out, "mem5.maxOu
90aa0 74 20 20 20 20 20 20 20 3d 20 25 75 5c 6e 22 2c t = %u\n",
90ab0 20 6d 65 6d 35 2e 6d 61 78 4f 75 74 29 3b 0a 20 mem5.maxOut);.
90ac0 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d fprintf(out, "m
90ad0 65 6d 35 2e 6d 61 78 43 6f 75 6e 74 20 20 20 20 em5.maxCount
90ae0 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6d = %u\n", mem5.m
90af0 61 78 43 6f 75 6e 74 29 3b 0a 20 20 66 70 72 69 axCount);. fpri
90b00 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 6d ntf(out, "mem5.m
90b10 61 78 52 65 71 75 65 73 74 20 20 20 3d 20 25 75 axRequest = %u
90b20 5c 6e 22 2c 20 6d 65 6d 35 2e 6d 61 78 52 65 71 \n", mem5.maxReq
90b30 75 65 73 74 29 3b 0a 20 20 6d 65 6d 73 79 73 35 uest);. memsys5
90b40 4c 65 61 76 65 28 29 3b 0a 20 20 69 66 28 20 6f Leave();. if( o
90b50 75 74 3d 3d 73 74 64 6f 75 74 20 29 7b 0a 20 20 ut==stdout ){.
90b60 20 20 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29 fflush(stdout)
90b70 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 ;. }else{. f
90b80 63 6c 6f 73 65 28 6f 75 74 29 3b 0a 20 20 7d 0a close(out);. }.
90b90 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
90ba0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
90bb0 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 the only routine
90bc0 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 77 69 in this file wi
90bd0 74 68 20 65 78 74 65 72 6e 61 6c 20 0a 2a 2a 20 th external .**
90be0 6c 69 6e 6b 61 67 65 2e 20 49 74 20 72 65 74 75 linkage. It retu
90bf0 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f rns a pointer to
90c00 20 61 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 a static sqlite
90c10 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 0a 2a 2a 3_mem_methods.**
90c20 20 73 74 72 75 63 74 20 70 6f 70 75 6c 61 74 65 struct populate
90c30 64 20 77 69 74 68 20 74 68 65 20 6d 65 6d 73 79 d with the memsy
90c40 73 35 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 53 s5 methods..*/.S
90c50 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f QLITE_PRIVATE co
90c60 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f nst sqlite3_mem_
90c70 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 methods *sqlite3
90c80 4d 65 6d 47 65 74 4d 65 6d 73 79 73 35 28 76 6f MemGetMemsys5(vo
90c90 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f id){. static co
90ca0 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f nst sqlite3_mem_
90cb0 6d 65 74 68 6f 64 73 20 6d 65 6d 73 79 73 35 4d methods memsys5M
90cc0 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 20 20 ethods = {.
90cd0 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 2c 0a 20 memsys5Malloc,.
90ce0 20 20 20 20 6d 65 6d 73 79 73 35 46 72 65 65 2c memsys5Free,
90cf0 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 52 65 61 . memsys5Rea
90d00 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d 65 6d 73 79 lloc,. memsy
90d10 73 35 53 69 7a 65 2c 0a 20 20 20 20 20 6d 65 6d s5Size,. mem
90d20 73 79 73 35 52 6f 75 6e 64 75 70 2c 0a 20 20 20 sys5Roundup,.
90d30 20 20 6d 65 6d 73 79 73 35 49 6e 69 74 2c 0a 20 memsys5Init,.
90d40 20 20 20 20 6d 65 6d 73 79 73 35 53 68 75 74 64 memsys5Shutd
90d50 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b own,. 0. };
90d60 0a 20 20 72 65 74 75 72 6e 20 26 6d 65 6d 73 79 . return &memsy
90d70 73 35 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 23 65 s5Methods;.}..#e
90d80 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 ndif /* SQLITE_E
90d90 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 20 2a 2f NABLE_MEMSYS5 */
90da0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
90db0 2a 20 45 6e 64 20 6f 66 20 6d 65 6d 35 2e 63 20 * End of mem5.c
90dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90df0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
90e00 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 * Begin file mut
90e10 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ex.c ***********
90e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90e40 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 /./*.** 2007 Aug
90e50 75 73 74 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 ust 14.**.** The
90e60 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
90e70 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
90e80 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
90e90 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
90ea0 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
90eb0 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
90ec0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
90ed0 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
90ee0 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
90ef0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
90f00 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
90f10 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
90f20 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
90f30 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
90f40 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
90f50 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
90f60 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
90f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90fb0 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 *****.** This fi
90fc0 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 le contains the
90fd0 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 C functions that
90fe0 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 implement mutex
90ff0 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 es..**.** This f
91000 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 ile contains cod
91010 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e e that is common
91020 20 61 63 72 6f 73 73 20 61 6c 6c 20 6d 75 74 65 across all mute
91030 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e x implementation
91040 73 2e 0a 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d s...**.** $Id: m
91050 75 74 65 78 2e 63 2c 76 20 31 2e 33 31 20 32 30 utex.c,v 1.31 20
91060 30 39 2f 30 37 2f 31 36 20 31 38 3a 32 31 3a 31 09/07/16 18:21:1
91070 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 8 drh Exp $.*/..
91080 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 #if defined(SQLI
91090 54 45 5f 44 45 42 55 47 29 20 26 26 20 21 64 65 TE_DEBUG) && !de
910a0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 fined(SQLITE_MUT
910b0 45 58 5f 4f 4d 49 54 29 0a 2f 2a 0a 2a 2a 20 46 EX_OMIT)./*.** F
910c0 6f 72 20 64 65 62 75 67 67 69 6e 67 20 70 75 72 or debugging pur
910d0 70 6f 73 65 73 2c 20 72 65 63 6f 72 64 20 77 68 poses, record wh
910e0 65 6e 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 en the mutex sub
910f0 73 79 73 74 65 6d 20 69 73 20 69 6e 69 74 69 61 system is initia
91100 6c 69 7a 65 64 0a 2a 2a 20 61 6e 64 20 75 6e 69 lized.** and uni
91110 6e 69 74 69 61 6c 69 7a 65 64 20 73 6f 20 74 68 nitialized so th
91120 61 74 20 77 65 20 63 61 6e 20 61 73 73 65 72 74 at we can assert
91130 28 29 20 69 66 20 74 68 65 72 65 20 69 73 20 61 () if there is a
91140 6e 20 61 74 74 65 6d 70 74 20 74 6f 0a 2a 2a 20 n attempt to.**
91150 61 6c 6c 6f 63 61 74 65 20 61 20 6d 75 74 65 78 allocate a mutex
91160 20 77 68 69 6c 65 20 74 68 65 20 73 79 73 74 65 while the syste
91170 6d 20 69 73 20 75 6e 69 6e 69 74 69 61 6c 69 7a m is uninitializ
91180 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 ed..*/.static SQ
91190 4c 49 54 45 5f 57 53 44 20 69 6e 74 20 6d 75 74 LITE_WSD int mut
911a0 65 78 49 73 49 6e 69 74 20 3d 20 30 3b 0a 23 65 exIsInit = 0;.#e
911b0 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 44 ndif /* SQLITE_D
911c0 45 42 55 47 20 2a 2f 0a 0a 0a 23 69 66 6e 64 65 EBUG */...#ifnde
911d0 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f f SQLITE_MUTEX_O
911e0 4d 49 54 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 MIT./*.** Initia
911f0 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 78 20 73 lize the mutex s
91200 79 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ystem..*/.SQLITE
91210 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
91220 69 74 65 33 4d 75 74 65 78 49 6e 69 74 28 76 6f ite3MutexInit(vo
91230 69 64 29 7b 20 0a 20 20 69 6e 74 20 72 63 20 3d id){ . int rc =
91240 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 SQLITE_OK;. if
91250 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 ( sqlite3GlobalC
91260 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 onfig.bCoreMutex
91270 20 29 7b 0a 20 20 20 20 69 66 28 20 21 73 71 6c ){. if( !sql
91280 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
91290 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 6c 6c .mutex.xMutexAll
912a0 6f 63 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 oc ){. /* I
912b0 66 20 74 68 65 20 78 4d 75 74 65 78 41 6c 6c 6f f the xMutexAllo
912c0 63 20 6d 65 74 68 6f 64 20 68 61 73 20 6e 6f 74 c method has not
912d0 20 62 65 65 6e 20 73 65 74 2c 20 74 68 65 6e 20 been set, then
912e0 74 68 65 20 75 73 65 72 20 64 69 64 20 6e 6f 74 the user did not
912f0 0a 20 20 20 20 20 20 2a 2a 20 69 6e 73 74 61 6c . ** instal
91300 6c 20 61 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d l a mutex implem
91310 65 6e 74 61 74 69 6f 6e 20 76 69 61 20 73 71 6c entation via sql
91320 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 70 72 ite3_config() pr
91330 69 6f 72 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a ior to . **
91340 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c sqlite3_initial
91350 69 7a 65 28 29 20 62 65 69 6e 67 20 63 61 6c 6c ize() being call
91360 65 64 2e 20 54 68 69 73 20 62 6c 6f 63 6b 20 63 ed. This block c
91370 6f 70 69 65 73 20 70 6f 69 6e 74 65 72 73 20 74 opies pointers t
91380 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 64 o. ** the d
91390 65 66 61 75 6c 74 20 69 6d 70 6c 65 6d 65 6e 74 efault implement
913a0 61 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 73 ation into the s
913b0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
913c0 69 67 20 73 74 72 75 63 74 75 72 65 2e 0a 20 20 ig structure..
913d0 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c */. sql
913e0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f ite3_mutex_metho
913f0 64 73 20 2a 70 46 72 6f 6d 20 3d 20 73 71 6c 69 ds *pFrom = sqli
91400 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28 te3DefaultMutex(
91410 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
91420 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a _mutex_methods *
91430 70 54 6f 20 3d 20 26 73 71 6c 69 74 65 33 47 6c pTo = &sqlite3Gl
91440 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 obalConfig.mutex
91450 3b 0a 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 ;.. memcpy(
91460 70 54 6f 2c 20 70 46 72 6f 6d 2c 20 6f 66 66 73 pTo, pFrom, offs
91470 65 74 6f 66 28 73 71 6c 69 74 65 33 5f 6d 75 74 etof(sqlite3_mut
91480 65 78 5f 6d 65 74 68 6f 64 73 2c 20 78 4d 75 74 ex_methods, xMut
91490 65 78 41 6c 6c 6f 63 29 29 3b 0a 20 20 20 20 20 exAlloc));.
914a0 20 6d 65 6d 63 70 79 28 26 70 54 6f 2d 3e 78 4d memcpy(&pTo->xM
914b0 75 74 65 78 46 72 65 65 2c 20 26 70 46 72 6f 6d utexFree, &pFrom
914c0 2d 3e 78 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 ->xMutexFree,.
914d0 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f sizeo
914e0 66 28 2a 70 54 6f 29 20 2d 20 6f 66 66 73 65 74 f(*pTo) - offset
914f0 6f 66 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 of(sqlite3_mutex
91500 5f 6d 65 74 68 6f 64 73 2c 20 78 4d 75 74 65 78 _methods, xMutex
91510 46 72 65 65 29 29 3b 0a 20 20 20 20 20 20 70 54 Free));. pT
91520 6f 2d 3e 78 4d 75 74 65 78 41 6c 6c 6f 63 20 3d o->xMutexAlloc =
91530 20 70 46 72 6f 6d 2d 3e 78 4d 75 74 65 78 41 6c pFrom->xMutexAl
91540 6c 6f 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 loc;. }. r
91550 63 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 c = sqlite3Globa
91560 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d lConfig.mutex.xM
91570 75 74 65 78 49 6e 69 74 28 29 3b 0a 20 20 7d 0a utexInit();. }.
91580 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
91590 45 42 55 47 0a 20 20 47 4c 4f 42 41 4c 28 69 6e EBUG. GLOBAL(in
915a0 74 2c 20 6d 75 74 65 78 49 73 49 6e 69 74 29 20 t, mutexIsInit)
915b0 3d 20 31 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 72 = 1;.#endif.. r
915c0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
915d0 2a 2a 20 53 68 75 74 64 6f 77 6e 20 74 68 65 20 ** Shutdown the
915e0 6d 75 74 65 78 20 73 79 73 74 65 6d 2e 20 54 68 mutex system. Th
915f0 69 73 20 63 61 6c 6c 20 66 72 65 65 73 20 72 65 is call frees re
91600 73 6f 75 72 63 65 73 20 61 6c 6c 6f 63 61 74 65 sources allocate
91610 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 4d d by.** sqlite3M
91620 75 74 65 78 49 6e 69 74 28 29 2e 0a 2a 2f 0a 53 utexInit()..*/.S
91630 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
91640 74 20 73 71 6c 69 74 65 33 4d 75 74 65 78 45 6e t sqlite3MutexEn
91650 64 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20 72 d(void){. int r
91660 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
91670 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 if( sqlite3Glob
91680 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 alConfig.mutex.x
91690 4d 75 74 65 78 45 6e 64 20 29 7b 0a 20 20 20 20 MutexEnd ){.
916a0 72 63 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 rc = sqlite3Glob
916b0 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 alConfig.mutex.x
916c0 4d 75 74 65 78 45 6e 64 28 29 3b 0a 20 20 7d 0a MutexEnd();. }.
916d0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
916e0 45 42 55 47 0a 20 20 47 4c 4f 42 41 4c 28 69 6e EBUG. GLOBAL(in
916f0 74 2c 20 6d 75 74 65 78 49 73 49 6e 69 74 29 20 t, mutexIsInit)
91700 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 72 = 0;.#endif.. r
91710 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
91720 2a 2a 20 52 65 74 72 69 65 76 65 20 61 20 70 6f ** Retrieve a po
91730 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74 69 inter to a stati
91740 63 20 6d 75 74 65 78 20 6f 72 20 61 6c 6c 6f 63 c mutex or alloc
91750 61 74 65 20 61 20 6e 65 77 20 64 79 6e 61 6d 69 ate a new dynami
91760 63 20 6f 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 c one..*/.SQLITE
91770 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 6d 75 74 _API sqlite3_mut
91780 65 78 20 2a 73 71 6c 69 74 65 33 5f 6d 75 74 65 ex *sqlite3_mute
91790 78 5f 61 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b x_alloc(int id){
917a0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
917b0 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 OMIT_AUTOINIT.
917c0 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 if( sqlite3_init
917d0 69 61 6c 69 7a 65 28 29 20 29 20 72 65 74 75 72 ialize() ) retur
917e0 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 n 0;.#endif. re
917f0 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 turn sqlite3Glob
91800 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 alConfig.mutex.x
91810 4d 75 74 65 78 41 6c 6c 6f 63 28 69 64 29 3b 0a MutexAlloc(id);.
91820 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 }..SQLITE_PRIVAT
91830 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 E sqlite3_mutex
91840 2a 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c *sqlite3MutexAll
91850 6f 63 28 69 6e 74 20 69 64 29 7b 0a 20 20 69 66 oc(int id){. if
91860 28 20 21 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c ( !sqlite3Global
91870 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 Config.bCoreMute
91880 78 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 x ){. return
91890 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 0;. }. assert(
918a0 20 47 4c 4f 42 41 4c 28 69 6e 74 2c 20 6d 75 74 GLOBAL(int, mut
918b0 65 78 49 73 49 6e 69 74 29 20 29 3b 0a 20 20 72 exIsInit) );. r
918c0 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f eturn sqlite3Glo
918d0 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e balConfig.mutex.
918e0 78 4d 75 74 65 78 41 6c 6c 6f 63 28 69 64 29 3b xMutexAlloc(id);
918f0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 .}../*.** Free a
91900 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 2e 0a dynamic mutex..
91910 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f */.SQLITE_API vo
91920 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 id sqlite3_mutex
91930 5f 66 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 _free(sqlite3_mu
91940 74 65 78 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 tex *p){. if( p
91950 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 ){. sqlite3G
91960 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 lobalConfig.mute
91970 78 2e 78 4d 75 74 65 78 46 72 65 65 28 70 29 3b x.xMutexFree(p);
91980 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 . }.}../*.** Ob
91990 74 61 69 6e 20 74 68 65 20 6d 75 74 65 78 20 70 tain the mutex p
919a0 2e 20 49 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 . If some other
919b0 74 68 72 65 61 64 20 61 6c 72 65 61 64 79 20 68 thread already h
919c0 61 73 20 74 68 65 20 6d 75 74 65 78 2c 20 62 6c as the mutex, bl
919d0 6f 63 6b 0a 2a 2a 20 75 6e 74 69 6c 20 69 74 20 ock.** until it
919e0 63 61 6e 20 62 65 20 6f 62 74 61 69 6e 65 64 2e can be obtained.
919f0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 .*/.SQLITE_API v
91a00 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 oid sqlite3_mute
91a10 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 5f x_enter(sqlite3_
91a20 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 66 28 mutex *p){. if(
91a30 20 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 p ){. sqlite
91a40 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 3GlobalConfig.mu
91a50 74 65 78 2e 78 4d 75 74 65 78 45 6e 74 65 72 28 tex.xMutexEnter(
91a60 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a p);. }.}../*.**
91a70 20 4f 62 74 61 69 6e 20 74 68 65 20 6d 75 74 65 Obtain the mute
91a80 78 20 70 2e 20 49 66 20 73 75 63 63 65 73 73 66 x p. If successf
91a90 75 6c 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ul, return SQLIT
91aa0 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65 2c E_OK. Otherwise,
91ab0 20 69 66 20 61 6e 6f 74 68 65 72 0a 2a 2a 20 74 if another.** t
91ac0 68 72 65 61 64 20 68 6f 6c 64 73 20 74 68 65 20 hread holds the
91ad0 6d 75 74 65 78 20 61 6e 64 20 69 74 20 63 61 6e mutex and it can
91ae0 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64 2c not be obtained,
91af0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 return SQLITE_B
91b00 55 53 59 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 USY..*/.SQLITE_A
91b10 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d PI int sqlite3_m
91b20 75 74 65 78 5f 74 72 79 28 73 71 6c 69 74 65 33 utex_try(sqlite3
91b30 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 6e _mutex *p){. in
91b40 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
91b50 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 ;. if( p ){.
91b60 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 return sqlite3G
91b70 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 lobalConfig.mute
91b80 78 2e 78 4d 75 74 65 78 54 72 79 28 70 29 3b 0a x.xMutexTry(p);.
91b90 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
91ba0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 .}../*.** The sq
91bb0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
91bc0 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 e() routine exit
91bd0 73 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 s a mutex that w
91be0 61 73 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a as previously.**
91bf0 20 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 20 entered by the
91c00 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68 same thread. Th
91c10 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e e behavior is un
91c20 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 6d defined if the m
91c30 75 74 65 78 20 0a 2a 2a 20 69 73 20 6e 6f 74 20 utex .** is not
91c40 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 currently entere
91c50 64 2e 20 49 66 20 61 20 4e 55 4c 4c 20 70 6f 69 d. If a NULL poi
91c60 6e 74 65 72 20 69 73 20 70 61 73 73 65 64 20 61 nter is passed a
91c70 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 0a 2a 2a s an argument.**
91c80 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
91c90 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 s a no-op..*/.SQ
91ca0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
91cb0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
91cc0 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 e(sqlite3_mutex
91cd0 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a *p){. if( p ){.
91ce0 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 sqlite3Globa
91cf0 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d lConfig.mutex.xM
91d00 75 74 65 78 4c 65 61 76 65 28 70 29 3b 0a 20 20 utexLeave(p);.
91d10 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 }.}..#ifndef NDE
91d20 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 BUG./*.** The sq
91d30 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
91d40 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d () and sqlite3_m
91d50 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 utex_notheld() r
91d60 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e outine are.** in
91d70 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69 tended for use i
91d80 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20 73 nside assert() s
91d90 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 51 tatements..*/.SQ
91da0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
91db0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
91dc0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 sqlite3_mutex *p
91dd0 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 ){. return p==0
91de0 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 || sqlite3Globa
91df0 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d lConfig.mutex.xM
91e00 75 74 65 78 48 65 6c 64 28 70 29 3b 0a 7d 0a 53 utexHeld(p);.}.S
91e10 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
91e20 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 lite3_mutex_noth
91e30 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 eld(sqlite3_mute
91e40 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 x *p){. return
91e50 70 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 47 p==0 || sqlite3G
91e60 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 lobalConfig.mute
91e70 78 2e 78 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 x.xMutexNotheld(
91e80 70 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 65 p);.}.#endif..#e
91e90 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d ndif /* SQLITE_M
91ea0 55 54 45 58 5f 4f 4d 49 54 20 2a 2f 0a 0a 2f 2a UTEX_OMIT */../*
91eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
91ec0 64 20 6f 66 20 6d 75 74 65 78 2e 63 20 2a 2a 2a d of mutex.c ***
91ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
91f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
91f10 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f 6e gin file mutex_n
91f20 6f 6f 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a oop.c **********
91f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
91f50 0a 2a 2a 20 32 30 30 38 20 4f 63 74 6f 62 65 72 .** 2008 October
91f60 20 30 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 07.**.** The au
91f70 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
91f80 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
91f90 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
91fa0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
91fb0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
91fc0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
91fd0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
91fe0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
91ff0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
92000 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
92010 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
92020 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
92030 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
92040 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
92050 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
92060 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
92070 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
92080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
92090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
920a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
920b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
920c0 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
920d0 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 contains the C f
920e0 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d unctions that im
920f0 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 2e plement mutexes.
92100 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c .**.** This impl
92110 65 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 20 74 68 ementation in th
92120 69 73 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 is file does not
92130 20 70 72 6f 76 69 64 65 20 61 6e 79 20 6d 75 74 provide any mut
92140 75 61 6c 0a 2a 2a 20 65 78 63 6c 75 73 69 6f 6e ual.** exclusion
92150 20 61 6e 64 20 69 73 20 74 68 75 73 20 73 75 69 and is thus sui
92160 74 61 62 6c 65 20 66 6f 72 20 75 73 65 20 6f 6e table for use on
92170 6c 79 20 69 6e 20 61 70 70 6c 69 63 61 74 69 6f ly in applicatio
92180 6e 73 0a 2a 2a 20 74 68 61 74 20 75 73 65 20 53 ns.** that use S
92190 51 4c 69 74 65 20 69 6e 20 61 20 73 69 6e 67 6c QLite in a singl
921a0 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 72 e thread. The r
921b0 6f 75 74 69 6e 65 73 20 64 65 66 69 6e 65 64 0a outines defined.
921c0 2a 2a 20 68 65 72 65 20 61 72 65 20 70 6c 61 63 ** here are plac
921d0 65 2d 68 6f 6c 64 65 72 73 2e 20 20 41 70 70 6c e-holders. Appl
921e0 69 63 61 74 69 6f 6e 73 20 63 61 6e 20 73 75 62 ications can sub
921f0 73 74 69 74 75 74 65 20 77 6f 72 6b 69 6e 67 0a stitute working.
92200 2a 2a 20 6d 75 74 65 78 20 72 6f 75 74 69 6e 65 ** mutex routine
92210 73 20 61 74 20 73 74 61 72 74 2d 74 69 6d 65 20 s at start-time
92220 75 73 69 6e 67 20 74 68 65 0a 2a 2a 0a 2a 2a 20 using the.**.**
92230 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 sqlite3_conf
92240 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 ig(SQLITE_CONFIG
92250 5f 4d 55 54 45 58 2c 2e 2e 2e 29 0a 2a 2a 0a 2a _MUTEX,...).**.*
92260 2a 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a * interface..**.
92270 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 65 64 20 77 ** If compiled w
92280 69 74 68 20 53 51 4c 49 54 45 5f 44 45 42 55 47 ith SQLITE_DEBUG
92290 2c 20 74 68 65 6e 20 61 64 64 69 74 69 6f 6e 61 , then additiona
922a0 6c 20 6c 6f 67 69 63 20 69 73 20 69 6e 73 65 72 l logic is inser
922b0 74 65 64 0a 2a 2a 20 74 68 61 74 20 64 6f 65 73 ted.** that does
922c0 20 65 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 20 error checking
922d0 6f 6e 20 6d 75 74 65 78 65 73 20 74 6f 20 6d 61 on mutexes to ma
922e0 6b 65 20 73 75 72 65 20 74 68 65 79 20 61 72 65 ke sure they are
922f0 20 62 65 69 6e 67 0a 2a 2a 20 63 61 6c 6c 65 64 being.** called
92300 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a 2a correctly..**.*
92310 2a 20 24 49 64 3a 20 6d 75 74 65 78 5f 6e 6f 6f * $Id: mutex_noo
92320 70 2e 63 2c 76 20 31 2e 33 20 32 30 30 38 2f 31 p.c,v 1.3 2008/1
92330 32 2f 30 35 20 31 37 3a 31 37 3a 30 38 20 64 72 2/05 17:17:08 dr
92340 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 23 69 66 h Exp $.*/...#if
92350 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
92360 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20 21 MUTEX_NOOP) && !
92370 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 defined(SQLITE_D
92380 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 53 74 75 62 EBUG)./*.** Stub
92390 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 61 6c routines for al
923a0 6c 20 6d 75 74 65 78 20 6d 65 74 68 6f 64 73 2e l mutex methods.
923b0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
923c0 69 6e 65 73 20 70 72 6f 76 69 64 65 20 6e 6f 20 ines provide no
923d0 6d 75 74 75 61 6c 20 65 78 63 6c 75 73 69 6f 6e mutual exclusion
923e0 20 6f 72 20 65 72 72 6f 72 20 63 68 65 63 6b 69 or error checki
923f0 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ng..*/.static in
92400 74 20 6e 6f 6f 70 4d 75 74 65 78 48 65 6c 64 28 t noopMutexHeld(
92410 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 sqlite3_mutex *p
92420 29 7b 20 72 65 74 75 72 6e 20 31 3b 20 7d 0a 73 ){ return 1; }.s
92430 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 tatic int noopMu
92440 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 texNotheld(sqlit
92450 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20 72 65 e3_mutex *p){ re
92460 74 75 72 6e 20 31 3b 20 7d 0a 73 74 61 74 69 63 turn 1; }.static
92470 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 49 6e int noopMutexIn
92480 69 74 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e it(void){ return
92490 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 SQLITE_OK; }.st
924a0 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 atic int noopMut
924b0 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 65 74 exEnd(void){ ret
924c0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d urn SQLITE_OK; }
924d0 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f .static sqlite3_
924e0 6d 75 74 65 78 20 2a 6e 6f 6f 70 4d 75 74 65 78 mutex *noopMutex
924f0 41 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b 20 72 Alloc(int id){ r
92500 65 74 75 72 6e 20 28 73 71 6c 69 74 65 33 5f 6d eturn (sqlite3_m
92510 75 74 65 78 2a 29 38 3b 20 7d 0a 73 74 61 74 69 utex*)8; }.stati
92520 63 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74 65 78 c void noopMutex
92530 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 Free(sqlite3_mut
92540 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 3b 20 ex *p){ return;
92550 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e 6f }.static void no
92560 6f 70 4d 75 74 65 78 45 6e 74 65 72 28 73 71 6c opMutexEnter(sql
92570 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20 ite3_mutex *p){
92580 72 65 74 75 72 6e 3b 20 7d 0a 73 74 61 74 69 63 return; }.static
92590 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 54 72 int noopMutexTr
925a0 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 y(sqlite3_mutex
925b0 2a 70 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 *p){ return SQLI
925c0 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 TE_OK; }.static
925d0 76 6f 69 64 20 6e 6f 6f 70 4d 75 74 65 78 4c 65 void noopMutexLe
925e0 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 ave(sqlite3_mute
925f0 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 3b 20 7d x *p){ return; }
92600 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ..SQLITE_PRIVATE
92610 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d sqlite3_mutex_m
92620 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 ethods *sqlite3D
92630 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 efaultMutex(void
92640 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 ){. static sqli
92650 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 te3_mutex_method
92660 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20 s sMutex = {.
92670 20 6e 6f 6f 70 4d 75 74 65 78 49 6e 69 74 2c 0a noopMutexInit,.
92680 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 45 6e 64 noopMutexEnd
92690 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 41 ,. noopMutexA
926a0 6c 6c 6f 63 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 lloc,. noopMu
926b0 74 65 78 46 72 65 65 2c 0a 20 20 20 20 6e 6f 6f texFree,. noo
926c0 70 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 pMutexEnter,.
926d0 20 6e 6f 6f 70 4d 75 74 65 78 54 72 79 2c 0a 20 noopMutexTry,.
926e0 20 20 20 6e 6f 6f 70 4d 75 74 65 78 4c 65 61 76 noopMutexLeav
926f0 65 2c 0a 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 e,.. noopMute
92700 78 48 65 6c 64 2c 0a 20 20 20 20 6e 6f 6f 70 4d xHeld,. noopM
92710 75 74 65 78 4e 6f 74 68 65 6c 64 0a 20 20 7d 3b utexNotheld. };
92720 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75 74 .. return &sMut
92730 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ex;.}.#endif /*
92740 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d defined(SQLITE_M
92750 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20 21 64 UTEX_NOOP) && !d
92760 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 efined(SQLITE_DE
92770 42 55 47 29 20 2a 2f 0a 0a 23 69 66 20 64 65 66 BUG) */..#if def
92780 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 45 ined(SQLITE_MUTE
92790 58 5f 4e 4f 4f 50 29 20 26 26 20 64 65 66 69 6e X_NOOP) && defin
927a0 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 ed(SQLITE_DEBUG)
927b0 0a 2f 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 69 ./*.** In this i
927c0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 65 mplementation, e
927d0 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 20 69 73 rror checking is
927e0 20 70 72 6f 76 69 64 65 64 20 66 6f 72 20 74 65 provided for te
927f0 73 74 69 6e 67 0a 2a 2a 20 61 6e 64 20 64 65 62 sting.** and deb
92800 75 67 67 69 6e 67 20 70 75 72 70 6f 73 65 73 2e ugging purposes.
92810 20 20 54 68 65 20 6d 75 74 65 78 65 73 20 73 74 The mutexes st
92820 69 6c 6c 20 64 6f 20 6e 6f 74 20 70 72 6f 76 69 ill do not provi
92830 64 65 20 61 6e 79 0a 2a 2a 20 6d 75 74 75 61 6c de any.** mutual
92840 20 65 78 63 6c 75 73 69 6f 6e 2e 0a 2a 2f 0a 0a exclusion..*/..
92850 2f 2a 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 /*.** The mutex
92860 6f 62 6a 65 63 74 0a 2a 2f 0a 73 74 72 75 63 74 object.*/.struct
92870 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b sqlite3_mutex {
92880 0a 20 20 69 6e 74 20 69 64 3b 20 20 20 20 20 2f . int id; /
92890 2a 20 54 68 65 20 6d 75 74 65 78 20 74 79 70 65 * The mutex type
928a0 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 */. int cnt;
928b0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 /* Number of e
928c0 6e 74 72 69 65 73 20 77 69 74 68 6f 75 74 20 61 ntries without a
928d0 20 6d 61 74 63 68 69 6e 67 20 6c 65 61 76 65 20 matching leave
928e0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 */.};../*.** The
928f0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
92900 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 eld() and sqlite
92910 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 3_mutex_notheld(
92920 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a ) routine are.**
92930 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 intended for us
92940 65 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 e inside assert(
92950 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f ) statements..*/
92960 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 62 75 .static int debu
92970 67 4d 75 74 65 78 48 65 6c 64 28 73 71 6c 69 74 gMutexHeld(sqlit
92980 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
92990 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 return p==0 || p
929a0 2d 3e 63 6e 74 3e 30 3b 0a 7d 0a 73 74 61 74 69 ->cnt>0;.}.stati
929b0 63 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78 c int debugMutex
929c0 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f Notheld(sqlite3_
929d0 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 mutex *p){. ret
929e0 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 63 urn p==0 || p->c
929f0 6e 74 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 nt==0;.}../*.**
92a00 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 Initialize and d
92a10 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 einitialize the
92a20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 2e mutex subsystem.
92a30 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 .*/.static int d
92a40 65 62 75 67 4d 75 74 65 78 49 6e 69 74 28 76 6f ebugMutexInit(vo
92a50 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 id){ return SQLI
92a60 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 TE_OK; }.static
92a70 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78 45 6e int debugMutexEn
92a80 64 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 d(void){ return
92a90 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f 2a SQLITE_OK; }../*
92aa0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
92ab0 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 6f mutex_alloc() ro
92ac0 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20 utine allocates
92ad0 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 61 a new.** mutex a
92ae0 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 nd returns a poi
92af0 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 49 66 20 nter to it. If
92b00 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 0a it returns NULL.
92b10 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 ** that means th
92b20 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75 6c 64 at a mutex could
92b30 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 not be allocate
92b40 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 d. .*/.static sq
92b50 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 64 65 62 lite3_mutex *deb
92b60 75 67 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 ugMutexAlloc(int
92b70 20 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 id){. static s
92b80 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 61 53 74 qlite3_mutex aSt
92b90 61 74 69 63 5b 36 5d 3b 0a 20 20 73 71 6c 69 74 atic[6];. sqlit
92ba0 65 33 5f 6d 75 74 65 78 20 2a 70 4e 65 77 20 3d e3_mutex *pNew =
92bb0 20 30 3b 0a 20 20 73 77 69 74 63 68 28 20 69 64 0;. switch( id
92bc0 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c ){. case SQL
92bd0 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 3a 0a ITE_MUTEX_FAST:.
92be0 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
92bf0 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 3a MUTEX_RECURSIVE:
92c00 20 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 {. pNew =
92c10 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 73 69 sqlite3Malloc(si
92c20 7a 65 6f 66 28 2a 70 4e 65 77 29 29 3b 0a 20 20 zeof(*pNew));.
92c30 20 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a if( pNew ){.
92c40 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 69 64 pNew->id
92c50 20 3d 20 69 64 3b 0a 20 20 20 20 20 20 20 20 70 = id;. p
92c60 4e 65 77 2d 3e 63 6e 74 20 3d 20 30 3b 0a 20 20 New->cnt = 0;.
92c70 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 }. brea
92c80 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 k;. }. def
92c90 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 ault: {. as
92ca0 73 65 72 74 28 20 69 64 2d 32 20 3e 3d 20 30 20 sert( id-2 >= 0
92cb0 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
92cc0 20 69 64 2d 32 20 3c 20 28 69 6e 74 29 28 73 69 id-2 < (int)(si
92cd0 7a 65 6f 66 28 61 53 74 61 74 69 63 29 2f 73 69 zeof(aStatic)/si
92ce0 7a 65 6f 66 28 61 53 74 61 74 69 63 5b 30 5d 29 zeof(aStatic[0])
92cf0 29 20 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 20 ) );. pNew
92d00 3d 20 26 61 53 74 61 74 69 63 5b 69 64 2d 32 5d = &aStatic[id-2]
92d10 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 69 64 ;. pNew->id
92d20 20 3d 20 69 64 3b 0a 20 20 20 20 20 20 62 72 65 = id;. bre
92d30 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 ak;. }. }.
92d40 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a return pNew;.}..
92d50 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
92d60 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 ne deallocates a
92d70 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f previously allo
92d80 63 61 74 65 64 20 6d 75 74 65 78 2e 0a 2a 2f 0a cated mutex..*/.
92d90 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 62 75 static void debu
92da0 67 4d 75 74 65 78 46 72 65 65 28 73 71 6c 69 74 gMutexFree(sqlit
92db0 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
92dc0 61 73 73 65 72 74 28 20 70 2d 3e 63 6e 74 3d 3d assert( p->cnt==
92dd0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
92de0 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 ->id==SQLITE_MUT
92df0 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64 EX_FAST || p->id
92e00 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 ==SQLITE_MUTEX_R
92e10 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 73 71 ECURSIVE );. sq
92e20 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 7d lite3_free(p);.}
92e30 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ../*.** The sqli
92e40 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
92e50 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 ) and sqlite3_mu
92e60 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69 6e tex_try() routin
92e70 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f es attempt.** to
92e80 20 65 6e 74 65 72 20 61 20 6d 75 74 65 78 2e 20 enter a mutex.
92e90 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65 If another thre
92ea0 61 64 20 69 73 20 61 6c 72 65 61 64 79 20 77 69 ad is already wi
92eb0 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78 2c 0a thin the mutex,.
92ec0 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ** sqlite3_mutex
92ed0 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 62 6c _enter() will bl
92ee0 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 33 5f ock and sqlite3_
92ef0 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69 6c 6c mutex_try() will
92f00 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 return.** SQLIT
92f10 45 5f 42 55 53 59 2e 20 20 54 68 65 20 73 71 6c E_BUSY. The sql
92f20 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 ite3_mutex_try()
92f30 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 interface retur
92f40 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 ns SQLITE_OK.**
92f50 75 70 6f 6e 20 73 75 63 63 65 73 73 66 75 6c 20 upon successful
92f60 65 6e 74 72 79 2e 20 20 4d 75 74 65 78 65 73 20 entry. Mutexes
92f70 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 53 51 created using SQ
92f80 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
92f90 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65 20 65 SIVE can.** be e
92fa0 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65 20 ntered multiple
92fb0 74 69 6d 65 73 20 62 79 20 74 68 65 20 73 61 6d times by the sam
92fc0 65 20 74 68 72 65 61 64 2e 20 20 49 6e 20 73 75 e thread. In su
92fd0 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a 2a ch cases the,.**
92fe0 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 65 mutex must be e
92ff0 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20 6e xited an equal n
93000 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 62 umber of times b
93010 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74 68 efore another th
93020 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 read.** can ente
93030 72 2e 20 20 49 66 20 74 68 65 20 73 61 6d 65 20 r. If the same
93040 74 68 72 65 61 64 20 74 72 69 65 73 20 74 6f 20 thread tries to
93050 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 72 20 enter any other
93060 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a 2a 2a kind of mutex.**
93070 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c more than once,
93080 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 the behavior is
93090 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 undefined..*/.s
930a0 74 61 74 69 63 20 76 6f 69 64 20 64 65 62 75 67 tatic void debug
930b0 4d 75 74 65 78 45 6e 74 65 72 28 73 71 6c 69 74 MutexEnter(sqlit
930c0 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
930d0 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 assert( p->id==S
930e0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
930f0 52 53 49 56 45 20 7c 7c 20 64 65 62 75 67 4d 75 RSIVE || debugMu
93100 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b texNotheld(p) );
93110 0a 20 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 7d 0a 73 . p->cnt++;.}.s
93120 74 61 74 69 63 20 69 6e 74 20 64 65 62 75 67 4d tatic int debugM
93130 75 74 65 78 54 72 79 28 73 71 6c 69 74 65 33 5f utexTry(sqlite3_
93140 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 mutex *p){. ass
93150 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 ert( p->id==SQLI
93160 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
93170 56 45 20 7c 7c 20 64 65 62 75 67 4d 75 74 65 78 VE || debugMutex
93180 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 Notheld(p) );.
93190 70 2d 3e 63 6e 74 2b 2b 3b 0a 20 20 72 65 74 75 p->cnt++;. retu
931a0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
931b0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 ./*.** The sqlit
931c0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 e3_mutex_leave()
931d0 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 routine exits a
931e0 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 0a mutex that was.
931f0 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e ** previously en
93200 74 65 72 65 64 20 62 79 20 74 68 65 20 73 61 6d tered by the sam
93210 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 62 e thread. The b
93220 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e ehavior.** is un
93230 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 6d defined if the m
93240 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75 72 72 utex is not curr
93250 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 6f 72 ently entered or
93260 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72 65 .** is not curre
93270 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 ntly allocated.
93280 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 SQLite will nev
93290 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2f er do either..*/
932a0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 62 .static void deb
932b0 75 67 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c ugMutexLeave(sql
932c0 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a ite3_mutex *p){.
932d0 20 20 61 73 73 65 72 74 28 20 64 65 62 75 67 4d assert( debugM
932e0 75 74 65 78 48 65 6c 64 28 70 29 20 29 3b 0a 20 utexHeld(p) );.
932f0 20 70 2d 3e 63 6e 74 2d 2d 3b 0a 20 20 61 73 73 p->cnt--;. ass
93300 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 ert( p->id==SQLI
93310 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
93320 56 45 20 7c 7c 20 64 65 62 75 67 4d 75 74 65 78 VE || debugMutex
93330 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 7d 0a Notheld(p) );.}.
93340 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
93350 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 sqlite3_mutex_me
93360 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 65 thods *sqlite3De
93370 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 faultMutex(void)
93380 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 {. static sqlit
93390 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 e3_mutex_methods
933a0 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20 20 sMutex = {.
933b0 64 65 62 75 67 4d 75 74 65 78 49 6e 69 74 2c 0a debugMutexInit,.
933c0 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 45 6e debugMutexEn
933d0 64 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 d,. debugMute
933e0 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 64 65 62 75 xAlloc,. debu
933f0 67 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 gMutexFree,.
93400 64 65 62 75 67 4d 75 74 65 78 45 6e 74 65 72 2c debugMutexEnter,
93410 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 54 . debugMutexT
93420 72 79 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 ry,. debugMut
93430 65 78 4c 65 61 76 65 2c 0a 0a 20 20 20 20 64 65 exLeave,.. de
93440 62 75 67 4d 75 74 65 78 48 65 6c 64 2c 0a 20 20 bugMutexHeld,.
93450 20 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 68 debugMutexNoth
93460 65 6c 64 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 eld. };.. retu
93470 72 6e 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 rn &sMutex;.}.#e
93480 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 ndif /* defined(
93490 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f SQLITE_MUTEX_NOO
934a0 50 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 P) && defined(SQ
934b0 4c 49 54 45 5f 44 45 42 55 47 29 20 2a 2f 0a 0a LITE_DEBUG) */..
934c0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
934d0 45 6e 64 20 6f 66 20 6d 75 74 65 78 5f 6e 6f 6f End of mutex_noo
934e0 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a p.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 2f 0a **************/.
93510 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
93520 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 Begin file mutex
93530 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a _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 2f 0a **************/.
93560 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73 /*.** 2007 Augus
93570 74 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 t 28.**.** The a
93580 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
93590 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
935a0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
935b0 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
935c0 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
935d0 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
935e0 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
935f0 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
93600 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
93610 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
93620 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
93630 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
93640 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
93650 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
93660 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
93670 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
93680 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 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 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 ***.** This file
936e0 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 contains the C
936f0 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 functions that i
93700 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 mplement mutexes
93710 20 66 6f 72 20 4f 53 2f 32 0a 2a 2a 0a 2a 2a 20 for OS/2.**.**
93720 24 49 64 3a 20 6d 75 74 65 78 5f 6f 73 32 2e 63 $Id: mutex_os2.c
93730 2c 76 20 31 2e 31 31 20 32 30 30 38 2f 31 31 2f ,v 1.11 2008/11/
93740 32 32 20 31 39 3a 35 30 3a 35 34 20 70 77 65 69 22 19:50:54 pwei
93750 6c 62 61 63 68 65 72 20 45 78 70 20 24 0a 2a 2f lbacher Exp $.*/
93760 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 ../*.** The code
93770 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73 in this file is
93780 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 53 51 only used if SQ
93790 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 20 69 LITE_MUTEX_OS2 i
937a0 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 53 65 s defined..** Se
937b0 65 20 74 68 65 20 6d 75 74 65 78 2e 68 20 66 69 e the mutex.h fi
937c0 6c 65 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a le for details..
937d0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
937e0 5f 4d 55 54 45 58 5f 4f 53 32 0a 0a 2f 2a 2a 2a _MUTEX_OS2../***
937f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93800 2a 2a 2a 20 4f 53 2f 32 20 4d 75 74 65 78 20 49 *** OS/2 Mutex I
93810 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a mplementation **
93820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93830 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ****.**.** This
93840 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
93850 66 20 6d 75 74 65 78 65 73 20 69 73 20 62 75 69 f mutexes is bui
93860 6c 74 20 75 73 69 6e 67 20 74 68 65 20 4f 53 2f lt using the OS/
93870 32 20 41 50 49 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 2 API..*/../*.**
93880 20 54 68 65 20 6d 75 74 65 78 20 6f 62 6a 65 63 The mutex objec
93890 74 0a 2a 2a 20 45 61 63 68 20 72 65 63 75 72 73 t.** Each recurs
938a0 69 76 65 20 6d 75 74 65 78 20 69 73 20 61 6e 20 ive mutex is an
938b0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
938c0 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
938d0 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 ure..*/.struct s
938e0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b 0a 20 qlite3_mutex {.
938f0 20 48 4d 54 58 20 6d 75 74 65 78 3b 20 20 20 20 HMTX mutex;
93900 20 20 20 2f 2a 20 4d 75 74 65 78 20 63 6f 6e 74 /* Mutex cont
93910 72 6f 6c 6c 69 6e 67 20 74 68 65 20 6c 6f 63 6b rolling the lock
93920 20 2a 2f 0a 20 20 69 6e 74 20 20 69 64 3b 20 20 */. int id;
93930 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 /* Mutex
93940 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20 20 type */. int
93950 6e 52 65 66 3b 20 20 20 20 20 20 20 20 2f 2a 20 nRef; /*
93960 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 Number of refere
93970 6e 63 65 73 20 2a 2f 0a 20 20 54 49 44 20 20 6f nces */. TID o
93980 77 6e 65 72 3b 20 20 20 20 20 20 20 2f 2a 20 54 wner; /* T
93990 68 72 65 61 64 20 68 6f 6c 64 69 6e 67 20 74 68 hread holding th
939a0 69 73 20 6d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a is mutex */.};..
939b0 23 64 65 66 69 6e 65 20 4f 53 32 5f 4d 55 54 45 #define OS2_MUTE
939c0 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 20 20 X_INITIALIZER
939d0 30 2c 30 2c 30 2c 30 0a 0a 2f 2a 0a 2a 2a 20 49 0,0,0,0../*.** I
939e0 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 nitialize and de
939f0 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d initialize the m
93a00 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 2e 0a utex subsystem..
93a10 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 */.static int os
93a20 32 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29 2MutexInit(void)
93a30 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f { return SQLITE_
93a40 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 OK; }.static int
93a50 20 6f 73 32 4d 75 74 65 78 45 6e 64 28 76 6f 69 os2MutexEnd(voi
93a60 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 d){ return SQLIT
93a70 45 5f 4f 4b 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 E_OK; }../*.** T
93a80 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 he sqlite3_mutex
93a90 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65 _alloc() routine
93aa0 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 allocates a new
93ab0 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 72 65 .** mutex and re
93ac0 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 turns a pointer
93ad0 74 6f 20 69 74 2e 20 20 49 66 20 69 74 20 72 65 to it. If it re
93ae0 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 turns NULL.** th
93af0 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 at means that a
93b00 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 mutex could not
93b10 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 0a 2a be allocated. .*
93b20 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 6e * SQLite will un
93b30 77 69 6e 64 20 69 74 73 20 73 74 61 63 6b 20 61 wind its stack a
93b40 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 nd return an err
93b50 6f 72 2e 20 20 54 68 65 20 61 72 67 75 6d 65 6e or. The argumen
93b60 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f t.** to sqlite3_
93b70 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73 mutex_alloc() is
93b80 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 69 6e one of these in
93b90 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a teger constants:
93ba0 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c .**.** <ul>.** <
93bb0 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 li> SQLITE_MUTE
93bc0 58 5f 46 41 53 54 20 20 20 20 20 20 20 20 20 20 X_FAST
93bd0 20 20 20 20 20 30 0a 2a 2a 20 3c 6c 69 3e 20 20 0.** <li>
93be0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
93bf0 55 52 53 49 56 45 20 20 20 20 20 20 20 20 20 20 URSIVE
93c00 31 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 1.** <li> SQLIT
93c10 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d E_MUTEX_STATIC_M
93c20 41 53 54 45 52 20 20 20 20 20 20 32 0a 2a 2a 20 ASTER 2.**
93c30 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
93c40 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 20 20 20 EX_STATIC_MEM
93c50 20 20 20 20 20 20 33 0a 2a 2a 20 3c 6c 69 3e 20 3.** <li>
93c60 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 SQLITE_MUTEX_ST
93c70 41 54 49 43 5f 50 52 4e 47 20 20 20 20 20 20 20 ATIC_PRNG
93c80 20 34 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 4.** </ul>.**.*
93c90 2a 20 54 68 65 20 66 69 72 73 74 20 74 77 6f 20 * The first two
93ca0 63 6f 6e 73 74 61 6e 74 73 20 63 61 75 73 65 20 constants cause
93cb0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c sqlite3_mutex_al
93cc0 6c 6f 63 28 29 20 74 6f 20 63 72 65 61 74 65 0a loc() to create.
93cd0 2a 2a 20 61 20 6e 65 77 20 6d 75 74 65 78 2e 20 ** a new mutex.
93ce0 20 54 68 65 20 6e 65 77 20 6d 75 74 65 78 20 69 The new mutex i
93cf0 73 20 72 65 63 75 72 73 69 76 65 20 77 68 65 6e s recursive when
93d00 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 SQLITE_MUTEX_RE
93d10 43 55 52 53 49 56 45 0a 2a 2a 20 69 73 20 75 73 CURSIVE.** is us
93d20 65 64 20 62 75 74 20 6e 6f 74 20 6e 65 63 65 73 ed but not neces
93d30 73 61 72 69 6c 79 20 73 6f 20 77 68 65 6e 20 53 sarily so when S
93d40 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 QLITE_MUTEX_FAST
93d50 20 69 73 20 75 73 65 64 2e 0a 2a 2a 20 54 68 65 is used..** The
93d60 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 mutex implement
93d70 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 6e ation does not n
93d80 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 64 69 eed to make a di
93d90 73 74 69 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 stinction.** bet
93da0 77 65 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 ween SQLITE_MUTE
93db0 58 5f 52 45 43 55 52 53 49 56 45 20 61 6e 64 20 X_RECURSIVE and
93dc0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
93dd0 54 20 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a 20 T if it does.**
93de0 6e 6f 74 20 77 61 6e 74 20 74 6f 2e 20 20 42 75 not want to. Bu
93df0 74 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f 6e t SQLite will on
93e00 6c 79 20 72 65 71 75 65 73 74 20 61 20 72 65 63 ly request a rec
93e10 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 6e 0a ursive mutex in.
93e20 2a 2a 20 63 61 73 65 73 20 77 68 65 72 65 20 69 ** cases where i
93e30 74 20 72 65 61 6c 6c 79 20 6e 65 65 64 73 20 6f t really needs o
93e40 6e 65 2e 20 20 49 66 20 61 20 66 61 73 74 65 72 ne. If a faster
93e50 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65 20 6d non-recursive m
93e60 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e utex.** implemen
93e70 74 61 74 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 tation is availa
93e80 62 6c 65 20 6f 6e 20 74 68 65 20 68 6f 73 74 20 ble on the host
93e90 70 6c 61 74 66 6f 72 6d 2c 20 74 68 65 20 6d 75 platform, the mu
93ea0 74 65 78 20 73 75 62 73 79 73 74 65 6d 0a 2a 2a tex subsystem.**
93eb0 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 73 75 might return su
93ec0 63 68 20 61 20 6d 75 74 65 78 20 69 6e 20 72 65 ch a mutex in re
93ed0 73 70 6f 6e 73 65 20 74 6f 20 53 51 4c 49 54 45 sponse to SQLITE
93ee0 5f 4d 55 54 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a _MUTEX_FAST..**.
93ef0 2a 2a 20 54 68 65 20 6f 74 68 65 72 20 61 6c 6c ** The other all
93f00 6f 77 65 64 20 70 61 72 61 6d 65 74 65 72 73 20 owed parameters
93f10 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 to sqlite3_mutex
93f20 5f 61 6c 6c 6f 63 28 29 20 65 61 63 68 20 72 65 _alloc() each re
93f30 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 turn.** a pointe
93f40 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 70 72 r to a static pr
93f50 65 65 78 69 73 74 69 6e 67 20 6d 75 74 65 78 2e eexisting mutex.
93f60 20 20 54 68 72 65 65 20 73 74 61 74 69 63 20 6d Three static m
93f70 75 74 65 78 65 73 20 61 72 65 0a 2a 2a 20 75 73 utexes are.** us
93f80 65 64 20 62 79 20 74 68 65 20 63 75 72 72 65 6e ed by the curren
93f90 74 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c t version of SQL
93fa0 69 74 65 2e 20 20 46 75 74 75 72 65 20 76 65 72 ite. Future ver
93fb0 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 0a sions of SQLite.
93fc0 2a 2a 20 6d 61 79 20 61 64 64 20 61 64 64 69 74 ** may add addit
93fd0 69 6f 6e 61 6c 20 73 74 61 74 69 63 20 6d 75 74 ional static mut
93fe0 65 78 65 73 2e 20 20 53 74 61 74 69 63 20 6d 75 exes. Static mu
93ff0 74 65 78 65 73 20 61 72 65 20 66 6f 72 20 69 6e texes are for in
94000 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 62 79 ternal.** use by
94010 20 53 51 4c 69 74 65 20 6f 6e 6c 79 2e 20 20 41 SQLite only. A
94020 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 pplications that
94030 20 75 73 65 20 53 51 4c 69 74 65 20 6d 75 74 65 use SQLite mute
94040 78 65 73 20 73 68 6f 75 6c 64 0a 2a 2a 20 75 73 xes should.** us
94050 65 20 6f 6e 6c 79 20 74 68 65 20 64 79 6e 61 6d e only the dynam
94060 69 63 20 6d 75 74 65 78 65 73 20 72 65 74 75 72 ic mutexes retur
94070 6e 65 64 20 62 79 20 53 51 4c 49 54 45 5f 4d 55 ned by SQLITE_MU
94080 54 45 58 5f 46 41 53 54 20 6f 72 0a 2a 2a 20 53 TEX_FAST or.** S
94090 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
940a0 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 RSIVE..**.** Not
940b0 65 20 74 68 61 74 20 69 66 20 6f 6e 65 20 6f 66 e that if one of
940c0 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 the dynamic mut
940d0 65 78 20 70 61 72 61 6d 65 74 65 72 73 20 28 53 ex parameters (S
940e0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 QLITE_MUTEX_FAST
940f0 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4d 55 .** or SQLITE_MU
94100 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 20 69 TEX_RECURSIVE) i
94110 73 20 75 73 65 64 20 74 68 65 6e 20 73 71 6c 69 s used then sqli
94120 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 te3_mutex_alloc(
94130 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 64 ).** returns a d
94140 69 66 66 65 72 65 6e 74 20 6d 75 74 65 78 20 6f ifferent mutex o
94150 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e 20 20 42 n every call. B
94160 75 74 20 66 6f 72 20 74 68 65 20 73 74 61 74 69 ut for the stati
94170 63 0a 2a 2a 20 6d 75 74 65 78 20 74 79 70 65 73 c.** mutex types
94180 2c 20 74 68 65 20 73 61 6d 65 20 6d 75 74 65 78 , the same mutex
94190 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 is returned on
941a0 65 76 65 72 79 20 63 61 6c 6c 20 74 68 61 74 20 every call that
941b0 68 61 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 has.** the same
941c0 74 79 70 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a type number..*/.
941d0 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d static sqlite3_m
941e0 75 74 65 78 20 2a 6f 73 32 4d 75 74 65 78 41 6c utex *os2MutexAl
941f0 6c 6f 63 28 69 6e 74 20 69 54 79 70 65 29 7b 0a loc(int iType){.
94200 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 sqlite3_mutex
94210 2a 70 20 3d 20 4e 55 4c 4c 3b 0a 20 20 73 77 69 *p = NULL;. swi
94220 74 63 68 28 20 69 54 79 70 65 20 29 7b 0a 20 20 tch( iType ){.
94230 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 case SQLITE_MU
94240 54 45 58 5f 46 41 53 54 3a 0a 20 20 20 20 63 61 TEX_FAST:. ca
94250 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f se SQLITE_MUTEX_
94260 52 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20 20 RECURSIVE: {.
94270 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 p = sqlite3Ma
94280 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 llocZero( sizeof
94290 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69 66 (*p) );. if
942a0 28 20 70 20 29 7b 0a 20 20 20 20 20 20 20 20 70 ( p ){. p
942b0 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 20 ->id = iType;.
942c0 20 20 20 20 20 20 69 66 28 20 44 6f 73 43 72 65 if( DosCre
942d0 61 74 65 4d 75 74 65 78 53 65 6d 28 20 30 2c 20 ateMutexSem( 0,
942e0 26 70 2d 3e 6d 75 74 65 78 2c 20 30 2c 20 46 41 &p->mutex, 0, FA
942f0 4c 53 45 20 29 20 21 3d 20 4e 4f 5f 45 52 52 4f LSE ) != NO_ERRO
94300 52 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 R ){. s
94310 71 6c 69 74 65 33 5f 66 72 65 65 28 20 70 20 29 qlite3_free( p )
94320 3b 0a 20 20 20 20 20 20 20 20 20 20 70 20 3d 20 ;. p =
94330 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 20 20 7d 0a NULL;. }.
94340 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 }. br
94350 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 eak;. }. d
94360 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 efault: {.
94370 73 74 61 74 69 63 20 76 6f 6c 61 74 69 6c 65 20 static volatile
94380 69 6e 74 20 69 73 49 6e 69 74 20 3d 20 30 3b 0a int isInit = 0;.
94390 20 20 20 20 20 20 73 74 61 74 69 63 20 73 71 6c static sql
943a0 69 74 65 33 5f 6d 75 74 65 78 20 73 74 61 74 69 ite3_mutex stati
943b0 63 4d 75 74 65 78 65 73 5b 5d 20 3d 20 7b 0a 20 cMutexes[] = {.
943c0 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 { OS2_MUT
943d0 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 EX_INITIALIZER,
943e0 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f 53 32 },. { OS2
943f0 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a _MUTEX_INITIALIZ
94400 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b ER, },. {
94410 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 OS2_MUTEX_INITI
94420 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 ALIZER, },.
94430 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 { OS2_MUTEX_I
94440 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 NITIALIZER, },.
94450 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 { OS2_MUT
94460 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 EX_INITIALIZER,
94470 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f 53 32 },. { OS2
94480 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a _MUTEX_INITIALIZ
94490 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 7d 3b 0a ER, },. };.
944a0 20 20 20 20 20 20 69 66 20 28 20 21 69 73 49 6e if ( !isIn
944b0 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 41 50 it ){. AP
944c0 49 52 45 54 20 72 63 3b 0a 20 20 20 20 20 20 20 IRET rc;.
944d0 20 50 54 49 42 20 70 74 69 62 3b 0a 20 20 20 20 PTIB ptib;.
944e0 20 20 20 20 50 50 49 42 20 70 70 69 62 3b 0a 20 PPIB ppib;.
944f0 20 20 20 20 20 20 20 48 4d 54 58 20 6d 75 74 65 HMTX mute
94500 78 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 x;. char
94510 6e 61 6d 65 5b 33 32 5d 3b 0a 20 20 20 20 20 20 name[32];.
94520 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 DosGetInfoBloc
94530 6b 73 28 20 26 70 74 69 62 2c 20 26 70 70 69 62 ks( &ptib, &ppib
94540 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 );. sqli
94550 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 20 73 69 te3_snprintf( si
94560 7a 65 6f 66 28 6e 61 6d 65 29 2c 20 6e 61 6d 65 zeof(name), name
94570 2c 20 22 5c 5c 53 45 4d 33 32 5c 5c 53 51 4c 49 , "\\SEM32\\SQLI
94580 54 45 25 30 34 78 22 2c 0a 20 20 20 20 20 20 20 TE%04x",.
94590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
945a0 20 20 20 70 70 69 62 2d 3e 70 69 62 5f 75 6c 70 ppib->pib_ulp
945b0 69 64 20 29 3b 0a 20 20 20 20 20 20 20 20 77 68 id );. wh
945c0 69 6c 65 28 20 21 69 73 49 6e 69 74 20 29 7b 0a ile( !isInit ){.
945d0 20 20 20 20 20 20 20 20 20 20 6d 75 74 65 78 20 mutex
945e0 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 72 = 0;. r
945f0 63 20 3d 20 44 6f 73 43 72 65 61 74 65 4d 75 74 c = DosCreateMut
94600 65 78 53 65 6d 28 20 6e 61 6d 65 2c 20 26 6d 75 exSem( name, &mu
94610 74 65 78 2c 20 30 2c 20 46 41 4c 53 45 29 3b 0a tex, 0, FALSE);.
94620 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 if( rc
94630 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a == NO_ERROR ){.
94640 20 20 20 20 20 20 20 20 20 20 20 20 75 6e 73 69 unsi
94650 67 6e 65 64 20 69 6e 74 20 69 3b 0a 20 20 20 20 gned int i;.
94660 20 20 20 20 20 20 20 20 69 66 28 20 21 69 73 49 if( !isI
94670 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 nit ){.
94680 20 20 20 20 20 66 6f 72 28 20 69 20 3d 20 30 3b for( i = 0;
94690 20 69 20 3c 20 73 69 7a 65 6f 66 28 73 74 61 74 i < sizeof(stat
946a0 69 63 4d 75 74 65 78 65 73 29 2f 73 69 7a 65 6f icMutexes)/sizeo
946b0 66 28 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b f(staticMutexes[
946c0 30 5d 29 3b 20 69 2b 2b 20 29 7b 0a 20 20 20 20 0]); i++ ){.
946d0 20 20 20 20 20 20 20 20 20 20 20 20 44 6f 73 43 DosC
946e0 72 65 61 74 65 4d 75 74 65 78 53 65 6d 28 20 30 reateMutexSem( 0
946f0 2c 20 26 73 74 61 74 69 63 4d 75 74 65 78 65 73 , &staticMutexes
94700 5b 69 5d 2e 6d 75 74 65 78 2c 20 30 2c 20 46 41 [i].mutex, 0, FA
94710 4c 53 45 20 29 3b 0a 20 20 20 20 20 20 20 20 20 LSE );.
94720 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
94730 20 20 20 20 20 69 73 49 6e 69 74 20 3d 20 31 3b isInit = 1;
94740 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 . }.
94750 20 20 20 20 20 20 20 20 20 20 20 44 6f 73 43 6c DosCl
94760 6f 73 65 4d 75 74 65 78 53 65 6d 28 20 6d 75 74 oseMutexSem( mut
94770 65 78 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ex );.
94780 7d 65 6c 73 65 20 69 66 28 20 72 63 20 3d 3d 20 }else if( rc ==
94790 45 52 52 4f 52 5f 44 55 50 4c 49 43 41 54 45 5f ERROR_DUPLICATE_
947a0 4e 41 4d 45 20 29 7b 0a 20 20 20 20 20 20 20 20 NAME ){.
947b0 20 20 20 20 44 6f 73 53 6c 65 65 70 28 20 31 20 DosSleep( 1
947c0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c );. }el
947d0 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 se{.
947e0 72 65 74 75 72 6e 20 70 3b 0a 20 20 20 20 20 20 return p;.
947f0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a }. }.
94800 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 }. as
94810 73 65 72 74 28 20 69 54 79 70 65 2d 32 20 3e 3d sert( iType-2 >=
94820 20 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 0 );. asse
94830 72 74 28 20 69 54 79 70 65 2d 32 20 3c 20 73 69 rt( iType-2 < si
94840 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65 78 zeof(staticMutex
94850 65 73 29 2f 73 69 7a 65 6f 66 28 73 74 61 74 69 es)/sizeof(stati
94860 63 4d 75 74 65 78 65 73 5b 30 5d 29 20 29 3b 0a cMutexes[0]) );.
94870 20 20 20 20 20 20 70 20 3d 20 26 73 74 61 74 69 p = &stati
94880 63 4d 75 74 65 78 65 73 5b 69 54 79 70 65 2d 32 cMutexes[iType-2
94890 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64 20 3d ];. p->id =
948a0 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 62 72 iType;. br
948b0 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 eak;. }. }.
948c0 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f return p;.}.../
948d0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
948e0 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 20 e deallocates a
948f0 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 previously alloc
94900 61 74 65 64 20 6d 75 74 65 78 2e 0a 2a 2a 20 53 ated mutex..** S
94910 51 4c 69 74 65 20 69 73 20 63 61 72 65 66 75 6c QLite is careful
94920 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20 65 to deallocate e
94930 76 65 72 79 20 6d 75 74 65 78 20 74 68 61 74 20 very mutex that
94940 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e 0a 2a 2f it allocates..*/
94950 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 32 .static void os2
94960 4d 75 74 65 78 46 72 65 65 28 73 71 6c 69 74 65 MutexFree(sqlite
94970 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 3_mutex *p){. i
94980 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e f( p==0 ) return
94990 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e ;. assert( p->n
949a0 52 65 66 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 Ref==0 );. asse
949b0 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 rt( p->id==SQLIT
949c0 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 7c 7c 20 E_MUTEX_FAST ||
949d0 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 p->id==SQLITE_MU
949e0 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b TEX_RECURSIVE );
949f0 0a 20 20 44 6f 73 43 6c 6f 73 65 4d 75 74 65 78 . DosCloseMutex
94a00 53 65 6d 28 20 70 2d 3e 6d 75 74 65 78 20 29 3b Sem( p->mutex );
94a10 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 . sqlite3_free(
94a20 20 70 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 p );.}../*.** T
94a30 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 he sqlite3_mutex
94a40 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71 6c _enter() and sql
94a50 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 ite3_mutex_try()
94a60 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d 70 routines attemp
94a70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 t.** to enter a
94a80 6d 75 74 65 78 2e 20 20 49 66 20 61 6e 6f 74 68 mutex. If anoth
94a90 65 72 20 74 68 72 65 61 64 20 69 73 20 61 6c 72 er thread is alr
94aa0 65 61 64 79 20 77 69 74 68 69 6e 20 74 68 65 20 eady within the
94ab0 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 mutex,.** sqlite
94ac0 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 3_mutex_enter()
94ad0 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73 will block and s
94ae0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 qlite3_mutex_try
94af0 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a () will return.*
94b00 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 * SQLITE_BUSY.
94b10 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 The sqlite3_mute
94b20 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61 63 x_try() interfac
94b30 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 e returns SQLITE
94b40 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 63 _OK.** upon succ
94b50 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20 4d essful entry. M
94b60 75 74 65 78 65 73 20 63 72 65 61 74 65 64 20 75 utexes created u
94b70 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54 45 sing SQLITE_MUTE
94b80 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e 0a X_RECURSIVE can.
94b90 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75 ** be entered mu
94ba0 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20 ltiple times by
94bb0 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e the same thread.
94bc0 20 20 49 6e 20 73 75 63 68 20 63 61 73 65 73 20 In such cases
94bd0 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 the,.** mutex mu
94be0 73 74 20 62 65 20 65 78 69 74 65 64 20 61 6e 20 st be exited an
94bf0 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 equal number of
94c00 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e 6f times before ano
94c10 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 63 ther thread.** c
94c20 61 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 74 68 an enter. If th
94c30 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 74 72 e same thread tr
94c40 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 79 ies to enter any
94c50 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 6d other kind of m
94c60 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 utex.** more tha
94c70 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 61 n once, the beha
94c80 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 vior is undefine
94c90 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 d..*/.static voi
94ca0 64 20 6f 73 32 4d 75 74 65 78 45 6e 74 65 72 28 d os2MutexEnter(
94cb0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 sqlite3_mutex *p
94cc0 29 7b 0a 20 20 54 49 44 20 74 69 64 3b 0a 20 20 ){. TID tid;.
94cd0 50 49 44 20 68 6f 6c 64 65 72 31 3b 0a 20 20 55 PID holder1;. U
94ce0 4c 4f 4e 47 20 68 6f 6c 64 65 72 32 3b 0a 20 20 LONG holder2;.
94cf0 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 if( p==0 ) retur
94d00 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e n;. assert( p->
94d10 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 id==SQLITE_MUTEX
94d20 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 6f 73 _RECURSIVE || os
94d30 32 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 2MutexNotheld(p)
94d40 20 29 3b 0a 20 20 44 6f 73 52 65 71 75 65 73 74 );. DosRequest
94d50 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 MutexSem(p->mute
94d60 78 2c 20 53 45 4d 5f 49 4e 44 45 46 49 4e 49 54 x, SEM_INDEFINIT
94d70 45 5f 57 41 49 54 29 3b 0a 20 20 44 6f 73 51 75 E_WAIT);. DosQu
94d80 65 72 79 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d eryMutexSem(p->m
94d90 75 74 65 78 2c 20 26 68 6f 6c 64 65 72 31 2c 20 utex, &holder1,
94da0 26 74 69 64 2c 20 26 68 6f 6c 64 65 72 32 29 3b &tid, &holder2);
94db0 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 74 69 . p->owner = ti
94dc0 64 3b 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a d;. p->nRef++;.
94dd0 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 }.static int os2
94de0 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74 65 33 MutexTry(sqlite3
94df0 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 6e _mutex *p){. in
94e00 74 20 72 63 3b 0a 20 20 54 49 44 20 74 69 64 3b t rc;. TID tid;
94e10 0a 20 20 50 49 44 20 68 6f 6c 64 65 72 31 3b 0a . PID holder1;.
94e20 20 20 55 4c 4f 4e 47 20 68 6f 6c 64 65 72 32 3b ULONG holder2;
94e30 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 . if( p==0 ) re
94e40 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
94e50 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d assert( p->id=
94e60 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 =SQLITE_MUTEX_RE
94e70 43 55 52 53 49 56 45 20 7c 7c 20 6f 73 32 4d 75 CURSIVE || os2Mu
94e80 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b texNotheld(p) );
94e90 0a 20 20 69 66 28 20 44 6f 73 52 65 71 75 65 73 . if( DosReques
94ea0 74 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 tMutexSem(p->mut
94eb0 65 78 2c 20 53 45 4d 5f 49 4d 4d 45 44 49 41 54 ex, SEM_IMMEDIAT
94ec0 45 5f 52 45 54 55 52 4e 29 20 3d 3d 20 4e 4f 5f E_RETURN) == NO_
94ed0 45 52 52 4f 52 29 20 7b 0a 20 20 20 20 44 6f 73 ERROR) {. Dos
94ee0 51 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70 2d QueryMutexSem(p-
94ef0 3e 6d 75 74 65 78 2c 20 26 68 6f 6c 64 65 72 31 >mutex, &holder1
94f00 2c 20 26 74 69 64 2c 20 26 68 6f 6c 64 65 72 32 , &tid, &holder2
94f10 29 3b 0a 20 20 20 20 70 2d 3e 6f 77 6e 65 72 20 );. p->owner
94f20 3d 20 74 69 64 3b 0a 20 20 20 20 70 2d 3e 6e 52 = tid;. p->nR
94f30 65 66 2b 2b 3b 0a 20 20 20 20 72 63 20 3d 20 53 ef++;. rc = S
94f40 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 20 65 6c QLITE_OK;. } el
94f50 73 65 20 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 se {. rc = SQ
94f60 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 0a LITE_BUSY;. }..
94f70 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
94f80 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 /*.** The sqlite
94f90 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 3_mutex_leave()
94fa0 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 20 routine exits a
94fb0 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 0a 2a mutex that was.*
94fc0 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 * previously ent
94fd0 65 72 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 ered by the same
94fe0 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 62 65 thread. The be
94ff0 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e 64 havior.** is und
95000 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 6d 75 efined if the mu
95010 74 65 78 20 69 73 20 6e 6f 74 20 63 75 72 72 65 tex is not curre
95020 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 6f 72 0a ntly entered or.
95030 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e ** is not curren
95040 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 tly allocated.
95050 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 65 SQLite will neve
95060 72 20 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2f 0a r do either..*/.
95070 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 32 4d static void os2M
95080 75 74 65 78 4c 65 61 76 65 28 73 71 6c 69 74 65 utexLeave(sqlite
95090 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 54 3_mutex *p){. T
950a0 49 44 20 74 69 64 3b 0a 20 20 50 49 44 20 68 6f ID tid;. PID ho
950b0 6c 64 65 72 31 3b 0a 20 20 55 4c 4f 4e 47 20 68 lder1;. ULONG h
950c0 6f 6c 64 65 72 32 3b 0a 20 20 69 66 28 20 70 3d older2;. if( p=
950d0 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 =0 ) return;. a
950e0 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 ssert( p->nRef>0
950f0 20 29 3b 0a 20 20 44 6f 73 51 75 65 72 79 4d 75 );. DosQueryMu
95100 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c texSem(p->mutex,
95110 20 26 68 6f 6c 64 65 72 31 2c 20 26 74 69 64 2c &holder1, &tid,
95120 20 26 68 6f 6c 64 65 72 32 29 3b 0a 20 20 61 73 &holder2);. as
95130 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65 72 3d 3d sert( p->owner==
95140 74 69 64 20 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 tid );. p->nRef
95150 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d --;. assert( p-
95160 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e 69 >nRef==0 || p->i
95170 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f d==SQLITE_MUTEX_
95180 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 44 RECURSIVE );. D
95190 6f 73 52 65 6c 65 61 73 65 4d 75 74 65 78 53 65 osReleaseMutexSe
951a0 6d 28 70 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a 0a m(p->mutex);.}..
951b0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
951c0 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 BUG./*.** The sq
951d0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
951e0 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d () and sqlite3_m
951f0 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 utex_notheld() r
95200 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e outine are.** in
95210 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69 tended for use i
95220 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20 73 nside assert() s
95230 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 tatements..*/.st
95240 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74 65 atic int os2Mute
95250 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 xHeld(sqlite3_mu
95260 74 65 78 20 2a 70 29 7b 0a 20 20 54 49 44 20 74 tex *p){. TID t
95270 69 64 3b 0a 20 20 50 49 44 20 70 69 64 3b 0a 20 id;. PID pid;.
95280 20 55 4c 4f 4e 47 20 75 6c 43 6f 75 6e 74 3b 0a ULONG ulCount;.
95290 20 20 50 54 49 42 20 70 74 69 62 3b 0a 20 20 69 PTIB ptib;. i
952a0 66 28 20 70 21 3d 30 20 29 20 7b 0a 20 20 20 20 f( p!=0 ) {.
952b0 44 6f 73 51 75 65 72 79 4d 75 74 65 78 53 65 6d DosQueryMutexSem
952c0 28 70 2d 3e 6d 75 74 65 78 2c 20 26 70 69 64 2c (p->mutex, &pid,
952d0 20 26 74 69 64 2c 20 26 75 6c 43 6f 75 6e 74 29 &tid, &ulCount)
952e0 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 ;. } else {.
952f0 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b DosGetInfoBlock
95300 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c 29 3b 0a s(&ptib, NULL);.
95310 20 20 20 20 74 69 64 20 3d 20 70 74 69 62 2d 3e tid = ptib->
95320 74 69 62 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f tib_ptib2->tib2_
95330 75 6c 74 69 64 3b 0a 20 20 7d 0a 20 20 72 65 74 ultid;. }. ret
95340 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 28 70 2d 3e urn p==0 || (p->
95350 6e 52 65 66 21 3d 30 20 26 26 20 70 2d 3e 6f 77 nRef!=0 && p->ow
95360 6e 65 72 3d 3d 74 69 64 29 3b 0a 7d 0a 73 74 61 ner==tid);.}.sta
95370 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74 65 78 tic int os2Mutex
95380 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f Notheld(sqlite3_
95390 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 54 49 44 mutex *p){. TID
953a0 20 74 69 64 3b 0a 20 20 50 49 44 20 70 69 64 3b tid;. PID pid;
953b0 0a 20 20 55 4c 4f 4e 47 20 75 6c 43 6f 75 6e 74 . ULONG ulCount
953c0 3b 0a 20 20 50 54 49 42 20 70 74 69 62 3b 0a 20 ;. PTIB ptib;.
953d0 20 69 66 28 20 70 21 3d 20 30 20 29 20 7b 0a 20 if( p!= 0 ) {.
953e0 20 20 20 44 6f 73 51 75 65 72 79 4d 75 74 65 78 DosQueryMutex
953f0 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 70 Sem(p->mutex, &p
95400 69 64 2c 20 26 74 69 64 2c 20 26 75 6c 43 6f 75 id, &tid, &ulCou
95410 6e 74 29 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a nt);. } else {.
95420 20 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c DosGetInfoBl
95430 6f 63 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c ocks(&ptib, NULL
95440 29 3b 0a 20 20 20 20 74 69 64 20 3d 20 70 74 69 );. tid = pti
95450 62 2d 3e 74 69 62 5f 70 74 69 62 32 2d 3e 74 69 b->tib_ptib2->ti
95460 62 32 5f 75 6c 74 69 64 3b 0a 20 20 7d 0a 20 20 b2_ultid;. }.
95470 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 return p==0 || p
95480 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e ->nRef==0 || p->
95490 6f 77 6e 65 72 21 3d 74 69 64 3b 0a 7d 0a 23 65 owner!=tid;.}.#e
954a0 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 ndif..SQLITE_PRI
954b0 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 VATE sqlite3_mut
954c0 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 ex_methods *sqli
954d0 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28 te3DefaultMutex(
954e0 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 void){. static
954f0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 sqlite3_mutex_me
95500 74 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 7b thods sMutex = {
95510 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 49 6e 69 . os2MutexIni
95520 74 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 45 t,. os2MutexE
95530 6e 64 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 nd,. os2Mutex
95540 41 6c 6c 6f 63 2c 0a 20 20 20 20 6f 73 32 4d 75 Alloc,. os2Mu
95550 74 65 78 46 72 65 65 2c 0a 20 20 20 20 6f 73 32 texFree,. os2
95560 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 MutexEnter,.
95570 6f 73 32 4d 75 74 65 78 54 72 79 2c 0a 20 20 20 os2MutexTry,.
95580 20 6f 73 32 4d 75 74 65 78 4c 65 61 76 65 2c 0a os2MutexLeave,.
95590 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
955a0 42 55 47 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 BUG. os2Mutex
955b0 48 65 6c 64 2c 0a 20 20 20 20 6f 73 32 4d 75 74 Held,. os2Mut
955c0 65 78 4e 6f 74 68 65 6c 64 0a 23 65 6e 64 69 66 exNotheld.#endif
955d0 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20 . };.. return
955e0 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 &sMutex;.}.#endi
955f0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 f /* SQLITE_MUTE
95600 58 5f 4f 53 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a X_OS2 */../*****
95610 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
95620 20 6d 75 74 65 78 5f 6f 73 32 2e 63 20 2a 2a 2a mutex_os2.c ***
95630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
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 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
95660 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
95670 66 69 6c 65 20 6d 75 74 65 78 5f 75 6e 69 78 2e file mutex_unix.
95680 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
95690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
956a0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
956b0 32 30 30 37 20 41 75 67 75 73 74 20 32 38 0a 2a 2007 August 28.*
956c0 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
956d0 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
956e0 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
956f0 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
95700 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
95710 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
95720 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
95730 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
95740 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
95750 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
95760 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
95770 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
95780 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
95790 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
957a0 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
957b0 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
957c0 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
957d0 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
957e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
957f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
95820 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
95830 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 ins the C functi
95840 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 ons that impleme
95850 6e 74 20 6d 75 74 65 78 65 73 20 66 6f 72 20 70 nt mutexes for p
95860 74 68 72 65 61 64 73 0a 2a 2a 0a 2a 2a 20 24 49 threads.**.** $I
95870 64 3a 20 6d 75 74 65 78 5f 75 6e 69 78 2e 63 2c d: mutex_unix.c,
95880 76 20 31 2e 31 36 20 32 30 30 38 2f 31 32 2f 30 v 1.16 2008/12/0
95890 38 20 31 38 3a 31 39 3a 31 38 20 64 72 68 20 45 8 18:19:18 drh E
958a0 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 xp $.*/../*.** T
958b0 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 he code in this
958c0 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 75 73 65 file is only use
958d0 64 20 69 66 20 77 65 20 61 72 65 20 63 6f 6d 70 d if we are comp
958e0 69 6c 69 6e 67 20 74 68 72 65 61 64 73 61 66 65 iling threadsafe
958f0 0a 2a 2a 20 75 6e 64 65 72 20 75 6e 69 78 20 77 .** under unix w
95900 69 74 68 20 70 74 68 72 65 61 64 73 2e 0a 2a 2a ith pthreads..**
95910 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 .** Note that th
95920 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f is implementatio
95930 6e 20 72 65 71 75 69 72 65 73 20 61 20 76 65 72 n requires a ver
95940 73 69 6f 6e 20 6f 66 20 70 74 68 72 65 61 64 73 sion of pthreads
95950 20 74 68 61 74 0a 2a 2a 20 73 75 70 70 6f 72 74 that.** support
95960 73 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 s recursive mute
95970 78 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 xes..*/.#ifdef S
95980 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 QLITE_MUTEX_PTHR
95990 45 41 44 53 0a 0a 23 69 6e 63 6c 75 64 65 20 3c EADS..#include <
959a0 70 74 68 72 65 61 64 2e 68 3e 0a 0a 0a 2f 2a 0a pthread.h>.../*.
959b0 2a 2a 20 45 61 63 68 20 72 65 63 75 72 73 69 76 ** Each recursiv
959c0 65 20 6d 75 74 65 78 20 69 73 20 61 6e 20 69 6e e mutex is an in
959d0 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f stance of the fo
959e0 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 llowing structur
959f0 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c e..*/.struct sql
95a00 69 74 65 33 5f 6d 75 74 65 78 20 7b 0a 20 20 70 ite3_mutex {. p
95a10 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 74 20 6d thread_mutex_t m
95a20 75 74 65 78 3b 20 20 20 20 20 2f 2a 20 4d 75 74 utex; /* Mut
95a30 65 78 20 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74 ex controlling t
95a40 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 he lock */. int
95a50 20 69 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 id;
95a60 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 /* Mutex
95a70 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20 6e type */. int n
95a80 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 Ref;
95a90 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
95aa0 6f 66 20 65 6e 74 72 61 6e 63 65 73 20 2a 2f 0a of entrances */.
95ab0 20 20 70 74 68 72 65 61 64 5f 74 20 6f 77 6e 65 pthread_t owne
95ac0 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 r; /*
95ad0 54 68 72 65 61 64 20 74 68 61 74 20 69 73 20 77 Thread that is w
95ae0 69 74 68 69 6e 20 74 68 69 73 20 6d 75 74 65 78 ithin this mutex
95af0 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 */.#ifdef SQLIT
95b00 45 5f 44 45 42 55 47 0a 20 20 69 6e 74 20 74 72 E_DEBUG. int tr
95b10 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ace;
95b20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 /* True to
95b30 74 72 61 63 65 20 63 68 61 6e 67 65 73 20 2a 2f trace changes */
95b40 0a 23 65 6e 64 69 66 0a 7d 3b 0a 23 69 66 64 65 .#endif.};.#ifde
95b50 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 23 f SQLITE_DEBUG.#
95b60 64 65 66 69 6e 65 20 53 51 4c 49 54 45 33 5f 4d define SQLITE3_M
95b70 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 UTEX_INITIALIZER
95b80 20 7b 20 50 54 48 52 45 41 44 5f 4d 55 54 45 58 { PTHREAD_MUTEX
95b90 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 30 2c _INITIALIZER, 0,
95ba0 20 30 2c 20 28 70 74 68 72 65 61 64 5f 74 29 30 0, (pthread_t)0
95bb0 2c 20 30 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66 , 0 }.#else.#def
95bc0 69 6e 65 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 ine SQLITE3_MUTE
95bd0 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b 20 X_INITIALIZER {
95be0 50 54 48 52 45 41 44 5f 4d 55 54 45 58 5f 49 4e PTHREAD_MUTEX_IN
95bf0 49 54 49 41 4c 49 5a 45 52 2c 20 30 2c 20 30 2c ITIALIZER, 0, 0,
95c00 20 28 70 74 68 72 65 61 64 5f 74 29 30 20 7d 0a (pthread_t)0 }.
95c10 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 #endif../*.** Th
95c20 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f e sqlite3_mutex_
95c30 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 held() and sqlit
95c40 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 e3_mutex_notheld
95c50 28 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a () routine are.*
95c60 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 * intended for u
95c70 73 65 20 6f 6e 6c 79 20 69 6e 73 69 64 65 20 61 se only inside a
95c80 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e ssert() statemen
95c90 74 73 2e 20 20 4f 6e 20 73 6f 6d 65 20 70 6c 61 ts. On some pla
95ca0 74 66 6f 72 6d 73 2c 0a 2a 2a 20 74 68 65 72 65 tforms,.** there
95cb0 20 6d 69 67 68 74 20 62 65 20 72 61 63 65 20 63 might be race c
95cc0 6f 6e 64 69 74 69 6f 6e 73 20 74 68 61 74 20 63 onditions that c
95cd0 61 6e 20 63 61 75 73 65 20 74 68 65 73 65 20 72 an cause these r
95ce0 6f 75 74 69 6e 65 73 20 74 6f 0a 2a 2a 20 64 65 outines to.** de
95cf0 6c 69 76 65 72 20 69 6e 63 6f 72 72 65 63 74 20 liver incorrect
95d00 72 65 73 75 6c 74 73 2e 20 20 49 6e 20 70 61 72 results. In par
95d10 74 69 63 75 6c 61 72 2c 20 69 66 20 70 74 68 72 ticular, if pthr
95d20 65 61 64 5f 65 71 75 61 6c 28 29 20 69 73 0a 2a ead_equal() is.*
95d30 2a 20 6e 6f 74 20 61 6e 20 61 74 6f 6d 69 63 20 * not an atomic
95d40 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 6e 20 operation, then
95d50 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d these routines m
95d60 69 67 68 74 20 64 65 6c 69 76 65 72 79 0a 2a 2a ight delivery.**
95d70 20 69 6e 63 6f 72 72 65 63 74 20 72 65 73 75 6c incorrect resul
95d80 74 73 2e 20 20 4f 6e 20 6d 6f 73 74 20 70 6c 61 ts. On most pla
95d90 74 66 6f 72 6d 73 2c 20 70 74 68 72 65 61 64 5f tforms, pthread_
95da0 65 71 75 61 6c 28 29 20 69 73 20 61 20 0a 2a 2a equal() is a .**
95db0 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20 74 comparison of t
95dc0 77 6f 20 69 6e 74 65 67 65 72 73 20 61 6e 64 20 wo integers and
95dd0 69 73 20 74 68 65 72 65 66 6f 72 65 20 61 74 6f is therefore ato
95de0 6d 69 63 2e 20 20 42 75 74 20 77 65 20 61 72 65 mic. But we are
95df0 0a 2a 2a 20 74 6f 6c 64 20 74 68 61 74 20 48 50 .** told that HP
95e00 55 58 20 69 73 20 6e 6f 74 20 73 75 63 68 20 61 UX is not such a
95e10 20 70 6c 61 74 66 6f 72 6d 2e 20 20 49 66 20 73 platform. If s
95e20 6f 2c 20 74 68 65 6e 20 74 68 65 73 65 20 72 6f o, then these ro
95e30 75 74 69 6e 65 73 0a 2a 2a 20 77 69 6c 6c 20 6e utines.** will n
95e40 6f 74 20 61 6c 77 61 79 73 20 77 6f 72 6b 20 63 ot always work c
95e50 6f 72 72 65 63 74 6c 79 20 6f 6e 20 48 50 55 58 orrectly on HPUX
95e60 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 74 68 6f 73 65 ..**.** On those
95e70 20 70 6c 61 74 66 6f 72 6d 73 20 77 68 65 72 65 platforms where
95e80 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 29 pthread_equal()
95e90 20 69 73 20 6e 6f 74 20 61 74 6f 6d 69 63 2c 20 is not atomic,
95ea0 53 51 4c 69 74 65 0a 2a 2a 20 73 68 6f 75 6c 64 SQLite.** should
95eb0 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 be compiled wit
95ec0 68 6f 75 74 20 2d 44 53 51 4c 49 54 45 5f 44 45 hout -DSQLITE_DE
95ed0 42 55 47 20 61 6e 64 20 77 69 74 68 20 2d 44 4e BUG and with -DN
95ee0 44 45 42 55 47 20 74 6f 0a 2a 2a 20 6d 61 6b 65 DEBUG to.** make
95ef0 20 73 75 72 65 20 6e 6f 20 61 73 73 65 72 74 28 sure no assert(
95f00 29 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 ) statements are
95f10 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20 68 evaluated and h
95f20 65 6e 63 65 20 74 68 65 73 65 0a 2a 2a 20 72 6f ence these.** ro
95f30 75 74 69 6e 65 73 20 61 72 65 20 6e 65 76 65 72 utines are never
95f40 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 23 69 66 20 called..*/.#if
95f50 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 !defined(NDEBUG)
95f60 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 || defined(SQLI
95f70 54 45 5f 44 45 42 55 47 29 0a 73 74 61 74 69 63 TE_DEBUG).static
95f80 20 69 6e 74 20 70 74 68 72 65 61 64 4d 75 74 65 int pthreadMute
95f90 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 xHeld(sqlite3_mu
95fa0 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 tex *p){. retur
95fb0 6e 20 28 70 2d 3e 6e 52 65 66 21 3d 30 20 26 26 n (p->nRef!=0 &&
95fc0 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 70 pthread_equal(p
95fd0 2d 3e 6f 77 6e 65 72 2c 20 70 74 68 72 65 61 64 ->owner, pthread
95fe0 5f 73 65 6c 66 28 29 29 29 3b 0a 7d 0a 73 74 61 _self()));.}.sta
95ff0 74 69 63 20 69 6e 74 20 70 74 68 72 65 61 64 4d tic int pthreadM
96000 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 utexNotheld(sqli
96010 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 te3_mutex *p){.
96020 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 3d return p->nRef=
96030 3d 30 20 7c 7c 20 70 74 68 72 65 61 64 5f 65 71 =0 || pthread_eq
96040 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20 70 74 ual(p->owner, pt
96050 68 72 65 61 64 5f 73 65 6c 66 28 29 29 3d 3d 30 hread_self())==0
96060 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ;.}.#endif../*.*
96070 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 * Initialize and
96080 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 deinitialize th
96090 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 e mutex subsyste
960a0 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 m..*/.static int
960b0 20 70 74 68 72 65 61 64 4d 75 74 65 78 49 6e 69 pthreadMutexIni
960c0 74 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 t(void){ return
960d0 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 SQLITE_OK; }.sta
960e0 74 69 63 20 69 6e 74 20 70 74 68 72 65 61 64 4d tic int pthreadM
960f0 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 utexEnd(void){ r
96100 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
96110 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 }../*.** The sq
96120 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f lite3_mutex_allo
96130 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f c() routine allo
96140 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d cates a new.** m
96150 75 74 65 78 20 61 6e 64 20 72 65 74 75 72 6e 73 utex and returns
96160 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 a pointer to it
96170 2e 20 20 49 66 20 69 74 20 72 65 74 75 72 6e 73 . If it returns
96180 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 NULL.** that me
96190 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74 65 78 ans that a mutex
961a0 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c could not be al
961b0 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74 65 located. SQLite
961c0 0a 2a 2a 20 77 69 6c 6c 20 75 6e 77 69 6e 64 20 .** will unwind
961d0 69 74 73 20 73 74 61 63 6b 20 61 6e 64 20 72 65 its stack and re
961e0 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 20 20 turn an error.
961f0 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 The argument.**
96200 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 to sqlite3_mutex
96210 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e 65 20 _alloc() is one
96220 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 65 72 of these integer
96230 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a constants:.**.*
96240 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 * <ul>.** <li>
96250 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
96260 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 T.** <li> SQLIT
96270 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
96280 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 E.** <li> SQLIT
96290 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d E_MUTEX_STATIC_M
962a0 41 53 54 45 52 0a 2a 2a 20 3c 6c 69 3e 20 20 53 ASTER.** <li> S
962b0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
962c0 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e 20 20 IC_MEM.** <li>
962d0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
962e0 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c 69 3e TIC_MEM2.** <li>
962f0 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 SQLITE_MUTEX_S
96300 54 41 54 49 43 5f 50 52 4e 47 0a 2a 2a 20 3c 6c TATIC_PRNG.** <l
96310 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 i> SQLITE_MUTEX
96320 5f 53 54 41 54 49 43 5f 4c 52 55 0a 2a 2a 20 3c _STATIC_LRU.** <
96330 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 li> SQLITE_MUTE
96340 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 0a 2a 2a X_STATIC_LRU2.**
96350 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 </ul>.**.** The
96360 20 66 69 72 73 74 20 74 77 6f 20 63 6f 6e 73 74 first two const
96370 61 6e 74 73 20 63 61 75 73 65 20 73 71 6c 69 74 ants cause sqlit
96380 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 e3_mutex_alloc()
96390 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61 20 to create.** a
963a0 6e 65 77 20 6d 75 74 65 78 2e 20 20 54 68 65 20 new mutex. The
963b0 6e 65 77 20 6d 75 74 65 78 20 69 73 20 72 65 63 new mutex is rec
963c0 75 72 73 69 76 65 20 77 68 65 6e 20 53 51 4c 49 ursive when SQLI
963d0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
963e0 56 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 62 75 VE.** is used bu
963f0 74 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c t not necessaril
96400 79 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 54 45 y so when SQLITE
96410 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 73 20 75 _MUTEX_FAST is u
96420 73 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 74 65 sed..** The mute
96430 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e x implementation
96440 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 does not need t
96450 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e 63 o make a distinc
96460 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20 tion.** between
96470 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
96480 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49 54 URSIVE and SQLIT
96490 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66 20 E_MUTEX_FAST if
964a0 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 it does.** not w
964b0 61 6e 74 20 74 6f 2e 20 20 42 75 74 20 53 51 4c ant to. But SQL
964c0 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 65 ite will only re
964d0 71 75 65 73 74 20 61 20 72 65 63 75 72 73 69 76 quest a recursiv
964e0 65 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 63 61 e mutex in.** ca
964f0 73 65 73 20 77 68 65 72 65 20 69 74 20 72 65 61 ses where it rea
96500 6c 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e 20 20 lly needs one.
96510 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d If a faster non-
96520 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 0a recursive mutex.
96530 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** implementatio
96540 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 6f n is available o
96550 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74 66 n the host platf
96560 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 73 orm, the mutex s
96570 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 ubsystem.** migh
96580 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 61 20 t return such a
96590 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 mutex in respons
965a0 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45 e to SQLITE_MUTE
965b0 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 X_FAST..**.** Th
965c0 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64 20 e other allowed
965d0 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 73 71 parameters to sq
965e0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f lite3_mutex_allo
965f0 63 28 29 20 65 61 63 68 20 72 65 74 75 72 6e 0a c() each return.
96600 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ** a pointer to
96610 61 20 73 74 61 74 69 63 20 70 72 65 65 78 69 73 a static preexis
96620 74 69 6e 67 20 6d 75 74 65 78 2e 20 20 53 69 78 ting mutex. Six
96630 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 static mutexes
96640 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 20 74 are.** used by t
96650 68 65 20 63 75 72 72 65 6e 74 20 76 65 72 73 69 he current versi
96660 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 46 on of SQLite. F
96670 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f uture versions o
96680 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 79 20 f SQLite.** may
96690 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 add additional s
966a0 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e 20 20 tatic mutexes.
966b0 53 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 61 Static mutexes a
966c0 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a re for internal.
966d0 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 ** use by SQLite
966e0 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61 74 only. Applicat
966f0 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 53 51 ions that use SQ
96700 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73 68 6f Lite mutexes sho
96710 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 uld.** use only
96720 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 the dynamic mute
96730 78 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 xes returned by
96740 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
96750 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d T or.** SQLITE_M
96760 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 2e 0a UTEX_RECURSIVE..
96770 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 **.** Note that
96780 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 64 79 if one of the dy
96790 6e 61 6d 69 63 20 6d 75 74 65 78 20 70 61 72 61 namic mutex para
967a0 6d 65 74 65 72 73 20 28 53 51 4c 49 54 45 5f 4d meters (SQLITE_M
967b0 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 6f 72 20 UTEX_FAST.** or
967c0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
967d0 55 52 53 49 56 45 29 20 69 73 20 75 73 65 64 20 URSIVE) is used
967e0 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 then sqlite3_mut
967f0 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20 72 65 ex_alloc().** re
96800 74 75 72 6e 73 20 61 20 64 69 66 66 65 72 65 6e turns a differen
96810 74 20 6d 75 74 65 78 20 6f 6e 20 65 76 65 72 79 t mutex on every
96820 20 63 61 6c 6c 2e 20 20 42 75 74 20 66 6f 72 20 call. But for
96830 74 68 65 20 73 74 61 74 69 63 20 0a 2a 2a 20 6d the static .** m
96840 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 65 20 utex types, the
96850 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 72 65 same mutex is re
96860 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 79 20 turned on every
96870 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a 2a call that has.**
96880 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20 6e the same type n
96890 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 umber..*/.static
968a0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a sqlite3_mutex *
968b0 70 74 68 72 65 61 64 4d 75 74 65 78 41 6c 6c 6f pthreadMutexAllo
968c0 63 28 69 6e 74 20 69 54 79 70 65 29 7b 0a 20 20 c(int iType){.
968d0 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d static sqlite3_m
968e0 75 74 65 78 20 73 74 61 74 69 63 4d 75 74 65 78 utex staticMutex
968f0 65 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 53 51 4c es[] = {. SQL
96900 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 ITE3_MUTEX_INITI
96910 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 ALIZER,. SQLI
96920 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 TE3_MUTEX_INITIA
96930 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 LIZER,. SQLIT
96940 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c E3_MUTEX_INITIAL
96950 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 IZER,. SQLITE
96960 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 3_MUTEX_INITIALI
96970 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 ZER,. SQLITE3
96980 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a _MUTEX_INITIALIZ
96990 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 5f ER,. SQLITE3_
969a0 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 MUTEX_INITIALIZE
969b0 52 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 R. };. sqlite3
969c0 5f 6d 75 74 65 78 20 2a 70 3b 0a 20 20 73 77 69 _mutex *p;. swi
969d0 74 63 68 28 20 69 54 79 70 65 20 29 7b 0a 20 20 tch( iType ){.
969e0 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 case SQLITE_MU
969f0 54 45 58 5f 52 45 43 55 52 53 49 56 45 3a 20 7b TEX_RECURSIVE: {
96a00 0a 20 20 20 20 20 20 70 20 3d 20 73 71 6c 69 74 . p = sqlit
96a10 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 e3MallocZero( si
96a20 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 zeof(*p) );.
96a30 20 20 69 66 28 20 70 20 29 7b 0a 23 69 66 64 65 if( p ){.#ifde
96a40 66 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52 4f f SQLITE_HOMEGRO
96a50 57 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d 55 54 WN_RECURSIVE_MUT
96a60 45 58 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 EX. /* If
96a70 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 recursive mutex
96a80 65 73 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c es are not avail
96a90 61 62 6c 65 2c 20 77 65 20 77 69 6c 6c 20 68 61 able, we will ha
96aa0 76 65 20 74 6f 0a 20 20 20 20 20 20 20 20 2a 2a ve to. **
96ab0 20 62 75 69 6c 64 20 6f 75 72 20 6f 77 6e 2e 20 build our own.
96ac0 20 53 65 65 20 62 65 6c 6f 77 2e 20 2a 2f 0a 20 See below. */.
96ad0 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d pthread_m
96ae0 75 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d 75 utex_init(&p->mu
96af0 74 65 78 2c 20 30 29 3b 0a 23 65 6c 73 65 0a 20 tex, 0);.#else.
96b00 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 61 20 /* Use a
96b10 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 recursive mutex
96b20 69 66 20 69 74 20 69 73 20 61 76 61 69 6c 61 62 if it is availab
96b30 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 74 le */. pt
96b40 68 72 65 61 64 5f 6d 75 74 65 78 61 74 74 72 5f hread_mutexattr_
96b50 74 20 72 65 63 75 72 73 69 76 65 41 74 74 72 3b t recursiveAttr;
96b60 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64 . pthread
96b70 5f 6d 75 74 65 78 61 74 74 72 5f 69 6e 69 74 28 _mutexattr_init(
96b80 26 72 65 63 75 72 73 69 76 65 41 74 74 72 29 3b &recursiveAttr);
96b90 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64 . pthread
96ba0 5f 6d 75 74 65 78 61 74 74 72 5f 73 65 74 74 79 _mutexattr_setty
96bb0 70 65 28 26 72 65 63 75 72 73 69 76 65 41 74 74 pe(&recursiveAtt
96bc0 72 2c 20 50 54 48 52 45 41 44 5f 4d 55 54 45 58 r, PTHREAD_MUTEX
96bd0 5f 52 45 43 55 52 53 49 56 45 29 3b 0a 20 20 20 _RECURSIVE);.
96be0 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 pthread_mut
96bf0 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d 75 74 65 ex_init(&p->mute
96c00 78 2c 20 26 72 65 63 75 72 73 69 76 65 41 74 74 x, &recursiveAtt
96c10 72 29 3b 0a 20 20 20 20 20 20 20 20 70 74 68 72 r);. pthr
96c20 65 61 64 5f 6d 75 74 65 78 61 74 74 72 5f 64 65 ead_mutexattr_de
96c30 73 74 72 6f 79 28 26 72 65 63 75 72 73 69 76 65 stroy(&recursive
96c40 41 74 74 72 29 3b 0a 23 65 6e 64 69 66 0a 20 20 Attr);.#endif.
96c50 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 54 p->id = iT
96c60 79 70 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ype;. }.
96c70 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
96c80 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
96c90 4d 55 54 45 58 5f 46 41 53 54 3a 20 7b 0a 20 20 MUTEX_FAST: {.
96ca0 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d p = sqlite3M
96cb0 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f allocZero( sizeo
96cc0 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69 f(*p) );. i
96cd0 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 20 20 f( p ){.
96ce0 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 p->id = iType;.
96cf0 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d pthread_m
96d00 75 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d 75 utex_init(&p->mu
96d10 74 65 78 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d tex, 0);. }
96d20 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
96d30 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a }. default:
96d40 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 {. assert(
96d50 20 69 54 79 70 65 2d 32 20 3e 3d 20 30 20 29 3b iType-2 >= 0 );
96d60 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 . assert( i
96d70 54 79 70 65 2d 32 20 3c 20 41 72 72 61 79 53 69 Type-2 < ArraySi
96d80 7a 65 28 73 74 61 74 69 63 4d 75 74 65 78 65 73 ze(staticMutexes
96d90 29 20 29 3b 0a 20 20 20 20 20 20 70 20 3d 20 26 ) );. p = &
96da0 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 69 54 staticMutexes[iT
96db0 79 70 65 2d 32 5d 3b 0a 20 20 20 20 20 20 70 2d ype-2];. p-
96dc0 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 >id = iType;.
96dd0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
96de0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a }. return p;.
96df0 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 }.../*.** This r
96e00 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 outine deallocat
96e10 65 73 20 61 20 70 72 65 76 69 6f 75 73 6c 79 0a es a previously.
96e20 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 6d 75 74 ** allocated mut
96e30 65 78 2e 20 20 53 51 4c 69 74 65 20 69 73 20 63 ex. SQLite is c
96e40 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c 6c 6f areful to deallo
96e50 63 61 74 65 20 65 76 65 72 79 0a 2a 2a 20 6d 75 cate every.** mu
96e60 74 65 78 20 74 68 61 74 20 69 74 20 61 6c 6c 6f tex that it allo
96e70 63 61 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 cates..*/.static
96e80 20 76 6f 69 64 20 70 74 68 72 65 61 64 4d 75 74 void pthreadMut
96e90 65 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d exFree(sqlite3_m
96ea0 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 utex *p){. asse
96eb0 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 rt( p->nRef==0 )
96ec0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 ;. assert( p->i
96ed0 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f d==SQLITE_MUTEX_
96ee0 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 FAST || p->id==S
96ef0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
96f00 52 53 49 56 45 20 29 3b 0a 20 20 70 74 68 72 65 RSIVE );. pthre
96f10 61 64 5f 6d 75 74 65 78 5f 64 65 73 74 72 6f 79 ad_mutex_destroy
96f20 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 (&p->mutex);. s
96f30 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a qlite3_free(p);.
96f40 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c }../*.** The sql
96f50 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
96f60 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d () and sqlite3_m
96f70 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69 utex_try() routi
96f80 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 nes attempt.** t
96f90 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 65 78 2e o enter a mutex.
96fa0 20 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 If another thr
96fb0 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20 77 ead is already w
96fc0 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78 2c ithin the mutex,
96fd0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 .** sqlite3_mute
96fe0 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 62 x_enter() will b
96ff0 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 33 lock and sqlite3
97000 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69 6c _mutex_try() wil
97010 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 l return.** SQLI
97020 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20 73 71 TE_BUSY. The sq
97030 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 lite3_mutex_try(
97040 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 ) interface retu
97050 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a rns SQLITE_OK.**
97060 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66 75 6c upon successful
97070 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78 65 73 entry. Mutexes
97080 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 53 created using S
97090 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
970a0 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65 20 RSIVE can.** be
970b0 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65 entered multiple
970c0 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 73 61 times by the sa
970d0 6d 65 20 74 68 72 65 61 64 2e 20 20 49 6e 20 73 me thread. In s
970e0 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a uch cases the,.*
970f0 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 * mutex must be
97100 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20 exited an equal
97110 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 number of times
97120 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74 before another t
97130 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 hread.** can ent
97140 65 72 2e 20 20 49 66 20 74 68 65 20 73 61 6d 65 er. If the same
97150 20 74 68 72 65 61 64 20 74 72 69 65 73 20 74 6f thread tries to
97160 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 72 enter any other
97170 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a 2a kind of mutex.*
97180 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 * more than once
97190 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 , the behavior i
971a0 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a s undefined..*/.
971b0 73 74 61 74 69 63 20 76 6f 69 64 20 70 74 68 72 static void pthr
971c0 65 61 64 4d 75 74 65 78 45 6e 74 65 72 28 73 71 eadMutexEnter(sq
971d0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b lite3_mutex *p){
971e0 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 . assert( p->id
971f0 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 ==SQLITE_MUTEX_R
97200 45 43 55 52 53 49 56 45 20 7c 7c 20 70 74 68 72 ECURSIVE || pthr
97210 65 61 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 eadMutexNotheld(
97220 70 29 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 p) );..#ifdef SQ
97230 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52 LITE_HOMEGROWN_R
97240 45 43 55 52 53 49 56 45 5f 4d 55 54 45 58 0a 20 ECURSIVE_MUTEX.
97250 20 2f 2a 20 49 66 20 72 65 63 75 72 73 69 76 65 /* If recursive
97260 20 6d 75 74 65 78 65 73 20 61 72 65 20 6e 6f 74 mutexes are not
97270 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 65 6e available, then
97280 20 77 65 20 68 61 76 65 20 74 6f 20 67 72 6f 77 we have to grow
97290 0a 20 20 2a 2a 20 6f 75 72 20 6f 77 6e 2e 20 20 . ** our own.
972a0 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 This implementat
972b0 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74 ion assumes that
972c0 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 29 pthread_equal()
972d0 0a 20 20 2a 2a 20 69 73 20 61 74 6f 6d 69 63 20 . ** is atomic
972e0 2d 20 74 68 61 74 20 69 74 20 63 61 6e 6e 6f 74 - that it cannot
972f0 20 62 65 20 64 65 63 65 69 76 65 64 20 69 6e 74 be deceived int
97300 6f 20 74 68 69 6e 6b 69 6e 67 20 73 65 6c 66 0a o thinking self.
97310 20 20 2a 2a 20 61 6e 64 20 70 2d 3e 6f 77 6e 65 ** and p->owne
97320 72 20 61 72 65 20 65 71 75 61 6c 20 69 66 20 70 r are equal if p
97330 2d 3e 6f 77 6e 65 72 20 63 68 61 6e 67 65 73 20 ->owner changes
97340 62 65 74 77 65 65 6e 20 74 77 6f 20 76 61 6c 75 between two valu
97350 65 73 0a 20 20 2a 2a 20 74 68 61 74 20 61 72 65 es. ** that are
97360 20 6e 6f 74 20 65 71 75 61 6c 20 74 6f 20 73 65 not equal to se
97370 6c 66 20 77 68 69 6c 65 20 74 68 65 20 63 6f 6d lf while the com
97380 70 61 72 69 73 6f 6e 20 69 73 20 74 61 6b 69 6e parison is takin
97390 67 20 70 6c 61 63 65 2e 0a 20 20 2a 2a 20 54 68 g place.. ** Th
973a0 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f is implementatio
973b0 6e 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20 61 n also assumes a
973c0 20 63 6f 68 65 72 65 6e 74 20 63 61 63 68 65 20 coherent cache
973d0 2d 20 74 68 61 74 20 0a 20 20 2a 2a 20 73 65 70 - that . ** sep
973e0 61 72 61 74 65 20 70 72 6f 63 65 73 73 65 73 20 arate processes
973f0 63 61 6e 6e 6f 74 20 72 65 61 64 20 64 69 66 66 cannot read diff
97400 65 72 65 6e 74 20 76 61 6c 75 65 73 20 66 72 6f erent values fro
97410 6d 20 74 68 65 20 73 61 6d 65 0a 20 20 2a 2a 20 m the same. **
97420 61 64 64 72 65 73 73 20 61 74 20 74 68 65 20 73 address at the s
97430 61 6d 65 20 74 69 6d 65 2e 20 20 49 66 20 65 69 ame time. If ei
97440 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 74 77 ther of these tw
97450 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 0a 20 20 2a o conditions. *
97460 2a 20 61 72 65 20 6e 6f 74 20 6d 65 74 2c 20 74 * are not met, t
97470 68 65 6e 20 74 68 65 20 6d 75 74 65 78 65 73 20 hen the mutexes
97480 77 69 6c 6c 20 66 61 69 6c 20 61 6e 64 20 70 72 will fail and pr
97490 6f 62 6c 65 6d 73 20 77 69 6c 6c 20 72 65 73 75 oblems will resu
974a0 6c 74 2e 0a 20 20 2a 2f 0a 20 20 7b 0a 20 20 20 lt.. */. {.
974b0 20 70 74 68 72 65 61 64 5f 74 20 73 65 6c 66 20 pthread_t self
974c0 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 = pthread_self()
974d0 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 52 65 ;. if( p->nRe
974e0 66 3e 30 20 26 26 20 70 74 68 72 65 61 64 5f 65 f>0 && pthread_e
974f0 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20 73 qual(p->owner, s
97500 65 6c 66 29 20 29 7b 0a 20 20 20 20 20 20 70 2d elf) ){. p-
97510 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d 65 6c >nRef++;. }el
97520 73 65 7b 0a 20 20 20 20 20 20 70 74 68 72 65 61 se{. pthrea
97530 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 70 2d d_mutex_lock(&p-
97540 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 61 >mutex);. a
97550 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d ssert( p->nRef==
97560 30 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 6f 77 0 );. p->ow
97570 6e 65 72 20 3d 20 73 65 6c 66 3b 0a 20 20 20 20 ner = self;.
97580 20 20 70 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 p->nRef = 1;.
97590 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 }. }.#else.
975a0 20 2f 2a 20 55 73 65 20 74 68 65 20 62 75 69 6c /* Use the buil
975b0 74 2d 69 6e 20 72 65 63 75 72 73 69 76 65 20 6d t-in recursive m
975c0 75 74 65 78 65 73 20 69 66 20 74 68 65 79 20 61 utexes if they a
975d0 72 65 20 61 76 61 69 6c 61 62 6c 65 2e 0a 20 20 re available..
975e0 2a 2f 0a 20 20 70 74 68 72 65 61 64 5f 6d 75 74 */. pthread_mut
975f0 65 78 5f 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 ex_lock(&p->mute
97600 78 29 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d x);. p->owner =
97610 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 3b pthread_self();
97620 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 23 65 . p->nRef++;.#e
97630 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c ndif..#ifdef SQL
97640 49 54 45 5f 44 45 42 55 47 0a 20 20 69 66 28 20 ITE_DEBUG. if(
97650 70 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 20 p->trace ){.
97660 70 72 69 6e 74 66 28 22 65 6e 74 65 72 20 6d 75 printf("enter mu
97670 74 65 78 20 25 70 20 28 25 64 29 20 77 69 74 68 tex %p (%d) with
97680 20 6e 52 65 66 3d 25 64 5c 6e 22 2c 20 70 2c 20 nRef=%d\n", p,
97690 70 2d 3e 74 72 61 63 65 2c 20 70 2d 3e 6e 52 65 p->trace, p->nRe
976a0 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d f);. }.#endif.}
976b0 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 68 72 .static int pthr
976c0 65 61 64 4d 75 74 65 78 54 72 79 28 73 71 6c 69 eadMutexTry(sqli
976d0 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 te3_mutex *p){.
976e0 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 int rc;. asser
976f0 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 t( p->id==SQLITE
97700 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
97710 20 7c 7c 20 70 74 68 72 65 61 64 4d 75 74 65 78 || pthreadMutex
97720 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 0a 23 Notheld(p) );..#
97730 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d ifdef SQLITE_HOM
97740 45 47 52 4f 57 4e 5f 52 45 43 55 52 53 49 56 45 EGROWN_RECURSIVE
97750 5f 4d 55 54 45 58 0a 20 20 2f 2a 20 49 66 20 72 _MUTEX. /* If r
97760 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 73 ecursive mutexes
97770 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 are not availab
97780 6c 65 2c 20 74 68 65 6e 20 77 65 20 68 61 76 65 le, then we have
97790 20 74 6f 20 67 72 6f 77 0a 20 20 2a 2a 20 6f 75 to grow. ** ou
977a0 72 20 6f 77 6e 2e 20 20 54 68 69 73 20 69 6d 70 r own. This imp
977b0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 73 73 75 lementation assu
977c0 6d 65 73 20 74 68 61 74 20 70 74 68 72 65 61 64 mes that pthread
977d0 5f 65 71 75 61 6c 28 29 0a 20 20 2a 2a 20 69 73 _equal(). ** is
977e0 20 61 74 6f 6d 69 63 20 2d 20 74 68 61 74 20 69 atomic - that i
977f0 74 20 63 61 6e 6e 6f 74 20 62 65 20 64 65 63 65 t cannot be dece
97800 69 76 65 64 20 69 6e 74 6f 20 74 68 69 6e 6b 69 ived into thinki
97810 6e 67 20 73 65 6c 66 0a 20 20 2a 2a 20 61 6e 64 ng self. ** and
97820 20 70 2d 3e 6f 77 6e 65 72 20 61 72 65 20 65 71 p->owner are eq
97830 75 61 6c 20 69 66 20 70 2d 3e 6f 77 6e 65 72 20 ual if p->owner
97840 63 68 61 6e 67 65 73 20 62 65 74 77 65 65 6e 20 changes between
97850 74 77 6f 20 76 61 6c 75 65 73 0a 20 20 2a 2a 20 two values. **
97860 74 68 61 74 20 61 72 65 20 6e 6f 74 20 65 71 75 that are not equ
97870 61 6c 20 74 6f 20 73 65 6c 66 20 77 68 69 6c 65 al to self while
97880 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 the comparison
97890 69 73 20 74 61 6b 69 6e 67 20 70 6c 61 63 65 2e is taking place.
978a0 0a 20 20 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 . ** This imple
978b0 6d 65 6e 74 61 74 69 6f 6e 20 61 6c 73 6f 20 61 mentation also a
978c0 73 73 75 6d 65 73 20 61 20 63 6f 68 65 72 65 6e ssumes a coheren
978d0 74 20 63 61 63 68 65 20 2d 20 74 68 61 74 20 0a t cache - that .
978e0 20 20 2a 2a 20 73 65 70 61 72 61 74 65 20 70 72 ** separate pr
978f0 6f 63 65 73 73 65 73 20 63 61 6e 6e 6f 74 20 72 ocesses cannot r
97900 65 61 64 20 64 69 66 66 65 72 65 6e 74 20 76 61 ead different va
97910 6c 75 65 73 20 66 72 6f 6d 20 74 68 65 20 73 61 lues from the sa
97920 6d 65 0a 20 20 2a 2a 20 61 64 64 72 65 73 73 20 me. ** address
97930 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 at the same time
97940 2e 20 20 49 66 20 65 69 74 68 65 72 20 6f 66 20 . If either of
97950 74 68 65 73 65 20 74 77 6f 20 63 6f 6e 64 69 74 these two condit
97960 69 6f 6e 73 0a 20 20 2a 2a 20 61 72 65 20 6e 6f ions. ** are no
97970 74 20 6d 65 74 2c 20 74 68 65 6e 20 74 68 65 20 t met, then the
97980 6d 75 74 65 78 65 73 20 77 69 6c 6c 20 66 61 69 mutexes will fai
97990 6c 20 61 6e 64 20 70 72 6f 62 6c 65 6d 73 20 77 l and problems w
979a0 69 6c 6c 20 72 65 73 75 6c 74 2e 0a 20 20 2a 2f ill result.. */
979b0 0a 20 20 7b 0a 20 20 20 20 70 74 68 72 65 61 64 . {. pthread
979c0 5f 74 20 73 65 6c 66 20 3d 20 70 74 68 72 65 61 _t self = pthrea
979d0 64 5f 73 65 6c 66 28 29 3b 0a 20 20 20 20 69 66 d_self();. if
979e0 28 20 70 2d 3e 6e 52 65 66 3e 30 20 26 26 20 70 ( p->nRef>0 && p
979f0 74 68 72 65 61 64 5f 65 71 75 61 6c 28 70 2d 3e thread_equal(p->
97a00 6f 77 6e 65 72 2c 20 73 65 6c 66 29 20 29 7b 0a owner, self) ){.
97a10 20 20 20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b p->nRef++;
97a20 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
97a30 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 TE_OK;. }else
97a40 20 69 66 28 20 70 74 68 72 65 61 64 5f 6d 75 74 if( pthread_mut
97a50 65 78 5f 74 72 79 6c 6f 63 6b 28 26 70 2d 3e 6d ex_trylock(&p->m
97a60 75 74 65 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 utex)==0 ){.
97a70 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 assert( p->nRe
97a80 66 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 2d f==0 );. p-
97a90 3e 6f 77 6e 65 72 20 3d 20 73 65 6c 66 3b 0a 20 >owner = self;.
97aa0 20 20 20 20 20 70 2d 3e 6e 52 65 66 20 3d 20 31 p->nRef = 1
97ab0 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c ;. rc = SQL
97ac0 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c 73 ITE_OK;. }els
97ad0 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 e{. rc = SQ
97ae0 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d LITE_BUSY;. }
97af0 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 . }.#else. /*
97b00 55 73 65 20 74 68 65 20 62 75 69 6c 74 2d 69 6e Use the built-in
97b10 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 recursive mutex
97b20 65 73 20 69 66 20 74 68 65 79 20 61 72 65 20 61 es if they are a
97b30 76 61 69 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 vailable.. */.
97b40 20 69 66 28 20 70 74 68 72 65 61 64 5f 6d 75 74 if( pthread_mut
97b50 65 78 5f 74 72 79 6c 6f 63 6b 28 26 70 2d 3e 6d ex_trylock(&p->m
97b60 75 74 65 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 utex)==0 ){.
97b70 70 2d 3e 6f 77 6e 65 72 20 3d 20 70 74 68 72 65 p->owner = pthre
97b80 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 20 20 70 ad_self();. p
97b90 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 72 63 ->nRef++;. rc
97ba0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
97bb0 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 }else{. rc =
97bc0 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d SQLITE_BUSY;. }
97bd0 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 .#endif..#ifdef
97be0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 69 SQLITE_DEBUG. i
97bf0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
97c00 20 26 26 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a && p->trace ){.
97c10 20 20 20 20 70 72 69 6e 74 66 28 22 65 6e 74 65 printf("ente
97c20 72 20 6d 75 74 65 78 20 25 70 20 28 25 64 29 20 r mutex %p (%d)
97c30 77 69 74 68 20 6e 52 65 66 3d 25 64 5c 6e 22 2c with nRef=%d\n",
97c40 20 70 2c 20 70 2d 3e 74 72 61 63 65 2c 20 70 2d p, p->trace, p-
97c50 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 >nRef);. }.#end
97c60 69 66 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a if. return rc;.
97c70 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c }../*.** The sql
97c80 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
97c90 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 () routine exits
97ca0 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 a mutex that wa
97cb0 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 s.** previously
97cc0 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 20 73 entered by the s
97cd0 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 ame thread. The
97ce0 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 behavior.** is
97cf0 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 undefined if the
97d00 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75 mutex is not cu
97d10 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 rrently entered
97d20 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 or.** is not cur
97d30 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 rently allocated
97d40 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e . SQLite will n
97d50 65 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a ever do either..
97d60 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 */.static void p
97d70 74 68 72 65 61 64 4d 75 74 65 78 4c 65 61 76 65 threadMutexLeave
97d80 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
97d90 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 74 p){. assert( pt
97da0 68 72 65 61 64 4d 75 74 65 78 48 65 6c 64 28 70 hreadMutexHeld(p
97db0 29 20 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d ) );. p->nRef--
97dc0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e ;. assert( p->n
97dd0 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e 69 64 3d Ref==0 || p->id=
97de0 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 =SQLITE_MUTEX_RE
97df0 43 55 52 53 49 56 45 20 29 3b 0a 0a 23 69 66 64 CURSIVE );..#ifd
97e00 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52 ef SQLITE_HOMEGR
97e10 4f 57 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d 55 OWN_RECURSIVE_MU
97e20 54 45 58 0a 20 20 69 66 28 20 70 2d 3e 6e 52 65 TEX. if( p->nRe
97e30 66 3d 3d 30 20 29 7b 0a 20 20 20 20 70 74 68 72 f==0 ){. pthr
97e40 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b ead_mutex_unlock
97e50 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d (&p->mutex);. }
97e60 0a 23 65 6c 73 65 0a 20 20 70 74 68 72 65 61 64 .#else. pthread
97e70 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 70 _mutex_unlock(&p
97e80 2d 3e 6d 75 74 65 78 29 3b 0a 23 65 6e 64 69 66 ->mutex);.#endif
97e90 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
97ea0 44 45 42 55 47 0a 20 20 69 66 28 20 70 2d 3e 74 DEBUG. if( p->t
97eb0 72 61 63 65 20 29 7b 0a 20 20 20 20 70 72 69 6e race ){. prin
97ec0 74 66 28 22 6c 65 61 76 65 20 6d 75 74 65 78 20 tf("leave mutex
97ed0 25 70 20 28 25 64 29 20 77 69 74 68 20 6e 52 65 %p (%d) with nRe
97ee0 66 3d 25 64 5c 6e 22 2c 20 70 2c 20 70 2d 3e 74 f=%d\n", p, p->t
97ef0 72 61 63 65 2c 20 70 2d 3e 6e 52 65 66 29 3b 0a race, p->nRef);.
97f00 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 53 51 }.#endif.}..SQ
97f10 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c LITE_PRIVATE sql
97f20 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f ite3_mutex_metho
97f30 64 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61 75 ds *sqlite3Defau
97f40 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 ltMutex(void){.
97f50 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f static sqlite3_
97f60 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 4d mutex_methods sM
97f70 75 74 65 78 20 3d 20 7b 0a 20 20 20 20 70 74 68 utex = {. pth
97f80 72 65 61 64 4d 75 74 65 78 49 6e 69 74 2c 0a 20 readMutexInit,.
97f90 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 45 pthreadMutexE
97fa0 6e 64 2c 0a 20 20 20 20 70 74 68 72 65 61 64 4d nd,. pthreadM
97fb0 75 74 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 70 utexAlloc,. p
97fc0 74 68 72 65 61 64 4d 75 74 65 78 46 72 65 65 2c threadMutexFree,
97fd0 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 . pthreadMute
97fe0 78 45 6e 74 65 72 2c 0a 20 20 20 20 70 74 68 72 xEnter,. pthr
97ff0 65 61 64 4d 75 74 65 78 54 72 79 2c 0a 20 20 20 eadMutexTry,.
98000 20 70 74 68 72 65 61 64 4d 75 74 65 78 4c 65 61 pthreadMutexLea
98010 76 65 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 54 ve,.#ifdef SQLIT
98020 45 5f 44 45 42 55 47 0a 20 20 20 20 70 74 68 72 E_DEBUG. pthr
98030 65 61 64 4d 75 74 65 78 48 65 6c 64 2c 0a 20 20 eadMutexHeld,.
98040 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 4e 6f pthreadMutexNo
98050 74 68 65 6c 64 0a 23 65 6c 73 65 0a 20 20 20 20 theld.#else.
98060 30 2c 0a 20 20 20 20 30 0a 23 65 6e 64 69 66 0a 0,. 0.#endif.
98070 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 };.. return &
98080 73 4d 75 74 65 78 3b 0a 7d 0a 0a 23 65 6e 64 69 sMutex;.}..#endi
98090 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 f /* SQLITE_MUTE
980a0 58 5f 50 54 48 52 45 41 44 20 2a 2f 0a 0a 2f 2a X_PTHREAD */../*
980b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
980c0 64 20 6f 66 20 6d 75 74 65 78 5f 75 6e 69 78 2e d of mutex_unix.
980d0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
980e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
980f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
98100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
98110 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f 77 gin file mutex_w
98120 33 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 32.c ***********
98130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
98140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
98150 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 .** 2007 August
98160 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 14.**.** The aut
98170 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
98180 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
98190 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
981a0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
981b0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
981c0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
981d0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
981e0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
981f0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
98200 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
98210 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
98220 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
98230 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
98240 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
98250 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
98260 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
98270 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
98280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
98290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
982a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
982b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
982c0 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
982d0 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 ontains the C fu
982e0 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 nctions that imp
982f0 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 20 66 lement mutexes f
98300 6f 72 20 77 69 6e 33 32 0a 2a 2a 0a 2a 2a 20 24 or win32.**.** $
98310 49 64 3a 20 6d 75 74 65 78 5f 77 33 32 2e 63 2c Id: mutex_w32.c,
98320 76 20 31 2e 31 38 20 32 30 30 39 2f 30 38 2f 31 v 1.18 2009/08/1
98330 30 20 30 33 3a 32 33 3a 32 31 20 73 68 61 6e 65 0 03:23:21 shane
98340 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a Exp $.*/../*.**
98350 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 The code in thi
98360 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 75 s file is only u
98370 73 65 64 20 69 66 20 77 65 20 61 72 65 20 63 6f sed if we are co
98380 6d 70 69 6c 69 6e 67 20 6d 75 6c 74 69 74 68 72 mpiling multithr
98390 65 61 64 65 64 0a 2a 2a 20 6f 6e 20 61 20 77 69 eaded.** on a wi
983a0 6e 33 32 20 73 79 73 74 65 6d 2e 0a 2a 2f 0a 23 n32 system..*/.#
983b0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 ifdef SQLITE_MUT
983c0 45 58 5f 57 33 32 0a 0a 2f 2a 0a 2a 2a 20 45 61 EX_W32../*.** Ea
983d0 63 68 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 ch recursive mut
983e0 65 78 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 ex is an instanc
983f0 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
98400 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f ng structure..*/
98410 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f .struct sqlite3_
98420 6d 75 74 65 78 20 7b 0a 20 20 43 52 49 54 49 43 mutex {. CRITIC
98430 41 4c 5f 53 45 43 54 49 4f 4e 20 6d 75 74 65 78 AL_SECTION mutex
98440 3b 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 63 6f ; /* Mutex co
98450 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 65 20 6c 6f ntrolling the lo
98460 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 64 3b 20 ck */. int id;
98470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
98480 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 79 70 65 /* Mutex type
98490 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 */. int nRef;
984a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
984b0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e /* Number of en
984c0 74 65 72 61 6e 63 65 73 20 2a 2f 0a 20 20 44 57 terances */. DW
984d0 4f 52 44 20 6f 77 6e 65 72 3b 20 20 20 20 20 20 ORD owner;
984e0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 72 65 /* Thre
984f0 61 64 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 ad holding this
98500 6d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a mutex */.};../*.
98510 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 28 ** Return true (
98520 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 77 65 20 non-zero) if we
98530 61 72 65 20 72 75 6e 6e 69 6e 67 20 75 6e 64 65 are running unde
98540 72 20 57 69 6e 4e 54 2c 20 57 69 6e 32 4b 2c 20 r WinNT, Win2K,
98550 57 69 6e 58 50 2c 0a 2a 2a 20 6f 72 20 57 69 6e WinXP,.** or Win
98560 43 45 2e 20 20 52 65 74 75 72 6e 20 66 61 6c 73 CE. Return fals
98570 65 20 28 7a 65 72 6f 29 20 66 6f 72 20 57 69 6e e (zero) for Win
98580 39 35 2c 20 57 69 6e 39 38 2c 20 6f 72 20 57 69 95, Win98, or Wi
98590 6e 4d 45 2e 0a 2a 2a 0a 2a 2a 20 48 65 72 65 20 nME..**.** Here
985a0 69 73 20 61 6e 20 69 6e 74 65 72 65 73 74 69 6e is an interestin
985b0 67 20 6f 62 73 65 72 76 61 74 69 6f 6e 3a 20 20 g observation:
985c0 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 61 6e Win95, Win98, an
985d0 64 20 57 69 6e 4d 45 20 6c 61 63 6b 0a 2a 2a 20 d WinME lack.**
985e0 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 the LockFileEx()
985f0 20 41 50 49 2e 20 20 42 75 74 20 77 65 20 63 61 API. But we ca
98600 6e 20 73 74 69 6c 6c 20 73 74 61 74 69 63 61 6c n still statical
98610 6c 79 20 6c 69 6e 6b 20 61 67 61 69 6e 73 74 20 ly link against
98620 74 68 61 74 0a 2a 2a 20 41 50 49 20 61 73 20 6c that.** API as l
98630 6f 6e 67 20 61 73 20 77 65 20 64 6f 6e 27 74 20 ong as we don't
98640 63 61 6c 6c 20 69 74 20 77 69 6e 20 72 75 6e 6e call it win runn
98650 69 6e 67 20 57 69 6e 39 35 2f 39 38 2f 4d 45 2e ing Win95/98/ME.
98660 20 20 41 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 74 A call to.** t
98670 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 his routine is u
98680 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 sed to determine
98690 20 69 66 20 74 68 65 20 68 6f 73 74 20 69 73 20 if the host is
986a0 57 69 6e 39 35 2f 39 38 2f 4d 45 20 6f 72 0a 2a Win95/98/ME or.*
986b0 2a 20 57 69 6e 4e 54 2f 32 4b 2f 58 50 20 73 6f * WinNT/2K/XP so
986c0 20 74 68 61 74 20 77 65 20 77 69 6c 6c 20 6b 6e that we will kn
986d0 6f 77 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f ow whether or no
986e0 74 20 77 65 20 63 61 6e 20 73 61 66 65 6c 79 20 t we can safely
986f0 63 61 6c 6c 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b call.** the Lock
98700 46 69 6c 65 45 78 28 29 20 41 50 49 2e 0a 2a 2a FileEx() API..**
98710 0a 2a 2a 20 6d 75 74 65 78 49 73 4e 54 28 29 20 .** mutexIsNT()
98720 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 66 6f 72 is only used for
98730 20 74 68 65 20 54 72 79 45 6e 74 65 72 43 72 69 the TryEnterCri
98740 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 29 20 41 ticalSection() A
98750 50 49 20 63 61 6c 6c 2c 0a 2a 2a 20 77 68 69 63 PI call,.** whic
98760 68 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 h is only availa
98770 62 6c 65 20 69 66 20 79 6f 75 72 20 61 70 70 6c ble if your appl
98780 69 63 61 74 69 6f 6e 20 77 61 73 20 63 6f 6d 70 ication was comp
98790 69 6c 65 64 20 77 69 74 68 20 0a 2a 2a 20 5f 57 iled with .** _W
987a0 49 4e 33 32 5f 57 49 4e 4e 54 20 64 65 66 69 6e IN32_WINNT defin
987b0 65 64 20 74 6f 20 61 20 76 61 6c 75 65 20 3e 3d ed to a value >=
987c0 20 30 78 30 34 30 30 2e 20 20 43 75 72 72 65 6e 0x0400. Curren
987d0 74 6c 79 2c 20 74 68 65 20 6f 6e 6c 79 0a 2a 2a tly, the only.**
987e0 20 63 61 6c 6c 20 74 6f 20 54 72 79 45 6e 74 65 call to TryEnte
987f0 72 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e rCriticalSection
98800 28 29 20 69 73 20 23 69 66 64 65 66 27 65 64 20 () is #ifdef'ed
98810 6f 75 74 2c 20 73 6f 20 23 69 66 64 65 66 20 0a out, so #ifdef .
98820 2a 2a 20 74 68 69 73 20 6f 75 74 20 61 73 20 77 ** this out as w
98830 65 6c 6c 2e 0a 2a 2f 0a 23 69 66 20 30 0a 23 69 ell..*/.#if 0.#i
98840 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 f SQLITE_OS_WINC
98850 45 0a 23 20 64 65 66 69 6e 65 20 6d 75 74 65 78 E.# define mutex
98860 49 73 4e 54 28 29 20 20 28 31 29 0a 23 65 6c 73 IsNT() (1).#els
98870 65 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20 6d e. static int m
98880 75 74 65 78 49 73 4e 54 28 76 6f 69 64 29 7b 0a utexIsNT(void){.
98890 20 20 20 20 73 74 61 74 69 63 20 69 6e 74 20 6f static int o
988a0 73 54 79 70 65 20 3d 20 30 3b 0a 20 20 20 20 69 sType = 0;. i
988b0 66 28 20 6f 73 54 79 70 65 3d 3d 30 20 29 7b 0a f( osType==0 ){.
988c0 20 20 20 20 20 20 4f 53 56 45 52 53 49 4f 4e 49 OSVERSIONI
988d0 4e 46 4f 20 73 49 6e 66 6f 3b 0a 20 20 20 20 20 NFO sInfo;.
988e0 20 73 49 6e 66 6f 2e 64 77 4f 53 56 65 72 73 69 sInfo.dwOSVersi
988f0 6f 6e 49 6e 66 6f 53 69 7a 65 20 3d 20 73 69 7a onInfoSize = siz
98900 65 6f 66 28 73 49 6e 66 6f 29 3b 0a 20 20 20 20 eof(sInfo);.
98910 20 20 47 65 74 56 65 72 73 69 6f 6e 45 78 28 26 GetVersionEx(&
98920 73 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 6f 73 sInfo);. os
98930 54 79 70 65 20 3d 20 73 49 6e 66 6f 2e 64 77 50 Type = sInfo.dwP
98940 6c 61 74 66 6f 72 6d 49 64 3d 3d 56 45 52 5f 50 latformId==VER_P
98950 4c 41 54 46 4f 52 4d 5f 57 49 4e 33 32 5f 4e 54 LATFORM_WIN32_NT
98960 20 3f 20 32 20 3a 20 31 3b 0a 20 20 20 20 7d 0a ? 2 : 1;. }.
98970 20 20 20 20 72 65 74 75 72 6e 20 6f 73 54 79 70 return osTyp
98980 65 3d 3d 32 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 e==2;. }.#endif
98990 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 /* SQLITE_OS_WI
989a0 4e 43 45 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 23 NCE */.#endif..#
989b0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
989c0 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c UG./*.** The sql
989d0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
989e0 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 ) and sqlite3_mu
989f0 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f tex_notheld() ro
98a00 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74 utine are.** int
98a10 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 6f 6e ended for use on
98a20 6c 79 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 ly inside assert
98a30 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a () statements..*
98a40 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e /.static int win
98a50 4d 75 74 65 78 48 65 6c 64 28 73 71 6c 69 74 65 MutexHeld(sqlite
98a60 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 3_mutex *p){. r
98a70 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 21 3d 30 eturn p->nRef!=0
98a80 20 26 26 20 70 2d 3e 6f 77 6e 65 72 3d 3d 47 65 && p->owner==Ge
98a90 74 43 75 72 72 65 6e 74 54 68 72 65 61 64 49 64 tCurrentThreadId
98aa0 28 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 ();.}.static int
98ab0 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65 6c 64 winMutexNotheld
98ac0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
98ad0 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e p){. return p->
98ae0 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e 6f 77 nRef==0 || p->ow
98af0 6e 65 72 21 3d 47 65 74 43 75 72 72 65 6e 74 54 ner!=GetCurrentT
98b00 68 72 65 61 64 49 64 28 29 3b 0a 7d 0a 23 65 6e hreadId();.}.#en
98b10 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 dif.../*.** Init
98b20 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 ialize and deini
98b30 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 tialize the mute
98b40 78 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a x subsystem..*/.
98b50 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d static sqlite3_m
98b60 75 74 65 78 20 77 69 6e 4d 75 74 65 78 5f 73 74 utex winMutex_st
98b70 61 74 69 63 4d 75 74 65 78 65 73 5b 36 5d 3b 0a aticMutexes[6];.
98b80 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d 75 static int winMu
98b90 74 65 78 5f 69 73 49 6e 69 74 20 3d 20 30 3b 0a tex_isInit = 0;.
98ba0 2f 2a 20 41 73 20 77 69 6e 4d 75 74 65 78 49 6e /* As winMutexIn
98bb0 69 74 28 29 20 61 6e 64 20 77 69 6e 4d 75 74 65 it() and winMute
98bc0 78 45 6e 64 28 29 20 61 72 65 20 63 61 6c 6c 65 xEnd() are calle
98bd0 64 20 61 73 20 70 61 72 74 0a 2a 2a 20 6f 66 20 d as part.** of
98be0 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 the sqlite3_init
98bf0 69 61 6c 69 7a 65 20 61 6e 64 20 73 71 6c 69 74 ialize and sqlit
98c00 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 0a 2a 2a e3_shutdown().**
98c10 20 70 72 6f 63 65 73 73 69 6e 67 2c 20 74 68 65 processing, the
98c20 20 22 69 6e 74 65 72 6c 6f 63 6b 65 64 22 20 6d "interlocked" m
98c30 61 67 69 63 20 69 73 20 70 72 6f 62 61 62 6c 79 agic is probably
98c40 20 6e 6f 74 0a 2a 2a 20 73 74 72 69 63 74 6c 79 not.** strictly
98c50 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 73 necessary..*/.s
98c60 74 61 74 69 63 20 6c 6f 6e 67 20 77 69 6e 4d 75 tatic long winMu
98c70 74 65 78 5f 6c 6f 63 6b 20 3d 20 30 3b 0a 0a 73 tex_lock = 0;..s
98c80 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d 75 74 tatic int winMut
98c90 65 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 0a 20 exInit(void){ .
98ca0 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20 74 6f /* The first to
98cb0 20 69 6e 63 72 65 6d 65 6e 74 20 74 6f 20 31 20 increment to 1
98cc0 64 6f 65 73 20 61 63 74 75 61 6c 20 69 6e 69 74 does actual init
98cd0 69 61 6c 69 7a 61 74 69 6f 6e 20 2a 2f 0a 20 20 ialization */.
98ce0 69 66 28 20 49 6e 74 65 72 6c 6f 63 6b 65 64 43 if( InterlockedC
98cf0 6f 6d 70 61 72 65 45 78 63 68 61 6e 67 65 28 26 ompareExchange(&
98d00 77 69 6e 4d 75 74 65 78 5f 6c 6f 63 6b 2c 20 31 winMutex_lock, 1
98d10 2c 20 30 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 , 0)==0 ){. i
98d20 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d nt i;. for(i=
98d30 30 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28 77 0; i<ArraySize(w
98d40 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63 4d 75 inMutex_staticMu
98d50 74 65 78 65 73 29 3b 20 69 2b 2b 29 7b 0a 20 20 texes); i++){.
98d60 20 20 20 20 49 6e 69 74 69 61 6c 69 7a 65 43 72 InitializeCr
98d70 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 77 iticalSection(&w
98d80 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63 4d 75 inMutex_staticMu
98d90 74 65 78 65 73 5b 69 5d 2e 6d 75 74 65 78 29 3b texes[i].mutex);
98da0 0a 20 20 20 20 7d 0a 20 20 20 20 77 69 6e 4d 75 . }. winMu
98db0 74 65 78 5f 69 73 49 6e 69 74 20 3d 20 31 3b 0a tex_isInit = 1;.
98dc0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
98dd0 53 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 69 73 20 Someone else is
98de0 69 6e 20 74 68 65 20 70 72 6f 63 65 73 73 20 6f in the process o
98df0 66 20 69 6e 69 74 69 6e 67 20 74 68 65 20 73 74 f initing the st
98e00 61 74 69 63 20 6d 75 74 65 78 65 73 20 2a 2f 0a atic mutexes */.
98e10 20 20 20 20 77 68 69 6c 65 28 20 21 77 69 6e 4d while( !winM
98e20 75 74 65 78 5f 69 73 49 6e 69 74 20 29 7b 0a 20 utex_isInit ){.
98e30 20 20 20 20 20 53 6c 65 65 70 28 31 29 3b 0a 20 Sleep(1);.
98e40 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
98e50 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a n SQLITE_OK; .}.
98e60 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d .static int winM
98e70 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 0a utexEnd(void){ .
98e80 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20 74 /* The first t
98e90 6f 20 64 65 63 72 65 6d 65 6e 74 20 74 6f 20 30 o decrement to 0
98ea0 20 64 6f 65 73 20 61 63 74 75 61 6c 20 73 68 75 does actual shu
98eb0 74 64 6f 77 6e 20 0a 20 20 2a 2a 20 28 77 68 69 tdown . ** (whi
98ec0 63 68 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65 ch should be the
98ed0 20 6c 61 73 74 20 74 6f 20 73 68 75 74 64 6f 77 last to shutdow
98ee0 6e 2e 29 20 2a 2f 0a 20 20 69 66 28 20 49 6e 74 n.) */. if( Int
98ef0 65 72 6c 6f 63 6b 65 64 43 6f 6d 70 61 72 65 45 erlockedCompareE
98f00 78 63 68 61 6e 67 65 28 26 77 69 6e 4d 75 74 65 xchange(&winMute
98f10 78 5f 6c 6f 63 6b 2c 20 30 2c 20 31 29 3d 3d 31 x_lock, 0, 1)==1
98f20 20 29 7b 0a 20 20 20 20 69 66 28 20 77 69 6e 4d ){. if( winM
98f30 75 74 65 78 5f 69 73 49 6e 69 74 3d 3d 31 20 29 utex_isInit==1 )
98f40 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 {. int i;.
98f50 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c for(i=0; i<
98f60 41 72 72 61 79 53 69 7a 65 28 77 69 6e 4d 75 74 ArraySize(winMut
98f70 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78 65 73 ex_staticMutexes
98f80 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 ); i++){.
98f90 20 44 65 6c 65 74 65 43 72 69 74 69 63 61 6c 53 DeleteCriticalS
98fa0 65 63 74 69 6f 6e 28 26 77 69 6e 4d 75 74 65 78 ection(&winMutex
98fb0 5f 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 69 _staticMutexes[i
98fc0 5d 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 ].mutex);.
98fd0 7d 0a 20 20 20 20 20 20 77 69 6e 4d 75 74 65 78 }. winMutex
98fe0 5f 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20 _isInit = 0;.
98ff0 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
99000 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 0a 2f SQLITE_OK; .}../
99010 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
99020 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 _mutex_alloc() r
99030 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 outine allocates
99040 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 a new.** mutex
99050 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f and returns a po
99060 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 49 66 inter to it. If
99070 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c it returns NULL
99080 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74 .** that means t
99090 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75 6c hat a mutex coul
990a0 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 d not be allocat
990b0 65 64 2e 20 20 53 51 4c 69 74 65 0a 2a 2a 20 77 ed. SQLite.** w
990c0 69 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 20 73 ill unwind its s
990d0 74 61 63 6b 20 61 6e 64 20 72 65 74 75 72 6e 20 tack and return
990e0 61 6e 20 65 72 72 6f 72 2e 20 20 54 68 65 20 61 an error. The a
990f0 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73 71 rgument.** to sq
99100 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f lite3_mutex_allo
99110 63 28 29 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 c() is one of th
99120 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 ese integer cons
99130 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c tants:.**.** <ul
99140 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 >.** <li> SQLIT
99150 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 E_MUTEX_FAST.**
99160 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
99170 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20 EX_RECURSIVE.**
99180 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
99190 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 EX_STATIC_MASTER
991a0 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 .** <li> SQLITE
991b0 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 _MUTEX_STATIC_ME
991c0 4d 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 M.** <li> SQLIT
991d0 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d E_MUTEX_STATIC_M
991e0 45 4d 32 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c EM2.** <li> SQL
991f0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
99200 5f 50 52 4e 47 0a 2a 2a 20 3c 6c 69 3e 20 20 53 _PRNG.** <li> S
99210 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
99220 49 43 5f 4c 52 55 0a 2a 2a 20 3c 6c 69 3e 20 20 IC_LRU.** <li>
99230 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
99240 54 49 43 5f 4c 52 55 32 0a 2a 2a 20 3c 2f 75 6c TIC_LRU2.** </ul
99250 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 >.**.** The firs
99260 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20 t two constants
99270 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 75 cause sqlite3_mu
99280 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63 tex_alloc() to c
99290 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d reate.** a new m
992a0 75 74 65 78 2e 20 20 54 68 65 20 6e 65 77 20 6d utex. The new m
992b0 75 74 65 78 20 69 73 20 72 65 63 75 72 73 69 76 utex is recursiv
992c0 65 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 e when SQLITE_MU
992d0 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a TEX_RECURSIVE.**
992e0 20 69 73 20 75 73 65 64 20 62 75 74 20 6e 6f 74 is used but not
992f0 20 6e 65 63 65 73 73 61 72 69 6c 79 20 73 6f 20 necessarily so
99300 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 when SQLITE_MUTE
99310 58 5f 46 41 53 54 20 69 73 20 75 73 65 64 2e 0a X_FAST is used..
99320 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 69 6d 70 ** The mutex imp
99330 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 lementation does
99340 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b not need to mak
99350 65 20 61 20 64 69 73 74 69 6e 63 74 69 6f 6e 0a e a distinction.
99360 2a 2a 20 62 65 74 77 65 65 6e 20 53 51 4c 49 54 ** between SQLIT
99370 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
99380 45 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d 55 54 E and SQLITE_MUT
99390 45 58 5f 46 41 53 54 20 69 66 20 69 74 20 64 6f EX_FAST if it do
993a0 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 es.** not want t
993b0 6f 2e 20 20 42 75 74 20 53 51 4c 69 74 65 20 77 o. But SQLite w
993c0 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 74 ill only request
993d0 20 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 a recursive mut
993e0 65 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77 ex in.** cases w
993f0 68 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20 6e here it really n
99400 65 65 64 73 20 6f 6e 65 2e 20 20 49 66 20 61 20 eeds one. If a
99410 66 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63 75 72 faster non-recur
99420 73 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20 69 6d sive mutex.** im
99430 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 plementation is
99440 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 available on the
99450 20 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d 2c 20 host platform,
99460 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 the mutex subsys
99470 74 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74 tem.** might ret
99480 75 72 6e 20 73 75 63 68 20 61 20 6d 75 74 65 78 urn such a mutex
99490 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 in response to
994a0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
994b0 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 74 68 T..**.** The oth
994c0 65 72 20 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d er allowed param
994d0 65 74 65 72 73 20 74 6f 20 73 71 6c 69 74 65 33 eters to sqlite3
994e0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65 _mutex_alloc() e
994f0 61 63 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 ach return.** a
99500 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 pointer to a sta
99510 74 69 63 20 70 72 65 65 78 69 73 74 69 6e 67 20 tic preexisting
99520 6d 75 74 65 78 2e 20 20 53 69 78 20 73 74 61 74 mutex. Six stat
99530 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 0a 2a ic mutexes are.*
99540 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 63 75 * used by the cu
99550 72 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 6f 66 rrent version of
99560 20 53 51 4c 69 74 65 2e 20 20 46 75 74 75 72 65 SQLite. Future
99570 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c versions of SQL
99580 69 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64 20 61 ite.** may add a
99590 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69 63 dditional static
995a0 20 6d 75 74 65 78 65 73 2e 20 20 53 74 61 74 69 mutexes. Stati
995b0 63 20 6d 75 74 65 78 65 73 20 61 72 65 20 66 6f c mutexes are fo
995c0 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 r internal.** us
995d0 65 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 6c 79 e by SQLite only
995e0 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 . Applications
995f0 74 68 61 74 20 75 73 65 20 53 51 4c 69 74 65 20 that use SQLite
99600 6d 75 74 65 78 65 73 20 73 68 6f 75 6c 64 0a 2a mutexes should.*
99610 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 20 64 * use only the d
99620 79 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20 72 ynamic mutexes r
99630 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c 49 54 eturned by SQLIT
99640 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f 72 0a E_MUTEX_FAST or.
99650 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f ** SQLITE_MUTEX_
99660 52 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a RECURSIVE..**.**
99670 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 6f 6e Note that if on
99680 65 20 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 63 e of the dynamic
99690 20 6d 75 74 65 78 20 70 61 72 61 6d 65 74 65 72 mutex parameter
996a0 73 20 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f s (SQLITE_MUTEX_
996b0 46 41 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 FAST.** or SQLIT
996c0 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
996d0 45 29 20 69 73 20 75 73 65 64 20 74 68 65 6e 20 E) is used then
996e0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c sqlite3_mutex_al
996f0 6c 6f 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 loc().** returns
99700 20 61 20 64 69 66 66 65 72 65 6e 74 20 6d 75 74 a different mut
99710 65 78 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c ex on every call
99720 2e 20 20 42 75 74 20 66 6f 72 20 74 68 65 20 73 . But for the s
99730 74 61 74 69 63 20 0a 2a 2a 20 6d 75 74 65 78 20 tatic .** mutex
99740 74 79 70 65 73 2c 20 74 68 65 20 73 61 6d 65 20 types, the same
99750 6d 75 74 65 78 20 69 73 20 72 65 74 75 72 6e 65 mutex is returne
99760 64 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 20 d on every call
99770 74 68 61 74 20 68 61 73 0a 2a 2a 20 74 68 65 20 that has.** the
99780 73 61 6d 65 20 74 79 70 65 20 6e 75 6d 62 65 72 same type number
99790 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 ..*/.static sqli
997a0 74 65 33 5f 6d 75 74 65 78 20 2a 77 69 6e 4d 75 te3_mutex *winMu
997b0 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 54 79 texAlloc(int iTy
997c0 70 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d pe){. sqlite3_m
997d0 75 74 65 78 20 2a 70 3b 0a 0a 20 20 73 77 69 74 utex *p;.. swit
997e0 63 68 28 20 69 54 79 70 65 20 29 7b 0a 20 20 20 ch( iType ){.
997f0 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 case SQLITE_MUT
99800 45 58 5f 46 41 53 54 3a 0a 20 20 20 20 63 61 73 EX_FAST:. cas
99810 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 e SQLITE_MUTEX_R
99820 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20 20 20 ECURSIVE: {.
99830 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c p = sqlite3Mal
99840 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 locZero( sizeof(
99850 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 *p) );. if(
99860 20 70 20 29 7b 20 20 0a 20 20 20 20 20 20 20 20 p ){ .
99870 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 p->id = iType;.
99880 20 20 20 20 20 20 20 49 6e 69 74 69 61 6c 69 7a Initializ
99890 65 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e eCriticalSection
998a0 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 (&p->mutex);.
998b0 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b }. break
998c0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 ;. }. defa
998d0 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 ult: {. ass
998e0 65 72 74 28 20 77 69 6e 4d 75 74 65 78 5f 69 73 ert( winMutex_is
998f0 49 6e 69 74 3d 3d 31 20 29 3b 0a 20 20 20 20 20 Init==1 );.
99900 20 61 73 73 65 72 74 28 20 69 54 79 70 65 2d 32 assert( iType-2
99910 20 3e 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 61 >= 0 );. a
99920 73 73 65 72 74 28 20 69 54 79 70 65 2d 32 20 3c ssert( iType-2 <
99930 20 41 72 72 61 79 53 69 7a 65 28 77 69 6e 4d 75 ArraySize(winMu
99940 74 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78 65 tex_staticMutexe
99950 73 29 20 29 3b 0a 20 20 20 20 20 20 70 20 3d 20 s) );. p =
99960 26 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63 &winMutex_static
99970 4d 75 74 65 78 65 73 5b 69 54 79 70 65 2d 32 5d Mutexes[iType-2]
99980 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 ;. p->id =
99990 69 54 79 70 65 3b 0a 20 20 20 20 20 20 62 72 65 iType;. bre
999a0 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 ak;. }. }.
999b0 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a return p;.}.../*
999c0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
999d0 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 20 70 deallocates a p
999e0 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 61 6c 6c reviously.** all
999f0 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 20 20 53 ocated mutex. S
99a00 51 4c 69 74 65 20 69 73 20 63 61 72 65 66 75 6c QLite is careful
99a10 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20 65 to deallocate e
99a20 76 65 72 79 0a 2a 2a 20 6d 75 74 65 78 20 74 68 very.** mutex th
99a30 61 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e at it allocates.
99a40 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
99a50 77 69 6e 4d 75 74 65 78 46 72 65 65 28 73 71 6c winMutexFree(sql
99a60 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a ite3_mutex *p){.
99a70 20 20 61 73 73 65 72 74 28 20 70 20 29 3b 0a 20 assert( p );.
99a80 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 assert( p->nRef
99a90 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
99aa0 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d p->id==SQLITE_M
99ab0 55 54 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e UTEX_FAST || p->
99ac0 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 id==SQLITE_MUTEX
99ad0 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 _RECURSIVE );.
99ae0 44 65 6c 65 74 65 43 72 69 74 69 63 61 6c 53 65 DeleteCriticalSe
99af0 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 ction(&p->mutex)
99b00 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 ;. sqlite3_free
99b10 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 (p);.}../*.** Th
99b20 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f e sqlite3_mutex_
99b30 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71 6c 69 enter() and sqli
99b40 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 te3_mutex_try()
99b50 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74 routines attempt
99b60 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 6d .** to enter a m
99b70 75 74 65 78 2e 20 20 49 66 20 61 6e 6f 74 68 65 utex. If anothe
99b80 72 20 74 68 72 65 61 64 20 69 73 20 61 6c 72 65 r thread is alre
99b90 61 64 79 20 77 69 74 68 69 6e 20 74 68 65 20 6d ady within the m
99ba0 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 utex,.** sqlite3
99bb0 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 77 _mutex_enter() w
99bc0 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71 ill block and sq
99bd0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 lite3_mutex_try(
99be0 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a ) will return.**
99bf0 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 54 SQLITE_BUSY. T
99c00 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 he sqlite3_mutex
99c10 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61 63 65 _try() interface
99c20 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f returns SQLITE_
99c30 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 63 65 OK.** upon succe
99c40 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20 4d 75 ssful entry. Mu
99c50 74 65 78 65 73 20 63 72 65 61 74 65 64 20 75 73 texes created us
99c60 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 ing SQLITE_MUTEX
99c70 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e 0a 2a _RECURSIVE can.*
99c80 2a 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c * be entered mul
99c90 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74 tiple times by t
99ca0 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 he same thread.
99cb0 20 49 6e 20 73 75 63 68 20 63 61 73 65 73 20 74 In such cases t
99cc0 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 he,.** mutex mus
99cd0 74 20 62 65 20 65 78 69 74 65 64 20 61 6e 20 65 t be exited an e
99ce0 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74 qual number of t
99cf0 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e 6f 74 imes before anot
99d00 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 63 61 her thread.** ca
99d10 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 74 68 65 n enter. If the
99d20 20 73 61 6d 65 20 74 68 72 65 61 64 20 74 72 69 same thread tri
99d30 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 79 20 es to enter any
99d40 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 6d 75 other kind of mu
99d50 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e tex.** more than
99d60 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 61 76 once, the behav
99d70 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 ior is undefined
99d80 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
99d90 20 77 69 6e 4d 75 74 65 78 45 6e 74 65 72 28 73 winMutexEnter(s
99da0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 qlite3_mutex *p)
99db0 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 {. assert( p->i
99dc0 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f d==SQLITE_MUTEX_
99dd0 52 45 43 55 52 53 49 56 45 20 7c 7c 20 77 69 6e RECURSIVE || win
99de0 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 MutexNotheld(p)
99df0 29 3b 0a 20 20 45 6e 74 65 72 43 72 69 74 69 63 );. EnterCritic
99e00 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 alSection(&p->mu
99e10 74 65 78 29 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72 tex);. p->owner
99e20 20 3d 20 47 65 74 43 75 72 72 65 6e 74 54 68 72 = GetCurrentThr
99e30 65 61 64 49 64 28 29 3b 20 0a 20 20 70 2d 3e 6e eadId(); . p->n
99e40 52 65 66 2b 2b 3b 0a 7d 0a 73 74 61 74 69 63 20 Ref++;.}.static
99e50 69 6e 74 20 77 69 6e 4d 75 74 65 78 54 72 79 28 int winMutexTry(
99e60 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 sqlite3_mutex *p
99e70 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
99e80 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 61 73 73 LITE_BUSY;. ass
99e90 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 ert( p->id==SQLI
99ea0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
99eb0 56 45 20 7c 7c 20 77 69 6e 4d 75 74 65 78 4e 6f VE || winMutexNo
99ec0 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 2f 2a theld(p) );. /*
99ed0 0a 20 20 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 . ** The sqlite
99ee0 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f 3_mutex_try() ro
99ef0 75 74 69 6e 65 20 69 73 20 76 65 72 79 20 72 61 utine is very ra
99f00 72 65 6c 79 20 75 73 65 64 2c 20 61 6e 64 20 77 rely used, and w
99f10 68 65 6e 20 69 74 0a 20 20 2a 2a 20 69 73 20 75 hen it. ** is u
99f20 73 65 64 20 69 74 20 69 73 20 6d 65 72 65 6c 79 sed it is merely
99f30 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e an optimization
99f40 2e 20 20 53 6f 20 69 74 20 69 73 20 4f 4b 20 66 . So it is OK f
99f50 6f 72 20 69 74 20 74 6f 20 61 6c 77 61 79 73 0a or it to always.
99f60 20 20 2a 2a 20 66 61 69 6c 2e 20 20 0a 20 20 2a ** fail. . *
99f70 2a 0a 20 20 2a 2a 20 54 68 65 20 54 72 79 45 6e *. ** The TryEn
99f80 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 74 69 terCriticalSecti
99f90 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 on() interface i
99fa0 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 s only available
99fb0 20 6f 6e 20 57 69 6e 4e 54 2e 0a 20 20 2a 2a 20 on WinNT.. **
99fc0 41 6e 64 20 73 6f 6d 65 20 77 69 6e 64 6f 77 73 And some windows
99fd0 20 63 6f 6d 70 69 6c 65 72 73 20 63 6f 6d 70 6c compilers compl
99fe0 61 69 6e 20 69 66 20 79 6f 75 20 74 72 79 20 74 ain if you try t
99ff0 6f 20 75 73 65 20 69 74 20 77 69 74 68 6f 75 74 o use it without
9a000 0a 20 20 2a 2a 20 66 69 72 73 74 20 64 6f 69 6e . ** first doin
9a010 67 20 73 6f 6d 65 20 23 64 65 66 69 6e 65 73 20 g some #defines
9a020 74 68 61 74 20 70 72 65 76 65 6e 74 20 53 51 4c that prevent SQL
9a030 69 74 65 20 66 72 6f 6d 20 62 75 69 6c 64 69 6e ite from buildin
9a040 67 20 6f 6e 20 57 69 6e 39 38 2e 0a 20 20 2a 2a g on Win98.. **
9a050 20 46 6f 72 20 74 68 61 74 20 72 65 61 73 6f 6e For that reason
9a060 2c 20 77 65 20 77 69 6c 6c 20 6f 6d 69 74 20 74 , we will omit t
9a070 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e his optimization
9a080 20 66 6f 72 20 6e 6f 77 2e 20 20 53 65 65 0a 20 for now. See.
9a090 20 2a 2a 20 74 69 63 6b 65 74 20 23 32 36 38 35 ** ticket #2685
9a0a0 2e 0a 20 20 2a 2f 0a 23 69 66 20 30 0a 20 20 69 .. */.#if 0. i
9a0b0 66 28 20 6d 75 74 65 78 49 73 4e 54 28 29 20 26 f( mutexIsNT() &
9a0c0 26 20 54 72 79 45 6e 74 65 72 43 72 69 74 69 63 & TryEnterCritic
9a0d0 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 alSection(&p->mu
9a0e0 74 65 78 29 20 29 7b 0a 20 20 20 20 70 2d 3e 6f tex) ){. p->o
9a0f0 77 6e 65 72 20 3d 20 47 65 74 43 75 72 72 65 6e wner = GetCurren
9a100 74 54 68 72 65 61 64 49 64 28 29 3b 0a 20 20 20 tThreadId();.
9a110 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 p->nRef++;.
9a120 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
9a130 20 20 7d 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 }.#else. UNUS
9a140 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 29 3b ED_PARAMETER(p);
9a150 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e .#endif. return
9a160 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 rc;.}../*.** Th
9a170 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f e sqlite3_mutex_
9a180 6c 65 61 76 65 28 29 20 72 6f 75 74 69 6e 65 20 leave() routine
9a190 65 78 69 74 73 20 61 20 6d 75 74 65 78 20 74 68 exits a mutex th
9a1a0 61 74 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f at was.** previo
9a1b0 75 73 6c 79 20 65 6e 74 65 72 65 64 20 62 79 20 usly entered by
9a1c0 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e the same thread.
9a1d0 20 20 54 68 65 20 62 65 68 61 76 69 6f 72 0a 2a The behavior.*
9a1e0 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 69 * is undefined i
9a1f0 66 20 74 68 65 20 6d 75 74 65 78 20 69 73 20 6e f the mutex is n
9a200 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 ot currently ent
9a210 65 72 65 64 20 6f 72 0a 2a 2a 20 69 73 20 6e 6f ered or.** is no
9a220 74 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f t currently allo
9a230 63 61 74 65 64 2e 20 20 53 51 4c 69 74 65 20 77 cated. SQLite w
9a240 69 6c 6c 20 6e 65 76 65 72 20 64 6f 20 65 69 74 ill never do eit
9a250 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 her..*/.static v
9a260 6f 69 64 20 77 69 6e 4d 75 74 65 78 4c 65 61 76 oid winMutexLeav
9a270 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 e(sqlite3_mutex
9a280 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 *p){. assert( p
9a290 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 61 73 ->nRef>0 );. as
9a2a0 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65 72 3d 3d sert( p->owner==
9a2b0 47 65 74 43 75 72 72 65 6e 74 54 68 72 65 61 64 GetCurrentThread
9a2c0 49 64 28 29 20 29 3b 0a 20 20 70 2d 3e 6e 52 65 Id() );. p->nRe
9a2d0 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 f--;. assert( p
9a2e0 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e ->nRef==0 || p->
9a2f0 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 id==SQLITE_MUTEX
9a300 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 _RECURSIVE );.
9a310 4c 65 61 76 65 43 72 69 74 69 63 61 6c 53 65 63 LeaveCriticalSec
9a320 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 3b tion(&p->mutex);
9a330 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .}..SQLITE_PRIVA
9a340 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 TE sqlite3_mutex
9a350 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 _methods *sqlite
9a360 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 3DefaultMutex(vo
9a370 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 id){. static sq
9a380 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 lite3_mutex_meth
9a390 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 ods sMutex = {.
9a3a0 20 20 20 77 69 6e 4d 75 74 65 78 49 6e 69 74 2c winMutexInit,
9a3b0 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 45 6e 64 . winMutexEnd
9a3c0 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 41 6c ,. winMutexAl
9a3d0 6c 6f 63 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 loc,. winMute
9a3e0 78 46 72 65 65 2c 0a 20 20 20 20 77 69 6e 4d 75 xFree,. winMu
9a3f0 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 77 69 texEnter,. wi
9a400 6e 4d 75 74 65 78 54 72 79 2c 0a 20 20 20 20 77 nMutexTry,. w
9a410 69 6e 4d 75 74 65 78 4c 65 61 76 65 2c 0a 23 69 inMutexLeave,.#i
9a420 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
9a430 47 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 48 65 G. winMutexHe
9a440 6c 64 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 ld,. winMutex
9a450 4e 6f 74 68 65 6c 64 0a 23 65 6c 73 65 0a 20 20 Notheld.#else.
9a460 20 20 30 2c 0a 20 20 20 20 30 0a 23 65 6e 64 69 0,. 0.#endi
9a470 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e f. };.. return
9a480 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 &sMutex;.}.#end
9a490 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54 if /* SQLITE_MUT
9a4a0 45 58 5f 57 33 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a EX_W32 */../****
9a4b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
9a4c0 66 20 6d 75 74 65 78 5f 77 33 32 2e 63 20 2a 2a f mutex_w32.c **
9a4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a4f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
9a500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
9a510 20 66 69 6c 65 20 6d 61 6c 6c 6f 63 2e 63 20 2a file malloc.c *
9a520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
9a550 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 2001 September
9a560 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 15.**.** The aut
9a570 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
9a580 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
9a590 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
9a5a0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
9a5b0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
9a5c0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
9a5d0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
9a5e0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
9a5f0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
9a600 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
9a610 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
9a620 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
9a630 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
9a640 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
9a650 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
9a660 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
9a670 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
9a680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a6b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a6c0 2a 0a 2a 2a 0a 2a 2a 20 4d 65 6d 6f 72 79 20 61 *.**.** Memory a
9a6d0 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 llocation functi
9a6e0 6f 6e 73 20 75 73 65 64 20 74 68 72 6f 75 67 68 ons used through
9a6f0 6f 75 74 20 73 71 6c 69 74 65 2e 0a 2a 2a 0a 2a out sqlite..**.*
9a700 2a 20 24 49 64 3a 20 6d 61 6c 6c 6f 63 2e 63 2c * $Id: malloc.c,
9a710 76 20 31 2e 36 36 20 32 30 30 39 2f 30 37 2f 31 v 1.66 2009/07/1
9a720 37 20 31 31 3a 34 34 3a 30 37 20 64 72 68 20 45 7 11:44:07 drh E
9a730 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 xp $.*/../*.** T
9a740 68 69 73 20 72 6f 75 74 69 6e 65 20 72 75 6e 73 his routine runs
9a750 20 77 68 65 6e 20 74 68 65 20 6d 65 6d 6f 72 79 when the memory
9a760 20 61 6c 6c 6f 63 61 74 6f 72 20 73 65 65 73 20 allocator sees
9a770 74 68 61 74 20 74 68 65 0a 2a 2a 20 74 6f 74 61 that the.** tota
9a780 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 l memory allocat
9a790 69 6f 6e 20 69 73 20 61 62 6f 75 74 20 74 6f 20 ion is about to
9a7a0 65 78 63 65 65 64 20 74 68 65 20 73 6f 66 74 20 exceed the soft
9a7b0 68 65 61 70 0a 2a 2a 20 6c 69 6d 69 74 2e 0a 2a heap.** limit..*
9a7c0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 6f /.static void so
9a7d0 66 74 48 65 61 70 4c 69 6d 69 74 45 6e 66 6f 72 ftHeapLimitEnfor
9a7e0 63 65 72 28 0a 20 20 76 6f 69 64 20 2a 4e 6f 74 cer(. void *Not
9a7f0 55 73 65 64 2c 20 0a 20 20 73 71 6c 69 74 65 33 Used, . sqlite3
9a800 5f 69 6e 74 36 34 20 4e 6f 74 55 73 65 64 32 2c _int64 NotUsed2,
9a810 0a 20 20 69 6e 74 20 61 6c 6c 6f 63 53 69 7a 65 . int allocSize
9a820 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 .){. UNUSED_PAR
9a830 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c AMETER2(NotUsed,
9a840 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 73 71 NotUsed2);. sq
9a850 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 lite3_release_me
9a860 6d 6f 72 79 28 61 6c 6c 6f 63 53 69 7a 65 29 3b mory(allocSize);
9a870 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 .}../*.** Set th
9a880 65 20 73 6f 66 74 20 68 65 61 70 2d 73 69 7a 65 e soft heap-size
9a890 20 6c 69 6d 69 74 20 66 6f 72 20 74 68 65 20 6c limit for the l
9a8a0 69 62 72 61 72 79 2e 20 50 61 73 73 69 6e 67 20 ibrary. Passing
9a8b0 61 20 7a 65 72 6f 20 6f 72 20 0a 2a 2a 20 6e 65 a zero or .** ne
9a8c0 67 61 74 69 76 65 20 76 61 6c 75 65 20 69 6e 64 gative value ind
9a8d0 69 63 61 74 65 73 20 6e 6f 20 6c 69 6d 69 74 2e icates no limit.
9a8e0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 .*/.SQLITE_API v
9a8f0 6f 69 64 20 73 71 6c 69 74 65 33 5f 73 6f 66 74 oid sqlite3_soft
9a900 5f 68 65 61 70 5f 6c 69 6d 69 74 28 69 6e 74 20 _heap_limit(int
9a910 6e 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 75 69 n){. sqlite3_ui
9a920 6e 74 36 34 20 69 4c 69 6d 69 74 3b 0a 20 20 69 nt64 iLimit;. i
9a930 6e 74 20 6f 76 65 72 61 67 65 3b 0a 20 20 69 66 nt overage;. if
9a940 28 20 6e 3c 30 20 29 7b 0a 20 20 20 20 69 4c 69 ( n<0 ){. iLi
9a950 6d 69 74 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 mit = 0;. }else
9a960 7b 0a 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 6e {. iLimit = n
9a970 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 ;. }.#ifndef SQ
9a980 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e LITE_OMIT_AUTOIN
9a990 49 54 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 69 IT. sqlite3_ini
9a9a0 74 69 61 6c 69 7a 65 28 29 3b 0a 23 65 6e 64 69 tialize();.#endi
9a9b0 66 0a 20 20 69 66 28 20 69 4c 69 6d 69 74 3e 30 f. if( iLimit>0
9a9c0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d ){. sqlite3M
9a9d0 65 6d 6f 72 79 41 6c 61 72 6d 28 73 6f 66 74 48 emoryAlarm(softH
9a9e0 65 61 70 4c 69 6d 69 74 45 6e 66 6f 72 63 65 72 eapLimitEnforcer
9a9f0 2c 20 30 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 , 0, iLimit);.
9aa00 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 }else{. sqlit
9aa10 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 30 2c e3MemoryAlarm(0,
9aa20 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 6f 76 0, 0);. }. ov
9aa30 65 72 61 67 65 20 3d 20 28 69 6e 74 29 28 73 71 erage = (int)(sq
9aa40 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 lite3_memory_use
9aa50 64 28 29 20 2d 20 28 69 36 34 29 6e 29 3b 0a 20 d() - (i64)n);.
9aa60 20 69 66 28 20 6f 76 65 72 61 67 65 3e 30 20 29 if( overage>0 )
9aa70 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 {. sqlite3_re
9aa80 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 6f 76 65 lease_memory(ove
9aa90 72 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a rage);. }.}../*
9aaa0 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 72 .** Attempt to r
9aab0 65 6c 65 61 73 65 20 75 70 20 74 6f 20 6e 20 62 elease up to n b
9aac0 79 74 65 73 20 6f 66 20 6e 6f 6e 2d 65 73 73 65 ytes of non-esse
9aad0 6e 74 69 61 6c 20 6d 65 6d 6f 72 79 20 63 75 72 ntial memory cur
9aae0 72 65 6e 74 6c 79 0a 2a 2a 20 68 65 6c 64 20 62 rently.** held b
9aaf0 79 20 53 51 4c 69 74 65 2e 20 41 6e 20 65 78 61 y SQLite. An exa
9ab00 6d 70 6c 65 20 6f 66 20 6e 6f 6e 2d 65 73 73 65 mple of non-esse
9ab10 6e 74 69 61 6c 20 6d 65 6d 6f 72 79 20 69 73 20 ntial memory is
9ab20 6d 65 6d 6f 72 79 20 75 73 65 64 20 74 6f 0a 2a memory used to.*
9ab30 2a 20 63 61 63 68 65 20 64 61 74 61 62 61 73 65 * cache database
9ab40 20 70 61 67 65 73 20 74 68 61 74 20 61 72 65 20 pages that are
9ab50 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 69 6e not currently in
9ab60 20 75 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f use..*/.SQLITE_
9ab70 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
9ab80 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 69 release_memory(i
9ab90 6e 74 20 6e 29 7b 0a 23 69 66 64 65 66 20 53 51 nt n){.#ifdef SQ
9aba0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f LITE_ENABLE_MEMO
9abb0 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 20 20 RY_MANAGEMENT.
9abc0 69 6e 74 20 6e 52 65 74 20 3d 20 30 3b 0a 20 20 int nRet = 0;.
9abd0 6e 52 65 74 20 2b 3d 20 73 71 6c 69 74 65 33 50 nRet += sqlite3P
9abe0 63 61 63 68 65 52 65 6c 65 61 73 65 4d 65 6d 6f cacheReleaseMemo
9abf0 72 79 28 6e 2d 6e 52 65 74 29 3b 0a 20 20 72 65 ry(n-nRet);. re
9ac00 74 75 72 6e 20 6e 52 65 74 3b 0a 23 65 6c 73 65 turn nRet;.#else
9ac10 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
9ac20 54 45 52 28 6e 29 3b 0a 20 20 72 65 74 75 72 6e TER(n);. return
9ac30 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6e 64 SQLITE_OK;.#end
9ac40 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 74 61 74 if.}../*.** Stat
9ac50 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6c 6f e information lo
9ac60 63 61 6c 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 cal to the memor
9ac70 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 y allocation sub
9ac80 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 system..*/.stati
9ac90 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 c SQLITE_WSD str
9aca0 75 63 74 20 4d 65 6d 30 47 6c 6f 62 61 6c 20 7b uct Mem0Global {
9acb0 0a 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 . /* Number of
9acc0 66 72 65 65 20 70 61 67 65 73 20 66 6f 72 20 73 free pages for s
9acd0 63 72 61 74 63 68 20 61 6e 64 20 70 61 67 65 2d cratch and page-
9ace0 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 2a 2f 0a cache memory */.
9acf0 20 20 75 33 32 20 6e 53 63 72 61 74 63 68 46 72 u32 nScratchFr
9ad00 65 65 3b 0a 20 20 75 33 32 20 6e 50 61 67 65 46 ee;. u32 nPageF
9ad10 72 65 65 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f ree;.. sqlite3_
9ad20 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 mutex *mutex;
9ad30 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 /* Mutex t
9ad40 6f 20 73 65 72 69 61 6c 69 7a 65 20 61 63 63 65 o serialize acce
9ad50 73 73 20 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a ss */.. /*. **
9ad60 20 54 68 65 20 61 6c 61 72 6d 20 63 61 6c 6c 62 The alarm callb
9ad70 61 63 6b 20 61 6e 64 20 69 74 73 20 61 72 67 75 ack and its argu
9ad80 6d 65 6e 74 73 2e 20 20 54 68 65 20 6d 65 6d 30 ments. The mem0
9ad90 2e 6d 75 74 65 78 20 6c 6f 63 6b 20 77 69 6c 6c .mutex lock will
9ada0 0a 20 20 2a 2a 20 62 65 20 68 65 6c 64 20 77 68 . ** be held wh
9adb0 69 6c 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b ile the callback
9adc0 20 69 73 20 72 75 6e 6e 69 6e 67 2e 20 20 52 65 is running. Re
9add0 63 75 72 73 69 76 65 20 63 61 6c 6c 73 20 69 6e cursive calls in
9ade0 74 6f 0a 20 20 2a 2a 20 74 68 65 20 6d 65 6d 6f to. ** the memo
9adf0 72 79 20 73 75 62 73 79 73 74 65 6d 20 61 72 65 ry subsystem are
9ae00 20 61 6c 6c 6f 77 65 64 2c 20 62 75 74 20 6e 6f allowed, but no
9ae10 20 6e 65 77 20 63 61 6c 6c 62 61 63 6b 73 20 77 new callbacks w
9ae20 69 6c 6c 20 62 65 0a 20 20 2a 2a 20 69 73 73 75 ill be. ** issu
9ae30 65 64 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 ed.. */. sqlit
9ae40 65 33 5f 69 6e 74 36 34 20 61 6c 61 72 6d 54 68 e3_int64 alarmTh
9ae50 72 65 73 68 6f 6c 64 3b 0a 20 20 76 6f 69 64 20 reshold;. void
9ae60 28 2a 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 29 (*alarmCallback)
9ae70 28 76 6f 69 64 2a 2c 20 73 71 6c 69 74 65 33 5f (void*, sqlite3_
9ae80 69 6e 74 36 34 2c 69 6e 74 29 3b 0a 20 20 76 6f int64,int);. vo
9ae90 69 64 20 2a 61 6c 61 72 6d 41 72 67 3b 0a 0a 20 id *alarmArg;..
9aea0 20 2f 2a 0a 20 20 2a 2a 20 50 6f 69 6e 74 65 72 /*. ** Pointer
9aeb0 73 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 s to the end of
9aec0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9aed0 66 69 67 2e 70 53 63 72 61 74 63 68 20 61 6e 64 fig.pScratch and
9aee0 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f . ** sqlite3Glo
9aef0 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 20 balConfig.pPage
9af00 74 6f 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 to a block of me
9af10 6d 6f 72 79 20 74 68 61 74 20 72 65 63 6f 72 64 mory that record
9af20 73 0a 20 20 2a 2a 20 77 68 69 63 68 20 70 61 67 s. ** which pag
9af30 65 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 es are available
9af40 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20 2a 61 53 .. */. u32 *aS
9af50 63 72 61 74 63 68 46 72 65 65 3b 0a 20 20 75 33 cratchFree;. u3
9af60 32 20 2a 61 50 61 67 65 46 72 65 65 3b 0a 7d 20 2 *aPageFree;.}
9af70 6d 65 6d 30 20 3d 20 7b 20 30 2c 20 30 2c 20 30 mem0 = { 0, 0, 0
9af80 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 20 , 0, 0, 0, 0, 0
9af90 7d 3b 0a 0a 23 64 65 66 69 6e 65 20 6d 65 6d 30 };..#define mem0
9afa0 20 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 4d GLOBAL(struct M
9afb0 65 6d 30 47 6c 6f 62 61 6c 2c 20 6d 65 6d 30 29 em0Global, mem0)
9afc0 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 ../*.** Initiali
9afd0 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c ze the memory al
9afe0 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 location subsyst
9aff0 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 em..*/.SQLITE_PR
9b000 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
9b010 33 4d 61 6c 6c 6f 63 49 6e 69 74 28 76 6f 69 64 3MallocInit(void
9b020 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 ){. if( sqlite3
9b030 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 GlobalConfig.m.x
9b040 4d 61 6c 6c 6f 63 3d 3d 30 20 29 7b 0a 20 20 20 Malloc==0 ){.
9b050 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 sqlite3MemSetDe
9b060 66 61 75 6c 74 28 29 3b 0a 20 20 7d 0a 20 20 6d fault();. }. m
9b070 65 6d 73 65 74 28 26 6d 65 6d 30 2c 20 30 2c 20 emset(&mem0, 0,
9b080 73 69 7a 65 6f 66 28 6d 65 6d 30 29 29 3b 0a 20 sizeof(mem0));.
9b090 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 if( sqlite3Glob
9b0a0 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 alConfig.bCoreMu
9b0b0 74 65 78 20 29 7b 0a 20 20 20 20 6d 65 6d 30 2e tex ){. mem0.
9b0c0 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d mutex = sqlite3M
9b0d0 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 utexAlloc(SQLITE
9b0e0 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 _MUTEX_STATIC_ME
9b0f0 4d 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 M);. }. if( sq
9b100 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9b110 67 2e 70 53 63 72 61 74 63 68 20 26 26 20 73 71 g.pScratch && sq
9b120 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9b130 67 2e 73 7a 53 63 72 61 74 63 68 3e 3d 31 30 30 g.szScratch>=100
9b140 0a 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 . && sqlite
9b150 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 3GlobalConfig.nS
9b160 63 72 61 74 63 68 3e 3d 30 20 29 7b 0a 20 20 20 cratch>=0 ){.
9b170 20 69 6e 74 20 69 3b 0a 20 20 20 20 73 71 6c 69 int i;. sqli
9b180 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
9b190 73 7a 53 63 72 61 74 63 68 20 3d 20 52 4f 55 4e szScratch = ROUN
9b1a0 44 44 4f 57 4e 38 28 73 71 6c 69 74 65 33 47 6c DDOWN8(sqlite3Gl
9b1b0 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 obalConfig.szScr
9b1c0 61 74 63 68 2d 34 29 3b 0a 20 20 20 20 6d 65 6d atch-4);. mem
9b1d0 30 2e 61 53 63 72 61 74 63 68 46 72 65 65 20 3d 0.aScratchFree =
9b1e0 20 28 75 33 32 2a 29 26 28 28 63 68 61 72 2a 29 (u32*)&((char*)
9b1f0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9b200 66 69 67 2e 70 53 63 72 61 74 63 68 29 0a 20 20 fig.pScratch).
9b210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9b220 5b 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f [sqlite3GlobalCo
9b230 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68 2a 73 nfig.szScratch*s
9b240 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9b250 69 67 2e 6e 53 63 72 61 74 63 68 5d 3b 0a 20 20 ig.nScratch];.
9b260 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 71 6c for(i=0; i<sql
9b270 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9b280 2e 6e 53 63 72 61 74 63 68 3b 20 69 2b 2b 29 7b .nScratch; i++){
9b290 20 6d 65 6d 30 2e 61 53 63 72 61 74 63 68 46 72 mem0.aScratchFr
9b2a0 65 65 5b 69 5d 20 3d 20 69 3b 20 7d 0a 20 20 20 ee[i] = i; }.
9b2b0 20 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 mem0.nScratchFr
9b2c0 65 65 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 ee = sqlite3Glob
9b2d0 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 alConfig.nScratc
9b2e0 68 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 h;. }else{.
9b2f0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9b300 66 69 67 2e 70 53 63 72 61 74 63 68 20 3d 20 30 fig.pScratch = 0
9b310 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f ;. sqlite3Glo
9b320 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 balConfig.szScra
9b330 74 63 68 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 tch = 0;. }. i
9b340 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c f( sqlite3Global
9b350 43 6f 6e 66 69 67 2e 70 50 61 67 65 20 26 26 20 Config.pPage &&
9b360 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9b370 66 69 67 2e 73 7a 50 61 67 65 3e 3d 35 31 32 0a fig.szPage>=512.
9b380 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 && sqlite3
9b390 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 GlobalConfig.nPa
9b3a0 67 65 3e 3d 31 20 29 7b 0a 20 20 20 20 69 6e 74 ge>=1 ){. int
9b3b0 20 69 3b 0a 20 20 20 20 69 6e 74 20 6f 76 65 72 i;. int over
9b3c0 68 65 61 64 3b 0a 20 20 20 20 69 6e 74 20 73 7a head;. int sz
9b3d0 20 3d 20 52 4f 55 4e 44 44 4f 57 4e 38 28 73 71 = ROUNDDOWN8(sq
9b3e0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9b3f0 67 2e 73 7a 50 61 67 65 29 3b 0a 20 20 20 20 69 g.szPage);. i
9b400 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33 47 6c nt n = sqlite3Gl
9b410 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 obalConfig.nPage
9b420 3b 0a 20 20 20 20 6f 76 65 72 68 65 61 64 20 3d ;. overhead =
9b430 20 28 34 2a 6e 20 2b 20 73 7a 20 2d 20 31 29 2f (4*n + sz - 1)/
9b440 73 7a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 sz;. sqlite3G
9b450 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 lobalConfig.nPag
9b460 65 20 2d 3d 20 6f 76 65 72 68 65 61 64 3b 0a 20 e -= overhead;.
9b470 20 20 20 6d 65 6d 30 2e 61 50 61 67 65 46 72 65 mem0.aPageFre
9b480 65 20 3d 20 28 75 33 32 2a 29 26 28 28 63 68 61 e = (u32*)&((cha
9b490 72 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c r*)sqlite3Global
9b4a0 43 6f 6e 66 69 67 2e 70 50 61 67 65 29 0a 20 20 Config.pPage).
9b4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9b4c0 5b 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f [sqlite3GlobalCo
9b4d0 6e 66 69 67 2e 73 7a 50 61 67 65 2a 73 71 6c 69 nfig.szPage*sqli
9b4e0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
9b4f0 6e 50 61 67 65 5d 3b 0a 20 20 20 20 66 6f 72 28 nPage];. for(
9b500 69 3d 30 3b 20 69 3c 73 71 6c 69 74 65 33 47 6c i=0; i<sqlite3Gl
9b510 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 obalConfig.nPage
9b520 3b 20 69 2b 2b 29 7b 20 6d 65 6d 30 2e 61 50 61 ; i++){ mem0.aPa
9b530 67 65 46 72 65 65 5b 69 5d 20 3d 20 69 3b 20 7d geFree[i] = i; }
9b540 0a 20 20 20 20 6d 65 6d 30 2e 6e 50 61 67 65 46 . mem0.nPageF
9b550 72 65 65 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f ree = sqlite3Glo
9b560 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 3b balConfig.nPage;
9b570 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 . }else{. sq
9b580 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9b590 67 2e 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 20 g.pPage = 0;.
9b5a0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
9b5b0 6e 66 69 67 2e 73 7a 50 61 67 65 20 3d 20 30 3b nfig.szPage = 0;
9b5c0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 . }. return sq
9b5d0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9b5e0 67 2e 6d 2e 78 49 6e 69 74 28 73 71 6c 69 74 65 g.m.xInit(sqlite
9b5f0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 3GlobalConfig.m.
9b600 70 41 70 70 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a pAppData);.}../*
9b610 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 .** Deinitialize
9b620 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f the memory allo
9b630 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d cation subsystem
9b640 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
9b650 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
9b660 4d 61 6c 6c 6f 63 45 6e 64 28 76 6f 69 64 29 7b MallocEnd(void){
9b670 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c . if( sqlite3Gl
9b680 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 68 obalConfig.m.xSh
9b690 75 74 64 6f 77 6e 20 29 7b 0a 20 20 20 20 73 71 utdown ){. sq
9b6a0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9b6b0 67 2e 6d 2e 78 53 68 75 74 64 6f 77 6e 28 73 71 g.m.xShutdown(sq
9b6c0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9b6d0 67 2e 6d 2e 70 41 70 70 44 61 74 61 29 3b 0a 20 g.m.pAppData);.
9b6e0 20 7d 0a 20 20 6d 65 6d 73 65 74 28 26 6d 65 6d }. memset(&mem
9b6f0 30 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d 65 6d 0, 0, sizeof(mem
9b700 30 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 0));.}../*.** Re
9b710 74 75 72 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 turn the amount
9b720 6f 66 20 6d 65 6d 6f 72 79 20 63 75 72 72 65 6e of memory curren
9b730 74 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 74 2e tly checked out.
9b740 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 .*/.SQLITE_API s
9b750 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c qlite3_int64 sql
9b760 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 ite3_memory_used
9b770 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20 6e 2c (void){. int n,
9b780 20 6d 78 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 mx;. sqlite3_i
9b790 6e 74 36 34 20 72 65 73 3b 0a 20 20 73 71 6c 69 nt64 res;. sqli
9b7a0 74 65 33 5f 73 74 61 74 75 73 28 53 51 4c 49 54 te3_status(SQLIT
9b7b0 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f E_STATUS_MEMORY_
9b7c0 55 53 45 44 2c 20 26 6e 2c 20 26 6d 78 2c 20 30 USED, &n, &mx, 0
9b7d0 29 3b 0a 20 20 72 65 73 20 3d 20 28 73 71 6c 69 );. res = (sqli
9b7e0 74 65 33 5f 69 6e 74 36 34 29 6e 3b 20 20 2f 2a te3_int64)n; /*
9b7f0 20 57 6f 72 6b 20 61 72 6f 75 6e 64 20 62 75 67 Work around bug
9b800 20 69 6e 20 42 6f 72 6c 61 6e 64 20 43 2e 20 54 in Borland C. T
9b810 69 63 6b 65 74 20 23 33 32 31 36 20 2a 2f 0a 20 icket #3216 */.
9b820 20 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a return res;.}..
9b830 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
9b840 20 6d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 maximum amount
9b850 6f 66 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 68 of memory that h
9b860 61 73 20 65 76 65 72 20 62 65 65 6e 0a 2a 2a 20 as ever been.**
9b870 63 68 65 63 6b 65 64 20 6f 75 74 20 73 69 6e 63 checked out sinc
9b880 65 20 65 69 74 68 65 72 20 74 68 65 20 62 65 67 e either the beg
9b890 69 6e 6e 69 6e 67 20 6f 66 20 74 68 69 73 20 70 inning of this p
9b8a0 72 6f 63 65 73 73 0a 2a 2a 20 6f 72 20 73 69 6e rocess.** or sin
9b8b0 63 65 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 ce the most rece
9b8c0 6e 74 20 72 65 73 65 74 2e 0a 2a 2f 0a 53 51 4c nt reset..*/.SQL
9b8d0 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f ITE_API sqlite3_
9b8e0 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 6d 65 int64 sqlite3_me
9b8f0 6d 6f 72 79 5f 68 69 67 68 77 61 74 65 72 28 69 mory_highwater(i
9b900 6e 74 20 72 65 73 65 74 46 6c 61 67 29 7b 0a 20 nt resetFlag){.
9b910 20 69 6e 74 20 6e 2c 20 6d 78 3b 0a 20 20 73 71 int n, mx;. sq
9b920 6c 69 74 65 33 5f 69 6e 74 36 34 20 72 65 73 3b lite3_int64 res;
9b930 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 . sqlite3_statu
9b940 73 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f s(SQLITE_STATUS_
9b950 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 26 6e 2c MEMORY_USED, &n,
9b960 20 26 6d 78 2c 20 72 65 73 65 74 46 6c 61 67 29 &mx, resetFlag)
9b970 3b 0a 20 20 72 65 73 20 3d 20 28 73 71 6c 69 74 ;. res = (sqlit
9b980 65 33 5f 69 6e 74 36 34 29 6d 78 3b 20 20 2f 2a e3_int64)mx; /*
9b990 20 57 6f 72 6b 20 61 72 6f 75 6e 64 20 62 75 67 Work around bug
9b9a0 20 69 6e 20 42 6f 72 6c 61 6e 64 20 43 2e 20 54 in Borland C. T
9b9b0 69 63 6b 65 74 20 23 33 32 31 36 20 2a 2f 0a 20 icket #3216 */.
9b9c0 20 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a return res;.}..
9b9d0 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 /*.** Change the
9b9e0 20 61 6c 61 72 6d 20 63 61 6c 6c 62 61 63 6b 0a alarm callback.
9b9f0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
9ba00 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d E int sqlite3Mem
9ba10 6f 72 79 41 6c 61 72 6d 28 0a 20 20 76 6f 69 64 oryAlarm(. void
9ba20 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 (*xCallback)(voi
9ba30 64 20 2a 70 41 72 67 2c 20 73 71 6c 69 74 65 33 d *pArg, sqlite3
9ba40 5f 69 6e 74 36 34 20 75 73 65 64 2c 69 6e 74 20 _int64 used,int
9ba50 4e 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 N),. void *pArg
9ba60 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 ,. sqlite3_int6
9ba70 34 20 69 54 68 72 65 73 68 6f 6c 64 0a 29 7b 0a 4 iThreshold.){.
9ba80 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
9ba90 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 enter(mem0.mutex
9baa0 29 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 );. mem0.alarmC
9bab0 61 6c 6c 62 61 63 6b 20 3d 20 78 43 61 6c 6c 62 allback = xCallb
9bac0 61 63 6b 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 ack;. mem0.alar
9bad0 6d 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 6d mArg = pArg;. m
9bae0 65 6d 30 2e 61 6c 61 72 6d 54 68 72 65 73 68 6f em0.alarmThresho
9baf0 6c 64 20 3d 20 69 54 68 72 65 73 68 6f 6c 64 3b ld = iThreshold;
9bb00 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
9bb10 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 _leave(mem0.mute
9bb20 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c x);. return SQL
9bb30 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 ITE_OK;.}..#ifnd
9bb40 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 ef SQLITE_OMIT_D
9bb50 45 50 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 EPRECATED./*.**
9bb60 44 65 70 72 65 63 61 74 65 64 20 65 78 74 65 72 Deprecated exter
9bb70 6e 61 6c 20 69 6e 74 65 72 66 61 63 65 2e 20 20 nal interface.
9bb80 49 6e 74 65 72 6e 61 6c 2f 63 6f 72 65 20 53 51 Internal/core SQ
9bb90 4c 69 74 65 20 63 6f 64 65 0a 2a 2a 20 73 68 6f Lite code.** sho
9bba0 75 6c 64 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 uld call sqlite3
9bbb0 4d 65 6d 6f 72 79 41 6c 61 72 6d 2e 0a 2a 2f 0a MemoryAlarm..*/.
9bbc0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
9bbd0 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 61 6c qlite3_memory_al
9bbe0 61 72 6d 28 0a 20 20 76 6f 69 64 28 2a 78 43 61 arm(. void(*xCa
9bbf0 6c 6c 62 61 63 6b 29 28 76 6f 69 64 20 2a 70 41 llback)(void *pA
9bc00 72 67 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 rg, sqlite3_int6
9bc10 34 20 75 73 65 64 2c 69 6e 74 20 4e 29 2c 0a 20 4 used,int N),.
9bc20 20 76 6f 69 64 20 2a 70 41 72 67 2c 0a 20 20 73 void *pArg,. s
9bc30 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 54 68 qlite3_int64 iTh
9bc40 72 65 73 68 6f 6c 64 0a 29 7b 0a 20 20 72 65 74 reshold.){. ret
9bc50 75 72 6e 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 urn sqlite3Memor
9bc60 79 41 6c 61 72 6d 28 78 43 61 6c 6c 62 61 63 6b yAlarm(xCallback
9bc70 2c 20 70 41 72 67 2c 20 69 54 68 72 65 73 68 6f , pArg, iThresho
9bc80 6c 64 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f ld);.}.#endif../
9bc90 2a 0a 2a 2a 20 54 72 69 67 67 65 72 20 74 68 65 *.** Trigger the
9bca0 20 61 6c 61 72 6d 20 0a 2a 2f 0a 73 74 61 74 69 alarm .*/.stati
9bcb0 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 c void sqlite3Ma
9bcc0 6c 6c 6f 63 41 6c 61 72 6d 28 69 6e 74 20 6e 42 llocAlarm(int nB
9bcd0 79 74 65 29 7b 0a 20 20 76 6f 69 64 20 28 2a 78 yte){. void (*x
9bce0 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c Callback)(void*,
9bcf0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e sqlite3_int64,in
9bd00 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e t);. sqlite3_in
9bd10 74 36 34 20 6e 6f 77 55 73 65 64 3b 0a 20 20 76 t64 nowUsed;. v
9bd20 6f 69 64 20 2a 70 41 72 67 3b 0a 20 20 69 66 28 oid *pArg;. if(
9bd30 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 mem0.alarmCallb
9bd40 61 63 6b 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b ack==0 ) return;
9bd50 0a 20 20 78 43 61 6c 6c 62 61 63 6b 20 3d 20 6d . xCallback = m
9bd60 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 em0.alarmCallbac
9bd70 6b 3b 0a 20 20 6e 6f 77 55 73 65 64 20 3d 20 73 k;. nowUsed = s
9bd80 71 6c 69 74 65 33 53 74 61 74 75 73 56 61 6c 75 qlite3StatusValu
9bd90 65 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f e(SQLITE_STATUS_
9bda0 4d 45 4d 4f 52 59 5f 55 53 45 44 29 3b 0a 20 20 MEMORY_USED);.
9bdb0 70 41 72 67 20 3d 20 6d 65 6d 30 2e 61 6c 61 72 pArg = mem0.alar
9bdc0 6d 41 72 67 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 mArg;. mem0.ala
9bdd0 72 6d 43 61 6c 6c 62 61 63 6b 20 3d 20 30 3b 0a rmCallback = 0;.
9bde0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
9bdf0 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 leave(mem0.mutex
9be00 29 3b 0a 20 20 78 43 61 6c 6c 62 61 63 6b 28 70 );. xCallback(p
9be10 41 72 67 2c 20 6e 6f 77 55 73 65 64 2c 20 6e 42 Arg, nowUsed, nB
9be20 79 74 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f yte);. sqlite3_
9be30 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 mutex_enter(mem0
9be40 2e 6d 75 74 65 78 29 3b 0a 20 20 6d 65 6d 30 2e .mutex);. mem0.
9be50 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20 3d 20 alarmCallback =
9be60 78 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 6d 65 6d xCallback;. mem
9be70 30 2e 61 6c 61 72 6d 41 72 67 20 3d 20 70 41 72 0.alarmArg = pAr
9be80 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 61 g;.}../*.** Do a
9be90 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
9bea0 6f 6e 20 77 69 74 68 20 73 74 61 74 69 73 74 69 on with statisti
9beb0 63 73 20 61 6e 64 20 61 6c 61 72 6d 73 2e 20 20 cs and alarms.
9bec0 41 73 73 75 6d 65 20 74 68 65 0a 2a 2a 20 6c 6f Assume the.** lo
9bed0 63 6b 20 69 73 20 61 6c 72 65 61 64 79 20 68 65 ck is already he
9bee0 6c 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ld..*/.static in
9bef0 74 20 6d 61 6c 6c 6f 63 57 69 74 68 41 6c 61 72 t mallocWithAlar
9bf00 6d 28 69 6e 74 20 6e 2c 20 76 6f 69 64 20 2a 2a m(int n, void **
9bf10 70 70 29 7b 0a 20 20 69 6e 74 20 6e 46 75 6c 6c pp){. int nFull
9bf20 3b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61 ;. void *p;. a
9bf30 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
9bf40 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 30 2e 6d utex_held(mem0.m
9bf50 75 74 65 78 29 20 29 3b 0a 20 20 6e 46 75 6c 6c utex) );. nFull
9bf60 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c = sqlite3Global
9bf70 43 6f 6e 66 69 67 2e 6d 2e 78 52 6f 75 6e 64 75 Config.m.xRoundu
9bf80 70 28 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 p(n);. sqlite3S
9bf90 74 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f tatusSet(SQLITE_
9bfa0 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 STATUS_MALLOC_SI
9bfb0 5a 45 2c 20 6e 29 3b 0a 20 20 69 66 28 20 6d 65 ZE, n);. if( me
9bfc0 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b m0.alarmCallback
9bfd0 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e !=0 ){. int n
9bfe0 55 73 65 64 20 3d 20 73 71 6c 69 74 65 33 53 74 Used = sqlite3St
9bff0 61 74 75 73 56 61 6c 75 65 28 53 51 4c 49 54 45 atusValue(SQLITE
9c000 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 _STATUS_MEMORY_U
9c010 53 45 44 29 3b 0a 20 20 20 20 69 66 28 20 6e 55 SED);. if( nU
9c020 73 65 64 2b 6e 46 75 6c 6c 20 3e 3d 20 6d 65 6d sed+nFull >= mem
9c030 30 2e 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 0.alarmThreshold
9c040 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
9c050 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 46 75 3MallocAlarm(nFu
9c060 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 ll);. }. }.
9c070 20 70 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 p = sqlite3Glob
9c080 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c alConfig.m.xMall
9c090 6f 63 28 6e 46 75 6c 6c 29 3b 0a 20 20 69 66 28 oc(nFull);. if(
9c0a0 20 70 3d 3d 30 20 26 26 20 6d 65 6d 30 2e 61 6c p==0 && mem0.al
9c0b0 61 72 6d 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20 armCallback ){.
9c0c0 20 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 sqlite3Malloc
9c0d0 41 6c 61 72 6d 28 6e 46 75 6c 6c 29 3b 0a 20 20 Alarm(nFull);.
9c0e0 20 20 70 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f p = sqlite3Glo
9c0f0 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c balConfig.m.xMal
9c100 6c 6f 63 28 6e 46 75 6c 6c 29 3b 0a 20 20 7d 0a loc(nFull);. }.
9c110 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 6e if( p ){. n
9c120 46 75 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4d 61 Full = sqlite3Ma
9c130 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 20 20 20 llocSize(p);.
9c140 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 sqlite3StatusAd
9c150 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f d(SQLITE_STATUS_
9c160 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 6e 46 75 MEMORY_USED, nFu
9c170 6c 6c 29 3b 0a 20 20 7d 0a 20 20 2a 70 70 20 3d ll);. }. *pp =
9c180 20 70 3b 0a 20 20 72 65 74 75 72 6e 20 6e 46 75 p;. return nFu
9c190 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c ll;.}../*.** All
9c1a0 6f 63 61 74 65 20 6d 65 6d 6f 72 79 2e 20 20 54 ocate memory. T
9c1b0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6c his routine is l
9c1c0 69 6b 65 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c ike sqlite3_mall
9c1d0 6f 63 28 29 20 65 78 63 65 70 74 20 74 68 61 74 oc() except that
9c1e0 20 69 74 0a 2a 2a 20 61 73 73 75 6d 65 73 20 74 it.** assumes t
9c1f0 68 65 20 6d 65 6d 6f 72 79 20 73 75 62 73 79 73 he memory subsys
9c200 74 65 6d 20 68 61 73 20 61 6c 72 65 61 64 79 20 tem has already
9c210 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 been initialized
9c220 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
9c230 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 ATE void *sqlite
9c240 33 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 3Malloc(int n){.
9c250 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 69 66 28 void *p;. if(
9c260 20 6e 3c 3d 30 20 7c 7c 20 6e 3e 3d 30 78 37 66 n<=0 || n>=0x7f
9c270 66 66 66 66 30 30 20 29 7b 0a 20 20 20 20 2f 2a ffff00 ){. /*
9c280 20 41 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 A memory alloca
9c290 74 69 6f 6e 20 6f 66 20 61 20 6e 75 6d 62 65 72 tion of a number
9c2a0 20 6f 66 20 62 79 74 65 73 20 77 68 69 63 68 20 of bytes which
9c2b0 69 73 20 6e 65 61 72 20 74 68 65 20 6d 61 78 69 is near the maxi
9c2c0 6d 75 6d 0a 20 20 20 20 2a 2a 20 73 69 67 6e 65 mum. ** signe
9c2d0 64 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 d integer value
9c2e0 6d 69 67 68 74 20 63 61 75 73 65 20 61 6e 20 69 might cause an i
9c2f0 6e 74 65 67 65 72 20 6f 76 65 72 66 6c 6f 77 20 nteger overflow
9c300 69 6e 73 69 64 65 20 6f 66 20 74 68 65 0a 20 20 inside of the.
9c310 20 20 2a 2a 20 78 4d 61 6c 6c 6f 63 28 29 2e 20 ** xMalloc().
9c320 20 48 65 6e 63 65 20 77 65 20 6c 69 6d 69 74 20 Hence we limit
9c330 74 68 65 20 6d 61 78 69 6d 75 6d 20 73 69 7a 65 the maximum size
9c340 20 74 6f 20 30 78 37 66 66 66 66 66 30 30 2c 20 to 0x7fffff00,
9c350 67 69 76 69 6e 67 0a 20 20 20 20 2a 2a 20 32 35 giving. ** 25
9c360 35 20 62 79 74 65 73 20 6f 66 20 6f 76 65 72 68 5 bytes of overh
9c370 65 61 64 2e 20 20 53 51 4c 69 74 65 20 69 74 73 ead. SQLite its
9c380 65 6c 66 20 77 69 6c 6c 20 6e 65 76 65 72 20 75 elf will never u
9c390 73 65 20 61 6e 79 74 68 69 6e 67 20 6e 65 61 72 se anything near
9c3a0 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 61 6d 6f . ** this amo
9c3b0 75 6e 74 2e 20 20 54 68 65 20 6f 6e 6c 79 20 77 unt. The only w
9c3c0 61 79 20 74 6f 20 72 65 61 63 68 20 74 68 65 20 ay to reach the
9c3d0 6c 69 6d 69 74 20 69 73 20 77 69 74 68 20 73 71 limit is with sq
9c3e0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 2a lite3_malloc() *
9c3f0 2f 0a 20 20 20 20 70 20 3d 20 30 3b 0a 20 20 7d /. p = 0;. }
9c400 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 else if( sqlite3
9c410 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 GlobalConfig.bMe
9c420 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 73 71 6c mstat ){. sql
9c430 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
9c440 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 (mem0.mutex);.
9c450 20 20 6d 61 6c 6c 6f 63 57 69 74 68 41 6c 61 72 mallocWithAlar
9c460 6d 28 6e 2c 20 26 70 29 3b 0a 20 20 20 20 73 71 m(n, &p);. sq
9c470 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
9c480 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 e(mem0.mutex);.
9c490 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20 }else{. p =
9c4a0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9c4b0 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 28 6e 29 fig.m.xMalloc(n)
9c4c0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 ;. }. return p
9c4d0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
9c4e0 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d version of the m
9c4f0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
9c500 20 69 73 20 66 6f 72 20 75 73 65 20 62 79 20 74 is for use by t
9c510 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e 0a he application..
9c520 2a 2a 20 46 69 72 73 74 20 6d 61 6b 65 20 73 75 ** First make su
9c530 72 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 73 75 re the memory su
9c540 62 73 79 73 74 65 6d 20 69 73 20 69 6e 69 74 69 bsystem is initi
9c550 61 6c 69 7a 65 64 2c 20 74 68 65 6e 20 64 6f 20 alized, then do
9c560 74 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f the.** allocatio
9c570 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 n..*/.SQLITE_API
9c580 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 6d void *sqlite3_m
9c590 61 6c 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 23 69 alloc(int n){.#i
9c5a0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
9c5b0 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 66 28 T_AUTOINIT. if(
9c5c0 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c sqlite3_initial
9c5d0 69 7a 65 28 29 20 29 20 72 65 74 75 72 6e 20 30 ize() ) return 0
9c5e0 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 ;.#endif. retur
9c5f0 6e 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 n sqlite3Malloc(
9c600 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 n);.}../*.** Eac
9c610 68 20 74 68 72 65 61 64 20 6d 61 79 20 6f 6e 6c h thread may onl
9c620 79 20 68 61 76 65 20 61 20 73 69 6e 67 6c 65 20 y have a single
9c630 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 6c 6c 6f outstanding allo
9c640 63 61 74 69 6f 6e 20 66 72 6f 6d 0a 2a 2a 20 78 cation from.** x
9c650 53 63 72 61 74 63 68 4d 61 6c 6c 6f 63 28 29 2e ScratchMalloc().
9c660 20 20 57 65 20 76 65 72 69 66 79 20 74 68 69 73 We verify this
9c670 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 6e 20 74 constraint in t
9c680 68 65 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 he single-thread
9c690 65 64 0a 2a 2a 20 63 61 73 65 20 62 79 20 73 65 ed.** case by se
9c6a0 74 74 69 6e 67 20 73 63 72 61 74 63 68 41 6c 6c tting scratchAll
9c6b0 6f 63 4f 75 74 20 74 6f 20 31 20 77 68 65 6e 20 ocOut to 1 when
9c6c0 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a an allocation.**
9c6d0 20 69 73 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 is outstanding
9c6e0 63 6c 65 61 72 69 6e 67 20 69 74 20 77 68 65 6e clearing it when
9c6f0 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 the allocation
9c700 69 73 20 66 72 65 65 64 2e 0a 2a 2f 0a 23 69 66 is freed..*/.#if
9c710 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 SQLITE_THREADSA
9c720 46 45 3d 3d 30 20 26 26 20 21 64 65 66 69 6e 65 FE==0 && !define
9c730 64 28 4e 44 45 42 55 47 29 0a 73 74 61 74 69 63 d(NDEBUG).static
9c740 20 69 6e 74 20 73 63 72 61 74 63 68 41 6c 6c 6f int scratchAllo
9c750 63 4f 75 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 cOut = 0;.#endif
9c760 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 .../*.** Allocat
9c770 65 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 69 73 e memory that is
9c780 20 74 6f 20 62 65 20 75 73 65 64 20 61 6e 64 20 to be used and
9c790 72 65 6c 65 61 73 65 64 20 72 69 67 68 74 20 61 released right a
9c7a0 77 61 79 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 way..** This rou
9c7b0 74 69 6e 65 20 69 73 20 73 69 6d 69 6c 61 72 20 tine is similar
9c7c0 74 6f 20 61 6c 6c 6f 63 61 28 29 20 69 6e 20 74 to alloca() in t
9c7d0 68 61 74 20 69 74 20 69 73 20 6e 6f 74 20 69 6e hat it is not in
9c7e0 74 65 6e 64 65 64 0a 2a 2a 20 66 6f 72 20 73 69 tended.** for si
9c7f0 74 75 61 74 69 6f 6e 73 20 77 68 65 72 65 20 74 tuations where t
9c800 68 65 20 6d 65 6d 6f 72 79 20 6d 69 67 68 74 20 he memory might
9c810 62 65 20 68 65 6c 64 20 6c 6f 6e 67 2d 74 65 72 be held long-ter
9c820 6d 2e 20 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74 m. This.** rout
9c830 69 6e 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20 ine is intended
9c840 74 6f 20 67 65 74 20 6d 65 6d 6f 72 79 20 74 6f to get memory to
9c850 20 6f 6c 64 20 6c 61 72 67 65 20 74 72 61 6e 73 old large trans
9c860 69 65 6e 74 20 64 61 74 61 0a 2a 2a 20 73 74 72 ient data.** str
9c870 75 63 74 75 72 65 73 20 74 68 61 74 20 77 6f 75 uctures that wou
9c880 6c 64 20 6e 6f 74 20 6e 6f 72 6d 61 6c 6c 79 20 ld not normally
9c890 66 69 74 20 6f 6e 20 74 68 65 20 73 74 61 63 6b fit on the stack
9c8a0 20 6f 66 20 61 6e 0a 2a 2a 20 65 6d 62 65 64 64 of an.** embedd
9c8b0 65 64 20 70 72 6f 63 65 73 73 6f 72 2e 0a 2a 2f ed processor..*/
9c8c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
9c8d0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 53 63 72 void *sqlite3Scr
9c8e0 61 74 63 68 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e atchMalloc(int n
9c8f0 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 ){. void *p;.
9c900 61 73 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a 0a assert( n>0 );..
9c910 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 #if SQLITE_THREA
9c920 44 53 41 46 45 3d 3d 30 20 26 26 20 21 64 65 66 DSAFE==0 && !def
9c930 69 6e 65 64 28 4e 44 45 42 55 47 29 0a 20 20 2f ined(NDEBUG). /
9c940 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 6e 6f * Verify that no
9c950 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 73 more than one s
9c960 63 72 61 74 63 68 20 61 6c 6c 6f 63 61 74 69 6f cratch allocatio
9c970 6e 20 70 65 72 20 74 68 72 65 61 64 0a 20 20 2a n per thread. *
9c980 2a 20 69 73 20 6f 75 74 73 74 61 6e 64 69 6e 67 * is outstanding
9c990 20 61 74 20 6f 6e 65 20 74 69 6d 65 2e 20 20 28 at one time. (
9c9a0 54 68 69 73 20 69 73 20 6f 6e 6c 79 20 63 68 65 This is only che
9c9b0 63 6b 65 64 20 69 6e 20 74 68 65 0a 20 20 2a 2a cked in the. **
9c9c0 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 single-threaded
9c9d0 20 63 61 73 65 20 73 69 6e 63 65 20 63 68 65 63 case since chec
9c9e0 6b 69 6e 67 20 69 6e 20 74 68 65 20 6d 75 6c 74 king in the mult
9c9f0 69 2d 74 68 72 65 61 64 65 64 20 63 61 73 65 0a i-threaded case.
9ca00 20 20 2a 2a 20 77 6f 75 6c 64 20 62 65 20 6d 75 ** would be mu
9ca10 63 68 20 6d 6f 72 65 20 63 6f 6d 70 6c 69 63 61 ch more complica
9ca20 74 65 64 2e 29 20 2a 2f 0a 20 20 61 73 73 65 72 ted.) */. asser
9ca30 74 28 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f t( scratchAllocO
9ca40 75 74 3d 3d 30 20 29 3b 0a 23 65 6e 64 69 66 0a ut==0 );.#endif.
9ca50 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c . if( sqlite3Gl
9ca60 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 obalConfig.szScr
9ca70 61 74 63 68 3c 6e 20 29 7b 0a 20 20 20 20 67 6f atch<n ){. go
9ca80 74 6f 20 73 63 72 61 74 63 68 5f 6f 76 65 72 66 to scratch_overf
9ca90 6c 6f 77 3b 0a 20 20 7d 65 6c 73 65 7b 20 20 0a low;. }else{ .
9caa0 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
9cab0 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 x_enter(mem0.mut
9cac0 65 78 29 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d ex);. if( mem
9cad0 30 2e 6e 53 63 72 61 74 63 68 46 72 65 65 3d 3d 0.nScratchFree==
9cae0 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 0 ){. sqlit
9caf0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d e3_mutex_leave(m
9cb00 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 em0.mutex);.
9cb10 20 20 67 6f 74 6f 20 73 63 72 61 74 63 68 5f 6f goto scratch_o
9cb20 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 7d 65 6c verflow;. }el
9cb30 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b se{. int i;
9cb40 0a 20 20 20 20 20 20 69 20 3d 20 6d 65 6d 30 2e . i = mem0.
9cb50 61 53 63 72 61 74 63 68 46 72 65 65 5b 2d 2d 6d aScratchFree[--m
9cb60 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 65 65 em0.nScratchFree
9cb70 5d 3b 0a 20 20 20 20 20 20 69 20 2a 3d 20 73 71 ];. i *= sq
9cb80 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9cb90 67 2e 73 7a 53 63 72 61 74 63 68 3b 0a 20 20 20 g.szScratch;.
9cba0 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 sqlite3Status
9cbb0 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 Add(SQLITE_STATU
9cbc0 53 5f 53 43 52 41 54 43 48 5f 55 53 45 44 2c 20 S_SCRATCH_USED,
9cbd0 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 1);. sqlite
9cbe0 33 53 74 61 74 75 73 53 65 74 28 53 51 4c 49 54 3StatusSet(SQLIT
9cbf0 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 E_STATUS_SCRATCH
9cc00 5f 53 49 5a 45 2c 20 6e 29 3b 0a 20 20 20 20 20 _SIZE, n);.
9cc10 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
9cc20 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 eave(mem0.mutex)
9cc30 3b 0a 20 20 20 20 20 20 70 20 3d 20 28 76 6f 69 ;. p = (voi
9cc40 64 2a 29 26 28 28 63 68 61 72 2a 29 73 71 6c 69 d*)&((char*)sqli
9cc50 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
9cc60 70 53 63 72 61 74 63 68 29 5b 69 5d 3b 0a 20 20 pScratch)[i];.
9cc70 20 20 20 20 61 73 73 65 72 74 28 20 20 28 28 28 assert( (((
9cc80 75 38 2a 29 70 20 2d 20 28 75 38 2a 29 30 29 20 u8*)p - (u8*)0)
9cc90 26 20 37 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d & 7)==0 );. }
9cca0 0a 20 20 7d 0a 23 69 66 20 53 51 4c 49 54 45 5f . }.#if SQLITE_
9ccb0 54 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26 26 THREADSAFE==0 &&
9ccc0 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 !defined(NDEBUG
9ccd0 29 0a 20 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 ). scratchAlloc
9cce0 4f 75 74 20 3d 20 70 21 3d 30 3b 0a 23 65 6e 64 Out = p!=0;.#end
9ccf0 69 66 0a 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a if.. return p;.
9cd00 0a 73 63 72 61 74 63 68 5f 6f 76 65 72 66 6c 6f .scratch_overflo
9cd10 77 3a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 w:. if( sqlite3
9cd20 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 GlobalConfig.bMe
9cd30 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 73 71 6c mstat ){. sql
9cd40 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
9cd50 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 (mem0.mutex);.
9cd60 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53 sqlite3StatusS
9cd70 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 et(SQLITE_STATUS
9cd80 5f 53 43 52 41 54 43 48 5f 53 49 5a 45 2c 20 6e _SCRATCH_SIZE, n
9cd90 29 3b 0a 20 20 20 20 6e 20 3d 20 6d 61 6c 6c 6f );. n = mallo
9cda0 63 57 69 74 68 41 6c 61 72 6d 28 6e 2c 20 26 70 cWithAlarm(n, &p
9cdb0 29 3b 0a 20 20 20 20 69 66 28 20 70 20 29 20 73 );. if( p ) s
9cdc0 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 qlite3StatusAdd(
9cdd0 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 SQLITE_STATUS_SC
9cde0 52 41 54 43 48 5f 4f 56 45 52 46 4c 4f 57 2c 20 RATCH_OVERFLOW,
9cdf0 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f n);. sqlite3_
9ce00 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 mutex_leave(mem0
9ce10 2e 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 .mutex);. }else
9ce20 7b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 {. p = sqlite
9ce30 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 3GlobalConfig.m.
9ce40 78 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 7d 0a xMalloc(n);. }.
9ce50 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 #if SQLITE_THREA
9ce60 44 53 41 46 45 3d 3d 30 20 26 26 20 21 64 65 66 DSAFE==0 && !def
9ce70 69 6e 65 64 28 4e 44 45 42 55 47 29 0a 20 20 73 ined(NDEBUG). s
9ce80 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d cratchAllocOut =
9ce90 20 70 21 3d 30 3b 0a 23 65 6e 64 69 66 0a 20 20 p!=0;.#endif.
9cea0 72 65 74 75 72 6e 20 70 3b 20 20 20 20 0a 7d 0a return p; .}.
9ceb0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
9cec0 6f 69 64 20 73 71 6c 69 74 65 33 53 63 72 61 74 oid sqlite3Scrat
9ced0 63 68 46 72 65 65 28 76 6f 69 64 20 2a 70 29 7b chFree(void *p){
9cee0 0a 20 20 69 66 28 20 70 20 29 7b 0a 0a 23 69 66 . if( p ){..#if
9cef0 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 SQLITE_THREADSA
9cf00 46 45 3d 3d 30 20 26 26 20 21 64 65 66 69 6e 65 FE==0 && !define
9cf10 64 28 4e 44 45 42 55 47 29 0a 20 20 20 20 2f 2a d(NDEBUG). /*
9cf20 20 56 65 72 69 66 79 20 74 68 61 74 20 6e 6f 20 Verify that no
9cf30 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 73 63 more than one sc
9cf40 72 61 74 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e ratch allocation
9cf50 20 70 65 72 20 74 68 72 65 61 64 0a 20 20 20 20 per thread.
9cf60 2a 2a 20 69 73 20 6f 75 74 73 74 61 6e 64 69 6e ** is outstandin
9cf70 67 20 61 74 20 6f 6e 65 20 74 69 6d 65 2e 20 20 g at one time.
9cf80 28 54 68 69 73 20 69 73 20 6f 6e 6c 79 20 63 68 (This is only ch
9cf90 65 63 6b 65 64 20 69 6e 20 74 68 65 0a 20 20 20 ecked in the.
9cfa0 20 2a 2a 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 ** single-threa
9cfb0 64 65 64 20 63 61 73 65 20 73 69 6e 63 65 20 63 ded case since c
9cfc0 68 65 63 6b 69 6e 67 20 69 6e 20 74 68 65 20 6d hecking in the m
9cfd0 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20 63 61 ulti-threaded ca
9cfe0 73 65 0a 20 20 20 20 2a 2a 20 77 6f 75 6c 64 20 se. ** would
9cff0 62 65 20 6d 75 63 68 20 6d 6f 72 65 20 63 6f 6d be much more com
9d000 70 6c 69 63 61 74 65 64 2e 29 20 2a 2f 0a 20 20 plicated.) */.
9d010 20 20 61 73 73 65 72 74 28 20 73 63 72 61 74 63 assert( scratc
9d020 68 41 6c 6c 6f 63 4f 75 74 3d 3d 31 20 29 3b 0a hAllocOut==1 );.
9d030 20 20 20 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 scratchAlloc
9d040 4f 75 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a Out = 0;.#endif.
9d050 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
9d060 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 GlobalConfig.pSc
9d070 72 61 74 63 68 3d 3d 30 0a 20 20 20 20 20 20 20 ratch==0.
9d080 20 20 20 20 7c 7c 20 70 3c 73 71 6c 69 74 65 33 || p<sqlite3
9d090 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 GlobalConfig.pSc
9d0a0 72 61 74 63 68 0a 20 20 20 20 20 20 20 20 20 20 ratch.
9d0b0 20 7c 7c 20 70 3e 3d 28 76 6f 69 64 2a 29 6d 65 || p>=(void*)me
9d0c0 6d 30 2e 61 53 63 72 61 74 63 68 46 72 65 65 20 m0.aScratchFree
9d0d0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c ){. if( sql
9d0e0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9d0f0 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 20 .bMemstat ){.
9d100 20 20 20 20 20 69 6e 74 20 69 53 69 7a 65 20 3d int iSize =
9d110 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 sqlite3MallocSi
9d120 7a 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 73 ze(p);. s
9d130 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
9d140 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a er(mem0.mutex);.
9d150 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 sqlite3S
9d160 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f tatusAdd(SQLITE_
9d170 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f STATUS_SCRATCH_O
9d180 56 45 52 46 4c 4f 57 2c 20 2d 69 53 69 7a 65 29 VERFLOW, -iSize)
9d190 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
9d1a0 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 3StatusAdd(SQLIT
9d1b0 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f E_STATUS_MEMORY_
9d1c0 55 53 45 44 2c 20 2d 69 53 69 7a 65 29 3b 0a 20 USED, -iSize);.
9d1d0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c sqlite3Gl
9d1e0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 obalConfig.m.xFr
9d1f0 65 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 73 ee(p);. s
9d200 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
9d210 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a ve(mem0.mutex);.
9d220 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
9d230 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 sqlite3Glob
9d240 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 alConfig.m.xFree
9d250 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 (p);. }.
9d260 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e }else{. in
9d270 74 20 69 3b 0a 20 20 20 20 20 20 69 20 3d 20 28 t i;. i = (
9d280 69 6e 74 29 28 28 75 38 2a 29 70 20 2d 20 28 75 int)((u8*)p - (u
9d290 38 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 8*)sqlite3Global
9d2a0 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 29 Config.pScratch)
9d2b0 3b 0a 20 20 20 20 20 20 69 20 2f 3d 20 73 71 6c ;. i /= sql
9d2c0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9d2d0 2e 73 7a 53 63 72 61 74 63 68 3b 0a 20 20 20 20 .szScratch;.
9d2e0 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 assert( i>=0 &
9d2f0 26 20 69 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61 & i<sqlite3Globa
9d300 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 lConfig.nScratch
9d310 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 );. sqlite
9d320 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 3_mutex_enter(me
9d330 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 m0.mutex);.
9d340 20 61 73 73 65 72 74 28 20 6d 65 6d 30 2e 6e 53 assert( mem0.nS
9d350 63 72 61 74 63 68 46 72 65 65 3c 28 75 33 32 29 cratchFree<(u32)
9d360 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9d370 66 69 67 2e 6e 53 63 72 61 74 63 68 20 29 3b 0a fig.nScratch );.
9d380 20 20 20 20 20 20 6d 65 6d 30 2e 61 53 63 72 61 mem0.aScra
9d390 74 63 68 46 72 65 65 5b 6d 65 6d 30 2e 6e 53 63 tchFree[mem0.nSc
9d3a0 72 61 74 63 68 46 72 65 65 2b 2b 5d 20 3d 20 69 ratchFree++] = i
9d3b0 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 ;. sqlite3S
9d3c0 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f tatusAdd(SQLITE_
9d3d0 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 55 STATUS_SCRATCH_U
9d3e0 53 45 44 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 SED, -1);.
9d3f0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
9d400 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b ave(mem0.mutex);
9d410 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a . }. }.}../*
9d420 0a 2a 2a 20 54 52 55 45 20 69 66 20 70 20 69 73 .** TRUE if p is
9d430 20 61 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d a lookaside mem
9d440 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 ory allocation f
9d450 72 6f 6d 20 64 62 0a 2a 2f 0a 23 69 66 6e 64 65 rom db.*/.#ifnde
9d460 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f f SQLITE_OMIT_LO
9d470 4f 4b 41 53 49 44 45 0a 73 74 61 74 69 63 20 69 OKASIDE.static i
9d480 6e 74 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 73 nt isLookaside(s
9d490 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 qlite3 *db, void
9d4a0 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 64 *p){. return d
9d4b0 62 20 26 26 20 70 20 26 26 20 70 3e 3d 64 62 2d b && p && p>=db-
9d4c0 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 53 74 61 72 >lookaside.pStar
9d4d0 74 20 26 26 20 70 3c 64 62 2d 3e 6c 6f 6f 6b 61 t && p<db->looka
9d4e0 73 69 64 65 2e 70 45 6e 64 3b 0a 7d 0a 23 65 6c side.pEnd;.}.#el
9d4f0 73 65 0a 23 64 65 66 69 6e 65 20 69 73 4c 6f 6f se.#define isLoo
9d500 6b 61 73 69 64 65 28 41 2c 42 29 20 30 0a 23 65 kaside(A,B) 0.#e
9d510 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 ndif../*.** Retu
9d520 72 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 rn the size of a
9d530 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
9d540 6f 6e 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 62 on previously ob
9d550 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 tained from.** s
9d560 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 20 6f qlite3Malloc() o
9d570 72 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 r sqlite3_malloc
9d580 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ()..*/.SQLITE_PR
9d590 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
9d5a0 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 76 6f 69 64 3MallocSize(void
9d5b0 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 *p){. return s
9d5c0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9d5d0 69 67 2e 6d 2e 78 53 69 7a 65 28 70 29 3b 0a 7d ig.m.xSize(p);.}
9d5e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
9d5f0 69 6e 74 20 73 71 6c 69 74 65 33 44 62 4d 61 6c int sqlite3DbMal
9d600 6c 6f 63 53 69 7a 65 28 73 71 6c 69 74 65 33 20 locSize(sqlite3
9d610 2a 64 62 2c 20 76 6f 69 64 20 2a 70 29 7b 0a 20 *db, void *p){.
9d620 20 61 73 73 65 72 74 28 20 64 62 3d 3d 30 20 7c assert( db==0 |
9d630 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f | sqlite3_mutex_
9d640 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 held(db->mutex)
9d650 29 3b 0a 20 20 69 66 28 20 69 73 4c 6f 6f 6b 61 );. if( isLooka
9d660 73 69 64 65 28 64 62 2c 20 70 29 20 29 7b 0a 20 side(db, p) ){.
9d670 20 20 20 72 65 74 75 72 6e 20 64 62 2d 3e 6c 6f return db->lo
9d680 6f 6b 61 73 69 64 65 2e 73 7a 3b 0a 20 20 7d 65 okaside.sz;. }e
9d690 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 lse{. return
9d6a0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9d6b0 66 69 67 2e 6d 2e 78 53 69 7a 65 28 70 29 3b 0a fig.m.xSize(p);.
9d6c0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 }.}../*.** Fre
9d6d0 65 20 6d 65 6d 6f 72 79 20 70 72 65 76 69 6f 75 e memory previou
9d6e0 73 6c 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f sly obtained fro
9d6f0 6d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 m sqlite3Malloc(
9d700 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 )..*/.SQLITE_API
9d710 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 66 72 void sqlite3_fr
9d720 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 69 ee(void *p){. i
9d730 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e f( p==0 ) return
9d740 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 ;. if( sqlite3G
9d750 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d lobalConfig.bMem
9d760 73 74 61 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 stat ){. sqli
9d770 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
9d780 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 mem0.mutex);.
9d790 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 sqlite3StatusAd
9d7a0 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f d(SQLITE_STATUS_
9d7b0 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 2d 73 71 MEMORY_USED, -sq
9d7c0 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 lite3MallocSize(
9d7d0 70 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 p));. sqlite3
9d7e0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 GlobalConfig.m.x
9d7f0 46 72 65 65 28 70 29 3b 0a 20 20 20 20 73 71 6c Free(p);. sql
9d800 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
9d810 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 (mem0.mutex);.
9d820 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 }else{. sqlit
9d830 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
9d840 2e 78 46 72 65 65 28 70 29 3b 0a 20 20 7d 0a 7d .xFree(p);. }.}
9d850 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d ../*.** Free mem
9d860 6f 72 79 20 74 68 61 74 20 6d 69 67 68 74 20 62 ory that might b
9d870 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 e associated wit
9d880 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 h a particular d
9d890 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 atabase.** conne
9d8a0 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ction..*/.SQLITE
9d8b0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
9d8c0 6c 69 74 65 33 44 62 46 72 65 65 28 73 71 6c 69 lite3DbFree(sqli
9d8d0 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 te3 *db, void *p
9d8e0 29 7b 0a 20 20 61 73 73 65 72 74 28 20 64 62 3d ){. assert( db=
9d8f0 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 =0 || sqlite3_mu
9d900 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 tex_held(db->mut
9d910 65 78 29 20 29 3b 0a 20 20 69 66 28 20 69 73 4c ex) );. if( isL
9d920 6f 6f 6b 61 73 69 64 65 28 64 62 2c 20 70 29 20 ookaside(db, p)
9d930 29 7b 0a 20 20 20 20 4c 6f 6f 6b 61 73 69 64 65 ){. Lookaside
9d940 53 6c 6f 74 20 2a 70 42 75 66 20 3d 20 28 4c 6f Slot *pBuf = (Lo
9d950 6f 6b 61 73 69 64 65 53 6c 6f 74 2a 29 70 3b 0a okasideSlot*)p;.
9d960 20 20 20 20 70 42 75 66 2d 3e 70 4e 65 78 74 20 pBuf->pNext
9d970 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e = db->lookaside.
9d980 70 46 72 65 65 3b 0a 20 20 20 20 64 62 2d 3e 6c pFree;. db->l
9d990 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 65 20 3d ookaside.pFree =
9d9a0 20 70 42 75 66 3b 0a 20 20 20 20 64 62 2d 3e 6c pBuf;. db->l
9d9b0 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 2d 2d 3b ookaside.nOut--;
9d9c0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 . }else{. sq
9d9d0 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 lite3_free(p);.
9d9e0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e }.}../*.** Chan
9d9f0 67 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 ge the size of a
9da00 6e 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72 n existing memor
9da10 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a y allocation.*/.
9da20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
9da30 6f 69 64 20 2a 73 71 6c 69 74 65 33 52 65 61 6c oid *sqlite3Real
9da40 6c 6f 63 28 76 6f 69 64 20 2a 70 4f 6c 64 2c 20 loc(void *pOld,
9da50 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 69 int nBytes){. i
9da60 6e 74 20 6e 4f 6c 64 2c 20 6e 4e 65 77 3b 0a 20 nt nOld, nNew;.
9da70 20 76 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20 69 void *pNew;. i
9da80 66 28 20 70 4f 6c 64 3d 3d 30 20 29 7b 0a 20 20 f( pOld==0 ){.
9da90 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
9daa0 4d 61 6c 6c 6f 63 28 6e 42 79 74 65 73 29 3b 0a Malloc(nBytes);.
9dab0 20 20 7d 0a 20 20 69 66 28 20 6e 42 79 74 65 73 }. if( nBytes
9dac0 3c 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 <=0 ){. sqlit
9dad0 65 33 5f 66 72 65 65 28 70 4f 6c 64 29 3b 0a 20 e3_free(pOld);.
9dae0 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
9daf0 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3e 3d 30 . if( nBytes>=0
9db00 78 37 66 66 66 66 66 30 30 20 29 7b 0a 20 20 20 x7fffff00 ){.
9db10 20 2f 2a 20 54 68 65 20 30 78 37 66 66 66 66 30 /* The 0x7ffff0
9db20 30 20 6c 69 6d 69 74 20 74 65 72 6d 20 69 73 20 0 limit term is
9db30 65 78 70 6c 61 69 6e 65 64 20 69 6e 20 63 6f 6d explained in com
9db40 6d 65 6e 74 73 20 6f 6e 20 73 71 6c 69 74 65 33 ments on sqlite3
9db50 4d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 20 20 20 20 Malloc() */.
9db60 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
9db70 6e 4f 6c 64 20 3d 20 73 71 6c 69 74 65 33 4d 61 nOld = sqlite3Ma
9db80 6c 6c 6f 63 53 69 7a 65 28 70 4f 6c 64 29 3b 0a llocSize(pOld);.
9db90 20 20 6e 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 nNew = sqlite3
9dba0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 GlobalConfig.m.x
9dbb0 52 6f 75 6e 64 75 70 28 6e 42 79 74 65 73 29 3b Roundup(nBytes);
9dbc0 0a 20 20 69 66 28 20 6e 4f 6c 64 3d 3d 6e 4e 65 . if( nOld==nNe
9dbd0 77 20 29 7b 0a 20 20 20 20 70 4e 65 77 20 3d 20 w ){. pNew =
9dbe0 70 4f 6c 64 3b 0a 20 20 7d 65 6c 73 65 20 69 66 pOld;. }else if
9dbf0 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 ( sqlite3GlobalC
9dc00 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 onfig.bMemstat )
9dc10 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 {. sqlite3_mu
9dc20 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d tex_enter(mem0.m
9dc30 75 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 utex);. sqlit
9dc40 65 33 53 74 61 74 75 73 53 65 74 28 53 51 4c 49 e3StatusSet(SQLI
9dc50 54 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 TE_STATUS_MALLOC
9dc60 5f 53 49 5a 45 2c 20 6e 42 79 74 65 73 29 3b 0a _SIZE, nBytes);.
9dc70 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 if( sqlite3S
9dc80 74 61 74 75 73 56 61 6c 75 65 28 53 51 4c 49 54 tatusValue(SQLIT
9dc90 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f E_STATUS_MEMORY_
9dca0 55 53 45 44 29 2b 6e 4e 65 77 2d 6e 4f 6c 64 20 USED)+nNew-nOld
9dcb0 3e 3d 20 0a 20 20 20 20 20 20 20 20 20 20 6d 65 >= . me
9dcc0 6d 30 2e 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c m0.alarmThreshol
9dcd0 64 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 d ){. sqlit
9dce0 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 4e e3MallocAlarm(nN
9dcf0 65 77 2d 6e 4f 6c 64 29 3b 0a 20 20 20 20 7d 0a ew-nOld);. }.
9dd00 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 pNew = sqlit
9dd10 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
9dd20 2e 78 52 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 .xRealloc(pOld,
9dd30 6e 4e 65 77 29 3b 0a 20 20 20 20 69 66 28 20 70 nNew);. if( p
9dd40 4e 65 77 3d 3d 30 20 26 26 20 6d 65 6d 30 2e 61 New==0 && mem0.a
9dd50 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20 29 7b 0a larmCallback ){.
9dd60 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 61 6c sqlite3Mal
9dd70 6c 6f 63 41 6c 61 72 6d 28 6e 42 79 74 65 73 29 locAlarm(nBytes)
9dd80 3b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 ;. pNew = s
9dd90 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9dda0 69 67 2e 6d 2e 78 52 65 61 6c 6c 6f 63 28 70 4f ig.m.xRealloc(pO
9ddb0 6c 64 2c 20 6e 4e 65 77 29 3b 0a 20 20 20 20 7d ld, nNew);. }
9ddc0 0a 20 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b . if( pNew ){
9ddd0 0a 20 20 20 20 20 20 6e 4e 65 77 20 3d 20 73 71 . nNew = sq
9dde0 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 lite3MallocSize(
9ddf0 70 4e 65 77 29 3b 0a 20 20 20 20 20 20 73 71 6c pNew);. sql
9de00 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 ite3StatusAdd(SQ
9de10 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f LITE_STATUS_MEMO
9de20 52 59 5f 55 53 45 44 2c 20 6e 4e 65 77 2d 6e 4f RY_USED, nNew-nO
9de30 6c 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 ld);. }. s
9de40 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
9de50 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a ve(mem0.mutex);.
9de60 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 }else{. pNe
9de70 77 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 w = sqlite3Globa
9de80 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 65 61 6c 6c lConfig.m.xReall
9de90 6f 63 28 70 4f 6c 64 2c 20 6e 4e 65 77 29 3b 0a oc(pOld, nNew);.
9dea0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 }. return pNe
9deb0 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 w;.}../*.** The
9dec0 70 75 62 6c 69 63 20 69 6e 74 65 72 66 61 63 65 public interface
9ded0 20 74 6f 20 73 71 6c 69 74 65 33 52 65 61 6c 6c to sqlite3Reall
9dee0 6f 63 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74 oc. Make sure t
9def0 68 61 74 20 74 68 65 20 6d 65 6d 6f 72 79 0a 2a hat the memory.*
9df00 2a 20 73 75 62 73 79 73 74 65 6d 20 69 73 20 69 * subsystem is i
9df10 6e 69 74 69 61 6c 69 7a 65 64 20 70 72 69 6f 72 nitialized prior
9df20 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 73 71 6c to invoking sql
9df30 69 74 65 52 65 61 6c 6c 6f 63 2e 0a 2a 2f 0a 53 iteRealloc..*/.S
9df40 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a QLITE_API void *
9df50 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 sqlite3_realloc(
9df60 76 6f 69 64 20 2a 70 4f 6c 64 2c 20 69 6e 74 20 void *pOld, int
9df70 6e 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 n){.#ifndef SQLI
9df80 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 TE_OMIT_AUTOINIT
9df90 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 . if( sqlite3_i
9dfa0 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65 nitialize() ) re
9dfb0 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 turn 0;.#endif.
9dfc0 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 52 return sqlite3R
9dfd0 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e 29 3b ealloc(pOld, n);
9dfe0 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 .}.../*.** Alloc
9dff0 61 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d 65 6d ate and zero mem
9e000 6f 72 79 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f ory..*/ .SQLITE_
9e010 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
9e020 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 lite3MallocZero(
9e030 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a int n){. void *
9e040 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f p = sqlite3Mallo
9e050 63 28 6e 29 3b 0a 20 20 69 66 28 20 70 20 29 7b c(n);. if( p ){
9e060 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 . memset(p, 0
9e070 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 , n);. }. retu
9e080 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 rn p;.}../*.** A
9e090 6c 6c 6f 63 61 74 65 20 61 6e 64 20 7a 65 72 6f llocate and zero
9e0a0 20 6d 65 6d 6f 72 79 2e 20 20 49 66 20 74 68 65 memory. If the
9e0b0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c allocation fail
9e0c0 73 2c 20 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6d s, make.** the m
9e0d0 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 allocFailed flag
9e0e0 20 69 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 in the connecti
9e0f0 6f 6e 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 on pointer..*/.S
9e100 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
9e110 69 64 20 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c id *sqlite3DbMal
9e120 6c 6f 63 5a 65 72 6f 28 73 71 6c 69 74 65 33 20 locZero(sqlite3
9e130 2a 64 62 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76 *db, int n){. v
9e140 6f 69 64 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 oid *p = sqlite3
9e150 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 DbMallocRaw(db,
9e160 6e 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 n);. if( p ){.
9e170 20 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 memset(p, 0,
9e180 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e n);. }. return
9e190 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c p;.}../*.** All
9e1a0 6f 63 61 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d ocate and zero m
9e1b0 65 6d 6f 72 79 2e 20 20 49 66 20 74 68 65 20 61 emory. If the a
9e1c0 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c llocation fails,
9e1d0 20 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6d 61 6c make.** the mal
9e1e0 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 69 locFailed flag i
9e1f0 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e n the connection
9e200 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 pointer..**.**
9e210 49 66 20 64 62 21 3d 30 20 61 6e 64 20 64 62 2d If db!=0 and db-
9e220 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 69 73 >mallocFailed is
9e230 20 74 72 75 65 20 28 69 6e 64 69 63 61 74 69 6e true (indicatin
9e240 67 20 61 20 70 72 69 6f 72 20 6d 61 6c 6c 6f 63 g a prior malloc
9e250 0a 2a 2a 20 66 61 69 6c 75 72 65 20 6f 6e 20 74 .** failure on t
9e260 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 he same database
9e270 20 63 6f 6e 6e 65 63 74 69 6f 6e 29 20 74 68 65 connection) the
9e280 6e 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 20 n always return
9e290 30 2e 0a 2a 2a 20 48 65 6e 63 65 20 66 6f 72 20 0..** Hence for
9e2a0 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 a particular dat
9e2b0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
9e2c0 2c 20 6f 6e 63 65 20 6d 61 6c 6c 6f 63 20 73 74 , once malloc st
9e2d0 61 72 74 73 0a 2a 2a 20 66 61 69 6c 69 6e 67 2c arts.** failing,
9e2e0 20 69 74 20 66 61 69 6c 73 20 63 6f 6e 73 69 73 it fails consis
9e2f0 74 65 6e 74 6c 79 20 75 6e 74 69 6c 20 6d 61 6c tently until mal
9e300 6c 6f 63 46 61 69 6c 65 64 20 69 73 20 72 65 73 locFailed is res
9e310 65 74 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 et..** This is a
9e320 6e 20 69 6d 70 6f 72 74 61 6e 74 20 61 73 73 75 n important assu
9e330 6d 70 74 69 6f 6e 2e 20 20 54 68 65 72 65 20 61 mption. There a
9e340 72 65 20 6d 61 6e 79 20 70 6c 61 63 65 73 20 69 re many places i
9e350 6e 20 74 68 65 0a 2a 2a 20 63 6f 64 65 20 74 68 n the.** code th
9e360 61 74 20 64 6f 20 74 68 69 6e 67 73 20 6c 69 6b at do things lik
9e370 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 e this:.**.**
9e380 20 20 20 20 20 20 69 6e 74 20 2a 61 20 3d 20 28 int *a = (
9e390 69 6e 74 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 int*)sqlite3DbMa
9e3a0 6c 6c 6f 63 52 61 77 28 64 62 2c 20 31 30 30 29 llocRaw(db, 100)
9e3b0 3b 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 74 ;.** int
9e3c0 20 2a 62 20 3d 20 28 69 6e 74 2a 29 73 71 6c 69 *b = (int*)sqli
9e3d0 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 te3DbMallocRaw(d
9e3e0 62 2c 20 32 30 30 29 3b 0a 2a 2a 20 20 20 20 20 b, 200);.**
9e3f0 20 20 20 20 69 66 28 20 62 20 29 20 61 5b 31 30 if( b ) a[10
9e400 5d 20 3d 20 39 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20 ] = 9;.**.** In
9e410 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20 other words, if
9e420 61 20 73 75 62 73 65 71 75 65 6e 74 20 6d 61 6c a subsequent mal
9e430 6c 6f 63 20 28 65 78 3a 20 22 62 22 29 20 77 6f loc (ex: "b") wo
9e440 72 6b 65 64 2c 20 69 74 20 69 73 20 61 73 73 75 rked, it is assu
9e450 6d 65 64 0a 2a 2a 20 74 68 61 74 20 61 6c 6c 20 med.** that all
9e460 70 72 69 6f 72 20 6d 61 6c 6c 6f 63 73 20 28 65 prior mallocs (e
9e470 78 3a 20 22 61 22 29 20 77 6f 72 6b 65 64 20 74 x: "a") worked t
9e480 6f 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 oo..*/.SQLITE_PR
9e490 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
9e4a0 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 73 te3DbMallocRaw(s
9e4b0 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 qlite3 *db, int
9e4c0 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 n){. void *p;.
9e4d0 20 61 73 73 65 72 74 28 20 64 62 3d 3d 30 20 7c assert( db==0 |
9e4e0 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f | sqlite3_mutex_
9e4f0 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 held(db->mutex)
9e500 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 );.#ifndef SQLIT
9e510 45 5f 4f 4d 49 54 5f 4c 4f 4f 4b 41 53 49 44 45 E_OMIT_LOOKASIDE
9e520 0a 20 20 69 66 28 20 64 62 20 29 7b 0a 20 20 20 . if( db ){.
9e530 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a LookasideSlot *
9e540 70 42 75 66 3b 0a 20 20 20 20 69 66 28 20 64 62 pBuf;. if( db
9e550 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
9e560 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 {. return 0
9e570 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
9e580 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 db->lookaside.bE
9e590 6e 61 62 6c 65 64 20 26 26 20 6e 3c 3d 64 62 2d nabled && n<=db-
9e5a0 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 0a 20 20 >lookaside.sz.
9e5b0 20 20 20 20 20 20 20 26 26 20 28 70 42 75 66 20 && (pBuf
9e5c0 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e = db->lookaside.
9e5d0 70 46 72 65 65 29 21 3d 30 20 29 7b 0a 20 20 20 pFree)!=0 ){.
9e5e0 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 db->lookaside
9e5f0 2e 70 46 72 65 65 20 3d 20 70 42 75 66 2d 3e 70 .pFree = pBuf->p
9e600 4e 65 78 74 3b 0a 20 20 20 20 20 20 64 62 2d 3e Next;. db->
9e610 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 2b 2b lookaside.nOut++
9e620 3b 0a 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e ;. if( db->
9e630 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3e 64 lookaside.nOut>d
9e640 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f b->lookaside.mxO
9e650 75 74 20 29 7b 0a 20 20 20 20 20 20 20 20 64 62 ut ){. db
9e660 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 ->lookaside.mxOu
9e670 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 t = db->lookasid
9e680 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 20 20 7d 0a e.nOut;. }.
9e690 20 20 20 20 20 20 72 65 74 75 72 6e 20 28 76 6f return (vo
9e6a0 69 64 2a 29 70 42 75 66 3b 0a 20 20 20 20 7d 0a id*)pBuf;. }.
9e6b0 20 20 7d 0a 23 65 6c 73 65 0a 20 20 69 66 28 20 }.#else. if(
9e6c0 64 62 20 26 26 20 64 62 2d 3e 6d 61 6c 6c 6f 63 db && db->malloc
9e6d0 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 Failed ){. re
9e6e0 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 23 65 6e 64 turn 0;. }.#end
9e6f0 69 66 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 if. p = sqlite3
9e700 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 69 66 28 Malloc(n);. if(
9e710 20 21 70 20 26 26 20 64 62 20 29 7b 0a 20 20 20 !p && db ){.
9e720 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
9e730 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 d = 1;. }. ret
9e740 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn p;.}../*.**
9e750 52 65 73 69 7a 65 20 74 68 65 20 62 6c 6f 63 6b Resize the block
9e760 20 6f 66 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 of memory point
9e770 65 64 20 74 6f 20 62 79 20 70 20 74 6f 20 6e 20 ed to by p to n
9e780 62 79 74 65 73 2e 20 49 66 20 74 68 65 0a 2a 2a bytes. If the.**
9e790 20 72 65 73 69 7a 65 20 66 61 69 6c 73 2c 20 73 resize fails, s
9e7a0 65 74 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69 et the mallocFai
9e7b0 6c 65 64 20 66 6c 61 67 20 69 6e 20 74 68 65 20 led flag in the
9e7c0 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62 6a 65 63 connection objec
9e7d0 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 t..*/.SQLITE_PRI
9e7e0 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
9e7f0 65 33 44 62 52 65 61 6c 6c 6f 63 28 73 71 6c 69 e3DbRealloc(sqli
9e800 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 te3 *db, void *p
9e810 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 , int n){. void
9e820 20 2a 70 4e 65 77 20 3d 20 30 3b 0a 20 20 61 73 *pNew = 0;. as
9e830 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a 20 sert( db!=0 );.
9e840 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
9e850 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e _mutex_held(db->
9e860 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 mutex) );. if(
9e870 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
9e880 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 ==0 ){. if( p
9e890 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 ==0 ){. ret
9e8a0 75 72 6e 20 73 71 6c 69 74 65 33 44 62 4d 61 6c urn sqlite3DbMal
9e8b0 6c 6f 63 52 61 77 28 64 62 2c 20 6e 29 3b 0a 20 locRaw(db, n);.
9e8c0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 73 4c }. if( isL
9e8d0 6f 6f 6b 61 73 69 64 65 28 64 62 2c 20 70 29 20 ookaside(db, p)
9e8e0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 3c 3d ){. if( n<=
9e8f0 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a db->lookaside.sz
9e900 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 ){. retu
9e910 72 6e 20 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 rn p;. }.
9e920 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 pNew = sqlit
9e930 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 e3DbMallocRaw(db
9e940 2c 20 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 , n);. if(
9e950 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 pNew ){.
9e960 6d 65 6d 63 70 79 28 70 4e 65 77 2c 20 70 2c 20 memcpy(pNew, p,
9e970 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a db->lookaside.sz
9e980 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
9e990 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b e3DbFree(db, p);
9e9a0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c . }. }el
9e9b0 73 65 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d se{. pNew =
9e9c0 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 sqlite3_realloc
9e9d0 28 70 2c 20 6e 29 3b 0a 20 20 20 20 20 20 69 66 (p, n);. if
9e9e0 28 20 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 ( !pNew ){.
9e9f0 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 db->mallocFai
9ea00 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d led = 1;. }
9ea10 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
9ea20 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a urn pNew;.}../*.
9ea30 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 72 65 ** Attempt to re
9ea40 61 6c 6c 6f 63 61 74 65 20 70 2e 20 20 49 66 20 allocate p. If
9ea50 74 68 65 20 72 65 61 6c 6c 6f 63 61 74 69 6f 6e the reallocation
9ea60 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 66 72 65 fails, then fre
9ea70 65 20 70 0a 2a 2a 20 61 6e 64 20 73 65 74 20 74 e p.** and set t
9ea80 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 he mallocFailed
9ea90 66 6c 61 67 20 69 6e 20 74 68 65 20 64 61 74 61 flag in the data
9eaa0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e base connection.
9eab0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
9eac0 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
9ead0 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 DbReallocOrFree(
9eae0 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 sqlite3 *db, voi
9eaf0 64 20 2a 70 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 d *p, int n){.
9eb00 76 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20 70 4e void *pNew;. pN
9eb10 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 ew = sqlite3DbRe
9eb20 61 6c 6c 6f 63 28 64 62 2c 20 70 2c 20 6e 29 3b alloc(db, p, n);
9eb30 0a 20 20 69 66 28 20 21 70 4e 65 77 20 29 7b 0a . if( !pNew ){.
9eb40 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
9eb50 65 28 64 62 2c 20 70 29 3b 0a 20 20 7d 0a 20 20 e(db, p);. }.
9eb60 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a return pNew;.}..
9eb70 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 63 6f 70 /*.** Make a cop
9eb80 79 20 6f 66 20 61 20 73 74 72 69 6e 67 20 69 6e y of a string in
9eb90 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 memory obtained
9eba0 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c from sqliteMall
9ebb0 6f 63 28 29 2e 20 54 68 65 73 65 20 0a 2a 2a 20 oc(). These .**
9ebc0 66 75 6e 63 74 69 6f 6e 73 20 63 61 6c 6c 20 73 functions call s
9ebd0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 52 61 77 28 qlite3MallocRaw(
9ebe0 29 20 64 69 72 65 63 74 6c 79 20 69 6e 73 74 65 ) directly inste
9ebf0 61 64 20 6f 66 20 73 71 6c 69 74 65 4d 61 6c 6c ad of sqliteMall
9ec00 6f 63 28 29 2e 20 54 68 69 73 0a 2a 2a 20 69 73 oc(). This.** is
9ec10 20 62 65 63 61 75 73 65 20 77 68 65 6e 20 6d 65 because when me
9ec20 6d 6f 72 79 20 64 65 62 75 67 67 69 6e 67 20 69 mory debugging i
9ec30 73 20 74 75 72 6e 65 64 20 6f 6e 2c 20 74 68 65 s turned on, the
9ec40 73 65 20 74 77 6f 20 66 75 6e 63 74 69 6f 6e 73 se two functions
9ec50 20 61 72 65 20 0a 2a 2a 20 63 61 6c 6c 65 64 20 are .** called
9ec60 76 69 61 20 6d 61 63 72 6f 73 20 74 68 61 74 20 via macros that
9ec70 72 65 63 6f 72 64 20 74 68 65 20 63 75 72 72 65 record the curre
9ec80 6e 74 20 66 69 6c 65 20 61 6e 64 20 6c 69 6e 65 nt file and line
9ec90 20 6e 75 6d 62 65 72 20 69 6e 20 74 68 65 0a 2a number in the.*
9eca0 2a 20 54 68 72 65 61 64 44 61 74 61 20 73 74 72 * ThreadData str
9ecb0 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 ucture..*/.SQLIT
9ecc0 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a E_PRIVATE char *
9ecd0 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 sqlite3DbStrDup(
9ece0 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e sqlite3 *db, con
9ecf0 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 63 st char *z){. c
9ed00 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 73 69 7a har *zNew;. siz
9ed10 65 5f 74 20 6e 3b 0a 20 20 69 66 28 20 7a 3d 3d e_t n;. if( z==
9ed20 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
9ed30 30 3b 0a 20 20 7d 0a 20 20 6e 20 3d 20 73 71 6c 0;. }. n = sql
9ed40 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 20 ite3Strlen30(z)
9ed50 2b 20 31 3b 0a 20 20 61 73 73 65 72 74 28 20 28 + 1;. assert( (
9ed60 6e 26 30 78 37 66 66 66 66 66 66 66 29 3d 3d 6e n&0x7fffffff)==n
9ed70 20 29 3b 0a 20 20 7a 4e 65 77 20 3d 20 73 71 6c );. zNew = sql
9ed80 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 ite3DbMallocRaw(
9ed90 64 62 2c 20 28 69 6e 74 29 6e 29 3b 0a 20 20 69 db, (int)n);. i
9eda0 66 28 20 7a 4e 65 77 20 29 7b 0a 20 20 20 20 6d f( zNew ){. m
9edb0 65 6d 63 70 79 28 7a 4e 65 77 2c 20 7a 2c 20 6e emcpy(zNew, z, n
9edc0 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
9edd0 7a 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 zNew;.}.SQLITE_P
9ede0 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c RIVATE char *sql
9edf0 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 73 71 ite3DbStrNDup(sq
9ee00 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 lite3 *db, const
9ee10 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 char *z, int n)
9ee20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a {. char *zNew;.
9ee30 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 if( z==0 ){.
9ee40 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
9ee50 20 20 61 73 73 65 72 74 28 20 28 6e 26 30 78 37 assert( (n&0x7
9ee60 66 66 66 66 66 66 66 29 3d 3d 6e 20 29 3b 0a 20 fffffff)==n );.
9ee70 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 zNew = sqlite3D
9ee80 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e bMallocRaw(db, n
9ee90 2b 31 29 3b 0a 20 20 69 66 28 20 7a 4e 65 77 20 +1);. if( zNew
9eea0 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 4e ){. memcpy(zN
9eeb0 65 77 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 7a ew, z, n);. z
9eec0 4e 65 77 5b 6e 5d 20 3d 20 30 3b 0a 20 20 7d 0a New[n] = 0;. }.
9eed0 20 20 72 65 74 75 72 6e 20 7a 4e 65 77 3b 0a 7d return zNew;.}
9eee0 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 ../*.** Create a
9eef0 20 73 74 72 69 6e 67 20 66 72 6f 6d 20 74 68 65 string from the
9ef00 20 7a 46 72 6f 6d 61 74 20 61 72 67 75 6d 65 6e zFromat argumen
9ef10 74 20 61 6e 64 20 74 68 65 20 76 61 5f 6c 69 73 t and the va_lis
9ef20 74 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2e 0a t that follows..
9ef30 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 73 74 72 ** Store the str
9ef40 69 6e 67 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 ing in memory ob
9ef50 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 tained from sqli
9ef60 74 65 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d teMalloc() and m
9ef70 61 6b 65 20 2a 70 7a 0a 2a 2a 20 70 6f 69 6e 74 ake *pz.** point
9ef80 20 74 6f 20 74 68 61 74 20 73 74 72 69 6e 67 2e to that string.
9ef90 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
9efa0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 TE void sqlite3S
9efb0 65 74 53 74 72 69 6e 67 28 63 68 61 72 20 2a 2a etString(char **
9efc0 70 7a 2c 20 73 71 6c 69 74 65 33 20 2a 64 62 2c pz, sqlite3 *db,
9efd0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f const char *zFo
9efe0 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 rmat, ...){. va
9eff0 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 _list ap;. char
9f000 20 2a 7a 3b 0a 0a 20 20 76 61 5f 73 74 61 72 74 *z;.. va_start
9f010 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 (ap, zFormat);.
9f020 20 7a 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 z = sqlite3VMPr
9f030 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 intf(db, zFormat
9f040 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 , ap);. va_end(
9f050 61 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 ap);. sqlite3Db
9f060 46 72 65 65 28 64 62 2c 20 2a 70 7a 29 3b 0a 20 Free(db, *pz);.
9f070 20 2a 70 7a 20 3d 20 7a 3b 0a 7d 0a 0a 0a 2f 2a *pz = z;.}.../*
9f080 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
9f090 6e 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 n must be called
9f0a0 20 62 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20 before exiting
9f0b0 61 6e 79 20 41 50 49 20 66 75 6e 63 74 69 6f 6e any API function
9f0c0 20 28 69 2e 65 2e 20 0a 2a 2a 20 72 65 74 75 72 (i.e. .** retur
9f0d0 6e 69 6e 67 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 ning control to
9f0e0 74 68 65 20 75 73 65 72 29 20 74 68 61 74 20 68 the user) that h
9f0f0 61 73 20 63 61 6c 6c 65 64 20 73 71 6c 69 74 65 as called sqlite
9f100 33 5f 6d 61 6c 6c 6f 63 20 6f 72 0a 2a 2a 20 73 3_malloc or.** s
9f110 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 2e 0a qlite3_realloc..
9f120 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e **.** The return
9f130 65 64 20 76 61 6c 75 65 20 69 73 20 6e 6f 72 6d ed value is norm
9f140 61 6c 6c 79 20 61 20 63 6f 70 79 20 6f 66 20 74 ally a copy of t
9f150 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
9f160 6e 74 20 74 6f 20 74 68 69 73 0a 2a 2a 20 66 75 nt to this.** fu
9f170 6e 63 74 69 6f 6e 2e 20 48 6f 77 65 76 65 72 2c nction. However,
9f180 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 if a malloc() f
9f190 61 69 6c 75 72 65 20 68 61 73 20 6f 63 63 75 72 ailure has occur
9f1a0 72 65 64 20 73 69 6e 63 65 20 74 68 65 20 70 72 red since the pr
9f1b0 65 76 69 6f 75 73 0a 2a 2a 20 69 6e 76 6f 63 61 evious.** invoca
9f1c0 74 69 6f 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 tion SQLITE_NOME
9f1d0 4d 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e M is returned in
9f1e0 73 74 65 61 64 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 stead. .**.** If
9f1f0 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d the first argum
9f200 65 6e 74 2c 20 64 62 2c 20 69 73 20 6e 6f 74 20 ent, db, is not
9f210 4e 55 4c 4c 20 61 6e 64 20 61 20 6d 61 6c 6c 6f NULL and a mallo
9f220 63 28 29 20 65 72 72 6f 72 20 68 61 73 20 6f 63 c() error has oc
9f230 63 75 72 72 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 curred,.** then
9f240 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 65 the connection e
9f250 72 72 6f 72 2d 63 6f 64 65 20 28 74 68 65 20 76 rror-code (the v
9f260 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 alue returned by
9f270 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 sqlite3_errcode
9f280 28 29 29 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f ()).** is set to
9f290 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a SQLITE_NOMEM..*
9f2a0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
9f2b0 20 69 6e 74 20 73 71 6c 69 74 65 33 41 70 69 45 int sqlite3ApiE
9f2c0 78 69 74 28 73 71 6c 69 74 65 33 2a 20 64 62 2c xit(sqlite3* db,
9f2d0 20 69 6e 74 20 72 63 29 7b 0a 20 20 2f 2a 20 49 int rc){. /* I
9f2e0 66 20 74 68 65 20 64 62 20 68 61 6e 64 6c 65 20 f the db handle
9f2f0 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 is not NULL, the
9f300 6e 20 77 65 20 6d 75 73 74 20 68 6f 6c 64 20 74 n we must hold t
9f310 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 he connection ha
9f320 6e 64 6c 65 0a 20 20 2a 2a 20 6d 75 74 65 78 20 ndle. ** mutex
9f330 68 65 72 65 2e 20 4f 74 68 65 72 77 69 73 65 20 here. Otherwise
9f340 74 68 65 20 72 65 61 64 20 28 61 6e 64 20 70 6f the read (and po
9f350 73 73 69 62 6c 65 20 77 72 69 74 65 29 20 6f 66 ssible write) of
9f360 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
9f370 64 20 0a 20 20 2a 2a 20 69 73 20 75 6e 73 61 66 d . ** is unsaf
9f380 65 2c 20 61 73 20 69 73 20 74 68 65 20 63 61 6c e, as is the cal
9f390 6c 20 74 6f 20 73 71 6c 69 74 65 33 45 72 72 6f l to sqlite3Erro
9f3a0 72 28 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 r().. */. asse
9f3b0 72 74 28 20 21 64 62 20 7c 7c 20 73 71 6c 69 74 rt( !db || sqlit
9f3c0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 e3_mutex_held(db
9f3d0 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 ->mutex) );. if
9f3e0 28 20 64 62 20 26 26 20 28 64 62 2d 3e 6d 61 6c ( db && (db->mal
9f3f0 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 72 63 3d locFailed || rc=
9f400 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f =SQLITE_IOERR_NO
9f410 4d 45 4d 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 MEM) ){. sqli
9f420 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c te3Error(db, SQL
9f430 49 54 45 5f 4e 4f 4d 45 4d 2c 20 30 29 3b 0a 20 ITE_NOMEM, 0);.
9f440 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 db->mallocFai
9f450 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 led = 0;. rc
9f460 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a = SQLITE_NOMEM;.
9f470 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20 }. return rc
9f480 26 20 28 64 62 20 3f 20 64 62 2d 3e 65 72 72 4d & (db ? db->errM
9f490 61 73 6b 20 3a 20 30 78 66 66 29 3b 0a 7d 0a 0a ask : 0xff);.}..
9f4a0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
9f4b0 45 6e 64 20 6f 66 20 6d 61 6c 6c 6f 63 2e 63 20 End of malloc.c
9f4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
9f4f0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
9f500 42 65 67 69 6e 20 66 69 6c 65 20 70 72 69 6e 74 Begin file print
9f510 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a f.c ************
9f520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
9f540 2f 2a 0a 2a 2a 20 54 68 65 20 22 70 72 69 6e 74 /*.** The "print
9f550 66 22 20 63 6f 64 65 20 74 68 61 74 20 66 6f 6c f" code that fol
9f560 6c 6f 77 73 20 64 61 74 65 73 20 66 72 6f 6d 20 lows dates from
9f570 74 68 65 20 31 39 38 30 27 73 2e 20 20 49 74 20 the 1980's. It
9f580 69 73 20 69 6e 0a 2a 2a 20 74 68 65 20 70 75 62 is in.** the pub
9f590 6c 69 63 20 64 6f 6d 61 69 6e 2e 20 20 54 68 65 lic domain. The
9f5a0 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6d 6d 65 6e original commen
9f5b0 74 73 20 61 72 65 20 69 6e 63 6c 75 64 65 64 20 ts are included
9f5c0 68 65 72 65 20 66 6f 72 0a 2a 2a 20 63 6f 6d 70 here for.** comp
9f5d0 6c 65 74 65 6e 65 73 73 2e 20 20 54 68 65 79 20 leteness. They
9f5e0 61 72 65 20 76 65 72 79 20 6f 75 74 2d 6f 66 2d are very out-of-
9f5f0 64 61 74 65 20 62 75 74 20 6d 69 67 68 74 20 62 date but might b
9f600 65 20 75 73 65 66 75 6c 20 61 73 0a 2a 2a 20 61 e useful as.** a
9f610 6e 20 68 69 73 74 6f 72 69 63 61 6c 20 72 65 66 n historical ref
9f620 65 72 65 6e 63 65 2e 20 20 4d 6f 73 74 20 6f 66 erence. Most of
9f630 20 74 68 65 20 22 65 6e 68 61 6e 63 65 6d 65 6e the "enhancemen
9f640 74 73 22 20 68 61 76 65 20 62 65 65 6e 20 62 61 ts" have been ba
9f650 63 6b 65 64 0a 2a 2a 20 6f 75 74 20 73 6f 20 74 cked.** out so t
9f660 68 61 74 20 74 68 65 20 66 75 6e 63 74 69 6f 6e hat the function
9f670 61 6c 69 74 79 20 69 73 20 6e 6f 77 20 74 68 65 ality is now the
9f680 20 73 61 6d 65 20 61 73 20 73 74 61 6e 64 61 72 same as standar
9f690 64 20 70 72 69 6e 74 66 28 29 2e 0a 2a 2a 0a 2a d printf()..**.*
9f6a0 2a 20 24 49 64 3a 20 70 72 69 6e 74 66 2e 63 2c * $Id: printf.c,
9f6b0 76 20 31 2e 31 30 34 20 32 30 30 39 2f 30 36 2f v 1.104 2009/06/
9f6c0 30 33 20 30 31 3a 32 34 3a 35 34 20 64 72 68 20 03 01:24:54 drh
9f6d0 45 78 70 20 24 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a Exp $.**.*******
9f6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f6f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f720 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f ***.**.** The fo
9f730 6c 6c 6f 77 69 6e 67 20 6d 6f 64 75 6c 65 73 20 llowing modules
9f740 69 73 20 61 6e 20 65 6e 68 61 6e 63 65 64 20 72 is an enhanced r
9f750 65 70 6c 61 63 65 6d 65 6e 74 20 66 6f 72 20 74 eplacement for t
9f760 68 65 20 22 70 72 69 6e 74 66 22 20 73 75 62 72 he "printf" subr
9f770 6f 75 74 69 6e 65 73 0a 2a 2a 20 66 6f 75 6e 64 outines.** found
9f780 20 69 6e 20 74 68 65 20 73 74 61 6e 64 61 72 64 in the standard
9f790 20 43 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65 C library. The
9f7a0 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 6e 68 61 6e following enhan
9f7b0 63 65 6d 65 6e 74 73 20 61 72 65 0a 2a 2a 20 73 cements are.** s
9f7c0 75 70 70 6f 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 upported:.**.**
9f7d0 20 20 20 20 20 2b 20 20 41 64 64 69 74 69 6f 6e + Addition
9f7e0 61 6c 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 al functions. T
9f7f0 68 65 20 73 74 61 6e 64 61 72 64 20 73 65 74 20 he standard set
9f800 6f 66 20 22 70 72 69 6e 74 66 22 20 66 75 6e 63 of "printf" func
9f810 74 69 6f 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 tions.**
9f820 20 69 6e 63 6c 75 64 65 73 20 70 72 69 6e 74 66 includes printf
9f830 2c 20 66 70 72 69 6e 74 66 2c 20 73 70 72 69 6e , fprintf, sprin
9f840 74 66 2c 20 76 70 72 69 6e 74 66 2c 20 76 66 70 tf, vprintf, vfp
9f850 72 69 6e 74 66 2c 20 61 6e 64 0a 2a 2a 20 20 20 rintf, and.**
9f860 20 20 20 20 20 20 76 73 70 72 69 6e 74 66 2e 20 vsprintf.
9f870 20 54 68 69 73 20 6d 6f 64 75 6c 65 20 61 64 64 This module add
9f880 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a s the following:
9f890 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 .**.**
9f8a0 20 2a 20 20 73 6e 70 72 69 6e 74 66 20 2d 2d 20 * snprintf --
9f8b0 57 6f 72 6b 73 20 6c 69 6b 65 20 73 70 72 69 6e Works like sprin
9f8c0 74 66 2c 20 62 75 74 20 68 61 73 20 61 6e 20 65 tf, but has an e
9f8d0 78 74 72 61 20 61 72 67 75 6d 65 6e 74 0a 2a 2a xtra argument.**
9f8e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9f8f0 20 20 20 20 20 20 20 20 20 20 77 68 69 63 68 20 which
9f900 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 is the size of t
9f910 68 65 20 62 75 66 66 65 72 20 77 72 69 74 74 65 he buffer writte
9f920 6e 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 n to..**.**
9f930 20 20 20 20 20 20 2a 20 20 6d 70 72 69 6e 74 66 * mprintf
9f940 20 2d 2d 20 20 53 69 6d 69 6c 61 72 20 74 6f 20 -- Similar to
9f950 73 70 72 69 6e 74 66 2e 20 20 57 72 69 74 65 73 sprintf. Writes
9f960 20 6f 75 74 70 75 74 20 74 6f 20 6d 65 6d 6f 72 output to memor
9f970 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 y.**
9f980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 62 ob
9f990 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c tained from mall
9f9a0 6f 63 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 oc..**.**
9f9b0 20 20 20 20 2a 20 20 78 70 72 69 6e 74 66 20 2d * xprintf -
9f9c0 2d 20 20 43 61 6c 6c 73 20 61 20 66 75 6e 63 74 - Calls a funct
9f9d0 69 6f 6e 20 74 6f 20 64 69 73 70 6f 73 65 20 6f ion to dispose o
9f9e0 66 20 6f 75 74 70 75 74 2e 0a 2a 2a 0a 2a 2a 20 f output..**.**
9f9f0 20 20 20 20 20 20 20 20 20 20 2a 20 20 6e 70 72 * npr
9fa00 69 6e 74 66 20 2d 2d 20 20 4e 6f 20 6f 75 74 70 intf -- No outp
9fa10 75 74 2c 20 62 75 74 20 72 65 74 75 72 6e 73 20 ut, but returns
9fa20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 the number of ch
9fa30 61 72 61 63 74 65 72 73 0a 2a 2a 20 20 20 20 20 aracters.**
9fa40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9fa50 20 20 20 20 20 74 68 61 74 20 77 6f 75 6c 64 20 that would
9fa60 68 61 76 65 20 62 65 65 6e 20 6f 75 74 70 75 74 have been output
9fa70 20 62 79 20 70 72 69 6e 74 66 2e 0a 2a 2a 0a 2a by printf..**.*
9fa80 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 41 * * A
9fa90 20 76 2d 20 76 65 72 73 69 6f 6e 20 28 65 78 3a v- version (ex:
9faa0 20 76 73 6e 70 72 69 6e 74 66 29 20 6f 66 20 65 vsnprintf) of e
9fab0 76 65 72 79 20 66 75 6e 63 74 69 6f 6e 20 69 73 very function is
9fac0 20 61 6c 73 6f 0a 2a 2a 20 20 20 20 20 20 20 20 also.**
9fad0 20 20 20 20 20 20 73 75 70 70 6c 69 65 64 2e 0a supplied..
9fae0 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 20 41 20 **.** + A
9faf0 66 65 77 20 65 78 74 65 6e 73 69 6f 6e 73 20 74 few extensions t
9fb00 6f 20 74 68 65 20 66 6f 72 6d 61 74 74 69 6e 67 o the formatting
9fb10 20 6e 6f 74 61 74 69 6f 6e 20 61 72 65 20 73 75 notation are su
9fb20 70 70 6f 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 pported:.**.**
9fb30 20 20 20 20 20 20 20 20 20 2a 20 20 54 68 65 20 * The
9fb40 22 3d 22 20 66 6c 61 67 20 28 73 69 6d 69 6c 61 "=" flag (simila
9fb50 72 20 74 6f 20 22 2d 22 29 20 63 61 75 73 65 73 r to "-") causes
9fb60 20 74 68 65 20 6f 75 74 70 75 74 20 74 6f 20 62 the output to b
9fb70 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 e.**
9fb80 20 20 62 65 20 63 65 6e 74 65 72 65 64 20 69 6e be centered in
9fb90 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 the appropriate
9fba0 6c 79 20 73 69 7a 65 64 20 66 69 65 6c 64 2e 0a ly sized field..
9fbb0 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 **.**
9fbc0 2a 20 20 54 68 65 20 25 62 20 66 69 65 6c 64 20 * The %b field
9fbd0 6f 75 74 70 75 74 73 20 61 6e 20 69 6e 74 65 67 outputs an integ
9fbe0 65 72 20 69 6e 20 62 69 6e 61 72 79 20 6e 6f 74 er in binary not
9fbf0 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 ation..**.**
9fc00 20 20 20 20 20 20 20 2a 20 20 54 68 65 20 25 63 * The %c
9fc10 20 66 69 65 6c 64 20 6e 6f 77 20 61 63 63 65 70 field now accep
9fc20 74 73 20 61 20 70 72 65 63 69 73 69 6f 6e 2e 20 ts a precision.
9fc30 20 54 68 65 20 63 68 61 72 61 63 74 65 72 20 6f The character o
9fc40 75 74 70 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 utput.**
9fc50 20 20 20 20 20 20 69 73 20 72 65 70 65 61 74 65 is repeate
9fc60 64 20 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20 d by the number
9fc70 6f 66 20 74 69 6d 65 73 20 74 68 65 20 70 72 65 of times the pre
9fc80 63 69 73 69 6f 6e 20 73 70 65 63 69 66 69 65 73 cision specifies
9fc90 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 ..**.**
9fca0 20 20 2a 20 20 54 68 65 20 25 27 20 66 69 65 6c * The %' fiel
9fcb0 64 20 77 6f 72 6b 73 20 6c 69 6b 65 20 25 63 2c d works like %c,
9fcc0 20 62 75 74 20 74 61 6b 65 73 20 61 73 20 69 74 but takes as it
9fcd0 73 20 63 68 61 72 61 63 74 65 72 20 74 68 65 0a s character the.
9fce0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
9fcf0 6e 65 78 74 20 63 68 61 72 61 63 74 65 72 20 6f next character o
9fd00 66 20 74 68 65 20 66 6f 72 6d 61 74 20 73 74 72 f the format str
9fd10 69 6e 67 2c 20 69 6e 73 74 65 61 64 20 6f 66 20 ing, instead of
9fd20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 20 20 20 20 the next.**
9fd30 20 20 20 20 20 20 20 20 20 61 72 67 75 6d 65 6e argumen
9fd40 74 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c t. For example,
9fd50 20 20 70 72 69 6e 74 66 28 22 25 2e 37 38 27 2d printf("%.78'-
9fd60 22 29 20 20 70 72 69 6e 74 73 20 37 38 20 6d 69 ") prints 78 mi
9fd70 6e 75 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 nus.**
9fd80 20 20 20 20 73 69 67 6e 73 2c 20 74 68 65 20 73 signs, the s
9fd90 61 6d 65 20 61 73 20 20 70 72 69 6e 74 66 28 22 ame as printf("
9fda0 25 2e 37 38 63 22 2c 27 2d 27 29 2e 0a 2a 2a 0a %.78c",'-')..**.
9fdb0 2a 2a 20 20 20 20 20 20 2b 20 20 57 68 65 6e 20 ** + When
9fdc0 63 6f 6d 70 69 6c 65 64 20 75 73 69 6e 67 20 47 compiled using G
9fdd0 43 43 20 6f 6e 20 61 20 53 50 41 52 43 2c 20 74 CC on a SPARC, t
9fde0 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 his version of p
9fdf0 72 69 6e 74 66 20 69 73 0a 2a 2a 20 20 20 20 20 rintf is.**
9fe00 20 20 20 20 66 61 73 74 65 72 20 74 68 61 6e 20 faster than
9fe10 74 68 65 20 6c 69 62 72 61 72 79 20 70 72 69 6e the library prin
9fe20 74 66 20 66 6f 72 20 53 55 4e 20 4f 53 20 34 2e tf for SUN OS 4.
9fe30 31 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 1..**.** +
9fe40 20 41 6c 6c 20 66 75 6e 63 74 69 6f 6e 73 20 61 All functions a
9fe50 72 65 20 66 75 6c 6c 79 20 72 65 65 6e 74 72 61 re fully reentra
9fe60 6e 74 2e 0a 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a nt..**.*/../*.**
9fe70 20 43 6f 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 Conversion type
9fe80 73 20 66 61 6c 6c 20 69 6e 74 6f 20 76 61 72 69 s fall into vari
9fe90 6f 75 73 20 63 61 74 65 67 6f 72 69 65 73 20 61 ous categories a
9fea0 73 20 64 65 66 69 6e 65 64 20 62 79 20 74 68 65 s defined by the
9feb0 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 6e .** following en
9fec0 75 6d 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 umeration..*/.#d
9fed0 65 66 69 6e 65 20 65 74 52 41 44 49 58 20 20 20 efine etRADIX
9fee0 20 20 20 20 31 20 2f 2a 20 49 6e 74 65 67 65 72 1 /* Integer
9fef0 20 74 79 70 65 73 2e 20 20 25 64 2c 20 25 78 2c types. %d, %x,
9ff00 20 25 6f 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 %o, and so fort
9ff10 68 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 46 h */.#define etF
9ff20 4c 4f 41 54 20 20 20 20 20 20 20 32 20 2f 2a 20 LOAT 2 /*
9ff30 46 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 2e 20 Floating point.
9ff40 20 25 66 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 %f */.#define e
9ff50 74 45 58 50 20 20 20 20 20 20 20 20 20 33 20 2f tEXP 3 /
9ff60 2a 20 45 78 70 6f 6e 65 6e 74 69 6f 6e 61 6c 20 * Exponentional
9ff70 6e 6f 74 61 74 69 6f 6e 2e 20 25 65 20 61 6e 64 notation. %e and
9ff80 20 25 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 %E */.#define e
9ff90 74 47 45 4e 45 52 49 43 20 20 20 20 20 34 20 2f tGENERIC 4 /
9ffa0 2a 20 46 6c 6f 61 74 69 6e 67 20 6f 72 20 65 78 * Floating or ex
9ffb0 70 6f 6e 65 6e 74 69 61 6c 2c 20 64 65 70 65 6e ponential, depen
9ffc0 64 69 6e 67 20 6f 6e 20 65 78 70 6f 6e 65 6e 74 ding on exponent
9ffd0 2e 20 25 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 . %g */.#define
9ffe0 65 74 53 49 5a 45 20 20 20 20 20 20 20 20 35 20 etSIZE 5
9fff0 2f 2a 20 52 65 74 75 72 6e 20 6e 75 6d 62 65 72 /* Return number
a0000 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 70 of characters p
a0010 72 6f 63 65 73 73 65 64 20 73 6f 20 66 61 72 2e rocessed so far.
a0020 20 25 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 %n */.#define e
a0030 74 53 54 52 49 4e 47 20 20 20 20 20 20 36 20 2f tSTRING 6 /
a0040 2a 20 53 74 72 69 6e 67 73 2e 20 25 73 20 2a 2f * Strings. %s */
a0050 0a 23 64 65 66 69 6e 65 20 65 74 44 59 4e 53 54 .#define etDYNST
a0060 52 49 4e 47 20 20 20 37 20 2f 2a 20 44 79 6e 61 RING 7 /* Dyna
a0070 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 mically allocate
a0080 64 20 73 74 72 69 6e 67 73 2e 20 25 7a 20 2a 2f d strings. %z */
a0090 0a 23 64 65 66 69 6e 65 20 65 74 50 45 52 43 45 .#define etPERCE
a00a0 4e 54 20 20 20 20 20 38 20 2f 2a 20 50 65 72 63 NT 8 /* Perc
a00b0 65 6e 74 20 73 79 6d 62 6f 6c 2e 20 25 25 20 2a ent symbol. %% *
a00c0 2f 0a 23 64 65 66 69 6e 65 20 65 74 43 48 41 52 /.#define etCHAR
a00d0 58 20 20 20 20 20 20 20 39 20 2f 2a 20 43 68 61 X 9 /* Cha
a00e0 72 61 63 74 65 72 73 2e 20 25 63 20 2a 2f 0a 2f racters. %c */./
a00f0 2a 20 54 68 65 20 72 65 73 74 20 61 72 65 20 65 * The rest are e
a0100 78 74 65 6e 73 69 6f 6e 73 2c 20 6e 6f 74 20 6e xtensions, not n
a0110 6f 72 6d 61 6c 6c 79 20 66 6f 75 6e 64 20 69 6e ormally found in
a0120 20 70 72 69 6e 74 66 28 29 20 2a 2f 0a 23 64 65 printf() */.#de
a0130 66 69 6e 65 20 65 74 53 51 4c 45 53 43 41 50 45 fine etSQLESCAPE
a0140 20 20 31 30 20 2f 2a 20 53 74 72 69 6e 67 73 20 10 /* Strings
a0150 77 69 74 68 20 27 5c 27 27 20 64 6f 75 62 6c 65 with '\'' double
a0160 64 2e 20 20 25 71 20 2a 2f 0a 23 64 65 66 69 6e d. %q */.#defin
a0170 65 20 65 74 53 51 4c 45 53 43 41 50 45 32 20 31 e etSQLESCAPE2 1
a0180 31 20 2f 2a 20 53 74 72 69 6e 67 73 20 77 69 74 1 /* Strings wit
a0190 68 20 27 5c 27 27 20 64 6f 75 62 6c 65 64 20 61 h '\'' doubled a
a01a0 6e 64 20 65 6e 63 6c 6f 73 65 64 20 69 6e 20 27 nd enclosed in '
a01b0 27 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ',.
a01c0 20 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c NUL
a01d0 4c 20 70 6f 69 6e 74 65 72 73 20 72 65 70 6c 61 L pointers repla
a01e0 63 65 64 20 62 79 20 53 51 4c 20 4e 55 4c 4c 2e ced by SQL NULL.
a01f0 20 20 25 51 20 2a 2f 0a 23 64 65 66 69 6e 65 20 %Q */.#define
a0200 65 74 54 4f 4b 45 4e 20 20 20 20 20 20 31 32 20 etTOKEN 12
a0210 2f 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 /* a pointer to
a0220 61 20 54 6f 6b 65 6e 20 73 74 72 75 63 74 75 72 a Token structur
a0230 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 e */.#define etS
a0240 52 43 4c 49 53 54 20 20 20 20 31 33 20 2f 2a 20 RCLIST 13 /*
a0250 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 53 a pointer to a S
a0260 72 63 4c 69 73 74 20 2a 2f 0a 23 64 65 66 69 6e rcList */.#defin
a0270 65 20 65 74 50 4f 49 4e 54 45 52 20 20 20 20 31 e etPOINTER 1
a0280 34 20 2f 2a 20 54 68 65 20 25 70 20 63 6f 6e 76 4 /* The %p conv
a0290 65 72 73 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e ersion */.#defin
a02a0 65 20 65 74 53 51 4c 45 53 43 41 50 45 33 20 31 e etSQLESCAPE3 1
a02b0 35 20 2f 2a 20 25 77 20 2d 3e 20 53 74 72 69 6e 5 /* %w -> Strin
a02c0 67 73 20 77 69 74 68 20 27 5c 22 27 20 64 6f 75 gs with '\"' dou
a02d0 62 6c 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 bled */.#define
a02e0 65 74 4f 52 44 49 4e 41 4c 20 20 20 20 31 36 20 etORDINAL 16
a02f0 2f 2a 20 25 72 20 2d 3e 20 31 73 74 2c 20 32 6e /* %r -> 1st, 2n
a0300 64 2c 20 33 72 64 2c 20 34 74 68 2c 20 65 74 63 d, 3rd, 4th, etc
a0310 2e 20 20 45 6e 67 6c 69 73 68 20 6f 6e 6c 79 20 . English only
a0320 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 65 74 49 4e */..#define etIN
a0330 56 41 4c 49 44 20 20 20 20 20 30 20 2f 2a 20 41 VALID 0 /* A
a0340 6e 79 20 75 6e 72 65 63 6f 67 6e 69 7a 65 64 20 ny unrecognized
a0350 63 6f 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 20 conversion type
a0360 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 22 65 */.../*.** An "e
a0370 74 42 79 74 65 22 20 69 73 20 61 6e 20 38 2d 62 tByte" is an 8-b
a0380 69 74 20 75 6e 73 69 67 6e 65 64 20 76 61 6c 75 it unsigned valu
a0390 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 75 6e e..*/.typedef un
a03a0 73 69 67 6e 65 64 20 63 68 61 72 20 65 74 42 79 signed char etBy
a03b0 74 65 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 te;../*.** Each
a03c0 62 75 69 6c 74 69 6e 20 63 6f 6e 76 65 72 73 69 builtin conversi
a03d0 6f 6e 20 63 68 61 72 61 63 74 65 72 20 28 65 78 on character (ex
a03e0 3a 20 74 68 65 20 27 64 27 20 69 6e 20 22 25 64 : the 'd' in "%d
a03f0 22 29 20 69 73 20 64 65 73 63 72 69 62 65 64 0a ") is described.
a0400 2a 2a 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 ** by an instanc
a0410 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
a0420 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2f 0a ng structure.*/.
a0430 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 65 typedef struct e
a0440 74 5f 69 6e 66 6f 20 7b 20 20 20 2f 2a 20 49 6e t_info { /* In
a0450 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 formation about
a0460 65 61 63 68 20 66 6f 72 6d 61 74 20 66 69 65 6c each format fiel
a0470 64 20 2a 2f 0a 20 20 63 68 61 72 20 66 6d 74 74 d */. char fmtt
a0480 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ype;
a0490 2f 2a 20 54 68 65 20 66 6f 72 6d 61 74 20 66 69 /* The format fi
a04a0 65 6c 64 20 63 6f 64 65 20 6c 65 74 74 65 72 20 eld code letter
a04b0 2a 2f 0a 20 20 65 74 42 79 74 65 20 62 61 73 65 */. etByte base
a04c0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
a04d0 20 54 68 65 20 62 61 73 65 20 66 6f 72 20 72 61 The base for ra
a04e0 64 69 78 20 63 6f 6e 76 65 72 73 69 6f 6e 20 2a dix conversion *
a04f0 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 73 /. etByte flags
a0500 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
a0510 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 46 One or more of F
a0520 4c 41 47 5f 20 63 6f 6e 73 74 61 6e 74 73 20 62 LAG_ constants b
a0530 65 6c 6f 77 20 2a 2f 0a 20 20 65 74 42 79 74 65 elow */. etByte
a0540 20 74 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 type;
a0550 20 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69 6f 6e /* Conversion
a0560 20 70 61 72 61 64 69 67 6d 20 2a 2f 0a 20 20 65 paradigm */. e
a0570 74 42 79 74 65 20 63 68 61 72 73 65 74 3b 20 20 tByte charset;
a0580 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 /* Offse
a0590 74 20 69 6e 74 6f 20 61 44 69 67 69 74 73 5b 5d t into aDigits[]
a05a0 20 6f 66 20 74 68 65 20 64 69 67 69 74 73 20 73 of the digits s
a05b0 74 72 69 6e 67 20 2a 2f 0a 20 20 65 74 42 79 74 tring */. etByt
a05c0 65 20 70 72 65 66 69 78 3b 20 20 20 20 20 20 20 e prefix;
a05d0 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e /* Offset in
a05e0 74 6f 20 61 50 72 65 66 69 78 5b 5d 20 6f 66 20 to aPrefix[] of
a05f0 74 68 65 20 70 72 65 66 69 78 20 73 74 72 69 6e the prefix strin
a0600 67 20 2a 2f 0a 7d 20 65 74 5f 69 6e 66 6f 3b 0a g */.} et_info;.
a0610 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 ./*.** Allowed v
a0620 61 6c 75 65 73 20 66 6f 72 20 65 74 5f 69 6e 66 alues for et_inf
a0630 6f 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69 o.flags.*/.#defi
a0640 6e 65 20 46 4c 41 47 5f 53 49 47 4e 45 44 20 20 ne FLAG_SIGNED
a0650 31 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 1 /* True if
a0660 20 74 68 65 20 76 61 6c 75 65 20 74 6f 20 63 6f the value to co
a0670 6e 76 65 72 74 20 69 73 20 73 69 67 6e 65 64 20 nvert is signed
a0680 2a 2f 0a 23 64 65 66 69 6e 65 20 46 4c 41 47 5f */.#define FLAG_
a0690 49 4e 54 45 52 4e 20 20 32 20 20 20 20 20 2f 2a INTERN 2 /*
a06a0 20 54 72 75 65 20 69 66 20 66 6f 72 20 69 6e 74 True if for int
a06b0 65 72 6e 61 6c 20 75 73 65 20 6f 6e 6c 79 20 2a ernal use only *
a06c0 2f 0a 23 64 65 66 69 6e 65 20 46 4c 41 47 5f 53 /.#define FLAG_S
a06d0 54 52 49 4e 47 20 20 34 20 20 20 20 20 2f 2a 20 TRING 4 /*
a06e0 41 6c 6c 6f 77 20 69 6e 66 69 6e 69 74 79 20 70 Allow infinity p
a06f0 72 65 63 69 73 69 6f 6e 20 2a 2f 0a 0a 0a 2f 2a recision */.../*
a0700 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
a0710 67 20 74 61 62 6c 65 20 69 73 20 73 65 61 72 63 g table is searc
a0720 68 65 64 20 6c 69 6e 65 61 72 6c 79 2c 20 73 6f hed linearly, so
a0730 20 69 74 20 69 73 20 67 6f 6f 64 20 74 6f 20 70 it is good to p
a0740 75 74 20 74 68 65 0a 2a 2a 20 6d 6f 73 74 20 66 ut the.** most f
a0750 72 65 71 75 65 6e 74 6c 79 20 75 73 65 64 20 63 requently used c
a0760 6f 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 73 20 onversion types
a0770 66 69 72 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 first..*/.static
a0780 20 63 6f 6e 73 74 20 63 68 61 72 20 61 44 69 67 const char aDig
a0790 69 74 73 5b 5d 20 3d 20 22 30 31 32 33 34 35 36 its[] = "0123456
a07a0 37 38 39 41 42 43 44 45 46 30 31 32 33 34 35 36 789ABCDEF0123456
a07b0 37 38 39 61 62 63 64 65 66 22 3b 0a 73 74 61 74 789abcdef";.stat
a07c0 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 61 50 ic const char aP
a07d0 72 65 66 69 78 5b 5d 20 3d 20 22 2d 78 30 5c 30 refix[] = "-x0\0
a07e0 30 30 58 30 22 3b 0a 73 74 61 74 69 63 20 63 6f 00X0";.static co
a07f0 6e 73 74 20 65 74 5f 69 6e 66 6f 20 66 6d 74 69 nst et_info fmti
a0800 6e 66 6f 5b 5d 20 3d 20 7b 0a 20 20 7b 20 20 27 nfo[] = {. { '
a0810 64 27 2c 20 31 30 2c 20 31 2c 20 65 74 52 41 44 d', 10, 1, etRAD
a0820 49 58 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d IX, 0, 0 }
a0830 2c 0a 20 20 7b 20 20 27 73 27 2c 20 20 30 2c 20 ,. { 's', 0,
a0840 34 2c 20 65 74 53 54 52 49 4e 47 2c 20 20 20 20 4, etSTRING,
a0850 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 0, 0 },. { '
a0860 67 27 2c 20 20 30 2c 20 31 2c 20 65 74 47 45 4e g', 0, 1, etGEN
a0870 45 52 49 43 2c 20 20 20 20 33 30 2c 20 30 20 7d ERIC, 30, 0 }
a0880 2c 0a 20 20 7b 20 20 27 7a 27 2c 20 20 30 2c 20 ,. { 'z', 0,
a0890 34 2c 20 65 74 44 59 4e 53 54 52 49 4e 47 2c 20 4, etDYNSTRING,
a08a0 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 0, 0 },. { '
a08b0 71 27 2c 20 20 30 2c 20 34 2c 20 65 74 53 51 4c q', 0, 4, etSQL
a08c0 45 53 43 41 50 45 2c 20 20 30 2c 20 20 30 20 7d ESCAPE, 0, 0 }
a08d0 2c 0a 20 20 7b 20 20 27 51 27 2c 20 20 30 2c 20 ,. { 'Q', 0,
a08e0 34 2c 20 65 74 53 51 4c 45 53 43 41 50 45 32 2c 4, etSQLESCAPE2,
a08f0 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 0, 0 },. { '
a0900 77 27 2c 20 20 30 2c 20 34 2c 20 65 74 53 51 4c w', 0, 4, etSQL
a0910 45 53 43 41 50 45 33 2c 20 30 2c 20 20 30 20 7d ESCAPE3, 0, 0 }
a0920 2c 0a 20 20 7b 20 20 27 63 27 2c 20 20 30 2c 20 ,. { 'c', 0,
a0930 30 2c 20 65 74 43 48 41 52 58 2c 20 20 20 20 20 0, etCHARX,
a0940 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 0, 0 },. { '
a0950 6f 27 2c 20 20 38 2c 20 30 2c 20 65 74 52 41 44 o', 8, 0, etRAD
a0960 49 58 2c 20 20 20 20 20 20 30 2c 20 20 32 20 7d IX, 0, 2 }
a0970 2c 0a 20 20 7b 20 20 27 75 27 2c 20 31 30 2c 20 ,. { 'u', 10,
a0980 30 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 0, etRADIX,
a0990 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 0, 0 },. { '
a09a0 78 27 2c 20 31 36 2c 20 30 2c 20 65 74 52 41 44 x', 16, 0, etRAD
a09b0 49 58 2c 20 20 20 20 20 20 31 36 2c 20 31 20 7d IX, 16, 1 }
a09c0 2c 0a 20 20 7b 20 20 27 58 27 2c 20 31 36 2c 20 ,. { 'X', 16,
a09d0 30 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 0, etRADIX,
a09e0 20 30 2c 20 20 34 20 7d 2c 0a 23 69 66 6e 64 65 0, 4 },.#ifnde
a09f0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c f SQLITE_OMIT_FL
a0a00 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 7b OATING_POINT. {
a0a10 20 20 27 66 27 2c 20 20 30 2c 20 31 2c 20 65 74 'f', 0, 1, et
a0a20 46 4c 4f 41 54 2c 20 20 20 20 20 20 30 2c 20 20 FLOAT, 0,
a0a30 30 20 7d 2c 0a 20 20 7b 20 20 27 65 27 2c 20 20 0 },. { 'e',
a0a40 30 2c 20 31 2c 20 65 74 45 58 50 2c 20 20 20 20 0, 1, etEXP,
a0a50 20 20 20 20 33 30 2c 20 30 20 7d 2c 0a 20 20 7b 30, 0 },. {
a0a60 20 20 27 45 27 2c 20 20 30 2c 20 31 2c 20 65 74 'E', 0, 1, et
a0a70 45 58 50 2c 20 20 20 20 20 20 20 20 31 34 2c 20 EXP, 14,
a0a80 30 20 7d 2c 0a 20 20 7b 20 20 27 47 27 2c 20 20 0 },. { 'G',
a0a90 30 2c 20 31 2c 20 65 74 47 45 4e 45 52 49 43 2c 0, 1, etGENERIC,
a0aa0 20 20 20 20 31 34 2c 20 30 20 7d 2c 0a 23 65 6e 14, 0 },.#en
a0ab0 64 69 66 0a 20 20 7b 20 20 27 69 27 2c 20 31 30 dif. { 'i', 10
a0ac0 2c 20 31 2c 20 65 74 52 41 44 49 58 2c 20 20 20 , 1, etRADIX,
a0ad0 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 0, 0 },. {
a0ae0 20 27 6e 27 2c 20 20 30 2c 20 30 2c 20 65 74 53 'n', 0, 0, etS
a0af0 49 5a 45 2c 20 20 20 20 20 20 20 30 2c 20 20 30 IZE, 0, 0
a0b00 20 7d 2c 0a 20 20 7b 20 20 27 25 27 2c 20 20 30 },. { '%', 0
a0b10 2c 20 30 2c 20 65 74 50 45 52 43 45 4e 54 2c 20 , 0, etPERCENT,
a0b20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 0, 0 },. {
a0b30 20 27 70 27 2c 20 31 36 2c 20 30 2c 20 65 74 50 'p', 16, 0, etP
a0b40 4f 49 4e 54 45 52 2c 20 20 20 20 30 2c 20 20 31 OINTER, 0, 1
a0b50 20 7d 2c 0a 0a 2f 2a 20 41 6c 6c 20 74 68 65 20 },../* All the
a0b60 72 65 73 74 20 68 61 76 65 20 74 68 65 20 46 4c rest have the FL
a0b70 41 47 5f 49 4e 54 45 52 4e 20 62 69 74 20 73 65 AG_INTERN bit se
a0b80 74 20 61 6e 64 20 61 72 65 20 74 68 75 73 20 66 t and are thus f
a0b90 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 or internal.** u
a0ba0 73 65 20 6f 6e 6c 79 20 2a 2f 0a 20 20 7b 20 20 se only */. {
a0bb0 27 54 27 2c 20 20 30 2c 20 32 2c 20 65 74 54 4f 'T', 0, 2, etTO
a0bc0 4b 45 4e 2c 20 20 20 20 20 20 30 2c 20 20 30 20 KEN, 0, 0
a0bd0 7d 2c 0a 20 20 7b 20 20 27 53 27 2c 20 20 30 2c },. { 'S', 0,
a0be0 20 32 2c 20 65 74 53 52 43 4c 49 53 54 2c 20 20 2, etSRCLIST,
a0bf0 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 0, 0 },. {
a0c00 27 72 27 2c 20 31 30 2c 20 33 2c 20 65 74 4f 52 'r', 10, 3, etOR
a0c10 44 49 4e 41 4c 2c 20 20 20 20 30 2c 20 20 30 20 DINAL, 0, 0
a0c20 7d 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 },.};../*.** If
a0c30 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 SQLITE_OMIT_FLOA
a0c40 54 49 4e 47 5f 50 4f 49 4e 54 20 69 73 20 64 65 TING_POINT is de
a0c50 66 69 6e 65 64 2c 20 74 68 65 6e 20 6e 6f 6e 65 fined, then none
a0c60 20 6f 66 20 74 68 65 20 66 6c 6f 61 74 69 6e 67 of the floating
a0c70 20 70 6f 69 6e 74 0a 2a 2a 20 63 6f 6e 76 65 72 point.** conver
a0c80 73 69 6f 6e 73 20 77 69 6c 6c 20 77 6f 72 6b 2e sions will work.
a0c90 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
a0ca0 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 TE_OMIT_FLOATING
a0cb0 5f 50 4f 49 4e 54 0a 2f 2a 0a 2a 2a 20 22 2a 76 _POINT./*.** "*v
a0cc0 61 6c 22 20 69 73 20 61 20 64 6f 75 62 6c 65 20 al" is a double
a0cd0 73 75 63 68 20 74 68 61 74 20 30 2e 31 20 3c 3d such that 0.1 <=
a0ce0 20 2a 76 61 6c 20 3c 20 31 30 2e 30 0a 2a 2a 20 *val < 10.0.**
a0cf0 52 65 74 75 72 6e 20 74 68 65 20 61 73 63 69 69 Return the ascii
a0d00 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 6c 65 code for the le
a0d10 61 64 69 6e 67 20 64 69 67 69 74 20 6f 66 20 2a ading digit of *
a0d20 76 61 6c 2c 20 74 68 65 6e 0a 2a 2a 20 6d 75 6c val, then.** mul
a0d30 74 69 70 6c 79 20 22 2a 76 61 6c 22 20 62 79 20 tiply "*val" by
a0d40 31 30 2e 30 20 74 6f 20 72 65 6e 6f 72 6d 61 6c 10.0 to renormal
a0d50 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 ize..**.** Examp
a0d60 6c 65 3a 0a 2a 2a 20 20 20 20 20 69 6e 70 75 74 le:.** input
a0d70 3a 20 20 20 20 20 2a 76 61 6c 20 3d 20 33 2e 31 : *val = 3.1
a0d80 34 31 35 39 0a 2a 2a 20 20 20 20 20 6f 75 74 70 4159.** outp
a0d90 75 74 3a 20 20 20 20 2a 76 61 6c 20 3d 20 31 2e ut: *val = 1.
a0da0 34 31 35 39 20 20 20 20 66 75 6e 63 74 69 6f 6e 4159 function
a0db0 20 72 65 74 75 72 6e 20 3d 20 27 33 27 0a 2a 2a return = '3'.**
a0dc0 0a 2a 2a 20 54 68 65 20 63 6f 75 6e 74 65 72 20 .** The counter
a0dd0 2a 63 6e 74 20 69 73 20 69 6e 63 72 65 6d 65 6e *cnt is incremen
a0de0 74 65 64 20 65 61 63 68 20 74 69 6d 65 2e 20 20 ted each time.
a0df0 41 66 74 65 72 20 63 6f 75 6e 74 65 72 20 65 78 After counter ex
a0e00 63 65 65 64 73 0a 2a 2a 20 31 36 20 28 74 68 65 ceeds.** 16 (the
a0e10 20 6e 75 6d 62 65 72 20 6f 66 20 73 69 67 6e 69 number of signi
a0e20 66 69 63 61 6e 74 20 64 69 67 69 74 73 20 69 6e ficant digits in
a0e30 20 61 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 29 a 64-bit float)
a0e40 20 27 30 27 20 69 73 0a 2a 2a 20 61 6c 77 61 79 '0' is.** alway
a0e50 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 s returned..*/.s
a0e60 74 61 74 69 63 20 63 68 61 72 20 65 74 5f 67 65 tatic char et_ge
a0e70 74 64 69 67 69 74 28 4c 4f 4e 47 44 4f 55 42 4c tdigit(LONGDOUBL
a0e80 45 5f 54 59 50 45 20 2a 76 61 6c 2c 20 69 6e 74 E_TYPE *val, int
a0e90 20 2a 63 6e 74 29 7b 0a 20 20 69 6e 74 20 64 69 *cnt){. int di
a0ea0 67 69 74 3b 0a 20 20 4c 4f 4e 47 44 4f 55 42 4c git;. LONGDOUBL
a0eb0 45 5f 54 59 50 45 20 64 3b 0a 20 20 69 66 28 20 E_TYPE d;. if(
a0ec0 28 2a 63 6e 74 29 2b 2b 20 3e 3d 20 31 36 20 29 (*cnt)++ >= 16 )
a0ed0 20 72 65 74 75 72 6e 20 27 30 27 3b 0a 20 20 64 return '0';. d
a0ee0 69 67 69 74 20 3d 20 28 69 6e 74 29 2a 76 61 6c igit = (int)*val
a0ef0 3b 0a 20 20 64 20 3d 20 64 69 67 69 74 3b 0a 20 ;. d = digit;.
a0f00 20 64 69 67 69 74 20 2b 3d 20 27 30 27 3b 0a 20 digit += '0';.
a0f10 20 2a 76 61 6c 20 3d 20 28 2a 76 61 6c 20 2d 20 *val = (*val -
a0f20 64 29 2a 31 30 2e 30 3b 0a 20 20 72 65 74 75 72 d)*10.0;. retur
a0f30 6e 20 28 63 68 61 72 29 64 69 67 69 74 3b 0a 7d n (char)digit;.}
a0f40 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
a0f50 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f E_OMIT_FLOATING_
a0f60 50 4f 49 4e 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 POINT */../*.**
a0f70 41 70 70 65 6e 64 20 4e 20 73 70 61 63 65 20 63 Append N space c
a0f80 68 61 72 61 63 74 65 72 73 20 74 6f 20 74 68 65 haracters to the
a0f90 20 67 69 76 65 6e 20 73 74 72 69 6e 67 20 62 75 given string bu
a0fa0 66 66 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ffer..*/.static
a0fb0 76 6f 69 64 20 61 70 70 65 6e 64 53 70 61 63 65 void appendSpace
a0fc0 28 53 74 72 41 63 63 75 6d 20 2a 70 41 63 63 75 (StrAccum *pAccu
a0fd0 6d 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 73 74 61 m, int N){. sta
a0fe0 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a tic const char z
a0ff0 53 70 61 63 65 73 5b 5d 20 3d 20 22 20 20 20 20 Spaces[] = "
a1000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a1010 20 20 20 20 20 20 20 20 20 22 3b 0a 20 20 77 68 ";. wh
a1020 69 6c 65 28 20 4e 3e 3d 28 69 6e 74 29 73 69 7a ile( N>=(int)siz
a1030 65 6f 66 28 7a 53 70 61 63 65 73 29 2d 31 20 29 eof(zSpaces)-1 )
a1040 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 {. sqlite3Str
a1050 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 AccumAppend(pAcc
a1060 75 6d 2c 20 7a 53 70 61 63 65 73 2c 20 73 69 7a um, zSpaces, siz
a1070 65 6f 66 28 7a 53 70 61 63 65 73 29 2d 31 29 3b eof(zSpaces)-1);
a1080 0a 20 20 20 20 4e 20 2d 3d 20 73 69 7a 65 6f 66 . N -= sizeof
a1090 28 7a 53 70 61 63 65 73 29 2d 31 3b 0a 20 20 7d (zSpaces)-1;. }
a10a0 0a 20 20 69 66 28 20 4e 3e 30 20 29 7b 0a 20 20 . if( N>0 ){.
a10b0 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 sqlite3StrAccu
a10c0 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 mAppend(pAccum,
a10d0 7a 53 70 61 63 65 73 2c 20 4e 29 3b 0a 20 20 7d zSpaces, N);. }
a10e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 20 6d 61 63 .}../*.** On mac
a10f0 68 69 6e 65 73 20 77 69 74 68 20 61 20 73 6d 61 hines with a sma
a1100 6c 6c 20 73 74 61 63 6b 20 73 69 7a 65 2c 20 79 ll stack size, y
a1110 6f 75 20 63 61 6e 20 72 65 64 65 66 69 6e 65 20 ou can redefine
a1120 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 50 52 the.** SQLITE_PR
a1130 49 4e 54 5f 42 55 46 5f 53 49 5a 45 20 74 6f 20 INT_BUF_SIZE to
a1140 62 65 20 6c 65 73 73 20 74 68 61 6e 20 33 35 30 be less than 350
a1150 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
a1160 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 ITE_PRINT_BUF_SI
a1170 5a 45 0a 23 20 69 66 20 64 65 66 69 6e 65 64 28 ZE.# if defined(
a1180 53 51 4c 49 54 45 5f 53 4d 41 4c 4c 5f 53 54 41 SQLITE_SMALL_STA
a1190 43 4b 29 0a 23 20 20 20 64 65 66 69 6e 65 20 53 CK).# define S
a11a0 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f QLITE_PRINT_BUF_
a11b0 53 49 5a 45 20 35 30 0a 23 20 65 6c 73 65 0a 23 SIZE 50.# else.#
a11c0 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 define SQLITE
a11d0 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 20 _PRINT_BUF_SIZE
a11e0 33 35 30 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 350.# endif.#end
a11f0 69 66 0a 23 64 65 66 69 6e 65 20 65 74 42 55 46 if.#define etBUF
a1200 53 49 5a 45 20 53 51 4c 49 54 45 5f 50 52 49 4e SIZE SQLITE_PRIN
a1210 54 5f 42 55 46 5f 53 49 5a 45 20 20 2f 2a 20 53 T_BUF_SIZE /* S
a1220 69 7a 65 20 6f 66 20 74 68 65 20 6f 75 74 70 75 ize of the outpu
a1230 74 20 62 75 66 66 65 72 20 2a 2f 0a 0a 2f 2a 0a t buffer */../*.
a1240 2a 2a 20 54 68 65 20 72 6f 6f 74 20 70 72 6f 67 ** The root prog
a1250 72 61 6d 2e 20 20 41 6c 6c 20 76 61 72 69 61 74 ram. All variat
a1260 69 6f 6e 73 20 63 61 6c 6c 20 74 68 69 73 20 63 ions call this c
a1270 6f 72 65 2e 0a 2a 2a 0a 2a 2a 20 49 4e 50 55 54 ore..**.** INPUT
a1280 53 3a 0a 2a 2a 20 20 20 66 75 6e 63 20 20 20 54 S:.** func T
a1290 68 69 73 20 69 73 20 61 20 70 6f 69 6e 74 65 72 his is a pointer
a12a0 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 to a function t
a12b0 61 6b 69 6e 67 20 74 68 72 65 65 20 61 72 67 75 aking three argu
a12c0 6d 65 6e 74 73 0a 2a 2a 20 20 20 20 20 20 20 20 ments.**
a12d0 20 20 20 20 31 2e 20 41 20 70 6f 69 6e 74 65 72 1. A pointer
a12e0 20 74 6f 20 61 6e 79 74 68 69 6e 67 2e 20 20 53 to anything. S
a12f0 61 6d 65 20 61 73 20 74 68 65 20 22 61 72 67 22 ame as the "arg"
a1300 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 20 parameter..**
a1310 20 20 20 20 20 20 20 20 20 20 32 2e 20 41 20 70 2. A p
a1320 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6c 69 ointer to the li
a1330 73 74 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 st of characters
a1340 20 74 6f 20 62 65 20 6f 75 74 70 75 74 0a 2a 2a to be output.**
a1350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 (
a1360 4e 6f 74 65 2c 20 74 68 69 73 20 6c 69 73 74 20 Note, this list
a1370 69 73 20 4e 4f 54 20 6e 75 6c 6c 20 74 65 72 6d is NOT null term
a1380 69 6e 61 74 65 64 2e 29 0a 2a 2a 20 20 20 20 20 inated.).**
a1390 20 20 20 20 20 20 20 33 2e 20 41 6e 20 69 6e 74 3. An int
a13a0 65 67 65 72 20 6e 75 6d 62 65 72 20 6f 66 20 63 eger number of c
a13b0 68 61 72 61 63 74 65 72 73 20 74 6f 20 62 65 20 haracters to be
a13c0 6f 75 74 70 75 74 2e 0a 2a 2a 20 20 20 20 20 20 output..**
a13d0 20 20 20 20 20 20 20 20 20 28 4e 6f 74 65 3a 20 (Note:
a13e0 54 68 69 73 20 6e 75 6d 62 65 72 20 6d 69 67 68 This number migh
a13f0 74 20 62 65 20 7a 65 72 6f 2e 29 0a 2a 2a 0a 2a t be zero.).**.*
a1400 2a 20 20 20 61 72 67 20 20 20 20 54 68 69 73 20 * arg This
a1410 69 73 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 is the pointer t
a1420 6f 20 61 6e 79 74 68 69 6e 67 20 77 68 69 63 68 o anything which
a1430 20 77 69 6c 6c 20 62 65 20 70 61 73 73 65 64 20 will be passed
a1440 61 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 as the.**
a1450 20 20 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e first argumen
a1460 74 20 74 6f 20 22 66 75 6e 63 22 2e 20 20 55 73 t to "func". Us
a1470 65 20 69 74 20 66 6f 72 20 77 68 61 74 65 76 65 e it for whateve
a1480 72 20 79 6f 75 20 6c 69 6b 65 2e 0a 2a 2a 0a 2a r you like..**.*
a1490 2a 20 20 20 66 6d 74 20 20 20 20 54 68 69 73 20 * fmt This
a14a0 69 73 20 74 68 65 20 66 6f 72 6d 61 74 20 73 74 is the format st
a14b0 72 69 6e 67 2c 20 61 73 20 69 6e 20 74 68 65 20 ring, as in the
a14c0 75 73 75 61 6c 20 70 72 69 6e 74 2e 0a 2a 2a 0a usual print..**.
a14d0 2a 2a 20 20 20 61 70 20 20 20 20 20 54 68 69 73 ** ap This
a14e0 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
a14f0 20 61 20 6c 69 73 74 20 6f 66 20 61 72 67 75 6d a list of argum
a1500 65 6e 74 73 2e 20 20 53 61 6d 65 20 61 73 20 69 ents. Same as i
a1510 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 76 66 n.** vf
a1520 70 72 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4f 55 54 print..**.** OUT
a1530 50 55 54 53 3a 0a 2a 2a 20 20 20 20 20 20 20 20 PUTS:.**
a1540 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c The return val
a1550 75 65 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 ue is the total
a1560 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 number of charac
a1570 74 65 72 73 20 73 65 6e 74 20 74 6f 0a 2a 2a 20 ters sent to.**
a1580 20 20 20 20 20 20 20 20 20 74 68 65 20 66 75 6e the fun
a1590 63 74 69 6f 6e 20 22 66 75 6e 63 22 2e 20 20 52 ction "func". R
a15a0 65 74 75 72 6e 73 20 2d 31 20 6f 6e 20 61 20 65 eturns -1 on a e
a15b0 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 rror..**.** Note
a15c0 20 74 68 61 74 20 74 68 65 20 6f 72 64 65 72 20 that the order
a15d0 69 6e 20 77 68 69 63 68 20 61 75 74 6f 6d 61 74 in which automat
a15e0 69 63 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 ic variables are
a15f0 20 64 65 63 6c 61 72 65 64 20 62 65 6c 6f 77 0a declared below.
a1600 2a 2a 20 73 65 65 6d 73 20 74 6f 20 6d 61 6b 65 ** seems to make
a1610 20 61 20 62 69 67 20 64 69 66 66 65 72 65 6e 63 a big differenc
a1620 65 20 69 6e 20 64 65 74 65 72 6d 69 6e 69 6e 67 e in determining
a1630 20 68 6f 77 20 66 61 73 74 20 74 68 69 73 20 62 how fast this b
a1640 65 61 73 74 0a 2a 2a 20 77 69 6c 6c 20 72 75 6e east.** will run
a1650 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
a1660 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
a1670 56 58 50 72 69 6e 74 66 28 0a 20 20 53 74 72 41 VXPrintf(. StrA
a1680 63 63 75 6d 20 2a 70 41 63 63 75 6d 2c 20 20 20 ccum *pAccum,
a1690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
a16a0 2a 20 41 63 63 75 6d 75 6c 61 74 65 20 72 65 73 * Accumulate res
a16b0 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 ults here */. i
a16c0 6e 74 20 75 73 65 45 78 74 65 6e 64 65 64 2c 20 nt useExtended,
a16d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a16e0 20 20 2f 2a 20 41 6c 6c 6f 77 20 65 78 74 65 6e /* Allow exten
a16f0 64 65 64 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e ded %-conversion
a1700 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 s */. const cha
a1710 72 20 2a 66 6d 74 2c 20 20 20 20 20 20 20 20 20 r *fmt,
a1720 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 /* For
a1730 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 mat string */.
a1740 76 61 5f 6c 69 73 74 20 61 70 20 20 20 20 20 20 va_list ap
a1750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a1760 20 20 20 2f 2a 20 61 72 67 75 6d 65 6e 74 73 20 /* arguments
a1770 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 63 3b 20 20 */.){. int c;
a1780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a1790 20 20 20 2f 2a 20 4e 65 78 74 20 63 68 61 72 61 /* Next chara
a17a0 63 74 65 72 20 69 6e 20 74 68 65 20 66 6f 72 6d cter in the form
a17b0 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 63 at string */. c
a17c0 68 61 72 20 2a 62 75 66 70 74 3b 20 20 20 20 20 har *bufpt;
a17d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 /* Poi
a17e0 6e 74 65 72 20 74 6f 20 74 68 65 20 63 6f 6e 76 nter to the conv
a17f0 65 72 73 69 6f 6e 20 62 75 66 66 65 72 20 2a 2f ersion buffer */
a1800 0a 20 20 69 6e 74 20 70 72 65 63 69 73 69 6f 6e . int precision
a1810 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
a1820 20 50 72 65 63 69 73 69 6f 6e 20 6f 66 20 74 68 Precision of th
a1830 65 20 63 75 72 72 65 6e 74 20 66 69 65 6c 64 20 e current field
a1840 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 67 74 68 3b */. int length;
a1850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a1860 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 /* Length of the
a1870 20 66 69 65 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 field */. int
a1880 69 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 idx;
a1890 20 20 20 20 20 20 20 2f 2a 20 41 20 67 65 6e 65 /* A gene
a18a0 72 61 6c 20 70 75 72 70 6f 73 65 20 6c 6f 6f 70 ral purpose loop
a18b0 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e counter */. in
a18c0 74 20 77 69 64 74 68 3b 20 20 20 20 20 20 20 20 t width;
a18d0 20 20 20 20 20 20 20 20 20 2f 2a 20 57 69 64 74 /* Widt
a18e0 68 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 h of the current
a18f0 20 66 69 65 6c 64 20 2a 2f 0a 20 20 65 74 42 79 field */. etBy
a1900 74 65 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 te flag_leftjust
a1910 69 66 79 3b 20 20 20 2f 2a 20 54 72 75 65 20 69 ify; /* True i
a1920 66 20 22 2d 22 20 66 6c 61 67 20 69 73 20 70 72 f "-" flag is pr
a1930 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 esent */. etByt
a1940 65 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e 3b e flag_plussign;
a1950 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
a1960 20 22 2b 22 20 66 6c 61 67 20 69 73 20 70 72 65 "+" flag is pre
a1970 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 sent */. etByte
a1980 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 3b flag_blanksign;
a1990 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
a19a0 22 20 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 " " flag is pres
a19b0 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 ent */. etByte
a19c0 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f flag_alternatefo
a19d0 72 6d 3b 20 2f 2a 20 54 72 75 65 20 69 66 20 22 rm; /* True if "
a19e0 23 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 #" flag is prese
a19f0 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 nt */. etByte f
a1a00 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 3b 20 20 20 lag_altform2;
a1a10 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 21 /* True if "!
a1a20 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e " flag is presen
a1a30 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c t */. etByte fl
a1a40 61 67 5f 7a 65 72 6f 70 61 64 3b 20 20 20 20 20 ag_zeropad;
a1a50 20 20 2f 2a 20 54 72 75 65 20 69 66 20 66 69 65 /* True if fie
a1a60 6c 64 20 77 69 64 74 68 20 63 6f 6e 73 74 61 6e ld width constan
a1a70 74 20 73 74 61 72 74 73 20 77 69 74 68 20 7a 65 t starts with ze
a1a80 72 6f 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 ro */. etByte f
a1a90 6c 61 67 5f 6c 6f 6e 67 3b 20 20 20 20 20 20 20 lag_long;
a1aa0 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 6c /* True if "l
a1ab0 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e " flag is presen
a1ac0 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c t */. etByte fl
a1ad0 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 3b 20 20 20 20 ag_longlong;
a1ae0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 /* True if the
a1af0 20 22 6c 6c 22 20 66 6c 61 67 20 69 73 20 70 72 "ll" flag is pr
a1b00 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 esent */. etByt
a1b10 65 20 64 6f 6e 65 3b 20 20 20 20 20 20 20 20 20 e done;
a1b20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 74 65 /* Loop te
a1b30 72 6d 69 6e 61 74 69 6f 6e 20 66 6c 61 67 20 2a rmination flag *
a1b40 2f 0a 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 /. sqlite_uint6
a1b50 34 20 6c 6f 6e 67 76 61 6c 75 65 3b 20 20 20 2f 4 longvalue; /
a1b60 2a 20 56 61 6c 75 65 20 66 6f 72 20 69 6e 74 65 * Value for inte
a1b70 67 65 72 20 74 79 70 65 73 20 2a 2f 0a 20 20 4c ger types */. L
a1b80 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 72 ONGDOUBLE_TYPE r
a1b90 65 61 6c 76 61 6c 75 65 3b 20 2f 2a 20 56 61 6c ealvalue; /* Val
a1ba0 75 65 20 66 6f 72 20 72 65 61 6c 20 74 79 70 65 ue for real type
a1bb0 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 65 74 5f s */. const et_
a1bc0 69 6e 66 6f 20 2a 69 6e 66 6f 70 3b 20 20 20 20 info *infop;
a1bd0 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
a1be0 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 the appropriate
a1bf0 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 2a info structure *
a1c00 2f 0a 20 20 63 68 61 72 20 62 75 66 5b 65 74 42 /. char buf[etB
a1c10 55 46 53 49 5a 45 5d 3b 20 20 20 20 20 20 20 2f UFSIZE]; /
a1c20 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 20 62 75 66 * Conversion buf
a1c30 66 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 70 72 fer */. char pr
a1c40 65 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 20 efix;
a1c50 20 20 20 20 2f 2a 20 50 72 65 66 69 78 20 63 68 /* Prefix ch
a1c60 61 72 61 63 74 65 72 2e 20 20 22 2b 22 20 6f 72 aracter. "+" or
a1c70 20 22 2d 22 20 6f 72 20 22 20 22 20 6f 72 20 27 "-" or " " or '
a1c80 5c 30 27 2e 20 2a 2f 0a 20 20 65 74 42 79 74 65 \0'. */. etByte
a1c90 20 78 74 79 70 65 20 3d 20 30 3b 20 20 20 20 20 xtype = 0;
a1ca0 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69 /* Conversi
a1cb0 6f 6e 20 70 61 72 61 64 69 67 6d 20 2a 2f 0a 20 on paradigm */.
a1cc0 20 63 68 61 72 20 2a 7a 45 78 74 72 61 3b 20 20 char *zExtra;
a1cd0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 /* E
a1ce0 78 74 72 61 20 6d 65 6d 6f 72 79 20 75 73 65 64 xtra memory used
a1cf0 20 66 6f 72 20 65 74 54 43 4c 45 53 43 41 50 45 for etTCLESCAPE
a1d00 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 2a 2f 0a conversions */.
a1d10 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
a1d20 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 MIT_FLOATING_POI
a1d30 4e 54 0a 20 20 69 6e 74 20 20 65 78 70 2c 20 65 NT. int exp, e
a1d40 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2;
a1d50 2f 2a 20 65 78 70 6f 6e 65 6e 74 20 6f 66 20 72 /* exponent of r
a1d60 65 61 6c 20 6e 75 6d 62 65 72 73 20 2a 2f 0a 20 eal numbers */.
a1d70 20 64 6f 75 62 6c 65 20 72 6f 75 6e 64 65 72 3b double rounder;
a1d80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 /* U
a1d90 73 65 64 20 66 6f 72 20 72 6f 75 6e 64 69 6e 67 sed for rounding
a1da0 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 floating point
a1db0 76 61 6c 75 65 73 20 2a 2f 0a 20 20 65 74 42 79 values */. etBy
a1dc0 74 65 20 66 6c 61 67 5f 64 70 3b 20 20 20 20 20 te flag_dp;
a1dd0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
a1de0 66 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 f decimal point
a1df0 73 68 6f 75 6c 64 20 62 65 20 73 68 6f 77 6e 20 should be shown
a1e00 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 */. etByte flag
a1e10 5f 72 74 7a 3b 20 20 20 20 20 20 20 20 20 20 20 _rtz;
a1e20 2f 2a 20 54 72 75 65 20 69 66 20 74 72 61 69 6c /* True if trail
a1e30 69 6e 67 20 7a 65 72 6f 73 20 73 68 6f 75 6c 64 ing zeros should
a1e40 20 62 65 20 72 65 6d 6f 76 65 64 20 2a 2f 0a 20 be removed */.
a1e50 20 65 74 42 79 74 65 20 66 6c 61 67 5f 65 78 70 etByte flag_exp
a1e60 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ; /* T
a1e70 72 75 65 20 74 6f 20 66 6f 72 63 65 20 64 69 73 rue to force dis
a1e80 70 6c 61 79 20 6f 66 20 74 68 65 20 65 78 70 6f play of the expo
a1e90 6e 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 73 nent */. int ns
a1ea0 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d;
a1eb0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
a1ec0 66 20 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69 f significant di
a1ed0 67 69 74 73 20 72 65 74 75 72 6e 65 64 20 2a 2f gits returned */
a1ee0 0a 23 65 6e 64 69 66 0a 0a 20 20 6c 65 6e 67 74 .#endif.. lengt
a1ef0 68 20 3d 20 30 3b 0a 20 20 62 75 66 70 74 20 3d h = 0;. bufpt =
a1f00 20 30 3b 0a 20 20 66 6f 72 28 3b 20 28 63 3d 28 0;. for(; (c=(
a1f10 2a 66 6d 74 29 29 21 3d 30 3b 20 2b 2b 66 6d 74 *fmt))!=0; ++fmt
a1f20 29 7b 0a 20 20 20 20 69 66 28 20 63 21 3d 27 25 ){. if( c!='%
a1f30 27 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 ' ){. int a
a1f40 6d 74 3b 0a 20 20 20 20 20 20 62 75 66 70 74 20 mt;. bufpt
a1f50 3d 20 28 63 68 61 72 20 2a 29 66 6d 74 3b 0a 20 = (char *)fmt;.
a1f60 20 20 20 20 20 61 6d 74 20 3d 20 31 3b 0a 20 20 amt = 1;.
a1f70 20 20 20 20 77 68 69 6c 65 28 20 28 63 3d 28 2a while( (c=(*
a1f80 2b 2b 66 6d 74 29 29 21 3d 27 25 27 20 26 26 20 ++fmt))!='%' &&
a1f90 63 21 3d 30 20 29 20 61 6d 74 2b 2b 3b 0a 20 20 c!=0 ) amt++;.
a1fa0 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 sqlite3StrAc
a1fb0 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d cumAppend(pAccum
a1fc0 2c 20 62 75 66 70 74 2c 20 61 6d 74 29 3b 0a 20 , bufpt, amt);.
a1fd0 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20 29 20 if( c==0 )
a1fe0 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
a1ff0 20 69 66 28 20 28 63 3d 28 2a 2b 2b 66 6d 74 29 if( (c=(*++fmt)
a2000 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 )==0 ){. sq
a2010 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 lite3StrAccumApp
a2020 65 6e 64 28 70 41 63 63 75 6d 2c 20 22 25 22 2c end(pAccum, "%",
a2030 20 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 1);. break
a2040 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 46 ;. }. /* F
a2050 69 6e 64 20 6f 75 74 20 77 68 61 74 20 66 6c 61 ind out what fla
a2060 67 73 20 61 72 65 20 70 72 65 73 65 6e 74 20 2a gs are present *
a2070 2f 0a 20 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a /. flag_leftj
a2080 75 73 74 69 66 79 20 3d 20 66 6c 61 67 5f 70 6c ustify = flag_pl
a2090 75 73 73 69 67 6e 20 3d 20 66 6c 61 67 5f 62 6c ussign = flag_bl
a20a0 61 6e 6b 73 69 67 6e 20 3d 20 0a 20 20 20 20 20 anksign = .
a20b0 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f flag_alternatefo
a20c0 72 6d 20 3d 20 66 6c 61 67 5f 61 6c 74 66 6f 72 rm = flag_altfor
a20d0 6d 32 20 3d 20 66 6c 61 67 5f 7a 65 72 6f 70 61 m2 = flag_zeropa
a20e0 64 20 3d 20 30 3b 0a 20 20 20 20 64 6f 6e 65 20 d = 0;. done
a20f0 3d 20 30 3b 0a 20 20 20 20 64 6f 7b 0a 20 20 20 = 0;. do{.
a2100 20 20 20 73 77 69 74 63 68 28 20 63 20 29 7b 0a switch( c ){.
a2110 20 20 20 20 20 20 20 20 63 61 73 65 20 27 2d 27 case '-'
a2120 3a 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 : flag_leftjus
a2130 74 69 66 79 20 3d 20 31 3b 20 20 20 20 20 62 72 tify = 1; br
a2140 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 eak;. cas
a2150 65 20 27 2b 27 3a 20 20 20 66 6c 61 67 5f 70 6c e '+': flag_pl
a2160 75 73 73 69 67 6e 20 3d 20 31 3b 20 20 20 20 20 ussign = 1;
a2170 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
a2180 20 20 63 61 73 65 20 27 20 27 3a 20 20 20 66 6c case ' ': fl
a2190 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 3d 20 31 ag_blanksign = 1
a21a0 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ; break;.
a21b0 20 20 20 20 20 20 20 63 61 73 65 20 27 23 27 3a case '#':
a21c0 20 20 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 flag_alternat
a21d0 65 66 6f 72 6d 20 3d 20 31 3b 20 20 20 62 72 65 eform = 1; bre
a21e0 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 ak;. case
a21f0 20 27 21 27 3a 20 20 20 66 6c 61 67 5f 61 6c 74 '!': flag_alt
a2200 66 6f 72 6d 32 20 3d 20 31 3b 20 20 20 20 20 20 form2 = 1;
a2210 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
a2220 20 63 61 73 65 20 27 30 27 3a 20 20 20 66 6c 61 case '0': fla
a2230 67 5f 7a 65 72 6f 70 61 64 20 3d 20 31 3b 20 20 g_zeropad = 1;
a2240 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
a2250 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20 default:
a2260 20 20 64 6f 6e 65 20 3d 20 31 3b 20 20 20 20 20 done = 1;
a2270 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 brea
a2280 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d k;. }. }
a2290 77 68 69 6c 65 28 20 21 64 6f 6e 65 20 26 26 20 while( !done &&
a22a0 28 63 3d 28 2a 2b 2b 66 6d 74 29 29 21 3d 30 20 (c=(*++fmt))!=0
a22b0 29 3b 0a 20 20 20 20 2f 2a 20 47 65 74 20 74 68 );. /* Get th
a22c0 65 20 66 69 65 6c 64 20 77 69 64 74 68 20 2a 2f e field width */
a22d0 0a 20 20 20 20 77 69 64 74 68 20 3d 20 30 3b 0a . width = 0;.
a22e0 20 20 20 20 69 66 28 20 63 3d 3d 27 2a 27 20 29 if( c=='*' )
a22f0 7b 0a 20 20 20 20 20 20 77 69 64 74 68 20 3d 20 {. width =
a2300 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a va_arg(ap,int);.
a2310 20 20 20 20 20 20 69 66 28 20 77 69 64 74 68 3c if( width<
a2320 30 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6c 61 0 ){. fla
a2330 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 3d 20 g_leftjustify =
a2340 31 3b 0a 20 20 20 20 20 20 20 20 77 69 64 74 68 1;. width
a2350 20 3d 20 2d 77 69 64 74 68 3b 0a 20 20 20 20 20 = -width;.
a2360 20 7d 0a 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b }. c = *++
a2370 66 6d 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a fmt;. }else{.
a2380 20 20 20 20 20 20 77 68 69 6c 65 28 20 63 3e 3d while( c>=
a2390 27 30 27 20 26 26 20 63 3c 3d 27 39 27 20 29 7b '0' && c<='9' ){
a23a0 0a 20 20 20 20 20 20 20 20 77 69 64 74 68 20 3d . width =
a23b0 20 77 69 64 74 68 2a 31 30 20 2b 20 63 20 2d 20 width*10 + c -
a23c0 27 30 27 3b 0a 20 20 20 20 20 20 20 20 63 20 3d '0';. c =
a23d0 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 20 7d *++fmt;. }
a23e0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 77 . }. if( w
a23f0 69 64 74 68 20 3e 20 65 74 42 55 46 53 49 5a 45 idth > etBUFSIZE
a2400 2d 31 30 20 29 7b 0a 20 20 20 20 20 20 77 69 64 -10 ){. wid
a2410 74 68 20 3d 20 65 74 42 55 46 53 49 5a 45 2d 31 th = etBUFSIZE-1
a2420 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 0;. }. /*
a2430 47 65 74 20 74 68 65 20 70 72 65 63 69 73 69 6f Get the precisio
a2440 6e 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 3d 3d n */. if( c==
a2450 27 2e 27 20 29 7b 0a 20 20 20 20 20 20 70 72 65 '.' ){. pre
a2460 63 69 73 69 6f 6e 20 3d 20 30 3b 0a 20 20 20 20 cision = 0;.
a2470 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 c = *++fmt;.
a2480 20 20 20 20 69 66 28 20 63 3d 3d 27 2a 27 20 29 if( c=='*' )
a2490 7b 0a 20 20 20 20 20 20 20 20 70 72 65 63 69 73 {. precis
a24a0 69 6f 6e 20 3d 20 76 61 5f 61 72 67 28 61 70 2c ion = va_arg(ap,
a24b0 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 int);. if
a24c0 28 20 70 72 65 63 69 73 69 6f 6e 3c 30 20 29 20 ( precision<0 )
a24d0 70 72 65 63 69 73 69 6f 6e 20 3d 20 2d 70 72 65 precision = -pre
a24e0 63 69 73 69 6f 6e 3b 0a 20 20 20 20 20 20 20 20 cision;.
a24f0 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 c = *++fmt;.
a2500 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
a2510 20 77 68 69 6c 65 28 20 63 3e 3d 27 30 27 20 26 while( c>='0' &
a2520 26 20 63 3c 3d 27 39 27 20 29 7b 0a 20 20 20 20 & c<='9' ){.
a2530 20 20 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20 precision
a2540 3d 20 70 72 65 63 69 73 69 6f 6e 2a 31 30 20 2b = precision*10 +
a2550 20 63 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 c - '0';.
a2560 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a c = *++fmt;.
a2570 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
a2580 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
a2590 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 2d precision = -
a25a0 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 1;. }. /*
a25b0 47 65 74 20 74 68 65 20 63 6f 6e 76 65 72 73 69 Get the conversi
a25c0 6f 6e 20 74 79 70 65 20 6d 6f 64 69 66 69 65 72 on type modifier
a25d0 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 3d 3d 27 */. if( c=='
a25e0 6c 27 20 29 7b 0a 20 20 20 20 20 20 66 6c 61 67 l' ){. flag
a25f0 5f 6c 6f 6e 67 20 3d 20 31 3b 0a 20 20 20 20 20 _long = 1;.
a2600 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 c = *++fmt;.
a2610 20 20 20 69 66 28 20 63 3d 3d 27 6c 27 20 29 7b if( c=='l' ){
a2620 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f . flag_lo
a2630 6e 67 6c 6f 6e 67 20 3d 20 31 3b 0a 20 20 20 20 nglong = 1;.
a2640 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a c = *++fmt;.
a2650 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
a2660 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f flag_longlo
a2670 6e 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a ng = 0;. }.
a2680 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
a2690 20 66 6c 61 67 5f 6c 6f 6e 67 20 3d 20 66 6c 61 flag_long = fla
a26a0 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 30 3b 0a g_longlong = 0;.
a26b0 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 46 65 74 }. /* Fet
a26c0 63 68 20 74 68 65 20 69 6e 66 6f 20 65 6e 74 72 ch the info entr
a26d0 79 20 66 6f 72 20 74 68 65 20 66 69 65 6c 64 20 y for the field
a26e0 2a 2f 0a 20 20 20 20 69 6e 66 6f 70 20 3d 20 26 */. infop = &
a26f0 66 6d 74 69 6e 66 6f 5b 30 5d 3b 0a 20 20 20 20 fmtinfo[0];.
a2700 78 74 79 70 65 20 3d 20 65 74 49 4e 56 41 4c 49 xtype = etINVALI
a2710 44 3b 0a 20 20 20 20 66 6f 72 28 69 64 78 3d 30 D;. for(idx=0
a2720 3b 20 69 64 78 3c 41 72 72 61 79 53 69 7a 65 28 ; idx<ArraySize(
a2730 66 6d 74 69 6e 66 6f 29 3b 20 69 64 78 2b 2b 29 fmtinfo); idx++)
a2740 7b 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 66 {. if( c==f
a2750 6d 74 69 6e 66 6f 5b 69 64 78 5d 2e 66 6d 74 74 mtinfo[idx].fmtt
a2760 79 70 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69 ype ){. i
a2770 6e 66 6f 70 20 3d 20 26 66 6d 74 69 6e 66 6f 5b nfop = &fmtinfo[
a2780 69 64 78 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 idx];. if
a2790 28 20 75 73 65 45 78 74 65 6e 64 65 64 20 7c 7c ( useExtended ||
a27a0 20 28 69 6e 66 6f 70 2d 3e 66 6c 61 67 73 20 26 (infop->flags &
a27b0 20 46 4c 41 47 5f 49 4e 54 45 52 4e 29 3d 3d 30 FLAG_INTERN)==0
a27c0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 78 74 ){. xt
a27d0 79 70 65 20 3d 20 69 6e 66 6f 70 2d 3e 74 79 70 ype = infop->typ
a27e0 65 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 e;. }else
a27f0 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 {. retu
a2800 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 rn;. }.
a2810 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
a2820 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 7a }. }. z
a2830 45 78 74 72 61 20 3d 20 30 3b 0a 0a 0a 20 20 20 Extra = 0;...
a2840 20 2f 2a 20 4c 69 6d 69 74 20 74 68 65 20 70 72 /* Limit the pr
a2850 65 63 69 73 69 6f 6e 20 74 6f 20 70 72 65 76 65 ecision to preve
a2860 6e 74 20 6f 76 65 72 66 6c 6f 77 69 6e 67 20 62 nt overflowing b
a2870 75 66 5b 5d 20 64 75 72 69 6e 67 20 63 6f 6e 76 uf[] during conv
a2880 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 66 ersion */. if
a2890 28 20 70 72 65 63 69 73 69 6f 6e 3e 65 74 42 55 ( precision>etBU
a28a0 46 53 49 5a 45 2d 34 30 20 26 26 20 28 69 6e 66 FSIZE-40 && (inf
a28b0 6f 70 2d 3e 66 6c 61 67 73 20 26 20 46 4c 41 47 op->flags & FLAG
a28c0 5f 53 54 52 49 4e 47 29 3d 3d 30 20 29 7b 0a 20 _STRING)==0 ){.
a28d0 20 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d precision =
a28e0 20 65 74 42 55 46 53 49 5a 45 2d 34 30 3b 0a 20 etBUFSIZE-40;.
a28f0 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 }.. /*.
a2900 20 2a 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e ** At this poin
a2910 74 2c 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 t, variables are
a2920 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 61 73 20 initialized as
a2930 66 6f 6c 6c 6f 77 73 3a 0a 20 20 20 20 2a 2a 0a follows:. **.
a2940 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 61 6c ** flag_al
a2950 74 65 72 6e 61 74 65 66 6f 72 6d 20 20 20 20 20 ternateform
a2960 20 20 20 20 20 54 52 55 45 20 69 66 20 61 20 27 TRUE if a '
a2970 23 27 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 #' is present..
a2980 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 61 6c 74 ** flag_alt
a2990 66 6f 72 6d 32 20 20 20 20 20 20 20 20 20 20 20 form2
a29a0 20 20 20 20 54 52 55 45 20 69 66 20 61 20 27 21 TRUE if a '!
a29b0 27 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 ' is present..
a29c0 20 20 2a 2a 20 20 20 66 6c 61 67 5f 70 6c 75 73 ** flag_plus
a29d0 73 69 67 6e 20 20 20 20 20 20 20 20 20 20 20 20 sign
a29e0 20 20 20 54 52 55 45 20 69 66 20 61 20 27 2b 27 TRUE if a '+'
a29f0 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 is present..
a2a00 20 2a 2a 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a ** flag_leftj
a2a10 75 73 74 69 66 79 20 20 20 20 20 20 20 20 20 20 ustify
a2a20 20 20 54 52 55 45 20 69 66 20 61 20 27 2d 27 20 TRUE if a '-'
a2a30 69 73 20 70 72 65 73 65 6e 74 20 6f 72 20 69 66 is present or if
a2a40 20 74 68 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 the. **
a2a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2a60 20 20 20 20 20 20 20 20 20 20 66 69 65 6c 64 20 field
a2a70 77 69 64 74 68 20 77 61 73 20 6e 65 67 61 74 69 width was negati
a2a80 76 65 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 ve.. ** fla
a2a90 67 5f 7a 65 72 6f 70 61 64 20 20 20 20 20 20 20 g_zeropad
a2aa0 20 20 20 20 20 20 20 20 20 54 52 55 45 20 69 66 TRUE if
a2ab0 20 74 68 65 20 77 69 64 74 68 20 62 65 67 61 6e the width began
a2ac0 20 77 69 74 68 20 30 2e 0a 20 20 20 20 2a 2a 20 with 0.. **
a2ad0 20 20 66 6c 61 67 5f 6c 6f 6e 67 20 20 20 20 20 flag_long
a2ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 52 TR
a2af0 55 45 20 69 66 20 74 68 65 20 6c 65 74 74 65 72 UE if the letter
a2b00 20 27 6c 27 20 28 65 6c 6c 29 20 70 72 65 66 69 'l' (ell) prefi
a2b10 78 65 64 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 xed. **
a2b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2b30 20 20 20 20 20 20 20 20 20 74 68 65 20 63 6f 6e the con
a2b40 76 65 72 73 69 6f 6e 20 63 68 61 72 61 63 74 65 version characte
a2b50 72 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 r.. ** flag
a2b60 5f 6c 6f 6e 67 6c 6f 6e 67 20 20 20 20 20 20 20 _longlong
a2b70 20 20 20 20 20 20 20 20 54 52 55 45 20 69 66 20 TRUE if
a2b80 74 68 65 20 6c 65 74 74 65 72 20 27 6c 6c 27 20 the letter 'll'
a2b90 28 65 6c 6c 20 65 6c 6c 29 20 70 72 65 66 69 78 (ell ell) prefix
a2ba0 65 64 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 ed. **
a2bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2bc0 20 20 20 20 20 20 20 20 74 68 65 20 63 6f 6e 76 the conv
a2bd0 65 72 73 69 6f 6e 20 63 68 61 72 61 63 74 65 72 ersion character
a2be0 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f .. ** flag_
a2bf0 62 6c 61 6e 6b 73 69 67 6e 20 20 20 20 20 20 20 blanksign
a2c00 20 20 20 20 20 20 20 54 52 55 45 20 69 66 20 61 TRUE if a
a2c10 20 27 20 27 20 69 73 20 70 72 65 73 65 6e 74 2e ' ' is present.
a2c20 0a 20 20 20 20 2a 2a 20 20 20 77 69 64 74 68 20 . ** width
a2c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2c40 20 20 20 20 20 20 54 68 65 20 73 70 65 63 69 66 The specif
a2c50 69 65 64 20 66 69 65 6c 64 20 77 69 64 74 68 2e ied field width.
a2c60 20 20 54 68 69 73 20 69 73 0a 20 20 20 20 2a 2a This is. **
a2c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 a
a2c90 6c 77 61 79 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 lways non-negati
a2ca0 76 65 2e 20 20 5a 65 72 6f 20 69 73 20 74 68 65 ve. Zero is the
a2cb0 20 64 65 66 61 75 6c 74 2e 0a 20 20 20 20 2a 2a default.. **
a2cc0 20 20 20 70 72 65 63 69 73 69 6f 6e 20 20 20 20 precision
a2cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 T
a2ce0 68 65 20 73 70 65 63 69 66 69 65 64 20 70 72 65 he specified pre
a2cf0 63 69 73 69 6f 6e 2e 20 20 54 68 65 20 64 65 66 cision. The def
a2d00 61 75 6c 74 0a 20 20 20 20 2a 2a 20 20 20 20 20 ault. **
a2d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2d20 20 20 20 20 20 20 20 20 20 20 69 73 20 2d 31 2e is -1.
a2d30 0a 20 20 20 20 2a 2a 20 20 20 78 74 79 70 65 20 . ** xtype
a2d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2d50 20 20 20 20 20 20 54 68 65 20 63 6c 61 73 73 20 The class
a2d60 6f 66 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f of the conversio
a2d70 6e 2e 0a 20 20 20 20 2a 2a 20 20 20 69 6e 66 6f n.. ** info
a2d80 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p
a2d90 20 20 20 20 20 20 20 20 50 6f 69 6e 74 65 72 20 Pointer
a2da0 74 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 to the appropria
a2db0 74 65 20 69 6e 66 6f 20 73 74 72 75 63 74 2e 0a te info struct..
a2dc0 20 20 20 20 2a 2f 0a 20 20 20 20 73 77 69 74 63 */. switc
a2dd0 68 28 20 78 74 79 70 65 20 29 7b 0a 20 20 20 20 h( xtype ){.
a2de0 20 20 63 61 73 65 20 65 74 50 4f 49 4e 54 45 52 case etPOINTER
a2df0 3a 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c :. flag_l
a2e00 6f 6e 67 6c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 onglong = sizeof
a2e10 28 63 68 61 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 (char*)==sizeof(
a2e20 69 36 34 29 3b 0a 20 20 20 20 20 20 20 20 66 6c i64);. fl
a2e30 61 67 5f 6c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 ag_long = sizeof
a2e40 28 63 68 61 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 (char*)==sizeof(
a2e50 6c 6f 6e 67 20 69 6e 74 29 3b 0a 20 20 20 20 20 long int);.
a2e60 20 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 /* Fall throu
a2e70 67 68 20 69 6e 74 6f 20 74 68 65 20 6e 65 78 74 gh into the next
a2e80 20 63 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 63 case */. c
a2e90 61 73 65 20 65 74 4f 52 44 49 4e 41 4c 3a 0a 20 ase etORDINAL:.
a2ea0 20 20 20 20 20 63 61 73 65 20 65 74 52 41 44 49 case etRADI
a2eb0 58 3a 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 X:. if( i
a2ec0 6e 66 6f 70 2d 3e 66 6c 61 67 73 20 26 20 46 4c nfop->flags & FL
a2ed0 41 47 5f 53 49 47 4e 45 44 20 29 7b 0a 20 20 20 AG_SIGNED ){.
a2ee0 20 20 20 20 20 20 20 69 36 34 20 76 3b 0a 20 20 i64 v;.
a2ef0 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 if( flag
a2f00 5f 6c 6f 6e 67 6c 6f 6e 67 20 29 7b 0a 20 20 20 _longlong ){.
a2f10 20 20 20 20 20 20 20 20 20 76 20 3d 20 76 61 5f v = va_
a2f20 61 72 67 28 61 70 2c 69 36 34 29 3b 0a 20 20 20 arg(ap,i64);.
a2f30 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 }else if(
a2f40 20 66 6c 61 67 5f 6c 6f 6e 67 20 29 7b 0a 20 20 flag_long ){.
a2f50 20 20 20 20 20 20 20 20 20 20 76 20 3d 20 76 61 v = va
a2f60 5f 61 72 67 28 61 70 2c 6c 6f 6e 67 20 69 6e 74 _arg(ap,long int
a2f70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c );. }el
a2f80 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 se{.
a2f90 76 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 6e v = va_arg(ap,in
a2fa0 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a t);. }.
a2fb0 20 20 20 20 20 20 20 20 20 20 69 66 28 20 76 3c if( v<
a2fc0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 0 ){.
a2fd0 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 2d 76 3b longvalue = -v;
a2fe0 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 72 65 . pre
a2ff0 66 69 78 20 3d 20 27 2d 27 3b 0a 20 20 20 20 20 fix = '-';.
a3000 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
a3010 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 longvalu
a3020 65 20 3d 20 76 3b 0a 20 20 20 20 20 20 20 20 20 e = v;.
a3030 20 20 20 69 66 28 20 66 6c 61 67 5f 70 6c 75 73 if( flag_plus
a3040 73 69 67 6e 20 29 20 20 20 20 20 20 20 20 70 72 sign ) pr
a3050 65 66 69 78 20 3d 20 27 2b 27 3b 0a 20 20 20 20 efix = '+';.
a3060 20 20 20 20 20 20 20 20 65 6c 73 65 20 69 66 28 else if(
a3070 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 flag_blanksign
a3080 29 20 20 70 72 65 66 69 78 20 3d 20 27 20 27 3b ) prefix = ' ';
a3090 0a 20 20 20 20 20 20 20 20 20 20 20 20 65 6c 73 . els
a30a0 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e
a30b0 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20 3d prefix =
a30c0 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 0;. }.
a30d0 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
a30e0 20 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 if( fla
a30f0 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 29 7b 0a 20 20 g_longlong ){.
a3100 20 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 longva
a3110 6c 75 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c lue = va_arg(ap,
a3120 75 36 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 u64);.
a3130 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 5f 6c }else if( flag_l
a3140 6f 6e 67 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ong ){.
a3150 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 76 longvalue = v
a3160 61 5f 61 72 67 28 61 70 2c 75 6e 73 69 67 6e 65 a_arg(ap,unsigne
a3170 64 20 6c 6f 6e 67 20 69 6e 74 29 3b 0a 20 20 20 d long int);.
a3180 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
a3190 20 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 longva
a31a0 6c 75 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c lue = va_arg(ap,
a31b0 75 6e 73 69 67 6e 65 64 20 69 6e 74 29 3b 0a 20 unsigned int);.
a31c0 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
a31d0 20 20 20 20 20 70 72 65 66 69 78 20 3d 20 30 3b prefix = 0;
a31e0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
a31f0 20 20 20 69 66 28 20 6c 6f 6e 67 76 61 6c 75 65 if( longvalue
a3200 3d 3d 30 20 29 20 66 6c 61 67 5f 61 6c 74 65 72 ==0 ) flag_alter
a3210 6e 61 74 65 66 6f 72 6d 20 3d 20 30 3b 0a 20 20 nateform = 0;.
a3220 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 7a if( flag_z
a3230 65 72 6f 70 61 64 20 26 26 20 70 72 65 63 69 73 eropad && precis
a3240 69 6f 6e 3c 77 69 64 74 68 2d 28 70 72 65 66 69 ion<width-(prefi
a3250 78 21 3d 30 29 20 29 7b 0a 20 20 20 20 20 20 20 x!=0) ){.
a3260 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 77 precision = w
a3270 69 64 74 68 2d 28 70 72 65 66 69 78 21 3d 30 29 idth-(prefix!=0)
a3280 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
a3290 20 20 20 20 62 75 66 70 74 20 3d 20 26 62 75 66 bufpt = &buf
a32a0 5b 65 74 42 55 46 53 49 5a 45 2d 31 5d 3b 0a 20 [etBUFSIZE-1];.
a32b0 20 20 20 20 20 20 20 69 66 28 20 78 74 79 70 65 if( xtype
a32c0 3d 3d 65 74 4f 52 44 49 4e 41 4c 20 29 7b 0a 20 ==etORDINAL ){.
a32d0 20 20 20 20 20 20 20 20 20 73 74 61 74 69 63 20 static
a32e0 63 6f 6e 73 74 20 63 68 61 72 20 7a 4f 72 64 5b const char zOrd[
a32f0 5d 20 3d 20 22 74 68 73 74 6e 64 72 64 22 3b 0a ] = "thstndrd";.
a3300 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 78 20 int x
a3310 3d 20 28 69 6e 74 29 28 6c 6f 6e 67 76 61 6c 75 = (int)(longvalu
a3320 65 20 25 20 31 30 29 3b 0a 20 20 20 20 20 20 20 e % 10);.
a3330 20 20 20 69 66 28 20 78 3e 3d 34 20 7c 7c 20 28 if( x>=4 || (
a3340 6c 6f 6e 67 76 61 6c 75 65 2f 31 30 29 25 31 30 longvalue/10)%10
a3350 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ==1 ){.
a3360 20 20 20 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 x = 0;.
a3370 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
a3380 62 75 66 5b 65 74 42 55 46 53 49 5a 45 2d 33 5d buf[etBUFSIZE-3]
a3390 20 3d 20 7a 4f 72 64 5b 78 2a 32 5d 3b 0a 20 20 = zOrd[x*2];.
a33a0 20 20 20 20 20 20 20 20 62 75 66 5b 65 74 42 55 buf[etBU
a33b0 46 53 49 5a 45 2d 32 5d 20 3d 20 7a 4f 72 64 5b FSIZE-2] = zOrd[
a33c0 78 2a 32 2b 31 5d 3b 0a 20 20 20 20 20 20 20 20 x*2+1];.
a33d0 20 20 62 75 66 70 74 20 2d 3d 20 32 3b 0a 20 20 bufpt -= 2;.
a33e0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
a33f0 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 67 69 {. regi
a3400 73 74 65 72 20 63 6f 6e 73 74 20 63 68 61 72 20 ster const char
a3410 2a 63 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 55 *cset; /* U
a3420 73 65 20 72 65 67 69 73 74 65 72 73 20 66 6f 72 se registers for
a3430 20 73 70 65 65 64 20 2a 2f 0a 20 20 20 20 20 20 speed */.
a3440 20 20 20 20 72 65 67 69 73 74 65 72 20 69 6e 74 register int
a3450 20 62 61 73 65 3b 0a 20 20 20 20 20 20 20 20 20 base;.
a3460 20 63 73 65 74 20 3d 20 26 61 44 69 67 69 74 73 cset = &aDigits
a3470 5b 69 6e 66 6f 70 2d 3e 63 68 61 72 73 65 74 5d [infop->charset]
a3480 3b 0a 20 20 20 20 20 20 20 20 20 20 62 61 73 65 ;. base
a3490 20 3d 20 69 6e 66 6f 70 2d 3e 62 61 73 65 3b 0a = infop->base;.
a34a0 20 20 20 20 20 20 20 20 20 20 64 6f 7b 20 20 20 do{
a34b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
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 2f 2a 20 43 6f 6e 76 65 /* Conve
a34e0 72 74 20 74 6f 20 61 73 63 69 69 20 2a 2f 0a 20 rt to ascii */.
a34f0 20 20 20 20 20 20 20 20 20 20 20 2a 28 2d 2d 62 *(--b
a3500 75 66 70 74 29 20 3d 20 63 73 65 74 5b 6c 6f 6e ufpt) = cset[lon
a3510 67 76 61 6c 75 65 25 62 61 73 65 5d 3b 0a 20 20 gvalue%base];.
a3520 20 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 longva
a3530 6c 75 65 20 3d 20 6c 6f 6e 67 76 61 6c 75 65 2f lue = longvalue/
a3540 62 61 73 65 3b 0a 20 20 20 20 20 20 20 20 20 20 base;.
a3550 7d 77 68 69 6c 65 28 20 6c 6f 6e 67 76 61 6c 75 }while( longvalu
a3560 65 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 7d e>0 );. }
a3570 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 . length
a3580 3d 20 28 69 6e 74 29 28 26 62 75 66 5b 65 74 42 = (int)(&buf[etB
a3590 55 46 53 49 5a 45 2d 31 5d 2d 62 75 66 70 74 29 UFSIZE-1]-bufpt)
a35a0 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 64 ;. for(id
a35b0 78 3d 70 72 65 63 69 73 69 6f 6e 2d 6c 65 6e 67 x=precision-leng
a35c0 74 68 3b 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d th; idx>0; idx--
a35d0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 2d ){. *(-
a35e0 2d 62 75 66 70 74 29 20 3d 20 27 30 27 3b 20 20 -bufpt) = '0';
a35f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a3600 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 5a 65 /* Ze
a3610 72 6f 20 70 61 64 20 2a 2f 0a 20 20 20 20 20 20 ro pad */.
a3620 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 }. if(
a3630 70 72 65 66 69 78 20 29 20 2a 28 2d 2d 62 75 66 prefix ) *(--buf
a3640 70 74 29 20 3d 20 70 72 65 66 69 78 3b 20 20 20 pt) = prefix;
a3650 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
a3660 64 64 20 73 69 67 6e 20 2a 2f 0a 20 20 20 20 20 dd sign */.
a3670 20 20 20 69 66 28 20 66 6c 61 67 5f 61 6c 74 65 if( flag_alte
a3680 72 6e 61 74 65 66 6f 72 6d 20 26 26 20 69 6e 66 rnateform && inf
a3690 6f 70 2d 3e 70 72 65 66 69 78 20 29 7b 20 20 20 op->prefix ){
a36a0 20 20 20 2f 2a 20 41 64 64 20 22 30 22 20 6f 72 /* Add "0" or
a36b0 20 22 30 78 22 20 2a 2f 0a 20 20 20 20 20 20 20 "0x" */.
a36c0 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 const char *p
a36d0 72 65 3b 0a 20 20 20 20 20 20 20 20 20 20 63 68 re;. ch
a36e0 61 72 20 78 3b 0a 20 20 20 20 20 20 20 20 20 20 ar x;.
a36f0 70 72 65 20 3d 20 26 61 50 72 65 66 69 78 5b 69 pre = &aPrefix[i
a3700 6e 66 6f 70 2d 3e 70 72 65 66 69 78 5d 3b 0a 20 nfop->prefix];.
a3710 20 20 20 20 20 20 20 20 20 66 6f 72 28 3b 20 28 for(; (
a3720 78 3d 28 2a 70 72 65 29 29 21 3d 30 3b 20 70 72 x=(*pre))!=0; pr
a3730 65 2b 2b 29 20 2a 28 2d 2d 62 75 66 70 74 29 20 e++) *(--bufpt)
a3740 3d 20 78 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 = x;. }.
a3750 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 length =
a3760 28 69 6e 74 29 28 26 62 75 66 5b 65 74 42 55 46 (int)(&buf[etBUF
a3770 53 49 5a 45 2d 31 5d 2d 62 75 66 70 74 29 3b 0a SIZE-1]-bufpt);.
a3780 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
a3790 20 20 20 20 20 63 61 73 65 20 65 74 46 4c 4f 41 case etFLOA
a37a0 54 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 T:. case et
a37b0 45 58 50 3a 0a 20 20 20 20 20 20 63 61 73 65 20 EXP:. case
a37c0 65 74 47 45 4e 45 52 49 43 3a 0a 20 20 20 20 20 etGENERIC:.
a37d0 20 20 20 72 65 61 6c 76 61 6c 75 65 20 3d 20 76 realvalue = v
a37e0 61 5f 61 72 67 28 61 70 2c 64 6f 75 62 6c 65 29 a_arg(ap,double)
a37f0 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
a3800 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 _OMIT_FLOATING_P
a3810 4f 49 4e 54 0a 20 20 20 20 20 20 20 20 69 66 28 OINT. if(
a3820 20 70 72 65 63 69 73 69 6f 6e 3c 30 20 29 20 70 precision<0 ) p
a3830 72 65 63 69 73 69 6f 6e 20 3d 20 36 3b 20 20 20 recision = 6;
a3840 20 20 20 20 20 20 2f 2a 20 53 65 74 20 64 65 66 /* Set def
a3850 61 75 6c 74 20 70 72 65 63 69 73 69 6f 6e 20 2a ault precision *
a3860 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 72 /. if( pr
a3870 65 63 69 73 69 6f 6e 3e 65 74 42 55 46 53 49 5a ecision>etBUFSIZ
a3880 45 2f 32 2d 31 30 20 29 20 70 72 65 63 69 73 69 E/2-10 ) precisi
a3890 6f 6e 20 3d 20 65 74 42 55 46 53 49 5a 45 2f 32 on = etBUFSIZE/2
a38a0 2d 31 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 -10;. if(
a38b0 20 72 65 61 6c 76 61 6c 75 65 3c 30 2e 30 20 29 realvalue<0.0 )
a38c0 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 61 6c {. real
a38d0 76 61 6c 75 65 20 3d 20 2d 72 65 61 6c 76 61 6c value = -realval
a38e0 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 70 72 ue;. pr
a38f0 65 66 69 78 20 3d 20 27 2d 27 3b 0a 20 20 20 20 efix = '-';.
a3900 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
a3910 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 70 6c if( flag_pl
a3920 75 73 73 69 67 6e 20 29 20 20 20 20 20 20 20 20 ussign )
a3930 20 20 70 72 65 66 69 78 20 3d 20 27 2b 27 3b 0a prefix = '+';.
a3940 20 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 69 else i
a3950 66 28 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 f( flag_blanksig
a3960 6e 20 29 20 20 20 20 70 72 65 66 69 78 20 3d 20 n ) prefix =
a3970 27 20 27 3b 0a 20 20 20 20 20 20 20 20 20 20 65 ' ';. e
a3980 6c 73 65 20 20 20 20 20 20 20 20 20 20 20 20 20 lse
a3990 20 20 20 20 20 20 20 20 20 20 20 20 70 72 65 66 pref
a39a0 69 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 ix = 0;.
a39b0 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 74 }. if( xt
a39c0 79 70 65 3d 3d 65 74 47 45 4e 45 52 49 43 20 26 ype==etGENERIC &
a39d0 26 20 70 72 65 63 69 73 69 6f 6e 3e 30 20 29 20 & precision>0 )
a39e0 70 72 65 63 69 73 69 6f 6e 2d 2d 3b 0a 23 69 66 precision--;.#if
a39f0 20 30 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 0. /* Ro
a3a00 75 6e 64 69 6e 67 20 77 6f 72 6b 73 20 6c 69 6b unding works lik
a3a10 65 20 42 53 44 20 77 68 65 6e 20 74 68 65 20 63 e BSD when the c
a3a20 6f 6e 73 74 61 6e 74 20 30 2e 34 39 39 39 20 69 onstant 0.4999 i
a3a30 73 20 75 73 65 64 2e 20 20 57 69 65 72 64 21 20 s used. Wierd!
a3a40 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 */. for(i
a3a50 64 78 3d 70 72 65 63 69 73 69 6f 6e 2c 20 72 6f dx=precision, ro
a3a60 75 6e 64 65 72 3d 30 2e 34 39 39 39 3b 20 69 64 under=0.4999; id
a3a70 78 3e 30 3b 20 69 64 78 2d 2d 2c 20 72 6f 75 6e x>0; idx--, roun
a3a80 64 65 72 2a 3d 30 2e 31 29 3b 0a 23 65 6c 73 65 der*=0.1);.#else
a3a90 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 74 20 6d . /* It m
a3aa0 61 6b 65 73 20 6d 6f 72 65 20 73 65 6e 73 65 20 akes more sense
a3ab0 74 6f 20 75 73 65 20 30 2e 35 20 2a 2f 0a 20 20 to use 0.5 */.
a3ac0 20 20 20 20 20 20 66 6f 72 28 69 64 78 3d 70 72 for(idx=pr
a3ad0 65 63 69 73 69 6f 6e 2c 20 72 6f 75 6e 64 65 72 ecision, rounder
a3ae0 3d 30 2e 35 3b 20 69 64 78 3e 30 3b 20 69 64 78 =0.5; idx>0; idx
a3af0 2d 2d 2c 20 72 6f 75 6e 64 65 72 2a 3d 30 2e 31 --, rounder*=0.1
a3b00 29 7b 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 ){}.#endif.
a3b10 20 20 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74 if( xtype==et
a3b20 46 4c 4f 41 54 20 29 20 72 65 61 6c 76 61 6c 75 FLOAT ) realvalu
a3b30 65 20 2b 3d 20 72 6f 75 6e 64 65 72 3b 0a 20 20 e += rounder;.
a3b40 20 20 20 20 20 20 2f 2a 20 4e 6f 72 6d 61 6c 69 /* Normali
a3b50 7a 65 20 72 65 61 6c 76 61 6c 75 65 20 74 6f 20 ze realvalue to
a3b60 77 69 74 68 69 6e 20 31 30 2e 30 20 3e 20 72 65 within 10.0 > re
a3b70 61 6c 76 61 6c 75 65 20 3e 3d 20 31 2e 30 20 2a alvalue >= 1.0 *
a3b80 2f 0a 20 20 20 20 20 20 20 20 65 78 70 20 3d 20 /. exp =
a3b90 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 0;. if( s
a3ba0 71 6c 69 74 65 33 49 73 4e 61 4e 28 28 64 6f 75 qlite3IsNaN((dou
a3bb0 62 6c 65 29 72 65 61 6c 76 61 6c 75 65 29 20 29 ble)realvalue) )
a3bc0 7b 0a 20 20 20 20 20 20 20 20 20 20 62 75 66 70 {. bufp
a3bd0 74 20 3d 20 22 4e 61 4e 22 3b 0a 20 20 20 20 20 t = "NaN";.
a3be0 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 33 3b length = 3;
a3bf0 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b . break
a3c00 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
a3c10 20 20 20 20 69 66 28 20 72 65 61 6c 76 61 6c 75 if( realvalu
a3c20 65 3e 30 2e 30 20 29 7b 0a 20 20 20 20 20 20 20 e>0.0 ){.
a3c30 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61 while( realva
a3c40 6c 75 65 3e 3d 31 65 33 32 20 26 26 20 65 78 70 lue>=1e32 && exp
a3c50 3c 3d 33 35 30 20 29 7b 20 72 65 61 6c 76 61 6c <=350 ){ realval
a3c60 75 65 20 2a 3d 20 31 65 2d 33 32 3b 20 65 78 70 ue *= 1e-32; exp
a3c70 2b 3d 33 32 3b 20 7d 0a 20 20 20 20 20 20 20 20 +=32; }.
a3c80 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61 6c while( realval
a3c90 75 65 3e 3d 31 65 38 20 26 26 20 65 78 70 3c 3d ue>=1e8 && exp<=
a3ca0 33 35 30 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 350 ){ realvalue
a3cb0 20 2a 3d 20 31 65 2d 38 3b 20 65 78 70 2b 3d 38 *= 1e-8; exp+=8
a3cc0 3b 20 7d 0a 20 20 20 20 20 20 20 20 20 20 77 68 ; }. wh
a3cd0 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d ile( realvalue>=
a3ce0 31 30 2e 30 20 26 26 20 65 78 70 3c 3d 33 35 30 10.0 && exp<=350
a3cf0 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d ){ realvalue *=
a3d00 20 30 2e 31 3b 20 65 78 70 2b 2b 3b 20 7d 0a 20 0.1; exp++; }.
a3d10 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 while(
a3d20 72 65 61 6c 76 61 6c 75 65 3c 31 65 2d 38 20 29 realvalue<1e-8 )
a3d30 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 31 { realvalue *= 1
a3d40 65 38 3b 20 65 78 70 2d 3d 38 3b 20 7d 0a 20 20 e8; exp-=8; }.
a3d50 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 72 while( r
a3d60 65 61 6c 76 61 6c 75 65 3c 31 2e 30 20 29 7b 20 ealvalue<1.0 ){
a3d70 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 31 30 2e realvalue *= 10.
a3d80 30 3b 20 65 78 70 2d 2d 3b 20 7d 0a 20 20 20 20 0; exp--; }.
a3d90 20 20 20 20 20 20 69 66 28 20 65 78 70 3e 33 35 if( exp>35
a3da0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 0 ){.
a3db0 20 69 66 28 20 70 72 65 66 69 78 3d 3d 27 2d 27 if( prefix=='-'
a3dc0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
a3dd0 20 20 62 75 66 70 74 20 3d 20 22 2d 49 6e 66 22 bufpt = "-Inf"
a3de0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 ;. }e
a3df0 6c 73 65 20 69 66 28 20 70 72 65 66 69 78 3d 3d lse if( prefix==
a3e00 27 2b 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 '+' ){.
a3e10 20 20 20 20 20 62 75 66 70 74 20 3d 20 22 2b 49 bufpt = "+I
a3e20 6e 66 22 3b 0a 20 20 20 20 20 20 20 20 20 20 20 nf";.
a3e30 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
a3e40 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 22 49 bufpt = "I
a3e50 6e 66 22 3b 0a 20 20 20 20 20 20 20 20 20 20 20 nf";.
a3e60 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c }. l
a3e70 65 6e 67 74 68 20 3d 20 73 71 6c 69 74 65 33 53 ength = sqlite3S
a3e80 74 72 6c 65 6e 33 30 28 62 75 66 70 74 29 3b 0a trlen30(bufpt);.
a3e90 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 brea
a3ea0 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 k;. }.
a3eb0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
a3ec0 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a 20 20 bufpt = buf;.
a3ed0 20 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 20 /*.
a3ee0 20 2a 2a 20 49 66 20 74 68 65 20 66 69 65 6c 64 ** If the field
a3ef0 20 74 79 70 65 20 69 73 20 65 74 47 45 4e 45 52 type is etGENER
a3f00 49 43 2c 20 74 68 65 6e 20 63 6f 6e 76 65 72 74 IC, then convert
a3f10 20 74 6f 20 65 69 74 68 65 72 20 65 74 45 58 50 to either etEXP
a3f20 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 65 . ** or e
a3f30 74 46 4c 4f 41 54 2c 20 61 73 20 61 70 70 72 6f tFLOAT, as appro
a3f40 70 72 69 61 74 65 2e 0a 20 20 20 20 20 20 20 20 priate..
a3f50 2a 2f 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f */. flag_
a3f60 65 78 70 20 3d 20 78 74 79 70 65 3d 3d 65 74 45 exp = xtype==etE
a3f70 58 50 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 XP;. if(
a3f80 78 74 79 70 65 21 3d 65 74 46 4c 4f 41 54 20 29 xtype!=etFLOAT )
a3f90 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 61 6c {. real
a3fa0 76 61 6c 75 65 20 2b 3d 20 72 6f 75 6e 64 65 72 value += rounder
a3fb0 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
a3fc0 72 65 61 6c 76 61 6c 75 65 3e 3d 31 30 2e 30 20 realvalue>=10.0
a3fd0 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 ){ realvalue *=
a3fe0 30 2e 31 3b 20 65 78 70 2b 2b 3b 20 7d 0a 20 20 0.1; exp++; }.
a3ff0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
a4000 69 66 28 20 78 74 79 70 65 3d 3d 65 74 47 45 4e if( xtype==etGEN
a4010 45 52 49 43 20 29 7b 0a 20 20 20 20 20 20 20 20 ERIC ){.
a4020 20 20 66 6c 61 67 5f 72 74 7a 20 3d 20 21 66 6c flag_rtz = !fl
a4030 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d ag_alternateform
a4040 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
a4050 65 78 70 3c 2d 34 20 7c 7c 20 65 78 70 3e 70 72 exp<-4 || exp>pr
a4060 65 63 69 73 69 6f 6e 20 29 7b 0a 20 20 20 20 20 ecision ){.
a4070 20 20 20 20 20 20 20 78 74 79 70 65 20 3d 20 65 xtype = e
a4080 74 45 58 50 3b 0a 20 20 20 20 20 20 20 20 20 20 tEXP;.
a4090 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
a40a0 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 70 precision = p
a40b0 72 65 63 69 73 69 6f 6e 20 2d 20 65 78 70 3b 0a recision - exp;.
a40c0 20 20 20 20 20 20 20 20 20 20 20 20 78 74 79 70 xtyp
a40d0 65 20 3d 20 65 74 46 4c 4f 41 54 3b 0a 20 20 20 e = etFLOAT;.
a40e0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
a40f0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
a4100 20 20 66 6c 61 67 5f 72 74 7a 20 3d 20 30 3b 0a flag_rtz = 0;.
a4110 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
a4120 20 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74 45 if( xtype==etE
a4130 58 50 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 XP ){.
a4140 65 32 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 e2 = 0;.
a4150 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
a4160 20 65 32 20 3d 20 65 78 70 3b 0a 20 20 20 20 20 e2 = exp;.
a4170 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6e 73 64 }. nsd
a4180 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 66 6c = 0;. fl
a4190 61 67 5f 64 70 20 3d 20 28 70 72 65 63 69 73 69 ag_dp = (precisi
a41a0 6f 6e 3e 30 20 3f 31 3a 30 29 20 7c 20 66 6c 61 on>0 ?1:0) | fla
a41b0 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20 g_alternateform
a41c0 7c 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 3b | flag_altform2;
a41d0 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 . /* The
a41e0 73 69 67 6e 20 69 6e 20 66 72 6f 6e 74 20 6f 66 sign in front of
a41f0 20 74 68 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 the number */.
a4200 20 20 20 20 20 20 20 69 66 28 20 70 72 65 66 69 if( prefi
a4210 78 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a x ){. *
a4220 28 62 75 66 70 74 2b 2b 29 20 3d 20 70 72 65 66 (bufpt++) = pref
a4230 69 78 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ix;. }.
a4240 20 20 20 20 20 20 2f 2a 20 44 69 67 69 74 73 20 /* Digits
a4250 70 72 69 6f 72 20 74 6f 20 74 68 65 20 64 65 63 prior to the dec
a4260 69 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 imal point */.
a4270 20 20 20 20 20 20 69 66 28 20 65 32 3c 30 20 29 if( e2<0 )
a4280 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 {. *(bu
a4290 66 70 74 2b 2b 29 20 3d 20 27 30 27 3b 0a 20 20 fpt++) = '0';.
a42a0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
a42b0 20 20 20 20 20 20 20 66 6f 72 28 3b 20 65 32 3e for(; e2>
a42c0 3d 30 3b 20 65 32 2d 2d 29 7b 0a 20 20 20 20 20 =0; e2--){.
a42d0 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b *(bufpt++
a42e0 29 20 3d 20 65 74 5f 67 65 74 64 69 67 69 74 28 ) = et_getdigit(
a42f0 26 72 65 61 6c 76 61 6c 75 65 2c 26 6e 73 64 29 &realvalue,&nsd)
a4300 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
a4310 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
a4320 2f 2a 20 54 68 65 20 64 65 63 69 6d 61 6c 20 70 /* The decimal p
a4330 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 oint */.
a4340 69 66 28 20 66 6c 61 67 5f 64 70 20 29 7b 0a 20 if( flag_dp ){.
a4350 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 *(bufpt
a4360 2b 2b 29 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20 ++) = '.';.
a4370 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 }. /*
a4380 22 30 22 20 64 69 67 69 74 73 20 61 66 74 65 72 "0" digits after
a4390 20 74 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 the decimal poi
a43a0 6e 74 20 62 75 74 20 62 65 66 6f 72 65 20 74 68 nt but before th
a43b0 65 20 66 69 72 73 74 0a 20 20 20 20 20 20 20 20 e first.
a43c0 2a 2a 20 73 69 67 6e 69 66 69 63 61 6e 74 20 64 ** significant d
a43d0 69 67 69 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 igit of the numb
a43e0 65 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f er */. fo
a43f0 72 28 65 32 2b 2b 3b 20 65 32 3c 30 3b 20 70 72 r(e2++; e2<0; pr
a4400 65 63 69 73 69 6f 6e 2d 2d 2c 20 65 32 2b 2b 29 ecision--, e2++)
a4410 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 {. asse
a4420 72 74 28 20 70 72 65 63 69 73 69 6f 6e 3e 30 20 rt( precision>0
a4430 29 3b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 );. *(b
a4440 75 66 70 74 2b 2b 29 20 3d 20 27 30 27 3b 0a 20 ufpt++) = '0';.
a4450 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
a4460 20 2f 2a 20 53 69 67 6e 69 66 69 63 61 6e 74 20 /* Significant
a4470 64 69 67 69 74 73 20 61 66 74 65 72 20 74 68 65 digits after the
a4480 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 2a decimal point *
a4490 2f 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 /. while(
a44a0 20 28 70 72 65 63 69 73 69 6f 6e 2d 2d 29 3e 30 (precision--)>0
a44b0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 ){. *(
a44c0 62 75 66 70 74 2b 2b 29 20 3d 20 65 74 5f 67 65 bufpt++) = et_ge
a44d0 74 64 69 67 69 74 28 26 72 65 61 6c 76 61 6c 75 tdigit(&realvalu
a44e0 65 2c 26 6e 73 64 29 3b 0a 20 20 20 20 20 20 20 e,&nsd);.
a44f0 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 65 }. /* Re
a4500 6d 6f 76 65 20 74 72 61 69 6c 69 6e 67 20 7a 65 move trailing ze
a4510 72 6f 73 20 61 6e 64 20 74 68 65 20 22 2e 22 20 ros and the "."
a4520 69 66 20 6e 6f 20 64 69 67 69 74 73 20 66 6f 6c if no digits fol
a4530 6c 6f 77 20 74 68 65 20 22 2e 22 20 2a 2f 0a 20 low the "." */.
a4540 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f if( flag_
a4550 72 74 7a 20 26 26 20 66 6c 61 67 5f 64 70 20 29 rtz && flag_dp )
a4560 7b 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c {. whil
a4570 65 28 20 62 75 66 70 74 5b 2d 31 5d 3d 3d 27 30 e( bufpt[-1]=='0
a4580 27 20 29 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d ' ) *(--bufpt) =
a4590 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 0;. as
a45a0 73 65 72 74 28 20 62 75 66 70 74 3e 62 75 66 20 sert( bufpt>buf
a45b0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
a45c0 20 62 75 66 70 74 5b 2d 31 5d 3d 3d 27 2e 27 20 bufpt[-1]=='.'
a45d0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 ){. i
a45e0 66 28 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 f( flag_altform2
a45f0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
a4600 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 *(bufpt++) = '
a4610 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 0';.
a4620 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
a4630 20 20 20 20 20 2a 28 2d 2d 62 75 66 70 74 29 20 *(--bufpt)
a4640 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 = 0;.
a4650 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
a4660 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
a4670 20 2f 2a 20 41 64 64 20 74 68 65 20 22 65 4e 4e /* Add the "eNN
a4680 4e 22 20 73 75 66 66 69 78 20 2a 2f 0a 20 20 20 N" suffix */.
a4690 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 65 78 if( flag_ex
a46a0 70 20 7c 7c 20 78 74 79 70 65 3d 3d 65 74 45 58 p || xtype==etEX
a46b0 50 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a P ){. *
a46c0 28 62 75 66 70 74 2b 2b 29 20 3d 20 61 44 69 67 (bufpt++) = aDig
a46d0 69 74 73 5b 69 6e 66 6f 70 2d 3e 63 68 61 72 73 its[infop->chars
a46e0 65 74 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 et];. i
a46f0 66 28 20 65 78 70 3c 30 20 29 7b 0a 20 20 20 20 f( exp<0 ){.
a4700 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b *(bufpt+
a4710 2b 29 20 3d 20 27 2d 27 3b 20 65 78 70 20 3d 20 +) = '-'; exp =
a4720 2d 65 78 70 3b 0a 20 20 20 20 20 20 20 20 20 20 -exp;.
a4730 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
a4740 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 *(bufpt++) =
a4750 27 2b 27 3b 0a 20 20 20 20 20 20 20 20 20 20 7d '+';. }
a4760 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 65 . if( e
a4770 78 70 3e 3d 31 30 30 20 29 7b 0a 20 20 20 20 20 xp>=100 ){.
a4780 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b *(bufpt++
a4790 29 20 3d 20 28 63 68 61 72 29 28 28 65 78 70 2f ) = (char)((exp/
a47a0 31 30 30 29 2b 27 30 27 29 3b 20 20 20 20 20 20 100)+'0');
a47b0 20 20 2f 2a 20 31 30 30 27 73 20 64 69 67 69 74 /* 100's digit
a47c0 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 */.
a47d0 65 78 70 20 25 3d 20 31 30 30 3b 0a 20 20 20 20 exp %= 100;.
a47e0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
a47f0 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 28 *(bufpt++) = (
a4800 63 68 61 72 29 28 65 78 70 2f 31 30 2b 27 30 27 char)(exp/10+'0'
a4810 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ); /
a4820 2a 20 31 30 27 73 20 64 69 67 69 74 20 2a 2f 0a * 10's digit */.
a4830 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 *(bufp
a4840 74 2b 2b 29 20 3d 20 28 63 68 61 72 29 28 65 78 t++) = (char)(ex
a4850 70 25 31 30 2b 27 30 27 29 3b 20 20 20 20 20 20 p%10+'0');
a4860 20 20 20 20 20 20 20 2f 2a 20 31 27 73 20 64 69 /* 1's di
a4870 67 69 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d git */. }
a4880 0a 20 20 20 20 20 20 20 20 2a 62 75 66 70 74 20 . *bufpt
a4890 3d 20 30 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a = 0;.. /*
a48a0 20 54 68 65 20 63 6f 6e 76 65 72 74 65 64 20 6e The converted n
a48b0 75 6d 62 65 72 20 69 73 20 69 6e 20 62 75 66 5b umber is in buf[
a48c0 5d 20 61 6e 64 20 7a 65 72 6f 20 74 65 72 6d 69 ] and zero termi
a48d0 6e 61 74 65 64 2e 20 4f 75 74 70 75 74 20 69 74 nated. Output it
a48e0 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 4e 6f 74 .. ** Not
a48f0 65 20 74 68 61 74 20 74 68 65 20 6e 75 6d 62 65 e that the numbe
a4900 72 20 69 73 20 69 6e 20 74 68 65 20 75 73 75 61 r is in the usua
a4910 6c 20 6f 72 64 65 72 2c 20 6e 6f 74 20 72 65 76 l order, not rev
a4920 65 72 73 65 64 20 61 73 20 77 69 74 68 0a 20 20 ersed as with.
a4930 20 20 20 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 ** integer
a4940 20 63 6f 6e 76 65 72 73 69 6f 6e 73 2e 20 2a 2f conversions. */
a4950 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 . length
a4960 3d 20 28 69 6e 74 29 28 62 75 66 70 74 2d 62 75 = (int)(bufpt-bu
a4970 66 29 3b 0a 20 20 20 20 20 20 20 20 62 75 66 70 f);. bufp
a4980 74 20 3d 20 62 75 66 3b 0a 0a 20 20 20 20 20 20 t = buf;..
a4990 20 20 2f 2a 20 53 70 65 63 69 61 6c 20 63 61 73 /* Special cas
a49a0 65 3a 20 20 41 64 64 20 6c 65 61 64 69 6e 67 20 e: Add leading
a49b0 7a 65 72 6f 73 20 69 66 20 74 68 65 20 66 6c 61 zeros if the fla
a49c0 67 5f 7a 65 72 6f 70 61 64 20 66 6c 61 67 20 69 g_zeropad flag i
a49d0 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 65 74 s. ** set
a49e0 20 61 6e 64 20 77 65 20 61 72 65 20 6e 6f 74 20 and we are not
a49f0 6c 65 66 74 20 6a 75 73 74 69 66 69 65 64 20 2a left justified *
a4a00 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 6c /. if( fl
a4a10 61 67 5f 7a 65 72 6f 70 61 64 20 26 26 20 21 66 ag_zeropad && !f
a4a20 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 lag_leftjustify
a4a30 26 26 20 6c 65 6e 67 74 68 20 3c 20 77 69 64 74 && length < widt
a4a40 68 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e h){. in
a4a50 74 20 69 3b 0a 20 20 20 20 20 20 20 20 20 20 69 t i;. i
a4a60 6e 74 20 6e 50 61 64 20 3d 20 77 69 64 74 68 20 nt nPad = width
a4a70 2d 20 6c 65 6e 67 74 68 3b 0a 20 20 20 20 20 20 - length;.
a4a80 20 20 20 20 66 6f 72 28 69 3d 77 69 64 74 68 3b for(i=width;
a4a90 20 69 3e 3d 6e 50 61 64 3b 20 69 2d 2d 29 7b 0a i>=nPad; i--){.
a4aa0 20 20 20 20 20 20 20 20 20 20 20 20 62 75 66 70 bufp
a4ab0 74 5b 69 5d 20 3d 20 62 75 66 70 74 5b 69 2d 6e t[i] = bufpt[i-n
a4ac0 50 61 64 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 Pad];.
a4ad0 7d 0a 20 20 20 20 20 20 20 20 20 20 69 20 3d 20 }. i =
a4ae0 70 72 65 66 69 78 21 3d 30 3b 0a 20 20 20 20 20 prefix!=0;.
a4af0 20 20 20 20 20 77 68 69 6c 65 28 20 6e 50 61 64 while( nPad
a4b00 2d 2d 20 29 20 62 75 66 70 74 5b 69 2b 2b 5d 20 -- ) bufpt[i++]
a4b10 3d 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 20 = '0';.
a4b20 20 6c 65 6e 67 74 68 20 3d 20 77 69 64 74 68 3b length = width;
a4b30 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 . }.#endi
a4b40 66 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b f. break;
a4b50 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 53 49 . case etSI
a4b60 5a 45 3a 0a 20 20 20 20 20 20 20 20 2a 28 76 61 ZE:. *(va
a4b70 5f 61 72 67 28 61 70 2c 69 6e 74 2a 29 29 20 3d _arg(ap,int*)) =
a4b80 20 70 41 63 63 75 6d 2d 3e 6e 43 68 61 72 3b 0a pAccum->nChar;.
a4b90 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d length =
a4ba0 20 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 20 20 width = 0;.
a4bb0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
a4bc0 20 63 61 73 65 20 65 74 50 45 52 43 45 4e 54 3a case etPERCENT:
a4bd0 0a 20 20 20 20 20 20 20 20 62 75 66 5b 30 5d 20 . buf[0]
a4be0 3d 20 27 25 27 3b 0a 20 20 20 20 20 20 20 20 62 = '%';. b
a4bf0 75 66 70 74 20 3d 20 62 75 66 3b 0a 20 20 20 20 ufpt = buf;.
a4c00 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 31 3b 0a length = 1;.
a4c10 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
a4c20 20 20 20 20 20 63 61 73 65 20 65 74 43 48 41 52 case etCHAR
a4c30 58 3a 0a 20 20 20 20 20 20 20 20 63 20 3d 20 76 X:. c = v
a4c40 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 a_arg(ap,int);.
a4c50 20 20 20 20 20 20 20 62 75 66 5b 30 5d 20 3d 20 buf[0] =
a4c60 28 63 68 61 72 29 63 3b 0a 20 20 20 20 20 20 20 (char)c;.
a4c70 20 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3e 3d if( precision>=
a4c80 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 0 ){. f
a4c90 6f 72 28 69 64 78 3d 31 3b 20 69 64 78 3c 70 72 or(idx=1; idx<pr
a4ca0 65 63 69 73 69 6f 6e 3b 20 69 64 78 2b 2b 29 20 ecision; idx++)
a4cb0 62 75 66 5b 69 64 78 5d 20 3d 20 28 63 68 61 72 buf[idx] = (char
a4cc0 29 63 3b 0a 20 20 20 20 20 20 20 20 20 20 6c 65 )c;. le
a4cd0 6e 67 74 68 20 3d 20 70 72 65 63 69 73 69 6f 6e ngth = precision
a4ce0 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
a4cf0 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 . lengt
a4d00 68 20 3d 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a h =1;. }.
a4d10 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 bufpt =
a4d20 62 75 66 3b 0a 20 20 20 20 20 20 20 20 62 72 65 buf;. bre
a4d30 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65 ak;. case e
a4d40 74 53 54 52 49 4e 47 3a 0a 20 20 20 20 20 20 63 tSTRING:. c
a4d50 61 73 65 20 65 74 44 59 4e 53 54 52 49 4e 47 3a ase etDYNSTRING:
a4d60 0a 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d . bufpt =
a4d70 20 76 61 5f 61 72 67 28 61 70 2c 63 68 61 72 2a va_arg(ap,char*
a4d80 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 62 );. if( b
a4d90 75 66 70 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 ufpt==0 ){.
a4da0 20 20 20 20 20 62 75 66 70 74 20 3d 20 22 22 3b bufpt = "";
a4db0 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 . }else i
a4dc0 66 28 20 78 74 79 70 65 3d 3d 65 74 44 59 4e 53 f( xtype==etDYNS
a4dd0 54 52 49 4e 47 20 29 7b 0a 20 20 20 20 20 20 20 TRING ){.
a4de0 20 20 20 7a 45 78 74 72 61 20 3d 20 62 75 66 70 zExtra = bufp
a4df0 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 t;. }.
a4e00 20 20 20 20 20 69 66 28 20 70 72 65 63 69 73 69 if( precisi
a4e10 6f 6e 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 on>=0 ){.
a4e20 20 20 20 66 6f 72 28 6c 65 6e 67 74 68 3d 30 3b for(length=0;
a4e30 20 6c 65 6e 67 74 68 3c 70 72 65 63 69 73 69 6f length<precisio
a4e40 6e 20 26 26 20 62 75 66 70 74 5b 6c 65 6e 67 74 n && bufpt[lengt
a4e50 68 5d 3b 20 6c 65 6e 67 74 68 2b 2b 29 7b 7d 0a h]; length++){}.
a4e60 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
a4e70 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 length
a4e80 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 = sqlite3Strlen3
a4e90 30 28 62 75 66 70 74 29 3b 0a 20 20 20 20 20 20 0(bufpt);.
a4ea0 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 }. brea
a4eb0 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 k;. case et
a4ec0 53 51 4c 45 53 43 41 50 45 3a 0a 20 20 20 20 20 SQLESCAPE:.
a4ed0 20 63 61 73 65 20 65 74 53 51 4c 45 53 43 41 50 case etSQLESCAP
a4ee0 45 32 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65 E2:. case e
a4ef0 74 53 51 4c 45 53 43 41 50 45 33 3a 20 7b 0a 20 tSQLESCAPE3: {.
a4f00 20 20 20 20 20 20 20 69 6e 74 20 69 2c 20 6a 2c int i, j,
a4f10 20 6e 2c 20 69 73 6e 75 6c 6c 3b 0a 20 20 20 20 n, isnull;.
a4f20 20 20 20 20 69 6e 74 20 6e 65 65 64 51 75 6f 74 int needQuot
a4f30 65 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 e;. char
a4f40 63 68 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 ch;. char
a4f50 20 71 20 3d 20 28 28 78 74 79 70 65 3d 3d 65 74 q = ((xtype==et
a4f60 53 51 4c 45 53 43 41 50 45 33 29 3f 27 22 27 3a SQLESCAPE3)?'"':
a4f70 27 5c 27 27 29 3b 20 20 20 2f 2a 20 51 75 6f 74 '\''); /* Quot
a4f80 65 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 e character */.
a4f90 20 20 20 20 20 20 20 63 68 61 72 20 2a 65 73 63 char *esc
a4fa0 61 72 67 20 3d 20 76 61 5f 61 72 67 28 61 70 2c arg = va_arg(ap,
a4fb0 63 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 20 20 char*);.
a4fc0 69 73 6e 75 6c 6c 20 3d 20 65 73 63 61 72 67 3d isnull = escarg=
a4fd0 3d 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 =0;. if(
a4fe0 69 73 6e 75 6c 6c 20 29 20 65 73 63 61 72 67 20 isnull ) escarg
a4ff0 3d 20 28 78 74 79 70 65 3d 3d 65 74 53 51 4c 45 = (xtype==etSQLE
a5000 53 43 41 50 45 32 20 3f 20 22 4e 55 4c 4c 22 20 SCAPE2 ? "NULL"
a5010 3a 20 22 28 4e 55 4c 4c 29 22 29 3b 0a 20 20 20 : "(NULL)");.
a5020 20 20 20 20 20 66 6f 72 28 69 3d 6e 3d 30 3b 20 for(i=n=0;
a5030 28 63 68 3d 65 73 63 61 72 67 5b 69 5d 29 21 3d (ch=escarg[i])!=
a5040 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 0; i++){.
a5050 20 20 20 69 66 28 20 63 68 3d 3d 71 20 29 20 20 if( ch==q )
a5060 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 n++;. }.
a5070 20 20 20 20 20 20 20 6e 65 65 64 51 75 6f 74 65 needQuote
a5080 20 3d 20 21 69 73 6e 75 6c 6c 20 26 26 20 78 74 = !isnull && xt
a5090 79 70 65 3d 3d 65 74 53 51 4c 45 53 43 41 50 45 ype==etSQLESCAPE
a50a0 32 3b 0a 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 2;. n +=
a50b0 69 20 2b 20 31 20 2b 20 6e 65 65 64 51 75 6f 74 i + 1 + needQuot
a50c0 65 2a 32 3b 0a 20 20 20 20 20 20 20 20 69 66 28 e*2;. if(
a50d0 20 6e 3e 65 74 42 55 46 53 49 5a 45 20 29 7b 0a n>etBUFSIZE ){.
a50e0 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74 20 bufpt
a50f0 3d 20 7a 45 78 74 72 61 20 3d 20 73 71 6c 69 74 = zExtra = sqlit
a5100 65 33 4d 61 6c 6c 6f 63 28 20 6e 20 29 3b 0a 20 e3Malloc( n );.
a5110 20 20 20 20 20 20 20 20 20 69 66 28 20 62 75 66 if( buf
a5120 70 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 pt==0 ){.
a5130 20 20 20 20 20 70 41 63 63 75 6d 2d 3e 6d 61 6c pAccum->mal
a5140 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 locFailed = 1;.
a5150 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 retur
a5160 6e 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 n;. }.
a5170 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
a5180 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 bufpt =
a5190 62 75 66 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 buf;. }.
a51a0 20 20 20 20 20 20 20 6a 20 3d 20 30 3b 0a 20 20 j = 0;.
a51b0 20 20 20 20 20 20 69 66 28 20 6e 65 65 64 51 75 if( needQu
a51c0 6f 74 65 20 29 20 62 75 66 70 74 5b 6a 2b 2b 5d ote ) bufpt[j++]
a51d0 20 3d 20 71 3b 0a 20 20 20 20 20 20 20 20 66 6f = q;. fo
a51e0 72 28 69 3d 30 3b 20 28 63 68 3d 65 73 63 61 72 r(i=0; (ch=escar
a51f0 67 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a g[i])!=0; i++){.
a5200 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74 5b bufpt[
a5210 6a 2b 2b 5d 20 3d 20 63 68 3b 0a 20 20 20 20 20 j++] = ch;.
a5220 20 20 20 20 20 69 66 28 20 63 68 3d 3d 71 20 29 if( ch==q )
a5230 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 63 68 bufpt[j++] = ch
a5240 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
a5250 20 20 20 20 69 66 28 20 6e 65 65 64 51 75 6f 74 if( needQuot
a5260 65 20 29 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d e ) bufpt[j++] =
a5270 20 71 3b 0a 20 20 20 20 20 20 20 20 62 75 66 70 q;. bufp
a5280 74 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 t[j] = 0;.
a5290 20 20 6c 65 6e 67 74 68 20 3d 20 6a 3b 0a 20 20 length = j;.
a52a0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 72 65 /* The pre
a52b0 63 69 73 69 6f 6e 20 69 73 20 69 67 6e 6f 72 65 cision is ignore
a52c0 64 20 6f 6e 20 25 71 20 61 6e 64 20 25 51 20 2a d on %q and %Q *
a52d0 2f 0a 20 20 20 20 20 20 20 20 2f 2a 20 69 66 28 /. /* if(
a52e0 20 70 72 65 63 69 73 69 6f 6e 3e 3d 30 20 26 26 precision>=0 &&
a52f0 20 70 72 65 63 69 73 69 6f 6e 3c 6c 65 6e 67 74 precision<lengt
a5300 68 20 29 20 6c 65 6e 67 74 68 20 3d 20 70 72 65 h ) length = pre
a5310 63 69 73 69 6f 6e 3b 20 2a 2f 0a 20 20 20 20 20 cision; */.
a5320 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
a5330 7d 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 54 }. case etT
a5340 4f 4b 45 4e 3a 20 7b 0a 20 20 20 20 20 20 20 20 OKEN: {.
a5350 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 20 3d 20 Token *pToken =
a5360 76 61 5f 61 72 67 28 61 70 2c 20 54 6f 6b 65 6e va_arg(ap, Token
a5370 2a 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 *);. if(
a5380 70 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 20 20 pToken ){.
a5390 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 sqlite3StrAc
a53a0 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d cumAppend(pAccum
a53b0 2c 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 , (const char*)p
a53c0 54 6f 6b 65 6e 2d 3e 7a 2c 20 70 54 6f 6b 65 6e Token->z, pToken
a53d0 2d 3e 6e 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a ->n);. }.
a53e0 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d length =
a53f0 20 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 20 20 width = 0;.
a5400 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
a5410 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 }. case et
a5420 53 52 43 4c 49 53 54 3a 20 7b 0a 20 20 20 20 20 SRCLIST: {.
a5430 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 SrcList *pSrc
a5440 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 53 72 = va_arg(ap, Sr
a5450 63 4c 69 73 74 2a 29 3b 0a 20 20 20 20 20 20 20 cList*);.
a5460 20 69 6e 74 20 6b 20 3d 20 76 61 5f 61 72 67 28 int k = va_arg(
a5470 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 ap, int);.
a5480 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 struct SrcList
a5490 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 _item *pItem = &
a54a0 70 53 72 63 2d 3e 61 5b 6b 5d 3b 0a 20 20 20 20 pSrc->a[k];.
a54b0 20 20 20 20 61 73 73 65 72 74 28 20 6b 3e 3d 30 assert( k>=0
a54c0 20 26 26 20 6b 3c 70 53 72 63 2d 3e 6e 53 72 63 && k<pSrc->nSrc
a54d0 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 );. if(
a54e0 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 pItem->zDatabase
a54f0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 ){. sq
a5500 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 lite3StrAccumApp
a5510 65 6e 64 28 70 41 63 63 75 6d 2c 20 70 49 74 65 end(pAccum, pIte
a5520 6d 2d 3e 7a 44 61 74 61 62 61 73 65 2c 20 2d 31 m->zDatabase, -1
a5530 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c );. sql
a5540 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 ite3StrAccumAppe
a5550 6e 64 28 70 41 63 63 75 6d 2c 20 22 2e 22 2c 20 nd(pAccum, ".",
a5560 31 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 1);. }.
a5570 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 sqlite3Str
a5580 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 AccumAppend(pAcc
a5590 75 6d 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 um, pItem->zName
a55a0 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 6c , -1);. l
a55b0 65 6e 67 74 68 20 3d 20 77 69 64 74 68 20 3d 20 ength = width =
a55c0 30 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 0;. break
a55d0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
a55e0 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 default: {.
a55f0 20 20 20 61 73 73 65 72 74 28 20 78 74 79 70 65 assert( xtype
a5600 3d 3d 65 74 49 4e 56 41 4c 49 44 20 29 3b 0a 20 ==etINVALID );.
a5610 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 return;.
a5620 20 20 20 20 20 7d 0a 20 20 20 20 7d 2f 2a 20 45 }. }/* E
a5630 6e 64 20 73 77 69 74 63 68 20 6f 76 65 72 20 74 nd switch over t
a5640 68 65 20 66 6f 72 6d 61 74 20 74 79 70 65 20 2a he format type *
a5650 2f 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 /. /*. **
a5660 54 68 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 The text of the
a5670 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 6f conversion is po
a5680 69 6e 74 65 64 20 74 6f 20 62 79 20 22 62 75 66 inted to by "buf
a5690 70 74 22 20 61 6e 64 20 69 73 0a 20 20 20 20 2a pt" and is. *
a56a0 2a 20 22 6c 65 6e 67 74 68 22 20 63 68 61 72 61 * "length" chara
a56b0 63 74 65 72 73 20 6c 6f 6e 67 2e 20 20 54 68 65 cters long. The
a56c0 20 66 69 65 6c 64 20 77 69 64 74 68 20 69 73 20 field width is
a56d0 22 77 69 64 74 68 22 2e 20 20 44 6f 0a 20 20 20 "width". Do.
a56e0 20 2a 2a 20 74 68 65 20 6f 75 74 70 75 74 2e 0a ** the output..
a56f0 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 */. if( !
a5700 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 flag_leftjustify
a5710 20 29 7b 0a 20 20 20 20 20 20 72 65 67 69 73 74 ){. regist
a5720 65 72 20 69 6e 74 20 6e 73 70 61 63 65 3b 0a 20 er int nspace;.
a5730 20 20 20 20 20 6e 73 70 61 63 65 20 3d 20 77 69 nspace = wi
a5740 64 74 68 2d 6c 65 6e 67 74 68 3b 0a 20 20 20 20 dth-length;.
a5750 20 20 69 66 28 20 6e 73 70 61 63 65 3e 30 20 29 if( nspace>0 )
a5760 7b 0a 20 20 20 20 20 20 20 20 61 70 70 65 6e 64 {. append
a5770 53 70 61 63 65 28 70 41 63 63 75 6d 2c 20 6e 73 Space(pAccum, ns
a5780 70 61 63 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 pace);. }.
a5790 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6c 65 6e }. if( len
a57a0 67 74 68 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 gth>0 ){. s
a57b0 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 qlite3StrAccumAp
a57c0 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 62 75 66 pend(pAccum, buf
a57d0 70 74 2c 20 6c 65 6e 67 74 68 29 3b 0a 20 20 20 pt, length);.
a57e0 20 7d 0a 20 20 20 20 69 66 28 20 66 6c 61 67 5f }. if( flag_
a57f0 6c 65 66 74 6a 75 73 74 69 66 79 20 29 7b 0a 20 leftjustify ){.
a5800 20 20 20 20 20 72 65 67 69 73 74 65 72 20 69 6e register in
a5810 74 20 6e 73 70 61 63 65 3b 0a 20 20 20 20 20 20 t nspace;.
a5820 6e 73 70 61 63 65 20 3d 20 77 69 64 74 68 2d 6c nspace = width-l
a5830 65 6e 67 74 68 3b 0a 20 20 20 20 20 20 69 66 28 ength;. if(
a5840 20 6e 73 70 61 63 65 3e 30 20 29 7b 0a 20 20 20 nspace>0 ){.
a5850 20 20 20 20 20 61 70 70 65 6e 64 53 70 61 63 65 appendSpace
a5860 28 70 41 63 63 75 6d 2c 20 6e 73 70 61 63 65 29 (pAccum, nspace)
a5870 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
a5880 20 20 20 20 69 66 28 20 7a 45 78 74 72 61 20 29 if( zExtra )
a5890 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f {. sqlite3_
a58a0 66 72 65 65 28 7a 45 78 74 72 61 29 3b 0a 20 20 free(zExtra);.
a58b0 20 20 7d 0a 20 20 7d 2f 2a 20 45 6e 64 20 66 6f }. }/* End fo
a58c0 72 20 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65 20 r loop over the
a58d0 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f format string */
a58e0 0a 7d 20 2f 2a 20 45 6e 64 20 6f 66 20 66 75 6e .} /* End of fun
a58f0 63 74 69 6f 6e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 ction */../*.**
a5900 41 70 70 65 6e 64 20 4e 20 62 79 74 65 73 20 6f Append N bytes o
a5910 66 20 74 65 78 74 20 66 72 6f 6d 20 7a 20 74 6f f text from z to
a5920 20 74 68 65 20 53 74 72 41 63 63 75 6d 20 6f 62 the StrAccum ob
a5930 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ject..*/.SQLITE_
a5940 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
a5950 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 ite3StrAccumAppe
a5960 6e 64 28 53 74 72 41 63 63 75 6d 20 2a 70 2c 20 nd(StrAccum *p,
a5970 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 const char *z, i
a5980 6e 74 20 4e 29 7b 0a 20 20 61 73 73 65 72 74 28 nt N){. assert(
a5990 20 7a 21 3d 30 20 7c 7c 20 4e 3d 3d 30 20 29 3b z!=0 || N==0 );
a59a0 0a 20 20 69 66 28 20 70 2d 3e 74 6f 6f 42 69 67 . if( p->tooBig
a59b0 20 7c 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c | p->mallocFail
a59c0 65 64 20 29 7b 0a 20 20 20 20 74 65 73 74 63 61 ed ){. testca
a59d0 73 65 28 70 2d 3e 74 6f 6f 42 69 67 29 3b 0a 20 se(p->tooBig);.
a59e0 20 20 20 74 65 73 74 63 61 73 65 28 70 2d 3e 6d testcase(p->m
a59f0 61 6c 6c 6f 63 46 61 69 6c 65 64 29 3b 0a 20 20 allocFailed);.
a5a00 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 return;. }.
a5a10 69 66 28 20 4e 3c 30 20 29 7b 0a 20 20 20 20 4e if( N<0 ){. N
a5a20 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e = sqlite3Strlen
a5a30 33 30 28 7a 29 3b 0a 20 20 7d 0a 20 20 69 66 28 30(z);. }. if(
a5a40 20 4e 3d 3d 30 20 7c 7c 20 4e 45 56 45 52 28 7a N==0 || NEVER(z
a5a50 3d 3d 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75 ==0) ){. retu
a5a60 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d rn;. }. if( p-
a5a70 3e 6e 43 68 61 72 2b 4e 20 3e 3d 20 70 2d 3e 6e >nChar+N >= p->n
a5a80 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 63 68 61 Alloc ){. cha
a5a90 72 20 2a 7a 4e 65 77 3b 0a 20 20 20 20 69 66 28 r *zNew;. if(
a5aa0 20 21 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 29 !p->useMalloc )
a5ab0 7b 0a 20 20 20 20 20 20 70 2d 3e 74 6f 6f 42 69 {. p->tooBi
a5ac0 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 4e 20 3d g = 1;. N =
a5ad0 20 70 2d 3e 6e 41 6c 6c 6f 63 20 2d 20 70 2d 3e p->nAlloc - p->
a5ae0 6e 43 68 61 72 20 2d 20 31 3b 0a 20 20 20 20 20 nChar - 1;.
a5af0 20 69 66 28 20 4e 3c 3d 30 20 29 7b 0a 20 20 20 if( N<=0 ){.
a5b00 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 return;.
a5b10 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
a5b20 20 20 20 20 20 20 69 36 34 20 73 7a 4e 65 77 20 i64 szNew
a5b30 3d 20 70 2d 3e 6e 43 68 61 72 3b 0a 20 20 20 20 = p->nChar;.
a5b40 20 20 73 7a 4e 65 77 20 2b 3d 20 4e 20 2b 20 31 szNew += N + 1
a5b50 3b 0a 20 20 20 20 20 20 69 66 28 20 73 7a 4e 65 ;. if( szNe
a5b60 77 20 3e 20 70 2d 3e 6d 78 41 6c 6c 6f 63 20 29 w > p->mxAlloc )
a5b70 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
a5b80 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 70 3StrAccumReset(p
a5b90 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 74 6f );. p->to
a5ba0 6f 42 69 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 oBig = 1;.
a5bb0 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 return;.
a5bc0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 }else{. p
a5bd0 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 28 69 6e 74 29 ->nAlloc = (int)
a5be0 73 7a 4e 65 77 3b 0a 20 20 20 20 20 20 7d 0a 20 szNew;. }.
a5bf0 20 20 20 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 zNew = sqli
a5c00 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 te3DbMallocRaw(p
a5c10 2d 3e 64 62 2c 20 70 2d 3e 6e 41 6c 6c 6f 63 20 ->db, p->nAlloc
a5c20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 4e 65 );. if( zNe
a5c30 77 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d w ){. mem
a5c40 63 70 79 28 7a 4e 65 77 2c 20 70 2d 3e 7a 54 65 cpy(zNew, p->zTe
a5c50 78 74 2c 20 70 2d 3e 6e 43 68 61 72 29 3b 0a 20 xt, p->nChar);.
a5c60 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 sqlite3St
a5c70 72 41 63 63 75 6d 52 65 73 65 74 28 70 29 3b 0a rAccumReset(p);.
a5c80 20 20 20 20 20 20 20 20 70 2d 3e 7a 54 65 78 74 p->zText
a5c90 20 3d 20 7a 4e 65 77 3b 0a 20 20 20 20 20 20 7d = zNew;. }
a5ca0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 2d else{. p-
a5cb0 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 >mallocFailed =
a5cc0 31 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 1;. sqlit
a5cd0 65 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 e3StrAccumReset(
a5ce0 70 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 p);. retu
a5cf0 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 rn;. }.
a5d00 7d 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 26 }. }. memcpy(&
a5d10 70 2d 3e 7a 54 65 78 74 5b 70 2d 3e 6e 43 68 61 p->zText[p->nCha
a5d20 72 5d 2c 20 7a 2c 20 4e 29 3b 0a 20 20 70 2d 3e r], z, N);. p->
a5d30 6e 43 68 61 72 20 2b 3d 20 4e 3b 0a 7d 0a 0a 2f nChar += N;.}../
a5d40 2a 0a 2a 2a 20 46 69 6e 69 73 68 20 6f 66 66 20 *.** Finish off
a5d50 61 20 73 74 72 69 6e 67 20 62 79 20 6d 61 6b 69 a string by maki
a5d60 6e 67 20 73 75 72 65 20 69 74 20 69 73 20 7a 65 ng sure it is ze
a5d70 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 0a 2a ro-terminated..*
a5d80 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 * Return a point
a5d90 65 72 20 74 6f 20 74 68 65 20 72 65 73 75 6c 74 er to the result
a5da0 69 6e 67 20 73 74 72 69 6e 67 2e 20 20 52 65 74 ing string. Ret
a5db0 75 72 6e 20 61 20 4e 55 4c 4c 0a 2a 2a 20 70 6f urn a NULL.** po
a5dc0 69 6e 74 65 72 20 69 66 20 61 6e 79 20 6b 69 6e inter if any kin
a5dd0 64 20 6f 66 20 65 72 72 6f 72 20 77 61 73 20 65 d of error was e
a5de0 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2f 0a 53 ncountered..*/.S
a5df0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 QLITE_PRIVATE ch
a5e00 61 72 20 2a 73 71 6c 69 74 65 33 53 74 72 41 63 ar *sqlite3StrAc
a5e10 63 75 6d 46 69 6e 69 73 68 28 53 74 72 41 63 63 cumFinish(StrAcc
a5e20 75 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d um *p){. if( p-
a5e30 3e 7a 54 65 78 74 20 29 7b 0a 20 20 20 20 70 2d >zText ){. p-
a5e40 3e 7a 54 65 78 74 5b 70 2d 3e 6e 43 68 61 72 5d >zText[p->nChar]
a5e50 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 70 2d = 0;. if( p-
a5e60 3e 75 73 65 4d 61 6c 6c 6f 63 20 26 26 20 70 2d >useMalloc && p-
a5e70 3e 7a 54 65 78 74 3d 3d 70 2d 3e 7a 42 61 73 65 >zText==p->zBase
a5e80 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 7a 54 65 ){. p->zTe
a5e90 78 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 xt = sqlite3DbMa
a5ea0 6c 6c 6f 63 52 61 77 28 70 2d 3e 64 62 2c 20 70 llocRaw(p->db, p
a5eb0 2d 3e 6e 43 68 61 72 2b 31 20 29 3b 0a 20 20 20 ->nChar+1 );.
a5ec0 20 20 20 69 66 28 20 70 2d 3e 7a 54 65 78 74 20 if( p->zText
a5ed0 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 ){. memcp
a5ee0 79 28 70 2d 3e 7a 54 65 78 74 2c 20 70 2d 3e 7a y(p->zText, p->z
a5ef0 42 61 73 65 2c 20 70 2d 3e 6e 43 68 61 72 2b 31 Base, p->nChar+1
a5f00 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a );. }else{.
a5f10 20 20 20 20 20 20 20 20 70 2d 3e 6d 61 6c 6c 6f p->mallo
a5f20 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 cFailed = 1;.
a5f30 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
a5f40 20 72 65 74 75 72 6e 20 70 2d 3e 7a 54 65 78 74 return p->zText
a5f50 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 65 74 ;.}../*.** Reset
a5f60 20 61 6e 20 53 74 72 41 63 63 75 6d 20 73 74 72 an StrAccum str
a5f70 69 6e 67 2e 20 20 52 65 63 6c 61 69 6d 20 61 6c ing. Reclaim al
a5f80 6c 20 6d 61 6c 6c 6f 63 65 64 20 6d 65 6d 6f 72 l malloced memor
a5f90 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 y..*/.SQLITE_PRI
a5fa0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
a5fb0 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 53 3StrAccumReset(S
a5fc0 74 72 41 63 63 75 6d 20 2a 70 29 7b 0a 20 20 69 trAccum *p){. i
a5fd0 66 28 20 70 2d 3e 7a 54 65 78 74 21 3d 70 2d 3e f( p->zText!=p->
a5fe0 7a 42 61 73 65 20 29 7b 0a 20 20 20 20 73 71 6c zBase ){. sql
a5ff0 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62 ite3DbFree(p->db
a6000 2c 20 70 2d 3e 7a 54 65 78 74 29 3b 0a 20 20 7d , p->zText);. }
a6010 0a 20 20 70 2d 3e 7a 54 65 78 74 20 3d 20 30 3b . p->zText = 0;
a6020 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 .}../*.** Initia
a6030 6c 69 7a 65 20 61 20 73 74 72 69 6e 67 20 61 63 lize a string ac
a6040 63 75 6d 75 6c 61 74 6f 72 0a 2a 2f 0a 53 51 4c cumulator.*/.SQL
a6050 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
a6060 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d sqlite3StrAccum
a6070 49 6e 69 74 28 53 74 72 41 63 63 75 6d 20 2a 70 Init(StrAccum *p
a6080 2c 20 63 68 61 72 20 2a 7a 42 61 73 65 2c 20 69 , char *zBase, i
a6090 6e 74 20 6e 2c 20 69 6e 74 20 6d 78 29 7b 0a 20 nt n, int mx){.
a60a0 20 70 2d 3e 7a 54 65 78 74 20 3d 20 70 2d 3e 7a p->zText = p->z
a60b0 42 61 73 65 20 3d 20 7a 42 61 73 65 3b 0a 20 20 Base = zBase;.
a60c0 70 2d 3e 64 62 20 3d 20 30 3b 0a 20 20 70 2d 3e p->db = 0;. p->
a60d0 6e 43 68 61 72 20 3d 20 30 3b 0a 20 20 70 2d 3e nChar = 0;. p->
a60e0 6e 41 6c 6c 6f 63 20 3d 20 6e 3b 0a 20 20 70 2d nAlloc = n;. p-
a60f0 3e 6d 78 41 6c 6c 6f 63 20 3d 20 6d 78 3b 0a 20 >mxAlloc = mx;.
a6100 20 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 p->useMalloc =
a6110 31 3b 0a 20 20 70 2d 3e 74 6f 6f 42 69 67 20 3d 1;. p->tooBig =
a6120 20 30 3b 0a 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46 0;. p->mallocF
a6130 61 69 6c 65 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a ailed = 0;.}../*
a6140 0a 2a 2a 20 50 72 69 6e 74 20 69 6e 74 6f 20 6d .** Print into m
a6150 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 emory obtained f
a6160 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 rom sqliteMalloc
a6170 28 29 2e 20 20 55 73 65 20 74 68 65 20 69 6e 74 (). Use the int
a6180 65 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 ernal.** %-conve
a6190 72 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 rsion extensions
a61a0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
a61b0 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 ATE char *sqlite
a61c0 33 56 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 65 3VMPrintf(sqlite
a61d0 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 3 *db, const cha
a61e0 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c r *zFormat, va_l
a61f0 69 73 74 20 61 70 29 7b 0a 20 20 63 68 61 72 20 ist ap){. char
a6200 2a 7a 3b 0a 20 20 63 68 61 72 20 7a 42 61 73 65 *z;. char zBase
a6210 5b 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 [SQLITE_PRINT_BU
a6220 46 5f 53 49 5a 45 5d 3b 0a 20 20 53 74 72 41 63 F_SIZE];. StrAc
a6230 63 75 6d 20 61 63 63 3b 0a 20 20 61 73 73 65 72 cum acc;. asser
a6240 74 28 20 64 62 21 3d 30 20 29 3b 0a 20 20 73 71 t( db!=0 );. sq
a6250 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 lite3StrAccumIni
a6260 74 28 26 61 63 63 2c 20 7a 42 61 73 65 2c 20 73 t(&acc, zBase, s
a6270 69 7a 65 6f 66 28 7a 42 61 73 65 29 2c 0a 20 20 izeof(zBase),.
a6280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6290 20 20 20 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 db->aLimit[S
a62a0 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 QLITE_LIMIT_LENG
a62b0 54 48 5d 29 3b 0a 20 20 61 63 63 2e 64 62 20 3d TH]);. acc.db =
a62c0 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 db;. sqlite3VX
a62d0 50 72 69 6e 74 66 28 26 61 63 63 2c 20 31 2c 20 Printf(&acc, 1,
a62e0 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 zFormat, ap);.
a62f0 7a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 41 63 z = sqlite3StrAc
a6300 63 75 6d 46 69 6e 69 73 68 28 26 61 63 63 29 3b cumFinish(&acc);
a6310 0a 20 20 69 66 28 20 61 63 63 2e 6d 61 6c 6c 6f . if( acc.mallo
a6320 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 64 cFailed ){. d
a6330 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
a6340 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 = 1;. }. retur
a6350 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 n z;.}../*.** Pr
a6360 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 int into memory
a6370 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 obtained from sq
a6380 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20 55 liteMalloc(). U
a6390 73 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a se the internal.
a63a0 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 20 ** %-conversion
a63b0 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 extensions..*/.S
a63c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 QLITE_PRIVATE ch
a63d0 61 72 20 2a 73 71 6c 69 74 65 33 4d 50 72 69 6e ar *sqlite3MPrin
a63e0 74 66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 tf(sqlite3 *db,
a63f0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 const char *zFor
a6400 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f mat, ...){. va_
a6410 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 20 list ap;. char
a6420 2a 7a 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 *z;. va_start(a
a6430 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a p, zFormat);. z
a6440 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e = sqlite3VMPrin
a6450 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 tf(db, zFormat,
a6460 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 ap);. va_end(ap
a6470 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d );. return z;.}
a6480 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 73 71 6c ../*.** Like sql
a6490 69 74 65 33 4d 50 72 69 6e 74 66 28 29 2c 20 62 ite3MPrintf(), b
a64a0 75 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 44 ut call sqlite3D
a64b0 62 46 72 65 65 28 29 20 6f 6e 20 7a 53 74 72 20 bFree() on zStr
a64c0 61 66 74 65 72 20 66 6f 72 6d 61 74 74 69 6e 67 after formatting
a64d0 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 61 .** the string a
a64e0 6e 64 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e nd before return
a64f0 6e 69 6e 67 2e 20 20 54 68 69 73 20 72 6f 75 74 ning. This rout
a6500 69 6e 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20 ine is intended
a6510 74 6f 20 62 65 20 75 73 65 64 0a 2a 2a 20 74 6f to be used.** to
a6520 20 6d 6f 64 69 66 79 20 61 6e 20 65 78 69 73 74 modify an exist
a6530 69 6e 67 20 73 74 72 69 6e 67 2e 20 20 46 6f 72 ing string. For
a6540 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 example:.**.**
a6550 20 20 20 20 20 20 78 20 3d 20 73 71 6c 69 74 65 x = sqlite
a6560 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 78 2c 20 3MPrintf(db, x,
a6570 22 70 72 65 66 69 78 20 25 73 20 73 75 66 66 69 "prefix %s suffi
a6580 78 22 2c 20 78 29 3b 0a 2a 2a 0a 2a 2f 0a 53 51 x", x);.**.*/.SQ
a6590 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 LITE_PRIVATE cha
a65a0 72 20 2a 73 71 6c 69 74 65 33 4d 41 70 70 65 6e r *sqlite3MAppen
a65b0 64 66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 df(sqlite3 *db,
a65c0 63 68 61 72 20 2a 7a 53 74 72 2c 20 63 6f 6e 73 char *zStr, cons
a65d0 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c t char *zFormat,
a65e0 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 ...){. va_list
a65f0 20 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a ap;. char *z;.
a6600 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a va_start(ap, z
a6610 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73 Format);. z = s
a6620 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64 qlite3VMPrintf(d
a6630 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b b, zFormat, ap);
a6640 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 . va_end(ap);.
a6650 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
a6660 62 2c 20 7a 53 74 72 29 3b 0a 20 20 72 65 74 75 b, zStr);. retu
a6670 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 rn z;.}../*.** P
a6680 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 rint into memory
a6690 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 obtained from s
a66a0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e qlite3_malloc().
a66b0 20 20 4f 6d 69 74 20 74 68 65 20 69 6e 74 65 72 Omit the inter
a66c0 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 nal.** %-convers
a66d0 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a ion extensions..
a66e0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68 */.SQLITE_API ch
a66f0 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 6d 70 72 ar *sqlite3_vmpr
a6700 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 20 intf(const char
a6710 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c 69 73 *zFormat, va_lis
a6720 74 20 61 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a t ap){. char *z
a6730 3b 0a 20 20 63 68 61 72 20 7a 42 61 73 65 5b 53 ;. char zBase[S
a6740 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f QLITE_PRINT_BUF_
a6750 53 49 5a 45 5d 3b 0a 20 20 53 74 72 41 63 63 75 SIZE];. StrAccu
a6760 6d 20 61 63 63 3b 0a 23 69 66 6e 64 65 66 20 53 m acc;.#ifndef S
a6770 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 QLITE_OMIT_AUTOI
a6780 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 NIT. if( sqlite
a6790 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 3_initialize() )
a67a0 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 return 0;.#endi
a67b0 66 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 f. sqlite3StrAc
a67c0 63 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a 42 cumInit(&acc, zB
a67d0 61 73 65 2c 20 73 69 7a 65 6f 66 28 7a 42 61 73 ase, sizeof(zBas
a67e0 65 29 2c 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c e), SQLITE_MAX_L
a67f0 45 4e 47 54 48 29 3b 0a 20 20 73 71 6c 69 74 65 ENGTH);. sqlite
a6800 33 56 58 50 72 69 6e 74 66 28 26 61 63 63 2c 20 3VXPrintf(&acc,
a6810 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0, zFormat, ap);
a6820 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 53 74 . z = sqlite3St
a6830 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26 61 63 rAccumFinish(&ac
a6840 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a c);. return z;.
a6850 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 69 }../*.** Print i
a6860 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 nto memory obtai
a6870 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 ned from sqlite3
a6880 5f 6d 61 6c 6c 6f 63 28 29 28 29 2e 20 20 4f 6d _malloc()(). Om
a6890 69 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a it the internal.
a68a0 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 20 ** %-conversion
a68b0 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 extensions..*/.S
a68c0 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a QLITE_API char *
a68d0 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 sqlite3_mprintf(
a68e0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 const char *zFor
a68f0 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f mat, ...){. va_
a6900 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 20 list ap;. char
a6910 2a 7a 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 *z;.#ifndef SQLI
a6920 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 TE_OMIT_AUTOINIT
a6930 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 . if( sqlite3_i
a6940 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65 nitialize() ) re
a6950 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 turn 0;.#endif.
a6960 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 va_start(ap, zF
a6970 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73 71 ormat);. z = sq
a6980 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28 7a lite3_vmprintf(z
a6990 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 Format, ap);. v
a69a0 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72 65 74 a_end(ap);. ret
a69b0 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn z;.}../*.**
a69c0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
a69d0 28 29 20 77 6f 72 6b 73 20 6c 69 6b 65 20 73 6e () works like sn
a69e0 70 72 69 6e 74 66 28 29 20 65 78 63 65 70 74 20 printf() except
a69f0 74 68 61 74 20 69 74 20 69 67 6e 6f 72 65 73 20 that it ignores
a6a00 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 6c the.** current l
a6a10 6f 63 61 6c 65 20 73 65 74 74 69 6e 67 73 2e 20 ocale settings.
a6a20 20 54 68 69 73 20 69 73 20 69 6d 70 6f 72 74 61 This is importa
a6a30 6e 74 20 66 6f 72 20 53 51 4c 69 74 65 20 62 65 nt for SQLite be
a6a40 63 61 75 73 65 20 77 65 0a 2a 2a 20 61 72 65 20 cause we.** are
a6a50 6e 6f 74 20 61 62 6c 65 20 74 6f 20 75 73 65 20 not able to use
a6a60 61 20 22 2c 22 20 61 73 20 74 68 65 20 64 65 63 a "," as the dec
a6a70 69 6d 61 6c 20 70 6f 69 6e 74 20 69 6e 20 70 6c imal point in pl
a6a80 61 63 65 20 6f 66 20 22 2e 22 20 61 73 0a 2a 2a ace of "." as.**
a6a90 20 73 70 65 63 69 66 69 65 64 20 62 79 20 73 6f specified by so
a6aa0 6d 65 20 6c 6f 63 61 6c 65 73 2e 0a 2a 2f 0a 53 me locales..*/.S
a6ab0 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a QLITE_API char *
a6ac0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
a6ad0 28 69 6e 74 20 6e 2c 20 63 68 61 72 20 2a 7a 42 (int n, char *zB
a6ae0 75 66 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a uf, const char *
a6af0 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 zFormat, ...){.
a6b00 20 63 68 61 72 20 2a 7a 3b 0a 20 20 76 61 5f 6c char *z;. va_l
a6b10 69 73 74 20 61 70 3b 0a 20 20 53 74 72 41 63 63 ist ap;. StrAcc
a6b20 75 6d 20 61 63 63 3b 0a 0a 20 20 69 66 28 20 6e um acc;.. if( n
a6b30 3c 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 <=0 ){. retur
a6b40 6e 20 7a 42 75 66 3b 0a 20 20 7d 0a 20 20 73 71 n zBuf;. }. sq
a6b50 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 lite3StrAccumIni
a6b60 74 28 26 61 63 63 2c 20 7a 42 75 66 2c 20 6e 2c t(&acc, zBuf, n,
a6b70 20 30 29 3b 0a 20 20 61 63 63 2e 75 73 65 4d 61 0);. acc.useMa
a6b80 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 76 61 5f 73 lloc = 0;. va_s
a6b90 74 61 72 74 28 61 70 2c 7a 46 6f 72 6d 61 74 29 tart(ap,zFormat)
a6ba0 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 ;. sqlite3VXPri
a6bb0 6e 74 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f ntf(&acc, 0, zFo
a6bc0 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f rmat, ap);. va_
a6bd0 65 6e 64 28 61 70 29 3b 0a 20 20 7a 20 3d 20 73 end(ap);. z = s
a6be0 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 qlite3StrAccumFi
a6bf0 6e 69 73 68 28 26 61 63 63 29 3b 0a 20 20 72 65 nish(&acc);. re
a6c00 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 23 69 66 20 64 turn z;.}..#if d
a6c10 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 efined(SQLITE_DE
a6c20 42 55 47 29 0a 2f 2a 0a 2a 2a 20 41 20 76 65 72 BUG)./*.** A ver
a6c30 73 69 6f 6e 20 6f 66 20 70 72 69 6e 74 66 28 29 sion of printf()
a6c40 20 74 68 61 74 20 75 6e 64 65 72 73 74 61 6e 64 that understand
a6c50 73 20 25 6c 6c 64 2e 20 20 55 73 65 64 20 66 6f s %lld. Used fo
a6c60 72 20 64 65 62 75 67 67 69 6e 67 2e 0a 2a 2a 20 r debugging..**
a6c70 54 68 65 20 70 72 69 6e 74 66 28 29 20 62 75 69 The printf() bui
a6c80 6c 74 20 69 6e 74 6f 20 73 6f 6d 65 20 76 65 72 lt into some ver
a6c90 73 69 6f 6e 73 20 6f 66 20 77 69 6e 64 6f 77 73 sions of windows
a6ca0 20 64 6f 65 73 20 6e 6f 74 20 75 6e 64 65 72 73 does not unders
a6cb0 74 61 6e 64 20 25 6c 6c 64 0a 2a 2a 20 61 6e 64 tand %lld.** and
a6cc0 20 73 65 67 66 61 75 6c 74 73 20 69 66 20 79 6f segfaults if yo
a6cd0 75 20 67 69 76 65 20 69 74 20 61 20 6c 6f 6e 67 u give it a long
a6ce0 20 6c 6f 6e 67 20 69 6e 74 2e 0a 2a 2f 0a 53 51 long int..*/.SQ
a6cf0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
a6d00 64 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 d sqlite3DebugPr
a6d10 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 20 intf(const char
a6d20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a *zFormat, ...){.
a6d30 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 va_list ap;.
a6d40 53 74 72 41 63 63 75 6d 20 61 63 63 3b 0a 20 20 StrAccum acc;.
a6d50 63 68 61 72 20 7a 42 75 66 5b 35 30 30 5d 3b 0a char zBuf[500];.
a6d60 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 sqlite3StrAccu
a6d70 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a 42 75 66 mInit(&acc, zBuf
a6d80 2c 20 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 , sizeof(zBuf),
a6d90 30 29 3b 0a 20 20 61 63 63 2e 75 73 65 4d 61 6c 0);. acc.useMal
a6da0 6c 6f 63 20 3d 20 30 3b 0a 20 20 76 61 5f 73 74 loc = 0;. va_st
a6db0 61 72 74 28 61 70 2c 7a 46 6f 72 6d 61 74 29 3b art(ap,zFormat);
a6dc0 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e . sqlite3VXPrin
a6dd0 74 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f 72 tf(&acc, 0, zFor
a6de0 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 mat, ap);. va_e
a6df0 6e 64 28 61 70 29 3b 0a 20 20 73 71 6c 69 74 65 nd(ap);. sqlite
a6e00 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 3StrAccumFinish(
a6e10 26 61 63 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 &acc);. fprintf
a6e20 28 73 74 64 6f 75 74 2c 22 25 73 22 2c 20 7a 42 (stdout,"%s", zB
a6e30 75 66 29 3b 0a 20 20 66 66 6c 75 73 68 28 73 74 uf);. fflush(st
a6e40 64 6f 75 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a dout);.}.#endif.
a6e50 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
a6e60 20 45 6e 64 20 6f 66 20 70 72 69 6e 74 66 2e 63 End of printf.c
a6e70 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
a6e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a6e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
a6ea0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
a6eb0 20 42 65 67 69 6e 20 66 69 6c 65 20 72 61 6e 64 Begin file rand
a6ec0 6f 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a om.c ***********
a6ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a6ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
a6ef0 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 ./*.** 2001 Sept
a6f00 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 ember 15.**.** T
a6f10 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
a6f20 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
a6f30 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
a6f40 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
a6f50 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
a6f60 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
a6f70 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
a6f80 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
a6f90 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
a6fa0 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
a6fb0 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
a6fc0 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
a6fd0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
a6fe0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
a6ff0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
a7000 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
a7010 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
a7020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7060 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 *******.** This
a7070 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f file contains co
a7080 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 de to implement
a7090 61 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20 a pseudo-random
a70a0 6e 75 6d 62 65 72 0a 2a 2a 20 67 65 6e 65 72 61 number.** genera
a70b0 74 6f 72 20 28 50 52 4e 47 29 20 66 6f 72 20 53 tor (PRNG) for S
a70c0 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 61 6e QLite..**.** Ran
a70d0 64 6f 6d 20 6e 75 6d 62 65 72 73 20 61 72 65 20 dom numbers are
a70e0 75 73 65 64 20 62 79 20 73 6f 6d 65 20 6f 66 20 used by some of
a70f0 74 68 65 20 64 61 74 61 62 61 73 65 20 62 61 63 the database bac
a7100 6b 65 6e 64 73 20 69 6e 20 6f 72 64 65 72 0a 2a kends in order.*
a7110 2a 20 74 6f 20 67 65 6e 65 72 61 74 65 20 72 61 * to generate ra
a7120 6e 64 6f 6d 20 69 6e 74 65 67 65 72 20 6b 65 79 ndom integer key
a7130 73 20 66 6f 72 20 74 61 62 6c 65 73 20 6f 72 20 s for tables or
a7140 72 61 6e 64 6f 6d 20 66 69 6c 65 6e 61 6d 65 73 random filenames
a7150 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 72 61 6e ..**.** $Id: ran
a7160 64 6f 6d 2e 63 2c 76 20 31 2e 32 39 20 32 30 30 dom.c,v 1.29 200
a7170 38 2f 31 32 2f 31 30 20 31 39 3a 32 36 3a 32 34 8/12/10 19:26:24
a7180 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a drh Exp $.*/...
a7190 2f 2a 20 41 6c 6c 20 74 68 72 65 61 64 73 20 73 /* All threads s
a71a0 68 61 72 65 20 61 20 73 69 6e 67 6c 65 20 72 61 hare a single ra
a71b0 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65 ndom number gene
a71c0 72 61 74 6f 72 2e 0a 2a 2a 20 54 68 69 73 20 73 rator..** This s
a71d0 74 72 75 63 74 75 72 65 20 69 73 20 74 68 65 20 tructure is the
a71e0 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66 current state of
a71f0 20 74 68 65 20 67 65 6e 65 72 61 74 6f 72 2e 0a the generator..
a7200 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 */.static SQLITE
a7210 5f 57 53 44 20 73 74 72 75 63 74 20 73 71 6c 69 _WSD struct sqli
a7220 74 65 33 50 72 6e 67 54 79 70 65 20 7b 0a 20 20 te3PrngType {.
a7230 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69 73 unsigned char is
a7240 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 2f Init; /
a7250 2a 20 54 72 75 65 20 69 66 20 69 6e 69 74 69 61 * True if initia
a7260 6c 69 7a 65 64 20 2a 2f 0a 20 20 75 6e 73 69 67 lized */. unsig
a7270 6e 65 64 20 63 68 61 72 20 69 2c 20 6a 3b 20 20 ned char i, j;
a7280 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 /* Sta
a7290 74 65 20 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a te variables */.
a72a0 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
a72b0 73 5b 32 35 36 5d 3b 20 20 20 20 20 20 20 20 20 s[256];
a72c0 20 2f 2a 20 53 74 61 74 65 20 76 61 72 69 61 62 /* State variab
a72d0 6c 65 73 20 2a 2f 0a 7d 20 73 71 6c 69 74 65 33 les */.} sqlite3
a72e0 50 72 6e 67 3b 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 Prng;../*.** Get
a72f0 20 61 20 73 69 6e 67 6c 65 20 38 2d 62 69 74 20 a single 8-bit
a7300 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 66 72 6f random value fro
a7310 6d 20 74 68 65 20 52 43 34 20 50 52 4e 47 2e 20 m the RC4 PRNG.
a7320 20 54 68 65 20 4d 75 74 65 78 0a 2a 2a 20 6d 75 The Mutex.** mu
a7330 73 74 20 62 65 20 68 65 6c 64 20 77 68 69 6c 65 st be held while
a7340 20 65 78 65 63 75 74 69 6e 67 20 74 68 69 73 20 executing this
a7350 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 57 routine..**.** W
a7360 68 79 20 6e 6f 74 20 6a 75 73 74 20 75 73 65 20 hy not just use
a7370 61 20 6c 69 62 72 61 72 79 20 72 61 6e 64 6f 6d a library random
a7380 20 67 65 6e 65 72 61 74 6f 72 20 6c 69 6b 65 20 generator like
a7390 6c 72 61 6e 64 34 38 28 29 20 66 6f 72 20 74 68 lrand48() for th
a73a0 69 73 3f 0a 2a 2a 20 42 65 63 61 75 73 65 20 74 is?.** Because t
a73b0 68 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20 6f he OP_NewRowid o
a73c0 70 63 6f 64 65 20 69 6e 20 74 68 65 20 56 44 42 pcode in the VDB
a73d0 45 20 64 65 70 65 6e 64 73 20 6f 6e 20 68 61 76 E depends on hav
a73e0 69 6e 67 20 61 20 76 65 72 79 0a 2a 2a 20 67 6f ing a very.** go
a73f0 6f 64 20 73 6f 75 72 63 65 20 6f 66 20 72 61 6e od source of ran
a7400 64 6f 6d 20 6e 75 6d 62 65 72 73 2e 20 20 54 68 dom numbers. Th
a7410 65 20 6c 72 61 6e 64 34 38 28 29 20 6c 69 62 72 e lrand48() libr
a7420 61 72 79 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 ary function may
a7430 0a 2a 2a 20 77 65 6c 6c 20 62 65 20 67 6f 6f 64 .** well be good
a7440 20 65 6e 6f 75 67 68 2e 20 20 42 75 74 20 6d 61 enough. But ma
a7450 79 62 65 20 6e 6f 74 2e 20 20 4f 72 20 6d 61 79 ybe not. Or may
a7460 62 65 20 6c 72 61 6e 64 34 38 28 29 20 68 61 73 be lrand48() has
a7470 20 73 6f 6d 65 0a 2a 2a 20 73 75 62 74 6c 65 20 some.** subtle
a7480 70 72 6f 62 6c 65 6d 73 20 6f 6e 20 73 6f 6d 65 problems on some
a7490 20 73 79 73 74 65 6d 73 20 74 68 61 74 20 63 6f systems that co
a74a0 75 6c 64 20 63 61 75 73 65 20 70 72 6f 62 6c 65 uld cause proble
a74b0 6d 73 2e 20 20 49 74 20 69 73 20 68 61 72 64 0a ms. It is hard.
a74c0 2a 2a 20 74 6f 20 6b 6e 6f 77 2e 20 20 54 6f 20 ** to know. To
a74d0 6d 69 6e 69 6d 69 7a 65 20 74 68 65 20 72 69 73 minimize the ris
a74e0 6b 20 6f 66 20 70 72 6f 62 6c 65 6d 73 20 64 75 k of problems du
a74f0 65 20 74 6f 20 62 61 64 20 6c 72 61 6e 64 34 38 e to bad lrand48
a7500 28 29 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 ().** implementa
a7510 74 69 6f 6e 73 2c 20 53 51 4c 69 74 65 20 75 73 tions, SQLite us
a7520 65 73 20 74 68 69 73 20 72 61 6e 64 6f 6d 20 6e es this random n
a7530 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 20 umber generator
a7540 62 61 73 65 64 0a 2a 2a 20 6f 6e 20 52 43 34 2c based.** on RC4,
a7550 20 77 68 69 63 68 20 77 65 20 6b 6e 6f 77 20 77 which we know w
a7560 6f 72 6b 73 20 76 65 72 79 20 77 65 6c 6c 2e 0a orks very well..
a7570 2a 2a 0a 2a 2a 20 28 4c 61 74 65 72 29 3a 20 20 **.** (Later):
a7580 41 63 74 75 61 6c 6c 79 2c 20 4f 50 5f 4e 65 77 Actually, OP_New
a7590 52 6f 77 69 64 20 64 6f 65 73 20 6e 6f 74 20 64 Rowid does not d
a75a0 65 70 65 6e 64 20 6f 6e 20 61 20 67 6f 6f 64 20 epend on a good
a75b0 73 6f 75 72 63 65 20 6f 66 0a 2a 2a 20 72 61 6e source of.** ran
a75c0 64 6f 6d 6e 65 73 73 20 61 6e 79 20 6d 6f 72 65 domness any more
a75d0 2e 20 20 42 75 74 20 77 65 20 77 69 6c 6c 20 6c . But we will l
a75e0 65 61 76 65 20 74 68 69 73 20 63 6f 64 65 20 69 eave this code i
a75f0 6e 20 61 6c 6c 20 74 68 65 20 73 61 6d 65 2e 0a n all the same..
a7600 2a 2f 0a 73 74 61 74 69 63 20 75 38 20 72 61 6e */.static u8 ran
a7610 64 6f 6d 42 79 74 65 28 76 6f 69 64 29 7b 0a 20 domByte(void){.
a7620 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 74 unsigned char t
a7630 3b 0a 0a 0a 20 20 2f 2a 20 54 68 65 20 22 77 73 ;... /* The "ws
a7640 64 50 72 6e 67 22 20 6d 61 63 72 6f 20 77 69 6c dPrng" macro wil
a7650 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68 65 l resolve to the
a7660 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20 6e pseudo-random n
a7670 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 0a umber generator.
a7680 20 20 2a 2a 20 73 74 61 74 65 20 76 65 63 74 6f ** state vecto
a7690 72 2e 20 20 49 66 20 77 72 69 74 61 62 6c 65 20 r. If writable
a76a0 73 74 61 74 69 63 20 64 61 74 61 20 69 73 20 75 static data is u
a76b0 6e 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 74 68 nsupported on th
a76c0 65 20 74 61 72 67 65 74 2c 0a 20 20 2a 2a 20 77 e target,. ** w
a76d0 65 20 68 61 76 65 20 74 6f 20 6c 6f 63 61 74 65 e have to locate
a76e0 20 74 68 65 20 73 74 61 74 65 20 76 65 63 74 6f the state vecto
a76f0 72 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20 20 r at run-time.
a7700 49 6e 20 74 68 65 20 6d 6f 72 65 20 63 6f 6d 6d In the more comm
a7710 6f 6e 0a 20 20 2a 2a 20 63 61 73 65 20 77 68 65 on. ** case whe
a7720 72 65 20 77 72 69 74 61 62 6c 65 20 73 74 61 74 re writable stat
a7730 69 63 20 64 61 74 61 20 69 73 20 73 75 70 70 6f ic data is suppo
a7740 72 74 65 64 2c 20 77 73 64 50 72 6e 67 20 63 61 rted, wsdPrng ca
a7750 6e 20 72 65 66 65 72 20 64 69 72 65 63 74 6c 79 n refer directly
a7760 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 22 73 71 . ** to the "sq
a7770 6c 69 74 65 33 50 72 6e 67 22 20 73 74 61 74 65 lite3Prng" state
a7780 20 76 65 63 74 6f 72 20 64 65 63 6c 61 72 65 64 vector declared
a7790 20 61 62 6f 76 65 2e 0a 20 20 2a 2f 0a 23 69 66 above.. */.#if
a77a0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
a77b0 57 53 44 0a 20 20 73 74 72 75 63 74 20 73 71 6c WSD. struct sql
a77c0 69 74 65 33 50 72 6e 67 54 79 70 65 20 2a 70 20 ite3PrngType *p
a77d0 3d 20 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 = &GLOBAL(struct
a77e0 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 sqlite3PrngType
a77f0 2c 20 73 71 6c 69 74 65 33 50 72 6e 67 29 3b 0a , sqlite3Prng);.
a7800 23 20 64 65 66 69 6e 65 20 77 73 64 50 72 6e 67 # define wsdPrng
a7810 20 70 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64 65 p[0].#else.# de
a7820 66 69 6e 65 20 77 73 64 50 72 6e 67 20 73 71 6c fine wsdPrng sql
a7830 69 74 65 33 50 72 6e 67 0a 23 65 6e 64 69 66 0a ite3Prng.#endif.
a7840 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a .. /* Initializ
a7850 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 e the state of t
a7860 68 65 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 he random number
a7870 20 67 65 6e 65 72 61 74 6f 72 20 6f 6e 63 65 2c generator once,
a7880 0a 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74 20 . ** the first
a7890 74 69 6d 65 20 74 68 69 73 20 72 6f 75 74 69 6e time this routin
a78a0 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 54 68 e is called. Th
a78b0 65 20 73 65 65 64 20 76 61 6c 75 65 20 64 6f 65 e seed value doe
a78c0 73 0a 20 20 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 s. ** not need
a78d0 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20 6c 6f 74 to contain a lot
a78e0 20 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20 73 of randomness s
a78f0 69 6e 63 65 20 77 65 20 61 72 65 20 6e 6f 74 0a ince we are not.
a7900 20 20 2a 2a 20 74 72 79 69 6e 67 20 74 6f 20 64 ** trying to d
a7910 6f 20 73 65 63 75 72 65 20 65 6e 63 72 79 70 74 o secure encrypt
a7920 69 6f 6e 20 6f 72 20 61 6e 79 74 68 69 6e 67 20 ion or anything
a7930 6c 69 6b 65 20 74 68 61 74 2e 2e 2e 0a 20 20 2a like that.... *
a7940 2a 0a 20 20 2a 2a 20 4e 6f 74 68 69 6e 67 20 69 *. ** Nothing i
a7950 6e 20 74 68 69 73 20 66 69 6c 65 20 6f 72 20 61 n this file or a
a7960 6e 79 77 68 65 72 65 20 65 6c 73 65 20 69 6e 20 nywhere else in
a7970 53 51 4c 69 74 65 20 64 6f 65 73 20 61 6e 79 20 SQLite does any
a7980 6b 69 6e 64 20 6f 66 0a 20 20 2a 2a 20 65 6e 63 kind of. ** enc
a7990 72 79 70 74 69 6f 6e 2e 20 20 54 68 65 20 52 43 ryption. The RC
a79a0 34 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 20 62 4 algorithm is b
a79b0 65 69 6e 67 20 75 73 65 64 20 61 73 20 61 20 50 eing used as a P
a79c0 52 4e 47 20 28 70 73 65 75 64 6f 2d 72 61 6e 64 RNG (pseudo-rand
a79d0 6f 6d 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 67 om. ** number g
a79e0 65 6e 65 72 61 74 6f 72 29 20 6e 6f 74 20 61 73 enerator) not as
a79f0 20 61 6e 20 65 6e 63 72 79 70 74 69 6f 6e 20 64 an encryption d
a7a00 65 76 69 63 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 evice.. */. if
a7a10 28 20 21 77 73 64 50 72 6e 67 2e 69 73 49 6e 69 ( !wsdPrng.isIni
a7a20 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a t ){. int i;.
a7a30 20 20 20 20 63 68 61 72 20 6b 5b 32 35 36 5d 3b char k[256];
a7a40 0a 20 20 20 20 77 73 64 50 72 6e 67 2e 6a 20 3d . wsdPrng.j =
a7a50 20 30 3b 0a 20 20 20 20 77 73 64 50 72 6e 67 2e 0;. wsdPrng.
a7a60 69 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 i = 0;. sqlit
a7a70 65 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73 e3OsRandomness(s
a7a80 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 qlite3_vfs_find(
a7a90 30 29 2c 20 32 35 36 2c 20 6b 29 3b 0a 20 20 20 0), 256, k);.
a7aa0 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32 35 36 3b for(i=0; i<256;
a7ab0 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 77 73 64 i++){. wsd
a7ac0 50 72 6e 67 2e 73 5b 69 5d 20 3d 20 28 75 38 29 Prng.s[i] = (u8)
a7ad0 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 i;. }. for
a7ae0 28 69 3d 30 3b 20 69 3c 32 35 36 3b 20 69 2b 2b (i=0; i<256; i++
a7af0 29 7b 0a 20 20 20 20 20 20 77 73 64 50 72 6e 67 ){. wsdPrng
a7b00 2e 6a 20 2b 3d 20 77 73 64 50 72 6e 67 2e 73 5b .j += wsdPrng.s[
a7b10 69 5d 20 2b 20 6b 5b 69 5d 3b 0a 20 20 20 20 20 i] + k[i];.
a7b20 20 74 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b 77 t = wsdPrng.s[w
a7b30 73 64 50 72 6e 67 2e 6a 5d 3b 0a 20 20 20 20 20 sdPrng.j];.
a7b40 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 wsdPrng.s[wsdPr
a7b50 6e 67 2e 6a 5d 20 3d 20 77 73 64 50 72 6e 67 2e ng.j] = wsdPrng.
a7b60 73 5b 69 5d 3b 0a 20 20 20 20 20 20 77 73 64 50 s[i];. wsdP
a7b70 72 6e 67 2e 73 5b 69 5d 20 3d 20 74 3b 0a 20 20 rng.s[i] = t;.
a7b80 20 20 7d 0a 20 20 20 20 77 73 64 50 72 6e 67 2e }. wsdPrng.
a7b90 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 7d 0a isInit = 1;. }.
a7ba0 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 . /* Generate a
a7bb0 6e 64 20 72 65 74 75 72 6e 20 73 69 6e 67 6c 65 nd return single
a7bc0 20 72 61 6e 64 6f 6d 20 62 79 74 65 0a 20 20 2a random byte. *
a7bd0 2f 0a 20 20 77 73 64 50 72 6e 67 2e 69 2b 2b 3b /. wsdPrng.i++;
a7be0 0a 20 20 74 20 3d 20 77 73 64 50 72 6e 67 2e 73 . t = wsdPrng.s
a7bf0 5b 77 73 64 50 72 6e 67 2e 69 5d 3b 0a 20 20 77 [wsdPrng.i];. w
a7c00 73 64 50 72 6e 67 2e 6a 20 2b 3d 20 74 3b 0a 20 sdPrng.j += t;.
a7c10 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 wsdPrng.s[wsdPr
a7c20 6e 67 2e 69 5d 20 3d 20 77 73 64 50 72 6e 67 2e ng.i] = wsdPrng.
a7c30 73 5b 77 73 64 50 72 6e 67 2e 6a 5d 3b 0a 20 20 s[wsdPrng.j];.
a7c40 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e wsdPrng.s[wsdPrn
a7c50 67 2e 6a 5d 20 3d 20 74 3b 0a 20 20 74 20 2b 3d g.j] = t;. t +=
a7c60 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 wsdPrng.s[wsdPr
a7c70 6e 67 2e 69 5d 3b 0a 20 20 72 65 74 75 72 6e 20 ng.i];. return
a7c80 77 73 64 50 72 6e 67 2e 73 5b 74 5d 3b 0a 7d 0a wsdPrng.s[t];.}.
a7c90 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 20 ./*.** Return N
a7ca0 72 61 6e 64 6f 6d 20 62 79 74 65 73 2e 0a 2a 2f random bytes..*/
a7cb0 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
a7cc0 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e sqlite3_randomn
a7cd0 65 73 73 28 69 6e 74 20 4e 2c 20 76 6f 69 64 20 ess(int N, void
a7ce0 2a 70 42 75 66 29 7b 0a 20 20 75 6e 73 69 67 6e *pBuf){. unsign
a7cf0 65 64 20 63 68 61 72 20 2a 7a 42 75 66 20 3d 20 ed char *zBuf =
a7d00 70 42 75 66 3b 0a 23 69 66 20 53 51 4c 49 54 45 pBuf;.#if SQLITE
a7d10 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 73 71 _THREADSAFE. sq
a7d20 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 lite3_mutex *mut
a7d30 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 ex = sqlite3Mute
a7d40 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 xAlloc(SQLITE_MU
a7d50 54 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 29 TEX_STATIC_PRNG)
a7d60 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 ;.#endif. sqlit
a7d70 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d e3_mutex_enter(m
a7d80 75 74 65 78 29 3b 0a 20 20 77 68 69 6c 65 28 20 utex);. while(
a7d90 4e 2d 2d 20 29 7b 0a 20 20 20 20 2a 28 7a 42 75 N-- ){. *(zBu
a7da0 66 2b 2b 29 20 3d 20 72 61 6e 64 6f 6d 42 79 74 f++) = randomByt
a7db0 65 28 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 e();. }. sqlit
a7dc0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d e3_mutex_leave(m
a7dd0 75 74 65 78 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 utex);.}..#ifnde
a7de0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 f SQLITE_OMIT_BU
a7df0 49 4c 54 49 4e 5f 54 45 53 54 0a 2f 2a 0a 2a 2a ILTIN_TEST./*.**
a7e00 20 46 6f 72 20 74 65 73 74 69 6e 67 20 70 75 72 For testing pur
a7e10 70 6f 73 65 73 2c 20 77 65 20 73 6f 6d 65 74 69 poses, we someti
a7e20 6d 65 73 20 77 61 6e 74 20 74 6f 20 70 72 65 73 mes want to pres
a7e30 65 72 76 65 20 74 68 65 20 73 74 61 74 65 20 6f erve the state o
a7e40 66 0a 2a 2a 20 50 52 4e 47 20 61 6e 64 20 72 65 f.** PRNG and re
a7e50 73 74 6f 72 65 20 74 68 65 20 50 52 4e 47 20 74 store the PRNG t
a7e60 6f 20 69 74 73 20 73 61 76 65 64 20 73 74 61 74 o its saved stat
a7e70 65 20 61 74 20 61 20 6c 61 74 65 72 20 74 69 6d e at a later tim
a7e80 65 2c 20 6f 72 0a 2a 2a 20 74 6f 20 72 65 73 65 e, or.** to rese
a7e90 74 20 74 68 65 20 50 52 4e 47 20 74 6f 20 69 74 t the PRNG to it
a7ea0 73 20 69 6e 69 74 69 61 6c 20 73 74 61 74 65 2e s initial state.
a7eb0 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 These routines
a7ec0 20 61 63 63 6f 6d 70 6c 69 73 68 0a 2a 2a 20 74 accomplish.** t
a7ed0 68 6f 73 65 20 74 61 73 6b 73 2e 0a 2a 2a 0a 2a hose tasks..**.*
a7ee0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 74 65 * The sqlite3_te
a7ef0 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 20 69 6e 74 st_control() int
a7f00 65 72 66 61 63 65 20 63 61 6c 6c 73 20 74 68 65 erface calls the
a7f10 73 65 20 72 6f 75 74 69 6e 65 73 20 74 6f 0a 2a se routines to.*
a7f20 2a 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 50 52 * control the PR
a7f30 4e 47 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 NG..*/.static SQ
a7f40 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 LITE_WSD struct
a7f50 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 20 sqlite3PrngType
a7f60 73 71 6c 69 74 65 33 53 61 76 65 64 50 72 6e 67 sqlite3SavedPrng
a7f70 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
a7f80 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72 6e void sqlite3Prn
a7f90 67 53 61 76 65 53 74 61 74 65 28 76 6f 69 64 29 gSaveState(void)
a7fa0 7b 0a 20 20 6d 65 6d 63 70 79 28 0a 20 20 20 20 {. memcpy(.
a7fb0 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 73 &GLOBAL(struct s
a7fc0 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 2c 20 qlite3PrngType,
a7fd0 73 71 6c 69 74 65 33 53 61 76 65 64 50 72 6e 67 sqlite3SavedPrng
a7fe0 29 2c 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73 ),. &GLOBAL(s
a7ff0 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e truct sqlite3Prn
a8000 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 50 72 gType, sqlite3Pr
a8010 6e 67 29 2c 0a 20 20 20 20 73 69 7a 65 6f 66 28 ng),. sizeof(
a8020 73 71 6c 69 74 65 33 50 72 6e 67 29 0a 20 20 29 sqlite3Prng). )
a8030 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
a8040 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
a8050 72 6e 67 52 65 73 74 6f 72 65 53 74 61 74 65 28 rngRestoreState(
a8060 76 6f 69 64 29 7b 0a 20 20 6d 65 6d 63 70 79 28 void){. memcpy(
a8070 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73 74 72 . &GLOBAL(str
a8080 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 uct sqlite3PrngT
a8090 79 70 65 2c 20 73 71 6c 69 74 65 33 50 72 6e 67 ype, sqlite3Prng
a80a0 29 2c 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73 ),. &GLOBAL(s
a80b0 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e truct sqlite3Prn
a80c0 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 53 61 gType, sqlite3Sa
a80d0 76 65 64 50 72 6e 67 29 2c 0a 20 20 20 20 73 69 vedPrng),. si
a80e0 7a 65 6f 66 28 73 71 6c 69 74 65 33 50 72 6e 67 zeof(sqlite3Prng
a80f0 29 0a 20 20 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f ). );.}.SQLITE_
a8100 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
a8110 69 74 65 33 50 72 6e 67 52 65 73 65 74 53 74 61 ite3PrngResetSta
a8120 74 65 28 76 6f 69 64 29 7b 0a 20 20 47 4c 4f 42 te(void){. GLOB
a8130 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 AL(struct sqlite
a8140 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c 69 74 3PrngType, sqlit
a8150 65 33 50 72 6e 67 29 2e 69 73 49 6e 69 74 20 3d e3Prng).isInit =
a8160 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 0;.}.#endif /*
a8170 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c SQLITE_OMIT_BUIL
a8180 54 49 4e 5f 54 45 53 54 20 2a 2f 0a 0a 2f 2a 2a TIN_TEST */../**
a8190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
a81a0 20 6f 66 20 72 61 6e 64 6f 6d 2e 63 20 2a 2a 2a of random.c ***
a81b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a81c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a81d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
a81e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
a81f0 69 6e 20 66 69 6c 65 20 75 74 66 2e 63 20 2a 2a in file utf.c **
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 2f 0a 2f 2a 0a ***********/./*.
a8230 2a 2a 20 32 30 30 34 20 41 70 72 69 6c 20 31 33 ** 2004 April 13
a8240 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
a8250 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
a8260 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
a8270 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
a8280 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
a8290 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
a82a0 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
a82b0 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
a82c0 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
a82d0 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
a82e0 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
a82f0 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
a8300 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
a8310 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
a8320 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
a8330 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
a8340 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
a8350 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
a8360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
a83a0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
a83b0 74 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 75 tains routines u
a83c0 73 65 64 20 74 6f 20 74 72 61 6e 73 6c 61 74 65 sed to translate
a83d0 20 62 65 74 77 65 65 6e 20 55 54 46 2d 38 2c 20 between UTF-8,
a83e0 0a 2a 2a 20 55 54 46 2d 31 36 2c 20 55 54 46 2d .** UTF-16, UTF-
a83f0 31 36 42 45 2c 20 61 6e 64 20 55 54 46 2d 31 36 16BE, and UTF-16
a8400 4c 45 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 75 LE..**.** $Id: u
a8410 74 66 2e 63 2c 76 20 31 2e 37 33 20 32 30 30 39 tf.c,v 1.73 2009
a8420 2f 30 34 2f 30 31 20 31 38 3a 34 30 3a 33 32 20 /04/01 18:40:32
a8430 64 72 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 drh Exp $.**.**
a8440 4e 6f 74 65 73 20 6f 6e 20 55 54 46 2d 38 3a 0a Notes on UTF-8:.
a8450 2a 2a 0a 2a 2a 20 20 20 42 79 74 65 2d 30 20 20 **.** Byte-0
a8460 20 20 42 79 74 65 2d 31 20 20 20 20 42 79 74 65 Byte-1 Byte
a8470 2d 32 20 20 20 20 42 79 74 65 2d 33 20 20 20 20 -2 Byte-3
a8480 56 61 6c 75 65 0a 2a 2a 20 20 30 78 78 78 78 78 Value.** 0xxxxx
a8490 78 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 xx
a84a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a84b0 20 20 20 30 30 30 30 30 30 30 30 20 30 30 30 30 00000000 0000
a84c0 30 30 30 30 20 30 78 78 78 78 78 78 78 0a 2a 2a 0000 0xxxxxxx.**
a84d0 20 20 31 31 30 79 79 79 79 79 20 20 31 30 78 78 110yyyyy 10xx
a84e0 78 78 78 78 20 20 20 20 20 20 20 20 20 20 20 20 xxxx
a84f0 20 20 20 20 20 20 20 20 20 20 20 30 30 30 30 30 00000
a8500 30 30 30 20 30 30 30 30 30 79 79 79 20 79 79 78 000 00000yyy yyx
a8510 78 78 78 78 78 0a 2a 2a 20 20 31 31 31 30 7a 7a xxxxx.** 1110zz
a8520 7a 7a 20 20 31 30 79 79 79 79 79 79 20 20 31 30 zz 10yyyyyy 10
a8530 78 78 78 78 78 78 20 20 20 20 20 20 20 20 20 20 xxxxxx
a8540 20 20 20 30 30 30 30 30 30 30 30 20 7a 7a 7a 7a 00000000 zzzz
a8550 79 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a yyyy yyxxxxxx.**
a8560 20 20 31 31 31 31 30 75 75 75 20 20 31 30 75 75 11110uuu 10uu
a8570 7a 7a 7a 7a 20 20 31 30 79 79 79 79 79 79 20 20 zzzz 10yyyyyy
a8580 31 30 78 78 78 78 78 78 20 20 20 30 30 30 75 75 10xxxxxx 000uu
a8590 75 75 75 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 uuu zzzzyyyy yyx
a85a0 78 78 78 78 78 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 4e xxxxx.**.**.** N
a85b0 6f 74 65 73 20 6f 6e 20 55 54 46 2d 31 36 3a 20 otes on UTF-16:
a85c0 20 28 77 69 74 68 20 77 77 77 77 2b 31 3d 3d 75 (with wwww+1==u
a85d0 75 75 75 75 29 0a 2a 2a 0a 2a 2a 20 20 20 20 20 uuuu).**.**
a85e0 20 57 6f 72 64 2d 30 20 20 20 20 20 20 20 20 20 Word-0
a85f0 20 20 20 20 20 20 57 6f 72 64 2d 31 20 20 20 20 Word-1
a8600 20 20 20 20 20 20 56 61 6c 75 65 0a 2a 2a 20 20 Value.**
a8610 31 31 30 31 31 30 77 77 20 77 77 7a 7a 7a 7a 79 110110ww wwzzzzy
a8620 79 20 20 20 31 31 30 31 31 31 79 79 20 79 79 78 y 110111yy yyx
a8630 78 78 78 78 78 20 20 20 20 30 30 30 75 75 75 75 xxxxx 000uuuu
a8640 75 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78 78 u zzzzyyyy yyxxx
a8650 78 78 78 0a 2a 2a 20 20 7a 7a 7a 7a 79 79 79 79 xxx.** zzzzyyyy
a8660 20 79 79 78 78 78 78 78 78 20 20 20 20 20 20 20 yyxxxxxx
a8670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a8680 20 30 30 30 30 30 30 30 30 20 7a 7a 7a 7a 79 79 00000000 zzzzyy
a8690 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 0a 2a yy yyxxxxxx.**.*
a86a0 2a 0a 2a 2a 20 42 4f 4d 20 6f 72 20 42 79 74 65 *.** BOM or Byte
a86b0 20 4f 72 64 65 72 20 4d 61 72 6b 3a 0a 2a 2a 20 Order Mark:.**
a86c0 20 20 20 20 30 78 66 66 20 30 78 66 65 20 20 20 0xff 0xfe
a86d0 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 75 74 little-endian ut
a86e0 66 2d 31 36 20 66 6f 6c 6c 6f 77 73 0a 2a 2a 20 f-16 follows.**
a86f0 20 20 20 20 30 78 66 65 20 30 78 66 66 20 20 20 0xfe 0xff
a8700 62 69 67 2d 65 6e 64 69 61 6e 20 75 74 66 2d 31 big-endian utf-1
a8710 36 20 66 6f 6c 6c 6f 77 73 0a 2a 2a 0a 2a 2f 0a 6 follows.**.*/.
a8720 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
a8730 49 6e 63 6c 75 64 65 20 76 64 62 65 49 6e 74 2e Include vdbeInt.
a8740 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 h in the middle
a8750 6f 66 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a of utf.c *******
a8760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
a8770 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
a8780 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 49 Begin file vdbeI
a8790 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a nt.h ***********
a87a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a87b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
a87c0 2f 2a 0a 2a 2a 20 32 30 30 33 20 53 65 70 74 65 /*.** 2003 Septe
a87d0 6d 62 65 72 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 mber 6.**.** The
a87e0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
a87f0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
a8800 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
a8810 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
a8820 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
a8830 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
a8840 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
a8850 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
a8860 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
a8870 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
a8880 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
a8890 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
a88a0 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
a88b0 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
a88c0 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
a88d0 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
a88e0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
a88f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8930 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 *****.** This is
a8940 20 74 68 65 20 68 65 61 64 65 72 20 66 69 6c 65 the header file
a8950 20 66 6f 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e for information
a8960 20 74 68 61 74 20 69 73 20 70 72 69 76 61 74 65 that is private
a8970 20 74 6f 20 74 68 65 0a 2a 2a 20 56 44 42 45 2e to the.** VDBE.
a8980 20 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 This informati
a8990 6f 6e 20 75 73 65 64 20 74 6f 20 61 6c 6c 20 62 on used to all b
a89a0 65 20 61 74 20 74 68 65 20 74 6f 70 20 6f 66 20 e at the top of
a89b0 74 68 65 20 73 69 6e 67 6c 65 0a 2a 2a 20 73 6f the single.** so
a89c0 75 72 63 65 20 63 6f 64 65 20 66 69 6c 65 20 22 urce code file "
a89d0 76 64 62 65 2e 63 22 2e 20 20 57 68 65 6e 20 74 vdbe.c". When t
a89e0 68 61 74 20 66 69 6c 65 20 62 65 63 61 6d 65 20 hat file became
a89f0 74 6f 6f 20 62 69 67 20 28 6f 76 65 72 0a 2a 2a too big (over.**
a8a00 20 36 30 30 30 20 6c 69 6e 65 73 20 6c 6f 6e 67 6000 lines long
a8a10 29 20 69 74 20 77 61 73 20 73 70 6c 69 74 20 75 ) it was split u
a8a20 70 20 69 6e 74 6f 20 73 65 76 65 72 61 6c 20 73 p into several s
a8a30 6d 61 6c 6c 65 72 20 66 69 6c 65 73 20 61 6e 64 maller files and
a8a40 0a 2a 2a 20 74 68 69 73 20 68 65 61 64 65 72 20 .** this header
a8a50 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 61 73 20 information was
a8a60 66 61 63 74 6f 72 65 64 20 6f 75 74 2e 0a 2a 2a factored out..**
a8a70 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 49 6e 74 .** $Id: vdbeInt
a8a80 2e 68 2c 76 20 31 2e 31 37 34 20 32 30 30 39 2f .h,v 1.174 2009/
a8a90 30 36 2f 32 33 20 31 34 3a 31 35 3a 30 34 20 64 06/23 14:15:04 d
a8aa0 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e rh Exp $.*/.#ifn
a8ab0 64 65 66 20 5f 56 44 42 45 49 4e 54 5f 48 5f 0a def _VDBEINT_H_.
a8ac0 23 64 65 66 69 6e 65 20 5f 56 44 42 45 49 4e 54 #define _VDBEINT
a8ad0 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 20 69 _H_../*.** SQL i
a8ae0 73 20 74 72 61 6e 73 6c 61 74 65 64 20 69 6e 74 s translated int
a8af0 6f 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66 20 o a sequence of
a8b00 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 74 6f 20 instructions to
a8b10 62 65 0a 2a 2a 20 65 78 65 63 75 74 65 64 20 62 be.** executed b
a8b20 79 20 61 20 76 69 72 74 75 61 6c 20 6d 61 63 68 y a virtual mach
a8b30 69 6e 65 2e 20 20 45 61 63 68 20 69 6e 73 74 72 ine. Each instr
a8b40 75 63 74 69 6f 6e 20 69 73 20 61 6e 20 69 6e 73 uction is an ins
a8b50 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 65 20 tance.** of the
a8b60 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
a8b70 75 72 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 ure..*/.typedef
a8b80 73 74 72 75 63 74 20 56 64 62 65 4f 70 20 4f 70 struct VdbeOp Op
a8b90 3b 0a 0a 2f 2a 0a 2a 2a 20 42 6f 6f 6c 65 61 6e ;../*.** Boolean
a8ba0 20 76 61 6c 75 65 73 0a 2a 2f 0a 74 79 70 65 64 values.*/.typed
a8bb0 65 66 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 ef unsigned char
a8bc0 20 42 6f 6f 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 Bool;../*.** A
a8bd0 63 75 72 73 6f 72 20 69 73 20 61 20 70 6f 69 6e cursor is a poin
a8be0 74 65 72 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c ter into a singl
a8bf0 65 20 42 54 72 65 65 20 77 69 74 68 69 6e 20 61 e BTree within a
a8c00 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a database file..
a8c10 2a 2a 20 54 68 65 20 63 75 72 73 6f 72 20 63 61 ** The cursor ca
a8c20 6e 20 73 65 65 6b 20 74 6f 20 61 20 42 54 72 65 n seek to a BTre
a8c30 65 20 65 6e 74 72 79 20 77 69 74 68 20 61 20 70 e entry with a p
a8c40 61 72 74 69 63 75 6c 61 72 20 6b 65 79 2c 20 6f articular key, o
a8c50 72 0a 2a 2a 20 6c 6f 6f 70 20 6f 76 65 72 20 61 r.** loop over a
a8c60 6c 6c 20 65 6e 74 72 69 65 73 20 6f 66 20 74 68 ll entries of th
a8c70 65 20 42 74 72 65 65 2e 20 20 59 6f 75 20 63 61 e Btree. You ca
a8c80 6e 20 61 6c 73 6f 20 69 6e 73 65 72 74 20 6e 65 n also insert ne
a8c90 77 20 42 54 72 65 65 0a 2a 2a 20 65 6e 74 72 69 w BTree.** entri
a8ca0 65 73 20 6f 72 20 72 65 74 72 69 65 76 65 20 74 es or retrieve t
a8cb0 68 65 20 6b 65 79 20 6f 72 20 64 61 74 61 20 66 he key or data f
a8cc0 72 6f 6d 20 74 68 65 20 65 6e 74 72 79 20 74 68 rom the entry th
a8cd0 61 74 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a at the cursor.**
a8ce0 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f is currently po
a8cf0 69 6e 74 69 6e 67 20 74 6f 2e 0a 2a 2a 20 0a 2a inting to..** .*
a8d00 2a 20 45 76 65 72 79 20 63 75 72 73 6f 72 20 74 * Every cursor t
a8d10 68 61 74 20 74 68 65 20 76 69 72 74 75 61 6c 20 hat the virtual
a8d20 6d 61 63 68 69 6e 65 20 68 61 73 20 6f 70 65 6e machine has open
a8d30 20 69 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 is represented
a8d40 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 by an.** instanc
a8d50 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
a8d60 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a ng structure..**
a8d70 0a 2a 2a 20 49 66 20 74 68 65 20 56 64 62 65 43 .** If the VdbeC
a8d80 75 72 73 6f 72 2e 69 73 54 72 69 67 67 65 72 52 ursor.isTriggerR
a8d90 6f 77 20 66 6c 61 67 20 69 73 20 73 65 74 20 69 ow flag is set i
a8da0 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 69 t means that thi
a8db0 73 20 63 75 72 73 6f 72 20 69 73 0a 2a 2a 20 72 s cursor is.** r
a8dc0 65 61 6c 6c 79 20 61 20 73 69 6e 67 6c 65 20 72 eally a single r
a8dd0 6f 77 20 74 68 61 74 20 72 65 70 72 65 73 65 6e ow that represen
a8de0 74 73 20 74 68 65 20 4e 45 57 20 6f 72 20 4f 4c ts the NEW or OL
a8df0 44 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 6f D pseudo-table o
a8e00 66 0a 2a 2a 20 61 20 72 6f 77 20 74 72 69 67 67 f.** a row trigg
a8e10 65 72 2e 20 20 54 68 65 20 64 61 74 61 20 66 6f er. The data fo
a8e20 72 20 74 68 65 20 72 6f 77 20 69 73 20 73 74 6f r the row is sto
a8e30 72 65 64 20 69 6e 20 56 64 62 65 43 75 72 73 6f red in VdbeCurso
a8e40 72 2e 70 44 61 74 61 20 61 6e 64 0a 2a 2a 20 74 r.pData and.** t
a8e50 68 65 20 72 6f 77 69 64 20 69 73 20 69 6e 20 56 he rowid is in V
a8e60 64 62 65 43 75 72 73 6f 72 2e 69 4b 65 79 2e 0a dbeCursor.iKey..
a8e70 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65 43 75 */.struct VdbeCu
a8e80 72 73 6f 72 20 7b 0a 20 20 42 74 43 75 72 73 6f rsor {. BtCurso
a8e90 72 20 2a 70 43 75 72 73 6f 72 3b 20 20 20 20 2f r *pCursor; /
a8ea0 2a 20 54 68 65 20 63 75 72 73 6f 72 20 73 74 72 * The cursor str
a8eb0 75 63 74 75 72 65 20 6f 66 20 74 68 65 20 62 61 ucture of the ba
a8ec0 63 6b 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 69 ckend */. int i
a8ed0 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 Db;
a8ee0 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 75 72 /* Index of cur
a8ef0 73 6f 72 20 64 61 74 61 62 61 73 65 20 69 6e 20 sor database in
a8f00 64 62 2d 3e 61 44 62 5b 5d 20 28 6f 72 20 2d 31 db->aDb[] (or -1
a8f10 29 20 2a 2f 0a 20 20 69 36 34 20 6c 61 73 74 52 ) */. i64 lastR
a8f20 6f 77 69 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 owid; /*
a8f30 4c 61 73 74 20 72 6f 77 69 64 20 66 72 6f 6d 20 Last rowid from
a8f40 61 20 4e 65 78 74 20 6f 72 20 4e 65 78 74 49 64 a Next or NextId
a8f50 78 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 x operation */.
a8f60 20 42 6f 6f 6c 20 7a 65 72 6f 65 64 3b 20 20 20 Bool zeroed;
a8f70 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
a8f80 66 20 7a 65 72 6f 65 64 20 6f 75 74 20 61 6e 64 f zeroed out and
a8f90 20 72 65 61 64 79 20 66 6f 72 20 72 65 75 73 65 ready for reuse
a8fa0 20 2a 2f 0a 20 20 42 6f 6f 6c 20 72 6f 77 69 64 */. Bool rowid
a8fb0 49 73 56 61 6c 69 64 3b 20 20 20 20 2f 2a 20 54 IsValid; /* T
a8fc0 72 75 65 20 69 66 20 6c 61 73 74 52 6f 77 69 64 rue if lastRowid
a8fd0 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20 42 is valid */. B
a8fe0 6f 6f 6c 20 61 74 46 69 72 73 74 3b 20 20 20 20 ool atFirst;
a8ff0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
a9000 70 6f 69 6e 74 69 6e 67 20 74 6f 20 66 69 72 73 pointing to firs
a9010 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 42 6f 6f t entry */. Boo
a9020 6c 20 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69 64 l useRandomRowid
a9030 3b 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 6e ; /* Generate n
a9040 65 77 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 ew record number
a9050 73 20 73 65 6d 69 2d 72 61 6e 64 6f 6d 6c 79 20 s semi-randomly
a9060 2a 2f 0a 20 20 42 6f 6f 6c 20 6e 75 6c 6c 52 6f */. Bool nullRo
a9070 77 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 w; /* Tr
a9080 75 65 20 69 66 20 70 6f 69 6e 74 69 6e 67 20 74 ue if pointing t
a9090 6f 20 61 20 72 6f 77 20 77 69 74 68 20 6e 6f 20 o a row with no
a90a0 64 61 74 61 20 2a 2f 0a 20 20 42 6f 6f 6c 20 64 data */. Bool d
a90b0 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3b 20 20 eferredMoveto;
a90c0 2f 2a 20 41 20 63 61 6c 6c 20 74 6f 20 73 71 6c /* A call to sql
a90d0 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 ite3BtreeMoveto(
a90e0 29 20 69 73 20 6e 65 65 64 65 64 20 2a 2f 0a 20 ) is needed */.
a90f0 20 42 6f 6f 6c 20 69 73 54 61 62 6c 65 3b 20 20 Bool isTable;
a9100 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
a9110 66 20 61 20 74 61 62 6c 65 20 72 65 71 75 69 72 f a table requir
a9120 69 6e 67 20 69 6e 74 65 67 65 72 20 6b 65 79 73 ing integer keys
a9130 20 2a 2f 0a 20 20 42 6f 6f 6c 20 69 73 49 6e 64 */. Bool isInd
a9140 65 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ex; /* T
a9150 72 75 65 20 69 66 20 61 6e 20 69 6e 64 65 78 20 rue if an index
a9160 63 6f 6e 74 61 69 6e 69 6e 67 20 6b 65 79 73 20 containing keys
a9170 6f 6e 6c 79 20 2d 20 6e 6f 20 64 61 74 61 20 2a only - no data *
a9180 2f 0a 20 20 69 36 34 20 6d 6f 76 65 74 6f 54 61 /. i64 movetoTa
a9190 72 67 65 74 3b 20 20 20 20 20 2f 2a 20 41 72 67 rget; /* Arg
a91a0 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 64 65 66 ument to the def
a91b0 65 72 72 65 64 20 73 71 6c 69 74 65 33 42 74 72 erred sqlite3Btr
a91c0 65 65 4d 6f 76 65 74 6f 28 29 20 2a 2f 0a 20 20 eeMoveto() */.
a91d0 42 74 72 65 65 20 2a 70 42 74 3b 20 20 20 20 20 Btree *pBt;
a91e0 20 20 20 20 20 20 2f 2a 20 53 65 70 61 72 61 74 /* Separat
a91f0 65 20 66 69 6c 65 20 68 6f 6c 64 69 6e 67 20 74 e file holding t
a9200 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 2a emporary table *
a9210 2f 0a 20 20 69 6e 74 20 70 73 65 75 64 6f 54 61 /. int pseudoTa
a9220 62 6c 65 52 65 67 3b 20 20 20 2f 2a 20 52 65 67 bleReg; /* Reg
a9230 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 70 73 ister holding ps
a9240 65 75 64 6f 74 61 62 6c 65 20 63 6f 6e 74 65 6e eudotable conten
a9250 74 2e 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f 20 t. */. KeyInfo
a9260 2a 70 4b 65 79 49 6e 66 6f 3b 20 20 20 20 2f 2a *pKeyInfo; /*
a9270 20 49 6e 66 6f 20 61 62 6f 75 74 20 69 6e 64 65 Info about inde
a9280 78 20 6b 65 79 73 20 6e 65 65 64 65 64 20 62 79 x keys needed by
a9290 20 69 6e 64 65 78 20 63 75 72 73 6f 72 73 20 2a index cursors *
a92a0 2f 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 20 /. int nField;
a92b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
a92c0 62 65 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e ber of fields in
a92d0 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 the header */.
a92e0 20 69 36 34 20 73 65 71 43 6f 75 6e 74 3b 20 20 i64 seqCount;
a92f0 20 20 20 20 20 20 20 2f 2a 20 53 65 71 75 65 6e /* Sequen
a9300 63 65 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 ce counter */.
a9310 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 sqlite3_vtab_cur
a9320 73 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 sor *pVtabCursor
a9330 3b 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 ; /* The cursor
a9340 20 66 6f 72 20 61 20 76 69 72 74 75 61 6c 20 74 for a virtual t
a9350 61 62 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 able */. const
a9360 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a sqlite3_module *
a9370 70 4d 6f 64 75 6c 65 3b 20 20 20 20 20 2f 2a 20 pModule; /*
a9380 4d 6f 64 75 6c 65 20 66 6f 72 20 63 75 72 73 6f Module for curso
a9390 72 20 70 56 74 61 62 43 75 72 73 6f 72 20 2a 2f r pVtabCursor */
a93a0 0a 0a 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 .. /* Result of
a93b0 20 6c 61 73 74 20 73 71 6c 69 74 65 33 42 74 72 last sqlite3Btr
a93c0 65 65 4d 6f 76 65 74 6f 28 29 20 64 6f 6e 65 20 eeMoveto() done
a93d0 62 79 20 61 6e 20 4f 50 5f 4e 6f 74 45 78 69 73 by an OP_NotExis
a93e0 74 73 20 6f 72 20 0a 20 20 2a 2a 20 4f 50 5f 49 ts or . ** OP_I
a93f0 73 55 6e 69 71 75 65 20 6f 70 63 6f 64 65 20 6f sUnique opcode o
a9400 6e 20 74 68 69 73 20 63 75 72 73 6f 72 2e 20 2a n this cursor. *
a9410 2f 0a 20 20 69 6e 74 20 73 65 65 6b 52 65 73 75 /. int seekResu
a9420 6c 74 3b 0a 0a 20 20 2f 2a 20 43 61 63 68 65 64 lt;.. /* Cached
a9430 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f information abo
a9440 75 74 20 74 68 65 20 68 65 61 64 65 72 20 66 6f ut the header fo
a9450 72 20 74 68 65 20 64 61 74 61 20 72 65 63 6f 72 r the data recor
a9460 64 20 74 68 61 74 20 74 68 65 0a 20 20 2a 2a 20 d that the. **
a9470 63 75 72 73 6f 72 20 69 73 20 63 75 72 72 65 6e cursor is curren
a9480 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e tly pointing to.
a9490 20 20 4f 6e 6c 79 20 76 61 6c 69 64 20 69 66 20 Only valid if
a94a0 63 61 63 68 65 53 74 61 74 75 73 20 6d 61 74 63 cacheStatus matc
a94b0 68 65 73 0a 20 20 2a 2a 20 56 64 62 65 2e 63 61 hes. ** Vdbe.ca
a94c0 63 68 65 43 74 72 2e 20 20 56 64 62 65 2e 63 61 cheCtr. Vdbe.ca
a94d0 63 68 65 43 74 72 20 77 69 6c 6c 20 6e 65 76 65 cheCtr will neve
a94e0 72 20 74 61 6b 65 20 6f 6e 20 74 68 65 20 76 61 r take on the va
a94f0 6c 75 65 20 6f 66 0a 20 20 2a 2a 20 43 41 43 48 lue of. ** CACH
a9500 45 5f 53 54 41 4c 45 20 61 6e 64 20 73 6f 20 73 E_STALE and so s
a9510 65 74 74 69 6e 67 20 63 61 63 68 65 53 74 61 74 etting cacheStat
a9520 75 73 3d 43 41 43 48 45 5f 53 54 41 4c 45 20 67 us=CACHE_STALE g
a9530 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 0a 20 uarantees that.
a9540 20 2a 2a 20 74 68 65 20 63 61 63 68 65 20 69 73 ** the cache is
a9550 20 6f 75 74 20 6f 66 20 64 61 74 65 2e 0a 20 20 out of date..
a9560 2a 2a 0a 20 20 2a 2a 20 61 52 6f 77 20 6d 69 67 **. ** aRow mig
a9570 68 74 20 70 6f 69 6e 74 20 74 6f 20 28 65 70 68 ht point to (eph
a9580 65 6d 65 72 61 6c 29 20 64 61 74 61 20 66 6f 72 emeral) data for
a9590 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 the current row
a95a0 2c 20 6f 72 20 69 74 20 6d 69 67 68 74 0a 20 20 , or it might.
a95b0 2a 2a 20 62 65 20 4e 55 4c 4c 2e 0a 20 20 2a 2f ** be NULL.. */
a95c0 0a 20 20 75 33 32 20 63 61 63 68 65 53 74 61 74 . u32 cacheStat
a95d0 75 73 3b 20 20 20 20 20 20 2f 2a 20 43 61 63 68 us; /* Cach
a95e0 65 20 69 73 20 76 61 6c 69 64 20 69 66 20 74 68 e is valid if th
a95f0 69 73 20 6d 61 74 63 68 65 73 20 56 64 62 65 2e is matches Vdbe.
a9600 63 61 63 68 65 43 74 72 20 2a 2f 0a 20 20 69 6e cacheCtr */. in
a9610 74 20 70 61 79 6c 6f 61 64 53 69 7a 65 3b 20 20 t payloadSize;
a9620 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d /* Total num
a9630 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 ber of bytes in
a9640 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 the record */.
a9650 75 33 32 20 2a 61 54 79 70 65 3b 20 20 20 20 20 u32 *aType;
a9660 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 76 61 /* Type va
a9670 6c 75 65 73 20 66 6f 72 20 61 6c 6c 20 65 6e 74 lues for all ent
a9680 72 69 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f ries in the reco
a9690 72 64 20 2a 2f 0a 20 20 75 33 32 20 2a 61 4f 66 rd */. u32 *aOf
a96a0 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 2f 2a fset; /*
a96b0 20 43 61 63 68 65 64 20 6f 66 66 73 65 74 73 20 Cached offsets
a96c0 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 to the start of
a96d0 65 61 63 68 20 63 6f 6c 75 6d 6e 73 20 64 61 74 each columns dat
a96e0 61 20 2a 2f 0a 20 20 75 38 20 2a 61 52 6f 77 3b a */. u8 *aRow;
a96f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
a9700 44 61 74 61 20 66 6f 72 20 74 68 65 20 63 75 72 Data for the cur
a9710 72 65 6e 74 20 72 6f 77 2c 20 69 66 20 61 6c 6c rent row, if all
a9720 20 6f 6e 20 6f 6e 65 20 70 61 67 65 20 2a 2f 0a on one page */.
a9730 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 };.typedef struc
a9740 74 20 56 64 62 65 43 75 72 73 6f 72 20 56 64 62 t VdbeCursor Vdb
a9750 65 43 75 72 73 6f 72 3b 0a 0a 2f 2a 0a 2a 2a 20 eCursor;../*.**
a9760 57 68 65 6e 20 61 20 73 75 62 2d 70 72 6f 67 72 When a sub-progr
a9770 61 6d 20 69 73 20 65 78 65 63 75 74 65 64 20 28 am is executed (
a9780 4f 50 5f 50 72 6f 67 72 61 6d 29 2c 20 61 20 73 OP_Program), a s
a9790 74 72 75 63 74 75 72 65 20 6f 66 20 74 68 69 73 tructure of this
a97a0 20 74 79 70 65 0a 2a 2a 20 69 73 20 61 6c 6c 6f type.** is allo
a97b0 63 61 74 65 64 20 74 6f 20 73 74 6f 72 65 20 74 cated to store t
a97c0 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 he current value
a97d0 20 6f 66 20 74 68 65 20 70 72 6f 67 72 61 6d 20 of the program
a97e0 63 6f 75 6e 74 65 72 2c 20 61 73 0a 2a 2a 20 77 counter, as.** w
a97f0 65 6c 6c 20 61 73 20 74 68 65 20 63 75 72 72 65 ell as the curre
a9800 6e 74 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 61 nt memory cell a
a9810 72 72 61 79 20 61 6e 64 20 76 61 72 69 6f 75 73 rray and various
a9820 20 6f 74 68 65 72 20 66 72 61 6d 65 20 73 70 65 other frame spe
a9830 63 69 66 69 63 0a 2a 2a 20 76 61 6c 75 65 73 20 cific.** values
a9840 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 56 64 stored in the Vd
a9850 62 65 20 73 74 72 75 63 74 2e 20 57 68 65 6e 20 be struct. When
a9860 74 68 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 the sub-program
a9870 69 73 20 66 69 6e 69 73 68 65 64 2c 20 0a 2a 2a is finished, .**
a9880 20 74 68 65 73 65 20 76 61 6c 75 65 73 20 61 72 these values ar
a9890 65 20 63 6f 70 69 65 64 20 62 61 63 6b 20 74 6f e copied back to
a98a0 20 74 68 65 20 56 64 62 65 20 66 72 6f 6d 20 74 the Vdbe from t
a98b0 68 65 20 56 64 62 65 46 72 61 6d 65 20 73 74 72 he VdbeFrame str
a98c0 75 63 74 75 72 65 2c 0a 2a 2a 20 72 65 73 74 6f ucture,.** resto
a98d0 72 69 6e 67 20 74 68 65 20 73 74 61 74 65 20 6f ring the state o
a98e0 66 20 74 68 65 20 56 4d 20 74 6f 20 61 73 20 69 f the VM to as i
a98f0 74 20 77 61 73 20 62 65 66 6f 72 65 20 74 68 65 t was before the
a9900 20 73 75 62 2d 70 72 6f 67 72 61 6d 0a 2a 2a 20 sub-program.**
a9910 62 65 67 61 6e 20 65 78 65 63 75 74 69 6e 67 2e began executing.
a9920 0a 2a 2a 0a 2a 2a 20 46 72 61 6d 65 73 20 61 72 .**.** Frames ar
a9930 65 20 73 74 6f 72 65 64 20 69 6e 20 61 20 6c 69 e stored in a li
a9940 6e 6b 65 64 20 6c 69 73 74 20 68 65 61 64 65 64 nked list headed
a9950 20 61 74 20 56 64 62 65 2e 70 50 61 72 65 6e 74 at Vdbe.pParent
a9960 2e 20 56 64 62 65 2e 70 50 61 72 65 6e 74 0a 2a . Vdbe.pParent.*
a9970 2a 20 69 73 20 74 68 65 20 70 61 72 65 6e 74 20 * is the parent
a9980 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 66 of the current f
a9990 72 61 6d 65 2c 20 6f 72 20 7a 65 72 6f 20 69 66 rame, or zero if
a99a0 20 74 68 65 20 63 75 72 72 65 6e 74 20 66 72 61 the current fra
a99b0 6d 65 0a 2a 2a 20 69 73 20 74 68 65 20 6d 61 69 me.** is the mai
a99c0 6e 20 56 64 62 65 20 70 72 6f 67 72 61 6d 2e 0a n Vdbe program..
a99d0 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
a99e0 74 20 56 64 62 65 46 72 61 6d 65 20 56 64 62 65 t VdbeFrame Vdbe
a99f0 46 72 61 6d 65 3b 0a 73 74 72 75 63 74 20 56 64 Frame;.struct Vd
a9a00 62 65 46 72 61 6d 65 20 7b 0a 20 20 56 64 62 65 beFrame {. Vdbe
a9a10 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 *v;
a9a20 20 20 20 20 2f 2a 20 56 4d 20 74 68 69 73 20 66 /* VM this f
a9a30 72 61 6d 65 20 62 65 6c 6f 6e 67 73 20 74 6f 20 rame belongs to
a9a40 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20 20 20 20 */. int pc;
a9a50 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
a9a60 50 72 6f 67 72 61 6d 20 43 6f 75 6e 74 65 72 20 Program Counter
a9a70 2a 2f 0a 20 20 4f 70 20 2a 61 4f 70 3b 20 20 20 */. Op *aOp;
a9a80 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
a9a90 50 72 6f 67 72 61 6d 20 69 6e 73 74 72 75 63 74 Program instruct
a9aa0 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f ions */. int nO
a9ab0 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p;
a9ac0 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 4f 70 /* Size of aOp
a9ad0 20 61 72 72 61 79 20 2a 2f 0a 20 20 4d 65 6d 20 array */. Mem
a9ae0 2a 61 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 *aMem;
a9af0 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 /* Array of
a9b00 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 2a 2f 0a memory cells */.
a9b10 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 20 int nMem;
a9b20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
a9b30 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 ber of entries i
a9b40 6e 20 61 4d 65 6d 20 2a 2f 0a 20 20 56 64 62 65 n aMem */. Vdbe
a9b50 43 75 72 73 6f 72 20 2a 2a 61 70 43 73 72 3b 20 Cursor **apCsr;
a9b60 20 20 20 20 2f 2a 20 45 6c 65 6d 65 6e 74 20 6f /* Element o
a9b70 66 20 56 64 62 65 20 63 75 72 73 6f 72 73 20 2a f Vdbe cursors *
a9b80 2f 0a 20 20 75 31 36 20 6e 43 75 72 73 6f 72 3b /. u16 nCursor;
a9b90 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
a9ba0 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 umber of entries
a9bb0 20 69 6e 20 61 70 43 73 72 20 2a 2f 0a 20 20 76 in apCsr */. v
a9bc0 6f 69 64 20 2a 74 6f 6b 65 6e 3b 20 20 20 20 20 oid *token;
a9bd0 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f /* Copy o
a9be0 66 20 53 75 62 50 72 6f 67 72 61 6d 2e 74 6f 6b f SubProgram.tok
a9bf0 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 69 en */. int nChi
a9c00 6c 64 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 ldMem;
a9c10 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d /* Number of mem
a9c20 6f 72 79 20 63 65 6c 6c 73 20 66 6f 72 20 63 68 ory cells for ch
a9c30 69 6c 64 20 66 72 61 6d 65 20 2a 2f 0a 20 20 69 ild frame */. i
a9c40 6e 74 20 6e 43 68 69 6c 64 43 73 72 3b 20 20 20 nt nChildCsr;
a9c50 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
a9c60 20 6f 66 20 63 75 72 73 6f 72 73 20 66 6f 72 20 of cursors for
a9c70 63 68 69 6c 64 20 66 72 61 6d 65 20 2a 2f 0a 20 child frame */.
a9c80 20 69 36 34 20 6c 61 73 74 52 6f 77 69 64 3b 20 i64 lastRowid;
a9c90 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 /* Last
a9ca0 20 69 6e 73 65 72 74 20 72 6f 77 69 64 20 28 73 insert rowid (s
a9cb0 71 6c 69 74 65 33 2e 6c 61 73 74 52 6f 77 69 64 qlite3.lastRowid
a9cc0 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61 6e ) */. int nChan
a9cd0 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f ge; /
a9ce0 2a 20 53 74 61 74 65 6d 65 6e 74 20 63 68 61 6e * Statement chan
a9cf0 67 65 73 20 28 56 64 62 65 2e 6e 43 68 61 6e 67 ges (Vdbe.nChang
a9d00 65 73 29 20 20 20 20 20 2a 2f 0a 20 20 56 64 62 es) */. Vdb
a9d10 65 46 72 61 6d 65 20 2a 70 50 61 72 65 6e 74 3b eFrame *pParent;
a9d20 20 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 6f /* Parent o
a9d30 66 20 74 68 69 73 20 66 72 61 6d 65 20 2a 2f 0a f this frame */.
a9d40 7d 3b 0a 0a 23 64 65 66 69 6e 65 20 56 64 62 65 };..#define Vdbe
a9d50 46 72 61 6d 65 4d 65 6d 28 70 29 20 28 28 4d 65 FrameMem(p) ((Me
a9d60 6d 20 2a 29 26 28 28 75 38 20 2a 29 70 29 5b 52 m *)&((u8 *)p)[R
a9d70 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 56 64 62 OUND8(sizeof(Vdb
a9d80 65 46 72 61 6d 65 29 29 5d 29 0a 0a 2f 2a 0a 2a eFrame))])../*.*
a9d90 2a 20 41 20 76 61 6c 75 65 20 66 6f 72 20 56 64 * A value for Vd
a9da0 62 65 43 75 72 73 6f 72 2e 63 61 63 68 65 56 61 beCursor.cacheVa
a9db0 6c 69 64 20 74 68 61 74 20 6d 65 61 6e 73 20 74 lid that means t
a9dc0 68 65 20 63 61 63 68 65 20 69 73 20 61 6c 77 61 he cache is alwa
a9dd0 79 73 20 69 6e 76 61 6c 69 64 2e 0a 2a 2f 0a 23 ys invalid..*/.#
a9de0 64 65 66 69 6e 65 20 43 41 43 48 45 5f 53 54 41 define CACHE_STA
a9df0 4c 45 20 30 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 LE 0../*.** Inte
a9e00 72 6e 61 6c 6c 79 2c 20 74 68 65 20 76 64 62 65 rnally, the vdbe
a9e10 20 6d 61 6e 69 70 75 6c 61 74 65 73 20 6e 65 61 manipulates nea
a9e20 72 6c 79 20 61 6c 6c 20 53 51 4c 20 76 61 6c 75 rly all SQL valu
a9e30 65 73 20 61 73 20 4d 65 6d 0a 2a 2a 20 73 74 72 es as Mem.** str
a9e40 75 63 74 75 72 65 73 2e 20 45 61 63 68 20 4d 65 uctures. Each Me
a9e50 6d 20 73 74 72 75 63 74 20 6d 61 79 20 63 61 63 m struct may cac
a9e60 68 65 20 6d 75 6c 74 69 70 6c 65 20 72 65 70 72 he multiple repr
a9e70 65 73 65 6e 74 61 74 69 6f 6e 73 20 28 73 74 72 esentations (str
a9e80 69 6e 67 2c 0a 2a 2a 20 69 6e 74 65 67 65 72 20 ing,.** integer
a9e90 65 74 63 2e 29 20 6f 66 20 74 68 65 20 73 61 6d etc.) of the sam
a9ea0 65 20 76 61 6c 75 65 2e 20 20 41 20 76 61 6c 75 e value. A valu
a9eb0 65 20 28 61 6e 64 20 74 68 65 72 65 66 6f 72 65 e (and therefore
a9ec0 20 4d 65 6d 20 73 74 72 75 63 74 75 72 65 29 0a Mem structure).
a9ed0 2a 2a 20 68 61 73 20 74 68 65 20 66 6f 6c 6c 6f ** has the follo
a9ee0 77 69 6e 67 20 70 72 6f 70 65 72 74 69 65 73 3a wing properties:
a9ef0 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 76 61 6c 75 .**.** Each valu
a9f00 65 20 68 61 73 20 61 20 6d 61 6e 69 66 65 73 74 e has a manifest
a9f10 20 74 79 70 65 2e 20 54 68 65 20 6d 61 6e 69 66 type. The manif
a9f20 65 73 74 20 74 79 70 65 20 6f 66 20 74 68 65 20 est type of the
a9f30 76 61 6c 75 65 20 73 74 6f 72 65 64 0a 2a 2a 20 value stored.**
a9f40 69 6e 20 61 20 4d 65 6d 20 73 74 72 75 63 74 20 in a Mem struct
a9f50 69 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 is returned by t
a9f60 68 65 20 4d 65 6d 54 79 70 65 28 4d 65 6d 2a 29 he MemType(Mem*)
a9f70 20 6d 61 63 72 6f 2e 20 54 68 65 20 74 79 70 65 macro. The type
a9f80 20 69 73 0a 2a 2a 20 6f 6e 65 20 6f 66 20 53 51 is.** one of SQ
a9f90 4c 49 54 45 5f 4e 55 4c 4c 2c 20 53 51 4c 49 54 LITE_NULL, SQLIT
a9fa0 45 5f 49 4e 54 45 47 45 52 2c 20 53 51 4c 49 54 E_INTEGER, SQLIT
a9fb0 45 5f 52 45 41 4c 2c 20 53 51 4c 49 54 45 5f 54 E_REAL, SQLITE_T
a9fc0 45 58 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 EXT or.** SQLITE
a9fd0 5f 42 4c 4f 42 2e 0a 2a 2f 0a 73 74 72 75 63 74 _BLOB..*/.struct
a9fe0 20 4d 65 6d 20 7b 0a 20 20 75 6e 69 6f 6e 20 7b Mem {. union {
a9ff0 0a 20 20 20 20 69 36 34 20 69 3b 20 20 20 20 20 . i64 i;
aa000 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 /* Inte
aa010 67 65 72 20 76 61 6c 75 65 2e 20 2a 2f 0a 20 20 ger value. */.
aa020 20 20 69 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20 int nZero;
aa030 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 /* Used wh
aa040 65 6e 20 62 69 74 20 4d 45 4d 5f 5a 65 72 6f 20 en bit MEM_Zero
aa050 69 73 20 73 65 74 20 69 6e 20 66 6c 61 67 73 20 is set in flags
aa060 2a 2f 0a 20 20 20 20 46 75 6e 63 44 65 66 20 2a */. FuncDef *
aa070 70 44 65 66 3b 20 20 20 20 20 20 2f 2a 20 55 73 pDef; /* Us
aa080 65 64 20 6f 6e 6c 79 20 77 68 65 6e 20 66 6c 61 ed only when fla
aa090 67 73 3d 3d 4d 45 4d 5f 41 67 67 20 2a 2f 0a 20 gs==MEM_Agg */.
aa0a0 20 20 20 52 6f 77 53 65 74 20 2a 70 52 6f 77 53 RowSet *pRowS
aa0b0 65 74 3b 20 20 20 20 2f 2a 20 55 73 65 64 20 6f et; /* Used o
aa0c0 6e 6c 79 20 77 68 65 6e 20 66 6c 61 67 73 3d 3d nly when flags==
aa0d0 4d 45 4d 5f 52 6f 77 53 65 74 20 2a 2f 0a 20 20 MEM_RowSet */.
aa0e0 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 46 72 VdbeFrame *pFr
aa0f0 61 6d 65 3b 20 20 2f 2a 20 55 73 65 64 20 77 68 ame; /* Used wh
aa100 65 6e 20 66 6c 61 67 73 3d 3d 4d 45 4d 5f 46 72 en flags==MEM_Fr
aa110 61 6d 65 20 2a 2f 0a 20 20 7d 20 75 3b 0a 20 20 ame */. } u;.
aa120 64 6f 75 62 6c 65 20 72 3b 20 20 20 20 20 20 20 double r;
aa130 20 20 20 20 2f 2a 20 52 65 61 6c 20 76 61 6c 75 /* Real valu
aa140 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a e */. sqlite3 *
aa150 64 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 db; /* Th
aa160 65 20 61 73 73 6f 63 69 61 74 65 64 20 64 61 74 e associated dat
aa170 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
aa180 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 3b 20 20 */. char *z;
aa190 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 /* Str
aa1a0 69 6e 67 20 6f 72 20 42 4c 4f 42 20 76 61 6c 75 ing or BLOB valu
aa1b0 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 20 e */. int n;
aa1c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
aa1d0 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 mber of characte
aa1e0 72 73 20 69 6e 20 73 74 72 69 6e 67 20 76 61 6c rs in string val
aa1f0 75 65 2c 20 65 78 63 6c 75 64 69 6e 67 20 27 5c ue, excluding '\
aa200 30 27 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61 67 0' */. u16 flag
aa210 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 s; /* S
aa220 6f 6d 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 ome combination
aa230 6f 66 20 4d 45 4d 5f 4e 75 6c 6c 2c 20 4d 45 4d of MEM_Null, MEM
aa240 5f 53 74 72 2c 20 4d 45 4d 5f 44 79 6e 2c 20 65 _Str, MEM_Dyn, e
aa250 74 63 2e 20 2a 2f 0a 20 20 75 38 20 20 74 79 70 tc. */. u8 typ
aa260 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 e; /*
aa270 4f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 4e 55 One of SQLITE_NU
aa280 4c 4c 2c 20 53 51 4c 49 54 45 5f 54 45 58 54 2c LL, SQLITE_TEXT,
aa290 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 2c SQLITE_INTEGER,
aa2a0 20 65 74 63 20 2a 2f 0a 20 20 75 38 20 20 65 6e etc */. u8 en
aa2b0 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a c; /*
aa2c0 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 SQLITE_UTF8, SQ
aa2d0 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20 53 51 LITE_UTF16BE, SQ
aa2e0 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 2a 2f 0a LITE_UTF16LE */.
aa2f0 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 void (*xDel)(v
aa300 6f 69 64 20 2a 29 3b 20 20 2f 2a 20 49 66 20 6e oid *); /* If n
aa310 6f 74 20 6e 75 6c 6c 2c 20 63 61 6c 6c 20 74 68 ot null, call th
aa320 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 64 is function to d
aa330 65 6c 65 74 65 20 4d 65 6d 2e 7a 20 2a 2f 0a 20 elete Mem.z */.
aa340 20 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f 63 3b 20 char *zMalloc;
aa350 20 20 20 20 20 2f 2a 20 44 79 6e 61 6d 69 63 20 /* Dynamic
aa360 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 buffer allocated
aa370 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c by sqlite3_mall
aa380 6f 63 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 4f oc() */.};../* O
aa390 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 ne or more of th
aa3a0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 e following flag
aa3b0 73 20 61 72 65 20 73 65 74 20 74 6f 20 69 6e 64 s are set to ind
aa3c0 69 63 61 74 65 20 74 68 65 20 76 61 6c 69 64 4f icate the validO
aa3d0 4b 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 74 K.** representat
aa3e0 69 6f 6e 73 20 6f 66 20 74 68 65 20 76 61 6c 75 ions of the valu
aa3f0 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 e stored in the
aa400 4d 65 6d 20 73 74 72 75 63 74 2e 0a 2a 2a 0a 2a Mem struct..**.*
aa410 2a 20 49 66 20 74 68 65 20 4d 45 4d 5f 4e 75 6c * If the MEM_Nul
aa420 6c 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 l flag is set, t
aa430 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 69 73 hen the value is
aa440 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 76 61 6c an SQL NULL val
aa450 75 65 2e 0a 2a 2a 20 4e 6f 20 6f 74 68 65 72 20 ue..** No other
aa460 66 6c 61 67 73 20 6d 61 79 20 62 65 20 73 65 74 flags may be set
aa470 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a in this case..*
aa480 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4d 45 4d 5f *.** If the MEM_
aa490 53 74 72 20 66 6c 61 67 20 69 73 20 73 65 74 20 Str flag is set
aa4a0 74 68 65 6e 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 then Mem.z point
aa4b0 73 20 61 74 20 61 20 73 74 72 69 6e 67 20 72 65 s at a string re
aa4c0 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a presentation..**
aa4d0 20 55 73 75 61 6c 6c 79 20 74 68 69 73 20 69 73 Usually this is
aa4e0 20 65 6e 63 6f 64 65 64 20 69 6e 20 74 68 65 20 encoded in the
aa4f0 73 61 6d 65 20 75 6e 69 63 6f 64 65 20 65 6e 63 same unicode enc
aa500 6f 64 69 6e 67 20 61 73 20 74 68 65 20 6d 61 69 oding as the mai
aa510 6e 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 28 73 n.** database (s
aa520 65 65 20 62 65 6c 6f 77 20 66 6f 72 20 65 78 63 ee below for exc
aa530 65 70 74 69 6f 6e 73 29 2e 20 49 66 20 74 68 65 eptions). If the
aa540 20 4d 45 4d 5f 54 65 72 6d 20 66 6c 61 67 20 69 MEM_Term flag i
aa550 73 20 61 6c 73 6f 0a 2a 2a 20 73 65 74 2c 20 74 s also.** set, t
aa560 68 65 6e 20 74 68 65 20 73 74 72 69 6e 67 20 69 hen the string i
aa570 73 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64 s nul terminated
aa580 2e 20 54 68 65 20 4d 45 4d 5f 49 6e 74 20 61 6e . The MEM_Int an
aa590 64 20 4d 45 4d 5f 52 65 61 6c 20 0a 2a 2a 20 66 d MEM_Real .** f
aa5a0 6c 61 67 73 20 6d 61 79 20 63 6f 65 78 69 73 74 lags may coexist
aa5b0 20 77 69 74 68 20 74 68 65 20 4d 45 4d 5f 53 74 with the MEM_St
aa5c0 72 20 66 6c 61 67 2e 0a 2a 2a 0a 2a 2a 20 4d 75 r flag..**.** Mu
aa5d0 6c 74 69 70 6c 65 20 6f 66 20 74 68 65 73 65 20 ltiple of these
aa5e0 76 61 6c 75 65 73 20 63 61 6e 20 61 70 70 65 61 values can appea
aa5f0 72 20 69 6e 20 4d 65 6d 2e 66 6c 61 67 73 2e 20 r in Mem.flags.
aa600 20 42 75 74 20 6f 6e 6c 79 20 6f 6e 65 0a 2a 2a But only one.**
aa610 20 61 74 20 61 20 74 69 6d 65 20 63 61 6e 20 61 at a time can a
aa620 70 70 65 61 72 20 69 6e 20 4d 65 6d 2e 74 79 70 ppear in Mem.typ
aa630 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 e..*/.#define ME
aa640 4d 5f 4e 75 6c 6c 20 20 20 20 20 20 30 78 30 30 M_Null 0x00
aa650 30 31 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 01 /* Value is
aa660 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 NULL */.#define
aa670 20 4d 45 4d 5f 53 74 72 20 20 20 20 20 20 20 30 MEM_Str 0
aa680 78 30 30 30 32 20 20 20 2f 2a 20 56 61 6c 75 65 x0002 /* Value
aa690 20 69 73 20 61 20 73 74 72 69 6e 67 20 2a 2f 0a is a string */.
aa6a0 23 64 65 66 69 6e 65 20 4d 45 4d 5f 49 6e 74 20 #define MEM_Int
aa6b0 20 20 20 20 20 20 30 78 30 30 30 34 20 20 20 2f 0x0004 /
aa6c0 2a 20 56 61 6c 75 65 20 69 73 20 61 6e 20 69 6e * Value is an in
aa6d0 74 65 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 teger */.#define
aa6e0 20 4d 45 4d 5f 52 65 61 6c 20 20 20 20 20 20 30 MEM_Real 0
aa6f0 78 30 30 30 38 20 20 20 2f 2a 20 56 61 6c 75 65 x0008 /* Value
aa700 20 69 73 20 61 20 72 65 61 6c 20 6e 75 6d 62 65 is a real numbe
aa710 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d r */.#define MEM
aa720 5f 42 6c 6f 62 20 20 20 20 20 20 30 78 30 30 31 _Blob 0x001
aa730 30 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20 0 /* Value is
aa740 61 20 42 4c 4f 42 20 2a 2f 0a 23 64 65 66 69 6e a BLOB */.#defin
aa750 65 20 4d 45 4d 5f 52 6f 77 53 65 74 20 20 20 20 e MEM_RowSet
aa760 30 78 30 30 32 30 20 20 20 2f 2a 20 56 61 6c 75 0x0020 /* Valu
aa770 65 20 69 73 20 61 20 52 6f 77 53 65 74 20 6f 62 e is a RowSet ob
aa780 6a 65 63 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ject */.#define
aa790 4d 45 4d 5f 46 72 61 6d 65 20 20 20 20 20 30 78 MEM_Frame 0x
aa7a0 30 30 34 30 20 20 20 2f 2a 20 56 61 6c 75 65 20 0040 /* Value
aa7b0 69 73 20 61 20 56 64 62 65 46 72 61 6d 65 20 6f is a VdbeFrame o
aa7c0 62 6a 65 63 74 20 2a 2f 0a 23 64 65 66 69 6e 65 bject */.#define
aa7d0 20 4d 45 4d 5f 54 79 70 65 4d 61 73 6b 20 20 30 MEM_TypeMask 0
aa7e0 78 30 30 66 66 20 20 20 2f 2a 20 4d 61 73 6b 20 x00ff /* Mask
aa7f0 6f 66 20 74 79 70 65 20 62 69 74 73 20 2a 2f 0a of type bits */.
aa800 0a 2f 2a 20 57 68 65 6e 65 76 65 72 20 4d 65 6d ./* Whenever Mem
aa810 20 63 6f 6e 74 61 69 6e 73 20 61 20 76 61 6c 69 contains a vali
aa820 64 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 d string or blob
aa830 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2c representation,
aa840 20 6f 6e 65 20 6f 66 0a 2a 2a 20 74 68 65 20 66 one of.** the f
aa850 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73 20 6d ollowing flags m
aa860 75 73 74 20 62 65 20 73 65 74 20 74 6f 20 64 65 ust be set to de
aa870 74 65 72 6d 69 6e 65 20 74 68 65 20 6d 65 6d 6f termine the memo
aa880 72 79 20 6d 61 6e 61 67 65 6d 65 6e 74 0a 2a 2a ry management.**
aa890 20 70 6f 6c 69 63 79 20 66 6f 72 20 4d 65 6d 2e policy for Mem.
aa8a0 7a 2e 20 20 54 68 65 20 4d 45 4d 5f 54 65 72 6d z. The MEM_Term
aa8b0 20 66 6c 61 67 20 74 65 6c 6c 73 20 75 73 20 77 flag tells us w
aa8c0 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 hether or not th
aa8d0 65 0a 2a 2a 20 73 74 72 69 6e 67 20 69 73 20 5c e.** string is \
aa8e0 30 30 30 20 6f 72 20 5c 75 30 30 30 30 20 74 65 000 or \u0000 te
aa8f0 72 6d 69 6e 61 74 65 64 0a 2a 2f 0a 23 64 65 66 rminated.*/.#def
aa900 69 6e 65 20 4d 45 4d 5f 54 65 72 6d 20 20 20 20 ine MEM_Term
aa910 20 20 30 78 30 32 30 30 20 20 20 2f 2a 20 53 74 0x0200 /* St
aa920 72 69 6e 67 20 72 65 70 20 69 73 20 6e 75 6c 20 ring rep is nul
aa930 74 65 72 6d 69 6e 61 74 65 64 20 2a 2f 0a 23 64 terminated */.#d
aa940 65 66 69 6e 65 20 4d 45 4d 5f 44 79 6e 20 20 20 efine MEM_Dyn
aa950 20 20 20 20 30 78 30 34 30 30 20 20 20 2f 2a 20 0x0400 /*
aa960 4e 65 65 64 20 74 6f 20 63 61 6c 6c 20 73 71 6c Need to call sql
aa970 69 74 65 46 72 65 65 28 29 20 6f 6e 20 4d 65 6d iteFree() on Mem
aa980 2e 7a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 .z */.#define ME
aa990 4d 5f 53 74 61 74 69 63 20 20 20 20 30 78 30 38 M_Static 0x08
aa9a0 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 00 /* Mem.z po
aa9b0 69 6e 74 73 20 74 6f 20 61 20 73 74 61 74 69 63 ints to a static
aa9c0 20 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 string */.#defi
aa9d0 6e 65 20 4d 45 4d 5f 45 70 68 65 6d 20 20 20 20 ne MEM_Ephem
aa9e0 20 30 78 31 30 30 30 20 20 20 2f 2a 20 4d 65 6d 0x1000 /* Mem
aa9f0 2e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 .z points to an
aaa00 65 70 68 65 6d 65 72 61 6c 20 73 74 72 69 6e 67 ephemeral string
aaa10 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f */.#define MEM_
aaa20 41 67 67 20 20 20 20 20 20 20 30 78 32 30 30 30 Agg 0x2000
aaa30 20 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e /* Mem.z poin
aaa40 74 73 20 74 6f 20 61 6e 20 61 67 67 20 66 75 6e ts to an agg fun
aaa50 63 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f ction context */
aaa60 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 5a 65 72 .#define MEM_Zer
aaa70 6f 20 20 20 20 20 20 30 78 34 30 30 30 20 20 20 o 0x4000
aaa80 2f 2a 20 4d 65 6d 2e 69 20 63 6f 6e 74 61 69 6e /* Mem.i contain
aaa90 73 20 63 6f 75 6e 74 20 6f 66 20 30 73 20 61 70 s count of 0s ap
aaaa0 70 65 6e 64 65 64 20 74 6f 20 62 6c 6f 62 20 2a pended to blob *
aaab0 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 /..#ifdef SQLITE
aaac0 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 _OMIT_INCRBLOB.
aaad0 20 23 75 6e 64 65 66 20 4d 45 4d 5f 5a 65 72 6f #undef MEM_Zero
aaae0 0a 20 20 23 64 65 66 69 6e 65 20 4d 45 4d 5f 5a . #define MEM_Z
aaaf0 65 72 6f 20 30 78 30 30 30 30 0a 23 65 6e 64 69 ero 0x0000.#endi
aab00 66 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 f.../*.** Clear
aab10 61 6e 79 20 65 78 69 73 74 69 6e 67 20 74 79 70 any existing typ
aab20 65 20 66 6c 61 67 73 20 66 72 6f 6d 20 61 20 4d e flags from a M
aab30 65 6d 20 61 6e 64 20 72 65 70 6c 61 63 65 20 74 em and replace t
aab40 68 65 6d 20 77 69 74 68 20 66 0a 2a 2f 0a 23 64 hem with f.*/.#d
aab50 65 66 69 6e 65 20 4d 65 6d 53 65 74 54 79 70 65 efine MemSetType
aab60 46 6c 61 67 28 70 2c 20 66 29 20 5c 0a 20 20 20 Flag(p, f) \.
aab70 28 28 70 29 2d 3e 66 6c 61 67 73 20 3d 20 28 28 ((p)->flags = ((
aab80 70 29 2d 3e 66 6c 61 67 73 26 7e 28 4d 45 4d 5f p)->flags&~(MEM_
aab90 54 79 70 65 4d 61 73 6b 7c 4d 45 4d 5f 5a 65 72 TypeMask|MEM_Zer
aaba0 6f 29 29 7c 66 29 0a 0a 0a 2f 2a 20 41 20 56 64 o))|f).../* A Vd
aabb0 62 65 46 75 6e 63 20 69 73 20 6a 75 73 74 20 61 beFunc is just a
aabc0 20 46 75 6e 63 44 65 66 20 28 64 65 66 69 6e 65 FuncDef (define
aabd0 64 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 d in sqliteInt.h
aabe0 29 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 0a ) that contains.
aabf0 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e ** additional in
aac00 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 formation about
aac10 61 75 78 69 6c 69 61 72 79 20 69 6e 66 6f 72 6d auxiliary inform
aac20 61 74 69 6f 6e 20 62 6f 75 6e 64 20 74 6f 20 61 ation bound to a
aac30 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 6f 66 20 74 rguments.** of t
aac40 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 he function. Th
aac50 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 69 6d is is used to im
aac60 70 6c 65 6d 65 6e 74 20 74 68 65 20 73 71 6c 69 plement the sqli
aac70 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28 te3_get_auxdata(
aac80 29 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 ).** and sqlite3
aac90 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 41 _set_auxdata() A
aaca0 50 49 73 2e 20 20 54 68 65 20 22 61 75 78 64 61 PIs. The "auxda
aacb0 74 61 22 20 69 73 20 73 6f 6d 65 20 61 75 78 69 ta" is some auxi
aacc0 6c 69 61 72 79 20 64 61 74 61 0a 2a 2a 20 74 68 liary data.** th
aacd0 61 74 20 63 61 6e 20 62 65 20 61 73 73 6f 63 69 at can be associ
aace0 61 74 65 64 20 77 69 74 68 20 61 20 63 6f 6e 73 ated with a cons
aacf0 74 61 6e 74 20 61 72 67 75 6d 65 6e 74 20 74 6f tant argument to
aad00 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 a function. Th
aad10 69 73 0a 2a 2a 20 61 6c 6c 6f 77 73 20 66 75 6e is.** allows fun
aad20 63 74 69 6f 6e 73 20 73 75 63 68 20 61 73 20 22 ctions such as "
aad30 72 65 67 65 78 70 22 20 74 6f 20 63 6f 6d 70 69 regexp" to compi
aad40 6c 65 20 74 68 65 69 72 20 63 6f 6e 73 74 61 6e le their constan
aad50 74 20 72 65 67 75 6c 61 72 0a 2a 2a 20 65 78 70 t regular.** exp
aad60 72 65 73 73 69 6f 6e 20 61 72 67 75 6d 65 6e 74 ression argument
aad70 20 6f 6e 63 65 20 61 6e 64 20 72 65 75 73 65 64 once and reused
aad80 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 20 63 6f the compiled co
aad90 64 65 20 66 6f 72 20 6d 75 6c 74 69 70 6c 65 0a de for multiple.
aada0 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 73 2e 0a ** invocations..
aadb0 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65 46 75 */.struct VdbeFu
aadc0 6e 63 20 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a nc {. FuncDef *
aadd0 70 46 75 6e 63 3b 20 20 20 20 20 20 20 20 20 20 pFunc;
aade0 20 20 20 20 20 2f 2a 20 54 68 65 20 64 65 66 69 /* The defi
aadf0 6e 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 66 75 nition of the fu
aae00 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 nction */. int
aae10 6e 41 75 78 3b 20 20 20 20 20 20 20 20 20 20 20 nAux;
aae20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
aae30 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 61 ber of entries a
aae40 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 70 41 llocated for apA
aae50 75 78 5b 5d 20 2a 2f 0a 20 20 73 74 72 75 63 74 ux[] */. struct
aae60 20 41 75 78 44 61 74 61 20 7b 0a 20 20 20 20 76 AuxData {. v
aae70 6f 69 64 20 2a 70 41 75 78 3b 20 20 20 20 20 20 oid *pAux;
aae80 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
aae90 41 75 78 20 64 61 74 61 20 66 6f 72 20 74 68 65 Aux data for the
aaea0 20 69 2d 74 68 20 61 72 67 75 6d 65 6e 74 20 2a i-th argument *
aaeb0 2f 0a 20 20 20 20 76 6f 69 64 20 28 2a 78 44 65 /. void (*xDe
aaec0 6c 65 74 65 29 28 76 6f 69 64 20 2a 29 3b 20 20 lete)(void *);
aaed0 20 20 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f /* Destructo
aaee0 72 20 66 6f 72 20 74 68 65 20 61 75 78 20 64 61 r for the aux da
aaef0 74 61 20 2a 2f 0a 20 20 7d 20 61 70 41 75 78 5b ta */. } apAux[
aaf00 31 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 1];
aaf10 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 73 6c 6f /* One slo
aaf20 74 20 66 6f 72 20 65 61 63 68 20 66 75 6e 63 74 t for each funct
aaf30 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a ion argument */.
aaf40 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22 63 };../*.** The "c
aaf50 6f 6e 74 65 78 74 22 20 61 72 67 75 6d 65 6e 74 ontext" argument
aaf60 20 66 6f 72 20 61 20 69 6e 73 74 61 6c 6c 61 62 for a installab
aaf70 6c 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20 41 20 le function. A
aaf80 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a pointer to an.**
aaf90 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 instance of thi
aafa0 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 74 s structure is t
aafb0 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
aafc0 74 20 74 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 t to the routine
aafd0 73 20 75 73 65 64 0a 2a 2a 20 69 6d 70 6c 65 6d s used.** implem
aafe0 65 6e 74 20 74 68 65 20 53 51 4c 20 66 75 6e 63 ent the SQL func
aaff0 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 tions..**.** The
ab000 72 65 20 69 73 20 61 20 74 79 70 65 64 65 66 20 re is a typedef
ab010 66 6f 72 20 74 68 69 73 20 73 74 72 75 63 74 75 for this structu
ab020 72 65 20 69 6e 20 73 71 6c 69 74 65 2e 68 2e 20 re in sqlite.h.
ab030 20 53 6f 20 61 6c 6c 20 72 6f 75 74 69 6e 65 73 So all routines
ab040 2c 0a 2a 2a 20 65 76 65 6e 20 74 68 65 20 70 75 ,.** even the pu
ab050 62 6c 69 63 20 69 6e 74 65 72 66 61 63 65 20 74 blic interface t
ab060 6f 20 53 51 4c 69 74 65 2c 20 63 61 6e 20 75 73 o SQLite, can us
ab070 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 e a pointer to t
ab080 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a his structure..*
ab090 2a 20 42 75 74 20 74 68 69 73 20 66 69 6c 65 20 * But this file
ab0a0 69 73 20 74 68 65 20 6f 6e 6c 79 20 70 6c 61 63 is the only plac
ab0b0 65 20 77 68 65 72 65 20 74 68 65 20 69 6e 74 65 e where the inte
ab0c0 72 6e 61 6c 20 64 65 74 61 69 6c 73 20 6f 66 20 rnal details of
ab0d0 74 68 69 73 0a 2a 2a 20 73 74 72 75 63 74 75 72 this.** structur
ab0e0 65 20 61 72 65 20 6b 6e 6f 77 6e 2e 0a 2a 2a 0a e are known..**.
ab0f0 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72 ** This structur
ab100 65 20 69 73 20 64 65 66 69 6e 65 64 20 69 6e 73 e is defined ins
ab110 69 64 65 20 6f 66 20 76 64 62 65 49 6e 74 2e 68 ide of vdbeInt.h
ab120 20 62 65 63 61 75 73 65 20 69 74 20 75 73 65 73 because it uses
ab130 20 73 75 62 73 74 72 75 63 74 75 72 65 73 0a 2a substructures.*
ab140 2a 20 28 4d 65 6d 29 20 77 68 69 63 68 20 61 72 * (Mem) which ar
ab150 65 20 6f 6e 6c 79 20 64 65 66 69 6e 65 64 20 74 e only defined t
ab160 68 65 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 here..*/.struct
ab170 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
ab180 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 46 75 {. FuncDef *pFu
ab190 6e 63 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 nc; /* Poi
ab1a0 6e 74 65 72 20 74 6f 20 66 75 6e 63 74 69 6f 6e nter to function
ab1b0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 4d information. M
ab1c0 55 53 54 20 42 45 20 46 49 52 53 54 20 2a 2f 0a UST BE FIRST */.
ab1d0 20 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62 VdbeFunc *pVdb
ab1e0 65 46 75 6e 63 3b 20 20 2f 2a 20 41 75 78 69 6c eFunc; /* Auxil
ab1f0 61 72 79 20 64 61 74 61 2c 20 69 66 20 63 72 65 ary data, if cre
ab200 61 74 65 64 2e 20 2a 2f 0a 20 20 4d 65 6d 20 73 ated. */. Mem s
ab210 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
ab220 20 2f 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 /* The return v
ab230 61 6c 75 65 20 69 73 20 73 74 6f 72 65 64 20 68 alue is stored h
ab240 65 72 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d ere */. Mem *pM
ab250 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f em; /
ab260 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 75 73 * Memory cell us
ab270 65 64 20 74 6f 20 73 74 6f 72 65 20 61 67 67 72 ed to store aggr
ab280 65 67 61 74 65 20 63 6f 6e 74 65 78 74 20 2a 2f egate context */
ab290 0a 20 20 69 6e 74 20 69 73 45 72 72 6f 72 3b 20 . int isError;
ab2a0 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f /* Erro
ab2b0 72 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64 20 r code returned
ab2c0 62 79 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e by the function.
ab2d0 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 */. CollSeq *p
ab2e0 43 6f 6c 6c 3b 20 20 20 20 20 20 20 2f 2a 20 43 Coll; /* C
ab2f0 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
ab300 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 e */.};../*.** A
ab310 20 53 65 74 20 73 74 72 75 63 74 75 72 65 20 69 Set structure i
ab320 73 20 75 73 65 64 20 66 6f 72 20 71 75 69 63 6b s used for quick
ab330 20 74 65 73 74 69 6e 67 20 74 6f 20 73 65 65 20 testing to see
ab340 69 66 20 61 20 76 61 6c 75 65 0a 2a 2a 20 69 73 if a value.** is
ab350 20 70 61 72 74 20 6f 66 20 61 20 73 6d 61 6c 6c part of a small
ab360 20 73 65 74 2e 20 20 53 65 74 73 20 61 72 65 20 set. Sets are
ab370 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e used to implemen
ab380 74 20 63 6f 64 65 20 6c 69 6b 65 0a 2a 2a 20 74 t code like.** t
ab390 68 69 73 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20 his:.**
ab3a0 20 20 20 78 2e 79 20 49 4e 20 28 27 68 69 27 2c x.y IN ('hi',
ab3b0 27 68 6f 6f 27 2c 27 68 75 6d 27 29 0a 2a 2f 0a 'hoo','hum').*/.
ab3c0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 typedef struct S
ab3d0 65 74 20 53 65 74 3b 0a 73 74 72 75 63 74 20 53 et Set;.struct S
ab3e0 65 74 20 7b 0a 20 20 48 61 73 68 20 68 61 73 68 et {. Hash hash
ab3f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
ab400 20 41 20 73 65 74 20 69 73 20 6a 75 73 74 20 61 A set is just a
ab410 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 hash table */.
ab420 20 48 61 73 68 45 6c 65 6d 20 2a 70 72 65 76 3b HashElem *prev;
ab430 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 76 69 /* Previ
ab440 6f 75 73 6c 79 20 61 63 63 65 73 73 65 64 20 68 ously accessed h
ab450 61 73 68 20 65 6c 65 6d 65 6e 20 2a 2f 0a 7d 3b ash elemen */.};
ab460 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 ../*.** An insta
ab470 6e 63 65 20 6f 66 20 74 68 65 20 76 69 72 74 75 nce of the virtu
ab480 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20 54 68 69 al machine. Thi
ab490 73 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 s structure cont
ab4a0 61 69 6e 73 20 74 68 65 20 63 6f 6d 70 6c 65 74 ains the complet
ab4b0 65 0a 2a 2a 20 73 74 61 74 65 20 6f 66 20 74 68 e.** state of th
ab4c0 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e e virtual machin
ab4d0 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 73 71 e..**.** The "sq
ab4e0 6c 69 74 65 33 5f 73 74 6d 74 22 20 73 74 72 75 lite3_stmt" stru
ab4f0 63 74 75 72 65 20 70 6f 69 6e 74 65 72 20 74 68 cture pointer th
ab500 61 74 20 69 73 20 72 65 74 75 72 6e 65 64 20 62 at is returned b
ab510 79 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 69 6c y sqlite3_compil
ab520 65 28 29 0a 2a 2a 20 69 73 20 72 65 61 6c 6c 79 e().** is really
ab530 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e a pointer to an
ab540 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 instance of thi
ab550 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a s structure..**.
ab560 2a 2a 20 54 68 65 20 56 64 62 65 2e 69 6e 56 74 ** The Vdbe.inVt
ab570 61 62 4d 65 74 68 6f 64 20 76 61 72 69 61 62 6c abMethod variabl
ab580 65 20 69 73 20 73 65 74 20 74 6f 20 6e 6f 6e 2d e is set to non-
ab590 7a 65 72 6f 20 66 6f 72 20 74 68 65 20 64 75 72 zero for the dur
ab5a0 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 61 6e 79 20 ation of.** any
ab5b0 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 65 virtual table me
ab5c0 74 68 6f 64 20 69 6e 76 6f 63 61 74 69 6f 6e 73 thod invocations
ab5d0 20 6d 61 64 65 20 62 79 20 74 68 65 20 76 64 62 made by the vdb
ab5e0 65 20 70 72 6f 67 72 61 6d 2e 20 49 74 20 69 73 e program. It is
ab5f0 0a 2a 2a 20 73 65 74 20 74 6f 20 32 20 66 6f 72 .** set to 2 for
ab600 20 78 44 65 73 74 72 6f 79 20 6d 65 74 68 6f 64 xDestroy method
ab610 20 63 61 6c 6c 73 20 61 6e 64 20 31 20 66 6f 72 calls and 1 for
ab620 20 61 6c 6c 20 6f 74 68 65 72 20 6d 65 74 68 6f all other metho
ab630 64 73 2e 20 54 68 69 73 0a 2a 2a 20 76 61 72 69 ds. This.** vari
ab640 61 62 6c 65 20 69 73 20 75 73 65 64 20 66 6f 72 able is used for
ab650 20 74 77 6f 20 70 75 72 70 6f 73 65 73 3a 20 74 two purposes: t
ab660 6f 20 61 6c 6c 6f 77 20 78 44 65 73 74 72 6f 79 o allow xDestroy
ab670 20 6d 65 74 68 6f 64 73 20 74 6f 20 65 78 65 63 methods to exec
ab680 75 74 65 0a 2a 2a 20 22 44 52 4f 50 20 54 41 42 ute.** "DROP TAB
ab690 4c 45 22 20 73 74 61 74 65 6d 65 6e 74 73 20 61 LE" statements a
ab6a0 6e 64 20 74 6f 20 70 72 65 76 65 6e 74 20 73 6f nd to prevent so
ab6b0 6d 65 20 6e 61 73 74 79 20 73 69 64 65 20 65 66 me nasty side ef
ab6c0 66 65 63 74 73 20 6f 66 0a 2a 2a 20 6d 61 6c 6c fects of.** mall
ab6d0 6f 63 20 66 61 69 6c 75 72 65 20 77 68 65 6e 20 oc failure when
ab6e0 53 51 4c 69 74 65 20 69 73 20 69 6e 76 6f 6b 65 SQLite is invoke
ab6f0 64 20 72 65 63 75 72 73 69 76 65 6c 79 20 62 79 d recursively by
ab700 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 a virtual table
ab710 20 0a 2a 2a 20 6d 65 74 68 6f 64 20 66 75 6e 63 .** method func
ab720 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 tion..*/.struct
ab730 56 64 62 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 Vdbe {. sqlite3
ab740 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 *db;
ab750 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 /* The database
ab760 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 connection that
ab770 20 6f 77 6e 73 20 74 68 69 73 20 73 74 61 74 65 owns this state
ab780 6d 65 6e 74 20 2a 2f 0a 20 20 56 64 62 65 20 2a ment */. Vdbe *
ab790 70 50 72 65 76 2c 2a 70 4e 65 78 74 3b 20 20 20 pPrev,*pNext;
ab7a0 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74 /* Linked list
ab7b0 20 6f 66 20 56 44 42 45 73 20 77 69 74 68 20 74 of VDBEs with t
ab7c0 68 65 20 73 61 6d 65 20 56 64 62 65 2e 64 62 20 he same Vdbe.db
ab7d0 2a 2f 0a 20 20 69 6e 74 20 6e 4f 70 3b 20 20 20 */. int nOp;
ab7e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
ab7f0 4e 75 6d 62 65 72 20 6f 66 20 69 6e 73 74 72 75 Number of instru
ab800 63 74 69 6f 6e 73 20 69 6e 20 74 68 65 20 70 72 ctions in the pr
ab810 6f 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e ogram */. int n
ab820 4f 70 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 OpAlloc;
ab830 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
ab840 73 6c 6f 74 73 20 61 6c 6c 6f 63 61 74 65 64 20 slots allocated
ab850 66 6f 72 20 61 4f 70 5b 5d 20 2a 2f 0a 20 20 4f for aOp[] */. O
ab860 70 20 2a 61 4f 70 3b 20 20 20 20 20 20 20 20 20 p *aOp;
ab870 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 /* Space
ab880 74 6f 20 68 6f 6c 64 20 74 68 65 20 76 69 72 74 to hold the virt
ab890 75 61 6c 20 6d 61 63 68 69 6e 65 27 73 20 70 72 ual machine's pr
ab8a0 6f 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e ogram */. int n
ab8b0 4c 61 62 65 6c 3b 20 20 20 20 20 20 20 20 20 20 Label;
ab8c0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
ab8d0 6c 61 62 65 6c 73 20 75 73 65 64 20 2a 2f 0a 20 labels used */.
ab8e0 20 69 6e 74 20 6e 4c 61 62 65 6c 41 6c 6c 6f 63 int nLabelAlloc
ab8f0 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 ; /* Numb
ab900 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f er of slots allo
ab910 63 61 74 65 64 20 69 6e 20 61 4c 61 62 65 6c 5b cated in aLabel[
ab920 5d 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 4c 61 62 ] */. int *aLab
ab930 65 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f el; /
ab940 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 * Space to hold
ab950 74 68 65 20 6c 61 62 65 6c 73 20 2a 2f 0a 20 20 the labels */.
ab960 4d 65 6d 20 2a 2a 61 70 41 72 67 3b 20 20 20 20 Mem **apArg;
ab970 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d /* Argum
ab980 65 6e 74 73 20 74 6f 20 63 75 72 72 65 6e 74 6c ents to currentl
ab990 79 20 65 78 65 63 75 74 69 6e 67 20 75 73 65 72 y executing user
ab9a0 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 4d function */. M
ab9b0 65 6d 20 2a 61 43 6f 6c 4e 61 6d 65 3b 20 20 20 em *aColName;
ab9c0 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e /* Column
ab9d0 20 6e 61 6d 65 73 20 74 6f 20 72 65 74 75 72 6e names to return
ab9e0 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52 65 73 75 */. Mem *pResu
ab9f0 6c 74 53 65 74 3b 20 20 20 20 20 20 20 20 2f 2a ltSet; /*
aba00 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 61 Pointer to an a
aba10 72 72 61 79 20 6f 66 20 72 65 73 75 6c 74 73 20 rray of results
aba20 2a 2f 0a 20 20 75 31 36 20 6e 52 65 73 43 6f 6c */. u16 nResCol
aba30 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 umn; /*
aba40 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e Number of column
aba50 73 20 69 6e 20 6f 6e 65 20 72 6f 77 20 6f 66 20 s in one row of
aba60 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a the result set *
aba70 2f 0a 20 20 75 31 36 20 6e 43 75 72 73 6f 72 3b /. u16 nCursor;
aba80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
aba90 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 69 umber of slots i
abaa0 6e 20 61 70 43 73 72 5b 5d 20 2a 2f 0a 20 20 56 n apCsr[] */. V
abab0 64 62 65 43 75 72 73 6f 72 20 2a 2a 61 70 43 73 dbeCursor **apCs
abac0 72 3b 20 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6c r; /* One el
abad0 65 6d 65 6e 74 20 6f 66 20 74 68 69 73 20 61 72 ement of this ar
abae0 72 61 79 20 66 6f 72 20 65 61 63 68 20 6f 70 65 ray for each ope
abaf0 6e 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 75 38 n cursor */. u8
abb00 20 65 72 72 6f 72 41 63 74 69 6f 6e 3b 20 20 20 errorAction;
abb10 20 20 20 20 20 20 2f 2a 20 52 65 63 6f 76 65 72 /* Recover
abb20 79 20 61 63 74 69 6f 6e 20 74 6f 20 64 6f 20 69 y action to do i
abb30 6e 20 63 61 73 65 20 6f 66 20 61 6e 20 65 72 72 n case of an err
abb40 6f 72 20 2a 2f 0a 20 20 75 38 20 6f 6b 56 61 72 or */. u8 okVar
abb50 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
abb60 2f 2a 20 54 72 75 65 20 69 66 20 61 7a 56 61 72 /* True if azVar
abb70 5b 5d 20 68 61 73 20 62 65 65 6e 20 69 6e 69 74 [] has been init
abb80 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 79 6e 56 ialized */. ynV
abb90 61 72 20 6e 56 61 72 3b 20 20 20 20 20 20 20 20 ar nVar;
abba0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
abbb0 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 56 61 f entries in aVa
abbc0 72 5b 5d 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 56 r[] */. Mem *aV
abbd0 61 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ar;
abbe0 20 2f 2a 20 56 61 6c 75 65 73 20 66 6f 72 20 74 /* Values for t
abbf0 68 65 20 4f 50 5f 56 61 72 69 61 62 6c 65 20 6f he OP_Variable o
abc00 70 63 6f 64 65 2e 20 2a 2f 0a 20 20 63 68 61 72 pcode. */. char
abc10 20 2a 2a 61 7a 56 61 72 3b 20 20 20 20 20 20 20 **azVar;
abc20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 76 /* Name of v
abc30 61 72 69 61 62 6c 65 73 20 2a 2f 0a 20 20 75 33 ariables */. u3
abc40 32 20 6d 61 67 69 63 3b 20 20 20 20 20 20 20 20 2 magic;
abc50 20 20 20 20 20 20 2f 2a 20 4d 61 67 69 63 20 6e /* Magic n
abc60 75 6d 62 65 72 20 66 6f 72 20 73 61 6e 69 74 79 umber for sanity
abc70 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20 69 checking */. i
abc80 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 nt nMem;
abc90 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
abca0 20 6f 66 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 of memory locat
abcb0 69 6f 6e 73 20 63 75 72 72 65 6e 74 6c 79 20 61 ions currently a
abcc0 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20 4d 65 llocated */. Me
abcd0 6d 20 2a 61 4d 65 6d 3b 20 20 20 20 20 20 20 20 m *aMem;
abce0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 65 6d /* The mem
abcf0 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f ory locations */
abd00 0a 20 20 75 33 32 20 63 61 63 68 65 43 74 72 3b . u32 cacheCtr;
abd10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 64 /* Vd
abd20 62 65 43 75 72 73 6f 72 20 72 6f 77 20 63 61 63 beCursor row cac
abd30 68 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 63 6f he generation co
abd40 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 unter */. int p
abd50 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
abd60 20 20 20 2f 2a 20 54 68 65 20 70 72 6f 67 72 61 /* The progra
abd70 6d 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 m counter */. i
abd80 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 nt rc;
abd90 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 /* Value
abda0 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 63 to return */. c
abdb0 68 61 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 har *zErrMsg;
abdc0 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 /* Error
abdd0 6d 65 73 73 61 67 65 20 77 72 69 74 74 65 6e 20 message written
abde0 68 65 72 65 20 2a 2f 0a 20 20 75 38 20 65 78 70 here */. u8 exp
abdf0 6c 61 69 6e 3b 20 20 20 20 20 20 20 20 20 20 20 lain;
abe00 20 20 2f 2a 20 54 72 75 65 20 69 66 20 45 58 50 /* True if EXP
abe10 4c 41 49 4e 20 70 72 65 73 65 6e 74 20 6f 6e 20 LAIN present on
abe20 53 51 4c 20 63 6f 6d 6d 61 6e 64 20 2a 2f 0a 20 SQL command */.
abe30 20 75 38 20 63 68 61 6e 67 65 43 6e 74 4f 6e 3b u8 changeCntOn;
abe40 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
abe50 20 74 6f 20 75 70 64 61 74 65 20 74 68 65 20 63 to update the c
abe60 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 2a 2f hange-counter */
abe70 0a 20 20 75 38 20 65 78 70 69 72 65 64 3b 20 20 . u8 expired;
abe80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
abe90 75 65 20 69 66 20 74 68 65 20 56 4d 20 6e 65 65 ue if the VM nee
abea0 64 73 20 74 6f 20 62 65 20 72 65 63 6f 6d 70 69 ds to be recompi
abeb0 6c 65 64 20 2a 2f 0a 20 20 75 38 20 6d 69 6e 57 led */. u8 minW
abec0 72 69 74 65 46 69 6c 65 46 6f 72 6d 61 74 3b 20 riteFileFormat;
abed0 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 66 69 6c 65 /* Minimum file
abee0 20 66 6f 72 6d 61 74 20 66 6f 72 20 77 72 69 74 format for writ
abef0 61 62 6c 65 20 64 61 74 61 62 61 73 65 20 66 69 able database fi
abf00 6c 65 73 20 2a 2f 0a 20 20 75 38 20 69 6e 56 74 les */. u8 inVt
abf10 61 62 4d 65 74 68 6f 64 3b 20 20 20 20 20 20 20 abMethod;
abf20 20 2f 2a 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 /* See comments
abf30 20 61 62 6f 76 65 20 2a 2f 0a 20 20 75 38 20 75 above */. u8 u
abf40 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 3b 20 sesStmtJournal;
abf50 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 75 /* True if u
abf60 73 65 73 20 61 20 73 74 61 74 65 6d 65 6e 74 20 ses a statement
abf70 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 38 20 journal */. u8
abf80 72 65 61 64 4f 6e 6c 79 3b 20 20 20 20 20 20 20 readOnly;
abf90 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 /* True for
abfa0 20 72 65 61 64 2d 6f 6e 6c 79 20 73 74 61 74 65 read-only state
abfb0 6d 65 6e 74 73 20 2a 2f 0a 20 20 75 38 20 69 73 ments */. u8 is
abfc0 50 72 65 70 61 72 65 56 32 3b 20 20 20 20 20 20 PrepareV2;
abfd0 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 72 /* True if pr
abfe0 65 70 61 72 65 64 20 77 69 74 68 20 70 72 65 70 epared with prep
abff0 61 72 65 5f 76 32 28 29 20 2a 2f 0a 20 20 69 6e are_v2() */. in
ac000 74 20 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20 t nChange;
ac010 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
ac020 6f 66 20 64 62 20 63 68 61 6e 67 65 73 20 6d 61 of db changes ma
ac030 64 65 20 73 69 6e 63 65 20 6c 61 73 74 20 72 65 de since last re
ac040 73 65 74 20 2a 2f 0a 20 20 69 6e 74 20 62 74 72 set */. int btr
ac050 65 65 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 eeMask;
ac060 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 64 /* Bitmask of d
ac070 62 2d 3e 61 44 62 5b 5d 20 65 6e 74 72 69 65 73 b->aDb[] entries
ac080 20 72 65 66 65 72 65 6e 63 65 64 20 2a 2f 0a 20 referenced */.
ac090 20 69 36 34 20 73 74 61 72 74 54 69 6d 65 3b 20 i64 startTime;
ac0a0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 69 6d 65 /* Time
ac0b0 20 77 68 65 6e 20 71 75 65 72 79 20 73 74 61 72 when query star
ac0c0 74 65 64 20 2d 20 75 73 65 64 20 66 6f 72 20 70 ted - used for p
ac0d0 72 6f 66 69 6c 69 6e 67 20 2a 2f 0a 20 20 42 74 rofiling */. Bt
ac0e0 72 65 65 4d 75 74 65 78 41 72 72 61 79 20 61 4d reeMutexArray aM
ac0f0 75 74 65 78 3b 20 2f 2a 20 41 6e 20 61 72 72 61 utex; /* An arra
ac100 79 20 6f 66 20 42 74 72 65 65 20 75 73 65 64 20 y of Btree used
ac110 68 65 72 65 20 61 6e 64 20 6e 65 65 64 69 6e 67 here and needing
ac120 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20 69 6e 74 20 locks */. int
ac130 61 43 6f 75 6e 74 65 72 5b 32 5d 3b 20 20 20 20 aCounter[2];
ac140 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 73 20 /* Counters
ac150 75 73 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f used by sqlite3_
ac160 73 74 6d 74 5f 73 74 61 74 75 73 28 29 20 2a 2f stmt_status() */
ac170 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 20 20 . char *zSql;
ac180 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 /* Te
ac190 78 74 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74 xt of the SQL st
ac1a0 61 74 65 6d 65 6e 74 20 74 68 61 74 20 67 65 6e atement that gen
ac1b0 65 72 61 74 65 64 20 74 68 69 73 20 2a 2f 0a 20 erated this */.
ac1c0 20 76 6f 69 64 20 2a 70 46 72 65 65 3b 20 20 20 void *pFree;
ac1d0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 /* Free
ac1e0 20 74 68 69 73 20 77 68 65 6e 20 64 65 6c 65 74 this when delet
ac1f0 69 6e 67 20 74 68 65 20 76 64 62 65 20 2a 2f 0a ing the vdbe */.
ac200 20 20 69 36 34 20 6e 46 6b 43 6f 6e 73 74 72 61 i64 nFkConstra
ac210 69 6e 74 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d int; /* Num
ac220 62 65 72 20 6f 66 20 69 6d 6d 2e 20 46 4b 20 63 ber of imm. FK c
ac230 6f 6e 73 74 72 61 69 6e 74 73 20 74 68 69 73 20 onstraints this
ac240 56 4d 20 2a 2f 0a 20 20 69 36 34 20 6e 53 74 6d VM */. i64 nStm
ac250 74 44 65 66 43 6f 6e 73 3b 20 20 20 20 20 20 20 tDefCons;
ac260 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 65 66 /* Number of def
ac270 2e 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 77 68 . constraints wh
ac280 65 6e 20 73 74 6d 74 20 73 74 61 72 74 65 64 20 en stmt started
ac290 2a 2f 0a 20 20 69 6e 74 20 69 53 74 61 74 65 6d */. int iStatem
ac2a0 65 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 ent; /*
ac2b0 53 74 61 74 65 6d 65 6e 74 20 6e 75 6d 62 65 72 Statement number
ac2c0 20 28 6f 72 20 30 20 69 66 20 68 61 73 20 6e 6f (or 0 if has no
ac2d0 74 20 6f 70 65 6e 65 64 20 73 74 6d 74 29 20 2a t opened stmt) *
ac2e0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
ac2f0 44 45 42 55 47 0a 20 20 46 49 4c 45 20 2a 74 72 DEBUG. FILE *tr
ac300 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ace;
ac310 2f 2a 20 57 72 69 74 65 20 61 6e 20 65 78 65 63 /* Write an exec
ac320 75 74 69 6f 6e 20 74 72 61 63 65 20 68 65 72 65 ution trace here
ac330 2c 20 69 66 20 6e 6f 74 20 4e 55 4c 4c 20 2a 2f , if not NULL */
ac340 0a 23 65 6e 64 69 66 0a 20 20 56 64 62 65 46 72 .#endif. VdbeFr
ac350 61 6d 65 20 2a 70 46 72 61 6d 65 3b 20 20 20 20 ame *pFrame;
ac360 20 20 2f 2a 20 50 61 72 65 6e 74 20 66 72 61 6d /* Parent fram
ac370 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 72 61 6d e */. int nFram
ac380 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f e; /
ac390 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 61 6d * Number of fram
ac3a0 65 73 20 69 6e 20 70 46 72 61 6d 65 20 6c 69 73 es in pFrame lis
ac3b0 74 20 2a 2f 0a 20 20 75 33 32 20 65 78 70 6d 61 t */. u32 expma
ac3c0 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f sk; /
ac3d0 2a 20 42 69 6e 64 69 6e 67 20 74 6f 20 74 68 65 * Binding to the
ac3e0 73 65 20 76 61 72 73 20 69 6e 76 61 6c 69 64 61 se vars invalida
ac3f0 74 65 73 20 56 4d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a tes VM */.};../*
ac400 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
ac410 67 20 61 72 65 20 61 6c 6c 6f 77 65 64 20 76 61 g are allowed va
ac420 6c 75 65 73 20 66 6f 72 20 56 64 62 65 2e 6d 61 lues for Vdbe.ma
ac430 67 69 63 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 56 gic.*/.#define V
ac440 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 20 DBE_MAGIC_INIT
ac450 20 20 20 30 78 32 36 62 63 65 61 61 35 20 20 20 0x26bceaa5
ac460 20 2f 2a 20 42 75 69 6c 64 69 6e 67 20 61 20 56 /* Building a V
ac470 44 42 45 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 23 DBE program */.#
ac480 64 65 66 69 6e 65 20 56 44 42 45 5f 4d 41 47 49 define VDBE_MAGI
ac490 43 5f 52 55 4e 20 20 20 20 20 20 30 78 62 64 66 C_RUN 0xbdf
ac4a0 32 30 64 61 33 20 20 20 20 2f 2a 20 56 44 42 45 20da3 /* VDBE
ac4b0 20 69 73 20 72 65 61 64 79 20 74 6f 20 65 78 65 is ready to exe
ac4c0 63 75 74 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 cute */.#define
ac4d0 56 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 VDBE_MAGIC_HALT
ac4e0 20 20 20 20 30 78 35 31 39 63 32 39 37 33 20 20 0x519c2973
ac4f0 20 20 2f 2a 20 56 44 42 45 20 68 61 73 20 63 6f /* VDBE has co
ac500 6d 70 6c 65 74 65 64 20 65 78 65 63 75 74 69 6f mpleted executio
ac510 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 n */.#define VDB
ac520 45 5f 4d 41 47 49 43 5f 44 45 41 44 20 20 20 20 E_MAGIC_DEAD
ac530 20 30 78 62 36 30 36 63 33 63 38 20 20 20 20 2f 0xb606c3c8 /
ac540 2a 20 54 68 65 20 56 44 42 45 20 68 61 73 20 62 * The VDBE has b
ac550 65 65 6e 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 een deallocated
ac560 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69 */../*.** Functi
ac570 6f 6e 20 70 72 6f 74 6f 74 79 70 65 73 0a 2a 2f on prototypes.*/
ac580 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
ac590 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
ac5a0 46 72 65 65 43 75 72 73 6f 72 28 56 64 62 65 20 FreeCursor(Vdbe
ac5b0 2a 2c 20 56 64 62 65 43 75 72 73 6f 72 2a 29 3b *, VdbeCursor*);
ac5c0 0a 76 6f 69 64 20 73 71 6c 69 74 65 56 64 62 65 .void sqliteVdbe
ac5d0 50 6f 70 53 74 61 63 6b 28 56 64 62 65 2a 2c 69 PopStack(Vdbe*,i
ac5e0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
ac5f0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
ac600 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 dbeCursorMoveto(
ac610 56 64 62 65 43 75 72 73 6f 72 2a 29 3b 0a 23 69 VdbeCursor*);.#i
ac620 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
ac630 5f 44 45 42 55 47 29 20 7c 7c 20 64 65 66 69 6e _DEBUG) || defin
ac640 65 64 28 56 44 42 45 5f 50 52 4f 46 49 4c 45 29 ed(VDBE_PROFILE)
ac650 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
ac660 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
ac670 50 72 69 6e 74 4f 70 28 46 49 4c 45 2a 2c 20 69 PrintOp(FILE*, i
ac680 6e 74 2c 20 4f 70 2a 29 3b 0a 23 65 6e 64 69 66 nt, Op*);.#endif
ac690 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
ac6a0 75 33 32 20 73 71 6c 69 74 65 33 56 64 62 65 53 u32 sqlite3VdbeS
ac6b0 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 75 33 32 erialTypeLen(u32
ac6c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
ac6d0 45 20 75 33 32 20 73 71 6c 69 74 65 33 56 64 62 E u32 sqlite3Vdb
ac6e0 65 53 65 72 69 61 6c 54 79 70 65 28 4d 65 6d 2a eSerialType(Mem*
ac6f0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
ac700 52 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 RIVATE u32 sqlit
ac710 65 33 56 64 62 65 53 65 72 69 61 6c 50 75 74 28 e3VdbeSerialPut(
ac720 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 2c 20 unsigned char*,
ac730 69 6e 74 2c 20 4d 65 6d 2a 2c 20 69 6e 74 29 3b int, Mem*, int);
ac740 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
ac750 75 33 32 20 73 71 6c 69 74 65 33 56 64 62 65 53 u32 sqlite3VdbeS
ac760 65 72 69 61 6c 47 65 74 28 63 6f 6e 73 74 20 75 erialGet(const u
ac770 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 2c 20 75 nsigned char*, u
ac780 33 32 2c 20 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 32, Mem*);.SQLIT
ac790 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
ac7a0 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 qlite3VdbeDelete
ac7b0 41 75 78 44 61 74 61 28 56 64 62 65 46 75 6e 63 AuxData(VdbeFunc
ac7c0 2a 2c 20 69 6e 74 29 3b 0a 0a 69 6e 74 20 73 71 *, int);..int sq
ac7d0 6c 69 74 65 32 42 74 72 65 65 4b 65 79 43 6f 6d lite2BtreeKeyCom
ac7e0 70 61 72 65 28 42 74 43 75 72 73 6f 72 20 2a 2c pare(BtCursor *,
ac7f0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2c 20 69 const void *, i
ac800 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 20 2a 29 3b nt, int, int *);
ac810 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
ac820 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 49 int sqlite3VdbeI
ac830 64 78 4b 65 79 43 6f 6d 70 61 72 65 28 56 64 62 dxKeyCompare(Vdb
ac840 65 43 75 72 73 6f 72 2a 2c 55 6e 70 61 63 6b 65 eCursor*,Unpacke
ac850 64 52 65 63 6f 72 64 2a 2c 69 6e 74 2a 29 3b 0a dRecord*,int*);.
ac860 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
ac870 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 nt sqlite3VdbeId
ac880 78 52 6f 77 69 64 28 73 71 6c 69 74 65 33 2a 2c xRowid(sqlite3*,
ac890 20 42 74 43 75 72 73 6f 72 20 2a 2c 20 69 36 34 BtCursor *, i64
ac8a0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
ac8b0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d ATE int sqlite3M
ac8c0 65 6d 43 6f 6d 70 61 72 65 28 63 6f 6e 73 74 20 emCompare(const
ac8d0 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a Mem*, const Mem*
ac8e0 2c 20 63 6f 6e 73 74 20 43 6f 6c 6c 53 65 71 2a , const CollSeq*
ac8f0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
ac900 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
ac910 65 45 78 65 63 28 56 64 62 65 2a 29 3b 0a 53 51 eExec(Vdbe*);.SQ
ac920 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
ac930 20 73 71 6c 69 74 65 33 56 64 62 65 4c 69 73 74 sqlite3VdbeList
ac940 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f (Vdbe*);.SQLITE_
ac950 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
ac960 74 65 33 56 64 62 65 48 61 6c 74 28 56 64 62 65 te3VdbeHalt(Vdbe
ac970 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
ac980 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
ac990 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 beChangeEncoding
ac9a0 28 4d 65 6d 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 (Mem *, int);.SQ
ac9b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
ac9c0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 sqlite3VdbeMemT
ac9d0 6f 6f 42 69 67 28 4d 65 6d 2a 29 3b 0a 53 51 4c ooBig(Mem*);.SQL
ac9e0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
ac9f0 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 6f sqlite3VdbeMemCo
aca00 70 79 28 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 4d py(Mem*, const M
aca10 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 em*);.SQLITE_PRI
aca20 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
aca30 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 3VdbeMemShallowC
aca40 6f 70 79 28 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 opy(Mem*, const
aca50 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 Mem*, int);.SQLI
aca60 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
aca70 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f sqlite3VdbeMemMo
aca80 76 65 28 4d 65 6d 2a 2c 20 4d 65 6d 2a 29 3b 0a ve(Mem*, Mem*);.
aca90 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
acaa0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
acab0 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 4d 65 mNulTerminate(Me
acac0 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 m*);.SQLITE_PRIV
acad0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
acae0 64 62 65 4d 65 6d 53 65 74 53 74 72 28 4d 65 6d dbeMemSetStr(Mem
acaf0 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 *, const char*,
acb00 69 6e 74 2c 20 75 38 2c 20 76 6f 69 64 28 2a 29 int, u8, void(*)
acb10 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 (void*));.SQLITE
acb20 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
acb30 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 lite3VdbeMemSetI
acb40 6e 74 36 34 28 4d 65 6d 2a 2c 20 69 36 34 29 3b nt64(Mem*, i64);
acb50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
acb60 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
acb70 4d 65 6d 53 65 74 44 6f 75 62 6c 65 28 4d 65 6d MemSetDouble(Mem
acb80 2a 2c 20 64 6f 75 62 6c 65 29 3b 0a 53 51 4c 49 *, double);.SQLI
acb90 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
acba0 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
acbb0 74 4e 75 6c 6c 28 4d 65 6d 2a 29 3b 0a 53 51 4c tNull(Mem*);.SQL
acbc0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
acbd0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
acbe0 65 74 5a 65 72 6f 42 6c 6f 62 28 4d 65 6d 2a 2c etZeroBlob(Mem*,
acbf0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
acc00 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
acc10 33 56 64 62 65 4d 65 6d 53 65 74 52 6f 77 53 65 3VdbeMemSetRowSe
acc20 74 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f t(Mem*);.SQLITE_
acc30 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
acc40 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 te3VdbeMemMakeWr
acc50 69 74 65 61 62 6c 65 28 4d 65 6d 2a 29 3b 0a 53 iteable(Mem*);.S
acc60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
acc70 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d t sqlite3VdbeMem
acc80 53 74 72 69 6e 67 69 66 79 28 4d 65 6d 2a 2c 20 Stringify(Mem*,
acc90 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
acca0 56 41 54 45 20 69 36 34 20 73 71 6c 69 74 65 33 VATE i64 sqlite3
accb0 56 64 62 65 49 6e 74 56 61 6c 75 65 28 4d 65 6d VdbeIntValue(Mem
accc0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
accd0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
acce0 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 beMemIntegerify(
accf0 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Mem*);.SQLITE_PR
acd00 49 56 41 54 45 20 64 6f 75 62 6c 65 20 73 71 6c IVATE double sql
acd10 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 ite3VdbeRealValu
acd20 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f e(Mem*);.SQLITE_
acd30 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
acd40 69 74 65 33 56 64 62 65 49 6e 74 65 67 65 72 41 ite3VdbeIntegerA
acd50 66 66 69 6e 69 74 79 28 4d 65 6d 2a 29 3b 0a 53 ffinity(Mem*);.S
acd60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
acd70 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d t sqlite3VdbeMem
acd80 52 65 61 6c 69 66 79 28 4d 65 6d 2a 29 3b 0a 53 Realify(Mem*);.S
acd90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
acda0 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d t sqlite3VdbeMem
acdb0 4e 75 6d 65 72 69 66 79 28 4d 65 6d 2a 29 3b 0a Numerify(Mem*);.
acdc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
acdd0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
acde0 6d 46 72 6f 6d 42 74 72 65 65 28 42 74 43 75 72 mFromBtree(BtCur
acdf0 73 6f 72 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 sor*,int,int,int
ace00 2c 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ,Mem*);.SQLITE_P
ace10 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
ace20 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
ace30 65 28 4d 65 6d 20 2a 70 29 3b 0a 53 51 4c 49 54 e(Mem *p);.SQLIT
ace40 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
ace50 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
ace60 65 61 73 65 45 78 74 65 72 6e 61 6c 28 4d 65 6d easeExternal(Mem
ace70 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 *p);.SQLITE_PRI
ace80 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
ace90 56 64 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 VdbeMemFinalize(
acea0 4d 65 6d 2a 2c 20 46 75 6e 63 44 65 66 2a 29 3b Mem*, FuncDef*);
aceb0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
acec0 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 const char *sqli
aced0 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28 69 6e te3OpcodeName(in
acee0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
acef0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
acf00 62 65 4f 70 63 6f 64 65 48 61 73 50 72 6f 70 65 beOpcodeHasPrope
acf10 72 74 79 28 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 rty(int, int);.S
acf20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
acf30 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d t sqlite3VdbeMem
acf40 47 72 6f 77 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 Grow(Mem *pMem,
acf50 69 6e 74 20 6e 2c 20 69 6e 74 20 70 72 65 73 65 int n, int prese
acf60 72 76 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 rve);.SQLITE_PRI
acf70 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
acf80 56 64 62 65 43 6c 6f 73 65 53 74 61 74 65 6d 65 VdbeCloseStateme
acf90 6e 74 28 56 64 62 65 20 2a 2c 20 69 6e 74 29 3b nt(Vdbe *, int);
acfa0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
acfb0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
acfc0 46 72 61 6d 65 44 65 6c 65 74 65 28 56 64 62 65 FrameDelete(Vdbe
acfd0 46 72 61 6d 65 2a 29 3b 0a 53 51 4c 49 54 45 5f Frame*);.SQLITE_
acfe0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
acff0 74 65 33 56 64 62 65 46 72 61 6d 65 52 65 73 74 te3VdbeFrameRest
ad000 6f 72 65 28 56 64 62 65 46 72 61 6d 65 20 2a 29 ore(VdbeFrame *)
ad010 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
ad020 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
ad030 65 4d 65 6d 53 74 6f 72 65 54 79 70 65 28 4d 65 eMemStoreType(Me
ad040 6d 20 2a 70 4d 65 6d 29 3b 0a 0a 23 69 66 6e 64 m *pMem);..#ifnd
ad050 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 ef SQLITE_OMIT_F
ad060 4f 52 45 49 47 4e 5f 4b 45 59 0a 53 51 4c 49 54 OREIGN_KEY.SQLIT
ad070 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
ad080 6c 69 74 65 33 56 64 62 65 43 68 65 63 6b 46 6b lite3VdbeCheckFk
ad090 28 56 64 62 65 20 2a 2c 20 69 6e 74 29 3b 0a 23 (Vdbe *, int);.#
ad0a0 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71 else.# define sq
ad0b0 6c 69 74 65 33 56 64 62 65 43 68 65 63 6b 46 6b lite3VdbeCheckFk
ad0c0 28 70 2c 69 29 20 30 0a 23 65 6e 64 69 66 0a 0a (p,i) 0.#endif..
ad0d0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
ad0e0 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 MIT_SHARED_CACHE
ad0f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
ad100 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
ad110 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65 72 28 MutexArrayEnter(
ad120 56 64 62 65 20 2a 70 29 3b 0a 23 65 6c 73 65 0a Vdbe *p);.#else.
ad130 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
ad140 56 64 62 65 4d 75 74 65 78 41 72 72 61 79 45 6e VdbeMutexArrayEn
ad150 74 65 72 28 70 29 0a 23 65 6e 64 69 66 0a 0a 53 ter(p).#endif..S
ad160 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
ad170 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d t sqlite3VdbeMem
ad180 54 72 61 6e 73 6c 61 74 65 28 4d 65 6d 2a 2c 20 Translate(Mem*,
ad190 75 38 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 u8);.#ifdef SQLI
ad1a0 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f TE_DEBUG.SQLITE_
ad1b0 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 PRIVATE void s
ad1c0 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 53 qlite3VdbePrintS
ad1d0 71 6c 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 ql(Vdbe*);.SQLIT
ad1e0 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 E_PRIVATE void
ad1f0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 50 sqlite3VdbeMemP
ad200 72 65 74 74 79 50 72 69 6e 74 28 4d 65 6d 20 2a rettyPrint(Mem *
ad210 70 4d 65 6d 2c 20 63 68 61 72 20 2a 7a 42 75 66 pMem, char *zBuf
ad220 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 );.#endif.SQLITE
ad230 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
ad240 69 74 65 33 56 64 62 65 4d 65 6d 48 61 6e 64 6c ite3VdbeMemHandl
ad250 65 42 6f 6d 28 4d 65 6d 20 2a 70 4d 65 6d 29 3b eBom(Mem *pMem);
ad260 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
ad270 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 53 _OMIT_INCRBLOB.S
ad280 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
ad290 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d int sqlite3VdbeM
ad2a0 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 4d 65 6d emExpandBlob(Mem
ad2b0 20 2a 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 *);.#else. #de
ad2c0 66 69 6e 65 20 73 71 6c 69 74 65 33 56 64 62 65 fine sqlite3Vdbe
ad2d0 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 78 29 MemExpandBlob(x)
ad2e0 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 65 6e 64 69 SQLITE_OK.#endi
ad2f0 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 f..#endif /* !de
ad300 66 69 6e 65 64 28 5f 56 44 42 45 49 4e 54 5f 48 fined(_VDBEINT_H
ad310 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a _) */../********
ad320 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 ****** End of vd
ad330 62 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a beInt.h ********
ad340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ad350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ad360 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
ad370 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e ****** Continuin
ad380 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 g where we left
ad390 6f 66 66 20 69 6e 20 75 74 66 2e 63 20 2a 2a 2a off in utf.c ***
ad3a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ad3b0 2a 2a 2a 2a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 *****/..#ifndef
ad3c0 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 SQLITE_AMALGAMAT
ad3d0 49 4f 4e 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f ION./*.** The fo
ad3e0 6c 6c 6f 77 69 6e 67 20 63 6f 6e 73 74 61 6e 74 llowing constant
ad3f0 20 76 61 6c 75 65 20 69 73 20 75 73 65 64 20 62 value is used b
ad400 79 20 74 68 65 20 53 51 4c 49 54 45 5f 42 49 47 y the SQLITE_BIG
ad410 45 4e 44 49 41 4e 20 61 6e 64 0a 2a 2a 20 53 51 ENDIAN and.** SQ
ad420 4c 49 54 45 5f 4c 49 54 54 4c 45 45 4e 44 49 41 LITE_LITTLEENDIA
ad430 4e 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 53 51 4c N macros..*/.SQL
ad440 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 ITE_PRIVATE cons
ad450 74 20 69 6e 74 20 73 71 6c 69 74 65 33 6f 6e 65 t int sqlite3one
ad460 20 3d 20 31 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 = 1;.#endif /*
ad470 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 SQLITE_AMALGAMAT
ad480 49 4f 4e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 ION */../*.** Th
ad490 69 73 20 6c 6f 6f 6b 75 70 20 74 61 62 6c 65 20 is lookup table
ad4a0 69 73 20 75 73 65 64 20 74 6f 20 68 65 6c 70 20 is used to help
ad4b0 64 65 63 6f 64 65 20 74 68 65 20 66 69 72 73 74 decode the first
ad4c0 20 62 79 74 65 20 6f 66 0a 2a 2a 20 61 20 6d 75 byte of.** a mu
ad4d0 6c 74 69 2d 62 79 74 65 20 55 54 46 38 20 63 68 lti-byte UTF8 ch
ad4e0 61 72 61 63 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 aracter..*/.stat
ad4f0 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 ic const unsigne
ad500 64 20 63 68 61 72 20 73 71 6c 69 74 65 33 55 74 d char sqlite3Ut
ad510 66 38 54 72 61 6e 73 31 5b 5d 20 3d 20 7b 0a 20 f8Trans1[] = {.
ad520 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 0x00, 0x01, 0x0
ad530 32 2c 20 30 78 30 33 2c 20 30 78 30 34 2c 20 30 2, 0x03, 0x04, 0
ad540 78 30 35 2c 20 30 78 30 36 2c 20 30 78 30 37 2c x05, 0x06, 0x07,
ad550 0a 20 20 30 78 30 38 2c 20 30 78 30 39 2c 20 30 . 0x08, 0x09, 0
ad560 78 30 61 2c 20 30 78 30 62 2c 20 30 78 30 63 2c x0a, 0x0b, 0x0c,
ad570 20 30 78 30 64 2c 20 30 78 30 65 2c 20 30 78 30 0x0d, 0x0e, 0x0
ad580 66 2c 0a 20 20 30 78 31 30 2c 20 30 78 31 31 2c f,. 0x10, 0x11,
ad590 20 30 78 31 32 2c 20 30 78 31 33 2c 20 30 78 31 0x12, 0x13, 0x1
ad5a0 34 2c 20 30 78 31 35 2c 20 30 78 31 36 2c 20 30 4, 0x15, 0x16, 0
ad5b0 78 31 37 2c 0a 20 20 30 78 31 38 2c 20 30 78 31 x17,. 0x18, 0x1
ad5c0 39 2c 20 30 78 31 61 2c 20 30 78 31 62 2c 20 30 9, 0x1a, 0x1b, 0
ad5d0 78 31 63 2c 20 30 78 31 64 2c 20 30 78 31 65 2c x1c, 0x1d, 0x1e,
ad5e0 20 30 78 31 66 2c 0a 20 20 30 78 30 30 2c 20 30 0x1f,. 0x00, 0
ad5f0 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33 2c x01, 0x02, 0x03,
ad600 20 30 78 30 34 2c 20 30 78 30 35 2c 20 30 78 30 0x04, 0x05, 0x0
ad610 36 2c 20 30 78 30 37 2c 0a 20 20 30 78 30 38 2c 6, 0x07,. 0x08,
ad620 20 30 78 30 39 2c 20 30 78 30 61 2c 20 30 78 30 0x09, 0x0a, 0x0
ad630 62 2c 20 30 78 30 63 2c 20 30 78 30 64 2c 20 30 b, 0x0c, 0x0d, 0
ad640 78 30 65 2c 20 30 78 30 66 2c 0a 20 20 30 78 30 x0e, 0x0f,. 0x0
ad650 30 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30 0, 0x01, 0x02, 0
ad660 78 30 33 2c 20 30 78 30 34 2c 20 30 78 30 35 2c x03, 0x04, 0x05,
ad670 20 30 78 30 36 2c 20 30 78 30 37 2c 0a 20 20 30 0x06, 0x07,. 0
ad680 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 32 2c x00, 0x01, 0x02,
ad690 20 30 78 30 33 2c 20 30 78 30 30 2c 20 30 78 30 0x03, 0x00, 0x0
ad6a0 31 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 0a 7d 1, 0x00, 0x00,.}
ad6b0 3b 0a 0a 0a 23 64 65 66 69 6e 65 20 57 52 49 54 ;...#define WRIT
ad6c0 45 5f 55 54 46 38 28 7a 4f 75 74 2c 20 63 29 20 E_UTF8(zOut, c)
ad6d0 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
ad6e0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 \. i
ad6f0 66 28 20 63 3c 30 78 30 30 30 38 30 20 29 7b 20 f( c<0x00080 ){
ad700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad720 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b \. *zOut+
ad730 2b 20 3d 20 28 75 38 29 28 63 26 30 78 46 46 29 + = (u8)(c&0xFF)
ad740 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
ad750 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
ad760 20 7d 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 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad790 20 20 20 20 20 20 5c 0a 20 20 65 6c 73 65 20 69 \. else i
ad7a0 66 28 20 63 3c 30 78 30 30 38 30 30 20 29 7b 20 f( c<0x00800 ){
ad7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad7c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
ad7d0 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 . *zOut++ = 0
ad7e0 78 43 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 36 xC0 + (u8)((c>>6
ad7f0 29 26 30 78 31 46 29 3b 20 20 20 20 20 20 20 20 )&0x1F);
ad800 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a \. *z
ad810 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 Out++ = 0x80 + (
ad820 75 38 29 28 63 20 26 20 30 78 33 46 29 3b 20 20 u8)(c & 0x3F);
ad830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad840 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20 \. }
ad850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad870 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 65 6c \. el
ad880 73 65 20 69 66 28 20 63 3c 30 78 31 30 30 30 30 se if( c<0x10000
ad890 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 ){
ad8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad8b0 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b \. *zOut++
ad8c0 20 3d 20 30 78 45 30 20 2b 20 28 75 38 29 28 28 = 0xE0 + (u8)((
ad8d0 63 3e 3e 31 32 29 26 30 78 30 46 29 3b 20 20 20 c>>12)&0x0F);
ad8e0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
ad8f0 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 *zOut++ = 0x80
ad900 20 2b 20 28 75 38 29 28 28 63 3e 3e 36 29 20 26 + (u8)((c>>6) &
ad910 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 20 20 0x3F);
ad920 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 \. *zOut
ad930 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38 29 ++ = 0x80 + (u8)
ad940 28 63 20 26 20 30 78 33 46 29 3b 20 20 20 20 20 (c & 0x3F);
ad950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
ad960 20 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 20 20 }else{
ad970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad990 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f \. *zO
ad9a0 75 74 2b 2b 20 3d 20 30 78 46 30 20 2b 20 28 75 ut++ = 0xF0 + (u
ad9b0 38 29 28 28 63 3e 3e 31 38 29 20 26 20 30 78 30 8)((c>>18) & 0x0
ad9c0 37 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 7);
ad9d0 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 \. *zOut++ =
ad9e0 30 78 38 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 0x80 + (u8)((c>>
ad9f0 31 32 29 20 26 20 30 78 33 46 29 3b 20 20 20 20 12) & 0x3F);
ada00 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a \. *
ada10 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 zOut++ = 0x80 +
ada20 28 75 38 29 28 28 63 3e 3e 36 29 20 26 20 30 78 (u8)((c>>6) & 0x
ada30 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 3F);
ada40 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 \. *zOut++
ada50 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28 63 20 = 0x80 + (u8)(c
ada60 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 20 & 0x3F);
ada70 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d \. }
ada80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ada90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adaa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adab0 20 20 20 20 5c 0a 7d 0a 0a 23 64 65 66 69 6e 65 \.}..#define
adac0 20 57 52 49 54 45 5f 55 54 46 31 36 4c 45 28 7a WRITE_UTF16LE(z
adad0 4f 75 74 2c 20 63 29 20 7b 20 20 20 20 20 20 20 Out, c) {
adae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adaf0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
adb00 20 69 66 28 20 63 3c 3d 30 78 46 46 46 46 20 29 if( c<=0xFFFF )
adb10 7b 20 20 20 20 20 20 20 20 20 20 20 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 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b \. *zOut++
adb50 20 3d 20 28 75 38 29 28 63 26 30 78 30 30 46 46 = (u8)(c&0x00FF
adb60 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 );
adb70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adb80 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a \. *
adb90 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 28 63 zOut++ = (u8)((c
adba0 3e 3e 38 29 26 30 78 30 30 46 46 29 3b 20 20 20 >>8)&0x00FF);
adbb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adbc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
adbd0 0a 20 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 20 . }else{
adbe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adbf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adc00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adc10 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 \. *zOut
adc20 2b 2b 20 3d 20 28 75 38 29 28 28 28 63 3e 3e 31 ++ = (u8)(((c>>1
adc30 30 29 26 30 78 30 30 33 46 29 20 2b 20 28 28 28 0)&0x003F) + (((
adc40 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 30 29 26 c-0x10000)>>10)&
adc50 30 78 30 30 43 30 29 29 3b 20 20 5c 0a 20 20 20 0x00C0)); \.
adc60 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 *zOut++ = (u8)(
adc70 30 78 30 30 44 38 20 2b 20 28 28 28 63 2d 30 78 0x00D8 + (((c-0x
adc80 31 30 30 30 30 29 3e 3e 31 38 29 26 30 78 30 33 10000)>>18)&0x03
adc90 29 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ));
adca0 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d \. *zOut++ =
adcb0 20 28 75 38 29 28 63 26 30 78 30 30 46 46 29 3b (u8)(c&0x00FF);
adcc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adcd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adce0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f \. *zO
adcf0 75 74 2b 2b 20 3d 20 28 75 38 29 28 30 78 30 30 ut++ = (u8)(0x00
add00 44 43 20 2b 20 28 28 63 3e 3e 38 29 26 30 78 30 DC + ((c>>8)&0x0
add10 33 29 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 3));
add20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
add30 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 }
add40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
add50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
add60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
add70 20 20 20 5c 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 \.}..#define
add80 57 52 49 54 45 5f 55 54 46 31 36 42 45 28 7a 4f WRITE_UTF16BE(zO
add90 75 74 2c 20 63 29 20 7b 20 20 20 20 20 20 20 20 ut, c) {
adda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
addb0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
addc0 69 66 28 20 63 3c 3d 30 78 46 46 46 46 20 29 7b if( c<=0xFFFF ){
addd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adde0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
addf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ade00 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 \. *zOut++
ade10 3d 20 28 75 38 29 28 28 63 3e 3e 38 29 26 30 78 = (u8)((c>>8)&0x
ade20 30 30 46 46 29 3b 20 20 20 20 20 20 20 20 20 20 00FF);
ade30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ade40 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a \. *z
ade50 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 63 26 30 Out++ = (u8)(c&0
ade60 78 30 30 46 46 29 3b 20 20 20 20 20 20 20 20 20 x00FF);
ade70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ade80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
ade90 20 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 20 20 }else{
adea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adeb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aded0 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b \. *zOut+
adee0 2b 20 3d 20 28 75 38 29 28 30 78 30 30 44 38 20 + = (u8)(0x00D8
adef0 2b 20 28 28 28 63 2d 30 78 31 30 30 30 30 29 3e + (((c-0x10000)>
adf00 3e 31 38 29 26 30 78 30 33 29 29 3b 20 20 20 20 >18)&0x03));
adf10 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
adf20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 28 *zOut++ = (u8)((
adf30 28 63 3e 3e 31 30 29 26 30 78 30 30 33 46 29 20 (c>>10)&0x003F)
adf40 2b 20 28 28 28 63 2d 30 78 31 30 30 30 30 29 3e + (((c-0x10000)>
adf50 3e 31 30 29 26 30 78 30 30 43 30 29 29 3b 20 20 >10)&0x00C0));
adf60 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 \. *zOut++ =
adf70 28 75 38 29 28 30 78 30 30 44 43 20 2b 20 28 28 (u8)(0x00DC + ((
adf80 63 3e 3e 38 29 26 30 78 30 33 29 29 3b 20 20 20 c>>8)&0x03));
adf90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adfa0 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 \. *zOu
adfb0 74 2b 2b 20 3d 20 28 75 38 29 28 63 26 30 78 30 t++ = (u8)(c&0x0
adfc0 30 46 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 0FF);
adfd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adfe0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
adff0 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 }
ae000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae030 20 20 5c 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 52 \.}..#define R
ae040 45 41 44 5f 55 54 46 31 36 4c 45 28 7a 49 6e 2c EAD_UTF16LE(zIn,
ae050 20 54 45 52 4d 2c 20 63 29 7b 20 20 20 20 20 20 TERM, c){
ae060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae070 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
ae080 20 63 20 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 c = (*zIn++);
ae090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae0c0 20 20 20 20 20 5c 0a 20 20 63 20 2b 3d 20 28 28 \. c += ((
ae0d0 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 20 *zIn++)<<8);
ae0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae0f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae100 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
ae110 20 69 66 28 20 63 3e 3d 30 78 44 38 30 30 20 26 if( c>=0xD800 &
ae120 26 20 63 3c 30 78 45 30 30 30 20 26 26 20 54 45 & c<0xE000 && TE
ae130 52 4d 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 RM ){
ae140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae150 20 20 20 20 20 5c 0a 20 20 20 20 69 6e 74 20 63 \. int c
ae160 32 20 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 2 = (*zIn++);
ae170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae190 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
ae1a0 20 20 20 63 32 20 2b 3d 20 28 28 2a 7a 49 6e 2b c2 += ((*zIn+
ae1b0 2b 29 3c 3c 38 29 3b 20 20 20 20 20 20 20 20 20 +)<<8);
ae1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae1e0 20 20 20 20 20 5c 0a 20 20 20 20 63 20 3d 20 28 \. c = (
ae1f0 63 32 26 30 78 30 33 46 46 29 20 2b 20 28 28 63 c2&0x03FF) + ((c
ae200 26 30 78 30 30 33 46 29 3c 3c 31 30 29 20 2b 20 &0x003F)<<10) +
ae210 28 28 28 63 26 30 78 30 33 43 30 29 2b 30 78 30 (((c&0x03C0)+0x0
ae220 30 34 30 29 3c 3c 31 30 29 3b 20 20 20 5c 0a 20 040)<<10); \.
ae230 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 }
ae240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae270 20 20 20 20 20 5c 0a 7d 0a 0a 23 64 65 66 69 6e \.}..#defin
ae280 65 20 52 45 41 44 5f 55 54 46 31 36 42 45 28 7a e READ_UTF16BE(z
ae290 49 6e 2c 20 54 45 52 4d 2c 20 63 29 7b 20 20 20 In, TERM, c){
ae2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae2b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae2c0 5c 0a 20 20 63 20 3d 20 28 28 2a 7a 49 6e 2b 2b \. c = ((*zIn++
ae2d0 29 3c 3c 38 29 3b 20 20 20 20 20 20 20 20 20 20 )<<8);
ae2e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae2f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae300 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20 2b 3d \. c +=
ae310 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 (*zIn++);
ae320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae350 5c 0a 20 20 69 66 28 20 63 3e 3d 30 78 44 38 30 \. if( c>=0xD80
ae360 30 20 26 26 20 63 3c 30 78 45 30 30 30 20 26 26 0 && c<0xE000 &&
ae370 20 54 45 52 4d 20 29 7b 20 20 20 20 20 20 20 20 TERM ){
ae380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae390 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69 6e \. in
ae3a0 74 20 63 32 20 3d 20 28 28 2a 7a 49 6e 2b 2b 29 t c2 = ((*zIn++)
ae3b0 3c 3c 38 29 3b 20 20 20 20 20 20 20 20 20 20 20 <<8);
ae3c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae3e0 5c 0a 20 20 20 20 63 32 20 2b 3d 20 28 2a 7a 49 \. c2 += (*zI
ae3f0 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20 n++);
ae400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae420 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 63 20 \. c
ae430 3d 20 28 63 32 26 30 78 30 33 46 46 29 20 2b 20 = (c2&0x03FF) +
ae440 28 28 63 26 30 78 30 30 33 46 29 3c 3c 31 30 29 ((c&0x003F)<<10)
ae450 20 2b 20 28 28 28 63 26 30 78 30 33 43 30 29 2b + (((c&0x03C0)+
ae460 30 78 30 30 34 30 29 3c 3c 31 30 29 3b 20 20 20 0x0040)<<10);
ae470 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20 20 \. }
ae480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae4b0 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 2f 2a 0a \.}../*.
ae4c0 2a 2a 20 54 72 61 6e 73 6c 61 74 65 20 61 20 73 ** Translate a s
ae4d0 69 6e 67 6c 65 20 55 54 46 2d 38 20 63 68 61 72 ingle UTF-8 char
ae4e0 61 63 74 65 72 2e 20 20 52 65 74 75 72 6e 20 74 acter. Return t
ae4f0 68 65 20 75 6e 69 63 6f 64 65 20 76 61 6c 75 65 he unicode value
ae500 2e 0a 2a 2a 0a 2a 2a 20 44 75 72 69 6e 67 20 74 ..**.** During t
ae510 72 61 6e 73 6c 61 74 69 6f 6e 2c 20 61 73 73 75 ranslation, assu
ae520 6d 65 20 74 68 61 74 20 74 68 65 20 62 79 74 65 me that the byte
ae530 20 74 68 61 74 20 7a 54 65 72 6d 20 70 6f 69 6e that zTerm poin
ae540 74 73 0a 2a 2a 20 69 73 20 61 20 30 78 30 30 2e ts.** is a 0x00.
ae550 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 70 .**.** Write a p
ae560 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 ointer to the ne
ae570 78 74 20 75 6e 72 65 61 64 20 62 79 74 65 20 62 xt unread byte b
ae580 61 63 6b 20 69 6e 74 6f 20 2a 70 7a 4e 65 78 74 ack into *pzNext
ae590 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 73 20 4f 6e ..**.** Notes On
ae5a0 20 49 6e 76 61 6c 69 64 20 55 54 46 2d 38 3a 0a Invalid UTF-8:.
ae5b0 2a 2a 0a 2a 2a 20 20 2a 20 20 54 68 69 73 20 72 **.** * This r
ae5c0 6f 75 74 69 6e 65 20 6e 65 76 65 72 20 61 6c 6c outine never all
ae5d0 6f 77 73 20 61 20 37 2d 62 69 74 20 63 68 61 72 ows a 7-bit char
ae5e0 61 63 74 65 72 20 28 30 78 30 30 20 74 68 72 6f acter (0x00 thro
ae5f0 75 67 68 20 30 78 37 66 29 20 74 6f 0a 2a 2a 20 ugh 0x7f) to.**
ae600 20 20 20 20 62 65 20 65 6e 63 6f 64 65 64 20 61 be encoded a
ae610 73 20 61 20 6d 75 6c 74 69 2d 62 79 74 65 20 63 s a multi-byte c
ae620 68 61 72 61 63 74 65 72 2e 20 20 41 6e 79 20 6d haracter. Any m
ae630 75 6c 74 69 2d 62 79 74 65 20 63 68 61 72 61 63 ulti-byte charac
ae640 74 65 72 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 ter that.**
ae650 61 74 74 65 6d 70 74 73 20 74 6f 20 65 6e 63 6f attempts to enco
ae660 64 65 20 61 20 76 61 6c 75 65 20 62 65 74 77 65 de a value betwe
ae670 65 6e 20 30 78 30 30 20 61 6e 64 20 30 78 37 66 en 0x00 and 0x7f
ae680 20 69 73 20 72 65 6e 64 65 72 65 64 20 61 73 20 is rendered as
ae690 30 78 66 66 66 64 2e 0a 2a 2a 0a 2a 2a 20 20 2a 0xfffd..**.** *
ae6a0 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6e This routine n
ae6b0 65 76 65 72 20 61 6c 6c 6f 77 73 20 61 20 55 54 ever allows a UT
ae6c0 46 31 36 20 73 75 72 72 6f 67 61 74 65 20 76 61 F16 surrogate va
ae6d0 6c 75 65 20 74 6f 20 62 65 20 65 6e 63 6f 64 65 lue to be encode
ae6e0 64 2e 0a 2a 2a 20 20 20 20 20 49 66 20 61 20 6d d..** If a m
ae6f0 75 6c 74 69 2d 62 79 74 65 20 63 68 61 72 61 63 ulti-byte charac
ae700 74 65 72 20 61 74 74 65 6d 70 74 73 20 74 6f 20 ter attempts to
ae710 65 6e 63 6f 64 65 20 61 20 76 61 6c 75 65 20 62 encode a value b
ae720 65 74 77 65 65 6e 0a 2a 2a 20 20 20 20 20 30 78 etween.** 0x
ae730 64 38 30 30 20 61 6e 64 20 30 78 65 30 30 30 20 d800 and 0xe000
ae740 74 68 65 6e 20 69 74 20 69 73 20 72 65 6e 64 65 then it is rende
ae750 72 65 64 20 61 73 20 30 78 66 66 66 64 2e 0a 2a red as 0xfffd..*
ae760 2a 0a 2a 2a 20 20 2a 20 20 42 79 74 65 73 20 69 *.** * Bytes i
ae770 6e 20 74 68 65 20 72 61 6e 67 65 20 6f 66 20 30 n the range of 0
ae780 78 38 30 20 74 68 72 6f 75 67 68 20 30 78 62 66 x80 through 0xbf
ae790 20 77 68 69 63 68 20 6f 63 63 75 72 20 61 73 20 which occur as
ae7a0 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 20 20 20 the first.**
ae7b0 20 62 79 74 65 20 6f 66 20 61 20 63 68 61 72 61 byte of a chara
ae7c0 63 74 65 72 20 61 72 65 20 69 6e 74 65 72 70 72 cter are interpr
ae7d0 65 74 65 64 20 61 73 20 73 69 6e 67 6c 65 2d 62 eted as single-b
ae7e0 79 74 65 20 63 68 61 72 61 63 74 65 72 73 0a 2a yte characters.*
ae7f0 2a 20 20 20 20 20 61 6e 64 20 72 65 6e 64 65 72 * and render
ae800 65 64 20 61 73 20 74 68 65 6d 73 65 6c 76 65 73 ed as themselves
ae810 20 65 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65 even though the
ae820 79 20 61 72 65 20 74 65 63 68 6e 69 63 61 6c 6c y are technicall
ae830 79 0a 2a 2a 20 20 20 20 20 69 6e 76 61 6c 69 64 y.** invalid
ae840 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a characters..**.
ae850 2a 2a 20 20 2a 20 20 54 68 69 73 20 72 6f 75 74 ** * This rout
ae860 69 6e 65 20 61 63 63 65 70 74 73 20 61 6e 20 69 ine accepts an i
ae870 6e 66 69 6e 69 74 65 20 6e 75 6d 62 65 72 20 6f nfinite number o
ae880 66 20 64 69 66 66 65 72 65 6e 74 20 55 54 46 38 f different UTF8
ae890 20 65 6e 63 6f 64 69 6e 67 73 0a 2a 2a 20 20 20 encodings.**
ae8a0 20 20 66 6f 72 20 75 6e 69 63 6f 64 65 20 76 61 for unicode va
ae8b0 6c 75 65 73 20 30 78 38 30 20 61 6e 64 20 67 72 lues 0x80 and gr
ae8c0 65 61 74 65 72 2e 20 20 49 74 20 64 6f 20 6e 6f eater. It do no
ae8d0 74 20 63 68 61 6e 67 65 20 6f 76 65 72 2d 6c 65 t change over-le
ae8e0 6e 67 74 68 0a 2a 2a 20 20 20 20 20 65 6e 63 6f ngth.** enco
ae8f0 64 69 6e 67 73 20 74 6f 20 30 78 66 66 66 64 20 dings to 0xfffd
ae900 61 73 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 20 as some systems
ae910 72 65 63 6f 6d 6d 65 6e 64 2e 0a 2a 2f 0a 23 64 recommend..*/.#d
ae920 65 66 69 6e 65 20 52 45 41 44 5f 55 54 46 38 28 efine READ_UTF8(
ae930 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 63 29 20 20 zIn, zTerm, c)
ae940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae950 20 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20 3d \. c =
ae960 20 2a 28 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 *(zIn++);
ae970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae990 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3e \. if( c>
ae9a0 3d 30 78 63 30 20 29 7b 20 20 20 20 20 20 20 20 =0xc0 ){
ae9b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae9d0 20 20 20 5c 0a 20 20 20 20 63 20 3d 20 73 71 6c \. c = sql
ae9e0 69 74 65 33 55 74 66 38 54 72 61 6e 73 31 5b 63 ite3Utf8Trans1[c
ae9f0 2d 30 78 63 30 5d 3b 20 20 20 20 20 20 20 20 20 -0xc0];
aea00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aea10 5c 0a 20 20 20 20 77 68 69 6c 65 28 20 7a 49 6e \. while( zIn
aea20 21 3d 7a 54 65 72 6d 20 26 26 20 28 2a 7a 49 6e !=zTerm && (*zIn
aea30 20 26 20 30 78 63 30 29 3d 3d 30 78 38 30 20 29 & 0xc0)==0x80 )
aea40 7b 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 { \.
aea50 20 20 20 20 20 63 20 3d 20 28 63 3c 3c 36 29 20 c = (c<<6)
aea60 2b 20 28 30 78 33 66 20 26 20 2a 28 7a 49 6e 2b + (0x3f & *(zIn+
aea70 2b 29 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 +));
aea80 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
aea90 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 }
aeaa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aeab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aeac0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69 66 28 \. if(
aead0 20 63 3c 30 78 38 30 20 20 20 20 20 20 20 20 20 c<0x80
aeae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aeaf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aeb00 20 20 20 20 5c 0a 20 20 20 20 20 20 20 20 7c 7c \. ||
aeb10 20 28 63 26 30 78 46 46 46 46 46 38 30 30 29 3d (c&0xFFFFF800)=
aeb20 3d 30 78 44 38 30 30 20 20 20 20 20 20 20 20 20 =0xD800
aeb30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aeb40 20 5c 0a 20 20 20 20 20 20 20 20 7c 7c 20 28 63 \. || (c
aeb50 26 30 78 46 46 46 46 46 46 46 45 29 3d 3d 30 78 &0xFFFFFFFE)==0x
aeb60 46 46 46 45 20 29 7b 20 20 63 20 3d 20 30 78 46 FFFE ){ c = 0xF
aeb70 46 46 44 3b 20 7d 20 20 20 20 20 20 20 20 5c 0a FFD; } \.
aeb80 20 20 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 }.SQLITE_PRIVA
aeb90 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74 TE int sqlite3Ut
aeba0 66 38 52 65 61 64 28 0a 20 20 63 6f 6e 73 74 20 f8Read(. const
aebb0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a unsigned char *z
aebc0 49 6e 2c 20 20 20 20 20 20 20 2f 2a 20 46 69 72 In, /* Fir
aebd0 73 74 20 62 79 74 65 20 6f 66 20 55 54 46 2d 38 st byte of UTF-8
aebe0 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 20 character */.
aebf0 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
aec00 68 61 72 20 2a 2a 70 7a 4e 65 78 74 20 20 20 20 har **pzNext
aec10 2f 2a 20 57 72 69 74 65 20 66 69 72 73 74 20 62 /* Write first b
aec20 79 74 65 20 70 61 73 74 20 55 54 46 2d 38 20 63 yte past UTF-8 c
aec30 68 61 72 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 har here */.){.
aec40 20 69 6e 74 20 63 3b 0a 0a 20 20 2f 2a 20 53 61 int c;.. /* Sa
aec50 6d 65 20 61 73 20 52 45 41 44 5f 55 54 46 38 28 me as READ_UTF8(
aec60 29 20 61 62 6f 76 65 20 62 75 74 20 77 69 74 68 ) above but with
aec70 6f 75 74 20 74 68 65 20 7a 54 65 72 6d 20 70 61 out the zTerm pa
aec80 72 61 6d 65 74 65 72 2e 0a 20 20 2a 2a 20 46 6f rameter.. ** Fo
aec90 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 r this routine,
aeca0 77 65 20 61 73 73 75 6d 65 20 74 68 65 20 55 54 we assume the UT
aecb0 46 38 20 73 74 72 69 6e 67 20 69 73 20 61 6c 77 F8 string is alw
aecc0 61 79 73 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 ays zero-termina
aecd0 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 63 20 3d 20 ted.. */. c =
aece0 2a 28 7a 49 6e 2b 2b 29 3b 0a 20 20 69 66 28 20 *(zIn++);. if(
aecf0 63 3e 3d 30 78 63 30 20 29 7b 0a 20 20 20 20 63 c>=0xc0 ){. c
aed00 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 54 72 = sqlite3Utf8Tr
aed10 61 6e 73 31 5b 63 2d 30 78 63 30 5d 3b 0a 20 20 ans1[c-0xc0];.
aed20 20 20 77 68 69 6c 65 28 20 28 2a 7a 49 6e 20 26 while( (*zIn &
aed30 20 30 78 63 30 29 3d 3d 30 78 38 30 20 29 7b 0a 0xc0)==0x80 ){.
aed40 20 20 20 20 20 20 63 20 3d 20 28 63 3c 3c 36 29 c = (c<<6)
aed50 20 2b 20 28 30 78 33 66 20 26 20 2a 28 7a 49 6e + (0x3f & *(zIn
aed60 2b 2b 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ++));. }.
aed70 69 66 28 20 63 3c 30 78 38 30 0a 20 20 20 20 20 if( c<0x80.
aed80 20 20 20 7c 7c 20 28 63 26 30 78 46 46 46 46 46 || (c&0xFFFFF
aed90 38 30 30 29 3d 3d 30 78 44 38 30 30 0a 20 20 20 800)==0xD800.
aeda0 20 20 20 20 20 7c 7c 20 28 63 26 30 78 46 46 46 || (c&0xFFF
aedb0 46 46 46 46 45 29 3d 3d 30 78 46 46 46 45 20 29 FFFFE)==0xFFFE )
aedc0 7b 20 20 63 20 3d 20 30 78 46 46 46 44 3b 20 7d { c = 0xFFFD; }
aedd0 0a 20 20 7d 0a 20 20 2a 70 7a 4e 65 78 74 20 3d . }. *pzNext =
aede0 20 7a 49 6e 3b 0a 20 20 72 65 74 75 72 6e 20 63 zIn;. return c
aedf0 3b 0a 7d 0a 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 49 66 ;.}...../*.** If
aee00 20 74 68 65 20 54 52 41 4e 53 4c 41 54 45 5f 54 the TRANSLATE_T
aee10 52 41 43 45 20 6d 61 63 72 6f 20 69 73 20 64 65 RACE macro is de
aee20 66 69 6e 65 64 2c 20 74 68 65 20 76 61 6c 75 65 fined, the value
aee30 20 6f 66 20 65 61 63 68 20 4d 65 6d 20 69 73 0a of each Mem is.
aee40 2a 2a 20 70 72 69 6e 74 65 64 20 6f 6e 20 73 74 ** printed on st
aee50 64 65 72 72 20 6f 6e 20 74 68 65 20 77 61 79 20 derr on the way
aee60 69 6e 74 6f 20 61 6e 64 20 6f 75 74 20 6f 66 20 into and out of
aee70 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 72 sqlite3VdbeMemTr
aee80 61 6e 73 6c 61 74 65 28 29 2e 0a 2a 2f 20 0a 2f anslate()..*/ ./
aee90 2a 20 23 64 65 66 69 6e 65 20 54 52 41 4e 53 4c * #define TRANSL
aeea0 41 54 45 5f 54 52 41 43 45 20 31 20 2a 2f 0a 0a ATE_TRACE 1 */..
aeeb0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
aeec0 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 MIT_UTF16./*.**
aeed0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 74 72 61 This routine tra
aeee0 6e 73 66 6f 72 6d 73 20 74 68 65 20 69 6e 74 65 nsforms the inte
aeef0 72 6e 61 6c 20 74 65 78 74 20 65 6e 63 6f 64 69 rnal text encodi
aef00 6e 67 20 75 73 65 64 20 62 79 20 70 4d 65 6d 20 ng used by pMem
aef10 74 6f 0a 2a 2a 20 64 65 73 69 72 65 64 45 6e 63 to.** desiredEnc
aef20 2e 20 49 74 20 69 73 20 61 6e 20 65 72 72 6f 72 . It is an error
aef30 20 69 66 20 74 68 65 20 73 74 72 69 6e 67 20 69 if the string i
aef40 73 20 61 6c 72 65 61 64 79 20 6f 66 20 74 68 65 s already of the
aef50 20 64 65 73 69 72 65 64 0a 2a 2a 20 65 6e 63 6f desired.** enco
aef60 64 69 6e 67 2c 20 6f 72 20 69 66 20 2a 70 4d 65 ding, or if *pMe
aef70 6d 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 61 m does not conta
aef80 69 6e 20 61 20 73 74 72 69 6e 67 20 76 61 6c 75 in a string valu
aef90 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
aefa0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
aefb0 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74 65 VdbeMemTranslate
aefc0 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 75 38 20 64 (Mem *pMem, u8 d
aefd0 65 73 69 72 65 64 45 6e 63 29 7b 0a 20 20 69 6e esiredEnc){. in
aefe0 74 20 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 20 t len;
aeff0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 /* Max
af000 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 6f imum length of o
af010 75 74 70 75 74 20 73 74 72 69 6e 67 20 69 6e 20 utput string in
af020 62 79 74 65 73 20 2a 2f 0a 20 20 75 6e 73 69 67 bytes */. unsig
af030 6e 65 64 20 63 68 61 72 20 2a 7a 4f 75 74 3b 20 ned char *zOut;
af040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
af050 20 2f 2a 20 4f 75 74 70 75 74 20 62 75 66 66 65 /* Output buffe
af060 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 r */. unsigned
af070 63 68 61 72 20 2a 7a 49 6e 3b 20 20 20 20 20 20 char *zIn;
af080 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
af090 49 6e 70 75 74 20 69 74 65 72 61 74 6f 72 20 2a Input iterator *
af0a0 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 /. unsigned cha
af0b0 72 20 2a 7a 54 65 72 6d 3b 20 20 20 20 20 20 20 r *zTerm;
af0c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 64 /* End
af0d0 20 6f 66 20 69 6e 70 75 74 20 2a 2f 0a 20 20 75 of input */. u
af0e0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b nsigned char *z;
af0f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
af100 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 69 /* Output i
af110 74 65 72 61 74 6f 72 20 2a 2f 0a 20 20 75 6e 73 terator */. uns
af120 69 67 6e 65 64 20 69 6e 74 20 63 3b 0a 0a 20 20 igned int c;..
af130 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 assert( pMem->db
af140 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d ==0 || sqlite3_m
af150 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e utex_held(pMem->
af160 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
af170 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c assert( pMem->fl
af180 61 67 73 26 4d 45 4d 5f 53 74 72 20 29 3b 0a 20 ags&MEM_Str );.
af190 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 65 assert( pMem->e
af1a0 6e 63 21 3d 64 65 73 69 72 65 64 45 6e 63 20 29 nc!=desiredEnc )
af1b0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d ;. assert( pMem
af1c0 2d 3e 65 6e 63 21 3d 30 20 29 3b 0a 20 20 61 73 ->enc!=0 );. as
af1d0 73 65 72 74 28 20 70 4d 65 6d 2d 3e 6e 3e 3d 30 sert( pMem->n>=0
af1e0 20 29 3b 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 );..#if defined
af1f0 28 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 (TRANSLATE_TRACE
af200 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c ) && defined(SQL
af210 49 54 45 5f 44 45 42 55 47 29 0a 20 20 7b 0a 20 ITE_DEBUG). {.
af220 20 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 char zBuf[100
af230 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ];. sqlite3Vd
af240 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74 beMemPrettyPrint
af250 28 70 4d 65 6d 2c 20 7a 42 75 66 29 3b 0a 20 20 (pMem, zBuf);.
af260 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 fprintf(stderr
af270 2c 20 22 49 4e 50 55 54 3a 20 20 25 73 5c 6e 22 , "INPUT: %s\n"
af280 2c 20 7a 42 75 66 29 3b 0a 20 20 7d 0a 23 65 6e , zBuf);. }.#en
af290 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 dif.. /* If the
af2a0 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20 69 73 20 translation is
af2b0 62 65 74 77 65 65 6e 20 55 54 46 2d 31 36 20 6c between UTF-16 l
af2c0 69 74 74 6c 65 20 61 6e 64 20 62 69 67 20 65 6e ittle and big en
af2d0 64 69 61 6e 2c 20 74 68 65 6e 20 0a 20 20 2a 2a dian, then . **
af2e0 20 61 6c 6c 20 74 68 61 74 20 69 73 20 72 65 71 all that is req
af2f0 75 69 72 65 64 20 69 73 20 74 6f 20 73 77 61 70 uired is to swap
af300 20 74 68 65 20 62 79 74 65 20 6f 72 64 65 72 2e the byte order.
af310 20 54 68 69 73 20 63 61 73 65 20 69 73 20 68 61 This case is ha
af320 6e 64 6c 65 64 0a 20 20 2a 2a 20 64 69 66 66 65 ndled. ** diffe
af330 72 65 6e 74 6c 79 20 66 72 6f 6d 20 74 68 65 20 rently from the
af340 6f 74 68 65 72 73 2e 0a 20 20 2a 2f 0a 20 20 69 others.. */. i
af350 66 28 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 53 51 f( pMem->enc!=SQ
af360 4c 49 54 45 5f 55 54 46 38 20 26 26 20 64 65 73 LITE_UTF8 && des
af370 69 72 65 64 45 6e 63 21 3d 53 51 4c 49 54 45 5f iredEnc!=SQLITE_
af380 55 54 46 38 20 29 7b 0a 20 20 20 20 75 38 20 74 UTF8 ){. u8 t
af390 65 6d 70 3b 0a 20 20 20 20 69 6e 74 20 72 63 3b emp;. int rc;
af3a0 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
af3b0 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 3VdbeMemMakeWrit
af3c0 65 61 62 6c 65 28 70 4d 65 6d 29 3b 0a 20 20 20 eable(pMem);.
af3d0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
af3e0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 OK ){. asse
af3f0 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e rt( rc==SQLITE_N
af400 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20 72 65 OMEM );. re
af410 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
af420 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 49 6e M;. }. zIn
af430 20 3d 20 28 75 38 2a 29 70 4d 65 6d 2d 3e 7a 3b = (u8*)pMem->z;
af440 0a 20 20 20 20 7a 54 65 72 6d 20 3d 20 26 7a 49 . zTerm = &zI
af450 6e 5b 70 4d 65 6d 2d 3e 6e 26 7e 31 5d 3b 0a 20 n[pMem->n&~1];.
af460 20 20 20 77 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 while( zIn<zT
af470 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 74 65 6d erm ){. tem
af480 70 20 3d 20 2a 7a 49 6e 3b 0a 20 20 20 20 20 20 p = *zIn;.
af490 2a 7a 49 6e 20 3d 20 2a 28 7a 49 6e 2b 31 29 3b *zIn = *(zIn+1);
af4a0 0a 20 20 20 20 20 20 7a 49 6e 2b 2b 3b 0a 20 20 . zIn++;.
af4b0 20 20 20 20 2a 7a 49 6e 2b 2b 20 3d 20 74 65 6d *zIn++ = tem
af4c0 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 p;. }. pMe
af4d0 6d 2d 3e 65 6e 63 20 3d 20 64 65 73 69 72 65 64 m->enc = desired
af4e0 45 6e 63 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72 Enc;. goto tr
af4f0 61 6e 73 6c 61 74 65 5f 6f 75 74 3b 0a 20 20 7d anslate_out;. }
af500 0a 0a 20 20 2f 2a 20 53 65 74 20 6c 65 6e 20 74 .. /* Set len t
af510 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 o the maximum nu
af520 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 mber of bytes re
af530 71 75 69 72 65 64 20 69 6e 20 74 68 65 20 6f 75 quired in the ou
af540 74 70 75 74 20 62 75 66 66 65 72 2e 20 2a 2f 0a tput buffer. */.
af550 20 20 69 66 28 20 64 65 73 69 72 65 64 45 6e 63 if( desiredEnc
af560 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b ==SQLITE_UTF8 ){
af570 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 63 6f 6e . /* When con
af580 76 65 72 74 69 6e 67 20 66 72 6f 6d 20 55 54 46 verting from UTF
af590 2d 31 36 2c 20 74 68 65 20 6d 61 78 69 6d 75 6d -16, the maximum
af5a0 20 67 72 6f 77 74 68 20 72 65 73 75 6c 74 73 20 growth results
af5b0 66 72 6f 6d 0a 20 20 20 20 2a 2a 20 74 72 61 6e from. ** tran
af5c0 73 6c 61 74 69 6e 67 20 61 20 32 2d 62 79 74 65 slating a 2-byte
af5d0 20 63 68 61 72 61 63 74 65 72 20 74 6f 20 61 20 character to a
af5e0 34 2d 62 79 74 65 20 55 54 46 2d 38 20 63 68 61 4-byte UTF-8 cha
af5f0 72 61 63 74 65 72 2e 0a 20 20 20 20 2a 2a 20 41 racter.. ** A
af600 20 73 69 6e 67 6c 65 20 62 79 74 65 20 69 73 20 single byte is
af610 72 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 65 required for the
af620 20 6f 75 74 70 75 74 20 73 74 72 69 6e 67 0a 20 output string.
af630 20 20 20 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e ** nul-termin
af640 61 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ator.. */.
af650 20 70 4d 65 6d 2d 3e 6e 20 26 3d 20 7e 31 3b 0a pMem->n &= ~1;.
af660 20 20 20 20 6c 65 6e 20 3d 20 70 4d 65 6d 2d 3e len = pMem->
af670 6e 20 2a 20 32 20 2b 20 31 3b 0a 20 20 7d 65 6c n * 2 + 1;. }el
af680 73 65 7b 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 se{. /* When
af690 63 6f 6e 76 65 72 74 69 6e 67 20 66 72 6f 6d 20 converting from
af6a0 55 54 46 2d 38 20 74 6f 20 55 54 46 2d 31 36 20 UTF-8 to UTF-16
af6b0 74 68 65 20 6d 61 78 69 6d 75 6d 20 67 72 6f 77 the maximum grow
af6c0 74 68 20 69 73 20 63 61 75 73 65 64 0a 20 20 20 th is caused.
af6d0 20 2a 2a 20 77 68 65 6e 20 61 20 31 2d 62 79 74 ** when a 1-byt
af6e0 65 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 e UTF-8 characte
af6f0 72 20 69 73 20 74 72 61 6e 73 6c 61 74 65 64 20 r is translated
af700 69 6e 74 6f 20 61 20 32 2d 62 79 74 65 20 55 54 into a 2-byte UT
af710 46 2d 31 36 0a 20 20 20 20 2a 2a 20 63 68 61 72 F-16. ** char
af720 61 63 74 65 72 2e 20 54 77 6f 20 62 79 74 65 73 acter. Two bytes
af730 20 61 72 65 20 72 65 71 75 69 72 65 64 20 69 6e are required in
af740 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 the output buff
af750 65 72 20 66 6f 72 20 74 68 65 0a 20 20 20 20 2a er for the. *
af760 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 * nul-terminator
af770 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6c 65 6e .. */. len
af780 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2a 20 32 20 2b = pMem->n * 2 +
af790 20 32 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 2;. }.. /* Se
af7a0 74 20 7a 49 6e 20 74 6f 20 70 6f 69 6e 74 20 61 t zIn to point a
af7b0 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 t the start of t
af7c0 68 65 20 69 6e 70 75 74 20 62 75 66 66 65 72 20 he input buffer
af7d0 61 6e 64 20 7a 54 65 72 6d 20 74 6f 20 70 6f 69 and zTerm to poi
af7e0 6e 74 20 31 0a 20 20 2a 2a 20 62 79 74 65 20 70 nt 1. ** byte p
af7f0 61 73 74 20 74 68 65 20 65 6e 64 2e 0a 20 20 2a ast the end.. *
af800 2a 0a 20 20 2a 2a 20 56 61 72 69 61 62 6c 65 20 *. ** Variable
af810 7a 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20 70 zOut is set to p
af820 6f 69 6e 74 20 61 74 20 74 68 65 20 6f 75 74 70 oint at the outp
af830 75 74 20 62 75 66 66 65 72 2c 20 73 70 61 63 65 ut buffer, space
af840 20 6f 62 74 61 69 6e 65 64 0a 20 20 2a 2a 20 66 obtained. ** f
af850 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c rom sqlite3_mall
af860 6f 63 28 29 2e 0a 20 20 2a 2f 0a 20 20 7a 49 6e oc().. */. zIn
af870 20 3d 20 28 75 38 2a 29 70 4d 65 6d 2d 3e 7a 3b = (u8*)pMem->z;
af880 0a 20 20 7a 54 65 72 6d 20 3d 20 26 7a 49 6e 5b . zTerm = &zIn[
af890 70 4d 65 6d 2d 3e 6e 5d 3b 0a 20 20 7a 4f 75 74 pMem->n];. zOut
af8a0 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
af8b0 6f 63 52 61 77 28 70 4d 65 6d 2d 3e 64 62 2c 20 ocRaw(pMem->db,
af8c0 6c 65 6e 29 3b 0a 20 20 69 66 28 20 21 7a 4f 75 len);. if( !zOu
af8d0 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 t ){. return
af8e0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
af8f0 7d 0a 20 20 7a 20 3d 20 7a 4f 75 74 3b 0a 0a 20 }. z = zOut;..
af900 20 69 66 28 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d if( pMem->enc==
af910 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 SQLITE_UTF8 ){.
af920 20 20 20 69 66 28 20 64 65 73 69 72 65 64 45 6e if( desiredEn
af930 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c c==SQLITE_UTF16L
af940 45 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 55 54 E ){. /* UT
af950 46 2d 38 20 2d 3e 20 55 54 46 2d 31 36 20 4c 69 F-8 -> UTF-16 Li
af960 74 74 6c 65 2d 65 6e 64 69 61 6e 20 2a 2f 0a 20 ttle-endian */.
af970 20 20 20 20 20 77 68 69 6c 65 28 20 7a 49 6e 3c while( zIn<
af980 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 20 zTerm ){.
af990 20 2f 2a 20 63 20 3d 20 73 71 6c 69 74 65 33 55 /* c = sqlite3U
af9a0 74 66 38 52 65 61 64 28 7a 49 6e 2c 20 7a 54 65 tf8Read(zIn, zTe
af9b0 72 6d 2c 20 28 63 6f 6e 73 74 20 75 38 2a 2a 29 rm, (const u8**)
af9c0 26 7a 49 6e 29 3b 20 2a 2f 0a 20 20 20 20 20 20 &zIn); */.
af9d0 20 20 52 45 41 44 5f 55 54 46 38 28 7a 49 6e 2c READ_UTF8(zIn,
af9e0 20 7a 54 65 72 6d 2c 20 63 29 3b 0a 20 20 20 20 zTerm, c);.
af9f0 20 20 20 20 57 52 49 54 45 5f 55 54 46 31 36 4c WRITE_UTF16L
afa00 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20 7d E(z, c);. }
afa10 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
afa20 20 20 61 73 73 65 72 74 28 20 64 65 73 69 72 65 assert( desire
afa30 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 dEnc==SQLITE_UTF
afa40 31 36 42 45 20 29 3b 0a 20 20 20 20 20 20 2f 2a 16BE );. /*
afa50 20 55 54 46 2d 38 20 2d 3e 20 55 54 46 2d 31 36 UTF-8 -> UTF-16
afa60 20 42 69 67 2d 65 6e 64 69 61 6e 20 2a 2f 0a 20 Big-endian */.
afa70 20 20 20 20 20 77 68 69 6c 65 28 20 7a 49 6e 3c while( zIn<
afa80 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 20 zTerm ){.
afa90 20 2f 2a 20 63 20 3d 20 73 71 6c 69 74 65 33 55 /* c = sqlite3U
afaa0 74 66 38 52 65 61 64 28 7a 49 6e 2c 20 7a 54 65 tf8Read(zIn, zTe
afab0 72 6d 2c 20 28 63 6f 6e 73 74 20 75 38 2a 2a 29 rm, (const u8**)
afac0 26 7a 49 6e 29 3b 20 2a 2f 0a 20 20 20 20 20 20 &zIn); */.
afad0 20 20 52 45 41 44 5f 55 54 46 38 28 7a 49 6e 2c READ_UTF8(zIn,
afae0 20 7a 54 65 72 6d 2c 20 63 29 3b 0a 20 20 20 20 zTerm, c);.
afaf0 20 20 20 20 57 52 49 54 45 5f 55 54 46 31 36 42 WRITE_UTF16B
afb00 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20 7d E(z, c);. }
afb10 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d . }. pMem-
afb20 3e 6e 20 3d 20 28 69 6e 74 29 28 7a 20 2d 20 7a >n = (int)(z - z
afb30 4f 75 74 29 3b 0a 20 20 20 20 2a 7a 2b 2b 20 3d Out);. *z++ =
afb40 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 0;. }else{.
afb50 20 61 73 73 65 72 74 28 20 64 65 73 69 72 65 64 assert( desired
afb60 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 Enc==SQLITE_UTF8
afb70 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4d 65 6d );. if( pMem
afb80 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 ->enc==SQLITE_UT
afb90 46 31 36 4c 45 20 29 7b 0a 20 20 20 20 20 20 2f F16LE ){. /
afba0 2a 20 55 54 46 2d 31 36 20 4c 69 74 74 6c 65 2d * UTF-16 Little-
afbb0 65 6e 64 69 61 6e 20 2d 3e 20 55 54 46 2d 38 20 endian -> UTF-8
afbc0 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 */. while(
afbd0 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20 zIn<zTerm ){.
afbe0 20 20 20 20 20 52 45 41 44 5f 55 54 46 31 36 4c READ_UTF16L
afbf0 45 28 7a 49 6e 2c 20 7a 49 6e 3c 7a 54 65 72 6d E(zIn, zIn<zTerm
afc00 2c 20 63 29 3b 20 0a 20 20 20 20 20 20 20 20 57 , c); . W
afc10 52 49 54 45 5f 55 54 46 38 28 7a 2c 20 63 29 3b RITE_UTF8(z, c);
afc20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c . }. }el
afc30 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 55 54 46 se{. /* UTF
afc40 2d 31 36 20 42 69 67 2d 65 6e 64 69 61 6e 20 2d -16 Big-endian -
afc50 3e 20 55 54 46 2d 38 20 2a 2f 0a 20 20 20 20 20 > UTF-8 */.
afc60 20 77 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 while( zIn<zTer
afc70 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 52 45 41 m ){. REA
afc80 44 5f 55 54 46 31 36 42 45 28 7a 49 6e 2c 20 7a D_UTF16BE(zIn, z
afc90 49 6e 3c 7a 54 65 72 6d 2c 20 63 29 3b 20 0a 20 In<zTerm, c); .
afca0 20 20 20 20 20 20 20 57 52 49 54 45 5f 55 54 46 WRITE_UTF
afcb0 38 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20 7d 8(z, c);. }
afcc0 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d . }. pMem-
afcd0 3e 6e 20 3d 20 28 69 6e 74 29 28 7a 20 2d 20 7a >n = (int)(z - z
afce0 4f 75 74 29 3b 0a 20 20 7d 0a 20 20 2a 7a 20 3d Out);. }. *z =
afcf0 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 0;. assert( (p
afd00 4d 65 6d 2d 3e 6e 2b 28 64 65 73 69 72 65 64 45 Mem->n+(desiredE
afd10 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 3f nc==SQLITE_UTF8?
afd20 31 3a 32 29 29 3c 3d 6c 65 6e 20 29 3b 0a 0a 20 1:2))<=len );..
afd30 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
afd40 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 elease(pMem);.
afd50 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 3d 20 7e pMem->flags &= ~
afd60 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f (MEM_Static|MEM_
afd70 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a Dyn|MEM_Ephem);.
afd80 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 64 65 pMem->enc = de
afd90 73 69 72 65 64 45 6e 63 3b 0a 20 20 70 4d 65 6d siredEnc;. pMem
afda0 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 4d 45 4d 5f ->flags |= (MEM_
afdb0 54 65 72 6d 7c 4d 45 4d 5f 44 79 6e 29 3b 0a 20 Term|MEM_Dyn);.
afdc0 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 pMem->z = (char
afdd0 2a 29 7a 4f 75 74 3b 0a 20 20 70 4d 65 6d 2d 3e *)zOut;. pMem->
afde0 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d 2d 3e zMalloc = pMem->
afdf0 7a 3b 0a 0a 74 72 61 6e 73 6c 61 74 65 5f 6f 75 z;..translate_ou
afe00 74 3a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 t:.#if defined(T
afe10 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 29 20 RANSLATE_TRACE)
afe20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 && defined(SQLIT
afe30 45 5f 44 45 42 55 47 29 0a 20 20 7b 0a 20 20 20 E_DEBUG). {.
afe40 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d 3b char zBuf[100];
afe50 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
afe60 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74 28 70 MemPrettyPrint(p
afe70 4d 65 6d 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20 Mem, zBuf);.
afe80 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 fprintf(stderr,
afe90 22 4f 55 54 50 55 54 3a 20 25 73 5c 6e 22 2c 20 "OUTPUT: %s\n",
afea0 7a 42 75 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 zBuf);. }.#endi
afeb0 66 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 f. return SQLIT
afec0 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 E_OK;.}../*.** T
afed0 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 his routine chec
afee0 6b 73 20 66 6f 72 20 61 20 62 79 74 65 2d 6f 72 ks for a byte-or
afef0 64 65 72 20 6d 61 72 6b 20 61 74 20 74 68 65 20 der mark at the
aff00 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 beginning of the
aff10 20 0a 2a 2a 20 55 54 46 2d 31 36 20 73 74 72 69 .** UTF-16 stri
aff20 6e 67 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d ng stored in *pM
aff30 65 6d 2e 20 49 66 20 6f 6e 65 20 69 73 20 70 72 em. If one is pr
aff40 65 73 65 6e 74 2c 20 69 74 20 69 73 20 72 65 6d esent, it is rem
aff50 6f 76 65 64 20 61 6e 64 0a 2a 2a 20 74 68 65 20 oved and.** the
aff60 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 encoding of the
aff70 4d 65 6d 20 61 64 6a 75 73 74 65 64 2e 20 54 68 Mem adjusted. Th
aff80 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 is routine does
aff90 6e 6f 74 20 64 6f 20 61 6e 79 0a 2a 2a 20 62 79 not do any.** by
affa0 74 65 2d 73 77 61 70 70 69 6e 67 2c 20 69 74 20 te-swapping, it
affb0 6a 75 73 74 20 73 65 74 73 20 4d 65 6d 2e 65 6e just sets Mem.en
affc0 63 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e c appropriately.
affd0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 6c 6c 6f 63 .**.** The alloc
affe0 61 74 69 6f 6e 20 28 73 74 61 74 69 63 2c 20 64 ation (static, d
afff0 79 6e 61 6d 69 63 20 65 74 63 2e 29 20 61 6e 64 ynamic etc.) and
b0000 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 encoding of the
b0010 20 4d 65 6d 20 6d 61 79 20 62 65 0a 2a 2a 20 63 Mem may be.** c
b0020 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20 66 hanged by this f
b0030 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 unction..*/.SQLI
b0040 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
b0050 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 48 61 6e qlite3VdbeMemHan
b0060 64 6c 65 42 6f 6d 28 4d 65 6d 20 2a 70 4d 65 6d dleBom(Mem *pMem
b0070 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
b0080 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 38 20 62 6f LITE_OK;. u8 bo
b0090 6d 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 m = 0;.. assert
b00a0 28 20 70 4d 65 6d 2d 3e 6e 3e 3d 30 20 29 3b 0a ( pMem->n>=0 );.
b00b0 20 20 69 66 28 20 70 4d 65 6d 2d 3e 6e 3e 31 20 if( pMem->n>1
b00c0 29 7b 0a 20 20 20 20 75 38 20 62 31 20 3d 20 2a ){. u8 b1 = *
b00d0 28 75 38 20 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 (u8 *)pMem->z;.
b00e0 20 20 20 75 38 20 62 32 20 3d 20 2a 28 28 28 75 u8 b2 = *(((u
b00f0 38 20 2a 29 70 4d 65 6d 2d 3e 7a 29 20 2b 20 31 8 *)pMem->z) + 1
b0100 29 3b 0a 20 20 20 20 69 66 28 20 62 31 3d 3d 30 );. if( b1==0
b0110 78 46 45 20 26 26 20 62 32 3d 3d 30 78 46 46 20 xFE && b2==0xFF
b0120 29 7b 0a 20 20 20 20 20 20 62 6f 6d 20 3d 20 53 ){. bom = S
b0130 51 4c 49 54 45 5f 55 54 46 31 36 42 45 3b 0a 20 QLITE_UTF16BE;.
b0140 20 20 20 7d 0a 20 20 20 20 69 66 28 20 62 31 3d }. if( b1=
b0150 3d 30 78 46 46 20 26 26 20 62 32 3d 3d 30 78 46 =0xFF && b2==0xF
b0160 45 20 29 7b 0a 20 20 20 20 20 20 62 6f 6d 20 3d E ){. bom =
b0170 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 3b SQLITE_UTF16LE;
b0180 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 . }. }. .
b0190 69 66 28 20 62 6f 6d 20 29 7b 0a 20 20 20 20 72 if( bom ){. r
b01a0 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d c = sqlite3VdbeM
b01b0 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 emMakeWriteable(
b01c0 70 4d 65 6d 29 3b 0a 20 20 20 20 69 66 28 20 72 pMem);. if( r
b01d0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
b01e0 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 2d 3d pMem->n -=
b01f0 20 32 3b 0a 20 20 20 20 20 20 6d 65 6d 6d 6f 76 2;. memmov
b0200 65 28 70 4d 65 6d 2d 3e 7a 2c 20 26 70 4d 65 6d e(pMem->z, &pMem
b0210 2d 3e 7a 5b 32 5d 2c 20 70 4d 65 6d 2d 3e 6e 29 ->z[2], pMem->n)
b0220 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b ;. pMem->z[
b0230 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 27 5c 30 27 3b pMem->n] = '\0';
b0240 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70 . pMem->z[p
b0250 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 27 5c 30 27 Mem->n+1] = '\0'
b0260 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c ;. pMem->fl
b0270 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b ags |= MEM_Term;
b0280 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 . pMem->enc
b0290 20 3d 20 62 6f 6d 3b 0a 20 20 20 20 7d 0a 20 20 = bom;. }.
b02a0 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
b02b0 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
b02c0 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a E_OMIT_UTF16 */.
b02d0 0a 2f 2a 0a 2a 2a 20 70 5a 20 69 73 20 61 20 55 ./*.** pZ is a U
b02e0 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 75 6e 69 TF-8 encoded uni
b02f0 63 6f 64 65 20 73 74 72 69 6e 67 2e 20 49 66 20 code string. If
b0300 6e 42 79 74 65 20 69 73 20 6c 65 73 73 20 74 68 nByte is less th
b0310 61 6e 20 7a 65 72 6f 2c 0a 2a 2a 20 72 65 74 75 an zero,.** retu
b0320 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rn the number of
b0330 20 75 6e 69 63 6f 64 65 20 63 68 61 72 61 63 74 unicode charact
b0340 65 72 73 20 69 6e 20 70 5a 20 75 70 20 74 6f 20 ers in pZ up to
b0350 28 62 75 74 20 6e 6f 74 20 69 6e 63 6c 75 64 69 (but not includi
b0360 6e 67 29 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 ng).** the first
b0370 20 30 78 30 30 20 62 79 74 65 2e 20 49 66 20 6e 0x00 byte. If n
b0380 42 79 74 65 20 69 73 20 6e 6f 74 20 6c 65 73 73 Byte is not less
b0390 20 74 68 61 6e 20 7a 65 72 6f 2c 20 72 65 74 75 than zero, retu
b03a0 72 6e 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 rn the.** number
b03b0 20 6f 66 20 75 6e 69 63 6f 64 65 20 63 68 61 72 of unicode char
b03c0 61 63 74 65 72 73 20 69 6e 20 74 68 65 20 66 69 acters in the fi
b03d0 72 73 74 20 6e 42 79 74 65 20 6f 66 20 70 5a 20 rst nByte of pZ
b03e0 28 6f 72 20 75 70 20 74 6f 20 0a 2a 2a 20 74 68 (or up to .** th
b03f0 65 20 66 69 72 73 74 20 30 78 30 30 2c 20 77 68 e first 0x00, wh
b0400 69 63 68 65 76 65 72 20 63 6f 6d 65 73 20 66 69 ichever comes fi
b0410 72 73 74 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f rst)..*/.SQLITE_
b0420 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
b0430 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28 63 te3Utf8CharLen(c
b0440 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 2c 20 onst char *zIn,
b0450 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 69 6e int nByte){. in
b0460 74 20 72 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 t r = 0;. const
b0470 20 75 38 20 2a 7a 20 3d 20 28 63 6f 6e 73 74 20 u8 *z = (const
b0480 75 38 2a 29 7a 49 6e 3b 0a 20 20 63 6f 6e 73 74 u8*)zIn;. const
b0490 20 75 38 20 2a 7a 54 65 72 6d 3b 0a 20 20 69 66 u8 *zTerm;. if
b04a0 28 20 6e 42 79 74 65 3e 3d 30 20 29 7b 0a 20 20 ( nByte>=0 ){.
b04b0 20 20 7a 54 65 72 6d 20 3d 20 26 7a 5b 6e 42 79 zTerm = &z[nBy
b04c0 74 65 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 te];. }else{.
b04d0 20 20 7a 54 65 72 6d 20 3d 20 28 63 6f 6e 73 74 zTerm = (const
b04e0 20 75 38 2a 29 28 2d 31 29 3b 0a 20 20 7d 0a 20 u8*)(-1);. }.
b04f0 20 61 73 73 65 72 74 28 20 7a 3c 3d 7a 54 65 72 assert( z<=zTer
b0500 6d 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a m );. while( *z
b0510 21 3d 30 20 26 26 20 7a 3c 7a 54 65 72 6d 20 29 !=0 && z<zTerm )
b0520 7b 0a 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 {. SQLITE_SKI
b0530 50 5f 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 72 P_UTF8(z);. r
b0540 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e ++;. }. return
b0550 20 72 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 74 r;.}../* This t
b0560 65 73 74 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 est function is
b0570 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 75 73 not currently us
b0580 65 64 20 62 79 20 74 68 65 20 61 75 74 6f 6d 61 ed by the automa
b0590 74 65 64 20 74 65 73 74 2d 73 75 69 74 65 2e 20 ted test-suite.
b05a0 0a 2a 2a 20 48 65 6e 63 65 20 69 74 20 69 73 20 .** Hence it is
b05b0 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 only available i
b05c0 6e 20 64 65 62 75 67 20 62 75 69 6c 64 73 2e 0a n debug builds..
b05d0 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 */.#if defined(S
b05e0 51 4c 49 54 45 5f 54 45 53 54 29 20 26 26 20 64 QLITE_TEST) && d
b05f0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 efined(SQLITE_DE
b0600 42 55 47 29 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 BUG)./*.** Trans
b0610 6c 61 74 65 20 55 54 46 2d 38 20 74 6f 20 55 54 late UTF-8 to UT
b0620 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 F-8..**.** This
b0630 68 61 73 20 74 68 65 20 65 66 66 65 63 74 20 6f has the effect o
b0640 66 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 74 68 f making sure th
b0650 61 74 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 at the string is
b0660 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a 2a 2a 20 well-formed.**
b0670 55 54 46 2d 38 2e 20 20 4d 69 73 63 6f 64 65 64 UTF-8. Miscoded
b0680 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65 20 characters are
b0690 72 65 6d 6f 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 removed..**.** T
b06a0 68 65 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20 69 he translation i
b06b0 73 20 64 6f 6e 65 20 69 6e 2d 70 6c 61 63 65 20 s done in-place
b06c0 28 73 69 6e 63 65 20 69 74 20 69 73 20 69 6d 70 (since it is imp
b06d0 6f 73 73 69 62 6c 65 20 66 6f 72 20 74 68 65 0a ossible for the.
b06e0 2a 2a 20 63 6f 72 72 65 63 74 20 55 54 46 2d 38 ** correct UTF-8
b06f0 20 65 6e 63 6f 64 69 6e 67 20 74 6f 20 62 65 20 encoding to be
b0700 6c 6f 6e 67 65 72 20 74 68 61 6e 20 61 20 6d 61 longer than a ma
b0710 6c 66 6f 72 6d 65 64 20 65 6e 63 6f 64 69 6e 67 lformed encoding
b0720 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 )..*/.SQLITE_PRI
b0730 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
b0740 55 74 66 38 54 6f 38 28 75 6e 73 69 67 6e 65 64 Utf8To8(unsigned
b0750 20 63 68 61 72 20 2a 7a 49 6e 29 7b 0a 20 20 75 char *zIn){. u
b0760 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 4f nsigned char *zO
b0770 75 74 20 3d 20 7a 49 6e 3b 0a 20 20 75 6e 73 69 ut = zIn;. unsi
b0780 67 6e 65 64 20 63 68 61 72 20 2a 7a 53 74 61 72 gned char *zStar
b0790 74 20 3d 20 7a 49 6e 3b 0a 20 20 75 33 32 20 63 t = zIn;. u32 c
b07a0 3b 0a 0a 20 20 77 68 69 6c 65 28 20 7a 49 6e 5b ;.. while( zIn[
b07b0 30 5d 20 29 7b 0a 20 20 20 20 63 20 3d 20 73 71 0] ){. c = sq
b07c0 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 49 lite3Utf8Read(zI
b07d0 6e 2c 20 28 63 6f 6e 73 74 20 75 38 2a 2a 29 26 n, (const u8**)&
b07e0 7a 49 6e 29 3b 0a 20 20 20 20 69 66 28 20 63 21 zIn);. if( c!
b07f0 3d 30 78 66 66 66 64 20 29 7b 0a 20 20 20 20 20 =0xfffd ){.
b0800 20 57 52 49 54 45 5f 55 54 46 38 28 7a 4f 75 74 WRITE_UTF8(zOut
b0810 2c 20 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a , c);. }. }.
b0820 20 20 2a 7a 4f 75 74 20 3d 20 30 3b 0a 20 20 72 *zOut = 0;. r
b0830 65 74 75 72 6e 20 28 69 6e 74 29 28 7a 4f 75 74 eturn (int)(zOut
b0840 20 2d 20 7a 53 74 61 72 74 29 3b 0a 7d 0a 23 65 - zStart);.}.#e
b0850 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 ndif..#ifndef SQ
b0860 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a LITE_OMIT_UTF16.
b0870 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 /*.** Convert a
b0880 55 54 46 2d 31 36 20 73 74 72 69 6e 67 20 69 6e UTF-16 string in
b0890 20 74 68 65 20 6e 61 74 69 76 65 20 65 6e 63 6f the native enco
b08a0 64 69 6e 67 20 69 6e 74 6f 20 61 20 55 54 46 2d ding into a UTF-
b08b0 38 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 4d 65 6d 8 string..** Mem
b08c0 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 ory to hold the
b08d0 55 54 46 2d 38 20 73 74 72 69 6e 67 20 69 73 20 UTF-8 string is
b08e0 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 obtained from sq
b08f0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 61 6e 64 lite3_malloc and
b0900 20 6d 75 73 74 0a 2a 2a 20 62 65 20 66 72 65 65 must.** be free
b0910 64 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 d by the calling
b0920 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a function..**.**
b0930 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 NULL is returne
b0940 64 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e d if there is an
b0950 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f allocation erro
b0960 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 r..*/.SQLITE_PRI
b0970 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 VATE char *sqlit
b0980 65 33 55 74 66 31 36 74 6f 38 28 73 71 6c 69 74 e3Utf16to8(sqlit
b0990 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 76 6f e3 *db, const vo
b09a0 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 42 79 74 65 id *z, int nByte
b09b0 29 7b 0a 20 20 4d 65 6d 20 6d 3b 0a 20 20 6d 65 ){. Mem m;. me
b09c0 6d 73 65 74 28 26 6d 2c 20 30 2c 20 73 69 7a 65 mset(&m, 0, size
b09d0 6f 66 28 6d 29 29 3b 0a 20 20 6d 2e 64 62 20 3d of(m));. m.db =
b09e0 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 db;. sqlite3Vd
b09f0 62 65 4d 65 6d 53 65 74 53 74 72 28 26 6d 2c 20 beMemSetStr(&m,
b0a00 7a 2c 20 6e 42 79 74 65 2c 20 53 51 4c 49 54 45 z, nByte, SQLITE
b0a10 5f 55 54 46 31 36 4e 41 54 49 56 45 2c 20 53 51 _UTF16NATIVE, SQ
b0a20 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 LITE_STATIC);.
b0a30 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
b0a40 65 45 6e 63 6f 64 69 6e 67 28 26 6d 2c 20 53 51 eEncoding(&m, SQ
b0a50 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 69 66 LITE_UTF8);. if
b0a60 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ( db->mallocFail
b0a70 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ed ){. sqlite
b0a80 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 3VdbeMemRelease(
b0a90 26 6d 29 3b 0a 20 20 20 20 6d 2e 7a 20 3d 20 30 &m);. m.z = 0
b0aa0 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
b0ab0 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 54 (m.flags & MEM_T
b0ac0 65 72 6d 29 21 3d 30 20 7c 7c 20 64 62 2d 3e 6d erm)!=0 || db->m
b0ad0 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 allocFailed );.
b0ae0 20 61 73 73 65 72 74 28 20 28 6d 2e 66 6c 61 67 assert( (m.flag
b0af0 73 20 26 20 4d 45 4d 5f 53 74 72 29 21 3d 30 20 s & MEM_Str)!=0
b0b00 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 || db->mallocFai
b0b10 6c 65 64 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 led );. return
b0b20 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 (m.flags & MEM_D
b0b30 79 6e 29 21 3d 30 20 3f 20 6d 2e 7a 20 3a 20 73 yn)!=0 ? m.z : s
b0b40 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 qlite3DbStrDup(d
b0b50 62 2c 20 6d 2e 7a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a b, m.z);.}../*.*
b0b60 2a 20 43 6f 6e 76 65 72 74 20 61 20 55 54 46 2d * Convert a UTF-
b0b70 38 20 73 74 72 69 6e 67 20 74 6f 20 74 68 65 20 8 string to the
b0b80 55 54 46 2d 31 36 20 65 6e 63 6f 64 69 6e 67 20 UTF-16 encoding
b0b90 73 70 65 63 69 66 69 65 64 20 62 79 20 70 61 72 specified by par
b0ba0 61 6d 65 74 65 72 0a 2a 2a 20 65 6e 63 2e 20 41 ameter.** enc. A
b0bb0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
b0bc0 6e 65 77 20 73 74 72 69 6e 67 20 69 73 20 72 65 new string is re
b0bd0 74 75 72 6e 65 64 2c 20 61 6e 64 20 74 68 65 20 turned, and the
b0be0 76 61 6c 75 65 20 6f 66 20 2a 70 6e 4f 75 74 0a value of *pnOut.
b0bf0 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 ** is set to the
b0c00 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 72 length of the r
b0c10 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 eturned string i
b0c20 6e 20 62 79 74 65 73 2e 20 54 68 65 20 63 61 6c n bytes. The cal
b0c30 6c 20 73 68 6f 75 6c 64 0a 2a 2a 20 61 72 72 61 l should.** arra
b0c40 6e 67 65 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69 nge to call sqli
b0c50 74 65 33 44 62 46 72 65 65 28 29 20 6f 6e 20 74 te3DbFree() on t
b0c60 68 65 20 72 65 74 75 72 6e 65 64 20 70 6f 69 6e he returned poin
b0c70 74 65 72 20 77 68 65 6e 20 69 74 20 69 73 0a 2a ter when it is.*
b0c80 2a 20 6e 6f 20 6c 6f 6e 67 65 72 20 72 65 71 75 * no longer requ
b0c90 69 72 65 64 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 ired..** .** If
b0ca0 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 a malloc failure
b0cb0 20 6f 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69 73 occurs, NULL is
b0cc0 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 returned and th
b0cd0 65 20 64 62 2e 6d 61 6c 6c 6f 63 46 61 69 6c 65 e db.mallocFaile
b0ce0 64 0a 2a 2a 20 66 6c 61 67 20 73 65 74 2e 0a 2a d.** flag set..*
b0cf0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
b0d00 45 4e 41 42 4c 45 5f 53 54 41 54 32 0a 53 51 4c ENABLE_STAT2.SQL
b0d10 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 ITE_PRIVATE char
b0d20 20 2a 73 71 6c 69 74 65 33 55 74 66 38 74 6f 31 *sqlite3Utf8to1
b0d30 36 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 75 6(sqlite3 *db, u
b0d40 38 20 65 6e 63 2c 20 63 68 61 72 20 2a 7a 2c 20 8 enc, char *z,
b0d50 69 6e 74 20 6e 2c 20 69 6e 74 20 2a 70 6e 4f 75 int n, int *pnOu
b0d60 74 29 7b 0a 20 20 4d 65 6d 20 6d 3b 0a 20 20 6d t){. Mem m;. m
b0d70 65 6d 73 65 74 28 26 6d 2c 20 30 2c 20 73 69 7a emset(&m, 0, siz
b0d80 65 6f 66 28 6d 29 29 3b 0a 20 20 6d 2e 64 62 20 eof(m));. m.db
b0d90 3d 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 56 = db;. sqlite3V
b0da0 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 6d 2c dbeMemSetStr(&m,
b0db0 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 z, n, SQLITE_UT
b0dc0 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 F8, SQLITE_STATI
b0dd0 43 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 C);. if( sqlite
b0de0 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74 3VdbeMemTranslat
b0df0 65 28 26 6d 2c 20 65 6e 63 29 20 29 7b 0a 20 20 e(&m, enc) ){.
b0e00 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 assert( db->ma
b0e10 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 llocFailed );.
b0e20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
b0e30 20 20 61 73 73 65 72 74 28 20 6d 2e 7a 3d 3d 6d assert( m.z==m
b0e40 2e 7a 4d 61 6c 6c 6f 63 20 29 3b 0a 20 20 2a 70 .zMalloc );. *p
b0e50 6e 4f 75 74 20 3d 20 6d 2e 6e 3b 0a 20 20 72 65 nOut = m.n;. re
b0e60 74 75 72 6e 20 6d 2e 7a 3b 0a 7d 0a 23 65 6e 64 turn m.z;.}.#end
b0e70 69 66 0a 0a 2f 2a 0a 2a 2a 20 7a 49 6e 20 69 73 if../*.** zIn is
b0e80 20 61 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 a UTF-16 encode
b0e90 64 20 75 6e 69 63 6f 64 65 20 73 74 72 69 6e 67 d unicode string
b0ea0 20 61 74 20 6c 65 61 73 74 20 6e 43 68 61 72 20 at least nChar
b0eb0 63 68 61 72 61 63 74 65 72 73 20 6c 6f 6e 67 2e characters long.
b0ec0 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
b0ed0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 umber of bytes i
b0ee0 6e 20 74 68 65 20 66 69 72 73 74 20 6e 43 68 61 n the first nCha
b0ef0 72 20 75 6e 69 63 6f 64 65 20 63 68 61 72 61 63 r unicode charac
b0f00 74 65 72 73 0a 2a 2a 20 69 6e 20 70 5a 2e 20 20 ters.** in pZ.
b0f10 6e 43 68 61 72 20 6d 75 73 74 20 62 65 20 6e 6f nChar must be no
b0f20 6e 2d 6e 65 67 61 74 69 76 65 2e 0a 2a 2f 0a 53 n-negative..*/.S
b0f30 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
b0f40 74 20 73 71 6c 69 74 65 33 55 74 66 31 36 42 79 t sqlite3Utf16By
b0f50 74 65 4c 65 6e 28 63 6f 6e 73 74 20 76 6f 69 64 teLen(const void
b0f60 20 2a 7a 49 6e 2c 20 69 6e 74 20 6e 43 68 61 72 *zIn, int nChar
b0f70 29 7b 0a 20 20 69 6e 74 20 63 3b 0a 20 20 75 6e ){. int c;. un
b0f80 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 signed char cons
b0f90 74 20 2a 7a 20 3d 20 7a 49 6e 3b 0a 20 20 69 6e t *z = zIn;. in
b0fa0 74 20 6e 20 3d 20 30 3b 0a 20 20 0a 20 20 69 66 t n = 0;. . if
b0fb0 28 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 ( SQLITE_UTF16NA
b0fc0 54 49 56 45 3d 3d 53 51 4c 49 54 45 5f 55 54 46 TIVE==SQLITE_UTF
b0fd0 31 36 42 45 20 29 7b 0a 20 20 20 20 77 68 69 6c 16BE ){. whil
b0fe0 65 28 20 6e 3c 6e 43 68 61 72 20 29 7b 0a 20 20 e( n<nChar ){.
b0ff0 20 20 20 20 52 45 41 44 5f 55 54 46 31 36 42 45 READ_UTF16BE
b1000 28 7a 2c 20 31 2c 20 63 29 3b 0a 20 20 20 20 20 (z, 1, c);.
b1010 20 6e 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 n++;. }. }e
b1020 6c 73 65 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 lse{. while(
b1030 6e 3c 6e 43 68 61 72 20 29 7b 0a 20 20 20 20 20 n<nChar ){.
b1040 20 52 45 41 44 5f 55 54 46 31 36 4c 45 28 7a 2c READ_UTF16LE(z,
b1050 20 31 2c 20 63 29 3b 0a 20 20 20 20 20 20 6e 2b 1, c);. n+
b1060 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 +;. }. }. r
b1070 65 74 75 72 6e 20 28 69 6e 74 29 28 7a 2d 28 75 eturn (int)(z-(u
b1080 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e nsigned char con
b1090 73 74 20 2a 29 7a 49 6e 29 3b 0a 7d 0a 0a 23 69 st *)zIn);.}..#i
b10a0 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
b10b0 5f 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20 54 68 69 _TEST)./*.** Thi
b10c0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
b10d0 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 54 43 4c led from the TCL
b10e0 20 74 65 73 74 20 66 75 6e 63 74 69 6f 6e 20 22 test function "
b10f0 74 72 61 6e 73 6c 61 74 65 5f 73 65 6c 66 74 65 translate_selfte
b1100 73 74 22 2e 0a 2a 2a 20 49 74 20 63 68 65 63 6b st"..** It check
b1110 73 20 74 68 61 74 20 74 68 65 20 70 72 69 6d 69 s that the primi
b1120 74 69 76 65 73 20 66 6f 72 20 73 65 72 69 61 6c tives for serial
b1130 69 7a 69 6e 67 20 61 6e 64 20 64 65 73 65 72 69 izing and deseri
b1140 61 6c 69 7a 69 6e 67 0a 2a 2a 20 63 68 61 72 61 alizing.** chara
b1150 63 74 65 72 73 20 69 6e 20 65 61 63 68 20 65 6e cters in each en
b1160 63 6f 64 69 6e 67 20 61 72 65 20 69 6e 76 65 72 coding are inver
b1170 73 65 73 20 6f 66 20 65 61 63 68 20 6f 74 68 65 ses of each othe
b1180 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 r..*/.SQLITE_PRI
b1190 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
b11a0 33 55 74 66 53 65 6c 66 54 65 73 74 28 76 6f 69 3UtfSelfTest(voi
b11b0 64 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 d){. unsigned i
b11c0 6e 74 20 69 2c 20 74 3b 0a 20 20 75 6e 73 69 67 nt i, t;. unsig
b11d0 6e 65 64 20 63 68 61 72 20 7a 42 75 66 5b 32 30 ned char zBuf[20
b11e0 5d 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 ];. unsigned ch
b11f0 61 72 20 2a 7a 3b 0a 20 20 69 6e 74 20 6e 3b 0a ar *z;. int n;.
b1200 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 63 unsigned int c
b1210 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c ;.. for(i=0; i<
b1220 30 78 30 30 31 31 30 30 30 30 3b 20 69 2b 2b 29 0x00110000; i++)
b1230 7b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a {. z = zBuf;.
b1240 20 20 20 20 57 52 49 54 45 5f 55 54 46 38 28 7a WRITE_UTF8(z
b1250 2c 20 69 29 3b 0a 20 20 20 20 6e 20 3d 20 28 69 , i);. n = (i
b1260 6e 74 29 28 7a 2d 7a 42 75 66 29 3b 0a 20 20 20 nt)(z-zBuf);.
b1270 20 61 73 73 65 72 74 28 20 6e 3e 30 20 26 26 20 assert( n>0 &&
b1280 6e 3c 3d 34 20 29 3b 0a 20 20 20 20 7a 5b 30 5d n<=4 );. z[0]
b1290 20 3d 20 30 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 = 0;. z = zB
b12a0 75 66 3b 0a 20 20 20 20 63 20 3d 20 73 71 6c 69 uf;. c = sqli
b12b0 74 65 33 55 74 66 38 52 65 61 64 28 7a 2c 20 28 te3Utf8Read(z, (
b12c0 63 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 29 3b 0a const u8**)&z);.
b12d0 20 20 20 20 74 20 3d 20 69 3b 0a 20 20 20 20 69 t = i;. i
b12e0 66 28 20 69 3e 3d 30 78 44 38 30 30 20 26 26 20 f( i>=0xD800 &&
b12f0 69 3c 3d 30 78 44 46 46 46 20 29 20 74 20 3d 20 i<=0xDFFF ) t =
b1300 30 78 46 46 46 44 3b 0a 20 20 20 20 69 66 28 20 0xFFFD;. if(
b1310 28 69 26 30 78 46 46 46 46 46 46 46 45 29 3d 3d (i&0xFFFFFFFE)==
b1320 30 78 46 46 46 45 20 29 20 74 20 3d 20 30 78 46 0xFFFE ) t = 0xF
b1330 46 46 44 3b 0a 20 20 20 20 61 73 73 65 72 74 28 FFD;. assert(
b1340 20 63 3d 3d 74 20 29 3b 0a 20 20 20 20 61 73 73 c==t );. ass
b1350 65 72 74 28 20 28 7a 2d 7a 42 75 66 29 3d 3d 6e ert( (z-zBuf)==n
b1360 20 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d );. }. for(i=
b1370 30 3b 20 69 3c 30 78 30 30 31 31 30 30 30 30 3b 0; i<0x00110000;
b1380 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 69 i++){. if( i
b1390 3e 3d 30 78 44 38 30 30 20 26 26 20 69 3c 30 78 >=0xD800 && i<0x
b13a0 45 30 30 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b E000 ) continue;
b13b0 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 . z = zBuf;.
b13c0 20 20 20 57 52 49 54 45 5f 55 54 46 31 36 4c 45 WRITE_UTF16LE
b13d0 28 7a 2c 20 69 29 3b 0a 20 20 20 20 6e 20 3d 20 (z, i);. n =
b13e0 28 69 6e 74 29 28 7a 2d 7a 42 75 66 29 3b 0a 20 (int)(z-zBuf);.
b13f0 20 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 26 assert( n>0 &
b1400 26 20 6e 3c 3d 34 20 29 3b 0a 20 20 20 20 7a 5b & n<=4 );. z[
b1410 30 5d 20 3d 20 30 3b 0a 20 20 20 20 7a 20 3d 20 0] = 0;. z =
b1420 7a 42 75 66 3b 0a 20 20 20 20 52 45 41 44 5f 55 zBuf;. READ_U
b1430 54 46 31 36 4c 45 28 7a 2c 20 31 2c 20 63 29 3b TF16LE(z, 1, c);
b1440 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 3d 3d . assert( c==
b1450 69 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 i );. assert(
b1460 20 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b 0a (z-zBuf)==n );.
b1470 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 }. for(i=0; i
b1480 3c 30 78 30 30 31 31 30 30 30 30 3b 20 69 2b 2b <0x00110000; i++
b1490 29 7b 0a 20 20 20 20 69 66 28 20 69 3e 3d 30 78 ){. if( i>=0x
b14a0 44 38 30 30 20 26 26 20 69 3c 30 78 45 30 30 30 D800 && i<0xE000
b14b0 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 ) continue;.
b14c0 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57 z = zBuf;. W
b14d0 52 49 54 45 5f 55 54 46 31 36 42 45 28 7a 2c 20 RITE_UTF16BE(z,
b14e0 69 29 3b 0a 20 20 20 20 6e 20 3d 20 28 69 6e 74 i);. n = (int
b14f0 29 28 7a 2d 7a 42 75 66 29 3b 0a 20 20 20 20 61 )(z-zBuf);. a
b1500 73 73 65 72 74 28 20 6e 3e 30 20 26 26 20 6e 3c ssert( n>0 && n<
b1510 3d 34 20 29 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d =4 );. z[0] =
b1520 20 30 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 0;. z = zBuf
b1530 3b 0a 20 20 20 20 52 45 41 44 5f 55 54 46 31 36 ;. READ_UTF16
b1540 42 45 28 7a 2c 20 31 2c 20 63 29 3b 0a 20 20 20 BE(z, 1, c);.
b1550 20 61 73 73 65 72 74 28 20 63 3d 3d 69 20 29 3b assert( c==i );
b1560 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 7a 2d . assert( (z-
b1570 7a 42 75 66 29 3d 3d 6e 20 29 3b 0a 20 20 7d 0a zBuf)==n );. }.
b1580 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
b1590 54 45 5f 54 45 53 54 20 2a 2f 0a 23 65 6e 64 69 TE_TEST */.#endi
b15a0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
b15b0 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 2a 2a 2a _UTF16 */../****
b15c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
b15d0 66 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a f utf.c ********
b15e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b15f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b1600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
b1610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
b1620 20 66 69 6c 65 20 75 74 69 6c 2e 63 20 2a 2a 2a file util.c ***
b1630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b1640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b1650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
b1660 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 2001 September
b1670 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 15.**.** The aut
b1680 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
b1690 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
b16a0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
b16b0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
b16c0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
b16d0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
b16e0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
b16f0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
b1700 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
b1710 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
b1720 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
b1730 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
b1740 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
b1750 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
b1760 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
b1770 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
b1780 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
b1790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b17a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b17b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b17c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b17d0 2a 0a 2a 2a 20 55 74 69 6c 69 74 79 20 66 75 6e *.** Utility fun
b17e0 63 74 69 6f 6e 73 20 75 73 65 64 20 74 68 72 6f ctions used thro
b17f0 75 67 68 6f 75 74 20 73 71 6c 69 74 65 2e 0a 2a ughout sqlite..*
b1800 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
b1810 6f 6e 74 61 69 6e 73 20 66 75 6e 63 74 69 6f 6e ontains function
b1820 73 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6e 67 s for allocating
b1830 20 6d 65 6d 6f 72 79 2c 20 63 6f 6d 70 61 72 69 memory, compari
b1840 6e 67 0a 2a 2a 20 73 74 72 69 6e 67 73 2c 20 61 ng.** strings, a
b1850 6e 64 20 73 74 75 66 66 20 6c 69 6b 65 20 74 68 nd stuff like th
b1860 61 74 2e 0a 2a 2a 0a 2a 2f 0a 23 69 66 64 65 66 at..**.*/.#ifdef
b1870 20 53 51 4c 49 54 45 5f 48 41 56 45 5f 49 53 4e SQLITE_HAVE_ISN
b1880 41 4e 0a 23 20 69 6e 63 6c 75 64 65 20 3c 6d 61 AN.# include <ma
b1890 74 68 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a 2f 2a th.h>.#endif../*
b18a0 0a 2a 2a 20 52 6f 75 74 69 6e 65 20 6e 65 65 64 .** Routine need
b18b0 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20 74 68 ed to support th
b18c0 65 20 74 65 73 74 63 61 73 65 28 29 20 6d 61 63 e testcase() mac
b18d0 72 6f 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 ro..*/.#ifdef SQ
b18e0 4c 49 54 45 5f 43 4f 56 45 52 41 47 45 5f 54 45 LITE_COVERAGE_TE
b18f0 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ST.SQLITE_PRIVAT
b1900 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f E void sqlite3Co
b1910 76 65 72 61 67 65 28 69 6e 74 20 78 29 7b 0a 20 verage(int x){.
b1920 20 73 74 61 74 69 63 20 69 6e 74 20 64 75 6d 6d static int dumm
b1930 79 20 3d 20 30 3b 0a 20 20 64 75 6d 6d 79 20 2b y = 0;. dummy +
b1940 3d 20 78 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f = x;.}.#endif../
b1950 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 *.** Return true
b1960 20 69 66 20 74 68 65 20 66 6c 6f 61 74 69 6e 67 if the floating
b1970 20 70 6f 69 6e 74 20 76 61 6c 75 65 20 69 73 20 point value is
b1980 4e 6f 74 20 61 20 4e 75 6d 62 65 72 20 28 4e 61 Not a Number (Na
b1990 4e 29 2e 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 N)..**.** Use th
b19a0 65 20 6d 61 74 68 20 6c 69 62 72 61 72 79 20 69 e math library i
b19b0 73 6e 61 6e 28 29 20 66 75 6e 63 74 69 6f 6e 20 snan() function
b19c0 69 66 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 if compiled with
b19d0 20 53 51 4c 49 54 45 5f 48 41 56 45 5f 49 53 4e SQLITE_HAVE_ISN
b19e0 41 4e 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 AN..** Otherwise
b19f0 2c 20 77 65 20 68 61 76 65 20 6f 75 72 20 6f 77 , we have our ow
b1a00 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e n implementation
b1a10 20 74 68 61 74 20 77 6f 72 6b 73 20 6f 6e 20 6d that works on m
b1a20 6f 73 74 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a ost systems..*/.
b1a30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
b1a40 6e 74 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 nt sqlite3IsNaN(
b1a50 64 6f 75 62 6c 65 20 78 29 7b 0a 20 20 69 6e 74 double x){. int
b1a60 20 72 63 3b 20 20 20 2f 2a 20 54 68 65 20 76 61 rc; /* The va
b1a70 6c 75 65 20 72 65 74 75 72 6e 20 2a 2f 0a 23 69 lue return */.#i
b1a80 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 f !defined(SQLIT
b1a90 45 5f 48 41 56 45 5f 49 53 4e 41 4e 29 0a 20 20 E_HAVE_ISNAN).
b1aa0 2f 2a 0a 20 20 2a 2a 20 53 79 73 74 65 6d 73 20 /*. ** Systems
b1ab0 74 68 61 74 20 73 75 70 70 6f 72 74 20 74 68 65 that support the
b1ac0 20 69 73 6e 61 6e 28 29 20 6c 69 62 72 61 72 79 isnan() library
b1ad0 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 function should
b1ae0 20 70 72 6f 62 61 62 6c 79 0a 20 20 2a 2a 20 6d probably. ** m
b1af0 61 6b 65 20 75 73 65 20 6f 66 20 69 74 20 62 79 ake use of it by
b1b00 20 63 6f 6d 70 69 6c 69 6e 67 20 77 69 74 68 20 compiling with
b1b10 2d 44 53 51 4c 49 54 45 5f 48 41 56 45 5f 49 53 -DSQLITE_HAVE_IS
b1b20 4e 41 4e 2e 20 20 42 75 74 20 77 65 20 68 61 76 NAN. But we hav
b1b30 65 0a 20 20 2a 2a 20 66 6f 75 6e 64 20 74 68 61 e. ** found tha
b1b40 74 20 6d 61 6e 79 20 73 79 73 74 65 6d 73 20 64 t many systems d
b1b50 6f 20 6e 6f 74 20 68 61 76 65 20 61 20 77 6f 72 o not have a wor
b1b60 6b 69 6e 67 20 69 73 6e 61 6e 28 29 20 66 75 6e king isnan() fun
b1b70 63 74 69 6f 6e 20 73 6f 0a 20 20 2a 2a 20 74 68 ction so. ** th
b1b80 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f is implementatio
b1b90 6e 20 69 73 20 70 72 6f 76 69 64 65 64 20 61 73 n is provided as
b1ba0 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 2e an alternative.
b1bb0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 . **. ** This
b1bc0 4e 61 4e 20 74 65 73 74 20 73 6f 6d 65 74 69 6d NaN test sometim
b1bd0 65 73 20 66 61 69 6c 73 20 69 66 20 63 6f 6d 70 es fails if comp
b1be0 69 6c 65 64 20 6f 6e 20 47 43 43 20 77 69 74 68 iled on GCC with
b1bf0 20 2d 66 66 61 73 74 2d 6d 61 74 68 2e 0a 20 20 -ffast-math..
b1c00 2a 2a 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 ** On the other
b1c10 68 61 6e 64 2c 20 74 68 65 20 75 73 65 20 6f 66 hand, the use of
b1c20 20 2d 66 66 61 73 74 2d 6d 61 74 68 20 63 6f 6d -ffast-math com
b1c30 65 73 20 77 69 74 68 20 74 68 65 20 66 6f 6c 6c es with the foll
b1c40 6f 77 69 6e 67 0a 20 20 2a 2a 20 77 61 72 6e 69 owing. ** warni
b1c50 6e 67 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 ng:. **. **
b1c60 20 20 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 5b This option [
b1c70 2d 66 66 61 73 74 2d 6d 61 74 68 5d 20 73 68 6f -ffast-math] sho
b1c80 75 6c 64 20 6e 65 76 65 72 20 62 65 20 74 75 72 uld never be tur
b1c90 6e 65 64 20 6f 6e 20 62 79 20 61 6e 79 0a 20 20 ned on by any.
b1ca0 2a 2a 20 20 20 20 20 20 2d 4f 20 6f 70 74 69 6f ** -O optio
b1cb0 6e 20 73 69 6e 63 65 20 69 74 20 63 61 6e 20 72 n since it can r
b1cc0 65 73 75 6c 74 20 69 6e 20 69 6e 63 6f 72 72 65 esult in incorre
b1cd0 63 74 20 6f 75 74 70 75 74 20 66 6f 72 20 70 72 ct output for pr
b1ce0 6f 67 72 61 6d 73 0a 20 20 2a 2a 20 20 20 20 20 ograms. **
b1cf0 20 77 68 69 63 68 20 64 65 70 65 6e 64 20 6f 6e which depend on
b1d00 20 61 6e 20 65 78 61 63 74 20 69 6d 70 6c 65 6d an exact implem
b1d10 65 6e 74 61 74 69 6f 6e 20 6f 66 20 49 45 45 45 entation of IEEE
b1d20 20 6f 72 20 49 53 4f 20 0a 20 20 2a 2a 20 20 20 or ISO . **
b1d30 20 20 20 72 75 6c 65 73 2f 73 70 65 63 69 66 69 rules/specifi
b1d40 63 61 74 69 6f 6e 73 20 66 6f 72 20 6d 61 74 68 cations for math
b1d50 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2a functions.. **
b1d60 0a 20 20 2a 2a 20 55 6e 64 65 72 20 4d 53 56 43 . ** Under MSVC
b1d70 2c 20 74 68 69 73 20 4e 61 4e 20 74 65 73 74 20 , this NaN test
b1d80 6d 61 79 20 66 61 69 6c 20 69 66 20 63 6f 6d 70 may fail if comp
b1d90 69 6c 65 64 20 77 69 74 68 20 61 20 66 6c 6f 61 iled with a floa
b1da0 74 69 6e 67 2d 0a 20 20 2a 2a 20 70 6f 69 6e 74 ting-. ** point
b1db0 20 70 72 65 63 69 73 69 6f 6e 20 6d 6f 64 65 20 precision mode
b1dc0 6f 74 68 65 72 20 74 68 61 6e 20 2f 66 70 3a 70 other than /fp:p
b1dd0 72 65 63 69 73 65 2e 20 20 46 72 6f 6d 20 74 68 recise. From th
b1de0 65 20 4d 53 44 4e 20 0a 20 20 2a 2a 20 64 6f 63 e MSDN . ** doc
b1df0 75 6d 65 6e 74 61 74 69 6f 6e 3a 0a 20 20 2a 2a umentation:. **
b1e00 0a 20 20 2a 2a 20 20 20 20 20 20 54 68 65 20 63 . ** The c
b1e10 6f 6d 70 69 6c 65 72 20 5b 77 69 74 68 20 2f 66 ompiler [with /f
b1e20 70 3a 70 72 65 63 69 73 65 5d 20 77 69 6c 6c 20 p:precise] will
b1e30 70 72 6f 70 65 72 6c 79 20 68 61 6e 64 6c 65 20 properly handle
b1e40 63 6f 6d 70 61 72 69 73 6f 6e 73 20 0a 20 20 2a comparisons . *
b1e50 2a 20 20 20 20 20 20 69 6e 76 6f 6c 76 69 6e 67 * involving
b1e60 20 4e 61 4e 2e 20 46 6f 72 20 65 78 61 6d 70 6c NaN. For exampl
b1e70 65 2c 20 78 20 21 3d 20 78 20 65 76 61 6c 75 61 e, x != x evalua
b1e80 74 65 73 20 74 6f 20 74 72 75 65 20 69 66 20 78 tes to true if x
b1e90 20 69 73 20 4e 61 4e 20 0a 20 20 2a 2a 20 20 20 is NaN . **
b1ea0 20 20 20 2e 2e 2e 0a 20 20 2a 2f 0a 23 69 66 64 .... */.#ifd
b1eb0 65 66 20 5f 5f 46 41 53 54 5f 4d 41 54 48 5f 5f ef __FAST_MATH__
b1ec0 0a 23 20 65 72 72 6f 72 20 53 51 4c 69 74 65 20 .# error SQLite
b1ed0 77 69 6c 6c 20 6e 6f 74 20 77 6f 72 6b 20 63 6f will not work co
b1ee0 72 72 65 63 74 6c 79 20 77 69 74 68 20 74 68 65 rrectly with the
b1ef0 20 2d 66 66 61 73 74 2d 6d 61 74 68 20 6f 70 74 -ffast-math opt
b1f00 69 6f 6e 20 6f 66 20 47 43 43 2e 0a 23 65 6e 64 ion of GCC..#end
b1f10 69 66 0a 20 20 76 6f 6c 61 74 69 6c 65 20 64 6f if. volatile do
b1f20 75 62 6c 65 20 79 20 3d 20 78 3b 0a 20 20 76 6f uble y = x;. vo
b1f30 6c 61 74 69 6c 65 20 64 6f 75 62 6c 65 20 7a 20 latile double z
b1f40 3d 20 79 3b 0a 20 20 72 63 20 3d 20 28 79 21 3d = y;. rc = (y!=
b1f50 7a 29 3b 0a 23 65 6c 73 65 20 20 2f 2a 20 69 66 z);.#else /* if
b1f60 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
b1f70 48 41 56 45 5f 49 53 4e 41 4e 29 20 2a 2f 0a 20 HAVE_ISNAN) */.
b1f80 20 72 63 20 3d 20 69 73 6e 61 6e 28 78 29 3b 0a rc = isnan(x);.
b1f90 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
b1fa0 5f 48 41 56 45 5f 49 53 4e 41 4e 20 2a 2f 0a 20 _HAVE_ISNAN */.
b1fb0 20 74 65 73 74 63 61 73 65 28 20 72 63 20 29 3b testcase( rc );
b1fc0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
b1fd0 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 61 ./*.** Compute a
b1fe0 20 73 74 72 69 6e 67 20 6c 65 6e 67 74 68 20 74 string length t
b1ff0 68 61 74 20 69 73 20 6c 69 6d 69 74 65 64 20 74 hat is limited t
b2000 6f 20 77 68 61 74 20 63 61 6e 20 62 65 20 73 74 o what can be st
b2010 6f 72 65 64 20 69 6e 0a 2a 2a 20 6c 6f 77 65 72 ored in.** lower
b2020 20 33 30 20 62 69 74 73 20 6f 66 20 61 20 33 32 30 bits of a 32
b2030 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 -bit signed inte
b2040 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 ger..**.** The v
b2050 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 77 69 alue returned wi
b2060 6c 6c 20 6e 65 76 65 72 20 62 65 20 6e 65 67 61 ll never be nega
b2070 74 69 76 65 2e 20 20 4e 6f 72 20 77 69 6c 6c 20 tive. Nor will
b2080 69 74 20 65 76 65 72 20 62 65 20 67 72 65 61 74 it ever be great
b2090 65 72 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20 61 er.** than the a
b20a0 63 74 75 61 6c 20 6c 65 6e 67 74 68 20 6f 66 20 ctual length of
b20b0 74 68 65 20 73 74 72 69 6e 67 2e 20 20 46 6f 72 the string. For
b20c0 20 76 65 72 79 20 6c 6f 6e 67 20 73 74 72 69 6e very long strin
b20d0 67 73 20 28 67 72 65 61 74 65 72 0a 2a 2a 20 74 gs (greater.** t
b20e0 68 61 6e 20 31 47 69 42 29 20 74 68 65 20 76 61 han 1GiB) the va
b20f0 6c 75 65 20 72 65 74 75 72 6e 65 64 20 6d 69 67 lue returned mig
b2100 68 74 20 62 65 20 6c 65 73 73 20 74 68 61 6e 20 ht be less than
b2110 74 68 65 20 74 72 75 65 20 73 74 72 69 6e 67 20 the true string
b2120 6c 65 6e 67 74 68 2e 0a 2a 2f 0a 53 51 4c 49 54 length..*/.SQLIT
b2130 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
b2140 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 63 6f lite3Strlen30(co
b2150 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 nst char *z){.
b2160 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 20 3d const char *z2 =
b2170 20 7a 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 z;. if( z==0 )
b2180 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 77 68 69 return 0;. whi
b2190 6c 65 28 20 2a 7a 32 20 29 7b 20 7a 32 2b 2b 3b le( *z2 ){ z2++;
b21a0 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 78 33 66 }. return 0x3f
b21b0 66 66 66 66 66 66 20 26 20 28 69 6e 74 29 28 7a ffffff & (int)(z
b21c0 32 20 2d 20 7a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 2 - z);.}../*.**
b21d0 20 53 65 74 20 74 68 65 20 6d 6f 73 74 20 72 65 Set the most re
b21e0 63 65 6e 74 20 65 72 72 6f 72 20 63 6f 64 65 20 cent error code
b21f0 61 6e 64 20 65 72 72 6f 72 20 73 74 72 69 6e 67 and error string
b2200 20 66 6f 72 20 74 68 65 20 73 71 6c 69 74 65 0a for the sqlite.
b2210 2a 2a 20 68 61 6e 64 6c 65 20 22 64 62 22 2e 20 ** handle "db".
b2220 54 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 The error code i
b2230 73 20 73 65 74 20 74 6f 20 22 65 72 72 5f 63 6f s set to "err_co
b2240 64 65 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 74 de"..**.** If it
b2250 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 73 74 is not NULL, st
b2260 72 69 6e 67 20 7a 46 6f 72 6d 61 74 20 73 70 65 ring zFormat spe
b2270 63 69 66 69 65 73 20 74 68 65 20 66 6f 72 6d 61 cifies the forma
b2280 74 20 6f 66 20 74 68 65 0a 2a 2a 20 65 72 72 6f t of the.** erro
b2290 72 20 73 74 72 69 6e 67 20 69 6e 20 74 68 65 20 r string in the
b22a0 73 74 79 6c 65 20 6f 66 20 74 68 65 20 70 72 69 style of the pri
b22b0 6e 74 66 20 66 75 6e 63 74 69 6f 6e 73 3a 20 54 ntf functions: T
b22c0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 he following.**
b22d0 66 6f 72 6d 61 74 20 63 68 61 72 61 63 74 65 72 format character
b22e0 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 3a 0a 2a s are allowed:.*
b22f0 2a 0a 2a 2a 20 20 20 20 20 20 25 73 20 20 20 20 *.** %s
b2300 20 20 49 6e 73 65 72 74 20 61 20 73 74 72 69 6e Insert a strin
b2310 67 0a 2a 2a 20 20 20 20 20 20 25 7a 20 20 20 20 g.** %z
b2320 20 20 41 20 73 74 72 69 6e 67 20 74 68 61 74 20 A string that
b2330 73 68 6f 75 6c 64 20 62 65 20 66 72 65 65 64 20 should be freed
b2340 61 66 74 65 72 20 75 73 65 0a 2a 2a 20 20 20 20 after use.**
b2350 20 20 25 64 20 20 20 20 20 20 49 6e 73 65 72 74 %d Insert
b2360 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 an integer.**
b2370 20 20 20 20 25 54 20 20 20 20 20 20 49 6e 73 65 %T Inse
b2380 72 74 20 61 20 74 6f 6b 65 6e 0a 2a 2a 20 20 20 rt a token.**
b2390 20 20 20 25 53 20 20 20 20 20 20 49 6e 73 65 72 %S Inser
b23a0 74 20 74 68 65 20 66 69 72 73 74 20 65 6c 65 6d t the first elem
b23b0 65 6e 74 20 6f 66 20 61 20 53 72 63 4c 69 73 74 ent of a SrcList
b23c0 0a 2a 2a 0a 2a 2a 20 7a 46 6f 72 6d 61 74 20 61 .**.** zFormat a
b23d0 6e 64 20 61 6e 79 20 73 74 72 69 6e 67 20 74 6f nd any string to
b23e0 6b 65 6e 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 kens that follow
b23f0 20 69 74 20 61 72 65 20 61 73 73 75 6d 65 64 20 it are assumed
b2400 74 6f 20 62 65 0a 2a 2a 20 65 6e 63 6f 64 65 64 to be.** encoded
b2410 20 69 6e 20 55 54 46 2d 38 2e 0a 2a 2a 0a 2a 2a in UTF-8..**.**
b2420 20 54 6f 20 63 6c 65 61 72 20 74 68 65 20 6d 6f To clear the mo
b2430 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72 20 st recent error
b2440 66 6f 72 20 73 71 6c 69 74 65 20 68 61 6e 64 6c for sqlite handl
b2450 65 20 22 64 62 22 2c 20 73 71 6c 69 74 65 33 45 e "db", sqlite3E
b2460 72 72 6f 72 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 rror.** should b
b2470 65 20 63 61 6c 6c 65 64 20 77 69 74 68 20 65 72 e called with er
b2480 72 5f 63 6f 64 65 20 73 65 74 20 74 6f 20 53 51 r_code set to SQ
b2490 4c 49 54 45 5f 4f 4b 20 61 6e 64 20 7a 46 6f 72 LITE_OK and zFor
b24a0 6d 61 74 20 73 65 74 0a 2a 2a 20 74 6f 20 4e 55 mat set.** to NU
b24b0 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 LL..*/.SQLITE_PR
b24c0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
b24d0 65 33 45 72 72 6f 72 28 73 71 6c 69 74 65 33 20 e3Error(sqlite3
b24e0 2a 64 62 2c 20 69 6e 74 20 65 72 72 5f 63 6f 64 *db, int err_cod
b24f0 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a e, const char *z
b2500 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 Format, ...){.
b2510 69 66 28 20 64 62 20 26 26 20 28 64 62 2d 3e 70 if( db && (db->p
b2520 45 72 72 20 7c 7c 20 28 64 62 2d 3e 70 45 72 72 Err || (db->pErr
b2530 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e = sqlite3ValueN
b2540 65 77 28 64 62 29 29 21 3d 30 29 20 29 7b 0a 20 ew(db))!=0) ){.
b2550 20 20 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d db->errCode =
b2560 20 65 72 72 5f 63 6f 64 65 3b 0a 20 20 20 20 69 err_code;. i
b2570 66 28 20 7a 46 6f 72 6d 61 74 20 29 7b 0a 20 20 f( zFormat ){.
b2580 20 20 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 20 char *z;.
b2590 20 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 va_list ap;.
b25a0 20 20 20 20 20 76 61 5f 73 74 61 72 74 28 61 70 va_start(ap
b25b0 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 20 20 , zFormat);.
b25c0 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 z = sqlite3VMP
b25d0 72 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 rintf(db, zForma
b25e0 74 2c 20 61 70 29 3b 0a 20 20 20 20 20 20 76 61 t, ap);. va
b25f0 5f 65 6e 64 28 61 70 29 3b 0a 20 20 20 20 20 20 _end(ap);.
b2600 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 sqlite3ValueSetS
b2610 74 72 28 64 62 2d 3e 70 45 72 72 2c 20 2d 31 2c tr(db->pErr, -1,
b2620 20 7a 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c z, SQLITE_UTF8,
b2630 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 SQLITE_DYNAMIC)
b2640 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
b2650 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 sqlite3ValueS
b2660 65 74 53 74 72 28 64 62 2d 3e 70 45 72 72 2c 20 etStr(db->pErr,
b2670 30 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55 54 46 0, 0, SQLITE_UTF
b2680 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 8, SQLITE_STATIC
b2690 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a );. }. }.}..
b26a0 2f 2a 0a 2a 2a 20 41 64 64 20 61 6e 20 65 72 72 /*.** Add an err
b26b0 6f 72 20 6d 65 73 73 61 67 65 20 74 6f 20 70 50 or message to pP
b26c0 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 61 6e arse->zErrMsg an
b26d0 64 20 69 6e 63 72 65 6d 65 6e 74 20 70 50 61 72 d increment pPar
b26e0 73 65 2d 3e 6e 45 72 72 2e 0a 2a 2a 20 54 68 65 se->nErr..** The
b26f0 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d 61 following forma
b2700 74 74 69 6e 67 20 63 68 61 72 61 63 74 65 72 73 tting characters
b2710 20 61 72 65 20 61 6c 6c 6f 77 65 64 3a 0a 2a 2a are allowed:.**
b2720 0a 2a 2a 20 20 20 20 20 20 25 73 20 20 20 20 20 .** %s
b2730 20 49 6e 73 65 72 74 20 61 20 73 74 72 69 6e 67 Insert a string
b2740 0a 2a 2a 20 20 20 20 20 20 25 7a 20 20 20 20 20 .** %z
b2750 20 41 20 73 74 72 69 6e 67 20 74 68 61 74 20 73 A string that s
b2760 68 6f 75 6c 64 20 62 65 20 66 72 65 65 64 20 61 hould be freed a
b2770 66 74 65 72 20 75 73 65 0a 2a 2a 20 20 20 20 20 fter use.**
b2780 20 25 64 20 20 20 20 20 20 49 6e 73 65 72 74 20 %d Insert
b2790 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 an integer.**
b27a0 20 20 20 25 54 20 20 20 20 20 20 49 6e 73 65 72 %T Inser
b27b0 74 20 61 20 74 6f 6b 65 6e 0a 2a 2a 20 20 20 20 t a token.**
b27c0 20 20 25 53 20 20 20 20 20 20 49 6e 73 65 72 74 %S Insert
b27d0 20 74 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 the first eleme
b27e0 6e 74 20 6f 66 20 61 20 53 72 63 4c 69 73 74 0a nt of a SrcList.
b27f0 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 **.** This funct
b2800 69 6f 6e 20 73 68 6f 75 6c 64 20 62 65 20 75 73 ion should be us
b2810 65 64 20 74 6f 20 72 65 70 6f 72 74 20 61 6e 79 ed to report any
b2820 20 65 72 72 6f 72 20 74 68 61 74 20 6f 63 63 75 error that occu
b2830 72 73 20 77 68 69 6c 73 74 0a 2a 2a 20 63 6f 6d rs whilst.** com
b2840 70 69 6c 69 6e 67 20 61 6e 20 53 51 4c 20 73 74 piling an SQL st
b2850 61 74 65 6d 65 6e 74 20 28 69 2e 65 2e 20 77 69 atement (i.e. wi
b2860 74 68 69 6e 20 73 71 6c 69 74 65 33 5f 70 72 65 thin sqlite3_pre
b2870 70 61 72 65 28 29 29 2e 20 54 68 65 0a 2a 2a 20 pare()). The.**
b2880 6c 61 73 74 20 74 68 69 6e 67 20 74 68 65 20 73 last thing the s
b2890 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 qlite3_prepare()
b28a0 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 69 function does i
b28b0 73 20 63 6f 70 79 20 74 68 65 20 65 72 72 6f 72 s copy the error
b28c0 0a 2a 2a 20 73 74 6f 72 65 64 20 62 79 20 74 68 .** stored by th
b28d0 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e 74 6f is function into
b28e0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 the database ha
b28f0 6e 64 6c 65 20 75 73 69 6e 67 20 73 71 6c 69 74 ndle using sqlit
b2900 65 33 45 72 72 6f 72 28 29 2e 0a 2a 2a 20 46 75 e3Error()..** Fu
b2910 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65 33 45 72 nction sqlite3Er
b2920 72 6f 72 28 29 20 73 68 6f 75 6c 64 20 62 65 20 ror() should be
b2930 75 73 65 64 20 64 75 72 69 6e 67 20 73 74 61 74 used during stat
b2940 65 6d 65 6e 74 20 65 78 65 63 75 74 69 6f 6e 0a ement execution.
b2950 2a 2a 20 28 73 71 6c 69 74 65 33 5f 73 74 65 70 ** (sqlite3_step
b2960 28 29 20 65 74 63 2e 29 2e 0a 2a 2f 0a 53 51 4c () etc.)..*/.SQL
b2970 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
b2980 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
b2990 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
b29a0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 const char *zFor
b29b0 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f mat, ...){. va_
b29c0 6c 69 73 74 20 61 70 3b 0a 20 20 73 71 6c 69 74 list ap;. sqlit
b29d0 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d e3 *db = pParse-
b29e0 3e 64 62 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e >db;. pParse->n
b29f0 45 72 72 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33 Err++;. sqlite3
b2a00 44 62 46 72 65 65 28 64 62 2c 20 70 50 61 72 73 DbFree(db, pPars
b2a10 65 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 76 e->zErrMsg);. v
b2a20 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 a_start(ap, zFor
b2a30 6d 61 74 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e mat);. pParse->
b2a40 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 zErrMsg = sqlite
b2a50 33 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a 46 3VMPrintf(db, zF
b2a60 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 ormat, ap);. va
b2a70 5f 65 6e 64 28 61 70 29 3b 0a 20 20 70 50 61 72 _end(ap);. pPar
b2a80 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f se->rc = SQLITE_
b2a90 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ERROR;.}../*.**
b2aa0 43 6c 65 61 72 20 74 68 65 20 65 72 72 6f 72 20 Clear the error
b2ab0 6d 65 73 73 61 67 65 20 69 6e 20 70 50 61 72 73 message in pPars
b2ac0 65 2c 20 69 66 20 61 6e 79 0a 2a 2f 0a 53 51 4c e, if any.*/.SQL
b2ad0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
b2ae0 20 73 71 6c 69 74 65 33 45 72 72 6f 72 43 6c 65 sqlite3ErrorCle
b2af0 61 72 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 ar(Parse *pParse
b2b00 29 7b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 ){. sqlite3DbFr
b2b10 65 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 ee(pParse->db, p
b2b20 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 29 3b Parse->zErrMsg);
b2b30 0a 20 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d . pParse->zErrM
b2b40 73 67 20 3d 20 30 3b 0a 20 20 70 50 61 72 73 65 sg = 0;. pParse
b2b50 2d 3e 6e 45 72 72 20 3d 20 30 3b 0a 7d 0a 0a 2f ->nErr = 0;.}../
b2b60 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 6e 20 *.** Convert an
b2b70 53 51 4c 2d 73 74 79 6c 65 20 71 75 6f 74 65 64 SQL-style quoted
b2b80 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20 6e string into a n
b2b90 6f 72 6d 61 6c 20 73 74 72 69 6e 67 20 62 79 20 ormal string by
b2ba0 72 65 6d 6f 76 69 6e 67 0a 2a 2a 20 74 68 65 20 removing.** the
b2bb0 71 75 6f 74 65 20 63 68 61 72 61 63 74 65 72 73 quote characters
b2bc0 2e 20 20 54 68 65 20 63 6f 6e 76 65 72 73 69 6f . The conversio
b2bd0 6e 20 69 73 20 64 6f 6e 65 20 69 6e 2d 70 6c 61 n is done in-pla
b2be0 63 65 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 69 ce. If the.** i
b2bf0 6e 70 75 74 20 64 6f 65 73 20 6e 6f 74 20 62 65 nput does not be
b2c00 67 69 6e 20 77 69 74 68 20 61 20 71 75 6f 74 65 gin with a quote
b2c10 20 63 68 61 72 61 63 74 65 72 2c 20 74 68 65 6e character, then
b2c20 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a this routine.**
b2c30 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a is a no-op..**.
b2c40 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 73 74 72 ** The input str
b2c50 69 6e 67 20 6d 75 73 74 20 62 65 20 7a 65 72 6f ing must be zero
b2c60 2d 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 41 20 -terminated. A
b2c70 6e 65 77 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 new zero-termina
b2c80 74 6f 72 0a 2a 2a 20 69 73 20 61 64 64 65 64 20 tor.** is added
b2c90 74 6f 20 74 68 65 20 64 65 71 75 6f 74 65 64 20 to the dequoted
b2ca0 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 string..**.** Th
b2cb0 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 e return value i
b2cc0 73 20 2d 31 20 69 66 20 6e 6f 20 64 65 71 75 6f s -1 if no dequo
b2cd0 74 69 6e 67 20 6f 63 63 75 72 73 20 6f 72 20 74 ting occurs or t
b2ce0 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 he length of the
b2cf0 0a 2a 2a 20 64 65 71 75 6f 74 65 64 20 73 74 72 .** dequoted str
b2d00 69 6e 67 2c 20 65 78 63 6c 75 73 69 76 65 20 6f ing, exclusive o
b2d10 66 20 74 68 65 20 7a 65 72 6f 20 74 65 72 6d 69 f the zero termi
b2d20 6e 61 74 6f 72 2c 20 69 66 20 64 65 71 75 6f 74 nator, if dequot
b2d30 69 6e 67 20 64 6f 65 73 0a 2a 2a 20 6f 63 63 75 ing does.** occu
b2d40 72 2e 0a 2a 2a 0a 2a 2a 20 32 30 30 32 2d 46 65 r..**.** 2002-Fe
b2d50 62 2d 31 34 3a 20 54 68 69 73 20 72 6f 75 74 69 b-14: This routi
b2d60 6e 65 20 69 73 20 65 78 74 65 6e 64 65 64 20 74 ne is extended t
b2d70 6f 20 72 65 6d 6f 76 65 20 4d 53 2d 41 63 63 65 o remove MS-Acce
b2d80 73 73 20 73 74 79 6c 65 0a 2a 2a 20 62 72 61 63 ss style.** brac
b2d90 6b 65 74 73 20 66 72 6f 6d 20 61 72 6f 75 6e 64 kets from around
b2da0 20 69 64 65 6e 74 69 66 65 72 73 2e 20 20 46 6f identifers. Fo
b2db0 72 20 65 78 61 6d 70 6c 65 3a 20 20 22 5b 61 2d r example: "[a-
b2dc0 62 2d 63 5d 22 20 62 65 63 6f 6d 65 73 0a 2a 2a b-c]" becomes.**
b2dd0 20 22 61 2d 62 2d 63 22 2e 0a 2a 2f 0a 53 51 4c "a-b-c"..*/.SQL
b2de0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
b2df0 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28 63 sqlite3Dequote(c
b2e00 68 61 72 20 2a 7a 29 7b 0a 20 20 63 68 61 72 20 har *z){. char
b2e10 71 75 6f 74 65 3b 0a 20 20 69 6e 74 20 69 2c 20 quote;. int i,
b2e20 6a 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 j;. if( z==0 )
b2e30 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 71 75 6f return -1;. quo
b2e40 74 65 20 3d 20 7a 5b 30 5d 3b 0a 20 20 73 77 69 te = z[0];. swi
b2e50 74 63 68 28 20 71 75 6f 74 65 20 29 7b 0a 20 20 tch( quote ){.
b2e60 20 20 63 61 73 65 20 27 5c 27 27 3a 20 20 62 72 case '\'': br
b2e70 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 27 22 eak;. case '"
b2e80 27 3a 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 ': break;.
b2e90 63 61 73 65 20 27 60 27 3a 20 20 20 62 72 65 61 case '`': brea
b2ea0 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 k;
b2eb0 20 20 2f 2a 20 46 6f 72 20 4d 79 53 51 4c 20 63 /* For MySQL c
b2ec0 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 2a 2f 0a ompatibility */.
b2ed0 20 20 20 20 63 61 73 65 20 27 5b 27 3a 20 20 20 case '[':
b2ee0 71 75 6f 74 65 20 3d 20 27 5d 27 3b 20 20 62 72 quote = ']'; br
b2ef0 65 61 6b 3b 20 20 2f 2a 20 46 6f 72 20 4d 53 20 eak; /* For MS
b2f00 53 71 6c 53 65 72 76 65 72 20 63 6f 6d 70 61 74 SqlServer compat
b2f10 69 62 69 6c 69 74 79 20 2a 2f 0a 20 20 20 20 64 ibility */. d
b2f20 65 66 61 75 6c 74 3a 20 20 20 20 72 65 74 75 72 efault: retur
b2f30 6e 20 2d 31 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 n -1;. }. for(
b2f40 69 3d 31 2c 20 6a 3d 30 3b 20 41 4c 57 41 59 53 i=1, j=0; ALWAYS
b2f50 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 (z[i]); i++){.
b2f60 20 20 69 66 28 20 7a 5b 69 5d 3d 3d 71 75 6f 74 if( z[i]==quot
b2f70 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 7a e ){. if( z
b2f80 5b 69 2b 31 5d 3d 3d 71 75 6f 74 65 20 29 7b 0a [i+1]==quote ){.
b2f90 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d z[j++] =
b2fa0 20 71 75 6f 74 65 3b 0a 20 20 20 20 20 20 20 20 quote;.
b2fb0 69 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 i++;. }else
b2fc0 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b {. break;
b2fd0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c . }. }el
b2fe0 73 65 7b 0a 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d se{. z[j++]
b2ff0 20 3d 20 7a 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 = z[i];. }.
b3000 20 7d 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 }. z[j] = 0;.
b3010 20 72 65 74 75 72 6e 20 6a 3b 0a 7d 0a 0a 2f 2a return j;.}../*
b3020 20 43 6f 6e 76 65 6e 69 65 6e 74 20 73 68 6f 72 Convenient shor
b3030 74 2d 68 61 6e 64 20 2a 2f 0a 23 64 65 66 69 6e t-hand */.#defin
b3040 65 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 20 73 e UpperToLower s
b3050 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 qlite3UpperToLow
b3060 65 72 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 73 er../*.** Some s
b3070 79 73 74 65 6d 73 20 68 61 76 65 20 73 74 72 69 ystems have stri
b3080 63 6d 70 28 29 2e 20 20 4f 74 68 65 72 73 20 68 cmp(). Others h
b3090 61 76 65 20 73 74 72 63 61 73 65 63 6d 70 28 29 ave strcasecmp()
b30a0 2e 20 20 42 65 63 61 75 73 65 0a 2a 2a 20 74 68 . Because.** th
b30b0 65 72 65 20 69 73 20 6e 6f 20 63 6f 6e 73 69 73 ere is no consis
b30c0 74 65 6e 63 79 2c 20 77 65 20 77 69 6c 6c 20 64 tency, we will d
b30d0 65 66 69 6e 65 20 6f 75 72 20 6f 77 6e 2e 0a 2a efine our own..*
b30e0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
b30f0 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 49 int sqlite3StrI
b3100 43 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a Cmp(const char *
b3110 7a 4c 65 66 74 2c 20 63 6f 6e 73 74 20 63 68 61 zLeft, const cha
b3120 72 20 2a 7a 52 69 67 68 74 29 7b 0a 20 20 72 65 r *zRight){. re
b3130 67 69 73 74 65 72 20 75 6e 73 69 67 6e 65 64 20 gister unsigned
b3140 63 68 61 72 20 2a 61 2c 20 2a 62 3b 0a 20 20 61 char *a, *b;. a
b3150 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 = (unsigned cha
b3160 72 20 2a 29 7a 4c 65 66 74 3b 0a 20 20 62 20 3d r *)zLeft;. b =
b3170 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 (unsigned char
b3180 2a 29 7a 52 69 67 68 74 3b 0a 20 20 77 68 69 6c *)zRight;. whil
b3190 65 28 20 2a 61 21 3d 30 20 26 26 20 55 70 70 65 e( *a!=0 && Uppe
b31a0 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d 3d 3d 55 70 rToLower[*a]==Up
b31b0 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d 29 7b perToLower[*b]){
b31c0 20 61 2b 2b 3b 20 62 2b 2b 3b 20 7d 0a 20 20 72 a++; b++; }. r
b31d0 65 74 75 72 6e 20 55 70 70 65 72 54 6f 4c 6f 77 eturn UpperToLow
b31e0 65 72 5b 2a 61 5d 20 2d 20 55 70 70 65 72 54 6f er[*a] - UpperTo
b31f0 4c 6f 77 65 72 5b 2a 62 5d 3b 0a 7d 0a 53 51 4c Lower[*b];.}.SQL
b3200 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
b3210 74 65 33 5f 73 74 72 6e 69 63 6d 70 28 63 6f 6e te3_strnicmp(con
b3220 73 74 20 63 68 61 72 20 2a 7a 4c 65 66 74 2c 20 st char *zLeft,
b3230 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 69 67 const char *zRig
b3240 68 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 ht, int N){. re
b3250 67 69 73 74 65 72 20 75 6e 73 69 67 6e 65 64 20 gister unsigned
b3260 63 68 61 72 20 2a 61 2c 20 2a 62 3b 0a 20 20 61 char *a, *b;. a
b3270 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 = (unsigned cha
b3280 72 20 2a 29 7a 4c 65 66 74 3b 0a 20 20 62 20 3d r *)zLeft;. b =
b3290 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 (unsigned char
b32a0 2a 29 7a 52 69 67 68 74 3b 0a 20 20 77 68 69 6c *)zRight;. whil
b32b0 65 28 20 4e 2d 2d 20 3e 20 30 20 26 26 20 2a 61 e( N-- > 0 && *a
b32c0 21 3d 30 20 26 26 20 55 70 70 65 72 54 6f 4c 6f !=0 && UpperToLo
b32d0 77 65 72 5b 2a 61 5d 3d 3d 55 70 70 65 72 54 6f wer[*a]==UpperTo
b32e0 4c 6f 77 65 72 5b 2a 62 5d 29 7b 20 61 2b 2b 3b Lower[*b]){ a++;
b32f0 20 62 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e b++; }. return
b3300 20 4e 3c 30 20 3f 20 30 20 3a 20 55 70 70 65 72 N<0 ? 0 : Upper
b3310 54 6f 4c 6f 77 65 72 5b 2a 61 5d 20 2d 20 55 70 ToLower[*a] - Up
b3320 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d 3b 0a perToLower[*b];.
b3330 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
b3340 54 52 55 45 20 69 66 20 7a 20 69 73 20 61 20 70 TRUE if z is a p
b3350 75 72 65 20 6e 75 6d 65 72 69 63 20 73 74 72 69 ure numeric stri
b3360 6e 67 2e 20 20 52 65 74 75 72 6e 20 46 41 4c 53 ng. Return FALS
b3370 45 20 61 6e 64 20 6c 65 61 76 65 0a 2a 2a 20 2a E and leave.** *
b3380 72 65 61 6c 6e 75 6d 20 75 6e 63 68 61 6e 67 65 realnum unchange
b3390 64 20 69 66 20 74 68 65 20 73 74 72 69 6e 67 20 d if the string
b33a0 63 6f 6e 74 61 69 6e 73 20 61 6e 79 20 63 68 61 contains any cha
b33b0 72 61 63 74 65 72 20 77 68 69 63 68 20 69 73 20 racter which is
b33c0 6e 6f 74 0a 2a 2a 20 70 61 72 74 20 6f 66 20 61 not.** part of a
b33d0 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 number..**.** I
b33e0 66 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 20 f the string is
b33f0 70 75 72 65 20 6e 75 6d 65 72 69 63 2c 20 73 65 pure numeric, se
b3400 74 20 2a 72 65 61 6c 6e 75 6d 20 74 6f 20 54 52 t *realnum to TR
b3410 55 45 20 69 66 20 74 68 65 20 73 74 72 69 6e 67 UE if the string
b3420 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 .** contains the
b3430 20 27 2e 27 20 63 68 61 72 61 63 74 65 72 20 6f '.' character o
b3440 72 20 61 6e 20 22 45 2b 30 30 30 22 20 73 74 79 r an "E+000" sty
b3450 6c 65 20 65 78 70 6f 6e 65 6e 74 69 61 74 69 6f le exponentiatio
b3460 6e 20 73 75 66 66 69 78 2e 0a 2a 2a 20 4f 74 68 n suffix..** Oth
b3470 65 72 77 69 73 65 20 73 65 74 20 2a 72 65 61 6c erwise set *real
b3480 6e 75 6d 20 74 6f 20 46 41 4c 53 45 2e 20 20 4e num to FALSE. N
b3490 6f 74 65 20 74 68 61 74 20 6a 75 73 74 20 62 65 ote that just be
b34a0 63 61 75 65 20 2a 72 65 61 6c 6e 75 6d 20 69 73 caue *realnum is
b34b0 0a 2a 2a 20 66 61 6c 73 65 20 64 6f 65 73 20 6e .** false does n
b34c0 6f 74 20 6d 65 61 6e 20 74 68 61 74 20 74 68 65 ot mean that the
b34d0 20 6e 75 6d 62 65 72 20 63 61 6e 20 62 65 20 73 number can be s
b34e0 75 63 63 65 73 73 66 75 6c 6c 79 20 63 6f 6e 76 uccessfully conv
b34f0 65 72 74 65 64 20 69 6e 74 6f 0a 2a 2a 20 61 6e erted into.** an
b3500 20 69 6e 74 65 67 65 72 20 2d 20 69 74 20 6d 69 integer - it mi
b3510 67 68 74 20 62 65 20 74 6f 6f 20 62 69 67 2e 0a ght be too big..
b3520 2a 2a 0a 2a 2a 20 41 6e 20 65 6d 70 74 79 20 73 **.** An empty s
b3530 74 72 69 6e 67 20 69 73 20 63 6f 6e 73 69 64 65 tring is conside
b3540 72 65 64 20 6e 6f 6e 2d 6e 75 6d 65 72 69 63 2e red non-numeric.
b3550 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
b3560 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 TE int sqlite3Is
b3570 4e 75 6d 62 65 72 28 63 6f 6e 73 74 20 63 68 61 Number(const cha
b3580 72 20 2a 7a 2c 20 69 6e 74 20 2a 72 65 61 6c 6e r *z, int *realn
b3590 75 6d 2c 20 75 38 20 65 6e 63 29 7b 0a 20 20 69 um, u8 enc){. i
b35a0 6e 74 20 69 6e 63 72 20 3d 20 28 65 6e 63 3d 3d nt incr = (enc==
b35b0 53 51 4c 49 54 45 5f 55 54 46 38 3f 31 3a 32 29 SQLITE_UTF8?1:2)
b35c0 3b 0a 20 20 69 66 28 20 65 6e 63 3d 3d 53 51 4c ;. if( enc==SQL
b35d0 49 54 45 5f 55 54 46 31 36 42 45 20 29 20 7a 2b ITE_UTF16BE ) z+
b35e0 2b 3b 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 2d 27 +;. if( *z=='-'
b35f0 20 7c 7c 20 2a 7a 3d 3d 27 2b 27 20 29 20 7a 20 || *z=='+' ) z
b3600 2b 3d 20 69 6e 63 72 3b 0a 20 20 69 66 28 20 21 += incr;. if( !
b3610 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a sqlite3Isdigit(*
b3620 7a 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e z) ){. return
b3630 20 30 3b 0a 20 20 7d 0a 20 20 7a 20 2b 3d 20 69 0;. }. z += i
b3640 6e 63 72 3b 0a 20 20 2a 72 65 61 6c 6e 75 6d 20 ncr;. *realnum
b3650 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 73 71 = 0;. while( sq
b3660 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29 lite3Isdigit(*z)
b3670 20 29 7b 20 7a 20 2b 3d 20 69 6e 63 72 3b 20 7d ){ z += incr; }
b3680 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 2e 27 20 29 . if( *z=='.' )
b3690 7b 0a 20 20 20 20 7a 20 2b 3d 20 69 6e 63 72 3b {. z += incr;
b36a0 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 . if( !sqlite
b36b0 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 20 72 3Isdigit(*z) ) r
b36c0 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 77 68 69 eturn 0;. whi
b36d0 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 69 67 le( sqlite3Isdig
b36e0 69 74 28 2a 7a 29 20 29 7b 20 7a 20 2b 3d 20 69 it(*z) ){ z += i
b36f0 6e 63 72 3b 20 7d 0a 20 20 20 20 2a 72 65 61 6c ncr; }. *real
b3700 6e 75 6d 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 num = 1;. }. i
b3710 66 28 20 2a 7a 3d 3d 27 65 27 20 7c 7c 20 2a 7a f( *z=='e' || *z
b3720 3d 3d 27 45 27 20 29 7b 0a 20 20 20 20 7a 20 2b =='E' ){. z +
b3730 3d 20 69 6e 63 72 3b 0a 20 20 20 20 69 66 28 20 = incr;. if(
b3740 2a 7a 3d 3d 27 2b 27 20 7c 7c 20 2a 7a 3d 3d 27 *z=='+' || *z=='
b3750 2d 27 20 29 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a -' ) z += incr;.
b3760 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 if( !sqlite3
b3770 49 73 64 69 67 69 74 28 2a 7a 29 20 29 20 72 65 Isdigit(*z) ) re
b3780 74 75 72 6e 20 30 3b 0a 20 20 20 20 77 68 69 6c turn 0;. whil
b3790 65 28 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 e( sqlite3Isdigi
b37a0 74 28 2a 7a 29 20 29 7b 20 7a 20 2b 3d 20 69 6e t(*z) ){ z += in
b37b0 63 72 3b 20 7d 0a 20 20 20 20 2a 72 65 61 6c 6e cr; }. *realn
b37c0 75 6d 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 um = 1;. }. re
b37d0 74 75 72 6e 20 2a 7a 3d 3d 30 3b 0a 7d 0a 0a 2f turn *z==0;.}../
b37e0 2a 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 20 *.** The string
b37f0 7a 5b 5d 20 69 73 20 61 6e 20 41 53 43 49 49 20 z[] is an ASCII
b3800 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f representation o
b3810 66 20 61 20 72 65 61 6c 20 6e 75 6d 62 65 72 2e f a real number.
b3820 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 69 73 .** Convert this
b3830 20 73 74 72 69 6e 67 20 74 6f 20 61 20 64 6f 75 string to a dou
b3840 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ble..**.** This
b3850 72 6f 75 74 69 6e 65 20 61 73 73 75 6d 65 73 20 routine assumes
b3860 74 68 61 74 20 7a 5b 5d 20 72 65 61 6c 6c 79 20 that z[] really
b3870 69 73 20 61 20 76 61 6c 69 64 20 6e 75 6d 62 65 is a valid numbe
b3880 72 2e 20 20 49 66 20 69 74 0a 2a 2a 20 69 73 20 r. If it.** is
b3890 6e 6f 74 2c 20 74 68 65 20 72 65 73 75 6c 74 20 not, the result
b38a0 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a is undefined..**
b38b0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
b38c0 20 69 73 20 75 73 65 64 20 69 6e 73 74 65 61 64 is used instead
b38d0 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 of the library
b38e0 61 74 6f 66 28 29 20 66 75 6e 63 74 69 6f 6e 20 atof() function
b38f0 62 65 63 61 75 73 65 0a 2a 2a 20 74 68 65 20 6c because.** the l
b3900 69 62 72 61 72 79 20 61 74 6f 66 28 29 20 6d 69 ibrary atof() mi
b3910 67 68 74 20 77 61 6e 74 20 74 6f 20 75 73 65 20 ght want to use
b3920 22 2c 22 20 61 73 20 74 68 65 20 64 65 63 69 6d "," as the decim
b3930 61 6c 20 70 6f 69 6e 74 20 69 6e 73 74 65 61 64 al point instead
b3940 0a 2a 2a 20 6f 66 20 22 2e 22 20 64 65 70 65 6e .** of "." depen
b3950 64 69 6e 67 20 6f 6e 20 68 6f 77 20 6c 6f 63 61 ding on how loca
b3960 6c 65 20 69 73 20 73 65 74 2e 20 20 42 75 74 20 le is set. But
b3970 74 68 61 74 20 77 6f 75 6c 64 20 63 61 75 73 65 that would cause
b3980 20 70 72 6f 62 6c 65 6d 73 0a 2a 2a 20 66 6f 72 problems.** for
b3990 20 53 51 4c 2e 20 20 53 6f 20 74 68 69 73 20 72 SQL. So this r
b39a0 6f 75 74 69 6e 65 20 61 6c 77 61 79 73 20 75 73 outine always us
b39b0 65 73 20 22 2e 22 20 72 65 67 61 72 64 6c 65 73 es "." regardles
b39c0 73 20 6f 66 20 6c 6f 63 61 6c 65 2e 0a 2a 2f 0a s of locale..*/.
b39d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
b39e0 6e 74 20 73 71 6c 69 74 65 33 41 74 6f 46 28 63 nt sqlite3AtoF(c
b39f0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 64 6f onst char *z, do
b3a00 75 62 6c 65 20 2a 70 52 65 73 75 6c 74 29 7b 0a uble *pResult){.
b3a10 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
b3a20 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 MIT_FLOATING_POI
b3a30 4e 54 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 NT. const char
b3a40 2a 7a 42 65 67 69 6e 20 3d 20 7a 3b 0a 20 20 2f *zBegin = z;. /
b3a50 2a 20 73 69 67 6e 20 2a 20 73 69 67 6e 69 66 69 * sign * signifi
b3a60 63 61 6e 64 20 2a 20 28 31 30 20 5e 20 28 65 73 cand * (10 ^ (es
b3a70 69 67 6e 20 2a 20 65 78 70 6f 6e 65 6e 74 29 29 ign * exponent))
b3a80 20 2a 2f 0a 20 20 69 6e 74 20 73 69 67 6e 20 3d */. int sign =
b3a90 20 31 3b 20 20 20 2f 2a 20 73 69 67 6e 20 6f 66 1; /* sign of
b3aa0 20 73 69 67 6e 69 66 69 63 61 6e 64 20 2a 2f 0a significand */.
b3ab0 20 20 69 36 34 20 73 20 3d 20 30 3b 20 20 20 20 i64 s = 0;
b3ac0 20 20 2f 2a 20 73 69 67 6e 69 66 69 63 61 6e 64 /* significand
b3ad0 20 2a 2f 0a 20 20 69 6e 74 20 64 20 3d 20 30 3b */. int d = 0;
b3ae0 20 20 20 20 20 20 2f 2a 20 61 64 6a 75 73 74 20 /* adjust
b3af0 65 78 70 6f 6e 65 6e 74 20 66 6f 72 20 73 68 69 exponent for shi
b3b00 66 74 69 6e 67 20 64 65 63 69 6d 61 6c 20 70 6f fting decimal po
b3b10 69 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 65 73 69 int */. int esi
b3b20 67 6e 20 3d 20 31 3b 20 20 2f 2a 20 73 69 67 6e gn = 1; /* sign
b3b30 20 6f 66 20 65 78 70 6f 6e 65 6e 74 20 2a 2f 0a of exponent */.
b3b40 20 20 69 6e 74 20 65 20 3d 20 30 3b 20 20 20 20 int e = 0;
b3b50 20 20 2f 2a 20 65 78 70 6f 6e 65 6e 74 20 2a 2f /* exponent */
b3b60 0a 20 20 64 6f 75 62 6c 65 20 72 65 73 75 6c 74 . double result
b3b70 3b 0a 20 20 69 6e 74 20 6e 44 69 67 69 74 73 20 ;. int nDigits
b3b80 3d 20 30 3b 0a 0a 20 20 2f 2a 20 73 6b 69 70 20 = 0;.. /* skip
b3b90 6c 65 61 64 69 6e 67 20 73 70 61 63 65 73 20 2a leading spaces *
b3ba0 2f 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 /. while( sqlit
b3bb0 65 33 49 73 73 70 61 63 65 28 2a 7a 29 20 29 20 e3Isspace(*z) )
b3bc0 7a 2b 2b 3b 0a 20 20 2f 2a 20 67 65 74 20 73 69 z++;. /* get si
b3bd0 67 6e 20 6f 66 20 73 69 67 6e 69 66 69 63 61 6e gn of significan
b3be0 64 20 2a 2f 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 d */. if( *z=='
b3bf0 2d 27 20 29 7b 0a 20 20 20 20 73 69 67 6e 20 3d -' ){. sign =
b3c00 20 2d 31 3b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 -1;. z++;.
b3c10 7d 65 6c 73 65 20 69 66 28 20 2a 7a 3d 3d 27 2b }else if( *z=='+
b3c20 27 20 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 ' ){. z++;.
b3c30 7d 0a 20 20 2f 2a 20 73 6b 69 70 20 6c 65 61 64 }. /* skip lead
b3c40 69 6e 67 20 7a 65 72 6f 65 73 20 2a 2f 0a 20 20 ing zeroes */.
b3c50 77 68 69 6c 65 28 20 7a 5b 30 5d 3d 3d 27 30 27 while( z[0]=='0'
b3c60 20 29 20 7a 2b 2b 2c 20 6e 44 69 67 69 74 73 2b ) z++, nDigits+
b3c70 2b 3b 0a 0a 20 20 2f 2a 20 63 6f 70 79 20 6d 61 +;.. /* copy ma
b3c80 78 20 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69 x significant di
b3c90 67 69 74 73 20 74 6f 20 73 69 67 6e 69 66 69 63 gits to signific
b3ca0 61 6e 64 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 and */. while(
b3cb0 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a sqlite3Isdigit(*
b3cc0 7a 29 20 26 26 20 73 3c 28 28 4c 41 52 47 45 53 z) && s<((LARGES
b3cd0 54 5f 49 4e 54 36 34 2d 39 29 2f 31 30 29 20 29 T_INT64-9)/10) )
b3ce0 7b 0a 20 20 20 20 73 20 3d 20 73 2a 31 30 20 2b {. s = s*10 +
b3cf0 20 28 2a 7a 20 2d 20 27 30 27 29 3b 0a 20 20 20 (*z - '0');.
b3d00 20 7a 2b 2b 2c 20 6e 44 69 67 69 74 73 2b 2b 3b z++, nDigits++;
b3d10 0a 20 20 7d 0a 20 20 2f 2a 20 73 6b 69 70 20 6e . }. /* skip n
b3d20 6f 6e 2d 73 69 67 6e 69 66 69 63 61 6e 74 20 73 on-significant s
b3d30 69 67 6e 69 66 69 63 61 6e 64 20 64 69 67 69 74 ignificand digit
b3d40 73 0a 20 20 2a 2a 20 28 69 6e 63 72 65 61 73 65 s. ** (increase
b3d50 20 65 78 70 6f 6e 65 6e 74 20 62 79 20 64 20 74 exponent by d t
b3d60 6f 20 73 68 69 66 74 20 64 65 63 69 6d 61 6c 20 o shift decimal
b3d70 6c 65 66 74 29 20 2a 2f 0a 20 20 77 68 69 6c 65 left) */. while
b3d80 28 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 ( sqlite3Isdigit
b3d90 28 2a 7a 29 20 29 20 7a 2b 2b 2c 20 6e 44 69 67 (*z) ) z++, nDig
b3da0 69 74 73 2b 2b 2c 20 64 2b 2b 3b 0a 0a 20 20 2f its++, d++;.. /
b3db0 2a 20 69 66 20 64 65 63 69 6d 61 6c 20 70 6f 69 * if decimal poi
b3dc0 6e 74 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f nt is present */
b3dd0 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 2e 27 20 29 . if( *z=='.' )
b3de0 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 2f {. z++;. /
b3df0 2a 20 63 6f 70 79 20 64 69 67 69 74 73 20 66 72 * copy digits fr
b3e00 6f 6d 20 61 66 74 65 72 20 64 65 63 69 6d 61 6c om after decimal
b3e10 20 74 6f 20 73 69 67 6e 69 66 69 63 61 6e 64 0a to significand.
b3e20 20 20 20 20 2a 2a 20 28 64 65 63 72 65 61 73 65 ** (decrease
b3e30 20 65 78 70 6f 6e 65 6e 74 20 62 79 20 64 20 74 exponent by d t
b3e40 6f 20 73 68 69 66 74 20 64 65 63 69 6d 61 6c 20 o shift decimal
b3e50 72 69 67 68 74 29 20 2a 2f 0a 20 20 20 20 77 68 right) */. wh
b3e60 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 69 ile( sqlite3Isdi
b3e70 67 69 74 28 2a 7a 29 20 26 26 20 73 3c 28 28 4c git(*z) && s<((L
b3e80 41 52 47 45 53 54 5f 49 4e 54 36 34 2d 39 29 2f ARGEST_INT64-9)/
b3e90 31 30 29 20 29 7b 0a 20 20 20 20 20 20 73 20 3d 10) ){. s =
b3ea0 20 73 2a 31 30 20 2b 20 28 2a 7a 20 2d 20 27 30 s*10 + (*z - '0
b3eb0 27 29 3b 0a 20 20 20 20 20 20 7a 2b 2b 2c 20 6e ');. z++, n
b3ec0 44 69 67 69 74 73 2b 2b 2c 20 64 2d 2d 3b 0a 20 Digits++, d--;.
b3ed0 20 20 20 7d 0a 20 20 20 20 2f 2a 20 73 6b 69 70 }. /* skip
b3ee0 20 6e 6f 6e 2d 73 69 67 6e 69 66 69 63 61 6e 74 non-significant
b3ef0 20 64 69 67 69 74 73 20 2a 2f 0a 20 20 20 20 77 digits */. w
b3f00 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 hile( sqlite3Isd
b3f10 69 67 69 74 28 2a 7a 29 20 29 20 7a 2b 2b 2c 20 igit(*z) ) z++,
b3f20 6e 44 69 67 69 74 73 2b 2b 3b 0a 20 20 7d 0a 0a nDigits++;. }..
b3f30 20 20 2f 2a 20 69 66 20 65 78 70 6f 6e 65 6e 74 /* if exponent
b3f40 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 is present */.
b3f50 20 69 66 28 20 2a 7a 3d 3d 27 65 27 20 7c 7c 20 if( *z=='e' ||
b3f60 2a 7a 3d 3d 27 45 27 20 29 7b 0a 20 20 20 20 7a *z=='E' ){. z
b3f70 2b 2b 3b 0a 20 20 20 20 2f 2a 20 67 65 74 20 73 ++;. /* get s
b3f80 69 67 6e 20 6f 66 20 65 78 70 6f 6e 65 6e 74 20 ign of exponent
b3f90 2a 2f 0a 20 20 20 20 69 66 28 20 2a 7a 3d 3d 27 */. if( *z=='
b3fa0 2d 27 20 29 7b 0a 20 20 20 20 20 20 65 73 69 67 -' ){. esig
b3fb0 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7a 2b n = -1;. z+
b3fc0 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 +;. }else if(
b3fd0 20 2a 7a 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 *z=='+' ){.
b3fe0 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 z++;. }.
b3ff0 20 2f 2a 20 63 6f 70 79 20 64 69 67 69 74 73 20 /* copy digits
b4000 74 6f 20 65 78 70 6f 6e 65 6e 74 20 2a 2f 0a 20 to exponent */.
b4010 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 while( sqlite
b4020 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b 0a 3Isdigit(*z) ){.
b4030 20 20 20 20 20 20 65 20 3d 20 65 2a 31 30 20 2b e = e*10 +
b4040 20 28 2a 7a 20 2d 20 27 30 27 29 3b 0a 20 20 20 (*z - '0');.
b4050 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 z++;. }.
b4060 7d 0a 0a 20 20 2f 2a 20 61 64 6a 75 73 74 20 65 }.. /* adjust e
b4070 78 70 6f 6e 65 6e 74 20 62 79 20 64 2c 20 61 6e xponent by d, an
b4080 64 20 75 70 64 61 74 65 20 73 69 67 6e 20 2a 2f d update sign */
b4090 0a 20 20 65 20 3d 20 28 65 2a 65 73 69 67 6e 29 . e = (e*esign)
b40a0 20 2b 20 64 3b 0a 20 20 69 66 28 20 65 3c 30 20 + d;. if( e<0
b40b0 29 20 7b 0a 20 20 20 20 65 73 69 67 6e 20 3d 20 ) {. esign =
b40c0 2d 31 3b 0a 20 20 20 20 65 20 2a 3d 20 2d 31 3b -1;. e *= -1;
b40d0 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 . } else {.
b40e0 65 73 69 67 6e 20 3d 20 31 3b 0a 20 20 7d 0a 0a esign = 1;. }..
b40f0 20 20 2f 2a 20 69 66 20 30 20 73 69 67 6e 69 66 /* if 0 signif
b4100 69 63 61 6e 64 20 2a 2f 0a 20 20 69 66 28 20 21 icand */. if( !
b4110 73 20 29 20 7b 0a 20 20 20 20 2f 2a 20 49 6e 20 s ) {. /* In
b4120 74 68 65 20 49 45 45 45 20 37 35 34 20 73 74 61 the IEEE 754 sta
b4130 6e 64 61 72 64 2c 20 7a 65 72 6f 20 69 73 20 73 ndard, zero is s
b4140 69 67 6e 65 64 2e 0a 20 20 20 20 2a 2a 20 41 64 igned.. ** Ad
b4150 64 20 74 68 65 20 73 69 67 6e 20 69 66 20 77 65 d the sign if we
b4160 27 76 65 20 73 65 65 6e 20 61 74 20 6c 65 61 73 've seen at leas
b4170 74 20 6f 6e 65 20 64 69 67 69 74 20 2a 2f 0a 20 t one digit */.
b4180 20 20 20 72 65 73 75 6c 74 20 3d 20 28 73 69 67 result = (sig
b4190 6e 3c 30 20 26 26 20 6e 44 69 67 69 74 73 29 20 n<0 && nDigits)
b41a0 3f 20 2d 28 64 6f 75 62 6c 65 29 30 20 3a 20 28 ? -(double)0 : (
b41b0 64 6f 75 62 6c 65 29 30 3b 0a 20 20 7d 20 65 6c double)0;. } el
b41c0 73 65 20 7b 0a 20 20 20 20 2f 2a 20 61 74 74 65 se {. /* atte
b41d0 6d 70 74 20 74 6f 20 72 65 64 75 63 65 20 65 78 mpt to reduce ex
b41e0 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 20 20 69 66 ponent */. if
b41f0 28 20 65 73 69 67 6e 3e 30 20 29 7b 0a 20 20 20 ( esign>0 ){.
b4200 20 20 20 77 68 69 6c 65 28 20 73 3c 28 4c 41 52 while( s<(LAR
b4210 47 45 53 54 5f 49 4e 54 36 34 2f 31 30 29 20 26 GEST_INT64/10) &
b4220 26 20 65 3e 30 20 29 20 65 2d 2d 2c 73 2a 3d 31 & e>0 ) e--,s*=1
b4230 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 0;. }else{.
b4240 20 20 20 20 77 68 69 6c 65 28 20 21 28 73 25 31 while( !(s%1
b4250 30 29 20 26 26 20 65 3e 30 20 29 20 65 2d 2d 2c 0) && e>0 ) e--,
b4260 73 2f 3d 31 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 s/=10;. }..
b4270 20 20 2f 2a 20 61 64 6a 75 73 74 20 74 68 65 20 /* adjust the
b4280 73 69 67 6e 20 6f 66 20 73 69 67 6e 69 66 69 63 sign of signific
b4290 61 6e 64 20 2a 2f 0a 20 20 20 20 73 20 3d 20 73 and */. s = s
b42a0 69 67 6e 3c 30 20 3f 20 2d 73 20 3a 20 73 3b 0a ign<0 ? -s : s;.
b42b0 0a 20 20 20 20 2f 2a 20 69 66 20 65 78 70 6f 6e . /* if expon
b42c0 65 6e 74 2c 20 73 63 61 6c 65 20 73 69 67 6e 69 ent, scale signi
b42d0 66 69 63 61 6e 64 20 61 73 20 61 70 70 72 6f 70 ficand as approp
b42e0 72 69 61 74 65 0a 20 20 20 20 2a 2a 20 61 6e 64 riate. ** and
b42f0 20 73 74 6f 72 65 20 69 6e 20 72 65 73 75 6c 74 store in result
b4300 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 20 29 . */. if( e )
b4310 7b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 73 {. double s
b4320 63 61 6c 65 20 3d 20 31 2e 30 3b 0a 20 20 20 20 cale = 1.0;.
b4330 20 20 2f 2a 20 61 74 74 65 6d 70 74 20 74 6f 20 /* attempt to
b4340 68 61 6e 64 6c 65 20 65 78 74 72 65 6d 65 6c 79 handle extremely
b4350 20 73 6d 61 6c 6c 2f 6c 61 72 67 65 20 6e 75 6d small/large num
b4360 62 65 72 73 20 62 65 74 74 65 72 20 2a 2f 0a 20 bers better */.
b4370 20 20 20 20 20 69 66 28 20 65 3e 33 30 37 20 26 if( e>307 &
b4380 26 20 65 3c 33 34 32 20 29 7b 0a 20 20 20 20 20 & e<342 ){.
b4390 20 20 20 77 68 69 6c 65 28 20 65 25 33 30 38 20 while( e%308
b43a0 29 20 7b 20 73 63 61 6c 65 20 2a 3d 20 31 2e 30 ) { scale *= 1.0
b43b0 65 2b 31 3b 20 65 20 2d 3d 20 31 3b 20 7d 0a 20 e+1; e -= 1; }.
b43c0 20 20 20 20 20 20 20 69 66 28 20 65 73 69 67 6e if( esign
b43d0 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 <0 ){.
b43e0 72 65 73 75 6c 74 20 3d 20 73 20 2f 20 73 63 61 result = s / sca
b43f0 6c 65 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 le;. re
b4400 73 75 6c 74 20 2f 3d 20 31 2e 30 65 2b 33 30 38 sult /= 1.0e+308
b4410 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
b4420 0a 20 20 20 20 20 20 20 20 20 20 72 65 73 75 6c . resul
b4430 74 20 3d 20 73 20 2a 20 73 63 61 6c 65 3b 0a 20 t = s * scale;.
b4440 20 20 20 20 20 20 20 20 20 72 65 73 75 6c 74 20 result
b4450 2a 3d 20 31 2e 30 65 2b 33 30 38 3b 0a 20 20 20 *= 1.0e+308;.
b4460 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c }. }el
b4470 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 31 se{. /* 1
b4480 2e 30 65 2b 32 32 20 69 73 20 74 68 65 20 6c 61 .0e+22 is the la
b4490 72 67 65 73 74 20 70 6f 77 65 72 20 6f 66 20 31 rgest power of 1
b44a0 30 20 74 68 61 6e 20 63 61 6e 20 62 65 20 0a 20 0 than can be .
b44b0 20 20 20 20 20 20 20 2a 2a 20 72 65 70 72 65 73 ** repres
b44c0 65 6e 74 65 64 20 65 78 61 63 74 6c 79 2e 20 2a ented exactly. *
b44d0 2f 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 /. while(
b44e0 20 65 25 32 32 20 29 20 7b 20 73 63 61 6c 65 20 e%22 ) { scale
b44f0 2a 3d 20 31 2e 30 65 2b 31 3b 20 65 20 2d 3d 20 *= 1.0e+1; e -=
b4500 31 3b 20 7d 0a 20 20 20 20 20 20 20 20 77 68 69 1; }. whi
b4510 6c 65 28 20 65 3e 30 20 29 20 7b 20 73 63 61 6c le( e>0 ) { scal
b4520 65 20 2a 3d 20 31 2e 30 65 2b 32 32 3b 20 65 20 e *= 1.0e+22; e
b4530 2d 3d 20 32 32 3b 20 7d 0a 20 20 20 20 20 20 20 -= 22; }.
b4540 20 69 66 28 20 65 73 69 67 6e 3c 30 20 29 7b 0a if( esign<0 ){.
b4550 20 20 20 20 20 20 20 20 20 20 72 65 73 75 6c 74 result
b4560 20 3d 20 73 20 2f 20 73 63 61 6c 65 3b 0a 20 20 = s / scale;.
b4570 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
b4580 20 20 20 20 20 20 20 72 65 73 75 6c 74 20 3d 20 result =
b4590 73 20 2a 20 73 63 61 6c 65 3b 0a 20 20 20 20 20 s * scale;.
b45a0 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
b45b0 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 } else {.
b45c0 72 65 73 75 6c 74 20 3d 20 28 64 6f 75 62 6c 65 result = (double
b45d0 29 73 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 )s;. }. }..
b45e0 20 2f 2a 20 73 74 6f 72 65 20 74 68 65 20 72 65 /* store the re
b45f0 73 75 6c 74 20 2a 2f 0a 20 20 2a 70 52 65 73 75 sult */. *pResu
b4600 6c 74 20 3d 20 72 65 73 75 6c 74 3b 0a 0a 20 20 lt = result;..
b4610 2f 2a 20 72 65 74 75 72 6e 20 6e 75 6d 62 65 72 /* return number
b4620 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 75 of characters u
b4630 73 65 64 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 sed */. return
b4640 28 69 6e 74 29 28 7a 20 2d 20 7a 42 65 67 69 6e (int)(z - zBegin
b4650 29 3b 0a 23 65 6c 73 65 0a 20 20 72 65 74 75 72 );.#else. retur
b4660 6e 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 n sqlite3Atoi64(
b4670 7a 2c 20 70 52 65 73 75 6c 74 29 3b 0a 23 65 6e z, pResult);.#en
b4680 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
b4690 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e IT_FLOATING_POIN
b46a0 54 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f T */.}../*.** Co
b46b0 6d 70 61 72 65 20 74 68 65 20 31 39 2d 63 68 61 mpare the 19-cha
b46c0 72 61 63 74 65 72 20 73 74 72 69 6e 67 20 7a 4e racter string zN
b46d0 75 6d 20 61 67 61 69 6e 73 74 20 74 68 65 20 74 um against the t
b46e0 65 78 74 20 72 65 70 72 65 73 65 6e 74 61 74 69 ext representati
b46f0 6f 6e 0a 2a 2a 20 76 61 6c 75 65 20 32 5e 36 33 on.** value 2^63
b4700 3a 20 20 39 32 32 33 33 37 32 30 33 36 38 35 34 : 9223372036854
b4710 37 37 35 38 30 38 2e 20 20 52 65 74 75 72 6e 20 775808. Return
b4720 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f 2c 20 negative, zero,
b4730 6f 72 20 70 6f 73 69 74 69 76 65 0a 2a 2a 20 69 or positive.** i
b4740 66 20 7a 4e 75 6d 20 69 73 20 6c 65 73 73 20 74 f zNum is less t
b4750 68 61 6e 2c 20 65 71 75 61 6c 20 74 6f 2c 20 6f han, equal to, o
b4760 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 r greater than t
b4770 68 65 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a he string..**.**
b4780 20 55 6e 6c 69 6b 65 20 6d 65 6d 63 6d 70 28 29 Unlike memcmp()
b4790 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
b47a0 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 72 guaranteed to r
b47b0 65 74 75 72 6e 20 74 68 65 20 64 69 66 66 65 72 eturn the differ
b47c0 65 6e 63 65 0a 2a 2a 20 69 6e 20 74 68 65 20 76 ence.** in the v
b47d0 61 6c 75 65 73 20 6f 66 20 74 68 65 20 6c 61 73 alues of the las
b47e0 74 20 64 69 67 69 74 20 69 66 20 74 68 65 20 6f t digit if the o
b47f0 6e 6c 79 20 64 69 66 66 65 72 65 6e 63 65 20 69 nly difference i
b4800 73 20 69 6e 20 74 68 65 0a 2a 2a 20 6c 61 73 74 s in the.** last
b4810 20 64 69 67 69 74 2e 20 20 53 6f 2c 20 66 6f 72 digit. So, for
b4820 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 0a 2a 2a 20 example,.**.**
b4830 20 20 20 20 20 63 6f 6d 70 61 72 65 32 70 6f 77 compare2pow
b4840 36 33 28 22 39 32 32 33 33 37 32 30 33 36 38 35 63("922337203685
b4850 34 37 37 35 38 30 30 22 29 0a 2a 2a 0a 2a 2a 20 4775800").**.**
b4860 77 69 6c 6c 20 72 65 74 75 72 6e 20 2d 38 2e 0a will return -8..
b4870 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f */.static int co
b4880 6d 70 61 72 65 32 70 6f 77 36 33 28 63 6f 6e 73 mpare2pow63(cons
b4890 74 20 63 68 61 72 20 2a 7a 4e 75 6d 29 7b 0a 20 t char *zNum){.
b48a0 20 69 6e 74 20 63 3b 0a 20 20 63 20 3d 20 6d 65 int c;. c = me
b48b0 6d 63 6d 70 28 7a 4e 75 6d 2c 22 39 32 32 33 33 mcmp(zNum,"92233
b48c0 37 32 30 33 36 38 35 34 37 37 35 38 30 22 2c 31 7203685477580",1
b48d0 38 29 2a 31 30 3b 0a 20 20 69 66 28 20 63 3d 3d 8)*10;. if( c==
b48e0 30 20 29 7b 0a 20 20 20 20 63 20 3d 20 7a 4e 75 0 ){. c = zNu
b48f0 6d 5b 31 38 5d 20 2d 20 27 38 27 3b 0a 20 20 7d m[18] - '8';. }
b4900 0a 20 20 72 65 74 75 72 6e 20 63 3b 0a 7d 0a 0a . return c;.}..
b4910 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 ./*.** Return TR
b4920 55 45 20 69 66 20 7a 4e 75 6d 20 69 73 20 61 20 UE if zNum is a
b4930 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 64-bit signed in
b4940 74 65 67 65 72 20 61 6e 64 20 77 72 69 74 65 0a teger and write.
b4950 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 ** the value of
b4960 74 68 65 20 69 6e 74 65 67 65 72 20 69 6e 74 6f the integer into
b4970 20 2a 70 4e 75 6d 2e 20 20 49 66 20 7a 4e 75 6d *pNum. If zNum
b4980 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 67 is not an integ
b4990 65 72 0a 2a 2a 20 6f 72 20 69 73 20 61 6e 20 69 er.** or is an i
b49a0 6e 74 65 67 65 72 20 74 68 61 74 20 69 73 20 74 nteger that is t
b49b0 6f 6f 20 6c 61 72 67 65 20 74 6f 20 62 65 20 65 oo large to be e
b49c0 78 70 72 65 73 73 65 64 20 77 69 74 68 20 36 34 xpressed with 64
b49d0 20 62 69 74 73 2c 0a 2a 2a 20 74 68 65 6e 20 72 bits,.** then r
b49e0 65 74 75 72 6e 20 66 61 6c 73 65 2e 0a 2a 2a 0a eturn false..**.
b49f0 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 72 6f 75 ** When this rou
b4a00 74 69 6e 65 20 77 61 73 20 6f 72 69 67 69 6e 61 tine was origina
b4a10 6c 6c 79 20 77 72 69 74 74 65 6e 20 69 74 20 64 lly written it d
b4a20 65 61 6c 74 20 77 69 74 68 20 6f 6e 6c 79 0a 2a ealt with only.*
b4a30 2a 20 33 32 2d 62 69 74 20 6e 75 6d 62 65 72 73 * 32-bit numbers
b4a40 2e 20 20 41 74 20 74 68 61 74 20 74 69 6d 65 2c . At that time,
b4a50 20 69 74 20 77 61 73 20 6d 75 63 68 20 66 61 73 it was much fas
b4a60 74 65 72 20 74 68 61 6e 20 74 68 65 0a 2a 2a 20 ter than the.**
b4a70 61 74 6f 69 28 29 20 6c 69 62 72 61 72 79 20 72 atoi() library r
b4a80 6f 75 74 69 6e 65 20 69 6e 20 52 65 64 48 61 74 outine in RedHat
b4a90 20 37 2e 32 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 7.2..*/.SQLITE_
b4aa0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
b4ab0 74 65 33 41 74 6f 69 36 34 28 63 6f 6e 73 74 20 te3Atoi64(const
b4ac0 63 68 61 72 20 2a 7a 4e 75 6d 2c 20 69 36 34 20 char *zNum, i64
b4ad0 2a 70 4e 75 6d 29 7b 0a 20 20 69 36 34 20 76 20 *pNum){. i64 v
b4ae0 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 65 67 3b 0a = 0;. int neg;.
b4af0 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20 20 63 6f int i, c;. co
b4b00 6e 73 74 20 63 68 61 72 20 2a 7a 53 74 61 72 74 nst char *zStart
b4b10 3b 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 ;. while( sqlit
b4b20 65 33 49 73 73 70 61 63 65 28 2a 7a 4e 75 6d 29 e3Isspace(*zNum)
b4b30 20 29 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 69 66 28 ) zNum++;. if(
b4b40 20 2a 7a 4e 75 6d 3d 3d 27 2d 27 20 29 7b 0a 20 *zNum=='-' ){.
b4b50 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20 20 20 neg = 1;.
b4b60 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 65 6c 73 65 20 zNum++;. }else
b4b70 69 66 28 20 2a 7a 4e 75 6d 3d 3d 27 2b 27 20 29 if( *zNum=='+' )
b4b80 7b 0a 20 20 20 20 6e 65 67 20 3d 20 30 3b 0a 20 {. neg = 0;.
b4b90 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 65 6c zNum++;. }el
b4ba0 73 65 7b 0a 20 20 20 20 6e 65 67 20 3d 20 30 3b se{. neg = 0;
b4bb0 0a 20 20 7d 0a 20 20 7a 53 74 61 72 74 20 3d 20 . }. zStart =
b4bc0 7a 4e 75 6d 3b 0a 20 20 77 68 69 6c 65 28 20 7a zNum;. while( z
b4bd0 4e 75 6d 5b 30 5d 3d 3d 27 30 27 20 29 7b 20 7a Num[0]=='0' ){ z
b4be0 4e 75 6d 2b 2b 3b 20 7d 20 2f 2a 20 53 6b 69 70 Num++; } /* Skip
b4bf0 20 6f 76 65 72 20 6c 65 61 64 69 6e 67 20 7a 65 over leading ze
b4c00 72 6f 73 2e 20 54 69 63 6b 65 74 20 23 32 34 35 ros. Ticket #245
b4c10 34 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 4 */. for(i=0;
b4c20 28 63 3d 7a 4e 75 6d 5b 69 5d 29 3e 3d 27 30 27 (c=zNum[i])>='0'
b4c30 20 26 26 20 63 3c 3d 27 39 27 3b 20 69 2b 2b 29 && c<='9'; i++)
b4c40 7b 0a 20 20 20 20 76 20 3d 20 76 2a 31 30 20 2b {. v = v*10 +
b4c50 20 63 20 2d 20 27 30 27 3b 0a 20 20 7d 0a 20 20 c - '0';. }.
b4c60 2a 70 4e 75 6d 20 3d 20 6e 65 67 20 3f 20 2d 76 *pNum = neg ? -v
b4c70 20 3a 20 76 3b 0a 20 20 69 66 28 20 63 21 3d 30 : v;. if( c!=0
b4c80 20 7c 7c 20 28 69 3d 3d 30 20 26 26 20 7a 53 74 || (i==0 && zSt
b4c90 61 72 74 3d 3d 7a 4e 75 6d 29 20 7c 7c 20 69 3e art==zNum) || i>
b4ca0 31 39 20 29 7b 0a 20 20 20 20 2f 2a 20 7a 4e 75 19 ){. /* zNu
b4cb0 6d 20 69 73 20 65 6d 70 74 79 20 6f 72 20 63 6f m is empty or co
b4cc0 6e 74 61 69 6e 73 20 6e 6f 6e 2d 6e 75 6d 65 72 ntains non-numer
b4cd0 69 63 20 74 65 78 74 20 6f 72 20 69 73 20 6c 6f ic text or is lo
b4ce0 6e 67 65 72 0a 20 20 20 20 2a 2a 20 74 68 61 6e nger. ** than
b4cf0 20 31 39 20 64 69 67 69 74 73 20 28 74 68 75 73 19 digits (thus
b4d00 20 67 75 61 72 61 6e 74 69 6e 67 20 74 68 61 74 guaranting that
b4d10 20 69 74 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 it is too large
b4d20 29 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 ) */. return
b4d30 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69 0;. }else if( i
b4d40 3c 31 39 20 29 7b 0a 20 20 20 20 2f 2a 20 4c 65 <19 ){. /* Le
b4d50 73 73 20 74 68 61 6e 20 31 39 20 64 69 67 69 74 ss than 19 digit
b4d60 73 2c 20 73 6f 20 77 65 20 6b 6e 6f 77 20 74 68 s, so we know th
b4d70 61 74 20 69 74 20 66 69 74 73 20 69 6e 20 36 34 at it fits in 64
b4d80 20 62 69 74 73 20 2a 2f 0a 20 20 20 20 72 65 74 bits */. ret
b4d90 75 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a urn 1;. }else{.
b4da0 20 20 20 20 2f 2a 20 31 39 2d 64 69 67 69 74 20 /* 19-digit
b4db0 6e 75 6d 62 65 72 73 20 6d 75 73 74 20 62 65 20 numbers must be
b4dc0 6e 6f 20 6c 61 72 67 65 72 20 74 68 61 6e 20 39 no larger than 9
b4dd0 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35 38 2233720368547758
b4de0 30 37 20 69 66 20 70 6f 73 69 74 69 76 65 0a 20 07 if positive.
b4df0 20 20 20 2a 2a 20 6f 72 20 39 32 32 33 33 37 32 ** or 9223372
b4e00 30 33 36 38 35 34 37 37 35 38 30 38 20 69 66 20 036854775808 if
b4e10 6e 65 67 61 74 69 76 65 2e 20 20 4e 6f 74 65 20 negative. Note
b4e20 74 68 61 74 20 39 32 32 33 33 37 32 30 33 36 38 that 92233720368
b4e30 35 34 36 36 35 38 30 38 0a 20 20 20 20 2a 2a 20 54665808. **
b4e40 69 73 20 32 5e 36 33 2e 20 2a 2f 0a 20 20 20 20 is 2^63. */.
b4e50 72 65 74 75 72 6e 20 63 6f 6d 70 61 72 65 32 70 return compare2p
b4e60 6f 77 36 33 28 7a 4e 75 6d 29 3c 6e 65 67 3b 0a ow63(zNum)<neg;.
b4e70 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 }.}../*.** The
b4e80 20 73 74 72 69 6e 67 20 7a 4e 75 6d 20 72 65 70 string zNum rep
b4e90 72 65 73 65 6e 74 73 20 61 6e 20 75 6e 73 69 67 resents an unsig
b4ea0 6e 65 64 20 69 6e 74 65 67 65 72 2e 20 20 54 68 ned integer. Th
b4eb0 65 20 7a 4e 75 6d 20 73 74 72 69 6e 67 0a 2a 2a e zNum string.**
b4ec0 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 6f 6e 65 consists of one
b4ed0 20 6f 72 20 6d 6f 72 65 20 64 69 67 69 74 20 63 or more digit c
b4ee0 68 61 72 61 63 74 65 72 73 20 61 6e 64 20 69 73 haracters and is
b4ef0 20 74 65 72 6d 69 6e 61 74 65 64 20 62 79 0a 2a terminated by.*
b4f00 2a 20 61 20 7a 65 72 6f 20 63 68 61 72 61 63 74 * a zero charact
b4f10 65 72 2e 20 20 41 6e 79 20 73 74 72 61 79 20 63 er. Any stray c
b4f20 68 61 72 61 63 74 65 72 73 20 69 6e 20 7a 4e 75 haracters in zNu
b4f30 6d 20 72 65 73 75 6c 74 20 69 6e 20 75 6e 64 65 m result in unde
b4f40 66 69 6e 65 64 0a 2a 2a 20 62 65 68 61 76 69 6f fined.** behavio
b4f50 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 r..**.** If the
b4f60 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 unsigned integer
b4f70 20 74 68 61 74 20 7a 4e 75 6d 20 72 65 70 72 65 that zNum repre
b4f80 73 65 6e 74 73 20 77 69 6c 6c 20 66 69 74 20 69 sents will fit i
b4f90 6e 20 61 0a 2a 2a 20 36 34 2d 62 69 74 20 73 69 n a.** 64-bit si
b4fa0 67 6e 65 64 20 69 6e 74 65 67 65 72 2c 20 72 65 gned integer, re
b4fb0 74 75 72 6e 20 54 52 55 45 2e 20 20 4f 74 68 65 turn TRUE. Othe
b4fc0 72 77 69 73 65 20 72 65 74 75 72 6e 20 46 41 4c rwise return FAL
b4fd0 53 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 SE..**.** If the
b4fe0 20 6e 65 67 46 6c 61 67 20 70 61 72 61 6d 65 74 negFlag paramet
b4ff0 65 72 20 69 73 20 74 72 75 65 2c 20 74 68 61 74 er is true, that
b5000 20 6d 65 61 6e 73 20 74 68 61 74 20 7a 4e 75 6d means that zNum
b5010 20 72 65 61 6c 6c 79 20 72 65 70 72 65 73 65 6e really represen
b5020 74 73 0a 2a 2a 20 61 20 6e 65 67 61 74 69 76 65 ts.** a negative
b5030 20 6e 75 6d 62 65 72 2e 20 20 28 54 68 65 20 6c number. (The l
b5040 65 61 64 69 6e 67 20 22 2d 22 20 69 73 20 6f 6d eading "-" is om
b5050 69 74 74 65 64 20 66 72 6f 6d 20 7a 4e 75 6d 2e itted from zNum.
b5060 29 20 20 54 68 69 73 0a 2a 2a 20 70 61 72 61 6d ) This.** param
b5070 65 74 65 72 20 69 73 20 6e 65 65 64 65 64 20 74 eter is needed t
b5080 6f 20 64 65 74 65 72 6d 69 6e 65 20 61 20 62 6f o determine a bo
b5090 75 6e 64 61 72 79 20 63 61 73 65 2e 20 20 41 20 undary case. A
b50a0 73 74 72 69 6e 67 0a 2a 2a 20 6f 66 20 22 39 32 string.** of "92
b50b0 32 33 33 37 33 30 33 36 38 35 34 37 37 35 38 30 2337303685477580
b50c0 38 22 20 72 65 74 75 72 6e 73 20 66 61 6c 73 65 8" returns false
b50d0 20 69 66 20 6e 65 67 46 6c 61 67 20 69 73 20 66 if negFlag is f
b50e0 61 6c 73 65 20 6f 72 20 74 72 75 65 0a 2a 2a 20 alse or true.**
b50f0 69 66 20 6e 65 67 46 6c 61 67 20 69 73 20 74 72 if negFlag is tr
b5100 75 65 2e 0a 2a 2a 0a 2a 2a 20 4c 65 61 64 69 6e ue..**.** Leadin
b5110 67 20 7a 65 72 6f 73 20 61 72 65 20 69 67 6e 6f g zeros are igno
b5120 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 red..*/.SQLITE_P
b5130 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
b5140 65 33 46 69 74 73 49 6e 36 34 42 69 74 73 28 63 e3FitsIn64Bits(c
b5150 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 75 6d 2c onst char *zNum,
b5160 20 69 6e 74 20 6e 65 67 46 6c 61 67 29 7b 0a 20 int negFlag){.
b5170 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 65 int i;. int ne
b5180 67 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 g = 0;.. assert
b5190 28 20 7a 4e 75 6d 5b 30 5d 3e 3d 27 30 27 20 26 ( zNum[0]>='0' &
b51a0 26 20 7a 4e 75 6d 5b 30 5d 3c 3d 27 39 27 20 29 & zNum[0]<='9' )
b51b0 3b 20 2f 2a 20 7a 4e 75 6d 20 69 73 20 61 6e 20 ; /* zNum is an
b51c0 75 6e 73 69 67 6e 65 64 20 6e 75 6d 62 65 72 20 unsigned number
b51d0 2a 2f 0a 0a 20 20 69 66 28 20 6e 65 67 46 6c 61 */.. if( negFla
b51e0 67 20 29 20 6e 65 67 20 3d 20 31 2d 6e 65 67 3b g ) neg = 1-neg;
b51f0 0a 20 20 77 68 69 6c 65 28 20 2a 7a 4e 75 6d 3d . while( *zNum=
b5200 3d 27 30 27 20 29 7b 0a 20 20 20 20 7a 4e 75 6d ='0' ){. zNum
b5210 2b 2b 3b 20 20 20 2f 2a 20 53 6b 69 70 20 6c 65 ++; /* Skip le
b5220 61 64 69 6e 67 20 7a 65 72 6f 73 2e 20 20 54 69 ading zeros. Ti
b5230 63 6b 65 74 20 23 32 34 35 34 20 2a 2f 0a 20 20 cket #2454 */.
b5240 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 7a 4e 75 }. for(i=0; zNu
b5250 6d 5b 69 5d 3b 20 69 2b 2b 29 7b 20 61 73 73 65 m[i]; i++){ asse
b5260 72 74 28 20 7a 4e 75 6d 5b 69 5d 3e 3d 27 30 27 rt( zNum[i]>='0'
b5270 20 26 26 20 7a 4e 75 6d 5b 69 5d 3c 3d 27 39 27 && zNum[i]<='9'
b5280 20 29 3b 20 7d 0a 20 20 69 66 28 20 69 3c 31 39 ); }. if( i<19
b5290 20 29 7b 0a 20 20 20 20 2f 2a 20 47 75 61 72 61 ){. /* Guara
b52a0 6e 74 65 65 64 20 74 6f 20 66 69 74 20 69 66 20 nteed to fit if
b52b0 6c 65 73 73 20 74 68 61 6e 20 31 39 20 64 69 67 less than 19 dig
b52c0 69 74 73 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 its */. retur
b52d0 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 n 1;. }else if(
b52e0 20 69 3e 31 39 20 29 7b 0a 20 20 20 20 2f 2a 20 i>19 ){. /*
b52f0 47 75 61 72 61 6e 74 65 65 64 20 74 6f 20 62 65 Guaranteed to be
b5300 20 74 6f 6f 20 62 69 67 20 69 66 20 67 72 65 61 too big if grea
b5310 74 65 72 20 74 68 61 6e 20 31 39 20 64 69 67 69 ter than 19 digi
b5320 74 73 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e ts */. return
b5330 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 0;. }else{.
b5340 20 2f 2a 20 43 6f 6d 70 61 72 65 20 61 67 61 69 /* Compare agai
b5350 6e 73 74 20 32 5e 36 33 2e 20 2a 2f 0a 20 20 20 nst 2^63. */.
b5360 20 72 65 74 75 72 6e 20 63 6f 6d 70 61 72 65 32 return compare2
b5370 70 6f 77 36 33 28 7a 4e 75 6d 29 3c 6e 65 67 3b pow63(zNum)<neg;
b5380 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 . }.}../*.** If
b5390 20 7a 4e 75 6d 20 72 65 70 72 65 73 65 6e 74 73 zNum represents
b53a0 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68 61 74 an integer that
b53b0 20 77 69 6c 6c 20 66 69 74 20 69 6e 20 33 32 2d will fit in 32-
b53c0 62 69 74 73 2c 20 74 68 65 6e 20 73 65 74 0a 2a bits, then set.*
b53d0 2a 20 2a 70 56 61 6c 75 65 20 74 6f 20 74 68 61 * *pValue to tha
b53e0 74 20 69 6e 74 65 67 65 72 20 61 6e 64 20 72 65 t integer and re
b53f0 74 75 72 6e 20 74 72 75 65 2e 20 20 4f 74 68 65 turn true. Othe
b5400 72 77 69 73 65 20 72 65 74 75 72 6e 20 66 61 6c rwise return fal
b5410 73 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 6e 6f se..**.** Any no
b5420 6e 2d 6e 75 6d 65 72 69 63 20 63 68 61 72 61 63 n-numeric charac
b5430 74 65 72 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 ters that follow
b5440 69 6e 67 20 7a 4e 75 6d 20 61 72 65 20 69 67 6e ing zNum are ign
b5450 6f 72 65 64 2e 0a 2a 2a 20 54 68 69 73 20 69 73 ored..** This is
b5460 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 different from
b5470 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 29 20 sqlite3Atoi64()
b5480 77 68 69 63 68 20 72 65 71 75 69 72 65 73 20 74 which requires t
b5490 68 65 0a 2a 2a 20 69 6e 70 75 74 20 6e 75 6d 62 he.** input numb
b54a0 65 72 20 74 6f 20 62 65 20 7a 65 72 6f 2d 74 65 er to be zero-te
b54b0 72 6d 69 6e 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c rminated..*/.SQL
b54c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
b54d0 73 71 6c 69 74 65 33 47 65 74 49 6e 74 33 32 28 sqlite3GetInt32(
b54e0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 75 6d const char *zNum
b54f0 2c 20 69 6e 74 20 2a 70 56 61 6c 75 65 29 7b 0a , int *pValue){.
b5500 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 76 sqlite_int64 v
b5510 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 2c 20 63 = 0;. int i, c
b5520 3b 0a 20 20 69 6e 74 20 6e 65 67 20 3d 20 30 3b ;. int neg = 0;
b5530 0a 20 20 69 66 28 20 7a 4e 75 6d 5b 30 5d 3d 3d . if( zNum[0]==
b5540 27 2d 27 20 29 7b 0a 20 20 20 20 6e 65 67 20 3d '-' ){. neg =
b5550 20 31 3b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 1;. zNum++;.
b5560 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 4e 75 6d }else if( zNum
b5570 5b 30 5d 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 [0]=='+' ){.
b5580 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 0a 20 20 77 68 zNum++;. }. wh
b5590 69 6c 65 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 30 ile( zNum[0]=='0
b55a0 27 20 29 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 66 6f ' ) zNum++;. fo
b55b0 72 28 69 3d 30 3b 20 69 3c 31 31 20 26 26 20 28 r(i=0; i<11 && (
b55c0 63 20 3d 20 7a 4e 75 6d 5b 69 5d 20 2d 20 27 30 c = zNum[i] - '0
b55d0 27 29 3e 3d 30 20 26 26 20 63 3c 3d 39 3b 20 69 ')>=0 && c<=9; i
b55e0 2b 2b 29 7b 0a 20 20 20 20 76 20 3d 20 76 2a 31 ++){. v = v*1
b55f0 30 20 2b 20 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0 + c;. }.. /*
b5600 20 54 68 65 20 6c 6f 6e 67 65 73 74 20 64 65 63 The longest dec
b5610 69 6d 61 6c 20 72 65 70 72 65 73 65 6e 74 61 74 imal representat
b5620 69 6f 6e 20 6f 66 20 61 20 33 32 20 62 69 74 20 ion of a 32 bit
b5630 69 6e 74 65 67 65 72 20 69 73 20 31 30 20 64 69 integer is 10 di
b5640 67 69 74 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 gits:. **. **
b5650 20 20 20 20 20 20 20 20 20 20 20 20 31 32 33 34 1234
b5660 35 36 37 38 39 30 0a 20 20 2a 2a 20 20 20 20 20 567890. **
b5670 32 5e 33 31 20 2d 3e 20 32 31 34 37 34 38 33 36 2^31 -> 21474836
b5680 34 38 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 3e 48. */. if( i>
b5690 31 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 10 ){. return
b56a0 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 76 2d 0;. }. if( v-
b56b0 6e 65 67 3e 32 31 34 37 34 38 33 36 34 37 20 29 neg>2147483647 )
b56c0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a {. return 0;.
b56d0 20 20 7d 0a 20 20 69 66 28 20 6e 65 67 20 29 7b }. if( neg ){
b56e0 0a 20 20 20 20 76 20 3d 20 2d 76 3b 0a 20 20 7d . v = -v;. }
b56f0 0a 20 20 2a 70 56 61 6c 75 65 20 3d 20 28 69 6e . *pValue = (in
b5700 74 29 76 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b t)v;. return 1;
b5710 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 76 61 .}../*.** The va
b5720 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e riable-length in
b5730 74 65 67 65 72 20 65 6e 63 6f 64 69 6e 67 20 69 teger encoding i
b5740 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a s as follows:.**
b5750 0a 2a 2a 20 4b 45 59 3a 0a 2a 2a 20 20 20 20 20 .** KEY:.**
b5760 20 20 20 20 41 20 3d 20 30 78 78 78 78 78 78 78 A = 0xxxxxxx
b5770 20 20 20 20 37 20 62 69 74 73 20 6f 66 20 64 61 7 bits of da
b5780 74 61 20 61 6e 64 20 6f 6e 65 20 66 6c 61 67 20 ta and one flag
b5790 62 69 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 42 bit.** B
b57a0 20 3d 20 31 78 78 78 78 78 78 78 20 20 20 20 37 = 1xxxxxxx 7
b57b0 20 62 69 74 73 20 6f 66 20 64 61 74 61 20 61 6e bits of data an
b57c0 64 20 6f 6e 65 20 66 6c 61 67 20 62 69 74 0a 2a d one flag bit.*
b57d0 2a 20 20 20 20 20 20 20 20 20 43 20 3d 20 78 78 * C = xx
b57e0 78 78 78 78 78 78 20 20 20 20 38 20 62 69 74 73 xxxxxx 8 bits
b57f0 20 6f 66 20 64 61 74 61 0a 2a 2a 0a 2a 2a 20 20 of data.**.**
b5800 37 20 62 69 74 73 20 2d 20 41 0a 2a 2a 20 31 34 7 bits - A.** 14
b5810 20 62 69 74 73 20 2d 20 42 41 0a 2a 2a 20 32 31 bits - BA.** 21
b5820 20 62 69 74 73 20 2d 20 42 42 41 0a 2a 2a 20 32 bits - BBA.** 2
b5830 38 20 62 69 74 73 20 2d 20 42 42 42 41 0a 2a 2a 8 bits - BBBA.**
b5840 20 33 35 20 62 69 74 73 20 2d 20 42 42 42 42 41 35 bits - BBBBA
b5850 0a 2a 2a 20 34 32 20 62 69 74 73 20 2d 20 42 42 .** 42 bits - BB
b5860 42 42 42 41 0a 2a 2a 20 34 39 20 62 69 74 73 20 BBBA.** 49 bits
b5870 2d 20 42 42 42 42 42 42 41 0a 2a 2a 20 35 36 20 - BBBBBBA.** 56
b5880 62 69 74 73 20 2d 20 42 42 42 42 42 42 42 41 0a bits - BBBBBBBA.
b5890 2a 2a 20 36 34 20 62 69 74 73 20 2d 20 42 42 42 ** 64 bits - BBB
b58a0 42 42 42 42 42 43 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a BBBBBC.*/../*.**
b58b0 20 57 72 69 74 65 20 61 20 36 34 2d 62 69 74 20 Write a 64-bit
b58c0 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 variable-length
b58d0 69 6e 74 65 67 65 72 20 74 6f 20 6d 65 6d 6f 72 integer to memor
b58e0 79 20 73 74 61 72 74 69 6e 67 20 61 74 20 70 5b y starting at p[
b58f0 30 5d 2e 0a 2a 2a 20 54 68 65 20 6c 65 6e 67 74 0]..** The lengt
b5900 68 20 6f 66 20 64 61 74 61 20 77 72 69 74 65 20 h of data write
b5910 77 69 6c 6c 20 62 65 20 62 65 74 77 65 65 6e 20 will be between
b5920 31 20 61 6e 64 20 39 20 62 79 74 65 73 2e 20 20 1 and 9 bytes.
b5930 54 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 The number.** of
b5940 20 62 79 74 65 73 20 77 72 69 74 74 65 6e 20 69 bytes written i
b5950 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a s returned..**.*
b5960 2a 20 41 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e * A variable-len
b5970 67 74 68 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 gth integer cons
b5980 69 73 74 73 20 6f 66 20 74 68 65 20 6c 6f 77 65 ists of the lowe
b5990 72 20 37 20 62 69 74 73 20 6f 66 20 65 61 63 68 r 7 bits of each
b59a0 20 62 79 74 65 0a 2a 2a 20 66 6f 72 20 61 6c 6c byte.** for all
b59b0 20 62 79 74 65 73 20 74 68 61 74 20 68 61 76 65 bytes that have
b59c0 20 74 68 65 20 38 74 68 20 62 69 74 20 73 65 74 the 8th bit set
b59d0 20 61 6e 64 20 6f 6e 65 20 62 79 74 65 20 77 69 and one byte wi
b59e0 74 68 20 74 68 65 20 38 74 68 0a 2a 2a 20 62 69 th the 8th.** bi
b59f0 74 20 63 6c 65 61 72 2e 20 20 45 78 63 65 70 74 t clear. Except
b5a00 2c 20 69 66 20 77 65 20 67 65 74 20 74 6f 20 74 , if we get to t
b5a10 68 65 20 39 74 68 20 62 79 74 65 2c 20 69 74 20 he 9th byte, it
b5a20 73 74 6f 72 65 73 20 74 68 65 20 66 75 6c 6c 0a stores the full.
b5a30 2a 2a 20 38 20 62 69 74 73 20 61 6e 64 20 69 73 ** 8 bits and is
b5a40 20 74 68 65 20 6c 61 73 74 20 62 79 74 65 2e 0a the last byte..
b5a50 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
b5a60 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 75 74 E int sqlite3Put
b5a70 56 61 72 69 6e 74 28 75 6e 73 69 67 6e 65 64 20 Varint(unsigned
b5a80 63 68 61 72 20 2a 70 2c 20 75 36 34 20 76 29 7b char *p, u64 v){
b5a90 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6e 3b 0a . int i, j, n;.
b5aa0 20 20 75 38 20 62 75 66 5b 31 30 5d 3b 0a 20 20 u8 buf[10];.
b5ab0 69 66 28 20 76 20 26 20 28 28 28 75 36 34 29 30 if( v & (((u64)0
b5ac0 78 66 66 30 30 30 30 30 30 29 3c 3c 33 32 29 20 xff000000)<<32)
b5ad0 29 7b 0a 20 20 20 20 70 5b 38 5d 20 3d 20 28 75 ){. p[8] = (u
b5ae0 38 29 76 3b 0a 20 20 20 20 76 20 3e 3e 3d 20 38 8)v;. v >>= 8
b5af0 3b 0a 20 20 20 20 66 6f 72 28 69 3d 37 3b 20 69 ;. for(i=7; i
b5b00 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 20 >=0; i--){.
b5b10 20 70 5b 69 5d 20 3d 20 28 75 38 29 28 28 76 20 p[i] = (u8)((v
b5b20 26 20 30 78 37 66 29 20 7c 20 30 78 38 30 29 3b & 0x7f) | 0x80);
b5b30 0a 20 20 20 20 20 20 76 20 3e 3e 3d 20 37 3b 0a . v >>= 7;.
b5b40 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e }. return
b5b50 20 39 3b 0a 20 20 7d 20 20 20 20 0a 20 20 6e 20 9;. } . n
b5b60 3d 20 30 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 62 = 0;. do{. b
b5b70 75 66 5b 6e 2b 2b 5d 20 3d 20 28 75 38 29 28 28 uf[n++] = (u8)((
b5b80 76 20 26 20 30 78 37 66 29 20 7c 20 30 78 38 30 v & 0x7f) | 0x80
b5b90 29 3b 0a 20 20 20 20 76 20 3e 3e 3d 20 37 3b 0a );. v >>= 7;.
b5ba0 20 20 7d 77 68 69 6c 65 28 20 76 21 3d 30 20 29 }while( v!=0 )
b5bb0 3b 0a 20 20 62 75 66 5b 30 5d 20 26 3d 20 30 78 ;. buf[0] &= 0x
b5bc0 37 66 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3c 7f;. assert( n<
b5bd0 3d 39 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 2c =9 );. for(i=0,
b5be0 20 6a 3d 6e 2d 31 3b 20 6a 3e 3d 30 3b 20 6a 2d j=n-1; j>=0; j-
b5bf0 2d 2c 20 69 2b 2b 29 7b 0a 20 20 20 20 70 5b 69 -, i++){. p[i
b5c00 5d 20 3d 20 62 75 66 5b 6a 5d 3b 0a 20 20 7d 0a ] = buf[j];. }.
b5c10 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f return n;.}../
b5c20 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
b5c30 65 20 69 73 20 61 20 66 61 73 74 65 72 20 76 65 e is a faster ve
b5c40 72 73 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 rsion of sqlite3
b5c50 50 75 74 56 61 72 69 6e 74 28 29 20 74 68 61 74 PutVarint() that
b5c60 20 6f 6e 6c 79 0a 2a 2a 20 77 6f 72 6b 73 20 66 only.** works f
b5c70 6f 72 20 33 32 2d 62 69 74 20 70 6f 73 69 74 69 or 32-bit positi
b5c80 76 65 20 69 6e 74 65 67 65 72 73 20 61 6e 64 20 ve integers and
b5c90 77 68 69 63 68 20 69 73 20 6f 70 74 69 6d 69 7a which is optimiz
b5ca0 65 64 20 66 6f 72 0a 2a 2a 20 74 68 65 20 63 6f ed for.** the co
b5cb0 6d 6d 6f 6e 20 63 61 73 65 20 6f 66 20 73 6d 61 mmon case of sma
b5cc0 6c 6c 20 69 6e 74 65 67 65 72 73 2e 20 20 41 20 ll integers. A
b5cd0 4d 41 43 52 4f 20 76 65 72 73 69 6f 6e 2c 20 70 MACRO version, p
b5ce0 75 74 56 61 72 69 6e 74 33 32 2c 0a 2a 2a 20 69 utVarint32,.** i
b5cf0 73 20 70 72 6f 76 69 64 65 64 20 77 68 69 63 68 s provided which
b5d00 20 69 6e 6c 69 6e 65 73 20 74 68 65 20 73 69 6e inlines the sin
b5d10 67 6c 65 2d 62 79 74 65 20 63 61 73 65 2e 20 20 gle-byte case.
b5d20 41 6c 6c 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 All code should
b5d30 75 73 65 0a 2a 2a 20 74 68 65 20 4d 41 43 52 4f use.** the MACRO
b5d40 20 76 65 72 73 69 6f 6e 20 61 73 20 74 68 69 73 version as this
b5d50 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 function assume
b5d60 73 20 74 68 65 20 73 69 6e 67 6c 65 2d 62 79 74 s the single-byt
b5d70 65 20 63 61 73 65 20 68 61 73 0a 2a 2a 20 61 6c e case has.** al
b5d80 72 65 61 64 79 20 62 65 65 6e 20 68 61 6e 64 6c ready been handl
b5d90 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
b5da0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
b5db0 33 50 75 74 56 61 72 69 6e 74 33 32 28 75 6e 73 3PutVarint32(uns
b5dc0 69 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 75 igned char *p, u
b5dd0 33 32 20 76 29 7b 0a 23 69 66 6e 64 65 66 20 70 32 v){.#ifndef p
b5de0 75 74 56 61 72 69 6e 74 33 32 0a 20 20 69 66 28 utVarint32. if(
b5df0 20 28 76 20 26 20 7e 30 78 37 66 29 3d 3d 30 20 (v & ~0x7f)==0
b5e00 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d 20 76 3b ){. p[0] = v;
b5e10 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 . return 1;.
b5e20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 }.#endif. if(
b5e30 28 76 20 26 20 7e 30 78 33 66 66 66 29 3d 3d 30 (v & ~0x3fff)==0
b5e40 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d 20 28 ){. p[0] = (
b5e50 75 38 29 28 28 76 3e 3e 37 29 20 7c 20 30 78 38 u8)((v>>7) | 0x8
b5e60 30 29 3b 0a 20 20 20 20 70 5b 31 5d 20 3d 20 28 0);. p[1] = (
b5e70 75 38 29 28 76 20 26 20 30 78 37 66 29 3b 0a 20 u8)(v & 0x7f);.
b5e80 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 7d return 2;. }
b5e90 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 . return sqlite
b5ea0 33 50 75 74 56 61 72 69 6e 74 28 70 2c 20 76 29 3PutVarint(p, v)
b5eb0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 ;.}../*.** Read
b5ec0 61 20 36 34 2d 62 69 74 20 76 61 72 69 61 62 6c a 64-bit variabl
b5ed0 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 e-length integer
b5ee0 20 66 72 6f 6d 20 6d 65 6d 6f 72 79 20 73 74 61 from memory sta
b5ef0 72 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a 2a rting at p[0]..*
b5f00 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d * Return the num
b5f10 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 61 ber of bytes rea
b5f20 64 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 73 d. The value is
b5f30 20 73 74 6f 72 65 64 20 69 6e 20 2a 76 2e 0a 2a stored in *v..*
b5f40 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
b5f50 20 75 38 20 73 71 6c 69 74 65 33 47 65 74 56 61 u8 sqlite3GetVa
b5f60 72 69 6e 74 28 63 6f 6e 73 74 20 75 6e 73 69 67 rint(const unsig
b5f70 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 75 36 34 ned char *p, u64
b5f80 20 2a 76 29 7b 0a 20 20 75 33 32 20 61 2c 62 2c *v){. u32 a,b,
b5f90 73 3b 0a 0a 20 20 61 20 3d 20 2a 70 3b 0a 20 20 s;.. a = *p;.
b5fa0 2f 2a 20 61 3a 20 70 30 20 28 75 6e 6d 61 73 6b /* a: p0 (unmask
b5fb0 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 ed) */. if (!(a
b5fc0 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 &0x80)). {.
b5fd0 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 65 74 75 *v = a;. retu
b5fe0 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b rn 1;. }.. p++
b5ff0 3b 0a 20 20 62 20 3d 20 2a 70 3b 0a 20 20 2f 2a ;. b = *p;. /*
b6000 20 62 3a 20 70 31 20 28 75 6e 6d 61 73 6b 65 64 b: p1 (unmasked
b6010 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 30 ) */. if (!(b&0
b6020 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 61 20 x80)). {. a
b6030 26 3d 20 30 78 37 66 3b 0a 20 20 20 20 61 20 3d &= 0x7f;. a =
b6040 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 a<<7;. a |=
b6050 62 3b 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0a 20 b;. *v = a;.
b6060 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 7d return 2;. }
b6070 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 .. p++;. a = a
b6080 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b <<14;. a |= *p;
b6090 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 31 34 20 . /* a: p0<<14
b60a0 7c 20 70 32 20 28 75 6e 6d 61 73 6b 65 64 29 20 | p2 (unmasked)
b60b0 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 38 */. if (!(a&0x8
b60c0 30 29 29 0a 20 20 7b 0a 20 20 20 20 61 20 26 3d 0)). {. a &=
b60d0 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 (0x7f<<14)|(0x7
b60e0 66 29 3b 0a 20 20 20 20 62 20 26 3d 20 30 78 37 f);. b &= 0x7
b60f0 66 3b 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37 3b f;. b = b<<7;
b6100 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 . a |= b;.
b6110 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 65 74 *v = a;. ret
b6120 75 72 6e 20 33 3b 0a 20 20 7d 0a 0a 20 20 2f 2a urn 3;. }.. /*
b6130 20 43 53 45 31 20 66 72 6f 6d 20 62 65 6c 6f 77 CSE1 from below
b6140 20 2a 2f 0a 20 20 61 20 26 3d 20 28 30 78 37 66 */. a &= (0x7f
b6150 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 <<14)|(0x7f);.
b6160 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 p++;. b = b<<14
b6170 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f ;. b |= *p;. /
b6180 2a 20 62 3a 20 70 31 3c 3c 31 34 20 7c 20 70 33 * b: p1<<14 | p3
b6190 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 (unmasked) */.
b61a0 20 69 66 20 28 21 28 62 26 30 78 38 30 29 29 0a if (!(b&0x80)).
b61b0 20 20 7b 0a 20 20 20 20 62 20 26 3d 20 28 30 78 {. b &= (0x
b61c0 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 7f<<14)|(0x7f);.
b61d0 20 20 20 20 2f 2a 20 6d 6f 76 65 64 20 43 53 45 /* moved CSE
b61e0 31 20 75 70 20 2a 2f 0a 20 20 20 20 2f 2a 20 61 1 up */. /* a
b61f0 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 &= (0x7f<<14)|(
b6200 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 61 20 0x7f); */. a
b6210 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d = a<<7;. a |=
b6220 20 62 3b 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0a b;. *v = a;.
b6230 20 20 20 20 72 65 74 75 72 6e 20 34 3b 0a 20 20 return 4;.
b6240 7d 0a 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 31 }.. /* a: p0<<1
b6250 34 20 7c 20 70 32 20 28 6d 61 73 6b 65 64 29 20 4 | p2 (masked)
b6260 2a 2f 0a 20 20 2f 2a 20 62 3a 20 70 31 3c 3c 31 */. /* b: p1<<1
b6270 34 20 7c 20 70 33 20 28 75 6e 6d 61 73 6b 65 64 4 | p3 (unmasked
b6280 29 20 2a 2f 0a 20 20 2f 2a 20 31 3a 73 61 76 65 ) */. /* 1:save
b6290 20 6f 66 66 20 70 30 3c 3c 32 31 20 7c 20 70 31 off p0<<21 | p1
b62a0 3c 3c 31 34 20 7c 20 70 32 3c 3c 37 20 7c 20 70 <<14 | p2<<7 | p
b62b0 33 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 3 (masked) */.
b62c0 2f 2a 20 6d 6f 76 65 64 20 43 53 45 31 20 75 70 /* moved CSE1 up
b62d0 20 2a 2f 0a 20 20 2f 2a 20 61 20 26 3d 20 28 30 */. /* a &= (0
b62e0 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b x7f<<14)|(0x7f);
b62f0 20 2a 2f 0a 20 20 62 20 26 3d 20 28 30 78 37 66 */. b &= (0x7f
b6300 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 <<14)|(0x7f);.
b6310 73 20 3d 20 61 3b 0a 20 20 2f 2a 20 73 3a 20 70 s = a;. /* s: p
b6320 30 3c 3c 31 34 20 7c 20 70 32 20 28 6d 61 73 6b 0<<14 | p2 (mask
b6330 65 64 29 20 2a 2f 0a 0a 20 20 70 2b 2b 3b 0a 20 ed) */.. p++;.
b6340 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 20 a = a<<14;. a
b6350 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 |= *p;. /* a: p
b6360 30 3c 3c 32 38 20 7c 20 70 32 3c 3c 31 34 20 7c 0<<28 | p2<<14 |
b6370 20 70 34 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a p4 (unmasked) *
b6380 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 38 30 /. if (!(a&0x80
b6390 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 77 65 )). {. /* we
b63a0 20 63 61 6e 20 73 6b 69 70 20 74 68 65 73 65 20 can skip these
b63b0 63 61 75 73 65 20 74 68 65 79 20 77 65 72 65 20 cause they were
b63c0 28 65 66 66 65 63 74 69 76 65 6c 79 29 20 64 6f (effectively) do
b63d0 6e 65 20 61 62 6f 76 65 20 69 6e 20 63 61 6c 63 ne above in calc
b63e0 27 69 6e 67 20 73 20 2a 2f 0a 20 20 20 20 2f 2a 'ing s */. /*
b63f0 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 32 38 29 a &= (0x7f<<28)
b6400 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 |(0x7f<<14)|(0x7
b6410 66 29 3b 20 2a 2f 0a 20 20 20 20 2f 2a 20 62 20 f); */. /* b
b6420 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 &= (0x7f<<14)|(0
b6430 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 62 20 3d x7f); */. b =
b6440 20 62 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 b<<7;. a |=
b6450 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e 31 38 b;. s = s>>18
b6460 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75 36 34 ;. *v = ((u64
b6470 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 20 )s)<<32 | a;.
b6480 20 72 65 74 75 72 6e 20 35 3b 0a 20 20 7d 0a 0a return 5;. }..
b6490 20 20 2f 2a 20 32 3a 73 61 76 65 20 6f 66 66 20 /* 2:save off
b64a0 70 30 3c 3c 32 31 20 7c 20 70 31 3c 3c 31 34 20 p0<<21 | p1<<14
b64b0 7c 20 70 32 3c 3c 37 20 7c 20 70 33 20 28 6d 61 | p2<<7 | p3 (ma
b64c0 73 6b 65 64 29 20 2a 2f 0a 20 20 73 20 3d 20 73 sked) */. s = s
b64d0 3c 3c 37 3b 0a 20 20 73 20 7c 3d 20 62 3b 0a 20 <<7;. s |= b;.
b64e0 20 2f 2a 20 73 3a 20 70 30 3c 3c 32 31 20 7c 20 /* s: p0<<21 |
b64f0 70 31 3c 3c 31 34 20 7c 20 70 32 3c 3c 37 20 7c p1<<14 | p2<<7 |
b6500 20 70 33 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a p3 (masked) */.
b6510 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c . p++;. b = b<
b6520 3c 31 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a <14;. b |= *p;.
b6530 20 20 2f 2a 20 62 3a 20 70 31 3c 3c 32 38 20 7c /* b: p1<<28 |
b6540 20 70 33 3c 3c 31 34 20 7c 20 70 35 20 28 75 6e p3<<14 | p5 (un
b6550 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 masked) */. if
b6560 28 21 28 62 26 30 78 38 30 29 29 0a 20 20 7b 0a (!(b&0x80)). {.
b6570 20 20 20 20 2f 2a 20 77 65 20 63 61 6e 20 73 6b /* we can sk
b6580 69 70 20 74 68 69 73 20 63 61 75 73 65 20 69 74 ip this cause it
b6590 20 77 61 73 20 28 65 66 66 65 63 74 69 76 65 6c was (effectivel
b65a0 79 29 20 64 6f 6e 65 20 61 62 6f 76 65 20 69 6e y) done above in
b65b0 20 63 61 6c 63 27 69 6e 67 20 73 20 2a 2f 0a 20 calc'ing s */.
b65c0 20 20 20 2f 2a 20 62 20 26 3d 20 28 30 78 37 66 /* b &= (0x7f
b65d0 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 <<28)|(0x7f<<14)
b65e0 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 |(0x7f); */.
b65f0 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c a &= (0x7f<<14)|
b6600 28 30 78 37 66 29 3b 0a 20 20 20 20 61 20 3d 20 (0x7f);. a =
b6610 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 a<<7;. a |= b
b6620 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e 31 38 3b ;. s = s>>18;
b6630 0a 20 20 20 20 2a 76 20 3d 20 28 28 75 36 34 29 . *v = ((u64)
b6640 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 20 20 s)<<32 | a;.
b6650 72 65 74 75 72 6e 20 36 3b 0a 20 20 7d 0a 0a 20 return 6;. }..
b6660 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 p++;. a = a<<1
b6670 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 4;. a |= *p;.
b6680 2f 2a 20 61 3a 20 70 32 3c 3c 32 38 20 7c 20 70 /* a: p2<<28 | p
b6690 34 3c 3c 31 34 20 7c 20 70 36 20 28 75 6e 6d 61 4<<14 | p6 (unma
b66a0 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 sked) */. if (!
b66b0 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 (a&0x80)). {.
b66c0 20 20 61 20 26 3d 20 28 30 78 31 66 3c 3c 32 38 a &= (0x1f<<28
b66d0 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 )|(0x7f<<14)|(0x
b66e0 37 66 29 3b 0a 20 20 20 20 62 20 26 3d 20 28 30 7f);. b &= (0
b66f0 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b x7f<<14)|(0x7f);
b6700 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 . b = b<<7;.
b6710 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20 73 a |= b;. s
b6720 20 3d 20 73 3e 3e 31 31 3b 0a 20 20 20 20 2a 76 = s>>11;. *v
b6730 20 3d 20 28 28 75 36 34 29 73 29 3c 3c 33 32 20 = ((u64)s)<<32
b6740 7c 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 | a;. return
b6750 37 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 53 45 7;. }.. /* CSE
b6760 32 20 66 72 6f 6d 20 62 65 6c 6f 77 20 2a 2f 0a 2 from below */.
b6770 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 a &= (0x7f<<14
b6780 29 7c 28 30 78 37 66 29 3b 0a 20 20 70 2b 2b 3b )|(0x7f);. p++;
b6790 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 20 . b = b<<14;.
b67a0 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a b |= *p;. /* b:
b67b0 20 70 33 3c 3c 32 38 20 7c 20 70 35 3c 3c 31 34 p3<<28 | p5<<14
b67c0 20 7c 20 70 37 20 28 75 6e 6d 61 73 6b 65 64 29 | p7 (unmasked)
b67d0 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 30 78 */. if (!(b&0x
b67e0 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 62 20 26 80)). {. b &
b67f0 3d 20 28 30 78 31 66 3c 3c 32 38 29 7c 28 30 78 = (0x1f<<28)|(0x
b6800 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 7f<<14)|(0x7f);.
b6810 20 20 20 20 2f 2a 20 6d 6f 76 65 64 20 43 53 45 /* moved CSE
b6820 32 20 75 70 20 2a 2f 0a 20 20 20 20 2f 2a 20 61 2 up */. /* a
b6830 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 &= (0x7f<<14)|(
b6840 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 61 20 0x7f); */. a
b6850 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d = a<<7;. a |=
b6860 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e 34 b;. s = s>>4
b6870 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75 36 34 ;. *v = ((u64
b6880 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 20 )s)<<32 | a;.
b6890 20 72 65 74 75 72 6e 20 38 3b 0a 20 20 7d 0a 0a return 8;. }..
b68a0 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c p++;. a = a<<
b68b0 31 35 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 15;. a |= *p;.
b68c0 20 2f 2a 20 61 3a 20 70 34 3c 3c 32 39 20 7c 20 /* a: p4<<29 |
b68d0 70 36 3c 3c 31 35 20 7c 20 70 38 20 28 75 6e 6d p6<<15 | p8 (unm
b68e0 61 73 6b 65 64 29 20 2a 2f 0a 0a 20 20 2f 2a 20 asked) */.. /*
b68f0 6d 6f 76 65 64 20 43 53 45 32 20 75 70 20 2a 2f moved CSE2 up */
b6900 0a 20 20 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 . /* a &= (0x7f
b6910 3c 3c 32 39 29 7c 28 30 78 37 66 3c 3c 31 35 29 <<29)|(0x7f<<15)
b6920 7c 28 30 78 66 66 29 3b 20 2a 2f 0a 20 20 62 20 |(0xff); */. b
b6930 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 &= (0x7f<<14)|(0
b6940 78 37 66 29 3b 0a 20 20 62 20 3d 20 62 3c 3c 38 x7f);. b = b<<8
b6950 3b 0a 20 20 61 20 7c 3d 20 62 3b 0a 0a 20 20 73 ;. a |= b;.. s
b6960 20 3d 20 73 3c 3c 34 3b 0a 20 20 62 20 3d 20 70 = s<<4;. b = p
b6970 5b 2d 34 5d 3b 0a 20 20 62 20 26 3d 20 30 78 37 [-4];. b &= 0x7
b6980 66 3b 0a 20 20 62 20 3d 20 62 3e 3e 33 3b 0a 20 f;. b = b>>3;.
b6990 20 73 20 7c 3d 20 62 3b 0a 0a 20 20 2a 76 20 3d s |= b;.. *v =
b69a0 20 28 28 75 36 34 29 73 29 3c 3c 33 32 20 7c 20 ((u64)s)<<32 |
b69b0 61 3b 0a 0a 20 20 72 65 74 75 72 6e 20 39 3b 0a a;.. return 9;.
b69c0 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 20 }../*.** Read a
b69d0 33 32 2d 62 69 74 20 76 61 72 69 61 62 6c 65 2d 32-bit variable-
b69e0 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 66 length integer f
b69f0 72 6f 6d 20 6d 65 6d 6f 72 79 20 73 74 61 72 74 rom memory start
b6a00 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a 2a 2a 20 ing at p[0]..**
b6a10 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 Return the numbe
b6a20 72 20 6f 66 20 62 79 74 65 73 20 72 65 61 64 2e r of bytes read.
b6a30 20 20 54 68 65 20 76 61 6c 75 65 20 69 73 20 73 The value is s
b6a40 74 6f 72 65 64 20 69 6e 20 2a 76 2e 0a 2a 2a 0a tored in *v..**.
b6a50 2a 2a 20 49 66 20 74 68 65 20 76 61 72 69 6e 74 ** If the varint
b6a60 20 73 74 6f 72 65 64 20 69 6e 20 70 5b 30 5d 20 stored in p[0]
b6a70 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 63 is larger than c
b6a80 61 6e 20 66 69 74 20 69 6e 20 61 20 33 32 2d 62 an fit in a 32-b
b6a90 69 74 20 75 6e 73 69 67 6e 65 64 0a 2a 2a 20 69 it unsigned.** i
b6aa0 6e 74 65 67 65 72 2c 20 74 68 65 6e 20 73 65 74 nteger, then set
b6ab0 20 2a 76 20 74 6f 20 30 78 66 66 66 66 66 66 66 *v to 0xfffffff
b6ac0 66 2e 0a 2a 2a 0a 2a 2a 20 41 20 4d 41 43 52 4f f..**.** A MACRO
b6ad0 20 76 65 72 73 69 6f 6e 2c 20 67 65 74 56 61 72 version, getVar
b6ae0 69 6e 74 33 32 2c 20 69 73 20 70 72 6f 76 69 64 int32, is provid
b6af0 65 64 20 77 68 69 63 68 20 69 6e 6c 69 6e 65 73 ed which inlines
b6b00 20 74 68 65 20 0a 2a 2a 20 73 69 6e 67 6c 65 2d the .** single-
b6b10 62 79 74 65 20 63 61 73 65 2e 20 20 41 6c 6c 20 byte case. All
b6b20 63 6f 64 65 20 73 68 6f 75 6c 64 20 75 73 65 20 code should use
b6b30 74 68 65 20 4d 41 43 52 4f 20 76 65 72 73 69 6f the MACRO versio
b6b40 6e 20 61 73 20 0a 2a 2a 20 74 68 69 73 20 66 75 n as .** this fu
b6b50 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 nction assumes t
b6b60 68 65 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 he single-byte c
b6b70 61 73 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 ase has already
b6b80 62 65 65 6e 20 68 61 6e 64 6c 65 64 2e 0a 2a 2f been handled..*/
b6b90 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
b6ba0 75 38 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 u8 sqlite3GetVar
b6bb0 69 6e 74 33 32 28 63 6f 6e 73 74 20 75 6e 73 69 int32(const unsi
b6bc0 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 75 33 gned char *p, u3
b6bd0 32 20 2a 76 29 7b 0a 20 20 75 33 32 20 61 2c 62 2 *v){. u32 a,b
b6be0 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 31 2d 62 79 ;.. /* The 1-by
b6bf0 74 65 20 63 61 73 65 2e 20 20 4f 76 65 72 77 68 te case. Overwh
b6c00 65 6c 6d 69 6e 67 6c 79 20 74 68 65 20 6d 6f 73 elmingly the mos
b6c10 74 20 63 6f 6d 6d 6f 6e 2e 20 20 48 61 6e 64 6c t common. Handl
b6c20 65 64 20 69 6e 6c 69 6e 65 0a 20 20 2a 2a 20 62 ed inline. ** b
b6c30 79 20 74 68 65 20 67 65 74 56 61 72 69 6e 33 32 y the getVarin32
b6c40 28 29 20 6d 61 63 72 6f 20 2a 2f 0a 20 20 61 20 () macro */. a
b6c50 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 = *p;. /* a: p0
b6c60 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 23 (unmasked) */.#
b6c70 69 66 6e 64 65 66 20 67 65 74 56 61 72 69 6e 74 ifndef getVarint
b6c80 33 32 0a 20 20 69 66 20 28 21 28 61 26 30 78 38 32. if (!(a&0x8
b6c90 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 56 0)). {. /* V
b6ca0 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20 30 20 alues between 0
b6cb0 61 6e 64 20 31 32 37 20 2a 2f 0a 20 20 20 20 2a and 127 */. *
b6cc0 76 20 3d 20 61 3b 0a 20 20 20 20 72 65 74 75 72 v = a;. retur
b6cd0 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a n 1;. }.#endif.
b6ce0 0a 20 20 2f 2a 20 54 68 65 20 32 2d 62 79 74 65 . /* The 2-byte
b6cf0 20 63 61 73 65 20 2a 2f 0a 20 20 70 2b 2b 3b 0a case */. p++;.
b6d00 20 20 62 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 b = *p;. /* b
b6d10 3a 20 70 31 20 28 75 6e 6d 61 73 6b 65 64 29 20 : p1 (unmasked)
b6d20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 30 78 38 */. if (!(b&0x8
b6d30 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 56 0)). {. /* V
b6d40 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20 31 32 alues between 12
b6d50 38 20 61 6e 64 20 31 36 33 38 33 20 2a 2f 0a 20 8 and 16383 */.
b6d60 20 20 20 61 20 26 3d 20 30 78 37 66 3b 0a 20 20 a &= 0x7f;.
b6d70 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 a = a<<7;.
b6d80 2a 76 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20 *v = a | b;.
b6d90 72 65 74 75 72 6e 20 32 3b 0a 20 20 7d 0a 0a 20 return 2;. }..
b6da0 20 2f 2a 20 54 68 65 20 33 2d 62 79 74 65 20 63 /* The 3-byte c
b6db0 61 73 65 20 2a 2f 0a 20 20 70 2b 2b 3b 0a 20 20 ase */. p++;.
b6dc0 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 20 7c a = a<<14;. a |
b6dd0 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 = *p;. /* a: p0
b6de0 3c 3c 31 34 20 7c 20 70 32 20 28 75 6e 6d 61 73 <<14 | p2 (unmas
b6df0 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 ked) */. if (!(
b6e00 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 a&0x80)). {.
b6e10 20 2f 2a 20 56 61 6c 75 65 73 20 62 65 74 77 65 /* Values betwe
b6e20 65 6e 20 31 36 33 38 34 20 61 6e 64 20 32 30 39 en 16384 and 209
b6e30 37 31 35 31 20 2a 2f 0a 20 20 20 20 61 20 26 3d 7151 */. a &=
b6e40 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 (0x7f<<14)|(0x7
b6e50 66 29 3b 0a 20 20 20 20 62 20 26 3d 20 30 78 37 f);. b &= 0x7
b6e60 66 3b 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37 3b f;. b = b<<7;
b6e70 0a 20 20 20 20 2a 76 20 3d 20 61 20 7c 20 62 3b . *v = a | b;
b6e80 0a 20 20 20 20 72 65 74 75 72 6e 20 33 3b 0a 20 . return 3;.
b6e90 20 7d 0a 0a 20 20 2f 2a 20 41 20 33 32 2d 62 69 }.. /* A 32-bi
b6ea0 74 20 76 61 72 69 6e 74 20 69 73 20 75 73 65 64 t varint is used
b6eb0 20 74 6f 20 73 74 6f 72 65 20 73 69 7a 65 20 69 to store size i
b6ec0 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 62 74 nformation in bt
b6ed0 72 65 65 73 2e 0a 20 20 2a 2a 20 4f 62 6a 65 63 rees.. ** Objec
b6ee0 74 73 20 61 72 65 20 72 61 72 65 6c 79 20 6c 61 ts are rarely la
b6ef0 72 67 65 72 20 74 68 61 6e 20 32 4d 69 42 20 6c rger than 2MiB l
b6f00 69 6d 69 74 20 6f 66 20 61 20 33 2d 62 79 74 65 imit of a 3-byte
b6f10 20 76 61 72 69 6e 74 2e 0a 20 20 2a 2a 20 41 20 varint.. ** A
b6f20 33 2d 62 79 74 65 20 76 61 72 69 6e 74 20 69 73 3-byte varint is
b6f30 20 73 75 66 66 69 63 69 65 6e 74 2c 20 66 6f 72 sufficient, for
b6f40 20 65 78 61 6d 70 6c 65 2c 20 74 6f 20 72 65 63 example, to rec
b6f50 6f 72 64 20 74 68 65 20 73 69 7a 65 0a 20 20 2a ord the size. *
b6f60 2a 20 6f 66 20 61 20 31 30 34 38 35 36 39 2d 62 * of a 1048569-b
b6f70 79 74 65 20 42 4c 4f 42 20 6f 72 20 73 74 72 69 yte BLOB or stri
b6f80 6e 67 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 65 ng.. **. ** We
b6f90 20 6f 6e 6c 79 20 75 6e 72 6f 6c 6c 20 74 68 65 only unroll the
b6fa0 20 66 69 72 73 74 20 31 2d 2c 20 32 2d 2c 20 61 first 1-, 2-, a
b6fb0 6e 64 20 33 2d 20 62 79 74 65 20 63 61 73 65 73 nd 3- byte cases
b6fc0 2e 20 20 54 68 65 20 76 65 72 79 0a 20 20 2a 2a . The very. **
b6fd0 20 72 61 72 65 20 6c 61 72 67 65 72 20 63 61 73 rare larger cas
b6fe0 65 73 20 63 61 6e 20 62 65 20 68 61 6e 64 6c 65 es can be handle
b6ff0 64 20 62 79 20 74 68 65 20 73 6c 6f 77 65 72 20 d by the slower
b7000 36 34 2d 62 69 74 20 76 61 72 69 6e 74 0a 20 20 64-bit varint.
b7010 2a 2a 20 72 6f 75 74 69 6e 65 2e 0a 20 20 2a 2f ** routine.. */
b7020 0a 23 69 66 20 31 0a 20 20 7b 0a 20 20 20 20 75 .#if 1. {. u
b7030 36 34 20 76 36 34 3b 0a 20 20 20 20 75 38 20 6e 64 v64;. u8 n
b7040 3b 0a 0a 20 20 20 20 70 20 2d 3d 20 32 3b 0a 20 ;.. p -= 2;.
b7050 20 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 47 65 n = sqlite3Ge
b7060 74 56 61 72 69 6e 74 28 70 2c 20 26 76 36 34 29 tVarint(p, &v64)
b7070 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e ;. assert( n>
b7080 33 20 26 26 20 6e 3c 3d 39 20 29 3b 0a 20 20 20 3 && n<=9 );.
b7090 20 69 66 28 20 28 76 36 34 20 26 20 53 51 4c 49 if( (v64 & SQLI
b70a0 54 45 5f 4d 41 58 5f 55 33 32 29 21 3d 76 36 34 TE_MAX_U32)!=v64
b70b0 20 29 7b 0a 20 20 20 20 20 20 2a 76 20 3d 20 30 ){. *v = 0
b70c0 78 66 66 66 66 66 66 66 66 3b 0a 20 20 20 20 7d xffffffff;. }
b70d0 65 6c 73 65 7b 0a 20 20 20 20 20 20 2a 76 20 3d else{. *v =
b70e0 20 28 75 33 32 29 76 36 34 3b 0a 20 20 20 20 7d (u32)v64;. }
b70f0 0a 20 20 20 20 72 65 74 75 72 6e 20 6e 3b 0a 20 . return n;.
b7100 20 7d 0a 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 46 }..#else. /* F
b7110 6f 72 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 or following cod
b7120 65 20 28 6b 65 70 74 20 66 6f 72 20 68 69 73 74 e (kept for hist
b7130 6f 72 69 63 61 6c 20 72 65 63 6f 72 64 20 6f 6e orical record on
b7140 6c 79 29 20 73 68 6f 77 73 20 61 6e 0a 20 20 2a ly) shows an. *
b7150 2a 20 75 6e 72 6f 6c 6c 69 6e 67 20 66 6f 72 20 * unrolling for
b7160 74 68 65 20 33 2d 20 61 6e 64 20 34 2d 62 79 74 the 3- and 4-byt
b7170 65 20 76 61 72 69 6e 74 20 63 61 73 65 73 2e 20 e varint cases.
b7180 20 54 68 69 73 20 63 6f 64 65 20 69 73 0a 20 20 This code is.
b7190 2a 2a 20 73 6c 69 67 68 74 6c 79 20 66 61 73 74 ** slightly fast
b71a0 65 72 2c 20 62 75 74 20 69 74 20 69 73 20 61 6c er, but it is al
b71b0 73 6f 20 6c 61 72 67 65 72 20 61 6e 64 20 6d 75 so larger and mu
b71c0 63 68 20 68 61 72 64 65 72 20 74 6f 20 74 65 73 ch harder to tes
b71d0 74 2e 0a 20 20 2a 2f 0a 20 20 70 2b 2b 3b 0a 20 t.. */. p++;.
b71e0 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 20 62 20 b = b<<14;. b
b71f0 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 |= *p;. /* b: p
b7200 31 3c 3c 31 34 20 7c 20 70 33 20 28 75 6e 6d 61 1<<14 | p3 (unma
b7210 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 sked) */. if (!
b7220 28 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 (b&0x80)). {.
b7230 20 20 2f 2a 20 56 61 6c 75 65 73 20 62 65 74 77 /* Values betw
b7240 65 65 6e 20 32 30 39 37 31 35 32 20 61 6e 64 20 een 2097152 and
b7250 32 36 38 34 33 35 34 35 35 20 2a 2f 0a 20 20 20 268435455 */.
b7260 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 b &= (0x7f<<14)
b7270 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 61 20 26 |(0x7f);. a &
b7280 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 = (0x7f<<14)|(0x
b7290 37 66 29 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c 7f);. a = a<<
b72a0 37 3b 0a 20 20 20 20 2a 76 20 3d 20 61 20 7c 20 7;. *v = a |
b72b0 62 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 34 3b b;. return 4;
b72c0 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 . }.. p++;. a
b72d0 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d = a<<14;. a |=
b72e0 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 3c *p;. /* a: p0<
b72f0 3c 32 38 20 7c 20 70 32 3c 3c 31 34 20 7c 20 70 <28 | p2<<14 | p
b7300 34 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 4 (unmasked) */.
b7310 20 20 69 66 20 28 21 28 61 26 30 78 38 30 29 29 if (!(a&0x80))
b7320 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 57 61 6c 75 . {. /* Walu
b7330 65 73 20 20 62 65 74 77 65 65 6e 20 32 36 38 34 es between 2684
b7340 33 35 34 35 36 20 61 6e 64 20 33 34 33 35 39 37 35456 and 343597
b7350 33 38 33 36 37 20 2a 2f 0a 20 20 20 20 61 20 26 38367 */. a &
b7360 3d 20 28 30 78 31 66 3c 3c 32 38 29 7c 28 30 78 = (0x1f<<28)|(0x
b7370 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 7f<<14)|(0x7f);.
b7380 20 20 20 20 62 20 26 3d 20 28 30 78 31 66 3c 3c b &= (0x1f<<
b7390 32 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 28)|(0x7f<<14)|(
b73a0 30 78 37 66 29 3b 0a 20 20 20 20 62 20 3d 20 62 0x7f);. b = b
b73b0 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20 61 20 <<7;. *v = a
b73c0 7c 20 62 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 | b;. return
b73d0 35 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 65 20 5;. }.. /* We
b73e0 63 61 6e 20 6f 6e 6c 79 20 72 65 61 63 68 20 74 can only reach t
b73f0 68 69 73 20 70 6f 69 6e 74 20 77 68 65 6e 20 72 his point when r
b7400 65 61 64 69 6e 67 20 61 20 63 6f 72 72 75 70 74 eading a corrupt
b7410 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 66 database. ** f
b7420 69 6c 65 2e 20 20 49 6e 20 74 68 61 74 20 63 61 ile. In that ca
b7430 73 65 20 77 65 20 61 72 65 20 6e 6f 74 20 69 6e se we are not in
b7440 20 61 6e 79 20 68 75 72 72 79 2e 20 20 55 73 65 any hurry. Use
b7450 20 74 68 65 20 28 72 65 6c 61 74 69 76 65 6c 79 the (relatively
b7460 0a 20 20 2a 2a 20 73 6c 6f 77 29 20 67 65 6e 65 . ** slow) gene
b7470 72 61 6c 2d 70 75 72 70 6f 73 65 20 73 71 6c 69 ral-purpose sqli
b7480 74 65 33 47 65 74 56 61 72 69 6e 74 28 29 20 72 te3GetVarint() r
b7490 6f 75 74 69 6e 65 20 74 6f 20 65 78 74 72 61 63 outine to extrac
b74a0 74 20 74 68 65 0a 20 20 2a 2a 20 76 61 6c 75 65 t the. ** value
b74b0 2e 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 75 36 34 . */. {. u64
b74c0 20 76 36 34 3b 0a 20 20 20 20 75 38 20 6e 3b 0a v64;. u8 n;.
b74d0 0a 20 20 20 20 70 20 2d 3d 20 34 3b 0a 20 20 20 . p -= 4;.
b74e0 20 6e 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 n = sqlite3GetV
b74f0 61 72 69 6e 74 28 70 2c 20 26 76 36 34 29 3b 0a arint(p, &v64);.
b7500 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e 35 20 assert( n>5
b7510 26 26 20 6e 3c 3d 39 20 29 3b 0a 20 20 20 20 2a && n<=9 );. *
b7520 76 20 3d 20 28 75 33 32 29 76 36 34 3b 0a 20 20 v = (u32)v64;.
b7530 20 20 72 65 74 75 72 6e 20 6e 3b 0a 20 20 7d 0a return n;. }.
b7540 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 #endif.}../*.**
b7550 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 Return the numbe
b7560 72 20 6f 66 20 62 79 74 65 73 20 74 68 61 74 20 r of bytes that
b7570 77 69 6c 6c 20 62 65 20 6e 65 65 64 65 64 20 74 will be needed t
b7580 6f 20 73 74 6f 72 65 20 74 68 65 20 67 69 76 65 o store the give
b7590 6e 0a 2a 2a 20 36 34 2d 62 69 74 20 69 6e 74 65 n.** 64-bit inte
b75a0 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ger..*/.SQLITE_P
b75b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
b75c0 65 33 56 61 72 69 6e 74 4c 65 6e 28 75 36 34 20 e3VarintLen(u64
b75d0 76 29 7b 0a 20 20 69 6e 74 20 69 20 3d 20 30 3b v){. int i = 0;
b75e0 0a 20 20 64 6f 7b 0a 20 20 20 20 69 2b 2b 3b 0a . do{. i++;.
b75f0 20 20 20 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 7d v >>= 7;. }
b7600 77 68 69 6c 65 28 20 76 21 3d 30 20 26 26 20 41 while( v!=0 && A
b7610 4c 57 41 59 53 28 69 3c 39 29 20 29 3b 0a 20 20 LWAYS(i<9) );.
b7620 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 0a 0a 2f 2a return i;.}.../*
b7630 0a 2a 2a 20 52 65 61 64 20 6f 72 20 77 72 69 74 .** Read or writ
b7640 65 20 61 20 66 6f 75 72 2d 62 79 74 65 20 62 69 e a four-byte bi
b7650 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 g-endian integer
b7660 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 value..*/.SQLIT
b7670 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 71 E_PRIVATE u32 sq
b7680 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 63 6f lite3Get4byte(co
b7690 6e 73 74 20 75 38 20 2a 70 29 7b 0a 20 20 72 65 nst u8 *p){. re
b76a0 74 75 72 6e 20 28 70 5b 30 5d 3c 3c 32 34 29 20 turn (p[0]<<24)
b76b0 7c 20 28 70 5b 31 5d 3c 3c 31 36 29 20 7c 20 28 | (p[1]<<16) | (
b76c0 70 5b 32 5d 3c 3c 38 29 20 7c 20 70 5b 33 5d 3b p[2]<<8) | p[3];
b76d0 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
b76e0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 75 E void sqlite3Pu
b76f0 74 34 62 79 74 65 28 75 6e 73 69 67 6e 65 64 20 t4byte(unsigned
b7700 63 68 61 72 20 2a 70 2c 20 75 33 32 20 76 29 7b char *p, u32 v){
b7710 0a 20 20 70 5b 30 5d 20 3d 20 28 75 38 29 28 76 . p[0] = (u8)(v
b7720 3e 3e 32 34 29 3b 0a 20 20 70 5b 31 5d 20 3d 20 >>24);. p[1] =
b7730 28 75 38 29 28 76 3e 3e 31 36 29 3b 0a 20 20 70 (u8)(v>>16);. p
b7740 5b 32 5d 20 3d 20 28 75 38 29 28 76 3e 3e 38 29 [2] = (u8)(v>>8)
b7750 3b 0a 20 20 70 5b 33 5d 20 3d 20 28 75 38 29 76 ;. p[3] = (u8)v
b7760 3b 0a 7d 0a 0a 0a 0a 23 69 66 20 21 64 65 66 69 ;.}....#if !defi
b7770 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
b7780 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 29 20 7c 7c BLOB_LITERAL) ||
b7790 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
b77a0 48 41 53 5f 43 4f 44 45 43 29 0a 2f 2a 0a 2a 2a HAS_CODEC)./*.**
b77b0 20 54 72 61 6e 73 6c 61 74 65 20 61 20 73 69 6e Translate a sin
b77c0 67 6c 65 20 62 79 74 65 20 6f 66 20 48 65 78 20 gle byte of Hex
b77d0 69 6e 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 2e into an integer.
b77e0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
b77f0 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 69 66 20 68 only works if h
b7800 20 72 65 61 6c 6c 79 20 69 73 20 61 20 76 61 6c really is a val
b7810 69 64 20 68 65 78 61 64 65 63 69 6d 61 6c 0a 2a id hexadecimal.*
b7820 2a 20 63 68 61 72 61 63 74 65 72 3a 20 20 30 2e * character: 0.
b7830 2e 39 61 2e 2e 66 41 2e 2e 46 0a 2a 2f 0a 73 74 .9a..fA..F.*/.st
b7840 61 74 69 63 20 75 38 20 68 65 78 54 6f 49 6e 74 atic u8 hexToInt
b7850 28 69 6e 74 20 68 29 7b 0a 20 20 61 73 73 65 72 (int h){. asser
b7860 74 28 20 28 68 3e 3d 27 30 27 20 26 26 20 68 3c t( (h>='0' && h<
b7870 3d 27 39 27 29 20 7c 7c 20 20 28 68 3e 3d 27 61 ='9') || (h>='a
b7880 27 20 26 26 20 68 3c 3d 27 66 27 29 20 7c 7c 20 ' && h<='f') ||
b7890 20 28 68 3e 3d 27 41 27 20 26 26 20 68 3c 3d 27 (h>='A' && h<='
b78a0 46 27 29 20 29 3b 0a 23 69 66 64 65 66 20 53 51 F') );.#ifdef SQ
b78b0 4c 49 54 45 5f 41 53 43 49 49 0a 20 20 68 20 2b LITE_ASCII. h +
b78c0 3d 20 39 2a 28 31 26 28 68 3e 3e 36 29 29 3b 0a = 9*(1&(h>>6));.
b78d0 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 #endif.#ifdef SQ
b78e0 4c 49 54 45 5f 45 42 43 44 49 43 0a 20 20 68 20 LITE_EBCDIC. h
b78f0 2b 3d 20 39 2a 28 31 26 7e 28 68 3e 3e 34 29 29 += 9*(1&~(h>>4))
b7900 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 ;.#endif. retur
b7910 6e 20 28 75 38 29 28 68 20 26 20 30 78 66 29 3b n (u8)(h & 0xf);
b7920 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 .}.#endif /* !SQ
b7930 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c LITE_OMIT_BLOB_L
b7940 49 54 45 52 41 4c 20 7c 7c 20 53 51 4c 49 54 45 ITERAL || SQLITE
b7950 5f 48 41 53 5f 43 4f 44 45 43 20 2a 2f 0a 0a 23 _HAS_CODEC */..#
b7960 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 if !defined(SQLI
b7970 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 TE_OMIT_BLOB_LIT
b7980 45 52 41 4c 29 20 7c 7c 20 64 65 66 69 6e 65 64 ERAL) || defined
b7990 28 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 (SQLITE_HAS_CODE
b79a0 43 29 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 C)./*.** Convert
b79b0 20 61 20 42 4c 4f 42 20 6c 69 74 65 72 61 6c 20 a BLOB literal
b79c0 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 78 27 68 of the form "x'h
b79d0 68 68 68 68 68 27 22 20 69 6e 74 6f 20 69 74 73 hhhhh'" into its
b79e0 20 62 69 6e 61 72 79 0a 2a 2a 20 76 61 6c 75 65 binary.** value
b79f0 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e . Return a poin
b7a00 74 65 72 20 74 6f 20 69 74 73 20 62 69 6e 61 72 ter to its binar
b7a10 79 20 76 61 6c 75 65 2e 20 20 53 70 61 63 65 20 y value. Space
b7a20 74 6f 20 68 6f 6c 64 20 74 68 65 0a 2a 2a 20 62 to hold the.** b
b7a30 69 6e 61 72 79 20 76 61 6c 75 65 20 68 61 73 20 inary value has
b7a40 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 66 72 been obtained fr
b7a50 6f 6d 20 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d 75 om malloc and mu
b7a60 73 74 20 62 65 20 66 72 65 65 64 20 62 79 0a 2a st be freed by.*
b7a70 2a 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f * the calling ro
b7a80 75 74 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 utine..*/.SQLITE
b7a90 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
b7aa0 71 6c 69 74 65 33 48 65 78 54 6f 42 6c 6f 62 28 qlite3HexToBlob(
b7ab0 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e sqlite3 *db, con
b7ac0 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 st char *z, int
b7ad0 6e 29 7b 0a 20 20 63 68 61 72 20 2a 7a 42 6c 6f n){. char *zBlo
b7ae0 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 7a b;. int i;.. z
b7af0 42 6c 6f 62 20 3d 20 28 63 68 61 72 20 2a 29 73 Blob = (char *)s
b7b00 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 qlite3DbMallocRa
b7b10 77 28 64 62 2c 20 6e 2f 32 20 2b 20 31 29 3b 0a w(db, n/2 + 1);.
b7b20 20 20 6e 2d 2d 3b 0a 20 20 69 66 28 20 7a 42 6c n--;. if( zBl
b7b30 6f 62 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d ob ){. for(i=
b7b40 30 3b 20 69 3c 6e 3b 20 69 2b 3d 32 29 7b 0a 20 0; i<n; i+=2){.
b7b50 20 20 20 20 20 7a 42 6c 6f 62 5b 69 2f 32 5d 20 zBlob[i/2]
b7b60 3d 20 28 68 65 78 54 6f 49 6e 74 28 7a 5b 69 5d = (hexToInt(z[i]
b7b70 29 3c 3c 34 29 20 7c 20 68 65 78 54 6f 49 6e 74 )<<4) | hexToInt
b7b80 28 7a 5b 69 2b 31 5d 29 3b 0a 20 20 20 20 7d 0a (z[i+1]);. }.
b7b90 20 20 20 20 7a 42 6c 6f 62 5b 69 2f 32 5d 20 3d zBlob[i/2] =
b7ba0 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 0;. }. return
b7bb0 20 7a 42 6c 6f 62 3b 0a 7d 0a 23 65 6e 64 69 66 zBlob;.}.#endif
b7bc0 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 /* !SQLITE_OMIT
b7bd0 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 20 7c 7c _BLOB_LITERAL ||
b7be0 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 SQLITE_HAS_CODE
b7bf0 43 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 C */.../*.** Cha
b7c00 6e 67 65 20 74 68 65 20 73 71 6c 69 74 65 2e 6d nge the sqlite.m
b7c10 61 67 69 63 20 66 72 6f 6d 20 53 51 4c 49 54 45 agic from SQLITE
b7c20 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 74 6f 20 53 _MAGIC_OPEN to S
b7c30 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 QLITE_MAGIC_BUSY
b7c40 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 ..** Return an e
b7c50 72 72 6f 72 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 rror (non-zero)
b7c60 69 66 20 74 68 65 20 6d 61 67 69 63 20 77 61 73 if the magic was
b7c70 20 6e 6f 74 20 53 51 4c 49 54 45 5f 4d 41 47 49 not SQLITE_MAGI
b7c80 43 5f 4f 50 45 4e 0a 2a 2a 20 77 68 65 6e 20 74 C_OPEN.** when t
b7c90 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 his routine is c
b7ca0 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 alled..**.** Thi
b7cb0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
b7cc0 6c 65 64 20 77 68 65 6e 20 65 6e 74 65 72 69 6e led when enterin
b7cd0 67 20 61 6e 20 53 51 4c 69 74 65 20 41 50 49 2e g an SQLite API.
b7ce0 20 20 54 68 65 20 53 51 4c 49 54 45 5f 4d 41 47 The SQLITE_MAG
b7cf0 49 43 5f 4f 50 45 4e 0a 2a 2a 20 76 61 6c 75 65 IC_OPEN.** value
b7d00 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 indicates that
b7d10 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e the database con
b7d20 6e 65 63 74 69 6f 6e 20 70 61 73 73 65 64 20 69 nection passed i
b7d30 6e 74 6f 20 74 68 65 20 41 50 49 20 69 73 0a 2a nto the API is.*
b7d40 2a 20 6f 70 65 6e 20 61 6e 64 20 69 73 20 6e 6f * open and is no
b7d50 74 20 62 65 69 6e 67 20 75 73 65 64 20 62 79 20 t being used by
b7d60 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 2e 20 another thread.
b7d70 20 42 79 20 63 68 61 6e 67 69 6e 67 20 74 68 65 By changing the
b7d80 20 76 61 6c 75 65 0a 2a 2a 20 74 6f 20 53 51 4c value.** to SQL
b7d90 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 77 ITE_MAGIC_BUSY w
b7da0 65 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 e indicate that
b7db0 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 the connection i
b7dc0 73 20 69 6e 20 75 73 65 2e 0a 2a 2a 20 73 71 6c s in use..** sql
b7dd0 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 29 20 ite3SafetyOff()
b7de0 62 65 6c 6f 77 20 77 69 6c 6c 20 63 68 61 6e 67 below will chang
b7df0 65 20 74 68 65 20 76 61 6c 75 65 20 62 61 63 6b e the value back
b7e00 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 to SQLITE_MAGIC
b7e10 5f 4f 50 45 4e 0a 2a 2a 20 77 68 65 6e 20 74 68 _OPEN.** when th
b7e20 65 20 41 50 49 20 65 78 69 74 73 2e 20 0a 2a 2a e API exits. .**
b7e30 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
b7e40 20 69 73 20 61 20 61 74 74 65 6d 70 74 20 74 6f is a attempt to
b7e50 20 64 65 74 65 63 74 20 69 66 20 74 77 6f 20 74 detect if two t
b7e60 68 72 65 61 64 73 20 75 73 65 20 74 68 65 0a 2a hreads use the.*
b7e70 2a 20 73 61 6d 65 20 73 71 6c 69 74 65 2a 20 70 * same sqlite* p
b7e80 6f 69 6e 74 65 72 20 61 74 20 74 68 65 20 73 61 ointer at the sa
b7e90 6d 65 20 74 69 6d 65 2e 20 20 54 68 65 72 65 20 me time. There
b7ea0 69 73 20 61 20 72 61 63 65 20 0a 2a 2a 20 63 6f is a race .** co
b7eb0 6e 64 69 74 69 6f 6e 20 73 6f 20 69 74 20 69 73 ndition so it is
b7ec0 20 70 6f 73 73 69 62 6c 65 20 74 68 61 74 20 74 possible that t
b7ed0 68 65 20 65 72 72 6f 72 20 69 73 20 6e 6f 74 20 he error is not
b7ee0 64 65 74 65 63 74 65 64 2e 0a 2a 2a 20 42 75 74 detected..** But
b7ef0 20 75 73 75 61 6c 6c 79 20 74 68 65 20 70 72 6f usually the pro
b7f00 62 6c 65 6d 20 77 69 6c 6c 20 62 65 20 73 65 65 blem will be see
b7f10 6e 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20 77 n. The result w
b7f20 69 6c 6c 20 62 65 20 61 6e 0a 2a 2a 20 65 72 72 ill be an.** err
b7f30 6f 72 20 77 68 69 63 68 20 63 61 6e 20 62 65 20 or which can be
b7f40 75 73 65 64 20 74 6f 20 64 65 62 75 67 20 74 68 used to debug th
b7f50 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 74 68 e application th
b7f60 61 74 20 69 73 0a 2a 2a 20 75 73 69 6e 67 20 53 at is.** using S
b7f70 51 4c 69 74 65 20 69 6e 63 6f 72 72 65 63 74 6c QLite incorrectl
b7f80 79 2e 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74 20 y..**.** Ticket
b7f90 23 32 30 32 3a 20 20 49 66 20 64 62 2d 3e 6d 61 #202: If db->ma
b7fa0 67 69 63 20 69 73 20 6e 6f 74 20 61 20 76 61 6c gic is not a val
b7fb0 69 64 20 6f 70 65 6e 20 76 61 6c 75 65 2c 20 74 id open value, t
b7fc0 61 6b 65 20 63 61 72 65 20 6e 6f 74 0a 2a 2a 20 ake care not.**
b7fd0 74 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 64 62 to modify the db
b7fe0 20 73 74 72 75 63 74 75 72 65 20 61 74 20 61 6c structure at al
b7ff0 6c 2e 20 20 49 74 20 63 6f 75 6c 64 20 62 65 20 l. It could be
b8000 74 68 61 74 20 64 62 20 69 73 20 61 20 73 74 61 that db is a sta
b8010 6c 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 2e 20 20 le.** pointer.
b8020 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 In other words,
b8030 69 74 20 63 6f 75 6c 64 20 62 65 20 74 68 61 74 it could be that
b8040 20 74 68 65 72 65 20 68 61 73 20 62 65 65 6e 20 there has been
b8050 61 20 70 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c 20 a prior.** call
b8060 74 6f 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 to sqlite3_close
b8070 28 64 62 29 20 61 6e 64 20 64 62 20 68 61 73 20 (db) and db has
b8080 62 65 65 6e 20 64 65 61 6c 6c 6f 63 61 74 65 64 been deallocated
b8090 2e 20 20 41 6e 64 20 77 65 20 64 6f 0a 2a 2a 20 . And we do.**
b80a0 6e 6f 74 20 77 61 6e 74 20 74 6f 20 77 72 69 74 not want to writ
b80b0 65 20 69 6e 74 6f 20 64 65 61 6c 6c 6f 63 61 74 e into deallocat
b80c0 65 64 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 23 69 ed memory..*/.#i
b80d0 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
b80e0 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 G.SQLITE_PRIVATE
b80f0 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 int sqlite3Safe
b8100 74 79 4f 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 tyOn(sqlite3 *db
b8110 29 7b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 67 ){. if( db->mag
b8120 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 ic==SQLITE_MAGIC
b8130 5f 4f 50 45 4e 20 29 7b 0a 20 20 20 20 64 62 2d _OPEN ){. db-
b8140 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f >magic = SQLITE_
b8150 4d 41 47 49 43 5f 42 55 53 59 3b 0a 20 20 20 20 MAGIC_BUSY;.
b8160 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
b8170 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d mutex_held(db->m
b8180 75 74 65 78 29 20 29 3b 0a 20 20 20 20 72 65 74 utex) );. ret
b8190 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 urn 0;. }else i
b81a0 66 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 f( db->magic==SQ
b81b0 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 LITE_MAGIC_BUSY
b81c0 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69 63 ){. db->magic
b81d0 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f = SQLITE_MAGIC_
b81e0 45 52 52 4f 52 3b 0a 20 20 20 20 64 62 2d 3e 75 ERROR;. db->u
b81f0 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20 1.isInterrupted
b8200 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 = 1;. }. retur
b8210 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f n 1;.}.#endif../
b8220 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 *.** Change the
b8230 6d 61 67 69 63 20 66 72 6f 6d 20 53 51 4c 49 54 magic from SQLIT
b8240 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 74 6f 20 E_MAGIC_BUSY to
b8250 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 SQLITE_MAGIC_OPE
b8260 4e 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20 N..** Return an
b8270 65 72 72 6f 72 20 28 6e 6f 6e 2d 7a 65 72 6f 29 error (non-zero)
b8280 20 69 66 20 74 68 65 20 6d 61 67 69 63 20 77 61 if the magic wa
b8290 73 20 6e 6f 74 20 53 51 4c 49 54 45 5f 4d 41 47 s not SQLITE_MAG
b82a0 49 43 5f 42 55 53 59 0a 2a 2a 20 77 68 65 6e 20 IC_BUSY.** when
b82b0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
b82c0 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 23 69 66 64 65 called..*/.#ifde
b82d0 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 f SQLITE_DEBUG.S
b82e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
b82f0 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f t sqlite3SafetyO
b8300 66 66 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b ff(sqlite3 *db){
b8310 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 67 69 63 . if( db->magic
b8320 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 ==SQLITE_MAGIC_B
b8330 55 53 59 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d USY ){. db->m
b8340 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 agic = SQLITE_MA
b8350 47 49 43 5f 4f 50 45 4e 3b 0a 20 20 20 20 61 73 GIC_OPEN;. as
b8360 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
b8370 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 tex_held(db->mut
b8380 65 78 29 20 29 3b 0a 20 20 20 20 72 65 74 75 72 ex) );. retur
b8390 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 n 0;. }else{.
b83a0 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 db->magic = SQ
b83b0 4c 49 54 45 5f 4d 41 47 49 43 5f 45 52 52 4f 52 LITE_MAGIC_ERROR
b83c0 3b 0a 20 20 20 20 64 62 2d 3e 75 31 2e 69 73 49 ;. db->u1.isI
b83d0 6e 74 65 72 72 75 70 74 65 64 20 3d 20 31 3b 0a nterrupted = 1;.
b83e0 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 return 1;.
b83f0 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a }.}.#endif../*.*
b8400 2a 20 43 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20 * Check to make
b8410 73 75 72 65 20 77 65 20 68 61 76 65 20 61 20 76 sure we have a v
b8420 61 6c 69 64 20 64 62 20 70 6f 69 6e 74 65 72 2e alid db pointer.
b8430 20 20 54 68 69 73 20 74 65 73 74 20 69 73 20 6e This test is n
b8440 6f 74 0a 2a 2a 20 66 6f 6f 6c 70 72 6f 6f 66 20 ot.** foolproof
b8450 62 75 74 20 69 74 20 64 6f 65 73 20 70 72 6f 76 but it does prov
b8460 69 64 65 20 73 6f 6d 65 20 6d 65 61 73 75 72 65 ide some measure
b8470 20 6f 66 20 70 72 6f 74 65 63 74 69 6f 6e 20 61 of protection a
b8480 67 61 69 6e 73 74 0a 2a 2a 20 6d 69 73 75 73 65 gainst.** misuse
b8490 20 6f 66 20 74 68 65 20 69 6e 74 65 72 66 61 63 of the interfac
b84a0 65 20 73 75 63 68 20 61 73 20 70 61 73 73 69 6e e such as passin
b84b0 67 20 69 6e 20 64 62 20 70 6f 69 6e 74 65 72 73 g in db pointers
b84c0 20 74 68 61 74 20 61 72 65 0a 2a 2a 20 4e 55 4c that are.** NUL
b84d0 4c 20 6f 72 20 77 68 69 63 68 20 68 61 76 65 20 L or which have
b84e0 62 65 65 6e 20 70 72 65 76 69 6f 75 73 6c 79 20 been previously
b84f0 63 6c 6f 73 65 64 2e 20 20 49 66 20 74 68 69 73 closed. If this
b8500 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 routine returns
b8510 0a 2a 2a 20 31 20 69 74 20 6d 65 61 6e 73 20 74 .** 1 it means t
b8520 68 61 74 20 74 68 65 20 64 62 20 70 6f 69 6e 74 hat the db point
b8530 65 72 20 69 73 20 76 61 6c 69 64 20 61 6e 64 20 er is valid and
b8540 30 20 69 66 20 69 74 20 73 68 6f 75 6c 64 20 6e 0 if it should n
b8550 6f 74 20 62 65 0a 2a 2a 20 64 65 72 65 66 65 72 ot be.** derefer
b8560 65 6e 63 65 64 20 66 6f 72 20 61 6e 79 20 72 65 enced for any re
b8570 61 73 6f 6e 2e 20 20 54 68 65 20 63 61 6c 6c 69 ason. The calli
b8580 6e 67 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 ng function shou
b8590 6c 64 20 69 6e 76 6f 6b 65 0a 2a 2a 20 53 51 4c ld invoke.** SQL
b85a0 49 54 45 5f 4d 49 53 55 53 45 20 69 6d 6d 65 64 ITE_MISUSE immed
b85b0 69 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 73 71 iately..**.** sq
b85c0 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b lite3SafetyCheck
b85d0 4f 6b 28 29 20 72 65 71 75 69 72 65 73 20 74 68 Ok() requires th
b85e0 61 74 20 74 68 65 20 64 62 20 70 6f 69 6e 74 65 at the db pointe
b85f0 72 20 62 65 20 76 61 6c 69 64 20 66 6f 72 0a 2a r be valid for.*
b8600 2a 20 75 73 65 2e 20 20 73 71 6c 69 74 65 33 53 * use. sqlite3S
b8610 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 afetyCheckSickOr
b8620 4f 6b 28 29 20 61 6c 6c 6f 77 73 20 61 20 64 62 Ok() allows a db
b8630 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20 66 61 pointer that fa
b8640 69 6c 65 64 20 74 6f 0a 2a 2a 20 6f 70 65 6e 20 iled to.** open
b8650 70 72 6f 70 65 72 6c 79 20 61 6e 64 20 69 73 20 properly and is
b8660 6e 6f 74 20 66 69 74 20 66 6f 72 20 67 65 6e 65 not fit for gene
b8670 72 61 6c 20 75 73 65 20 62 75 74 20 77 68 69 63 ral use but whic
b8680 68 20 63 61 6e 20 62 65 0a 2a 2a 20 75 73 65 64 h can be.** used
b8690 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 as an argument
b86a0 74 6f 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 to sqlite3_errms
b86b0 67 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 g() or sqlite3_c
b86c0 6c 6f 73 65 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 lose()..*/.SQLIT
b86d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
b86e0 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b lite3SafetyCheck
b86f0 4f 6b 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b Ok(sqlite3 *db){
b8700 0a 20 20 75 33 32 20 6d 61 67 69 63 3b 0a 20 20 . u32 magic;.
b8710 69 66 28 20 64 62 3d 3d 30 20 29 20 72 65 74 75 if( db==0 ) retu
b8720 72 6e 20 30 3b 0a 20 20 6d 61 67 69 63 20 3d 20 rn 0;. magic =
b8730 64 62 2d 3e 6d 61 67 69 63 3b 0a 20 20 69 66 28 db->magic;. if(
b8740 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d magic!=SQLITE_M
b8750 41 47 49 43 5f 4f 50 45 4e 20 0a 23 69 66 64 65 AGIC_OPEN .#ifde
b8760 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 f SQLITE_DEBUG.
b8770 20 20 20 20 26 26 20 6d 61 67 69 63 21 3d 53 51 && magic!=SQ
b8780 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 0a LITE_MAGIC_BUSY.
b8790 23 65 6e 64 69 66 0a 20 20 29 7b 0a 20 20 20 20 #endif. ){.
b87a0 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 return 0;. }els
b87b0 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b e{. return 1;
b87c0 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 . }.}.SQLITE_PR
b87d0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
b87e0 33 53 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b 3SafetyCheckSick
b87f0 4f 72 4f 6b 28 73 71 6c 69 74 65 33 20 2a 64 62 OrOk(sqlite3 *db
b8800 29 7b 0a 20 20 75 33 32 20 6d 61 67 69 63 3b 0a ){. u32 magic;.
b8810 20 20 6d 61 67 69 63 20 3d 20 64 62 2d 3e 6d 61 magic = db->ma
b8820 67 69 63 3b 0a 20 20 69 66 28 20 6d 61 67 69 63 gic;. if( magic
b8830 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 53 !=SQLITE_MAGIC_S
b8840 49 43 4b 20 26 26 0a 20 20 20 20 20 20 6d 61 67 ICK &&. mag
b8850 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 ic!=SQLITE_MAGIC
b8860 5f 4f 50 45 4e 20 26 26 0a 20 20 20 20 20 20 6d _OPEN &&. m
b8870 61 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47 agic!=SQLITE_MAG
b8880 49 43 5f 42 55 53 59 20 29 20 72 65 74 75 72 6e IC_BUSY ) return
b8890 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 0;. return 1;.
b88a0 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a }../************
b88b0 2a 2a 20 45 6e 64 20 6f 66 20 75 74 69 6c 2e 63 ** End of util.c
b88c0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
b88d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b88e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b88f0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
b8900 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68 61 ** Begin file ha
b8910 73 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a sh.c ***********
b8920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8940 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 */./*.** 2001 Se
b8950 70 74 65 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a 2a ptember 22.**.**
b8960 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
b8970 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
b8980 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
b8990 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
b89a0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
b89b0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
b89c0 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
b89d0 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
b89e0 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
b89f0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
b8a00 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
b8a10 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
b8a20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
b8a30 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
b8a40 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
b8a50 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
b8a60 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
b8a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 *********.** Thi
b8ac0 73 20 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 s is the impleme
b8ad0 6e 74 61 74 69 6f 6e 20 6f 66 20 67 65 6e 65 72 ntation of gener
b8ae0 69 63 20 68 61 73 68 2d 74 61 62 6c 65 73 0a 2a ic hash-tables.*
b8af0 2a 20 75 73 65 64 20 69 6e 20 53 51 4c 69 74 65 * used in SQLite
b8b00 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 61 73 ..**.** $Id: has
b8b10 68 2e 63 2c 76 20 31 2e 33 38 20 32 30 30 39 2f h.c,v 1.38 2009/
b8b20 30 35 2f 30 39 20 32 33 3a 32 39 3a 31 32 20 64 05/09 23:29:12 d
b8b30 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 rh Exp $.*/../*
b8b40 54 75 72 6e 20 62 75 6c 6b 20 6d 65 6d 6f 72 79 Turn bulk memory
b8b50 20 69 6e 74 6f 20 61 20 68 61 73 68 20 74 61 62 into a hash tab
b8b60 6c 65 20 6f 62 6a 65 63 74 20 62 79 20 69 6e 69 le object by ini
b8b70 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 0a 2a 2a tializing the.**
b8b80 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 48 fields of the H
b8b90 61 73 68 20 73 74 72 75 63 74 75 72 65 2e 0a 2a ash structure..*
b8ba0 2a 0a 2a 2a 20 22 70 4e 65 77 22 20 69 73 20 61 *.** "pNew" is a
b8bb0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
b8bc0 68 61 73 68 20 74 61 62 6c 65 20 74 68 61 74 20 hash table that
b8bd0 69 73 20 74 6f 20 62 65 20 69 6e 69 74 69 61 6c is to be initial
b8be0 69 7a 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ized..*/.SQLITE_
b8bf0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
b8c00 69 74 65 33 48 61 73 68 49 6e 69 74 28 48 61 73 ite3HashInit(Has
b8c10 68 20 2a 70 4e 65 77 29 7b 0a 20 20 61 73 73 65 h *pNew){. asse
b8c20 72 74 28 20 70 4e 65 77 21 3d 30 20 29 3b 0a 20 rt( pNew!=0 );.
b8c30 20 70 4e 65 77 2d 3e 66 69 72 73 74 20 3d 20 30 pNew->first = 0
b8c40 3b 0a 20 20 70 4e 65 77 2d 3e 63 6f 75 6e 74 20 ;. pNew->count
b8c50 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 68 74 73 = 0;. pNew->hts
b8c60 69 7a 65 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d ize = 0;. pNew-
b8c70 3e 68 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 20 52 >ht = 0;.}../* R
b8c80 65 6d 6f 76 65 20 61 6c 6c 20 65 6e 74 72 69 65 emove all entrie
b8c90 73 20 66 72 6f 6d 20 61 20 68 61 73 68 20 74 61 s from a hash ta
b8ca0 62 6c 65 2e 20 20 52 65 63 6c 61 69 6d 20 61 6c ble. Reclaim al
b8cb0 6c 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 43 61 6c l memory..** Cal
b8cc0 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 l this routine t
b8cd0 6f 20 64 65 6c 65 74 65 20 61 20 68 61 73 68 20 o delete a hash
b8ce0 74 61 62 6c 65 20 6f 72 20 74 6f 20 72 65 73 65 table or to rese
b8cf0 74 20 61 20 68 61 73 68 20 74 61 62 6c 65 0a 2a t a hash table.*
b8d00 2a 20 74 6f 20 74 68 65 20 65 6d 70 74 79 20 73 * to the empty s
b8d10 74 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tate..*/.SQLITE_
b8d20 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
b8d30 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 48 61 ite3HashClear(Ha
b8d40 73 68 20 2a 70 48 29 7b 0a 20 20 48 61 73 68 45 sh *pH){. HashE
b8d50 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 20 20 lem *elem;
b8d60 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e /* For loopin
b8d70 67 20 6f 76 65 72 20 61 6c 6c 20 65 6c 65 6d 65 g over all eleme
b8d80 6e 74 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65 nts of the table
b8d90 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 */.. assert( p
b8da0 48 21 3d 30 20 29 3b 0a 20 20 65 6c 65 6d 20 3d H!=0 );. elem =
b8db0 20 70 48 2d 3e 66 69 72 73 74 3b 0a 20 20 70 48 pH->first;. pH
b8dc0 2d 3e 66 69 72 73 74 20 3d 20 30 3b 0a 20 20 73 ->first = 0;. s
b8dd0 71 6c 69 74 65 33 5f 66 72 65 65 28 70 48 2d 3e qlite3_free(pH->
b8de0 68 74 29 3b 0a 20 20 70 48 2d 3e 68 74 20 3d 20 ht);. pH->ht =
b8df0 30 3b 0a 20 20 70 48 2d 3e 68 74 73 69 7a 65 20 0;. pH->htsize
b8e00 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 65 6c = 0;. while( el
b8e10 65 6d 20 29 7b 0a 20 20 20 20 48 61 73 68 45 6c em ){. HashEl
b8e20 65 6d 20 2a 6e 65 78 74 5f 65 6c 65 6d 20 3d 20 em *next_elem =
b8e30 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 elem->next;.
b8e40 73 71 6c 69 74 65 33 5f 66 72 65 65 28 65 6c 65 sqlite3_free(ele
b8e50 6d 29 3b 0a 20 20 20 20 65 6c 65 6d 20 3d 20 6e m);. elem = n
b8e60 65 78 74 5f 65 6c 65 6d 3b 0a 20 20 7d 0a 20 20 ext_elem;. }.
b8e70 70 48 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 7d pH->count = 0;.}
b8e80 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 61 73 68 ../*.** The hash
b8e90 69 6e 67 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f ing function..*/
b8ea0 0a 73 74 61 74 69 63 20 75 6e 73 69 67 6e 65 64 .static unsigned
b8eb0 20 69 6e 74 20 73 74 72 48 61 73 68 28 63 6f 6e int strHash(con
b8ec0 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 st char *z, int
b8ed0 6e 4b 65 79 29 7b 0a 20 20 69 6e 74 20 68 20 3d nKey){. int h =
b8ee0 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 4b 0;. assert( nK
b8ef0 65 79 3e 3d 30 20 29 3b 0a 20 20 77 68 69 6c 65 ey>=0 );. while
b8f00 28 20 6e 4b 65 79 20 3e 20 30 20 20 29 7b 0a 20 ( nKey > 0 ){.
b8f10 20 20 20 68 20 3d 20 28 68 3c 3c 33 29 20 5e 20 h = (h<<3) ^
b8f20 68 20 5e 20 73 71 6c 69 74 65 33 55 70 70 65 72 h ^ sqlite3Upper
b8f30 54 6f 4c 6f 77 65 72 5b 28 75 6e 73 69 67 6e 65 ToLower[(unsigne
b8f40 64 20 63 68 61 72 29 2a 7a 2b 2b 5d 3b 0a 20 20 d char)*z++];.
b8f50 20 20 6e 4b 65 79 2d 2d 3b 0a 20 20 7d 0a 20 20 nKey--;. }.
b8f60 72 65 74 75 72 6e 20 68 3b 0a 7d 0a 0a 0a 2f 2a return h;.}.../*
b8f70 20 4c 69 6e 6b 20 70 4e 65 77 20 65 6c 65 6d 65 Link pNew eleme
b8f80 6e 74 20 69 6e 74 6f 20 74 68 65 20 68 61 73 68 nt into the hash
b8f90 20 74 61 62 6c 65 20 70 48 2e 20 20 49 66 20 70 table pH. If p
b8fa0 45 6e 74 72 79 21 3d 30 20 74 68 65 6e 20 61 6c Entry!=0 then al
b8fb0 73 6f 0a 2a 2a 20 69 6e 73 65 72 74 20 70 4e 65 so.** insert pNe
b8fc0 77 20 69 6e 74 6f 20 74 68 65 20 70 45 6e 74 72 w into the pEntr
b8fd0 79 20 68 61 73 68 20 62 75 63 6b 65 74 2e 0a 2a y hash bucket..*
b8fe0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e /.static void in
b8ff0 73 65 72 74 45 6c 65 6d 65 6e 74 28 0a 20 20 48 sertElement(. H
b9000 61 73 68 20 2a 70 48 2c 20 20 20 20 20 20 20 20 ash *pH,
b9010 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6d /* The com
b9020 70 6c 65 74 65 20 68 61 73 68 20 74 61 62 6c 65 plete hash table
b9030 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 5f 68 74 */. struct _ht
b9040 20 2a 70 45 6e 74 72 79 2c 20 20 20 20 2f 2a 20 *pEntry, /*
b9050 54 68 65 20 65 6e 74 72 79 20 69 6e 74 6f 20 77 The entry into w
b9060 68 69 63 68 20 70 4e 65 77 20 69 73 20 69 6e 73 hich pNew is ins
b9070 65 72 74 65 64 20 2a 2f 0a 20 20 48 61 73 68 45 erted */. HashE
b9080 6c 65 6d 20 2a 70 4e 65 77 20 20 20 20 20 20 20 lem *pNew
b9090 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74 /* The element
b90a0 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 to be inserted
b90b0 2a 2f 0a 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d */.){. HashElem
b90c0 20 2a 70 48 65 61 64 3b 20 20 20 20 20 20 20 2f *pHead; /
b90d0 2a 20 46 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 * First element
b90e0 61 6c 72 65 61 64 79 20 69 6e 20 70 45 6e 74 72 already in pEntr
b90f0 79 20 2a 2f 0a 20 20 69 66 28 20 70 45 6e 74 72 y */. if( pEntr
b9100 79 20 29 7b 0a 20 20 20 20 70 48 65 61 64 20 3d y ){. pHead =
b9110 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 20 3f pEntry->count ?
b9120 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3a pEntry->chain :
b9130 20 30 3b 0a 20 20 20 20 70 45 6e 74 72 79 2d 3e 0;. pEntry->
b9140 63 6f 75 6e 74 2b 2b 3b 0a 20 20 20 20 70 45 6e count++;. pEn
b9150 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 70 4e 65 try->chain = pNe
b9160 77 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 w;. }else{.
b9170 70 48 65 61 64 20 3d 20 30 3b 0a 20 20 7d 0a 20 pHead = 0;. }.
b9180 20 69 66 28 20 70 48 65 61 64 20 29 7b 0a 20 20 if( pHead ){.
b9190 20 20 70 4e 65 77 2d 3e 6e 65 78 74 20 3d 20 70 pNew->next = p
b91a0 48 65 61 64 3b 0a 20 20 20 20 70 4e 65 77 2d 3e Head;. pNew->
b91b0 70 72 65 76 20 3d 20 70 48 65 61 64 2d 3e 70 72 prev = pHead->pr
b91c0 65 76 3b 0a 20 20 20 20 69 66 28 20 70 48 65 61 ev;. if( pHea
b91d0 64 2d 3e 70 72 65 76 20 29 7b 20 70 48 65 61 64 d->prev ){ pHead
b91e0 2d 3e 70 72 65 76 2d 3e 6e 65 78 74 20 3d 20 70 ->prev->next = p
b91f0 4e 65 77 3b 20 7d 0a 20 20 20 20 65 6c 73 65 20 New; }. else
b9200 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 70 48 { pH
b9210 2d 3e 66 69 72 73 74 20 3d 20 70 4e 65 77 3b 20 ->first = pNew;
b9220 7d 0a 20 20 20 20 70 48 65 61 64 2d 3e 70 72 65 }. pHead->pre
b9230 76 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 65 6c 73 v = pNew;. }els
b9240 65 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 65 78 e{. pNew->nex
b9250 74 20 3d 20 70 48 2d 3e 66 69 72 73 74 3b 0a 20 t = pH->first;.
b9260 20 20 20 69 66 28 20 70 48 2d 3e 66 69 72 73 74 if( pH->first
b9270 20 29 7b 20 70 48 2d 3e 66 69 72 73 74 2d 3e 70 ){ pH->first->p
b9280 72 65 76 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 rev = pNew; }.
b9290 20 20 70 4e 65 77 2d 3e 70 72 65 76 20 3d 20 30 pNew->prev = 0
b92a0 3b 0a 20 20 20 20 70 48 2d 3e 66 69 72 73 74 20 ;. pH->first
b92b0 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 7d 0a 0a 0a = pNew;. }.}...
b92c0 2f 2a 20 52 65 73 69 7a 65 20 74 68 65 20 68 61 /* Resize the ha
b92d0 73 68 20 74 61 62 6c 65 20 73 6f 20 74 68 61 74 sh table so that
b92e0 20 69 74 20 63 61 6e 74 61 69 6e 73 20 22 6e 65 it cantains "ne
b92f0 77 5f 73 69 7a 65 22 20 62 75 63 6b 65 74 73 2e w_size" buckets.
b9300 0a 2a 2a 0a 2a 2a 20 54 68 65 20 68 61 73 68 20 .**.** The hash
b9310 74 61 62 6c 65 20 6d 69 67 68 74 20 66 61 69 6c table might fail
b9320 20 74 6f 20 72 65 73 69 7a 65 20 69 66 20 73 71 to resize if sq
b9330 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 66 lite3_malloc() f
b9340 61 69 6c 73 20 6f 72 0a 2a 2a 20 69 66 20 74 68 ails or.** if th
b9350 65 20 6e 65 77 20 73 69 7a 65 20 69 73 20 74 68 e new size is th
b9360 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 70 72 e same as the pr
b9370 69 6f 72 20 73 69 7a 65 2e 0a 2a 2a 20 52 65 74 ior size..** Ret
b9380 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20 urn TRUE if the
b9390 72 65 73 69 7a 65 20 6f 63 63 75 72 73 20 61 6e resize occurs an
b93a0 64 20 66 61 6c 73 65 20 69 66 20 6e 6f 74 2e 0a d false if not..
b93b0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 */.static int re
b93c0 68 61 73 68 28 48 61 73 68 20 2a 70 48 2c 20 75 hash(Hash *pH, u
b93d0 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 65 77 5f nsigned int new_
b93e0 73 69 7a 65 29 7b 0a 20 20 73 74 72 75 63 74 20 size){. struct
b93f0 5f 68 74 20 2a 6e 65 77 5f 68 74 3b 20 20 20 20 _ht *new_ht;
b9400 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e /* The n
b9410 65 77 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f ew hash table */
b9420 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 . HashElem *ele
b9430 6d 2c 20 2a 6e 65 78 74 5f 65 6c 65 6d 3b 20 20 m, *next_elem;
b9440 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 /* For looping
b9450 20 6f 76 65 72 20 65 78 69 73 74 69 6e 67 20 65 over existing e
b9460 6c 65 6d 65 6e 74 73 20 2a 2f 0a 0a 23 69 66 20 lements */..#if
b9470 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f SQLITE_MALLOC_SO
b9480 46 54 5f 4c 49 4d 49 54 3e 30 0a 20 20 69 66 28 FT_LIMIT>0. if(
b9490 20 6e 65 77 5f 73 69 7a 65 2a 73 69 7a 65 6f 66 new_size*sizeof
b94a0 28 73 74 72 75 63 74 20 5f 68 74 29 3e 53 51 4c (struct _ht)>SQL
b94b0 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f ITE_MALLOC_SOFT_
b94c0 4c 49 4d 49 54 20 29 7b 0a 20 20 20 20 6e 65 77 LIMIT ){. new
b94d0 5f 73 69 7a 65 20 3d 20 53 51 4c 49 54 45 5f 4d _size = SQLITE_M
b94e0 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 ALLOC_SOFT_LIMIT
b94f0 2f 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f /sizeof(struct _
b9500 68 74 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e ht);. }. if( n
b9510 65 77 5f 73 69 7a 65 3d 3d 70 48 2d 3e 68 74 73 ew_size==pH->hts
b9520 69 7a 65 20 29 20 72 65 74 75 72 6e 20 30 3b 0a ize ) return 0;.
b9530 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 54 68 65 #endif.. /* The
b9540 20 69 6e 61 62 69 6c 69 74 79 20 74 6f 20 61 6c inability to al
b9550 6c 6f 63 61 74 65 73 20 73 70 61 63 65 20 66 6f locates space fo
b9560 72 20 61 20 6c 61 72 67 65 72 20 68 61 73 68 20 r a larger hash
b9570 74 61 62 6c 65 20 69 73 0a 20 20 2a 2a 20 61 20 table is. ** a
b9580 70 65 72 66 6f 72 6d 61 6e 63 65 20 68 69 74 20 performance hit
b9590 62 75 74 20 69 74 20 69 73 20 6e 6f 74 20 61 20 but it is not a
b95a0 66 61 74 61 6c 20 65 72 72 6f 72 2e 20 20 53 6f fatal error. So
b95b0 20 6d 61 72 6b 20 74 68 65 0a 20 20 2a 2a 20 61 mark the. ** a
b95c0 6c 6c 6f 63 61 74 69 6f 6e 20 61 73 20 61 20 62 llocation as a b
b95d0 65 6e 69 67 6e 2e 0a 20 20 2a 2f 0a 20 20 73 71 enign.. */. sq
b95e0 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e lite3BeginBenign
b95f0 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 6e 65 77 5f Malloc();. new_
b9600 68 74 20 3d 20 28 73 74 72 75 63 74 20 5f 68 74 ht = (struct _ht
b9610 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 *)sqlite3Malloc
b9620 28 20 6e 65 77 5f 73 69 7a 65 2a 73 69 7a 65 6f ( new_size*sizeo
b9630 66 28 73 74 72 75 63 74 20 5f 68 74 29 20 29 3b f(struct _ht) );
b9640 0a 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e . sqlite3EndBen
b9650 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 0a 20 20 ignMalloc();..
b9660 69 66 28 20 6e 65 77 5f 68 74 3d 3d 30 20 29 20 if( new_ht==0 )
b9670 72 65 74 75 72 6e 20 30 3b 0a 20 20 73 71 6c 69 return 0;. sqli
b9680 74 65 33 5f 66 72 65 65 28 70 48 2d 3e 68 74 29 te3_free(pH->ht)
b9690 3b 0a 20 20 70 48 2d 3e 68 74 20 3d 20 6e 65 77 ;. pH->ht = new
b96a0 5f 68 74 3b 0a 20 20 70 48 2d 3e 68 74 73 69 7a _ht;. pH->htsiz
b96b0 65 20 3d 20 6e 65 77 5f 73 69 7a 65 20 3d 20 73 e = new_size = s
b96c0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 qlite3MallocSize
b96d0 28 6e 65 77 5f 68 74 29 2f 73 69 7a 65 6f 66 28 (new_ht)/sizeof(
b96e0 73 74 72 75 63 74 20 5f 68 74 29 3b 0a 20 20 6d struct _ht);. m
b96f0 65 6d 73 65 74 28 6e 65 77 5f 68 74 2c 20 30 2c emset(new_ht, 0,
b9700 20 6e 65 77 5f 73 69 7a 65 2a 73 69 7a 65 6f 66 new_size*sizeof
b9710 28 73 74 72 75 63 74 20 5f 68 74 29 29 3b 0a 20 (struct _ht));.
b9720 20 66 6f 72 28 65 6c 65 6d 3d 70 48 2d 3e 66 69 for(elem=pH->fi
b9730 72 73 74 2c 20 70 48 2d 3e 66 69 72 73 74 3d 30 rst, pH->first=0
b9740 3b 20 65 6c 65 6d 3b 20 65 6c 65 6d 20 3d 20 6e ; elem; elem = n
b9750 65 78 74 5f 65 6c 65 6d 29 7b 0a 20 20 20 20 75 ext_elem){. u
b9760 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 20 3d 20 nsigned int h =
b9770 73 74 72 48 61 73 68 28 65 6c 65 6d 2d 3e 70 4b strHash(elem->pK
b9780 65 79 2c 20 65 6c 65 6d 2d 3e 6e 4b 65 79 29 20 ey, elem->nKey)
b9790 25 20 6e 65 77 5f 73 69 7a 65 3b 0a 20 20 20 20 % new_size;.
b97a0 6e 65 78 74 5f 65 6c 65 6d 20 3d 20 65 6c 65 6d next_elem = elem
b97b0 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 69 6e 73 65 ->next;. inse
b97c0 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20 26 6e rtElement(pH, &n
b97d0 65 77 5f 68 74 5b 68 5d 2c 20 65 6c 65 6d 29 3b ew_ht[h], elem);
b97e0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b . }. return 1;
b97f0 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 66 75 6e 63 .}../* This func
b9800 74 69 6f 6e 20 28 66 6f 72 20 69 6e 74 65 72 6e tion (for intern
b9810 61 6c 20 75 73 65 20 6f 6e 6c 79 29 20 6c 6f 63 al use only) loc
b9820 61 74 65 73 20 61 6e 20 65 6c 65 6d 65 6e 74 20 ates an element
b9830 69 6e 20 61 6e 0a 2a 2a 20 68 61 73 68 20 74 61 in an.** hash ta
b9840 62 6c 65 20 74 68 61 74 20 6d 61 74 63 68 65 73 ble that matches
b9850 20 74 68 65 20 67 69 76 65 6e 20 6b 65 79 2e 20 the given key.
b9860 20 54 68 65 20 68 61 73 68 20 66 6f 72 20 74 68 The hash for th
b9870 69 73 20 6b 65 79 20 68 61 73 0a 2a 2a 20 61 6c is key has.** al
b9880 72 65 61 64 79 20 62 65 65 6e 20 63 6f 6d 70 75 ready been compu
b9890 74 65 64 20 61 6e 64 20 69 73 20 70 61 73 73 65 ted and is passe
b98a0 64 20 61 73 20 74 68 65 20 34 74 68 20 70 61 72 d as the 4th par
b98b0 61 6d 65 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 ameter..*/.stati
b98c0 63 20 48 61 73 68 45 6c 65 6d 20 2a 66 69 6e 64 c HashElem *find
b98d0 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 ElementGivenHash
b98e0 28 0a 20 20 63 6f 6e 73 74 20 48 61 73 68 20 2a (. const Hash *
b98f0 70 48 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 70 pH, /* The p
b9900 48 20 74 6f 20 62 65 20 73 65 61 72 63 68 65 64 H to be searched
b9910 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
b9920 20 2a 70 4b 65 79 2c 20 20 20 2f 2a 20 54 68 65 *pKey, /* The
b9930 20 6b 65 79 20 77 65 20 61 72 65 20 73 65 61 72 key we are sear
b9940 63 68 69 6e 67 20 66 6f 72 20 2a 2f 0a 20 20 69 ching for */. i
b9950 6e 74 20 6e 4b 65 79 2c 20 20 20 20 20 20 20 20 nt nKey,
b9960 20 20 20 2f 2a 20 42 79 74 65 73 20 69 6e 20 6b /* Bytes in k
b9970 65 79 20 28 6e 6f 74 20 63 6f 75 6e 74 69 6e 67 ey (not counting
b9980 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 zero terminator
b9990 29 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 ) */. unsigned
b99a0 69 6e 74 20 68 20 20 20 20 20 20 2f 2a 20 54 68 int h /* Th
b99b0 65 20 68 61 73 68 20 66 6f 72 20 74 68 69 73 20 e hash for this
b99c0 6b 65 79 2e 20 2a 2f 0a 29 7b 0a 20 20 48 61 73 key. */.){. Has
b99d0 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 hElem *elem;
b99e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 /* U
b99f0 73 65 64 20 74 6f 20 6c 6f 6f 70 20 74 68 72 75 sed to loop thru
b9a00 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 6c 69 73 the element lis
b9a10 74 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 75 6e 74 t */. int count
b9a20 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
b9a30 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
b9a40 6f 66 20 65 6c 65 6d 65 6e 74 73 20 6c 65 66 74 of elements left
b9a50 20 74 6f 20 74 65 73 74 20 2a 2f 0a 0a 20 20 69 to test */.. i
b9a60 66 28 20 70 48 2d 3e 68 74 20 29 7b 0a 20 20 20 f( pH->ht ){.
b9a70 20 73 74 72 75 63 74 20 5f 68 74 20 2a 70 45 6e struct _ht *pEn
b9a80 74 72 79 20 3d 20 26 70 48 2d 3e 68 74 5b 68 5d try = &pH->ht[h]
b9a90 3b 0a 20 20 20 20 65 6c 65 6d 20 3d 20 70 45 6e ;. elem = pEn
b9aa0 74 72 79 2d 3e 63 68 61 69 6e 3b 0a 20 20 20 20 try->chain;.
b9ab0 63 6f 75 6e 74 20 3d 20 70 45 6e 74 72 79 2d 3e count = pEntry->
b9ac0 63 6f 75 6e 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a count;. }else{.
b9ad0 20 20 20 20 65 6c 65 6d 20 3d 20 70 48 2d 3e 66 elem = pH->f
b9ae0 69 72 73 74 3b 0a 20 20 20 20 63 6f 75 6e 74 20 irst;. count
b9af0 3d 20 70 48 2d 3e 63 6f 75 6e 74 3b 0a 20 20 7d = pH->count;. }
b9b00 0a 20 20 77 68 69 6c 65 28 20 63 6f 75 6e 74 2d . while( count-
b9b10 2d 20 26 26 20 41 4c 57 41 59 53 28 65 6c 65 6d - && ALWAYS(elem
b9b20 29 20 29 7b 0a 20 20 20 20 69 66 28 20 65 6c 65 ) ){. if( ele
b9b30 6d 2d 3e 6e 4b 65 79 3d 3d 6e 4b 65 79 20 26 26 m->nKey==nKey &&
b9b40 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 sqlite3StrNICmp
b9b50 28 65 6c 65 6d 2d 3e 70 4b 65 79 2c 70 4b 65 79 (elem->pKey,pKey
b9b60 2c 6e 4b 65 79 29 3d 3d 30 20 29 7b 20 0a 20 20 ,nKey)==0 ){ .
b9b70 20 20 20 20 72 65 74 75 72 6e 20 65 6c 65 6d 3b return elem;
b9b80 0a 20 20 20 20 7d 0a 20 20 20 20 65 6c 65 6d 20 . }. elem
b9b90 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 = elem->next;.
b9ba0 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a }. return 0;.}.
b9bb0 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 20 73 69 6e ./* Remove a sin
b9bc0 67 6c 65 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 gle entry from t
b9bd0 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 67 69 he hash table gi
b9be0 76 65 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f ven a pointer to
b9bf0 20 74 68 61 74 0a 2a 2a 20 65 6c 65 6d 65 6e 74 that.** element
b9c00 20 61 6e 64 20 61 20 68 61 73 68 20 6f 6e 20 74 and a hash on t
b9c10 68 65 20 65 6c 65 6d 65 6e 74 27 73 20 6b 65 79 he element's key
b9c20 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
b9c30 20 72 65 6d 6f 76 65 45 6c 65 6d 65 6e 74 47 69 removeElementGi
b9c40 76 65 6e 48 61 73 68 28 0a 20 20 48 61 73 68 20 venHash(. Hash
b9c50 2a 70 48 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 *pH, /*
b9c60 54 68 65 20 70 48 20 63 6f 6e 74 61 69 6e 69 6e The pH containin
b9c70 67 20 22 65 6c 65 6d 22 20 2a 2f 0a 20 20 48 61 g "elem" */. Ha
b9c80 73 68 45 6c 65 6d 2a 20 65 6c 65 6d 2c 20 20 20 shElem* elem,
b9c90 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74 20 74 /* The element t
b9ca0 6f 20 62 65 20 72 65 6d 6f 76 65 64 20 66 72 6f o be removed fro
b9cb0 6d 20 74 68 65 20 70 48 20 2a 2f 0a 20 20 75 6e m the pH */. un
b9cc0 73 69 67 6e 65 64 20 69 6e 74 20 68 20 20 20 20 signed int h
b9cd0 2f 2a 20 48 61 73 68 20 76 61 6c 75 65 20 66 6f /* Hash value fo
b9ce0 72 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 2a 2f r the element */
b9cf0 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 5f 68 74 .){. struct _ht
b9d00 20 2a 70 45 6e 74 72 79 3b 0a 20 20 69 66 28 20 *pEntry;. if(
b9d10 65 6c 65 6d 2d 3e 70 72 65 76 20 29 7b 0a 20 20 elem->prev ){.
b9d20 20 20 65 6c 65 6d 2d 3e 70 72 65 76 2d 3e 6e 65 elem->prev->ne
b9d30 78 74 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b xt = elem->next;
b9d40 20 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 . }else{. p
b9d50 48 2d 3e 66 69 72 73 74 20 3d 20 65 6c 65 6d 2d H->first = elem-
b9d60 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28 >next;. }. if(
b9d70 20 65 6c 65 6d 2d 3e 6e 65 78 74 20 29 7b 0a 20 elem->next ){.
b9d80 20 20 20 65 6c 65 6d 2d 3e 6e 65 78 74 2d 3e 70 elem->next->p
b9d90 72 65 76 20 3d 20 65 6c 65 6d 2d 3e 70 72 65 76 rev = elem->prev
b9da0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 2d 3e ;. }. if( pH->
b9db0 68 74 20 29 7b 0a 20 20 20 20 70 45 6e 74 72 79 ht ){. pEntry
b9dc0 20 3d 20 26 70 48 2d 3e 68 74 5b 68 5d 3b 0a 20 = &pH->ht[h];.
b9dd0 20 20 20 69 66 28 20 70 45 6e 74 72 79 2d 3e 63 if( pEntry->c
b9de0 68 61 69 6e 3d 3d 65 6c 65 6d 20 29 7b 0a 20 20 hain==elem ){.
b9df0 20 20 20 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 pEntry->chai
b9e00 6e 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a n = elem->next;.
b9e10 20 20 20 20 7d 0a 20 20 20 20 70 45 6e 74 72 79 }. pEntry
b9e20 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a 20 20 20 20 61 ->count--;. a
b9e30 73 73 65 72 74 28 20 70 45 6e 74 72 79 2d 3e 63 ssert( pEntry->c
b9e40 6f 75 6e 74 3e 3d 30 20 29 3b 0a 20 20 7d 0a 20 ount>=0 );. }.
b9e50 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 20 65 sqlite3_free( e
b9e60 6c 65 6d 20 29 3b 0a 20 20 70 48 2d 3e 63 6f 75 lem );. pH->cou
b9e70 6e 74 2d 2d 3b 0a 20 20 69 66 28 20 70 48 2d 3e nt--;. if( pH->
b9e80 63 6f 75 6e 74 3c 3d 30 20 29 7b 0a 20 20 20 20 count<=0 ){.
b9e90 61 73 73 65 72 74 28 20 70 48 2d 3e 66 69 72 73 assert( pH->firs
b9ea0 74 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 t==0 );. asse
b9eb0 72 74 28 20 70 48 2d 3e 63 6f 75 6e 74 3d 3d 30 rt( pH->count==0
b9ec0 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 48 );. sqlite3H
b9ed0 61 73 68 43 6c 65 61 72 28 70 48 29 3b 0a 20 20 ashClear(pH);.
b9ee0 7d 0a 7d 0a 0a 2f 2a 20 41 74 74 65 6d 70 74 20 }.}../* Attempt
b9ef0 74 6f 20 6c 6f 63 61 74 65 20 61 6e 20 65 6c 65 to locate an ele
b9f00 6d 65 6e 74 20 6f 66 20 74 68 65 20 68 61 73 68 ment of the hash
b9f10 20 74 61 62 6c 65 20 70 48 20 77 69 74 68 20 61 table pH with a
b9f20 20 6b 65 79 0a 2a 2a 20 74 68 61 74 20 6d 61 74 key.** that mat
b9f30 63 68 65 73 20 70 4b 65 79 2c 6e 4b 65 79 2e 20 ches pKey,nKey.
b9f40 20 52 65 74 75 72 6e 20 74 68 65 20 64 61 74 61 Return the data
b9f50 20 66 6f 72 20 74 68 69 73 20 65 6c 65 6d 65 6e for this elemen
b9f60 74 20 69 66 20 69 74 20 69 73 0a 2a 2a 20 66 6f t if it is.** fo
b9f70 75 6e 64 2c 20 6f 72 20 4e 55 4c 4c 20 69 66 20 und, or NULL if
b9f80 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 there is no matc
b9f90 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 h..*/.SQLITE_PRI
b9fa0 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
b9fb0 65 33 48 61 73 68 46 69 6e 64 28 63 6f 6e 73 74 e3HashFind(const
b9fc0 20 48 61 73 68 20 2a 70 48 2c 20 63 6f 6e 73 74 Hash *pH, const
b9fd0 20 63 68 61 72 20 2a 70 4b 65 79 2c 20 69 6e 74 char *pKey, int
b9fe0 20 6e 4b 65 79 29 7b 0a 20 20 48 61 73 68 45 6c nKey){. HashEl
b9ff0 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 2f 2a 20 em *elem; /*
ba000 54 68 65 20 65 6c 65 6d 65 6e 74 20 74 68 61 74 The element that
ba010 20 6d 61 74 63 68 65 73 20 6b 65 79 20 2a 2f 0a matches key */.
ba020 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 unsigned int h
ba030 3b 20 20 20 20 2f 2a 20 41 20 68 61 73 68 20 6f ; /* A hash o
ba040 6e 20 6b 65 79 20 2a 2f 0a 0a 20 20 61 73 73 65 n key */.. asse
ba050 72 74 28 20 70 48 21 3d 30 20 29 3b 0a 20 20 61 rt( pH!=0 );. a
ba060 73 73 65 72 74 28 20 70 4b 65 79 21 3d 30 20 29 ssert( pKey!=0 )
ba070 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 4b 65 79 ;. assert( nKey
ba080 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 48 2d >=0 );. if( pH-
ba090 3e 68 74 20 29 7b 0a 20 20 20 20 68 20 3d 20 73 >ht ){. h = s
ba0a0 74 72 48 61 73 68 28 70 4b 65 79 2c 20 6e 4b 65 trHash(pKey, nKe
ba0b0 79 29 20 25 20 70 48 2d 3e 68 74 73 69 7a 65 3b y) % pH->htsize;
ba0c0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 20 . }else{. h
ba0d0 3d 20 30 3b 0a 20 20 7d 0a 20 20 65 6c 65 6d 20 = 0;. }. elem
ba0e0 3d 20 66 69 6e 64 45 6c 65 6d 65 6e 74 47 69 76 = findElementGiv
ba0f0 65 6e 48 61 73 68 28 70 48 2c 20 70 4b 65 79 2c enHash(pH, pKey,
ba100 20 6e 4b 65 79 2c 20 68 29 3b 0a 20 20 72 65 74 nKey, h);. ret
ba110 75 72 6e 20 65 6c 65 6d 20 3f 20 65 6c 65 6d 2d urn elem ? elem-
ba120 3e 64 61 74 61 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a >data : 0;.}../*
ba130 20 49 6e 73 65 72 74 20 61 6e 20 65 6c 65 6d 65 Insert an eleme
ba140 6e 74 20 69 6e 74 6f 20 74 68 65 20 68 61 73 68 nt into the hash
ba150 20 74 61 62 6c 65 20 70 48 2e 20 20 54 68 65 20 table pH. The
ba160 6b 65 79 20 69 73 20 70 4b 65 79 2c 6e 4b 65 79 key is pKey,nKey
ba170 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61 .** and the data
ba180 20 69 73 20 22 64 61 74 61 22 2e 0a 2a 2a 0a 2a is "data"..**.*
ba190 2a 20 49 66 20 6e 6f 20 65 6c 65 6d 65 6e 74 20 * If no element
ba1a0 65 78 69 73 74 73 20 77 69 74 68 20 61 20 6d 61 exists with a ma
ba1b0 74 63 68 69 6e 67 20 6b 65 79 2c 20 74 68 65 6e tching key, then
ba1c0 20 61 20 6e 65 77 0a 2a 2a 20 65 6c 65 6d 65 6e a new.** elemen
ba1d0 74 20 69 73 20 63 72 65 61 74 65 64 20 61 6e 64 t is created and
ba1e0 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 NULL is returne
ba1f0 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 6f 74 d..**.** If anot
ba200 68 65 72 20 65 6c 65 6d 65 6e 74 20 61 6c 72 65 her element alre
ba210 61 64 79 20 65 78 69 73 74 73 20 77 69 74 68 20 ady exists with
ba220 74 68 65 20 73 61 6d 65 20 6b 65 79 2c 20 74 68 the same key, th
ba230 65 6e 20 74 68 65 0a 2a 2a 20 6e 65 77 20 64 61 en the.** new da
ba240 74 61 20 72 65 70 6c 61 63 65 73 20 74 68 65 20 ta replaces the
ba250 6f 6c 64 20 64 61 74 61 20 61 6e 64 20 74 68 65 old data and the
ba260 20 6f 6c 64 20 64 61 74 61 20 69 73 20 72 65 74 old data is ret
ba270 75 72 6e 65 64 2e 0a 2a 2a 20 54 68 65 20 6b 65 urned..** The ke
ba280 79 20 69 73 20 6e 6f 74 20 63 6f 70 69 65 64 20 y is not copied
ba290 69 6e 20 74 68 69 73 20 69 6e 73 74 61 6e 63 65 in this instance
ba2a0 2e 20 20 49 66 20 61 20 6d 61 6c 6c 6f 63 20 66 . If a malloc f
ba2b0 61 69 6c 73 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 ails, then.** th
ba2c0 65 20 6e 65 77 20 64 61 74 61 20 69 73 20 72 65 e new data is re
ba2d0 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65 20 68 turned and the h
ba2e0 61 73 68 20 74 61 62 6c 65 20 69 73 20 75 6e 63 ash table is unc
ba2f0 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 hanged..**.** If
ba300 20 74 68 65 20 22 64 61 74 61 22 20 70 61 72 61 the "data" para
ba310 6d 65 74 65 72 20 74 6f 20 74 68 69 73 20 66 75 meter to this fu
ba320 6e 63 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 nction is NULL,
ba330 74 68 65 6e 20 74 68 65 0a 2a 2a 20 65 6c 65 6d then the.** elem
ba340 65 6e 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e ent correspondin
ba350 67 20 74 6f 20 22 6b 65 79 22 20 69 73 20 72 65 g to "key" is re
ba360 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 68 moved from the h
ba370 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 ash table..*/.SQ
ba380 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
ba390 64 20 2a 73 71 6c 69 74 65 33 48 61 73 68 49 6e d *sqlite3HashIn
ba3a0 73 65 72 74 28 48 61 73 68 20 2a 70 48 2c 20 63 sert(Hash *pH, c
ba3b0 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b 65 79 2c onst char *pKey,
ba3c0 20 69 6e 74 20 6e 4b 65 79 2c 20 76 6f 69 64 20 int nKey, void
ba3d0 2a 64 61 74 61 29 7b 0a 20 20 75 6e 73 69 67 6e *data){. unsign
ba3e0 65 64 20 69 6e 74 20 68 3b 20 20 20 20 20 20 20 ed int h;
ba3f0 2f 2a 20 74 68 65 20 68 61 73 68 20 6f 66 20 74 /* the hash of t
ba400 68 65 20 6b 65 79 20 6d 6f 64 75 6c 6f 20 68 61 he key modulo ha
ba410 73 68 20 74 61 62 6c 65 20 73 69 7a 65 20 2a 2f sh table size */
ba420 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 . HashElem *ele
ba430 6d 3b 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 m; /* Used
ba440 20 74 6f 20 6c 6f 6f 70 20 74 68 72 75 20 74 68 to loop thru th
ba450 65 20 65 6c 65 6d 65 6e 74 20 6c 69 73 74 20 2a e element list *
ba460 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 6e 65 /. HashElem *ne
ba470 77 5f 65 6c 65 6d 3b 20 20 20 2f 2a 20 4e 65 77 w_elem; /* New
ba480 20 65 6c 65 6d 65 6e 74 20 61 64 64 65 64 20 74 element added t
ba490 6f 20 74 68 65 20 70 48 20 2a 2f 0a 0a 20 20 61 o the pH */.. a
ba4a0 73 73 65 72 74 28 20 70 48 21 3d 30 20 29 3b 0a ssert( pH!=0 );.
ba4b0 20 20 61 73 73 65 72 74 28 20 70 4b 65 79 21 3d assert( pKey!=
ba4c0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 0 );. assert( n
ba4d0 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 Key>=0 );. if(
ba4e0 70 48 2d 3e 68 74 73 69 7a 65 20 29 7b 0a 20 20 pH->htsize ){.
ba4f0 20 20 68 20 3d 20 73 74 72 48 61 73 68 28 70 4b h = strHash(pK
ba500 65 79 2c 20 6e 4b 65 79 29 20 25 20 70 48 2d 3e ey, nKey) % pH->
ba510 68 74 73 69 7a 65 3b 0a 20 20 7d 65 6c 73 65 7b htsize;. }else{
ba520 0a 20 20 20 20 68 20 3d 20 30 3b 0a 20 20 7d 0a . h = 0;. }.
ba530 20 20 65 6c 65 6d 20 3d 20 66 69 6e 64 45 6c 65 elem = findEle
ba540 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 28 70 48 mentGivenHash(pH
ba550 2c 70 4b 65 79 2c 6e 4b 65 79 2c 68 29 3b 0a 20 ,pKey,nKey,h);.
ba560 20 69 66 28 20 65 6c 65 6d 20 29 7b 0a 20 20 20 if( elem ){.
ba570 20 76 6f 69 64 20 2a 6f 6c 64 5f 64 61 74 61 20 void *old_data
ba580 3d 20 65 6c 65 6d 2d 3e 64 61 74 61 3b 0a 20 20 = elem->data;.
ba590 20 20 69 66 28 20 64 61 74 61 3d 3d 30 20 29 7b if( data==0 ){
ba5a0 0a 20 20 20 20 20 20 72 65 6d 6f 76 65 45 6c 65 . removeEle
ba5b0 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 28 70 48 mentGivenHash(pH
ba5c0 2c 65 6c 65 6d 2c 68 29 3b 0a 20 20 20 20 7d 65 ,elem,h);. }e
ba5d0 6c 73 65 7b 0a 20 20 20 20 20 20 65 6c 65 6d 2d lse{. elem-
ba5e0 3e 64 61 74 61 20 3d 20 64 61 74 61 3b 0a 20 20 >data = data;.
ba5f0 20 20 20 20 65 6c 65 6d 2d 3e 70 4b 65 79 20 3d elem->pKey =
ba600 20 70 4b 65 79 3b 0a 20 20 20 20 20 20 61 73 73 pKey;. ass
ba610 65 72 74 28 6e 4b 65 79 3d 3d 65 6c 65 6d 2d 3e ert(nKey==elem->
ba620 6e 4b 65 79 29 3b 0a 20 20 20 20 7d 0a 20 20 20 nKey);. }.
ba630 20 72 65 74 75 72 6e 20 6f 6c 64 5f 64 61 74 61 return old_data
ba640 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 61 74 61 ;. }. if( data
ba650 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a ==0 ) return 0;.
ba660 20 20 6e 65 77 5f 65 6c 65 6d 20 3d 20 28 48 61 new_elem = (Ha
ba670 73 68 45 6c 65 6d 2a 29 73 71 6c 69 74 65 33 4d shElem*)sqlite3M
ba680 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 48 61 alloc( sizeof(Ha
ba690 73 68 45 6c 65 6d 29 20 29 3b 0a 20 20 69 66 28 shElem) );. if(
ba6a0 20 6e 65 77 5f 65 6c 65 6d 3d 3d 30 20 29 20 72 new_elem==0 ) r
ba6b0 65 74 75 72 6e 20 64 61 74 61 3b 0a 20 20 6e 65 eturn data;. ne
ba6c0 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 20 3d 20 70 w_elem->pKey = p
ba6d0 4b 65 79 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d Key;. new_elem-
ba6e0 3e 6e 4b 65 79 20 3d 20 6e 4b 65 79 3b 0a 20 20 >nKey = nKey;.
ba6f0 6e 65 77 5f 65 6c 65 6d 2d 3e 64 61 74 61 20 3d new_elem->data =
ba700 20 64 61 74 61 3b 0a 20 20 70 48 2d 3e 63 6f 75 data;. pH->cou
ba710 6e 74 2b 2b 3b 0a 20 20 69 66 28 20 70 48 2d 3e nt++;. if( pH->
ba720 63 6f 75 6e 74 3e 3d 31 30 20 26 26 20 70 48 2d count>=10 && pH-
ba730 3e 63 6f 75 6e 74 20 3e 20 32 2a 70 48 2d 3e 68 >count > 2*pH->h
ba740 74 73 69 7a 65 20 29 7b 0a 20 20 20 20 69 66 28 tsize ){. if(
ba750 20 72 65 68 61 73 68 28 70 48 2c 20 70 48 2d 3e rehash(pH, pH->
ba760 63 6f 75 6e 74 2a 32 29 20 29 7b 0a 20 20 20 20 count*2) ){.
ba770 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e 68 74 assert( pH->ht
ba780 73 69 7a 65 3e 30 20 29 3b 0a 20 20 20 20 20 20 size>0 );.
ba790 68 20 3d 20 73 74 72 48 61 73 68 28 70 4b 65 79 h = strHash(pKey
ba7a0 2c 20 6e 4b 65 79 29 20 25 20 70 48 2d 3e 68 74 , nKey) % pH->ht
ba7b0 73 69 7a 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a size;. }. }.
ba7c0 20 20 69 66 28 20 70 48 2d 3e 68 74 20 29 7b 0a if( pH->ht ){.
ba7d0 20 20 20 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e insertElemen
ba7e0 74 28 70 48 2c 20 26 70 48 2d 3e 68 74 5b 68 5d t(pH, &pH->ht[h]
ba7f0 2c 20 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20 20 7d , new_elem);. }
ba800 65 6c 73 65 7b 0a 20 20 20 20 69 6e 73 65 72 74 else{. insert
ba810 45 6c 65 6d 65 6e 74 28 70 48 2c 20 30 2c 20 6e Element(pH, 0, n
ba820 65 77 5f 65 6c 65 6d 29 3b 0a 20 20 7d 0a 20 20 ew_elem);. }.
ba830 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 2a return 0;.}../**
ba840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
ba850 20 6f 66 20 68 61 73 68 2e 63 20 2a 2a 2a 2a 2a of hash.c *****
ba860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ba870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ba880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
ba890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
ba8a0 69 6e 20 66 69 6c 65 20 6f 70 63 6f 64 65 73 2e in file opcodes.
ba8b0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
ba8c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ba8d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 ***********/./*
ba8e0 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 Automatically ge
ba8f0 6e 65 72 61 74 65 64 2e 20 20 44 6f 20 6e 6f 74 nerated. Do not
ba900 20 65 64 69 74 20 2a 2f 0a 2f 2a 20 53 65 65 20 edit */./* See
ba910 74 68 65 20 6d 6b 6f 70 63 6f 64 65 63 2e 61 77 the mkopcodec.aw
ba920 6b 20 73 63 72 69 70 74 20 66 6f 72 20 64 65 74 k script for det
ba930 61 69 6c 73 2e 20 2a 2f 0a 23 69 66 20 21 64 65 ails. */.#if !de
ba940 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
ba950 54 5f 45 58 50 4c 41 49 4e 29 20 7c 7c 20 21 64 T_EXPLAIN) || !d
ba960 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c efined(NDEBUG) |
ba970 7c 20 64 65 66 69 6e 65 64 28 56 44 42 45 5f 50 | defined(VDBE_P
ba980 52 4f 46 49 4c 45 29 20 7c 7c 20 64 65 66 69 6e ROFILE) || defin
ba990 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 ed(SQLITE_DEBUG)
ba9a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
ba9b0 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 const char *sqli
ba9c0 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28 69 6e te3OpcodeName(in
ba9d0 74 20 69 29 7b 0a 20 73 74 61 74 69 63 20 63 6f t i){. static co
ba9e0 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 20 nst char *const
ba9f0 61 7a 4e 61 6d 65 5b 5d 20 3d 20 7b 20 22 3f 22 azName[] = { "?"
baa00 2c 0a 20 20 20 20 20 2f 2a 20 20 20 31 20 2a 2f ,. /* 1 */
baa10 20 22 47 6f 74 6f 22 2c 0a 20 20 20 20 20 2f 2a "Goto",. /*
baa20 20 20 20 32 20 2a 2f 20 22 47 6f 73 75 62 22 2c 2 */ "Gosub",
baa30 0a 20 20 20 20 20 2f 2a 20 20 20 33 20 2a 2f 20 . /* 3 */
baa40 22 52 65 74 75 72 6e 22 2c 0a 20 20 20 20 20 2f "Return",. /
baa50 2a 20 20 20 34 20 2a 2f 20 22 59 69 65 6c 64 22 * 4 */ "Yield"
baa60 2c 0a 20 20 20 20 20 2f 2a 20 20 20 35 20 2a 2f ,. /* 5 */
baa70 20 22 48 61 6c 74 49 66 4e 75 6c 6c 22 2c 0a 20 "HaltIfNull",.
baa80 20 20 20 20 2f 2a 20 20 20 36 20 2a 2f 20 22 48 /* 6 */ "H
baa90 61 6c 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 alt",. /*
baaa0 37 20 2a 2f 20 22 49 6e 74 65 67 65 72 22 2c 0a 7 */ "Integer",.
baab0 20 20 20 20 20 2f 2a 20 20 20 38 20 2a 2f 20 22 /* 8 */ "
baac0 49 6e 74 36 34 22 2c 0a 20 20 20 20 20 2f 2a 20 Int64",. /*
baad0 20 20 39 20 2a 2f 20 22 53 74 72 69 6e 67 22 2c 9 */ "String",
baae0 0a 20 20 20 20 20 2f 2a 20 20 31 30 20 2a 2f 20 . /* 10 */
baaf0 22 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 "Null",. /*
bab00 20 31 31 20 2a 2f 20 22 42 6c 6f 62 22 2c 0a 20 11 */ "Blob",.
bab10 20 20 20 20 2f 2a 20 20 31 32 20 2a 2f 20 22 56 /* 12 */ "V
bab20 61 72 69 61 62 6c 65 22 2c 0a 20 20 20 20 20 2f ariable",. /
bab30 2a 20 20 31 33 20 2a 2f 20 22 4d 6f 76 65 22 2c * 13 */ "Move",
bab40 0a 20 20 20 20 20 2f 2a 20 20 31 34 20 2a 2f 20 . /* 14 */
bab50 22 43 6f 70 79 22 2c 0a 20 20 20 20 20 2f 2a 20 "Copy",. /*
bab60 20 31 35 20 2a 2f 20 22 53 43 6f 70 79 22 2c 0a 15 */ "SCopy",.
bab70 20 20 20 20 20 2f 2a 20 20 31 36 20 2a 2f 20 22 /* 16 */ "
bab80 52 65 73 75 6c 74 52 6f 77 22 2c 0a 20 20 20 20 ResultRow",.
bab90 20 2f 2a 20 20 31 37 20 2a 2f 20 22 43 6f 6c 6c /* 17 */ "Coll
baba0 53 65 71 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 Seq",. /* 1
babb0 38 20 2a 2f 20 22 46 75 6e 63 74 69 6f 6e 22 2c 8 */ "Function",
babc0 0a 20 20 20 20 20 2f 2a 20 20 31 39 20 2a 2f 20 . /* 19 */
babd0 22 4e 6f 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 "Not",. /*
babe0 32 30 20 2a 2f 20 22 41 64 64 49 6d 6d 22 2c 0a 20 */ "AddImm",.
babf0 20 20 20 20 20 2f 2a 20 20 32 31 20 2a 2f 20 22 /* 21 */ "
bac00 4d 75 73 74 42 65 49 6e 74 22 2c 0a 20 20 20 20 MustBeInt",.
bac10 20 2f 2a 20 20 32 32 20 2a 2f 20 22 52 65 61 6c /* 22 */ "Real
bac20 41 66 66 69 6e 69 74 79 22 2c 0a 20 20 20 20 20 Affinity",.
bac30 2f 2a 20 20 32 33 20 2a 2f 20 22 50 65 72 6d 75 /* 23 */ "Permu
bac40 74 61 74 69 6f 6e 22 2c 0a 20 20 20 20 20 2f 2a tation",. /*
bac50 20 20 32 34 20 2a 2f 20 22 43 6f 6d 70 61 72 65 24 */ "Compare
bac60 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 35 20 2a ",. /* 25 *
bac70 2f 20 22 4a 75 6d 70 22 2c 0a 20 20 20 20 20 2f / "Jump",. /
bac80 2a 20 20 32 36 20 2a 2f 20 22 49 66 22 2c 0a 20 * 26 */ "If",.
bac90 20 20 20 20 2f 2a 20 20 32 37 20 2a 2f 20 22 49 /* 27 */ "I
baca0 66 4e 6f 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 fNot",. /*
bacb0 32 38 20 2a 2f 20 22 43 6f 6c 75 6d 6e 22 2c 0a 28 */ "Column",.
bacc0 20 20 20 20 20 2f 2a 20 20 32 39 20 2a 2f 20 22 /* 29 */ "
bacd0 41 66 66 69 6e 69 74 79 22 2c 0a 20 20 20 20 20 Affinity",.
bace0 2f 2a 20 20 33 30 20 2a 2f 20 22 4d 61 6b 65 52 /* 30 */ "MakeR
bacf0 65 63 6f 72 64 22 2c 0a 20 20 20 20 20 2f 2a 20 ecord",. /*
bad00 20 33 31 20 2a 2f 20 22 43 6f 75 6e 74 22 2c 0a 31 */ "Count",.
bad10 20 20 20 20 20 2f 2a 20 20 33 32 20 2a 2f 20 22 /* 32 */ "
bad20 53 61 76 65 70 6f 69 6e 74 22 2c 0a 20 20 20 20 Savepoint",.
bad30 20 2f 2a 20 20 33 33 20 2a 2f 20 22 41 75 74 6f /* 33 */ "Auto
bad40 43 6f 6d 6d 69 74 22 2c 0a 20 20 20 20 20 2f 2a Commit",. /*
bad50 20 20 33 34 20 2a 2f 20 22 54 72 61 6e 73 61 63 34 */ "Transac
bad60 74 69 6f 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 tion",. /*
bad70 33 35 20 2a 2f 20 22 52 65 61 64 43 6f 6f 6b 69 35 */ "ReadCooki
bad80 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 36 20 e",. /* 36
bad90 2a 2f 20 22 53 65 74 43 6f 6f 6b 69 65 22 2c 0a */ "SetCookie",.
bada0 20 20 20 20 20 2f 2a 20 20 33 37 20 2a 2f 20 22 /* 37 */ "
badb0 56 65 72 69 66 79 43 6f 6f 6b 69 65 22 2c 0a 20 VerifyCookie",.
badc0 20 20 20 20 2f 2a 20 20 33 38 20 2a 2f 20 22 4f /* 38 */ "O
badd0 70 65 6e 52 65 61 64 22 2c 0a 20 20 20 20 20 2f penRead",. /
bade0 2a 20 20 33 39 20 2a 2f 20 22 4f 70 65 6e 57 72 * 39 */ "OpenWr
badf0 69 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 ite",. /* 4
bae00 30 20 2a 2f 20 22 4f 70 65 6e 45 70 68 65 6d 65 0 */ "OpenEpheme
bae10 72 61 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 ral",. /* 4
bae20 31 20 2a 2f 20 22 4f 70 65 6e 50 73 65 75 64 6f 1 */ "OpenPseudo
bae30 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 32 20 2a ",. /* 42 *
bae40 2f 20 22 43 6c 6f 73 65 22 2c 0a 20 20 20 20 20 / "Close",.
bae50 2f 2a 20 20 34 33 20 2a 2f 20 22 53 65 65 6b 4c /* 43 */ "SeekL
bae60 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 34 20 t",. /* 44
bae70 2a 2f 20 22 53 65 65 6b 4c 65 22 2c 0a 20 20 20 */ "SeekLe",.
bae80 20 20 2f 2a 20 20 34 35 20 2a 2f 20 22 53 65 65 /* 45 */ "See
bae90 6b 47 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 kGe",. /* 4
baea0 36 20 2a 2f 20 22 53 65 65 6b 47 74 22 2c 0a 20 6 */ "SeekGt",.
baeb0 20 20 20 20 2f 2a 20 20 34 37 20 2a 2f 20 22 53 /* 47 */ "S
baec0 65 65 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 eek",. /* 4
baed0 38 20 2a 2f 20 22 4e 6f 74 46 6f 75 6e 64 22 2c 8 */ "NotFound",
baee0 0a 20 20 20 20 20 2f 2a 20 20 34 39 20 2a 2f 20 . /* 49 */
baef0 22 46 6f 75 6e 64 22 2c 0a 20 20 20 20 20 2f 2a "Found",. /*
baf00 20 20 35 30 20 2a 2f 20 22 49 73 55 6e 69 71 75 50 */ "IsUniqu
baf10 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 31 20 e",. /* 51
baf20 2a 2f 20 22 4e 6f 74 45 78 69 73 74 73 22 2c 0a */ "NotExists",.
baf30 20 20 20 20 20 2f 2a 20 20 35 32 20 2a 2f 20 22 /* 52 */ "
baf40 53 65 71 75 65 6e 63 65 22 2c 0a 20 20 20 20 20 Sequence",.
baf50 2f 2a 20 20 35 33 20 2a 2f 20 22 4e 65 77 52 6f /* 53 */ "NewRo
baf60 77 69 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 wid",. /* 5
baf70 34 20 2a 2f 20 22 49 6e 73 65 72 74 22 2c 0a 20 4 */ "Insert",.
baf80 20 20 20 20 2f 2a 20 20 35 35 20 2a 2f 20 22 49 /* 55 */ "I
baf90 6e 73 65 72 74 49 6e 74 22 2c 0a 20 20 20 20 20 nsertInt",.
bafa0 2f 2a 20 20 35 36 20 2a 2f 20 22 44 65 6c 65 74 /* 56 */ "Delet
bafb0 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 37 20 e",. /* 57
bafc0 2a 2f 20 22 52 65 73 65 74 43 6f 75 6e 74 22 2c */ "ResetCount",
bafd0 0a 20 20 20 20 20 2f 2a 20 20 35 38 20 2a 2f 20 . /* 58 */
bafe0 22 52 6f 77 4b 65 79 22 2c 0a 20 20 20 20 20 2f "RowKey",. /
baff0 2a 20 20 35 39 20 2a 2f 20 22 52 6f 77 44 61 74 * 59 */ "RowDat
bb000 61 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 30 20 a",. /* 60
bb010 2a 2f 20 22 52 6f 77 69 64 22 2c 0a 20 20 20 20 */ "Rowid",.
bb020 20 2f 2a 20 20 36 31 20 2a 2f 20 22 4e 75 6c 6c /* 61 */ "Null
bb030 52 6f 77 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 Row",. /* 6
bb040 32 20 2a 2f 20 22 4c 61 73 74 22 2c 0a 20 20 20 2 */ "Last",.
bb050 20 20 2f 2a 20 20 36 33 20 2a 2f 20 22 53 6f 72 /* 63 */ "Sor
bb060 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 34 20 t",. /* 64
bb070 2a 2f 20 22 52 65 77 69 6e 64 22 2c 0a 20 20 20 */ "Rewind",.
bb080 20 20 2f 2a 20 20 36 35 20 2a 2f 20 22 50 72 65 /* 65 */ "Pre
bb090 76 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 36 20 v",. /* 66
bb0a0 2a 2f 20 22 4e 65 78 74 22 2c 0a 20 20 20 20 20 */ "Next",.
bb0b0 2f 2a 20 20 36 37 20 2a 2f 20 22 49 64 78 49 6e /* 67 */ "IdxIn
bb0c0 73 65 72 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 sert",. /*
bb0d0 36 38 20 2a 2f 20 22 4f 72 22 2c 0a 20 20 20 20 68 */ "Or",.
bb0e0 20 2f 2a 20 20 36 39 20 2a 2f 20 22 41 6e 64 22 /* 69 */ "And"
bb0f0 2c 0a 20 20 20 20 20 2f 2a 20 20 37 30 20 2a 2f ,. /* 70 */
bb100 20 22 49 64 78 44 65 6c 65 74 65 22 2c 0a 20 20 "IdxDelete",.
bb110 20 20 20 2f 2a 20 20 37 31 20 2a 2f 20 22 49 64 /* 71 */ "Id
bb120 78 52 6f 77 69 64 22 2c 0a 20 20 20 20 20 2f 2a xRowid",. /*
bb130 20 20 37 32 20 2a 2f 20 22 49 64 78 4c 54 22 2c 72 */ "IdxLT",
bb140 0a 20 20 20 20 20 2f 2a 20 20 37 33 20 2a 2f 20 . /* 73 */
bb150 22 49 73 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 2f "IsNull",. /
bb160 2a 20 20 37 34 20 2a 2f 20 22 4e 6f 74 4e 75 6c * 74 */ "NotNul
bb170 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 35 20 l",. /* 75
bb180 2a 2f 20 22 4e 65 22 2c 0a 20 20 20 20 20 2f 2a */ "Ne",. /*
bb190 20 20 37 36 20 2a 2f 20 22 45 71 22 2c 0a 20 20 76 */ "Eq",.
bb1a0 20 20 20 2f 2a 20 20 37 37 20 2a 2f 20 22 47 74 /* 77 */ "Gt
bb1b0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 38 20 2a ",. /* 78 *
bb1c0 2f 20 22 4c 65 22 2c 0a 20 20 20 20 20 2f 2a 20 / "Le",. /*
bb1d0 20 37 39 20 2a 2f 20 22 4c 74 22 2c 0a 20 20 20 79 */ "Lt",.
bb1e0 20 20 2f 2a 20 20 38 30 20 2a 2f 20 22 47 65 22 /* 80 */ "Ge"
bb1f0 2c 0a 20 20 20 20 20 2f 2a 20 20 38 31 20 2a 2f ,. /* 81 */
bb200 20 22 49 64 78 47 45 22 2c 0a 20 20 20 20 20 2f "IdxGE",. /
bb210 2a 20 20 38 32 20 2a 2f 20 22 42 69 74 41 6e 64 * 82 */ "BitAnd
bb220 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 33 20 2a ",. /* 83 *
bb230 2f 20 22 42 69 74 4f 72 22 2c 0a 20 20 20 20 20 / "BitOr",.
bb240 2f 2a 20 20 38 34 20 2a 2f 20 22 53 68 69 66 74 /* 84 */ "Shift
bb250 4c 65 66 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 Left",. /*
bb260 38 35 20 2a 2f 20 22 53 68 69 66 74 52 69 67 68 85 */ "ShiftRigh
bb270 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 36 20 t",. /* 86
bb280 2a 2f 20 22 41 64 64 22 2c 0a 20 20 20 20 20 2f */ "Add",. /
bb290 2a 20 20 38 37 20 2a 2f 20 22 53 75 62 74 72 61 * 87 */ "Subtra
bb2a0 63 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 38 ct",. /* 88
bb2b0 20 2a 2f 20 22 4d 75 6c 74 69 70 6c 79 22 2c 0a */ "Multiply",.
bb2c0 20 20 20 20 20 2f 2a 20 20 38 39 20 2a 2f 20 22 /* 89 */ "
bb2d0 44 69 76 69 64 65 22 2c 0a 20 20 20 20 20 2f 2a Divide",. /*
bb2e0 20 20 39 30 20 2a 2f 20 22 52 65 6d 61 69 6e 64 90 */ "Remaind
bb2f0 65 72 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 31 er",. /* 91
bb300 20 2a 2f 20 22 43 6f 6e 63 61 74 22 2c 0a 20 20 */ "Concat",.
bb310 20 20 20 2f 2a 20 20 39 32 20 2a 2f 20 22 44 65 /* 92 */ "De
bb320 73 74 72 6f 79 22 2c 0a 20 20 20 20 20 2f 2a 20 stroy",. /*
bb330 20 39 33 20 2a 2f 20 22 42 69 74 4e 6f 74 22 2c 93 */ "BitNot",
bb340 0a 20 20 20 20 20 2f 2a 20 20 39 34 20 2a 2f 20 . /* 94 */
bb350 22 53 74 72 69 6e 67 38 22 2c 0a 20 20 20 20 20 "String8",.
bb360 2f 2a 20 20 39 35 20 2a 2f 20 22 43 6c 65 61 72 /* 95 */ "Clear
bb370 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 36 20 2a ",. /* 96 *
bb380 2f 20 22 43 72 65 61 74 65 49 6e 64 65 78 22 2c / "CreateIndex",
bb390 0a 20 20 20 20 20 2f 2a 20 20 39 37 20 2a 2f 20 . /* 97 */
bb3a0 22 43 72 65 61 74 65 54 61 62 6c 65 22 2c 0a 20 "CreateTable",.
bb3b0 20 20 20 20 2f 2a 20 20 39 38 20 2a 2f 20 22 50 /* 98 */ "P
bb3c0 61 72 73 65 53 63 68 65 6d 61 22 2c 0a 20 20 20 arseSchema",.
bb3d0 20 20 2f 2a 20 20 39 39 20 2a 2f 20 22 4c 6f 61 /* 99 */ "Loa
bb3e0 64 41 6e 61 6c 79 73 69 73 22 2c 0a 20 20 20 20 dAnalysis",.
bb3f0 20 2f 2a 20 31 30 30 20 2a 2f 20 22 44 72 6f 70 /* 100 */ "Drop
bb400 54 61 62 6c 65 22 2c 0a 20 20 20 20 20 2f 2a 20 Table",. /*
bb410 31 30 31 20 2a 2f 20 22 44 72 6f 70 49 6e 64 65 101 */ "DropInde
bb420 78 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 32 20 x",. /* 102
bb430 2a 2f 20 22 44 72 6f 70 54 72 69 67 67 65 72 22 */ "DropTrigger"
bb440 2c 0a 20 20 20 20 20 2f 2a 20 31 30 33 20 2a 2f ,. /* 103 */
bb450 20 22 49 6e 74 65 67 72 69 74 79 43 6b 22 2c 0a "IntegrityCk",.
bb460 20 20 20 20 20 2f 2a 20 31 30 34 20 2a 2f 20 22 /* 104 */ "
bb470 52 6f 77 53 65 74 41 64 64 22 2c 0a 20 20 20 20 RowSetAdd",.
bb480 20 2f 2a 20 31 30 35 20 2a 2f 20 22 52 6f 77 53 /* 105 */ "RowS
bb490 65 74 52 65 61 64 22 2c 0a 20 20 20 20 20 2f 2a etRead",. /*
bb4a0 20 31 30 36 20 2a 2f 20 22 52 6f 77 53 65 74 54 106 */ "RowSetT
bb4b0 65 73 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 est",. /* 10
bb4c0 37 20 2a 2f 20 22 50 72 6f 67 72 61 6d 22 2c 0a 7 */ "Program",.
bb4d0 20 20 20 20 20 2f 2a 20 31 30 38 20 2a 2f 20 22 /* 108 */ "
bb4e0 50 61 72 61 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 Param",. /*
bb4f0 31 30 39 20 2a 2f 20 22 46 6b 43 6f 75 6e 74 65 109 */ "FkCounte
bb500 72 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 30 20 r",. /* 110
bb510 2a 2f 20 22 46 6b 49 66 5a 65 72 6f 22 2c 0a 20 */ "FkIfZero",.
bb520 20 20 20 20 2f 2a 20 31 31 31 20 2a 2f 20 22 4d /* 111 */ "M
bb530 65 6d 4d 61 78 22 2c 0a 20 20 20 20 20 2f 2a 20 emMax",. /*
bb540 31 31 32 20 2a 2f 20 22 49 66 50 6f 73 22 2c 0a 112 */ "IfPos",.
bb550 20 20 20 20 20 2f 2a 20 31 31 33 20 2a 2f 20 22 /* 113 */ "
bb560 49 66 4e 65 67 22 2c 0a 20 20 20 20 20 2f 2a 20 IfNeg",. /*
bb570 31 31 34 20 2a 2f 20 22 49 66 5a 65 72 6f 22 2c 114 */ "IfZero",
bb580 0a 20 20 20 20 20 2f 2a 20 31 31 35 20 2a 2f 20 . /* 115 */
bb590 22 41 67 67 53 74 65 70 22 2c 0a 20 20 20 20 20 "AggStep",.
bb5a0 2f 2a 20 31 31 36 20 2a 2f 20 22 41 67 67 46 69 /* 116 */ "AggFi
bb5b0 6e 61 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 nal",. /* 11
bb5c0 37 20 2a 2f 20 22 56 61 63 75 75 6d 22 2c 0a 20 7 */ "Vacuum",.
bb5d0 20 20 20 20 2f 2a 20 31 31 38 20 2a 2f 20 22 49 /* 118 */ "I
bb5e0 6e 63 72 56 61 63 75 75 6d 22 2c 0a 20 20 20 20 ncrVacuum",.
bb5f0 20 2f 2a 20 31 31 39 20 2a 2f 20 22 45 78 70 69 /* 119 */ "Expi
bb600 72 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 30 re",. /* 120
bb610 20 2a 2f 20 22 54 61 62 6c 65 4c 6f 63 6b 22 2c */ "TableLock",
bb620 0a 20 20 20 20 20 2f 2a 20 31 32 31 20 2a 2f 20 . /* 121 */
bb630 22 56 42 65 67 69 6e 22 2c 0a 20 20 20 20 20 2f "VBegin",. /
bb640 2a 20 31 32 32 20 2a 2f 20 22 56 43 72 65 61 74 * 122 */ "VCreat
bb650 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 33 20 e",. /* 123
bb660 2a 2f 20 22 56 44 65 73 74 72 6f 79 22 2c 0a 20 */ "VDestroy",.
bb670 20 20 20 20 2f 2a 20 31 32 34 20 2a 2f 20 22 56 /* 124 */ "V
bb680 4f 70 65 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 Open",. /* 1
bb690 32 35 20 2a 2f 20 22 56 46 69 6c 74 65 72 22 2c 25 */ "VFilter",
bb6a0 0a 20 20 20 20 20 2f 2a 20 31 32 36 20 2a 2f 20 . /* 126 */
bb6b0 22 56 43 6f 6c 75 6d 6e 22 2c 0a 20 20 20 20 20 "VColumn",.
bb6c0 2f 2a 20 31 32 37 20 2a 2f 20 22 56 4e 65 78 74 /* 127 */ "VNext
bb6d0 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 38 20 2a ",. /* 128 *
bb6e0 2f 20 22 56 52 65 6e 61 6d 65 22 2c 0a 20 20 20 / "VRename",.
bb6f0 20 20 2f 2a 20 31 32 39 20 2a 2f 20 22 56 55 70 /* 129 */ "VUp
bb700 64 61 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 date",. /* 1
bb710 33 30 20 2a 2f 20 22 52 65 61 6c 22 2c 0a 20 20 30 */ "Real",.
bb720 20 20 20 2f 2a 20 31 33 31 20 2a 2f 20 22 50 61 /* 131 */ "Pa
bb730 67 65 63 6f 75 6e 74 22 2c 0a 20 20 20 20 20 2f gecount",. /
bb740 2a 20 31 33 32 20 2a 2f 20 22 54 72 61 63 65 22 * 132 */ "Trace"
bb750 2c 0a 20 20 20 20 20 2f 2a 20 31 33 33 20 2a 2f ,. /* 133 */
bb760 20 22 4e 6f 6f 70 22 2c 0a 20 20 20 20 20 2f 2a "Noop",. /*
bb770 20 31 33 34 20 2a 2f 20 22 45 78 70 6c 61 69 6e 134 */ "Explain
bb780 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 35 20 2a ",. /* 135 *
bb790 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 35 22 2c / "NotUsed_135",
bb7a0 0a 20 20 20 20 20 2f 2a 20 31 33 36 20 2a 2f 20 . /* 136 */
bb7b0 22 4e 6f 74 55 73 65 64 5f 31 33 36 22 2c 0a 20 "NotUsed_136",.
bb7c0 20 20 20 20 2f 2a 20 31 33 37 20 2a 2f 20 22 4e /* 137 */ "N
bb7d0 6f 74 55 73 65 64 5f 31 33 37 22 2c 0a 20 20 20 otUsed_137",.
bb7e0 20 20 2f 2a 20 31 33 38 20 2a 2f 20 22 4e 6f 74 /* 138 */ "Not
bb7f0 55 73 65 64 5f 31 33 38 22 2c 0a 20 20 20 20 20 Used_138",.
bb800 2f 2a 20 31 33 39 20 2a 2f 20 22 4e 6f 74 55 73 /* 139 */ "NotUs
bb810 65 64 5f 31 33 39 22 2c 0a 20 20 20 20 20 2f 2a ed_139",. /*
bb820 20 31 34 30 20 2a 2f 20 22 4e 6f 74 55 73 65 64 140 */ "NotUsed
bb830 5f 31 34 30 22 2c 0a 20 20 20 20 20 2f 2a 20 31 _140",. /* 1
bb840 34 31 20 2a 2f 20 22 54 6f 54 65 78 74 22 2c 0a 41 */ "ToText",.
bb850 20 20 20 20 20 2f 2a 20 31 34 32 20 2a 2f 20 22 /* 142 */ "
bb860 54 6f 42 6c 6f 62 22 2c 0a 20 20 20 20 20 2f 2a ToBlob",. /*
bb870 20 31 34 33 20 2a 2f 20 22 54 6f 4e 75 6d 65 72 143 */ "ToNumer
bb880 69 63 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 34 ic",. /* 144
bb890 20 2a 2f 20 22 54 6f 49 6e 74 22 2c 0a 20 20 20 */ "ToInt",.
bb8a0 20 20 2f 2a 20 31 34 35 20 2a 2f 20 22 54 6f 52 /* 145 */ "ToR
bb8b0 65 61 6c 22 2c 0a 20 20 7d 3b 0a 20 20 72 65 74 eal",. };. ret
bb8c0 75 72 6e 20 61 7a 4e 61 6d 65 5b 69 5d 3b 0a 7d urn azName[i];.}
bb8d0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a .#endif../******
bb8e0 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
bb8f0 6f 70 63 6f 64 65 73 2e 63 20 2a 2a 2a 2a 2a 2a opcodes.c ******
bb900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb920 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
bb930 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
bb940 69 6c 65 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a ile os_os2.c ***
bb950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb970 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
bb980 30 30 36 20 46 65 62 20 31 34 0a 2a 2a 0a 2a 2a 006 Feb 14.**.**
bb990 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
bb9a0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
bb9b0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
bb9c0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
bb9d0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
bb9e0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
bb9f0 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
bba00 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
bba10 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
bba20 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
bba30 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
bba40 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
bba50 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
bba60 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
bba70 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
bba80 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
bba90 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
bbaa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
bbaf0 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
bbb00 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 68 61 ontains code tha
bbb10 74 20 69 73 20 73 70 65 63 69 66 69 63 20 74 6f t is specific to
bbb20 20 4f 53 2f 32 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 OS/2..**.** $Id
bbb30 3a 20 6f 73 5f 6f 73 32 2e 63 2c 76 20 31 2e 36 : os_os2.c,v 1.6
bbb40 33 20 32 30 30 38 2f 31 32 2f 31 30 20 31 39 3a 3 2008/12/10 19:
bbb50 32 36 3a 32 34 20 64 72 68 20 45 78 70 20 24 0a 26:24 drh Exp $.
bbb60 2a 2f 0a 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f */...#if SQLITE_
bbb70 4f 53 5f 4f 53 32 0a 0a 2f 2a 0a 2a 2a 20 41 20 OS_OS2../*.** A
bbb80 4e 6f 74 65 20 41 62 6f 75 74 20 4d 65 6d 6f 72 Note About Memor
bbb90 79 20 41 6c 6c 6f 63 61 74 69 6f 6e 3a 0a 2a 2a y Allocation:.**
bbba0 0a 2a 2a 20 54 68 69 73 20 64 72 69 76 65 72 20 .** This driver
bbbb0 75 73 65 73 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 uses malloc()/fr
bbbc0 65 65 28 29 20 64 69 72 65 63 74 6c 79 20 72 61 ee() directly ra
bbbd0 74 68 65 72 20 74 68 61 6e 20 67 6f 69 6e 67 20 ther than going
bbbe0 74 68 72 6f 75 67 68 0a 2a 2a 20 74 68 65 20 53 through.** the S
bbbf0 51 4c 69 74 65 2d 77 72 61 70 70 65 72 73 20 73 QLite-wrappers s
bbc00 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2f qlite3_malloc()/
bbc10 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 2e 20 sqlite3_free().
bbc20 20 54 68 6f 73 65 20 77 72 61 70 70 65 72 73 0a Those wrappers.
bbc30 2a 2a 20 61 72 65 20 64 65 73 69 67 6e 65 64 20 ** are designed
bbc40 66 6f 72 20 75 73 65 20 6f 6e 20 65 6d 62 65 64 for use on embed
bbc50 64 65 64 20 73 79 73 74 65 6d 73 20 77 68 65 72 ded systems wher
bbc60 65 20 6d 65 6d 6f 72 79 20 69 73 20 73 63 61 72 e memory is scar
bbc70 63 65 20 61 6e 64 0a 2a 2a 20 6d 61 6c 6c 6f 63 ce and.** malloc
bbc80 20 66 61 69 6c 75 72 65 73 20 68 61 70 70 65 6e failures happen
bbc90 20 66 72 65 71 75 65 6e 74 6c 79 2e 20 20 4f 53 frequently. OS
bbca0 2f 32 20 64 6f 65 73 20 6e 6f 74 20 74 79 70 69 /2 does not typi
bbcb0 63 61 6c 6c 79 20 72 75 6e 20 6f 6e 0a 2a 2a 20 cally run on.**
bbcc0 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d 73 embedded systems
bbcd0 2c 20 61 6e 64 20 77 68 65 6e 20 69 74 20 64 6f , and when it do
bbce0 65 73 20 74 68 65 20 64 65 76 65 6c 6f 70 65 72 es the developer
bbcf0 73 20 6e 6f 72 6d 61 6c 6c 79 20 68 61 76 65 20 s normally have
bbd00 62 69 67 67 65 72 0a 2a 2a 20 70 72 6f 62 6c 65 bigger.** proble
bbd10 6d 73 20 74 6f 20 77 6f 72 72 79 20 61 62 6f 75 ms to worry abou
bbd20 74 20 74 68 61 6e 20 72 75 6e 6e 69 6e 67 20 6f t than running o
bbd30 75 74 20 6f 66 20 6d 65 6d 6f 72 79 2e 20 20 53 ut of memory. S
bbd40 6f 20 74 68 65 72 65 20 69 73 20 6e 6f 74 0a 2a o there is not.*
bbd50 2a 20 61 20 63 6f 6d 70 65 6c 6c 69 6e 67 20 6e * a compelling n
bbd60 65 65 64 20 74 6f 20 75 73 65 20 74 68 65 20 77 eed to use the w
bbd70 72 61 70 70 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 42 rappers..**.** B
bbd80 75 74 20 74 68 65 72 65 20 69 73 20 61 20 67 6f ut there is a go
bbd90 6f 64 20 72 65 61 73 6f 6e 20 74 6f 20 6e 6f 74 od reason to not
bbda0 20 75 73 65 20 74 68 65 20 77 72 61 70 70 65 72 use the wrapper
bbdb0 73 2e 20 20 49 66 20 77 65 20 75 73 65 20 74 68 s. If we use th
bbdc0 65 0a 2a 2a 20 77 72 61 70 70 65 72 73 20 74 68 e.** wrappers th
bbdd0 65 6e 20 77 65 20 77 69 6c 6c 20 67 65 74 20 73 en we will get s
bbde0 69 6d 75 6c 61 74 65 64 20 6d 61 6c 6c 6f 63 28 imulated malloc(
bbdf0 29 20 66 61 69 6c 75 72 65 73 20 77 69 74 68 69 ) failures withi
bbe00 6e 20 74 68 69 73 0a 2a 2a 20 64 72 69 76 65 72 n this.** driver
bbe10 2e 20 20 41 6e 64 20 74 68 61 74 20 63 61 75 73 . And that caus
bbe20 65 73 20 61 6c 6c 20 6b 69 6e 64 73 20 6f 66 20 es all kinds of
bbe30 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20 6f 75 72 problems for our
bbe40 20 74 65 73 74 73 2e 20 20 57 65 0a 2a 2a 20 63 tests. We.** c
bbe50 6f 75 6c 64 20 65 6e 68 61 6e 63 65 20 53 51 4c ould enhance SQL
bbe60 69 74 65 20 74 6f 20 64 65 61 6c 20 77 69 74 68 ite to deal with
bbe70 20 73 69 6d 75 6c 61 74 65 64 20 6d 61 6c 6c 6f simulated mallo
bbe80 63 20 66 61 69 6c 75 72 65 73 20 77 69 74 68 69 c failures withi
bbe90 6e 0a 2a 2a 20 74 68 65 20 4f 53 20 64 72 69 76 n.** the OS driv
bbea0 65 72 2c 20 62 75 74 20 74 68 65 20 63 6f 64 65 er, but the code
bbeb0 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 74 68 to deal with th
bbec0 6f 73 65 20 66 61 69 6c 75 72 65 20 77 6f 75 6c ose failure woul
bbed0 64 20 6e 6f 74 0a 2a 2a 20 62 65 20 65 78 65 72 d not.** be exer
bbee0 63 69 73 65 64 20 6f 6e 20 4c 69 6e 75 78 20 28 cised on Linux (
bbef0 77 68 69 63 68 20 64 6f 65 73 20 6e 6f 74 20 6e which does not n
bbf00 65 65 64 20 74 6f 20 6d 61 6c 6c 6f 63 28 29 20 eed to malloc()
bbf10 69 6e 20 74 68 65 20 64 72 69 76 65 72 29 0a 2a in the driver).*
bbf20 2a 20 61 6e 64 20 73 6f 20 77 65 20 77 6f 75 6c * and so we woul
bbf30 64 20 68 61 76 65 20 64 69 66 66 69 63 75 6c 74 d have difficult
bbf40 79 20 77 72 69 74 69 6e 67 20 63 6f 76 65 72 61 y writing covera
bbf50 67 65 20 74 65 73 74 73 20 66 6f 72 20 74 68 61 ge tests for tha
bbf60 74 0a 2a 2a 20 63 6f 64 65 2e 20 20 42 65 74 74 t.** code. Bett
bbf70 65 72 20 74 6f 20 6c 65 61 76 65 20 74 68 65 20 er to leave the
bbf80 63 6f 64 65 20 6f 75 74 2c 20 77 65 20 74 68 69 code out, we thi
bbf90 6e 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f nk..**.** The po
bbfa0 69 6e 74 20 6f 66 20 74 68 69 73 20 64 69 73 63 int of this disc
bbfb0 75 73 73 69 6f 6e 20 69 73 20 61 73 20 66 6f 6c ussion is as fol
bbfc0 6c 6f 77 73 3a 20 20 57 68 65 6e 20 63 72 65 61 lows: When crea
bbfd0 74 69 6e 67 20 61 20 6e 65 77 0a 2a 2a 20 4f 53 ting a new.** OS
bbfe0 20 6c 61 79 65 72 20 66 6f 72 20 61 6e 20 65 6d layer for an em
bbff0 62 65 64 64 65 64 20 73 79 73 74 65 6d 2c 20 69 bedded system, i
bc000 66 20 79 6f 75 20 75 73 65 20 74 68 69 73 20 66 f you use this f
bc010 69 6c 65 20 61 73 20 61 6e 20 65 78 61 6d 70 6c ile as an exampl
bc020 65 2c 0a 2a 2a 20 61 76 6f 69 64 20 74 68 65 20 e,.** avoid the
bc030 75 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2f use of malloc()/
bc040 66 72 65 65 28 29 2e 20 20 54 68 6f 73 65 20 72 free(). Those r
bc050 6f 75 74 69 6e 65 73 20 77 6f 72 6b 20 6f 6b 20 outines work ok
bc060 6f 6e 20 4f 53 2f 32 0a 2a 2a 20 64 65 73 6b 74 on OS/2.** deskt
bc070 6f 70 73 20 62 75 74 20 6e 6f 74 20 73 6f 20 77 ops but not so w
bc080 65 6c 6c 20 69 6e 20 65 6d 62 65 64 64 65 64 20 ell in embedded
bc090 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a 0a 2f 2a 0a systems..*/../*.
bc0a0 2a 2a 20 4d 61 63 72 6f 73 20 75 73 65 64 20 74 ** Macros used t
bc0b0 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65 74 o determine whet
bc0c0 68 65 72 20 6f 72 20 6e 6f 74 20 74 6f 20 75 73 her or not to us
bc0d0 65 20 74 68 72 65 61 64 73 2e 0a 2a 2f 0a 23 69 e threads..*/.#i
bc0e0 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
bc0f0 5f 54 48 52 45 41 44 53 41 46 45 29 20 26 26 20 _THREADSAFE) &&
bc100 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
bc110 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 E.# define SQLIT
bc120 45 5f 4f 53 32 5f 54 48 52 45 41 44 53 20 31 0a E_OS2_THREADS 1.
bc130 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e #endif../*.** In
bc140 63 6c 75 64 65 20 63 6f 64 65 20 74 68 61 74 20 clude code that
bc150 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c is common to all
bc160 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f os_*.c files.*/
bc170 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
bc180 20 49 6e 63 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d Include os_comm
bc190 6f 6e 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 on.h in the midd
bc1a0 6c 65 20 6f 66 20 6f 73 5f 6f 73 32 2e 63 20 2a le of os_os2.c *
bc1b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
bc1c0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
bc1d0 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 63 Begin file os_c
bc1e0 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a ommon.h ********
bc1f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
bc210 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 ./*.** 2004 May
bc220 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 22.**.** The aut
bc230 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
bc240 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
bc250 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
bc260 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
bc270 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
bc280 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
bc290 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
bc2a0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
bc2b0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
bc2c0 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
bc2d0 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
bc2e0 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
bc2f0 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
bc300 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
bc310 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
bc320 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
bc330 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
bc340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc380 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 ******.**.** Thi
bc390 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
bc3a0 6d 61 63 72 6f 73 20 61 6e 64 20 61 20 6c 69 74 macros and a lit
bc3b0 74 6c 65 20 62 69 74 20 6f 66 20 63 6f 64 65 20 tle bit of code
bc3c0 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 that is common t
bc3d0 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74 68 65 20 o.** all of the
bc3e0 70 6c 61 74 66 6f 72 6d 2d 73 70 65 63 69 66 69 platform-specifi
bc3f0 63 20 66 69 6c 65 73 20 28 6f 73 5f 2a 2e 63 29 c files (os_*.c)
bc400 20 61 6e 64 20 69 73 20 23 69 6e 63 6c 75 64 65 and is #include
bc410 64 20 69 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a 20 d into those.**
bc420 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 files..**.** Thi
bc430 73 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 s file should be
bc440 20 23 69 6e 63 6c 75 64 65 64 20 62 79 20 74 68 #included by th
bc450 65 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 20 6f e os_*.c files o
bc460 6e 6c 79 2e 20 20 49 74 20 69 73 20 6e 6f 74 20 nly. It is not
bc470 61 0a 2a 2a 20 67 65 6e 65 72 61 6c 20 70 75 72 a.** general pur
bc480 70 6f 73 65 20 68 65 61 64 65 72 20 66 69 6c 65 pose header file
bc490 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f ..**.** $Id: os_
bc4a0 63 6f 6d 6d 6f 6e 2e 68 2c 76 20 31 2e 33 38 20 common.h,v 1.38
bc4b0 32 30 30 39 2f 30 32 2f 32 34 20 31 38 3a 34 30 2009/02/24 18:40
bc4c0 3a 35 30 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 :50 danielk1977
bc4d0 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 Exp $.*/.#ifndef
bc4e0 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23 _OS_COMMON_H_.#
bc4f0 64 65 66 69 6e 65 20 5f 4f 53 5f 43 4f 4d 4d 4f define _OS_COMMO
bc500 4e 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c N_H_../*.** At l
bc510 65 61 73 74 20 74 77 6f 20 62 75 67 73 20 68 61 east two bugs ha
bc520 76 65 20 73 6c 69 70 70 65 64 20 69 6e 20 62 65 ve slipped in be
bc530 63 61 75 73 65 20 77 65 20 63 68 61 6e 67 65 64 cause we changed
bc540 20 74 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 the MEMORY_DEBU
bc550 47 0a 2a 2a 20 6d 61 63 72 6f 20 74 6f 20 53 51 G.** macro to SQ
bc560 4c 49 54 45 5f 44 45 42 55 47 20 61 6e 64 20 73 LITE_DEBUG and s
bc570 6f 6d 65 20 6f 6c 64 65 72 20 6d 61 6b 65 66 69 ome older makefi
bc580 6c 65 73 20 68 61 76 65 20 6e 6f 74 20 79 65 74 les have not yet
bc590 20 6d 61 64 65 20 74 68 65 0a 2a 2a 20 73 77 69 made the.** swi
bc5a0 74 63 68 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 tch. The follow
bc5b0 69 6e 67 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 ing code should
bc5c0 63 61 74 63 68 20 74 68 69 73 20 70 72 6f 62 6c catch this probl
bc5d0 65 6d 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 em at compile-ti
bc5e0 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 4d 45 me..*/.#ifdef ME
bc5f0 4d 4f 52 59 5f 44 45 42 55 47 0a 23 20 65 72 72 MORY_DEBUG.# err
bc600 6f 72 20 22 54 68 65 20 4d 45 4d 4f 52 59 5f 44 or "The MEMORY_D
bc610 45 42 55 47 20 6d 61 63 72 6f 20 69 73 20 6f 62 EBUG macro is ob
bc620 73 6f 6c 65 74 65 2e 20 20 55 73 65 20 53 51 4c solete. Use SQL
bc630 49 54 45 5f 44 45 42 55 47 20 69 6e 73 74 65 61 ITE_DEBUG instea
bc640 64 2e 22 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 d.".#endif..#ifd
bc650 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
bc660 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
bc670 6e 74 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 nt sqlite3OSTrac
bc680 65 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f e = 0;.#define O
bc690 53 54 52 41 43 45 31 28 58 29 20 20 20 20 20 20 STRACE1(X)
bc6a0 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 if( sqlite3OS
bc6b0 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 Trace ) sqlite3D
bc6c0 65 62 75 67 50 72 69 6e 74 66 28 58 29 0a 23 64 ebugPrintf(X).#d
bc6d0 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 28 58 efine OSTRACE2(X
bc6e0 2c 59 29 20 20 20 20 20 20 20 69 66 28 20 73 71 ,Y) if( sq
bc6f0 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 lite3OSTrace ) s
bc700 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 qlite3DebugPrint
bc710 66 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f f(X,Y).#define O
bc720 53 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 20 20 STRACE3(X,Y,Z)
bc730 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 if( sqlite3OS
bc740 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 Trace ) sqlite3D
bc750 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a ebugPrintf(X,Y,Z
bc760 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
bc770 45 34 28 58 2c 59 2c 5a 2c 41 29 20 20 20 69 66 E4(X,Y,Z,A) if
bc780 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 ( sqlite3OSTrace
bc790 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 ) sqlite3DebugP
bc7a0 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 29 0a 23 rintf(X,Y,Z,A).#
bc7b0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28 define OSTRACE5(
bc7c0 58 2c 59 2c 5a 2c 41 2c 42 29 20 69 66 28 20 73 X,Y,Z,A,B) if( s
bc7d0 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 qlite3OSTrace )
bc7e0 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
bc7f0 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 tf(X,Y,Z,A,B).#d
bc800 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28 58 efine OSTRACE6(X
bc810 2c 59 2c 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20 20 ,Y,Z,A,B,C) \.
bc820 20 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54 72 if(sqlite3OSTr
bc830 61 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 75 ace) sqlite3Debu
bc840 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c gPrintf(X,Y,Z,A,
bc850 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 B,C).#define OST
bc860 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c RACE7(X,Y,Z,A,B,
bc870 43 2c 44 29 20 5c 0a 20 20 20 20 69 66 28 73 71 C,D) \. if(sq
bc880 6c 69 74 65 33 4f 53 54 72 61 63 65 29 20 73 71 lite3OSTrace) sq
bc890 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 lite3DebugPrintf
bc8a0 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a (X,Y,Z,A,B,C,D).
bc8b0 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 53 #else.#define OS
bc8c0 54 52 41 43 45 31 28 58 29 0a 23 64 65 66 69 6e TRACE1(X).#defin
bc8d0 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 0a e OSTRACE2(X,Y).
bc8e0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 33 #define OSTRACE3
bc8f0 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 (X,Y,Z).#define
bc900 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 OSTRACE4(X,Y,Z,A
bc910 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
bc920 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 E5(X,Y,Z,A,B).#d
bc930 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28 58 efine OSTRACE6(X
bc940 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 ,Y,Z,A,B,C).#def
bc950 69 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c 59 ine OSTRACE7(X,Y
bc960 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6e 64 ,Z,A,B,C,D).#end
bc970 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 if../*.** Macros
bc980 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 for performance
bc990 20 74 72 61 63 69 6e 67 2e 20 20 4e 6f 72 6d 61 tracing. Norma
bc9a0 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 2e 20 lly turned off.
bc9b0 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20 6f Only works.** o
bc9c0 6e 20 69 34 38 36 20 68 61 72 64 77 61 72 65 2e n i486 hardware.
bc9d0 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
bc9e0 45 5f 50 45 52 46 4f 52 4d 41 4e 43 45 5f 54 52 E_PERFORMANCE_TR
bc9f0 41 43 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69 ACE../* .** hwti
bca00 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20 69 6e me.h contains in
bca10 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65 72 20 63 line assembler c
bca20 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e ode for implemen
bca30 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d 70 65 ting .** high-pe
bca40 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69 6e 67 rformance timing
bca50 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a routines..*/./*
bca60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e ************* In
bca70 63 6c 75 64 65 20 68 77 74 69 6d 65 2e 68 20 69 clude hwtime.h i
bca80 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 n the middle of
bca90 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a os_common.h ****
bcaa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
bcab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
bcac0 67 69 6e 20 66 69 6c 65 20 68 77 74 69 6d 65 2e gin file hwtime.
bcad0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
bcae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcaf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
bcb00 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 37 0a .** 2008 May 27.
bcb10 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
bcb20 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
bcb30 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
bcb40 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
bcb50 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
bcb60 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
bcb70 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
bcb80 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
bcb90 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
bcba0 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
bcbb0 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
bcbc0 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
bcbd0 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
bcbe0 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
bcbf0 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
bcc00 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
bcc10 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
bcc20 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
bcc30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcc40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcc50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcc60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcc70 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ***.**.** This f
bcc80 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c ile contains inl
bcc90 69 6e 65 20 61 73 6d 20 63 6f 64 65 20 66 6f 72 ine asm code for
bcca0 20 72 65 74 72 69 65 76 69 6e 67 20 22 68 69 67 retrieving "hig
bccb0 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22 0a 2a h-performance".*
bccc0 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f 72 20 78 * counters for x
bccd0 38 36 20 63 6c 61 73 73 20 43 50 55 73 2e 0a 2a 86 class CPUs..*
bcce0 2a 0a 2a 2a 20 24 49 64 3a 20 68 77 74 69 6d 65 *.** $Id: hwtime
bccf0 2e 68 2c 76 20 31 2e 33 20 32 30 30 38 2f 30 38 .h,v 1.3 2008/08
bcd00 2f 30 31 20 31 34 3a 33 33 3a 31 35 20 73 68 61 /01 14:33:15 sha
bcd10 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e ne Exp $.*/.#ifn
bcd20 64 65 66 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 23 def _HWTIME_H_.#
bcd30 64 65 66 69 6e 65 20 5f 48 57 54 49 4d 45 5f 48 define _HWTIME_H
bcd40 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c _../*.** The fol
bcd50 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20 6f lowing routine o
bcd60 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 70 65 6e nly works on pen
bcd70 74 69 75 6d 2d 63 6c 61 73 73 20 28 6f 72 20 6e tium-class (or n
bcd80 65 77 65 72 29 20 70 72 6f 63 65 73 73 6f 72 73 ewer) processors
bcd90 2e 0a 2a 2a 20 49 74 20 75 73 65 73 20 74 68 65 ..** It uses the
bcda0 20 52 44 54 53 43 20 6f 70 63 6f 64 65 20 74 6f RDTSC opcode to
bcdb0 20 72 65 61 64 20 74 68 65 20 63 79 63 6c 65 20 read the cycle
bcdc0 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75 74 20 count value out
bcdd0 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 63 65 73 of the.** proces
bcde0 73 6f 72 20 61 6e 64 20 72 65 74 75 72 6e 73 20 sor and returns
bcdf0 74 68 61 74 20 76 61 6c 75 65 2e 20 20 54 68 69 that value. Thi
bce00 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f s can be used fo
bce10 72 20 68 69 67 68 2d 72 65 73 0a 2a 2a 20 70 72 r high-res.** pr
bce20 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66 20 ofiling..*/.#if
bce30 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f (defined(__GNUC_
bce40 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d _) || defined(_M
bce50 53 43 5f 56 45 52 29 29 20 26 26 20 5c 0a 20 20 SC_VER)) && \.
bce60 20 20 20 20 28 64 65 66 69 6e 65 64 28 69 33 38 (defined(i38
bce70 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 6) || defined(__
bce80 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e i386__) || defin
bce90 65 64 28 5f 4d 5f 49 58 38 36 29 29 0a 0a 20 20 ed(_M_IX86))..
bcea0 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e #if defined(__GN
bceb0 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e UC__).. __inlin
bcec0 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 e__ sqlite_uint6
bced0 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 4 sqlite3Hwtime(
bcee0 76 6f 69 64 29 7b 0a 20 20 20 20 20 75 6e 73 69 void){. unsi
bcef0 67 6e 65 64 20 69 6e 74 20 6c 6f 2c 20 68 69 3b gned int lo, hi;
bcf00 0a 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f . __asm__ __
bcf10 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 volatile__ ("rdt
bcf20 73 63 22 20 3a 20 22 3d 61 22 20 28 6c 6f 29 2c sc" : "=a" (lo),
bcf30 20 22 3d 64 22 20 28 68 69 29 29 3b 0a 20 20 20 "=d" (hi));.
bcf40 20 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 return (sqlite
bcf50 5f 75 69 6e 74 36 34 29 68 69 20 3c 3c 20 33 32 _uint64)hi << 32
bcf60 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23 65 | lo;. }.. #e
bcf70 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 lif defined(_MSC
bcf80 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 65 63 6c 73 _VER).. __decls
bcf90 70 65 63 28 6e 61 6b 65 64 29 20 5f 5f 69 6e 6c pec(naked) __inl
bcfa0 69 6e 65 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 ine sqlite_uint6
bcfb0 34 20 5f 5f 63 64 65 63 6c 20 73 71 6c 69 74 65 4 __cdecl sqlite
bcfc0 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 3Hwtime(void){.
bcfd0 20 20 20 20 5f 5f 61 73 6d 20 7b 0a 20 20 20 20 __asm {.
bcfe0 20 20 20 20 72 64 74 73 63 0a 20 20 20 20 20 20 rdtsc.
bcff0 20 20 72 65 74 20 20 20 20 20 20 20 3b 20 72 65 ret ; re
bd000 74 75 72 6e 20 76 61 6c 75 65 20 61 74 20 45 44 turn value at ED
bd010 58 3a 45 41 58 0a 20 20 20 20 20 7d 0a 20 20 7d X:EAX. }. }
bd020 0a 0a 20 20 23 65 6e 64 69 66 0a 0a 23 65 6c 69 .. #endif..#eli
bd030 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 f (defined(__GNU
bd040 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28 C__) && defined(
bd050 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 0a 0a 20 20 __x86_64__))..
bd060 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 __inline__ sqlit
bd070 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 e_uint64 sqlite3
bd080 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 Hwtime(void){.
bd090 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e unsigned lon
bd0a0 67 20 76 61 6c 3b 0a 20 20 20 20 20 20 5f 5f 61 g val;. __a
bd0b0 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f sm__ __volatile_
bd0c0 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d 41 _ ("rdtsc" : "=A
bd0d0 22 20 28 76 61 6c 29 29 3b 0a 20 20 20 20 20 20 " (val));.
bd0e0 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20 7d 0a return val;. }.
bd0f0 20 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64 .#elif (defined
bd100 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 (__GNUC__) && de
bd110 66 69 6e 65 64 28 5f 5f 70 70 63 5f 5f 29 29 0a fined(__ppc__)).
bd120 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 . __inline__ sq
bd130 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 lite_uint64 sqli
bd140 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b te3Hwtime(void){
bd150 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 . unsigned
bd160 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 76 61 6c long long retval
bd170 3b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 ;. unsigned
bd180 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20 20 long junk;.
bd190 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 __asm__ __vola
bd1a0 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20 20 tile__ ("\n\.
bd1b0 20 20 20 20 20 20 20 31 3a 20 20 20 20 20 20 6d 1: m
bd1c0 66 74 62 75 20 20 20 25 31 5c 6e 5c 0a 20 20 20 ftbu %1\n\.
bd1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d m
bd1e0 66 74 62 20 20 20 20 25 4c 30 5c 6e 5c 0a 20 20 ftb %L0\n\.
bd1f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bd200 6d 66 74 62 75 20 20 20 25 30 5c 6e 5c 0a 20 20 mftbu %0\n\.
bd210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bd220 63 6d 70 77 20 20 20 20 25 30 2c 25 31 5c 6e 5c cmpw %0,%1\n\
bd230 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
bd240 20 20 20 62 6e 65 20 20 20 20 20 31 62 22 0a 20 bne 1b".
bd250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bd260 20 3a 20 22 3d 72 22 20 28 72 65 74 76 61 6c 29 : "=r" (retval)
bd270 2c 20 22 3d 72 22 20 28 6a 75 6e 6b 29 29 3b 0a , "=r" (junk));.
bd280 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 65 74 return ret
bd290 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a val;. }..#else.
bd2a0 0a 20 20 23 65 72 72 6f 72 20 4e 65 65 64 20 69 . #error Need i
bd2b0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
bd2c0 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 sqlite3Hwtime()
bd2d0 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f for your platfo
bd2e0 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 rm... /*. ** T
bd2f0 6f 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 6f 75 o compile withou
bd300 74 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 73 t implementing s
bd310 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 66 qlite3Hwtime() f
bd320 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d or your platform
bd330 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 61 6e 20 72 ,. ** you can r
bd340 65 6d 6f 76 65 20 74 68 65 20 61 62 6f 76 65 20 emove the above
bd350 23 65 72 72 6f 72 20 61 6e 64 20 75 73 65 20 74 #error and use t
bd360 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a he following. *
bd370 2a 20 73 74 75 62 20 66 75 6e 63 74 69 6f 6e 2e * stub function.
bd380 20 20 59 6f 75 20 77 69 6c 6c 20 6c 6f 73 65 20 You will lose
bd390 74 69 6d 69 6e 67 20 73 75 70 70 6f 72 74 20 66 timing support f
bd3a0 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66 20 or many. ** of
bd3b0 74 68 65 20 64 65 62 75 67 67 69 6e 67 20 61 6e the debugging an
bd3c0 64 20 74 65 73 74 69 6e 67 20 75 74 69 6c 69 74 d testing utilit
bd3d0 69 65 73 2c 20 62 75 74 20 69 74 20 73 68 6f 75 ies, but it shou
bd3e0 6c 64 20 61 74 0a 20 20 2a 2a 20 6c 65 61 73 74 ld at. ** least
bd3f0 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e compile and run
bd400 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 .. */.SQLITE_PR
bd410 49 56 41 54 45 20 20 20 73 71 6c 69 74 65 5f 75 IVATE sqlite_u
bd420 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 int64 sqlite3Hwt
bd430 69 6d 65 28 76 6f 69 64 29 7b 20 72 65 74 75 72 ime(void){ retur
bd440 6e 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 n ((sqlite_uint6
bd450 34 29 30 29 3b 20 7d 0a 0a 23 65 6e 64 69 66 0a 4)0); }..#endif.
bd460 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 .#endif /* !defi
bd470 6e 65 64 28 5f 48 57 54 49 4d 45 5f 48 5f 29 20 ned(_HWTIME_H_)
bd480 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
bd490 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 77 74 69 6d *** End of hwtim
bd4a0 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e.h ************
bd4b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd4d0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
bd4e0 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 *** Continuing w
bd4f0 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 here we left off
bd500 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 in os_common.h
bd510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd520 2a 2a 2f 0a 0a 73 74 61 74 69 63 20 73 71 6c 69 **/..static sqli
bd530 74 65 5f 75 69 6e 74 36 34 20 67 5f 73 74 61 72 te_uint64 g_star
bd540 74 3b 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 t;.static sqlite
bd550 5f 75 69 6e 74 36 34 20 67 5f 65 6c 61 70 73 65 _uint64 g_elapse
bd560 64 3b 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 d;.#define TIMER
bd570 5f 53 54 41 52 54 20 20 20 20 20 20 20 67 5f 73 _START g_s
bd580 74 61 72 74 3d 73 71 6c 69 74 65 33 48 77 74 69 tart=sqlite3Hwti
bd590 6d 65 28 29 0a 23 64 65 66 69 6e 65 20 54 49 4d me().#define TIM
bd5a0 45 52 5f 45 4e 44 20 20 20 20 20 20 20 20 20 67 ER_END g
bd5b0 5f 65 6c 61 70 73 65 64 3d 73 71 6c 69 74 65 33 _elapsed=sqlite3
bd5c0 48 77 74 69 6d 65 28 29 2d 67 5f 73 74 61 72 74 Hwtime()-g_start
bd5d0 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 .#define TIMER_E
bd5e0 4c 41 50 53 45 44 20 20 20 20 20 67 5f 65 6c 61 LAPSED g_ela
bd5f0 70 73 65 64 0a 23 65 6c 73 65 0a 23 64 65 66 69 psed.#else.#defi
bd600 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54 0a 23 ne TIMER_START.#
bd610 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 define TIMER_END
bd620 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 .#define TIMER_E
bd630 4c 41 50 53 45 44 20 20 20 20 20 28 28 73 71 6c LAPSED ((sql
bd640 69 74 65 5f 75 69 6e 74 36 34 29 30 29 0a 23 65 ite_uint64)0).#e
bd650 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 ndif../*.** If w
bd660 65 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 20 74 e compile with t
bd670 68 65 20 53 51 4c 49 54 45 5f 54 45 53 54 20 6d he SQLITE_TEST m
bd680 61 63 72 6f 20 73 65 74 2c 20 74 68 65 6e 20 74 acro set, then t
bd690 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f he following blo
bd6a0 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64 65 20 77 69 ck.** of code wi
bd6b0 6c 6c 20 67 69 76 65 20 75 73 20 74 68 65 20 61 ll give us the a
bd6c0 62 69 6c 69 74 79 20 74 6f 20 73 69 6d 75 6c 61 bility to simula
bd6d0 74 65 20 61 20 64 69 73 6b 20 49 2f 4f 20 65 72 te a disk I/O er
bd6e0 72 6f 72 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73 ror. This.** is
bd6f0 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e used for testin
bd700 67 20 74 68 65 20 49 2f 4f 20 72 65 63 6f 76 65 g the I/O recove
bd710 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 69 66 ry logic..*/.#if
bd720 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
bd730 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
bd740 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
bd750 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 hit = 0;
bd760 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d /* Total num
bd770 62 65 72 20 6f 66 20 49 2f 4f 20 45 72 72 6f 72 ber of I/O Error
bd780 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 s */.SQLITE_API
bd790 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 int sqlite3_io_e
bd7a0 72 72 6f 72 5f 68 61 72 64 68 69 74 20 3d 20 30 rror_hardhit = 0
bd7b0 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 ; /* Numb
bd7c0 65 72 20 6f 66 20 6e 6f 6e 2d 62 65 6e 69 67 6e er of non-benign
bd7d0 20 65 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 errors */.SQLIT
bd7e0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
bd7f0 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 3_io_error_pendi
bd800 6e 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f ng = 0; /
bd810 2a 20 43 6f 75 6e 74 20 64 6f 77 6e 20 74 6f 20 * Count down to
bd820 66 69 72 73 74 20 49 2f 4f 20 65 72 72 6f 72 20 first I/O error
bd830 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
bd840 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 t sqlite3_io_err
bd850 6f 72 5f 70 65 72 73 69 73 74 20 3d 20 30 3b 20 or_persist = 0;
bd860 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
bd870 66 20 49 2f 4f 20 65 72 72 6f 72 73 20 70 65 72 f I/O errors per
bd880 73 69 73 74 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 sist */.SQLITE_A
bd890 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 PI int sqlite3_i
bd8a0 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 3d o_error_benign =
bd8b0 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 0; /* T
bd8c0 72 75 65 20 69 66 20 65 72 72 6f 72 73 20 61 72 rue if errors ar
bd8d0 65 20 62 65 6e 69 67 6e 20 2a 2f 0a 53 51 4c 49 e benign */.SQLI
bd8e0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
bd8f0 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 e3_diskfull_pend
bd900 69 6e 67 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f ing = 0;.SQLITE_
bd910 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
bd920 64 69 73 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23 64 diskfull = 0;.#d
bd930 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f efine SimulateIO
bd940 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 20 73 ErrorBenign(X) s
bd950 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
bd960 62 65 6e 69 67 6e 3d 28 58 29 0a 23 64 65 66 69 benign=(X).#defi
bd970 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 ne SimulateIOErr
bd980 6f 72 28 43 4f 44 45 29 20 20 5c 0a 20 20 69 66 or(CODE) \. if
bd990 28 20 28 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 ( (sqlite3_io_er
bd9a0 72 6f 72 5f 70 65 72 73 69 73 74 20 26 26 20 73 ror_persist && s
bd9b0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
bd9c0 68 69 74 29 20 5c 0a 20 20 20 20 20 20 20 7c 7c hit) \. ||
bd9d0 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
bd9e0 72 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20 31 r_pending-- == 1
bd9f0 20 29 20 20 5c 0a 20 20 20 20 20 20 20 20 20 20 ) \.
bda00 20 20 20 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65 72 { local_ioer
bda10 72 28 29 3b 20 43 4f 44 45 3b 20 7d 0a 73 74 61 r(); CODE; }.sta
bda20 74 69 63 20 76 6f 69 64 20 6c 6f 63 61 6c 5f 69 tic void local_i
bda30 6f 65 72 72 28 29 7b 0a 20 20 49 4f 54 52 41 43 oerr(){. IOTRAC
bda40 45 28 28 22 49 4f 45 52 52 5c 6e 22 29 29 3b 0a E(("IOERR\n"));.
bda50 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 sqlite3_io_err
bda60 6f 72 5f 68 69 74 2b 2b 3b 0a 20 20 69 66 28 20 or_hit++;. if(
bda70 21 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f !sqlite3_io_erro
bda80 72 5f 62 65 6e 69 67 6e 20 29 20 73 71 6c 69 74 r_benign ) sqlit
bda90 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 e3_io_error_hard
bdaa0 68 69 74 2b 2b 3b 0a 7d 0a 23 64 65 66 69 6e 65 hit++;.}.#define
bdab0 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c SimulateDiskful
bdac0 6c 45 72 72 6f 72 28 43 4f 44 45 29 20 5c 0a 20 lError(CODE) \.
bdad0 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64 69 if( sqlite3_di
bdae0 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 29 skfull_pending )
bdaf0 7b 20 5c 0a 20 20 20 20 20 69 66 28 20 73 71 6c { \. if( sql
bdb00 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 ite3_diskfull_pe
bdb10 6e 64 69 6e 67 20 3d 3d 20 31 20 29 7b 20 5c 0a nding == 1 ){ \.
bdb20 20 20 20 20 20 20 20 6c 6f 63 61 6c 5f 69 6f 65 local_ioe
bdb30 72 72 28 29 3b 20 5c 0a 20 20 20 20 20 20 20 73 rr(); \. s
bdb40 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 qlite3_diskfull
bdb50 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 = 1; \. sq
bdb60 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 lite3_io_error_h
bdb70 69 74 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 it = 1; \.
bdb80 20 43 4f 44 45 3b 20 5c 0a 20 20 20 20 20 7d 65 CODE; \. }e
bdb90 6c 73 65 7b 20 5c 0a 20 20 20 20 20 20 20 73 71 lse{ \. sq
bdba0 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 lite3_diskfull_p
bdbb0 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a 20 20 20 20 ending--; \.
bdbc0 20 7d 20 5c 0a 20 20 20 7d 0a 23 65 6c 73 65 0a } \. }.#else.
bdbd0 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 #define Simulate
bdbe0 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 IOErrorBenign(X)
bdbf0 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 .#define Simulat
bdc00 65 49 4f 45 72 72 6f 72 28 41 29 0a 23 64 65 66 eIOError(A).#def
bdc10 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73 6b ine SimulateDisk
bdc20 66 75 6c 6c 45 72 72 6f 72 28 41 29 0a 23 65 6e fullError(A).#en
bdc30 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 dif../*.** When
bdc40 74 65 73 74 69 6e 67 2c 20 6b 65 65 70 20 61 20 testing, keep a
bdc50 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d count of the num
bdc60 62 65 72 20 6f 66 20 6f 70 65 6e 20 66 69 6c 65 ber of open file
bdc70 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c s..*/.#ifdef SQL
bdc80 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f ITE_TEST.SQLITE_
bdc90 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
bdca0 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 20 open_file_count
bdcb0 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 70 65 = 0;.#define Ope
bdcc0 6e 43 6f 75 6e 74 65 72 28 58 29 20 20 73 71 6c nCounter(X) sql
bdcd0 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 ite3_open_file_c
bdce0 6f 75 6e 74 2b 3d 28 58 29 0a 23 65 6c 73 65 0a ount+=(X).#else.
bdcf0 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e #define OpenCoun
bdd00 74 65 72 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23 ter(X).#endif..#
bdd10 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 endif /* !define
bdd20 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 29 d(_OS_COMMON_H_)
bdd30 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../**********
bdd40 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 63 **** End of os_c
bdd50 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a ommon.h ********
bdd60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bdd70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bdd80 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
bdd90 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 **** Continuing
bdda0 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 where we left of
bddb0 66 20 69 6e 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a f in os_os2.c **
bddc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bddd0 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ***/../*.** The
bdde0 6f 73 32 46 69 6c 65 20 73 74 72 75 63 74 75 72 os2File structur
bddf0 65 20 69 73 20 73 75 62 63 6c 61 73 73 20 6f 66 e is subclass of
bde00 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 70 sqlite3_file sp
bde10 65 63 69 66 69 63 20 66 6f 72 20 74 68 65 20 4f ecific for the O
bde20 53 2f 32 0a 2a 2a 20 70 72 6f 74 61 62 69 6c 69 S/2.** protabili
bde30 74 79 20 6c 61 79 65 72 2e 0a 2a 2f 0a 74 79 70 ty layer..*/.typ
bde40 65 64 65 66 20 73 74 72 75 63 74 20 6f 73 32 46 edef struct os2F
bde50 69 6c 65 20 6f 73 32 46 69 6c 65 3b 0a 73 74 72 ile os2File;.str
bde60 75 63 74 20 6f 73 32 46 69 6c 65 20 7b 0a 20 20 uct os2File {.
bde70 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f const sqlite3_io
bde80 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f _methods *pMetho
bde90 64 3b 20 20 2f 2a 20 41 6c 77 61 79 73 20 74 68 d; /* Always th
bdea0 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 2a 2f e first entry */
bdeb0 0a 20 20 48 46 49 4c 45 20 68 3b 20 20 20 20 20 . HFILE h;
bdec0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
bded0 48 61 6e 64 6c 65 20 66 6f 72 20 61 63 63 65 73 Handle for acces
bdee0 73 69 6e 67 20 74 68 65 20 66 69 6c 65 20 2a 2f sing the file */
bdef0 0a 20 20 63 68 61 72 2a 20 70 61 74 68 54 6f 44 . char* pathToD
bdf00 65 6c 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 el; /*
bdf10 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 Name of file to
bdf20 64 65 6c 65 74 65 20 6f 6e 20 63 6c 6f 73 65 2c delete on close,
bdf30 20 4e 55 4c 4c 20 69 66 20 6e 6f 74 20 2a 2f 0a NULL if not */.
bdf40 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
bdf50 6c 6f 63 6b 74 79 70 65 3b 20 20 20 2f 2a 20 54 locktype; /* T
bdf60 79 70 65 20 6f 66 20 6c 6f 63 6b 20 63 75 72 72 ype of lock curr
bdf70 65 6e 74 6c 79 20 68 65 6c 64 20 6f 6e 20 74 68 ently held on th
bdf80 69 73 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 23 is file */.};..#
bdf90 64 65 66 69 6e 65 20 4c 4f 43 4b 5f 54 49 4d 45 define LOCK_TIME
bdfa0 4f 55 54 20 31 30 4c 20 2f 2a 20 74 68 65 20 64 OUT 10L /* the d
bdfb0 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e 67 20 74 efault locking t
bdfc0 69 6d 65 6f 75 74 20 2a 2f 0a 0a 2f 2a 2a 2a 2a imeout */../****
bdfd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bdfe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bdff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be010 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 *********.** The
be020 20 6e 65 78 74 20 67 72 6f 75 70 20 6f 66 20 72 next group of r
be030 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e outines implemen
be040 74 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 t the I/O method
be050 73 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 s specified.** b
be060 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f y the sqlite3_io
be070 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e _methods object.
be080 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
be090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be0a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be0b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
be0d0 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 ../*.** Close a
be0e0 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 file..*/.static
be0f0 69 6e 74 20 6f 73 32 43 6c 6f 73 65 28 20 73 71 int os2Close( sq
be100 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 20 29 lite3_file *id )
be110 7b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 {. APIRET rc =
be120 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 6f 73 32 46 NO_ERROR;. os2F
be130 69 6c 65 20 2a 70 46 69 6c 65 3b 0a 20 20 69 66 ile *pFile;. if
be140 28 20 69 64 20 26 26 20 28 70 46 69 6c 65 20 3d ( id && (pFile =
be150 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 20 21 (os2File*)id) !
be160 3d 20 30 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 = 0 ){. OSTRA
be170 43 45 32 28 20 22 43 4c 4f 53 45 20 25 64 5c 6e CE2( "CLOSE %d\n
be180 22 2c 20 70 46 69 6c 65 2d 3e 68 20 29 3b 0a 20 ", pFile->h );.
be190 20 20 20 72 63 20 3d 20 44 6f 73 43 6c 6f 73 65 rc = DosClose
be1a0 28 20 70 46 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 ( pFile->h );.
be1b0 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 pFile->locktyp
be1c0 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 e = NO_LOCK;.
be1d0 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 61 74 68 if( pFile->path
be1e0 54 6f 44 65 6c 20 21 3d 20 4e 55 4c 4c 20 29 7b ToDel != NULL ){
be1f0 0a 20 20 20 20 20 20 72 63 20 3d 20 44 6f 73 46 . rc = DosF
be200 6f 72 63 65 44 65 6c 65 74 65 28 20 28 50 53 5a orceDelete( (PSZ
be210 29 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 )pFile->pathToDe
be220 6c 20 29 3b 0a 20 20 20 20 20 20 66 72 65 65 28 l );. free(
be230 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 pFile->pathToDe
be240 6c 20 29 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 l );. pFile
be250 2d 3e 70 61 74 68 54 6f 44 65 6c 20 3d 20 4e 55 ->pathToDel = NU
be260 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 64 LL;. }. id
be270 20 3d 20 30 3b 0a 20 20 20 20 4f 70 65 6e 43 6f = 0;. OpenCo
be280 75 6e 74 65 72 28 20 2d 31 20 29 3b 0a 20 20 7d unter( -1 );. }
be290 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 20 3d 3d .. return rc ==
be2a0 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 NO_ERROR ? SQLI
be2b0 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 TE_OK : SQLITE_I
be2c0 4f 45 52 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 OERR;.}../*.** R
be2d0 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 61 20 ead data from a
be2e0 66 69 6c 65 20 69 6e 74 6f 20 61 20 62 75 66 66 file into a buff
be2f0 65 72 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 er. Return SQLI
be300 54 45 5f 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20 TE_OK if all.**
be310 62 79 74 65 73 20 77 65 72 65 20 72 65 61 64 20 bytes were read
be320 73 75 63 63 65 73 73 66 75 6c 6c 79 20 61 6e 64 successfully and
be330 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 69 66 SQLITE_IOERR if
be340 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a anything goes.*
be350 2a 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 * wrong..*/.stat
be360 69 63 20 69 6e 74 20 6f 73 32 52 65 61 64 28 0a ic int os2Read(.
be370 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a sqlite3_file *
be380 69 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 id,
be390 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 72 65 61 /* File to rea
be3a0 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 76 6f 69 64 d from */. void
be3b0 20 2a 70 42 75 66 2c 20 20 20 20 20 20 20 20 20 *pBuf,
be3c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 /* W
be3d0 72 69 74 65 20 63 6f 6e 74 65 6e 74 20 69 6e 74 rite content int
be3e0 6f 20 74 68 69 73 20 62 75 66 66 65 72 20 2a 2f o this buffer */
be3f0 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 . int amt,
be400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
be410 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
be420 62 79 74 65 73 20 74 6f 20 72 65 61 64 20 2a 2f bytes to read */
be430 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 . sqlite3_int64
be440 20 6f 66 66 73 65 74 20 20 20 20 20 20 20 20 20 offset
be450 20 20 20 2f 2a 20 42 65 67 69 6e 20 72 65 61 64 /* Begin read
be460 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66 66 73 ing at this offs
be470 65 74 20 2a 2f 0a 29 7b 0a 20 20 55 4c 4f 4e 47 et */.){. ULONG
be480 20 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 20 3d 20 fileLocation =
be490 30 4c 3b 0a 20 20 55 4c 4f 4e 47 20 67 6f 74 3b 0L;. ULONG got;
be4a0 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c . os2File *pFil
be4b0 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 e = (os2File*)id
be4c0 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d ;. assert( id!=
be4d0 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 0 );. SimulateI
be4e0 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 OError( return S
be4f0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 QLITE_IOERR_READ
be500 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20 );. OSTRACE3(
be510 22 52 45 41 44 20 25 64 20 6c 6f 63 6b 3d 25 64 "READ %d lock=%d
be520 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 \n", pFile->h, p
be530 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 File->locktype )
be540 3b 0a 20 20 69 66 28 20 44 6f 73 53 65 74 46 69 ;. if( DosSetFi
be550 6c 65 50 74 72 28 70 46 69 6c 65 2d 3e 68 2c 20 lePtr(pFile->h,
be560 6f 66 66 73 65 74 2c 20 46 49 4c 45 5f 42 45 47 offset, FILE_BEG
be570 49 4e 2c 20 26 66 69 6c 65 4c 6f 63 61 74 69 6f IN, &fileLocatio
be580 6e 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 n) != NO_ERROR )
be590 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
be5a0 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 20 ITE_IOERR;. }.
be5b0 20 69 66 28 20 44 6f 73 52 65 61 64 28 20 70 46 if( DosRead( pF
be5c0 69 6c 65 2d 3e 68 2c 20 70 42 75 66 2c 20 61 6d ile->h, pBuf, am
be5d0 74 2c 20 26 67 6f 74 20 29 20 21 3d 20 4e 4f 5f t, &got ) != NO_
be5e0 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 ERROR ){. ret
be5f0 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
be600 5f 52 45 41 44 3b 0a 20 20 7d 0a 20 20 69 66 28 _READ;. }. if(
be610 20 67 6f 74 20 3d 3d 20 28 55 4c 4f 4e 47 29 61 got == (ULONG)a
be620 6d 74 20 29 0a 20 20 20 20 72 65 74 75 72 6e 20 mt ). return
be630 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 65 6c 73 SQLITE_OK;. els
be640 65 20 7b 0a 20 20 20 20 2f 2a 20 55 6e 72 65 61 e {. /* Unrea
be650 64 20 70 6f 72 74 69 6f 6e 73 20 6f 66 20 74 68 d portions of th
be660 65 20 69 6e 70 75 74 20 62 75 66 66 65 72 20 6d e input buffer m
be670 75 73 74 20 62 65 20 7a 65 72 6f 2d 66 69 6c 6c ust be zero-fill
be680 65 64 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74 ed */. memset
be690 28 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b (&((char*)pBuf)[
be6a0 67 6f 74 5d 2c 20 30 2c 20 61 6d 74 2d 67 6f 74 got], 0, amt-got
be6b0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 );. return SQ
be6c0 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 LITE_IOERR_SHORT
be6d0 5f 52 45 41 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a _READ;. }.}../*
be6e0 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20 66 .** Write data f
be6f0 72 6f 6d 20 61 20 62 75 66 66 65 72 20 69 6e 74 rom a buffer int
be700 6f 20 61 20 66 69 6c 65 2e 20 20 52 65 74 75 72 o a file. Retur
be710 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 n SQLITE_OK on s
be720 75 63 63 65 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d uccess.** or som
be730 65 20 6f 74 68 65 72 20 65 72 72 6f 72 20 63 6f e other error co
be740 64 65 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a de on failure..*
be750 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 /.static int os2
be760 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74 65 33 Write(. sqlite3
be770 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 _file *id,
be780 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 /* File
be790 20 74 6f 20 77 72 69 74 65 20 69 6e 74 6f 20 2a to write into *
be7a0 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a /. const void *
be7b0 70 42 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 pBuf,
be7c0 20 20 20 20 2f 2a 20 54 68 65 20 62 79 74 65 73 /* The bytes
be7d0 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 2a to be written *
be7e0 2f 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 /. int amt,
be7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
be800 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
be810 20 62 79 74 65 73 20 74 6f 20 77 72 69 74 65 20 bytes to write
be820 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 */. sqlite3_int
be830 36 34 20 6f 66 66 73 65 74 20 20 20 20 20 20 20 64 offset
be840 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 /* Offset i
be850 6e 74 6f 20 74 68 65 20 66 69 6c 65 20 74 6f 20 nto the file to
be860 62 65 67 69 6e 20 77 72 69 74 69 6e 67 20 61 74 begin writing at
be870 20 2a 2f 0a 29 7b 0a 20 20 55 4c 4f 4e 47 20 66 */.){. ULONG f
be880 69 6c 65 4c 6f 63 61 74 69 6f 6e 20 3d 20 30 4c ileLocation = 0L
be890 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 ;. APIRET rc =
be8a0 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 55 4c 4f 4e NO_ERROR;. ULON
be8b0 47 20 77 72 6f 74 65 3b 0a 20 20 6f 73 32 46 69 G wrote;. os2Fi
be8c0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 le *pFile = (os2
be8d0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 73 73 65 File*)id;. asse
be8e0 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 53 rt( id!=0 );. S
be8f0 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 imulateIOError(
be900 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
be910 45 52 52 5f 57 52 49 54 45 20 29 3b 0a 20 20 53 ERR_WRITE );. S
be920 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 imulateDiskfullE
be930 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c rror( return SQL
be940 49 54 45 5f 46 55 4c 4c 20 29 3b 0a 20 20 4f 53 ITE_FULL );. OS
be950 54 52 41 43 45 33 28 20 22 57 52 49 54 45 20 25 TRACE3( "WRITE %
be960 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 d lock=%d\n", pF
be970 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c ile->h, pFile->l
be980 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 69 66 28 ocktype );. if(
be990 20 44 6f 73 53 65 74 46 69 6c 65 50 74 72 28 70 DosSetFilePtr(p
be9a0 46 69 6c 65 2d 3e 68 2c 20 6f 66 66 73 65 74 2c File->h, offset,
be9b0 20 46 49 4c 45 5f 42 45 47 49 4e 2c 20 26 66 69 FILE_BEGIN, &fi
be9c0 6c 65 4c 6f 63 61 74 69 6f 6e 29 20 21 3d 20 4e leLocation) != N
be9d0 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 72 O_ERROR ){. r
be9e0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
be9f0 52 52 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 RR;. }. assert
bea00 28 20 61 6d 74 3e 30 20 29 3b 0a 20 20 77 68 69 ( amt>0 );. whi
bea10 6c 65 28 20 61 6d 74 20 3e 20 30 20 26 26 0a 20 le( amt > 0 &&.
bea20 20 20 20 20 20 20 20 20 28 20 72 63 20 3d 20 44 ( rc = D
bea30 6f 73 57 72 69 74 65 28 20 70 46 69 6c 65 2d 3e osWrite( pFile->
bea40 68 2c 20 28 50 56 4f 49 44 29 70 42 75 66 2c 20 h, (PVOID)pBuf,
bea50 61 6d 74 2c 20 26 77 72 6f 74 65 20 29 20 29 20 amt, &wrote ) )
bea60 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 26 26 0a 20 == NO_ERROR &&.
bea70 20 20 20 20 20 20 20 20 77 72 6f 74 65 20 3e 20 wrote >
bea80 30 0a 20 20 29 7b 0a 20 20 20 20 61 6d 74 20 2d 0. ){. amt -
bea90 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20 70 42 75 = wrote;. pBu
beaa0 66 20 3d 20 26 28 28 63 68 61 72 2a 29 70 42 75 f = &((char*)pBu
beab0 66 29 5b 77 72 6f 74 65 5d 3b 0a 20 20 7d 0a 0a f)[wrote];. }..
beac0 20 20 72 65 74 75 72 6e 20 28 20 72 63 20 21 3d return ( rc !=
bead0 20 4e 4f 5f 45 52 52 4f 52 20 7c 7c 20 61 6d 74 NO_ERROR || amt
beae0 20 3e 20 28 69 6e 74 29 77 72 6f 74 65 20 29 20 > (int)wrote )
beaf0 3f 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 3a 20 ? SQLITE_FULL :
beb00 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
beb10 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 61 6e 20 .** Truncate an
beb20 6f 70 65 6e 20 66 69 6c 65 20 74 6f 20 61 20 73 open file to a s
beb30 70 65 63 69 66 69 65 64 20 73 69 7a 65 0a 2a 2f pecified size.*/
beb40 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 54 .static int os2T
beb50 72 75 6e 63 61 74 65 28 20 73 71 6c 69 74 65 33 runcate( sqlite3
beb60 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 34 20 6e _file *id, i64 n
beb70 42 79 74 65 20 29 7b 0a 20 20 41 50 49 52 45 54 Byte ){. APIRET
beb80 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a rc = NO_ERROR;.
beb90 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 os2File *pFile
beba0 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b = (os2File*)id;
bebb0 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 52 . OSTRACE3( "TR
bebc0 55 4e 43 41 54 45 20 25 64 20 25 6c 6c 64 5c 6e UNCATE %d %lld\n
bebd0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6e 42 79 ", pFile->h, nBy
bebe0 74 65 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 te );. Simulate
bebf0 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 IOError( return
bec00 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 SQLITE_IOERR_TRU
bec10 4e 43 41 54 45 20 29 3b 0a 20 20 72 63 20 3d 20 NCATE );. rc =
bec20 44 6f 73 53 65 74 46 69 6c 65 53 69 7a 65 28 20 DosSetFileSize(
bec30 70 46 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 20 pFile->h, nByte
bec40 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 20 3d );. return rc =
bec50 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c = NO_ERROR ? SQL
bec60 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f ITE_OK : SQLITE_
bec70 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 3b 0a IOERR_TRUNCATE;.
bec80 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 }..#ifdef SQLITE
bec90 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e _TEST./*.** Coun
beca0 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 t the number of
becb0 66 75 6c 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f fullsyncs and no
becc0 72 6d 61 6c 20 73 79 6e 63 73 2e 20 20 54 68 69 rmal syncs. Thi
becd0 73 20 69 73 20 75 73 65 64 20 74 6f 20 74 65 73 s is used to tes
bece0 74 0a 2a 2a 20 74 68 61 74 20 73 79 6e 63 73 20 t.** that syncs
becf0 61 6e 64 20 66 75 6c 6c 73 79 6e 63 73 20 61 72 and fullsyncs ar
bed00 65 20 6f 63 63 75 72 69 6e 67 20 61 74 20 74 68 e occuring at th
bed10 65 20 72 69 67 68 74 20 74 69 6d 65 73 2e 0a 2a e right times..*
bed20 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
bed30 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f sqlite3_sync_co
bed40 75 6e 74 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f unt = 0;.SQLITE_
bed50 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
bed60 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 20 3d fullsync_count =
bed70 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 0;.#endif../*.*
bed80 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 * Make sure all
bed90 77 72 69 74 65 73 20 74 6f 20 61 20 70 61 72 74 writes to a part
beda0 69 63 75 6c 61 72 20 66 69 6c 65 20 61 72 65 20 icular file are
bedb0 63 6f 6d 6d 69 74 74 65 64 20 74 6f 20 64 69 73 committed to dis
bedc0 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 k..*/.static int
bedd0 20 6f 73 32 53 79 6e 63 28 20 73 71 6c 69 74 65 os2Sync( sqlite
bede0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
bedf0 66 6c 61 67 73 20 29 7b 0a 20 20 6f 73 32 46 69 flags ){. os2Fi
bee00 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 le *pFile = (os2
bee10 46 69 6c 65 2a 29 69 64 3b 0a 20 20 4f 53 54 52 File*)id;. OSTR
bee20 41 43 45 33 28 20 22 53 59 4e 43 20 25 64 20 6c ACE3( "SYNC %d l
bee30 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 ock=%d\n", pFile
bee40 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b ->h, pFile->lock
bee50 74 79 70 65 20 29 3b 0a 23 69 66 64 65 66 20 53 type );.#ifdef S
bee60 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28 QLITE_TEST. if(
bee70 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f flags & SQLITE_
bee80 53 59 4e 43 5f 46 55 4c 4c 29 7b 0a 20 20 20 20 SYNC_FULL){.
bee90 73 71 6c 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 sqlite3_fullsync
beea0 5f 63 6f 75 6e 74 2b 2b 3b 0a 20 20 7d 0a 20 20 _count++;. }.
beeb0 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 sqlite3_sync_cou
beec0 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 2f nt++;.#endif. /
beed0 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c 65 64 * If we compiled
beee0 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 with the SQLITE
beef0 5f 4e 4f 5f 53 59 4e 43 20 66 6c 61 67 2c 20 74 _NO_SYNC flag, t
bef00 68 65 6e 20 73 79 6e 63 69 6e 67 20 69 73 20 61 hen syncing is a
bef10 0a 20 20 2a 2a 20 6e 6f 2d 6f 70 0a 20 20 2a 2f . ** no-op. */
bef20 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4e .#ifdef SQLITE_N
bef30 4f 5f 53 59 4e 43 0a 20 20 55 4e 55 53 45 44 5f O_SYNC. UNUSED_
bef40 50 41 52 41 4d 45 54 45 52 28 70 46 69 6c 65 29 PARAMETER(pFile)
bef50 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
bef60 45 5f 4f 4b 3b 0a 23 65 6c 73 65 0a 20 20 72 65 E_OK;.#else. re
bef70 74 75 72 6e 20 44 6f 73 52 65 73 65 74 42 75 66 turn DosResetBuf
bef80 66 65 72 28 20 70 46 69 6c 65 2d 3e 68 20 29 20 fer( pFile->h )
bef90 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 51 == NO_ERROR ? SQ
befa0 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 LITE_OK : SQLITE
befb0 5f 49 4f 45 52 52 3b 0a 23 65 6e 64 69 66 0a 7d _IOERR;.#endif.}
befc0 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e ../*.** Determin
befd0 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 69 e the current si
befe0 7a 65 20 6f 66 20 61 20 66 69 6c 65 20 69 6e 20 ze of a file in
beff0 62 79 74 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20 bytes.*/.static
bf000 69 6e 74 20 6f 73 32 46 69 6c 65 53 69 7a 65 28 int os2FileSize(
bf010 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 sqlite3_file *i
bf020 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 d, sqlite3_int64
bf030 20 2a 70 53 69 7a 65 20 29 7b 0a 20 20 41 50 49 *pSize ){. API
bf040 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f RET rc = NO_ERRO
bf050 52 3b 0a 20 20 46 49 4c 45 53 54 41 54 55 53 33 R;. FILESTATUS3
bf060 20 66 73 74 73 33 46 69 6c 65 49 6e 66 6f 3b 0a fsts3FileInfo;.
bf070 20 20 6d 65 6d 73 65 74 28 26 66 73 74 73 33 46 memset(&fsts3F
bf080 69 6c 65 49 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 ileInfo, 0, size
bf090 6f 66 28 66 73 74 73 33 46 69 6c 65 49 6e 66 6f of(fsts3FileInfo
bf0a0 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 ));. assert( id
bf0b0 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 !=0 );. Simulat
bf0c0 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e eIOError( return
bf0d0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 SQLITE_IOERR_FS
bf0e0 54 41 54 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f TAT );. rc = Do
bf0f0 73 51 75 65 72 79 46 69 6c 65 49 6e 66 6f 28 20 sQueryFileInfo(
bf100 28 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e ((os2File*)id)->
bf110 68 2c 20 46 49 4c 5f 53 54 41 4e 44 41 52 44 2c h, FIL_STANDARD,
bf120 20 26 66 73 74 73 33 46 69 6c 65 49 6e 66 6f 2c &fsts3FileInfo,
bf130 20 73 69 7a 65 6f 66 28 46 49 4c 45 53 54 41 54 sizeof(FILESTAT
bf140 55 53 33 29 20 29 3b 0a 20 20 69 66 28 20 72 63 US3) );. if( rc
bf150 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a == NO_ERROR ){.
bf160 20 20 20 20 2a 70 53 69 7a 65 20 3d 20 66 73 74 *pSize = fst
bf170 73 33 46 69 6c 65 49 6e 66 6f 2e 63 62 46 69 6c s3FileInfo.cbFil
bf180 65 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 e;. return SQ
bf190 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 LITE_OK;. }else
bf1a0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
bf1b0 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b ITE_IOERR_FSTAT;
bf1c0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 . }.}../*.** Ac
bf1d0 71 75 69 72 65 20 61 20 72 65 61 64 65 72 20 6c quire a reader l
bf1e0 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ock..*/.static i
bf1f0 6e 74 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 20 nt getReadLock(
bf200 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 29 os2File *pFile )
bf210 7b 0a 20 20 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f {. FILELOCK Lo
bf220 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 20 20 20 ckArea,.
bf230 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a UnlockArea;.
bf240 20 20 41 50 49 52 45 54 20 72 65 73 3b 0a 20 20 APIRET res;.
bf250 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 61 memset(&LockArea
bf260 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b , 0, sizeof(Lock
bf270 41 72 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65 74 Area));. memset
bf280 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c (&UnlockArea, 0,
bf290 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 sizeof(UnlockAr
bf2a0 65 61 29 29 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 ea));. LockArea
bf2b0 2e 6c 4f 66 66 73 65 74 20 3d 20 53 48 41 52 45 .lOffset = SHARE
bf2c0 44 5f 46 49 52 53 54 3b 0a 20 20 4c 6f 63 6b 41 D_FIRST;. LockA
bf2d0 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48 41 rea.lRange = SHA
bf2e0 52 45 44 5f 53 49 5a 45 3b 0a 20 20 55 6e 6c 6f RED_SIZE;. Unlo
bf2f0 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d ckArea.lOffset =
bf300 20 30 4c 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 0L;. UnlockAre
bf310 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 a.lRange = 0L;.
bf320 20 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c res = DosSetFil
bf330 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 eLocks( pFile->h
bf340 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 , &UnlockArea, &
bf350 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 LockArea, LOCK_T
bf360 49 4d 45 4f 55 54 2c 20 31 4c 20 29 3b 0a 20 20 IMEOUT, 1L );.
bf370 4f 53 54 52 41 43 45 33 28 20 22 47 45 54 52 45 OSTRACE3( "GETRE
bf380 41 44 4c 4f 43 4b 20 25 64 20 72 65 73 3d 25 64 ADLOCK %d res=%d
bf390 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 \n", pFile->h, r
bf3a0 65 73 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 es );. return r
bf3b0 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 es;.}../*.** Und
bf3c0 6f 20 61 20 72 65 61 64 6c 6f 63 6b 0a 2a 2f 0a o a readlock.*/.
bf3d0 73 74 61 74 69 63 20 69 6e 74 20 75 6e 6c 6f 63 static int unloc
bf3e0 6b 52 65 61 64 4c 6f 63 6b 28 20 6f 73 32 46 69 kReadLock( os2Fi
bf3f0 6c 65 20 2a 69 64 20 29 7b 0a 20 20 46 49 4c 45 le *id ){. FILE
bf400 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c 0a LOCK LockArea,.
bf410 20 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c 6f Unlo
bf420 63 6b 41 72 65 61 3b 0a 20 20 41 50 49 52 45 54 ckArea;. APIRET
bf430 20 72 65 73 3b 0a 20 20 6d 65 6d 73 65 74 28 26 res;. memset(&
bf440 4c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a LockArea, 0, siz
bf450 65 6f 66 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a eof(LockArea));.
bf460 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b memset(&Unlock
bf470 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 Area, 0, sizeof(
bf480 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 UnlockArea));.
bf490 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 LockArea.lOffset
bf4a0 20 3d 20 30 4c 3b 0a 20 20 4c 6f 63 6b 41 72 65 = 0L;. LockAre
bf4b0 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 a.lRange = 0L;.
bf4c0 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 UnlockArea.lOff
bf4d0 73 65 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 set = SHARED_FIR
bf4e0 53 54 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61 ST;. UnlockArea
bf4f0 2e 6c 52 61 6e 67 65 20 3d 20 53 48 41 52 45 44 .lRange = SHARED
bf500 5f 53 49 5a 45 3b 0a 20 20 72 65 73 20 3d 20 44 _SIZE;. res = D
bf510 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 osSetFileLocks(
bf520 69 64 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 id->h, &UnlockAr
bf530 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c ea, &LockArea, L
bf540 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 31 4c 20 OCK_TIMEOUT, 1L
bf550 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22 );. OSTRACE3( "
bf560 55 4e 4c 4f 43 4b 2d 52 45 41 44 4c 4f 43 4b 20 UNLOCK-READLOCK
bf570 66 69 6c 65 20 68 61 6e 64 6c 65 3d 25 64 20 72 file handle=%d r
bf580 65 73 3d 25 64 3f 5c 6e 22 2c 20 69 64 2d 3e 68 es=%d?\n", id->h
bf590 2c 20 72 65 73 20 29 3b 0a 20 20 72 65 74 75 72 , res );. retur
bf5a0 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 n res;.}../*.**
bf5b0 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69 Lock the file wi
bf5c0 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 th the lock spec
bf5d0 69 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 ified by paramet
bf5e0 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e er locktype - on
bf5f0 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c e.** of the foll
bf600 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 owing:.**.**
bf610 20 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b (1) SHARED_LOCK
bf620 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53 45 .** (2) RESE
bf630 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 RVED_LOCK.**
bf640 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 (3) PENDING_LOC
bf650 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43 K.** (4) EXC
bf660 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a LUSIVE_LOCK.**.*
bf670 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e * Sometimes when
bf680 20 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 requesting one
bf690 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69 lock state, addi
bf6a0 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 tional lock stat
bf6b0 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 es.** are insert
bf6c0 65 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 ed in between.
bf6d0 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 The locking migh
bf6e0 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 t fail on one of
bf6f0 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 the later.** tr
bf700 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e ansitions leavin
bf710 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 g the lock state
bf720 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 different from
bf730 77 68 61 74 20 69 74 20 73 74 61 72 74 65 64 20 what it started
bf740 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f but.** still sho
bf750 72 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 rt of its goal.
bf760 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 The following c
bf770 68 61 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 hart shows the a
bf780 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 llowed.** transi
bf790 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e tions and the in
bf7a0 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69 serted intermedi
bf7b0 61 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a ate states:.**.*
bf7c0 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e * UNLOCKED ->
bf7d0 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 SHARED.** SH
bf7e0 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44 ARED -> RESERVED
bf7f0 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e .** SHARED ->
bf800 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 (PENDING) -> EX
bf810 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 CLUSIVE.** RE
bf820 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 SERVED -> (PENDI
bf830 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 NG) -> EXCLUSIVE
bf840 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d .** PENDING -
bf850 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a > EXCLUSIVE.**.*
bf860 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 * This routine w
bf870 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 ill only increas
bf880 65 20 61 20 6c 6f 63 6b 2e 20 20 54 68 65 20 6f e a lock. The o
bf890 73 32 55 6e 6c 6f 63 6b 28 29 20 72 6f 75 74 69 s2Unlock() routi
bf8a0 6e 65 0a 2a 2a 20 65 72 61 73 65 73 20 61 6c 6c ne.** erases all
bf8b0 20 6c 6f 63 6b 73 20 61 74 20 6f 6e 63 65 20 61 locks at once a
bf8c0 6e 64 20 72 65 74 75 72 6e 73 20 75 73 20 69 6d nd returns us im
bf8d0 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 6c 6f 63 mediately to loc
bf8e0 6b 69 6e 67 20 6c 65 76 65 6c 20 30 2e 0a 2a 2a king level 0..**
bf8f0 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 It is not possi
bf900 62 6c 65 20 74 6f 20 6c 6f 77 65 72 20 74 68 65 ble to lower the
bf910 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f locking level o
bf920 6e 65 20 73 74 65 70 20 61 74 20 61 20 74 69 6d ne step at a tim
bf930 65 2e 20 20 59 6f 75 0a 2a 2a 20 6d 75 73 74 20 e. You.** must
bf940 67 6f 20 73 74 72 61 69 67 68 74 20 74 6f 20 6c go straight to l
bf950 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 30 2e 0a ocking level 0..
bf960 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 */.static int os
bf970 32 4c 6f 63 6b 28 20 73 71 6c 69 74 65 33 5f 66 2Lock( sqlite3_f
bf980 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 ile *id, int loc
bf990 6b 74 79 70 65 20 29 7b 0a 20 20 69 6e 74 20 72 ktype ){. int r
bf9a0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 c = SQLITE_OK;
bf9b0 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 /* Return c
bf9c0 6f 64 65 20 66 72 6f 6d 20 73 75 62 72 6f 75 74 ode from subrout
bf9d0 69 6e 65 73 20 2a 2f 0a 20 20 41 50 49 52 45 54 ines */. APIRET
bf9e0 20 72 65 73 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b res = NO_ERROR;
bf9f0 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 /* Result of
bfa00 20 61 6e 20 4f 53 2f 32 20 6c 6f 63 6b 20 63 61 an OS/2 lock ca
bfa10 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 4c ll */. int newL
bfa20 6f 63 6b 74 79 70 65 3b 20 20 20 20 20 20 20 2f ocktype; /
bfa30 2a 20 53 65 74 20 70 46 69 6c 65 2d 3e 6c 6f 63 * Set pFile->loc
bfa40 6b 74 79 70 65 20 74 6f 20 74 68 69 73 20 76 61 ktype to this va
bfa50 6c 75 65 20 62 65 66 6f 72 65 20 65 78 69 74 69 lue before exiti
bfa60 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 67 6f 74 50 ng */. int gotP
bfa70 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 2f endingLock = 0;/
bfa80 2a 20 54 72 75 65 20 69 66 20 77 65 20 61 63 71 * True if we acq
bfa90 75 69 72 65 64 20 61 20 50 45 4e 44 49 4e 47 20 uired a PENDING
bfaa0 6c 6f 63 6b 20 74 68 69 73 20 74 69 6d 65 20 2a lock this time *
bfab0 2f 0a 20 20 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f /. FILELOCK Lo
bfac0 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 20 20 20 ckArea,.
bfad0 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a UnlockArea;.
bfae0 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 os2File *pFile
bfaf0 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b = (os2File*)id;
bfb00 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 . memset(&LockA
bfb10 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c rea, 0, sizeof(L
bfb20 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d 65 6d ockArea));. mem
bfb30 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c set(&UnlockArea,
bfb40 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 0, sizeof(Unloc
bfb50 6b 41 72 65 61 29 29 3b 0a 20 20 61 73 73 65 72 kArea));. asser
bfb60 74 28 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 t( pFile!=0 );.
bfb70 20 4f 53 54 52 41 43 45 34 28 20 22 4c 4f 43 4b OSTRACE4( "LOCK
bfb80 20 25 64 20 25 64 20 77 61 73 20 25 64 5c 6e 22 %d %d was %d\n"
bfb90 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b , pFile->h, lock
bfba0 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 type, pFile->loc
bfbb0 6b 74 79 70 65 20 29 3b 0a 0a 20 20 2f 2a 20 49 ktype );.. /* I
bfbc0 66 20 74 68 65 72 65 20 69 73 20 61 6c 72 65 61 f there is alrea
bfbd0 64 79 20 61 20 6c 6f 63 6b 20 6f 66 20 74 68 69 dy a lock of thi
bfbe0 73 20 74 79 70 65 20 6f 72 20 6d 6f 72 65 20 72 s type or more r
bfbf0 65 73 74 72 69 63 74 69 76 65 20 6f 6e 20 74 68 estrictive on th
bfc00 65 0a 20 20 2a 2a 20 6f 73 32 46 69 6c 65 2c 20 e. ** os2File,
bfc10 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 do nothing. Don'
bfc20 74 20 75 73 65 20 74 68 65 20 65 6e 64 5f 6c 6f t use the end_lo
bfc30 63 6b 3a 20 65 78 69 74 20 70 61 74 68 2c 20 61 ck: exit path, a
bfc40 73 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 6d s. ** sqlite3_m
bfc50 75 74 65 78 5f 65 6e 74 65 72 28 29 20 68 61 73 utex_enter() has
bfc60 6e 27 74 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 n't been called
bfc70 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 yet.. */. if(
bfc80 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e pFile->locktype>
bfc90 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 =locktype ){.
bfca0 20 4f 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b OSTRACE3( "LOCK
bfcb0 20 25 64 20 25 64 20 6f 6b 20 28 61 6c 72 65 61 %d %d ok (alrea
bfcc0 64 79 20 68 65 6c 64 29 5c 6e 22 2c 20 70 46 69 dy held)\n", pFi
bfcd0 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 20 le->h, locktype
bfce0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 );. return SQ
bfcf0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 LITE_OK;. }..
bfd00 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 /* Make sure the
bfd10 20 6c 6f 63 6b 69 6e 67 20 73 65 71 75 65 6e 63 locking sequenc
bfd20 65 20 69 73 20 63 6f 72 72 65 63 74 0a 20 20 2a e is correct. *
bfd30 2f 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c /. assert( pFil
bfd40 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f e->locktype!=NO_
bfd50 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 LOCK || locktype
bfd60 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b ==SHARED_LOCK );
bfd70 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 . assert( lockt
bfd80 79 70 65 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 ype!=PENDING_LOC
bfd90 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c K );. assert( l
bfda0 6f 63 6b 74 79 70 65 21 3d 52 45 53 45 52 56 45 ocktype!=RESERVE
bfdb0 44 5f 4c 4f 43 4b 20 7c 7c 20 70 46 69 6c 65 2d D_LOCK || pFile-
bfdc0 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 >locktype==SHARE
bfdd0 44 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 D_LOCK );.. /*
bfde0 4c 6f 63 6b 20 74 68 65 20 50 45 4e 44 49 4e 47 Lock the PENDING
bfdf0 5f 4c 4f 43 4b 20 62 79 74 65 20 69 66 20 77 65 _LOCK byte if we
bfe00 20 6e 65 65 64 20 74 6f 20 61 63 71 75 69 72 65 need to acquire
bfe10 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 a PENDING lock
bfe20 6f 72 0a 20 20 2a 2a 20 61 20 53 48 41 52 45 44 or. ** a SHARED
bfe30 20 6c 6f 63 6b 2e 20 20 49 66 20 77 65 20 61 72 lock. If we ar
bfe40 65 20 61 63 71 75 69 72 69 6e 67 20 61 20 53 48 e acquiring a SH
bfe50 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20 61 ARED lock, the a
bfe60 63 71 75 69 73 69 74 69 6f 6e 20 6f 66 0a 20 20 cquisition of.
bfe70 2a 2a 20 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c ** the PENDING_L
bfe80 4f 43 4b 20 62 79 74 65 20 69 73 20 74 65 6d 70 OCK byte is temp
bfe90 6f 72 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 6e 65 orary.. */. ne
bfea0 77 4c 6f 63 6b 74 79 70 65 20 3d 20 70 46 69 6c wLocktype = pFil
bfeb0 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 69 e->locktype;. i
bfec0 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 f( pFile->lockty
bfed0 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 0a 20 20 20 20 pe==NO_LOCK.
bfee0 20 20 7c 7c 20 28 6c 6f 63 6b 74 79 70 65 3d 3d || (locktype==
bfef0 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 EXCLUSIVE_LOCK &
bff00 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 & pFile->locktyp
bff10 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b e==RESERVED_LOCK
bff20 29 0a 20 20 29 7b 0a 20 20 20 20 4c 6f 63 6b 41 ). ){. LockA
bff30 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 rea.lOffset = PE
bff40 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 NDING_BYTE;.
bff50 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 LockArea.lRange
bff60 3d 20 31 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b = 1L;. Unlock
bff70 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 Area.lOffset = 0
bff80 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 L;. UnlockAre
bff90 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 0a a.lRange = 0L;..
bffa0 20 20 20 20 2f 2a 20 77 61 69 74 20 6c 6f 6e 67 /* wait long
bffb0 65 72 20 74 68 61 6e 20 4c 4f 43 4b 5f 54 49 4d er than LOCK_TIM
bffc0 45 4f 55 54 20 68 65 72 65 20 6e 6f 74 20 74 6f EOUT here not to
bffd0 20 68 61 76 65 20 74 6f 20 74 72 79 20 6d 75 6c have to try mul
bffe0 74 69 70 6c 65 20 74 69 6d 65 73 20 2a 2f 0a 20 tiple times */.
bfff0 20 20 20 72 65 73 20 3d 20 44 6f 73 53 65 74 46 res = DosSetF
c0000 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d ileLocks( pFile-
c0010 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c >h, &UnlockArea,
c0020 20 26 4c 6f 63 6b 41 72 65 61 2c 20 31 30 30 4c &LockArea, 100L
c0030 2c 20 30 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 , 0L );. if(
c0040 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 res == NO_ERROR
c0050 29 7b 0a 20 20 20 20 20 20 67 6f 74 50 65 6e 64 ){. gotPend
c0060 69 6e 67 4c 6f 63 6b 20 3d 20 31 3b 0a 20 20 20 ingLock = 1;.
c0070 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 4c 4f OSTRACE3( "LO
c0080 43 4b 20 25 64 20 70 65 6e 64 69 6e 67 20 6c 6f CK %d pending lo
c0090 63 6b 20 62 6f 6f 6c 65 61 6e 20 73 65 74 2e 20 ck boolean set.
c00a0 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c res=%d\n", pFil
c00b0 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 20 e->h, res );.
c00c0 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 }. }.. /* Acq
c00d0 75 69 72 65 20 61 20 73 68 61 72 65 64 20 6c 6f uire a shared lo
c00e0 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f ck. */. if( lo
c00f0 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c cktype==SHARED_L
c0100 4f 43 4b 20 26 26 20 72 65 73 20 3d 3d 20 4e 4f OCK && res == NO
c0110 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 61 73 _ERROR ){. as
c0120 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 sert( pFile->loc
c0130 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 ktype==NO_LOCK )
c0140 3b 0a 20 20 20 20 72 65 73 20 3d 20 67 65 74 52 ;. res = getR
c0150 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a eadLock(pFile);.
c0160 20 20 20 20 69 66 28 20 72 65 73 20 3d 3d 20 4e if( res == N
c0170 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 O_ERROR ){.
c0180 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 53 newLocktype = S
c0190 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 HARED_LOCK;.
c01a0 7d 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 }. OSTRACE3(
c01b0 22 4c 4f 43 4b 20 25 64 20 61 63 71 75 69 72 65 "LOCK %d acquire
c01c0 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e 20 72 65 shared lock. re
c01d0 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e s=%d\n", pFile->
c01e0 68 2c 20 72 65 73 20 29 3b 0a 20 20 7d 0a 0a 20 h, res );. }..
c01f0 20 2f 2a 20 41 63 71 75 69 72 65 20 61 20 52 45 /* Acquire a RE
c0200 53 45 52 56 45 44 20 6c 6f 63 6b 0a 20 20 2a 2f SERVED lock. */
c0210 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d . if( locktype=
c0220 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 26 =RESERVED_LOCK &
c0230 26 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f & res == NO_ERRO
c0240 52 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 R ){. assert(
c0250 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
c0260 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b ==SHARED_LOCK );
c0270 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f . LockArea.lO
c0280 66 66 73 65 74 20 3d 20 52 45 53 45 52 56 45 44 ffset = RESERVED
c0290 5f 42 59 54 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 _BYTE;. LockA
c02a0 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b rea.lRange = 1L;
c02b0 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e . UnlockArea.
c02c0 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 lOffset = 0L;.
c02d0 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 UnlockArea.lRa
c02e0 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20 72 65 nge = 0L;. re
c02f0 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f s = DosSetFileLo
c0300 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 cks( pFile->h, &
c0310 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 UnlockArea, &Loc
c0320 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 kArea, LOCK_TIME
c0330 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 69 OUT, 0L );. i
c0340 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 f( res == NO_ERR
c0350 4f 52 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c OR ){. newL
c0360 6f 63 6b 74 79 70 65 20 3d 20 52 45 53 45 52 56 ocktype = RESERV
c0370 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 ED_LOCK;. }.
c0380 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 4c 4f OSTRACE3( "LO
c0390 43 4b 20 25 64 20 61 63 71 75 69 72 65 20 72 65 CK %d acquire re
c03a0 73 65 72 76 65 64 20 6c 6f 63 6b 2e 20 72 65 73 served lock. res
c03b0 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 =%d\n", pFile->h
c03c0 2c 20 72 65 73 20 29 3b 0a 20 20 7d 0a 0a 20 20 , res );. }..
c03d0 2f 2a 20 41 63 71 75 69 72 65 20 61 20 50 45 4e /* Acquire a PEN
c03e0 44 49 4e 47 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 DING lock. */.
c03f0 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 if( locktype==E
c0400 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 XCLUSIVE_LOCK &&
c0410 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 res == NO_ERROR
c0420 20 29 7b 0a 20 20 20 20 6e 65 77 4c 6f 63 6b 74 ){. newLockt
c0430 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f ype = PENDING_LO
c0440 43 4b 3b 0a 20 20 20 20 67 6f 74 50 65 6e 64 69 CK;. gotPendi
c0450 6e 67 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20 ngLock = 0;.
c0460 4f 53 54 52 41 43 45 32 28 20 22 4c 4f 43 4b 20 OSTRACE2( "LOCK
c0470 25 64 20 61 63 71 75 69 72 65 20 70 65 6e 64 69 %d acquire pendi
c0480 6e 67 20 6c 6f 63 6b 2e 20 70 65 6e 64 69 6e 67 ng lock. pending
c0490 20 6c 6f 63 6b 20 62 6f 6f 6c 65 61 6e 20 75 6e lock boolean un
c04a0 73 65 74 2e 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e set.\n", pFile->
c04b0 68 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 h );. }.. /* A
c04c0 63 71 75 69 72 65 20 61 6e 20 45 58 43 4c 55 53 cquire an EXCLUS
c04d0 49 56 45 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 IVE lock. */.
c04e0 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 if( locktype==EX
c04f0 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 CLUSIVE_LOCK &&
c0500 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 res == NO_ERROR
c0510 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
c0520 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d File->locktype>=
c0530 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 SHARED_LOCK );.
c0540 20 20 20 72 65 73 20 3d 20 75 6e 6c 6f 63 6b 52 res = unlockR
c0550 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a eadLock(pFile);.
c0560 20 20 20 20 4f 53 54 52 41 43 45 32 28 20 22 75 OSTRACE2( "u
c0570 6e 72 65 61 64 6c 6f 63 6b 20 3d 20 25 64 5c 6e nreadlock = %d\n
c0580 22 2c 20 72 65 73 20 29 3b 0a 20 20 20 20 4c 6f ", res );. Lo
c0590 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d ckArea.lOffset =
c05a0 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 SHARED_FIRST;.
c05b0 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e LockArea.lRan
c05c0 67 65 20 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 ge = SHARED_SIZE
c05d0 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 ;. UnlockArea
c05e0 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 .lOffset = 0L;.
c05f0 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 UnlockArea.lR
c0600 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20 72 ange = 0L;. r
c0610 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c es = DosSetFileL
c0620 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 ocks( pFile->h,
c0630 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f &UnlockArea, &Lo
c0640 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d ckArea, LOCK_TIM
c0650 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 EOUT, 0L );.
c0660 69 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 if( res == NO_ER
c0670 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 ROR ){. new
c0680 4c 6f 63 6b 74 79 70 65 20 3d 20 45 58 43 4c 55 Locktype = EXCLU
c0690 53 49 56 45 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d SIVE_LOCK;. }
c06a0 65 6c 73 65 7b 0a 20 20 20 20 20 20 4f 53 54 52 else{. OSTR
c06b0 41 43 45 32 28 20 22 4f 53 2f 32 20 65 72 72 6f ACE2( "OS/2 erro
c06c0 72 2d 63 6f 64 65 20 3d 20 25 64 5c 6e 22 2c 20 r-code = %d\n",
c06d0 72 65 73 20 29 3b 0a 20 20 20 20 20 20 67 65 74 res );. get
c06e0 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b ReadLock(pFile);
c06f0 0a 20 20 20 20 7d 0a 20 20 20 20 4f 53 54 52 41 . }. OSTRA
c0700 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 61 63 CE3( "LOCK %d ac
c0710 71 75 69 72 65 20 65 78 63 6c 75 73 69 76 65 20 quire exclusive
c0720 6c 6f 63 6b 2e 20 20 72 65 73 3d 25 64 5c 6e 22 lock. res=%d\n"
c0730 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 , pFile->h, res
c0740 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 );. }.. /* If
c0750 77 65 20 61 72 65 20 68 6f 6c 64 69 6e 67 20 61 we are holding a
c0760 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 74 68 PENDING lock th
c0770 61 74 20 6f 75 67 68 74 20 74 6f 20 62 65 20 72 at ought to be r
c0780 65 6c 65 61 73 65 64 2c 20 74 68 65 6e 0a 20 20 eleased, then.
c0790 2a 2a 20 72 65 6c 65 61 73 65 20 69 74 20 6e 6f ** release it no
c07a0 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 67 6f w.. */. if( go
c07b0 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 26 26 20 tPendingLock &&
c07c0 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 locktype==SHARED
c07d0 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 69 6e 74 _LOCK ){. int
c07e0 20 72 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 r;. LockArea
c07f0 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 .lOffset = 0L;.
c0800 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e LockArea.lRan
c0810 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c ge = 0L;. Unl
c0820 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 ockArea.lOffset
c0830 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a = PENDING_BYTE;.
c0840 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c UnlockArea.l
c0850 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 Range = 1L;.
c0860 72 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f r = DosSetFileLo
c0870 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 cks( pFile->h, &
c0880 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 UnlockArea, &Loc
c0890 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 kArea, LOCK_TIME
c08a0 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f OUT, 0L );. O
c08b0 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 STRACE3( "LOCK %
c08c0 64 20 75 6e 6c 6f 63 6b 69 6e 67 20 70 65 6e 64 d unlocking pend
c08d0 69 6e 67 2f 69 73 20 73 68 61 72 65 64 2e 20 72 ing/is shared. r
c08e0 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 =%d\n", pFile->h
c08f0 2c 20 72 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a , r );. }.. /*
c0900 20 55 70 64 61 74 65 20 74 68 65 20 73 74 61 74 Update the stat
c0910 65 20 6f 66 20 74 68 65 20 6c 6f 63 6b 20 68 61 e of the lock ha
c0920 73 20 68 65 6c 64 20 69 6e 20 74 68 65 20 66 69 s held in the fi
c0930 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 68 le descriptor th
c0940 65 6e 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20 74 en. ** return t
c0950 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 72 he appropriate r
c0960 65 73 75 6c 74 20 63 6f 64 65 2e 0a 20 20 2a 2f esult code.. */
c0970 0a 20 20 69 66 28 20 72 65 73 20 3d 3d 20 4e 4f . if( res == NO
c0980 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 72 63 _ERROR ){. rc
c0990 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
c09a0 7d 65 6c 73 65 7b 0a 20 20 20 20 4f 53 54 52 41 }else{. OSTRA
c09b0 43 45 34 28 20 22 4c 4f 43 4b 20 46 41 49 4c 45 CE4( "LOCK FAILE
c09c0 44 20 25 64 20 74 72 79 69 6e 67 20 66 6f 72 20 D %d trying for
c09d0 25 64 20 62 75 74 20 67 6f 74 20 25 64 5c 6e 22 %d but got %d\n"
c09e0 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 , pFile->h,.
c09f0 20 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 lockty
c0a00 70 65 2c 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 pe, newLocktype
c0a10 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 );. rc = SQLI
c0a20 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20 70 TE_BUSY;. }. p
c0a30 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d File->locktype =
c0a40 20 6e 65 77 4c 6f 63 6b 74 79 70 65 3b 0a 20 20 newLocktype;.
c0a50 4f 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 OSTRACE3( "LOCK
c0a60 25 64 20 6e 6f 77 20 25 64 5c 6e 22 2c 20 70 46 %d now %d\n", pF
c0a70 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c ile->h, pFile->l
c0a80 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 72 65 74 ocktype );. ret
c0a90 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
c0aa0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 This routine ch
c0ab0 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 69 73 ecks if there is
c0ac0 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b a RESERVED lock
c0ad0 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 held on the spe
c0ae0 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 cified.** file b
c0af0 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 y this or any ot
c0b00 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20 her process. If
c0b10 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 such a lock is h
c0b20 65 6c 64 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 6e eld, return.** n
c0b30 6f 6e 2d 7a 65 72 6f 2c 20 6f 74 68 65 72 77 69 on-zero, otherwi
c0b40 73 65 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 se zero..*/.stat
c0b50 69 63 20 69 6e 74 20 6f 73 32 43 68 65 63 6b 52 ic int os2CheckR
c0b60 65 73 65 72 76 65 64 4c 6f 63 6b 28 20 73 71 6c eservedLock( sql
c0b70 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
c0b80 6e 74 20 2a 70 4f 75 74 20 29 7b 0a 20 20 69 6e nt *pOut ){. in
c0b90 74 20 72 20 3d 20 30 3b 0a 20 20 6f 73 32 46 69 t r = 0;. os2Fi
c0ba0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 le *pFile = (os2
c0bb0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 73 73 65 File*)id;. asse
c0bc0 72 74 28 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a rt( pFile!=0 );.
c0bd0 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 if( pFile->loc
c0be0 6b 74 79 70 65 3e 3d 52 45 53 45 52 56 45 44 5f ktype>=RESERVED_
c0bf0 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 20 3d 20 LOCK ){. r =
c0c00 31 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 1;. OSTRACE3(
c0c10 20 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 "TEST WR-LOCK %
c0c20 64 20 25 64 20 28 6c 6f 63 61 6c 29 5c 6e 22 2c d %d (local)\n",
c0c30 20 70 46 69 6c 65 2d 3e 68 2c 20 72 20 29 3b 0a pFile->h, r );.
c0c40 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 46 49 4c }else{. FIL
c0c50 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c ELOCK LockArea,
c0c60 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 55 . U
c0c70 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 20 20 41 nlockArea;. A
c0c80 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 PIRET rc = NO_ER
c0c90 52 4f 52 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 ROR;. memset(
c0ca0 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 &LockArea, 0, si
c0cb0 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61 29 29 3b zeof(LockArea));
c0cc0 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c . memset(&Unl
c0cd0 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 ockArea, 0, size
c0ce0 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b of(UnlockArea));
c0cf0 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f . LockArea.lO
c0d00 66 66 73 65 74 20 3d 20 52 45 53 45 52 56 45 44 ffset = RESERVED
c0d10 5f 42 59 54 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 _BYTE;. LockA
c0d20 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b rea.lRange = 1L;
c0d30 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e . UnlockArea.
c0d40 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 lOffset = 0L;.
c0d50 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 UnlockArea.lRa
c0d60 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20 72 63 nge = 0L;. rc
c0d70 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 = DosSetFileLoc
c0d80 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 ks( pFile->h, &U
c0d90 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b nlockArea, &Lock
c0da0 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f Area, LOCK_TIMEO
c0db0 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 UT, 0L );. OS
c0dc0 54 52 41 43 45 33 28 20 22 54 45 53 54 20 57 52 TRACE3( "TEST WR
c0dd0 2d 4c 4f 43 4b 20 25 64 20 6c 6f 63 6b 20 72 65 -LOCK %d lock re
c0de0 73 65 72 76 65 64 20 62 79 74 65 20 72 63 3d 25 served byte rc=%
c0df0 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 d\n", pFile->h,
c0e00 72 63 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63 rc );. if( rc
c0e10 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a == NO_ERROR ){.
c0e20 20 20 20 20 20 20 41 50 49 52 45 54 20 72 63 75 APIRET rcu
c0e30 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 20 2f 2a 20 = NO_ERROR; /*
c0e40 72 65 74 75 72 6e 20 63 6f 64 65 20 66 6f 72 20 return code for
c0e50 75 6e 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 unlocking */.
c0e60 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 LockArea.lOff
c0e70 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 20 20 set = 0L;.
c0e80 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 LockArea.lRange
c0e90 3d 20 30 4c 3b 0a 20 20 20 20 20 20 55 6e 6c 6f = 0L;. Unlo
c0ea0 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d ckArea.lOffset =
c0eb0 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a RESERVED_BYTE;.
c0ec0 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 UnlockArea
c0ed0 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 .lRange = 1L;.
c0ee0 20 20 20 20 72 63 75 20 3d 20 44 6f 73 53 65 74 rcu = DosSet
c0ef0 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 FileLocks( pFile
c0f00 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 ->h, &UnlockArea
c0f10 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 , &LockArea, LOC
c0f20 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20 29 3b K_TIMEOUT, 0L );
c0f30 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 33 28 . OSTRACE3(
c0f40 20 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 "TEST WR-LOCK %
c0f50 64 20 75 6e 6c 6f 63 6b 20 72 65 73 65 72 76 65 d unlock reserve
c0f60 64 20 62 79 74 65 20 72 3d 25 64 5c 6e 22 2c 20 d byte r=%d\n",
c0f70 70 46 69 6c 65 2d 3e 68 2c 20 72 63 75 20 29 3b pFile->h, rcu );
c0f80 0a 20 20 20 20 7d 0a 20 20 20 20 72 20 3d 20 21 . }. r = !
c0f90 28 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 (rc == NO_ERROR)
c0fa0 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 ;. OSTRACE3(
c0fb0 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 "TEST WR-LOCK %d
c0fc0 20 25 64 20 28 72 65 6d 6f 74 65 29 5c 6e 22 2c %d (remote)\n",
c0fd0 20 70 46 69 6c 65 2d 3e 68 2c 20 72 20 29 3b 0a pFile->h, r );.
c0fe0 20 20 7d 0a 20 20 2a 70 4f 75 74 20 3d 20 72 3b }. *pOut = r;
c0ff0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
c1000 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f _OK;.}../*.** Lo
c1010 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 wer the locking
c1020 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 level on file de
c1030 73 63 72 69 70 74 6f 72 20 69 64 20 74 6f 20 6c scriptor id to l
c1040 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 ocktype. lockty
c1050 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 pe.** must be ei
c1060 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 ther NO_LOCK or
c1070 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a SHARED_LOCK..**.
c1080 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e ** If the lockin
c1090 67 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 g level of the f
c10a0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 ile descriptor i
c10b0 73 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 s already at or
c10c0 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 below.** the req
c10d0 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c uested locking l
c10e0 65 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 evel, this routi
c10f0 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a ne is a no-op..*
c1100 2a 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 *.** It is not p
c1110 6f 73 73 69 62 6c 65 20 66 6f 72 20 74 68 69 73 ossible for this
c1120 20 72 6f 75 74 69 6e 65 20 74 6f 20 66 61 69 6c routine to fail
c1130 20 69 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 if the second a
c1140 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 4e 4f rgument.** is NO
c1150 5f 4c 4f 43 4b 2e 20 20 49 66 20 74 68 65 20 73 _LOCK. If the s
c1160 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 econd argument i
c1170 73 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 74 68 s SHARED_LOCK th
c1180 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a en this routine.
c1190 2a 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 ** might return
c11a0 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 2a 2f SQLITE_IOERR;.*/
c11b0 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 55 .static int os2U
c11c0 6e 6c 6f 63 6b 28 20 73 71 6c 69 74 65 33 5f 66 nlock( sqlite3_f
c11d0 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 ile *id, int loc
c11e0 6b 74 79 70 65 20 29 7b 0a 20 20 69 6e 74 20 74 ktype ){. int t
c11f0 79 70 65 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a ype;. os2File *
c1200 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 pFile = (os2File
c1210 2a 29 69 64 3b 0a 20 20 41 50 49 52 45 54 20 72 *)id;. APIRET r
c1220 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
c1230 20 41 50 49 52 45 54 20 72 65 73 20 3d 20 4e 4f APIRET res = NO
c1240 5f 45 52 52 4f 52 3b 0a 20 20 46 49 4c 45 4c 4f _ERROR;. FILELO
c1250 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 CK LockArea,.
c1260 20 20 20 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b Unlock
c1270 41 72 65 61 3b 0a 20 20 6d 65 6d 73 65 74 28 26 Area;. memset(&
c1280 4c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a LockArea, 0, siz
c1290 65 6f 66 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a eof(LockArea));.
c12a0 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b memset(&Unlock
c12b0 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 Area, 0, sizeof(
c12c0 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 UnlockArea));.
c12d0 61 73 73 65 72 74 28 20 70 46 69 6c 65 21 3d 30 assert( pFile!=0
c12e0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f );. assert( lo
c12f0 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c cktype<=SHARED_L
c1300 4f 43 4b 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 OCK );. OSTRACE
c1310 34 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 74 6f 4( "UNLOCK %d to
c1320 20 25 64 20 77 61 73 20 25 64 5c 6e 22 2c 20 70 %d was %d\n", p
c1330 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 File->h, locktyp
c1340 65 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 e, pFile->lockty
c1350 70 65 20 29 3b 0a 20 20 74 79 70 65 20 3d 20 70 pe );. type = p
c1360 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a File->locktype;.
c1370 20 20 69 66 28 20 74 79 70 65 3e 3d 45 58 43 4c if( type>=EXCL
c1380 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 USIVE_LOCK ){.
c1390 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 LockArea.lOffs
c13a0 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 et = 0L;. Loc
c13b0 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 kArea.lRange = 0
c13c0 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 L;. UnlockAre
c13d0 61 2e 6c 4f 66 66 73 65 74 20 3d 20 53 48 41 52 a.lOffset = SHAR
c13e0 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 55 6e ED_FIRST;. Un
c13f0 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 lockArea.lRange
c1400 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 = SHARED_SIZE;.
c1410 20 20 20 72 65 73 20 3d 20 44 6f 73 53 65 74 46 res = DosSetF
c1420 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d ileLocks( pFile-
c1430 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c >h, &UnlockArea,
c1440 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b &LockArea, LOCK
c1450 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a _TIMEOUT, 0L );.
c1460 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 55 OSTRACE3( "U
c1470 4e 4c 4f 43 4b 20 25 64 20 65 78 63 6c 75 73 69 NLOCK %d exclusi
c1480 76 65 20 6c 6f 63 6b 20 72 65 73 3d 25 64 5c 6e ve lock res=%d\n
c1490 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 ", pFile->h, res
c14a0 20 29 3b 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b );. if( lock
c14b0 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 type==SHARED_LOC
c14c0 4b 20 26 26 20 67 65 74 52 65 61 64 4c 6f 63 6b K && getReadLock
c14d0 28 70 46 69 6c 65 29 20 21 3d 20 4e 4f 5f 45 52 (pFile) != NO_ER
c14e0 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 ROR ){. /*
c14f0 54 68 69 73 20 73 68 6f 75 6c 64 20 6e 65 76 65 This should neve
c1500 72 20 68 61 70 70 65 6e 2e 20 20 57 65 20 73 68 r happen. We sh
c1510 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65 20 61 ould always be a
c1520 62 6c 65 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 ble to. **
c1530 72 65 61 63 71 75 69 72 65 20 74 68 65 20 72 65 reacquire the re
c1540 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 ad lock */.
c1550 20 4f 53 54 52 41 43 45 33 28 20 22 55 4e 4c 4f OSTRACE3( "UNLO
c1560 43 4b 20 25 64 20 74 6f 20 25 64 20 67 65 74 52 CK %d to %d getR
c1570 65 61 64 4c 6f 63 6b 28 29 20 66 61 69 6c 65 64 eadLock() failed
c1580 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c \n", pFile->h, l
c1590 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 20 ocktype );.
c15a0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 rc = SQLITE_IOE
c15b0 52 52 5f 55 4e 4c 4f 43 4b 3b 0a 20 20 20 20 7d RR_UNLOCK;. }
c15c0 0a 20 20 7d 0a 20 20 69 66 28 20 74 79 70 65 3e . }. if( type>
c15d0 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 =RESERVED_LOCK )
c15e0 7b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c {. LockArea.l
c15f0 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 Offset = 0L;.
c1600 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 LockArea.lRange
c1610 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 = 0L;. Unloc
c1620 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 kArea.lOffset =
c1630 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 RESERVED_BYTE;.
c1640 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 UnlockArea.lR
c1650 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 72 ange = 1L;. r
c1660 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c es = DosSetFileL
c1670 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 ocks( pFile->h,
c1680 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f &UnlockArea, &Lo
c1690 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d ckArea, LOCK_TIM
c16a0 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 EOUT, 0L );.
c16b0 4f 53 54 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 OSTRACE3( "UNLOC
c16c0 4b 20 25 64 20 72 65 73 65 72 76 65 64 20 72 65 K %d reserved re
c16d0 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e s=%d\n", pFile->
c16e0 68 2c 20 72 65 73 20 29 3b 0a 20 20 7d 0a 20 20 h, res );. }.
c16f0 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f if( locktype==NO
c1700 5f 4c 4f 43 4b 20 26 26 20 74 79 70 65 3e 3d 53 _LOCK && type>=S
c1710 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 HARED_LOCK ){.
c1720 20 20 72 65 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 res = unlockRe
c1730 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 adLock(pFile);.
c1740 20 20 20 4f 53 54 52 41 43 45 35 28 20 22 55 4e OSTRACE5( "UN
c1750 4c 4f 43 4b 20 25 64 20 69 73 20 25 64 20 77 61 LOCK %d is %d wa
c1760 6e 74 20 25 64 20 72 65 73 3d 25 64 5c 6e 22 2c nt %d res=%d\n",
c1770 20 70 46 69 6c 65 2d 3e 68 2c 20 74 79 70 65 2c pFile->h, type,
c1780 20 6c 6f 63 6b 74 79 70 65 2c 20 72 65 73 20 29 locktype, res )
c1790 3b 0a 20 20 7d 0a 20 20 69 66 28 20 74 79 70 65 ;. }. if( type
c17a0 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 >=PENDING_LOCK )
c17b0 7b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c {. LockArea.l
c17c0 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 Offset = 0L;.
c17d0 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 LockArea.lRange
c17e0 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 = 0L;. Unloc
c17f0 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 kArea.lOffset =
c1800 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 PENDING_BYTE;.
c1810 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 UnlockArea.lRa
c1820 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 72 65 nge = 1L;. re
c1830 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f s = DosSetFileLo
c1840 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 cks( pFile->h, &
c1850 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 UnlockArea, &Loc
c1860 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 kArea, LOCK_TIME
c1870 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f OUT, 0L );. O
c1880 53 54 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b STRACE3( "UNLOCK
c1890 20 25 64 20 70 65 6e 64 69 6e 67 20 72 65 73 3d %d pending res=
c18a0 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c %d\n", pFile->h,
c18b0 20 72 65 73 20 29 3b 0a 20 20 7d 0a 20 20 70 46 res );. }. pF
c18c0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 ile->locktype =
c18d0 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 4f 53 54 52 locktype;. OSTR
c18e0 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 ACE3( "UNLOCK %d
c18f0 20 6e 6f 77 20 25 64 5c 6e 22 2c 20 70 46 69 6c now %d\n", pFil
c1900 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 e->h, pFile->loc
c1910 6b 74 79 70 65 20 29 3b 0a 20 20 72 65 74 75 72 ktype );. retur
c1920 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 n rc;.}../*.** C
c1930 6f 6e 74 72 6f 6c 20 61 6e 64 20 71 75 65 72 79 ontrol and query
c1940 20 6f 66 20 74 68 65 20 6f 70 65 6e 20 66 69 6c of the open fil
c1950 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 e handle..*/.sta
c1960 74 69 63 20 69 6e 74 20 6f 73 32 46 69 6c 65 43 tic int os2FileC
c1970 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 ontrol(sqlite3_f
c1980 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f 70 2c ile *id, int op,
c1990 20 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 void *pArg){.
c19a0 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 switch( op ){.
c19b0 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 43 case SQLITE_FC
c19c0 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 3a 20 7b NTL_LOCKSTATE: {
c19d0 0a 20 20 20 20 20 20 2a 28 69 6e 74 2a 29 70 41 . *(int*)pA
c19e0 72 67 20 3d 20 28 28 6f 73 32 46 69 6c 65 2a 29 rg = ((os2File*)
c19f0 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 id)->locktype;.
c1a00 20 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 OSTRACE3( "
c1a10 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 20 FCNTL_LOCKSTATE
c1a20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 28 %d lock=%d\n", (
c1a30 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 68 (os2File*)id)->h
c1a40 2c 20 28 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 , ((os2File*)id)
c1a50 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 ->locktype );.
c1a60 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
c1a70 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a E_OK;. }. }.
c1a80 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
c1a90 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ERROR;.}../*.**
c1aa0 52 65 74 75 72 6e 20 74 68 65 20 73 65 63 74 6f Return the secto
c1ab0 72 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 r size in bytes
c1ac0 6f 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e of the underlyin
c1ad0 67 20 62 6c 6f 63 6b 20 64 65 76 69 63 65 20 66 g block device f
c1ae0 6f 72 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 66 or.** the specif
c1af0 69 65 64 20 66 69 6c 65 2e 20 54 68 69 73 20 69 ied file. This i
c1b00 73 20 61 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20 s almost always
c1b10 35 31 32 20 62 79 74 65 73 2c 20 62 75 74 20 6d 512 bytes, but m
c1b20 61 79 20 62 65 0a 2a 2a 20 6c 61 72 67 65 72 20 ay be.** larger
c1b30 66 6f 72 20 73 6f 6d 65 20 64 65 76 69 63 65 73 for some devices
c1b40 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 ..**.** SQLite c
c1b50 6f 64 65 20 61 73 73 75 6d 65 73 20 74 68 69 73 ode assumes this
c1b60 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 function cannot
c1b70 20 66 61 69 6c 2e 20 49 74 20 61 6c 73 6f 20 61 fail. It also a
c1b80 73 73 75 6d 65 73 20 74 68 61 74 0a 2a 2a 20 69 ssumes that.** i
c1b90 66 20 74 77 6f 20 66 69 6c 65 73 20 61 72 65 20 f two files are
c1ba0 63 72 65 61 74 65 64 20 69 6e 20 74 68 65 20 73 created in the s
c1bb0 61 6d 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 ame file-system
c1bc0 64 69 72 65 63 74 6f 72 79 20 28 69 2e 65 2e 0a directory (i.e..
c1bd0 2a 2a 20 61 20 64 61 74 61 62 61 73 65 20 61 6e ** a database an
c1be0 64 20 69 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69 d its journal fi
c1bf0 6c 65 29 20 74 68 61 74 20 74 68 65 20 73 65 63 le) that the sec
c1c00 74 6f 72 20 73 69 7a 65 20 77 69 6c 6c 20 62 65 tor size will be
c1c10 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 66 6f 72 the.** same for
c1c20 20 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 both..*/.static
c1c30 20 69 6e 74 20 6f 73 32 53 65 63 74 6f 72 53 69 int os2SectorSi
c1c40 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ze(sqlite3_file
c1c50 2a 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 53 *id){. return S
c1c60 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 QLITE_DEFAULT_SE
c1c70 43 54 4f 52 5f 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a CTOR_SIZE;.}../*
c1c80 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 76 65 63 .** Return a vec
c1c90 74 6f 72 20 6f 66 20 64 65 76 69 63 65 20 63 68 tor of device ch
c1ca0 61 72 61 63 74 65 72 69 73 74 69 63 73 2e 0a 2a aracteristics..*
c1cb0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 /.static int os2
c1cc0 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 DeviceCharacteri
c1cd0 73 74 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69 stics(sqlite3_fi
c1ce0 6c 65 20 2a 69 64 29 7b 0a 20 20 72 65 74 75 72 le *id){. retur
c1cf0 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 n 0;.}.../*.** C
c1d00 68 61 72 61 63 74 65 72 20 73 65 74 20 63 6f 6e haracter set con
c1d10 76 65 72 73 69 6f 6e 20 6f 62 6a 65 63 74 73 20 version objects
c1d20 75 73 65 64 20 62 79 20 63 6f 6e 76 65 72 73 69 used by conversi
c1d30 6f 6e 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a on routines..*/.
c1d40 73 74 61 74 69 63 20 55 63 6f 6e 76 4f 62 6a 65 static UconvObje
c1d50 63 74 20 75 63 55 74 66 38 20 3d 20 4e 55 4c 4c ct ucUtf8 = NULL
c1d60 3b 20 2f 2a 20 63 6f 6e 76 65 72 74 20 62 65 74 ; /* convert bet
c1d70 77 65 65 6e 20 55 54 46 2d 38 20 61 6e 64 20 55 ween UTF-8 and U
c1d80 43 53 2d 32 20 2a 2f 0a 73 74 61 74 69 63 20 55 CS-2 */.static U
c1d90 63 6f 6e 76 4f 62 6a 65 63 74 20 75 63 6c 43 70 convObject uclCp
c1da0 20 3d 20 4e 55 4c 4c 3b 20 20 2f 2a 20 63 6f 6e = NULL; /* con
c1db0 76 65 72 74 20 62 65 74 77 65 65 6e 20 6c 6f 63 vert between loc
c1dc0 61 6c 20 63 6f 64 65 70 61 67 65 20 61 6e 64 20 al codepage and
c1dd0 55 43 53 2d 32 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 UCS-2 */../*.**
c1de0 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 Helper function
c1df0 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 to initialize th
c1e00 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 62 6a e conversion obj
c1e10 65 63 74 73 20 66 72 6f 6d 20 61 6e 64 20 74 6f ects from and to
c1e20 20 55 54 46 2d 38 2e 0a 2a 2f 0a 73 74 61 74 69 UTF-8..*/.stati
c1e30 63 20 76 6f 69 64 20 69 6e 69 74 55 63 6f 6e 76 c void initUconv
c1e40 4f 62 6a 65 63 74 73 28 20 76 6f 69 64 20 29 7b Objects( void ){
c1e50 0a 20 20 69 66 28 20 55 6e 69 43 72 65 61 74 65 . if( UniCreate
c1e60 55 63 6f 6e 76 4f 62 6a 65 63 74 28 20 55 54 46 UconvObject( UTF
c1e70 5f 38 2c 20 26 75 63 55 74 66 38 20 29 20 21 3d _8, &ucUtf8 ) !=
c1e80 20 55 4c 53 5f 53 55 43 43 45 53 53 20 29 0a 20 ULS_SUCCESS ).
c1e90 20 20 20 75 63 55 74 66 38 20 3d 20 4e 55 4c 4c ucUtf8 = NULL
c1ea0 3b 0a 20 20 69 66 20 28 20 55 6e 69 43 72 65 61 ;. if ( UniCrea
c1eb0 74 65 55 63 6f 6e 76 4f 62 6a 65 63 74 28 20 28 teUconvObject( (
c1ec0 55 6e 69 43 68 61 72 20 2a 29 4c 22 40 70 61 74 UniChar *)L"@pat
c1ed0 68 3d 79 65 73 22 2c 20 26 75 63 6c 43 70 20 29 h=yes", &uclCp )
c1ee0 20 21 3d 20 55 4c 53 5f 53 55 43 43 45 53 53 20 != ULS_SUCCESS
c1ef0 29 0a 20 20 20 20 75 63 6c 43 70 20 3d 20 4e 55 ). uclCp = NU
c1f00 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c LL;.}../*.** Hel
c1f10 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 per function to
c1f20 66 72 65 65 20 74 68 65 20 63 6f 6e 76 65 72 73 free the convers
c1f30 69 6f 6e 20 6f 62 6a 65 63 74 73 20 66 72 6f 6d ion objects from
c1f40 20 61 6e 64 20 74 6f 20 55 54 46 2d 38 2e 0a 2a and to UTF-8..*
c1f50 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 /.static void fr
c1f60 65 65 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 20 eeUconvObjects(
c1f70 76 6f 69 64 20 29 7b 0a 20 20 69 66 20 28 20 75 void ){. if ( u
c1f80 63 55 74 66 38 20 29 0a 20 20 20 20 55 6e 69 46 cUtf8 ). UniF
c1f90 72 65 65 55 63 6f 6e 76 4f 62 6a 65 63 74 28 20 reeUconvObject(
c1fa0 75 63 55 74 66 38 20 29 3b 0a 20 20 69 66 20 28 ucUtf8 );. if (
c1fb0 20 75 63 6c 43 70 20 29 0a 20 20 20 20 55 6e 69 uclCp ). Uni
c1fc0 46 72 65 65 55 63 6f 6e 76 4f 62 6a 65 63 74 28 FreeUconvObject(
c1fd0 20 75 63 6c 43 70 20 29 3b 0a 20 20 75 63 55 74 uclCp );. ucUt
c1fe0 66 38 20 3d 20 4e 55 4c 4c 3b 0a 20 20 75 63 6c f8 = NULL;. ucl
c1ff0 43 70 20 3d 20 4e 55 4c 4c 3b 0a 7d 0a 0a 2f 2a Cp = NULL;.}../*
c2000 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 74 .** Helper funct
c2010 69 6f 6e 20 74 6f 20 63 6f 6e 76 65 72 74 20 55 ion to convert U
c2020 54 46 2d 38 20 66 69 6c 65 6e 61 6d 65 73 20 74 TF-8 filenames t
c2030 6f 20 6c 6f 63 61 6c 20 4f 53 2f 32 20 63 6f 64 o local OS/2 cod
c2040 65 70 61 67 65 2e 0a 2a 2a 20 54 68 65 20 74 77 epage..** The tw
c2050 6f 2d 73 74 65 70 20 70 72 6f 63 65 73 73 3a 20 o-step process:
c2060 66 69 72 73 74 20 63 6f 6e 76 65 72 74 20 74 68 first convert th
c2070 65 20 69 6e 63 6f 6d 69 6e 67 20 55 54 46 2d 38 e incoming UTF-8
c2080 20 73 74 72 69 6e 67 0a 2a 2a 20 69 6e 74 6f 20 string.** into
c2090 55 43 53 2d 32 20 61 6e 64 20 74 68 65 6e 20 66 UCS-2 and then f
c20a0 72 6f 6d 20 55 43 53 2d 32 20 74 6f 20 74 68 65 rom UCS-2 to the
c20b0 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61 67 current codepag
c20c0 65 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e e..** The return
c20d0 65 64 20 63 68 61 72 20 70 6f 69 6e 74 65 72 20 ed char pointer
c20e0 68 61 73 20 74 6f 20 62 65 20 66 72 65 65 64 2e has to be freed.
c20f0 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 .*/.static char
c2100 2a 63 6f 6e 76 65 72 74 55 74 66 38 50 61 74 68 *convertUtf8Path
c2110 54 6f 43 70 28 20 63 6f 6e 73 74 20 63 68 61 72 ToCp( const char
c2120 20 2a 69 6e 20 29 7b 0a 20 20 55 6e 69 43 68 61 *in ){. UniCha
c2130 72 20 74 65 6d 70 50 61 74 68 5b 43 43 48 4d 41 r tempPath[CCHMA
c2140 58 50 41 54 48 5d 3b 0a 20 20 63 68 61 72 20 2a XPATH];. char *
c2150 6f 75 74 20 3d 20 28 63 68 61 72 20 2a 29 63 61 out = (char *)ca
c2160 6c 6c 6f 63 28 20 43 43 48 4d 41 58 50 41 54 48 lloc( CCHMAXPATH
c2170 2c 20 31 20 29 3b 0a 0a 20 20 69 66 28 20 21 6f , 1 );.. if( !o
c2180 75 74 20 29 0a 20 20 20 20 72 65 74 75 72 6e 20 ut ). return
c2190 4e 55 4c 4c 3b 0a 0a 20 20 69 66 28 20 21 75 63 NULL;.. if( !uc
c21a0 55 74 66 38 20 7c 7c 20 21 75 63 6c 43 70 20 29 Utf8 || !uclCp )
c21b0 0a 20 20 20 20 69 6e 69 74 55 63 6f 6e 76 4f 62 . initUconvOb
c21c0 6a 65 63 74 73 28 29 3b 0a 0a 20 20 2f 2a 20 64 jects();.. /* d
c21d0 65 74 65 72 6d 69 6e 65 20 73 74 72 69 6e 67 20 etermine string
c21e0 66 6f 72 20 74 68 65 20 63 6f 6e 76 65 72 73 69 for the conversi
c21f0 6f 6e 20 6f 66 20 55 54 46 2d 38 20 77 68 69 63 on of UTF-8 whic
c2200 68 20 69 73 20 43 50 31 32 30 38 20 2a 2f 0a 20 h is CP1208 */.
c2210 20 69 66 28 20 55 6e 69 53 74 72 54 6f 55 63 73 if( UniStrToUcs
c2220 28 20 75 63 55 74 66 38 2c 20 74 65 6d 70 50 61 ( ucUtf8, tempPa
c2230 74 68 2c 20 28 63 68 61 72 20 2a 29 69 6e 2c 20 th, (char *)in,
c2240 43 43 48 4d 41 58 50 41 54 48 20 29 20 21 3d 20 CCHMAXPATH ) !=
c2250 55 4c 53 5f 53 55 43 43 45 53 53 20 29 0a 20 20 ULS_SUCCESS ).
c2260 20 20 72 65 74 75 72 6e 20 6f 75 74 3b 20 2f 2a return out; /*
c2270 20 69 66 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 if conversion f
c2280 61 69 6c 73 2c 20 72 65 74 75 72 6e 20 74 68 65 ails, return the
c2290 20 65 6d 70 74 79 20 73 74 72 69 6e 67 20 2a 2f empty string */
c22a0 0a 0a 20 20 2f 2a 20 63 6f 6e 76 65 72 73 69 6f .. /* conversio
c22b0 6e 20 66 6f 72 20 63 75 72 72 65 6e 74 20 63 6f n for current co
c22c0 64 65 70 61 67 65 20 77 68 69 63 68 20 63 61 6e depage which can
c22d0 20 62 65 20 75 73 65 64 20 66 6f 72 20 70 61 74 be used for pat
c22e0 68 73 20 2a 2f 0a 20 20 55 6e 69 53 74 72 46 72 hs */. UniStrFr
c22f0 6f 6d 55 63 73 28 20 75 63 6c 43 70 2c 20 6f 75 omUcs( uclCp, ou
c2300 74 2c 20 74 65 6d 70 50 61 74 68 2c 20 43 43 48 t, tempPath, CCH
c2310 4d 41 58 50 41 54 48 20 29 3b 0a 0a 20 20 72 65 MAXPATH );.. re
c2320 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a turn out;.}../*.
c2330 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 74 69 ** Helper functi
c2340 6f 6e 20 74 6f 20 63 6f 6e 76 65 72 74 20 66 69 on to convert fi
c2350 6c 65 6e 61 6d 65 73 20 66 72 6f 6d 20 6c 6f 63 lenames from loc
c2360 61 6c 20 63 6f 64 65 70 61 67 65 20 74 6f 20 55 al codepage to U
c2370 54 46 2d 38 2e 0a 2a 2a 20 54 68 65 20 74 77 6f TF-8..** The two
c2380 2d 73 74 65 70 20 70 72 6f 63 65 73 73 3a 20 66 -step process: f
c2390 69 72 73 74 20 63 6f 6e 76 65 72 74 20 74 68 65 irst convert the
c23a0 20 69 6e 63 6f 6d 69 6e 67 20 63 6f 64 65 70 61 incoming codepa
c23b0 67 65 2d 73 70 65 63 69 66 69 63 0a 2a 2a 20 73 ge-specific.** s
c23c0 74 72 69 6e 67 20 69 6e 74 6f 20 55 43 53 2d 32 tring into UCS-2
c23d0 20 61 6e 64 20 74 68 65 6e 20 66 72 6f 6d 20 55 and then from U
c23e0 43 53 2d 32 20 74 6f 20 74 68 65 20 63 6f 64 65 CS-2 to the code
c23f0 70 61 67 65 20 6f 66 20 55 54 46 2d 38 2e 0a 2a page of UTF-8..*
c2400 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 63 * The returned c
c2410 68 61 72 20 70 6f 69 6e 74 65 72 20 68 61 73 20 har pointer has
c2420 74 6f 20 62 65 20 66 72 65 65 64 2e 0a 2a 2a 0a to be freed..**.
c2430 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
c2440 20 69 73 20 6e 6f 6e 2d 73 74 61 74 69 63 20 74 is non-static t
c2450 6f 20 62 65 20 61 62 6c 65 20 74 6f 20 75 73 65 o be able to use
c2460 20 74 68 69 73 20 69 6e 20 73 68 65 6c 6c 2e 63 this in shell.c
c2470 20 61 6e 64 0a 2a 2a 20 73 69 6d 69 6c 61 72 20 and.** similar
c2480 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 applications tha
c2490 74 20 74 61 6b 65 20 63 6f 6d 6d 61 6e 64 20 6c t take command l
c24a0 69 6e 65 20 61 72 67 75 6d 65 6e 74 73 2e 0a 2a ine arguments..*
c24b0 2f 0a 63 68 61 72 20 2a 63 6f 6e 76 65 72 74 43 /.char *convertC
c24c0 70 50 61 74 68 54 6f 55 74 66 38 28 20 63 6f 6e pPathToUtf8( con
c24d0 73 74 20 63 68 61 72 20 2a 69 6e 20 29 7b 0a 20 st char *in ){.
c24e0 20 55 6e 69 43 68 61 72 20 74 65 6d 70 50 61 74 UniChar tempPat
c24f0 68 5b 43 43 48 4d 41 58 50 41 54 48 5d 3b 0a 20 h[CCHMAXPATH];.
c2500 20 63 68 61 72 20 2a 6f 75 74 20 3d 20 28 63 68 char *out = (ch
c2510 61 72 20 2a 29 63 61 6c 6c 6f 63 28 20 43 43 48 ar *)calloc( CCH
c2520 4d 41 58 50 41 54 48 2c 20 31 20 29 3b 0a 0a 20 MAXPATH, 1 );..
c2530 20 69 66 28 20 21 6f 75 74 20 29 0a 20 20 20 20 if( !out ).
c2540 72 65 74 75 72 6e 20 4e 55 4c 4c 3b 0a 0a 20 20 return NULL;..
c2550 69 66 28 20 21 75 63 55 74 66 38 20 7c 7c 20 21 if( !ucUtf8 || !
c2560 75 63 6c 43 70 20 29 0a 20 20 20 20 69 6e 69 74 uclCp ). init
c2570 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29 3b 0a UconvObjects();.
c2580 0a 20 20 2f 2a 20 63 6f 6e 76 65 72 73 69 6f 6e . /* conversion
c2590 20 66 6f 72 20 63 75 72 72 65 6e 74 20 63 6f 64 for current cod
c25a0 65 70 61 67 65 20 77 68 69 63 68 20 63 61 6e 20 epage which can
c25b0 62 65 20 75 73 65 64 20 66 6f 72 20 70 61 74 68 be used for path
c25c0 73 20 2a 2f 0a 20 20 69 66 28 20 55 6e 69 53 74 s */. if( UniSt
c25d0 72 54 6f 55 63 73 28 20 75 63 6c 43 70 2c 20 74 rToUcs( uclCp, t
c25e0 65 6d 70 50 61 74 68 2c 20 28 63 68 61 72 20 2a empPath, (char *
c25f0 29 69 6e 2c 20 43 43 48 4d 41 58 50 41 54 48 20 )in, CCHMAXPATH
c2600 29 20 21 3d 20 55 4c 53 5f 53 55 43 43 45 53 53 ) != ULS_SUCCESS
c2610 20 29 0a 20 20 20 20 72 65 74 75 72 6e 20 6f 75 ). return ou
c2620 74 3b 20 2f 2a 20 69 66 20 63 6f 6e 76 65 72 73 t; /* if convers
c2630 69 6f 6e 20 66 61 69 6c 73 2c 20 72 65 74 75 72 ion fails, retur
c2640 6e 20 74 68 65 20 65 6d 70 74 79 20 73 74 72 69 n the empty stri
c2650 6e 67 20 2a 2f 0a 0a 20 20 2f 2a 20 64 65 74 65 ng */.. /* dete
c2660 72 6d 69 6e 65 20 73 74 72 69 6e 67 20 66 6f 72 rmine string for
c2670 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 the conversion
c2680 6f 66 20 55 54 46 2d 38 20 77 68 69 63 68 20 69 of UTF-8 which i
c2690 73 20 43 50 31 32 30 38 20 2a 2f 0a 20 20 55 6e s CP1208 */. Un
c26a0 69 53 74 72 46 72 6f 6d 55 63 73 28 20 75 63 55 iStrFromUcs( ucU
c26b0 74 66 38 2c 20 6f 75 74 2c 20 74 65 6d 70 50 61 tf8, out, tempPa
c26c0 74 68 2c 20 43 43 48 4d 41 58 50 41 54 48 20 29 th, CCHMAXPATH )
c26d0 3b 0a 0a 20 20 72 65 74 75 72 6e 20 6f 75 74 3b ;.. return out;
c26e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 .}../*.** This v
c26f0 65 63 74 6f 72 20 64 65 66 69 6e 65 73 20 61 6c ector defines al
c2700 6c 20 74 68 65 20 6d 65 74 68 6f 64 73 20 74 68 l the methods th
c2710 61 74 20 63 61 6e 20 6f 70 65 72 61 74 65 20 6f at can operate o
c2720 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f n an.** sqlite3_
c2730 66 69 6c 65 20 66 6f 72 20 6f 73 32 2e 0a 2a 2f file for os2..*/
c2740 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 .static const sq
c2750 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
c2760 20 6f 73 32 49 6f 4d 65 74 68 6f 64 20 3d 20 7b os2IoMethod = {
c2770 0a 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 . 1,
c2780 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
c2790 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 6f 73 iVersion */. os
c27a0 32 43 6c 6f 73 65 2c 0a 20 20 6f 73 32 52 65 61 2Close,. os2Rea
c27b0 64 2c 0a 20 20 6f 73 32 57 72 69 74 65 2c 0a 20 d,. os2Write,.
c27c0 20 6f 73 32 54 72 75 6e 63 61 74 65 2c 0a 20 20 os2Truncate,.
c27d0 6f 73 32 53 79 6e 63 2c 0a 20 20 6f 73 32 46 69 os2Sync,. os2Fi
c27e0 6c 65 53 69 7a 65 2c 0a 20 20 6f 73 32 4c 6f 63 leSize,. os2Loc
c27f0 6b 2c 0a 20 20 6f 73 32 55 6e 6c 6f 63 6b 2c 0a k,. os2Unlock,.
c2800 20 20 6f 73 32 43 68 65 63 6b 52 65 73 65 72 76 os2CheckReserv
c2810 65 64 4c 6f 63 6b 2c 0a 20 20 6f 73 32 46 69 6c edLock,. os2Fil
c2820 65 43 6f 6e 74 72 6f 6c 2c 0a 20 20 6f 73 32 53 eControl,. os2S
c2830 65 63 74 6f 72 53 69 7a 65 2c 0a 20 20 6f 73 32 ectorSize,. os2
c2840 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 DeviceCharacteri
c2850 73 74 69 63 73 0a 7d 3b 0a 0a 2f 2a 2a 2a 2a 2a stics.};../*****
c2860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c28a0 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65 72 65 20 65 ******.** Here e
c28b0 6e 64 73 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 nds the I/O meth
c28c0 6f 64 73 20 74 68 61 74 20 66 6f 72 6d 20 74 68 ods that form th
c28d0 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 e sqlite3_io_met
c28e0 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a hods object..**.
c28f0 2a 2a 20 54 68 65 20 6e 65 78 74 20 62 6c 6f 63 ** The next bloc
c2900 6b 20 6f 66 20 63 6f 64 65 20 69 6d 70 6c 65 6d k of code implem
c2910 65 6e 74 73 20 74 68 65 20 56 46 53 20 6d 65 74 ents the VFS met
c2920 68 6f 64 73 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a hods..**********
c2930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2970 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 **/../*.** Creat
c2980 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 e a temporary fi
c2990 6c 65 20 6e 61 6d 65 20 69 6e 20 7a 42 75 66 2e le name in zBuf.
c29a0 20 20 7a 42 75 66 20 6d 75 73 74 20 62 65 20 62 zBuf must be b
c29b0 69 67 20 65 6e 6f 75 67 68 20 74 6f 0a 2a 2a 20 ig enough to.**
c29c0 68 6f 6c 64 20 61 74 20 70 56 66 73 2d 3e 6d 78 hold at pVfs->mx
c29d0 50 61 74 68 6e 61 6d 65 20 63 68 61 72 61 63 74 Pathname charact
c29e0 65 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ers..*/.static i
c29f0 6e 74 20 67 65 74 54 65 6d 70 6e 61 6d 65 28 69 nt getTempname(i
c2a00 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a nt nBuf, char *z
c2a10 42 75 66 20 29 7b 0a 20 20 73 74 61 74 69 63 20 Buf ){. static
c2a20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
c2a30 68 61 72 20 7a 43 68 61 72 73 5b 5d 20 3d 0a 20 har zChars[] =.
c2a40 20 20 20 22 61 62 63 64 65 66 67 68 69 6a 6b 6c "abcdefghijkl
c2a50 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a 22 0a mnopqrstuvwxyz".
c2a60 20 20 20 20 22 41 42 43 44 45 46 47 48 49 4a 4b "ABCDEFGHIJK
c2a70 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a 22 LMNOPQRSTUVWXYZ"
c2a80 0a 20 20 20 20 22 30 31 32 33 34 35 36 37 38 39 . "0123456789
c2a90 22 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 ";. int i, j;.
c2aa0 20 63 68 61 72 20 7a 54 65 6d 70 50 61 74 68 42 char zTempPathB
c2ab0 75 66 5b 33 5d 3b 0a 20 20 50 53 5a 20 7a 54 65 uf[3];. PSZ zTe
c2ac0 6d 70 50 61 74 68 20 3d 20 28 50 53 5a 29 26 7a mpPath = (PSZ)&z
c2ad0 54 65 6d 70 50 61 74 68 42 75 66 3b 0a 20 20 69 TempPathBuf;. i
c2ae0 66 28 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f f( sqlite3_temp_
c2af0 64 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 directory ){.
c2b00 20 7a 54 65 6d 70 50 61 74 68 20 3d 20 73 71 6c zTempPath = sql
c2b10 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 ite3_temp_direct
c2b20 6f 72 79 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ory;. }else{.
c2b30 20 20 69 66 28 20 44 6f 73 53 63 61 6e 45 6e 76 if( DosScanEnv
c2b40 28 20 28 50 53 5a 29 22 54 45 4d 50 22 2c 20 26 ( (PSZ)"TEMP", &
c2b50 7a 54 65 6d 70 50 61 74 68 20 29 20 29 7b 0a 20 zTempPath ) ){.
c2b60 20 20 20 20 20 69 66 28 20 44 6f 73 53 63 61 6e if( DosScan
c2b70 45 6e 76 28 20 28 50 53 5a 29 22 54 4d 50 22 2c Env( (PSZ)"TMP",
c2b80 20 26 7a 54 65 6d 70 50 61 74 68 20 29 20 29 7b &zTempPath ) ){
c2b90 0a 20 20 20 20 20 20 20 20 69 66 28 20 44 6f 73 . if( Dos
c2ba0 53 63 61 6e 45 6e 76 28 20 28 50 53 5a 29 22 54 ScanEnv( (PSZ)"T
c2bb0 4d 50 44 49 52 22 2c 20 26 7a 54 65 6d 70 50 61 MPDIR", &zTempPa
c2bc0 74 68 20 29 20 29 7b 0a 20 20 20 20 20 20 20 20 th ) ){.
c2bd0 20 20 20 55 4c 4f 4e 47 20 75 6c 44 72 69 76 65 ULONG ulDrive
c2be0 4e 75 6d 20 3d 20 30 2c 20 75 6c 44 72 69 76 65 Num = 0, ulDrive
c2bf0 4d 61 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 Map = 0;.
c2c00 20 20 20 20 44 6f 73 51 75 65 72 79 43 75 72 72 DosQueryCurr
c2c10 65 6e 74 44 69 73 6b 28 20 26 75 6c 44 72 69 76 entDisk( &ulDriv
c2c20 65 4e 75 6d 2c 20 26 75 6c 44 72 69 76 65 4d 61 eNum, &ulDriveMa
c2c30 70 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 p );.
c2c40 73 70 72 69 6e 74 66 28 20 28 63 68 61 72 2a 29 sprintf( (char*)
c2c50 7a 54 65 6d 70 50 61 74 68 2c 20 22 25 63 3a 22 zTempPath, "%c:"
c2c60 2c 20 28 63 68 61 72 29 28 20 27 41 27 20 2b 20 , (char)( 'A' +
c2c70 75 6c 44 72 69 76 65 4e 75 6d 20 2d 20 31 20 29 ulDriveNum - 1 )
c2c80 20 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 );. }.
c2c90 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
c2ca0 20 20 2f 2a 20 53 74 72 69 70 20 6f 66 66 20 61 /* Strip off a
c2cb0 20 74 72 61 69 6c 69 6e 67 20 73 6c 61 73 68 65 trailing slashe
c2cc0 73 20 6f 72 20 62 61 63 6b 73 6c 61 73 68 65 73 s or backslashes
c2cd0 2c 20 6f 74 68 65 72 77 69 73 65 20 77 65 20 77 , otherwise we w
c2ce0 6f 75 6c 64 20 67 65 74 20 2a 0a 20 20 20 2a 20 ould get *. *
c2cf0 6d 75 6c 74 69 70 6c 65 20 28 62 61 63 6b 29 73 multiple (back)s
c2d00 6c 61 73 68 65 73 20 77 68 69 63 68 20 63 61 75 lashes which cau
c2d10 73 65 73 20 44 6f 73 4f 70 65 6e 28 29 20 74 6f ses DosOpen() to
c2d20 20 66 61 69 6c 2e 20 20 20 20 20 20 20 20 20 20 fail.
c2d30 20 20 20 20 2a 0a 20 20 20 2a 20 54 72 61 69 6c *. * Trail
c2d40 69 6e 67 20 73 70 61 63 65 73 20 61 72 65 20 6e ing spaces are n
c2d50 6f 74 20 61 6c 6c 6f 77 65 64 2c 20 65 69 74 68 ot allowed, eith
c2d60 65 72 2e 20 20 20 20 20 20 20 20 20 20 20 20 20 er.
c2d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a *
c2d80 2f 0a 20 20 6a 20 3d 20 73 71 6c 69 74 65 33 53 /. j = sqlite3S
c2d90 74 72 6c 65 6e 33 30 28 7a 54 65 6d 70 50 61 74 trlen30(zTempPat
c2da0 68 29 3b 0a 20 20 77 68 69 6c 65 28 20 6a 20 3e h);. while( j >
c2db0 20 30 20 26 26 20 28 20 7a 54 65 6d 70 50 61 74 0 && ( zTempPat
c2dc0 68 5b 6a 2d 31 5d 20 3d 3d 20 27 5c 5c 27 20 7c h[j-1] == '\\' |
c2dd0 7c 20 7a 54 65 6d 70 50 61 74 68 5b 6a 2d 31 5d | zTempPath[j-1]
c2de0 20 3d 3d 20 27 2f 27 0a 20 20 20 20 20 20 20 20 == '/'.
c2df0 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 7a || z
c2e00 54 65 6d 70 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d TempPath[j-1] ==
c2e10 20 27 20 27 20 29 20 29 7b 0a 20 20 20 20 6a 2d ' ' ) ){. j-
c2e20 2d 3b 0a 20 20 7d 0a 20 20 7a 54 65 6d 70 50 61 -;. }. zTempPa
c2e30 74 68 5b 6a 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 th[j] = '\0';.
c2e40 69 66 28 20 21 73 71 6c 69 74 65 33 5f 74 65 6d if( !sqlite3_tem
c2e50 70 5f 64 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 p_directory ){.
c2e60 20 20 20 63 68 61 72 20 2a 7a 54 65 6d 70 50 61 char *zTempPa
c2e70 74 68 55 54 46 20 3d 20 63 6f 6e 76 65 72 74 43 thUTF = convertC
c2e80 70 50 61 74 68 54 6f 55 74 66 38 28 20 7a 54 65 pPathToUtf8( zTe
c2e90 6d 70 50 61 74 68 20 29 3b 0a 20 20 20 20 73 71 mpPath );. sq
c2ea0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 20 lite3_snprintf(
c2eb0 6e 42 75 66 2d 33 30 2c 20 7a 42 75 66 2c 0a 20 nBuf-30, zBuf,.
c2ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c2ed0 20 20 20 20 20 22 25 73 5c 5c 22 53 51 4c 49 54 "%s\\"SQLIT
c2ee0 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 E_TEMP_FILE_PREF
c2ef0 49 58 2c 20 7a 54 65 6d 70 50 61 74 68 55 54 46 IX, zTempPathUTF
c2f00 20 29 3b 0a 20 20 20 20 66 72 65 65 28 20 7a 54 );. free( zT
c2f10 65 6d 70 50 61 74 68 55 54 46 20 29 3b 0a 20 20 empPathUTF );.
c2f20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 }else{. sqlit
c2f30 65 33 5f 73 6e 70 72 69 6e 74 66 28 20 6e 42 75 e3_snprintf( nBu
c2f40 66 2d 33 30 2c 20 7a 42 75 66 2c 0a 20 20 20 20 f-30, zBuf,.
c2f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c2f60 20 20 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54 "%s\\"SQLITE_T
c2f70 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c EMP_FILE_PREFIX,
c2f80 20 7a 54 65 6d 70 50 61 74 68 20 29 3b 0a 20 20 zTempPath );.
c2f90 7d 0a 20 20 6a 20 3d 20 73 71 6c 69 74 65 33 53 }. j = sqlite3S
c2fa0 74 72 6c 65 6e 33 30 28 20 7a 42 75 66 20 29 3b trlen30( zBuf );
c2fb0 0a 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f . sqlite3_rando
c2fc0 6d 6e 65 73 73 28 20 32 30 2c 20 26 7a 42 75 66 mness( 20, &zBuf
c2fd0 5b 6a 5d 20 29 3b 0a 20 20 66 6f 72 28 20 69 20 [j] );. for( i
c2fe0 3d 20 30 3b 20 69 20 3c 20 32 30 3b 20 69 2b 2b = 0; i < 20; i++
c2ff0 2c 20 6a 2b 2b 20 29 7b 0a 20 20 20 20 7a 42 75 , j++ ){. zBu
c3000 66 5b 6a 5d 20 3d 20 28 63 68 61 72 29 7a 43 68 f[j] = (char)zCh
c3010 61 72 73 5b 20 28 28 75 6e 73 69 67 6e 65 64 20 ars[ ((unsigned
c3020 63 68 61 72 29 7a 42 75 66 5b 6a 5d 29 25 28 73 char)zBuf[j])%(s
c3030 69 7a 65 6f 66 28 7a 43 68 61 72 73 29 2d 31 29 izeof(zChars)-1)
c3040 20 5d 3b 0a 20 20 7d 0a 20 20 7a 42 75 66 5b 6a ];. }. zBuf[j
c3050 5d 20 3d 20 30 3b 0a 20 20 4f 53 54 52 41 43 45 ] = 0;. OSTRACE
c3060 32 28 20 22 54 45 4d 50 20 46 49 4c 45 4e 41 4d 2( "TEMP FILENAM
c3070 45 3a 20 25 73 5c 6e 22 2c 20 7a 42 75 66 20 29 E: %s\n", zBuf )
c3080 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
c3090 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 E_OK;.}.../*.**
c30a0 54 75 72 6e 20 61 20 72 65 6c 61 74 69 76 65 20 Turn a relative
c30b0 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20 pathname into a
c30c0 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 2e 20 20 full pathname.
c30d0 57 72 69 74 65 20 74 68 65 20 66 75 6c 6c 0a 2a Write the full.*
c30e0 2a 20 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 * pathname into
c30f0 7a 46 75 6c 6c 5b 5d 2e 20 20 7a 46 75 6c 6c 5b zFull[]. zFull[
c3100 5d 20 77 69 6c 6c 20 62 65 20 61 74 20 6c 65 61 ] will be at lea
c3110 73 74 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e st pVfs->mxPathn
c3120 61 6d 65 0a 2a 2a 20 62 79 74 65 73 20 69 6e 20 ame.** bytes in
c3130 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 size..*/.static
c3140 69 6e 74 20 6f 73 32 46 75 6c 6c 50 61 74 68 6e int os2FullPathn
c3150 61 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 ame(. sqlite3_v
c3160 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 fs *pVfs,
c3170 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
c3180 20 76 66 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 vfs object */.
c3190 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 65 const char *zRe
c31a0 6c 61 74 69 76 65 2c 20 20 20 20 20 20 2f 2a 20 lative, /*
c31b0 50 6f 73 73 69 62 6c 79 20 72 65 6c 61 74 69 76 Possibly relativ
c31c0 65 20 69 6e 70 75 74 20 70 61 74 68 20 2a 2f 0a e input path */.
c31d0 20 20 69 6e 74 20 6e 46 75 6c 6c 2c 20 20 20 20 int nFull,
c31e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
c31f0 20 53 69 7a 65 20 6f 66 20 6f 75 74 70 75 74 20 Size of output
c3200 62 75 66 66 65 72 20 69 6e 20 62 79 74 65 73 20 buffer in bytes
c3210 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c */. char *zFull
c3220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3230 20 2f 2a 20 4f 75 74 70 75 74 20 62 75 66 66 65 /* Output buffe
c3240 72 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a r */.){. char *
c3250 7a 52 65 6c 61 74 69 76 65 43 70 20 3d 20 63 6f zRelativeCp = co
c3260 6e 76 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 nvertUtf8PathToC
c3270 70 28 20 7a 52 65 6c 61 74 69 76 65 20 29 3b 0a p( zRelative );.
c3280 20 20 63 68 61 72 20 7a 46 75 6c 6c 43 70 5b 43 char zFullCp[C
c3290 43 48 4d 41 58 50 41 54 48 5d 20 3d 20 22 5c 30 CHMAXPATH] = "\0
c32a0 22 3b 0a 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c ";. char *zFull
c32b0 55 54 46 3b 0a 20 20 41 50 49 52 45 54 20 72 63 UTF;. APIRET rc
c32c0 20 3d 20 44 6f 73 51 75 65 72 79 50 61 74 68 49 = DosQueryPathI
c32d0 6e 66 6f 28 20 7a 52 65 6c 61 74 69 76 65 43 70 nfo( zRelativeCp
c32e0 2c 20 46 49 4c 5f 51 55 45 52 59 46 55 4c 4c 4e , FIL_QUERYFULLN
c32f0 41 4d 45 2c 20 7a 46 75 6c 6c 43 70 2c 0a 20 20 AME, zFullCp,.
c3300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 43 43 CC
c3320 48 4d 41 58 50 41 54 48 20 29 3b 0a 20 20 66 72 HMAXPATH );. fr
c3330 65 65 28 20 7a 52 65 6c 61 74 69 76 65 43 70 20 ee( zRelativeCp
c3340 29 3b 0a 20 20 7a 46 75 6c 6c 55 54 46 20 3d 20 );. zFullUTF =
c3350 63 6f 6e 76 65 72 74 43 70 50 61 74 68 54 6f 55 convertCpPathToU
c3360 74 66 38 28 20 7a 46 75 6c 6c 43 70 20 29 3b 0a tf8( zFullCp );.
c3370 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
c3380 74 66 28 20 6e 46 75 6c 6c 2c 20 7a 46 75 6c 6c tf( nFull, zFull
c3390 2c 20 7a 46 75 6c 6c 55 54 46 20 29 3b 0a 20 20 , zFullUTF );.
c33a0 66 72 65 65 28 20 7a 46 75 6c 6c 55 54 46 20 29 free( zFullUTF )
c33b0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 20 3d 3d ;. return rc ==
c33c0 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 NO_ERROR ? SQLI
c33d0 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 TE_OK : SQLITE_I
c33e0 4f 45 52 52 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 OERR;.}.../*.**
c33f0 4f 70 65 6e 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a Open a file..*/.
c3400 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4f 70 static int os2Op
c3410 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 en(. sqlite3_vf
c3420 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 s *pVfs,
c3430 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 /* Not used
c3440 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
c3450 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 *zName,
c3460 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 /* Name of th
c3470 65 20 66 69 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 e file */. sqli
c3480 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 te3_file *id,
c3490 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 /* Wri
c34a0 74 65 20 74 68 65 20 53 51 4c 69 74 65 20 66 69 te the SQLite fi
c34b0 6c 65 20 68 61 6e 64 6c 65 20 68 65 72 65 20 2a le handle here *
c34c0 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 /. int flags,
c34d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c34e0 20 20 2f 2a 20 4f 70 65 6e 20 6d 6f 64 65 20 66 /* Open mode f
c34f0 6c 61 67 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 lags */. int *p
c3500 4f 75 74 46 6c 61 67 73 20 20 20 20 20 20 20 20 OutFlags
c3510 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 75 /* Statu
c3520 73 20 72 65 74 75 72 6e 20 66 6c 61 67 73 20 2a s return flags *
c3530 2f 0a 29 7b 0a 20 20 48 46 49 4c 45 20 68 3b 0a /.){. HFILE h;.
c3540 20 20 55 4c 4f 4e 47 20 75 6c 46 69 6c 65 41 74 ULONG ulFileAt
c3550 74 72 69 62 75 74 65 20 3d 20 46 49 4c 45 5f 4e tribute = FILE_N
c3560 4f 52 4d 41 4c 3b 0a 20 20 55 4c 4f 4e 47 20 75 ORMAL;. ULONG u
c3570 6c 4f 70 65 6e 46 6c 61 67 73 20 3d 20 30 3b 0a lOpenFlags = 0;.
c3580 20 20 55 4c 4f 4e 47 20 75 6c 4f 70 65 6e 4d 6f ULONG ulOpenMo
c3590 64 65 20 3d 20 30 3b 0a 20 20 6f 73 32 46 69 6c de = 0;. os2Fil
c35a0 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 e *pFile = (os2F
c35b0 69 6c 65 2a 29 69 64 3b 0a 20 20 41 50 49 52 45 ile*)id;. APIRE
c35c0 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b T rc = NO_ERROR;
c35d0 0a 20 20 55 4c 4f 4e 47 20 75 6c 41 63 74 69 6f . ULONG ulActio
c35e0 6e 3b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 n;. char *zName
c35f0 43 70 3b 0a 20 20 63 68 61 72 20 7a 54 6d 70 6e Cp;. char zTmpn
c3600 61 6d 65 5b 43 43 48 4d 41 58 50 41 54 48 2b 31 ame[CCHMAXPATH+1
c3610 5d 3b 20 20 20 20 2f 2a 20 42 75 66 66 65 72 20 ]; /* Buffer
c3620 74 6f 20 68 6f 6c 64 20 6e 61 6d 65 20 6f 66 20 to hold name of
c3630 74 65 6d 70 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 temp file */..
c3640 2f 2a 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64 /* If the second
c3650 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 argument to thi
c3660 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 4e 55 s function is NU
c3670 4c 4c 2c 20 67 65 6e 65 72 61 74 65 20 61 20 0a LL, generate a .
c3680 20 20 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 66 ** temporary f
c3690 69 6c 65 20 6e 61 6d 65 20 74 6f 20 75 73 65 20 ile name to use
c36a0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 7a 4e 61 . */. if( !zNa
c36b0 6d 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 me ){. int rc
c36c0 20 3d 20 67 65 74 54 65 6d 70 6e 61 6d 65 28 43 = getTempname(C
c36d0 43 48 4d 41 58 50 41 54 48 2b 31 2c 20 7a 54 6d CHMAXPATH+1, zTm
c36e0 70 6e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 pname);. if(
c36f0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
c3700 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
c3710 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 4e 61 6d ;. }. zNam
c3720 65 20 3d 20 7a 54 6d 70 6e 61 6d 65 3b 0a 20 20 e = zTmpname;.
c3730 7d 0a 0a 0a 20 20 6d 65 6d 73 65 74 28 20 70 46 }... memset( pF
c3740 69 6c 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a ile, 0, sizeof(*
c3750 70 46 69 6c 65 29 20 29 3b 0a 0a 20 20 4f 53 54 pFile) );.. OST
c3760 52 41 43 45 32 28 20 22 4f 50 45 4e 20 77 61 6e RACE2( "OPEN wan
c3770 74 20 25 64 5c 6e 22 2c 20 66 6c 61 67 73 20 29 t %d\n", flags )
c3780 3b 0a 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 ;.. if( flags &
c3790 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 SQLITE_OPEN_REA
c37a0 44 57 52 49 54 45 20 29 7b 0a 20 20 20 20 75 6c DWRITE ){. ul
c37b0 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e OpenMode |= OPEN
c37c0 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 54 _ACCESS_READWRIT
c37d0 45 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 E;. OSTRACE1(
c37e0 20 22 4f 50 45 4e 20 72 65 61 64 2f 77 72 69 74 "OPEN read/writ
c37f0 65 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b e\n" );. }else{
c3800 0a 20 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 . ulOpenMode
c3810 7c 3d 20 4f 50 45 4e 5f 41 43 43 45 53 53 5f 52 |= OPEN_ACCESS_R
c3820 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 4f 53 54 EADONLY;. OST
c3830 52 41 43 45 31 28 20 22 4f 50 45 4e 20 72 65 61 RACE1( "OPEN rea
c3840 64 20 6f 6e 6c 79 5c 6e 22 20 29 3b 0a 20 20 7d d only\n" );. }
c3850 0a 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 .. if( flags &
c3860 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 SQLITE_OPEN_CREA
c3870 54 45 20 29 7b 0a 20 20 20 20 75 6c 4f 70 65 6e TE ){. ulOpen
c3880 46 6c 61 67 73 20 7c 3d 20 4f 50 45 4e 5f 41 43 Flags |= OPEN_AC
c3890 54 49 4f 4e 5f 4f 50 45 4e 5f 49 46 5f 45 58 49 TION_OPEN_IF_EXI
c38a0 53 54 53 20 7c 20 4f 50 45 4e 5f 41 43 54 49 4f STS | OPEN_ACTIO
c38b0 4e 5f 43 52 45 41 54 45 5f 49 46 5f 4e 45 57 3b N_CREATE_IF_NEW;
c38c0 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 . OSTRACE1( "
c38d0 4f 50 45 4e 20 6f 70 65 6e 20 6e 65 77 2f 63 72 OPEN open new/cr
c38e0 65 61 74 65 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c eate\n" );. }el
c38f0 73 65 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 46 6c se{. ulOpenFl
c3900 61 67 73 20 7c 3d 20 4f 50 45 4e 5f 41 43 54 49 ags |= OPEN_ACTI
c3910 4f 4e 5f 4f 50 45 4e 5f 49 46 5f 45 58 49 53 54 ON_OPEN_IF_EXIST
c3920 53 20 7c 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f S | OPEN_ACTION_
c3930 46 41 49 4c 5f 49 46 5f 4e 45 57 3b 0a 20 20 20 FAIL_IF_NEW;.
c3940 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e OSTRACE1( "OPEN
c3950 20 6f 70 65 6e 20 65 78 69 73 74 69 6e 67 5c 6e open existing\n
c3960 22 20 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 " );. }.. if(
c3970 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f flags & SQLITE_O
c3980 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 29 7b 0a 20 PEN_MAIN_DB ){.
c3990 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d ulOpenMode |=
c39a0 20 4f 50 45 4e 5f 53 48 41 52 45 5f 44 45 4e 59 OPEN_SHARE_DENY
c39b0 4e 4f 4e 45 3b 0a 20 20 20 20 4f 53 54 52 41 43 NONE;. OSTRAC
c39c0 45 31 28 20 22 4f 50 45 4e 20 73 68 61 72 65 20 E1( "OPEN share
c39d0 72 65 61 64 2f 77 72 69 74 65 5c 6e 22 20 29 3b read/write\n" );
c39e0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6c . }else{. ul
c39f0 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e OpenMode |= OPEN
c3a00 5f 53 48 41 52 45 5f 44 45 4e 59 57 52 49 54 45 _SHARE_DENYWRITE
c3a10 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 ;. OSTRACE1(
c3a20 22 4f 50 45 4e 20 73 68 61 72 65 20 72 65 61 64 "OPEN share read
c3a30 20 6f 6e 6c 79 5c 6e 22 20 29 3b 0a 20 20 7d 0a only\n" );. }.
c3a40 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 . if( flags & S
c3a50 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 QLITE_OPEN_DELET
c3a60 45 4f 4e 43 4c 4f 53 45 20 29 7b 0a 20 20 20 20 EONCLOSE ){.
c3a70 63 68 61 72 20 70 61 74 68 55 74 66 38 5b 43 43 char pathUtf8[CC
c3a80 48 4d 41 58 50 41 54 48 5d 3b 0a 23 69 66 64 65 HMAXPATH];.#ifde
c3a90 66 20 4e 44 45 42 55 47 20 2f 2a 20 77 68 65 6e f NDEBUG /* when
c3aa0 20 64 65 62 75 67 67 69 6e 67 20 77 65 20 77 61 debugging we wa
c3ab0 6e 74 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 nt to make sure
c3ac0 69 74 20 69 73 20 64 65 6c 65 74 65 64 20 2a 2f it is deleted */
c3ad0 0a 20 20 20 20 75 6c 46 69 6c 65 41 74 74 72 69 . ulFileAttri
c3ae0 62 75 74 65 20 3d 20 46 49 4c 45 5f 48 49 44 44 bute = FILE_HIDD
c3af0 45 4e 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 6f EN;.#endif. o
c3b00 73 32 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 20 s2FullPathname(
c3b10 70 56 66 73 2c 20 7a 4e 61 6d 65 2c 20 43 43 48 pVfs, zName, CCH
c3b20 4d 41 58 50 41 54 48 2c 20 70 61 74 68 55 74 66 MAXPATH, pathUtf
c3b30 38 20 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 8 );. pFile->
c3b40 70 61 74 68 54 6f 44 65 6c 20 3d 20 63 6f 6e 76 pathToDel = conv
c3b50 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 ertUtf8PathToCp(
c3b60 20 70 61 74 68 55 74 66 38 20 29 3b 0a 20 20 20 pathUtf8 );.
c3b70 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e OSTRACE1( "OPEN
c3b80 20 68 69 64 64 65 6e 2f 64 65 6c 65 74 65 20 6f hidden/delete o
c3b90 6e 20 63 6c 6f 73 65 20 66 69 6c 65 20 61 74 74 n close file att
c3ba0 72 69 62 75 74 65 73 5c 6e 22 20 29 3b 0a 20 20 ributes\n" );.
c3bb0 7d 65 6c 73 65 7b 0a 20 20 20 20 70 46 69 6c 65 }else{. pFile
c3bc0 2d 3e 70 61 74 68 54 6f 44 65 6c 20 3d 20 4e 55 ->pathToDel = NU
c3bd0 4c 4c 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 31 LL;. OSTRACE1
c3be0 28 20 22 4f 50 45 4e 20 6e 6f 72 6d 61 6c 20 66 ( "OPEN normal f
c3bf0 69 6c 65 20 61 74 74 72 69 62 75 74 65 5c 6e 22 ile attribute\n"
c3c00 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 61 6c );. }.. /* al
c3c10 77 61 79 73 20 6f 70 65 6e 20 69 6e 20 72 61 6e ways open in ran
c3c20 64 6f 6d 20 61 63 63 65 73 73 20 6d 6f 64 65 20 dom access mode
c3c30 66 6f 72 20 70 6f 73 73 69 62 6c 79 20 62 65 74 for possibly bet
c3c40 74 65 72 20 73 70 65 65 64 20 2a 2f 0a 20 20 75 ter speed */. u
c3c50 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 lOpenMode |= OPE
c3c60 4e 5f 46 4c 41 47 53 5f 52 41 4e 44 4f 4d 3b 0a N_FLAGS_RANDOM;.
c3c70 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 ulOpenMode |=
c3c80 4f 50 45 4e 5f 46 4c 41 47 53 5f 46 41 49 4c 5f OPEN_FLAGS_FAIL_
c3c90 4f 4e 5f 45 52 52 4f 52 3b 0a 20 20 75 6c 4f 70 ON_ERROR;. ulOp
c3ca0 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 46 enMode |= OPEN_F
c3cb0 4c 41 47 53 5f 4e 4f 49 4e 48 45 52 49 54 3b 0a LAGS_NOINHERIT;.
c3cc0 0a 20 20 7a 4e 61 6d 65 43 70 20 3d 20 63 6f 6e . zNameCp = con
c3cd0 76 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 vertUtf8PathToCp
c3ce0 28 20 7a 4e 61 6d 65 20 29 3b 0a 20 20 72 63 20 ( zName );. rc
c3cf0 3d 20 44 6f 73 4f 70 65 6e 28 20 28 50 53 5a 29 = DosOpen( (PSZ)
c3d00 7a 4e 61 6d 65 43 70 2c 0a 20 20 20 20 20 20 20 zNameCp,.
c3d10 20 20 20 20 20 20 20 20 20 26 68 2c 0a 20 20 20 &h,.
c3d20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 75 6c &ul
c3d30 41 63 74 69 6f 6e 2c 0a 20 20 20 20 20 20 20 20 Action,.
c3d40 20 20 20 20 20 20 20 20 30 4c 2c 0a 20 20 20 20 0L,.
c3d50 20 20 20 20 20 20 20 20 20 20 20 20 75 6c 46 69 ulFi
c3d60 6c 65 41 74 74 72 69 62 75 74 65 2c 0a 20 20 20 leAttribute,.
c3d70 20 20 20 20 20 20 20 20 20 20 20 20 20 75 6c 4f ulO
c3d80 70 65 6e 46 6c 61 67 73 2c 0a 20 20 20 20 20 20 penFlags,.
c3d90 20 20 20 20 20 20 20 20 20 20 75 6c 4f 70 65 6e ulOpen
c3da0 4d 6f 64 65 2c 0a 20 20 20 20 20 20 20 20 20 20 Mode,.
c3db0 20 20 20 20 20 20 28 50 45 41 4f 50 32 29 4e 55 (PEAOP2)NU
c3dc0 4c 4c 20 29 3b 0a 20 20 66 72 65 65 28 20 7a 4e LL );. free( zN
c3dd0 61 6d 65 43 70 20 29 3b 0a 20 20 69 66 28 20 72 ameCp );. if( r
c3de0 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b c != NO_ERROR ){
c3df0 0a 20 20 20 20 4f 53 54 52 41 43 45 37 28 20 22 . OSTRACE7( "
c3e00 4f 50 45 4e 20 49 6e 76 61 6c 69 64 20 68 61 6e OPEN Invalid han
c3e10 64 6c 65 20 72 63 3d 25 64 3a 20 7a 4e 61 6d 65 dle rc=%d: zName
c3e20 3d 25 73 2c 20 75 6c 41 63 74 69 6f 6e 3d 25 23 =%s, ulAction=%#
c3e30 6c 78 2c 20 75 6c 41 74 74 72 3d 25 23 6c 78 2c lx, ulAttr=%#lx,
c3e40 20 75 6c 46 6c 61 67 73 3d 25 23 6c 78 2c 20 75 ulFlags=%#lx, u
c3e50 6c 4d 6f 64 65 3d 25 23 6c 78 5c 6e 22 2c 0a 20 lMode=%#lx\n",.
c3e60 20 20 20 20 20 20 20 20 20 20 20 20 20 72 63 2c rc,
c3e70 20 7a 4e 61 6d 65 2c 20 75 6c 41 63 74 69 6f 6e zName, ulAction
c3e80 2c 20 75 6c 46 69 6c 65 41 74 74 72 69 62 75 74 , ulFileAttribut
c3e90 65 2c 20 75 6c 4f 70 65 6e 46 6c 61 67 73 2c 20 e, ulOpenFlags,
c3ea0 75 6c 4f 70 65 6e 4d 6f 64 65 20 29 3b 0a 20 20 ulOpenMode );.
c3eb0 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 61 74 if( pFile->pat
c3ec0 68 54 6f 44 65 6c 20 29 0a 20 20 20 20 20 20 66 hToDel ). f
c3ed0 72 65 65 28 20 70 46 69 6c 65 2d 3e 70 61 74 68 ree( pFile->path
c3ee0 54 6f 44 65 6c 20 29 3b 0a 20 20 20 20 70 46 69 ToDel );. pFi
c3ef0 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 3d 20 le->pathToDel =
c3f00 4e 55 4c 4c 3b 0a 20 20 20 20 69 66 28 20 66 6c NULL;. if( fl
c3f10 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 ags & SQLITE_OPE
c3f20 4e 5f 52 45 41 44 57 52 49 54 45 20 29 7b 0a 20 N_READWRITE ){.
c3f30 20 20 20 20 20 4f 53 54 52 41 43 45 32 28 20 22 OSTRACE2( "
c3f40 4f 50 45 4e 20 25 64 20 49 6e 76 61 6c 69 64 20 OPEN %d Invalid
c3f50 68 61 6e 64 6c 65 5c 6e 22 2c 20 28 28 66 6c 61 handle\n", ((fla
c3f60 67 73 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e gs | SQLITE_OPEN
c3f70 5f 52 45 41 44 4f 4e 4c 59 29 20 26 20 7e 53 51 _READONLY) & ~SQ
c3f80 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 LITE_OPEN_READWR
c3f90 49 54 45 29 20 29 3b 0a 20 20 20 20 20 20 72 65 ITE) );. re
c3fa0 74 75 72 6e 20 6f 73 32 4f 70 65 6e 28 20 70 56 turn os2Open( pV
c3fb0 66 73 2c 20 7a 4e 61 6d 65 2c 20 69 64 2c 0a 20 fs, zName, id,.
c3fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3fd0 20 20 20 20 20 28 28 66 6c 61 67 73 20 7c 20 53 ((flags | S
c3fe0 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f QLITE_OPEN_READO
c3ff0 4e 4c 59 29 20 26 20 7e 53 51 4c 49 54 45 5f 4f NLY) & ~SQLITE_O
c4000 50 45 4e 5f 52 45 41 44 57 52 49 54 45 29 2c 0a PEN_READWRITE),.
c4010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c4020 20 20 20 20 20 20 70 4f 75 74 46 6c 61 67 73 20 pOutFlags
c4030 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
c4040 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
c4050 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 E_CANTOPEN;.
c4060 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 4f 75 }. }.. if( pOu
c4070 74 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 2a 70 tFlags ){. *p
c4080 4f 75 74 46 6c 61 67 73 20 3d 20 66 6c 61 67 73 OutFlags = flags
c4090 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 & SQLITE_OPEN_R
c40a0 45 41 44 57 52 49 54 45 20 3f 20 53 51 4c 49 54 EADWRITE ? SQLIT
c40b0 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 E_OPEN_READWRITE
c40c0 20 3a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 : SQLITE_OPEN_R
c40d0 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 0a 20 20 EADONLY;. }..
c40e0 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d pFile->pMethod =
c40f0 20 26 6f 73 32 49 6f 4d 65 74 68 6f 64 3b 0a 20 &os2IoMethod;.
c4100 20 70 46 69 6c 65 2d 3e 68 20 3d 20 68 3b 0a 20 pFile->h = h;.
c4110 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2b 31 29 OpenCounter(+1)
c4120 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22 4f ;. OSTRACE3( "O
c4130 50 45 4e 20 25 64 20 70 4f 75 74 46 6c 61 67 73 PEN %d pOutFlags
c4140 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 =%d\n", pFile->h
c4150 2c 20 70 4f 75 74 46 6c 61 67 73 20 29 3b 0a 20 , pOutFlags );.
c4160 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
c4170 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 K;.}../*.** Dele
c4180 74 65 20 74 68 65 20 6e 61 6d 65 64 20 66 69 6c te the named fil
c4190 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
c41a0 20 6f 73 32 44 65 6c 65 74 65 28 0a 20 20 73 71 os2Delete(. sq
c41b0 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
c41c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c41d0 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 /* Not used
c41e0 20 6f 6e 20 6f 73 32 20 2a 2f 0a 20 20 63 6f 6e on os2 */. con
c41f0 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 st char *zFilena
c4200 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 me,
c4210 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 /* Name of f
c4220 69 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f ile to delete */
c4230 0a 20 20 69 6e 74 20 73 79 6e 63 44 69 72 20 20 . int syncDir
c4240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c4250 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 /* Not
c4260 20 75 73 65 64 20 6f 6e 20 6f 73 32 20 2a 2f 0a used on os2 */.
c4270 29 7b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d ){. APIRET rc =
c4280 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 63 68 61 NO_ERROR;. cha
c4290 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 43 70 20 3d r *zFilenameCp =
c42a0 20 63 6f 6e 76 65 72 74 55 74 66 38 50 61 74 68 convertUtf8Path
c42b0 54 6f 43 70 28 20 7a 46 69 6c 65 6e 61 6d 65 20 ToCp( zFilename
c42c0 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 );. SimulateIOE
c42d0 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c rror( return SQL
c42e0 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 ITE_IOERR_DELETE
c42f0 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 44 65 );. rc = DosDe
c4300 6c 65 74 65 28 20 28 50 53 5a 29 7a 46 69 6c 65 lete( (PSZ)zFile
c4310 6e 61 6d 65 43 70 20 29 3b 0a 20 20 66 72 65 65 nameCp );. free
c4320 28 20 7a 46 69 6c 65 6e 61 6d 65 43 70 20 29 3b ( zFilenameCp );
c4330 0a 20 20 4f 53 54 52 41 43 45 32 28 20 22 44 45 . OSTRACE2( "DE
c4340 4c 45 54 45 20 5c 22 25 73 5c 22 5c 6e 22 2c 20 LETE \"%s\"\n",
c4350 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a 20 20 72 zFilename );. r
c4360 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 eturn rc == NO_E
c4370 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b RROR ? SQLITE_OK
c4380 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f : SQLITE_IOERR_
c4390 44 45 4c 45 54 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a DELETE;.}../*.**
c43a0 20 43 68 65 63 6b 20 74 68 65 20 65 78 69 73 74 Check the exist
c43b0 61 6e 63 65 20 61 6e 64 20 73 74 61 74 75 73 20 ance and status
c43c0 6f 66 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 of a file..*/.st
c43d0 61 74 69 63 20 69 6e 74 20 6f 73 32 41 63 63 65 atic int os2Acce
c43e0 73 73 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 ss(. sqlite3_vf
c43f0 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 s *pVfs,
c4400 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 6f /* Not used on o
c4410 73 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 s2 */. const ch
c4420 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 ar *zFilename,
c4430 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c /* Name of fil
c4440 65 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a 20 20 e to check */.
c4450 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 int flags,
c4460 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 79 70 /* Typ
c4470 65 20 6f 66 20 74 65 73 74 20 74 6f 20 6d 61 6b e of test to mak
c4480 65 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 20 2a e on this file *
c4490 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74 20 20 20 /. int *pOut
c44a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
c44b0 20 57 72 69 74 65 20 72 65 73 75 6c 74 73 20 68 Write results h
c44c0 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 46 49 4c 45 ere */.){. FILE
c44d0 53 54 41 54 55 53 33 20 66 73 74 73 33 43 6f 6e STATUS3 fsts3Con
c44e0 66 69 67 49 6e 66 6f 3b 0a 20 20 41 50 49 52 45 figInfo;. APIRE
c44f0 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b T rc = NO_ERROR;
c4500 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 . char *zFilena
c4510 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72 74 55 74 meCp = convertUt
c4520 66 38 50 61 74 68 54 6f 43 70 28 20 7a 46 69 6c f8PathToCp( zFil
c4530 65 6e 61 6d 65 20 29 3b 0a 0a 20 20 6d 65 6d 73 ename );.. mems
c4540 65 74 28 20 26 66 73 74 73 33 43 6f 6e 66 69 67 et( &fsts3Config
c4550 49 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 Info, 0, sizeof(
c4560 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 29 fsts3ConfigInfo)
c4570 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 51 75 );. rc = DosQu
c4580 65 72 79 50 61 74 68 49 6e 66 6f 28 20 28 50 53 eryPathInfo( (PS
c4590 5a 29 7a 46 69 6c 65 6e 61 6d 65 43 70 2c 20 46 Z)zFilenameCp, F
c45a0 49 4c 5f 53 54 41 4e 44 41 52 44 2c 0a 20 20 20 IL_STANDARD,.
c45b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c45c0 20 20 20 20 20 20 26 66 73 74 73 33 43 6f 6e 66 &fsts3Conf
c45d0 69 67 49 6e 66 6f 2c 20 73 69 7a 65 6f 66 28 46 igInfo, sizeof(F
c45e0 49 4c 45 53 54 41 54 55 53 33 29 20 29 3b 0a 20 ILESTATUS3) );.
c45f0 20 66 72 65 65 28 20 7a 46 69 6c 65 6e 61 6d 65 free( zFilename
c4600 43 70 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 Cp );. OSTRACE4
c4610 28 20 22 41 43 43 45 53 53 20 66 73 74 73 33 43 ( "ACCESS fsts3C
c4620 6f 6e 66 69 67 49 6e 66 6f 2e 61 74 74 72 46 69 onfigInfo.attrFi
c4630 6c 65 3d 25 64 20 66 6c 61 67 73 3d 25 64 20 72 le=%d flags=%d r
c4640 63 3d 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 c=%d\n",.
c4650 20 20 20 20 20 66 73 74 73 33 43 6f 6e 66 69 67 fsts3Config
c4660 49 6e 66 6f 2e 61 74 74 72 46 69 6c 65 2c 20 66 Info.attrFile, f
c4670 6c 61 67 73 2c 20 72 63 20 29 3b 0a 20 20 73 77 lags, rc );. sw
c4680 69 74 63 68 28 20 66 6c 61 67 73 20 29 7b 0a 20 itch( flags ){.
c4690 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 case SQLITE_A
c46a0 43 43 45 53 53 5f 52 45 41 44 3a 0a 20 20 20 20 CCESS_READ:.
c46b0 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 case SQLITE_ACCE
c46c0 53 53 5f 45 58 49 53 54 53 3a 0a 20 20 20 20 20 SS_EXISTS:.
c46d0 20 72 63 20 3d 20 28 72 63 20 3d 3d 20 4e 4f 5f rc = (rc == NO_
c46e0 45 52 52 4f 52 29 3b 0a 20 20 20 20 20 20 4f 53 ERROR);. OS
c46f0 54 52 41 43 45 33 28 20 22 41 43 43 45 53 53 20 TRACE3( "ACCESS
c4700 25 73 20 61 63 63 65 73 73 20 6f 66 20 72 65 61 %s access of rea
c4710 64 20 61 6e 64 20 65 78 69 73 74 73 20 20 72 63 d and exists rc
c4720 3d 25 64 5c 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d =%d\n", zFilenam
c4730 65 2c 20 72 63 20 29 3b 0a 20 20 20 20 20 20 62 e, rc );. b
c4740 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 reak;. case S
c4750 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 QLITE_ACCESS_REA
c4760 44 57 52 49 54 45 3a 0a 20 20 20 20 20 20 72 63 DWRITE:. rc
c4770 20 3d 20 28 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 = (rc == NO_ERR
c4780 4f 52 29 20 26 26 20 28 20 28 66 73 74 73 33 43 OR) && ( (fsts3C
c4790 6f 6e 66 69 67 49 6e 66 6f 2e 61 74 74 72 46 69 onfigInfo.attrFi
c47a0 6c 65 20 26 20 46 49 4c 45 5f 52 45 41 44 4f 4e le & FILE_READON
c47b0 4c 59 29 20 3d 3d 20 30 20 29 3b 0a 20 20 20 20 LY) == 0 );.
c47c0 20 20 4f 53 54 52 41 43 45 33 28 20 22 41 43 43 OSTRACE3( "ACC
c47d0 45 53 53 20 25 73 20 61 63 63 65 73 73 20 6f 66 ESS %s access of
c47e0 20 72 65 61 64 2f 77 72 69 74 65 20 20 72 63 3d read/write rc=
c47f0 25 64 5c 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 %d\n", zFilename
c4800 2c 20 72 63 20 29 3b 0a 20 20 20 20 20 20 62 72 , rc );. br
c4810 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 eak;. default
c4820 3a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 :. assert(
c4830 21 22 49 6e 76 61 6c 69 64 20 66 6c 61 67 73 20 !"Invalid flags
c4840 61 72 67 75 6d 65 6e 74 22 20 29 3b 0a 20 20 7d argument" );. }
c4850 0a 20 20 2a 70 4f 75 74 20 3d 20 72 63 3b 0a 20 . *pOut = rc;.
c4860 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
c4870 4b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 K;.}...#ifndef S
c4880 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f QLITE_OMIT_LOAD_
c4890 45 58 54 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 EXTENSION./*.**
c48a0 49 6e 74 65 72 66 61 63 65 73 20 66 6f 72 20 6f Interfaces for o
c48b0 70 65 6e 69 6e 67 20 61 20 73 68 61 72 65 64 20 pening a shared
c48c0 6c 69 62 72 61 72 79 2c 20 66 69 6e 64 69 6e 67 library, finding
c48d0 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a entry points.**
c48e0 20 77 69 74 68 69 6e 20 74 68 65 20 73 68 61 72 within the shar
c48f0 65 64 20 6c 69 62 72 61 72 79 2c 20 61 6e 64 20 ed library, and
c4900 63 6c 6f 73 69 6e 67 20 74 68 65 20 73 68 61 72 closing the shar
c4910 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 2f ed library..*/./
c4920 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 73 20 *.** Interfaces
c4930 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20 73 68 for opening a sh
c4940 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 66 69 ared library, fi
c4950 6e 64 69 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e nding entry poin
c4960 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 ts.** within the
c4970 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c shared library,
c4980 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68 65 and closing the
c4990 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2e shared library.
c49a0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
c49b0 2a 6f 73 32 44 6c 4f 70 65 6e 28 73 71 6c 69 74 *os2DlOpen(sqlit
c49c0 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 63 6f e3_vfs *pVfs, co
c49d0 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e nst char *zFilen
c49e0 61 6d 65 29 7b 0a 20 20 55 43 48 41 52 20 6c 6f ame){. UCHAR lo
c49f0 61 64 45 72 72 5b 32 35 36 5d 3b 0a 20 20 48 4d adErr[256];. HM
c4a00 4f 44 55 4c 45 20 68 6d 6f 64 3b 0a 20 20 41 50 ODULE hmod;. AP
c4a10 49 52 45 54 20 72 63 3b 0a 20 20 63 68 61 72 20 IRET rc;. char
c4a20 2a 7a 46 69 6c 65 6e 61 6d 65 43 70 20 3d 20 63 *zFilenameCp = c
c4a30 6f 6e 76 65 72 74 55 74 66 38 50 61 74 68 54 6f onvertUtf8PathTo
c4a40 43 70 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 Cp(zFilename);.
c4a50 20 72 63 20 3d 20 44 6f 73 4c 6f 61 64 4d 6f 64 rc = DosLoadMod
c4a60 75 6c 65 28 28 50 53 5a 29 6c 6f 61 64 45 72 72 ule((PSZ)loadErr
c4a70 2c 20 73 69 7a 65 6f 66 28 6c 6f 61 64 45 72 72 , sizeof(loadErr
c4a80 29 2c 20 7a 46 69 6c 65 6e 61 6d 65 43 70 2c 20 ), zFilenameCp,
c4a90 26 68 6d 6f 64 29 3b 0a 20 20 66 72 65 65 28 7a &hmod);. free(z
c4aa0 46 69 6c 65 6e 61 6d 65 43 70 29 3b 0a 20 20 72 FilenameCp);. r
c4ab0 65 74 75 72 6e 20 72 63 20 21 3d 20 4e 4f 5f 45 eturn rc != NO_E
c4ac0 52 52 4f 52 20 3f 20 30 20 3a 20 28 76 6f 69 64 RROR ? 0 : (void
c4ad0 2a 29 68 6d 6f 64 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 *)hmod;.}./*.**
c4ae0 41 20 6e 6f 2d 6f 70 20 73 69 6e 63 65 20 74 68 A no-op since th
c4af0 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 e error code is
c4b00 72 65 74 75 72 6e 65 64 20 6f 6e 20 74 68 65 20 returned on the
c4b10 44 6f 73 4c 6f 61 64 4d 6f 64 75 6c 65 20 63 61 DosLoadModule ca
c4b20 6c 6c 2e 0a 2a 2a 20 6f 73 32 44 6c 6f 70 65 6e ll..** os2Dlopen
c4b30 20 72 65 74 75 72 6e 73 20 7a 65 72 6f 20 69 66 returns zero if
c4b40 20 44 6f 73 4c 6f 61 64 4d 6f 64 75 6c 65 20 69 DosLoadModule i
c4b50 73 20 6e 6f 74 20 73 75 63 63 65 73 73 66 75 6c s not successful
c4b60 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
c4b70 20 6f 73 32 44 6c 45 72 72 6f 72 28 73 71 6c 69 os2DlError(sqli
c4b80 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 te3_vfs *pVfs, i
c4b90 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a nt nBuf, char *z
c4ba0 42 75 66 4f 75 74 29 7b 0a 2f 2a 20 6e 6f 2d 6f BufOut){./* no-o
c4bb0 70 20 2a 2f 0a 7d 0a 73 74 61 74 69 63 20 76 6f p */.}.static vo
c4bc0 69 64 20 2a 6f 73 32 44 6c 53 79 6d 28 73 71 6c id *os2DlSym(sql
c4bd0 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 ite3_vfs *pVfs,
c4be0 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65 2c 20 63 void *pHandle, c
c4bf0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 79 6d 62 onst char *zSymb
c4c00 6f 6c 29 7b 0a 20 20 50 46 4e 20 70 66 6e 3b 0a ol){. PFN pfn;.
c4c10 20 20 41 50 49 52 45 54 20 72 63 3b 0a 20 20 72 APIRET rc;. r
c4c20 63 20 3d 20 44 6f 73 51 75 65 72 79 50 72 6f 63 c = DosQueryProc
c4c30 41 64 64 72 28 28 48 4d 4f 44 55 4c 45 29 70 48 Addr((HMODULE)pH
c4c40 61 6e 64 6c 65 2c 20 30 4c 2c 20 7a 53 79 6d 62 andle, 0L, zSymb
c4c50 6f 6c 2c 20 26 70 66 6e 29 3b 0a 20 20 69 66 28 ol, &pfn);. if(
c4c60 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 rc != NO_ERROR
c4c70 29 7b 0a 20 20 20 20 2f 2a 20 69 66 20 74 68 65 ){. /* if the
c4c80 20 73 79 6d 62 6f 6c 20 69 74 73 65 6c 66 20 77 symbol itself w
c4c90 61 73 20 6e 6f 74 20 66 6f 75 6e 64 2c 20 73 65 as not found, se
c4ca0 61 72 63 68 20 61 67 61 69 6e 20 66 6f 72 20 74 arch again for t
c4cb0 68 65 20 73 61 6d 65 0a 20 20 20 20 20 2a 20 73 he same. * s
c4cc0 79 6d 62 6f 6c 20 77 69 74 68 20 61 6e 20 65 78 ymbol with an ex
c4cd0 74 72 61 20 75 6e 64 65 72 73 63 6f 72 65 2c 20 tra underscore,
c4ce0 74 68 61 74 20 6d 69 67 68 74 20 62 65 20 6e 65 that might be ne
c4cf0 65 64 65 64 20 64 65 70 65 6e 64 69 6e 67 0a 20 eded depending.
c4d00 20 20 20 20 2a 20 6f 6e 20 74 68 65 20 63 61 6c * on the cal
c4d10 6c 69 6e 67 20 63 6f 6e 76 65 6e 74 69 6f 6e 20 ling convention
c4d20 2a 2f 0a 20 20 20 20 63 68 61 72 20 5f 7a 53 79 */. char _zSy
c4d30 6d 62 6f 6c 5b 32 35 36 5d 20 3d 20 22 5f 22 3b mbol[256] = "_";
c4d40 0a 20 20 20 20 73 74 72 6e 63 61 74 28 5f 7a 53 . strncat(_zS
c4d50 79 6d 62 6f 6c 2c 20 7a 53 79 6d 62 6f 6c 2c 20 ymbol, zSymbol,
c4d60 32 35 35 29 3b 0a 20 20 20 20 72 63 20 3d 20 44 255);. rc = D
c4d70 6f 73 51 75 65 72 79 50 72 6f 63 41 64 64 72 28 osQueryProcAddr(
c4d80 28 48 4d 4f 44 55 4c 45 29 70 48 61 6e 64 6c 65 (HMODULE)pHandle
c4d90 2c 20 30 4c 2c 20 5f 7a 53 79 6d 62 6f 6c 2c 20 , 0L, _zSymbol,
c4da0 26 70 66 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 &pfn);. }. ret
c4db0 75 72 6e 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 urn rc != NO_ERR
c4dc0 4f 52 20 3f 20 30 20 3a 20 28 76 6f 69 64 2a 29 OR ? 0 : (void*)
c4dd0 70 66 6e 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f pfn;.}.static vo
c4de0 69 64 20 6f 73 32 44 6c 43 6c 6f 73 65 28 73 71 id os2DlClose(sq
c4df0 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
c4e00 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65 29 7b void *pHandle){
c4e10 0a 20 20 44 6f 73 46 72 65 65 4d 6f 64 75 6c 65 . DosFreeModule
c4e20 28 28 48 4d 4f 44 55 4c 45 29 70 48 61 6e 64 6c ((HMODULE)pHandl
c4e30 65 29 3b 0a 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 e);.}.#else /* i
c4e40 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f f SQLITE_OMIT_LO
c4e50 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20 69 73 20 AD_EXTENSION is
c4e60 64 65 66 69 6e 65 64 3a 20 2a 2f 0a 20 20 23 64 defined: */. #d
c4e70 65 66 69 6e 65 20 6f 73 32 44 6c 4f 70 65 6e 20 efine os2DlOpen
c4e80 30 0a 20 20 23 64 65 66 69 6e 65 20 6f 73 32 44 0. #define os2D
c4e90 6c 45 72 72 6f 72 20 30 0a 20 20 23 64 65 66 69 lError 0. #defi
c4ea0 6e 65 20 6f 73 32 44 6c 53 79 6d 20 30 0a 20 20 ne os2DlSym 0.
c4eb0 23 64 65 66 69 6e 65 20 6f 73 32 44 6c 43 6c 6f #define os2DlClo
c4ec0 73 65 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a se 0.#endif.../*
c4ed0 0a 2a 2a 20 57 72 69 74 65 20 75 70 20 74 6f 20 .** Write up to
c4ee0 6e 42 75 66 20 62 79 74 65 73 20 6f 66 20 72 61 nBuf bytes of ra
c4ef0 6e 64 6f 6d 6e 65 73 73 20 69 6e 74 6f 20 7a 42 ndomness into zB
c4f00 75 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e uf..*/.static in
c4f10 74 20 6f 73 32 52 61 6e 64 6f 6d 6e 65 73 73 28 t os2Randomness(
c4f20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
c4f30 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 s, int nBuf, cha
c4f40 72 20 2a 7a 42 75 66 20 29 7b 0a 20 20 69 6e 74 r *zBuf ){. int
c4f50 20 6e 20 3d 20 30 3b 0a 23 69 66 20 64 65 66 69 n = 0;.#if defi
c4f60 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 ned(SQLITE_TEST)
c4f70 0a 20 20 6e 20 3d 20 6e 42 75 66 3b 0a 20 20 6d . n = nBuf;. m
c4f80 65 6d 73 65 74 28 7a 42 75 66 2c 20 30 2c 20 6e emset(zBuf, 0, n
c4f90 42 75 66 29 3b 0a 23 65 6c 73 65 0a 20 20 69 6e Buf);.#else. in
c4fa0 74 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3d 20 t sizeofULong =
c4fb0 73 69 7a 65 6f 66 28 55 4c 4f 4e 47 29 3b 0a 20 sizeof(ULONG);.
c4fc0 20 69 66 28 20 28 69 6e 74 29 73 69 7a 65 6f 66 if( (int)sizeof
c4fd0 28 44 41 54 45 54 49 4d 45 29 20 3c 3d 20 6e 42 (DATETIME) <= nB
c4fe0 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 44 41 uf - n ){. DA
c4ff0 54 45 54 49 4d 45 20 78 3b 0a 20 20 20 20 44 6f TETIME x;. Do
c5000 73 47 65 74 44 61 74 65 54 69 6d 65 28 26 78 29 sGetDateTime(&x)
c5010 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 ;. memcpy(&zB
c5020 75 66 5b 6e 5d 2c 20 26 78 2c 20 73 69 7a 65 6f uf[n], &x, sizeo
c5030 66 28 78 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 f(x));. n +=
c5040 73 69 7a 65 6f 66 28 78 29 3b 0a 20 20 7d 0a 0a sizeof(x);. }..
c5050 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e if( sizeofULon
c5060 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b g <= nBuf - n ){
c5070 0a 20 20 20 20 50 50 49 42 20 70 70 69 62 3b 0a . PPIB ppib;.
c5080 20 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c DosGetInfoBl
c5090 6f 63 6b 73 28 4e 55 4c 4c 2c 20 26 70 70 69 62 ocks(NULL, &ppib
c50a0 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a );. memcpy(&z
c50b0 42 75 66 5b 6e 5d 2c 20 26 70 70 69 62 2d 3e 70 Buf[n], &ppib->p
c50c0 69 62 5f 75 6c 70 69 64 2c 20 73 69 7a 65 6f 66 ib_ulpid, sizeof
c50d0 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 6e 20 2b 3d ULong);. n +=
c50e0 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 sizeofULong;.
c50f0 7d 0a 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 55 }.. if( sizeofU
c5100 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e Long <= nBuf - n
c5110 20 29 7b 0a 20 20 20 20 50 54 49 42 20 70 74 69 ){. PTIB pti
c5120 62 3b 0a 20 20 20 20 44 6f 73 47 65 74 49 6e 66 b;. DosGetInf
c5130 6f 42 6c 6f 63 6b 73 28 26 70 74 69 62 2c 20 4e oBlocks(&ptib, N
c5140 55 4c 4c 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 ULL);. memcpy
c5150 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 70 74 69 62 (&zBuf[n], &ptib
c5160 2d 3e 74 69 62 5f 70 74 69 62 32 2d 3e 74 69 62 ->tib_ptib2->tib
c5170 32 5f 75 6c 74 69 64 2c 20 73 69 7a 65 6f 66 55 2_ultid, sizeofU
c5180 4c 6f 6e 67 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 Long);. n +=
c5190 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 7d sizeofULong;. }
c51a0 0a 0a 20 20 2f 2a 20 69 66 20 77 65 20 73 74 69 .. /* if we sti
c51b0 6c 6c 20 68 61 76 65 6e 27 74 20 66 69 6c 6c 65 ll haven't fille
c51c0 64 20 74 68 65 20 62 75 66 66 65 72 20 79 65 74 d the buffer yet
c51d0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 77 the following w
c51e0 69 6c 6c 20 2a 2f 0a 20 20 2f 2a 20 67 72 61 62 ill */. /* grab
c51f0 20 65 76 65 72 79 74 68 69 6e 67 20 6f 6e 63 65 everything once
c5200 20 69 6e 73 74 65 61 64 20 6f 66 20 6d 61 6b 69 instead of maki
c5210 6e 67 20 73 65 76 65 72 61 6c 20 63 61 6c 6c 73 ng several calls
c5220 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 69 74 for a single it
c5230 65 6d 20 2a 2f 0a 20 20 69 66 28 20 73 69 7a 65 em */. if( size
c5240 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 ofULong <= nBuf
c5250 2d 20 6e 20 29 7b 0a 20 20 20 20 55 4c 4f 4e 47 - n ){. ULONG
c5260 20 75 6c 53 79 73 49 6e 66 6f 5b 51 53 56 5f 4d ulSysInfo[QSV_M
c5270 41 58 5d 3b 0a 20 20 20 20 44 6f 73 51 75 65 72 AX];. DosQuer
c5280 79 53 79 73 49 6e 66 6f 28 31 4c 2c 20 51 53 56 ySysInfo(1L, QSV
c5290 5f 4d 41 58 2c 20 75 6c 53 79 73 49 6e 66 6f 2c _MAX, ulSysInfo,
c52a0 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 2a 20 51 sizeofULong * Q
c52b0 53 56 5f 4d 41 58 29 3b 0a 0a 20 20 20 20 6d 65 SV_MAX);.. me
c52c0 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 mcpy(&zBuf[n], &
c52d0 75 6c 53 79 73 49 6e 66 6f 5b 51 53 56 5f 4d 53 ulSysInfo[QSV_MS
c52e0 5f 43 4f 55 4e 54 20 2d 20 31 5d 2c 20 73 69 7a _COUNT - 1], siz
c52f0 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 6e eofULong);. n
c5300 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b += sizeofULong;
c5310 0a 0a 20 20 20 20 69 66 28 20 73 69 7a 65 6f 66 .. if( sizeof
c5320 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 ULong <= nBuf -
c5330 6e 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 n ){. memcp
c5340 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 y(&zBuf[n], &ulS
c5350 79 73 49 6e 66 6f 5b 51 53 56 5f 54 49 4d 45 52 ysInfo[QSV_TIMER
c5360 5f 49 4e 54 45 52 56 41 4c 20 2d 20 31 5d 2c 20 _INTERVAL - 1],
c5370 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 sizeofULong);.
c5380 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 n += sizeofU
c5390 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 Long;. }.
c53a0 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 if( sizeofULong
c53b0 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 <= nBuf - n ){.
c53c0 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 memcpy(&zBu
c53d0 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e 66 6f f[n], &ulSysInfo
c53e0 5b 51 53 56 5f 54 49 4d 45 5f 4c 4f 57 20 2d 20 [QSV_TIME_LOW -
c53f0 31 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 1], sizeofULong)
c5400 3b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 73 69 7a ;. n += siz
c5410 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a eofULong;. }.
c5420 20 20 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c if( sizeofUL
c5430 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 ong <= nBuf - n
c5440 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 ){. memcpy(
c5450 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 &zBuf[n], &ulSys
c5460 49 6e 66 6f 5b 51 53 56 5f 54 49 4d 45 5f 48 49 Info[QSV_TIME_HI
c5470 47 48 20 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55 GH - 1], sizeofU
c5480 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 20 6e 20 2b Long);. n +
c5490 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 = sizeofULong;.
c54a0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 69 7a }. if( siz
c54b0 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 eofULong <= nBuf
c54c0 20 2d 20 6e 20 29 7b 0a 20 20 20 20 20 20 6d 65 - n ){. me
c54d0 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 mcpy(&zBuf[n], &
c54e0 75 6c 53 79 73 49 6e 66 6f 5b 51 53 56 5f 54 4f ulSysInfo[QSV_TO
c54f0 54 41 56 41 49 4c 4d 45 4d 20 2d 20 31 5d 2c 20 TAVAILMEM - 1],
c5500 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 sizeofULong);.
c5510 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 n += sizeofU
c5520 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a Long;. }. }.
c5530 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e #endif.. return
c5540 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 n;.}../*.** Sle
c5550 65 70 20 66 6f 72 20 61 20 6c 69 74 74 6c 65 20 ep for a little
c5560 77 68 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 74 while. Return t
c5570 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d he amount of tim
c5580 65 20 73 6c 65 70 74 2e 0a 2a 2a 20 54 68 65 20 e slept..** The
c5590 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 argument is the
c55a0 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 number of micros
c55b0 65 63 6f 6e 64 73 20 77 65 20 77 61 6e 74 20 74 econds we want t
c55c0 6f 20 73 6c 65 65 70 2e 0a 2a 2a 20 54 68 65 20 o sleep..** The
c55d0 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 return value is
c55e0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 the number of mi
c55f0 63 72 6f 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c croseconds of sl
c5600 65 65 70 20 61 63 74 75 61 6c 6c 79 0a 2a 2a 20 eep actually.**
c5610 72 65 71 75 65 73 74 65 64 20 66 72 6f 6d 20 74 requested from t
c5620 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 he underlying op
c5630 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2c 20 erating system,
c5640 61 20 6e 75 6d 62 65 72 20 77 68 69 63 68 0a 2a a number which.*
c5650 2a 20 6d 69 67 68 74 20 62 65 20 67 72 65 61 74 * might be great
c5660 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c er than or equal
c5670 20 74 6f 20 74 68 65 20 61 72 67 75 6d 65 6e 74 to the argument
c5680 2c 20 62 75 74 20 6e 6f 74 20 6c 65 73 73 0a 2a , but not less.*
c5690 2a 20 74 68 61 6e 20 74 68 65 20 61 72 67 75 6d * than the argum
c56a0 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ent..*/.static i
c56b0 6e 74 20 6f 73 32 53 6c 65 65 70 28 20 73 71 6c nt os2Sleep( sql
c56c0 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 ite3_vfs *pVfs,
c56d0 69 6e 74 20 6d 69 63 72 6f 73 65 63 20 29 7b 0a int microsec ){.
c56e0 20 20 44 6f 73 53 6c 65 65 70 28 20 28 6d 69 63 DosSleep( (mic
c56f0 72 6f 73 65 63 2f 31 30 30 30 29 20 29 3b 0a 20 rosec/1000) );.
c5700 20 72 65 74 75 72 6e 20 6d 69 63 72 6f 73 65 63 return microsec
c5710 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 ;.}../*.** The f
c5720 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c ollowing variabl
c5730 65 2c 20 69 66 20 73 65 74 20 74 6f 20 61 20 6e e, if set to a n
c5740 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2c 20 62 on-zero value, b
c5750 65 63 6f 6d 65 73 20 74 68 65 20 72 65 73 75 6c ecomes the resul
c5760 74 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 66 72 t.** returned fr
c5770 6f 6d 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 om sqlite3OsCurr
c5780 65 6e 74 54 69 6d 65 28 29 2e 20 20 54 68 69 73 entTime(). This
c5790 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 is used for tes
c57a0 74 69 6e 67 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 ting..*/.#ifdef
c57b0 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 SQLITE_TEST.SQLI
c57c0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
c57d0 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 e3_current_time
c57e0 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a = 0;.#endif../*.
c57f0 2a 2a 20 46 69 6e 64 20 74 68 65 20 63 75 72 72 ** Find the curr
c5800 65 6e 74 20 74 69 6d 65 20 28 69 6e 20 55 6e 69 ent time (in Uni
c5810 76 65 72 73 61 6c 20 43 6f 6f 72 64 69 6e 61 74 versal Coordinat
c5820 65 64 20 54 69 6d 65 29 2e 20 20 57 72 69 74 65 ed Time). Write
c5830 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 the.** current
c5840 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 61 73 time and date as
c5850 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 6e 75 a Julian Day nu
c5860 6d 62 65 72 20 69 6e 74 6f 20 2a 70 72 4e 6f 77 mber into *prNow
c5870 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 30 and.** return 0
c5880 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 . Return 1 if t
c5890 68 65 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65 he time and date
c58a0 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 cannot be found
c58b0 2e 0a 2a 2f 0a 69 6e 74 20 6f 73 32 43 75 72 72 ..*/.int os2Curr
c58c0 65 6e 74 54 69 6d 65 28 20 73 71 6c 69 74 65 33 entTime( sqlite3
c58d0 5f 76 66 73 20 2a 70 56 66 73 2c 20 64 6f 75 62 _vfs *pVfs, doub
c58e0 6c 65 20 2a 70 72 4e 6f 77 20 29 7b 0a 20 20 64 le *prNow ){. d
c58f0 6f 75 62 6c 65 20 6e 6f 77 3b 0a 20 20 53 48 4f ouble now;. SHO
c5900 52 54 20 6d 69 6e 75 74 65 3b 20 2f 2a 20 6e 65 RT minute; /* ne
c5910 65 64 73 20 74 6f 20 62 65 20 61 62 6c 65 20 74 eds to be able t
c5920 6f 20 63 6f 70 65 20 77 69 74 68 20 6e 65 67 61 o cope with nega
c5930 74 69 76 65 20 74 69 6d 65 7a 6f 6e 65 20 6f 66 tive timezone of
c5940 66 73 65 74 20 2a 2f 0a 20 20 55 53 48 4f 52 54 fset */. USHORT
c5950 20 73 65 63 6f 6e 64 2c 20 68 6f 75 72 2c 0a 20 second, hour,.
c5960 20 20 20 20 20 20 20 20 64 61 79 2c 20 6d 6f 6e day, mon
c5970 74 68 2c 20 79 65 61 72 3b 0a 20 20 44 41 54 45 th, year;. DATE
c5980 54 49 4d 45 20 64 74 3b 0a 20 20 44 6f 73 47 65 TIME dt;. DosGe
c5990 74 44 61 74 65 54 69 6d 65 28 20 26 64 74 20 29 tDateTime( &dt )
c59a0 3b 0a 20 20 73 65 63 6f 6e 64 20 3d 20 28 55 53 ;. second = (US
c59b0 48 4f 52 54 29 64 74 2e 73 65 63 6f 6e 64 73 3b HORT)dt.seconds;
c59c0 0a 20 20 6d 69 6e 75 74 65 20 3d 20 28 53 48 4f . minute = (SHO
c59d0 52 54 29 64 74 2e 6d 69 6e 75 74 65 73 20 2b 20 RT)dt.minutes +
c59e0 64 74 2e 74 69 6d 65 7a 6f 6e 65 3b 0a 20 20 68 dt.timezone;. h
c59f0 6f 75 72 20 3d 20 28 55 53 48 4f 52 54 29 64 74 our = (USHORT)dt
c5a00 2e 68 6f 75 72 73 3b 0a 20 20 64 61 79 20 3d 20 .hours;. day =
c5a10 28 55 53 48 4f 52 54 29 64 74 2e 64 61 79 3b 0a (USHORT)dt.day;.
c5a20 20 20 6d 6f 6e 74 68 20 3d 20 28 55 53 48 4f 52 month = (USHOR
c5a30 54 29 64 74 2e 6d 6f 6e 74 68 3b 0a 20 20 79 65 T)dt.month;. ye
c5a40 61 72 20 3d 20 28 55 53 48 4f 52 54 29 64 74 2e ar = (USHORT)dt.
c5a50 79 65 61 72 3b 0a 0a 20 20 2f 2a 20 43 61 6c 63 year;.. /* Calc
c5a60 75 6c 61 74 69 6f 6e 73 20 66 72 6f 6d 20 68 74 ulations from ht
c5a70 74 70 3a 2f 2f 77 77 77 2e 61 73 74 72 6f 2e 6b tp://www.astro.k
c5a80 65 65 6c 65 2e 61 63 2e 75 6b 2f 7e 72 6e 6f 2f eele.ac.uk/~rno/
c5a90 41 73 74 72 6f 6e 6f 6d 79 2f 68 6a 64 2e 68 74 Astronomy/hjd.ht
c5aa0 6d 6c 0a 20 20 20 20 20 68 74 74 70 3a 2f 2f 77 ml. http://w
c5ab0 77 77 2e 61 73 74 72 6f 2e 6b 65 65 6c 65 2e 61 ww.astro.keele.a
c5ac0 63 2e 75 6b 2f 7e 72 6e 6f 2f 41 73 74 72 6f 6e c.uk/~rno/Astron
c5ad0 6f 6d 79 2f 68 6a 64 2d 30 2e 31 2e 63 20 2a 2f omy/hjd-0.1.c */
c5ae0 0a 20 20 2f 2a 20 43 61 6c 63 75 6c 61 74 65 20 . /* Calculate
c5af0 74 68 65 20 4a 75 6c 69 61 6e 20 64 61 79 73 20 the Julian days
c5b00 2a 2f 0a 20 20 6e 6f 77 20 3d 20 64 61 79 20 2d */. now = day -
c5b10 20 33 32 30 37 36 20 2b 0a 20 20 20 20 31 34 36 32076 +. 146
c5b20 31 2a 28 79 65 61 72 20 2b 20 34 38 30 30 20 2b 1*(year + 4800 +
c5b30 20 28 6d 6f 6e 74 68 20 2d 20 31 34 29 2f 31 32 (month - 14)/12
c5b40 29 2f 34 20 2b 0a 20 20 20 20 33 36 37 2a 28 6d )/4 +. 367*(m
c5b50 6f 6e 74 68 20 2d 20 32 20 2d 20 28 6d 6f 6e 74 onth - 2 - (mont
c5b60 68 20 2d 20 31 34 29 2f 31 32 2a 31 32 29 2f 31 h - 14)/12*12)/1
c5b70 32 20 2d 0a 20 20 20 20 33 2a 28 28 79 65 61 72 2 -. 3*((year
c5b80 20 2b 20 34 39 30 30 20 2b 20 28 6d 6f 6e 74 68 + 4900 + (month
c5b90 20 2d 20 31 34 29 2f 31 32 29 2f 31 30 30 29 2f - 14)/12)/100)/
c5ba0 34 3b 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 4;.. /* Add the
c5bb0 20 66 72 61 63 74 69 6f 6e 61 6c 20 68 6f 75 72 fractional hour
c5bc0 73 2c 20 6d 69 6e 73 20 61 6e 64 20 73 65 63 6f s, mins and seco
c5bd0 6e 64 73 20 2a 2f 0a 20 20 6e 6f 77 20 2b 3d 20 nds */. now +=
c5be0 28 68 6f 75 72 20 2b 20 31 32 2e 30 29 2f 32 34 (hour + 12.0)/24
c5bf0 2e 30 3b 0a 20 20 6e 6f 77 20 2b 3d 20 6d 69 6e .0;. now += min
c5c00 75 74 65 2f 31 34 34 30 2e 30 3b 0a 20 20 6e 6f ute/1440.0;. no
c5c10 77 20 2b 3d 20 73 65 63 6f 6e 64 2f 38 36 34 30 w += second/8640
c5c20 30 2e 30 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 0.0;. *prNow =
c5c30 6e 6f 77 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 now;.#ifdef SQLI
c5c40 54 45 5f 54 45 53 54 0a 20 20 69 66 28 20 73 71 TE_TEST. if( sq
c5c50 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 lite3_current_ti
c5c60 6d 65 20 29 7b 0a 20 20 20 20 2a 70 72 4e 6f 77 me ){. *prNow
c5c70 20 3d 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 = sqlite3_curre
c5c80 6e 74 5f 74 69 6d 65 2f 38 36 34 30 30 2e 30 20 nt_time/86400.0
c5c90 2b 20 32 34 34 30 35 38 37 2e 35 3b 0a 20 20 7d + 2440587.5;. }
c5ca0 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e .#endif. return
c5cb0 20 30 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 0;.}..static in
c5cc0 74 20 6f 73 32 47 65 74 4c 61 73 74 45 72 72 6f t os2GetLastErro
c5cd0 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 r(sqlite3_vfs *p
c5ce0 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 Vfs, int nBuf, c
c5cf0 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 72 65 har *zBuf){. re
c5d00 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn 0;.}../*.**
c5d10 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 Initialize and
c5d20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 deinitialize the
c5d30 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 operating syste
c5d40 6d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a m interface..*/.
c5d50 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
c5d60 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 76 qlite3_os_init(v
c5d70 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 oid){. static s
c5d80 71 6c 69 74 65 33 5f 76 66 73 20 6f 73 32 56 66 qlite3_vfs os2Vf
c5d90 73 20 3d 20 7b 0a 20 20 20 20 31 2c 20 20 20 20 s = {. 1,
c5da0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
c5db0 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 iVersion */.
c5dc0 73 69 7a 65 6f 66 28 6f 73 32 46 69 6c 65 29 2c sizeof(os2File),
c5dd0 20 20 20 2f 2a 20 73 7a 4f 73 46 69 6c 65 20 2a /* szOsFile *
c5de0 2f 0a 20 20 20 20 43 43 48 4d 41 58 50 41 54 48 /. CCHMAXPATH
c5df0 2c 20 20 20 20 20 20 20 20 2f 2a 20 6d 78 50 61 , /* mxPa
c5e00 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c thname */. 0,
c5e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c5e20 20 2f 2a 20 70 4e 65 78 74 20 2a 2f 0a 20 20 20 /* pNext */.
c5e30 20 22 6f 73 32 22 2c 20 20 20 20 20 20 20 20 20 "os2",
c5e40 20 20 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a /* zName */.
c5e50 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 0,
c5e60 20 20 20 20 20 20 20 2f 2a 20 70 41 70 70 44 61 /* pAppDa
c5e70 74 61 20 2a 2f 0a 0a 20 20 20 20 6f 73 32 4f 70 ta */.. os2Op
c5e80 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a en, /*
c5e90 20 78 4f 70 65 6e 20 2a 2f 0a 20 20 20 20 6f 73 xOpen */. os
c5ea0 32 44 65 6c 65 74 65 2c 20 20 20 20 20 20 20 20 2Delete,
c5eb0 20 2f 2a 20 78 44 65 6c 65 74 65 20 2a 2f 0a 20 /* xDelete */.
c5ec0 20 20 20 6f 73 32 41 63 63 65 73 73 2c 20 20 20 os2Access,
c5ed0 20 20 20 20 20 20 2f 2a 20 78 41 63 63 65 73 73 /* xAccess
c5ee0 20 2a 2f 0a 20 20 20 20 6f 73 32 46 75 6c 6c 50 */. os2FullP
c5ef0 61 74 68 6e 61 6d 65 2c 20 20 20 2f 2a 20 78 46 athname, /* xF
c5f00 75 6c 6c 50 61 74 68 6e 61 6d 65 20 2a 2f 0a 20 ullPathname */.
c5f10 20 20 20 6f 73 32 44 6c 4f 70 65 6e 2c 20 20 20 os2DlOpen,
c5f20 20 20 20 20 20 20 2f 2a 20 78 44 6c 4f 70 65 6e /* xDlOpen
c5f30 20 2a 2f 0a 20 20 20 20 6f 73 32 44 6c 45 72 72 */. os2DlErr
c5f40 6f 72 2c 20 20 20 20 20 20 20 20 2f 2a 20 78 44 or, /* xD
c5f50 6c 45 72 72 6f 72 20 2a 2f 0a 20 20 20 20 6f 73 lError */. os
c5f60 32 44 6c 53 79 6d 2c 20 20 20 20 20 20 20 20 20 2DlSym,
c5f70 20 2f 2a 20 78 44 6c 53 79 6d 20 2a 2f 0a 20 20 /* xDlSym */.
c5f80 20 20 6f 73 32 44 6c 43 6c 6f 73 65 2c 20 20 20 os2DlClose,
c5f90 20 20 20 20 20 2f 2a 20 78 44 6c 43 6c 6f 73 65 /* xDlClose
c5fa0 20 2a 2f 0a 20 20 20 20 6f 73 32 52 61 6e 64 6f */. os2Rando
c5fb0 6d 6e 65 73 73 2c 20 20 20 20 20 2f 2a 20 78 52 mness, /* xR
c5fc0 61 6e 64 6f 6d 6e 65 73 73 20 2a 2f 0a 20 20 20 andomness */.
c5fd0 20 6f 73 32 53 6c 65 65 70 2c 20 20 20 20 20 20 os2Sleep,
c5fe0 20 20 20 20 2f 2a 20 78 53 6c 65 65 70 20 2a 2f /* xSleep */
c5ff0 0a 20 20 20 20 6f 73 32 43 75 72 72 65 6e 74 54 . os2CurrentT
c6000 69 6d 65 2c 20 20 20 20 2f 2a 20 78 43 75 72 72 ime, /* xCurr
c6010 65 6e 74 54 69 6d 65 20 2a 2f 0a 20 20 20 20 6f entTime */. o
c6020 73 32 47 65 74 4c 61 73 74 45 72 72 6f 72 20 20 s2GetLastError
c6030 20 20 2f 2a 20 78 47 65 74 4c 61 73 74 45 72 72 /* xGetLastErr
c6040 6f 72 20 2a 2f 0a 20 20 7d 3b 0a 20 20 73 71 6c or */. };. sql
c6050 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65 ite3_vfs_registe
c6060 72 28 26 6f 73 32 56 66 73 2c 20 31 29 3b 0a 20 r(&os2Vfs, 1);.
c6070 20 69 6e 69 74 55 63 6f 6e 76 4f 62 6a 65 63 74 initUconvObject
c6080 73 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 s();. return SQ
c6090 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 53 51 4c 49 54 LITE_OK;.}.SQLIT
c60a0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
c60b0 33 5f 6f 73 5f 65 6e 64 28 76 6f 69 64 29 7b 0a 3_os_end(void){.
c60c0 20 20 66 72 65 65 55 63 6f 6e 76 4f 62 6a 65 63 freeUconvObjec
c60d0 74 73 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 ts();. return S
c60e0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e QLITE_OK;.}..#en
c60f0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 dif /* SQLITE_OS
c6100 5f 4f 53 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a _OS2 */../******
c6110 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
c6120 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a os_os2.c *******
c6130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c6140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c6150 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
c6160 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
c6170 69 6c 65 20 6f 73 5f 75 6e 69 78 2e 63 20 2a 2a ile os_unix.c **
c6180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c6190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c61a0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
c61b0 30 30 34 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 004 May 22.**.**
c61c0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
c61d0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
c61e0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
c61f0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
c6200 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
c6210 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
c6220 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
c6230 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
c6240 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
c6250 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
c6260 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
c6270 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
c6280 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
c6290 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
c62a0 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
c62b0 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
c62c0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
c62d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c62e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c62f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c6300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c6310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
c6320 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
c6330 6f 6e 74 61 69 6e 73 20 74 68 65 20 56 46 53 20 ontains the VFS
c6340 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 implementation f
c6350 6f 72 20 75 6e 69 78 2d 6c 69 6b 65 20 6f 70 65 or unix-like ope
c6360 72 61 74 69 6e 67 20 73 79 73 74 65 6d 73 0a 2a rating systems.*
c6370 2a 20 69 6e 63 6c 75 64 65 20 4c 69 6e 75 78 2c * include Linux,
c6380 20 4d 61 63 4f 53 58 2c 20 2a 42 53 44 2c 20 51 MacOSX, *BSD, Q
c6390 4e 58 2c 20 56 78 57 6f 72 6b 73 2c 20 41 49 58 NX, VxWorks, AIX
c63a0 2c 20 48 50 55 58 2c 20 61 6e 64 20 6f 74 68 65 , HPUX, and othe
c63b0 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 rs..**.** There
c63c0 61 72 65 20 61 63 74 75 61 6c 6c 79 20 73 65 76 are actually sev
c63d0 65 72 61 6c 20 64 69 66 66 65 72 65 6e 74 20 56 eral different V
c63e0 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f FS implementatio
c63f0 6e 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65 2e ns in this file.
c6400 0a 2a 2a 20 54 68 65 20 64 69 66 66 65 72 65 6e .** The differen
c6410 63 65 73 20 61 72 65 20 69 6e 20 74 68 65 20 77 ces are in the w
c6420 61 79 20 74 68 61 74 20 66 69 6c 65 20 6c 6f 63 ay that file loc
c6430 6b 69 6e 67 20 69 73 20 64 6f 6e 65 2e 20 20 54 king is done. T
c6440 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20 69 6d he default.** im
c6450 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 plementation use
c6460 73 20 50 6f 73 69 78 20 41 64 76 69 73 6f 72 79 s Posix Advisory
c6470 20 4c 6f 63 6b 73 2e 20 20 41 6c 74 65 72 6e 61 Locks. Alterna
c6480 74 69 76 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 tive implementat
c6490 69 6f 6e 73 0a 2a 2a 20 75 73 65 20 66 6c 6f 63 ions.** use floc
c64a0 6b 28 29 2c 20 64 6f 74 2d 66 69 6c 65 73 2c 20 k(), dot-files,
c64b0 76 61 72 69 6f 75 73 20 70 72 6f 70 72 69 65 74 various propriet
c64c0 61 72 79 20 6c 6f 63 6b 69 6e 67 20 73 63 68 65 ary locking sche
c64d0 6d 61 73 2c 20 6f 72 20 73 69 6d 70 6c 79 0a 2a mas, or simply.*
c64e0 2a 20 73 6b 69 70 20 6c 6f 63 6b 69 6e 67 20 61 * skip locking a
c64f0 6c 6c 20 74 6f 67 65 74 68 65 72 2e 0a 2a 2a 0a ll together..**.
c6500 2a 2a 20 54 68 69 73 20 73 6f 75 72 63 65 20 66 ** This source f
c6510 69 6c 65 20 69 73 20 6f 72 67 61 6e 69 7a 65 64 ile is organized
c6520 20 69 6e 74 6f 20 64 69 76 69 73 69 6f 6e 73 20 into divisions
c6530 77 68 65 72 65 20 74 68 65 20 6c 6f 67 69 63 20 where the logic
c6540 66 6f 72 20 76 61 72 69 6f 75 73 0a 2a 2a 20 73 for various.** s
c6550 75 62 66 75 6e 63 74 69 6f 6e 73 20 69 73 20 63 ubfunctions is c
c6560 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 6e 20 ontained within
c6570 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 the appropriate
c6580 64 69 76 69 73 69 6f 6e 2e 20 20 50 4c 45 41 53 division. PLEAS
c6590 45 0a 2a 2a 20 4b 45 45 50 20 54 48 45 20 53 54 E.** KEEP THE ST
c65a0 52 55 43 54 55 52 45 20 4f 46 20 54 48 49 53 20 RUCTURE OF THIS
c65b0 46 49 4c 45 20 49 4e 54 41 43 54 2e 20 20 4e 65 FILE INTACT. Ne
c65c0 77 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 62 65 w code should be
c65d0 20 70 6c 61 63 65 64 0a 2a 2a 20 69 6e 20 74 68 placed.** in th
c65e0 65 20 63 6f 72 72 65 63 74 20 64 69 76 69 73 69 e correct divisi
c65f0 6f 6e 20 61 6e 64 20 73 68 6f 75 6c 64 20 62 65 on and should be
c6600 20 63 6c 65 61 72 6c 79 20 6c 61 62 65 6c 65 64 clearly labeled
c6610 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 61 79 6f ..**.** The layo
c6620 75 74 20 6f 66 20 64 69 76 69 73 69 6f 6e 73 20 ut of divisions
c6630 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a is as follows:.*
c6640 2a 0a 2a 2a 20 20 20 2a 20 20 47 65 6e 65 72 61 *.** * Genera
c6650 6c 2d 70 75 72 70 6f 73 65 20 64 65 63 6c 61 72 l-purpose declar
c6660 61 74 69 6f 6e 73 20 61 6e 64 20 75 74 69 6c 69 ations and utili
c6670 74 79 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a ty functions..**
c6680 20 20 20 2a 20 20 55 6e 69 71 75 65 20 66 69 6c * Unique fil
c6690 65 20 49 44 20 6c 6f 67 69 63 20 75 73 65 64 20 e ID logic used
c66a0 62 79 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a 20 20 by VxWorks..**
c66b0 20 2a 20 20 56 61 72 69 6f 75 73 20 6c 6f 63 6b * Various lock
c66c0 69 6e 67 20 70 72 69 6d 69 74 69 76 65 20 69 6d ing primitive im
c66d0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 28 61 plementations (a
c66e0 6c 6c 20 65 78 63 65 70 74 20 70 72 6f 78 79 20 ll except proxy
c66f0 6c 6f 63 6b 69 6e 67 29 3a 0a 2a 2a 20 20 20 20 locking):.**
c6700 20 20 2b 20 66 6f 72 20 50 6f 73 69 78 20 41 64 + for Posix Ad
c6710 76 69 73 6f 72 79 20 4c 6f 63 6b 73 0a 2a 2a 20 visory Locks.**
c6720 20 20 20 20 20 2b 20 66 6f 72 20 6e 6f 2d 6f 70 + for no-op
c6730 20 6c 6f 63 6b 73 0a 2a 2a 20 20 20 20 20 20 2b locks.** +
c6740 20 66 6f 72 20 64 6f 74 2d 66 69 6c 65 20 6c 6f for dot-file lo
c6750 63 6b 73 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f cks.** + fo
c6760 72 20 66 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e r flock() lockin
c6770 67 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 g.** + for
c6780 6e 61 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 named semaphore
c6790 6c 6f 63 6b 73 20 28 56 78 57 6f 72 6b 73 20 6f locks (VxWorks o
c67a0 6e 6c 79 29 0a 2a 2a 20 20 20 20 20 20 2b 20 66 nly).** + f
c67b0 6f 72 20 41 46 50 20 66 69 6c 65 73 79 73 74 65 or AFP filesyste
c67c0 6d 20 6c 6f 63 6b 73 20 28 4d 61 63 4f 53 58 20 m locks (MacOSX
c67d0 6f 6e 6c 79 29 0a 2a 2a 20 20 20 2a 20 20 73 71 only).** * sq
c67e0 6c 69 74 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f lite3_file metho
c67f0 64 73 20 6e 6f 74 20 61 73 73 6f 63 69 61 74 65 ds not associate
c6800 64 20 77 69 74 68 20 6c 6f 63 6b 69 6e 67 2e 0a d with locking..
c6810 2a 2a 20 20 20 2a 20 20 44 65 66 69 6e 69 74 69 ** * Definiti
c6820 6f 6e 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 69 ons of sqlite3_i
c6830 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 o_methods object
c6840 73 20 66 6f 72 20 61 6c 6c 20 6c 6f 63 6b 69 6e s for all lockin
c6850 67 0a 2a 2a 20 20 20 20 20 20 6d 65 74 68 6f 64 g.** method
c6860 73 20 70 6c 75 73 20 22 66 69 6e 64 65 72 22 20 s plus "finder"
c6870 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 65 61 functions for ea
c6880 63 68 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f ch locking metho
c6890 64 2e 0a 2a 2a 20 20 20 2a 20 20 73 71 6c 69 74 d..** * sqlit
c68a0 65 33 5f 76 66 73 20 6d 65 74 68 6f 64 20 69 6d e3_vfs method im
c68b0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a plementations..*
c68c0 2a 20 20 20 2a 20 20 4c 6f 63 6b 69 6e 67 20 70 * * Locking p
c68d0 72 69 6d 69 74 69 76 65 73 20 66 6f 72 20 74 68 rimitives for th
c68e0 65 20 70 72 6f 78 79 20 75 62 65 72 2d 6c 6f 63 e proxy uber-loc
c68f0 6b 69 6e 67 2d 6d 65 74 68 6f 64 2e 20 28 4d 61 king-method. (Ma
c6900 63 4f 53 58 20 6f 6e 6c 79 29 0a 2a 2a 20 20 20 cOSX only).**
c6910 2a 20 20 44 65 66 69 6e 69 74 69 6f 6e 73 20 6f * Definitions o
c6920 66 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 f sqlite3_vfs ob
c6930 6a 65 63 74 73 20 66 6f 72 20 61 6c 6c 20 6c 6f jects for all lo
c6940 63 6b 69 6e 67 20 6d 65 74 68 6f 64 73 0a 2a 2a cking methods.**
c6950 20 20 20 20 20 20 70 6c 75 73 20 69 6d 70 6c 65 plus imple
c6960 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 73 71 mentations of sq
c6970 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 lite3_os_init()
c6980 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 and sqlite3_os_e
c6990 6e 64 28 29 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c nd()..*/.#if SQL
c69a0 49 54 45 5f 4f 53 5f 55 4e 49 58 20 20 20 20 20 ITE_OS_UNIX
c69b0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 /* This
c69c0 20 66 69 6c 65 20 69 73 20 75 73 65 64 20 6f 6e file is used on
c69d0 20 75 6e 69 78 20 6f 6e 6c 79 20 2a 2f 0a 0a 2f unix only */../
c69e0 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 76 *.** There are v
c69f0 61 72 69 6f 75 73 20 6d 65 74 68 6f 64 73 20 66 arious methods f
c6a00 6f 72 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 or file locking
c6a10 75 73 65 64 20 66 6f 72 20 63 6f 6e 63 75 72 72 used for concurr
c6a20 65 6e 63 79 0a 2a 2a 20 63 6f 6e 74 72 6f 6c 3a ency.** control:
c6a30 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 50 4f 53 49 .**.** 1. POSI
c6a40 58 20 6c 6f 63 6b 69 6e 67 20 28 74 68 65 20 64 X locking (the d
c6a50 65 66 61 75 6c 74 29 2c 0a 2a 2a 20 20 20 32 2e efault),.** 2.
c6a60 20 4e 6f 20 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 No locking,.**
c6a70 20 20 33 2e 20 44 6f 74 2d 66 69 6c 65 20 6c 6f 3. Dot-file lo
c6a80 63 6b 69 6e 67 2c 0a 2a 2a 20 20 20 34 2e 20 66 cking,.** 4. f
c6a90 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 2c 0a lock() locking,.
c6aa0 2a 2a 20 20 20 35 2e 20 41 46 50 20 6c 6f 63 6b ** 5. AFP lock
c6ab0 69 6e 67 20 28 4f 53 58 20 6f 6e 6c 79 29 2c 0a ing (OSX only),.
c6ac0 2a 2a 20 20 20 36 2e 20 4e 61 6d 65 64 20 50 4f ** 6. Named PO
c6ad0 53 49 58 20 73 65 6d 61 70 68 6f 72 65 73 20 28 SIX semaphores (
c6ae0 56 58 57 6f 72 6b 73 20 6f 6e 6c 79 29 2c 0a 2a VXWorks only),.*
c6af0 2a 20 20 20 37 2e 20 70 72 6f 78 79 20 6c 6f 63 * 7. proxy loc
c6b00 6b 69 6e 67 2e 20 28 4f 53 58 20 6f 6e 6c 79 29 king. (OSX only)
c6b10 0a 2a 2a 0a 2a 2a 20 53 74 79 6c 65 73 20 34 2c .**.** Styles 4,
c6b20 20 35 2c 20 61 6e 64 20 37 20 61 72 65 20 6f 6e 5, and 7 are on
c6b30 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 66 20 ly available of
c6b40 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f SQLITE_ENABLE_LO
c6b50 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 2a 2a 20 69 CKING_STYLE.** i
c6b60 73 20 64 65 66 69 6e 65 64 20 74 6f 20 31 2e 20 s defined to 1.
c6b70 20 54 68 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 The SQLITE_ENAB
c6b80 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 LE_LOCKING_STYLE
c6b90 20 61 6c 73 6f 20 65 6e 61 62 6c 65 73 20 61 75 also enables au
c6ba0 74 6f 6d 61 74 69 63 0a 2a 2a 20 73 65 6c 65 63 tomatic.** selec
c6bb0 74 69 6f 6e 20 6f 66 20 74 68 65 20 61 70 70 72 tion of the appr
c6bc0 6f 70 72 69 61 74 65 20 6c 6f 63 6b 69 6e 67 20 opriate locking
c6bd0 73 74 79 6c 65 20 62 61 73 65 64 20 6f 6e 20 74 style based on t
c6be0 68 65 20 66 69 6c 65 73 79 73 74 65 6d 0a 2a 2a he filesystem.**
c6bf0 20 77 68 65 72 65 20 74 68 65 20 64 61 74 61 62 where the datab
c6c00 61 73 65 20 69 73 20 6c 6f 63 61 74 65 64 2e 20 ase is located.
c6c10 20 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 .*/.#if !define
c6c20 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f d(SQLITE_ENABLE_
c6c30 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 29 0a 23 LOCKING_STYLE).#
c6c40 20 20 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 if defined(__A
c6c50 50 50 4c 45 5f 5f 29 0a 23 20 20 20 20 64 65 66 PPLE__).# def
c6c60 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c ine SQLITE_ENABL
c6c70 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 E_LOCKING_STYLE
c6c80 31 0a 23 20 20 65 6c 73 65 0a 23 20 20 20 20 64 1.# else.# d
c6c90 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e 41 efine SQLITE_ENA
c6ca0 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c BLE_LOCKING_STYL
c6cb0 45 20 30 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e E 0.# endif.#en
c6cc0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e dif../*.** Defin
c6cd0 65 20 74 68 65 20 4f 53 5f 56 58 57 4f 52 4b 53 e the OS_VXWORKS
c6ce0 20 70 72 65 2d 70 72 6f 63 65 73 73 6f 72 20 6d pre-processor m
c6cf0 61 63 72 6f 20 74 6f 20 31 20 69 66 20 62 75 69 acro to 1 if bui
c6d00 6c 64 69 6e 67 20 6f 6e 20 0a 2a 2a 20 76 78 77 lding on .** vxw
c6d10 6f 72 6b 73 2c 20 6f 72 20 30 20 6f 74 68 65 72 orks, or 0 other
c6d20 77 69 73 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 wise..*/.#ifndef
c6d30 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 23 20 20 69 OS_VXWORKS.# i
c6d40 66 20 64 65 66 69 6e 65 64 28 5f 5f 52 54 50 5f f defined(__RTP_
c6d50 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 57 _) || defined(_W
c6d60 52 53 5f 4b 45 52 4e 45 4c 29 0a 23 20 20 20 20 RS_KERNEL).#
c6d70 64 65 66 69 6e 65 20 4f 53 5f 56 58 57 4f 52 4b define OS_VXWORK
c6d80 53 20 31 0a 23 20 20 65 6c 73 65 0a 23 20 20 20 S 1.# else.#
c6d90 20 64 65 66 69 6e 65 20 4f 53 5f 56 58 57 4f 52 define OS_VXWOR
c6da0 4b 53 20 30 0a 23 20 20 65 6e 64 69 66 0a 23 65 KS 0.# endif.#e
c6db0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 ndif../*.** Thes
c6dc0 65 20 23 64 65 66 69 6e 65 73 20 73 68 6f 75 6c e #defines shoul
c6dd0 64 20 65 6e 61 62 6c 65 20 3e 32 47 42 20 66 69 d enable >2GB fi
c6de0 6c 65 20 73 75 70 70 6f 72 74 20 6f 6e 20 50 6f le support on Po
c6df0 73 69 78 20 69 66 20 74 68 65 0a 2a 2a 20 75 6e six if the.** un
c6e00 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 derlying operati
c6e10 6e 67 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72 ng system suppor
c6e20 74 73 20 69 74 2e 20 20 49 66 20 74 68 65 20 4f ts it. If the O
c6e30 53 20 6c 61 63 6b 73 0a 2a 2a 20 6c 61 72 67 65 S lacks.** large
c6e40 20 66 69 6c 65 20 73 75 70 70 6f 72 74 2c 20 74 file support, t
c6e50 68 65 73 65 20 73 68 6f 75 6c 64 20 62 65 20 6e hese should be n
c6e60 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61 72 o-ops..**.** Lar
c6e70 67 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74 20 ge file support
c6e80 63 61 6e 20 62 65 20 64 69 73 61 62 6c 65 64 20 can be disabled
c6e90 75 73 69 6e 67 20 74 68 65 20 2d 44 53 51 4c 49 using the -DSQLI
c6ea0 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 20 73 TE_DISABLE_LFS s
c6eb0 77 69 74 63 68 0a 2a 2a 20 6f 6e 20 74 68 65 20 witch.** on the
c6ec0 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 compiler command
c6ed0 20 6c 69 6e 65 2e 20 20 54 68 69 73 20 69 73 20 line. This is
c6ee0 6e 65 63 65 73 73 61 72 79 20 69 66 20 79 6f 75 necessary if you
c6ef0 20 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 0a 2a are compiling.*
c6f00 2a 20 6f 6e 20 61 20 72 65 63 65 6e 74 20 6d 61 * on a recent ma
c6f10 63 68 69 6e 65 20 28 65 78 3a 20 52 65 64 48 61 chine (ex: RedHa
c6f20 74 20 37 2e 32 29 20 62 75 74 20 79 6f 75 20 77 t 7.2) but you w
c6f30 61 6e 74 20 79 6f 75 72 20 63 6f 64 65 20 74 6f ant your code to
c6f40 20 77 6f 72 6b 0a 2a 2a 20 6f 6e 20 61 6e 20 6f work.** on an o
c6f50 6c 64 65 72 20 6d 61 63 68 69 6e 65 20 28 65 78 lder machine (ex
c6f60 3a 20 52 65 64 48 61 74 20 36 2e 30 29 2e 20 20 : RedHat 6.0).
c6f70 49 66 20 79 6f 75 20 63 6f 6d 70 69 6c 65 20 6f If you compile o
c6f80 6e 20 52 65 64 48 61 74 20 37 2e 32 0a 2a 2a 20 n RedHat 7.2.**
c6f90 77 69 74 68 6f 75 74 20 74 68 69 73 20 6f 70 74 without this opt
c6fa0 69 6f 6e 2c 20 4c 46 53 20 69 73 20 65 6e 61 62 ion, LFS is enab
c6fb0 6c 65 2e 20 20 42 75 74 20 4c 46 53 20 64 6f 65 le. But LFS doe
c6fc0 73 20 6e 6f 74 20 65 78 69 73 74 20 69 6e 20 74 s not exist in t
c6fd0 68 65 20 6b 65 72 6e 65 6c 0a 2a 2a 20 69 6e 20 he kernel.** in
c6fe0 52 65 64 48 61 74 20 36 2e 30 2c 20 73 6f 20 74 RedHat 6.0, so t
c6ff0 68 65 20 63 6f 64 65 20 77 6f 6e 27 74 20 77 6f he code won't wo
c7000 72 6b 2e 20 20 48 65 6e 63 65 2c 20 66 6f 72 20 rk. Hence, for
c7010 6d 61 78 69 6d 75 6d 20 62 69 6e 61 72 79 0a 2a maximum binary.*
c7020 2a 20 70 6f 72 74 61 62 69 6c 69 74 79 20 79 6f * portability yo
c7030 75 20 73 68 6f 75 6c 64 20 6f 6d 69 74 20 4c 46 u should omit LF
c7040 53 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 65 S..**.** The pre
c7050 76 69 6f 75 73 20 70 61 72 61 67 72 61 70 68 20 vious paragraph
c7060 77 61 73 20 77 72 69 74 74 65 6e 20 69 6e 20 32 was written in 2
c7070 30 30 35 2e 20 20 28 54 68 69 73 20 70 61 72 61 005. (This para
c7080 67 72 61 70 68 20 69 73 20 77 72 69 74 74 65 6e graph is written
c7090 0a 2a 2a 20 6f 6e 20 32 30 30 38 2d 31 31 2d 32 .** on 2008-11-2
c70a0 38 2e 29 20 54 68 65 73 65 20 64 61 79 73 2c 20 8.) These days,
c70b0 61 6c 6c 20 4c 69 6e 75 78 20 6b 65 72 6e 65 6c all Linux kernel
c70c0 73 20 73 75 70 70 6f 72 74 20 6c 61 72 67 65 20 s support large
c70d0 66 69 6c 65 73 2c 20 73 6f 0a 2a 2a 20 79 6f 75 files, so.** you
c70e0 20 73 68 6f 75 6c 64 20 70 72 6f 62 61 62 6c 79 should probably
c70f0 20 6c 65 61 76 65 20 4c 46 53 20 65 6e 61 62 6c leave LFS enabl
c7100 65 64 2e 20 20 42 75 74 20 73 6f 6d 65 20 65 6d ed. But some em
c7110 62 65 64 64 65 64 20 70 6c 61 74 66 6f 72 6d 73 bedded platforms
c7120 20 6d 69 67 68 74 0a 2a 2a 20 6c 61 63 6b 20 4c might.** lack L
c7130 46 53 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 FS in which case
c7140 20 74 68 65 20 53 51 4c 49 54 45 5f 44 49 53 41 the SQLITE_DISA
c7150 42 4c 45 5f 4c 46 53 20 6d 61 63 72 6f 20 6d 69 BLE_LFS macro mi
c7160 67 68 74 20 73 74 69 6c 6c 20 62 65 20 75 73 65 ght still be use
c7170 66 75 6c 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ful..*/.#ifndef
c7180 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c SQLITE_DISABLE_L
c7190 46 53 0a 23 20 64 65 66 69 6e 65 20 5f 4c 41 52 FS.# define _LAR
c71a0 47 45 5f 46 49 4c 45 20 20 20 20 20 20 20 31 0a GE_FILE 1.
c71b0 23 20 69 66 6e 64 65 66 20 5f 46 49 4c 45 5f 4f # ifndef _FILE_O
c71c0 46 46 53 45 54 5f 42 49 54 53 0a 23 20 20 20 64 FFSET_BITS.# d
c71d0 65 66 69 6e 65 20 5f 46 49 4c 45 5f 4f 46 46 53 efine _FILE_OFFS
c71e0 45 54 5f 42 49 54 53 20 36 34 0a 23 20 65 6e 64 ET_BITS 64.# end
c71f0 69 66 0a 23 20 64 65 66 69 6e 65 20 5f 4c 41 52 if.# define _LAR
c7200 47 45 46 49 4c 45 5f 53 4f 55 52 43 45 20 31 0a GEFILE_SOURCE 1.
c7210 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 73 74 #endif../*.** st
c7220 61 6e 64 61 72 64 20 69 6e 63 6c 75 64 65 20 66 andard include f
c7230 69 6c 65 73 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 64 iles..*/.#includ
c7240 65 20 3c 73 79 73 2f 74 79 70 65 73 2e 68 3e 0a e <sys/types.h>.
c7250 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 73 74 #include <sys/st
c7260 61 74 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c at.h>.#include <
c7270 66 63 6e 74 6c 2e 68 3e 0a 23 69 6e 63 6c 75 64 fcntl.h>.#includ
c7280 65 20 3c 75 6e 69 73 74 64 2e 68 3e 0a 23 69 6e e <unistd.h>.#in
c7290 63 6c 75 64 65 20 3c 73 79 73 2f 74 69 6d 65 2e clude <sys/time.
c72a0 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 65 72 72 h>.#include <err
c72b0 6e 6f 2e 68 3e 0a 0a 23 69 66 20 53 51 4c 49 54 no.h>..#if SQLIT
c72c0 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 E_ENABLE_LOCKING
c72d0 5f 53 54 59 4c 45 0a 23 20 69 6e 63 6c 75 64 65 _STYLE.# include
c72e0 20 3c 73 79 73 2f 69 6f 63 74 6c 2e 68 3e 0a 23 <sys/ioctl.h>.#
c72f0 20 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 23 if OS_VXWORKS.#
c7300 20 20 69 6e 63 6c 75 64 65 20 3c 73 65 6d 61 70 include <semap
c7310 68 6f 72 65 2e 68 3e 0a 23 20 20 69 6e 63 6c 75 hore.h>.# inclu
c7320 64 65 20 3c 6c 69 6d 69 74 73 2e 68 3e 0a 23 20 de <limits.h>.#
c7330 65 6c 73 65 0a 23 20 20 69 6e 63 6c 75 64 65 20 else.# include
c7340 3c 73 79 73 2f 66 69 6c 65 2e 68 3e 0a 23 20 20 <sys/file.h>.#
c7350 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 70 61 72 include <sys/par
c7360 61 6d 2e 68 3e 0a 23 20 20 69 6e 63 6c 75 64 65 am.h>.# include
c7370 20 3c 73 79 73 2f 6d 6f 75 6e 74 2e 68 3e 0a 23 <sys/mount.h>.#
c7380 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 20 2f 2a endif.#endif /*
c7390 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c SQLITE_ENABLE_L
c73a0 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a OCKING_STYLE */.
c73b0 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72 65 ./*.** If we are
c73c0 20 74 6f 20 62 65 20 74 68 72 65 61 64 2d 73 61 to be thread-sa
c73d0 66 65 2c 20 69 6e 63 6c 75 64 65 20 74 68 65 20 fe, include the
c73e0 70 74 68 72 65 61 64 73 20 68 65 61 64 65 72 20 pthreads header
c73f0 61 6e 64 20 64 65 66 69 6e 65 0a 2a 2a 20 74 68 and define.** th
c7400 65 20 53 51 4c 49 54 45 5f 55 4e 49 58 5f 54 48 e SQLITE_UNIX_TH
c7410 52 45 41 44 53 20 6d 61 63 72 6f 2e 0a 2a 2f 0a READS macro..*/.
c7420 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 #if SQLITE_THREA
c7430 44 53 41 46 45 0a 23 20 64 65 66 69 6e 65 20 53 DSAFE.# define S
c7440 51 4c 49 54 45 5f 55 4e 49 58 5f 54 48 52 45 41 QLITE_UNIX_THREA
c7450 44 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a DS 1.#endif../*.
c7460 2a 2a 20 44 65 66 61 75 6c 74 20 70 65 72 6d 69 ** Default permi
c7470 73 73 69 6f 6e 73 20 77 68 65 6e 20 63 72 65 61 ssions when crea
c7480 74 69 6e 67 20 61 20 6e 65 77 20 66 69 6c 65 0a ting a new file.
c7490 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
c74a0 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50 E_DEFAULT_FILE_P
c74b0 45 52 4d 49 53 53 49 4f 4e 53 0a 23 20 64 65 66 ERMISSIONS.# def
c74c0 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 ine SQLITE_DEFAU
c74d0 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53 53 49 LT_FILE_PERMISSI
c74e0 4f 4e 53 20 30 36 34 34 0a 23 65 6e 64 69 66 0a ONS 0644.#endif.
c74f0 0a 2f 2a 0a 20 2a 2a 20 44 65 66 61 75 6c 74 20 ./*. ** Default
c7500 70 65 72 6d 69 73 73 69 6f 6e 73 20 77 68 65 6e permissions when
c7510 20 63 72 65 61 74 69 6e 67 20 61 75 74 6f 20 70 creating auto p
c7520 72 6f 78 79 20 64 69 72 0a 20 2a 2f 0a 23 69 66 roxy dir. */.#if
c7530 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 ndef SQLITE_DEFA
c7540 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f 50 45 52 ULT_PROXYDIR_PER
c7550 4d 49 53 53 49 4f 4e 53 0a 23 20 64 65 66 69 6e MISSIONS.# defin
c7560 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 e SQLITE_DEFAULT
c7570 5f 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 _PROXYDIR_PERMIS
c7580 53 49 4f 4e 53 20 30 37 35 35 0a 23 65 6e 64 69 SIONS 0755.#endi
c7590 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d f../*.** Maximum
c75a0 20 73 75 70 70 6f 72 74 65 64 20 70 61 74 68 2d supported path-
c75b0 6c 65 6e 67 74 68 2e 0a 2a 2f 0a 23 64 65 66 69 length..*/.#defi
c75c0 6e 65 20 4d 41 58 5f 50 41 54 48 4e 41 4d 45 20 ne MAX_PATHNAME
c75d0 35 31 32 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 6c 79 20 512../*.** Only
c75e0 73 65 74 20 74 68 65 20 6c 61 73 74 45 72 72 6e set the lastErrn
c75f0 6f 20 69 66 20 74 68 65 20 65 72 72 6f 72 20 63 o if the error c
c7600 6f 64 65 20 69 73 20 61 20 72 65 61 6c 20 65 72 ode is a real er
c7610 72 6f 72 20 61 6e 64 20 6e 6f 74 20 0a 2a 2a 20 ror and not .**
c7620 61 20 6e 6f 72 6d 61 6c 20 65 78 70 65 63 74 65 a normal expecte
c7630 64 20 72 65 74 75 72 6e 20 63 6f 64 65 20 6f 66 d return code of
c7640 20 53 51 4c 49 54 45 5f 42 55 53 59 20 6f 72 20 SQLITE_BUSY or
c7650 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2f 0a 23 64 65 SQLITE_OK.*/.#de
c7660 66 69 6e 65 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 fine IS_LOCK_ERR
c7670 4f 52 28 78 29 20 20 28 28 78 20 21 3d 20 53 51 OR(x) ((x != SQ
c7680 4c 49 54 45 5f 4f 4b 29 20 26 26 20 28 78 20 21 LITE_OK) && (x !
c7690 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 29 29 0a = SQLITE_BUSY)).
c76a0 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 ../*.** Sometime
c76b0 73 2c 20 61 66 74 65 72 20 61 20 66 69 6c 65 20 s, after a file
c76c0 68 61 6e 64 6c 65 20 69 73 20 63 6c 6f 73 65 64 handle is closed
c76d0 20 62 79 20 53 51 4c 69 74 65 2c 20 74 68 65 20 by SQLite, the
c76e0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 0a file descriptor.
c76f0 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 63 6c 6f ** cannot be clo
c7700 73 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e sed immediately.
c7710 20 49 6e 20 74 68 65 73 65 20 63 61 73 65 73 2c In these cases,
c7720 20 69 6e 73 74 61 6e 63 65 73 20 6f 66 20 74 68 instances of th
c7730 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 e following.** s
c7740 74 72 75 63 74 75 72 65 20 61 72 65 20 75 73 65 tructure are use
c7750 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 66 d to store the f
c7760 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 77 ile descriptor w
c7770 68 69 6c 65 20 77 61 69 74 69 6e 67 20 66 6f 72 hile waiting for
c7780 20 61 6e 0a 2a 2a 20 6f 70 70 6f 72 74 75 6e 69 an.** opportuni
c7790 74 79 20 74 6f 20 65 69 74 68 65 72 20 63 6c 6f ty to either clo
c77a0 73 65 20 6f 72 20 72 65 75 73 65 20 69 74 2e 0a se or reuse it..
c77b0 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
c77c0 74 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 55 t UnixUnusedFd U
c77d0 6e 69 78 55 6e 75 73 65 64 46 64 3b 0a 73 74 72 nixUnusedFd;.str
c77e0 75 63 74 20 55 6e 69 78 55 6e 75 73 65 64 46 64 uct UnixUnusedFd
c77f0 20 7b 0a 20 20 69 6e 74 20 66 64 3b 20 20 20 20 {. int fd;
c7800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
c7810 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f * File descripto
c7820 72 20 74 6f 20 63 6c 6f 73 65 20 2a 2f 0a 20 20 r to close */.
c7830 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20 int flags;
c7840 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 /* Fla
c7850 67 73 20 74 68 69 73 20 66 69 6c 65 20 64 65 73 gs this file des
c7860 63 72 69 70 74 6f 72 20 77 61 73 20 6f 70 65 6e criptor was open
c7870 65 64 20 77 69 74 68 20 2a 2f 0a 20 20 55 6e 69 ed with */. Uni
c7880 78 55 6e 75 73 65 64 46 64 20 2a 70 4e 65 78 74 xUnusedFd *pNext
c7890 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 75 ; /* Next u
c78a0 6e 75 73 65 64 20 66 69 6c 65 20 64 65 73 63 72 nused file descr
c78b0 69 70 74 6f 72 20 6f 6e 20 73 61 6d 65 20 66 69 iptor on same fi
c78c0 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 le */.};../*.**
c78d0 54 68 65 20 75 6e 69 78 46 69 6c 65 20 73 74 72 The unixFile str
c78e0 75 63 74 75 72 65 20 69 73 20 73 75 62 63 6c 61 ucture is subcla
c78f0 73 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 ss of sqlite3_fi
c7900 6c 65 20 73 70 65 63 69 66 69 63 20 74 6f 20 74 le specific to t
c7910 68 65 20 75 6e 69 78 0a 2a 2a 20 56 46 53 20 69 he unix.** VFS i
c7920 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a mplementations..
c7930 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
c7940 74 20 75 6e 69 78 46 69 6c 65 20 75 6e 69 78 46 t unixFile unixF
c7950 69 6c 65 3b 0a 73 74 72 75 63 74 20 75 6e 69 78 ile;.struct unix
c7960 46 69 6c 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 File {. sqlite3
c7970 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 63 6f 6e 73 _io_methods cons
c7980 74 20 2a 70 4d 65 74 68 6f 64 3b 20 20 2f 2a 20 t *pMethod; /*
c7990 41 6c 77 61 79 73 20 74 68 65 20 66 69 72 73 74 Always the first
c79a0 20 65 6e 74 72 79 20 2a 2f 0a 20 20 73 74 72 75 entry */. stru
c79b0 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a ct unixOpenCnt *
c79c0 70 4f 70 65 6e 3b 20 20 20 20 20 20 20 2f 2a 20 pOpen; /*
c79d0 49 6e 66 6f 20 61 62 6f 75 74 20 61 6c 6c 20 6f Info about all o
c79e0 70 65 6e 20 66 64 27 73 20 6f 6e 20 74 68 69 73 pen fd's on this
c79f0 20 69 6e 6f 64 65 20 2a 2f 0a 20 20 73 74 72 75 inode */. stru
c7a00 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 ct unixLockInfo
c7a10 2a 70 4c 6f 63 6b 3b 20 20 20 20 20 20 2f 2a 20 *pLock; /*
c7a20 49 6e 66 6f 20 61 62 6f 75 74 20 6c 6f 63 6b 73 Info about locks
c7a30 20 6f 6e 20 74 68 69 73 20 69 6e 6f 64 65 20 2a on this inode *
c7a40 2f 0a 20 20 69 6e 74 20 68 3b 20 20 20 20 20 20 /. int h;
c7a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c7a60 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 /* The file
c7a70 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 descriptor */.
c7a80 20 69 6e 74 20 64 69 72 66 64 3b 20 20 20 20 20 int dirfd;
c7a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c7aa0 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69 /* File descri
c7ab0 70 74 6f 72 20 66 6f 72 20 74 68 65 20 64 69 72 ptor for the dir
c7ac0 65 63 74 6f 72 79 20 2a 2f 0a 20 20 75 6e 73 69 ectory */. unsi
c7ad0 67 6e 65 64 20 63 68 61 72 20 6c 6f 63 6b 74 79 gned char lockty
c7ae0 70 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 pe; /*
c7af0 54 68 65 20 74 79 70 65 20 6f 66 20 6c 6f 63 6b The type of lock
c7b00 20 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 66 64 held on this fd
c7b10 20 2a 2f 0a 20 20 69 6e 74 20 6c 61 73 74 45 72 */. int lastEr
c7b20 72 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 rno;
c7b30 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 75 6e /* The un
c7b40 69 78 20 65 72 72 6e 6f 20 66 72 6f 6d 20 74 68 ix errno from th
c7b50 65 20 6c 61 73 74 20 49 2f 4f 20 65 72 72 6f 72 e last I/O error
c7b60 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 6c 6f 63 6b */. void *lock
c7b70 69 6e 67 43 6f 6e 74 65 78 74 3b 20 20 20 20 20 ingContext;
c7b80 20 20 20 20 20 20 20 2f 2a 20 4c 6f 63 6b 69 6e /* Lockin
c7b90 67 20 73 74 79 6c 65 20 73 70 65 63 69 66 69 63 g style specific
c7ba0 20 73 74 61 74 65 20 2a 2f 0a 20 20 55 6e 69 78 state */. Unix
c7bb0 55 6e 75 73 65 64 46 64 20 2a 70 55 6e 75 73 65 UnusedFd *pUnuse
c7bc0 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 d; /*
c7bd0 50 72 65 2d 61 6c 6c 6f 63 61 74 65 64 20 55 6e Pre-allocated Un
c7be0 69 78 55 6e 75 73 65 64 46 64 20 2a 2f 0a 20 20 ixUnusedFd */.
c7bf0 69 6e 74 20 66 69 6c 65 46 6c 61 67 73 3b 20 20 int fileFlags;
c7c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c7c10 20 2f 2a 20 4d 69 73 63 65 6c 6c 61 6e 6f 75 73 /* Miscellanous
c7c20 20 66 6c 61 67 73 20 2a 2f 0a 23 69 66 20 53 51 flags */.#if SQ
c7c30 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b LITE_ENABLE_LOCK
c7c40 49 4e 47 5f 53 54 59 4c 45 0a 20 20 69 6e 74 20 ING_STYLE. int
c7c50 6f 70 65 6e 46 6c 61 67 73 3b 20 20 20 20 20 20 openFlags;
c7c60 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
c7c70 54 68 65 20 66 6c 61 67 73 20 73 70 65 63 69 66 The flags specif
c7c80 69 65 64 20 61 74 20 6f 70 65 6e 28 29 20 2a 2f ied at open() */
c7c90 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 .#endif.#if SQLI
c7ca0 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 TE_THREADSAFE &&
c7cb0 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 defined(__linux
c7cc0 5f 5f 29 0a 20 20 70 74 68 72 65 61 64 5f 74 20 __). pthread_t
c7cd0 74 69 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 tid;
c7ce0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 68 /* The th
c7cf0 72 65 61 64 20 74 68 61 74 20 22 6f 77 6e 73 22 read that "owns"
c7d00 20 74 68 69 73 20 75 6e 69 78 46 69 6c 65 20 2a this unixFile *
c7d10 2f 0a 23 65 6e 64 69 66 0a 23 69 66 20 4f 53 5f /.#endif.#if OS_
c7d20 56 58 57 4f 52 4b 53 0a 20 20 69 6e 74 20 69 73 VXWORKS. int is
c7d30 44 65 6c 65 74 65 3b 20 20 20 20 20 20 20 20 20 Delete;
c7d40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 /* De
c7d50 6c 65 74 65 20 6f 6e 20 63 6c 6f 73 65 20 69 66 lete on close if
c7d60 20 74 72 75 65 20 2a 2f 0a 20 20 73 74 72 75 63 true */. struc
c7d70 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 t vxworksFileId
c7d80 2a 70 49 64 3b 20 20 20 20 20 20 20 2f 2a 20 55 *pId; /* U
c7d90 6e 69 71 75 65 20 66 69 6c 65 20 49 44 20 2a 2f nique file ID */
c7da0 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 .#endif.#ifndef
c7db0 4e 44 45 42 55 47 0a 20 20 2f 2a 20 54 68 65 20 NDEBUG. /* The
c7dc0 6e 65 78 74 20 67 72 6f 75 70 20 6f 66 20 76 61 next group of va
c7dd0 72 69 61 62 6c 65 73 20 61 72 65 20 75 73 65 64 riables are used
c7de0 20 74 6f 20 74 72 61 63 6b 20 77 68 65 74 68 65 to track whethe
c7df0 72 20 6f 72 20 6e 6f 74 20 74 68 65 0a 20 20 2a r or not the. *
c7e00 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f * transaction co
c7e10 75 6e 74 65 72 20 69 6e 20 62 79 74 65 73 20 32 unter in bytes 2
c7e20 34 2d 32 37 20 6f 66 20 64 61 74 61 62 61 73 65 4-27 of database
c7e30 20 66 69 6c 65 73 20 61 72 65 20 75 70 64 61 74 files are updat
c7e40 65 64 0a 20 20 2a 2a 20 77 68 65 6e 65 76 65 72 ed. ** whenever
c7e50 20 61 6e 79 20 70 61 72 74 20 6f 66 20 74 68 65 any part of the
c7e60 20 64 61 74 61 62 61 73 65 20 63 68 61 6e 67 65 database change
c7e70 73 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f 6e s. An assertion
c7e80 20 66 61 75 6c 74 20 77 69 6c 6c 0a 20 20 2a 2a fault will. **
c7e90 20 6f 63 63 75 72 20 69 66 20 61 20 66 69 6c 65 occur if a file
c7ea0 20 69 73 20 75 70 64 61 74 65 64 20 77 69 74 68 is updated with
c7eb0 6f 75 74 20 61 6c 73 6f 20 75 70 64 61 74 69 6e out also updatin
c7ec0 67 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f g the transactio
c7ed0 6e 0a 20 20 2a 2a 20 63 6f 75 6e 74 65 72 2e 20 n. ** counter.
c7ee0 20 54 68 69 73 20 74 65 73 74 20 69 73 20 6d 61 This test is ma
c7ef0 64 65 20 74 6f 20 61 76 6f 69 64 20 6e 65 77 20 de to avoid new
c7f00 70 72 6f 62 6c 65 6d 73 20 73 69 6d 69 6c 61 72 problems similar
c7f10 20 74 6f 20 74 68 65 0a 20 20 2a 2a 20 6f 6e 65 to the. ** one
c7f20 20 64 65 73 63 72 69 62 65 64 20 62 79 20 74 69 described by ti
c7f30 63 6b 65 74 20 23 33 35 38 34 2e 20 0a 20 20 2a cket #3584. . *
c7f40 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 /. unsigned cha
c7f50 72 20 74 72 61 6e 73 43 6e 74 72 43 68 6e 67 3b r transCntrChng;
c7f60 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 /* True if th
c7f70 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f e transaction co
c7f80 75 6e 74 65 72 20 63 68 61 6e 67 65 64 20 2a 2f unter changed */
c7f90 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
c7fa0 20 64 62 55 70 64 61 74 65 3b 20 20 20 20 20 20 dbUpdate;
c7fb0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6e 79 /* True if any
c7fc0 20 70 61 72 74 20 6f 66 20 64 61 74 61 62 61 73 part of databas
c7fd0 65 20 66 69 6c 65 20 63 68 61 6e 67 65 64 20 2a e file changed *
c7fe0 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 /. unsigned cha
c7ff0 72 20 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 3b r inNormalWrite;
c8000 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 6e /* True if in
c8010 20 61 20 6e 6f 72 6d 61 6c 20 77 72 69 74 65 20 a normal write
c8020 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 23 65 6e operation */.#en
c8030 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 dif.#ifdef SQLIT
c8040 45 5f 54 45 53 54 0a 20 20 2f 2a 20 49 6e 20 74 E_TEST. /* In t
c8050 65 73 74 20 6d 6f 64 65 2c 20 69 6e 63 72 65 61 est mode, increa
c8060 73 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 se the size of t
c8070 68 69 73 20 73 74 72 75 63 74 75 72 65 20 61 20 his structure a
c8080 62 69 74 20 73 6f 20 74 68 61 74 20 0a 20 20 2a bit so that . *
c8090 2a 20 69 74 20 69 73 20 6c 61 72 67 65 72 20 74 * it is larger t
c80a0 68 61 6e 20 74 68 65 20 73 74 72 75 63 74 20 43 han the struct C
c80b0 72 61 73 68 46 69 6c 65 20 64 65 66 69 6e 65 64 rashFile defined
c80c0 20 69 6e 20 74 65 73 74 36 2e 63 2e 0a 20 20 2a in test6.c.. *
c80d0 2f 0a 20 20 63 68 61 72 20 61 50 61 64 64 69 6e /. char aPaddin
c80e0 67 5b 33 32 5d 3b 0a 23 65 6e 64 69 66 0a 7d 3b g[32];.#endif.};
c80f0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ../*.** The foll
c8100 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 64 65 66 owing macros def
c8110 69 6e 65 20 62 69 74 73 20 69 6e 20 75 6e 69 78 ine bits in unix
c8120 46 69 6c 65 2e 66 69 6c 65 46 6c 61 67 73 0a 2a File.fileFlags.*
c8130 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
c8140 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b _WHOLE_FILE_LOCK
c8150 49 4e 47 20 20 30 78 30 30 30 31 20 20 20 2f 2a ING 0x0001 /*
c8160 20 55 73 65 20 77 68 6f 6c 65 2d 66 69 6c 65 20 Use whole-file
c8170 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 0a 2f 2a 0a 2a locking */../*.*
c8180 2a 20 49 6e 63 6c 75 64 65 20 63 6f 64 65 20 74 * Include code t
c8190 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f hat is common to
c81a0 20 61 6c 6c 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 all os_*.c file
c81b0 73 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a s.*/./**********
c81c0 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 73 5f **** Include os_
c81d0 63 6f 6d 6d 6f 6e 2e 68 20 69 6e 20 74 68 65 20 common.h in the
c81e0 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 75 6e 69 middle of os_uni
c81f0 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a x.c ************
c8200 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
c8210 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
c8220 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a os_common.h ****
c8230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8250 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 ***/./*.** 2004
c8260 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 May 22.**.** The
c8270 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
c8280 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
c8290 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
c82a0 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
c82b0 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
c82c0 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
c82d0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
c82e0 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
c82f0 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
c8300 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
c8310 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
c8320 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
c8330 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
c8340 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
c8350 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
c8360 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
c8370 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
c8380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c83a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c83b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c83c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
c83d0 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
c83e0 69 6e 73 20 6d 61 63 72 6f 73 20 61 6e 64 20 61 ins macros and a
c83f0 20 6c 69 74 74 6c 65 20 62 69 74 20 6f 66 20 63 little bit of c
c8400 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d ode that is comm
c8410 6f 6e 20 74 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 20 on to.** all of
c8420 74 68 65 20 70 6c 61 74 66 6f 72 6d 2d 73 70 65 the platform-spe
c8430 63 69 66 69 63 20 66 69 6c 65 73 20 28 6f 73 5f cific files (os_
c8440 2a 2e 63 29 20 61 6e 64 20 69 73 20 23 69 6e 63 *.c) and is #inc
c8450 6c 75 64 65 64 20 69 6e 74 6f 20 74 68 6f 73 65 luded into those
c8460 0a 2a 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a .** files..**.**
c8470 20 54 68 69 73 20 66 69 6c 65 20 73 68 6f 75 6c This file shoul
c8480 64 20 62 65 20 23 69 6e 63 6c 75 64 65 64 20 62 d be #included b
c8490 79 20 74 68 65 20 6f 73 5f 2a 2e 63 20 66 69 6c y the os_*.c fil
c84a0 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20 69 73 20 es only. It is
c84b0 6e 6f 74 20 61 0a 2a 2a 20 67 65 6e 65 72 61 6c not a.** general
c84c0 20 70 75 72 70 6f 73 65 20 68 65 61 64 65 72 20 purpose header
c84d0 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a file..**.** $Id:
c84e0 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 2c 76 20 31 os_common.h,v 1
c84f0 2e 33 38 20 32 30 30 39 2f 30 32 2f 32 34 20 31 .38 2009/02/24 1
c8500 38 3a 34 30 3a 35 30 20 64 61 6e 69 65 6c 6b 31 8:40:50 danielk1
c8510 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 977 Exp $.*/.#if
c8520 6e 64 65 66 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f ndef _OS_COMMON_
c8530 48 5f 0a 23 64 65 66 69 6e 65 20 5f 4f 53 5f 43 H_.#define _OS_C
c8540 4f 4d 4d 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 OMMON_H_../*.**
c8550 41 74 20 6c 65 61 73 74 20 74 77 6f 20 62 75 67 At least two bug
c8560 73 20 68 61 76 65 20 73 6c 69 70 70 65 64 20 69 s have slipped i
c8570 6e 20 62 65 63 61 75 73 65 20 77 65 20 63 68 61 n because we cha
c8580 6e 67 65 64 20 74 68 65 20 4d 45 4d 4f 52 59 5f nged the MEMORY_
c8590 44 45 42 55 47 0a 2a 2a 20 6d 61 63 72 6f 20 74 DEBUG.** macro t
c85a0 6f 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 61 o SQLITE_DEBUG a
c85b0 6e 64 20 73 6f 6d 65 20 6f 6c 64 65 72 20 6d 61 nd some older ma
c85c0 6b 65 66 69 6c 65 73 20 68 61 76 65 20 6e 6f 74 kefiles have not
c85d0 20 79 65 74 20 6d 61 64 65 20 74 68 65 0a 2a 2a yet made the.**
c85e0 20 73 77 69 74 63 68 2e 20 20 54 68 65 20 66 6f switch. The fo
c85f0 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 73 68 6f llowing code sho
c8600 75 6c 64 20 63 61 74 63 68 20 74 68 69 73 20 70 uld catch this p
c8610 72 6f 62 6c 65 6d 20 61 74 20 63 6f 6d 70 69 6c roblem at compil
c8620 65 2d 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 e-time..*/.#ifde
c8630 66 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 23 f MEMORY_DEBUG.#
c8640 20 65 72 72 6f 72 20 22 54 68 65 20 4d 45 4d 4f error "The MEMO
c8650 52 59 5f 44 45 42 55 47 20 6d 61 63 72 6f 20 69 RY_DEBUG macro i
c8660 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20 55 73 65 s obsolete. Use
c8670 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 69 6e SQLITE_DEBUG in
c8680 73 74 65 61 64 2e 22 0a 23 65 6e 64 69 66 0a 0a stead.".#endif..
c8690 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
c86a0 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 BUG.SQLITE_PRIVA
c86b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 53 TE int sqlite3OS
c86c0 54 72 61 63 65 20 3d 20 30 3b 0a 23 64 65 66 69 Trace = 0;.#defi
c86d0 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29 20 20 ne OSTRACE1(X)
c86e0 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 if( sqlit
c86f0 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 e3OSTrace ) sqli
c8700 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 te3DebugPrintf(X
c8710 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
c8720 45 32 28 58 2c 59 29 20 20 20 20 20 20 20 69 66 E2(X,Y) if
c8730 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 ( sqlite3OSTrace
c8740 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 ) sqlite3DebugP
c8750 72 69 6e 74 66 28 58 2c 59 29 0a 23 64 65 66 69 rintf(X,Y).#defi
c8760 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c 59 2c ne OSTRACE3(X,Y,
c8770 5a 29 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 Z) if( sqlit
c8780 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 e3OSTrace ) sqli
c8790 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 te3DebugPrintf(X
c87a0 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 ,Y,Z).#define OS
c87b0 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 20 TRACE4(X,Y,Z,A)
c87c0 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 if( sqlite3OST
c87d0 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 race ) sqlite3De
c87e0 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c bugPrintf(X,Y,Z,
c87f0 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 A).#define OSTRA
c8800 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 20 69 CE5(X,Y,Z,A,B) i
c8810 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 f( sqlite3OSTrac
c8820 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 e ) sqlite3Debug
c8830 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 Printf(X,Y,Z,A,B
c8840 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
c8850 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 20 E6(X,Y,Z,A,B,C)
c8860 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 65 33 \. if(sqlite3
c8870 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 65 33 OSTrace) sqlite3
c8880 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c DebugPrintf(X,Y,
c8890 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 Z,A,B,C).#define
c88a0 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c OSTRACE7(X,Y,Z,
c88b0 41 2c 42 2c 43 2c 44 29 20 5c 0a 20 20 20 20 69 A,B,C,D) \. i
c88c0 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 f(sqlite3OSTrace
c88d0 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 ) sqlite3DebugPr
c88e0 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 intf(X,Y,Z,A,B,C
c88f0 2c 44 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e ,D).#else.#defin
c8900 65 20 4f 53 54 52 41 43 45 31 28 58 29 0a 23 64 e OSTRACE1(X).#d
c8910 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 28 58 efine OSTRACE2(X
c8920 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 ,Y).#define OSTR
c8930 41 43 45 33 28 58 2c 59 2c 5a 29 0a 23 64 65 66 ACE3(X,Y,Z).#def
c8940 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58 2c 59 ine OSTRACE4(X,Y
c8950 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 ,Z,A).#define OS
c8960 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 TRACE5(X,Y,Z,A,B
c8970 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
c8980 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a E6(X,Y,Z,A,B,C).
c8990 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 37 #define OSTRACE7
c89a0 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a (X,Y,Z,A,B,C,D).
c89b0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 #endif../*.** Ma
c89c0 63 72 6f 73 20 66 6f 72 20 70 65 72 66 6f 72 6d cros for perform
c89d0 61 6e 63 65 20 74 72 61 63 69 6e 67 2e 20 20 4e ance tracing. N
c89e0 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f ormally turned o
c89f0 66 66 2e 20 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a ff. Only works.
c8a00 2a 2a 20 6f 6e 20 69 34 38 36 20 68 61 72 64 77 ** on i486 hardw
c8a10 61 72 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 are..*/.#ifdef S
c8a20 51 4c 49 54 45 5f 50 45 52 46 4f 52 4d 41 4e 43 QLITE_PERFORMANC
c8a30 45 5f 54 52 41 43 45 0a 0a 2f 2a 20 0a 2a 2a 20 E_TRACE../* .**
c8a40 68 77 74 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e hwtime.h contain
c8a50 73 20 69 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c s inline assembl
c8a60 65 72 20 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c er code for impl
c8a70 65 6d 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67 ementing .** hig
c8a80 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 h-performance ti
c8a90 6d 69 6e 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a ming routines..*
c8aa0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
c8ab0 2a 20 49 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 * Include hwtime
c8ac0 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 .h in the middle
c8ad0 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 of os_common.h
c8ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8af0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
c8b00 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68 77 74 * Begin file hwt
c8b10 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ime.h **********
c8b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8b40 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 /./*.** 2008 May
c8b50 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 27.**.** The au
c8b60 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
c8b70 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
c8b80 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
c8b90 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
c8ba0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
c8bb0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
c8bc0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
c8bd0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
c8be0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
c8bf0 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
c8c00 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
c8c10 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
c8c20 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
c8c30 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
c8c40 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
c8c50 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
c8c60 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
c8c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8cb0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 *******.**.** Th
c8cc0 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
c8cd0 20 69 6e 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 inline asm code
c8ce0 20 66 6f 72 20 72 65 74 72 69 65 76 69 6e 67 20 for retrieving
c8cf0 22 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 "high-performanc
c8d00 65 22 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 e".** counters f
c8d10 6f 72 20 78 38 36 20 63 6c 61 73 73 20 43 50 55 or x86 class CPU
c8d20 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 77 s..**.** $Id: hw
c8d30 74 69 6d 65 2e 68 2c 76 20 31 2e 33 20 32 30 30 time.h,v 1.3 200
c8d40 38 2f 30 38 2f 30 31 20 31 34 3a 33 33 3a 31 35 8/08/01 14:33:15
c8d50 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a shane Exp $.*/.
c8d60 23 69 66 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f #ifndef _HWTIME_
c8d70 48 5f 0a 23 64 65 66 69 6e 65 20 5f 48 57 54 49 H_.#define _HWTI
c8d80 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 ME_H_../*.** The
c8d90 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 following routi
c8da0 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e ne only works on
c8db0 20 70 65 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28 pentium-class (
c8dc0 6f 72 20 6e 65 77 65 72 29 20 70 72 6f 63 65 73 or newer) proces
c8dd0 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73 sors..** It uses
c8de0 20 74 68 65 20 52 44 54 53 43 20 6f 70 63 6f 64 the RDTSC opcod
c8df0 65 20 74 6f 20 72 65 61 64 20 74 68 65 20 63 79 e to read the cy
c8e00 63 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 cle count value
c8e10 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 out of the.** pr
c8e20 6f 63 65 73 73 6f 72 20 61 6e 64 20 72 65 74 75 ocessor and retu
c8e30 72 6e 73 20 74 68 61 74 20 76 61 6c 75 65 2e 20 rns that value.
c8e40 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 This can be use
c8e50 64 20 66 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a d for high-res.*
c8e60 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a * profiling..*/.
c8e70 23 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 #if (defined(__G
c8e80 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 NUC__) || define
c8e90 64 28 5f 4d 53 43 5f 56 45 52 29 29 20 26 26 20 d(_MSC_VER)) &&
c8ea0 5c 0a 20 20 20 20 20 20 28 64 65 66 69 6e 65 64 \. (defined
c8eb0 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 (i386) || define
c8ec0 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 d(__i386__) || d
c8ed0 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29 efined(_M_IX86))
c8ee0 0a 0a 20 20 23 69 66 20 64 65 66 69 6e 65 64 28 .. #if defined(
c8ef0 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 __GNUC__).. __i
c8f00 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 nline__ sqlite_u
c8f10 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 int64 sqlite3Hwt
c8f20 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 ime(void){.
c8f30 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c unsigned int lo,
c8f40 20 68 69 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f hi;. __asm_
c8f50 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 _ __volatile__ (
c8f60 22 72 64 74 73 63 22 20 3a 20 22 3d 61 22 20 28 "rdtsc" : "=a" (
c8f70 6c 6f 29 2c 20 22 3d 64 22 20 28 68 69 29 29 3b lo), "=d" (hi));
c8f80 0a 20 20 20 20 20 72 65 74 75 72 6e 20 28 73 71 . return (sq
c8f90 6c 69 74 65 5f 75 69 6e 74 36 34 29 68 69 20 3c lite_uint64)hi <
c8fa0 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a < 32 | lo;. }..
c8fb0 20 20 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 #elif defined(
c8fc0 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 _MSC_VER).. __d
c8fd0 65 63 6c 73 70 65 63 28 6e 61 6b 65 64 29 20 5f eclspec(naked) _
c8fe0 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75 _inline sqlite_u
c8ff0 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20 73 71 int64 __cdecl sq
c9000 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 lite3Hwtime(void
c9010 29 7b 0a 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a ){. __asm {.
c9020 20 20 20 20 20 20 20 20 72 64 74 73 63 0a 20 20 rdtsc.
c9030 20 20 20 20 20 20 72 65 74 20 20 20 20 20 20 20 ret
c9040 3b 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 61 ; return value a
c9050 74 20 45 44 58 3a 45 41 58 0a 20 20 20 20 20 7d t EDX:EAX. }
c9060 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a . }.. #endif..
c9070 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f #elif (defined(_
c9080 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 _GNUC__) && defi
c9090 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 ned(__x86_64__))
c90a0 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 .. __inline__ s
c90b0 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c qlite_uint64 sql
c90c0 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 ite3Hwtime(void)
c90d0 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 {. unsigned
c90e0 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20 long val;.
c90f0 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 __asm__ __volat
c9100 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a ile__ ("rdtsc" :
c9110 20 22 3d 41 22 20 28 76 61 6c 29 29 3b 0a 20 20 "=A" (val));.
c9120 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a return val;.
c9130 20 20 7d 0a 20 0a 23 65 6c 69 66 20 28 64 65 66 }. .#elif (def
c9140 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 ined(__GNUC__) &
c9150 26 20 64 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f & defined(__ppc_
c9160 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f _)).. __inline_
c9170 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 _ sqlite_uint64
c9180 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f sqlite3Hwtime(vo
c9190 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 id){. unsig
c91a0 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 ned long long re
c91b0 74 76 61 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69 tval;. unsi
c91c0 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a gned long junk;.
c91d0 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f __asm__ __
c91e0 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c volatile__ ("\n\
c91f0 0a 20 20 20 20 20 20 20 20 20 20 31 3a 20 20 20 . 1:
c9200 20 20 20 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c mftbu %1\n\
c9210 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
c9220 20 20 20 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e mftb %L0\n
c9230 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
c9240 20 20 20 20 6d 66 74 62 75 20 20 20 25 30 5c 6e mftbu %0\n
c9250 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
c9260 20 20 20 20 63 6d 70 77 20 20 20 20 25 30 2c 25 cmpw %0,%
c9270 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 1\n\.
c9280 20 20 20 20 20 20 20 62 6e 65 20 20 20 20 20 31 bne 1
c9290 62 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 b".
c92a0 20 20 20 20 20 3a 20 22 3d 72 22 20 28 72 65 74 : "=r" (ret
c92b0 76 61 6c 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b val), "=r" (junk
c92c0 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ));. return
c92d0 20 72 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 retval;. }..#e
c92e0 6c 73 65 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65 lse.. #error Ne
c92f0 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ed implementatio
c9300 6e 20 6f 66 20 73 71 6c 69 74 65 33 48 77 74 69 n of sqlite3Hwti
c9310 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c me() for your pl
c9320 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 atform... /*.
c9330 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69 ** To compile wi
c9340 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69 thout implementi
c9350 6e 67 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 ng sqlite3Hwtime
c9360 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 () for your plat
c9370 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 form,. ** you c
c9380 61 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 61 62 an remove the ab
c9390 6f 76 65 20 23 65 72 72 6f 72 20 61 6e 64 20 75 ove #error and u
c93a0 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 se the following
c93b0 0a 20 20 2a 2a 20 73 74 75 62 20 66 75 6e 63 74 . ** stub funct
c93c0 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c ion. You will l
c93d0 6f 73 65 20 74 69 6d 69 6e 67 20 73 75 70 70 6f ose timing suppo
c93e0 72 74 20 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a rt for many. **
c93f0 20 6f 66 20 74 68 65 20 64 65 62 75 67 67 69 6e of the debuggin
c9400 67 20 61 6e 64 20 74 65 73 74 69 6e 67 20 75 74 g and testing ut
c9410 69 6c 69 74 69 65 73 2c 20 62 75 74 20 69 74 20 ilities, but it
c9420 73 68 6f 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c should at. ** l
c9430 65 61 73 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64 east compile and
c9440 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 run.. */.SQLIT
c9450 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c 69 E_PRIVATE sqli
c9460 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 te_uint64 sqlite
c9470 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 20 72 3Hwtime(void){ r
c9480 65 74 75 72 6e 20 28 28 73 71 6c 69 74 65 5f 75 eturn ((sqlite_u
c9490 69 6e 74 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e int64)0); }..#en
c94a0 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 dif..#endif /* !
c94b0 64 65 66 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f defined(_HWTIME_
c94c0 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a H_) */../*******
c94d0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 ******* End of h
c94e0 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a wtime.h ********
c94f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c9500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c9510 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
c9520 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 ******* Continui
c9530 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 ng where we left
c9540 20 6f 66 66 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f off in os_commo
c9550 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n.h ************
c9560 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61 74 69 63 20 ******/..static
c9570 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f sqlite_uint64 g_
c9580 73 74 61 72 74 3b 0a 73 74 61 74 69 63 20 73 71 start;.static sq
c9590 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 65 6c lite_uint64 g_el
c95a0 61 70 73 65 64 3b 0a 23 64 65 66 69 6e 65 20 54 apsed;.#define T
c95b0 49 4d 45 52 5f 53 54 41 52 54 20 20 20 20 20 20 IMER_START
c95c0 20 67 5f 73 74 61 72 74 3d 73 71 6c 69 74 65 33 g_start=sqlite3
c95d0 48 77 74 69 6d 65 28 29 0a 23 64 65 66 69 6e 65 Hwtime().#define
c95e0 20 54 49 4d 45 52 5f 45 4e 44 20 20 20 20 20 20 TIMER_END
c95f0 20 20 20 67 5f 65 6c 61 70 73 65 64 3d 73 71 6c g_elapsed=sql
c9600 69 74 65 33 48 77 74 69 6d 65 28 29 2d 67 5f 73 ite3Hwtime()-g_s
c9610 74 61 72 74 0a 23 64 65 66 69 6e 65 20 54 49 4d tart.#define TIM
c9620 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20 20 67 ER_ELAPSED g
c9630 5f 65 6c 61 70 73 65 64 0a 23 65 6c 73 65 0a 23 _elapsed.#else.#
c9640 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 define TIMER_STA
c9650 52 54 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 RT.#define TIMER
c9660 5f 45 4e 44 0a 23 64 65 66 69 6e 65 20 54 49 4d _END.#define TIM
c9670 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20 20 28 ER_ELAPSED (
c9680 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 (sqlite_uint64)0
c9690 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ).#endif../*.**
c96a0 49 66 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 If we compile wi
c96b0 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45 th the SQLITE_TE
c96c0 53 54 20 6d 61 63 72 6f 20 73 65 74 2c 20 74 68 ST macro set, th
c96d0 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 en the following
c96e0 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64 block.** of cod
c96f0 65 20 77 69 6c 6c 20 67 69 76 65 20 75 73 20 74 e will give us t
c9700 68 65 20 61 62 69 6c 69 74 79 20 74 6f 20 73 69 he ability to si
c9710 6d 75 6c 61 74 65 20 61 20 64 69 73 6b 20 49 2f mulate a disk I/
c9720 4f 20 65 72 72 6f 72 2e 20 20 54 68 69 73 0a 2a O error. This.*
c9730 2a 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 * is used for te
c9740 73 74 69 6e 67 20 74 68 65 20 49 2f 4f 20 72 65 sting the I/O re
c9750 63 6f 76 65 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f covery logic..*/
c9760 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
c9770 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 EST.SQLITE_API i
c9780 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 nt sqlite3_io_er
c9790 72 6f 72 5f 68 69 74 20 3d 20 30 3b 20 20 20 20 ror_hit = 0;
c97a0 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c /* Total
c97b0 20 6e 75 6d 62 65 72 20 6f 66 20 49 2f 4f 20 45 number of I/O E
c97c0 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f rrors */.SQLITE_
c97d0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
c97e0 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74 io_error_hardhit
c97f0 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 = 0; /*
c9800 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 62 65 Number of non-be
c9810 6e 69 67 6e 20 65 72 72 6f 72 73 20 2a 2f 0a 53 nign errors */.S
c9820 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
c9830 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 lite3_io_error_p
c9840 65 6e 64 69 6e 67 20 3d 20 30 3b 20 20 20 20 20 ending = 0;
c9850 20 20 20 2f 2a 20 43 6f 75 6e 74 20 64 6f 77 6e /* Count down
c9860 20 74 6f 20 66 69 72 73 74 20 49 2f 4f 20 65 72 to first I/O er
c9870 72 6f 72 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ror */.SQLITE_AP
c9880 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f I int sqlite3_io
c9890 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20 3d _error_persist =
c98a0 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 0; /* Tr
c98b0 75 65 20 69 66 20 49 2f 4f 20 65 72 72 6f 72 73 ue if I/O errors
c98c0 20 70 65 72 73 69 73 74 20 2a 2f 0a 53 51 4c 49 persist */.SQLI
c98d0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
c98e0 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 e3_io_error_beni
c98f0 67 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 gn = 0;
c9900 2f 2a 20 54 72 75 65 20 69 66 20 65 72 72 6f 72 /* True if error
c9910 73 20 61 72 65 20 62 65 6e 69 67 6e 20 2a 2f 0a s are benign */.
c9920 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
c9930 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f qlite3_diskfull_
c9940 70 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 53 51 4c pending = 0;.SQL
c9950 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
c9960 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 30 te3_diskfull = 0
c9970 3b 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 ;.#define Simula
c9980 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 teIOErrorBenign(
c9990 58 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 X) sqlite3_io_er
c99a0 72 6f 72 5f 62 65 6e 69 67 6e 3d 28 58 29 0a 23 ror_benign=(X).#
c99b0 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 define SimulateI
c99c0 4f 45 72 72 6f 72 28 43 4f 44 45 29 20 20 5c 0a OError(CODE) \.
c99d0 20 20 69 66 28 20 28 73 71 6c 69 74 65 33 5f 69 if( (sqlite3_i
c99e0 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20 o_error_persist
c99f0 26 26 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 && sqlite3_io_er
c9a00 72 6f 72 5f 68 69 74 29 20 5c 0a 20 20 20 20 20 ror_hit) \.
c9a10 20 20 7c 7c 20 73 71 6c 69 74 65 33 5f 69 6f 5f || sqlite3_io_
c9a20 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 2d 2d 20 error_pending--
c9a30 3d 3d 20 31 20 29 20 20 5c 0a 20 20 20 20 20 20 == 1 ) \.
c9a40 20 20 20 20 20 20 20 20 7b 20 6c 6f 63 61 6c 5f { local_
c9a50 69 6f 65 72 72 28 29 3b 20 43 4f 44 45 3b 20 7d ioerr(); CODE; }
c9a60 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 63 .static void loc
c9a70 61 6c 5f 69 6f 65 72 72 28 29 7b 0a 20 20 49 4f al_ioerr(){. IO
c9a80 54 52 41 43 45 28 28 22 49 4f 45 52 52 5c 6e 22 TRACE(("IOERR\n"
c9a90 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f ));. sqlite3_io
c9aa0 5f 65 72 72 6f 72 5f 68 69 74 2b 2b 3b 0a 20 20 _error_hit++;.
c9ab0 69 66 28 20 21 73 71 6c 69 74 65 33 5f 69 6f 5f if( !sqlite3_io_
c9ac0 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 29 20 73 error_benign ) s
c9ad0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
c9ae0 68 61 72 64 68 69 74 2b 2b 3b 0a 7d 0a 23 64 65 hardhit++;.}.#de
c9af0 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73 fine SimulateDis
c9b00 6b 66 75 6c 6c 45 72 72 6f 72 28 43 4f 44 45 29 kfullError(CODE)
c9b10 20 5c 0a 20 20 20 69 66 28 20 73 71 6c 69 74 65 \. if( sqlite
c9b20 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 3_diskfull_pendi
c9b30 6e 67 20 29 7b 20 5c 0a 20 20 20 20 20 69 66 28 ng ){ \. if(
c9b40 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c sqlite3_diskful
c9b50 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d 20 31 20 29 l_pending == 1 )
c9b60 7b 20 5c 0a 20 20 20 20 20 20 20 6c 6f 63 61 6c { \. local
c9b70 5f 69 6f 65 72 72 28 29 3b 20 5c 0a 20 20 20 20 _ioerr(); \.
c9b80 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 sqlite3_diskf
c9b90 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 ull = 1; \.
c9ba0 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 sqlite3_io_err
c9bb0 6f 72 5f 68 69 74 20 3d 20 31 3b 20 5c 0a 20 20 or_hit = 1; \.
c9bc0 20 20 20 20 20 43 4f 44 45 3b 20 5c 0a 20 20 20 CODE; \.
c9bd0 20 20 7d 65 6c 73 65 7b 20 5c 0a 20 20 20 20 20 }else{ \.
c9be0 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 sqlite3_diskfu
c9bf0 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a ll_pending--; \.
c9c00 20 20 20 20 20 7d 20 5c 0a 20 20 20 7d 0a 23 65 } \. }.#e
c9c10 6c 73 65 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 lse.#define Simu
c9c20 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 lateIOErrorBenig
c9c30 6e 28 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d n(X).#define Sim
c9c40 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 41 29 0a ulateIOError(A).
c9c50 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 #define Simulate
c9c60 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 41 29 DiskfullError(A)
c9c70 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 .#endif../*.** W
c9c80 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 6b 65 65 hen testing, kee
c9c90 70 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 p a count of the
c9ca0 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20 number of open
c9cb0 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 files..*/.#ifdef
c9cc0 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
c9cd0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
c9ce0 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f te3_open_file_co
c9cf0 75 6e 74 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 unt = 0;.#define
c9d00 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 20 OpenCounter(X)
c9d10 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 sqlite3_open_fi
c9d20 6c 65 5f 63 6f 75 6e 74 2b 3d 28 58 29 0a 23 65 le_count+=(X).#e
c9d30 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e lse.#define Open
c9d40 43 6f 75 6e 74 65 72 28 58 29 0a 23 65 6e 64 69 Counter(X).#endi
c9d50 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 f..#endif /* !de
c9d60 66 69 6e 65 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e fined(_OS_COMMON
c9d70 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a _H_) */../******
c9d80 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
c9d90 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a os_common.h ****
c9da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c9db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c9dc0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
c9dd0 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 ******** Continu
c9de0 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 ing where we lef
c9df0 74 20 6f 66 66 20 69 6e 20 6f 73 5f 75 6e 69 78 t off in os_unix
c9e00 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
c9e10 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 *******/../*.**
c9e20 44 65 66 69 6e 65 20 76 61 72 69 6f 75 73 20 6d Define various m
c9e30 61 63 72 6f 73 20 74 68 61 74 20 61 72 65 20 6d acros that are m
c9e40 69 73 73 69 6e 67 20 66 72 6f 6d 20 73 6f 6d 65 issing from some
c9e50 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66 systems..*/.#if
c9e60 6e 64 65 66 20 4f 5f 4c 41 52 47 45 46 49 4c 45 ndef O_LARGEFILE
c9e70 0a 23 20 64 65 66 69 6e 65 20 4f 5f 4c 41 52 47 .# define O_LARG
c9e80 45 46 49 4c 45 20 30 0a 23 65 6e 64 69 66 0a 23 EFILE 0.#endif.#
c9e90 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 49 53 ifdef SQLITE_DIS
c9ea0 41 42 4c 45 5f 4c 46 53 0a 23 20 75 6e 64 65 66 ABLE_LFS.# undef
c9eb0 20 4f 5f 4c 41 52 47 45 46 49 4c 45 0a 23 20 64 O_LARGEFILE.# d
c9ec0 65 66 69 6e 65 20 4f 5f 4c 41 52 47 45 46 49 4c efine O_LARGEFIL
c9ed0 45 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 E 0.#endif.#ifnd
c9ee0 65 66 20 4f 5f 4e 4f 46 4f 4c 4c 4f 57 0a 23 20 ef O_NOFOLLOW.#
c9ef0 64 65 66 69 6e 65 20 4f 5f 4e 4f 46 4f 4c 4c 4f define O_NOFOLLO
c9f00 57 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 W 0.#endif.#ifnd
c9f10 65 66 20 4f 5f 42 49 4e 41 52 59 0a 23 20 64 65 ef O_BINARY.# de
c9f20 66 69 6e 65 20 4f 5f 42 49 4e 41 52 59 20 30 0a fine O_BINARY 0.
c9f30 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 #endif../*.** Th
c9f40 65 20 44 4a 47 50 50 20 63 6f 6d 70 69 6c 65 72 e DJGPP compiler
c9f50 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 6c 6f 6f environment loo
c9f60 6b 73 20 6d 6f 73 74 6c 79 20 6c 69 6b 65 20 55 ks mostly like U
c9f70 6e 69 78 2c 20 62 75 74 20 69 74 0a 2a 2a 20 6c nix, but it.** l
c9f80 61 63 6b 73 20 74 68 65 20 66 63 6e 74 6c 28 29 acks the fcntl()
c9f90 20 73 79 73 74 65 6d 20 63 61 6c 6c 2e 20 20 53 system call. S
c9fa0 6f 20 72 65 64 65 66 69 6e 65 20 66 63 6e 74 6c o redefine fcntl
c9fb0 28 29 20 74 6f 20 62 65 20 73 6f 6d 65 74 68 69 () to be somethi
c9fc0 6e 67 0a 2a 2a 20 74 68 61 74 20 61 6c 77 61 79 ng.** that alway
c9fd0 73 20 73 75 63 63 65 65 64 73 2e 20 20 54 68 69 s succeeds. Thi
c9fe0 73 20 6d 65 61 6e 73 20 74 68 61 74 20 6c 6f 63 s means that loc
c9ff0 6b 69 6e 67 20 64 6f 65 73 20 6e 6f 74 20 6f 63 king does not oc
ca000 63 75 72 20 75 6e 64 65 72 0a 2a 2a 20 44 4a 47 cur under.** DJG
ca010 50 50 2e 20 20 42 75 74 20 69 74 20 69 73 20 44 PP. But it is D
ca020 4f 53 20 2d 20 77 68 61 74 20 64 69 64 20 79 6f OS - what did yo
ca030 75 20 65 78 70 65 63 74 3f 0a 2a 2f 0a 23 69 66 u expect?.*/.#if
ca040 64 65 66 20 5f 5f 44 4a 47 50 50 5f 5f 0a 23 20 def __DJGPP__.#
ca050 64 65 66 69 6e 65 20 66 63 6e 74 6c 28 41 2c 42 define fcntl(A,B
ca060 2c 43 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a ,C) 0.#endif../*
ca070 0a 2a 2a 20 54 68 65 20 74 68 72 65 61 64 69 64 .** The threadid
ca080 20 6d 61 63 72 6f 20 72 65 73 6f 6c 76 65 73 20 macro resolves
ca090 74 6f 20 74 68 65 20 74 68 72 65 61 64 2d 69 64 to the thread-id
ca0a0 20 6f 72 20 74 6f 20 30 2e 20 20 55 73 65 64 20 or to 0. Used
ca0b0 66 6f 72 0a 2a 2a 20 74 65 73 74 69 6e 67 20 61 for.** testing a
ca0c0 6e 64 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c nd debugging onl
ca0d0 79 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 y..*/.#if SQLITE
ca0e0 5f 54 48 52 45 41 44 53 41 46 45 0a 23 64 65 66 _THREADSAFE.#def
ca0f0 69 6e 65 20 74 68 72 65 61 64 69 64 20 70 74 68 ine threadid pth
ca100 72 65 61 64 5f 73 65 6c 66 28 29 0a 23 65 6c 73 read_self().#els
ca110 65 0a 23 64 65 66 69 6e 65 20 74 68 72 65 61 64 e.#define thread
ca120 69 64 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a id 0.#endif.../*
ca130 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 74 .** Helper funct
ca140 69 6f 6e 73 20 74 6f 20 6f 62 74 61 69 6e 20 61 ions to obtain a
ca150 6e 64 20 72 65 6c 69 6e 71 75 69 73 68 20 74 68 nd relinquish th
ca160 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 2e 20 e global mutex.
ca170 54 68 65 0a 2a 2a 20 67 6c 6f 62 61 6c 20 6d 75 The.** global mu
ca180 74 65 78 20 69 73 20 75 73 65 64 20 74 6f 20 70 tex is used to p
ca190 72 6f 74 65 63 74 20 74 68 65 20 75 6e 69 78 4f rotect the unixO
ca1a0 70 65 6e 43 6e 74 2c 20 75 6e 69 78 4c 6f 63 6b penCnt, unixLock
ca1b0 49 6e 66 6f 20 61 6e 64 0a 2a 2a 20 76 78 77 6f Info and.** vxwo
ca1c0 72 6b 73 46 69 6c 65 49 64 20 6f 62 6a 65 63 74 rksFileId object
ca1d0 73 20 75 73 65 64 20 62 79 20 74 68 69 73 20 66 s used by this f
ca1e0 69 6c 65 2c 20 61 6c 6c 20 6f 66 20 77 68 69 63 ile, all of whic
ca1f0 68 20 6d 61 79 20 62 65 20 0a 2a 2a 20 73 68 61 h may be .** sha
ca200 72 65 64 20 62 79 20 6d 75 6c 74 69 70 6c 65 20 red by multiple
ca210 74 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 46 threads..**.** F
ca220 75 6e 63 74 69 6f 6e 20 75 6e 69 78 4d 75 74 65 unction unixMute
ca230 78 48 65 6c 64 28 29 20 69 73 20 75 73 65 64 20 xHeld() is used
ca240 74 6f 20 61 73 73 65 72 74 28 29 20 74 68 61 74 to assert() that
ca250 20 74 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 the global mute
ca260 78 20 0a 2a 2a 20 69 73 20 68 65 6c 64 20 77 68 x .** is held wh
ca270 65 6e 20 72 65 71 75 69 72 65 64 2e 20 54 68 69 en required. Thi
ca280 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e s function is on
ca290 6c 79 20 75 73 65 64 20 61 73 20 70 61 72 74 20 ly used as part
ca2a0 6f 66 20 61 73 73 65 72 74 28 29 20 0a 2a 2a 20 of assert() .**
ca2b0 73 74 61 74 65 6d 65 6e 74 73 2e 20 65 2e 67 2e statements. e.g.
ca2c0 0a 2a 2a 0a 2a 2a 20 20 20 75 6e 69 78 45 6e 74 .**.** unixEnt
ca2d0 65 72 4d 75 74 65 78 28 29 0a 2a 2a 20 20 20 20 erMutex().**
ca2e0 20 61 73 73 65 72 74 28 20 75 6e 69 78 4d 75 74 assert( unixMut
ca2f0 65 78 48 65 6c 64 28 29 20 29 3b 0a 2a 2a 20 20 exHeld() );.**
ca300 20 75 6e 69 78 45 6e 74 65 72 4c 65 61 76 65 28 unixEnterLeave(
ca310 29 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ).*/.static void
ca320 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 unixEnterMutex(
ca330 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 void){. sqlite3
ca340 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c _mutex_enter(sql
ca350 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 ite3MutexAlloc(S
ca360 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
ca370 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 7d 0a 73 IC_MASTER));.}.s
ca380 74 61 74 69 63 20 76 6f 69 64 20 75 6e 69 78 4c tatic void unixL
ca390 65 61 76 65 4d 75 74 65 78 28 76 6f 69 64 29 7b eaveMutex(void){
ca3a0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
ca3b0 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 33 4d 75 _leave(sqlite3Mu
ca3c0 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f texAlloc(SQLITE_
ca3d0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 MUTEX_STATIC_MAS
ca3e0 54 45 52 29 29 3b 0a 7d 0a 23 69 66 64 65 66 20 TER));.}.#ifdef
ca3f0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 73 74 61 SQLITE_DEBUG.sta
ca400 74 69 63 20 69 6e 74 20 75 6e 69 78 4d 75 74 65 tic int unixMute
ca410 78 48 65 6c 64 28 76 6f 69 64 29 20 7b 0a 20 20 xHeld(void) {.
ca420 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 6d return sqlite3_m
ca430 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 65 utex_held(sqlite
ca440 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 3MutexAlloc(SQLI
ca450 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
ca460 4d 41 53 54 45 52 29 29 3b 0a 7d 0a 23 65 6e 64 MASTER));.}.#end
ca470 69 66 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 if...#ifdef SQLI
ca480 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 48 TE_DEBUG./*.** H
ca490 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 66 elper function f
ca4a0 6f 72 20 70 72 69 6e 74 69 6e 67 20 6f 75 74 20 or printing out
ca4b0 74 72 61 63 65 20 69 6e 66 6f 72 6d 61 74 69 6f trace informatio
ca4c0 6e 20 66 72 6f 6d 20 64 65 62 75 67 67 69 6e 67 n from debugging
ca4d0 0a 2a 2a 20 62 69 6e 61 72 69 65 73 2e 20 54 68 .** binaries. Th
ca4e0 69 73 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 is returns the s
ca4f0 74 72 69 6e 67 20 72 65 70 72 65 73 65 74 61 74 tring represetat
ca500 69 6f 6e 20 6f 66 20 74 68 65 20 73 75 70 70 6c ion of the suppl
ca510 69 65 64 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6c ied.** integer l
ca520 6f 63 6b 2d 74 79 70 65 2e 0a 2a 2f 0a 73 74 61 ock-type..*/.sta
ca530 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a tic const char *
ca540 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 69 6e 74 locktypeName(int
ca550 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 73 77 locktype){. sw
ca560 69 74 63 68 28 20 6c 6f 63 6b 74 79 70 65 20 29 itch( locktype )
ca570 7b 0a 20 20 20 20 63 61 73 65 20 4e 4f 5f 4c 4f {. case NO_LO
ca580 43 4b 3a 20 72 65 74 75 72 6e 20 22 4e 4f 4e 45 CK: return "NONE
ca590 22 3b 0a 20 20 20 20 63 61 73 65 20 53 48 41 52 ";. case SHAR
ca5a0 45 44 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 ED_LOCK: return
ca5b0 22 53 48 41 52 45 44 22 3b 0a 20 20 20 20 63 61 "SHARED";. ca
ca5c0 73 65 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b se RESERVED_LOCK
ca5d0 3a 20 72 65 74 75 72 6e 20 22 52 45 53 45 52 56 : return "RESERV
ca5e0 45 44 22 3b 0a 20 20 20 20 63 61 73 65 20 50 45 ED";. case PE
ca5f0 4e 44 49 4e 47 5f 4c 4f 43 4b 3a 20 72 65 74 75 NDING_LOCK: retu
ca600 72 6e 20 22 50 45 4e 44 49 4e 47 22 3b 0a 20 20 rn "PENDING";.
ca610 20 20 63 61 73 65 20 45 58 43 4c 55 53 49 56 45 case EXCLUSIVE
ca620 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 45 _LOCK: return "E
ca630 58 43 4c 55 53 49 56 45 22 3b 0a 20 20 7d 0a 20 XCLUSIVE";. }.
ca640 20 72 65 74 75 72 6e 20 22 45 52 52 4f 52 22 3b return "ERROR";
ca650 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 .}.#endif..#ifde
ca660 66 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52 f SQLITE_LOCK_TR
ca670 41 43 45 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 ACE./*.** Print
ca680 6f 75 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 out information
ca690 61 62 6f 75 74 20 61 6c 6c 20 6c 6f 63 6b 69 6e about all lockin
ca6a0 67 20 6f 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a g operations..**
ca6b0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
ca6c0 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 72 6f is used for tro
ca6d0 75 62 6c 65 73 68 6f 6f 74 69 6e 67 20 6c 6f 63 ubleshooting loc
ca6e0 6b 73 20 6f 6e 20 6d 75 6c 74 69 74 68 72 65 61 ks on multithrea
ca6f0 64 65 64 0a 2a 2a 20 70 6c 61 74 66 6f 72 6d 73 ded.** platforms
ca700 2e 20 20 45 6e 61 62 6c 65 20 62 79 20 63 6f 6d . Enable by com
ca710 70 69 6c 69 6e 67 20 77 69 74 68 20 74 68 65 20 piling with the
ca720 2d 44 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52 -DSQLITE_LOCK_TR
ca730 41 43 45 0a 2a 2a 20 63 6f 6d 6d 61 6e 64 2d 6c ACE.** command-l
ca740 69 6e 65 20 6f 70 74 69 6f 6e 20 6f 6e 20 74 68 ine option on th
ca750 65 20 63 6f 6d 70 69 6c 65 72 2e 20 20 54 68 69 e compiler. Thi
ca760 73 20 63 6f 64 65 20 69 73 20 6e 6f 72 6d 61 6c s code is normal
ca770 6c 79 0a 2a 2a 20 74 75 72 6e 65 64 20 6f 66 66 ly.** turned off
ca780 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
ca790 6c 6f 63 6b 54 72 61 63 65 28 69 6e 74 20 66 64 lockTrace(int fd
ca7a0 2c 20 69 6e 74 20 6f 70 2c 20 73 74 72 75 63 74 , int op, struct
ca7b0 20 66 6c 6f 63 6b 20 2a 70 29 7b 0a 20 20 63 68 flock *p){. ch
ca7c0 61 72 20 2a 7a 4f 70 4e 61 6d 65 2c 20 2a 7a 54 ar *zOpName, *zT
ca7d0 79 70 65 3b 0a 20 20 69 6e 74 20 73 3b 0a 20 20 ype;. int s;.
ca7e0 69 6e 74 20 73 61 76 65 64 45 72 72 6e 6f 3b 0a int savedErrno;.
ca7f0 20 20 69 66 28 20 6f 70 3d 3d 46 5f 47 45 54 4c if( op==F_GETL
ca800 4b 20 29 7b 0a 20 20 20 20 7a 4f 70 4e 61 6d 65 K ){. zOpName
ca810 20 3d 20 22 47 45 54 4c 4b 22 3b 0a 20 20 7d 65 = "GETLK";. }e
ca820 6c 73 65 20 69 66 28 20 6f 70 3d 3d 46 5f 53 45 lse if( op==F_SE
ca830 54 4c 4b 20 29 7b 0a 20 20 20 20 7a 4f 70 4e 61 TLK ){. zOpNa
ca840 6d 65 20 3d 20 22 53 45 54 4c 4b 22 3b 0a 20 20 me = "SETLK";.
ca850 7d 65 6c 73 65 7b 0a 20 20 20 20 73 20 3d 20 66 }else{. s = f
ca860 63 6e 74 6c 28 66 64 2c 20 6f 70 2c 20 70 29 3b cntl(fd, op, p);
ca870 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 . sqlite3Debu
ca880 67 50 72 69 6e 74 66 28 22 66 63 6e 74 6c 20 75 gPrintf("fcntl u
ca890 6e 6b 6e 6f 77 6e 20 25 64 20 25 64 20 25 64 5c nknown %d %d %d\
ca8a0 6e 22 2c 20 66 64 2c 20 6f 70 2c 20 73 29 3b 0a n", fd, op, s);.
ca8b0 20 20 20 20 72 65 74 75 72 6e 20 73 3b 0a 20 20 return s;.
ca8c0 7d 0a 20 20 69 66 28 20 70 2d 3e 6c 5f 74 79 70 }. if( p->l_typ
ca8d0 65 3d 3d 46 5f 52 44 4c 43 4b 20 29 7b 0a 20 20 e==F_RDLCK ){.
ca8e0 20 20 7a 54 79 70 65 20 3d 20 22 52 44 4c 43 4b zType = "RDLCK
ca8f0 22 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 ";. }else if( p
ca900 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 57 52 4c 43 ->l_type==F_WRLC
ca910 4b 20 29 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d K ){. zType =
ca920 20 22 57 52 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73 "WRLCK";. }els
ca930 65 20 69 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d e if( p->l_type=
ca940 3d 46 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 =F_UNLCK ){.
ca950 7a 54 79 70 65 20 3d 20 22 55 4e 4c 43 4b 22 3b zType = "UNLCK";
ca960 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 . }else{. as
ca970 73 65 72 74 28 20 30 20 29 3b 0a 20 20 7d 0a 20 sert( 0 );. }.
ca980 20 61 73 73 65 72 74 28 20 70 2d 3e 6c 5f 77 68 assert( p->l_wh
ca990 65 6e 63 65 3d 3d 53 45 45 4b 5f 53 45 54 20 29 ence==SEEK_SET )
ca9a0 3b 0a 20 20 73 20 3d 20 66 63 6e 74 6c 28 66 64 ;. s = fcntl(fd
ca9b0 2c 20 6f 70 2c 20 70 29 3b 0a 20 20 73 61 76 65 , op, p);. save
ca9c0 64 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a dErrno = errno;.
ca9d0 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 sqlite3DebugPr
ca9e0 69 6e 74 66 28 22 66 63 6e 74 6c 20 25 64 20 25 intf("fcntl %d %
ca9f0 64 20 25 73 20 25 73 20 25 64 20 25 64 20 25 64 d %s %s %d %d %d
caa00 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20 74 68 72 %d\n",. thr
caa10 65 61 64 69 64 2c 20 66 64 2c 20 7a 4f 70 4e 61 eadid, fd, zOpNa
caa20 6d 65 2c 20 7a 54 79 70 65 2c 20 28 69 6e 74 29 me, zType, (int)
caa30 70 2d 3e 6c 5f 73 74 61 72 74 2c 20 28 69 6e 74 p->l_start, (int
caa40 29 70 2d 3e 6c 5f 6c 65 6e 2c 0a 20 20 20 20 20 )p->l_len,.
caa50 28 69 6e 74 29 70 2d 3e 6c 5f 70 69 64 2c 20 73 (int)p->l_pid, s
caa60 29 3b 0a 20 20 69 66 28 20 73 3d 3d 28 2d 31 29 );. if( s==(-1)
caa70 20 26 26 20 6f 70 3d 3d 46 5f 53 45 54 4c 4b 20 && op==F_SETLK
caa80 26 26 20 28 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 && (p->l_type==F
caa90 5f 52 44 4c 43 4b 20 7c 7c 20 70 2d 3e 6c 5f 74 _RDLCK || p->l_t
caaa0 79 70 65 3d 3d 46 5f 57 52 4c 43 4b 29 20 29 7b ype==F_WRLCK) ){
caab0 0a 20 20 20 20 73 74 72 75 63 74 20 66 6c 6f 63 . struct floc
caac0 6b 20 6c 32 3b 0a 20 20 20 20 6c 32 20 3d 20 2a k l2;. l2 = *
caad0 70 3b 0a 20 20 20 20 66 63 6e 74 6c 28 66 64 2c p;. fcntl(fd,
caae0 20 46 5f 47 45 54 4c 4b 2c 20 26 6c 32 29 3b 0a F_GETLK, &l2);.
caaf0 20 20 20 20 69 66 28 20 6c 32 2e 6c 5f 74 79 70 if( l2.l_typ
cab00 65 3d 3d 46 5f 52 44 4c 43 4b 20 29 7b 0a 20 20 e==F_RDLCK ){.
cab10 20 20 20 20 7a 54 79 70 65 20 3d 20 22 52 44 4c zType = "RDL
cab20 43 4b 22 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 CK";. }else i
cab30 66 28 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d 46 5f f( l2.l_type==F_
cab40 57 52 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20 7a WRLCK ){. z
cab50 54 79 70 65 20 3d 20 22 57 52 4c 43 4b 22 3b 0a Type = "WRLCK";.
cab60 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 32 }else if( l2
cab70 2e 6c 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b .l_type==F_UNLCK
cab80 20 29 7b 0a 20 20 20 20 20 20 7a 54 79 70 65 20 ){. zType
cab90 3d 20 22 55 4e 4c 43 4b 22 3b 0a 20 20 20 20 7d = "UNLCK";. }
caba0 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 else{. asse
cabb0 72 74 28 20 30 20 29 3b 0a 20 20 20 20 7d 0a 20 rt( 0 );. }.
cabc0 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 sqlite3DebugP
cabd0 72 69 6e 74 66 28 22 66 63 6e 74 6c 2d 66 61 69 rintf("fcntl-fai
cabe0 6c 75 72 65 2d 72 65 61 73 6f 6e 3a 20 25 73 20 lure-reason: %s
cabf0 25 64 20 25 64 20 25 64 5c 6e 22 2c 0a 20 20 20 %d %d %d\n",.
cac00 20 20 20 20 7a 54 79 70 65 2c 20 28 69 6e 74 29 zType, (int)
cac10 6c 32 2e 6c 5f 73 74 61 72 74 2c 20 28 69 6e 74 l2.l_start, (int
cac20 29 6c 32 2e 6c 5f 6c 65 6e 2c 20 28 69 6e 74 29 )l2.l_len, (int)
cac30 6c 32 2e 6c 5f 70 69 64 29 3b 0a 20 20 7d 0a 20 l2.l_pid);. }.
cac40 20 65 72 72 6e 6f 20 3d 20 73 61 76 65 64 45 72 errno = savedEr
cac50 72 6e 6f 3b 0a 20 20 72 65 74 75 72 6e 20 73 3b rno;. return s;
cac60 0a 7d 0a 23 64 65 66 69 6e 65 20 66 63 6e 74 6c .}.#define fcntl
cac70 20 6c 6f 63 6b 54 72 61 63 65 0a 23 65 6e 64 69 lockTrace.#endi
cac80 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4c 4f 43 4b f /* SQLITE_LOCK
cac90 5f 54 52 41 43 45 20 2a 2f 0a 0a 0a 0a 2f 2a 0a _TRACE */..../*.
caca0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
cacb0 74 72 61 6e 73 6c 61 74 65 73 20 61 20 73 74 61 translates a sta
cacc0 6e 64 61 72 64 20 50 4f 53 49 58 20 65 72 72 6e ndard POSIX errn
cacd0 6f 20 63 6f 64 65 20 69 6e 74 6f 20 73 6f 6d 65 o code into some
cace0 74 68 69 6e 67 0a 2a 2a 20 75 73 65 66 75 6c 20 thing.** useful
cacf0 74 6f 20 74 68 65 20 63 6c 69 65 6e 74 73 20 6f to the clients o
cad00 66 20 74 68 65 20 73 71 6c 69 74 65 33 20 66 75 f the sqlite3 fu
cad10 6e 63 74 69 6f 6e 73 2e 20 20 53 70 65 63 69 66 nctions. Specif
cad20 69 63 61 6c 6c 79 2c 20 69 74 20 69 73 0a 2a 2a ically, it is.**
cad30 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 74 72 61 intended to tra
cad40 6e 73 6c 61 74 65 20 61 20 76 61 72 69 65 74 79 nslate a variety
cad50 20 6f 66 20 22 74 72 79 20 61 67 61 69 6e 22 20 of "try again"
cad60 65 72 72 6f 72 73 20 69 6e 74 6f 20 53 51 4c 49 errors into SQLI
cad70 54 45 5f 42 55 53 59 0a 2a 2a 20 61 6e 64 20 61 TE_BUSY.** and a
cad80 20 76 61 72 69 65 74 79 20 6f 66 20 22 70 6c 65 variety of "ple
cad90 61 73 65 20 63 6c 6f 73 65 20 74 68 65 20 66 69 ase close the fi
cada0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 4e 4f le descriptor NO
cadb0 57 22 20 65 72 72 6f 72 73 20 69 6e 74 6f 20 0a W" errors into .
cadc0 2a 2a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a ** SQLITE_IOERR.
cadd0 2a 2a 20 0a 2a 2a 20 45 72 72 6f 72 73 20 64 75 ** .** Errors du
cade0 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 ring initializat
cadf0 69 6f 6e 20 6f 66 20 6c 6f 63 6b 73 2c 20 6f 72 ion of locks, or
cae00 20 66 69 6c 65 20 73 79 73 74 65 6d 20 73 75 70 file system sup
cae10 70 6f 72 74 20 66 6f 72 20 6c 6f 63 6b 73 2c 0a port for locks,.
cae20 2a 2a 20 73 68 6f 75 6c 64 20 68 61 6e 64 6c 65 ** should handle
cae30 20 45 4e 4f 4c 43 4b 2c 20 45 4e 4f 54 53 55 50 ENOLCK, ENOTSUP
cae40 2c 20 45 4f 50 4e 4f 54 53 55 50 50 20 73 65 70 , EOPNOTSUPP sep
cae50 61 72 61 74 65 6c 79 2e 0a 2a 2f 0a 73 74 61 74 arately..*/.stat
cae60 69 63 20 69 6e 74 20 73 71 6c 69 74 65 45 72 72 ic int sqliteErr
cae70 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 orFromPosixError
cae80 28 69 6e 74 20 70 6f 73 69 78 45 72 72 6f 72 2c (int posixError,
cae90 20 69 6e 74 20 73 71 6c 69 74 65 49 4f 45 72 72 int sqliteIOErr
caea0 29 20 7b 0a 20 20 73 77 69 74 63 68 20 28 70 6f ) {. switch (po
caeb0 73 69 78 45 72 72 6f 72 29 20 7b 0a 20 20 63 61 sixError) {. ca
caec0 73 65 20 30 3a 20 0a 20 20 20 20 72 65 74 75 72 se 0: . retur
caed0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 n SQLITE_OK;.
caee0 20 0a 20 20 63 61 73 65 20 45 41 47 41 49 4e 3a . case EAGAIN:
caef0 0a 20 20 63 61 73 65 20 45 54 49 4d 45 44 4f 55 . case ETIMEDOU
caf00 54 3a 0a 20 20 63 61 73 65 20 45 42 55 53 59 3a T:. case EBUSY:
caf10 0a 20 20 63 61 73 65 20 45 49 4e 54 52 3a 0a 20 . case EINTR:.
caf20 20 63 61 73 65 20 45 4e 4f 4c 43 4b 3a 20 20 0a case ENOLCK: .
caf30 20 20 20 20 2f 2a 20 72 61 6e 64 6f 6d 20 4e 46 /* random NF
caf40 53 20 72 65 74 72 79 20 65 72 72 6f 72 2c 20 75 S retry error, u
caf50 6e 6c 65 73 73 20 64 75 72 69 6e 67 20 66 69 6c nless during fil
caf60 65 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 e system support
caf70 20 0a 20 20 20 20 20 2a 20 69 6e 74 72 6f 73 70 . * introsp
caf80 65 63 74 69 6f 6e 2c 20 69 6e 20 77 68 69 63 68 ection, in which
caf90 20 69 74 20 61 63 74 75 61 6c 6c 79 20 6d 65 61 it actually mea
cafa0 6e 73 20 77 68 61 74 20 69 74 20 73 61 79 73 20 ns what it says
cafb0 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 */. return SQ
cafc0 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 0a LITE_BUSY;. .
cafd0 20 20 63 61 73 65 20 45 41 43 43 45 53 3a 20 0a case EACCES: .
cafe0 20 20 20 20 2f 2a 20 45 41 43 43 45 53 20 69 73 /* EACCES is
caff0 20 6c 69 6b 65 20 45 41 47 41 49 4e 20 64 75 72 like EAGAIN dur
cb000 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 ing locking oper
cb010 61 74 69 6f 6e 73 2c 20 62 75 74 20 6e 6f 74 20 ations, but not
cb020 61 6e 79 20 6f 74 68 65 72 20 74 69 6d 65 2a 2f any other time*/
cb030 0a 20 20 20 20 69 66 28 20 28 73 71 6c 69 74 65 . if( (sqlite
cb040 49 4f 45 72 72 20 3d 3d 20 53 51 4c 49 54 45 5f IOErr == SQLITE_
cb050 49 4f 45 52 52 5f 4c 4f 43 4b 29 20 7c 7c 20 0a IOERR_LOCK) || .
cb060 09 28 73 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d .(sqliteIOErr ==
cb070 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e SQLITE_IOERR_UN
cb080 4c 4f 43 4b 29 20 7c 7c 20 0a 09 28 73 71 6c 69 LOCK) || ..(sqli
cb090 74 65 49 4f 45 72 72 20 3d 3d 20 53 51 4c 49 54 teIOErr == SQLIT
cb0a0 45 5f 49 4f 45 52 52 5f 52 44 4c 4f 43 4b 29 20 E_IOERR_RDLOCK)
cb0b0 7c 7c 0a 09 28 73 71 6c 69 74 65 49 4f 45 72 72 ||..(sqliteIOErr
cb0c0 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 == SQLITE_IOERR
cb0d0 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f _CHECKRESERVEDLO
cb0e0 43 4b 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 CK) ){. ret
cb0f0 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b urn SQLITE_BUSY;
cb100 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 65 6c . }. /* el
cb110 73 65 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 se fall through
cb120 2a 2f 0a 20 20 63 61 73 65 20 45 50 45 52 4d 3a */. case EPERM:
cb130 20 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c . return SQL
cb140 49 54 45 5f 50 45 52 4d 3b 0a 20 20 20 20 0a 20 ITE_PERM;. .
cb150 20 63 61 73 65 20 45 44 45 41 44 4c 4b 3a 0a 20 case EDEADLK:.
cb160 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
cb170 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 3b 0a _IOERR_BLOCKED;.
cb180 20 20 20 20 0a 23 69 66 20 45 4f 50 4e 4f 54 53 .#if EOPNOTS
cb190 55 50 50 21 3d 45 4e 4f 54 53 55 50 0a 20 20 63 UPP!=ENOTSUP. c
cb1a0 61 73 65 20 45 4f 50 4e 4f 54 53 55 50 50 3a 20 ase EOPNOTSUPP:
cb1b0 0a 20 20 20 20 2f 2a 20 73 6f 6d 65 74 68 69 6e . /* somethin
cb1c0 67 20 77 65 6e 74 20 74 65 72 72 69 62 6c 79 20 g went terribly
cb1d0 61 77 72 79 2c 20 75 6e 6c 65 73 73 20 64 75 72 awry, unless dur
cb1e0 69 6e 67 20 66 69 6c 65 20 73 79 73 74 65 6d 20 ing file system
cb1f0 73 75 70 70 6f 72 74 20 0a 20 20 20 20 20 2a 20 support . *
cb200 69 6e 74 72 6f 73 70 65 63 74 69 6f 6e 2c 20 69 introspection, i
cb210 6e 20 77 68 69 63 68 20 69 74 20 61 63 74 75 61 n which it actua
cb220 6c 6c 79 20 6d 65 61 6e 73 20 77 68 61 74 20 69 lly means what i
cb230 74 20 73 61 79 73 20 2a 2f 0a 23 65 6e 64 69 66 t says */.#endif
cb240 0a 23 69 66 64 65 66 20 45 4e 4f 54 53 55 50 0a .#ifdef ENOTSUP.
cb250 20 20 63 61 73 65 20 45 4e 4f 54 53 55 50 3a 20 case ENOTSUP:
cb260 0a 20 20 20 20 2f 2a 20 69 6e 76 61 6c 69 64 20 . /* invalid
cb270 66 64 2c 20 75 6e 6c 65 73 73 20 64 75 72 69 6e fd, unless durin
cb280 67 20 66 69 6c 65 20 73 79 73 74 65 6d 20 73 75 g file system su
cb290 70 70 6f 72 74 20 69 6e 74 72 6f 73 70 65 63 74 pport introspect
cb2a0 69 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20 0a 20 ion, in which .
cb2b0 20 20 20 20 2a 20 69 74 20 61 63 74 75 61 6c 6c * it actuall
cb2c0 79 20 6d 65 61 6e 73 20 77 68 61 74 20 69 74 20 y means what it
cb2d0 73 61 79 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 says */.#endif.
cb2e0 20 63 61 73 65 20 45 49 4f 3a 0a 20 20 63 61 73 case EIO:. cas
cb2f0 65 20 45 42 41 44 46 3a 0a 20 20 63 61 73 65 20 e EBADF:. case
cb300 45 49 4e 56 41 4c 3a 0a 20 20 63 61 73 65 20 45 EINVAL:. case E
cb310 4e 4f 54 43 4f 4e 4e 3a 0a 20 20 63 61 73 65 20 NOTCONN:. case
cb320 45 4e 4f 44 45 56 3a 0a 20 20 63 61 73 65 20 45 ENODEV:. case E
cb330 4e 58 49 4f 3a 0a 20 20 63 61 73 65 20 45 4e 4f NXIO:. case ENO
cb340 45 4e 54 3a 0a 20 20 63 61 73 65 20 45 53 54 41 ENT:. case ESTA
cb350 4c 45 3a 0a 20 20 63 61 73 65 20 45 4e 4f 53 59 LE:. case ENOSY
cb360 53 3a 0a 20 20 20 20 2f 2a 20 74 68 65 73 65 20 S:. /* these
cb370 73 68 6f 75 6c 64 20 66 6f 72 63 65 20 74 68 65 should force the
cb380 20 63 6c 69 65 6e 74 20 74 6f 20 63 6c 6f 73 65 client to close
cb390 20 74 68 65 20 66 69 6c 65 20 61 6e 64 20 72 65 the file and re
cb3a0 63 6f 6e 6e 65 63 74 20 2a 2f 0a 20 20 20 20 0a connect */. .
cb3b0 20 20 64 65 66 61 75 6c 74 3a 20 0a 20 20 20 20 default: .
cb3c0 72 65 74 75 72 6e 20 73 71 6c 69 74 65 49 4f 45 return sqliteIOE
cb3d0 72 72 3b 0a 20 20 7d 0a 7d 0a 0a 0a 0a 2f 2a 2a rr;. }.}..../**
cb3e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb3f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a ************.***
cb430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 ***************
cb440 42 65 67 69 6e 20 55 6e 69 71 75 65 20 46 69 6c Begin Unique Fil
cb450 65 20 49 44 20 55 74 69 6c 69 74 79 20 55 73 65 e ID Utility Use
cb460 64 20 42 79 20 56 78 57 6f 72 6b 73 20 2a 2a 2a d By VxWorks ***
cb470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
cb480 2a 2a 20 4f 6e 20 6d 6f 73 74 20 76 65 72 73 69 ** On most versi
cb490 6f 6e 73 20 6f 66 20 75 6e 69 78 2c 20 77 65 20 ons of unix, we
cb4a0 63 61 6e 20 67 65 74 20 61 20 75 6e 69 71 75 65 can get a unique
cb4b0 20 49 44 20 66 6f 72 20 61 20 66 69 6c 65 20 62 ID for a file b
cb4c0 79 20 63 6f 6e 63 61 74 65 6e 61 74 69 6e 67 0a y concatenating.
cb4d0 2a 2a 20 74 68 65 20 64 65 76 69 63 65 20 6e 75 ** the device nu
cb4e0 6d 62 65 72 20 61 6e 64 20 74 68 65 20 69 6e 6f mber and the ino
cb4f0 64 65 20 6e 75 6d 62 65 72 2e 20 20 42 75 74 20 de number. But
cb500 74 68 69 73 20 64 6f 65 73 20 6e 6f 74 20 77 6f this does not wo
cb510 72 6b 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a rk on VxWorks..*
cb520 2a 20 4f 6e 20 56 78 57 6f 72 6b 73 2c 20 61 20 * On VxWorks, a
cb530 75 6e 69 71 75 65 20 66 69 6c 65 20 69 64 20 6d unique file id m
cb540 75 73 74 20 62 65 20 62 61 73 65 64 20 6f 6e 20 ust be based on
cb550 74 68 65 20 63 61 6e 6f 6e 69 63 61 6c 20 66 69 the canonical fi
cb560 6c 65 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 lename..**.** A
cb570 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e pointer to an in
cb580 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f stance of the fo
cb590 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 llowing structur
cb5a0 65 20 63 61 6e 20 62 65 20 75 73 65 64 20 61 73 e can be used as
cb5b0 20 61 0a 2a 2a 20 75 6e 69 71 75 65 20 66 69 6c a.** unique fil
cb5c0 65 20 49 44 20 69 6e 20 56 78 57 6f 72 6b 73 2e e ID in VxWorks.
cb5d0 20 20 45 61 63 68 20 69 6e 73 74 61 6e 63 65 20 Each instance
cb5e0 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 of this structur
cb5f0 65 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 e contains.** a
cb600 63 6f 70 79 20 6f 66 20 74 68 65 20 63 61 6e 6f copy of the cano
cb610 6e 69 63 61 6c 20 66 69 6c 65 6e 61 6d 65 2e 20 nical filename.
cb620 20 54 68 65 72 65 20 69 73 20 61 6c 73 6f 20 61 There is also a
cb630 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 reference count
cb640 2e 20 20 0a 2a 2a 20 54 68 65 20 73 74 72 75 63 . .** The struc
cb650 74 75 72 65 20 69 73 20 72 65 63 6c 61 69 6d 65 ture is reclaime
cb660 64 20 77 68 65 6e 20 74 68 65 20 6e 75 6d 62 65 d when the numbe
cb670 72 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f r of pointers to
cb680 20 69 74 20 64 72 6f 70 73 20 74 6f 0a 2a 2a 20 it drops to.**
cb690 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 zero..**.** Ther
cb6a0 65 20 61 72 65 20 6e 65 76 65 72 20 76 65 72 79 e are never very
cb6b0 20 6d 61 6e 79 20 66 69 6c 65 73 20 6f 70 65 6e many files open
cb6c0 20 61 74 20 6f 6e 65 20 74 69 6d 65 20 61 6e 64 at one time and
cb6d0 20 6c 6f 6f 6b 75 70 73 20 61 72 65 20 6e 6f 74 lookups are not
cb6e0 0a 2a 2a 20 61 20 70 65 72 66 6f 72 6d 61 6e 63 .** a performanc
cb6f0 65 2d 63 72 69 74 69 63 61 6c 20 70 61 74 68 2c e-critical path,
cb700 20 73 6f 20 69 74 20 69 73 20 73 75 66 66 69 63 so it is suffic
cb710 69 65 6e 74 20 74 6f 20 70 75 74 20 74 68 65 73 ient to put thes
cb720 65 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73 20 e.** structures
cb730 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 on a linked list
cb740 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 76 78 77 6f ..*/.struct vxwo
cb750 72 6b 73 46 69 6c 65 49 64 20 7b 0a 20 20 73 74 rksFileId {. st
cb760 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 ruct vxworksFile
cb770 49 64 20 2a 70 4e 65 78 74 3b 20 20 2f 2a 20 4e Id *pNext; /* N
cb780 65 78 74 20 69 6e 20 61 20 6c 69 73 74 20 6f 66 ext in a list of
cb790 20 74 68 65 6d 20 61 6c 6c 20 2a 2f 0a 20 20 69 them all */. i
cb7a0 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 nt nRef;
cb7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
cb7c0 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 Number of refere
cb7d0 6e 63 65 73 20 74 6f 20 74 68 69 73 20 6f 6e 65 nces to this one
cb7e0 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b */. int nName;
cb7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cb800 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 /* Length of
cb810 20 74 68 65 20 7a 43 61 6e 6f 6e 69 63 61 6c 4e the zCanonicalN
cb820 61 6d 65 5b 5d 20 73 74 72 69 6e 67 20 2a 2f 0a ame[] string */.
cb830 20 20 63 68 61 72 20 2a 7a 43 61 6e 6f 6e 69 63 char *zCanonic
cb840 61 6c 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 alName;
cb850 2f 2a 20 43 61 6e 6f 6e 69 63 61 6c 20 66 69 6c /* Canonical fil
cb860 65 6e 61 6d 65 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 ename */.};..#if
cb870 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 2f 2a 20 0a OS_VXWORKS./* .
cb880 2a 2a 20 41 6c 6c 20 75 6e 69 71 75 65 20 66 69 ** All unique fi
cb890 6c 65 6e 61 6d 65 73 20 61 72 65 20 68 65 6c 64 lenames are held
cb8a0 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 on a linked lis
cb8b0 74 20 68 65 61 64 65 64 20 62 79 20 74 68 69 73 t headed by this
cb8c0 0a 2a 2a 20 76 61 72 69 61 62 6c 65 3a 0a 2a 2f .** variable:.*/
cb8d0 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 76 .static struct v
cb8e0 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 76 78 xworksFileId *vx
cb8f0 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74 20 3d 20 worksFileList =
cb900 30 3b 0a 0a 2f 2a 0a 2a 2a 20 53 69 6d 70 6c 69 0;../*.** Simpli
cb910 66 79 20 61 20 66 69 6c 65 6e 61 6d 65 20 69 6e fy a filename in
cb920 74 6f 20 69 74 73 20 63 61 6e 6f 6e 69 63 61 6c to its canonical
cb930 20 66 6f 72 6d 0a 2a 2a 20 62 79 20 6d 61 6b 69 form.** by maki
cb940 6e 67 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ng the following
cb950 20 63 68 61 6e 67 65 73 3a 0a 2a 2a 0a 2a 2a 20 changes:.**.**
cb960 20 2a 20 72 65 6d 6f 76 69 6e 67 20 61 6e 79 20 * removing any
cb970 74 72 61 69 6c 69 6e 67 20 61 6e 64 20 64 75 70 trailing and dup
cb980 6c 69 63 61 74 65 20 2f 0a 2a 2a 20 20 2a 20 63 licate /.** * c
cb990 6f 6e 76 65 72 74 20 2f 2e 2f 20 69 6e 74 6f 20 onvert /./ into
cb9a0 6a 75 73 74 20 2f 0a 2a 2a 20 20 2a 20 63 6f 6e just /.** * con
cb9b0 76 65 72 74 20 2f 41 2f 2e 2e 2f 20 77 68 65 72 vert /A/../ wher
cb9c0 65 20 41 20 69 73 20 61 6e 79 20 73 69 6d 70 6c e A is any simpl
cb9d0 65 20 6e 61 6d 65 20 69 6e 74 6f 20 6a 75 73 74 e name into just
cb9e0 20 2f 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 65 73 /.**.** Changes
cb9f0 20 61 72 65 20 6d 61 64 65 20 69 6e 2d 70 6c 61 are made in-pla
cba00 63 65 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 ce. Return the
cba10 6e 65 77 20 6e 61 6d 65 20 6c 65 6e 67 74 68 2e new name length.
cba20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 72 69 67 69 .**.** The origi
cba30 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 nal filename is
cba40 69 6e 20 7a 5b 30 2e 2e 6e 2d 31 5d 2e 20 20 52 in z[0..n-1]. R
cba50 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
cba60 20 6f 66 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 of.** character
cba70 73 20 69 6e 20 74 68 65 20 73 69 6d 70 6c 69 66 s in the simplif
cba80 69 65 64 20 6e 61 6d 65 2e 0a 2a 2f 0a 73 74 61 ied name..*/.sta
cba90 74 69 63 20 69 6e 74 20 76 78 77 6f 72 6b 73 53 tic int vxworksS
cbaa0 69 6d 70 6c 69 66 79 4e 61 6d 65 28 63 68 61 72 implifyName(char
cbab0 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 69 *z, int n){. i
cbac0 6e 74 20 69 2c 20 6a 3b 0a 20 20 77 68 69 6c 65 nt i, j;. while
cbad0 28 20 6e 3e 31 20 26 26 20 7a 5b 6e 2d 31 5d 3d ( n>1 && z[n-1]=
cbae0 3d 27 2f 27 20 29 7b 20 6e 2d 2d 3b 20 7d 0a 20 ='/' ){ n--; }.
cbaf0 20 66 6f 72 28 69 3d 6a 3d 30 3b 20 69 3c 6e 3b for(i=j=0; i<n;
cbb00 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a i++){. if( z
cbb10 5b 69 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 [i]=='/' ){.
cbb20 20 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 27 2f if( z[i+1]=='/
cbb30 27 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 ' ) continue;.
cbb40 20 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d if( z[i+1]==
cbb50 27 2e 27 20 26 26 20 69 2b 32 3c 6e 20 26 26 20 '.' && i+2<n &&
cbb60 7a 5b 69 2b 32 5d 3d 3d 27 2f 27 20 29 7b 0a 20 z[i+2]=='/' ){.
cbb70 20 20 20 20 20 20 20 69 20 2b 3d 20 31 3b 0a 20 i += 1;.
cbb80 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b continue;
cbb90 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
cbba0 66 28 20 7a 5b 69 2b 31 5d 3d 3d 27 2e 27 20 26 f( z[i+1]=='.' &
cbbb0 26 20 69 2b 33 3c 6e 20 26 26 20 7a 5b 69 2b 32 & i+3<n && z[i+2
cbbc0 5d 3d 3d 27 2e 27 20 26 26 20 7a 5b 69 2b 33 5d ]=='.' && z[i+3]
cbbd0 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 20 =='/' ){.
cbbe0 20 77 68 69 6c 65 28 20 6a 3e 30 20 26 26 20 7a while( j>0 && z
cbbf0 5b 6a 2d 31 5d 21 3d 27 2f 27 20 29 7b 20 6a 2d [j-1]!='/' ){ j-
cbc00 2d 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 -; }. if(
cbc10 20 6a 3e 30 20 29 7b 20 6a 2d 2d 3b 20 7d 0a 20 j>0 ){ j--; }.
cbc20 20 20 20 20 20 20 20 69 20 2b 3d 20 32 3b 0a 20 i += 2;.
cbc30 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b continue;
cbc40 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
cbc50 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d z[j++] = z[i]
cbc60 3b 0a 20 20 7d 0a 20 20 7a 5b 6a 5d 20 3d 20 30 ;. }. z[j] = 0
cbc70 3b 0a 20 20 72 65 74 75 72 6e 20 6a 3b 0a 7d 0a ;. return j;.}.
cbc80 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 61 20 75 6e ./*.** Find a un
cbc90 69 71 75 65 20 66 69 6c 65 20 49 44 20 66 6f 72 ique file ID for
cbca0 20 74 68 65 20 67 69 76 65 6e 20 61 62 73 6f 6c the given absol
cbcb0 75 74 65 20 70 61 74 68 6e 61 6d 65 2e 20 20 52 ute pathname. R
cbcc0 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 eturn.** a point
cbcd0 65 72 20 74 6f 20 74 68 65 20 76 78 77 6f 72 6b er to the vxwork
cbce0 73 46 69 6c 65 49 64 20 6f 62 6a 65 63 74 2e 20 sFileId object.
cbcf0 20 54 68 69 73 20 70 6f 69 6e 74 65 72 20 69 73 This pointer is
cbd00 20 74 68 65 20 75 6e 69 71 75 65 0a 2a 2a 20 66 the unique.** f
cbd10 69 6c 65 20 49 44 2e 0a 2a 2a 0a 2a 2a 20 54 68 ile ID..**.** Th
cbd20 65 20 6e 52 65 66 20 66 69 65 6c 64 20 6f 66 20 e nRef field of
cbd30 74 68 65 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 the vxworksFileI
cbd40 64 20 6f 62 6a 65 63 74 20 69 73 20 69 6e 63 72 d object is incr
cbd50 65 6d 65 6e 74 65 64 20 62 65 66 6f 72 65 0a 2a emented before.*
cbd60 2a 20 74 68 65 20 6f 62 6a 65 63 74 20 69 73 20 * the object is
cbd70 72 65 74 75 72 6e 65 64 2e 20 20 41 20 6e 65 77 returned. A new
cbd80 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 6f vxworksFileId o
cbd90 62 6a 65 63 74 20 69 73 20 63 72 65 61 74 65 64 bject is created
cbda0 0a 2a 2a 20 61 6e 64 20 61 64 64 65 64 20 74 6f .** and added to
cbdb0 20 74 68 65 20 67 6c 6f 62 61 6c 20 6c 69 73 74 the global list
cbdc0 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a if necessary..*
cbdd0 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79 *.** If a memory
cbde0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f allocation erro
cbdf0 72 20 6f 63 63 75 72 73 2c 20 72 65 74 75 72 6e r occurs, return
cbe00 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 NULL..*/.static
cbe10 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 struct vxworksF
cbe20 69 6c 65 49 64 20 2a 76 78 77 6f 72 6b 73 46 69 ileId *vxworksFi
cbe30 6e 64 46 69 6c 65 49 64 28 63 6f 6e 73 74 20 63 ndFileId(const c
cbe40 68 61 72 20 2a 7a 41 62 73 6f 6c 75 74 65 4e 61 har *zAbsoluteNa
cbe50 6d 65 29 7b 0a 20 20 73 74 72 75 63 74 20 76 78 me){. struct vx
cbe60 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 4e 65 worksFileId *pNe
cbe70 77 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 73 65 w; /* se
cbe80 61 72 63 68 20 6b 65 79 20 61 6e 64 20 6e 65 77 arch key and new
cbe90 20 66 69 6c 65 20 49 44 20 2a 2f 0a 20 20 73 74 file ID */. st
cbea0 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 ruct vxworksFile
cbeb0 49 64 20 2a 70 43 61 6e 64 69 64 61 74 65 3b 20 Id *pCandidate;
cbec0 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 /* For looping
cbed0 20 6f 76 65 72 20 65 78 69 73 74 69 6e 67 20 66 over existing f
cbee0 69 6c 65 20 49 44 73 20 2a 2f 0a 20 20 69 6e 74 ile IDs */. int
cbef0 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 n;
cbf00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cbf10 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 41 /* Length of zA
cbf20 62 73 6f 6c 75 74 65 4e 61 6d 65 20 73 74 72 69 bsoluteName stri
cbf30 6e 67 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 ng */.. assert(
cbf40 20 7a 41 62 73 6f 6c 75 74 65 4e 61 6d 65 5b 30 zAbsoluteName[0
cbf50 5d 3d 3d 27 2f 27 20 29 3b 0a 20 20 6e 20 3d 20 ]=='/' );. n =
cbf60 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 41 62 73 (int)strlen(zAbs
cbf70 6f 6c 75 74 65 4e 61 6d 65 29 3b 0a 20 20 70 4e oluteName);. pN
cbf80 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c ew = sqlite3_mal
cbf90 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 4e 65 loc( sizeof(*pNe
cbfa0 77 29 20 2b 20 28 6e 2b 31 29 20 29 3b 0a 20 20 w) + (n+1) );.
cbfb0 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 if( pNew==0 ) re
cbfc0 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e turn 0;. pNew->
cbfd0 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 20 3d zCanonicalName =
cbfe0 20 28 63 68 61 72 2a 29 26 70 4e 65 77 5b 31 5d (char*)&pNew[1]
cbff0 3b 0a 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 2d ;. memcpy(pNew-
cc000 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c >zCanonicalName,
cc010 20 7a 41 62 73 6f 6c 75 74 65 4e 61 6d 65 2c 20 zAbsoluteName,
cc020 6e 2b 31 29 3b 0a 20 20 6e 20 3d 20 76 78 77 6f n+1);. n = vxwo
cc030 72 6b 73 53 69 6d 70 6c 69 66 79 4e 61 6d 65 28 rksSimplifyName(
cc040 70 4e 65 77 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c pNew->zCanonical
cc050 4e 61 6d 65 2c 20 6e 29 3b 0a 0a 20 20 2f 2a 20 Name, n);.. /*
cc060 53 65 61 72 63 68 20 66 6f 72 20 61 6e 20 65 78 Search for an ex
cc070 69 73 74 69 6e 67 20 65 6e 74 72 79 20 74 68 61 isting entry tha
cc080 74 20 6d 61 74 63 68 69 6e 67 20 74 68 65 20 63 t matching the c
cc090 61 6e 6f 6e 69 63 61 6c 20 6e 61 6d 65 2e 0a 20 anonical name..
cc0a0 20 2a 2a 20 49 66 20 66 6f 75 6e 64 2c 20 69 6e ** If found, in
cc0b0 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66 65 crement the refe
cc0c0 72 65 6e 63 65 20 63 6f 75 6e 74 20 61 6e 64 20 rence count and
cc0d0 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 return a pointer
cc0e0 20 74 6f 0a 20 20 2a 2a 20 74 68 65 20 65 78 69 to. ** the exi
cc0f0 73 74 69 6e 67 20 66 69 6c 65 20 49 44 2e 0a 20 sting file ID..
cc100 20 2a 2f 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d */. unixEnterM
cc110 75 74 65 78 28 29 3b 0a 20 20 66 6f 72 28 70 43 utex();. for(pC
cc120 61 6e 64 69 64 61 74 65 3d 76 78 77 6f 72 6b 73 andidate=vxworks
cc130 46 69 6c 65 4c 69 73 74 3b 20 70 43 61 6e 64 69 FileList; pCandi
cc140 64 61 74 65 3b 20 70 43 61 6e 64 69 64 61 74 65 date; pCandidate
cc150 3d 70 43 61 6e 64 69 64 61 74 65 2d 3e 70 4e 65 =pCandidate->pNe
cc160 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 43 61 xt){. if( pCa
cc170 6e 64 69 64 61 74 65 2d 3e 6e 4e 61 6d 65 3d 3d ndidate->nName==
cc180 6e 20 0a 20 20 20 20 20 26 26 20 6d 65 6d 63 6d n . && memcm
cc190 70 28 70 43 61 6e 64 69 64 61 74 65 2d 3e 7a 43 p(pCandidate->zC
cc1a0 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 70 4e anonicalName, pN
cc1b0 65 77 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 ew->zCanonicalNa
cc1c0 6d 65 2c 20 6e 29 3d 3d 30 0a 20 20 20 20 29 7b me, n)==0. ){
cc1d0 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f . sqlite3_
cc1e0 66 72 65 65 28 70 4e 65 77 29 3b 0a 20 20 20 20 free(pNew);.
cc1f0 20 20 20 70 43 61 6e 64 69 64 61 74 65 2d 3e 6e pCandidate->n
cc200 52 65 66 2b 2b 3b 0a 20 20 20 20 20 20 20 75 6e Ref++;. un
cc210 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a ixLeaveMutex();.
cc220 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 43 return pC
cc230 61 6e 64 69 64 61 74 65 3b 0a 20 20 20 20 7d 0a andidate;. }.
cc240 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 20 6d 61 74 }.. /* No mat
cc250 63 68 20 77 61 73 20 66 6f 75 6e 64 2e 20 20 57 ch was found. W
cc260 65 20 77 69 6c 6c 20 6d 61 6b 65 20 61 20 6e 65 e will make a ne
cc270 77 20 66 69 6c 65 20 49 44 20 2a 2f 0a 20 20 70 w file ID */. p
cc280 4e 65 77 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 New->nRef = 1;.
cc290 20 70 4e 65 77 2d 3e 6e 4e 61 6d 65 20 3d 20 6e pNew->nName = n
cc2a0 3b 0a 20 20 70 4e 65 77 2d 3e 70 4e 65 78 74 20 ;. pNew->pNext
cc2b0 3d 20 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73 = vxworksFileLis
cc2c0 74 3b 0a 20 20 76 78 77 6f 72 6b 73 46 69 6c 65 t;. vxworksFile
cc2d0 4c 69 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 75 List = pNew;. u
cc2e0 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b nixLeaveMutex();
cc2f0 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a . return pNew;.
cc300 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 }../*.** Decreme
cc310 6e 74 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 nt the reference
cc320 20 63 6f 75 6e 74 20 6f 6e 20 61 20 76 78 77 6f count on a vxwo
cc330 72 6b 73 46 69 6c 65 49 64 20 6f 62 6a 65 63 74 rksFileId object
cc340 2e 20 20 46 72 65 65 0a 2a 2a 20 74 68 65 20 6f . Free.** the o
cc350 62 6a 65 63 74 20 77 68 65 6e 20 74 68 65 20 72 bject when the r
cc360 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 72 eference count r
cc370 65 61 63 68 65 73 20 7a 65 72 6f 2e 0a 2a 2f 0a eaches zero..*/.
cc380 73 74 61 74 69 63 20 76 6f 69 64 20 76 78 77 6f static void vxwo
cc390 72 6b 73 52 65 6c 65 61 73 65 46 69 6c 65 49 64 rksReleaseFileId
cc3a0 28 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 (struct vxworksF
cc3b0 69 6c 65 49 64 20 2a 70 49 64 29 7b 0a 20 20 75 ileId *pId){. u
cc3c0 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b nixEnterMutex();
cc3d0 0a 20 20 61 73 73 65 72 74 28 20 70 49 64 2d 3e . assert( pId->
cc3e0 6e 52 65 66 3e 30 20 29 3b 0a 20 20 70 49 64 2d nRef>0 );. pId-
cc3f0 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 >nRef--;. if( p
cc400 49 64 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 Id->nRef==0 ){.
cc410 20 20 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b struct vxwork
cc420 73 46 69 6c 65 49 64 20 2a 2a 70 70 3b 0a 20 20 sFileId **pp;.
cc430 20 20 66 6f 72 28 70 70 3d 26 76 78 77 6f 72 6b for(pp=&vxwork
cc440 73 46 69 6c 65 4c 69 73 74 3b 20 2a 70 70 20 26 sFileList; *pp &
cc450 26 20 2a 70 70 21 3d 70 49 64 3b 20 70 70 20 3d & *pp!=pId; pp =
cc460 20 26 28 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29 &((*pp)->pNext)
cc470 29 7b 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){}. assert(
cc480 2a 70 70 3d 3d 70 49 64 20 29 3b 0a 20 20 20 20 *pp==pId );.
cc490 2a 70 70 20 3d 20 70 49 64 2d 3e 70 4e 65 78 74 *pp = pId->pNext
cc4a0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 ;. sqlite3_fr
cc4b0 65 65 28 70 49 64 29 3b 0a 20 20 7d 0a 20 20 75 ee(pId);. }. u
cc4c0 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b nixLeaveMutex();
cc4d0 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f .}.#endif /* OS_
cc4e0 56 58 57 4f 52 4b 53 20 2a 2f 0a 2f 2a 2a 2a 2a VXWORKS */./****
cc4f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
cc500 6f 66 20 55 6e 69 71 75 65 20 46 69 6c 65 20 49 of Unique File I
cc510 44 20 55 74 69 6c 69 74 79 20 55 73 65 64 20 42 D Utility Used B
cc520 79 20 56 78 57 6f 72 6b 73 20 2a 2a 2a 2a 2a 2a y VxWorks ******
cc530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a **********.*****
cc540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 2a *********/.../**
cc590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc5a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc5b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc5c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc5d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a ************.***
cc5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc5f0 2a 2a 2a 2a 2a 2a 2a 2a 20 50 6f 73 69 78 20 41 ******** Posix A
cc600 64 76 69 73 6f 72 79 20 4c 6f 63 6b 69 6e 67 20 dvisory Locking
cc610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
cc630 2a 2a 20 50 4f 53 49 58 20 61 64 76 69 73 6f 72 ** POSIX advisor
cc640 79 20 6c 6f 63 6b 73 20 61 72 65 20 62 72 6f 6b y locks are brok
cc650 65 6e 20 62 79 20 64 65 73 69 67 6e 2e 20 20 41 en by design. A
cc660 4e 53 49 20 53 54 44 20 31 30 30 33 2e 31 20 28 NSI STD 1003.1 (
cc670 31 39 39 36 29 0a 2a 2a 20 73 65 63 74 69 6f 6e 1996).** section
cc680 20 36 2e 35 2e 32 2e 32 20 6c 69 6e 65 73 20 34 6.5.2.2 lines 4
cc690 38 33 20 74 68 72 6f 75 67 68 20 34 39 30 20 73 83 through 490 s
cc6a0 70 65 63 69 66 79 20 74 68 61 74 20 77 68 65 6e pecify that when
cc6b0 20 61 20 70 72 6f 63 65 73 73 0a 2a 2a 20 73 65 a process.** se
cc6c0 74 73 20 6f 72 20 63 6c 65 61 72 73 20 61 20 6c ts or clears a l
cc6d0 6f 63 6b 2c 20 74 68 61 74 20 6f 70 65 72 61 74 ock, that operat
cc6e0 69 6f 6e 20 6f 76 65 72 72 69 64 65 73 20 61 6e ion overrides an
cc6f0 79 20 70 72 69 6f 72 20 6c 6f 63 6b 73 20 73 65 y prior locks se
cc700 74 0a 2a 2a 20 62 79 20 74 68 65 20 73 61 6d 65 t.** by the same
cc710 20 70 72 6f 63 65 73 73 2e 20 20 49 74 20 64 6f process. It do
cc720 65 73 20 6e 6f 74 20 65 78 70 6c 69 63 69 74 6c es not explicitl
cc730 79 20 73 61 79 20 73 6f 2c 20 62 75 74 20 74 68 y say so, but th
cc740 69 73 20 69 6d 70 6c 69 65 73 0a 2a 2a 20 74 68 is implies.** th
cc750 61 74 20 69 74 20 6f 76 65 72 72 69 64 65 73 20 at it overrides
cc760 6c 6f 63 6b 73 20 73 65 74 20 62 79 20 74 68 65 locks set by the
cc770 20 73 61 6d 65 20 70 72 6f 63 65 73 73 20 75 73 same process us
cc780 69 6e 67 20 61 20 64 69 66 66 65 72 65 6e 74 0a ing a different.
cc790 2a 2a 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 ** file descript
cc7a0 6f 72 2e 20 20 43 6f 6e 73 69 64 65 72 20 74 68 or. Consider th
cc7b0 69 73 20 74 65 73 74 20 63 61 73 65 3a 0a 2a 2a is test case:.**
cc7c0 0a 2a 2a 20 20 20 20 20 20 20 69 6e 74 20 66 64 .** int fd
cc7d0 31 20 3d 20 6f 70 65 6e 28 22 2e 2f 66 69 6c 65 1 = open("./file
cc7e0 31 22 2c 20 4f 5f 52 44 57 52 7c 4f 5f 43 52 45 1", O_RDWR|O_CRE
cc7f0 41 54 2c 20 30 36 34 34 29 3b 0a 2a 2a 20 20 20 AT, 0644);.**
cc800 20 20 20 20 69 6e 74 20 66 64 32 20 3d 20 6f 70 int fd2 = op
cc810 65 6e 28 22 2e 2f 66 69 6c 65 32 22 2c 20 4f 5f en("./file2", O_
cc820 52 44 57 52 7c 4f 5f 43 52 45 41 54 2c 20 30 36 RDWR|O_CREAT, 06
cc830 34 34 29 3b 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f 44);.**.** Suppo
cc840 73 65 20 2e 2f 66 69 6c 65 31 20 61 6e 64 20 2e se ./file1 and .
cc850 2f 66 69 6c 65 32 20 61 72 65 20 72 65 61 6c 6c /file2 are reall
cc860 79 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 20 y the same file
cc870 28 62 65 63 61 75 73 65 0a 2a 2a 20 6f 6e 65 20 (because.** one
cc880 69 73 20 61 20 68 61 72 64 20 6f 72 20 73 79 6d is a hard or sym
cc890 62 6f 6c 69 63 20 6c 69 6e 6b 20 74 6f 20 74 68 bolic link to th
cc8a0 65 20 6f 74 68 65 72 29 20 74 68 65 6e 20 69 66 e other) then if
cc8b0 20 79 6f 75 20 73 65 74 0a 2a 2a 20 61 6e 20 65 you set.** an e
cc8c0 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 6f 6e xclusive lock on
cc8d0 20 66 64 31 2c 20 74 68 65 6e 20 74 72 79 20 74 fd1, then try t
cc8e0 6f 20 67 65 74 20 61 6e 20 65 78 63 6c 75 73 69 o get an exclusi
cc8f0 76 65 20 6c 6f 63 6b 0a 2a 2a 20 6f 6e 20 66 64 ve lock.** on fd
cc900 32 2c 20 69 74 20 77 6f 72 6b 73 2e 20 20 49 20 2, it works. I
cc910 77 6f 75 6c 64 20 68 61 76 65 20 65 78 70 65 63 would have expec
cc920 74 65 64 20 74 68 65 20 73 65 63 6f 6e 64 20 6c ted the second l
cc930 6f 63 6b 20 74 6f 0a 2a 2a 20 66 61 69 6c 20 73 ock to.** fail s
cc940 69 6e 63 65 20 74 68 65 72 65 20 77 61 73 20 61 ince there was a
cc950 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f 6e lready a lock on
cc960 20 74 68 65 20 66 69 6c 65 20 64 75 65 20 74 6f the file due to
cc970 20 66 64 31 2e 0a 2a 2a 20 42 75 74 20 6e 6f 74 fd1..** But not
cc980 20 73 6f 2e 20 20 53 69 6e 63 65 20 62 6f 74 68 so. Since both
cc990 20 6c 6f 63 6b 73 20 63 61 6d 65 20 66 72 6f 6d locks came from
cc9a0 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 the same proces
cc9b0 73 2c 20 74 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 s, the.** second
cc9c0 20 6f 76 65 72 72 69 64 65 73 20 74 68 65 20 66 overrides the f
cc9d0 69 72 73 74 2c 20 65 76 65 6e 20 74 68 6f 75 67 irst, even thoug
cc9e0 68 20 74 68 65 79 20 77 65 72 65 20 6f 6e 20 64 h they were on d
cc9f0 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66 69 6c 65 ifferent.** file
cca00 20 64 65 73 63 72 69 70 74 6f 72 73 20 6f 70 65 descriptors ope
cca10 6e 65 64 20 6f 6e 20 64 69 66 66 65 72 65 6e 74 ned on different
cca20 20 66 69 6c 65 20 6e 61 6d 65 73 2e 0a 2a 2a 0a file names..**.
cca30 2a 2a 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68 ** This means th
cca40 61 74 20 77 65 20 63 61 6e 6e 6f 74 20 75 73 65 at we cannot use
cca50 20 50 4f 53 49 58 20 6c 6f 63 6b 73 20 74 6f 20 POSIX locks to
cca60 73 79 6e 63 68 72 6f 6e 69 7a 65 20 66 69 6c 65 synchronize file
cca70 20 61 63 63 65 73 73 0a 2a 2a 20 61 6d 6f 6e 67 access.** among
cca80 20 63 6f 6d 70 65 74 69 6e 67 20 74 68 72 65 61 competing threa
cca90 64 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 70 ds of the same p
ccaa0 72 6f 63 65 73 73 2e 20 20 50 4f 53 49 58 20 6c rocess. POSIX l
ccab0 6f 63 6b 73 20 77 69 6c 6c 20 77 6f 72 6b 20 66 ocks will work f
ccac0 69 6e 65 0a 2a 2a 20 74 6f 20 73 79 6e 63 68 72 ine.** to synchr
ccad0 6f 6e 69 7a 65 20 61 63 63 65 73 73 20 66 6f 72 onize access for
ccae0 20 74 68 72 65 61 64 73 20 69 6e 20 73 65 70 61 threads in sepa
ccaf0 72 61 74 65 20 70 72 6f 63 65 73 73 65 73 2c 20 rate processes,
ccb00 62 75 74 20 6e 6f 74 0a 2a 2a 20 74 68 72 65 61 but not.** threa
ccb10 64 73 20 77 69 74 68 69 6e 20 74 68 65 20 73 61 ds within the sa
ccb20 6d 65 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a me process..**.*
ccb30 2a 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 * To work around
ccb40 20 74 68 65 20 70 72 6f 62 6c 65 6d 2c 20 53 51 the problem, SQ
ccb50 4c 69 74 65 20 68 61 73 20 74 6f 20 6d 61 6e 61 Lite has to mana
ccb60 67 65 20 66 69 6c 65 20 6c 6f 63 6b 73 20 69 6e ge file locks in
ccb70 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20 6f 6e 20 69 ternally.** on i
ccb80 74 73 20 6f 77 6e 2e 20 20 57 68 65 6e 65 76 65 ts own. Wheneve
ccb90 72 20 61 20 6e 65 77 20 64 61 74 61 62 61 73 65 r a new database
ccba0 20 69 73 20 6f 70 65 6e 65 64 2c 20 77 65 20 68 is opened, we h
ccbb0 61 76 65 20 74 6f 20 66 69 6e 64 20 74 68 65 0a ave to find the.
ccbc0 2a 2a 20 73 70 65 63 69 66 69 63 20 69 6e 6f 64 ** specific inod
ccbd0 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
ccbe0 65 20 66 69 6c 65 20 28 74 68 65 20 69 6e 6f 64 e file (the inod
ccbf0 65 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 e is determined
ccc00 62 79 20 74 68 65 0a 2a 2a 20 73 74 5f 64 65 76 by the.** st_dev
ccc10 20 61 6e 64 20 73 74 5f 69 6e 6f 20 66 69 65 6c and st_ino fiel
ccc20 64 73 20 6f 66 20 74 68 65 20 73 74 61 74 20 73 ds of the stat s
ccc30 74 72 75 63 74 75 72 65 20 74 68 61 74 20 66 73 tructure that fs
ccc40 74 61 74 28 29 20 66 69 6c 6c 73 20 69 6e 29 0a tat() fills in).
ccc50 2a 2a 20 61 6e 64 20 63 68 65 63 6b 20 66 6f 72 ** and check for
ccc60 20 6c 6f 63 6b 73 20 61 6c 72 65 61 64 79 20 65 locks already e
ccc70 78 69 73 74 69 6e 67 20 6f 6e 20 74 68 61 74 20 xisting on that
ccc80 69 6e 6f 64 65 2e 20 20 57 68 65 6e 20 6c 6f 63 inode. When loc
ccc90 6b 73 20 61 72 65 0a 2a 2a 20 63 72 65 61 74 65 ks are.** create
ccca0 64 20 6f 72 20 72 65 6d 6f 76 65 64 2c 20 77 65 d or removed, we
cccb0 20 68 61 76 65 20 74 6f 20 6c 6f 6f 6b 20 61 74 have to look at
cccc0 20 6f 75 72 20 6f 77 6e 20 69 6e 74 65 72 6e 61 our own interna
cccd0 6c 20 72 65 63 6f 72 64 20 6f 66 20 74 68 65 0a l record of the.
ccce0 2a 2a 20 6c 6f 63 6b 73 20 74 6f 20 73 65 65 20 ** locks to see
cccf0 69 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 if another threa
ccd00 64 20 68 61 73 20 70 72 65 76 69 6f 75 73 6c 79 d has previously
ccd10 20 73 65 74 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 set a lock on t
ccd20 68 61 74 20 73 61 6d 65 0a 2a 2a 20 69 6e 6f 64 hat same.** inod
ccd30 65 2e 0a 2a 2a 0a 2a 2a 20 28 41 73 69 64 65 3a e..**.** (Aside:
ccd40 20 54 68 65 20 75 73 65 20 6f 66 20 69 6e 6f 64 The use of inod
ccd50 65 20 6e 75 6d 62 65 72 73 20 61 73 20 75 6e 69 e numbers as uni
ccd60 71 75 65 20 49 44 73 20 64 6f 65 73 20 6e 6f 74 que IDs does not
ccd70 20 77 6f 72 6b 20 6f 6e 20 56 78 57 6f 72 6b 73 work on VxWorks
ccd80 2e 0a 2a 2a 20 46 6f 72 20 56 78 57 6f 72 6b 73 ..** For VxWorks
ccd90 2c 20 77 65 20 68 61 76 65 20 74 6f 20 75 73 65 , we have to use
ccda0 20 74 68 65 20 61 6c 74 65 72 6e 61 74 69 76 65 the alternative
ccdb0 20 75 6e 69 71 75 65 20 49 44 20 73 79 73 74 65 unique ID syste
ccdc0 6d 20 62 61 73 65 64 20 6f 6e 0a 2a 2a 20 63 61 m based on.** ca
ccdd0 6e 6f 6e 69 63 61 6c 20 66 69 6c 65 6e 61 6d 65 nonical filename
ccde0 20 61 6e 64 20 69 6d 70 6c 65 6d 65 6e 74 65 64 and implemented
ccdf0 20 69 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 in the previous
cce00 20 64 69 76 69 73 69 6f 6e 2e 29 0a 2a 2a 0a 2a division.).**.*
cce10 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 66 69 * The sqlite3_fi
cce20 6c 65 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 le structure for
cce30 20 50 4f 53 49 58 20 69 73 20 6e 6f 20 6c 6f 6e POSIX is no lon
cce40 67 65 72 20 6a 75 73 74 20 61 6e 20 69 6e 74 65 ger just an inte
cce50 67 65 72 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 ger file.** desc
cce60 72 69 70 74 6f 72 2e 20 20 49 74 20 69 73 20 6e riptor. It is n
cce70 6f 77 20 61 20 73 74 72 75 63 74 75 72 65 20 74 ow a structure t
cce80 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20 69 6e hat holds the in
cce90 74 65 67 65 72 20 66 69 6c 65 0a 2a 2a 20 64 65 teger file.** de
ccea0 73 63 72 69 70 74 6f 72 20 61 6e 64 20 61 20 70 scriptor and a p
cceb0 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 72 75 ointer to a stru
ccec0 63 74 75 72 65 20 74 68 61 74 20 64 65 73 63 72 cture that descr
cced0 69 62 65 73 20 74 68 65 20 69 6e 74 65 72 6e 61 ibes the interna
ccee0 6c 0a 2a 2a 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 l.** locks on th
ccef0 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 e corresponding
ccf00 69 6e 6f 64 65 2e 20 20 54 68 65 72 65 20 69 73 inode. There is
ccf10 20 6f 6e 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72 one locking str
ccf20 75 63 74 75 72 65 0a 2a 2a 20 70 65 72 20 69 6e ucture.** per in
ccf30 6f 64 65 2c 20 73 6f 20 69 66 20 74 68 65 20 73 ode, so if the s
ccf40 61 6d 65 20 69 6e 6f 64 65 20 69 73 20 6f 70 65 ame inode is ope
ccf50 6e 65 64 20 74 77 69 63 65 2c 20 62 6f 74 68 20 ned twice, both
ccf60 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74 75 unixFile structu
ccf70 72 65 73 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 res.** point to
ccf80 74 68 65 20 73 61 6d 65 20 6c 6f 63 6b 69 6e 67 the same locking
ccf90 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 structure. The
ccfa0 20 6c 6f 63 6b 69 6e 67 20 73 74 72 75 63 74 75 locking structu
ccfb0 72 65 20 6b 65 65 70 73 0a 2a 2a 20 61 20 72 65 re keeps.** a re
ccfc0 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 28 73 ference count (s
ccfd0 6f 20 77 65 20 77 69 6c 6c 20 6b 6e 6f 77 20 77 o we will know w
ccfe0 68 65 6e 20 74 6f 20 64 65 6c 65 74 65 20 69 74 hen to delete it
ccff0 29 20 61 6e 64 20 61 20 22 63 6e 74 22 0a 2a 2a ) and a "cnt".**
cd000 20 66 69 65 6c 64 20 74 68 61 74 20 74 65 6c 6c field that tell
cd010 73 20 75 73 20 69 74 73 20 69 6e 74 65 72 6e 61 s us its interna
cd020 6c 20 6c 6f 63 6b 20 73 74 61 74 75 73 2e 20 20 l lock status.
cd030 63 6e 74 3d 3d 30 20 6d 65 61 6e 73 20 74 68 65 cnt==0 means the
cd040 0a 2a 2a 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f .** file is unlo
cd050 63 6b 65 64 2e 20 20 63 6e 74 3d 3d 2d 31 20 6d cked. cnt==-1 m
cd060 65 61 6e 73 20 74 68 65 20 66 69 6c 65 20 68 61 eans the file ha
cd070 73 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c s an exclusive l
cd080 6f 63 6b 2e 0a 2a 2a 20 63 6e 74 3e 30 20 6d 65 ock..** cnt>0 me
cd090 61 6e 73 20 74 68 65 72 65 20 61 72 65 20 63 6e ans there are cn
cd0a0 74 20 73 68 61 72 65 64 20 6c 6f 63 6b 73 20 6f t shared locks o
cd0b0 6e 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a n the file..**.*
cd0c0 2a 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f * Any attempt to
cd0d0 20 6c 6f 63 6b 20 6f 72 20 75 6e 6c 6f 63 6b 20 lock or unlock
cd0e0 61 20 66 69 6c 65 20 66 69 72 73 74 20 63 68 65 a file first che
cd0f0 63 6b 73 20 74 68 65 20 6c 6f 63 6b 69 6e 67 0a cks the locking.
cd100 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 ** structure. T
cd110 68 65 20 66 63 6e 74 6c 28 29 20 73 79 73 74 65 he fcntl() syste
cd120 6d 20 63 61 6c 6c 20 69 73 20 6f 6e 6c 79 20 69 m call is only i
cd130 6e 76 6f 6b 65 64 20 74 6f 20 73 65 74 20 61 20 nvoked to set a
cd140 0a 2a 2a 20 50 4f 53 49 58 20 6c 6f 63 6b 20 69 .** POSIX lock i
cd150 66 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6c f the internal l
cd160 6f 63 6b 20 73 74 72 75 63 74 75 72 65 20 74 72 ock structure tr
cd170 61 6e 73 69 74 69 6f 6e 73 20 62 65 74 77 65 65 ansitions betwee
cd180 6e 0a 2a 2a 20 61 20 6c 6f 63 6b 65 64 20 61 6e n.** a locked an
cd190 64 20 61 6e 20 75 6e 6c 6f 63 6b 65 64 20 73 74 d an unlocked st
cd1a0 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 42 75 74 20 77 ate..**.** But w
cd1b0 61 69 74 3a 20 20 74 68 65 72 65 20 61 72 65 20 ait: there are
cd1c0 79 65 74 20 6d 6f 72 65 20 70 72 6f 62 6c 65 6d yet more problem
cd1d0 73 20 77 69 74 68 20 50 4f 53 49 58 20 61 64 76 s with POSIX adv
cd1e0 69 73 6f 72 79 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a isory locks..**.
cd1f0 2a 2a 20 49 66 20 79 6f 75 20 63 6c 6f 73 65 20 ** If you close
cd200 61 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f a file descripto
cd210 72 20 74 68 61 74 20 70 6f 69 6e 74 73 20 74 6f r that points to
cd220 20 61 20 66 69 6c 65 20 74 68 61 74 20 68 61 73 a file that has
cd230 20 6c 6f 63 6b 73 2c 0a 2a 2a 20 61 6c 6c 20 6c locks,.** all l
cd240 6f 63 6b 73 20 6f 6e 20 74 68 61 74 20 66 69 6c ocks on that fil
cd250 65 20 74 68 61 74 20 61 72 65 20 6f 77 6e 65 64 e that are owned
cd260 20 62 79 20 74 68 65 20 63 75 72 72 65 6e 74 20 by the current
cd270 70 72 6f 63 65 73 73 20 61 72 65 0a 2a 2a 20 72 process are.** r
cd280 65 6c 65 61 73 65 64 2e 20 20 54 6f 20 77 6f 72 eleased. To wor
cd290 6b 20 61 72 6f 75 6e 64 20 74 68 69 73 20 70 72 k around this pr
cd2a0 6f 62 6c 65 6d 2c 20 65 61 63 68 20 75 6e 69 78 oblem, each unix
cd2b0 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 63 File structure c
cd2c0 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 70 6f 69 ontains.** a poi
cd2d0 6e 74 65 72 20 74 6f 20 61 6e 20 75 6e 69 78 4f nter to an unixO
cd2e0 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 penCnt structure
cd2f0 2e 20 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20 . There is one
cd300 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 unixOpenCnt stru
cd310 63 74 75 72 65 0a 2a 2a 20 70 65 72 20 6f 70 65 cture.** per ope
cd320 6e 20 69 6e 6f 64 65 2c 20 77 68 69 63 68 20 6d n inode, which m
cd330 65 61 6e 73 20 74 68 61 74 20 6d 75 6c 74 69 70 eans that multip
cd340 6c 65 20 75 6e 69 78 46 69 6c 65 20 63 61 6e 20 le unixFile can
cd350 70 6f 69 6e 74 20 74 6f 20 61 20 73 69 6e 67 6c point to a singl
cd360 65 0a 2a 2a 20 75 6e 69 78 4f 70 65 6e 43 6e 74 e.** unixOpenCnt
cd370 2e 20 20 57 68 65 6e 20 61 6e 20 61 74 74 65 6d . When an attem
cd380 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 63 6c pt is made to cl
cd390 6f 73 65 20 61 6e 20 75 6e 69 78 46 69 6c 65 2c ose an unixFile,
cd3a0 20 69 66 20 74 68 65 72 65 20 61 72 65 0a 2a 2a if there are.**
cd3b0 20 6f 74 68 65 72 20 75 6e 69 78 46 69 6c 65 20 other unixFile
cd3c0 6f 70 65 6e 20 6f 6e 20 74 68 65 20 73 61 6d 65 open on the same
cd3d0 20 69 6e 6f 64 65 20 74 68 61 74 20 61 72 65 20 inode that are
cd3e0 68 6f 6c 64 69 6e 67 20 6c 6f 63 6b 73 2c 20 74 holding locks, t
cd3f0 68 65 20 63 61 6c 6c 0a 2a 2a 20 74 6f 20 63 6c he call.** to cl
cd400 6f 73 65 28 29 20 74 68 65 20 66 69 6c 65 20 64 ose() the file d
cd410 65 73 63 72 69 70 74 6f 72 20 69 73 20 64 65 66 escriptor is def
cd420 65 72 72 65 64 20 75 6e 74 69 6c 20 61 6c 6c 20 erred until all
cd430 6f 66 20 74 68 65 20 6c 6f 63 6b 73 20 63 6c 65 of the locks cle
cd440 61 72 2e 0a 2a 2a 20 54 68 65 20 75 6e 69 78 4f ar..** The unixO
cd450 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 penCnt structure
cd460 20 6b 65 65 70 73 20 61 20 6c 69 73 74 20 6f 66 keeps a list of
cd470 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
cd480 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 0a 2a s that need to.*
cd490 2a 20 62 65 20 63 6c 6f 73 65 64 20 61 6e 64 20 * be closed and
cd4a0 74 68 61 74 20 6c 69 73 74 20 69 73 20 77 61 6c that list is wal
cd4b0 6b 65 64 20 28 61 6e 64 20 63 6c 65 61 72 65 64 ked (and cleared
cd4c0 29 20 77 68 65 6e 20 74 68 65 20 6c 61 73 74 20 ) when the last
cd4d0 6c 6f 63 6b 0a 2a 2a 20 63 6c 65 61 72 73 2e 0a lock.** clears..
cd4e0 2a 2a 0a 2a 2a 20 59 65 74 20 61 6e 6f 74 68 65 **.** Yet anothe
cd4f0 72 20 70 72 6f 62 6c 65 6d 3a 20 20 4c 69 6e 75 r problem: Linu
cd500 78 54 68 72 65 61 64 73 20 64 6f 20 6e 6f 74 20 xThreads do not
cd510 70 6c 61 79 20 77 65 6c 6c 20 77 69 74 68 20 70 play well with p
cd520 6f 73 69 78 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a osix locks..**.*
cd530 2a 20 4d 61 6e 79 20 6f 6c 64 65 72 20 76 65 72 * Many older ver
cd540 73 69 6f 6e 73 20 6f 66 20 6c 69 6e 75 78 20 75 sions of linux u
cd550 73 65 20 74 68 65 20 4c 69 6e 75 78 54 68 72 65 se the LinuxThre
cd560 61 64 73 20 6c 69 62 72 61 72 79 20 77 68 69 63 ads library whic
cd570 68 20 69 73 0a 2a 2a 20 6e 6f 74 20 70 6f 73 69 h is.** not posi
cd580 78 20 63 6f 6d 70 6c 69 61 6e 74 2e 20 20 55 6e x compliant. Un
cd590 64 65 72 20 4c 69 6e 75 78 54 68 72 65 61 64 73 der LinuxThreads
cd5a0 2c 20 61 20 6c 6f 63 6b 20 63 72 65 61 74 65 64 , a lock created
cd5b0 20 62 79 20 74 68 72 65 61 64 0a 2a 2a 20 41 20 by thread.** A
cd5c0 63 61 6e 6e 6f 74 20 62 65 20 6d 6f 64 69 66 69 cannot be modifi
cd5d0 65 64 20 6f 72 20 6f 76 65 72 72 69 64 64 65 6e ed or overridden
cd5e0 20 62 79 20 61 20 64 69 66 66 65 72 65 6e 74 20 by a different
cd5f0 74 68 72 65 61 64 20 42 2e 0a 2a 2a 20 4f 6e 6c thread B..** Onl
cd600 79 20 74 68 72 65 61 64 20 41 20 63 61 6e 20 6d y thread A can m
cd610 6f 64 69 66 79 20 74 68 65 20 6c 6f 63 6b 2e 20 odify the lock.
cd620 20 4c 6f 63 6b 69 6e 67 20 62 65 68 61 76 69 6f Locking behavio
cd630 72 20 69 73 20 63 6f 72 72 65 63 74 0a 2a 2a 20 r is correct.**
cd640 69 66 20 74 68 65 20 61 70 70 6c 69 61 74 69 6f if the appliatio
cd650 6e 20 75 73 65 73 20 74 68 65 20 6e 65 77 65 72 n uses the newer
cd660 20 4e 61 74 69 76 65 20 50 6f 73 69 78 20 54 68 Native Posix Th
cd670 72 65 61 64 20 4c 69 62 72 61 72 79 20 28 4e 50 read Library (NP
cd680 54 4c 29 0a 2a 2a 20 6f 6e 20 6c 69 6e 75 78 20 TL).** on linux
cd690 2d 20 77 69 74 68 20 4e 50 54 4c 20 61 20 6c 6f - with NPTL a lo
cd6a0 63 6b 20 63 72 65 61 74 65 64 20 62 79 20 74 68 ck created by th
cd6b0 72 65 61 64 20 41 20 63 61 6e 20 6f 76 65 72 72 read A can overr
cd6c0 69 64 65 20 6c 6f 63 6b 73 0a 2a 2a 20 69 6e 20 ide locks.** in
cd6d0 74 68 72 65 61 64 20 42 2e 20 20 42 75 74 20 74 thread B. But t
cd6e0 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20 74 here is no way t
cd6f0 6f 20 6b 6e 6f 77 20 61 74 20 63 6f 6d 70 69 6c o know at compil
cd700 65 2d 74 69 6d 65 20 77 68 69 63 68 0a 2a 2a 20 e-time which.**
cd710 74 68 72 65 61 64 69 6e 67 20 6c 69 62 72 61 72 threading librar
cd720 79 20 69 73 20 62 65 69 6e 67 20 75 73 65 64 2e y is being used.
cd730 20 20 53 6f 20 74 68 65 72 65 20 69 73 20 6e 6f So there is no
cd740 20 77 61 79 20 74 6f 20 6b 6e 6f 77 20 61 74 0a way to know at.
cd750 2a 2a 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 ** compile-time
cd760 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 whether or not t
cd770 68 72 65 61 64 20 41 20 63 61 6e 20 6f 76 65 72 hread A can over
cd780 72 69 64 65 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 ride locks on th
cd790 72 65 61 64 20 42 2e 0a 2a 2a 20 57 65 20 68 61 read B..** We ha
cd7a0 76 65 20 74 6f 20 64 6f 20 61 20 72 75 6e 2d 74 ve to do a run-t
cd7b0 69 6d 65 20 63 68 65 63 6b 20 74 6f 20 64 69 73 ime check to dis
cd7c0 63 6f 76 65 72 20 74 68 65 20 62 65 68 61 76 69 cover the behavi
cd7d0 6f 72 20 6f 66 20 74 68 65 0a 2a 2a 20 63 75 72 or of the.** cur
cd7e0 72 65 6e 74 20 70 72 6f 63 65 73 73 2e 0a 2a 2a rent process..**
cd7f0 0a 2a 2a 20 4f 6e 20 73 79 73 74 65 6d 73 20 77 .** On systems w
cd800 68 65 72 65 20 74 68 72 65 61 64 20 41 20 69 73 here thread A is
cd810 20 75 6e 61 62 6c 65 20 74 6f 20 6d 6f 64 69 66 unable to modif
cd820 79 20 6c 6f 63 6b 73 20 63 72 65 61 74 65 64 20 y locks created
cd830 62 79 0a 2a 2a 20 74 68 72 65 61 64 20 42 2c 20 by.** thread B,
cd840 77 65 20 68 61 76 65 20 74 6f 20 6b 65 65 70 20 we have to keep
cd850 74 72 61 63 6b 20 6f 66 20 77 68 69 63 68 20 74 track of which t
cd860 68 72 65 61 64 20 63 72 65 61 74 65 64 20 65 61 hread created ea
cd870 63 68 0a 2a 2a 20 6c 6f 63 6b 2e 20 20 48 65 6e ch.** lock. Hen
cd880 63 65 20 74 68 65 72 65 20 69 73 20 61 6e 20 65 ce there is an e
cd890 78 74 72 61 20 66 69 65 6c 64 20 69 6e 20 74 68 xtra field in th
cd8a0 65 20 6b 65 79 20 74 6f 20 74 68 65 20 75 6e 69 e key to the uni
cd8b0 78 4c 6f 63 6b 49 6e 66 6f 0a 2a 2a 20 73 74 72 xLockInfo.** str
cd8c0 75 63 74 75 72 65 20 74 6f 20 72 65 63 6f 72 64 ucture to record
cd8d0 20 74 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f this informatio
cd8e0 6e 2e 20 20 41 6e 64 20 6f 6e 20 74 68 6f 73 65 n. And on those
cd8f0 20 73 79 73 74 65 6d 73 20 69 74 0a 2a 2a 20 69 systems it.** i
cd900 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 62 65 67 s illegal to beg
cd910 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e in a transaction
cd920 20 69 6e 20 6f 6e 65 20 74 68 72 65 61 64 20 61 in one thread a
cd930 6e 64 20 66 69 6e 69 73 68 20 69 74 0a 2a 2a 20 nd finish it.**
cd940 69 6e 20 61 6e 6f 74 68 65 72 2e 20 20 46 6f 72 in another. For
cd950 20 74 68 69 73 20 6c 61 74 74 65 72 20 72 65 73 this latter res
cd960 74 72 69 63 74 69 6f 6e 2c 20 74 68 65 72 65 20 triction, there
cd970 69 73 20 6e 6f 20 77 6f 72 6b 2d 61 72 6f 75 6e is no work-aroun
cd980 64 2e 0a 2a 2a 20 49 74 20 69 73 20 61 20 6c 69 d..** It is a li
cd990 6d 69 74 61 74 69 6f 6e 20 6f 66 20 4c 69 6e 75 mitation of Linu
cd9a0 78 54 68 72 65 61 64 73 2e 0a 2a 2f 0a 0a 2f 2a xThreads..*/../*
cd9b0 0a 2a 2a 20 53 65 74 20 6f 72 20 63 68 65 63 6b .** Set or check
cd9c0 20 74 68 65 20 75 6e 69 78 46 69 6c 65 2e 74 69 the unixFile.ti
cd9d0 64 20 66 69 65 6c 64 2e 20 20 54 68 69 73 20 66 d field. This f
cd9e0 69 65 6c 64 20 69 73 20 73 65 74 20 77 68 65 6e ield is set when
cd9f0 20 61 6e 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 an unixFile.**
cda00 69 73 20 66 69 72 73 74 20 6f 70 65 6e 65 64 2e is first opened.
cda10 20 20 41 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 All subsequent
cda20 20 75 73 65 73 20 6f 66 20 74 68 65 20 75 6e 69 uses of the uni
cda30 78 46 69 6c 65 20 76 65 72 69 66 79 20 74 68 61 xFile verify tha
cda40 74 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 74 68 t the.** same th
cda50 72 65 61 64 20 69 73 20 6f 70 65 72 61 74 69 6e read is operatin
cda60 67 20 6f 6e 20 74 68 65 20 75 6e 69 78 46 69 6c g on the unixFil
cda70 65 2e 20 20 53 6f 6d 65 20 6f 70 65 72 61 74 69 e. Some operati
cda80 6e 67 20 73 79 73 74 65 6d 73 20 64 6f 0a 2a 2a ng systems do.**
cda90 20 6e 6f 74 20 61 6c 6c 6f 77 20 6c 6f 63 6b 73 not allow locks
cdaa0 20 74 6f 20 62 65 20 6f 76 65 72 72 69 64 64 65 to be overridde
cdab0 6e 20 62 79 20 6f 74 68 65 72 20 74 68 72 65 61 n by other threa
cdac0 64 73 20 61 6e 64 20 74 68 61 74 20 72 65 73 74 ds and that rest
cdad0 72 69 63 74 69 6f 6e 0a 2a 2a 20 6d 65 61 6e 73 riction.** means
cdae0 20 74 68 61 74 20 73 71 6c 69 74 65 33 2a 20 64 that sqlite3* d
cdaf0 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 73 20 atabase handles
cdb00 63 61 6e 6e 6f 74 20 62 65 20 6d 6f 76 65 64 20 cannot be moved
cdb10 66 72 6f 6d 20 6f 6e 65 20 74 68 72 65 61 64 0a from one thread.
cdb20 2a 2a 20 74 6f 20 61 6e 6f 74 68 65 72 20 77 68 ** to another wh
cdb30 69 6c 65 20 6c 6f 63 6b 73 20 61 72 65 20 68 65 ile locks are he
cdb40 6c 64 2e 0a 2a 2a 0a 2a 2a 20 56 65 72 73 69 6f ld..**.** Versio
cdb50 6e 20 33 2e 33 2e 31 20 28 32 30 30 36 2d 30 31 n 3.3.1 (2006-01
cdb60 2d 31 35 29 3a 20 20 75 6e 69 78 46 69 6c 65 20 -15): unixFile
cdb70 63 61 6e 20 62 65 20 6d 6f 76 65 64 20 66 72 6f can be moved fro
cdb80 6d 20 6f 6e 65 20 74 68 72 65 61 64 20 74 6f 0a m one thread to.
cdb90 2a 2a 20 61 6e 6f 74 68 65 72 20 61 73 20 6c 6f ** another as lo
cdba0 6e 67 20 61 73 20 77 65 20 61 72 65 20 72 75 6e ng as we are run
cdbb0 6e 69 6e 67 20 6f 6e 20 61 20 73 79 73 74 65 6d ning on a system
cdbc0 20 74 68 61 74 20 73 75 70 70 6f 72 74 73 20 74 that supports t
cdbd0 68 72 65 61 64 73 0a 2a 2a 20 6f 76 65 72 72 69 hreads.** overri
cdbe0 64 69 6e 67 20 65 61 63 68 20 6f 74 68 65 72 73 ding each others
cdbf0 20 6c 6f 63 6b 73 20 28 77 68 69 63 68 20 69 73 locks (which is
cdc00 20 6e 6f 77 20 74 68 65 20 6d 6f 73 74 20 63 6f now the most co
cdc10 6d 6d 6f 6e 20 62 65 68 61 76 69 6f 72 29 0a 2a mmon behavior).*
cdc20 2a 20 6f 72 20 69 66 20 6e 6f 20 6c 6f 63 6b 73 * or if no locks
cdc30 20 61 72 65 20 68 65 6c 64 2e 20 20 42 75 74 20 are held. But
cdc40 74 68 65 20 75 6e 69 78 46 69 6c 65 2e 70 4c 6f the unixFile.pLo
cdc50 63 6b 20 66 69 65 6c 64 20 6e 65 65 64 73 20 74 ck field needs t
cdc60 6f 20 62 65 0a 2a 2a 20 72 65 63 6f 6d 70 75 74 o be.** recomput
cdc70 65 64 20 62 65 63 61 75 73 65 20 69 74 73 20 6b ed because its k
cdc80 65 79 20 69 6e 63 6c 75 64 65 73 20 74 68 65 20 ey includes the
cdc90 74 68 72 65 61 64 2d 69 64 2e 20 20 53 65 65 20 thread-id. See
cdca0 74 68 65 20 0a 2a 2a 20 74 72 61 6e 73 66 65 72 the .** transfer
cdcb0 4f 77 6e 65 72 73 68 69 70 28 29 20 66 75 6e 63 Ownership() func
cdcc0 74 69 6f 6e 20 62 65 6c 6f 77 20 66 6f 72 20 61 tion below for a
cdcd0 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d dditional inform
cdce0 61 74 69 6f 6e 0a 2a 2f 0a 23 69 66 20 53 51 4c ation.*/.#if SQL
cdcf0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 ITE_THREADSAFE &
cdd00 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 & defined(__linu
cdd10 78 5f 5f 29 0a 23 20 64 65 66 69 6e 65 20 53 45 x__).# define SE
cdd20 54 5f 54 48 52 45 41 44 49 44 28 58 29 20 20 20 T_THREADID(X)
cdd30 28 58 29 2d 3e 74 69 64 20 3d 20 70 74 68 72 65 (X)->tid = pthre
cdd40 61 64 5f 73 65 6c 66 28 29 0a 23 20 64 65 66 69 ad_self().# defi
cdd50 6e 65 20 43 48 45 43 4b 5f 54 48 52 45 41 44 49 ne CHECK_THREADI
cdd60 44 28 58 29 20 28 74 68 72 65 61 64 73 4f 76 65 D(X) (threadsOve
cdd70 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c rrideEachOthersL
cdd80 6f 63 6b 73 3d 3d 30 20 26 26 20 5c 0a 20 20 20 ocks==0 && \.
cdd90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cdda0 20 20 20 20 20 20 20 20 20 21 70 74 68 72 65 61 !pthrea
cddb0 64 5f 65 71 75 61 6c 28 28 58 29 2d 3e 74 69 64 d_equal((X)->tid
cddc0 2c 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 , pthread_self()
cddd0 29 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e )).#else.# defin
cdde0 65 20 53 45 54 5f 54 48 52 45 41 44 49 44 28 58 e SET_THREADID(X
cddf0 29 0a 23 20 64 65 66 69 6e 65 20 43 48 45 43 4b ).# define CHECK
cde00 5f 54 48 52 45 41 44 49 44 28 58 29 20 30 0a 23 _THREADID(X) 0.#
cde10 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 endif../*.** An
cde20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
cde30 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
cde40 75 72 65 20 73 65 72 76 65 73 20 61 73 20 74 68 ure serves as th
cde50 65 20 6b 65 79 20 75 73 65 64 0a 2a 2a 20 74 6f e key used.** to
cde60 20 6c 6f 63 61 74 65 20 61 20 70 61 72 74 69 63 locate a partic
cde70 75 6c 61 72 20 75 6e 69 78 4f 70 65 6e 43 6e 74 ular unixOpenCnt
cde80 20 73 74 72 75 63 74 75 72 65 20 67 69 76 65 6e structure given
cde90 20 69 74 73 20 69 6e 6f 64 65 2e 20 20 54 68 69 its inode. Thi
cdea0 73 0a 2a 2a 20 69 73 20 74 68 65 20 73 61 6d 65 s.** is the same
cdeb0 20 61 73 20 74 68 65 20 75 6e 69 78 4c 6f 63 6b as the unixLock
cdec0 4b 65 79 20 65 78 63 65 70 74 20 74 68 61 74 20 Key except that
cded0 74 68 65 20 74 68 72 65 61 64 20 49 44 20 69 73 the thread ID is
cdee0 20 6f 6d 69 74 74 65 64 2e 0a 2a 2f 0a 73 74 72 omitted..*/.str
cdef0 75 63 74 20 75 6e 69 78 46 69 6c 65 49 64 20 7b uct unixFileId {
cdf00 0a 20 20 64 65 76 5f 74 20 64 65 76 3b 20 20 20 . dev_t dev;
cdf10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
cdf20 2a 20 44 65 76 69 63 65 20 6e 75 6d 62 65 72 20 * Device number
cdf30 2a 2f 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b */.#if OS_VXWORK
cdf40 53 0a 20 20 73 74 72 75 63 74 20 76 78 77 6f 72 S. struct vxwor
cdf50 6b 73 46 69 6c 65 49 64 20 2a 70 49 64 3b 20 20 ksFileId *pId;
cdf60 2f 2a 20 55 6e 69 71 75 65 20 66 69 6c 65 20 49 /* Unique file I
cdf70 44 20 66 6f 72 20 76 78 77 6f 72 6b 73 2e 20 2a D for vxworks. *
cdf80 2f 0a 23 65 6c 73 65 0a 20 20 69 6e 6f 5f 74 20 /.#else. ino_t
cdf90 69 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 ino;
cdfa0 20 20 20 20 20 20 2f 2a 20 49 6e 6f 64 65 20 6e /* Inode n
cdfb0 75 6d 62 65 72 20 2a 2f 0a 23 65 6e 64 69 66 0a umber */.#endif.
cdfc0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 };../*.** An ins
cdfd0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
cdfe0 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
cdff0 20 73 65 72 76 65 73 20 61 73 20 74 68 65 20 6b serves as the k
ce000 65 79 20 75 73 65 64 0a 2a 2a 20 74 6f 20 6c 6f ey used.** to lo
ce010 63 61 74 65 20 61 20 70 61 72 74 69 63 75 6c 61 cate a particula
ce020 72 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 73 r unixLockInfo s
ce030 74 72 75 63 74 75 72 65 20 67 69 76 65 6e 20 69 tructure given i
ce040 74 73 20 69 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 ts inode..**.**
ce050 49 66 20 74 68 72 65 61 64 73 20 63 61 6e 6e 6f If threads canno
ce060 74 20 6f 76 65 72 72 69 64 65 20 65 61 63 68 20 t override each
ce070 6f 74 68 65 72 73 20 6c 6f 63 6b 73 20 28 4c 69 others locks (Li
ce080 6e 75 78 54 68 72 65 61 64 73 29 2c 20 74 68 65 nuxThreads), the
ce090 6e 20 77 65 0a 2a 2a 20 73 65 74 20 74 68 65 20 n we.** set the
ce0a0 75 6e 69 78 4c 6f 63 6b 4b 65 79 2e 74 69 64 20 unixLockKey.tid
ce0b0 66 69 65 6c 64 20 74 6f 20 74 68 65 20 74 68 72 field to the thr
ce0c0 65 61 64 20 49 44 2e 20 20 49 66 20 74 68 72 65 ead ID. If thre
ce0d0 61 64 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65 ads can override
ce0e0 0a 2a 2a 20 65 61 63 68 20 6f 74 68 65 72 73 20 .** each others
ce0f0 6c 6f 63 6b 73 20 28 50 6f 73 69 78 20 61 6e 64 locks (Posix and
ce100 20 4e 50 54 4c 29 20 74 68 65 6e 20 74 69 64 20 NPTL) then tid
ce110 69 73 20 61 6c 77 61 79 73 20 73 65 74 20 74 6f is always set to
ce120 20 7a 65 72 6f 2e 0a 2a 2a 20 74 69 64 20 69 73 zero..** tid is
ce130 20 6f 6d 69 74 74 65 64 20 69 66 20 77 65 20 63 omitted if we c
ce140 6f 6d 70 69 6c 65 20 77 69 74 68 6f 75 74 20 74 ompile without t
ce150 68 72 65 61 64 69 6e 67 20 73 75 70 70 6f 72 74 hreading support
ce160 20 6f 72 20 6f 6e 20 61 6e 20 4f 53 0a 2a 2a 20 or on an OS.**
ce170 6f 74 68 65 72 20 74 68 61 6e 20 6c 69 6e 75 78 other than linux
ce180 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 75 6e 69 78 ..*/.struct unix
ce190 4c 6f 63 6b 4b 65 79 20 7b 0a 20 20 73 74 72 75 LockKey {. stru
ce1a0 63 74 20 75 6e 69 78 46 69 6c 65 49 64 20 66 69 ct unixFileId fi
ce1b0 64 3b 20 20 2f 2a 20 55 6e 69 71 75 65 20 69 64 d; /* Unique id
ce1c0 65 6e 74 69 66 69 65 72 20 66 6f 72 20 74 68 65 entifier for the
ce1d0 20 66 69 6c 65 20 2a 2f 0a 23 69 66 20 53 51 4c file */.#if SQL
ce1e0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 ITE_THREADSAFE &
ce1f0 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 & defined(__linu
ce200 78 5f 5f 29 0a 20 20 70 74 68 72 65 61 64 5f 74 x__). pthread_t
ce210 20 74 69 64 3b 20 20 2f 2a 20 54 68 72 65 61 64 tid; /* Thread
ce220 20 49 44 20 6f 66 20 6c 6f 63 6b 20 6f 77 6e 65 ID of lock owne
ce230 72 2e 20 5a 65 72 6f 20 69 66 20 6e 6f 74 20 75 r. Zero if not u
ce240 73 69 6e 67 20 4c 69 6e 75 78 54 68 72 65 61 64 sing LinuxThread
ce250 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a s */.#endif.};..
ce260 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 /*.** An instanc
ce270 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
ce280 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 73 20 ng structure is
ce290 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 65 61 allocated for ea
ce2a0 63 68 20 6f 70 65 6e 0a 2a 2a 20 69 6e 6f 64 65 ch open.** inode
ce2b0 2e 20 20 4f 72 2c 20 6f 6e 20 4c 69 6e 75 78 54 . Or, on LinuxT
ce2c0 68 72 65 61 64 73 2c 20 74 68 65 72 65 20 69 73 hreads, there is
ce2d0 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 73 74 one of these st
ce2e0 72 75 63 74 75 72 65 73 20 66 6f 72 0a 2a 2a 20 ructures for.**
ce2f0 65 61 63 68 20 69 6e 6f 64 65 20 6f 70 65 6e 65 each inode opene
ce300 64 20 62 79 20 65 61 63 68 20 74 68 72 65 61 64 d by each thread
ce310 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 ..**.** A single
ce320 20 69 6e 6f 64 65 20 63 61 6e 20 68 61 76 65 20 inode can have
ce330 6d 75 6c 74 69 70 6c 65 20 66 69 6c 65 20 64 65 multiple file de
ce340 73 63 72 69 70 74 6f 72 73 2c 20 73 6f 20 65 61 scriptors, so ea
ce350 63 68 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 73 ch unixFile.** s
ce360 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e tructure contain
ce370 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
ce380 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
ce390 69 73 20 6f 62 6a 65 63 74 20 61 6e 64 20 74 68 is object and th
ce3a0 69 73 0a 2a 2a 20 6f 62 6a 65 63 74 20 6b 65 65 is.** object kee
ce3b0 70 73 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 ps a count of th
ce3c0 65 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69 78 e number of unix
ce3d0 46 69 6c 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f File pointing to
ce3e0 20 69 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 75 it..*/.struct u
ce3f0 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 7b 0a 20 20 nixLockInfo {.
ce400 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 4b struct unixLockK
ce410 65 79 20 6c 6f 63 6b 4b 65 79 3b 20 20 20 20 20 ey lockKey;
ce420 2f 2a 20 54 68 65 20 6c 6f 6f 6b 75 70 20 6b 65 /* The lookup ke
ce430 79 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20 y */. int cnt;
ce440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ce450 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
ce460 20 6f 66 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 of SHARED locks
ce470 20 68 65 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 6c held */. int l
ce480 6f 63 6b 74 79 70 65 3b 20 20 20 20 20 20 20 20 ocktype;
ce490 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e /* On
ce4a0 65 20 6f 66 20 53 48 41 52 45 44 5f 4c 4f 43 4b e of SHARED_LOCK
ce4b0 2c 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 , RESERVED_LOCK
ce4c0 65 74 63 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 etc. */. int nR
ce4d0 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ef;
ce4e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
ce4f0 62 65 72 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 ber of pointers
ce500 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72 to this structur
ce510 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e e */. struct un
ce520 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4e 65 78 ixLockInfo *pNex
ce530 74 3b 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f t; /* List o
ce540 66 20 61 6c 6c 20 75 6e 69 78 4c 6f 63 6b 49 6e f all unixLockIn
ce550 66 6f 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 20 20 fo objects */.
ce560 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 struct unixLockI
ce570 6e 66 6f 20 2a 70 50 72 65 76 3b 20 20 20 20 20 nfo *pPrev;
ce580 2f 2a 20 20 20 20 2e 2e 2e 2e 20 64 6f 75 62 6c /* .... doubl
ce590 79 20 6c 69 6e 6b 65 64 20 2a 2f 0a 7d 3b 0a 0a y linked */.};..
ce5a0 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 /*.** An instanc
ce5b0 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
ce5c0 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 73 20 ng structure is
ce5d0 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 65 61 allocated for ea
ce5e0 63 68 20 6f 70 65 6e 0a 2a 2a 20 69 6e 6f 64 65 ch open.** inode
ce5f0 2e 20 20 54 68 69 73 20 73 74 72 75 63 74 75 72 . This structur
ce600 65 20 6b 65 65 70 73 20 74 72 61 63 6b 20 6f 66 e keeps track of
ce610 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c the number of l
ce620 6f 63 6b 73 20 6f 6e 20 74 68 61 74 0a 2a 2a 20 ocks on that.**
ce630 69 6e 6f 64 65 2e 20 20 49 66 20 61 20 63 6c 6f inode. If a clo
ce640 73 65 20 69 73 20 61 74 74 65 6d 70 74 65 64 20 se is attempted
ce650 61 67 61 69 6e 73 74 20 61 6e 20 69 6e 6f 64 65 against an inode
ce660 20 74 68 61 74 20 69 73 20 68 6f 6c 64 69 6e 67 that is holding
ce670 0a 2a 2a 20 6c 6f 63 6b 73 2c 20 74 68 65 20 63 .** locks, the c
ce680 6c 6f 73 65 20 69 73 20 64 65 66 65 72 72 65 64 lose is deferred
ce690 20 75 6e 74 69 6c 20 61 6c 6c 20 6c 6f 63 6b 73 until all locks
ce6a0 20 63 6c 65 61 72 20 62 79 20 61 64 64 69 6e 67 clear by adding
ce6b0 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 64 65 73 the.** file des
ce6c0 63 72 69 70 74 6f 72 20 74 6f 20 62 65 20 63 6c criptor to be cl
ce6d0 6f 73 65 64 20 74 6f 20 74 68 65 20 70 65 6e 64 osed to the pend
ce6e0 69 6e 67 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 ing list..**.**
ce6f0 54 4f 44 4f 3a 20 20 43 6f 6e 73 69 64 65 72 20 TODO: Consider
ce700 63 68 61 6e 67 69 6e 67 20 74 68 69 73 20 73 6f changing this so
ce710 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20 6f that there is o
ce720 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 66 69 6c nly a single fil
ce730 65 0a 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20 e.** descriptor
ce740 66 6f 72 20 65 61 63 68 20 6f 70 65 6e 20 66 69 for each open fi
ce750 6c 65 2c 20 65 76 65 6e 20 77 68 65 6e 20 69 74 le, even when it
ce760 20 69 73 20 6f 70 65 6e 65 64 20 6d 75 6c 74 69 is opened multi
ce770 70 6c 65 20 74 69 6d 65 73 2e 0a 2a 2a 20 54 68 ple times..** Th
ce780 65 20 63 6c 6f 73 65 28 29 20 73 79 73 74 65 6d e close() system
ce790 20 63 61 6c 6c 20 77 6f 75 6c 64 20 6f 6e 6c 79 call would only
ce7a0 20 6f 63 63 75 72 20 77 68 65 6e 20 74 68 65 20 occur when the
ce7b0 6c 61 73 74 20 64 61 74 61 62 61 73 65 0a 2a 2a last database.**
ce7c0 20 75 73 69 6e 67 20 74 68 65 20 66 69 6c 65 20 using the file
ce7d0 63 6c 6f 73 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 closes..*/.struc
ce7e0 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 7b 0a t unixOpenCnt {.
ce7f0 20 20 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c struct unixFil
ce800 65 49 64 20 66 69 6c 65 49 64 3b 20 20 20 2f 2a eId fileId; /*
ce810 20 54 68 65 20 6c 6f 6f 6b 75 70 20 6b 65 79 20 The lookup key
ce820 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 */. int nRef;
ce830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ce840 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f /* Number of po
ce850 69 6e 74 65 72 73 20 74 6f 20 74 68 69 73 20 73 inters to this s
ce860 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 69 6e tructure */. in
ce870 74 20 6e 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 t nLock;
ce880 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
ce890 62 65 72 20 6f 66 20 6f 75 74 73 74 61 6e 64 69 ber of outstandi
ce8a0 6e 67 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20 55 6e ng locks */. Un
ce8b0 69 78 55 6e 75 73 65 64 46 64 20 2a 70 55 6e 75 ixUnusedFd *pUnu
ce8c0 73 65 64 3b 20 20 20 20 20 20 2f 2a 20 55 6e 75 sed; /* Unu
ce8d0 73 65 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 sed file descrip
ce8e0 74 6f 72 73 20 74 6f 20 63 6c 6f 73 65 20 2a 2f tors to close */
ce8f0 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a .#if OS_VXWORKS.
ce900 20 20 73 65 6d 5f 74 20 2a 70 53 65 6d 3b 20 20 sem_t *pSem;
ce910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ce920 20 20 20 2f 2a 20 4e 61 6d 65 64 20 50 4f 53 49 /* Named POSI
ce930 58 20 73 65 6d 61 70 68 6f 72 65 20 2a 2f 0a 20 X semaphore */.
ce940 20 63 68 61 72 20 61 53 65 6d 4e 61 6d 65 5b 4d char aSemName[M
ce950 41 58 5f 50 41 54 48 4e 41 4d 45 2b 32 5d 3b 20 AX_PATHNAME+2];
ce960 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 61 /* Name of tha
ce970 74 20 73 65 6d 61 70 68 6f 72 65 20 2a 2f 0a 23 t semaphore */.#
ce980 65 6e 64 69 66 0a 20 20 73 74 72 75 63 74 20 75 endif. struct u
ce990 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4e 65 78 nixOpenCnt *pNex
ce9a0 74 2c 20 2a 70 50 72 65 76 3b 20 20 20 2f 2a 20 t, *pPrev; /*
ce9b0 4c 69 73 74 20 6f 66 20 61 6c 6c 20 75 6e 69 78 List of all unix
ce9c0 4f 70 65 6e 43 6e 74 20 6f 62 6a 65 63 74 73 20 OpenCnt objects
ce9d0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4c 69 73 */.};../*.** Lis
ce9e0 74 73 20 6f 66 20 61 6c 6c 20 75 6e 69 78 4c 6f ts of all unixLo
ce9f0 63 6b 49 6e 66 6f 20 61 6e 64 20 75 6e 69 78 4f ckInfo and unixO
cea00 70 65 6e 43 6e 74 20 6f 62 6a 65 63 74 73 2e 20 penCnt objects.
cea10 20 54 68 65 73 65 20 75 73 65 64 20 74 6f 20 62 These used to b
cea20 65 20 68 61 73 68 0a 2a 2a 20 74 61 62 6c 65 73 e hash.** tables
cea30 2e 20 20 42 75 74 20 74 68 65 20 6e 75 6d 62 65 . But the numbe
cea40 72 20 6f 66 20 6f 62 6a 65 63 74 73 20 69 73 20 r of objects is
cea50 72 61 72 65 6c 79 20 6d 6f 72 65 20 74 68 61 6e rarely more than
cea60 20 61 20 64 6f 7a 65 6e 20 61 6e 64 0a 2a 2a 20 a dozen and.**
cea70 6e 65 76 65 72 20 65 78 63 65 65 64 73 20 61 20 never exceeds a
cea80 66 65 77 20 74 68 6f 75 73 61 6e 64 2e 20 20 41 few thousand. A
cea90 6e 64 20 6c 6f 6f 6b 75 70 20 69 73 20 6e 6f 74 nd lookup is not
ceaa0 20 6f 6e 20 61 20 63 72 69 74 69 63 61 6c 0a 2a on a critical.*
ceab0 2a 20 70 61 74 68 20 73 6f 20 61 20 73 69 6d 70 * path so a simp
ceac0 6c 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 77 le linked list w
cead0 69 6c 6c 20 73 75 66 66 69 63 65 2e 0a 2a 2f 0a ill suffice..*/.
ceae0 73 74 61 74 69 63 20 73 74 72 75 63 74 20 75 6e static struct un
ceaf0 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 6c 6f 63 6b ixLockInfo *lock
ceb00 4c 69 73 74 20 3d 20 30 3b 0a 73 74 61 74 69 63 List = 0;.static
ceb10 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e struct unixOpen
ceb20 43 6e 74 20 2a 6f 70 65 6e 4c 69 73 74 20 3d 20 Cnt *openList =
ceb30 30 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 0;../*.** This v
ceb40 61 72 69 61 62 6c 65 20 72 65 6d 65 6d 62 65 72 ariable remember
ceb50 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 s whether or not
ceb60 20 74 68 72 65 61 64 73 20 63 61 6e 20 6f 76 65 threads can ove
ceb70 72 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 72 rride each other
ceb80 73 0a 2a 2a 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a s.** locks..**.*
ceb90 2a 20 20 20 20 30 3a 20 20 4e 6f 2e 20 20 54 68 * 0: No. Th
ceba0 72 65 61 64 73 20 63 61 6e 6e 6f 74 20 6f 76 65 reads cannot ove
cebb0 72 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 72 rride each other
cebc0 73 20 6c 6f 63 6b 73 2e 20 20 28 4c 69 6e 75 78 s locks. (Linux
cebd0 54 68 72 65 61 64 73 29 0a 2a 2a 20 20 20 20 31 Threads).** 1
cebe0 3a 20 20 59 65 73 2e 20 20 54 68 72 65 61 64 73 : Yes. Threads
cebf0 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20 65 61 can override ea
cec00 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 2e ch others locks.
cec10 20 20 28 50 6f 73 69 78 20 26 20 4e 4c 50 54 29 (Posix & NLPT)
cec20 0a 2a 2a 20 20 20 2d 31 3a 20 20 57 65 20 64 6f .** -1: We do
cec30 6e 27 74 20 6b 6e 6f 77 20 79 65 74 2e 0a 2a 2a n't know yet..**
cec40 0a 2a 2a 20 4f 6e 20 73 6f 6d 65 20 73 79 73 74 .** On some syst
cec50 65 6d 73 2c 20 77 65 20 6b 6e 6f 77 20 61 74 20 ems, we know at
cec60 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 69 66 20 compile-time if
cec70 74 68 72 65 61 64 73 20 63 61 6e 20 6f 76 65 72 threads can over
cec80 72 69 64 65 20 65 61 63 68 0a 2a 2a 20 6f 74 68 ride each.** oth
cec90 65 72 73 20 6c 6f 63 6b 73 2e 20 20 4f 6e 20 74 ers locks. On t
ceca0 68 6f 73 65 20 73 79 73 74 65 6d 73 2c 20 74 68 hose systems, th
cecb0 65 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 5f e SQLITE_THREAD_
cecc0 4f 56 45 52 52 49 44 45 5f 4c 4f 43 4b 20 6d 61 OVERRIDE_LOCK ma
cecd0 63 72 6f 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 73 cro.** will be s
cece0 65 74 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 et appropriately
cecf0 2e 20 20 4f 6e 20 6f 74 68 65 72 20 73 79 73 74 . On other syst
ced00 65 6d 73 2c 20 77 65 20 68 61 76 65 20 74 6f 20 ems, we have to
ced10 63 68 65 63 6b 20 61 74 0a 2a 2a 20 72 75 6e 74 check at.** runt
ced20 69 6d 65 2e 20 20 4f 6e 20 74 68 65 73 65 20 6c ime. On these l
ced30 61 74 74 65 72 20 73 79 73 74 65 6d 73 2c 20 53 atter systems, S
ced40 51 4c 54 49 45 5f 54 48 52 45 41 44 5f 4f 56 45 QLTIE_THREAD_OVE
ced50 52 52 49 44 45 5f 4c 4f 43 4b 20 69 73 0a 2a 2a RRIDE_LOCK is.**
ced60 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a undefined..**.*
ced70 2a 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 20 * This variable
ced80 6e 6f 72 6d 61 6c 6c 79 20 68 61 73 20 66 69 6c normally has fil
ced90 65 20 73 63 6f 70 65 20 6f 6e 6c 79 2e 20 20 42 e scope only. B
ceda0 75 74 20 64 75 72 69 6e 67 20 74 65 73 74 69 6e ut during testin
cedb0 67 2c 20 77 65 20 6d 61 6b 65 0a 2a 2a 20 69 74 g, we make.** it
cedc0 20 61 20 67 6c 6f 62 61 6c 20 73 6f 20 74 68 61 a global so tha
cedd0 74 20 74 68 65 20 74 65 73 74 20 63 6f 64 65 20 t the test code
cede0 63 61 6e 20 63 68 61 6e 67 65 20 69 74 73 20 76 can change its v
cedf0 61 6c 75 65 20 69 6e 20 6f 72 64 65 72 20 74 6f alue in order to
cee00 20 76 65 72 69 66 79 0a 2a 2a 20 74 68 61 74 20 verify.** that
cee10 74 68 65 20 72 69 67 68 74 20 73 74 75 66 66 20 the right stuff
cee20 68 61 70 70 65 6e 73 20 69 6e 20 65 69 74 68 65 happens in eithe
cee30 72 20 63 61 73 65 2e 0a 2a 2f 0a 23 69 66 20 53 r case..*/.#if S
cee40 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
cee50 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 && defined(__li
cee60 6e 75 78 5f 5f 29 0a 23 20 20 69 66 6e 64 65 66 nux__).# ifndef
cee70 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 5f 4f SQLITE_THREAD_O
cee80 56 45 52 52 49 44 45 5f 4c 4f 43 4b 0a 23 20 20 VERRIDE_LOCK.#
cee90 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f define SQLITE_
ceea0 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f THREAD_OVERRIDE_
ceeb0 4c 4f 43 4b 20 2d 31 0a 23 20 20 65 6e 64 69 66 LOCK -1.# endif
ceec0 0a 23 20 20 69 66 64 65 66 20 53 51 4c 49 54 45 .# ifdef SQLITE
ceed0 5f 54 45 53 54 0a 69 6e 74 20 74 68 72 65 61 64 _TEST.int thread
ceee0 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 sOverrideEachOth
ceef0 65 72 73 4c 6f 63 6b 73 20 3d 20 53 51 4c 49 54 ersLocks = SQLIT
cef00 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 E_THREAD_OVERRID
cef10 45 5f 4c 4f 43 4b 3b 0a 23 20 20 65 6c 73 65 0a E_LOCK;.# else.
cef20 73 74 61 74 69 63 20 69 6e 74 20 74 68 72 65 61 static int threa
cef30 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 dsOverrideEachOt
cef40 68 65 72 73 4c 6f 63 6b 73 20 3d 20 53 51 4c 49 hersLocks = SQLI
cef50 54 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 TE_THREAD_OVERRI
cef60 44 45 5f 4c 4f 43 4b 3b 0a 23 20 20 65 6e 64 69 DE_LOCK;.# endi
cef70 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 f.#endif../*.**
cef80 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20 68 This structure h
cef90 6f 6c 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e olds information
cefa0 20 70 61 73 73 65 64 20 69 6e 74 6f 20 69 6e 64 passed into ind
cefb0 69 76 69 64 75 61 6c 20 74 65 73 74 0a 2a 2a 20 ividual test.**
cefc0 74 68 72 65 61 64 73 20 62 79 20 74 68 65 20 74 threads by the t
cefd0 65 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 estThreadLocking
cefe0 42 65 68 61 76 69 6f 72 28 29 20 72 6f 75 74 69 Behavior() routi
ceff0 6e 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 74 68 ne..*/.struct th
cf000 72 65 61 64 54 65 73 74 44 61 74 61 20 7b 0a 20 readTestData {.
cf010 20 69 6e 74 20 66 64 3b 20 20 20 20 20 20 20 20 int fd;
cf020 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 /* File
cf030 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a to be locked */.
cf040 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c struct flock l
cf050 6f 63 6b 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 ock; /* The
cf060 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f locking operatio
cf070 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 75 6c n */. int resul
cf080 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a t; /*
cf090 20 52 65 73 75 6c 74 20 6f 66 20 74 68 65 20 6c Result of the l
cf0a0 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e ocking operation
cf0b0 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 20 53 51 4c 49 */.};..#if SQLI
cf0c0 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 TE_THREADSAFE &&
cf0d0 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 defined(__linux
cf0e0 5f 5f 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 __)./*.** This f
cf0f0 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 unction is used
cf100 61 73 20 74 68 65 20 6d 61 69 6e 20 72 6f 75 74 as the main rout
cf110 69 6e 65 20 66 6f 72 20 61 20 74 68 72 65 61 64 ine for a thread
cf120 20 6c 61 75 6e 63 68 65 64 20 62 79 0a 2a 2a 20 launched by.**
cf130 74 65 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e testThreadLockin
cf140 67 42 65 68 61 76 69 6f 72 28 29 2e 20 49 74 20 gBehavior(). It
cf150 74 65 73 74 73 20 77 68 65 74 68 65 72 20 74 68 tests whether th
cf160 65 20 73 68 61 72 65 64 2d 6c 6f 63 6b 20 6f 62 e shared-lock ob
cf170 74 61 69 6e 65 64 0a 2a 2a 20 62 79 20 74 68 65 tained.** by the
cf180 20 6d 61 69 6e 20 74 68 72 65 61 64 20 69 6e 20 main thread in
cf190 74 65 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e testThreadLockin
cf1a0 67 42 65 68 61 76 69 6f 72 28 29 20 63 6f 6e 66 gBehavior() conf
cf1b0 6c 69 63 74 73 20 77 69 74 68 20 61 0a 2a 2a 20 licts with a.**
cf1c0 68 79 70 6f 74 68 65 74 69 63 61 6c 20 77 72 69 hypothetical wri
cf1d0 74 65 2d 6c 6f 63 6b 20 6f 62 74 61 69 6e 65 64 te-lock obtained
cf1e0 20 62 79 20 74 68 69 73 20 74 68 72 65 61 64 20 by this thread
cf1f0 6f 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 on the same file
cf200 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 77 72 69 74 ..**.** The writ
cf210 65 2d 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 61 63 e-lock is not ac
cf220 74 75 61 6c 6c 79 20 61 63 71 75 69 72 65 64 2c tually acquired,
cf230 20 61 73 20 74 68 69 73 20 69 73 20 6e 6f 74 20 as this is not
cf240 70 6f 73 73 69 62 6c 65 20 69 66 20 0a 2a 2a 20 possible if .**
cf250 74 68 65 20 66 69 6c 65 20 69 73 20 6f 70 65 6e the file is open
cf260 20 69 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 6f in read-only mo
cf270 64 65 20 28 73 65 65 20 74 69 63 6b 65 74 20 23 de (see ticket #
cf280 33 34 37 32 29 2e 0a 2a 2f 20 0a 73 74 61 74 69 3472)..*/ .stati
cf290 63 20 76 6f 69 64 20 2a 74 68 72 65 61 64 4c 6f c void *threadLo
cf2a0 63 6b 69 6e 67 54 65 73 74 28 76 6f 69 64 20 2a ckingTest(void *
cf2b0 70 41 72 67 29 7b 0a 20 20 73 74 72 75 63 74 20 pArg){. struct
cf2c0 74 68 72 65 61 64 54 65 73 74 44 61 74 61 20 2a threadTestData *
cf2d0 70 44 61 74 61 20 3d 20 28 73 74 72 75 63 74 20 pData = (struct
cf2e0 74 68 72 65 61 64 54 65 73 74 44 61 74 61 2a 29 threadTestData*)
cf2f0 70 41 72 67 3b 0a 20 20 70 44 61 74 61 2d 3e 72 pArg;. pData->r
cf300 65 73 75 6c 74 20 3d 20 66 63 6e 74 6c 28 70 44 esult = fcntl(pD
cf310 61 74 61 2d 3e 66 64 2c 20 46 5f 47 45 54 4c 4b ata->fd, F_GETLK
cf320 2c 20 26 70 44 61 74 61 2d 3e 6c 6f 63 6b 29 3b , &pData->lock);
cf330 0a 20 20 72 65 74 75 72 6e 20 70 41 72 67 3b 0a . return pArg;.
cf340 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
cf350 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 TE_THREADSAFE &&
cf360 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 defined(__linux
cf370 5f 5f 29 20 2a 2f 0a 0a 0a 23 69 66 20 53 51 4c __) */...#if SQL
cf380 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 ITE_THREADSAFE &
cf390 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 & defined(__linu
cf3a0 78 5f 5f 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 x__)./*.** This
cf3b0 70 72 6f 63 65 64 75 72 65 20 61 74 74 65 6d 70 procedure attemp
cf3c0 74 73 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 ts to determine
cf3d0 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 whether or not t
cf3e0 68 72 65 61 64 73 0a 2a 2a 20 63 61 6e 20 6f 76 hreads.** can ov
cf3f0 65 72 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 erride each othe
cf400 72 73 20 6c 6f 63 6b 73 20 74 68 65 6e 20 73 65 rs locks then se
cf410 74 73 20 74 68 65 20 0a 2a 2a 20 74 68 72 65 61 ts the .** threa
cf420 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 dsOverrideEachOt
cf430 68 65 72 73 4c 6f 63 6b 73 20 76 61 72 69 61 62 hersLocks variab
cf440 6c 65 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 le appropriately
cf450 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
cf460 20 74 65 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 testThreadLocki
cf470 6e 67 42 65 68 61 76 69 6f 72 28 69 6e 74 20 66 ngBehavior(int f
cf480 64 5f 6f 72 69 67 29 7b 0a 20 20 69 6e 74 20 66 d_orig){. int f
cf490 64 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 d;. int rc;. s
cf4a0 74 72 75 63 74 20 74 68 72 65 61 64 54 65 73 74 truct threadTest
cf4b0 44 61 74 61 20 64 3b 0a 20 20 73 74 72 75 63 74 Data d;. struct
cf4c0 20 66 6c 6f 63 6b 20 6c 3b 0a 20 20 70 74 68 72 flock l;. pthr
cf4d0 65 61 64 5f 74 20 74 3b 0a 0a 20 20 66 64 20 3d ead_t t;.. fd =
cf4e0 20 64 75 70 28 66 64 5f 6f 72 69 67 29 3b 0a 20 dup(fd_orig);.
cf4f0 20 69 66 28 20 66 64 3c 30 20 29 20 72 65 74 75 if( fd<0 ) retu
cf500 72 6e 3b 0a 20 20 6d 65 6d 73 65 74 28 26 6c 2c rn;. memset(&l,
cf510 20 30 2c 20 73 69 7a 65 6f 66 28 6c 29 29 3b 0a 0, sizeof(l));.
cf520 20 20 6c 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52 l.l_type = F_R
cf530 44 4c 43 4b 3b 0a 20 20 6c 2e 6c 5f 6c 65 6e 20 DLCK;. l.l_len
cf540 3d 20 31 3b 0a 20 20 6c 2e 6c 5f 73 74 61 72 74 = 1;. l.l_start
cf550 20 3d 20 30 3b 0a 20 20 6c 2e 6c 5f 77 68 65 6e = 0;. l.l_when
cf560 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 ce = SEEK_SET;.
cf570 20 72 63 20 3d 20 66 63 6e 74 6c 28 66 64 5f 6f rc = fcntl(fd_o
cf580 72 69 67 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c rig, F_SETLK, &l
cf590 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 30 20 29 );. if( rc!=0 )
cf5a0 20 72 65 74 75 72 6e 3b 0a 20 20 6d 65 6d 73 65 return;. memse
cf5b0 74 28 26 64 2c 20 30 2c 20 73 69 7a 65 6f 66 28 t(&d, 0, sizeof(
cf5c0 64 29 29 3b 0a 20 20 64 2e 66 64 20 3d 20 66 64 d));. d.fd = fd
cf5d0 3b 0a 20 20 64 2e 6c 6f 63 6b 20 3d 20 6c 3b 0a ;. d.lock = l;.
cf5e0 20 20 64 2e 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 d.lock.l_type
cf5f0 3d 20 46 5f 57 52 4c 43 4b 3b 0a 20 20 69 66 28 = F_WRLCK;. if(
cf600 20 70 74 68 72 65 61 64 5f 63 72 65 61 74 65 28 pthread_create(
cf610 26 74 2c 20 30 2c 20 74 68 72 65 61 64 4c 6f 63 &t, 0, threadLoc
cf620 6b 69 6e 67 54 65 73 74 2c 20 26 64 29 3d 3d 30 kingTest, &d)==0
cf630 20 29 7b 0a 20 20 20 20 70 74 68 72 65 61 64 5f ){. pthread_
cf640 6a 6f 69 6e 28 74 2c 20 30 29 3b 0a 20 20 7d 0a join(t, 0);. }.
cf650 20 20 63 6c 6f 73 65 28 66 64 29 3b 0a 20 20 69 close(fd);. i
cf660 66 28 20 64 2e 72 65 73 75 6c 74 21 3d 30 20 29 f( d.result!=0 )
cf670 20 72 65 74 75 72 6e 3b 0a 20 20 74 68 72 65 61 return;. threa
cf680 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 dsOverrideEachOt
cf690 68 65 72 73 4c 6f 63 6b 73 20 3d 20 28 64 2e 6c hersLocks = (d.l
cf6a0 6f 63 6b 2e 6c 5f 74 79 70 65 3d 3d 46 5f 55 4e ock.l_type==F_UN
cf6b0 4c 43 4b 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f LCK);.}.#endif /
cf6c0 2a 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 * SQLITE_THREADS
cf6d0 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f AFE && defined(_
cf6e0 5f 6c 69 6e 75 78 5f 5f 29 20 2a 2f 0a 0a 2f 2a _linux__) */../*
cf6f0 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 20 75 6e .** Release a un
cf700 69 78 4c 6f 63 6b 49 6e 66 6f 20 73 74 72 75 63 ixLockInfo struc
cf710 74 75 72 65 20 70 72 65 76 69 6f 75 73 6c 79 20 ture previously
cf720 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 66 69 6e allocated by fin
cf730 64 4c 6f 63 6b 49 6e 66 6f 28 29 2e 0a 2a 2a 0a dLockInfo()..**.
cf740 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 65 6e 74 ** The mutex ent
cf750 65 72 65 64 20 75 73 69 6e 67 20 74 68 65 20 75 ered using the u
cf760 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 20 nixEnterMutex()
cf770 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 function must be
cf780 20 68 65 6c 64 0a 2a 2a 20 77 68 65 6e 20 74 68 held.** when th
cf790 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 is function is c
cf7a0 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 alled..*/.static
cf7b0 20 76 6f 69 64 20 72 65 6c 65 61 73 65 4c 6f 63 void releaseLoc
cf7c0 6b 49 6e 66 6f 28 73 74 72 75 63 74 20 75 6e 69 kInfo(struct uni
cf7d0 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b xLockInfo *pLock
cf7e0 29 7b 0a 20 20 61 73 73 65 72 74 28 20 75 6e 69 ){. assert( uni
cf7f0 78 4d 75 74 65 78 48 65 6c 64 28 29 20 29 3b 0a xMutexHeld() );.
cf800 20 20 69 66 28 20 70 4c 6f 63 6b 20 29 7b 0a 20 if( pLock ){.
cf810 20 20 20 70 4c 6f 63 6b 2d 3e 6e 52 65 66 2d 2d pLock->nRef--
cf820 3b 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d ;. if( pLock-
cf830 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 >nRef==0 ){.
cf840 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 70 50 72 if( pLock->pPr
cf850 65 76 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 ev ){. as
cf860 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 70 50 72 sert( pLock->pPr
cf870 65 76 2d 3e 70 4e 65 78 74 3d 3d 70 4c 6f 63 6b ev->pNext==pLock
cf880 20 29 3b 0a 20 20 20 20 20 20 20 20 70 4c 6f 63 );. pLoc
cf890 6b 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 k->pPrev->pNext
cf8a0 3d 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a = pLock->pNext;.
cf8b0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
cf8c0 20 20 20 20 20 61 73 73 65 72 74 28 20 6c 6f 63 assert( loc
cf8d0 6b 4c 69 73 74 3d 3d 70 4c 6f 63 6b 20 29 3b 0a kList==pLock );.
cf8e0 20 20 20 20 20 20 20 20 6c 6f 63 6b 4c 69 73 74 lockList
cf8f0 20 3d 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b = pLock->pNext;
cf900 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
cf910 66 28 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 20 f( pLock->pNext
cf920 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 ){. asser
cf930 74 28 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 2d t( pLock->pNext-
cf940 3e 70 50 72 65 76 3d 3d 70 4c 6f 63 6b 20 29 3b >pPrev==pLock );
cf950 0a 20 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e . pLock->
cf960 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 pNext->pPrev = p
cf970 4c 6f 63 6b 2d 3e 70 50 72 65 76 3b 0a 20 20 20 Lock->pPrev;.
cf980 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 }. sqlit
cf990 65 33 5f 66 72 65 65 28 70 4c 6f 63 6b 29 3b 0a e3_free(pLock);.
cf9a0 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
cf9b0 2a 2a 20 52 65 6c 65 61 73 65 20 61 20 75 6e 69 ** Release a uni
cf9c0 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75 xOpenCnt structu
cf9d0 72 65 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c re previously al
cf9e0 6c 6f 63 61 74 65 64 20 62 79 20 66 69 6e 64 4c located by findL
cf9f0 6f 63 6b 49 6e 66 6f 28 29 2e 0a 2a 2a 0a 2a 2a ockInfo()..**.**
cfa00 20 54 68 65 20 6d 75 74 65 78 20 65 6e 74 65 72 The mutex enter
cfa10 65 64 20 75 73 69 6e 67 20 74 68 65 20 75 6e 69 ed using the uni
cfa20 78 45 6e 74 65 72 4d 75 74 65 78 28 29 20 66 75 xEnterMutex() fu
cfa30 6e 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 68 nction must be h
cfa40 65 6c 64 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 eld.** when this
cfa50 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c function is cal
cfa60 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 led..*/.static v
cfa70 6f 69 64 20 72 65 6c 65 61 73 65 4f 70 65 6e 43 oid releaseOpenC
cfa80 6e 74 28 73 74 72 75 63 74 20 75 6e 69 78 4f 70 nt(struct unixOp
cfa90 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 29 7b 0a 20 enCnt *pOpen){.
cfaa0 20 61 73 73 65 72 74 28 20 75 6e 69 78 4d 75 74 assert( unixMut
cfab0 65 78 48 65 6c 64 28 29 20 29 3b 0a 20 20 69 66 exHeld() );. if
cfac0 28 20 70 4f 70 65 6e 20 29 7b 0a 20 20 20 20 70 ( pOpen ){. p
cfad0 4f 70 65 6e 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 Open->nRef--;.
cfae0 20 20 69 66 28 20 70 4f 70 65 6e 2d 3e 6e 52 65 if( pOpen->nRe
cfaf0 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 f==0 ){. if
cfb00 28 20 70 4f 70 65 6e 2d 3e 70 50 72 65 76 20 29 ( pOpen->pPrev )
cfb10 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 {. assert
cfb20 28 20 70 4f 70 65 6e 2d 3e 70 50 72 65 76 2d 3e ( pOpen->pPrev->
cfb30 70 4e 65 78 74 3d 3d 70 4f 70 65 6e 20 29 3b 0a pNext==pOpen );.
cfb40 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 70 pOpen->p
cfb50 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 4f Prev->pNext = pO
cfb60 70 65 6e 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 pen->pNext;.
cfb70 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
cfb80 20 61 73 73 65 72 74 28 20 6f 70 65 6e 4c 69 73 assert( openLis
cfb90 74 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20 t==pOpen );.
cfba0 20 20 20 20 6f 70 65 6e 4c 69 73 74 20 3d 20 70 openList = p
cfbb0 4f 70 65 6e 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 Open->pNext;.
cfbc0 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 }. if( p
cfbd0 4f 70 65 6e 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 Open->pNext ){.
cfbe0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
cfbf0 4f 70 65 6e 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 Open->pNext->pPr
cfc00 65 76 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 20 ev==pOpen );.
cfc10 20 20 20 20 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 pOpen->pNex
cfc20 74 2d 3e 70 50 72 65 76 20 3d 20 70 4f 70 65 6e t->pPrev = pOpen
cfc30 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20 20 20 7d ->pPrev;. }
cfc40 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 .#if SQLITE_THRE
cfc50 41 44 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 ADSAFE && define
cfc60 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 20 20 20 d(__linux__).
cfc70 20 20 20 61 73 73 65 72 74 28 20 21 70 4f 70 65 assert( !pOpe
cfc80 6e 2d 3e 70 55 6e 75 73 65 64 20 7c 7c 20 74 68 n->pUnused || th
cfc90 72 65 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 readsOverrideEac
cfca0 68 4f 74 68 65 72 73 4c 6f 63 6b 73 3d 3d 30 20 hOthersLocks==0
cfcb0 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 );.#endif..
cfcc0 20 2f 2a 20 49 66 20 70 4f 70 65 6e 2d 3e 70 55 /* If pOpen->pU
cfcd0 6e 75 73 65 64 20 69 73 20 6e 6f 74 20 6e 75 6c nused is not nul
cfce0 6c 2c 20 74 68 65 6e 20 6d 65 6d 6f 72 79 20 61 l, then memory a
cfcf0 6e 64 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74 nd file-descript
cfd00 6f 72 73 0a 20 20 20 20 20 20 2a 2a 20 61 72 65 ors. ** are
cfd10 20 6c 65 61 6b 65 64 2e 0a 20 20 20 20 20 20 2a leaked.. *
cfd20 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 *. ** This
cfd30 77 69 6c 6c 20 6f 6e 6c 79 20 68 61 70 70 65 6e will only happen
cfd40 20 69 66 2c 20 75 6e 64 65 72 20 4c 69 6e 75 78 if, under Linux
cfd50 74 68 72 65 61 64 73 2c 20 74 68 65 20 75 73 65 threads, the use
cfd60 72 20 68 61 73 20 6f 70 65 6e 65 64 0a 20 20 20 r has opened.
cfd70 20 20 20 2a 2a 20 61 20 74 72 61 6e 73 61 63 74 ** a transact
cfd80 69 6f 6e 20 69 6e 20 6f 6e 65 20 74 68 72 65 61 ion in one threa
cfd90 64 2c 20 74 68 65 6e 20 61 74 74 65 6d 70 74 73 d, then attempts
cfda0 20 74 6f 20 63 6c 6f 73 65 20 74 68 65 20 64 61 to close the da
cfdb0 74 61 62 61 73 65 0a 20 20 20 20 20 20 2a 2a 20 tabase. **
cfdc0 68 61 6e 64 6c 65 20 66 72 6f 6d 20 61 6e 6f 74 handle from anot
cfdd0 68 65 72 20 74 68 72 65 61 64 20 28 77 69 74 68 her thread (with
cfde0 6f 75 74 20 66 69 72 73 74 20 75 6e 6c 6f 63 6b out first unlock
cfdf0 69 6e 67 20 74 68 65 20 64 62 20 66 69 6c 65 29 ing the db file)
cfe00 2e 0a 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 .. ** This
cfe10 69 73 20 61 20 6d 69 73 75 73 65 2e 20 20 2a 2f is a misuse. */
cfe20 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 . sqlite3_f
cfe30 72 65 65 28 70 4f 70 65 6e 29 3b 0a 20 20 20 20 ree(pOpen);.
cfe40 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 }. }.}../*.** G
cfe50 69 76 65 6e 20 61 20 66 69 6c 65 20 64 65 73 63 iven a file desc
cfe60 72 69 70 74 6f 72 2c 20 6c 6f 63 61 74 65 20 75 riptor, locate u
cfe70 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 61 6e 64 20 nixLockInfo and
cfe80 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 unixOpenCnt stru
cfe90 63 74 75 72 65 73 20 74 68 61 74 0a 2a 2a 20 64 ctures that.** d
cfea0 65 73 63 72 69 62 65 73 20 74 68 61 74 20 66 69 escribes that fi
cfeb0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20 20 le descriptor.
cfec0 43 72 65 61 74 65 20 6e 65 77 20 6f 6e 65 73 20 Create new ones
cfed0 69 66 20 6e 65 63 65 73 73 61 72 79 2e 20 20 54 if necessary. T
cfee0 68 65 0a 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c he.** return val
cfef0 75 65 73 20 6d 69 67 68 74 20 62 65 20 75 6e 69 ues might be uni
cff00 6e 69 74 69 61 6c 69 7a 65 64 20 69 66 20 61 6e nitialized if an
cff10 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a error occurs..*
cff20 2a 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 65 *.** The mutex e
cff30 6e 74 65 72 65 64 20 75 73 69 6e 67 20 74 68 65 ntered using the
cff40 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 unixEnterMutex(
cff50 29 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 ) function must
cff60 62 65 20 68 65 6c 64 0a 2a 2a 20 77 68 65 6e 20 be held.** when
cff70 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
cff80 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 called..**.** R
cff90 65 74 75 72 6e 20 61 6e 20 61 70 70 72 6f 70 72 eturn an appropr
cffa0 69 61 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e iate error code.
cffb0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 .*/.static int f
cffc0 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 0a 20 20 75 indLockInfo(. u
cffd0 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 nixFile *pFile,
cffe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
cfff0 20 55 6e 69 78 20 66 69 6c 65 20 77 69 74 68 20 Unix file with
d0000 66 69 6c 65 20 64 65 73 63 20 75 73 65 64 20 69 file desc used i
d0010 6e 20 74 68 65 20 6b 65 79 20 2a 2f 0a 20 20 73 n the key */. s
d0020 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e truct unixLockIn
d0030 66 6f 20 2a 2a 70 70 4c 6f 63 6b 2c 20 20 2f 2a fo **ppLock, /*
d0040 20 52 65 74 75 72 6e 20 74 68 65 20 75 6e 69 78 Return the unix
d0050 4c 6f 63 6b 49 6e 66 6f 20 73 74 72 75 63 74 75 LockInfo structu
d0060 72 65 20 68 65 72 65 20 2a 2f 0a 20 20 73 74 72 re here */. str
d0070 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 uct unixOpenCnt
d0080 2a 2a 70 70 4f 70 65 6e 20 20 20 20 2f 2a 20 52 **ppOpen /* R
d0090 65 74 75 72 6e 20 74 68 65 20 75 6e 69 78 4f 70 eturn the unixOp
d00a0 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 20 enCnt structure
d00b0 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 here */.){. int
d00c0 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 rc;
d00d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
d00e0 79 73 74 65 6d 20 63 61 6c 6c 20 72 65 74 75 72 ystem call retur
d00f0 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 n code */. int
d0100 66 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 fd;
d0110 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
d0120 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f e file descripto
d0130 72 20 66 6f 72 20 70 46 69 6c 65 20 2a 2f 0a 20 r for pFile */.
d0140 20 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b struct unixLock
d0150 4b 65 79 20 6c 6f 63 6b 4b 65 79 3b 20 20 20 20 Key lockKey;
d0160 2f 2a 20 4c 6f 6f 6b 75 70 20 6b 65 79 20 66 6f /* Lookup key fo
d0170 72 20 74 68 65 20 75 6e 69 78 4c 6f 63 6b 49 6e r the unixLockIn
d0180 66 6f 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a fo structure */.
d0190 20 20 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c struct unixFil
d01a0 65 49 64 20 66 69 6c 65 49 64 3b 20 20 20 20 20 eId fileId;
d01b0 20 2f 2a 20 4c 6f 6f 6b 75 70 20 6b 65 79 20 66 /* Lookup key f
d01c0 6f 72 20 74 68 65 20 75 6e 69 78 4f 70 65 6e 43 or the unixOpenC
d01d0 6e 74 20 73 74 72 75 63 74 20 2a 2f 0a 20 20 73 nt struct */. s
d01e0 74 72 75 63 74 20 73 74 61 74 20 73 74 61 74 62 truct stat statb
d01f0 75 66 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a uf; /*
d0200 20 4c 6f 77 2d 6c 65 76 65 6c 20 66 69 6c 65 20 Low-level file
d0210 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 information */.
d0220 20 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b struct unixLock
d0230 49 6e 66 6f 20 2a 70 4c 6f 63 6b 20 3d 20 30 3b Info *pLock = 0;
d0240 2f 2a 20 43 61 6e 64 69 64 61 74 65 20 75 6e 69 /* Candidate uni
d0250 78 4c 6f 63 6b 49 6e 66 6f 20 6f 62 6a 65 63 74 xLockInfo object
d0260 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 */. struct uni
d0270 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 3b xOpenCnt *pOpen;
d0280 20 20 20 20 20 2f 2a 20 43 61 6e 64 69 64 61 74 /* Candidat
d0290 65 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 6f 62 e unixOpenCnt ob
d02a0 6a 65 63 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 ject */.. asser
d02b0 74 28 20 75 6e 69 78 4d 75 74 65 78 48 65 6c 64 t( unixMutexHeld
d02c0 28 29 20 29 3b 0a 0a 20 20 2f 2a 20 47 65 74 20 () );.. /* Get
d02d0 6c 6f 77 2d 6c 65 76 65 6c 20 69 6e 66 6f 72 6d low-level inform
d02e0 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 ation about the
d02f0 66 69 6c 65 20 74 68 61 74 20 77 65 20 63 61 6e file that we can
d0300 20 75 73 65 64 20 74 6f 0a 20 20 2a 2a 20 63 72 used to. ** cr
d0310 65 61 74 65 20 61 20 75 6e 69 71 75 65 20 6e 61 eate a unique na
d0320 6d 65 20 66 6f 72 20 74 68 65 20 66 69 6c 65 2e me for the file.
d0330 0a 20 20 2a 2f 0a 20 20 66 64 20 3d 20 70 46 69 . */. fd = pFi
d0340 6c 65 2d 3e 68 3b 0a 20 20 72 63 20 3d 20 66 73 le->h;. rc = fs
d0350 74 61 74 28 66 64 2c 20 26 73 74 61 74 62 75 66 tat(fd, &statbuf
d0360 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 30 20 29 );. if( rc!=0 )
d0370 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 {. pFile->las
d0380 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a tErrno = errno;.
d0390 23 69 66 64 65 66 20 45 4f 56 45 52 46 4c 4f 57 #ifdef EOVERFLOW
d03a0 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e . if( pFile->
d03b0 6c 61 73 74 45 72 72 6e 6f 3d 3d 45 4f 56 45 52 lastErrno==EOVER
d03c0 46 4c 4f 57 20 29 20 72 65 74 75 72 6e 20 53 51 FLOW ) return SQ
d03d0 4c 49 54 45 5f 4e 4f 4c 46 53 3b 0a 23 65 6e 64 LITE_NOLFS;.#end
d03e0 69 66 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 if. return SQ
d03f0 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a LITE_IOERR;. }.
d0400 0a 23 69 66 64 65 66 20 5f 5f 41 50 50 4c 45 5f .#ifdef __APPLE_
d0410 5f 0a 20 20 2f 2a 20 4f 6e 20 4f 53 20 58 20 6f _. /* On OS X o
d0420 6e 20 61 6e 20 6d 73 64 6f 73 20 66 69 6c 65 73 n an msdos files
d0430 79 73 74 65 6d 2c 20 74 68 65 20 69 6e 6f 64 65 ystem, the inode
d0440 20 6e 75 6d 62 65 72 20 69 73 20 72 65 70 6f 72 number is repor
d0450 74 65 64 0a 20 20 2a 2a 20 69 6e 63 6f 72 72 65 ted. ** incorre
d0460 63 74 6c 79 20 66 6f 72 20 7a 65 72 6f 2d 73 69 ctly for zero-si
d0470 7a 65 20 66 69 6c 65 73 2e 20 20 53 65 65 20 74 ze files. See t
d0480 69 63 6b 65 74 20 23 33 32 36 30 2e 20 20 54 6f icket #3260. To
d0490 20 77 6f 72 6b 0a 20 20 2a 2a 20 61 72 6f 75 6e work. ** aroun
d04a0 64 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 28 d this problem (
d04b0 77 65 20 63 6f 6e 73 69 64 65 72 20 69 74 20 61 we consider it a
d04c0 20 62 75 67 20 69 6e 20 4f 53 20 58 2c 20 6e 6f bug in OS X, no
d04d0 74 20 53 51 4c 69 74 65 29 0a 20 20 2a 2a 20 77 t SQLite). ** w
d04e0 65 20 61 6c 77 61 79 73 20 69 6e 63 72 65 61 73 e always increas
d04f0 65 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 e the file size
d0500 74 6f 20 31 20 62 79 20 77 72 69 74 69 6e 67 20 to 1 by writing
d0510 61 20 73 69 6e 67 6c 65 20 62 79 74 65 0a 20 20 a single byte.
d0520 2a 2a 20 70 72 69 6f 72 20 74 6f 20 61 63 63 65 ** prior to acce
d0530 73 73 69 6e 67 20 74 68 65 20 69 6e 6f 64 65 20 ssing the inode
d0540 6e 75 6d 62 65 72 2e 20 20 54 68 65 20 6f 6e 65 number. The one
d0550 20 62 79 74 65 20 77 72 69 74 74 65 6e 20 69 73 byte written is
d0560 0a 20 20 2a 2a 20 61 6e 20 41 53 43 49 49 20 27 . ** an ASCII '
d0570 53 27 20 63 68 61 72 61 63 74 65 72 20 77 68 69 S' character whi
d0580 63 68 20 61 6c 73 6f 20 68 61 70 70 65 6e 73 20 ch also happens
d0590 74 6f 20 62 65 20 74 68 65 20 66 69 72 73 74 20 to be the first
d05a0 62 79 74 65 0a 20 20 2a 2a 20 69 6e 20 74 68 65 byte. ** in the
d05b0 20 68 65 61 64 65 72 20 6f 66 20 65 76 65 72 79 header of every
d05c0 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 SQLite database
d05d0 2e 20 20 49 6e 20 74 68 69 73 20 77 61 79 2c 20 . In this way,
d05e0 69 66 20 74 68 65 72 65 0a 20 20 2a 2a 20 69 73 if there. ** is
d05f0 20 61 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f a race conditio
d0600 6e 20 73 75 63 68 20 74 68 61 74 20 61 6e 6f 74 n such that anot
d0610 68 65 72 20 74 68 72 65 61 64 20 68 61 73 20 61 her thread has a
d0620 6c 72 65 61 64 79 20 70 6f 70 75 6c 61 74 65 64 lready populated
d0630 0a 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74 20 . ** the first
d0640 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 page of the data
d0650 62 61 73 65 2c 20 6e 6f 20 64 61 6d 61 67 65 20 base, no damage
d0660 69 73 20 64 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 is done.. */.
d0670 69 66 28 20 73 74 61 74 62 75 66 2e 73 74 5f 73 if( statbuf.st_s
d0680 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 ize==0 ){. rc
d0690 20 3d 20 77 72 69 74 65 28 66 64 2c 20 22 53 22 = write(fd, "S"
d06a0 2c 20 31 29 3b 0a 20 20 20 20 69 66 28 20 72 63 , 1);. if( rc
d06b0 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 72 65 74 !=1 ){. ret
d06c0 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
d06d0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d ;. }. rc =
d06e0 20 66 73 74 61 74 28 66 64 2c 20 26 73 74 61 74 fstat(fd, &stat
d06f0 62 75 66 29 3b 0a 20 20 20 20 69 66 28 20 72 63 buf);. if( rc
d0700 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 46 69 !=0 ){. pFi
d0710 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
d0720 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 65 74 errno;. ret
d0730 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
d0740 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 ;. }. }.#end
d0750 69 66 0a 0a 20 20 6d 65 6d 73 65 74 28 26 6c 6f if.. memset(&lo
d0760 63 6b 4b 65 79 2c 20 30 2c 20 73 69 7a 65 6f 66 ckKey, 0, sizeof
d0770 28 6c 6f 63 6b 4b 65 79 29 29 3b 0a 20 20 6c 6f (lockKey));. lo
d0780 63 6b 4b 65 79 2e 66 69 64 2e 64 65 76 20 3d 20 ckKey.fid.dev =
d0790 73 74 61 74 62 75 66 2e 73 74 5f 64 65 76 3b 0a statbuf.st_dev;.
d07a0 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 #if OS_VXWORKS.
d07b0 20 6c 6f 63 6b 4b 65 79 2e 66 69 64 2e 70 49 64 lockKey.fid.pId
d07c0 20 3d 20 70 46 69 6c 65 2d 3e 70 49 64 3b 0a 23 = pFile->pId;.#
d07d0 65 6c 73 65 0a 20 20 6c 6f 63 6b 4b 65 79 2e 66 else. lockKey.f
d07e0 69 64 2e 69 6e 6f 20 3d 20 73 74 61 74 62 75 66 id.ino = statbuf
d07f0 2e 73 74 5f 69 6e 6f 3b 0a 23 65 6e 64 69 66 0a .st_ino;.#endif.
d0800 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 #if SQLITE_THREA
d0810 44 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 DSAFE && defined
d0820 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 20 20 69 66 (__linux__). if
d0830 28 20 74 68 72 65 61 64 73 4f 76 65 72 72 69 64 ( threadsOverrid
d0840 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 eEachOthersLocks
d0850 3c 30 20 29 7b 0a 20 20 20 20 74 65 73 74 54 68 <0 ){. testTh
d0860 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76 readLockingBehav
d0870 69 6f 72 28 66 64 29 3b 0a 20 20 7d 0a 20 20 6c ior(fd);. }. l
d0880 6f 63 6b 4b 65 79 2e 74 69 64 20 3d 20 74 68 72 ockKey.tid = thr
d0890 65 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 eadsOverrideEach
d08a0 4f 74 68 65 72 73 4c 6f 63 6b 73 20 3f 20 30 20 OthersLocks ? 0
d08b0 3a 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 : pthread_self()
d08c0 3b 0a 23 65 6e 64 69 66 0a 20 20 66 69 6c 65 49 ;.#endif. fileI
d08d0 64 20 3d 20 6c 6f 63 6b 4b 65 79 2e 66 69 64 3b d = lockKey.fid;
d08e0 0a 20 20 69 66 28 20 70 70 4c 6f 63 6b 21 3d 30 . if( ppLock!=0
d08f0 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b 20 3d 20 ){. pLock =
d0900 6c 6f 63 6b 4c 69 73 74 3b 0a 20 20 20 20 77 68 lockList;. wh
d0910 69 6c 65 28 20 70 4c 6f 63 6b 20 26 26 20 6d 65 ile( pLock && me
d0920 6d 63 6d 70 28 26 6c 6f 63 6b 4b 65 79 2c 20 26 mcmp(&lockKey, &
d0930 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 4b 65 79 2c 20 pLock->lockKey,
d0940 73 69 7a 65 6f 66 28 6c 6f 63 6b 4b 65 79 29 29 sizeof(lockKey))
d0950 20 29 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 20 ){. pLock
d0960 3d 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a = pLock->pNext;.
d0970 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4c }. if( pL
d0980 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ock==0 ){.
d0990 70 4c 6f 63 6b 20 3d 20 73 71 6c 69 74 65 33 5f pLock = sqlite3_
d09a0 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a malloc( sizeof(*
d09b0 70 4c 6f 63 6b 29 20 29 3b 0a 20 20 20 20 20 20 pLock) );.
d09c0 69 66 28 20 70 4c 6f 63 6b 3d 3d 30 20 29 7b 0a if( pLock==0 ){.
d09d0 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
d09e0 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 ITE_NOMEM;.
d09f0 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 66 69 6e goto exit_fin
d0a00 64 6c 6f 63 6b 69 6e 66 6f 3b 0a 20 20 20 20 20 dlockinfo;.
d0a10 20 7d 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 }. memcpy(
d0a20 26 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 4b 65 79 2c &pLock->lockKey,
d0a30 26 6c 6f 63 6b 4b 65 79 2c 73 69 7a 65 6f 66 28 &lockKey,sizeof(
d0a40 6c 6f 63 6b 4b 65 79 29 29 3b 0a 20 20 20 20 20 lockKey));.
d0a50 20 70 4c 6f 63 6b 2d 3e 6e 52 65 66 20 3d 20 31 pLock->nRef = 1
d0a60 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 63 ;. pLock->c
d0a70 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4c nt = 0;. pL
d0a80 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 ock->locktype =
d0a90 30 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 0;. pLock->
d0aa0 70 4e 65 78 74 20 3d 20 6c 6f 63 6b 4c 69 73 74 pNext = lockList
d0ab0 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 ;. pLock->p
d0ac0 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 20 20 Prev = 0;.
d0ad0 69 66 28 20 6c 6f 63 6b 4c 69 73 74 20 29 20 6c if( lockList ) l
d0ae0 6f 63 6b 4c 69 73 74 2d 3e 70 50 72 65 76 20 3d ockList->pPrev =
d0af0 20 70 4c 6f 63 6b 3b 0a 20 20 20 20 20 20 6c 6f pLock;. lo
d0b00 63 6b 4c 69 73 74 20 3d 20 70 4c 6f 63 6b 3b 0a ckList = pLock;.
d0b10 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
d0b20 20 70 4c 6f 63 6b 2d 3e 6e 52 65 66 2b 2b 3b 0a pLock->nRef++;.
d0b30 20 20 20 20 7d 0a 20 20 20 20 2a 70 70 4c 6f 63 }. *ppLoc
d0b40 6b 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 7d 0a 20 k = pLock;. }.
d0b50 20 69 66 28 20 70 70 4f 70 65 6e 21 3d 30 20 29 if( ppOpen!=0 )
d0b60 7b 0a 20 20 20 20 70 4f 70 65 6e 20 3d 20 6f 70 {. pOpen = op
d0b70 65 6e 4c 69 73 74 3b 0a 20 20 20 20 77 68 69 6c enList;. whil
d0b80 65 28 20 70 4f 70 65 6e 20 26 26 20 6d 65 6d 63 e( pOpen && memc
d0b90 6d 70 28 26 66 69 6c 65 49 64 2c 20 26 70 4f 70 mp(&fileId, &pOp
d0ba0 65 6e 2d 3e 66 69 6c 65 49 64 2c 20 73 69 7a 65 en->fileId, size
d0bb0 6f 66 28 66 69 6c 65 49 64 29 29 20 29 7b 0a 20 of(fileId)) ){.
d0bc0 20 20 20 20 20 70 4f 70 65 6e 20 3d 20 70 4f 70 pOpen = pOp
d0bd0 65 6e 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d en->pNext;. }
d0be0 0a 20 20 20 20 69 66 28 20 70 4f 70 65 6e 3d 3d . if( pOpen==
d0bf0 30 20 29 7b 0a 20 20 20 20 20 20 70 4f 70 65 6e 0 ){. pOpen
d0c00 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f = sqlite3_mallo
d0c10 63 28 20 73 69 7a 65 6f 66 28 2a 70 4f 70 65 6e c( sizeof(*pOpen
d0c20 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 ) );. if( p
d0c30 4f 70 65 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 Open==0 ){.
d0c40 20 20 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e releaseLockIn
d0c50 66 6f 28 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 20 fo(pLock);.
d0c60 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e rc = SQLITE_N
d0c70 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 67 6f OMEM;. go
d0c80 74 6f 20 65 78 69 74 5f 66 69 6e 64 6c 6f 63 6b to exit_findlock
d0c90 69 6e 66 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 info;. }.
d0ca0 20 20 20 20 6d 65 6d 73 65 74 28 70 4f 70 65 6e memset(pOpen
d0cb0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 4f 70 , 0, sizeof(*pOp
d0cc0 65 6e 29 29 3b 0a 20 20 20 20 20 20 70 4f 70 65 en));. pOpe
d0cd0 6e 2d 3e 66 69 6c 65 49 64 20 3d 20 66 69 6c 65 n->fileId = file
d0ce0 49 64 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d Id;. pOpen-
d0cf0 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 20 >nRef = 1;.
d0d00 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 20 3d 20 pOpen->pNext =
d0d10 6f 70 65 6e 4c 69 73 74 3b 0a 20 20 20 20 20 20 openList;.
d0d20 69 66 28 20 6f 70 65 6e 4c 69 73 74 20 29 20 6f if( openList ) o
d0d30 70 65 6e 4c 69 73 74 2d 3e 70 50 72 65 76 20 3d penList->pPrev =
d0d40 20 70 4f 70 65 6e 3b 0a 20 20 20 20 20 20 6f 70 pOpen;. op
d0d50 65 6e 4c 69 73 74 20 3d 20 70 4f 70 65 6e 3b 0a enList = pOpen;.
d0d60 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
d0d70 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 2b 2b 3b 0a pOpen->nRef++;.
d0d80 20 20 20 20 7d 0a 20 20 20 20 2a 70 70 4f 70 65 }. *ppOpe
d0d90 6e 20 3d 20 70 4f 70 65 6e 3b 0a 20 20 7d 0a 0a n = pOpen;. }..
d0da0 65 78 69 74 5f 66 69 6e 64 6c 6f 63 6b 69 6e 66 exit_findlockinf
d0db0 6f 3a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a o:. return rc;.
d0dc0 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 }../*.** If we a
d0dd0 72 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 re currently in
d0de0 61 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 a different thre
d0df0 61 64 20 74 68 61 6e 20 74 68 65 20 74 68 72 65 ad than the thre
d0e00 61 64 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 75 ad that the.** u
d0e10 6e 69 78 46 69 6c 65 20 61 72 67 75 6d 65 6e 74 nixFile argument
d0e20 20 62 65 6c 6f 6e 67 73 20 74 6f 2c 20 74 68 65 belongs to, the
d0e30 6e 20 74 72 61 6e 73 66 65 72 20 6f 77 6e 65 72 n transfer owner
d0e40 73 68 69 70 20 6f 66 20 74 68 65 20 75 6e 69 78 ship of the unix
d0e50 46 69 6c 65 0a 2a 2a 20 6f 76 65 72 20 74 6f 20 File.** over to
d0e60 74 68 65 20 63 75 72 72 65 6e 74 20 74 68 72 65 the current thre
d0e70 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 75 6e 69 78 ad..**.** A unix
d0e80 46 69 6c 65 20 69 73 20 6f 6e 6c 79 20 6f 77 6e File is only own
d0e90 65 64 20 62 79 20 61 20 74 68 72 65 61 64 20 6f ed by a thread o
d0ea0 6e 20 73 79 73 74 65 6d 73 20 74 68 61 74 20 75 n systems that u
d0eb0 73 65 20 4c 69 6e 75 78 54 68 72 65 61 64 73 2e se LinuxThreads.
d0ec0 0a 2a 2a 0a 2a 2a 20 4f 77 6e 65 72 73 68 69 70 .**.** Ownership
d0ed0 20 74 72 61 6e 73 66 65 72 20 69 73 20 6f 6e 6c transfer is onl
d0ee0 79 20 61 6c 6c 6f 77 65 64 20 69 66 20 74 68 65 y allowed if the
d0ef0 20 75 6e 69 78 46 69 6c 65 20 69 73 20 63 75 72 unixFile is cur
d0f00 72 65 6e 74 6c 79 20 75 6e 6c 6f 63 6b 65 64 2e rently unlocked.
d0f10 0a 2a 2a 20 49 66 20 74 68 65 20 75 6e 69 78 46 .** If the unixF
d0f20 69 6c 65 20 69 73 20 6c 6f 63 6b 65 64 20 61 6e ile is locked an
d0f30 64 20 61 6e 20 6f 77 6e 65 72 73 68 69 70 20 69 d an ownership i
d0f40 73 20 77 72 6f 6e 67 2c 20 74 68 65 6e 20 72 65 s wrong, then re
d0f50 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d turn.** SQLITE_M
d0f60 49 53 55 53 45 2e 20 20 53 51 4c 49 54 45 5f 4f ISUSE. SQLITE_O
d0f70 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 K is returned if
d0f80 20 65 76 65 72 79 74 68 69 6e 67 20 77 6f 72 6b everything work
d0f90 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 s..*/.#if SQLITE
d0fa0 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64 _THREADSAFE && d
d0fb0 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f efined(__linux__
d0fc0 29 0a 73 74 61 74 69 63 20 69 6e 74 20 74 72 61 ).static int tra
d0fd0 6e 73 66 65 72 4f 77 6e 65 72 73 68 69 70 28 75 nsferOwnership(u
d0fe0 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b nixFile *pFile){
d0ff0 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 70 74 68 . int rc;. pth
d1000 72 65 61 64 5f 74 20 68 53 65 6c 66 3b 0a 20 20 read_t hSelf;.
d1010 69 66 28 20 74 68 72 65 61 64 73 4f 76 65 72 72 if( threadsOverr
d1020 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63 ideEachOthersLoc
d1030 6b 73 20 29 7b 0a 20 20 20 20 2f 2a 20 4f 77 6e ks ){. /* Own
d1040 65 72 73 68 69 70 20 74 72 61 6e 73 66 65 72 73 ership transfers
d1050 20 6e 6f 74 20 6e 65 65 64 65 64 20 6f 6e 20 74 not needed on t
d1060 68 69 73 20 73 79 73 74 65 6d 20 2a 2f 0a 20 20 his system */.
d1070 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
d1080 4f 4b 3b 0a 20 20 7d 0a 20 20 68 53 65 6c 66 20 OK;. }. hSelf
d1090 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 = pthread_self()
d10a0 3b 0a 20 20 69 66 28 20 70 74 68 72 65 61 64 5f ;. if( pthread_
d10b0 65 71 75 61 6c 28 70 46 69 6c 65 2d 3e 74 69 64 equal(pFile->tid
d10c0 2c 20 68 53 65 6c 66 29 20 29 7b 0a 20 20 20 20 , hSelf) ){.
d10d0 2f 2a 20 57 65 20 61 72 65 20 73 74 69 6c 6c 20 /* We are still
d10e0 69 6e 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 in the same thre
d10f0 61 64 20 2a 2f 0a 20 20 20 20 4f 53 54 52 41 43 ad */. OSTRAC
d1100 45 31 28 22 4e 6f 2d 74 72 61 6e 73 66 65 72 2c E1("No-transfer,
d1110 20 73 61 6d 65 20 74 68 72 65 61 64 5c 6e 22 29 same thread\n")
d1120 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
d1130 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 ITE_OK;. }. if
d1140 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 ( pFile->locktyp
d1150 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 e!=NO_LOCK ){.
d1160 20 20 2f 2a 20 57 65 20 63 61 6e 6e 6f 74 20 63 /* We cannot c
d1170 68 61 6e 67 65 20 6f 77 6e 65 72 73 68 69 70 20 hange ownership
d1180 77 68 69 6c 65 20 77 65 20 61 72 65 20 68 6f 6c while we are hol
d1190 64 69 6e 67 20 61 20 6c 6f 63 6b 21 20 2a 2f 0a ding a lock! */.
d11a0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
d11b0 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 E_MISUSE;. }.
d11c0 4f 53 54 52 41 43 45 34 28 22 54 72 61 6e 73 66 OSTRACE4("Transf
d11d0 65 72 20 6f 77 6e 65 72 73 68 69 70 20 6f 66 20 er ownership of
d11e0 25 64 20 66 72 6f 6d 20 25 64 20 74 6f 20 25 64 %d from %d to %d
d11f0 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 \n",.
d1200 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 pFile->h, pFile
d1210 2d 3e 74 69 64 2c 20 68 53 65 6c 66 29 3b 0a 20 ->tid, hSelf);.
d1220 20 70 46 69 6c 65 2d 3e 74 69 64 20 3d 20 68 53 pFile->tid = hS
d1230 65 6c 66 3b 0a 20 20 69 66 20 28 70 46 69 6c 65 elf;. if (pFile
d1240 2d 3e 70 4c 6f 63 6b 20 21 3d 20 4e 55 4c 4c 29 ->pLock != NULL)
d1250 20 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 4c 6f {. releaseLo
d1260 63 6b 49 6e 66 6f 28 70 46 69 6c 65 2d 3e 70 4c ckInfo(pFile->pL
d1270 6f 63 6b 29 3b 0a 20 20 20 20 72 63 20 3d 20 66 ock);. rc = f
d1280 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70 46 69 6c indLockInfo(pFil
d1290 65 2c 20 26 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b e, &pFile->pLock
d12a0 2c 20 30 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 , 0);. OSTRAC
d12b0 45 35 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 69 E5("LOCK %d i
d12c0 73 20 6e 6f 77 20 25 73 28 25 73 2c 25 64 29 5c s now %s(%s,%d)\
d12d0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 n", pFile->h,.
d12e0 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 locktyp
d12f0 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 eName(pFile->loc
d1300 6b 74 79 70 65 29 2c 0a 20 20 20 20 20 20 20 20 ktype),.
d1310 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 locktypeName(
d1320 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f pFile->pLock->lo
d1330 63 6b 74 79 70 65 29 2c 20 70 46 69 6c 65 2d 3e cktype), pFile->
d1340 70 4c 6f 63 6b 2d 3e 63 6e 74 29 3b 0a 20 20 20 pLock->cnt);.
d1350 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 20 return rc;. }
d1360 65 6c 73 65 20 7b 0a 20 20 20 20 72 65 74 75 72 else {. retur
d1370 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
d1380 0a 7d 0a 23 65 6c 73 65 20 20 2f 2a 20 69 66 20 .}.#else /* if
d1390 6e 6f 74 20 53 51 4c 49 54 45 5f 54 48 52 45 41 not SQLITE_THREA
d13a0 44 53 41 46 45 20 2a 2f 0a 20 20 2f 2a 20 4f 6e DSAFE */. /* On
d13b0 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 single-threaded
d13c0 20 62 75 69 6c 64 73 2c 20 6f 77 6e 65 72 73 68 builds, ownersh
d13d0 69 70 20 74 72 61 6e 73 66 65 72 20 69 73 20 61 ip transfer is a
d13e0 20 6e 6f 2d 6f 70 20 2a 2f 0a 23 20 64 65 66 69 no-op */.# defi
d13f0 6e 65 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 ne transferOwner
d1400 73 68 69 70 28 58 29 20 53 51 4c 49 54 45 5f 4f ship(X) SQLITE_O
d1410 4b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 K.#endif /* SQLI
d1420 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 2a 2f TE_THREADSAFE */
d1430 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f .../*.** This ro
d1440 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 utine checks if
d1450 74 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52 there is a RESER
d1460 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e VED lock held on
d1470 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a the specified.*
d1480 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f * file by this o
d1490 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 r any other proc
d14a0 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c ess. If such a l
d14b0 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 74 ock is held, set
d14c0 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 *pResOut.** to
d14d0 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 a non-zero value
d14e0 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52 65 73 otherwise *pRes
d14f0 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65 Out is set to ze
d1500 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 ro. The return
d1510 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 value.** is set
d1520 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c to SQLITE_OK unl
d1530 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 ess an I/O error
d1540 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 6c occurs during l
d1550 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f ock checking..*/
d1560 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 .static int unix
d1570 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
d1580 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a k(sqlite3_file *
d1590 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 id, int *pResOut
d15a0 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
d15b0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 LITE_OK;. int r
d15c0 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 75 eserved = 0;. u
d15d0 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d nixFile *pFile =
d15e0 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a (unixFile*)id;.
d15f0 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 . SimulateIOErr
d1600 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 or( return SQLIT
d1610 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 E_IOERR_CHECKRES
d1620 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 0a 20 ERVEDLOCK; );..
d1630 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 assert( pFile )
d1640 3b 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 ;. unixEnterMut
d1650 65 78 28 29 3b 20 2f 2a 20 42 65 63 61 75 73 65 ex(); /* Because
d1660 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 69 73 pFile->pLock is
d1670 20 73 68 61 72 65 64 20 61 63 72 6f 73 73 20 74 shared across t
d1680 68 72 65 61 64 73 20 2a 2f 0a 0a 20 20 2f 2a 20 hreads */.. /*
d1690 43 68 65 63 6b 20 69 66 20 61 20 74 68 72 65 61 Check if a threa
d16a0 64 20 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73 d in this proces
d16b0 73 20 68 6f 6c 64 73 20 73 75 63 68 20 61 20 6c s holds such a l
d16c0 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 ock */. if( pFi
d16d0 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 le->pLock->lockt
d16e0 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 ype>SHARED_LOCK
d16f0 29 7b 0a 20 20 20 20 72 65 73 65 72 76 65 64 20 ){. reserved
d1700 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f = 1;. }.. /* O
d1710 74 68 65 72 77 69 73 65 20 73 65 65 20 69 66 20 therwise see if
d1720 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 some other proce
d1730 73 73 20 68 6f 6c 64 73 20 69 74 2e 0a 20 20 2a ss holds it.. *
d1740 2f 0a 23 69 66 6e 64 65 66 20 5f 5f 44 4a 47 50 /.#ifndef __DJGP
d1750 50 5f 5f 0a 20 20 69 66 28 20 21 72 65 73 65 72 P__. if( !reser
d1760 76 65 64 20 29 7b 0a 20 20 20 20 73 74 72 75 63 ved ){. struc
d1770 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 t flock lock;.
d1780 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 lock.l_whence
d1790 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20 = SEEK_SET;.
d17a0 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 52 lock.l_start = R
d17b0 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 ESERVED_BYTE;.
d17c0 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 lock.l_len = 1
d17d0 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 ;. lock.l_typ
d17e0 65 20 3d 20 46 5f 57 52 4c 43 4b 3b 0a 20 20 20 e = F_WRLCK;.
d17f0 20 69 66 20 28 2d 31 20 3d 3d 20 66 63 6e 74 6c if (-1 == fcntl
d1800 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 47 45 54 (pFile->h, F_GET
d1810 4c 4b 2c 20 26 6c 6f 63 6b 29 29 20 7b 0a 20 20 LK, &lock)) {.
d1820 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d int tErrno =
d1830 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 errno;. rc
d1840 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 = sqliteErrorFr
d1850 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 omPosixError(tEr
d1860 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 rno, SQLITE_IOER
d1870 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c R_CHECKRESERVEDL
d1880 4f 43 4b 29 3b 0a 20 20 20 20 20 20 70 46 69 6c OCK);. pFil
d1890 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 e->lastErrno = t
d18a0 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 20 65 6c 73 Errno;. } els
d18b0 65 20 69 66 28 20 6c 6f 63 6b 2e 6c 5f 74 79 70 e if( lock.l_typ
d18c0 65 21 3d 46 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20 e!=F_UNLCK ){.
d18d0 20 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 reserved = 1
d18e0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 ;. }. }.#end
d18f0 69 66 0a 20 20 0a 20 20 75 6e 69 78 4c 65 61 76 if. . unixLeav
d1900 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f 53 54 52 eMutex();. OSTR
d1910 41 43 45 34 28 22 54 45 53 54 20 57 52 2d 4c 4f ACE4("TEST WR-LO
d1920 43 4b 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 CK %d %d %d\n",
d1930 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65 pFile->h, rc, re
d1940 73 65 72 76 65 64 29 3b 0a 0a 20 20 2a 70 52 65 served);.. *pRe
d1950 73 4f 75 74 20 3d 20 72 65 73 65 72 76 65 64 3b sOut = reserved;
d1960 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
d1970 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61 ./*.** Perform a
d1980 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 6f 70 file locking op
d1990 65 72 61 74 69 6f 6e 20 6f 6e 20 61 20 72 61 6e eration on a ran
d19a0 67 65 20 6f 66 20 62 79 74 65 73 20 69 6e 20 61 ge of bytes in a
d19b0 20 66 69 6c 65 2e 0a 2a 2a 20 54 68 65 20 22 6f file..** The "o
d19c0 70 22 20 70 61 72 61 6d 65 74 65 72 20 73 68 6f p" parameter sho
d19d0 75 6c 64 20 62 65 20 6f 6e 65 20 6f 66 20 46 5f uld be one of F_
d19e0 52 44 4c 43 4b 2c 20 46 5f 57 52 4c 43 4b 2c 20 RDLCK, F_WRLCK,
d19f0 6f 72 20 46 5f 55 4e 4c 43 4b 2e 0a 2a 2a 20 52 or F_UNLCK..** R
d1a00 65 74 75 72 6e 20 30 20 6f 6e 20 73 75 63 63 65 eturn 0 on succe
d1a10 73 73 20 6f 72 20 2d 31 20 66 6f 72 20 66 61 69 ss or -1 for fai
d1a20 6c 75 72 65 2e 20 20 4f 6e 20 66 61 69 6c 75 72 lure. On failur
d1a30 65 2c 20 77 72 69 74 65 20 74 68 65 20 65 72 72 e, write the err
d1a40 6f 72 0a 2a 2a 20 63 6f 64 65 20 69 6e 74 6f 20 or.** code into
d1a50 2a 70 45 72 72 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a *pErrcode..**.**
d1a60 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f 57 If the SQLITE_W
d1a70 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e HOLE_FILE_LOCKIN
d1a80 47 20 62 69 74 20 69 73 20 63 6c 65 61 72 2c 20 G bit is clear,
d1a90 74 68 65 6e 20 6f 6e 6c 79 20 6c 6f 63 6b 0a 2a then only lock.*
d1aa0 2a 20 74 68 65 20 72 61 6e 67 65 20 6f 66 20 62 * the range of b
d1ab0 79 74 65 73 20 6f 6e 20 74 68 65 20 6c 6f 63 6b ytes on the lock
d1ac0 69 6e 67 20 70 61 67 65 20 62 65 74 77 65 65 6e ing page between
d1ad0 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 61 6e SHARED_FIRST an
d1ae0 64 0a 2a 2a 20 53 48 41 52 45 44 5f 53 49 5a 45 d.** SHARED_SIZE
d1af0 2e 20 20 49 66 20 53 51 4c 49 54 45 5f 57 48 4f . If SQLITE_WHO
d1b00 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 20 LE_FILE_LOCKING
d1b10 69 73 20 73 65 74 2c 20 74 68 65 6e 20 6c 6f 63 is set, then loc
d1b20 6b 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 73 20 66 k all.** bytes f
d1b30 72 6f 6d 20 30 20 75 70 20 74 6f 20 62 75 74 20 rom 0 up to but
d1b40 6e 6f 74 20 69 6e 63 6c 75 64 69 6e 67 20 50 45 not including PE
d1b50 4e 44 49 4e 47 5f 42 59 54 45 2c 20 61 6e 64 20 NDING_BYTE, and
d1b60 61 6c 6c 20 62 79 74 65 73 0a 2a 2a 20 74 68 61 all bytes.** tha
d1b70 74 20 66 6f 6c 6c 6f 77 20 53 48 41 52 45 44 5f t follow SHARED_
d1b80 46 49 52 53 54 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 FIRST..**.** In
d1b90 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 6f 66 20 other words, of
d1ba0 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c SQLITE_WHOLE_FIL
d1bb0 45 5f 4c 4f 43 4b 49 4e 47 20 69 66 20 66 61 6c E_LOCKING if fal
d1bc0 73 65 20 28 74 68 65 20 68 69 73 74 6f 72 69 63 se (the historic
d1bd0 61 6c 0a 2a 2a 20 64 65 66 61 75 6c 74 20 63 61 al.** default ca
d1be0 73 65 29 20 74 68 65 6e 20 6f 6e 6c 79 20 6c 6f se) then only lo
d1bf0 63 6b 20 61 20 73 6d 61 6c 6c 20 72 61 6e 67 65 ck a small range
d1c00 20 6f 66 20 62 79 74 65 73 20 66 72 6f 6d 20 53 of bytes from S
d1c10 48 41 52 45 44 5f 46 49 52 53 54 0a 2a 2a 20 74 HARED_FIRST.** t
d1c20 68 72 6f 75 67 68 20 53 48 41 52 45 44 5f 46 49 hrough SHARED_FI
d1c30 52 53 54 2b 53 48 41 52 45 44 5f 53 49 5a 45 2d RST+SHARED_SIZE-
d1c40 31 2e 20 20 42 75 74 20 69 66 20 53 51 4c 49 54 1. But if SQLIT
d1c50 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 E_WHOLE_FILE_LOC
d1c60 4b 49 4e 47 20 69 73 0a 2a 2a 20 74 72 75 65 20 KING is.** true
d1c70 74 68 65 6e 20 6c 6f 63 6b 20 65 76 65 72 79 20 then lock every
d1c80 62 79 74 65 20 69 6e 20 74 68 65 20 66 69 6c 65 byte in the file
d1c90 20 65 78 63 65 70 74 20 66 6f 72 20 50 45 4e 44 except for PEND
d1ca0 49 4e 47 5f 42 59 54 45 20 61 6e 64 0a 2a 2a 20 ING_BYTE and.**
d1cb0 52 45 53 45 52 56 45 44 5f 42 59 54 45 2e 0a 2a RESERVED_BYTE..*
d1cc0 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 57 48 4f 4c *.** SQLITE_WHOL
d1cd0 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 3d 74 E_FILE_LOCKING=t
d1ce0 72 75 65 20 6f 76 65 72 6c 61 70 73 20 53 51 4c rue overlaps SQL
d1cf0 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c ITE_WHOLE_FILE_L
d1d00 4f 43 4b 49 4e 47 3d 66 61 6c 73 65 0a 2a 2a 20 OCKING=false.**
d1d10 61 6e 64 20 73 6f 20 74 68 65 20 6c 6f 63 6b 69 and so the locki
d1d20 6e 67 20 73 63 68 65 6d 65 73 20 61 72 65 20 63 ng schemes are c
d1d30 6f 6d 70 61 74 69 62 6c 65 2e 20 20 4f 6e 65 20 ompatible. One
d1d40 74 79 70 65 20 6f 66 20 6c 6f 63 6b 20 77 69 6c type of lock wil
d1d50 6c 0a 2a 2a 20 65 66 66 65 63 74 69 76 65 6c 79 l.** effectively
d1d60 20 65 78 63 6c 75 64 65 20 74 68 65 20 6f 74 68 exclude the oth
d1d70 65 72 20 74 79 70 65 2e 20 20 54 68 65 20 72 65 er type. The re
d1d80 61 73 6f 6e 20 66 6f 72 20 75 73 69 6e 67 20 74 ason for using t
d1d90 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 57 48 4f he.** SQLITE_WHO
d1da0 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 3d LE_FILE_LOCKING=
d1db0 74 72 75 65 20 69 73 20 74 68 61 74 20 62 79 20 true is that by
d1dc0 69 6e 64 69 63 61 74 69 6e 67 20 74 68 65 20 66 indicating the f
d1dd0 75 6c 6c 20 72 61 6e 67 65 0a 2a 2a 20 6f 66 20 ull range.** of
d1de0 62 79 74 65 73 20 74 6f 20 62 65 20 72 65 61 64 bytes to be read
d1df0 20 6f 72 20 77 72 69 74 74 65 6e 2c 20 77 65 20 or written, we
d1e00 67 69 76 65 20 68 69 6e 74 73 20 74 6f 20 4e 46 give hints to NF
d1e10 53 20 74 6f 20 68 65 6c 70 20 69 74 0a 2a 2a 20 S to help it.**
d1e20 6d 61 69 6e 74 61 69 6e 20 63 61 63 68 65 20 63 maintain cache c
d1e30 6f 68 65 72 65 6e 63 79 2e 20 20 4f 6e 20 74 68 oherency. On th
d1e40 65 20 6f 74 68 65 72 20 68 61 6e 64 2c 20 77 68 e other hand, wh
d1e50 6f 6c 65 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 ole file locking
d1e60 0a 2a 2a 20 69 73 20 73 6c 6f 77 65 72 2c 20 73 .** is slower, s
d1e70 6f 20 77 65 20 64 6f 6e 27 74 20 77 61 6e 74 20 o we don't want
d1e80 74 6f 20 75 73 65 20 69 74 20 65 78 63 65 70 74 to use it except
d1e90 20 66 6f 72 20 4e 46 53 2e 0a 2a 2f 0a 73 74 61 for NFS..*/.sta
d1ea0 74 69 63 20 69 6e 74 20 72 61 6e 67 65 4c 6f 63 tic int rangeLoc
d1eb0 6b 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c k(unixFile *pFil
d1ec0 65 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 2a e, int op, int *
d1ed0 70 45 72 72 63 6f 64 65 29 7b 0a 20 20 73 74 72 pErrcode){. str
d1ee0 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a uct flock lock;.
d1ef0 20 20 69 6e 74 20 72 63 3b 0a 20 20 6c 6f 63 6b int rc;. lock
d1f00 2e 6c 5f 74 79 70 65 20 3d 20 6f 70 3b 0a 20 20 .l_type = op;.
d1f10 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 53 lock.l_start = S
d1f20 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 6c HARED_FIRST;. l
d1f30 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 ock.l_whence = S
d1f40 45 45 4b 5f 53 45 54 3b 0a 20 20 69 66 28 20 28 EEK_SET;. if( (
d1f50 70 46 69 6c 65 2d 3e 66 69 6c 65 46 6c 61 67 73 pFile->fileFlags
d1f60 20 26 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f & SQLITE_WHOLE_
d1f70 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 29 3d 3d 30 FILE_LOCKING)==0
d1f80 20 29 7b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c ){. lock.l_l
d1f90 65 6e 20 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 en = SHARED_SIZE
d1fa0 3b 0a 20 20 20 20 72 63 20 3d 20 66 63 6e 74 6c ;. rc = fcntl
d1fb0 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 (pFile->h, F_SET
d1fc0 4c 4b 2c 20 26 6c 6f 63 6b 29 3b 0a 20 20 20 20 LK, &lock);.
d1fd0 2a 70 45 72 72 63 6f 64 65 20 3d 20 65 72 72 6e *pErrcode = errn
d1fe0 6f 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 o;. }else{.
d1ff0 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 30 3b 0a lock.l_len = 0;.
d2000 20 20 20 20 72 63 20 3d 20 66 63 6e 74 6c 28 70 rc = fcntl(p
d2010 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b File->h, F_SETLK
d2020 2c 20 26 6c 6f 63 6b 29 3b 0a 20 20 20 20 2a 70 , &lock);. *p
d2030 45 72 72 63 6f 64 65 20 3d 20 65 72 72 6e 6f 3b Errcode = errno;
d2040 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 6f . if( NEVER(o
d2050 70 3d 3d 46 5f 55 4e 4c 43 4b 29 20 7c 7c 20 72 p==F_UNLCK) || r
d2060 63 21 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 c!=(-1) ){.
d2070 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 lock.l_start =
d2080 30 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 0;. lock.l_
d2090 6c 65 6e 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 len = PENDING_BY
d20a0 54 45 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 TE;. rc = f
d20b0 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46 cntl(pFile->h, F
d20c0 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3b 0a _SETLK, &lock);.
d20d0 20 20 20 20 20 20 69 66 28 20 41 4c 57 41 59 53 if( ALWAYS
d20e0 28 6f 70 21 3d 46 5f 55 4e 4c 43 4b 29 20 26 26 (op!=F_UNLCK) &&
d20f0 20 72 63 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 rc==(-1) ){.
d2100 20 20 20 20 20 2a 70 45 72 72 63 6f 64 65 20 3d *pErrcode =
d2110 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 errno;.
d2120 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f lock.l_type = F_
d2130 55 4e 4c 43 4b 3b 0a 20 20 20 20 20 20 20 20 6c UNLCK;. l
d2140 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 53 48 ock.l_start = SH
d2150 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 ARED_FIRST;.
d2160 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d lock.l_len =
d2170 20 30 3b 0a 20 20 20 20 20 20 20 20 66 63 6e 74 0;. fcnt
d2180 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 l(pFile->h, F_SE
d2190 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3b 0a 20 20 20 TLK, &lock);.
d21a0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
d21b0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
d21c0 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 *.** Lock the fi
d21d0 6c 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b le with the lock
d21e0 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 61 specified by pa
d21f0 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 rameter locktype
d2200 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 - one.** of the
d2210 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a following:.**.*
d2220 2a 20 20 20 20 20 28 31 29 20 53 48 41 52 45 44 * (1) SHARED
d2230 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 _LOCK.** (2)
d2240 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a RESERVED_LOCK.*
d2250 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e * (3) PENDIN
d2260 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 G_LOCK.** (4
d2270 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b ) EXCLUSIVE_LOCK
d2280 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 .**.** Sometimes
d2290 20 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 when requesting
d22a0 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c one lock state,
d22b0 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b additional lock
d22c0 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 states.** are i
d22d0 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 nserted in betwe
d22e0 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 en. The locking
d22f0 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f might fail on o
d2300 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a ne of the later.
d2310 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c ** transitions l
d2320 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 eaving the lock
d2330 73 74 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 state different
d2340 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 from what it sta
d2350 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c rted but.** stil
d2360 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 l short of its g
d2370 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 oal. The follow
d2380 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 ing chart shows
d2390 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 the allowed.** t
d23a0 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 ransitions and t
d23b0 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 he inserted inte
d23c0 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 73 3a rmediate states:
d23d0 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b .**.** UNLOCK
d23e0 45 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 ED -> SHARED.**
d23f0 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 SHARED -> RES
d2400 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 ERVED.** SHAR
d2410 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 ED -> (PENDING)
d2420 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 -> EXCLUSIVE.**
d2430 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 RESERVED -> (
d2440 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c PENDING) -> EXCL
d2450 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 USIVE.** PEND
d2460 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 ING -> EXCLUSIVE
d2470 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
d2480 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e ine will only in
d2490 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 crease a lock.
d24a0 55 73 65 20 74 68 65 20 73 71 6c 69 74 65 33 4f Use the sqlite3O
d24b0 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 sUnlock().** rou
d24c0 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61 20 tine to lower a
d24d0 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a locking level..*
d24e0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 /.static int uni
d24f0 78 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 xLock(sqlite3_fi
d2500 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b le *id, int lock
d2510 74 79 70 65 29 7b 0a 20 20 2f 2a 20 54 68 65 20 type){. /* The
d2520 66 6f 6c 6c 6f 77 69 6e 67 20 64 65 73 63 72 69 following descri
d2530 62 65 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e bes the implemen
d2540 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 76 61 tation of the va
d2550 72 69 6f 75 73 20 6c 6f 63 6b 73 20 61 6e 64 0a rious locks and.
d2560 20 20 2a 2a 20 6c 6f 63 6b 20 74 72 61 6e 73 69 ** lock transi
d2570 74 69 6f 6e 73 20 69 6e 20 74 65 72 6d 73 20 6f tions in terms o
d2580 66 20 74 68 65 20 50 4f 53 49 58 20 61 64 76 69 f the POSIX advi
d2590 73 6f 72 79 20 73 68 61 72 65 64 20 61 6e 64 20 sory shared and
d25a0 65 78 63 6c 75 73 69 76 65 0a 20 20 2a 2a 20 6c exclusive. ** l
d25b0 6f 63 6b 20 70 72 69 6d 69 74 69 76 65 73 20 28 ock primitives (
d25c0 63 61 6c 6c 65 64 20 72 65 61 64 2d 6c 6f 63 6b called read-lock
d25d0 73 20 61 6e 64 20 77 72 69 74 65 2d 6c 6f 63 6b s and write-lock
d25e0 73 20 62 65 6c 6f 77 2c 20 74 6f 20 61 76 6f 69 s below, to avoi
d25f0 64 0a 20 20 2a 2a 20 63 6f 6e 66 75 73 69 6f 6e d. ** confusion
d2600 20 77 69 74 68 20 53 51 4c 69 74 65 20 6c 6f 63 with SQLite loc
d2610 6b 20 6e 61 6d 65 73 29 2e 20 54 68 65 20 61 6c k names). The al
d2620 67 6f 72 69 74 68 6d 73 20 61 72 65 20 63 6f 6d gorithms are com
d2630 70 6c 69 63 61 74 65 64 0a 20 20 2a 2a 20 73 6c plicated. ** sl
d2640 69 67 68 74 6c 79 20 69 6e 20 6f 72 64 65 72 20 ightly in order
d2650 74 6f 20 62 65 20 63 6f 6d 70 61 74 69 62 6c 65 to be compatible
d2660 20 77 69 74 68 20 77 69 6e 64 6f 77 73 20 73 79 with windows sy
d2670 73 74 65 6d 73 20 73 69 6d 75 6c 74 61 6e 65 6f stems simultaneo
d2680 75 73 6c 79 0a 20 20 2a 2a 20 61 63 63 65 73 73 usly. ** access
d2690 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61 74 ing the same dat
d26a0 61 62 61 73 65 20 66 69 6c 65 2c 20 69 6e 20 63 abase file, in c
d26b0 61 73 65 20 74 68 61 74 20 69 73 20 65 76 65 72 ase that is ever
d26c0 20 72 65 71 75 69 72 65 64 2e 0a 20 20 2a 2a 0a required.. **.
d26d0 20 20 2a 2a 20 53 79 6d 62 6f 6c 73 20 64 65 66 ** Symbols def
d26e0 69 6e 65 64 20 69 6e 20 6f 73 2e 68 20 69 6e 64 ined in os.h ind
d26f0 65 6e 74 69 66 79 20 74 68 65 20 27 70 65 6e 64 entify the 'pend
d2700 69 6e 67 20 62 79 74 65 27 20 61 6e 64 20 74 68 ing byte' and th
d2710 65 20 27 72 65 73 65 72 76 65 64 0a 20 20 2a 2a e 'reserved. **
d2720 20 62 79 74 65 27 2c 20 65 61 63 68 20 73 69 6e byte', each sin
d2730 67 6c 65 20 62 79 74 65 73 20 61 74 20 77 65 6c gle bytes at wel
d2740 6c 20 6b 6e 6f 77 6e 20 6f 66 66 73 65 74 73 2c l known offsets,
d2750 20 61 6e 64 20 74 68 65 20 27 73 68 61 72 65 64 and the 'shared
d2760 20 62 79 74 65 0a 20 20 2a 2a 20 72 61 6e 67 65 byte. ** range
d2770 27 2c 20 61 20 72 61 6e 67 65 20 6f 66 20 35 31 ', a range of 51
d2780 30 20 62 79 74 65 73 20 61 74 20 61 20 77 65 6c 0 bytes at a wel
d2790 6c 20 6b 6e 6f 77 6e 20 6f 66 66 73 65 74 2e 0a l known offset..
d27a0 20 20 2a 2a 0a 20 20 2a 2a 20 54 6f 20 6f 62 74 **. ** To obt
d27b0 61 69 6e 20 61 20 53 48 41 52 45 44 20 6c 6f 63 ain a SHARED loc
d27c0 6b 2c 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 69 k, a read-lock i
d27d0 73 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20 74 68 s obtained on th
d27e0 65 20 27 70 65 6e 64 69 6e 67 0a 20 20 2a 2a 20 e 'pending. **
d27f0 62 79 74 65 27 2e 20 20 49 66 20 74 68 69 73 20 byte'. If this
d2800 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 61 is successful, a
d2810 20 72 61 6e 64 6f 6d 20 62 79 74 65 20 66 72 6f random byte fro
d2820 6d 20 74 68 65 20 27 73 68 61 72 65 64 20 62 79 m the 'shared by
d2830 74 65 0a 20 20 2a 2a 20 72 61 6e 67 65 27 20 69 te. ** range' i
d2840 73 20 72 65 61 64 2d 6c 6f 63 6b 65 64 20 61 6e s read-locked an
d2850 64 20 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 d the lock on th
d2860 65 20 27 70 65 6e 64 69 6e 67 20 62 79 74 65 27 e 'pending byte'
d2870 20 72 65 6c 65 61 73 65 64 2e 0a 20 20 2a 2a 0a released.. **.
d2880 20 20 2a 2a 20 41 20 70 72 6f 63 65 73 73 20 6d ** A process m
d2890 61 79 20 6f 6e 6c 79 20 6f 62 74 61 69 6e 20 61 ay only obtain a
d28a0 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 61 RESERVED lock a
d28b0 66 74 65 72 20 69 74 20 68 61 73 20 61 20 53 48 fter it has a SH
d28c0 41 52 45 44 20 6c 6f 63 6b 2e 0a 20 20 2a 2a 20 ARED lock.. **
d28d0 41 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 A RESERVED lock
d28e0 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 is implemented b
d28f0 79 20 67 72 61 62 62 69 6e 67 20 61 20 77 72 69 y grabbing a wri
d2900 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 te-lock on the.
d2910 20 2a 2a 20 27 72 65 73 65 72 76 65 64 20 62 79 ** 'reserved by
d2920 74 65 27 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 te'. . **. **
d2930 41 20 70 72 6f 63 65 73 73 20 6d 61 79 20 6f 6e A process may on
d2940 6c 79 20 6f 62 74 61 69 6e 20 61 20 50 45 4e 44 ly obtain a PEND
d2950 49 4e 47 20 6c 6f 63 6b 20 61 66 74 65 72 20 69 ING lock after i
d2960 74 20 68 61 73 20 6f 62 74 61 69 6e 65 64 20 61 t has obtained a
d2970 0a 20 20 2a 2a 20 53 48 41 52 45 44 20 6c 6f 63 . ** SHARED loc
d2980 6b 2e 20 41 20 50 45 4e 44 49 4e 47 20 6c 6f 63 k. A PENDING loc
d2990 6b 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 k is implemented
d29a0 20 62 79 20 6f 62 74 61 69 6e 69 6e 67 20 61 20 by obtaining a
d29b0 77 72 69 74 65 2d 6c 6f 63 6b 0a 20 20 2a 2a 20 write-lock. **
d29c0 6f 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67 20 on the 'pending
d29d0 62 79 74 65 27 2e 20 54 68 69 73 20 65 6e 73 75 byte'. This ensu
d29e0 72 65 73 20 74 68 61 74 20 6e 6f 20 6e 65 77 20 res that no new
d29f0 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 63 61 6e SHARED locks can
d2a00 20 62 65 0a 20 20 2a 2a 20 6f 62 74 61 69 6e 65 be. ** obtaine
d2a10 64 2c 20 62 75 74 20 65 78 69 73 74 69 6e 67 20 d, but existing
d2a20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 61 72 65 SHARED locks are
d2a30 20 61 6c 6c 6f 77 65 64 20 74 6f 20 70 65 72 73 allowed to pers
d2a40 69 73 74 2e 20 41 20 70 72 6f 63 65 73 73 0a 20 ist. A process.
d2a50 20 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 ** does not hav
d2a60 65 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 52 45 e to obtain a RE
d2a70 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 SERVED lock on t
d2a80 68 65 20 77 61 79 20 74 6f 20 61 20 50 45 4e 44 he way to a PEND
d2a90 49 4e 47 20 6c 6f 63 6b 2e 0a 20 20 2a 2a 20 54 ING lock.. ** T
d2aa0 68 69 73 20 70 72 6f 70 65 72 74 79 20 69 73 20 his property is
d2ab0 75 73 65 64 20 62 79 20 74 68 65 20 61 6c 67 6f used by the algo
d2ac0 72 69 74 68 6d 20 66 6f 72 20 72 6f 6c 6c 69 6e rithm for rollin
d2ad0 67 20 62 61 63 6b 20 61 20 6a 6f 75 72 6e 61 6c g back a journal
d2ae0 20 66 69 6c 65 0a 20 20 2a 2a 20 61 66 74 65 72 file. ** after
d2af0 20 61 20 63 72 61 73 68 2e 0a 20 20 2a 2a 0a 20 a crash.. **.
d2b00 20 2a 2a 20 41 6e 20 45 58 43 4c 55 53 49 56 45 ** An EXCLUSIVE
d2b10 20 6c 6f 63 6b 2c 20 6f 62 74 61 69 6e 65 64 20 lock, obtained
d2b20 61 66 74 65 72 20 61 20 50 45 4e 44 49 4e 47 20 after a PENDING
d2b30 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 69 73 lock is held, is
d2b40 0a 20 20 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 65 . ** implemente
d2b50 64 20 62 79 20 6f 62 74 61 69 6e 69 6e 67 20 61 d by obtaining a
d2b60 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 write-lock on t
d2b70 68 65 20 65 6e 74 69 72 65 20 27 73 68 61 72 65 he entire 'share
d2b80 64 20 62 79 74 65 0a 20 20 2a 2a 20 72 61 6e 67 d byte. ** rang
d2b90 65 27 2e 20 53 69 6e 63 65 20 61 6c 6c 20 6f 74 e'. Since all ot
d2ba0 68 65 72 20 6c 6f 63 6b 73 20 72 65 71 75 69 72 her locks requir
d2bb0 65 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e e a read-lock on
d2bc0 20 6f 6e 65 20 6f 66 20 74 68 65 20 62 79 74 65 one of the byte
d2bd0 73 0a 20 20 2a 2a 20 77 69 74 68 69 6e 20 74 68 s. ** within th
d2be0 69 73 20 72 61 6e 67 65 2c 20 74 68 69 73 20 65 is range, this e
d2bf0 6e 73 75 72 65 73 20 74 68 61 74 20 6e 6f 20 6f nsures that no o
d2c00 74 68 65 72 20 6c 6f 63 6b 73 20 61 72 65 20 68 ther locks are h
d2c10 65 6c 64 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 eld on the. **
d2c20 64 61 74 61 62 61 73 65 2e 20 0a 20 20 2a 2a 0a database. . **.
d2c30 20 20 2a 2a 20 54 68 65 20 72 65 61 73 6f 6e 20 ** The reason
d2c40 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20 63 61 a single byte ca
d2c50 6e 6e 6f 74 20 62 65 20 75 73 65 64 20 69 6e 73 nnot be used ins
d2c60 74 65 61 64 20 6f 66 20 74 68 65 20 27 73 68 61 tead of the 'sha
d2c70 72 65 64 20 62 79 74 65 0a 20 20 2a 2a 20 72 61 red byte. ** ra
d2c80 6e 67 65 27 20 69 73 20 74 68 61 74 20 73 6f 6d nge' is that som
d2c90 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 77 69 e versions of wi
d2ca0 6e 64 6f 77 73 20 64 6f 20 6e 6f 74 20 73 75 70 ndows do not sup
d2cb0 70 6f 72 74 20 72 65 61 64 2d 6c 6f 63 6b 73 2e port read-locks.
d2cc0 20 42 79 0a 20 20 2a 2a 20 6c 6f 63 6b 69 6e 67 By. ** locking
d2cd0 20 61 20 72 61 6e 64 6f 6d 20 62 79 74 65 20 66 a random byte f
d2ce0 72 6f 6d 20 61 20 72 61 6e 67 65 2c 20 63 6f 6e rom a range, con
d2cf0 63 75 72 72 65 6e 74 20 53 48 41 52 45 44 20 6c current SHARED l
d2d00 6f 63 6b 73 20 6d 61 79 20 65 78 69 73 74 0a 20 ocks may exist.
d2d10 20 2a 2a 20 65 76 65 6e 20 69 66 20 74 68 65 20 ** even if the
d2d20 6c 6f 63 6b 69 6e 67 20 70 72 69 6d 69 74 69 76 locking primitiv
d2d30 65 20 75 73 65 64 20 69 73 20 61 6c 77 61 79 73 e used is always
d2d40 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 2e 0a 20 a write-lock..
d2d50 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 */. int rc = S
d2d60 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78 QLITE_OK;. unix
d2d70 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 File *pFile = (u
d2d80 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 73 nixFile*)id;. s
d2d90 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e truct unixLockIn
d2da0 66 6f 20 2a 70 4c 6f 63 6b 20 3d 20 70 46 69 6c fo *pLock = pFil
d2db0 65 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 73 74 72 75 e->pLock;. stru
d2dc0 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 ct flock lock;.
d2dd0 20 69 6e 74 20 73 20 3d 20 30 3b 0a 20 20 69 6e int s = 0;. in
d2de0 74 20 74 45 72 72 6e 6f 3b 0a 0a 20 20 61 73 73 t tErrno;.. ass
d2df0 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 ert( pFile );.
d2e00 4f 53 54 52 41 43 45 37 28 22 4c 4f 43 4b 20 20 OSTRACE7("LOCK
d2e10 20 20 25 64 20 25 73 20 77 61 73 20 25 73 28 25 %d %s was %s(%
d2e20 73 2c 25 64 29 20 70 69 64 3d 25 64 5c 6e 22 2c s,%d) pid=%d\n",
d2e30 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 pFile->h,.
d2e40 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f locktypeName(lo
d2e50 63 6b 74 79 70 65 29 2c 20 6c 6f 63 6b 74 79 70 cktype), locktyp
d2e60 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 eName(pFile->loc
d2e70 6b 74 79 70 65 29 2c 0a 20 20 20 20 20 20 6c 6f ktype),. lo
d2e80 63 6b 74 79 70 65 4e 61 6d 65 28 70 4c 6f 63 6b cktypeName(pLock
d2e90 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 20 70 4c 6f ->locktype), pLo
d2ea0 63 6b 2d 3e 63 6e 74 20 2c 20 67 65 74 70 69 64 ck->cnt , getpid
d2eb0 28 29 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 ());.. /* If th
d2ec0 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 ere is already a
d2ed0 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 79 lock of this ty
d2ee0 70 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 pe or more restr
d2ef0 69 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 ictive on the.
d2f00 2a 2a 20 75 6e 69 78 46 69 6c 65 2c 20 64 6f 20 ** unixFile, do
d2f10 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 nothing. Don't u
d2f20 73 65 20 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a se the end_lock:
d2f30 20 65 78 69 74 20 70 61 74 68 2c 20 61 73 0a 20 exit path, as.
d2f40 20 2a 2a 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 ** unixEnterMut
d2f50 65 78 28 29 20 68 61 73 6e 27 74 20 62 65 65 6e ex() hasn't been
d2f60 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a 20 20 2a called yet.. *
d2f70 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c /. if( pFile->l
d2f80 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74 79 70 ocktype>=locktyp
d2f90 65 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 e ){. OSTRACE
d2fa0 33 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 3("LOCK %d %s
d2fb0 20 6f 6b 20 28 61 6c 72 65 61 64 79 20 68 65 6c ok (already hel
d2fc0 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c d)\n", pFile->h,
d2fd0 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f 63 . loc
d2fe0 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 ktypeName(lockty
d2ff0 70 65 29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e pe));. return
d3000 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
d3010 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 . /* Make sure
d3020 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 65 71 75 the locking sequ
d3030 65 6e 63 65 20 69 73 20 63 6f 72 72 65 63 74 2e ence is correct.
d3040 0a 20 20 2a 2a 20 20 28 31 29 20 57 65 20 6e 65 . ** (1) We ne
d3050 76 65 72 20 6d 6f 76 65 20 66 72 6f 6d 20 75 6e ver move from un
d3060 6c 6f 63 6b 65 64 20 74 6f 20 61 6e 79 74 68 69 locked to anythi
d3070 6e 67 20 68 69 67 68 65 72 20 74 68 61 6e 20 73 ng higher than s
d3080 68 61 72 65 64 20 6c 6f 63 6b 2e 0a 20 20 2a 2a hared lock.. **
d3090 20 20 28 32 29 20 53 51 4c 69 74 65 20 6e 65 76 (2) SQLite nev
d30a0 65 72 20 65 78 70 6c 69 63 69 74 6c 79 20 72 65 er explicitly re
d30b0 71 75 65 73 74 73 20 61 20 70 65 6e 64 69 67 20 quests a pendig
d30c0 6c 6f 63 6b 2e 0a 20 20 2a 2a 20 20 28 33 29 20 lock.. ** (3)
d30d0 41 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 69 73 A shared lock is
d30e0 20 61 6c 77 61 79 73 20 68 65 6c 64 20 77 68 65 always held whe
d30f0 6e 20 61 20 72 65 73 65 72 76 65 20 6c 6f 63 6b n a reserve lock
d3100 20 69 73 20 72 65 71 75 65 73 74 65 64 2e 0a 20 is requested..
d3110 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 46 */. assert( pF
d3120 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e ile->locktype!=N
d3130 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 O_LOCK || lockty
d3140 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 pe==SHARED_LOCK
d3150 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 );. assert( loc
d3160 6b 74 79 70 65 21 3d 50 45 4e 44 49 4e 47 5f 4c ktype!=PENDING_L
d3170 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 OCK );. assert(
d3180 20 6c 6f 63 6b 74 79 70 65 21 3d 52 45 53 45 52 locktype!=RESER
d3190 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 46 69 6c VED_LOCK || pFil
d31a0 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 e->locktype==SHA
d31b0 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f RED_LOCK );.. /
d31c0 2a 20 54 68 69 73 20 6d 75 74 65 78 20 69 73 20 * This mutex is
d31d0 6e 65 65 64 65 64 20 62 65 63 61 75 73 65 20 70 needed because p
d31e0 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 69 73 20 73 File->pLock is s
d31f0 68 61 72 65 64 20 61 63 72 6f 73 73 20 74 68 72 hared across thr
d3200 65 61 64 73 0a 20 20 2a 2f 0a 20 20 75 6e 69 78 eads. */. unix
d3210 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 0a 20 EnterMutex();..
d3220 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 /* Make sure th
d3230 65 20 63 75 72 72 65 6e 74 20 74 68 72 65 61 64 e current thread
d3240 20 6f 77 6e 73 20 74 68 65 20 70 46 69 6c 65 2e owns the pFile.
d3250 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 74 72 61 . */. rc = tra
d3260 6e 73 66 65 72 4f 77 6e 65 72 73 68 69 70 28 70 nsferOwnership(p
d3270 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 21 File);. if( rc!
d3280 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
d3290 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 unixLeaveMutex
d32a0 28 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 ();. return r
d32b0 63 3b 0a 20 20 7d 0a 20 20 70 4c 6f 63 6b 20 3d c;. }. pLock =
d32c0 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 3b 0a 0a pFile->pLock;..
d32d0 20 20 2f 2a 20 49 66 20 73 6f 6d 65 20 74 68 72 /* If some thr
d32e0 65 61 64 20 75 73 69 6e 67 20 74 68 69 73 20 50 ead using this P
d32f0 49 44 20 68 61 73 20 61 20 6c 6f 63 6b 20 76 69 ID has a lock vi
d3300 61 20 61 20 64 69 66 66 65 72 65 6e 74 20 75 6e a a different un
d3310 69 78 46 69 6c 65 2a 0a 20 20 2a 2a 20 68 61 6e ixFile*. ** han
d3320 64 6c 65 20 74 68 61 74 20 70 72 65 63 6c 75 64 dle that preclud
d3330 65 73 20 74 68 65 20 72 65 71 75 65 73 74 65 64 es the requested
d3340 20 6c 6f 63 6b 2c 20 72 65 74 75 72 6e 20 42 55 lock, return BU
d3350 53 59 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 SY.. */. if( (
d3360 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 pFile->locktype!
d3370 3d 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 =pLock->locktype
d3380 20 26 26 20 0a 20 20 20 20 20 20 20 20 20 20 28 && . (
d3390 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3e pLock->locktype>
d33a0 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 7c 7c =PENDING_LOCK ||
d33b0 20 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 locktype>SHARED
d33c0 5f 4c 4f 43 4b 29 29 0a 20 20 29 7b 0a 20 20 20 _LOCK)). ){.
d33d0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 rc = SQLITE_BUS
d33e0 59 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f Y;. goto end_
d33f0 6c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 lock;. }.. /*
d3400 49 66 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b If a SHARED lock
d3410 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 61 is requested, a
d3420 6e 64 20 73 6f 6d 65 20 74 68 72 65 61 64 20 75 nd some thread u
d3430 73 69 6e 67 20 74 68 69 73 20 50 49 44 20 61 6c sing this PID al
d3440 72 65 61 64 79 0a 20 20 2a 2a 20 68 61 73 20 61 ready. ** has a
d3450 20 53 48 41 52 45 44 20 6f 72 20 52 45 53 45 52 SHARED or RESER
d3460 56 45 44 20 6c 6f 63 6b 2c 20 74 68 65 6e 20 69 VED lock, then i
d3470 6e 63 72 65 6d 65 6e 74 20 72 65 66 65 72 65 6e ncrement referen
d3480 63 65 20 63 6f 75 6e 74 73 20 61 6e 64 0a 20 20 ce counts and.
d3490 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ** return SQLITE
d34a0 5f 4f 4b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 _OK.. */. if(
d34b0 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 locktype==SHARED
d34c0 5f 4c 4f 43 4b 20 26 26 20 0a 20 20 20 20 20 20 _LOCK && .
d34d0 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 (pLock->locktype
d34e0 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 7c 7c ==SHARED_LOCK ||
d34f0 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 pLock->locktype
d3500 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 ==RESERVED_LOCK)
d3510 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
d3520 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 locktype==SHARED
d3530 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 61 73 73 _LOCK );. ass
d3540 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b ert( pFile->lock
d3550 74 79 70 65 3d 3d 30 20 29 3b 0a 20 20 20 20 61 type==0 );. a
d3560 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 63 6e ssert( pLock->cn
d3570 74 3e 30 20 29 3b 0a 20 20 20 20 70 46 69 6c 65 t>0 );. pFile
d3580 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 ->locktype = SHA
d3590 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 70 4c RED_LOCK;. pL
d35a0 6f 63 6b 2d 3e 63 6e 74 2b 2b 3b 0a 20 20 20 20 ock->cnt++;.
d35b0 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c pFile->pOpen->nL
d35c0 6f 63 6b 2b 2b 3b 0a 20 20 20 20 67 6f 74 6f 20 ock++;. goto
d35d0 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 0a 0a end_lock;. }...
d35e0 20 20 2f 2a 20 41 20 50 45 4e 44 49 4e 47 20 6c /* A PENDING l
d35f0 6f 63 6b 20 69 73 20 6e 65 65 64 65 64 20 62 65 ock is needed be
d3600 66 6f 72 65 20 61 63 71 75 69 72 69 6e 67 20 61 fore acquiring a
d3610 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 61 6e 64 SHARED lock and
d3620 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 61 63 71 before. ** acq
d3630 75 69 72 69 6e 67 20 61 6e 20 45 58 43 4c 55 53 uiring an EXCLUS
d3640 49 56 45 20 6c 6f 63 6b 2e 20 20 46 6f 72 20 74 IVE lock. For t
d3650 68 65 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 he SHARED lock,
d3660 74 68 65 20 50 45 4e 44 49 4e 47 20 77 69 6c 6c the PENDING will
d3670 0a 20 20 2a 2a 20 62 65 20 72 65 6c 65 61 73 65 . ** be release
d3680 64 2e 0a 20 20 2a 2f 0a 20 20 6c 6f 63 6b 2e 6c d.. */. lock.l
d3690 5f 6c 65 6e 20 3d 20 31 4c 3b 0a 20 20 6c 6f 63 _len = 1L;. loc
d36a0 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 k.l_whence = SEE
d36b0 4b 5f 53 45 54 3b 0a 20 20 69 66 28 20 6c 6f 63 K_SET;. if( loc
d36c0 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f ktype==SHARED_LO
d36d0 43 4b 20 0a 20 20 20 20 20 20 7c 7c 20 28 6c 6f CK . || (lo
d36e0 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 cktype==EXCLUSIV
d36f0 45 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c 65 2d E_LOCK && pFile-
d3700 3e 6c 6f 63 6b 74 79 70 65 3c 50 45 4e 44 49 4e >locktype<PENDIN
d3710 47 5f 4c 4f 43 4b 29 0a 20 20 29 7b 0a 20 20 20 G_LOCK). ){.
d3720 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 28 lock.l_type = (
d3730 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 locktype==SHARED
d3740 5f 4c 4f 43 4b 3f 46 5f 52 44 4c 43 4b 3a 46 5f _LOCK?F_RDLCK:F_
d3750 57 52 4c 43 4b 29 3b 0a 20 20 20 20 6c 6f 63 6b WRLCK);. lock
d3760 2e 6c 5f 73 74 61 72 74 20 3d 20 50 45 4e 44 49 .l_start = PENDI
d3770 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 73 20 3d NG_BYTE;. s =
d3780 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c fcntl(pFile->h,
d3790 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 F_SETLK, &lock)
d37a0 3b 0a 20 20 20 20 69 66 28 20 73 3d 3d 28 2d 31 ;. if( s==(-1
d37b0 29 20 29 7b 0a 20 20 20 20 20 20 74 45 72 72 6e ) ){. tErrn
d37c0 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 o = errno;.
d37d0 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f rc = sqliteErro
d37e0 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 rFromPosixError(
d37f0 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 tErrno, SQLITE_I
d3800 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 OERR_LOCK);.
d3810 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 if( IS_LOCK_ER
d3820 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 ROR(rc) ){.
d3830 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 pFile->lastEr
d3840 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 rno = tErrno;.
d3850 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f }. goto
d3860 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d end_lock;. }
d3870 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 49 66 20 63 . }... /* If c
d3880 6f 6e 74 72 6f 6c 20 67 65 74 73 20 74 6f 20 74 ontrol gets to t
d3890 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e 20 his point, then
d38a0 61 63 74 75 61 6c 6c 79 20 67 6f 20 61 68 65 61 actually go ahea
d38b0 64 20 61 6e 64 20 6d 61 6b 65 0a 20 20 2a 2a 20 d and make. **
d38c0 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d operating system
d38d0 20 63 61 6c 6c 73 20 66 6f 72 20 74 68 65 20 73 calls for the s
d38e0 70 65 63 69 66 69 65 64 20 6c 6f 63 6b 2e 0a 20 pecified lock..
d38f0 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 */. if( lockty
d3900 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 pe==SHARED_LOCK
d3910 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
d3920 4c 6f 63 6b 2d 3e 63 6e 74 3d 3d 30 20 29 3b 0a Lock->cnt==0 );.
d3930 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 assert( pLoc
d3940 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 30 20 29 k->locktype==0 )
d3950 3b 0a 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 67 65 ;.. /* Now ge
d3960 74 20 74 68 65 20 72 65 61 64 2d 6c 6f 63 6b 20 t the read-lock
d3970 2a 2f 0a 20 20 20 20 73 20 3d 20 72 61 6e 67 65 */. s = range
d3980 4c 6f 63 6b 28 70 46 69 6c 65 2c 20 46 5f 52 44 Lock(pFile, F_RD
d3990 4c 43 4b 2c 20 26 74 45 72 72 6e 6f 29 3b 0a 0a LCK, &tErrno);..
d39a0 20 20 20 20 2f 2a 20 44 72 6f 70 20 74 68 65 20 /* Drop the
d39b0 74 65 6d 70 6f 72 61 72 79 20 50 45 4e 44 49 4e temporary PENDIN
d39c0 47 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 6c 6f G lock */. lo
d39d0 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 50 45 4e ck.l_start = PEN
d39e0 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 6c DING_BYTE;. l
d39f0 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 4c 3b 0a ock.l_len = 1L;.
d3a00 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 lock.l_type
d3a10 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 69 = F_UNLCK;. i
d3a20 66 28 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e f( fcntl(pFile->
d3a30 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 h, F_SETLK, &loc
d3a40 6b 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 k)!=0 ){. i
d3a50 66 28 20 73 20 21 3d 20 2d 31 20 29 7b 0a 20 20 f( s != -1 ){.
d3a60 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 63 6f /* This co
d3a70 75 6c 64 20 68 61 70 70 65 6e 20 77 69 74 68 20 uld happen with
d3a80 61 20 6e 65 74 77 6f 72 6b 20 6d 6f 75 6e 74 20 a network mount
d3a90 2a 2f 0a 20 20 20 20 20 20 20 20 74 45 72 72 6e */. tErrn
d3aa0 6f 20 3d 20 65 72 72 6e 6f 3b 20 0a 20 20 20 20 o = errno; .
d3ab0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 rc = sqliteE
d3ac0 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 rrorFromPosixErr
d3ad0 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 or(tErrno, SQLIT
d3ae0 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b E_IOERR_UNLOCK);
d3af0 20 0a 20 20 20 20 20 20 20 20 69 66 28 20 49 53 . if( IS
d3b00 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 _LOCK_ERROR(rc)
d3b10 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46 69 ){. pFi
d3b20 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
d3b30 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 tErrno;.
d3b40 7d 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 }. goto e
d3b50 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 20 20 7d nd_lock;. }
d3b60 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 . }. if( s
d3b70 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 ==(-1) ){.
d3b80 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 rc = sqliteError
d3b90 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 FromPosixError(t
d3ba0 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f Errno, SQLITE_IO
d3bb0 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 ERR_LOCK);.
d3bc0 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 if( IS_LOCK_ERR
d3bd0 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 OR(rc) ){.
d3be0 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
d3bf0 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 no = tErrno;.
d3c00 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
d3c10 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 pFile->loc
d3c20 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c ktype = SHARED_L
d3c30 4f 43 4b 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 OCK;. pFile
d3c40 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2b 2b ->pOpen->nLock++
d3c50 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 63 ;. pLock->c
d3c60 6e 74 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 nt = 1;. }.
d3c70 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 74 79 }else if( lockty
d3c80 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f pe==EXCLUSIVE_LO
d3c90 43 4b 20 26 26 20 70 4c 6f 63 6b 2d 3e 63 6e 74 CK && pLock->cnt
d3ca0 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 >1 ){. /* We
d3cb0 61 72 65 20 74 72 79 69 6e 67 20 66 6f 72 20 61 are trying for a
d3cc0 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b n exclusive lock
d3cd0 20 62 75 74 20 61 6e 6f 74 68 65 72 20 74 68 72 but another thr
d3ce0 65 61 64 20 69 6e 20 74 68 69 73 0a 20 20 20 20 ead in this.
d3cf0 2a 2a 20 73 61 6d 65 20 70 72 6f 63 65 73 73 20 ** same process
d3d00 69 73 20 73 74 69 6c 6c 20 68 6f 6c 64 69 6e 67 is still holding
d3d10 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e 20 a shared lock.
d3d20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 */. rc = SQLI
d3d30 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73 65 TE_BUSY;. }else
d3d40 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65 71 {. /* The req
d3d50 75 65 73 74 20 77 61 73 20 66 6f 72 20 61 20 52 uest was for a R
d3d60 45 53 45 52 56 45 44 20 6f 72 20 45 58 43 4c 55 ESERVED or EXCLU
d3d70 53 49 56 45 20 6c 6f 63 6b 2e 20 20 49 74 20 69 SIVE lock. It i
d3d80 73 0a 20 20 20 20 2a 2a 20 61 73 73 75 6d 65 64 s. ** assumed
d3d90 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20 61 that there is a
d3da0 20 53 48 41 52 45 44 20 6f 72 20 67 72 65 61 74 SHARED or great
d3db0 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 er lock on the f
d3dc0 69 6c 65 0a 20 20 20 20 2a 2a 20 61 6c 72 65 61 ile. ** alrea
d3dd0 64 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 dy.. */. a
d3de0 73 73 65 72 74 28 20 30 21 3d 70 46 69 6c 65 2d ssert( 0!=pFile-
d3df0 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 >locktype );.
d3e00 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 lock.l_type = F
d3e10 5f 57 52 4c 43 4b 3b 0a 20 20 20 20 73 77 69 74 _WRLCK;. swit
d3e20 63 68 28 20 6c 6f 63 6b 74 79 70 65 20 29 7b 0a ch( locktype ){.
d3e30 20 20 20 20 20 20 63 61 73 65 20 52 45 53 45 52 case RESER
d3e40 56 45 44 5f 4c 4f 43 4b 3a 0a 20 20 20 20 20 20 VED_LOCK:.
d3e50 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d lock.l_start =
d3e60 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a RESERVED_BYTE;.
d3e70 20 20 20 20 20 20 20 20 73 20 3d 20 66 63 6e 74 s = fcnt
d3e80 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 l(pFile->h, F_SE
d3e90 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3b 0a 20 20 20 TLK, &lock);.
d3ea0 20 20 20 20 20 74 45 72 72 6e 6f 20 3d 20 65 72 tErrno = er
d3eb0 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 62 72 65 rno;. bre
d3ec0 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 45 ak;. case E
d3ed0 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 3a 0a 20 XCLUSIVE_LOCK:.
d3ee0 20 20 20 20 20 20 20 73 20 3d 20 72 61 6e 67 65 s = range
d3ef0 4c 6f 63 6b 28 70 46 69 6c 65 2c 20 46 5f 57 52 Lock(pFile, F_WR
d3f00 4c 43 4b 2c 20 26 74 45 72 72 6e 6f 29 3b 0a 20 LCK, &tErrno);.
d3f10 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
d3f20 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 default:.
d3f30 20 20 20 20 20 61 73 73 65 72 74 28 30 29 3b 0a assert(0);.
d3f40 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 3d }. if( s=
d3f50 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 72 =(-1) ){. r
d3f60 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 c = sqliteErrorF
d3f70 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 romPosixError(tE
d3f80 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 rrno, SQLITE_IOE
d3f90 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 RR_LOCK);.
d3fa0 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f if( IS_LOCK_ERRO
d3fb0 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 R(rc) ){.
d3fc0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
d3fd0 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 o = tErrno;.
d3fe0 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 }. }. }.
d3ff0 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 ..#ifndef NDEBUG
d4000 0a 20 20 2f 2a 20 53 65 74 20 75 70 20 74 68 65 . /* Set up the
d4010 20 74 72 61 6e 73 61 63 74 69 6f 6e 2d 63 6f 75 transaction-cou
d4020 6e 74 65 72 20 63 68 61 6e 67 65 20 63 68 65 63 nter change chec
d4030 6b 69 6e 67 20 66 6c 61 67 73 20 77 68 65 6e 0a king flags when.
d4040 20 20 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 69 ** transitioni
d4050 6e 67 20 66 72 6f 6d 20 61 20 53 48 41 52 45 44 ng from a SHARED
d4060 20 74 6f 20 61 20 52 45 53 45 52 56 45 44 20 6c to a RESERVED l
d4070 6f 63 6b 2e 20 20 54 68 65 20 63 68 61 6e 67 65 ock. The change
d4080 0a 20 20 2a 2a 20 66 72 6f 6d 20 53 48 41 52 45 . ** from SHARE
d4090 44 20 74 6f 20 52 45 53 45 52 56 45 44 20 6d 61 D to RESERVED ma
d40a0 72 6b 73 20 74 68 65 20 62 65 67 69 6e 6e 69 6e rks the beginnin
d40b0 67 20 6f 66 20 61 20 6e 6f 72 6d 61 6c 0a 20 20 g of a normal.
d40c0 2a 2a 20 77 72 69 74 65 20 6f 70 65 72 61 74 69 ** write operati
d40d0 6f 6e 20 28 6e 6f 74 20 61 20 68 6f 74 20 6a 6f on (not a hot jo
d40e0 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 29 2e urnal rollback).
d40f0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d . */. if( rc==
d4100 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 20 26 26 20 SQLITE_OK. &&
d4110 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c pFile->locktype<
d4120 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 20 20 20 =SHARED_LOCK.
d4130 26 26 20 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 && locktype==RES
d4140 45 52 56 45 44 5f 4c 4f 43 4b 0a 20 20 29 7b 0a ERVED_LOCK. ){.
d4150 20 20 20 20 70 46 69 6c 65 2d 3e 74 72 61 6e 73 pFile->trans
d4160 43 6e 74 72 43 68 6e 67 20 3d 20 30 3b 0a 20 20 CntrChng = 0;.
d4170 20 20 70 46 69 6c 65 2d 3e 64 62 55 70 64 61 74 pFile->dbUpdat
d4180 65 20 3d 20 30 3b 0a 20 20 20 20 70 46 69 6c 65 e = 0;. pFile
d4190 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 20 ->inNormalWrite
d41a0 3d 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a = 1;. }.#endif.
d41b0 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 .. if( rc==SQLI
d41c0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 46 69 TE_OK ){. pFi
d41d0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c le->locktype = l
d41e0 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 70 4c 6f ocktype;. pLo
d41f0 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c ck->locktype = l
d4200 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 65 6c 73 65 ocktype;. }else
d4210 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 if( locktype==E
d4220 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b XCLUSIVE_LOCK ){
d4230 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b . pFile->lock
d4240 74 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c type = PENDING_L
d4250 4f 43 4b 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e OCK;. pLock->
d4260 6c 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e 44 49 locktype = PENDI
d4270 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 7d 0a 0a 65 6e NG_LOCK;. }..en
d4280 64 5f 6c 6f 63 6b 3a 0a 20 20 75 6e 69 78 4c 65 d_lock:. unixLe
d4290 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f 53 aveMutex();. OS
d42a0 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 20 20 20 TRACE4("LOCK
d42b0 25 64 20 25 73 20 25 73 5c 6e 22 2c 20 70 46 69 %d %s %s\n", pFi
d42c0 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 4e le->h, locktypeN
d42d0 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 0a ame(locktype), .
d42e0 20 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 rc==SQLITE
d42f0 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61 _OK ? "ok" : "fa
d4300 69 6c 65 64 22 29 3b 0a 20 20 72 65 74 75 72 6e iled");. return
d4310 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c rc;.}../*.** Cl
d4320 6f 73 65 20 61 6c 6c 20 66 69 6c 65 20 64 65 73 ose all file des
d4330 63 72 69 70 74 6f 72 73 20 61 63 63 75 6d 75 61 criptors accumua
d4340 74 65 64 20 69 6e 20 74 68 65 20 75 6e 69 78 4f ted in the unixO
d4350 70 65 6e 43 6e 74 2d 3e 70 55 6e 75 73 65 64 20 penCnt->pUnused
d4360 6c 69 73 74 2e 0a 2a 2a 20 49 66 20 61 6c 6c 20 list..** If all
d4370 73 75 63 68 20 66 69 6c 65 20 64 65 73 63 72 69 such file descri
d4380 70 74 6f 72 73 20 61 72 65 20 63 6c 6f 73 65 64 ptors are closed
d4390 20 77 69 74 68 6f 75 74 20 65 72 72 6f 72 2c 20 without error,
d43a0 74 68 65 20 6c 69 73 74 20 69 73 0a 2a 2a 20 63 the list is.** c
d43b0 6c 65 61 72 65 64 20 61 6e 64 20 53 51 4c 49 54 leared and SQLIT
d43c0 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 0a 2a E_OK returned..*
d43d0 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 *.** Otherwise,
d43e0 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 if an error occu
d43f0 72 73 2c 20 74 68 65 6e 20 73 75 63 63 65 73 73 rs, then success
d4400 66 75 6c 6c 79 20 63 6c 6f 73 65 64 20 66 69 6c fully closed fil
d4410 65 20 64 65 73 63 72 69 70 74 6f 72 0a 2a 2a 20 e descriptor.**
d4420 65 6e 74 72 69 65 73 20 61 72 65 20 72 65 6d 6f entries are remo
d4430 76 65 64 20 66 72 6f 6d 20 74 68 65 20 6c 69 73 ved from the lis
d4440 74 2c 20 61 6e 64 20 53 51 4c 49 54 45 5f 49 4f t, and SQLITE_IO
d4450 45 52 52 5f 43 4c 4f 53 45 20 72 65 74 75 72 6e ERR_CLOSE return
d4460 65 64 2e 20 0a 2a 2a 20 6e 6f 74 20 64 65 6c 65 ed. .** not dele
d4470 74 65 64 20 61 6e 64 20 53 51 4c 49 54 45 5f 49 ted and SQLITE_I
d4480 4f 45 52 52 5f 43 4c 4f 53 45 20 72 65 74 75 72 OERR_CLOSE retur
d4490 6e 65 64 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 20 ned..*/ .static
d44a0 69 6e 74 20 63 6c 6f 73 65 50 65 6e 64 69 6e 67 int closePending
d44b0 46 64 73 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 Fds(unixFile *pF
d44c0 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d ile){. int rc =
d44d0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 74 SQLITE_OK;. st
d44e0 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 ruct unixOpenCnt
d44f0 20 2a 70 4f 70 65 6e 20 3d 20 70 46 69 6c 65 2d *pOpen = pFile-
d4500 3e 70 4f 70 65 6e 3b 0a 20 20 55 6e 69 78 55 6e >pOpen;. UnixUn
d4510 75 73 65 64 46 64 20 2a 70 45 72 72 6f 72 20 3d usedFd *pError =
d4520 20 30 3b 0a 20 20 55 6e 69 78 55 6e 75 73 65 64 0;. UnixUnused
d4530 46 64 20 2a 70 3b 0a 20 20 55 6e 69 78 55 6e 75 Fd *p;. UnixUnu
d4540 73 65 64 46 64 20 2a 70 4e 65 78 74 3b 0a 20 20 sedFd *pNext;.
d4550 66 6f 72 28 70 3d 70 4f 70 65 6e 2d 3e 70 55 6e for(p=pOpen->pUn
d4560 75 73 65 64 3b 20 70 3b 20 70 3d 70 4e 65 78 74 used; p; p=pNext
d4570 29 7b 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 70 ){. pNext = p
d4580 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 69 66 28 ->pNext;. if(
d4590 20 63 6c 6f 73 65 28 70 2d 3e 66 64 29 20 29 7b close(p->fd) ){
d45a0 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 . pFile->la
d45b0 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b stErrno = errno;
d45c0 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
d45d0 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a TE_IOERR_CLOSE;.
d45e0 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d p->pNext =
d45f0 20 70 45 72 72 6f 72 3b 0a 20 20 20 20 20 20 70 pError;. p
d4600 45 72 72 6f 72 20 3d 20 70 3b 0a 20 20 20 20 7d Error = p;. }
d4610 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 else{. sqli
d4620 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 20 te3_free(p);.
d4630 20 7d 0a 20 20 7d 0a 20 20 70 4f 70 65 6e 2d 3e }. }. pOpen->
d4640 70 55 6e 75 73 65 64 20 3d 20 70 45 72 72 6f 72 pUnused = pError
d4650 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
d4660 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20 ../*.** Add the
d4670 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
d4680 75 73 65 64 20 62 79 20 66 69 6c 65 20 68 61 6e used by file han
d4690 64 6c 65 20 70 46 69 6c 65 20 74 6f 20 74 68 65 dle pFile to the
d46a0 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a corresponding.*
d46b0 2a 20 70 55 6e 75 73 65 64 20 6c 69 73 74 2e 0a * pUnused list..
d46c0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 */.static void s
d46d0 65 74 50 65 6e 64 69 6e 67 46 64 28 75 6e 69 78 etPendingFd(unix
d46e0 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 File *pFile){.
d46f0 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 struct unixOpenC
d4700 6e 74 20 2a 70 4f 70 65 6e 20 3d 20 70 46 69 6c nt *pOpen = pFil
d4710 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 55 6e 69 78 e->pOpen;. Unix
d4720 55 6e 75 73 65 64 46 64 20 2a 70 20 3d 20 70 46 UnusedFd *p = pF
d4730 69 6c 65 2d 3e 70 55 6e 75 73 65 64 3b 0a 20 20 ile->pUnused;.
d4740 70 2d 3e 70 4e 65 78 74 20 3d 20 70 4f 70 65 6e p->pNext = pOpen
d4750 2d 3e 70 55 6e 75 73 65 64 3b 0a 20 20 70 4f 70 ->pUnused;. pOp
d4760 65 6e 2d 3e 70 55 6e 75 73 65 64 20 3d 20 70 3b en->pUnused = p;
d4770 0a 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 2d 31 . pFile->h = -1
d4780 3b 0a 20 20 70 46 69 6c 65 2d 3e 70 55 6e 75 73 ;. pFile->pUnus
d4790 65 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ed = 0;.}../*.**
d47a0 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 Lower the locki
d47b0 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 ng level on file
d47c0 20 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c descriptor pFil
d47d0 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 e to locktype.
d47e0 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 locktype.** must
d47f0 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f be either NO_LO
d4800 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 CK or SHARED_LOC
d4810 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 K..**.** If the
d4820 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 locking level of
d4830 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 the file descri
d4840 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 ptor is already
d4850 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 at or below.** t
d4860 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 he requested loc
d4870 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 king level, this
d4880 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f routine is a no
d4890 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 -op..*/.static i
d48a0 6e 74 20 75 6e 69 78 55 6e 6c 6f 63 6b 28 73 71 nt unixUnlock(sq
d48b0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
d48c0 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 int locktype){.
d48d0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
d48e0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
d48f0 3b 20 2f 2a 20 54 68 65 20 6f 70 65 6e 20 66 69 ; /* The open fi
d4900 6c 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 le */. struct u
d4910 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f nixLockInfo *pLo
d4920 63 6b 3b 20 20 20 20 20 20 2f 2a 20 53 74 72 75 ck; /* Stru
d4930 63 74 75 72 65 20 64 65 73 63 72 69 62 69 6e 67 cture describing
d4940 20 63 75 72 72 65 6e 74 20 6c 6f 63 6b 20 73 74 current lock st
d4950 61 74 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 ate */. struct
d4960 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 20 20 20 20 20 flock lock;
d4970 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 /* Inf
d4980 6f 72 6d 61 74 69 6f 6e 20 70 61 73 73 65 64 20 ormation passed
d4990 69 6e 74 6f 20 66 63 6e 74 6c 28 29 20 2a 2f 0a into fcntl() */.
d49a0 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
d49b0 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 E_OK;
d49c0 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 /* Return cod
d49d0 65 20 66 72 6f 6d 20 74 68 69 73 20 69 6e 74 65 e from this inte
d49e0 72 66 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 68 rface */. int h
d49f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
d4a00 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
d4a10 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 66 69 he underlying fi
d4a20 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f le descriptor */
d4a30 0a 20 20 69 6e 74 20 74 45 72 72 6e 6f 3b 20 20 . int tErrno;
d4a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d4a50 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 63 6f 64 /* Error cod
d4a60 65 20 66 72 6f 6d 20 73 79 73 74 65 6d 20 63 61 e from system ca
d4a70 6c 6c 20 65 72 72 6f 72 73 20 2a 2f 0a 0a 20 20 ll errors */..
d4a80 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b assert( pFile );
d4a90 0a 20 20 4f 53 54 52 41 43 45 37 28 22 55 4e 4c . OSTRACE7("UNL
d4aa0 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73 20 25 OCK %d %d was %
d4ab0 64 28 25 64 2c 25 64 29 20 70 69 64 3d 25 64 5c d(%d,%d) pid=%d\
d4ac0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f n", pFile->h, lo
d4ad0 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 70 46 cktype,. pF
d4ae0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70 ile->locktype, p
d4af0 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63 File->pLock->loc
d4b00 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 70 4c ktype, pFile->pL
d4b10 6f 63 6b 2d 3e 63 6e 74 2c 20 67 65 74 70 69 64 ock->cnt, getpid
d4b20 28 29 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 ());.. assert(
d4b30 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 locktype<=SHARED
d4b40 5f 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 28 20 70 _LOCK );. if( p
d4b50 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 3d File->locktype<=
d4b60 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 locktype ){.
d4b70 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
d4b80 3b 0a 20 20 7d 0a 20 20 69 66 28 20 43 48 45 43 ;. }. if( CHEC
d4b90 4b 5f 54 48 52 45 41 44 49 44 28 70 46 69 6c 65 K_THREADID(pFile
d4ba0 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ) ){. return
d4bb0 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 SQLITE_MISUSE;.
d4bc0 20 7d 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 }. unixEnterMu
d4bd0 74 65 78 28 29 3b 0a 20 20 68 20 3d 20 70 46 69 tex();. h = pFi
d4be0 6c 65 2d 3e 68 3b 0a 20 20 70 4c 6f 63 6b 20 3d le->h;. pLock =
d4bf0 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 3b 0a 20 pFile->pLock;.
d4c00 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e assert( pLock->
d4c10 63 6e 74 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 cnt!=0 );. if(
d4c20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e pFile->locktype>
d4c30 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 SHARED_LOCK ){.
d4c40 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b assert( pLock
d4c50 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 70 46 69 6c ->locktype==pFil
d4c60 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 e->locktype );.
d4c70 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 SimulateIOErr
d4c80 6f 72 42 65 6e 69 67 6e 28 31 29 3b 0a 20 20 20 orBenign(1);.
d4c90 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
d4ca0 28 20 68 3d 28 2d 31 29 20 29 0a 20 20 20 20 53 ( h=(-1) ). S
d4cb0 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 imulateIOErrorBe
d4cc0 6e 69 67 6e 28 30 29 3b 0a 0a 23 69 66 6e 64 65 nign(0);..#ifnde
d4cd0 66 20 4e 44 45 42 55 47 0a 20 20 20 20 2f 2a 20 f NDEBUG. /*
d4ce0 57 68 65 6e 20 72 65 64 75 63 69 6e 67 20 61 20 When reducing a
d4cf0 6c 6f 63 6b 20 73 75 63 68 20 74 68 61 74 20 6f lock such that o
d4d00 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 63 ther processes c
d4d10 61 6e 20 73 74 61 72 74 0a 20 20 20 20 2a 2a 20 an start. **
d4d20 72 65 61 64 69 6e 67 20 74 68 65 20 64 61 74 61 reading the data
d4d30 62 61 73 65 20 66 69 6c 65 20 61 67 61 69 6e 2c base file again,
d4d40 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 make sure that
d4d50 74 68 65 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 the. ** trans
d4d60 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 77 action counter w
d4d70 61 73 20 75 70 64 61 74 65 64 20 69 66 20 61 6e as updated if an
d4d80 79 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 y part of the da
d4d90 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 66 69 tabase. ** fi
d4da0 6c 65 20 63 68 61 6e 67 65 64 2e 20 20 49 66 20 le changed. If
d4db0 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
d4dc0 63 6f 75 6e 74 65 72 20 69 73 20 6e 6f 74 20 75 counter is not u
d4dd0 70 64 61 74 65 64 2c 0a 20 20 20 20 2a 2a 20 6f pdated,. ** o
d4de0 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 ther connections
d4df0 20 74 6f 20 74 68 65 20 73 61 6d 65 20 66 69 6c to the same fil
d4e00 65 20 6d 69 67 68 74 20 6e 6f 74 20 72 65 61 6c e might not real
d4e10 69 7a 65 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 ize that. **
d4e20 74 68 65 20 66 69 6c 65 20 68 61 73 20 63 68 61 the file has cha
d4e30 6e 67 65 64 20 61 6e 64 20 68 65 6e 63 65 20 6d nged and hence m
d4e40 69 67 68 74 20 6e 6f 74 20 6b 6e 6f 77 20 74 6f ight not know to
d4e50 20 66 6c 75 73 68 20 74 68 65 69 72 0a 20 20 20 flush their.
d4e60 20 2a 2a 20 63 61 63 68 65 2e 20 20 54 68 65 20 ** cache. The
d4e70 75 73 65 20 6f 66 20 61 20 73 74 61 6c 65 20 63 use of a stale c
d4e80 61 63 68 65 20 63 61 6e 20 6c 65 61 64 20 74 6f ache can lead to
d4e90 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 database corrup
d4ea0 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 tion.. */.
d4eb0 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e assert( pFile->
d4ec0 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 3d 3d 30 inNormalWrite==0
d4ed0 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 70 46 69 . || pFi
d4ee0 6c 65 2d 3e 64 62 55 70 64 61 74 65 3d 3d 30 0a le->dbUpdate==0.
d4ef0 20 20 20 20 20 20 20 20 20 7c 7c 20 70 46 69 6c || pFil
d4f00 65 2d 3e 74 72 61 6e 73 43 6e 74 72 43 68 6e 67 e->transCntrChng
d4f10 3d 3d 31 20 29 3b 0a 20 20 20 20 70 46 69 6c 65 ==1 );. pFile
d4f20 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 20 ->inNormalWrite
d4f30 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 20 20 = 0;.#endif...
d4f40 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d if( locktype==
d4f50 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 SHARED_LOCK ){.
d4f60 20 20 20 20 20 69 66 28 20 72 61 6e 67 65 4c 6f if( rangeLo
d4f70 63 6b 28 70 46 69 6c 65 2c 20 46 5f 52 44 4c 43 ck(pFile, F_RDLC
d4f80 4b 2c 20 26 74 45 72 72 6e 6f 29 3d 3d 28 2d 31 K, &tErrno)==(-1
d4f90 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 ) ){. rc
d4fa0 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f = sqliteErrorFro
d4fb0 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 mPosixError(tErr
d4fc0 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 no, SQLITE_IOERR
d4fd0 5f 52 44 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 _RDLOCK);.
d4fe0 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 if( IS_LOCK_ER
d4ff0 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 ROR(rc) ){.
d5000 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 pFile->last
d5010 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a Errno = tErrno;.
d5020 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
d5030 20 20 67 6f 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63 goto end_unloc
d5040 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d k;. }. }
d5050 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 . lock.l_type
d5060 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 = F_UNLCK;.
d5070 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 lock.l_whence =
d5080 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20 6c 6f SEEK_SET;. lo
d5090 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 50 45 4e ck.l_start = PEN
d50a0 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 6c DING_BYTE;. l
d50b0 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 32 4c 3b 20 ock.l_len = 2L;
d50c0 20 61 73 73 65 72 74 28 20 50 45 4e 44 49 4e 47 assert( PENDING
d50d0 5f 42 59 54 45 2b 31 3d 3d 52 45 53 45 52 56 45 _BYTE+1==RESERVE
d50e0 44 5f 42 59 54 45 20 29 3b 0a 20 20 20 20 69 66 D_BYTE );. if
d50f0 28 20 66 63 6e 74 6c 28 68 2c 20 46 5f 53 45 54 ( fcntl(h, F_SET
d5100 4c 4b 2c 20 26 6c 6f 63 6b 29 21 3d 28 2d 31 29 LK, &lock)!=(-1)
d5110 20 29 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d ){. pLock-
d5120 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 >locktype = SHAR
d5130 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c ED_LOCK;. }el
d5140 73 65 7b 0a 20 20 20 20 20 20 74 45 72 72 6e 6f se{. tErrno
d5150 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 = errno;.
d5160 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 rc = sqliteError
d5170 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 FromPosixError(t
d5180 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f Errno, SQLITE_IO
d5190 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 20 20 ERR_UNLOCK);.
d51a0 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 if( IS_LOCK_E
d51b0 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 RROR(rc) ){.
d51c0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 pFile->lastE
d51d0 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 rrno = tErrno;.
d51e0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 }. got
d51f0 6f 20 65 6e 64 5f 75 6e 6c 6f 63 6b 3b 0a 20 20 o end_unlock;.
d5200 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6c 6f }. }. if( lo
d5210 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 cktype==NO_LOCK
d5220 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 75 6e ){. struct un
d5230 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e ixOpenCnt *pOpen
d5240 3b 0a 0a 20 20 20 20 2f 2a 20 44 65 63 72 65 6d ;.. /* Decrem
d5250 65 6e 74 20 74 68 65 20 73 68 61 72 65 64 20 6c ent the shared l
d5260 6f 63 6b 20 63 6f 75 6e 74 65 72 2e 20 20 52 65 ock counter. Re
d5270 6c 65 61 73 65 20 74 68 65 20 6c 6f 63 6b 20 75 lease the lock u
d5280 73 69 6e 67 20 61 6e 0a 20 20 20 20 2a 2a 20 4f sing an. ** O
d5290 53 20 63 61 6c 6c 20 6f 6e 6c 79 20 77 68 65 6e S call only when
d52a0 20 61 6c 6c 20 74 68 72 65 61 64 73 20 69 6e 20 all threads in
d52b0 74 68 69 73 20 73 61 6d 65 20 70 72 6f 63 65 73 this same proces
d52c0 73 20 68 61 76 65 20 72 65 6c 65 61 73 65 64 0a s have released.
d52d0 20 20 20 20 2a 2a 20 74 68 65 20 6c 6f 63 6b 2e ** the lock.
d52e0 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 4c 6f 63 . */. pLoc
d52f0 6b 2d 3e 63 6e 74 2d 2d 3b 0a 20 20 20 20 69 66 k->cnt--;. if
d5300 28 20 70 4c 6f 63 6b 2d 3e 63 6e 74 3d 3d 30 20 ( pLock->cnt==0
d5310 29 7b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f ){. lock.l_
d5320 74 79 70 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a type = F_UNLCK;.
d5330 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 lock.l_whe
d5340 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a nce = SEEK_SET;.
d5350 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 lock.l_sta
d5360 72 74 20 3d 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 rt = lock.l_len
d5370 3d 20 30 4c 3b 0a 20 20 20 20 20 20 53 69 6d 75 = 0L;. Simu
d5380 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 lateIOErrorBenig
d5390 6e 28 31 29 3b 0a 20 20 20 20 20 20 53 69 6d 75 n(1);. Simu
d53a0 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 68 3d 28 lateIOError( h=(
d53b0 2d 31 29 20 29 0a 20 20 20 20 20 20 53 69 6d 75 -1) ). Simu
d53c0 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 lateIOErrorBenig
d53d0 6e 28 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 n(0);. if(
d53e0 66 63 6e 74 6c 28 68 2c 20 46 5f 53 45 54 4c 4b fcntl(h, F_SETLK
d53f0 2c 20 26 6c 6f 63 6b 29 21 3d 28 2d 31 29 20 29 , &lock)!=(-1) )
d5400 7b 0a 20 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d {. pLock-
d5410 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c >locktype = NO_L
d5420 4f 43 4b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 OCK;. }else
d5430 7b 0a 20 20 20 20 20 20 20 20 74 45 72 72 6e 6f {. tErrno
d5440 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 = errno;.
d5450 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 rc = sqliteErr
d5460 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 orFromPosixError
d5470 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f (tErrno, SQLITE_
d5480 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 IOERR_UNLOCK);.
d5490 20 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f if( IS_LO
d54a0 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a CK_ERROR(rc) ){.
d54b0 20 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d pFile-
d54c0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 >lastErrno = tEr
d54d0 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 rno;. }.
d54e0 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f pLock->lo
d54f0 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b cktype = NO_LOCK
d5500 3b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d ;. pFile-
d5510 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c >locktype = NO_L
d5520 4f 43 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 OCK;. }.
d5530 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 63 72 65 }.. /* Decre
d5540 6d 65 6e 74 20 74 68 65 20 63 6f 75 6e 74 20 6f ment the count o
d5550 66 20 6c 6f 63 6b 73 20 61 67 61 69 6e 73 74 20 f locks against
d5560 74 68 69 73 20 73 61 6d 65 20 66 69 6c 65 2e 20 this same file.
d5570 20 57 68 65 6e 20 74 68 65 0a 20 20 20 20 2a 2a When the. **
d5580 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 7a count reaches z
d5590 65 72 6f 2c 20 63 6c 6f 73 65 20 61 6e 79 20 6f ero, close any o
d55a0 74 68 65 72 20 66 69 6c 65 20 64 65 73 63 72 69 ther file descri
d55b0 70 74 6f 72 73 20 77 68 6f 73 65 20 63 6c 6f 73 ptors whose clos
d55c0 65 0a 20 20 20 20 2a 2a 20 77 61 73 20 64 65 66 e. ** was def
d55d0 65 72 72 65 64 20 62 65 63 61 75 73 65 20 6f 66 erred because of
d55e0 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63 outstanding loc
d55f0 6b 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 ks.. */. p
d5600 4f 70 65 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 4f Open = pFile->pO
d5610 70 65 6e 3b 0a 20 20 20 20 70 4f 70 65 6e 2d 3e pen;. pOpen->
d5620 6e 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 61 73 73 nLock--;. ass
d5630 65 72 74 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 ert( pOpen->nLoc
d5640 6b 3e 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 k>=0 );. if(
d5650 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3d 3d 30 20 pOpen->nLock==0
d5660 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 32 ){. int rc2
d5670 20 3d 20 63 6c 6f 73 65 50 65 6e 64 69 6e 67 46 = closePendingF
d5680 64 73 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 20 ds(pFile);.
d5690 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
d56a0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 OK ){. rc
d56b0 20 3d 20 72 63 32 3b 0a 20 20 20 20 20 20 7d 0a = rc2;. }.
d56c0 20 20 20 20 7d 0a 20 20 7d 0a 09 0a 65 6e 64 5f }. }...end_
d56d0 75 6e 6c 6f 63 6b 3a 0a 20 20 75 6e 69 78 4c 65 unlock:. unixLe
d56e0 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 aveMutex();. if
d56f0 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
d5700 29 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 ) pFile->locktyp
d5710 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 e = locktype;.
d5720 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
d5730 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
d5740 6e 20 70 65 72 66 6f 72 6d 73 20 74 68 65 20 70 n performs the p
d5750 61 72 74 73 20 6f 66 20 74 68 65 20 22 63 6c 6f arts of the "clo
d5760 73 65 20 66 69 6c 65 22 20 6f 70 65 72 61 74 69 se file" operati
d5770 6f 6e 20 0a 2a 2a 20 63 6f 6d 6d 6f 6e 20 74 6f on .** common to
d5780 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 73 63 68 all locking sch
d5790 65 6d 65 73 2e 20 49 74 20 63 6c 6f 73 65 73 20 emes. It closes
d57a0 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 61 6e the directory an
d57b0 64 20 66 69 6c 65 0a 2a 2a 20 68 61 6e 64 6c 65 d file.** handle
d57c0 73 2c 20 69 66 20 74 68 65 79 20 61 72 65 20 76 s, if they are v
d57d0 61 6c 69 64 2c 20 61 6e 64 20 73 65 74 73 20 61 alid, and sets a
d57e0 6c 6c 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 ll fields of the
d57f0 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 73 74 72 unixFile.** str
d5800 75 63 74 75 72 65 20 74 6f 20 30 2e 0a 2a 2a 0a ucture to 0..**.
d5810 2a 2a 20 49 74 20 69 73 20 2a 6e 6f 74 2a 20 6e ** It is *not* n
d5820 65 63 65 73 73 61 72 79 20 74 6f 20 68 6f 6c 64 ecessary to hold
d5830 20 74 68 65 20 6d 75 74 65 78 20 77 68 65 6e 20 the mutex when
d5840 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
d5850 63 61 6c 6c 65 64 2c 0a 2a 2a 20 65 76 65 6e 20 called,.** even
d5860 6f 6e 20 56 78 57 6f 72 6b 73 2e 20 20 41 20 6d on VxWorks. A m
d5870 75 74 65 78 20 77 69 6c 6c 20 62 65 20 61 63 71 utex will be acq
d5880 75 69 72 65 64 20 6f 6e 20 56 78 57 6f 72 6b 73 uired on VxWorks
d5890 20 62 79 20 74 68 65 0a 2a 2a 20 76 78 77 6f 72 by the.** vxwor
d58a0 6b 73 52 65 6c 65 61 73 65 46 69 6c 65 49 64 28 ksReleaseFileId(
d58b0 29 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 73 74 ) routine..*/.st
d58c0 61 74 69 63 20 69 6e 74 20 63 6c 6f 73 65 55 6e atic int closeUn
d58d0 69 78 46 69 6c 65 28 73 71 6c 69 74 65 33 5f 66 ixFile(sqlite3_f
d58e0 69 6c 65 20 2a 69 64 29 7b 0a 20 20 75 6e 69 78 ile *id){. unix
d58f0 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 File *pFile = (u
d5900 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 nixFile*)id;. i
d5910 66 28 20 70 46 69 6c 65 20 29 7b 0a 20 20 20 20 f( pFile ){.
d5920 69 66 28 20 70 46 69 6c 65 2d 3e 64 69 72 66 64 if( pFile->dirfd
d5930 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 >=0 ){. int
d5940 20 65 72 72 20 3d 20 63 6c 6f 73 65 28 70 46 69 err = close(pFi
d5950 6c 65 2d 3e 64 69 72 66 64 29 3b 0a 20 20 20 20 le->dirfd);.
d5960 20 20 69 66 28 20 65 72 72 20 29 7b 0a 20 20 20 if( err ){.
d5970 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 pFile->last
d5980 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
d5990 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 return SQ
d59a0 4c 49 54 45 5f 49 4f 45 52 52 5f 44 49 52 5f 43 LITE_IOERR_DIR_C
d59b0 4c 4f 53 45 3b 0a 20 20 20 20 20 20 7d 65 6c 73 LOSE;. }els
d59c0 65 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 e{. pFile
d59d0 2d 3e 64 69 72 66 64 3d 2d 31 3b 0a 20 20 20 20 ->dirfd=-1;.
d59e0 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 }. }. if
d59f0 28 20 70 46 69 6c 65 2d 3e 68 3e 3d 30 20 29 7b ( pFile->h>=0 ){
d5a00 0a 20 20 20 20 20 20 69 6e 74 20 65 72 72 20 3d . int err =
d5a10 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 68 29 close(pFile->h)
d5a20 3b 0a 20 20 20 20 20 20 69 66 28 20 65 72 72 20 ;. if( err
d5a30 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 ){. pFile
d5a40 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 ->lastErrno = er
d5a50 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72 65 74 rno;. ret
d5a60 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
d5a70 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 20 20 7d 0a _CLOSE;. }.
d5a80 20 20 20 20 7d 0a 23 69 66 20 4f 53 5f 56 58 57 }.#if OS_VXW
d5a90 4f 52 4b 53 0a 20 20 20 20 69 66 28 20 70 46 69 ORKS. if( pFi
d5aa0 6c 65 2d 3e 70 49 64 20 29 7b 0a 20 20 20 20 20 le->pId ){.
d5ab0 20 69 66 28 20 70 46 69 6c 65 2d 3e 69 73 44 65 if( pFile->isDe
d5ac0 6c 65 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 lete ){.
d5ad0 75 6e 6c 69 6e 6b 28 70 46 69 6c 65 2d 3e 70 49 unlink(pFile->pI
d5ae0 64 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d d->zCanonicalNam
d5af0 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 e);. }.
d5b00 20 20 76 78 77 6f 72 6b 73 52 65 6c 65 61 73 65 vxworksRelease
d5b10 46 69 6c 65 49 64 28 70 46 69 6c 65 2d 3e 70 49 FileId(pFile->pI
d5b20 64 29 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d d);. pFile-
d5b30 3e 70 49 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a >pId = 0;. }.
d5b40 23 65 6e 64 69 66 0a 20 20 20 20 4f 53 54 52 41 #endif. OSTRA
d5b50 43 45 32 28 22 43 4c 4f 53 45 20 20 20 25 2d 33 CE2("CLOSE %-3
d5b60 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b d\n", pFile->h);
d5b70 0a 20 20 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 . OpenCounter
d5b80 28 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 (-1);. sqlite
d5b90 33 5f 66 72 65 65 28 70 46 69 6c 65 2d 3e 70 55 3_free(pFile->pU
d5ba0 6e 75 73 65 64 29 3b 0a 20 20 20 20 6d 65 6d 73 nused);. mems
d5bb0 65 74 28 70 46 69 6c 65 2c 20 30 2c 20 73 69 7a et(pFile, 0, siz
d5bc0 65 6f 66 28 75 6e 69 78 46 69 6c 65 29 29 3b 0a eof(unixFile));.
d5bd0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
d5be0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
d5bf0 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a 2a Close a file..*
d5c00 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 /.static int uni
d5c10 78 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 xClose(sqlite3_f
d5c20 69 6c 65 20 2a 69 64 29 7b 0a 20 20 69 6e 74 20 ile *id){. int
d5c30 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
d5c40 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20 20 20 if( id ){.
d5c50 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 unixFile *pFile
d5c60 3d 20 28 75 6e 69 78 46 69 6c 65 20 2a 29 69 64 = (unixFile *)id
d5c70 3b 0a 20 20 20 20 75 6e 69 78 55 6e 6c 6f 63 6b ;. unixUnlock
d5c80 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 (id, NO_LOCK);.
d5c90 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 unixEnterMute
d5ca0 78 28 29 3b 0a 20 20 20 20 69 66 28 20 70 46 69 x();. if( pFi
d5cb0 6c 65 2d 3e 70 4f 70 65 6e 20 26 26 20 70 46 69 le->pOpen && pFi
d5cc0 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b le->pOpen->nLock
d5cd0 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 ){. /* If
d5ce0 74 68 65 72 65 20 61 72 65 20 6f 75 74 73 74 61 there are outsta
d5cf0 6e 64 69 6e 67 20 6c 6f 63 6b 73 2c 20 64 6f 20 nding locks, do
d5d00 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 63 6c 6f not actually clo
d5d10 73 65 20 74 68 65 20 66 69 6c 65 20 6a 75 73 74 se the file just
d5d20 0a 20 20 20 20 20 20 2a 2a 20 79 65 74 20 62 65 . ** yet be
d5d30 63 61 75 73 65 20 74 68 61 74 20 77 6f 75 6c 64 cause that would
d5d40 20 63 6c 65 61 72 20 74 68 6f 73 65 20 6c 6f 63 clear those loc
d5d50 6b 73 2e 20 20 49 6e 73 74 65 61 64 2c 20 61 64 ks. Instead, ad
d5d60 64 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 20 d the file.
d5d70 20 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20 74 ** descriptor t
d5d80 6f 20 70 4f 70 65 6e 2d 3e 70 55 6e 75 73 65 64 o pOpen->pUnused
d5d90 20 6c 69 73 74 2e 20 20 49 74 20 77 69 6c 6c 20 list. It will
d5da0 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 be automatically
d5db0 20 63 6c 6f 73 65 64 20 0a 20 20 20 20 20 20 2a closed . *
d5dc0 2a 20 77 68 65 6e 20 74 68 65 20 6c 61 73 74 20 * when the last
d5dd0 6c 6f 63 6b 20 69 73 20 63 6c 65 61 72 65 64 2e lock is cleared.
d5de0 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
d5df0 73 65 74 50 65 6e 64 69 6e 67 46 64 28 70 46 69 setPendingFd(pFi
d5e00 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 le);. }. r
d5e10 65 6c 65 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 eleaseLockInfo(p
d5e20 46 69 6c 65 2d 3e 70 4c 6f 63 6b 29 3b 0a 20 20 File->pLock);.
d5e30 20 20 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74 releaseOpenCnt
d5e40 28 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 29 3b 0a (pFile->pOpen);.
d5e50 20 20 20 20 72 63 20 3d 20 63 6c 6f 73 65 55 6e rc = closeUn
d5e60 69 78 46 69 6c 65 28 69 64 29 3b 0a 20 20 20 20 ixFile(id);.
d5e70 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 unixLeaveMutex()
d5e80 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
d5e90 63 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a c;.}../*********
d5ea0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 ***** End of the
d5eb0 20 70 6f 73 69 78 20 61 64 76 69 73 6f 72 79 20 posix advisory
d5ec0 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 lock implementat
d5ed0 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ion ************
d5ee0 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *****.**********
d5ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5f30 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a ****/../********
d5f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5f60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5f80 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ******.*********
d5f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5fa0 2a 2a 2a 2a 2a 20 4e 6f 2d 6f 70 20 4c 6f 63 6b ***** No-op Lock
d5fb0 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ing ************
d5fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5fd0 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4f 66 20 ******.**.** Of
d5fe0 74 68 65 20 76 61 72 69 6f 75 73 20 6c 6f 63 6b the various lock
d5ff0 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ing implementati
d6000 6f 6e 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 ons available, t
d6010 68 69 73 20 69 73 20 62 79 20 66 61 72 20 74 68 his is by far th
d6020 65 0a 2a 2a 20 73 69 6d 70 6c 65 73 74 3a 20 20 e.** simplest:
d6030 6c 6f 63 6b 69 6e 67 20 69 73 20 69 67 6e 6f 72 locking is ignor
d6040 65 64 2e 20 20 4e 6f 20 61 74 74 65 6d 70 74 20 ed. No attempt
d6050 69 73 20 6d 61 64 65 20 74 6f 20 6c 6f 63 6b 20 is made to lock
d6060 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 the database.**
d6070 66 69 6c 65 20 66 6f 72 20 72 65 61 64 69 6e 67 file for reading
d6080 20 6f 72 20 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a or writing..**.
d6090 2a 2a 20 54 68 69 73 20 6c 6f 63 6b 69 6e 67 20 ** This locking
d60a0 6d 6f 64 65 20 69 73 20 61 70 70 72 6f 70 72 69 mode is appropri
d60b0 61 74 65 20 66 6f 72 20 75 73 65 20 6f 6e 20 72 ate for use on r
d60c0 65 61 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61 73 ead-only databas
d60d0 65 73 0a 2a 2a 20 28 65 78 3a 20 64 61 74 61 62 es.** (ex: datab
d60e0 61 73 65 73 20 74 68 61 74 20 61 72 65 20 62 75 ases that are bu
d60f0 72 6e 65 64 20 69 6e 74 6f 20 43 44 2d 52 4f 4d rned into CD-ROM
d6100 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2e 29 20 , for example.)
d6110 20 49 74 20 63 61 6e 0a 2a 2a 20 61 6c 73 6f 20 It can.** also
d6120 62 65 20 75 73 65 64 20 69 66 20 74 68 65 20 61 be used if the a
d6130 70 70 6c 69 63 61 74 69 6f 6e 20 65 6d 70 6c 6f pplication emplo
d6140 79 73 20 73 6f 6d 65 20 65 78 74 65 72 6e 61 6c ys some external
d6150 20 6d 65 63 68 61 6e 69 73 6d 20 74 6f 0a 2a 2a mechanism to.**
d6160 20 70 72 65 76 65 6e 74 20 73 69 6d 75 6c 74 61 prevent simulta
d6170 6e 65 6f 75 73 20 61 63 63 65 73 73 20 6f 66 20 neous access of
d6180 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 the same databas
d6190 65 20 62 79 20 74 77 6f 20 6f 72 20 6d 6f 72 65 e by two or more
d61a0 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e .** database con
d61b0 6e 65 63 74 69 6f 6e 73 2e 20 20 42 75 74 20 74 nections. But t
d61c0 68 65 72 65 20 69 73 20 61 20 73 65 72 69 6f 75 here is a seriou
d61d0 73 20 72 69 73 6b 20 6f 66 20 64 61 74 61 62 61 s risk of databa
d61e0 73 65 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e se.** corruption
d61f0 20 69 66 20 74 68 69 73 20 6c 6f 63 6b 69 6e 67 if this locking
d6200 20 6d 6f 64 65 20 69 73 20 75 73 65 64 20 69 6e mode is used in
d6210 20 73 69 74 75 61 74 69 6f 6e 73 20 77 68 65 72 situations wher
d6220 65 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 64 61 e multiple.** da
d6230 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
d6240 6e 73 20 61 72 65 20 61 63 63 65 73 73 69 6e 67 ns are accessing
d6250 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 the same databa
d6260 73 65 20 66 69 6c 65 20 61 74 20 74 68 65 20 73 se file at the s
d6270 61 6d 65 0a 2a 2a 20 74 69 6d 65 20 61 6e 64 20 ame.** time and
d6280 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 one or more of t
d6290 68 6f 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 hose connections
d62a0 20 61 72 65 20 77 72 69 74 69 6e 67 2e 0a 2a 2f are writing..*/
d62b0 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6c ..static int nol
d62c0 6f 63 6b 43 68 65 63 6b 52 65 73 65 72 76 65 64 ockCheckReserved
d62d0 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c Lock(sqlite3_fil
d62e0 65 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 e *NotUsed, int
d62f0 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 55 4e 55 *pResOut){. UNU
d6300 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f SED_PARAMETER(No
d6310 74 55 73 65 64 29 3b 0a 20 20 2a 70 52 65 73 4f tUsed);. *pResO
d6320 75 74 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e ut = 0;. return
d6330 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 73 74 SQLITE_OK;.}.st
d6340 61 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b 4c atic int nolockL
d6350 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ock(sqlite3_file
d6360 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 4e *NotUsed, int N
d6370 6f 74 55 73 65 64 32 29 7b 0a 20 20 55 4e 55 53 otUsed2){. UNUS
d6380 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f ED_PARAMETER2(No
d6390 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 tUsed, NotUsed2)
d63a0 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
d63b0 45 5f 4f 4b 3b 0a 7d 0a 73 74 61 74 69 63 20 69 E_OK;.}.static i
d63c0 6e 74 20 6e 6f 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 nt nolockUnlock(
d63d0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f sqlite3_file *No
d63e0 74 55 73 65 64 2c 20 69 6e 74 20 4e 6f 74 55 73 tUsed, int NotUs
d63f0 65 64 32 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 ed2){. UNUSED_P
d6400 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 ARAMETER2(NotUse
d6410 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 d, NotUsed2);.
d6420 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
d6430 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 ;.}../*.** Close
d6440 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 the file..*/.st
d6450 61 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b 43 atic int nolockC
d6460 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c lose(sqlite3_fil
d6470 65 20 2a 69 64 29 20 7b 0a 20 20 72 65 74 75 72 e *id) {. retur
d6480 6e 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 n closeUnixFile(
d6490 69 64 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a id);.}../*******
d64a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
d64b0 20 6f 66 20 74 68 65 20 6e 6f 2d 6f 70 20 6c 6f of the no-op lo
d64c0 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ck implementatio
d64d0 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n **************
d64e0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a *******.********
d64f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6530 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a ******/../******
d6540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6580 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a ********.*******
d6590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d65a0 2a 2a 20 42 65 67 69 6e 20 64 6f 74 2d 66 69 6c ** Begin dot-fil
d65b0 65 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a e Locking ******
d65c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d65d0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 ********.**.** T
d65e0 68 65 20 64 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 he dotfile locki
d65f0 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ng implementatio
d6600 6e 20 75 73 65 73 20 74 68 65 20 65 78 69 73 74 n uses the exist
d6610 61 6e 63 65 20 6f 66 20 73 65 70 61 72 61 74 65 ance of separate
d6620 20 6c 6f 63 6b 0a 2a 2a 20 66 69 6c 65 73 20 69 lock.** files i
d6630 6e 20 6f 72 64 65 72 20 74 6f 20 63 6f 6e 74 72 n order to contr
d6640 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 ol access to the
d6650 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 database. This
d6660 20 77 6f 72 6b 73 20 6f 6e 20 6a 75 73 74 0a 2a works on just.*
d6670 2a 20 61 62 6f 75 74 20 65 76 65 72 79 20 66 69 * about every fi
d6680 6c 65 73 79 73 74 65 6d 20 69 6d 61 67 69 6e 61 lesystem imagina
d6690 62 6c 65 2e 20 20 42 75 74 20 74 68 65 72 65 20 ble. But there
d66a0 61 72 65 20 73 65 72 69 6f 75 73 20 64 6f 77 6e are serious down
d66b0 73 69 64 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 sides:.**.**
d66c0 28 31 29 20 20 54 68 65 72 65 20 69 73 20 7a 65 (1) There is ze
d66d0 72 6f 20 63 6f 6e 63 75 72 72 65 6e 63 79 2e 20 ro concurrency.
d66e0 20 41 20 73 69 6e 67 6c 65 20 72 65 61 64 65 72 A single reader
d66f0 20 62 6c 6f 63 6b 73 20 61 6c 6c 20 6f 74 68 65 blocks all othe
d6700 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 63 6f 6e r.** con
d6710 6e 65 63 74 69 6f 6e 73 20 66 72 6f 6d 20 72 65 nections from re
d6720 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 ading or writing
d6730 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a the database..*
d6740 2a 0a 2a 2a 20 20 20 20 28 32 29 20 20 41 6e 20 *.** (2) An
d6750 61 70 70 6c 69 63 61 74 69 6f 6e 20 63 72 61 73 application cras
d6760 68 20 6f 72 20 70 6f 77 65 72 20 6c 6f 73 73 20 h or power loss
d6770 63 61 6e 20 6c 65 61 76 65 20 73 74 61 6c 65 20 can leave stale
d6780 6c 6f 63 6b 20 66 69 6c 65 73 0a 2a 2a 20 20 20 lock files.**
d6790 20 20 20 20 20 20 73 69 74 74 69 6e 67 20 61 72 sitting ar
d67a0 6f 75 6e 64 20 74 68 61 74 20 6e 65 65 64 20 74 ound that need t
d67b0 6f 20 62 65 20 63 6c 65 61 72 65 64 20 6d 61 6e o be cleared man
d67c0 75 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 4e 65 76 ually..**.** Nev
d67d0 65 72 74 68 65 6c 65 73 73 2c 20 61 20 64 6f 74 ertheless, a dot
d67e0 6c 6f 63 6b 20 69 73 20 61 6e 20 61 70 70 72 6f lock is an appro
d67f0 70 72 69 61 74 65 20 6c 6f 63 6b 69 6e 67 20 6d priate locking m
d6800 6f 64 65 20 66 6f 72 20 75 73 65 20 69 66 20 6e ode for use if n
d6810 6f 0a 2a 2a 20 6f 74 68 65 72 20 6c 6f 63 6b 69 o.** other locki
d6820 6e 67 20 73 74 72 61 74 65 67 79 20 69 73 20 61 ng strategy is a
d6830 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 vailable..**.**
d6840 44 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 Dotfile locking
d6850 77 6f 72 6b 73 20 62 79 20 63 72 65 61 74 69 6e works by creatin
d6860 67 20 61 20 66 69 6c 65 20 69 6e 20 74 68 65 20 g a file in the
d6870 73 61 6d 65 20 64 69 72 65 63 74 6f 72 79 20 61 same directory a
d6880 73 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 s the.** databas
d6890 65 20 61 6e 64 20 77 69 74 68 20 74 68 65 20 73 e and with the s
d68a0 61 6d 65 20 6e 61 6d 65 20 62 75 74 20 77 69 74 ame name but wit
d68b0 68 20 61 20 22 2e 6c 6f 63 6b 22 20 65 78 74 65 h a ".lock" exte
d68c0 6e 73 69 6f 6e 20 61 64 64 65 64 2e 0a 2a 2a 20 nsion added..**
d68d0 54 68 65 20 65 78 69 73 74 61 6e 63 65 20 6f 66 The existance of
d68e0 20 61 20 6c 6f 63 6b 20 66 69 6c 65 20 69 6d 70 a lock file imp
d68f0 6c 69 65 73 20 61 6e 20 45 58 43 4c 55 53 49 56 lies an EXCLUSIV
d6900 45 20 6c 6f 63 6b 2e 20 20 41 6c 6c 20 6f 74 68 E lock. All oth
d6910 65 72 20 6c 6f 63 6b 0a 2a 2a 20 74 79 70 65 73 er lock.** types
d6920 20 28 53 48 41 52 45 44 2c 20 52 45 53 45 52 56 (SHARED, RESERV
d6930 45 44 2c 20 50 45 4e 44 49 4e 47 29 20 61 72 65 ED, PENDING) are
d6940 20 6d 61 70 70 65 64 20 69 6e 74 6f 20 45 58 43 mapped into EXC
d6950 4c 55 53 49 56 45 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a LUSIVE..*/../*.*
d6960 2a 20 54 68 65 20 66 69 6c 65 20 73 75 66 66 69 * The file suffi
d6970 78 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 64 x added to the d
d6980 61 74 61 20 62 61 73 65 20 66 69 6c 65 6e 61 6d ata base filenam
d6990 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 72 e in order to cr
d69a0 65 61 74 65 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b eate the.** lock
d69b0 20 66 69 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e file..*/.#defin
d69c0 65 20 44 4f 54 4c 4f 43 4b 5f 53 55 46 46 49 58 e DOTLOCK_SUFFIX
d69d0 20 22 2e 6c 6f 63 6b 22 0a 0a 2f 2a 0a 2a 2a 20 ".lock"../*.**
d69e0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 This routine che
d69f0 63 6b 73 20 69 66 20 74 68 65 72 65 20 69 73 20 cks if there is
d6a00 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 a RESERVED lock
d6a10 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 held on the spec
d6a20 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 ified.** file by
d6a30 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 this or any oth
d6a40 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 er process. If s
d6a50 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 uch a lock is he
d6a60 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 74 ld, set *pResOut
d6a70 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 .** to a non-zer
d6a80 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 77 69 73 o value otherwis
d6a90 65 20 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65 e *pResOut is se
d6aa0 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20 t to zero. The
d6ab0 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 return value.**
d6ac0 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 is set to SQLITE
d6ad0 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f _OK unless an I/
d6ae0 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 O error occurs d
d6af0 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b uring lock check
d6b00 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 64 6f ing..**.** In do
d6b10 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 2c 20 65 tfile locking, e
d6b20 69 74 68 65 72 20 61 20 6c 6f 63 6b 20 65 78 69 ither a lock exi
d6b30 73 74 73 20 6f 72 20 69 74 20 64 6f 65 73 20 6e sts or it does n
d6b40 6f 74 2e 20 20 53 6f 20 69 6e 20 74 68 69 73 0a ot. So in this.
d6b50 2a 2a 20 76 61 72 69 61 74 69 6f 6e 20 6f 66 20 ** variation of
d6b60 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
d6b70 6b 28 29 2c 20 2a 70 52 65 73 4f 75 74 20 69 73 k(), *pResOut is
d6b80 20 73 65 74 20 74 6f 20 74 72 75 65 20 69 66 20 set to true if
d6b90 61 6e 79 20 6c 6f 63 6b 0a 2a 2a 20 69 73 20 68 any lock.** is h
d6ba0 65 6c 64 20 6f 6e 20 74 68 65 20 66 69 6c 65 20 eld on the file
d6bb0 61 6e 64 20 66 61 6c 73 65 20 69 66 20 74 68 65 and false if the
d6bc0 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63 6b 65 file is unlocke
d6bd0 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 d..*/.static int
d6be0 20 64 6f 74 6c 6f 63 6b 43 68 65 63 6b 52 65 73 dotlockCheckRes
d6bf0 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 ervedLock(sqlite
d6c00 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
d6c10 2a 70 52 65 73 4f 75 74 29 20 7b 0a 20 20 69 6e *pResOut) {. in
d6c20 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
d6c30 3b 0a 20 20 69 6e 74 20 72 65 73 65 72 76 65 64 ;. int reserved
d6c40 20 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65 = 0;. unixFile
d6c50 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 *pFile = (unixF
d6c60 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 53 69 6d 75 ile*)id;.. Simu
d6c70 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 lateIOError( ret
d6c80 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
d6c90 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f _CHECKRESERVEDLO
d6ca0 43 4b 3b 20 29 3b 0a 20 20 0a 20 20 61 73 73 65 CK; );. . asse
d6cb0 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 0a 20 20 rt( pFile );..
d6cc0 2f 2a 20 43 68 65 63 6b 20 69 66 20 61 20 74 68 /* Check if a th
d6cd0 72 65 61 64 20 69 6e 20 74 68 69 73 20 70 72 6f read in this pro
d6ce0 63 65 73 73 20 68 6f 6c 64 73 20 73 75 63 68 20 cess holds such
d6cf0 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20 a lock */. if(
d6d00 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e pFile->locktype>
d6d10 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 SHARED_LOCK ){.
d6d20 20 20 20 2f 2a 20 45 69 74 68 65 72 20 74 68 69 /* Either thi
d6d30 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 72 20 s connection or
d6d40 73 6f 6d 65 20 6f 74 68 65 72 20 63 6f 6e 6e 65 some other conne
d6d50 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 73 61 6d ction in the sam
d6d60 65 20 70 72 6f 63 65 73 73 0a 20 20 20 20 2a 2a e process. **
d6d70 20 68 6f 6c 64 73 20 61 20 6c 6f 63 6b 20 6f 6e holds a lock on
d6d80 20 74 68 65 20 66 69 6c 65 2e 20 20 4e 6f 20 6e the file. No n
d6d90 65 65 64 20 74 6f 20 63 68 65 63 6b 20 66 75 72 eed to check fur
d6da0 74 68 65 72 2e 20 2a 2f 0a 20 20 20 20 72 65 73 ther. */. res
d6db0 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c erved = 1;. }el
d6dc0 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 6c se{. /* The l
d6dd0 6f 63 6b 20 69 73 20 68 65 6c 64 20 69 66 20 61 ock is held if a
d6de0 6e 64 20 6f 6e 6c 79 20 69 66 20 74 68 65 20 6c nd only if the l
d6df0 6f 63 6b 66 69 6c 65 20 65 78 69 73 74 73 20 2a ockfile exists *
d6e00 2f 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 /. const char
d6e10 20 2a 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63 *zLockFile = (c
d6e20 6f 6e 73 74 20 63 68 61 72 2a 29 70 46 69 6c 65 onst char*)pFile
d6e30 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 ->lockingContext
d6e40 3b 0a 20 20 20 20 72 65 73 65 72 76 65 64 20 3d ;. reserved =
d6e50 20 61 63 63 65 73 73 28 7a 4c 6f 63 6b 46 69 6c access(zLockFil
d6e60 65 2c 20 30 29 3d 3d 30 3b 0a 20 20 7d 0a 20 20 e, 0)==0;. }.
d6e70 4f 53 54 52 41 43 45 34 28 22 54 45 53 54 20 57 OSTRACE4("TEST W
d6e80 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 25 64 5c R-LOCK %d %d %d\
d6e90 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 n", pFile->h, rc
d6ea0 2c 20 72 65 73 65 72 76 65 64 29 3b 0a 20 20 2a , reserved);. *
d6eb0 70 52 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76 pResOut = reserv
d6ec0 65 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b ed;. return rc;
d6ed0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 .}../*.** Lock t
d6ee0 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 he file with the
d6ef0 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 lock specified
d6f00 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 by parameter loc
d6f10 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f ktype - one.** o
d6f20 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a f the following:
d6f30 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 .**.** (1) S
d6f40 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 HARED_LOCK.**
d6f50 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c (2) RESERVED_L
d6f60 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 OCK.** (3) P
d6f70 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 ENDING_LOCK.**
d6f80 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 (4) EXCLUSIVE
d6f90 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 _LOCK.**.** Some
d6fa0 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 times when reque
d6fb0 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 sting one lock s
d6fc0 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c tate, additional
d6fd0 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 lock states.**
d6fe0 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 are inserted in
d6ff0 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f between. The lo
d7000 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c cking might fail
d7010 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c on one of the l
d7020 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 ater.** transiti
d7030 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 ons leaving the
d7040 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 lock state diffe
d7050 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 rent from what i
d7060 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a t started but.**
d7070 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 still short of
d7080 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 its goal. The f
d7090 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 ollowing chart s
d70a0 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 hows the allowed
d70b0 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 .** transitions
d70c0 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 and the inserted
d70d0 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 intermediate st
d70e0 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 ates:.**.** U
d70f0 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 NLOCKED -> SHARE
d7100 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d D.** SHARED -
d7110 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 > RESERVED.**
d7120 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 SHARED -> (PEND
d7130 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 ING) -> EXCLUSIV
d7140 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 E.** RESERVED
d7150 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e -> (PENDING) ->
d7160 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 EXCLUSIVE.**
d7170 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c PENDING -> EXCL
d7180 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 USIVE.**.** This
d7190 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e routine will on
d71a0 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f ly increase a lo
d71b0 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c ck. Use the sql
d71c0 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a ite3OsUnlock().*
d71d0 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 * routine to low
d71e0 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 er a locking lev
d71f0 65 6c 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 20 64 el..**.** With d
d7200 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 2c 20 otfile locking,
d7210 77 65 20 72 65 61 6c 6c 79 20 6f 6e 6c 79 20 73 we really only s
d7220 75 70 70 6f 72 74 20 73 74 61 74 65 20 28 34 29 upport state (4)
d7230 3a 20 45 58 43 4c 55 53 49 56 45 2e 0a 2a 2a 20 : EXCLUSIVE..**
d7240 42 75 74 20 77 65 20 74 72 61 63 6b 20 74 68 65 But we track the
d7250 20 6f 74 68 65 72 20 6c 6f 63 6b 69 6e 67 20 6c other locking l
d7260 65 76 65 6c 73 20 69 6e 74 65 72 6e 61 6c 6c 79 evels internally
d7270 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
d7280 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 28 73 71 6c 69 dotlockLock(sqli
d7290 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e te3_file *id, in
d72a0 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 t locktype) {.
d72b0 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 unixFile *pFile
d72c0 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b = (unixFile*)id;
d72d0 0a 20 20 69 6e 74 20 66 64 3b 0a 20 20 63 68 61 . int fd;. cha
d72e0 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 r *zLockFile = (
d72f0 63 68 61 72 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f char *)pFile->lo
d7300 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 ckingContext;.
d7310 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
d7320 4f 4b 3b 0a 0a 0a 20 20 2f 2a 20 49 66 20 77 65 OK;... /* If we
d7330 20 68 61 76 65 20 61 6e 79 20 6c 6f 63 6b 2c 20 have any lock,
d7340 74 68 65 6e 20 74 68 65 20 6c 6f 63 6b 20 66 69 then the lock fi
d7350 6c 65 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 le already exist
d7360 73 2e 20 20 41 6c 6c 20 77 65 20 68 61 76 65 0a s. All we have.
d7370 20 20 2a 2a 20 74 6f 20 64 6f 20 69 73 20 61 64 ** to do is ad
d7380 6a 75 73 74 20 6f 75 72 20 69 6e 74 65 72 6e 61 just our interna
d7390 6c 20 72 65 63 6f 72 64 20 6f 66 20 74 68 65 20 l record of the
d73a0 6c 6f 63 6b 20 6c 65 76 65 6c 2e 0a 20 20 2a 2f lock level.. */
d73b0 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f . if( pFile->lo
d73c0 63 6b 74 79 70 65 20 3e 20 4e 4f 5f 4c 4f 43 4b cktype > NO_LOCK
d73d0 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c ){. pFile->l
d73e0 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 ocktype = lockty
d73f0 70 65 3b 0a 23 69 66 20 21 4f 53 5f 56 58 57 4f pe;.#if !OS_VXWO
d7400 52 4b 53 0a 20 20 20 20 2f 2a 20 41 6c 77 61 79 RKS. /* Alway
d7410 73 20 75 70 64 61 74 65 20 74 68 65 20 74 69 6d s update the tim
d7420 65 73 74 61 6d 70 20 6f 6e 20 74 68 65 20 6f 6c estamp on the ol
d7430 64 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 75 74 d file */. ut
d7440 69 6d 65 73 28 7a 4c 6f 63 6b 46 69 6c 65 2c 20 imes(zLockFile,
d7450 4e 55 4c 4c 29 3b 0a 23 65 6e 64 69 66 0a 20 20 NULL);.#endif.
d7460 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
d7470 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 OK;. }. . /*
d7480 67 72 61 62 20 61 6e 20 65 78 63 6c 75 73 69 76 grab an exclusiv
d7490 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 66 64 20 3d e lock */. fd =
d74a0 20 6f 70 65 6e 28 7a 4c 6f 63 6b 46 69 6c 65 2c open(zLockFile,
d74b0 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 43 52 45 41 54 O_RDONLY|O_CREAT
d74c0 7c 4f 5f 45 58 43 4c 2c 30 36 30 30 29 3b 0a 20 |O_EXCL,0600);.
d74d0 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 20 if( fd<0 ){.
d74e0 20 2f 2a 20 66 61 69 6c 65 64 20 74 6f 20 6f 70 /* failed to op
d74f0 65 6e 2f 63 72 65 61 74 65 20 74 68 65 20 66 69 en/create the fi
d7500 6c 65 2c 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 le, someone else
d7510 20 6d 61 79 20 68 61 76 65 20 73 74 6f 6c 65 6e may have stolen
d7520 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 the lock */.
d7530 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 int tErrno = er
d7540 72 6e 6f 3b 0a 20 20 20 20 69 66 28 20 45 45 58 rno;. if( EEX
d7550 49 53 54 20 3d 3d 20 74 45 72 72 6e 6f 20 29 7b IST == tErrno ){
d7560 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
d7570 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 20 65 TE_BUSY;. } e
d7580 6c 73 65 20 7b 0a 20 20 20 20 20 20 72 63 20 3d lse {. rc =
d7590 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d sqliteErrorFrom
d75a0 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e PosixError(tErrn
d75b0 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f o, SQLITE_IOERR_
d75c0 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 LOCK);. if(
d75d0 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 IS_LOCK_ERROR(r
d75e0 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 c) ){. pF
d75f0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
d7600 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d tErrno;. }
d7610 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 . }. retur
d7620 6e 20 72 63 3b 0a 20 20 7d 20 0a 20 20 69 66 28 n rc;. } . if(
d7630 20 63 6c 6f 73 65 28 66 64 29 20 29 7b 0a 20 20 close(fd) ){.
d7640 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
d7650 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 no = errno;.
d7660 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 rc = SQLITE_IOER
d7670 52 5f 43 4c 4f 53 45 3b 0a 20 20 7d 0a 20 20 0a R_CLOSE;. }. .
d7680 20 20 2f 2a 20 67 6f 74 20 69 74 2c 20 73 65 74 /* got it, set
d7690 20 74 68 65 20 74 79 70 65 20 61 6e 64 20 72 65 the type and re
d76a0 74 75 72 6e 20 6f 6b 20 2a 2f 0a 20 20 70 46 69 turn ok */. pFi
d76b0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c le->locktype = l
d76c0 6f 63 6b 74 79 70 65 3b 0a 20 20 72 65 74 75 72 ocktype;. retur
d76d0 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c n rc;.}../*.** L
d76e0 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 ower the locking
d76f0 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 level on file d
d7700 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 escriptor pFile
d7710 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f to locktype. lo
d7720 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 cktype.** must b
d7730 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b e either NO_LOCK
d7740 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e or SHARED_LOCK.
d7750 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f .**.** If the lo
d7760 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 cking level of t
d7770 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 he file descript
d7780 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 or is already at
d7790 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 or below.** the
d77a0 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 requested locki
d77b0 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 ng level, this r
d77c0 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f outine is a no-o
d77d0 70 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 p..**.** When th
d77e0 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 e locking level
d77f0 72 65 61 63 68 65 73 20 4e 4f 5f 4c 4f 43 4b 2c reaches NO_LOCK,
d7800 20 64 65 6c 65 74 65 20 74 68 65 20 6c 6f 63 6b delete the lock
d7810 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 file..*/.static
d7820 20 69 6e 74 20 64 6f 74 6c 6f 63 6b 55 6e 6c 6f int dotlockUnlo
d7830 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ck(sqlite3_file
d7840 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 *id, int locktyp
d7850 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 e) {. unixFile
d7860 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 *pFile = (unixFi
d7870 6c 65 2a 29 69 64 3b 0a 20 20 63 68 61 72 20 2a le*)id;. char *
d7880 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63 68 61 zLockFile = (cha
d7890 72 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 r *)pFile->locki
d78a0 6e 67 43 6f 6e 74 65 78 74 3b 0a 0a 20 20 61 73 ngContext;.. as
d78b0 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 sert( pFile );.
d78c0 20 4f 53 54 52 41 43 45 35 28 22 55 4e 4c 4f 43 OSTRACE5("UNLOC
d78d0 4b 20 20 25 64 20 25 64 20 77 61 73 20 25 64 20 K %d %d was %d
d78e0 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 pid=%d\n", pFile
d78f0 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a 09 ->h, locktype,..
d7900 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 pFile->lockty
d7910 70 65 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20 pe, getpid());.
d7920 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 assert( locktyp
d7930 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 e<=SHARED_LOCK )
d7940 3b 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20 ;. . /* no-op
d7950 69 66 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a 20 if possible */.
d7960 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b if( pFile->lock
d7970 74 79 70 65 3d 3d 6c 6f 63 6b 74 79 70 65 20 29 type==locktype )
d7980 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
d7990 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f ITE_OK;. }.. /
d79a0 2a 20 54 6f 20 64 6f 77 6e 67 72 61 64 65 20 74 * To downgrade t
d79b0 6f 20 73 68 61 72 65 64 2c 20 73 69 6d 70 6c 79 o shared, simply
d79c0 20 75 70 64 61 74 65 20 6f 75 72 20 69 6e 74 65 update our inte
d79d0 72 6e 61 6c 20 6e 6f 74 69 6f 6e 20 6f 66 20 74 rnal notion of t
d79e0 68 65 0a 20 20 2a 2a 20 6c 6f 63 6b 20 73 74 61 he. ** lock sta
d79f0 74 65 2e 20 20 4e 6f 20 6e 65 65 64 20 74 6f 20 te. No need to
d7a00 6d 65 73 73 20 77 69 74 68 20 74 68 65 20 66 69 mess with the fi
d7a10 6c 65 20 6f 6e 20 64 69 73 6b 2e 0a 20 20 2a 2f le on disk.. */
d7a20 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d . if( locktype=
d7a30 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a =SHARED_LOCK ){.
d7a40 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 pFile->lockt
d7a50 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 ype = SHARED_LOC
d7a60 4b 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 K;. return SQ
d7a70 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a LITE_OK;. }. .
d7a80 20 20 2f 2a 20 54 6f 20 66 75 6c 6c 79 20 75 6e /* To fully un
d7a90 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 73 lock the databas
d7aa0 65 2c 20 64 65 6c 65 74 65 20 74 68 65 20 6c 6f e, delete the lo
d7ab0 63 6b 20 66 69 6c 65 20 2a 2f 0a 20 20 61 73 73 ck file */. ass
d7ac0 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e ert( locktype==N
d7ad0 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 28 20 O_LOCK );. if(
d7ae0 75 6e 6c 69 6e 6b 28 7a 4c 6f 63 6b 46 69 6c 65 unlink(zLockFile
d7af0 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 ) ){. int rc
d7b00 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 74 45 72 = 0;. int tEr
d7b10 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 rno = errno;.
d7b20 20 69 66 28 20 45 4e 4f 45 4e 54 20 21 3d 20 74 if( ENOENT != t
d7b30 45 72 72 6e 6f 20 29 7b 0a 20 20 20 20 20 20 72 Errno ){. r
d7b40 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 c = sqliteErrorF
d7b50 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 romPosixError(tE
d7b60 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 rrno, SQLITE_IOE
d7b70 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 RR_UNLOCK);.
d7b80 7d 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 }. if( IS_LOC
d7b90 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 K_ERROR(rc) ){.
d7ba0 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 pFile->last
d7bb0 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a Errno = tErrno;.
d7bc0 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e }. return
d7bd0 20 72 63 3b 20 0a 20 20 7d 0a 20 20 70 46 69 6c rc; . }. pFil
d7be0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f e->locktype = NO
d7bf0 5f 4c 4f 43 4b 3b 0a 20 20 72 65 74 75 72 6e 20 _LOCK;. return
d7c00 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
d7c10 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 .** Close a file
d7c20 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 . Make sure the
d7c30 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 72 lock has been r
d7c40 65 6c 65 61 73 65 64 20 62 65 66 6f 72 65 20 63 eleased before c
d7c50 6c 6f 73 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 losing..*/.stati
d7c60 63 20 69 6e 74 20 64 6f 74 6c 6f 63 6b 43 6c 6f c int dotlockClo
d7c70 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 se(sqlite3_file
d7c80 2a 69 64 29 20 7b 0a 20 20 69 6e 74 20 72 63 3b *id) {. int rc;
d7c90 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20 20 . if( id ){.
d7ca0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
d7cb0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
d7cc0 3b 0a 20 20 20 20 64 6f 74 6c 6f 63 6b 55 6e 6c ;. dotlockUnl
d7cd0 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 ock(id, NO_LOCK)
d7ce0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 ;. sqlite3_fr
d7cf0 65 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e ee(pFile->lockin
d7d00 67 43 6f 6e 74 65 78 74 29 3b 0a 20 20 7d 0a 20 gContext);. }.
d7d10 20 72 63 20 3d 20 63 6c 6f 73 65 55 6e 69 78 46 rc = closeUnixF
d7d20 69 6c 65 28 69 64 29 3b 0a 20 20 72 65 74 75 72 ile(id);. retur
d7d30 6e 20 72 63 3b 0a 7d 0a 2f 2a 2a 2a 2a 2a 2a 2a n rc;.}./*******
d7d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
d7d50 6f 66 20 74 68 65 20 64 6f 74 2d 66 69 6c 65 20 of the dot-file
d7d60 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 lock implementat
d7d70 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ion ************
d7d80 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a *******.********
d7d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7dd0 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a ******/../******
d7de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7e20 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a ********.*******
d7e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7e40 2a 2a 2a 20 42 65 67 69 6e 20 66 6c 6f 63 6b 20 *** Begin flock
d7e50 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a Locking ********
d7e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7e70 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 55 ********.**.** U
d7e80 73 65 20 74 68 65 20 66 6c 6f 63 6b 28 29 20 73 se the flock() s
d7e90 79 73 74 65 6d 20 63 61 6c 6c 20 74 6f 20 64 6f ystem call to do
d7ea0 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 2e 0a 2a file locking..*
d7eb0 2a 0a 2a 2a 20 66 6c 6f 63 6b 28 29 20 6c 6f 63 *.** flock() loc
d7ec0 6b 69 6e 67 20 69 73 20 6c 69 6b 65 20 64 6f 74 king is like dot
d7ed0 2d 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 69 6e -file locking in
d7ee0 20 74 68 61 74 20 74 68 65 20 76 61 72 69 6f 75 that the variou
d7ef0 73 0a 2a 2a 20 66 69 6e 65 2d 67 72 61 69 6e 20 s.** fine-grain
d7f00 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 73 20 73 locking levels s
d7f10 75 70 70 6f 72 74 65 64 20 62 79 20 53 51 4c 69 upported by SQLi
d7f20 74 65 20 61 72 65 20 63 6f 6c 6c 61 70 73 65 64 te are collapsed
d7f30 20 69 6e 74 6f 0a 2a 2a 20 61 20 73 69 6e 67 6c into.** a singl
d7f40 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b e exclusive lock
d7f50 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 . In other word
d7f60 73 2c 20 53 48 41 52 45 44 2c 20 52 45 53 45 52 s, SHARED, RESER
d7f70 56 45 44 2c 20 61 6e 64 0a 2a 2a 20 50 45 4e 44 VED, and.** PEND
d7f80 49 4e 47 20 6c 6f 63 6b 73 20 61 72 65 20 74 68 ING locks are th
d7f90 65 20 73 61 6d 65 20 74 68 69 6e 67 20 61 73 20 e same thing as
d7fa0 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 an EXCLUSIVE loc
d7fb0 6b 2e 20 20 53 51 4c 69 74 65 0a 2a 2a 20 73 74 k. SQLite.** st
d7fc0 69 6c 6c 20 77 6f 72 6b 73 20 77 68 65 6e 20 79 ill works when y
d7fd0 6f 75 20 64 6f 20 74 68 69 73 2c 20 62 75 74 20 ou do this, but
d7fe0 63 6f 6e 63 75 72 72 65 6e 63 79 20 69 73 20 72 concurrency is r
d7ff0 65 64 75 63 65 64 20 73 69 6e 63 65 0a 2a 2a 20 educed since.**
d8000 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 70 72 only a single pr
d8010 6f 63 65 73 73 20 63 61 6e 20 62 65 20 72 65 61 ocess can be rea
d8020 64 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 ding the databas
d8030 65 20 61 74 20 61 20 74 69 6d 65 2e 0a 2a 2a 0a e at a time..**.
d8040 2a 2a 20 4f 6d 69 74 20 74 68 69 73 20 73 65 63 ** Omit this sec
d8050 74 69 6f 6e 20 69 66 20 53 51 4c 49 54 45 5f 45 tion if SQLITE_E
d8060 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
d8070 59 4c 45 20 69 73 20 74 75 72 6e 65 64 20 6f 66 YLE is turned of
d8080 66 20 6f 72 20 69 66 0a 2a 2a 20 63 6f 6d 70 69 f or if.** compi
d8090 6c 69 6e 67 20 66 6f 72 20 56 58 57 4f 52 4b 53 ling for VXWORKS
d80a0 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f ..*/.#if SQLITE_
d80b0 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
d80c0 54 59 4c 45 20 26 26 20 21 4f 53 5f 56 58 57 4f TYLE && !OS_VXWO
d80d0 52 4b 53 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 RKS../*.** This
d80e0 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 routine checks i
d80f0 66 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53 f there is a RES
d8100 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 ERVED lock held
d8110 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64 on the specified
d8120 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 .** file by this
d8130 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 or any other pr
d8140 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 ocess. If such a
d8150 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 lock is held, s
d8160 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74 et *pResOut.** t
d8170 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c o a non-zero val
d8180 75 65 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52 ue otherwise *pR
d8190 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20 esOut is set to
d81a0 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72 zero. The retur
d81b0 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 n value.** is se
d81c0 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 t to SQLITE_OK u
d81d0 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 nless an I/O err
d81e0 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 or occurs during
d81f0 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a lock checking..
d8200 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c */.static int fl
d8210 6f 63 6b 43 68 65 63 6b 52 65 73 65 72 76 65 64 ockCheckReserved
d8220 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c Lock(sqlite3_fil
d8230 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 e *id, int *pRes
d8240 4f 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d Out){. int rc =
d8250 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e SQLITE_OK;. in
d8260 74 20 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a t reserved = 0;.
d8270 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
d8280 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 e = (unixFile*)i
d8290 64 3b 0a 20 20 0a 20 20 53 69 6d 75 6c 61 74 65 d;. . Simulate
d82a0 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 IOError( return
d82b0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 SQLITE_IOERR_CHE
d82c0 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 CKRESERVEDLOCK;
d82d0 29 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 );. . assert(
d82e0 70 46 69 6c 65 20 29 3b 0a 20 20 0a 20 20 2f 2a pFile );. . /*
d82f0 20 43 68 65 63 6b 20 69 66 20 61 20 74 68 72 65 Check if a thre
d8300 61 64 20 69 6e 20 74 68 69 73 20 70 72 6f 63 65 ad in this proce
d8310 73 73 20 68 6f 6c 64 73 20 73 75 63 68 20 61 20 ss holds such a
d8320 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46 lock */. if( pF
d8330 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 ile->locktype>SH
d8340 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 ARED_LOCK ){.
d8350 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 reserved = 1;.
d8360 20 7d 0a 20 20 0a 20 20 2f 2a 20 4f 74 68 65 72 }. . /* Other
d8370 77 69 73 65 20 73 65 65 20 69 66 20 73 6f 6d 65 wise see if some
d8380 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 68 other process h
d8390 6f 6c 64 73 20 69 74 2e 20 2a 2f 0a 20 20 69 66 olds it. */. if
d83a0 28 20 21 72 65 73 65 72 76 65 64 20 29 7b 0a 20 ( !reserved ){.
d83b0 20 20 20 2f 2a 20 61 74 74 65 6d 70 74 20 74 6f /* attempt to
d83c0 20 67 65 74 20 74 68 65 20 6c 6f 63 6b 20 2a 2f get the lock */
d83d0 0a 20 20 20 20 69 6e 74 20 6c 72 63 20 3d 20 66 . int lrc = f
d83e0 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c lock(pFile->h, L
d83f0 4f 43 4b 5f 45 58 20 7c 20 4c 4f 43 4b 5f 4e 42 OCK_EX | LOCK_NB
d8400 29 3b 0a 20 20 20 20 69 66 28 20 21 6c 72 63 20 );. if( !lrc
d8410 29 7b 0a 20 20 20 20 20 20 2f 2a 20 67 6f 74 20 ){. /* got
d8420 74 68 65 20 6c 6f 63 6b 2c 20 75 6e 6c 6f 63 6b the lock, unlock
d8430 20 69 74 20 2a 2f 0a 20 20 20 20 20 20 6c 72 63 it */. lrc
d8440 20 3d 20 66 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e = flock(pFile->
d8450 68 2c 20 4c 4f 43 4b 5f 55 4e 29 3b 0a 20 20 20 h, LOCK_UN);.
d8460 20 20 20 69 66 20 28 20 6c 72 63 20 29 20 7b 0a if ( lrc ) {.
d8470 20 20 20 20 20 20 20 20 69 6e 74 20 74 45 72 72 int tErr
d8480 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 no = errno;.
d8490 20 20 20 20 2f 2a 20 75 6e 6c 6f 63 6b 20 66 61 /* unlock fa
d84a0 69 6c 65 64 20 77 69 74 68 20 61 6e 20 65 72 72 iled with an err
d84b0 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 6c 72 or */. lr
d84c0 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 c = sqliteErrorF
d84d0 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 romPosixError(tE
d84e0 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 rrno, SQLITE_IOE
d84f0 52 52 5f 55 4e 4c 4f 43 4b 29 3b 20 0a 20 20 20 RR_UNLOCK); .
d8500 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b if( IS_LOCK
d8510 5f 45 52 52 4f 52 28 6c 72 63 29 20 29 7b 0a 20 _ERROR(lrc) ){.
d8520 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e pFile->
d8530 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 lastErrno = tErr
d8540 6e 6f 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 no;. rc
d8550 20 3d 20 6c 72 63 3b 0a 20 20 20 20 20 20 20 20 = lrc;.
d8560 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 }. }. }
d8570 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 69 6e 74 else {. int
d8580 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b tErrno = errno;
d8590 0a 20 20 20 20 20 20 72 65 73 65 72 76 65 64 20 . reserved
d85a0 3d 20 31 3b 0a 20 20 20 20 20 20 2f 2a 20 73 6f = 1;. /* so
d85b0 6d 65 6f 6e 65 20 65 6c 73 65 20 6d 69 67 68 74 meone else might
d85c0 20 68 61 76 65 20 69 74 20 72 65 73 65 72 76 65 have it reserve
d85d0 64 20 2a 2f 0a 20 20 20 20 20 20 6c 72 63 20 3d d */. lrc =
d85e0 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d sqliteErrorFrom
d85f0 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e PosixError(tErrn
d8600 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f o, SQLITE_IOERR_
d8610 4c 4f 43 4b 29 3b 20 0a 20 20 20 20 20 20 69 66 LOCK); . if
d8620 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 ( IS_LOCK_ERROR(
d8630 6c 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 lrc) ){.
d8640 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f pFile->lastErrno
d8650 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 = tErrno;.
d8660 20 20 20 72 63 20 3d 20 6c 72 63 3b 0a 20 20 20 rc = lrc;.
d8670 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
d8680 20 4f 53 54 52 41 43 45 34 28 22 54 45 53 54 20 OSTRACE4("TEST
d8690 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 25 64 WR-LOCK %d %d %d
d86a0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 \n", pFile->h, r
d86b0 63 2c 20 72 65 73 65 72 76 65 64 29 3b 0a 0a 23 c, reserved);..#
d86c0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 49 47 4e ifdef SQLITE_IGN
d86d0 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 ORE_FLOCK_LOCK_E
d86e0 52 52 4f 52 53 0a 20 20 69 66 28 20 28 72 63 20 RRORS. if( (rc
d86f0 26 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 29 20 & SQLITE_IOERR)
d8700 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 == SQLITE_IOERR
d8710 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 ){. rc = SQLI
d8720 54 45 5f 4f 4b 3b 0a 20 20 20 20 72 65 73 65 72 TE_OK;. reser
d8730 76 65 64 3d 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 ved=1;. }.#endi
d8740 66 20 2f 2a 20 53 51 4c 49 54 45 5f 49 47 4e 4f f /* SQLITE_IGNO
d8750 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 RE_FLOCK_LOCK_ER
d8760 52 4f 52 53 20 2a 2f 0a 20 20 2a 70 52 65 73 4f RORS */. *pResO
d8770 75 74 20 3d 20 72 65 73 65 72 76 65 64 3b 0a 20 ut = reserved;.
d8780 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
d8790 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 *.** Lock the fi
d87a0 6c 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b le with the lock
d87b0 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 61 specified by pa
d87c0 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 rameter locktype
d87d0 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 - one.** of the
d87e0 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a following:.**.*
d87f0 2a 20 20 20 20 20 28 31 29 20 53 48 41 52 45 44 * (1) SHARED
d8800 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 _LOCK.** (2)
d8810 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a RESERVED_LOCK.*
d8820 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e * (3) PENDIN
d8830 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 G_LOCK.** (4
d8840 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b ) EXCLUSIVE_LOCK
d8850 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 .**.** Sometimes
d8860 20 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 when requesting
d8870 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c one lock state,
d8880 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b additional lock
d8890 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 states.** are i
d88a0 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 nserted in betwe
d88b0 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 en. The locking
d88c0 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f might fail on o
d88d0 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a ne of the later.
d88e0 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c ** transitions l
d88f0 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 eaving the lock
d8900 73 74 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 state different
d8910 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 from what it sta
d8920 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c rted but.** stil
d8930 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 l short of its g
d8940 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 oal. The follow
d8950 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 ing chart shows
d8960 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 the allowed.** t
d8970 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 ransitions and t
d8980 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 he inserted inte
d8990 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 73 3a rmediate states:
d89a0 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b .**.** UNLOCK
d89b0 45 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 ED -> SHARED.**
d89c0 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 SHARED -> RES
d89d0 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 ERVED.** SHAR
d89e0 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 ED -> (PENDING)
d89f0 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 -> EXCLUSIVE.**
d8a00 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 RESERVED -> (
d8a10 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c PENDING) -> EXCL
d8a20 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 USIVE.** PEND
d8a30 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 ING -> EXCLUSIVE
d8a40 0a 2a 2a 0a 2a 2a 20 66 6c 6f 63 6b 28 29 20 6f .**.** flock() o
d8a50 6e 6c 79 20 72 65 61 6c 6c 79 20 73 75 70 70 6f nly really suppo
d8a60 72 74 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 rt EXCLUSIVE loc
d8a70 6b 73 2e 20 20 57 65 20 74 72 61 63 6b 20 69 6e ks. We track in
d8a80 74 65 72 6d 65 64 69 61 74 65 0a 2a 2a 20 6c 6f termediate.** lo
d8a90 63 6b 20 73 74 61 74 65 73 20 69 6e 20 74 68 65 ck states in the
d8aa0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 74 sqlite3_file st
d8ab0 72 75 63 74 75 72 65 2c 20 62 75 74 20 61 6c 6c ructure, but all
d8ac0 20 6c 6f 63 6b 73 20 53 48 41 52 45 44 20 6f 72 locks SHARED or
d8ad0 0a 2a 2a 20 61 62 6f 76 65 20 61 72 65 20 72 65 .** above are re
d8ae0 61 6c 6c 79 20 45 58 43 4c 55 53 49 56 45 20 6c ally EXCLUSIVE l
d8af0 6f 63 6b 73 20 61 6e 64 20 65 78 63 6c 75 64 65 ocks and exclude
d8b00 20 61 6c 6c 20 6f 74 68 65 72 20 70 72 6f 63 65 all other proce
d8b10 73 73 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 63 63 sses from.** acc
d8b20 65 73 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a ess the file..**
d8b30 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
d8b40 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 will only incre
d8b50 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 ase a lock. Use
d8b60 20 74 68 65 20 73 71 6c 69 74 65 33 4f 73 55 6e the sqlite3OsUn
d8b70 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e lock().** routin
d8b80 65 20 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 e to lower a loc
d8b90 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 king level..*/.s
d8ba0 74 61 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b 4c tatic int flockL
d8bb0 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ock(sqlite3_file
d8bc0 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 *id, int lockty
d8bd0 70 65 29 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d pe) {. int rc =
d8be0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e SQLITE_OK;. un
d8bf0 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 ixFile *pFile =
d8c00 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a (unixFile*)id;..
d8c10 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 assert( pFile
d8c20 29 3b 0a 0a 20 20 2f 2a 20 69 66 20 77 65 20 61 );.. /* if we a
d8c30 6c 72 65 61 64 79 20 68 61 76 65 20 61 20 6c 6f lready have a lo
d8c40 63 6b 2c 20 69 74 20 69 73 20 65 78 63 6c 75 73 ck, it is exclus
d8c50 69 76 65 2e 20 20 0a 20 20 2a 2a 20 4a 75 73 74 ive. . ** Just
d8c60 20 61 64 6a 75 73 74 20 6c 65 76 65 6c 20 61 6e adjust level an
d8c70 64 20 70 75 6e 74 20 6f 6e 20 6f 75 74 74 61 20 d punt on outta
d8c80 68 65 72 65 2e 20 2a 2f 0a 20 20 69 66 20 28 70 here. */. if (p
d8c90 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3e File->locktype >
d8ca0 20 4e 4f 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 NO_LOCK) {.
d8cb0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 pFile->locktype
d8cc0 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 = locktype;.
d8cd0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
d8ce0 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 67 72 ;. }. . /* gr
d8cf0 61 62 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 ab an exclusive
d8d00 6c 6f 63 6b 20 2a 2f 0a 20 20 0a 20 20 69 66 20 lock */. . if
d8d10 28 66 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c (flock(pFile->h,
d8d20 20 4c 4f 43 4b 5f 45 58 20 7c 20 4c 4f 43 4b 5f LOCK_EX | LOCK_
d8d30 4e 42 29 29 20 7b 0a 20 20 20 20 69 6e 74 20 74 NB)) {. int t
d8d40 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
d8d50 20 20 20 2f 2a 20 64 69 64 6e 27 74 20 67 65 74 /* didn't get
d8d60 2c 20 6d 75 73 74 20 62 65 20 62 75 73 79 20 2a , must be busy *
d8d70 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 /. rc = sqlit
d8d80 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 eErrorFromPosixE
d8d90 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c rror(tErrno, SQL
d8da0 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b ITE_IOERR_LOCK);
d8db0 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b . if( IS_LOCK
d8dc0 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 _ERROR(rc) ){.
d8dd0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 pFile->lastE
d8de0 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 rrno = tErrno;.
d8df0 20 20 20 7d 0a 20 20 7d 20 65 6c 73 65 20 7b 0a }. } else {.
d8e00 20 20 20 20 2f 2a 20 67 6f 74 20 69 74 2c 20 73 /* got it, s
d8e10 65 74 20 74 68 65 20 74 79 70 65 20 61 6e 64 20 et the type and
d8e20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f 0a 20 20 20 return ok */.
d8e30 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
d8e40 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d = locktype;. }
d8e50 0a 20 20 4f 53 54 52 41 43 45 34 28 22 4c 4f 43 . OSTRACE4("LOC
d8e60 4b 20 20 20 20 25 64 20 25 73 20 25 73 5c 6e 22 K %d %s %s\n"
d8e70 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b , pFile->h, lock
d8e80 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 typeName(locktyp
d8e90 65 29 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 e), .
d8ea0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 rc==SQLITE_OK ?
d8eb0 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29 "ok" : "failed")
d8ec0 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
d8ed0 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 IGNORE_FLOCK_LOC
d8ee0 4b 5f 45 52 52 4f 52 53 0a 20 20 69 66 28 20 28 K_ERRORS. if( (
d8ef0 72 63 20 26 20 53 51 4c 49 54 45 5f 49 4f 45 52 rc & SQLITE_IOER
d8f00 52 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 R) == SQLITE_IOE
d8f10 52 52 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 RR ){. rc = S
d8f20 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a QLITE_BUSY;. }.
d8f30 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
d8f40 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f _IGNORE_FLOCK_LO
d8f50 43 4b 5f 45 52 52 4f 52 53 20 2a 2f 0a 20 20 72 CK_ERRORS */. r
d8f60 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a eturn rc;.}.../*
d8f70 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f .** Lower the lo
d8f80 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 cking level on f
d8f90 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70 ile descriptor p
d8fa0 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 File to locktype
d8fb0 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d . locktype.** m
d8fc0 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f ust be either NO
d8fd0 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f _LOCK or SHARED_
d8fe0 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 LOCK..**.** If t
d8ff0 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c he locking level
d9000 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 of the file des
d9010 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 criptor is alrea
d9020 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a dy at or below.*
d9030 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 * the requested
d9040 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 locking level, t
d9050 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 his routine is a
d9060 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 no-op..*/.stati
d9070 63 20 69 6e 74 20 66 6c 6f 63 6b 55 6e 6c 6f 63 c int flockUnloc
d9080 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a k(sqlite3_file *
d9090 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 id, int locktype
d90a0 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a ) {. unixFile *
d90b0 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c pFile = (unixFil
d90c0 65 2a 29 69 64 3b 0a 20 20 0a 20 20 61 73 73 65 e*)id;. . asse
d90d0 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f rt( pFile );. O
d90e0 53 54 52 41 43 45 35 28 22 55 4e 4c 4f 43 4b 20 STRACE5("UNLOCK
d90f0 20 25 64 20 25 64 20 77 61 73 20 25 64 20 70 69 %d %d was %d pi
d9100 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e d=%d\n", pFile->
d9110 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a 20 20 20 h, locktype,.
d9120 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c pFile->l
d9130 6f 63 6b 74 79 70 65 2c 20 67 65 74 70 69 64 28 ocktype, getpid(
d9140 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f ));. assert( lo
d9150 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c cktype<=SHARED_L
d9160 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 6e OCK );. . /* n
d9170 6f 2d 6f 70 20 69 66 20 70 6f 73 73 69 62 6c 65 o-op if possible
d9180 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d */. if( pFile-
d9190 3e 6c 6f 63 6b 74 79 70 65 3d 3d 6c 6f 63 6b 74 >locktype==lockt
d91a0 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ype ){. retur
d91b0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
d91c0 0a 20 20 0a 20 20 2f 2a 20 73 68 61 72 65 64 20 . . /* shared
d91d0 63 61 6e 20 6a 75 73 74 20 62 65 20 73 65 74 20 can just be set
d91e0 62 65 63 61 75 73 65 20 77 65 20 61 6c 77 61 79 because we alway
d91f0 73 20 68 61 76 65 20 61 6e 20 65 78 63 6c 75 73 s have an exclus
d9200 69 76 65 20 2a 2f 0a 20 20 69 66 20 28 6c 6f 63 ive */. if (loc
d9210 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f ktype==SHARED_LO
d9220 43 4b 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d CK) {. pFile-
d9230 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b >locktype = lock
d9240 74 79 70 65 3b 0a 20 20 20 20 72 65 74 75 72 6e type;. return
d9250 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
d9260 20 20 0a 20 20 2f 2a 20 6e 6f 2c 20 72 65 61 6c . /* no, real
d9270 6c 79 2c 20 75 6e 6c 6f 63 6b 2e 20 2a 2f 0a 20 ly, unlock. */.
d9280 20 69 6e 74 20 72 63 20 3d 20 66 6c 6f 63 6b 28 int rc = flock(
d9290 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 55 pFile->h, LOCK_U
d92a0 4e 29 3b 0a 20 20 69 66 20 28 72 63 29 20 7b 0a N);. if (rc) {.
d92b0 20 20 20 20 69 6e 74 20 72 2c 20 74 45 72 72 6e int r, tErrn
d92c0 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 o = errno;. r
d92d0 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 = sqliteErrorFr
d92e0 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 omPosixError(tEr
d92f0 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 rno, SQLITE_IOER
d9300 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 R_UNLOCK);. i
d9310 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 f( IS_LOCK_ERROR
d9320 28 72 29 20 29 7b 0a 20 20 20 20 20 20 70 46 69 (r) ){. pFi
d9330 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
d9340 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 23 69 tErrno;. }.#i
d9350 66 64 65 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f fdef SQLITE_IGNO
d9360 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 RE_FLOCK_LOCK_ER
d9370 52 4f 52 53 0a 20 20 20 20 69 66 28 20 28 72 20 RORS. if( (r
d9380 26 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 29 20 & SQLITE_IOERR)
d9390 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 == SQLITE_IOERR
d93a0 29 7b 0a 20 20 20 20 20 20 72 20 3d 20 53 51 4c ){. r = SQL
d93b0 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 0a ITE_BUSY;. }.
d93c0 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
d93d0 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f _IGNORE_FLOCK_LO
d93e0 43 4b 5f 45 52 52 4f 52 53 20 2a 2f 0a 20 20 20 CK_ERRORS */.
d93f0 20 0a 20 20 20 20 72 65 74 75 72 6e 20 72 3b 0a . return r;.
d9400 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 70 } else {. p
d9410 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d File->locktype =
d9420 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 72 65 NO_LOCK;. re
d9430 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
d9440 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f }.}../*.** Clo
d9450 73 65 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 se a file..*/.st
d9460 61 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b 43 6c atic int flockCl
d9470 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ose(sqlite3_file
d9480 20 2a 69 64 29 20 7b 0a 20 20 69 66 28 20 69 64 *id) {. if( id
d9490 20 29 7b 0a 20 20 20 20 66 6c 6f 63 6b 55 6e 6c ){. flockUnl
d94a0 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 ock(id, NO_LOCK)
d94b0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 63 ;. }. return c
d94c0 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29 loseUnixFile(id)
d94d0 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 ;.}..#endif /* S
d94e0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 QLITE_ENABLE_LOC
d94f0 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 21 4f KING_STYLE && !O
d9500 53 5f 56 58 57 4f 52 4b 20 2a 2f 0a 0a 2f 2a 2a S_VXWORK */../**
d9510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9520 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 66 6c 6f * End of the flo
d9530 63 6b 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e ck lock implemen
d9540 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a tation *********
d9550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a ************.***
d9560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d95a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a ***********/../*
d95b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d95c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d95d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d95e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d95f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
d9600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9610 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 4e 61 6d ****** Begin Nam
d9620 65 64 20 53 65 6d 61 70 68 6f 72 65 20 4c 6f 63 ed Semaphore Loc
d9630 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a king ***********
d9640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
d9650 0a 2a 2a 20 4e 61 6d 65 64 20 73 65 6d 61 70 68 .** Named semaph
d9660 6f 72 65 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6f ore locking is o
d9670 6e 6c 79 20 73 75 70 70 6f 72 74 65 64 20 6f 6e nly supported on
d9680 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a 0a 2a 2a 20 VxWorks..**.**
d9690 53 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e Semaphore lockin
d96a0 67 20 69 73 20 6c 69 6b 65 20 64 6f 74 2d 6c 6f g is like dot-lo
d96b0 63 6b 20 61 6e 64 20 66 6c 6f 63 6b 20 69 6e 20 ck and flock in
d96c0 74 68 61 74 20 69 74 20 72 65 61 6c 6c 79 20 6f that it really o
d96d0 6e 6c 79 0a 2a 2a 20 73 75 70 70 6f 72 74 73 20 nly.** supports
d96e0 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 69 6e EXCLUSIVE lockin
d96f0 67 2e 20 20 4f 6e 6c 79 20 61 20 73 69 6e 67 6c g. Only a singl
d9700 65 20 70 72 6f 63 65 73 73 20 63 61 6e 20 72 65 e process can re
d9710 61 64 20 6f 72 20 77 72 69 74 65 0a 2a 2a 20 74 ad or write.** t
d9720 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
d9730 20 61 74 20 61 20 74 69 6d 65 2e 20 20 54 68 69 at a time. Thi
d9740 73 20 72 65 64 75 63 65 73 20 70 6f 74 65 6e 74 s reduces potent
d9750 69 61 6c 20 63 6f 6e 63 75 72 72 65 6e 63 79 2c ial concurrency,
d9760 20 62 75 74 0a 2a 2a 20 6d 61 6b 65 73 20 74 68 but.** makes th
d9770 65 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 e lock implement
d9780 61 74 69 6f 6e 20 6d 75 63 68 20 65 61 73 69 65 ation much easie
d9790 72 2e 0a 2a 2f 0a 23 69 66 20 4f 53 5f 56 58 57 r..*/.#if OS_VXW
d97a0 4f 52 4b 53 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 ORKS../*.** This
d97b0 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 routine checks
d97c0 69 66 20 74 68 65 72 65 20 69 73 20 61 20 52 45 if there is a RE
d97d0 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 SERVED lock held
d97e0 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 on the specifie
d97f0 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 d.** file by thi
d9800 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 s or any other p
d9810 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 rocess. If such
d9820 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 a lock is held,
d9830 73 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 set *pResOut.**
d9840 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 to a non-zero va
d9850 6c 75 65 20 6f 74 68 65 72 77 69 73 65 20 2a 70 lue otherwise *p
d9860 52 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f ResOut is set to
d9870 20 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 74 75 zero. The retu
d9880 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 rn value.** is s
d9890 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 et to SQLITE_OK
d98a0 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 unless an I/O er
d98b0 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e ror occurs durin
d98c0 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e g lock checking.
d98d0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 .*/.static int s
d98e0 65 6d 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c emCheckReservedL
d98f0 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ock(sqlite3_file
d9900 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f *id, int *pResO
d9910 75 74 29 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d ut) {. int rc =
d9920 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e SQLITE_OK;. in
d9930 74 20 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a t reserved = 0;.
d9940 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
d9950 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 e = (unixFile*)i
d9960 64 3b 0a 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f d;.. SimulateIO
d9970 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 Error( return SQ
d9980 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b LITE_IOERR_CHECK
d9990 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b RESERVEDLOCK; );
d99a0 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 . . assert( pF
d99b0 69 6c 65 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 ile );.. /* Che
d99c0 63 6b 20 69 66 20 61 20 74 68 72 65 61 64 20 69 ck if a thread i
d99d0 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 n this process h
d99e0 6f 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b olds such a lock
d99f0 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d */. if( pFile-
d9a00 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 >locktype>SHARED
d9a10 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 _LOCK ){. res
d9a20 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 erved = 1;. }.
d9a30 20 0a 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 . /* Otherwise
d9a40 20 73 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 see if some oth
d9a50 65 72 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 er process holds
d9a60 20 69 74 2e 20 2a 2f 0a 20 20 69 66 28 20 21 72 it. */. if( !r
d9a70 65 73 65 72 76 65 64 20 29 7b 0a 20 20 20 20 73 eserved ){. s
d9a80 65 6d 5f 74 20 2a 70 53 65 6d 20 3d 20 70 46 69 em_t *pSem = pFi
d9a90 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 3b le->pOpen->pSem;
d9aa0 0a 20 20 20 20 73 74 72 75 63 74 20 73 74 61 74 . struct stat
d9ab0 20 73 74 61 74 42 75 66 3b 0a 0a 20 20 20 20 69 statBuf;.. i
d9ac0 66 28 20 73 65 6d 5f 74 72 79 77 61 69 74 28 70 f( sem_trywait(p
d9ad0 53 65 6d 29 3d 3d 2d 31 20 29 7b 0a 20 20 20 20 Sem)==-1 ){.
d9ae0 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 int tErrno = e
d9af0 72 72 6e 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 rrno;. if(
d9b00 45 41 47 41 49 4e 20 21 3d 20 74 45 72 72 6e 6f EAGAIN != tErrno
d9b10 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
d9b20 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d sqliteErrorFrom
d9b30 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e PosixError(tErrn
d9b40 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f o, SQLITE_IOERR_
d9b50 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 CHECKRESERVEDLOC
d9b60 4b 29 3b 0a 20 20 20 20 20 20 20 20 70 46 69 6c K);. pFil
d9b70 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 e->lastErrno = t
d9b80 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 20 65 Errno;. } e
d9b90 6c 73 65 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a lse {. /*
d9ba0 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 68 61 someone else ha
d9bb0 73 20 74 68 65 20 6c 6f 63 6b 20 77 68 65 6e 20 s the lock when
d9bc0 77 65 20 61 72 65 20 69 6e 20 4e 4f 5f 4c 4f 43 we are in NO_LOC
d9bd0 4b 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 73 K */. res
d9be0 65 72 76 65 64 20 3d 20 28 70 46 69 6c 65 2d 3e erved = (pFile->
d9bf0 6c 6f 63 6b 74 79 70 65 20 3c 20 53 48 41 52 45 locktype < SHARE
d9c00 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 7d D_LOCK);. }
d9c10 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
d9c20 20 20 2f 2a 20 77 65 20 63 6f 75 6c 64 20 68 61 /* we could ha
d9c30 76 65 20 69 74 20 69 66 20 77 65 20 77 61 6e 74 ve it if we want
d9c40 20 69 74 20 2a 2f 0a 20 20 20 20 20 20 73 65 6d it */. sem
d9c50 5f 70 6f 73 74 28 70 53 65 6d 29 3b 0a 20 20 20 _post(pSem);.
d9c60 20 7d 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 }. }. OSTRACE
d9c70 34 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 4("TEST WR-LOCK
d9c80 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 70 46 69 %d %d %d\n", pFi
d9c90 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65 72 le->h, rc, reser
d9ca0 76 65 64 29 3b 0a 0a 20 20 2a 70 52 65 73 4f 75 ved);.. *pResOu
d9cb0 74 20 3d 20 72 65 73 65 72 76 65 64 3b 0a 20 20 t = reserved;.
d9cc0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
d9cd0 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c .** Lock the fil
d9ce0 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 e with the lock
d9cf0 73 70 65 63 69 66 69 65 64 20 62 79 20 70 61 72 specified by par
d9d00 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 ameter locktype
d9d10 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 - one.** of the
d9d20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a following:.**.**
d9d30 20 20 20 20 20 28 31 29 20 53 48 41 52 45 44 5f (1) SHARED_
d9d40 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 LOCK.** (2)
d9d50 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a RESERVED_LOCK.**
d9d60 20 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47 (3) PENDING
d9d70 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 _LOCK.** (4)
d9d80 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a EXCLUSIVE_LOCK.
d9d90 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 **.** Sometimes
d9da0 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 when requesting
d9db0 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 one lock state,
d9dc0 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 additional lock
d9dd0 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e states.** are in
d9de0 73 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 65 serted in betwee
d9df0 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 n. The locking
d9e00 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e might fail on on
d9e10 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a e of the later.*
d9e20 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 * transitions le
d9e30 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 aving the lock s
d9e40 74 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 tate different f
d9e50 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 rom what it star
d9e60 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c ted but.** still
d9e70 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f short of its go
d9e80 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 al. The followi
d9e90 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 ng chart shows t
d9ea0 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 he allowed.** tr
d9eb0 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 ansitions and th
d9ec0 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 e inserted inter
d9ed0 6d 65 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a mediate states:.
d9ee0 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 **.** UNLOCKE
d9ef0 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 D -> SHARED.**
d9f00 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 SHARED -> RESE
d9f10 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 RVED.** SHARE
d9f20 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d D -> (PENDING) -
d9f30 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 > EXCLUSIVE.**
d9f40 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 RESERVED -> (P
d9f50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 ENDING) -> EXCLU
d9f60 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 SIVE.** PENDI
d9f70 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a NG -> EXCLUSIVE.
d9f80 2a 2a 0a 2a 2a 20 53 65 6d 61 70 68 6f 72 65 20 **.** Semaphore
d9f90 6c 6f 63 6b 73 20 6f 6e 6c 79 20 72 65 61 6c 6c locks only reall
d9fa0 79 20 73 75 70 70 6f 72 74 20 45 58 43 4c 55 53 y support EXCLUS
d9fb0 49 56 45 20 6c 6f 63 6b 73 2e 20 20 57 65 20 74 IVE locks. We t
d9fc0 72 61 63 6b 20 69 6e 74 65 72 6d 65 64 69 61 74 rack intermediat
d9fd0 65 0a 2a 2a 20 6c 6f 63 6b 20 73 74 61 74 65 73 e.** lock states
d9fe0 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f in the sqlite3_
d9ff0 66 69 6c 65 20 73 74 72 75 63 74 75 72 65 2c 20 file structure,
da000 62 75 74 20 61 6c 6c 20 6c 6f 63 6b 73 20 53 48 but all locks SH
da010 41 52 45 44 20 6f 72 0a 2a 2a 20 61 62 6f 76 65 ARED or.** above
da020 20 61 72 65 20 72 65 61 6c 6c 79 20 45 58 43 4c are really EXCL
da030 55 53 49 56 45 20 6c 6f 63 6b 73 20 61 6e 64 20 USIVE locks and
da040 65 78 63 6c 75 64 65 20 61 6c 6c 20 6f 74 68 65 exclude all othe
da050 72 20 70 72 6f 63 65 73 73 65 73 20 66 72 6f 6d r processes from
da060 0a 2a 2a 20 61 63 63 65 73 73 20 74 68 65 20 66 .** access the f
da070 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ile..**.** This
da080 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c routine will onl
da090 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 y increase a loc
da0a0 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c 69 k. Use the sqli
da0b0 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a te3OsUnlock().**
da0c0 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 routine to lowe
da0d0 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 r a locking leve
da0e0 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 l..*/.static int
da0f0 20 73 65 6d 4c 6f 63 6b 28 73 71 6c 69 74 65 33 semLock(sqlite3
da100 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c _file *id, int l
da110 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 ocktype) {. uni
da120 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 xFile *pFile = (
da130 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 unixFile*)id;.
da140 69 6e 74 20 66 64 3b 0a 20 20 73 65 6d 5f 74 20 int fd;. sem_t
da150 2a 70 53 65 6d 20 3d 20 70 46 69 6c 65 2d 3e 70 *pSem = pFile->p
da160 4f 70 65 6e 2d 3e 70 53 65 6d 3b 0a 20 20 69 6e Open->pSem;. in
da170 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
da180 3b 0a 0a 20 20 2f 2a 20 69 66 20 77 65 20 61 6c ;.. /* if we al
da190 72 65 61 64 79 20 68 61 76 65 20 61 20 6c 6f 63 ready have a loc
da1a0 6b 2c 20 69 74 20 69 73 20 65 78 63 6c 75 73 69 k, it is exclusi
da1b0 76 65 2e 20 20 0a 20 20 2a 2a 20 4a 75 73 74 20 ve. . ** Just
da1c0 61 64 6a 75 73 74 20 6c 65 76 65 6c 20 61 6e 64 adjust level and
da1d0 20 70 75 6e 74 20 6f 6e 20 6f 75 74 74 61 20 68 punt on outta h
da1e0 65 72 65 2e 20 2a 2f 0a 20 20 69 66 20 28 70 46 ere. */. if (pF
da1f0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3e 20 ile->locktype >
da200 4e 4f 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70 NO_LOCK) {. p
da210 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d File->locktype =
da220 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 72 locktype;. r
da230 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
da240 20 20 20 67 6f 74 6f 20 73 65 6d 5f 65 6e 64 5f goto sem_end_
da250 6c 6f 63 6b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f lock;. }. . /
da260 2a 20 6c 6f 63 6b 20 73 65 6d 61 70 68 6f 72 65 * lock semaphore
da270 20 6e 6f 77 20 62 75 74 20 62 61 69 6c 20 6f 75 now but bail ou
da280 74 20 77 68 65 6e 20 61 6c 72 65 61 64 79 20 6c t when already l
da290 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 69 66 28 20 ocked. */. if(
da2a0 73 65 6d 5f 74 72 79 77 61 69 74 28 70 53 65 6d sem_trywait(pSem
da2b0 29 3d 3d 2d 31 20 29 7b 0a 20 20 20 20 72 63 20 )==-1 ){. rc
da2c0 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 = SQLITE_BUSY;.
da2d0 20 20 20 67 6f 74 6f 20 73 65 6d 5f 65 6e 64 5f goto sem_end_
da2e0 6c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 lock;. }.. /*
da2f0 67 6f 74 20 69 74 2c 20 73 65 74 20 74 68 65 20 got it, set the
da300 74 79 70 65 20 61 6e 64 20 72 65 74 75 72 6e 20 type and return
da310 6f 6b 20 2a 2f 0a 20 20 70 46 69 6c 65 2d 3e 6c ok */. pFile->l
da320 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 ocktype = lockty
da330 70 65 3b 0a 0a 20 73 65 6d 5f 65 6e 64 5f 6c 6f pe;.. sem_end_lo
da340 63 6b 3a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b ck:. return rc;
da350 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 .}../*.** Lower
da360 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 the locking leve
da370 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 l on file descri
da380 70 74 6f 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f ptor pFile to lo
da390 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 cktype. locktyp
da3a0 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 e.** must be eit
da3b0 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 her NO_LOCK or S
da3c0 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a HARED_LOCK..**.*
da3d0 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 * If the locking
da3e0 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 level of the fi
da3f0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 le descriptor is
da400 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 already at or b
da410 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 elow.** the requ
da420 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 ested locking le
da430 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e vel, this routin
da440 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f e is a no-op..*/
da450 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 6d 55 .static int semU
da460 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 nlock(sqlite3_fi
da470 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b le *id, int lock
da480 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 type) {. unixFi
da490 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 le *pFile = (uni
da4a0 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 73 65 6d xFile*)id;. sem
da4b0 5f 74 20 2a 70 53 65 6d 20 3d 20 70 46 69 6c 65 _t *pSem = pFile
da4c0 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 3b 0a 0a ->pOpen->pSem;..
da4d0 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 assert( pFile
da4e0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53 65 );. assert( pSe
da4f0 6d 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 m );. OSTRACE5(
da500 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 "UNLOCK %d %d w
da510 61 73 20 25 64 20 70 69 64 3d 25 64 5c 6e 22 2c as %d pid=%d\n",
da520 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 pFile->h, lockt
da530 79 70 65 2c 0a 09 20 20 20 70 46 69 6c 65 2d 3e ype,.. pFile->
da540 6c 6f 63 6b 74 79 70 65 2c 20 67 65 74 70 69 64 locktype, getpid
da550 28 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c ());. assert( l
da560 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f ocktype<=SHARED_
da570 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 LOCK );. . /*
da580 6e 6f 2d 6f 70 20 69 66 20 70 6f 73 73 69 62 6c no-op if possibl
da590 65 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 e */. if( pFile
da5a0 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 6c 6f 63 6b ->locktype==lock
da5b0 74 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75 type ){. retu
da5c0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
da5d0 7d 0a 20 20 0a 20 20 2f 2a 20 73 68 61 72 65 64 }. . /* shared
da5e0 20 63 61 6e 20 6a 75 73 74 20 62 65 20 73 65 74 can just be set
da5f0 20 62 65 63 61 75 73 65 20 77 65 20 61 6c 77 61 because we alwa
da600 79 73 20 68 61 76 65 20 61 6e 20 65 78 63 6c 75 ys have an exclu
da610 73 69 76 65 20 2a 2f 0a 20 20 69 66 20 28 6c 6f sive */. if (lo
da620 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c cktype==SHARED_L
da630 4f 43 4b 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 OCK) {. pFile
da640 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 ->locktype = loc
da650 6b 74 79 70 65 3b 0a 20 20 20 20 72 65 74 75 72 ktype;. retur
da660 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
da670 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2c 20 72 65 61 . . /* no, rea
da680 6c 6c 79 20 75 6e 6c 6f 63 6b 2e 20 2a 2f 0a 20 lly unlock. */.
da690 20 69 66 20 28 20 73 65 6d 5f 70 6f 73 74 28 70 if ( sem_post(p
da6a0 53 65 6d 29 3d 3d 2d 31 20 29 20 7b 0a 20 20 20 Sem)==-1 ) {.
da6b0 20 69 6e 74 20 72 63 2c 20 74 45 72 72 6e 6f 20 int rc, tErrno
da6c0 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 63 20 = errno;. rc
da6d0 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f = sqliteErrorFro
da6e0 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 mPosixError(tErr
da6f0 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 no, SQLITE_IOERR
da700 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 _UNLOCK);. if
da710 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 ( IS_LOCK_ERROR(
da720 72 63 29 20 29 7b 0a 20 20 20 20 20 20 70 46 69 rc) ){. pFi
da730 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
da740 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 tErrno;. }.
da750 20 20 72 65 74 75 72 6e 20 72 63 3b 20 0a 20 20 return rc; .
da760 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 }. pFile->lockt
da770 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 ype = NO_LOCK;.
da780 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
da790 4b 3b 0a 7d 0a 0a 2f 2a 0a 20 2a 2a 20 43 6c 6f K;.}../*. ** Clo
da7a0 73 65 20 61 20 66 69 6c 65 2e 0a 20 2a 2f 0a 73 se a file.. */.s
da7b0 74 61 74 69 63 20 69 6e 74 20 73 65 6d 43 6c 6f tatic int semClo
da7c0 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 se(sqlite3_file
da7d0 2a 69 64 29 20 7b 0a 20 20 69 66 28 20 69 64 20 *id) {. if( id
da7e0 29 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 ){. unixFile
da7f0 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 *pFile = (unixFi
da800 6c 65 2a 29 69 64 3b 0a 20 20 20 20 73 65 6d 55 le*)id;. semU
da810 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 nlock(id, NO_LOC
da820 4b 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 K);. assert(
da830 70 46 69 6c 65 20 29 3b 0a 20 20 20 20 75 6e 69 pFile );. uni
da840 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 xEnterMutex();.
da850 20 20 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e releaseLockIn
da860 66 6f 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 29 fo(pFile->pLock)
da870 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 4f 70 65 ;. releaseOpe
da880 6e 43 6e 74 28 70 46 69 6c 65 2d 3e 70 4f 70 65 nCnt(pFile->pOpe
da890 6e 29 3b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 n);. unixLeav
da8a0 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 63 6c eMutex();. cl
da8b0 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b oseUnixFile(id);
da8c0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
da8d0 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 LITE_OK;.}..#end
da8e0 69 66 20 2f 2a 20 4f 53 5f 56 58 57 4f 52 4b 53 if /* OS_VXWORKS
da8f0 20 2a 2f 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 64 20 */./*.** Named
da900 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e semaphore lockin
da910 67 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 g is only availa
da920 62 6c 65 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a ble on VxWorks..
da930 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
da940 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 6e 61 ** End of the na
da950 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f med semaphore lo
da960 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ck implementatio
da970 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n **************
da980 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
da990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da9a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da9b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da9c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da9d0 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */.../**********
da9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da9f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
daa00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
daa10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
daa20 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****.***********
daa30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
daa40 20 42 65 67 69 6e 20 41 46 50 20 4c 6f 63 6b 69 Begin AFP Locki
daa50 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ng *************
daa60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
daa70 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 41 46 50 20 69 ****.**.** AFP i
daa80 73 20 74 68 65 20 41 70 70 6c 65 20 46 69 6c 69 s the Apple Fili
daa90 6e 67 20 50 72 6f 74 6f 63 6f 6c 2e 20 20 41 46 ng Protocol. AF
daaa0 50 20 69 73 20 61 20 6e 65 74 77 6f 72 6b 20 66 P is a network f
daab0 69 6c 65 73 79 73 74 65 6d 20 66 6f 75 6e 64 0a ilesystem found.
daac0 2a 2a 20 6f 6e 20 41 70 70 6c 65 20 4d 61 63 69 ** on Apple Maci
daad0 6e 74 6f 73 68 20 63 6f 6d 70 75 74 65 72 73 20 ntosh computers
daae0 2d 20 62 6f 74 68 20 4f 53 39 20 61 6e 64 20 4f - both OS9 and O
daaf0 53 58 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 72 64 2d SX..**.** Third-
dab00 70 61 72 74 79 20 69 6d 70 6c 65 6d 65 6e 74 61 party implementa
dab10 74 69 6f 6e 73 20 6f 66 20 41 46 50 20 61 72 65 tions of AFP are
dab20 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 42 75 74 available. But
dab30 20 74 68 69 73 20 63 6f 64 65 20 68 65 72 65 0a this code here.
dab40 2a 2a 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e ** only works on
dab50 20 4f 53 58 2e 0a 2a 2f 0a 0a 23 69 66 20 64 65 OSX..*/..#if de
dab60 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 fined(__APPLE__)
dab70 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c && SQLITE_ENABL
dab80 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a E_LOCKING_STYLE.
dab90 2f 2a 0a 2a 2a 20 54 68 65 20 61 66 70 4c 6f 63 /*.** The afpLoc
daba0 6b 69 6e 67 43 6f 6e 74 65 78 74 20 73 74 72 75 kingContext stru
dabb0 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 cture contains a
dabc0 6c 6c 20 61 66 70 20 6c 6f 63 6b 20 73 70 65 63 ll afp lock spec
dabd0 69 66 69 63 20 73 74 61 74 65 0a 2a 2f 0a 74 79 ific state.*/.ty
dabe0 70 65 64 65 66 20 73 74 72 75 63 74 20 61 66 70 pedef struct afp
dabf0 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 61 LockingContext a
dac00 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 fpLockingContext
dac10 3b 0a 73 74 72 75 63 74 20 61 66 70 4c 6f 63 6b ;.struct afpLock
dac20 69 6e 67 43 6f 6e 74 65 78 74 20 7b 0a 20 20 75 ingContext {. u
dac30 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
dac40 67 20 73 68 61 72 65 64 42 79 74 65 3b 0a 20 20 g sharedByte;.
dac50 63 6f 6e 73 74 20 63 68 61 72 20 2a 64 62 50 61 const char *dbPa
dac60 74 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 th;
dac70 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6f /* Name of the o
dac80 70 65 6e 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a pen file */.};..
dac90 73 74 72 75 63 74 20 42 79 74 65 52 61 6e 67 65 struct ByteRange
daca0 4c 6f 63 6b 50 42 32 0a 7b 0a 20 20 75 6e 73 69 LockPB2.{. unsi
dacb0 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6f gned long long o
dacc0 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 2f 2a ffset; /*
dacd0 20 6f 66 66 73 65 74 20 74 6f 20 66 69 72 73 74 offset to first
dace0 20 62 79 74 65 20 74 6f 20 6c 6f 63 6b 20 2a 2f byte to lock */
dacf0 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 . unsigned long
dad00 20 6c 6f 6e 67 20 6c 65 6e 67 74 68 3b 20 20 20 long length;
dad10 20 20 20 20 20 2f 2a 20 6e 62 72 20 6f 66 20 62 /* nbr of b
dad20 79 74 65 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a ytes to lock */.
dad30 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 unsigned long
dad40 6c 6f 6e 67 20 72 65 74 52 61 6e 67 65 53 74 61 long retRangeSta
dad50 72 74 3b 20 2f 2a 20 6e 62 72 20 6f 66 20 31 73 rt; /* nbr of 1s
dad60 74 20 62 79 74 65 20 6c 6f 63 6b 65 64 20 69 66 t byte locked if
dad70 20 73 75 63 63 65 73 73 66 75 6c 20 2a 2f 0a 20 successful */.
dad80 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 75 unsigned char u
dad90 6e 4c 6f 63 6b 46 6c 61 67 3b 20 20 20 20 20 20 nLockFlag;
dada0 20 20 20 2f 2a 20 31 20 3d 20 75 6e 6c 6f 63 6b /* 1 = unlock
dadb0 2c 20 30 20 3d 20 6c 6f 63 6b 20 2a 2f 0a 20 20 , 0 = lock */.
dadc0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 74 unsigned char st
dadd0 61 72 74 45 6e 64 46 6c 61 67 3b 20 20 20 20 20 artEndFlag;
dade0 20 20 2f 2a 20 31 3d 72 65 6c 20 74 6f 20 65 6e /* 1=rel to en
dadf0 64 20 6f 66 20 66 6f 72 6b 2c 20 30 3d 72 65 6c d of fork, 0=rel
dae00 20 74 6f 20 73 74 61 72 74 20 2a 2f 0a 20 20 69 to start */. i
dae10 6e 74 20 66 64 3b 20 20 20 20 20 20 20 20 20 20 nt fd;
dae20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dae30 20 2f 2a 20 66 69 6c 65 20 64 65 73 63 20 74 6f /* file desc to
dae40 20 61 73 73 6f 63 20 74 68 69 73 20 6c 6f 63 6b assoc this lock
dae50 20 77 69 74 68 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 with */.};..#de
dae60 66 69 6e 65 20 61 66 70 66 73 42 79 74 65 52 61 fine afpfsByteRa
dae70 6e 67 65 4c 6f 63 6b 32 46 53 43 54 4c 20 20 20 ngeLock2FSCTL
dae80 20 20 20 20 20 5f 49 4f 57 52 28 27 7a 27 2c 20 _IOWR('z',
dae90 32 33 2c 20 73 74 72 75 63 74 20 42 79 74 65 52 23, struct ByteR
daea0 61 6e 67 65 4c 6f 63 6b 50 42 32 29 0a 0a 2f 2a angeLockPB2)../*
daeb0 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20 75 74 .** This is a ut
daec0 69 6c 69 74 79 20 66 6f 72 20 73 65 74 74 69 6e ility for settin
daed0 67 20 6f 72 20 63 6c 65 61 72 69 6e 67 20 61 20 g or clearing a
daee0 62 69 74 2d 72 61 6e 67 65 20 6c 6f 63 6b 20 6f bit-range lock o
daef0 6e 20 61 6e 0a 2a 2a 20 41 46 50 20 66 69 6c 65 n an.** AFP file
daf00 73 79 73 74 65 6d 2e 0a 2a 2a 20 0a 2a 2a 20 52 system..** .** R
daf10 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 eturn SQLITE_OK
daf20 6f 6e 20 73 75 63 63 65 73 73 2c 20 53 51 4c 49 on success, SQLI
daf30 54 45 5f 42 55 53 59 20 6f 6e 20 66 61 69 6c 75 TE_BUSY on failu
daf40 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e re..*/.static in
daf50 74 20 61 66 70 53 65 74 4c 6f 63 6b 28 0a 20 20 t afpSetLock(.
daf60 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68 const char *path
daf70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
daf80 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 * Name of the fi
daf90 6c 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 le to be locked
dafa0 6f 72 20 75 6e 6c 6f 63 6b 65 64 20 2a 2f 0a 20 or unlocked */.
dafb0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
dafc0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
dafd0 2f 2a 20 4f 70 65 6e 20 66 69 6c 65 20 64 65 73 /* Open file des
dafe0 63 72 69 70 74 6f 72 20 6f 6e 20 70 61 74 68 20 criptor on path
daff0 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f */. unsigned lo
db000 6e 67 20 6c 6f 6e 67 20 6f 66 66 73 65 74 2c 20 ng long offset,
db010 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 /* First byt
db020 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a e to be locked *
db030 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e /. unsigned lon
db040 67 20 6c 6f 6e 67 20 6c 65 6e 67 74 68 2c 20 20 g long length,
db050 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
db060 62 79 74 65 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f bytes to lock */
db070 0a 20 20 69 6e 74 20 73 65 74 4c 6f 63 6b 46 6c . int setLockFl
db080 61 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ag
db090 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 73 65 74 /* True to set
db0a0 20 6c 6f 63 6b 2e 20 20 46 61 6c 73 65 20 74 6f lock. False to
db0b0 20 63 6c 65 61 72 20 6c 6f 63 6b 20 2a 2f 0a 29 clear lock */.)
db0c0 7b 0a 20 20 73 74 72 75 63 74 20 42 79 74 65 52 {. struct ByteR
db0d0 61 6e 67 65 4c 6f 63 6b 50 42 32 20 70 62 3b 0a angeLockPB2 pb;.
db0e0 20 20 69 6e 74 20 65 72 72 3b 0a 20 20 0a 20 20 int err;. .
db0f0 70 62 2e 75 6e 4c 6f 63 6b 46 6c 61 67 20 3d 20 pb.unLockFlag =
db100 73 65 74 4c 6f 63 6b 46 6c 61 67 20 3f 20 30 20 setLockFlag ? 0
db110 3a 20 31 3b 0a 20 20 70 62 2e 73 74 61 72 74 45 : 1;. pb.startE
db120 6e 64 46 6c 61 67 20 3d 20 30 3b 0a 20 20 70 62 ndFlag = 0;. pb
db130 2e 6f 66 66 73 65 74 20 3d 20 6f 66 66 73 65 74 .offset = offset
db140 3b 0a 20 20 70 62 2e 6c 65 6e 67 74 68 20 3d 20 ;. pb.length =
db150 6c 65 6e 67 74 68 3b 20 0a 20 20 70 62 2e 66 64 length; . pb.fd
db160 20 3d 20 70 46 69 6c 65 2d 3e 68 3b 0a 20 20 0a = pFile->h;. .
db170 20 20 4f 53 54 52 41 43 45 36 28 22 41 46 50 53 OSTRACE6("AFPS
db180 45 54 4c 4f 43 4b 20 5b 25 73 5d 20 66 6f 72 20 ETLOCK [%s] for
db190 25 64 25 73 20 69 6e 20 72 61 6e 67 65 20 25 6c %d%s in range %l
db1a0 6c 78 3a 25 6c 6c 78 5c 6e 22 2c 20 0a 20 20 20 lx:%llx\n", .
db1b0 20 28 73 65 74 4c 6f 63 6b 46 6c 61 67 3f 22 4f (setLockFlag?"O
db1c0 4e 22 3a 22 4f 46 46 22 29 2c 20 70 46 69 6c 65 N":"OFF"), pFile
db1d0 2d 3e 68 2c 20 28 70 62 2e 66 64 3d 3d 2d 31 3f ->h, (pb.fd==-1?
db1e0 22 5b 74 65 73 74 76 61 6c 2d 31 5d 22 3a 22 22 "[testval-1]":""
db1f0 29 2c 0a 20 20 20 20 6f 66 66 73 65 74 2c 20 6c ),. offset, l
db200 65 6e 67 74 68 29 3b 0a 20 20 65 72 72 20 3d 20 ength);. err =
db210 66 73 63 74 6c 28 70 61 74 68 2c 20 61 66 70 66 fsctl(path, afpf
db220 73 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 32 46 sByteRangeLock2F
db230 53 43 54 4c 2c 20 26 70 62 2c 20 30 29 3b 0a 20 SCTL, &pb, 0);.
db240 20 69 66 20 28 20 65 72 72 3d 3d 2d 31 20 29 20 if ( err==-1 )
db250 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 {. int rc;.
db260 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 int tErrno = e
db270 72 72 6e 6f 3b 0a 20 20 20 20 4f 53 54 52 41 43 rrno;. OSTRAC
db280 45 34 28 22 41 46 50 53 45 54 4c 4f 43 4b 20 66 E4("AFPSETLOCK f
db290 61 69 6c 65 64 20 74 6f 20 66 73 63 74 6c 28 29 ailed to fsctl()
db2a0 20 27 25 73 27 20 25 64 20 25 73 5c 6e 22 2c 0a '%s' %d %s\n",.
db2b0 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 74 pat
db2c0 68 2c 20 74 45 72 72 6e 6f 2c 20 73 74 72 65 72 h, tErrno, strer
db2d0 72 6f 72 28 74 45 72 72 6e 6f 29 29 3b 0a 23 69 ror(tErrno));.#i
db2e0 66 64 65 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f fdef SQLITE_IGNO
db2f0 52 45 5f 41 46 50 5f 4c 4f 43 4b 5f 45 52 52 4f RE_AFP_LOCK_ERRO
db300 52 53 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 RS. rc = SQLI
db310 54 45 5f 42 55 53 59 3b 0a 23 65 6c 73 65 0a 20 TE_BUSY;.#else.
db320 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 rc = sqliteEr
db330 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f rorFromPosixErro
db340 72 28 74 45 72 72 6e 6f 2c 0a 20 20 20 20 20 20 r(tErrno,.
db350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 65 se
db360 74 4c 6f 63 6b 46 6c 61 67 20 3f 20 53 51 4c 49 tLockFlag ? SQLI
db370 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 20 3a 20 TE_IOERR_LOCK :
db380 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c SQLITE_IOERR_UNL
db390 4f 43 4b 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 OCK);.#endif /*
db3a0 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 41 46 SQLITE_IGNORE_AF
db3b0 50 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 20 2a 2f P_LOCK_ERRORS */
db3c0 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b . if( IS_LOCK
db3d0 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 _ERROR(rc) ){.
db3e0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 pFile->lastE
db3f0 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 rrno = tErrno;.
db400 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 }. return
db410 72 63 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 rc;. } else {.
db420 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
db430 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a _OK;. }.}../*.*
db440 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 * This routine c
db450 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 69 hecks if there i
db460 73 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 s a RESERVED loc
db470 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 k held on the sp
db480 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 ecified.** file
db490 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f by this or any o
db4a0 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 ther process. If
db4b0 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 such a lock is
db4c0 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 4f held, set *pResO
db4d0 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a ut.** to a non-z
db4e0 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 77 ero value otherw
db4f0 69 73 65 20 2a 70 52 65 73 4f 75 74 20 69 73 20 ise *pResOut is
db500 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 set to zero. Th
db510 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a e return value.*
db520 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 * is set to SQLI
db530 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 TE_OK unless an
db540 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 I/O error occurs
db550 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 during lock che
db560 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 cking..*/.static
db570 20 69 6e 74 20 61 66 70 43 68 65 63 6b 52 65 73 int afpCheckRes
db580 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 ervedLock(sqlite
db590 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
db5a0 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 69 6e 74 *pResOut){. int
db5b0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
db5c0 0a 20 20 69 6e 74 20 72 65 73 65 72 76 65 64 20 . int reserved
db5d0 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 = 0;. unixFile
db5e0 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 *pFile = (unixFi
db5f0 6c 65 2a 29 69 64 3b 0a 20 20 0a 20 20 53 69 6d le*)id;. . Sim
db600 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 ulateIOError( re
db610 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 turn SQLITE_IOER
db620 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c R_CHECKRESERVEDL
db630 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 20 61 73 73 OCK; );. . ass
db640 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 ert( pFile );.
db650 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 afpLockingContex
db660 74 20 2a 63 6f 6e 74 65 78 74 20 3d 20 28 61 66 t *context = (af
db670 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 pLockingContext
db680 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e *) pFile->lockin
db690 67 43 6f 6e 74 65 78 74 3b 0a 20 20 0a 20 20 2f gContext;. . /
db6a0 2a 20 43 68 65 63 6b 20 69 66 20 61 20 74 68 72 * Check if a thr
db6b0 65 61 64 20 69 6e 20 74 68 69 73 20 70 72 6f 63 ead in this proc
db6c0 65 73 73 20 68 6f 6c 64 73 20 73 75 63 68 20 61 ess holds such a
db6d0 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70 lock */. if( p
db6e0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 File->locktype>S
db6f0 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 HARED_LOCK ){.
db700 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a reserved = 1;.
db710 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 4f 74 68 65 }. . /* Othe
db720 72 77 69 73 65 20 73 65 65 20 69 66 20 73 6f 6d rwise see if som
db730 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 e other process
db740 68 6f 6c 64 73 20 69 74 2e 0a 20 20 20 2a 2f 0a holds it.. */.
db750 20 20 69 66 28 20 21 72 65 73 65 72 76 65 64 20 if( !reserved
db760 29 7b 0a 20 20 20 20 2f 2a 20 6c 6f 63 6b 20 74 ){. /* lock t
db770 68 65 20 52 45 53 45 52 56 45 44 20 62 79 74 65 he RESERVED byte
db780 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 72 63 20 */. int lrc
db790 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e = afpSetLock(con
db7a0 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 text->dbPath, pF
db7b0 69 6c 65 2c 20 52 45 53 45 52 56 45 44 5f 42 59 ile, RESERVED_BY
db7c0 54 45 2c 20 31 2c 31 29 3b 20 20 0a 20 20 20 20 TE, 1,1); .
db7d0 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 6c if( SQLITE_OK==l
db7e0 72 63 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 69 rc ){. /* i
db7f0 66 20 77 65 20 73 75 63 63 65 65 64 65 64 20 69 f we succeeded i
db800 6e 20 74 61 6b 69 6e 67 20 74 68 65 20 72 65 73 n taking the res
db810 65 72 76 65 64 20 6c 6f 63 6b 2c 20 75 6e 6c 6f erved lock, unlo
db820 63 6b 20 69 74 20 74 6f 20 72 65 73 74 6f 72 65 ck it to restore
db830 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6f 72 . ** the or
db840 69 67 69 6e 61 6c 20 73 74 61 74 65 20 2a 2f 0a iginal state */.
db850 20 20 20 20 20 20 6c 72 63 20 3d 20 61 66 70 53 lrc = afpS
db860 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e etLock(context->
db870 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 52 dbPath, pFile, R
db880 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 31 2c ESERVED_BYTE, 1,
db890 20 30 29 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 0);. } else
db8a0 7b 0a 20 20 20 20 20 20 2f 2a 20 69 66 20 77 65 {. /* if we
db8b0 20 66 61 69 6c 65 64 20 74 6f 20 67 65 74 20 74 failed to get t
db8c0 68 65 20 6c 6f 63 6b 20 74 68 65 6e 20 73 6f 6d he lock then som
db8d0 65 6f 6e 65 20 65 6c 73 65 20 6d 75 73 74 20 68 eone else must h
db8e0 61 76 65 20 69 74 20 2a 2f 0a 20 20 20 20 20 20 ave it */.
db8f0 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 reserved = 1;.
db900 20 20 7d 0a 20 20 20 20 69 66 28 20 49 53 5f 4c }. if( IS_L
db910 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 29 20 29 OCK_ERROR(lrc) )
db920 7b 0a 20 20 20 20 20 20 72 63 3d 6c 72 63 3b 0a {. rc=lrc;.
db930 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 4f }. }. . O
db940 53 54 52 41 43 45 34 28 22 54 45 53 54 20 57 52 STRACE4("TEST WR
db950 2d 4c 4f 43 4b 20 25 64 20 25 64 20 25 64 5c 6e -LOCK %d %d %d\n
db960 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c ", pFile->h, rc,
db970 20 72 65 73 65 72 76 65 64 29 3b 0a 20 20 0a 20 reserved);. .
db980 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65 73 65 *pResOut = rese
db990 72 76 65 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 rved;. return r
db9a0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b c;.}../*.** Lock
db9b0 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 the file with t
db9c0 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 he lock specifie
db9d0 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c d by parameter l
db9e0 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a ocktype - one.**
db9f0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
dba00 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 g:.**.** (1)
dba10 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 SHARED_LOCK.**
dba20 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45 44 (2) RESERVED
dba30 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 _LOCK.** (3)
dba40 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a PENDING_LOCK.**
dba50 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 (4) EXCLUSI
dba60 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f VE_LOCK.**.** So
dba70 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 metimes when req
dba80 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b uesting one lock
dba90 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e state, addition
dbaa0 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a al lock states.*
dbab0 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 * are inserted i
dbac0 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 n between. The
dbad0 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 locking might fa
dbae0 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 il on one of the
dbaf0 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 later.** transi
dbb00 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 tions leaving th
dbb10 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 e lock state dif
dbb20 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 ferent from what
dbb30 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74 0a it started but.
dbb40 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f ** still short o
dbb50 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 f its goal. The
dbb60 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 following chart
dbb70 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 shows the allow
dbb80 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e ed.** transition
dbb90 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 s and the insert
dbba0 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 ed intermediate
dbbb0 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 states:.**.**
dbbc0 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 UNLOCKED -> SHA
dbbd0 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 RED.** SHARED
dbbe0 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 -> RESERVED.**
dbbf0 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 SHARED -> (PE
dbc00 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 NDING) -> EXCLUS
dbc10 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 IVE.** RESERV
dbc20 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 ED -> (PENDING)
dbc30 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 -> EXCLUSIVE.**
dbc40 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 PENDING -> EX
dbc50 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 CLUSIVE.**.** Th
dbc60 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 is routine will
dbc70 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 only increase a
dbc80 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 lock. Use the s
dbc90 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 qlite3OsUnlock()
dbca0 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c .** routine to l
dbcb0 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c ower a locking l
dbcc0 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 evel..*/.static
dbcd0 69 6e 74 20 61 66 70 4c 6f 63 6b 28 73 71 6c 69 int afpLock(sqli
dbce0 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e te3_file *id, in
dbcf0 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69 t locktype){. i
dbd00 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
dbd10 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 K;. unixFile *p
dbd20 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 File = (unixFile
dbd30 2a 29 69 64 3b 0a 20 20 61 66 70 4c 6f 63 6b 69 *)id;. afpLocki
dbd40 6e 67 43 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 ngContext *conte
dbd50 78 74 20 3d 20 28 61 66 70 4c 6f 63 6b 69 6e 67 xt = (afpLocking
dbd60 43 6f 6e 74 65 78 74 20 2a 29 20 70 46 69 6c 65 Context *) pFile
dbd70 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 ->lockingContext
dbd80 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 ;. . assert( p
dbd90 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 File );. OSTRAC
dbda0 45 35 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 E5("LOCK %d %
dbdb0 73 20 77 61 73 20 25 73 20 70 69 64 3d 25 64 5c s was %s pid=%d\
dbdc0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 n", pFile->h,.
dbdd0 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e locktypeN
dbde0 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 6c ame(locktype), l
dbdf0 6f 63 6b 74 79 70 65 4e 61 6d 65 28 70 46 69 6c ocktypeName(pFil
dbe00 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 20 67 65 e->locktype), ge
dbe10 74 70 69 64 28 29 29 3b 0a 0a 20 20 2f 2a 20 49 tpid());.. /* I
dbe20 66 20 74 68 65 72 65 20 69 73 20 61 6c 72 65 61 f there is alrea
dbe30 64 79 20 61 20 6c 6f 63 6b 20 6f 66 20 74 68 69 dy a lock of thi
dbe40 73 20 74 79 70 65 20 6f 72 20 6d 6f 72 65 20 72 s type or more r
dbe50 65 73 74 72 69 63 74 69 76 65 20 6f 6e 20 74 68 estrictive on th
dbe60 65 0a 20 20 2a 2a 20 75 6e 69 78 46 69 6c 65 2c e. ** unixFile,
dbe70 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e do nothing. Don
dbe80 27 74 20 75 73 65 20 74 68 65 20 61 66 70 5f 65 't use the afp_e
dbe90 6e 64 5f 6c 6f 63 6b 3a 20 65 78 69 74 20 70 61 nd_lock: exit pa
dbea0 74 68 2c 20 61 73 0a 20 20 2a 2a 20 75 6e 69 78 th, as. ** unix
dbeb0 45 6e 74 65 72 4d 75 74 65 78 28 29 20 68 61 73 EnterMutex() has
dbec0 6e 27 74 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 n't been called
dbed0 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 yet.. */. if(
dbee0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e pFile->locktype>
dbef0 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 =locktype ){.
dbf00 20 4f 53 54 52 41 43 45 33 28 22 4c 4f 43 4b 20 OSTRACE3("LOCK
dbf10 20 20 20 25 64 20 25 73 20 6f 6b 20 28 61 6c 72 %d %s ok (alr
dbf20 65 61 64 79 20 68 65 6c 64 29 5c 6e 22 2c 20 70 eady held)\n", p
dbf30 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 File->h,.
dbf40 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 locktypeName
dbf50 28 6c 6f 63 6b 74 79 70 65 29 29 3b 0a 20 20 20 (locktype));.
dbf60 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
dbf70 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b K;. }.. /* Mak
dbf80 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 e sure the locki
dbf90 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 63 ng sequence is c
dbfa0 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20 61 73 orrect. */. as
dbfb0 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 sert( pFile->loc
dbfc0 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c ktype!=NO_LOCK |
dbfd0 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 | locktype==SHAR
dbfe0 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 ED_LOCK );. ass
dbff0 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 50 ert( locktype!=P
dc000 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 ENDING_LOCK );.
dc010 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 assert( locktyp
dc020 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b e!=RESERVED_LOCK
dc030 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 || pFile->lockt
dc040 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype==SHARED_LOCK
dc050 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 54 68 69 73 );. . /* This
dc060 20 6d 75 74 65 78 20 69 73 20 6e 65 65 64 65 64 mutex is needed
dc070 20 62 65 63 61 75 73 65 20 70 46 69 6c 65 2d 3e because pFile->
dc080 70 4c 6f 63 6b 20 69 73 20 73 68 61 72 65 64 20 pLock is shared
dc090 61 63 72 6f 73 73 20 74 68 72 65 61 64 73 0a 20 across threads.
dc0a0 20 2a 2f 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d */. unixEnterM
dc0b0 75 74 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 4d 61 utex();.. /* Ma
dc0c0 6b 65 20 73 75 72 65 20 74 68 65 20 63 75 72 72 ke sure the curr
dc0d0 65 6e 74 20 74 68 72 65 61 64 20 6f 77 6e 73 20 ent thread owns
dc0e0 74 68 65 20 70 46 69 6c 65 2e 0a 20 20 2a 2f 0a the pFile.. */.
dc0f0 20 20 72 63 20 3d 20 74 72 61 6e 73 66 65 72 4f rc = transferO
dc100 77 6e 65 72 73 68 69 70 28 70 46 69 6c 65 29 3b wnership(pFile);
dc110 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
dc120 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 6e 69 78 E_OK ){. unix
dc130 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 LeaveMutex();.
dc140 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d return rc;. }
dc150 0a 20 20 20 20 0a 20 20 2f 2a 20 41 20 50 45 4e . . /* A PEN
dc160 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 6e 65 65 DING lock is nee
dc170 64 65 64 20 62 65 66 6f 72 65 20 61 63 71 75 69 ded before acqui
dc180 72 69 6e 67 20 61 20 53 48 41 52 45 44 20 6c 6f ring a SHARED lo
dc190 63 6b 20 61 6e 64 20 62 65 66 6f 72 65 0a 20 20 ck and before.
dc1a0 2a 2a 20 61 63 71 75 69 72 69 6e 67 20 61 6e 20 ** acquiring an
dc1b0 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 EXCLUSIVE lock.
dc1c0 20 46 6f 72 20 74 68 65 20 53 48 41 52 45 44 20 For the SHARED
dc1d0 6c 6f 63 6b 2c 20 74 68 65 20 50 45 4e 44 49 4e lock, the PENDIN
dc1e0 47 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 72 G will. ** be r
dc1f0 65 6c 65 61 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 eleased.. */.
dc200 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 if( locktype==SH
dc210 41 52 45 44 5f 4c 4f 43 4b 20 0a 20 20 20 20 20 ARED_LOCK .
dc220 20 7c 7c 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 || (locktype==E
dc230 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 XCLUSIVE_LOCK &&
dc240 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
dc250 3c 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 29 0a 20 <PENDING_LOCK).
dc260 20 29 7b 0a 20 20 20 20 69 6e 74 20 66 61 69 6c ){. int fail
dc270 65 64 3b 0a 20 20 20 20 66 61 69 6c 65 64 20 3d ed;. failed =
dc280 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 afpSetLock(cont
dc290 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 ext->dbPath, pFi
dc2a0 6c 65 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 le, PENDING_BYTE
dc2b0 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 69 66 20 , 1, 1);. if
dc2c0 28 66 61 69 6c 65 64 29 20 7b 0a 20 20 20 20 20 (failed) {.
dc2d0 20 72 63 20 3d 20 66 61 69 6c 65 64 3b 0a 20 20 rc = failed;.
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 0a 20 20 7d _lock;. }. }
dc300 0a 20 20 0a 20 20 2f 2a 20 49 66 20 63 6f 6e 74 . . /* If cont
dc310 72 6f 6c 20 67 65 74 73 20 74 6f 20 74 68 69 73 rol gets to this
dc320 20 70 6f 69 6e 74 2c 20 74 68 65 6e 20 61 63 74 point, then act
dc330 75 61 6c 6c 79 20 67 6f 20 61 68 65 61 64 20 61 ually go ahead a
dc340 6e 64 20 6d 61 6b 65 0a 20 20 2a 2a 20 6f 70 65 nd make. ** ope
dc350 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 63 61 rating system ca
dc360 6c 6c 73 20 66 6f 72 20 74 68 65 20 73 70 65 63 lls for the spec
dc370 69 66 69 65 64 20 6c 6f 63 6b 2e 0a 20 20 2a 2f ified lock.. */
dc380 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d . if( locktype=
dc390 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a =SHARED_LOCK ){.
dc3a0 20 20 20 20 69 6e 74 20 6c 6b 2c 20 6c 72 63 31 int lk, lrc1
dc3b0 2c 20 6c 72 63 32 3b 0a 20 20 20 20 69 6e 74 20 , lrc2;. int
dc3c0 6c 72 63 31 45 72 72 6e 6f 20 3d 20 30 3b 0a 20 lrc1Errno = 0;.
dc3d0 20 20 20 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 67 . /* Now g
dc3e0 65 74 20 74 68 65 20 72 65 61 64 2d 6c 6f 63 6b et the read-lock
dc3f0 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 2a 2f 0a SHARED_LOCK */.
dc400 20 20 20 20 2f 2a 20 6e 6f 74 65 20 74 68 61 74 /* note that
dc410 20 74 68 65 20 71 75 61 6c 69 74 79 20 6f 66 20 the quality of
dc420 74 68 65 20 72 61 6e 64 6f 6d 6e 65 73 73 20 64 the randomness d
dc430 6f 65 73 6e 27 74 20 6d 61 74 74 65 72 20 74 68 oesn't matter th
dc440 61 74 20 6d 75 63 68 20 2a 2f 0a 20 20 20 20 6c at much */. l
dc450 6b 20 3d 20 72 61 6e 64 6f 6d 28 29 3b 20 0a 20 k = random(); .
dc460 20 20 20 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72 context->shar
dc470 65 64 42 79 74 65 20 3d 20 28 6c 6b 20 26 20 30 edByte = (lk & 0
dc480 78 37 66 66 66 66 66 66 66 29 25 28 53 48 41 52 x7fffffff)%(SHAR
dc490 45 44 5f 53 49 5a 45 20 2d 20 31 29 3b 0a 20 20 ED_SIZE - 1);.
dc4a0 20 20 6c 72 63 31 20 3d 20 61 66 70 53 65 74 4c lrc1 = afpSetL
dc4b0 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 ock(context->dbP
dc4c0 61 74 68 2c 20 70 46 69 6c 65 2c 20 0a 20 20 20 ath, pFile, .
dc4d0 20 20 20 20 20 20 20 53 48 41 52 45 44 5f 46 49 SHARED_FI
dc4e0 52 53 54 2b 63 6f 6e 74 65 78 74 2d 3e 73 68 61 RST+context->sha
dc4f0 72 65 64 42 79 74 65 2c 20 31 2c 20 31 29 3b 0a redByte, 1, 1);.
dc500 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f if( IS_LOCK_
dc510 45 52 52 4f 52 28 6c 72 63 31 29 20 29 7b 0a 20 ERROR(lrc1) ){.
dc520 20 20 20 20 20 6c 72 63 31 45 72 72 6e 6f 20 3d lrc1Errno =
dc530 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
dc540 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 o;. }. /*
dc550 44 72 6f 70 20 74 68 65 20 74 65 6d 70 6f 72 61 Drop the tempora
dc560 72 79 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 ry PENDING lock
dc570 2a 2f 0a 20 20 20 20 6c 72 63 32 20 3d 20 61 66 */. lrc2 = af
dc580 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 pSetLock(context
dc590 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c ->dbPath, pFile,
dc5a0 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 31 PENDING_BYTE, 1
dc5b0 2c 20 30 29 3b 0a 20 20 20 20 0a 20 20 20 20 69 , 0);. . i
dc5c0 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 f( IS_LOCK_ERROR
dc5d0 28 6c 72 63 31 29 20 29 20 7b 0a 20 20 20 20 20 (lrc1) ) {.
dc5e0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
dc5f0 6f 20 3d 20 6c 72 63 31 45 72 72 6e 6f 3b 0a 20 o = lrc1Errno;.
dc600 20 20 20 20 20 72 63 20 3d 20 6c 72 63 31 3b 0a rc = lrc1;.
dc610 20 20 20 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 goto afp_e
dc620 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d 20 65 nd_lock;. } e
dc630 6c 73 65 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f lse if( IS_LOCK_
dc640 45 52 52 4f 52 28 6c 72 63 32 29 20 29 7b 0a 20 ERROR(lrc2) ){.
dc650 20 20 20 20 20 72 63 20 3d 20 6c 72 63 32 3b 0a rc = lrc2;.
dc660 20 20 20 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 goto afp_e
dc670 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d 20 65 nd_lock;. } e
dc680 6c 73 65 20 69 66 28 20 6c 72 63 31 20 21 3d 20 lse if( lrc1 !=
dc690 53 51 4c 49 54 45 5f 4f 4b 20 29 20 7b 0a 20 20 SQLITE_OK ) {.
dc6a0 20 20 20 20 72 63 20 3d 20 6c 72 63 31 3b 0a 20 rc = lrc1;.
dc6b0 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 } else {.
dc6c0 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 pFile->locktyp
dc6d0 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b e = SHARED_LOCK;
dc6e0 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 70 4f . pFile->pO
dc6f0 70 65 6e 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 pen->nLock++;.
dc700 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
dc710 20 2f 2a 20 54 68 65 20 72 65 71 75 65 73 74 20 /* The request
dc720 77 61 73 20 66 6f 72 20 61 20 52 45 53 45 52 56 was for a RESERV
dc730 45 44 20 6f 72 20 45 58 43 4c 55 53 49 56 45 20 ED or EXCLUSIVE
dc740 6c 6f 63 6b 2e 20 20 49 74 20 69 73 0a 20 20 20 lock. It is.
dc750 20 2a 2a 20 61 73 73 75 6d 65 64 20 74 68 61 74 ** assumed that
dc760 20 74 68 65 72 65 20 69 73 20 61 20 53 48 41 52 there is a SHAR
dc770 45 44 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f ED or greater lo
dc780 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 0a 20 ck on the file.
dc790 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 2e 0a 20 ** already..
dc7a0 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 66 61 */. int fa
dc7b0 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20 61 73 iled = 0;. as
dc7c0 73 65 72 74 28 20 30 21 3d 70 46 69 6c 65 2d 3e sert( 0!=pFile->
dc7d0 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 locktype );.
dc7e0 69 66 20 28 6c 6f 63 6b 74 79 70 65 20 3e 3d 20 if (locktype >=
dc7f0 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26 RESERVED_LOCK &&
dc800 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
dc810 20 3c 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b < RESERVED_LOCK
dc820 29 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 ) {. /* A
dc830 63 71 75 69 72 65 20 61 20 52 45 53 45 52 56 45 cquire a RESERVE
dc840 44 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 D lock */.
dc850 20 20 66 61 69 6c 65 64 20 3d 20 61 66 70 53 65 failed = afpSe
dc860 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 tLock(context->d
dc870 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 52 45 bPath, pFile, RE
dc880 53 45 52 56 45 44 5f 42 59 54 45 2c 20 31 2c 31 SERVED_BYTE, 1,1
dc890 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 20 );. }. if
dc8a0 28 21 66 61 69 6c 65 64 20 26 26 20 6c 6f 63 6b (!failed && lock
dc8b0 74 79 70 65 20 3d 3d 20 45 58 43 4c 55 53 49 56 type == EXCLUSIV
dc8c0 45 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 20 20 E_LOCK) {.
dc8d0 2f 2a 20 41 63 71 75 69 72 65 20 61 6e 20 45 58 /* Acquire an EX
dc8e0 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 2a 2f 0a CLUSIVE lock */.
dc8f0 20 20 20 20 20 20 20 20 0a 20 20 20 20 20 20 2f . /
dc900 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 73 68 61 * Remove the sha
dc910 72 65 64 20 6c 6f 63 6b 20 62 65 66 6f 72 65 20 red lock before
dc920 74 72 79 69 6e 67 20 74 68 65 20 72 61 6e 67 65 trying the range
dc930 2e 20 20 77 65 27 6c 6c 20 6e 65 65 64 20 74 6f . we'll need to
dc940 20 0a 20 20 20 20 20 20 2a 2a 20 72 65 65 73 74 . ** reest
dc950 61 62 6c 69 73 68 20 74 68 65 20 73 68 61 72 65 ablish the share
dc960 64 20 6c 6f 63 6b 20 69 66 20 77 65 20 63 61 6e d lock if we can
dc970 27 74 20 67 65 74 20 74 68 65 20 20 61 66 70 55 't get the afpU
dc980 6e 6c 6f 63 6b 0a 20 20 20 20 20 20 2a 2f 0a 20 nlock. */.
dc990 20 20 20 20 20 69 66 28 20 21 28 66 61 69 6c 65 if( !(faile
dc9a0 64 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 d = afpSetLock(c
dc9b0 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 ontext->dbPath,
dc9c0 70 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 49 pFile, SHARED_FI
dc9d0 52 53 54 20 2b 0a 20 20 20 20 20 20 20 20 20 20 RST +.
dc9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 c
dc9f0 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 42 79 ontext->sharedBy
dca00 74 65 2c 20 31 2c 20 30 29 29 20 29 7b 0a 20 20 te, 1, 0)) ){.
dca10 20 20 20 20 20 20 69 6e 74 20 66 61 69 6c 65 64 int failed
dca20 32 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 2 = SQLITE_OK;.
dca30 20 20 20 20 20 20 20 2f 2a 20 6e 6f 77 20 61 74 /* now at
dca40 74 65 6d 6d 70 74 20 74 6f 20 67 65 74 20 74 68 temmpt to get th
dca50 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b e exclusive lock
dca60 20 72 61 6e 67 65 20 2a 2f 0a 20 20 20 20 20 20 range */.
dca70 20 20 66 61 69 6c 65 64 20 3d 20 61 66 70 53 65 failed = afpSe
dca80 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 tLock(context->d
dca90 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 53 48 bPath, pFile, SH
dcaa0 41 52 45 44 5f 46 49 52 53 54 2c 20 0a 20 20 20 ARED_FIRST, .
dcab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dcac0 20 20 20 20 20 20 20 20 20 20 20 20 53 48 41 52 SHAR
dcad0 45 44 5f 53 49 5a 45 2c 20 31 29 3b 0a 20 20 20 ED_SIZE, 1);.
dcae0 20 20 20 20 20 69 66 28 20 66 61 69 6c 65 64 20 if( failed
dcaf0 26 26 20 28 66 61 69 6c 65 64 32 20 3d 20 61 66 && (failed2 = af
dcb00 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 pSetLock(context
dcb10 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c ->dbPath, pFile,
dcb20 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
dcb30 20 20 20 20 20 20 20 20 20 53 48 41 52 45 44 5f SHARED_
dcb40 46 49 52 53 54 20 2b 20 63 6f 6e 74 65 78 74 2d FIRST + context-
dcb50 3e 73 68 61 72 65 64 42 79 74 65 2c 20 31 2c 20 >sharedByte, 1,
dcb60 31 29 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 1)) ){.
dcb70 20 2f 2a 20 43 61 6e 27 74 20 72 65 65 73 74 61 /* Can't reesta
dcb80 62 6c 69 73 68 20 74 68 65 20 73 68 61 72 65 64 blish the shared
dcb90 20 6c 6f 63 6b 2e 20 20 53 71 6c 69 74 65 20 63 lock. Sqlite c
dcba0 61 6e 27 74 20 64 65 61 6c 2c 20 74 68 69 73 20 an't deal, this
dcbb0 69 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 is. **
dcbc0 61 20 63 72 69 74 69 63 61 6c 20 49 2f 4f 20 65 a critical I/O e
dcbd0 72 72 6f 72 0a 20 20 20 20 20 20 20 20 20 20 2a rror. *
dcbe0 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d /. rc =
dcbf0 20 28 28 66 61 69 6c 65 64 20 26 20 53 51 4c 49 ((failed & SQLI
dcc00 54 45 5f 49 4f 45 52 52 29 20 3d 3d 20 53 51 4c TE_IOERR) == SQL
dcc10 49 54 45 5f 49 4f 45 52 52 29 20 3f 20 66 61 69 ITE_IOERR) ? fai
dcc20 6c 65 64 32 20 3a 20 0a 20 20 20 20 20 20 20 20 led2 : .
dcc30 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4f SQLITE_IO
dcc40 45 52 52 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 ERR_LOCK;.
dcc50 20 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 goto afp_end
dcc60 5f 6c 6f 63 6b 3b 0a 20 20 20 20 20 20 20 20 7d _lock;. }
dcc70 20 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 . }else{.
dcc80 20 20 20 20 20 20 20 72 63 20 3d 20 66 61 69 6c rc = fail
dcc90 65 64 3b 20 0a 20 20 20 20 20 20 7d 0a 20 20 20 ed; . }.
dcca0 20 7d 0a 20 20 20 20 69 66 28 20 66 61 69 6c 65 }. if( faile
dccb0 64 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 d ){. rc =
dccc0 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 failed;. }.
dccd0 7d 0a 20 20 0a 20 20 69 66 28 20 72 63 3d 3d 53 }. . if( rc==S
dcce0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
dccf0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 pFile->locktype
dcd00 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 65 = locktype;. }e
dcd10 6c 73 65 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 lse if( locktype
dcd20 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b ==EXCLUSIVE_LOCK
dcd30 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c ){. pFile->l
dcd40 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e 44 49 4e ocktype = PENDIN
dcd50 47 5f 4c 4f 43 4b 3b 0a 20 20 7d 0a 20 20 0a 61 G_LOCK;. }. .a
dcd60 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 75 fp_end_lock:. u
dcd70 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b nixLeaveMutex();
dcd80 0a 20 20 4f 53 54 52 41 43 45 34 28 22 4c 4f 43 . OSTRACE4("LOC
dcd90 4b 20 20 20 20 25 64 20 25 73 20 25 73 5c 6e 22 K %d %s %s\n"
dcda0 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b , pFile->h, lock
dcdb0 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 typeName(locktyp
dcdc0 65 29 2c 20 0a 20 20 20 20 20 20 20 20 20 72 63 e), . rc
dcdd0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f ==SQLITE_OK ? "o
dcde0 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29 3b 0a k" : "failed");.
dcdf0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
dce00 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 /*.** Lower the
dce10 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e locking level on
dce20 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
dce30 20 70 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 pFile to lockty
dce40 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a pe. locktype.**
dce50 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 must be either
dce60 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 NO_LOCK or SHARE
dce70 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 D_LOCK..**.** If
dce80 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 the locking lev
dce90 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 el of the file d
dcea0 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 escriptor is alr
dceb0 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 eady at or below
dcec0 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 .** the requeste
dced0 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c d locking level,
dcee0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
dcef0 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 a no-op..*/.sta
dcf00 74 69 63 20 69 6e 74 20 61 66 70 55 6e 6c 6f 63 tic int afpUnloc
dcf10 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a k(sqlite3_file *
dcf20 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 id, int locktype
dcf30 29 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 ) {. int rc = S
dcf40 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78 QLITE_OK;. unix
dcf50 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 File *pFile = (u
dcf60 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 nixFile*)id;. a
dcf70 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 fpLockingContext
dcf80 20 2a 70 43 74 78 20 3d 20 28 61 66 70 4c 6f 63 *pCtx = (afpLoc
dcf90 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20 70 kingContext *) p
dcfa0 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e File->lockingCon
dcfb0 74 65 78 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 text;.. assert(
dcfc0 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 pFile );. OSTR
dcfd0 41 43 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64 ACE5("UNLOCK %d
dcfe0 20 25 64 20 77 61 73 20 25 64 20 70 69 64 3d 25 %d was %d pid=%
dcff0 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 d\n", pFile->h,
dd000 6c 6f 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 locktype,.
dd010 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 pFile->lockty
dd020 70 65 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a pe, getpid());..
dd030 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 assert( lockty
dd040 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 pe<=SHARED_LOCK
dd050 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e );. if( pFile->
dd060 6c 6f 63 6b 74 79 70 65 3c 3d 6c 6f 63 6b 74 79 locktype<=lockty
dd070 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e pe ){. return
dd080 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
dd090 20 20 69 66 28 20 43 48 45 43 4b 5f 54 48 52 45 if( CHECK_THRE
dd0a0 41 44 49 44 28 70 46 69 6c 65 29 20 29 7b 0a 20 ADID(pFile) ){.
dd0b0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
dd0c0 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 75 _MISUSE;. }. u
dd0d0 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b nixEnterMutex();
dd0e0 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f . if( pFile->lo
dd0f0 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f cktype>SHARED_LO
dd100 43 4b 20 29 7b 0a 20 20 20 20 0a 20 20 20 20 69 CK ){. . i
dd110 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 f( pFile->lockty
dd120 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f pe==EXCLUSIVE_LO
dd130 43 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d CK ){. rc =
dd140 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 78 afpSetLock(pCtx
dd150 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c ->dbPath, pFile,
dd160 20 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20 53 SHARED_FIRST, S
dd170 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a HARED_SIZE, 0);.
dd180 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
dd190 4c 49 54 45 5f 4f 4b 20 26 26 20 6c 6f 63 6b 74 LITE_OK && lockt
dd1a0 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype==SHARED_LOCK
dd1b0 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 6f ){. /* o
dd1c0 6e 6c 79 20 72 65 2d 65 73 74 61 62 6c 69 73 68 nly re-establish
dd1d0 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b the shared lock
dd1e0 20 69 66 20 6e 65 63 65 73 73 61 72 79 20 2a 2f if necessary */
dd1f0 0a 20 20 20 20 20 20 20 20 69 6e 74 20 73 68 61 . int sha
dd200 72 65 64 4c 6f 63 6b 42 79 74 65 20 3d 20 53 48 redLockByte = SH
dd210 41 52 45 44 5f 46 49 52 53 54 2b 70 43 74 78 2d ARED_FIRST+pCtx-
dd220 3e 73 68 61 72 65 64 42 79 74 65 3b 0a 20 20 20 >sharedByte;.
dd230 20 20 20 20 20 72 63 20 3d 20 61 66 70 53 65 74 rc = afpSet
dd240 4c 6f 63 6b 28 70 43 74 78 2d 3e 64 62 50 61 74 Lock(pCtx->dbPat
dd250 68 2c 20 70 46 69 6c 65 2c 20 73 68 61 72 65 64 h, pFile, shared
dd260 4c 6f 63 6b 42 79 74 65 2c 20 31 2c 20 31 29 3b LockByte, 1, 1);
dd270 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
dd280 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
dd290 45 5f 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c E_OK && pFile->l
dd2a0 6f 63 6b 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47 ocktype>=PENDING
dd2b0 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 72 _LOCK ){. r
dd2c0 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 c = afpSetLock(p
dd2d0 43 74 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 Ctx->dbPath, pFi
dd2e0 6c 65 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 le, PENDING_BYTE
dd2f0 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 7d 20 0a , 1, 0);. } .
dd300 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
dd310 54 45 5f 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e TE_OK && pFile->
dd320 6c 6f 63 6b 74 79 70 65 3e 3d 52 45 53 45 52 56 locktype>=RESERV
dd330 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 ED_LOCK ){.
dd340 20 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b rc = afpSetLock
dd350 28 70 43 74 78 2d 3e 64 62 50 61 74 68 2c 20 70 (pCtx->dbPath, p
dd360 46 69 6c 65 2c 20 52 45 53 45 52 56 45 44 5f 42 File, RESERVED_B
dd370 59 54 45 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 YTE, 1, 0);.
dd380 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f }. }else if( lo
dd390 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 cktype==NO_LOCK
dd3a0 29 7b 0a 20 20 20 20 2f 2a 20 63 6c 65 61 72 20 ){. /* clear
dd3b0 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 the shared lock
dd3c0 2a 2f 0a 20 20 20 20 69 6e 74 20 73 68 61 72 65 */. int share
dd3d0 64 4c 6f 63 6b 42 79 74 65 20 3d 20 53 48 41 52 dLockByte = SHAR
dd3e0 45 44 5f 46 49 52 53 54 2b 70 43 74 78 2d 3e 73 ED_FIRST+pCtx->s
dd3f0 68 61 72 65 64 42 79 74 65 3b 0a 20 20 20 20 72 haredByte;. r
dd400 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 c = afpSetLock(p
dd410 43 74 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 Ctx->dbPath, pFi
dd420 6c 65 2c 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 le, sharedLockBy
dd430 74 65 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 0a te, 1, 0);. }..
dd440 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
dd450 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 6c _OK ){. if( l
dd460 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b ocktype==NO_LOCK
dd470 20 29 7b 0a 20 20 20 20 20 20 73 74 72 75 63 74 ){. struct
dd480 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f unixOpenCnt *pO
dd490 70 65 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 pen = pFile->pOp
dd4a0 65 6e 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d en;. pOpen-
dd4b0 3e 6e 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 20 20 >nLock--;.
dd4c0 61 73 73 65 72 74 28 20 70 4f 70 65 6e 2d 3e 6e assert( pOpen->n
dd4d0 4c 6f 63 6b 3e 3d 30 20 29 3b 0a 20 20 20 20 20 Lock>=0 );.
dd4e0 20 69 66 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 if( pOpen->nLoc
dd4f0 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 k==0 ){.
dd500 72 63 20 3d 20 63 6c 6f 73 65 50 65 6e 64 69 6e rc = closePendin
dd510 67 46 64 73 28 70 46 69 6c 65 29 3b 0a 20 20 20 gFds(pFile);.
dd520 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
dd530 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 unixLeaveMutex(
dd540 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
dd550 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 46 ITE_OK ){. pF
dd560 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 ile->locktype =
dd570 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 20 locktype;. }.
dd580 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
dd590 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 .** Close a file
dd5a0 20 26 20 63 6c 65 61 6e 75 70 20 41 46 50 20 73 & cleanup AFP s
dd5b0 70 65 63 69 66 69 63 20 6c 6f 63 6b 69 6e 67 20 pecific locking
dd5c0 63 6f 6e 74 65 78 74 20 0a 2a 2f 0a 73 74 61 74 context .*/.stat
dd5d0 69 63 20 69 6e 74 20 61 66 70 43 6c 6f 73 65 28 ic int afpClose(
dd5e0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
dd5f0 29 20 7b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a ) {. if( id ){.
dd600 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 unixFile *pF
dd610 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a ile = (unixFile*
dd620 29 69 64 3b 0a 20 20 20 20 61 66 70 55 6e 6c 6f )id;. afpUnlo
dd630 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b ck(id, NO_LOCK);
dd640 0a 20 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 . unixEnterMu
dd650 74 65 78 28 29 3b 0a 20 20 20 20 69 66 28 20 70 tex();. if( p
dd660 46 69 6c 65 2d 3e 70 4f 70 65 6e 20 26 26 20 70 File->pOpen && p
dd670 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f File->pOpen->nLo
dd680 63 6b 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 ck ){. /* I
dd690 66 20 74 68 65 72 65 20 61 72 65 20 6f 75 74 73 f there are outs
dd6a0 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 2c 20 64 tanding locks, d
dd6b0 6f 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 63 o not actually c
dd6c0 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 20 6a 75 lose the file ju
dd6d0 73 74 0a 20 20 20 20 20 20 2a 2a 20 79 65 74 20 st. ** yet
dd6e0 62 65 63 61 75 73 65 20 74 68 61 74 20 77 6f 75 because that wou
dd6f0 6c 64 20 63 6c 65 61 72 20 74 68 6f 73 65 20 6c ld clear those l
dd700 6f 63 6b 73 2e 20 20 49 6e 73 74 65 61 64 2c 20 ocks. Instead,
dd710 61 64 64 20 74 68 65 20 66 69 6c 65 0a 20 20 20 add the file.
dd720 20 20 20 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 ** descriptor
dd730 20 74 6f 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 to pOpen->aPend
dd740 69 6e 67 2e 20 20 49 74 20 77 69 6c 6c 20 62 65 ing. It will be
dd750 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 automatically c
dd760 6c 6f 73 65 64 20 77 68 65 6e 0a 20 20 20 20 20 losed when.
dd770 20 2a 2a 20 74 68 65 20 6c 61 73 74 20 6c 6f 63 ** the last loc
dd780 6b 20 69 73 20 63 6c 65 61 72 65 64 2e 0a 20 20 k is cleared..
dd790 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 65 74 */. set
dd7a0 50 65 6e 64 69 6e 67 46 64 28 70 46 69 6c 65 29 PendingFd(pFile)
dd7b0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 6c 65 ;. }. rele
dd7c0 61 73 65 4f 70 65 6e 43 6e 74 28 70 46 69 6c 65 aseOpenCnt(pFile
dd7d0 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 73 71 ->pOpen);. sq
dd7e0 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69 6c 65 lite3_free(pFile
dd7f0 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 ->lockingContext
dd800 29 3b 0a 20 20 20 20 63 6c 6f 73 65 55 6e 69 78 );. closeUnix
dd810 46 69 6c 65 28 69 64 29 3b 0a 20 20 20 20 75 6e File(id);. un
dd820 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a ixLeaveMutex();.
dd830 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
dd840 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 ITE_OK;.}..#endi
dd850 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 5f 5f 41 f /* defined(__A
dd860 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 PPLE__) && SQLIT
dd870 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 E_ENABLE_LOCKING
dd880 5f 53 54 59 4c 45 20 2a 2f 0a 2f 2a 0a 2a 2a 20 _STYLE */./*.**
dd890 54 68 65 20 63 6f 64 65 20 61 62 6f 76 65 20 69 The code above i
dd8a0 73 20 74 68 65 20 41 46 50 20 6c 6f 63 6b 20 69 s the AFP lock i
dd8b0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 mplementation.
dd8c0 54 68 65 20 63 6f 64 65 20 69 73 20 73 70 65 63 The code is spec
dd8d0 69 66 69 63 0a 2a 2a 20 74 6f 20 4d 61 63 4f 53 ific.** to MacOS
dd8e0 58 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 77 X and does not w
dd8f0 6f 72 6b 20 6f 6e 20 6f 74 68 65 72 20 75 6e 69 ork on other uni
dd900 78 20 70 6c 61 74 66 6f 72 6d 73 2e 20 20 4e 6f x platforms. No
dd910 20 61 6c 74 65 72 6e 61 74 69 76 65 0a 2a 2a 20 alternative.**
dd920 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 49 is available. I
dd930 66 20 79 6f 75 20 64 6f 6e 27 74 20 63 6f 6d 70 f you don't comp
dd940 69 6c 65 20 66 6f 72 20 61 20 6d 61 63 2c 20 74 ile for a mac, t
dd950 68 65 6e 20 74 68 65 20 22 75 6e 69 78 2d 61 66 hen the "unix-af
dd960 70 22 0a 2a 2a 20 56 46 53 20 69 73 20 6e 6f 74 p".** VFS is not
dd970 20 61 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a available..**.*
dd980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd990 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 **** End of the
dd9a0 41 46 50 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 AFP lock impleme
dd9b0 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a ntation ********
dd9c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
dd9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd9f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dda00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dda10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
dda20 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
dda30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dda40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dda50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dda60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dda70 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
dda80 2a 20 4e 6f 6e 2d 6c 6f 63 6b 69 6e 67 20 73 71 * Non-locking sq
dda90 6c 69 74 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f lite3_file metho
ddaa0 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ds *************
ddab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ddac0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 .**.** The next
ddad0 64 69 76 69 73 69 6f 6e 20 63 6f 6e 74 61 69 6e division contain
ddae0 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e s implementation
ddaf0 73 20 66 6f 72 20 61 6c 6c 20 6d 65 74 68 6f 64 s for all method
ddb00 73 20 6f 66 20 74 68 65 20 0a 2a 2a 20 73 71 6c s of the .** sql
ddb10 69 74 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 ite3_file object
ddb20 20 6f 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 other than the
ddb30 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 73 2e locking methods.
ddb40 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a The locking.**
ddb50 20 6d 65 74 68 6f 64 73 20 77 65 72 65 20 64 65 methods were de
ddb60 66 69 6e 65 64 20 69 6e 20 64 69 76 69 73 69 6f fined in divisio
ddb70 6e 73 20 61 62 6f 76 65 20 28 6f 6e 65 20 6c 6f ns above (one lo
ddb80 63 6b 69 6e 67 20 6d 65 74 68 6f 64 20 70 65 72 cking method per
ddb90 0a 2a 2a 20 64 69 76 69 73 69 6f 6e 29 2e 20 20 .** division).
ddba0 54 68 6f 73 65 20 6d 65 74 68 6f 64 73 20 74 68 Those methods th
ddbb0 61 74 20 61 72 65 20 63 6f 6d 6d 6f 6e 20 74 6f at are common to
ddbc0 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 all locking mod
ddbd0 65 73 0a 2a 2a 20 61 72 65 20 67 61 74 68 65 72 es.** are gather
ddbe0 20 74 6f 67 65 74 68 65 72 20 69 6e 74 6f 20 74 together into t
ddbf0 68 69 73 20 64 69 76 69 73 69 6f 6e 2e 0a 2a 2f his division..*/
ddc00 0a 0a 2f 2a 0a 2a 2a 20 53 65 65 6b 20 74 6f 20 ../*.** Seek to
ddc10 74 68 65 20 6f 66 66 73 65 74 20 70 61 73 73 65 the offset passe
ddc20 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 d as the second
ddc30 61 72 67 75 6d 65 6e 74 2c 20 74 68 65 6e 20 72 argument, then r
ddc40 65 61 64 20 63 6e 74 20 0a 2a 2a 20 62 79 74 65 ead cnt .** byte
ddc50 73 20 69 6e 74 6f 20 70 42 75 66 2e 20 52 65 74 s into pBuf. Ret
ddc60 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
ddc70 66 20 62 79 74 65 73 20 61 63 74 75 61 6c 6c 79 f bytes actually
ddc80 20 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 4e 42 3a read..**.** NB:
ddc90 20 20 49 66 20 79 6f 75 20 64 65 66 69 6e 65 20 If you define
ddca0 55 53 45 5f 50 52 45 41 44 20 6f 72 20 55 53 45 USE_PREAD or USE
ddcb0 5f 50 52 45 41 44 36 34 2c 20 74 68 65 6e 20 69 _PREAD64, then i
ddcc0 74 20 6d 69 67 68 74 20 61 6c 73 6f 0a 2a 2a 20 t might also.**
ddcd0 62 65 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 be necessary to
ddce0 64 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f define _XOPEN_SO
ddcf0 55 52 43 45 20 74 6f 20 62 65 20 35 30 30 2e 20 URCE to be 500.
ddd00 20 54 68 69 73 20 76 61 72 69 65 73 20 66 72 6f This varies fro
ddd10 6d 0a 2a 2a 20 6f 6e 65 20 73 79 73 74 65 6d 20 m.** one system
ddd20 74 6f 20 61 6e 6f 74 68 65 72 2e 20 20 53 69 6e to another. Sin
ddd30 63 65 20 53 51 4c 69 74 65 20 64 6f 65 73 20 6e ce SQLite does n
ddd40 6f 74 20 64 65 66 69 6e 65 20 55 53 45 5f 50 52 ot define USE_PR
ddd50 45 41 44 0a 2a 2a 20 61 6e 79 20 61 6e 79 20 66 EAD.** any any f
ddd60 6f 72 6d 20 62 79 20 64 65 66 61 75 6c 74 2c 20 orm by default,
ddd70 77 65 20 77 69 6c 6c 20 6e 6f 74 20 61 74 74 65 we will not atte
ddd80 6d 70 74 20 74 6f 20 64 65 66 69 6e 65 20 5f 58 mpt to define _X
ddd90 4f 50 45 4e 5f 53 4f 55 52 43 45 2e 0a 2a 2a 20 OPEN_SOURCE..**
ddda0 53 65 65 20 74 69 63 6b 65 74 73 20 23 32 37 34 See tickets #274
dddb0 31 20 61 6e 64 20 23 32 36 38 31 2e 0a 2a 2a 0a 1 and #2681..**.
dddc0 2a 2a 20 54 6f 20 61 76 6f 69 64 20 73 74 6f 6d ** To avoid stom
dddd0 70 69 6e 67 20 74 68 65 20 65 72 72 6e 6f 20 76 ping the errno v
ddde0 61 6c 75 65 20 6f 6e 20 61 20 66 61 69 6c 65 64 alue on a failed
dddf0 20 72 65 61 64 20 74 68 65 20 6c 61 73 74 45 72 read the lastEr
dde00 72 6e 6f 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 rno value.** is
dde10 73 65 74 20 62 65 66 6f 72 65 20 72 65 74 75 72 set before retur
dde20 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ning..*/.static
dde30 69 6e 74 20 73 65 65 6b 41 6e 64 52 65 61 64 28 int seekAndRead(
dde40 75 6e 69 78 46 69 6c 65 20 2a 69 64 2c 20 73 71 unixFile *id, sq
dde50 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 lite3_int64 offs
dde60 65 74 2c 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 et, void *pBuf,
dde70 69 6e 74 20 63 6e 74 29 7b 0a 20 20 69 6e 74 20 int cnt){. int
dde80 67 6f 74 3b 0a 20 20 69 36 34 20 6e 65 77 4f 66 got;. i64 newOf
dde90 66 73 65 74 3b 0a 20 20 54 49 4d 45 52 5f 53 54 fset;. TIMER_ST
ddea0 41 52 54 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 ART;.#if defined
ddeb0 28 55 53 45 5f 50 52 45 41 44 29 0a 20 20 67 6f (USE_PREAD). go
ddec0 74 20 3d 20 70 72 65 61 64 28 69 64 2d 3e 68 2c t = pread(id->h,
dded0 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73 pBuf, cnt, offs
ddee0 65 74 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 et);. SimulateI
ddef0 4f 45 72 72 6f 72 28 20 67 6f 74 20 3d 20 2d 31 OError( got = -1
ddf00 20 29 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 );.#elif define
ddf10 64 28 55 53 45 5f 50 52 45 41 44 36 34 29 0a 20 d(USE_PREAD64).
ddf20 20 67 6f 74 20 3d 20 70 72 65 61 64 36 34 28 69 got = pread64(i
ddf30 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c d->h, pBuf, cnt,
ddf40 20 6f 66 66 73 65 74 29 3b 0a 20 20 53 69 6d 75 offset);. Simu
ddf50 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 67 6f 74 lateIOError( got
ddf60 20 3d 20 2d 31 20 29 3b 0a 23 65 6c 73 65 0a 20 = -1 );.#else.
ddf70 20 6e 65 77 4f 66 66 73 65 74 20 3d 20 6c 73 65 newOffset = lse
ddf80 65 6b 28 69 64 2d 3e 68 2c 20 6f 66 66 73 65 74 ek(id->h, offset
ddf90 2c 20 53 45 45 4b 5f 53 45 54 29 3b 0a 20 20 53 , SEEK_SET);. S
ddfa0 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 imulateIOError(
ddfb0 6e 65 77 4f 66 66 73 65 74 2d 2d 20 29 3b 0a 20 newOffset-- );.
ddfc0 20 69 66 28 20 6e 65 77 4f 66 66 73 65 74 21 3d if( newOffset!=
ddfd0 6f 66 66 73 65 74 20 29 7b 0a 20 20 20 20 69 66 offset ){. if
ddfe0 28 20 6e 65 77 4f 66 66 73 65 74 20 3d 3d 20 2d ( newOffset == -
ddff0 31 20 29 7b 0a 20 20 20 20 20 20 28 28 75 6e 69 1 ){. ((uni
de000 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 xFile*)id)->last
de010 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
de020 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
de030 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d ((unixFile*)id)-
de040 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b 09 >lastErrno = 0;.
de050 09 09 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 ... }. ret
de060 75 72 6e 20 2d 31 3b 0a 20 20 7d 0a 20 20 67 6f urn -1;. }. go
de070 74 20 3d 20 72 65 61 64 28 69 64 2d 3e 68 2c 20 t = read(id->h,
de080 70 42 75 66 2c 20 63 6e 74 29 3b 0a 23 65 6e 64 pBuf, cnt);.#end
de090 69 66 0a 20 20 54 49 4d 45 52 5f 45 4e 44 3b 0a if. TIMER_END;.
de0a0 20 20 69 66 28 20 67 6f 74 3c 30 20 29 7b 0a 20 if( got<0 ){.
de0b0 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 ((unixFile*)i
de0c0 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 d)->lastErrno =
de0d0 65 72 72 6e 6f 3b 0a 20 20 7d 0a 20 20 4f 53 54 errno;. }. OST
de0e0 52 41 43 45 35 28 22 52 45 41 44 20 20 20 20 25 RACE5("READ %
de0f0 2d 33 64 20 25 35 64 20 25 37 6c 6c 64 20 25 6c -3d %5d %7lld %l
de100 6c 75 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 67 6f lu\n", id->h, go
de110 74 2c 20 6f 66 66 73 65 74 2c 20 54 49 4d 45 52 t, offset, TIMER
de120 5f 45 4c 41 50 53 45 44 29 3b 0a 20 20 72 65 74 _ELAPSED);. ret
de130 75 72 6e 20 67 6f 74 3b 0a 7d 0a 0a 2f 2a 0a 2a urn got;.}../*.*
de140 2a 20 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d * Read data from
de150 20 61 20 66 69 6c 65 20 69 6e 74 6f 20 61 20 62 a file into a b
de160 75 66 66 65 72 2e 20 20 52 65 74 75 72 6e 20 53 uffer. Return S
de170 51 4c 49 54 45 5f 4f 4b 20 69 66 20 61 6c 6c 0a QLITE_OK if all.
de180 2a 2a 20 62 79 74 65 73 20 77 65 72 65 20 72 65 ** bytes were re
de190 61 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 ad successfully
de1a0 61 6e 64 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 and SQLITE_IOERR
de1b0 20 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 if anything goe
de1c0 73 0a 2a 2a 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 s.** wrong..*/.s
de1d0 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 52 65 tatic int unixRe
de1e0 61 64 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 ad(. sqlite3_fi
de1f0 6c 65 20 2a 69 64 2c 20 0a 20 20 76 6f 69 64 20 le *id, . void
de200 2a 70 42 75 66 2c 20 0a 20 20 69 6e 74 20 61 6d *pBuf, . int am
de210 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 t,. sqlite3_int
de220 36 34 20 6f 66 66 73 65 74 0a 29 7b 0a 20 20 75 64 offset.){. u
de230 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d nixFile *pFile =
de240 20 28 75 6e 69 78 46 69 6c 65 20 2a 29 69 64 3b (unixFile *)id;
de250 0a 20 20 69 6e 74 20 67 6f 74 3b 0a 20 20 61 73 . int got;. as
de260 73 65 72 74 28 20 69 64 20 29 3b 0a 0a 20 20 2f sert( id );.. /
de270 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 64 * If this is a d
de280 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 6e 6f atabase file (no
de290 74 20 61 20 6a 6f 75 72 6e 61 6c 2c 20 6d 61 73 t a journal, mas
de2a0 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 6f 72 20 74 ter-journal or t
de2b0 65 6d 70 0a 20 20 2a 2a 20 66 69 6c 65 29 2c 20 emp. ** file),
de2c0 74 68 65 20 62 79 74 65 73 20 69 6e 20 74 68 65 the bytes in the
de2d0 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67 65 20 73 locking range s
de2e0 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65 20 72 hould never be r
de2f0 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e 2e 20 ead or written.
de300 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 */. assert( pFi
de310 6c 65 2d 3e 70 55 6e 75 73 65 64 3d 3d 30 0a 20 le->pUnused==0.
de320 20 20 20 20 20 20 7c 7c 20 6f 66 66 73 65 74 3e || offset>
de330 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 35 31 =PENDING_BYTE+51
de340 32 0a 20 20 20 20 20 20 20 7c 7c 20 6f 66 66 73 2. || offs
de350 65 74 2b 61 6d 74 3c 3d 50 45 4e 44 49 4e 47 5f et+amt<=PENDING_
de360 42 59 54 45 20 0a 20 20 29 3b 0a 0a 20 20 67 6f BYTE . );.. go
de370 74 20 3d 20 73 65 65 6b 41 6e 64 52 65 61 64 28 t = seekAndRead(
de380 70 46 69 6c 65 2c 20 6f 66 66 73 65 74 2c 20 70 pFile, offset, p
de390 42 75 66 2c 20 61 6d 74 29 3b 0a 20 20 69 66 28 Buf, amt);. if(
de3a0 20 67 6f 74 3d 3d 61 6d 74 20 29 7b 0a 20 20 20 got==amt ){.
de3b0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
de3c0 4b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 67 K;. }else if( g
de3d0 6f 74 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20 6c ot<0 ){. /* l
de3e0 61 73 74 45 72 72 6e 6f 20 73 65 74 20 62 79 20 astErrno set by
de3f0 73 65 65 6b 41 6e 64 52 65 61 64 20 2a 2f 0a 20 seekAndRead */.
de400 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
de410 5f 49 4f 45 52 52 5f 52 45 41 44 3b 0a 20 20 7d _IOERR_READ;. }
de420 65 6c 73 65 7b 0a 20 20 20 20 70 46 69 6c 65 2d else{. pFile-
de430 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b 20 >lastErrno = 0;
de440 2f 2a 20 6e 6f 74 20 61 20 73 79 73 74 65 6d 20 /* not a system
de450 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 2f 2a 20 error */. /*
de460 55 6e 72 65 61 64 20 70 61 72 74 73 20 6f 66 20 Unread parts of
de470 74 68 65 20 62 75 66 66 65 72 20 6d 75 73 74 20 the buffer must
de480 62 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a be zero-filled *
de490 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 /. memset(&((
de4a0 63 68 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d char*)pBuf)[got]
de4b0 2c 20 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 , 0, amt-got);.
de4c0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
de4d0 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 _IOERR_SHORT_REA
de4e0 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 D;. }.}../*.**
de4f0 53 65 65 6b 20 74 6f 20 74 68 65 20 6f 66 66 73 Seek to the offs
de500 65 74 20 69 6e 20 69 64 2d 3e 6f 66 66 73 65 74 et in id->offset
de510 20 74 68 65 6e 20 72 65 61 64 20 63 6e 74 20 62 then read cnt b
de520 79 74 65 73 20 69 6e 74 6f 20 70 42 75 66 2e 0a ytes into pBuf..
de530 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
de540 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 63 mber of bytes ac
de550 74 75 61 6c 6c 79 20 72 65 61 64 2e 20 20 55 70 tually read. Up
de560 64 61 74 65 20 74 68 65 20 6f 66 66 73 65 74 2e date the offset.
de570 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 .**.** To avoid
de580 73 74 6f 6d 70 69 6e 67 20 74 68 65 20 65 72 72 stomping the err
de590 6e 6f 20 76 61 6c 75 65 20 6f 6e 20 61 20 66 61 no value on a fa
de5a0 69 6c 65 64 20 77 72 69 74 65 20 74 68 65 20 6c iled write the l
de5b0 61 73 74 45 72 72 6e 6f 20 76 61 6c 75 65 0a 2a astErrno value.*
de5c0 2a 20 69 73 20 73 65 74 20 62 65 66 6f 72 65 20 * is set before
de5d0 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 returning..*/.st
de5e0 61 74 69 63 20 69 6e 74 20 73 65 65 6b 41 6e 64 atic int seekAnd
de5f0 57 72 69 74 65 28 75 6e 69 78 46 69 6c 65 20 2a Write(unixFile *
de600 69 64 2c 20 69 36 34 20 6f 66 66 73 65 74 2c 20 id, i64 offset,
de610 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66 const void *pBuf
de620 2c 20 69 6e 74 20 63 6e 74 29 7b 0a 20 20 69 6e , int cnt){. in
de630 74 20 67 6f 74 3b 0a 20 20 69 36 34 20 6e 65 77 t got;. i64 new
de640 4f 66 66 73 65 74 3b 0a 20 20 54 49 4d 45 52 5f Offset;. TIMER_
de650 53 54 41 52 54 3b 0a 23 69 66 20 64 65 66 69 6e START;.#if defin
de660 65 64 28 55 53 45 5f 50 52 45 41 44 29 0a 20 20 ed(USE_PREAD).
de670 67 6f 74 20 3d 20 70 77 72 69 74 65 28 69 64 2d got = pwrite(id-
de680 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f >h, pBuf, cnt, o
de690 66 66 73 65 74 29 3b 0a 23 65 6c 69 66 20 64 65 ffset);.#elif de
de6a0 66 69 6e 65 64 28 55 53 45 5f 50 52 45 41 44 36 fined(USE_PREAD6
de6b0 34 29 0a 20 20 67 6f 74 20 3d 20 70 77 72 69 74 4). got = pwrit
de6c0 65 36 34 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c e64(id->h, pBuf,
de6d0 20 63 6e 74 2c 20 6f 66 66 73 65 74 29 3b 0a 23 cnt, offset);.#
de6e0 65 6c 73 65 0a 20 20 6e 65 77 4f 66 66 73 65 74 else. newOffset
de6f0 20 3d 20 6c 73 65 65 6b 28 69 64 2d 3e 68 2c 20 = lseek(id->h,
de700 6f 66 66 73 65 74 2c 20 53 45 45 4b 5f 53 45 54 offset, SEEK_SET
de710 29 3b 0a 20 20 69 66 28 20 6e 65 77 4f 66 66 73 );. if( newOffs
de720 65 74 21 3d 6f 66 66 73 65 74 20 29 7b 0a 20 20 et!=offset ){.
de730 20 20 69 66 28 20 6e 65 77 4f 66 66 73 65 74 20 if( newOffset
de740 3d 3d 20 2d 31 20 29 7b 0a 20 20 20 20 20 20 28 == -1 ){. (
de750 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e (unixFile*)id)->
de760 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e lastErrno = errn
de770 6f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 o;. }else{.
de780 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 ((unixFile*)
de790 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d id)->lastErrno =
de7a0 20 30 3b 09 09 09 0a 20 20 20 20 7d 0a 20 20 20 0;.... }.
de7b0 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 7d 0a return -1;. }.
de7c0 20 20 67 6f 74 20 3d 20 77 72 69 74 65 28 69 64 got = write(id
de7d0 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 29 3b ->h, pBuf, cnt);
de7e0 0a 23 65 6e 64 69 66 0a 20 20 54 49 4d 45 52 5f .#endif. TIMER_
de7f0 45 4e 44 3b 0a 20 20 69 66 28 20 67 6f 74 3c 30 END;. if( got<0
de800 20 29 7b 0a 20 20 20 20 28 28 75 6e 69 78 46 69 ){. ((unixFi
de810 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 le*)id)->lastErr
de820 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 7d 0a no = errno;. }.
de830 0a 20 20 4f 53 54 52 41 43 45 35 28 22 57 52 49 . OSTRACE5("WRI
de840 54 45 20 20 20 25 2d 33 64 20 25 35 64 20 25 37 TE %-3d %5d %7
de850 6c 6c 64 20 25 6c 6c 75 5c 6e 22 2c 20 69 64 2d lld %llu\n", id-
de860 3e 68 2c 20 67 6f 74 2c 20 6f 66 66 73 65 74 2c >h, got, offset,
de870 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 29 3b TIMER_ELAPSED);
de880 0a 20 20 72 65 74 75 72 6e 20 67 6f 74 3b 0a 7d . return got;.}
de890 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 .../*.** Write d
de8a0 61 74 61 20 66 72 6f 6d 20 61 20 62 75 66 66 65 ata from a buffe
de8b0 72 20 69 6e 74 6f 20 61 20 66 69 6c 65 2e 20 20 r into a file.
de8c0 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b Return SQLITE_OK
de8d0 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f on success.** o
de8e0 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 72 r some other err
de8f0 6f 72 20 63 6f 64 65 20 6f 6e 20 66 61 69 6c 75 or code on failu
de900 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e re..*/.static in
de910 74 20 75 6e 69 78 57 72 69 74 65 28 0a 20 20 73 t unixWrite(. s
de920 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
de930 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a . const void *
de940 70 42 75 66 2c 20 0a 20 20 69 6e 74 20 61 6d 74 pBuf, . int amt
de950 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 ,. sqlite3_int6
de960 34 20 6f 66 66 73 65 74 20 0a 29 7b 0a 20 20 75 4 offset .){. u
de970 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d nixFile *pFile =
de980 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a (unixFile*)id;.
de990 20 20 69 6e 74 20 77 72 6f 74 65 20 3d 20 30 3b int wrote = 0;
de9a0 0a 20 20 61 73 73 65 72 74 28 20 69 64 20 29 3b . assert( id );
de9b0 0a 20 20 61 73 73 65 72 74 28 20 61 6d 74 3e 30 . assert( amt>0
de9c0 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 );.. /* If thi
de9d0 73 20 69 73 20 61 20 64 61 74 61 62 61 73 65 20 s is a database
de9e0 66 69 6c 65 20 28 6e 6f 74 20 61 20 6a 6f 75 72 file (not a jour
de9f0 6e 61 6c 2c 20 6d 61 73 74 65 72 2d 6a 6f 75 72 nal, master-jour
dea00 6e 61 6c 20 6f 72 20 74 65 6d 70 0a 20 20 2a 2a nal or temp. **
dea10 20 66 69 6c 65 29 2c 20 74 68 65 20 62 79 74 65 file), the byte
dea20 73 20 69 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 s in the locking
dea30 20 72 61 6e 67 65 20 73 68 6f 75 6c 64 20 6e 65 range should ne
dea40 76 65 72 20 62 65 20 72 65 61 64 20 6f 72 20 77 ver be read or w
dea50 72 69 74 74 65 6e 2e 20 2a 2f 0a 20 20 61 73 73 ritten. */. ass
dea60 65 72 74 28 20 70 46 69 6c 65 2d 3e 70 55 6e 75 ert( pFile->pUnu
dea70 73 65 64 3d 3d 30 0a 20 20 20 20 20 20 20 7c 7c sed==0. ||
dea80 20 6f 66 66 73 65 74 3e 3d 50 45 4e 44 49 4e 47 offset>=PENDING
dea90 5f 42 59 54 45 2b 35 31 32 0a 20 20 20 20 20 20 _BYTE+512.
deaa0 20 7c 7c 20 6f 66 66 73 65 74 2b 61 6d 74 3c 3d || offset+amt<=
deab0 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 0a 20 20 PENDING_BYTE .
deac0 29 3b 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 );..#ifndef NDEB
dead0 55 47 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 72 UG. /* If we ar
deae0 65 20 64 6f 69 6e 67 20 61 20 6e 6f 72 6d 61 6c e doing a normal
deaf0 20 77 72 69 74 65 20 74 6f 20 61 20 64 61 74 61 write to a data
deb00 62 61 73 65 20 66 69 6c 65 20 28 61 73 20 6f 70 base file (as op
deb10 70 6f 73 65 64 20 74 6f 0a 20 20 2a 2a 20 64 6f posed to. ** do
deb20 69 6e 67 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 ing a hot-journa
deb30 6c 20 72 6f 6c 6c 62 61 63 6b 20 6f 72 20 61 20 l rollback or a
deb40 77 72 69 74 65 20 74 6f 20 73 6f 6d 65 20 66 69 write to some fi
deb50 6c 65 20 6f 74 68 65 72 20 74 68 61 6e 20 61 0a le other than a.
deb60 20 20 2a 2a 20 6e 6f 72 6d 61 6c 20 64 61 74 61 ** normal data
deb70 62 61 73 65 20 66 69 6c 65 29 20 74 68 65 6e 20 base file) then
deb80 72 65 63 6f 72 64 20 74 68 65 20 66 61 63 74 20 record the fact
deb90 74 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 that the databas
deba0 65 0a 20 20 2a 2a 20 68 61 73 20 63 68 61 6e 67 e. ** has chang
debb0 65 64 2e 20 20 49 66 20 74 68 65 20 74 72 61 6e ed. If the tran
debc0 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 saction counter
debd0 69 73 20 6d 6f 64 69 66 69 65 64 2c 20 72 65 63 is modified, rec
debe0 6f 72 64 20 74 68 61 74 0a 20 20 2a 2a 20 66 61 ord that. ** fa
debf0 63 74 20 74 6f 6f 2e 0a 20 20 2a 2f 0a 20 20 69 ct too.. */. i
dec00 66 28 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d f( pFile->inNorm
dec10 61 6c 57 72 69 74 65 20 29 7b 0a 20 20 20 20 70 alWrite ){. p
dec20 46 69 6c 65 2d 3e 64 62 55 70 64 61 74 65 20 3d File->dbUpdate =
dec30 20 31 3b 20 20 2f 2a 20 54 68 65 20 64 61 74 61 1; /* The data
dec40 62 61 73 65 20 68 61 73 20 62 65 65 6e 20 6d 6f base has been mo
dec50 64 69 66 69 65 64 20 2a 2f 0a 20 20 20 20 69 66 dified */. if
dec60 28 20 6f 66 66 73 65 74 3c 3d 32 34 20 26 26 20 ( offset<=24 &&
dec70 6f 66 66 73 65 74 2b 61 6d 74 3e 3d 32 37 20 29 offset+amt>=27 )
dec80 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 3b 0a {. int rc;.
dec90 20 20 20 20 20 20 63 68 61 72 20 6f 6c 64 43 6e char oldCn
deca0 74 72 5b 34 5d 3b 0a 20 20 20 20 20 20 53 69 6d tr[4];. Sim
decb0 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 ulateIOErrorBeni
decc0 67 6e 28 31 29 3b 0a 20 20 20 20 20 20 72 63 20 gn(1);. rc
decd0 3d 20 73 65 65 6b 41 6e 64 52 65 61 64 28 70 46 = seekAndRead(pF
dece0 69 6c 65 2c 20 32 34 2c 20 6f 6c 64 43 6e 74 72 ile, 24, oldCntr
decf0 2c 20 34 29 3b 0a 20 20 20 20 20 20 53 69 6d 75 , 4);. Simu
ded00 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 lateIOErrorBenig
ded10 6e 28 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 n(0);. if(
ded20 72 63 21 3d 34 20 7c 7c 20 6d 65 6d 63 6d 70 28 rc!=4 || memcmp(
ded30 6f 6c 64 43 6e 74 72 2c 20 26 28 28 63 68 61 72 oldCntr, &((char
ded40 2a 29 70 42 75 66 29 5b 32 34 2d 6f 66 66 73 65 *)pBuf)[24-offse
ded50 74 5d 2c 20 34 29 21 3d 30 20 29 7b 0a 20 20 20 t], 4)!=0 ){.
ded60 20 20 20 20 20 70 46 69 6c 65 2d 3e 74 72 61 6e pFile->tran
ded70 73 43 6e 74 72 43 68 6e 67 20 3d 20 31 3b 20 20 sCntrChng = 1;
ded80 2f 2a 20 54 68 65 20 74 72 61 6e 73 61 63 74 69 /* The transacti
ded90 6f 6e 20 63 6f 75 6e 74 65 72 20 68 61 73 20 63 on counter has c
deda0 68 61 6e 67 65 64 20 2a 2f 0a 20 20 20 20 20 20 hanged */.
dedb0 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 }. }. }.#end
dedc0 69 66 0a 0a 20 20 77 68 69 6c 65 28 20 61 6d 74 if.. while( amt
dedd0 3e 30 20 26 26 20 28 77 72 6f 74 65 20 3d 20 73 >0 && (wrote = s
dede0 65 65 6b 41 6e 64 57 72 69 74 65 28 70 46 69 6c eekAndWrite(pFil
dedf0 65 2c 20 6f 66 66 73 65 74 2c 20 70 42 75 66 2c e, offset, pBuf,
dee00 20 61 6d 74 29 29 3e 30 20 29 7b 0a 20 20 20 20 amt))>0 ){.
dee10 61 6d 74 20 2d 3d 20 77 72 6f 74 65 3b 0a 20 20 amt -= wrote;.
dee20 20 20 6f 66 66 73 65 74 20 2b 3d 20 77 72 6f 74 offset += wrot
dee30 65 3b 0a 20 20 20 20 70 42 75 66 20 3d 20 26 28 e;. pBuf = &(
dee40 28 63 68 61 72 2a 29 70 42 75 66 29 5b 77 72 6f (char*)pBuf)[wro
dee50 74 65 5d 3b 0a 20 20 7d 0a 20 20 53 69 6d 75 6c te];. }. Simul
dee60 61 74 65 49 4f 45 72 72 6f 72 28 28 20 77 72 6f ateIOError(( wro
dee70 74 65 3d 28 2d 31 29 2c 20 61 6d 74 3d 31 20 29 te=(-1), amt=1 )
dee80 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73 );. SimulateDis
dee90 6b 66 75 6c 6c 45 72 72 6f 72 28 28 20 77 72 6f kfullError(( wro
deea0 74 65 3d 30 2c 20 61 6d 74 3d 31 20 29 29 3b 0a te=0, amt=1 ));.
deeb0 20 20 69 66 28 20 61 6d 74 3e 30 20 29 7b 0a 20 if( amt>0 ){.
deec0 20 20 20 69 66 28 20 77 72 6f 74 65 3c 30 20 29 if( wrote<0 )
deed0 7b 0a 20 20 20 20 20 20 2f 2a 20 6c 61 73 74 45 {. /* lastE
deee0 72 72 6e 6f 20 73 65 74 20 62 79 20 73 65 65 6b rrno set by seek
deef0 41 6e 64 57 72 69 74 65 20 2a 2f 0a 20 20 20 20 AndWrite */.
def00 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
def10 49 4f 45 52 52 5f 57 52 49 54 45 3b 0a 20 20 20 IOERR_WRITE;.
def20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 46 }else{. pF
def30 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
def40 20 30 3b 20 2f 2a 20 6e 6f 74 20 61 20 73 79 73 0; /* not a sys
def50 74 65 6d 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 tem error */.
def60 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
def70 5f 46 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d _FULL;. }. }
def80 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
def90 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 _OK;.}..#ifdef S
defa0 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a QLITE_TEST./*.**
defb0 20 43 6f 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 Count the numbe
defc0 72 20 6f 66 20 66 75 6c 6c 73 79 6e 63 73 20 61 r of fullsyncs a
defd0 6e 64 20 6e 6f 72 6d 61 6c 20 73 79 6e 63 73 2e nd normal syncs.
defe0 20 20 54 68 69 73 20 69 73 20 75 73 65 64 20 74 This is used t
deff0 6f 20 74 65 73 74 0a 2a 2a 20 74 68 61 74 20 73 o test.** that s
df000 79 6e 63 73 20 61 6e 64 20 66 75 6c 6c 73 79 6e yncs and fullsyn
df010 63 73 20 61 72 65 20 6f 63 63 75 72 72 69 6e 67 cs are occurring
df020 20 61 74 20 74 68 65 20 72 69 67 68 74 20 74 69 at the right ti
df030 6d 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 mes..*/.SQLITE_A
df040 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 PI int sqlite3_s
df050 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 53 ync_count = 0;.S
df060 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
df070 6c 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 lite3_fullsync_c
df080 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 ount = 0;.#endif
df090 0a 0a 2f 2a 0a 2a 2a 20 57 65 20 64 6f 20 6e 6f ../*.** We do no
df0a0 74 20 74 72 75 73 74 20 73 79 73 74 65 6d 73 20 t trust systems
df0b0 74 6f 20 70 72 6f 76 69 64 65 20 61 20 77 6f 72 to provide a wor
df0c0 6b 69 6e 67 20 66 64 61 74 61 73 79 6e 63 28 29 king fdatasync()
df0d0 2e 20 20 53 6f 6d 65 20 64 6f 2e 0a 2a 2a 20 4f . Some do..** O
df0e0 74 68 65 72 73 20 64 6f 20 6e 6f 2e 20 20 54 6f thers do no. To
df0f0 20 62 65 20 73 61 66 65 2c 20 77 65 20 77 69 6c be safe, we wil
df100 6c 20 73 74 69 63 6b 20 77 69 74 68 20 74 68 65 l stick with the
df110 20 28 73 6c 6f 77 65 72 29 20 66 73 79 6e 63 28 (slower) fsync(
df120 29 2e 0a 2a 2a 20 49 66 20 79 6f 75 20 6b 6e 6f )..** If you kno
df130 77 20 74 68 61 74 20 79 6f 75 72 20 73 79 73 74 w that your syst
df140 65 6d 20 64 6f 65 73 20 73 75 70 70 6f 72 74 20 em does support
df150 66 64 61 74 61 73 79 6e 63 28 29 20 63 6f 72 72 fdatasync() corr
df160 65 63 74 6c 79 2c 0a 2a 2a 20 74 68 65 6e 20 73 ectly,.** then s
df170 69 6d 70 6c 79 20 63 6f 6d 70 69 6c 65 20 77 69 imply compile wi
df180 74 68 20 2d 44 66 64 61 74 61 73 79 6e 63 3d 66 th -Dfdatasync=f
df190 64 61 74 61 73 79 6e 63 0a 2a 2f 0a 23 69 66 20 datasync.*/.#if
df1a0 21 64 65 66 69 6e 65 64 28 66 64 61 74 61 73 79 !defined(fdatasy
df1b0 6e 63 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 nc) && !defined(
df1c0 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 23 20 64 65 66 __linux__).# def
df1d0 69 6e 65 20 66 64 61 74 61 73 79 6e 63 20 66 73 ine fdatasync fs
df1e0 79 6e 63 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ync.#endif../*.*
df1f0 2a 20 44 65 66 69 6e 65 20 48 41 56 45 5f 46 55 * Define HAVE_FU
df200 4c 4c 46 53 59 4e 43 20 74 6f 20 30 20 6f 72 20 LLFSYNC to 0 or
df210 31 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 1 depending on w
df220 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 0a 2a 2a hether or not.**
df230 20 74 68 65 20 46 5f 46 55 4c 4c 46 53 59 4e 43 the F_FULLFSYNC
df240 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65 macro is define
df250 64 2e 20 20 46 5f 46 55 4c 4c 46 53 59 4e 43 20 d. F_FULLFSYNC
df260 69 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 is currently.**
df270 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 6f only available o
df280 6e 20 4d 61 63 20 4f 53 20 58 2e 20 20 42 75 74 n Mac OS X. But
df290 20 74 68 61 74 20 63 6f 75 6c 64 20 63 68 61 6e that could chan
df2a0 67 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 46 5f ge..*/.#ifdef F_
df2b0 46 55 4c 4c 46 53 59 4e 43 0a 23 20 64 65 66 69 FULLFSYNC.# defi
df2c0 6e 65 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e ne HAVE_FULLFSYN
df2d0 43 20 31 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 C 1.#else.# defi
df2e0 6e 65 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e ne HAVE_FULLFSYN
df2f0 43 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a C 0.#endif.../*.
df300 2a 2a 20 54 68 65 20 66 73 79 6e 63 28 29 20 73 ** The fsync() s
df310 79 73 74 65 6d 20 63 61 6c 6c 20 64 6f 65 73 20 ystem call does
df320 6e 6f 74 20 77 6f 72 6b 20 61 73 20 61 64 76 65 not work as adve
df330 72 74 69 73 65 64 20 6f 6e 20 6d 61 6e 79 0a 2a rtised on many.*
df340 2a 20 75 6e 69 78 20 73 79 73 74 65 6d 73 2e 20 * unix systems.
df350 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 The following p
df360 72 6f 63 65 64 75 72 65 20 69 73 20 61 6e 20 61 rocedure is an a
df370 74 74 65 6d 70 74 20 74 6f 20 6d 61 6b 65 0a 2a ttempt to make.*
df380 2a 20 69 74 20 77 6f 72 6b 20 62 65 74 74 65 72 * it work better
df390 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 ..**.** The SQLI
df3a0 54 45 5f 4e 4f 5f 53 59 4e 43 20 6d 61 63 72 6f TE_NO_SYNC macro
df3b0 20 64 69 73 61 62 6c 65 73 20 61 6c 6c 20 66 73 disables all fs
df3c0 79 6e 63 28 29 73 2e 20 20 54 68 69 73 20 69 73 ync()s. This is
df3d0 20 75 73 65 66 75 6c 0a 2a 2a 20 66 6f 72 20 74 useful.** for t
df3e0 65 73 74 69 6e 67 20 77 68 65 6e 20 77 65 20 77 esting when we w
df3f0 61 6e 74 20 74 6f 20 72 75 6e 20 74 68 72 6f 75 ant to run throu
df400 67 68 20 74 68 65 20 74 65 73 74 20 73 75 69 74 gh the test suit
df410 65 20 71 75 69 63 6b 6c 79 2e 0a 2a 2a 20 59 6f e quickly..** Yo
df420 75 20 61 72 65 20 73 74 72 6f 6e 67 6c 79 20 61 u are strongly a
df430 64 76 69 73 65 64 20 2a 6e 6f 74 2a 20 74 6f 20 dvised *not* to
df440 64 65 70 6c 6f 79 20 77 69 74 68 20 53 51 4c 49 deploy with SQLI
df450 54 45 5f 4e 4f 5f 53 59 4e 43 0a 2a 2a 20 65 6e TE_NO_SYNC.** en
df460 61 62 6c 65 64 2c 20 68 6f 77 65 76 65 72 2c 20 abled, however,
df470 73 69 6e 63 65 20 77 69 74 68 20 53 51 4c 49 54 since with SQLIT
df480 45 5f 4e 4f 5f 53 59 4e 43 20 65 6e 61 62 6c 65 E_NO_SYNC enable
df490 64 2c 20 61 6e 20 4f 53 20 63 72 61 73 68 0a 2a d, an OS crash.*
df4a0 2a 20 6f 72 20 70 6f 77 65 72 20 66 61 69 6c 75 * or power failu
df4b0 72 65 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 63 re will likely c
df4c0 6f 72 72 75 70 74 20 74 68 65 20 64 61 74 61 62 orrupt the datab
df4d0 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 ase file..**.**
df4e0 53 51 4c 69 74 65 20 73 65 74 73 20 74 68 65 20 SQLite sets the
df4f0 64 61 74 61 4f 6e 6c 79 20 66 6c 61 67 20 69 66 dataOnly flag if
df500 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
df510 20 66 69 6c 65 20 69 73 20 75 6e 63 68 61 6e 67 file is unchang
df520 65 64 2e 0a 2a 2a 20 54 68 65 20 69 64 65 61 20 ed..** The idea
df530 62 65 68 69 6e 64 20 64 61 74 61 4f 6e 6c 79 20 behind dataOnly
df540 69 73 20 74 68 61 74 20 69 74 20 73 68 6f 75 6c is that it shoul
df550 64 20 6f 6e 6c 79 20 77 72 69 74 65 20 74 68 65 d only write the
df560 20 66 69 6c 65 20 63 6f 6e 74 65 6e 74 0a 2a 2a file content.**
df570 20 74 6f 20 64 69 73 6b 2c 20 6e 6f 74 20 74 68 to disk, not th
df580 65 20 69 6e 6f 64 65 2e 20 20 57 65 20 6f 6e 6c e inode. We onl
df590 79 20 73 65 74 20 64 61 74 61 4f 6e 6c 79 20 69 y set dataOnly i
df5a0 66 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 f the file size
df5b0 69 73 20 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64 is .** unchanged
df5c0 20 73 69 6e 63 65 20 74 68 65 20 66 69 6c 65 20 since the file
df5d0 73 69 7a 65 20 69 73 20 70 61 72 74 20 6f 66 20 size is part of
df5e0 74 68 65 20 69 6e 6f 64 65 2e 20 20 48 6f 77 65 the inode. Howe
df5f0 76 65 72 2c 20 0a 2a 2a 20 54 65 64 20 54 73 27 ver, .** Ted Ts'
df600 6f 20 74 65 6c 6c 73 20 75 73 20 74 68 61 74 20 o tells us that
df610 66 64 61 74 61 73 79 6e 63 28 29 20 77 69 6c 6c fdatasync() will
df620 20 61 6c 73 6f 20 77 72 69 74 65 20 74 68 65 20 also write the
df630 69 6e 6f 64 65 20 69 66 20 74 68 65 0a 2a 2a 20 inode if the.**
df640 66 69 6c 65 20 73 69 7a 65 20 68 61 73 20 63 68 file size has ch
df650 61 6e 67 65 64 2e 20 20 54 68 65 20 6f 6e 6c 79 anged. The only
df660 20 72 65 61 6c 20 64 69 66 66 65 72 65 6e 63 65 real difference
df670 20 62 65 74 77 65 65 6e 20 66 64 61 74 61 73 79 between fdatasy
df680 6e 63 28 29 0a 2a 2a 20 61 6e 64 20 66 73 79 6e nc().** and fsyn
df690 63 28 29 2c 20 54 65 64 20 74 65 6c 6c 73 20 75 c(), Ted tells u
df6a0 73 2c 20 69 73 20 74 68 61 74 20 66 64 61 74 61 s, is that fdata
df6b0 73 79 6e 63 28 29 20 77 69 6c 6c 20 6e 6f 74 20 sync() will not
df6c0 66 6c 75 73 68 20 74 68 65 0a 2a 2a 20 69 6e 6f flush the.** ino
df6d0 64 65 20 69 66 20 74 68 65 20 6d 74 69 6d 65 20 de if the mtime
df6e0 6f 72 20 6f 77 6e 65 72 20 6f 72 20 6f 74 68 65 or owner or othe
df6f0 72 20 69 6e 6f 64 65 20 61 74 74 72 69 62 75 74 r inode attribut
df700 65 73 20 68 61 76 65 20 63 68 61 6e 67 65 64 2e es have changed.
df710 0a 2a 2a 20 57 65 20 6f 6e 6c 79 20 63 61 72 65 .** We only care
df720 20 61 62 6f 75 74 20 74 68 65 20 66 69 6c 65 20 about the file
df730 73 69 7a 65 2c 20 6e 6f 74 20 74 68 65 20 6f 74 size, not the ot
df740 68 65 72 20 66 69 6c 65 20 61 74 74 72 69 62 75 her file attribu
df750 74 65 73 2c 20 73 6f 0a 2a 2a 20 61 73 20 66 61 tes, so.** as fa
df760 72 20 61 73 20 53 51 4c 69 74 65 20 69 73 20 63 r as SQLite is c
df770 6f 6e 63 65 72 6e 65 64 2c 20 61 6e 20 66 64 61 oncerned, an fda
df780 74 61 73 79 6e 63 28 29 20 69 73 20 61 6c 77 61 tasync() is alwa
df790 79 73 20 61 64 65 71 75 61 74 65 2e 0a 2a 2a 20 ys adequate..**
df7a0 53 6f 2c 20 77 65 20 61 6c 77 61 79 73 20 75 73 So, we always us
df7b0 65 20 66 64 61 74 61 73 79 6e 63 28 29 20 69 66 e fdatasync() if
df7c0 20 69 74 20 69 73 20 61 76 61 69 6c 61 62 6c 65 it is available
df7d0 2c 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 0a , regardless of.
df7e0 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 ** the value of
df7f0 74 68 65 20 64 61 74 61 4f 6e 6c 79 20 66 6c 61 the dataOnly fla
df800 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 g..*/.static int
df810 20 66 75 6c 6c 5f 66 73 79 6e 63 28 69 6e 74 20 full_fsync(int
df820 66 64 2c 20 69 6e 74 20 66 75 6c 6c 53 79 6e 63 fd, int fullSync
df830 2c 20 69 6e 74 20 64 61 74 61 4f 6e 6c 79 29 7b , int dataOnly){
df840 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 2f 2a . int rc;.. /*
df850 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 22 The following "
df860 69 66 64 65 66 2f 65 6c 69 66 2f 65 6c 73 65 2f ifdef/elif/else/
df870 22 20 62 6c 6f 63 6b 20 68 61 73 20 74 68 65 20 " block has the
df880 73 61 6d 65 20 73 74 72 75 63 74 75 72 65 20 61 same structure a
df890 73 0a 20 20 2a 2a 20 74 68 65 20 6f 6e 65 20 62 s. ** the one b
df8a0 65 6c 6f 77 2e 20 49 74 20 69 73 20 72 65 70 6c elow. It is repl
df8b0 69 63 61 74 65 64 20 68 65 72 65 20 73 6f 6c 65 icated here sole
df8c0 6c 79 20 74 6f 20 61 76 6f 69 64 20 63 6c 75 74 ly to avoid clut
df8d0 74 65 72 69 6e 67 20 0a 20 20 2a 2a 20 75 70 20 tering . ** up
df8e0 74 68 65 20 72 65 61 6c 20 63 6f 64 65 20 77 69 the real code wi
df8f0 74 68 20 74 68 65 20 55 4e 55 53 45 44 5f 50 41 th the UNUSED_PA
df900 52 41 4d 45 54 45 52 28 29 20 6d 61 63 72 6f 73 RAMETER() macros
df910 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 .. */.#ifdef SQ
df920 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 55 LITE_NO_SYNC. U
df930 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
df940 66 64 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 fd);. UNUSED_PA
df950 52 41 4d 45 54 45 52 28 66 75 6c 6c 53 79 6e 63 RAMETER(fullSync
df960 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 );. UNUSED_PARA
df970 4d 45 54 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b METER(dataOnly);
df980 0a 23 65 6c 69 66 20 48 41 56 45 5f 46 55 4c 4c .#elif HAVE_FULL
df990 46 53 59 4e 43 0a 20 20 55 4e 55 53 45 44 5f 50 FSYNC. UNUSED_P
df9a0 41 52 41 4d 45 54 45 52 28 64 61 74 61 4f 6e 6c ARAMETER(dataOnl
df9b0 79 29 3b 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 y);.#else. UNUS
df9c0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 66 75 6c ED_PARAMETER(ful
df9d0 6c 53 79 6e 63 29 3b 0a 20 20 55 4e 55 53 45 44 lSync);. UNUSED
df9e0 5f 50 41 52 41 4d 45 54 45 52 28 64 61 74 61 4f _PARAMETER(dataO
df9f0 6e 6c 79 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 nly);.#endif..
dfa00 2f 2a 20 52 65 63 6f 72 64 20 74 68 65 20 6e 75 /* Record the nu
dfa10 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 mber of times th
dfa20 61 74 20 77 65 20 64 6f 20 61 20 6e 6f 72 6d 61 at we do a norma
dfa30 6c 20 66 73 79 6e 63 28 29 20 61 6e 64 20 0a 20 l fsync() and .
dfa40 20 2a 2a 20 46 55 4c 4c 53 59 4e 43 2e 20 20 54 ** FULLSYNC. T
dfa50 68 69 73 20 69 73 20 75 73 65 64 20 64 75 72 69 his is used duri
dfa60 6e 67 20 74 65 73 74 69 6e 67 20 74 6f 20 76 65 ng testing to ve
dfa70 72 69 66 79 20 74 68 61 74 20 74 68 69 73 20 70 rify that this p
dfa80 72 6f 63 65 64 75 72 65 0a 20 20 2a 2a 20 67 65 rocedure. ** ge
dfa90 74 73 20 63 61 6c 6c 65 64 20 77 69 74 68 20 74 ts called with t
dfaa0 68 65 20 63 6f 72 72 65 63 74 20 61 72 67 75 6d he correct argum
dfab0 65 6e 74 73 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 ents.. */.#ifde
dfac0 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 f SQLITE_TEST.
dfad0 69 66 28 20 66 75 6c 6c 53 79 6e 63 20 29 20 73 if( fullSync ) s
dfae0 71 6c 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f qlite3_fullsync_
dfaf0 63 6f 75 6e 74 2b 2b 3b 0a 20 20 73 71 6c 69 74 count++;. sqlit
dfb00 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b e3_sync_count++;
dfb10 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 .#endif.. /* If
dfb20 20 77 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 we compiled wit
dfb30 68 20 74 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f h the SQLITE_NO_
dfb40 53 59 4e 43 20 66 6c 61 67 2c 20 74 68 65 6e 20 SYNC flag, then
dfb50 73 79 6e 63 69 6e 67 20 69 73 20 61 0a 20 20 2a syncing is a. *
dfb60 2a 20 6e 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66 * no-op. */.#if
dfb70 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 def SQLITE_NO_SY
dfb80 4e 43 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 NC. rc = SQLITE
dfb90 5f 4f 4b 3b 0a 23 65 6c 69 66 20 48 41 56 45 5f _OK;.#elif HAVE_
dfba0 46 55 4c 4c 46 53 59 4e 43 0a 20 20 69 66 28 20 FULLFSYNC. if(
dfbb0 66 75 6c 6c 53 79 6e 63 20 29 7b 0a 20 20 20 20 fullSync ){.
dfbc0 72 63 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20 46 rc = fcntl(fd, F
dfbd0 5f 46 55 4c 4c 46 53 59 4e 43 2c 20 30 29 3b 0a _FULLFSYNC, 0);.
dfbe0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 }else{. rc
dfbf0 3d 20 31 3b 0a 20 20 7d 0a 20 20 2f 2a 20 49 66 = 1;. }. /* If
dfc00 20 74 68 65 20 46 55 4c 4c 46 53 59 4e 43 20 66 the FULLFSYNC f
dfc10 61 69 6c 65 64 2c 20 66 61 6c 6c 20 62 61 63 6b ailed, fall back
dfc20 20 74 6f 20 61 74 74 65 6d 70 74 69 6e 67 20 61 to attempting a
dfc30 6e 20 66 73 79 6e 63 28 29 2e 0a 20 20 2a 2a 20 n fsync().. **
dfc40 49 74 20 73 68 6f 75 6c 64 6e 27 74 20 62 65 20 It shouldn't be
dfc50 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 66 75 6c possible for ful
dfc60 6c 66 73 79 6e 63 20 74 6f 20 66 61 69 6c 20 6f lfsync to fail o
dfc70 6e 20 74 68 65 20 6c 6f 63 61 6c 20 0a 20 20 2a n the local . *
dfc80 2a 20 66 69 6c 65 20 73 79 73 74 65 6d 20 28 6f * file system (o
dfc90 6e 20 4f 53 58 29 2c 20 73 6f 20 66 61 69 6c 75 n OSX), so failu
dfca0 72 65 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 re indicates tha
dfcb0 74 20 46 55 4c 4c 46 53 59 4e 43 0a 20 20 2a 2a t FULLFSYNC. **
dfcc0 20 69 73 6e 27 74 20 73 75 70 70 6f 72 74 65 64 isn't supported
dfcd0 20 66 6f 72 20 74 68 69 73 20 66 69 6c 65 20 73 for this file s
dfce0 79 73 74 65 6d 2e 20 53 6f 2c 20 61 74 74 65 6d ystem. So, attem
dfcf0 70 74 20 61 6e 20 66 73 79 6e 63 20 0a 20 20 2a pt an fsync . *
dfd00 2a 20 61 6e 64 20 28 66 6f 72 20 6e 6f 77 29 20 * and (for now)
dfd10 69 67 6e 6f 72 65 20 74 68 65 20 6f 76 65 72 68 ignore the overh
dfd20 65 61 64 20 6f 66 20 61 20 73 75 70 65 72 66 6c ead of a superfl
dfd30 75 6f 75 73 20 66 63 6e 74 6c 20 63 61 6c 6c 2e uous fcntl call.
dfd40 20 20 0a 20 20 2a 2a 20 49 74 27 64 20 62 65 20 . ** It'd be
dfd50 62 65 74 74 65 72 20 74 6f 20 64 65 74 65 63 74 better to detect
dfd60 20 66 75 6c 6c 66 73 79 6e 63 20 73 75 70 70 6f fullfsync suppo
dfd70 72 74 20 6f 6e 63 65 20 61 6e 64 20 61 76 6f 69 rt once and avoi
dfd80 64 20 0a 20 20 2a 2a 20 74 68 65 20 66 63 6e 74 d . ** the fcnt
dfd90 6c 20 63 61 6c 6c 20 65 76 65 72 79 20 74 69 6d l call every tim
dfda0 65 20 73 79 6e 63 20 69 73 20 63 61 6c 6c 65 64 e sync is called
dfdb0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 20 .. */. if( rc
dfdc0 29 20 72 63 20 3d 20 66 73 79 6e 63 28 66 64 29 ) rc = fsync(fd)
dfdd0 3b 0a 0a 23 65 6c 73 65 20 0a 20 20 72 63 20 3d ;..#else . rc =
dfde0 20 66 64 61 74 61 73 79 6e 63 28 66 64 29 3b 0a fdatasync(fd);.
dfdf0 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 #if OS_VXWORKS.
dfe00 20 69 66 28 20 72 63 3d 3d 2d 31 20 26 26 20 65 if( rc==-1 && e
dfe10 72 72 6e 6f 3d 3d 45 4e 4f 54 53 55 50 20 29 7b rrno==ENOTSUP ){
dfe20 0a 20 20 20 20 72 63 20 3d 20 66 73 79 6e 63 28 . rc = fsync(
dfe30 66 64 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 fd);. }.#endif
dfe40 2f 2a 20 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f /* OS_VXWORKS */
dfe50 0a 23 65 6e 64 69 66 20 2f 2a 20 69 66 64 65 66 .#endif /* ifdef
dfe60 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 SQLITE_NO_SYNC
dfe70 65 6c 69 66 20 48 41 56 45 5f 46 55 4c 4c 46 53 elif HAVE_FULLFS
dfe80 59 4e 43 20 2a 2f 0a 0a 20 20 69 66 28 20 4f 53 YNC */.. if( OS
dfe90 5f 56 58 57 4f 52 4b 53 20 26 26 20 72 63 21 3d _VXWORKS && rc!=
dfea0 20 2d 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 -1 ){. rc =
dfeb0 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 0;. }. return
dfec0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b rc;.}../*.** Mak
dfed0 65 20 73 75 72 65 20 61 6c 6c 20 77 72 69 74 65 e sure all write
dfee0 73 20 74 6f 20 61 20 70 61 72 74 69 63 75 6c 61 s to a particula
dfef0 72 20 66 69 6c 65 20 61 72 65 20 63 6f 6d 6d 69 r file are commi
dff00 74 74 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a tted to disk..**
dff10 0a 2a 2a 20 49 66 20 64 61 74 61 4f 6e 6c 79 3d .** If dataOnly=
dff20 3d 30 20 74 68 65 6e 20 62 6f 74 68 20 74 68 65 =0 then both the
dff30 20 66 69 6c 65 20 69 74 73 65 6c 66 20 61 6e 64 file itself and
dff40 20 69 74 73 20 6d 65 74 61 64 61 74 61 20 28 66 its metadata (f
dff50 69 6c 65 0a 2a 2a 20 73 69 7a 65 2c 20 61 63 63 ile.** size, acc
dff60 65 73 73 20 74 69 6d 65 2c 20 65 74 63 29 20 61 ess time, etc) a
dff70 72 65 20 73 79 6e 63 65 64 2e 20 20 49 66 20 64 re synced. If d
dff80 61 74 61 4f 6e 6c 79 21 3d 30 20 74 68 65 6e 20 ataOnly!=0 then
dff90 6f 6e 6c 79 20 74 68 65 0a 2a 2a 20 66 69 6c 65 only the.** file
dffa0 20 64 61 74 61 20 69 73 20 73 79 6e 63 65 64 2e data is synced.
dffb0 0a 2a 2a 0a 2a 2a 20 55 6e 64 65 72 20 55 6e 69 .**.** Under Uni
dffc0 78 2c 20 61 6c 73 6f 20 6d 61 6b 65 20 73 75 72 x, also make sur
dffd0 65 20 74 68 61 74 20 74 68 65 20 64 69 72 65 63 e that the direc
dffe0 74 6f 72 79 20 65 6e 74 72 79 20 66 6f 72 20 74 tory entry for t
dfff0 68 65 20 66 69 6c 65 0a 2a 2a 20 68 61 73 20 62 he file.** has b
e0000 65 65 6e 20 63 72 65 61 74 65 64 20 62 79 20 66 een created by f
e0010 73 79 6e 63 2d 69 6e 67 20 74 68 65 20 64 69 72 sync-ing the dir
e0020 65 63 74 6f 72 79 20 74 68 61 74 20 63 6f 6e 74 ectory that cont
e0030 61 69 6e 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a ains the file..*
e0040 2a 20 49 66 20 77 65 20 64 6f 20 6e 6f 74 20 64 * If we do not d
e0050 6f 20 74 68 69 73 20 61 6e 64 20 77 65 20 65 6e o this and we en
e0060 63 6f 75 6e 74 65 72 20 61 20 70 6f 77 65 72 20 counter a power
e0070 66 61 69 6c 75 72 65 2c 20 74 68 65 20 64 69 72 failure, the dir
e0080 65 63 74 6f 72 79 0a 2a 2a 20 65 6e 74 72 79 20 ectory.** entry
e0090 66 6f 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 for the journal
e00a0 6d 69 67 68 74 20 6e 6f 74 20 65 78 69 73 74 20 might not exist
e00b0 61 66 74 65 72 20 77 65 20 72 65 62 6f 6f 74 2e after we reboot.
e00c0 20 20 54 68 65 20 6e 65 78 74 0a 2a 2a 20 53 51 The next.** SQ
e00d0 4c 69 74 65 20 74 6f 20 61 63 63 65 73 73 20 74 Lite to access t
e00e0 68 65 20 66 69 6c 65 20 77 69 6c 6c 20 6e 6f 74 he file will not
e00f0 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 20 6a know that the j
e0100 6f 75 72 6e 61 6c 20 65 78 69 73 74 73 20 28 62 ournal exists (b
e0110 65 63 61 75 73 65 0a 2a 2a 20 74 68 65 20 64 69 ecause.** the di
e0120 72 65 63 74 6f 72 79 20 65 6e 74 72 79 20 66 6f rectory entry fo
e0130 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77 61 r the journal wa
e0140 73 20 6e 65 76 65 72 20 63 72 65 61 74 65 64 29 s never created)
e0150 20 61 6e 64 20 74 68 65 20 74 72 61 6e 73 61 63 and the transac
e0160 74 69 6f 6e 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 tion.** will not
e0170 20 72 6f 6c 6c 20 62 61 63 6b 20 2d 20 70 6f 73 roll back - pos
e0180 73 69 62 6c 79 20 6c 65 61 64 69 6e 67 20 74 6f sibly leading to
e0190 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 database corrup
e01a0 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 tion..*/.static
e01b0 69 6e 74 20 75 6e 69 78 53 79 6e 63 28 73 71 6c int unixSync(sql
e01c0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
e01d0 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74 nt flags){. int
e01e0 20 72 63 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 rc;. unixFile
e01f0 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 *pFile = (unixFi
e0200 6c 65 2a 29 69 64 3b 0a 0a 20 20 69 6e 74 20 69 le*)id;.. int i
e0210 73 44 61 74 61 4f 6e 6c 79 20 3d 20 28 66 6c 61 sDataOnly = (fla
e0220 67 73 26 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 gs&SQLITE_SYNC_D
e0230 41 54 41 4f 4e 4c 59 29 3b 0a 20 20 69 6e 74 20 ATAONLY);. int
e0240 69 73 46 75 6c 6c 73 79 6e 63 20 3d 20 28 66 6c isFullsync = (fl
e0250 61 67 73 26 30 78 30 46 29 3d 3d 53 51 4c 49 54 ags&0x0F)==SQLIT
e0260 45 5f 53 59 4e 43 5f 46 55 4c 4c 3b 0a 0a 20 20 E_SYNC_FULL;..
e0270 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 6f 6e /* Check that on
e0280 65 20 6f 66 20 53 51 4c 49 54 45 5f 53 59 4e 43 e of SQLITE_SYNC
e0290 5f 4e 4f 52 4d 41 4c 20 6f 72 20 46 55 4c 4c 20 _NORMAL or FULL
e02a0 77 61 73 20 70 61 73 73 65 64 20 2a 2f 0a 20 20 was passed */.
e02b0 61 73 73 65 72 74 28 28 66 6c 61 67 73 26 30 78 assert((flags&0x
e02c0 30 46 29 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 0F)==SQLITE_SYNC
e02d0 5f 4e 4f 52 4d 41 4c 0a 20 20 20 20 20 20 7c 7c _NORMAL. ||
e02e0 20 28 66 6c 61 67 73 26 30 78 30 46 29 3d 3d 53 (flags&0x0F)==S
e02f0 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 0a QLITE_SYNC_FULL.
e0300 20 20 29 3b 0a 0a 20 20 2f 2a 20 55 6e 69 78 20 );.. /* Unix
e0310 63 61 6e 6e 6f 74 2c 20 62 75 74 20 73 6f 6d 65 cannot, but some
e0320 20 73 79 73 74 65 6d 73 20 6d 61 79 20 72 65 74 systems may ret
e0330 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 urn SQLITE_FULL
e0340 66 72 6f 6d 20 68 65 72 65 2e 20 54 68 69 73 0a from here. This.
e0350 20 20 2a 2a 20 6c 69 6e 65 20 69 73 20 74 6f 20 ** line is to
e0360 74 65 73 74 20 74 68 61 74 20 64 6f 69 6e 67 20 test that doing
e0370 73 6f 20 64 6f 65 73 20 6e 6f 74 20 63 61 75 73 so does not caus
e0380 65 20 61 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 0a e any problems..
e0390 20 20 2a 2f 0a 20 20 53 69 6d 75 6c 61 74 65 44 */. SimulateD
e03a0 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 20 72 65 iskfullError( re
e03b0 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c turn SQLITE_FULL
e03c0 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 );.. assert( p
e03d0 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 File );. OSTRAC
e03e0 45 32 28 22 53 59 4e 43 20 20 20 20 25 2d 33 64 E2("SYNC %-3d
e03f0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a \n", pFile->h);.
e0400 20 20 72 63 20 3d 20 66 75 6c 6c 5f 66 73 79 6e rc = full_fsyn
e0410 63 28 70 46 69 6c 65 2d 3e 68 2c 20 69 73 46 75 c(pFile->h, isFu
e0420 6c 6c 73 79 6e 63 2c 20 69 73 44 61 74 61 4f 6e llsync, isDataOn
e0430 6c 79 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 ly);. SimulateI
e0440 4f 45 72 72 6f 72 28 20 72 63 3d 31 20 29 3b 0a OError( rc=1 );.
e0450 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 if( rc ){.
e0460 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f pFile->lastErrno
e0470 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 65 = errno;. re
e0480 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 turn SQLITE_IOER
e0490 52 5f 46 53 59 4e 43 3b 0a 20 20 7d 0a 20 20 69 R_FSYNC;. }. i
e04a0 66 28 20 70 46 69 6c 65 2d 3e 64 69 72 66 64 3e f( pFile->dirfd>
e04b0 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 65 72 =0 ){. int er
e04c0 72 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 34 28 r;. OSTRACE4(
e04d0 22 44 49 52 53 59 4e 43 20 25 2d 33 64 20 28 68 "DIRSYNC %-3d (h
e04e0 61 76 65 5f 66 75 6c 6c 66 73 79 6e 63 3d 25 64 ave_fullfsync=%d
e04f0 20 66 75 6c 6c 73 79 6e 63 3d 25 64 29 5c 6e 22 fullsync=%d)\n"
e0500 2c 20 70 46 69 6c 65 2d 3e 64 69 72 66 64 2c 0a , pFile->dirfd,.
e0510 20 20 20 20 20 20 20 20 20 20 20 20 48 41 56 45 HAVE
e0520 5f 46 55 4c 4c 46 53 59 4e 43 2c 20 69 73 46 75 _FULLFSYNC, isFu
e0530 6c 6c 73 79 6e 63 29 3b 0a 23 69 66 6e 64 65 66 llsync);.#ifndef
e0540 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f SQLITE_DISABLE_
e0550 44 49 52 53 59 4e 43 0a 20 20 20 20 2f 2a 20 54 DIRSYNC. /* T
e0560 68 65 20 64 69 72 65 63 74 6f 72 79 20 73 79 6e he directory syn
e0570 63 20 69 73 20 6f 6e 6c 79 20 61 74 74 65 6d 70 c is only attemp
e0580 74 65 64 20 69 66 20 66 75 6c 6c 5f 66 73 79 6e ted if full_fsyn
e0590 63 20 69 73 0a 20 20 20 20 2a 2a 20 74 75 72 6e c is. ** turn
e05a0 65 64 20 6f 66 66 20 6f 72 20 75 6e 61 76 61 69 ed off or unavai
e05b0 6c 61 62 6c 65 2e 20 20 49 66 20 61 20 66 75 6c lable. If a ful
e05c0 6c 5f 66 73 79 6e 63 20 6f 63 63 75 72 72 65 64 l_fsync occurred
e05d0 20 61 62 6f 76 65 2c 0a 20 20 20 20 2a 2a 20 74 above,. ** t
e05e0 68 65 6e 20 74 68 65 20 64 69 72 65 63 74 6f 72 hen the director
e05f0 79 20 73 79 6e 63 20 69 73 20 73 75 70 65 72 66 y sync is superf
e0600 6c 75 6f 75 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 luous.. */.
e0610 20 20 69 66 28 20 28 21 48 41 56 45 5f 46 55 4c if( (!HAVE_FUL
e0620 4c 46 53 59 4e 43 20 7c 7c 20 21 69 73 46 75 6c LFSYNC || !isFul
e0630 6c 73 79 6e 63 29 20 26 26 20 66 75 6c 6c 5f 66 lsync) && full_f
e0640 73 79 6e 63 28 70 46 69 6c 65 2d 3e 64 69 72 66 sync(pFile->dirf
e0650 64 2c 30 2c 30 29 20 29 7b 0a 20 20 20 20 20 20 d,0,0) ){.
e0660 20 2f 2a 0a 20 20 20 20 20 20 20 2a 2a 20 57 65 /*. ** We
e0670 20 68 61 76 65 20 72 65 63 65 69 76 65 64 20 6d have received m
e0680 75 6c 74 69 70 6c 65 20 72 65 70 6f 72 74 73 20 ultiple reports
e0690 6f 66 20 66 73 79 6e 63 28 29 20 72 65 74 75 72 of fsync() retur
e06a0 6e 69 6e 67 0a 20 20 20 20 20 20 20 2a 2a 20 65 ning. ** e
e06b0 72 72 6f 72 73 20 77 68 65 6e 20 61 70 70 6c 69 rrors when appli
e06c0 65 64 20 74 6f 20 64 69 72 65 63 74 6f 72 69 65 ed to directorie
e06d0 73 20 6f 6e 20 63 65 72 74 61 69 6e 20 66 69 6c s on certain fil
e06e0 65 20 73 79 73 74 65 6d 73 2e 0a 20 20 20 20 20 e systems..
e06f0 20 20 2a 2a 20 41 20 66 61 69 6c 65 64 20 64 69 ** A failed di
e0700 72 65 63 74 6f 72 79 20 73 79 6e 63 20 69 73 20 rectory sync is
e0710 6e 6f 74 20 61 20 62 69 67 20 64 65 61 6c 2e 20 not a big deal.
e0720 20 53 6f 20 69 74 20 73 65 65 6d 73 0a 20 20 20 So it seems.
e0730 20 20 20 20 2a 2a 20 62 65 74 74 65 72 20 74 6f ** better to
e0740 20 69 67 6e 6f 72 65 20 74 68 65 20 65 72 72 6f ignore the erro
e0750 72 2e 20 20 54 69 63 6b 65 74 20 23 31 36 35 37 r. Ticket #1657
e0760 0a 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 . */.
e0770 20 20 2f 2a 20 70 46 69 6c 65 2d 3e 6c 61 73 74 /* pFile->last
e0780 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 20 2a Errno = errno; *
e0790 2f 0a 20 20 20 20 20 20 20 2f 2a 20 72 65 74 75 /. /* retu
e07a0 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b rn SQLITE_IOERR;
e07b0 20 2a 2f 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 */. }.#endif
e07c0 0a 20 20 20 20 65 72 72 20 3d 20 63 6c 6f 73 65 . err = close
e07d0 28 70 46 69 6c 65 2d 3e 64 69 72 66 64 29 3b 20 (pFile->dirfd);
e07e0 2f 2a 20 4f 6e 6c 79 20 6e 65 65 64 20 74 6f 20 /* Only need to
e07f0 73 79 6e 63 20 6f 6e 63 65 2c 20 73 6f 20 63 6c sync once, so cl
e0800 6f 73 65 20 74 68 65 20 2a 2f 0a 20 20 20 20 69 ose the */. i
e0810 66 28 20 65 72 72 3d 3d 30 20 29 7b 20 20 20 20 f( err==0 ){
e0820 20 20 20 20 20 20 20 20 20 20 2f 2a 20 64 69 72 /* dir
e0830 65 63 74 6f 72 79 20 77 68 65 6e 20 77 65 20 61 ectory when we a
e0840 72 65 20 64 6f 6e 65 20 2a 2f 0a 20 20 20 20 20 re done */.
e0850 20 70 46 69 6c 65 2d 3e 64 69 72 66 64 20 3d 20 pFile->dirfd =
e0860 2d 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 -1;. }else{.
e0870 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 pFile->last
e0880 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
e0890 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
e08a0 5f 49 4f 45 52 52 5f 44 49 52 5f 43 4c 4f 53 45 _IOERR_DIR_CLOSE
e08b0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
e08c0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
e08d0 2a 20 54 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 * Truncate an op
e08e0 65 6e 20 66 69 6c 65 20 74 6f 20 61 20 73 70 65 en file to a spe
e08f0 63 69 66 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 cified size.*/.s
e0900 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 54 72 tatic int unixTr
e0910 75 6e 63 61 74 65 28 73 71 6c 69 74 65 33 5f 66 uncate(sqlite3_f
e0920 69 6c 65 20 2a 69 64 2c 20 69 36 34 20 6e 42 79 ile *id, i64 nBy
e0930 74 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 te){. int rc;.
e0940 20 61 73 73 65 72 74 28 20 69 64 20 29 3b 0a 20 assert( id );.
e0950 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
e0960 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ( return SQLITE_
e0970 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 20 29 IOERR_TRUNCATE )
e0980 3b 0a 20 20 72 63 20 3d 20 66 74 72 75 6e 63 61 ;. rc = ftrunca
e0990 74 65 28 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 te(((unixFile*)i
e09a0 64 29 2d 3e 68 2c 20 28 6f 66 66 5f 74 29 6e 42 d)->h, (off_t)nB
e09b0 79 74 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 yte);. if( rc )
e09c0 7b 0a 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 {. ((unixFile
e09d0 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f *)id)->lastErrno
e09e0 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 65 = errno;. re
e09f0 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 turn SQLITE_IOER
e0a00 52 5f 54 52 55 4e 43 41 54 45 3b 0a 20 20 7d 65 R_TRUNCATE;. }e
e0a10 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 lse{. return
e0a20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d SQLITE_OK;. }.}
e0a30 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e ../*.** Determin
e0a40 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 69 e the current si
e0a50 7a 65 20 6f 66 20 61 20 66 69 6c 65 20 69 6e 20 ze of a file in
e0a60 62 79 74 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20 bytes.*/.static
e0a70 69 6e 74 20 75 6e 69 78 46 69 6c 65 53 69 7a 65 int unixFileSize
e0a80 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
e0a90 64 2c 20 69 36 34 20 2a 70 53 69 7a 65 29 7b 0a d, i64 *pSize){.
e0aa0 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 74 72 75 int rc;. stru
e0ab0 63 74 20 73 74 61 74 20 62 75 66 3b 0a 20 20 61 ct stat buf;. a
e0ac0 73 73 65 72 74 28 20 69 64 20 29 3b 0a 20 20 72 ssert( id );. r
e0ad0 63 20 3d 20 66 73 74 61 74 28 28 28 75 6e 69 78 c = fstat(((unix
e0ae0 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 26 62 File*)id)->h, &b
e0af0 75 66 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 uf);. SimulateI
e0b00 4f 45 72 72 6f 72 28 20 72 63 3d 31 20 29 3b 0a OError( rc=1 );.
e0b10 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 if( rc!=0 ){.
e0b20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 ((unixFile*)i
e0b30 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 d)->lastErrno =
e0b40 65 72 72 6e 6f 3b 0a 20 20 20 20 72 65 74 75 72 errno;. retur
e0b50 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 n SQLITE_IOERR_F
e0b60 53 54 41 54 3b 0a 20 20 7d 0a 20 20 2a 70 53 69 STAT;. }. *pSi
e0b70 7a 65 20 3d 20 62 75 66 2e 73 74 5f 73 69 7a 65 ze = buf.st_size
e0b80 3b 0a 0a 20 20 2f 2a 20 57 68 65 6e 20 6f 70 65 ;.. /* When ope
e0b90 6e 69 6e 67 20 61 20 7a 65 72 6f 2d 73 69 7a 65 ning a zero-size
e0ba0 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 20 66 database, the f
e0bb0 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29 20 70 72 indLockInfo() pr
e0bc0 6f 63 65 64 75 72 65 0a 20 20 2a 2a 20 77 72 69 ocedure. ** wri
e0bd0 74 65 73 20 61 20 73 69 6e 67 6c 65 20 62 79 74 tes a single byt
e0be0 65 20 69 6e 74 6f 20 74 68 61 74 20 66 69 6c 65 e into that file
e0bf0 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 77 6f 72 in order to wor
e0c00 6b 20 61 72 6f 75 6e 64 20 61 20 62 75 67 0a 20 k around a bug.
e0c10 20 2a 2a 20 69 6e 20 74 68 65 20 4f 53 2d 58 20 ** in the OS-X
e0c20 6d 73 64 6f 73 20 66 69 6c 65 73 79 73 74 65 6d msdos filesystem
e0c30 2e 20 20 49 6e 20 6f 72 64 65 72 20 74 6f 20 61 . In order to a
e0c40 76 6f 69 64 20 70 72 6f 62 6c 65 6d 73 20 77 69 void problems wi
e0c50 74 68 20 75 70 70 65 72 0a 20 20 2a 2a 20 6c 61 th upper. ** la
e0c60 79 65 72 73 2c 20 77 65 20 6e 65 65 64 20 74 6f yers, we need to
e0c70 20 72 65 70 6f 72 74 20 74 68 69 73 20 66 69 6c report this fil
e0c80 65 20 73 69 7a 65 20 61 73 20 7a 65 72 6f 20 65 e size as zero e
e0c90 76 65 6e 20 74 68 6f 75 67 68 20 69 74 20 69 73 ven though it is
e0ca0 0a 20 20 2a 2a 20 72 65 61 6c 6c 79 20 31 2e 20 . ** really 1.
e0cb0 20 20 54 69 63 6b 65 74 20 23 33 32 36 30 2e 0a Ticket #3260..
e0cc0 20 20 2a 2f 0a 20 20 69 66 28 20 2a 70 53 69 7a */. if( *pSiz
e0cd0 65 3d 3d 31 20 29 20 2a 70 53 69 7a 65 20 3d 20 e==1 ) *pSize =
e0ce0 30 3b 0a 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 0;... return SQ
e0cf0 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 LITE_OK;.}..#if
e0d00 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f SQLITE_ENABLE_LO
e0d10 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 CKING_STYLE && d
e0d20 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f efined(__APPLE__
e0d30 29 0a 2f 2a 0a 2a 2a 20 48 61 6e 64 6c 65 72 20 )./*.** Handler
e0d40 66 6f 72 20 70 72 6f 78 79 2d 6c 6f 63 6b 69 6e for proxy-lockin
e0d50 67 20 66 69 6c 65 2d 63 6f 6e 74 72 6f 6c 20 76 g file-control v
e0d60 65 72 62 73 2e 20 20 44 65 66 69 6e 65 64 20 62 erbs. Defined b
e0d70 65 6c 6f 77 20 69 6e 20 74 68 65 0a 2a 2a 20 70 elow in the.** p
e0d80 72 6f 78 79 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 roxying locking
e0d90 64 69 76 69 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 division..*/.sta
e0da0 74 69 63 20 69 6e 74 20 70 72 6f 78 79 46 69 6c tic int proxyFil
e0db0 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 eControl(sqlite3
e0dc0 5f 66 69 6c 65 2a 2c 69 6e 74 2c 76 6f 69 64 2a _file*,int,void*
e0dd0 29 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a );.#endif.../*.*
e0de0 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 6e * Information an
e0df0 64 20 63 6f 6e 74 72 6f 6c 20 6f 66 20 61 6e 20 d control of an
e0e00 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c 65 open file handle
e0e10 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
e0e20 75 6e 69 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 unixFileControl(
e0e30 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
e0e40 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a , int op, void *
e0e50 70 41 72 67 29 7b 0a 20 20 73 77 69 74 63 68 28 pArg){. switch(
e0e60 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 op ){. case
e0e70 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 SQLITE_FCNTL_LOC
e0e80 4b 53 54 41 54 45 3a 20 7b 0a 20 20 20 20 20 20 KSTATE: {.
e0e90 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 28 28 *(int*)pArg = ((
e0ea0 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c unixFile*)id)->l
e0eb0 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 20 20 72 ocktype;. r
e0ec0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
e0ed0 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
e0ee0 53 51 4c 49 54 45 5f 4c 41 53 54 5f 45 52 52 4e SQLITE_LAST_ERRN
e0ef0 4f 3a 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 O: {. *(int
e0f00 2a 29 70 41 72 67 20 3d 20 28 28 75 6e 69 78 46 *)pArg = ((unixF
e0f10 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 ile*)id)->lastEr
e0f20 72 6e 6f 3b 0a 20 20 20 20 20 20 72 65 74 75 72 rno;. retur
e0f30 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 n SQLITE_OK;.
e0f40 20 7d 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 }.#ifndef NDEBU
e0f50 47 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 G. /* The pag
e0f60 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 6d 65 er calls this me
e0f70 74 68 6f 64 20 74 6f 20 73 69 67 6e 61 6c 20 74 thod to signal t
e0f80 68 61 74 20 69 74 20 68 61 73 20 64 6f 6e 65 0a hat it has done.
e0f90 20 20 20 20 2a 2a 20 61 20 72 6f 6c 6c 62 61 63 ** a rollbac
e0fa0 6b 20 61 6e 64 20 74 68 61 74 20 74 68 65 20 64 k and that the d
e0fb0 61 74 61 62 61 73 65 20 69 73 20 74 68 65 72 65 atabase is there
e0fc0 66 6f 72 65 20 75 6e 63 68 61 6e 67 65 64 20 61 fore unchanged a
e0fd0 6e 64 0a 20 20 20 20 2a 2a 20 69 74 20 68 65 6e nd. ** it hen
e0fe0 63 65 20 69 74 20 69 73 20 4f 4b 20 66 6f 72 20 ce it is OK for
e0ff0 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
e1000 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 74 change counter t
e1010 6f 20 62 65 0a 20 20 20 20 2a 2a 20 75 6e 63 68 o be. ** unch
e1020 61 6e 67 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 anged.. */.
e1030 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 43 case SQLITE_FC
e1040 4e 54 4c 5f 44 42 5f 55 4e 43 48 41 4e 47 45 44 NTL_DB_UNCHANGED
e1050 3a 20 7b 0a 20 20 20 20 20 20 28 28 75 6e 69 78 : {. ((unix
e1060 46 69 6c 65 2a 29 69 64 29 2d 3e 64 62 55 70 64 File*)id)->dbUpd
e1070 61 74 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 ate = 0;. r
e1080 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
e1090 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69 . }.#endif.#i
e10a0 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
e10b0 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 LOCKING_STYLE &&
e10c0 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 defined(__APPLE
e10d0 5f 5f 29 0a 20 20 20 20 63 61 73 65 20 53 51 4c __). case SQL
e10e0 49 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 ITE_SET_LOCKPROX
e10f0 59 46 49 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 YFILE:. case
e1100 53 51 4c 49 54 45 5f 47 45 54 5f 4c 4f 43 4b 50 SQLITE_GET_LOCKP
e1110 52 4f 58 59 46 49 4c 45 3a 20 7b 0a 20 20 20 20 ROXYFILE: {.
e1120 20 20 72 65 74 75 72 6e 20 70 72 6f 78 79 46 69 return proxyFi
e1130 6c 65 43 6f 6e 74 72 6f 6c 28 69 64 2c 6f 70 2c leControl(id,op,
e1140 70 41 72 67 29 3b 0a 20 20 20 20 7d 0a 23 65 6e pArg);. }.#en
e1150 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e dif /* SQLITE_EN
e1160 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 ABLE_LOCKING_STY
e1170 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f LE && defined(__
e1180 41 50 50 4c 45 5f 5f 29 20 2a 2f 0a 20 20 7d 0a APPLE__) */. }.
e1190 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
e11a0 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ERROR;.}../*.**
e11b0 52 65 74 75 72 6e 20 74 68 65 20 73 65 63 74 6f Return the secto
e11c0 72 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 r size in bytes
e11d0 6f 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e of the underlyin
e11e0 67 20 62 6c 6f 63 6b 20 64 65 76 69 63 65 20 66 g block device f
e11f0 6f 72 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 66 or.** the specif
e1200 69 65 64 20 66 69 6c 65 2e 20 54 68 69 73 20 69 ied file. This i
e1210 73 20 61 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20 s almost always
e1220 35 31 32 20 62 79 74 65 73 2c 20 62 75 74 20 6d 512 bytes, but m
e1230 61 79 20 62 65 0a 2a 2a 20 6c 61 72 67 65 72 20 ay be.** larger
e1240 66 6f 72 20 73 6f 6d 65 20 64 65 76 69 63 65 73 for some devices
e1250 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 ..**.** SQLite c
e1260 6f 64 65 20 61 73 73 75 6d 65 73 20 74 68 69 73 ode assumes this
e1270 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 function cannot
e1280 20 66 61 69 6c 2e 20 49 74 20 61 6c 73 6f 20 61 fail. It also a
e1290 73 73 75 6d 65 73 20 74 68 61 74 0a 2a 2a 20 69 ssumes that.** i
e12a0 66 20 74 77 6f 20 66 69 6c 65 73 20 61 72 65 20 f two files are
e12b0 63 72 65 61 74 65 64 20 69 6e 20 74 68 65 20 73 created in the s
e12c0 61 6d 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 ame file-system
e12d0 64 69 72 65 63 74 6f 72 79 20 28 69 2e 65 2e 0a directory (i.e..
e12e0 2a 2a 20 61 20 64 61 74 61 62 61 73 65 20 61 6e ** a database an
e12f0 64 20 69 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69 d its journal fi
e1300 6c 65 29 20 74 68 61 74 20 74 68 65 20 73 65 63 le) that the sec
e1310 74 6f 72 20 73 69 7a 65 20 77 69 6c 6c 20 62 65 tor size will be
e1320 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 66 6f 72 the.** same for
e1330 20 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 both..*/.static
e1340 20 69 6e 74 20 75 6e 69 78 53 65 63 74 6f 72 53 int unixSectorS
e1350 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ize(sqlite3_file
e1360 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e *NotUsed){. UN
e1370 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e USED_PARAMETER(N
e1380 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 otUsed);. retur
e1390 6e 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 n SQLITE_DEFAULT
e13a0 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 7d 0a _SECTOR_SIZE;.}.
e13b0 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
e13c0 65 20 64 65 76 69 63 65 20 63 68 61 72 61 63 74 e device charact
e13d0 65 72 69 73 74 69 63 73 20 66 6f 72 20 74 68 65 eristics for the
e13e0 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61 file. This is a
e13f0 6c 77 61 79 73 20 30 20 66 6f 72 20 75 6e 69 78 lways 0 for unix
e1400 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
e1410 75 6e 69 78 44 65 76 69 63 65 43 68 61 72 61 63 unixDeviceCharac
e1420 74 65 72 69 73 74 69 63 73 28 73 71 6c 69 74 65 teristics(sqlite
e1430 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 29 3_file *NotUsed)
e1440 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d {. UNUSED_PARAM
e1450 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 ETER(NotUsed);.
e1460 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a return 0;.}../*
e1470 0a 2a 2a 20 48 65 72 65 20 65 6e 64 73 20 74 68 .** Here ends th
e1480 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
e1490 20 6f 66 20 61 6c 6c 20 73 71 6c 69 74 65 33 5f of all sqlite3_
e14a0 66 69 6c 65 20 6d 65 74 68 6f 64 73 2e 0a 2a 2a file methods..**
e14b0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
e14c0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 73 71 6c 69 ******* End sqli
e14d0 74 65 33 5f 66 69 6c 65 20 4d 65 74 68 6f 64 73 te3_file Methods
e14e0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
e14f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e1500 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
e1510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e1520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e1530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e1540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
e1550 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 64 69 76 ../*.** This div
e1560 69 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 64 ision contains d
e1570 65 66 69 6e 69 74 69 6f 6e 73 20 6f 66 20 73 71 efinitions of sq
e1580 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
e1590 20 6f 62 6a 65 63 74 73 20 74 68 61 74 0a 2a 2a objects that.**
e15a0 20 69 6d 70 6c 65 6d 65 6e 74 20 76 61 72 69 6f implement vario
e15b0 75 73 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 us file locking
e15c0 73 74 72 61 74 65 67 69 65 73 2e 20 20 49 74 20 strategies. It
e15d0 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 73 20 64 65 also contains de
e15e0 66 69 6e 69 74 69 6f 6e 73 0a 2a 2a 20 6f 66 20 finitions.** of
e15f0 22 66 69 6e 64 65 72 22 20 66 75 6e 63 74 69 6f "finder" functio
e1600 6e 73 2e 20 20 41 20 66 69 6e 64 65 72 2d 66 75 ns. A finder-fu
e1610 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 nction is used t
e1620 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 61 70 70 o locate the app
e1630 72 6f 70 72 69 61 74 65 0a 2a 2a 20 73 71 6c 69 ropriate.** sqli
e1640 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f te3_io_methods o
e1650 62 6a 65 63 74 20 66 6f 72 20 61 20 70 61 72 74 bject for a part
e1660 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 icular database
e1670 66 69 6c 65 2e 20 20 54 68 65 20 70 41 70 70 44 file. The pAppD
e1680 61 74 61 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 20 ata.** field of
e1690 74 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 20 the sqlite3_vfs
e16a0 56 46 53 20 6f 62 6a 65 63 74 73 20 61 72 65 20 VFS objects are
e16b0 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 62 initialized to b
e16c0 65 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a e pointers to.**
e16d0 20 74 68 65 20 63 6f 72 72 65 63 74 20 66 69 6e the correct fin
e16e0 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20 66 6f 72 der-function for
e16f0 20 74 68 61 74 20 56 46 53 2e 0a 2a 2a 0a 2a 2a that VFS..**.**
e1700 20 4d 6f 73 74 20 66 69 6e 64 65 72 20 66 75 6e Most finder fun
e1710 63 74 69 6f 6e 73 20 72 65 74 75 72 6e 20 61 20 ctions return a
e1720 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 69 78 pointer to a fix
e1730 65 64 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 ed sqlite3_io_me
e1740 74 68 6f 64 73 0a 2a 2a 20 6f 62 6a 65 63 74 2e thods.** object.
e1750 20 20 54 68 65 20 6f 6e 6c 79 20 69 6e 74 65 72 The only inter
e1760 65 73 74 69 6e 67 20 66 69 6e 64 65 72 2d 66 75 esting finder-fu
e1770 6e 63 74 69 6f 6e 20 69 73 20 61 75 74 6f 6c 6f nction is autolo
e1780 63 6b 49 6f 46 69 6e 64 65 72 2c 20 77 68 69 63 ckIoFinder, whic
e1790 68 0a 2a 2a 20 6c 6f 6f 6b 73 20 61 74 20 74 68 h.** looks at th
e17a0 65 20 66 69 6c 65 73 79 73 74 65 6d 20 74 79 70 e filesystem typ
e17b0 65 20 61 6e 64 20 74 72 69 65 73 20 74 6f 20 67 e and tries to g
e17c0 75 65 73 73 20 74 68 65 20 62 65 73 74 20 6c 6f uess the best lo
e17d0 63 6b 69 6e 67 0a 2a 2a 20 73 74 72 61 74 65 67 cking.** strateg
e17e0 79 20 66 72 6f 6d 20 74 68 61 74 2e 0a 2a 2a 0a y from that..**.
e17f0 2a 2a 20 46 6f 72 20 66 69 6e 64 65 72 2d 66 75 ** For finder-fu
e1800 6e 74 69 6f 6e 20 46 2c 20 74 77 6f 20 6f 62 6a ntion F, two obj
e1810 65 63 74 73 20 61 72 65 20 63 72 65 61 74 65 64 ects are created
e1820 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 28 31 29 20 54 :.**.** (1) T
e1830 68 65 20 72 65 61 6c 20 66 69 6e 64 65 72 2d 66 he real finder-f
e1840 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 64 20 22 46 unction named "F
e1850 49 6d 70 74 28 29 22 2e 0a 2a 2a 0a 2a 2a 20 20 Impt()"..**.**
e1860 20 20 28 32 29 20 41 20 63 6f 6e 73 74 61 6e 74 (2) A constant
e1870 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 pointer to this
e1880 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 64 20 function named
e1890 6a 75 73 74 20 22 46 22 2e 0a 2a 2a 0a 2a 2a 0a just "F"..**.**.
e18a0 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 ** A pointer to
e18b0 74 68 65 20 46 20 70 6f 69 6e 74 65 72 20 69 73 the F pointer is
e18c0 20 75 73 65 64 20 61 73 20 74 68 65 20 70 41 70 used as the pAp
e18d0 70 44 61 74 61 20 76 61 6c 75 65 20 66 6f 72 20 pData value for
e18e0 56 46 53 0a 2a 2a 20 6f 62 6a 65 63 74 73 2e 20 VFS.** objects.
e18f0 20 57 65 20 68 61 76 65 20 74 6f 20 64 6f 20 74 We have to do t
e1900 68 69 73 20 69 6e 73 74 65 61 64 20 6f 66 20 6c his instead of l
e1910 65 74 74 69 6e 67 20 70 41 70 70 44 61 74 61 20 etting pAppData
e1920 70 6f 69 6e 74 0a 2a 2a 20 64 69 72 65 63 74 6c point.** directl
e1930 79 20 61 74 20 74 68 65 20 66 69 6e 64 65 72 2d y at the finder-
e1940 66 75 6e 63 74 69 6f 6e 20 73 69 6e 63 65 20 43 function since C
e1950 39 30 20 72 75 6c 65 73 20 70 72 65 76 65 6e 74 90 rules prevent
e1960 20 61 20 76 6f 69 64 2a 0a 2a 2a 20 66 72 6f 6d a void*.** from
e1970 20 62 65 20 63 61 73 74 20 69 6e 74 6f 20 61 20 be cast into a
e1980 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 function pointer
e1990 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 ..**.**.** Each
e19a0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 instance of this
e19b0 20 6d 61 63 72 6f 20 67 65 6e 65 72 61 74 65 73 macro generates
e19c0 20 74 77 6f 20 6f 62 6a 65 63 74 73 3a 0a 2a 2a two objects:.**
e19d0 0a 2a 2a 20 20 20 2a 20 20 41 20 63 6f 6e 73 74 .** * A const
e19e0 61 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d ant sqlite3_io_m
e19f0 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 63 61 ethods object ca
e1a00 6c 6c 20 4d 45 54 48 4f 44 20 74 68 61 74 20 68 ll METHOD that h
e1a10 61 73 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 20 20 as locking.**
e1a20 20 20 20 6d 65 74 68 6f 64 73 20 43 4c 4f 53 45 methods CLOSE
e1a30 2c 20 4c 4f 43 4b 2c 20 55 4e 4c 4f 43 4b 2c 20 , LOCK, UNLOCK,
e1a40 43 4b 52 45 53 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a CKRESLOCK..**.**
e1a50 20 20 20 2a 20 20 41 6e 20 49 2f 4f 20 6d 65 74 * An I/O met
e1a60 68 6f 64 20 66 69 6e 64 65 72 20 66 75 6e 63 74 hod finder funct
e1a70 69 6f 6e 20 63 61 6c 6c 65 64 20 46 49 4e 44 45 ion called FINDE
e1a80 52 20 74 68 61 74 20 72 65 74 75 72 6e 73 20 61 R that returns a
e1a90 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 20 20 20 20 pointer.**
e1aa0 20 74 6f 20 74 68 65 20 4d 45 54 48 4f 44 20 6f to the METHOD o
e1ab0 62 6a 65 63 74 20 69 6e 20 74 68 65 20 70 72 65 bject in the pre
e1ac0 76 69 6f 75 73 20 62 75 6c 6c 65 74 2e 0a 2a 2f vious bullet..*/
e1ad0 0a 23 64 65 66 69 6e 65 20 49 4f 4d 45 54 48 4f .#define IOMETHO
e1ae0 44 53 28 46 49 4e 44 45 52 2c 20 4d 45 54 48 4f DS(FINDER, METHO
e1af0 44 2c 20 43 4c 4f 53 45 2c 20 4c 4f 43 4b 2c 20 D, CLOSE, LOCK,
e1b00 55 4e 4c 4f 43 4b 2c 20 43 4b 4c 4f 43 4b 29 20 UNLOCK, CKLOCK)
e1b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
e1b20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c static const sql
e1b30 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 ite3_io_methods
e1b40 4d 45 54 48 4f 44 20 3d 20 7b 20 20 20 20 20 20 METHOD = {
e1b50 20 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 5c 0a 20 \.
e1b70 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 1,
e1b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e1b90 20 69 56 65 72 73 69 6f 6e 20 2a 2f 20 20 20 20 iVersion */
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 5c 0a 20 20 \.
e1bc0 20 43 4c 4f 53 45 2c 20 20 20 20 20 20 20 20 20 CLOSE,
e1bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
e1be0 78 43 6c 6f 73 65 20 2a 2f 20 20 20 20 20 20 20 xClose */
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 5c 0a 20 20 20 \.
e1c10 75 6e 69 78 52 65 61 64 2c 20 20 20 20 20 20 20 unixRead,
e1c20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
e1c30 52 65 61 64 20 2a 2f 20 20 20 20 20 20 20 20 20 Read */
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 5c 0a 20 20 20 75 \. u
e1c60 6e 69 78 57 72 69 74 65 2c 20 20 20 20 20 20 20 nixWrite,
e1c70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 57 /* xW
e1c80 72 69 74 65 20 2a 2f 20 20 20 20 20 20 20 20 20 rite */
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 5c 0a 20 20 20 75 6e \. un
e1cb0 69 78 54 72 75 6e 63 61 74 65 2c 20 20 20 20 20 ixTruncate,
e1cc0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 54 72 /* xTr
e1cd0 75 6e 63 61 74 65 20 2a 2f 20 20 20 20 20 20 20 uncate */
e1ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1cf0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 \. uni
e1d00 78 53 79 6e 63 2c 20 20 20 20 20 20 20 20 20 20 xSync,
e1d10 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 79 6e /* xSyn
e1d20 63 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 c */
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 5c 0a 20 20 20 75 6e 69 78 \. unix
e1d50 46 69 6c 65 53 69 7a 65 2c 20 20 20 20 20 20 20 FileSize,
e1d60 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69 6c 65 /* xFile
e1d70 53 69 7a 65 20 2a 2f 20 20 20 20 20 20 20 20 20 Size */
e1d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1d90 20 20 20 20 20 20 5c 0a 20 20 20 4c 4f 43 4b 2c \. LOCK,
e1da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1db0 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 /* xLock
e1dc0 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
e1dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1de0 20 20 20 20 20 5c 0a 20 20 20 55 4e 4c 4f 43 4b \. UNLOCK
e1df0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
e1e00 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b /* xUnlock
e1e10 20 2a 2f 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 5c 0a 20 20 20 43 4b 4c 4f 43 4b 2c \. CKLOCK,
e1e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1e50 20 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 /* xCheckRe
e1e60 73 65 72 76 65 64 4c 6f 63 6b 20 2a 2f 20 20 20 servedLock */
e1e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1e80 20 20 20 5c 0a 20 20 20 75 6e 69 78 46 69 6c 65 \. unixFile
e1e90 43 6f 6e 74 72 6f 6c 2c 20 20 20 20 20 20 20 20 Control,
e1ea0 20 20 20 20 2f 2a 20 78 46 69 6c 65 43 6f 6e 74 /* xFileCont
e1eb0 72 6f 6c 20 2a 2f 20 20 20 20 20 20 20 20 20 20 rol */
e1ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1ed0 20 20 5c 0a 20 20 20 75 6e 69 78 53 65 63 74 6f \. unixSecto
e1ee0 72 53 69 7a 65 2c 20 20 20 20 20 20 20 20 20 20 rSize,
e1ef0 20 20 20 2f 2a 20 78 53 65 63 74 6f 72 53 69 7a /* xSectorSiz
e1f00 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 e */
e1f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1f20 20 5c 0a 20 20 20 75 6e 69 78 44 65 76 69 63 65 \. unixDevice
e1f30 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20 Characteristics
e1f40 20 20 2f 2a 20 78 44 65 76 69 63 65 43 61 70 61 /* xDeviceCapa
e1f50 62 69 6c 69 74 69 65 73 20 2a 2f 20 20 20 20 20 bilities */
e1f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1f70 5c 0a 7d 3b 20 20 20 20 20 20 20 20 20 20 20 20 \.};
e1f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
e1fc0 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 .static const sq
e1fd0 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
e1fe0 20 2a 46 49 4e 44 45 52 23 23 49 6d 70 6c 28 63 *FINDER##Impl(c
e1ff0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 75 6e onst char *z, un
e2000 69 78 46 69 6c 65 20 2a 70 29 7b 20 20 20 5c 0a ixFile *p){ \.
e2010 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
e2020 45 52 28 7a 29 3b 20 55 4e 55 53 45 44 5f 50 41 ER(z); UNUSED_PA
e2030 52 41 4d 45 54 45 52 28 70 29 3b 20 20 20 20 20 RAMETER(p);
e2040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2050 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
e2060 20 72 65 74 75 72 6e 20 26 4d 45 54 48 4f 44 3b return &METHOD;
e2070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e20a0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 20 \.}
e20b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e20c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e20d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e20e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e20f0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 73 74 61 \.sta
e2100 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 tic const sqlite
e2110 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 28 2a 3_io_methods *(*
e2120 63 6f 6e 73 74 20 46 49 4e 44 45 52 29 28 63 6f const FINDER)(co
e2130 6e 73 74 20 63 68 61 72 2a 2c 75 6e 69 78 46 69 nst char*,unixFi
e2140 6c 65 20 2a 70 29 20 20 20 20 5c 0a 20 20 20 20 le *p) \.
e2150 3d 20 46 49 4e 44 45 52 23 23 49 6d 70 6c 3b 0a = FINDER##Impl;.
e2160 0a 2f 2a 0a 2a 2a 20 48 65 72 65 20 61 72 65 20 ./*.** Here are
e2170 61 6c 6c 20 6f 66 20 74 68 65 20 73 71 6c 69 74 all of the sqlit
e2180 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 e3_io_methods ob
e2190 6a 65 63 74 73 20 66 6f 72 20 65 61 63 68 20 6f jects for each o
e21a0 66 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 f the.** locking
e21b0 20 73 74 72 61 74 65 67 69 65 73 2e 20 20 46 75 strategies. Fu
e21c0 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 72 65 74 nctions that ret
e21d0 75 72 6e 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 urn pointers to
e21e0 74 68 65 73 65 20 6d 65 74 68 6f 64 73 0a 2a 2a these methods.**
e21f0 20 61 72 65 20 61 6c 73 6f 20 63 72 65 61 74 65 are also create
e2200 64 2e 0a 2a 2f 0a 49 4f 4d 45 54 48 4f 44 53 28 d..*/.IOMETHODS(
e2210 0a 20 20 70 6f 73 69 78 49 6f 46 69 6e 64 65 72 . posixIoFinder
e2220 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
e2230 46 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 Finder function
e2240 6e 61 6d 65 20 2a 2f 0a 20 20 70 6f 73 69 78 49 name */. posixI
e2250 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20 20 oMethods,
e2260 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 /* sqlite3_i
e2270 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 o_methods object
e2280 20 6e 61 6d 65 20 2a 2f 0a 20 20 75 6e 69 78 43 name */. unixC
e2290 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 lose,
e22a0 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d /* xClose m
e22b0 65 74 68 6f 64 20 2a 2f 0a 20 20 75 6e 69 78 4c ethod */. unixL
e22c0 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 ock,
e22d0 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 /* xLock me
e22e0 74 68 6f 64 20 2a 2f 0a 20 20 75 6e 69 78 55 6e thod */. unixUn
e22f0 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 lock,
e2300 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d /* xUnlock m
e2310 65 74 68 6f 64 20 2a 2f 0a 20 20 75 6e 69 78 43 ethod */. unixC
e2320 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b heckReservedLock
e2330 20 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 /* xCheckRe
e2340 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f servedLock metho
e2350 64 20 2a 2f 0a 29 0a 49 4f 4d 45 54 48 4f 44 53 d */.).IOMETHODS
e2360 28 0a 20 20 6e 6f 6c 6f 63 6b 49 6f 46 69 6e 64 (. nolockIoFind
e2370 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a er, /*
e2380 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e Finder function
e2390 20 6e 61 6d 65 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 name */. noloc
e23a0 6b 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 kIoMethods,
e23b0 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f /* sqlite3_
e23c0 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 io_methods objec
e23d0 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 6e 6f 6c 6f t name */. nolo
e23e0 63 6b 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 ckClose,
e23f0 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 /* xClose
e2400 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 6e 6f 6c 6f method */. nolo
e2410 63 6b 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 ckLock,
e2420 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d /* xLock m
e2430 65 74 68 6f 64 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 ethod */. noloc
e2440 6b 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 kUnlock,
e2450 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 /* xUnlock
e2460 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 6e 6f 6c 6f method */. nolo
e2470 63 6b 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c ckCheckReservedL
e2480 6f 63 6b 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 ock /* xCheckR
e2490 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74 68 eservedLock meth
e24a0 6f 64 20 2a 2f 0a 29 0a 49 4f 4d 45 54 48 4f 44 od */.).IOMETHOD
e24b0 53 28 0a 20 20 64 6f 74 6c 6f 63 6b 49 6f 46 69 S(. dotlockIoFi
e24c0 6e 64 65 72 2c 20 20 20 20 20 20 20 20 20 20 2f nder, /
e24d0 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69 6f * Finder functio
e24e0 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 64 6f 74 6c n name */. dotl
e24f0 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 ockIoMethods,
e2500 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 /* sqlite3
e2510 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 _io_methods obje
e2520 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 64 6f 74 ct name */. dot
e2530 6c 6f 63 6b 43 6c 6f 73 65 2c 20 20 20 20 20 20 lockClose,
e2540 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 /* xClose
e2550 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 64 6f 74 method */. dot
e2560 6c 6f 63 6b 4c 6f 63 6b 2c 20 20 20 20 20 20 20 lockLock,
e2570 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 /* xLock
e2580 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 64 6f 74 6c method */. dotl
e2590 6f 63 6b 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 ockUnlock,
e25a0 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b /* xUnlock
e25b0 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 64 6f 74 method */. dot
e25c0 6c 6f 63 6b 43 68 65 63 6b 52 65 73 65 72 76 65 lockCheckReserve
e25d0 64 4c 6f 63 6b 20 20 2f 2a 20 78 43 68 65 63 6b dLock /* xCheck
e25e0 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74 ReservedLock met
e25f0 68 6f 64 20 2a 2f 0a 29 0a 0a 23 69 66 20 53 51 hod */.)..#if SQ
e2600 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b LITE_ENABLE_LOCK
e2610 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 21 4f 53 ING_STYLE && !OS
e2620 5f 56 58 57 4f 52 4b 53 0a 49 4f 4d 45 54 48 4f _VXWORKS.IOMETHO
e2630 44 53 28 0a 20 20 66 6c 6f 63 6b 49 6f 46 69 6e DS(. flockIoFin
e2640 64 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 der,
e2650 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69 /* Finder functi
e2660 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 66 6c 6f on name */. flo
e2670 63 6b 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 ckIoMethods,
e2680 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 /* sqlite
e2690 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 3_io_methods obj
e26a0 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 66 6c ect name */. fl
e26b0 6f 63 6b 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 ockClose,
e26c0 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 /* xClos
e26d0 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 66 6c e method */. fl
e26e0 6f 63 6b 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 ockLock,
e26f0 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b /* xLock
e2700 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 66 6c 6f method */. flo
e2710 63 6b 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 ckUnlock,
e2720 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 /* xUnloc
e2730 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 66 6c k method */. fl
e2740 6f 63 6b 43 68 65 63 6b 52 65 73 65 72 76 65 64 ockCheckReserved
e2750 4c 6f 63 6b 20 20 20 20 2f 2a 20 78 43 68 65 63 Lock /* xChec
e2760 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 kReservedLock me
e2770 74 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66 thod */.).#endif
e2780 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 ..#if OS_VXWORKS
e2790 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 73 65 .IOMETHODS(. se
e27a0 6d 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 20 mIoFinder,
e27b0 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 /* Finde
e27c0 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 r function name
e27d0 2a 2f 0a 20 20 73 65 6d 49 6f 4d 65 74 68 6f 64 */. semIoMethod
e27e0 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f s, /
e27f0 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 * sqlite3_io_met
e2800 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 hods object name
e2810 20 2a 2f 0a 20 20 73 65 6d 43 6c 6f 73 65 2c 20 */. semClose,
e2820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2830 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 /* xClose method
e2840 20 2a 2f 0a 20 20 73 65 6d 4c 6f 63 6b 2c 20 20 */. semLock,
e2850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2860 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 /* xLock method
e2870 2a 2f 0a 20 20 73 65 6d 55 6e 6c 6f 63 6b 2c 20 */. semUnlock,
e2880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
e2890 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 * xUnlock method
e28a0 20 2a 2f 0a 20 20 73 65 6d 43 68 65 63 6b 52 65 */. semCheckRe
e28b0 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 20 20 servedLock
e28c0 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 /* xCheckReserve
e28d0 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a dLock method */.
e28e0 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 64 65 ).#endif..#if de
e28f0 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 fined(__APPLE__)
e2900 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c && SQLITE_ENABL
e2910 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a E_LOCKING_STYLE.
e2920 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 61 66 70 IOMETHODS(. afp
e2930 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 20 20 IoFinder,
e2940 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 /* Finder
e2950 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a function name *
e2960 2f 0a 20 20 61 66 70 49 6f 4d 65 74 68 6f 64 73 /. afpIoMethods
e2970 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a , /*
e2980 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
e2990 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 ods object name
e29a0 2a 2f 0a 20 20 61 66 70 43 6c 6f 73 65 2c 20 20 */. afpClose,
e29b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
e29c0 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 * xClose method
e29d0 2a 2f 0a 20 20 61 66 70 4c 6f 63 6b 2c 20 20 20 */. afpLock,
e29e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
e29f0 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a * xLock method *
e2a00 2f 0a 20 20 61 66 70 55 6e 6c 6f 63 6b 2c 20 20 /. afpUnlock,
e2a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e2a20 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 xUnlock method
e2a30 2a 2f 0a 20 20 61 66 70 43 68 65 63 6b 52 65 73 */. afpCheckRes
e2a40 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 20 20 2f ervedLock /
e2a50 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 * xCheckReserved
e2a60 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 Lock method */.)
e2a70 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
e2a80 68 65 20 22 57 68 6f 6c 65 20 46 69 6c 65 20 4c he "Whole File L
e2a90 6f 63 6b 69 6e 67 22 20 66 69 6e 64 65 72 20 72 ocking" finder r
e2aa0 65 74 75 72 6e 73 20 74 68 65 20 73 61 6d 65 20 eturns the same
e2ab0 73 65 74 20 6f 66 20 6d 65 74 68 6f 64 73 20 61 set of methods a
e2ac0 73 0a 2a 2a 20 74 68 65 20 70 6f 73 69 78 20 6c s.** the posix l
e2ad0 6f 63 6b 69 6e 67 20 66 69 6e 64 65 72 2e 20 20 ocking finder.
e2ae0 42 75 74 20 69 74 20 61 6c 73 6f 20 73 65 74 73 But it also sets
e2af0 20 74 68 65 20 53 51 4c 49 54 45 5f 57 48 4f 4c the SQLITE_WHOL
e2b00 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 0a 2a E_FILE_LOCKING.*
e2b10 2a 20 66 6c 61 67 20 74 6f 20 66 6f 72 63 65 20 * flag to force
e2b20 74 68 65 20 70 6f 73 69 78 20 61 64 76 69 73 6f the posix adviso
e2b30 72 79 20 6c 6f 63 6b 73 20 74 6f 20 63 6f 76 65 ry locks to cove
e2b40 72 20 74 68 65 20 77 68 6f 6c 65 20 66 69 6c 65 r the whole file
e2b50 20 69 6e 73 74 65 61 64 0a 2a 2a 20 6f 66 20 6a instead.** of j
e2b60 75 73 74 20 61 20 73 6d 61 6c 6c 20 73 70 61 6e ust a small span
e2b70 20 6f 66 20 62 79 74 65 73 20 6e 65 61 72 20 74 of bytes near t
e2b80 68 65 20 31 47 69 42 20 62 6f 75 6e 64 61 72 79 he 1GiB boundary
e2b90 2e 20 20 57 68 6f 6c 65 20 46 69 6c 65 20 4c 6f . Whole File Lo
e2ba0 63 6b 69 6e 67 0a 2a 2a 20 69 73 20 75 73 65 66 cking.** is usef
e2bb0 75 6c 20 6f 6e 20 4e 46 53 2d 6d 6f 75 6e 74 65 ul on NFS-mounte
e2bc0 64 20 66 69 6c 65 73 20 73 69 6e 63 65 20 69 74 d files since it
e2bd0 20 68 65 6c 70 73 20 4e 46 53 20 74 6f 20 6d 61 helps NFS to ma
e2be0 69 6e 74 61 69 6e 20 63 61 63 68 65 0a 2a 2a 20 intain cache.**
e2bf0 63 6f 68 65 72 65 6e 63 79 2e 20 20 42 75 74 20 coherency. But
e2c00 69 74 20 69 73 20 61 20 64 65 74 72 69 6d 65 6e it is a detrimen
e2c10 74 20 74 6f 20 6f 74 68 65 72 20 66 69 6c 65 73 t to other files
e2c20 79 73 74 65 6d 73 20 73 69 6e 63 65 20 69 74 20 ystems since it
e2c30 72 75 6e 73 0a 2a 2a 20 73 6c 6f 77 65 72 2e 0a runs.** slower..
e2c40 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 */.static const
e2c50 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
e2c60 64 73 20 2a 70 6f 73 69 78 57 66 6c 49 6f 46 69 ds *posixWflIoFi
e2c70 6e 64 65 72 49 6d 70 6c 28 63 6f 6e 73 74 20 63 nderImpl(const c
e2c80 68 61 72 2a 7a 2c 20 75 6e 69 78 46 69 6c 65 2a har*z, unixFile*
e2c90 70 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 p){. UNUSED_PAR
e2ca0 41 4d 45 54 45 52 28 7a 29 3b 0a 20 20 70 2d 3e AMETER(z);. p->
e2cb0 66 69 6c 65 46 6c 61 67 73 20 3d 20 53 51 4c 49 fileFlags = SQLI
e2cc0 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f TE_WHOLE_FILE_LO
e2cd0 43 4b 49 4e 47 3b 0a 20 20 72 65 74 75 72 6e 20 CKING;. return
e2ce0 26 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73 3b &posixIoMethods;
e2cf0 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 .}.static const
e2d00 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
e2d10 64 73 20 0a 20 20 2a 28 2a 63 6f 6e 73 74 20 70 ds . *(*const p
e2d20 6f 73 69 78 57 66 6c 49 6f 46 69 6e 64 65 72 29 osixWflIoFinder)
e2d30 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 6e 69 (const char*,uni
e2d40 78 46 69 6c 65 20 2a 70 29 20 3d 20 70 6f 73 69 xFile *p) = posi
e2d50 78 57 66 6c 49 6f 46 69 6e 64 65 72 49 6d 70 6c xWflIoFinderImpl
e2d60 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 72 6f ;../*.** The pro
e2d70 78 79 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f xy locking metho
e2d80 64 20 69 73 20 61 20 22 73 75 70 65 72 2d 6d 65 d is a "super-me
e2d90 74 68 6f 64 22 20 69 6e 20 74 68 65 20 73 65 6e thod" in the sen
e2da0 73 65 20 74 68 61 74 20 69 74 0a 2a 2a 20 6f 70 se that it.** op
e2db0 65 6e 73 20 73 65 63 6f 6e 64 61 72 79 20 66 69 ens secondary fi
e2dc0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 66 le descriptors f
e2dd0 6f 72 20 74 68 65 20 63 6f 6e 63 68 20 61 6e 64 or the conch and
e2de0 20 6c 6f 63 6b 20 66 69 6c 65 73 20 61 6e 64 0a lock files and.
e2df0 2a 2a 20 69 74 20 75 73 65 73 20 70 72 6f 78 79 ** it uses proxy
e2e00 2c 20 64 6f 74 2d 66 69 6c 65 2c 20 41 46 50 2c , dot-file, AFP,
e2e10 20 61 6e 64 20 66 6c 6f 63 6b 28 29 20 6c 6f 63 and flock() loc
e2e20 6b 69 6e 67 20 6d 65 74 68 6f 64 73 20 6f 6e 20 king methods on
e2e30 74 68 6f 73 65 0a 2a 2a 20 73 65 63 6f 6e 64 61 those.** seconda
e2e40 72 79 20 66 69 6c 65 73 2e 20 20 46 6f 72 20 74 ry files. For t
e2e50 68 69 73 20 72 65 61 73 6f 6e 2c 20 74 68 65 20 his reason, the
e2e60 64 69 76 69 73 69 6f 6e 20 74 68 61 74 20 69 6d division that im
e2e70 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 70 72 6f 78 plements.** prox
e2e80 79 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6c 6f 63 y locking is loc
e2e90 61 74 65 64 20 6d 75 63 68 20 66 75 72 74 68 65 ated much furthe
e2ea0 72 20 64 6f 77 6e 20 69 6e 20 74 68 65 20 66 69 r down in the fi
e2eb0 6c 65 2e 20 20 42 75 74 20 77 65 20 6e 65 65 64 le. But we need
e2ec0 0a 2a 2a 20 74 6f 20 67 6f 20 61 68 65 61 64 20 .** to go ahead
e2ed0 61 6e 64 20 64 65 66 69 6e 65 20 74 68 65 20 73 and define the s
e2ee0 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
e2ef0 73 20 61 6e 64 20 66 69 6e 64 65 72 20 66 75 6e s and finder fun
e2f00 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 70 72 6f ction.** for pro
e2f10 78 79 20 6c 6f 63 6b 69 6e 67 20 68 65 72 65 2e xy locking here.
e2f20 20 20 53 6f 20 77 65 20 66 6f 72 77 61 72 64 20 So we forward
e2f30 64 65 63 6c 61 72 65 20 74 68 65 20 49 2f 4f 20 declare the I/O
e2f40 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 23 69 66 20 methods..*/.#if
e2f50 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f defined(__APPLE_
e2f60 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 _) && SQLITE_ENA
e2f70 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c BLE_LOCKING_STYL
e2f80 45 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f E.static int pro
e2f90 78 79 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f xyClose(sqlite3_
e2fa0 66 69 6c 65 2a 29 3b 0a 73 74 61 74 69 63 20 69 file*);.static i
e2fb0 6e 74 20 70 72 6f 78 79 4c 6f 63 6b 28 73 71 6c nt proxyLock(sql
e2fc0 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 ite3_file*, int)
e2fd0 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f ;.static int pro
e2fe0 78 79 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 xyUnlock(sqlite3
e2ff0 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 73 74 _file*, int);.st
e3000 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43 68 atic int proxyCh
e3010 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 eckReservedLock(
e3020 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 sqlite3_file*, i
e3030 6e 74 2a 29 3b 0a 49 4f 4d 45 54 48 4f 44 53 28 nt*);.IOMETHODS(
e3040 0a 20 20 70 72 6f 78 79 49 6f 46 69 6e 64 65 72 . proxyIoFinder
e3050 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
e3060 46 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 Finder function
e3070 6e 61 6d 65 20 2a 2f 0a 20 20 70 72 6f 78 79 49 name */. proxyI
e3080 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20 20 oMethods,
e3090 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 /* sqlite3_i
e30a0 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 o_methods object
e30b0 20 6e 61 6d 65 20 2a 2f 0a 20 20 70 72 6f 78 79 name */. proxy
e30c0 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 Close,
e30d0 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d /* xClose m
e30e0 65 74 68 6f 64 20 2a 2f 0a 20 20 70 72 6f 78 79 ethod */. proxy
e30f0 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 Lock,
e3100 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 /* xLock me
e3110 74 68 6f 64 20 2a 2f 0a 20 20 70 72 6f 78 79 55 thod */. proxyU
e3120 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 nlock,
e3130 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d /* xUnlock m
e3140 65 74 68 6f 64 20 2a 2f 0a 20 20 70 72 6f 78 79 ethod */. proxy
e3150 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
e3160 6b 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 k /* xCheckRe
e3170 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f servedLock metho
e3180 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66 0a 0a 0a d */.).#endif...
e3190 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 #if defined(__AP
e31a0 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 PLE__) && SQLITE
e31b0 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f _ENABLE_LOCKING_
e31c0 53 54 59 4c 45 0a 2f 2a 20 0a 2a 2a 20 54 68 69 STYLE./* .** Thi
e31d0 73 20 22 66 69 6e 64 65 72 22 20 66 75 6e 63 74 s "finder" funct
e31e0 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 ion attempts to
e31f0 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 62 65 determine the be
e3200 73 74 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 st locking strat
e3210 65 67 79 20 0a 2a 2a 20 66 6f 72 20 74 68 65 20 egy .** for the
e3220 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 22 66 database file "f
e3230 69 6c 65 50 61 74 68 22 2e 20 20 49 74 20 74 68 ilePath". It th
e3240 65 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 en returns the s
e3250 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
e3260 73 0a 2a 2a 20 6f 62 6a 65 63 74 20 74 68 61 74 s.** object that
e3270 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 61 74 implements that
e3280 20 73 74 72 61 74 65 67 79 2e 0a 2a 2a 0a 2a 2a strategy..**.**
e3290 20 54 68 69 73 20 69 73 20 66 6f 72 20 4d 61 63 This is for Mac
e32a0 4f 53 58 20 6f 6e 6c 79 2e 0a 2a 2f 0a 73 74 61 OSX only..*/.sta
e32b0 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 tic const sqlite
e32c0 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 61 75 3_io_methods *au
e32d0 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d tolockIoFinderIm
e32e0 70 6c 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 pl(. const char
e32f0 20 2a 66 69 6c 65 50 61 74 68 2c 20 20 20 20 2f *filePath, /
e3300 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 * name of the da
e3310 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 tabase file */.
e3320 20 75 6e 69 78 46 69 6c 65 20 2a 70 4e 65 77 20 unixFile *pNew
e3330 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 70 65 /* ope
e3340 6e 20 66 69 6c 65 20 6f 62 6a 65 63 74 20 66 6f n file object fo
e3350 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 r the database f
e3360 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 73 74 61 74 ile */.){. stat
e3370 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 ic const struct
e3380 4d 61 70 70 69 6e 67 20 7b 0a 20 20 20 20 63 6f Mapping {. co
e3390 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 73 nst char *zFiles
e33a0 79 73 74 65 6d 3b 20 20 20 20 20 20 20 20 20 20 ystem;
e33b0 20 20 20 20 2f 2a 20 46 69 6c 65 73 79 73 74 65 /* Filesyste
e33c0 6d 20 74 79 70 65 20 6e 61 6d 65 20 2a 2f 0a 20 m type name */.
e33d0 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 const sqlite3
e33e0 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 _io_methods *pMe
e33f0 74 68 6f 64 73 3b 20 20 20 2f 2a 20 41 70 70 72 thods; /* Appr
e3400 6f 70 72 69 61 74 65 20 6c 6f 63 6b 69 6e 67 20 opriate locking
e3410 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 7d 20 61 4d method */. } aM
e3420 61 70 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b 20 22 ap[] = {. { "
e3430 68 66 73 22 2c 20 20 20 20 26 70 6f 73 69 78 49 hfs", &posixI
e3440 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 20 20 20 20 oMethods },.
e3450 7b 20 22 75 66 73 22 2c 20 20 20 20 26 70 6f 73 { "ufs", &pos
e3460 69 78 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 20 ixIoMethods },.
e3470 20 20 20 7b 20 22 61 66 70 66 73 22 2c 20 20 26 { "afpfs", &
e3480 61 66 70 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a afpIoMethods },.
e3490 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e #ifdef SQLITE_EN
e34a0 41 42 4c 45 5f 41 46 50 5f 4c 4f 43 4b 49 4e 47 ABLE_AFP_LOCKING
e34b0 5f 53 4d 42 0a 20 20 20 20 7b 20 22 73 6d 62 66 _SMB. { "smbf
e34c0 73 22 2c 20 20 26 61 66 70 49 6f 4d 65 74 68 6f s", &afpIoMetho
e34d0 64 73 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 20 20 ds },.#else.
e34e0 7b 20 22 73 6d 62 66 73 22 2c 20 20 26 66 6c 6f { "smbfs", &flo
e34f0 63 6b 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 23 ckIoMethods },.#
e3500 65 6e 64 69 66 0a 20 20 20 20 7b 20 22 77 65 62 endif. { "web
e3510 64 61 76 22 2c 20 26 6e 6f 6c 6f 63 6b 49 6f 4d dav", &nolockIoM
e3520 65 74 68 6f 64 73 20 7d 2c 0a 20 20 20 20 7b 20 ethods },. {
e3530 30 2c 20 30 20 7d 0a 20 20 7d 3b 0a 20 20 69 6e 0, 0 }. };. in
e3540 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 73 74 t i;. struct st
e3550 61 74 66 73 20 66 73 49 6e 66 6f 3b 0a 20 20 73 atfs fsInfo;. s
e3560 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b truct flock lock
e3570 49 6e 66 6f 3b 0a 0a 20 20 69 66 28 20 21 66 69 Info;.. if( !fi
e3580 6c 65 50 61 74 68 20 29 7b 0a 20 20 20 20 2f 2a lePath ){. /*
e3590 20 49 66 20 66 69 6c 65 50 61 74 68 3d 3d 4e 55 If filePath==NU
e35a0 4c 4c 20 74 68 61 74 20 6d 65 61 6e 73 20 77 65 LL that means we
e35b0 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 are dealing wit
e35c0 68 20 61 20 74 72 61 6e 73 69 65 6e 74 20 66 69 h a transient fi
e35d0 6c 65 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 64 le. ** that d
e35e0 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 oes not need to
e35f0 62 65 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 be locked. */.
e3600 20 20 72 65 74 75 72 6e 20 26 6e 6f 6c 6f 63 6b return &nolock
e3610 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 0a 20 IoMethods;. }.
e3620 20 69 66 28 20 73 74 61 74 66 73 28 66 69 6c 65 if( statfs(file
e3630 50 61 74 68 2c 20 26 66 73 49 6e 66 6f 29 20 21 Path, &fsInfo) !
e3640 3d 20 2d 31 20 29 7b 0a 20 20 20 20 69 66 28 20 = -1 ){. if(
e3650 66 73 49 6e 66 6f 2e 66 5f 66 6c 61 67 73 20 26 fsInfo.f_flags &
e3660 20 4d 4e 54 5f 52 44 4f 4e 4c 59 20 29 7b 0a 20 MNT_RDONLY ){.
e3670 20 20 20 20 20 72 65 74 75 72 6e 20 26 6e 6f 6c return &nol
e3680 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 ockIoMethods;.
e3690 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b }. for(i=0;
e36a0 20 61 4d 61 70 5b 69 5d 2e 7a 46 69 6c 65 73 79 aMap[i].zFilesy
e36b0 73 74 65 6d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 stem; i++){.
e36c0 20 20 69 66 28 20 73 74 72 63 6d 70 28 66 73 49 if( strcmp(fsI
e36d0 6e 66 6f 2e 66 5f 66 73 74 79 70 65 6e 61 6d 65 nfo.f_fstypename
e36e0 2c 20 61 4d 61 70 5b 69 5d 2e 7a 46 69 6c 65 73 , aMap[i].zFiles
e36f0 79 73 74 65 6d 29 3d 3d 30 20 29 7b 0a 20 20 20 ystem)==0 ){.
e3700 20 20 20 20 20 72 65 74 75 72 6e 20 61 4d 61 70 return aMap
e3710 5b 69 5d 2e 70 4d 65 74 68 6f 64 73 3b 0a 20 20 [i].pMethods;.
e3720 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
e3730 0a 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 63 61 . /* Default ca
e3740 73 65 2e 20 48 61 6e 64 6c 65 73 2c 20 61 6d 6f se. Handles, amo
e3750 6e 67 73 74 20 6f 74 68 65 72 73 2c 20 22 6e 66 ngst others, "nf
e3760 73 22 2e 0a 20 20 2a 2a 20 54 65 73 74 20 62 79 s".. ** Test by
e3770 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b 20 75 73 te-range lock us
e3780 69 6e 67 20 66 63 6e 74 6c 28 29 2e 20 49 66 20 ing fcntl(). If
e3790 74 68 65 20 63 61 6c 6c 20 73 75 63 63 65 65 64 the call succeed
e37a0 73 2c 20 0a 20 20 2a 2a 20 61 73 73 75 6d 65 20 s, . ** assume
e37b0 74 68 61 74 20 74 68 65 20 66 69 6c 65 2d 73 79 that the file-sy
e37c0 73 74 65 6d 20 73 75 70 70 6f 72 74 73 20 50 4f stem supports PO
e37d0 53 49 58 20 73 74 79 6c 65 20 6c 6f 63 6b 73 2e SIX style locks.
e37e0 20 0a 20 20 2a 2f 0a 20 20 6c 6f 63 6b 49 6e 66 . */. lockInf
e37f0 6f 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0a 20 20 6c o.l_len = 1;. l
e3800 6f 63 6b 49 6e 66 6f 2e 6c 5f 73 74 61 72 74 20 ockInfo.l_start
e3810 3d 20 30 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e = 0;. lockInfo.
e3820 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f l_whence = SEEK_
e3830 53 45 54 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e SET;. lockInfo.
e3840 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c 43 4b l_type = F_RDLCK
e3850 3b 0a 20 20 69 66 28 20 66 63 6e 74 6c 28 70 4e ;. if( fcntl(pN
e3860 65 77 2d 3e 68 2c 20 46 5f 47 45 54 4c 4b 2c 20 ew->h, F_GETLK,
e3870 26 6c 6f 63 6b 49 6e 66 6f 29 21 3d 2d 31 20 29 &lockInfo)!=-1 )
e3880 20 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 66 69 6c {. pNew->fil
e3890 65 46 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 5f eFlags = SQLITE_
e38a0 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 WHOLE_FILE_LOCKI
e38b0 4e 47 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 NG;. return &
e38c0 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73 3b 0a posixIoMethods;.
e38d0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 }else{. ret
e38e0 75 72 6e 20 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65 urn &dotlockIoMe
e38f0 74 68 6f 64 73 3b 0a 20 20 7d 0a 7d 0a 73 74 61 thods;. }.}.sta
e3900 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 tic const sqlite
e3910 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 0a 20 20 3_io_methods .
e3920 2a 28 2a 63 6f 6e 73 74 20 61 75 74 6f 6c 6f 63 *(*const autoloc
e3930 6b 49 6f 46 69 6e 64 65 72 29 28 63 6f 6e 73 74 kIoFinder)(const
e3940 20 63 68 61 72 2a 2c 75 6e 69 78 46 69 6c 65 2a char*,unixFile*
e3950 29 20 3d 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 ) = autolockIoFi
e3960 6e 64 65 72 49 6d 70 6c 3b 0a 0a 23 65 6e 64 69 nderImpl;..#endi
e3970 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 5f 5f 41 f /* defined(__A
e3980 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 PPLE__) && SQLIT
e3990 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 E_ENABLE_LOCKING
e39a0 5f 53 54 59 4c 45 20 2a 2f 0a 0a 23 69 66 20 4f _STYLE */..#if O
e39b0 53 5f 56 58 57 4f 52 4b 53 20 26 26 20 53 51 4c S_VXWORKS && SQL
e39c0 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
e39d0 4e 47 5f 53 54 59 4c 45 0a 2f 2a 20 0a 2a 2a 20 NG_STYLE./* .**
e39e0 54 68 69 73 20 22 66 69 6e 64 65 72 22 20 66 75 This "finder" fu
e39f0 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 nction attempts
e3a00 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 to determine the
e3a10 20 62 65 73 74 20 6c 6f 63 6b 69 6e 67 20 73 74 best locking st
e3a20 72 61 74 65 67 79 20 0a 2a 2a 20 66 6f 72 20 74 rategy .** for t
e3a30 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
e3a40 20 22 66 69 6c 65 50 61 74 68 22 2e 20 20 49 74 "filePath". It
e3a50 20 74 68 65 6e 20 72 65 74 75 72 6e 73 20 74 68 then returns th
e3a60 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 e sqlite3_io_met
e3a70 68 6f 64 73 0a 2a 2a 20 6f 62 6a 65 63 74 20 74 hods.** object t
e3a80 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 hat implements t
e3a90 68 61 74 20 73 74 72 61 74 65 67 79 2e 0a 2a 2a hat strategy..**
e3aa0 0a 2a 2a 20 54 68 69 73 20 69 73 20 66 6f 72 20 .** This is for
e3ab0 56 58 57 6f 72 6b 73 20 6f 6e 6c 79 2e 0a 2a 2f VXWorks only..*/
e3ac0 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 .static const sq
e3ad0 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
e3ae0 20 2a 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 *autolockIoFind
e3af0 65 72 49 6d 70 6c 28 0a 20 20 63 6f 6e 73 74 20 erImpl(. const
e3b00 63 68 61 72 20 2a 66 69 6c 65 50 61 74 68 2c 20 char *filePath,
e3b10 20 20 20 2f 2a 20 6e 61 6d 65 20 6f 66 20 74 68 /* name of th
e3b20 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
e3b30 2a 2f 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 */. unixFile *p
e3b40 4e 65 77 20 20 20 20 20 20 20 20 20 20 20 2f 2a New /*
e3b50 20 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 6f the open file o
e3b60 62 6a 65 63 74 20 2a 2f 0a 29 7b 0a 20 20 73 74 bject */.){. st
e3b70 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 49 ruct flock lockI
e3b80 6e 66 6f 3b 0a 0a 20 20 69 66 28 20 21 66 69 6c nfo;.. if( !fil
e3b90 65 50 61 74 68 20 29 7b 0a 20 20 20 20 2f 2a 20 ePath ){. /*
e3ba0 49 66 20 66 69 6c 65 50 61 74 68 3d 3d 4e 55 4c If filePath==NUL
e3bb0 4c 20 74 68 61 74 20 6d 65 61 6e 73 20 77 65 20 L that means we
e3bc0 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 are dealing with
e3bd0 20 61 20 74 72 61 6e 73 69 65 6e 74 20 66 69 6c a transient fil
e3be0 65 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 64 6f e. ** that do
e3bf0 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 es not need to b
e3c00 65 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 20 e locked. */.
e3c10 20 72 65 74 75 72 6e 20 26 6e 6f 6c 6f 63 6b 49 return &nolockI
e3c20 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 0a 0a 20 oMethods;. }..
e3c30 20 2f 2a 20 54 65 73 74 20 69 66 20 66 63 6e 74 /* Test if fcnt
e3c40 6c 28 29 20 69 73 20 73 75 70 70 6f 72 74 65 64 l() is supported
e3c50 20 61 6e 64 20 75 73 65 20 50 4f 53 49 58 20 73 and use POSIX s
e3c60 74 79 6c 65 20 6c 6f 63 6b 73 2e 0a 20 20 2a 2a tyle locks.. **
e3c70 20 4f 74 68 65 72 77 69 73 65 20 66 61 6c 6c 20 Otherwise fall
e3c80 62 61 63 6b 20 74 6f 20 74 68 65 20 6e 61 6d 65 back to the name
e3c90 64 20 73 65 6d 61 70 68 6f 72 65 20 6d 65 74 68 d semaphore meth
e3ca0 6f 64 2e 0a 20 20 2a 2f 0a 20 20 6c 6f 63 6b 49 od.. */. lockI
e3cb0 6e 66 6f 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0a 20 nfo.l_len = 1;.
e3cc0 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 73 74 61 72 lockInfo.l_star
e3cd0 74 20 3d 20 30 3b 0a 20 20 6c 6f 63 6b 49 6e 66 t = 0;. lockInf
e3ce0 6f 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 o.l_whence = SEE
e3cf0 4b 5f 53 45 54 3b 0a 20 20 6c 6f 63 6b 49 6e 66 K_SET;. lockInf
e3d00 6f 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c o.l_type = F_RDL
e3d10 43 4b 3b 0a 20 20 69 66 28 20 66 63 6e 74 6c 28 CK;. if( fcntl(
e3d20 70 4e 65 77 2d 3e 68 2c 20 46 5f 47 45 54 4c 4b pNew->h, F_GETLK
e3d30 2c 20 26 6c 6f 63 6b 49 6e 66 6f 29 21 3d 2d 31 , &lockInfo)!=-1
e3d40 20 29 20 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ) {. return
e3d50 26 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73 3b &posixIoMethods;
e3d60 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 . }else{. re
e3d70 74 75 72 6e 20 26 73 65 6d 49 6f 4d 65 74 68 6f turn &semIoMetho
e3d80 64 73 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 ds;. }.}.static
e3d90 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 const sqlite3_i
e3da0 6f 5f 6d 65 74 68 6f 64 73 20 0a 20 20 2a 28 2a o_methods . *(*
e3db0 63 6f 6e 73 74 20 61 75 74 6f 6c 6f 63 6b 49 6f const autolockIo
e3dc0 46 69 6e 64 65 72 29 28 63 6f 6e 73 74 20 63 68 Finder)(const ch
e3dd0 61 72 2a 2c 75 6e 69 78 46 69 6c 65 2a 29 20 3d ar*,unixFile*) =
e3de0 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 autolockIoFinde
e3df0 72 49 6d 70 6c 3b 0a 0a 23 65 6e 64 69 66 20 2f rImpl;..#endif /
e3e00 2a 20 4f 53 5f 56 58 57 4f 52 4b 53 20 26 26 20 * OS_VXWORKS &&
e3e10 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f SQLITE_ENABLE_LO
e3e20 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 0a CKING_STYLE */..
e3e30 2f 2a 0a 2a 2a 20 41 6e 20 61 62 73 74 72 61 63 /*.** An abstrac
e3e40 74 20 74 79 70 65 20 66 6f 72 20 61 20 70 6f 69 t type for a poi
e3e50 6e 74 65 72 20 74 6f 20 61 20 49 4f 20 6d 65 74 nter to a IO met
e3e60 68 6f 64 20 66 69 6e 64 65 72 20 66 75 6e 63 74 hod finder funct
e3e70 69 6f 6e 3a 0a 2a 2f 0a 74 79 70 65 64 65 66 20 ion:.*/.typedef
e3e80 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f const sqlite3_io
e3e90 5f 6d 65 74 68 6f 64 73 20 2a 28 2a 66 69 6e 64 _methods *(*find
e3ea0 65 72 5f 74 79 70 65 29 28 63 6f 6e 73 74 20 63 er_type)(const c
e3eb0 68 61 72 2a 2c 75 6e 69 78 46 69 6c 65 2a 29 3b har*,unixFile*);
e3ec0 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .../************
e3ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3f10 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
e3f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 ************* sq
e3f30 6c 69 74 65 33 5f 76 66 73 20 6d 65 74 68 6f 64 lite3_vfs method
e3f40 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a s **************
e3f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
e3f60 2a 0a 2a 2a 20 54 68 69 73 20 64 69 76 69 73 69 *.** This divisi
e3f70 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 on contains the
e3f80 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
e3f90 66 20 6d 65 74 68 6f 64 73 20 6f 6e 20 74 68 65 f methods on the
e3fa0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 66 73 20 .** sqlite3_vfs
e3fb0 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a object..*/../*.*
e3fc0 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 * Initialize the
e3fd0 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 contents of the
e3fe0 20 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74 unixFile struct
e3ff0 75 72 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 ure pointed to b
e4000 79 20 70 49 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 y pId..*/.static
e4010 20 69 6e 74 20 66 69 6c 6c 49 6e 55 6e 69 78 46 int fillInUnixF
e4020 69 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 ile(. sqlite3_v
e4030 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 2f fs *pVfs, /
e4040 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 * Pointer to vfs
e4050 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74 object */. int
e4060 20 68 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 h,
e4070 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 66 69 6c /* Open fil
e4080 65 20 64 65 73 63 72 69 70 74 6f 72 20 6f 66 20 e descriptor of
e4090 66 69 6c 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 file being opene
e40a0 64 20 2a 2f 0a 20 20 69 6e 74 20 64 69 72 66 64 d */. int dirfd
e40b0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
e40c0 2a 20 44 69 72 65 63 74 6f 72 79 20 66 69 6c 65 * Directory file
e40d0 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 descriptor */.
e40e0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 sqlite3_file *p
e40f0 49 64 2c 20 20 20 20 20 20 2f 2a 20 57 72 69 74 Id, /* Writ
e4100 65 20 74 6f 20 74 68 65 20 75 6e 69 78 46 69 6c e to the unixFil
e4110 65 20 73 74 72 75 63 74 75 72 65 20 68 65 72 65 e structure here
e4120 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
e4130 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 2f 2a *zFilename, /*
e4140 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c Name of the fil
e4150 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 2a e being opened *
e4160 2f 0a 20 20 69 6e 74 20 6e 6f 4c 6f 63 6b 2c 20 /. int noLock,
e4170 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
e4180 6d 69 74 20 6c 6f 63 6b 69 6e 67 20 69 66 20 74 mit locking if t
e4190 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73 44 rue */. int isD
e41a0 65 6c 65 74 65 20 20 20 20 20 20 20 20 20 20 20 elete
e41b0 20 2f 2a 20 44 65 6c 65 74 65 20 6f 6e 20 63 6c /* Delete on cl
e41c0 6f 73 65 20 69 66 20 74 72 75 65 20 2a 2f 0a 29 ose if true */.)
e41d0 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 {. const sqlite
e41e0 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4c 3_io_methods *pL
e41f0 6f 63 6b 69 6e 67 53 74 79 6c 65 3b 0a 20 20 75 ockingStyle;. u
e4200 6e 69 78 46 69 6c 65 20 2a 70 4e 65 77 20 3d 20 nixFile *pNew =
e4210 28 75 6e 69 78 46 69 6c 65 20 2a 29 70 49 64 3b (unixFile *)pId;
e4220 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
e4230 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74 TE_OK;.. assert
e4240 28 20 70 4e 65 77 2d 3e 70 4c 6f 63 6b 3d 3d 4e ( pNew->pLock==N
e4250 55 4c 4c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ULL );. assert(
e4260 20 70 4e 65 77 2d 3e 70 4f 70 65 6e 3d 3d 4e 55 pNew->pOpen==NU
e4270 4c 4c 20 29 3b 0a 0a 20 20 2f 2a 20 50 61 72 61 LL );.. /* Para
e4280 6d 65 74 65 72 20 69 73 44 65 6c 65 74 65 20 69 meter isDelete i
e4290 73 20 6f 6e 6c 79 20 75 73 65 64 20 6f 6e 20 76 s only used on v
e42a0 78 77 6f 72 6b 73 2e 20 45 78 70 72 65 73 73 20 xworks. Express
e42b0 74 68 69 73 20 65 78 70 6c 69 63 69 74 6c 79 20 this explicitly
e42c0 0a 20 20 2a 2a 20 68 65 72 65 20 74 6f 20 70 72 . ** here to pr
e42d0 65 76 65 6e 74 20 63 6f 6d 70 69 6c 65 72 20 77 event compiler w
e42e0 61 72 6e 69 6e 67 73 20 61 62 6f 75 74 20 75 6e arnings about un
e42f0 75 73 65 64 20 70 61 72 61 6d 65 74 65 72 73 2e used parameters.
e4300 0a 20 20 2a 2f 0a 20 20 55 4e 55 53 45 44 5f 50 . */. UNUSED_P
e4310 41 52 41 4d 45 54 45 52 28 69 73 44 65 6c 65 74 ARAMETER(isDelet
e4320 65 29 3b 0a 0a 20 20 4f 53 54 52 41 43 45 33 28 e);.. OSTRACE3(
e4330 22 4f 50 45 4e 20 20 20 20 25 2d 33 64 20 25 73 "OPEN %-3d %s
e4340 5c 6e 22 2c 20 68 2c 20 7a 46 69 6c 65 6e 61 6d \n", h, zFilenam
e4350 65 29 3b 20 20 20 20 0a 20 20 70 4e 65 77 2d 3e e); . pNew->
e4360 68 20 3d 20 68 3b 0a 20 20 70 4e 65 77 2d 3e 64 h = h;. pNew->d
e4370 69 72 66 64 20 3d 20 64 69 72 66 64 3b 0a 20 20 irfd = dirfd;.
e4380 53 45 54 5f 54 48 52 45 41 44 49 44 28 70 4e 65 SET_THREADID(pNe
e4390 77 29 3b 0a 20 20 70 4e 65 77 2d 3e 66 69 6c 65 w);. pNew->file
e43a0 46 6c 61 67 73 20 3d 20 30 3b 0a 0a 23 69 66 20 Flags = 0;..#if
e43b0 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 70 4e 65 OS_VXWORKS. pNe
e43c0 77 2d 3e 70 49 64 20 3d 20 76 78 77 6f 72 6b 73 w->pId = vxworks
e43d0 46 69 6e 64 46 69 6c 65 49 64 28 7a 46 69 6c 65 FindFileId(zFile
e43e0 6e 61 6d 65 29 3b 0a 20 20 69 66 28 20 70 4e 65 name);. if( pNe
e43f0 77 2d 3e 70 49 64 3d 3d 30 20 29 7b 0a 20 20 20 w->pId==0 ){.
e4400 20 6e 6f 4c 6f 63 6b 20 3d 20 31 3b 0a 20 20 20 noLock = 1;.
e4410 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d rc = SQLITE_NOM
e4420 45 4d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a EM;. }.#endif..
e4430 20 20 69 66 28 20 6e 6f 4c 6f 63 6b 20 29 7b 0a if( noLock ){.
e4440 20 20 20 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c pLockingStyl
e4450 65 20 3d 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 e = &nolockIoMet
e4460 68 6f 64 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 hods;. }else{.
e4470 20 20 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 pLockingStyle
e4480 20 3d 20 28 2a 2a 28 66 69 6e 64 65 72 5f 74 79 = (**(finder_ty
e4490 70 65 2a 29 70 56 66 73 2d 3e 70 41 70 70 44 61 pe*)pVfs->pAppDa
e44a0 74 61 29 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 70 ta)(zFilename, p
e44b0 4e 65 77 29 3b 0a 23 69 66 20 53 51 4c 49 54 45 New);.#if SQLITE
e44c0 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f _ENABLE_LOCKING_
e44d0 53 54 59 4c 45 0a 20 20 20 20 2f 2a 20 43 61 63 STYLE. /* Cac
e44e0 68 65 20 7a 46 69 6c 65 6e 61 6d 65 20 69 6e 20 he zFilename in
e44f0 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 the locking cont
e4500 65 78 74 20 28 41 46 50 20 61 6e 64 20 64 6f 74 ext (AFP and dot
e4510 6c 6f 63 6b 20 6f 76 65 72 72 69 64 65 29 20 66 lock override) f
e4520 6f 72 0a 20 20 20 20 2a 2a 20 70 72 6f 78 79 4c or. ** proxyL
e4530 6f 63 6b 20 61 63 74 69 76 61 74 69 6f 6e 20 69 ock activation i
e4540 73 20 70 6f 73 73 69 62 6c 65 20 28 72 65 6d 6f s possible (remo
e4550 74 65 20 70 72 6f 78 79 20 69 73 20 62 61 73 65 te proxy is base
e4560 64 20 6f 6e 20 64 62 20 6e 61 6d 65 29 0a 20 20 d on db name).
e4570 20 20 2a 2a 20 7a 46 69 6c 65 6e 61 6d 65 20 72 ** zFilename r
e4580 65 6d 61 69 6e 73 20 76 61 6c 69 64 20 75 6e 74 emains valid unt
e4590 69 6c 20 66 69 6c 65 20 69 73 20 63 6c 6f 73 65 il file is close
e45a0 64 2c 20 74 6f 20 73 75 70 70 6f 72 74 20 2a 2f d, to support */
e45b0 0a 20 20 20 20 70 4e 65 77 2d 3e 6c 6f 63 6b 69 . pNew->locki
e45c0 6e 67 43 6f 6e 74 65 78 74 20 3d 20 28 76 6f 69 ngContext = (voi
e45d0 64 2a 29 7a 46 69 6c 65 6e 61 6d 65 3b 0a 23 65 d*)zFilename;.#e
e45e0 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 69 66 28 20 ndif. }.. if(
e45f0 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d 3d pLockingStyle ==
e4600 20 26 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73 &posixIoMethods
e4610 20 29 7b 0a 20 20 20 20 75 6e 69 78 45 6e 74 65 ){. unixEnte
e4620 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 72 63 rMutex();. rc
e4630 20 3d 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 = findLockInfo(
e4640 70 4e 65 77 2c 20 26 70 4e 65 77 2d 3e 70 4c 6f pNew, &pNew->pLo
e4650 63 6b 2c 20 26 70 4e 65 77 2d 3e 70 4f 70 65 6e ck, &pNew->pOpen
e4660 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
e4670 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
e4680 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 /* If an error
e4690 20 6f 63 63 75 72 65 64 20 69 6e 20 66 69 6e 64 occured in find
e46a0 4c 6f 63 6b 49 6e 66 6f 28 29 2c 20 63 6c 6f 73 LockInfo(), clos
e46b0 65 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 e the file descr
e46c0 69 70 74 6f 72 0a 20 20 20 20 20 20 2a 2a 20 69 iptor. ** i
e46d0 6d 6d 65 64 69 61 74 65 6c 79 2c 20 62 65 66 6f mmediately, befo
e46e0 72 65 20 72 65 6c 65 61 73 69 6e 67 20 74 68 65 re releasing the
e46f0 20 6d 75 74 65 78 2e 20 66 69 6e 64 4c 6f 63 6b mutex. findLock
e4700 49 6e 66 6f 28 29 20 6d 61 79 20 66 61 69 6c 0a Info() may fail.
e4710 20 20 20 20 20 20 2a 2a 20 69 6e 20 74 77 6f 20 ** in two
e4720 73 63 65 6e 61 72 69 6f 73 3a 0a 20 20 20 20 20 scenarios:.
e4730 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 28 **. ** (
e4740 61 29 20 41 20 63 61 6c 6c 20 74 6f 20 66 73 74 a) A call to fst
e4750 61 74 28 29 20 66 61 69 6c 65 64 2e 0a 20 20 20 at() failed..
e4760 20 20 20 2a 2a 20 20 20 28 62 29 20 41 20 6d 61 ** (b) A ma
e4770 6c 6c 6f 63 20 66 61 69 6c 65 64 2e 0a 20 20 20 lloc failed..
e4780 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 53 **. ** S
e4790 63 65 6e 61 72 69 6f 20 28 62 29 20 6d 61 79 20 cenario (b) may
e47a0 6f 6e 6c 79 20 6f 63 63 75 72 20 69 66 20 74 68 only occur if th
e47b0 65 20 70 72 6f 63 65 73 73 20 69 73 20 68 6f 6c e process is hol
e47c0 64 69 6e 67 20 6e 6f 20 6f 74 68 65 72 0a 20 20 ding no other.
e47d0 20 20 20 20 2a 2a 20 66 69 6c 65 20 64 65 73 63 ** file desc
e47e0 72 69 70 74 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 riptors open on
e47f0 74 68 65 20 73 61 6d 65 20 66 69 6c 65 2e 20 49 the same file. I
e4800 66 20 74 68 65 72 65 20 77 65 72 65 20 6f 74 68 f there were oth
e4810 65 72 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a er file. **
e4820 20 64 65 73 63 72 69 70 74 6f 72 73 20 6f 6e 20 descriptors on
e4830 74 68 69 73 20 66 69 6c 65 2c 20 74 68 65 6e 20 this file, then
e4840 6e 6f 20 6d 61 6c 6c 6f 63 20 77 6f 75 6c 64 20 no malloc would
e4850 62 65 20 72 65 71 75 69 72 65 64 20 62 79 0a 20 be required by.
e4860 20 20 20 20 20 2a 2a 20 66 69 6e 64 4c 6f 63 6b ** findLock
e4870 49 6e 66 6f 28 29 2e 20 49 66 20 74 68 69 73 20 Info(). If this
e4880 69 73 20 74 68 65 20 63 61 73 65 2c 20 69 74 20 is the case, it
e4890 69 73 20 71 75 69 74 65 20 73 61 66 65 20 74 6f is quite safe to
e48a0 20 63 6c 6f 73 65 0a 20 20 20 20 20 20 2a 2a 20 close. **
e48b0 68 61 6e 64 6c 65 20 68 20 2d 20 61 73 20 69 74 handle h - as it
e48c0 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 is guaranteed t
e48d0 68 61 74 20 6e 6f 20 70 6f 73 69 78 20 6c 6f 63 hat no posix loc
e48e0 6b 73 20 77 69 6c 6c 20 62 65 20 72 65 6c 65 61 ks will be relea
e48f0 73 65 64 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 sed. ** by
e4900 64 6f 69 6e 67 20 73 6f 2e 0a 20 20 20 20 20 20 doing so..
e4910 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 49 66 20 73 **. ** If s
e4920 63 65 6e 61 72 69 6f 20 28 61 29 20 63 61 75 73 cenario (a) caus
e4930 65 64 20 74 68 65 20 65 72 72 6f 72 20 74 68 65 ed the error the
e4940 6e 20 74 68 69 6e 67 73 20 61 72 65 20 6e 6f 74 n things are not
e4950 20 73 6f 20 73 61 66 65 2e 20 54 68 65 0a 20 20 so safe. The.
e4960 20 20 20 20 2a 2a 20 69 6d 70 6c 69 63 69 74 20 ** implicit
e4970 61 73 73 75 6d 70 74 69 6f 6e 20 68 65 72 65 20 assumption here
e4980 69 73 20 74 68 61 74 20 69 66 20 66 73 74 61 74 is that if fstat
e4990 28 29 20 66 61 69 6c 73 2c 20 74 68 69 6e 67 73 () fails, things
e49a0 20 61 72 65 20 69 6e 0a 20 20 20 20 20 20 2a 2a are in. **
e49b0 20 73 75 63 68 20 62 61 64 20 73 68 61 70 65 20 such bad shape
e49c0 74 68 61 74 20 64 72 6f 70 70 69 6e 67 20 61 20 that dropping a
e49d0 6c 6f 63 6b 20 6f 72 20 74 77 6f 20 64 6f 65 73 lock or two does
e49e0 6e 27 74 20 6d 61 74 74 65 72 20 6d 75 63 68 2e n't matter much.
e49f0 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
e4a00 63 6c 6f 73 65 28 68 29 3b 0a 20 20 20 20 20 20 close(h);.
e4a10 68 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 h = -1;. }.
e4a20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 unixLeaveMutex
e4a30 28 29 3b 0a 20 20 7d 0a 0a 23 69 66 20 53 51 4c ();. }..#if SQL
e4a40 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
e4a50 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69 NG_STYLE && defi
e4a60 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 ned(__APPLE__).
e4a70 20 65 6c 73 65 20 69 66 28 20 70 4c 6f 63 6b 69 else if( pLocki
e4a80 6e 67 53 74 79 6c 65 20 3d 3d 20 26 61 66 70 49 ngStyle == &afpI
e4a90 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 oMethods ){.
e4aa0 2f 2a 20 41 46 50 20 6c 6f 63 6b 69 6e 67 20 75 /* AFP locking u
e4ab0 73 65 73 20 74 68 65 20 66 69 6c 65 20 70 61 74 ses the file pat
e4ac0 68 20 73 6f 20 69 74 20 6e 65 65 64 73 20 74 6f h so it needs to
e4ad0 20 62 65 20 69 6e 63 6c 75 64 65 64 20 69 6e 0a be included in.
e4ae0 20 20 20 20 2a 2a 20 74 68 65 20 61 66 70 4c 6f ** the afpLo
e4af0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2e 0a 20 20 ckingContext..
e4b00 20 20 2a 2f 0a 20 20 20 20 61 66 70 4c 6f 63 6b */. afpLock
e4b10 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 ingContext *pCtx
e4b20 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 6c 6f 63 6b ;. pNew->lock
e4b30 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70 43 74 ingContext = pCt
e4b40 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c x = sqlite3_mall
e4b50 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 43 74 78 oc( sizeof(*pCtx
e4b60 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 43 74 ) );. if( pCt
e4b70 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 x==0 ){. rc
e4b80 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b = SQLITE_NOMEM;
e4b90 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
e4ba0 20 20 2f 2a 20 4e 42 3a 20 7a 46 69 6c 65 6e 61 /* NB: zFilena
e4bb0 6d 65 20 65 78 69 73 74 73 20 61 6e 64 20 72 65 me exists and re
e4bc0 6d 61 69 6e 73 20 76 61 6c 69 64 20 75 6e 74 69 mains valid unti
e4bd0 6c 20 74 68 65 20 66 69 6c 65 20 69 73 20 63 6c l the file is cl
e4be0 6f 73 65 64 0a 20 20 20 20 20 20 2a 2a 20 61 63 osed. ** ac
e4bf0 63 6f 72 64 69 6e 67 20 74 6f 20 72 65 71 75 69 cording to requi
e4c00 72 65 6d 65 6e 74 20 46 31 31 31 34 31 2e 20 20 rement F11141.
e4c10 53 6f 20 77 65 20 64 6f 20 6e 6f 74 20 6e 65 65 So we do not nee
e4c20 64 20 74 6f 20 6d 61 6b 65 20 61 0a 20 20 20 20 d to make a.
e4c30 20 20 2a 2a 20 63 6f 70 79 20 6f 66 20 74 68 65 ** copy of the
e4c40 20 66 69 6c 65 6e 61 6d 65 2e 20 2a 2f 0a 20 20 filename. */.
e4c50 20 20 20 20 70 43 74 78 2d 3e 64 62 50 61 74 68 pCtx->dbPath
e4c60 20 3d 20 7a 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 = zFilename;.
e4c70 20 20 20 20 73 72 61 6e 64 6f 6d 64 65 76 28 29 srandomdev()
e4c80 3b 0a 20 20 20 20 20 20 75 6e 69 78 45 6e 74 65 ;. unixEnte
e4c90 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 20 20 rMutex();.
e4ca0 72 63 20 3d 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 rc = findLockInf
e4cb0 6f 28 70 4e 65 77 2c 20 4e 55 4c 4c 2c 20 26 70 o(pNew, NULL, &p
e4cc0 4e 65 77 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 New->pOpen);.
e4cd0 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 unixLeaveMute
e4ce0 78 28 29 3b 20 20 20 20 20 20 20 20 0a 20 20 20 x(); .
e4cf0 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 }. }.#endif..
e4d00 20 65 6c 73 65 20 69 66 28 20 70 4c 6f 63 6b 69 else if( pLocki
e4d10 6e 67 53 74 79 6c 65 20 3d 3d 20 26 64 6f 74 6c ngStyle == &dotl
e4d20 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a ockIoMethods ){.
e4d30 20 20 20 20 2f 2a 20 44 6f 74 66 69 6c 65 20 6c /* Dotfile l
e4d40 6f 63 6b 69 6e 67 20 75 73 65 73 20 74 68 65 20 ocking uses the
e4d50 66 69 6c 65 20 70 61 74 68 20 73 6f 20 69 74 20 file path so it
e4d60 6e 65 65 64 73 20 74 6f 20 62 65 20 69 6e 63 6c needs to be incl
e4d70 75 64 65 64 20 69 6e 0a 20 20 20 20 2a 2a 20 74 uded in. ** t
e4d80 68 65 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 69 6e he dotlockLockin
e4d90 67 43 6f 6e 74 65 78 74 20 0a 20 20 20 20 2a 2f gContext . */
e4da0 0a 20 20 20 20 63 68 61 72 20 2a 7a 4c 6f 63 6b . char *zLock
e4db0 46 69 6c 65 3b 0a 20 20 20 20 69 6e 74 20 6e 46 File;. int nF
e4dc0 69 6c 65 6e 61 6d 65 3b 0a 20 20 20 20 6e 46 69 ilename;. nFi
e4dd0 6c 65 6e 61 6d 65 20 3d 20 28 69 6e 74 29 73 74 lename = (int)st
e4de0 72 6c 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 29 20 rlen(zFilename)
e4df0 2b 20 36 3b 0a 20 20 20 20 7a 4c 6f 63 6b 46 69 + 6;. zLockFi
e4e00 6c 65 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c le = (char *)sql
e4e10 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 46 69 6c ite3_malloc(nFil
e4e20 65 6e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 ename);. if(
e4e30 7a 4c 6f 63 6b 46 69 6c 65 3d 3d 30 20 29 7b 0a zLockFile==0 ){.
e4e40 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
e4e50 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c E_NOMEM;. }el
e4e60 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 se{. sqlite
e4e70 33 5f 73 6e 70 72 69 6e 74 66 28 6e 46 69 6c 65 3_snprintf(nFile
e4e80 6e 61 6d 65 2c 20 7a 4c 6f 63 6b 46 69 6c 65 2c name, zLockFile,
e4e90 20 22 25 73 22 20 44 4f 54 4c 4f 43 4b 5f 53 55 "%s" DOTLOCK_SU
e4ea0 46 46 49 58 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 FFIX, zFilename)
e4eb0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65 77 ;. }. pNew
e4ec0 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 ->lockingContext
e4ed0 20 3d 20 7a 4c 6f 63 6b 46 69 6c 65 3b 0a 20 20 = zLockFile;.
e4ee0 7d 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b }..#if OS_VXWORK
e4ef0 53 0a 20 20 65 6c 73 65 20 69 66 28 20 70 4c 6f S. else if( pLo
e4f00 63 6b 69 6e 67 53 74 79 6c 65 20 3d 3d 20 26 73 ckingStyle == &s
e4f10 65 6d 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 emIoMethods ){.
e4f20 20 20 20 2f 2a 20 4e 61 6d 65 64 20 73 65 6d 61 /* Named sema
e4f30 70 68 6f 72 65 20 6c 6f 63 6b 69 6e 67 20 75 73 phore locking us
e4f40 65 73 20 74 68 65 20 66 69 6c 65 20 70 61 74 68 es the file path
e4f50 20 73 6f 20 69 74 20 6e 65 65 64 73 20 74 6f 20 so it needs to
e4f60 62 65 0a 20 20 20 20 2a 2a 20 69 6e 63 6c 75 64 be. ** includ
e4f70 65 64 20 69 6e 20 74 68 65 20 73 65 6d 4c 6f 63 ed in the semLoc
e4f80 6b 69 6e 67 43 6f 6e 74 65 78 74 0a 20 20 20 20 kingContext.
e4f90 2a 2f 0a 20 20 20 20 75 6e 69 78 45 6e 74 65 72 */. unixEnter
e4fa0 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 72 63 20 Mutex();. rc
e4fb0 3d 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70 = findLockInfo(p
e4fc0 4e 65 77 2c 20 26 70 4e 65 77 2d 3e 70 4c 6f 63 New, &pNew->pLoc
e4fd0 6b 2c 20 26 70 4e 65 77 2d 3e 70 4f 70 65 6e 29 k, &pNew->pOpen)
e4fe0 3b 0a 20 20 20 20 69 66 28 20 28 72 63 3d 3d 53 ;. if( (rc==S
e4ff0 51 4c 49 54 45 5f 4f 4b 29 20 26 26 20 28 70 4e QLITE_OK) && (pN
e5000 65 77 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 3d ew->pOpen->pSem=
e5010 3d 4e 55 4c 4c 29 20 29 7b 0a 20 20 20 20 20 20 =NULL) ){.
e5020 63 68 61 72 20 2a 7a 53 65 6d 4e 61 6d 65 20 3d char *zSemName =
e5030 20 70 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e 61 53 pNew->pOpen->aS
e5040 65 6d 4e 61 6d 65 3b 0a 20 20 20 20 20 20 69 6e emName;. in
e5050 74 20 6e 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 t n;. sqlit
e5060 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f e3_snprintf(MAX_
e5070 50 41 54 48 4e 41 4d 45 2c 20 7a 53 65 6d 4e 61 PATHNAME, zSemNa
e5080 6d 65 2c 20 22 2f 25 73 2e 73 65 6d 22 2c 0a 20 me, "/%s.sem",.
e5090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e50a0 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 49 64 2d pNew->pId-
e50b0 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 29 >zCanonicalName)
e50c0 3b 0a 20 20 20 20 20 20 66 6f 72 28 20 6e 3d 31 ;. for( n=1
e50d0 3b 20 7a 53 65 6d 4e 61 6d 65 5b 6e 5d 3b 20 6e ; zSemName[n]; n
e50e0 2b 2b 20 29 0a 20 20 20 20 20 20 20 20 69 66 28 ++ ). if(
e50f0 20 7a 53 65 6d 4e 61 6d 65 5b 6e 5d 3d 3d 27 2f zSemName[n]=='/
e5100 27 20 29 20 7a 53 65 6d 4e 61 6d 65 5b 6e 5d 20 ' ) zSemName[n]
e5110 3d 20 27 5f 27 3b 0a 20 20 20 20 20 20 70 4e 65 = '_';. pNe
e5120 77 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 20 3d w->pOpen->pSem =
e5130 20 73 65 6d 5f 6f 70 65 6e 28 7a 53 65 6d 4e 61 sem_open(zSemNa
e5140 6d 65 2c 20 4f 5f 43 52 45 41 54 2c 20 30 36 36 me, O_CREAT, 066
e5150 36 2c 20 31 29 3b 0a 20 20 20 20 20 20 69 66 28 6, 1);. if(
e5160 20 70 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e 70 53 pNew->pOpen->pS
e5170 65 6d 20 3d 3d 20 53 45 4d 5f 46 41 49 4c 45 44 em == SEM_FAILED
e5180 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
e5190 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
e51a0 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4f 70 pNew->pOp
e51b0 65 6e 2d 3e 61 53 65 6d 4e 61 6d 65 5b 30 5d 20 en->aSemName[0]
e51c0 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 7d 0a = '\0';. }.
e51d0 20 20 20 20 7d 0a 20 20 20 20 75 6e 69 78 4c 65 }. unixLe
e51e0 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a aveMutex();. }.
e51f0 23 65 6e 64 69 66 0a 20 20 0a 20 20 70 4e 65 77 #endif. . pNew
e5200 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b ->lastErrno = 0;
e5210 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a .#if OS_VXWORKS.
e5220 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
e5230 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 6e 6c 69 6e _OK ){. unlin
e5240 6b 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 k(zFilename);.
e5250 20 20 69 73 44 65 6c 65 74 65 20 3d 20 30 3b 0a isDelete = 0;.
e5260 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 69 73 44 65 }. pNew->isDe
e5270 6c 65 74 65 20 3d 20 69 73 44 65 6c 65 74 65 3b lete = isDelete;
e5280 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 72 63 .#endif. if( rc
e5290 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
e52a0 20 20 20 69 66 28 20 64 69 72 66 64 3e 3d 30 20 if( dirfd>=0
e52b0 29 20 63 6c 6f 73 65 28 64 69 72 66 64 29 3b 20 ) close(dirfd);
e52c0 2f 2a 20 73 69 6c 65 6e 74 20 6c 65 61 6b 20 69 /* silent leak i
e52d0 66 20 66 61 69 6c 2c 20 61 6c 72 65 61 64 79 20 f fail, already
e52e0 69 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 in error */.
e52f0 69 66 28 20 68 3e 3d 30 20 29 20 63 6c 6f 73 65 if( h>=0 ) close
e5300 28 68 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 (h);. }else{.
e5310 20 20 70 4e 65 77 2d 3e 70 4d 65 74 68 6f 64 20 pNew->pMethod
e5320 3d 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 3b = pLockingStyle;
e5330 0a 20 20 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 . OpenCounter
e5340 28 2b 31 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 (+1);. }. retu
e5350 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
e5360 4f 70 65 6e 20 61 20 66 69 6c 65 20 64 65 73 63 Open a file desc
e5370 72 69 70 74 6f 72 20 74 6f 20 74 68 65 20 64 69 riptor to the di
e5380 72 65 63 74 6f 72 79 20 63 6f 6e 74 61 69 6e 69 rectory containi
e5390 6e 67 20 66 69 6c 65 20 7a 46 69 6c 65 6e 61 6d ng file zFilenam
e53a0 65 2e 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 e..** If success
e53b0 66 75 6c 2c 20 2a 70 46 64 20 69 73 20 73 65 74 ful, *pFd is set
e53c0 20 74 6f 20 74 68 65 20 6f 70 65 6e 65 64 20 66 to the opened f
e53d0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 61 ile descriptor a
e53e0 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 nd.** SQLITE_OK
e53f0 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 is returned. If
e5400 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c an error occurs,
e5410 20 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f 4e either SQLITE_N
e5420 4f 4d 45 4d 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 OMEM.** or SQLIT
e5430 45 5f 43 41 4e 54 4f 50 45 4e 20 69 73 20 72 65 E_CANTOPEN is re
e5440 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 46 64 20 turned and *pFd
e5450 69 73 20 73 65 74 20 74 6f 20 61 6e 20 75 6e 64 is set to an und
e5460 65 66 69 6e 65 64 0a 2a 2a 20 76 61 6c 75 65 2e efined.** value.
e5470 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 .**.** If SQLITE
e5480 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2c _OK is returned,
e5490 20 74 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72 the caller is r
e54a0 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 63 esponsible for c
e54b0 6c 6f 73 69 6e 67 0a 2a 2a 20 74 68 65 20 66 69 losing.** the fi
e54c0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a 70 le descriptor *p
e54d0 46 64 20 75 73 69 6e 67 20 63 6c 6f 73 65 28 29 Fd using close()
e54e0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
e54f0 6f 70 65 6e 44 69 72 65 63 74 6f 72 79 28 63 6f openDirectory(co
e5500 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e nst char *zFilen
e5510 61 6d 65 2c 20 69 6e 74 20 2a 70 46 64 29 7b 0a ame, int *pFd){.
e5520 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 int ii;. int
e5530 66 64 20 3d 20 2d 31 3b 0a 20 20 63 68 61 72 20 fd = -1;. char
e5540 7a 44 69 72 6e 61 6d 65 5b 4d 41 58 5f 50 41 54 zDirname[MAX_PAT
e5550 48 4e 41 4d 45 2b 31 5d 3b 0a 0a 20 20 73 71 6c HNAME+1];.. sql
e5560 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41 ite3_snprintf(MA
e5570 58 5f 50 41 54 48 4e 41 4d 45 2c 20 7a 44 69 72 X_PATHNAME, zDir
e5580 6e 61 6d 65 2c 20 22 25 73 22 2c 20 7a 46 69 6c name, "%s", zFil
e5590 65 6e 61 6d 65 29 3b 0a 20 20 66 6f 72 28 69 69 ename);. for(ii
e55a0 3d 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 44 69 =(int)strlen(zDi
e55b0 72 6e 61 6d 65 29 3b 20 69 69 3e 31 20 26 26 20 rname); ii>1 &&
e55c0 7a 44 69 72 6e 61 6d 65 5b 69 69 5d 21 3d 27 2f zDirname[ii]!='/
e55d0 27 3b 20 69 69 2d 2d 29 3b 0a 20 20 69 66 28 20 '; ii--);. if(
e55e0 69 69 3e 30 20 29 7b 0a 20 20 20 20 7a 44 69 72 ii>0 ){. zDir
e55f0 6e 61 6d 65 5b 69 69 5d 20 3d 20 27 5c 30 27 3b name[ii] = '\0';
e5600 0a 20 20 20 20 66 64 20 3d 20 6f 70 65 6e 28 7a . fd = open(z
e5610 44 69 72 6e 61 6d 65 2c 20 4f 5f 52 44 4f 4e 4c Dirname, O_RDONL
e5620 59 7c 4f 5f 42 49 4e 41 52 59 2c 20 30 29 3b 0a Y|O_BINARY, 0);.
e5630 20 20 20 20 69 66 28 20 66 64 3e 3d 30 20 29 7b if( fd>=0 ){
e5640 0a 23 69 66 64 65 66 20 46 44 5f 43 4c 4f 45 58 .#ifdef FD_CLOEX
e5650 45 43 0a 20 20 20 20 20 20 66 63 6e 74 6c 28 66 EC. fcntl(f
e5660 64 2c 20 46 5f 53 45 54 46 44 2c 20 66 63 6e 74 d, F_SETFD, fcnt
e5670 6c 28 66 64 2c 20 46 5f 47 45 54 46 44 2c 20 30 l(fd, F_GETFD, 0
e5680 29 20 7c 20 46 44 5f 43 4c 4f 45 58 45 43 29 3b ) | FD_CLOEXEC);
e5690 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 4f 53 .#endif. OS
e56a0 54 52 41 43 45 33 28 22 4f 50 45 4e 44 49 52 20 TRACE3("OPENDIR
e56b0 25 2d 33 64 20 25 73 5c 6e 22 2c 20 66 64 2c 20 %-3d %s\n", fd,
e56c0 7a 44 69 72 6e 61 6d 65 29 3b 0a 20 20 20 20 7d zDirname);. }
e56d0 0a 20 20 7d 0a 20 20 2a 70 46 64 20 3d 20 66 64 . }. *pFd = fd
e56e0 3b 0a 20 20 72 65 74 75 72 6e 20 28 66 64 3e 3d ;. return (fd>=
e56f0 30 3f 53 51 4c 49 54 45 5f 4f 4b 3a 53 51 4c 49 0?SQLITE_OK:SQLI
e5700 54 45 5f 43 41 4e 54 4f 50 45 4e 29 3b 0a 7d 0a TE_CANTOPEN);.}.
e5710 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 ./*.** Create a
e5720 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e temporary file n
e5730 61 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20 7a 42 ame in zBuf. zB
e5740 75 66 20 6d 75 73 74 20 62 65 20 61 6c 6c 6f 63 uf must be alloc
e5750 61 74 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 63 ated.** by the c
e5760 61 6c 6c 69 6e 67 20 70 72 6f 63 65 73 73 20 61 alling process a
e5770 6e 64 20 6d 75 73 74 20 62 65 20 62 69 67 20 65 nd must be big e
e5780 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 61 74 nough to hold at
e5790 20 6c 65 61 73 74 0a 2a 2a 20 70 56 66 73 2d 3e least.** pVfs->
e57a0 6d 78 50 61 74 68 6e 61 6d 65 20 62 79 74 65 73 mxPathname bytes
e57b0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
e57c0 67 65 74 54 65 6d 70 6e 61 6d 65 28 69 6e 74 20 getTempname(int
e57d0 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 nBuf, char *zBuf
e57e0 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 ){. static cons
e57f0 74 20 63 68 61 72 20 2a 61 7a 44 69 72 73 5b 5d t char *azDirs[]
e5800 20 3d 20 7b 0a 20 20 20 20 20 30 2c 0a 20 20 20 = {. 0,.
e5810 20 20 30 2c 0a 20 20 20 20 20 22 2f 76 61 72 2f 0,. "/var/
e5820 74 6d 70 22 2c 0a 20 20 20 20 20 22 2f 75 73 72 tmp",. "/usr
e5830 2f 74 6d 70 22 2c 0a 20 20 20 20 20 22 2f 74 6d /tmp",. "/tm
e5840 70 22 2c 0a 20 20 20 20 20 22 2e 22 2c 0a 20 20 p",. ".",.
e5850 7d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 };. static cons
e5860 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 t unsigned char
e5870 7a 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22 zChars[] =. "
e5880 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 abcdefghijklmnop
e5890 71 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20 20 qrstuvwxyz".
e58a0 22 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f "ABCDEFGHIJKLMNO
e58b0 50 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20 20 PQRSTUVWXYZ".
e58c0 20 22 30 31 32 33 34 35 36 37 38 39 22 3b 0a 20 "0123456789";.
e58d0 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 2c unsigned int i,
e58e0 20 6a 3b 0a 20 20 73 74 72 75 63 74 20 73 74 61 j;. struct sta
e58f0 74 20 62 75 66 3b 0a 20 20 63 6f 6e 73 74 20 63 t buf;. const c
e5900 68 61 72 20 2a 7a 44 69 72 20 3d 20 22 2e 22 3b har *zDir = ".";
e5910 0a 0a 20 20 2f 2a 20 49 74 27 73 20 6f 64 64 20 .. /* It's odd
e5920 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 6e 20 69 to simulate an i
e5930 6f 2d 65 72 72 6f 72 20 68 65 72 65 2c 20 62 75 o-error here, bu
e5940 74 20 72 65 61 6c 6c 79 20 74 68 69 73 20 69 73 t really this is
e5950 20 6a 75 73 74 0a 20 20 2a 2a 20 75 73 69 6e 67 just. ** using
e5960 20 74 68 65 20 69 6f 2d 65 72 72 6f 72 20 69 6e the io-error in
e5970 66 72 61 73 74 72 75 63 74 75 72 65 20 74 6f 20 frastructure to
e5980 74 65 73 74 20 74 68 61 74 20 53 51 4c 69 74 65 test that SQLite
e5990 20 68 61 6e 64 6c 65 73 20 74 68 69 73 0a 20 20 handles this.
e59a0 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 66 61 69 6c ** function fail
e59b0 69 6e 67 2e 20 0a 20 20 2a 2f 0a 20 20 53 69 6d ing. . */. Sim
e59c0 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 ulateIOError( re
e59d0 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 turn SQLITE_IOER
e59e0 52 20 29 3b 0a 0a 20 20 61 7a 44 69 72 73 5b 30 R );.. azDirs[0
e59f0 5d 20 3d 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 ] = sqlite3_temp
e5a00 5f 64 69 72 65 63 74 6f 72 79 3b 0a 20 20 69 66 _directory;. if
e5a10 20 28 4e 55 4c 4c 20 3d 3d 20 61 7a 44 69 72 73 (NULL == azDirs
e5a20 5b 31 5d 29 20 7b 0a 20 20 20 20 61 7a 44 69 72 [1]) {. azDir
e5a30 73 5b 31 5d 20 3d 20 67 65 74 65 6e 76 28 22 54 s[1] = getenv("T
e5a40 4d 50 44 49 52 22 29 3b 0a 20 20 7d 0a 20 20 0a MPDIR");. }. .
e5a50 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a for(i=0; i<siz
e5a60 65 6f 66 28 61 7a 44 69 72 73 29 2f 73 69 7a 65 eof(azDirs)/size
e5a70 6f 66 28 61 7a 44 69 72 73 5b 30 5d 29 3b 20 69 of(azDirs[0]); i
e5a80 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 7a 44 ++){. if( azD
e5a90 69 72 73 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 irs[i]==0 ) cont
e5aa0 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 73 74 inue;. if( st
e5ab0 61 74 28 61 7a 44 69 72 73 5b 69 5d 2c 20 26 62 at(azDirs[i], &b
e5ac0 75 66 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a uf) ) continue;.
e5ad0 20 20 20 20 69 66 28 20 21 53 5f 49 53 44 49 52 if( !S_ISDIR
e5ae0 28 62 75 66 2e 73 74 5f 6d 6f 64 65 29 20 29 20 (buf.st_mode) )
e5af0 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 continue;. if
e5b00 28 20 61 63 63 65 73 73 28 61 7a 44 69 72 73 5b ( access(azDirs[
e5b10 69 5d 2c 20 30 37 29 20 29 20 63 6f 6e 74 69 6e i], 07) ) contin
e5b20 75 65 3b 0a 20 20 20 20 7a 44 69 72 20 3d 20 61 ue;. zDir = a
e5b30 7a 44 69 72 73 5b 69 5d 3b 0a 20 20 20 20 62 72 zDirs[i];. br
e5b40 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 eak;. }.. /* C
e5b50 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 6f 75 heck that the ou
e5b60 74 70 75 74 20 62 75 66 66 65 72 20 69 73 20 6c tput buffer is l
e5b70 61 72 67 65 20 65 6e 6f 75 67 68 20 66 6f 72 20 arge enough for
e5b80 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 66 69 the temporary fi
e5b90 6c 65 20 0a 20 20 2a 2a 20 6e 61 6d 65 2e 20 49 le . ** name. I
e5ba0 66 20 69 74 20 69 73 20 6e 6f 74 2c 20 72 65 74 f it is not, ret
e5bb0 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 urn SQLITE_ERROR
e5bc0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 73 74 .. */. if( (st
e5bd0 72 6c 65 6e 28 7a 44 69 72 29 20 2b 20 73 74 72 rlen(zDir) + str
e5be0 6c 65 6e 28 53 51 4c 49 54 45 5f 54 45 4d 50 5f len(SQLITE_TEMP_
e5bf0 46 49 4c 45 5f 50 52 45 46 49 58 29 20 2b 20 31 FILE_PREFIX) + 1
e5c00 37 29 20 3e 3d 20 28 73 69 7a 65 5f 74 29 6e 42 7) >= (size_t)nB
e5c10 75 66 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e uf ){. return
e5c20 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 SQLITE_ERROR;.
e5c30 20 7d 0a 0a 20 20 64 6f 7b 0a 20 20 20 20 73 71 }.. do{. sq
e5c40 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e lite3_snprintf(n
e5c50 42 75 66 2d 31 37 2c 20 7a 42 75 66 2c 20 22 25 Buf-17, zBuf, "%
e5c60 73 2f 22 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 s/"SQLITE_TEMP_F
e5c70 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a 44 69 72 ILE_PREFIX, zDir
e5c80 29 3b 0a 20 20 20 20 6a 20 3d 20 28 69 6e 74 29 );. j = (int)
e5c90 73 74 72 6c 65 6e 28 7a 42 75 66 29 3b 0a 20 20 strlen(zBuf);.
e5ca0 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d sqlite3_random
e5cb0 6e 65 73 73 28 31 35 2c 20 26 7a 42 75 66 5b 6a ness(15, &zBuf[j
e5cc0 5d 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b ]);. for(i=0;
e5cd0 20 69 3c 31 35 3b 20 69 2b 2b 2c 20 6a 2b 2b 29 i<15; i++, j++)
e5ce0 7b 0a 20 20 20 20 20 20 7a 42 75 66 5b 6a 5d 20 {. zBuf[j]
e5cf0 3d 20 28 63 68 61 72 29 7a 43 68 61 72 73 5b 20 = (char)zChars[
e5d00 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 ((unsigned char)
e5d10 7a 42 75 66 5b 6a 5d 29 25 28 73 69 7a 65 6f 66 zBuf[j])%(sizeof
e5d20 28 7a 43 68 61 72 73 29 2d 31 29 20 5d 3b 0a 20 (zChars)-1) ];.
e5d30 20 20 20 7d 0a 20 20 20 20 7a 42 75 66 5b 6a 5d }. zBuf[j]
e5d40 20 3d 20 30 3b 0a 20 20 7d 77 68 69 6c 65 28 20 = 0;. }while(
e5d50 61 63 63 65 73 73 28 7a 42 75 66 2c 30 29 3d 3d access(zBuf,0)==
e5d60 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 0 );. return SQ
e5d70 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 LITE_OK;.}..#if
e5d80 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f SQLITE_ENABLE_LO
e5d90 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 CKING_STYLE && d
e5da0 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f efined(__APPLE__
e5db0 29 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 20 )./*.** Routine
e5dc0 74 6f 20 74 72 61 6e 73 66 6f 72 6d 20 61 20 75 to transform a u
e5dd0 6e 69 78 46 69 6c 65 20 69 6e 74 6f 20 61 20 70 nixFile into a p
e5de0 72 6f 78 79 2d 6c 6f 63 6b 69 6e 67 20 75 6e 69 roxy-locking uni
e5df0 78 46 69 6c 65 2e 0a 2a 2a 20 49 6d 70 6c 65 6d xFile..** Implem
e5e00 65 6e 74 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 entation in the
e5e10 70 72 6f 78 79 2d 6c 6f 63 6b 20 64 69 76 69 73 proxy-lock divis
e5e20 69 6f 6e 2c 20 62 75 74 20 75 73 65 64 20 62 79 ion, but used by
e5e30 20 75 6e 69 78 4f 70 65 6e 28 29 0a 2a 2a 20 69 unixOpen().** i
e5e40 66 20 53 51 4c 49 54 45 5f 50 52 45 46 45 52 5f f SQLITE_PREFER_
e5e50 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 20 69 73 PROXY_LOCKING is
e5e60 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 defined..*/.sta
e5e70 74 69 63 20 69 6e 74 20 70 72 6f 78 79 54 72 61 tic int proxyTra
e5e80 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c 65 28 75 nsformUnixFile(u
e5e90 6e 69 78 46 69 6c 65 2a 2c 20 63 6f 6e 73 74 20 nixFile*, const
e5ea0 63 68 61 72 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a char*);.#endif..
e5eb0 2f 2a 0a 2a 2a 20 53 65 61 72 63 68 20 66 6f 72 /*.** Search for
e5ec0 20 61 6e 20 75 6e 75 73 65 64 20 66 69 6c 65 20 an unused file
e5ed0 64 65 73 63 72 69 70 74 6f 72 20 74 68 61 74 20 descriptor that
e5ee0 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 74 68 was opened on th
e5ef0 65 20 64 61 74 61 62 61 73 65 20 0a 2a 2a 20 66 e database .** f
e5f00 69 6c 65 20 28 6e 6f 74 20 61 20 6a 6f 75 72 6e ile (not a journ
e5f10 61 6c 20 6f 72 20 6d 61 73 74 65 72 2d 6a 6f 75 al or master-jou
e5f20 72 6e 61 6c 20 66 69 6c 65 29 20 69 64 65 6e 74 rnal file) ident
e5f30 69 66 69 65 64 20 62 79 20 70 61 74 68 6e 61 6d ified by pathnam
e5f40 65 0a 2a 2a 20 7a 50 61 74 68 20 77 69 74 68 20 e.** zPath with
e5f50 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 58 58 58 20 SQLITE_OPEN_XXX
e5f60 66 6c 61 67 73 20 6d 61 74 63 68 69 6e 67 20 74 flags matching t
e5f70 68 6f 73 65 20 70 61 73 73 65 64 20 61 73 20 74 hose passed as t
e5f80 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 61 72 67 he second.** arg
e5f90 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 ument to this fu
e5fa0 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 75 nction..**.** Su
e5fb0 63 68 20 61 20 66 69 6c 65 20 64 65 73 63 72 69 ch a file descri
e5fc0 70 74 6f 72 20 6d 61 79 20 65 78 69 73 74 20 69 ptor may exist i
e5fd0 66 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e f a database con
e5fe0 6e 65 63 74 69 6f 6e 20 77 61 73 20 63 6c 6f 73 nection was clos
e5ff0 65 64 0a 2a 2a 20 62 75 74 20 74 68 65 20 61 73 ed.** but the as
e6000 73 6f 63 69 61 74 65 64 20 66 69 6c 65 20 64 65 sociated file de
e6010 73 63 72 69 70 74 6f 72 20 63 6f 75 6c 64 20 6e scriptor could n
e6020 6f 74 20 62 65 20 63 6c 6f 73 65 64 20 62 65 63 ot be closed bec
e6030 61 75 73 65 20 73 6f 6d 65 0a 2a 2a 20 6f 74 68 ause some.** oth
e6040 65 72 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 er file descript
e6050 6f 72 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 73 or open on the s
e6060 61 6d 65 20 66 69 6c 65 20 69 73 20 68 6f 6c 64 ame file is hold
e6070 69 6e 67 20 61 20 66 69 6c 65 2d 6c 6f 63 6b 2e ing a file-lock.
e6080 0a 2a 2a 20 52 65 66 65 72 20 74 6f 20 63 6f 6d .** Refer to com
e6090 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 75 6e 69 ments in the uni
e60a0 78 43 6c 6f 73 65 28 29 20 66 75 6e 63 74 69 6f xClose() functio
e60b0 6e 20 61 6e 64 20 74 68 65 20 6c 65 6e 67 74 68 n and the length
e60c0 79 20 63 6f 6d 6d 65 6e 74 0a 2a 2a 20 64 65 73 y comment.** des
e60d0 63 72 69 62 69 6e 67 20 22 50 6f 73 69 78 20 41 cribing "Posix A
e60e0 64 76 69 73 6f 72 79 20 4c 6f 63 6b 69 6e 67 22 dvisory Locking"
e60f0 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 at the start of
e6100 20 74 68 69 73 20 66 69 6c 65 20 66 6f 72 20 0a this file for .
e6110 2a 2a 20 66 75 72 74 68 65 72 20 64 65 74 61 69 ** further detai
e6120 6c 73 2e 20 41 6c 73 6f 2c 20 74 69 63 6b 65 74 ls. Also, ticket
e6130 20 23 34 30 31 38 2e 0a 2a 2a 0a 2a 2a 20 49 66 #4018..**.** If
e6140 20 61 20 73 75 69 74 61 62 6c 65 20 66 69 6c 65 a suitable file
e6150 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 66 descriptor is f
e6160 6f 75 6e 64 2c 20 74 68 65 6e 20 69 74 20 69 73 ound, then it is
e6170 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 6e 6f returned. If no
e6180 0a 2a 2a 20 73 75 63 68 20 66 69 6c 65 20 64 65 .** such file de
e6190 73 63 72 69 70 74 6f 72 20 69 73 20 6c 6f 63 61 scriptor is loca
e61a0 74 65 64 2c 20 2d 31 20 69 73 20 72 65 74 75 72 ted, -1 is retur
e61b0 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 55 ned..*/.static U
e61c0 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 66 69 6e nixUnusedFd *fin
e61d0 64 52 65 75 73 61 62 6c 65 46 64 28 63 6f 6e 73 dReusableFd(cons
e61e0 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 69 t char *zPath, i
e61f0 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 55 6e 69 nt flags){. Uni
e6200 78 55 6e 75 73 65 64 46 64 20 2a 70 55 6e 75 73 xUnusedFd *pUnus
e6210 65 64 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 44 6f ed = 0;.. /* Do
e6220 20 6e 6f 74 20 73 65 61 72 63 68 20 66 6f 72 20 not search for
e6230 61 6e 20 75 6e 75 73 65 64 20 66 69 6c 65 20 64 an unused file d
e6240 65 73 63 72 69 70 74 6f 72 20 6f 6e 20 76 78 77 escriptor on vxw
e6250 6f 72 6b 73 2e 20 4e 6f 74 20 62 65 63 61 75 73 orks. Not becaus
e6260 65 0a 20 20 2a 2a 20 76 78 77 6f 72 6b 73 20 77 e. ** vxworks w
e6270 6f 75 6c 64 20 6e 6f 74 20 62 65 6e 65 66 69 74 ould not benefit
e6280 20 66 72 6f 6d 20 74 68 65 20 63 68 61 6e 67 65 from the change
e6290 20 28 69 74 20 6d 69 67 68 74 2c 20 77 65 27 72 (it might, we'r
e62a0 65 20 6e 6f 74 20 73 75 72 65 29 2c 0a 20 20 2a e not sure),. *
e62b0 2a 20 62 75 74 20 62 65 63 61 75 73 65 20 6e 6f * but because no
e62c0 20 77 61 79 20 74 6f 20 74 65 73 74 20 69 74 20 way to test it
e62d0 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 76 61 is currently ava
e62e0 69 6c 61 62 6c 65 2e 20 49 74 20 69 73 20 62 65 ilable. It is be
e62f0 74 74 65 72 20 0a 20 20 2a 2a 20 6e 6f 74 20 74 tter . ** not t
e6300 6f 20 72 69 73 6b 20 62 72 65 61 6b 69 6e 67 20 o risk breaking
e6310 76 78 77 6f 72 6b 73 20 73 75 70 70 6f 72 74 20 vxworks support
e6320 66 6f 72 20 74 68 65 20 73 61 6b 65 20 6f 66 20 for the sake of
e6330 73 75 63 68 20 61 6e 20 6f 62 73 63 75 72 65 20 such an obscure
e6340 0a 20 20 2a 2a 20 66 65 61 74 75 72 65 2e 20 20 . ** feature.
e6350 2a 2f 0a 23 69 66 20 21 4f 53 5f 56 58 57 4f 52 */.#if !OS_VXWOR
e6360 4b 53 0a 20 20 73 74 72 75 63 74 20 73 74 61 74 KS. struct stat
e6370 20 73 53 74 61 74 3b 20 20 20 20 20 20 20 20 20 sStat;
e6380 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 /* Res
e6390 75 6c 74 73 20 6f 66 20 73 74 61 74 28 29 20 63 ults of stat() c
e63a0 61 6c 6c 20 2a 2f 0a 0a 20 20 2f 2a 20 41 20 73 all */.. /* A s
e63b0 74 61 74 28 29 20 63 61 6c 6c 20 6d 61 79 20 66 tat() call may f
e63c0 61 69 6c 20 66 6f 72 20 76 61 72 69 6f 75 73 20 ail for various
e63d0 72 65 61 73 6f 6e 73 2e 20 49 66 20 74 68 69 73 reasons. If this
e63e0 20 68 61 70 70 65 6e 73 2c 20 69 74 20 69 73 0a happens, it is.
e63f0 20 20 2a 2a 20 61 6c 6d 6f 73 74 20 63 65 72 74 ** almost cert
e6400 61 69 6e 20 74 68 61 74 20 61 6e 20 6f 70 65 6e ain that an open
e6410 28 29 20 63 61 6c 6c 20 6f 6e 20 74 68 65 20 73 () call on the s
e6420 61 6d 65 20 70 61 74 68 20 77 69 6c 6c 20 61 6c ame path will al
e6430 73 6f 20 66 61 69 6c 2e 0a 20 20 2a 2a 20 46 6f so fail.. ** Fo
e6440 72 20 74 68 69 73 20 72 65 61 73 6f 6e 2c 20 69 r this reason, i
e6450 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
e6460 73 20 69 6e 20 74 68 65 20 73 74 61 74 28 29 20 s in the stat()
e6470 63 61 6c 6c 20 68 65 72 65 2c 20 69 74 20 69 73 call here, it is
e6480 0a 20 20 2a 2a 20 69 67 6e 6f 72 65 64 20 61 6e . ** ignored an
e6490 64 20 2d 31 20 69 73 20 72 65 74 75 72 6e 65 64 d -1 is returned
e64a0 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 77 69 6c . The caller wil
e64b0 6c 20 74 72 79 20 74 6f 20 6f 70 65 6e 20 61 20 l try to open a
e64c0 6e 65 77 20 66 69 6c 65 0a 20 20 2a 2a 20 64 65 new file. ** de
e64d0 73 63 72 69 70 74 6f 72 20 6f 6e 20 74 68 65 20 scriptor on the
e64e0 73 61 6d 65 20 70 61 74 68 2c 20 66 61 69 6c 2c same path, fail,
e64f0 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65 and return an e
e6500 72 72 6f 72 20 74 6f 20 53 51 4c 69 74 65 2e 0a rror to SQLite..
e6510 20 20 2a 2a 0a 20 20 2a 2a 20 45 76 65 6e 20 69 **. ** Even i
e6520 66 20 61 20 73 75 62 73 65 71 75 65 6e 74 20 6f f a subsequent o
e6530 70 65 6e 28 29 20 63 61 6c 6c 20 64 6f 65 73 20 pen() call does
e6540 73 75 63 63 65 65 64 2c 20 74 68 65 20 63 6f 6e succeed, the con
e6550 73 65 71 75 65 6e 63 65 73 20 6f 66 0a 20 20 2a sequences of. *
e6560 2a 20 6e 6f 74 20 73 65 61 72 63 68 69 6e 67 20 * not searching
e6570 66 6f 72 20 61 20 72 65 73 75 73 61 62 6c 65 20 for a resusable
e6580 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
e6590 61 72 65 20 6e 6f 74 20 64 69 72 65 2e 20 20 2a are not dire. *
e65a0 2f 0a 20 20 69 66 28 20 30 3d 3d 73 74 61 74 28 /. if( 0==stat(
e65b0 7a 50 61 74 68 2c 20 26 73 53 74 61 74 29 20 29 zPath, &sStat) )
e65c0 7b 0a 20 20 20 20 73 74 72 75 63 74 20 75 6e 69 {. struct uni
e65d0 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 3b 0a 20 20 xOpenCnt *pO;.
e65e0 20 20 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c struct unixFil
e65f0 65 49 64 20 69 64 3b 0a 20 20 20 20 69 64 2e 64 eId id;. id.d
e6600 65 76 20 3d 20 73 53 74 61 74 2e 73 74 5f 64 65 ev = sStat.st_de
e6610 76 3b 0a 20 20 20 20 69 64 2e 69 6e 6f 20 3d 20 v;. id.ino =
e6620 73 53 74 61 74 2e 73 74 5f 69 6e 6f 3b 0a 0a 20 sStat.st_ino;..
e6630 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 unixEnterMute
e6640 78 28 29 3b 0a 20 20 20 20 66 6f 72 28 70 4f 3d x();. for(pO=
e6650 6f 70 65 6e 4c 69 73 74 3b 20 70 4f 20 26 26 20 openList; pO &&
e6660 6d 65 6d 63 6d 70 28 26 69 64 2c 20 26 70 4f 2d memcmp(&id, &pO-
e6670 3e 66 69 6c 65 49 64 2c 20 73 69 7a 65 6f 66 28 >fileId, sizeof(
e6680 69 64 29 29 3b 20 70 4f 3d 70 4f 2d 3e 70 4e 65 id)); pO=pO->pNe
e6690 78 74 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 20 xt);. if( pO
e66a0 29 7b 0a 20 20 20 20 20 20 55 6e 69 78 55 6e 75 ){. UnixUnu
e66b0 73 65 64 46 64 20 2a 2a 70 70 3b 0a 20 20 20 20 sedFd **pp;.
e66c0 20 20 66 6f 72 28 70 70 3d 26 70 4f 2d 3e 70 55 for(pp=&pO->pU
e66d0 6e 75 73 65 64 3b 20 2a 70 70 20 26 26 20 28 2a nused; *pp && (*
e66e0 70 70 29 2d 3e 66 6c 61 67 73 21 3d 66 6c 61 67 pp)->flags!=flag
e66f0 73 3b 20 70 70 3d 26 28 28 2a 70 70 29 2d 3e 70 s; pp=&((*pp)->p
e6700 4e 65 78 74 29 29 3b 0a 20 20 20 20 20 20 70 55 Next));. pU
e6710 6e 75 73 65 64 20 3d 20 2a 70 70 3b 0a 20 20 20 nused = *pp;.
e6720 20 20 20 69 66 28 20 70 55 6e 75 73 65 64 20 29 if( pUnused )
e6730 7b 0a 20 20 20 20 20 20 20 20 2a 70 70 20 3d 20 {. *pp =
e6740 70 55 6e 75 73 65 64 2d 3e 70 4e 65 78 74 3b 0a pUnused->pNext;.
e6750 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
e6760 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 unixLeaveMutex
e6770 28 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 20 ();. }.#endif
e6780 20 20 2f 2a 20 69 66 20 21 4f 53 5f 56 58 57 4f /* if !OS_VXWO
e6790 52 4b 53 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 RKS */. return
e67a0 70 55 6e 75 73 65 64 3b 0a 7d 0a 0a 2f 2a 0a 2a pUnused;.}../*.*
e67b0 2a 20 4f 70 65 6e 20 74 68 65 20 66 69 6c 65 20 * Open the file
e67c0 7a 50 61 74 68 2e 0a 2a 2a 20 0a 2a 2a 20 50 72 zPath..** .** Pr
e67d0 65 76 69 6f 75 73 6c 79 2c 20 74 68 65 20 53 51 eviously, the SQ
e67e0 4c 69 74 65 20 4f 53 20 6c 61 79 65 72 20 75 73 Lite OS layer us
e67f0 65 64 20 74 68 72 65 65 20 66 75 6e 63 74 69 6f ed three functio
e6800 6e 73 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 74 ns in place of t
e6810 68 69 73 0a 2a 2a 20 6f 6e 65 3a 0a 2a 2a 0a 2a his.** one:.**.*
e6820 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4f * sqlite3OsO
e6830 70 65 6e 52 65 61 64 57 72 69 74 65 28 29 3b 0a penReadWrite();.
e6840 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 ** sqlite3Os
e6850 4f 70 65 6e 52 65 61 64 4f 6e 6c 79 28 29 3b 0a OpenReadOnly();.
e6860 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 ** sqlite3Os
e6870 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 28 29 3b OpenExclusive();
e6880 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 61 6c .**.** These cal
e6890 6c 73 20 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f ls correspond to
e68a0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 the following c
e68b0 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 6f 66 20 66 ombinations of f
e68c0 6c 61 67 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 lags:.**.**
e68d0 52 65 61 64 57 72 69 74 65 28 29 20 2d 3e 20 20 ReadWrite() ->
e68e0 20 20 20 28 52 45 41 44 57 52 49 54 45 20 7c 20 (READWRITE |
e68f0 43 52 45 41 54 45 29 0a 2a 2a 20 20 20 20 20 52 CREATE).** R
e6900 65 61 64 4f 6e 6c 79 28 29 20 20 2d 3e 20 20 20 eadOnly() ->
e6910 20 20 28 52 45 41 44 4f 4e 4c 59 29 20 0a 2a 2a (READONLY) .**
e6920 20 20 20 20 20 4f 70 65 6e 45 78 63 6c 75 73 69 OpenExclusi
e6930 76 65 28 29 20 2d 3e 20 28 52 45 41 44 57 52 49 ve() -> (READWRI
e6940 54 45 20 7c 20 43 52 45 41 54 45 20 7c 20 45 58 TE | CREATE | EX
e6950 43 4c 55 53 49 56 45 29 0a 2a 2a 0a 2a 2a 20 54 CLUSIVE).**.** T
e6960 68 65 20 6f 6c 64 20 4f 70 65 6e 45 78 63 6c 75 he old OpenExclu
e6970 73 69 76 65 28 29 20 61 63 63 65 70 74 65 64 20 sive() accepted
e6980 61 20 62 6f 6f 6c 65 61 6e 20 61 72 67 75 6d 65 a boolean argume
e6990 6e 74 20 2d 20 22 64 65 6c 46 6c 61 67 22 2e 20 nt - "delFlag".
e69a0 49 66 0a 2a 2a 20 74 72 75 65 2c 20 74 68 65 20 If.** true, the
e69b0 66 69 6c 65 20 77 61 73 20 63 6f 6e 66 69 67 75 file was configu
e69c0 72 65 64 20 74 6f 20 62 65 20 61 75 74 6f 6d 61 red to be automa
e69d0 74 69 63 61 6c 6c 79 20 64 65 6c 65 74 65 64 20 tically deleted
e69e0 77 68 65 6e 20 74 68 65 0a 2a 2a 20 66 69 6c 65 when the.** file
e69f0 20 68 61 6e 64 6c 65 20 63 6c 6f 73 65 64 2e 20 handle closed.
e6a00 54 6f 20 61 63 68 69 65 76 65 20 74 68 65 20 73 To achieve the s
e6a10 61 6d 65 20 65 66 66 65 63 74 20 75 73 69 6e 67 ame effect using
e6a20 20 74 68 69 73 20 6e 65 77 20 0a 2a 2a 20 69 6e this new .** in
e6a30 74 65 72 66 61 63 65 2c 20 61 64 64 20 74 68 65 terface, add the
e6a40 20 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 66 DELETEONCLOSE f
e6a50 6c 61 67 20 74 6f 20 74 68 6f 73 65 20 73 70 65 lag to those spe
e6a60 63 69 66 69 65 64 20 61 62 6f 76 65 20 66 6f 72 cified above for
e6a70 20 0a 2a 2a 20 4f 70 65 6e 45 78 63 6c 75 73 69 .** OpenExclusi
e6a80 76 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ve()..*/.static
e6a90 69 6e 74 20 75 6e 69 78 4f 70 65 6e 28 0a 20 20 int unixOpen(.
e6aa0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
e6ab0 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 s, /*
e6ac0 54 68 65 20 56 46 53 20 66 6f 72 20 77 68 69 63 The VFS for whic
e6ad0 68 20 74 68 69 73 20 69 73 20 74 68 65 20 78 4f h this is the xO
e6ae0 70 65 6e 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 pen method */.
e6af0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 const char *zPat
e6b00 68 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 h, /*
e6b10 50 61 74 68 6e 61 6d 65 20 6f 66 20 66 69 6c 65 Pathname of file
e6b20 20 74 6f 20 62 65 20 6f 70 65 6e 65 64 20 2a 2f to be opened */
e6b30 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 . sqlite3_file
e6b40 2a 70 46 69 6c 65 2c 20 20 20 20 20 20 20 20 20 *pFile,
e6b50 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65 73 63 /* The file desc
e6b60 72 69 70 74 6f 72 20 74 6f 20 62 65 20 66 69 6c riptor to be fil
e6b70 6c 65 64 20 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 led in */. int
e6b80 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 flags,
e6b90 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 /* Inpu
e6ba0 74 20 66 6c 61 67 73 20 74 6f 20 63 6f 6e 74 72 t flags to contr
e6bb0 6f 6c 20 74 68 65 20 6f 70 65 6e 69 6e 67 20 2a ol the opening *
e6bc0 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c 61 /. int *pOutFla
e6bd0 67 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 gs
e6be0 20 2f 2a 20 4f 75 74 70 75 74 20 66 6c 61 67 73 /* Output flags
e6bf0 20 72 65 74 75 72 6e 65 64 20 74 6f 20 53 51 4c returned to SQL
e6c00 69 74 65 20 63 6f 72 65 20 2a 2f 0a 29 7b 0a 20 ite core */.){.
e6c10 20 75 6e 69 78 46 69 6c 65 20 2a 70 20 3d 20 28 unixFile *p = (
e6c20 75 6e 69 78 46 69 6c 65 20 2a 29 70 46 69 6c 65 unixFile *)pFile
e6c30 3b 0a 20 20 69 6e 74 20 66 64 20 3d 20 2d 31 3b ;. int fd = -1;
e6c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e6c50 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 /* File descr
e6c60 69 70 74 6f 72 20 72 65 74 75 72 6e 65 64 20 62 iptor returned b
e6c70 79 20 6f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e y open() */. in
e6c80 74 20 64 69 72 66 64 20 3d 20 2d 31 3b 20 20 20 t dirfd = -1;
e6c90 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
e6ca0 44 69 72 65 63 74 6f 72 79 20 66 69 6c 65 20 64 Directory file d
e6cb0 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 69 escriptor */. i
e6cc0 6e 74 20 6f 70 65 6e 46 6c 61 67 73 20 3d 20 30 nt openFlags = 0
e6cd0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
e6ce0 20 46 6c 61 67 73 20 74 6f 20 70 61 73 73 20 74 Flags to pass t
e6cf0 6f 20 6f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e o open() */. in
e6d00 74 20 65 54 79 70 65 20 3d 20 66 6c 61 67 73 26 t eType = flags&
e6d10 30 78 46 46 46 46 46 46 30 30 3b 20 20 2f 2a 20 0xFFFFFF00; /*
e6d20 54 79 70 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 Type of file to
e6d30 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f open */. int no
e6d40 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20 Lock;
e6d50 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
e6d60 20 74 6f 20 6f 6d 69 74 20 6c 6f 63 6b 69 6e 67 to omit locking
e6d70 20 70 72 69 6d 69 74 69 76 65 73 20 2a 2f 0a 20 primitives */.
e6d80 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
e6d90 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 _OK;
e6da0 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 52 65 74 75 /* Function Retu
e6db0 72 6e 20 43 6f 64 65 20 2a 2f 0a 0a 20 20 69 6e rn Code */.. in
e6dc0 74 20 69 73 45 78 63 6c 75 73 69 76 65 20 20 3d t isExclusive =
e6dd0 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 (flags & SQLITE
e6de0 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 29 _OPEN_EXCLUSIVE)
e6df0 3b 0a 20 20 69 6e 74 20 69 73 44 65 6c 65 74 65 ;. int isDelete
e6e00 20 20 20 20 20 3d 20 28 66 6c 61 67 73 20 26 20 = (flags &
e6e10 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 SQLITE_OPEN_DELE
e6e20 54 45 4f 4e 43 4c 4f 53 45 29 3b 0a 20 20 69 6e TEONCLOSE);. in
e6e30 74 20 69 73 43 72 65 61 74 65 20 20 20 20 20 3d t isCreate =
e6e40 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 (flags & SQLITE
e6e50 5f 4f 50 45 4e 5f 43 52 45 41 54 45 29 3b 0a 20 _OPEN_CREATE);.
e6e60 20 69 6e 74 20 69 73 52 65 61 64 6f 6e 6c 79 20 int isReadonly
e6e70 20 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c = (flags & SQL
e6e80 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c ITE_OPEN_READONL
e6e90 59 29 3b 0a 20 20 69 6e 74 20 69 73 52 65 61 64 Y);. int isRead
e6ea0 57 72 69 74 65 20 20 3d 20 28 66 6c 61 67 73 20 Write = (flags
e6eb0 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 & SQLITE_OPEN_RE
e6ec0 41 44 57 52 49 54 45 29 3b 0a 0a 20 20 2f 2a 20 ADWRITE);.. /*
e6ed0 49 66 20 63 72 65 61 74 69 6e 67 20 61 20 6d 61 If creating a ma
e6ee0 73 74 65 72 20 6f 72 20 6d 61 69 6e 2d 66 69 6c ster or main-fil
e6ef0 65 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 69 73 20 e journal, this
e6f00 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c 20 6f 70 function will op
e6f10 65 6e 0a 20 20 2a 2a 20 61 20 66 69 6c 65 2d 64 en. ** a file-d
e6f20 65 73 63 72 69 70 74 6f 72 20 6f 6e 20 74 68 65 escriptor on the
e6f30 20 64 69 72 65 63 74 6f 72 79 20 74 6f 6f 2e 20 directory too.
e6f40 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 75 The first time u
e6f50 6e 69 78 53 79 6e 63 28 29 0a 20 20 2a 2a 20 69 nixSync(). ** i
e6f60 73 20 63 61 6c 6c 65 64 20 74 68 65 20 64 69 72 s called the dir
e6f70 65 63 74 6f 72 79 20 66 69 6c 65 20 64 65 73 63 ectory file desc
e6f80 72 69 70 74 6f 72 20 77 69 6c 6c 20 62 65 20 66 riptor will be f
e6f90 73 79 6e 63 28 29 65 64 20 61 6e 64 20 63 6c 6f sync()ed and clo
e6fa0 73 65 28 29 64 2e 0a 20 20 2a 2f 0a 20 20 69 6e se()d.. */. in
e6fb0 74 20 69 73 4f 70 65 6e 44 69 72 65 63 74 6f 72 t isOpenDirector
e6fc0 79 20 3d 20 28 69 73 43 72 65 61 74 65 20 26 26 y = (isCreate &&
e6fd0 20 0a 20 20 20 20 20 20 28 65 54 79 70 65 3d 3d . (eType==
e6fe0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 SQLITE_OPEN_MAST
e6ff0 45 52 5f 4a 4f 55 52 4e 41 4c 20 7c 7c 20 65 54 ER_JOURNAL || eT
e7000 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e ype==SQLITE_OPEN
e7010 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29 0a 20 _MAIN_JOURNAL).
e7020 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 72 67 );.. /* If arg
e7030 75 6d 65 6e 74 20 7a 50 61 74 68 20 69 73 20 61 ument zPath is a
e7040 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 74 NULL pointer, t
e7050 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
e7060 72 65 71 75 69 72 65 64 20 74 6f 20 6f 70 65 6e required to open
e7070 0a 20 20 2a 2a 20 61 20 74 65 6d 70 6f 72 61 72 . ** a temporar
e7080 79 20 66 69 6c 65 2e 20 55 73 65 20 74 68 69 73 y file. Use this
e7090 20 62 75 66 66 65 72 20 74 6f 20 73 74 6f 72 65 buffer to store
e70a0 20 74 68 65 20 66 69 6c 65 20 6e 61 6d 65 20 69 the file name i
e70b0 6e 2e 0a 20 20 2a 2f 0a 20 20 63 68 61 72 20 7a n.. */. char z
e70c0 54 6d 70 6e 61 6d 65 5b 4d 41 58 5f 50 41 54 48 Tmpname[MAX_PATH
e70d0 4e 41 4d 45 2b 31 5d 3b 0a 20 20 63 6f 6e 73 74 NAME+1];. const
e70e0 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 7a char *zName = z
e70f0 50 61 74 68 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 Path;.. /* Chec
e7100 6b 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 k the following
e7110 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 74 statements are t
e7120 72 75 65 3a 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 rue: . **. **
e7130 20 20 28 61 29 20 45 78 61 63 74 6c 79 20 6f 6e (a) Exactly on
e7140 65 20 6f 66 20 74 68 65 20 52 45 41 44 57 52 49 e of the READWRI
e7150 54 45 20 61 6e 64 20 52 45 41 44 4f 4e 4c 59 20 TE and READONLY
e7160 66 6c 61 67 73 20 6d 75 73 74 20 62 65 20 73 65 flags must be se
e7170 74 2c 20 61 6e 64 20 0a 20 20 2a 2a 20 20 20 28 t, and . ** (
e7180 62 29 20 69 66 20 43 52 45 41 54 45 20 69 73 20 b) if CREATE is
e7190 73 65 74 2c 20 74 68 65 6e 20 52 45 41 44 57 52 set, then READWR
e71a0 49 54 45 20 6d 75 73 74 20 61 6c 73 6f 20 62 65 ITE must also be
e71b0 20 73 65 74 2c 20 61 6e 64 0a 20 20 2a 2a 20 20 set, and. **
e71c0 20 28 63 29 20 69 66 20 45 58 43 4c 55 53 49 56 (c) if EXCLUSIV
e71d0 45 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 43 E is set, then C
e71e0 52 45 41 54 45 20 6d 75 73 74 20 61 6c 73 6f 20 REATE must also
e71f0 62 65 20 73 65 74 2e 0a 20 20 2a 2a 20 20 20 28 be set.. ** (
e7200 64 29 20 69 66 20 44 45 4c 45 54 45 4f 4e 43 4c d) if DELETEONCL
e7210 4f 53 45 20 69 73 20 73 65 74 2c 20 74 68 65 6e OSE is set, then
e7220 20 43 52 45 41 54 45 20 6d 75 73 74 20 61 6c 73 CREATE must als
e7230 6f 20 62 65 20 73 65 74 2e 0a 20 20 2a 2f 0a 20 o be set.. */.
e7240 20 61 73 73 65 72 74 28 28 69 73 52 65 61 64 6f assert((isReado
e7250 6e 6c 79 3d 3d 30 20 7c 7c 20 69 73 52 65 61 64 nly==0 || isRead
e7260 57 72 69 74 65 3d 3d 30 29 20 26 26 20 28 69 73 Write==0) && (is
e7270 52 65 61 64 57 72 69 74 65 20 7c 7c 20 69 73 52 ReadWrite || isR
e7280 65 61 64 6f 6e 6c 79 29 29 3b 0a 20 20 61 73 73 eadonly));. ass
e7290 65 72 74 28 69 73 43 72 65 61 74 65 3d 3d 30 20 ert(isCreate==0
e72a0 7c 7c 20 69 73 52 65 61 64 57 72 69 74 65 29 3b || isReadWrite);
e72b0 0a 20 20 61 73 73 65 72 74 28 69 73 45 78 63 6c . assert(isExcl
e72c0 75 73 69 76 65 3d 3d 30 20 7c 7c 20 69 73 43 72 usive==0 || isCr
e72d0 65 61 74 65 29 3b 0a 20 20 61 73 73 65 72 74 28 eate);. assert(
e72e0 69 73 44 65 6c 65 74 65 3d 3d 30 20 7c 7c 20 69 isDelete==0 || i
e72f0 73 43 72 65 61 74 65 29 3b 0a 0a 20 20 2f 2a 20 sCreate);.. /*
e7300 54 68 65 20 6d 61 69 6e 20 44 42 2c 20 6d 61 69 The main DB, mai
e7310 6e 20 6a 6f 75 72 6e 61 6c 2c 20 61 6e 64 20 6d n journal, and m
e7320 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 61 72 aster journal ar
e7330 65 20 6e 65 76 65 72 20 61 75 74 6f 6d 61 74 69 e never automati
e7340 63 61 6c 6c 79 0a 20 20 2a 2a 20 64 65 6c 65 74 cally. ** delet
e7350 65 64 2e 20 4e 6f 72 20 61 72 65 20 74 68 65 79 ed. Nor are they
e7360 20 65 76 65 72 20 74 65 6d 70 6f 72 61 72 79 20 ever temporary
e7370 66 69 6c 65 73 2e 20 20 2a 2f 0a 20 20 61 73 73 files. */. ass
e7380 65 72 74 28 20 28 21 69 73 44 65 6c 65 74 65 20 ert( (!isDelete
e7390 26 26 20 7a 4e 61 6d 65 29 20 7c 7c 20 65 54 79 && zName) || eTy
e73a0 70 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f pe!=SQLITE_OPEN_
e73b0 4d 41 49 4e 5f 44 42 20 29 3b 0a 20 20 61 73 73 MAIN_DB );. ass
e73c0 65 72 74 28 20 28 21 69 73 44 65 6c 65 74 65 20 ert( (!isDelete
e73d0 26 26 20 7a 4e 61 6d 65 29 20 7c 7c 20 65 54 79 && zName) || eTy
e73e0 70 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f pe!=SQLITE_OPEN_
e73f0 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 29 3b 0a MAIN_JOURNAL );.
e7400 20 20 61 73 73 65 72 74 28 20 28 21 69 73 44 65 assert( (!isDe
e7410 6c 65 74 65 20 26 26 20 7a 4e 61 6d 65 29 20 7c lete && zName) |
e7420 7c 20 65 54 79 70 65 21 3d 53 51 4c 49 54 45 5f | eType!=SQLITE_
e7430 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 OPEN_MASTER_JOUR
e7440 4e 41 4c 20 29 3b 0a 0a 20 20 2f 2a 20 41 73 73 NAL );.. /* Ass
e7450 65 72 74 20 74 68 61 74 20 74 68 65 20 75 70 70 ert that the upp
e7460 65 72 20 6c 61 79 65 72 20 68 61 73 20 73 65 74 er layer has set
e7470 20 6f 6e 65 20 6f 66 20 74 68 65 20 22 66 69 6c one of the "fil
e7480 65 2d 74 79 70 65 22 20 66 6c 61 67 73 2e 20 2a e-type" flags. *
e7490 2f 0a 20 20 61 73 73 65 72 74 28 20 65 54 79 70 /. assert( eTyp
e74a0 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d e==SQLITE_OPEN_M
e74b0 41 49 4e 5f 44 42 20 20 20 20 20 20 7c 7c 20 65 AIN_DB || e
e74c0 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 Type==SQLITE_OPE
e74d0 4e 5f 54 45 4d 50 5f 44 42 20 0a 20 20 20 20 20 N_TEMP_DB .
e74e0 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 || eType==SQLI
e74f0 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 TE_OPEN_MAIN_JOU
e7500 52 4e 41 4c 20 7c 7c 20 65 54 79 70 65 3d 3d 53 RNAL || eType==S
e7510 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f QLITE_OPEN_TEMP_
e7520 4a 4f 55 52 4e 41 4c 20 0a 20 20 20 20 20 20 20 JOURNAL .
e7530 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 || eType==SQLITE
e7540 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c _OPEN_SUBJOURNAL
e7550 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c || eType==SQL
e7560 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f ITE_OPEN_MASTER_
e7570 4a 4f 55 52 4e 41 4c 20 0a 20 20 20 20 20 20 20 JOURNAL .
e7580 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 || eType==SQLITE
e7590 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f _OPEN_TRANSIENT_
e75a0 44 42 0a 20 20 29 3b 0a 0a 20 20 6d 65 6d 73 65 DB. );.. memse
e75b0 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75 t(p, 0, sizeof(u
e75c0 6e 69 78 46 69 6c 65 29 29 3b 0a 0a 20 20 69 66 nixFile));.. if
e75d0 28 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f ( eType==SQLITE_
e75e0 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 29 7b 0a OPEN_MAIN_DB ){.
e75f0 20 20 20 20 55 6e 69 78 55 6e 75 73 65 64 46 64 UnixUnusedFd
e7600 20 2a 70 55 6e 75 73 65 64 3b 0a 20 20 20 20 70 *pUnused;. p
e7610 55 6e 75 73 65 64 20 3d 20 66 69 6e 64 52 65 75 Unused = findReu
e7620 73 61 62 6c 65 46 64 28 7a 4e 61 6d 65 2c 20 66 sableFd(zName, f
e7630 6c 61 67 73 29 3b 0a 20 20 20 20 69 66 28 20 70 lags);. if( p
e7640 55 6e 75 73 65 64 20 29 7b 0a 20 20 20 20 20 20 Unused ){.
e7650 66 64 20 3d 20 70 55 6e 75 73 65 64 2d 3e 66 64 fd = pUnused->fd
e7660 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
e7670 20 20 20 70 55 6e 75 73 65 64 20 3d 20 73 71 6c pUnused = sql
e7680 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 ite3_malloc(size
e7690 6f 66 28 2a 70 55 6e 75 73 65 64 29 29 3b 0a 20 of(*pUnused));.
e76a0 20 20 20 20 20 69 66 28 20 21 70 55 6e 75 73 65 if( !pUnuse
e76b0 64 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 d ){. ret
e76c0 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
e76d0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
e76e0 20 20 20 20 70 2d 3e 70 55 6e 75 73 65 64 20 3d p->pUnused =
e76f0 20 70 55 6e 75 73 65 64 3b 0a 20 20 7d 65 6c 73 pUnused;. }els
e7700 65 20 69 66 28 20 21 7a 4e 61 6d 65 20 29 7b 0a e if( !zName ){.
e7710 20 20 20 20 2f 2a 20 49 66 20 7a 4e 61 6d 65 20 /* If zName
e7720 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 75 70 70 is NULL, the upp
e7730 65 72 20 6c 61 79 65 72 20 69 73 20 72 65 71 75 er layer is requ
e7740 65 73 74 69 6e 67 20 61 20 74 65 6d 70 20 66 69 esting a temp fi
e7750 6c 65 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 le. */. asser
e7760 74 28 69 73 44 65 6c 65 74 65 20 26 26 20 21 69 t(isDelete && !i
e7770 73 4f 70 65 6e 44 69 72 65 63 74 6f 72 79 29 3b sOpenDirectory);
e7780 0a 20 20 20 20 72 63 20 3d 20 67 65 74 54 65 6d . rc = getTem
e7790 70 6e 61 6d 65 28 4d 41 58 5f 50 41 54 48 4e 41 pname(MAX_PATHNA
e77a0 4d 45 2b 31 2c 20 7a 54 6d 70 6e 61 6d 65 29 3b ME+1, zTmpname);
e77b0 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
e77c0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
e77d0 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d return rc;. }
e77e0 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 7a 54 6d . zName = zTm
e77f0 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a pname;. }.. /*
e7800 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20 76 Determine the v
e7810 61 6c 75 65 20 6f 66 20 74 68 65 20 66 6c 61 67 alue of the flag
e7820 73 20 70 61 72 61 6d 65 74 65 72 20 70 61 73 73 s parameter pass
e7830 65 64 20 74 6f 20 50 4f 53 49 58 20 66 75 6e 63 ed to POSIX func
e7840 74 69 6f 6e 0a 20 20 2a 2a 20 6f 70 65 6e 28 29 tion. ** open()
e7850 2e 20 54 68 65 73 65 20 6d 75 73 74 20 62 65 20 . These must be
e7860 63 61 6c 63 75 6c 61 74 65 64 20 65 76 65 6e 20 calculated even
e7870 69 66 20 6f 70 65 6e 28 29 20 69 73 20 6e 6f 74 if open() is not
e7880 20 63 61 6c 6c 65 64 2c 20 61 73 0a 20 20 2a 2a called, as. **
e7890 20 74 68 65 79 20 6d 61 79 20 62 65 20 73 74 6f they may be sto
e78a0 72 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 red as part of t
e78b0 68 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 61 he file handle a
e78c0 6e 64 20 75 73 65 64 20 62 79 20 74 68 65 20 0a nd used by the .
e78d0 20 20 2a 2a 20 27 63 6f 6e 63 68 20 66 69 6c 65 ** 'conch file
e78e0 27 20 6c 6f 63 6b 69 6e 67 20 66 75 6e 63 74 69 ' locking functi
e78f0 6f 6e 73 20 6c 61 74 65 72 20 6f 6e 2e 20 20 2a ons later on. *
e7900 2f 0a 20 20 69 66 28 20 69 73 52 65 61 64 6f 6e /. if( isReadon
e7910 6c 79 20 29 20 20 6f 70 65 6e 46 6c 61 67 73 20 ly ) openFlags
e7920 7c 3d 20 4f 5f 52 44 4f 4e 4c 59 3b 0a 20 20 69 |= O_RDONLY;. i
e7930 66 28 20 69 73 52 65 61 64 57 72 69 74 65 20 29 f( isReadWrite )
e7940 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 5f openFlags |= O_
e7950 52 44 57 52 3b 0a 20 20 69 66 28 20 69 73 43 72 RDWR;. if( isCr
e7960 65 61 74 65 20 29 20 20 20 20 6f 70 65 6e 46 6c eate ) openFl
e7970 61 67 73 20 7c 3d 20 4f 5f 43 52 45 41 54 3b 0a ags |= O_CREAT;.
e7980 20 20 69 66 28 20 69 73 45 78 63 6c 75 73 69 76 if( isExclusiv
e7990 65 20 29 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d e ) openFlags |=
e79a0 20 28 4f 5f 45 58 43 4c 7c 4f 5f 4e 4f 46 4f 4c (O_EXCL|O_NOFOL
e79b0 4c 4f 57 29 3b 0a 20 20 6f 70 65 6e 46 6c 61 67 LOW);. openFlag
e79c0 73 20 7c 3d 20 28 4f 5f 4c 41 52 47 45 46 49 4c s |= (O_LARGEFIL
e79d0 45 7c 4f 5f 42 49 4e 41 52 59 29 3b 0a 0a 20 20 E|O_BINARY);..
e79e0 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 if( fd<0 ){.
e79f0 6d 6f 64 65 5f 74 20 6f 70 65 6e 4d 6f 64 65 20 mode_t openMode
e7a00 3d 20 28 69 73 44 65 6c 65 74 65 3f 30 36 30 30 = (isDelete?0600
e7a10 3a 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f :SQLITE_DEFAULT_
e7a20 46 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53 FILE_PERMISSIONS
e7a30 29 3b 0a 20 20 20 20 66 64 20 3d 20 6f 70 65 6e );. fd = open
e7a40 28 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46 6c 61 67 (zName, openFlag
e7a50 73 2c 20 6f 70 65 6e 4d 6f 64 65 29 3b 0a 20 20 s, openMode);.
e7a60 20 20 4f 53 54 52 41 43 45 34 28 22 4f 50 45 4e OSTRACE4("OPEN
e7a70 58 20 20 20 25 2d 33 64 20 25 73 20 30 25 6f 5c X %-3d %s 0%o\
e7a80 6e 22 2c 20 66 64 2c 20 7a 4e 61 6d 65 2c 20 6f n", fd, zName, o
e7a90 70 65 6e 46 6c 61 67 73 29 3b 0a 20 20 20 20 69 penFlags);. i
e7aa0 66 28 20 66 64 3c 30 20 26 26 20 65 72 72 6e 6f f( fd<0 && errno
e7ab0 21 3d 45 49 53 44 49 52 20 26 26 20 69 73 52 65 !=EISDIR && isRe
e7ac0 61 64 57 72 69 74 65 20 26 26 20 21 69 73 45 78 adWrite && !isEx
e7ad0 63 6c 75 73 69 76 65 20 29 7b 0a 20 20 20 20 20 clusive ){.
e7ae0 20 2f 2a 20 46 61 69 6c 65 64 20 74 6f 20 6f 70 /* Failed to op
e7af0 65 6e 20 74 68 65 20 66 69 6c 65 20 66 6f 72 20 en the file for
e7b00 72 65 61 64 2f 77 72 69 74 65 20 61 63 63 65 73 read/write acces
e7b10 73 2e 20 54 72 79 20 72 65 61 64 2d 6f 6e 6c 79 s. Try read-only
e7b20 2e 20 2a 2f 0a 20 20 20 20 20 20 66 6c 61 67 73 . */. flags
e7b30 20 26 3d 20 7e 28 53 51 4c 49 54 45 5f 4f 50 45 &= ~(SQLITE_OPE
e7b40 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49 N_READWRITE|SQLI
e7b50 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 29 3b TE_OPEN_CREATE);
e7b60 0a 20 20 20 20 20 20 6f 70 65 6e 46 6c 61 67 73 . openFlags
e7b70 20 26 3d 20 7e 28 4f 5f 52 44 57 52 7c 4f 5f 43 &= ~(O_RDWR|O_C
e7b80 52 45 41 54 29 3b 0a 20 20 20 20 20 20 66 6c 61 REAT);. fla
e7b90 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 4f 50 45 gs |= SQLITE_OPE
e7ba0 4e 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 N_READONLY;.
e7bb0 20 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f openFlags |= O
e7bc0 5f 52 44 4f 4e 4c 59 3b 0a 20 20 20 20 20 20 66 _RDONLY;. f
e7bd0 64 20 3d 20 6f 70 65 6e 28 7a 4e 61 6d 65 2c 20 d = open(zName,
e7be0 6f 70 65 6e 46 6c 61 67 73 2c 20 6f 70 65 6e 4d openFlags, openM
e7bf0 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ode);. }.
e7c00 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 if( fd<0 ){.
e7c10 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 41 rc = SQLITE_CA
e7c20 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 20 20 67 6f NTOPEN;. go
e7c30 74 6f 20 6f 70 65 6e 5f 66 69 6e 69 73 68 65 64 to open_finished
e7c40 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 ;. }. }. as
e7c50 73 65 72 74 28 20 66 64 3e 3d 30 20 29 3b 0a 20 sert( fd>=0 );.
e7c60 20 69 66 28 20 70 4f 75 74 46 6c 61 67 73 20 29 if( pOutFlags )
e7c70 7b 0a 20 20 20 20 2a 70 4f 75 74 46 6c 61 67 73 {. *pOutFlags
e7c80 20 3d 20 66 6c 61 67 73 3b 0a 20 20 7d 0a 0a 20 = flags;. }..
e7c90 20 69 66 28 20 70 2d 3e 70 55 6e 75 73 65 64 20 if( p->pUnused
e7ca0 29 7b 0a 20 20 20 20 70 2d 3e 70 55 6e 75 73 65 ){. p->pUnuse
e7cb0 64 2d 3e 66 64 20 3d 20 66 64 3b 0a 20 20 20 20 d->fd = fd;.
e7cc0 70 2d 3e 70 55 6e 75 73 65 64 2d 3e 66 6c 61 67 p->pUnused->flag
e7cd0 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20 7d 0a 0a s = flags;. }..
e7ce0 20 20 69 66 28 20 69 73 44 65 6c 65 74 65 20 29 if( isDelete )
e7cf0 7b 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 {.#if OS_VXWORKS
e7d00 0a 20 20 20 20 7a 50 61 74 68 20 3d 20 7a 4e 61 . zPath = zNa
e7d10 6d 65 3b 0a 23 65 6c 73 65 0a 20 20 20 20 75 6e me;.#else. un
e7d20 6c 69 6e 6b 28 7a 4e 61 6d 65 29 3b 0a 23 65 6e link(zName);.#en
e7d30 64 69 66 0a 20 20 7d 0a 23 69 66 20 53 51 4c 49 dif. }.#if SQLI
e7d40 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e TE_ENABLE_LOCKIN
e7d50 47 5f 53 54 59 4c 45 0a 20 20 65 6c 73 65 7b 0a G_STYLE. else{.
e7d60 20 20 20 20 70 2d 3e 6f 70 65 6e 46 6c 61 67 73 p->openFlags
e7d70 20 3d 20 6f 70 65 6e 46 6c 61 67 73 3b 0a 20 20 = openFlags;.
e7d80 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 }.#endif.. if(
e7d90 69 73 4f 70 65 6e 44 69 72 65 63 74 6f 72 79 20 isOpenDirectory
e7da0 29 7b 0a 20 20 20 20 72 63 20 3d 20 6f 70 65 6e ){. rc = open
e7db0 44 69 72 65 63 74 6f 72 79 28 7a 50 61 74 68 2c Directory(zPath,
e7dc0 20 26 64 69 72 66 64 29 3b 0a 20 20 20 20 69 66 &dirfd);. if
e7dd0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
e7de0 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 74 20 69 ){. /* It i
e7df0 73 20 73 61 66 65 20 74 6f 20 63 6c 6f 73 65 20 s safe to close
e7e00 66 64 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 fd at this point
e7e10 2c 20 62 65 63 61 75 73 65 20 69 74 20 69 73 20 , because it is
e7e20 67 75 61 72 61 6e 74 65 65 64 20 6e 6f 74 0a 20 guaranteed not.
e7e30 20 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 6f 70 ** to be op
e7e40 65 6e 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65 en on a database
e7e50 20 66 69 6c 65 2e 20 49 66 20 69 74 20 77 65 72 file. If it wer
e7e60 65 20 6f 70 65 6e 20 6f 6e 20 61 20 64 61 74 61 e open on a data
e7e70 62 61 73 65 20 66 69 6c 65 2c 0a 20 20 20 20 20 base file,.
e7e80 20 2a 2a 20 69 74 20 77 6f 75 6c 64 20 6e 6f 74 ** it would not
e7e90 20 62 65 20 73 61 66 65 20 74 6f 20 63 6c 6f 73 be safe to clos
e7ea0 65 20 61 73 20 74 68 69 73 20 77 6f 75 6c 64 20 e as this would
e7eb0 72 65 6c 65 61 73 65 20 61 6e 79 20 6c 6f 63 6b release any lock
e7ec0 73 20 68 65 6c 64 0a 20 20 20 20 20 20 2a 2a 20 s held. **
e7ed0 6f 6e 20 74 68 65 20 66 69 6c 65 20 62 79 20 74 on the file by t
e7ee0 68 69 73 20 70 72 6f 63 65 73 73 2e 20 20 2a 2f his process. */
e7ef0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 65 . assert( e
e7f00 54 79 70 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45 Type!=SQLITE_OPE
e7f10 4e 5f 4d 41 49 4e 5f 44 42 20 29 3b 0a 20 20 20 N_MAIN_DB );.
e7f20 20 20 20 63 6c 6f 73 65 28 66 64 29 3b 20 20 20 close(fd);
e7f30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 69 6c /* sil
e7f40 65 6e 74 6c 79 20 6c 65 61 6b 20 69 66 20 66 61 ently leak if fa
e7f50 69 6c 2c 20 61 6c 72 65 61 64 79 20 69 6e 20 65 il, already in e
e7f60 72 72 6f 72 20 2a 2f 0a 20 20 20 20 20 20 67 6f rror */. go
e7f70 74 6f 20 6f 70 65 6e 5f 66 69 6e 69 73 68 65 64 to open_finished
e7f80 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 23 69 66 ;. }. }..#if
e7f90 64 65 66 20 46 44 5f 43 4c 4f 45 58 45 43 0a 20 def FD_CLOEXEC.
e7fa0 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 53 45 54 fcntl(fd, F_SET
e7fb0 46 44 2c 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f FD, fcntl(fd, F_
e7fc0 47 45 54 46 44 2c 20 30 29 20 7c 20 46 44 5f 43 GETFD, 0) | FD_C
e7fd0 4c 4f 45 58 45 43 29 3b 0a 23 65 6e 64 69 66 0a LOEXEC);.#endif.
e7fe0 0a 20 20 6e 6f 4c 6f 63 6b 20 3d 20 65 54 79 70 . noLock = eTyp
e7ff0 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d e!=SQLITE_OPEN_M
e8000 41 49 4e 5f 44 42 3b 0a 0a 23 69 66 20 53 51 4c AIN_DB;..#if SQL
e8010 49 54 45 5f 50 52 45 46 45 52 5f 50 52 4f 58 59 ITE_PREFER_PROXY
e8020 5f 4c 4f 43 4b 49 4e 47 0a 20 20 69 66 28 20 7a _LOCKING. if( z
e8030 50 61 74 68 21 3d 4e 55 4c 4c 20 26 26 20 21 6e Path!=NULL && !n
e8040 6f 4c 6f 63 6b 20 26 26 20 70 56 66 73 2d 3e 78 oLock && pVfs->x
e8050 4f 70 65 6e 20 29 7b 0a 20 20 20 20 63 68 61 72 Open ){. char
e8060 20 2a 65 6e 76 66 6f 72 63 65 20 3d 20 67 65 74 *envforce = get
e8070 65 6e 76 28 22 53 51 4c 49 54 45 5f 46 4f 52 43 env("SQLITE_FORC
e8080 45 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 22 E_PROXY_LOCKING"
e8090 29 3b 0a 20 20 20 20 69 6e 74 20 75 73 65 50 72 );. int usePr
e80a0 6f 78 79 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a oxy = 0;.. /*
e80b0 20 53 51 4c 49 54 45 5f 46 4f 52 43 45 5f 50 52 SQLITE_FORCE_PR
e80c0 4f 58 59 5f 4c 4f 43 4b 49 4e 47 3d 3d 31 20 6d OXY_LOCKING==1 m
e80d0 65 61 6e 73 20 66 6f 72 63 65 20 61 6c 77 61 79 eans force alway
e80e0 73 20 75 73 65 20 70 72 6f 78 79 2c 20 30 20 6d s use proxy, 0 m
e80f0 65 61 6e 73 20 0a 20 20 20 20 2a 2a 20 6e 65 76 eans . ** nev
e8100 65 72 20 75 73 65 20 70 72 6f 78 79 2c 20 4e 55 er use proxy, NU
e8110 4c 4c 20 6d 65 61 6e 73 20 75 73 65 20 70 72 6f LL means use pro
e8120 78 79 20 66 6f 72 20 6e 6f 6e 2d 6c 6f 63 61 6c xy for non-local
e8130 20 66 69 6c 65 73 20 6f 6e 6c 79 2e 20 20 2a 2f files only. */
e8140 0a 20 20 20 20 69 66 28 20 65 6e 76 66 6f 72 63 . if( envforc
e8150 65 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 e!=NULL ){.
e8160 20 75 73 65 50 72 6f 78 79 20 3d 20 61 74 6f 69 useProxy = atoi
e8170 28 65 6e 76 66 6f 72 63 65 29 3e 30 3b 0a 20 20 (envforce)>0;.
e8180 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 }else{. s
e8190 74 72 75 63 74 20 73 74 61 74 66 73 20 66 73 49 truct statfs fsI
e81a0 6e 66 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 73 nfo;. if( s
e81b0 74 61 74 66 73 28 7a 50 61 74 68 2c 20 26 66 73 tatfs(zPath, &fs
e81c0 49 6e 66 6f 29 20 3d 3d 20 2d 31 20 29 7b 0a 20 Info) == -1 ){.
e81d0 20 20 20 20 20 20 20 2f 2a 20 49 6e 20 74 68 65 /* In the
e81e0 6f 72 79 2c 20 74 68 65 20 63 6c 6f 73 65 28 66 ory, the close(f
e81f0 64 29 20 63 61 6c 6c 20 69 73 20 73 75 62 2d 6f d) call is sub-o
e8200 70 74 69 6d 61 6c 2e 20 49 66 20 74 68 65 20 66 ptimal. If the f
e8210 69 6c 65 20 6f 70 65 6e 65 64 0a 20 20 20 20 20 ile opened.
e8220 20 20 20 2a 2a 20 77 69 74 68 20 66 64 20 69 73 ** with fd is
e8230 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 a database file
e8240 2c 20 61 6e 64 20 74 68 65 72 65 20 61 72 65 20 , and there are
e8250 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e other connection
e8260 73 20 6f 70 65 6e 0a 20 20 20 20 20 20 20 20 2a s open. *
e8270 2a 20 6f 6e 20 74 68 61 74 20 66 69 6c 65 20 74 * on that file t
e8280 68 61 74 20 61 72 65 20 63 75 72 72 65 6e 74 6c hat are currentl
e8290 79 20 68 6f 6c 64 69 6e 67 20 61 64 76 69 73 6f y holding adviso
e82a0 72 79 20 6c 6f 63 6b 73 20 6f 6e 20 69 74 2c 0a ry locks on it,.
e82b0 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 ** then
e82c0 74 68 65 20 63 61 6c 6c 20 74 6f 20 63 6c 6f 73 the call to clos
e82d0 65 28 29 20 77 69 6c 6c 20 63 61 6e 63 65 6c 20 e() will cancel
e82e0 74 68 6f 73 65 20 6c 6f 63 6b 73 2e 20 49 6e 20 those locks. In
e82f0 70 72 61 63 74 69 63 65 2c 0a 20 20 20 20 20 20 practice,.
e8300 20 20 2a 2a 20 77 65 27 72 65 20 61 73 73 75 6d ** we're assum
e8310 69 6e 67 20 74 68 61 74 20 73 74 61 74 66 73 28 ing that statfs(
e8320 29 20 64 6f 65 73 6e 27 74 20 66 61 69 6c 20 76 ) doesn't fail v
e8330 65 72 79 20 6f 66 74 65 6e 2e 20 41 74 20 6c 65 ery often. At le
e8340 61 73 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e ast. ** n
e8350 6f 74 20 77 68 69 6c 65 20 6f 74 68 65 72 20 66 ot while other f
e8360 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 ile descriptors
e8370 6f 70 65 6e 65 64 20 62 79 20 74 68 65 20 73 61 opened by the sa
e8380 6d 65 20 70 72 6f 63 65 73 73 20 6f 6e 0a 20 20 me process on.
e8390 20 20 20 20 20 20 2a 2a 20 74 68 65 20 73 61 6d ** the sam
e83a0 65 20 66 69 6c 65 20 61 72 65 20 77 6f 72 6b 69 e file are worki
e83b0 6e 67 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 ng. */.
e83c0 70 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 p->lastErrno = e
e83d0 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 69 66 rrno;. if
e83e0 28 20 64 69 72 66 64 3e 3d 30 20 29 7b 0a 20 20 ( dirfd>=0 ){.
e83f0 20 20 20 20 20 20 20 20 63 6c 6f 73 65 28 64 69 close(di
e8400 72 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c rfd); /* silentl
e8410 79 20 6c 65 61 6b 20 69 66 20 66 61 69 6c 2c 20 y leak if fail,
e8420 69 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 in error */.
e8430 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63 6c }. cl
e8440 6f 73 65 28 66 64 29 3b 20 2f 2a 20 73 69 6c 65 ose(fd); /* sile
e8450 6e 74 6c 79 20 6c 65 61 6b 20 69 66 20 66 61 69 ntly leak if fai
e8460 6c 2c 20 69 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 l, in error */.
e8470 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
e8480 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 53 3b TE_IOERR_ACCESS;
e8490 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 . goto op
e84a0 65 6e 5f 66 69 6e 69 73 68 65 64 3b 0a 20 20 20 en_finished;.
e84b0 20 20 20 7d 0a 20 20 20 20 20 20 75 73 65 50 72 }. usePr
e84c0 6f 78 79 20 3d 20 21 28 66 73 49 6e 66 6f 2e 66 oxy = !(fsInfo.f
e84d0 5f 66 6c 61 67 73 26 4d 4e 54 5f 4c 4f 43 41 4c _flags&MNT_LOCAL
e84e0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 );. }. if(
e84f0 20 75 73 65 50 72 6f 78 79 20 29 7b 0a 20 20 20 useProxy ){.
e8500 20 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e 55 6e rc = fillInUn
e8510 69 78 46 69 6c 65 28 70 56 66 73 2c 20 66 64 2c ixFile(pVfs, fd,
e8520 20 64 69 72 66 64 2c 20 70 46 69 6c 65 2c 20 7a dirfd, pFile, z
e8530 50 61 74 68 2c 20 6e 6f 4c 6f 63 6b 2c 20 69 73 Path, noLock, is
e8540 44 65 6c 65 74 65 29 3b 0a 20 20 20 20 20 20 69 Delete);. i
e8550 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
e8560 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
e8570 20 70 72 6f 78 79 54 72 61 6e 73 66 6f 72 6d 55 proxyTransformU
e8580 6e 69 78 46 69 6c 65 28 28 75 6e 69 78 46 69 6c nixFile((unixFil
e8590 65 2a 29 70 46 69 6c 65 2c 20 22 3a 61 75 74 6f e*)pFile, ":auto
e85a0 3a 22 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 :");. }.
e85b0 20 20 20 67 6f 74 6f 20 6f 70 65 6e 5f 66 69 6e goto open_fin
e85c0 69 73 68 65 64 3b 0a 20 20 20 20 7d 0a 20 20 7d ished;. }. }
e85d0 0a 23 65 6e 64 69 66 0a 20 20 0a 20 20 72 63 20 .#endif. . rc
e85e0 3d 20 66 69 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 = fillInUnixFile
e85f0 28 70 56 66 73 2c 20 66 64 2c 20 64 69 72 66 64 (pVfs, fd, dirfd
e8600 2c 20 70 46 69 6c 65 2c 20 7a 50 61 74 68 2c 20 , pFile, zPath,
e8610 6e 6f 4c 6f 63 6b 2c 20 69 73 44 65 6c 65 74 65 noLock, isDelete
e8620 29 3b 0a 6f 70 65 6e 5f 66 69 6e 69 73 68 65 64 );.open_finished
e8630 3a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 :. if( rc!=SQLI
e8640 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c TE_OK ){. sql
e8650 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 70 55 6e ite3_free(p->pUn
e8660 75 73 65 64 29 3b 0a 20 20 7d 0a 20 20 72 65 74 used);. }. ret
e8670 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a urn rc;.}.../*.*
e8680 2a 20 44 65 6c 65 74 65 20 74 68 65 20 66 69 6c * Delete the fil
e8690 65 20 61 74 20 7a 50 61 74 68 2e 20 49 66 20 74 e at zPath. If t
e86a0 68 65 20 64 69 72 53 79 6e 63 20 61 72 67 75 6d he dirSync argum
e86b0 65 6e 74 20 69 73 20 74 72 75 65 2c 20 66 73 79 ent is true, fsy
e86c0 6e 63 28 29 0a 2a 2a 20 74 68 65 20 64 69 72 65 nc().** the dire
e86d0 63 74 6f 72 79 20 61 66 74 65 72 20 64 65 6c 65 ctory after dele
e86e0 74 69 6e 67 20 74 68 65 20 66 69 6c 65 2e 0a 2a ting the file..*
e86f0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 /.static int uni
e8700 78 44 65 6c 65 74 65 28 0a 20 20 73 71 6c 69 74 xDelete(. sqlit
e8710 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c e3_vfs *NotUsed,
e8720 20 20 20 20 20 2f 2a 20 56 46 53 20 63 6f 6e 74 /* VFS cont
e8730 61 69 6e 69 6e 67 20 74 68 69 73 20 61 73 20 74 aining this as t
e8740 68 65 20 78 44 65 6c 65 74 65 20 6d 65 74 68 6f he xDelete metho
e8750 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 d */. const cha
e8760 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 20 20 20 r *zPath,
e8770 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 /* Name of file
e8780 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20 2a to be deleted *
e8790 2f 0a 20 20 69 6e 74 20 64 69 72 53 79 6e 63 20 /. int dirSync
e87a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e87b0 20 49 66 20 74 72 75 65 2c 20 66 73 79 6e 63 28 If true, fsync(
e87c0 29 20 64 69 72 65 63 74 6f 72 79 20 61 66 74 65 ) directory afte
e87d0 72 20 64 65 6c 65 74 69 6e 67 20 66 69 6c 65 20 r deleting file
e87e0 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d */.){. int rc =
e87f0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 55 4e SQLITE_OK;. UN
e8800 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e USED_PARAMETER(N
e8810 6f 74 55 73 65 64 29 3b 0a 20 20 53 69 6d 75 6c otUsed);. Simul
e8820 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74 75 72 ateIOError(retur
e8830 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 n SQLITE_IOERR_D
e8840 45 4c 45 54 45 29 3b 0a 20 20 75 6e 6c 69 6e 6b ELETE);. unlink
e8850 28 7a 50 61 74 68 29 3b 0a 23 69 66 6e 64 65 66 (zPath);.#ifndef
e8860 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f SQLITE_DISABLE_
e8870 44 49 52 53 59 4e 43 0a 20 20 69 66 28 20 64 69 DIRSYNC. if( di
e8880 72 53 79 6e 63 20 29 7b 0a 20 20 20 20 69 6e 74 rSync ){. int
e8890 20 66 64 3b 0a 20 20 20 20 72 63 20 3d 20 6f 70 fd;. rc = op
e88a0 65 6e 44 69 72 65 63 74 6f 72 79 28 7a 50 61 74 enDirectory(zPat
e88b0 68 2c 20 26 66 64 29 3b 0a 20 20 20 20 69 66 28 h, &fd);. if(
e88c0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
e88d0 7b 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 {.#if OS_VXWORKS
e88e0 0a 20 20 20 20 20 20 69 66 28 20 66 73 79 6e 63 . if( fsync
e88f0 28 66 64 29 3d 3d 2d 31 20 29 0a 23 65 6c 73 65 (fd)==-1 ).#else
e8900 0a 20 20 20 20 20 20 69 66 28 20 66 73 79 6e 63 . if( fsync
e8910 28 66 64 29 20 29 0a 23 65 6e 64 69 66 0a 20 20 (fd) ).#endif.
e8920 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 72 63 {. rc
e8930 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f = SQLITE_IOERR_
e8940 44 49 52 5f 46 53 59 4e 43 3b 0a 20 20 20 20 20 DIR_FSYNC;.
e8950 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63 6c 6f }. if( clo
e8960 73 65 28 66 64 29 26 26 21 72 63 20 29 7b 0a 20 se(fd)&&!rc ){.
e8970 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
e8980 54 45 5f 49 4f 45 52 52 5f 44 49 52 5f 43 4c 4f TE_IOERR_DIR_CLO
e8990 53 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 SE;. }.
e89a0 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 }. }.#endif. r
e89b0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
e89c0 2a 2a 20 54 65 73 74 20 74 68 65 20 65 78 69 73 ** Test the exis
e89d0 74 61 6e 63 65 20 6f 66 20 6f 72 20 61 63 63 65 tance of or acce
e89e0 73 73 20 70 65 72 6d 69 73 73 69 6f 6e 73 20 6f ss permissions o
e89f0 66 20 66 69 6c 65 20 7a 50 61 74 68 2e 20 54 68 f file zPath. Th
e8a00 65 0a 2a 2a 20 74 65 73 74 20 70 65 72 66 6f 72 e.** test perfor
e8a10 6d 65 64 20 64 65 70 65 6e 64 73 20 6f 6e 20 74 med depends on t
e8a20 68 65 20 76 61 6c 75 65 20 6f 66 20 66 6c 61 67 he value of flag
e8a30 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 51 4c s:.**.** SQL
e8a40 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 ITE_ACCESS_EXIST
e8a50 53 3a 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 S: Return 1 if t
e8a60 68 65 20 66 69 6c 65 20 65 78 69 73 74 73 0a 2a he file exists.*
e8a70 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 41 43 43 * SQLITE_ACC
e8a80 45 53 53 5f 52 45 41 44 57 52 49 54 45 3a 20 52 ESS_READWRITE: R
e8a90 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 66 eturn 1 if the f
e8aa0 69 6c 65 20 69 73 20 72 65 61 64 20 61 6e 64 20 ile is read and
e8ab0 77 72 69 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 20 writable..**
e8ac0 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 SQLITE_ACCESS_R
e8ad0 45 41 44 4f 4e 4c 59 3a 20 52 65 74 75 72 6e 20 EADONLY: Return
e8ae0 31 20 69 66 20 74 68 65 20 66 69 6c 65 20 69 73 1 if the file is
e8af0 20 72 65 61 64 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a readable..**.**
e8b00 20 4f 74 68 65 72 77 69 73 65 20 72 65 74 75 72 Otherwise retur
e8b10 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 n 0..*/.static i
e8b20 6e 74 20 75 6e 69 78 41 63 63 65 73 73 28 0a 20 nt unixAccess(.
e8b30 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f sqlite3_vfs *No
e8b40 74 55 73 65 64 2c 20 20 20 2f 2a 20 54 68 65 20 tUsed, /* The
e8b50 56 46 53 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 VFS containing t
e8b60 68 69 73 20 78 41 63 63 65 73 73 20 6d 65 74 68 his xAccess meth
e8b70 6f 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 od */. const ch
e8b80 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 20 20 ar *zPath,
e8b90 2f 2a 20 50 61 74 68 20 6f 66 20 74 68 65 20 66 /* Path of the f
e8ba0 69 6c 65 20 74 6f 20 65 78 61 6d 69 6e 65 20 2a ile to examine *
e8bb0 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 /. int flags,
e8bc0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 /* W
e8bd0 68 61 74 20 64 6f 20 77 65 20 77 61 6e 74 20 74 hat do we want t
e8be0 6f 20 6c 65 61 72 6e 20 61 62 6f 75 74 20 74 68 o learn about th
e8bf0 65 20 7a 50 61 74 68 20 66 69 6c 65 3f 20 2a 2f e zPath file? */
e8c00 0a 20 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 20 . int *pResOut
e8c10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 /* Wr
e8c20 69 74 65 20 72 65 73 75 6c 74 20 62 6f 6f 6c 65 ite result boole
e8c30 61 6e 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 an here */.){.
e8c40 69 6e 74 20 61 6d 6f 64 65 20 3d 20 30 3b 0a 20 int amode = 0;.
e8c50 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
e8c60 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 53 69 R(NotUsed);. Si
e8c70 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 mulateIOError( r
e8c80 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
e8c90 52 52 5f 41 43 43 45 53 53 3b 20 29 3b 0a 20 20 RR_ACCESS; );.
e8ca0 73 77 69 74 63 68 28 20 66 6c 61 67 73 20 29 7b switch( flags ){
e8cb0 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
e8cc0 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 3a 0a _ACCESS_EXISTS:.
e8cd0 20 20 20 20 20 20 61 6d 6f 64 65 20 3d 20 46 5f amode = F_
e8ce0 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b OK;. break;
e8cf0 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
e8d00 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 54 _ACCESS_READWRIT
e8d10 45 3a 0a 20 20 20 20 20 20 61 6d 6f 64 65 20 3d E:. amode =
e8d20 20 57 5f 4f 4b 7c 52 5f 4f 4b 3b 0a 20 20 20 20 W_OK|R_OK;.
e8d30 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 break;. cas
e8d40 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f e SQLITE_ACCESS_
e8d50 52 45 41 44 3a 0a 20 20 20 20 20 20 61 6d 6f 64 READ:. amod
e8d60 65 20 3d 20 52 5f 4f 4b 3b 0a 20 20 20 20 20 20 e = R_OK;.
e8d70 62 72 65 61 6b 3b 0a 0a 20 20 20 20 64 65 66 61 break;.. defa
e8d80 75 6c 74 3a 0a 20 20 20 20 20 20 61 73 73 65 72 ult:. asser
e8d90 74 28 21 22 49 6e 76 61 6c 69 64 20 66 6c 61 67 t(!"Invalid flag
e8da0 73 20 61 72 67 75 6d 65 6e 74 22 29 3b 0a 20 20 s argument");.
e8db0 7d 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 28 }. *pResOut = (
e8dc0 61 63 63 65 73 73 28 7a 50 61 74 68 2c 20 61 6d access(zPath, am
e8dd0 6f 64 65 29 3d 3d 30 29 3b 0a 20 20 72 65 74 75 ode)==0);. retu
e8de0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
e8df0 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 61 20 72 ../*.** Turn a r
e8e00 65 6c 61 74 69 76 65 20 70 61 74 68 6e 61 6d 65 elative pathname
e8e10 20 69 6e 74 6f 20 61 20 66 75 6c 6c 20 70 61 74 into a full pat
e8e20 68 6e 61 6d 65 2e 20 54 68 65 20 72 65 6c 61 74 hname. The relat
e8e30 69 76 65 20 70 61 74 68 0a 2a 2a 20 69 73 20 73 ive path.** is s
e8e40 74 6f 72 65 64 20 61 73 20 61 20 6e 75 6c 2d 74 tored as a nul-t
e8e50 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67 erminated string
e8e60 20 69 6e 20 74 68 65 20 62 75 66 66 65 72 20 70 in the buffer p
e8e70 6f 69 6e 74 65 64 20 74 6f 20 62 79 0a 2a 2a 20 ointed to by.**
e8e80 7a 50 61 74 68 2e 20 0a 2a 2a 0a 2a 2a 20 7a 4f zPath. .**.** zO
e8e90 75 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 62 ut points to a b
e8ea0 75 66 66 65 72 20 6f 66 20 61 74 20 6c 65 61 73 uffer of at leas
e8eb0 74 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 6d 78 t sqlite3_vfs.mx
e8ec0 50 61 74 68 6e 61 6d 65 20 62 79 74 65 73 20 0a Pathname bytes .
e8ed0 2a 2a 20 28 69 6e 20 74 68 69 73 20 63 61 73 65 ** (in this case
e8ee0 2c 20 4d 41 58 5f 50 41 54 48 4e 41 4d 45 20 62 , MAX_PATHNAME b
e8ef0 79 74 65 73 29 2e 20 54 68 65 20 66 75 6c 6c 2d ytes). The full-
e8f00 70 61 74 68 20 69 73 20 77 72 69 74 74 65 6e 20 path is written
e8f10 74 6f 0a 2a 2a 20 74 68 69 73 20 62 75 66 66 65 to.** this buffe
e8f20 72 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 r before returni
e8f30 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ng..*/.static in
e8f40 74 20 75 6e 69 78 46 75 6c 6c 50 61 74 68 6e 61 t unixFullPathna
e8f50 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 me(. sqlite3_vf
e8f60 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 s *pVfs,
e8f70 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
e8f80 6f 20 76 66 73 20 6f 62 6a 65 63 74 20 2a 2f 0a o vfs object */.
e8f90 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 const char *zP
e8fa0 61 74 68 2c 20 20 20 20 20 20 20 20 20 20 20 20 ath,
e8fb0 2f 2a 20 50 6f 73 73 69 62 6c 79 20 72 65 6c 61 /* Possibly rela
e8fc0 74 69 76 65 20 69 6e 70 75 74 20 70 61 74 68 20 tive input path
e8fd0 2a 2f 0a 20 20 69 6e 74 20 6e 4f 75 74 2c 20 20 */. int nOut,
e8fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e8ff0 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 75 /* Size of ou
e9000 74 70 75 74 20 62 75 66 66 65 72 20 69 6e 20 62 tput buffer in b
e9010 79 74 65 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a ytes */. char *
e9020 7a 4f 75 74 20 20 20 20 20 20 20 20 20 20 20 20 zOut
e9030 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 /* Outpu
e9040 74 20 62 75 66 66 65 72 20 2a 2f 0a 29 7b 0a 0a t buffer */.){..
e9050 20 20 2f 2a 20 49 74 27 73 20 6f 64 64 20 74 6f /* It's odd to
e9060 20 73 69 6d 75 6c 61 74 65 20 61 6e 20 69 6f 2d simulate an io-
e9070 65 72 72 6f 72 20 68 65 72 65 2c 20 62 75 74 20 error here, but
e9080 72 65 61 6c 6c 79 20 74 68 69 73 20 69 73 20 6a really this is j
e9090 75 73 74 0a 20 20 2a 2a 20 75 73 69 6e 67 20 74 ust. ** using t
e90a0 68 65 20 69 6f 2d 65 72 72 6f 72 20 69 6e 66 72 he io-error infr
e90b0 61 73 74 72 75 63 74 75 72 65 20 74 6f 20 74 65 astructure to te
e90c0 73 74 20 74 68 61 74 20 53 51 4c 69 74 65 20 68 st that SQLite h
e90d0 61 6e 64 6c 65 73 20 74 68 69 73 0a 20 20 2a 2a andles this. **
e90e0 20 66 75 6e 63 74 69 6f 6e 20 66 61 69 6c 69 6e function failin
e90f0 67 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e g. This function
e9100 20 63 6f 75 6c 64 20 66 61 69 6c 20 69 66 2c 20 could fail if,
e9110 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 for example, the
e9120 0a 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 77 6f . ** current wo
e9130 72 6b 69 6e 67 20 64 69 72 65 63 74 6f 72 79 20 rking directory
e9140 68 61 73 20 62 65 65 6e 20 75 6e 6c 69 6e 6b 65 has been unlinke
e9150 64 2e 0a 20 20 2a 2f 0a 20 20 53 69 6d 75 6c 61 d.. */. Simula
e9160 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 teIOError( retur
e9170 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 29 n SQLITE_ERROR )
e9180 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 56 66 ;.. assert( pVf
e9190 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 3d 3d 4d s->mxPathname==M
e91a0 41 58 5f 50 41 54 48 4e 41 4d 45 20 29 3b 0a 20 AX_PATHNAME );.
e91b0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
e91c0 52 28 70 56 66 73 29 3b 0a 0a 20 20 7a 4f 75 74 R(pVfs);.. zOut
e91d0 5b 6e 4f 75 74 2d 31 5d 20 3d 20 27 5c 30 27 3b [nOut-1] = '\0';
e91e0 0a 20 20 69 66 28 20 7a 50 61 74 68 5b 30 5d 3d . if( zPath[0]=
e91f0 3d 27 2f 27 20 29 7b 0a 20 20 20 20 73 71 6c 69 ='/' ){. sqli
e9200 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 4f 75 te3_snprintf(nOu
e9210 74 2c 20 7a 4f 75 74 2c 20 22 25 73 22 2c 20 7a t, zOut, "%s", z
e9220 50 61 74 68 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a Path);. }else{.
e9230 20 20 20 20 69 6e 74 20 6e 43 77 64 3b 0a 20 20 int nCwd;.
e9240 20 20 69 66 28 20 67 65 74 63 77 64 28 7a 4f 75 if( getcwd(zOu
e9250 74 2c 20 6e 4f 75 74 2d 31 29 3d 3d 30 20 29 7b t, nOut-1)==0 ){
e9260 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
e9270 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 LITE_CANTOPEN;.
e9280 20 20 20 7d 0a 20 20 20 20 6e 43 77 64 20 3d 20 }. nCwd =
e9290 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 4f 75 74 (int)strlen(zOut
e92a0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 );. sqlite3_s
e92b0 6e 70 72 69 6e 74 66 28 6e 4f 75 74 2d 6e 43 77 nprintf(nOut-nCw
e92c0 64 2c 20 26 7a 4f 75 74 5b 6e 43 77 64 5d 2c 20 d, &zOut[nCwd],
e92d0 22 2f 25 73 22 2c 20 7a 50 61 74 68 29 3b 0a 20 "/%s", zPath);.
e92e0 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }. return SQLI
e92f0 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 TE_OK;.}...#ifnd
e9300 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c ef SQLITE_OMIT_L
e9310 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f 2a OAD_EXTENSION./*
e9320 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 73 20 66 .** Interfaces f
e9330 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20 73 68 61 or opening a sha
e9340 72 65 64 20 6c 69 62 72 61 72 79 2c 20 66 69 6e red library, fin
e9350 64 69 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e 74 ding entry point
e9360 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20 s.** within the
e9370 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 shared library,
e9380 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 and closing the
e9390 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2e 0a shared library..
e93a0 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 64 6c 66 */.#include <dlf
e93b0 63 6e 2e 68 3e 0a 73 74 61 74 69 63 20 76 6f 69 cn.h>.static voi
e93c0 64 20 2a 75 6e 69 78 44 6c 4f 70 65 6e 28 73 71 d *unixDlOpen(sq
e93d0 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 lite3_vfs *NotUs
e93e0 65 64 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ed, const char *
e93f0 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 55 4e zFilename){. UN
e9400 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e USED_PARAMETER(N
e9410 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 otUsed);. retur
e9420 6e 20 64 6c 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 n dlopen(zFilena
e9430 6d 65 2c 20 52 54 4c 44 5f 4e 4f 57 20 7c 20 52 me, RTLD_NOW | R
e9440 54 4c 44 5f 47 4c 4f 42 41 4c 29 3b 0a 7d 0a 0a TLD_GLOBAL);.}..
e9450 2f 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 61 6c /*.** SQLite cal
e9460 6c 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e ls this function
e9470 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74 immediately aft
e9480 65 72 20 61 20 63 61 6c 6c 20 74 6f 20 75 6e 69 er a call to uni
e9490 78 44 6c 53 79 6d 28 29 20 6f 72 0a 2a 2a 20 75 xDlSym() or.** u
e94a0 6e 69 78 44 6c 4f 70 65 6e 28 29 20 66 61 69 6c nixDlOpen() fail
e94b0 73 20 28 72 65 74 75 72 6e 73 20 61 20 6e 75 6c s (returns a nul
e94c0 6c 20 70 6f 69 6e 74 65 72 29 2e 20 49 66 20 61 l pointer). If a
e94d0 20 6d 6f 72 65 20 64 65 74 61 69 6c 65 64 20 65 more detailed e
e94e0 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20 rror.** message
e94f0 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 69 74 is available, it
e9500 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 7a is written to z
e9510 42 75 66 4f 75 74 2e 20 49 66 20 6e 6f 20 65 72 BufOut. If no er
e9520 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 ror message.** i
e9530 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 7a 42 75 s available, zBu
e9540 66 4f 75 74 20 69 73 20 6c 65 66 74 20 75 6e 6d fOut is left unm
e9550 6f 64 69 66 69 65 64 20 61 6e 64 20 53 51 4c 69 odified and SQLi
e9560 74 65 20 75 73 65 73 20 61 20 64 65 66 61 75 6c te uses a defaul
e9570 74 0a 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73 61 t.** error messa
e9580 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ge..*/.static vo
e9590 69 64 20 75 6e 69 78 44 6c 45 72 72 6f 72 28 73 id unixDlError(s
e95a0 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 qlite3_vfs *NotU
e95b0 73 65 64 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 sed, int nBuf, c
e95c0 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 20 har *zBufOut){.
e95d0 20 63 68 61 72 20 2a 7a 45 72 72 3b 0a 20 20 55 char *zErr;. U
e95e0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
e95f0 4e 6f 74 55 73 65 64 29 3b 0a 20 20 75 6e 69 78 NotUsed);. unix
e9600 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 EnterMutex();.
e9610 7a 45 72 72 20 3d 20 64 6c 65 72 72 6f 72 28 29 zErr = dlerror()
e9620 3b 0a 20 20 69 66 28 20 7a 45 72 72 20 29 7b 0a ;. if( zErr ){.
e9630 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
e9640 69 6e 74 66 28 6e 42 75 66 2c 20 7a 42 75 66 4f intf(nBuf, zBufO
e9650 75 74 2c 20 22 25 73 22 2c 20 7a 45 72 72 29 3b ut, "%s", zErr);
e9660 0a 20 20 7d 0a 20 20 75 6e 69 78 4c 65 61 76 65 . }. unixLeave
e9670 4d 75 74 65 78 28 29 3b 0a 7d 0a 73 74 61 74 69 Mutex();.}.stati
e9680 63 20 76 6f 69 64 20 28 2a 75 6e 69 78 44 6c 53 c void (*unixDlS
e9690 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a ym(sqlite3_vfs *
e96a0 4e 6f 74 55 73 65 64 2c 20 76 6f 69 64 20 2a 70 NotUsed, void *p
e96b0 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 7a 53 79 , const char*zSy
e96c0 6d 29 29 28 76 6f 69 64 29 7b 0a 20 20 2f 2a 20 m))(void){. /*
e96d0 0a 20 20 2a 2a 20 47 43 43 20 77 69 74 68 20 2d . ** GCC with -
e96e0 70 65 64 61 6e 74 69 63 2d 65 72 72 6f 72 73 20 pedantic-errors
e96f0 73 61 79 73 20 74 68 61 74 20 43 39 30 20 64 6f says that C90 do
e9700 65 73 20 6e 6f 74 20 61 6c 6c 6f 77 20 61 20 76 es not allow a v
e9710 6f 69 64 2a 20 74 6f 20 62 65 0a 20 20 2a 2a 20 oid* to be. **
e9720 63 61 73 74 20 69 6e 74 6f 20 61 20 70 6f 69 6e cast into a poin
e9730 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f ter to a functio
e9740 6e 2e 20 20 41 6e 64 20 79 65 74 20 74 68 65 20 n. And yet the
e9750 6c 69 62 72 61 72 79 20 64 6c 73 79 6d 28 29 20 library dlsym()
e9760 72 6f 75 74 69 6e 65 0a 20 20 2a 2a 20 72 65 74 routine. ** ret
e9770 75 72 6e 73 20 61 20 76 6f 69 64 2a 20 77 68 69 urns a void* whi
e9780 63 68 20 69 73 20 72 65 61 6c 6c 79 20 61 20 70 ch is really a p
e9790 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63 ointer to a func
e97a0 74 69 6f 6e 2e 20 20 53 6f 20 68 6f 77 20 64 6f tion. So how do
e97b0 20 77 65 0a 20 20 2a 2a 20 75 73 65 20 64 6c 73 we. ** use dls
e97c0 79 6d 28 29 20 77 69 74 68 20 2d 70 65 64 61 6e ym() with -pedan
e97d0 74 69 63 2d 65 72 72 6f 72 73 3f 0a 20 20 2a 2a tic-errors?. **
e97e0 0a 20 20 2a 2a 20 56 61 72 69 61 62 6c 65 20 78 . ** Variable x
e97f0 20 62 65 6c 6f 77 20 69 73 20 64 65 66 69 6e 65 below is define
e9800 64 20 74 6f 20 62 65 20 61 20 70 6f 69 6e 74 65 d to be a pointe
e9810 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 r to a function
e9820 74 61 6b 69 6e 67 0a 20 20 2a 2a 20 70 61 72 61 taking. ** para
e9830 6d 65 74 65 72 73 20 76 6f 69 64 2a 20 61 6e 64 meters void* and
e9840 20 63 6f 6e 73 74 20 63 68 61 72 2a 20 61 6e 64 const char* and
e9850 20 72 65 74 75 72 6e 69 6e 67 20 61 20 70 6f 69 returning a poi
e9860 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69 nter to a functi
e9870 6f 6e 2e 0a 20 20 2a 2a 20 57 65 20 69 6e 69 74 on.. ** We init
e9880 69 61 6c 69 7a 65 20 78 20 62 79 20 61 73 73 69 ialize x by assi
e9890 67 6e 69 6e 67 20 69 74 20 61 20 70 6f 69 6e 74 gning it a point
e98a0 65 72 20 74 6f 20 74 68 65 20 64 6c 73 79 6d 28 er to the dlsym(
e98b0 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2a ) function.. **
e98c0 20 28 54 68 61 74 20 61 73 73 69 67 6e 6d 65 6e (That assignmen
e98d0 74 20 72 65 71 75 69 72 65 73 20 61 20 63 61 73 t requires a cas
e98e0 74 2e 29 20 20 54 68 65 6e 20 77 65 20 63 61 6c t.) Then we cal
e98f0 6c 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 l the function t
e9900 68 61 74 0a 20 20 2a 2a 20 78 20 70 6f 69 6e 74 hat. ** x point
e9910 73 20 74 6f 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a s to. . **. *
e9920 2a 20 54 68 69 73 20 77 6f 72 6b 2d 61 72 6f 75 * This work-arou
e9930 6e 64 20 69 73 20 75 6e 6c 69 6b 65 6c 79 20 74 nd is unlikely t
e9940 6f 20 77 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 o work correctly
e9950 20 6f 6e 20 61 6e 79 20 73 79 73 74 65 6d 20 77 on any system w
e9960 68 65 72 65 0a 20 20 2a 2a 20 79 6f 75 20 72 65 here. ** you re
e9970 61 6c 6c 79 20 63 61 6e 6e 6f 74 20 63 61 73 74 ally cannot cast
e9980 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e a function poin
e9990 74 65 72 20 69 6e 74 6f 20 76 6f 69 64 2a 2e 20 ter into void*.
e99a0 20 42 75 74 20 74 68 65 6e 2c 20 6f 6e 20 74 68 But then, on th
e99b0 65 0a 20 20 2a 2a 20 6f 74 68 65 72 20 68 61 6e e. ** other han
e99c0 64 2c 20 64 6c 73 79 6d 28 29 20 77 69 6c 6c 20 d, dlsym() will
e99d0 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20 73 75 63 68 not work on such
e99e0 20 61 20 73 79 73 74 65 6d 20 65 69 74 68 65 72 a system either
e99f0 2c 20 73 6f 20 77 65 20 68 61 76 65 0a 20 20 2a , so we have. *
e9a00 2a 20 6e 6f 74 20 72 65 61 6c 6c 79 20 6c 6f 73 * not really los
e9a10 74 20 61 6e 79 74 68 69 6e 67 2e 0a 20 20 2a 2f t anything.. */
e9a20 0a 20 20 76 6f 69 64 20 28 2a 28 2a 78 29 28 76 . void (*(*x)(v
e9a30 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a oid*,const char*
e9a40 29 29 28 76 6f 69 64 29 3b 0a 20 20 55 4e 55 53 ))(void);. UNUS
e9a50 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 ED_PARAMETER(Not
e9a60 55 73 65 64 29 3b 0a 20 20 78 20 3d 20 28 76 6f Used);. x = (vo
e9a70 69 64 28 2a 28 2a 29 28 76 6f 69 64 2a 2c 63 6f id(*(*)(void*,co
e9a80 6e 73 74 20 63 68 61 72 2a 29 29 28 76 6f 69 64 nst char*))(void
e9a90 29 29 64 6c 73 79 6d 3b 0a 20 20 72 65 74 75 72 ))dlsym;. retur
e9aa0 6e 20 28 2a 78 29 28 70 2c 20 7a 53 79 6d 29 3b n (*x)(p, zSym);
e9ab0 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 .}.static void u
e9ac0 6e 69 78 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 nixDlClose(sqlit
e9ad0 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c e3_vfs *NotUsed,
e9ae0 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65 29 7b void *pHandle){
e9af0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
e9b00 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 TER(NotUsed);.
e9b10 64 6c 63 6c 6f 73 65 28 70 48 61 6e 64 6c 65 29 dlclose(pHandle)
e9b20 3b 0a 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 ;.}.#else /* if
e9b30 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 SQLITE_OMIT_LOAD
e9b40 5f 45 58 54 45 4e 53 49 4f 4e 20 69 73 20 64 65 _EXTENSION is de
e9b50 66 69 6e 65 64 3a 20 2a 2f 0a 20 20 23 64 65 66 fined: */. #def
e9b60 69 6e 65 20 75 6e 69 78 44 6c 4f 70 65 6e 20 20 ine unixDlOpen
e9b70 30 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69 78 0. #define unix
e9b80 44 6c 45 72 72 6f 72 20 30 0a 20 20 23 64 65 66 DlError 0. #def
e9b90 69 6e 65 20 75 6e 69 78 44 6c 53 79 6d 20 20 20 ine unixDlSym
e9ba0 30 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69 78 0. #define unix
e9bb0 44 6c 43 6c 6f 73 65 20 30 0a 23 65 6e 64 69 66 DlClose 0.#endif
e9bc0 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 6e 42 ../*.** Write nB
e9bd0 75 66 20 62 79 74 65 73 20 6f 66 20 72 61 6e 64 uf bytes of rand
e9be0 6f 6d 20 64 61 74 61 20 74 6f 20 74 68 65 20 73 om data to the s
e9bf0 75 70 70 6c 69 65 64 20 62 75 66 66 65 72 20 7a upplied buffer z
e9c00 42 75 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 Buf..*/.static i
e9c10 6e 74 20 75 6e 69 78 52 61 6e 64 6f 6d 6e 65 73 nt unixRandomnes
e9c20 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e s(sqlite3_vfs *N
e9c30 6f 74 55 73 65 64 2c 20 69 6e 74 20 6e 42 75 66 otUsed, int nBuf
e9c40 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 , char *zBuf){.
e9c50 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
e9c60 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 61 73 R(NotUsed);. as
e9c70 73 65 72 74 28 28 73 69 7a 65 5f 74 29 6e 42 75 sert((size_t)nBu
e9c80 66 3e 3d 28 73 69 7a 65 6f 66 28 74 69 6d 65 5f f>=(sizeof(time_
e9c90 74 29 2b 73 69 7a 65 6f 66 28 69 6e 74 29 29 29 t)+sizeof(int)))
e9ca0 3b 0a 0a 20 20 2f 2a 20 57 65 20 68 61 76 65 20 ;.. /* We have
e9cb0 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 7a 42 to initialize zB
e9cc0 75 66 20 74 6f 20 70 72 65 76 65 6e 74 20 76 61 uf to prevent va
e9cd0 6c 67 72 69 6e 64 20 66 72 6f 6d 20 72 65 70 6f lgrind from repo
e9ce0 72 74 69 6e 67 0a 20 20 2a 2a 20 65 72 72 6f 72 rting. ** error
e9cf0 73 2e 20 20 54 68 65 20 72 65 70 6f 72 74 73 20 s. The reports
e9d00 69 73 73 75 65 64 20 62 79 20 76 61 6c 67 72 69 issued by valgri
e9d10 6e 64 20 61 72 65 20 69 6e 63 6f 72 72 65 63 74 nd are incorrect
e9d20 20 2d 20 77 65 20 77 6f 75 6c 64 0a 20 20 2a 2a - we would. **
e9d30 20 70 72 65 66 65 72 20 74 68 61 74 20 74 68 65 prefer that the
e9d40 20 72 61 6e 64 6f 6d 6e 65 73 73 20 62 65 20 69 randomness be i
e9d50 6e 63 72 65 61 73 65 64 20 62 79 20 6d 61 6b 69 ncreased by maki
e9d60 6e 67 20 75 73 65 20 6f 66 20 74 68 65 0a 20 20 ng use of the.
e9d70 2a 2a 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 ** uninitialized
e9d80 20 73 70 61 63 65 20 69 6e 20 7a 42 75 66 20 2d space in zBuf -
e9d90 20 62 75 74 20 76 61 6c 67 72 69 6e 64 20 65 72 but valgrind er
e9da0 72 6f 72 73 20 74 65 6e 64 20 74 6f 20 77 6f 72 rors tend to wor
e9db0 72 79 0a 20 20 2a 2a 20 73 6f 6d 65 20 75 73 65 ry. ** some use
e9dc0 72 73 2e 20 20 52 61 74 68 65 72 20 74 68 61 6e rs. Rather than
e9dd0 20 61 72 67 75 65 2c 20 69 74 20 73 65 65 6d 73 argue, it seems
e9de0 20 65 61 73 69 65 72 20 6a 75 73 74 20 74 6f 20 easier just to
e9df0 69 6e 69 74 69 61 6c 69 7a 65 0a 20 20 2a 2a 20 initialize. **
e9e00 74 68 65 20 77 68 6f 6c 65 20 61 72 72 61 79 20 the whole array
e9e10 61 6e 64 20 73 69 6c 65 6e 63 65 20 76 61 6c 67 and silence valg
e9e20 72 69 6e 64 2c 20 65 76 65 6e 20 69 66 20 74 68 rind, even if th
e9e30 61 74 20 6d 65 61 6e 73 20 6c 65 73 73 20 72 61 at means less ra
e9e40 6e 64 6f 6d 6e 65 73 73 0a 20 20 2a 2a 20 69 6e ndomness. ** in
e9e50 20 74 68 65 20 72 61 6e 64 6f 6d 20 73 65 65 64 the random seed
e9e60 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 65 6e .. **. ** When
e9e70 20 74 65 73 74 69 6e 67 2c 20 69 6e 69 74 69 61 testing, initia
e9e80 6c 69 7a 69 6e 67 20 7a 42 75 66 5b 5d 20 74 6f lizing zBuf[] to
e9e90 20 7a 65 72 6f 20 69 73 20 61 6c 6c 20 77 65 20 zero is all we
e9ea0 64 6f 2e 20 20 54 68 61 74 20 6d 65 61 6e 73 0a do. That means.
e9eb0 20 20 2a 2a 20 74 68 61 74 20 77 65 20 61 6c 77 ** that we alw
e9ec0 61 79 73 20 75 73 65 20 74 68 65 20 73 61 6d 65 ays use the same
e9ed0 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 73 random number s
e9ee0 65 71 75 65 6e 63 65 2e 20 20 54 68 69 73 20 6d equence. This m
e9ef0 61 6b 65 73 20 74 68 65 0a 20 20 2a 2a 20 74 65 akes the. ** te
e9f00 73 74 73 20 72 65 70 65 61 74 61 62 6c 65 2e 0a sts repeatable..
e9f10 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 7a 42 */. memset(zB
e9f20 75 66 2c 20 30 2c 20 6e 42 75 66 29 3b 0a 23 69 uf, 0, nBuf);.#i
e9f30 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 f !defined(SQLIT
e9f40 45 5f 54 45 53 54 29 0a 20 20 7b 0a 20 20 20 20 E_TEST). {.
e9f50 69 6e 74 20 70 69 64 2c 20 66 64 3b 0a 20 20 20 int pid, fd;.
e9f60 20 66 64 20 3d 20 6f 70 65 6e 28 22 2f 64 65 76 fd = open("/dev
e9f70 2f 75 72 61 6e 64 6f 6d 22 2c 20 4f 5f 52 44 4f /urandom", O_RDO
e9f80 4e 4c 59 29 3b 0a 20 20 20 20 69 66 28 20 66 64 NLY);. if( fd
e9f90 3c 30 20 29 7b 0a 20 20 20 20 20 20 74 69 6d 65 <0 ){. time
e9fa0 5f 74 20 74 3b 0a 20 20 20 20 20 20 74 69 6d 65 _t t;. time
e9fb0 28 26 74 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 (&t);. memc
e9fc0 70 79 28 7a 42 75 66 2c 20 26 74 2c 20 73 69 7a py(zBuf, &t, siz
e9fd0 65 6f 66 28 74 29 29 3b 0a 20 20 20 20 20 20 70 eof(t));. p
e9fe0 69 64 20 3d 20 67 65 74 70 69 64 28 29 3b 0a 20 id = getpid();.
e9ff0 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 memcpy(&zBu
ea000 66 5b 73 69 7a 65 6f 66 28 74 29 5d 2c 20 26 70 f[sizeof(t)], &p
ea010 69 64 2c 20 73 69 7a 65 6f 66 28 70 69 64 29 29 id, sizeof(pid))
ea020 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
ea030 73 69 7a 65 6f 66 28 74 29 2b 73 69 7a 65 6f 66 sizeof(t)+sizeof
ea040 28 70 69 64 29 3c 3d 28 73 69 7a 65 5f 74 29 6e (pid)<=(size_t)n
ea050 42 75 66 20 29 3b 0a 20 20 20 20 20 20 6e 42 75 Buf );. nBu
ea060 66 20 3d 20 73 69 7a 65 6f 66 28 74 29 20 2b 20 f = sizeof(t) +
ea070 73 69 7a 65 6f 66 28 70 69 64 29 3b 0a 20 20 20 sizeof(pid);.
ea080 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 42 }else{. nB
ea090 75 66 20 3d 20 72 65 61 64 28 66 64 2c 20 7a 42 uf = read(fd, zB
ea0a0 75 66 2c 20 6e 42 75 66 29 3b 0a 20 20 20 20 20 uf, nBuf);.
ea0b0 20 63 6c 6f 73 65 28 66 64 29 3b 0a 20 20 20 20 close(fd);.
ea0c0 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 }. }.#endif. r
ea0d0 65 74 75 72 6e 20 6e 42 75 66 3b 0a 7d 0a 0a 0a eturn nBuf;.}...
ea0e0 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 6f 72 20 /*.** Sleep for
ea0f0 61 20 6c 69 74 74 6c 65 20 77 68 69 6c 65 2e 20 a little while.
ea100 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d 6f 75 Return the amou
ea110 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c 65 70 74 nt of time slept
ea120 2e 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e ..** The argumen
ea130 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 t is the number
ea140 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 of microseconds
ea150 77 65 20 77 61 6e 74 20 74 6f 20 73 6c 65 65 70 we want to sleep
ea160 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 ..** The return
ea170 76 61 6c 75 65 20 69 73 20 74 68 65 20 6e 75 6d value is the num
ea180 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f ber of microseco
ea190 6e 64 73 20 6f 66 20 73 6c 65 65 70 20 61 63 74 nds of sleep act
ea1a0 75 61 6c 6c 79 0a 2a 2a 20 72 65 71 75 65 73 74 ually.** request
ea1b0 65 64 20 66 72 6f 6d 20 74 68 65 20 75 6e 64 65 ed from the unde
ea1c0 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 rlying operating
ea1d0 20 73 79 73 74 65 6d 2c 20 61 20 6e 75 6d 62 65 system, a numbe
ea1e0 72 20 77 68 69 63 68 0a 2a 2a 20 6d 69 67 68 74 r which.** might
ea1f0 20 62 65 20 67 72 65 61 74 65 72 20 74 68 61 6e be greater than
ea200 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 or equal to the
ea210 20 61 72 67 75 6d 65 6e 74 2c 20 62 75 74 20 6e argument, but n
ea220 6f 74 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 ot less.** than
ea230 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f the argument..*/
ea240 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 .static int unix
ea250 53 6c 65 65 70 28 73 71 6c 69 74 65 33 5f 76 66 Sleep(sqlite3_vf
ea260 73 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 s *NotUsed, int
ea270 6d 69 63 72 6f 73 65 63 6f 6e 64 73 29 7b 0a 23 microseconds){.#
ea280 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 if OS_VXWORKS.
ea290 73 74 72 75 63 74 20 74 69 6d 65 73 70 65 63 20 struct timespec
ea2a0 73 70 3b 0a 0a 20 20 73 70 2e 74 76 5f 73 65 63 sp;.. sp.tv_sec
ea2b0 20 3d 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 = microseconds
ea2c0 2f 20 31 30 30 30 30 30 30 3b 0a 20 20 73 70 2e / 1000000;. sp.
ea2d0 74 76 5f 6e 73 65 63 20 3d 20 28 6d 69 63 72 6f tv_nsec = (micro
ea2e0 73 65 63 6f 6e 64 73 20 25 20 31 30 30 30 30 30 seconds % 100000
ea2f0 30 29 20 2a 20 31 30 30 30 3b 0a 20 20 6e 61 6e 0) * 1000;. nan
ea300 6f 73 6c 65 65 70 28 26 73 70 2c 20 4e 55 4c 4c osleep(&sp, NULL
ea310 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 );. UNUSED_PARA
ea320 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a METER(NotUsed);.
ea330 20 20 72 65 74 75 72 6e 20 6d 69 63 72 6f 73 65 return microse
ea340 63 6f 6e 64 73 3b 0a 23 65 6c 69 66 20 64 65 66 conds;.#elif def
ea350 69 6e 65 64 28 48 41 56 45 5f 55 53 4c 45 45 50 ined(HAVE_USLEEP
ea360 29 20 26 26 20 48 41 56 45 5f 55 53 4c 45 45 50 ) && HAVE_USLEEP
ea370 0a 20 20 75 73 6c 65 65 70 28 6d 69 63 72 6f 73 . usleep(micros
ea380 65 63 6f 6e 64 73 29 3b 0a 20 20 55 4e 55 53 45 econds);. UNUSE
ea390 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 D_PARAMETER(NotU
ea3a0 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 6d sed);. return m
ea3b0 69 63 72 6f 73 65 63 6f 6e 64 73 3b 0a 23 65 6c icroseconds;.#el
ea3c0 73 65 0a 20 20 69 6e 74 20 73 65 63 6f 6e 64 73 se. int seconds
ea3d0 20 3d 20 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73 = (microseconds
ea3e0 2b 39 39 39 39 39 39 29 2f 31 30 30 30 30 30 30 +999999)/1000000
ea3f0 3b 0a 20 20 73 6c 65 65 70 28 73 65 63 6f 6e 64 ;. sleep(second
ea400 73 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 s);. UNUSED_PAR
ea410 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
ea420 0a 20 20 72 65 74 75 72 6e 20 73 65 63 6f 6e 64 . return second
ea430 73 2a 31 30 30 30 30 30 30 3b 0a 23 65 6e 64 69 s*1000000;.#endi
ea440 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 f.}../*.** The f
ea450 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c ollowing variabl
ea460 65 2c 20 69 66 20 73 65 74 20 74 6f 20 61 20 6e e, if set to a n
ea470 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2c 20 69 on-zero value, i
ea480 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 s interpreted as
ea490 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f .** the number o
ea4a0 66 20 73 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 f seconds since
ea4b0 31 39 37 30 20 61 6e 64 20 69 73 20 75 73 65 64 1970 and is used
ea4c0 20 74 6f 20 73 65 74 20 74 68 65 20 72 65 73 75 to set the resu
ea4d0 6c 74 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 65 33 lt of.** sqlite3
ea4e0 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 29 20 OsCurrentTime()
ea4f0 64 75 72 69 6e 67 20 74 65 73 74 69 6e 67 2e 0a during testing..
ea500 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
ea510 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 _TEST.SQLITE_API
ea520 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 75 72 int sqlite3_cur
ea530 72 65 6e 74 5f 74 69 6d 65 20 3d 20 30 3b 20 20 rent_time = 0;
ea540 2f 2a 20 46 61 6b 65 20 73 79 73 74 65 6d 20 74 /* Fake system t
ea550 69 6d 65 20 69 6e 20 73 65 63 6f 6e 64 73 20 73 ime in seconds s
ea560 69 6e 63 65 20 31 39 37 30 2e 20 2a 2f 0a 23 65 ince 1970. */.#e
ea570 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 ndif../*.** Find
ea580 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 69 6d the current tim
ea590 65 20 28 69 6e 20 55 6e 69 76 65 72 73 61 6c 20 e (in Universal
ea5a0 43 6f 6f 72 64 69 6e 61 74 65 64 20 54 69 6d 65 Coordinated Time
ea5b0 29 2e 20 20 57 72 69 74 65 20 74 68 65 0a 2a 2a ). Write the.**
ea5c0 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 61 6e current time an
ea5d0 64 20 64 61 74 65 20 61 73 20 61 20 4a 75 6c 69 d date as a Juli
ea5e0 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 20 69 6e an Day number in
ea5f0 74 6f 20 2a 70 72 4e 6f 77 20 61 6e 64 0a 2a 2a to *prNow and.**
ea600 20 72 65 74 75 72 6e 20 30 2e 20 20 52 65 74 75 return 0. Retu
ea610 72 6e 20 31 20 69 66 20 74 68 65 20 74 69 6d 65 rn 1 if the time
ea620 20 61 6e 64 20 64 61 74 65 20 63 61 6e 6e 6f 74 and date cannot
ea630 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 73 74 be found..*/.st
ea640 61 74 69 63 20 69 6e 74 20 75 6e 69 78 43 75 72 atic int unixCur
ea650 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74 65 33 rentTime(sqlite3
ea660 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 64 _vfs *NotUsed, d
ea670 6f 75 62 6c 65 20 2a 70 72 4e 6f 77 29 7b 0a 23 ouble *prNow){.#
ea680 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 if defined(SQLIT
ea690 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f E_OMIT_FLOATING_
ea6a0 50 4f 49 4e 54 29 0a 20 20 74 69 6d 65 5f 74 20 POINT). time_t
ea6b0 74 3b 0a 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 t;. time(&t);.
ea6c0 20 2a 70 72 4e 6f 77 20 3d 20 28 28 28 73 71 6c *prNow = (((sql
ea6d0 69 74 65 33 5f 69 6e 74 36 34 29 74 29 2f 38 36 ite3_int64)t)/86
ea6e0 34 30 20 2b 20 32 34 34 30 35 38 37 35 29 2f 31 40 + 24405875)/1
ea6f0 30 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64 0;.#elif defined
ea700 28 4e 4f 5f 47 45 54 54 4f 44 29 0a 20 20 74 69 (NO_GETTOD). ti
ea710 6d 65 5f 74 20 74 3b 0a 20 20 74 69 6d 65 28 26 me_t t;. time(&
ea720 74 29 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 74 t);. *prNow = t
ea730 2f 38 36 34 30 30 2e 30 20 2b 20 32 34 34 30 35 /86400.0 + 24405
ea740 38 37 2e 35 3b 0a 23 65 6c 69 66 20 4f 53 5f 56 87.5;.#elif OS_V
ea750 58 57 4f 52 4b 53 0a 20 20 73 74 72 75 63 74 20 XWORKS. struct
ea760 74 69 6d 65 73 70 65 63 20 73 4e 6f 77 3b 0a 20 timespec sNow;.
ea770 20 63 6c 6f 63 6b 5f 67 65 74 74 69 6d 65 28 43 clock_gettime(C
ea780 4c 4f 43 4b 5f 52 45 41 4c 54 49 4d 45 2c 20 26 LOCK_REALTIME, &
ea790 73 4e 6f 77 29 3b 0a 20 20 2a 70 72 4e 6f 77 20 sNow);. *prNow
ea7a0 3d 20 32 34 34 30 35 38 37 2e 35 20 2b 20 73 4e = 2440587.5 + sN
ea7b0 6f 77 2e 74 76 5f 73 65 63 2f 38 36 34 30 30 2e ow.tv_sec/86400.
ea7c0 30 20 2b 20 73 4e 6f 77 2e 74 76 5f 6e 73 65 63 0 + sNow.tv_nsec
ea7d0 2f 38 36 34 30 30 30 30 30 30 30 30 30 30 30 2e /86400000000000.
ea7e0 30 3b 0a 23 65 6c 73 65 0a 20 20 73 74 72 75 63 0;.#else. struc
ea7f0 74 20 74 69 6d 65 76 61 6c 20 73 4e 6f 77 3b 0a t timeval sNow;.
ea800 20 20 67 65 74 74 69 6d 65 6f 66 64 61 79 28 26 gettimeofday(&
ea810 73 4e 6f 77 2c 20 30 29 3b 0a 20 20 2a 70 72 4e sNow, 0);. *prN
ea820 6f 77 20 3d 20 32 34 34 30 35 38 37 2e 35 20 2b ow = 2440587.5 +
ea830 20 73 4e 6f 77 2e 74 76 5f 73 65 63 2f 38 36 34 sNow.tv_sec/864
ea840 30 30 2e 30 20 2b 20 73 4e 6f 77 2e 74 76 5f 75 00.0 + sNow.tv_u
ea850 73 65 63 2f 38 36 34 30 30 30 30 30 30 30 30 2e sec/86400000000.
ea860 30 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 0;.#endif..#ifde
ea870 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 f SQLITE_TEST.
ea880 69 66 28 20 73 71 6c 69 74 65 33 5f 63 75 72 72 if( sqlite3_curr
ea890 65 6e 74 5f 74 69 6d 65 20 29 7b 0a 20 20 20 20 ent_time ){.
ea8a0 2a 70 72 4e 6f 77 20 3d 20 73 71 6c 69 74 65 33 *prNow = sqlite3
ea8b0 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 2f 38 36 _current_time/86
ea8c0 34 30 30 2e 30 20 2b 20 32 34 34 30 35 38 37 2e 400.0 + 2440587.
ea8d0 35 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 5;. }.#endif.
ea8e0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
ea8f0 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 (NotUsed);. ret
ea900 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn 0;.}../*.**
ea910 57 65 20 61 64 64 65 64 20 74 68 65 20 78 47 65 We added the xGe
ea920 74 4c 61 73 74 45 72 72 6f 72 28 29 20 6d 65 74 tLastError() met
ea930 68 6f 64 20 77 69 74 68 20 74 68 65 20 69 6e 74 hod with the int
ea940 65 6e 74 69 6f 6e 20 6f 66 20 70 72 6f 76 69 64 ention of provid
ea950 69 6e 67 0a 2a 2a 20 62 65 74 74 65 72 20 6c 6f ing.** better lo
ea960 77 2d 6c 65 76 65 6c 20 65 72 72 6f 72 20 6d 65 w-level error me
ea970 73 73 61 67 65 73 20 77 68 65 6e 20 6f 70 65 72 ssages when oper
ea980 61 74 69 6e 67 2d 73 79 73 74 65 6d 20 70 72 6f ating-system pro
ea990 62 6c 65 6d 73 20 63 6f 6d 65 20 75 70 0a 2a 2a blems come up.**
ea9a0 20 64 75 72 69 6e 67 20 53 51 4c 69 74 65 20 6f during SQLite o
ea9b0 70 65 72 61 74 69 6f 6e 2e 20 20 42 75 74 20 73 peration. But s
ea9c0 6f 20 66 61 72 2c 20 6e 6f 6e 65 20 6f 66 20 74 o far, none of t
ea9d0 68 61 74 20 68 61 73 20 62 65 65 6e 20 69 6d 70 hat has been imp
ea9e0 6c 65 6d 65 6e 74 65 64 0a 2a 2a 20 69 6e 20 74 lemented.** in t
ea9f0 68 65 20 63 6f 72 65 2e 20 20 53 6f 20 74 68 69 he core. So thi
eaa00 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 65 76 s routine is nev
eaa10 65 72 20 63 61 6c 6c 65 64 2e 20 20 46 6f 72 20 er called. For
eaa20 6e 6f 77 2c 20 69 74 20 69 73 20 6d 65 72 65 6c now, it is merel
eaa30 79 0a 2a 2a 20 61 20 70 6c 61 63 65 2d 68 6f 6c y.** a place-hol
eaa40 64 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 der..*/.static i
eaa50 6e 74 20 75 6e 69 78 47 65 74 4c 61 73 74 45 72 nt unixGetLastEr
eaa60 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 ror(sqlite3_vfs
eaa70 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 4e 6f *NotUsed, int No
eaa80 74 55 73 65 64 32 2c 20 63 68 61 72 20 2a 4e 6f tUsed2, char *No
eaa90 74 55 73 65 64 33 29 7b 0a 20 20 55 4e 55 53 45 tUsed3){. UNUSE
eaaa0 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 D_PARAMETER(NotU
eaab0 73 65 64 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 sed);. UNUSED_P
eaac0 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 ARAMETER(NotUsed
eaad0 32 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 2);. UNUSED_PAR
eaae0 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 33 29 AMETER(NotUsed3)
eaaf0 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a ;. return 0;.}.
eab00 0a 2f 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./*.************
eab10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
eab20 20 6f 66 20 73 71 6c 69 74 65 33 5f 76 66 73 20 of sqlite3_vfs
eab30 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a methods ********
eab40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eab50 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***.************
eab60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eab70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eab80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eab90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eaba0 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/../**********
eabb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eabc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eabd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eabe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eabf0 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****.***********
eac00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 ***************
eac10 42 65 67 69 6e 20 50 72 6f 78 79 20 4c 6f 63 6b Begin Proxy Lock
eac20 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ing ************
eac30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eac40 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 50 72 6f 78 79 ****.**.** Proxy
eac50 20 6c 6f 63 6b 69 6e 67 20 69 73 20 61 20 22 75 locking is a "u
eac60 62 65 72 2d 6c 6f 63 6b 69 6e 67 2d 6d 65 74 68 ber-locking-meth
eac70 6f 64 22 20 69 6e 20 74 68 69 73 20 73 65 6e 73 od" in this sens
eac80 65 3a 20 20 49 74 20 75 73 65 73 20 74 68 65 0a e: It uses the.
eac90 2a 2a 20 6f 74 68 65 72 20 6c 6f 63 6b 69 6e 67 ** other locking
eaca0 20 6d 65 74 68 6f 64 73 20 6f 6e 20 73 65 63 6f methods on seco
eacb0 6e 64 61 72 79 20 6c 6f 63 6b 20 66 69 6c 65 73 ndary lock files
eacc0 2e 20 20 50 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 . Proxy locking
eacd0 20 69 73 20 61 0a 2a 2a 20 6d 65 74 61 2d 6c 61 is a.** meta-la
eace0 79 65 72 20 6f 76 65 72 20 74 6f 70 20 6f 66 20 yer over top of
eacf0 74 68 65 20 70 72 69 6d 69 74 69 76 65 20 6c 6f the primitive lo
ead00 63 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 65 cking implemente
ead10 64 20 61 62 6f 76 65 2e 20 20 46 6f 72 0a 2a 2a d above. For.**
ead20 20 74 68 69 73 20 72 65 61 73 6f 6e 2c 20 74 68 this reason, th
ead30 65 20 64 69 76 69 73 69 6f 6e 20 74 68 61 74 20 e division that
ead40 69 6d 70 6c 65 6d 65 6e 74 73 20 6f 66 20 70 72 implements of pr
ead50 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69 73 20 64 oxy locking is d
ead60 65 66 65 72 72 65 64 0a 2a 2a 20 75 6e 74 69 6c eferred.** until
ead70 20 6c 61 74 65 20 69 6e 20 74 68 65 20 66 69 6c late in the fil
ead80 65 20 28 68 65 72 65 29 20 61 66 74 65 72 20 61 e (here) after a
ead90 6c 6c 20 6f 66 20 74 68 65 20 6f 74 68 65 72 20 ll of the other
eada0 49 2f 4f 20 6d 65 74 68 6f 64 73 20 68 61 76 65 I/O methods have
eadb0 0a 2a 2a 20 62 65 65 6e 20 64 65 66 69 6e 65 64 .** been defined
eadc0 20 2d 20 73 6f 20 74 68 61 74 20 74 68 65 20 70 - so that the p
eadd0 72 69 6d 69 74 69 76 65 20 6c 6f 63 6b 69 6e 67 rimitive locking
eade0 20 6d 65 74 68 6f 64 73 20 61 72 65 20 61 76 61 methods are ava
eadf0 69 6c 61 62 6c 65 0a 2a 2a 20 61 73 20 73 65 72 ilable.** as ser
eae00 76 69 63 65 73 20 74 6f 20 68 65 6c 70 20 77 69 vices to help wi
eae10 74 68 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 th the implement
eae20 61 74 69 6f 6e 20 6f 66 20 70 72 6f 78 79 20 6c ation of proxy l
eae30 6f 63 6b 69 6e 67 2e 0a 2a 2a 0a 2a 2a 2a 2a 0a ocking..**.****.
eae40 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c **.** The defaul
eae50 74 20 6c 6f 63 6b 69 6e 67 20 73 63 68 65 6d 65 t locking scheme
eae60 73 20 69 6e 20 53 51 4c 69 74 65 20 75 73 65 20 s in SQLite use
eae70 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b 73 byte-range locks
eae80 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 on the.** datab
eae90 61 73 65 20 66 69 6c 65 20 74 6f 20 63 6f 6f 72 ase file to coor
eaea0 64 69 6e 61 74 65 20 73 61 66 65 2c 20 63 6f 6e dinate safe, con
eaeb0 63 75 72 72 65 6e 74 20 61 63 63 65 73 73 20 62 current access b
eaec0 79 20 6d 75 6c 74 69 70 6c 65 20 72 65 61 64 65 y multiple reade
eaed0 72 73 0a 2a 2a 20 61 6e 64 20 77 72 69 74 65 72 rs.** and writer
eaee0 73 20 5b 68 74 74 70 3a 2f 2f 73 71 6c 69 74 65 s [http://sqlite
eaef0 2e 6f 72 67 2f 6c 6f 63 6b 69 6e 67 76 33 2e 68 .org/lockingv3.h
eaf00 74 6d 6c 5d 2e 20 20 54 68 65 20 66 69 76 65 20 tml]. The five
eaf10 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 file locking.**
eaf20 73 74 61 74 65 73 20 28 55 4e 4c 4f 43 4b 45 44 states (UNLOCKED
eaf30 2c 20 50 45 4e 44 49 4e 47 2c 20 53 48 41 52 45 , PENDING, SHARE
eaf40 44 2c 20 52 45 53 45 52 56 45 44 2c 20 45 58 43 D, RESERVED, EXC
eaf50 4c 55 53 49 56 45 29 20 61 72 65 20 69 6d 70 6c LUSIVE) are impl
eaf60 65 6d 65 6e 74 65 64 0a 2a 2a 20 61 73 20 50 4f emented.** as PO
eaf70 53 49 58 20 72 65 61 64 20 26 20 77 72 69 74 65 SIX read & write
eaf80 20 6c 6f 63 6b 73 20 6f 76 65 72 20 66 69 78 65 locks over fixe
eaf90 64 20 73 65 74 20 6f 66 20 6c 6f 63 61 74 69 6f d set of locatio
eafa0 6e 73 20 28 76 69 61 20 66 73 63 74 6c 29 2c 0a ns (via fsctl),.
eafb0 2a 2a 20 6f 6e 20 41 46 50 20 61 6e 64 20 53 4d ** on AFP and SM
eafc0 42 20 6f 6e 6c 79 20 65 78 63 6c 75 73 69 76 65 B only exclusive
eafd0 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b byte-range lock
eafe0 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 20 s are available
eaff0 76 69 61 20 66 73 63 74 6c 0a 2a 2a 20 77 69 74 via fsctl.** wit
eb000 68 20 5f 49 4f 57 52 28 27 7a 27 2c 20 32 33 2c h _IOWR('z', 23,
eb010 20 73 74 72 75 63 74 20 42 79 74 65 52 61 6e 67 struct ByteRang
eb020 65 4c 6f 63 6b 50 42 32 29 20 74 6f 20 74 72 61 eLockPB2) to tra
eb030 63 6b 20 74 68 65 20 73 61 6d 65 20 35 20 73 74 ck the same 5 st
eb040 61 74 65 73 2e 0a 2a 2a 20 54 6f 20 73 69 6d 75 ates..** To simu
eb050 6c 61 74 65 20 61 20 46 5f 52 44 4c 43 4b 20 6f late a F_RDLCK o
eb060 6e 20 74 68 65 20 73 68 61 72 65 64 20 72 61 6e n the shared ran
eb070 67 65 2c 20 6f 6e 20 41 46 50 20 61 20 72 61 6e ge, on AFP a ran
eb080 64 6f 6d 6c 79 20 73 65 6c 65 63 74 65 64 0a 2a domly selected.*
eb090 2a 20 61 64 64 72 65 73 73 20 69 6e 20 74 68 65 * address in the
eb0a0 20 73 68 61 72 65 64 20 72 61 6e 67 65 20 69 73 shared range is
eb0b0 20 74 61 6b 65 6e 20 66 6f 72 20 61 20 53 48 41 taken for a SHA
eb0c0 52 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20 65 6e RED lock, the en
eb0d0 74 69 72 65 0a 2a 2a 20 73 68 61 72 65 64 20 72 tire.** shared r
eb0e0 61 6e 67 65 20 69 73 20 74 61 6b 65 6e 20 66 6f ange is taken fo
eb0f0 72 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c r an EXCLUSIVE l
eb100 6f 63 6b 29 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 ock):.**.**
eb110 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 20 20 PENDING_BYTE
eb120 20 20 20 20 20 30 78 34 30 30 30 30 30 30 30 09 0x40000000.
eb130 09 20 20 20 09 0a 2a 2a 20 20 20 20 20 20 52 45 . ..** RE
eb140 53 45 52 56 45 44 5f 42 59 54 45 20 20 20 20 20 SERVED_BYTE
eb150 20 20 30 78 34 30 30 30 30 30 30 31 0a 2a 2a 20 0x40000001.**
eb160 20 20 20 20 20 53 48 41 52 45 44 5f 52 41 4e 47 SHARED_RANG
eb170 45 20 20 20 20 20 20 20 20 30 78 34 30 30 30 30 E 0x40000
eb180 30 30 32 20 2d 3e 20 30 78 34 30 30 30 30 32 30 002 -> 0x4000020
eb190 30 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 0.**.** This wor
eb1a0 6b 73 20 77 65 6c 6c 20 6f 6e 20 74 68 65 20 6c ks well on the l
eb1b0 6f 63 61 6c 20 66 69 6c 65 20 73 79 73 74 65 6d ocal file system
eb1c0 2c 20 62 75 74 20 73 68 6f 77 73 20 61 20 6e 65 , but shows a ne
eb1d0 61 72 6c 79 20 31 30 30 78 0a 2a 2a 20 73 6c 6f arly 100x.** slo
eb1e0 77 64 6f 77 6e 20 69 6e 20 72 65 61 64 20 70 65 wdown in read pe
eb1f0 72 66 6f 72 6d 61 6e 63 65 20 6f 6e 20 41 46 50 rformance on AFP
eb200 20 62 65 63 61 75 73 65 20 74 68 65 20 41 46 50 because the AFP
eb210 20 63 6c 69 65 6e 74 20 64 69 73 61 62 6c 65 73 client disables
eb220 0a 2a 2a 20 74 68 65 20 72 65 61 64 20 63 61 63 .** the read cac
eb230 68 65 20 77 68 65 6e 20 62 79 74 65 2d 72 61 6e he when byte-ran
eb240 67 65 20 6c 6f 63 6b 73 20 61 72 65 20 70 72 65 ge locks are pre
eb250 73 65 6e 74 2e 20 20 45 6e 61 62 6c 69 6e 67 20 sent. Enabling
eb260 74 68 65 20 72 65 61 64 0a 2a 2a 20 63 61 63 68 the read.** cach
eb270 65 20 65 78 70 6f 73 65 73 20 61 20 63 61 63 68 e exposes a cach
eb280 65 20 63 6f 68 65 72 65 6e 63 79 20 70 72 6f 62 e coherency prob
eb290 6c 65 6d 20 74 68 61 74 20 69 73 20 70 72 65 73 lem that is pres
eb2a0 65 6e 74 20 6f 6e 20 61 6c 6c 20 4f 53 20 58 0a ent on all OS X.
eb2b0 2a 2a 20 73 75 70 70 6f 72 74 65 64 20 6e 65 74 ** supported net
eb2c0 77 6f 72 6b 20 66 69 6c 65 20 73 79 73 74 65 6d work file system
eb2d0 73 2e 20 20 4e 46 53 20 61 6e 64 20 41 46 50 20 s. NFS and AFP
eb2e0 62 6f 74 68 20 6f 62 73 65 72 76 65 20 74 68 65 both observe the
eb2f0 0a 2a 2a 20 63 6c 6f 73 65 2d 74 6f 2d 6f 70 65 .** close-to-ope
eb300 6e 20 73 65 6d 61 6e 74 69 63 73 20 66 6f 72 20 n semantics for
eb310 65 6e 73 75 72 69 6e 67 20 63 61 63 68 65 20 63 ensuring cache c
eb320 6f 68 65 72 65 6e 63 79 0a 2a 2a 20 5b 68 74 74 oherency.** [htt
eb330 70 3a 2f 2f 6e 66 73 2e 73 6f 75 72 63 65 66 6f p://nfs.sourcefo
eb340 72 67 65 2e 6e 65 74 2f 23 66 61 71 5f 61 38 5d rge.net/#faq_a8]
eb350 2c 20 77 68 69 63 68 20 64 6f 65 73 20 6e 6f 74 , which does not
eb360 20 65 66 66 65 63 74 69 76 65 6c 79 0a 2a 2a 20 effectively.**
eb370 61 64 64 72 65 73 73 20 74 68 65 20 72 65 71 75 address the requ
eb380 69 72 65 6d 65 6e 74 73 20 66 6f 72 20 63 6f 6e irements for con
eb390 63 75 72 72 65 6e 74 20 64 61 74 61 62 61 73 65 current database
eb3a0 20 61 63 63 65 73 73 20 62 79 20 6d 75 6c 74 69 access by multi
eb3b0 70 6c 65 0a 2a 2a 20 72 65 61 64 65 72 73 20 61 ple.** readers a
eb3c0 6e 64 20 77 72 69 74 65 72 73 0a 2a 2a 20 5b 68 nd writers.** [h
eb3d0 74 74 70 3a 2f 2f 77 77 77 2e 6e 61 62 62 6c 65 ttp://www.nabble
eb3e0 2e 63 6f 6d 2f 53 51 4c 69 74 65 2d 6f 6e 2d 4e .com/SQLite-on-N
eb3f0 46 53 2d 63 61 63 68 65 2d 63 6f 68 65 72 65 6e FS-cache-coheren
eb400 63 79 2d 74 64 31 35 36 35 35 37 30 31 2e 68 74 cy-td15655701.ht
eb410 6d 6c 5d 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 64 ml]..**.** To ad
eb420 64 72 65 73 73 20 74 68 65 20 70 65 72 66 6f 72 dress the perfor
eb430 6d 61 6e 63 65 20 61 6e 64 20 63 61 63 68 65 20 mance and cache
eb440 63 6f 68 65 72 65 6e 63 79 20 69 73 73 75 65 73 coherency issues
eb450 2c 20 70 72 6f 78 79 20 66 69 6c 65 20 6c 6f 63 , proxy file loc
eb460 6b 69 6e 67 0a 2a 2a 20 63 68 61 6e 67 65 73 20 king.** changes
eb470 74 68 65 20 77 61 79 20 64 61 74 61 62 61 73 65 the way database
eb480 20 61 63 63 65 73 73 20 69 73 20 63 6f 6e 74 72 access is contr
eb490 6f 6c 6c 65 64 20 62 79 20 6c 69 6d 69 74 69 6e olled by limitin
eb4a0 67 20 61 63 63 65 73 73 20 74 6f 20 61 0a 2a 2a g access to a.**
eb4b0 20 73 69 6e 67 6c 65 20 68 6f 73 74 20 61 74 20 single host at
eb4c0 61 20 74 69 6d 65 20 61 6e 64 20 6d 6f 76 69 6e a time and movin
eb4d0 67 20 66 69 6c 65 20 6c 6f 63 6b 73 20 6f 66 66 g file locks off
eb4e0 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
eb4f0 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 6f 6e 74 file.** and ont
eb500 6f 20 61 20 70 72 6f 78 79 20 66 69 6c 65 20 6f o a proxy file o
eb510 6e 20 74 68 65 20 6c 6f 63 61 6c 20 66 69 6c 65 n the local file
eb520 20 73 79 73 74 65 6d 2e 20 20 0a 2a 2a 0a 2a 2a system. .**.**
eb530 0a 2a 2a 20 55 73 69 6e 67 20 70 72 6f 78 79 20 .** Using proxy
eb540 6c 6f 63 6b 73 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d locks.** -------
eb550 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a ----------.**.**
eb560 20 43 20 41 50 49 73 0a 2a 2a 0a 2a 2a 20 20 73 C APIs.**.** s
eb570 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 qlite3_file_cont
eb580 72 6f 6c 28 64 62 2c 20 64 62 6e 61 6d 65 2c 20 rol(db, dbname,
eb590 53 51 4c 49 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 SQLITE_SET_LOCKP
eb5a0 52 4f 58 59 46 49 4c 45 2c 0a 2a 2a 20 20 20 20 ROXYFILE,.**
eb5b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
eb5c0 20 20 20 3c 70 72 6f 78 79 5f 70 61 74 68 3e 20 <proxy_path>
eb5d0 7c 20 22 3a 61 75 74 6f 3a 22 29 3b 0a 2a 2a 20 | ":auto:");.**
eb5e0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f sqlite3_file_co
eb5f0 6e 74 72 6f 6c 28 64 62 2c 20 64 62 6e 61 6d 65 ntrol(db, dbname
eb600 2c 20 53 51 4c 49 54 45 5f 47 45 54 5f 4c 4f 43 , SQLITE_GET_LOC
eb610 4b 50 52 4f 58 59 46 49 4c 45 2c 20 26 3c 70 72 KPROXYFILE, &<pr
eb620 6f 78 79 5f 70 61 74 68 3e 29 3b 0a 2a 2a 0a 2a oxy_path>);.**.*
eb630 2a 0a 2a 2a 20 53 51 4c 20 70 72 61 67 6d 61 73 *.** SQL pragmas
eb640 0a 2a 2a 0a 2a 2a 20 20 50 52 41 47 4d 41 20 5b .**.** PRAGMA [
eb650 64 61 74 61 62 61 73 65 2e 5d 6c 6f 63 6b 5f 70 database.]lock_p
eb660 72 6f 78 79 5f 66 69 6c 65 3d 3c 70 72 6f 78 79 roxy_file=<proxy
eb670 5f 70 61 74 68 3e 20 7c 20 3a 61 75 74 6f 3a 0a _path> | :auto:.
eb680 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 ** PRAGMA [data
eb690 62 61 73 65 2e 5d 6c 6f 63 6b 5f 70 72 6f 78 79 base.]lock_proxy
eb6a0 5f 66 69 6c 65 0a 2a 2a 0a 2a 2a 20 53 70 65 63 _file.**.** Spec
eb6b0 69 66 79 69 6e 67 20 22 3a 61 75 74 6f 3a 22 20 ifying ":auto:"
eb6c0 6d 65 61 6e 73 20 74 68 61 74 20 69 66 20 74 68 means that if th
eb6d0 65 72 65 20 69 73 20 61 20 63 6f 6e 63 68 20 66 ere is a conch f
eb6e0 69 6c 65 20 77 69 74 68 20 61 20 6d 61 74 63 68 ile with a match
eb6f0 69 6e 67 0a 2a 2a 20 68 6f 73 74 20 49 44 20 69 ing.** host ID i
eb700 6e 20 69 74 2c 20 74 68 65 20 70 72 6f 78 79 20 n it, the proxy
eb710 70 61 74 68 20 69 6e 20 74 68 65 20 63 6f 6e 63 path in the conc
eb720 68 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 75 h file will be u
eb730 73 65 64 2c 20 6f 74 68 65 72 77 69 73 65 0a 2a sed, otherwise.*
eb740 2a 20 61 20 70 72 6f 78 79 20 70 61 74 68 20 62 * a proxy path b
eb750 61 73 65 64 20 6f 6e 20 74 68 65 20 75 73 65 72 ased on the user
eb760 27 73 20 74 65 6d 70 20 64 69 72 0a 2a 2a 20 28 's temp dir.** (
eb770 76 69 61 20 63 6f 6e 66 73 74 72 28 5f 43 53 5f via confstr(_CS_
eb780 44 41 52 57 49 4e 5f 55 53 45 52 5f 54 45 4d 50 DARWIN_USER_TEMP
eb790 5f 44 49 52 2c 2e 2e 2e 29 29 20 77 69 6c 6c 20 _DIR,...)) will
eb7a0 62 65 20 75 73 65 64 20 61 6e 64 20 74 68 65 0a be used and the.
eb7b0 2a 2a 20 61 63 74 75 61 6c 20 70 72 6f 78 79 20 ** actual proxy
eb7c0 66 69 6c 65 20 6e 61 6d 65 20 69 73 20 67 65 6e file name is gen
eb7d0 65 72 61 74 65 64 20 66 72 6f 6d 20 74 68 65 20 erated from the
eb7e0 6e 61 6d 65 20 61 6e 64 20 70 61 74 68 20 6f 66 name and path of
eb7f0 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 the.** database
eb800 20 66 69 6c 65 2e 20 20 46 6f 72 20 65 78 61 6d file. For exam
eb810 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 ple:.**.**
eb820 20 46 6f 72 20 64 61 74 61 62 61 73 65 20 70 61 For database pa
eb830 74 68 20 22 2f 55 73 65 72 73 2f 6d 65 2f 66 6f th "/Users/me/fo
eb840 6f 2e 64 62 22 20 0a 2a 2a 20 20 20 20 20 20 20 o.db" .**
eb850 54 68 65 20 6c 6f 63 6b 20 70 61 74 68 20 77 69 The lock path wi
eb860 6c 6c 20 62 65 20 22 3c 74 6d 70 64 69 72 3e 2f ll be "<tmpdir>/
eb870 73 71 6c 69 74 65 70 6c 6f 63 6b 73 2f 5f 55 73 sqliteplocks/_Us
eb880 65 72 73 5f 6d 65 5f 66 6f 6f 2e 64 62 3a 61 75 ers_me_foo.db:au
eb890 74 6f 3a 22 29 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 to:").**.** Once
eb8a0 20 61 20 6c 6f 63 6b 20 70 72 6f 78 79 20 69 73 a lock proxy is
eb8b0 20 63 6f 6e 66 69 67 75 72 65 64 20 66 6f 72 20 configured for
eb8c0 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 a database conne
eb8d0 63 74 69 6f 6e 2c 20 69 74 20 63 61 6e 20 6e 6f ction, it can no
eb8e0 74 0a 2a 2a 20 62 65 20 72 65 6d 6f 76 65 64 2c t.** be removed,
eb8f0 20 68 6f 77 65 76 65 72 20 69 74 20 6d 61 79 20 however it may
eb900 62 65 20 73 77 69 74 63 68 65 64 20 74 6f 20 61 be switched to a
eb910 20 64 69 66 66 65 72 65 6e 74 20 70 72 6f 78 79 different proxy
eb920 20 70 61 74 68 20 76 69 61 0a 2a 2a 20 74 68 65 path via.** the
eb930 20 61 62 6f 76 65 20 41 50 49 73 20 28 61 73 73 above APIs (ass
eb940 75 6d 69 6e 67 20 74 68 65 20 63 6f 6e 63 68 20 uming the conch
eb950 66 69 6c 65 20 69 73 20 6e 6f 74 20 62 65 69 6e file is not bein
eb960 67 20 68 65 6c 64 20 62 79 20 61 6e 6f 74 68 65 g held by anothe
eb970 72 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 r.** connection
eb980 6f 72 20 70 72 6f 63 65 73 73 29 2e 20 0a 2a 2a or process). .**
eb990 0a 2a 2a 0a 2a 2a 20 48 6f 77 20 70 72 6f 78 79 .**.** How proxy
eb9a0 20 6c 6f 63 6b 69 6e 67 20 77 6f 72 6b 73 0a 2a locking works.*
eb9b0 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d * --------------
eb9c0 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 ---------.**.**
eb9d0 50 72 6f 78 79 20 66 69 6c 65 20 6c 6f 63 6b 69 Proxy file locki
eb9e0 6e 67 20 72 65 6c 69 65 73 20 70 72 69 6d 61 72 ng relies primar
eb9f0 69 6c 79 20 6f 6e 20 74 77 6f 20 6e 65 77 20 73 ily on two new s
eba00 75 70 70 6f 72 74 69 6e 67 20 66 69 6c 65 73 3a upporting files:
eba10 20 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 63 6f 6e .**.** * con
eba20 63 68 20 66 69 6c 65 20 74 6f 20 6c 69 6d 69 74 ch file to limit
eba30 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 64 access to the d
eba40 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 atabase file to
eba50 61 20 73 69 6e 67 6c 65 20 68 6f 73 74 0a 2a 2a a single host.**
eba60 20 20 20 20 20 20 61 74 20 61 20 74 69 6d 65 0a at a time.
eba70 2a 2a 0a 2a 2a 20 20 20 2a 20 20 70 72 6f 78 79 **.** * proxy
eba80 20 66 69 6c 65 20 74 6f 20 61 63 74 20 61 73 20 file to act as
eba90 61 20 70 72 6f 78 79 20 66 6f 72 20 74 68 65 20 a proxy for the
ebaa0 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73 20 6e advisory locks n
ebab0 6f 72 6d 61 6c 6c 79 0a 2a 2a 20 20 20 20 20 20 ormally.**
ebac0 74 61 6b 65 6e 20 6f 6e 20 74 68 65 20 64 61 74 taken on the dat
ebad0 61 62 61 73 65 0a 2a 2a 0a 2a 2a 20 54 68 65 20 abase.**.** The
ebae0 63 6f 6e 63 68 20 66 69 6c 65 20 2d 20 74 6f 20 conch file - to
ebaf0 75 73 65 20 61 20 70 72 6f 78 79 20 66 69 6c 65 use a proxy file
ebb00 2c 20 73 71 6c 69 74 65 20 6d 75 73 74 20 66 69 , sqlite must fi
ebb10 72 73 74 20 22 68 6f 6c 64 20 74 68 65 20 63 6f rst "hold the co
ebb20 6e 63 68 22 0a 2a 2a 20 62 79 20 74 61 6b 69 6e nch".** by takin
ebb30 67 20 61 6e 20 73 71 6c 69 74 65 2d 73 74 79 6c g an sqlite-styl
ebb40 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 6f 6e e shared lock on
ebb50 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 2c the conch file,
ebb60 20 72 65 61 64 69 6e 67 20 74 68 65 0a 2a 2a 20 reading the.**
ebb70 63 6f 6e 74 65 6e 74 73 20 61 6e 64 20 63 6f 6d contents and com
ebb80 70 61 72 69 6e 67 20 74 68 65 20 68 6f 73 74 27 paring the host'
ebb90 73 20 75 6e 69 71 75 65 20 68 6f 73 74 20 49 44 s unique host ID
ebba0 20 28 73 65 65 20 62 65 6c 6f 77 29 20 61 6e 64 (see below) and
ebbb0 20 6c 6f 63 6b 0a 2a 2a 20 70 72 6f 78 79 20 70 lock.** proxy p
ebbc0 61 74 68 20 61 67 61 69 6e 73 74 20 74 68 65 20 ath against the
ebbd0 76 61 6c 75 65 73 20 73 74 6f 72 65 64 20 69 6e values stored in
ebbe0 20 74 68 65 20 63 6f 6e 63 68 2e 20 20 54 68 65 the conch. The
ebbf0 20 63 6f 6e 63 68 20 66 69 6c 65 20 69 73 0a 2a conch file is.*
ebc00 2a 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 * stored in the
ebc10 73 61 6d 65 20 64 69 72 65 63 74 6f 72 79 20 61 same directory a
ebc20 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 s the database f
ebc30 69 6c 65 20 61 6e 64 20 74 68 65 20 66 69 6c 65 ile and the file
ebc40 20 6e 61 6d 65 0a 2a 2a 20 69 73 20 70 61 74 74 name.** is patt
ebc50 65 72 6e 65 64 20 61 66 74 65 72 20 74 68 65 20 erned after the
ebc60 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6e 61 database file na
ebc70 6d 65 20 61 73 20 22 2e 3c 64 61 74 61 62 61 73 me as ".<databas
ebc80 65 6e 61 6d 65 3e 2d 63 6f 6e 63 68 22 2e 0a 2a ename>-conch"..*
ebc90 2a 20 49 66 20 74 68 65 20 63 6f 6e 63 68 20 66 * If the conch f
ebca0 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 ile does not exi
ebcb0 73 74 2c 20 6f 72 20 69 74 27 73 20 63 6f 6e 74 st, or it's cont
ebcc0 65 6e 74 73 20 64 6f 20 6e 6f 74 20 6d 61 74 63 ents do not matc
ebcd0 68 20 74 68 65 0a 2a 2a 20 68 6f 73 74 20 49 44 h the.** host ID
ebce0 20 61 6e 64 2f 6f 72 20 70 72 6f 78 79 20 70 61 and/or proxy pa
ebcf0 74 68 2c 20 74 68 65 6e 20 74 68 65 20 6c 6f 63 th, then the loc
ebd00 6b 20 69 73 20 65 73 63 61 6c 61 74 65 64 20 74 k is escalated t
ebd10 6f 20 61 6e 20 65 78 63 6c 75 73 69 76 65 0a 2a o an exclusive.*
ebd20 2a 20 6c 6f 63 6b 20 61 6e 64 20 74 68 65 20 63 * lock and the c
ebd30 6f 6e 63 68 20 66 69 6c 65 20 63 6f 6e 74 65 6e onch file conten
ebd40 74 73 20 69 73 20 75 70 64 61 74 65 64 20 77 69 ts is updated wi
ebd50 74 68 20 74 68 65 20 68 6f 73 74 20 49 44 20 61 th the host ID a
ebd60 6e 64 20 70 72 6f 78 79 0a 2a 2a 20 70 61 74 68 nd proxy.** path
ebd70 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20 69 73 and the lock is
ebd80 20 64 6f 77 6e 67 72 61 64 65 64 20 74 6f 20 61 downgraded to a
ebd90 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 67 61 shared lock aga
ebda0 69 6e 2e 20 20 49 66 20 74 68 65 20 63 6f 6e 63 in. If the conc
ebdb0 68 0a 2a 2a 20 69 73 20 68 65 6c 64 20 62 79 20 h.** is held by
ebdc0 61 6e 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 another process
ebdd0 28 77 69 74 68 20 61 20 73 68 61 72 65 64 20 6c (with a shared l
ebde0 6f 63 6b 29 2c 20 74 68 65 20 65 78 63 6c 75 73 ock), the exclus
ebdf0 69 76 65 20 6c 6f 63 6b 0a 2a 2a 20 77 69 6c 6c ive lock.** will
ebe00 20 66 61 69 6c 20 61 6e 64 20 53 51 4c 49 54 45 fail and SQLITE
ebe10 5f 42 55 53 59 20 69 73 20 72 65 74 75 72 6e 65 _BUSY is returne
ebe20 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 6f d..**.** The pro
ebe30 78 79 20 66 69 6c 65 20 2d 20 61 20 73 69 6e 67 xy file - a sing
ebe40 6c 65 2d 62 79 74 65 20 66 69 6c 65 20 75 73 65 le-byte file use
ebe50 64 20 66 6f 72 20 61 6c 6c 20 61 64 76 69 73 6f d for all adviso
ebe60 72 79 20 66 69 6c 65 20 6c 6f 63 6b 73 0a 2a 2a ry file locks.**
ebe70 20 6e 6f 72 6d 61 6c 6c 79 20 74 61 6b 65 6e 20 normally taken
ebe80 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
ebe90 66 69 6c 65 2e 20 20 20 54 68 69 73 20 61 6c 6c file. This all
ebea0 6f 77 73 20 66 6f 72 20 73 61 66 65 20 73 68 61 ows for safe sha
ebeb0 72 69 6e 67 0a 2a 2a 20 6f 66 20 74 68 65 20 64 ring.** of the d
ebec0 61 74 61 62 61 73 65 20 66 69 6c 65 20 66 6f 72 atabase file for
ebed0 20 6d 75 6c 74 69 70 6c 65 20 72 65 61 64 65 72 multiple reader
ebee0 73 20 61 6e 64 20 77 72 69 74 65 72 73 20 6f 6e s and writers on
ebef0 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 68 6f 73 the same.** hos
ebf00 74 20 28 74 68 65 20 63 6f 6e 63 68 20 65 6e 73 t (the conch ens
ebf10 75 72 65 73 20 74 68 61 74 20 74 68 65 79 20 61 ures that they a
ebf20 6c 6c 20 75 73 65 20 74 68 65 20 73 61 6d 65 20 ll use the same
ebf30 6c 6f 63 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 29 local lock file)
ebf40 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 ..**.** There is
ebf50 20 61 20 74 68 69 72 64 20 66 69 6c 65 20 2d 20 a third file -
ebf60 74 68 65 20 68 6f 73 74 20 49 44 20 66 69 6c 65 the host ID file
ebf70 20 2d 20 75 73 65 64 20 61 73 20 61 20 70 65 72 - used as a per
ebf80 73 69 73 74 65 6e 74 20 72 65 63 6f 72 64 0a 2a sistent record.*
ebf90 2a 20 6f 66 20 61 20 75 6e 69 71 75 65 20 69 64 * of a unique id
ebfa0 65 6e 74 69 66 69 65 72 20 66 6f 72 20 74 68 65 entifier for the
ebfb0 20 68 6f 73 74 2c 20 61 20 31 32 38 2d 62 79 74 host, a 128-byt
ebfc0 65 20 75 6e 69 71 75 65 20 68 6f 73 74 20 69 64 e unique host id
ebfd0 20 66 69 6c 65 0a 2a 2a 20 69 6e 20 74 68 65 20 file.** in the
ebfe0 70 61 74 68 20 64 65 66 69 6e 65 64 20 62 79 20 path defined by
ebff0 74 68 65 20 48 4f 53 54 49 44 50 41 54 48 20 6d the HOSTIDPATH m
ec000 61 63 72 6f 20 28 64 65 66 61 75 6c 74 20 76 61 acro (default va
ec010 6c 75 65 20 69 73 0a 2a 2a 20 2f 4c 69 62 72 61 lue is.** /Libra
ec020 72 79 2f 43 61 63 68 65 73 2f 2e 63 6f 6d 2e 61 ry/Caches/.com.a
ec030 70 70 6c 65 2e 73 71 6c 69 74 65 43 6f 6e 63 68 pple.sqliteConch
ec040 48 6f 73 74 49 64 29 2e 0a 2a 2a 0a 2a 2a 20 52 HostId)..**.** R
ec050 65 71 75 65 73 74 69 6e 67 20 74 68 65 20 6c 6f equesting the lo
ec060 63 6b 20 70 72 6f 78 79 20 64 6f 65 73 20 6e 6f ck proxy does no
ec070 74 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 61 t immediately ta
ec080 6b 65 20 74 68 65 20 63 6f 6e 63 68 2c 20 69 74 ke the conch, it
ec090 20 69 73 0a 2a 2a 20 6f 6e 6c 79 20 74 61 6b 65 is.** only take
ec0a0 6e 20 77 68 65 6e 20 74 68 65 20 66 69 72 73 74 n when the first
ec0b0 20 72 65 71 75 65 73 74 20 74 6f 20 6c 6f 63 6b request to lock
ec0c0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 database file i
ec0d0 73 20 6d 61 64 65 2e 20 20 0a 2a 2a 20 54 68 69 s made. .** Thi
ec0e0 73 20 6d 61 74 63 68 65 73 20 74 68 65 20 73 65 s matches the se
ec0f0 6d 61 6e 74 69 63 73 20 6f 66 20 74 68 65 20 74 mantics of the t
ec100 72 61 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 69 raditional locki
ec110 6e 67 20 62 65 68 61 76 69 6f 72 2c 20 77 68 65 ng behavior, whe
ec120 72 65 0a 2a 2a 20 6f 70 65 6e 69 6e 67 20 61 20 re.** opening a
ec130 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 61 20 connection to a
ec140 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 64 6f database file do
ec150 65 73 20 6e 6f 74 20 74 61 6b 65 20 61 20 6c 6f es not take a lo
ec160 63 6b 20 6f 6e 20 69 74 2e 0a 2a 2a 20 54 68 65 ck on it..** The
ec170 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 6e 64 shared lock and
ec180 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 64 65 an open file de
ec190 73 63 72 69 70 74 6f 72 20 61 72 65 20 6d 61 69 scriptor are mai
ec1a0 6e 74 61 69 6e 65 64 20 75 6e 74 69 6c 20 0a 2a ntained until .*
ec1b0 2a 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e * the connection
ec1c0 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
ec1d0 20 69 73 20 63 6c 6f 73 65 64 2e 20 0a 2a 2a 0a is closed. .**.
ec1e0 2a 2a 20 54 68 65 20 70 72 6f 78 79 20 66 69 6c ** The proxy fil
ec1f0 65 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20 66 e and the lock f
ec200 69 6c 65 20 61 72 65 20 6e 65 76 65 72 20 64 65 ile are never de
ec210 6c 65 74 65 64 20 73 6f 20 74 68 65 79 20 6f 6e leted so they on
ec220 6c 79 20 6e 65 65 64 0a 2a 2a 20 74 6f 20 62 65 ly need.** to be
ec230 20 63 72 65 61 74 65 64 20 74 68 65 20 66 69 72 created the fir
ec240 73 74 20 74 69 6d 65 20 74 68 65 79 20 61 72 65 st time they are
ec250 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e used..**.** Con
ec260 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f figuration optio
ec270 6e 73 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ns.** ----------
ec280 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a -----------.**.*
ec290 2a 20 20 53 51 4c 49 54 45 5f 50 52 45 46 45 52 * SQLITE_PREFER
ec2a0 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 0a 2a _PROXY_LOCKING.*
ec2b0 2a 0a 2a 2a 20 20 20 20 20 20 20 44 61 74 61 62 *.** Datab
ec2c0 61 73 65 20 66 69 6c 65 73 20 61 63 63 65 73 73 ase files access
ec2d0 65 64 20 6f 6e 20 6e 6f 6e 2d 6c 6f 63 61 6c 20 ed on non-local
ec2e0 66 69 6c 65 20 73 79 73 74 65 6d 73 20 61 72 65 file systems are
ec2f0 0a 2a 2a 20 20 20 20 20 20 20 61 75 74 6f 6d 61 .** automa
ec300 74 69 63 61 6c 6c 79 20 63 6f 6e 66 69 67 75 72 tically configur
ec310 65 64 20 66 6f 72 20 70 72 6f 78 79 20 6c 6f 63 ed for proxy loc
ec320 6b 69 6e 67 2c 20 6c 6f 63 6b 20 66 69 6c 65 73 king, lock files
ec330 20 61 72 65 0a 2a 2a 20 20 20 20 20 20 20 6e 61 are.** na
ec340 6d 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c med automaticall
ec350 79 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 y using the same
ec360 20 6c 6f 67 69 63 20 61 73 0a 2a 2a 20 20 20 20 logic as.**
ec370 20 20 20 50 52 41 47 4d 41 20 6c 6f 63 6b 5f 70 PRAGMA lock_p
ec380 72 6f 78 79 5f 66 69 6c 65 3d 22 3a 61 75 74 6f roxy_file=":auto
ec390 3a 22 0a 2a 2a 20 20 20 20 0a 2a 2a 20 20 53 51 :".** .** SQ
ec3a0 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45 42 55 47 LITE_PROXY_DEBUG
ec3b0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 45 6e 61 .**.** Ena
ec3c0 62 6c 65 73 20 74 68 65 20 6c 6f 67 67 69 6e 67 bles the logging
ec3d0 20 6f 66 20 65 72 72 6f 72 20 6d 65 73 73 61 67 of error messag
ec3e0 65 73 20 64 75 72 69 6e 67 20 68 6f 73 74 20 69 es during host i
ec3f0 64 20 66 69 6c 65 0a 2a 2a 20 20 20 20 20 20 20 d file.**
ec400 72 65 74 72 69 65 76 61 6c 20 61 6e 64 20 63 72 retrieval and cr
ec410 65 61 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 20 48 4f eation.**.** HO
ec420 53 54 49 44 50 41 54 48 0a 2a 2a 0a 2a 2a 20 20 STIDPATH.**.**
ec430 20 20 20 20 20 4f 76 65 72 72 69 64 65 73 20 74 Overrides t
ec440 68 65 20 64 65 66 61 75 6c 74 20 68 6f 73 74 20 he default host
ec450 49 44 20 66 69 6c 65 20 70 61 74 68 20 6c 6f 63 ID file path loc
ec460 61 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 20 4c 4f 43 ation.**.** LOC
ec470 4b 50 52 4f 58 59 44 49 52 0a 2a 2a 0a 2a 2a 20 KPROXYDIR.**.**
ec480 20 20 20 20 20 20 4f 76 65 72 72 69 64 65 73 20 Overrides
ec490 74 68 65 20 64 65 66 61 75 6c 74 20 64 69 72 65 the default dire
ec4a0 63 74 6f 72 79 20 75 73 65 64 20 66 6f 72 20 6c ctory used for l
ec4b0 6f 63 6b 20 70 72 6f 78 79 20 66 69 6c 65 73 20 ock proxy files
ec4c0 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 61 72 that.** ar
ec4d0 65 20 6e 61 6d 65 64 20 61 75 74 6f 6d 61 74 69 e named automati
ec4e0 63 61 6c 6c 79 20 76 69 61 20 74 68 65 20 22 3a cally via the ":
ec4f0 61 75 74 6f 3a 22 20 73 65 74 74 69 6e 67 0a 2a auto:" setting.*
ec500 2a 0a 2a 2a 20 20 53 51 4c 49 54 45 5f 44 45 46 *.** SQLITE_DEF
ec510 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f 50 45 AULT_PROXYDIR_PE
ec520 52 4d 49 53 53 49 4f 4e 53 0a 2a 2a 0a 2a 2a 20 RMISSIONS.**.**
ec530 20 20 20 20 20 20 50 65 72 6d 69 73 73 69 6f 6e Permission
ec540 73 20 74 6f 20 75 73 65 20 77 68 65 6e 20 63 72 s to use when cr
ec550 65 61 74 69 6e 67 20 61 20 64 69 72 65 63 74 6f eating a directo
ec560 72 79 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 ry for storing t
ec570 68 65 0a 2a 2a 20 20 20 20 20 20 20 6c 6f 63 6b he.** lock
ec580 20 70 72 6f 78 79 20 66 69 6c 65 73 2c 20 6f 6e proxy files, on
ec590 6c 79 20 75 73 65 64 20 77 68 65 6e 20 4c 4f 43 ly used when LOC
ec5a0 4b 50 52 4f 58 59 44 49 52 20 69 73 20 6e 6f 74 KPROXYDIR is not
ec5b0 20 73 65 74 2e 0a 2a 2a 20 20 20 20 0a 2a 2a 20 set..** .**
ec5c0 20 20 20 0a 2a 2a 20 41 73 20 6d 65 6e 74 69 6f .** As mentio
ec5d0 6e 65 64 20 61 62 6f 76 65 2c 20 77 68 65 6e 20 ned above, when
ec5e0 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 53 51 compiled with SQ
ec5f0 4c 49 54 45 5f 50 52 45 46 45 52 5f 50 52 4f 58 LITE_PREFER_PROX
ec600 59 5f 4c 4f 43 4b 49 4e 47 2c 0a 2a 2a 20 73 65 Y_LOCKING,.** se
ec610 74 74 69 6e 67 20 74 68 65 20 65 6e 76 69 72 6f tting the enviro
ec620 6e 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 20 53 nment variable S
ec630 51 4c 49 54 45 5f 46 4f 52 43 45 5f 50 52 4f 58 QLITE_FORCE_PROX
ec640 59 5f 4c 4f 43 4b 49 4e 47 20 74 6f 20 31 20 77 Y_LOCKING to 1 w
ec650 69 6c 6c 0a 2a 2a 20 66 6f 72 63 65 20 70 72 6f ill.** force pro
ec660 78 79 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 62 65 xy locking to be
ec670 20 75 73 65 64 20 66 6f 72 20 65 76 65 72 79 20 used for every
ec680 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6f 70 database file op
ec690 65 6e 65 64 2c 20 61 6e 64 20 30 0a 2a 2a 20 77 ened, and 0.** w
ec6a0 69 6c 6c 20 66 6f 72 63 65 20 61 75 74 6f 6d 61 ill force automa
ec6b0 74 69 63 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e tic proxy lockin
ec6c0 67 20 74 6f 20 62 65 20 64 69 73 61 62 6c 65 64 g to be disabled
ec6d0 20 66 6f 72 20 61 6c 6c 20 64 61 74 61 62 61 73 for all databas
ec6e0 65 0a 2a 2a 20 66 69 6c 65 73 20 28 65 78 70 6c e.** files (expl
ec6f0 69 63 69 74 79 20 63 61 6c 6c 69 6e 67 20 74 68 icity calling th
ec700 65 20 53 51 4c 49 54 45 5f 53 45 54 5f 4c 4f 43 e SQLITE_SET_LOC
ec710 4b 50 52 4f 58 59 46 49 4c 45 20 70 72 61 67 6d KPROXYFILE pragm
ec720 61 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 5f 66 a or.** sqlite_f
ec730 69 6c 65 5f 63 6f 6e 74 72 6f 6c 20 41 50 49 20 ile_control API
ec740 69 73 20 6e 6f 74 20 61 66 66 65 63 74 65 64 20 is not affected
ec750 62 79 20 53 51 4c 49 54 45 5f 46 4f 52 43 45 5f by SQLITE_FORCE_
ec760 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 29 2e 0a PROXY_LOCKING)..
ec770 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 78 79 20 */../*.** Proxy
ec780 6c 6f 63 6b 69 6e 67 20 69 73 20 6f 6e 6c 79 20 locking is only
ec790 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 4d 61 63 available on Mac
ec7a0 4f 53 58 20 0a 2a 2f 0a 23 69 66 20 64 65 66 69 OSX .*/.#if defi
ec7b0 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 ned(__APPLE__) &
ec7c0 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f & SQLITE_ENABLE_
ec7d0 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 0a 23 LOCKING_STYLE..#
ec7e0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
ec7f0 54 0a 2f 2a 20 73 69 6d 75 6c 61 74 65 20 6d 75 T./* simulate mu
ec800 6c 74 69 70 6c 65 20 68 6f 73 74 73 20 62 79 20 ltiple hosts by
ec810 63 72 65 61 74 69 6e 67 20 75 6e 69 71 75 65 20 creating unique
ec820 68 6f 73 74 69 64 20 66 69 6c 65 20 70 61 74 68 hostid file path
ec830 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 s */.SQLITE_API
ec840 69 6e 74 20 73 71 6c 69 74 65 33 5f 68 6f 73 74 int sqlite3_host
ec850 69 64 5f 6e 75 6d 20 3d 20 30 3b 0a 23 65 6e 64 id_num = 0;.#end
ec860 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 72 if../*.** The pr
ec870 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 oxyLockingContex
ec880 74 20 68 61 73 20 74 68 65 20 70 61 74 68 20 61 t has the path a
ec890 6e 64 20 66 69 6c 65 20 73 74 72 75 63 74 75 72 nd file structur
ec8a0 65 73 20 66 6f 72 20 74 68 65 20 72 65 6d 6f 74 es for the remot
ec8b0 65 20 0a 2a 2a 20 61 6e 64 20 6c 6f 63 61 6c 20 e .** and local
ec8c0 70 72 6f 78 79 20 66 69 6c 65 73 20 69 6e 20 69 proxy files in i
ec8d0 74 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 t.*/.typedef str
ec8e0 75 63 74 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 uct proxyLocking
ec8f0 43 6f 6e 74 65 78 74 20 70 72 6f 78 79 4c 6f 63 Context proxyLoc
ec900 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 73 74 72 kingContext;.str
ec910 75 63 74 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 uct proxyLocking
ec920 43 6f 6e 74 65 78 74 20 7b 0a 20 20 75 6e 69 78 Context {. unix
ec930 46 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65 3b File *conchFile;
ec940 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e /* Open
ec950 20 63 6f 6e 63 68 20 66 69 6c 65 20 2a 2f 0a 20 conch file */.
ec960 20 63 68 61 72 20 2a 63 6f 6e 63 68 46 69 6c 65 char *conchFile
ec970 50 61 74 68 3b 20 20 20 20 20 20 20 20 20 2f 2a Path; /*
ec980 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6e Name of the con
ec990 63 68 20 66 69 6c 65 20 2a 2f 0a 20 20 75 6e 69 ch file */. uni
ec9a0 78 46 69 6c 65 20 2a 6c 6f 63 6b 50 72 6f 78 79 xFile *lockProxy
ec9b0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 ; /* Ope
ec9c0 6e 20 70 72 6f 78 79 20 6c 6f 63 6b 20 66 69 6c n proxy lock fil
ec9d0 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 6c 6f 63 e */. char *loc
ec9e0 6b 50 72 6f 78 79 50 61 74 68 3b 20 20 20 20 20 kProxyPath;
ec9f0 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 /* Name of t
eca00 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b 20 66 69 he proxy lock fi
eca10 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 64 62 le */. char *db
eca20 50 61 74 68 3b 20 20 20 20 20 20 20 20 20 20 20 Path;
eca30 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 /* Name of
eca40 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 2a 2f the open file */
eca50 0a 20 20 69 6e 74 20 63 6f 6e 63 68 48 65 6c 64 . int conchHeld
eca60 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
eca70 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 63 /* True if the c
eca80 6f 6e 63 68 20 69 73 20 63 75 72 72 65 6e 74 6c onch is currentl
eca90 79 20 68 65 6c 64 20 2a 2f 0a 20 20 76 6f 69 64 y held */. void
ecaa0 20 2a 6f 6c 64 4c 6f 63 6b 69 6e 67 43 6f 6e 74 *oldLockingCont
ecab0 65 78 74 3b 20 20 20 20 20 2f 2a 20 4f 72 69 67 ext; /* Orig
ecac0 69 6e 61 6c 20 6c 6f 63 6b 69 6e 67 63 6f 6e 74 inal lockingcont
ecad0 65 78 74 20 74 6f 20 72 65 73 74 6f 72 65 20 6f ext to restore o
ecae0 6e 20 63 6c 6f 73 65 20 2a 2f 0a 20 20 73 71 6c n close */. sql
ecaf0 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 ite3_io_methods
ecb00 63 6f 6e 73 74 20 2a 70 4f 6c 64 4d 65 74 68 6f const *pOldMetho
ecb10 64 3b 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e d; /* Origin
ecb20 61 6c 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 66 al I/O methods f
ecb30 6f 72 20 63 6c 6f 73 65 20 2a 2f 0a 7d 3b 0a 0a or close */.};..
ecb40 2f 2a 20 48 4f 53 54 49 44 4c 45 4e 20 61 6e 64 /* HOSTIDLEN and
ecb50 20 43 4f 4e 43 48 4c 45 4e 20 62 6f 74 68 20 69 CONCHLEN both i
ecb60 6e 63 6c 75 64 65 20 73 70 61 63 65 20 66 6f 72 nclude space for
ecb70 20 74 68 65 20 73 74 72 69 6e 67 20 0a 2a 2a 20 the string .**
ecb80 74 65 72 6d 69 6e 61 74 69 6e 67 20 6e 75 6c 20 terminating nul
ecb90 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 48 4f 53 54 .*/.#define HOST
ecba0 49 44 4c 45 4e 20 20 20 20 20 20 20 20 20 31 32 IDLEN 12
ecbb0 38 0a 23 64 65 66 69 6e 65 20 43 4f 4e 43 48 4c 8.#define CONCHL
ecbc0 45 4e 20 20 20 20 20 20 20 20 20 20 28 4d 41 58 EN (MAX
ecbd0 50 41 54 48 4c 45 4e 2b 48 4f 53 54 49 44 4c 45 PATHLEN+HOSTIDLE
ecbe0 4e 2b 31 29 0a 23 69 66 6e 64 65 66 20 48 4f 53 N+1).#ifndef HOS
ecbf0 54 49 44 50 41 54 48 0a 23 20 64 65 66 69 6e 65 TIDPATH.# define
ecc00 20 48 4f 53 54 49 44 50 41 54 48 20 20 20 20 20 HOSTIDPATH
ecc10 20 20 22 2f 4c 69 62 72 61 72 79 2f 43 61 63 68 "/Library/Cach
ecc20 65 73 2f 2e 63 6f 6d 2e 61 70 70 6c 65 2e 73 71 es/.com.apple.sq
ecc30 6c 69 74 65 43 6f 6e 63 68 48 6f 73 74 49 64 22 liteConchHostId"
ecc40 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 62 61 73 69 .#endif../* basi
ecc50 63 61 6c 6c 79 20 61 20 63 6f 70 79 20 6f 66 20 cally a copy of
ecc60 75 6e 69 78 52 61 6e 64 6f 6d 6e 65 73 73 20 77 unixRandomness w
ecc70 69 74 68 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a ith different.**
ecc80 20 74 65 73 74 20 62 65 68 61 76 69 6f 72 20 62 test behavior b
ecc90 75 69 6c 74 20 69 6e 20 2a 2f 0a 73 74 61 74 69 uilt in */.stati
ecca0 63 20 69 6e 74 20 70 72 6f 78 79 47 65 6e 65 72 c int proxyGener
eccb0 61 74 65 48 6f 73 74 49 44 28 63 68 61 72 20 2a ateHostID(char *
eccc0 70 48 6f 73 74 49 44 29 7b 0a 20 20 69 6e 74 20 pHostID){. int
eccd0 70 69 64 2c 20 66 64 2c 20 6c 65 6e 3b 0a 20 20 pid, fd, len;.
ecce0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 6b unsigned char *k
eccf0 65 79 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 ey = (unsigned c
ecd00 68 61 72 20 2a 29 70 48 6f 73 74 49 44 3b 0a 20 har *)pHostID;.
ecd10 20 0a 20 20 6d 65 6d 73 65 74 28 6b 65 79 2c 20 . memset(key,
ecd20 30 2c 20 48 4f 53 54 49 44 4c 45 4e 29 3b 0a 20 0, HOSTIDLEN);.
ecd30 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 66 64 20 3d len = 0;. fd =
ecd40 20 6f 70 65 6e 28 22 2f 64 65 76 2f 75 72 61 6e open("/dev/uran
ecd50 64 6f 6d 22 2c 20 4f 5f 52 44 4f 4e 4c 59 29 3b dom", O_RDONLY);
ecd60 0a 20 20 69 66 28 20 66 64 3e 3d 30 20 29 7b 0a . if( fd>=0 ){.
ecd70 20 20 20 20 6c 65 6e 20 3d 20 72 65 61 64 28 66 len = read(f
ecd80 64 2c 20 6b 65 79 2c 20 48 4f 53 54 49 44 4c 45 d, key, HOSTIDLE
ecd90 4e 29 3b 0a 20 20 20 20 63 6c 6f 73 65 28 66 64 N);. close(fd
ecda0 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c ); /* silently l
ecdb0 65 61 6b 20 74 68 65 20 66 64 20 69 66 20 69 74 eak the fd if it
ecdc0 20 66 61 69 6c 73 20 2a 2f 0a 20 20 7d 0a 20 20 fails */. }.
ecdd0 69 66 28 20 6c 65 6e 20 3c 20 48 4f 53 54 49 44 if( len < HOSTID
ecde0 4c 45 4e 20 29 7b 0a 20 20 20 20 74 69 6d 65 5f LEN ){. time_
ecdf0 74 20 74 3b 0a 20 20 20 20 74 69 6d 65 28 26 74 t t;. time(&t
ece00 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 6b 65 );. memcpy(ke
ece10 79 2c 20 26 74 2c 20 73 69 7a 65 6f 66 28 74 29 y, &t, sizeof(t)
ece20 29 3b 0a 20 20 20 20 70 69 64 20 3d 20 67 65 74 );. pid = get
ece30 70 69 64 28 29 3b 0a 20 20 20 20 6d 65 6d 63 70 pid();. memcp
ece40 79 28 26 6b 65 79 5b 73 69 7a 65 6f 66 28 74 29 y(&key[sizeof(t)
ece50 5d 2c 20 26 70 69 64 2c 20 73 69 7a 65 6f 66 28 ], &pid, sizeof(
ece60 70 69 64 29 29 3b 0a 20 20 7d 0a 20 20 0a 23 69 pid));. }. .#i
ece70 66 64 65 66 20 4d 41 4b 45 5f 50 52 45 54 54 59 fdef MAKE_PRETTY
ece80 5f 48 4f 53 54 49 44 0a 20 20 7b 0a 20 20 20 20 _HOSTID. {.
ece90 69 6e 74 20 69 3b 0a 20 20 20 20 2f 2a 20 66 69 int i;. /* fi
ecea0 6c 74 65 72 20 74 68 65 20 62 79 74 65 73 20 69 lter the bytes i
eceb0 6e 74 6f 20 70 72 69 6e 74 61 62 6c 65 20 61 73 nto printable as
ecec0 63 69 69 20 63 68 61 72 61 63 74 65 72 73 20 61 cii characters a
eced0 6e 64 20 4e 55 4c 20 74 65 72 6d 69 6e 61 74 65 nd NUL terminate
ecee0 20 2a 2f 0a 20 20 20 20 6b 65 79 5b 28 48 4f 53 */. key[(HOS
ecef0 54 49 44 4c 45 4e 2d 31 29 5d 20 3d 20 30 78 30 TIDLEN-1)] = 0x0
ecf00 30 3b 0a 20 20 20 20 66 6f 72 28 20 69 3d 30 3b 0;. for( i=0;
ecf10 20 69 3c 28 48 4f 53 54 49 44 4c 45 4e 2d 31 29 i<(HOSTIDLEN-1)
ecf20 3b 20 69 2b 2b 20 29 7b 0a 20 20 20 20 20 20 75 ; i++ ){. u
ecf30 6e 73 69 67 6e 65 64 20 63 68 61 72 20 70 61 20 nsigned char pa
ecf40 3d 20 6b 65 79 5b 69 5d 26 30 78 37 46 3b 0a 20 = key[i]&0x7F;.
ecf50 20 20 20 20 20 69 66 28 20 70 61 3c 30 78 32 30 if( pa<0x20
ecf60 20 29 7b 0a 20 20 20 20 20 20 20 20 6b 65 79 5b ){. key[
ecf70 69 5d 20 3d 20 28 6b 65 79 5b 69 5d 26 30 78 38 i] = (key[i]&0x8
ecf80 30 20 3d 3d 20 30 78 38 30 29 20 3f 20 70 61 2b 0 == 0x80) ? pa+
ecf90 30 78 34 30 20 3a 20 70 61 2b 30 78 32 30 3b 0a 0x40 : pa+0x20;.
ecfa0 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
ecfb0 70 61 3d 3d 30 78 37 46 20 29 7b 0a 20 20 20 20 pa==0x7F ){.
ecfc0 20 20 20 20 6b 65 79 5b 69 5d 20 3d 20 28 6b 65 key[i] = (ke
ecfd0 79 5b 69 5d 26 30 78 38 30 20 3d 3d 20 30 78 38 y[i]&0x80 == 0x8
ecfe0 30 29 20 3f 20 70 61 3d 30 78 32 30 20 3a 20 70 0) ? pa=0x20 : p
ecff0 61 2b 30 78 37 45 3b 0a 20 20 20 20 20 20 7d 0a a+0x7E;. }.
ed000 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 }. }.#endif
ed010 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
ed020 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 77 72 69 74 65 _OK;.}../* write
ed030 73 20 74 68 65 20 68 6f 73 74 20 69 64 20 70 61 s the host id pa
ed040 74 68 20 74 6f 20 70 61 74 68 2c 20 70 61 74 68 th to path, path
ed050 20 73 68 6f 75 6c 64 20 62 65 20 61 6e 20 70 72 should be an pr
ed060 65 2d 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 e-allocated buff
ed070 65 72 0a 2a 2a 20 77 69 74 68 20 65 6e 6f 75 67 er.** with enoug
ed080 68 20 73 70 61 63 65 20 66 6f 72 20 61 20 70 61 h space for a pa
ed090 74 68 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f th .*/.static vo
ed0a0 69 64 20 70 72 6f 78 79 47 65 74 48 6f 73 74 49 id proxyGetHostI
ed0b0 44 50 61 74 68 28 63 68 61 72 20 2a 70 61 74 68 DPath(char *path
ed0c0 2c 20 73 69 7a 65 5f 74 20 6c 65 6e 29 7b 0a 20 , size_t len){.
ed0d0 20 73 74 72 6c 63 70 79 28 70 61 74 68 2c 20 48 strlcpy(path, H
ed0e0 4f 53 54 49 44 50 41 54 48 2c 20 6c 65 6e 29 3b OSTIDPATH, len);
ed0f0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
ed100 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 EST. if( sqlite
ed110 33 5f 68 6f 73 74 69 64 5f 6e 75 6d 3e 30 20 29 3_hostid_num>0 )
ed120 7b 0a 20 20 20 20 63 68 61 72 20 73 75 66 66 69 {. char suffi
ed130 78 5b 32 5d 20 3d 20 22 31 22 3b 0a 20 20 20 20 x[2] = "1";.
ed140 73 75 66 66 69 78 5b 30 5d 20 3d 20 73 75 66 66 suffix[0] = suff
ed150 69 78 5b 30 5d 20 2b 20 73 71 6c 69 74 65 33 5f ix[0] + sqlite3_
ed160 68 6f 73 74 69 64 5f 6e 75 6d 3b 0a 20 20 20 20 hostid_num;.
ed170 73 74 72 6c 63 61 74 28 70 61 74 68 2c 20 73 75 strlcat(path, su
ed180 66 66 69 78 2c 20 6c 65 6e 29 3b 0a 20 20 7d 0a ffix, len);. }.
ed190 23 65 6e 64 69 66 0a 20 20 4f 53 54 52 41 43 45 #endif. OSTRACE
ed1a0 33 28 22 47 45 54 48 4f 53 54 49 44 50 41 54 48 3("GETHOSTIDPATH
ed1b0 20 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 %s pid=%d\n",
ed1c0 70 61 74 68 2c 20 67 65 74 70 69 64 28 29 29 3b path, getpid());
ed1d0 0a 7d 0a 0a 2f 2a 20 67 65 74 20 74 68 65 20 68 .}../* get the h
ed1e0 6f 73 74 20 49 44 20 66 72 6f 6d 20 61 20 73 71 ost ID from a sq
ed1f0 6c 69 74 65 20 68 6f 73 74 69 64 20 66 69 6c 65 lite hostid file
ed200 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 0a stored in the .
ed210 2a 2a 20 75 73 65 72 2d 73 70 65 63 69 66 69 63 ** user-specific
ed220 20 74 6d 70 20 64 69 72 65 63 74 6f 72 79 2c 20 tmp directory,
ed230 63 72 65 61 74 65 20 74 68 65 20 49 44 20 69 66 create the ID if
ed240 20 69 74 27 73 20 6e 6f 74 20 74 68 65 72 65 20 it's not there
ed250 61 6c 72 65 61 64 79 20 0a 2a 2f 0a 73 74 61 74 already .*/.stat
ed260 69 63 20 69 6e 74 20 70 72 6f 78 79 47 65 74 48 ic int proxyGetH
ed270 6f 73 74 49 44 28 63 68 61 72 20 2a 70 48 6f 73 ostID(char *pHos
ed280 74 49 44 2c 20 69 6e 74 20 2a 70 45 72 72 6f 72 tID, int *pError
ed290 29 7b 0a 20 20 69 6e 74 20 66 64 3b 0a 20 20 63 ){. int fd;. c
ed2a0 68 61 72 20 70 61 74 68 5b 4d 41 58 50 41 54 48 har path[MAXPATH
ed2b0 4c 45 4e 5d 3b 20 0a 20 20 73 69 7a 65 5f 74 20 LEN]; . size_t
ed2c0 6c 65 6e 3b 0a 20 20 69 6e 74 20 72 63 3d 53 51 len;. int rc=SQ
ed2d0 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 70 72 6f 78 LITE_OK;.. prox
ed2e0 79 47 65 74 48 6f 73 74 49 44 50 61 74 68 28 70 yGetHostIDPath(p
ed2f0 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e 29 ath, MAXPATHLEN)
ed300 3b 0a 20 20 2f 2a 20 74 72 79 20 74 6f 20 63 72 ;. /* try to cr
ed310 65 61 74 65 20 74 68 65 20 68 6f 73 74 20 49 44 eate the host ID
ed320 20 66 69 6c 65 2c 20 69 66 20 69 74 20 61 6c 72 file, if it alr
ed330 65 61 64 79 20 65 78 69 73 74 73 20 72 65 61 64 eady exists read
ed340 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 2a 2f the contents */
ed350 0a 20 20 66 64 20 3d 20 6f 70 65 6e 28 70 61 74 . fd = open(pat
ed360 68 2c 20 4f 5f 43 52 45 41 54 7c 4f 5f 57 52 4f h, O_CREAT|O_WRO
ed370 4e 4c 59 7c 4f 5f 45 58 43 4c 2c 20 30 36 34 34 NLY|O_EXCL, 0644
ed380 29 3b 0a 20 20 69 66 28 20 66 64 3c 30 20 29 7b );. if( fd<0 ){
ed390 0a 20 20 20 20 69 6e 74 20 65 72 72 3d 65 72 72 . int err=err
ed3a0 6e 6f 3b 0a 09 09 0a 20 20 20 20 69 66 28 20 65 no;.... if( e
ed3b0 72 72 21 3d 45 45 58 49 53 54 20 29 7b 0a 23 69 rr!=EEXIST ){.#i
ed3c0 66 64 65 66 20 53 51 4c 49 54 45 5f 50 52 4f 58 fdef SQLITE_PROX
ed3d0 59 5f 44 45 42 55 47 20 2f 2a 20 73 65 74 20 74 Y_DEBUG /* set t
ed3e0 68 65 20 73 71 6c 69 74 65 20 65 72 72 6f 72 20 he sqlite error
ed3f0 6d 65 73 73 61 67 65 20 69 6e 73 74 65 61 64 20 message instead
ed400 2a 2f 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 */. fprintf
ed410 28 73 74 64 65 72 72 2c 20 22 73 71 6c 69 74 65 (stderr, "sqlite
ed420 20 65 72 72 6f 72 20 63 72 65 61 74 69 6e 67 20 error creating
ed430 68 6f 73 74 20 49 44 20 66 69 6c 65 20 25 73 3a host ID file %s:
ed440 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 %s\n",.
ed450 20 20 20 20 20 20 70 61 74 68 2c 20 73 74 72 65 path, stre
ed460 72 72 6f 72 28 65 72 72 29 29 3b 0a 23 65 6e 64 rror(err));.#end
ed470 69 66 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 if. return
ed480 53 51 4c 49 54 45 5f 50 45 52 4d 3b 0a 20 20 20 SQLITE_PERM;.
ed490 20 7d 0a 20 20 20 20 2f 2a 20 63 6f 75 6c 64 6e }. /* couldn
ed4a0 27 74 20 63 72 65 61 74 65 20 74 68 65 20 66 69 't create the fi
ed4b0 6c 65 2c 20 72 65 61 64 20 69 74 20 69 6e 73 74 le, read it inst
ed4c0 65 61 64 20 2a 2f 0a 20 20 20 20 66 64 20 3d 20 ead */. fd =
ed4d0 6f 70 65 6e 28 70 61 74 68 2c 20 4f 5f 52 44 4f open(path, O_RDO
ed4e0 4e 4c 59 7c 4f 5f 45 58 43 4c 29 3b 0a 20 20 20 NLY|O_EXCL);.
ed4f0 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 23 69 66 if( fd<0 ){.#if
ed500 64 65 66 20 53 51 4c 49 54 45 5f 50 52 4f 58 59 def SQLITE_PROXY
ed510 5f 44 45 42 55 47 20 2f 2a 20 73 65 74 20 74 68 _DEBUG /* set th
ed520 65 20 73 71 6c 69 74 65 20 65 72 72 6f 72 20 6d e sqlite error m
ed530 65 73 73 61 67 65 20 69 6e 73 74 65 61 64 20 2a essage instead *
ed540 2f 0a 20 20 20 20 20 20 69 6e 74 20 65 72 72 20 /. int err
ed550 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 66 = errno;. f
ed560 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 printf(stderr, "
ed570 73 71 6c 69 74 65 20 65 72 72 6f 72 20 6f 70 65 sqlite error ope
ed580 6e 69 6e 67 20 68 6f 73 74 20 49 44 20 66 69 6c ning host ID fil
ed590 65 20 25 73 3a 20 25 73 5c 6e 22 2c 0a 20 20 20 e %s: %s\n",.
ed5a0 20 20 20 20 20 20 20 20 20 20 20 70 61 74 68 2c path,
ed5b0 20 73 74 72 65 72 72 6f 72 28 65 72 72 29 29 3b strerror(err));
ed5c0 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 72 65 .#endif. re
ed5d0 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 45 52 4d turn SQLITE_PERM
ed5e0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6c 65 6e 20 ;. }. len
ed5f0 3d 20 70 72 65 61 64 28 66 64 2c 20 70 48 6f 73 = pread(fd, pHos
ed600 74 49 44 2c 20 48 4f 53 54 49 44 4c 45 4e 2c 20 tID, HOSTIDLEN,
ed610 30 29 3b 0a 20 20 20 20 69 66 28 20 6c 65 6e 3c 0);. if( len<
ed620 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 45 72 72 0 ){. *pErr
ed630 6f 72 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 or = errno;.
ed640 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f rc = SQLITE_IO
ed650 45 52 52 5f 52 45 41 44 3b 0a 20 20 20 20 7d 65 ERR_READ;. }e
ed660 6c 73 65 20 69 66 28 20 6c 65 6e 3c 48 4f 53 54 lse if( len<HOST
ed670 49 44 4c 45 4e 20 29 7b 0a 20 20 20 20 20 20 2a IDLEN ){. *
ed680 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 20 20 pError = 0;.
ed690 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f rc = SQLITE_IO
ed6a0 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a ERR_SHORT_READ;.
ed6b0 20 20 20 20 7d 0a 20 20 20 20 63 6c 6f 73 65 28 }. close(
ed6c0 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 fd); /* silently
ed6d0 20 6c 65 61 6b 20 74 68 65 20 66 64 20 69 66 20 leak the fd if
ed6e0 69 74 20 66 61 69 6c 73 20 2a 2f 0a 20 20 20 20 it fails */.
ed6f0 4f 53 54 52 41 43 45 33 28 22 47 45 54 48 4f 53 OSTRACE3("GETHOS
ed700 54 49 44 20 20 72 65 61 64 20 25 73 20 70 69 64 TID read %s pid
ed710 3d 25 64 5c 6e 22 2c 20 70 48 6f 73 74 49 44 2c =%d\n", pHostID,
ed720 20 67 65 74 70 69 64 28 29 29 3b 0a 20 20 20 20 getpid());.
ed730 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 65 6c return rc;. }el
ed740 73 65 7b 0a 20 20 20 20 2f 2a 20 77 65 27 72 65 se{. /* we're
ed750 20 63 72 65 61 74 69 6e 67 20 74 68 65 20 68 6f creating the ho
ed760 73 74 20 49 44 20 66 69 6c 65 20 28 75 73 65 20 st ID file (use
ed770 61 20 72 61 6e 64 6f 6d 20 73 74 72 69 6e 67 20 a random string
ed780 6f 66 20 62 79 74 65 73 29 20 2a 2f 0a 20 20 20 of bytes) */.
ed790 20 70 72 6f 78 79 47 65 6e 65 72 61 74 65 48 6f proxyGenerateHo
ed7a0 73 74 49 44 28 70 48 6f 73 74 49 44 29 3b 0a 20 stID(pHostID);.
ed7b0 20 20 20 6c 65 6e 20 3d 20 70 77 72 69 74 65 28 len = pwrite(
ed7c0 66 64 2c 20 70 48 6f 73 74 49 44 2c 20 48 4f 53 fd, pHostID, HOS
ed7d0 54 49 44 4c 45 4e 2c 20 30 29 3b 0a 20 20 20 20 TIDLEN, 0);.
ed7e0 69 66 28 20 6c 65 6e 3c 30 20 29 7b 0a 20 20 20 if( len<0 ){.
ed7f0 20 20 20 2a 70 45 72 72 6f 72 20 3d 20 65 72 72 *pError = err
ed800 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 no;. rc = S
ed810 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52 49 54 QLITE_IOERR_WRIT
ed820 45 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 E;. }else if(
ed830 20 6c 65 6e 3c 48 4f 53 54 49 44 4c 45 4e 20 29 len<HOSTIDLEN )
ed840 7b 0a 20 20 20 20 20 20 2a 70 45 72 72 6f 72 20 {. *pError
ed850 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 = 0;. rc =
ed860 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52 49 SQLITE_IOERR_WRI
ed870 54 45 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 6c TE;. }. cl
ed880 6f 73 65 28 66 64 29 3b 20 2f 2a 20 73 69 6c 65 ose(fd); /* sile
ed890 6e 74 6c 79 20 6c 65 61 6b 20 74 68 65 20 66 64 ntly leak the fd
ed8a0 20 69 66 20 69 74 20 66 61 69 6c 73 20 2a 2f 0a if it fails */.
ed8b0 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 47 45 OSTRACE3("GE
ed8c0 54 48 4f 53 54 49 44 20 20 77 72 6f 74 65 20 25 THOSTID wrote %
ed8d0 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 48 6f s pid=%d\n", pHo
ed8e0 73 74 49 44 2c 20 67 65 74 70 69 64 28 29 29 3b stID, getpid());
ed8f0 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a . return rc;.
ed900 20 20 7d 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e }.}..static in
ed910 74 20 70 72 6f 78 79 47 65 74 4c 6f 63 6b 50 61 t proxyGetLockPa
ed920 74 68 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 64 th(const char *d
ed930 62 50 61 74 68 2c 20 63 68 61 72 20 2a 6c 50 61 bPath, char *lPa
ed940 74 68 2c 20 73 69 7a 65 5f 74 20 6d 61 78 4c 65 th, size_t maxLe
ed950 6e 29 7b 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 n){. int len;.
ed960 20 69 6e 74 20 64 62 4c 65 6e 3b 0a 20 20 69 6e int dbLen;. in
ed970 74 20 69 3b 0a 0a 23 69 66 64 65 66 20 4c 4f 43 t i;..#ifdef LOC
ed980 4b 50 52 4f 58 59 44 49 52 0a 20 20 6c 65 6e 20 KPROXYDIR. len
ed990 3d 20 73 74 72 6c 63 70 79 28 6c 50 61 74 68 2c = strlcpy(lPath,
ed9a0 20 4c 4f 43 4b 50 52 4f 58 59 44 49 52 2c 20 6d LOCKPROXYDIR, m
ed9b0 61 78 4c 65 6e 29 3b 0a 23 65 6c 73 65 0a 23 20 axLen);.#else.#
ed9c0 69 66 64 65 66 20 5f 43 53 5f 44 41 52 57 49 4e ifdef _CS_DARWIN
ed9d0 5f 55 53 45 52 5f 54 45 4d 50 5f 44 49 52 0a 20 _USER_TEMP_DIR.
ed9e0 20 7b 0a 20 20 20 20 63 6f 6e 66 73 74 72 28 5f {. confstr(_
ed9f0 43 53 5f 44 41 52 57 49 4e 5f 55 53 45 52 5f 54 CS_DARWIN_USER_T
eda00 45 4d 50 5f 44 49 52 2c 20 6c 50 61 74 68 2c 20 EMP_DIR, lPath,
eda10 6d 61 78 4c 65 6e 29 3b 0a 20 20 20 20 6c 65 6e maxLen);. len
eda20 20 3d 20 73 74 72 6c 63 61 74 28 6c 50 61 74 68 = strlcat(lPath
eda30 2c 20 22 73 71 6c 69 74 65 70 6c 6f 63 6b 73 22 , "sqliteplocks"
eda40 2c 20 6d 61 78 4c 65 6e 29 3b 0a 20 20 20 20 69 , maxLen);. i
eda50 66 28 20 6d 6b 64 69 72 28 6c 50 61 74 68 2c 20 f( mkdir(lPath,
eda60 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 SQLITE_DEFAULT_P
eda70 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 53 49 ROXYDIR_PERMISSI
eda80 4f 4e 53 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a ONS) ){. /*
eda90 20 69 66 20 6d 6b 64 69 72 20 66 61 69 6c 73 2c if mkdir fails,
edaa0 20 68 61 6e 64 6c 65 20 61 73 20 6c 6f 63 6b 20 handle as lock
edab0 66 69 6c 65 20 63 72 65 61 74 69 6f 6e 20 66 61 file creation fa
edac0 69 6c 75 72 65 20 2a 2f 0a 23 20 20 69 66 64 65 ilure */.# ifde
edad0 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 f SQLITE_DEBUG.
edae0 20 20 20 20 20 69 6e 74 20 65 72 72 20 3d 20 65 int err = e
edaf0 72 72 6e 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 rrno;. if(
edb00 65 72 72 21 3d 45 45 58 49 53 54 20 29 7b 0a 20 err!=EEXIST ){.
edb10 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 fprintf(s
edb20 74 64 65 72 72 2c 20 22 70 72 6f 78 79 47 65 74 tderr, "proxyGet
edb30 4c 6f 63 6b 50 61 74 68 3a 20 6d 6b 64 69 72 28 LockPath: mkdir(
edb40 25 73 2c 30 25 6f 29 20 65 72 72 6f 72 20 25 64 %s,0%o) error %d
edb50 20 25 73 5c 6e 22 2c 20 6c 50 61 74 68 2c 0a 20 %s\n", lPath,.
edb60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 S
edb70 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 52 QLITE_DEFAULT_PR
edb80 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 53 49 4f OXYDIR_PERMISSIO
edb90 4e 53 2c 20 65 72 72 2c 20 73 74 72 65 72 72 6f NS, err, strerro
edba0 72 28 65 72 72 29 29 3b 0a 20 20 20 20 20 20 7d r(err));. }
edbb0 0a 23 20 20 65 6e 64 69 66 0a 20 20 20 20 7d 65 .# endif. }e
edbc0 6c 73 65 7b 0a 20 20 20 20 20 20 4f 53 54 52 41 lse{. OSTRA
edbd0 43 45 33 28 22 47 45 54 4c 4f 43 4b 50 41 54 48 CE3("GETLOCKPATH
edbe0 20 20 6d 6b 64 69 72 20 25 73 20 70 69 64 3d 25 mkdir %s pid=%
edbf0 64 5c 6e 22 2c 20 6c 50 61 74 68 2c 20 67 65 74 d\n", lPath, get
edc00 70 69 64 28 29 29 3b 0a 20 20 20 20 7d 0a 20 20 pid());. }.
edc10 20 20 0a 20 20 7d 0a 23 20 65 6c 73 65 0a 20 20 . }.# else.
edc20 6c 65 6e 20 3d 20 73 74 72 6c 63 70 79 28 6c 50 len = strlcpy(lP
edc30 61 74 68 2c 20 22 2f 74 6d 70 2f 22 2c 20 6d 61 ath, "/tmp/", ma
edc40 78 4c 65 6e 29 3b 0a 23 20 65 6e 64 69 66 0a 23 xLen);.# endif.#
edc50 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 6c 50 61 endif.. if( lPa
edc60 74 68 5b 6c 65 6e 2d 31 5d 21 3d 27 2f 27 20 29 th[len-1]!='/' )
edc70 7b 0a 20 20 20 20 6c 65 6e 20 3d 20 73 74 72 6c {. len = strl
edc80 63 61 74 28 6c 50 61 74 68 2c 20 22 2f 22 2c 20 cat(lPath, "/",
edc90 6d 61 78 4c 65 6e 29 3b 0a 20 20 7d 0a 20 20 0a maxLen);. }. .
edca0 20 20 2f 2a 20 74 72 61 6e 73 66 6f 72 6d 20 74 /* transform t
edcb0 68 65 20 64 62 20 70 61 74 68 20 74 6f 20 61 20 he db path to a
edcc0 75 6e 69 71 75 65 20 63 61 63 68 65 20 6e 61 6d unique cache nam
edcd0 65 20 2a 2f 0a 20 20 64 62 4c 65 6e 20 3d 20 28 e */. dbLen = (
edce0 69 6e 74 29 73 74 72 6c 65 6e 28 64 62 50 61 74 int)strlen(dbPat
edcf0 68 29 3b 0a 20 20 66 6f 72 28 20 69 3d 30 3b 20 h);. for( i=0;
edd00 69 3c 64 62 4c 65 6e 20 26 26 20 28 69 2b 6c 65 i<dbLen && (i+le
edd10 6e 2b 37 29 3c 6d 61 78 4c 65 6e 3b 20 69 2b 2b n+7)<maxLen; i++
edd20 29 7b 0a 20 20 20 20 63 68 61 72 20 63 20 3d 20 ){. char c =
edd30 64 62 50 61 74 68 5b 69 5d 3b 0a 20 20 20 20 6c dbPath[i];. l
edd40 50 61 74 68 5b 69 2b 6c 65 6e 5d 20 3d 20 28 63 Path[i+len] = (c
edd50 3d 3d 27 2f 27 29 3f 27 5f 27 3a 63 3b 0a 20 20 =='/')?'_':c;.
edd60 7d 0a 20 20 6c 50 61 74 68 5b 69 2b 6c 65 6e 5d }. lPath[i+len]
edd70 3d 27 5c 30 27 3b 0a 20 20 73 74 72 6c 63 61 74 ='\0';. strlcat
edd80 28 6c 50 61 74 68 2c 20 22 3a 61 75 74 6f 3a 22 (lPath, ":auto:"
edd90 2c 20 6d 61 78 4c 65 6e 29 3b 0a 20 20 72 65 74 , maxLen);. ret
edda0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
eddb0 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 ../*.** Create a
eddc0 20 6e 65 77 20 56 46 53 20 66 69 6c 65 20 64 65 new VFS file de
eddd0 73 63 72 69 70 74 6f 72 20 28 73 74 6f 72 65 64 scriptor (stored
edde0 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 in memory obtai
eddf0 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 ned from.** sqli
ede00 74 65 33 5f 6d 61 6c 6c 6f 63 29 20 61 6e 64 20 te3_malloc) and
ede10 6f 70 65 6e 20 74 68 65 20 66 69 6c 65 20 6e 61 open the file na
ede20 6d 65 64 20 22 70 61 74 68 22 20 69 6e 20 74 68 med "path" in th
ede30 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f e file descripto
ede40 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c r..**.** The cal
ede50 6c 65 72 20 69 73 20 72 65 73 70 6f 6e 73 69 62 ler is responsib
ede60 6c 65 20 6e 6f 74 20 6f 6e 6c 79 20 66 6f 72 20 le not only for
ede70 63 6c 6f 73 69 6e 67 20 74 68 65 20 66 69 6c 65 closing the file
ede80 20 64 65 73 63 72 69 70 74 6f 72 0a 2a 2a 20 62 descriptor.** b
ede90 75 74 20 61 6c 73 6f 20 66 6f 72 20 66 72 65 65 ut also for free
edea0 69 6e 67 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 ing the memory a
edeb0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
edec0 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 he file descript
eded0 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e or..*/.static in
edee0 74 20 70 72 6f 78 79 43 72 65 61 74 65 55 6e 69 t proxyCreateUni
edef0 78 46 69 6c 65 28 63 6f 6e 73 74 20 63 68 61 72 xFile(const char
edf00 20 2a 70 61 74 68 2c 20 75 6e 69 78 46 69 6c 65 *path, unixFile
edf10 20 2a 2a 70 70 46 69 6c 65 29 20 7b 0a 20 20 75 **ppFile) {. u
edf20 6e 69 78 46 69 6c 65 20 2a 70 4e 65 77 3b 0a 20 nixFile *pNew;.
edf30 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 53 51 4c int flags = SQL
edf40 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 ITE_OPEN_MAIN_DB
edf50 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 |SQLITE_OPEN_CRE
edf60 41 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f ATE|SQLITE_OPEN_
edf70 52 45 41 44 57 52 49 54 45 3b 0a 20 20 69 6e 74 READWRITE;. int
edf80 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
edf90 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 64 . sqlite3_vfs d
edfa0 75 6d 6d 79 56 66 73 3b 0a 0a 20 20 70 4e 65 77 ummyVfs;.. pNew
edfb0 20 3d 20 28 75 6e 69 78 46 69 6c 65 20 2a 29 73 = (unixFile *)s
edfc0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 qlite3_malloc(si
edfd0 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65 29 29 3b zeof(unixFile));
edfe0 0a 20 20 69 66 28 20 21 70 4e 65 77 20 29 7b 0a . if( !pNew ){.
edff0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
ee000 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d E_NOMEM;. }. m
ee010 65 6d 73 65 74 28 70 4e 65 77 2c 20 30 2c 20 73 emset(pNew, 0, s
ee020 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65 29 29 izeof(unixFile))
ee030 3b 0a 0a 20 20 2f 2a 20 43 61 6c 6c 20 75 6e 69 ;.. /* Call uni
ee040 78 4f 70 65 6e 28 29 20 74 6f 20 6f 70 65 6e 20 xOpen() to open
ee050 74 68 65 20 70 72 6f 78 79 20 66 69 6c 65 2e 20 the proxy file.
ee060 54 68 65 20 66 6c 61 67 73 20 70 61 73 73 65 64 The flags passed
ee070 20 74 6f 20 75 6e 69 78 4f 70 65 6e 28 29 0a 20 to unixOpen().
ee080 20 2a 2a 20 73 75 67 67 65 73 74 20 74 68 61 74 ** suggest that
ee090 20 74 68 65 20 66 69 6c 65 20 62 65 69 6e 67 20 the file being
ee0a0 6f 70 65 6e 65 64 20 69 73 20 61 20 22 6d 61 69 opened is a "mai
ee0b0 6e 20 64 61 74 61 62 61 73 65 22 2e 20 54 68 69 n database". Thi
ee0c0 73 20 69 73 0a 20 20 2a 2a 20 6e 65 63 65 73 73 s is. ** necess
ee0d0 61 72 79 20 61 73 20 6f 74 68 65 72 20 66 69 6c ary as other fil
ee0e0 65 20 74 79 70 65 73 20 64 6f 20 6e 6f 74 20 6e e types do not n
ee0f0 65 63 65 73 73 61 72 69 6c 79 20 73 75 70 70 6f ecessarily suppo
ee100 72 74 20 6c 6f 63 6b 69 6e 67 2e 20 49 74 0a 20 rt locking. It.
ee110 20 2a 2a 20 69 73 20 62 65 74 74 65 72 20 74 6f ** is better to
ee120 20 75 73 65 20 75 6e 69 78 4f 70 65 6e 28 29 20 use unixOpen()
ee130 69 6e 73 74 65 61 64 20 6f 66 20 6f 70 65 6e 69 instead of openi
ee140 6e 67 20 74 68 65 20 66 69 6c 65 20 64 69 72 65 ng the file dire
ee150 63 74 6c 79 20 77 69 74 68 0a 20 20 2a 2a 20 6f ctly with. ** o
ee160 70 65 6e 28 29 2c 20 61 73 20 75 6e 69 78 4f 70 pen(), as unixOp
ee170 65 6e 28 29 20 73 65 74 73 20 75 70 20 74 68 65 en() sets up the
ee180 20 76 61 72 69 6f 75 73 20 6d 65 63 68 61 6e 69 various mechani
ee190 73 6d 73 20 72 65 71 75 69 72 65 64 20 74 6f 0a sms required to.
ee1a0 20 20 2a 2a 20 6d 61 6b 65 20 73 75 72 65 20 61 ** make sure a
ee1b0 20 63 61 6c 6c 20 74 6f 20 63 6c 6f 73 65 28 29 call to close()
ee1c0 20 64 6f 65 73 20 6e 6f 74 20 63 61 75 73 65 20 does not cause
ee1d0 74 68 65 20 73 79 73 74 65 6d 20 74 6f 20 64 69 the system to di
ee1e0 73 63 61 72 64 0a 20 20 2a 2a 20 50 4f 53 49 58 scard. ** POSIX
ee1f0 20 6c 6f 63 6b 73 20 70 72 65 6d 61 74 75 72 65 locks premature
ee200 6c 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 74 ly.. **. ** It
ee210 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 is important th
ee220 61 74 20 74 68 65 20 78 4f 70 65 6e 20 6d 65 6d at the xOpen mem
ee230 62 65 72 20 6f 66 20 74 68 65 20 56 46 53 20 6f ber of the VFS o
ee240 62 6a 65 63 74 20 70 61 73 73 65 64 20 74 6f 20 bject passed to
ee250 0a 20 20 2a 2a 20 75 6e 69 78 4f 70 65 6e 28 29 . ** unixOpen()
ee260 20 69 73 20 4e 55 4c 4c 2e 20 54 68 69 73 20 74 is NULL. This t
ee270 65 6c 6c 73 20 75 6e 69 78 4f 70 65 6e 28 29 20 ells unixOpen()
ee280 6d 61 79 20 74 72 79 20 74 6f 20 6f 70 65 6e 20 may try to open
ee290 61 20 70 72 6f 78 79 2d 66 69 6c 65 20 0a 20 20 a proxy-file .
ee2a0 2a 2a 20 66 6f 72 20 74 68 65 20 70 72 6f 78 79 ** for the proxy
ee2b0 2d 66 69 6c 65 20 28 63 72 65 61 74 69 6e 67 20 -file (creating
ee2c0 61 20 70 6f 74 65 6e 74 69 61 6c 20 69 6e 66 69 a potential infi
ee2d0 6e 69 74 65 20 6c 6f 6f 70 29 2e 0a 20 20 2a 2f nite loop).. */
ee2e0 0a 20 20 64 75 6d 6d 79 56 66 73 2e 70 41 70 70 . dummyVfs.pApp
ee2f0 44 61 74 61 20 3d 20 28 76 6f 69 64 2a 29 26 61 Data = (void*)&a
ee300 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 3b utolockIoFinder;
ee310 0a 20 20 64 75 6d 6d 79 56 66 73 2e 78 4f 70 65 . dummyVfs.xOpe
ee320 6e 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20 75 6e n = 0;. rc = un
ee330 69 78 4f 70 65 6e 28 26 64 75 6d 6d 79 56 66 73 ixOpen(&dummyVfs
ee340 2c 20 70 61 74 68 2c 20 28 73 71 6c 69 74 65 33 , path, (sqlite3
ee350 5f 66 69 6c 65 20 2a 29 70 4e 65 77 2c 20 66 6c _file *)pNew, fl
ee360 61 67 73 2c 20 26 66 6c 61 67 73 29 3b 0a 20 20 ags, &flags);.
ee370 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
ee380 4b 20 26 26 20 28 66 6c 61 67 73 26 53 51 4c 49 K && (flags&SQLI
ee390 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 TE_OPEN_READONLY
ee3a0 29 20 29 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 70 ) ){. pNew->p
ee3b0 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 28 Method->xClose((
ee3c0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 70 sqlite3_file *)p
ee3d0 4e 65 77 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 New);. rc = S
ee3e0 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a QLITE_CANTOPEN;.
ee3f0 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 21 3d 53 }.. if( rc!=S
ee400 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
ee410 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4e 65 sqlite3_free(pNe
ee420 77 29 3b 0a 20 20 20 20 70 4e 65 77 20 3d 20 30 w);. pNew = 0
ee430 3b 0a 20 20 7d 0a 0a 20 20 2a 70 70 46 69 6c 65 ;. }.. *ppFile
ee440 20 3d 20 70 4e 65 77 3b 0a 20 20 72 65 74 75 72 = pNew;. retur
ee450 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 74 61 6b 65 n rc;.}../* take
ee460 73 20 74 68 65 20 63 6f 6e 63 68 20 62 79 20 74 s the conch by t
ee470 61 6b 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c aking a shared l
ee480 6f 63 6b 20 61 6e 64 20 72 65 61 64 20 74 68 65 ock and read the
ee490 20 63 6f 6e 74 65 6e 74 73 20 63 6f 6e 63 68 2c contents conch,
ee4a0 20 69 66 20 0a 2a 2a 20 6c 6f 63 6b 50 61 74 68 if .** lockPath
ee4b0 20 69 73 20 6e 6f 6e 2d 4e 55 4c 4c 2c 20 74 68 is non-NULL, th
ee4c0 65 20 68 6f 73 74 20 49 44 20 61 6e 64 20 6c 6f e host ID and lo
ee4d0 63 6b 20 66 69 6c 65 20 70 61 74 68 20 6d 75 73 ck file path mus
ee4e0 74 20 6d 61 74 63 68 2e 20 20 41 20 4e 55 4c 4c t match. A NULL
ee4f0 20 0a 2a 2a 20 6c 6f 63 6b 50 61 74 68 20 6d 65 .** lockPath me
ee500 61 6e 73 20 74 68 61 74 20 74 68 65 20 6c 6f 63 ans that the loc
ee510 6b 50 61 74 68 20 69 6e 20 74 68 65 20 63 6f 6e kPath in the con
ee520 63 68 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 ch file will be
ee530 75 73 65 64 20 69 66 20 74 68 65 20 0a 2a 2a 20 used if the .**
ee540 68 6f 73 74 20 49 44 73 20 6d 61 74 63 68 2c 20 host IDs match,
ee550 6f 72 20 61 20 6e 65 77 20 6c 6f 63 6b 20 70 61 or a new lock pa
ee560 74 68 20 77 69 6c 6c 20 62 65 20 67 65 6e 65 72 th will be gener
ee570 61 74 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c ated automatical
ee580 6c 79 20 0a 2a 2a 20 61 6e 64 20 77 72 69 74 74 ly .** and writt
ee590 65 6e 20 74 6f 20 74 68 65 20 63 6f 6e 63 68 20 en to the conch
ee5a0 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 file..*/.static
ee5b0 69 6e 74 20 70 72 6f 78 79 54 61 6b 65 43 6f 6e int proxyTakeCon
ee5c0 63 68 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 ch(unixFile *pFi
ee5d0 6c 65 29 7b 0a 20 20 70 72 6f 78 79 4c 6f 63 6b le){. proxyLock
ee5e0 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 ingContext *pCtx
ee5f0 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 = (proxyLocking
ee600 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c 65 2d Context *)pFile-
ee610 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b >lockingContext;
ee620 20 0a 20 20 0a 20 20 69 66 28 20 70 43 74 78 2d . . if( pCtx-
ee630 3e 63 6f 6e 63 68 48 65 6c 64 3e 30 20 29 7b 0a >conchHeld>0 ){.
ee640 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
ee650 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 E_OK;. }else{.
ee660 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 6f 6e unixFile *con
ee670 63 68 46 69 6c 65 20 3d 20 70 43 74 78 2d 3e 63 chFile = pCtx->c
ee680 6f 6e 63 68 46 69 6c 65 3b 0a 20 20 20 20 63 68 onchFile;. ch
ee690 61 72 20 74 65 73 74 56 61 6c 75 65 5b 43 4f 4e ar testValue[CON
ee6a0 43 48 4c 45 4e 5d 3b 0a 20 20 20 20 63 68 61 72 CHLEN];. char
ee6b0 20 63 6f 6e 63 68 56 61 6c 75 65 5b 43 4f 4e 43 conchValue[CONC
ee6c0 48 4c 45 4e 5d 3b 0a 20 20 20 20 63 68 61 72 20 HLEN];. char
ee6d0 6c 6f 63 6b 50 61 74 68 5b 4d 41 58 50 41 54 48 lockPath[MAXPATH
ee6e0 4c 45 4e 5d 3b 0a 20 20 20 20 63 68 61 72 20 2a LEN];. char *
ee6f0 74 4c 6f 63 6b 50 61 74 68 20 3d 20 4e 55 4c 4c tLockPath = NULL
ee700 3b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 53 ;. int rc = S
ee710 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 69 6e QLITE_OK;. in
ee720 74 20 72 65 61 64 52 63 20 3d 20 53 51 4c 49 54 t readRc = SQLIT
ee730 45 5f 4f 4b 3b 0a 20 20 20 20 69 6e 74 20 73 79 E_OK;. int sy
ee740 6e 63 50 65 72 6d 73 20 3d 20 30 3b 0a 0a 20 20 ncPerms = 0;..
ee750 20 20 4f 53 54 52 41 43 45 34 28 22 54 41 4b 45 OSTRACE4("TAKE
ee760 43 4f 4e 43 48 20 20 25 64 20 66 6f 72 20 25 73 CONCH %d for %s
ee770 20 70 69 64 3d 25 64 5c 6e 22 2c 20 63 6f 6e 63 pid=%d\n", conc
ee780 68 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 hFile->h,.
ee790 20 20 20 20 20 20 20 28 70 43 74 78 2d 3e 6c 6f (pCtx->lo
ee7a0 63 6b 50 72 6f 78 79 50 61 74 68 20 3f 20 70 43 ckProxyPath ? pC
ee7b0 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 tx->lockProxyPat
ee7c0 68 20 3a 20 22 3a 61 75 74 6f 3a 22 29 2c 20 67 h : ":auto:"), g
ee7d0 65 74 70 69 64 28 29 29 3b 0a 0a 20 20 20 20 72 etpid());.. r
ee7e0 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 c = conchFile->p
ee7f0 4d 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28 73 Method->xLock((s
ee800 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e qlite3_file*)con
ee810 63 68 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 4c chFile, SHARED_L
ee820 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 72 63 OCK);. if( rc
ee830 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
ee840 20 20 20 20 20 69 6e 74 20 70 45 72 72 6f 72 20 int pError
ee850 3d 20 30 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65 = 0;. memse
ee860 74 28 74 65 73 74 56 61 6c 75 65 2c 20 30 2c 20 t(testValue, 0,
ee870 43 4f 4e 43 48 4c 45 4e 29 3b 20 2f 2a 20 63 6f CONCHLEN); /* co
ee880 6e 63 68 20 69 73 20 66 69 78 65 64 20 73 69 7a nch is fixed siz
ee890 65 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 e */. rc =
ee8a0 70 72 6f 78 79 47 65 74 48 6f 73 74 49 44 28 74 proxyGetHostID(t
ee8b0 65 73 74 56 61 6c 75 65 2c 20 26 70 45 72 72 6f estValue, &pErro
ee8c0 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28 72 r);. if( (r
ee8d0 63 26 30 78 66 66 29 3d 3d 53 51 4c 49 54 45 5f c&0xff)==SQLITE_
ee8e0 49 4f 45 52 52 20 29 7b 0a 20 20 20 20 20 20 20 IOERR ){.
ee8f0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
ee900 6f 20 3d 20 70 45 72 72 6f 72 3b 0a 20 20 20 20 o = pError;.
ee910 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 43 }. if( pC
ee920 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 tx->lockProxyPat
ee930 68 20 29 7b 0a 20 20 20 20 20 20 20 20 73 74 72 h ){. str
ee940 6c 63 70 79 28 26 74 65 73 74 56 61 6c 75 65 5b lcpy(&testValue[
ee950 48 4f 53 54 49 44 4c 45 4e 5d 2c 20 70 43 74 78 HOSTIDLEN], pCtx
ee960 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 2c ->lockProxyPath,
ee970 20 4d 41 58 50 41 54 48 4c 45 4e 29 3b 0a 20 20 MAXPATHLEN);.
ee980 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
ee990 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
ee9a0 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 K ){. goto
ee9b0 65 6e 64 5f 74 61 6b 65 63 6f 6e 63 68 3b 0a 20 end_takeconch;.
ee9c0 20 20 20 7d 0a 20 20 20 20 0a 20 20 20 20 72 65 }. . re
ee9d0 61 64 52 63 20 3d 20 75 6e 69 78 52 65 61 64 28 adRc = unixRead(
ee9e0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 (sqlite3_file *)
ee9f0 63 6f 6e 63 68 46 69 6c 65 2c 20 63 6f 6e 63 68 conchFile, conch
eea00 56 61 6c 75 65 2c 20 43 4f 4e 43 48 4c 45 4e 2c Value, CONCHLEN,
eea10 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 65 61 0);. if( rea
eea20 64 52 63 21 3d 53 51 4c 49 54 45 5f 49 4f 45 52 dRc!=SQLITE_IOER
eea30 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 29 7b 0a R_SHORT_READ ){.
eea40 20 20 20 20 20 20 69 66 28 20 72 65 61 64 52 63 if( readRc
eea50 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
eea60 20 20 20 20 20 20 20 69 66 28 20 28 72 63 26 30 if( (rc&0
eea70 78 66 66 29 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 xff)==SQLITE_IOE
eea80 52 52 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 RR ){.
eea90 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f pFile->lastErrno
eeaa0 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 6c 61 = conchFile->la
eeab0 73 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 stErrno;.
eeac0 20 7d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 }. rc =
eead0 72 65 61 64 52 63 3b 0a 20 20 20 20 20 20 20 20 readRc;.
eeae0 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65 63 6f 6e goto end_takecon
eeaf0 63 68 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ch;. }.
eeb00 20 20 2f 2a 20 69 66 20 74 68 65 20 63 6f 6e 63 /* if the conc
eeb10 68 20 68 61 73 20 64 61 74 61 20 63 6f 6d 70 61 h has data compa
eeb20 72 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 re the contents
eeb30 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 21 70 43 */. if( !pC
eeb40 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 tx->lockProxyPat
eeb50 68 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 h ){. /*
eeb60 66 6f 72 20 61 75 74 6f 2d 6e 61 6d 65 64 20 6c for auto-named l
eeb70 6f 63 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 2c 20 ocal lock file,
eeb80 6a 75 73 74 20 63 68 65 63 6b 20 74 68 65 20 68 just check the h
eeb90 6f 73 74 20 49 44 20 61 6e 64 20 77 65 27 6c 6c ost ID and we'll
eeba0 0a 20 20 20 20 20 20 20 20 20 2a 2a 20 75 73 65 . ** use
eebb0 20 74 68 65 20 6c 6f 63 61 6c 20 6c 6f 63 6b 20 the local lock
eebc0 66 69 6c 65 20 70 61 74 68 20 74 68 61 74 27 73 file path that's
eebd0 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 72 already in ther
eebe0 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 e */. if(
eebf0 20 21 6d 65 6d 63 6d 70 28 74 65 73 74 56 61 6c !memcmp(testVal
eec00 75 65 2c 20 63 6f 6e 63 68 56 61 6c 75 65 2c 20 ue, conchValue,
eec10 48 4f 53 54 49 44 4c 45 4e 29 20 29 7b 0a 20 20 HOSTIDLEN) ){.
eec20 20 20 20 20 20 20 20 20 74 4c 6f 63 6b 50 61 74 tLockPat
eec30 68 20 3d 20 28 63 68 61 72 20 2a 29 26 63 6f 6e h = (char *)&con
eec40 63 68 56 61 6c 75 65 5b 48 4f 53 54 49 44 4c 45 chValue[HOSTIDLE
eec50 4e 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f N];. go
eec60 74 6f 20 65 6e 64 5f 74 61 6b 65 63 6f 6e 63 68 to end_takeconch
eec70 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
eec80 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
eec90 20 2f 2a 20 77 65 27 76 65 20 67 6f 74 20 74 68 /* we've got th
eeca0 65 20 63 6f 6e 63 68 20 69 66 20 63 6f 6e 63 68 e conch if conch
eecb0 56 61 6c 75 65 20 6d 61 74 63 68 65 73 20 6f 75 Value matches ou
eecc0 72 20 70 61 74 68 20 61 6e 64 20 68 6f 73 74 20 r path and host
eecd0 49 44 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 ID */. if
eece0 28 20 21 6d 65 6d 63 6d 70 28 74 65 73 74 56 61 ( !memcmp(testVa
eecf0 6c 75 65 2c 20 63 6f 6e 63 68 56 61 6c 75 65 2c lue, conchValue,
eed00 20 43 4f 4e 43 48 4c 45 4e 29 20 29 7b 0a 20 20 CONCHLEN) ){.
eed10 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 goto end
eed20 5f 74 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 20 _takeconch;.
eed30 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
eed40 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f }else{. /
eed50 2a 20 61 20 73 68 6f 72 74 20 72 65 61 64 20 6d * a short read m
eed60 65 61 6e 73 20 77 65 27 72 65 20 22 63 72 65 61 eans we're "crea
eed70 74 69 6e 67 22 20 74 68 65 20 63 6f 6e 63 68 20 ting" the conch
eed80 28 65 76 65 6e 20 74 68 6f 75 67 68 20 69 74 20 (even though it
eed90 63 6f 75 6c 64 20 0a 20 20 20 20 20 20 2a 2a 20 could . **
eeda0 68 61 76 65 20 62 65 65 6e 20 75 73 65 72 2d 69 have been user-i
eedb0 6e 74 65 72 76 65 6e 74 69 6f 6e 29 2c 20 69 66 ntervention), if
eedc0 20 77 65 20 61 63 71 75 69 72 65 20 74 68 65 20 we acquire the
eedd0 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2c 0a exclusive lock,.
eede0 20 20 20 20 20 20 2a 2a 20 77 65 27 6c 6c 20 74 ** we'll t
eedf0 72 79 20 74 6f 20 6d 61 74 63 68 20 74 68 65 20 ry to match the
eee00 63 75 72 72 65 6e 74 20 6f 6e 2d 64 69 73 6b 20 current on-disk
eee10 70 65 72 6d 69 73 73 69 6f 6e 73 20 6f 66 20 74 permissions of t
eee20 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 he database.
eee30 20 20 2a 2f 0a 20 20 20 20 20 20 73 79 6e 63 50 */. syncP
eee40 65 72 6d 73 20 3d 20 31 3b 0a 20 20 20 20 7d 0a erms = 1;. }.
eee50 20 20 20 20 0a 20 20 20 20 2f 2a 20 65 69 74 68 . /* eith
eee60 65 72 20 63 6f 6e 63 68 20 77 61 73 20 65 6d 74 er conch was emt
eee70 70 79 20 6f 72 20 64 69 64 6e 27 74 20 6d 61 74 py or didn't mat
eee80 63 68 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 70 ch */. if( !p
eee90 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 Ctx->lockProxyPa
eeea0 74 68 20 29 7b 0a 20 20 20 20 20 20 70 72 6f 78 th ){. prox
eeeb0 79 47 65 74 4c 6f 63 6b 50 61 74 68 28 70 43 74 yGetLockPath(pCt
eeec0 78 2d 3e 64 62 50 61 74 68 2c 20 6c 6f 63 6b 50 x->dbPath, lockP
eeed0 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e 29 ath, MAXPATHLEN)
eeee0 3b 0a 20 20 20 20 20 20 74 4c 6f 63 6b 50 61 74 ;. tLockPat
eeef0 68 20 3d 20 6c 6f 63 6b 50 61 74 68 3b 0a 20 20 h = lockPath;.
eef00 20 20 20 20 73 74 72 6c 63 70 79 28 26 74 65 73 strlcpy(&tes
eef10 74 56 61 6c 75 65 5b 48 4f 53 54 49 44 4c 45 4e tValue[HOSTIDLEN
eef20 5d 2c 20 6c 6f 63 6b 50 61 74 68 2c 20 4d 41 58 ], lockPath, MAX
eef30 50 41 54 48 4c 45 4e 29 3b 0a 20 20 20 20 7d 0a PATHLEN);. }.
eef40 20 20 20 20 0a 20 20 20 20 2f 2a 20 75 70 64 61 . /* upda
eef50 74 65 20 63 6f 6e 63 68 20 77 69 74 68 20 68 6f te conch with ho
eef60 73 74 20 61 6e 64 20 70 61 74 68 20 28 74 68 69 st and path (thi
eef70 73 20 77 69 6c 6c 20 66 61 69 6c 20 69 66 20 6f s will fail if o
eef80 74 68 65 72 20 70 72 6f 63 65 73 73 0a 20 20 20 ther process.
eef90 20 20 2a 2a 20 68 61 73 20 61 20 73 68 61 72 65 ** has a share
eefa0 64 20 6c 6f 63 6b 20 61 6c 72 65 61 64 79 29 20 d lock already)
eefb0 2a 2f 0a 20 20 20 20 72 63 20 3d 20 63 6f 6e 63 */. rc = conc
eefc0 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e hFile->pMethod->
eefd0 78 4c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 xLock((sqlite3_f
eefe0 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20 ile*)conchFile,
eeff0 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b EXCLUSIVE_LOCK);
ef000 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
ef010 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
ef020 72 63 20 3d 20 75 6e 69 78 57 72 69 74 65 28 28 rc = unixWrite((
ef030 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 63 sqlite3_file *)c
ef040 6f 6e 63 68 46 69 6c 65 2c 20 74 65 73 74 56 61 onchFile, testVa
ef050 6c 75 65 2c 20 43 4f 4e 43 48 4c 45 4e 2c 20 30 lue, CONCHLEN, 0
ef060 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d );. if( rc=
ef070 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 73 79 =SQLITE_OK && sy
ef080 6e 63 50 65 72 6d 73 20 29 7b 0a 20 20 20 20 20 ncPerms ){.
ef090 20 20 20 73 74 72 75 63 74 20 73 74 61 74 20 62 struct stat b
ef0a0 75 66 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 uf;. int
ef0b0 65 72 72 20 3d 20 66 73 74 61 74 28 70 46 69 6c err = fstat(pFil
ef0c0 65 2d 3e 68 2c 20 26 62 75 66 29 3b 0a 20 20 20 e->h, &buf);.
ef0d0 20 20 20 20 20 69 66 28 20 65 72 72 3d 3d 30 20 if( err==0
ef0e0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ){. /*
ef0f0 74 72 79 20 74 6f 20 6d 61 74 63 68 20 74 68 65 try to match the
ef100 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 70 database file p
ef110 65 72 6d 69 73 73 69 6f 6e 73 2c 20 69 67 6e 6f ermissions, igno
ef120 72 65 20 66 61 69 6c 75 72 65 20 2a 2f 0a 23 69 re failure */.#i
ef130 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 50 52 4f fndef SQLITE_PRO
ef140 58 59 5f 44 45 42 55 47 0a 20 20 20 20 20 20 20 XY_DEBUG.
ef150 20 20 20 66 63 68 6d 6f 64 28 63 6f 6e 63 68 46 fchmod(conchF
ef160 69 6c 65 2d 3e 68 2c 20 62 75 66 2e 73 74 5f 6d ile->h, buf.st_m
ef170 6f 64 65 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 ode);.#else.
ef180 20 20 20 20 20 20 69 66 28 20 66 63 68 6d 6f 64 if( fchmod
ef190 28 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c 20 62 (conchFile->h, b
ef1a0 75 66 2e 73 74 5f 6d 6f 64 65 29 21 3d 30 20 29 uf.st_mode)!=0 )
ef1b0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e {. in
ef1c0 74 20 63 6f 64 65 20 3d 20 65 72 72 6e 6f 3b 0a t code = errno;.
ef1d0 20 20 20 20 20 20 20 20 20 20 20 20 66 70 72 69 fpri
ef1e0 6e 74 66 28 73 74 64 65 72 72 2c 20 22 66 63 68 ntf(stderr, "fch
ef1f0 6d 6f 64 20 25 6f 20 46 41 49 4c 45 44 20 77 69 mod %o FAILED wi
ef200 74 68 20 25 64 20 25 73 5c 6e 22 2c 0a 20 20 20 th %d %s\n",.
ef210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ef220 20 20 20 20 20 20 20 20 20 20 62 75 66 2e 73 74 buf.st
ef230 5f 6d 6f 64 65 2c 20 63 6f 64 65 2c 20 73 74 72 _mode, code, str
ef240 65 72 72 6f 72 28 63 6f 64 65 29 29 3b 0a 20 20 error(code));.
ef250 20 20 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b } else {
ef260 0a 20 20 20 20 20 20 20 20 20 20 20 20 66 70 72 . fpr
ef270 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 66 63 intf(stderr, "fc
ef280 68 6d 6f 64 20 25 6f 20 53 55 43 43 45 44 45 44 hmod %o SUCCEDED
ef290 5c 6e 22 2c 62 75 66 2e 73 74 5f 6d 6f 64 65 29 \n",buf.st_mode)
ef2a0 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
ef2b0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
ef2c0 20 20 20 20 20 20 20 69 6e 74 20 63 6f 64 65 20 int code
ef2d0 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 = errno;.
ef2e0 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 fprintf(stder
ef2f0 72 2c 20 22 53 54 41 54 20 46 41 49 4c 45 44 5b r, "STAT FAILED[
ef300 25 64 5d 20 77 69 74 68 20 25 64 20 25 73 5c 6e %d] with %d %s\n
ef310 22 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 ", .
ef320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 65 72 er
ef330 72 2c 20 63 6f 64 65 2c 20 73 74 72 65 72 72 6f r, code, strerro
ef340 72 28 63 6f 64 65 29 29 3b 0a 23 65 6e 64 69 66 r(code));.#endif
ef350 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
ef360 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 63 6f 6e }. }. con
ef370 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d chFile->pMethod-
ef380 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 6c 69 74 65 >xUnlock((sqlite
ef390 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 3_file*)conchFil
ef3a0 65 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b e, SHARED_LOCK);
ef3b0 0a 20 20 0a 65 6e 64 5f 74 61 6b 65 63 6f 6e 63 . .end_takeconc
ef3c0 68 3a 0a 20 20 20 20 4f 53 54 52 41 43 45 32 28 h:. OSTRACE2(
ef3d0 22 54 52 41 4e 53 50 52 4f 58 59 3a 20 43 4c 4f "TRANSPROXY: CLO
ef3e0 53 45 20 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 SE %d\n", pFile
ef3f0 2d 3e 68 29 3b 0a 20 20 20 20 69 66 28 20 72 63 ->h);. if( rc
ef400 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 ==SQLITE_OK && p
ef410 46 69 6c 65 2d 3e 6f 70 65 6e 46 6c 61 67 73 20 File->openFlags
ef420 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 46 69 ){. if( pFi
ef430 6c 65 2d 3e 68 3e 3d 30 20 29 7b 0a 23 69 66 64 le->h>=0 ){.#ifd
ef440 65 66 20 53 54 52 49 43 54 5f 43 4c 4f 53 45 5f ef STRICT_CLOSE_
ef450 45 52 52 4f 52 0a 20 20 20 20 20 20 20 20 69 66 ERROR. if
ef460 28 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 68 ( close(pFile->h
ef470 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 ) ){. p
ef480 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 File->lastErrno
ef490 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 = errno;.
ef4a0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
ef4b0 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 20 _IOERR_CLOSE;.
ef4c0 20 20 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 }.#else.
ef4d0 20 20 20 20 20 20 63 6c 6f 73 65 28 70 46 69 6c close(pFil
ef4e0 65 2d 3e 68 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 e->h); /* silent
ef4f0 6c 79 20 6c 65 61 6b 20 66 64 20 69 66 20 66 61 ly leak fd if fa
ef500 69 6c 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 20 il */.#endif.
ef510 20 20 20 7d 0a 20 20 20 20 20 20 70 46 69 6c 65 }. pFile
ef520 2d 3e 68 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 ->h = -1;.
ef530 69 6e 74 20 66 64 20 3d 20 6f 70 65 6e 28 70 43 int fd = open(pC
ef540 74 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c tx->dbPath, pFil
ef550 65 2d 3e 6f 70 65 6e 46 6c 61 67 73 2c 0a 20 20 e->openFlags,.
ef560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ef570 20 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 SQLITE_DEFAULT
ef580 5f 46 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f 4e _FILE_PERMISSION
ef590 53 29 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 S);. OSTRAC
ef5a0 45 32 28 22 54 52 41 4e 53 50 52 4f 58 59 3a 20 E2("TRANSPROXY:
ef5b0 4f 50 45 4e 20 20 25 64 5c 6e 22 2c 20 66 64 29 OPEN %d\n", fd)
ef5c0 3b 0a 20 20 20 20 20 20 69 66 28 20 66 64 3e 3d ;. if( fd>=
ef5d0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 0 ){. pFi
ef5e0 6c 65 2d 3e 68 20 3d 20 66 64 3b 0a 20 20 20 20 le->h = fd;.
ef5f0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
ef600 20 72 63 3d 53 51 4c 49 54 45 5f 43 41 4e 54 4f rc=SQLITE_CANTO
ef610 50 45 4e 3b 20 2f 2a 20 53 51 4c 49 54 45 5f 42 PEN; /* SQLITE_B
ef620 55 53 59 3f 20 70 72 6f 78 79 54 61 6b 65 43 6f USY? proxyTakeCo
ef630 6e 63 68 20 63 61 6c 6c 65 64 0a 20 20 20 20 20 nch called.
ef640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ef650 20 20 20 20 20 20 20 20 20 20 64 75 72 69 6e 67 during
ef660 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 locking */.
ef670 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 }. }. if
ef680 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
ef690 26 26 20 21 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 && !pCtx->lockPr
ef6a0 6f 78 79 20 29 7b 0a 20 20 20 20 20 20 63 68 61 oxy ){. cha
ef6b0 72 20 2a 70 61 74 68 20 3d 20 74 4c 6f 63 6b 50 r *path = tLockP
ef6c0 61 74 68 20 3f 20 74 4c 6f 63 6b 50 61 74 68 20 ath ? tLockPath
ef6d0 3a 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 : pCtx->lockProx
ef6e0 79 50 61 74 68 3b 0a 20 20 20 20 20 20 2f 2a 20 yPath;. /*
ef6f0 41 43 53 3a 20 4e 65 65 64 20 74 6f 20 6d 61 6b ACS: Need to mak
ef700 65 20 61 20 63 6f 70 79 20 6f 66 20 70 61 74 68 e a copy of path
ef710 20 73 6f 6d 65 74 69 6d 65 73 20 2a 2f 0a 20 20 sometimes */.
ef720 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 43 72 rc = proxyCr
ef730 65 61 74 65 55 6e 69 78 46 69 6c 65 28 70 61 74 eateUnixFile(pat
ef740 68 2c 20 26 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 h, &pCtx->lockPr
ef750 6f 78 79 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 oxy);. }.
ef760 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
ef770 4b 20 29 7b 0a 20 20 20 20 20 20 70 43 74 78 2d K ){. pCtx-
ef780 3e 63 6f 6e 63 68 48 65 6c 64 20 3d 20 31 3b 0a >conchHeld = 1;.
ef790 0a 20 20 20 20 20 20 69 66 28 20 74 4c 6f 63 6b . if( tLock
ef7a0 50 61 74 68 20 29 7b 0a 20 20 20 20 20 20 20 20 Path ){.
ef7b0 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 pCtx->lockProxyP
ef7c0 61 74 68 20 3d 20 73 71 6c 69 74 65 33 44 62 53 ath = sqlite3DbS
ef7d0 74 72 44 75 70 28 30 2c 20 74 4c 6f 63 6b 50 61 trDup(0, tLockPa
ef7e0 74 68 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 th);. if(
ef7f0 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 pCtx->lockProxy
ef800 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 26 61 66 ->pMethod == &af
ef810 70 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 pIoMethods ){.
ef820 20 20 20 20 20 20 20 20 28 28 61 66 70 4c 6f 63 ((afpLoc
ef830 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 43 kingContext *)pC
ef840 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 2d 3e 6c tx->lockProxy->l
ef850 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 2d 3e ockingContext)->
ef860 64 62 50 61 74 68 20 3d 0a 20 20 20 20 20 20 20 dbPath =.
ef870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43 pC
ef880 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 tx->lockProxyPat
ef890 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 h;. }.
ef8a0 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c 73 65 20 }. } else
ef8b0 7b 0a 20 20 20 20 20 20 63 6f 6e 63 68 46 69 6c {. conchFil
ef8c0 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c e->pMethod->xUnl
ef8d0 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c ock((sqlite3_fil
ef8e0 65 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20 4e 4f e*)conchFile, NO
ef8f0 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 7d 0a 20 20 _LOCK);. }.
ef900 20 20 4f 53 54 52 41 43 45 33 28 22 54 41 4b 45 OSTRACE3("TAKE
ef910 43 4f 4e 43 48 20 20 25 64 20 25 73 5c 6e 22 2c CONCH %d %s\n",
ef920 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c 20 72 conchFile->h, r
ef930 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3f 22 6f 6b c==SQLITE_OK?"ok
ef940 22 3a 22 66 61 69 6c 65 64 22 29 3b 0a 20 20 20 ":"failed");.
ef950 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a return rc;. }.
ef960 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 70 46 69 6c }../*.** If pFil
ef970 65 20 68 6f 6c 64 73 20 61 20 6c 6f 63 6b 20 6f e holds a lock o
ef980 6e 20 61 20 63 6f 6e 63 68 20 66 69 6c 65 2c 20 n a conch file,
ef990 74 68 65 6e 20 72 65 6c 65 61 73 65 20 74 68 61 then release tha
ef9a0 74 20 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 t lock..*/.stati
ef9b0 63 20 69 6e 74 20 70 72 6f 78 79 52 65 6c 65 61 c int proxyRelea
ef9c0 73 65 43 6f 6e 63 68 28 75 6e 69 78 46 69 6c 65 seConch(unixFile
ef9d0 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 *pFile){. int
ef9e0 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rc;
ef9f0 20 20 20 20 20 20 20 20 2f 2a 20 53 75 62 72 6f /* Subro
efa00 75 74 69 6e 65 20 72 65 74 75 72 6e 20 63 6f 64 utine return cod
efa10 65 20 2a 2f 0a 20 20 70 72 6f 78 79 4c 6f 63 6b e */. proxyLock
efa20 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 ingContext *pCtx
efa30 3b 20 20 2f 2a 20 54 68 65 20 6c 6f 63 6b 69 6e ; /* The lockin
efa40 67 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 74 68 g context for th
efa50 65 20 70 72 6f 78 79 20 6c 6f 63 6b 20 2a 2f 0a e proxy lock */.
efa60 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 6f 6e 63 unixFile *conc
efa70 68 46 69 6c 65 3b 20 20 20 20 20 20 20 20 2f 2a hFile; /*
efa80 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6e Name of the con
efa90 63 68 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 70 43 ch file */.. pC
efaa0 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69 tx = (proxyLocki
efab0 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c ngContext *)pFil
efac0 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 e->lockingContex
efad0 74 3b 0a 20 20 63 6f 6e 63 68 46 69 6c 65 20 3d t;. conchFile =
efae0 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 pCtx->conchFile
efaf0 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 22 52 45 ;. OSTRACE4("RE
efb00 4c 45 41 53 45 43 4f 4e 43 48 20 20 25 64 20 66 LEASECONCH %d f
efb10 6f 72 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c or %s pid=%d\n",
efb20 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c 0a 20 conchFile->h,.
efb30 20 20 20 20 20 20 20 20 20 20 28 70 43 74 78 2d (pCtx-
efb40 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3f >lockProxyPath ?
efb50 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 pCtx->lockProxy
efb60 50 61 74 68 20 3a 20 22 3a 61 75 74 6f 3a 22 29 Path : ":auto:")
efb70 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 67 65 , . ge
efb80 74 70 69 64 28 29 29 3b 0a 20 20 70 43 74 78 2d tpid());. pCtx-
efb90 3e 63 6f 6e 63 68 48 65 6c 64 20 3d 20 30 3b 0a >conchHeld = 0;.
efba0 20 20 72 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 rc = conchFile
efbb0 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f ->pMethod->xUnlo
efbc0 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ck((sqlite3_file
efbd0 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20 4e 4f 5f *)conchFile, NO_
efbe0 4c 4f 43 4b 29 3b 0a 20 20 4f 53 54 52 41 43 45 LOCK);. OSTRACE
efbf0 33 28 22 52 45 4c 45 41 53 45 43 4f 4e 43 48 20 3("RELEASECONCH
efc00 20 25 64 20 25 73 5c 6e 22 2c 20 63 6f 6e 63 68 %d %s\n", conch
efc10 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 File->h,.
efc20 20 20 20 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f (rc==SQLITE_
efc30 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61 69 OK ? "ok" : "fai
efc40 6c 65 64 22 29 29 3b 0a 20 20 72 65 74 75 72 6e led"));. return
efc50 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 rc;.}../*.** Gi
efc60 76 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 ven the name of
efc70 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c a database file,
efc80 20 63 6f 6d 70 75 74 65 20 74 68 65 20 6e 61 6d compute the nam
efc90 65 20 6f 66 20 69 74 73 20 63 6f 6e 63 68 20 66 e of its conch f
efca0 69 6c 65 2e 0a 2a 2a 20 53 74 6f 72 65 20 74 68 ile..** Store th
efcb0 65 20 63 6f 6e 63 68 20 66 69 6c 65 6e 61 6d 65 e conch filename
efcc0 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 in memory obtai
efcd0 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 ned from sqlite3
efce0 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 4d 61 _malloc()..** Ma
efcf0 6b 65 20 2a 70 43 6f 6e 63 68 50 61 74 68 20 70 ke *pConchPath p
efd00 6f 69 6e 74 20 74 6f 20 74 68 65 20 6e 65 77 20 oint to the new
efd10 6e 61 6d 65 2e 20 20 52 65 74 75 72 6e 20 53 51 name. Return SQ
efd20 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 LITE_OK on succe
efd30 73 73 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f ss.** or SQLITE_
efd40 4e 4f 4d 45 4d 20 69 66 20 75 6e 61 62 6c 65 20 NOMEM if unable
efd50 74 6f 20 6f 62 74 61 69 6e 20 6d 65 6d 6f 72 79 to obtain memory
efd60 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c ..**.** The call
efd70 65 72 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c er is responsibl
efd80 65 20 66 6f 72 20 65 6e 73 75 72 69 6e 67 20 74 e for ensuring t
efd90 68 61 74 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 hat the allocate
efda0 64 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 70 61 63 d memory.** spac
efdb0 65 20 69 73 20 65 76 65 6e 74 75 61 6c 6c 79 20 e is eventually
efdc0 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 2a 70 43 freed..**.** *pC
efdd0 6f 6e 63 68 50 61 74 68 20 69 73 20 73 65 74 20 onchPath is set
efde0 74 6f 20 4e 55 4c 4c 20 69 66 20 61 20 6d 65 6d to NULL if a mem
efdf0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 ory allocation e
efe00 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a rror occurs..*/.
efe10 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 static int proxy
efe20 43 72 65 61 74 65 43 6f 6e 63 68 50 61 74 68 6e CreateConchPathn
efe30 61 6d 65 28 63 68 61 72 20 2a 64 62 50 61 74 68 ame(char *dbPath
efe40 2c 20 63 68 61 72 20 2a 2a 70 43 6f 6e 63 68 50 , char **pConchP
efe50 61 74 68 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 ath){. int i;
efe60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
efe70 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f /* Loop co
efe80 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6c unter */. int l
efe90 65 6e 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e en = (int)strlen
efea0 28 64 62 50 61 74 68 29 3b 20 2f 2a 20 4c 65 6e (dbPath); /* Len
efeb0 67 74 68 20 6f 66 20 64 61 74 61 62 61 73 65 20 gth of database
efec0 66 69 6c 65 6e 61 6d 65 20 2d 20 64 62 50 61 74 filename - dbPat
efed0 68 20 2a 2f 0a 20 20 63 68 61 72 20 2a 63 6f 6e h */. char *con
efee0 63 68 50 61 74 68 3b 20 20 20 20 20 20 20 20 20 chPath;
efef0 20 20 20 20 20 2f 2a 20 62 75 66 66 65 72 20 69 /* buffer i
eff00 6e 20 77 68 69 63 68 20 74 6f 20 63 6f 6e 73 74 n which to const
eff10 72 75 63 74 20 63 6f 6e 63 68 20 6e 61 6d 65 20 ruct conch name
eff20 2a 2f 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 */.. /* Allocat
eff30 65 20 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 e space for the
eff40 63 6f 6e 63 68 20 66 69 6c 65 6e 61 6d 65 20 61 conch filename a
eff50 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 nd initialize th
eff60 65 20 6e 61 6d 65 20 74 6f 0a 20 20 2a 2a 20 74 e name to. ** t
eff70 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 6f he name of the o
eff80 72 69 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 riginal database
eff90 20 66 69 6c 65 2e 20 2a 2f 20 20 0a 20 20 2a 70 file. */ . *p
effa0 43 6f 6e 63 68 50 61 74 68 20 3d 20 63 6f 6e 63 ConchPath = conc
effb0 68 50 61 74 68 20 3d 20 28 63 68 61 72 20 2a 29 hPath = (char *)
effc0 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6c sqlite3_malloc(l
effd0 65 6e 20 2b 20 38 29 3b 0a 20 20 69 66 28 20 63 en + 8);. if( c
effe0 6f 6e 63 68 50 61 74 68 3d 3d 30 20 29 7b 0a 20 onchPath==0 ){.
efff0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
f0000 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 _NOMEM;. }. me
f0010 6d 63 70 79 28 63 6f 6e 63 68 50 61 74 68 2c 20 mcpy(conchPath,
f0020 64 62 50 61 74 68 2c 20 6c 65 6e 2b 31 29 3b 0a dbPath, len+1);.
f0030 20 20 0a 20 20 2f 2a 20 6e 6f 77 20 69 6e 73 65 . /* now inse
f0040 72 74 20 61 20 22 2e 22 20 62 65 66 6f 72 65 20 rt a "." before
f0050 74 68 65 20 6c 61 73 74 20 2f 20 63 68 61 72 61 the last / chara
f0060 63 74 65 72 20 2a 2f 0a 20 20 66 6f 72 28 20 69 cter */. for( i
f0070 3d 28 6c 65 6e 2d 31 29 3b 20 69 3e 3d 30 3b 20 =(len-1); i>=0;
f0080 69 2d 2d 20 29 7b 0a 20 20 20 20 69 66 28 20 63 i-- ){. if( c
f0090 6f 6e 63 68 50 61 74 68 5b 69 5d 3d 3d 27 2f 27 onchPath[i]=='/'
f00a0 20 29 7b 0a 20 20 20 20 20 20 69 2b 2b 3b 0a 20 ){. i++;.
f00b0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
f00c0 7d 0a 20 20 7d 0a 20 20 63 6f 6e 63 68 50 61 74 }. }. conchPat
f00d0 68 5b 69 5d 3d 27 2e 27 3b 0a 20 20 77 68 69 6c h[i]='.';. whil
f00e0 65 20 28 20 69 3c 6c 65 6e 20 29 7b 0a 20 20 20 e ( i<len ){.
f00f0 20 63 6f 6e 63 68 50 61 74 68 5b 69 2b 31 5d 3d conchPath[i+1]=
f0100 64 62 50 61 74 68 5b 69 5d 3b 0a 20 20 20 20 69 dbPath[i];. i
f0110 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 61 70 ++;. }.. /* ap
f0120 70 65 6e 64 20 74 68 65 20 22 2d 63 6f 6e 63 68 pend the "-conch
f0130 22 20 73 75 66 66 69 78 20 74 6f 20 74 68 65 20 " suffix to the
f0140 66 69 6c 65 20 2a 2f 0a 20 20 6d 65 6d 63 70 79 file */. memcpy
f0150 28 26 63 6f 6e 63 68 50 61 74 68 5b 69 2b 31 5d (&conchPath[i+1]
f0160 2c 20 22 2d 63 6f 6e 63 68 22 2c 20 37 29 3b 0a , "-conch", 7);.
f0170 20 20 61 73 73 65 72 74 28 20 28 69 6e 74 29 73 assert( (int)s
f0180 74 72 6c 65 6e 28 63 6f 6e 63 68 50 61 74 68 29 trlen(conchPath)
f0190 20 3d 3d 20 6c 65 6e 2b 37 20 29 3b 0a 0a 20 20 == len+7 );..
f01a0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
f01b0 3b 0a 7d 0a 0a 0a 2f 2a 20 54 61 6b 65 73 20 61 ;.}.../* Takes a
f01c0 20 66 75 6c 6c 79 20 63 6f 6e 66 69 67 75 72 65 fully configure
f01d0 64 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2d d proxy locking-
f01e0 73 74 79 6c 65 20 75 6e 69 78 20 66 69 6c 65 20 style unix file
f01f0 61 6e 64 20 73 77 69 74 63 68 65 73 0a 2a 2a 20 and switches.**
f0200 74 68 65 20 6c 6f 63 61 6c 20 6c 6f 63 6b 20 66 the local lock f
f0210 69 6c 65 20 70 61 74 68 20 0a 2a 2f 0a 73 74 61 ile path .*/.sta
f0220 74 69 63 20 69 6e 74 20 73 77 69 74 63 68 4c 6f tic int switchLo
f0230 63 6b 50 72 6f 78 79 50 61 74 68 28 75 6e 69 78 ckProxyPath(unix
f0240 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 63 6f 6e File *pFile, con
f0250 73 74 20 63 68 61 72 20 2a 70 61 74 68 29 20 7b st char *path) {
f0260 0a 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 . proxyLockingC
f0270 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28 ontext *pCtx = (
f0280 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 proxyLockingCont
f0290 65 78 74 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b ext*)pFile->lock
f02a0 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 63 68 ingContext;. ch
f02b0 61 72 20 2a 6f 6c 64 50 61 74 68 20 3d 20 70 43 ar *oldPath = pC
f02c0 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 tx->lockProxyPat
f02d0 68 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 h;. int rc = SQ
f02e0 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20 LITE_OK;.. if(
f02f0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 pFile->locktype!
f0300 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 =NO_LOCK ){.
f0310 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 return SQLITE_BU
f0320 53 59 3b 0a 20 20 7d 20 20 0a 0a 20 20 2f 2a 20 SY;. } .. /*
f0330 6e 6f 74 68 69 6e 67 20 74 6f 20 64 6f 20 69 66 nothing to do if
f0340 20 74 68 65 20 70 61 74 68 20 69 73 20 4e 55 4c the path is NUL
f0350 4c 2c 20 3a 61 75 74 6f 3a 20 6f 72 20 6d 61 74 L, :auto: or mat
f0360 63 68 65 73 20 74 68 65 20 65 78 69 73 74 69 6e ches the existin
f0370 67 20 70 61 74 68 20 2a 2f 0a 20 20 69 66 28 20 g path */. if(
f0380 21 70 61 74 68 20 7c 7c 20 70 61 74 68 5b 30 5d !path || path[0]
f0390 3d 3d 27 5c 30 27 20 7c 7c 20 21 73 74 72 63 6d =='\0' || !strcm
f03a0 70 28 70 61 74 68 2c 20 22 3a 61 75 74 6f 3a 22 p(path, ":auto:"
f03b0 29 20 7c 7c 0a 20 20 20 20 28 6f 6c 64 50 61 74 ) ||. (oldPat
f03c0 68 20 26 26 20 21 73 74 72 6e 63 6d 70 28 6f 6c h && !strncmp(ol
f03d0 64 50 61 74 68 2c 20 70 61 74 68 2c 20 4d 41 58 dPath, path, MAX
f03e0 50 41 54 48 4c 45 4e 29 29 20 29 7b 0a 20 20 20 PATHLEN)) ){.
f03f0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
f0400 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 K;. }else{.
f0410 75 6e 69 78 46 69 6c 65 20 2a 6c 6f 63 6b 50 72 unixFile *lockPr
f0420 6f 78 79 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b oxy = pCtx->lock
f0430 50 72 6f 78 79 3b 0a 20 20 20 20 70 43 74 78 2d Proxy;. pCtx-
f0440 3e 6c 6f 63 6b 50 72 6f 78 79 3d 4e 55 4c 4c 3b >lockProxy=NULL;
f0450 0a 20 20 20 20 70 43 74 78 2d 3e 63 6f 6e 63 68 . pCtx->conch
f0460 48 65 6c 64 20 3d 20 30 3b 0a 20 20 20 20 69 66 Held = 0;. if
f0470 28 20 6c 6f 63 6b 50 72 6f 78 79 21 3d 4e 55 4c ( lockProxy!=NUL
f0480 4c 20 29 7b 0a 20 20 20 20 20 20 72 63 3d 6c 6f L ){. rc=lo
f0490 63 6b 50 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 ckProxy->pMethod
f04a0 2d 3e 78 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 ->xClose((sqlite
f04b0 33 5f 66 69 6c 65 20 2a 29 6c 6f 63 6b 50 72 6f 3_file *)lockPro
f04c0 78 79 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 xy);. if( r
f04d0 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 c ) return rc;.
f04e0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 sqlite3_fre
f04f0 65 28 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20 e(lockProxy);.
f0500 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f }. sqlite3_
f0510 66 72 65 65 28 6f 6c 64 50 61 74 68 29 3b 0a 20 free(oldPath);.
f0520 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f pCtx->lockPro
f0530 78 79 50 61 74 68 20 3d 20 73 71 6c 69 74 65 33 xyPath = sqlite3
f0540 44 62 53 74 72 44 75 70 28 30 2c 20 70 61 74 68 DbStrDup(0, path
f0550 29 3b 0a 20 20 7d 0a 20 20 0a 20 20 72 65 74 75 );. }. . retu
f0560 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
f0570 70 46 69 6c 65 20 69 73 20 61 20 66 69 6c 65 20 pFile is a file
f0580 74 68 61 74 20 68 61 73 20 62 65 65 6e 20 6f 70 that has been op
f0590 65 6e 65 64 20 62 79 20 61 20 70 72 69 6f 72 20 ened by a prior
f05a0 78 4f 70 65 6e 20 63 61 6c 6c 2e 20 20 64 62 50 xOpen call. dbP
f05b0 61 74 68 0a 2a 2a 20 69 73 20 61 20 73 74 72 69 ath.** is a stri
f05c0 6e 67 20 62 75 66 66 65 72 20 61 74 20 6c 65 61 ng buffer at lea
f05d0 73 74 20 4d 41 58 50 41 54 48 4c 45 4e 2b 31 20 st MAXPATHLEN+1
f05e0 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 73 69 characters in si
f05f0 7a 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 ze..**.** This r
f0600 6f 75 74 69 6e 65 20 66 69 6e 64 20 74 68 65 20 outine find the
f0610 66 69 6c 65 6e 61 6d 65 20 61 73 73 6f 63 69 61 filename associa
f0620 74 65 64 20 77 69 74 68 20 70 46 69 6c 65 20 61 ted with pFile a
f0630 6e 64 20 77 72 69 74 65 73 20 69 74 0a 2a 2a 20 nd writes it.**
f0640 69 6e 74 20 64 62 50 61 74 68 2e 0a 2a 2f 0a 73 int dbPath..*/.s
f0650 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 47 tatic int proxyG
f0660 65 74 44 62 50 61 74 68 46 6f 72 55 6e 69 78 46 etDbPathForUnixF
f0670 69 6c 65 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 ile(unixFile *pF
f0680 69 6c 65 2c 20 63 68 61 72 20 2a 64 62 50 61 74 ile, char *dbPat
f0690 68 29 7b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 h){.#if defined(
f06a0 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 20 69 66 28 __APPLE__). if(
f06b0 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 pFile->pMethod
f06c0 3d 3d 20 26 61 66 70 49 6f 4d 65 74 68 6f 64 73 == &afpIoMethods
f06d0 20 29 7b 0a 20 20 20 20 2f 2a 20 61 66 70 20 73 ){. /* afp s
f06e0 74 79 6c 65 20 6b 65 65 70 73 20 61 20 72 65 66 tyle keeps a ref
f06f0 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 64 62 erence to the db
f0700 20 70 61 74 68 20 69 6e 20 74 68 65 20 66 69 6c path in the fil
f0710 65 50 61 74 68 20 66 69 65 6c 64 20 0a 20 20 20 ePath field .
f0720 20 2a 2a 20 6f 66 20 74 68 65 20 73 74 72 75 63 ** of the struc
f0730 74 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 t */. assert(
f0740 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 28 63 68 (int)strlen((ch
f0750 61 72 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 ar*)pFile->locki
f0760 6e 67 43 6f 6e 74 65 78 74 29 3c 3d 4d 41 58 50 ngContext)<=MAXP
f0770 41 54 48 4c 45 4e 20 29 3b 0a 20 20 20 20 73 74 ATHLEN );. st
f0780 72 63 70 79 28 64 62 50 61 74 68 2c 20 28 28 61 rcpy(dbPath, ((a
f0790 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 fpLockingContext
f07a0 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e *)pFile->lockin
f07b0 67 43 6f 6e 74 65 78 74 29 2d 3e 64 62 50 61 74 gContext)->dbPat
f07c0 68 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 h);. }else.#end
f07d0 69 66 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e if. if( pFile->
f07e0 70 4d 65 74 68 6f 64 20 3d 3d 20 26 64 6f 74 6c pMethod == &dotl
f07f0 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a ockIoMethods ){.
f0800 20 20 20 20 2f 2a 20 64 6f 74 20 6c 6f 63 6b 20 /* dot lock
f0810 73 74 79 6c 65 20 75 73 65 73 20 74 68 65 20 6c style uses the l
f0820 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 74 ocking context t
f0830 6f 20 73 74 6f 72 65 20 74 68 65 20 64 6f 74 20 o store the dot
f0840 6c 6f 63 6b 0a 20 20 20 20 2a 2a 20 66 69 6c 65 lock. ** file
f0850 20 70 61 74 68 20 2a 2f 0a 20 20 20 20 69 6e 74 path */. int
f0860 20 6c 65 6e 20 3d 20 73 74 72 6c 65 6e 28 28 63 len = strlen((c
f0870 68 61 72 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 har *)pFile->loc
f0880 6b 69 6e 67 43 6f 6e 74 65 78 74 29 20 2d 20 73 kingContext) - s
f0890 74 72 6c 65 6e 28 44 4f 54 4c 4f 43 4b 5f 53 55 trlen(DOTLOCK_SU
f08a0 46 46 49 58 29 3b 0a 20 20 20 20 6d 65 6d 63 70 FFIX);. memcp
f08b0 79 28 64 62 50 61 74 68 2c 20 28 63 68 61 72 20 y(dbPath, (char
f08c0 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 *)pFile->locking
f08d0 43 6f 6e 74 65 78 74 2c 20 6c 65 6e 20 2b 20 31 Context, len + 1
f08e0 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
f08f0 2f 2a 20 61 6c 6c 20 6f 74 68 65 72 20 73 74 79 /* all other sty
f0900 6c 65 73 20 75 73 65 20 74 68 65 20 6c 6f 63 6b les use the lock
f0910 69 6e 67 20 63 6f 6e 74 65 78 74 20 74 6f 20 73 ing context to s
f0920 74 6f 72 65 20 74 68 65 20 64 62 20 66 69 6c 65 tore the db file
f0930 20 70 61 74 68 20 2a 2f 0a 20 20 20 20 61 73 73 path */. ass
f0940 65 72 74 28 20 73 74 72 6c 65 6e 28 28 63 68 61 ert( strlen((cha
f0950 72 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e r*)pFile->lockin
f0960 67 43 6f 6e 74 65 78 74 29 3c 3d 4d 41 58 50 41 gContext)<=MAXPA
f0970 54 48 4c 45 4e 20 29 3b 0a 20 20 20 20 73 74 72 THLEN );. str
f0980 63 70 79 28 64 62 50 61 74 68 2c 20 28 63 68 61 cpy(dbPath, (cha
f0990 72 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 r *)pFile->locki
f09a0 6e 67 43 6f 6e 74 65 78 74 29 3b 0a 20 20 7d 0a ngContext);. }.
f09b0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
f09c0 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 6b OK;.}../*.** Tak
f09d0 65 73 20 61 6e 20 61 6c 72 65 61 64 79 20 66 69 es an already fi
f09e0 6c 6c 65 64 20 69 6e 20 75 6e 69 78 20 66 69 6c lled in unix fil
f09f0 65 20 61 6e 64 20 61 6c 74 65 72 73 20 69 74 20 e and alters it
f0a00 73 6f 20 61 6c 6c 20 66 69 6c 65 20 6c 6f 63 6b so all file lock
f0a10 69 6e 67 20 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 ing .** will be
f0a20 70 65 72 66 6f 72 6d 65 64 20 6f 6e 20 74 68 65 performed on the
f0a30 20 6c 6f 63 61 6c 20 70 72 6f 78 79 20 6c 6f 63 local proxy loc
f0a40 6b 20 66 69 6c 65 2e 20 20 54 68 65 20 66 6f 6c k file. The fol
f0a50 6c 6f 77 69 6e 67 20 66 69 65 6c 64 73 0a 2a 2a lowing fields.**
f0a60 20 61 72 65 20 70 72 65 73 65 72 76 65 64 20 69 are preserved i
f0a70 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f n the locking co
f0a80 6e 74 65 78 74 20 73 6f 20 74 68 61 74 20 74 68 ntext so that th
f0a90 65 79 20 63 61 6e 20 62 65 20 72 65 73 74 6f 72 ey can be restor
f0aa0 65 64 20 61 6e 64 20 0a 2a 2a 20 74 68 65 20 75 ed and .** the u
f0ab0 6e 69 78 20 73 74 72 75 63 74 75 72 65 20 70 72 nix structure pr
f0ac0 6f 70 65 72 6c 79 20 63 6c 65 61 6e 65 64 20 75 operly cleaned u
f0ad0 70 20 61 74 20 63 6c 6f 73 65 20 74 69 6d 65 3a p at close time:
f0ae0 0a 2a 2a 20 20 2d 3e 6c 6f 63 6b 69 6e 67 43 6f .** ->lockingCo
f0af0 6e 74 65 78 74 0a 2a 2a 20 20 2d 3e 70 4d 65 74 ntext.** ->pMet
f0b00 68 6f 64 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e hod.*/.static in
f0b10 74 20 70 72 6f 78 79 54 72 61 6e 73 66 6f 72 6d t proxyTransform
f0b20 55 6e 69 78 46 69 6c 65 28 75 6e 69 78 46 69 6c UnixFile(unixFil
f0b30 65 20 2a 70 46 69 6c 65 2c 20 63 6f 6e 73 74 20 e *pFile, const
f0b40 63 68 61 72 20 2a 70 61 74 68 29 20 7b 0a 20 20 char *path) {.
f0b50 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 proxyLockingCont
f0b60 65 78 74 20 2a 70 43 74 78 3b 0a 20 20 63 68 61 ext *pCtx;. cha
f0b70 72 20 64 62 50 61 74 68 5b 4d 41 58 50 41 54 48 r dbPath[MAXPATH
f0b80 4c 45 4e 2b 31 5d 3b 20 20 20 20 20 20 20 2f 2a LEN+1]; /*
f0b90 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 Name of the dat
f0ba0 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 abase file */.
f0bb0 63 68 61 72 20 2a 6c 6f 63 6b 50 61 74 68 3d 4e char *lockPath=N
f0bc0 55 4c 4c 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 ULL;. int rc =
f0bd0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 0a 20 20 SQLITE_OK;. .
f0be0 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 if( pFile->lockt
f0bf0 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a ype!=NO_LOCK ){.
f0c00 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
f0c10 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20 70 72 E_BUSY;. }. pr
f0c20 6f 78 79 47 65 74 44 62 50 61 74 68 46 6f 72 55 oxyGetDbPathForU
f0c30 6e 69 78 46 69 6c 65 28 70 46 69 6c 65 2c 20 64 nixFile(pFile, d
f0c40 62 50 61 74 68 29 3b 0a 20 20 69 66 28 20 21 70 bPath);. if( !p
f0c50 61 74 68 20 7c 7c 20 70 61 74 68 5b 30 5d 3d 3d ath || path[0]==
f0c60 27 5c 30 27 20 7c 7c 20 21 73 74 72 63 6d 70 28 '\0' || !strcmp(
f0c70 70 61 74 68 2c 20 22 3a 61 75 74 6f 3a 22 29 20 path, ":auto:")
f0c80 29 7b 0a 20 20 20 20 6c 6f 63 6b 50 61 74 68 3d ){. lockPath=
f0c90 4e 55 4c 4c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 NULL;. }else{.
f0ca0 20 20 20 6c 6f 63 6b 50 61 74 68 3d 28 63 68 61 lockPath=(cha
f0cb0 72 20 2a 29 70 61 74 68 3b 0a 20 20 7d 0a 20 20 r *)path;. }.
f0cc0 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54 52 41 . OSTRACE4("TRA
f0cd0 4e 53 50 52 4f 58 59 20 20 25 64 20 66 6f 72 20 NSPROXY %d for
f0ce0 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 %s pid=%d\n", pF
f0cf0 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 ile->h,.
f0d00 20 20 20 28 6c 6f 63 6b 50 61 74 68 20 3f 20 6c (lockPath ? l
f0d10 6f 63 6b 50 61 74 68 20 3a 20 22 3a 61 75 74 6f ockPath : ":auto
f0d20 3a 22 29 2c 20 67 65 74 70 69 64 28 29 29 3b 0a :"), getpid());.
f0d30 0a 20 20 70 43 74 78 20 3d 20 73 71 6c 69 74 65 . pCtx = sqlite
f0d40 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 3_malloc( sizeof
f0d50 28 2a 70 43 74 78 29 20 29 3b 0a 20 20 69 66 28 (*pCtx) );. if(
f0d60 20 70 43 74 78 3d 3d 30 20 29 7b 0a 20 20 20 20 pCtx==0 ){.
f0d70 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
f0d80 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 MEM;. }. memse
f0d90 74 28 70 43 74 78 2c 20 30 2c 20 73 69 7a 65 6f t(pCtx, 0, sizeo
f0da0 66 28 2a 70 43 74 78 29 29 3b 0a 0a 20 20 72 63 f(*pCtx));.. rc
f0db0 20 3d 20 70 72 6f 78 79 43 72 65 61 74 65 43 6f = proxyCreateCo
f0dc0 6e 63 68 50 61 74 68 6e 61 6d 65 28 64 62 50 61 nchPathname(dbPa
f0dd0 74 68 2c 20 26 70 43 74 78 2d 3e 63 6f 6e 63 68 th, &pCtx->conch
f0de0 46 69 6c 65 50 61 74 68 29 3b 0a 20 20 69 66 28 FilePath);. if(
f0df0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
f0e00 7b 0a 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 {. rc = proxy
f0e10 43 72 65 61 74 65 55 6e 69 78 46 69 6c 65 28 70 CreateUnixFile(p
f0e20 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 Ctx->conchFilePa
f0e30 74 68 2c 20 26 70 43 74 78 2d 3e 63 6f 6e 63 68 th, &pCtx->conch
f0e40 46 69 6c 65 29 3b 0a 20 20 7d 20 20 0a 20 20 69 File);. } . i
f0e50 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
f0e60 20 26 26 20 6c 6f 63 6b 50 61 74 68 20 29 7b 0a && lockPath ){.
f0e70 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 pCtx->lockPr
f0e80 6f 78 79 50 61 74 68 20 3d 20 73 71 6c 69 74 65 oxyPath = sqlite
f0e90 33 44 62 53 74 72 44 75 70 28 30 2c 20 6c 6f 63 3DbStrDup(0, loc
f0ea0 6b 50 61 74 68 29 3b 0a 20 20 7d 0a 0a 20 20 69 kPath);. }.. i
f0eb0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
f0ec0 20 29 7b 0a 20 20 20 20 2f 2a 20 61 6c 6c 20 6d ){. /* all m
f0ed0 65 6d 6f 72 79 20 69 73 20 61 6c 6c 6f 63 61 74 emory is allocat
f0ee0 65 64 2c 20 70 72 6f 78 79 73 20 61 72 65 20 63 ed, proxys are c
f0ef0 72 65 61 74 65 64 20 61 6e 64 20 61 73 73 69 67 reated and assig
f0f00 6e 65 64 2c 20 0a 20 20 20 20 2a 2a 20 73 77 69 ned, . ** swi
f0f10 74 63 68 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 tch the locking
f0f20 63 6f 6e 74 65 78 74 20 61 6e 64 20 70 4d 65 74 context and pMet
f0f30 68 6f 64 20 74 68 65 6e 20 72 65 74 75 72 6e 2e hod then return.
f0f40 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 43 74 78 . */. pCtx
f0f50 2d 3e 64 62 50 61 74 68 20 3d 20 73 71 6c 69 74 ->dbPath = sqlit
f0f60 65 33 44 62 53 74 72 44 75 70 28 30 2c 20 64 62 e3DbStrDup(0, db
f0f70 50 61 74 68 29 3b 0a 20 20 20 20 70 43 74 78 2d Path);. pCtx-
f0f80 3e 6f 6c 64 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 >oldLockingConte
f0f90 78 74 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b xt = pFile->lock
f0fa0 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 ingContext;.
f0fb0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f pFile->lockingCo
f0fc0 6e 74 65 78 74 20 3d 20 70 43 74 78 3b 0a 20 20 ntext = pCtx;.
f0fd0 20 20 70 43 74 78 2d 3e 70 4f 6c 64 4d 65 74 68 pCtx->pOldMeth
f0fe0 6f 64 20 3d 20 70 46 69 6c 65 2d 3e 70 4d 65 74 od = pFile->pMet
f0ff0 68 6f 64 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e hod;. pFile->
f1000 70 4d 65 74 68 6f 64 20 3d 20 26 70 72 6f 78 79 pMethod = &proxy
f1010 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c IoMethods;. }el
f1020 73 65 7b 0a 20 20 20 20 69 66 28 20 70 43 74 78 se{. if( pCtx
f1030 2d 3e 63 6f 6e 63 68 46 69 6c 65 20 29 7b 20 0a ->conchFile ){ .
f1040 20 20 20 20 20 20 72 63 20 3d 20 70 43 74 78 2d rc = pCtx-
f1050 3e 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 >conchFile->pMet
f1060 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 28 73 71 6c hod->xClose((sql
f1070 69 74 65 33 5f 66 69 6c 65 20 2a 29 70 43 74 78 ite3_file *)pCtx
f1080 2d 3e 63 6f 6e 63 68 46 69 6c 65 29 3b 0a 20 20 ->conchFile);.
f1090 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 if( rc ) ret
f10a0 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 73 71 urn rc;. sq
f10b0 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74 78 2d lite3_free(pCtx-
f10c0 3e 63 6f 6e 63 68 46 69 6c 65 29 3b 0a 20 20 20 >conchFile);.
f10d0 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 }. sqlite3_f
f10e0 72 65 65 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46 ree(pCtx->conchF
f10f0 69 6c 65 50 61 74 68 29 3b 20 0a 20 20 20 20 73 ilePath); . s
f1100 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74 78 qlite3_free(pCtx
f1110 29 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 );. }. OSTRACE
f1120 33 28 22 54 52 41 4e 53 50 52 4f 58 59 20 20 25 3("TRANSPROXY %
f1130 64 20 25 73 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e d %s\n", pFile->
f1140 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 28 72 h,. (r
f1150 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 c==SQLITE_OK ? "
f1160 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29 29 ok" : "failed"))
f1170 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
f1180 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f .../*.** This ro
f1190 75 74 69 6e 65 20 68 61 6e 64 6c 65 73 20 73 71 utine handles sq
f11a0 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 lite3_file_contr
f11b0 6f 6c 28 29 20 63 61 6c 6c 73 20 74 68 61 74 20 ol() calls that
f11c0 61 72 65 20 73 70 65 63 69 66 69 63 0a 2a 2a 20 are specific.**
f11d0 74 6f 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 to proxy locking
f11e0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
f11f0 70 72 6f 78 79 46 69 6c 65 43 6f 6e 74 72 6f 6c proxyFileControl
f1200 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
f1210 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 d, int op, void
f1220 2a 70 41 72 67 29 7b 0a 20 20 73 77 69 74 63 68 *pArg){. switch
f1230 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 ( op ){. case
f1240 20 53 51 4c 49 54 45 5f 47 45 54 5f 4c 4f 43 4b SQLITE_GET_LOCK
f1250 50 52 4f 58 59 46 49 4c 45 3a 20 7b 0a 20 20 20 PROXYFILE: {.
f1260 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 unixFile *pFi
f1270 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 le = (unixFile*)
f1280 69 64 3b 0a 20 20 20 20 20 20 69 66 28 20 70 46 id;. if( pF
f1290 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 ile->pMethod ==
f12a0 26 70 72 6f 78 79 49 6f 4d 65 74 68 6f 64 73 20 &proxyIoMethods
f12b0 29 7b 0a 20 20 20 20 20 20 20 20 70 72 6f 78 79 ){. proxy
f12c0 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a LockingContext *
f12d0 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 pCtx = (proxyLoc
f12e0 6b 69 6e 67 43 6f 6e 74 65 78 74 2a 29 70 46 69 kingContext*)pFi
f12f0 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 le->lockingConte
f1300 78 74 3b 0a 20 20 20 20 20 20 20 20 70 72 6f 78 xt;. prox
f1310 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c 65 yTakeConch(pFile
f1320 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 );. if( p
f1330 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 Ctx->lockProxyPa
f1340 74 68 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 th ){.
f1350 2a 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 29 *(const char **)
f1360 70 41 72 67 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 pArg = pCtx->loc
f1370 6b 50 72 6f 78 79 50 61 74 68 3b 0a 20 20 20 20 kProxyPath;.
f1380 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
f1390 20 20 20 20 20 2a 28 63 6f 6e 73 74 20 63 68 61 *(const cha
f13a0 72 20 2a 2a 29 70 41 72 67 20 3d 20 22 3a 61 75 r **)pArg = ":au
f13b0 74 6f 3a 20 28 6e 6f 74 20 68 65 6c 64 29 22 3b to: (not held)";
f13c0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
f13d0 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 } else {.
f13e0 20 20 2a 28 63 6f 6e 73 74 20 63 68 61 72 20 2a *(const char *
f13f0 2a 29 70 41 72 67 20 3d 20 4e 55 4c 4c 3b 0a 20 *)pArg = NULL;.
f1400 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 }. ret
f1410 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
f1420 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 }. case SQ
f1430 4c 49 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f LITE_SET_LOCKPRO
f1440 58 59 46 49 4c 45 3a 20 7b 0a 20 20 20 20 20 20 XYFILE: {.
f1450 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 unixFile *pFile
f1460 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b = (unixFile*)id;
f1470 0a 20 20 20 20 20 20 69 6e 74 20 72 63 20 3d 20 . int rc =
f1480 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 SQLITE_OK;.
f1490 20 69 6e 74 20 69 73 50 72 6f 78 79 53 74 79 6c int isProxyStyl
f14a0 65 20 3d 20 28 70 46 69 6c 65 2d 3e 70 4d 65 74 e = (pFile->pMet
f14b0 68 6f 64 20 3d 3d 20 26 70 72 6f 78 79 49 6f 4d hod == &proxyIoM
f14c0 65 74 68 6f 64 73 29 3b 0a 20 20 20 20 20 20 69 ethods);. i
f14d0 66 28 20 70 41 72 67 3d 3d 4e 55 4c 4c 20 7c 7c f( pArg==NULL ||
f14e0 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 (const char *)p
f14f0 41 72 67 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 Arg==0 ){.
f1500 20 20 69 66 28 20 69 73 50 72 6f 78 79 53 74 79 if( isProxySty
f1510 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 le ){.
f1520 2f 2a 20 74 75 72 6e 20 6f 66 66 20 70 72 6f 78 /* turn off prox
f1530 79 20 6c 6f 63 6b 69 6e 67 20 2d 20 6e 6f 74 20 y locking - not
f1540 73 75 70 70 6f 72 74 65 64 20 2a 2f 0a 20 20 20 supported */.
f1550 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
f1560 54 45 5f 45 52 52 4f 52 20 2f 2a 53 51 4c 49 54 TE_ERROR /*SQLIT
f1570 45 5f 50 52 4f 54 4f 43 4f 4c 3f 20 53 51 4c 49 E_PROTOCOL? SQLI
f1580 54 45 5f 4d 49 53 55 53 45 3f 2a 2f 3b 0a 20 20 TE_MISUSE?*/;.
f1590 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
f15a0 20 20 20 20 20 20 20 2f 2a 20 74 75 72 6e 20 6f /* turn o
f15b0 66 66 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 ff proxy locking
f15c0 20 2d 20 61 6c 72 65 61 64 79 20 6f 66 66 20 2d - already off -
f15d0 20 4e 4f 4f 50 20 2a 2f 0a 20 20 20 20 20 20 20 NOOP */.
f15e0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
f15f0 4b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 K;. }.
f1600 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
f1610 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 72 const char *pr
f1620 6f 78 79 50 61 74 68 20 3d 20 28 63 6f 6e 73 74 oxyPath = (const
f1630 20 63 68 61 72 20 2a 29 70 41 72 67 3b 0a 20 20 char *)pArg;.
f1640 20 20 20 20 20 20 69 66 28 20 69 73 50 72 6f 78 if( isProx
f1650 79 53 74 79 6c 65 20 29 7b 0a 20 20 20 20 20 20 yStyle ){.
f1660 20 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 proxyLocking
f1670 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20 Context *pCtx =
f1680 0a 20 20 20 20 20 20 20 20 20 20 20 20 28 70 72 . (pr
f1690 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 oxyLockingContex
f16a0 74 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e t*)pFile->lockin
f16b0 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 20 20 gContext;.
f16c0 20 20 20 20 69 66 28 20 21 73 74 72 63 6d 70 28 if( !strcmp(
f16d0 70 41 72 67 2c 20 22 3a 61 75 74 6f 3a 22 29 20 pArg, ":auto:")
f16e0 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 28 . || (
f16f0 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 pCtx->lockProxyP
f1700 61 74 68 20 26 26 0a 20 20 20 20 20 20 20 20 20 ath &&.
f1710 20 20 20 20 20 20 21 73 74 72 6e 63 6d 70 28 70 !strncmp(p
f1720 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 Ctx->lockProxyPa
f1730 74 68 2c 20 70 72 6f 78 79 50 61 74 68 2c 20 4d th, proxyPath, M
f1740 41 58 50 41 54 48 4c 45 4e 29 29 0a 20 20 20 20 AXPATHLEN)).
f1750 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 ){.
f1760 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
f1770 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d _OK;. }
f1780 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
f1790 20 20 72 63 20 3d 20 73 77 69 74 63 68 4c 6f 63 rc = switchLoc
f17a0 6b 50 72 6f 78 79 50 61 74 68 28 70 46 69 6c 65 kProxyPath(pFile
f17b0 2c 20 70 72 6f 78 79 50 61 74 68 29 3b 0a 20 20 , proxyPath);.
f17c0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
f17d0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
f17e0 20 20 20 2f 2a 20 74 75 72 6e 20 6f 6e 20 70 72 /* turn on pr
f17f0 6f 78 79 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 oxy file locking
f1800 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63 */. rc
f1810 20 3d 20 70 72 6f 78 79 54 72 61 6e 73 66 6f 72 = proxyTransfor
f1820 6d 55 6e 69 78 46 69 6c 65 28 70 46 69 6c 65 2c mUnixFile(pFile,
f1830 20 70 72 6f 78 79 50 61 74 68 29 3b 0a 20 20 20 proxyPath);.
f1840 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
f1850 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
f1860 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c }. defaul
f1870 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 t: {. asser
f1880 74 28 20 30 20 29 3b 20 20 2f 2a 20 54 68 65 20 t( 0 ); /* The
f1890 63 61 6c 6c 20 61 73 73 75 72 65 73 20 74 68 61 call assures tha
f18a0 74 20 6f 6e 6c 79 20 76 61 6c 69 64 20 6f 70 63 t only valid opc
f18b0 6f 64 65 73 20 61 72 65 20 73 65 6e 74 20 2a 2f odes are sent */
f18c0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 4e . }. }. /*N
f18d0 4f 54 52 45 41 43 48 45 44 2a 2f 0a 20 20 72 65 OTREACHED*/. re
f18e0 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f turn SQLITE_ERRO
f18f0 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 69 74 68 R;.}../*.** With
f1900 69 6e 20 74 68 69 73 20 64 69 76 69 73 69 6f 6e in this division
f1910 20 28 74 68 65 20 70 72 6f 78 79 69 6e 67 20 6c (the proxying l
f1920 6f 63 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 ocking implement
f1930 61 74 69 6f 6e 29 20 74 68 65 20 70 72 6f 63 65 ation) the proce
f1940 64 75 72 65 73 0a 2a 2a 20 61 62 6f 76 65 20 74 dures.** above t
f1950 68 69 73 20 70 6f 69 6e 74 20 61 72 65 20 61 6c his point are al
f1960 6c 20 75 74 69 6c 69 74 69 65 73 2e 20 20 54 68 l utilities. Th
f1970 65 20 6c 6f 63 6b 2d 72 65 6c 61 74 65 64 20 6d e lock-related m
f1980 65 74 68 6f 64 73 20 6f 66 20 74 68 65 0a 2a 2a ethods of the.**
f1990 20 70 72 6f 78 79 2d 6c 6f 63 6b 69 6e 67 20 73 proxy-locking s
f19a0 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
f19b0 20 6f 62 6a 65 63 74 20 66 6f 6c 6c 6f 77 2e 0a object follow..
f19c0 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 */.../*.** This
f19d0 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 routine checks i
f19e0 66 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53 f there is a RES
f19f0 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 ERVED lock held
f1a00 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64 on the specified
f1a10 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 .** file by this
f1a20 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 or any other pr
f1a30 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 ocess. If such a
f1a40 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 lock is held, s
f1a50 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74 et *pResOut.** t
f1a60 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c o a non-zero val
f1a70 75 65 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52 ue otherwise *pR
f1a80 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20 esOut is set to
f1a90 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72 zero. The retur
f1aa0 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 n value.** is se
f1ab0 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 t to SQLITE_OK u
f1ac0 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 nless an I/O err
f1ad0 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 or occurs during
f1ae0 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a lock checking..
f1af0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 */.static int pr
f1b00 6f 78 79 43 68 65 63 6b 52 65 73 65 72 76 65 64 oxyCheckReserved
f1b10 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c Lock(sqlite3_fil
f1b20 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 e *id, int *pRes
f1b30 4f 75 74 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c Out) {. unixFil
f1b40 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 e *pFile = (unix
f1b50 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 File*)id;. int
f1b60 72 63 20 3d 20 70 72 6f 78 79 54 61 6b 65 43 6f rc = proxyTakeCo
f1b70 6e 63 68 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 nch(pFile);. if
f1b80 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
f1b90 29 7b 0a 20 20 20 20 70 72 6f 78 79 4c 6f 63 6b ){. proxyLock
f1ba0 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 ingContext *pCtx
f1bb0 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 = (proxyLocking
f1bc0 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c 65 2d Context *)pFile-
f1bd0 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b >lockingContext;
f1be0 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 . unixFile *p
f1bf0 72 6f 78 79 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 roxy = pCtx->loc
f1c00 6b 50 72 6f 78 79 3b 0a 20 20 20 20 72 65 74 75 kProxy;. retu
f1c10 72 6e 20 70 72 6f 78 79 2d 3e 70 4d 65 74 68 6f rn proxy->pMetho
f1c20 64 2d 3e 78 43 68 65 63 6b 52 65 73 65 72 76 65 d->xCheckReserve
f1c30 64 4c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 dLock((sqlite3_f
f1c40 69 6c 65 2a 29 70 72 6f 78 79 2c 20 70 52 65 73 ile*)proxy, pRes
f1c50 4f 75 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 Out);. }. retu
f1c60 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
f1c70 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69 Lock the file wi
f1c80 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 th the lock spec
f1c90 69 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 ified by paramet
f1ca0 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e er locktype - on
f1cb0 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c e.** of the foll
f1cc0 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 owing:.**.**
f1cd0 20 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b (1) SHARED_LOCK
f1ce0 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53 45 .** (2) RESE
f1cf0 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 RVED_LOCK.**
f1d00 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 (3) PENDING_LOC
f1d10 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43 K.** (4) EXC
f1d20 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a LUSIVE_LOCK.**.*
f1d30 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e * Sometimes when
f1d40 20 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 requesting one
f1d50 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69 lock state, addi
f1d60 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 tional lock stat
f1d70 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 es.** are insert
f1d80 65 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 ed in between.
f1d90 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 The locking migh
f1da0 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 t fail on one of
f1db0 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 the later.** tr
f1dc0 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e ansitions leavin
f1dd0 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 g the lock state
f1de0 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 different from
f1df0 77 68 61 74 20 69 74 20 73 74 61 72 74 65 64 20 what it started
f1e00 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f but.** still sho
f1e10 72 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 rt of its goal.
f1e20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 The following c
f1e30 68 61 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 hart shows the a
f1e40 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 llowed.** transi
f1e50 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e tions and the in
f1e60 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69 serted intermedi
f1e70 61 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a ate states:.**.*
f1e80 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e * UNLOCKED ->
f1e90 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 SHARED.** SH
f1ea0 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44 ARED -> RESERVED
f1eb0 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e .** SHARED ->
f1ec0 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 (PENDING) -> EX
f1ed0 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 CLUSIVE.** RE
f1ee0 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 SERVED -> (PENDI
f1ef0 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 NG) -> EXCLUSIVE
f1f00 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d .** PENDING -
f1f10 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a > EXCLUSIVE.**.*
f1f20 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 * This routine w
f1f30 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 ill only increas
f1f40 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 e a lock. Use t
f1f50 68 65 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f he sqlite3OsUnlo
f1f60 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 ck().** routine
f1f70 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 to lower a locki
f1f80 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 ng level..*/.sta
f1f90 74 69 63 20 69 6e 74 20 70 72 6f 78 79 4c 6f 63 tic int proxyLoc
f1fa0 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a k(sqlite3_file *
f1fb0 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 id, int locktype
f1fc0 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a ) {. unixFile *
f1fd0 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c pFile = (unixFil
f1fe0 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 72 63 20 e*)id;. int rc
f1ff0 3d 20 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68 = proxyTakeConch
f2000 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 72 (pFile);. if( r
f2010 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
f2020 20 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 proxyLocking
f2030 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20 Context *pCtx =
f2040 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e (proxyLockingCon
f2050 74 65 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f text *)pFile->lo
f2060 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 ckingContext;.
f2070 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 72 6f 78 unixFile *prox
f2080 79 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 y = pCtx->lockPr
f2090 6f 78 79 3b 0a 20 20 20 20 72 63 20 3d 20 70 72 oxy;. rc = pr
f20a0 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 4c oxy->pMethod->xL
f20b0 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c ock((sqlite3_fil
f20c0 65 2a 29 70 72 6f 78 79 2c 20 6c 6f 63 6b 74 79 e*)proxy, lockty
f20d0 70 65 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e pe);. pFile->
f20e0 6c 6f 63 6b 74 79 70 65 20 3d 20 70 72 6f 78 79 locktype = proxy
f20f0 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 0a ->locktype;. }.
f2100 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
f2110 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 ./*.** Lower the
f2120 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f locking level o
f2130 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f n file descripto
f2140 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 r pFile to lockt
f2150 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a ype. locktype.*
f2160 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 * must be either
f2170 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 NO_LOCK or SHAR
f2180 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 ED_LOCK..**.** I
f2190 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 f the locking le
f21a0 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 vel of the file
f21b0 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c descriptor is al
f21c0 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f ready at or belo
f21d0 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 w.** the request
f21e0 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c ed locking level
f21f0 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 , this routine i
f2200 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 s a no-op..*/.st
f2210 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 55 6e atic int proxyUn
f2220 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c lock(sqlite3_fil
f2230 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 e *id, int lockt
f2240 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c ype) {. unixFil
f2250 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 e *pFile = (unix
f2260 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 File*)id;. int
f2270 72 63 20 3d 20 70 72 6f 78 79 54 61 6b 65 43 6f rc = proxyTakeCo
f2280 6e 63 68 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 nch(pFile);. if
f2290 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
f22a0 29 7b 0a 20 20 20 20 70 72 6f 78 79 4c 6f 63 6b ){. proxyLock
f22b0 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 ingContext *pCtx
f22c0 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 = (proxyLocking
f22d0 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c 65 2d Context *)pFile-
f22e0 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b >lockingContext;
f22f0 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 . unixFile *p
f2300 72 6f 78 79 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 roxy = pCtx->loc
f2310 6b 50 72 6f 78 79 3b 0a 20 20 20 20 72 63 20 3d kProxy;. rc =
f2320 20 70 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d proxy->pMethod-
f2330 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 6c 69 74 65 >xUnlock((sqlite
f2340 33 5f 66 69 6c 65 2a 29 70 72 6f 78 79 2c 20 6c 3_file*)proxy, l
f2350 6f 63 6b 74 79 70 65 29 3b 0a 20 20 20 20 70 46 ocktype);. pF
f2360 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 ile->locktype =
f2370 70 72 6f 78 79 2d 3e 6c 6f 63 6b 74 79 70 65 3b proxy->locktype;
f2380 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
f2390 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 ;.}../*.** Close
f23a0 20 61 20 66 69 6c 65 20 74 68 61 74 20 75 73 65 a file that use
f23b0 73 20 70 72 6f 78 79 20 6c 6f 63 6b 73 2e 0a 2a s proxy locks..*
f23c0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f /.static int pro
f23d0 78 79 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f xyClose(sqlite3_
f23e0 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69 66 file *id) {. if
f23f0 28 20 69 64 20 29 7b 0a 20 20 20 20 75 6e 69 78 ( id ){. unix
f2400 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 File *pFile = (u
f2410 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 nixFile*)id;.
f2420 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e proxyLockingCon
f2430 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 text *pCtx = (pr
f2440 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 oxyLockingContex
f2450 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 t *)pFile->locki
f2460 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75 ngContext;. u
f2470 6e 69 78 46 69 6c 65 20 2a 6c 6f 63 6b 50 72 6f nixFile *lockPro
f2480 78 79 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 xy = pCtx->lockP
f2490 72 6f 78 79 3b 0a 20 20 20 20 75 6e 69 78 46 69 roxy;. unixFi
f24a0 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65 20 3d 20 le *conchFile =
f24b0 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 3b pCtx->conchFile;
f24c0 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 53 51 . int rc = SQ
f24d0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 0a 20 20 LITE_OK;. .
f24e0 20 20 69 66 28 20 6c 6f 63 6b 50 72 6f 78 79 20 if( lockProxy
f24f0 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 6f ){. rc = lo
f2500 63 6b 50 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 ckProxy->pMethod
f2510 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 6c 69 74 ->xUnlock((sqlit
f2520 65 33 5f 66 69 6c 65 2a 29 6c 6f 63 6b 50 72 6f e3_file*)lockPro
f2530 78 79 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 xy, NO_LOCK);.
f2540 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 if( rc ) ret
f2550 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 72 63 urn rc;. rc
f2560 20 3d 20 6c 6f 63 6b 50 72 6f 78 79 2d 3e 70 4d = lockProxy->pM
f2570 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 28 73 ethod->xClose((s
f2580 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 6c 6f 63 qlite3_file*)loc
f2590 6b 50 72 6f 78 79 29 3b 0a 20 20 20 20 20 20 69 kProxy);. i
f25a0 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
f25b0 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 c;. sqlite3
f25c0 5f 66 72 65 65 28 6c 6f 63 6b 50 72 6f 78 79 29 _free(lockProxy)
f25d0 3b 0a 20 20 20 20 20 20 70 43 74 78 2d 3e 6c 6f ;. pCtx->lo
f25e0 63 6b 50 72 6f 78 79 20 3d 20 30 3b 0a 20 20 20 ckProxy = 0;.
f25f0 20 7d 0a 20 20 20 20 69 66 28 20 63 6f 6e 63 68 }. if( conch
f2600 46 69 6c 65 20 29 7b 0a 20 20 20 20 20 20 69 66 File ){. if
f2610 28 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48 65 6c ( pCtx->conchHel
f2620 64 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 d ){. rc
f2630 3d 20 70 72 6f 78 79 52 65 6c 65 61 73 65 43 6f = proxyReleaseCo
f2640 6e 63 68 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 nch(pFile);.
f2650 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 if( rc ) ret
f2660 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a urn rc;. }.
f2670 20 20 20 20 20 20 72 63 20 3d 20 63 6f 6e 63 68 rc = conch
f2680 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 File->pMethod->x
f2690 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f 66 Close((sqlite3_f
f26a0 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65 29 3b ile*)conchFile);
f26b0 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 . if( rc )
f26c0 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
f26d0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 63 6f sqlite3_free(co
f26e0 6e 63 68 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a nchFile);. }.
f26f0 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
f2700 28 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 (pCtx->lockProxy
f2710 50 61 74 68 29 3b 0a 20 20 20 20 73 71 6c 69 74 Path);. sqlit
f2720 65 33 5f 66 72 65 65 28 70 43 74 78 2d 3e 63 6f e3_free(pCtx->co
f2730 6e 63 68 46 69 6c 65 50 61 74 68 29 3b 0a 20 20 nchFilePath);.
f2740 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
f2750 43 74 78 2d 3e 64 62 50 61 74 68 29 3b 0a 20 20 Ctx->dbPath);.
f2760 20 20 2f 2a 20 72 65 73 74 6f 72 65 20 74 68 65 /* restore the
f2770 20 6f 72 69 67 69 6e 61 6c 20 6c 6f 63 6b 69 6e original lockin
f2780 67 20 63 6f 6e 74 65 78 74 20 61 6e 64 20 70 4d g context and pM
f2790 65 74 68 6f 64 20 74 68 65 6e 20 63 6c 6f 73 65 ethod then close
f27a0 20 69 74 20 2a 2f 0a 20 20 20 20 70 46 69 6c 65 it */. pFile
f27b0 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 ->lockingContext
f27c0 20 3d 20 70 43 74 78 2d 3e 6f 6c 64 4c 6f 63 6b = pCtx->oldLock
f27d0 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 ingContext;.
f27e0 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d pFile->pMethod =
f27f0 20 70 43 74 78 2d 3e 70 4f 6c 64 4d 65 74 68 6f pCtx->pOldMetho
f2800 64 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 d;. sqlite3_f
f2810 72 65 65 28 70 43 74 78 29 3b 0a 20 20 20 20 72 ree(pCtx);. r
f2820 65 74 75 72 6e 20 70 46 69 6c 65 2d 3e 70 4d 65 eturn pFile->pMe
f2830 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 69 64 29 thod->xClose(id)
f2840 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 ;. }. return S
f2850 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 0a 23 QLITE_OK;.}....#
f2860 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 endif /* defined
f2870 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 (__APPLE__) && S
f2880 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 QLITE_ENABLE_LOC
f2890 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 2f 2a KING_STYLE */./*
f28a0 0a 2a 2a 20 54 68 65 20 70 72 6f 78 79 20 6c 6f .** The proxy lo
f28b0 63 6b 69 6e 67 20 73 74 79 6c 65 20 69 73 20 69 cking style is i
f28c0 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 ntended for use
f28d0 77 69 74 68 20 41 46 50 20 66 69 6c 65 73 79 73 with AFP filesys
f28e0 74 65 6d 73 2e 0a 2a 2a 20 41 6e 64 20 73 69 6e tems..** And sin
f28f0 63 65 20 41 46 50 20 69 73 20 6f 6e 6c 79 20 73 ce AFP is only s
f2900 75 70 70 6f 72 74 65 64 20 6f 6e 20 4d 61 63 4f upported on MacO
f2910 53 58 2c 20 74 68 65 20 70 72 6f 78 79 20 6c 6f SX, the proxy lo
f2920 63 6b 69 6e 67 20 69 73 20 61 6c 73 6f 0a 2a 2a cking is also.**
f2930 20 72 65 73 74 72 69 63 74 65 64 20 74 6f 20 4d restricted to M
f2940 61 63 4f 53 58 2e 0a 2a 2a 20 0a 2a 2a 0a 2a 2a acOSX..** .**.**
f2950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2960 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 70 72 6f * End of the pro
f2970 78 79 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e xy lock implemen
f2980 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a tation *********
f2990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
f29a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f29b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f29c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f29d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f29e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f ************/../
f29f0 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 *.** Initialize
f2a00 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 the operating sy
f2a10 73 74 65 6d 20 69 6e 74 65 72 66 61 63 65 2e 0a stem interface..
f2a20 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
f2a30 6e 65 20 72 65 67 69 73 74 65 72 73 20 61 6c 6c ne registers all
f2a40 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 VFS implementat
f2a50 69 6f 6e 73 20 66 6f 72 20 75 6e 69 78 2d 6c 69 ions for unix-li
f2a60 6b 65 20 6f 70 65 72 61 74 69 6e 67 0a 2a 2a 20 ke operating.**
f2a70 73 79 73 74 65 6d 73 2e 20 20 54 68 69 73 20 72 systems. This r
f2a80 6f 75 74 69 6e 65 2c 20 61 6e 64 20 74 68 65 20 outine, and the
f2a90 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 sqlite3_os_end()
f2aa0 20 72 6f 75 74 69 6e 65 20 74 68 61 74 20 66 6f routine that fo
f2ab0 6c 6c 6f 77 73 2c 0a 2a 2a 20 73 68 6f 75 6c 64 llows,.** should
f2ac0 20 62 65 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 be the only rou
f2ad0 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69 tines in this fi
f2ae0 6c 65 20 74 68 61 74 20 61 72 65 20 76 69 73 69 le that are visi
f2af0 62 6c 65 20 66 72 6f 6d 20 6f 74 68 65 72 0a 2a ble from other.*
f2b00 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 * files..**.** T
f2b10 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 his routine is c
f2b20 61 6c 6c 65 64 20 6f 6e 63 65 20 64 75 72 69 6e alled once durin
f2b30 67 20 53 51 4c 69 74 65 20 69 6e 69 74 69 61 6c g SQLite initial
f2b40 69 7a 61 74 69 6f 6e 20 61 6e 64 20 62 79 20 61 ization and by a
f2b50 0a 2a 2a 20 73 69 6e 67 6c 65 20 74 68 72 65 61 .** single threa
f2b60 64 2e 20 20 54 68 65 20 6d 65 6d 6f 72 79 20 61 d. The memory a
f2b70 6c 6c 6f 63 61 74 69 6f 6e 20 61 6e 64 20 6d 75 llocation and mu
f2b80 74 65 78 20 73 75 62 73 79 73 74 65 6d 73 20 68 tex subsystems h
f2b90 61 76 65 20 6e 6f 74 0a 2a 2a 20 6e 65 63 65 73 ave not.** neces
f2ba0 73 61 72 69 6c 79 20 62 65 65 6e 20 69 6e 69 74 sarily been init
f2bb0 69 61 6c 69 7a 65 64 20 77 68 65 6e 20 74 68 69 ialized when thi
f2bc0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
f2bd0 6c 65 64 2c 20 61 6e 64 20 73 6f 20 74 68 65 79 led, and so they
f2be0 0a 2a 2a 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 .** should not b
f2bf0 65 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 e used..*/.SQLIT
f2c00 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
f2c10 33 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64 29 7b 3_os_init(void){
f2c20 20 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 54 68 65 . /* . ** The
f2c30 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f following macro
f2c40 20 64 65 66 69 6e 65 73 20 61 6e 20 69 6e 69 74 defines an init
f2c50 69 61 6c 69 7a 65 72 20 66 6f 72 20 61 6e 20 73 ializer for an s
f2c60 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 qlite3_vfs objec
f2c70 74 2e 0a 20 20 2a 2a 20 54 68 65 20 6e 61 6d 65 t.. ** The name
f2c80 20 6f 66 20 74 68 65 20 56 46 53 20 69 73 20 4e of the VFS is N
f2c90 41 4d 45 2e 20 20 54 68 65 20 70 41 70 70 44 61 AME. The pAppDa
f2ca0 74 61 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 ta is a pointer
f2cb0 74 6f 20 61 20 70 6f 69 6e 74 65 72 0a 20 20 2a to a pointer. *
f2cc0 2a 20 74 6f 20 74 68 65 20 22 66 69 6e 64 65 72 * to the "finder
f2cd0 22 20 66 75 6e 63 74 69 6f 6e 2e 20 20 28 70 41 " function. (pA
f2ce0 70 70 44 61 74 61 20 69 73 20 61 20 70 6f 69 6e ppData is a poin
f2cf0 74 65 72 20 74 6f 20 61 20 70 6f 69 6e 74 65 72 ter to a pointer
f2d00 20 62 65 63 61 75 73 65 0a 20 20 2a 2a 20 73 69 because. ** si
f2d10 6c 6c 79 20 43 39 30 20 72 75 6c 65 73 20 70 72 lly C90 rules pr
f2d20 6f 68 69 62 69 74 20 61 20 76 6f 69 64 2a 20 66 ohibit a void* f
f2d30 72 6f 6d 20 62 65 69 6e 67 20 63 61 73 74 20 74 rom being cast t
f2d40 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 o a function poi
f2d50 6e 74 65 72 0a 20 20 2a 2a 20 61 6e 64 20 73 6f nter. ** and so
f2d60 20 77 65 20 68 61 76 65 20 74 6f 20 67 6f 20 74 we have to go t
f2d70 68 72 6f 75 67 68 20 74 68 65 20 69 6e 74 65 72 hrough the inter
f2d80 6d 65 64 69 61 74 65 20 70 6f 69 6e 74 65 72 20 mediate pointer
f2d90 74 6f 20 61 76 6f 69 64 20 70 72 6f 62 6c 65 6d to avoid problem
f2da0 73 0a 20 20 2a 2a 20 77 68 65 6e 20 63 6f 6d 70 s. ** when comp
f2db0 69 6c 69 6e 67 20 77 69 74 68 20 2d 70 65 64 61 iling with -peda
f2dc0 6e 74 69 63 2d 65 72 72 6f 72 73 20 6f 6e 20 47 ntic-errors on G
f2dd0 43 43 2e 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 CC.). **. ** T
f2de0 68 65 20 46 49 4e 44 45 52 20 70 61 72 61 6d 65 he FINDER parame
f2df0 74 65 72 20 74 6f 20 74 68 69 73 20 6d 61 63 72 ter to this macr
f2e00 6f 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 o is the name of
f2e10 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 the pointer to
f2e20 74 68 65 0a 20 20 2a 2a 20 66 69 6e 64 65 72 2d the. ** finder-
f2e30 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 66 function. The f
f2e40 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20 72 inder-function r
f2e50 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 eturns a pointer
f2e60 20 74 6f 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c to the. ** sql
f2e70 69 74 65 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f ite_io_methods o
f2e80 62 6a 65 63 74 20 74 68 61 74 20 69 6d 70 6c 65 bject that imple
f2e90 6d 65 6e 74 73 20 74 68 65 20 64 65 73 69 72 65 ments the desire
f2ea0 64 20 6c 6f 63 6b 69 6e 67 0a 20 20 2a 2a 20 62 d locking. ** b
f2eb0 65 68 61 76 69 6f 72 73 2e 20 20 53 65 65 20 74 ehaviors. See t
f2ec0 68 65 20 64 69 76 69 73 69 6f 6e 20 61 62 6f 76 he division abov
f2ed0 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 e that contains
f2ee0 74 68 65 20 49 4f 4d 45 54 48 4f 44 53 0a 20 20 the IOMETHODS.
f2ef0 2a 2a 20 6d 61 63 72 6f 20 66 6f 72 20 61 64 64 ** macro for add
f2f00 69 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f ition informatio
f2f10 6e 20 6f 6e 20 66 69 6e 64 65 72 2d 66 75 6e 63 n on finder-func
f2f20 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a tions.. **. **
f2f30 20 4d 6f 73 74 20 66 69 6e 64 65 72 73 20 73 69 Most finders si
f2f40 6d 70 6c 79 20 72 65 74 75 72 6e 20 61 20 70 6f mply return a po
f2f50 69 6e 74 65 72 20 74 6f 20 61 20 66 69 78 65 64 inter to a fixed
f2f60 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
f2f70 6f 64 73 0a 20 20 2a 2a 20 6f 62 6a 65 63 74 2e ods. ** object.
f2f80 20 20 42 75 74 20 74 68 65 20 22 61 75 74 6f 6c But the "autol
f2f90 6f 63 6b 49 6f 46 69 6e 64 65 72 22 20 61 76 61 ockIoFinder" ava
f2fa0 69 6c 61 62 6c 65 20 6f 6e 20 4d 61 63 4f 53 58 ilable on MacOSX
f2fb0 20 64 6f 65 73 20 61 20 6c 69 74 74 6c 65 0a 20 does a little.
f2fc0 20 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 74 68 ** more than th
f2fd0 61 74 3b 20 69 74 20 6c 6f 6f 6b 73 20 61 74 20 at; it looks at
f2fe0 74 68 65 20 66 69 6c 65 73 79 73 74 65 6d 20 74 the filesystem t
f2ff0 79 70 65 20 74 68 61 74 20 68 6f 73 74 73 20 74 ype that hosts t
f3000 68 65 20 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 he . ** databas
f3010 65 20 66 69 6c 65 20 61 6e 64 20 74 72 69 65 73 e file and tries
f3020 20 74 6f 20 63 68 6f 6f 73 65 20 61 6e 20 6c 6f to choose an lo
f3030 63 6b 69 6e 67 20 6d 65 74 68 6f 64 20 61 70 70 cking method app
f3040 72 6f 70 72 69 61 74 65 20 66 6f 72 0a 20 20 2a ropriate for. *
f3050 2a 20 74 68 61 74 20 66 69 6c 65 73 79 73 74 65 * that filesyste
f3060 6d 20 74 69 6d 65 2e 0a 20 20 2a 2f 0a 20 20 23 m time.. */. #
f3070 64 65 66 69 6e 65 20 55 4e 49 58 56 46 53 28 56 define UNIXVFS(V
f3080 46 53 4e 41 4d 45 2c 20 46 49 4e 44 45 52 29 20 FSNAME, FINDER)
f3090 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
f30a0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 31 \. 1
f30b0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
f30c0 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e /* iVersion
f30d0 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 */
f30e0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 73 69 7a \. siz
f30f0 65 6f 66 28 75 6e 69 78 46 69 6c 65 29 2c 20 20 eof(unixFile),
f3100 20 20 20 2f 2a 20 73 7a 4f 73 46 69 6c 65 20 2a /* szOsFile *
f3110 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
f3120 20 20 20 20 20 5c 0a 20 20 20 20 4d 41 58 5f 50 \. MAX_P
f3130 41 54 48 4e 41 4d 45 2c 20 20 20 20 20 20 20 20 ATHNAME,
f3140 20 2f 2a 20 6d 78 50 61 74 68 6e 61 6d 65 20 2a /* mxPathname *
f3150 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
f3160 20 20 20 5c 0a 20 20 20 20 30 2c 20 20 20 20 20 \. 0,
f3170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
f3180 2a 20 70 4e 65 78 74 20 2a 2f 20 20 20 20 20 20 * pNext */
f3190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f31a0 20 5c 0a 20 20 20 20 56 46 53 4e 41 4d 45 2c 20 \. VFSNAME,
f31b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
f31c0 7a 4e 61 6d 65 20 2a 2f 20 20 20 20 20 20 20 20 zName */
f31d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
f31e0 0a 20 20 20 20 28 76 6f 69 64 2a 29 26 46 49 4e . (void*)&FIN
f31f0 44 45 52 2c 20 20 20 20 20 20 20 2f 2a 20 70 41 DER, /* pA
f3200 70 70 44 61 74 61 20 2a 2f 20 20 20 20 20 20 20 ppData */
f3210 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
f3220 20 20 20 75 6e 69 78 4f 70 65 6e 2c 20 20 20 20 unixOpen,
f3230 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 /* xOpe
f3240 6e 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 n */
f3250 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
f3260 20 75 6e 69 78 44 65 6c 65 74 65 2c 20 20 20 20 unixDelete,
f3270 20 20 20 20 20 20 20 2f 2a 20 78 44 65 6c 65 74 /* xDelet
f3280 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 e */
f3290 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 \. u
f32a0 6e 69 78 41 63 63 65 73 73 2c 20 20 20 20 20 20 nixAccess,
f32b0 20 20 20 20 20 2f 2a 20 78 41 63 63 65 73 73 20 /* xAccess
f32c0 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
f32d0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 \. uni
f32e0 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 20 xFullPathname,
f32f0 20 20 20 2f 2a 20 78 46 75 6c 6c 50 61 74 68 6e /* xFullPathn
f3300 61 6d 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 ame */
f3310 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 44 \. unixD
f3320 6c 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20 lOpen,
f3330 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f 20 20 /* xDlOpen */
f3340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f3350 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 44 6c 45 \. unixDlE
f3360 72 72 6f 72 2c 20 20 20 20 20 20 20 20 20 20 2f rror, /
f3370 2a 20 78 44 6c 45 72 72 6f 72 20 2a 2f 20 20 20 * xDlError */
f3380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f3390 20 5c 0a 20 20 20 20 75 6e 69 78 44 6c 53 79 6d \. unixDlSym
f33a0 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
f33b0 78 44 6c 53 79 6d 20 2a 2f 20 20 20 20 20 20 20 xDlSym */
f33c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
f33d0 0a 20 20 20 20 75 6e 69 78 44 6c 43 6c 6f 73 65 . unixDlClose
f33e0 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 , /* xD
f33f0 6c 43 6c 6f 73 65 20 2a 2f 20 20 20 20 20 20 20 lClose */
f3400 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
f3410 20 20 20 75 6e 69 78 52 61 6e 64 6f 6d 6e 65 73 unixRandomnes
f3420 73 2c 20 20 20 20 20 20 20 2f 2a 20 78 52 61 6e s, /* xRan
f3430 64 6f 6d 6e 65 73 73 20 2a 2f 20 20 20 20 20 20 domness */
f3440 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
f3450 20 75 6e 69 78 53 6c 65 65 70 2c 20 20 20 20 20 unixSleep,
f3460 20 20 20 20 20 20 20 2f 2a 20 78 53 6c 65 65 70 /* xSleep
f3470 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 */
f3480 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 \. u
f3490 6e 69 78 43 75 72 72 65 6e 74 54 69 6d 65 2c 20 nixCurrentTime,
f34a0 20 20 20 20 20 2f 2a 20 78 43 75 72 72 65 6e 74 /* xCurrent
f34b0 54 69 6d 65 20 2a 2f 20 20 20 20 20 20 20 20 20 Time */
f34c0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 \. uni
f34d0 78 47 65 74 4c 61 73 74 45 72 72 6f 72 20 20 20 xGetLastError
f34e0 20 20 20 2f 2a 20 78 47 65 74 4c 61 73 74 45 72 /* xGetLastEr
f34f0 72 6f 72 20 2a 2f 20 20 20 20 20 20 20 20 20 20 ror */
f3500 20 20 20 20 20 5c 0a 20 20 7d 0a 0a 20 20 2f 2a \. }.. /*
f3510 0a 20 20 2a 2a 20 41 6c 6c 20 64 65 66 61 75 6c . ** All defaul
f3520 74 20 56 46 53 65 73 20 66 6f 72 20 75 6e 69 78 t VFSes for unix
f3530 20 61 72 65 20 63 6f 6e 74 61 69 6e 65 64 20 69 are contained i
f3540 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 n the following
f3550 61 72 72 61 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a array.. **. **
f3560 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 73 Note that the s
f3570 71 6c 69 74 65 33 5f 76 66 73 2e 70 4e 65 78 74 qlite3_vfs.pNext
f3580 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 56 46 field of the VF
f3590 53 20 6f 62 6a 65 63 74 20 69 73 20 6d 6f 64 69 S object is modi
f35a0 66 69 65 64 0a 20 20 2a 2a 20 62 79 20 74 68 65 fied. ** by the
f35b0 20 53 51 4c 69 74 65 20 63 6f 72 65 20 77 68 65 SQLite core whe
f35c0 6e 20 74 68 65 20 56 46 53 20 69 73 20 72 65 67 n the VFS is reg
f35d0 69 73 74 65 72 65 64 2e 20 20 53 6f 20 74 68 65 istered. So the
f35e0 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 following. **
f35f0 61 72 72 61 79 20 63 61 6e 6e 6f 74 20 62 65 20 array cannot be
f3600 63 6f 6e 73 74 2e 0a 20 20 2a 2f 0a 20 20 73 74 const.. */. st
f3610 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76 66 73 atic sqlite3_vfs
f3620 20 61 56 66 73 5b 5d 20 3d 20 7b 0a 23 69 66 20 aVfs[] = {.#if
f3630 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f SQLITE_ENABLE_LO
f3640 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 28 CKING_STYLE && (
f3650 4f 53 5f 56 58 57 4f 52 4b 53 20 7c 7c 20 64 65 OS_VXWORKS || de
f3660 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 fined(__APPLE__)
f3670 29 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 ). UNIXVFS("u
f3680 6e 69 78 22 2c 20 20 20 20 20 20 20 20 20 20 61 nix", a
f3690 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 20 utolockIoFinder
f36a0 29 2c 0a 23 65 6c 73 65 0a 20 20 20 20 55 4e 49 ),.#else. UNI
f36b0 58 56 46 53 28 22 75 6e 69 78 22 2c 20 20 20 20 XVFS("unix",
f36c0 20 20 20 20 20 20 70 6f 73 69 78 49 6f 46 69 6e posixIoFin
f36d0 64 65 72 20 29 2c 0a 23 65 6e 64 69 66 0a 20 20 der ),.#endif.
f36e0 20 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d UNIXVFS("unix-
f36f0 6e 6f 6e 65 22 2c 20 20 20 20 20 6e 6f 6c 6f 63 none", noloc
f3700 6b 49 6f 46 69 6e 64 65 72 20 29 2c 0a 20 20 20 kIoFinder ),.
f3710 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 64 UNIXVFS("unix-d
f3720 6f 74 66 69 6c 65 22 2c 20 20 64 6f 74 6c 6f 63 otfile", dotloc
f3730 6b 49 6f 46 69 6e 64 65 72 20 29 2c 0a 20 20 20 kIoFinder ),.
f3740 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 77 UNIXVFS("unix-w
f3750 66 6c 22 2c 20 20 20 20 20 20 70 6f 73 69 78 57 fl", posixW
f3760 66 6c 49 6f 46 69 6e 64 65 72 20 29 2c 0a 23 69 flIoFinder ),.#i
f3770 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 f OS_VXWORKS.
f3780 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 6e UNIXVFS("unix-n
f3790 61 6d 65 64 73 65 6d 22 2c 20 73 65 6d 49 6f 46 amedsem", semIoF
f37a0 69 6e 64 65 72 20 29 2c 0a 23 65 6e 64 69 66 0a inder ),.#endif.
f37b0 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c #if SQLITE_ENABL
f37c0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a E_LOCKING_STYLE.
f37d0 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 6e 69 UNIXVFS("uni
f37e0 78 2d 70 6f 73 69 78 22 2c 20 20 20 20 70 6f 73 x-posix", pos
f37f0 69 78 49 6f 46 69 6e 64 65 72 20 29 2c 0a 23 69 ixIoFinder ),.#i
f3800 66 20 21 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 f !OS_VXWORKS.
f3810 20 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d UNIXVFS("unix-
f3820 66 6c 6f 63 6b 22 2c 20 20 20 20 66 6c 6f 63 6b flock", flock
f3830 49 6f 46 69 6e 64 65 72 20 29 2c 0a 23 65 6e 64 IoFinder ),.#end
f3840 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 if.#endif.#if SQ
f3850 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b LITE_ENABLE_LOCK
f3860 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 65 66 ING_STYLE && def
f3870 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a ined(__APPLE__).
f3880 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 6e 69 UNIXVFS("uni
f3890 78 2d 61 66 70 22 2c 20 20 20 20 20 20 61 66 70 x-afp", afp
f38a0 49 6f 46 69 6e 64 65 72 20 29 2c 0a 20 20 20 20 IoFinder ),.
f38b0 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 70 72 UNIXVFS("unix-pr
f38c0 6f 78 79 22 2c 20 20 20 20 70 72 6f 78 79 49 6f oxy", proxyIo
f38d0 46 69 6e 64 65 72 20 29 2c 0a 23 65 6e 64 69 66 Finder ),.#endif
f38e0 0a 20 20 7d 3b 0a 20 20 75 6e 73 69 67 6e 65 64 . };. unsigned
f38f0 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 int i;
f3900 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 /* Loop counter
f3910 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 67 69 73 74 */.. /* Regist
f3920 65 72 20 61 6c 6c 20 56 46 53 65 73 20 64 65 66 er all VFSes def
f3930 69 6e 65 64 20 69 6e 20 74 68 65 20 61 56 66 73 ined in the aVfs
f3940 5b 5d 20 61 72 72 61 79 20 2a 2f 0a 20 20 66 6f [] array */. fo
f3950 72 28 69 3d 30 3b 20 69 3c 28 73 69 7a 65 6f 66 r(i=0; i<(sizeof
f3960 28 61 56 66 73 29 2f 73 69 7a 65 6f 66 28 73 71 (aVfs)/sizeof(sq
f3970 6c 69 74 65 33 5f 76 66 73 29 29 3b 20 69 2b 2b lite3_vfs)); i++
f3980 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 ){. sqlite3_v
f3990 66 73 5f 72 65 67 69 73 74 65 72 28 26 61 56 66 fs_register(&aVf
f39a0 73 5b 69 5d 2c 20 69 3d 3d 30 29 3b 0a 20 20 7d s[i], i==0);. }
f39b0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
f39c0 5f 4f 4b 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 _OK; .}../*.** S
f39d0 68 75 74 64 6f 77 6e 20 74 68 65 20 6f 70 65 72 hutdown the oper
f39e0 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 ating system int
f39f0 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 53 6f erface..**.** So
f3a00 6d 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 me operating sys
f3a10 74 65 6d 73 20 6d 69 67 68 74 20 6e 65 65 64 20 tems might need
f3a20 74 6f 20 64 6f 20 73 6f 6d 65 20 63 6c 65 61 6e to do some clean
f3a30 75 70 20 69 6e 20 74 68 69 73 20 72 6f 75 74 69 up in this routi
f3a40 6e 65 2c 0a 2a 2a 20 74 6f 20 72 65 6c 65 61 73 ne,.** to releas
f3a50 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c e dynamically al
f3a60 6c 6f 63 61 74 65 64 20 6f 62 6a 65 63 74 73 2e located objects.
f3a70 20 20 42 75 74 20 6e 6f 74 20 6f 6e 20 75 6e 69 But not on uni
f3a80 78 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 x..** This routi
f3a90 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 20 66 6f ne is a no-op fo
f3aa0 72 20 75 6e 69 78 2e 0a 2a 2f 0a 53 51 4c 49 54 r unix..*/.SQLIT
f3ab0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
f3ac0 33 5f 6f 73 5f 65 6e 64 28 76 6f 69 64 29 7b 20 3_os_end(void){
f3ad0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
f3ae0 5f 4f 4b 3b 20 0a 7d 0a 20 0a 23 65 6e 64 69 66 _OK; .}. .#endif
f3af0 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e /* SQLITE_OS_UN
f3b00 49 58 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a IX */../********
f3b10 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 ****** End of os
f3b20 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a _unix.c ********
f3b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3b50 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
f3b60 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
f3b70 65 20 6f 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a e os_win.c *****
f3b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3ba0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
f3bb0 34 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 4 May 22.**.** T
f3bc0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
f3bd0 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
f3be0 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
f3bf0 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
f3c00 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
f3c10 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
f3c20 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
f3c30 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
f3c40 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
f3c50 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
f3c60 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
f3c70 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
f3c80 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
f3c90 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
f3ca0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
f3cb0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
f3cc0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
f3cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
f3d20 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
f3d30 74 61 69 6e 73 20 63 6f 64 65 20 74 68 61 74 20 tains code that
f3d40 69 73 20 73 70 65 63 69 66 69 63 20 74 6f 20 77 is specific to w
f3d50 69 6e 64 6f 77 73 2e 0a 2a 2f 0a 23 69 66 20 53 indows..*/.#if S
f3d60 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 20 20 20 QLITE_OS_WIN
f3d70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
f3d80 69 73 20 66 69 6c 65 20 69 73 20 75 73 65 64 20 is file is used
f3d90 66 6f 72 20 77 69 6e 64 6f 77 73 20 6f 6e 6c 79 for windows only
f3da0 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e 6f */.../*.** A No
f3db0 74 65 20 41 62 6f 75 74 20 4d 65 6d 6f 72 79 20 te About Memory
f3dc0 41 6c 6c 6f 63 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a Allocation:.**.*
f3dd0 2a 20 54 68 69 73 20 64 72 69 76 65 72 20 75 73 * This driver us
f3de0 65 73 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65 es malloc()/free
f3df0 28 29 20 64 69 72 65 63 74 6c 79 20 72 61 74 68 () directly rath
f3e00 65 72 20 74 68 61 6e 20 67 6f 69 6e 67 20 74 68 er than going th
f3e10 72 6f 75 67 68 0a 2a 2a 20 74 68 65 20 53 51 4c rough.** the SQL
f3e20 69 74 65 2d 77 72 61 70 70 65 72 73 20 73 71 6c ite-wrappers sql
f3e30 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2f 73 71 ite3_malloc()/sq
f3e40 6c 69 74 65 33 5f 66 72 65 65 28 29 2e 20 20 54 lite3_free(). T
f3e50 68 6f 73 65 20 77 72 61 70 70 65 72 73 0a 2a 2a hose wrappers.**
f3e60 20 61 72 65 20 64 65 73 69 67 6e 65 64 20 66 6f are designed fo
f3e70 72 20 75 73 65 20 6f 6e 20 65 6d 62 65 64 64 65 r use on embedde
f3e80 64 20 73 79 73 74 65 6d 73 20 77 68 65 72 65 20 d systems where
f3e90 6d 65 6d 6f 72 79 20 69 73 20 73 63 61 72 63 65 memory is scarce
f3ea0 20 61 6e 64 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 and.** malloc f
f3eb0 61 69 6c 75 72 65 73 20 68 61 70 70 65 6e 20 66 ailures happen f
f3ec0 72 65 71 75 65 6e 74 6c 79 2e 20 20 57 69 6e 33 requently. Win3
f3ed0 32 20 64 6f 65 73 20 6e 6f 74 20 74 79 70 69 63 2 does not typic
f3ee0 61 6c 6c 79 20 72 75 6e 20 6f 6e 0a 2a 2a 20 65 ally run on.** e
f3ef0 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d 73 2c mbedded systems,
f3f00 20 61 6e 64 20 77 68 65 6e 20 69 74 20 64 6f 65 and when it doe
f3f10 73 20 74 68 65 20 64 65 76 65 6c 6f 70 65 72 73 s the developers
f3f20 20 6e 6f 72 6d 61 6c 6c 79 20 68 61 76 65 20 62 normally have b
f3f30 69 67 67 65 72 0a 2a 2a 20 70 72 6f 62 6c 65 6d igger.** problem
f3f40 73 20 74 6f 20 77 6f 72 72 79 20 61 62 6f 75 74 s to worry about
f3f50 20 74 68 61 6e 20 72 75 6e 6e 69 6e 67 20 6f 75 than running ou
f3f60 74 20 6f 66 20 6d 65 6d 6f 72 79 2e 20 20 53 6f t of memory. So
f3f70 20 74 68 65 72 65 20 69 73 20 6e 6f 74 0a 2a 2a there is not.**
f3f80 20 61 20 63 6f 6d 70 65 6c 6c 69 6e 67 20 6e 65 a compelling ne
f3f90 65 64 20 74 6f 20 75 73 65 20 74 68 65 20 77 72 ed to use the wr
f3fa0 61 70 70 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 42 75 appers..**.** Bu
f3fb0 74 20 74 68 65 72 65 20 69 73 20 61 20 67 6f 6f t there is a goo
f3fc0 64 20 72 65 61 73 6f 6e 20 74 6f 20 6e 6f 74 20 d reason to not
f3fd0 75 73 65 20 74 68 65 20 77 72 61 70 70 65 72 73 use the wrappers
f3fe0 2e 20 20 49 66 20 77 65 20 75 73 65 20 74 68 65 . If we use the
f3ff0 0a 2a 2a 20 77 72 61 70 70 65 72 73 20 74 68 65 .** wrappers the
f4000 6e 20 77 65 20 77 69 6c 6c 20 67 65 74 20 73 69 n we will get si
f4010 6d 75 6c 61 74 65 64 20 6d 61 6c 6c 6f 63 28 29 mulated malloc()
f4020 20 66 61 69 6c 75 72 65 73 20 77 69 74 68 69 6e failures within
f4030 20 74 68 69 73 0a 2a 2a 20 64 72 69 76 65 72 2e this.** driver.
f4040 20 20 41 6e 64 20 74 68 61 74 20 63 61 75 73 65 And that cause
f4050 73 20 61 6c 6c 20 6b 69 6e 64 73 20 6f 66 20 70 s all kinds of p
f4060 72 6f 62 6c 65 6d 73 20 66 6f 72 20 6f 75 72 20 roblems for our
f4070 74 65 73 74 73 2e 20 20 57 65 0a 2a 2a 20 63 6f tests. We.** co
f4080 75 6c 64 20 65 6e 68 61 6e 63 65 20 53 51 4c 69 uld enhance SQLi
f4090 74 65 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 te to deal with
f40a0 73 69 6d 75 6c 61 74 65 64 20 6d 61 6c 6c 6f 63 simulated malloc
f40b0 20 66 61 69 6c 75 72 65 73 20 77 69 74 68 69 6e failures within
f40c0 0a 2a 2a 20 74 68 65 20 4f 53 20 64 72 69 76 65 .** the OS drive
f40d0 72 2c 20 62 75 74 20 74 68 65 20 63 6f 64 65 20 r, but the code
f40e0 74 6f 20 64 65 61 6c 20 77 69 74 68 20 74 68 6f to deal with tho
f40f0 73 65 20 66 61 69 6c 75 72 65 20 77 6f 75 6c 64 se failure would
f4100 20 6e 6f 74 0a 2a 2a 20 62 65 20 65 78 65 72 63 not.** be exerc
f4110 69 73 65 64 20 6f 6e 20 4c 69 6e 75 78 20 28 77 ised on Linux (w
f4120 68 69 63 68 20 64 6f 65 73 20 6e 6f 74 20 6e 65 hich does not ne
f4130 65 64 20 74 6f 20 6d 61 6c 6c 6f 63 28 29 20 69 ed to malloc() i
f4140 6e 20 74 68 65 20 64 72 69 76 65 72 29 0a 2a 2a n the driver).**
f4150 20 61 6e 64 20 73 6f 20 77 65 20 77 6f 75 6c 64 and so we would
f4160 20 68 61 76 65 20 64 69 66 66 69 63 75 6c 74 79 have difficulty
f4170 20 77 72 69 74 69 6e 67 20 63 6f 76 65 72 61 67 writing coverag
f4180 65 20 74 65 73 74 73 20 66 6f 72 20 74 68 61 74 e tests for that
f4190 0a 2a 2a 20 63 6f 64 65 2e 20 20 42 65 74 74 65 .** code. Bette
f41a0 72 20 74 6f 20 6c 65 61 76 65 20 74 68 65 20 63 r to leave the c
f41b0 6f 64 65 20 6f 75 74 2c 20 77 65 20 74 68 69 6e ode out, we thin
f41c0 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 k..**.** The poi
f41d0 6e 74 20 6f 66 20 74 68 69 73 20 64 69 73 63 75 nt of this discu
f41e0 73 73 69 6f 6e 20 69 73 20 61 73 20 66 6f 6c 6c ssion is as foll
f41f0 6f 77 73 3a 20 20 57 68 65 6e 20 63 72 65 61 74 ows: When creat
f4200 69 6e 67 20 61 20 6e 65 77 0a 2a 2a 20 4f 53 20 ing a new.** OS
f4210 6c 61 79 65 72 20 66 6f 72 20 61 6e 20 65 6d 62 layer for an emb
f4220 65 64 64 65 64 20 73 79 73 74 65 6d 2c 20 69 66 edded system, if
f4230 20 79 6f 75 20 75 73 65 20 74 68 69 73 20 66 69 you use this fi
f4240 6c 65 20 61 73 20 61 6e 20 65 78 61 6d 70 6c 65 le as an example
f4250 2c 0a 2a 2a 20 61 76 6f 69 64 20 74 68 65 20 75 ,.** avoid the u
f4260 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2f 66 se of malloc()/f
f4270 72 65 65 28 29 2e 20 20 54 68 6f 73 65 20 72 6f ree(). Those ro
f4280 75 74 69 6e 65 73 20 77 6f 72 6b 20 6f 6b 20 6f utines work ok o
f4290 6e 20 77 69 6e 64 6f 77 73 0a 2a 2a 20 64 65 73 n windows.** des
f42a0 6b 74 6f 70 73 20 62 75 74 20 6e 6f 74 20 73 6f ktops but not so
f42b0 20 77 65 6c 6c 20 69 6e 20 65 6d 62 65 64 64 65 well in embedde
f42c0 64 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a 0a 23 d systems..*/..#
f42d0 69 6e 63 6c 75 64 65 20 3c 77 69 6e 62 61 73 65 include <winbase
f42e0 2e 68 3e 0a 0a 23 69 66 64 65 66 20 5f 5f 43 59 .h>..#ifdef __CY
f42f0 47 57 49 4e 5f 5f 0a 23 20 69 6e 63 6c 75 64 65 GWIN__.# include
f4300 20 3c 73 79 73 2f 63 79 67 77 69 6e 2e 68 3e 0a <sys/cygwin.h>.
f4310 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 #endif../*.** Ma
f4320 63 72 6f 73 20 75 73 65 64 20 74 6f 20 64 65 74 cros used to det
f4330 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f ermine whether o
f4340 72 20 6e 6f 74 20 74 6f 20 75 73 65 20 74 68 72 r not to use thr
f4350 65 61 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 eads..*/.#if def
f4360 69 6e 65 64 28 54 48 52 45 41 44 53 41 46 45 29 ined(THREADSAFE)
f4370 20 26 26 20 54 48 52 45 41 44 53 41 46 45 0a 23 && THREADSAFE.#
f4380 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 define SQLITE_W
f4390 33 32 5f 54 48 52 45 41 44 53 20 31 0a 23 65 6e 32_THREADS 1.#en
f43a0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 dif../*.** Inclu
f43b0 64 65 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 de code that is
f43c0 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6f 73 common to all os
f43d0 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f 0a 2f 2a _*.c files.*/./*
f43e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e ************* In
f43f0 63 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e clude os_common.
f4400 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 h in the middle
f4410 6f 66 20 6f 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a of os_win.c ****
f4420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
f4430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
f4440 67 69 6e 20 66 69 6c 65 20 6f 73 5f 63 6f 6d 6d gin file os_comm
f4450 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a on.h ***********
f4460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
f4480 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 32 0a .** 2004 May 22.
f4490 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
f44a0 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
f44b0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
f44c0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
f44d0 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
f44e0 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
f44f0 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
f4500 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
f4510 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
f4520 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
f4530 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
f4540 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
f4550 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
f4560 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
f4570 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
f4580 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
f4590 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
f45a0 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
f45b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f45c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f45d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f45e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f45f0 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ***.**.** This f
f4600 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6d 61 63 ile contains mac
f4610 72 6f 73 20 61 6e 64 20 61 20 6c 69 74 74 6c 65 ros and a little
f4620 20 62 69 74 20 6f 66 20 63 6f 64 65 20 74 68 61 bit of code tha
f4630 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a t is common to.*
f4640 2a 20 61 6c 6c 20 6f 66 20 74 68 65 20 70 6c 61 * all of the pla
f4650 74 66 6f 72 6d 2d 73 70 65 63 69 66 69 63 20 66 tform-specific f
f4660 69 6c 65 73 20 28 6f 73 5f 2a 2e 63 29 20 61 6e iles (os_*.c) an
f4670 64 20 69 73 20 23 69 6e 63 6c 75 64 65 64 20 69 d is #included i
f4680 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a 20 66 69 6c nto those.** fil
f4690 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 es..**.** This f
f46a0 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 23 69 ile should be #i
f46b0 6e 63 6c 75 64 65 64 20 62 79 20 74 68 65 20 6f ncluded by the o
f46c0 73 5f 2a 2e 63 20 66 69 6c 65 73 20 6f 6e 6c 79 s_*.c files only
f46d0 2e 20 20 49 74 20 69 73 20 6e 6f 74 20 61 0a 2a . It is not a.*
f46e0 2a 20 67 65 6e 65 72 61 6c 20 70 75 72 70 6f 73 * general purpos
f46f0 65 20 68 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a e header file..*
f4700 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f 63 6f 6d *.** $Id: os_com
f4710 6d 6f 6e 2e 68 2c 76 20 31 2e 33 38 20 32 30 30 mon.h,v 1.38 200
f4720 39 2f 30 32 2f 32 34 20 31 38 3a 34 30 3a 35 30 9/02/24 18:40:50
f4730 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 danielk1977 Exp
f4740 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 4f $.*/.#ifndef _O
f4750 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23 64 65 66 S_COMMON_H_.#def
f4760 69 6e 65 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 ine _OS_COMMON_H
f4770 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 _../*.** At leas
f4780 74 20 74 77 6f 20 62 75 67 73 20 68 61 76 65 20 t two bugs have
f4790 73 6c 69 70 70 65 64 20 69 6e 20 62 65 63 61 75 slipped in becau
f47a0 73 65 20 77 65 20 63 68 61 6e 67 65 64 20 74 68 se we changed th
f47b0 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 2a e MEMORY_DEBUG.*
f47c0 2a 20 6d 61 63 72 6f 20 74 6f 20 53 51 4c 49 54 * macro to SQLIT
f47d0 45 5f 44 45 42 55 47 20 61 6e 64 20 73 6f 6d 65 E_DEBUG and some
f47e0 20 6f 6c 64 65 72 20 6d 61 6b 65 66 69 6c 65 73 older makefiles
f47f0 20 68 61 76 65 20 6e 6f 74 20 79 65 74 20 6d 61 have not yet ma
f4800 64 65 20 74 68 65 0a 2a 2a 20 73 77 69 74 63 68 de the.** switch
f4810 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 . The following
f4820 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 63 61 74 code should cat
f4830 63 68 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 ch this problem
f4840 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e at compile-time.
f4850 0a 2a 2f 0a 23 69 66 64 65 66 20 4d 45 4d 4f 52 .*/.#ifdef MEMOR
f4860 59 5f 44 45 42 55 47 0a 23 20 65 72 72 6f 72 20 Y_DEBUG.# error
f4870 22 54 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 "The MEMORY_DEBU
f4880 47 20 6d 61 63 72 6f 20 69 73 20 6f 62 73 6f 6c G macro is obsol
f4890 65 74 65 2e 20 20 55 73 65 20 53 51 4c 49 54 45 ete. Use SQLITE
f48a0 5f 44 45 42 55 47 20 69 6e 73 74 65 61 64 2e 22 _DEBUG instead."
f48b0 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 .#endif..#ifdef
f48c0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c SQLITE_DEBUG.SQL
f48d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
f48e0 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 3d sqlite3OSTrace =
f48f0 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 0;.#define OSTR
f4900 41 43 45 31 28 58 29 20 20 20 20 20 20 20 20 20 ACE1(X)
f4910 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 if( sqlite3OSTra
f4920 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 ce ) sqlite3Debu
f4930 67 50 72 69 6e 74 66 28 58 29 0a 23 64 65 66 69 gPrintf(X).#defi
f4940 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 ne OSTRACE2(X,Y)
f4950 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 if( sqlit
f4960 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 e3OSTrace ) sqli
f4970 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 te3DebugPrintf(X
f4980 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 ,Y).#define OSTR
f4990 41 43 45 33 28 58 2c 59 2c 5a 29 20 20 20 20 20 ACE3(X,Y,Z)
f49a0 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 if( sqlite3OSTra
f49b0 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 ce ) sqlite3Debu
f49c0 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 29 0a 23 gPrintf(X,Y,Z).#
f49d0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28 define OSTRACE4(
f49e0 58 2c 59 2c 5a 2c 41 29 20 20 20 69 66 28 20 73 X,Y,Z,A) if( s
f49f0 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 qlite3OSTrace )
f4a00 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
f4a10 74 66 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 tf(X,Y,Z,A).#def
f4a20 69 6e 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59 ine OSTRACE5(X,Y
f4a30 2c 5a 2c 41 2c 42 29 20 69 66 28 20 73 71 6c 69 ,Z,A,B) if( sqli
f4a40 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c te3OSTrace ) sql
f4a50 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 ite3DebugPrintf(
f4a60 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 X,Y,Z,A,B).#defi
f4a70 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c ne OSTRACE6(X,Y,
f4a80 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20 20 20 20 69 Z,A,B,C) \. i
f4a90 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 f(sqlite3OSTrace
f4aa0 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 ) sqlite3DebugPr
f4ab0 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 intf(X,Y,Z,A,B,C
f4ac0 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
f4ad0 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 E7(X,Y,Z,A,B,C,D
f4ae0 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 ) \. if(sqlit
f4af0 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 e3OSTrace) sqlit
f4b00 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c e3DebugPrintf(X,
f4b10 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6c Y,Z,A,B,C,D).#el
f4b20 73 65 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 se.#define OSTRA
f4b30 43 45 31 28 58 29 0a 23 64 65 66 69 6e 65 20 4f CE1(X).#define O
f4b40 53 54 52 41 43 45 32 28 58 2c 59 29 0a 23 64 65 STRACE2(X,Y).#de
f4b50 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c fine OSTRACE3(X,
f4b60 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 Y,Z).#define OST
f4b70 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 0a 23 RACE4(X,Y,Z,A).#
f4b80 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28 define OSTRACE5(
f4b90 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 X,Y,Z,A,B).#defi
f4ba0 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c ne OSTRACE6(X,Y,
f4bb0 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 Z,A,B,C).#define
f4bc0 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c OSTRACE7(X,Y,Z,
f4bd0 41 2c 42 2c 43 2c 44 29 0a 23 65 6e 64 69 66 0a A,B,C,D).#endif.
f4be0 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f ./*.** Macros fo
f4bf0 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 72 r performance tr
f4c00 61 63 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79 acing. Normally
f4c10 20 74 75 72 6e 65 64 20 6f 66 66 2e 20 20 4f 6e turned off. On
f4c20 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e 20 69 ly works.** on i
f4c30 34 38 36 20 68 61 72 64 77 61 72 65 2e 0a 2a 2f 486 hardware..*/
f4c40 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 50 .#ifdef SQLITE_P
f4c50 45 52 46 4f 52 4d 41 4e 43 45 5f 54 52 41 43 45 ERFORMANCE_TRACE
f4c60 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d 65 2e ../* .** hwtime.
f4c70 68 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e h contains inlin
f4c80 65 20 61 73 73 65 6d 62 6c 65 72 20 63 6f 64 65 e assembler code
f4c90 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e for implementin
f4ca0 67 20 0a 2a 2a 20 68 69 67 68 2d 70 65 72 66 6f g .** high-perfo
f4cb0 72 6d 61 6e 63 65 20 74 69 6d 69 6e 67 20 72 6f rmance timing ro
f4cc0 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a utines..*/./****
f4cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 ********** Inclu
f4ce0 64 65 20 68 77 74 69 6d 65 2e 68 20 69 6e 20 74 de hwtime.h in t
f4cf0 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f he middle of os_
f4d00 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a common.h *******
f4d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
f4d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
f4d30 20 66 69 6c 65 20 68 77 74 69 6d 65 2e 68 20 2a file hwtime.h *
f4d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
f4d70 20 32 30 30 38 20 4d 61 79 20 32 37 0a 2a 2a 0a 2008 May 27.**.
f4d80 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
f4d90 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
f4da0 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
f4db0 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
f4dc0 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
f4dd0 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
f4de0 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
f4df0 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
f4e00 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
f4e10 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
f4e20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
f4e30 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
f4e40 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
f4e50 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
f4e60 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
f4e70 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
f4e80 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
f4e90 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
f4ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4ee0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
f4ef0 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 contains inline
f4f00 20 61 73 6d 20 63 6f 64 65 20 66 6f 72 20 72 65 asm code for re
f4f10 74 72 69 65 76 69 6e 67 20 22 68 69 67 68 2d 70 trieving "high-p
f4f20 65 72 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a 20 63 erformance".** c
f4f30 6f 75 6e 74 65 72 73 20 66 6f 72 20 78 38 36 20 ounters for x86
f4f40 63 6c 61 73 73 20 43 50 55 73 2e 0a 2a 2a 0a 2a class CPUs..**.*
f4f50 2a 20 24 49 64 3a 20 68 77 74 69 6d 65 2e 68 2c * $Id: hwtime.h,
f4f60 76 20 31 2e 33 20 32 30 30 38 2f 30 38 2f 30 31 v 1.3 2008/08/01
f4f70 20 31 34 3a 33 33 3a 31 35 20 73 68 61 6e 65 20 14:33:15 shane
f4f80 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 Exp $.*/.#ifndef
f4f90 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 23 64 65 66 _HWTIME_H_.#def
f4fa0 69 6e 65 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 0a ine _HWTIME_H_..
f4fb0 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
f4fc0 69 6e 67 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 ing routine only
f4fd0 20 77 6f 72 6b 73 20 6f 6e 20 70 65 6e 74 69 75 works on pentiu
f4fe0 6d 2d 63 6c 61 73 73 20 28 6f 72 20 6e 65 77 65 m-class (or newe
f4ff0 72 29 20 70 72 6f 63 65 73 73 6f 72 73 2e 0a 2a r) processors..*
f5000 2a 20 49 74 20 75 73 65 73 20 74 68 65 20 52 44 * It uses the RD
f5010 54 53 43 20 6f 70 63 6f 64 65 20 74 6f 20 72 65 TSC opcode to re
f5020 61 64 20 74 68 65 20 63 79 63 6c 65 20 63 6f 75 ad the cycle cou
f5030 6e 74 20 76 61 6c 75 65 20 6f 75 74 20 6f 66 20 nt value out of
f5040 74 68 65 0a 2a 2a 20 70 72 6f 63 65 73 73 6f 72 the.** processor
f5050 20 61 6e 64 20 72 65 74 75 72 6e 73 20 74 68 61 and returns tha
f5060 74 20 76 61 6c 75 65 2e 20 20 54 68 69 73 20 63 t value. This c
f5070 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20 68 an be used for h
f5080 69 67 68 2d 72 65 73 0a 2a 2a 20 70 72 6f 66 69 igh-res.** profi
f5090 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66 20 28 64 65 ling..*/.#if (de
f50a0 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 fined(__GNUC__)
f50b0 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f || defined(_MSC_
f50c0 56 45 52 29 29 20 26 26 20 5c 0a 20 20 20 20 20 VER)) && \.
f50d0 20 28 64 65 66 69 6e 65 64 28 69 33 38 36 29 20 (defined(i386)
f50e0 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 69 33 38 || defined(__i38
f50f0 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 6__) || defined(
f5100 5f 4d 5f 49 58 38 36 29 29 0a 0a 20 20 23 69 66 _M_IX86)).. #if
f5110 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f defined(__GNUC_
f5120 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f _).. __inline__
f5130 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 sqlite_uint64 s
f5140 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 qlite3Hwtime(voi
f5150 64 29 7b 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 d){. unsigne
f5160 64 20 69 6e 74 20 6c 6f 2c 20 68 69 3b 0a 20 20 d int lo, hi;.
f5170 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c __asm__ __vol
f5180 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 atile__ ("rdtsc"
f5190 20 3a 20 22 3d 61 22 20 28 6c 6f 29 2c 20 22 3d : "=a" (lo), "=
f51a0 64 22 20 28 68 69 29 29 3b 0a 20 20 20 20 20 72 d" (hi));. r
f51b0 65 74 75 72 6e 20 28 73 71 6c 69 74 65 5f 75 69 eturn (sqlite_ui
f51c0 6e 74 36 34 29 68 69 20 3c 3c 20 33 32 20 7c 20 nt64)hi << 32 |
f51d0 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23 65 6c 69 66 lo;. }.. #elif
f51e0 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 defined(_MSC_VE
f51f0 52 29 0a 0a 20 20 5f 5f 64 65 63 6c 73 70 65 63 R).. __declspec
f5200 28 6e 61 6b 65 64 29 20 5f 5f 69 6e 6c 69 6e 65 (naked) __inline
f5210 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 5f sqlite_uint64 _
f5220 5f 63 64 65 63 6c 20 73 71 6c 69 74 65 33 48 77 _cdecl sqlite3Hw
f5230 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 time(void){.
f5240 20 5f 5f 61 73 6d 20 7b 0a 20 20 20 20 20 20 20 __asm {.
f5250 20 72 64 74 73 63 0a 20 20 20 20 20 20 20 20 72 rdtsc. r
f5260 65 74 20 20 20 20 20 20 20 3b 20 72 65 74 75 72 et ; retur
f5270 6e 20 76 61 6c 75 65 20 61 74 20 45 44 58 3a 45 n value at EDX:E
f5280 41 58 0a 20 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 AX. }. }..
f5290 20 23 65 6e 64 69 66 0a 0a 23 65 6c 69 66 20 28 #endif..#elif (
f52a0 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f defined(__GNUC__
f52b0 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 78 ) && defined(__x
f52c0 38 36 5f 36 34 5f 5f 29 29 0a 0a 20 20 5f 5f 69 86_64__)).. __i
f52d0 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 nline__ sqlite_u
f52e0 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 int64 sqlite3Hwt
f52f0 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 ime(void){.
f5300 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 76 unsigned long v
f5310 61 6c 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f al;. __asm_
f5320 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 _ __volatile__ (
f5330 22 72 64 74 73 63 22 20 3a 20 22 3d 41 22 20 28 "rdtsc" : "=A" (
f5340 76 61 6c 29 29 3b 0a 20 20 20 20 20 20 72 65 74 val));. ret
f5350 75 72 6e 20 76 61 6c 3b 0a 20 20 7d 0a 20 0a 23 urn val;. }. .#
f5360 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f elif (defined(__
f5370 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e GNUC__) && defin
f5380 65 64 28 5f 5f 70 70 63 5f 5f 29 29 0a 0a 20 20 ed(__ppc__))..
f5390 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 __inline__ sqlit
f53a0 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 e_uint64 sqlite3
f53b0 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 Hwtime(void){.
f53c0 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e unsigned lon
f53d0 67 20 6c 6f 6e 67 20 72 65 74 76 61 6c 3b 0a 20 g long retval;.
f53e0 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f unsigned lo
f53f0 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20 20 20 20 5f ng junk;. _
f5400 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c _asm__ __volatil
f5410 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20 20 20 20 20 e__ ("\n\.
f5420 20 20 20 20 31 3a 20 20 20 20 20 20 6d 66 74 62 1: mftb
f5430 75 20 20 20 25 31 5c 6e 5c 0a 20 20 20 20 20 20 u %1\n\.
f5440 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 62 mftb
f5450 20 20 20 20 25 4c 30 5c 6e 5c 0a 20 20 20 20 20 %L0\n\.
f5460 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 mft
f5470 62 75 20 20 20 25 30 5c 6e 5c 0a 20 20 20 20 20 bu %0\n\.
f5480 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6d 70 cmp
f5490 77 20 20 20 20 25 30 2c 25 31 5c 6e 5c 0a 20 20 w %0,%1\n\.
f54a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f54b0 62 6e 65 20 20 20 20 20 31 62 22 0a 20 20 20 20 bne 1b".
f54c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 3a 20 :
f54d0 22 3d 72 22 20 28 72 65 74 76 61 6c 29 2c 20 22 "=r" (retval), "
f54e0 3d 72 22 20 28 6a 75 6e 6b 29 29 3b 0a 20 20 20 =r" (junk));.
f54f0 20 20 20 72 65 74 75 72 6e 20 72 65 74 76 61 6c return retval
f5500 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a 0a 20 20 ;. }..#else..
f5510 23 65 72 72 6f 72 20 4e 65 65 64 20 69 6d 70 6c #error Need impl
f5520 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 ementation of sq
f5530 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f lite3Hwtime() fo
f5540 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2e r your platform.
f5550 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 6f 20 63 .. /*. ** To c
f5560 6f 6d 70 69 6c 65 20 77 69 74 68 6f 75 74 20 69 ompile without i
f5570 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 73 71 6c 69 mplementing sqli
f5580 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20 te3Hwtime() for
f5590 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2c 0a 20 your platform,.
f55a0 20 2a 2a 20 79 6f 75 20 63 61 6e 20 72 65 6d 6f ** you can remo
f55b0 76 65 20 74 68 65 20 61 62 6f 76 65 20 23 65 72 ve the above #er
f55c0 72 6f 72 20 61 6e 64 20 75 73 65 20 74 68 65 20 ror and use the
f55d0 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 73 following. ** s
f55e0 74 75 62 20 66 75 6e 63 74 69 6f 6e 2e 20 20 59 tub function. Y
f55f0 6f 75 20 77 69 6c 6c 20 6c 6f 73 65 20 74 69 6d ou will lose tim
f5600 69 6e 67 20 73 75 70 70 6f 72 74 20 66 6f 72 20 ing support for
f5610 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66 20 74 68 65 many. ** of the
f5620 20 64 65 62 75 67 67 69 6e 67 20 61 6e 64 20 74 debugging and t
f5630 65 73 74 69 6e 67 20 75 74 69 6c 69 74 69 65 73 esting utilities
f5640 2c 20 62 75 74 20 69 74 20 73 68 6f 75 6c 64 20 , but it should
f5650 61 74 0a 20 20 2a 2a 20 6c 65 61 73 74 20 63 6f at. ** least co
f5660 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e 2e 0a 20 mpile and run..
f5670 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */.SQLITE_PRIVA
f5680 54 45 20 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 TE sqlite_uint
f5690 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 64 sqlite3Hwtime
f56a0 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 28 (void){ return (
f56b0 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 (sqlite_uint64)0
f56c0 29 3b 20 7d 0a 0a 23 65 6e 64 69 66 0a 0a 23 65 ); }..#endif..#e
f56d0 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 ndif /* !defined
f56e0 28 5f 48 57 54 49 4d 45 5f 48 5f 29 20 2a 2f 0a (_HWTIME_H_) */.
f56f0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
f5700 20 45 6e 64 20 6f 66 20 68 77 74 69 6d 65 2e 68 End of hwtime.h
f5710 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
f5720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f5730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
f5740 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
f5750 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 Continuing wher
f5760 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e e we left off in
f5770 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a os_common.h ***
f5780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
f5790 0a 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f ..static sqlite_
f57a0 75 69 6e 74 36 34 20 67 5f 73 74 61 72 74 3b 0a uint64 g_start;.
f57b0 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f 75 69 static sqlite_ui
f57c0 6e 74 36 34 20 67 5f 65 6c 61 70 73 65 64 3b 0a nt64 g_elapsed;.
f57d0 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 #define TIMER_ST
f57e0 41 52 54 20 20 20 20 20 20 20 67 5f 73 74 61 72 ART g_star
f57f0 74 3d 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 t=sqlite3Hwtime(
f5800 29 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f ).#define TIMER_
f5810 45 4e 44 20 20 20 20 20 20 20 20 20 67 5f 65 6c END g_el
f5820 61 70 73 65 64 3d 73 71 6c 69 74 65 33 48 77 74 apsed=sqlite3Hwt
f5830 69 6d 65 28 29 2d 67 5f 73 74 61 72 74 0a 23 64 ime()-g_start.#d
f5840 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 efine TIMER_ELAP
f5850 53 45 44 20 20 20 20 20 67 5f 65 6c 61 70 73 65 SED g_elapse
f5860 64 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 d.#else.#define
f5870 54 49 4d 45 52 5f 53 54 41 52 54 0a 23 64 65 66 TIMER_START.#def
f5880 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 0a 23 64 ine TIMER_END.#d
f5890 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 efine TIMER_ELAP
f58a0 53 45 44 20 20 20 20 20 28 28 73 71 6c 69 74 65 SED ((sqlite
f58b0 5f 75 69 6e 74 36 34 29 30 29 0a 23 65 6e 64 69 _uint64)0).#endi
f58c0 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 63 f../*.** If we c
f58d0 6f 6d 70 69 6c 65 20 77 69 74 68 20 74 68 65 20 ompile with the
f58e0 53 51 4c 49 54 45 5f 54 45 53 54 20 6d 61 63 72 SQLITE_TEST macr
f58f0 6f 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 o set, then the
f5900 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 0a following block.
f5910 2a 2a 20 6f 66 20 63 6f 64 65 20 77 69 6c 6c 20 ** of code will
f5920 67 69 76 65 20 75 73 20 74 68 65 20 61 62 69 6c give us the abil
f5930 69 74 79 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 ity to simulate
f5940 61 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 a disk I/O error
f5950 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73 20 75 73 . This.** is us
f5960 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 74 ed for testing t
f5970 68 65 20 49 2f 4f 20 72 65 63 6f 76 65 72 79 20 he I/O recovery
f5980 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 69 66 64 65 66 logic..*/.#ifdef
f5990 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
f59a0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
f59b0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 te3_io_error_hit
f59c0 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
f59d0 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 /* Total number
f59e0 20 6f 66 20 49 2f 4f 20 45 72 72 6f 72 73 20 2a of I/O Errors *
f59f0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
f5a00 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
f5a10 72 5f 68 61 72 64 68 69 74 20 3d 20 30 3b 20 20 r_hardhit = 0;
f5a20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
f5a30 6f 66 20 6e 6f 6e 2d 62 65 6e 69 67 6e 20 65 72 of non-benign er
f5a40 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 rors */.SQLITE_A
f5a50 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 PI int sqlite3_i
f5a60 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 o_error_pending
f5a70 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 = 0; /* C
f5a80 6f 75 6e 74 20 64 6f 77 6e 20 74 6f 20 66 69 72 ount down to fir
f5a90 73 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a st I/O error */.
f5aa0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
f5ab0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
f5ac0 70 65 72 73 69 73 74 20 3d 20 30 3b 20 20 20 20 persist = 0;
f5ad0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 49 /* True if I
f5ae0 2f 4f 20 65 72 72 6f 72 73 20 70 65 72 73 69 73 /O errors persis
f5af0 74 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 t */.SQLITE_API
f5b00 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 int sqlite3_io_e
f5b10 72 72 6f 72 5f 62 65 6e 69 67 6e 20 3d 20 30 3b rror_benign = 0;
f5b20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
f5b30 20 69 66 20 65 72 72 6f 72 73 20 61 72 65 20 62 if errors are b
f5b40 65 6e 69 67 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f enign */.SQLITE_
f5b50 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
f5b60 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 diskfull_pending
f5b70 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 = 0;.SQLITE_API
f5b80 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73 int sqlite3_dis
f5b90 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23 64 65 66 69 kfull = 0;.#defi
f5ba0 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 ne SimulateIOErr
f5bb0 6f 72 42 65 6e 69 67 6e 28 58 29 20 73 71 6c 69 orBenign(X) sqli
f5bc0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e te3_io_error_ben
f5bd0 69 67 6e 3d 28 58 29 0a 23 64 65 66 69 6e 65 20 ign=(X).#define
f5be0 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
f5bf0 43 4f 44 45 29 20 20 5c 0a 20 20 69 66 28 20 28 CODE) \. if( (
f5c00 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
f5c10 5f 70 65 72 73 69 73 74 20 26 26 20 73 71 6c 69 _persist && sqli
f5c20 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 te3_io_error_hit
f5c30 29 20 5c 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 ) \. || sq
f5c40 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 lite3_io_error_p
f5c50 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20 31 20 29 20 ending-- == 1 )
f5c60 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
f5c70 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 { local_ioerr()
f5c80 3b 20 43 4f 44 45 3b 20 7d 0a 73 74 61 74 69 63 ; CODE; }.static
f5c90 20 76 6f 69 64 20 6c 6f 63 61 6c 5f 69 6f 65 72 void local_ioer
f5ca0 72 28 29 7b 0a 20 20 49 4f 54 52 41 43 45 28 28 r(){. IOTRACE((
f5cb0 22 49 4f 45 52 52 5c 6e 22 29 29 3b 0a 20 20 73 "IOERR\n"));. s
f5cc0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
f5cd0 68 69 74 2b 2b 3b 0a 20 20 69 66 28 20 21 73 71 hit++;. if( !sq
f5ce0 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 lite3_io_error_b
f5cf0 65 6e 69 67 6e 20 29 20 73 71 6c 69 74 65 33 5f enign ) sqlite3_
f5d00 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74 io_error_hardhit
f5d10 2b 2b 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 53 69 ++;.}.#define Si
f5d20 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 mulateDiskfullEr
f5d30 72 6f 72 28 43 4f 44 45 29 20 5c 0a 20 20 20 69 ror(CODE) \. i
f5d40 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 f( sqlite3_diskf
f5d50 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 29 7b 20 5c ull_pending ){ \
f5d60 0a 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 . if( sqlite
f5d70 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 3_diskfull_pendi
f5d80 6e 67 20 3d 3d 20 31 20 29 7b 20 5c 0a 20 20 20 ng == 1 ){ \.
f5d90 20 20 20 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 local_ioerr(
f5da0 29 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 ); \. sqli
f5db0 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 31 te3_diskfull = 1
f5dc0 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 ; \. sqlit
f5dd0 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 e3_io_error_hit
f5de0 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 43 4f = 1; \. CO
f5df0 44 45 3b 20 5c 0a 20 20 20 20 20 7d 65 6c 73 65 DE; \. }else
f5e00 7b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 { \. sqlit
f5e10 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 e3_diskfull_pend
f5e20 69 6e 67 2d 2d 3b 20 5c 0a 20 20 20 20 20 7d 20 ing--; \. }
f5e30 5c 0a 20 20 20 7d 0a 23 65 6c 73 65 0a 23 64 65 \. }.#else.#de
f5e40 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 fine SimulateIOE
f5e50 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 0a 23 64 rrorBenign(X).#d
f5e60 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f efine SimulateIO
f5e70 45 72 72 6f 72 28 41 29 0a 23 64 65 66 69 6e 65 Error(A).#define
f5e80 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c SimulateDiskful
f5e90 6c 45 72 72 6f 72 28 41 29 0a 23 65 6e 64 69 66 lError(A).#endif
f5ea0 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 65 73 ../*.** When tes
f5eb0 74 69 6e 67 2c 20 6b 65 65 70 20 61 20 63 6f 75 ting, keep a cou
f5ec0 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 nt of the number
f5ed0 20 6f 66 20 6f 70 65 6e 20 66 69 6c 65 73 2e 0a of open files..
f5ee0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
f5ef0 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 _TEST.SQLITE_API
f5f00 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 int sqlite3_ope
f5f10 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 20 3d 20 30 n_file_count = 0
f5f20 3b 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f ;.#define OpenCo
f5f30 75 6e 74 65 72 28 58 29 20 20 73 71 6c 69 74 65 unter(X) sqlite
f5f40 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 3_open_file_coun
f5f50 74 2b 3d 28 58 29 0a 23 65 6c 73 65 0a 23 64 65 t+=(X).#else.#de
f5f60 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 fine OpenCounter
f5f70 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 (X).#endif..#end
f5f80 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f if /* !defined(_
f5f90 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 29 20 2a 2f OS_COMMON_H_) */
f5fa0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
f5fb0 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 63 6f 6d 6d * End of os_comm
f5fc0 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a on.h ***********
f5fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f5fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f5ff0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
f6000 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 * Continuing whe
f6010 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 re we left off i
f6020 6e 20 6f 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a n os_win.c *****
f6030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f6040 2f 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 6d 69 /../*.** Some mi
f6050 63 72 6f 73 6f 66 74 20 63 6f 6d 70 69 6c 65 72 crosoft compiler
f6060 73 20 6c 61 63 6b 20 74 68 69 73 20 64 65 66 69 s lack this defi
f6070 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 nition..*/.#ifnd
f6080 65 66 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f ef INVALID_FILE_
f6090 41 54 54 52 49 42 55 54 45 53 0a 23 20 64 65 66 ATTRIBUTES.# def
f60a0 69 6e 65 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 ine INVALID_FILE
f60b0 5f 41 54 54 52 49 42 55 54 45 53 20 28 28 44 57 _ATTRIBUTES ((DW
f60c0 4f 52 44 29 2d 31 29 20 0a 23 65 6e 64 69 66 0a ORD)-1) .#endif.
f60d0 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 ./*.** Determine
f60e0 20 69 66 20 77 65 20 61 72 65 20 64 65 61 6c 69 if we are deali
f60f0 6e 67 20 77 69 74 68 20 57 69 6e 64 6f 77 73 43 ng with WindowsC
f6100 45 20 2d 20 77 68 69 63 68 20 68 61 73 20 61 20 E - which has a
f6110 6d 75 63 68 0a 2a 2a 20 72 65 64 75 63 65 64 20 much.** reduced
f6120 41 50 49 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 API..*/.#if SQLI
f6130 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 23 20 64 65 TE_OS_WINCE.# de
f6140 66 69 6e 65 20 41 72 65 46 69 6c 65 41 70 69 73 fine AreFileApis
f6150 41 4e 53 49 28 29 20 31 0a 23 20 64 65 66 69 6e ANSI() 1.# defin
f6160 65 20 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 57 e FormatMessageW
f6170 28 61 2c 62 2c 63 2c 64 2c 65 2c 66 2c 67 29 20 (a,b,c,d,e,f,g)
f6180 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 0.#endif../*.**
f6190 57 69 6e 43 45 20 6c 61 63 6b 73 20 6e 61 74 69 WinCE lacks nati
f61a0 76 65 20 73 75 70 70 6f 72 74 20 66 6f 72 20 66 ve support for f
f61b0 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 73 6f 20 77 ile locking so w
f61c0 65 20 68 61 76 65 20 74 6f 20 66 61 6b 65 20 69 e have to fake i
f61d0 74 0a 2a 2a 20 77 69 74 68 20 73 6f 6d 65 20 63 t.** with some c
f61e0 6f 64 65 20 6f 66 20 6f 75 72 20 6f 77 6e 2e 0a ode of our own..
f61f0 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 */.#if SQLITE_OS
f6200 5f 57 49 4e 43 45 0a 74 79 70 65 64 65 66 20 73 _WINCE.typedef s
f6210 74 72 75 63 74 20 77 69 6e 63 65 4c 6f 63 6b 20 truct winceLock
f6220 7b 0a 20 20 69 6e 74 20 6e 52 65 61 64 65 72 73 {. int nReaders
f6230 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 ; /* Numbe
f6240 72 20 6f 66 20 72 65 61 64 65 72 20 6c 6f 63 6b r of reader lock
f6250 73 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a 20 20 s obtained */.
f6260 42 4f 4f 4c 20 62 50 65 6e 64 69 6e 67 3b 20 20 BOOL bPending;
f6270 20 20 20 20 2f 2a 20 49 6e 64 69 63 61 74 65 73 /* Indicates
f6280 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 a pending lock
f6290 68 61 73 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 has been obtaine
f62a0 64 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62 52 65 73 d */. BOOL bRes
f62b0 65 72 76 65 64 3b 20 20 20 20 20 2f 2a 20 49 6e erved; /* In
f62c0 64 69 63 61 74 65 73 20 61 20 72 65 73 65 72 76 dicates a reserv
f62d0 65 64 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e ed lock has been
f62e0 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a 20 20 42 obtained */. B
f62f0 4f 4f 4c 20 62 45 78 63 6c 75 73 69 76 65 3b 20 OOL bExclusive;
f6300 20 20 20 2f 2a 20 49 6e 64 69 63 61 74 65 73 20 /* Indicates
f6310 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 an exclusive loc
f6320 6b 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61 69 k has been obtai
f6330 6e 65 64 20 2a 2f 0a 7d 20 77 69 6e 63 65 4c 6f ned */.} winceLo
f6340 63 6b 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ck;.#endif../*.*
f6350 2a 20 54 68 65 20 77 69 6e 46 69 6c 65 20 73 74 * The winFile st
f6360 72 75 63 74 75 72 65 20 69 73 20 61 20 73 75 62 ructure is a sub
f6370 63 6c 61 73 73 20 6f 66 20 73 71 6c 69 74 65 33 class of sqlite3
f6380 5f 66 69 6c 65 2a 20 73 70 65 63 69 66 69 63 20 _file* specific
f6390 74 6f 20 74 68 65 20 77 69 6e 33 32 0a 2a 2a 20 to the win32.**
f63a0 70 6f 72 74 61 62 69 6c 69 74 79 20 6c 61 79 65 portability laye
f63b0 72 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 r..*/.typedef st
f63c0 72 75 63 74 20 77 69 6e 46 69 6c 65 20 77 69 6e ruct winFile win
f63d0 46 69 6c 65 3b 0a 73 74 72 75 63 74 20 77 69 6e File;.struct win
f63e0 46 69 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20 73 File {. const s
f63f0 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
f6400 73 20 2a 70 4d 65 74 68 6f 64 3b 2f 2a 20 4d 75 s *pMethod;/* Mu
f6410 73 74 20 62 65 20 66 69 72 73 74 20 2a 2f 0a 20 st be first */.
f6420 20 48 41 4e 44 4c 45 20 68 3b 20 20 20 20 20 20 HANDLE h;
f6430 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 /* Hand
f6440 6c 65 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67 le for accessing
f6450 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 the file */. u
f6460 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6c 6f 63 nsigned char loc
f6470 6b 74 79 70 65 3b 20 2f 2a 20 54 79 70 65 20 6f ktype; /* Type o
f6480 66 20 6c 6f 63 6b 20 63 75 72 72 65 6e 74 6c 79 f lock currently
f6490 20 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 66 69 held on this fi
f64a0 6c 65 20 2a 2f 0a 20 20 73 68 6f 72 74 20 73 68 le */. short sh
f64b0 61 72 65 64 4c 6f 63 6b 42 79 74 65 3b 20 20 20 aredLockByte;
f64c0 2f 2a 20 52 61 6e 64 6f 6d 6c 79 20 63 68 6f 73 /* Randomly chos
f64d0 65 6e 20 62 79 74 65 20 75 73 65 64 20 61 73 20 en byte used as
f64e0 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f a shared lock */
f64f0 0a 20 20 44 57 4f 52 44 20 6c 61 73 74 45 72 72 . DWORD lastErr
f6500 6e 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 no; /* Th
f6510 65 20 57 69 6e 64 6f 77 73 20 65 72 72 6e 6f 20 e Windows errno
f6520 66 72 6f 6d 20 74 68 65 20 6c 61 73 74 20 49 2f from the last I/
f6530 4f 20 65 72 72 6f 72 20 2a 2f 0a 20 20 44 57 4f O error */. DWO
f6540 52 44 20 73 65 63 74 6f 72 53 69 7a 65 3b 20 20 RD sectorSize;
f6550 20 20 20 20 20 2f 2a 20 53 65 63 74 6f 72 20 73 /* Sector s
f6560 69 7a 65 20 6f 66 20 74 68 65 20 64 65 76 69 63 ize of the devic
f6570 65 20 66 69 6c 65 20 69 73 20 6f 6e 20 2a 2f 0a e file is on */.
f6580 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 #if SQLITE_OS_WI
f6590 4e 43 45 0a 20 20 57 43 48 41 52 20 2a 7a 44 65 NCE. WCHAR *zDe
f65a0 6c 65 74 65 4f 6e 43 6c 6f 73 65 3b 20 20 2f 2a leteOnClose; /*
f65b0 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f Name of file to
f65c0 20 64 65 6c 65 74 65 20 77 68 65 6e 20 63 6c 6f delete when clo
f65d0 73 69 6e 67 20 2a 2f 0a 20 20 48 41 4e 44 4c 45 sing */. HANDLE
f65e0 20 68 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 hMutex;
f65f0 20 20 2f 2a 20 4d 75 74 65 78 20 75 73 65 64 20 /* Mutex used
f6600 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 to control acces
f6610 73 20 74 6f 20 73 68 61 72 65 64 20 6c 6f 63 6b s to shared lock
f6620 20 2a 2f 20 20 0a 20 20 48 41 4e 44 4c 45 20 68 */ . HANDLE h
f6630 53 68 61 72 65 64 3b 20 20 20 20 20 20 20 20 20 Shared;
f6640 2f 2a 20 53 68 61 72 65 64 20 6d 65 6d 6f 72 79 /* Shared memory
f6650 20 73 65 67 6d 65 6e 74 20 75 73 65 64 20 66 6f segment used fo
f6660 72 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 77 r locking */. w
f6670 69 6e 63 65 4c 6f 63 6b 20 6c 6f 63 61 6c 3b 20 inceLock local;
f6680 20 20 20 20 20 20 20 2f 2a 20 4c 6f 63 6b 73 20 /* Locks
f6690 6f 62 74 61 69 6e 65 64 20 62 79 20 74 68 69 73 obtained by this
f66a0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 77 69 6e instance of win
f66b0 46 69 6c 65 20 2a 2f 0a 20 20 77 69 6e 63 65 4c File */. winceL
f66c0 6f 63 6b 20 2a 73 68 61 72 65 64 3b 20 20 20 20 ock *shared;
f66d0 20 20 2f 2a 20 47 6c 6f 62 61 6c 20 73 68 61 72 /* Global shar
f66e0 65 64 20 6c 6f 63 6b 20 6d 65 6d 6f 72 79 20 66 ed lock memory f
f66f0 6f 72 20 74 68 65 20 66 69 6c 65 20 20 2a 2f 0a or the file */.
f6700 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a #endif.};../*.**
f6710 20 46 6f 72 77 61 72 64 20 70 72 6f 74 6f 74 79 Forward prototy
f6720 70 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 pes..*/.static i
f6730 6e 74 20 67 65 74 53 65 63 74 6f 72 53 69 7a 65 nt getSectorSize
f6740 28 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 (. sqlite3_vf
f6750 73 20 2a 70 56 66 73 2c 0a 20 20 20 20 63 6f 6e s *pVfs,. con
f6760 73 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 st char *zRelati
f6770 76 65 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 ve /* UTF-8
f6780 66 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a 29 3b 0a file name */.);.
f6790 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f ./*.** The follo
f67a0 77 69 6e 67 20 76 61 72 69 61 62 6c 65 20 69 73 wing variable is
f67b0 20 28 6e 6f 72 6d 61 6c 6c 79 29 20 73 65 74 20 (normally) set
f67c0 6f 6e 63 65 20 61 6e 64 20 6e 65 76 65 72 20 63 once and never c
f67d0 68 61 6e 67 65 73 0a 2a 2a 20 74 68 65 72 65 61 hanges.** therea
f67e0 66 74 65 72 2e 20 20 49 74 20 72 65 63 6f 72 64 fter. It record
f67f0 73 20 77 68 65 74 68 65 72 20 74 68 65 20 6f 70 s whether the op
f6800 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 erating system i
f6810 73 20 57 69 6e 39 35 0a 2a 2a 20 6f 72 20 57 69 s Win95.** or Wi
f6820 6e 4e 54 2e 0a 2a 2a 0a 2a 2a 20 30 3a 20 20 20 nNT..**.** 0:
f6830 4f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d Operating system
f6840 20 75 6e 6b 6e 6f 77 6e 2e 0a 2a 2a 20 31 3a 20 unknown..** 1:
f6850 20 20 4f 70 65 72 61 74 69 6e 67 20 73 79 73 74 Operating syst
f6860 65 6d 20 69 73 20 57 69 6e 39 35 2e 0a 2a 2a 20 em is Win95..**
f6870 32 3a 20 20 20 4f 70 65 72 61 74 69 6e 67 20 73 2: Operating s
f6880 79 73 74 65 6d 20 69 73 20 57 69 6e 4e 54 2e 0a ystem is WinNT..
f6890 2a 2a 0a 2a 2a 20 49 6e 20 6f 72 64 65 72 20 74 **.** In order t
f68a0 6f 20 66 61 63 69 6c 69 74 61 74 65 20 74 65 73 o facilitate tes
f68b0 74 69 6e 67 20 6f 6e 20 61 20 57 69 6e 4e 54 20 ting on a WinNT
f68c0 73 79 73 74 65 6d 2c 20 74 68 65 20 74 65 73 74 system, the test
f68d0 20 66 69 78 74 75 72 65 0a 2a 2a 20 63 61 6e 20 fixture.** can
f68e0 6d 61 6e 75 61 6c 6c 79 20 73 65 74 20 74 68 69 manually set thi
f68f0 73 20 76 61 6c 75 65 20 74 6f 20 31 20 74 6f 20 s value to 1 to
f6900 65 6d 75 6c 61 74 65 20 57 69 6e 39 38 20 62 65 emulate Win98 be
f6910 68 61 76 69 6f 72 2e 0a 2a 2f 0a 23 69 66 64 65 havior..*/.#ifde
f6920 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 f SQLITE_TEST.SQ
f6930 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
f6940 69 74 65 33 5f 6f 73 5f 74 79 70 65 20 3d 20 30 ite3_os_type = 0
f6950 3b 0a 23 65 6c 73 65 0a 73 74 61 74 69 63 20 69 ;.#else.static i
f6960 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 nt sqlite3_os_ty
f6970 70 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a pe = 0;.#endif..
f6980 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 /*.** Return tru
f6990 65 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 e (non-zero) if
f69a0 77 65 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 75 we are running u
f69b0 6e 64 65 72 20 57 69 6e 4e 54 2c 20 57 69 6e 32 nder WinNT, Win2
f69c0 4b 2c 20 57 69 6e 58 50 2c 0a 2a 2a 20 6f 72 20 K, WinXP,.** or
f69d0 57 69 6e 43 45 2e 20 20 52 65 74 75 72 6e 20 66 WinCE. Return f
f69e0 61 6c 73 65 20 28 7a 65 72 6f 29 20 66 6f 72 20 alse (zero) for
f69f0 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 6f 72 Win95, Win98, or
f6a00 20 57 69 6e 4d 45 2e 0a 2a 2a 0a 2a 2a 20 48 65 WinME..**.** He
f6a10 72 65 20 69 73 20 61 6e 20 69 6e 74 65 72 65 73 re is an interes
f6a20 74 69 6e 67 20 6f 62 73 65 72 76 61 74 69 6f 6e ting observation
f6a30 3a 20 20 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c : Win95, Win98,
f6a40 20 61 6e 64 20 57 69 6e 4d 45 20 6c 61 63 6b 0a and WinME lack.
f6a50 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 ** the LockFileE
f6a60 78 28 29 20 41 50 49 2e 20 20 42 75 74 20 77 65 x() API. But we
f6a70 20 63 61 6e 20 73 74 69 6c 6c 20 73 74 61 74 69 can still stati
f6a80 63 61 6c 6c 79 20 6c 69 6e 6b 20 61 67 61 69 6e cally link again
f6a90 73 74 20 74 68 61 74 0a 2a 2a 20 41 50 49 20 61 st that.** API a
f6aa0 73 20 6c 6f 6e 67 20 61 73 20 77 65 20 64 6f 6e s long as we don
f6ab0 27 74 20 63 61 6c 6c 20 69 74 20 77 68 65 6e 20 't call it when
f6ac0 72 75 6e 6e 69 6e 67 20 57 69 6e 39 35 2f 39 38 running Win95/98
f6ad0 2f 4d 45 2e 20 20 41 20 63 61 6c 6c 20 74 6f 0a /ME. A call to.
f6ae0 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 ** this routine
f6af0 69 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 is used to deter
f6b00 6d 69 6e 65 20 69 66 20 74 68 65 20 68 6f 73 74 mine if the host
f6b10 20 69 73 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20 is Win95/98/ME
f6b20 6f 72 0a 2a 2a 20 57 69 6e 4e 54 2f 32 4b 2f 58 or.** WinNT/2K/X
f6b30 50 20 73 6f 20 74 68 61 74 20 77 65 20 77 69 6c P so that we wil
f6b40 6c 20 6b 6e 6f 77 20 77 68 65 74 68 65 72 20 6f l know whether o
f6b50 72 20 6e 6f 74 20 77 65 20 63 61 6e 20 73 61 66 r not we can saf
f6b60 65 6c 79 20 63 61 6c 6c 0a 2a 2a 20 74 68 65 20 ely call.** the
f6b70 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 LockFileEx() API
f6b80 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f ..*/.#if SQLITE_
f6b90 4f 53 5f 57 49 4e 43 45 0a 23 20 64 65 66 69 6e OS_WINCE.# defin
f6ba0 65 20 69 73 4e 54 28 29 20 20 28 31 29 0a 23 65 e isNT() (1).#e
f6bb0 6c 73 65 0a 20 20 73 74 61 74 69 63 20 69 6e 74 lse. static int
f6bc0 20 69 73 4e 54 28 76 6f 69 64 29 7b 0a 20 20 20 isNT(void){.
f6bd0 20 69 66 28 20 73 71 6c 69 74 65 33 5f 6f 73 5f if( sqlite3_os_
f6be0 74 79 70 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 type==0 ){.
f6bf0 20 4f 53 56 45 52 53 49 4f 4e 49 4e 46 4f 20 73 OSVERSIONINFO s
f6c00 49 6e 66 6f 3b 0a 20 20 20 20 20 20 73 49 6e 66 Info;. sInf
f6c10 6f 2e 64 77 4f 53 56 65 72 73 69 6f 6e 49 6e 66 o.dwOSVersionInf
f6c20 6f 53 69 7a 65 20 3d 20 73 69 7a 65 6f 66 28 73 oSize = sizeof(s
f6c30 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 47 65 74 Info);. Get
f6c40 56 65 72 73 69 6f 6e 45 78 28 26 73 49 6e 66 6f VersionEx(&sInfo
f6c50 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
f6c60 5f 6f 73 5f 74 79 70 65 20 3d 20 73 49 6e 66 6f _os_type = sInfo
f6c70 2e 64 77 50 6c 61 74 66 6f 72 6d 49 64 3d 3d 56 .dwPlatformId==V
f6c80 45 52 5f 50 4c 41 54 46 4f 52 4d 5f 57 49 4e 33 ER_PLATFORM_WIN3
f6c90 32 5f 4e 54 20 3f 20 32 20 3a 20 31 3b 0a 20 20 2_NT ? 2 : 1;.
f6ca0 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 73 }. return s
f6cb0 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 3d 3d qlite3_os_type==
f6cc0 32 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 2;. }.#endif /*
f6cd0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
f6ce0 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 */../*.** Conve
f6cf0 72 74 20 61 20 55 54 46 2d 38 20 73 74 72 69 6e rt a UTF-8 strin
f6d00 67 20 74 6f 20 6d 69 63 72 6f 73 6f 66 74 20 75 g to microsoft u
f6d10 6e 69 63 6f 64 65 20 28 55 54 46 2d 31 36 3f 29 nicode (UTF-16?)
f6d20 2e 20 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 20 74 . .**.** Space t
f6d30 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 o hold the retur
f6d40 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 6f 62 ned string is ob
f6d50 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c tained from mall
f6d60 6f 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 57 43 oc..*/.static WC
f6d70 48 41 52 20 2a 75 74 66 38 54 6f 55 6e 69 63 6f HAR *utf8ToUnico
f6d80 64 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a de(const char *z
f6d90 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 Filename){. int
f6da0 20 6e 43 68 61 72 3b 0a 20 20 57 43 48 41 52 20 nChar;. WCHAR
f6db0 2a 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 3b 0a *zWideFilename;.
f6dc0 0a 20 20 6e 43 68 61 72 20 3d 20 4d 75 6c 74 69 . nChar = Multi
f6dd0 42 79 74 65 54 6f 57 69 64 65 43 68 61 72 28 43 ByteToWideChar(C
f6de0 50 5f 55 54 46 38 2c 20 30 2c 20 7a 46 69 6c 65 P_UTF8, 0, zFile
f6df0 6e 61 6d 65 2c 20 2d 31 2c 20 4e 55 4c 4c 2c 20 name, -1, NULL,
f6e00 30 29 3b 0a 20 20 7a 57 69 64 65 46 69 6c 65 6e 0);. zWideFilen
f6e10 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 43 ame = malloc( nC
f6e20 68 61 72 2a 73 69 7a 65 6f 66 28 7a 57 69 64 65 har*sizeof(zWide
f6e30 46 69 6c 65 6e 61 6d 65 5b 30 5d 29 20 29 3b 0a Filename[0]) );.
f6e40 20 20 69 66 28 20 7a 57 69 64 65 46 69 6c 65 6e if( zWideFilen
f6e50 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 ame==0 ){. re
f6e60 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 43 turn 0;. }. nC
f6e70 68 61 72 20 3d 20 4d 75 6c 74 69 42 79 74 65 54 har = MultiByteT
f6e80 6f 57 69 64 65 43 68 61 72 28 43 50 5f 55 54 46 oWideChar(CP_UTF
f6e90 38 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 8, 0, zFilename,
f6ea0 20 2d 31 2c 20 7a 57 69 64 65 46 69 6c 65 6e 61 -1, zWideFilena
f6eb0 6d 65 2c 20 6e 43 68 61 72 29 3b 0a 20 20 69 66 me, nChar);. if
f6ec0 28 20 6e 43 68 61 72 3d 3d 30 20 29 7b 0a 20 20 ( nChar==0 ){.
f6ed0 20 20 66 72 65 65 28 7a 57 69 64 65 46 69 6c 65 free(zWideFile
f6ee0 6e 61 6d 65 29 3b 0a 20 20 20 20 7a 57 69 64 65 name);. zWide
f6ef0 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 Filename = 0;.
f6f00 7d 0a 20 20 72 65 74 75 72 6e 20 7a 57 69 64 65 }. return zWide
f6f10 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a Filename;.}../*.
f6f20 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 69 63 72 6f ** Convert micro
f6f30 73 6f 66 74 20 75 6e 69 63 6f 64 65 20 74 6f 20 soft unicode to
f6f40 55 54 46 2d 38 2e 20 20 53 70 61 63 65 20 74 6f UTF-8. Space to
f6f50 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e hold the return
f6f60 65 64 20 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20 ed string is.**
f6f70 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 obtained from ma
f6f80 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 lloc()..*/.stati
f6f90 63 20 63 68 61 72 20 2a 75 6e 69 63 6f 64 65 54 c char *unicodeT
f6fa0 6f 55 74 66 38 28 63 6f 6e 73 74 20 57 43 48 41 oUtf8(const WCHA
f6fb0 52 20 2a 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 R *zWideFilename
f6fc0 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a ){. int nByte;.
f6fd0 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d char *zFilenam
f6fe0 65 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 e;.. nByte = Wi
f6ff0 64 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 deCharToMultiByt
f7000 65 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a 57 e(CP_UTF8, 0, zW
f7010 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c ideFilename, -1,
f7020 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 0, 0, 0, 0);.
f7030 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c zFilename = mall
f7040 6f 63 28 20 6e 42 79 74 65 20 29 3b 0a 20 20 69 oc( nByte );. i
f7050 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 f( zFilename==0
f7060 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
f7070 0a 20 20 7d 0a 20 20 6e 42 79 74 65 20 3d 20 57 . }. nByte = W
f7080 69 64 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 ideCharToMultiBy
f7090 74 65 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a te(CP_UTF8, 0, z
f70a0 57 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 WideFilename, -1
f70b0 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 , zFilename, nBy
f70c0 74 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 te,.
f70d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f70e0 20 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e 0, 0);. if( n
f70f0 42 79 74 65 20 3d 3d 20 30 20 29 7b 0a 20 20 20 Byte == 0 ){.
f7100 20 66 72 65 65 28 7a 46 69 6c 65 6e 61 6d 65 29 free(zFilename)
f7110 3b 0a 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 ;. zFilename
f7120 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 = 0;. }. retur
f7130 6e 20 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a n zFilename;.}..
f7140 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 6e /*.** Convert an
f7150 20 61 6e 73 69 20 73 74 72 69 6e 67 20 74 6f 20 ansi string to
f7160 6d 69 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 microsoft unicod
f7170 65 2c 20 62 61 73 65 64 20 6f 6e 20 74 68 65 0a e, based on the.
f7180 2a 2a 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 ** current codep
f7190 61 67 65 20 73 65 74 74 69 6e 67 73 20 66 6f 72 age settings for
f71a0 20 66 69 6c 65 20 61 70 69 73 2e 0a 2a 2a 20 0a file apis..** .
f71b0 2a 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 ** Space to hold
f71c0 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 the returned st
f71d0 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64 ring is obtained
f71e0 0a 2a 2a 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e .** from malloc.
f71f0 0a 2a 2f 0a 73 74 61 74 69 63 20 57 43 48 41 52 .*/.static WCHAR
f7200 20 2a 6d 62 63 73 54 6f 55 6e 69 63 6f 64 65 28 *mbcsToUnicode(
f7210 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c const char *zFil
f7220 65 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 ename){. int nB
f7230 79 74 65 3b 0a 20 20 57 43 48 41 52 20 2a 7a 4d yte;. WCHAR *zM
f7240 62 63 73 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 69 bcsFilename;. i
f7250 6e 74 20 63 6f 64 65 70 61 67 65 20 3d 20 41 72 nt codepage = Ar
f7260 65 46 69 6c 65 41 70 69 73 41 4e 53 49 28 29 20 eFileApisANSI()
f7270 3f 20 43 50 5f 41 43 50 20 3a 20 43 50 5f 4f 45 ? CP_ACP : CP_OE
f7280 4d 43 50 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 MCP;.. nByte =
f7290 4d 75 6c 74 69 42 79 74 65 54 6f 57 69 64 65 43 MultiByteToWideC
f72a0 68 61 72 28 63 6f 64 65 70 61 67 65 2c 20 30 2c har(codepage, 0,
f72b0 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 zFilename, -1,
f72c0 4e 55 4c 4c 2c 30 29 2a 73 69 7a 65 6f 66 28 57 NULL,0)*sizeof(W
f72d0 43 48 41 52 29 3b 0a 20 20 7a 4d 62 63 73 46 69 CHAR);. zMbcsFi
f72e0 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 lename = malloc(
f72f0 20 6e 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a 4d nByte*sizeof(zM
f7300 62 63 73 46 69 6c 65 6e 61 6d 65 5b 30 5d 29 20 bcsFilename[0])
f7310 29 3b 0a 20 20 69 66 28 20 7a 4d 62 63 73 46 69 );. if( zMbcsFi
f7320 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 lename==0 ){.
f7330 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
f7340 20 6e 42 79 74 65 20 3d 20 4d 75 6c 74 69 42 79 nByte = MultiBy
f7350 74 65 54 6f 57 69 64 65 43 68 61 72 28 63 6f 64 teToWideChar(cod
f7360 65 70 61 67 65 2c 20 30 2c 20 7a 46 69 6c 65 6e epage, 0, zFilen
f7370 61 6d 65 2c 20 2d 31 2c 20 7a 4d 62 63 73 46 69 ame, -1, zMbcsFi
f7380 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 65 29 3b 0a lename, nByte);.
f7390 20 20 69 66 28 20 6e 42 79 74 65 3d 3d 30 20 29 if( nByte==0 )
f73a0 7b 0a 20 20 20 20 66 72 65 65 28 7a 4d 62 63 73 {. free(zMbcs
f73b0 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a Filename);. z
f73c0 4d 62 63 73 46 69 6c 65 6e 61 6d 65 20 3d 20 30 MbcsFilename = 0
f73d0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a ;. }. return z
f73e0 4d 62 63 73 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a MbcsFilename;.}.
f73f0 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 6d ./*.** Convert m
f7400 69 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65 icrosoft unicode
f7410 20 74 6f 20 6d 75 6c 74 69 62 79 74 65 20 63 68 to multibyte ch
f7420 61 72 61 63 74 65 72 20 73 74 72 69 6e 67 2c 20 aracter string,
f7430 62 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 based on the.**
f7440 75 73 65 72 27 73 20 41 6e 73 69 20 63 6f 64 65 user's Ansi code
f7450 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 53 70 61 63 page..**.** Spac
f7460 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 e to hold the re
f7470 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 turned string is
f7480 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a obtained from.*
f7490 2a 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 * malloc()..*/.s
f74a0 74 61 74 69 63 20 63 68 61 72 20 2a 75 6e 69 63 tatic char *unic
f74b0 6f 64 65 54 6f 4d 62 63 73 28 63 6f 6e 73 74 20 odeToMbcs(const
f74c0 57 43 48 41 52 20 2a 7a 57 69 64 65 46 69 6c 65 WCHAR *zWideFile
f74d0 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79 name){. int nBy
f74e0 74 65 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c te;. char *zFil
f74f0 65 6e 61 6d 65 3b 0a 20 20 69 6e 74 20 63 6f 64 ename;. int cod
f7500 65 70 61 67 65 20 3d 20 41 72 65 46 69 6c 65 41 epage = AreFileA
f7510 70 69 73 41 4e 53 49 28 29 20 3f 20 43 50 5f 41 pisANSI() ? CP_A
f7520 43 50 20 3a 20 43 50 5f 4f 45 4d 43 50 3b 0a 0a CP : CP_OEMCP;..
f7530 20 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68 nByte = WideCh
f7540 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 63 6f arToMultiByte(co
f7550 64 65 70 61 67 65 2c 20 30 2c 20 7a 57 69 64 65 depage, 0, zWide
f7560 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 30 2c Filename, -1, 0,
f7570 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7a 46 69 0, 0, 0);. zFi
f7580 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 lename = malloc(
f7590 20 6e 42 79 74 65 20 29 3b 0a 20 20 69 66 28 20 nByte );. if(
f75a0 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a zFilename==0 ){.
f75b0 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
f75c0 7d 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 64 65 }. nByte = Wide
f75d0 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 CharToMultiByte(
f75e0 63 6f 64 65 70 61 67 65 2c 20 30 2c 20 7a 57 69 codepage, 0, zWi
f75f0 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 deFilename, -1,
f7600 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 65 zFilename, nByte
f7610 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
f7620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f7630 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e 42 79 0, 0);. if( nBy
f7640 74 65 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 66 te == 0 ){. f
f7650 72 65 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a ree(zFilename);.
f7660 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 zFilename =
f7670 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 0;. }. return
f7680 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a zFilename;.}../*
f7690 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 75 6c 74 .** Convert mult
f76a0 69 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 ibyte character
f76b0 73 74 72 69 6e 67 20 74 6f 20 55 54 46 2d 38 2e string to UTF-8.
f76c0 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 Space to hold
f76d0 74 68 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 the.** returned
f76e0 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e string is obtain
f76f0 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 ed from malloc()
f7700 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
f7710 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 77 69 char *sqlite3_wi
f7720 6e 33 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 66 38 n32_mbcs_to_utf8
f7730 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 (const char *zFi
f7740 6c 65 6e 61 6d 65 29 7b 0a 20 20 63 68 61 72 20 lename){. char
f7750 2a 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38 3b 0a *zFilenameUtf8;.
f7760 20 20 57 43 48 41 52 20 2a 7a 54 6d 70 57 69 64 WCHAR *zTmpWid
f7770 65 3b 0a 0a 20 20 7a 54 6d 70 57 69 64 65 20 3d e;.. zTmpWide =
f7780 20 6d 62 63 73 54 6f 55 6e 69 63 6f 64 65 28 7a mbcsToUnicode(z
f7790 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 69 66 28 Filename);. if(
f77a0 20 7a 54 6d 70 57 69 64 65 3d 3d 30 20 29 7b 0a zTmpWide==0 ){.
f77b0 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
f77c0 7d 0a 20 20 7a 46 69 6c 65 6e 61 6d 65 55 74 66 }. zFilenameUtf
f77d0 38 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74 66 8 = unicodeToUtf
f77e0 38 28 7a 54 6d 70 57 69 64 65 29 3b 0a 20 20 66 8(zTmpWide);. f
f77f0 72 65 65 28 7a 54 6d 70 57 69 64 65 29 3b 0a 20 ree(zTmpWide);.
f7800 20 72 65 74 75 72 6e 20 7a 46 69 6c 65 6e 61 6d return zFilenam
f7810 65 55 74 66 38 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 eUtf8;.}../*.**
f7820 43 6f 6e 76 65 72 74 20 55 54 46 2d 38 20 74 6f Convert UTF-8 to
f7830 20 6d 75 6c 74 69 62 79 74 65 20 63 68 61 72 61 multibyte chara
f7840 63 74 65 72 20 73 74 72 69 6e 67 2e 20 20 53 70 cter string. Sp
f7850 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 ace to hold the
f7860 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 73 74 72 .** returned str
f7870 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64 20 ing is obtained
f7880 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a from malloc()..*
f7890 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 75 /.static char *u
f78a0 74 66 38 54 6f 4d 62 63 73 28 63 6f 6e 73 74 20 tf8ToMbcs(const
f78b0 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 char *zFilename)
f78c0 7b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e {. char *zFilen
f78d0 61 6d 65 4d 62 63 73 3b 0a 20 20 57 43 48 41 52 ameMbcs;. WCHAR
f78e0 20 2a 7a 54 6d 70 57 69 64 65 3b 0a 0a 20 20 7a *zTmpWide;.. z
f78f0 54 6d 70 57 69 64 65 20 3d 20 75 74 66 38 54 6f TmpWide = utf8To
f7900 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d Unicode(zFilenam
f7910 65 29 3b 0a 20 20 69 66 28 20 7a 54 6d 70 57 69 e);. if( zTmpWi
f7920 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 de==0 ){. ret
f7930 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 46 69 urn 0;. }. zFi
f7940 6c 65 6e 61 6d 65 4d 62 63 73 20 3d 20 75 6e 69 lenameMbcs = uni
f7950 63 6f 64 65 54 6f 4d 62 63 73 28 7a 54 6d 70 57 codeToMbcs(zTmpW
f7960 69 64 65 29 3b 0a 20 20 66 72 65 65 28 7a 54 6d ide);. free(zTm
f7970 70 57 69 64 65 29 3b 0a 20 20 72 65 74 75 72 6e pWide);. return
f7980 20 7a 46 69 6c 65 6e 61 6d 65 4d 62 63 73 3b 0a zFilenameMbcs;.
f7990 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 }..#if SQLITE_OS
f79a0 5f 57 49 4e 43 45 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a _WINCE./********
f79b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f79c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f79d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f79e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f79f0 2a 0a 2a 2a 20 54 68 69 73 20 73 65 63 74 69 6f *.** This sectio
f7a00 6e 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 n contains code
f7a10 66 6f 72 20 57 69 6e 43 45 20 6f 6e 6c 79 2e 0a for WinCE only..
f7a20 2a 2f 0a 2f 2a 0a 2a 2a 20 57 69 6e 64 6f 77 73 */./*.** Windows
f7a30 43 45 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 CE does not have
f7a40 20 61 20 6c 6f 63 61 6c 74 69 6d 65 28 29 20 66 a localtime() f
f7a50 75 6e 63 74 69 6f 6e 2e 20 20 53 6f 20 63 72 65 unction. So cre
f7a60 61 74 65 20 61 0a 2a 2a 20 73 75 62 73 74 69 74 ate a.** substit
f7a70 75 74 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 74 ute..*/.struct t
f7a80 6d 20 2a 5f 5f 63 64 65 63 6c 20 6c 6f 63 61 6c m *__cdecl local
f7a90 74 69 6d 65 28 63 6f 6e 73 74 20 74 69 6d 65 5f time(const time_
f7aa0 74 20 2a 74 29 0a 7b 0a 20 20 73 74 61 74 69 63 t *t).{. static
f7ab0 20 73 74 72 75 63 74 20 74 6d 20 79 3b 0a 20 20 struct tm y;.
f7ac0 46 49 4c 45 54 49 4d 45 20 75 54 6d 2c 20 6c 54 FILETIME uTm, lT
f7ad0 6d 3b 0a 20 20 53 59 53 54 45 4d 54 49 4d 45 20 m;. SYSTEMTIME
f7ae0 70 54 6d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 pTm;. sqlite3_i
f7af0 6e 74 36 34 20 74 36 34 3b 0a 20 20 74 36 34 20 nt64 t64;. t64
f7b00 3d 20 2a 74 3b 0a 20 20 74 36 34 20 3d 20 28 74 = *t;. t64 = (t
f7b10 36 34 20 2b 20 31 31 36 34 34 34 37 33 36 30 30 64 + 11644473600
f7b20 29 2a 31 30 30 30 30 30 30 30 3b 0a 20 20 75 54 )*10000000;. uT
f7b30 6d 2e 64 77 4c 6f 77 44 61 74 65 54 69 6d 65 20 m.dwLowDateTime
f7b40 3d 20 28 44 57 4f 52 44 29 28 74 36 34 20 26 20 = (DWORD)(t64 &
f7b50 30 78 46 46 46 46 46 46 46 46 29 3b 0a 20 20 75 0xFFFFFFFF);. u
f7b60 54 6d 2e 64 77 48 69 67 68 44 61 74 65 54 69 6d Tm.dwHighDateTim
f7b70 65 3d 20 28 44 57 4f 52 44 29 28 74 36 34 20 3e e= (DWORD)(t64 >
f7b80 3e 20 33 32 29 3b 0a 20 20 46 69 6c 65 54 69 6d > 32);. FileTim
f7b90 65 54 6f 4c 6f 63 61 6c 46 69 6c 65 54 69 6d 65 eToLocalFileTime
f7ba0 28 26 75 54 6d 2c 26 6c 54 6d 29 3b 0a 20 20 46 (&uTm,&lTm);. F
f7bb0 69 6c 65 54 69 6d 65 54 6f 53 79 73 74 65 6d 54 ileTimeToSystemT
f7bc0 69 6d 65 28 26 6c 54 6d 2c 26 70 54 6d 29 3b 0a ime(&lTm,&pTm);.
f7bd0 20 20 79 2e 74 6d 5f 79 65 61 72 20 3d 20 70 54 y.tm_year = pT
f7be0 6d 2e 77 59 65 61 72 20 2d 20 31 39 30 30 3b 0a m.wYear - 1900;.
f7bf0 20 20 79 2e 74 6d 5f 6d 6f 6e 20 3d 20 70 54 6d y.tm_mon = pTm
f7c00 2e 77 4d 6f 6e 74 68 20 2d 20 31 3b 0a 20 20 79 .wMonth - 1;. y
f7c10 2e 74 6d 5f 77 64 61 79 20 3d 20 70 54 6d 2e 77 .tm_wday = pTm.w
f7c20 44 61 79 4f 66 57 65 65 6b 3b 0a 20 20 79 2e 74 DayOfWeek;. y.t
f7c30 6d 5f 6d 64 61 79 20 3d 20 70 54 6d 2e 77 44 61 m_mday = pTm.wDa
f7c40 79 3b 0a 20 20 79 2e 74 6d 5f 68 6f 75 72 20 3d y;. y.tm_hour =
f7c50 20 70 54 6d 2e 77 48 6f 75 72 3b 0a 20 20 79 2e pTm.wHour;. y.
f7c60 74 6d 5f 6d 69 6e 20 3d 20 70 54 6d 2e 77 4d 69 tm_min = pTm.wMi
f7c70 6e 75 74 65 3b 0a 20 20 79 2e 74 6d 5f 73 65 63 nute;. y.tm_sec
f7c80 20 3d 20 70 54 6d 2e 77 53 65 63 6f 6e 64 3b 0a = pTm.wSecond;.
f7c90 20 20 72 65 74 75 72 6e 20 26 79 3b 0a 7d 0a 0a return &y;.}..
f7ca0 2f 2a 20 54 68 69 73 20 77 69 6c 6c 20 6e 65 76 /* This will nev
f7cb0 65 72 20 62 65 20 63 61 6c 6c 65 64 2c 20 62 75 er be called, bu
f7cc0 74 20 64 65 66 69 6e 65 64 20 74 6f 20 6d 61 6b t defined to mak
f7cd0 65 20 74 68 65 20 63 6f 64 65 20 63 6f 6d 70 69 e the code compi
f7ce0 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 47 65 le */.#define Ge
f7cf0 74 54 65 6d 70 50 61 74 68 41 28 61 2c 62 29 0a tTempPathA(a,b).
f7d00 0a 23 64 65 66 69 6e 65 20 4c 6f 63 6b 46 69 6c .#define LockFil
f7d10 65 28 61 2c 62 2c 63 2c 64 2c 65 29 20 20 20 20 e(a,b,c,d,e)
f7d20 20 20 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 winceLockFile
f7d30 28 26 61 2c 20 62 2c 20 63 2c 20 64 2c 20 65 29 (&a, b, c, d, e)
f7d40 0a 23 64 65 66 69 6e 65 20 55 6e 6c 6f 63 6b 46 .#define UnlockF
f7d50 69 6c 65 28 61 2c 62 2c 63 2c 64 2c 65 29 20 20 ile(a,b,c,d,e)
f7d60 20 20 20 77 69 6e 63 65 55 6e 6c 6f 63 6b 46 69 winceUnlockFi
f7d70 6c 65 28 26 61 2c 20 62 2c 20 63 2c 20 64 2c 20 le(&a, b, c, d,
f7d80 65 29 0a 23 64 65 66 69 6e 65 20 4c 6f 63 6b 46 e).#define LockF
f7d90 69 6c 65 45 78 28 61 2c 62 2c 63 2c 64 2c 65 2c ileEx(a,b,c,d,e,
f7da0 66 29 20 20 20 77 69 6e 63 65 4c 6f 63 6b 46 69 f) winceLockFi
f7db0 6c 65 45 78 28 26 61 2c 20 62 2c 20 63 2c 20 64 leEx(&a, b, c, d
f7dc0 2c 20 65 2c 20 66 29 0a 0a 23 64 65 66 69 6e 65 , e, f)..#define
f7dd0 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46 49 HANDLE_TO_WINFI
f7de0 4c 45 28 61 29 20 28 77 69 6e 46 69 6c 65 2a 29 LE(a) (winFile*)
f7df0 26 28 28 63 68 61 72 2a 29 61 29 5b 2d 28 69 6e &((char*)a)[-(in
f7e00 74 29 6f 66 66 73 65 74 6f 66 28 77 69 6e 46 69 t)offsetof(winFi
f7e10 6c 65 2c 68 29 5d 0a 0a 2f 2a 0a 2a 2a 20 41 63 le,h)]../*.** Ac
f7e20 71 75 69 72 65 20 61 20 6c 6f 63 6b 20 6f 6e 20 quire a lock on
f7e30 74 68 65 20 68 61 6e 64 6c 65 20 68 0a 2a 2f 0a the handle h.*/.
f7e40 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 63 static void winc
f7e50 65 4d 75 74 65 78 41 63 71 75 69 72 65 28 48 41 eMutexAcquire(HA
f7e60 4e 44 4c 45 20 68 29 7b 0a 20 20 20 44 57 4f 52 NDLE h){. DWOR
f7e70 44 20 64 77 45 72 72 3b 0a 20 20 20 64 6f 20 7b D dwErr;. do {
f7e80 0a 20 20 20 20 20 64 77 45 72 72 20 3d 20 57 61 . dwErr = Wa
f7e90 69 74 46 6f 72 53 69 6e 67 6c 65 4f 62 6a 65 63 itForSingleObjec
f7ea0 74 28 68 2c 20 49 4e 46 49 4e 49 54 45 29 3b 0a t(h, INFINITE);.
f7eb0 20 20 20 7d 20 77 68 69 6c 65 20 28 64 77 45 72 } while (dwEr
f7ec0 72 20 21 3d 20 57 41 49 54 5f 4f 42 4a 45 43 54 r != WAIT_OBJECT
f7ed0 5f 30 20 26 26 20 64 77 45 72 72 20 21 3d 20 57 _0 && dwErr != W
f7ee0 41 49 54 5f 41 42 41 4e 44 4f 4e 45 44 29 3b 0a AIT_ABANDONED);.
f7ef0 7d 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 }./*.** Release
f7f00 61 20 6c 6f 63 6b 20 61 63 71 75 69 72 65 64 20 a lock acquired
f7f10 62 79 20 77 69 6e 63 65 4d 75 74 65 78 41 63 71 by winceMutexAcq
f7f20 75 69 72 65 28 29 0a 2a 2f 0a 23 64 65 66 69 6e uire().*/.#defin
f7f30 65 20 77 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 e winceMutexRele
f7f40 61 73 65 28 68 29 20 52 65 6c 65 61 73 65 4d 75 ase(h) ReleaseMu
f7f50 74 65 78 28 68 29 0a 0a 2f 2a 0a 2a 2a 20 43 72 tex(h)../*.** Cr
f7f60 65 61 74 65 20 74 68 65 20 6d 75 74 65 78 20 61 eate the mutex a
f7f70 6e 64 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 nd shared memory
f7f80 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e used for lockin
f7f90 67 20 69 6e 20 74 68 65 20 66 69 6c 65 0a 2a 2a g in the file.**
f7fa0 20 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c descriptor pFil
f7fb0 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c e.*/.static BOOL
f7fc0 20 77 69 6e 63 65 43 72 65 61 74 65 4c 6f 63 6b winceCreateLock
f7fd0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 (const char *zFi
f7fe0 6c 65 6e 61 6d 65 2c 20 77 69 6e 46 69 6c 65 20 lename, winFile
f7ff0 2a 70 46 69 6c 65 29 7b 0a 20 20 57 43 48 41 52 *pFile){. WCHAR
f8000 20 2a 7a 54 6f 6b 3b 0a 20 20 57 43 48 41 52 20 *zTok;. WCHAR
f8010 2a 7a 4e 61 6d 65 20 3d 20 75 74 66 38 54 6f 55 *zName = utf8ToU
f8020 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65 nicode(zFilename
f8030 29 3b 0a 20 20 42 4f 4f 4c 20 62 49 6e 69 74 20 );. BOOL bInit
f8040 3d 20 54 52 55 45 3b 0a 0a 20 20 2f 2a 20 49 6e = TRUE;.. /* In
f8050 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6c 6f 63 itialize the loc
f8060 61 6c 20 6c 6f 63 6b 64 61 74 61 20 2a 2f 0a 20 al lockdata */.
f8070 20 5a 65 72 6f 4d 65 6d 6f 72 79 28 26 70 46 69 ZeroMemory(&pFi
f8080 6c 65 2d 3e 6c 6f 63 61 6c 2c 20 73 69 7a 65 6f le->local, sizeo
f8090 66 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 29 29 f(pFile->local))
f80a0 3b 0a 0a 20 20 2f 2a 20 52 65 70 6c 61 63 65 20 ;.. /* Replace
f80b0 74 68 65 20 62 61 63 6b 73 6c 61 73 68 65 73 20 the backslashes
f80c0 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 6e 61 6d from the filenam
f80d0 65 20 61 6e 64 20 6c 6f 77 65 72 63 61 73 65 20 e and lowercase
f80e0 69 74 0a 20 20 2a 2a 20 74 6f 20 64 65 72 69 76 it. ** to deriv
f80f0 65 20 61 20 6d 75 74 65 78 20 6e 61 6d 65 2e 20 e a mutex name.
f8100 2a 2f 0a 20 20 7a 54 6f 6b 20 3d 20 43 68 61 72 */. zTok = Char
f8110 4c 6f 77 65 72 57 28 7a 4e 61 6d 65 29 3b 0a 20 LowerW(zName);.
f8120 20 66 6f 72 20 28 3b 2a 7a 54 6f 6b 3b 7a 54 6f for (;*zTok;zTo
f8130 6b 2b 2b 29 7b 0a 20 20 20 20 69 66 20 28 2a 7a k++){. if (*z
f8140 54 6f 6b 20 3d 3d 20 27 5c 5c 27 29 20 2a 7a 54 Tok == '\\') *zT
f8150 6f 6b 20 3d 20 27 5f 27 3b 0a 20 20 7d 0a 0a 20 ok = '_';. }..
f8160 20 2f 2a 20 43 72 65 61 74 65 2f 6f 70 65 6e 20 /* Create/open
f8170 74 68 65 20 6e 61 6d 65 64 20 6d 75 74 65 78 20 the named mutex
f8180 2a 2f 0a 20 20 70 46 69 6c 65 2d 3e 68 4d 75 74 */. pFile->hMut
f8190 65 78 20 3d 20 43 72 65 61 74 65 4d 75 74 65 78 ex = CreateMutex
f81a0 57 28 4e 55 4c 4c 2c 20 46 41 4c 53 45 2c 20 7a W(NULL, FALSE, z
f81b0 4e 61 6d 65 29 3b 0a 20 20 69 66 20 28 21 70 46 Name);. if (!pF
f81c0 69 6c 65 2d 3e 68 4d 75 74 65 78 29 7b 0a 20 20 ile->hMutex){.
f81d0 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
f81e0 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f no = GetLastErro
f81f0 72 28 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 4e r();. free(zN
f8200 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e ame);. return
f8210 20 46 41 4c 53 45 3b 0a 20 20 7d 0a 0a 20 20 2f FALSE;. }.. /
f8220 2a 20 41 63 71 75 69 72 65 20 74 68 65 20 6d 75 * Acquire the mu
f8230 74 65 78 20 62 65 66 6f 72 65 20 63 6f 6e 74 69 tex before conti
f8240 6e 75 69 6e 67 20 2a 2f 0a 20 20 77 69 6e 63 65 nuing */. wince
f8250 4d 75 74 65 78 41 63 71 75 69 72 65 28 70 46 69 MutexAcquire(pFi
f8260 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 0a le->hMutex);. .
f8270 20 20 2f 2a 20 53 69 6e 63 65 20 74 68 65 20 6e /* Since the n
f8280 61 6d 65 73 20 6f 66 20 6e 61 6d 65 64 20 6d 75 ames of named mu
f8290 74 65 78 65 73 2c 20 73 65 6d 61 70 68 6f 72 65 texes, semaphore
f82a0 73 2c 20 66 69 6c 65 20 6d 61 70 70 69 6e 67 73 s, file mappings
f82b0 20 65 74 63 20 61 72 65 20 0a 20 20 2a 2a 20 63 etc are . ** c
f82c0 61 73 65 2d 73 65 6e 73 69 74 69 76 65 2c 20 74 ase-sensitive, t
f82d0 61 6b 65 20 61 64 76 61 6e 74 61 67 65 20 6f 66 ake advantage of
f82e0 20 74 68 61 74 20 62 79 20 75 70 70 65 72 63 61 that by upperca
f82f0 73 69 6e 67 20 74 68 65 20 6d 75 74 65 78 20 6e sing the mutex n
f8300 61 6d 65 0a 20 20 2a 2a 20 61 6e 64 20 75 73 69 ame. ** and usi
f8310 6e 67 20 74 68 61 74 20 61 73 20 74 68 65 20 73 ng that as the s
f8320 68 61 72 65 64 20 66 69 6c 65 6d 61 70 70 69 6e hared filemappin
f8330 67 20 6e 61 6d 65 2e 0a 20 20 2a 2f 0a 20 20 43 g name.. */. C
f8340 68 61 72 55 70 70 65 72 57 28 7a 4e 61 6d 65 29 harUpperW(zName)
f8350 3b 0a 20 20 70 46 69 6c 65 2d 3e 68 53 68 61 72 ;. pFile->hShar
f8360 65 64 20 3d 20 43 72 65 61 74 65 46 69 6c 65 4d ed = CreateFileM
f8370 61 70 70 69 6e 67 57 28 49 4e 56 41 4c 49 44 5f appingW(INVALID_
f8380 48 41 4e 44 4c 45 5f 56 41 4c 55 45 2c 20 4e 55 HANDLE_VALUE, NU
f8390 4c 4c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 LL,.
f83a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f83b0 20 20 20 20 20 20 20 20 20 20 20 50 41 47 45 5f PAGE_
f83c0 52 45 41 44 57 52 49 54 45 2c 20 30 2c 20 73 69 READWRITE, 0, si
f83d0 7a 65 6f 66 28 77 69 6e 63 65 4c 6f 63 6b 29 2c zeof(winceLock),
f83e0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
f83f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f8400 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 29 3b 20 zName);
f8410 20 0a 0a 20 20 2f 2a 20 53 65 74 20 61 20 66 6c .. /* Set a fl
f8420 61 67 20 74 68 61 74 20 69 6e 64 69 63 61 74 65 ag that indicate
f8430 73 20 77 65 27 72 65 20 74 68 65 20 66 69 72 73 s we're the firs
f8440 74 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 t to create the
f8450 6d 65 6d 6f 72 79 20 73 6f 20 69 74 20 0a 20 20 memory so it .
f8460 2a 2a 20 6d 75 73 74 20 62 65 20 7a 65 72 6f 2d ** must be zero-
f8470 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 initialized */.
f8480 20 69 66 20 28 47 65 74 4c 61 73 74 45 72 72 6f if (GetLastErro
f8490 72 28 29 20 3d 3d 20 45 52 52 4f 52 5f 41 4c 52 r() == ERROR_ALR
f84a0 45 41 44 59 5f 45 58 49 53 54 53 29 7b 0a 20 20 EADY_EXISTS){.
f84b0 20 20 62 49 6e 69 74 20 3d 20 46 41 4c 53 45 3b bInit = FALSE;
f84c0 0a 20 20 7d 0a 0a 20 20 66 72 65 65 28 7a 4e 61 . }.. free(zNa
f84d0 6d 65 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 77 65 me);.. /* If we
f84e0 20 73 75 63 63 65 65 64 65 64 20 69 6e 20 6d 61 succeeded in ma
f84f0 6b 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 king the shared
f8500 6d 65 6d 6f 72 79 20 68 61 6e 64 6c 65 2c 20 6d memory handle, m
f8510 61 70 20 69 74 2e 20 2a 2f 0a 20 20 69 66 20 28 ap it. */. if (
f8520 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 29 7b pFile->hShared){
f8530 0a 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 . pFile->shar
f8540 65 64 20 3d 20 28 77 69 6e 63 65 4c 6f 63 6b 2a ed = (winceLock*
f8550 29 4d 61 70 56 69 65 77 4f 66 46 69 6c 65 28 70 )MapViewOfFile(p
f8560 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 2c 20 0a File->hShared, .
f8570 20 20 20 20 20 20 20 20 20 20 20 20 20 46 49 4c FIL
f8580 45 5f 4d 41 50 5f 52 45 41 44 7c 46 49 4c 45 5f E_MAP_READ|FILE_
f8590 4d 41 50 5f 57 52 49 54 45 2c 20 30 2c 20 30 2c MAP_WRITE, 0, 0,
f85a0 20 73 69 7a 65 6f 66 28 77 69 6e 63 65 4c 6f 63 sizeof(winceLoc
f85b0 6b 29 29 3b 0a 20 20 20 20 2f 2a 20 49 66 20 6d k));. /* If m
f85c0 61 70 70 69 6e 67 20 66 61 69 6c 65 64 2c 20 63 apping failed, c
f85d0 6c 6f 73 65 20 74 68 65 20 73 68 61 72 65 64 20 lose the shared
f85e0 6d 65 6d 6f 72 79 20 68 61 6e 64 6c 65 20 61 6e memory handle an
f85f0 64 20 65 72 61 73 65 20 69 74 20 2a 2f 0a 20 20 d erase it */.
f8600 20 20 69 66 20 28 21 70 46 69 6c 65 2d 3e 73 68 if (!pFile->sh
f8610 61 72 65 64 29 7b 0a 20 20 20 20 20 20 70 46 69 ared){. pFi
f8620 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
f8630 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a GetLastError();.
f8640 20 20 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c CloseHandl
f8650 65 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 e(pFile->hShared
f8660 29 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e );. pFile->
f8670 68 53 68 61 72 65 64 20 3d 20 4e 55 4c 4c 3b 0a hShared = NULL;.
f8680 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
f8690 49 66 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 If shared memory
f86a0 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 72 could not be cr
f86b0 65 61 74 65 64 2c 20 74 68 65 6e 20 63 6c 6f 73 eated, then clos
f86c0 65 20 74 68 65 20 6d 75 74 65 78 20 61 6e 64 20 e the mutex and
f86d0 66 61 69 6c 20 2a 2f 0a 20 20 69 66 20 28 70 46 fail */. if (pF
f86e0 69 6c 65 2d 3e 68 53 68 61 72 65 64 20 3d 3d 20 ile->hShared ==
f86f0 4e 55 4c 4c 29 7b 0a 20 20 20 20 77 69 6e 63 65 NULL){. wince
f8700 4d 75 74 65 78 52 65 6c 65 61 73 65 28 70 46 69 MutexRelease(pFi
f8710 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 20 le->hMutex);.
f8720 20 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 CloseHandle(pFi
f8730 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 20 le->hMutex);.
f8740 20 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 20 3d pFile->hMutex =
f8750 20 4e 55 4c 4c 3b 0a 20 20 20 20 72 65 74 75 72 NULL;. retur
f8760 6e 20 46 41 4c 53 45 3b 0a 20 20 7d 0a 20 20 0a n FALSE;. }. .
f8770 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 /* Initialize
f8780 74 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 the shared memor
f8790 79 20 69 66 20 77 65 27 72 65 20 73 75 70 70 6f y if we're suppo
f87a0 73 65 64 20 74 6f 20 2a 2f 0a 20 20 69 66 20 28 sed to */. if (
f87b0 62 49 6e 69 74 29 20 7b 0a 20 20 20 20 5a 65 72 bInit) {. Zer
f87c0 6f 4d 65 6d 6f 72 79 28 70 46 69 6c 65 2d 3e 73 oMemory(pFile->s
f87d0 68 61 72 65 64 2c 20 73 69 7a 65 6f 66 28 77 69 hared, sizeof(wi
f87e0 6e 63 65 4c 6f 63 6b 29 29 3b 0a 20 20 7d 0a 0a nceLock));. }..
f87f0 20 20 77 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 winceMutexRele
f8800 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 ase(pFile->hMute
f8810 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 54 52 55 x);. return TRU
f8820 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 74 E;.}../*.** Dest
f8830 72 6f 79 20 74 68 65 20 70 61 72 74 20 6f 66 20 roy the part of
f8840 77 69 6e 46 69 6c 65 20 74 68 61 74 20 64 65 61 winFile that dea
f8850 6c 73 20 77 69 74 68 20 77 69 6e 63 65 20 6c 6f ls with wince lo
f8860 63 6b 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f cks.*/.static vo
f8870 69 64 20 77 69 6e 63 65 44 65 73 74 72 6f 79 4c id winceDestroyL
f8880 6f 63 6b 28 77 69 6e 46 69 6c 65 20 2a 70 46 69 ock(winFile *pFi
f8890 6c 65 29 7b 0a 20 20 69 66 20 28 70 46 69 6c 65 le){. if (pFile
f88a0 2d 3e 68 4d 75 74 65 78 29 7b 0a 20 20 20 20 2f ->hMutex){. /
f88b0 2a 20 41 63 71 75 69 72 65 20 74 68 65 20 6d 75 * Acquire the mu
f88c0 74 65 78 20 2a 2f 0a 20 20 20 20 77 69 6e 63 65 tex */. wince
f88d0 4d 75 74 65 78 41 63 71 75 69 72 65 28 70 46 69 MutexAcquire(pFi
f88e0 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a 20 20 le->hMutex);..
f88f0 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 /* The followi
f8900 6e 67 20 62 6c 6f 63 6b 73 20 73 68 6f 75 6c 64 ng blocks should
f8910 20 70 72 6f 62 61 62 6c 79 20 61 73 73 65 72 74 probably assert
f8920 20 69 6e 20 64 65 62 75 67 20 6d 6f 64 65 2c 20 in debug mode,
f8930 62 75 74 20 74 68 65 79 0a 20 20 20 20 20 20 20 but they.
f8940 61 72 65 20 74 6f 20 63 6c 65 61 6e 75 70 20 69 are to cleanup i
f8950 6e 20 63 61 73 65 20 61 6e 79 20 6c 6f 63 6b 73 n case any locks
f8960 20 72 65 6d 61 69 6e 65 64 20 6f 70 65 6e 20 2a remained open *
f8970 2f 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d /. if (pFile-
f8980 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 29 >local.nReaders)
f8990 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 {. pFile->s
f89a0 68 61 72 65 64 2d 3e 6e 52 65 61 64 65 72 73 20 hared->nReaders
f89b0 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 --;. }. if
f89c0 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 (pFile->local.b
f89d0 52 65 73 65 72 76 65 64 29 7b 0a 20 20 20 20 20 Reserved){.
f89e0 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e pFile->shared->
f89f0 62 52 65 73 65 72 76 65 64 20 3d 20 46 41 4c 53 bReserved = FALS
f8a00 45 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 20 E;. }. if
f8a10 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50 (pFile->local.bP
f8a20 65 6e 64 69 6e 67 29 7b 0a 20 20 20 20 20 20 70 ending){. p
f8a30 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50 File->shared->bP
f8a40 65 6e 64 69 6e 67 20 3d 20 46 41 4c 53 45 3b 0a ending = FALSE;.
f8a50 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 70 46 }. if (pF
f8a60 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78 63 6c ile->local.bExcl
f8a70 75 73 69 76 65 29 7b 0a 20 20 20 20 20 20 70 46 usive){. pF
f8a80 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 ile->shared->bEx
f8a90 63 6c 75 73 69 76 65 20 3d 20 46 41 4c 53 45 3b clusive = FALSE;
f8aa0 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 . }.. /* D
f8ab0 65 2d 72 65 66 65 72 65 6e 63 65 20 61 6e 64 20 e-reference and
f8ac0 63 6c 6f 73 65 20 6f 75 72 20 63 6f 70 79 20 6f close our copy o
f8ad0 66 20 74 68 65 20 73 68 61 72 65 64 20 6d 65 6d f the shared mem
f8ae0 6f 72 79 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 ory handle */.
f8af0 20 20 55 6e 6d 61 70 56 69 65 77 4f 66 46 69 6c UnmapViewOfFil
f8b00 65 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 29 e(pFile->shared)
f8b10 3b 0a 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c ;. CloseHandl
f8b20 65 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 e(pFile->hShared
f8b30 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 6f 6e 65 20 );.. /* Done
f8b40 77 69 74 68 20 74 68 65 20 6d 75 74 65 78 20 2a with the mutex *
f8b50 2f 0a 20 20 20 20 77 69 6e 63 65 4d 75 74 65 78 /. winceMutex
f8b60 52 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 Release(pFile->h
f8b70 4d 75 74 65 78 29 3b 20 20 20 20 0a 20 20 20 20 Mutex); .
f8b80 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c CloseHandle(pFil
f8b90 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 20 20 e->hMutex);.
f8ba0 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 20 3d 20 pFile->hMutex =
f8bb0 4e 55 4c 4c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 NULL;. }.}../*
f8bc0 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e 74 .** An implement
f8bd0 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 4c 6f 63 ation of the Loc
f8be0 6b 46 69 6c 65 28 29 20 41 50 49 20 6f 66 20 77 kFile() API of w
f8bf0 69 6e 64 6f 77 73 20 66 6f 72 20 77 69 6e 63 65 indows for wince
f8c00 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20 .*/.static BOOL
f8c10 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 28 0a 20 winceLockFile(.
f8c20 20 48 41 4e 44 4c 45 20 2a 70 68 46 69 6c 65 2c HANDLE *phFile,
f8c30 0a 20 20 44 57 4f 52 44 20 64 77 46 69 6c 65 4f . DWORD dwFileO
f8c40 66 66 73 65 74 4c 6f 77 2c 0a 20 20 44 57 4f 52 ffsetLow,. DWOR
f8c50 44 20 64 77 46 69 6c 65 4f 66 66 73 65 74 48 69 D dwFileOffsetHi
f8c60 67 68 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75 6d gh,. DWORD nNum
f8c70 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b berOfBytesToLock
f8c80 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75 Low,. DWORD nNu
f8c90 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 mberOfBytesToLoc
f8ca0 6b 48 69 67 68 0a 29 7b 0a 20 20 77 69 6e 46 69 kHigh.){. winFi
f8cb0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 48 41 4e 44 le *pFile = HAND
f8cc0 4c 45 5f 54 4f 5f 57 49 4e 46 49 4c 45 28 70 68 LE_TO_WINFILE(ph
f8cd0 46 69 6c 65 29 3b 0a 20 20 42 4f 4f 4c 20 62 52 File);. BOOL bR
f8ce0 65 74 75 72 6e 20 3d 20 46 41 4c 53 45 3b 0a 0a eturn = FALSE;..
f8cf0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
f8d00 45 52 28 64 77 46 69 6c 65 4f 66 66 73 65 74 48 ER(dwFileOffsetH
f8d10 69 67 68 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 igh);. UNUSED_P
f8d20 41 52 41 4d 45 54 45 52 28 6e 4e 75 6d 62 65 72 ARAMETER(nNumber
f8d30 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 48 69 67 OfBytesToLockHig
f8d40 68 29 3b 0a 0a 20 20 69 66 20 28 21 70 46 69 6c h);.. if (!pFil
f8d50 65 2d 3e 68 4d 75 74 65 78 29 20 72 65 74 75 72 e->hMutex) retur
f8d60 6e 20 54 52 55 45 3b 0a 20 20 77 69 6e 63 65 4d n TRUE;. winceM
f8d70 75 74 65 78 41 63 71 75 69 72 65 28 70 46 69 6c utexAcquire(pFil
f8d80 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a 20 20 2f e->hMutex);.. /
f8d90 2a 20 57 61 6e 74 69 6e 67 20 61 6e 20 65 78 63 * Wanting an exc
f8da0 6c 75 73 69 76 65 20 6c 6f 63 6b 3f 20 2a 2f 0a lusive lock? */.
f8db0 20 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73 if (dwFileOffs
f8dc0 65 74 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 etLow == (DWORD)
f8dd0 53 48 41 52 45 44 5f 46 49 52 53 54 0a 20 20 20 SHARED_FIRST.
f8de0 20 20 20 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 && nNumberOf
f8df0 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d BytesToLockLow =
f8e00 3d 20 28 44 57 4f 52 44 29 53 48 41 52 45 44 5f = (DWORD)SHARED_
f8e10 53 49 5a 45 29 7b 0a 20 20 20 20 69 66 20 28 70 SIZE){. if (p
f8e20 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 File->shared->nR
f8e30 65 61 64 65 72 73 20 3d 3d 20 30 20 26 26 20 70 eaders == 0 && p
f8e40 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 File->shared->bE
f8e50 78 63 6c 75 73 69 76 65 20 3d 3d 20 30 29 7b 0a xclusive == 0){.
f8e60 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 pFile->sh
f8e70 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 76 65 ared->bExclusive
f8e80 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 20 20 20 = TRUE;.
f8e90 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78 pFile->local.bEx
f8ea0 63 6c 75 73 69 76 65 20 3d 20 54 52 55 45 3b 0a clusive = TRUE;.
f8eb0 20 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d bReturn =
f8ec0 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d TRUE;. }. }
f8ed0 0a 0a 20 20 2f 2a 20 57 61 6e 74 20 61 20 72 65 .. /* Want a re
f8ee0 61 64 2d 6f 6e 6c 79 20 6c 6f 63 6b 3f 20 2a 2f ad-only lock? */
f8ef0 0a 20 20 65 6c 73 65 20 69 66 20 28 64 77 46 69 . else if (dwFi
f8f00 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 28 leOffsetLow == (
f8f10 44 57 4f 52 44 29 53 48 41 52 45 44 5f 46 49 52 DWORD)SHARED_FIR
f8f20 53 54 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 ST &&.
f8f30 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 nNumberOfBytesT
f8f40 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a oLockLow == 1){.
f8f50 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 73 if (pFile->s
f8f60 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 76 hared->bExclusiv
f8f70 65 20 3d 3d 20 30 29 7b 0a 20 20 20 20 20 20 70 e == 0){. p
f8f80 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 File->local.nRea
f8f90 64 65 72 73 20 2b 2b 3b 0a 20 20 20 20 20 20 69 ders ++;. i
f8fa0 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e f (pFile->local.
f8fb0 6e 52 65 61 64 65 72 73 20 3d 3d 20 31 29 7b 0a nReaders == 1){.
f8fc0 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 pFile->s
f8fd0 68 61 72 65 64 2d 3e 6e 52 65 61 64 65 72 73 20 hared->nReaders
f8fe0 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ++;. }.
f8ff0 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45 bReturn = TRUE
f9000 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f ;. }. }.. /
f9010 2a 20 57 61 6e 74 20 61 20 70 65 6e 64 69 6e 67 * Want a pending
f9020 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 65 6c 73 65 lock? */. else
f9030 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65 if (dwFileOffse
f9040 74 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 50 tLow == (DWORD)P
f9050 45 4e 44 49 4e 47 5f 42 59 54 45 20 26 26 20 6e ENDING_BYTE && n
f9060 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c NumberOfBytesToL
f9070 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 ockLow == 1){.
f9080 20 20 2f 2a 20 49 66 20 6e 6f 20 70 65 6e 64 69 /* If no pendi
f9090 6e 67 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e ng lock has been
f90a0 20 61 63 71 75 69 72 65 64 2c 20 74 68 65 6e 20 acquired, then
f90b0 61 63 71 75 69 72 65 20 69 74 20 2a 2f 0a 20 20 acquire it */.
f90c0 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 73 68 61 if (pFile->sha
f90d0 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67 20 3d 3d red->bPending ==
f90e0 20 30 29 20 7b 0a 20 20 20 20 20 20 70 46 69 6c 0) {. pFil
f90f0 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50 65 6e 64 e->shared->bPend
f9100 69 6e 67 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 ing = TRUE;.
f9110 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 pFile->local.b
f9120 50 65 6e 64 69 6e 67 20 3d 20 54 52 55 45 3b 0a Pending = TRUE;.
f9130 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 bReturn =
f9140 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a TRUE;. }. }.
f9150 0a 20 20 2f 2a 20 57 61 6e 74 20 61 20 72 65 73 . /* Want a res
f9160 65 72 76 65 64 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 erved lock? */.
f9170 20 65 6c 73 65 20 69 66 20 28 64 77 46 69 6c 65 else if (dwFile
f9180 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 28 44 57 OffsetLow == (DW
f9190 4f 52 44 29 52 45 53 45 52 56 45 44 5f 42 59 54 ORD)RESERVED_BYT
f91a0 45 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 E && nNumberOfBy
f91b0 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 tesToLockLow ==
f91c0 31 29 7b 0a 20 20 20 20 69 66 20 28 70 46 69 6c 1){. if (pFil
f91d0 65 2d 3e 73 68 61 72 65 64 2d 3e 62 52 65 73 65 e->shared->bRese
f91e0 72 76 65 64 20 3d 3d 20 30 29 20 7b 0a 20 20 20 rved == 0) {.
f91f0 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 pFile->shared
f9200 2d 3e 62 52 65 73 65 72 76 65 64 20 3d 20 54 52 ->bReserved = TR
f9210 55 45 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d UE;. pFile-
f9220 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72 76 65 64 >local.bReserved
f9230 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 20 20 62 = TRUE;. b
f9240 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 Return = TRUE;.
f9250 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 77 69 6e 63 }. }.. winc
f9260 65 4d 75 74 65 78 52 65 6c 65 61 73 65 28 70 46 eMutexRelease(pF
f9270 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 ile->hMutex);.
f9280 72 65 74 75 72 6e 20 62 52 65 74 75 72 6e 3b 0a return bReturn;.
f9290 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6d 70 6c }../*.** An impl
f92a0 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 ementation of th
f92b0 65 20 55 6e 6c 6f 63 6b 46 69 6c 65 20 41 50 49 e UnlockFile API
f92c0 20 6f 66 20 77 69 6e 64 6f 77 73 20 66 6f 72 20 of windows for
f92d0 77 69 6e 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20 wince.*/.static
f92e0 42 4f 4f 4c 20 77 69 6e 63 65 55 6e 6c 6f 63 6b BOOL winceUnlock
f92f0 46 69 6c 65 28 0a 20 20 48 41 4e 44 4c 45 20 2a File(. HANDLE *
f9300 70 68 46 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 phFile,. DWORD
f9310 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 2c dwFileOffsetLow,
f9320 0a 20 20 44 57 4f 52 44 20 64 77 46 69 6c 65 4f . DWORD dwFileO
f9330 66 66 73 65 74 48 69 67 68 2c 0a 20 20 44 57 4f ffsetHigh,. DWO
f9340 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 RD nNumberOfByte
f9350 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 2c 0a 20 20 sToUnlockLow,.
f9360 44 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 DWORD nNumberOfB
f9370 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 48 69 67 68 ytesToUnlockHigh
f9380 0a 29 7b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 .){. winFile *p
f9390 46 69 6c 65 20 3d 20 48 41 4e 44 4c 45 5f 54 4f File = HANDLE_TO
f93a0 5f 57 49 4e 46 49 4c 45 28 70 68 46 69 6c 65 29 _WINFILE(phFile)
f93b0 3b 0a 20 20 42 4f 4f 4c 20 62 52 65 74 75 72 6e ;. BOOL bReturn
f93c0 20 3d 20 46 41 4c 53 45 3b 0a 0a 20 20 55 4e 55 = FALSE;.. UNU
f93d0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 64 77 SED_PARAMETER(dw
f93e0 46 69 6c 65 4f 66 66 73 65 74 48 69 67 68 29 3b FileOffsetHigh);
f93f0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
f9400 54 45 52 28 6e 4e 75 6d 62 65 72 4f 66 42 79 74 TER(nNumberOfByt
f9410 65 73 54 6f 55 6e 6c 6f 63 6b 48 69 67 68 29 3b esToUnlockHigh);
f9420 0a 0a 20 20 69 66 20 28 21 70 46 69 6c 65 2d 3e .. if (!pFile->
f9430 68 4d 75 74 65 78 29 20 72 65 74 75 72 6e 20 54 hMutex) return T
f9440 52 55 45 3b 0a 20 20 77 69 6e 63 65 4d 75 74 65 RUE;. winceMute
f9450 78 41 63 71 75 69 72 65 28 70 46 69 6c 65 2d 3e xAcquire(pFile->
f9460 68 4d 75 74 65 78 29 3b 0a 0a 20 20 2f 2a 20 52 hMutex);.. /* R
f9470 65 6c 65 61 73 69 6e 67 20 61 20 72 65 61 64 65 eleasing a reade
f9480 72 20 6c 6f 63 6b 20 6f 72 20 61 6e 20 65 78 63 r lock or an exc
f9490 6c 75 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a 20 lusive lock */.
f94a0 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65 if (dwFileOffse
f94b0 74 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 53 tLow == (DWORD)S
f94c0 48 41 52 45 44 5f 46 49 52 53 54 29 7b 0a 20 20 HARED_FIRST){.
f94d0 20 20 2f 2a 20 44 69 64 20 77 65 20 68 61 76 65 /* Did we have
f94e0 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f an exclusive lo
f94f0 63 6b 3f 20 2a 2f 0a 20 20 20 20 69 66 20 28 70 ck? */. if (p
f9500 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78 63 File->local.bExc
f9510 6c 75 73 69 76 65 29 7b 0a 20 20 20 20 20 20 61 lusive){. a
f9520 73 73 65 72 74 28 6e 4e 75 6d 62 65 72 4f 66 42 ssert(nNumberOfB
f9530 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 ytesToUnlockLow
f9540 3d 3d 20 28 44 57 4f 52 44 29 53 48 41 52 45 44 == (DWORD)SHARED
f9550 5f 53 49 5a 45 29 3b 0a 20 20 20 20 20 20 70 46 _SIZE);. pF
f9560 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78 63 6c ile->local.bExcl
f9570 75 73 69 76 65 20 3d 20 46 41 4c 53 45 3b 0a 20 usive = FALSE;.
f9580 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 pFile->shar
f9590 65 64 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 3d ed->bExclusive =
f95a0 20 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 62 52 FALSE;. bR
f95b0 65 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 eturn = TRUE;.
f95c0 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 69 64 20 }.. /* Did
f95d0 77 65 20 6a 75 73 74 20 68 61 76 65 20 61 20 72 we just have a r
f95e0 65 61 64 65 72 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 eader lock? */.
f95f0 20 20 20 65 6c 73 65 20 69 66 20 28 70 46 69 6c else if (pFil
f9600 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 e->local.nReader
f9610 73 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 s){. assert
f9620 28 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 (nNumberOfBytesT
f9630 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 28 44 oUnlockLow == (D
f9640 57 4f 52 44 29 53 48 41 52 45 44 5f 53 49 5a 45 WORD)SHARED_SIZE
f9650 20 7c 7c 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 || nNumberOfByt
f9660 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d esToUnlockLow ==
f9670 20 31 29 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 1);. pFile
f9680 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 ->local.nReaders
f9690 20 2d 2d 3b 0a 20 20 20 20 20 20 69 66 20 28 70 --;. if (p
f96a0 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 File->local.nRea
f96b0 64 65 72 73 20 3d 3d 20 30 29 0a 20 20 20 20 20 ders == 0).
f96c0 20 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 {. pFile
f96d0 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 65 61 64 65 ->shared->nReade
f96e0 72 73 20 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a 20 rs --;. }.
f96f0 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54 bReturn = T
f9700 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a RUE;. }. }..
f9710 20 20 2f 2a 20 52 65 6c 65 61 73 69 6e 67 20 61 /* Releasing a
f9720 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 2a 2f pending lock */
f9730 0a 20 20 65 6c 73 65 20 69 66 20 28 64 77 46 69 . else if (dwFi
f9740 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 28 leOffsetLow == (
f9750 44 57 4f 52 44 29 50 45 4e 44 49 4e 47 5f 42 59 DWORD)PENDING_BY
f9760 54 45 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 TE && nNumberOfB
f9770 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 ytesToUnlockLow
f9780 3d 3d 20 31 29 7b 0a 20 20 20 20 69 66 20 28 70 == 1){. if (p
f9790 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e File->local.bPen
f97a0 64 69 6e 67 29 7b 0a 20 20 20 20 20 20 70 46 69 ding){. pFi
f97b0 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 le->local.bPendi
f97c0 6e 67 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 ng = FALSE;.
f97d0 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d pFile->shared-
f97e0 3e 62 50 65 6e 64 69 6e 67 20 3d 20 46 41 4c 53 >bPending = FALS
f97f0 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e E;. bReturn
f9800 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 = TRUE;. }.
f9810 20 7d 0a 20 20 2f 2a 20 52 65 6c 65 61 73 69 6e }. /* Releasin
f9820 67 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 g a reserved loc
f9830 6b 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 k */. else if (
f9840 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 dwFileOffsetLow
f9850 3d 3d 20 28 44 57 4f 52 44 29 52 45 53 45 52 56 == (DWORD)RESERV
f9860 45 44 5f 42 59 54 45 20 26 26 20 6e 4e 75 6d 62 ED_BYTE && nNumb
f9870 65 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 erOfBytesToUnloc
f9880 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20 kLow == 1){.
f9890 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c if (pFile->local
f98a0 2e 62 52 65 73 65 72 76 65 64 29 20 7b 0a 20 20 .bReserved) {.
f98b0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c pFile->local
f98c0 2e 62 52 65 73 65 72 76 65 64 20 3d 20 46 41 4c .bReserved = FAL
f98d0 53 45 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d SE;. pFile-
f98e0 3e 73 68 61 72 65 64 2d 3e 62 52 65 73 65 72 76 >shared->bReserv
f98f0 65 64 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 ed = FALSE;.
f9900 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45 bReturn = TRUE
f9910 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 77 ;. }. }.. w
f9920 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 61 73 65 inceMutexRelease
f9930 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b (pFile->hMutex);
f9940 0a 20 20 72 65 74 75 72 6e 20 62 52 65 74 75 72 . return bRetur
f9950 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 n;.}../*.** An i
f9960 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
f9970 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 the LockFileEx(
f9980 29 20 41 50 49 20 6f 66 20 77 69 6e 64 6f 77 73 ) API of windows
f9990 20 66 6f 72 20 77 69 6e 63 65 0a 2a 2f 0a 73 74 for wince.*/.st
f99a0 61 74 69 63 20 42 4f 4f 4c 20 77 69 6e 63 65 4c atic BOOL winceL
f99b0 6f 63 6b 46 69 6c 65 45 78 28 0a 20 20 48 41 4e ockFileEx(. HAN
f99c0 44 4c 45 20 2a 70 68 46 69 6c 65 2c 0a 20 20 44 DLE *phFile,. D
f99d0 57 4f 52 44 20 64 77 46 6c 61 67 73 2c 0a 20 20 WORD dwFlags,.
f99e0 44 57 4f 52 44 20 64 77 52 65 73 65 72 76 65 64 DWORD dwReserved
f99f0 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75 6d 62 65 ,. DWORD nNumbe
f9a00 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f rOfBytesToLockLo
f9a10 77 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75 6d 62 w,. DWORD nNumb
f9a20 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 48 erOfBytesToLockH
f9a30 69 67 68 2c 0a 20 20 4c 50 4f 56 45 52 4c 41 50 igh,. LPOVERLAP
f9a40 50 45 44 20 6c 70 4f 76 65 72 6c 61 70 70 65 64 PED lpOverlapped
f9a50 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 .){. UNUSED_PAR
f9a60 41 4d 45 54 45 52 28 64 77 52 65 73 65 72 76 65 AMETER(dwReserve
f9a70 64 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 d);. UNUSED_PAR
f9a80 41 4d 45 54 45 52 28 6e 4e 75 6d 62 65 72 4f 66 AMETER(nNumberOf
f9a90 42 79 74 65 73 54 6f 4c 6f 63 6b 48 69 67 68 29 BytesToLockHigh)
f9aa0 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 ;.. /* If the c
f9ab0 61 6c 6c 65 72 20 77 61 6e 74 73 20 61 20 73 68 aller wants a sh
f9ac0 61 72 65 64 20 72 65 61 64 20 6c 6f 63 6b 2c 20 ared read lock,
f9ad0 66 6f 72 77 61 72 64 20 74 68 69 73 20 63 61 6c forward this cal
f9ae0 6c 0a 20 20 2a 2a 20 74 6f 20 77 69 6e 63 65 4c l. ** to winceL
f9af0 6f 63 6b 46 69 6c 65 20 2a 2f 0a 20 20 69 66 20 ockFile */. if
f9b00 28 6c 70 4f 76 65 72 6c 61 70 70 65 64 2d 3e 4f (lpOverlapped->O
f9b10 66 66 73 65 74 20 3d 3d 20 28 44 57 4f 52 44 29 ffset == (DWORD)
f9b20 53 48 41 52 45 44 5f 46 49 52 53 54 20 26 26 0a SHARED_FIRST &&.
f9b30 20 20 20 20 20 20 64 77 46 6c 61 67 73 20 3d 3d dwFlags ==
f9b40 20 31 20 26 26 0a 20 20 20 20 20 20 6e 4e 75 6d 1 &&. nNum
f9b50 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b berOfBytesToLock
f9b60 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 53 48 Low == (DWORD)SH
f9b70 41 52 45 44 5f 53 49 5a 45 29 7b 0a 20 20 20 20 ARED_SIZE){.
f9b80 72 65 74 75 72 6e 20 77 69 6e 63 65 4c 6f 63 6b return winceLock
f9b90 46 69 6c 65 28 70 68 46 69 6c 65 2c 20 53 48 41 File(phFile, SHA
f9ba0 52 45 44 5f 46 49 52 53 54 2c 20 30 2c 20 31 2c RED_FIRST, 0, 1,
f9bb0 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 0);. }. retur
f9bc0 6e 20 46 41 4c 53 45 3b 0a 7d 0a 2f 2a 0a 2a 2a n FALSE;.}./*.**
f9bd0 20 45 6e 64 20 6f 66 20 74 68 65 20 73 70 65 63 End of the spec
f9be0 69 61 6c 20 63 6f 64 65 20 66 6f 72 20 77 69 6e ial code for win
f9bf0 63 65 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ce.*************
f9c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9c40 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 /.#endif /* SQLI
f9c50 54 45 5f 4f 53 5f 57 49 4e 43 45 20 2a 2f 0a 0a TE_OS_WINCE */..
f9c60 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /***************
f9c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
f9cb0 2a 20 54 68 65 20 6e 65 78 74 20 67 72 6f 75 70 * The next group
f9cc0 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 of routines imp
f9cd0 6c 65 6d 65 6e 74 20 74 68 65 20 49 2f 4f 20 6d lement the I/O m
f9ce0 65 74 68 6f 64 73 20 73 70 65 63 69 66 69 65 64 ethods specified
f9cf0 0a 2a 2a 20 62 79 20 74 68 65 20 73 71 6c 69 74 .** by the sqlit
f9d00 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 e3_io_methods ob
f9d10 6a 65 63 74 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ject..**********
f9d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9d60 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f ****/../*.** Clo
f9d70 73 65 20 61 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a se a file..**.**
f9d80 20 49 74 20 69 73 20 72 65 70 6f 72 74 65 64 20 It is reported
f9d90 74 68 61 74 20 61 6e 20 61 74 74 65 6d 70 74 20 that an attempt
f9da0 74 6f 20 63 6c 6f 73 65 20 61 20 68 61 6e 64 6c to close a handl
f9db0 65 20 6d 69 67 68 74 20 73 6f 6d 65 74 69 6d 65 e might sometime
f9dc0 73 0a 2a 2a 20 66 61 69 6c 2e 20 20 54 68 69 73 s.** fail. This
f9dd0 20 69 73 20 61 20 76 65 72 79 20 75 6e 72 65 61 is a very unrea
f9de0 73 6f 6e 61 62 6c 65 20 72 65 73 75 6c 74 2c 20 sonable result,
f9df0 62 75 74 20 77 69 6e 64 6f 77 73 20 69 73 20 6e but windows is n
f9e00 6f 74 6f 72 69 6f 75 73 0a 2a 2a 20 66 6f 72 20 otorious.** for
f9e10 62 65 69 6e 67 20 75 6e 72 65 61 73 6f 6e 61 62 being unreasonab
f9e20 6c 65 20 73 6f 20 49 20 64 6f 20 6e 6f 74 20 64 le so I do not d
f9e30 6f 75 62 74 20 74 68 61 74 20 69 74 20 6d 69 67 oubt that it mig
f9e40 68 74 20 68 61 70 70 65 6e 2e 20 20 49 66 0a 2a ht happen. If.*
f9e50 2a 20 74 68 65 20 63 6c 6f 73 65 20 66 61 69 6c * the close fail
f9e60 73 2c 20 77 65 20 70 61 75 73 65 20 66 6f 72 20 s, we pause for
f9e70 31 30 30 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 100 milliseconds
f9e80 20 61 6e 64 20 74 72 79 20 61 67 61 69 6e 2e 20 and try again.
f9e90 20 41 73 0a 2a 2a 20 6d 61 6e 79 20 61 73 20 4d As.** many as M
f9ea0 58 5f 43 4c 4f 53 45 5f 41 54 54 45 4d 50 54 20 X_CLOSE_ATTEMPT
f9eb0 61 74 74 65 6d 70 74 73 20 74 6f 20 63 6c 6f 73 attempts to clos
f9ec0 65 20 74 68 65 20 68 61 6e 64 6c 65 20 61 72 65 e the handle are
f9ed0 20 6d 61 64 65 20 62 65 66 6f 72 65 0a 2a 2a 20 made before.**
f9ee0 67 69 76 69 6e 67 20 75 70 20 61 6e 64 20 72 65 giving up and re
f9ef0 74 75 72 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72 turning an error
f9f00 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f ..*/.#define MX_
f9f10 43 4c 4f 53 45 5f 41 54 54 45 4d 50 54 20 33 0a CLOSE_ATTEMPT 3.
f9f20 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 43 6c static int winCl
f9f30 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ose(sqlite3_file
f9f40 20 2a 69 64 29 7b 0a 20 20 69 6e 74 20 72 63 2c *id){. int rc,
f9f50 20 63 6e 74 20 3d 20 30 3b 0a 20 20 77 69 6e 46 cnt = 0;. winF
f9f60 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 ile *pFile = (wi
f9f70 6e 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 73 nFile*)id;.. as
f9f80 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 sert( id!=0 );.
f9f90 20 4f 53 54 52 41 43 45 32 28 22 43 4c 4f 53 45 OSTRACE2("CLOSE
f9fa0 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 %d\n", pFile->h
f9fb0 29 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 72 63 20 );. do{. rc
f9fc0 3d 20 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46 = CloseHandle(pF
f9fd0 69 6c 65 2d 3e 68 29 3b 0a 20 20 7d 77 68 69 6c ile->h);. }whil
f9fe0 65 28 20 72 63 3d 3d 30 20 26 26 20 2b 2b 63 6e e( rc==0 && ++cn
f9ff0 74 20 3c 20 4d 58 5f 43 4c 4f 53 45 5f 41 54 54 t < MX_CLOSE_ATT
fa000 45 4d 50 54 20 26 26 20 28 53 6c 65 65 70 28 31 EMPT && (Sleep(1
fa010 30 30 29 2c 20 31 29 20 29 3b 0a 23 69 66 20 53 00), 1) );.#if S
fa020 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 23 QLITE_OS_WINCE.#
fa030 64 65 66 69 6e 65 20 57 49 4e 43 45 5f 44 45 4c define WINCE_DEL
fa040 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 20 33 ETION_ATTEMPTS 3
fa050 0a 20 20 77 69 6e 63 65 44 65 73 74 72 6f 79 4c . winceDestroyL
fa060 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 ock(pFile);. if
fa070 28 20 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 ( pFile->zDelete
fa080 4f 6e 43 6c 6f 73 65 20 29 7b 0a 20 20 20 20 69 OnClose ){. i
fa090 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 nt cnt = 0;.
fa0a0 77 68 69 6c 65 28 0a 20 20 20 20 20 20 20 20 20 while(.
fa0b0 20 20 44 65 6c 65 74 65 46 69 6c 65 57 28 70 46 DeleteFileW(pF
fa0c0 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c ile->zDeleteOnCl
fa0d0 6f 73 65 29 3d 3d 30 0a 20 20 20 20 20 20 20 20 ose)==0.
fa0e0 26 26 20 47 65 74 46 69 6c 65 41 74 74 72 69 62 && GetFileAttrib
fa0f0 75 74 65 73 57 28 70 46 69 6c 65 2d 3e 7a 44 65 utesW(pFile->zDe
fa100 6c 65 74 65 4f 6e 43 6c 6f 73 65 29 21 3d 30 78 leteOnClose)!=0x
fa110 66 66 66 66 66 66 66 66 20 0a 20 20 20 20 20 20 ffffffff .
fa120 20 20 26 26 20 63 6e 74 2b 2b 20 3c 20 57 49 4e && cnt++ < WIN
fa130 43 45 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 CE_DELETION_ATTE
fa140 4d 50 54 53 0a 20 20 20 20 29 7b 0a 20 20 20 20 MPTS. ){.
fa150 20 20 20 53 6c 65 65 70 28 31 30 30 29 3b 20 20 Sleep(100);
fa160 2f 2a 20 57 61 69 74 20 61 20 6c 69 74 74 6c 65 /* Wait a little
fa170 20 62 65 66 6f 72 65 20 74 72 79 69 6e 67 20 61 before trying a
fa180 67 61 69 6e 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 gain */. }.
fa190 20 20 66 72 65 65 28 70 46 69 6c 65 2d 3e 7a 44 free(pFile->zD
fa1a0 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 29 3b 0a 20 eleteOnClose);.
fa1b0 20 7d 0a 23 65 6e 64 69 66 0a 20 20 4f 70 65 6e }.#endif. Open
fa1c0 43 6f 75 6e 74 65 72 28 2d 31 29 3b 0a 20 20 72 Counter(-1);. r
fa1d0 65 74 75 72 6e 20 72 63 20 3f 20 53 51 4c 49 54 eturn rc ? SQLIT
fa1e0 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f E_OK : SQLITE_IO
fa1f0 45 52 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f ERR;.}../*.** So
fa200 6d 65 20 6d 69 63 72 6f 73 6f 66 74 20 63 6f 6d me microsoft com
fa210 70 69 6c 65 72 73 20 6c 61 63 6b 20 74 68 69 73 pilers lack this
fa220 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a definition..*/.
fa230 23 69 66 6e 64 65 66 20 49 4e 56 41 4c 49 44 5f #ifndef INVALID_
fa240 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52 SET_FILE_POINTER
fa250 0a 23 20 64 65 66 69 6e 65 20 49 4e 56 41 4c 49 .# define INVALI
fa260 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 D_SET_FILE_POINT
fa270 45 52 20 28 28 44 57 4f 52 44 29 2d 31 29 0a 23 ER ((DWORD)-1).#
fa280 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 endif../*.** Rea
fa290 64 20 64 61 74 61 20 66 72 6f 6d 20 61 20 66 69 d data from a fi
fa2a0 6c 65 20 69 6e 74 6f 20 61 20 62 75 66 66 65 72 le into a buffer
fa2b0 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 . Return SQLITE
fa2c0 5f 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20 62 79 _OK if all.** by
fa2d0 74 65 73 20 77 65 72 65 20 72 65 61 64 20 73 75 tes were read su
fa2e0 63 63 65 73 73 66 75 6c 6c 79 20 61 6e 64 20 53 ccessfully and S
fa2f0 51 4c 49 54 45 5f 49 4f 45 52 52 20 69 66 20 61 QLITE_IOERR if a
fa300 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 nything goes.**
fa310 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 wrong..*/.static
fa320 20 69 6e 74 20 77 69 6e 52 65 61 64 28 0a 20 20 int winRead(.
fa330 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
fa340 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 , /* Fi
fa350 6c 65 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 le to read from
fa360 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 66 2c */. void *pBuf,
fa370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fa380 2f 2a 20 57 72 69 74 65 20 63 6f 6e 74 65 6e 74 /* Write content
fa390 20 69 6e 74 6f 20 74 68 69 73 20 62 75 66 66 65 into this buffe
fa3a0 72 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c 20 r */. int amt,
fa3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fa3c0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
fa3d0 79 74 65 73 20 74 6f 20 72 65 61 64 20 2a 2f 0a ytes to read */.
fa3e0 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 sqlite3_int64
fa3f0 6f 66 66 73 65 74 20 20 20 20 20 20 20 2f 2a 20 offset /*
fa400 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20 61 74 Begin reading at
fa410 20 74 68 69 73 20 6f 66 66 73 65 74 20 2a 2f 0a this offset */.
fa420 29 7b 0a 20 20 4c 4f 4e 47 20 75 70 70 65 72 42 ){. LONG upperB
fa430 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 28 6f 66 its = (LONG)((of
fa440 66 73 65 74 3e 3e 33 32 29 20 26 20 30 78 37 66 fset>>32) & 0x7f
fa450 66 66 66 66 66 66 29 3b 0a 20 20 4c 4f 4e 47 20 ffffff);. LONG
fa460 6c 6f 77 65 72 42 69 74 73 20 3d 20 28 4c 4f 4e lowerBits = (LON
fa470 47 29 28 6f 66 66 73 65 74 20 26 20 30 78 66 66 G)(offset & 0xff
fa480 66 66 66 66 66 66 29 3b 0a 20 20 44 57 4f 52 44 ffffff);. DWORD
fa490 20 72 63 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a rc;. winFile *
fa4a0 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 pFile = (winFile
fa4b0 2a 29 69 64 3b 0a 20 20 44 57 4f 52 44 20 65 72 *)id;. DWORD er
fa4c0 72 6f 72 3b 0a 20 20 44 57 4f 52 44 20 67 6f 74 ror;. DWORD got
fa4d0 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 ;.. assert( id!
fa4e0 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 =0 );. Simulate
fa4f0 49 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 IOError(return S
fa500 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 QLITE_IOERR_READ
fa510 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 22 52 );. OSTRACE3("R
fa520 45 41 44 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e EAD %d lock=%d\n
fa530 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 ", pFile->h, pFi
fa540 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 le->locktype);.
fa550 20 72 63 20 3d 20 53 65 74 46 69 6c 65 50 6f 69 rc = SetFilePoi
fa560 6e 74 65 72 28 70 46 69 6c 65 2d 3e 68 2c 20 6c nter(pFile->h, l
fa570 6f 77 65 72 42 69 74 73 2c 20 26 75 70 70 65 72 owerBits, &upper
fa580 42 69 74 73 2c 20 46 49 4c 45 5f 42 45 47 49 4e Bits, FILE_BEGIN
fa590 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 49 4e 56 );. if( rc==INV
fa5a0 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f ALID_SET_FILE_PO
fa5b0 49 4e 54 45 52 20 26 26 20 28 65 72 72 6f 72 3d INTER && (error=
fa5c0 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29 21 GetLastError())!
fa5d0 3d 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 =NO_ERROR ){.
fa5e0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
fa5f0 6f 20 3d 20 65 72 72 6f 72 3b 0a 20 20 20 20 72 o = error;. r
fa600 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c eturn SQLITE_FUL
fa610 4c 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 52 65 L;. }. if( !Re
fa620 61 64 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c adFile(pFile->h,
fa630 20 70 42 75 66 2c 20 61 6d 74 2c 20 26 67 6f 74 pBuf, amt, &got
fa640 2c 20 30 29 20 29 7b 0a 20 20 20 20 70 46 69 6c , 0) ){. pFil
fa650 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 e->lastErrno = G
fa660 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 etLastError();.
fa670 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
fa680 5f 49 4f 45 52 52 5f 52 45 41 44 3b 0a 20 20 7d _IOERR_READ;. }
fa690 0a 20 20 69 66 28 20 67 6f 74 3d 3d 28 44 57 4f . if( got==(DWO
fa6a0 52 44 29 61 6d 74 20 29 7b 0a 20 20 20 20 72 65 RD)amt ){. re
fa6b0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
fa6c0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
fa6d0 55 6e 72 65 61 64 20 70 61 72 74 73 20 6f 66 20 Unread parts of
fa6e0 74 68 65 20 62 75 66 66 65 72 20 6d 75 73 74 20 the buffer must
fa6f0 62 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a be zero-filled *
fa700 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 /. memset(&((
fa710 63 68 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d char*)pBuf)[got]
fa720 2c 20 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 , 0, amt-got);.
fa730 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
fa740 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 _IOERR_SHORT_REA
fa750 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 D;. }.}../*.**
fa760 57 72 69 74 65 20 64 61 74 61 20 66 72 6f 6d 20 Write data from
fa770 61 20 62 75 66 66 65 72 20 69 6e 74 6f 20 61 20 a buffer into a
fa780 66 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 51 file. Return SQ
fa790 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 LITE_OK on succe
fa7a0 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f 74 ss.** or some ot
fa7b0 68 65 72 20 65 72 72 6f 72 20 63 6f 64 65 20 6f her error code o
fa7c0 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 n failure..*/.st
fa7d0 61 74 69 63 20 69 6e 74 20 77 69 6e 57 72 69 74 atic int winWrit
fa7e0 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c e(. sqlite3_fil
fa7f0 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 2f e *id, /
fa800 2a 20 46 69 6c 65 20 74 6f 20 77 72 69 74 65 20 * File to write
fa810 69 6e 74 6f 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 into */. const
fa820 76 6f 69 64 20 2a 70 42 75 66 2c 20 20 20 20 20 void *pBuf,
fa830 20 20 20 20 2f 2a 20 54 68 65 20 62 79 74 65 73 /* The bytes
fa840 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 2a to be written *
fa850 2f 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 /. int amt,
fa860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
fa870 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
fa880 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 73 to write */. s
fa890 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 qlite3_int64 off
fa8a0 73 65 74 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 set /* Offs
fa8b0 65 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 et into the file
fa8c0 20 74 6f 20 62 65 67 69 6e 20 77 72 69 74 69 6e to begin writin
fa8d0 67 20 61 74 20 2a 2f 0a 29 7b 0a 20 20 4c 4f 4e g at */.){. LON
fa8e0 47 20 75 70 70 65 72 42 69 74 73 20 3d 20 28 4c G upperBits = (L
fa8f0 4f 4e 47 29 28 28 6f 66 66 73 65 74 3e 3e 33 32 ONG)((offset>>32
fa900 29 20 26 20 30 78 37 66 66 66 66 66 66 66 29 3b ) & 0x7fffffff);
fa910 0a 20 20 4c 4f 4e 47 20 6c 6f 77 65 72 42 69 74 . LONG lowerBit
fa920 73 20 3d 20 28 4c 4f 4e 47 29 28 6f 66 66 73 65 s = (LONG)(offse
fa930 74 20 26 20 30 78 66 66 66 66 66 66 66 66 29 3b t & 0xffffffff);
fa940 0a 20 20 44 57 4f 52 44 20 72 63 3b 0a 20 20 77 . DWORD rc;. w
fa950 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 inFile *pFile =
fa960 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 (winFile*)id;.
fa970 44 57 4f 52 44 20 65 72 72 6f 72 3b 0a 20 20 44 DWORD error;. D
fa980 57 4f 52 44 20 77 72 6f 74 65 20 3d 20 30 3b 0a WORD wrote = 0;.
fa990 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 . assert( id!=0
fa9a0 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f );. SimulateIO
fa9b0 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c Error(return SQL
fa9c0 49 54 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 29 ITE_IOERR_WRITE)
fa9d0 3b 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b ;. SimulateDisk
fa9e0 66 75 6c 6c 45 72 72 6f 72 28 72 65 74 75 72 6e fullError(return
fa9f0 20 53 51 4c 49 54 45 5f 46 55 4c 4c 29 3b 0a 20 SQLITE_FULL);.
faa00 20 4f 53 54 52 41 43 45 33 28 22 57 52 49 54 45 OSTRACE3("WRITE
faa10 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 %d lock=%d\n",
faa20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d pFile->h, pFile-
faa30 3e 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 72 63 >locktype);. rc
faa40 20 3d 20 53 65 74 46 69 6c 65 50 6f 69 6e 74 65 = SetFilePointe
faa50 72 28 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 77 65 r(pFile->h, lowe
faa60 72 42 69 74 73 2c 20 26 75 70 70 65 72 42 69 74 rBits, &upperBit
faa70 73 2c 20 46 49 4c 45 5f 42 45 47 49 4e 29 3b 0a s, FILE_BEGIN);.
faa80 20 20 69 66 28 20 72 63 3d 3d 49 4e 56 41 4c 49 if( rc==INVALI
faa90 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 D_SET_FILE_POINT
faaa0 45 52 20 26 26 20 28 65 72 72 6f 72 3d 47 65 74 ER && (error=Get
faab0 4c 61 73 74 45 72 72 6f 72 28 29 29 21 3d 4e 4f LastError())!=NO
faac0 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 70 46 _ERROR ){. pF
faad0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
faae0 20 65 72 72 6f 72 3b 0a 20 20 20 20 72 65 74 75 error;. retu
faaf0 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a rn SQLITE_FULL;.
fab00 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 61 6d }. assert( am
fab10 74 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65 28 0a t>0 );. while(.
fab20 20 20 20 20 20 61 6d 74 3e 30 0a 20 20 20 20 20 amt>0.
fab30 26 26 20 28 72 63 20 3d 20 57 72 69 74 65 46 69 && (rc = WriteFi
fab40 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 70 42 75 le(pFile->h, pBu
fab50 66 2c 20 61 6d 74 2c 20 26 77 72 6f 74 65 2c 20 f, amt, &wrote,
fab60 30 29 29 21 3d 30 0a 20 20 20 20 20 26 26 20 77 0))!=0. && w
fab70 72 6f 74 65 3e 30 0a 20 20 29 7b 0a 20 20 20 20 rote>0. ){.
fab80 61 6d 74 20 2d 3d 20 77 72 6f 74 65 3b 0a 20 20 amt -= wrote;.
fab90 20 20 70 42 75 66 20 3d 20 26 28 28 63 68 61 72 pBuf = &((char
faba0 2a 29 70 42 75 66 29 5b 77 72 6f 74 65 5d 3b 0a *)pBuf)[wrote];.
fabb0 20 20 7d 0a 20 20 69 66 28 20 21 72 63 20 7c 7c }. if( !rc ||
fabc0 20 61 6d 74 3e 28 69 6e 74 29 77 72 6f 74 65 20 amt>(int)wrote
fabd0 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 ){. pFile->la
fabe0 73 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 stErrno = GetLas
fabf0 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 72 65 tError();. re
fac00 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c turn SQLITE_FULL
fac10 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 ;. }. return S
fac20 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
fac30 2a 2a 20 54 72 75 6e 63 61 74 65 20 61 6e 20 6f ** Truncate an o
fac40 70 65 6e 20 66 69 6c 65 20 74 6f 20 61 20 73 70 pen file to a sp
fac50 65 63 69 66 69 65 64 20 73 69 7a 65 0a 2a 2f 0a ecified size.*/.
fac60 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 54 72 static int winTr
fac70 75 6e 63 61 74 65 28 73 71 6c 69 74 65 33 5f 66 uncate(sqlite3_f
fac80 69 6c 65 20 2a 69 64 2c 20 73 71 6c 69 74 65 33 ile *id, sqlite3
fac90 5f 69 6e 74 36 34 20 6e 42 79 74 65 29 7b 0a 20 _int64 nByte){.
faca0 20 4c 4f 4e 47 20 75 70 70 65 72 42 69 74 73 20 LONG upperBits
facb0 3d 20 28 4c 4f 4e 47 29 28 28 6e 42 79 74 65 3e = (LONG)((nByte>
facc0 3e 33 32 29 20 26 20 30 78 37 66 66 66 66 66 66 >32) & 0x7ffffff
facd0 66 29 3b 0a 20 20 4c 4f 4e 47 20 6c 6f 77 65 72 f);. LONG lower
face0 42 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 6e 42 Bits = (LONG)(nB
facf0 79 74 65 20 26 20 30 78 66 66 66 66 66 66 66 66 yte & 0xffffffff
fad00 29 3b 0a 20 20 44 57 4f 52 44 20 72 63 3b 0a 20 );. DWORD rc;.
fad10 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 winFile *pFile
fad20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a = (winFile*)id;.
fad30 20 20 44 57 4f 52 44 20 65 72 72 6f 72 3b 0a 0a DWORD error;..
fad40 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 assert( id!=0
fad50 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 22 54 );. OSTRACE3("T
fad60 52 55 4e 43 41 54 45 20 25 64 20 25 6c 6c 64 5c RUNCATE %d %lld\
fad70 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6e 42 n", pFile->h, nB
fad80 79 74 65 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 yte);. Simulate
fad90 49 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 IOError(return S
fada0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e QLITE_IOERR_TRUN
fadb0 43 41 54 45 29 3b 0a 20 20 72 63 20 3d 20 53 65 CATE);. rc = Se
fadc0 74 46 69 6c 65 50 6f 69 6e 74 65 72 28 70 46 69 tFilePointer(pFi
fadd0 6c 65 2d 3e 68 2c 20 6c 6f 77 65 72 42 69 74 73 le->h, lowerBits
fade0 2c 20 26 75 70 70 65 72 42 69 74 73 2c 20 46 49 , &upperBits, FI
fadf0 4c 45 5f 42 45 47 49 4e 29 3b 0a 20 20 69 66 28 LE_BEGIN);. if(
fae00 20 72 63 3d 3d 49 4e 56 41 4c 49 44 5f 53 45 54 rc==INVALID_SET
fae10 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 26 26 _FILE_POINTER &&
fae20 20 28 65 72 72 6f 72 3d 47 65 74 4c 61 73 74 45 (error=GetLastE
fae30 72 72 6f 72 28 29 29 21 3d 4e 4f 5f 45 52 52 4f rror())!=NO_ERRO
fae40 52 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e R ){. pFile->
fae50 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6f lastErrno = erro
fae60 72 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 r;. return SQ
fae70 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 LITE_IOERR_TRUNC
fae80 41 54 45 3b 0a 20 20 7d 0a 20 20 2f 2a 20 53 65 ATE;. }. /* Se
fae90 74 45 6e 64 4f 66 46 69 6c 65 20 77 69 6c 6c 20 tEndOfFile will
faea0 66 61 69 6c 20 69 66 20 6e 42 79 74 65 20 69 73 fail if nByte is
faeb0 20 6e 65 67 61 74 69 76 65 20 2a 2f 0a 20 20 69 negative */. i
faec0 66 28 20 21 53 65 74 45 6e 64 4f 66 46 69 6c 65 f( !SetEndOfFile
faed0 28 70 46 69 6c 65 2d 3e 68 29 20 29 7b 0a 20 20 (pFile->h) ){.
faee0 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
faef0 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f no = GetLastErro
faf00 72 28 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 r();. return
faf10 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 SQLITE_IOERR_TRU
faf20 4e 43 41 54 45 3b 0a 20 20 7d 0a 20 20 72 65 74 NCATE;. }. ret
faf30 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
faf40 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
faf50 54 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 TEST./*.** Count
faf60 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 the number of f
faf70 75 6c 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72 ullsyncs and nor
faf80 6d 61 6c 20 73 79 6e 63 73 2e 20 20 54 68 69 73 mal syncs. This
faf90 20 69 73 20 75 73 65 64 20 74 6f 20 74 65 73 74 is used to test
fafa0 0a 2a 2a 20 74 68 61 74 20 73 79 6e 63 73 20 61 .** that syncs a
fafb0 6e 64 20 66 75 6c 6c 73 79 6e 63 73 20 61 72 65 nd fullsyncs are
fafc0 20 6f 63 63 75 72 69 6e 67 20 61 74 20 74 68 65 occuring at the
fafd0 20 72 69 67 68 74 20 74 69 6d 65 73 2e 0a 2a 2f right times..*/
fafe0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
faff0 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 sqlite3_sync_cou
fb000 6e 74 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 nt = 0;.SQLITE_A
fb010 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 PI int sqlite3_f
fb020 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 ullsync_count =
fb030 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 0;.#endif../*.**
fb040 20 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 77 Make sure all w
fb050 72 69 74 65 73 20 74 6f 20 61 20 70 61 72 74 69 rites to a parti
fb060 63 75 6c 61 72 20 66 69 6c 65 20 61 72 65 20 63 cular file are c
fb070 6f 6d 6d 69 74 74 65 64 20 74 6f 20 64 69 73 6b ommitted to disk
fb080 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
fb090 77 69 6e 53 79 6e 63 28 73 71 6c 69 74 65 33 5f winSync(sqlite3_
fb0a0 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c file *id, int fl
fb0b0 61 67 73 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 ags){.#ifndef SQ
fb0c0 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 77 LITE_NO_SYNC. w
fb0d0 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 inFile *pFile =
fb0e0 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 (winFile*)id;..
fb0f0 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 assert( id!=0 )
fb100 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 22 53 59 ;. OSTRACE3("SY
fb110 4e 43 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 NC %d lock=%d\n"
fb120 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c , pFile->h, pFil
fb130 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 3b 0a 23 65 e->locktype);.#e
fb140 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 lse. UNUSED_PAR
fb150 41 4d 45 54 45 52 28 69 64 29 3b 0a 23 65 6e 64 AMETER(id);.#end
fb160 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 if.#ifndef SQLIT
fb170 45 5f 54 45 53 54 0a 20 20 55 4e 55 53 45 44 5f E_TEST. UNUSED_
fb180 50 41 52 41 4d 45 54 45 52 28 66 6c 61 67 73 29 PARAMETER(flags)
fb190 3b 0a 23 65 6c 73 65 0a 20 20 69 66 28 20 66 6c ;.#else. if( fl
fb1a0 61 67 73 20 26 20 53 51 4c 49 54 45 5f 53 59 4e ags & SQLITE_SYN
fb1b0 43 5f 46 55 4c 4c 20 29 7b 0a 20 20 20 20 73 71 C_FULL ){. sq
fb1c0 6c 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 lite3_fullsync_c
fb1d0 6f 75 6e 74 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71 ount++;. }. sq
fb1e0 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 lite3_sync_count
fb1f0 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 2f 2a 20 ++;.#endif. /*
fb200 49 66 20 77 65 20 63 6f 6d 70 69 6c 65 64 20 77 If we compiled w
fb210 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 4e ith the SQLITE_N
fb220 4f 5f 53 59 4e 43 20 66 6c 61 67 2c 20 74 68 65 O_SYNC flag, the
fb230 6e 20 73 79 6e 63 69 6e 67 20 69 73 20 61 0a 20 n syncing is a.
fb240 20 2a 2a 20 6e 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 ** no-op. */.#
fb250 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f ifdef SQLITE_NO_
fb260 53 59 4e 43 0a 20 20 20 20 72 65 74 75 72 6e 20 SYNC. return
fb270 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6c 73 65 SQLITE_OK;.#else
fb280 0a 20 20 69 66 28 20 46 6c 75 73 68 46 69 6c 65 . if( FlushFile
fb290 42 75 66 66 65 72 73 28 70 46 69 6c 65 2d 3e 68 Buffers(pFile->h
fb2a0 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ) ){. return
fb2b0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c SQLITE_OK;. }el
fb2c0 73 65 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c se{. pFile->l
fb2d0 61 73 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 astErrno = GetLa
fb2e0 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 72 stError();. r
fb2f0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
fb300 52 52 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d RR;. }.#endif.}
fb310 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e ../*.** Determin
fb320 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 69 e the current si
fb330 7a 65 20 6f 66 20 61 20 66 69 6c 65 20 69 6e 20 ze of a file in
fb340 62 79 74 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20 bytes.*/.static
fb350 69 6e 74 20 77 69 6e 46 69 6c 65 53 69 7a 65 28 int winFileSize(
fb360 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
fb370 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 , sqlite3_int64
fb380 2a 70 53 69 7a 65 29 7b 0a 20 20 44 57 4f 52 44 *pSize){. DWORD
fb390 20 75 70 70 65 72 42 69 74 73 3b 0a 20 20 44 57 upperBits;. DW
fb3a0 4f 52 44 20 6c 6f 77 65 72 42 69 74 73 3b 0a 20 ORD lowerBits;.
fb3b0 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 winFile *pFile
fb3c0 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a = (winFile*)id;.
fb3d0 20 20 44 57 4f 52 44 20 65 72 72 6f 72 3b 0a 0a DWORD error;..
fb3e0 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 assert( id!=0
fb3f0 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 );. SimulateIOE
fb400 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 rror(return SQLI
fb410 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 29 3b TE_IOERR_FSTAT);
fb420 0a 20 20 6c 6f 77 65 72 42 69 74 73 20 3d 20 47 . lowerBits = G
fb430 65 74 46 69 6c 65 53 69 7a 65 28 70 46 69 6c 65 etFileSize(pFile
fb440 2d 3e 68 2c 20 26 75 70 70 65 72 42 69 74 73 29 ->h, &upperBits)
fb450 3b 0a 20 20 69 66 28 20 20 20 28 6c 6f 77 65 72 ;. if( (lower
fb460 42 69 74 73 20 3d 3d 20 49 4e 56 41 4c 49 44 5f Bits == INVALID_
fb470 46 49 4c 45 5f 53 49 5a 45 29 0a 20 20 20 20 20 FILE_SIZE).
fb480 26 26 20 28 28 65 72 72 6f 72 20 3d 20 47 65 74 && ((error = Get
fb490 4c 61 73 74 45 72 72 6f 72 28 29 29 20 21 3d 20 LastError()) !=
fb4a0 4e 4f 5f 45 52 52 4f 52 29 20 29 0a 20 20 7b 0a NO_ERROR) ). {.
fb4b0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 pFile->lastE
fb4c0 72 72 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a 20 20 rrno = error;.
fb4d0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
fb4e0 49 4f 45 52 52 5f 46 53 54 41 54 3b 0a 20 20 7d IOERR_FSTAT;. }
fb4f0 0a 20 20 2a 70 53 69 7a 65 20 3d 20 28 28 28 73 . *pSize = (((s
fb500 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 75 70 70 qlite3_int64)upp
fb510 65 72 42 69 74 73 29 3c 3c 33 32 29 20 2b 20 6c erBits)<<32) + l
fb520 6f 77 65 72 42 69 74 73 3b 0a 20 20 72 65 74 75 owerBits;. retu
fb530 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
fb540 0a 2f 2a 0a 2a 2a 20 4c 4f 43 4b 46 49 4c 45 5f ./*.** LOCKFILE_
fb550 46 41 49 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59 FAIL_IMMEDIATELY
fb560 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 6f 6e is undefined on
fb570 20 73 6f 6d 65 20 57 69 6e 64 6f 77 73 20 73 79 some Windows sy
fb580 73 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 stems..*/.#ifnde
fb590 66 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f f LOCKFILE_FAIL_
fb5a0 49 4d 4d 45 44 49 41 54 45 4c 59 0a 23 20 64 65 IMMEDIATELY.# de
fb5b0 66 69 6e 65 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 fine LOCKFILE_FA
fb5c0 49 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59 20 31 IL_IMMEDIATELY 1
fb5d0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 .#endif../*.** A
fb5e0 63 71 75 69 72 65 20 61 20 72 65 61 64 65 72 20 cquire a reader
fb5f0 6c 6f 63 6b 2e 0a 2a 2a 20 44 69 66 66 65 72 65 lock..** Differe
fb600 6e 74 20 41 50 49 20 72 6f 75 74 69 6e 65 73 20 nt API routines
fb610 61 72 65 20 63 61 6c 6c 65 64 20 64 65 70 65 6e are called depen
fb620 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 72 20 ding on whether
fb630 6f 72 20 6e 6f 74 20 74 68 69 73 0a 2a 2a 20 69 or not this.** i
fb640 73 20 57 69 6e 39 35 20 6f 72 20 57 69 6e 4e 54 s Win95 or WinNT
fb650 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
fb660 67 65 74 52 65 61 64 4c 6f 63 6b 28 77 69 6e 46 getReadLock(winF
fb670 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 ile *pFile){. i
fb680 6e 74 20 72 65 73 3b 0a 20 20 69 66 28 20 69 73 nt res;. if( is
fb690 4e 54 28 29 20 29 7b 0a 20 20 20 20 4f 56 45 52 NT() ){. OVER
fb6a0 4c 41 50 50 45 44 20 6f 76 6c 70 3b 0a 20 20 20 LAPPED ovlp;.
fb6b0 20 6f 76 6c 70 2e 4f 66 66 73 65 74 20 3d 20 53 ovlp.Offset = S
fb6c0 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 HARED_FIRST;.
fb6d0 20 6f 76 6c 70 2e 4f 66 66 73 65 74 48 69 67 68 ovlp.OffsetHigh
fb6e0 20 3d 20 30 3b 0a 20 20 20 20 6f 76 6c 70 2e 68 = 0;. ovlp.h
fb6f0 45 76 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20 72 Event = 0;. r
fb700 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 45 78 28 es = LockFileEx(
fb710 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 46 49 pFile->h, LOCKFI
fb720 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 49 41 54 LE_FAIL_IMMEDIAT
fb730 45 4c 59 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ELY,.
fb740 20 20 20 20 20 20 20 20 20 20 30 2c 20 53 48 41 0, SHA
fb750 52 45 44 5f 53 49 5a 45 2c 20 30 2c 20 26 6f 76 RED_SIZE, 0, &ov
fb760 6c 70 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 lp);./* isNT() i
fb770 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 s 1 if SQLITE_OS
fb780 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 _WINCE==1, so th
fb790 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 is else is never
fb7a0 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2f 0a 23 executed. .*/.#
fb7b0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
fb7c0 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 CE==0. }else{.
fb7d0 20 20 20 69 6e 74 20 6c 6b 3b 0a 20 20 20 20 73 int lk;. s
fb7e0 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 qlite3_randomnes
fb7f0 73 28 73 69 7a 65 6f 66 28 6c 6b 29 2c 20 26 6c s(sizeof(lk), &l
fb800 6b 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 73 k);. pFile->s
fb810 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 20 3d 20 haredLockByte =
fb820 28 73 68 6f 72 74 29 28 28 6c 6b 20 26 20 30 78 (short)((lk & 0x
fb830 37 66 66 66 66 66 66 66 29 25 28 53 48 41 52 45 7fffffff)%(SHARE
fb840 44 5f 53 49 5a 45 20 2d 20 31 29 29 3b 0a 20 20 D_SIZE - 1));.
fb850 20 20 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 res = LockFile
fb860 28 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45 (pFile->h, SHARE
fb870 44 5f 46 49 52 53 54 2b 70 46 69 6c 65 2d 3e 73 D_FIRST+pFile->s
fb880 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 30 haredLockByte, 0
fb890 2c 20 31 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a , 1, 0);.#endif.
fb8a0 20 20 7d 0a 20 20 69 66 28 20 72 65 73 20 3d 3d }. if( res ==
fb8b0 20 30 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 0 ){. pFile-
fb8c0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 65 74 >lastErrno = Get
fb8d0 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 7d LastError();. }
fb8e0 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d . return res;.}
fb8f0 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 61 20 72 ../*.** Undo a r
fb900 65 61 64 6c 6f 63 6b 0a 2a 2f 0a 73 74 61 74 69 eadlock.*/.stati
fb910 63 20 69 6e 74 20 75 6e 6c 6f 63 6b 52 65 61 64 c int unlockRead
fb920 4c 6f 63 6b 28 77 69 6e 46 69 6c 65 20 2a 70 46 Lock(winFile *pF
fb930 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 65 73 3b ile){. int res;
fb940 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b . if( isNT() ){
fb950 0a 20 20 20 20 72 65 73 20 3d 20 55 6e 6c 6f 63 . res = Unloc
fb960 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 kFile(pFile->h,
fb970 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20 30 2c SHARED_FIRST, 0,
fb980 20 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 29 SHARED_SIZE, 0)
fb990 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 ;./* isNT() is 1
fb9a0 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 if SQLITE_OS_WI
fb9b0 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 NCE==1, so this
fb9c0 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 else is never ex
fb9d0 65 63 75 74 65 64 2e 20 0a 2a 2f 0a 23 69 66 20 ecuted. .*/.#if
fb9e0 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d SQLITE_OS_WINCE=
fb9f0 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 =0. }else{.
fba00 72 65 73 20 3d 20 55 6e 6c 6f 63 6b 46 69 6c 65 res = UnlockFile
fba10 28 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45 (pFile->h, SHARE
fba20 44 5f 46 49 52 53 54 20 2b 20 70 46 69 6c 65 2d D_FIRST + pFile-
fba30 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 2c >sharedLockByte,
fba40 20 30 2c 20 31 2c 20 30 29 3b 0a 23 65 6e 64 69 0, 1, 0);.#endi
fba50 66 0a 20 20 7d 0a 20 20 69 66 28 20 72 65 73 20 f. }. if( res
fba60 3d 3d 20 30 20 29 7b 0a 20 20 20 20 70 46 69 6c == 0 ){. pFil
fba70 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 e->lastErrno = G
fba80 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 etLastError();.
fba90 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b }. return res;
fbaa0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 .}../*.** Lock t
fbab0 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 he file with the
fbac0 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 lock specified
fbad0 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 by parameter loc
fbae0 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f ktype - one.** o
fbaf0 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a f the following:
fbb00 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 .**.** (1) S
fbb10 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 HARED_LOCK.**
fbb20 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c (2) RESERVED_L
fbb30 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 OCK.** (3) P
fbb40 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 ENDING_LOCK.**
fbb50 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 (4) EXCLUSIVE
fbb60 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 _LOCK.**.** Some
fbb70 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 times when reque
fbb80 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 sting one lock s
fbb90 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c tate, additional
fbba0 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 lock states.**
fbbb0 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 are inserted in
fbbc0 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f between. The lo
fbbd0 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c cking might fail
fbbe0 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c on one of the l
fbbf0 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 ater.** transiti
fbc00 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 ons leaving the
fbc10 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 lock state diffe
fbc20 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 rent from what i
fbc30 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a t started but.**
fbc40 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 still short of
fbc50 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 its goal. The f
fbc60 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 ollowing chart s
fbc70 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 hows the allowed
fbc80 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 .** transitions
fbc90 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 and the inserted
fbca0 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 intermediate st
fbcb0 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 ates:.**.** U
fbcc0 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 NLOCKED -> SHARE
fbcd0 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d D.** SHARED -
fbce0 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 > RESERVED.**
fbcf0 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 SHARED -> (PEND
fbd00 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 ING) -> EXCLUSIV
fbd10 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 E.** RESERVED
fbd20 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e -> (PENDING) ->
fbd30 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 EXCLUSIVE.**
fbd40 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c PENDING -> EXCL
fbd50 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 USIVE.**.** This
fbd60 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e routine will on
fbd70 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f ly increase a lo
fbd80 63 6b 2e 20 20 54 68 65 20 77 69 6e 55 6e 6c 6f ck. The winUnlo
fbd90 63 6b 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 ck() routine.**
fbda0 65 72 61 73 65 73 20 61 6c 6c 20 6c 6f 63 6b 73 erases all locks
fbdb0 20 61 74 20 6f 6e 63 65 20 61 6e 64 20 72 65 74 at once and ret
fbdc0 75 72 6e 73 20 75 73 20 69 6d 6d 65 64 69 61 74 urns us immediat
fbdd0 65 6c 79 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c ely to locking l
fbde0 65 76 65 6c 20 30 2e 0a 2a 2a 20 49 74 20 69 73 evel 0..** It is
fbdf0 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f not possible to
fbe00 20 6c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 lower the locki
fbe10 6e 67 20 6c 65 76 65 6c 20 6f 6e 65 20 73 74 65 ng level one ste
fbe20 70 20 61 74 20 61 20 74 69 6d 65 2e 20 20 59 6f p at a time. Yo
fbe30 75 0a 2a 2a 20 6d 75 73 74 20 67 6f 20 73 74 72 u.** must go str
fbe40 61 69 67 68 74 20 74 6f 20 6c 6f 63 6b 69 6e 67 aight to locking
fbe50 20 6c 65 76 65 6c 20 30 2e 0a 2a 2f 0a 73 74 61 level 0..*/.sta
fbe60 74 69 63 20 69 6e 74 20 77 69 6e 4c 6f 63 6b 28 tic int winLock(
fbe70 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
fbe80 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b , int locktype){
fbe90 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
fbea0 54 45 5f 4f 4b 3b 20 20 20 20 2f 2a 20 52 65 74 TE_OK; /* Ret
fbeb0 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 75 urn code from su
fbec0 62 72 6f 75 74 69 6e 65 73 20 2a 2f 0a 20 20 69 broutines */. i
fbed0 6e 74 20 72 65 73 20 3d 20 31 3b 20 20 20 20 20 nt res = 1;
fbee0 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 /* Result
fbef0 6f 66 20 61 20 77 69 6e 64 6f 77 73 20 6c 6f 63 of a windows loc
fbf00 6b 20 63 61 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 k call */. int
fbf10 6e 65 77 4c 6f 63 6b 74 79 70 65 3b 20 20 20 20 newLocktype;
fbf20 20 20 20 2f 2a 20 53 65 74 20 70 46 69 6c 65 2d /* Set pFile-
fbf30 3e 6c 6f 63 6b 74 79 70 65 20 74 6f 20 74 68 69 >locktype to thi
fbf40 73 20 76 61 6c 75 65 20 62 65 66 6f 72 65 20 65 s value before e
fbf50 78 69 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 xiting */. int
fbf60 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d gotPendingLock =
fbf70 20 30 3b 2f 2a 20 54 72 75 65 20 69 66 20 77 65 0;/* True if we
fbf80 20 61 63 71 75 69 72 65 64 20 61 20 50 45 4e 44 acquired a PEND
fbf90 49 4e 47 20 6c 6f 63 6b 20 74 68 69 73 20 74 69 ING lock this ti
fbfa0 6d 65 20 2a 2f 0a 20 20 77 69 6e 46 69 6c 65 20 me */. winFile
fbfb0 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c *pFile = (winFil
fbfc0 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52 44 20 65 e*)id;. DWORD e
fbfd0 72 72 6f 72 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b rror = NO_ERROR;
fbfe0 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d .. assert( id!=
fbff0 30 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 0 );. OSTRACE5(
fc000 22 4c 4f 43 4b 20 25 64 20 25 64 20 77 61 73 20 "LOCK %d %d was
fc010 25 64 28 25 64 29 5c 6e 22 2c 0a 20 20 20 20 20 %d(%d)\n",.
fc020 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 2c 20 6c pFile->h, l
fc030 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e ocktype, pFile->
fc040 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d locktype, pFile-
fc050 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 29 >sharedLockByte)
fc060 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 ;.. /* If there
fc070 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 6c 6f is already a lo
fc080 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70 65 20 ck of this type
fc090 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69 63 74 or more restrict
fc0a0 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 ive on the. **
fc0b0 4f 73 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 68 69 OsFile, do nothi
fc0c0 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65 20 74 68 ng. Don't use th
fc0d0 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78 69 74 e end_lock: exit
fc0e0 20 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a 20 73 path, as. ** s
fc0f0 71 6c 69 74 65 33 4f 73 45 6e 74 65 72 4d 75 74 qlite3OsEnterMut
fc100 65 78 28 29 20 68 61 73 6e 27 74 20 62 65 65 6e ex() hasn't been
fc110 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a 20 20 2a called yet.. *
fc120 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c /. if( pFile->l
fc130 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74 79 70 ocktype>=locktyp
fc140 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 e ){. return
fc150 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a SQLITE_OK;. }..
fc160 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 /* Make sure t
fc170 68 65 20 6c 6f 63 6b 69 6e 67 20 73 65 71 75 65 he locking seque
fc180 6e 63 65 20 69 73 20 63 6f 72 72 65 63 74 0a 20 nce is correct.
fc190 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 46 */. assert( pF
fc1a0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e ile->locktype!=N
fc1b0 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 O_LOCK || lockty
fc1c0 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 pe==SHARED_LOCK
fc1d0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 );. assert( loc
fc1e0 6b 74 79 70 65 21 3d 50 45 4e 44 49 4e 47 5f 4c ktype!=PENDING_L
fc1f0 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 OCK );. assert(
fc200 20 6c 6f 63 6b 74 79 70 65 21 3d 52 45 53 45 52 locktype!=RESER
fc210 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 46 69 6c VED_LOCK || pFil
fc220 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 e->locktype==SHA
fc230 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f RED_LOCK );.. /
fc240 2a 20 4c 6f 63 6b 20 74 68 65 20 50 45 4e 44 49 * Lock the PENDI
fc250 4e 47 5f 4c 4f 43 4b 20 62 79 74 65 20 69 66 20 NG_LOCK byte if
fc260 77 65 20 6e 65 65 64 20 74 6f 20 61 63 71 75 69 we need to acqui
fc270 72 65 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 re a PENDING loc
fc280 6b 20 6f 72 0a 20 20 2a 2a 20 61 20 53 48 41 52 k or. ** a SHAR
fc290 45 44 20 6c 6f 63 6b 2e 20 20 49 66 20 77 65 20 ED lock. If we
fc2a0 61 72 65 20 61 63 71 75 69 72 69 6e 67 20 61 20 are acquiring a
fc2b0 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68 65 SHARED lock, the
fc2c0 20 61 63 71 75 69 73 69 74 69 6f 6e 20 6f 66 0a acquisition of.
fc2d0 20 20 2a 2a 20 74 68 65 20 50 45 4e 44 49 4e 47 ** the PENDING
fc2e0 5f 4c 4f 43 4b 20 62 79 74 65 20 69 73 20 74 65 _LOCK byte is te
fc2f0 6d 70 6f 72 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 mporary.. */.
fc300 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 70 46 newLocktype = pF
fc310 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 ile->locktype;.
fc320 20 69 66 28 20 20 20 28 70 46 69 6c 65 2d 3e 6c if( (pFile->l
fc330 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b ocktype==NO_LOCK
fc340 29 0a 20 20 20 20 20 7c 7c 20 28 20 20 20 28 6c ). || ( (l
fc350 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 ocktype==EXCLUSI
fc360 56 45 5f 4c 4f 43 4b 29 0a 20 20 20 20 20 20 20 VE_LOCK).
fc370 20 20 26 26 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 && (pFile->loc
fc380 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f ktype==RESERVED_
fc390 4c 4f 43 4b 29 29 0a 20 20 29 7b 0a 20 20 20 20 LOCK)). ){.
fc3a0 69 6e 74 20 63 6e 74 20 3d 20 33 3b 0a 20 20 20 int cnt = 3;.
fc3b0 20 77 68 69 6c 65 28 20 63 6e 74 2d 2d 3e 30 20 while( cnt-->0
fc3c0 26 26 20 28 72 65 73 20 3d 20 4c 6f 63 6b 46 69 && (res = LockFi
fc3d0 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 50 45 4e le(pFile->h, PEN
fc3e0 44 49 4e 47 5f 42 59 54 45 2c 20 30 2c 20 31 2c DING_BYTE, 0, 1,
fc3f0 20 30 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 0))==0 ){.
fc400 20 2f 2a 20 54 72 79 20 33 20 74 69 6d 65 73 20 /* Try 3 times
fc410 74 6f 20 67 65 74 20 74 68 65 20 70 65 6e 64 69 to get the pendi
fc420 6e 67 20 6c 6f 63 6b 2e 20 20 54 68 65 20 70 65 ng lock. The pe
fc430 6e 64 69 6e 67 20 6c 6f 63 6b 20 6d 69 67 68 74 nding lock might
fc440 20 62 65 0a 20 20 20 20 20 20 2a 2a 20 68 65 6c be. ** hel
fc450 64 20 62 79 20 61 6e 6f 74 68 65 72 20 72 65 61 d by another rea
fc460 64 65 72 20 70 72 6f 63 65 73 73 20 77 68 6f 20 der process who
fc470 77 69 6c 6c 20 72 65 6c 65 61 73 65 20 69 74 20 will release it
fc480 6d 6f 6d 65 6e 74 61 72 69 6c 79 2e 0a 20 20 20 momentarily..
fc490 20 20 20 2a 2f 0a 20 20 20 20 20 20 4f 53 54 52 */. OSTR
fc4a0 41 43 45 32 28 22 63 6f 75 6c 64 20 6e 6f 74 20 ACE2("could not
fc4b0 67 65 74 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f get a PENDING lo
fc4c0 63 6b 2e 20 63 6e 74 3d 25 64 5c 6e 22 2c 20 63 ck. cnt=%d\n", c
fc4d0 6e 74 29 3b 0a 20 20 20 20 20 20 53 6c 65 65 70 nt);. Sleep
fc4e0 28 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 67 (1);. }. g
fc4f0 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 otPendingLock =
fc500 72 65 73 3b 0a 20 20 20 20 69 66 28 20 21 72 65 res;. if( !re
fc510 73 20 29 7b 0a 20 20 20 20 20 20 65 72 72 6f 72 s ){. error
fc520 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 = GetLastError(
fc530 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 );. }. }..
fc540 2f 2a 20 41 63 71 75 69 72 65 20 61 20 73 68 61 /* Acquire a sha
fc550 72 65 64 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 red lock. */.
fc560 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 if( locktype==SH
fc570 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73 ARED_LOCK && res
fc580 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
fc590 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d pFile->locktype=
fc5a0 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 =NO_LOCK );.
fc5b0 72 65 73 20 3d 20 67 65 74 52 65 61 64 4c 6f 63 res = getReadLoc
fc5c0 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 69 66 k(pFile);. if
fc5d0 28 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20 6e ( res ){. n
fc5e0 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 ewLocktype = SHA
fc5f0 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 RED_LOCK;. }e
fc600 6c 73 65 7b 0a 20 20 20 20 20 20 65 72 72 6f 72 lse{. error
fc610 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 = GetLastError(
fc620 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 );. }. }..
fc630 2f 2a 20 41 63 71 75 69 72 65 20 61 20 52 45 53 /* Acquire a RES
fc640 45 52 56 45 44 20 6c 6f 63 6b 0a 20 20 2a 2f 0a ERVED lock. */.
fc650 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d if( locktype==
fc660 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26 RESERVED_LOCK &&
fc670 20 72 65 73 20 29 7b 0a 20 20 20 20 61 73 73 65 res ){. asse
fc680 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 rt( pFile->lockt
fc690 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype==SHARED_LOCK
fc6a0 20 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 4c 6f );. res = Lo
fc6b0 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c ckFile(pFile->h,
fc6c0 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 RESERVED_BYTE,
fc6d0 30 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 69 66 0, 1, 0);. if
fc6e0 28 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20 6e ( res ){. n
fc6f0 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 52 45 53 ewLocktype = RES
fc700 45 52 56 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 ERVED_LOCK;.
fc710 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 65 72 72 }else{. err
fc720 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f or = GetLastErro
fc730 72 28 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a r();. }. }..
fc740 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 20 50 /* Acquire a P
fc750 45 4e 44 49 4e 47 20 6c 6f 63 6b 0a 20 20 2a 2f ENDING lock. */
fc760 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d . if( locktype=
fc770 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 =EXCLUSIVE_LOCK
fc780 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20 6e 65 && res ){. ne
fc790 77 4c 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e 44 wLocktype = PEND
fc7a0 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 20 20 67 6f ING_LOCK;. go
fc7b0 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 tPendingLock = 0
fc7c0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 ;. }.. /* Acqu
fc7d0 69 72 65 20 61 6e 20 45 58 43 4c 55 53 49 56 45 ire an EXCLUSIVE
fc7e0 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 lock. */. if(
fc7f0 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 locktype==EXCLU
fc800 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 SIVE_LOCK && res
fc810 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
fc820 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e pFile->locktype>
fc830 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a =SHARED_LOCK );.
fc840 20 20 20 20 72 65 73 20 3d 20 75 6e 6c 6f 63 6b res = unlock
fc850 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b ReadLock(pFile);
fc860 0a 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 75 . OSTRACE2("u
fc870 6e 72 65 61 64 6c 6f 63 6b 20 3d 20 25 64 5c 6e nreadlock = %d\n
fc880 22 2c 20 72 65 73 29 3b 0a 20 20 20 20 72 65 73 ", res);. res
fc890 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c = LockFile(pFil
fc8a0 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52 e->h, SHARED_FIR
fc8b0 53 54 2c 20 30 2c 20 53 48 41 52 45 44 5f 53 49 ST, 0, SHARED_SI
fc8c0 5a 45 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 ZE, 0);. if(
fc8d0 72 65 73 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 res ){. new
fc8e0 4c 6f 63 6b 74 79 70 65 20 3d 20 45 58 43 4c 55 Locktype = EXCLU
fc8f0 53 49 56 45 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d SIVE_LOCK;. }
fc900 65 6c 73 65 7b 0a 20 20 20 20 20 20 65 72 72 6f else{. erro
fc910 72 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 r = GetLastError
fc920 28 29 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 ();. OSTRAC
fc930 45 32 28 22 65 72 72 6f 72 2d 63 6f 64 65 20 3d E2("error-code =
fc940 20 25 64 5c 6e 22 2c 20 65 72 72 6f 72 29 3b 0a %d\n", error);.
fc950 20 20 20 20 20 20 67 65 74 52 65 61 64 4c 6f 63 getReadLoc
fc960 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a k(pFile);. }.
fc970 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 }.. /* If we
fc980 61 72 65 20 68 6f 6c 64 69 6e 67 20 61 20 50 45 are holding a PE
fc990 4e 44 49 4e 47 20 6c 6f 63 6b 20 74 68 61 74 20 NDING lock that
fc9a0 6f 75 67 68 74 20 74 6f 20 62 65 20 72 65 6c 65 ought to be rele
fc9b0 61 73 65 64 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 ased, then. **
fc9c0 72 65 6c 65 61 73 65 20 69 74 20 6e 6f 77 2e 0a release it now..
fc9d0 20 20 2a 2f 0a 20 20 69 66 28 20 67 6f 74 50 65 */. if( gotPe
fc9e0 6e 64 69 6e 67 4c 6f 63 6b 20 26 26 20 6c 6f 63 ndingLock && loc
fc9f0 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f ktype==SHARED_LO
fca00 43 4b 20 29 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b CK ){. Unlock
fca10 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 50 File(pFile->h, P
fca20 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 30 2c 20 ENDING_BYTE, 0,
fca30 31 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 1, 0);. }.. /*
fca40 20 55 70 64 61 74 65 20 74 68 65 20 73 74 61 74 Update the stat
fca50 65 20 6f 66 20 74 68 65 20 6c 6f 63 6b 20 68 61 e of the lock ha
fca60 73 20 68 65 6c 64 20 69 6e 20 74 68 65 20 66 69 s held in the fi
fca70 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 68 le descriptor th
fca80 65 6e 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20 74 en. ** return t
fca90 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 72 he appropriate r
fcaa0 65 73 75 6c 74 20 63 6f 64 65 2e 0a 20 20 2a 2f esult code.. */
fcab0 0a 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 . if( res ){.
fcac0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b rc = SQLITE_OK
fcad0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4f ;. }else{. O
fcae0 53 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 46 41 STRACE4("LOCK FA
fcaf0 49 4c 45 44 20 25 64 20 74 72 79 69 6e 67 20 66 ILED %d trying f
fcb00 6f 72 20 25 64 20 62 75 74 20 67 6f 74 20 25 64 or %d but got %d
fcb10 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 \n", pFile->h,.
fcb20 20 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 lockty
fcb30 70 65 2c 20 6e 65 77 4c 6f 63 6b 74 79 70 65 29 pe, newLocktype)
fcb40 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 ;. pFile->las
fcb50 74 45 72 72 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a tErrno = error;.
fcb60 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
fcb70 42 55 53 59 3b 0a 20 20 7d 0a 20 20 70 46 69 6c BUSY;. }. pFil
fcb80 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 28 75 e->locktype = (u
fcb90 38 29 6e 65 77 4c 6f 63 6b 74 79 70 65 3b 0a 20 8)newLocktype;.
fcba0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
fcbb0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
fcbc0 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 e checks if ther
fcbd0 65 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 e is a RESERVED
fcbe0 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 lock held on the
fcbf0 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 specified.** fi
fcc00 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e le by this or an
fcc10 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e y other process.
fcc20 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 If such a lock
fcc30 69 73 20 68 65 6c 64 2c 20 72 65 74 75 72 6e 0a is held, return.
fcc40 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 6f 74 68 ** non-zero, oth
fcc50 65 72 77 69 73 65 20 7a 65 72 6f 2e 0a 2a 2f 0a erwise zero..*/.
fcc60 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 43 68 static int winCh
fcc70 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 eckReservedLock(
fcc80 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
fcc90 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b , int *pResOut){
fcca0 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 77 69 6e . int rc;. win
fccb0 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 File *pFile = (w
fccc0 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 inFile*)id;.. a
fccd0 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a ssert( id!=0 );.
fcce0 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 if( pFile->loc
fccf0 6b 74 79 70 65 3e 3d 52 45 53 45 52 56 45 44 5f ktype>=RESERVED_
fcd00 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d LOCK ){. rc =
fcd10 20 31 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 1;. OSTRACE3
fcd20 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 ("TEST WR-LOCK %
fcd30 64 20 25 64 20 28 6c 6f 63 61 6c 29 5c 6e 22 2c d %d (local)\n",
fcd40 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 29 3b 0a pFile->h, rc);.
fcd50 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 }else{. rc
fcd60 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 = LockFile(pFile
fcd70 2d 3e 68 2c 20 52 45 53 45 52 56 45 44 5f 42 59 ->h, RESERVED_BY
fcd80 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 TE, 0, 1, 0);.
fcd90 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 if( rc ){.
fcda0 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 UnlockFile(pFi
fcdb0 6c 65 2d 3e 68 2c 20 52 45 53 45 52 56 45 44 5f le->h, RESERVED_
fcdc0 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a BYTE, 0, 1, 0);.
fcdd0 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 21 }. rc = !
fcde0 72 63 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 rc;. OSTRACE3
fcdf0 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 ("TEST WR-LOCK %
fce00 64 20 25 64 20 28 72 65 6d 6f 74 65 29 5c 6e 22 d %d (remote)\n"
fce10 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 29 3b , pFile->h, rc);
fce20 0a 20 20 7d 0a 20 20 2a 70 52 65 73 4f 75 74 20 . }. *pResOut
fce30 3d 20 72 63 3b 0a 20 20 72 65 74 75 72 6e 20 53 = rc;. return S
fce40 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
fce50 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 ** Lower the loc
fce60 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 king level on fi
fce70 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 64 le descriptor id
fce80 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c to locktype. l
fce90 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 ocktype.** must
fcea0 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 be either NO_LOC
fceb0 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b K or SHARED_LOCK
fcec0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c ..**.** If the l
fced0 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 ocking level of
fcee0 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 the file descrip
fcef0 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 tor is already a
fcf00 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 t or below.** th
fcf10 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b e requested lock
fcf20 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 ing level, this
fcf30 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d routine is a no-
fcf40 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 op..**.** It is
fcf50 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 not possible for
fcf60 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f this routine to
fcf70 20 66 61 69 6c 20 69 66 20 74 68 65 20 73 65 63 fail if the sec
fcf80 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 ond argument.**
fcf90 69 73 20 4e 4f 5f 4c 4f 43 4b 2e 20 20 49 66 20 is NO_LOCK. If
fcfa0 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d the second argum
fcfb0 65 6e 74 20 69 73 20 53 48 41 52 45 44 5f 4c 4f ent is SHARED_LO
fcfc0 43 4b 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 CK then this rou
fcfd0 74 69 6e 65 0a 2a 2a 20 6d 69 67 68 74 20 72 65 tine.** might re
fcfe0 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 turn SQLITE_IOER
fcff0 52 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 R;.*/.static int
fd000 20 77 69 6e 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 winUnlock(sqlit
fd010 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
fd020 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69 6e locktype){. in
fd030 74 20 74 79 70 65 3b 0a 20 20 77 69 6e 46 69 6c t type;. winFil
fd040 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 e *pFile = (winF
fd050 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 72 ile*)id;. int r
fd060 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
fd070 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 21 3d assert( pFile!=
fd080 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 0 );. assert( l
fd090 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f ocktype<=SHARED_
fd0a0 4c 4f 43 4b 20 29 3b 0a 20 20 4f 53 54 52 41 43 LOCK );. OSTRAC
fd0b0 45 35 28 22 55 4e 4c 4f 43 4b 20 25 64 20 74 6f E5("UNLOCK %d to
fd0c0 20 25 64 20 77 61 73 20 25 64 28 25 64 29 5c 6e %d was %d(%d)\n
fd0d0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 ", pFile->h, loc
fd0e0 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 20 20 20 ktype,.
fd0f0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
fd100 2c 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c , pFile->sharedL
fd110 6f 63 6b 42 79 74 65 29 3b 0a 20 20 74 79 70 65 ockByte);. type
fd120 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 = pFile->lockty
fd130 70 65 3b 0a 20 20 69 66 28 20 74 79 70 65 3e 3d pe;. if( type>=
fd140 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 EXCLUSIVE_LOCK )
fd150 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 {. UnlockFile
fd160 28 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45 (pFile->h, SHARE
fd170 44 5f 46 49 52 53 54 2c 20 30 2c 20 53 48 41 52 D_FIRST, 0, SHAR
fd180 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 20 20 20 ED_SIZE, 0);.
fd190 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 if( locktype==S
fd1a0 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 21 67 HARED_LOCK && !g
fd1b0 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 etReadLock(pFile
fd1c0 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 ) ){. /* Th
fd1d0 69 73 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 is should never
fd1e0 68 61 70 70 65 6e 2e 20 20 57 65 20 73 68 6f 75 happen. We shou
fd1f0 6c 64 20 61 6c 77 61 79 73 20 62 65 20 61 62 6c ld always be abl
fd200 65 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 72 65 e to. ** re
fd210 61 63 71 75 69 72 65 20 74 68 65 20 72 65 61 64 acquire the read
fd220 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 72 lock */. r
fd230 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 c = SQLITE_IOERR
fd240 5f 55 4e 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 _UNLOCK;. }.
fd250 20 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 52 }. if( type>=R
fd260 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a ESERVED_LOCK ){.
fd270 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 UnlockFile(p
fd280 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52 56 45 File->h, RESERVE
fd290 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 D_BYTE, 0, 1, 0)
fd2a0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6c 6f 63 6b ;. }. if( lock
fd2b0 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 26 26 type==NO_LOCK &&
fd2c0 20 74 79 70 65 3e 3d 53 48 41 52 45 44 5f 4c 4f type>=SHARED_LO
fd2d0 43 4b 20 29 7b 0a 20 20 20 20 75 6e 6c 6f 63 6b CK ){. unlock
fd2e0 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b ReadLock(pFile);
fd2f0 0a 20 20 7d 0a 20 20 69 66 28 20 74 79 70 65 3e . }. if( type>
fd300 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 7b =PENDING_LOCK ){
fd310 0a 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 . UnlockFile(
fd320 70 46 69 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 4e pFile->h, PENDIN
fd330 47 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 G_BYTE, 0, 1, 0)
fd340 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c ;. }. pFile->l
fd350 6f 63 6b 74 79 70 65 20 3d 20 28 75 38 29 6c 6f ocktype = (u8)lo
fd360 63 6b 74 79 70 65 3b 0a 20 20 72 65 74 75 72 6e cktype;. return
fd370 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f rc;.}../*.** Co
fd380 6e 74 72 6f 6c 20 61 6e 64 20 71 75 65 72 79 20 ntrol and query
fd390 6f 66 20 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 of the open file
fd3a0 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 handle..*/.stat
fd3b0 69 63 20 69 6e 74 20 77 69 6e 46 69 6c 65 43 6f ic int winFileCo
fd3c0 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 ntrol(sqlite3_fi
fd3d0 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 le *id, int op,
fd3e0 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 73 void *pArg){. s
fd3f0 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 witch( op ){.
fd400 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e case SQLITE_FCN
fd410 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 3a 20 7b 0a TL_LOCKSTATE: {.
fd420 20 20 20 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 *(int*)pAr
fd430 67 20 3d 20 28 28 77 69 6e 46 69 6c 65 2a 29 69 g = ((winFile*)i
fd440 64 29 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 d)->locktype;.
fd450 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
fd460 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 E_OK;. }.
fd470 63 61 73 65 20 53 51 4c 49 54 45 5f 4c 41 53 54 case SQLITE_LAST
fd480 5f 45 52 52 4e 4f 3a 20 7b 0a 20 20 20 20 20 20 _ERRNO: {.
fd490 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 28 69 *(int*)pArg = (i
fd4a0 6e 74 29 28 28 77 69 6e 46 69 6c 65 2a 29 69 64 nt)((winFile*)id
fd4b0 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 20 )->lastErrno;.
fd4c0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
fd4d0 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a E_OK;. }. }.
fd4e0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
fd4f0 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ERROR;.}../*.**
fd500 52 65 74 75 72 6e 20 74 68 65 20 73 65 63 74 6f Return the secto
fd510 72 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 r size in bytes
fd520 6f 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e of the underlyin
fd530 67 20 62 6c 6f 63 6b 20 64 65 76 69 63 65 20 66 g block device f
fd540 6f 72 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 66 or.** the specif
fd550 69 65 64 20 66 69 6c 65 2e 20 54 68 69 73 20 69 ied file. This i
fd560 73 20 61 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20 s almost always
fd570 35 31 32 20 62 79 74 65 73 2c 20 62 75 74 20 6d 512 bytes, but m
fd580 61 79 20 62 65 0a 2a 2a 20 6c 61 72 67 65 72 20 ay be.** larger
fd590 66 6f 72 20 73 6f 6d 65 20 64 65 76 69 63 65 73 for some devices
fd5a0 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 ..**.** SQLite c
fd5b0 6f 64 65 20 61 73 73 75 6d 65 73 20 74 68 69 73 ode assumes this
fd5c0 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 function cannot
fd5d0 20 66 61 69 6c 2e 20 49 74 20 61 6c 73 6f 20 61 fail. It also a
fd5e0 73 73 75 6d 65 73 20 74 68 61 74 0a 2a 2a 20 69 ssumes that.** i
fd5f0 66 20 74 77 6f 20 66 69 6c 65 73 20 61 72 65 20 f two files are
fd600 63 72 65 61 74 65 64 20 69 6e 20 74 68 65 20 73 created in the s
fd610 61 6d 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 ame file-system
fd620 64 69 72 65 63 74 6f 72 79 20 28 69 2e 65 2e 0a directory (i.e..
fd630 2a 2a 20 61 20 64 61 74 61 62 61 73 65 20 61 6e ** a database an
fd640 64 20 69 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69 d its journal fi
fd650 6c 65 29 20 74 68 61 74 20 74 68 65 20 73 65 63 le) that the sec
fd660 74 6f 72 20 73 69 7a 65 20 77 69 6c 6c 20 62 65 tor size will be
fd670 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 66 6f 72 the.** same for
fd680 20 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 both..*/.static
fd690 20 69 6e 74 20 77 69 6e 53 65 63 74 6f 72 53 69 int winSectorSi
fd6a0 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ze(sqlite3_file
fd6b0 2a 69 64 29 7b 0a 20 20 61 73 73 65 72 74 28 20 *id){. assert(
fd6c0 69 64 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 id!=0 );. retur
fd6d0 6e 20 28 69 6e 74 29 28 28 28 77 69 6e 46 69 6c n (int)(((winFil
fd6e0 65 2a 29 69 64 29 2d 3e 73 65 63 74 6f 72 53 69 e*)id)->sectorSi
fd6f0 7a 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 ze);.}../*.** Re
fd700 74 75 72 6e 20 61 20 76 65 63 74 6f 72 20 6f 66 turn a vector of
fd710 20 64 65 76 69 63 65 20 63 68 61 72 61 63 74 65 device characte
fd720 72 69 73 74 69 63 73 2e 0a 2a 2f 0a 73 74 61 74 ristics..*/.stat
fd730 69 63 20 69 6e 74 20 77 69 6e 44 65 76 69 63 65 ic int winDevice
fd740 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 Characteristics(
fd750 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
fd760 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 ){. UNUSED_PARA
fd770 4d 45 54 45 52 28 69 64 29 3b 0a 20 20 72 65 74 METER(id);. ret
fd780 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn 0;.}../*.**
fd790 54 68 69 73 20 76 65 63 74 6f 72 20 64 65 66 69 This vector defi
fd7a0 6e 65 73 20 61 6c 6c 20 74 68 65 20 6d 65 74 68 nes all the meth
fd7b0 6f 64 73 20 74 68 61 74 20 63 61 6e 20 6f 70 65 ods that can ope
fd7c0 72 61 74 65 20 6f 6e 20 61 6e 0a 2a 2a 20 73 71 rate on an.** sq
fd7d0 6c 69 74 65 33 5f 66 69 6c 65 20 66 6f 72 20 77 lite3_file for w
fd7e0 69 6e 33 32 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 in32..*/.static
fd7f0 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f const sqlite3_io
fd800 5f 6d 65 74 68 6f 64 73 20 77 69 6e 49 6f 4d 65 _methods winIoMe
fd810 74 68 6f 64 20 3d 20 7b 0a 20 20 31 2c 20 20 20 thod = {. 1,
fd820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fd830 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e /* iVersion
fd840 20 2a 2f 0a 20 20 77 69 6e 43 6c 6f 73 65 2c 0a */. winClose,.
fd850 20 20 77 69 6e 52 65 61 64 2c 0a 20 20 77 69 6e winRead,. win
fd860 57 72 69 74 65 2c 0a 20 20 77 69 6e 54 72 75 6e Write,. winTrun
fd870 63 61 74 65 2c 0a 20 20 77 69 6e 53 79 6e 63 2c cate,. winSync,
fd880 0a 20 20 77 69 6e 46 69 6c 65 53 69 7a 65 2c 0a . winFileSize,.
fd890 20 20 77 69 6e 4c 6f 63 6b 2c 0a 20 20 77 69 6e winLock,. win
fd8a0 55 6e 6c 6f 63 6b 2c 0a 20 20 77 69 6e 43 68 65 Unlock,. winChe
fd8b0 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 2c 0a ckReservedLock,.
fd8c0 20 20 77 69 6e 46 69 6c 65 43 6f 6e 74 72 6f 6c winFileControl
fd8d0 2c 0a 20 20 77 69 6e 53 65 63 74 6f 72 53 69 7a ,. winSectorSiz
fd8e0 65 2c 0a 20 20 77 69 6e 44 65 76 69 63 65 43 68 e,. winDeviceCh
fd8f0 61 72 61 63 74 65 72 69 73 74 69 63 73 0a 7d 3b aracteristics.};
fd900 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
fd910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
fd950 2a 20 48 65 72 65 20 65 6e 64 73 20 74 68 65 20 * Here ends the
fd960 49 2f 4f 20 6d 65 74 68 6f 64 73 20 74 68 61 74 I/O methods that
fd970 20 66 6f 72 6d 20 74 68 65 20 73 71 6c 69 74 65 form the sqlite
fd980 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 3_io_methods obj
fd990 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e ect..**.** The n
fd9a0 65 78 74 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 ext block of cod
fd9b0 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 e implements the
fd9c0 20 56 46 53 20 6d 65 74 68 6f 64 73 2e 0a 2a 2a VFS methods..**
fd9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd9f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fda00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fda10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a **********/../*.
fda20 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 55 54 46 ** Convert a UTF
fda30 2d 38 20 66 69 6c 65 6e 61 6d 65 20 69 6e 74 6f -8 filename into
fda40 20 77 68 61 74 65 76 65 72 20 66 6f 72 6d 20 74 whatever form t
fda50 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a he underlying.**
fda60 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 operating syste
fda70 6d 20 77 61 6e 74 73 20 66 69 6c 65 6e 61 6d 65 m wants filename
fda80 73 20 69 6e 2e 20 20 53 70 61 63 65 20 74 6f 20 s in. Space to
fda90 68 6f 6c 64 20 74 68 65 20 72 65 73 75 6c 74 0a hold the result.
fdaa0 2a 2a 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 ** is obtained f
fdab0 72 6f 6d 20 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d rom malloc and m
fdac0 75 73 74 20 62 65 20 66 72 65 65 64 20 62 79 20 ust be freed by
fdad0 74 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 the calling.** f
fdae0 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 unction..*/.stat
fdaf0 69 63 20 76 6f 69 64 20 2a 63 6f 6e 76 65 72 74 ic void *convert
fdb00 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 63 6f 6e Utf8Filename(con
fdb10 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 st char *zFilena
fdb20 6d 65 29 7b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f me){. void *zCo
fdb30 6e 76 65 72 74 65 64 20 3d 20 30 3b 0a 20 20 69 nverted = 0;. i
fdb40 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 f( isNT() ){.
fdb50 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 75 74 zConverted = ut
fdb60 66 38 54 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c f8ToUnicode(zFil
fdb70 65 6e 61 6d 65 29 3b 0a 2f 2a 20 69 73 4e 54 28 ename);./* isNT(
fdb80 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 ) is 1 if SQLITE
fdb90 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f _OS_WINCE==1, so
fdba0 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 this else is ne
fdbb0 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a ver executed. .*
fdbc0 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f /.#if SQLITE_OS_
fdbd0 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 WINCE==0. }else
fdbe0 7b 0a 20 20 20 20 7a 43 6f 6e 76 65 72 74 65 64 {. zConverted
fdbf0 20 3d 20 75 74 66 38 54 6f 4d 62 63 73 28 7a 46 = utf8ToMbcs(zF
fdc00 69 6c 65 6e 61 6d 65 29 3b 0a 23 65 6e 64 69 66 ilename);.#endif
fdc10 0a 20 20 7d 0a 20 20 2f 2a 20 63 61 6c 6c 65 72 . }. /* caller
fdc20 20 77 69 6c 6c 20 68 61 6e 64 6c 65 20 6f 75 74 will handle out
fdc30 20 6f 66 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 of memory */.
fdc40 72 65 74 75 72 6e 20 7a 43 6f 6e 76 65 72 74 65 return zConverte
fdc50 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 d;.}../*.** Crea
fdc60 74 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 te a temporary f
fdc70 69 6c 65 20 6e 61 6d 65 20 69 6e 20 7a 42 75 66 ile name in zBuf
fdc80 2e 20 20 7a 42 75 66 20 6d 75 73 74 20 62 65 20 . zBuf must be
fdc90 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 0a 2a 2a big enough to.**
fdca0 20 68 6f 6c 64 20 61 74 20 70 56 66 73 2d 3e 6d hold at pVfs->m
fdcb0 78 50 61 74 68 6e 61 6d 65 20 63 68 61 72 61 63 xPathname charac
fdcc0 74 65 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ters..*/.static
fdcd0 69 6e 74 20 67 65 74 54 65 6d 70 6e 61 6d 65 28 int getTempname(
fdce0 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a int nBuf, char *
fdcf0 7a 42 75 66 29 7b 0a 20 20 73 74 61 74 69 63 20 zBuf){. static
fdd00 63 68 61 72 20 7a 43 68 61 72 73 5b 5d 20 3d 0a char zChars[] =.
fdd10 20 20 20 20 22 61 62 63 64 65 66 67 68 69 6a 6b "abcdefghijk
fdd20 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a 22 lmnopqrstuvwxyz"
fdd30 0a 20 20 20 20 22 41 42 43 44 45 46 47 48 49 4a . "ABCDEFGHIJ
fdd40 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a KLMNOPQRSTUVWXYZ
fdd50 22 0a 20 20 20 20 22 30 31 32 33 34 35 36 37 38 ". "012345678
fdd60 39 22 3b 0a 20 20 73 69 7a 65 5f 74 20 69 2c 20 9";. size_t i,
fdd70 6a 3b 0a 20 20 63 68 61 72 20 7a 54 65 6d 70 50 j;. char zTempP
fdd80 61 74 68 5b 4d 41 58 5f 50 41 54 48 2b 31 5d 3b ath[MAX_PATH+1];
fdd90 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 74 . if( sqlite3_t
fdda0 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 29 7b emp_directory ){
fddb0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 . sqlite3_snp
fddc0 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 2d 33 rintf(MAX_PATH-3
fddd0 30 2c 20 7a 54 65 6d 70 50 61 74 68 2c 20 22 25 0, zTempPath, "%
fdde0 73 22 2c 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 s", sqlite3_temp
fddf0 5f 64 69 72 65 63 74 6f 72 79 29 3b 0a 20 20 7d _directory);. }
fde00 65 6c 73 65 20 69 66 28 20 69 73 4e 54 28 29 20 else if( isNT()
fde10 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4d 75 ){. char *zMu
fde20 6c 74 69 3b 0a 20 20 20 20 57 43 48 41 52 20 7a lti;. WCHAR z
fde30 57 69 64 65 50 61 74 68 5b 4d 41 58 5f 50 41 54 WidePath[MAX_PAT
fde40 48 5d 3b 0a 20 20 20 20 47 65 74 54 65 6d 70 50 H];. GetTempP
fde50 61 74 68 57 28 4d 41 58 5f 50 41 54 48 2d 33 30 athW(MAX_PATH-30
fde60 2c 20 7a 57 69 64 65 50 61 74 68 29 3b 0a 20 20 , zWidePath);.
fde70 20 20 7a 4d 75 6c 74 69 20 3d 20 75 6e 69 63 6f zMulti = unico
fde80 64 65 54 6f 55 74 66 38 28 7a 57 69 64 65 50 61 deToUtf8(zWidePa
fde90 74 68 29 3b 0a 20 20 20 20 69 66 28 20 7a 4d 75 th);. if( zMu
fdea0 6c 74 69 20 29 7b 0a 20 20 20 20 20 20 73 71 6c lti ){. sql
fdeb0 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41 ite3_snprintf(MA
fdec0 58 5f 50 41 54 48 2d 33 30 2c 20 7a 54 65 6d 70 X_PATH-30, zTemp
fded0 50 61 74 68 2c 20 22 25 73 22 2c 20 7a 4d 75 6c Path, "%s", zMul
fdee0 74 69 29 3b 0a 20 20 20 20 20 20 66 72 65 65 28 ti);. free(
fdef0 7a 4d 75 6c 74 69 29 3b 0a 20 20 20 20 7d 65 6c zMulti);. }el
fdf00 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e se{. return
fdf10 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
fdf20 20 20 20 7d 0a 2f 2a 20 69 73 4e 54 28 29 20 69 }./* isNT() i
fdf30 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 s 1 if SQLITE_OS
fdf40 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 _WINCE==1, so th
fdf50 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 is else is never
fdf60 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 executed. .** S
fdf70 69 6e 63 65 20 74 68 65 20 41 53 43 49 49 20 76 ince the ASCII v
fdf80 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 ersion of these
fdf90 57 69 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e Windows API do n
fdfa0 6f 74 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e ot exist for WIN
fdfb0 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f CE,.** it's impo
fdfc0 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 rtant to not ref
fdfd0 65 72 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 erence them for
fdfe0 57 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f WINCE builds..*/
fdff0 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 .#if SQLITE_OS_W
fe000 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b INCE==0. }else{
fe010 0a 20 20 20 20 63 68 61 72 20 2a 7a 55 74 66 38 . char *zUtf8
fe020 3b 0a 20 20 20 20 63 68 61 72 20 7a 4d 62 63 73 ;. char zMbcs
fe030 50 61 74 68 5b 4d 41 58 5f 50 41 54 48 5d 3b 0a Path[MAX_PATH];.
fe040 20 20 20 20 47 65 74 54 65 6d 70 50 61 74 68 41 GetTempPathA
fe050 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 4d (MAX_PATH-30, zM
fe060 62 63 73 50 61 74 68 29 3b 0a 20 20 20 20 7a 55 bcsPath);. zU
fe070 74 66 38 20 3d 20 73 71 6c 69 74 65 33 5f 77 69 tf8 = sqlite3_wi
fe080 6e 33 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 66 38 n32_mbcs_to_utf8
fe090 28 7a 4d 62 63 73 50 61 74 68 29 3b 0a 20 20 20 (zMbcsPath);.
fe0a0 20 69 66 28 20 7a 55 74 66 38 20 29 7b 0a 20 20 if( zUtf8 ){.
fe0b0 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
fe0c0 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 2d 33 30 intf(MAX_PATH-30
fe0d0 2c 20 7a 54 65 6d 70 50 61 74 68 2c 20 22 25 73 , zTempPath, "%s
fe0e0 22 2c 20 7a 55 74 66 38 29 3b 0a 20 20 20 20 20 ", zUtf8);.
fe0f0 20 66 72 65 65 28 7a 55 74 66 38 29 3b 0a 20 20 free(zUtf8);.
fe100 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 }else{. r
fe110 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
fe120 45 4d 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 EM;. }.#endif
fe130 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 73 71 6c . }. for(i=sql
fe140 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 65 ite3Strlen30(zTe
fe150 6d 70 50 61 74 68 29 3b 20 69 3e 30 20 26 26 20 mpPath); i>0 &&
fe160 7a 54 65 6d 70 50 61 74 68 5b 69 2d 31 5d 3d 3d zTempPath[i-1]==
fe170 27 5c 5c 27 3b 20 69 2d 2d 29 7b 7d 0a 20 20 7a '\\'; i--){}. z
fe180 54 65 6d 70 50 61 74 68 5b 69 5d 20 3d 20 30 3b TempPath[i] = 0;
fe190 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 . sqlite3_snpri
fe1a0 6e 74 66 28 6e 42 75 66 2d 33 30 2c 20 7a 42 75 ntf(nBuf-30, zBu
fe1b0 66 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 f,.
fe1c0 20 20 20 20 20 20 22 25 73 5c 5c 22 53 51 4c 49 "%s\\"SQLI
fe1d0 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 TE_TEMP_FILE_PRE
fe1e0 46 49 58 2c 20 7a 54 65 6d 70 50 61 74 68 29 3b FIX, zTempPath);
fe1f0 0a 20 20 6a 20 3d 20 73 71 6c 69 74 65 33 53 74 . j = sqlite3St
fe200 72 6c 65 6e 33 30 28 7a 42 75 66 29 3b 0a 20 20 rlen30(zBuf);.
fe210 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 sqlite3_randomne
fe220 73 73 28 32 30 2c 20 26 7a 42 75 66 5b 6a 5d 29 ss(20, &zBuf[j])
fe230 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32 ;. for(i=0; i<2
fe240 30 3b 20 69 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20 20 0; i++, j++){.
fe250 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 28 63 68 61 zBuf[j] = (cha
fe260 72 29 7a 43 68 61 72 73 5b 20 28 28 75 6e 73 69 r)zChars[ ((unsi
fe270 67 6e 65 64 20 63 68 61 72 29 7a 42 75 66 5b 6a gned char)zBuf[j
fe280 5d 29 25 28 73 69 7a 65 6f 66 28 7a 43 68 61 72 ])%(sizeof(zChar
fe290 73 29 2d 31 29 20 5d 3b 0a 20 20 7d 0a 20 20 7a s)-1) ];. }. z
fe2a0 42 75 66 5b 6a 5d 20 3d 20 30 3b 0a 20 20 4f 53 Buf[j] = 0;. OS
fe2b0 54 52 41 43 45 32 28 22 54 45 4d 50 20 46 49 4c TRACE2("TEMP FIL
fe2c0 45 4e 41 4d 45 3a 20 25 73 5c 6e 22 2c 20 7a 42 ENAME: %s\n", zB
fe2d0 75 66 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 uf);. return SQ
fe2e0 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 0a 2f 2a 0a LITE_OK; .}../*.
fe2f0 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 ** The return va
fe300 6c 75 65 20 6f 66 20 67 65 74 4c 61 73 74 45 72 lue of getLastEr
fe310 72 6f 72 4d 73 67 0a 2a 2a 20 69 73 20 7a 65 72 rorMsg.** is zer
fe320 6f 20 69 66 20 74 68 65 20 65 72 72 6f 72 20 6d o if the error m
fe330 65 73 73 61 67 65 20 66 69 74 73 20 69 6e 20 74 essage fits in t
fe340 68 65 20 62 75 66 66 65 72 2c 20 6f 72 20 6e 6f he buffer, or no
fe350 6e 2d 7a 65 72 6f 0a 2a 2a 20 6f 74 68 65 72 77 n-zero.** otherw
fe360 69 73 65 20 28 69 66 20 74 68 65 20 6d 65 73 73 ise (if the mess
fe370 61 67 65 20 77 61 73 20 74 72 75 6e 63 61 74 65 age was truncate
fe380 64 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e d)..*/.static in
fe390 74 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d 73 t getLastErrorMs
fe3a0 67 28 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 g(int nBuf, char
fe3b0 20 2a 7a 42 75 66 29 7b 0a 20 20 2f 2a 20 46 6f *zBuf){. /* Fo
fe3c0 72 6d 61 74 4d 65 73 73 61 67 65 20 72 65 74 75 rmatMessage retu
fe3d0 72 6e 73 20 30 20 6f 6e 20 66 61 69 6c 75 72 65 rns 0 on failure
fe3e0 2e 20 20 4f 74 68 65 72 77 69 73 65 20 69 74 0a . Otherwise it.
fe3f0 20 20 2a 2a 20 72 65 74 75 72 6e 73 20 74 68 65 ** returns the
fe400 20 6e 75 6d 62 65 72 20 6f 66 20 54 43 48 41 52 number of TCHAR
fe410 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 s written to the
fe420 20 6f 75 74 70 75 74 0a 20 20 2a 2a 20 62 75 66 output. ** buf
fe430 66 65 72 2c 20 65 78 63 6c 75 64 69 6e 67 20 74 fer, excluding t
fe440 68 65 20 74 65 72 6d 69 6e 61 74 69 6e 67 20 6e he terminating n
fe450 75 6c 6c 20 63 68 61 72 2e 0a 20 20 2a 2f 0a 20 ull char.. */.
fe460 20 44 57 4f 52 44 20 65 72 72 6f 72 20 3d 20 47 DWORD error = G
fe470 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 etLastError();.
fe480 20 44 57 4f 52 44 20 64 77 4c 65 6e 20 3d 20 30 DWORD dwLen = 0
fe490 3b 0a 20 20 63 68 61 72 20 2a 7a 4f 75 74 20 3d ;. char *zOut =
fe4a0 20 30 3b 0a 0a 20 20 69 66 28 20 69 73 4e 54 28 0;.. if( isNT(
fe4b0 29 20 29 7b 0a 20 20 20 20 57 43 48 41 52 20 2a ) ){. WCHAR *
fe4c0 7a 54 65 6d 70 57 69 64 65 20 3d 20 4e 55 4c 4c zTempWide = NULL
fe4d0 3b 0a 20 20 20 20 64 77 4c 65 6e 20 3d 20 46 6f ;. dwLen = Fo
fe4e0 72 6d 61 74 4d 65 73 73 61 67 65 57 28 46 4f 52 rmatMessageW(FOR
fe4f0 4d 41 54 5f 4d 45 53 53 41 47 45 5f 41 4c 4c 4f MAT_MESSAGE_ALLO
fe500 43 41 54 45 5f 42 55 46 46 45 52 20 7c 20 46 4f CATE_BUFFER | FO
fe510 52 4d 41 54 5f 4d 45 53 53 41 47 45 5f 46 52 4f RMAT_MESSAGE_FRO
fe520 4d 5f 53 59 53 54 45 4d 20 7c 20 46 4f 52 4d 41 M_SYSTEM | FORMA
fe530 54 5f 4d 45 53 53 41 47 45 5f 49 47 4e 4f 52 45 T_MESSAGE_IGNORE
fe540 5f 49 4e 53 45 52 54 53 2c 0a 20 20 20 20 20 20 _INSERTS,.
fe550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe560 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 NULL,.
fe570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe580 20 20 20 20 20 20 65 72 72 6f 72 2c 0a 20 20 20 error,.
fe590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe5a0 20 20 20 20 20 20 20 20 30 2c 0a 20 20 20 20 20 0,.
fe5b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe5c0 20 20 20 20 20 20 28 4c 50 57 53 54 52 29 20 26 (LPWSTR) &
fe5d0 7a 54 65 6d 70 57 69 64 65 2c 0a 20 20 20 20 20 zTempWide,.
fe5e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe5f0 20 20 20 20 20 20 30 2c 0a 20 20 20 20 20 20 20 0,.
fe600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe610 20 20 20 20 30 29 3b 0a 20 20 20 20 69 66 28 20 0);. if(
fe620 64 77 4c 65 6e 20 3e 20 30 20 29 7b 0a 20 20 20 dwLen > 0 ){.
fe630 20 20 20 2f 2a 20 61 6c 6c 6f 63 61 74 65 20 61 /* allocate a
fe640 20 62 75 66 66 65 72 20 61 6e 64 20 63 6f 6e 76 buffer and conv
fe650 65 72 74 20 74 6f 20 55 54 46 38 20 2a 2f 0a 20 ert to UTF8 */.
fe660 20 20 20 20 20 7a 4f 75 74 20 3d 20 75 6e 69 63 zOut = unic
fe670 6f 64 65 54 6f 55 74 66 38 28 7a 54 65 6d 70 57 odeToUtf8(zTempW
fe680 69 64 65 29 3b 0a 20 20 20 20 20 20 2f 2a 20 66 ide);. /* f
fe690 72 65 65 20 74 68 65 20 73 79 73 74 65 6d 20 62 ree the system b
fe6a0 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 20 uffer allocated
fe6b0 62 79 20 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 by FormatMessage
fe6c0 20 2a 2f 0a 20 20 20 20 20 20 4c 6f 63 61 6c 46 */. LocalF
fe6d0 72 65 65 28 7a 54 65 6d 70 57 69 64 65 29 3b 0a ree(zTempWide);.
fe6e0 20 20 20 20 7d 0a 2f 2a 20 69 73 4e 54 28 29 20 }./* isNT()
fe6f0 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f is 1 if SQLITE_O
fe700 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 S_WINCE==1, so t
fe710 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 his else is neve
fe720 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 r executed. .**
fe730 53 69 6e 63 65 20 74 68 65 20 41 53 43 49 49 20 Since the ASCII
fe740 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 version of these
fe750 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 Windows API do
fe760 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20 57 49 not exist for WI
fe770 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 NCE,.** it's imp
fe780 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 ortant to not re
fe790 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 ference them for
fe7a0 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a WINCE builds..*
fe7b0 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f /.#if SQLITE_OS_
fe7c0 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 WINCE==0. }else
fe7d0 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 65 6d {. char *zTem
fe7e0 70 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 64 77 p = NULL;. dw
fe7f0 4c 65 6e 20 3d 20 46 6f 72 6d 61 74 4d 65 73 73 Len = FormatMess
fe800 61 67 65 41 28 46 4f 52 4d 41 54 5f 4d 45 53 53 ageA(FORMAT_MESS
fe810 41 47 45 5f 41 4c 4c 4f 43 41 54 45 5f 42 55 46 AGE_ALLOCATE_BUF
fe820 46 45 52 20 7c 20 46 4f 52 4d 41 54 5f 4d 45 53 FER | FORMAT_MES
fe830 53 41 47 45 5f 46 52 4f 4d 5f 53 59 53 54 45 4d SAGE_FROM_SYSTEM
fe840 20 7c 20 46 4f 52 4d 41 54 5f 4d 45 53 53 41 47 | FORMAT_MESSAG
fe850 45 5f 49 47 4e 4f 52 45 5f 49 4e 53 45 52 54 53 E_IGNORE_INSERTS
fe860 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
fe870 20 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c NUL
fe880 4c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 L,.
fe890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 65 72 er
fe8a0 72 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ror,.
fe8b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe8c0 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 0,.
fe8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 4c (L
fe8e0 50 53 54 52 29 20 26 7a 54 65 6d 70 2c 0a 20 20 PSTR) &zTemp,.
fe8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe900 20 20 20 20 20 20 20 20 20 30 2c 0a 20 20 20 20 0,.
fe910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe920 20 20 20 20 20 20 20 30 29 3b 0a 20 20 20 20 69 0);. i
fe930 66 28 20 64 77 4c 65 6e 20 3e 20 30 20 29 7b 0a f( dwLen > 0 ){.
fe940 20 20 20 20 20 20 2f 2a 20 61 6c 6c 6f 63 61 74 /* allocat
fe950 65 20 61 20 62 75 66 66 65 72 20 61 6e 64 20 63 e a buffer and c
fe960 6f 6e 76 65 72 74 20 74 6f 20 55 54 46 38 20 2a onvert to UTF8 *
fe970 2f 0a 20 20 20 20 20 20 7a 4f 75 74 20 3d 20 73 /. zOut = s
fe980 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f 6d 62 63 qlite3_win32_mbc
fe990 73 5f 74 6f 5f 75 74 66 38 28 7a 54 65 6d 70 29 s_to_utf8(zTemp)
fe9a0 3b 0a 20 20 20 20 20 20 2f 2a 20 66 72 65 65 20 ;. /* free
fe9b0 74 68 65 20 73 79 73 74 65 6d 20 62 75 66 66 65 the system buffe
fe9c0 72 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 46 r allocated by F
fe9d0 6f 72 6d 61 74 4d 65 73 73 61 67 65 20 2a 2f 0a ormatMessage */.
fe9e0 20 20 20 20 20 20 4c 6f 63 61 6c 46 72 65 65 28 LocalFree(
fe9f0 7a 54 65 6d 70 29 3b 0a 20 20 20 20 7d 0a 23 65 zTemp);. }.#e
fea00 6e 64 69 66 0a 20 20 7d 0a 20 20 69 66 28 20 30 ndif. }. if( 0
fea10 20 3d 3d 20 64 77 4c 65 6e 20 29 7b 0a 20 20 20 == dwLen ){.
fea20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
fea30 66 28 6e 42 75 66 2c 20 7a 42 75 66 2c 20 22 4f f(nBuf, zBuf, "O
fea40 73 45 72 72 6f 72 20 30 78 25 78 20 28 25 75 29 sError 0x%x (%u)
fea50 22 2c 20 65 72 72 6f 72 2c 20 65 72 72 6f 72 29 ", error, error)
fea60 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f ;. }else{. /
fea70 2a 20 63 6f 70 79 20 61 20 6d 61 78 69 6d 75 6d * copy a maximum
fea80 20 6f 66 20 6e 42 75 66 20 63 68 61 72 73 20 74 of nBuf chars t
fea90 6f 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 o output buffer
feaa0 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 */. sqlite3_s
feab0 6e 70 72 69 6e 74 66 28 6e 42 75 66 2c 20 7a 42 nprintf(nBuf, zB
feac0 75 66 2c 20 22 25 73 22 2c 20 7a 4f 75 74 29 3b uf, "%s", zOut);
fead0 0a 20 20 20 20 2f 2a 20 66 72 65 65 20 74 68 65 . /* free the
feae0 20 55 54 46 38 20 62 75 66 66 65 72 20 2a 2f 0a UTF8 buffer */.
feaf0 20 20 20 20 66 72 65 65 28 7a 4f 75 74 29 3b 0a free(zOut);.
feb00 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a }. return 0;.
feb10 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 }../*.** Open a
feb20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 file..*/.static
feb30 69 6e 74 20 77 69 6e 4f 70 65 6e 28 0a 20 20 73 int winOpen(. s
feb40 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
feb50 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 , /* Not
feb60 75 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 used */. const
feb70 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 char *zName,
feb80 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 /* Name of t
feb90 68 65 20 66 69 6c 65 20 28 55 54 46 2d 38 29 20 he file (UTF-8)
feba0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c */. sqlite3_fil
febb0 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 2f e *id, /
febc0 2a 20 57 72 69 74 65 20 74 68 65 20 53 51 4c 69 * Write the SQLi
febd0 74 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 68 te file handle h
febe0 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 ere */. int fla
febf0 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 gs,
fec00 20 20 20 2f 2a 20 4f 70 65 6e 20 6d 6f 64 65 20 /* Open mode
fec10 66 6c 61 67 73 20 2a 2f 0a 20 20 69 6e 74 20 2a flags */. int *
fec20 70 4f 75 74 46 6c 61 67 73 20 20 20 20 20 20 20 pOutFlags
fec30 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73 20 72 /* Status r
fec40 65 74 75 72 6e 20 66 6c 61 67 73 20 2a 2f 0a 29 eturn flags */.)
fec50 7b 0a 20 20 48 41 4e 44 4c 45 20 68 3b 0a 20 20 {. HANDLE h;.
fec60 44 57 4f 52 44 20 64 77 44 65 73 69 72 65 64 41 DWORD dwDesiredA
fec70 63 63 65 73 73 3b 0a 20 20 44 57 4f 52 44 20 64 ccess;. DWORD d
fec80 77 53 68 61 72 65 4d 6f 64 65 3b 0a 20 20 44 57 wShareMode;. DW
fec90 4f 52 44 20 64 77 43 72 65 61 74 69 6f 6e 44 69 ORD dwCreationDi
feca0 73 70 6f 73 69 74 69 6f 6e 3b 0a 20 20 44 57 4f sposition;. DWO
fecb0 52 44 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74 RD dwFlagsAndAtt
fecc0 72 69 62 75 74 65 73 20 3d 20 30 3b 0a 23 69 66 ributes = 0;.#if
fecd0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
fece0 0a 20 20 69 6e 74 20 69 73 54 65 6d 70 20 3d 20 . int isTemp =
fecf0 30 3b 0a 23 65 6e 64 69 66 0a 20 20 77 69 6e 46 0;.#endif. winF
fed00 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 ile *pFile = (wi
fed10 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 76 6f 69 nFile*)id;. voi
fed20 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 3b 20 20 d *zConverted;
fed30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
fed40 2a 20 46 69 6c 65 6e 61 6d 65 20 69 6e 20 4f 53 * Filename in OS
fed50 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 63 encoding */. c
fed60 6f 6e 73 74 20 63 68 61 72 20 2a 7a 55 74 66 38 onst char *zUtf8
fed70 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b 20 20 20 Name = zName;
fed80 20 2f 2a 20 46 69 6c 65 6e 61 6d 65 20 69 6e 20 /* Filename in
fed90 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 2a UTF-8 encoding *
feda0 2f 0a 20 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d /. char zTmpnam
fedb0 65 5b 4d 41 58 5f 50 41 54 48 2b 31 5d 3b 20 20 e[MAX_PATH+1];
fedc0 20 20 20 20 20 20 2f 2a 20 42 75 66 66 65 72 20 /* Buffer
fedd0 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 74 used to create t
fede0 65 6d 70 20 66 69 6c 65 6e 61 6d 65 20 2a 2f 0a emp filename */.
fedf0 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 . assert( id!=0
fee00 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 );. UNUSED_PAR
fee10 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 0a 20 AMETER(pVfs);..
fee20 20 2f 2a 20 49 66 20 74 68 65 20 73 65 63 6f 6e /* If the secon
fee30 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 d argument to th
fee40 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 4e is function is N
fee50 55 4c 4c 2c 20 67 65 6e 65 72 61 74 65 20 61 20 ULL, generate a
fee60 0a 20 20 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 . ** temporary
fee70 66 69 6c 65 20 6e 61 6d 65 20 74 6f 20 75 73 65 file name to use
fee80 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 7a 55 . */. if( !zU
fee90 74 66 38 4e 61 6d 65 20 29 7b 0a 20 20 20 20 69 tf8Name ){. i
feea0 6e 74 20 72 63 20 3d 20 67 65 74 54 65 6d 70 6e nt rc = getTempn
feeb0 61 6d 65 28 4d 41 58 5f 50 41 54 48 2b 31 2c 20 ame(MAX_PATH+1,
feec0 7a 54 6d 70 6e 61 6d 65 29 3b 0a 20 20 20 20 69 zTmpname);. i
feed0 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
feee0 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
feef0 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a rc;. }. z
fef00 55 74 66 38 4e 61 6d 65 20 3d 20 7a 54 6d 70 6e Utf8Name = zTmpn
fef10 61 6d 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 ame;. }.. /* C
fef20 6f 6e 76 65 72 74 20 74 68 65 20 66 69 6c 65 6e onvert the filen
fef30 61 6d 65 20 74 6f 20 74 68 65 20 73 79 73 74 65 ame to the syste
fef40 6d 20 65 6e 63 6f 64 69 6e 67 2e 20 2a 2f 0a 20 m encoding. */.
fef50 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f zConverted = co
fef60 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d nvertUtf8Filenam
fef70 65 28 7a 55 74 66 38 4e 61 6d 65 29 3b 0a 20 20 e(zUtf8Name);.
fef80 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 64 3d 3d if( zConverted==
fef90 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
fefa0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
fefb0 7d 0a 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 }.. if( flags &
fefc0 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 SQLITE_OPEN_REA
fefd0 44 57 52 49 54 45 20 29 7b 0a 20 20 20 20 64 77 DWRITE ){. dw
fefe0 44 65 73 69 72 65 64 41 63 63 65 73 73 20 3d 20 DesiredAccess =
feff0 47 45 4e 45 52 49 43 5f 52 45 41 44 20 7c 20 47 GENERIC_READ | G
ff000 45 4e 45 52 49 43 5f 57 52 49 54 45 3b 0a 20 20 ENERIC_WRITE;.
ff010 7d 65 6c 73 65 7b 0a 20 20 20 20 64 77 44 65 73 }else{. dwDes
ff020 69 72 65 64 41 63 63 65 73 73 20 3d 20 47 45 4e iredAccess = GEN
ff030 45 52 49 43 5f 52 45 41 44 3b 0a 20 20 7d 0a 20 ERIC_READ;. }.
ff040 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f /* SQLITE_OPEN_
ff050 45 58 43 4c 55 53 49 56 45 20 69 73 20 75 73 65 EXCLUSIVE is use
ff060 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 d to make sure t
ff070 68 61 74 20 61 20 6e 65 77 20 66 69 6c 65 20 69 hat a new file i
ff080 73 20 0a 20 20 2a 2a 20 63 72 65 61 74 65 64 2e s . ** created.
ff090 20 53 51 4c 69 74 65 20 64 6f 65 73 6e 27 74 20 SQLite doesn't
ff0a0 75 73 65 20 69 74 20 74 6f 20 69 6e 64 69 63 61 use it to indica
ff0b0 74 65 20 22 65 78 63 6c 75 73 69 76 65 20 61 63 te "exclusive ac
ff0c0 63 65 73 73 22 20 0a 20 20 2a 2a 20 61 73 20 69 cess" . ** as i
ff0d0 74 20 69 73 20 75 73 75 61 6c 6c 79 20 75 6e 64 t is usually und
ff0e0 65 72 73 74 6f 6f 64 2e 0a 20 20 2a 2f 0a 20 20 erstood.. */.
ff0f0 61 73 73 65 72 74 28 21 28 66 6c 61 67 73 20 26 assert(!(flags &
ff100 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 SQLITE_OPEN_EXC
ff110 4c 55 53 49 56 45 29 20 7c 7c 20 28 66 6c 61 67 LUSIVE) || (flag
ff120 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f s & SQLITE_OPEN_
ff130 43 52 45 41 54 45 29 29 3b 0a 20 20 69 66 28 20 CREATE));. if(
ff140 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f flags & SQLITE_O
ff150 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 29 7b PEN_EXCLUSIVE ){
ff160 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65 73 20 . /* Creates
ff170 61 20 6e 65 77 20 66 69 6c 65 2c 20 6f 6e 6c 79 a new file, only
ff180 20 69 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 if it does not
ff190 61 6c 72 65 61 64 79 20 65 78 69 73 74 2e 20 2a already exist. *
ff1a0 2f 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 /. /* If the
ff1b0 66 69 6c 65 20 65 78 69 73 74 73 2c 20 69 74 20 file exists, it
ff1c0 66 61 69 6c 73 2e 20 2a 2f 0a 20 20 20 20 64 77 fails. */. dw
ff1d0 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 CreationDisposit
ff1e0 69 6f 6e 20 3d 20 43 52 45 41 54 45 5f 4e 45 57 ion = CREATE_NEW
ff1f0 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66 6c ;. }else if( fl
ff200 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 ags & SQLITE_OPE
ff210 4e 5f 43 52 45 41 54 45 20 29 7b 0a 20 20 20 20 N_CREATE ){.
ff220 2f 2a 20 4f 70 65 6e 20 65 78 69 73 74 69 6e 67 /* Open existing
ff230 20 66 69 6c 65 2c 20 6f 72 20 63 72 65 61 74 65 file, or create
ff240 20 69 66 20 69 74 20 64 6f 65 73 6e 27 74 20 65 if it doesn't e
ff250 78 69 73 74 20 2a 2f 0a 20 20 20 20 64 77 43 72 xist */. dwCr
ff260 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f eationDispositio
ff270 6e 20 3d 20 4f 50 45 4e 5f 41 4c 57 41 59 53 3b n = OPEN_ALWAYS;
ff280 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
ff290 20 4f 70 65 6e 73 20 61 20 66 69 6c 65 2c 20 6f Opens a file, o
ff2a0 6e 6c 79 20 69 66 20 69 74 20 65 78 69 73 74 73 nly if it exists
ff2b0 2e 20 2a 2f 0a 20 20 20 20 64 77 43 72 65 61 74 . */. dwCreat
ff2c0 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e 20 3d ionDisposition =
ff2d0 20 4f 50 45 4e 5f 45 58 49 53 54 49 4e 47 3b 0a OPEN_EXISTING;.
ff2e0 20 20 7d 0a 20 20 64 77 53 68 61 72 65 4d 6f 64 }. dwShareMod
ff2f0 65 20 3d 20 46 49 4c 45 5f 53 48 41 52 45 5f 52 e = FILE_SHARE_R
ff300 45 41 44 20 7c 20 46 49 4c 45 5f 53 48 41 52 45 EAD | FILE_SHARE
ff310 5f 57 52 49 54 45 3b 0a 20 20 69 66 28 20 66 6c _WRITE;. if( fl
ff320 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 ags & SQLITE_OPE
ff330 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 N_DELETEONCLOSE
ff340 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 ){.#if SQLITE_OS
ff350 5f 57 49 4e 43 45 0a 20 20 20 20 64 77 46 6c 61 _WINCE. dwFla
ff360 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 20 gsAndAttributes
ff370 3d 20 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 = FILE_ATTRIBUTE
ff380 5f 48 49 44 44 45 4e 3b 0a 20 20 20 20 69 73 54 _HIDDEN;. isT
ff390 65 6d 70 20 3d 20 31 3b 0a 23 65 6c 73 65 0a 20 emp = 1;.#else.
ff3a0 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74 dwFlagsAndAtt
ff3b0 72 69 62 75 74 65 73 20 3d 20 46 49 4c 45 5f 41 ributes = FILE_A
ff3c0 54 54 52 49 42 55 54 45 5f 54 45 4d 50 4f 52 41 TTRIBUTE_TEMPORA
ff3d0 52 59 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 RY.
ff3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff3f0 20 20 7c 20 46 49 4c 45 5f 41 54 54 52 49 42 55 | FILE_ATTRIBU
ff400 54 45 5f 48 49 44 44 45 4e 0a 20 20 20 20 20 20 TE_HIDDEN.
ff410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff420 20 20 20 20 20 20 20 20 20 7c 20 46 49 4c 45 5f | FILE_
ff430 46 4c 41 47 5f 44 45 4c 45 54 45 5f 4f 4e 5f 43 FLAG_DELETE_ON_C
ff440 4c 4f 53 45 3b 0a 23 65 6e 64 69 66 0a 20 20 7d LOSE;.#endif. }
ff450 65 6c 73 65 7b 0a 20 20 20 20 64 77 46 6c 61 67 else{. dwFlag
ff460 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 20 3d sAndAttributes =
ff470 20 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 5f FILE_ATTRIBUTE_
ff480 4e 4f 52 4d 41 4c 3b 0a 20 20 7d 0a 20 20 2f 2a NORMAL;. }. /*
ff490 20 52 65 70 6f 72 74 73 20 66 72 6f 6d 20 74 68 Reports from th
ff4a0 65 20 69 6e 74 65 72 6e 65 74 20 61 72 65 20 74 e internet are t
ff4b0 68 61 74 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 hat performance
ff4c0 69 73 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20 62 is always. ** b
ff4d0 65 74 74 65 72 20 69 66 20 46 49 4c 45 5f 46 4c etter if FILE_FL
ff4e0 41 47 5f 52 41 4e 44 4f 4d 5f 41 43 43 45 53 53 AG_RANDOM_ACCESS
ff4f0 20 69 73 20 75 73 65 64 2e 20 20 54 69 63 6b 65 is used. Ticke
ff500 74 20 23 32 36 39 39 2e 20 2a 2f 0a 23 69 66 20 t #2699. */.#if
ff510 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a SQLITE_OS_WINCE.
ff520 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72 dwFlagsAndAttr
ff530 69 62 75 74 65 73 20 7c 3d 20 46 49 4c 45 5f 46 ibutes |= FILE_F
ff540 4c 41 47 5f 52 41 4e 44 4f 4d 5f 41 43 43 45 53 LAG_RANDOM_ACCES
ff550 53 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 S;.#endif. if(
ff560 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 68 20 isNT() ){. h
ff570 3d 20 43 72 65 61 74 65 46 69 6c 65 57 28 28 57 = CreateFileW((W
ff580 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 CHAR*)zConverted
ff590 2c 0a 20 20 20 20 20 20 20 64 77 44 65 73 69 72 ,. dwDesir
ff5a0 65 64 41 63 63 65 73 73 2c 0a 20 20 20 20 20 20 edAccess,.
ff5b0 20 64 77 53 68 61 72 65 4d 6f 64 65 2c 0a 20 20 dwShareMode,.
ff5c0 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 NULL,.
ff5d0 20 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70 dwCreationDisp
ff5e0 6f 73 69 74 69 6f 6e 2c 0a 20 20 20 20 20 20 20 osition,.
ff5f0 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62 dwFlagsAndAttrib
ff600 75 74 65 73 2c 0a 20 20 20 20 20 20 20 4e 55 4c utes,. NUL
ff610 4c 0a 20 20 20 20 29 3b 0a 2f 2a 20 69 73 4e 54 L. );./* isNT
ff620 28 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 () is 1 if SQLIT
ff630 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 E_OS_WINCE==1, s
ff640 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e o this else is n
ff650 65 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a ever executed. .
ff660 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41 53 43 ** Since the ASC
ff670 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 II version of th
ff680 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20 ese Windows API
ff690 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 do not exist for
ff6a0 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 WINCE,.** it's
ff6b0 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 important to not
ff6c0 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20 reference them
ff6d0 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73 for WINCE builds
ff6e0 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f ..*/.#if SQLITE_
ff6f0 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 OS_WINCE==0. }e
ff700 6c 73 65 7b 0a 20 20 20 20 68 20 3d 20 43 72 65 lse{. h = Cre
ff710 61 74 65 46 69 6c 65 41 28 28 63 68 61 72 2a 29 ateFileA((char*)
ff720 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20 20 20 20 zConverted,.
ff730 20 20 20 64 77 44 65 73 69 72 65 64 41 63 63 65 dwDesiredAcce
ff740 73 73 2c 0a 20 20 20 20 20 20 20 64 77 53 68 61 ss,. dwSha
ff750 72 65 4d 6f 64 65 2c 0a 20 20 20 20 20 20 20 4e reMode,. N
ff760 55 4c 4c 2c 0a 20 20 20 20 20 20 20 64 77 43 72 ULL,. dwCr
ff770 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f eationDispositio
ff780 6e 2c 0a 20 20 20 20 20 20 20 64 77 46 6c 61 67 n,. dwFlag
ff790 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 2c 0a sAndAttributes,.
ff7a0 20 20 20 20 20 20 20 4e 55 4c 4c 0a 20 20 20 20 NULL.
ff7b0 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 );.#endif. }.
ff7c0 69 66 28 20 68 3d 3d 49 4e 56 41 4c 49 44 5f 48 if( h==INVALID_H
ff7d0 41 4e 44 4c 45 5f 56 41 4c 55 45 20 29 7b 0a 20 ANDLE_VALUE ){.
ff7e0 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 free(zConvert
ff7f0 65 64 29 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 ed);. if( fla
ff800 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e gs & SQLITE_OPEN
ff810 5f 52 45 41 44 57 52 49 54 45 20 29 7b 0a 20 20 _READWRITE ){.
ff820 20 20 20 20 72 65 74 75 72 6e 20 77 69 6e 4f 70 return winOp
ff830 65 6e 28 70 56 66 73 2c 20 7a 4e 61 6d 65 2c 20 en(pVfs, zName,
ff840 69 64 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 id, .
ff850 20 20 28 28 66 6c 61 67 73 7c 53 51 4c 49 54 45 ((flags|SQLITE
ff860 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 26 _OPEN_READONLY)&
ff870 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 ~SQLITE_OPEN_REA
ff880 44 57 52 49 54 45 29 2c 20 70 4f 75 74 46 6c 61 DWRITE), pOutFla
ff890 67 73 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a gs);. }else{.
ff8a0 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
ff8b0 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 ITE_CANTOPEN;.
ff8c0 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f }. }. if( pO
ff8d0 75 74 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 69 utFlags ){. i
ff8e0 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 f( flags & SQLIT
ff8f0 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 E_OPEN_READWRITE
ff900 20 29 7b 0a 20 20 20 20 20 20 2a 70 4f 75 74 46 ){. *pOutF
ff910 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 4f 50 lags = SQLITE_OP
ff920 45 4e 5f 52 45 41 44 57 52 49 54 45 3b 0a 20 20 EN_READWRITE;.
ff930 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2a }else{. *
ff940 70 4f 75 74 46 6c 61 67 73 20 3d 20 53 51 4c 49 pOutFlags = SQLI
ff950 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 TE_OPEN_READONLY
ff960 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6d 65 ;. }. }. me
ff970 6d 73 65 74 28 70 46 69 6c 65 2c 20 30 2c 20 73 mset(pFile, 0, s
ff980 69 7a 65 6f 66 28 2a 70 46 69 6c 65 29 29 3b 0a izeof(*pFile));.
ff990 20 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 pFile->pMethod
ff9a0 20 3d 20 26 77 69 6e 49 6f 4d 65 74 68 6f 64 3b = &winIoMethod;
ff9b0 0a 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 68 3b . pFile->h = h;
ff9c0 0a 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 . pFile->lastEr
ff9d0 72 6e 6f 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a rno = NO_ERROR;.
ff9e0 20 20 70 46 69 6c 65 2d 3e 73 65 63 74 6f 72 53 pFile->sectorS
ff9f0 69 7a 65 20 3d 20 67 65 74 53 65 63 74 6f 72 53 ize = getSectorS
ffa00 69 7a 65 28 70 56 66 73 2c 20 7a 55 74 66 38 4e ize(pVfs, zUtf8N
ffa10 61 6d 65 29 3b 0a 23 69 66 20 53 51 4c 49 54 45 ame);.#if SQLITE
ffa20 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 69 66 28 20 _OS_WINCE. if(
ffa30 28 66 6c 61 67 73 20 26 20 28 53 51 4c 49 54 45 (flags & (SQLITE
ffa40 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c _OPEN_READWRITE|
ffa50 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e SQLITE_OPEN_MAIN
ffa60 5f 44 42 29 29 20 3d 3d 0a 20 20 20 20 20 20 20 _DB)) ==.
ffa70 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f (SQLITE_
ffa80 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 OPEN_READWRITE|S
ffa90 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f QLITE_OPEN_MAIN_
ffaa0 44 42 29 0a 20 20 20 20 20 20 20 26 26 20 21 77 DB). && !w
ffab0 69 6e 63 65 43 72 65 61 74 65 4c 6f 63 6b 28 7a inceCreateLock(z
ffac0 4e 61 6d 65 2c 20 70 46 69 6c 65 29 0a 20 20 29 Name, pFile). )
ffad0 7b 0a 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c {. CloseHandl
ffae0 65 28 68 29 3b 0a 20 20 20 20 66 72 65 65 28 7a e(h);. free(z
ffaf0 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 Converted);.
ffb00 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 return SQLITE_CA
ffb10 4e 54 4f 50 45 4e 3b 0a 20 20 7d 0a 20 20 69 66 NTOPEN;. }. if
ffb20 28 20 69 73 54 65 6d 70 20 29 7b 0a 20 20 20 20 ( isTemp ){.
ffb30 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e pFile->zDeleteOn
ffb40 43 6c 6f 73 65 20 3d 20 7a 43 6f 6e 76 65 72 74 Close = zConvert
ffb50 65 64 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 ed;. }else.#end
ffb60 69 66 0a 20 20 7b 0a 20 20 20 20 66 72 65 65 28 if. {. free(
ffb70 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 7d zConverted);. }
ffb80 0a 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2b . OpenCounter(+
ffb90 31 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 1);. return SQL
ffba0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
ffbb0 20 44 65 6c 65 74 65 20 74 68 65 20 6e 61 6d 65 Delete the name
ffbc0 64 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f d file..**.** No
ffbd0 74 65 20 74 68 61 74 20 77 69 6e 64 6f 77 73 20 te that windows
ffbe0 64 6f 65 73 20 6e 6f 74 20 61 6c 6c 6f 77 20 61 does not allow a
ffbf0 20 66 69 6c 65 20 74 6f 20 62 65 20 64 65 6c 65 file to be dele
ffc00 74 65 64 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 ted if some othe
ffc10 72 0a 2a 2a 20 70 72 6f 63 65 73 73 20 68 61 73 r.** process has
ffc20 20 69 74 20 6f 70 65 6e 2e 20 20 53 6f 6d 65 74 it open. Somet
ffc30 69 6d 65 73 20 61 20 76 69 72 75 73 20 73 63 61 imes a virus sca
ffc40 6e 6e 65 72 20 6f 72 20 69 6e 64 65 78 69 6e 67 nner or indexing
ffc50 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 77 69 6c 6c program.** will
ffc60 20 6f 70 65 6e 20 61 20 6a 6f 75 72 6e 61 6c 20 open a journal
ffc70 66 69 6c 65 20 73 68 6f 72 74 6c 79 20 61 66 74 file shortly aft
ffc80 65 72 20 69 74 20 69 73 20 63 72 65 61 74 65 64 er it is created
ffc90 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 64 6f 0a in order to do.
ffca0 2a 2a 20 77 68 61 74 65 76 65 72 20 69 74 20 64 ** whatever it d
ffcb0 6f 65 73 2e 20 20 57 68 69 6c 65 20 74 68 69 73 oes. While this
ffcc0 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 69 other process i
ffcd0 73 20 68 6f 6c 64 69 6e 67 20 74 68 65 0a 2a 2a s holding the.**
ffce0 20 66 69 6c 65 20 6f 70 65 6e 2c 20 77 65 20 77 file open, we w
ffcf0 69 6c 6c 20 62 65 20 75 6e 61 62 6c 65 20 74 6f ill be unable to
ffd00 20 64 65 6c 65 74 65 20 69 74 2e 20 20 54 6f 20 delete it. To
ffd10 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 69 73 work around this
ffd20 0a 2a 2a 20 70 72 6f 62 6c 65 6d 2c 20 77 65 20 .** problem, we
ffd30 64 65 6c 61 79 20 31 30 30 20 6d 69 6c 6c 69 73 delay 100 millis
ffd40 65 63 6f 6e 64 73 20 61 6e 64 20 74 72 79 20 74 econds and try t
ffd50 6f 20 64 65 6c 65 74 65 20 61 67 61 69 6e 2e 20 o delete again.
ffd60 20 55 70 0a 2a 2a 20 74 6f 20 4d 58 5f 44 45 4c Up.** to MX_DEL
ffd70 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 73 20 64 ETION_ATTEMPTs d
ffd80 65 6c 65 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 eletion attempts
ffd90 20 61 72 65 20 72 75 6e 20 62 65 66 6f 72 65 20 are run before
ffda0 67 69 76 69 6e 67 0a 2a 2a 20 75 70 20 61 6e 64 giving.** up and
ffdb0 20 72 65 74 75 72 6e 69 6e 67 20 61 6e 20 65 72 returning an er
ffdc0 72 6f 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ror..*/.#define
ffdd0 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 MX_DELETION_ATTE
ffde0 4d 50 54 53 20 35 0a 73 74 61 74 69 63 20 69 6e MPTS 5.static in
ffdf0 74 20 77 69 6e 44 65 6c 65 74 65 28 0a 20 20 73 t winDelete(. s
ffe00 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
ffe10 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f , /* No
ffe20 74 20 75 73 65 64 20 6f 6e 20 77 69 6e 33 32 20 t used on win32
ffe30 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
ffe40 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20 20 *zFilename,
ffe50 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 /* Name of file
ffe60 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 to delete */.
ffe70 69 6e 74 20 73 79 6e 63 44 69 72 20 20 20 20 20 int syncDir
ffe80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
ffe90 6f 74 20 75 73 65 64 20 6f 6e 20 77 69 6e 33 32 ot used on win32
ffea0 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 63 6e 74 */.){. int cnt
ffeb0 20 3d 20 30 3b 0a 20 20 44 57 4f 52 44 20 72 63 = 0;. DWORD rc
ffec0 3b 0a 20 20 44 57 4f 52 44 20 65 72 72 6f 72 20 ;. DWORD error
ffed0 3d 20 30 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f = 0;. void *zCo
ffee0 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e 76 65 72 nverted = conver
ffef0 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a 46 tUtf8Filename(zF
fff00 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 55 4e 55 53 ilename);. UNUS
fff10 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 ED_PARAMETER(pVf
fff20 73 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 s);. UNUSED_PAR
fff30 41 4d 45 54 45 52 28 73 79 6e 63 44 69 72 29 3b AMETER(syncDir);
fff40 0a 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 . if( zConverte
fff50 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 d==0 ){. retu
fff60 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
fff70 0a 20 20 7d 0a 20 20 53 69 6d 75 6c 61 74 65 49 . }. SimulateI
fff80 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 OError(return SQ
fff90 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 LITE_IOERR_DELET
fffa0 45 29 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 29 E);. if( isNT()
fffb0 20 29 7b 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 ){. do{.
fffc0 20 20 44 65 6c 65 74 65 46 69 6c 65 57 28 7a 43 DeleteFileW(zC
fffd0 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 7d onverted);. }
fffe0 77 68 69 6c 65 28 20 20 20 28 20 20 20 28 28 72 while( ( ((r
ffff0 63 20 3d 20 47 65 74 46 69 6c 65 41 74 74 72 69 c = GetFileAttri
10000 62 75 74 65 73 57 28 7a 43 6f 6e 76 65 72 74 65 butesW(zConverte
10001 64 29 29 20 21 3d 20 49 4e 56 41 4c 49 44 5f 46 d)) != INVALID_F
10002 49 4c 45 5f 41 54 54 52 49 42 55 54 45 53 29 0a ILE_ATTRIBUTES).
10003 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c |
10004 7c 20 28 28 65 72 72 6f 72 20 3d 20 47 65 74 4c | ((error = GetL
10005 61 73 74 45 72 72 6f 72 28 29 29 20 3d 3d 20 45 astError()) == E
10006 52 52 4f 52 5f 41 43 43 45 53 53 5f 44 45 4e 49 RROR_ACCESS_DENI
10007 45 44 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 ED)).
10008 26 26 20 28 2b 2b 63 6e 74 20 3c 20 4d 58 5f 44 && (++cnt < MX_D
10009 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 ELETION_ATTEMPTS
1000a 29 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 20 ). &&
1000b 28 53 6c 65 65 70 28 31 30 30 29 2c 20 31 29 20 (Sleep(100), 1)
1000c 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 );./* isNT() is
1000d 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 1 if SQLITE_OS_W
1000e 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 INCE==1, so this
1000f 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 else is never e
10010 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e xecuted. .** Sin
10011 63 65 20 74 68 65 20 41 53 43 49 49 20 76 65 72 ce the ASCII ver
10012 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69 sion of these Wi
10013 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 ndows API do not
10014 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45 exist for WINCE
10015 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 ,.** it's import
10016 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 ant to not refer
10017 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49 ence them for WI
10018 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 NCE builds..*/.#
10019 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
1001a 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 CE==0. }else{.
1001b 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 44 65 6c do{. Del
1001c 65 74 65 46 69 6c 65 41 28 7a 43 6f 6e 76 65 72 eteFileA(zConver
1001d 74 65 64 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65 ted);. }while
1001e 28 20 20 20 28 20 20 20 28 28 72 63 20 3d 20 47 ( ( ((rc = G
1001f 65 74 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 etFileAttributes
10020 41 28 7a 43 6f 6e 76 65 72 74 65 64 29 29 20 21 A(zConverted)) !
10021 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 = INVALID_FILE_A
10022 54 54 52 49 42 55 54 45 53 29 0a 20 20 20 20 20 TTRIBUTES).
10023 20 20 20 20 20 20 20 20 20 20 7c 7c 20 28 28 65 || ((e
10024 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72 rror = GetLastEr
10025 72 6f 72 28 29 29 20 3d 3d 20 45 52 52 4f 52 5f ror()) == ERROR_
10026 41 43 43 45 53 53 5f 44 45 4e 49 45 44 29 29 0a ACCESS_DENIED)).
10027 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28 2b && (+
10028 2b 63 6e 74 20 3c 20 4d 58 5f 44 45 4c 45 54 49 +cnt < MX_DELETI
10029 4f 4e 5f 41 54 54 45 4d 50 54 53 29 0a 20 20 20 ON_ATTEMPTS).
1002a 20 20 20 20 20 20 20 20 26 26 20 28 53 6c 65 65 && (Slee
1002b 70 28 31 30 30 29 2c 20 31 29 20 29 3b 0a 23 65 p(100), 1) );.#e
1002c 6e 64 69 66 0a 20 20 7d 0a 20 20 66 72 65 65 28 ndif. }. free(
1002d 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 4f zConverted);. O
1002e 53 54 52 41 43 45 32 28 22 44 45 4c 45 54 45 20 STRACE2("DELETE
1002f 5c 22 25 73 5c 22 5c 6e 22 2c 20 7a 46 69 6c 65 \"%s\"\n", zFile
10030 6e 61 6d 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 name);. return
10031 28 20 20 20 28 72 63 20 3d 3d 20 49 4e 56 41 4c ( (rc == INVAL
10032 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54 ID_FILE_ATTRIBUT
10033 45 53 29 20 0a 20 20 20 20 20 20 20 20 20 20 26 ES) . &
10034 26 20 28 65 72 72 6f 72 20 3d 3d 20 45 52 52 4f & (error == ERRO
10035 52 5f 46 49 4c 45 5f 4e 4f 54 5f 46 4f 55 4e 44 R_FILE_NOT_FOUND
10036 29 29 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a )) ? SQLITE_OK :
10037 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 SQLITE_IOERR_DE
10038 4c 45 54 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 LETE;.}../*.** C
10039 68 65 63 6b 20 74 68 65 20 65 78 69 73 74 61 6e heck the existan
1003a 63 65 20 61 6e 64 20 73 74 61 74 75 73 20 6f 66 ce and status of
1003b 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 a file..*/.stat
1003c 69 63 20 69 6e 74 20 77 69 6e 41 63 63 65 73 73 ic int winAccess
1003d 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 (. sqlite3_vfs
1003e 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 2f *pVfs, /
1003f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 77 69 * Not used on wi
10040 6e 33 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 n32 */. const c
10041 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 har *zFilename,
10042 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 /* Name of f
10043 69 6c 65 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a ile to check */.
10044 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 int flags,
10045 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10046 54 79 70 65 20 6f 66 20 74 65 73 74 20 74 6f 20 Type of test to
10047 6d 61 6b 65 20 6f 6e 20 74 68 69 73 20 66 69 6c make on this fil
10048 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 73 e */. int *pRes
10049 4f 75 74 20 20 20 20 20 20 20 20 20 20 20 20 20 Out
1004a 20 20 2f 2a 20 4f 55 54 3a 20 52 65 73 75 6c 74 /* OUT: Result
1004b 20 2a 2f 0a 29 7b 0a 20 20 44 57 4f 52 44 20 61 */.){. DWORD a
1004c 74 74 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 ttr;. int rc =
1004d 30 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 0;. void *zConv
1004e 65 72 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 erted = convertU
1004f 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a 46 69 6c tf8Filename(zFil
10050 65 6e 61 6d 65 29 3b 0a 20 20 55 4e 55 53 45 44 ename);. UNUSED
10051 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 _PARAMETER(pVfs)
10052 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 74 ;. if( zConvert
10053 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 ed==0 ){. ret
10054 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
10055 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 4e 54 ;. }. if( isNT
10056 28 29 20 29 7b 0a 20 20 20 20 61 74 74 72 20 3d () ){. attr =
10057 20 47 65 74 46 69 6c 65 41 74 74 72 69 62 75 74 GetFileAttribut
10058 65 73 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e esW((WCHAR*)zCon
10059 76 65 72 74 65 64 29 3b 0a 2f 2a 20 69 73 4e 54 verted);./* isNT
1005a 28 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 () is 1 if SQLIT
1005b 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 E_OS_WINCE==1, s
1005c 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e o this else is n
1005d 65 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a ever executed. .
1005e 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41 53 43 ** Since the ASC
1005f 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 II version of th
10060 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20 ese Windows API
10061 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 do not exist for
10062 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 WINCE,.** it's
10063 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 important to not
10064 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20 reference them
10065 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73 for WINCE builds
10066 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f ..*/.#if SQLITE_
10067 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 OS_WINCE==0. }e
10068 6c 73 65 7b 0a 20 20 20 20 61 74 74 72 20 3d 20 lse{. attr =
10069 47 65 74 46 69 6c 65 41 74 74 72 69 62 75 74 65 GetFileAttribute
1006a 73 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 sA((char*)zConve
1006b 72 74 65 64 29 3b 0a 23 65 6e 64 69 66 0a 20 20 rted);.#endif.
1006c 7d 0a 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 }. free(zConver
1006d 74 65 64 29 3b 0a 20 20 73 77 69 74 63 68 28 20 ted);. switch(
1006e 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 63 61 73 flags ){. cas
1006f 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f e SQLITE_ACCESS_
10070 52 45 41 44 3a 0a 20 20 20 20 63 61 73 65 20 53 READ:. case S
10071 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 QLITE_ACCESS_EXI
10072 53 54 53 3a 0a 20 20 20 20 20 20 72 63 20 3d 20 STS:. rc =
10073 61 74 74 72 21 3d 49 4e 56 41 4c 49 44 5f 46 49 attr!=INVALID_FI
10074 4c 45 5f 41 54 54 52 49 42 55 54 45 53 3b 0a 20 LE_ATTRIBUTES;.
10075 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
10076 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 case SQLITE_ACCE
10077 53 53 5f 52 45 41 44 57 52 49 54 45 3a 0a 20 20 SS_READWRITE:.
10078 20 20 20 20 72 63 20 3d 20 28 61 74 74 72 20 26 rc = (attr &
10079 20 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 5f FILE_ATTRIBUTE_
1007a 52 45 41 44 4f 4e 4c 59 29 3d 3d 30 3b 0a 20 20 READONLY)==0;.
1007b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 break;. d
1007c 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 61 73 efault:. as
1007d 73 65 72 74 28 21 22 49 6e 76 61 6c 69 64 20 66 sert(!"Invalid f
1007e 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 22 29 3b lags argument");
1007f 0a 20 20 7d 0a 20 20 2a 70 52 65 73 4f 75 74 20 . }. *pResOut
10080 3d 20 72 63 3b 0a 20 20 72 65 74 75 72 6e 20 53 = rc;. return S
10081 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a QLITE_OK;.}.../*
10082 0a 2a 2a 20 54 75 72 6e 20 61 20 72 65 6c 61 74 .** Turn a relat
10083 69 76 65 20 70 61 74 68 6e 61 6d 65 20 69 6e 74 ive pathname int
10084 6f 20 61 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d o a full pathnam
10085 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 66 75 e. Write the fu
10086 6c 6c 0a 2a 2a 20 70 61 74 68 6e 61 6d 65 20 69 ll.** pathname i
10087 6e 74 6f 20 7a 4f 75 74 5b 5d 2e 20 20 7a 4f 75 nto zOut[]. zOu
10088 74 5b 5d 20 77 69 6c 6c 20 62 65 20 61 74 20 6c t[] will be at l
10089 65 61 73 74 20 70 56 66 73 2d 3e 6d 78 50 61 74 east pVfs->mxPat
1008a 68 6e 61 6d 65 0a 2a 2a 20 62 79 74 65 73 20 69 hname.** bytes i
1008b 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 n size..*/.stati
1008c 63 20 69 6e 74 20 77 69 6e 46 75 6c 6c 50 61 74 c int winFullPat
1008d 68 6e 61 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 hname(. sqlite3
1008e 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 _vfs *pVfs,
1008f 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
10090 72 20 74 6f 20 76 66 73 20 6f 62 6a 65 63 74 20 r to vfs object
10091 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
10092 2a 7a 52 65 6c 61 74 69 76 65 2c 20 20 20 20 20 *zRelative,
10093 20 20 20 2f 2a 20 50 6f 73 73 69 62 6c 79 20 72 /* Possibly r
10094 65 6c 61 74 69 76 65 20 69 6e 70 75 74 20 70 61 elative input pa
10095 74 68 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 75 6c th */. int nFul
10096 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 l,
10097 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
10098 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 69 output buffer i
10099 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 63 68 61 n bytes */. cha
1009a 72 20 2a 7a 46 75 6c 6c 20 20 20 20 20 20 20 20 r *zFull
1009b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 /* Ou
1009c 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 29 tput buffer */.)
1009d 7b 0a 20 20 0a 23 69 66 20 64 65 66 69 6e 65 64 {. .#if defined
1009e 28 5f 5f 43 59 47 57 49 4e 5f 5f 29 0a 20 20 55 (__CYGWIN__). U
1009f 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
100a0 6e 46 75 6c 6c 29 3b 0a 20 20 63 79 67 77 69 6e nFull);. cygwin
100a1 5f 63 6f 6e 76 5f 74 6f 5f 66 75 6c 6c 5f 77 69 _conv_to_full_wi
100a2 6e 33 32 5f 70 61 74 68 28 7a 52 65 6c 61 74 69 n32_path(zRelati
100a3 76 65 2c 20 7a 46 75 6c 6c 29 3b 0a 20 20 72 65 ve, zFull);. re
100a4 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
100a5 23 65 6e 64 69 66 0a 0a 23 69 66 20 53 51 4c 49 #endif..#if SQLI
100a6 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 55 4e TE_OS_WINCE. UN
100a7 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e USED_PARAMETER(n
100a8 46 75 6c 6c 29 3b 0a 20 20 2f 2a 20 57 69 6e 43 Full);. /* WinC
100a9 45 20 68 61 73 20 6e 6f 20 63 6f 6e 63 65 70 74 E has no concept
100aa 20 6f 66 20 61 20 72 65 6c 61 74 69 76 65 20 70 of a relative p
100ab 61 74 68 6e 61 6d 65 2c 20 6f 72 20 73 6f 20 49 athname, or so I
100ac 20 61 6d 20 74 6f 6c 64 2e 20 2a 2f 0a 20 20 73 am told. */. s
100ad 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
100ae 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 pVfs->mxPathname
100af 2c 20 7a 46 75 6c 6c 2c 20 22 25 73 22 2c 20 7a , zFull, "%s", z
100b0 52 65 6c 61 74 69 76 65 29 3b 0a 20 20 72 65 74 Relative);. ret
100b1 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 urn SQLITE_OK;.#
100b2 65 6e 64 69 66 0a 0a 23 69 66 20 21 53 51 4c 49 endif..#if !SQLI
100b3 54 45 5f 4f 53 5f 57 49 4e 43 45 20 26 26 20 21 TE_OS_WINCE && !
100b4 64 65 66 69 6e 65 64 28 5f 5f 43 59 47 57 49 4e defined(__CYGWIN
100b5 5f 5f 29 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b __). int nByte;
100b6 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 . void *zConver
100b7 74 65 64 3b 0a 20 20 63 68 61 72 20 2a 7a 4f 75 ted;. char *zOu
100b8 74 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 t;. UNUSED_PARA
100b9 4d 45 54 45 52 28 6e 46 75 6c 6c 29 3b 0a 20 20 METER(nFull);.
100ba 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e zConverted = con
100bb 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 vertUtf8Filename
100bc 28 7a 52 65 6c 61 74 69 76 65 29 3b 0a 20 20 69 (zRelative);. i
100bd 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 f( isNT() ){.
100be 20 57 43 48 41 52 20 2a 7a 54 65 6d 70 3b 0a 20 WCHAR *zTemp;.
100bf 20 20 20 6e 42 79 74 65 20 3d 20 47 65 74 46 75 nByte = GetFu
100c0 6c 6c 50 61 74 68 4e 61 6d 65 57 28 28 57 43 48 llPathNameW((WCH
100c1 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20 AR*)zConverted,
100c2 30 2c 20 30 2c 20 30 29 20 2b 20 33 3b 0a 20 20 0, 0, 0) + 3;.
100c3 20 20 7a 54 65 6d 70 20 3d 20 6d 61 6c 6c 6f 63 zTemp = malloc
100c4 28 20 6e 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a ( nByte*sizeof(z
100c5 54 65 6d 70 5b 30 5d 29 20 29 3b 0a 20 20 20 20 Temp[0]) );.
100c6 69 66 28 20 7a 54 65 6d 70 3d 3d 30 20 29 7b 0a if( zTemp==0 ){.
100c7 20 20 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 free(zConv
100c8 65 72 74 65 64 29 3b 0a 20 20 20 20 20 20 72 65 erted);. re
100c9 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
100ca 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 47 65 74 M;. }. Get
100cb 46 75 6c 6c 50 61 74 68 4e 61 6d 65 57 28 28 57 FullPathNameW((W
100cc 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 CHAR*)zConverted
100cd 2c 20 6e 42 79 74 65 2c 20 7a 54 65 6d 70 2c 20 , nByte, zTemp,
100ce 30 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 43 6f 0);. free(zCo
100cf 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 7a 4f nverted);. zO
100d0 75 74 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74 ut = unicodeToUt
100d1 66 38 28 7a 54 65 6d 70 29 3b 0a 20 20 20 20 66 f8(zTemp);. f
100d2 72 65 65 28 7a 54 65 6d 70 29 3b 0a 2f 2a 20 69 ree(zTemp);./* i
100d3 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 51 sNT() is 1 if SQ
100d4 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 LITE_OS_WINCE==1
100d5 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 , so this else i
100d6 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 s never executed
100d7 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 . .** Since the
100d8 41 53 43 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 ASCII version of
100d9 20 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20 41 these Windows A
100da 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 PI do not exist
100db 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 for WINCE,.** it
100dc 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 's important to
100dd 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74 68 not reference th
100de 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75 69 em for WINCE bui
100df 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 lds..*/.#if SQLI
100e0 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 TE_OS_WINCE==0.
100e1 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72 }else{. char
100e2 20 2a 7a 54 65 6d 70 3b 0a 20 20 20 20 6e 42 79 *zTemp;. nBy
100e3 74 65 20 3d 20 47 65 74 46 75 6c 6c 50 61 74 68 te = GetFullPath
100e4 4e 61 6d 65 41 28 28 63 68 61 72 2a 29 7a 43 6f NameA((char*)zCo
100e5 6e 76 65 72 74 65 64 2c 20 30 2c 20 30 2c 20 30 nverted, 0, 0, 0
100e6 29 20 2b 20 33 3b 0a 20 20 20 20 7a 54 65 6d 70 ) + 3;. zTemp
100e7 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 = malloc( nByte
100e8 2a 73 69 7a 65 6f 66 28 7a 54 65 6d 70 5b 30 5d *sizeof(zTemp[0]
100e9 29 20 29 3b 0a 20 20 20 20 69 66 28 20 7a 54 65 ) );. if( zTe
100ea 6d 70 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 mp==0 ){. f
100eb 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b ree(zConverted);
100ec 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
100ed 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 LITE_NOMEM;.
100ee 7d 0a 20 20 20 20 47 65 74 46 75 6c 6c 50 61 74 }. GetFullPat
100ef 68 4e 61 6d 65 41 28 28 63 68 61 72 2a 29 7a 43 hNameA((char*)zC
100f0 6f 6e 76 65 72 74 65 64 2c 20 6e 42 79 74 65 2c onverted, nByte,
100f1 20 7a 54 65 6d 70 2c 20 30 29 3b 0a 20 20 20 20 zTemp, 0);.
100f2 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 free(zConverted)
100f3 3b 0a 20 20 20 20 7a 4f 75 74 20 3d 20 73 71 6c ;. zOut = sql
100f4 69 74 65 33 5f 77 69 6e 33 32 5f 6d 62 63 73 5f ite3_win32_mbcs_
100f5 74 6f 5f 75 74 66 38 28 7a 54 65 6d 70 29 3b 0a to_utf8(zTemp);.
100f6 20 20 20 20 66 72 65 65 28 7a 54 65 6d 70 29 3b free(zTemp);
100f7 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 69 66 .#endif. }. if
100f8 28 20 7a 4f 75 74 20 29 7b 0a 20 20 20 20 73 71 ( zOut ){. sq
100f9 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 70 lite3_snprintf(p
100fa 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2c Vfs->mxPathname,
100fb 20 7a 46 75 6c 6c 2c 20 22 25 73 22 2c 20 7a 4f zFull, "%s", zO
100fc 75 74 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 4f ut);. free(zO
100fd 75 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 ut);. return
100fe 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c SQLITE_OK;. }el
100ff 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 se{. return S
10100 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d QLITE_NOMEM;. }
10101 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a .#endif.}../*.**
10102 20 47 65 74 20 74 68 65 20 73 65 63 74 6f 72 20 Get the sector
10103 73 69 7a 65 20 6f 66 20 74 68 65 20 64 65 76 69 size of the devi
10104 63 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 ce used to store
10105 0a 2a 2a 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 .** file..*/.sta
10106 74 69 63 20 69 6e 74 20 67 65 74 53 65 63 74 6f tic int getSecto
10107 72 53 69 7a 65 28 0a 20 20 20 20 73 71 6c 69 74 rSize(. sqlit
10108 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 0a 20 20 e3_vfs *pVfs,.
10109 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 const char *zR
1010a 65 6c 61 74 69 76 65 20 20 20 20 20 2f 2a 20 55 elative /* U
1010b 54 46 2d 38 20 66 69 6c 65 20 6e 61 6d 65 20 2a TF-8 file name *
1010c 2f 0a 29 7b 0a 20 20 44 57 4f 52 44 20 62 79 74 /.){. DWORD byt
1010d 65 73 50 65 72 53 65 63 74 6f 72 20 3d 20 53 51 esPerSector = SQ
1010e 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 LITE_DEFAULT_SEC
1010f 54 4f 52 5f 53 49 5a 45 3b 0a 20 20 2f 2a 20 47 TOR_SIZE;. /* G
10110 65 74 44 69 73 6b 46 72 65 65 53 70 61 63 65 20 etDiskFreeSpace
10111 69 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 65 64 is not supported
10112 20 75 6e 64 65 72 20 57 49 4e 43 45 20 2a 2f 0a under WINCE */.
10113 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 #if SQLITE_OS_WI
10114 4e 43 45 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 NCE. UNUSED_PAR
10115 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 AMETER(pVfs);.
10116 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
10117 28 7a 52 65 6c 61 74 69 76 65 29 3b 0a 23 65 6c (zRelative);.#el
10118 73 65 0a 20 20 63 68 61 72 20 7a 46 75 6c 6c 70 se. char zFullp
10119 61 74 68 5b 4d 41 58 5f 50 41 54 48 2b 31 5d 3b ath[MAX_PATH+1];
1011a 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 44 57 4f . int rc;. DWO
1011b 52 44 20 64 77 52 65 74 20 3d 20 30 3b 0a 20 20 RD dwRet = 0;.
1011c 44 57 4f 52 44 20 64 77 44 75 6d 6d 79 3b 0a 0a DWORD dwDummy;..
1011d 20 20 2f 2a 0a 20 20 2a 2a 20 57 65 20 6e 65 65 /*. ** We nee
1011e 64 20 74 6f 20 67 65 74 20 74 68 65 20 66 75 6c d to get the ful
1011f 6c 20 70 61 74 68 20 6e 61 6d 65 20 6f 66 20 74 l path name of t
10120 68 65 20 66 69 6c 65 0a 20 20 2a 2a 20 74 6f 20 he file. ** to
10121 67 65 74 20 74 68 65 20 64 72 69 76 65 20 6c 65 get the drive le
10122 74 74 65 72 20 74 6f 20 6c 6f 6f 6b 20 75 70 20 tter to look up
10123 74 68 65 20 73 65 63 74 6f 72 0a 20 20 2a 2a 20 the sector. **
10124 73 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 size.. */. rc
10125 3d 20 77 69 6e 46 75 6c 6c 50 61 74 68 6e 61 6d = winFullPathnam
10126 65 28 70 56 66 73 2c 20 7a 52 65 6c 61 74 69 76 e(pVfs, zRelativ
10127 65 2c 20 4d 41 58 5f 50 41 54 48 2c 20 7a 46 75 e, MAX_PATH, zFu
10128 6c 6c 70 61 74 68 29 3b 0a 20 20 69 66 28 20 72 llpath);. if( r
10129 63 20 3d 3d 20 53 51 4c 49 54 45 5f 4f 4b 20 29 c == SQLITE_OK )
1012a 0a 20 20 7b 0a 20 20 20 20 76 6f 69 64 20 2a 7a . {. void *z
1012b 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e 76 Converted = conv
1012c 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 ertUtf8Filename(
1012d 7a 46 75 6c 6c 70 61 74 68 29 3b 0a 20 20 20 20 zFullpath);.
1012e 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 64 20 29 if( zConverted )
1012f 7b 0a 20 20 20 20 20 20 69 66 28 20 69 73 4e 54 {. if( isNT
10130 28 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a () ){. /*
10131 20 74 72 69 6d 20 70 61 74 68 20 74 6f 20 6a 75 trim path to ju
10132 73 74 20 64 72 69 76 65 20 72 65 66 65 72 65 6e st drive referen
10133 63 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 57 43 ce */. WC
10134 48 41 52 20 2a 70 20 3d 20 7a 43 6f 6e 76 65 72 HAR *p = zConver
10135 74 65 64 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 ted;. for
10136 28 3b 2a 70 3b 70 2b 2b 29 7b 0a 20 20 20 20 20 (;*p;p++){.
10137 20 20 20 20 20 69 66 28 20 2a 70 20 3d 3d 20 27 if( *p == '
10138 5c 5c 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 \\' ){.
10139 20 20 20 2a 70 20 3d 20 27 5c 30 27 3b 0a 20 20 *p = '\0';.
1013a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b break;
1013b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
1013c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64 }. d
1013d 77 52 65 74 20 3d 20 47 65 74 44 69 73 6b 46 72 wRet = GetDiskFr
1013e 65 65 53 70 61 63 65 57 28 28 57 43 48 41 52 2a eeSpaceW((WCHAR*
1013f 29 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20 20 20 )zConverted,.
10140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10141 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 &
10142 64 77 44 75 6d 6d 79 2c 0a 20 20 20 20 20 20 20 dwDummy,.
10143 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10144 20 20 20 20 20 20 20 20 20 20 20 26 62 79 74 65 &byte
10145 73 50 65 72 53 65 63 74 6f 72 2c 0a 20 20 20 20 sPerSector,.
10146 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10147 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 64 &d
10148 77 44 75 6d 6d 79 2c 0a 20 20 20 20 20 20 20 20 wDummy,.
10149 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1014a 20 20 20 20 20 20 20 20 20 20 26 64 77 44 75 6d &dwDum
1014b 6d 79 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 my);. }else
1014c 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 74 72 69 {. /* tri
1014d 6d 20 70 61 74 68 20 74 6f 20 6a 75 73 74 20 64 m path to just d
1014e 72 69 76 65 20 72 65 66 65 72 65 6e 63 65 20 2a rive reference *
1014f 2f 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a /. char *
10150 70 20 3d 20 28 63 68 61 72 20 2a 29 7a 43 6f 6e p = (char *)zCon
10151 76 65 72 74 65 64 3b 0a 20 20 20 20 20 20 20 20 verted;.
10152 66 6f 72 28 3b 2a 70 3b 70 2b 2b 29 7b 0a 20 20 for(;*p;p++){.
10153 20 20 20 20 20 20 20 20 69 66 28 20 2a 70 20 3d if( *p =
10154 3d 20 27 5c 5c 27 20 29 7b 0a 20 20 20 20 20 20 = '\\' ){.
10155 20 20 20 20 20 20 2a 70 20 3d 20 27 5c 30 27 3b *p = '\0';
10156 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 . bre
10157 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a ak;. }.
10158 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
10159 20 20 64 77 52 65 74 20 3d 20 47 65 74 44 69 73 dwRet = GetDis
1015a 6b 46 72 65 65 53 70 61 63 65 41 28 28 63 68 61 kFreeSpaceA((cha
1015b 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20 r*)zConverted,.
1015c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1015d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1015e 20 26 64 77 44 75 6d 6d 79 2c 0a 20 20 20 20 20 &dwDummy,.
1015f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10160 20 20 20 20 20 20 20 20 20 20 20 20 20 26 62 79 &by
10161 74 65 73 50 65 72 53 65 63 74 6f 72 2c 0a 20 20 tesPerSector,.
10162 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10163 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10164 26 64 77 44 75 6d 6d 79 2c 0a 20 20 20 20 20 20 &dwDummy,.
10165 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10166 20 20 20 20 20 20 20 20 20 20 20 20 26 64 77 44 &dwD
10167 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 7d 0a 20 ummy);. }.
10168 20 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 free(zConve
10169 72 74 65 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 rted);. }.
1016a 20 69 66 28 20 21 64 77 52 65 74 20 29 7b 0a 20 if( !dwRet ){.
1016b 20 20 20 20 20 62 79 74 65 73 50 65 72 53 65 63 bytesPerSec
1016c 74 6f 72 20 3d 20 53 51 4c 49 54 45 5f 44 45 46 tor = SQLITE_DEF
1016d 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 AULT_SECTOR_SIZE
1016e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 ;. }. }.#end
1016f 69 66 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 if. return (int
10170 29 20 62 79 74 65 73 50 65 72 53 65 63 74 6f 72 ) bytesPerSector
10171 3b 20 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 ; .}..#ifndef SQ
10172 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 LITE_OMIT_LOAD_E
10173 58 54 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 XTENSION./*.** I
10174 6e 74 65 72 66 61 63 65 73 20 66 6f 72 20 6f 70 nterfaces for op
10175 65 6e 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c ening a shared l
10176 69 62 72 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 ibrary, finding
10177 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 entry points.**
10178 77 69 74 68 69 6e 20 74 68 65 20 73 68 61 72 65 within the share
10179 64 20 6c 69 62 72 61 72 79 2c 20 61 6e 64 20 63 d library, and c
1017a 6c 6f 73 69 6e 67 20 74 68 65 20 73 68 61 72 65 losing the share
1017b 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 2f 2a d library..*/./*
1017c 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 73 20 66 .** Interfaces f
1017d 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20 73 68 61 or opening a sha
1017e 72 65 64 20 6c 69 62 72 61 72 79 2c 20 66 69 6e red library, fin
1017f 64 69 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e 74 ding entry point
10180 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20 s.** within the
10181 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 shared library,
10182 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 and closing the
10183 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2e 0a shared library..
10184 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a */.static void *
10185 77 69 6e 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 winDlOpen(sqlite
10186 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 3_vfs *pVfs, con
10187 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 st char *zFilena
10188 6d 65 29 7b 0a 20 20 48 41 4e 44 4c 45 20 68 3b me){. HANDLE h;
10189 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 . void *zConver
1018a 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 ted = convertUtf
1018b 38 46 69 6c 65 6e 61 6d 65 28 7a 46 69 6c 65 6e 8Filename(zFilen
1018c 61 6d 65 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 ame);. UNUSED_P
1018d 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a ARAMETER(pVfs);.
1018e 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 64 if( zConverted
1018f 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
10190 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 n 0;. }. if( i
10191 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 68 20 3d sNT() ){. h =
10192 20 4c 6f 61 64 4c 69 62 72 61 72 79 57 28 28 57 LoadLibraryW((W
10193 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 CHAR*)zConverted
10194 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 );./* isNT() is
10195 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 1 if SQLITE_OS_W
10196 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 INCE==1, so this
10197 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 else is never e
10198 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e xecuted. .** Sin
10199 63 65 20 74 68 65 20 41 53 43 49 49 20 76 65 72 ce the ASCII ver
1019a 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69 sion of these Wi
1019b 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 ndows API do not
1019c 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45 exist for WINCE
1019d 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 ,.** it's import
1019e 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 ant to not refer
1019f 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49 ence them for WI
101a0 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 NCE builds..*/.#
101a1 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
101a2 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 CE==0. }else{.
101a3 20 20 20 68 20 3d 20 4c 6f 61 64 4c 69 62 72 61 h = LoadLibra
101a4 72 79 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 ryA((char*)zConv
101a5 65 72 74 65 64 29 3b 0a 23 65 6e 64 69 66 0a 20 erted);.#endif.
101a6 20 7d 0a 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 }. free(zConve
101a7 72 74 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 rted);. return
101a8 28 76 6f 69 64 2a 29 68 3b 0a 7d 0a 73 74 61 74 (void*)h;.}.stat
101a9 69 63 20 76 6f 69 64 20 77 69 6e 44 6c 45 72 72 ic void winDlErr
101aa 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a or(sqlite3_vfs *
101ab 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 pVfs, int nBuf,
101ac 63 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a char *zBufOut){.
101ad 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
101ae 45 52 28 70 56 66 73 29 3b 0a 20 20 67 65 74 4c ER(pVfs);. getL
101af 61 73 74 45 72 72 6f 72 4d 73 67 28 6e 42 75 66 astErrorMsg(nBuf
101b0 2c 20 7a 42 75 66 4f 75 74 29 3b 0a 7d 0a 76 6f , zBufOut);.}.vo
101b1 69 64 20 28 2a 77 69 6e 44 6c 53 79 6d 28 73 71 id (*winDlSym(sq
101b2 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
101b3 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65 2c 20 void *pHandle,
101b4 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 79 6d const char *zSym
101b5 62 6f 6c 29 29 28 76 6f 69 64 29 7b 0a 20 20 55 bol))(void){. U
101b6 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
101b7 70 56 66 73 29 3b 0a 23 69 66 20 53 51 4c 49 54 pVfs);.#if SQLIT
101b8 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 2f 2a 20 E_OS_WINCE. /*
101b9 54 68 65 20 47 65 74 50 72 6f 63 41 64 64 72 65 The GetProcAddre
101ba 73 73 41 28 29 20 72 6f 75 74 69 6e 65 20 69 73 ssA() routine is
101bb 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 only available
101bc 6f 6e 20 77 69 6e 63 65 2e 20 2a 2f 0a 20 20 72 on wince. */. r
101bd 65 74 75 72 6e 20 28 76 6f 69 64 28 2a 29 28 76 eturn (void(*)(v
101be 6f 69 64 29 29 47 65 74 50 72 6f 63 41 64 64 72 oid))GetProcAddr
101bf 65 73 73 41 28 28 48 41 4e 44 4c 45 29 70 48 61 essA((HANDLE)pHa
101c0 6e 64 6c 65 2c 20 7a 53 79 6d 62 6f 6c 29 3b 0a ndle, zSymbol);.
101c1 23 65 6c 73 65 0a 20 20 2f 2a 20 41 6c 6c 20 6f #else. /* All o
101c2 74 68 65 72 20 77 69 6e 64 6f 77 73 20 70 6c 61 ther windows pla
101c3 74 66 6f 72 6d 73 20 65 78 70 65 63 74 20 47 65 tforms expect Ge
101c4 74 50 72 6f 63 41 64 64 72 65 73 73 28 29 20 74 tProcAddress() t
101c5 6f 20 74 61 6b 65 0a 20 20 2a 2a 20 61 6e 20 41 o take. ** an A
101c6 6e 73 69 20 73 74 72 69 6e 67 20 72 65 67 61 72 nsi string regar
101c7 64 6c 65 73 73 20 6f 66 20 74 68 65 20 5f 55 4e dless of the _UN
101c8 49 43 4f 44 45 20 73 65 74 74 69 6e 67 20 2a 2f ICODE setting */
101c9 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 28 . return (void(
101ca 2a 29 28 76 6f 69 64 29 29 47 65 74 50 72 6f 63 *)(void))GetProc
101cb 41 64 64 72 65 73 73 28 28 48 41 4e 44 4c 45 29 Address((HANDLE)
101cc 70 48 61 6e 64 6c 65 2c 20 7a 53 79 6d 62 6f 6c pHandle, zSymbol
101cd 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 76 6f 69 64 );.#endif.}.void
101ce 20 77 69 6e 44 6c 43 6c 6f 73 65 28 73 71 6c 69 winDlClose(sqli
101cf 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 te3_vfs *pVfs, v
101d0 6f 69 64 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 oid *pHandle){.
101d1 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
101d2 52 28 70 56 66 73 29 3b 0a 20 20 46 72 65 65 4c R(pVfs);. FreeL
101d3 69 62 72 61 72 79 28 28 48 41 4e 44 4c 45 29 70 ibrary((HANDLE)p
101d4 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6c 73 65 Handle);.}.#else
101d5 20 2f 2a 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d /* if SQLITE_OM
101d6 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f IT_LOAD_EXTENSIO
101d7 4e 20 69 73 20 64 65 66 69 6e 65 64 3a 20 2a 2f N is defined: */
101d8 0a 20 20 23 64 65 66 69 6e 65 20 77 69 6e 44 6c . #define winDl
101d9 4f 70 65 6e 20 20 30 0a 20 20 23 64 65 66 69 6e Open 0. #defin
101da 65 20 77 69 6e 44 6c 45 72 72 6f 72 20 30 0a 20 e winDlError 0.
101db 20 23 64 65 66 69 6e 65 20 77 69 6e 44 6c 53 79 #define winDlSy
101dc 6d 20 20 20 30 0a 20 20 23 64 65 66 69 6e 65 20 m 0. #define
101dd 77 69 6e 44 6c 43 6c 6f 73 65 20 30 0a 23 65 6e winDlClose 0.#en
101de 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 dif.../*.** Writ
101df 65 20 75 70 20 74 6f 20 6e 42 75 66 20 62 79 74 e up to nBuf byt
101e0 65 73 20 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 es of randomness
101e1 20 69 6e 74 6f 20 7a 42 75 66 2e 0a 2a 2f 0a 73 into zBuf..*/.s
101e2 74 61 74 69 63 20 69 6e 74 20 77 69 6e 52 61 6e tatic int winRan
101e3 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f domness(sqlite3_
101e4 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e vfs *pVfs, int n
101e5 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 Buf, char *zBuf)
101e6 7b 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 {. int n = 0;.
101e7 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
101e8 52 28 70 56 66 73 29 3b 0a 23 69 66 20 64 65 66 R(pVfs);.#if def
101e9 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 ined(SQLITE_TEST
101ea 29 0a 20 20 6e 20 3d 20 6e 42 75 66 3b 0a 20 20 ). n = nBuf;.
101eb 6d 65 6d 73 65 74 28 7a 42 75 66 2c 20 30 2c 20 memset(zBuf, 0,
101ec 6e 42 75 66 29 3b 0a 23 65 6c 73 65 0a 20 20 69 nBuf);.#else. i
101ed 66 28 20 73 69 7a 65 6f 66 28 53 59 53 54 45 4d f( sizeof(SYSTEM
101ee 54 49 4d 45 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b TIME)<=nBuf-n ){
101ef 0a 20 20 20 20 53 59 53 54 45 4d 54 49 4d 45 20 . SYSTEMTIME
101f0 78 3b 0a 20 20 20 20 47 65 74 53 79 73 74 65 6d x;. GetSystem
101f1 54 69 6d 65 28 26 78 29 3b 0a 20 20 20 20 6d 65 Time(&x);. me
101f2 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 mcpy(&zBuf[n], &
101f3 78 2c 20 73 69 7a 65 6f 66 28 78 29 29 3b 0a 20 x, sizeof(x));.
101f4 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 78 n += sizeof(x
101f5 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 69 7a );. }. if( siz
101f6 65 6f 66 28 44 57 4f 52 44 29 3c 3d 6e 42 75 66 eof(DWORD)<=nBuf
101f7 2d 6e 20 29 7b 0a 20 20 20 20 44 57 4f 52 44 20 -n ){. DWORD
101f8 70 69 64 20 3d 20 47 65 74 43 75 72 72 65 6e 74 pid = GetCurrent
101f9 50 72 6f 63 65 73 73 49 64 28 29 3b 0a 20 20 20 ProcessId();.
101fa 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d memcpy(&zBuf[n]
101fb 2c 20 26 70 69 64 2c 20 73 69 7a 65 6f 66 28 70 , &pid, sizeof(p
101fc 69 64 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 id));. n += s
101fd 69 7a 65 6f 66 28 70 69 64 29 3b 0a 20 20 7d 0a izeof(pid);. }.
101fe 20 20 69 66 28 20 73 69 7a 65 6f 66 28 44 57 4f if( sizeof(DWO
101ff 52 44 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 RD)<=nBuf-n ){.
10200 20 20 20 44 57 4f 52 44 20 63 6e 74 20 3d 20 47 DWORD cnt = G
10201 65 74 54 69 63 6b 43 6f 75 6e 74 28 29 3b 0a 20 etTickCount();.
10202 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b memcpy(&zBuf[
10203 6e 5d 2c 20 26 63 6e 74 2c 20 73 69 7a 65 6f 66 n], &cnt, sizeof
10204 28 63 6e 74 29 29 3b 0a 20 20 20 20 6e 20 2b 3d (cnt));. n +=
10205 20 73 69 7a 65 6f 66 28 63 6e 74 29 3b 0a 20 20 sizeof(cnt);.
10206 7d 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 28 4c }. if( sizeof(L
10207 41 52 47 45 5f 49 4e 54 45 47 45 52 29 3c 3d 6e ARGE_INTEGER)<=n
10208 42 75 66 2d 6e 20 29 7b 0a 20 20 20 20 4c 41 52 Buf-n ){. LAR
10209 47 45 5f 49 4e 54 45 47 45 52 20 69 3b 0a 20 20 GE_INTEGER i;.
1020a 20 20 51 75 65 72 79 50 65 72 66 6f 72 6d 61 6e QueryPerforman
1020b 63 65 43 6f 75 6e 74 65 72 28 26 69 29 3b 0a 20 ceCounter(&i);.
1020c 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b memcpy(&zBuf[
1020d 6e 5d 2c 20 26 69 2c 20 73 69 7a 65 6f 66 28 69 n], &i, sizeof(i
1020e 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a ));. n += siz
1020f 65 6f 66 28 69 29 3b 0a 20 20 7d 0a 23 65 6e 64 eof(i);. }.#end
10210 69 66 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d if. return n;.}
10211 0a 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 .../*.** Sleep f
10212 6f 72 20 61 20 6c 69 74 74 6c 65 20 77 68 69 6c or a little whil
10213 65 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 61 e. Return the a
10214 6d 6f 75 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c mount of time sl
10215 65 70 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ept..*/.static i
10216 6e 74 20 77 69 6e 53 6c 65 65 70 28 73 71 6c 69 nt winSleep(sqli
10217 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 te3_vfs *pVfs, i
10218 6e 74 20 6d 69 63 72 6f 73 65 63 29 7b 0a 20 20 nt microsec){.
10219 53 6c 65 65 70 28 28 6d 69 63 72 6f 73 65 63 2b Sleep((microsec+
1021a 39 39 39 29 2f 31 30 30 30 29 3b 0a 20 20 55 4e 999)/1000);. UN
1021b 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 USED_PARAMETER(p
1021c 56 66 73 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 Vfs);. return (
1021d 28 6d 69 63 72 6f 73 65 63 2b 39 39 39 29 2f 31 (microsec+999)/1
1021e 30 30 30 29 2a 31 30 30 30 3b 0a 7d 0a 0a 2f 2a 000)*1000;.}../*
1021f 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
10220 67 20 76 61 72 69 61 62 6c 65 2c 20 69 66 20 73 g variable, if s
10221 65 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f et to a non-zero
10222 20 76 61 6c 75 65 2c 20 62 65 63 6f 6d 65 73 20 value, becomes
10223 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 72 65 the result.** re
10224 74 75 72 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 turned from sqli
10225 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 te3OsCurrentTime
10226 28 29 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 (). This is use
10227 64 20 66 6f 72 20 74 65 73 74 69 6e 67 2e 0a 2a d for testing..*
10228 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
10229 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 TEST.SQLITE_API
1022a 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 75 72 72 int sqlite3_curr
1022b 65 6e 74 5f 74 69 6d 65 20 3d 20 30 3b 0a 23 65 ent_time = 0;.#e
1022c 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 ndif../*.** Find
1022d 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 69 6d the current tim
1022e 65 20 28 69 6e 20 55 6e 69 76 65 72 73 61 6c 20 e (in Universal
1022f 43 6f 6f 72 64 69 6e 61 74 65 64 20 54 69 6d 65 Coordinated Time
10230 29 2e 20 20 57 72 69 74 65 20 74 68 65 0a 2a 2a ). Write the.**
10231 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 61 6e current time an
10232 64 20 64 61 74 65 20 61 73 20 61 20 4a 75 6c 69 d date as a Juli
10233 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 20 69 6e an Day number in
10234 74 6f 20 2a 70 72 4e 6f 77 20 61 6e 64 0a 2a 2a to *prNow and.**
10235 20 72 65 74 75 72 6e 20 30 2e 20 20 52 65 74 75 return 0. Retu
10236 72 6e 20 31 20 69 66 20 74 68 65 20 74 69 6d 65 rn 1 if the time
10237 20 61 6e 64 20 64 61 74 65 20 63 61 6e 6e 6f 74 and date cannot
10238 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 69 6e be found..*/.in
10239 74 20 77 69 6e 43 75 72 72 65 6e 74 54 69 6d 65 t winCurrentTime
1023a 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 (sqlite3_vfs *pV
1023b 66 73 2c 20 64 6f 75 62 6c 65 20 2a 70 72 4e 6f fs, double *prNo
1023c 77 29 7b 0a 20 20 46 49 4c 45 54 49 4d 45 20 66 w){. FILETIME f
1023d 74 3b 0a 20 20 2f 2a 20 46 49 4c 45 54 49 4d 45 t;. /* FILETIME
1023e 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 20 structure is a
1023f 36 34 2d 62 69 74 20 76 61 6c 75 65 20 72 65 70 64-bit value rep
10240 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 6e 75 resenting the nu
10241 6d 62 65 72 20 6f 66 20 0a 20 20 20 20 20 31 30 mber of . 10
10242 30 2d 6e 61 6e 6f 73 65 63 6f 6e 64 20 69 6e 74 0-nanosecond int
10243 65 72 76 61 6c 73 20 73 69 6e 63 65 20 4a 61 6e ervals since Jan
10244 75 61 72 79 20 31 2c 20 31 36 30 31 20 28 3d 20 uary 1, 1601 (=
10245 4a 44 20 32 33 30 35 38 31 33 2e 35 29 2e 20 0a JD 2305813.5). .
10246 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 */. sqlite3_i
10247 6e 74 36 34 20 74 69 6d 65 57 3b 20 20 20 2f 2a nt64 timeW; /*
10248 20 57 68 6f 6c 65 20 64 61 79 73 20 2a 2f 0a 20 Whole days */.
10249 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 74 sqlite3_int64 t
1024a 69 6d 65 46 3b 20 20 20 2f 2a 20 46 72 61 63 74 imeF; /* Fract
1024b 69 6f 6e 61 6c 20 44 61 79 73 20 2a 2f 0a 0a 20 ional Days */..
1024c 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 31 30 /* Number of 10
1024d 30 2d 6e 61 6e 6f 73 65 63 6f 6e 64 20 69 6e 74 0-nanosecond int
1024e 65 72 76 61 6c 73 20 69 6e 20 61 20 73 69 6e 67 ervals in a sing
1024f 6c 65 20 64 61 79 20 2a 2f 0a 20 20 73 74 61 74 le day */. stat
10250 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 ic const sqlite3
10251 5f 69 6e 74 36 34 20 6e 74 75 50 65 72 44 61 79 _int64 ntuPerDay
10252 20 3d 20 0a 20 20 20 20 20 20 31 30 30 30 30 30 = . 100000
10253 30 30 2a 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 00*(sqlite3_int6
10254 34 29 38 36 34 30 30 3b 0a 0a 20 20 2f 2a 20 4e 4)86400;.. /* N
10255 75 6d 62 65 72 20 6f 66 20 31 30 30 2d 6e 61 6e umber of 100-nan
10256 6f 73 65 63 6f 6e 64 20 69 6e 74 65 72 76 61 6c osecond interval
10257 73 20 69 6e 20 68 61 6c 66 20 6f 66 20 61 20 64 s in half of a d
10258 61 79 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 ay */. static c
10259 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6e 74 onst sqlite3_int
1025a 36 34 20 6e 74 75 50 65 72 48 61 6c 66 44 61 79 64 ntuPerHalfDay
1025b 20 3d 20 0a 20 20 20 20 20 20 31 30 30 30 30 30 = . 100000
1025c 30 30 2a 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 00*(sqlite3_int6
1025d 34 29 34 33 32 30 30 3b 0a 0a 20 20 2f 2a 20 32 4)43200;.. /* 2
1025e 5e 33 32 20 2d 20 74 6f 20 61 76 6f 69 64 20 75 ^32 - to avoid u
1025f 73 65 20 6f 66 20 4c 4c 20 61 6e 64 20 77 61 72 se of LL and war
10260 6e 69 6e 67 73 20 69 6e 20 67 63 63 20 2a 2f 0a nings in gcc */.
10261 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 static const s
10262 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6d 61 78 qlite3_int64 max
10263 33 32 42 69 74 56 61 6c 75 65 20 3d 20 0a 20 20 32BitValue = .
10264 20 20 20 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 (sqlite3_int
10265 36 34 29 32 30 30 30 30 30 30 30 30 30 20 2b 20 64)2000000000 +
10266 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 32 (sqlite3_int64)2
10267 30 30 30 30 30 30 30 30 30 20 2b 20 28 73 71 6c 000000000 + (sql
10268 69 74 65 33 5f 69 6e 74 36 34 29 32 39 34 39 36 ite3_int64)29496
10269 37 32 39 36 3b 0a 0a 23 69 66 20 53 51 4c 49 54 7296;..#if SQLIT
1026a 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 53 59 53 E_OS_WINCE. SYS
1026b 54 45 4d 54 49 4d 45 20 74 69 6d 65 3b 0a 20 20 TEMTIME time;.
1026c 47 65 74 53 79 73 74 65 6d 54 69 6d 65 28 26 74 GetSystemTime(&t
1026d 69 6d 65 29 3b 0a 20 20 2f 2a 20 69 66 20 53 79 ime);. /* if Sy
1026e 73 74 65 6d 54 69 6d 65 54 6f 46 69 6c 65 54 69 stemTimeToFileTi
1026f 6d 65 28 29 20 66 61 69 6c 73 2c 20 69 74 20 72 me() fails, it r
10270 65 74 75 72 6e 73 20 7a 65 72 6f 2e 20 2a 2f 0a eturns zero. */.
10271 20 20 69 66 20 28 21 53 79 73 74 65 6d 54 69 6d if (!SystemTim
10272 65 54 6f 46 69 6c 65 54 69 6d 65 28 26 74 69 6d eToFileTime(&tim
10273 65 2c 26 66 74 29 29 7b 0a 20 20 20 20 72 65 74 e,&ft)){. ret
10274 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6c 73 65 urn 1;. }.#else
10275 0a 20 20 47 65 74 53 79 73 74 65 6d 54 69 6d 65 . GetSystemTime
10276 41 73 46 69 6c 65 54 69 6d 65 28 20 26 66 74 20 AsFileTime( &ft
10277 29 3b 0a 23 65 6e 64 69 66 0a 20 20 55 4e 55 53 );.#endif. UNUS
10278 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 ED_PARAMETER(pVf
10279 73 29 3b 0a 20 20 74 69 6d 65 57 20 3d 20 28 28 s);. timeW = ((
1027a 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 66 (sqlite3_int64)f
1027b 74 2e 64 77 48 69 67 68 44 61 74 65 54 69 6d 65 t.dwHighDateTime
1027c 29 2a 6d 61 78 33 32 42 69 74 56 61 6c 75 65 29 )*max32BitValue)
1027d 20 2b 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 + (sqlite3_int6
1027e 34 29 66 74 2e 64 77 4c 6f 77 44 61 74 65 54 69 4)ft.dwLowDateTi
1027f 6d 65 3b 0a 20 20 74 69 6d 65 46 20 3d 20 74 69 me;. timeF = ti
10280 6d 65 57 20 25 20 6e 74 75 50 65 72 44 61 79 3b meW % ntuPerDay;
10281 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 72 61 /* fra
10282 63 74 69 6f 6e 61 6c 20 64 61 79 73 20 28 31 30 ctional days (10
10283 30 2d 6e 61 6e 6f 73 65 63 6f 6e 64 73 29 20 2a 0-nanoseconds) *
10284 2f 0a 20 20 74 69 6d 65 57 20 3d 20 74 69 6d 65 /. timeW = time
10285 57 20 2f 20 6e 74 75 50 65 72 44 61 79 3b 20 20 W / ntuPerDay;
10286 20 20 20 20 20 20 20 20 2f 2a 20 77 68 6f 6c 65 /* whole
10287 20 64 61 79 73 20 2a 2f 0a 20 20 74 69 6d 65 57 days */. timeW
10288 20 3d 20 74 69 6d 65 57 20 2b 20 32 33 30 35 38 = timeW + 23058
10289 31 33 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 13; /
1028a 2a 20 61 64 64 20 77 68 6f 6c 65 20 64 61 79 73 * add whole days
1028b 20 28 66 72 6f 6d 20 32 33 30 35 38 31 33 2e 35 (from 2305813.5
1028c 29 20 2a 2f 0a 20 20 74 69 6d 65 46 20 3d 20 74 ) */. timeF = t
1028d 69 6d 65 46 20 2b 20 6e 74 75 50 65 72 48 61 6c imeF + ntuPerHal
1028e 66 44 61 79 3b 20 20 20 20 20 20 2f 2a 20 61 64 fDay; /* ad
1028f 64 20 68 61 6c 66 20 61 20 64 61 79 20 28 66 72 d half a day (fr
10290 6f 6d 20 32 33 30 35 38 31 33 2e 35 29 20 2a 2f om 2305813.5) */
10291 0a 20 20 74 69 6d 65 57 20 3d 20 74 69 6d 65 57 . timeW = timeW
10292 20 2b 20 28 74 69 6d 65 46 2f 6e 74 75 50 65 72 + (timeF/ntuPer
10293 44 61 79 29 3b 20 20 2f 2a 20 61 64 64 20 77 68 Day); /* add wh
10294 6f 6c 65 20 64 61 79 20 69 66 20 68 61 6c 66 20 ole day if half
10295 64 61 79 20 6d 61 64 65 20 6f 6e 65 20 2a 2f 0a day made one */.
10296 20 20 74 69 6d 65 46 20 3d 20 74 69 6d 65 46 20 timeF = timeF
10297 25 20 6e 74 75 50 65 72 44 61 79 3b 20 20 20 20 % ntuPerDay;
10298 20 20 20 20 20 20 2f 2a 20 63 6f 6d 70 75 74 65 /* compute
10299 20 6e 65 77 20 66 72 61 63 74 69 6f 6e 61 6c 20 new fractional
1029a 64 61 79 73 20 2a 2f 0a 20 20 2a 70 72 4e 6f 77 days */. *prNow
1029b 20 3d 20 28 64 6f 75 62 6c 65 29 74 69 6d 65 57 = (double)timeW
1029c 20 2b 20 28 28 64 6f 75 62 6c 65 29 74 69 6d 65 + ((double)time
1029d 46 20 2f 20 28 64 6f 75 62 6c 65 29 6e 74 75 50 F / (double)ntuP
1029e 65 72 44 61 79 29 3b 0a 23 69 66 64 65 66 20 53 erDay);.#ifdef S
1029f 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28 QLITE_TEST. if(
102a0 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 sqlite3_current
102a1 5f 74 69 6d 65 20 29 7b 0a 20 20 20 20 2a 70 72 _time ){. *pr
102a2 4e 6f 77 20 3d 20 28 28 64 6f 75 62 6c 65 29 73 Now = ((double)s
102a3 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 qlite3_current_t
102a4 69 6d 65 20 2b 20 28 64 6f 75 62 6c 65 29 34 33 ime + (double)43
102a5 32 30 30 29 20 2f 20 28 64 6f 75 62 6c 65 29 38 200) / (double)8
102a6 36 34 30 30 20 2b 20 28 64 6f 75 62 6c 65 29 32 6400 + (double)2
102a7 34 34 30 35 38 37 3b 0a 20 20 7d 0a 23 65 6e 64 440587;. }.#end
102a8 69 66 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d if. return 0;.}
102a9 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 64 65 61 ../*.** The idea
102aa 20 69 73 20 74 68 61 74 20 74 68 69 73 20 66 75 is that this fu
102ab 6e 63 74 69 6f 6e 20 77 6f 72 6b 73 20 6c 69 6b nction works lik
102ac 65 20 61 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 e a combination
102ad 6f 66 0a 2a 2a 20 47 65 74 4c 61 73 74 45 72 72 of.** GetLastErr
102ae 6f 72 28 29 20 61 6e 64 20 46 6f 72 6d 61 74 4d or() and FormatM
102af 65 73 73 61 67 65 28 29 20 6f 6e 20 77 69 6e 64 essage() on wind
102b0 6f 77 73 20 28 6f 72 20 65 72 72 6e 6f 20 61 6e ows (or errno an
102b1 64 0a 2a 2a 20 73 74 72 65 72 72 6f 72 5f 72 28 d.** strerror_r(
102b2 29 20 6f 6e 20 75 6e 69 78 29 2e 20 41 66 74 65 ) on unix). Afte
102b3 72 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 r an error is re
102b4 74 75 72 6e 65 64 20 62 79 20 61 6e 20 4f 53 0a turned by an OS.
102b5 2a 2a 20 66 75 6e 63 74 69 6f 6e 2c 20 53 51 4c ** function, SQL
102b6 69 74 65 20 63 61 6c 6c 73 20 74 68 69 73 20 66 ite calls this f
102b7 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 7a 42 75 unction with zBu
102b8 66 20 70 6f 69 6e 74 69 6e 67 20 74 6f 0a 2a 2a f pointing to.**
102b9 20 61 20 62 75 66 66 65 72 20 6f 66 20 6e 42 75 a buffer of nBu
102ba 66 20 62 79 74 65 73 2e 20 54 68 65 20 4f 53 20 f bytes. The OS
102bb 6c 61 79 65 72 20 73 68 6f 75 6c 64 20 70 6f 70 layer should pop
102bc 75 6c 61 74 65 20 74 68 65 0a 2a 2a 20 62 75 66 ulate the.** buf
102bd 66 65 72 20 77 69 74 68 20 61 20 6e 75 6c 2d 74 fer with a nul-t
102be 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 erminated UTF-8
102bf 65 6e 63 6f 64 65 64 20 65 72 72 6f 72 20 6d 65 encoded error me
102c0 73 73 61 67 65 0a 2a 2a 20 64 65 73 63 72 69 62 ssage.** describ
102c1 69 6e 67 20 74 68 65 20 6c 61 73 74 20 49 4f 20 ing the last IO
102c2 65 72 72 6f 72 20 74 6f 20 68 61 76 65 20 6f 63 error to have oc
102c3 63 75 72 72 65 64 20 77 69 74 68 69 6e 20 74 68 curred within th
102c4 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 72 e calling.** thr
102c5 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ead..**.** If th
102c6 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 e error message
102c7 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 66 6f 72 is too large for
102c8 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 62 75 the supplied bu
102c9 66 66 65 72 2c 0a 2a 2a 20 69 74 20 73 68 6f 75 ffer,.** it shou
102ca 6c 64 20 62 65 20 74 72 75 6e 63 61 74 65 64 2e ld be truncated.
102cb 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 The return valu
102cc 65 20 6f 66 20 78 47 65 74 4c 61 73 74 45 72 72 e of xGetLastErr
102cd 6f 72 0a 2a 2a 20 69 73 20 7a 65 72 6f 20 69 66 or.** is zero if
102ce 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 the error messa
102cf 67 65 20 66 69 74 73 20 69 6e 20 74 68 65 20 62 ge fits in the b
102d0 75 66 66 65 72 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 uffer, or non-ze
102d1 72 6f 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65 20 ro.** otherwise
102d2 28 69 66 20 74 68 65 20 6d 65 73 73 61 67 65 20 (if the message
102d3 77 61 73 20 74 72 75 6e 63 61 74 65 64 29 2e 20 was truncated).
102d4 49 66 20 6e 6f 6e 2d 7a 65 72 6f 20 69 73 20 72 If non-zero is r
102d5 65 74 75 72 6e 65 64 2c 0a 2a 2a 20 74 68 65 6e eturned,.** then
102d6 20 69 74 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 it is not neces
102d7 73 61 72 79 20 74 6f 20 69 6e 63 6c 75 64 65 20 sary to include
102d8 74 68 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 the nul-terminat
102d9 6f 72 20 63 68 61 72 61 63 74 65 72 0a 2a 2a 20 or character.**
102da 69 6e 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 in the output bu
102db 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 20 ffer..**.** Not
102dc 73 75 70 70 6c 79 69 6e 67 20 61 6e 20 65 72 72 supplying an err
102dd 6f 72 20 6d 65 73 73 61 67 65 20 77 69 6c 6c 20 or message will
102de 68 61 76 65 20 6e 6f 20 61 64 76 65 72 73 65 20 have no adverse
102df 65 66 66 65 63 74 0a 2a 2a 20 6f 6e 20 53 51 4c effect.** on SQL
102e0 69 74 65 2e 20 49 74 20 69 73 20 66 69 6e 65 20 ite. It is fine
102e1 74 6f 20 68 61 76 65 20 61 6e 20 69 6d 70 6c 65 to have an imple
102e2 6d 65 6e 74 61 74 69 6f 6e 20 74 68 61 74 20 6e mentation that n
102e3 65 76 65 72 0a 2a 2a 20 72 65 74 75 72 6e 73 20 ever.** returns
102e4 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 an error message
102e5 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 6e 74 20 78 47 :.**.** int xG
102e6 65 74 4c 61 73 74 45 72 72 6f 72 28 73 71 6c 69 etLastError(sqli
102e7 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 te3_vfs *pVfs, i
102e8 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a nt nBuf, char *z
102e9 42 75 66 29 7b 0a 2a 2a 20 20 20 20 20 61 73 73 Buf){.** ass
102ea 65 72 74 28 7a 42 75 66 5b 30 5d 3d 3d 27 5c 30 ert(zBuf[0]=='\0
102eb 27 29 3b 0a 2a 2a 20 20 20 20 20 72 65 74 75 72 ');.** retur
102ec 6e 20 30 3b 0a 2a 2a 20 20 20 7d 0a 2a 2a 0a 2a n 0;.** }.**.*
102ed 2a 20 48 6f 77 65 76 65 72 20 69 66 20 61 6e 20 * However if an
102ee 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 73 error message is
102ef 20 73 75 70 70 6c 69 65 64 2c 20 69 74 20 77 69 supplied, it wi
102f0 6c 6c 20 62 65 20 69 6e 63 6f 72 70 6f 72 61 74 ll be incorporat
102f1 65 64 0a 2a 2a 20 62 79 20 73 71 6c 69 74 65 20 ed.** by sqlite
102f2 69 6e 74 6f 20 74 68 65 20 65 72 72 6f 72 20 6d into the error m
102f3 65 73 73 61 67 65 20 61 76 61 69 6c 61 62 6c 65 essage available
102f4 20 74 6f 20 74 68 65 20 75 73 65 72 20 75 73 69 to the user usi
102f5 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 65 72 ng.** sqlite3_er
102f6 72 6d 73 67 28 29 2c 20 70 6f 73 73 69 62 6c 79 rmsg(), possibly
102f7 20 6d 61 6b 69 6e 67 20 49 4f 20 65 72 72 6f 72 making IO error
102f8 73 20 65 61 73 69 65 72 20 74 6f 20 64 65 62 75 s easier to debu
102f9 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 g..*/.static int
102fa 20 77 69 6e 47 65 74 4c 61 73 74 45 72 72 6f 72 winGetLastError
102fb 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 (sqlite3_vfs *pV
102fc 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 fs, int nBuf, ch
102fd 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 55 4e 55 ar *zBuf){. UNU
102fe 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 SED_PARAMETER(pV
102ff 66 73 29 3b 0a 20 20 72 65 74 75 72 6e 20 67 65 fs);. return ge
10300 74 4c 61 73 74 45 72 72 6f 72 4d 73 67 28 6e 42 tLastErrorMsg(nB
10301 75 66 2c 20 7a 42 75 66 29 3b 0a 7d 0a 0a 2f 2a uf, zBuf);.}../*
10302 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 .** Initialize a
10303 6e 64 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 nd deinitialize
10304 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 the operating sy
10305 73 74 65 6d 20 69 6e 74 65 72 66 61 63 65 2e 0a stem interface..
10306 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
10307 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 t sqlite3_os_ini
10308 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 t(void){. stati
10309 63 20 73 71 6c 69 74 65 33 5f 76 66 73 20 77 69 c sqlite3_vfs wi
1030a 6e 56 66 73 20 3d 20 7b 0a 20 20 20 20 31 2c 20 nVfs = {. 1,
1030b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1030c 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 /* iVersion */.
1030d 20 20 20 73 69 7a 65 6f 66 28 77 69 6e 46 69 6c sizeof(winFil
1030e 65 29 2c 20 20 20 2f 2a 20 73 7a 4f 73 46 69 6c e), /* szOsFil
1030f 65 20 2a 2f 0a 20 20 20 20 4d 41 58 5f 50 41 54 e */. MAX_PAT
10310 48 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d H, /* m
10311 78 50 61 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20 xPathname */.
10312 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
10313 20 20 20 20 2f 2a 20 70 4e 65 78 74 20 2a 2f 0a /* pNext */.
10314 20 20 20 20 22 77 69 6e 33 32 22 2c 20 20 20 20 "win32",
10315 20 20 20 20 20 20 20 2f 2a 20 7a 4e 61 6d 65 20 /* zName
10316 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 */. 0,
10317 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 41 70 /* pAp
10318 70 44 61 74 61 20 2a 2f 0a 20 0a 20 20 20 20 77 pData */. . w
10319 69 6e 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 inOpen,
1031a 20 20 2f 2a 20 78 4f 70 65 6e 20 2a 2f 0a 20 20 /* xOpen */.
1031b 20 20 77 69 6e 44 65 6c 65 74 65 2c 20 20 20 20 winDelete,
1031c 20 20 20 20 20 2f 2a 20 78 44 65 6c 65 74 65 20 /* xDelete
1031d 2a 2f 0a 20 20 20 20 77 69 6e 41 63 63 65 73 73 */. winAccess
1031e 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 41 63 , /* xAc
1031f 63 65 73 73 20 2a 2f 0a 20 20 20 20 77 69 6e 46 cess */. winF
10320 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 20 20 2f ullPathname, /
10321 2a 20 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 * xFullPathname
10322 2a 2f 0a 20 20 20 20 77 69 6e 44 6c 4f 70 65 6e */. winDlOpen
10323 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c , /* xDl
10324 4f 70 65 6e 20 2a 2f 0a 20 20 20 20 77 69 6e 44 Open */. winD
10325 6c 45 72 72 6f 72 2c 20 20 20 20 20 20 20 20 2f lError, /
10326 2a 20 78 44 6c 45 72 72 6f 72 20 2a 2f 0a 20 20 * xDlError */.
10327 20 20 77 69 6e 44 6c 53 79 6d 2c 20 20 20 20 20 winDlSym,
10328 20 20 20 20 20 2f 2a 20 78 44 6c 53 79 6d 20 2a /* xDlSym *
10329 2f 0a 20 20 20 20 77 69 6e 44 6c 43 6c 6f 73 65 /. winDlClose
1032a 2c 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 43 , /* xDlC
1032b 6c 6f 73 65 20 2a 2f 0a 20 20 20 20 77 69 6e 52 lose */. winR
1032c 61 6e 64 6f 6d 6e 65 73 73 2c 20 20 20 20 20 2f andomness, /
1032d 2a 20 78 52 61 6e 64 6f 6d 6e 65 73 73 20 2a 2f * xRandomness */
1032e 0a 20 20 20 20 77 69 6e 53 6c 65 65 70 2c 20 20 . winSleep,
1032f 20 20 20 20 20 20 20 20 2f 2a 20 78 53 6c 65 65 /* xSlee
10330 70 20 2a 2f 0a 20 20 20 20 77 69 6e 43 75 72 72 p */. winCurr
10331 65 6e 74 54 69 6d 65 2c 20 20 20 20 2f 2a 20 78 entTime, /* x
10332 43 75 72 72 65 6e 74 54 69 6d 65 20 2a 2f 0a 20 CurrentTime */.
10333 20 20 20 77 69 6e 47 65 74 4c 61 73 74 45 72 72 winGetLastErr
10334 6f 72 20 20 20 20 2f 2a 20 78 47 65 74 4c 61 73 or /* xGetLas
10335 74 45 72 72 6f 72 20 2a 2f 0a 20 20 7d 3b 0a 0a tError */. };..
10336 20 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 sqlite3_vfs_re
10337 67 69 73 74 65 72 28 26 77 69 6e 56 66 73 2c 20 gister(&winVfs,
10338 31 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 1);. return SQL
10339 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 53 51 4c 49 54 ITE_OK; .}.SQLIT
1033a 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1033b 33 5f 6f 73 5f 65 6e 64 28 76 6f 69 64 29 7b 20 3_os_end(void){
1033c 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
1033d 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f _OK;.}..#endif /
1033e 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 * SQLITE_OS_WIN
1033f 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
10340 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 77 69 *** End of os_wi
10341 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n.c ************
10342 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10343 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10344 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
10345 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 62 *** Begin file b
10346 69 74 76 65 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a itvec.c ********
10347 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10348 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10349 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 46 **/./*.** 2008 F
1034a 65 62 72 75 61 72 79 20 31 36 0a 2a 2a 0a 2a 2a ebruary 16.**.**
1034b 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
1034c 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
1034d 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
1034e 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
1034f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
10350 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
10351 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
10352 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
10353 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
10354 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
10355 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
10356 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
10357 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
10358 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
10359 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
1035a 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
1035b 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
1035c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1035d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1035e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1035f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10360 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 *********.** Thi
10361 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 s file implement
10362 73 20 61 6e 20 6f 62 6a 65 63 74 20 74 68 61 74 s an object that
10363 20 72 65 70 72 65 73 65 6e 74 73 20 61 20 66 69 represents a fi
10364 78 65 64 2d 6c 65 6e 67 74 68 0a 2a 2a 20 62 69 xed-length.** bi
10365 74 6d 61 70 2e 20 20 42 69 74 73 20 61 72 65 20 tmap. Bits are
10366 6e 75 6d 62 65 72 65 64 20 73 74 61 72 74 69 6e numbered startin
10367 67 20 77 69 74 68 20 31 2e 0a 2a 2a 0a 2a 2a 20 g with 1..**.**
10368 41 20 62 69 74 6d 61 70 20 69 73 20 75 73 65 64 A bitmap is used
10369 20 74 6f 20 72 65 63 6f 72 64 20 77 68 69 63 68 to record which
1036a 20 70 61 67 65 73 20 6f 66 20 61 20 64 61 74 61 pages of a data
1036b 62 61 73 65 20 66 69 6c 65 20 68 61 76 65 20 62 base file have b
1036c 65 65 6e 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 6c 65 een.** journalle
1036d 64 20 64 75 72 69 6e 67 20 61 20 74 72 61 6e 73 d during a trans
1036e 61 63 74 69 6f 6e 2c 20 6f 72 20 77 68 69 63 68 action, or which
1036f 20 70 61 67 65 73 20 68 61 76 65 20 74 68 65 20 pages have the
10370 22 64 6f 6e 74 2d 77 72 69 74 65 22 0a 2a 2a 20 "dont-write".**
10371 70 72 6f 70 65 72 74 79 2e 20 20 55 73 75 61 6c property. Usual
10372 6c 79 20 6f 6e 6c 79 20 61 20 66 65 77 20 70 61 ly only a few pa
10373 67 65 73 20 61 72 65 20 6d 65 65 74 20 65 69 74 ges are meet eit
10374 68 65 72 20 63 6f 6e 64 69 74 69 6f 6e 2e 0a 2a her condition..*
10375 2a 20 53 6f 20 74 68 65 20 62 69 74 6d 61 70 20 * So the bitmap
10376 69 73 20 75 73 75 61 6c 6c 79 20 73 70 61 72 73 is usually spars
10377 65 20 61 6e 64 20 68 61 73 20 6c 6f 77 20 63 61 e and has low ca
10378 72 64 69 6e 61 6c 69 74 79 2e 0a 2a 2a 20 42 75 rdinality..** Bu
10379 74 20 73 6f 6d 65 74 69 6d 65 73 20 28 66 6f 72 t sometimes (for
1037a 20 65 78 61 6d 70 6c 65 20 77 68 65 6e 20 64 75 example when du
1037b 72 69 6e 67 20 61 20 44 52 4f 50 20 6f 66 20 61 ring a DROP of a
1037c 20 6c 61 72 67 65 20 74 61 62 6c 65 29 20 6d 6f large table) mo
1037d 73 74 0a 2a 2a 20 6f 72 20 61 6c 6c 20 6f 66 20 st.** or all of
1037e 74 68 65 20 70 61 67 65 73 20 69 6e 20 61 20 64 the pages in a d
1037f 61 74 61 62 61 73 65 20 63 61 6e 20 67 65 74 20 atabase can get
10380 6a 6f 75 72 6e 61 6c 6c 65 64 2e 20 20 49 6e 20 journalled. In
10381 74 68 6f 73 65 20 63 61 73 65 73 2c 20 0a 2a 2a those cases, .**
10382 20 74 68 65 20 62 69 74 6d 61 70 20 62 65 63 6f the bitmap beco
10383 6d 65 73 20 64 65 6e 73 65 20 77 69 74 68 20 68 mes dense with h
10384 69 67 68 20 63 61 72 64 69 6e 61 6c 69 74 79 2e igh cardinality.
10385 20 20 54 68 65 20 61 6c 67 6f 72 69 74 68 6d 20 The algorithm
10386 6e 65 65 64 73 20 0a 2a 2a 20 74 6f 20 68 61 6e needs .** to han
10387 64 6c 65 20 62 6f 74 68 20 63 61 73 65 73 20 77 dle both cases w
10388 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ell..**.** The s
10389 69 7a 65 20 6f 66 20 74 68 65 20 62 69 74 6d 61 ize of the bitma
1038a 70 20 69 73 20 66 69 78 65 64 20 77 68 65 6e 20 p is fixed when
1038b 74 68 65 20 6f 62 6a 65 63 74 20 69 73 20 63 72 the object is cr
1038c 65 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c eated..**.** All
1038d 20 62 69 74 73 20 61 72 65 20 63 6c 65 61 72 20 bits are clear
1038e 77 68 65 6e 20 74 68 65 20 62 69 74 6d 61 70 20 when the bitmap
1038f 69 73 20 63 72 65 61 74 65 64 2e 20 20 49 6e 64 is created. Ind
10390 69 76 69 64 75 61 6c 20 62 69 74 73 0a 2a 2a 20 ividual bits.**
10391 6d 61 79 20 62 65 20 73 65 74 20 6f 72 20 63 6c may be set or cl
10392 65 61 72 65 64 20 6f 6e 65 20 61 74 20 61 20 74 eared one at a t
10393 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 65 73 74 20 ime..**.** Test
10394 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65 20 61 operations are a
10395 62 6f 75 74 20 31 30 30 20 74 69 6d 65 73 20 6d bout 100 times m
10396 6f 72 65 20 63 6f 6d 6d 6f 6e 20 74 68 61 74 20 ore common that
10397 73 65 74 20 6f 70 65 72 61 74 69 6f 6e 73 2e 0a set operations..
10398 2a 2a 20 43 6c 65 61 72 20 6f 70 65 72 61 74 69 ** Clear operati
10399 6f 6e 73 20 61 72 65 20 65 78 63 65 65 64 69 6e ons are exceedin
1039a 67 6c 79 20 72 61 72 65 2e 20 20 54 68 65 72 65 gly rare. There
1039b 20 61 72 65 20 75 73 75 61 6c 6c 79 20 62 65 74 are usually bet
1039c 77 65 65 6e 0a 2a 2a 20 35 20 61 6e 64 20 35 30 ween.** 5 and 50
1039d 30 20 73 65 74 20 6f 70 65 72 61 74 69 6f 6e 73 0 set operations
1039e 20 70 65 72 20 42 69 74 76 65 63 20 6f 62 6a 65 per Bitvec obje
1039f 63 74 2c 20 74 68 6f 75 67 68 20 74 68 65 20 6e ct, though the n
103a0 75 6d 62 65 72 20 6f 66 20 73 65 74 73 20 63 61 umber of sets ca
103a1 6e 0a 2a 2a 20 73 6f 6d 65 74 69 6d 65 73 20 67 n.** sometimes g
103a2 72 6f 77 20 69 6e 74 6f 20 74 65 6e 73 20 6f 66 row into tens of
103a3 20 74 68 6f 75 73 61 6e 64 73 20 6f 72 20 6c 61 thousands or la
103a4 72 67 65 72 2e 20 20 54 68 65 20 73 69 7a 65 20 rger. The size
103a5 6f 66 20 74 68 65 0a 2a 2a 20 42 69 74 76 65 63 of the.** Bitvec
103a6 20 6f 62 6a 65 63 74 20 69 73 20 74 68 65 20 6e object is the n
103a7 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 umber of pages i
103a8 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
103a9 69 6c 65 20 61 74 20 74 68 65 0a 2a 2a 20 73 74 ile at the.** st
103aa 61 72 74 20 6f 66 20 61 20 74 72 61 6e 73 61 63 art of a transac
103ab 74 69 6f 6e 2c 20 61 6e 64 20 69 73 20 74 68 75 tion, and is thu
103ac 73 20 75 73 75 61 6c 6c 79 20 6c 65 73 73 20 74 s usually less t
103ad 68 61 6e 20 61 20 66 65 77 20 74 68 6f 75 73 61 han a few thousa
103ae 6e 64 2c 0a 2a 2a 20 62 75 74 20 63 61 6e 20 62 nd,.** but can b
103af 65 20 61 73 20 6c 61 72 67 65 20 61 73 20 32 20 e as large as 2
103b0 62 69 6c 6c 69 6f 6e 20 66 6f 72 20 61 20 72 65 billion for a re
103b1 61 6c 6c 79 20 62 69 67 20 64 61 74 61 62 61 73 ally big databas
103b2 65 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 e..**.** @(#) $I
103b3 64 3a 20 62 69 74 76 65 63 2e 63 2c 76 20 31 2e d: bitvec.c,v 1.
103b4 31 37 20 32 30 30 39 2f 30 37 2f 32 35 20 31 37 17 2009/07/25 17
103b5 3a 33 33 3a 32 36 20 64 72 68 20 45 78 70 20 24 :33:26 drh Exp $
103b6 0a 2a 2f 0a 0a 2f 2a 20 53 69 7a 65 20 6f 66 20 .*/../* Size of
103b7 74 68 65 20 42 69 74 76 65 63 20 73 74 72 75 63 the Bitvec struc
103b8 74 75 72 65 20 69 6e 20 62 79 74 65 73 2e 20 2a ture in bytes. *
103b9 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 /.#define BITVEC
103ba 5f 53 5a 20 20 20 20 20 20 20 20 28 73 69 7a 65 _SZ (size
103bb 6f 66 28 76 6f 69 64 2a 29 2a 31 32 38 29 20 20 of(void*)*128)
103bc 2f 2a 20 35 31 32 20 6f 6e 20 33 32 62 69 74 2e /* 512 on 32bit.
103bd 20 20 31 30 32 34 20 6f 6e 20 36 34 62 69 74 20 1024 on 64bit
103be 2a 2f 0a 0a 2f 2a 20 52 6f 75 6e 64 20 74 68 65 */../* Round the
103bf 20 75 6e 69 6f 6e 20 73 69 7a 65 20 64 6f 77 6e union size down
103c0 20 74 6f 20 74 68 65 20 6e 65 61 72 65 73 74 20 to the nearest
103c1 70 6f 69 6e 74 65 72 20 62 6f 75 6e 64 61 72 79 pointer boundary
103c2 2c 20 73 69 6e 63 65 20 74 68 61 74 27 73 20 68 , since that's h
103c3 6f 77 20 0a 2a 2a 20 69 74 20 77 69 6c 6c 20 62 ow .** it will b
103c4 65 20 61 6c 69 67 6e 65 64 20 77 69 74 68 69 6e e aligned within
103c5 20 74 68 65 20 42 69 74 76 65 63 20 73 74 72 75 the Bitvec stru
103c6 63 74 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 ct. */.#define B
103c7 49 54 56 45 43 5f 55 53 49 5a 45 20 20 20 20 20 ITVEC_USIZE
103c8 28 28 28 42 49 54 56 45 43 5f 53 5a 2d 28 33 2a (((BITVEC_SZ-(3*
103c9 73 69 7a 65 6f 66 28 75 33 32 29 29 29 2f 73 69 sizeof(u32)))/si
103ca 7a 65 6f 66 28 42 69 74 76 65 63 2a 29 29 2a 73 zeof(Bitvec*))*s
103cb 69 7a 65 6f 66 28 42 69 74 76 65 63 2a 29 29 0a izeof(Bitvec*)).
103cc 0a 2f 2a 20 54 79 70 65 20 6f 66 20 74 68 65 20 ./* Type of the
103cd 61 72 72 61 79 20 22 65 6c 65 6d 65 6e 74 22 20 array "element"
103ce 66 6f 72 20 74 68 65 20 62 69 74 6d 61 70 20 72 for the bitmap r
103cf 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 20 0a epresentation. .
103d0 2a 2a 20 53 68 6f 75 6c 64 20 62 65 20 61 20 70 ** Should be a p
103d1 6f 77 65 72 20 6f 66 20 32 2c 20 61 6e 64 20 69 ower of 2, and i
103d2 64 65 61 6c 6c 79 2c 20 65 76 65 6e 6c 79 20 64 deally, evenly d
103d3 69 76 69 64 65 20 69 6e 74 6f 20 42 49 54 56 45 ivide into BITVE
103d4 43 5f 55 53 49 5a 45 2e 20 0a 2a 2a 20 53 65 74 C_USIZE. .** Set
103d5 74 69 6e 67 20 74 68 69 73 20 74 6f 20 74 68 65 ting this to the
103d6 20 22 6e 61 74 75 72 61 6c 20 77 6f 72 64 22 20 "natural word"
103d7 73 69 7a 65 20 6f 66 20 79 6f 75 72 20 43 50 55 size of your CPU
103d8 20 6d 61 79 20 69 6d 70 72 6f 76 65 0a 2a 2a 20 may improve.**
103d9 70 65 72 66 6f 72 6d 61 6e 63 65 2e 20 2a 2f 0a performance. */.
103da 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 54 #define BITVEC_T
103db 45 4c 45 4d 20 20 20 20 20 75 38 0a 2f 2a 20 53 ELEM u8./* S
103dc 69 7a 65 2c 20 69 6e 20 62 69 74 73 2c 20 6f 66 ize, in bits, of
103dd 20 74 68 65 20 62 69 74 6d 61 70 20 65 6c 65 6d the bitmap elem
103de 65 6e 74 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ent. */.#define
103df 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 20 20 20 BITVEC_SZELEM
103e0 20 38 0a 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 8./* Number of
103e1 65 6c 65 6d 65 6e 74 73 20 69 6e 20 61 20 62 69 elements in a bi
103e2 74 6d 61 70 20 61 72 72 61 79 2e 20 2a 2f 0a 23 tmap array. */.#
103e3 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 4e 45 define BITVEC_NE
103e4 4c 45 4d 20 20 20 20 20 28 42 49 54 56 45 43 5f LEM (BITVEC_
103e5 55 53 49 5a 45 2f 73 69 7a 65 6f 66 28 42 49 54 USIZE/sizeof(BIT
103e6 56 45 43 5f 54 45 4c 45 4d 29 29 0a 2f 2a 20 4e VEC_TELEM))./* N
103e7 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 69 6e umber of bits in
103e8 20 74 68 65 20 62 69 74 6d 61 70 20 61 72 72 61 the bitmap arra
103e9 79 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 y. */.#define BI
103ea 54 56 45 43 5f 4e 42 49 54 20 20 20 20 20 20 28 TVEC_NBIT (
103eb 42 49 54 56 45 43 5f 4e 45 4c 45 4d 2a 42 49 54 BITVEC_NELEM*BIT
103ec 56 45 43 5f 53 5a 45 4c 45 4d 29 0a 0a 2f 2a 20 VEC_SZELEM)../*
103ed 4e 75 6d 62 65 72 20 6f 66 20 75 33 32 20 76 61 Number of u32 va
103ee 6c 75 65 73 20 69 6e 20 68 61 73 68 20 74 61 62 lues in hash tab
103ef 6c 65 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 le. */.#define B
103f0 49 54 56 45 43 5f 4e 49 4e 54 20 20 20 20 20 20 ITVEC_NINT
103f1 28 42 49 54 56 45 43 5f 55 53 49 5a 45 2f 73 69 (BITVEC_USIZE/si
103f2 7a 65 6f 66 28 75 33 32 29 29 0a 2f 2a 20 4d 61 zeof(u32))./* Ma
103f3 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 ximum number of
103f4 65 6e 74 72 69 65 73 20 69 6e 20 68 61 73 68 20 entries in hash
103f5 74 61 62 6c 65 20 62 65 66 6f 72 65 20 0a 2a 2a table before .**
103f6 20 73 75 62 2d 64 69 76 69 64 69 6e 67 20 61 6e sub-dividing an
103f7 64 20 72 65 2d 68 61 73 68 69 6e 67 2e 20 2a 2f d re-hashing. */
103f8 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f .#define BITVEC_
103f9 4d 58 48 41 53 48 20 20 20 20 28 42 49 54 56 45 MXHASH (BITVE
103fa 43 5f 4e 49 4e 54 2f 32 29 0a 2f 2a 20 48 61 73 C_NINT/2)./* Has
103fb 68 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 66 6f hing function fo
103fc 72 20 74 68 65 20 61 48 61 73 68 20 72 65 70 72 r the aHash repr
103fd 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20 45 esentation..** E
103fe 6d 70 69 72 69 63 61 6c 20 74 65 73 74 69 6e 67 mpirical testing
103ff 20 73 68 6f 77 65 64 20 74 68 61 74 20 74 68 65 showed that the
10400 20 2a 33 37 20 6d 75 6c 74 69 70 6c 69 65 72 20 *37 multiplier
10401 0a 2a 2a 20 28 61 6e 20 61 72 62 69 74 72 61 72 .** (an arbitrar
10402 79 20 70 72 69 6d 65 29 69 6e 20 74 68 65 20 68 y prime)in the h
10403 61 73 68 20 66 75 6e 63 74 69 6f 6e 20 70 72 6f ash function pro
10404 76 69 64 65 64 20 0a 2a 2a 20 6e 6f 20 66 65 77 vided .** no few
10405 65 72 20 63 6f 6c 6c 69 73 69 6f 6e 73 20 74 68 er collisions th
10406 61 6e 20 74 68 65 20 6e 6f 2d 6f 70 20 2a 31 2e an the no-op *1.
10407 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 */.#define BITV
10408 45 43 5f 48 41 53 48 28 58 29 20 20 20 28 28 28 EC_HASH(X) (((
10409 58 29 2a 31 29 25 42 49 54 56 45 43 5f 4e 49 4e X)*1)%BITVEC_NIN
1040a 54 29 0a 0a 23 64 65 66 69 6e 65 20 42 49 54 56 T)..#define BITV
1040b 45 43 5f 4e 50 54 52 20 20 20 20 20 20 28 42 49 EC_NPTR (BI
1040c 54 56 45 43 5f 55 53 49 5a 45 2f 73 69 7a 65 6f TVEC_USIZE/sizeo
1040d 66 28 42 69 74 76 65 63 20 2a 29 29 0a 0a 0a 2f f(Bitvec *)).../
1040e 2a 0a 2a 2a 20 41 20 62 69 74 6d 61 70 20 69 73 *.** A bitmap is
1040f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 an instance of
10410 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
10411 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 ructure..**.** T
10412 68 69 73 20 62 69 74 6d 61 70 20 72 65 63 6f 72 his bitmap recor
10413 64 73 20 74 68 65 20 65 78 69 73 74 61 6e 63 65 ds the existance
10414 20 6f 66 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 of zero or more
10415 20 62 69 74 73 0a 2a 2a 20 77 69 74 68 20 76 61 bits.** with va
10416 6c 75 65 73 20 62 65 74 77 65 65 6e 20 31 20 61 lues between 1 a
10417 6e 64 20 69 53 69 7a 65 2c 20 69 6e 63 6c 75 73 nd iSize, inclus
10418 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 ive..**.** There
10419 20 61 72 65 20 74 68 72 65 65 20 70 6f 73 73 69 are three possi
1041a 62 6c 65 20 72 65 70 72 65 73 65 6e 74 61 74 69 ble representati
1041b 6f 6e 73 20 6f 66 20 74 68 65 20 62 69 74 6d 61 ons of the bitma
1041c 70 2e 0a 2a 2a 20 49 66 20 69 53 69 7a 65 3c 3d p..** If iSize<=
1041d 42 49 54 56 45 43 5f 4e 42 49 54 2c 20 74 68 65 BITVEC_NBIT, the
1041e 6e 20 42 69 74 76 65 63 2e 75 2e 61 42 69 74 6d n Bitvec.u.aBitm
1041f 61 70 5b 5d 20 69 73 20 61 20 73 74 72 61 69 67 ap[] is a straig
10420 68 74 0a 2a 2a 20 62 69 74 6d 61 70 2e 20 20 54 ht.** bitmap. T
10421 68 65 20 6c 65 61 73 74 20 73 69 67 6e 69 66 69 he least signifi
10422 63 61 6e 74 20 62 69 74 20 69 73 20 62 69 74 20 cant bit is bit
10423 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 53 69 7a 1..**.** If iSiz
10424 65 3e 42 49 54 56 45 43 5f 4e 42 49 54 20 61 6e e>BITVEC_NBIT an
10425 64 20 69 44 69 76 69 73 6f 72 3d 3d 30 20 74 68 d iDivisor==0 th
10426 65 6e 20 42 69 74 76 65 63 2e 75 2e 61 48 61 73 en Bitvec.u.aHas
10427 68 5b 5d 20 69 73 0a 2a 2a 20 61 20 68 61 73 68 h[] is.** a hash
10428 20 74 61 62 6c 65 20 74 68 61 74 20 77 69 6c 6c table that will
10429 20 68 6f 6c 64 20 75 70 20 74 6f 20 42 49 54 56 hold up to BITV
1042a 45 43 5f 4d 58 48 41 53 48 20 64 69 73 74 69 6e EC_MXHASH distin
1042b 63 74 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a ct values..**.**
1042c 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 Otherwise, the
1042d 76 61 6c 75 65 20 69 20 69 73 20 72 65 64 69 72 value i is redir
1042e 65 63 74 65 64 20 69 6e 74 6f 20 6f 6e 65 20 6f ected into one o
1042f 66 20 42 49 54 56 45 43 5f 4e 50 54 52 0a 2a 2a f BITVEC_NPTR.**
10430 20 73 75 62 2d 62 69 74 6d 61 70 73 20 70 6f 69 sub-bitmaps poi
10431 6e 74 65 64 20 74 6f 20 62 79 20 42 69 74 76 65 nted to by Bitve
10432 63 2e 75 2e 61 70 53 75 62 5b 5d 2e 20 20 45 61 c.u.apSub[]. Ea
10433 63 68 20 73 75 62 62 69 74 6d 61 70 0a 2a 2a 20 ch subbitmap.**
10434 68 61 6e 64 6c 65 73 20 75 70 20 74 6f 20 69 44 handles up to iD
10435 69 76 69 73 6f 72 20 73 65 70 61 72 61 74 65 20 ivisor separate
10436 76 61 6c 75 65 73 20 6f 66 20 69 2e 20 20 61 70 values of i. ap
10437 53 75 62 5b 30 5d 20 68 6f 6c 64 73 0a 2a 2a 20 Sub[0] holds.**
10438 76 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20 31 values between 1
10439 20 61 6e 64 20 69 44 69 76 69 73 6f 72 2e 20 20 and iDivisor.
1043a 61 70 53 75 62 5b 31 5d 20 68 6f 6c 64 73 20 76 apSub[1] holds v
1043b 61 6c 75 65 73 20 62 65 74 77 65 65 6e 0a 2a 2a alues between.**
1043c 20 69 44 69 76 69 73 6f 72 2b 31 20 61 6e 64 20 iDivisor+1 and
1043d 32 2a 69 44 69 76 69 73 6f 72 2e 20 20 61 70 53 2*iDivisor. apS
1043e 75 62 5b 4e 5d 20 68 6f 6c 64 73 20 76 61 6c 75 ub[N] holds valu
1043f 65 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 4e 2a es between.** N*
10440 69 44 69 76 69 73 6f 72 2b 31 20 61 6e 64 20 28 iDivisor+1 and (
10441 4e 2b 31 29 2a 69 44 69 76 69 73 6f 72 2e 20 20 N+1)*iDivisor.
10442 45 61 63 68 20 73 75 62 62 69 74 6d 61 70 20 69 Each subbitmap i
10443 73 20 6e 6f 72 6d 61 6c 69 7a 65 64 0a 2a 2a 20 s normalized.**
10444 74 6f 20 68 6f 6c 64 20 64 65 61 6c 20 77 69 74 to hold deal wit
10445 68 20 76 61 6c 75 65 73 20 62 65 74 77 65 65 6e h values between
10446 20 31 20 61 6e 64 20 69 44 69 76 69 73 6f 72 2e 1 and iDivisor.
10447 0a 2a 2f 0a 73 74 72 75 63 74 20 42 69 74 76 65 .*/.struct Bitve
10448 63 20 7b 0a 20 20 75 33 32 20 69 53 69 7a 65 3b c {. u32 iSize;
10449 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d /* Maximum
1044a 20 62 69 74 20 69 6e 64 65 78 2e 20 20 4d 61 78 bit index. Max
1044b 20 69 53 69 7a 65 20 69 73 20 34 2c 32 39 34 2c iSize is 4,294,
1044c 39 36 37 2c 32 39 36 2e 20 2a 2f 0a 20 20 75 33 967,296. */. u3
1044d 32 20 6e 53 65 74 3b 20 20 20 20 20 20 20 2f 2a 2 nSet; /*
1044e 20 4e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 Number of bits
1044f 74 68 61 74 20 61 72 65 20 73 65 74 20 2d 20 6f that are set - o
10450 6e 6c 79 20 76 61 6c 69 64 20 66 6f 72 20 61 48 nly valid for aH
10451 61 73 68 0a 20 20 20 20 20 20 20 20 20 20 20 20 ash.
10452 20 20 20 20 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 ** element
10453 2e 20 20 4d 61 78 20 69 73 20 42 49 54 56 45 43 . Max is BITVEC
10454 5f 4e 49 4e 54 2e 20 20 46 6f 72 20 42 49 54 56 _NINT. For BITV
10455 45 43 5f 53 5a 20 6f 66 20 35 31 32 2c 0a 20 20 EC_SZ of 512,.
10456 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10457 2a 2a 20 74 68 69 73 20 77 6f 75 6c 64 20 62 65 ** this would be
10458 20 31 32 35 2e 20 2a 2f 0a 20 20 75 33 32 20 69 125. */. u32 i
10459 44 69 76 69 73 6f 72 3b 20 20 20 2f 2a 20 4e 75 Divisor; /* Nu
1045a 6d 62 65 72 20 6f 66 20 62 69 74 73 20 68 61 6e mber of bits han
1045b 64 6c 65 64 20 62 79 20 65 61 63 68 20 61 70 53 dled by each apS
1045c 75 62 5b 5d 20 65 6e 74 72 79 2e 20 2a 2f 0a 20 ub[] entry. */.
1045d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1045e 20 2f 2a 20 53 68 6f 75 6c 64 20 3e 3d 30 20 66 /* Should >=0 f
1045f 6f 72 20 61 70 53 75 62 20 65 6c 65 6d 65 6e 74 or apSub element
10460 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 . */.
10461 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 20 69 44 /* Max iD
10462 69 76 69 73 6f 72 20 69 73 20 6d 61 78 28 75 33 ivisor is max(u3
10463 32 29 20 2f 20 42 49 54 56 45 43 5f 4e 50 54 52 2) / BITVEC_NPTR
10464 20 2b 20 31 2e 20 20 2a 2f 0a 20 20 20 20 20 20 + 1. */.
10465 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
10466 6f 72 20 61 20 42 49 54 56 45 43 5f 53 5a 20 6f or a BITVEC_SZ o
10467 66 20 35 31 32 2c 20 74 68 69 73 20 77 6f 75 6c f 512, this woul
10468 64 20 62 65 20 33 34 2c 33 35 39 2c 37 33 39 2e d be 34,359,739.
10469 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 */. union {.
1046a 20 20 42 49 54 56 45 43 5f 54 45 4c 45 4d 20 61 BITVEC_TELEM a
1046b 42 69 74 6d 61 70 5b 42 49 54 56 45 43 5f 4e 45 Bitmap[BITVEC_NE
1046c 4c 45 4d 5d 3b 20 20 20 20 2f 2a 20 42 69 74 6d LEM]; /* Bitm
1046d 61 70 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f ap representatio
1046e 6e 20 2a 2f 0a 20 20 20 20 75 33 32 20 61 48 61 n */. u32 aHa
1046f 73 68 5b 42 49 54 56 45 43 5f 4e 49 4e 54 5d 3b sh[BITVEC_NINT];
10470 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 74 61 /* Hash ta
10471 62 6c 65 20 72 65 70 72 65 73 65 6e 74 61 74 69 ble representati
10472 6f 6e 20 2a 2f 0a 20 20 20 20 42 69 74 76 65 63 on */. Bitvec
10473 20 2a 61 70 53 75 62 5b 42 49 54 56 45 43 5f 4e *apSub[BITVEC_N
10474 50 54 52 5d 3b 20 20 2f 2a 20 52 65 63 75 72 73 PTR]; /* Recurs
10475 69 76 65 20 72 65 70 72 65 73 65 6e 74 61 74 69 ive representati
10476 6f 6e 20 2a 2f 0a 20 20 7d 20 75 3b 0a 7d 3b 0a on */. } u;.};.
10477 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 ./*.** Create a
10478 6e 65 77 20 62 69 74 6d 61 70 20 6f 62 6a 65 63 new bitmap objec
10479 74 20 61 62 6c 65 20 74 6f 20 68 61 6e 64 6c 65 t able to handle
1047a 20 62 69 74 73 20 62 65 74 77 65 65 6e 20 30 20 bits between 0
1047b 61 6e 64 20 69 53 69 7a 65 2c 0a 2a 2a 20 69 6e and iSize,.** in
1047c 63 6c 75 73 69 76 65 2e 20 20 52 65 74 75 72 6e clusive. Return
1047d 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
1047e 65 20 6e 65 77 20 6f 62 6a 65 63 74 2e 20 20 52 e new object. R
1047f 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 0a 2a eturn NULL if .*
10480 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a * malloc fails..
10481 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
10482 45 20 42 69 74 76 65 63 20 2a 73 71 6c 69 74 65 E Bitvec *sqlite
10483 33 42 69 74 76 65 63 43 72 65 61 74 65 28 75 33 3BitvecCreate(u3
10484 32 20 69 53 69 7a 65 29 7b 0a 20 20 42 69 74 76 2 iSize){. Bitv
10485 65 63 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 ec *p;. assert(
10486 20 73 69 7a 65 6f 66 28 2a 70 29 3d 3d 42 49 54 sizeof(*p)==BIT
10487 56 45 43 5f 53 5a 20 29 3b 0a 20 20 70 20 3d 20 VEC_SZ );. p =
10488 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 sqlite3MallocZer
10489 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b o( sizeof(*p) );
1048a 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 . if( p ){.
1048b 70 2d 3e 69 53 69 7a 65 20 3d 20 69 53 69 7a 65 p->iSize = iSize
1048c 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 ;. }. return p
1048d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b ;.}../*.** Check
1048e 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 69 to see if the i
1048f 2d 74 68 20 62 69 74 20 69 73 20 73 65 74 2e 20 -th bit is set.
10490 20 52 65 74 75 72 6e 20 74 72 75 65 20 6f 72 20 Return true or
10491 66 61 6c 73 65 2e 0a 2a 2a 20 49 66 20 70 20 69 false..** If p i
10492 73 20 4e 55 4c 4c 20 28 69 66 20 74 68 65 20 62 s NULL (if the b
10493 69 74 6d 61 70 20 68 61 73 20 6e 6f 74 20 62 65 itmap has not be
10494 65 6e 20 63 72 65 61 74 65 64 29 20 6f 72 20 69 en created) or i
10495 66 0a 2a 2a 20 69 20 69 73 20 6f 75 74 20 6f 66 f.** i is out of
10496 20 72 61 6e 67 65 2c 20 74 68 65 6e 20 72 65 74 range, then ret
10497 75 72 6e 20 66 61 6c 73 65 2e 0a 2a 2f 0a 53 51 urn false..*/.SQ
10498 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
10499 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 sqlite3BitvecTe
1049a 73 74 28 42 69 74 76 65 63 20 2a 70 2c 20 75 33 st(Bitvec *p, u3
1049b 32 20 69 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 2 i){. if( p==0
1049c 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 ) return 0;. i
1049d 66 28 20 69 3e 70 2d 3e 69 53 69 7a 65 20 7c 7c f( i>p->iSize ||
1049e 20 69 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 i==0 ) return 0
1049f 3b 0a 20 20 69 2d 2d 3b 0a 20 20 77 68 69 6c 65 ;. i--;. while
104a0 28 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 29 7b ( p->iDivisor ){
104a1 0a 20 20 20 20 75 33 32 20 62 69 6e 20 3d 20 69 . u32 bin = i
104a2 2f 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20 /p->iDivisor;.
104a3 20 20 69 20 3d 20 69 25 70 2d 3e 69 44 69 76 69 i = i%p->iDivi
104a4 73 6f 72 3b 0a 20 20 20 20 70 20 3d 20 70 2d 3e sor;. p = p->
104a5 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3b 0a 20 20 u.apSub[bin];.
104a6 20 20 69 66 20 28 21 70 29 20 7b 0a 20 20 20 20 if (!p) {.
104a7 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 return 0;.
104a8 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 69 }. }. if( p->i
104a9 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f 4e 42 49 Size<=BITVEC_NBI
104aa 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 T ){. return
104ab 28 70 2d 3e 75 2e 61 42 69 74 6d 61 70 5b 69 2f (p->u.aBitmap[i/
104ac 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 5d 20 26 BITVEC_SZELEM] &
104ad 20 28 31 3c 3c 28 69 26 28 42 49 54 56 45 43 5f (1<<(i&(BITVEC_
104ae 53 5a 45 4c 45 4d 2d 31 29 29 29 29 21 3d 30 3b SZELEM-1))))!=0;
104af 0a 20 20 7d 20 65 6c 73 65 7b 0a 20 20 20 20 75 . } else{. u
104b0 33 32 20 68 20 3d 20 42 49 54 56 45 43 5f 48 41 32 h = BITVEC_HA
104b1 53 48 28 69 2b 2b 29 3b 0a 20 20 20 20 77 68 69 SH(i++);. whi
104b2 6c 65 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 le( p->u.aHash[h
104b3 5d 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 ] ){. if( p
104b4 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 3d 3d 69 20 ->u.aHash[h]==i
104b5 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 ) return 1;.
104b6 20 20 68 20 3d 20 28 68 2b 31 29 20 25 20 42 49 h = (h+1) % BI
104b7 54 56 45 43 5f 4e 49 4e 54 3b 0a 20 20 20 20 7d TVEC_NINT;. }
104b8 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
104b9 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 }.}../*.** Set
104ba 74 68 65 20 69 2d 74 68 20 62 69 74 2e 20 20 52 the i-th bit. R
104bb 65 74 75 72 6e 20 30 20 6f 6e 20 73 75 63 63 65 eturn 0 on succe
104bc 73 73 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 ss and an error
104bd 63 6f 64 65 20 69 66 0a 2a 2a 20 61 6e 79 74 68 code if.** anyth
104be 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a ing goes wrong..
104bf 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
104c0 6e 65 20 6d 69 67 68 74 20 63 61 75 73 65 20 73 ne might cause s
104c1 75 62 2d 62 69 74 6d 61 70 73 20 74 6f 20 62 65 ub-bitmaps to be
104c2 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 46 61 69 allocated. Fai
104c3 6c 69 6e 67 0a 2a 2a 20 74 6f 20 67 65 74 20 74 ling.** to get t
104c4 68 65 20 6d 65 6d 6f 72 79 20 6e 65 65 64 65 64 he memory needed
104c5 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 73 75 62 to hold the sub
104c6 2d 62 69 74 6d 61 70 20 69 73 20 74 68 65 20 6f -bitmap is the o
104c7 6e 6c 79 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 nly.** that can
104c8 67 6f 20 77 72 6f 6e 67 20 77 69 74 68 20 61 6e go wrong with an
104c9 20 69 6e 73 65 72 74 2c 20 61 73 73 75 6d 69 6e insert, assumin
104ca 67 20 70 20 61 6e 64 20 69 20 61 72 65 20 76 61 g p and i are va
104cb 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 lid..**.** The c
104cc 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 alling function
104cd 6d 75 73 74 20 65 6e 73 75 72 65 20 74 68 61 74 must ensure that
104ce 20 70 20 69 73 20 61 20 76 61 6c 69 64 20 42 69 p is a valid Bi
104cf 74 76 65 63 20 6f 62 6a 65 63 74 0a 2a 2a 20 61 tvec object.** a
104d0 6e 64 20 74 68 61 74 20 74 68 65 20 76 61 6c 75 nd that the valu
104d1 65 20 66 6f 72 20 22 69 22 20 69 73 20 77 69 74 e for "i" is wit
104d2 68 69 6e 20 72 61 6e 67 65 20 6f 66 20 74 68 65 hin range of the
104d3 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 2e 0a Bitvec object..
104d4 2a 2a 20 4f 74 68 65 72 77 69 73 65 20 74 68 65 ** Otherwise the
104d5 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 behavior is und
104d6 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 efined..*/.SQLIT
104d7 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
104d8 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 42 lite3BitvecSet(B
104d9 69 74 76 65 63 20 2a 70 2c 20 75 33 32 20 69 29 itvec *p, u32 i)
104da 7b 0a 20 20 75 33 32 20 68 3b 0a 20 20 69 66 28 {. u32 h;. if(
104db 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 p==0 ) return S
104dc 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 QLITE_OK;. asse
104dd 72 74 28 20 69 3e 30 20 29 3b 0a 20 20 61 73 73 rt( i>0 );. ass
104de 65 72 74 28 20 69 3c 3d 70 2d 3e 69 53 69 7a 65 ert( i<=p->iSize
104df 20 29 3b 0a 20 20 69 2d 2d 3b 0a 20 20 77 68 69 );. i--;. whi
104e0 6c 65 28 28 70 2d 3e 69 53 69 7a 65 20 3e 20 42 le((p->iSize > B
104e1 49 54 56 45 43 5f 4e 42 49 54 29 20 26 26 20 70 ITVEC_NBIT) && p
104e2 2d 3e 69 44 69 76 69 73 6f 72 29 20 7b 0a 20 20 ->iDivisor) {.
104e3 20 20 75 33 32 20 62 69 6e 20 3d 20 69 2f 70 2d u32 bin = i/p-
104e4 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20 20 20 69 >iDivisor;. i
104e5 20 3d 20 69 25 70 2d 3e 69 44 69 76 69 73 6f 72 = i%p->iDivisor
104e6 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 75 2e 61 ;. if( p->u.a
104e7 70 53 75 62 5b 62 69 6e 5d 3d 3d 30 20 29 7b 0a pSub[bin]==0 ){.
104e8 20 20 20 20 20 20 70 2d 3e 75 2e 61 70 53 75 62 p->u.apSub
104e9 5b 62 69 6e 5d 20 3d 20 73 71 6c 69 74 65 33 42 [bin] = sqlite3B
104ea 69 74 76 65 63 43 72 65 61 74 65 28 20 70 2d 3e itvecCreate( p->
104eb 69 44 69 76 69 73 6f 72 20 29 3b 0a 20 20 20 20 iDivisor );.
104ec 20 20 69 66 28 20 70 2d 3e 75 2e 61 70 53 75 62 if( p->u.apSub
104ed 5b 62 69 6e 5d 3d 3d 30 20 29 20 72 65 74 75 72 [bin]==0 ) retur
104ee 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
104ef 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 2d }. p = p-
104f0 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3b 0a 20 >u.apSub[bin];.
104f1 20 7d 0a 20 20 69 66 28 20 70 2d 3e 69 53 69 7a }. if( p->iSiz
104f2 65 3c 3d 42 49 54 56 45 43 5f 4e 42 49 54 20 29 e<=BITVEC_NBIT )
104f3 7b 0a 20 20 20 20 70 2d 3e 75 2e 61 42 69 74 6d {. p->u.aBitm
104f4 61 70 5b 69 2f 42 49 54 56 45 43 5f 53 5a 45 4c ap[i/BITVEC_SZEL
104f5 45 4d 5d 20 7c 3d 20 31 20 3c 3c 20 28 69 26 28 EM] |= 1 << (i&(
104f6 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 2d 31 29 BITVEC_SZELEM-1)
104f7 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 );. return SQ
104f8 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 68 LITE_OK;. }. h
104f9 20 3d 20 42 49 54 56 45 43 5f 48 41 53 48 28 69 = BITVEC_HASH(i
104fa 2b 2b 29 3b 0a 20 20 2f 2a 20 69 66 20 74 68 65 ++);. /* if the
104fb 72 65 20 77 61 73 6e 27 74 20 61 20 68 61 73 68 re wasn't a hash
104fc 20 63 6f 6c 6c 69 73 69 6f 6e 2c 20 61 6e 64 20 collision, and
104fd 74 68 69 73 20 64 6f 65 73 6e 27 74 20 2a 2f 0a this doesn't */.
104fe 20 20 2f 2a 20 63 6f 6d 70 6c 65 74 65 6c 79 20 /* completely
104ff 66 69 6c 6c 20 74 68 65 20 68 61 73 68 2c 20 74 fill the hash, t
10500 68 65 6e 20 6a 75 73 74 20 61 64 64 20 69 74 20 hen just add it
10501 77 69 74 68 6f 75 74 20 2a 2f 0a 20 20 2f 2a 20 without */. /*
10502 77 6f 72 72 69 6e 67 20 61 62 6f 75 74 20 73 75 worring about su
10503 62 2d 64 69 76 69 64 69 6e 67 20 61 6e 64 20 72 b-dividing and r
10504 65 2d 68 61 73 68 69 6e 67 2e 20 2a 2f 0a 20 20 e-hashing. */.
10505 69 66 28 20 21 70 2d 3e 75 2e 61 48 61 73 68 5b if( !p->u.aHash[
10506 68 5d 20 29 7b 0a 20 20 20 20 69 66 20 28 70 2d h] ){. if (p-
10507 3e 6e 53 65 74 3c 28 42 49 54 56 45 43 5f 4e 49 >nSet<(BITVEC_NI
10508 4e 54 2d 31 29 29 20 7b 0a 20 20 20 20 20 20 67 NT-1)) {. g
10509 6f 74 6f 20 62 69 74 76 65 63 5f 73 65 74 5f 65 oto bitvec_set_e
1050a 6e 64 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b nd;. } else {
1050b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 69 74 76 . goto bitv
1050c 65 63 5f 73 65 74 5f 72 65 68 61 73 68 3b 0a 20 ec_set_rehash;.
1050d 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 20 74 68 }. }. /* th
1050e 65 72 65 20 77 61 73 20 61 20 63 6f 6c 6c 69 73 ere was a collis
1050f 69 6f 6e 2c 20 63 68 65 63 6b 20 74 6f 20 73 65 ion, check to se
10510 65 20 69 66 20 69 74 27 73 20 61 6c 72 65 61 64 e if it's alread
10511 79 20 2a 2f 0a 20 20 2f 2a 20 69 6e 20 68 61 73 y */. /* in has
10512 68 2c 20 69 66 20 6e 6f 74 2c 20 74 72 79 20 74 h, if not, try t
10513 6f 20 66 69 6e 64 20 61 20 73 70 6f 74 20 66 6f o find a spot fo
10514 72 20 69 74 20 2a 2f 0a 20 20 64 6f 20 7b 0a 20 r it */. do {.
10515 20 20 20 69 66 28 20 70 2d 3e 75 2e 61 48 61 73 if( p->u.aHas
10516 68 5b 68 5d 3d 3d 69 20 29 20 72 65 74 75 72 6e h[h]==i ) return
10517 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
10518 68 2b 2b 3b 0a 20 20 20 20 69 66 28 20 68 3e 3d h++;. if( h>=
10519 42 49 54 56 45 43 5f 4e 49 4e 54 20 29 20 68 20 BITVEC_NINT ) h
1051a 3d 20 30 3b 0a 20 20 7d 20 77 68 69 6c 65 28 20 = 0;. } while(
1051b 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 20 29 3b p->u.aHash[h] );
1051c 0a 20 20 2f 2a 20 77 65 20 64 69 64 6e 27 74 20 . /* we didn't
1051d 66 69 6e 64 20 69 74 20 69 6e 20 74 68 65 20 68 find it in the h
1051e 61 73 68 2e 20 20 68 20 70 6f 69 6e 74 73 20 74 ash. h points t
1051f 6f 20 74 68 65 20 66 69 72 73 74 20 2a 2f 0a 20 o the first */.
10520 20 2f 2a 20 61 76 61 69 6c 61 62 6c 65 20 66 72 /* available fr
10521 65 65 20 73 70 6f 74 2e 20 63 68 65 63 6b 20 74 ee spot. check t
10522 6f 20 73 65 65 20 69 66 20 74 68 69 73 20 69 73 o see if this is
10523 20 67 6f 69 6e 67 20 74 6f 20 2a 2f 0a 20 20 2f going to */. /
10524 2a 20 6d 61 6b 65 20 6f 75 72 20 68 61 73 68 20 * make our hash
10525 74 6f 6f 20 22 66 75 6c 6c 22 2e 20 20 2a 2f 0a too "full". */.
10526 62 69 74 76 65 63 5f 73 65 74 5f 72 65 68 61 73 bitvec_set_rehas
10527 68 3a 0a 20 20 69 66 28 20 70 2d 3e 6e 53 65 74 h:. if( p->nSet
10528 3e 3d 42 49 54 56 45 43 5f 4d 58 48 41 53 48 20 >=BITVEC_MXHASH
10529 29 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 ){. unsigned
1052a 69 6e 74 20 6a 3b 0a 20 20 20 20 69 6e 74 20 72 int j;. int r
1052b 63 3b 0a 20 20 20 20 75 33 32 20 2a 61 69 56 61 c;. u32 *aiVa
1052c 6c 75 65 73 20 3d 20 73 71 6c 69 74 65 33 53 74 lues = sqlite3St
1052d 61 63 6b 41 6c 6c 6f 63 52 61 77 28 30 2c 20 73 ackAllocRaw(0, s
1052e 69 7a 65 6f 66 28 70 2d 3e 75 2e 61 48 61 73 68 izeof(p->u.aHash
1052f 29 29 3b 0a 20 20 20 20 69 66 28 20 61 69 56 61 ));. if( aiVa
10530 6c 75 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 lues==0 ){.
10531 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
10532 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b OMEM;. }else{
10533 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 61 69 . memcpy(ai
10534 56 61 6c 75 65 73 2c 20 70 2d 3e 75 2e 61 48 61 Values, p->u.aHa
10535 73 68 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 75 2e sh, sizeof(p->u.
10536 61 48 61 73 68 29 29 3b 0a 20 20 20 20 20 20 6d aHash));. m
10537 65 6d 73 65 74 28 70 2d 3e 75 2e 61 70 53 75 62 emset(p->u.apSub
10538 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 75 , 0, sizeof(p->u
10539 2e 61 70 53 75 62 29 29 3b 0a 20 20 20 20 20 20 .apSub));.
1053a 70 2d 3e 69 44 69 76 69 73 6f 72 20 3d 20 28 70 p->iDivisor = (p
1053b 2d 3e 69 53 69 7a 65 20 2b 20 42 49 54 56 45 43 ->iSize + BITVEC
1053c 5f 4e 50 54 52 20 2d 20 31 29 2f 42 49 54 56 45 _NPTR - 1)/BITVE
1053d 43 5f 4e 50 54 52 3b 0a 20 20 20 20 20 20 72 63 C_NPTR;. rc
1053e 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 = sqlite3Bitvec
1053f 53 65 74 28 70 2c 20 69 29 3b 0a 20 20 20 20 20 Set(p, i);.
10540 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 42 49 54 56 for(j=0; j<BITV
10541 45 43 5f 4e 49 4e 54 3b 20 6a 2b 2b 29 7b 0a 20 EC_NINT; j++){.
10542 20 20 20 20 20 20 20 69 66 28 20 61 69 56 61 6c if( aiVal
10543 75 65 73 5b 6a 5d 20 29 20 72 63 20 7c 3d 20 73 ues[j] ) rc |= s
10544 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 qlite3BitvecSet(
10545 70 2c 20 61 69 56 61 6c 75 65 73 5b 6a 5d 29 3b p, aiValues[j]);
10546 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 . }. s
10547 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65 65 28 qlite3StackFree(
10548 30 2c 20 61 69 56 61 6c 75 65 73 29 3b 0a 20 20 0, aiValues);.
10549 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
1054a 20 20 20 7d 0a 20 20 7d 0a 62 69 74 76 65 63 5f }. }.bitvec_
1054b 73 65 74 5f 65 6e 64 3a 0a 20 20 70 2d 3e 6e 53 set_end:. p->nS
1054c 65 74 2b 2b 3b 0a 20 20 70 2d 3e 75 2e 61 48 61 et++;. p->u.aHa
1054d 73 68 5b 68 5d 20 3d 20 69 3b 0a 20 20 72 65 74 sh[h] = i;. ret
1054e 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1054f 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 ../*.** Clear th
10550 65 20 69 2d 74 68 20 62 69 74 2e 0a 2a 2a 0a 2a e i-th bit..**.*
10551 2a 20 70 42 75 66 20 6d 75 73 74 20 62 65 20 61 * pBuf must be a
10552 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 74 20 6c pointer to at l
10553 65 61 73 74 20 42 49 54 56 45 43 5f 53 5a 20 62 east BITVEC_SZ b
10554 79 74 65 73 20 6f 66 20 74 65 6d 70 6f 72 61 72 ytes of temporar
10555 79 20 73 74 6f 72 61 67 65 0a 2a 2a 20 74 68 61 y storage.** tha
10556 74 20 42 69 74 76 65 63 43 6c 65 61 72 20 63 61 t BitvecClear ca
10557 6e 20 75 73 65 20 74 6f 20 72 65 62 75 69 6c 74 n use to rebuilt
10558 20 69 74 73 20 68 61 73 68 20 74 61 62 6c 65 2e its hash table.
10559 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1055a 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
1055b 69 74 76 65 63 43 6c 65 61 72 28 42 69 74 76 65 itvecClear(Bitve
1055c 63 20 2a 70 2c 20 75 33 32 20 69 2c 20 76 6f 69 c *p, u32 i, voi
1055d 64 20 2a 70 42 75 66 29 7b 0a 20 20 69 66 28 20 d *pBuf){. if(
1055e 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 p==0 ) return;.
1055f 20 61 73 73 65 72 74 28 20 69 3e 30 20 29 3b 0a assert( i>0 );.
10560 20 20 69 2d 2d 3b 0a 20 20 77 68 69 6c 65 28 20 i--;. while(
10561 70 2d 3e 69 44 69 76 69 73 6f 72 20 29 7b 0a 20 p->iDivisor ){.
10562 20 20 20 75 33 32 20 62 69 6e 20 3d 20 69 2f 70 u32 bin = i/p
10563 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20 20 20 ->iDivisor;.
10564 69 20 3d 20 69 25 70 2d 3e 69 44 69 76 69 73 6f i = i%p->iDiviso
10565 72 3b 0a 20 20 20 20 70 20 3d 20 70 2d 3e 75 2e r;. p = p->u.
10566 61 70 53 75 62 5b 62 69 6e 5d 3b 0a 20 20 20 20 apSub[bin];.
10567 69 66 20 28 21 70 29 20 7b 0a 20 20 20 20 20 20 if (!p) {.
10568 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 return;. }.
10569 7d 0a 20 20 69 66 28 20 70 2d 3e 69 53 69 7a 65 }. if( p->iSize
1056a 3c 3d 42 49 54 56 45 43 5f 4e 42 49 54 20 29 7b <=BITVEC_NBIT ){
1056b 0a 20 20 20 20 70 2d 3e 75 2e 61 42 69 74 6d 61 . p->u.aBitma
1056c 70 5b 69 2f 42 49 54 56 45 43 5f 53 5a 45 4c 45 p[i/BITVEC_SZELE
1056d 4d 5d 20 26 3d 20 7e 28 31 20 3c 3c 20 28 69 26 M] &= ~(1 << (i&
1056e 28 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 2d 31 (BITVEC_SZELEM-1
1056f 29 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 )));. }else{.
10570 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6a unsigned int j
10571 3b 0a 20 20 20 20 75 33 32 20 2a 61 69 56 61 6c ;. u32 *aiVal
10572 75 65 73 20 3d 20 70 42 75 66 3b 0a 20 20 20 20 ues = pBuf;.
10573 6d 65 6d 63 70 79 28 61 69 56 61 6c 75 65 73 2c memcpy(aiValues,
10574 20 70 2d 3e 75 2e 61 48 61 73 68 2c 20 73 69 7a p->u.aHash, siz
10575 65 6f 66 28 70 2d 3e 75 2e 61 48 61 73 68 29 29 eof(p->u.aHash))
10576 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 2d 3e ;. memset(p->
10577 75 2e 61 48 61 73 68 2c 20 30 2c 20 73 69 7a 65 u.aHash, 0, size
10578 6f 66 28 70 2d 3e 75 2e 61 48 61 73 68 29 29 3b of(p->u.aHash));
10579 0a 20 20 20 20 70 2d 3e 6e 53 65 74 20 3d 20 30 . p->nSet = 0
1057a 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a ;. for(j=0; j
1057b 3c 42 49 54 56 45 43 5f 4e 49 4e 54 3b 20 6a 2b <BITVEC_NINT; j+
1057c 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 69 +){. if( ai
1057d 56 61 6c 75 65 73 5b 6a 5d 20 26 26 20 61 69 56 Values[j] && aiV
1057e 61 6c 75 65 73 5b 6a 5d 21 3d 28 69 2b 31 29 20 alues[j]!=(i+1)
1057f 29 7b 0a 20 20 20 20 20 20 20 20 75 33 32 20 68 ){. u32 h
10580 20 3d 20 42 49 54 56 45 43 5f 48 41 53 48 28 61 = BITVEC_HASH(a
10581 69 56 61 6c 75 65 73 5b 6a 5d 2d 31 29 3b 0a 20 iValues[j]-1);.
10582 20 20 20 20 20 20 20 70 2d 3e 6e 53 65 74 2b 2b p->nSet++
10583 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 ;. while(
10584 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 20 29 p->u.aHash[h] )
10585 7b 0a 20 20 20 20 20 20 20 20 20 20 68 2b 2b 3b {. h++;
10586 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 68 . if( h
10587 3e 3d 42 49 54 56 45 43 5f 4e 49 4e 54 20 29 20 >=BITVEC_NINT )
10588 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d h = 0;. }
10589 0a 20 20 20 20 20 20 20 20 70 2d 3e 75 2e 61 48 . p->u.aH
1058a 61 73 68 5b 68 5d 20 3d 20 61 69 56 61 6c 75 65 ash[h] = aiValue
1058b 73 5b 6a 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 s[j];. }.
1058c 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
1058d 20 44 65 73 74 72 6f 79 20 61 20 62 69 74 6d 61 Destroy a bitma
1058e 70 20 6f 62 6a 65 63 74 2e 20 20 52 65 63 6c 61 p object. Recla
1058f 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 75 73 im all memory us
10590 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
10591 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
10592 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 e3BitvecDestroy(
10593 42 69 74 76 65 63 20 2a 70 29 7b 0a 20 20 69 66 Bitvec *p){. if
10594 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b ( p==0 ) return;
10595 0a 20 20 69 66 28 20 70 2d 3e 69 44 69 76 69 73 . if( p->iDivis
10596 6f 72 20 29 7b 0a 20 20 20 20 75 6e 73 69 67 6e or ){. unsign
10597 65 64 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f ed int i;. fo
10598 72 28 69 3d 30 3b 20 69 3c 42 49 54 56 45 43 5f r(i=0; i<BITVEC_
10599 4e 50 54 52 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 NPTR; i++){.
1059a 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 sqlite3BitvecD
1059b 65 73 74 72 6f 79 28 70 2d 3e 75 2e 61 70 53 75 estroy(p->u.apSu
1059c 62 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d b[i]);. }. }
1059d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 . sqlite3_free(
1059e 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 p);.}../*.** Ret
1059f 75 72 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 urn the value of
105a0 20 74 68 65 20 69 53 69 7a 65 20 70 61 72 61 6d the iSize param
105a1 65 74 65 72 20 73 70 65 63 69 66 69 65 64 20 77 eter specified w
105a2 68 65 6e 20 42 69 74 76 65 63 20 2a 70 0a 2a 2a hen Bitvec *p.**
105a3 20 77 61 73 20 63 72 65 61 74 65 64 2e 0a 2a 2f was created..*/
105a4 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
105a5 75 33 32 20 73 71 6c 69 74 65 33 42 69 74 76 65 u32 sqlite3Bitve
105a6 63 53 69 7a 65 28 42 69 74 76 65 63 20 2a 70 29 cSize(Bitvec *p)
105a7 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 69 53 {. return p->iS
105a8 69 7a 65 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 ize;.}..#ifndef
105a9 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c SQLITE_OMIT_BUIL
105aa 54 49 4e 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 4c TIN_TEST./*.** L
105ab 65 74 20 56 5b 5d 20 62 65 20 61 6e 20 61 72 72 et V[] be an arr
105ac 61 79 20 6f 66 20 75 6e 73 69 67 6e 65 64 20 63 ay of unsigned c
105ad 68 61 72 61 63 74 65 72 73 20 73 75 66 66 69 63 haracters suffic
105ae 69 65 6e 74 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20 ient to hold.**
105af 75 70 20 74 6f 20 4e 20 62 69 74 73 2e 20 20 4c up to N bits. L
105b0 65 74 20 49 20 62 65 20 61 6e 20 69 6e 74 65 67 et I be an integ
105b1 65 72 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64 er between 0 and
105b2 20 4e 2e 20 20 30 3c 3d 49 3c 4e 2e 0a 2a 2a 20 N. 0<=I<N..**
105b3 54 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 Then the followi
105b4 6e 67 20 6d 61 63 72 6f 73 20 63 61 6e 20 62 65 ng macros can be
105b5 20 75 73 65 64 20 74 6f 20 73 65 74 2c 20 63 6c used to set, cl
105b6 65 61 72 2c 20 6f 72 20 74 65 73 74 0a 2a 2a 20 ear, or test.**
105b7 69 6e 64 69 76 69 64 75 61 6c 20 62 69 74 73 20 individual bits
105b8 77 69 74 68 69 6e 20 56 2e 0a 2a 2f 0a 23 64 65 within V..*/.#de
105b9 66 69 6e 65 20 53 45 54 42 49 54 28 56 2c 49 29 fine SETBIT(V,I)
105ba 20 20 20 20 20 20 56 5b 49 3e 3e 33 5d 20 7c 3d V[I>>3] |=
105bb 20 28 31 3c 3c 28 49 26 37 29 29 0a 23 64 65 66 (1<<(I&7)).#def
105bc 69 6e 65 20 43 4c 45 41 52 42 49 54 28 56 2c 49 ine CLEARBIT(V,I
105bd 29 20 20 20 20 56 5b 49 3e 3e 33 5d 20 26 3d 20 ) V[I>>3] &=
105be 7e 28 31 3c 3c 28 49 26 37 29 29 0a 23 64 65 66 ~(1<<(I&7)).#def
105bf 69 6e 65 20 54 45 53 54 42 49 54 28 56 2c 49 29 ine TESTBIT(V,I)
105c0 20 20 20 20 20 28 56 5b 49 3e 3e 33 5d 26 28 31 (V[I>>3]&(1
105c1 3c 3c 28 49 26 37 29 29 29 21 3d 30 0a 0a 2f 2a <<(I&7)))!=0../*
105c2 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
105c3 20 72 75 6e 73 20 61 6e 20 65 78 74 65 6e 73 69 runs an extensi
105c4 76 65 20 74 65 73 74 20 6f 66 20 74 68 65 20 42 ve test of the B
105c5 69 74 76 65 63 20 63 6f 64 65 2e 0a 2a 2a 0a 2a itvec code..**.*
105c6 2a 20 54 68 65 20 69 6e 70 75 74 20 69 73 20 61 * The input is a
105c7 6e 20 61 72 72 61 79 20 6f 66 20 69 6e 74 65 67 n array of integ
105c8 65 72 73 20 74 68 61 74 20 61 63 74 73 20 61 73 ers that acts as
105c9 20 61 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 74 6f a program.** to
105ca 20 74 65 73 74 20 74 68 65 20 42 69 74 76 65 63 test the Bitvec
105cb 2e 20 20 54 68 65 20 69 6e 74 65 67 65 72 73 20 . The integers
105cc 61 72 65 20 6f 70 63 6f 64 65 73 20 66 6f 6c 6c are opcodes foll
105cd 6f 77 65 64 0a 2a 2a 20 62 79 20 30 2c 20 31 2c owed.** by 0, 1,
105ce 20 6f 72 20 33 20 6f 70 65 72 61 6e 64 73 2c 20 or 3 operands,
105cf 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 depending on the
105d0 20 6f 70 63 6f 64 65 2e 20 20 41 6e 6f 74 68 65 opcode. Anothe
105d1 72 0a 2a 2a 20 6f 70 63 6f 64 65 20 66 6f 6c 6c r.** opcode foll
105d2 6f 77 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 ows immediately
105d3 61 66 74 65 72 20 74 68 65 20 6c 61 73 74 20 6f after the last o
105d4 70 65 72 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 perand..**.** Th
105d5 65 72 65 20 61 72 65 20 36 20 6f 70 63 6f 64 65 ere are 6 opcode
105d6 73 20 6e 75 6d 62 65 72 65 64 20 66 72 6f 6d 20 s numbered from
105d7 30 20 74 68 72 6f 75 67 68 20 35 2e 20 20 30 20 0 through 5. 0
105d8 69 73 20 74 68 65 0a 2a 2a 20 22 68 61 6c 74 22 is the.** "halt"
105d9 20 6f 70 63 6f 64 65 20 61 6e 64 20 63 61 75 73 opcode and caus
105da 65 73 20 74 68 65 20 74 65 73 74 20 74 6f 20 65 es the test to e
105db 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 30 20 20 nd..**.** 0
105dc 20 20 20 20 20 20 20 20 48 61 6c 74 20 61 6e 64 Halt and
105dd 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 return the numb
105de 65 72 20 6f 66 20 65 72 72 6f 72 73 0a 2a 2a 20 er of errors.**
105df 20 20 20 31 20 4e 20 53 20 58 20 20 20 20 53 65 1 N S X Se
105e0 74 20 4e 20 62 69 74 73 20 62 65 67 69 6e 6e 69 t N bits beginni
105e1 6e 67 20 77 69 74 68 20 53 20 61 6e 64 20 69 6e ng with S and in
105e2 63 72 65 6d 65 6e 74 69 6e 67 20 62 79 20 58 0a crementing by X.
105e3 2a 2a 20 20 20 20 32 20 4e 20 53 20 58 20 20 20 ** 2 N S X
105e4 20 43 6c 65 61 72 20 4e 20 62 69 74 73 20 62 65 Clear N bits be
105e5 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 53 20 61 ginning with S a
105e6 6e 64 20 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20 nd incrementing
105e7 62 79 20 58 0a 2a 2a 20 20 20 20 33 20 4e 20 20 by X.** 3 N
105e8 20 20 20 20 20 20 53 65 74 20 4e 20 72 61 6e 64 Set N rand
105e9 6f 6d 6c 79 20 63 68 6f 73 65 6e 20 62 69 74 73 omly chosen bits
105ea 0a 2a 2a 20 20 20 20 34 20 4e 20 20 20 20 20 20 .** 4 N
105eb 20 20 43 6c 65 61 72 20 4e 20 72 61 6e 64 6f 6d Clear N random
105ec 6c 79 20 63 68 6f 73 65 6e 20 62 69 74 73 0a 2a ly chosen bits.*
105ed 2a 20 20 20 20 35 20 4e 20 53 20 58 20 20 20 20 * 5 N S X
105ee 53 65 74 20 4e 20 62 69 74 73 20 66 72 6f 6d 20 Set N bits from
105ef 53 20 69 6e 63 72 65 6d 65 6e 74 20 58 20 69 6e S increment X in
105f0 20 61 72 72 61 79 20 6f 6e 6c 79 2c 20 6e 6f 74 array only, not
105f1 20 69 6e 20 62 69 74 76 65 63 0a 2a 2a 0a 2a 2a in bitvec.**.**
105f2 20 54 68 65 20 6f 70 63 6f 64 65 73 20 31 20 74 The opcodes 1 t
105f3 68 72 6f 75 67 68 20 34 20 70 65 72 66 6f 72 6d hrough 4 perform
105f4 20 73 65 74 20 61 6e 64 20 63 6c 65 61 72 20 6f set and clear o
105f5 70 65 72 61 74 69 6f 6e 73 20 61 72 65 20 70 65 perations are pe
105f6 72 66 6f 72 6d 65 64 0a 2a 2a 20 6f 6e 20 62 6f rformed.** on bo
105f7 74 68 20 61 20 42 69 74 76 65 63 20 6f 62 6a 65 th a Bitvec obje
105f8 63 74 20 61 6e 64 20 6f 6e 20 61 20 6c 69 6e 65 ct and on a line
105f9 61 72 20 61 72 72 61 79 20 6f 66 20 62 69 74 73 ar array of bits
105fa 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d obtained from m
105fb 61 6c 6c 6f 63 2e 0a 2a 2a 20 4f 70 63 6f 64 65 alloc..** Opcode
105fc 20 35 20 77 6f 72 6b 73 20 6f 6e 20 74 68 65 20 5 works on the
105fd 6c 69 6e 65 61 72 20 61 72 72 61 79 20 6f 6e 6c linear array onl
105fe 79 2c 20 6e 6f 74 20 6f 6e 20 74 68 65 20 42 69 y, not on the Bi
105ff 74 76 65 63 2e 0a 2a 2a 20 4f 70 63 6f 64 65 20 tvec..** Opcode
10600 35 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 6c 5 is used to del
10601 69 62 65 72 61 74 65 6c 79 20 69 6e 64 75 63 65 iberately induce
10602 20 61 20 66 61 75 6c 74 20 69 6e 20 6f 72 64 65 a fault in orde
10603 72 20 74 6f 0a 2a 2a 20 63 6f 6e 66 69 72 6d 20 r to.** confirm
10604 74 68 61 74 20 65 72 72 6f 72 20 64 65 74 65 63 that error detec
10605 74 69 6f 6e 20 77 6f 72 6b 73 2e 0a 2a 2a 0a 2a tion works..**.*
10606 2a 20 41 74 20 74 68 65 20 63 6f 6e 63 6c 75 73 * At the conclus
10607 69 6f 6e 20 6f 66 20 74 68 65 20 74 65 73 74 20 ion of the test
10608 74 68 65 20 6c 69 6e 65 61 72 20 61 72 72 61 79 the linear array
10609 20 69 73 20 63 6f 6d 70 61 72 65 64 0a 2a 2a 20 is compared.**
1060a 61 67 61 69 6e 73 74 20 74 68 65 20 42 69 74 76 against the Bitv
1060b 65 63 20 6f 62 6a 65 63 74 2e 20 20 49 66 20 74 ec object. If t
1060c 68 65 72 65 20 61 72 65 20 61 6e 79 20 64 69 66 here are any dif
1060d 66 65 72 65 6e 63 65 73 2c 0a 2a 2a 20 61 6e 20 ferences,.** an
1060e 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 error is returne
1060f 64 2e 20 20 49 66 20 74 68 65 79 20 61 72 65 20 d. If they are
10610 74 68 65 20 73 61 6d 65 2c 20 7a 65 72 6f 20 69 the same, zero i
10611 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a s returned..**.*
10612 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c * If a memory al
10613 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f location error o
10614 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20 2d 31 ccurs, return -1
10615 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
10616 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
10617 69 74 76 65 63 42 75 69 6c 74 69 6e 54 65 73 74 itvecBuiltinTest
10618 28 69 6e 74 20 73 7a 2c 20 69 6e 74 20 2a 61 4f (int sz, int *aO
10619 70 29 7b 0a 20 20 42 69 74 76 65 63 20 2a 70 42 p){. Bitvec *pB
1061a 69 74 76 65 63 20 3d 20 30 3b 0a 20 20 75 6e 73 itvec = 0;. uns
1061b 69 67 6e 65 64 20 63 68 61 72 20 2a 70 56 20 3d igned char *pV =
1061c 20 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 2d 0;. int rc = -
1061d 31 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 78 2c 20 1;. int i, nx,
1061e 70 63 2c 20 6f 70 3b 0a 20 20 76 6f 69 64 20 2a pc, op;. void *
1061f 70 54 6d 70 53 70 61 63 65 3b 0a 0a 20 20 2f 2a pTmpSpace;.. /*
10620 20 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 42 69 Allocate the Bi
10621 74 76 65 63 20 74 6f 20 62 65 20 74 65 73 74 65 tvec to be teste
10622 64 20 61 6e 64 20 61 20 6c 69 6e 65 61 72 20 61 d and a linear a
10623 72 72 61 79 20 6f 66 0a 20 20 2a 2a 20 62 69 74 rray of. ** bit
10624 73 20 74 6f 20 61 63 74 20 61 73 20 74 68 65 20 s to act as the
10625 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a 20 20 70 reference */. p
10626 42 69 74 76 65 63 20 3d 20 73 71 6c 69 74 65 33 Bitvec = sqlite3
10627 42 69 74 76 65 63 43 72 65 61 74 65 28 20 73 7a BitvecCreate( sz
10628 20 29 3b 0a 20 20 70 56 20 3d 20 73 71 6c 69 74 );. pV = sqlit
10629 65 33 5f 6d 61 6c 6c 6f 63 28 20 28 73 7a 2b 37 e3_malloc( (sz+7
1062a 29 2f 38 20 2b 20 31 20 29 3b 0a 20 20 70 54 6d )/8 + 1 );. pTm
1062b 70 53 70 61 63 65 20 3d 20 73 71 6c 69 74 65 33 pSpace = sqlite3
1062c 5f 6d 61 6c 6c 6f 63 28 42 49 54 56 45 43 5f 53 _malloc(BITVEC_S
1062d 5a 29 3b 0a 20 20 69 66 28 20 70 42 69 74 76 65 Z);. if( pBitve
1062e 63 3d 3d 30 20 7c 7c 20 70 56 3d 3d 30 20 7c 7c c==0 || pV==0 ||
1062f 20 70 54 6d 70 53 70 61 63 65 3d 3d 30 20 20 29 pTmpSpace==0 )
10630 20 67 6f 74 6f 20 62 69 74 76 65 63 5f 65 6e 64 goto bitvec_end
10631 3b 0a 20 20 6d 65 6d 73 65 74 28 70 56 2c 20 30 ;. memset(pV, 0
10632 2c 20 28 73 7a 2b 37 29 2f 38 20 2b 20 31 29 3b , (sz+7)/8 + 1);
10633 0a 0a 20 20 2f 2a 20 4e 55 4c 4c 20 70 42 69 74 .. /* NULL pBit
10634 76 65 63 20 74 65 73 74 73 20 2a 2f 0a 20 20 73 vec tests */. s
10635 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 qlite3BitvecSet(
10636 30 2c 20 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 0, 1);. sqlite3
10637 42 69 74 76 65 63 43 6c 65 61 72 28 30 2c 20 31 BitvecClear(0, 1
10638 2c 20 70 54 6d 70 53 70 61 63 65 29 3b 0a 0a 20 , pTmpSpace);..
10639 20 2f 2a 20 52 75 6e 20 74 68 65 20 70 72 6f 67 /* Run the prog
1063a 72 61 6d 20 2a 2f 0a 20 20 70 63 20 3d 20 30 3b ram */. pc = 0;
1063b 0a 20 20 77 68 69 6c 65 28 20 28 6f 70 20 3d 20 . while( (op =
1063c 61 4f 70 5b 70 63 5d 29 21 3d 30 20 29 7b 0a 20 aOp[pc])!=0 ){.
1063d 20 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b switch( op ){
1063e 0a 20 20 20 20 20 20 63 61 73 65 20 31 3a 0a 20 . case 1:.
1063f 20 20 20 20 20 63 61 73 65 20 32 3a 0a 20 20 20 case 2:.
10640 20 20 20 63 61 73 65 20 35 3a 20 7b 0a 20 20 20 case 5: {.
10641 20 20 20 20 20 6e 78 20 3d 20 34 3b 0a 20 20 20 nx = 4;.
10642 20 20 20 20 20 69 20 3d 20 61 4f 70 5b 70 63 2b i = aOp[pc+
10643 32 5d 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 20 2] - 1;.
10644 61 4f 70 5b 70 63 2b 32 5d 20 2b 3d 20 61 4f 70 aOp[pc+2] += aOp
10645 5b 70 63 2b 33 5d 3b 0a 20 20 20 20 20 20 20 20 [pc+3];.
10646 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 break;. }.
10647 20 20 20 20 20 63 61 73 65 20 33 3a 0a 20 20 20 case 3:.
10648 20 20 20 63 61 73 65 20 34 3a 20 0a 20 20 20 20 case 4: .
10649 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 default: {.
1064a 20 20 20 20 20 6e 78 20 3d 20 32 3b 0a 20 20 20 nx = 2;.
1064b 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e sqlite3_ran
1064c 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 69 domness(sizeof(i
1064d 29 2c 20 26 69 29 3b 0a 20 20 20 20 20 20 20 20 ), &i);.
1064e 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 break;. }.
1064f 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 2d 2d }. if( (--
10650 61 4f 70 5b 70 63 2b 31 5d 29 20 3e 20 30 20 29 aOp[pc+1]) > 0 )
10651 20 6e 78 20 3d 20 30 3b 0a 20 20 20 20 70 63 20 nx = 0;. pc
10652 2b 3d 20 6e 78 3b 0a 20 20 20 20 69 20 3d 20 28 += nx;. i = (
10653 69 20 26 20 30 78 37 66 66 66 66 66 66 66 29 25 i & 0x7fffffff)%
10654 73 7a 3b 0a 20 20 20 20 69 66 28 20 28 6f 70 20 sz;. if( (op
10655 26 20 31 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 & 1)!=0 ){.
10656 20 53 45 54 42 49 54 28 70 56 2c 20 28 69 2b 31 SETBIT(pV, (i+1
10657 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6f 70 ));. if( op
10658 21 3d 35 20 29 7b 0a 20 20 20 20 20 20 20 20 69 !=5 ){. i
10659 66 28 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 f( sqlite3Bitvec
1065a 53 65 74 28 70 42 69 74 76 65 63 2c 20 69 2b 31 Set(pBitvec, i+1
1065b 29 20 29 20 67 6f 74 6f 20 62 69 74 76 65 63 5f ) ) goto bitvec_
1065c 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 end;. }.
1065d 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 43 4c }else{. CL
1065e 45 41 52 42 49 54 28 70 56 2c 20 28 69 2b 31 29 EARBIT(pV, (i+1)
1065f 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
10660 42 69 74 76 65 63 43 6c 65 61 72 28 70 42 69 74 BitvecClear(pBit
10661 76 65 63 2c 20 69 2b 31 2c 20 70 54 6d 70 53 70 vec, i+1, pTmpSp
10662 61 63 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ace);. }. }.
10663 0a 20 20 2f 2a 20 54 65 73 74 20 74 6f 20 6d 61 . /* Test to ma
10664 6b 65 20 73 75 72 65 20 74 68 65 20 6c 69 6e 65 ke sure the line
10665 61 72 20 61 72 72 61 79 20 65 78 61 63 74 6c 79 ar array exactly
10666 20 6d 61 74 63 68 65 73 20 74 68 65 0a 20 20 2a matches the. *
10667 2a 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 2e * Bitvec object.
10668 20 20 53 74 61 72 74 20 77 69 74 68 20 74 68 65 Start with the
10669 20 61 73 73 75 6d 70 74 69 6f 6e 20 74 68 61 74 assumption that
1066a 20 74 68 65 79 20 64 6f 0a 20 20 2a 2a 20 6d 61 they do. ** ma
1066b 74 63 68 20 28 72 63 3d 3d 30 29 2e 20 20 43 68 tch (rc==0). Ch
1066c 61 6e 67 65 20 72 63 20 74 6f 20 6e 6f 6e 2d 7a ange rc to non-z
1066d 65 72 6f 20 69 66 20 61 20 64 69 73 63 72 65 70 ero if a discrep
1066e 61 6e 63 79 0a 20 20 2a 2a 20 69 73 20 66 6f 75 ancy. ** is fou
1066f 6e 64 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 nd.. */. rc =
10670 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 sqlite3BitvecTes
10671 74 28 30 2c 30 29 20 2b 20 73 71 6c 69 74 65 33 t(0,0) + sqlite3
10672 42 69 74 76 65 63 54 65 73 74 28 70 42 69 74 76 BitvecTest(pBitv
10673 65 63 2c 20 73 7a 2b 31 29 0a 20 20 20 20 20 20 ec, sz+1).
10674 20 20 20 20 2b 20 73 71 6c 69 74 65 33 42 69 74 + sqlite3Bit
10675 76 65 63 54 65 73 74 28 70 42 69 74 76 65 63 2c vecTest(pBitvec,
10676 20 30 29 0a 20 20 20 20 20 20 20 20 20 20 2b 20 0). +
10677 28 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 69 (sqlite3BitvecSi
10678 7a 65 28 70 42 69 74 76 65 63 29 20 2d 20 73 7a ze(pBitvec) - sz
10679 29 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c );. for(i=1; i<
1067a 3d 73 7a 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 =sz; i++){. i
1067b 66 28 20 20 28 54 45 53 54 42 49 54 28 70 56 2c f( (TESTBIT(pV,
1067c 69 29 29 21 3d 73 71 6c 69 74 65 33 42 69 74 76 i))!=sqlite3Bitv
1067d 65 63 54 65 73 74 28 70 42 69 74 76 65 63 2c 69 ecTest(pBitvec,i
1067e 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 ) ){. rc =
1067f 69 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a i;. break;.
10680 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
10681 46 72 65 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 Free allocated s
10682 74 72 75 63 74 75 72 65 20 2a 2f 0a 62 69 74 76 tructure */.bitv
10683 65 63 5f 65 6e 64 3a 0a 20 20 73 71 6c 69 74 65 ec_end:. sqlite
10684 33 5f 66 72 65 65 28 70 54 6d 70 53 70 61 63 65 3_free(pTmpSpace
10685 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 );. sqlite3_fre
10686 65 28 70 56 29 3b 0a 20 20 73 71 6c 69 74 65 33 e(pV);. sqlite3
10687 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 42 BitvecDestroy(pB
10688 69 74 76 65 63 29 3b 0a 20 20 72 65 74 75 72 6e itvec);. return
10689 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a rc;.}.#endif /*
1068a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 SQLITE_OMIT_BUI
1068b 4c 54 49 4e 5f 54 45 53 54 20 2a 2f 0a 0a 2f 2a LTIN_TEST */../*
1068c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
1068d 64 20 6f 66 20 62 69 74 76 65 63 2e 63 20 2a 2a d of bitvec.c **
1068e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1068f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
10691 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
10692 67 69 6e 20 66 69 6c 65 20 70 63 61 63 68 65 2e gin file pcache.
10693 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
10694 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10695 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
10696 0a 2a 2a 20 32 30 30 38 20 41 75 67 75 73 74 20 .** 2008 August
10697 30 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 05.**.** The aut
10698 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
10699 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
1069a 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
1069b 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
1069c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
1069d 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
1069e 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
1069f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
106a0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
106a1 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
106a2 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
106a3 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
106a4 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
106a5 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
106a6 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
106a7 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
106a8 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
106a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
106aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
106ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
106ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
106ad 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 *.** This file i
106ae 6d 70 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 70 mplements that p
106af 61 67 65 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a age cache..**.**
106b0 20 40 28 23 29 20 24 49 64 3a 20 70 63 61 63 68 @(#) $Id: pcach
106b1 65 2e 63 2c 76 20 31 2e 34 37 20 32 30 30 39 2f e.c,v 1.47 2009/
106b2 30 37 2f 32 35 20 31 31 3a 34 36 3a 34 39 20 64 07/25 11:46:49 d
106b3 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 anielk1977 Exp $
106b4 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 6f 6d .*/../*.** A com
106b5 70 6c 65 74 65 20 70 61 67 65 20 63 61 63 68 65 plete page cache
106b6 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 is an instance
106b7 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 of this structur
106b8 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 43 61 e..*/.struct PCa
106b9 63 68 65 20 7b 0a 20 20 50 67 48 64 72 20 2a 70 che {. PgHdr *p
106ba 44 69 72 74 79 2c 20 2a 70 44 69 72 74 79 54 61 Dirty, *pDirtyTa
106bb 69 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4c il; /* L
106bc 69 73 74 20 6f 66 20 64 69 72 74 79 20 70 61 67 ist of dirty pag
106bd 65 73 20 69 6e 20 4c 52 55 20 6f 72 64 65 72 20 es in LRU order
106be 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 53 79 6e */. PgHdr *pSyn
106bf 63 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 ced;
106c0 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 /* Last
106c1 20 73 79 6e 63 65 64 20 70 61 67 65 20 69 6e 20 synced page in
106c2 64 69 72 74 79 20 70 61 67 65 20 6c 69 73 74 20 dirty page list
106c3 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 */. int nRef;
106c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
106c5 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
106c6 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 64 er of referenced
106c7 20 70 61 67 65 73 20 2a 2f 0a 20 20 69 6e 74 20 pages */. int
106c8 6e 4d 61 78 3b 20 20 20 20 20 20 20 20 20 20 20 nMax;
106c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
106ca 2f 2a 20 43 6f 6e 66 69 67 75 72 65 64 20 63 61 /* Configured ca
106cb 63 68 65 20 73 69 7a 65 20 2a 2f 0a 20 20 69 6e che size */. in
106cc 74 20 73 7a 50 61 67 65 3b 20 20 20 20 20 20 20 t szPage;
106cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
106ce 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 76 65 /* Size of eve
106cf 72 79 20 70 61 67 65 20 69 6e 20 74 68 69 73 20 ry page in this
106d0 63 61 63 68 65 20 2a 2f 0a 20 20 69 6e 74 20 73 cache */. int s
106d1 7a 45 78 74 72 61 3b 20 20 20 20 20 20 20 20 20 zExtra;
106d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
106d3 2a 20 53 69 7a 65 20 6f 66 20 65 78 74 72 61 20 * Size of extra
106d4 73 70 61 63 65 20 66 6f 72 20 65 61 63 68 20 70 space for each p
106d5 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 62 50 75 age */. int bPu
106d6 72 67 65 61 62 6c 65 3b 20 20 20 20 20 20 20 20 rgeable;
106d7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
106d8 54 72 75 65 20 69 66 20 70 61 67 65 73 20 61 72 True if pages ar
106d9 65 20 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74 6f e on backing sto
106da 72 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 re */. int (*xS
106db 74 72 65 73 73 29 28 76 6f 69 64 2a 2c 50 67 48 tress)(void*,PgH
106dc 64 72 2a 29 3b 20 20 20 20 20 20 20 2f 2a 20 43 dr*); /* C
106dd 61 6c 6c 20 74 6f 20 74 72 79 20 6d 61 6b 65 20 all to try make
106de 61 20 70 61 67 65 20 63 6c 65 61 6e 20 2a 2f 0a a page clean */.
106df 20 20 76 6f 69 64 20 2a 70 53 74 72 65 73 73 3b void *pStress;
106e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
106e1 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e /* Argumen
106e2 74 20 74 6f 20 78 53 74 72 65 73 73 20 2a 2f 0a t to xStress */.
106e3 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 sqlite3_pcache
106e4 20 2a 70 43 61 63 68 65 3b 20 20 20 20 20 20 20 *pCache;
106e5 20 20 20 20 20 20 2f 2a 20 50 6c 75 67 67 61 62 /* Pluggab
106e6 6c 65 20 63 61 63 68 65 20 6d 6f 64 75 6c 65 20 le cache module
106e7 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 50 61 67 */. PgHdr *pPag
106e8 65 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 e1;
106e9 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 66 65 /* Refe
106ea 72 65 6e 63 65 20 74 6f 20 70 61 67 65 20 31 20 rence to page 1
106eb 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d */.};../*.** Som
106ec 65 20 6f 66 20 74 68 65 20 61 73 73 65 72 74 28 e of the assert(
106ed 29 20 6d 61 63 72 6f 73 20 69 6e 20 74 68 69 73 ) macros in this
106ee 20 63 6f 64 65 20 61 72 65 20 74 6f 6f 20 65 78 code are too ex
106ef 70 65 6e 73 69 76 65 20 74 6f 20 72 75 6e 0a 2a pensive to run.*
106f0 2a 20 65 76 65 6e 20 64 75 72 69 6e 67 20 6e 6f * even during no
106f1 72 6d 61 6c 20 64 65 62 75 67 67 69 6e 67 2e 20 rmal debugging.
106f2 20 55 73 65 20 74 68 65 6d 20 6f 6e 6c 79 20 72 Use them only r
106f3 61 72 65 6c 79 20 6f 6e 20 6c 6f 6e 67 2d 72 75 arely on long-ru
106f4 6e 6e 69 6e 67 0a 2a 2a 20 74 65 73 74 73 2e 20 nning.** tests.
106f5 20 45 6e 61 62 6c 65 20 74 68 65 20 65 78 70 65 Enable the expe
106f6 6e 73 69 76 65 20 61 73 73 65 72 74 73 20 75 73 nsive asserts us
106f7 69 6e 67 20 74 68 65 0a 2a 2a 20 2d 44 53 51 4c ing the.** -DSQL
106f8 49 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e ITE_ENABLE_EXPEN
106f9 53 49 56 45 5f 41 53 53 45 52 54 3d 31 20 63 6f SIVE_ASSERT=1 co
106fa 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f mpile-time optio
106fb 6e 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c n..*/.#ifdef SQL
106fc 49 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e ITE_ENABLE_EXPEN
106fd 53 49 56 45 5f 41 53 53 45 52 54 0a 23 20 64 65 SIVE_ASSERT.# de
106fe 66 69 6e 65 20 65 78 70 65 6e 73 69 76 65 5f 61 fine expensive_a
106ff 73 73 65 72 74 28 58 29 20 20 61 73 73 65 72 74 ssert(X) assert
10700 28 58 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 (X).#else.# defi
10701 6e 65 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 ne expensive_ass
10702 65 72 74 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f ert(X).#endif../
10703 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10704 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10705 2a 2a 20 4c 69 6e 6b 65 64 20 4c 69 73 74 20 4d ** Linked List M
10706 61 6e 61 67 65 6d 65 6e 74 20 2a 2a 2a 2a 2a 2a anagement ******
10707 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
10708 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 .#if !defined(ND
10709 45 42 55 47 29 20 26 26 20 64 65 66 69 6e 65 64 EBUG) && defined
1070a 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 45 (SQLITE_ENABLE_E
1070b 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52 54 29 XPENSIVE_ASSERT)
1070c 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61 ./*.** Check tha
1070d 74 20 74 68 65 20 70 43 61 63 68 65 2d 3e 70 53 t the pCache->pS
1070e 79 6e 63 65 64 20 76 61 72 69 61 62 6c 65 20 69 ynced variable i
1070f 73 20 73 65 74 20 63 6f 72 72 65 63 74 6c 79 2e s set correctly.
10710 20 49 66 20 69 74 0a 2a 2a 20 69 73 20 6e 6f 74 If it.** is not
10711 2c 20 65 69 74 68 65 72 20 66 61 69 6c 20 61 6e , either fail an
10712 20 61 73 73 65 72 74 20 6f 72 20 72 65 74 75 72 assert or retur
10713 6e 20 7a 65 72 6f 2e 20 4f 74 68 65 72 77 69 73 n zero. Otherwis
10714 65 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e e, return.** non
10715 2d 7a 65 72 6f 2e 20 54 68 69 73 20 69 73 20 6f -zero. This is o
10716 6e 6c 79 20 75 73 65 64 20 69 6e 20 64 65 62 75 nly used in debu
10717 67 67 69 6e 67 20 62 75 69 6c 64 73 2c 20 61 73 gging builds, as
10718 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 follows:.**.**
10719 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65 expensive_asse
1071a 72 74 28 20 70 63 61 63 68 65 43 68 65 63 6b 53 rt( pcacheCheckS
1071b 79 6e 63 65 64 28 70 43 61 63 68 65 29 20 29 3b ynced(pCache) );
1071c 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
1071d 63 61 63 68 65 43 68 65 63 6b 53 79 6e 63 65 64 cacheCheckSynced
1071e 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 (PCache *pCache)
1071f 7b 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 {. PgHdr *p;.
10720 66 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 for(p=pCache->pD
10721 69 72 74 79 54 61 69 6c 3b 20 70 21 3d 70 43 61 irtyTail; p!=pCa
10722 63 68 65 2d 3e 70 53 79 6e 63 65 64 3b 20 70 3d che->pSynced; p=
10723 70 2d 3e 70 44 69 72 74 79 50 72 65 76 29 7b 0a p->pDirtyPrev){.
10724 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e assert( p->n
10725 52 65 66 20 7c 7c 20 28 70 2d 3e 66 6c 61 67 73 Ref || (p->flags
10726 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 &PGHDR_NEED_SYNC
10727 29 20 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ) );. }. retur
10728 6e 20 28 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 52 n (p==0 || p->nR
10729 65 66 20 7c 7c 20 28 70 2d 3e 66 6c 61 67 73 26 ef || (p->flags&
1072a 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 PGHDR_NEED_SYNC)
1072b 3d 3d 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f ==0);.}.#endif /
1072c 2a 20 21 4e 44 45 42 55 47 20 26 26 20 53 51 4c * !NDEBUG && SQL
1072d 49 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e ITE_ENABLE_EXPEN
1072e 53 49 56 45 5f 41 53 53 45 52 54 20 2a 2f 0a 0a SIVE_ASSERT */..
1072f 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 70 61 67 /*.** Remove pag
10730 65 20 70 50 61 67 65 20 66 72 6f 6d 20 74 68 65 e pPage from the
10731 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20 70 list of dirty p
10732 61 67 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ages..*/.static
10733 76 6f 69 64 20 70 63 61 63 68 65 52 65 6d 6f 76 void pcacheRemov
10734 65 46 72 6f 6d 44 69 72 74 79 4c 69 73 74 28 50 eFromDirtyList(P
10735 67 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20 gHdr *pPage){.
10736 50 43 61 63 68 65 20 2a 70 20 3d 20 70 50 61 67 PCache *p = pPag
10737 65 2d 3e 70 43 61 63 68 65 3b 0a 0a 20 20 61 73 e->pCache;.. as
10738 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 44 69 sert( pPage->pDi
10739 72 74 79 4e 65 78 74 20 7c 7c 20 70 50 61 67 65 rtyNext || pPage
1073a 3d 3d 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 ==p->pDirtyTail
1073b 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 );. assert( pPa
1073c 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 20 7c ge->pDirtyPrev |
1073d 7c 20 70 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72 | pPage==p->pDir
1073e 74 79 20 29 3b 0a 0a 20 20 2f 2a 20 55 70 64 61 ty );.. /* Upda
1073f 74 65 20 74 68 65 20 50 43 61 63 68 65 31 2e 70 te the PCache1.p
10740 53 79 6e 63 65 64 20 76 61 72 69 61 62 6c 65 20 Synced variable
10741 69 66 20 6e 65 63 65 73 73 61 72 79 2e 20 2a 2f if necessary. */
10742 0a 20 20 69 66 28 20 70 2d 3e 70 53 79 6e 63 65 . if( p->pSynce
10743 64 3d 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 20 d==pPage ){.
10744 50 67 48 64 72 20 2a 70 53 79 6e 63 65 64 20 3d PgHdr *pSynced =
10745 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 pPage->pDirtyPr
10746 65 76 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 ev;. while( p
10747 53 79 6e 63 65 64 20 26 26 20 28 70 53 79 6e 63 Synced && (pSync
10748 65 64 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f ed->flags&PGHDR_
10749 4e 45 45 44 5f 53 59 4e 43 29 20 29 7b 0a 20 20 NEED_SYNC) ){.
1074a 20 20 20 20 70 53 79 6e 63 65 64 20 3d 20 70 53 pSynced = pS
1074b 79 6e 63 65 64 2d 3e 70 44 69 72 74 79 50 72 65 ynced->pDirtyPre
1074c 76 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e v;. }. p->
1074d 70 53 79 6e 63 65 64 20 3d 20 70 53 79 6e 63 65 pSynced = pSynce
1074e 64 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 d;. }.. if( pP
1074f 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 age->pDirtyNext
10750 29 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 44 ){. pPage->pD
10751 69 72 74 79 4e 65 78 74 2d 3e 70 44 69 72 74 79 irtyNext->pDirty
10752 50 72 65 76 20 3d 20 70 50 61 67 65 2d 3e 70 44 Prev = pPage->pD
10753 69 72 74 79 50 72 65 76 3b 0a 20 20 7d 65 6c 73 irtyPrev;. }els
10754 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 e{. assert( p
10755 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72 74 79 54 Page==p->pDirtyT
10756 61 69 6c 20 29 3b 0a 20 20 20 20 70 2d 3e 70 44 ail );. p->pD
10757 69 72 74 79 54 61 69 6c 20 3d 20 70 50 61 67 65 irtyTail = pPage
10758 2d 3e 70 44 69 72 74 79 50 72 65 76 3b 0a 20 20 ->pDirtyPrev;.
10759 7d 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 }. if( pPage->p
1075a 44 69 72 74 79 50 72 65 76 20 29 7b 0a 20 20 20 DirtyPrev ){.
1075b 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 pPage->pDirtyPr
1075c 65 76 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 3d ev->pDirtyNext =
1075d 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 pPage->pDirtyNe
1075e 78 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 xt;. }else{.
1075f 20 61 73 73 65 72 74 28 20 70 50 61 67 65 3d 3d assert( pPage==
10760 70 2d 3e 70 44 69 72 74 79 20 29 3b 0a 20 20 20 p->pDirty );.
10761 20 70 2d 3e 70 44 69 72 74 79 20 3d 20 70 50 61 p->pDirty = pPa
10762 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 3b 0a ge->pDirtyNext;.
10763 20 20 7d 0a 20 20 70 50 61 67 65 2d 3e 70 44 69 }. pPage->pDi
10764 72 74 79 4e 65 78 74 20 3d 20 30 3b 0a 20 20 70 rtyNext = 0;. p
10765 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 Page->pDirtyPrev
10766 20 3d 20 30 3b 0a 0a 20 20 65 78 70 65 6e 73 69 = 0;.. expensi
10767 76 65 5f 61 73 73 65 72 74 28 20 70 63 61 63 68 ve_assert( pcach
10768 65 43 68 65 63 6b 53 79 6e 63 65 64 28 70 29 20 eCheckSynced(p)
10769 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 );.}../*.** Add
1076a 70 61 67 65 20 70 50 61 67 65 20 74 6f 20 74 68 page pPage to th
1076b 65 20 68 65 61 64 20 6f 66 20 74 68 65 20 64 69 e head of the di
1076c 72 74 79 20 6c 69 73 74 20 28 50 43 61 63 68 65 rty list (PCache
1076d 31 2e 70 44 69 72 74 79 20 69 73 20 73 65 74 20 1.pDirty is set
1076e 74 6f 0a 2a 2a 20 70 50 61 67 65 29 2e 0a 2a 2f to.** pPage)..*/
1076f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 .static void pca
10770 63 68 65 41 64 64 54 6f 44 69 72 74 79 4c 69 73 cheAddToDirtyLis
10771 74 28 50 67 48 64 72 20 2a 70 50 61 67 65 29 7b t(PgHdr *pPage){
10772 0a 20 20 50 43 61 63 68 65 20 2a 70 20 3d 20 70 . PCache *p = p
10773 50 61 67 65 2d 3e 70 43 61 63 68 65 3b 0a 0a 20 Page->pCache;..
10774 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
10775 70 44 69 72 74 79 4e 65 78 74 3d 3d 30 20 26 26 pDirtyNext==0 &&
10776 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 pPage->pDirtyPr
10777 65 76 3d 3d 30 20 26 26 20 70 2d 3e 70 44 69 72 ev==0 && p->pDir
10778 74 79 21 3d 70 50 61 67 65 20 29 3b 0a 0a 20 20 ty!=pPage );..
10779 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 pPage->pDirtyNex
1077a 74 20 3d 20 70 2d 3e 70 44 69 72 74 79 3b 0a 20 t = p->pDirty;.
1077b 20 69 66 28 20 70 50 61 67 65 2d 3e 70 44 69 72 if( pPage->pDir
1077c 74 79 4e 65 78 74 20 29 7b 0a 20 20 20 20 61 73 tyNext ){. as
1077d 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 44 69 sert( pPage->pDi
1077e 72 74 79 4e 65 78 74 2d 3e 70 44 69 72 74 79 50 rtyNext->pDirtyP
1077f 72 65 76 3d 3d 30 20 29 3b 0a 20 20 20 20 70 50 rev==0 );. pP
10780 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 2d age->pDirtyNext-
10781 3e 70 44 69 72 74 79 50 72 65 76 20 3d 20 70 50 >pDirtyPrev = pP
10782 61 67 65 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 44 age;. }. p->pD
10783 69 72 74 79 20 3d 20 70 50 61 67 65 3b 0a 20 20 irty = pPage;.
10784 69 66 28 20 21 70 2d 3e 70 44 69 72 74 79 54 61 if( !p->pDirtyTa
10785 69 6c 20 29 7b 0a 20 20 20 20 70 2d 3e 70 44 69 il ){. p->pDi
10786 72 74 79 54 61 69 6c 20 3d 20 70 50 61 67 65 3b rtyTail = pPage;
10787 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 2d 3e 70 . }. if( !p->p
10788 53 79 6e 63 65 64 20 26 26 20 30 3d 3d 28 70 50 Synced && 0==(pP
10789 61 67 65 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 age->flags&PGHDR
1078a 5f 4e 45 45 44 5f 53 59 4e 43 29 20 29 7b 0a 20 _NEED_SYNC) ){.
1078b 20 20 20 70 2d 3e 70 53 79 6e 63 65 64 20 3d 20 p->pSynced =
1078c 70 50 61 67 65 3b 0a 20 20 7d 0a 20 20 65 78 70 pPage;. }. exp
1078d 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28 20 70 ensive_assert( p
1078e 63 61 63 68 65 43 68 65 63 6b 53 79 6e 63 65 64 cacheCheckSynced
1078f 28 70 29 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 (p) );.}../*.**
10790 57 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 74 Wrapper around t
10791 68 65 20 70 6c 75 67 67 61 62 6c 65 20 63 61 63 he pluggable cac
10792 68 65 73 20 78 55 6e 70 69 6e 20 6d 65 74 68 6f hes xUnpin metho
10793 64 2e 20 49 66 20 74 68 65 20 63 61 63 68 65 20 d. If the cache
10794 69 73 0a 2a 2a 20 62 65 69 6e 67 20 75 73 65 64 is.** being used
10795 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 for an in-memor
10796 79 20 64 61 74 61 62 61 73 65 2c 20 74 68 69 73 y database, this
10797 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e function is a n
10798 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 o-op..*/.static
10799 76 6f 69 64 20 70 63 61 63 68 65 55 6e 70 69 6e void pcacheUnpin
1079a 28 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 50 43 (PgHdr *p){. PC
1079b 61 63 68 65 20 2a 70 43 61 63 68 65 20 3d 20 70 ache *pCache = p
1079c 2d 3e 70 43 61 63 68 65 3b 0a 20 20 69 66 28 20 ->pCache;. if(
1079d 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 pCache->bPurgeab
1079e 6c 65 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d le ){. if( p-
1079f 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 >pgno==1 ){.
107a0 20 20 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31 pCache->pPage1
107a1 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = 0;. }.
107a2 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
107a3 66 69 67 2e 70 63 61 63 68 65 2e 78 55 6e 70 69 fig.pcache.xUnpi
107a4 6e 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 n(pCache->pCache
107a5 2c 20 70 2c 20 30 29 3b 0a 20 20 7d 0a 7d 0a 0a , p, 0);. }.}..
107a6 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /***************
107a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
107a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
107a9 2a 2a 2a 2a 20 47 65 6e 65 72 61 6c 20 49 6e 74 **** General Int
107aa 65 72 66 61 63 65 73 20 2a 2a 2a 2a 2a 2a 0a 2a erfaces ******.*
107ab 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 *.** Initialize
107ac 61 6e 64 20 73 68 75 74 64 6f 77 6e 20 74 68 65 and shutdown the
107ad 20 70 61 67 65 20 63 61 63 68 65 20 73 75 62 73 page cache subs
107ae 79 73 74 65 6d 2e 20 4e 65 69 74 68 65 72 20 6f ystem. Neither o
107af 66 20 74 68 65 73 65 20 0a 2a 2a 20 66 75 6e 63 f these .** func
107b0 74 69 6f 6e 73 20 61 72 65 20 74 68 72 65 61 64 tions are thread
107b1 73 61 66 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f safe..*/.SQLITE_
107b2 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
107b3 74 65 33 50 63 61 63 68 65 49 6e 69 74 69 61 6c te3PcacheInitial
107b4 69 7a 65 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 ize(void){. if(
107b5 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
107b6 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 49 6e 69 nfig.pcache.xIni
107b7 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 t==0 ){. sqli
107b8 74 65 33 50 43 61 63 68 65 53 65 74 44 65 66 61 te3PCacheSetDefa
107b9 75 6c 74 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74 ult();. }. ret
107ba 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 urn sqlite3Globa
107bb 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 lConfig.pcache.x
107bc 49 6e 69 74 28 73 71 6c 69 74 65 33 47 6c 6f 62 Init(sqlite3Glob
107bd 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e alConfig.pcache.
107be 70 41 72 67 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f pArg);.}.SQLITE_
107bf 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
107c0 69 74 65 33 50 63 61 63 68 65 53 68 75 74 64 6f ite3PcacheShutdo
107c1 77 6e 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 wn(void){. if(
107c2 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
107c3 66 69 67 2e 70 63 61 63 68 65 2e 78 53 68 75 74 fig.pcache.xShut
107c4 64 6f 77 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 down ){. sqli
107c5 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
107c6 70 63 61 63 68 65 2e 78 53 68 75 74 64 6f 77 6e pcache.xShutdown
107c7 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f (sqlite3GlobalCo
107c8 6e 66 69 67 2e 70 63 61 63 68 65 2e 70 41 72 67 nfig.pcache.pArg
107c9 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
107ca 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 Return the size
107cb 69 6e 20 62 79 74 65 73 20 6f 66 20 61 20 50 43 in bytes of a PC
107cc 61 63 68 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a ache object..*/.
107cd 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
107ce 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 nt sqlite3Pcache
107cf 53 69 7a 65 28 76 6f 69 64 29 7b 20 72 65 74 75 Size(void){ retu
107d0 72 6e 20 73 69 7a 65 6f 66 28 50 43 61 63 68 65 rn sizeof(PCache
107d1 29 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 ); }../*.** Crea
107d2 74 65 20 61 20 6e 65 77 20 50 43 61 63 68 65 20 te a new PCache
107d3 6f 62 6a 65 63 74 2e 20 53 74 6f 72 61 67 65 20 object. Storage
107d4 73 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 space to hold th
107d5 65 20 6f 62 6a 65 63 74 0a 2a 2a 20 68 61 73 20 e object.** has
107d6 61 6c 72 65 61 64 79 20 62 65 65 6e 20 61 6c 6c already been all
107d7 6f 63 61 74 65 64 20 61 6e 64 20 69 73 20 70 61 ocated and is pa
107d8 73 73 65 64 20 69 6e 20 61 73 20 74 68 65 20 70 ssed in as the p
107d9 20 70 6f 69 6e 74 65 72 2e 20 0a 2a 2a 20 54 68 pointer. .** Th
107da 65 20 63 61 6c 6c 65 72 20 64 69 73 63 6f 76 65 e caller discove
107db 72 73 20 68 6f 77 20 6d 75 63 68 20 73 70 61 63 rs how much spac
107dc 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 61 6c e needs to be al
107dd 6c 6f 63 61 74 65 64 20 62 79 20 0a 2a 2a 20 63 located by .** c
107de 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 50 63 alling sqlite3Pc
107df 61 63 68 65 53 69 7a 65 28 29 2e 0a 2a 2f 0a 53 acheSize()..*/.S
107e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
107e1 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
107e2 4f 70 65 6e 28 0a 20 20 69 6e 74 20 73 7a 50 61 Open(. int szPa
107e3 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ge,
107e4 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
107e5 65 76 65 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 every page */.
107e6 69 6e 74 20 73 7a 45 78 74 72 61 2c 20 20 20 20 int szExtra,
107e7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
107e8 45 78 74 72 61 20 73 70 61 63 65 20 61 73 73 6f Extra space asso
107e9 63 69 61 74 65 64 20 77 69 74 68 20 65 61 63 68 ciated with each
107ea 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 62 page */. int b
107eb 50 75 72 67 65 61 62 6c 65 2c 20 20 20 20 20 20 Purgeable,
107ec 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
107ed 69 66 20 70 61 67 65 73 20 61 72 65 20 6f 6e 20 if pages are on
107ee 62 61 63 6b 69 6e 67 20 73 74 6f 72 65 20 2a 2f backing store */
107ef 0a 20 20 69 6e 74 20 28 2a 78 53 74 72 65 73 73 . int (*xStress
107f0 29 28 76 6f 69 64 2a 2c 50 67 48 64 72 2a 29 2c )(void*,PgHdr*),
107f1 2f 2a 20 43 61 6c 6c 20 74 6f 20 74 72 79 20 74 /* Call to try t
107f2 6f 20 6d 61 6b 65 20 70 61 67 65 73 20 63 6c 65 o make pages cle
107f3 61 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 an */. void *pS
107f4 74 72 65 73 73 2c 20 20 20 20 20 20 20 20 20 20 tress,
107f5 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 /* Argument
107f6 20 74 6f 20 78 53 74 72 65 73 73 20 2a 2f 0a 20 to xStress */.
107f7 20 50 43 61 63 68 65 20 2a 70 20 20 20 20 20 20 PCache *p
107f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
107f9 20 50 72 65 61 6c 6c 6f 63 61 74 65 64 20 73 70 Preallocated sp
107fa 61 63 65 20 66 6f 72 20 74 68 65 20 50 43 61 63 ace for the PCac
107fb 68 65 20 2a 2f 0a 29 7b 0a 20 20 6d 65 6d 73 65 he */.){. memse
107fc 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 50 t(p, 0, sizeof(P
107fd 43 61 63 68 65 29 29 3b 0a 20 20 70 2d 3e 73 7a Cache));. p->sz
107fe 50 61 67 65 20 3d 20 73 7a 50 61 67 65 3b 0a 20 Page = szPage;.
107ff 20 70 2d 3e 73 7a 45 78 74 72 61 20 3d 20 73 7a p->szExtra = sz
10800 45 78 74 72 61 3b 0a 20 20 70 2d 3e 62 50 75 72 Extra;. p->bPur
10801 67 65 61 62 6c 65 20 3d 20 62 50 75 72 67 65 61 geable = bPurgea
10802 62 6c 65 3b 0a 20 20 70 2d 3e 78 53 74 72 65 73 ble;. p->xStres
10803 73 20 3d 20 78 53 74 72 65 73 73 3b 0a 20 20 70 s = xStress;. p
10804 2d 3e 70 53 74 72 65 73 73 20 3d 20 70 53 74 72 ->pStress = pStr
10805 65 73 73 3b 0a 20 20 70 2d 3e 6e 4d 61 78 20 3d ess;. p->nMax =
10806 20 31 30 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 100;.}../*.** C
10807 68 61 6e 67 65 20 74 68 65 20 70 61 67 65 20 73 hange the page s
10808 69 7a 65 20 66 6f 72 20 50 43 61 63 68 65 20 6f ize for PCache o
10809 62 6a 65 63 74 2e 20 54 68 65 20 63 61 6c 6c 65 bject. The calle
1080a 72 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74 68 r must ensure th
1080b 61 74 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 at there.** are
1080c 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 no outstanding p
1080d 61 67 65 20 72 65 66 65 72 65 6e 63 65 73 20 77 age references w
1080e 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f hen this functio
1080f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a n is called..*/.
10810 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
10811 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
10812 65 53 65 74 50 61 67 65 53 69 7a 65 28 50 43 61 eSetPageSize(PCa
10813 63 68 65 20 2a 70 43 61 63 68 65 2c 20 69 6e 74 che *pCache, int
10814 20 73 7a 50 61 67 65 29 7b 0a 20 20 61 73 73 65 szPage){. asse
10815 72 74 28 20 70 43 61 63 68 65 2d 3e 6e 52 65 66 rt( pCache->nRef
10816 3d 3d 30 20 26 26 20 70 43 61 63 68 65 2d 3e 70 ==0 && pCache->p
10817 44 69 72 74 79 3d 3d 30 20 29 3b 0a 20 20 69 66 Dirty==0 );. if
10818 28 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 ( pCache->pCache
10819 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 ){. sqlite3G
1081a 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 lobalConfig.pcac
1081b 68 65 2e 78 44 65 73 74 72 6f 79 28 70 43 61 63 he.xDestroy(pCac
1081c 68 65 2d 3e 70 43 61 63 68 65 29 3b 0a 20 20 20 he->pCache);.
1081d 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 pCache->pCache
1081e 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 43 61 63 68 = 0;. }. pCach
1081f 65 2d 3e 73 7a 50 61 67 65 20 3d 20 73 7a 50 61 e->szPage = szPa
10820 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 ge;.}../*.** Try
10821 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 70 61 67 to obtain a pag
10822 65 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 e from the cache
10823 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
10824 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
10825 63 61 63 68 65 46 65 74 63 68 28 0a 20 20 50 43 cacheFetch(. PC
10826 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20 20 20 ache *pCache,
10827 20 20 20 20 2f 2a 20 4f 62 74 61 69 6e 20 74 68 /* Obtain th
10828 65 20 70 61 67 65 20 66 72 6f 6d 20 74 68 69 73 e page from this
10829 20 63 61 63 68 65 20 2a 2f 0a 20 20 50 67 6e 6f cache */. Pgno
1082a 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 pgno,
1082b 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 /* Page number
1082c 20 74 6f 20 6f 62 74 61 69 6e 20 2a 2f 0a 20 20 to obtain */.
1082d 69 6e 74 20 63 72 65 61 74 65 46 6c 61 67 2c 20 int createFlag,
1082e 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 /* If true
1082f 2c 20 63 72 65 61 74 65 20 70 61 67 65 20 69 66 , create page if
10830 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 it does not exi
10831 73 74 20 61 6c 72 65 61 64 79 20 2a 2f 0a 20 20 st already */.
10832 50 67 48 64 72 20 2a 2a 70 70 50 61 67 65 20 20 PgHdr **ppPage
10833 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 /* Write t
10834 68 65 20 70 61 67 65 20 68 65 72 65 20 2a 2f 0a he page here */.
10835 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 50 61 67 ){. PgHdr *pPag
10836 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 65 43 72 e = 0;. int eCr
10837 65 61 74 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 eate;.. assert(
10838 20 70 43 61 63 68 65 21 3d 30 20 29 3b 0a 20 20 pCache!=0 );.
10839 61 73 73 65 72 74 28 20 63 72 65 61 74 65 46 6c assert( createFl
1083a 61 67 3d 3d 31 20 7c 7c 20 63 72 65 61 74 65 46 ag==1 || createF
1083b 6c 61 67 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 lag==0 );. asse
1083c 72 74 28 20 70 67 6e 6f 3e 30 20 29 3b 0a 0a 20 rt( pgno>0 );..
1083d 20 2f 2a 20 49 66 20 74 68 65 20 70 6c 75 67 67 /* If the plugg
1083e 61 62 6c 65 20 63 61 63 68 65 20 28 73 71 6c 69 able cache (sqli
1083f 74 65 33 5f 70 63 61 63 68 65 2a 29 20 68 61 73 te3_pcache*) has
10840 20 6e 6f 74 20 62 65 65 6e 20 61 6c 6c 6f 63 61 not been alloca
10841 74 65 64 2c 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 ted,. ** alloca
10842 74 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a te it now.. */.
10843 20 20 69 66 28 20 21 70 43 61 63 68 65 2d 3e 70 if( !pCache->p
10844 43 61 63 68 65 20 26 26 20 63 72 65 61 74 65 46 Cache && createF
10845 6c 61 67 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 lag ){. sqlit
10846 65 33 5f 70 63 61 63 68 65 20 2a 70 3b 0a 20 20 e3_pcache *p;.
10847 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 20 int nByte;.
10848 20 6e 42 79 74 65 20 3d 20 70 43 61 63 68 65 2d nByte = pCache-
10849 3e 73 7a 50 61 67 65 20 2b 20 70 43 61 63 68 65 >szPage + pCache
1084a 2d 3e 73 7a 45 78 74 72 61 20 2b 20 73 69 7a 65 ->szExtra + size
1084b 6f 66 28 50 67 48 64 72 29 3b 0a 20 20 20 20 70 of(PgHdr);. p
1084c 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c = sqlite3Global
1084d 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 43 Config.pcache.xC
1084e 72 65 61 74 65 28 6e 42 79 74 65 2c 20 70 43 61 reate(nByte, pCa
1084f 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 29 che->bPurgeable)
10850 3b 0a 20 20 20 20 69 66 28 20 21 70 20 29 7b 0a ;. if( !p ){.
10851 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
10852 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d ITE_NOMEM;. }
10853 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 . sqlite3Glob
10854 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e alConfig.pcache.
10855 78 43 61 63 68 65 73 69 7a 65 28 70 2c 20 70 43 xCachesize(p, pC
10856 61 63 68 65 2d 3e 6e 4d 61 78 29 3b 0a 20 20 20 ache->nMax);.
10857 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 pCache->pCache
10858 3d 20 70 3b 0a 20 20 7d 0a 0a 20 20 65 43 72 65 = p;. }.. eCre
10859 61 74 65 20 3d 20 63 72 65 61 74 65 46 6c 61 67 ate = createFlag
1085a 20 2a 20 28 31 20 2b 20 28 21 70 43 61 63 68 65 * (1 + (!pCache
1085b 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 7c 7c 20 ->bPurgeable ||
1085c 21 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 29 !pCache->pDirty)
1085d 29 3b 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d );. if( pCache-
1085e 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 20 20 70 >pCache ){. p
1085f 50 61 67 65 20 3d 20 73 71 6c 69 74 65 33 47 6c Page = sqlite3Gl
10860 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 obalConfig.pcach
10861 65 2e 78 46 65 74 63 68 28 70 43 61 63 68 65 2d e.xFetch(pCache-
10862 3e 70 43 61 63 68 65 2c 20 70 67 6e 6f 2c 20 65 >pCache, pgno, e
10863 43 72 65 61 74 65 29 3b 0a 20 20 7d 0a 0a 20 20 Create);. }..
10864 69 66 28 20 21 70 50 61 67 65 20 26 26 20 65 43 if( !pPage && eC
10865 72 65 61 74 65 3d 3d 31 20 29 7b 0a 20 20 20 20 reate==1 ){.
10866 50 67 48 64 72 20 2a 70 50 67 3b 0a 0a 20 20 20 PgHdr *pPg;..
10867 20 2f 2a 20 46 69 6e 64 20 61 20 64 69 72 74 79 /* Find a dirty
10868 20 70 61 67 65 20 74 6f 20 77 72 69 74 65 2d 6f page to write-o
10869 75 74 20 61 6e 64 20 72 65 63 79 63 6c 65 2e 20 ut and recycle.
1086a 46 69 72 73 74 20 74 72 79 20 74 6f 20 66 69 6e First try to fin
1086b 64 20 61 20 0a 20 20 20 20 2a 2a 20 70 61 67 65 d a . ** page
1086c 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 72 that does not r
1086d 65 71 75 69 72 65 20 61 20 6a 6f 75 72 6e 61 6c equire a journal
1086e 2d 73 79 6e 63 20 28 6f 6e 65 20 77 69 74 68 20 -sync (one with
1086f 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 0a PGHDR_NEED_SYNC.
10870 20 20 20 20 2a 2a 20 63 6c 65 61 72 65 64 29 2c ** cleared),
10871 20 62 75 74 20 69 66 20 74 68 61 74 20 69 73 20 but if that is
10872 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 73 65 74 not possible set
10873 74 6c 65 20 66 6f 72 20 61 6e 79 20 6f 74 68 65 tle for any othe
10874 72 20 0a 20 20 20 20 2a 2a 20 75 6e 72 65 66 65 r . ** unrefe
10875 72 65 6e 63 65 64 20 64 69 72 74 79 20 70 61 67 renced dirty pag
10876 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 65 78 e.. */. ex
10877 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28 20 pensive_assert(
10878 70 63 61 63 68 65 43 68 65 63 6b 53 79 6e 63 65 pcacheCheckSynce
10879 64 28 70 43 61 63 68 65 29 20 29 3b 0a 20 20 20 d(pCache) );.
1087a 20 66 6f 72 28 70 50 67 3d 70 43 61 63 68 65 2d for(pPg=pCache-
1087b 3e 70 53 79 6e 63 65 64 3b 20 0a 20 20 20 20 20 >pSynced; .
1087c 20 20 20 70 50 67 20 26 26 20 28 70 50 67 2d 3e pPg && (pPg->
1087d 6e 52 65 66 20 7c 7c 20 28 70 50 67 2d 3e 66 6c nRef || (pPg->fl
1087e 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 ags&PGHDR_NEED_S
1087f 59 4e 43 29 29 3b 20 0a 20 20 20 20 20 20 20 20 YNC)); .
10880 70 50 67 3d 70 50 67 2d 3e 70 44 69 72 74 79 50 pPg=pPg->pDirtyP
10881 72 65 76 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 rev. );. i
10882 66 28 20 21 70 50 67 20 29 7b 0a 20 20 20 20 20 f( !pPg ){.
10883 20 66 6f 72 28 70 50 67 3d 70 43 61 63 68 65 2d for(pPg=pCache-
10884 3e 70 44 69 72 74 79 54 61 69 6c 3b 20 70 50 67 >pDirtyTail; pPg
10885 20 26 26 20 70 50 67 2d 3e 6e 52 65 66 3b 20 70 && pPg->nRef; p
10886 50 67 3d 70 50 67 2d 3e 70 44 69 72 74 79 50 72 Pg=pPg->pDirtyPr
10887 65 76 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 ev);. }. i
10888 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 20 20 f( pPg ){.
10889 69 6e 74 20 72 63 3b 0a 20 20 20 20 20 20 72 63 int rc;. rc
1088a 20 3d 20 70 43 61 63 68 65 2d 3e 78 53 74 72 65 = pCache->xStre
1088b 73 73 28 70 43 61 63 68 65 2d 3e 70 53 74 72 65 ss(pCache->pStre
1088c 73 73 2c 20 70 50 67 29 3b 0a 20 20 20 20 20 20 ss, pPg);.
1088d 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
1088e 4b 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f K && rc!=SQLITE_
1088f 42 55 53 59 20 29 7b 0a 20 20 20 20 20 20 20 20 BUSY ){.
10890 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
10891 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 50 }. }.. pP
10892 61 67 65 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f age = sqlite3Glo
10893 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 balConfig.pcache
10894 2e 78 46 65 74 63 68 28 70 43 61 63 68 65 2d 3e .xFetch(pCache->
10895 70 43 61 63 68 65 2c 20 70 67 6e 6f 2c 20 32 29 pCache, pgno, 2)
10896 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 ;. }.. if( pPa
10897 67 65 20 29 7b 0a 20 20 20 20 69 66 28 20 21 70 ge ){. if( !p
10898 50 61 67 65 2d 3e 70 44 61 74 61 20 29 7b 0a 20 Page->pData ){.
10899 20 20 20 20 20 6d 65 6d 73 65 74 28 70 50 61 67 memset(pPag
1089a 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 50 67 48 e, 0, sizeof(PgH
1089b 64 72 29 20 2b 20 70 43 61 63 68 65 2d 3e 73 7a dr) + pCache->sz
1089c 45 78 74 72 61 29 3b 0a 20 20 20 20 20 20 70 50 Extra);. pP
1089d 61 67 65 2d 3e 70 45 78 74 72 61 20 3d 20 28 76 age->pExtra = (v
1089e 6f 69 64 2a 29 26 70 50 61 67 65 5b 31 5d 3b 0a oid*)&pPage[1];.
1089f 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70 44 61 pPage->pDa
108a0 74 61 20 3d 20 28 76 6f 69 64 20 2a 29 26 28 28 ta = (void *)&((
108a1 63 68 61 72 20 2a 29 70 50 61 67 65 29 5b 73 69 char *)pPage)[si
108a2 7a 65 6f 66 28 50 67 48 64 72 29 20 2b 20 70 43 zeof(PgHdr) + pC
108a3 61 63 68 65 2d 3e 73 7a 45 78 74 72 61 5d 3b 0a ache->szExtra];.
108a4 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70 43 61 pPage->pCa
108a5 63 68 65 20 3d 20 70 43 61 63 68 65 3b 0a 20 20 che = pCache;.
108a6 20 20 20 20 70 50 61 67 65 2d 3e 70 67 6e 6f 20 pPage->pgno
108a7 3d 20 70 67 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 = pgno;. }.
108a8 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
108a9 3e 70 43 61 63 68 65 3d 3d 70 43 61 63 68 65 20 >pCache==pCache
108aa 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
108ab 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d 70 67 6e 6f Page->pgno==pgno
108ac 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
108ad 70 50 61 67 65 2d 3e 70 45 78 74 72 61 3d 3d 28 pPage->pExtra==(
108ae 76 6f 69 64 20 2a 29 26 70 50 61 67 65 5b 31 5d void *)&pPage[1]
108af 20 29 3b 0a 0a 20 20 20 20 69 66 28 20 30 3d 3d );.. if( 0==
108b0 70 50 61 67 65 2d 3e 6e 52 65 66 20 29 7b 0a 20 pPage->nRef ){.
108b1 20 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 52 65 pCache->nRe
108b2 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 f++;. }. p
108b3 50 61 67 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 Page->nRef++;.
108b4 20 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29 7b if( pgno==1 ){
108b5 0a 20 20 20 20 20 20 70 43 61 63 68 65 2d 3e 70 . pCache->p
108b6 50 61 67 65 31 20 3d 20 70 50 61 67 65 3b 0a 20 Page1 = pPage;.
108b7 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 70 50 61 }. }. *ppPa
108b8 67 65 20 3d 20 70 50 61 67 65 3b 0a 20 20 72 65 ge = pPage;. re
108b9 74 75 72 6e 20 28 70 50 61 67 65 3d 3d 30 20 26 turn (pPage==0 &
108ba 26 20 65 43 72 65 61 74 65 29 20 3f 20 53 51 4c & eCreate) ? SQL
108bb 49 54 45 5f 4e 4f 4d 45 4d 20 3a 20 53 51 4c 49 ITE_NOMEM : SQLI
108bc 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
108bd 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65 Decrement the re
108be 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 6f 6e ference count on
108bf 20 61 20 70 61 67 65 2e 20 49 66 20 74 68 65 20 a page. If the
108c0 70 61 67 65 20 69 73 20 63 6c 65 61 6e 20 61 6e page is clean an
108c1 64 20 74 68 65 0a 2a 2a 20 72 65 66 65 72 65 6e d the.** referen
108c2 63 65 20 63 6f 75 6e 74 20 64 72 6f 70 73 20 74 ce count drops t
108c3 6f 20 30 2c 20 74 68 65 6e 20 69 74 20 69 73 20 o 0, then it is
108c4 6d 61 64 65 20 65 6c 69 62 6c 65 20 66 6f 72 20 made elible for
108c5 72 65 63 79 63 6c 69 6e 67 2e 0a 2a 2f 0a 53 51 recycling..*/.SQ
108c6 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
108c7 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 d sqlite3PcacheR
108c8 65 6c 65 61 73 65 28 50 67 48 64 72 20 2a 70 29 elease(PgHdr *p)
108c9 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e {. assert( p->n
108ca 52 65 66 3e 30 20 29 3b 0a 20 20 70 2d 3e 6e 52 Ref>0 );. p->nR
108cb 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 2d 3e 6e ef--;. if( p->n
108cc 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 50 43 Ref==0 ){. PC
108cd 61 63 68 65 20 2a 70 43 61 63 68 65 20 3d 20 70 ache *pCache = p
108ce 2d 3e 70 43 61 63 68 65 3b 0a 20 20 20 20 70 43 ->pCache;. pC
108cf 61 63 68 65 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 ache->nRef--;.
108d0 20 20 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 26 if( (p->flags&
108d1 50 47 48 44 52 5f 44 49 52 54 59 29 3d 3d 30 20 PGHDR_DIRTY)==0
108d2 29 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 55 ){. pcacheU
108d3 6e 70 69 6e 28 70 29 3b 0a 20 20 20 20 7d 65 6c npin(p);. }el
108d4 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 4d 6f 76 se{. /* Mov
108d5 65 20 74 68 65 20 70 61 67 65 20 74 6f 20 74 68 e the page to th
108d6 65 20 68 65 61 64 20 6f 66 20 74 68 65 20 64 69 e head of the di
108d7 72 74 79 20 6c 69 73 74 2e 20 2a 2f 0a 20 20 20 rty list. */.
108d8 20 20 20 70 63 61 63 68 65 52 65 6d 6f 76 65 46 pcacheRemoveF
108d9 72 6f 6d 44 69 72 74 79 4c 69 73 74 28 70 29 3b romDirtyList(p);
108da 0a 20 20 20 20 20 20 70 63 61 63 68 65 41 64 64 . pcacheAdd
108db 54 6f 44 69 72 74 79 4c 69 73 74 28 70 29 3b 0a ToDirtyList(p);.
108dc 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
108dd 2a 2a 20 49 6e 63 72 65 61 73 65 20 74 68 65 20 ** Increase the
108de 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 reference count
108df 6f 66 20 61 20 73 75 70 70 6c 69 65 64 20 70 61 of a supplied pa
108e0 67 65 20 62 79 20 31 2e 0a 2a 2f 0a 53 51 4c 49 ge by 1..*/.SQLI
108e1 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
108e2 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 sqlite3PcacheRef
108e3 28 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 61 73 (PgHdr *p){. as
108e4 73 65 72 74 28 70 2d 3e 6e 52 65 66 3e 30 29 3b sert(p->nRef>0);
108e5 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 7d 0a . p->nRef++;.}.
108e6 0a 2f 2a 0a 2a 2a 20 44 72 6f 70 20 61 20 70 61 ./*.** Drop a pa
108e7 67 65 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 ge from the cach
108e8 65 2e 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 e. There must be
108e9 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 72 65 66 exactly one ref
108ea 65 72 65 6e 63 65 20 74 6f 20 74 68 65 0a 2a 2a erence to the.**
108eb 20 70 61 67 65 2e 20 54 68 69 73 20 66 75 6e 63 page. This func
108ec 74 69 6f 6e 20 64 65 6c 65 74 65 73 20 74 68 61 tion deletes tha
108ed 74 20 72 65 66 65 72 65 6e 63 65 2c 20 73 6f 20 t reference, so
108ee 61 66 74 65 72 20 69 74 20 72 65 74 75 72 6e 73 after it returns
108ef 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 70 6f 69 the.** page poi
108f0 6e 74 65 64 20 74 6f 20 62 79 20 70 20 69 73 20 nted to by p is
108f1 69 6e 76 61 6c 69 64 2e 0a 2a 2f 0a 53 51 4c 49 invalid..*/.SQLI
108f2 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
108f3 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 72 6f sqlite3PcacheDro
108f4 70 28 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 50 p(PgHdr *p){. P
108f5 43 61 63 68 65 20 2a 70 43 61 63 68 65 3b 0a 20 Cache *pCache;.
108f6 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 assert( p->nRef
108f7 3d 3d 31 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e ==1 );. if( p->
108f8 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 flags&PGHDR_DIRT
108f9 59 20 29 7b 0a 20 20 20 20 70 63 61 63 68 65 52 Y ){. pcacheR
108fa 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79 4c 69 emoveFromDirtyLi
108fb 73 74 28 70 29 3b 0a 20 20 7d 0a 20 20 70 43 61 st(p);. }. pCa
108fc 63 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b che = p->pCache;
108fd 0a 20 20 70 43 61 63 68 65 2d 3e 6e 52 65 66 2d . pCache->nRef-
108fe 2d 3b 0a 20 20 69 66 28 20 70 2d 3e 70 67 6e 6f -;. if( p->pgno
108ff 3d 3d 31 20 29 7b 0a 20 20 20 20 70 43 61 63 68 ==1 ){. pCach
10900 65 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 e->pPage1 = 0;.
10901 20 7d 0a 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 }. sqlite3Glob
10902 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e alConfig.pcache.
10903 78 55 6e 70 69 6e 28 70 43 61 63 68 65 2d 3e 70 xUnpin(pCache->p
10904 43 61 63 68 65 2c 20 70 2c 20 31 29 3b 0a 7d 0a Cache, p, 1);.}.
10905 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 ./*.** Make sure
10906 20 74 68 65 20 70 61 67 65 20 69 73 20 6d 61 72 the page is mar
10907 6b 65 64 20 61 73 20 64 69 72 74 79 2e 20 49 66 ked as dirty. If
10908 20 69 74 20 69 73 6e 27 74 20 64 69 72 74 79 20 it isn't dirty
10909 61 6c 72 65 61 64 79 2c 0a 2a 2a 20 6d 61 6b 65 already,.** make
1090a 20 69 74 20 73 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 it so..*/.SQLIT
1090b 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1090c 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 qlite3PcacheMake
1090d 44 69 72 74 79 28 50 67 48 64 72 20 2a 70 29 7b Dirty(PgHdr *p){
1090e 0a 20 20 70 2d 3e 66 6c 61 67 73 20 26 3d 20 7e . p->flags &= ~
1090f 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 PGHDR_DONT_WRITE
10910 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e ;. assert( p->n
10911 52 65 66 3e 30 20 29 3b 0a 20 20 69 66 28 20 30 Ref>0 );. if( 0
10912 3d 3d 28 70 2d 3e 66 6c 61 67 73 20 26 20 50 47 ==(p->flags & PG
10913 48 44 52 5f 44 49 52 54 59 29 20 29 7b 0a 20 20 HDR_DIRTY) ){.
10914 20 20 70 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 p->flags |= PG
10915 48 44 52 5f 44 49 52 54 59 3b 0a 20 20 20 20 70 HDR_DIRTY;. p
10916 63 61 63 68 65 41 64 64 54 6f 44 69 72 74 79 4c cacheAddToDirtyL
10917 69 73 74 28 20 70 29 3b 0a 20 20 7d 0a 7d 0a 0a ist( p);. }.}..
10918 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 /*.** Make sure
10919 74 68 65 20 70 61 67 65 20 69 73 20 6d 61 72 6b the page is mark
1091a 65 64 20 61 73 20 63 6c 65 61 6e 2e 20 49 66 20 ed as clean. If
1091b 69 74 20 69 73 6e 27 74 20 63 6c 65 61 6e 20 61 it isn't clean a
1091c 6c 72 65 61 64 79 2c 0a 2a 2a 20 6d 61 6b 65 20 lready,.** make
1091d 69 74 20 73 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 it so..*/.SQLITE
1091e 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1091f 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 lite3PcacheMakeC
10920 6c 65 61 6e 28 50 67 48 64 72 20 2a 70 29 7b 0a lean(PgHdr *p){.
10921 20 20 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 if( (p->flags
10922 26 20 50 47 48 44 52 5f 44 49 52 54 59 29 20 29 & PGHDR_DIRTY) )
10923 7b 0a 20 20 20 20 70 63 61 63 68 65 52 65 6d 6f {. pcacheRemo
10924 76 65 46 72 6f 6d 44 69 72 74 79 4c 69 73 74 28 veFromDirtyList(
10925 70 29 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 p);. p->flags
10926 20 26 3d 20 7e 28 50 47 48 44 52 5f 44 49 52 54 &= ~(PGHDR_DIRT
10927 59 7c 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e Y|PGHDR_NEED_SYN
10928 43 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e C);. if( p->n
10929 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 Ref==0 ){.
1092a 70 63 61 63 68 65 55 6e 70 69 6e 28 70 29 3b 0a pcacheUnpin(p);.
1092b 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
1092c 2a 2a 20 4d 61 6b 65 20 65 76 65 72 79 20 70 61 ** Make every pa
1092d 67 65 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 ge in the cache
1092e 63 6c 65 61 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 clean..*/.SQLITE
1092f 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
10930 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 6e lite3PcacheClean
10931 41 6c 6c 28 50 43 61 63 68 65 20 2a 70 43 61 63 All(PCache *pCac
10932 68 65 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 3b he){. PgHdr *p;
10933 0a 20 20 77 68 69 6c 65 28 20 28 70 20 3d 20 70 . while( (p = p
10934 43 61 63 68 65 2d 3e 70 44 69 72 74 79 29 21 3d Cache->pDirty)!=
10935 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 0 ){. sqlite3
10936 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 PcacheMakeClean(
10937 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a p);. }.}../*.**
10938 20 43 6c 65 61 72 20 74 68 65 20 50 47 48 44 52 Clear the PGHDR
10939 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 _NEED_SYNC flag
1093a 66 72 6f 6d 20 61 6c 6c 20 64 69 72 74 79 20 70 from all dirty p
1093b 61 67 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ages..*/.SQLITE_
1093c 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1093d 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 53 ite3PcacheClearS
1093e 79 6e 63 46 6c 61 67 73 28 50 43 61 63 68 65 20 yncFlags(PCache
1093f 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67 48 64 *pCache){. PgHd
10940 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70 43 r *p;. for(p=pC
10941 61 63 68 65 2d 3e 70 44 69 72 74 79 3b 20 70 3b ache->pDirty; p;
10942 20 70 3d 70 2d 3e 70 44 69 72 74 79 4e 65 78 74 p=p->pDirtyNext
10943 29 7b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 ){. p->flags
10944 26 3d 20 7e 50 47 48 44 52 5f 4e 45 45 44 5f 53 &= ~PGHDR_NEED_S
10945 59 4e 43 3b 0a 20 20 7d 0a 20 20 70 43 61 63 68 YNC;. }. pCach
10946 65 2d 3e 70 53 79 6e 63 65 64 20 3d 20 70 43 61 e->pSynced = pCa
10947 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69 6c 3b che->pDirtyTail;
10948 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 .}../*.** Change
10949 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 the page number
1094a 20 6f 66 20 70 61 67 65 20 70 20 74 6f 20 6e 65 of page p to ne
1094b 77 50 67 6e 6f 2e 20 0a 2a 2f 0a 53 51 4c 49 54 wPgno. .*/.SQLIT
1094c 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1094d 71 6c 69 74 65 33 50 63 61 63 68 65 4d 6f 76 65 qlite3PcacheMove
1094e 28 50 67 48 64 72 20 2a 70 2c 20 50 67 6e 6f 20 (PgHdr *p, Pgno
1094f 6e 65 77 50 67 6e 6f 29 7b 0a 20 20 50 43 61 63 newPgno){. PCac
10950 68 65 20 2a 70 43 61 63 68 65 20 3d 20 70 2d 3e he *pCache = p->
10951 70 43 61 63 68 65 3b 0a 20 20 61 73 73 65 72 74 pCache;. assert
10952 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 ( p->nRef>0 );.
10953 20 61 73 73 65 72 74 28 20 6e 65 77 50 67 6e 6f assert( newPgno
10954 3e 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 47 >0 );. sqlite3G
10955 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 lobalConfig.pcac
10956 68 65 2e 78 52 65 6b 65 79 28 70 43 61 63 68 65 he.xRekey(pCache
10957 2d 3e 70 43 61 63 68 65 2c 20 70 2c 20 70 2d 3e ->pCache, p, p->
10958 70 67 6e 6f 2c 20 6e 65 77 50 67 6e 6f 29 3b 0a pgno, newPgno);.
10959 20 20 70 2d 3e 70 67 6e 6f 20 3d 20 6e 65 77 50 p->pgno = newP
1095a 67 6e 6f 3b 0a 20 20 69 66 28 20 28 70 2d 3e 66 gno;. if( (p->f
1095b 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 lags&PGHDR_DIRTY
1095c 29 20 26 26 20 28 70 2d 3e 66 6c 61 67 73 26 50 ) && (p->flags&P
1095d 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 20 GHDR_NEED_SYNC)
1095e 29 7b 0a 20 20 20 20 70 63 61 63 68 65 52 65 6d ){. pcacheRem
1095f 6f 76 65 46 72 6f 6d 44 69 72 74 79 4c 69 73 74 oveFromDirtyList
10960 28 70 29 3b 0a 20 20 20 20 70 63 61 63 68 65 41 (p);. pcacheA
10961 64 64 54 6f 44 69 72 74 79 4c 69 73 74 28 70 29 ddToDirtyList(p)
10962 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 ;. }.}../*.** D
10963 72 6f 70 20 65 76 65 72 79 20 63 61 63 68 65 20 rop every cache
10964 65 6e 74 72 79 20 77 68 6f 73 65 20 70 61 67 65 entry whose page
10965 20 6e 75 6d 62 65 72 20 69 73 20 67 72 65 61 74 number is great
10966 65 72 20 74 68 61 6e 20 22 70 67 6e 6f 22 2e 20 er than "pgno".
10967 54 68 65 0a 2a 2a 20 63 61 6c 6c 65 72 20 6d 75 The.** caller mu
10968 73 74 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 st ensure that t
10969 68 65 72 65 20 61 72 65 20 6e 6f 20 6f 75 74 73 here are no outs
1096a 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63 tanding referenc
1096b 65 73 20 74 6f 20 61 6e 79 20 70 61 67 65 73 0a es to any pages.
1096c 2a 2a 20 6f 74 68 65 72 20 74 68 61 6e 20 70 61 ** other than pa
1096d 67 65 20 31 20 77 69 74 68 20 61 20 70 61 67 65 ge 1 with a page
1096e 20 6e 75 6d 62 65 72 20 67 72 65 61 74 65 72 20 number greater
1096f 74 68 61 6e 20 70 67 6e 6f 2e 0a 2a 2a 0a 2a 2a than pgno..**.**
10970 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20 72 If there is a r
10971 65 66 65 72 65 6e 63 65 20 74 6f 20 70 61 67 65 eference to page
10972 20 31 20 61 6e 64 20 74 68 65 20 70 67 6e 6f 20 1 and the pgno
10973 70 61 72 61 6d 65 74 65 72 20 70 61 73 73 65 64 parameter passed
10974 20 74 6f 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 to this.** func
10975 74 69 6f 6e 20 69 73 20 30 2c 20 74 68 65 6e 20 tion is 0, then
10976 74 68 65 20 64 61 74 61 20 61 72 65 61 20 61 73 the data area as
10977 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 61 sociated with pa
10978 67 65 20 31 20 69 73 20 7a 65 72 6f 65 64 2c 20 ge 1 is zeroed,
10979 62 75 74 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 but.** the page
1097a 6f 62 6a 65 63 74 20 69 73 20 6e 6f 74 20 64 72 object is not dr
1097b 6f 70 70 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 opped..*/.SQLITE
1097c 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1097d 6c 69 74 65 33 50 63 61 63 68 65 54 72 75 6e 63 lite3PcacheTrunc
1097e 61 74 65 28 50 43 61 63 68 65 20 2a 70 43 61 63 ate(PCache *pCac
1097f 68 65 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a he, Pgno pgno){.
10980 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70 43 if( pCache->pC
10981 61 63 68 65 20 29 7b 0a 20 20 20 20 50 67 48 64 ache ){. PgHd
10982 72 20 2a 70 3b 0a 20 20 20 20 50 67 48 64 72 20 r *p;. PgHdr
10983 2a 70 4e 65 78 74 3b 0a 20 20 20 20 66 6f 72 28 *pNext;. for(
10984 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 p=pCache->pDirty
10985 3b 20 70 3b 20 70 3d 70 4e 65 78 74 29 7b 0a 20 ; p; p=pNext){.
10986 20 20 20 20 20 70 4e 65 78 74 20 3d 20 70 2d 3e pNext = p->
10987 70 44 69 72 74 79 4e 65 78 74 3b 0a 20 20 20 20 pDirtyNext;.
10988 20 20 69 66 28 20 70 2d 3e 70 67 6e 6f 3e 70 67 if( p->pgno>pg
10989 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 no ){. as
1098a 73 65 72 74 28 20 70 2d 3e 66 6c 61 67 73 26 50 sert( p->flags&P
1098b 47 48 44 52 5f 44 49 52 54 59 20 29 3b 0a 20 20 GHDR_DIRTY );.
1098c 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 sqlite3Pca
1098d 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 70 29 3b cheMakeClean(p);
1098e 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
1098f 20 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20 26 if( pgno==0 &
10990 26 20 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31 & pCache->pPage1
10991 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 ){. memset
10992 28 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31 2d (pCache->pPage1-
10993 3e 70 44 61 74 61 2c 20 30 2c 20 70 43 61 63 68 >pData, 0, pCach
10994 65 2d 3e 73 7a 50 61 67 65 29 3b 0a 20 20 20 20 e->szPage);.
10995 20 20 70 67 6e 6f 20 3d 20 31 3b 0a 20 20 20 20 pgno = 1;.
10996 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f }. sqlite3Glo
10997 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 balConfig.pcache
10998 2e 78 54 72 75 6e 63 61 74 65 28 70 43 61 63 68 .xTruncate(pCach
10999 65 2d 3e 70 43 61 63 68 65 2c 20 70 67 6e 6f 2b e->pCache, pgno+
1099a 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 1);. }.}../*.**
1099b 20 43 6c 6f 73 65 20 61 20 63 61 63 68 65 2e 0a Close a cache..
1099c 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1099d 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 E void sqlite3Pc
1099e 61 63 68 65 43 6c 6f 73 65 28 50 43 61 63 68 65 acheClose(PCache
1099f 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 69 66 28 *pCache){. if(
109a0 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 pCache->pCache
109a1 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c ){. sqlite3Gl
109a2 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 obalConfig.pcach
109a3 65 2e 78 44 65 73 74 72 6f 79 28 70 43 61 63 68 e.xDestroy(pCach
109a4 65 2d 3e 70 43 61 63 68 65 29 3b 0a 20 20 7d 0a e->pCache);. }.
109a5 7d 0a 0a 2f 2a 20 0a 2a 2a 20 44 69 73 63 61 72 }../* .** Discar
109a6 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f d the contents o
109a7 66 20 74 68 65 20 63 61 63 68 65 2e 0a 2a 2f 0a f the cache..*/.
109a8 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
109a9 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
109aa 65 43 6c 65 61 72 28 50 43 61 63 68 65 20 2a 70 eClear(PCache *p
109ab 43 61 63 68 65 29 7b 0a 20 20 73 71 6c 69 74 65 Cache){. sqlite
109ac 33 50 63 61 63 68 65 54 72 75 6e 63 61 74 65 28 3PcacheTruncate(
109ad 70 43 61 63 68 65 2c 20 30 29 3b 0a 7d 0a 0a 2f pCache, 0);.}../
109ae 2a 0a 2a 2a 20 4d 65 72 67 65 20 74 77 6f 20 6c *.** Merge two l
109af 69 73 74 73 20 6f 66 20 70 61 67 65 73 20 63 6f ists of pages co
109b0 6e 6e 65 63 74 65 64 20 62 79 20 70 44 69 72 74 nnected by pDirt
109b1 79 20 61 6e 64 20 69 6e 20 70 67 6e 6f 20 6f 72 y and in pgno or
109b2 64 65 72 2e 0a 2a 2a 20 44 6f 20 6e 6f 74 20 62 der..** Do not b
109b3 6f 74 68 20 66 69 78 69 6e 67 20 74 68 65 20 70 oth fixing the p
109b4 44 69 72 74 79 50 72 65 76 20 70 6f 69 6e 74 65 DirtyPrev pointe
109b5 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 50 67 rs..*/.static Pg
109b6 48 64 72 20 2a 70 63 61 63 68 65 4d 65 72 67 65 Hdr *pcacheMerge
109b7 44 69 72 74 79 4c 69 73 74 28 50 67 48 64 72 20 DirtyList(PgHdr
109b8 2a 70 41 2c 20 50 67 48 64 72 20 2a 70 42 29 7b *pA, PgHdr *pB){
109b9 0a 20 20 50 67 48 64 72 20 72 65 73 75 6c 74 2c . PgHdr result,
109ba 20 2a 70 54 61 69 6c 3b 0a 20 20 70 54 61 69 6c *pTail;. pTail
109bb 20 3d 20 26 72 65 73 75 6c 74 3b 0a 20 20 77 68 = &result;. wh
109bc 69 6c 65 28 20 70 41 20 26 26 20 70 42 20 29 7b ile( pA && pB ){
109bd 0a 20 20 20 20 69 66 28 20 70 41 2d 3e 70 67 6e . if( pA->pgn
109be 6f 3c 70 42 2d 3e 70 67 6e 6f 20 29 7b 0a 20 20 o<pB->pgno ){.
109bf 20 20 20 20 70 54 61 69 6c 2d 3e 70 44 69 72 74 pTail->pDirt
109c0 79 20 3d 20 70 41 3b 0a 20 20 20 20 20 20 70 54 y = pA;. pT
109c1 61 69 6c 20 3d 20 70 41 3b 0a 20 20 20 20 20 20 ail = pA;.
109c2 70 41 20 3d 20 70 41 2d 3e 70 44 69 72 74 79 3b pA = pA->pDirty;
109c3 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
109c4 20 20 70 54 61 69 6c 2d 3e 70 44 69 72 74 79 20 pTail->pDirty
109c5 3d 20 70 42 3b 0a 20 20 20 20 20 20 70 54 61 69 = pB;. pTai
109c6 6c 20 3d 20 70 42 3b 0a 20 20 20 20 20 20 70 42 l = pB;. pB
109c7 20 3d 20 70 42 2d 3e 70 44 69 72 74 79 3b 0a 20 = pB->pDirty;.
109c8 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 }. }. if( p
109c9 41 20 29 7b 0a 20 20 20 20 70 54 61 69 6c 2d 3e A ){. pTail->
109ca 70 44 69 72 74 79 20 3d 20 70 41 3b 0a 20 20 7d pDirty = pA;. }
109cb 65 6c 73 65 20 69 66 28 20 70 42 20 29 7b 0a 20 else if( pB ){.
109cc 20 20 20 70 54 61 69 6c 2d 3e 70 44 69 72 74 79 pTail->pDirty
109cd 20 3d 20 70 42 3b 0a 20 20 7d 65 6c 73 65 7b 0a = pB;. }else{.
109ce 20 20 20 20 70 54 61 69 6c 2d 3e 70 44 69 72 74 pTail->pDirt
109cf 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 y = 0;. }. ret
109d0 75 72 6e 20 72 65 73 75 6c 74 2e 70 44 69 72 74 urn result.pDirt
109d1 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 72 74 y;.}../*.** Sort
109d2 20 74 68 65 20 6c 69 73 74 20 6f 66 20 70 61 67 the list of pag
109d3 65 73 20 69 6e 20 61 63 63 65 6e 64 69 6e 67 20 es in accending
109d4 6f 72 64 65 72 20 62 79 20 70 67 6e 6f 2e 20 20 order by pgno.
109d5 50 61 67 65 73 20 61 72 65 0a 2a 2a 20 63 6f 6e Pages are.** con
109d6 6e 65 63 74 65 64 20 62 79 20 70 44 69 72 74 79 nected by pDirty
109d7 20 70 6f 69 6e 74 65 72 73 2e 20 20 54 68 65 20 pointers. The
109d8 70 44 69 72 74 79 50 72 65 76 20 70 6f 69 6e 74 pDirtyPrev point
109d9 65 72 73 20 61 72 65 0a 2a 2a 20 63 6f 72 72 75 ers are.** corru
109da 70 74 65 64 20 62 79 20 74 68 69 73 20 73 6f 72 pted by this sor
109db 74 2e 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 65 20 74 t..**.** Since t
109dc 68 65 72 65 20 63 61 6e 6e 6f 74 20 62 65 20 6d here cannot be m
109dd 6f 72 65 20 74 68 61 6e 20 32 5e 33 31 20 64 69 ore than 2^31 di
109de 73 74 69 6e 63 74 20 70 61 67 65 73 20 69 6e 20 stinct pages in
109df 61 20 64 61 74 61 62 61 73 65 2c 0a 2a 2a 20 74 a database,.** t
109e0 68 65 72 65 20 63 61 6e 6e 6f 74 20 62 65 20 6d here cannot be m
109e1 6f 72 65 20 74 68 61 6e 20 33 31 20 62 75 63 6b ore than 31 buck
109e2 65 74 73 20 72 65 71 75 69 72 65 64 20 62 79 20 ets required by
109e3 74 68 65 20 6d 65 72 67 65 20 73 6f 72 74 65 72 the merge sorter
109e4 2e 0a 2a 2a 20 4f 6e 65 20 65 78 74 72 61 20 62 ..** One extra b
109e5 75 63 6b 65 74 20 69 73 20 61 64 64 65 64 20 74 ucket is added t
109e6 6f 20 63 61 74 63 68 20 6f 76 65 72 66 6c 6f 77 o catch overflow
109e7 20 69 6e 20 63 61 73 65 20 73 6f 6d 65 74 68 69 in case somethi
109e8 6e 67 0a 2a 2a 20 65 76 65 72 20 63 68 61 6e 67 ng.** ever chang
109e9 65 73 20 74 6f 20 6d 61 6b 65 20 74 68 65 20 70 es to make the p
109ea 72 65 76 69 6f 75 73 20 73 65 6e 74 65 6e 63 65 revious sentence
109eb 20 69 6e 63 6f 72 72 65 63 74 2e 0a 2a 2f 0a 23 incorrect..*/.#
109ec 64 65 66 69 6e 65 20 4e 5f 53 4f 52 54 5f 42 55 define N_SORT_BU
109ed 43 4b 45 54 20 20 33 32 0a 73 74 61 74 69 63 20 CKET 32.static
109ee 50 67 48 64 72 20 2a 70 63 61 63 68 65 53 6f 72 PgHdr *pcacheSor
109ef 74 44 69 72 74 79 4c 69 73 74 28 50 67 48 64 72 tDirtyList(PgHdr
109f0 20 2a 70 49 6e 29 7b 0a 20 20 50 67 48 64 72 20 *pIn){. PgHdr
109f1 2a 61 5b 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 *a[N_SORT_BUCKET
109f2 5d 2c 20 2a 70 3b 0a 20 20 69 6e 74 20 69 3b 0a ], *p;. int i;.
109f3 20 20 6d 65 6d 73 65 74 28 61 2c 20 30 2c 20 73 memset(a, 0, s
109f4 69 7a 65 6f 66 28 61 29 29 3b 0a 20 20 77 68 69 izeof(a));. whi
109f5 6c 65 28 20 70 49 6e 20 29 7b 0a 20 20 20 20 70 le( pIn ){. p
109f6 20 3d 20 70 49 6e 3b 0a 20 20 20 20 70 49 6e 20 = pIn;. pIn
109f7 3d 20 70 2d 3e 70 44 69 72 74 79 3b 0a 20 20 20 = p->pDirty;.
109f8 20 70 2d 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a p->pDirty = 0;.
109f9 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 41 4c 57 for(i=0; ALW
109fa 41 59 53 28 69 3c 4e 5f 53 4f 52 54 5f 42 55 43 AYS(i<N_SORT_BUC
109fb 4b 45 54 2d 31 29 3b 20 69 2b 2b 29 7b 0a 20 20 KET-1); i++){.
109fc 20 20 20 20 69 66 28 20 61 5b 69 5d 3d 3d 30 20 if( a[i]==0
109fd 29 7b 0a 20 20 20 20 20 20 20 20 61 5b 69 5d 20 ){. a[i]
109fe 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 62 72 65 = p;. bre
109ff 61 6b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b ak;. }else{
10a00 0a 20 20 20 20 20 20 20 20 70 20 3d 20 70 63 61 . p = pca
10a01 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c 69 73 cheMergeDirtyLis
10a02 74 28 61 5b 69 5d 2c 20 70 29 3b 0a 20 20 20 20 t(a[i], p);.
10a03 20 20 20 20 61 5b 69 5d 20 3d 20 30 3b 0a 20 20 a[i] = 0;.
10a04 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
10a05 69 66 28 20 4e 45 56 45 52 28 69 3d 3d 4e 5f 53 if( NEVER(i==N_S
10a06 4f 52 54 5f 42 55 43 4b 45 54 2d 31 29 20 29 7b ORT_BUCKET-1) ){
10a07 0a 20 20 20 20 20 20 2f 2a 20 54 6f 20 67 65 74 . /* To get
10a08 20 68 65 72 65 2c 20 74 68 65 72 65 20 6e 65 65 here, there nee
10a09 64 20 74 6f 20 62 65 20 32 5e 28 4e 5f 53 4f 52 d to be 2^(N_SOR
10a0a 54 5f 42 55 43 4b 45 54 29 20 65 6c 65 6d 65 6e T_BUCKET) elemen
10a0b 74 73 20 69 6e 0a 20 20 20 20 20 20 2a 2a 20 74 ts in. ** t
10a0c 68 65 20 69 6e 70 75 74 20 6c 69 73 74 2e 20 20 he input list.
10a0d 42 75 74 20 74 68 61 74 20 69 73 20 69 6d 70 6f But that is impo
10a0e 73 73 69 62 6c 65 2e 0a 20 20 20 20 20 20 2a 2f ssible.. */
10a0f 0a 20 20 20 20 20 20 61 5b 69 5d 20 3d 20 70 63 . a[i] = pc
10a10 61 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c 69 acheMergeDirtyLi
10a11 73 74 28 61 5b 69 5d 2c 20 70 29 3b 0a 20 20 20 st(a[i], p);.
10a12 20 7d 0a 20 20 7d 0a 20 20 70 20 3d 20 61 5b 30 }. }. p = a[0
10a13 5d 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c ];. for(i=1; i<
10a14 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 3b 20 69 N_SORT_BUCKET; i
10a15 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 70 63 61 ++){. p = pca
10a16 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c 69 73 cheMergeDirtyLis
10a17 74 28 70 2c 20 61 5b 69 5d 29 3b 0a 20 20 7d 0a t(p, a[i]);. }.
10a18 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f return p;.}../
10a19 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 6c 69 *.** Return a li
10a1a 73 74 20 6f 66 20 61 6c 6c 20 64 69 72 74 79 20 st of all dirty
10a1b 70 61 67 65 73 20 69 6e 20 74 68 65 20 63 61 63 pages in the cac
10a1c 68 65 2c 20 73 6f 72 74 65 64 20 62 79 20 70 61 he, sorted by pa
10a1d 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 53 51 ge number..*/.SQ
10a1e 4c 49 54 45 5f 50 52 49 56 41 54 45 20 50 67 48 LITE_PRIVATE PgH
10a1f 64 72 20 2a 73 71 6c 69 74 65 33 50 63 61 63 68 dr *sqlite3Pcach
10a20 65 44 69 72 74 79 4c 69 73 74 28 50 43 61 63 68 eDirtyList(PCach
10a21 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67 e *pCache){. Pg
10a22 48 64 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d Hdr *p;. for(p=
10a23 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 3b 20 pCache->pDirty;
10a24 70 3b 20 70 3d 70 2d 3e 70 44 69 72 74 79 4e 65 p; p=p->pDirtyNe
10a25 78 74 29 7b 0a 20 20 20 20 70 2d 3e 70 44 69 72 xt){. p->pDir
10a26 74 79 20 3d 20 70 2d 3e 70 44 69 72 74 79 4e 65 ty = p->pDirtyNe
10a27 78 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e xt;. }. return
10a28 20 70 63 61 63 68 65 53 6f 72 74 44 69 72 74 79 pcacheSortDirty
10a29 4c 69 73 74 28 70 43 61 63 68 65 2d 3e 70 44 69 List(pCache->pDi
10a2a 72 74 79 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 rty);.}../* .**
10a2b 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74 61 6c Return the total
10a2c 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 number of refer
10a2d 65 6e 63 65 64 20 70 61 67 65 73 20 68 65 6c 64 enced pages held
10a2e 20 62 79 20 74 68 65 20 63 61 63 68 65 2e 0a 2a by the cache..*
10a2f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
10a30 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 int sqlite3Pcac
10a31 68 65 52 65 66 43 6f 75 6e 74 28 50 43 61 63 68 heRefCount(PCach
10a32 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 72 65 e *pCache){. re
10a33 74 75 72 6e 20 70 43 61 63 68 65 2d 3e 6e 52 65 turn pCache->nRe
10a34 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 f;.}../*.** Retu
10a35 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rn the number of
10a36 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 references to t
10a37 68 65 20 70 61 67 65 20 73 75 70 70 6c 69 65 64 he page supplied
10a38 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e as an argument.
10a39 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
10a3a 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 TE int sqlite3Pc
10a3b 61 63 68 65 50 61 67 65 52 65 66 63 6f 75 6e 74 achePageRefcount
10a3c 28 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 72 65 (PgHdr *p){. re
10a3d 74 75 72 6e 20 70 2d 3e 6e 52 65 66 3b 0a 7d 0a turn p->nRef;.}.
10a3e 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ./* .** Return t
10a3f 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 he total number
10a40 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 of pages in the
10a41 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 cache..*/.SQLITE
10a42 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
10a43 69 74 65 33 50 63 61 63 68 65 50 61 67 65 63 6f ite3PcachePageco
10a44 75 6e 74 28 50 43 61 63 68 65 20 2a 70 43 61 63 unt(PCache *pCac
10a45 68 65 29 7b 0a 20 20 69 6e 74 20 6e 50 61 67 65 he){. int nPage
10a46 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 43 61 63 = 0;. if( pCac
10a47 68 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 he->pCache ){.
10a48 20 20 6e 50 61 67 65 20 3d 20 73 71 6c 69 74 65 nPage = sqlite
10a49 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 3GlobalConfig.pc
10a4a 61 63 68 65 2e 78 50 61 67 65 63 6f 75 6e 74 28 ache.xPagecount(
10a4b 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 29 3b pCache->pCache);
10a4c 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 50 . }. return nP
10a4d 61 67 65 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 age;.}..#ifdef S
10a4e 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a QLITE_TEST./*.**
10a4f 20 47 65 74 20 74 68 65 20 73 75 67 67 65 73 74 Get the suggest
10a50 65 64 20 63 61 63 68 65 2d 73 69 7a 65 20 76 61 ed cache-size va
10a51 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 lue..*/.SQLITE_P
10a52 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
10a53 65 33 50 63 61 63 68 65 47 65 74 43 61 63 68 65 e3PcacheGetCache
10a54 73 69 7a 65 28 50 43 61 63 68 65 20 2a 70 43 61 size(PCache *pCa
10a55 63 68 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 che){. return p
10a56 43 61 63 68 65 2d 3e 6e 4d 61 78 3b 0a 7d 0a 23 Cache->nMax;.}.#
10a57 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 endif../*.** Set
10a58 20 74 68 65 20 73 75 67 67 65 73 74 65 64 20 63 the suggested c
10a59 61 63 68 65 2d 73 69 7a 65 20 76 61 6c 75 65 2e ache-size value.
10a5a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
10a5b 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
10a5c 63 61 63 68 65 53 65 74 43 61 63 68 65 73 69 7a cacheSetCachesiz
10a5d 65 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 e(PCache *pCache
10a5e 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 , int mxPage){.
10a5f 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 20 3d 20 pCache->nMax =
10a60 6d 78 50 61 67 65 3b 0a 20 20 69 66 28 20 70 43 mxPage;. if( pC
10a61 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a ache->pCache ){.
10a62 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 sqlite3Globa
10a63 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 lConfig.pcache.x
10a64 43 61 63 68 65 73 69 7a 65 28 70 43 61 63 68 65 Cachesize(pCache
10a65 2d 3e 70 43 61 63 68 65 2c 20 6d 78 50 61 67 65 ->pCache, mxPage
10a66 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 20 64 65 );. }.}..#if de
10a67 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 48 45 fined(SQLITE_CHE
10a68 43 4b 5f 50 41 47 45 53 29 20 7c 7c 20 64 65 66 CK_PAGES) || def
10a69 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 ined(SQLITE_DEBU
10a6a 47 29 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 61 6c 6c G)./*.** For all
10a6b 20 64 69 72 74 79 20 70 61 67 65 73 20 63 75 72 dirty pages cur
10a6c 72 65 6e 74 6c 79 20 69 6e 20 74 68 65 20 63 61 rently in the ca
10a6d 63 68 65 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 che, invoke the
10a6e 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 63 61 6c specified.** cal
10a6f 6c 62 61 63 6b 2e 20 54 68 69 73 20 69 73 20 6f lback. This is o
10a70 6e 6c 79 20 75 73 65 64 20 69 66 20 74 68 65 20 nly used if the
10a71 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 SQLITE_CHECK_PAG
10a72 45 53 20 6d 61 63 72 6f 20 69 73 0a 2a 2a 20 64 ES macro is.** d
10a73 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 efined..*/.SQLIT
10a74 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
10a75 71 6c 69 74 65 33 50 63 61 63 68 65 49 74 65 72 qlite3PcacheIter
10a76 61 74 65 44 69 72 74 79 28 50 43 61 63 68 65 20 ateDirty(PCache
10a77 2a 70 43 61 63 68 65 2c 20 76 6f 69 64 20 28 2a *pCache, void (*
10a78 78 49 74 65 72 29 28 50 67 48 64 72 20 2a 29 29 xIter)(PgHdr *))
10a79 7b 0a 20 20 50 67 48 64 72 20 2a 70 44 69 72 74 {. PgHdr *pDirt
10a7a 79 3b 0a 20 20 66 6f 72 28 70 44 69 72 74 79 3d y;. for(pDirty=
10a7b 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 3b 20 pCache->pDirty;
10a7c 70 44 69 72 74 79 3b 20 70 44 69 72 74 79 3d 70 pDirty; pDirty=p
10a7d 44 69 72 74 79 2d 3e 70 44 69 72 74 79 4e 65 78 Dirty->pDirtyNex
10a7e 74 29 7b 0a 20 20 20 20 78 49 74 65 72 28 70 44 t){. xIter(pD
10a7f 69 72 74 79 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e irty);. }.}.#en
10a80 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a dif../**********
10a81 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 63 61 63 **** End of pcac
10a82 68 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a he.c ***********
10a83 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a84 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a85 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
10a86 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
10a87 70 63 61 63 68 65 31 2e 63 20 2a 2a 2a 2a 2a 2a pcache1.c ******
10a88 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a89 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a8a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 ***/./*.** 2008
10a8b 4e 6f 76 65 6d 62 65 72 20 30 35 0a 2a 2a 0a 2a November 05.**.*
10a8c 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
10a8d 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
10a8e 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
10a8f 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
10a90 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
10a91 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
10a92 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
10a93 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
10a94 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
10a95 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
10a96 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
10a97 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
10a98 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
10a99 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
10a9a 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
10a9b 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
10a9c 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
10a9d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
10a9e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a9f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10aa1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
10aa2 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 This file imple
10aa3 6d 65 6e 74 73 20 74 68 65 20 64 65 66 61 75 6c ments the defaul
10aa4 74 20 70 61 67 65 20 63 61 63 68 65 20 69 6d 70 t page cache imp
10aa5 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 28 74 68 65 lementation (the
10aa6 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63 .** sqlite3_pcac
10aa7 68 65 20 69 6e 74 65 72 66 61 63 65 29 2e 20 49 he interface). I
10aa8 74 20 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 73 20 t also contains
10aa9 70 61 72 74 20 6f 66 20 74 68 65 20 69 6d 70 6c part of the impl
10aaa 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f 66 ementation.** of
10aab 20 74 68 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 the SQLITE_CONF
10aac 49 47 5f 50 41 47 45 43 41 43 48 45 20 61 6e 64 IG_PAGECACHE and
10aad 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 sqlite3_release
10aae 5f 6d 65 6d 6f 72 79 28 29 20 66 65 61 74 75 72 _memory() featur
10aaf 65 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 64 65 es..** If the de
10ab0 66 61 75 6c 74 20 70 61 67 65 20 63 61 63 68 65 fault page cache
10ab1 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
10ab2 69 73 20 6f 76 65 72 72 69 64 65 6e 2c 20 74 68 is overriden, th
10ab3 65 6e 20 6e 65 69 74 68 65 72 20 6f 66 0a 2a 2a en neither of.**
10ab4 20 74 68 65 73 65 20 74 77 6f 20 66 65 61 74 75 these two featu
10ab5 72 65 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c res are availabl
10ab6 65 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 e..**.** @(#) $I
10ab7 64 3a 20 70 63 61 63 68 65 31 2e 63 2c 76 20 31 d: pcache1.c,v 1
10ab8 2e 31 39 20 32 30 30 39 2f 30 37 2f 31 37 20 31 .19 2009/07/17 1
10ab9 31 3a 34 34 3a 30 37 20 64 72 68 20 45 78 70 20 1:44:07 drh Exp
10aba 24 0a 2a 2f 0a 0a 0a 74 79 70 65 64 65 66 20 73 $.*/...typedef s
10abb 74 72 75 63 74 20 50 43 61 63 68 65 31 20 50 43 truct PCache1 PC
10abc 61 63 68 65 31 3b 0a 74 79 70 65 64 65 66 20 73 ache1;.typedef s
10abd 74 72 75 63 74 20 50 67 48 64 72 31 20 50 67 48 truct PgHdr1 PgH
10abe 64 72 31 3b 0a 74 79 70 65 64 65 66 20 73 74 72 dr1;.typedef str
10abf 75 63 74 20 50 67 46 72 65 65 73 6c 6f 74 20 50 uct PgFreeslot P
10ac0 67 46 72 65 65 73 6c 6f 74 3b 0a 0a 2f 2a 20 50 gFreeslot;../* P
10ac1 6f 69 6e 74 65 72 73 20 74 6f 20 73 74 72 75 63 ointers to struc
10ac2 74 75 72 65 73 20 6f 66 20 74 68 69 73 20 74 79 tures of this ty
10ac3 70 65 20 61 72 65 20 63 61 73 74 20 61 6e 64 20 pe are cast and
10ac4 72 65 74 75 72 6e 65 64 20 61 73 20 0a 2a 2a 20 returned as .**
10ac5 6f 70 61 71 75 65 20 73 71 6c 69 74 65 33 5f 70 opaque sqlite3_p
10ac6 63 61 63 68 65 2a 20 68 61 6e 64 6c 65 73 0a 2a cache* handles.*
10ac7 2f 0a 73 74 72 75 63 74 20 50 43 61 63 68 65 31 /.struct PCache1
10ac8 20 7b 0a 20 20 2f 2a 20 43 61 63 68 65 20 63 6f {. /* Cache co
10ac9 6e 66 69 67 75 72 61 74 69 6f 6e 20 70 61 72 61 nfiguration para
10aca 6d 65 74 65 72 73 2e 20 50 61 67 65 20 73 69 7a meters. Page siz
10acb 65 20 28 73 7a 50 61 67 65 29 20 61 6e 64 20 74 e (szPage) and t
10acc 68 65 20 70 75 72 67 65 61 62 6c 65 0a 20 20 2a he purgeable. *
10acd 2a 20 66 6c 61 67 20 28 62 50 75 72 67 65 61 62 * flag (bPurgeab
10ace 6c 65 29 20 61 72 65 20 73 65 74 20 77 68 65 6e le) are set when
10acf 20 74 68 65 20 63 61 63 68 65 20 69 73 20 63 72 the cache is cr
10ad0 65 61 74 65 64 2e 20 6e 4d 61 78 20 6d 61 79 20 eated. nMax may
10ad1 62 65 20 0a 20 20 2a 2a 20 6d 6f 64 69 66 69 65 be . ** modifie
10ad2 64 20 61 74 20 61 6e 79 20 74 69 6d 65 20 62 79 d at any time by
10ad3 20 61 20 63 61 6c 6c 20 74 6f 20 74 68 65 20 70 a call to the p
10ad4 63 61 63 68 65 31 43 61 63 68 65 53 69 7a 65 28 cache1CacheSize(
10ad5 29 20 6d 65 74 68 6f 64 2e 0a 20 20 2a 2a 20 54 ) method.. ** T
10ad6 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 he global mutex
10ad7 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 65 must be held whe
10ad8 6e 20 61 63 63 65 73 73 69 6e 67 20 6e 4d 61 78 n accessing nMax
10ad9 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 50 .. */. int szP
10ada 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 age;
10adb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10adc 53 69 7a 65 20 6f 66 20 61 6c 6c 6f 63 61 74 65 Size of allocate
10add 64 20 70 61 67 65 73 20 69 6e 20 62 79 74 65 73 d pages in bytes
10ade 20 2a 2f 0a 20 20 69 6e 74 20 62 50 75 72 67 65 */. int bPurge
10adf 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 able;
10ae0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
10ae1 65 20 69 66 20 63 61 63 68 65 20 69 73 20 70 75 e if cache is pu
10ae2 72 67 65 61 62 6c 65 20 2a 2f 0a 20 20 75 6e 73 rgeable */. uns
10ae3 69 67 6e 65 64 20 69 6e 74 20 6e 4d 69 6e 3b 20 igned int nMin;
10ae4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10ae5 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 6e 75 6d 62 /* Minimum numb
10ae6 65 72 20 6f 66 20 70 61 67 65 73 20 72 65 73 65 er of pages rese
10ae7 72 76 65 64 20 2a 2f 0a 20 20 75 6e 73 69 67 6e rved */. unsign
10ae8 65 64 20 69 6e 74 20 6e 4d 61 78 3b 20 20 20 20 ed int nMax;
10ae9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10aea 20 43 6f 6e 66 69 67 75 72 65 64 20 22 63 61 63 Configured "cac
10aeb 68 65 5f 73 69 7a 65 22 20 76 61 6c 75 65 20 2a he_size" value *
10aec 2f 0a 0a 20 20 2f 2a 20 48 61 73 68 20 74 61 62 /.. /* Hash tab
10aed 6c 65 20 6f 66 20 61 6c 6c 20 70 61 67 65 73 2e le of all pages.
10aee 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 The following v
10aef 61 72 69 61 62 6c 65 73 20 6d 61 79 20 6f 6e 6c ariables may onl
10af0 79 20 62 65 20 61 63 63 65 73 73 65 64 0a 20 20 y be accessed.
10af1 2a 2a 20 77 68 65 6e 20 74 68 65 20 61 63 63 65 ** when the acce
10af2 73 73 6f 72 20 69 73 20 68 6f 6c 64 69 6e 67 20 ssor is holding
10af3 74 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 the global mutex
10af4 20 28 73 65 65 20 70 63 61 63 68 65 31 45 6e 74 (see pcache1Ent
10af5 65 72 4d 75 74 65 78 28 29 20 0a 20 20 2a 2a 20 erMutex() . **
10af6 61 6e 64 20 70 63 61 63 68 65 31 4c 65 61 76 65 and pcache1Leave
10af7 4d 75 74 65 78 28 29 29 2e 0a 20 20 2a 2f 0a 20 Mutex()).. */.
10af8 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 52 unsigned int nR
10af9 65 63 79 63 6c 61 62 6c 65 3b 20 20 20 20 20 20 ecyclable;
10afa 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
10afb 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 4c f pages in the L
10afc 52 55 20 6c 69 73 74 20 2a 2f 0a 20 20 75 6e 73 RU list */. uns
10afd 69 67 6e 65 64 20 69 6e 74 20 6e 50 61 67 65 3b igned int nPage;
10afe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10aff 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 /* Total number
10b00 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 70 48 of pages in apH
10b01 61 73 68 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 ash */. unsigne
10b02 64 20 69 6e 74 20 6e 48 61 73 68 3b 20 20 20 20 d int nHash;
10b03 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10b04 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 Number of slots
10b05 69 6e 20 61 70 48 61 73 68 5b 5d 20 2a 2f 0a 20 in apHash[] */.
10b06 20 50 67 48 64 72 31 20 2a 2a 61 70 48 61 73 68 PgHdr1 **apHash
10b07 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
10b08 20 20 20 20 20 2f 2a 20 48 61 73 68 20 74 61 62 /* Hash tab
10b09 6c 65 20 66 6f 72 20 66 61 73 74 20 6c 6f 6f 6b le for fast look
10b0a 75 70 20 62 79 20 6b 65 79 20 2a 2f 0a 0a 20 20 up by key */..
10b0b 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4d 61 unsigned int iMa
10b0c 78 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 xKey;
10b0d 20 20 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 6b /* Largest k
10b0e 65 79 20 73 65 65 6e 20 73 69 6e 63 65 20 78 54 ey seen since xT
10b0f 72 75 6e 63 61 74 65 28 29 20 2a 2f 0a 7d 3b 0a runcate() */.};.
10b10 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 63 61 63 68 ./*.** Each cach
10b11 65 20 65 6e 74 72 79 20 69 73 20 72 65 70 72 65 e entry is repre
10b12 73 65 6e 74 65 64 20 62 79 20 61 6e 20 69 6e 73 sented by an ins
10b13 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
10b14 6c 6f 77 69 6e 67 20 0a 2a 2a 20 73 74 72 75 63 lowing .** struc
10b15 74 75 72 65 2e 20 41 20 62 75 66 66 65 72 20 6f ture. A buffer o
10b16 66 20 50 67 48 64 72 31 2e 70 43 61 63 68 65 2d f PgHdr1.pCache-
10b17 3e 73 7a 50 61 67 65 20 62 79 74 65 73 20 69 73 >szPage bytes is
10b18 20 61 6c 6c 6f 63 61 74 65 64 20 0a 2a 2a 20 64 allocated .** d
10b19 69 72 65 63 74 6c 79 20 62 65 66 6f 72 65 20 74 irectly before t
10b1a 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 6e his structure in
10b1b 20 6d 65 6d 6f 72 79 20 28 73 65 65 20 74 68 65 memory (see the
10b1c 20 50 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 PGHDR1_TO_PAGE(
10b1d 29 20 0a 2a 2a 20 6d 61 63 72 6f 20 62 65 6c 6f ) .** macro belo
10b1e 77 29 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 67 w)..*/.struct Pg
10b1f 48 64 72 31 20 7b 0a 20 20 75 6e 73 69 67 6e 65 Hdr1 {. unsigne
10b20 64 20 69 6e 74 20 69 4b 65 79 3b 20 20 20 20 20 d int iKey;
10b21 20 20 20 20 20 20 20 20 2f 2a 20 4b 65 79 20 76 /* Key v
10b22 61 6c 75 65 20 28 70 61 67 65 20 6e 75 6d 62 65 alue (page numbe
10b23 72 29 20 2a 2f 0a 20 20 50 67 48 64 72 31 20 2a r) */. PgHdr1 *
10b24 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 pNext;
10b25 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 69 /* Next i
10b26 6e 20 68 61 73 68 20 74 61 62 6c 65 20 63 68 61 n hash table cha
10b27 69 6e 20 2a 2f 0a 20 20 50 43 61 63 68 65 31 20 in */. PCache1
10b28 2a 70 43 61 63 68 65 3b 20 20 20 20 20 20 20 20 *pCache;
10b29 20 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65 20 /* Cache
10b2a 74 68 61 74 20 63 75 72 72 65 6e 74 6c 79 20 6f that currently o
10b2b 77 6e 73 20 74 68 69 73 20 70 61 67 65 20 2a 2f wns this page */
10b2c 0a 20 20 50 67 48 64 72 31 20 2a 70 4c 72 75 4e . PgHdr1 *pLruN
10b2d 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 ext;
10b2e 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 4c 52 55 /* Next in LRU
10b2f 20 6c 69 73 74 20 6f 66 20 75 6e 70 69 6e 6e 65 list of unpinne
10b30 64 20 70 61 67 65 73 20 2a 2f 0a 20 20 50 67 48 d pages */. PgH
10b31 64 72 31 20 2a 70 4c 72 75 50 72 65 76 3b 20 20 dr1 *pLruPrev;
10b32 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
10b33 72 65 76 69 6f 75 73 20 69 6e 20 4c 52 55 20 6c revious in LRU l
10b34 69 73 74 20 6f 66 20 75 6e 70 69 6e 6e 65 64 20 ist of unpinned
10b35 70 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a pages */.};../*.
10b36 2a 2a 20 46 72 65 65 20 73 6c 6f 74 73 20 69 6e ** Free slots in
10b37 20 74 68 65 20 61 6c 6c 6f 63 61 74 6f 72 20 75 the allocator u
10b38 73 65 64 20 74 6f 20 64 69 76 69 64 65 20 75 70 sed to divide up
10b39 20 74 68 65 20 62 75 66 66 65 72 20 70 72 6f 76 the buffer prov
10b3a 69 64 65 64 20 75 73 69 6e 67 0a 2a 2a 20 74 68 ided using.** th
10b3b 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f e SQLITE_CONFIG_
10b3c 50 41 47 45 43 41 43 48 45 20 6d 65 63 68 61 6e PAGECACHE mechan
10b3d 69 73 6d 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 ism..*/.struct P
10b3e 67 46 72 65 65 73 6c 6f 74 20 7b 0a 20 20 50 67 gFreeslot {. Pg
10b3f 46 72 65 65 73 6c 6f 74 20 2a 70 4e 65 78 74 3b Freeslot *pNext;
10b40 20 20 2f 2a 20 4e 65 78 74 20 66 72 65 65 20 73 /* Next free s
10b41 6c 6f 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a lot */.};../*.**
10b42 20 47 6c 6f 62 61 6c 20 64 61 74 61 20 75 73 65 Global data use
10b43 64 20 62 79 20 74 68 69 73 20 63 61 63 68 65 2e d by this cache.
10b44 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54 .*/.static SQLIT
10b45 45 5f 57 53 44 20 73 74 72 75 63 74 20 50 43 61 E_WSD struct PCa
10b46 63 68 65 47 6c 6f 62 61 6c 20 7b 0a 20 20 73 71 cheGlobal {. sq
10b47 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 lite3_mutex *mut
10b48 65 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ex;
10b49 20 20 2f 2a 20 73 74 61 74 69 63 20 6d 75 74 65 /* static mute
10b4a 78 20 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c x MUTEX_STATIC_L
10b4b 52 55 20 2a 2f 0a 0a 20 20 69 6e 74 20 6e 4d 61 RU */.. int nMa
10b4c 78 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 xPage;
10b4d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10b4e 53 75 6d 20 6f 66 20 6e 4d 61 78 50 61 67 65 20 Sum of nMaxPage
10b4f 66 6f 72 20 70 75 72 67 65 61 62 6c 65 20 63 61 for purgeable ca
10b50 63 68 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d ches */. int nM
10b51 69 6e 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 inPage;
10b52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10b53 20 53 75 6d 20 6f 66 20 6e 4d 69 6e 50 61 67 65 Sum of nMinPage
10b54 20 66 6f 72 20 70 75 72 67 65 61 62 6c 65 20 63 for purgeable c
10b55 61 63 68 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e aches */. int n
10b56 43 75 72 72 65 6e 74 50 61 67 65 3b 20 20 20 20 CurrentPage;
10b57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
10b58 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 75 72 67 * Number of purg
10b59 65 61 62 6c 65 20 70 61 67 65 73 20 61 6c 6c 6f eable pages allo
10b5a 63 61 74 65 64 20 2a 2f 0a 20 20 50 67 48 64 72 cated */. PgHdr
10b5b 31 20 2a 70 4c 72 75 48 65 61 64 2c 20 2a 70 4c 1 *pLruHead, *pL
10b5c 72 75 54 61 69 6c 3b 20 20 20 20 20 20 20 20 2f ruTail; /
10b5d 2a 20 4c 52 55 20 6c 69 73 74 20 6f 66 20 75 6e * LRU list of un
10b5e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 2a 2f 0a pinned pages */.
10b5f 0a 20 20 2f 2a 20 56 61 72 69 61 62 6c 65 73 20 . /* Variables
10b60 72 65 6c 61 74 65 64 20 74 6f 20 53 51 4c 49 54 related to SQLIT
10b61 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 E_CONFIG_PAGECAC
10b62 48 45 20 73 65 74 74 69 6e 67 73 2e 20 2a 2f 0a HE settings. */.
10b63 20 20 69 6e 74 20 73 7a 53 6c 6f 74 3b 20 20 20 int szSlot;
10b64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10b65 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
10b66 20 65 61 63 68 20 66 72 65 65 20 73 6c 6f 74 20 each free slot
10b67 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 74 61 72 */. void *pStar
10b68 74 2c 20 2a 70 45 6e 64 3b 20 20 20 20 20 20 20 t, *pEnd;
10b69 20 20 20 20 20 20 20 20 20 2f 2a 20 42 6f 75 6e /* Boun
10b6a 64 73 20 6f 66 20 70 61 67 65 63 61 63 68 65 20 ds of pagecache
10b6b 6d 61 6c 6c 6f 63 20 72 61 6e 67 65 20 2a 2f 0a malloc range */.
10b6c 20 20 50 67 46 72 65 65 73 6c 6f 74 20 2a 70 46 PgFreeslot *pF
10b6d 72 65 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ree;
10b6e 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20 70 61 /* Free pa
10b6f 67 65 20 62 6c 6f 63 6b 73 20 2a 2f 0a 20 20 69 ge blocks */. i
10b70 6e 74 20 69 73 49 6e 69 74 3b 20 20 20 20 20 20 nt isInit;
10b71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10b72 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 6e /* True if in
10b73 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 7d 20 70 itialized */.} p
10b74 63 61 63 68 65 31 5f 67 3b 0a 0a 2f 2a 0a 2a 2a cache1_g;../*.**
10b75 20 41 6c 6c 20 63 6f 64 65 20 69 6e 20 74 68 69 All code in thi
10b76 73 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 61 63 s file should ac
10b77 63 65 73 73 20 74 68 65 20 67 6c 6f 62 61 6c 20 cess the global
10b78 73 74 72 75 63 74 75 72 65 20 61 62 6f 76 65 20 structure above
10b79 76 69 61 20 74 68 65 0a 2a 2a 20 61 6c 69 61 73 via the.** alias
10b7a 20 22 70 63 61 63 68 65 31 22 2e 20 54 68 69 73 "pcache1". This
10b7b 20 65 6e 73 75 72 65 73 20 74 68 61 74 20 74 68 ensures that th
10b7c 65 20 57 53 44 20 65 6d 75 6c 61 74 69 6f 6e 20 e WSD emulation
10b7d 69 73 20 75 73 65 64 20 77 68 65 6e 0a 2a 2a 20 is used when.**
10b7e 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20 73 79 compiling for sy
10b7f 73 74 65 6d 73 20 74 68 61 74 20 64 6f 20 6e 6f stems that do no
10b80 74 20 73 75 70 70 6f 72 74 20 72 65 61 6c 20 57 t support real W
10b81 53 44 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 70 SD..*/.#define p
10b82 63 61 63 68 65 31 20 28 47 4c 4f 42 41 4c 28 73 cache1 (GLOBAL(s
10b83 74 72 75 63 74 20 50 43 61 63 68 65 47 6c 6f 62 truct PCacheGlob
10b84 61 6c 2c 20 70 63 61 63 68 65 31 5f 67 29 29 0a al, pcache1_g)).
10b85 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 50 67 ./*.** When a Pg
10b86 48 64 72 31 20 73 74 72 75 63 74 75 72 65 20 69 Hdr1 structure i
10b87 73 20 61 6c 6c 6f 63 61 74 65 64 2c 20 74 68 65 s allocated, the
10b88 20 61 73 73 6f 63 69 61 74 65 64 20 50 43 61 63 associated PCac
10b89 68 65 31 2e 73 7a 50 61 67 65 0a 2a 2a 20 62 79 he1.szPage.** by
10b8a 74 65 73 20 6f 66 20 64 61 74 61 20 61 72 65 20 tes of data are
10b8b 6c 6f 63 61 74 65 64 20 64 69 72 65 63 74 6c 79 located directly
10b8c 20 62 65 66 6f 72 65 20 69 74 20 69 6e 20 6d 65 before it in me
10b8d 6d 6f 72 79 20 28 69 2e 65 2e 20 74 68 65 20 74 mory (i.e. the t
10b8e 6f 74 61 6c 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 otal.** size of
10b8f 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 the allocation i
10b90 73 20 73 69 7a 65 6f 66 28 50 67 48 64 72 31 29 s sizeof(PgHdr1)
10b91 2b 50 43 61 63 68 65 31 2e 73 7a 50 61 67 65 20 +PCache1.szPage
10b92 62 79 74 65 29 2e 20 54 68 65 0a 2a 2a 20 50 47 byte). The.** PG
10b93 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 29 20 6d HDR1_TO_PAGE() m
10b94 61 63 72 6f 20 74 61 6b 65 73 20 61 20 70 6f 69 acro takes a poi
10b95 6e 74 65 72 20 74 6f 20 61 20 50 67 48 64 72 31 nter to a PgHdr1
10b96 20 73 74 72 75 63 74 75 72 65 20 61 73 0a 2a 2a structure as.**
10b97 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 61 6e 64 an argument and
10b98 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 returns a point
10b99 65 72 20 74 6f 20 74 68 65 20 61 73 73 6f 63 69 er to the associ
10b9a 61 74 65 64 20 62 6c 6f 63 6b 20 6f 66 20 73 7a ated block of sz
10b9b 50 61 67 65 0a 2a 2a 20 62 79 74 65 73 2e 20 54 Page.** bytes. T
10b9c 68 65 20 50 41 47 45 5f 54 4f 5f 50 47 48 44 52 he PAGE_TO_PGHDR
10b9d 31 28 29 20 6d 61 63 72 6f 20 64 6f 65 73 20 74 1() macro does t
10b9e 68 65 20 6f 70 70 6f 73 69 74 65 3a 20 69 74 73 he opposite: its
10b9f 20 61 72 67 75 6d 65 6e 74 20 69 73 0a 2a 2a 20 argument is.**
10ba0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 62 a pointer to a b
10ba1 6c 6f 63 6b 20 6f 66 20 73 7a 50 61 67 65 20 62 lock of szPage b
10ba2 79 74 65 73 20 6f 66 20 64 61 74 61 20 61 6e 64 ytes of data and
10ba3 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 the return valu
10ba4 65 20 69 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 e is.** a pointe
10ba5 72 20 74 6f 20 74 68 65 20 61 73 73 6f 63 69 61 r to the associa
10ba6 74 65 64 20 50 67 48 64 72 31 20 73 74 72 75 63 ted PgHdr1 struc
10ba7 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 61 73 ture..**.** as
10ba8 73 65 72 74 28 20 50 47 48 44 52 31 5f 54 4f 5f sert( PGHDR1_TO_
10ba9 50 41 47 45 28 50 41 47 45 5f 54 4f 5f 50 47 48 PAGE(PAGE_TO_PGH
10baa 44 52 31 28 70 43 61 63 68 65 2c 20 58 29 29 3d DR1(pCache, X))=
10bab 3d 58 20 29 3b 0a 2a 2f 0a 23 64 65 66 69 6e 65 =X );.*/.#define
10bac 20 50 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 PGHDR1_TO_PAGE(
10bad 70 29 20 20 20 20 28 76 6f 69 64 2a 29 28 28 28 p) (void*)(((
10bae 63 68 61 72 2a 29 70 29 20 2d 20 70 2d 3e 70 43 char*)p) - p->pC
10baf 61 63 68 65 2d 3e 73 7a 50 61 67 65 29 0a 23 64 ache->szPage).#d
10bb0 65 66 69 6e 65 20 50 41 47 45 5f 54 4f 5f 50 47 efine PAGE_TO_PG
10bb1 48 44 52 31 28 63 2c 20 70 29 20 28 50 67 48 64 HDR1(c, p) (PgHd
10bb2 72 31 2a 29 28 28 28 63 68 61 72 2a 29 70 29 20 r1*)(((char*)p)
10bb3 2b 20 63 2d 3e 73 7a 50 61 67 65 29 0a 0a 2f 2a + c->szPage)../*
10bb4 0a 2a 2a 20 4d 61 63 72 6f 73 20 74 6f 20 65 6e .** Macros to en
10bb5 74 65 72 20 61 6e 64 20 6c 65 61 76 65 20 74 68 ter and leave th
10bb6 65 20 67 6c 6f 62 61 6c 20 4c 52 55 20 6d 75 74 e global LRU mut
10bb7 65 78 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 70 ex..*/.#define p
10bb8 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 cache1EnterMutex
10bb9 28 29 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 () sqlite3_mutex
10bba 5f 65 6e 74 65 72 28 70 63 61 63 68 65 31 2e 6d _enter(pcache1.m
10bbb 75 74 65 78 29 0a 23 64 65 66 69 6e 65 20 70 63 utex).#define pc
10bbc 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 ache1LeaveMutex(
10bbd 29 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ) sqlite3_mutex_
10bbe 6c 65 61 76 65 28 70 63 61 63 68 65 31 2e 6d 75 leave(pcache1.mu
10bbf 74 65 78 29 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a tex)../*********
10bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10bc1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10bc2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10bc3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10bc4 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
10bc5 20 50 61 67 65 20 41 6c 6c 6f 63 61 74 69 6f 6e Page Allocation
10bc6 2f 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 /SQLITE_CONFIG_P
10bc7 43 41 43 48 45 20 52 65 6c 61 74 65 64 20 46 75 CACHE Related Fu
10bc8 6e 63 74 69 6f 6e 73 20 2a 2a 2a 2a 2a 2a 2a 2a nctions ********
10bc9 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 ******/../*.** T
10bca 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
10bcb 63 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 69 6e called during in
10bcc 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 69 66 20 itialization if
10bcd 61 20 73 74 61 74 69 63 20 62 75 66 66 65 72 20 a static buffer
10bce 69 73 20 0a 2a 2a 20 73 75 70 70 6c 69 65 64 20 is .** supplied
10bcf 74 6f 20 75 73 65 20 66 6f 72 20 74 68 65 20 70 to use for the p
10bd0 61 67 65 2d 63 61 63 68 65 20 62 79 20 70 61 73 age-cache by pas
10bd1 73 69 6e 67 20 74 68 65 20 53 51 4c 49 54 45 5f sing the SQLITE_
10bd2 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 CONFIG_PAGECACHE
10bd3 0a 2a 2a 20 76 65 72 62 20 74 6f 20 73 71 6c 69 .** verb to sqli
10bd4 74 65 33 5f 63 6f 6e 66 69 67 28 29 2e 20 50 61 te3_config(). Pa
10bd5 72 61 6d 65 74 65 72 20 70 42 75 66 20 70 6f 69 rameter pBuf poi
10bd6 6e 74 73 20 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 nts to an alloca
10bd7 74 69 6f 6e 20 6c 61 72 67 65 0a 2a 2a 20 65 6e tion large.** en
10bd8 6f 75 67 68 20 74 6f 20 63 6f 6e 74 61 69 6e 20 ough to contain
10bd9 27 6e 27 20 62 75 66 66 65 72 73 20 6f 66 20 27 'n' buffers of '
10bda 73 7a 27 20 62 79 74 65 73 20 65 61 63 68 2e 0a sz' bytes each..
10bdb 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
10bdc 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 43 E void sqlite3PC
10bdd 61 63 68 65 42 75 66 66 65 72 53 65 74 75 70 28 acheBufferSetup(
10bde 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 void *pBuf, int
10bdf 73 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 69 66 sz, int n){. if
10be0 28 20 70 63 61 63 68 65 31 2e 69 73 49 6e 69 74 ( pcache1.isInit
10be1 20 29 7b 0a 20 20 20 20 50 67 46 72 65 65 73 6c ){. PgFreesl
10be2 6f 74 20 2a 70 3b 0a 20 20 20 20 73 7a 20 3d 20 ot *p;. sz =
10be3 52 4f 55 4e 44 44 4f 57 4e 38 28 73 7a 29 3b 0a ROUNDDOWN8(sz);.
10be4 20 20 20 20 70 63 61 63 68 65 31 2e 73 7a 53 6c pcache1.szSl
10be5 6f 74 20 3d 20 73 7a 3b 0a 20 20 20 20 70 63 61 ot = sz;. pca
10be6 63 68 65 31 2e 70 53 74 61 72 74 20 3d 20 70 42 che1.pStart = pB
10be7 75 66 3b 0a 20 20 20 20 70 63 61 63 68 65 31 2e uf;. pcache1.
10be8 70 46 72 65 65 20 3d 20 30 3b 0a 20 20 20 20 77 pFree = 0;. w
10be9 68 69 6c 65 28 20 6e 2d 2d 20 29 7b 0a 20 20 20 hile( n-- ){.
10bea 20 20 20 70 20 3d 20 28 50 67 46 72 65 65 73 6c p = (PgFreesl
10beb 6f 74 2a 29 70 42 75 66 3b 0a 20 20 20 20 20 20 ot*)pBuf;.
10bec 70 2d 3e 70 4e 65 78 74 20 3d 20 70 63 61 63 68 p->pNext = pcach
10bed 65 31 2e 70 46 72 65 65 3b 0a 20 20 20 20 20 20 e1.pFree;.
10bee 70 63 61 63 68 65 31 2e 70 46 72 65 65 20 3d 20 pcache1.pFree =
10bef 70 3b 0a 20 20 20 20 20 20 70 42 75 66 20 3d 20 p;. pBuf =
10bf0 28 76 6f 69 64 2a 29 26 28 28 63 68 61 72 2a 29 (void*)&((char*)
10bf1 70 42 75 66 29 5b 73 7a 5d 3b 0a 20 20 20 20 7d pBuf)[sz];. }
10bf2 0a 20 20 20 20 70 63 61 63 68 65 31 2e 70 45 6e . pcache1.pEn
10bf3 64 20 3d 20 70 42 75 66 3b 0a 20 20 7d 0a 7d 0a d = pBuf;. }.}.
10bf4 0a 2f 2a 0a 2a 2a 20 4d 61 6c 6c 6f 63 20 66 75 ./*.** Malloc fu
10bf5 6e 63 74 69 6f 6e 20 75 73 65 64 20 77 69 74 68 nction used with
10bf6 69 6e 20 74 68 69 73 20 66 69 6c 65 20 74 6f 20 in this file to
10bf7 61 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 allocate space f
10bf8 72 6f 6d 20 74 68 65 20 62 75 66 66 65 72 0a 2a rom the buffer.*
10bf9 2a 20 63 6f 6e 66 69 67 75 72 65 64 20 75 73 69 * configured usi
10bfa 6e 67 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 ng sqlite3_confi
10bfb 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f g(SQLITE_CONFIG_
10bfc 50 41 47 45 43 41 43 48 45 29 20 6f 70 74 69 6f PAGECACHE) optio
10bfd 6e 2e 20 49 66 20 6e 6f 20 0a 2a 2a 20 73 75 63 n. If no .** suc
10bfe 68 20 62 75 66 66 65 72 20 65 78 69 73 74 73 20 h buffer exists
10bff 6f 72 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73 or there is no s
10c00 70 61 63 65 20 6c 65 66 74 20 69 6e 20 69 74 2c pace left in it,
10c01 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 this function f
10c02 61 6c 6c 73 20 0a 2a 2a 20 62 61 63 6b 20 74 6f alls .** back to
10c03 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 sqlite3Malloc()
10c04 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
10c05 20 2a 70 63 61 63 68 65 31 41 6c 6c 6f 63 28 69 *pcache1Alloc(i
10c06 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 76 6f 69 nt nByte){. voi
10c07 64 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 d *p;. assert(
10c08 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
10c09 6c 64 28 70 63 61 63 68 65 31 2e 6d 75 74 65 78 ld(pcache1.mutex
10c0a 29 20 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 ) );. if( nByte
10c0b 3c 3d 70 63 61 63 68 65 31 2e 73 7a 53 6c 6f 74 <=pcache1.szSlot
10c0c 20 26 26 20 70 63 61 63 68 65 31 2e 70 46 72 65 && pcache1.pFre
10c0d 65 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 e ){. assert(
10c0e 20 70 63 61 63 68 65 31 2e 69 73 49 6e 69 74 20 pcache1.isInit
10c0f 29 3b 0a 20 20 20 20 70 20 3d 20 28 50 67 48 64 );. p = (PgHd
10c10 72 31 20 2a 29 70 63 61 63 68 65 31 2e 70 46 72 r1 *)pcache1.pFr
10c11 65 65 3b 0a 20 20 20 20 70 63 61 63 68 65 31 2e ee;. pcache1.
10c12 70 46 72 65 65 20 3d 20 70 63 61 63 68 65 31 2e pFree = pcache1.
10c13 70 46 72 65 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 pFree->pNext;.
10c14 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53 sqlite3StatusS
10c15 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 et(SQLITE_STATUS
10c16 5f 50 41 47 45 43 41 43 48 45 5f 53 49 5a 45 2c _PAGECACHE_SIZE,
10c17 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 73 71 6c nByte);. sql
10c18 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 ite3StatusAdd(SQ
10c19 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 LITE_STATUS_PAGE
10c1a 43 41 43 48 45 5f 55 53 45 44 2c 20 31 29 3b 0a CACHE_USED, 1);.
10c1b 20 20 7d 65 6c 73 65 7b 0a 0a 20 20 20 20 2f 2a }else{.. /*
10c1c 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 Allocate a new
10c1d 62 75 66 66 65 72 20 75 73 69 6e 67 20 73 71 6c buffer using sql
10c1e 69 74 65 33 4d 61 6c 6c 6f 63 2e 20 42 65 66 6f ite3Malloc. Befo
10c1f 72 65 20 64 6f 69 6e 67 20 73 6f 2c 20 65 78 69 re doing so, exi
10c20 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 67 6c 6f t the. ** glo
10c21 62 61 6c 20 70 63 61 63 68 65 20 6d 75 74 65 78 bal pcache mutex
10c22 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 65 20 and unlock the
10c23 70 61 67 65 72 2d 63 61 63 68 65 20 6f 62 6a 65 pager-cache obje
10c24 63 74 20 70 43 61 63 68 65 2e 20 54 68 69 73 20 ct pCache. This
10c25 69 73 20 0a 20 20 20 20 2a 2a 20 73 6f 20 74 68 is . ** so th
10c26 61 74 20 69 66 20 74 68 65 20 61 74 74 65 6d 70 at if the attemp
10c27 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 t to allocate a
10c28 6e 65 77 20 62 75 66 66 65 72 20 63 61 75 73 65 new buffer cause
10c29 73 20 74 68 65 20 74 68 65 20 0a 20 20 20 20 2a s the the . *
10c2a 2a 20 63 6f 6e 66 69 67 75 72 65 64 20 73 6f 66 * configured sof
10c2b 74 2d 68 65 61 70 2d 6c 69 6d 69 74 20 74 6f 20 t-heap-limit to
10c2c 62 65 20 62 72 65 61 63 68 65 64 2c 20 69 74 20 be breached, it
10c2d 77 69 6c 6c 20 62 65 20 70 6f 73 73 69 62 6c 65 will be possible
10c2e 20 74 6f 0a 20 20 20 20 2a 2a 20 72 65 63 6c 61 to. ** recla
10c2f 69 6d 20 6d 65 6d 6f 72 79 20 66 72 6f 6d 20 74 im memory from t
10c30 68 69 73 20 70 61 67 65 72 2d 63 61 63 68 65 2e his pager-cache.
10c31 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 63 61 63 . */. pcac
10c32 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b he1LeaveMutex();
10c33 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 . p = sqlite3
10c34 4d 61 6c 6c 6f 63 28 6e 42 79 74 65 29 3b 0a 20 Malloc(nByte);.
10c35 20 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d pcache1EnterM
10c36 75 74 65 78 28 29 3b 0a 20 20 20 20 69 66 28 20 utex();. if(
10c37 70 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 73 p ){. int s
10c38 7a 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f z = sqlite3Mallo
10c39 63 53 69 7a 65 28 70 29 3b 0a 20 20 20 20 20 20 cSize(p);.
10c3a 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 sqlite3StatusAdd
10c3b 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 (SQLITE_STATUS_P
10c3c 41 47 45 43 41 43 48 45 5f 4f 56 45 52 46 4c 4f AGECACHE_OVERFLO
10c3d 57 2c 20 73 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 W, sz);. }.
10c3e 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a }. return p;.}.
10c3f 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 61 ./*.** Free an a
10c40 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 20 llocated buffer
10c41 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 70 63 obtained from pc
10c42 61 63 68 65 31 41 6c 6c 6f 63 28 29 2e 0a 2a 2f ache1Alloc()..*/
10c43 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 .static void pca
10c44 63 68 65 31 46 72 65 65 28 76 6f 69 64 20 2a 70 che1Free(void *p
10c45 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ){. assert( sql
10c46 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
10c47 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 pcache1.mutex) )
10c48 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 ;. if( p==0 ) r
10c49 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 3e 3d eturn;. if( p>=
10c4a 70 63 61 63 68 65 31 2e 70 53 74 61 72 74 20 26 pcache1.pStart &
10c4b 26 20 70 3c 70 63 61 63 68 65 31 2e 70 45 6e 64 & p<pcache1.pEnd
10c4c 20 29 7b 0a 20 20 20 20 50 67 46 72 65 65 73 6c ){. PgFreesl
10c4d 6f 74 20 2a 70 53 6c 6f 74 3b 0a 20 20 20 20 73 ot *pSlot;. s
10c4e 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 qlite3StatusAdd(
10c4f 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 SQLITE_STATUS_PA
10c50 47 45 43 41 43 48 45 5f 55 53 45 44 2c 20 2d 31 GECACHE_USED, -1
10c51 29 3b 0a 20 20 20 20 70 53 6c 6f 74 20 3d 20 28 );. pSlot = (
10c52 50 67 46 72 65 65 73 6c 6f 74 2a 29 70 3b 0a 20 PgFreeslot*)p;.
10c53 20 20 20 70 53 6c 6f 74 2d 3e 70 4e 65 78 74 20 pSlot->pNext
10c54 3d 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 3b = pcache1.pFree;
10c55 0a 20 20 20 20 70 63 61 63 68 65 31 2e 70 46 72 . pcache1.pFr
10c56 65 65 20 3d 20 70 53 6c 6f 74 3b 0a 20 20 7d 65 ee = pSlot;. }e
10c57 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 69 53 69 lse{. int iSi
10c58 7a 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c ze = sqlite3Mall
10c59 6f 63 53 69 7a 65 28 70 29 3b 0a 20 20 20 20 73 ocSize(p);. s
10c5a 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 qlite3StatusAdd(
10c5b 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 SQLITE_STATUS_PA
10c5c 47 45 43 41 43 48 45 5f 4f 56 45 52 46 4c 4f 57 GECACHE_OVERFLOW
10c5d 2c 20 2d 69 53 69 7a 65 29 3b 0a 20 20 20 20 73 , -iSize);. s
10c5e 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a qlite3_free(p);.
10c5f 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c }.}../*.** All
10c60 6f 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 ocate a new page
10c61 20 6f 62 6a 65 63 74 20 69 6e 69 74 69 61 6c 6c object initiall
10c62 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 y associated wit
10c63 68 20 63 61 63 68 65 20 70 43 61 63 68 65 2e 0a h cache pCache..
10c64 2a 2f 0a 73 74 61 74 69 63 20 50 67 48 64 72 31 */.static PgHdr1
10c65 20 2a 70 63 61 63 68 65 31 41 6c 6c 6f 63 50 61 *pcache1AllocPa
10c66 67 65 28 50 43 61 63 68 65 31 20 2a 70 43 61 63 ge(PCache1 *pCac
10c67 68 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 he){. int nByte
10c68 20 3d 20 73 69 7a 65 6f 66 28 50 67 48 64 72 31 = sizeof(PgHdr1
10c69 29 20 2b 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 ) + pCache->szPa
10c6a 67 65 3b 0a 20 20 76 6f 69 64 20 2a 70 50 67 20 ge;. void *pPg
10c6b 3d 20 70 63 61 63 68 65 31 41 6c 6c 6f 63 28 6e = pcache1Alloc(n
10c6c 42 79 74 65 29 3b 0a 20 20 50 67 48 64 72 31 20 Byte);. PgHdr1
10c6d 2a 70 3b 0a 20 20 69 66 28 20 70 50 67 20 29 7b *p;. if( pPg ){
10c6e 0a 20 20 20 20 70 20 3d 20 50 41 47 45 5f 54 4f . p = PAGE_TO
10c6f 5f 50 47 48 44 52 31 28 70 43 61 63 68 65 2c 20 _PGHDR1(pCache,
10c70 70 50 67 29 3b 0a 20 20 20 20 69 66 28 20 70 43 pPg);. if( pC
10c71 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 ache->bPurgeable
10c72 20 29 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 ){. pcache
10c73 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 2b 2b 1.nCurrentPage++
10c74 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b ;. }. }else{
10c75 0a 20 20 20 20 70 20 3d 20 30 3b 0a 20 20 7d 0a . p = 0;. }.
10c76 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f return p;.}../
10c77 2a 0a 2a 2a 20 46 72 65 65 20 61 20 70 61 67 65 *.** Free a page
10c78 20 6f 62 6a 65 63 74 20 61 6c 6c 6f 63 61 74 65 object allocate
10c79 64 20 62 79 20 70 63 61 63 68 65 31 41 6c 6c 6f d by pcache1Allo
10c7a 63 50 61 67 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 cPage()..**.** T
10c7b 68 65 20 70 6f 69 6e 74 65 72 20 69 73 20 61 6c he pointer is al
10c7c 6c 6f 77 65 64 20 74 6f 20 62 65 20 4e 55 4c 4c lowed to be NULL
10c7d 2c 20 77 68 69 63 68 20 69 73 20 70 72 75 64 65 , which is prude
10c7e 6e 74 2e 20 20 42 75 74 20 69 74 20 74 75 72 6e nt. But it turn
10c7f 73 20 6f 75 74 0a 2a 2a 20 74 68 61 74 20 74 68 s out.** that th
10c80 65 20 63 75 72 72 65 6e 74 20 69 6d 70 6c 65 6d e current implem
10c81 65 6e 74 61 74 69 6f 6e 20 68 61 70 70 65 6e 73 entation happens
10c82 20 74 6f 20 6e 65 76 65 72 20 63 61 6c 6c 20 74 to never call t
10c83 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 77 his routine.** w
10c84 69 74 68 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 ith a NULL point
10c85 65 72 2c 20 73 6f 20 77 65 20 6d 61 72 6b 20 74 er, so we mark t
10c86 68 65 20 4e 55 4c 4c 20 74 65 73 74 20 77 69 74 he NULL test wit
10c87 68 20 41 4c 57 41 59 53 28 29 2e 0a 2a 2f 0a 73 h ALWAYS()..*/.s
10c88 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 tatic void pcach
10c89 65 31 46 72 65 65 50 61 67 65 28 50 67 48 64 72 e1FreePage(PgHdr
10c8a 31 20 2a 70 29 7b 0a 20 20 69 66 28 20 41 4c 57 1 *p){. if( ALW
10c8b 41 59 53 28 70 29 20 29 7b 0a 20 20 20 20 69 66 AYS(p) ){. if
10c8c 28 20 70 2d 3e 70 43 61 63 68 65 2d 3e 62 50 75 ( p->pCache->bPu
10c8d 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 rgeable ){.
10c8e 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e pcache1.nCurren
10c8f 74 50 61 67 65 2d 2d 3b 0a 20 20 20 20 7d 0a 20 tPage--;. }.
10c90 20 20 20 70 63 61 63 68 65 31 46 72 65 65 28 50 pcache1Free(P
10c91 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 70 29 GHDR1_TO_PAGE(p)
10c92 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
10c93 4d 61 6c 6c 6f 63 20 66 75 6e 63 74 69 6f 6e 20 Malloc function
10c94 75 73 65 64 20 62 79 20 53 51 4c 69 74 65 20 74 used by SQLite t
10c95 6f 20 6f 62 74 61 69 6e 20 73 70 61 63 65 20 66 o obtain space f
10c96 72 6f 6d 20 74 68 65 20 62 75 66 66 65 72 20 63 rom the buffer c
10c97 6f 6e 66 69 67 75 72 65 64 0a 2a 2a 20 75 73 69 onfigured.** usi
10c98 6e 67 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 ng sqlite3_confi
10c99 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f g(SQLITE_CONFIG_
10c9a 50 41 47 45 43 41 43 48 45 29 20 6f 70 74 69 6f PAGECACHE) optio
10c9b 6e 2e 20 49 66 20 6e 6f 20 73 75 63 68 20 62 75 n. If no such bu
10c9c 66 66 65 72 0a 2a 2a 20 65 78 69 73 74 73 2c 20 ffer.** exists,
10c9d 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 61 this function fa
10c9e 6c 6c 73 20 62 61 63 6b 20 74 6f 20 73 71 6c 69 lls back to sqli
10c9f 74 65 33 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a te3Malloc()..*/.
10ca0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
10ca1 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 oid *sqlite3Page
10ca2 4d 61 6c 6c 6f 63 28 69 6e 74 20 73 7a 29 7b 0a Malloc(int sz){.
10ca3 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 70 63 61 void *p;. pca
10ca4 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 che1EnterMutex()
10ca5 3b 0a 20 20 70 20 3d 20 70 63 61 63 68 65 31 41 ;. p = pcache1A
10ca6 6c 6c 6f 63 28 73 7a 29 3b 0a 20 20 70 63 61 63 lloc(sz);. pcac
10ca7 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b he1LeaveMutex();
10ca8 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a . return p;.}..
10ca9 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 61 6c /*.** Free an al
10caa 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 20 6f located buffer o
10cab 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c btained from sql
10cac 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 29 ite3PageMalloc()
10cad 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
10cae 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
10caf 50 61 67 65 46 72 65 65 28 76 6f 69 64 20 2a 70 PageFree(void *p
10cb0 29 7b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 ){. pcache1Ente
10cb1 72 4d 75 74 65 78 28 29 3b 0a 20 20 70 63 61 63 rMutex();. pcac
10cb2 68 65 31 46 72 65 65 28 70 29 3b 0a 20 20 70 63 he1Free(p);. pc
10cb3 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 ache1LeaveMutex(
10cb4 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a );.}../*********
10cb5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10cb6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10cb7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10cb8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10cb9 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
10cba 20 47 65 6e 65 72 61 6c 20 49 6d 70 6c 65 6d 65 General Impleme
10cbb 6e 74 61 74 69 6f 6e 20 46 75 6e 63 74 69 6f 6e ntation Function
10cbc 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a s **************
10cbd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10cbe 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 ******/../*.** T
10cbf 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
10cc0 75 73 65 64 20 74 6f 20 72 65 73 69 7a 65 20 74 used to resize t
10cc1 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 75 73 he hash table us
10cc2 65 64 20 62 79 20 74 68 65 20 63 61 63 68 65 20 ed by the cache
10cc3 70 61 73 73 65 64 0a 2a 2a 20 61 73 20 74 68 65 passed.** as the
10cc4 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e first argument.
10cc5 0a 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61 .**.** The globa
10cc6 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 l mutex must be
10cc7 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 held when this f
10cc8 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
10cc9 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 d..*/.static int
10cca 20 70 63 61 63 68 65 31 52 65 73 69 7a 65 48 61 pcache1ResizeHa
10ccb 73 68 28 50 43 61 63 68 65 31 20 2a 70 29 7b 0a sh(PCache1 *p){.
10ccc 20 20 50 67 48 64 72 31 20 2a 2a 61 70 4e 65 77 PgHdr1 **apNew
10ccd 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 ;. unsigned int
10cce 20 6e 4e 65 77 3b 0a 20 20 75 6e 73 69 67 6e 65 nNew;. unsigne
10ccf 64 20 69 6e 74 20 69 3b 0a 0a 20 20 61 73 73 65 d int i;.. asse
10cd0 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
10cd1 78 5f 68 65 6c 64 28 70 63 61 63 68 65 31 2e 6d x_held(pcache1.m
10cd2 75 74 65 78 29 20 29 3b 0a 0a 20 20 6e 4e 65 77 utex) );.. nNew
10cd3 20 3d 20 70 2d 3e 6e 48 61 73 68 2a 32 3b 0a 20 = p->nHash*2;.
10cd4 20 69 66 28 20 6e 4e 65 77 3c 32 35 36 20 29 7b if( nNew<256 ){
10cd5 0a 20 20 20 20 6e 4e 65 77 20 3d 20 32 35 36 3b . nNew = 256;
10cd6 0a 20 20 7d 0a 0a 20 20 70 63 61 63 68 65 31 4c . }.. pcache1L
10cd7 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 69 eaveMutex();. i
10cd8 66 28 20 70 2d 3e 6e 48 61 73 68 20 29 7b 20 73 f( p->nHash ){ s
10cd9 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 qlite3BeginBenig
10cda 6e 4d 61 6c 6c 6f 63 28 29 3b 20 7d 0a 20 20 61 nMalloc(); }. a
10cdb 70 4e 65 77 20 3d 20 28 50 67 48 64 72 31 20 2a pNew = (PgHdr1 *
10cdc 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 *)sqlite3_malloc
10cdd 28 73 69 7a 65 6f 66 28 50 67 48 64 72 31 20 2a (sizeof(PgHdr1 *
10cde 29 2a 6e 4e 65 77 29 3b 0a 20 20 69 66 28 20 70 )*nNew);. if( p
10cdf 2d 3e 6e 48 61 73 68 20 29 7b 20 73 71 6c 69 74 ->nHash ){ sqlit
10ce0 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f e3EndBenignMallo
10ce1 63 28 29 3b 20 7d 0a 20 20 70 63 61 63 68 65 31 c(); }. pcache1
10ce2 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 EnterMutex();.
10ce3 69 66 28 20 61 70 4e 65 77 20 29 7b 0a 20 20 20 if( apNew ){.
10ce4 20 6d 65 6d 73 65 74 28 61 70 4e 65 77 2c 20 30 memset(apNew, 0
10ce5 2c 20 73 69 7a 65 6f 66 28 50 67 48 64 72 31 20 , sizeof(PgHdr1
10ce6 2a 29 2a 6e 4e 65 77 29 3b 0a 20 20 20 20 66 6f *)*nNew);. fo
10ce7 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 48 61 73 r(i=0; i<p->nHas
10ce8 68 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 50 h; i++){. P
10ce9 67 48 64 72 31 20 2a 70 50 61 67 65 3b 0a 20 20 gHdr1 *pPage;.
10cea 20 20 20 20 50 67 48 64 72 31 20 2a 70 4e 65 78 PgHdr1 *pNex
10ceb 74 20 3d 20 70 2d 3e 61 70 48 61 73 68 5b 69 5d t = p->apHash[i]
10cec 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 28 ;. while( (
10ced 70 50 61 67 65 20 3d 20 70 4e 65 78 74 29 21 3d pPage = pNext)!=
10cee 30 20 29 7b 0a 20 20 20 20 20 20 20 20 75 6e 73 0 ){. uns
10cef 69 67 6e 65 64 20 69 6e 74 20 68 20 3d 20 70 50 igned int h = pP
10cf0 61 67 65 2d 3e 69 4b 65 79 20 25 20 6e 4e 65 77 age->iKey % nNew
10cf1 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 78 74 20 ;. pNext
10cf2 3d 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 3b 0a = pPage->pNext;.
10cf3 20 20 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70 pPage->p
10cf4 4e 65 78 74 20 3d 20 61 70 4e 65 77 5b 68 5d 3b Next = apNew[h];
10cf5 0a 20 20 20 20 20 20 20 20 61 70 4e 65 77 5b 68 . apNew[h
10cf6 5d 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 20 ] = pPage;.
10cf7 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c }. }. sql
10cf8 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 61 70 48 ite3_free(p->apH
10cf9 61 73 68 29 3b 0a 20 20 20 20 70 2d 3e 61 70 48 ash);. p->apH
10cfa 61 73 68 20 3d 20 61 70 4e 65 77 3b 0a 20 20 20 ash = apNew;.
10cfb 20 70 2d 3e 6e 48 61 73 68 20 3d 20 6e 4e 65 77 p->nHash = nNew
10cfc 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 ;. }.. return
10cfd 28 70 2d 3e 61 70 48 61 73 68 20 3f 20 53 51 4c (p->apHash ? SQL
10cfe 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f ITE_OK : SQLITE_
10cff 4e 4f 4d 45 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a NOMEM);.}../*.**
10d00 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
10d01 73 20 75 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c s used internall
10d02 79 20 74 6f 20 72 65 6d 6f 76 65 20 74 68 65 20 y to remove the
10d03 70 61 67 65 20 70 50 61 67 65 20 66 72 6f 6d 20 page pPage from
10d04 74 68 65 20 0a 2a 2a 20 67 6c 6f 62 61 6c 20 4c the .** global L
10d05 52 55 20 6c 69 73 74 2c 20 69 66 20 69 73 20 70 RU list, if is p
10d06 61 72 74 20 6f 66 20 69 74 2e 20 49 66 20 70 50 art of it. If pP
10d07 61 67 65 20 69 73 20 6e 6f 74 20 70 61 72 74 20 age is not part
10d08 6f 66 20 74 68 65 20 67 6c 6f 62 61 6c 0a 2a 2a of the global.**
10d09 20 4c 52 55 20 6c 69 73 74 2c 20 74 68 65 6e 20 LRU list, then
10d0a 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
10d0b 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 a no-op..**.**
10d0c 54 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 The global mutex
10d0d 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 must be held wh
10d0e 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e en this function
10d0f 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 is called..*/.s
10d10 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 tatic void pcach
10d11 65 31 50 69 6e 50 61 67 65 28 50 67 48 64 72 31 e1PinPage(PgHdr1
10d12 20 2a 70 50 61 67 65 29 7b 0a 20 20 61 73 73 65 *pPage){. asse
10d13 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
10d14 78 5f 68 65 6c 64 28 70 63 61 63 68 65 31 2e 6d x_held(pcache1.m
10d15 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 utex) );. if( p
10d16 50 61 67 65 20 26 26 20 28 70 50 61 67 65 2d 3e Page && (pPage->
10d17 70 4c 72 75 4e 65 78 74 20 7c 7c 20 70 50 61 67 pLruNext || pPag
10d18 65 3d 3d 70 63 61 63 68 65 31 2e 70 4c 72 75 54 e==pcache1.pLruT
10d19 61 69 6c 29 20 29 7b 0a 20 20 20 20 69 66 28 20 ail) ){. if(
10d1a 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 20 pPage->pLruPrev
10d1b 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e ){. pPage->
10d1c 70 4c 72 75 50 72 65 76 2d 3e 70 4c 72 75 4e 65 pLruPrev->pLruNe
10d1d 78 74 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72 75 xt = pPage->pLru
10d1e 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 Next;. }.
10d1f 69 66 28 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e if( pPage->pLruN
10d20 65 78 74 20 29 7b 0a 20 20 20 20 20 20 70 50 61 ext ){. pPa
10d21 67 65 2d 3e 70 4c 72 75 4e 65 78 74 2d 3e 70 4c ge->pLruNext->pL
10d22 72 75 50 72 65 76 20 3d 20 70 50 61 67 65 2d 3e ruPrev = pPage->
10d23 70 4c 72 75 50 72 65 76 3b 0a 20 20 20 20 7d 0a pLruPrev;. }.
10d24 20 20 20 20 69 66 28 20 70 63 61 63 68 65 31 2e if( pcache1.
10d25 70 4c 72 75 48 65 61 64 3d 3d 70 50 61 67 65 20 pLruHead==pPage
10d26 29 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 ){. pcache1
10d27 2e 70 4c 72 75 48 65 61 64 20 3d 20 70 50 61 67 .pLruHead = pPag
10d28 65 2d 3e 70 4c 72 75 4e 65 78 74 3b 0a 20 20 20 e->pLruNext;.
10d29 20 7d 0a 20 20 20 20 69 66 28 20 70 63 61 63 68 }. if( pcach
10d2a 65 31 2e 70 4c 72 75 54 61 69 6c 3d 3d 70 50 61 e1.pLruTail==pPa
10d2b 67 65 20 29 7b 0a 20 20 20 20 20 20 70 63 61 63 ge ){. pcac
10d2c 68 65 31 2e 70 4c 72 75 54 61 69 6c 20 3d 20 70 he1.pLruTail = p
10d2d 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 3b 0a Page->pLruPrev;.
10d2e 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 2d }. pPage-
10d2f 3e 70 4c 72 75 4e 65 78 74 20 3d 20 30 3b 0a 20 >pLruNext = 0;.
10d30 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 pPage->pLruPr
10d31 65 76 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 ev = 0;. pPag
10d32 65 2d 3e 70 43 61 63 68 65 2d 3e 6e 52 65 63 79 e->pCache->nRecy
10d33 63 6c 61 62 6c 65 2d 2d 3b 0a 20 20 7d 0a 7d 0a clable--;. }.}.
10d34 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 ../*.** Remove t
10d35 68 65 20 70 61 67 65 20 73 75 70 70 6c 69 65 64 he page supplied
10d36 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 as an argument
10d37 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 from the hash ta
10d38 62 6c 65 20 0a 2a 2a 20 28 50 43 61 63 68 65 31 ble .** (PCache1
10d39 2e 61 70 48 61 73 68 20 73 74 72 75 63 74 75 72 .apHash structur
10d3a 65 29 20 74 68 61 74 20 69 74 20 69 73 20 63 75 e) that it is cu
10d3b 72 72 65 6e 74 6c 79 20 73 74 6f 72 65 64 20 69 rrently stored i
10d3c 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f n..**.** The glo
10d3d 62 61 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 62 bal mutex must b
10d3e 65 20 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 e held when this
10d3f 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c function is cal
10d40 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 led..*/.static v
10d41 6f 69 64 20 70 63 61 63 68 65 31 52 65 6d 6f 76 oid pcache1Remov
10d42 65 46 72 6f 6d 48 61 73 68 28 50 67 48 64 72 31 eFromHash(PgHdr1
10d43 20 2a 70 50 61 67 65 29 7b 0a 20 20 75 6e 73 69 *pPage){. unsi
10d44 67 6e 65 64 20 69 6e 74 20 68 3b 0a 20 20 50 43 gned int h;. PC
10d45 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20 ache1 *pCache =
10d46 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3b 0a 20 pPage->pCache;.
10d47 20 50 67 48 64 72 31 20 2a 2a 70 70 3b 0a 0a 20 PgHdr1 **pp;..
10d48 20 68 20 3d 20 70 50 61 67 65 2d 3e 69 4b 65 79 h = pPage->iKey
10d49 20 25 20 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 % pCache->nHash
10d4a 3b 0a 20 20 66 6f 72 28 70 70 3d 26 70 43 61 63 ;. for(pp=&pCac
10d4b 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 3b 20 28 he->apHash[h]; (
10d4c 2a 70 70 29 21 3d 70 50 61 67 65 3b 20 70 70 3d *pp)!=pPage; pp=
10d4d 26 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29 3b 0a &(*pp)->pNext);.
10d4e 20 20 2a 70 70 20 3d 20 28 2a 70 70 29 2d 3e 70 *pp = (*pp)->p
10d4f 4e 65 78 74 3b 0a 0a 20 20 70 43 61 63 68 65 2d Next;.. pCache-
10d50 3e 6e 50 61 67 65 2d 2d 3b 0a 7d 0a 0a 2f 2a 0a >nPage--;.}../*.
10d51 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 ** If there are
10d52 63 75 72 72 65 6e 74 6c 79 20 6d 6f 72 65 20 74 currently more t
10d53 68 61 6e 20 70 63 61 63 68 65 2e 6e 4d 61 78 50 han pcache.nMaxP
10d54 61 67 65 20 70 61 67 65 73 20 61 6c 6c 6f 63 61 age pages alloca
10d55 74 65 64 2c 20 74 72 79 0a 2a 2a 20 74 6f 20 72 ted, try.** to r
10d56 65 63 79 63 6c 65 20 70 61 67 65 73 20 74 6f 20 ecycle pages to
10d57 72 65 64 75 63 65 20 74 68 65 20 6e 75 6d 62 65 reduce the numbe
10d58 72 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20 70 r allocated to p
10d59 63 61 63 68 65 2e 6e 4d 61 78 50 61 67 65 2e 0a cache.nMaxPage..
10d5a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 */.static void p
10d5b 63 61 63 68 65 31 45 6e 66 6f 72 63 65 4d 61 78 cache1EnforceMax
10d5c 50 61 67 65 28 76 6f 69 64 29 7b 0a 20 20 61 73 Page(void){. as
10d5d 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
10d5e 74 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65 31 tex_held(pcache1
10d5f 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 77 68 69 .mutex) );. whi
10d60 6c 65 28 20 70 63 61 63 68 65 31 2e 6e 43 75 72 le( pcache1.nCur
10d61 72 65 6e 74 50 61 67 65 3e 70 63 61 63 68 65 31 rentPage>pcache1
10d62 2e 6e 4d 61 78 50 61 67 65 20 26 26 20 70 63 61 .nMaxPage && pca
10d63 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 20 29 7b che1.pLruTail ){
10d64 0a 20 20 20 20 50 67 48 64 72 31 20 2a 70 20 3d . PgHdr1 *p =
10d65 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69 pcache1.pLruTai
10d66 6c 3b 0a 20 20 20 20 70 63 61 63 68 65 31 50 69 l;. pcache1Pi
10d67 6e 50 61 67 65 28 70 29 3b 0a 20 20 20 20 70 63 nPage(p);. pc
10d68 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 ache1RemoveFromH
10d69 61 73 68 28 70 29 3b 0a 20 20 20 20 70 63 61 63 ash(p);. pcac
10d6a 68 65 31 46 72 65 65 50 61 67 65 28 70 29 3b 0a he1FreePage(p);.
10d6b 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 69 73 }.}../*.** Dis
10d6c 63 61 72 64 20 61 6c 6c 20 70 61 67 65 73 20 66 card all pages f
10d6d 72 6f 6d 20 63 61 63 68 65 20 70 43 61 63 68 65 rom cache pCache
10d6e 20 77 69 74 68 20 61 20 70 61 67 65 20 6e 75 6d with a page num
10d6f 62 65 72 20 28 6b 65 79 20 76 61 6c 75 65 29 20 ber (key value)
10d70 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e .** greater than
10d71 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 69 4c 69 or equal to iLi
10d72 6d 69 74 2e 20 41 6e 79 20 70 69 6e 6e 65 64 20 mit. Any pinned
10d73 70 61 67 65 73 20 74 68 61 74 20 6d 65 65 74 20 pages that meet
10d74 74 68 69 73 20 0a 2a 2a 20 63 72 69 74 65 72 69 this .** criteri
10d75 61 20 61 72 65 20 75 6e 70 69 6e 6e 65 64 20 62 a are unpinned b
10d76 65 66 6f 72 65 20 74 68 65 79 20 61 72 65 20 64 efore they are d
10d77 69 73 63 61 72 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 iscarded..**.**
10d78 54 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 The global mutex
10d79 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 must be held wh
10d7a 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e en this function
10d7b 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 is called..*/.s
10d7c 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 tatic void pcach
10d7d 65 31 54 72 75 6e 63 61 74 65 55 6e 73 61 66 65 e1TruncateUnsafe
10d7e 28 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 (. PCache1 *pCa
10d7f 63 68 65 2c 20 0a 20 20 75 6e 73 69 67 6e 65 64 che, . unsigned
10d80 20 69 6e 74 20 69 4c 69 6d 69 74 20 0a 29 7b 0a int iLimit .){.
10d81 20 20 54 45 53 54 4f 4e 4c 59 28 20 75 6e 73 69 TESTONLY( unsi
10d82 67 6e 65 64 20 69 6e 74 20 6e 50 61 67 65 20 3d gned int nPage =
10d83 20 30 3b 20 29 20 20 20 20 20 20 2f 2a 20 55 73 0; ) /* Us
10d84 65 64 20 74 6f 20 61 73 73 65 72 74 20 70 43 61 ed to assert pCa
10d85 63 68 65 2d 3e 6e 50 61 67 65 20 69 73 20 63 6f che->nPage is co
10d86 72 72 65 63 74 20 2a 2f 0a 20 20 75 6e 73 69 67 rrect */. unsig
10d87 6e 65 64 20 69 6e 74 20 68 3b 0a 20 20 61 73 73 ned int h;. ass
10d88 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
10d89 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65 31 2e ex_held(pcache1.
10d8a 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 mutex) );. for(
10d8b 68 3d 30 3b 20 68 3c 70 43 61 63 68 65 2d 3e 6e h=0; h<pCache->n
10d8c 48 61 73 68 3b 20 68 2b 2b 29 7b 0a 20 20 20 20 Hash; h++){.
10d8d 50 67 48 64 72 31 20 2a 2a 70 70 20 3d 20 26 70 PgHdr1 **pp = &p
10d8e 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d Cache->apHash[h]
10d8f 3b 20 0a 20 20 20 20 50 67 48 64 72 31 20 2a 70 ; . PgHdr1 *p
10d90 50 61 67 65 3b 0a 20 20 20 20 77 68 69 6c 65 28 Page;. while(
10d91 20 28 70 50 61 67 65 20 3d 20 2a 70 70 29 21 3d (pPage = *pp)!=
10d92 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 0 ){. if( p
10d93 50 61 67 65 2d 3e 69 4b 65 79 3e 3d 69 4c 69 6d Page->iKey>=iLim
10d94 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 it ){. pC
10d95 61 63 68 65 2d 3e 6e 50 61 67 65 2d 2d 3b 0a 20 ache->nPage--;.
10d96 20 20 20 20 20 20 20 2a 70 70 20 3d 20 70 50 61 *pp = pPa
10d97 67 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 ge->pNext;.
10d98 20 20 20 70 63 61 63 68 65 31 50 69 6e 50 61 67 pcache1PinPag
10d99 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 e(pPage);.
10d9a 20 20 70 63 61 63 68 65 31 46 72 65 65 50 61 67 pcache1FreePag
10d9b 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 e(pPage);.
10d9c 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 }else{. p
10d9d 70 20 3d 20 26 70 50 61 67 65 2d 3e 70 4e 65 78 p = &pPage->pNex
10d9e 74 3b 0a 20 20 20 20 20 20 20 20 54 45 53 54 4f t;. TESTO
10d9f 4e 4c 59 28 20 6e 50 61 67 65 2b 2b 3b 20 29 0a NLY( nPage++; ).
10da0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
10da1 7d 0a 20 20 61 73 73 65 72 74 28 20 70 43 61 63 }. assert( pCac
10da2 68 65 2d 3e 6e 50 61 67 65 3d 3d 6e 50 61 67 65 he->nPage==nPage
10da3 20 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a );.}../********
10da4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10da5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10da6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10da7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10da8 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
10da9 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 * sqlite3_pcache
10daa 20 4d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a Methods *******
10dab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10dac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10dad 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 *******/../*.**
10dae 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f Implementation o
10daf 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 f the sqlite3_pc
10db0 61 63 68 65 2e 78 49 6e 69 74 20 6d 65 74 68 6f ache.xInit metho
10db1 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 d..*/.static int
10db2 20 70 63 61 63 68 65 31 49 6e 69 74 28 76 6f 69 pcache1Init(voi
10db3 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 d *NotUsed){. U
10db4 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
10db5 4e 6f 74 55 73 65 64 29 3b 0a 20 20 61 73 73 65 NotUsed);. asse
10db6 72 74 28 20 70 63 61 63 68 65 31 2e 69 73 49 6e rt( pcache1.isIn
10db7 69 74 3d 3d 30 20 29 3b 0a 20 20 6d 65 6d 73 65 it==0 );. memse
10db8 74 28 26 70 63 61 63 68 65 31 2c 20 30 2c 20 73 t(&pcache1, 0, s
10db9 69 7a 65 6f 66 28 70 63 61 63 68 65 31 29 29 3b izeof(pcache1));
10dba 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c . if( sqlite3Gl
10dbb 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 obalConfig.bCore
10dbc 4d 75 74 65 78 20 29 7b 0a 20 20 20 20 70 63 61 Mutex ){. pca
10dbd 63 68 65 31 2e 6d 75 74 65 78 20 3d 20 73 71 6c che1.mutex = sql
10dbe 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
10dbf 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 (SQLITE_MUTEX_ST
10dc0 41 54 49 43 5f 4c 52 55 29 3b 0a 20 20 7d 0a 20 ATIC_LRU);. }.
10dc1 20 70 63 61 63 68 65 31 2e 69 73 49 6e 69 74 20 pcache1.isInit
10dc2 3d 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 = 1;. return SQ
10dc3 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
10dc4 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * Implementation
10dc5 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f of the sqlite3_
10dc6 70 63 61 63 68 65 2e 78 53 68 75 74 64 6f 77 6e pcache.xShutdown
10dc7 20 6d 65 74 68 6f 64 2e 0a 2a 2a 20 4e 6f 74 65 method..** Note
10dc8 20 74 68 61 74 20 74 68 65 20 73 74 61 74 69 63 that the static
10dc9 20 6d 75 74 65 78 20 61 6c 6c 6f 63 61 74 65 64 mutex allocated
10dca 20 69 6e 20 78 49 6e 69 74 20 64 6f 65 73 20 0a in xInit does .
10dcb 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 ** not need to b
10dcc 65 20 66 72 65 65 64 2e 0a 2a 2f 0a 73 74 61 74 e freed..*/.stat
10dcd 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 53 ic void pcache1S
10dce 68 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f hutdown(void *No
10dcf 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 tUsed){. UNUSED
10dd0 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 _PARAMETER(NotUs
10dd1 65 64 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ed);. assert( p
10dd2 63 61 63 68 65 31 2e 69 73 49 6e 69 74 21 3d 30 cache1.isInit!=0
10dd3 20 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 70 63 );. memset(&pc
10dd4 61 63 68 65 31 2c 20 30 2c 20 73 69 7a 65 6f 66 ache1, 0, sizeof
10dd5 28 70 63 61 63 68 65 31 29 29 3b 0a 7d 0a 0a 2f (pcache1));.}../
10dd6 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 *.** Implementat
10dd7 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 ion of the sqlit
10dd8 65 33 5f 70 63 61 63 68 65 2e 78 43 72 65 61 74 e3_pcache.xCreat
10dd9 65 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 e method..**.**
10dda 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 63 Allocate a new c
10ddb 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ache..*/.static
10ddc 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a sqlite3_pcache *
10ddd 70 63 61 63 68 65 31 43 72 65 61 74 65 28 69 6e pcache1Create(in
10dde 74 20 73 7a 50 61 67 65 2c 20 69 6e 74 20 62 50 t szPage, int bP
10ddf 75 72 67 65 61 62 6c 65 29 7b 0a 20 20 50 43 61 urgeable){. PCa
10de0 63 68 65 31 20 2a 70 43 61 63 68 65 3b 0a 0a 20 che1 *pCache;..
10de1 20 70 43 61 63 68 65 20 3d 20 28 50 43 61 63 68 pCache = (PCach
10de2 65 31 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c e1 *)sqlite3_mal
10de3 6c 6f 63 28 73 69 7a 65 6f 66 28 50 43 61 63 68 loc(sizeof(PCach
10de4 65 31 29 29 3b 0a 20 20 69 66 28 20 70 43 61 63 e1));. if( pCac
10de5 68 65 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 he ){. memset
10de6 28 70 43 61 63 68 65 2c 20 30 2c 20 73 69 7a 65 (pCache, 0, size
10de7 6f 66 28 50 43 61 63 68 65 31 29 29 3b 0a 20 20 of(PCache1));.
10de8 20 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 pCache->szPage
10de9 20 3d 20 73 7a 50 61 67 65 3b 0a 20 20 20 20 70 = szPage;. p
10dea 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c Cache->bPurgeabl
10deb 65 20 3d 20 28 62 50 75 72 67 65 61 62 6c 65 20 e = (bPurgeable
10dec 3f 20 31 20 3a 20 30 29 3b 0a 20 20 20 20 69 66 ? 1 : 0);. if
10ded 28 20 62 50 75 72 67 65 61 62 6c 65 20 29 7b 0a ( bPurgeable ){.
10dee 20 20 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 4d pCache->nM
10def 69 6e 20 3d 20 31 30 3b 0a 20 20 20 20 20 20 70 in = 10;. p
10df0 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 cache1EnterMutex
10df1 28 29 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 ();. pcache
10df2 31 2e 6e 4d 69 6e 50 61 67 65 20 2b 3d 20 70 43 1.nMinPage += pC
10df3 61 63 68 65 2d 3e 6e 4d 69 6e 3b 0a 20 20 20 20 ache->nMin;.
10df4 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 pcache1LeaveMu
10df5 74 65 78 28 29 3b 0a 20 20 20 20 7d 0a 20 20 7d tex();. }. }
10df6 0a 20 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74 . return (sqlit
10df7 65 33 5f 70 63 61 63 68 65 20 2a 29 70 43 61 63 e3_pcache *)pCac
10df8 68 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 he;.}../*.** Imp
10df9 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
10dfa 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 he sqlite3_pcach
10dfb 65 2e 78 43 61 63 68 65 73 69 7a 65 20 6d 65 74 e.xCachesize met
10dfc 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 43 6f 6e 66 hod. .**.** Conf
10dfd 69 67 75 72 65 20 74 68 65 20 63 61 63 68 65 5f igure the cache_
10dfe 73 69 7a 65 20 6c 69 6d 69 74 20 66 6f 72 20 61 size limit for a
10dff 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 cache..*/.stati
10e00 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 43 61 c void pcache1Ca
10e01 63 68 65 73 69 7a 65 28 73 71 6c 69 74 65 33 5f chesize(sqlite3_
10e02 70 63 61 63 68 65 20 2a 70 2c 20 69 6e 74 20 6e pcache *p, int n
10e03 4d 61 78 29 7b 0a 20 20 50 43 61 63 68 65 31 20 Max){. PCache1
10e04 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61 63 68 *pCache = (PCach
10e05 65 31 20 2a 29 70 3b 0a 20 20 69 66 28 20 70 43 e1 *)p;. if( pC
10e06 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 ache->bPurgeable
10e07 20 29 7b 0a 20 20 20 20 70 63 61 63 68 65 31 45 ){. pcache1E
10e08 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 nterMutex();.
10e09 20 70 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 pcache1.nMaxPag
10e0a 65 20 2b 3d 20 28 6e 4d 61 78 20 2d 20 70 43 61 e += (nMax - pCa
10e0b 63 68 65 2d 3e 6e 4d 61 78 29 3b 0a 20 20 20 20 che->nMax);.
10e0c 70 43 61 63 68 65 2d 3e 6e 4d 61 78 20 3d 20 6e pCache->nMax = n
10e0d 4d 61 78 3b 0a 20 20 20 20 70 63 61 63 68 65 31 Max;. pcache1
10e0e 45 6e 66 6f 72 63 65 4d 61 78 50 61 67 65 28 29 EnforceMaxPage()
10e0f 3b 0a 20 20 20 20 70 63 61 63 68 65 31 4c 65 61 ;. pcache1Lea
10e10 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a 7d veMutex();. }.}
10e11 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e ../*.** Implemen
10e12 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 tation of the sq
10e13 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 50 61 lite3_pcache.xPa
10e14 67 65 63 6f 75 6e 74 20 6d 65 74 68 6f 64 2e 20 gecount method.
10e15 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
10e16 63 61 63 68 65 31 50 61 67 65 63 6f 75 6e 74 28 cache1Pagecount(
10e17 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a sqlite3_pcache *
10e18 70 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 70 p){. int n;. p
10e19 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 cache1EnterMutex
10e1a 28 29 3b 0a 20 20 6e 20 3d 20 28 28 50 43 61 63 ();. n = ((PCac
10e1b 68 65 31 20 2a 29 70 29 2d 3e 6e 50 61 67 65 3b he1 *)p)->nPage;
10e1c 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d . pcache1LeaveM
10e1d 75 74 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e utex();. return
10e1e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 n;.}../*.** Imp
10e1f 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
10e20 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 he sqlite3_pcach
10e21 65 2e 78 46 65 74 63 68 20 6d 65 74 68 6f 64 2e e.xFetch method.
10e22 20 0a 2a 2a 0a 2a 2a 20 46 65 74 63 68 20 61 20 .**.** Fetch a
10e23 70 61 67 65 20 62 79 20 6b 65 79 20 76 61 6c 75 page by key valu
10e24 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 65 72 e..**.** Whether
10e25 20 6f 72 20 6e 6f 74 20 61 20 6e 65 77 20 70 61 or not a new pa
10e26 67 65 20 6d 61 79 20 62 65 20 61 6c 6c 6f 63 61 ge may be alloca
10e27 74 65 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 ted by this func
10e28 74 69 6f 6e 20 64 65 70 65 6e 64 73 20 6f 6e 0a tion depends on.
10e29 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 ** the value of
10e2a 74 68 65 20 63 72 65 61 74 65 46 6c 61 67 20 61 the createFlag a
10e2b 72 67 75 6d 65 6e 74 2e 20 20 30 20 6d 65 61 6e rgument. 0 mean
10e2c 73 20 64 6f 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 s do not allocat
10e2d 65 20 61 20 6e 65 77 0a 2a 2a 20 70 61 67 65 2e e a new.** page.
10e2e 20 20 31 20 6d 65 61 6e 73 20 61 6c 6c 6f 63 61 1 means alloca
10e2f 74 65 20 61 20 6e 65 77 20 70 61 67 65 20 69 66 te a new page if
10e30 20 73 70 61 63 65 20 69 73 20 65 61 73 69 6c 79 space is easily
10e31 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 32 20 0a available. 2 .
10e32 2a 2a 20 6d 65 61 6e 73 20 74 6f 20 74 72 79 20 ** means to try
10e33 72 65 61 6c 6c 79 20 68 61 72 64 20 74 6f 20 61 really hard to a
10e34 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 61 llocate a new pa
10e35 67 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 20 ge..**.** For a
10e36 6e 6f 6e 2d 70 75 72 67 65 61 62 6c 65 20 63 61 non-purgeable ca
10e37 63 68 65 20 28 61 20 63 61 63 68 65 20 75 73 65 che (a cache use
10e38 64 20 61 73 20 74 68 65 20 73 74 6f 72 61 67 65 d as the storage
10e39 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 for an in-memor
10e3a 79 0a 2a 2a 20 64 61 74 61 62 61 73 65 29 20 74 y.** database) t
10e3b 68 65 72 65 20 69 73 20 72 65 61 6c 6c 79 20 6e here is really n
10e3c 6f 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 o difference bet
10e3d 77 65 65 6e 20 63 72 65 61 74 65 46 6c 61 67 20 ween createFlag
10e3e 31 20 61 6e 64 20 32 2e 20 20 53 6f 0a 2a 2a 20 1 and 2. So.**
10e3f 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 the calling func
10e40 74 69 6f 6e 20 28 70 63 61 63 68 65 2e 63 29 20 tion (pcache.c)
10e41 77 69 6c 6c 20 6e 65 76 65 72 20 68 61 76 65 20 will never have
10e42 61 20 63 72 65 61 74 65 46 6c 61 67 20 6f 66 20 a createFlag of
10e43 31 20 6f 6e 0a 2a 2a 20 61 20 6e 6f 6e 2d 70 75 1 on.** a non-pu
10e44 72 67 61 62 6c 65 20 63 61 63 68 65 2e 0a 2a 2a rgable cache..**
10e45 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 74 68 .** There are th
10e46 72 65 65 20 64 69 66 66 65 72 65 6e 74 20 61 70 ree different ap
10e47 70 72 6f 61 63 68 65 73 20 74 6f 20 6f 62 74 61 proaches to obta
10e48 69 6e 69 6e 67 20 73 70 61 63 65 20 66 6f 72 20 ining space for
10e49 61 20 70 61 67 65 2c 0a 2a 2a 20 64 65 70 65 6e a page,.** depen
10e4a 64 69 6e 67 20 6f 6e 20 74 68 65 20 76 61 6c 75 ding on the valu
10e4b 65 20 6f 66 20 70 61 72 61 6d 65 74 65 72 20 63 e of parameter c
10e4c 72 65 61 74 65 46 6c 61 67 20 28 77 68 69 63 68 reateFlag (which
10e4d 20 6d 61 79 20 62 65 20 30 2c 20 31 20 6f 72 20 may be 0, 1 or
10e4e 32 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 52 2)..**.** 1. R
10e4f 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 egardless of the
10e50 20 76 61 6c 75 65 20 6f 66 20 63 72 65 61 74 65 value of create
10e51 46 6c 61 67 2c 20 74 68 65 20 63 61 63 68 65 20 Flag, the cache
10e52 69 73 20 73 65 61 72 63 68 65 64 20 66 6f 72 20 is searched for
10e53 61 20 0a 2a 2a 20 20 20 20 20 20 63 6f 70 79 20 a .** copy
10e54 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 of the requested
10e55 20 70 61 67 65 2e 20 49 66 20 6f 6e 65 20 69 73 page. If one is
10e56 20 66 6f 75 6e 64 2c 20 69 74 20 69 73 20 72 65 found, it is re
10e57 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 turned..**.**
10e58 32 2e 20 49 66 20 63 72 65 61 74 65 46 6c 61 67 2. If createFlag
10e59 3d 3d 30 20 61 6e 64 20 74 68 65 20 70 61 67 65 ==0 and the page
10e5a 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 is not already
10e5b 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 4e 55 in the cache, NU
10e5c 4c 4c 20 69 73 0a 2a 2a 20 20 20 20 20 20 72 65 LL is.** re
10e5d 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 turned..**.**
10e5e 33 2e 20 49 66 20 63 72 65 61 74 65 46 6c 61 67 3. If createFlag
10e5f 20 69 73 20 31 2c 20 61 6e 64 20 74 68 65 20 70 is 1, and the p
10e60 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 age is not alrea
10e61 64 79 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c dy in the cache,
10e62 0a 2a 2a 20 20 20 20 20 20 61 6e 64 20 69 66 20 .** and if
10e63 65 69 74 68 65 72 20 6f 66 20 74 68 65 20 66 6f either of the fo
10e64 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 72 75 65 llowing are true
10e65 2c 20 72 65 74 75 72 6e 20 4e 55 4c 4c 3a 0a 2a , return NULL:.*
10e66 2a 0a 2a 2a 20 20 20 20 20 20 20 28 61 29 20 74 *.** (a) t
10e67 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 he number of pag
10e68 65 73 20 70 69 6e 6e 65 64 20 62 79 20 74 68 65 es pinned by the
10e69 20 63 61 63 68 65 20 69 73 20 67 72 65 61 74 65 cache is greate
10e6a 72 20 74 68 61 6e 0a 2a 2a 20 20 20 20 20 20 20 r than.**
10e6b 20 20 20 20 50 43 61 63 68 65 31 2e 6e 4d 61 78 PCache1.nMax
10e6c 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 28 62 , or.** (b
10e6d 29 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 ) the number of
10e6e 70 61 67 65 73 20 70 69 6e 6e 65 64 20 62 79 20 pages pinned by
10e6f 74 68 65 20 63 61 63 68 65 20 69 73 20 67 72 65 the cache is gre
10e70 61 74 65 72 20 74 68 61 6e 0a 2a 2a 20 20 20 20 ater than.**
10e71 20 20 20 20 20 20 20 74 68 65 20 73 75 6d 20 6f the sum o
10e72 66 20 6e 4d 61 78 20 66 6f 72 20 61 6c 6c 20 70 f nMax for all p
10e73 75 72 67 65 61 62 6c 65 20 63 61 63 68 65 73 2c urgeable caches,
10e74 20 6c 65 73 73 20 74 68 65 20 73 75 6d 20 6f 66 less the sum of
10e75 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6e .** n
10e76 4d 69 6e 20 66 6f 72 20 61 6c 6c 20 6f 74 68 65 Min for all othe
10e77 72 20 70 75 72 67 65 61 62 6c 65 20 63 61 63 68 r purgeable cach
10e78 65 73 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 34 2e 20 es. .**.** 4.
10e79 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 66 If none of the f
10e7a 69 72 73 74 20 74 68 72 65 65 20 63 6f 6e 64 69 irst three condi
10e7b 74 69 6f 6e 73 20 61 70 70 6c 79 20 61 6e 64 20 tions apply and
10e7c 74 68 65 20 63 61 63 68 65 20 69 73 20 6d 61 72 the cache is mar
10e7d 6b 65 64 0a 2a 2a 20 20 20 20 20 20 61 73 20 70 ked.** as p
10e7e 75 72 67 65 61 62 6c 65 2c 20 61 6e 64 20 69 66 urgeable, and if
10e7f 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c one of the foll
10e80 6f 77 69 6e 67 20 69 73 20 74 72 75 65 3a 0a 2a owing is true:.*
10e81 2a 0a 2a 2a 20 20 20 20 20 20 20 28 61 29 20 54 *.** (a) T
10e82 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 he number of pag
10e83 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 es allocated for
10e84 20 74 68 65 20 63 61 63 68 65 20 69 73 20 61 6c the cache is al
10e85 72 65 61 64 79 20 0a 2a 2a 20 20 20 20 20 20 20 ready .**
10e86 20 20 20 20 50 43 61 63 68 65 31 2e 6e 4d 61 78 PCache1.nMax
10e87 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 , or.**.**
10e88 20 28 62 29 20 54 68 65 20 6e 75 6d 62 65 72 20 (b) The number
10e89 6f 66 20 70 61 67 65 73 20 61 6c 6c 6f 63 61 74 of pages allocat
10e8a 65 64 20 66 6f 72 20 61 6c 6c 20 70 75 72 67 65 ed for all purge
10e8b 61 62 6c 65 20 63 61 63 68 65 73 20 69 73 0a 2a able caches is.*
10e8c 2a 20 20 20 20 20 20 20 20 20 20 20 61 6c 72 65 * alre
10e8d 61 64 79 20 65 71 75 61 6c 20 74 6f 20 6f 72 20 ady equal to or
10e8e 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 greater than the
10e8f 20 73 75 6d 20 6f 66 20 6e 4d 61 78 20 66 6f 72 sum of nMax for
10e90 20 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 all.**
10e91 20 20 70 75 72 67 65 61 62 6c 65 20 63 61 63 68 purgeable cach
10e92 65 73 2c 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 74 es,.**.** t
10e93 68 65 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 72 hen attempt to r
10e94 65 63 79 63 6c 65 20 61 20 70 61 67 65 20 66 72 ecycle a page fr
10e95 6f 6d 20 74 68 65 20 4c 52 55 20 6c 69 73 74 2e om the LRU list.
10e96 20 49 66 20 69 74 20 69 73 20 74 68 65 20 72 69 If it is the ri
10e97 67 68 74 0a 2a 2a 20 20 20 20 20 20 73 69 7a 65 ght.** size
10e98 2c 20 72 65 74 75 72 6e 20 74 68 65 20 72 65 63 , return the rec
10e99 79 63 6c 65 64 20 62 75 66 66 65 72 2e 20 4f 74 ycled buffer. Ot
10e9a 68 65 72 77 69 73 65 2c 20 66 72 65 65 20 74 68 herwise, free th
10e9b 65 20 62 75 66 66 65 72 20 61 6e 64 0a 2a 2a 20 e buffer and.**
10e9c 20 20 20 20 20 70 72 6f 63 65 65 64 20 74 6f 20 proceed to
10e9d 73 74 65 70 20 35 2e 20 0a 2a 2a 0a 2a 2a 20 20 step 5. .**.**
10e9e 20 35 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 61 5. Otherwise, a
10e9f 6c 6c 6f 63 61 74 65 20 61 6e 64 20 72 65 74 75 llocate and retu
10ea0 72 6e 20 61 20 6e 65 77 20 70 61 67 65 20 62 75 rn a new page bu
10ea1 66 66 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ffer..*/.static
10ea2 76 6f 69 64 20 2a 70 63 61 63 68 65 31 46 65 74 void *pcache1Fet
10ea3 63 68 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 ch(sqlite3_pcach
10ea4 65 20 2a 70 2c 20 75 6e 73 69 67 6e 65 64 20 69 e *p, unsigned i
10ea5 6e 74 20 69 4b 65 79 2c 20 69 6e 74 20 63 72 65 nt iKey, int cre
10ea6 61 74 65 46 6c 61 67 29 7b 0a 20 20 75 6e 73 69 ateFlag){. unsi
10ea7 67 6e 65 64 20 69 6e 74 20 6e 50 69 6e 6e 65 64 gned int nPinned
10ea8 3b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 ;. PCache1 *pCa
10ea9 63 68 65 20 3d 20 28 50 43 61 63 68 65 31 20 2a che = (PCache1 *
10eaa 29 70 3b 0a 20 20 50 67 48 64 72 31 20 2a 70 50 )p;. PgHdr1 *pP
10eab 61 67 65 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 age = 0;.. asse
10eac 72 74 28 20 70 43 61 63 68 65 2d 3e 62 50 75 72 rt( pCache->bPur
10ead 67 65 61 62 6c 65 20 7c 7c 20 63 72 65 61 74 65 geable || create
10eae 46 6c 61 67 21 3d 31 20 29 3b 0a 20 20 70 63 61 Flag!=1 );. pca
10eaf 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 che1EnterMutex()
10eb0 3b 0a 20 20 69 66 28 20 63 72 65 61 74 65 46 6c ;. if( createFl
10eb1 61 67 3d 3d 31 20 29 20 73 71 6c 69 74 65 33 42 ag==1 ) sqlite3B
10eb2 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 eginBenignMalloc
10eb3 28 29 3b 0a 0a 20 20 2f 2a 20 53 65 61 72 63 68 ();.. /* Search
10eb4 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 the hash table
10eb5 66 6f 72 20 61 6e 20 65 78 69 73 74 69 6e 67 20 for an existing
10eb6 65 6e 74 72 79 2e 20 2a 2f 0a 20 20 69 66 28 20 entry. */. if(
10eb7 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3e 30 20 pCache->nHash>0
10eb8 29 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 ){. unsigned
10eb9 69 6e 74 20 68 20 3d 20 69 4b 65 79 20 25 20 70 int h = iKey % p
10eba 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20 Cache->nHash;.
10ebb 20 20 66 6f 72 28 70 50 61 67 65 3d 70 43 61 63 for(pPage=pCac
10ebc 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 3b 20 70 he->apHash[h]; p
10ebd 50 61 67 65 26 26 70 50 61 67 65 2d 3e 69 4b 65 Page&&pPage->iKe
10ebe 79 21 3d 69 4b 65 79 3b 20 70 50 61 67 65 3d 70 y!=iKey; pPage=p
10ebf 50 61 67 65 2d 3e 70 4e 65 78 74 29 3b 0a 20 20 Page->pNext);.
10ec0 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 65 20 7c }.. if( pPage |
10ec1 7c 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 30 20 | createFlag==0
10ec2 29 7b 0a 20 20 20 20 70 63 61 63 68 65 31 50 69 ){. pcache1Pi
10ec3 6e 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 nPage(pPage);.
10ec4 20 20 67 6f 74 6f 20 66 65 74 63 68 5f 6f 75 74 goto fetch_out
10ec5 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 ;. }.. /* Step
10ec6 20 33 20 6f 66 20 68 65 61 64 65 72 20 63 6f 6d 3 of header com
10ec7 6d 65 6e 74 2e 20 2a 2f 0a 20 20 6e 50 69 6e 6e ment. */. nPinn
10ec8 65 64 20 3d 20 70 43 61 63 68 65 2d 3e 6e 50 61 ed = pCache->nPa
10ec9 67 65 20 2d 20 70 43 61 63 68 65 2d 3e 6e 52 65 ge - pCache->nRe
10eca 63 79 63 6c 61 62 6c 65 3b 0a 20 20 69 66 28 20 cyclable;. if(
10ecb 63 72 65 61 74 65 46 6c 61 67 3d 3d 31 20 26 26 createFlag==1 &&
10ecc 20 28 0a 20 20 20 20 20 20 20 20 6e 50 69 6e 6e (. nPinn
10ecd 65 64 3e 3d 28 70 63 61 63 68 65 31 2e 6e 4d 61 ed>=(pcache1.nMa
10ece 78 50 61 67 65 2b 70 43 61 63 68 65 2d 3e 6e 4d xPage+pCache->nM
10ecf 69 6e 2d 70 63 61 63 68 65 31 2e 6e 4d 69 6e 50 in-pcache1.nMinP
10ed0 61 67 65 29 0a 20 20 20 20 20 7c 7c 20 6e 50 69 age). || nPi
10ed1 6e 6e 65 64 3e 3d 28 70 43 61 63 68 65 2d 3e 6e nned>=(pCache->n
10ed2 4d 61 78 20 2a 20 39 20 2f 20 31 30 29 0a 20 20 Max * 9 / 10).
10ed3 29 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66 65 74 )){. goto fet
10ed4 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 69 ch_out;. }.. i
10ed5 66 28 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 f( pCache->nPage
10ed6 3e 3d 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 20 >=pCache->nHash
10ed7 26 26 20 70 63 61 63 68 65 31 52 65 73 69 7a 65 && pcache1Resize
10ed8 48 61 73 68 28 70 43 61 63 68 65 29 20 29 7b 0a Hash(pCache) ){.
10ed9 20 20 20 20 67 6f 74 6f 20 66 65 74 63 68 5f 6f goto fetch_o
10eda 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 ut;. }.. /* St
10edb 65 70 20 34 2e 20 54 72 79 20 74 6f 20 72 65 63 ep 4. Try to rec
10edc 79 63 6c 65 20 61 20 70 61 67 65 20 62 75 66 66 ycle a page buff
10edd 65 72 20 69 66 20 61 70 70 72 6f 70 72 69 61 74 er if appropriat
10ede 65 2e 20 2a 2f 0a 20 20 69 66 28 20 70 43 61 63 e. */. if( pCac
10edf 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 26 he->bPurgeable &
10ee0 26 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 & pcache1.pLruTa
10ee1 69 6c 20 26 26 20 28 0a 20 20 20 20 20 28 70 43 il && (. (pC
10ee2 61 63 68 65 2d 3e 6e 50 61 67 65 2b 31 3e 3d 70 ache->nPage+1>=p
10ee3 43 61 63 68 65 2d 3e 6e 4d 61 78 29 20 7c 7c 20 Cache->nMax) ||
10ee4 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 pcache1.nCurrent
10ee5 50 61 67 65 3e 3d 70 63 61 63 68 65 31 2e 6e 4d Page>=pcache1.nM
10ee6 61 78 50 61 67 65 0a 20 20 29 29 7b 0a 20 20 20 axPage. )){.
10ee7 20 70 50 61 67 65 20 3d 20 70 63 61 63 68 65 31 pPage = pcache1
10ee8 2e 70 4c 72 75 54 61 69 6c 3b 0a 20 20 20 20 70 .pLruTail;. p
10ee9 63 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d cache1RemoveFrom
10eea 48 61 73 68 28 70 50 61 67 65 29 3b 0a 20 20 20 Hash(pPage);.
10eeb 20 70 63 61 63 68 65 31 50 69 6e 50 61 67 65 28 pcache1PinPage(
10eec 70 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 pPage);. if(
10eed 70 50 61 67 65 2d 3e 70 43 61 63 68 65 2d 3e 73 pPage->pCache->s
10eee 7a 50 61 67 65 21 3d 70 43 61 63 68 65 2d 3e 73 zPage!=pCache->s
10eef 7a 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 70 zPage ){. p
10ef0 63 61 63 68 65 31 46 72 65 65 50 61 67 65 28 70 cache1FreePage(p
10ef1 50 61 67 65 29 3b 0a 20 20 20 20 20 20 70 50 61 Page);. pPa
10ef2 67 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 ge = 0;. }els
10ef3 65 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 e{. pcache1
10ef4 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 20 2d 3d .nCurrentPage -=
10ef5 20 28 70 50 61 67 65 2d 3e 70 43 61 63 68 65 2d (pPage->pCache-
10ef6 3e 62 50 75 72 67 65 61 62 6c 65 20 2d 20 70 43 >bPurgeable - pC
10ef7 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 ache->bPurgeable
10ef8 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 );. }. }..
10ef9 2f 2a 20 53 74 65 70 20 35 2e 20 49 66 20 61 20 /* Step 5. If a
10efa 75 73 61 62 6c 65 20 70 61 67 65 20 62 75 66 66 usable page buff
10efb 65 72 20 68 61 73 20 73 74 69 6c 6c 20 6e 6f 74 er has still not
10efc 20 62 65 65 6e 20 66 6f 75 6e 64 2c 20 0a 20 20 been found, .
10efd 2a 2a 20 61 74 74 65 6d 70 74 20 74 6f 20 61 6c ** attempt to al
10efe 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 6f 6e 65 locate a new one
10eff 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 . . */. if( !p
10f00 50 61 67 65 20 29 7b 0a 20 20 20 20 70 50 61 67 Page ){. pPag
10f01 65 20 3d 20 70 63 61 63 68 65 31 41 6c 6c 6f 63 e = pcache1Alloc
10f02 50 61 67 65 28 70 43 61 63 68 65 29 3b 0a 20 20 Page(pCache);.
10f03 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 65 20 29 }.. if( pPage )
10f04 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 {. unsigned i
10f05 6e 74 20 68 20 3d 20 69 4b 65 79 20 25 20 70 43 nt h = iKey % pC
10f06 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20 20 ache->nHash;.
10f07 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 2b 2b pCache->nPage++
10f08 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 4b 65 ;. pPage->iKe
10f09 79 20 3d 20 69 4b 65 79 3b 0a 20 20 20 20 70 50 y = iKey;. pP
10f0a 61 67 65 2d 3e 70 4e 65 78 74 20 3d 20 70 43 61 age->pNext = pCa
10f0b 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 3b 0a che->apHash[h];.
10f0c 20 20 20 20 70 50 61 67 65 2d 3e 70 43 61 63 68 pPage->pCach
10f0d 65 20 3d 20 70 43 61 63 68 65 3b 0a 20 20 20 20 e = pCache;.
10f0e 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 20 pPage->pLruPrev
10f0f 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e = 0;. pPage->
10f10 70 4c 72 75 4e 65 78 74 20 3d 20 30 3b 0a 20 20 pLruNext = 0;.
10f11 20 20 2a 28 76 6f 69 64 20 2a 2a 29 28 50 47 48 *(void **)(PGH
10f12 44 52 31 5f 54 4f 5f 50 41 47 45 28 70 50 61 67 DR1_TO_PAGE(pPag
10f13 65 29 29 20 3d 20 30 3b 0a 20 20 20 20 70 43 61 e)) = 0;. pCa
10f14 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 20 3d che->apHash[h] =
10f15 20 70 50 61 67 65 3b 0a 20 20 7d 0a 0a 66 65 74 pPage;. }..fet
10f16 63 68 5f 6f 75 74 3a 0a 20 20 69 66 28 20 70 50 ch_out:. if( pP
10f17 61 67 65 20 26 26 20 69 4b 65 79 3e 70 43 61 63 age && iKey>pCac
10f18 68 65 2d 3e 69 4d 61 78 4b 65 79 20 29 7b 0a 20 he->iMaxKey ){.
10f19 20 20 20 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b pCache->iMaxK
10f1a 65 79 20 3d 20 69 4b 65 79 3b 0a 20 20 7d 0a 20 ey = iKey;. }.
10f1b 20 69 66 28 20 63 72 65 61 74 65 46 6c 61 67 3d if( createFlag=
10f1c 3d 31 20 29 20 73 71 6c 69 74 65 33 45 6e 64 42 =1 ) sqlite3EndB
10f1d 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 enignMalloc();.
10f1e 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 pcache1LeaveMut
10f1f 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 ex();. return (
10f20 70 50 61 67 65 20 3f 20 50 47 48 44 52 31 5f 54 pPage ? PGHDR1_T
10f21 4f 5f 50 41 47 45 28 70 50 61 67 65 29 20 3a 20 O_PAGE(pPage) :
10f22 30 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d 0);.}.../*.** Im
10f23 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
10f24 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 the sqlite3_pcac
10f25 68 65 2e 78 55 6e 70 69 6e 20 6d 65 74 68 6f 64 he.xUnpin method
10f26 2e 0a 2a 2a 0a 2a 2a 20 4d 61 72 6b 20 61 20 70 ..**.** Mark a p
10f27 61 67 65 20 61 73 20 75 6e 70 69 6e 6e 65 64 20 age as unpinned
10f28 28 65 6c 69 67 69 62 6c 65 20 66 6f 72 20 61 73 (eligible for as
10f29 79 6e 63 68 72 6f 6e 6f 75 73 20 72 65 63 79 63 ynchronous recyc
10f2a 6c 69 6e 67 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 ling)..*/.static
10f2b 20 76 6f 69 64 20 70 63 61 63 68 65 31 55 6e 70 void pcache1Unp
10f2c 69 6e 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 in(sqlite3_pcach
10f2d 65 20 2a 70 2c 20 76 6f 69 64 20 2a 70 50 67 2c e *p, void *pPg,
10f2e 20 69 6e 74 20 72 65 75 73 65 55 6e 6c 69 6b 65 int reuseUnlike
10f2f 6c 79 29 7b 0a 20 20 50 43 61 63 68 65 31 20 2a ly){. PCache1 *
10f30 70 43 61 63 68 65 20 3d 20 28 50 43 61 63 68 65 pCache = (PCache
10f31 31 20 2a 29 70 3b 0a 20 20 50 67 48 64 72 31 20 1 *)p;. PgHdr1
10f32 2a 70 50 61 67 65 20 3d 20 50 41 47 45 5f 54 4f *pPage = PAGE_TO
10f33 5f 50 47 48 44 52 31 28 70 43 61 63 68 65 2c 20 _PGHDR1(pCache,
10f34 70 50 67 29 3b 0a 20 0a 20 20 61 73 73 65 72 74 pPg);. . assert
10f35 28 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3d ( pPage->pCache=
10f36 3d 70 43 61 63 68 65 20 29 3b 0a 20 20 70 63 61 =pCache );. pca
10f37 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 che1EnterMutex()
10f38 3b 0a 0a 20 20 2f 2a 20 49 74 20 69 73 20 61 6e ;.. /* It is an
10f39 20 65 72 72 6f 72 20 74 6f 20 63 61 6c 6c 20 74 error to call t
10f3a 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 66 20 his function if
10f3b 74 68 65 20 70 61 67 65 20 69 73 20 61 6c 72 65 the page is alre
10f3c 61 64 79 20 0a 20 20 2a 2a 20 70 61 72 74 20 6f ady . ** part o
10f3d 66 20 74 68 65 20 67 6c 6f 62 61 6c 20 4c 52 55 f the global LRU
10f3e 20 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 61 73 list.. */. as
10f3f 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 4c 72 sert( pPage->pLr
10f40 75 50 72 65 76 3d 3d 30 20 26 26 20 70 50 61 67 uPrev==0 && pPag
10f41 65 2d 3e 70 4c 72 75 4e 65 78 74 3d 3d 30 20 29 e->pLruNext==0 )
10f42 3b 0a 20 20 61 73 73 65 72 74 28 20 70 63 61 63 ;. assert( pcac
10f43 68 65 31 2e 70 4c 72 75 48 65 61 64 21 3d 70 50 he1.pLruHead!=pP
10f44 61 67 65 20 26 26 20 70 63 61 63 68 65 31 2e 70 age && pcache1.p
10f45 4c 72 75 54 61 69 6c 21 3d 70 50 61 67 65 20 29 LruTail!=pPage )
10f46 3b 0a 0a 20 20 69 66 28 20 72 65 75 73 65 55 6e ;.. if( reuseUn
10f47 6c 69 6b 65 6c 79 20 7c 7c 20 70 63 61 63 68 65 likely || pcache
10f48 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 3e 70 1.nCurrentPage>p
10f49 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 20 cache1.nMaxPage
10f4a 29 7b 0a 20 20 20 20 70 63 61 63 68 65 31 52 65 ){. pcache1Re
10f4b 6d 6f 76 65 46 72 6f 6d 48 61 73 68 28 70 50 61 moveFromHash(pPa
10f4c 67 65 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 ge);. pcache1
10f4d 46 72 65 65 50 61 67 65 28 70 50 61 67 65 29 3b FreePage(pPage);
10f4e 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
10f4f 20 41 64 64 20 74 68 65 20 70 61 67 65 20 74 6f Add the page to
10f50 20 74 68 65 20 67 6c 6f 62 61 6c 20 4c 52 55 20 the global LRU
10f51 6c 69 73 74 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20 list. Normally,
10f52 74 68 65 20 70 61 67 65 20 69 73 20 61 64 64 65 the page is adde
10f53 64 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 d to. ** the
10f54 68 65 61 64 20 6f 66 20 74 68 65 20 6c 69 73 74 head of the list
10f55 20 28 6c 61 73 74 20 70 61 67 65 20 74 6f 20 62 (last page to b
10f56 65 20 72 65 63 79 63 6c 65 64 29 2e 20 48 6f 77 e recycled). How
10f57 65 76 65 72 2c 20 69 66 20 74 68 65 20 0a 20 20 ever, if the .
10f58 20 20 2a 2a 20 72 65 75 73 65 55 6e 6c 69 6b 65 ** reuseUnlike
10f59 6c 79 20 66 6c 61 67 20 70 61 73 73 65 64 20 74 ly flag passed t
10f5a 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 o this function
10f5b 69 73 20 74 72 75 65 2c 20 74 68 65 20 70 61 67 is true, the pag
10f5c 65 20 69 73 20 61 64 64 65 64 0a 20 20 20 20 2a e is added. *
10f5d 2a 20 74 6f 20 74 68 65 20 74 61 69 6c 20 6f 66 * to the tail of
10f5e 20 74 68 65 20 6c 69 73 74 20 28 66 69 72 73 74 the list (first
10f5f 20 70 61 67 65 20 74 6f 20 62 65 20 72 65 63 79 page to be recy
10f60 63 6c 65 64 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 cled).. */.
10f61 20 20 69 66 28 20 70 63 61 63 68 65 31 2e 70 4c if( pcache1.pL
10f62 72 75 48 65 61 64 20 29 7b 0a 20 20 20 20 20 20 ruHead ){.
10f63 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 pcache1.pLruHead
10f64 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20 70 50 61 ->pLruPrev = pPa
10f65 67 65 3b 0a 20 20 20 20 20 20 70 50 61 67 65 2d ge;. pPage-
10f66 3e 70 4c 72 75 4e 65 78 74 20 3d 20 70 63 61 63 >pLruNext = pcac
10f67 68 65 31 2e 70 4c 72 75 48 65 61 64 3b 0a 20 20 he1.pLruHead;.
10f68 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72 75 pcache1.pLru
10f69 48 65 61 64 20 3d 20 70 50 61 67 65 3b 0a 20 20 Head = pPage;.
10f6a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 }else{. p
10f6b 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 20 cache1.pLruTail
10f6c 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 20 20 70 = pPage;. p
10f6d 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 20 cache1.pLruHead
10f6e 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 7d 0a 20 = pPage;. }.
10f6f 20 20 20 70 43 61 63 68 65 2d 3e 6e 52 65 63 79 pCache->nRecy
10f70 63 6c 61 62 6c 65 2b 2b 3b 0a 20 20 7d 0a 0a 20 clable++;. }..
10f71 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 pcache1LeaveMut
10f72 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 ex();.}../*.** I
10f73 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
10f74 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 the sqlite3_pca
10f75 63 68 65 2e 78 52 65 6b 65 79 20 6d 65 74 68 6f che.xRekey metho
10f76 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f d. .*/.static vo
10f77 69 64 20 70 63 61 63 68 65 31 52 65 6b 65 79 28 id pcache1Rekey(
10f78 0a 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 . sqlite3_pcach
10f79 65 20 2a 70 2c 0a 20 20 76 6f 69 64 20 2a 70 50 e *p,. void *pP
10f7a 67 2c 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e g,. unsigned in
10f7b 74 20 69 4f 6c 64 2c 0a 20 20 75 6e 73 69 67 6e t iOld,. unsign
10f7c 65 64 20 69 6e 74 20 69 4e 65 77 0a 29 7b 0a 20 ed int iNew.){.
10f7d 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 PCache1 *pCache
10f7e 20 3d 20 28 50 43 61 63 68 65 31 20 2a 29 70 3b = (PCache1 *)p;
10f7f 0a 20 20 50 67 48 64 72 31 20 2a 70 50 61 67 65 . PgHdr1 *pPage
10f80 20 3d 20 50 41 47 45 5f 54 4f 5f 50 47 48 44 52 = PAGE_TO_PGHDR
10f81 31 28 70 43 61 63 68 65 2c 20 70 50 67 29 3b 0a 1(pCache, pPg);.
10f82 20 20 50 67 48 64 72 31 20 2a 2a 70 70 3b 0a 20 PgHdr1 **pp;.
10f83 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 3b unsigned int h;
10f84 20 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 . assert( pPag
10f85 65 2d 3e 69 4b 65 79 3d 3d 69 4f 6c 64 20 29 3b e->iKey==iOld );
10f86 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
10f87 2d 3e 70 43 61 63 68 65 3d 3d 70 43 61 63 68 65 ->pCache==pCache
10f88 20 29 3b 0a 0a 20 20 70 63 61 63 68 65 31 45 6e );.. pcache1En
10f89 74 65 72 4d 75 74 65 78 28 29 3b 0a 0a 20 20 68 terMutex();.. h
10f8a 20 3d 20 69 4f 6c 64 25 70 43 61 63 68 65 2d 3e = iOld%pCache->
10f8b 6e 48 61 73 68 3b 0a 20 20 70 70 20 3d 20 26 70 nHash;. pp = &p
10f8c 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d Cache->apHash[h]
10f8d 3b 0a 20 20 77 68 69 6c 65 28 20 28 2a 70 70 29 ;. while( (*pp)
10f8e 21 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 20 70 !=pPage ){. p
10f8f 70 20 3d 20 26 28 2a 70 70 29 2d 3e 70 4e 65 78 p = &(*pp)->pNex
10f90 74 3b 0a 20 20 7d 0a 20 20 2a 70 70 20 3d 20 70 t;. }. *pp = p
10f91 50 61 67 65 2d 3e 70 4e 65 78 74 3b 0a 0a 20 20 Page->pNext;..
10f92 68 20 3d 20 69 4e 65 77 25 70 43 61 63 68 65 2d h = iNew%pCache-
10f93 3e 6e 48 61 73 68 3b 0a 20 20 70 50 61 67 65 2d >nHash;. pPage-
10f94 3e 69 4b 65 79 20 3d 20 69 4e 65 77 3b 0a 20 20 >iKey = iNew;.
10f95 70 50 61 67 65 2d 3e 70 4e 65 78 74 20 3d 20 70 pPage->pNext = p
10f96 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d Cache->apHash[h]
10f97 3b 0a 20 20 70 43 61 63 68 65 2d 3e 61 70 48 61 ;. pCache->apHa
10f98 73 68 5b 68 5d 20 3d 20 70 50 61 67 65 3b 0a 0a sh[h] = pPage;..
10f99 20 20 2f 2a 20 54 68 65 20 78 52 65 6b 65 79 28 /* The xRekey(
10f9a 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 6f ) interface is o
10f9b 6e 6c 79 20 75 73 65 64 20 74 6f 20 6d 6f 76 65 nly used to move
10f9c 20 70 61 67 65 73 20 65 61 72 6c 69 65 72 20 69 pages earlier i
10f9d 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 n the. ** datab
10f9e 61 73 65 20 66 69 6c 65 20 28 69 6e 20 6f 72 64 ase file (in ord
10f9f 65 72 20 74 6f 20 6d 6f 76 65 20 61 6c 6c 20 66 er to move all f
10fa0 72 65 65 20 70 61 67 65 73 20 74 6f 20 74 68 65 ree pages to the
10fa1 20 65 6e 64 20 6f 66 20 74 68 65 0a 20 20 2a 2a end of the. **
10fa2 20 66 69 6c 65 20 77 68 65 72 65 20 74 68 65 79 file where they
10fa3 20 63 61 6e 20 62 65 20 74 72 75 6e 63 61 74 65 can be truncate
10fa4 64 20 6f 66 66 2e 29 20 20 48 65 6e 63 65 2c 20 d off.) Hence,
10fa5 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 it is not possib
10fa6 6c 65 0a 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 le. ** for the
10fa7 6e 65 77 20 70 61 67 65 20 6e 75 6d 62 65 72 20 new page number
10fa8 74 6f 20 62 65 20 67 72 65 61 74 65 72 20 74 68 to be greater th
10fa9 61 6e 20 74 68 65 20 6c 61 72 67 65 73 74 20 70 an the largest p
10faa 72 65 76 69 6f 75 73 6c 79 0a 20 20 2a 2a 20 66 reviously. ** f
10fab 65 74 63 68 65 64 20 70 61 67 65 2e 20 20 42 75 etched page. Bu
10fac 74 20 77 65 20 72 65 74 61 69 6e 20 74 68 65 20 t we retain the
10fad 66 6f 6c 6c 6f 77 69 6e 67 20 74 65 73 74 20 69 following test i
10fae 6e 20 63 61 73 65 20 78 52 65 6b 65 79 28 29 0a n case xRekey().
10faf 20 20 2a 2a 20 62 65 67 69 6e 73 20 74 6f 20 62 ** begins to b
10fb0 65 20 75 73 65 64 20 69 6e 20 64 69 66 66 65 72 e used in differ
10fb1 65 6e 74 20 77 61 79 73 20 69 6e 20 74 68 65 20 ent ways in the
10fb2 66 75 74 75 72 65 2e 0a 20 20 2a 2f 0a 20 20 69 future.. */. i
10fb3 66 28 20 4e 45 56 45 52 28 69 4e 65 77 3e 70 43 f( NEVER(iNew>pC
10fb4 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 29 20 29 ache->iMaxKey) )
10fb5 7b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 69 4d {. pCache->iM
10fb6 61 78 4b 65 79 20 3d 20 69 4e 65 77 3b 0a 20 20 axKey = iNew;.
10fb7 7d 0a 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 }.. pcache1Leav
10fb8 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a eMutex();.}../*.
10fb9 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** Implementatio
10fba 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 n of the sqlite3
10fbb 5f 70 63 61 63 68 65 2e 78 54 72 75 6e 63 61 74 _pcache.xTruncat
10fbc 65 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a e method. .**.**
10fbd 20 44 69 73 63 61 72 64 20 61 6c 6c 20 75 6e 70 Discard all unp
10fbe 69 6e 6e 65 64 20 70 61 67 65 73 20 69 6e 20 74 inned pages in t
10fbf 68 65 20 63 61 63 68 65 20 77 69 74 68 20 61 20 he cache with a
10fc0 70 61 67 65 20 6e 75 6d 62 65 72 20 65 71 75 61 page number equa
10fc1 6c 20 74 6f 0a 2a 2a 20 6f 72 20 67 72 65 61 74 l to.** or great
10fc2 65 72 20 74 68 61 6e 20 70 61 72 61 6d 65 74 65 er than paramete
10fc3 72 20 69 4c 69 6d 69 74 2e 20 41 6e 79 20 70 69 r iLimit. Any pi
10fc4 6e 6e 65 64 20 70 61 67 65 73 20 77 69 74 68 20 nned pages with
10fc5 61 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 2a 2a a page number.**
10fc6 20 65 71 75 61 6c 20 74 6f 20 6f 72 20 67 72 65 equal to or gre
10fc7 61 74 65 72 20 74 68 61 6e 20 69 4c 69 6d 69 74 ater than iLimit
10fc8 20 61 72 65 20 69 6d 70 6c 69 63 69 74 6c 79 20 are implicitly
10fc9 75 6e 70 69 6e 6e 65 64 2e 0a 2a 2f 0a 73 74 61 unpinned..*/.sta
10fca 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 tic void pcache1
10fcb 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74 65 33 Truncate(sqlite3
10fcc 5f 70 63 61 63 68 65 20 2a 70 2c 20 75 6e 73 69 _pcache *p, unsi
10fcd 67 6e 65 64 20 69 6e 74 20 69 4c 69 6d 69 74 29 gned int iLimit)
10fce 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 {. PCache1 *pCa
10fcf 63 68 65 20 3d 20 28 50 43 61 63 68 65 31 20 2a che = (PCache1 *
10fd0 29 70 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 )p;. pcache1Ent
10fd1 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 erMutex();. if(
10fd2 20 69 4c 69 6d 69 74 3c 3d 70 43 61 63 68 65 2d iLimit<=pCache-
10fd3 3e 69 4d 61 78 4b 65 79 20 29 7b 0a 20 20 20 20 >iMaxKey ){.
10fd4 70 63 61 63 68 65 31 54 72 75 6e 63 61 74 65 55 pcache1TruncateU
10fd5 6e 73 61 66 65 28 70 43 61 63 68 65 2c 20 69 4c nsafe(pCache, iL
10fd6 69 6d 69 74 29 3b 0a 20 20 20 20 70 43 61 63 68 imit);. pCach
10fd7 65 2d 3e 69 4d 61 78 4b 65 79 20 3d 20 69 4c 69 e->iMaxKey = iLi
10fd8 6d 69 74 2d 31 3b 0a 20 20 7d 0a 20 20 70 63 61 mit-1;. }. pca
10fd9 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 che1LeaveMutex()
10fda 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 ;.}../*.** Imple
10fdb 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 mentation of the
10fdc 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e sqlite3_pcache.
10fdd 78 44 65 73 74 72 6f 79 20 6d 65 74 68 6f 64 2e xDestroy method.
10fde 20 0a 2a 2a 0a 2a 2a 20 44 65 73 74 72 6f 79 20 .**.** Destroy
10fdf 61 20 63 61 63 68 65 20 61 6c 6c 6f 63 61 74 65 a cache allocate
10fe0 64 20 75 73 69 6e 67 20 70 63 61 63 68 65 31 43 d using pcache1C
10fe1 72 65 61 74 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 reate()..*/.stat
10fe2 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 44 ic void pcache1D
10fe3 65 73 74 72 6f 79 28 73 71 6c 69 74 65 33 5f 70 estroy(sqlite3_p
10fe4 63 61 63 68 65 20 2a 70 29 7b 0a 20 20 50 43 61 cache *p){. PCa
10fe5 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28 che1 *pCache = (
10fe6 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 70 PCache1 *)p;. p
10fe7 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 cache1EnterMutex
10fe8 28 29 3b 0a 20 20 70 63 61 63 68 65 31 54 72 75 ();. pcache1Tru
10fe9 6e 63 61 74 65 55 6e 73 61 66 65 28 70 43 61 63 ncateUnsafe(pCac
10fea 68 65 2c 20 30 29 3b 0a 20 20 70 63 61 63 68 65 he, 0);. pcache
10feb 31 2e 6e 4d 61 78 50 61 67 65 20 2d 3d 20 70 43 1.nMaxPage -= pC
10fec 61 63 68 65 2d 3e 6e 4d 61 78 3b 0a 20 20 70 63 ache->nMax;. pc
10fed 61 63 68 65 31 2e 6e 4d 69 6e 50 61 67 65 20 2d ache1.nMinPage -
10fee 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 69 6e 3b 0a = pCache->nMin;.
10fef 20 20 70 63 61 63 68 65 31 45 6e 66 6f 72 63 65 pcache1Enforce
10ff0 4d 61 78 50 61 67 65 28 29 3b 0a 20 20 70 63 61 MaxPage();. pca
10ff1 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 che1LeaveMutex()
10ff2 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 ;. sqlite3_free
10ff3 28 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 29 (pCache->apHash)
10ff4 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 ;. sqlite3_free
10ff5 28 70 43 61 63 68 65 29 3b 0a 7d 0a 0a 2f 2a 0a (pCache);.}../*.
10ff6 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
10ff7 20 69 73 20 63 61 6c 6c 65 64 20 64 75 72 69 6e is called durin
10ff8 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e g initialization
10ff9 20 28 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 (sqlite3_initia
10ffa 6c 69 7a 65 28 29 29 20 74 6f 0a 2a 2a 20 69 6e lize()) to.** in
10ffb 73 74 61 6c 6c 20 74 68 65 20 64 65 66 61 75 6c stall the defaul
10ffc 74 20 70 6c 75 67 67 61 62 6c 65 20 63 61 63 68 t pluggable cach
10ffd 65 20 6d 6f 64 75 6c 65 2c 20 61 73 73 75 6d 69 e module, assumi
10ffe 6e 67 20 74 68 65 20 75 73 65 72 20 68 61 73 20 ng the user has
10fff 6e 6f 74 0a 2a 2a 20 61 6c 72 65 61 64 79 20 70 not.** already p
11000 72 6f 76 69 64 65 64 20 61 6e 20 61 6c 74 65 72 rovided an alter
11001 6e 61 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 native..*/.SQLIT
11002 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
11003 71 6c 69 74 65 33 50 43 61 63 68 65 53 65 74 44 qlite3PCacheSetD
11004 65 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a 20 20 efault(void){.
11005 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 70 static sqlite3_p
11006 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 20 64 65 cache_methods de
11007 66 61 75 6c 74 4d 65 74 68 6f 64 73 20 3d 20 7b faultMethods = {
11008 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 . 0,
11009 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1100a 20 70 41 72 67 20 2a 2f 0a 20 20 20 20 70 63 61 pArg */. pca
1100b 63 68 65 31 49 6e 69 74 2c 20 20 20 20 20 20 20 che1Init,
1100c 20 20 20 20 20 20 2f 2a 20 78 49 6e 69 74 20 2a /* xInit *
1100d 2f 0a 20 20 20 20 70 63 61 63 68 65 31 53 68 75 /. pcache1Shu
1100e 74 64 6f 77 6e 2c 20 20 20 20 20 20 20 20 20 2f tdown, /
1100f 2a 20 78 53 68 75 74 64 6f 77 6e 20 2a 2f 0a 20 * xShutdown */.
11010 20 20 20 70 63 61 63 68 65 31 43 72 65 61 74 65 pcache1Create
11011 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 , /* x
11012 43 72 65 61 74 65 20 2a 2f 0a 20 20 20 20 70 63 Create */. pc
11013 61 63 68 65 31 43 61 63 68 65 73 69 7a 65 2c 20 ache1Cachesize,
11014 20 20 20 20 20 20 20 2f 2a 20 78 43 61 63 68 65 /* xCache
11015 73 69 7a 65 20 2a 2f 0a 20 20 20 20 70 63 61 63 size */. pcac
11016 68 65 31 50 61 67 65 63 6f 75 6e 74 2c 20 20 20 he1Pagecount,
11017 20 20 20 20 20 2f 2a 20 78 50 61 67 65 63 6f 75 /* xPagecou
11018 6e 74 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 nt */. pcache
11019 31 46 65 74 63 68 2c 20 20 20 20 20 20 20 20 20 1Fetch,
1101a 20 20 20 2f 2a 20 78 46 65 74 63 68 20 2a 2f 0a /* xFetch */.
1101b 20 20 20 20 70 63 61 63 68 65 31 55 6e 70 69 6e pcache1Unpin
1101c 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
1101d 78 55 6e 70 69 6e 20 2a 2f 0a 20 20 20 20 70 63 xUnpin */. pc
1101e 61 63 68 65 31 52 65 6b 65 79 2c 20 20 20 20 20 ache1Rekey,
1101f 20 20 20 20 20 20 20 2f 2a 20 78 52 65 6b 65 79 /* xRekey
11020 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 54 */. pcache1T
11021 72 75 6e 63 61 74 65 2c 20 20 20 20 20 20 20 20 runcate,
11022 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 2a 2f /* xTruncate */
11023 0a 20 20 20 20 70 63 61 63 68 65 31 44 65 73 74 . pcache1Dest
11024 72 6f 79 20 20 20 20 20 20 20 20 20 20 20 2f 2a roy /*
11025 20 78 44 65 73 74 72 6f 79 20 2a 2f 0a 20 20 7d xDestroy */. }
11026 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 ;. sqlite3_conf
11027 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 ig(SQLITE_CONFIG
11028 5f 50 43 41 43 48 45 2c 20 26 64 65 66 61 75 6c _PCACHE, &defaul
11029 74 4d 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a 23 69 tMethods);.}..#i
1102a 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
1102b 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 LE_MEMORY_MANAGE
1102c 4d 45 4e 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 MENT./*.** This
1102d 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
1102e 65 64 20 74 6f 20 66 72 65 65 20 73 75 70 65 72 ed to free super
1102f 66 6c 75 6f 75 73 20 64 79 6e 61 6d 69 63 61 6c fluous dynamical
11030 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d ly allocated mem
11031 6f 72 79 0a 2a 2a 20 68 65 6c 64 20 62 79 20 74 ory.** held by t
11032 68 65 20 70 61 67 65 72 20 73 79 73 74 65 6d 2e he pager system.
11033 20 4d 65 6d 6f 72 79 20 69 6e 20 75 73 65 20 62 Memory in use b
11034 79 20 61 6e 79 20 53 51 4c 69 74 65 20 70 61 67 y any SQLite pag
11035 65 72 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 er allocated.**
11036 62 79 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 by the current t
11037 68 72 65 61 64 20 6d 61 79 20 62 65 20 73 71 6c hread may be sql
11038 69 74 65 33 5f 66 72 65 65 28 29 65 64 2e 0a 2a ite3_free()ed..*
11039 2a 0a 2a 2a 20 6e 52 65 71 20 69 73 20 74 68 65 *.** nReq is the
1103a 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
1103b 20 6f 66 20 6d 65 6d 6f 72 79 20 72 65 71 75 69 of memory requi
1103c 72 65 64 2e 20 4f 6e 63 65 20 74 68 69 73 20 6d red. Once this m
1103d 75 63 68 20 68 61 73 0a 2a 2a 20 62 65 65 6e 20 uch has.** been
1103e 72 65 6c 65 61 73 65 64 2c 20 74 68 65 20 66 75 released, the fu
1103f 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e 20 nction returns.
11040 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 The return value
11041 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 is the total nu
11042 6d 62 65 72 20 0a 2a 2a 20 6f 66 20 62 79 74 65 mber .** of byte
11043 73 20 6f 66 20 6d 65 6d 6f 72 79 20 72 65 6c 65 s of memory rele
11044 61 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ased..*/.SQLITE_
11045 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
11046 74 65 33 50 63 61 63 68 65 52 65 6c 65 61 73 65 te3PcacheRelease
11047 4d 65 6d 6f 72 79 28 69 6e 74 20 6e 52 65 71 29 Memory(int nReq)
11048 7b 0a 20 20 69 6e 74 20 6e 46 72 65 65 20 3d 20 {. int nFree =
11049 30 3b 0a 20 20 69 66 28 20 70 63 61 63 68 65 31 0;. if( pcache1
1104a 2e 70 53 74 61 72 74 3d 3d 30 20 29 7b 0a 20 20 .pStart==0 ){.
1104b 20 20 50 67 48 64 72 31 20 2a 70 3b 0a 20 20 20 PgHdr1 *p;.
1104c 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 pcache1EnterMut
1104d 65 78 28 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 ex();. while(
1104e 20 28 6e 52 65 71 3c 30 20 7c 7c 20 6e 46 72 65 (nReq<0 || nFre
1104f 65 3c 6e 52 65 71 29 20 26 26 20 28 70 3d 70 63 e<nReq) && (p=pc
11050 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 29 20 ache1.pLruTail)
11051 29 7b 0a 20 20 20 20 20 20 6e 46 72 65 65 20 2b ){. nFree +
11052 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 = sqlite3MallocS
11053 69 7a 65 28 50 47 48 44 52 31 5f 54 4f 5f 50 41 ize(PGHDR1_TO_PA
11054 47 45 28 70 29 29 3b 0a 20 20 20 20 20 20 70 63 GE(p));. pc
11055 61 63 68 65 31 50 69 6e 50 61 67 65 28 70 29 3b ache1PinPage(p);
11056 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 52 65 . pcache1Re
11057 6d 6f 76 65 46 72 6f 6d 48 61 73 68 28 70 29 3b moveFromHash(p);
11058 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 46 72 . pcache1Fr
11059 65 65 50 61 67 65 28 70 29 3b 0a 20 20 20 20 7d eePage(p);. }
1105a 0a 20 20 20 20 70 63 61 63 68 65 31 4c 65 61 76 . pcache1Leav
1105b 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a 20 20 eMutex();. }.
1105c 72 65 74 75 72 6e 20 6e 46 72 65 65 3b 0a 7d 0a return nFree;.}.
1105d 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
1105e 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d _ENABLE_MEMORY_M
1105f 41 4e 41 47 45 4d 45 4e 54 20 2a 2f 0a 0a 23 69 ANAGEMENT */..#i
11060 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
11061 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
11062 74 69 6f 6e 20 69 73 20 75 73 65 64 20 62 79 20 tion is used by
11063 74 65 73 74 20 70 72 6f 63 65 64 75 72 65 73 20 test procedures
11064 74 6f 20 69 6e 73 70 65 63 74 20 74 68 65 20 69 to inspect the i
11065 6e 74 65 72 6e 61 6c 20 73 74 61 74 65 0a 2a 2a nternal state.**
11066 20 6f 66 20 74 68 65 20 67 6c 6f 62 61 6c 20 63 of the global c
11067 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ache..*/.SQLITE_
11068 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
11069 69 74 65 33 50 63 61 63 68 65 53 74 61 74 73 28 ite3PcacheStats(
1106a 0a 20 20 69 6e 74 20 2a 70 6e 43 75 72 72 65 6e . int *pnCurren
1106b 74 2c 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 t, /* OUT:
1106c 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 Total number of
1106d 70 61 67 65 73 20 63 61 63 68 65 64 20 2a 2f 0a pages cached */.
1106e 20 20 69 6e 74 20 2a 70 6e 4d 61 78 2c 20 20 20 int *pnMax,
1106f 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 47 /* OUT: G
11070 6c 6f 62 61 6c 20 6d 61 78 69 6d 75 6d 20 63 61 lobal maximum ca
11071 63 68 65 20 73 69 7a 65 20 2a 2f 0a 20 20 69 6e che size */. in
11072 74 20 2a 70 6e 4d 69 6e 2c 20 20 20 20 20 20 20 t *pnMin,
11073 20 20 20 2f 2a 20 4f 55 54 3a 20 53 75 6d 20 6f /* OUT: Sum o
11074 66 20 50 43 61 63 68 65 31 2e 6e 4d 69 6e 20 66 f PCache1.nMin f
11075 6f 72 20 70 75 72 67 65 61 62 6c 65 20 63 61 63 or purgeable cac
11076 68 65 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e hes */. int *pn
11077 52 65 63 79 63 6c 61 62 6c 65 20 20 20 20 2f 2a Recyclable /*
11078 20 4f 55 54 3a 20 54 6f 74 61 6c 20 6e 75 6d 62 OUT: Total numb
11079 65 72 20 6f 66 20 70 61 67 65 73 20 61 76 61 69 er of pages avai
1107a 6c 61 62 6c 65 20 66 6f 72 20 72 65 63 79 63 6c lable for recycl
1107b 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 50 67 48 64 ing */.){. PgHd
1107c 72 31 20 2a 70 3b 0a 20 20 69 6e 74 20 6e 52 65 r1 *p;. int nRe
1107d 63 79 63 6c 61 62 6c 65 20 3d 20 30 3b 0a 20 20 cyclable = 0;.
1107e 66 6f 72 28 70 3d 70 63 61 63 68 65 31 2e 70 4c for(p=pcache1.pL
1107f 72 75 48 65 61 64 3b 20 70 3b 20 70 3d 70 2d 3e ruHead; p; p=p->
11080 70 4c 72 75 4e 65 78 74 29 7b 0a 20 20 20 20 6e pLruNext){. n
11081 52 65 63 79 63 6c 61 62 6c 65 2b 2b 3b 0a 20 20 Recyclable++;.
11082 7d 0a 20 20 2a 70 6e 43 75 72 72 65 6e 74 20 3d }. *pnCurrent =
11083 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e pcache1.nCurren
11084 74 50 61 67 65 3b 0a 20 20 2a 70 6e 4d 61 78 20 tPage;. *pnMax
11085 3d 20 70 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 = pcache1.nMaxPa
11086 67 65 3b 0a 20 20 2a 70 6e 4d 69 6e 20 3d 20 70 ge;. *pnMin = p
11087 63 61 63 68 65 31 2e 6e 4d 69 6e 50 61 67 65 3b cache1.nMinPage;
11088 0a 20 20 2a 70 6e 52 65 63 79 63 6c 61 62 6c 65 . *pnRecyclable
11089 20 3d 20 6e 52 65 63 79 63 6c 61 62 6c 65 3b 0a = nRecyclable;.
1108a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a }.#endif../*****
1108b 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
1108c 20 70 63 61 63 68 65 31 2e 63 20 2a 2a 2a 2a 2a pcache1.c *****
1108d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1108e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1108f 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
11090 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
11091 66 69 6c 65 20 72 6f 77 73 65 74 2e 63 20 2a 2a file rowset.c **
11092 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11093 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11094 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
11095 32 30 30 38 20 44 65 63 65 6d 62 65 72 20 33 0a 2008 December 3.
11096 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
11097 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
11098 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
11099 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
1109a 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
1109b 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
1109c 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
1109d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
1109e 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
1109f 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
110a0 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
110a1 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
110a2 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
110a3 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
110a4 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
110a5 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
110a6 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
110a7 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
110a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
110a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
110aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
110ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
110ac 2a 0a 2a 2a 20 54 68 69 73 20 6d 6f 64 75 6c 65 *.** This module
110ad 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 6e 20 6f implements an o
110ae 62 6a 65 63 74 20 77 65 20 63 61 6c 6c 20 61 20 bject we call a
110af 22 52 6f 77 53 65 74 22 2e 0a 2a 2a 0a 2a 2a 20 "RowSet"..**.**
110b0 54 68 65 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 The RowSet objec
110b1 74 20 69 73 20 61 20 63 6f 6c 6c 65 63 74 69 6f t is a collectio
110b2 6e 20 6f 66 20 72 6f 77 69 64 73 2e 20 20 52 6f n of rowids. Ro
110b3 77 69 64 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 wids.** are inse
110b4 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20 52 6f rted into the Ro
110b5 77 53 65 74 20 69 6e 20 61 6e 20 61 72 62 69 74 wSet in an arbit
110b6 72 61 72 79 20 6f 72 64 65 72 2e 20 20 49 6e 73 rary order. Ins
110b7 65 72 74 73 0a 2a 2a 20 63 61 6e 20 62 65 20 69 erts.** can be i
110b8 6e 74 65 72 6d 69 78 65 64 20 77 69 74 68 20 74 ntermixed with t
110b9 65 73 74 73 20 74 6f 20 73 65 65 20 69 66 20 61 ests to see if a
110ba 20 67 69 76 65 6e 20 72 6f 77 69 64 20 68 61 73 given rowid has
110bb 20 62 65 65 6e 0a 2a 2a 20 70 72 65 76 69 6f 75 been.** previou
110bc 73 6c 79 20 69 6e 73 65 72 74 65 64 20 69 6e 74 sly inserted int
110bd 6f 20 74 68 65 20 52 6f 77 53 65 74 2e 0a 2a 2a o the RowSet..**
110be 0a 2a 2a 20 41 66 74 65 72 20 61 6c 6c 20 69 6e .** After all in
110bf 73 65 72 74 73 20 61 72 65 20 66 69 6e 69 73 68 serts are finish
110c0 65 64 2c 20 69 74 20 69 73 20 70 6f 73 73 69 62 ed, it is possib
110c1 6c 65 20 74 6f 20 65 78 74 72 61 63 74 20 74 68 le to extract th
110c2 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74 73 20 6f 66 e.** elements of
110c3 20 74 68 65 20 52 6f 77 53 65 74 20 69 6e 20 73 the RowSet in s
110c4 6f 72 74 65 64 20 6f 72 64 65 72 2e 20 20 4f 6e orted order. On
110c5 63 65 20 74 68 69 73 20 65 78 74 72 61 63 74 69 ce this extracti
110c6 6f 6e 0a 2a 2a 20 70 72 6f 63 65 73 73 20 68 61 on.** process ha
110c7 73 20 73 74 61 72 74 65 64 2c 20 6e 6f 20 6e 65 s started, no ne
110c8 77 20 65 6c 65 6d 65 6e 74 73 20 6d 61 79 20 62 w elements may b
110c9 65 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2a 0a 2a e inserted..**.*
110ca 2a 20 48 65 6e 63 65 2c 20 74 68 65 20 70 72 69 * Hence, the pri
110cb 6d 69 74 69 76 65 20 6f 70 65 72 61 74 69 6f 6e mitive operation
110cc 73 20 66 6f 72 20 61 20 52 6f 77 53 65 74 20 61 s for a RowSet a
110cd 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 43 52 45 re:.**.** CRE
110ce 41 54 45 0a 2a 2a 20 20 20 20 49 4e 53 45 52 54 ATE.** INSERT
110cf 0a 2a 2a 20 20 20 20 54 45 53 54 0a 2a 2a 20 20 .** TEST.**
110d0 20 20 53 4d 41 4c 4c 45 53 54 0a 2a 2a 20 20 20 SMALLEST.**
110d1 20 44 45 53 54 52 4f 59 0a 2a 2a 0a 2a 2a 20 54 DESTROY.**.** T
110d2 68 65 20 43 52 45 41 54 45 20 61 6e 64 20 44 45 he CREATE and DE
110d3 53 54 52 4f 59 20 70 72 69 6d 69 74 69 76 65 73 STROY primitives
110d4 20 61 72 65 20 74 68 65 20 63 6f 6e 73 74 72 75 are the constru
110d5 63 74 6f 72 20 61 6e 64 20 64 65 73 74 72 75 63 ctor and destruc
110d6 74 6f 72 2c 0a 2a 2a 20 6f 62 76 69 6f 75 73 6c tor,.** obviousl
110d7 79 2e 20 20 54 68 65 20 49 4e 53 45 52 54 20 70 y. The INSERT p
110d8 72 69 6d 69 74 69 76 65 20 61 64 64 73 20 61 20 rimitive adds a
110d9 6e 65 77 20 65 6c 65 6d 65 6e 74 20 74 6f 20 74 new element to t
110da 68 65 20 52 6f 77 53 65 74 2e 0a 2a 2a 20 54 45 he RowSet..** TE
110db 53 54 20 63 68 65 63 6b 73 20 74 6f 20 73 65 65 ST checks to see
110dc 20 69 66 20 61 6e 20 65 6c 65 6d 65 6e 74 20 69 if an element i
110dd 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 s already in the
110de 20 52 6f 77 53 65 74 2e 20 20 53 4d 41 4c 4c 45 RowSet. SMALLE
110df 53 54 0a 2a 2a 20 65 78 74 72 61 63 74 73 20 74 ST.** extracts t
110e0 68 65 20 6c 65 61 73 74 20 76 61 6c 75 65 20 66 he least value f
110e1 72 6f 6d 20 74 68 65 20 52 6f 77 53 65 74 2e 0a rom the RowSet..
110e2 2a 2a 0a 2a 2a 20 54 68 65 20 49 4e 53 45 52 54 **.** The INSERT
110e3 20 70 72 69 6d 69 74 69 76 65 20 6d 69 67 68 74 primitive might
110e4 20 61 6c 6c 6f 63 61 74 65 20 61 64 64 69 74 69 allocate additi
110e5 6f 6e 61 6c 20 6d 65 6d 6f 72 79 2e 20 20 4d 65 onal memory. Me
110e6 6d 6f 72 79 20 69 73 0a 2a 2a 20 61 6c 6c 6f 63 mory is.** alloc
110e7 61 74 65 64 20 69 6e 20 63 68 75 6e 6b 73 20 73 ated in chunks s
110e8 6f 20 6d 6f 73 74 20 49 4e 53 45 52 54 73 20 64 o most INSERTs d
110e9 6f 20 6e 6f 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e o no allocation.
110ea 20 20 54 68 65 72 65 20 69 73 20 61 6e 20 0a 2a There is an .*
110eb 2a 20 75 70 70 65 72 20 62 6f 75 6e 64 20 6f 6e * upper bound on
110ec 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6c 6c the size of all
110ed 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e 20 20 ocated memory.
110ee 4e 6f 20 6d 65 6d 6f 72 79 20 69 73 20 66 72 65 No memory is fre
110ef 65 64 0a 2a 2a 20 75 6e 74 69 6c 20 44 45 53 54 ed.** until DEST
110f0 52 4f 59 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 54 ROY..**.** The T
110f1 45 53 54 20 70 72 69 6d 69 74 69 76 65 20 69 6e EST primitive in
110f2 63 6c 75 64 65 73 20 61 20 22 62 61 74 63 68 22 cludes a "batch"
110f3 20 6e 75 6d 62 65 72 2e 20 20 54 68 65 20 54 45 number. The TE
110f4 53 54 20 70 72 69 6d 69 74 69 76 65 0a 2a 2a 20 ST primitive.**
110f5 77 69 6c 6c 20 6f 6e 6c 79 20 73 65 65 20 65 6c will only see el
110f6 65 6d 65 6e 74 73 20 74 68 61 74 20 77 65 72 65 ements that were
110f7 20 69 6e 73 65 72 74 65 64 20 62 65 66 6f 72 65 inserted before
110f8 20 74 68 65 20 6c 61 73 74 20 63 68 61 6e 67 65 the last change
110f9 0a 2a 2a 20 69 6e 20 74 68 65 20 62 61 74 63 68 .** in the batch
110fa 20 6e 75 6d 62 65 72 2e 20 20 49 6e 20 6f 74 68 number. In oth
110fb 65 72 20 77 6f 72 64 73 2c 20 69 66 20 61 6e 20 er words, if an
110fc 49 4e 53 45 52 54 20 6f 63 63 75 72 73 20 62 65 INSERT occurs be
110fd 74 77 65 65 6e 0a 2a 2a 20 74 77 6f 20 54 45 53 tween.** two TES
110fe 54 73 20 77 68 65 72 65 20 74 68 65 20 54 45 53 Ts where the TES
110ff 54 73 20 68 61 76 65 20 74 68 65 20 73 61 6d 65 Ts have the same
11100 20 62 61 74 63 68 20 6e 75 62 6d 65 72 2c 20 74 batch nubmer, t
11101 68 65 6e 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 hen the.** value
11102 20 61 64 64 65 64 20 62 79 20 74 68 65 20 49 4e added by the IN
11103 53 45 52 54 20 77 69 6c 6c 20 6e 6f 74 20 62 65 SERT will not be
11104 20 76 69 73 69 62 6c 65 20 74 6f 20 74 68 65 20 visible to the
11105 73 65 63 6f 6e 64 20 54 45 53 54 2e 0a 2a 2a 20 second TEST..**
11106 54 68 65 20 69 6e 69 74 69 61 6c 20 62 61 74 63 The initial batc
11107 68 20 6e 75 6d 62 65 72 20 69 73 20 7a 65 72 6f h number is zero
11108 2c 20 73 6f 20 69 66 20 74 68 65 20 76 65 72 79 , so if the very
11109 20 66 69 72 73 74 20 54 45 53 54 20 63 6f 6e 74 first TEST cont
1110a 61 69 6e 73 0a 2a 2a 20 61 20 6e 6f 6e 2d 7a 65 ains.** a non-ze
1110b 72 6f 20 62 61 74 63 68 20 6e 75 6d 62 65 72 2c ro batch number,
1110c 20 69 74 20 77 69 6c 6c 20 73 65 65 20 61 6c 6c it will see all
1110d 20 70 72 69 6f 72 20 49 4e 53 45 52 54 73 2e 0a prior INSERTs..
1110e 2a 2a 0a 2a 2a 20 4e 6f 20 49 4e 53 45 52 54 73 **.** No INSERTs
1110f 20 6d 61 79 20 6f 63 63 75 72 73 20 61 66 74 65 may occurs afte
11110 72 20 61 20 53 4d 41 4c 4c 45 53 54 2e 20 20 41 r a SMALLEST. A
11111 6e 20 61 73 73 65 72 74 69 6f 6e 20 77 69 6c 6c n assertion will
11112 20 66 61 69 6c 20 69 66 0a 2a 2a 20 74 68 61 74 fail if.** that
11113 20 69 73 20 61 74 74 65 6d 70 74 65 64 2e 0a 2a is attempted..*
11114 2a 0a 2a 2a 20 54 68 65 20 63 6f 73 74 20 6f 66 *.** The cost of
11115 20 61 6e 20 49 4e 53 45 52 54 20 69 73 20 72 6f an INSERT is ro
11116 75 67 68 6c 79 20 63 6f 6e 73 74 61 6e 74 2e 20 ughly constant.
11117 20 28 53 6f 6d 65 74 69 6d 65 20 6e 65 77 20 6d (Sometime new m
11118 65 6d 6f 72 79 0a 2a 2a 20 68 61 73 20 74 6f 20 emory.** has to
11119 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 6f 6e 20 be allocated on
1111a 61 6e 20 49 4e 53 45 52 54 2e 29 20 20 54 68 65 an INSERT.) The
1111b 20 63 6f 73 74 20 6f 66 20 61 20 54 45 53 54 20 cost of a TEST
1111c 77 69 74 68 20 61 20 6e 65 77 0a 2a 2a 20 62 61 with a new.** ba
1111d 74 63 68 20 6e 75 6d 62 65 72 20 69 73 20 4f 28 tch number is O(
1111e 4e 6c 6f 67 4e 29 20 77 68 65 72 65 20 4e 20 69 NlogN) where N i
1111f 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
11120 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 elements in the
11121 52 6f 77 53 65 74 2e 0a 2a 2a 20 54 68 65 20 63 RowSet..** The c
11122 6f 73 74 20 6f 66 20 61 20 54 45 53 54 20 75 73 ost of a TEST us
11123 69 6e 67 20 74 68 65 20 73 61 6d 65 20 62 61 74 ing the same bat
11124 63 68 20 6e 75 6d 62 65 72 20 69 73 20 4f 28 6c ch number is O(l
11125 6f 67 4e 29 2e 20 20 54 68 65 20 63 6f 73 74 0a ogN). The cost.
11126 2a 2a 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 ** of the first
11127 53 4d 41 4c 4c 45 53 54 20 69 73 20 4f 28 4e 6c SMALLEST is O(Nl
11128 6f 67 4e 29 2e 20 20 53 65 63 6f 6e 64 20 61 6e ogN). Second an
11129 64 20 73 75 62 73 65 71 75 65 6e 74 20 53 4d 41 d subsequent SMA
1112a 4c 4c 45 53 54 0a 2a 2a 20 70 72 69 6d 69 74 69 LLEST.** primiti
1112b 76 65 73 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 ves are constant
1112c 20 74 69 6d 65 2e 20 20 54 68 65 20 63 6f 73 74 time. The cost
1112d 20 6f 66 20 44 45 53 54 52 4f 59 20 69 73 20 4f of DESTROY is O
1112e 28 4e 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 (N)..**.** There
1112f 20 69 73 20 61 6e 20 61 64 64 65 64 20 63 6f 73 is an added cos
11130 74 20 6f 66 20 4f 28 4e 29 20 77 68 65 6e 20 73 t of O(N) when s
11131 77 69 74 63 68 69 6e 67 20 62 65 74 77 65 65 6e witching between
11132 20 54 45 53 54 20 61 6e 64 0a 2a 2a 20 53 4d 41 TEST and.** SMA
11133 4c 4c 45 53 54 20 70 72 69 6d 69 74 69 76 65 73 LLEST primitives
11134 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 72 6f 77 ..**.** $Id: row
11135 73 65 74 2e 63 2c 76 20 31 2e 37 20 32 30 30 39 set.c,v 1.7 2009
11136 2f 30 35 2f 32 32 20 30 31 3a 30 30 3a 31 33 20 /05/22 01:00:13
11137 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f drh Exp $.*/.../
11138 2a 0a 2a 2a 20 54 61 72 67 65 74 20 73 69 7a 65 *.** Target size
11139 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 for allocation
1113a 63 68 75 6e 6b 73 2e 0a 2a 2f 0a 23 64 65 66 69 chunks..*/.#defi
1113b 6e 65 20 52 4f 57 53 45 54 5f 41 4c 4c 4f 43 41 ne ROWSET_ALLOCA
1113c 54 49 4f 4e 5f 53 49 5a 45 20 31 30 32 34 0a 0a TION_SIZE 1024..
1113d 2f 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 /*.** The number
1113e 20 6f 66 20 72 6f 77 73 65 74 20 65 6e 74 72 69 of rowset entri
1113f 65 73 20 70 65 72 20 61 6c 6c 6f 63 61 74 69 6f es per allocatio
11140 6e 20 63 68 75 6e 6b 2e 0a 2a 2f 0a 23 64 65 66 n chunk..*/.#def
11141 69 6e 65 20 52 4f 57 53 45 54 5f 45 4e 54 52 59 ine ROWSET_ENTRY
11142 5f 50 45 52 5f 43 48 55 4e 4b 20 20 5c 0a 20 20 _PER_CHUNK \.
11143 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11144 20 20 20 20 20 28 28 52 4f 57 53 45 54 5f 41 4c ((ROWSET_AL
11145 4c 4f 43 41 54 49 4f 4e 5f 53 49 5a 45 2d 38 29 LOCATION_SIZE-8)
11146 2f 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 52 /sizeof(struct R
11147 6f 77 53 65 74 45 6e 74 72 79 29 29 0a 0a 2f 2a owSetEntry))../*
11148 0a 2a 2a 20 45 61 63 68 20 65 6e 74 72 79 20 69 .** Each entry i
11149 6e 20 61 20 52 6f 77 53 65 74 20 69 73 20 61 6e n a RowSet is an
1114a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
1114b 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 62 6a 65 63 following objec
1114c 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 6f 77 t..*/.struct Row
1114d 53 65 74 45 6e 74 72 79 20 7b 20 20 20 20 20 20 SetEntry {
1114e 20 20 20 20 20 20 0a 20 20 69 36 34 20 76 3b 20 . i64 v;
1114f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11150 20 20 20 20 20 20 20 2f 2a 20 52 4f 57 49 44 20 /* ROWID
11151 76 61 6c 75 65 20 66 6f 72 20 74 68 69 73 20 65 value for this e
11152 6e 74 72 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 ntry */. struct
11153 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 52 RowSetEntry *pR
11154 69 67 68 74 3b 20 20 20 2f 2a 20 52 69 67 68 74 ight; /* Right
11155 20 73 75 62 74 72 65 65 20 28 6c 61 72 67 65 72 subtree (larger
11156 20 65 6e 74 72 69 65 73 29 20 6f 72 20 6c 69 73 entries) or lis
11157 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f t */. struct Ro
11158 77 53 65 74 45 6e 74 72 79 20 2a 70 4c 65 66 74 wSetEntry *pLeft
11159 3b 20 20 20 20 2f 2a 20 4c 65 66 74 20 73 75 62 ; /* Left sub
1115a 74 72 65 65 20 28 73 6d 61 6c 6c 65 72 20 65 6e tree (smaller en
1115b 74 72 69 65 73 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a tries) */.};../*
1115c 0a 2a 2a 20 52 6f 77 53 65 74 45 6e 74 72 79 20 .** RowSetEntry
1115d 6f 62 6a 65 63 74 73 20 61 72 65 20 61 6c 6c 6f objects are allo
1115e 63 61 74 65 64 20 69 6e 20 6c 61 72 67 65 20 63 cated in large c
1115f 68 75 6e 6b 73 20 28 69 6e 73 74 61 6e 63 65 73 hunks (instances
11160 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f of the.** follo
11161 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 29 20 wing structure)
11162 74 6f 20 72 65 64 75 63 65 20 6d 65 6d 6f 72 79 to reduce memory
11163 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 76 65 72 allocation over
11164 68 65 61 64 2e 20 20 54 68 65 0a 2a 2a 20 63 68 head. The.** ch
11165 75 6e 6b 73 20 61 72 65 20 6b 65 70 74 20 6f 6e unks are kept on
11166 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 73 a linked list s
11167 6f 20 74 68 61 74 20 74 68 65 79 20 63 61 6e 20 o that they can
11168 62 65 20 64 65 61 6c 6c 6f 63 61 74 65 64 0a 2a be deallocated.*
11169 2a 20 77 68 65 6e 20 74 68 65 20 52 6f 77 53 65 * when the RowSe
1116a 74 20 69 73 20 64 65 73 74 72 6f 79 65 64 2e 0a t is destroyed..
1116b 2a 2f 0a 73 74 72 75 63 74 20 52 6f 77 53 65 74 */.struct RowSet
1116c 43 68 75 6e 6b 20 7b 0a 20 20 73 74 72 75 63 74 Chunk {. struct
1116d 20 52 6f 77 53 65 74 43 68 75 6e 6b 20 2a 70 4e RowSetChunk *pN
1116e 65 78 74 43 68 75 6e 6b 3b 20 20 20 20 20 20 20 extChunk;
1116f 20 2f 2a 20 4e 65 78 74 20 63 68 75 6e 6b 20 6f /* Next chunk o
11170 6e 20 6c 69 73 74 20 6f 66 20 74 68 65 6d 20 61 n list of them a
11171 6c 6c 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 ll */. struct R
11172 6f 77 53 65 74 45 6e 74 72 79 20 61 45 6e 74 72 owSetEntry aEntr
11173 79 5b 52 4f 57 53 45 54 5f 45 4e 54 52 59 5f 50 y[ROWSET_ENTRY_P
11174 45 52 5f 43 48 55 4e 4b 5d 3b 20 2f 2a 20 41 6c ER_CHUNK]; /* Al
11175 6c 6f 63 61 74 65 64 20 65 6e 74 72 69 65 73 20 located entries
11176 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 52 */.};../*.** A R
11177 6f 77 53 65 74 20 69 6e 20 61 6e 20 69 6e 73 74 owSet in an inst
11178 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c ance of the foll
11179 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e owing structure.
1117a 0a 2a 2a 0a 2a 2a 20 41 20 74 79 70 65 64 65 66 .**.** A typedef
1117b 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 of this structu
1117c 72 65 20 69 66 20 66 6f 75 6e 64 20 69 6e 20 73 re if found in s
1117d 71 6c 69 74 65 49 6e 74 2e 68 2e 0a 2a 2f 0a 73 qliteInt.h..*/.s
1117e 74 72 75 63 74 20 52 6f 77 53 65 74 20 7b 0a 20 truct RowSet {.
1117f 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 43 68 struct RowSetCh
11180 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b 20 20 20 20 unk *pChunk;
11181 2f 2a 20 4c 69 73 74 20 6f 66 20 61 6c 6c 20 63 /* List of all c
11182 68 75 6e 6b 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 hunk allocations
11183 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 */. sqlite3 *d
11184 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 b;
11185 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 /* The data
11186 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
11187 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 */. struct RowS
11188 65 74 45 6e 74 72 79 20 2a 70 45 6e 74 72 79 3b etEntry *pEntry;
11189 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 65 /* List of e
1118a 6e 74 72 69 65 73 20 75 73 69 6e 67 20 70 52 69 ntries using pRi
1118b 67 68 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 ght */. struct
1118c 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4c 61 RowSetEntry *pLa
1118d 73 74 3b 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 st; /* Last
1118e 65 6e 74 72 79 20 6f 6e 20 74 68 65 20 70 45 6e entry on the pEn
1118f 74 72 79 20 6c 69 73 74 20 2a 2f 0a 20 20 73 74 try list */. st
11190 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
11191 20 2a 70 46 72 65 73 68 3b 20 20 20 20 2f 2a 20 *pFresh; /*
11192 53 6f 75 72 63 65 20 6f 66 20 6e 65 77 20 65 6e Source of new en
11193 74 72 79 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 20 try objects */.
11194 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e struct RowSetEn
11195 74 72 79 20 2a 70 54 72 65 65 3b 20 20 20 20 20 try *pTree;
11196 2f 2a 20 42 69 6e 61 72 79 20 74 72 65 65 20 6f /* Binary tree o
11197 66 20 65 6e 74 72 69 65 73 20 2a 2f 0a 20 20 75 f entries */. u
11198 31 36 20 6e 46 72 65 73 68 3b 20 20 20 20 20 20 16 nFresh;
11199 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1119a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 62 6a 65 63 Number of objec
1119b 74 73 20 6f 6e 20 70 46 72 65 73 68 20 2a 2f 0a ts on pFresh */.
1119c 20 20 75 38 20 69 73 53 6f 72 74 65 64 3b 20 20 u8 isSorted;
1119d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1119e 20 2f 2a 20 54 72 75 65 20 69 66 20 70 45 6e 74 /* True if pEnt
1119f 72 79 20 69 73 20 73 6f 72 74 65 64 20 2a 2f 0a ry is sorted */.
111a0 20 20 75 38 20 69 42 61 74 63 68 3b 20 20 20 20 u8 iBatch;
111a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
111a2 20 2f 2a 20 43 75 72 72 65 6e 74 20 69 6e 73 65 /* Current inse
111a3 72 74 20 62 61 74 63 68 20 2a 2f 0a 7d 3b 0a 0a rt batch */.};..
111a4 2f 2a 0a 2a 2a 20 54 75 72 6e 20 62 75 6c 6b 20 /*.** Turn bulk
111a5 6d 65 6d 6f 72 79 20 69 6e 74 6f 20 61 20 52 6f memory into a Ro
111a6 77 53 65 74 20 6f 62 6a 65 63 74 2e 20 20 4e 20 wSet object. N
111a7 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 0a bytes of memory.
111a8 2a 2a 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 ** are available
111a9 20 61 74 20 70 53 70 61 63 65 2e 20 20 54 68 65 at pSpace. The
111aa 20 64 62 20 70 6f 69 6e 74 65 72 20 69 73 20 75 db pointer is u
111ab 73 65 64 20 61 73 20 61 20 6d 65 6d 6f 72 79 20 sed as a memory
111ac 63 6f 6e 74 65 78 74 0a 2a 2a 20 66 6f 72 20 61 context.** for a
111ad 6e 79 20 73 75 62 73 65 71 75 65 6e 74 20 61 6c ny subsequent al
111ae 6c 6f 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6e locations that n
111af 65 65 64 20 74 6f 20 6f 63 63 75 72 2e 0a 2a 2a eed to occur..**
111b0 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 Return a pointe
111b1 72 20 74 6f 20 74 68 65 20 6e 65 77 20 52 6f 77 r to the new Row
111b2 53 65 74 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a Set object..**.*
111b3 2a 20 49 74 20 6d 75 73 74 20 62 65 20 74 68 65 * It must be the
111b4 20 63 61 73 65 20 74 68 61 74 20 4e 20 69 73 20 case that N is
111b5 73 75 66 66 69 63 69 65 6e 74 20 74 6f 20 6d 61 sufficient to ma
111b6 6b 65 20 61 20 52 6f 77 73 65 74 2e 20 20 49 66 ke a Rowset. If
111b7 20 6e 6f 74 0a 2a 2a 20 61 6e 20 61 73 73 65 72 not.** an asser
111b8 74 69 6f 6e 20 66 61 75 6c 74 20 6f 63 63 75 72 tion fault occur
111b9 73 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 4e 20 69 s..** .** If N i
111ba 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 s larger than th
111bb 65 20 6d 69 6e 69 6d 75 6d 2c 20 75 73 65 20 74 e minimum, use t
111bc 68 65 20 73 75 72 70 6c 75 73 20 61 73 20 61 6e he surplus as an
111bd 20 69 6e 69 74 69 61 6c 0a 2a 2a 20 61 6c 6c 6f initial.** allo
111be 63 61 74 69 6f 6e 20 6f 66 20 65 6e 74 72 69 65 cation of entrie
111bf 73 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 62 s available to b
111c0 65 20 66 69 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c e filled..*/.SQL
111c1 49 54 45 5f 50 52 49 56 41 54 45 20 52 6f 77 53 ITE_PRIVATE RowS
111c2 65 74 20 2a 73 71 6c 69 74 65 33 52 6f 77 53 65 et *sqlite3RowSe
111c3 74 49 6e 69 74 28 73 71 6c 69 74 65 33 20 2a 64 tInit(sqlite3 *d
111c4 62 2c 20 76 6f 69 64 20 2a 70 53 70 61 63 65 2c b, void *pSpace,
111c5 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 4e 29 unsigned int N)
111c6 7b 0a 20 20 52 6f 77 53 65 74 20 2a 70 3b 0a 20 {. RowSet *p;.
111c7 20 61 73 73 65 72 74 28 20 4e 20 3e 3d 20 52 4f assert( N >= RO
111c8 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a 70 29 29 UND8(sizeof(*p))
111c9 20 29 3b 0a 20 20 70 20 3d 20 70 53 70 61 63 65 );. p = pSpace
111ca 3b 0a 20 20 70 2d 3e 70 43 68 75 6e 6b 20 3d 20 ;. p->pChunk =
111cb 30 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0;. p->db = db;
111cc 0a 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 30 . p->pEntry = 0
111cd 3b 0a 20 20 70 2d 3e 70 4c 61 73 74 20 3d 20 30 ;. p->pLast = 0
111ce 3b 0a 20 20 70 2d 3e 70 54 72 65 65 20 3d 20 30 ;. p->pTree = 0
111cf 3b 0a 20 20 70 2d 3e 70 46 72 65 73 68 20 3d 20 ;. p->pFresh =
111d0 28 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e (struct RowSetEn
111d1 74 72 79 2a 29 28 52 4f 55 4e 44 38 28 73 69 7a try*)(ROUND8(siz
111d2 65 6f 66 28 2a 70 29 29 20 2b 20 28 63 68 61 72 eof(*p)) + (char
111d3 2a 29 70 29 3b 0a 20 20 70 2d 3e 6e 46 72 65 73 *)p);. p->nFres
111d4 68 20 3d 20 28 75 31 36 29 28 28 4e 20 2d 20 52 h = (u16)((N - R
111d5 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a 70 29 OUND8(sizeof(*p)
111d6 29 29 2f 73 69 7a 65 6f 66 28 73 74 72 75 63 74 ))/sizeof(struct
111d7 20 52 6f 77 53 65 74 45 6e 74 72 79 29 29 3b 0a RowSetEntry));.
111d8 20 20 70 2d 3e 69 73 53 6f 72 74 65 64 20 3d 20 p->isSorted =
111d9 31 3b 0a 20 20 70 2d 3e 69 42 61 74 63 68 20 3d 1;. p->iBatch =
111da 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 0;. return p;.
111db 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 61 6c 6c 6f 63 }../*.** Dealloc
111dc 61 74 65 20 61 6c 6c 20 63 68 75 6e 6b 73 20 66 ate all chunks f
111dd 72 6f 6d 20 61 20 52 6f 77 53 65 74 2e 20 20 54 rom a RowSet. T
111de 68 69 73 20 66 72 65 65 73 20 61 6c 6c 20 6d 65 his frees all me
111df 6d 6f 72 79 20 74 68 61 74 0a 2a 2a 20 74 68 65 mory that.** the
111e0 20 52 6f 77 53 65 74 20 68 61 73 20 61 6c 6c 6f RowSet has allo
111e1 63 61 74 65 64 20 6f 76 65 72 20 69 74 73 20 6c cated over its l
111e2 69 66 65 74 69 6d 65 2e 20 20 54 68 69 73 20 72 ifetime. This r
111e3 6f 75 74 69 6e 65 20 69 73 0a 2a 2a 20 74 68 65 outine is.** the
111e4 20 64 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20 destructor for
111e5 74 68 65 20 52 6f 77 53 65 74 2e 0a 2a 2f 0a 53 the RowSet..*/.S
111e6 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
111e7 69 64 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 id sqlite3RowSet
111e8 43 6c 65 61 72 28 52 6f 77 53 65 74 20 2a 70 29 Clear(RowSet *p)
111e9 7b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 {. struct RowSe
111ea 74 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 2c 20 tChunk *pChunk,
111eb 2a 70 4e 65 78 74 43 68 75 6e 6b 3b 0a 20 20 66 *pNextChunk;. f
111ec 6f 72 28 70 43 68 75 6e 6b 3d 70 2d 3e 70 43 68 or(pChunk=p->pCh
111ed 75 6e 6b 3b 20 70 43 68 75 6e 6b 3b 20 70 43 68 unk; pChunk; pCh
111ee 75 6e 6b 20 3d 20 70 4e 65 78 74 43 68 75 6e 6b unk = pNextChunk
111ef 29 7b 0a 20 20 20 20 70 4e 65 78 74 43 68 75 6e ){. pNextChun
111f0 6b 20 3d 20 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 k = pChunk->pNex
111f1 74 43 68 75 6e 6b 3b 0a 20 20 20 20 73 71 6c 69 tChunk;. sqli
111f2 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62 2c te3DbFree(p->db,
111f3 20 70 43 68 75 6e 6b 29 3b 0a 20 20 7d 0a 20 20 pChunk);. }.
111f4 70 2d 3e 70 43 68 75 6e 6b 20 3d 20 30 3b 0a 20 p->pChunk = 0;.
111f5 20 70 2d 3e 6e 46 72 65 73 68 20 3d 20 30 3b 0a p->nFresh = 0;.
111f6 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 30 3b p->pEntry = 0;
111f7 0a 20 20 70 2d 3e 70 4c 61 73 74 20 3d 20 30 3b . p->pLast = 0;
111f8 0a 20 20 70 2d 3e 70 54 72 65 65 20 3d 20 30 3b . p->pTree = 0;
111f9 0a 20 20 70 2d 3e 69 73 53 6f 72 74 65 64 20 3d . p->isSorted =
111fa 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 1;.}../*.** Ins
111fb 65 72 74 20 61 20 6e 65 77 20 76 61 6c 75 65 20 ert a new value
111fc 69 6e 74 6f 20 61 20 52 6f 77 53 65 74 2e 0a 2a into a RowSet..*
111fd 2a 0a 2a 2a 20 54 68 65 20 6d 61 6c 6c 6f 63 46 *.** The mallocF
111fe 61 69 6c 65 64 20 66 6c 61 67 20 6f 66 20 74 68 ailed flag of th
111ff 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
11200 63 74 69 6f 6e 20 69 73 20 73 65 74 20 69 66 20 ction is set if
11201 61 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f a.** memory allo
11202 63 61 74 69 6f 6e 20 66 61 69 6c 73 2e 0a 2a 2f cation fails..*/
11203 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
11204 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 77 53 void sqlite3RowS
11205 65 74 49 6e 73 65 72 74 28 52 6f 77 53 65 74 20 etInsert(RowSet
11206 2a 70 2c 20 69 36 34 20 72 6f 77 69 64 29 7b 0a *p, i64 rowid){.
11207 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 struct RowSetE
11208 6e 74 72 79 20 2a 70 45 6e 74 72 79 3b 20 20 2f ntry *pEntry; /
11209 2a 20 54 68 65 20 6e 65 77 20 65 6e 74 72 79 20 * The new entry
1120a 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 */. struct RowS
1120b 65 74 45 6e 74 72 79 20 2a 70 4c 61 73 74 3b 20 etEntry *pLast;
1120c 20 20 2f 2a 20 54 68 65 20 6c 61 73 74 20 70 72 /* The last pr
1120d 69 6f 72 20 65 6e 74 72 79 20 2a 2f 0a 20 20 61 ior entry */. a
1120e 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 ssert( p!=0 );.
1120f 20 69 66 28 20 70 2d 3e 6e 46 72 65 73 68 3d 3d if( p->nFresh==
11210 30 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 0 ){. struct
11211 52 6f 77 53 65 74 43 68 75 6e 6b 20 2a 70 4e 65 RowSetChunk *pNe
11212 77 3b 0a 20 20 20 20 70 4e 65 77 20 3d 20 73 71 w;. pNew = sq
11213 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 lite3DbMallocRaw
11214 28 70 2d 3e 64 62 2c 20 73 69 7a 65 6f 66 28 2a (p->db, sizeof(*
11215 70 4e 65 77 29 29 3b 0a 20 20 20 20 69 66 28 20 pNew));. if(
11216 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20 pNew==0 ){.
11217 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 return;. }.
11218 20 20 20 70 4e 65 77 2d 3e 70 4e 65 78 74 43 68 pNew->pNextCh
11219 75 6e 6b 20 3d 20 70 2d 3e 70 43 68 75 6e 6b 3b unk = p->pChunk;
1121a 0a 20 20 20 20 70 2d 3e 70 43 68 75 6e 6b 20 3d . p->pChunk =
1121b 20 70 4e 65 77 3b 0a 20 20 20 20 70 2d 3e 70 46 pNew;. p->pF
1121c 72 65 73 68 20 3d 20 70 4e 65 77 2d 3e 61 45 6e resh = pNew->aEn
1121d 74 72 79 3b 0a 20 20 20 20 70 2d 3e 6e 46 72 65 try;. p->nFre
1121e 73 68 20 3d 20 52 4f 57 53 45 54 5f 45 4e 54 52 sh = ROWSET_ENTR
1121f 59 5f 50 45 52 5f 43 48 55 4e 4b 3b 0a 20 20 7d Y_PER_CHUNK;. }
11220 0a 20 20 70 45 6e 74 72 79 20 3d 20 70 2d 3e 70 . pEntry = p->p
11221 46 72 65 73 68 2b 2b 3b 0a 20 20 70 2d 3e 6e 46 Fresh++;. p->nF
11222 72 65 73 68 2d 2d 3b 0a 20 20 70 45 6e 74 72 79 resh--;. pEntry
11223 2d 3e 76 20 3d 20 72 6f 77 69 64 3b 0a 20 20 70 ->v = rowid;. p
11224 45 6e 74 72 79 2d 3e 70 52 69 67 68 74 20 3d 20 Entry->pRight =
11225 30 3b 0a 20 20 70 4c 61 73 74 20 3d 20 70 2d 3e 0;. pLast = p->
11226 70 4c 61 73 74 3b 0a 20 20 69 66 28 20 70 4c 61 pLast;. if( pLa
11227 73 74 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d st ){. if( p-
11228 3e 69 73 53 6f 72 74 65 64 20 26 26 20 72 6f 77 >isSorted && row
11229 69 64 3c 3d 70 4c 61 73 74 2d 3e 76 20 29 7b 0a id<=pLast->v ){.
1122a 20 20 20 20 20 20 70 2d 3e 69 73 53 6f 72 74 65 p->isSorte
1122b 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 d = 0;. }.
1122c 20 70 4c 61 73 74 2d 3e 70 52 69 67 68 74 20 3d pLast->pRight =
1122d 20 70 45 6e 74 72 79 3b 0a 20 20 7d 65 6c 73 65 pEntry;. }else
1122e 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d {. assert( p-
1122f 3e 70 45 6e 74 72 79 3d 3d 30 20 29 3b 20 2f 2a >pEntry==0 ); /*
11230 20 46 69 72 65 73 20 69 66 20 49 4e 53 45 52 54 Fires if INSERT
11231 20 61 66 74 65 72 20 53 4d 41 4c 4c 45 53 54 20 after SMALLEST
11232 2a 2f 0a 20 20 20 20 70 2d 3e 70 45 6e 74 72 79 */. p->pEntry
11233 20 3d 20 70 45 6e 74 72 79 3b 0a 20 20 7d 0a 20 = pEntry;. }.
11234 20 70 2d 3e 70 4c 61 73 74 20 3d 20 70 45 6e 74 p->pLast = pEnt
11235 72 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 72 ry;.}../*.** Mer
11236 67 65 20 74 77 6f 20 6c 69 73 74 73 20 6f 66 20 ge two lists of
11237 52 6f 77 53 65 74 45 6e 74 72 79 20 6f 62 6a 65 RowSetEntry obje
11238 63 74 73 2e 20 20 52 65 6d 6f 76 65 20 64 75 70 cts. Remove dup
11239 6c 69 63 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 licates..**.** T
1123a 68 65 20 69 6e 70 75 74 20 6c 69 73 74 73 20 61 he input lists a
1123b 72 65 20 63 6f 6e 6e 65 63 74 65 64 20 76 69 61 re connected via
1123c 20 70 52 69 67 68 74 20 70 6f 69 6e 74 65 72 73 pRight pointers
1123d 20 61 6e 64 20 61 72 65 20 0a 2a 2a 20 61 73 73 and are .** ass
1123e 75 6d 65 64 20 74 6f 20 65 61 63 68 20 61 6c 72 umed to each alr
1123f 65 61 64 79 20 62 65 20 69 6e 20 73 6f 72 74 65 eady be in sorte
11240 64 20 6f 72 64 65 72 2e 0a 2a 2f 0a 73 74 61 74 d order..*/.stat
11241 69 63 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 ic struct RowSet
11242 45 6e 74 72 79 20 2a 72 6f 77 53 65 74 4d 65 72 Entry *rowSetMer
11243 67 65 28 0a 20 20 73 74 72 75 63 74 20 52 6f 77 ge(. struct Row
11244 53 65 74 45 6e 74 72 79 20 2a 70 41 2c 20 20 20 SetEntry *pA,
11245 20 2f 2a 20 46 69 72 73 74 20 73 6f 72 74 65 64 /* First sorted
11246 20 6c 69 73 74 20 74 6f 20 62 65 20 6d 65 72 67 list to be merg
11247 65 64 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 ed */. struct R
11248 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 42 20 20 owSetEntry *pB
11249 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 73 6f 72 /* Second sor
1124a 74 65 64 20 6c 69 73 74 20 74 6f 20 62 65 20 6d ted list to be m
1124b 65 72 67 65 64 20 2a 2f 0a 29 7b 0a 20 20 73 74 erged */.){. st
1124c 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
1124d 20 68 65 61 64 3b 0a 20 20 73 74 72 75 63 74 20 head;. struct
1124e 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 54 61 RowSetEntry *pTa
1124f 69 6c 3b 0a 0a 20 20 70 54 61 69 6c 20 3d 20 26 il;.. pTail = &
11250 68 65 61 64 3b 0a 20 20 77 68 69 6c 65 28 20 70 head;. while( p
11251 41 20 26 26 20 70 42 20 29 7b 0a 20 20 20 20 61 A && pB ){. a
11252 73 73 65 72 74 28 20 70 41 2d 3e 70 52 69 67 68 ssert( pA->pRigh
11253 74 3d 3d 30 20 7c 7c 20 70 41 2d 3e 76 3c 3d 70 t==0 || pA->v<=p
11254 41 2d 3e 70 52 69 67 68 74 2d 3e 76 20 29 3b 0a A->pRight->v );.
11255 20 20 20 20 61 73 73 65 72 74 28 20 70 42 2d 3e assert( pB->
11256 70 52 69 67 68 74 3d 3d 30 20 7c 7c 20 70 42 2d pRight==0 || pB-
11257 3e 76 3c 3d 70 42 2d 3e 70 52 69 67 68 74 2d 3e >v<=pB->pRight->
11258 76 20 29 3b 0a 20 20 20 20 69 66 28 20 70 41 2d v );. if( pA-
11259 3e 76 3c 70 42 2d 3e 76 20 29 7b 0a 20 20 20 20 >v<pB->v ){.
1125a 20 20 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 20 pTail->pRight
1125b 3d 20 70 41 3b 0a 20 20 20 20 20 20 70 41 20 3d = pA;. pA =
1125c 20 70 41 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 pA->pRight;.
1125d 20 20 20 70 54 61 69 6c 20 3d 20 70 54 61 69 6c pTail = pTail
1125e 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 7d 65 ->pRight;. }e
1125f 6c 73 65 20 69 66 28 20 70 42 2d 3e 76 3c 70 41 lse if( pB->v<pA
11260 2d 3e 76 20 29 7b 0a 20 20 20 20 20 20 70 54 61 ->v ){. pTa
11261 69 6c 2d 3e 70 52 69 67 68 74 20 3d 20 70 42 3b il->pRight = pB;
11262 0a 20 20 20 20 20 20 70 42 20 3d 20 70 42 2d 3e . pB = pB->
11263 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 70 54 pRight;. pT
11264 61 69 6c 20 3d 20 70 54 61 69 6c 2d 3e 70 52 69 ail = pTail->pRi
11265 67 68 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ght;. }else{.
11266 20 20 20 20 20 20 70 41 20 3d 20 70 41 2d 3e 70 pA = pA->p
11267 52 69 67 68 74 3b 0a 20 20 20 20 7d 0a 20 20 7d Right;. }. }
11268 0a 20 20 69 66 28 20 70 41 20 29 7b 0a 20 20 20 . if( pA ){.
11269 20 61 73 73 65 72 74 28 20 70 41 2d 3e 70 52 69 assert( pA->pRi
1126a 67 68 74 3d 3d 30 20 7c 7c 20 70 41 2d 3e 76 3c ght==0 || pA->v<
1126b 3d 70 41 2d 3e 70 52 69 67 68 74 2d 3e 76 20 29 =pA->pRight->v )
1126c 3b 0a 20 20 20 20 70 54 61 69 6c 2d 3e 70 52 69 ;. pTail->pRi
1126d 67 68 74 20 3d 20 70 41 3b 0a 20 20 7d 65 6c 73 ght = pA;. }els
1126e 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 e{. assert( p
1126f 42 3d 3d 30 20 7c 7c 20 70 42 2d 3e 70 52 69 67 B==0 || pB->pRig
11270 68 74 3d 3d 30 20 7c 7c 20 70 42 2d 3e 76 3c 3d ht==0 || pB->v<=
11271 70 42 2d 3e 70 52 69 67 68 74 2d 3e 76 20 29 3b pB->pRight->v );
11272 0a 20 20 20 20 70 54 61 69 6c 2d 3e 70 52 69 67 . pTail->pRig
11273 68 74 20 3d 20 70 42 3b 0a 20 20 7d 0a 20 20 72 ht = pB;. }. r
11274 65 74 75 72 6e 20 68 65 61 64 2e 70 52 69 67 68 eturn head.pRigh
11275 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 72 74 t;.}../*.** Sort
11276 20 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 6e all elements on
11277 20 74 68 65 20 70 45 6e 74 72 79 20 6c 69 73 74 the pEntry list
11278 20 6f 66 20 74 68 65 20 52 6f 77 53 65 74 20 69 of the RowSet i
11279 6e 74 6f 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 nto ascending or
1127a 64 65 72 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 20 der..*/ .static
1127b 76 6f 69 64 20 72 6f 77 53 65 74 53 6f 72 74 28 void rowSetSort(
1127c 52 6f 77 53 65 74 20 2a 70 29 7b 0a 20 20 75 6e RowSet *p){. un
1127d 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a 20 20 signed int i;.
1127e 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 struct RowSetEnt
1127f 72 79 20 2a 70 45 6e 74 72 79 3b 0a 20 20 73 74 ry *pEntry;. st
11280 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
11281 20 2a 61 42 75 63 6b 65 74 5b 34 30 5d 3b 0a 0a *aBucket[40];..
11282 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 73 53 assert( p->isS
11283 6f 72 74 65 64 3d 3d 30 20 29 3b 0a 20 20 6d 65 orted==0 );. me
11284 6d 73 65 74 28 61 42 75 63 6b 65 74 2c 20 30 2c mset(aBucket, 0,
11285 20 73 69 7a 65 6f 66 28 61 42 75 63 6b 65 74 29 sizeof(aBucket)
11286 29 3b 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 );. while( p->p
11287 45 6e 74 72 79 20 29 7b 0a 20 20 20 20 70 45 6e Entry ){. pEn
11288 74 72 79 20 3d 20 70 2d 3e 70 45 6e 74 72 79 3b try = p->pEntry;
11289 0a 20 20 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d . p->pEntry =
1128a 20 70 45 6e 74 72 79 2d 3e 70 52 69 67 68 74 3b pEntry->pRight;
1128b 0a 20 20 20 20 70 45 6e 74 72 79 2d 3e 70 52 69 . pEntry->pRi
1128c 67 68 74 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 ght = 0;. for
1128d 28 69 3d 30 3b 20 61 42 75 63 6b 65 74 5b 69 5d (i=0; aBucket[i]
1128e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 45 ; i++){. pE
1128f 6e 74 72 79 20 3d 20 72 6f 77 53 65 74 4d 65 72 ntry = rowSetMer
11290 67 65 28 61 42 75 63 6b 65 74 5b 69 5d 2c 20 70 ge(aBucket[i], p
11291 45 6e 74 72 79 29 3b 0a 20 20 20 20 20 20 61 42 Entry);. aB
11292 75 63 6b 65 74 5b 69 5d 20 3d 20 30 3b 0a 20 20 ucket[i] = 0;.
11293 20 20 7d 0a 20 20 20 20 61 42 75 63 6b 65 74 5b }. aBucket[
11294 69 5d 20 3d 20 70 45 6e 74 72 79 3b 0a 20 20 7d i] = pEntry;. }
11295 0a 20 20 70 45 6e 74 72 79 20 3d 20 30 3b 0a 20 . pEntry = 0;.
11296 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65 for(i=0; i<size
11297 6f 66 28 61 42 75 63 6b 65 74 29 2f 73 69 7a 65 of(aBucket)/size
11298 6f 66 28 61 42 75 63 6b 65 74 5b 30 5d 29 3b 20 of(aBucket[0]);
11299 69 2b 2b 29 7b 0a 20 20 20 20 70 45 6e 74 72 79 i++){. pEntry
1129a 20 3d 20 72 6f 77 53 65 74 4d 65 72 67 65 28 70 = rowSetMerge(p
1129b 45 6e 74 72 79 2c 20 61 42 75 63 6b 65 74 5b 69 Entry, aBucket[i
1129c 5d 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 45 6e ]);. }. p->pEn
1129d 74 72 79 20 3d 20 70 45 6e 74 72 79 3b 0a 20 20 try = pEntry;.
1129e 70 2d 3e 70 4c 61 73 74 20 3d 20 30 3b 0a 20 20 p->pLast = 0;.
1129f 70 2d 3e 69 73 53 6f 72 74 65 64 20 3d 20 31 3b p->isSorted = 1;
112a0 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 .}.../*.** The i
112a1 6e 70 75 74 2c 20 70 49 6e 2c 20 69 73 20 61 20 nput, pIn, is a
112a2 62 69 6e 61 72 79 20 74 72 65 65 20 28 6f 72 20 binary tree (or
112a3 73 75 62 74 72 65 65 29 20 6f 66 20 52 6f 77 53 subtree) of RowS
112a4 65 74 45 6e 74 72 79 20 6f 62 6a 65 63 74 73 2e etEntry objects.
112a5 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 69 73 .** Convert this
112a6 20 74 72 65 65 20 69 6e 74 6f 20 61 20 6c 69 6e tree into a lin
112a7 6b 65 64 20 6c 69 73 74 20 63 6f 6e 6e 65 63 74 ked list connect
112a8 65 64 20 62 79 20 74 68 65 20 70 52 69 67 68 74 ed by the pRight
112a9 20 70 6f 69 6e 74 65 72 73 0a 2a 2a 20 61 6e 64 pointers.** and
112aa 20 72 65 74 75 72 6e 20 70 6f 69 6e 74 65 72 73 return pointers
112ab 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 61 6e to the first an
112ac 64 20 6c 61 73 74 20 65 6c 65 6d 65 6e 74 73 20 d last elements
112ad 6f 66 20 74 68 65 20 6e 65 77 20 6c 69 73 74 2e of the new list.
112ae 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
112af 72 6f 77 53 65 74 54 72 65 65 54 6f 4c 69 73 74 rowSetTreeToList
112b0 28 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 (. struct RowSe
112b1 74 45 6e 74 72 79 20 2a 70 49 6e 2c 20 20 20 20 tEntry *pIn,
112b2 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 6f 66 20 /* Root of
112b3 74 68 65 20 69 6e 70 75 74 20 74 72 65 65 20 2a the input tree *
112b4 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 /. struct RowSe
112b5 74 45 6e 74 72 79 20 2a 2a 70 70 46 69 72 73 74 tEntry **ppFirst
112b6 2c 20 20 20 20 2f 2a 20 57 72 69 74 65 20 68 65 , /* Write he
112b7 61 64 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74 ad of the output
112b8 20 6c 69 73 74 20 68 65 72 65 20 2a 2f 0a 20 20 list here */.
112b9 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 struct RowSetEnt
112ba 72 79 20 2a 2a 70 70 4c 61 73 74 20 20 20 20 20 ry **ppLast
112bb 20 2f 2a 20 57 72 69 74 65 20 74 61 69 6c 20 6f /* Write tail o
112bc 66 20 74 68 65 20 6f 75 74 70 75 74 20 6c 69 73 f the output lis
112bd 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 61 t here */.){. a
112be 73 73 65 72 74 28 20 70 49 6e 21 3d 30 20 29 3b ssert( pIn!=0 );
112bf 0a 20 20 69 66 28 20 70 49 6e 2d 3e 70 4c 65 66 . if( pIn->pLef
112c0 74 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 t ){. struct
112c1 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 3b 0a RowSetEntry *p;.
112c2 20 20 20 20 72 6f 77 53 65 74 54 72 65 65 54 6f rowSetTreeTo
112c3 4c 69 73 74 28 70 49 6e 2d 3e 70 4c 65 66 74 2c List(pIn->pLeft,
112c4 20 70 70 46 69 72 73 74 2c 20 26 70 29 3b 0a 20 ppFirst, &p);.
112c5 20 20 20 70 2d 3e 70 52 69 67 68 74 20 3d 20 70 p->pRight = p
112c6 49 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 In;. }else{.
112c7 20 2a 70 70 46 69 72 73 74 20 3d 20 70 49 6e 3b *ppFirst = pIn;
112c8 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 6e 2d 3e . }. if( pIn->
112c9 70 52 69 67 68 74 20 29 7b 0a 20 20 20 20 72 6f pRight ){. ro
112ca 77 53 65 74 54 72 65 65 54 6f 4c 69 73 74 28 70 wSetTreeToList(p
112cb 49 6e 2d 3e 70 52 69 67 68 74 2c 20 26 70 49 6e In->pRight, &pIn
112cc 2d 3e 70 52 69 67 68 74 2c 20 70 70 4c 61 73 74 ->pRight, ppLast
112cd 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
112ce 2a 70 70 4c 61 73 74 20 3d 20 70 49 6e 3b 0a 20 *ppLast = pIn;.
112cf 20 7d 0a 20 20 61 73 73 65 72 74 28 20 28 2a 70 }. assert( (*p
112d0 70 4c 61 73 74 29 2d 3e 70 52 69 67 68 74 3d 3d pLast)->pRight==
112d1 30 20 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 0 );.}.../*.** C
112d2 6f 6e 76 65 72 74 20 61 20 73 6f 72 74 65 64 20 onvert a sorted
112d3 6c 69 73 74 20 6f 66 20 65 6c 65 6d 65 6e 74 73 list of elements
112d4 20 28 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 70 (connected by p
112d5 52 69 67 68 74 29 20 69 6e 74 6f 20 61 20 62 69 Right) into a bi
112d6 6e 61 72 79 0a 2a 2a 20 74 72 65 65 20 77 69 74 nary.** tree wit
112d7 68 20 64 65 70 74 68 20 6f 66 20 69 44 65 70 74 h depth of iDept
112d8 68 2e 20 20 41 20 64 65 70 74 68 20 6f 66 20 31 h. A depth of 1
112d9 20 6d 65 61 6e 73 20 74 68 65 20 74 72 65 65 20 means the tree
112da 63 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e 67 6c contains a singl
112db 65 0a 2a 2a 20 6e 6f 64 65 20 74 61 6b 65 6e 20 e.** node taken
112dc 66 72 6f 6d 20 74 68 65 20 68 65 61 64 20 6f 66 from the head of
112dd 20 2a 70 70 4c 69 73 74 2e 20 20 41 20 64 65 70 *ppList. A dep
112de 74 68 20 6f 66 20 32 20 6d 65 61 6e 73 20 61 20 th of 2 means a
112df 74 72 65 65 20 77 69 74 68 0a 2a 2a 20 74 68 72 tree with.** thr
112e0 65 65 20 6e 6f 64 65 73 2e 20 20 41 6e 64 20 73 ee nodes. And s
112e1 6f 20 66 6f 72 74 68 2e 0a 2a 2a 0a 2a 2a 20 55 o forth..**.** U
112e2 73 65 20 61 73 20 6d 61 6e 79 20 65 6e 74 72 69 se as many entri
112e3 65 73 20 66 72 6f 6d 20 74 68 65 20 69 6e 70 75 es from the inpu
112e4 74 20 6c 69 73 74 20 61 73 20 72 65 71 75 69 72 t list as requir
112e5 65 64 20 61 6e 64 20 75 70 64 61 74 65 20 74 68 ed and update th
112e6 65 0a 2a 2a 20 2a 70 70 4c 69 73 74 20 74 6f 20 e.** *ppList to
112e7 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 75 6e 75 point to the unu
112e8 73 65 64 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 sed elements of
112e9 74 68 65 20 6c 69 73 74 2e 20 20 49 66 20 74 68 the list. If th
112ea 65 20 69 6e 70 75 74 0a 2a 2a 20 6c 69 73 74 20 e input.** list
112eb 63 6f 6e 74 61 69 6e 73 20 74 6f 6f 20 66 65 77 contains too few
112ec 20 65 6c 65 6d 65 6e 74 73 2c 20 74 68 65 6e 20 elements, then
112ed 63 6f 6e 73 74 72 75 63 74 20 61 6e 20 69 6e 63 construct an inc
112ee 6f 6d 70 6c 65 74 65 20 74 72 65 65 0a 2a 2a 20 omplete tree.**
112ef 61 6e 64 20 6c 65 61 76 65 20 2a 70 70 4c 69 73 and leave *ppLis
112f0 74 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a t set to NULL..*
112f1 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f *.** Return a po
112f2 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72 6f 6f inter to the roo
112f3 74 20 6f 66 20 74 68 65 20 63 6f 6e 73 74 72 75 t of the constru
112f4 63 74 65 64 20 62 69 6e 61 72 79 20 74 72 65 65 cted binary tree
112f5 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 ..*/.static stru
112f6 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a ct RowSetEntry *
112f7 72 6f 77 53 65 74 4e 44 65 65 70 54 72 65 65 28 rowSetNDeepTree(
112f8 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 . struct RowSet
112f9 45 6e 74 72 79 20 2a 2a 70 70 4c 69 73 74 2c 0a Entry **ppList,.
112fa 20 20 69 6e 74 20 69 44 65 70 74 68 0a 29 7b 0a int iDepth.){.
112fb 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 struct RowSetE
112fc 6e 74 72 79 20 2a 70 3b 20 20 20 20 20 20 20 20 ntry *p;
112fd 20 2f 2a 20 52 6f 6f 74 20 6f 66 20 74 68 65 20 /* Root of the
112fe 6e 65 77 20 74 72 65 65 20 2a 2f 0a 20 20 73 74 new tree */. st
112ff 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
11300 20 2a 70 4c 65 66 74 3b 20 20 20 20 20 2f 2a 20 *pLeft; /*
11301 4c 65 66 74 20 73 75 62 74 72 65 65 20 2a 2f 0a Left subtree */.
11302 20 20 69 66 28 20 2a 70 70 4c 69 73 74 3d 3d 30 if( *ppList==0
11303 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 ){. return 0
11304 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 44 65 70 ;. }. if( iDep
11305 74 68 3d 3d 31 20 29 7b 0a 20 20 20 20 70 20 3d th==1 ){. p =
11306 20 2a 70 70 4c 69 73 74 3b 0a 20 20 20 20 2a 70 *ppList;. *p
11307 70 4c 69 73 74 20 3d 20 70 2d 3e 70 52 69 67 68 pList = p->pRigh
11308 74 3b 0a 20 20 20 20 70 2d 3e 70 4c 65 66 74 20 t;. p->pLeft
11309 3d 20 70 2d 3e 70 52 69 67 68 74 20 3d 20 30 3b = p->pRight = 0;
1130a 0a 20 20 20 20 72 65 74 75 72 6e 20 70 3b 0a 20 . return p;.
1130b 20 7d 0a 20 20 70 4c 65 66 74 20 3d 20 72 6f 77 }. pLeft = row
1130c 53 65 74 4e 44 65 65 70 54 72 65 65 28 70 70 4c SetNDeepTree(ppL
1130d 69 73 74 2c 20 69 44 65 70 74 68 2d 31 29 3b 0a ist, iDepth-1);.
1130e 20 20 70 20 3d 20 2a 70 70 4c 69 73 74 3b 0a 20 p = *ppList;.
1130f 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20 if( p==0 ){.
11310 20 72 65 74 75 72 6e 20 70 4c 65 66 74 3b 0a 20 return pLeft;.
11311 20 7d 0a 20 20 70 2d 3e 70 4c 65 66 74 20 3d 20 }. p->pLeft =
11312 70 4c 65 66 74 3b 0a 20 20 2a 70 70 4c 69 73 74 pLeft;. *ppList
11313 20 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20 = p->pRight;.
11314 70 2d 3e 70 52 69 67 68 74 20 3d 20 72 6f 77 53 p->pRight = rowS
11315 65 74 4e 44 65 65 70 54 72 65 65 28 70 70 4c 69 etNDeepTree(ppLi
11316 73 74 2c 20 69 44 65 70 74 68 2d 31 29 3b 0a 20 st, iDepth-1);.
11317 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a return p;.}../*
11318 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 73 6f .** Convert a so
11319 72 74 65 64 20 6c 69 73 74 20 6f 66 20 65 6c 65 rted list of ele
1131a 6d 65 6e 74 73 20 69 6e 74 6f 20 61 20 62 69 6e ments into a bin
1131b 61 72 79 20 74 72 65 65 2e 20 4d 61 6b 65 20 74 ary tree. Make t
1131c 68 65 20 74 72 65 65 0a 2a 2a 20 61 73 20 64 65 he tree.** as de
1131d 65 70 20 61 73 20 69 74 20 6e 65 65 64 73 20 74 ep as it needs t
1131e 6f 20 62 65 20 69 6e 20 6f 72 64 65 72 20 74 6f o be in order to
1131f 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 65 6e 74 contain the ent
11320 69 72 65 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 ire list..*/.sta
11321 74 69 63 20 73 74 72 75 63 74 20 52 6f 77 53 65 tic struct RowSe
11322 74 45 6e 74 72 79 20 2a 72 6f 77 53 65 74 4c 69 tEntry *rowSetLi
11323 73 74 54 6f 54 72 65 65 28 73 74 72 75 63 74 20 stToTree(struct
11324 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4c 69 RowSetEntry *pLi
11325 73 74 29 7b 0a 20 20 69 6e 74 20 69 44 65 70 74 st){. int iDept
11326 68 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 h; /*
11327 44 65 70 74 68 20 6f 66 20 74 68 65 20 74 72 65 Depth of the tre
11328 65 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 73 74 e so far */. st
11329 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
1132a 20 2a 70 3b 20 20 20 20 20 20 20 2f 2a 20 43 75 *p; /* Cu
1132b 72 72 65 6e 74 20 74 72 65 65 20 72 6f 6f 74 20 rrent tree root
1132c 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 */. struct RowS
1132d 65 74 45 6e 74 72 79 20 2a 70 4c 65 66 74 3b 20 etEntry *pLeft;
1132e 20 20 2f 2a 20 4c 65 66 74 20 73 75 62 74 72 65 /* Left subtre
1132f 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 e */.. assert(
11330 70 4c 69 73 74 21 3d 30 20 29 3b 0a 20 20 70 20 pList!=0 );. p
11331 3d 20 70 4c 69 73 74 3b 0a 20 20 70 4c 69 73 74 = pList;. pList
11332 20 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20 = p->pRight;.
11333 70 2d 3e 70 4c 65 66 74 20 3d 20 70 2d 3e 70 52 p->pLeft = p->pR
11334 69 67 68 74 20 3d 20 30 3b 0a 20 20 66 6f 72 28 ight = 0;. for(
11335 69 44 65 70 74 68 3d 31 3b 20 70 4c 69 73 74 3b iDepth=1; pList;
11336 20 69 44 65 70 74 68 2b 2b 29 7b 0a 20 20 20 20 iDepth++){.
11337 70 4c 65 66 74 20 3d 20 70 3b 0a 20 20 20 20 70 pLeft = p;. p
11338 20 3d 20 70 4c 69 73 74 3b 0a 20 20 20 20 70 4c = pList;. pL
11339 69 73 74 20 3d 20 70 2d 3e 70 52 69 67 68 74 3b ist = p->pRight;
1133a 0a 20 20 20 20 70 2d 3e 70 4c 65 66 74 20 3d 20 . p->pLeft =
1133b 70 4c 65 66 74 3b 0a 20 20 20 20 70 2d 3e 70 52 pLeft;. p->pR
1133c 69 67 68 74 20 3d 20 72 6f 77 53 65 74 4e 44 65 ight = rowSetNDe
1133d 65 70 54 72 65 65 28 26 70 4c 69 73 74 2c 20 69 epTree(&pList, i
1133e 44 65 70 74 68 29 3b 0a 20 20 7d 0a 20 20 72 65 Depth);. }. re
1133f 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn p;.}../*.**
11340 20 43 6f 6e 76 65 72 74 20 74 68 65 20 6c 69 73 Convert the lis
11341 74 20 69 6e 20 70 2d 3e 70 45 6e 74 72 79 20 69 t in p->pEntry i
11342 6e 74 6f 20 61 20 73 6f 72 74 65 64 20 6c 69 73 nto a sorted lis
11343 74 20 69 66 20 69 74 20 69 73 20 6e 6f 74 0a 2a t if it is not.*
11344 2a 20 73 6f 72 74 65 64 20 61 6c 72 65 61 64 79 * sorted already
11345 2e 20 20 49 66 20 74 68 65 72 65 20 69 73 20 61 . If there is a
11346 20 62 69 6e 61 72 79 20 74 72 65 65 20 6f 6e 20 binary tree on
11347 70 2d 3e 70 54 72 65 65 2c 20 74 68 65 6e 0a 2a p->pTree, then.*
11348 2a 20 63 6f 6e 76 65 72 74 20 69 74 20 69 6e 74 * convert it int
11349 6f 20 61 20 6c 69 73 74 20 74 6f 6f 20 61 6e 64 o a list too and
1134a 20 6d 65 72 67 65 20 69 74 20 69 6e 74 6f 20 74 merge it into t
1134b 68 65 20 70 2d 3e 70 45 6e 74 72 79 20 6c 69 73 he p->pEntry lis
1134c 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 t..*/.static voi
1134d 64 20 72 6f 77 53 65 74 54 6f 4c 69 73 74 28 52 d rowSetToList(R
1134e 6f 77 53 65 74 20 2a 70 29 7b 0a 20 20 69 66 28 owSet *p){. if(
1134f 20 21 70 2d 3e 69 73 53 6f 72 74 65 64 20 29 7b !p->isSorted ){
11350 0a 20 20 20 20 72 6f 77 53 65 74 53 6f 72 74 28 . rowSetSort(
11351 70 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d p);. }. if( p-
11352 3e 70 54 72 65 65 20 29 7b 0a 20 20 20 20 73 74 >pTree ){. st
11353 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
11354 20 2a 70 48 65 61 64 2c 20 2a 70 54 61 69 6c 3b *pHead, *pTail;
11355 0a 20 20 20 20 72 6f 77 53 65 74 54 72 65 65 54 . rowSetTreeT
11356 6f 4c 69 73 74 28 70 2d 3e 70 54 72 65 65 2c 20 oList(p->pTree,
11357 26 70 48 65 61 64 2c 20 26 70 54 61 69 6c 29 3b &pHead, &pTail);
11358 0a 20 20 20 20 70 2d 3e 70 54 72 65 65 20 3d 20 . p->pTree =
11359 30 3b 0a 20 20 20 20 70 2d 3e 70 45 6e 74 72 79 0;. p->pEntry
1135a 20 3d 20 72 6f 77 53 65 74 4d 65 72 67 65 28 70 = rowSetMerge(p
1135b 2d 3e 70 45 6e 74 72 79 2c 20 70 48 65 61 64 29 ->pEntry, pHead)
1135c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 ;. }.}../*.** E
1135d 78 74 72 61 63 74 20 74 68 65 20 73 6d 61 6c 6c xtract the small
1135e 65 73 74 20 65 6c 65 6d 65 6e 74 20 66 72 6f 6d est element from
1135f 20 74 68 65 20 52 6f 77 53 65 74 2e 0a 2a 2a 20 the RowSet..**
11360 57 72 69 74 65 20 74 68 65 20 65 6c 65 6d 65 6e Write the elemen
11361 74 20 69 6e 74 6f 20 2a 70 52 6f 77 69 64 2e 20 t into *pRowid.
11362 20 52 65 74 75 72 6e 20 31 20 6f 6e 20 73 75 63 Return 1 on suc
11363 63 65 73 73 2e 20 20 52 65 74 75 72 6e 0a 2a 2a cess. Return.**
11364 20 30 20 69 66 20 74 68 65 20 52 6f 77 53 65 74 0 if the RowSet
11365 20 69 73 20 61 6c 72 65 61 64 79 20 65 6d 70 74 is already empt
11366 79 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 74 y..**.** After t
11367 68 69 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20 his routine has
11368 62 65 65 6e 20 63 61 6c 6c 65 64 2c 20 74 68 65 been called, the
11369 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 49 6e sqlite3RowSetIn
1136a 73 65 72 74 28 29 0a 2a 2a 20 72 6f 75 74 69 6e sert().** routin
1136b 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 63 61 6c e may not be cal
1136c 6c 65 64 20 61 67 61 69 6e 2e 20 20 0a 2a 2f 0a led again. .*/.
1136d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1136e 6e 74 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 nt sqlite3RowSet
1136f 4e 65 78 74 28 52 6f 77 53 65 74 20 2a 70 2c 20 Next(RowSet *p,
11370 69 36 34 20 2a 70 52 6f 77 69 64 29 7b 0a 20 20 i64 *pRowid){.
11371 72 6f 77 53 65 74 54 6f 4c 69 73 74 28 70 29 3b rowSetToList(p);
11372 0a 20 20 69 66 28 20 70 2d 3e 70 45 6e 74 72 79 . if( p->pEntry
11373 20 29 7b 0a 20 20 20 20 2a 70 52 6f 77 69 64 20 ){. *pRowid
11374 3d 20 70 2d 3e 70 45 6e 74 72 79 2d 3e 76 3b 0a = p->pEntry->v;.
11375 20 20 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 p->pEntry =
11376 70 2d 3e 70 45 6e 74 72 79 2d 3e 70 52 69 67 68 p->pEntry->pRigh
11377 74 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 45 t;. if( p->pE
11378 6e 74 72 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 ntry==0 ){.
11379 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 43 6c sqlite3RowSetCl
1137a 65 61 72 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 ear(p);. }.
1137b 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65 return 1;. }e
1137c 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 lse{. return
1137d 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 0;. }.}../*.**
1137e 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 Check to see if
1137f 65 6c 65 6d 65 6e 74 20 69 52 6f 77 69 64 20 77 element iRowid w
11380 61 73 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f as inserted into
11381 20 74 68 65 20 74 68 65 20 72 6f 77 73 65 74 20 the the rowset
11382 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 61 6e as.** part of an
11383 79 20 69 6e 73 65 72 74 20 62 61 74 63 68 20 70 y insert batch p
11384 72 69 6f 72 20 74 6f 20 69 42 61 74 63 68 2e 20 rior to iBatch.
11385 20 52 65 74 75 72 6e 20 31 20 6f 72 20 30 2e 0a Return 1 or 0..
11386 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
11387 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 6f 77 E int sqlite3Row
11388 53 65 74 54 65 73 74 28 52 6f 77 53 65 74 20 2a SetTest(RowSet *
11389 70 52 6f 77 53 65 74 2c 20 75 38 20 69 42 61 74 pRowSet, u8 iBat
1138a 63 68 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 ch, sqlite3_int6
1138b 34 20 69 52 6f 77 69 64 29 7b 0a 20 20 73 74 72 4 iRowid){. str
1138c 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 uct RowSetEntry
1138d 2a 70 3b 0a 20 20 69 66 28 20 69 42 61 74 63 68 *p;. if( iBatch
1138e 21 3d 70 52 6f 77 53 65 74 2d 3e 69 42 61 74 63 !=pRowSet->iBatc
1138f 68 20 29 7b 0a 20 20 20 20 69 66 28 20 70 52 6f h ){. if( pRo
11390 77 53 65 74 2d 3e 70 45 6e 74 72 79 20 29 7b 0a wSet->pEntry ){.
11391 20 20 20 20 20 20 72 6f 77 53 65 74 54 6f 4c 69 rowSetToLi
11392 73 74 28 70 52 6f 77 53 65 74 29 3b 0a 20 20 20 st(pRowSet);.
11393 20 20 20 70 52 6f 77 53 65 74 2d 3e 70 54 72 65 pRowSet->pTre
11394 65 20 3d 20 72 6f 77 53 65 74 4c 69 73 74 54 6f e = rowSetListTo
11395 54 72 65 65 28 70 52 6f 77 53 65 74 2d 3e 70 45 Tree(pRowSet->pE
11396 6e 74 72 79 29 3b 0a 20 20 20 20 20 20 70 52 6f ntry);. pRo
11397 77 53 65 74 2d 3e 70 45 6e 74 72 79 20 3d 20 30 wSet->pEntry = 0
11398 3b 0a 20 20 20 20 20 20 70 52 6f 77 53 65 74 2d ;. pRowSet-
11399 3e 70 4c 61 73 74 20 3d 20 30 3b 0a 20 20 20 20 >pLast = 0;.
1139a 7d 0a 20 20 20 20 70 52 6f 77 53 65 74 2d 3e 69 }. pRowSet->i
1139b 42 61 74 63 68 20 3d 20 69 42 61 74 63 68 3b 0a Batch = iBatch;.
1139c 20 20 7d 0a 20 20 70 20 3d 20 70 52 6f 77 53 65 }. p = pRowSe
1139d 74 2d 3e 70 54 72 65 65 3b 0a 20 20 77 68 69 6c t->pTree;. whil
1139e 65 28 20 70 20 29 7b 0a 20 20 20 20 69 66 28 20 e( p ){. if(
1139f 70 2d 3e 76 3c 69 52 6f 77 69 64 20 29 7b 0a 20 p->v<iRowid ){.
113a0 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 52 69 67 p = p->pRig
113a1 68 74 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 ht;. }else if
113a2 28 20 70 2d 3e 76 3e 69 52 6f 77 69 64 20 29 7b ( p->v>iRowid ){
113a3 0a 20 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 4c . p = p->pL
113a4 65 66 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a eft;. }else{.
113a5 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a return 1;.
113a6 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
113a7 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a rn 0;.}../******
113a8 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
113a9 72 6f 77 73 65 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a rowset.c *******
113aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
113ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
113ac 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
113ad 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
113ae 69 6c 65 20 70 61 67 65 72 2e 63 20 2a 2a 2a 2a ile pager.c ****
113af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
113b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
113b1 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
113b2 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 001 September 15
113b3 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
113b4 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
113b5 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
113b6 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
113b7 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
113b8 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
113b9 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
113ba 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
113bb 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
113bc 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
113bd 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
113be 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
113bf 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
113c0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
113c1 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
113c2 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
113c3 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
113c4 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
113c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
113c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
113c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
113c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
113c9 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 69 ** This is the i
113ca 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
113cb 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 20 the page cache
113cc 73 75 62 73 79 73 74 65 6d 20 6f 72 20 22 70 61 subsystem or "pa
113cd 67 65 72 22 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 ger"..** .** The
113ce 20 70 61 67 65 72 20 69 73 20 75 73 65 64 20 74 pager is used t
113cf 6f 20 61 63 63 65 73 73 20 61 20 64 61 74 61 62 o access a datab
113d0 61 73 65 20 64 69 73 6b 20 66 69 6c 65 2e 20 20 ase disk file.
113d1 49 74 20 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a It implements.**
113d2 20 61 74 6f 6d 69 63 20 63 6f 6d 6d 69 74 20 61 atomic commit a
113d3 6e 64 20 72 6f 6c 6c 62 61 63 6b 20 74 68 72 6f nd rollback thro
113d4 75 67 68 20 74 68 65 20 75 73 65 20 6f 66 20 61 ugh the use of a
113d5 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68 journal file th
113d6 61 74 0a 2a 2a 20 69 73 20 73 65 70 61 72 61 74 at.** is separat
113d7 65 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 e from the datab
113d8 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65 20 70 ase file. The p
113d9 61 67 65 72 20 61 6c 73 6f 20 69 6d 70 6c 65 6d ager also implem
113da 65 6e 74 73 20 66 69 6c 65 0a 2a 2a 20 6c 6f 63 ents file.** loc
113db 6b 69 6e 67 20 74 6f 20 70 72 65 76 65 6e 74 20 king to prevent
113dc 74 77 6f 20 70 72 6f 63 65 73 73 65 73 20 66 72 two processes fr
113dd 6f 6d 20 77 72 69 74 69 6e 67 20 74 68 65 20 73 om writing the s
113de 61 6d 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 ame database.**
113df 66 69 6c 65 20 73 69 6d 75 6c 74 61 6e 65 6f 75 file simultaneou
113e0 73 6c 79 2c 20 6f 72 20 6f 6e 65 20 70 72 6f 63 sly, or one proc
113e1 65 73 73 20 66 72 6f 6d 20 72 65 61 64 69 6e 67 ess from reading
113e2 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77 68 the database wh
113e3 69 6c 65 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 69 ile.** another i
113e4 73 20 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a s writing..**.**
113e5 20 40 28 23 29 20 24 49 64 3a 20 70 61 67 65 72 @(#) $Id: pager
113e6 2e 63 2c 76 20 31 2e 36 32 39 20 32 30 30 39 2f .c,v 1.629 2009/
113e7 30 38 2f 31 30 20 31 37 3a 34 38 3a 35 37 20 64 08/10 17:48:57 d
113e8 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e rh Exp $.*/.#ifn
113e9 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
113ea 44 49 53 4b 49 4f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 DISKIO../*.** Ma
113eb 63 72 6f 73 20 66 6f 72 20 74 72 6f 75 62 6c 65 cros for trouble
113ec 73 68 6f 6f 74 69 6e 67 2e 20 20 4e 6f 72 6d 61 shooting. Norma
113ed 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 0a 2a lly turned off.*
113ee 2f 0a 23 69 66 20 30 0a 69 6e 74 20 73 71 6c 69 /.#if 0.int sqli
113ef 74 65 33 50 61 67 65 72 54 72 61 63 65 3d 31 3b te3PagerTrace=1;
113f0 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e 61 /* True to ena
113f1 62 6c 65 20 74 72 61 63 69 6e 67 20 2a 2f 0a 23 ble tracing */.#
113f2 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 44 65 define sqlite3De
113f3 62 75 67 50 72 69 6e 74 66 20 70 72 69 6e 74 66 bugPrintf printf
113f4 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 54 52 .#define PAGERTR
113f5 41 43 45 28 58 29 20 20 20 20 20 69 66 28 20 73 ACE(X) if( s
113f6 71 6c 69 74 65 33 50 61 67 65 72 54 72 61 63 65 qlite3PagerTrace
113f7 20 29 7b 20 73 71 6c 69 74 65 33 44 65 62 75 67 ){ sqlite3Debug
113f8 50 72 69 6e 74 66 20 58 3b 20 7d 0a 23 65 6c 73 Printf X; }.#els
113f9 65 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 54 e.#define PAGERT
113fa 52 41 43 45 28 58 29 0a 23 65 6e 64 69 66 0a 0a RACE(X).#endif..
113fb 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
113fc 69 6e 67 20 74 77 6f 20 6d 61 63 72 6f 73 20 61 ing two macros a
113fd 72 65 20 75 73 65 64 20 77 69 74 68 69 6e 20 74 re used within t
113fe 68 65 20 50 41 47 45 52 54 52 41 43 45 28 29 20 he PAGERTRACE()
113ff 6d 61 63 72 6f 73 20 61 62 6f 76 65 0a 2a 2a 20 macros above.**
11400 74 6f 20 70 72 69 6e 74 20 6f 75 74 20 66 69 6c to print out fil
11401 65 2d 64 65 73 63 72 69 70 74 6f 72 73 2e 20 0a e-descriptors. .
11402 2a 2a 0a 2a 2a 20 50 41 47 45 52 49 44 28 29 20 **.** PAGERID()
11403 74 61 6b 65 73 20 61 20 70 6f 69 6e 74 65 72 20 takes a pointer
11404 74 6f 20 61 20 50 61 67 65 72 20 73 74 72 75 63 to a Pager struc
11405 74 20 61 73 20 69 74 73 20 61 72 67 75 6d 65 6e t as its argumen
11406 74 2e 20 54 68 65 0a 2a 2a 20 61 73 73 6f 63 69 t. The.** associ
11407 61 74 65 64 20 66 69 6c 65 2d 64 65 73 63 72 69 ated file-descri
11408 70 74 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 ptor is returned
11409 2e 20 46 49 4c 45 48 41 4e 44 4c 45 49 44 28 29 . FILEHANDLEID()
1140a 20 74 61 6b 65 73 20 61 6e 20 73 71 6c 69 74 65 takes an sqlite
1140b 33 5f 66 69 6c 65 0a 2a 2a 20 73 74 72 75 63 74 3_file.** struct
1140c 20 61 73 20 69 74 73 20 61 72 67 75 6d 65 6e 74 as its argument
1140d 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 ..*/.#define PAG
1140e 45 52 49 44 28 70 29 20 28 28 69 6e 74 29 28 70 ERID(p) ((int)(p
1140f 2d 3e 66 64 29 29 0a 23 64 65 66 69 6e 65 20 46 ->fd)).#define F
11410 49 4c 45 48 41 4e 44 4c 45 49 44 28 66 64 29 20 ILEHANDLEID(fd)
11411 28 28 69 6e 74 29 66 64 29 0a 0a 2f 2a 0a 2a 2a ((int)fd)../*.**
11412 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20 The page cache
11413 61 73 20 61 20 77 68 6f 6c 65 20 69 73 20 61 6c as a whole is al
11414 77 61 79 73 20 69 6e 20 6f 6e 65 20 6f 66 20 74 ways in one of t
11415 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 he following.**
11416 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 states:.**.**
11417 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 20 20 20 PAGER_UNLOCK
11418 20 20 20 20 54 68 65 20 70 61 67 65 20 63 61 63 The page cac
11419 68 65 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e he is not curren
1141a 74 6c 79 20 72 65 61 64 69 6e 67 20 6f 72 20 0a tly reading or .
1141b 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
1141c 20 20 20 20 20 20 20 20 20 77 72 69 74 69 6e 67 writing
1141d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
1141e 6c 65 2e 20 20 54 68 65 72 65 20 69 73 20 6e 6f le. There is no
1141f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
11420 20 20 20 20 20 20 20 20 20 20 64 61 74 61 20 68 data h
11421 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 eld in memory.
11422 54 68 69 73 20 69 73 20 74 68 65 20 69 6e 69 74 This is the init
11423 69 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ial.**
11424 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74 61 sta
11425 74 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 te..**.** PAGE
11426 52 5f 53 48 41 52 45 44 20 20 20 20 20 20 20 20 R_SHARED
11427 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20 69 The page cache i
11428 73 20 72 65 61 64 69 6e 67 20 74 68 65 20 64 61 s reading the da
11429 74 61 62 61 73 65 2e 0a 2a 2a 20 20 20 20 20 20 tabase..**
1142a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1142b 20 57 72 69 74 69 6e 67 20 69 73 20 6e 6f 74 20 Writing is not
1142c 70 65 72 6d 69 74 74 65 64 2e 20 20 54 68 65 72 permitted. Ther
1142d 65 20 63 61 6e 20 62 65 0a 2a 2a 20 20 20 20 20 e can be.**
1142e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1142f 20 20 6d 75 6c 74 69 70 6c 65 20 72 65 61 64 65 multiple reade
11430 72 73 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 rs accessing the
11431 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 0a 2a same database.*
11432 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
11433 20 20 20 20 20 20 20 20 66 69 6c 65 20 61 74 20 file at
11434 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a 2a the same time..*
11435 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f 52 45 53 *.** PAGER_RES
11436 45 52 56 45 44 20 20 20 20 20 20 54 68 69 73 20 ERVED This
11437 70 72 6f 63 65 73 73 20 68 61 73 20 72 65 73 65 process has rese
11438 72 76 65 64 20 74 68 65 20 64 61 74 61 62 61 73 rved the databas
11439 65 20 66 6f 72 20 77 72 69 74 69 6e 67 0a 2a 2a e for writing.**
1143a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1143b 20 20 20 20 20 20 20 62 75 74 20 68 61 73 20 6e but has n
1143c 6f 74 20 79 65 74 20 6d 61 64 65 20 61 6e 79 20 ot yet made any
1143d 63 68 61 6e 67 65 73 2e 20 20 4f 6e 6c 79 20 6f changes. Only o
1143e 6e 65 20 70 72 6f 63 65 73 73 0a 2a 2a 20 20 20 ne process.**
1143f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11440 20 20 20 20 61 74 20 61 20 74 69 6d 65 20 63 61 at a time ca
11441 6e 20 72 65 73 65 72 76 65 20 74 68 65 20 64 61 n reserve the da
11442 74 61 62 61 73 65 2e 20 20 54 68 65 20 6f 72 69 tabase. The ori
11443 67 69 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 ginal.**
11444 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 d
11445 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 73 atabase file has
11446 20 6e 6f 74 20 62 65 65 6e 20 6d 6f 64 69 66 69 not been modifi
11447 65 64 20 73 6f 20 6f 74 68 65 72 0a 2a 2a 20 20 ed so other.**
11448 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11449 20 20 20 20 20 70 72 6f 63 65 73 73 65 73 20 6d processes m
1144a 61 79 20 73 74 69 6c 6c 20 62 65 20 72 65 61 64 ay still be read
1144b 69 6e 67 20 74 68 65 20 6f 6e 2d 64 69 73 6b 0a ing the on-disk.
1144c 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
1144d 20 20 20 20 20 20 20 20 20 64 61 74 61 62 61 73 databas
1144e 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 e file..**.**
1144f 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 PAGER_EXCLUSIVE
11450 20 20 20 20 54 68 65 20 70 61 67 65 20 63 61 63 The page cac
11451 68 65 20 69 73 20 77 72 69 74 69 6e 67 20 74 68 he is writing th
11452 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20 20 e database..**
11453 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11454 20 20 20 20 20 41 63 63 65 73 73 20 69 73 20 65 Access is e
11455 78 63 6c 75 73 69 76 65 2e 20 20 4e 6f 20 6f 74 xclusive. No ot
11456 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 6f 72 her processes or
11457 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
11458 20 20 20 20 20 20 20 20 20 20 74 68 72 65 61 64 thread
11459 73 20 63 61 6e 20 62 65 20 72 65 61 64 69 6e 67 s can be reading
1145a 20 6f 72 20 77 72 69 74 69 6e 67 20 77 68 69 6c or writing whil
1145b 65 20 6f 6e 65 0a 2a 2a 20 20 20 20 20 20 20 20 e one.**
1145c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
1145d 72 6f 63 65 73 73 20 69 73 20 77 72 69 74 69 6e rocess is writin
1145e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 52 g..**.** PAGER
1145f 5f 53 59 4e 43 45 44 20 20 20 20 20 20 20 20 54 _SYNCED T
11460 68 65 20 70 61 67 65 72 20 6d 6f 76 65 73 20 74 he pager moves t
11461 6f 20 74 68 69 73 20 73 74 61 74 65 20 66 72 6f o this state fro
11462 6d 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 m PAGER_EXCLUSIV
11463 45 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 E.**
11464 20 20 20 20 20 20 20 20 20 20 20 61 66 74 65 72 after
11465 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 all dirty pages
11466 20 68 61 76 65 20 62 65 65 6e 20 77 72 69 74 74 have been writt
11467 65 6e 20 74 6f 20 74 68 65 0a 2a 2a 20 20 20 20 en to the.**
11468 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11469 20 20 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 database file
1146a 20 61 6e 64 20 74 68 65 20 66 69 6c 65 20 68 61 and the file ha
1146b 73 20 62 65 65 6e 20 73 79 6e 63 65 64 20 74 6f s been synced to
1146c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
1146d 20 20 20 20 20 20 20 20 20 20 64 69 73 6b 2e 20 disk.
1146e 41 6c 6c 20 74 68 61 74 20 72 65 6d 61 69 6e 73 All that remains
1146f 20 74 6f 20 64 6f 20 69 73 20 74 6f 20 72 65 6d to do is to rem
11470 6f 76 65 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 ove or.**
11471 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11472 74 72 75 6e 63 61 74 65 20 74 68 65 20 6a 6f 75 truncate the jou
11473 72 6e 61 6c 20 66 69 6c 65 20 61 6e 64 20 74 68 rnal file and th
11474 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a e transaction .*
11475 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
11476 20 20 20 20 20 20 20 20 77 69 6c 6c 20 62 65 20 will be
11477 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a committed..**.**
11478 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20 The page cache
11479 63 6f 6d 65 73 20 75 70 20 69 6e 20 50 41 47 45 comes up in PAGE
1147a 52 5f 55 4e 4c 4f 43 4b 2e 20 20 54 68 65 20 66 R_UNLOCK. The f
1147b 69 72 73 74 20 74 69 6d 65 20 61 0a 2a 2a 20 73 irst time a.** s
1147c 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29 qlite3PagerGet()
1147d 20 6f 63 63 75 72 73 2c 20 74 68 65 20 73 74 61 occurs, the sta
1147e 74 65 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 74 te transitions t
1147f 6f 20 50 41 47 45 52 5f 53 48 41 52 45 44 2e 0a o PAGER_SHARED..
11480 2a 2a 20 41 66 74 65 72 20 61 6c 6c 20 70 61 67 ** After all pag
11481 65 73 20 68 61 76 65 20 62 65 65 6e 20 72 65 6c es have been rel
11482 65 61 73 65 64 20 75 73 69 6e 67 20 73 71 6c 69 eased using sqli
11483 74 65 5f 70 61 67 65 5f 75 6e 72 65 66 28 29 2c te_page_unref(),
11484 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 20 74 72 .** the state tr
11485 61 6e 73 69 74 69 6f 6e 73 20 62 61 63 6b 20 74 ansitions back t
11486 6f 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 2e 20 o PAGER_UNLOCK.
11487 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 0a The first time.
11488 2a 2a 20 74 68 61 74 20 73 71 6c 69 74 65 33 50 ** that sqlite3P
11489 61 67 65 72 57 72 69 74 65 28 29 20 69 73 20 63 agerWrite() is c
1148a 61 6c 6c 65 64 2c 20 74 68 65 20 73 74 61 74 65 alled, the state
1148b 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 74 6f 0a transitions to.
1148c 2a 2a 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 ** PAGER_RESERVE
1148d 44 2e 20 20 28 4e 6f 74 65 20 74 68 61 74 20 73 D. (Note that s
1148e 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
1148f 28 29 20 63 61 6e 20 6f 6e 6c 79 20 62 65 0a 2a () can only be.*
11490 2a 20 63 61 6c 6c 65 64 20 6f 6e 20 61 6e 20 6f * called on an o
11491 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 20 utstanding page
11492 77 68 69 63 68 20 6d 65 61 6e 73 20 74 68 61 74 which means that
11493 20 74 68 65 20 70 61 67 65 72 20 6d 75 73 74 0a the pager must.
11494 2a 2a 20 62 65 20 69 6e 20 50 41 47 45 52 5f 53 ** be in PAGER_S
11495 48 41 52 45 44 20 62 65 66 6f 72 65 20 69 74 20 HARED before it
11496 74 72 61 6e 73 69 74 69 6f 6e 73 20 74 6f 20 50 transitions to P
11497 41 47 45 52 5f 52 45 53 45 52 56 45 44 2e 29 0a AGER_RESERVED.).
11498 2a 2a 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 ** PAGER_RESERVE
11499 44 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 D means that the
1149a 72 65 20 69 73 20 61 6e 20 6f 70 65 6e 20 72 6f re is an open ro
1149b 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a llback journal..
1149c 2a 2a 20 54 68 65 20 74 72 61 6e 73 69 74 69 6f ** The transitio
1149d 6e 20 74 6f 20 50 41 47 45 52 5f 45 58 43 4c 55 n to PAGER_EXCLU
1149e 53 49 56 45 20 6f 63 63 75 72 73 20 62 65 66 6f SIVE occurs befo
1149f 72 65 20 61 6e 79 20 63 68 61 6e 67 65 73 0a 2a re any changes.*
114a0 2a 20 61 72 65 20 6d 61 64 65 20 74 6f 20 74 68 * are made to th
114a1 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c e database file,
114a2 20 74 68 6f 75 67 68 20 77 72 69 74 65 73 20 74 though writes t
114a3 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 0a 2a o the rollback.*
114a4 2a 20 6a 6f 75 72 6e 61 6c 20 6f 63 63 75 72 73 * journal occurs
114a5 20 77 69 74 68 20 6a 75 73 74 20 50 41 47 45 52 with just PAGER
114a6 5f 52 45 53 45 52 56 45 44 2e 20 20 41 66 74 65 _RESERVED. Afte
114a7 72 20 61 6e 20 73 71 6c 69 74 65 33 50 61 67 65 r an sqlite3Page
114a8 72 52 6f 6c 6c 62 61 63 6b 28 29 0a 2a 2a 20 6f rRollback().** o
114a9 72 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f r sqlite3PagerCo
114aa 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 2c 20 mmitPhaseTwo(),
114ab 74 68 65 20 73 74 61 74 65 20 63 61 6e 20 67 6f the state can go
114ac 20 62 61 63 6b 20 74 6f 20 50 41 47 45 52 5f 53 back to PAGER_S
114ad 48 41 52 45 44 2c 0a 2a 2a 20 6f 72 20 69 74 20 HARED,.** or it
114ae 63 61 6e 20 73 74 61 79 20 61 74 20 50 41 47 45 can stay at PAGE
114af 52 5f 45 58 43 4c 55 53 49 56 45 20 69 66 20 77 R_EXCLUSIVE if w
114b0 65 20 61 72 65 20 69 6e 20 65 78 63 6c 75 73 69 e are in exclusi
114b1 76 65 20 61 63 63 65 73 73 20 6d 6f 64 65 2e 0a ve access mode..
114b2 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 */.#define PAGER
114b3 5f 55 4e 4c 4f 43 4b 20 20 20 20 20 20 30 0a 23 _UNLOCK 0.#
114b4 64 65 66 69 6e 65 20 50 41 47 45 52 5f 53 48 41 define PAGER_SHA
114b5 52 45 44 20 20 20 20 20 20 31 20 20 20 2f 2a 20 RED 1 /*
114b6 73 61 6d 65 20 61 73 20 53 48 41 52 45 44 5f 4c same as SHARED_L
114b7 4f 43 4b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 OCK */.#define P
114b8 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 20 20 AGER_RESERVED
114b9 20 32 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 2 /* same as
114ba 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 2a 2f RESERVED_LOCK */
114bb 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 45 .#define PAGER_E
114bc 58 43 4c 55 53 49 56 45 20 20 20 34 20 20 20 2f XCLUSIVE 4 /
114bd 2a 20 73 61 6d 65 20 61 73 20 45 58 43 4c 55 53 * same as EXCLUS
114be 49 56 45 5f 4c 4f 43 4b 20 2a 2f 0a 23 64 65 66 IVE_LOCK */.#def
114bf 69 6e 65 20 50 41 47 45 52 5f 53 59 4e 43 45 44 ine PAGER_SYNCED
114c0 20 20 20 20 20 20 35 0a 0a 2f 2a 0a 2a 2a 20 41 5../*.** A
114c1 20 6d 61 63 72 6f 20 75 73 65 64 20 66 6f 72 20 macro used for
114c2 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 63 6f 64 invoking the cod
114c3 65 63 20 69 66 20 74 68 65 72 65 20 69 73 20 6f ec if there is o
114c4 6e 65 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c ne.*/.#ifdef SQL
114c5 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 23 20 ITE_HAS_CODEC.#
114c6 64 65 66 69 6e 65 20 43 4f 44 45 43 31 28 50 2c define CODEC1(P,
114c7 44 2c 4e 2c 58 2c 45 29 20 5c 0a 20 20 20 20 69 D,N,X,E) \. i
114c8 66 28 20 50 2d 3e 78 43 6f 64 65 63 20 26 26 20 f( P->xCodec &&
114c9 50 2d 3e 78 43 6f 64 65 63 28 50 2d 3e 70 43 6f P->xCodec(P->pCo
114ca 64 65 63 2c 44 2c 4e 2c 58 29 3d 3d 30 20 29 7b dec,D,N,X)==0 ){
114cb 20 45 3b 20 7d 0a 23 20 64 65 66 69 6e 65 20 43 E; }.# define C
114cc 4f 44 45 43 32 28 50 2c 44 2c 4e 2c 58 2c 45 2c ODEC2(P,D,N,X,E,
114cd 4f 29 20 5c 0a 20 20 20 20 69 66 28 20 50 2d 3e O) \. if( P->
114ce 78 43 6f 64 65 63 3d 3d 30 20 29 7b 20 4f 3d 28 xCodec==0 ){ O=(
114cf 63 68 61 72 2a 29 44 3b 20 7d 65 6c 73 65 20 5c char*)D; }else \
114d0 0a 20 20 20 20 69 66 28 20 28 4f 3d 28 63 68 61 . if( (O=(cha
114d1 72 2a 29 28 50 2d 3e 78 43 6f 64 65 63 28 50 2d r*)(P->xCodec(P-
114d2 3e 70 43 6f 64 65 63 2c 44 2c 4e 2c 58 29 29 29 >pCodec,D,N,X)))
114d3 3d 3d 30 20 29 7b 20 45 3b 20 7d 0a 23 65 6c 73 ==0 ){ E; }.#els
114d4 65 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 45 43 e.# define CODEC
114d5 31 28 50 2c 44 2c 4e 2c 58 2c 45 29 20 20 20 2f 1(P,D,N,X,E) /
114d6 2a 20 4e 4f 2d 4f 50 20 2a 2f 0a 23 20 64 65 66 * NO-OP */.# def
114d7 69 6e 65 20 43 4f 44 45 43 32 28 50 2c 44 2c 4e ine CODEC2(P,D,N
114d8 2c 58 2c 45 2c 4f 29 20 4f 3d 28 63 68 61 72 2a ,X,E,O) O=(char*
114d9 29 44 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a )D.#endif../*.**
114da 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 61 6c 6c The maximum all
114db 6f 77 65 64 20 73 65 63 74 6f 72 20 73 69 7a 65 owed sector size
114dc 2e 20 36 34 4b 69 42 2e 20 49 66 20 74 68 65 20 . 64KiB. If the
114dd 78 53 65 63 74 6f 72 73 69 7a 65 28 29 20 6d 65 xSectorsize() me
114de 74 68 6f 64 20 0a 2a 2a 20 72 65 74 75 72 6e 73 thod .** returns
114df 20 61 20 76 61 6c 75 65 20 6c 61 72 67 65 72 20 a value larger
114e0 74 68 61 6e 20 74 68 69 73 2c 20 74 68 65 6e 20 than this, then
114e1 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 20 MAX_SECTOR_SIZE
114e2 69 73 20 75 73 65 64 20 69 6e 73 74 65 61 64 2e is used instead.
114e3 0a 2a 2a 20 54 68 69 73 20 63 6f 75 6c 64 20 63 .** This could c
114e4 6f 6e 63 65 69 76 61 62 6c 79 20 63 61 75 73 65 onceivably cause
114e5 20 63 6f 72 72 75 70 74 69 6f 6e 20 66 6f 6c 6c corruption foll
114e6 6f 77 69 6e 67 20 61 20 70 6f 77 65 72 20 66 61 owing a power fa
114e7 69 6c 75 72 65 20 6f 6e 0a 2a 2a 20 73 75 63 68 ilure on.** such
114e8 20 61 20 73 79 73 74 65 6d 2e 20 54 68 69 73 20 a system. This
114e9 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 6e 20 is currently an
114ea 75 6e 64 6f 63 75 6d 65 6e 74 65 64 20 6c 69 6d undocumented lim
114eb 69 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d it..*/.#define M
114ec 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 20 30 AX_SECTOR_SIZE 0
114ed 78 31 30 30 30 30 0a 0a 2f 2a 0a 2a 2a 20 41 6e x10000../*.** An
114ee 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
114ef 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
114f0 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 ture is allocate
114f1 64 20 66 6f 72 20 65 61 63 68 20 61 63 74 69 76 d for each activ
114f2 65 0a 2a 2a 20 73 61 76 65 70 6f 69 6e 74 20 61 e.** savepoint a
114f3 6e 64 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 nd statement tra
114f4 6e 73 61 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 nsaction in the
114f5 73 79 73 74 65 6d 2e 20 41 6c 6c 20 73 75 63 68 system. All such
114f6 20 73 74 72 75 63 74 75 72 65 73 0a 2a 2a 20 61 structures.** a
114f7 72 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 re stored in the
114f8 20 50 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e Pager.aSavepoin
114f9 74 5b 5d 20 61 72 72 61 79 2c 20 77 68 69 63 68 t[] array, which
114fa 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e is allocated an
114fb 64 0a 2a 2a 20 72 65 73 69 7a 65 64 20 75 73 69 d.** resized usi
114fc 6e 67 20 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f ng sqlite3Reallo
114fd 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 c()..**.** When
114fe 61 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20 63 a savepoint is c
114ff 72 65 61 74 65 64 2c 20 74 68 65 20 50 61 67 65 reated, the Page
11500 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f rSavepoint.iHdrO
11501 66 66 73 65 74 20 66 69 65 6c 64 20 69 73 0a 2a ffset field is.*
11502 2a 20 73 65 74 20 74 6f 20 30 2e 20 49 66 20 61 * set to 0. If a
11503 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 journal-header
11504 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 is written into
11505 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c the main journal
11506 20 77 68 69 6c 65 0a 2a 2a 20 74 68 65 20 73 61 while.** the sa
11507 76 65 70 6f 69 6e 74 20 69 73 20 61 63 74 69 76 vepoint is activ
11508 65 2c 20 74 68 65 6e 20 69 48 64 72 4f 66 66 73 e, then iHdrOffs
11509 65 74 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 et is set to the
1150a 20 62 79 74 65 20 6f 66 66 73 65 74 20 0a 2a 2a byte offset .**
1150b 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c immediately fol
1150c 6c 6f 77 69 6e 67 20 74 68 65 20 6c 61 73 74 20 lowing the last
1150d 6a 6f 75 72 6e 61 6c 20 72 65 63 6f 72 64 20 77 journal record w
1150e 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 ritten into the
1150f 6d 61 69 6e 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 main.** journal
11510 62 65 66 6f 72 65 20 74 68 65 20 6a 6f 75 72 6e before the journ
11511 61 6c 2d 68 65 61 64 65 72 2e 20 54 68 69 73 20 al-header. This
11512 69 73 20 72 65 71 75 69 72 65 64 20 64 75 72 69 is required duri
11513 6e 67 20 73 61 76 65 70 6f 69 6e 74 0a 2a 2a 20 ng savepoint.**
11514 72 6f 6c 6c 62 61 63 6b 20 28 73 65 65 20 70 61 rollback (see pa
11515 67 65 72 50 6c 61 79 62 61 63 6b 53 61 76 65 70 gerPlaybackSavep
11516 6f 69 6e 74 28 29 29 2e 0a 2a 2f 0a 74 79 70 65 oint())..*/.type
11517 64 65 66 20 73 74 72 75 63 74 20 50 61 67 65 72 def struct Pager
11518 53 61 76 65 70 6f 69 6e 74 20 50 61 67 65 72 53 Savepoint PagerS
11519 61 76 65 70 6f 69 6e 74 3b 0a 73 74 72 75 63 74 avepoint;.struct
1151a 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 PagerSavepoint
1151b 7b 0a 20 20 69 36 34 20 69 4f 66 66 73 65 74 3b {. i64 iOffset;
1151c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1151d 20 2f 2a 20 53 74 61 72 74 69 6e 67 20 6f 66 66 /* Starting off
1151e 73 65 74 20 69 6e 20 6d 61 69 6e 20 6a 6f 75 72 set in main jour
1151f 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20 69 48 64 nal */. i64 iHd
11520 72 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 rOffset;
11521 20 20 20 20 20 20 2f 2a 20 53 65 65 20 61 62 6f /* See abo
11522 76 65 20 2a 2f 0a 20 20 42 69 74 76 65 63 20 2a ve */. Bitvec *
11523 70 49 6e 53 61 76 65 70 6f 69 6e 74 3b 20 20 20 pInSavepoint;
11524 20 20 20 20 20 2f 2a 20 53 65 74 20 6f 66 20 70 /* Set of p
11525 61 67 65 73 20 69 6e 20 74 68 69 73 20 73 61 76 ages in this sav
11526 65 70 6f 69 6e 74 20 2a 2f 0a 20 20 50 67 6e 6f epoint */. Pgno
11527 20 6e 4f 72 69 67 3b 20 20 20 20 20 20 20 20 20 nOrig;
11528 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 72 69 67 /* Orig
11529 69 6e 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 inal number of p
1152a 61 67 65 73 20 69 6e 20 66 69 6c 65 20 2a 2f 0a ages in file */.
1152b 20 20 50 67 6e 6f 20 69 53 75 62 52 65 63 3b 20 Pgno iSubRec;
1152c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1152d 2a 20 49 6e 64 65 78 20 6f 66 20 66 69 72 73 74 * Index of first
1152e 20 72 65 63 6f 72 64 20 69 6e 20 73 75 62 2d 6a record in sub-j
1152f 6f 75 72 6e 61 6c 20 2a 2f 0a 7d 3b 0a 0a 2f 2a ournal */.};../*
11530 0a 2a 2a 20 41 20 6f 70 65 6e 20 70 61 67 65 20 .** A open page
11531 63 61 63 68 65 20 69 73 20 61 6e 20 69 6e 73 74 cache is an inst
11532 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c ance of the foll
11533 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e owing structure.
11534 0a 2a 2a 0a 2a 2a 20 65 72 72 43 6f 64 65 0a 2a .**.** errCode.*
11535 2a 0a 2a 2a 20 20 20 50 61 67 65 72 2e 65 72 72 *.** Pager.err
11536 43 6f 64 65 20 6d 61 79 20 62 65 20 73 65 74 20 Code may be set
11537 74 6f 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 2c to SQLITE_IOERR,
11538 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2c SQLITE_CORRUPT,
11539 20 6f 72 0a 2a 2a 20 20 20 6f 72 20 53 51 4c 49 or.** or SQLI
1153a 54 45 5f 46 55 4c 4c 2e 20 4f 6e 63 65 20 6f 6e TE_FULL. Once on
1153b 65 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 74 e of the first t
1153c 68 72 65 65 20 65 72 72 6f 72 73 20 6f 63 63 75 hree errors occu
1153d 72 73 2c 20 69 74 20 70 65 72 73 69 73 74 73 0a rs, it persists.
1153e 2a 2a 20 20 20 61 6e 64 20 69 73 20 72 65 74 75 ** and is retu
1153f 72 6e 65 64 20 61 73 20 74 68 65 20 72 65 73 75 rned as the resu
11540 6c 74 20 6f 66 20 65 76 65 72 79 20 6d 61 6a 6f lt of every majo
11541 72 20 70 61 67 65 72 20 41 50 49 20 63 61 6c 6c r pager API call
11542 2e 20 20 54 68 65 0a 2a 2a 20 20 20 53 51 4c 49 . The.** SQLI
11543 54 45 5f 46 55 4c 4c 20 72 65 74 75 72 6e 20 63 TE_FULL return c
11544 6f 64 65 20 69 73 20 73 6c 69 67 68 74 6c 79 20 ode is slightly
11545 64 69 66 66 65 72 65 6e 74 2e 20 49 74 20 70 65 different. It pe
11546 72 73 69 73 74 73 20 6f 6e 6c 79 20 75 6e 74 69 rsists only unti
11547 6c 20 74 68 65 0a 2a 2a 20 20 20 6e 65 78 74 20 l the.** next
11548 73 75 63 63 65 73 73 66 75 6c 20 72 6f 6c 6c 62 successful rollb
11549 61 63 6b 20 69 73 20 70 65 72 66 6f 72 6d 65 64 ack is performed
1154a 20 6f 6e 20 74 68 65 20 70 61 67 65 72 20 63 61 on the pager ca
1154b 63 68 65 2e 20 41 6c 73 6f 2c 0a 2a 2a 20 20 20 che. Also,.**
1154c 53 51 4c 49 54 45 5f 46 55 4c 4c 20 64 6f 65 73 SQLITE_FULL does
1154d 20 6e 6f 74 20 61 66 66 65 63 74 20 74 68 65 20 not affect the
1154e 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 sqlite3PagerGet(
1154f 29 20 61 6e 64 20 73 71 6c 69 74 65 33 50 61 67 ) and sqlite3Pag
11550 65 72 4c 6f 6f 6b 75 70 28 29 0a 2a 2a 20 20 20 erLookup().**
11551 41 50 49 73 2c 20 74 68 65 79 20 6d 61 79 20 73 APIs, they may s
11552 74 69 6c 6c 20 62 65 20 75 73 65 64 20 73 75 63 till be used suc
11553 63 65 73 73 66 75 6c 6c 79 2e 0a 2a 2a 0a 2a 2a cessfully..**.**
11554 20 64 62 53 69 7a 65 56 61 6c 69 64 2c 20 64 62 dbSizeValid, db
11555 53 69 7a 65 2c 20 64 62 4f 72 69 67 53 69 7a 65 Size, dbOrigSize
11556 2c 20 64 62 46 69 6c 65 53 69 7a 65 0a 2a 2a 0a , dbFileSize.**.
11557 2a 2a 20 20 20 4d 61 6e 61 67 69 6e 67 20 74 68 ** Managing th
11558 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 e size of the da
11559 74 61 62 61 73 65 20 66 69 6c 65 20 69 6e 20 70 tabase file in p
1155a 61 67 65 73 20 69 73 20 61 20 6c 69 74 74 6c 65 ages is a little
1155b 20 63 6f 6d 70 6c 69 63 61 74 65 64 2e 0a 2a 2a complicated..**
1155c 20 20 20 54 68 65 20 76 61 72 69 61 62 6c 65 20 The variable
1155d 50 61 67 65 72 2e 64 62 53 69 7a 65 20 63 6f 6e Pager.dbSize con
1155e 74 61 69 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 tains the number
1155f 20 6f 66 20 70 61 67 65 73 20 74 68 61 74 20 74 of pages that t
11560 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 he database.**
11561 20 69 6d 61 67 65 20 63 75 72 72 65 6e 74 6c 79 image currently
11562 20 63 6f 6e 74 61 69 6e 73 2e 20 41 73 20 74 68 contains. As th
11563 65 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 e database image
11564 20 67 72 6f 77 73 20 6f 72 20 73 68 72 69 6e 6b grows or shrink
11565 73 20 74 68 69 73 0a 2a 2a 20 20 20 76 61 72 69 s this.** vari
11566 61 62 6c 65 20 69 73 20 75 70 64 61 74 65 64 2e able is updated.
11567 20 54 68 65 20 76 61 72 69 61 62 6c 65 20 50 61 The variable Pa
11568 67 65 72 2e 64 62 46 69 6c 65 53 69 7a 65 20 63 ger.dbFileSize c
11569 6f 6e 74 61 69 6e 73 20 74 68 65 20 6e 75 6d 62 ontains the numb
1156a 65 72 0a 2a 2a 20 20 20 6f 66 20 70 61 67 65 73 er.** of pages
1156b 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
1156c 20 66 69 6c 65 2e 20 54 68 69 73 20 6d 61 79 20 file. This may
1156d 62 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f be different fro
1156e 6d 20 50 61 67 65 72 2e 64 62 53 69 7a 65 0a 2a m Pager.dbSize.*
1156f 2a 20 20 20 69 66 20 73 6f 6d 65 20 70 61 67 65 * if some page
11570 73 20 68 61 76 65 20 62 65 65 6e 20 61 70 70 65 s have been appe
11571 6e 64 65 64 20 74 6f 20 74 68 65 20 64 61 74 61 nded to the data
11572 62 61 73 65 20 69 6d 61 67 65 20 62 75 74 20 6e base image but n
11573 6f 74 20 79 65 74 20 77 72 69 74 74 65 6e 0a 2a ot yet written.*
11574 2a 20 20 20 6f 75 74 20 66 72 6f 6d 20 74 68 65 * out from the
11575 20 63 61 63 68 65 20 74 6f 20 74 68 65 20 61 63 cache to the ac
11576 74 75 61 6c 20 66 69 6c 65 20 6f 6e 20 64 69 73 tual file on dis
11577 6b 2e 20 4f 72 20 69 66 20 74 68 65 20 69 6d 61 k. Or if the ima
11578 67 65 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 20 ge has been.**
11579 20 74 72 75 6e 63 61 74 65 64 20 62 79 20 61 6e truncated by an
1157a 20 69 6e 63 72 65 6d 65 6e 74 61 6c 2d 76 61 63 incremental-vac
1157b 75 75 6d 20 6f 70 65 72 61 74 69 6f 6e 2e 20 54 uum operation. T
1157c 68 65 20 50 61 67 65 72 2e 64 62 4f 72 69 67 53 he Pager.dbOrigS
1157d 69 7a 65 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 ize variable.**
1157e 20 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6e contains the n
1157f 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 umber of pages i
11580 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 n the database i
11581 6d 61 67 65 20 77 68 65 6e 20 74 68 65 20 63 75 mage when the cu
11582 72 72 65 6e 74 0a 2a 2a 20 20 20 74 72 61 6e 73 rrent.** trans
11583 61 63 74 69 6f 6e 20 77 61 73 20 6f 70 65 6e 65 action was opene
11584 64 2e 20 54 68 65 20 63 6f 6e 74 65 6e 74 73 20 d. The contents
11585 6f 66 20 61 6c 6c 20 74 68 72 65 65 20 6f 66 20 of all three of
11586 74 68 65 73 65 20 76 61 72 69 61 62 6c 65 73 20 these variables
11587 69 73 0a 2a 2a 20 20 20 6f 6e 6c 79 20 67 75 61 is.** only gua
11588 72 61 6e 74 65 65 64 20 74 6f 20 62 65 20 63 6f ranteed to be co
11589 72 72 65 63 74 20 69 66 20 74 68 65 20 62 6f 6f rrect if the boo
1158a 6c 65 61 6e 20 50 61 67 65 72 2e 64 62 53 69 7a lean Pager.dbSiz
1158b 65 56 61 6c 69 64 20 69 73 20 74 72 75 65 2e 0a eValid is true..
1158c 2a 2a 0a 2a 2a 20 20 20 54 4f 44 4f 3a 20 55 6e **.** TODO: Un
1158d 64 65 72 20 77 68 61 74 20 63 6f 6e 64 69 74 69 der what conditi
1158e 6f 6e 73 20 69 73 20 64 62 53 69 7a 65 56 61 6c ons is dbSizeVal
1158f 69 64 20 73 65 74 3f 20 43 6c 65 61 72 65 64 3f id set? Cleared?
11590 0a 2a 2a 0a 2a 2a 20 63 68 61 6e 67 65 43 6f 75 .**.** changeCou
11591 6e 74 44 6f 6e 65 0a 2a 2a 0a 2a 2a 20 20 20 54 ntDone.**.** T
11592 68 69 73 20 62 6f 6f 6c 65 61 6e 20 76 61 72 69 his boolean vari
11593 61 62 6c 65 20 69 73 20 75 73 65 64 20 74 6f 20 able is used to
11594 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 make sure that t
11595 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 he change-counte
11596 72 20 0a 2a 2a 20 20 20 28 74 68 65 20 34 2d 62 r .** (the 4-b
11597 79 74 65 20 68 65 61 64 65 72 20 66 69 65 6c 64 yte header field
11598 20 61 74 20 62 79 74 65 20 6f 66 66 73 65 74 20 at byte offset
11599 32 34 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 24 of the databa
1159a 73 65 20 66 69 6c 65 29 20 69 73 20 0a 2a 2a 20 se file) is .**
1159b 20 20 6e 6f 74 20 75 70 64 61 74 65 64 20 6d 6f not updated mo
1159c 72 65 20 6f 66 74 65 6e 20 74 68 61 6e 20 6e 65 re often than ne
1159d 63 65 73 73 61 72 79 2e 20 0a 2a 2a 0a 2a 2a 20 cessary. .**.**
1159e 20 20 49 74 20 69 73 20 73 65 74 20 74 6f 20 74 It is set to t
1159f 72 75 65 20 77 68 65 6e 20 74 68 65 20 63 68 61 rue when the cha
115a0 6e 67 65 2d 63 6f 75 6e 74 65 72 20 66 69 65 6c nge-counter fiel
115a1 64 20 69 73 20 75 70 64 61 74 65 64 2c 20 77 68 d is updated, wh
115a2 69 63 68 20 0a 2a 2a 20 20 20 63 61 6e 20 6f 6e ich .** can on
115a3 6c 79 20 68 61 70 70 65 6e 20 69 66 20 61 6e 20 ly happen if an
115a4 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 69 exclusive lock i
115a5 73 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 64 61 s held on the da
115a6 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 tabase file..**
115a7 20 20 49 74 20 69 73 20 63 6c 65 61 72 65 64 20 It is cleared
115a8 28 73 65 74 20 74 6f 20 66 61 6c 73 65 29 20 77 (set to false) w
115a9 68 65 6e 65 76 65 72 20 61 6e 20 65 78 63 6c 75 henever an exclu
115aa 73 69 76 65 20 6c 6f 63 6b 20 69 73 20 0a 2a 2a sive lock is .**
115ab 20 20 20 72 65 6c 69 6e 71 75 69 73 68 65 64 20 relinquished
115ac 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
115ad 66 69 6c 65 2e 20 45 61 63 68 20 74 69 6d 65 20 file. Each time
115ae 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 a transaction is
115af 20 63 6f 6d 6d 69 74 74 65 64 2c 0a 2a 2a 20 20 committed,.**
115b0 20 54 68 65 20 63 68 61 6e 67 65 43 6f 75 6e 74 The changeCount
115b1 44 6f 6e 65 20 66 6c 61 67 20 69 73 20 69 6e 73 Done flag is ins
115b2 70 65 63 74 65 64 2e 20 49 66 20 69 74 20 69 73 pected. If it is
115b3 20 74 72 75 65 2c 20 74 68 65 20 77 6f 72 6b 20 true, the work
115b4 6f 66 0a 2a 2a 20 20 20 75 70 64 61 74 69 6e 67 of.** updating
115b5 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e the change-coun
115b6 74 65 72 20 69 73 20 6f 6d 69 74 74 65 64 20 66 ter is omitted f
115b7 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 or the current t
115b8 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a ransaction..**.*
115b9 2a 20 20 20 54 68 69 73 20 6d 65 63 68 61 6e 69 * This mechani
115ba 73 6d 20 6d 65 61 6e 73 20 74 68 61 74 20 77 68 sm means that wh
115bb 65 6e 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 en running in ex
115bc 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20 61 20 clusive mode, a
115bd 63 6f 6e 6e 65 63 74 69 6f 6e 20 0a 2a 2a 20 20 connection .**
115be 20 6e 65 65 64 20 6f 6e 6c 79 20 75 70 64 61 74 need only updat
115bf 65 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 e the change-cou
115c0 6e 74 65 72 20 6f 6e 63 65 2c 20 66 6f 72 20 74 nter once, for t
115c1 68 65 20 66 69 72 73 74 20 74 72 61 6e 73 61 63 he first transac
115c2 74 69 6f 6e 0a 2a 2a 20 20 20 63 6f 6d 6d 69 74 tion.** commit
115c3 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 64 62 4d 6f 64 ted..**.** dbMod
115c4 69 66 69 65 64 0a 2a 2a 0a 2a 2a 20 20 20 54 68 ified.**.** Th
115c5 65 20 64 62 4d 6f 64 69 66 69 65 64 20 66 6c 61 e dbModified fla
115c6 67 20 69 73 20 73 65 74 20 77 68 65 6e 65 76 65 g is set wheneve
115c7 72 20 61 20 64 61 74 61 62 61 73 65 20 70 61 67 r a database pag
115c8 65 20 69 73 20 64 69 72 74 69 65 64 2e 0a 2a 2a e is dirtied..**
115c9 20 20 20 49 74 20 69 73 20 63 6c 65 61 72 65 64 It is cleared
115ca 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 65 at the end of e
115cb 61 63 68 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e ach transaction.
115cc 0a 2a 2a 0a 2a 2a 20 20 20 49 74 20 69 73 20 75 .**.** It is u
115cd 73 65 64 20 77 68 65 6e 20 63 6f 6d 6d 69 74 74 sed when committ
115ce 69 6e 67 20 6f 72 20 6f 74 68 65 72 77 69 73 65 ing or otherwise
115cf 20 65 6e 64 69 6e 67 20 61 20 74 72 61 6e 73 61 ending a transa
115d0 63 74 69 6f 6e 2e 20 49 66 0a 2a 2a 20 20 20 74 ction. If.** t
115d1 68 65 20 64 62 4d 6f 64 69 66 69 65 64 20 66 6c he dbModified fl
115d2 61 67 20 69 73 20 63 6c 65 61 72 20 74 68 65 6e ag is clear then
115d3 20 6c 65 73 73 20 77 6f 72 6b 20 68 61 73 20 74 less work has t
115d4 6f 20 62 65 20 64 6f 6e 65 2e 0a 2a 2a 0a 2a 2a o be done..**.**
115d5 20 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 0a journalStarted.
115d6 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 66 6c 61 **.** This fla
115d7 67 20 69 73 20 73 65 74 20 77 68 65 6e 65 76 65 g is set wheneve
115d8 72 20 74 68 65 20 74 68 65 20 6d 61 69 6e 20 6a r the the main j
115d9 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 ournal is synced
115da 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65 20 70 . .**.** The p
115db 6f 69 6e 74 20 6f 66 20 74 68 69 73 20 66 6c 61 oint of this fla
115dc 67 20 69 73 20 74 68 61 74 20 69 74 20 6d 75 73 g is that it mus
115dd 74 20 62 65 20 73 65 74 20 61 66 74 65 72 20 74 t be set after t
115de 68 65 20 0a 2a 2a 20 20 20 66 69 72 73 74 20 6a he .** first j
115df 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69 6e ournal header in
115e0 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 a journal file
115e1 68 61 73 20 62 65 65 6e 20 73 79 6e 63 65 64 20 has been synced
115e2 74 6f 20 64 69 73 6b 2e 0a 2a 2a 20 20 20 41 66 to disk..** Af
115e3 74 65 72 20 74 68 69 73 20 68 61 73 20 68 61 70 ter this has hap
115e4 70 65 6e 65 64 2c 20 6e 65 77 20 70 61 67 65 73 pened, new pages
115e5 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 appended to the
115e6 20 64 61 74 61 62 61 73 65 20 0a 2a 2a 20 20 20 database .**
115e7 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 68 65 20 do not need the
115e8 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 PGHDR_NEED_SYNC
115e9 66 6c 61 67 20 73 65 74 2c 20 61 73 20 74 68 65 flag set, as the
115ea 79 20 64 6f 20 6e 6f 74 20 6e 65 65 64 0a 2a 2a y do not need.**
115eb 20 20 20 74 6f 20 77 61 69 74 20 66 6f 72 20 61 to wait for a
115ec 20 6a 6f 75 72 6e 61 6c 20 73 79 6e 63 20 62 65 journal sync be
115ed 66 6f 72 65 20 74 68 65 79 20 63 61 6e 20 62 65 fore they can be
115ee 20 77 72 69 74 74 65 6e 20 6f 75 74 20 74 6f 0a written out to.
115ef 2a 2a 20 20 20 74 68 65 20 64 61 74 61 62 61 73 ** the databas
115f0 65 20 66 69 6c 65 20 28 73 65 65 20 66 75 6e 63 e file (see func
115f1 74 69 6f 6e 20 70 61 67 65 72 5f 77 72 69 74 65 tion pager_write
115f2 28 29 29 2e 0a 2a 2a 20 20 20 0a 2a 2a 20 73 65 ())..** .** se
115f3 74 4d 61 73 74 65 72 0a 2a 2a 0a 2a 2a 20 20 20 tMaster.**.**
115f4 54 68 69 73 20 76 61 72 69 61 62 6c 65 20 69 73 This variable is
115f5 20 75 73 65 64 20 74 6f 20 65 6e 73 75 72 65 20 used to ensure
115f6 74 68 61 74 20 74 68 65 20 6d 61 73 74 65 72 20 that the master
115f7 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d journal file nam
115f8 65 0a 2a 2a 20 20 20 28 69 66 20 61 6e 79 29 20 e.** (if any)
115f9 69 73 20 6f 6e 6c 79 20 77 72 69 74 74 65 6e 20 is only written
115fa 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c into the journal
115fb 20 66 69 6c 65 20 6f 6e 63 65 2e 0a 2a 2a 0a 2a file once..**.*
115fc 2a 20 20 20 57 68 65 6e 20 63 6f 6d 6d 69 74 74 * When committ
115fd 69 6e 67 20 61 20 74 72 61 6e 73 61 63 74 69 6f ing a transactio
115fe 6e 2c 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f n, the master jo
115ff 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 urnal file name
11600 28 69 66 20 61 6e 79 29 0a 2a 2a 20 20 20 6d 61 (if any).** ma
11601 79 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74 y be written int
11602 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 o the journal fi
11603 6c 65 20 77 68 69 6c 65 20 74 68 65 20 70 61 67 le while the pag
11604 65 72 20 69 73 20 73 74 69 6c 6c 20 69 6e 0a 2a er is still in.*
11605 2a 20 20 20 50 41 47 45 52 5f 52 45 53 45 52 56 * PAGER_RESERV
11606 45 44 20 73 74 61 74 65 20 28 73 65 65 20 43 6f ED state (see Co
11607 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 20 66 mmitPhaseOne() f
11608 6f 72 20 74 68 65 20 61 63 74 69 6f 6e 29 2e 20 or the action).
11609 49 74 0a 2a 2a 20 20 20 74 68 65 6e 20 61 74 74 It.** then att
1160a 65 6d 70 74 73 20 74 6f 20 75 70 67 72 61 64 65 empts to upgrade
1160b 20 74 6f 20 61 6e 20 65 78 63 6c 75 73 69 76 65 to an exclusive
1160c 20 6c 6f 63 6b 2e 20 49 66 20 74 68 69 73 20 61 lock. If this a
1160d 74 74 65 6d 70 74 0a 2a 2a 20 20 20 66 61 69 6c ttempt.** fail
1160e 73 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 42 s, then SQLITE_B
1160f 55 53 59 20 6d 61 79 20 62 65 20 72 65 74 75 72 USY may be retur
11610 6e 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 20 ned to the user
11611 61 6e 64 20 74 68 65 20 75 73 65 72 0a 2a 2a 20 and the user.**
11612 20 20 6d 61 79 20 61 74 74 65 6d 70 74 20 74 6f may attempt to
11613 20 63 6f 6d 6d 69 74 20 74 68 65 20 74 72 61 6e commit the tran
11614 73 61 63 74 69 6f 6e 20 61 67 61 69 6e 20 6c 61 saction again la
11615 74 65 72 20 28 63 61 6c 6c 69 6e 67 0a 2a 2a 20 ter (calling.**
11616 20 20 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 CommitPhaseOne
11617 28 29 20 61 67 61 69 6e 29 2e 20 54 68 69 73 20 () again). This
11618 66 6c 61 67 20 69 73 20 75 73 65 64 20 74 6f 20 flag is used to
11619 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 20 ensure that the
1161a 0a 2a 2a 20 20 20 6d 61 73 74 65 72 20 6a 6f 75 .** master jou
1161b 72 6e 61 6c 20 6e 61 6d 65 20 69 73 20 6f 6e 6c rnal name is onl
1161c 79 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 y written to the
1161d 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68 journal file th
1161e 65 20 66 69 72 73 74 0a 2a 2a 20 20 20 74 69 6d e first.** tim
1161f 65 20 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 e CommitPhaseOne
11620 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a () is called..**
11621 0a 2a 2a 20 64 6f 4e 6f 74 53 79 6e 63 0a 2a 2a .** doNotSync.**
11622 0a 2a 2a 20 20 20 54 68 69 73 20 76 61 72 69 61 .** This varia
11623 62 6c 65 20 69 73 20 73 65 74 20 61 6e 64 20 63 ble is set and c
11624 6c 65 61 72 65 64 20 62 79 20 73 71 6c 69 74 65 leared by sqlite
11625 33 50 61 67 65 72 57 72 69 74 65 28 29 2e 0a 2a 3PagerWrite()..*
11626 2a 0a 2a 2a 20 6e 65 65 64 53 79 6e 63 0a 2a 2a *.** needSync.**
11627 0a 2a 2a 20 20 20 54 4f 44 4f 3a 20 49 74 20 6d .** TODO: It m
11628 69 67 68 74 20 62 65 20 65 61 73 69 65 72 20 74 ight be easier t
11629 6f 20 73 65 74 20 74 68 69 73 20 76 61 72 69 61 o set this varia
1162a 62 6c 65 20 69 6e 20 77 72 69 74 65 4a 6f 75 72 ble in writeJour
1162b 6e 61 6c 48 64 72 28 29 0a 2a 2a 20 20 20 61 6e nalHdr().** an
1162c 64 20 77 72 69 74 65 4d 61 73 74 65 72 4a 6f 75 d writeMasterJou
1162d 72 6e 61 6c 28 29 20 6f 6e 6c 79 2e 20 43 68 61 rnal() only. Cha
1162e 6e 67 65 20 69 74 73 20 6d 65 61 6e 69 6e 67 20 nge its meaning
1162f 74 6f 20 22 75 6e 73 79 6e 63 65 64 20 64 61 74 to "unsynced dat
11630 61 0a 2a 2a 20 20 20 68 61 73 20 62 65 65 6e 20 a.** has been
11631 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a written to the j
11632 6f 75 72 6e 61 6c 22 2e 0a 2a 2a 0a 2a 2a 20 73 ournal"..**.** s
11633 75 62 6a 49 6e 4d 65 6d 6f 72 79 0a 2a 2a 0a 2a ubjInMemory.**.*
11634 2a 20 20 20 54 68 69 73 20 69 73 20 61 20 62 6f * This is a bo
11635 6f 6c 65 61 6e 20 76 61 72 69 61 62 6c 65 2e 20 olean variable.
11636 49 66 20 74 72 75 65 2c 20 74 68 65 6e 20 61 6e If true, then an
11637 79 20 72 65 71 75 69 72 65 64 20 73 75 62 2d 6a y required sub-j
11638 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 69 73 20 6f ournal.** is o
11639 70 65 6e 65 64 20 61 73 20 61 6e 20 69 6e 2d 6d pened as an in-m
1163a 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69 emory journal fi
1163b 6c 65 2e 20 49 66 20 66 61 6c 73 65 2c 20 74 68 le. If false, th
1163c 65 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 en in-memory.**
1163d 20 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 73 20 61 sub-journals a
1163e 72 65 20 6f 6e 6c 79 20 75 73 65 64 20 66 6f 72 re only used for
1163f 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 72 in-memory pager
11640 20 66 69 6c 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 files..*/.struc
11641 74 20 50 61 67 65 72 20 7b 0a 20 20 73 71 6c 69 t Pager {. sqli
11642 74 65 33 5f 76 66 73 20 2a 70 56 66 73 3b 20 20 te3_vfs *pVfs;
11643 20 20 20 20 20 20 20 20 2f 2a 20 4f 53 20 66 75 /* OS fu
11644 6e 63 74 69 6f 6e 73 20 74 6f 20 75 73 65 20 66 nctions to use f
11645 6f 72 20 49 4f 20 2a 2f 0a 20 20 75 38 20 65 78 or IO */. u8 ex
11646 63 6c 75 73 69 76 65 4d 6f 64 65 3b 20 20 20 20 clusiveMode;
11647 20 20 20 20 20 20 20 2f 2a 20 42 6f 6f 6c 65 61 /* Boolea
11648 6e 2e 20 54 72 75 65 20 69 66 20 6c 6f 63 6b 69 n. True if locki
11649 6e 67 5f 6d 6f 64 65 3d 3d 45 58 43 4c 55 53 49 ng_mode==EXCLUSI
1164a 56 45 20 2a 2f 0a 20 20 75 38 20 6a 6f 75 72 6e VE */. u8 journ
1164b 61 6c 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20 alMode;
1164c 20 20 20 20 2f 2a 20 4f 6e 20 6f 66 20 74 68 65 /* On of the
1164d 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f PAGER_JOURNALMO
1164e 44 45 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 DE_* values */.
1164f 20 75 38 20 75 73 65 4a 6f 75 72 6e 61 6c 3b 20 u8 useJournal;
11650 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
11651 55 73 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a Use a rollback j
11652 6f 75 72 6e 61 6c 20 6f 6e 20 74 68 69 73 20 66 ournal on this f
11653 69 6c 65 20 2a 2f 0a 20 20 75 38 20 6e 6f 52 65 ile */. u8 noRe
11654 61 64 6c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 adlock;
11655 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 62 /* Do not b
11656 6f 74 68 65 72 20 74 6f 20 6f 62 74 61 69 6e 20 other to obtain
11657 72 65 61 64 6c 6f 63 6b 73 20 2a 2f 0a 20 20 75 readlocks */. u
11658 38 20 6e 6f 53 79 6e 63 3b 20 20 20 20 20 20 20 8 noSync;
11659 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f /* Do
1165a 20 6e 6f 74 20 73 79 6e 63 20 74 68 65 20 6a 6f not sync the jo
1165b 75 72 6e 61 6c 20 69 66 20 74 72 75 65 20 2a 2f urnal if true */
1165c 0a 20 20 75 38 20 66 75 6c 6c 53 79 6e 63 3b 20 . u8 fullSync;
1165d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1165e 2a 20 44 6f 20 65 78 74 72 61 20 73 79 6e 63 73 * Do extra syncs
1165f 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 of the journal
11660 66 6f 72 20 72 6f 62 75 73 74 6e 65 73 73 20 2a for robustness *
11661 2f 0a 20 20 75 38 20 73 79 6e 63 5f 66 6c 61 67 /. u8 sync_flag
11662 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s;
11663 2f 2a 20 4f 6e 65 20 6f 66 20 53 59 4e 43 5f 4e /* One of SYNC_N
11664 4f 52 4d 41 4c 20 6f 72 20 53 59 4e 43 5f 46 55 ORMAL or SYNC_FU
11665 4c 4c 20 2a 2f 0a 20 20 75 38 20 74 65 6d 70 46 LL */. u8 tempF
11666 69 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ile;
11667 20 20 20 20 2f 2a 20 7a 46 69 6c 65 6e 61 6d 65 /* zFilename
11668 20 69 73 20 61 20 74 65 6d 70 6f 72 61 72 79 20 is a temporary
11669 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 72 65 61 file */. u8 rea
1166a 64 4f 6e 6c 79 3b 20 20 20 20 20 20 20 20 20 20 dOnly;
1166b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f /* True fo
1166c 72 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 64 61 r a read-only da
1166d 74 61 62 61 73 65 20 2a 2f 0a 20 20 75 38 20 6d tabase */. u8 m
1166e 65 6d 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 emDb;
1166f 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
11670 74 6f 20 69 6e 68 69 62 69 74 20 61 6c 6c 20 66 to inhibit all f
11671 69 6c 65 20 49 2f 4f 20 2a 2f 0a 0a 20 20 2f 2a ile I/O */.. /*
11672 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 The following b
11673 6c 6f 63 6b 20 63 6f 6e 74 61 69 6e 73 20 74 68 lock contains th
11674 6f 73 65 20 63 6c 61 73 73 20 6d 65 6d 62 65 72 ose class member
11675 73 20 74 68 61 74 20 61 72 65 20 64 79 6e 61 6d s that are dynam
11676 69 63 61 6c 6c 79 0a 20 20 2a 2a 20 6d 6f 64 69 ically. ** modi
11677 66 69 65 64 20 64 75 72 69 6e 67 20 6e 6f 72 6d fied during norm
11678 61 6c 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 54 al operations. T
11679 68 65 20 6f 74 68 65 72 20 76 61 72 69 61 62 6c he other variabl
1167a 65 73 20 69 6e 20 74 68 69 73 20 73 74 72 75 63 es in this struc
1167b 74 75 72 65 0a 20 20 2a 2a 20 61 72 65 20 65 69 ture. ** are ei
1167c 74 68 65 72 20 63 6f 6e 73 74 61 6e 74 20 74 68 ther constant th
1167d 72 6f 75 67 68 6f 75 74 20 74 68 65 20 6c 69 66 roughout the lif
1167e 65 74 69 6d 65 20 6f 66 20 74 68 65 20 70 61 67 etime of the pag
1167f 65 72 2c 20 6f 72 20 65 6c 73 65 0a 20 20 2a 2a er, or else. **
11680 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 63 used to store c
11681 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 70 61 72 onfiguration par
11682 61 6d 65 74 65 72 73 20 74 68 61 74 20 61 66 66 ameters that aff
11683 65 63 74 20 74 68 65 20 77 61 79 20 74 68 65 20 ect the way the
11684 70 61 67 65 72 20 0a 20 20 2a 2a 20 6f 70 65 72 pager . ** oper
11685 61 74 65 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 ates.. **. **
11686 54 68 65 20 27 73 74 61 74 65 27 20 76 61 72 69 The 'state' vari
11687 61 62 6c 65 20 69 73 20 64 65 73 63 72 69 62 65 able is describe
11688 64 20 69 6e 20 6d 6f 72 65 20 64 65 74 61 69 6c d in more detail
11689 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 0a along with the.
1168a 20 20 2a 2a 20 64 65 73 63 72 69 70 74 69 6f 6e ** description
1168b 73 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 s of the values
1168c 69 74 20 6d 61 79 20 74 61 6b 65 20 2d 20 50 41 it may take - PA
1168d 47 45 52 5f 55 4e 4c 4f 43 4b 20 65 74 63 2e 20 GER_UNLOCK etc.
1168e 4d 61 6e 79 20 6f 66 20 74 68 65 0a 20 20 2a 2a Many of the. **
1168f 20 6f 74 68 65 72 20 76 61 72 69 61 62 6c 65 73 other variables
11690 20 69 6e 20 74 68 69 73 20 62 6c 6f 63 6b 20 61 in this block a
11691 72 65 20 64 65 73 63 72 69 62 65 64 20 69 6e 20 re described in
11692 74 68 65 20 63 6f 6d 6d 65 6e 74 20 64 69 72 65 the comment dire
11693 63 74 6c 79 20 0a 20 20 2a 2a 20 61 62 6f 76 65 ctly . ** above
11694 20 74 68 69 73 20 63 6c 61 73 73 20 64 65 66 69 this class defi
11695 6e 69 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 75 nition.. */. u
11696 38 20 73 74 61 74 65 3b 20 20 20 20 20 20 20 20 8 state;
11697 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 41 /* PA
11698 47 45 52 5f 55 4e 4c 4f 43 4b 2c 20 5f 53 48 41 GER_UNLOCK, _SHA
11699 52 45 44 2c 20 5f 52 45 53 45 52 56 45 44 2c 20 RED, _RESERVED,
1169a 65 74 63 2e 20 2a 2f 0a 20 20 75 38 20 64 62 4d etc. */. u8 dbM
1169b 6f 64 69 66 69 65 64 3b 20 20 20 20 20 20 20 20 odified;
1169c 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
1169d 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 63 there are any c
1169e 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 44 62 hanges to the Db
1169f 20 2a 2f 0a 20 20 75 38 20 6e 65 65 64 53 79 6e */. u8 needSyn
116a0 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
116a1 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6e 20 /* True if an
116a2 66 73 79 6e 63 28 29 20 69 73 20 6e 65 65 64 65 fsync() is neede
116a3 64 20 6f 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c d on the journal
116a4 20 2a 2f 0a 20 20 75 38 20 6a 6f 75 72 6e 61 6c */. u8 journal
116a5 53 74 61 72 74 65 64 3b 20 20 20 20 20 20 20 20 Started;
116a6 20 20 2f 2a 20 54 72 75 65 20 69 66 20 68 65 61 /* True if hea
116a7 64 65 72 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 69 der of journal i
116a8 73 20 73 79 6e 63 65 64 20 2a 2f 0a 20 20 75 38 s synced */. u8
116a9 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 changeCountDone
116aa 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74 ; /* Set
116ab 20 61 66 74 65 72 20 69 6e 63 72 65 6d 65 6e 74 after increment
116ac 69 6e 67 20 74 68 65 20 63 68 61 6e 67 65 2d 63 ing the change-c
116ad 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 38 20 73 ounter */. u8 s
116ae 65 74 4d 61 73 74 65 72 3b 20 20 20 20 20 20 20 etMaster;
116af 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
116b0 69 66 20 61 20 6d 2d 6a 20 6e 61 6d 65 20 68 61 if a m-j name ha
116b1 73 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 s been written t
116b2 6f 20 6a 72 6e 6c 20 2a 2f 0a 20 20 75 38 20 64 o jrnl */. u8 d
116b3 6f 4e 6f 74 53 79 6e 63 3b 20 20 20 20 20 20 20 oNotSync;
116b4 20 20 20 20 20 20 20 20 2f 2a 20 42 6f 6f 6c 65 /* Boole
116b5 61 6e 2e 20 57 68 69 6c 65 20 74 72 75 65 2c 20 an. While true,
116b6 64 6f 20 6e 6f 74 20 73 70 69 6c 6c 20 74 68 65 do not spill the
116b7 20 63 61 63 68 65 20 2a 2f 0a 20 20 75 38 20 64 cache */. u8 d
116b8 62 53 69 7a 65 56 61 6c 69 64 3b 20 20 20 20 20 bSizeValid;
116b9 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 77 /* Set w
116ba 68 65 6e 20 64 62 53 69 7a 65 20 69 73 20 63 6f hen dbSize is co
116bb 72 72 65 63 74 20 2a 2f 0a 20 20 75 38 20 73 75 rrect */. u8 su
116bc 62 6a 49 6e 4d 65 6d 6f 72 79 3b 20 20 20 20 20 bjInMemory;
116bd 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 /* True t
116be 6f 20 75 73 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 o use in-memory
116bf 73 75 62 2d 6a 6f 75 72 6e 61 6c 73 20 2a 2f 0a sub-journals */.
116c0 20 20 50 67 6e 6f 20 64 62 53 69 7a 65 3b 20 20 Pgno dbSize;
116c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
116c2 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 Number of pages
116c3 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
116c4 20 2a 2f 0a 20 20 50 67 6e 6f 20 64 62 4f 72 69 */. Pgno dbOri
116c5 67 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 gSize;
116c6 20 20 2f 2a 20 64 62 53 69 7a 65 20 62 65 66 6f /* dbSize befo
116c7 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 re the current t
116c8 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 ransaction */.
116c9 50 67 6e 6f 20 64 62 46 69 6c 65 53 69 7a 65 3b Pgno dbFileSize;
116ca 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
116cb 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 umber of pages i
116cc 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
116cd 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 65 72 72 ile */. int err
116ce 43 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 Code;
116cf 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 73 /* One of s
116d0 65 76 65 72 61 6c 20 6b 69 6e 64 73 20 6f 66 20 everal kinds of
116d1 65 72 72 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 errors */. int
116d2 6e 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 nRec;
116d3 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 73 /* Pages
116d4 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 73 69 6e 63 journalled sinc
116d5 65 20 6c 61 73 74 20 6a 2d 68 65 61 64 65 72 20 e last j-header
116d6 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 75 33 32 written */. u32
116d7 20 63 6b 73 75 6d 49 6e 69 74 3b 20 20 20 20 20 cksumInit;
116d8 20 20 20 20 20 20 20 20 20 2f 2a 20 51 75 61 73 /* Quas
116d9 69 2d 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 61 i-random value a
116da 64 64 65 64 20 74 6f 20 65 76 65 72 79 20 63 68 dded to every ch
116db 65 63 6b 73 75 6d 20 2a 2f 0a 20 20 75 33 32 20 ecksum */. u32
116dc 6e 53 75 62 52 65 63 3b 20 20 20 20 20 20 20 20 nSubRec;
116dd 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
116de 72 20 6f 66 20 72 65 63 6f 72 64 73 20 77 72 69 r of records wri
116df 74 74 65 6e 20 74 6f 20 73 75 62 2d 6a 6f 75 72 tten to sub-jour
116e0 6e 61 6c 20 2a 2f 0a 20 20 42 69 74 76 65 63 20 nal */. Bitvec
116e1 2a 70 49 6e 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 *pInJournal;
116e2 20 20 20 20 20 2f 2a 20 4f 6e 65 20 62 69 74 20 /* One bit
116e3 66 6f 72 20 65 61 63 68 20 70 61 67 65 20 69 6e for each page in
116e4 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
116e5 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f le */. sqlite3_
116e6 66 69 6c 65 20 2a 66 64 3b 20 20 20 20 20 20 20 file *fd;
116e7 20 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 /* File desc
116e8 72 69 70 74 6f 72 20 66 6f 72 20 64 61 74 61 62 riptor for datab
116e9 61 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ase */. sqlite3
116ea 5f 66 69 6c 65 20 2a 6a 66 64 3b 20 20 20 20 20 _file *jfd;
116eb 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 /* File des
116ec 63 72 69 70 74 6f 72 20 66 6f 72 20 6d 61 69 6e criptor for main
116ed 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 73 71 journal */. sq
116ee 6c 69 74 65 33 5f 66 69 6c 65 20 2a 73 6a 66 64 lite3_file *sjfd
116ef 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c ; /* Fil
116f0 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 e descriptor for
116f1 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a sub-journal */.
116f2 20 20 69 36 34 20 6a 6f 75 72 6e 61 6c 4f 66 66 i64 journalOff
116f3 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
116f4 20 43 75 72 72 65 6e 74 20 77 72 69 74 65 20 6f Current write o
116f5 66 66 73 65 74 20 69 6e 20 74 68 65 20 6a 6f 75 ffset in the jou
116f6 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69 rnal file */. i
116f7 36 34 20 6a 6f 75 72 6e 61 6c 48 64 72 3b 20 20 64 journalHdr;
116f8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 /* By
116f9 74 65 20 6f 66 66 73 65 74 20 74 6f 20 70 72 65 te offset to pre
116fa 76 69 6f 75 73 20 6a 6f 75 72 6e 61 6c 20 68 65 vious journal he
116fb 61 64 65 72 20 2a 2f 0a 20 20 50 61 67 65 72 53 ader */. PagerS
116fc 61 76 65 70 6f 69 6e 74 20 2a 61 53 61 76 65 70 avepoint *aSavep
116fd 6f 69 6e 74 3b 20 2f 2a 20 41 72 72 61 79 20 6f oint; /* Array o
116fe 66 20 61 63 74 69 76 65 20 73 61 76 65 70 6f 69 f active savepoi
116ff 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 61 nts */. int nSa
11700 76 65 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 vepoint;
11701 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
11702 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 61 53 f elements in aS
11703 61 76 65 70 6f 69 6e 74 5b 5d 20 2a 2f 0a 20 20 avepoint[] */.
11704 63 68 61 72 20 64 62 46 69 6c 65 56 65 72 73 5b char dbFileVers[
11705 31 36 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 16]; /* C
11706 68 61 6e 67 65 73 20 77 68 65 6e 65 76 65 72 20 hanges whenever
11707 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 63 68 database file ch
11708 61 6e 67 65 73 20 2a 2f 0a 20 20 75 33 32 20 73 anges */. u32 s
11709 65 63 74 6f 72 53 69 7a 65 3b 20 20 20 20 20 20 ectorSize;
1170a 20 20 20 20 20 20 20 2f 2a 20 41 73 73 75 6d 65 /* Assume
1170b 64 20 73 65 63 74 6f 72 20 73 69 7a 65 20 64 75 d sector size du
1170c 72 69 6e 67 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f ring rollback */
1170d 0a 0a 20 20 75 31 36 20 6e 45 78 74 72 61 3b 20 .. u16 nExtra;
1170e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1170f 2f 2a 20 41 64 64 20 74 68 69 73 20 6d 61 6e 79 /* Add this many
11710 20 62 79 74 65 73 20 74 6f 20 65 61 63 68 20 69 bytes to each i
11711 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 20 2a 2f n-memory page */
11712 0a 20 20 69 31 36 20 6e 52 65 73 65 72 76 65 3b . i16 nReserve;
11713 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
11714 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 6e 75 73 * Number of unus
11715 65 64 20 62 79 74 65 73 20 61 74 20 65 6e 64 20 ed bytes at end
11716 6f 66 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a of each page */.
11717 20 20 75 33 32 20 76 66 73 46 6c 61 67 73 3b 20 u32 vfsFlags;
11718 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
11719 20 46 6c 61 67 73 20 66 6f 72 20 73 71 6c 69 74 Flags for sqlit
1171a 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a e3_vfs.xOpen() *
1171b 2f 0a 20 20 69 6e 74 20 70 61 67 65 53 69 7a 65 /. int pageSize
1171c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1171d 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 /* Number of byt
1171e 65 73 20 69 6e 20 61 20 70 61 67 65 20 2a 2f 0a es in a page */.
1171f 20 20 50 67 6e 6f 20 6d 78 50 67 6e 6f 3b 20 20 Pgno mxPgno;
11720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
11721 20 4d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 Maximum allowed
11722 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 size of the dat
11723 61 62 61 73 65 20 2a 2f 0a 20 20 63 68 61 72 20 abase */. char
11724 2a 7a 46 69 6c 65 6e 61 6d 65 3b 20 20 20 20 20 *zFilename;
11725 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
11726 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 f the database f
11727 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a ile */. char *z
11728 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 20 Journal;
11729 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 /* Name of
1172a 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
1172b 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 42 75 73 */. int (*xBus
1172c 79 48 61 6e 64 6c 65 72 29 28 76 6f 69 64 2a 29 yHandler)(void*)
1172d 3b 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f ; /* Function to
1172e 20 63 61 6c 6c 20 77 68 65 6e 20 62 75 73 79 20 call when busy
1172f 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 73 79 */. void *pBusy
11730 48 61 6e 64 6c 65 72 41 72 67 3b 20 20 20 20 20 HandlerArg;
11731 20 2f 2a 20 43 6f 6e 74 65 78 74 20 61 72 67 75 /* Context argu
11732 6d 65 6e 74 20 66 6f 72 20 78 42 75 73 79 48 61 ment for xBusyHa
11733 6e 64 6c 65 72 20 2a 2f 0a 23 69 66 64 65 66 20 ndler */.#ifdef
11734 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 6e SQLITE_TEST. in
11735 74 20 6e 48 69 74 2c 20 6e 4d 69 73 73 3b 20 20 t nHit, nMiss;
11736 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63 /* Cac
11737 68 65 20 68 69 74 73 20 61 6e 64 20 6d 69 73 73 he hits and miss
11738 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 ing */. int nRe
11739 61 64 2c 20 6e 57 72 69 74 65 3b 20 20 20 20 20 ad, nWrite;
1173a 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 /* Database
1173b 20 70 61 67 65 73 20 72 65 61 64 2f 77 72 69 74 pages read/writ
1173c 74 65 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 ten */.#endif.
1173d 76 6f 69 64 20 28 2a 78 52 65 69 6e 69 74 65 72 void (*xReiniter
1173e 29 28 44 62 50 61 67 65 2a 29 3b 20 2f 2a 20 43 )(DbPage*); /* C
1173f 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 all this routine
11740 20 77 68 65 6e 20 72 65 6c 6f 61 64 69 6e 67 20 when reloading
11741 70 61 67 65 73 20 2a 2f 0a 23 69 66 64 65 66 20 pages */.#ifdef
11742 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 SQLITE_HAS_CODEC
11743 0a 20 20 76 6f 69 64 20 2a 28 2a 78 43 6f 64 65 . void *(*xCode
11744 63 29 28 76 6f 69 64 2a 2c 76 6f 69 64 2a 2c 50 c)(void*,void*,P
11745 67 6e 6f 2c 69 6e 74 29 3b 20 2f 2a 20 52 6f 75 gno,int); /* Rou
11746 74 69 6e 65 20 66 6f 72 20 65 6e 2f 64 65 63 6f tine for en/deco
11747 64 69 6e 67 20 64 61 74 61 20 2a 2f 0a 20 20 76 ding data */. v
11748 6f 69 64 20 28 2a 78 43 6f 64 65 63 53 69 7a 65 oid (*xCodecSize
11749 43 68 6e 67 29 28 76 6f 69 64 2a 2c 69 6e 74 2c Chng)(void*,int,
1174a 69 6e 74 29 3b 20 2f 2a 20 4e 6f 74 69 66 79 20 int); /* Notify
1174b 6f 66 20 70 61 67 65 20 73 69 7a 65 20 63 68 61 of page size cha
1174c 6e 67 65 73 20 2a 2f 0a 20 20 76 6f 69 64 20 28 nges */. void (
1174d 2a 78 43 6f 64 65 63 46 72 65 65 29 28 76 6f 69 *xCodecFree)(voi
1174e 64 2a 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 d*);
1174f 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 /* Destructor f
11750 6f 72 20 74 68 65 20 63 6f 64 65 63 20 2a 2f 0a or the codec */.
11751 20 20 76 6f 69 64 20 2a 70 43 6f 64 65 63 3b 20 void *pCodec;
11752 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
11753 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 First argument
11754 74 6f 20 78 43 6f 64 65 63 2e 2e 2e 20 6d 65 74 to xCodec... met
11755 68 6f 64 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 hods */.#endif.
11756 20 63 68 61 72 20 2a 70 54 6d 70 53 70 61 63 65 char *pTmpSpace
11757 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
11758 50 61 67 65 72 2e 70 61 67 65 53 69 7a 65 20 62 Pager.pageSize b
11759 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 66 6f ytes of space fo
1175a 72 20 74 6d 70 20 75 73 65 20 2a 2f 0a 20 20 69 r tmp use */. i
1175b 36 34 20 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 64 journalSizeLi
1175c 6d 69 74 3b 20 20 20 20 20 20 20 2f 2a 20 53 69 mit; /* Si
1175d 7a 65 20 6c 69 6d 69 74 20 66 6f 72 20 70 65 72 ze limit for per
1175e 73 69 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 sistent journal
1175f 66 69 6c 65 73 20 2a 2f 0a 20 20 50 43 61 63 68 files */. PCach
11760 65 20 2a 70 50 43 61 63 68 65 3b 20 20 20 20 20 e *pPCache;
11761 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
11762 72 20 74 6f 20 70 61 67 65 20 63 61 63 68 65 20 r to page cache
11763 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 73 71 6c 69 object */. sqli
11764 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 42 61 63 te3_backup *pBac
11765 6b 75 70 3b 20 20 20 20 2f 2a 20 50 6f 69 6e 74 kup; /* Point
11766 65 72 20 74 6f 20 6c 69 73 74 20 6f 66 20 6f 6e er to list of on
11767 67 6f 69 6e 67 20 62 61 63 6b 75 70 20 70 72 6f going backup pro
11768 63 65 73 73 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a cesses */.};../*
11769 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
1176a 67 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c g global variabl
1176b 65 73 20 68 6f 6c 64 20 63 6f 75 6e 74 65 72 73 es hold counters
1176c 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 74 65 73 used for.** tes
1176d 74 69 6e 67 20 70 75 72 70 6f 73 65 73 20 6f 6e ting purposes on
1176e 6c 79 2e 20 20 54 68 65 73 65 20 76 61 72 69 61 ly. These varia
1176f 62 6c 65 73 20 64 6f 20 6e 6f 74 20 65 78 69 73 bles do not exis
11770 74 20 69 6e 0a 2a 2a 20 61 20 6e 6f 6e 2d 74 65 t in.** a non-te
11771 73 74 69 6e 67 20 62 75 69 6c 64 2e 20 20 54 68 sting build. Th
11772 65 73 65 20 76 61 72 69 61 62 6c 65 73 20 61 72 ese variables ar
11773 65 20 6e 6f 74 20 74 68 72 65 61 64 2d 73 61 66 e not thread-saf
11774 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c e..*/.#ifdef SQL
11775 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f ITE_TEST.SQLITE_
11776 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
11777 70 61 67 65 72 5f 72 65 61 64 64 62 5f 63 6f 75 pager_readdb_cou
11778 6e 74 20 3d 20 30 3b 20 20 20 20 2f 2a 20 4e 75 nt = 0; /* Nu
11779 6d 62 65 72 20 6f 66 20 66 75 6c 6c 20 70 61 67 mber of full pag
1177a 65 73 20 72 65 61 64 20 66 72 6f 6d 20 44 42 20 es read from DB
1177b 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
1177c 74 20 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f t sqlite3_pager_
1177d 77 72 69 74 65 64 62 5f 63 6f 75 6e 74 20 3d 20 writedb_count =
1177e 30 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 0; /* Number o
1177f 66 20 66 75 6c 6c 20 70 61 67 65 73 20 77 72 69 f full pages wri
11780 74 74 65 6e 20 74 6f 20 44 42 20 2a 2f 0a 53 51 tten to DB */.SQ
11781 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
11782 69 74 65 33 5f 70 61 67 65 72 5f 77 72 69 74 65 ite3_pager_write
11783 6a 5f 63 6f 75 6e 74 20 3d 20 30 3b 20 20 20 20 j_count = 0;
11784 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 /* Number of pag
11785 65 73 20 77 72 69 74 74 65 6e 20 74 6f 20 6a 6f es written to jo
11786 75 72 6e 61 6c 20 2a 2f 0a 23 20 64 65 66 69 6e urnal */.# defin
11787 65 20 50 41 47 45 52 5f 49 4e 43 52 28 76 29 20 e PAGER_INCR(v)
11788 20 76 2b 2b 0a 23 65 6c 73 65 0a 23 20 64 65 66 v++.#else.# def
11789 69 6e 65 20 50 41 47 45 52 5f 49 4e 43 52 28 76 ine PAGER_INCR(v
1178a 29 0a 23 65 6e 64 69 66 0a 0a 0a 0a 2f 2a 0a 2a ).#endif..../*.*
1178b 2a 20 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 * Journal files
1178c 62 65 67 69 6e 20 77 69 74 68 20 74 68 65 20 66 begin with the f
1178d 6f 6c 6c 6f 77 69 6e 67 20 6d 61 67 69 63 20 73 ollowing magic s
1178e 74 72 69 6e 67 2e 20 20 54 68 65 20 64 61 74 61 tring. The data
1178f 0a 2a 2a 20 77 61 73 20 6f 62 74 61 69 6e 65 64 .** was obtained
11790 20 66 72 6f 6d 20 2f 64 65 76 2f 72 61 6e 64 6f from /dev/rando
11791 6d 2e 20 20 49 74 20 69 73 20 75 73 65 64 20 6f m. It is used o
11792 6e 6c 79 20 61 73 20 61 20 73 61 6e 69 74 79 20 nly as a sanity
11793 63 68 65 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 69 6e check..**.** Sin
11794 63 65 20 76 65 72 73 69 6f 6e 20 32 2e 38 2e 30 ce version 2.8.0
11795 2c 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 6f , the journal fo
11796 72 6d 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 64 rmat contains ad
11797 64 69 74 69 6f 6e 61 6c 20 73 61 6e 69 74 79 0a ditional sanity.
11798 2a 2a 20 63 68 65 63 6b 69 6e 67 20 69 6e 66 6f ** checking info
11799 72 6d 61 74 69 6f 6e 2e 20 20 49 66 20 74 68 65 rmation. If the
1179a 20 70 6f 77 65 72 20 66 61 69 6c 73 20 77 68 69 power fails whi
1179b 6c 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 le the journal i
1179c 73 20 62 65 69 6e 67 0a 2a 2a 20 77 72 69 74 74 s being.** writt
1179d 65 6e 2c 20 73 65 6d 69 2d 72 61 6e 64 6f 6d 20 en, semi-random
1179e 67 61 72 62 61 67 65 20 64 61 74 61 20 6d 69 67 garbage data mig
1179f 68 74 20 61 70 70 65 61 72 20 69 6e 20 74 68 65 ht appear in the
117a0 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 journal.** file
117a1 20 61 66 74 65 72 20 70 6f 77 65 72 20 69 73 20 after power is
117a2 72 65 73 74 6f 72 65 64 2e 20 20 49 66 20 61 6e restored. If an
117a3 20 61 74 74 65 6d 70 74 20 69 73 20 74 68 65 6e attempt is then
117a4 20 6d 61 64 65 0a 2a 2a 20 74 6f 20 72 6f 6c 6c made.** to roll
117a5 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 62 61 63 the journal bac
117a6 6b 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 k, the database
117a7 63 6f 75 6c 64 20 62 65 20 63 6f 72 72 75 70 74 could be corrupt
117a8 65 64 2e 20 20 54 68 65 20 61 64 64 69 74 69 6f ed. The additio
117a9 6e 61 6c 0a 2a 2a 20 73 61 6e 69 74 79 20 63 68 nal.** sanity ch
117aa 65 63 6b 69 6e 67 20 64 61 74 61 20 69 73 20 61 ecking data is a
117ab 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 64 69 73 n attempt to dis
117ac 63 6f 76 65 72 20 74 68 65 20 67 61 72 62 61 67 cover the garbag
117ad 65 20 69 6e 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 e in the.** jour
117ae 6e 61 6c 20 61 6e 64 20 69 67 6e 6f 72 65 20 69 nal and ignore i
117af 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 6e t..**.** The san
117b0 69 74 79 20 63 68 65 63 6b 69 6e 67 20 69 6e 66 ity checking inf
117b1 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 ormation for the
117b2 20 6e 65 77 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 new journal for
117b3 6d 61 74 20 63 6f 6e 73 69 73 74 73 0a 2a 2a 20 mat consists.**
117b4 6f 66 20 61 20 33 32 2d 62 69 74 20 63 68 65 63 of a 32-bit chec
117b5 6b 73 75 6d 20 6f 6e 20 65 61 63 68 20 70 61 67 ksum on each pag
117b6 65 20 6f 66 20 64 61 74 61 2e 20 20 54 68 65 20 e of data. The
117b7 63 68 65 63 6b 73 75 6d 20 63 6f 76 65 72 73 20 checksum covers
117b8 62 6f 74 68 0a 2a 2a 20 74 68 65 20 70 61 67 65 both.** the page
117b9 20 6e 75 6d 62 65 72 20 61 6e 64 20 74 68 65 20 number and the
117ba 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
117bb 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 66 bytes of data f
117bc 6f 72 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 20 or the page..**
117bd 54 68 69 73 20 63 6b 73 75 6d 20 69 73 20 69 6e This cksum is in
117be 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 61 20 33 itialized to a 3
117bf 32 2d 62 69 74 20 72 61 6e 64 6f 6d 20 76 61 6c 2-bit random val
117c0 75 65 20 74 68 61 74 20 61 70 70 65 61 72 73 20 ue that appears
117c1 69 6e 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 in the.** journa
117c2 6c 20 66 69 6c 65 20 72 69 67 68 74 20 61 66 74 l file right aft
117c3 65 72 20 74 68 65 20 68 65 61 64 65 72 2e 20 20 er the header.
117c4 54 68 65 20 72 61 6e 64 6f 6d 20 69 6e 69 74 69 The random initi
117c5 61 6c 69 7a 65 72 20 69 73 20 69 6d 70 6f 72 74 alizer is import
117c6 61 6e 74 2c 0a 2a 2a 20 62 65 63 61 75 73 65 20 ant,.** because
117c7 67 61 72 62 61 67 65 20 64 61 74 61 20 74 68 61 garbage data tha
117c8 74 20 61 70 70 65 61 72 73 20 61 74 20 74 68 65 t appears at the
117c9 20 65 6e 64 20 6f 66 20 61 20 6a 6f 75 72 6e 61 end of a journa
117ca 6c 20 69 73 20 6c 69 6b 65 6c 79 0a 2a 2a 20 64 l is likely.** d
117cb 61 74 61 20 74 68 61 74 20 77 61 73 20 6f 6e 63 ata that was onc
117cc 65 20 69 6e 20 6f 74 68 65 72 20 66 69 6c 65 73 e in other files
117cd 20 74 68 61 74 20 68 61 76 65 20 6e 6f 77 20 62 that have now b
117ce 65 65 6e 20 64 65 6c 65 74 65 64 2e 20 20 49 66 een deleted. If
117cf 20 74 68 65 0a 2a 2a 20 67 61 72 62 61 67 65 20 the.** garbage
117d0 64 61 74 61 20 63 61 6d 65 20 66 72 6f 6d 20 61 data came from a
117d1 6e 20 6f 62 73 6f 6c 65 74 65 20 6a 6f 75 72 6e n obsolete journ
117d2 61 6c 20 66 69 6c 65 2c 20 74 68 65 20 63 68 65 al file, the che
117d3 63 6b 73 75 6d 73 20 6d 69 67 68 74 0a 2a 2a 20 cksums might.**
117d4 62 65 20 63 6f 72 72 65 63 74 2e 20 20 42 75 74 be correct. But
117d5 20 62 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 by initializing
117d6 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 74 6f the checksum to
117d7 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 77 68 random value wh
117d8 69 63 68 0a 2a 2a 20 69 73 20 64 69 66 66 65 72 ich.** is differ
117d9 65 6e 74 20 66 6f 72 20 65 76 65 72 79 20 6a 6f ent for every jo
117da 75 72 6e 61 6c 2c 20 77 65 20 6d 69 6e 69 6d 69 urnal, we minimi
117db 7a 65 20 74 68 61 74 20 72 69 73 6b 2e 0a 2a 2f ze that risk..*/
117dc 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e .static const un
117dd 73 69 67 6e 65 64 20 63 68 61 72 20 61 4a 6f 75 signed char aJou
117de 72 6e 61 6c 4d 61 67 69 63 5b 5d 20 3d 20 7b 0a rnalMagic[] = {.
117df 20 20 30 78 64 39 2c 20 30 78 64 35 2c 20 30 78 0xd9, 0xd5, 0x
117e0 30 35 2c 20 30 78 66 39 2c 20 30 78 32 30 2c 20 05, 0xf9, 0x20,
117e1 30 78 61 31 2c 20 30 78 36 33 2c 20 30 78 64 37 0xa1, 0x63, 0xd7
117e2 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ,.};../*.** The
117e3 73 69 7a 65 20 6f 66 20 74 68 65 20 6f 66 20 65 size of the of e
117e4 61 63 68 20 70 61 67 65 20 72 65 63 6f 72 64 20 ach page record
117e5 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 in the journal i
117e6 73 20 67 69 76 65 6e 20 62 79 0a 2a 2a 20 74 68 s given by.** th
117e7 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 e following macr
117e8 6f 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f o..*/.#define JO
117e9 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 URNAL_PG_SZ(pPag
117ea 65 72 29 20 20 28 28 70 50 61 67 65 72 2d 3e 70 er) ((pPager->p
117eb 61 67 65 53 69 7a 65 29 20 2b 20 38 29 0a 0a 2f ageSize) + 8)../
117ec 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c *.** The journal
117ed 20 68 65 61 64 65 72 20 73 69 7a 65 20 66 6f 72 header size for
117ee 20 74 68 69 73 20 70 61 67 65 72 2e 20 54 68 69 this pager. Thi
117ef 73 20 69 73 20 75 73 75 61 6c 6c 79 20 74 68 65 s is usually the
117f0 20 73 61 6d 65 20 0a 2a 2a 20 73 69 7a 65 20 61 same .** size a
117f1 73 20 61 20 73 69 6e 67 6c 65 20 64 69 73 6b 20 s a single disk
117f2 73 65 63 74 6f 72 2e 20 53 65 65 20 61 6c 73 6f sector. See also
117f3 20 73 65 74 53 65 63 74 6f 72 53 69 7a 65 28 29 setSectorSize()
117f4 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f 55 ..*/.#define JOU
117f5 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 RNAL_HDR_SZ(pPag
117f6 65 72 29 20 28 70 50 61 67 65 72 2d 3e 73 65 63 er) (pPager->sec
117f7 74 6f 72 53 69 7a 65 29 0a 0a 2f 2a 0a 2a 2a 20 torSize)../*.**
117f8 54 68 65 20 6d 61 63 72 6f 20 4d 45 4d 44 42 20 The macro MEMDB
117f9 69 73 20 74 72 75 65 20 69 66 20 77 65 20 61 72 is true if we ar
117fa 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61 e dealing with a
117fb 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 n in-memory data
117fc 62 61 73 65 2e 0a 2a 2a 20 57 65 20 64 6f 20 74 base..** We do t
117fd 68 69 73 20 61 73 20 61 20 6d 61 63 72 6f 20 73 his as a macro s
117fe 6f 20 74 68 61 74 20 69 66 20 74 68 65 20 53 51 o that if the SQ
117ff 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 LITE_OMIT_MEMORY
11800 44 42 20 6d 61 63 72 6f 20 69 73 20 73 65 74 2c DB macro is set,
11801 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 .** the value of
11802 20 4d 45 4d 44 42 20 77 69 6c 6c 20 62 65 20 61 MEMDB will be a
11803 20 63 6f 6e 73 74 61 6e 74 20 61 6e 64 20 74 68 constant and th
11804 65 20 63 6f 6d 70 69 6c 65 72 20 77 69 6c 6c 20 e compiler will
11805 6f 70 74 69 6d 69 7a 65 0a 2a 2a 20 6f 75 74 20 optimize.** out
11806 63 6f 64 65 20 74 68 61 74 20 77 6f 75 6c 64 20 code that would
11807 6e 65 76 65 72 20 65 78 65 63 75 74 65 2e 0a 2a never execute..*
11808 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
11809 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 0a 23 20 OMIT_MEMORYDB.#
1180a 64 65 66 69 6e 65 20 4d 45 4d 44 42 20 30 0a 23 define MEMDB 0.#
1180b 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 4d 45 else.# define ME
1180c 4d 44 42 20 70 50 61 67 65 72 2d 3e 6d 65 6d 44 MDB pPager->memD
1180d 62 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 b.#endif../*.**
1180e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 67 61 The maximum lega
1180f 6c 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 l page number is
11810 20 28 32 5e 33 31 20 2d 20 31 29 2e 0a 2a 2f 0a (2^31 - 1)..*/.
11811 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4d 41 #define PAGER_MA
11812 58 5f 50 47 4e 4f 20 32 31 34 37 34 38 33 36 34 X_PGNO 214748364
11813 37 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 7..#ifndef NDEBU
11814 47 20 0a 2f 2a 0a 2a 2a 20 55 73 61 67 65 3a 0a G ./*.** Usage:.
11815 2a 2a 0a 2a 2a 20 20 20 61 73 73 65 72 74 28 20 **.** assert(
11816 61 73 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61 assert_pager_sta
11817 74 65 28 70 50 61 67 65 72 29 20 29 3b 0a 2a 2f te(pPager) );.*/
11818 0a 73 74 61 74 69 63 20 69 6e 74 20 61 73 73 65 .static int asse
11819 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28 50 rt_pager_state(P
1181a 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 0a ager *pPager){..
1181b 20 20 2f 2a 20 41 20 74 65 6d 70 2d 66 69 6c 65 /* A temp-file
1181c 20 69 73 20 61 6c 77 61 79 73 20 69 6e 20 50 41 is always in PA
1181d 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 6f 72 GER_EXCLUSIVE or
1181e 20 50 41 47 45 52 5f 53 59 4e 43 45 44 20 73 74 PAGER_SYNCED st
1181f 61 74 65 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 ate. */. assert
11820 28 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 ( pPager->tempFi
11821 6c 65 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d le==0 || pPager-
11822 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 >state>=PAGER_EX
11823 43 4c 55 53 49 56 45 20 29 3b 0a 0a 20 20 2f 2a CLUSIVE );.. /*
11824 20 54 68 65 20 63 68 61 6e 67 65 43 6f 75 6e 74 The changeCount
11825 44 6f 6e 65 20 66 6c 61 67 20 69 73 20 61 6c 77 Done flag is alw
11826 61 79 73 20 73 65 74 20 66 6f 72 20 74 65 6d 70 ays set for temp
11827 2d 66 69 6c 65 73 20 2a 2f 0a 20 20 61 73 73 65 -files */. asse
11828 72 74 28 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 rt( pPager->temp
11829 46 69 6c 65 3d 3d 30 20 7c 7c 20 70 50 61 67 65 File==0 || pPage
1182a 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f r->changeCountDo
1182b 6e 65 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 ne );.. return
1182c 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 1;.}.#endif../*.
1182d 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 ** Return true i
1182e 66 20 69 74 20 69 73 20 6e 65 63 65 73 73 61 72 f it is necessar
1182f 79 20 74 6f 20 77 72 69 74 65 20 70 61 67 65 20 y to write page
11830 2a 70 50 67 20 69 6e 74 6f 20 74 68 65 20 73 75 *pPg into the su
11831 62 2d 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 41 20 b-journal..** A
11832 70 61 67 65 20 6e 65 65 64 73 20 74 6f 20 62 65 page needs to be
11833 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 written into th
11834 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 66 e sub-journal if
11835 20 74 68 65 72 65 20 65 78 69 73 74 73 20 6f 6e there exists on
11836 65 0a 2a 2a 20 6f 72 20 6d 6f 72 65 20 6f 70 65 e.** or more ope
11837 6e 20 73 61 76 65 70 6f 69 6e 74 73 20 66 6f 72 n savepoints for
11838 20 77 68 69 63 68 3a 0a 2a 2a 0a 2a 2a 20 20 20 which:.**.**
11839 2a 20 54 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 * The page-numbe
1183a 72 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f r is less than o
1183b 72 20 65 71 75 61 6c 20 74 6f 20 50 61 67 65 72 r equal to Pager
1183c 53 61 76 65 70 6f 69 6e 74 2e 6e 4f 72 69 67 2c Savepoint.nOrig,
1183d 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 54 68 65 20 and.** * The
1183e 62 69 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e bit correspondin
1183f 67 20 74 6f 20 74 68 65 20 70 61 67 65 2d 6e 75 g to the page-nu
11840 6d 62 65 72 20 69 73 20 6e 6f 74 20 73 65 74 20 mber is not set
11841 69 6e 0a 2a 2a 20 20 20 20 20 50 61 67 65 72 53 in.** PagerS
11842 61 76 65 70 6f 69 6e 74 2e 70 49 6e 53 61 76 65 avepoint.pInSave
11843 70 6f 69 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 point..*/.static
11844 20 69 6e 74 20 73 75 62 6a 52 65 71 75 69 72 65 int subjRequire
11845 73 50 61 67 65 28 50 67 48 64 72 20 2a 70 50 67 sPage(PgHdr *pPg
11846 29 7b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 20 3d ){. Pgno pgno =
11847 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20 50 61 pPg->pgno;. Pa
11848 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 ger *pPager = pP
11849 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 69 6e 74 g->pPager;. int
1184a 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 i;. for(i=0; i
1184b 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f <pPager->nSavepo
1184c 69 6e 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 50 int; i++){. P
1184d 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 70 agerSavepoint *p
1184e 20 3d 20 26 70 50 61 67 65 72 2d 3e 61 53 61 76 = &pPager->aSav
1184f 65 70 6f 69 6e 74 5b 69 5d 3b 0a 20 20 20 20 69 epoint[i];. i
11850 66 28 20 70 2d 3e 6e 4f 72 69 67 3e 3d 70 67 6e f( p->nOrig>=pgn
11851 6f 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 42 o && 0==sqlite3B
11852 69 74 76 65 63 54 65 73 74 28 70 2d 3e 70 49 6e itvecTest(p->pIn
11853 53 61 76 65 70 6f 69 6e 74 2c 20 70 67 6e 6f 29 Savepoint, pgno)
11854 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
11855 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 1;. }. }.
11856 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a return 0;.}../*.
11857 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 ** Return true i
11858 66 20 74 68 65 20 70 61 67 65 20 69 73 20 61 6c f the page is al
11859 72 65 61 64 79 20 69 6e 20 74 68 65 20 6a 6f 75 ready in the jou
1185a 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 rnal file..*/.st
1185b 61 74 69 63 20 69 6e 74 20 70 61 67 65 49 6e 4a atic int pageInJ
1185c 6f 75 72 6e 61 6c 28 50 67 48 64 72 20 2a 70 50 ournal(PgHdr *pP
1185d 67 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c g){. return sql
1185e 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 ite3BitvecTest(p
1185f 50 67 2d 3e 70 50 61 67 65 72 2d 3e 70 49 6e 4a Pg->pPager->pInJ
11860 6f 75 72 6e 61 6c 2c 20 70 50 67 2d 3e 70 67 6e ournal, pPg->pgn
11861 6f 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 o);.}../*.** Rea
11862 64 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 d a 32-bit integ
11863 65 72 20 66 72 6f 6d 20 74 68 65 20 67 69 76 65 er from the give
11864 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f n file descripto
11865 72 2e 20 20 53 74 6f 72 65 20 74 68 65 20 69 6e r. Store the in
11866 74 65 67 65 72 0a 2a 2a 20 74 68 61 74 20 69 73 teger.** that is
11867 20 72 65 61 64 20 69 6e 20 2a 70 52 65 73 2e 20 read in *pRes.
11868 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f Return SQLITE_O
11869 4b 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 K if everything
1186a 77 6f 72 6b 65 64 2c 20 6f 72 20 61 6e 0a 2a 2a worked, or an.**
1186b 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73 error code is s
1186c 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 omething goes wr
1186d 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 76 ong..**.** All v
1186e 61 6c 75 65 73 20 61 72 65 20 73 74 6f 72 65 64 alues are stored
1186f 20 6f 6e 20 64 69 73 6b 20 61 73 20 62 69 67 2d on disk as big-
11870 65 6e 64 69 61 6e 2e 0a 2a 2f 0a 73 74 61 74 69 endian..*/.stati
11871 63 20 69 6e 74 20 72 65 61 64 33 32 62 69 74 73 c int read32bits
11872 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 (sqlite3_file *f
11873 64 2c 20 69 36 34 20 6f 66 66 73 65 74 2c 20 75 d, i64 offset, u
11874 33 32 20 2a 70 52 65 73 29 7b 0a 20 20 75 6e 73 32 *pRes){. uns
11875 69 67 6e 65 64 20 63 68 61 72 20 61 63 5b 34 5d igned char ac[4]
11876 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c ;. int rc = sql
11877 69 74 65 33 4f 73 52 65 61 64 28 66 64 2c 20 61 ite3OsRead(fd, a
11878 63 2c 20 73 69 7a 65 6f 66 28 61 63 29 2c 20 6f c, sizeof(ac), o
11879 66 66 73 65 74 29 3b 0a 20 20 69 66 28 20 72 63 ffset);. if( rc
1187a 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1187b 20 20 20 2a 70 52 65 73 20 3d 20 73 71 6c 69 74 *pRes = sqlit
1187c 65 33 47 65 74 34 62 79 74 65 28 61 63 29 3b 0a e3Get4byte(ac);.
1187d 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
1187e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 .}../*.** Write
1187f 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 a 32-bit integer
11880 20 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 62 into a string b
11881 75 66 66 65 72 20 69 6e 20 62 69 67 2d 65 6e 64 uffer in big-end
11882 69 61 6e 20 62 79 74 65 20 6f 72 64 65 72 2e 0a ian byte order..
11883 2a 2f 0a 23 64 65 66 69 6e 65 20 70 75 74 33 32 */.#define put32
11884 62 69 74 73 28 41 2c 42 29 20 20 73 71 6c 69 74 bits(A,B) sqlit
11885 65 33 50 75 74 34 62 79 74 65 28 28 75 38 2a 29 e3Put4byte((u8*)
11886 41 2c 42 29 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 A,B)../*.** Writ
11887 65 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 e a 32-bit integ
11888 65 72 20 69 6e 74 6f 20 74 68 65 20 67 69 76 65 er into the give
11889 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f n file descripto
1188a 72 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 r. Return SQLIT
1188b 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 E_OK.** on succe
1188c 73 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 ss or an error c
1188d 6f 64 65 20 69 73 20 73 6f 6d 65 74 68 69 6e 67 ode is something
1188e 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2f 0a goes wrong..*/.
1188f 73 74 61 74 69 63 20 69 6e 74 20 77 72 69 74 65 static int write
11890 33 32 62 69 74 73 28 73 71 6c 69 74 65 33 5f 66 32bits(sqlite3_f
11891 69 6c 65 20 2a 66 64 2c 20 69 36 34 20 6f 66 66 ile *fd, i64 off
11892 73 65 74 2c 20 75 33 32 20 76 61 6c 29 7b 0a 20 set, u32 val){.
11893 20 63 68 61 72 20 61 63 5b 34 5d 3b 0a 20 20 70 char ac[4];. p
11894 75 74 33 32 62 69 74 73 28 61 63 2c 20 76 61 6c ut32bits(ac, val
11895 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 );. return sqli
11896 74 65 33 4f 73 57 72 69 74 65 28 66 64 2c 20 61 te3OsWrite(fd, a
11897 63 2c 20 34 2c 20 6f 66 66 73 65 74 29 3b 0a 7d c, 4, offset);.}
11898 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75 ../*.** The argu
11899 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 6d 61 63 ment to this mac
1189a 72 6f 20 69 73 20 61 20 66 69 6c 65 20 64 65 73 ro is a file des
1189b 63 72 69 70 74 6f 72 20 28 74 79 70 65 20 73 71 criptor (type sq
1189c 6c 69 74 65 33 5f 66 69 6c 65 2a 29 2e 0a 2a 2a lite3_file*)..**
1189d 20 52 65 74 75 72 6e 20 30 20 69 66 20 69 74 20 Return 0 if it
1189e 69 73 20 6e 6f 74 20 6f 70 65 6e 2c 20 6f 72 20 is not open, or
1189f 6e 6f 6e 2d 7a 65 72 6f 20 28 62 75 74 20 6e 6f non-zero (but no
118a0 74 20 31 29 20 69 66 20 69 74 20 69 73 2e 0a 2a t 1) if it is..*
118a1 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 73 6f 20 *.** This is so
118a2 74 68 61 74 20 65 78 70 72 65 73 73 69 6f 6e 73 that expressions
118a3 20 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 can be written
118a4 61 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 66 28 20 as:.**.** if(
118a5 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a isOpen(pPager->j
118a6 66 64 29 20 29 7b 20 2e 2e 2e 0a 2a 2a 0a 2a 2a fd) ){ ....**.**
118a7 20 69 6e 73 74 65 61 64 20 6f 66 0a 2a 2a 0a 2a instead of.**.*
118a8 2a 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e * if( pPager->
118a9 6a 66 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b jfd->pMethods ){
118aa 20 2e 2e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ....*/.#define
118ab 69 73 4f 70 65 6e 28 70 46 64 29 20 28 28 70 46 isOpen(pFd) ((pF
118ac 64 29 2d 3e 70 4d 65 74 68 6f 64 73 29 0a 0a 2f d)->pMethods)../
118ad 2a 0a 2a 2a 20 49 66 20 66 69 6c 65 20 70 46 64 *.** If file pFd
118ae 20 69 73 20 6f 70 65 6e 2c 20 63 61 6c 6c 20 73 is open, call s
118af 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 qlite3OsUnlock()
118b0 20 6f 6e 20 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 on it..*/.stati
118b1 63 20 69 6e 74 20 6f 73 55 6e 6c 6f 63 6b 28 73 c int osUnlock(s
118b2 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 64 qlite3_file *pFd
118b3 2c 20 69 6e 74 20 65 4c 6f 63 6b 29 7b 0a 20 20 , int eLock){.
118b4 69 66 28 20 21 69 73 4f 70 65 6e 28 70 46 64 29 if( !isOpen(pFd)
118b5 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
118b6 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 QLITE_OK;. }.
118b7 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4f 73 return sqlite3Os
118b8 55 6e 6c 6f 63 6b 28 70 46 64 2c 20 65 4c 6f 63 Unlock(pFd, eLoc
118b9 6b 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 k);.}../*.** Thi
118ba 73 20 66 75 6e 63 74 69 6f 6e 20 64 65 74 65 72 s function deter
118bb 6d 69 6e 65 73 20 77 68 65 74 68 65 72 20 6f 72 mines whether or
118bc 20 6e 6f 74 20 74 68 65 20 61 74 6f 6d 69 63 2d not the atomic-
118bd 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 write optimizati
118be 6f 6e 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 on.** can be use
118bf 64 20 77 69 74 68 20 74 68 69 73 20 70 61 67 65 d with this page
118c0 72 2e 20 54 68 65 20 6f 70 74 69 6d 69 7a 61 74 r. The optimizat
118c1 69 6f 6e 20 63 61 6e 20 62 65 20 75 73 65 64 20 ion can be used
118c2 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 28 61 29 20 74 if:.**.** (a) t
118c3 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 he value returne
118c4 64 20 62 79 20 4f 73 44 65 76 69 63 65 43 68 61 d by OsDeviceCha
118c5 72 61 63 74 65 72 69 73 74 69 63 73 28 29 20 69 racteristics() i
118c6 6e 64 69 63 61 74 65 73 20 74 68 61 74 0a 2a 2a ndicates that.**
118c7 20 20 20 20 20 20 61 20 64 61 74 61 62 61 73 65 a database
118c8 20 70 61 67 65 20 6d 61 79 20 62 65 20 77 72 69 page may be wri
118c9 74 74 65 6e 20 61 74 6f 6d 69 63 61 6c 6c 79 2c tten atomically,
118ca 20 61 6e 64 0a 2a 2a 20 20 28 62 29 20 74 68 65 and.** (b) the
118cb 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
118cc 62 79 20 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 by OsSectorSize(
118cd 29 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f ) is less than o
118ce 72 20 65 71 75 61 6c 0a 2a 2a 20 20 20 20 20 20 r equal.**
118cf 74 6f 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 to the page size
118d0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70 74 69 ..**.** The opti
118d1 6d 69 7a 61 74 69 6f 6e 20 69 73 20 61 6c 73 6f mization is also
118d2 20 61 6c 77 61 79 73 20 65 6e 61 62 6c 65 64 20 always enabled
118d3 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 66 69 for temporary fi
118d4 6c 65 73 2e 20 49 74 20 69 73 0a 2a 2a 20 61 6e les. It is.** an
118d5 20 65 72 72 6f 72 20 74 6f 20 63 61 6c 6c 20 74 error to call t
118d6 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 66 20 his function if
118d7 70 50 61 67 65 72 20 69 73 20 6f 70 65 6e 65 64 pPager is opened
118d8 20 6f 6e 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 on an in-memory
118d9 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a .** database..**
118da 0a 2a 2a 20 49 66 20 74 68 65 20 6f 70 74 69 6d .** If the optim
118db 69 7a 61 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 62 ization cannot b
118dc 65 20 75 73 65 64 2c 20 30 20 69 73 20 72 65 74 e used, 0 is ret
118dd 75 72 6e 65 64 2e 20 49 66 20 69 74 20 63 61 6e urned. If it can
118de 20 62 65 20 75 73 65 64 2c 0a 2a 2a 20 74 68 65 be used,.** the
118df 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 n the value retu
118e0 72 6e 65 64 20 69 73 20 74 68 65 20 73 69 7a 65 rned is the size
118e1 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 of the journal
118e2 66 69 6c 65 20 77 68 65 6e 20 69 74 0a 2a 2a 20 file when it.**
118e3 63 6f 6e 74 61 69 6e 73 20 72 6f 6c 6c 62 61 63 contains rollbac
118e4 6b 20 64 61 74 61 20 66 6f 72 20 65 78 61 63 74 k data for exact
118e5 6c 79 20 6f 6e 65 20 70 61 67 65 2e 0a 2a 2f 0a ly one page..*/.
118e6 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e #ifdef SQLITE_EN
118e7 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 ABLE_ATOMIC_WRIT
118e8 45 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e E.static int jrn
118e9 6c 42 75 66 66 65 72 53 69 7a 65 28 50 61 67 65 lBufferSize(Page
118ea 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 61 73 r *pPager){. as
118eb 73 65 72 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a sert( !MEMDB );.
118ec 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 74 if( !pPager->t
118ed 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20 20 69 empFile ){. i
118ee 6e 74 20 64 63 3b 20 20 20 20 20 20 20 20 20 20 nt dc;
118ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
118f0 20 2f 2a 20 44 65 76 69 63 65 20 63 68 61 72 61 /* Device chara
118f1 63 74 65 72 69 73 74 69 63 73 20 2a 2f 0a 20 20 cteristics */.
118f2 20 20 69 6e 74 20 6e 53 65 63 74 6f 72 3b 20 20 int nSector;
118f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
118f4 20 20 20 20 2f 2a 20 53 65 63 74 6f 72 20 73 69 /* Sector si
118f5 7a 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 7a ze */. int sz
118f6 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 Page;
118f7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
118f8 61 67 65 20 73 69 7a 65 20 2a 2f 0a 0a 20 20 20 age size */..
118f9 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 assert( isOpen(
118fa 70 50 61 67 65 72 2d 3e 66 64 29 20 29 3b 0a 20 pPager->fd) );.
118fb 20 20 20 64 63 20 3d 20 73 71 6c 69 74 65 33 4f dc = sqlite3O
118fc 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 sDeviceCharacter
118fd 69 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 istics(pPager->f
118fe 64 29 3b 0a 20 20 20 20 6e 53 65 63 74 6f 72 20 d);. nSector
118ff 3d 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 = pPager->sector
11900 53 69 7a 65 3b 0a 20 20 20 20 73 7a 50 61 67 65 Size;. szPage
11901 20 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 = pPager->pageS
11902 69 7a 65 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 ize;.. assert
11903 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 (SQLITE_IOCAP_AT
11904 4f 4d 49 43 35 31 32 3d 3d 28 35 31 32 3e 3e 38 OMIC512==(512>>8
11905 29 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 53 ));. assert(S
11906 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d QLITE_IOCAP_ATOM
11907 49 43 36 34 4b 3d 3d 28 36 35 35 33 36 3e 3e 38 IC64K==(65536>>8
11908 29 29 3b 0a 20 20 20 20 69 66 28 20 30 3d 3d 28 ));. if( 0==(
11909 64 63 26 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 dc&(SQLITE_IOCAP
1190a 5f 41 54 4f 4d 49 43 7c 28 73 7a 50 61 67 65 3e _ATOMIC|(szPage>
1190b 3e 38 29 29 20 7c 7c 20 6e 53 65 63 74 6f 72 3e >8)) || nSector>
1190c 73 7a 50 61 67 65 29 20 29 7b 0a 20 20 20 20 20 szPage) ){.
1190d 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d return 0;. }
1190e 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 4a . }.. return J
1190f 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 OURNAL_HDR_SZ(pP
11910 61 67 65 72 29 20 2b 20 4a 4f 55 52 4e 41 4c 5f ager) + JOURNAL_
11911 50 47 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 7d PG_SZ(pPager);.}
11912 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 .#endif../*.** I
11913 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 f SQLITE_CHECK_P
11914 41 47 45 53 20 69 73 20 64 65 66 69 6e 65 64 20 AGES is defined
11915 74 68 65 6e 20 77 65 20 64 6f 20 73 6f 6d 65 20 then we do some
11916 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 0a sanity checking.
11917 2a 2a 20 6f 6e 20 74 68 65 20 63 61 63 68 65 20 ** on the cache
11918 75 73 69 6e 67 20 61 20 68 61 73 68 20 66 75 6e using a hash fun
11919 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 ction. This is
1191a 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 used for testing
1191b 0a 2a 2a 20 61 6e 64 20 64 65 62 75 67 67 69 6e .** and debuggin
1191c 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a 23 69 66 64 65 g only..*/.#ifde
1191d 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 f SQLITE_CHECK_P
1191e 41 47 45 53 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 AGES./*.** Retur
1191f 6e 20 61 20 33 32 2d 62 69 74 20 68 61 73 68 20 n a 32-bit hash
11920 6f 66 20 74 68 65 20 70 61 67 65 20 64 61 74 61 of the page data
11921 20 66 6f 72 20 70 50 61 67 65 2e 0a 2a 2f 0a 73 for pPage..*/.s
11922 74 61 74 69 63 20 75 33 32 20 70 61 67 65 72 5f tatic u32 pager_
11923 64 61 74 61 68 61 73 68 28 69 6e 74 20 6e 42 79 datahash(int nBy
11924 74 65 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 te, unsigned cha
11925 72 20 2a 70 44 61 74 61 29 7b 0a 20 20 75 33 32 r *pData){. u32
11926 20 68 61 73 68 20 3d 20 30 3b 0a 20 20 69 6e 74 hash = 0;. int
11927 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 i;. for(i=0; i
11928 3c 6e 42 79 74 65 3b 20 69 2b 2b 29 7b 0a 20 20 <nByte; i++){.
11929 20 20 68 61 73 68 20 3d 20 28 68 61 73 68 2a 31 hash = (hash*1
1192a 30 33 39 29 20 2b 20 70 44 61 74 61 5b 69 5d 3b 039) + pData[i];
1192b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 61 . }. return ha
1192c 73 68 3b 0a 7d 0a 73 74 61 74 69 63 20 75 33 32 sh;.}.static u32
1192d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 pager_pagehash(
1192e 50 67 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 PgHdr *pPage){.
1192f 20 72 65 74 75 72 6e 20 70 61 67 65 72 5f 64 61 return pager_da
11930 74 61 68 61 73 68 28 70 50 61 67 65 2d 3e 70 50 tahash(pPage->pP
11931 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 ager->pageSize,
11932 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a (unsigned char *
11933 29 70 50 61 67 65 2d 3e 70 44 61 74 61 29 3b 0a )pPage->pData);.
11934 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 }.static void pa
11935 67 65 72 5f 73 65 74 5f 70 61 67 65 68 61 73 68 ger_set_pagehash
11936 28 50 67 48 64 72 20 2a 70 50 61 67 65 29 7b 0a (PgHdr *pPage){.
11937 20 20 70 50 61 67 65 2d 3e 70 61 67 65 48 61 73 pPage->pageHas
11938 68 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 h = pager_pageha
11939 73 68 28 70 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a sh(pPage);.}../*
1193a 0a 2a 2a 20 54 68 65 20 43 48 45 43 4b 5f 50 41 .** The CHECK_PA
1193b 47 45 20 6d 61 63 72 6f 20 74 61 6b 65 73 20 61 GE macro takes a
1193c 20 50 67 48 64 72 2a 20 61 73 20 61 6e 20 61 72 PgHdr* as an ar
1193d 67 75 6d 65 6e 74 2e 20 49 66 20 53 51 4c 49 54 gument. If SQLIT
1193e 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 2a 2a E_CHECK_PAGES.**
1193f 20 69 73 20 64 65 66 69 6e 65 64 2c 20 61 6e 64 is defined, and
11940 20 4e 44 45 42 55 47 20 69 73 20 6e 6f 74 20 64 NDEBUG is not d
11941 65 66 69 6e 65 64 2c 20 61 6e 20 61 73 73 65 72 efined, an asser
11942 74 28 29 20 73 74 61 74 65 6d 65 6e 74 20 63 68 t() statement ch
11943 65 63 6b 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 ecks.** that the
11944 20 70 61 67 65 20 69 73 20 65 69 74 68 65 72 20 page is either
11945 64 69 72 74 79 20 6f 72 20 73 74 69 6c 6c 20 6d dirty or still m
11946 61 74 63 68 65 73 20 74 68 65 20 63 61 6c 63 75 atches the calcu
11947 6c 61 74 65 64 20 70 61 67 65 2d 68 61 73 68 2e lated page-hash.
11948 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 48 45 43 .*/.#define CHEC
11949 4b 5f 50 41 47 45 28 78 29 20 63 68 65 63 6b 50 K_PAGE(x) checkP
1194a 61 67 65 28 78 29 0a 73 74 61 74 69 63 20 76 6f age(x).static vo
1194b 69 64 20 63 68 65 63 6b 50 61 67 65 28 50 67 48 id checkPage(PgH
1194c 64 72 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 dr *pPg){. Page
1194d 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d r *pPager = pPg-
1194e 3e 70 50 61 67 65 72 3b 0a 20 20 61 73 73 65 72 >pPager;. asser
1194f 74 28 20 21 70 50 67 2d 3e 70 61 67 65 48 61 73 t( !pPg->pageHas
11950 68 20 7c 7c 20 70 50 61 67 65 72 2d 3e 65 72 72 h || pPager->err
11951 43 6f 64 65 0a 20 20 20 20 20 20 7c 7c 20 28 70 Code. || (p
11952 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f Pg->flags&PGHDR_
11953 44 49 52 54 59 29 20 7c 7c 20 70 50 67 2d 3e 70 DIRTY) || pPg->p
11954 61 67 65 48 61 73 68 3d 3d 70 61 67 65 72 5f 70 ageHash==pager_p
11955 61 67 65 68 61 73 68 28 70 50 67 29 20 29 3b 0a agehash(pPg) );.
11956 7d 0a 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 }..#else.#define
11957 20 70 61 67 65 72 5f 64 61 74 61 68 61 73 68 28 pager_datahash(
11958 58 2c 59 29 20 20 30 0a 23 64 65 66 69 6e 65 20 X,Y) 0.#define
11959 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 58 pager_pagehash(X
1195a 29 20 20 30 0a 23 64 65 66 69 6e 65 20 43 48 45 ) 0.#define CHE
1195b 43 4b 5f 50 41 47 45 28 78 29 0a 23 65 6e 64 69 CK_PAGE(x).#endi
1195c 66 20 20 2f 2a 20 53 51 4c 49 54 45 5f 43 48 45 f /* SQLITE_CHE
1195d 43 4b 5f 50 41 47 45 53 20 2a 2f 0a 0a 2f 2a 0a CK_PAGES */../*.
1195e 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 69 73 20 ** When this is
1195f 63 61 6c 6c 65 64 20 74 68 65 20 6a 6f 75 72 6e called the journ
11960 61 6c 20 66 69 6c 65 20 66 6f 72 20 70 61 67 65 al file for page
11961 72 20 70 50 61 67 65 72 20 6d 75 73 74 20 62 65 r pPager must be
11962 20 6f 70 65 6e 2e 0a 2a 2a 20 54 68 69 73 20 66 open..** This f
11963 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 unction attempts
11964 20 74 6f 20 72 65 61 64 20 61 20 6d 61 73 74 65 to read a maste
11965 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e r journal file n
11966 61 6d 65 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a ame from the .**
11967 20 65 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 end of the file
11968 20 61 6e 64 2c 20 69 66 20 73 75 63 63 65 73 73 and, if success
11969 66 75 6c 2c 20 63 6f 70 69 65 73 20 69 74 20 69 ful, copies it i
1196a 6e 74 6f 20 6d 65 6d 6f 72 79 20 73 75 70 70 6c nto memory suppl
1196b 69 65 64 20 0a 2a 2a 20 62 79 20 74 68 65 20 63 ied .** by the c
1196c 61 6c 6c 65 72 2e 20 53 65 65 20 63 6f 6d 6d 65 aller. See comme
1196d 6e 74 73 20 61 62 6f 76 65 20 77 72 69 74 65 4d nts above writeM
1196e 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 29 20 66 asterJournal() f
1196f 6f 72 20 74 68 65 20 66 6f 72 6d 61 74 0a 2a 2a or the format.**
11970 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 61 used to store a
11971 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
11972 66 69 6c 65 20 6e 61 6d 65 20 61 74 20 74 68 65 file name at the
11973 20 65 6e 64 20 6f 66 20 61 20 6a 6f 75 72 6e 61 end of a journa
11974 6c 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7a 4d l file..**.** zM
11975 61 73 74 65 72 20 6d 75 73 74 20 70 6f 69 6e 74 aster must point
11976 20 74 6f 20 61 20 62 75 66 66 65 72 20 6f 66 20 to a buffer of
11977 61 74 20 6c 65 61 73 74 20 6e 4d 61 73 74 65 72 at least nMaster
11978 20 62 79 74 65 73 20 61 6c 6c 6f 63 61 74 65 64 bytes allocated
11979 20 62 79 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65 by.** the calle
1197a 72 2e 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62 r. This should b
1197b 65 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 6d 78 e sqlite3_vfs.mx
1197c 50 61 74 68 6e 61 6d 65 2b 31 20 28 74 6f 20 65 Pathname+1 (to e
1197d 6e 73 75 72 65 20 74 68 65 72 65 20 69 73 0a 2a nsure there is.*
1197e 2a 20 65 6e 6f 75 67 68 20 73 70 61 63 65 20 74 * enough space t
1197f 6f 20 77 72 69 74 65 20 74 68 65 20 6d 61 73 74 o write the mast
11980 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 29 er journal name)
11981 2e 20 49 66 20 74 68 65 20 6d 61 73 74 65 72 20 . If the master
11982 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 6e 61 6d 65 20 journal.** name
11983 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 in the journal i
11984 73 20 6c 6f 6e 67 65 72 20 74 68 61 6e 20 6e 4d s longer than nM
11985 61 73 74 65 72 20 62 79 74 65 73 20 28 69 6e 63 aster bytes (inc
11986 6c 75 64 69 6e 67 20 61 0a 2a 2a 20 6e 75 6c 2d luding a.** nul-
11987 74 65 72 6d 69 6e 61 74 6f 72 29 2c 20 74 68 65 terminator), the
11988 6e 20 74 68 69 73 20 69 73 20 68 61 6e 64 6c 65 n this is handle
11989 64 20 61 73 20 69 66 20 6e 6f 20 6d 61 73 74 65 d as if no maste
1198a 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 0a 2a r journal name.*
1198b 2a 20 77 65 72 65 20 70 72 65 73 65 6e 74 20 69 * were present i
1198c 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a n the journal..*
1198d 2a 0a 2a 2a 20 49 66 20 61 20 6d 61 73 74 65 72 *.** If a master
1198e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 journal file na
1198f 6d 65 20 69 73 20 70 72 65 73 65 6e 74 20 61 74 me is present at
11990 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 the end of the
11991 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 2c journal.** file,
11992 20 74 68 65 6e 20 69 74 20 69 73 20 63 6f 70 69 then it is copi
11993 65 64 20 69 6e 74 6f 20 74 68 65 20 62 75 66 66 ed into the buff
11994 65 72 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 er pointed to by
11995 20 7a 4d 61 73 74 65 72 2e 20 41 0a 2a 2a 20 6e zMaster. A.** n
11996 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 20 62 79 ul-terminator by
11997 74 65 20 69 73 20 61 70 70 65 6e 64 65 64 20 74 te is appended t
11998 6f 20 74 68 65 20 62 75 66 66 65 72 20 66 6f 6c o the buffer fol
11999 6c 6f 77 69 6e 67 20 74 68 65 20 6d 61 73 74 65 lowing the maste
1199a 72 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c r.** journal fil
1199b 65 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 e name..**.** If
1199c 20 69 74 20 69 73 20 64 65 74 65 72 6d 69 6e 65 it is determine
1199d 64 20 74 68 61 74 20 6e 6f 20 6d 61 73 74 65 72 d that no master
1199e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 journal file na
1199f 6d 65 20 69 73 20 70 72 65 73 65 6e 74 20 0a 2a me is present .*
119a0 2a 20 7a 4d 61 73 74 65 72 5b 30 5d 20 69 73 20 * zMaster[0] is
119a1 73 65 74 20 74 6f 20 30 20 61 6e 64 20 53 51 4c set to 0 and SQL
119a2 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e ITE_OK returned.
119a3 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 .**.** If an err
119a4 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 or occurs while
119a5 72 65 61 64 69 6e 67 20 66 72 6f 6d 20 74 68 65 reading from the
119a6 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 61 journal file, a
119a7 6e 20 53 51 4c 69 74 65 0a 2a 2a 20 65 72 72 6f n SQLite.** erro
119a8 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e r code is return
119a9 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ed..*/.static in
119aa 74 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 t readMasterJour
119ab 6e 61 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 nal(sqlite3_file
119ac 20 2a 70 4a 72 6e 6c 2c 20 63 68 61 72 20 2a 7a *pJrnl, char *z
119ad 4d 61 73 74 65 72 2c 20 75 33 32 20 6e 4d 61 73 Master, u32 nMas
119ae 74 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 ter){. int rc;
119af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
119b0 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 /* Return cod
119b1 65 20 2a 2f 0a 20 20 75 33 32 20 6c 65 6e 3b 20 e */. u32 len;
119b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
119b3 20 20 2f 2a 20 4c 65 6e 67 74 68 20 69 6e 20 62 /* Length in b
119b4 79 74 65 73 20 6f 66 20 6d 61 73 74 65 72 20 6a ytes of master j
119b5 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20 ournal name */.
119b6 20 69 36 34 20 73 7a 4a 3b 20 20 20 20 20 20 20 i64 szJ;
119b7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
119b8 6f 74 61 6c 20 73 69 7a 65 20 69 6e 20 62 79 74 otal size in byt
119b9 65 73 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 66 69 es of journal fi
119ba 6c 65 20 70 4a 72 6e 6c 20 2a 2f 0a 20 20 75 33 le pJrnl */. u3
119bb 32 20 63 6b 73 75 6d 3b 20 20 20 20 20 20 20 20 2 cksum;
119bc 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 4a 20 63 /* MJ c
119bd 68 65 63 6b 73 75 6d 20 76 61 6c 75 65 20 72 65 hecksum value re
119be 61 64 20 66 72 6f 6d 20 6a 6f 75 72 6e 61 6c 20 ad from journal
119bf 2a 2f 0a 20 20 75 33 32 20 75 3b 20 20 20 20 20 */. u32 u;
119c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
119c1 2f 2a 20 55 6e 73 69 67 6e 65 64 20 6c 6f 6f 70 /* Unsigned loop
119c2 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 6e counter */. un
119c3 73 69 67 6e 65 64 20 63 68 61 72 20 61 4d 61 67 signed char aMag
119c4 69 63 5b 38 5d 3b 20 20 20 2f 2a 20 41 20 62 75 ic[8]; /* A bu
119c5 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 ffer to hold the
119c6 20 6d 61 67 69 63 20 68 65 61 64 65 72 20 2a 2f magic header */
119c7 0a 20 20 7a 4d 61 73 74 65 72 5b 30 5d 20 3d 20 . zMaster[0] =
119c8 27 5c 30 27 3b 0a 0a 20 20 69 66 28 20 53 51 4c '\0';.. if( SQL
119c9 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 ITE_OK!=(rc = sq
119ca 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 lite3OsFileSize(
119cb 70 4a 72 6e 6c 2c 20 26 73 7a 4a 29 29 0a 20 20 pJrnl, &szJ)).
119cc 20 7c 7c 20 73 7a 4a 3c 31 36 0a 20 20 20 7c 7c || szJ<16. ||
119cd 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 SQLITE_OK!=(rc
119ce 3d 20 72 65 61 64 33 32 62 69 74 73 28 70 4a 72 = read32bits(pJr
119cf 6e 6c 2c 20 73 7a 4a 2d 31 36 2c 20 26 6c 65 6e nl, szJ-16, &len
119d0 29 29 0a 20 20 20 7c 7c 20 6c 65 6e 3e 3d 6e 4d )). || len>=nM
119d1 61 73 74 65 72 20 0a 20 20 20 7c 7c 20 53 51 4c aster . || SQL
119d2 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 ITE_OK!=(rc = re
119d3 61 64 33 32 62 69 74 73 28 70 4a 72 6e 6c 2c 20 ad32bits(pJrnl,
119d4 73 7a 4a 2d 31 32 2c 20 26 63 6b 73 75 6d 29 29 szJ-12, &cksum))
119d5 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b . || SQLITE_OK
119d6 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f !=(rc = sqlite3O
119d7 73 52 65 61 64 28 70 4a 72 6e 6c 2c 20 61 4d 61 sRead(pJrnl, aMa
119d8 67 69 63 2c 20 38 2c 20 73 7a 4a 2d 38 29 29 0a gic, 8, szJ-8)).
119d9 20 20 20 7c 7c 20 6d 65 6d 63 6d 70 28 61 4d 61 || memcmp(aMa
119da 67 69 63 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 gic, aJournalMag
119db 69 63 2c 20 38 29 0a 20 20 20 7c 7c 20 53 51 4c ic, 8). || SQL
119dc 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 ITE_OK!=(rc = sq
119dd 6c 69 74 65 33 4f 73 52 65 61 64 28 70 4a 72 6e lite3OsRead(pJrn
119de 6c 2c 20 7a 4d 61 73 74 65 72 2c 20 6c 65 6e 2c l, zMaster, len,
119df 20 73 7a 4a 2d 31 36 2d 6c 65 6e 29 29 0a 20 20 szJ-16-len)).
119e0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 ){. return rc
119e1 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 65 20 ;. }.. /* See
119e2 69 66 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 if the checksum
119e3 6d 61 74 63 68 65 73 20 74 68 65 20 6d 61 73 74 matches the mast
119e4 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 er journal name
119e5 2a 2f 0a 20 20 66 6f 72 28 75 3d 30 3b 20 75 3c */. for(u=0; u<
119e6 6c 65 6e 3b 20 75 2b 2b 29 7b 0a 20 20 20 20 63 len; u++){. c
119e7 6b 73 75 6d 20 2d 3d 20 7a 4d 61 73 74 65 72 5b ksum -= zMaster[
119e8 75 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 63 6b u];. }. if( ck
119e9 73 75 6d 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 sum ){. /* If
119ea 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 64 6f the checksum do
119eb 65 73 6e 27 74 20 61 64 64 20 75 70 2c 20 74 68 esn't add up, th
119ec 65 6e 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f en one or more o
119ed 66 20 74 68 65 20 64 69 73 6b 20 73 65 63 74 6f f the disk secto
119ee 72 73 0a 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 rs. ** contai
119ef 6e 69 6e 67 20 74 68 65 20 6d 61 73 74 65 72 20 ning the master
119f0 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 journal filename
119f1 20 69 73 20 63 6f 72 72 75 70 74 65 64 2e 20 54 is corrupted. T
119f2 68 69 73 20 6d 65 61 6e 73 0a 20 20 20 20 2a 2a his means. **
119f3 20 64 65 66 69 6e 69 74 65 6c 79 20 72 6f 6c 6c definitely roll
119f4 20 62 61 63 6b 2c 20 73 6f 20 6a 75 73 74 20 72 back, so just r
119f5 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 eturn SQLITE_OK
119f6 61 6e 64 20 72 65 70 6f 72 74 20 61 20 28 6e 75 and report a (nu
119f7 6c 29 0a 20 20 20 20 2a 2a 20 6d 61 73 74 65 72 l). ** master
119f8 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d -journal filenam
119f9 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6c 65 e.. */. le
119fa 6e 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 7a 4d 61 n = 0;. }. zMa
119fb 73 74 65 72 5b 6c 65 6e 5d 20 3d 20 27 5c 30 27 ster[len] = '\0'
119fc 3b 0a 20 20 20 0a 20 20 72 65 74 75 72 6e 20 53 ;. . return S
119fd 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
119fe 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6f 66 ** Return the of
119ff 66 73 65 74 20 6f 66 20 74 68 65 20 73 65 63 74 fset of the sect
11a00 6f 72 20 62 6f 75 6e 64 61 72 79 20 61 74 20 6f or boundary at o
11a01 72 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 0a 2a r immediately .*
11a02 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 * following the
11a03 76 61 6c 75 65 20 69 6e 20 70 50 61 67 65 72 2d value in pPager-
11a04 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 61 73 73 >journalOff, ass
11a05 75 6d 69 6e 67 20 61 20 73 65 63 74 6f 72 20 0a uming a sector .
11a06 2a 2a 20 73 69 7a 65 20 6f 66 20 70 50 61 67 65 ** size of pPage
11a07 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 62 79 r->sectorSize by
11a08 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 69 2e 65 20 66 tes..**.** i.e f
11a09 6f 72 20 61 20 73 65 63 74 6f 72 20 73 69 7a 65 or a sector size
11a0a 20 6f 66 20 35 31 32 3a 0a 2a 2a 0a 2a 2a 20 20 of 512:.**.**
11a0b 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 Pager.journalOf
11a0c 66 20 20 20 20 20 20 20 20 20 20 52 65 74 75 72 f Retur
11a0d 6e 20 76 61 6c 75 65 0a 2a 2a 20 20 20 2d 2d 2d n value.** ---
11a0e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
11a0f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
11a10 2d 2d 2d 2d 0a 2a 2a 20 20 20 30 20 20 20 20 20 ----.** 0
11a11 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11a12 20 20 20 20 30 0a 2a 2a 20 20 20 35 31 32 20 20 0.** 512
11a13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11a14 20 20 20 20 20 35 31 32 0a 2a 2a 20 20 20 31 30 512.** 10
11a15 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0
11a16 20 20 20 20 20 20 20 20 35 31 32 0a 2a 2a 20 20 512.**
11a17 20 32 30 30 30 20 20 20 20 20 20 20 20 20 20 20 2000
11a18 20 20 20 20 20 20 20 20 20 20 20 32 30 34 38 0a 2048.
11a19 2a 2a 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 36 ** .*/.static i6
11a1a 34 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73 4 journalHdrOffs
11a1b 65 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 et(Pager *pPager
11a1c 29 7b 0a 20 20 69 36 34 20 6f 66 66 73 65 74 20 ){. i64 offset
11a1d 3d 20 30 3b 0a 20 20 69 36 34 20 63 20 3d 20 70 = 0;. i64 c = p
11a1e 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
11a1f 66 3b 0a 20 20 69 66 28 20 63 20 29 7b 0a 20 20 f;. if( c ){.
11a20 20 20 6f 66 66 73 65 74 20 3d 20 28 28 63 2d 31 offset = ((c-1
11a21 29 2f 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a )/JOURNAL_HDR_SZ
11a22 28 70 50 61 67 65 72 29 20 2b 20 31 29 20 2a 20 (pPager) + 1) *
11a23 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 JOURNAL_HDR_SZ(p
11a24 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 61 73 Pager);. }. as
11a25 73 65 72 74 28 20 6f 66 66 73 65 74 25 4a 4f 55 sert( offset%JOU
11a26 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 RNAL_HDR_SZ(pPag
11a27 65 72 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 er)==0 );. asse
11a28 72 74 28 20 6f 66 66 73 65 74 3e 3d 63 20 29 3b rt( offset>=c );
11a29 0a 20 20 61 73 73 65 72 74 28 20 28 6f 66 66 73 . assert( (offs
11a2a 65 74 2d 63 29 3c 4a 4f 55 52 4e 41 4c 5f 48 44 et-c)<JOURNAL_HD
11a2b 52 5f 53 5a 28 70 50 61 67 65 72 29 20 29 3b 0a R_SZ(pPager) );.
11a2c 20 20 72 65 74 75 72 6e 20 6f 66 66 73 65 74 3b return offset;
11a2d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f .}../*.** The jo
11a2e 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 urnal file must
11a2f 62 65 20 6f 70 65 6e 20 77 68 65 6e 20 74 68 69 be open when thi
11a30 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
11a31 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 lled..**.** This
11a32 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e function is a n
11a33 6f 2d 6f 70 20 69 66 20 74 68 65 20 6a 6f 75 72 o-op if the jour
11a34 6e 61 6c 20 66 69 6c 65 20 68 61 73 20 6e 6f 74 nal file has not
11a35 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f been written to
11a36 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20 63 .** within the c
11a37 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 urrent transacti
11a38 6f 6e 20 28 69 2e 65 2e 20 69 66 20 50 61 67 65 on (i.e. if Page
11a39 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 29 r.journalOff==0)
11a3a 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 64 6f 54 72 75 ..**.** If doTru
11a3b 6e 63 61 74 65 20 69 73 20 6e 6f 6e 2d 7a 65 72 ncate is non-zer
11a3c 6f 20 6f 72 20 74 68 65 20 50 61 67 65 72 2e 6a o or the Pager.j
11a3d 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 ournalSizeLimit
11a3e 76 61 72 69 61 62 6c 65 20 69 73 0a 2a 2a 20 73 variable is.** s
11a3f 65 74 20 74 6f 20 30 2c 20 74 68 65 6e 20 74 72 et to 0, then tr
11a40 75 6e 63 61 74 65 20 74 68 65 20 6a 6f 75 72 6e uncate the journ
11a41 61 6c 20 66 69 6c 65 20 74 6f 20 7a 65 72 6f 20 al file to zero
11a42 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 20 4f bytes in size. O
11a43 74 68 65 72 77 69 73 65 2c 0a 2a 2a 20 7a 65 72 therwise,.** zer
11a44 6f 20 74 68 65 20 32 38 2d 62 79 74 65 20 68 65 o the 28-byte he
11a45 61 64 65 72 20 61 74 20 74 68 65 20 73 74 61 72 ader at the star
11a46 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c t of the journal
11a47 20 66 69 6c 65 2e 20 49 6e 20 65 69 74 68 65 72 file. In either
11a48 20 63 61 73 65 2c 20 0a 2a 2a 20 69 66 20 74 68 case, .** if th
11a49 65 20 70 61 67 65 72 20 69 73 20 6e 6f 74 20 69 e pager is not i
11a4a 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 n no-sync mode,
11a4b 73 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c sync the journal
11a4c 20 66 69 6c 65 20 69 6d 6d 65 64 69 61 74 65 6c file immediatel
11a4d 79 20 0a 2a 2a 20 61 66 74 65 72 20 77 72 69 74 y .** after writ
11a4e 69 6e 67 20 6f 72 20 74 72 75 6e 63 61 74 69 6e ing or truncatin
11a4f 67 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 g it..**.** If P
11a50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 53 69 7a 65 ager.journalSize
11a51 4c 69 6d 69 74 20 69 73 20 73 65 74 20 74 6f 20 Limit is set to
11a52 61 20 70 6f 73 69 74 69 76 65 2c 20 6e 6f 6e 2d a positive, non-
11a53 7a 65 72 6f 20 76 61 6c 75 65 2c 20 61 6e 64 0a zero value, and.
11a54 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 ** following the
11a55 20 74 72 75 6e 63 61 74 69 6f 6e 20 6f 72 20 7a truncation or z
11a56 65 72 6f 69 6e 67 20 64 65 73 63 72 69 62 65 64 eroing described
11a57 20 61 62 6f 76 65 20 74 68 65 20 73 69 7a 65 20 above the size
11a58 6f 66 20 74 68 65 20 0a 2a 2a 20 6a 6f 75 72 6e of the .** journ
11a59 61 6c 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 al file in bytes
11a5a 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 is larger than
11a5b 74 68 69 73 20 76 61 6c 75 65 2c 20 74 68 65 6e this value, then
11a5c 20 74 72 75 6e 63 61 74 65 20 74 68 65 0a 2a 2a truncate the.**
11a5d 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f journal file to
11a5e 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 53 69 Pager.journalSi
11a5f 7a 65 4c 69 6d 69 74 20 62 79 74 65 73 2e 20 54 zeLimit bytes. T
11a60 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
11a61 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 6e 65 65 64 does.** not need
11a62 20 74 6f 20 62 65 20 73 79 6e 63 65 64 20 66 6f to be synced fo
11a63 6c 6c 6f 77 69 6e 67 20 74 68 69 73 20 6f 70 65 llowing this ope
11a64 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 ration..**.** If
11a65 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 an IO error occ
11a66 75 72 73 2c 20 61 62 61 6e 64 6f 6e 20 70 72 6f urs, abandon pro
11a67 63 65 73 73 69 6e 67 20 61 6e 64 20 72 65 74 75 cessing and retu
11a68 72 6e 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20 rn the IO error
11a69 63 6f 64 65 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 code..** Otherwi
11a6a 73 65 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 se, return SQLIT
11a6b 45 5f 4f 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 E_OK..*/.static
11a6c 69 6e 74 20 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48 int zeroJournalH
11a6d 64 72 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 dr(Pager *pPager
11a6e 2c 20 69 6e 74 20 64 6f 54 72 75 6e 63 61 74 65 , int doTruncate
11a6f 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
11a70 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 LITE_OK;
11a71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11a72 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e /* Return
11a73 20 63 6f 64 65 20 2a 2f 0a 20 20 61 73 73 65 72 code */. asser
11a74 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 t( isOpen(pPager
11a75 2d 3e 6a 66 64 29 20 29 3b 0a 20 20 69 66 28 20 ->jfd) );. if(
11a76 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
11a77 66 66 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 ff ){. const
11a78 69 36 34 20 69 4c 69 6d 69 74 20 3d 20 70 50 61 i64 iLimit = pPa
11a79 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65 ger->journalSize
11a7a 4c 69 6d 69 74 3b 20 20 20 20 2f 2a 20 4c 6f 63 Limit; /* Loc
11a7b 61 6c 20 63 61 63 68 65 20 6f 66 20 6a 73 6c 20 al cache of jsl
11a7c 2a 2f 0a 0a 20 20 20 20 49 4f 54 52 41 43 45 28 */.. IOTRACE(
11a7d 28 22 4a 5a 45 52 4f 48 44 52 20 25 70 5c 6e 22 ("JZEROHDR %p\n"
11a7e 2c 20 70 50 61 67 65 72 29 29 0a 20 20 20 20 69 , pPager)). i
11a7f 66 28 20 64 6f 54 72 75 6e 63 61 74 65 20 7c 7c f( doTruncate ||
11a80 20 69 4c 69 6d 69 74 3d 3d 30 20 29 7b 0a 20 20 iLimit==0 ){.
11a81 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
11a82 4f 73 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 OsTruncate(pPage
11a83 72 2d 3e 6a 66 64 2c 20 30 29 3b 0a 20 20 20 20 r->jfd, 0);.
11a84 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 74 61 }else{. sta
11a85 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a tic const char z
11a86 65 72 6f 48 64 72 5b 32 38 5d 20 3d 20 7b 30 7d eroHdr[28] = {0}
11a87 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c ;. rc = sql
11a88 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 ite3OsWrite(pPag
11a89 65 72 2d 3e 6a 66 64 2c 20 7a 65 72 6f 48 64 72 er->jfd, zeroHdr
11a8a 2c 20 73 69 7a 65 6f 66 28 7a 65 72 6f 48 64 72 , sizeof(zeroHdr
11a8b 29 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 ), 0);. }.
11a8c 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
11a8d 4f 4b 20 26 26 20 21 70 50 61 67 65 72 2d 3e 6e OK && !pPager->n
11a8e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 72 oSync ){. r
11a8f 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e c = sqlite3OsSyn
11a90 63 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 53 c(pPager->jfd, S
11a91 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f QLITE_SYNC_DATAO
11a92 4e 4c 59 7c 70 50 61 67 65 72 2d 3e 73 79 6e 63 NLY|pPager->sync
11a93 5f 66 6c 61 67 73 29 3b 0a 20 20 20 20 7d 0a 0a _flags);. }..
11a94 20 20 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 /* At this p
11a95 6f 69 6e 74 20 74 68 65 20 74 72 61 6e 73 61 63 oint the transac
11a96 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 tion is committe
11a97 64 20 62 75 74 20 74 68 65 20 77 72 69 74 65 20 d but the write
11a98 6c 6f 63 6b 20 0a 20 20 20 20 2a 2a 20 69 73 20 lock . ** is
11a99 73 74 69 6c 6c 20 68 65 6c 64 20 6f 6e 20 74 68 still held on th
11a9a 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65 72 65 e file. If there
11a9b 20 69 73 20 61 20 73 69 7a 65 20 6c 69 6d 69 74 is a size limit
11a9c 20 63 6f 6e 66 69 67 75 72 65 64 20 66 6f 72 20 configured for
11a9d 0a 20 20 20 20 2a 2a 20 74 68 65 20 70 65 72 73 . ** the pers
11a9e 69 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 61 istent journal a
11a9f 6e 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 nd the journal f
11aa0 69 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 63 6f ile currently co
11aa1 6e 73 75 6d 65 73 20 6d 6f 72 65 0a 20 20 20 20 nsumes more.
11aa2 2a 2a 20 73 70 61 63 65 20 74 68 61 6e 20 74 68 ** space than th
11aa3 61 74 20 6c 69 6d 69 74 20 61 6c 6c 6f 77 73 20 at limit allows
11aa4 66 6f 72 2c 20 74 72 75 6e 63 61 74 65 20 69 74 for, truncate it
11aa5 20 6e 6f 77 2e 20 54 68 65 72 65 20 69 73 20 6e now. There is n
11aa6 6f 20 6e 65 65 64 0a 20 20 20 20 2a 2a 20 74 6f o need. ** to
11aa7 20 73 79 6e 63 20 74 68 65 20 66 69 6c 65 20 66 sync the file f
11aa8 6f 6c 6c 6f 77 69 6e 67 20 74 68 69 73 20 6f 70 ollowing this op
11aa9 65 72 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a eration.. */.
11aaa 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
11aab 54 45 5f 4f 4b 20 26 26 20 69 4c 69 6d 69 74 3e TE_OK && iLimit>
11aac 30 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 73 0 ){. i64 s
11aad 7a 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 z;. rc = sq
11aae 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 lite3OsFileSize(
11aaf 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 73 7a pPager->jfd, &sz
11ab0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d );. if( rc=
11ab1 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 73 7a =SQLITE_OK && sz
11ab2 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 20 >iLimit ){.
11ab3 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
11ab4 73 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 sTruncate(pPager
11ab5 2d 3e 6a 66 64 2c 20 69 4c 69 6d 69 74 29 3b 0a ->jfd, iLimit);.
11ab6 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
11ab7 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
11ab8 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 ../*.** The jour
11ab9 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65 nal file must be
11aba 20 6f 70 65 6e 20 77 68 65 6e 20 74 68 69 73 20 open when this
11abb 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
11abc 64 2e 20 41 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 d. A journal.**
11abd 68 65 61 64 65 72 20 28 4a 4f 55 52 4e 41 4c 5f header (JOURNAL_
11abe 48 44 52 5f 53 5a 20 62 79 74 65 73 29 20 69 73 HDR_SZ bytes) is
11abf 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 written into th
11ac0 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 e journal file a
11ac1 74 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 t the.** current
11ac2 20 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a location..**.**
11ac3 20 54 68 65 20 66 6f 72 6d 61 74 20 66 6f 72 20 The format for
11ac4 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 the journal head
11ac5 65 72 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 er is as follows
11ac6 3a 0a 2a 2a 20 2d 20 38 20 62 79 74 65 73 3a 20 :.** - 8 bytes:
11ac7 4d 61 67 69 63 20 69 64 65 6e 74 69 66 79 69 6e Magic identifyin
11ac8 67 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 g journal format
11ac9 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 ..** - 4 bytes:
11aca 4e 75 6d 62 65 72 20 6f 66 20 72 65 63 6f 72 64 Number of record
11acb 73 20 69 6e 20 6a 6f 75 72 6e 61 6c 2c 20 6f 72 s in journal, or
11acc 20 2d 31 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 -1 no-sync mode
11acd 20 69 73 20 6f 6e 2e 0a 2a 2a 20 2d 20 34 20 62 is on..** - 4 b
11ace 79 74 65 73 3a 20 52 61 6e 64 6f 6d 20 6e 75 6d ytes: Random num
11acf 62 65 72 20 75 73 65 64 20 66 6f 72 20 70 61 67 ber used for pag
11ad0 65 20 68 61 73 68 2e 0a 2a 2a 20 2d 20 34 20 62 e hash..** - 4 b
11ad1 79 74 65 73 3a 20 49 6e 69 74 69 61 6c 20 64 61 ytes: Initial da
11ad2 74 61 62 61 73 65 20 70 61 67 65 20 63 6f 75 6e tabase page coun
11ad3 74 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a t..** - 4 bytes:
11ad4 20 53 65 63 74 6f 72 20 73 69 7a 65 20 75 73 65 Sector size use
11ad5 64 20 62 79 20 74 68 65 20 70 72 6f 63 65 73 73 d by the process
11ad6 20 74 68 61 74 20 77 72 6f 74 65 20 74 68 69 73 that wrote this
11ad7 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 2d 20 34 journal..** - 4
11ad8 20 62 79 74 65 73 3a 20 44 61 74 61 62 61 73 65 bytes: Database
11ad9 20 70 61 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 0a page size..** .
11ada 2a 2a 20 46 6f 6c 6c 6f 77 65 64 20 62 79 20 28 ** Followed by (
11adb 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 2d JOURNAL_HDR_SZ -
11adc 20 32 38 29 20 62 79 74 65 73 20 6f 66 20 75 6e 28) bytes of un
11add 75 73 65 64 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 used space..*/.s
11ade 74 61 74 69 63 20 69 6e 74 20 77 72 69 74 65 4a tatic int writeJ
11adf 6f 75 72 6e 61 6c 48 64 72 28 50 61 67 65 72 20 ournalHdr(Pager
11ae0 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 *pPager){. int
11ae1 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 rc = SQLITE_OK;
11ae2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11ae3 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a /* Return code *
11ae4 2f 0a 20 20 63 68 61 72 20 2a 7a 48 65 61 64 65 /. char *zHeade
11ae5 72 20 3d 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 r = pPager->pTmp
11ae6 53 70 61 63 65 3b 20 20 2f 2a 20 54 65 6d 70 6f Space; /* Tempo
11ae7 72 61 72 79 20 73 70 61 63 65 20 75 73 65 64 20 rary space used
11ae8 74 6f 20 62 75 69 6c 64 20 68 65 61 64 65 72 20 to build header
11ae9 2a 2f 0a 20 20 75 33 32 20 6e 48 65 61 64 65 72 */. u32 nHeader
11aea 20 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 = pPager->pageS
11aeb 69 7a 65 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 ize; /* Size
11aec 20 6f 66 20 62 75 66 66 65 72 20 70 6f 69 6e 74 of buffer point
11aed 65 64 20 74 6f 20 62 79 20 7a 48 65 61 64 65 72 ed to by zHeader
11aee 20 2a 2f 0a 20 20 75 33 32 20 6e 57 72 69 74 65 */. u32 nWrite
11aef 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
11af0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 /* Byt
11af1 65 73 20 6f 66 20 68 65 61 64 65 72 20 73 65 63 es of header sec
11af2 74 6f 72 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 tor written */.
11af3 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 int ii;
11af4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11af5 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 /* Loop cou
11af6 6e 74 65 72 20 2a 2f 0a 0a 20 20 61 73 73 65 72 nter */.. asser
11af7 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 t( isOpen(pPager
11af8 2d 3e 6a 66 64 29 20 29 3b 20 20 20 20 20 20 2f ->jfd) ); /
11af9 2a 20 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d * Journal file m
11afa 75 73 74 20 62 65 20 6f 70 65 6e 2e 20 2a 2f 0a ust be open. */.
11afb 0a 20 20 69 66 28 20 6e 48 65 61 64 65 72 3e 4a . if( nHeader>J
11afc 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 OURNAL_HDR_SZ(pP
11afd 61 67 65 72 29 20 29 7b 0a 20 20 20 20 6e 48 65 ager) ){. nHe
11afe 61 64 65 72 20 3d 20 4a 4f 55 52 4e 41 4c 5f 48 ader = JOURNAL_H
11aff 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 DR_SZ(pPager);.
11b00 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 }.. /* If ther
11b01 65 20 61 72 65 20 61 63 74 69 76 65 20 73 61 76 e are active sav
11b02 65 70 6f 69 6e 74 73 20 61 6e 64 20 61 6e 79 20 epoints and any
11b03 6f 66 20 74 68 65 6d 20 77 65 72 65 20 63 72 65 of them were cre
11b04 61 74 65 64 20 0a 20 20 2a 2a 20 73 69 6e 63 65 ated . ** since
11b05 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 the most recent
11b06 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 journal header
11b07 77 61 73 20 77 72 69 74 74 65 6e 2c 20 75 70 64 was written, upd
11b08 61 74 65 20 74 68 65 20 0a 20 20 2a 2a 20 50 61 ate the . ** Pa
11b09 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 gerSavepoint.iHd
11b0a 72 4f 66 66 73 65 74 20 66 69 65 6c 64 73 20 6e rOffset fields n
11b0b 6f 77 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 ow.. */. for(i
11b0c 69 3d 30 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e i=0; ii<pPager->
11b0d 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b nSavepoint; ii++
11b0e 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 ){. if( pPage
11b0f 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 r->aSavepoint[ii
11b10 5d 2e 69 48 64 72 4f 66 66 73 65 74 3d 3d 30 20 ].iHdrOffset==0
11b11 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d ){. pPager-
11b12 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e >aSavepoint[ii].
11b13 69 48 64 72 4f 66 66 73 65 74 20 3d 20 70 50 61 iHdrOffset = pPa
11b14 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b ger->journalOff;
11b15 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 50 . }. }.. pP
11b16 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 ager->journalHdr
11b17 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e = pPager->journ
11b18 61 6c 4f 66 66 20 3d 20 6a 6f 75 72 6e 61 6c 48 alOff = journalH
11b19 64 72 4f 66 66 73 65 74 28 70 50 61 67 65 72 29 drOffset(pPager)
11b1a 3b 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 57 72 ;.. /* . ** Wr
11b1b 69 74 65 20 74 68 65 20 6e 52 65 63 20 46 69 65 ite the nRec Fie
11b1c 6c 64 20 2d 20 74 68 65 20 6e 75 6d 62 65 72 20 ld - the number
11b1d 6f 66 20 70 61 67 65 20 72 65 63 6f 72 64 73 20 of page records
11b1e 74 68 61 74 20 66 6f 6c 6c 6f 77 20 74 68 69 73 that follow this
11b1f 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 68 65 . ** journal he
11b20 61 64 65 72 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20 ader. Normally,
11b21 7a 65 72 6f 20 69 73 20 77 72 69 74 74 65 6e 20 zero is written
11b22 74 6f 20 74 68 69 73 20 76 61 6c 75 65 20 61 74 to this value at
11b23 20 74 68 69 73 20 74 69 6d 65 2e 0a 20 20 2a 2a this time.. **
11b24 20 41 66 74 65 72 20 74 68 65 20 72 65 63 6f 72 After the recor
11b25 64 73 20 61 72 65 20 61 64 64 65 64 20 74 6f 20 ds are added to
11b26 74 68 65 20 6a 6f 75 72 6e 61 6c 20 28 61 6e 64 the journal (and
11b27 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 73 79 6e the journal syn
11b28 63 65 64 2c 20 0a 20 20 2a 2a 20 69 66 20 69 6e ced, . ** if in
11b29 20 66 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 29 full-sync mode)
11b2a 2c 20 74 68 65 20 7a 65 72 6f 20 69 73 20 6f 76 , the zero is ov
11b2b 65 72 77 72 69 74 74 65 6e 20 77 69 74 68 20 74 erwritten with t
11b2c 68 65 20 74 72 75 65 20 6e 75 6d 62 65 72 0a 20 he true number.
11b2d 20 2a 2a 20 6f 66 20 72 65 63 6f 72 64 73 20 28 ** of records (
11b2e 73 65 65 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 see syncJournal(
11b2f 29 29 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 )).. **. ** A
11b30 66 61 73 74 65 72 20 61 6c 74 65 72 6e 61 74 69 faster alternati
11b31 76 65 20 69 73 20 74 6f 20 77 72 69 74 65 20 30 ve is to write 0
11b32 78 46 46 46 46 46 46 46 46 20 74 6f 20 74 68 65 xFFFFFFFF to the
11b33 20 6e 52 65 63 20 66 69 65 6c 64 2e 20 57 68 65 nRec field. Whe
11b34 6e 0a 20 20 2a 2a 20 72 65 61 64 69 6e 67 20 74 n. ** reading t
11b35 68 65 20 6a 6f 75 72 6e 61 6c 20 74 68 69 73 20 he journal this
11b36 76 61 6c 75 65 20 74 65 6c 6c 73 20 53 51 4c 69 value tells SQLi
11b37 74 65 20 74 6f 20 61 73 73 75 6d 65 20 74 68 61 te to assume tha
11b38 74 20 74 68 65 0a 20 20 2a 2a 20 72 65 73 74 20 t the. ** rest
11b39 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 of the journal f
11b3a 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 76 61 6c ile contains val
11b3b 69 64 20 70 61 67 65 20 72 65 63 6f 72 64 73 2e id page records.
11b3c 20 54 68 69 73 20 61 73 73 75 6d 70 74 69 6f 6e This assumption
11b3d 0a 20 20 2a 2a 20 69 73 20 64 61 6e 67 65 72 6f . ** is dangero
11b3e 75 73 2c 20 61 73 20 69 66 20 61 20 66 61 69 6c us, as if a fail
11b3f 75 72 65 20 6f 63 63 75 72 72 65 64 20 77 68 69 ure occurred whi
11b40 6c 73 74 20 77 72 69 74 69 6e 67 20 74 6f 20 74 lst writing to t
11b41 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 he journal. **
11b42 66 69 6c 65 20 69 74 20 6d 61 79 20 63 6f 6e 74 file it may cont
11b43 61 69 6e 20 73 6f 6d 65 20 67 61 72 62 61 67 65 ain some garbage
11b44 20 64 61 74 61 2e 20 54 68 65 72 65 20 61 72 65 data. There are
11b45 20 74 77 6f 20 73 63 65 6e 61 72 69 6f 73 0a 20 two scenarios.
11b46 20 2a 2a 20 77 68 65 72 65 20 74 68 69 73 20 72 ** where this r
11b47 69 73 6b 20 63 61 6e 20 62 65 20 69 67 6e 6f 72 isk can be ignor
11b48 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 ed:. **. **
11b49 2a 20 57 68 65 6e 20 74 68 65 20 70 61 67 65 72 * When the pager
11b4a 20 69 73 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d is in no-sync m
11b4b 6f 64 65 2e 20 43 6f 72 72 75 70 74 69 6f 6e 20 ode. Corruption
11b4c 63 61 6e 20 66 6f 6c 6c 6f 77 20 61 0a 20 20 2a can follow a. *
11b4d 2a 20 20 20 20 20 70 6f 77 65 72 20 66 61 69 6c * power fail
11b4e 75 72 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 ure in this case
11b4f 20 61 6e 79 77 61 79 2e 0a 20 20 2a 2a 0a 20 20 anyway.. **.
11b50 2a 2a 20 20 20 2a 20 57 68 65 6e 20 74 68 65 20 ** * When the
11b51 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 SQLITE_IOCAP_SAF
11b52 45 5f 41 50 50 45 4e 44 20 66 6c 61 67 20 69 73 E_APPEND flag is
11b53 20 73 65 74 2e 20 54 68 69 73 20 67 75 61 72 61 set. This guara
11b54 6e 74 65 65 73 0a 20 20 2a 2a 20 20 20 20 20 74 ntees. ** t
11b55 68 61 74 20 67 61 72 62 61 67 65 20 64 61 74 61 hat garbage data
11b56 20 69 73 20 6e 65 76 65 72 20 61 70 70 65 6e 64 is never append
11b57 65 64 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 ed to the journa
11b58 6c 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 61 l file.. */. a
11b59 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 ssert( isOpen(pP
11b5a 61 67 65 72 2d 3e 66 64 29 20 7c 7c 20 70 50 61 ager->fd) || pPa
11b5b 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 3b 0a 20 ger->noSync );.
11b5c 20 69 66 28 20 28 70 50 61 67 65 72 2d 3e 6e 6f if( (pPager->no
11b5d 53 79 6e 63 29 20 7c 7c 20 28 70 50 61 67 65 72 Sync) || (pPager
11b5e 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 ->journalMode==P
11b5f 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
11b60 5f 4d 45 4d 4f 52 59 29 0a 20 20 20 7c 7c 20 28 _MEMORY). || (
11b61 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 sqlite3OsDeviceC
11b62 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 70 haracteristics(p
11b63 50 61 67 65 72 2d 3e 66 64 29 26 53 51 4c 49 54 Pager->fd)&SQLIT
11b64 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 E_IOCAP_SAFE_APP
11b65 45 4e 44 29 20 0a 20 20 29 7b 0a 20 20 20 20 6d END) . ){. m
11b66 65 6d 63 70 79 28 7a 48 65 61 64 65 72 2c 20 61 emcpy(zHeader, a
11b67 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69 JournalMagic, si
11b68 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 zeof(aJournalMag
11b69 69 63 29 29 3b 0a 20 20 20 20 70 75 74 33 32 62 ic));. put32b
11b6a 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a its(&zHeader[siz
11b6b 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 eof(aJournalMagi
11b6c 63 29 5d 2c 20 30 78 66 66 66 66 66 66 66 66 29 c)], 0xffffffff)
11b6d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d ;. }else{. m
11b6e 65 6d 73 65 74 28 7a 48 65 61 64 65 72 2c 20 30 emset(zHeader, 0
11b6f 2c 20 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 , sizeof(aJourna
11b70 6c 4d 61 67 69 63 29 2b 34 29 3b 0a 20 20 7d 0a lMagic)+4);. }.
11b71 0a 20 20 2f 2a 20 54 68 65 20 72 61 6e 64 6f 6d . /* The random
11b72 20 63 68 65 63 6b 2d 68 61 73 68 20 69 6e 69 74 check-hash init
11b73 69 61 6c 69 73 65 72 20 2a 2f 20 0a 20 20 73 71 ialiser */ . sq
11b74 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 lite3_randomness
11b75 28 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e (sizeof(pPager->
11b76 63 6b 73 75 6d 49 6e 69 74 29 2c 20 26 70 50 61 cksumInit), &pPa
11b77 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 3b ger->cksumInit);
11b78 0a 20 20 70 75 74 33 32 62 69 74 73 28 26 7a 48 . put32bits(&zH
11b79 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f eader[sizeof(aJo
11b7a 75 72 6e 61 6c 4d 61 67 69 63 29 2b 34 5d 2c 20 urnalMagic)+4],
11b7b 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 pPager->cksumIni
11b7c 74 29 3b 0a 20 20 2f 2a 20 54 68 65 20 69 6e 69 t);. /* The ini
11b7d 74 69 61 6c 20 64 61 74 61 62 61 73 65 20 73 69 tial database si
11b7e 7a 65 20 2a 2f 0a 20 20 70 75 74 33 32 62 69 74 ze */. put32bit
11b7f 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f s(&zHeader[sizeo
11b80 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 f(aJournalMagic)
11b81 2b 38 5d 2c 20 70 50 61 67 65 72 2d 3e 64 62 4f +8], pPager->dbO
11b82 72 69 67 53 69 7a 65 29 3b 0a 20 20 2f 2a 20 54 rigSize);. /* T
11b83 68 65 20 61 73 73 75 6d 65 64 20 73 65 63 74 6f he assumed secto
11b84 72 20 73 69 7a 65 20 66 6f 72 20 74 68 69 73 20 r size for this
11b85 70 72 6f 63 65 73 73 20 2a 2f 0a 20 20 70 75 74 process */. put
11b86 33 32 62 69 74 73 28 26 7a 48 65 61 64 65 72 5b 32bits(&zHeader[
11b87 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d sizeof(aJournalM
11b88 61 67 69 63 29 2b 31 32 5d 2c 20 70 50 61 67 65 agic)+12], pPage
11b89 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 29 3b 0a r->sectorSize);.
11b8a 0a 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 73 . /* The page s
11b8b 69 7a 65 20 2a 2f 0a 20 20 70 75 74 33 32 62 69 ize */. put32bi
11b8c 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 ts(&zHeader[size
11b8d 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 of(aJournalMagic
11b8e 29 2b 31 36 5d 2c 20 70 50 61 67 65 72 2d 3e 70 )+16], pPager->p
11b8f 61 67 65 53 69 7a 65 29 3b 0a 0a 20 20 2f 2a 20 ageSize);.. /*
11b90 49 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 Initializing the
11b91 20 74 61 69 6c 20 6f 66 20 74 68 65 20 62 75 66 tail of the buf
11b92 66 65 72 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 fer is not neces
11b93 73 61 72 79 2e 20 20 45 76 65 72 79 74 68 69 6e sary. Everythin
11b94 67 0a 20 20 2a 2a 20 77 6f 72 6b 73 20 66 69 6e g. ** works fin
11b95 64 20 69 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 d if the followi
11b96 6e 67 20 6d 65 6d 73 65 74 28 29 20 69 73 20 6f ng memset() is o
11b97 6d 69 74 74 65 64 2e 20 20 42 75 74 20 69 6e 69 mitted. But ini
11b98 74 69 61 6c 69 7a 69 6e 67 0a 20 20 2a 2a 20 74 tializing. ** t
11b99 68 65 20 6d 65 6d 6f 72 79 20 70 72 65 76 65 6e he memory preven
11b9a 74 73 20 76 61 6c 67 72 69 6e 64 20 66 72 6f 6d ts valgrind from
11b9b 20 63 6f 6d 70 6c 61 69 6e 69 6e 67 2c 20 73 6f complaining, so
11b9c 20 77 65 20 61 72 65 20 77 69 6c 6c 69 6e 67 20 we are willing
11b9d 74 6f 0a 20 20 2a 2a 20 74 61 6b 65 20 74 68 65 to. ** take the
11b9e 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 68 69 74 performance hit
11b9f 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 .. */. memset(
11ba0 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 &zHeader[sizeof(
11ba1 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 32 aJournalMagic)+2
11ba2 30 5d 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 0], 0,.
11ba3 6e 48 65 61 64 65 72 2d 28 73 69 7a 65 6f 66 28 nHeader-(sizeof(
11ba4 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 32 aJournalMagic)+2
11ba5 30 29 29 3b 0a 0a 20 20 2f 2a 20 49 6e 20 74 68 0));.. /* In th
11ba6 65 6f 72 79 2c 20 69 74 20 69 73 20 6f 6e 6c 79 eory, it is only
11ba7 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 77 72 necessary to wr
11ba8 69 74 65 20 74 68 65 20 32 38 20 62 79 74 65 73 ite the 28 bytes
11ba9 20 74 68 61 74 20 74 68 65 20 0a 20 20 2a 2a 20 that the . **
11baa 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 63 journal header c
11bab 6f 6e 73 75 6d 65 73 20 74 6f 20 74 68 65 20 6a onsumes to the j
11bac 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 65 72 65 ournal file here
11bad 2e 20 54 68 65 6e 20 69 6e 63 72 65 6d 65 6e 74 . Then increment
11bae 20 74 68 65 20 0a 20 20 2a 2a 20 50 61 67 65 72 the . ** Pager
11baf 2e 6a 6f 75 72 6e 61 6c 4f 66 66 20 76 61 72 69 .journalOff vari
11bb0 61 62 6c 65 20 62 79 20 4a 4f 55 52 4e 41 4c 5f able by JOURNAL_
11bb1 48 44 52 5f 53 5a 20 73 6f 20 74 68 61 74 20 74 HDR_SZ so that t
11bb2 68 65 20 6e 65 78 74 20 0a 20 20 2a 2a 20 72 65 he next . ** re
11bb3 63 6f 72 64 20 69 73 20 77 72 69 74 74 65 6e 20 cord is written
11bb4 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 to the following
11bb5 20 73 65 63 74 6f 72 20 28 6c 65 61 76 69 6e 67 sector (leaving
11bb6 20 61 20 67 61 70 20 69 6e 20 74 68 65 20 66 69 a gap in the fi
11bb7 6c 65 0a 20 20 2a 2a 20 74 68 61 74 20 77 69 6c le. ** that wil
11bb8 6c 20 62 65 20 69 6d 70 6c 69 63 69 74 6c 79 20 l be implicitly
11bb9 66 69 6c 6c 65 64 20 69 6e 20 62 79 20 74 68 65 filled in by the
11bba 20 4f 53 29 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 OS).. **. **
11bbb 48 6f 77 65 76 65 72 20 69 74 20 68 61 73 20 62 However it has b
11bbc 65 65 6e 20 64 69 73 63 6f 76 65 72 65 64 20 74 een discovered t
11bbd 68 61 74 20 6f 6e 20 73 6f 6d 65 20 73 79 73 74 hat on some syst
11bbe 65 6d 73 20 74 68 69 73 20 70 61 74 74 65 72 6e ems this pattern
11bbf 20 63 61 6e 20 0a 20 20 2a 2a 20 62 65 20 73 69 can . ** be si
11bc0 67 6e 69 66 69 63 61 6e 74 6c 79 20 73 6c 6f 77 gnificantly slow
11bc1 65 72 20 74 68 61 6e 20 63 6f 6e 74 69 67 75 6f er than contiguo
11bc2 75 73 6c 79 20 77 72 69 74 69 6e 67 20 64 61 74 usly writing dat
11bc3 61 20 74 6f 20 74 68 65 20 66 69 6c 65 2c 0a 20 a to the file,.
11bc4 20 2a 2a 20 65 76 65 6e 20 69 66 20 74 68 61 74 ** even if that
11bc5 20 6d 65 61 6e 73 20 65 78 70 6c 69 63 69 74 6c means explicitl
11bc6 79 20 77 72 69 74 69 6e 67 20 64 61 74 61 20 74 y writing data t
11bc7 6f 20 74 68 65 20 62 6c 6f 63 6b 20 6f 66 20 0a o the block of .
11bc8 20 20 2a 2a 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 ** (JOURNAL_HD
11bc9 52 5f 53 5a 20 2d 20 32 38 29 20 62 79 74 65 73 R_SZ - 28) bytes
11bca 20 74 68 61 74 20 77 69 6c 6c 20 6e 6f 74 20 62 that will not b
11bcb 65 20 75 73 65 64 2e 20 53 6f 20 74 68 61 74 20 e used. So that
11bcc 69 73 20 77 68 61 74 0a 20 20 2a 2a 20 69 73 20 is what. ** is
11bcd 64 6f 6e 65 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a done. . **. **
11bce 20 54 68 65 20 6c 6f 6f 70 20 69 73 20 72 65 71 The loop is req
11bcf 75 69 72 65 64 20 68 65 72 65 20 69 6e 20 63 61 uired here in ca
11bd0 73 65 20 74 68 65 20 73 65 63 74 6f 72 2d 73 69 se the sector-si
11bd1 7a 65 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 ze is larger tha
11bd2 6e 20 74 68 65 20 0a 20 20 2a 2a 20 64 61 74 61 n the . ** data
11bd3 62 61 73 65 20 70 61 67 65 20 73 69 7a 65 2e 20 base page size.
11bd4 53 69 6e 63 65 20 74 68 65 20 7a 48 65 61 64 65 Since the zHeade
11bd5 72 20 62 75 66 66 65 72 20 69 73 20 6f 6e 6c 79 r buffer is only
11bd6 20 50 61 67 65 72 2e 70 61 67 65 53 69 7a 65 0a Pager.pageSize.
11bd7 20 20 2a 2a 20 62 79 74 65 73 20 69 6e 20 73 69 ** bytes in si
11bd8 7a 65 2c 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e ze, more than on
11bd9 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 e call to sqlite
11bda 33 4f 73 57 72 69 74 65 28 29 20 6d 61 79 20 62 3OsWrite() may b
11bdb 65 20 72 65 71 75 69 72 65 64 0a 20 20 2a 2a 20 e required. **
11bdc 74 6f 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20 to populate the
11bdd 65 6e 74 69 72 65 20 6a 6f 75 72 6e 61 6c 20 68 entire journal h
11bde 65 61 64 65 72 20 73 65 63 74 6f 72 2e 0a 20 20 eader sector..
11bdf 2a 2f 20 0a 20 20 66 6f 72 28 6e 57 72 69 74 65 */ . for(nWrite
11be0 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f =0; rc==SQLITE_O
11be1 4b 26 26 6e 57 72 69 74 65 3c 4a 4f 55 52 4e 41 K&&nWrite<JOURNA
11be2 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 L_HDR_SZ(pPager)
11be3 3b 20 6e 57 72 69 74 65 2b 3d 6e 48 65 61 64 65 ; nWrite+=nHeade
11be4 72 29 7b 0a 20 20 20 20 49 4f 54 52 41 43 45 28 r){. IOTRACE(
11be5 28 22 4a 48 44 52 20 25 70 20 25 6c 6c 64 20 25 ("JHDR %p %lld %
11be6 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 d\n", pPager, pP
11be7 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 ager->journalHdr
11be8 2c 20 6e 48 65 61 64 65 72 29 29 0a 20 20 20 20 , nHeader)).
11be9 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 rc = sqlite3OsWr
11bea 69 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c ite(pPager->jfd,
11beb 20 7a 48 65 61 64 65 72 2c 20 6e 48 65 61 64 65 zHeader, nHeade
11bec 72 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e r, pPager->journ
11bed 61 6c 4f 66 66 29 3b 0a 20 20 20 20 70 50 61 67 alOff);. pPag
11bee 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b er->journalOff +
11bef 3d 20 6e 48 65 61 64 65 72 3b 0a 20 20 7d 0a 0a = nHeader;. }..
11bf0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
11bf1 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 /*.** The journa
11bf2 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20 6f l file must be o
11bf3 70 65 6e 20 77 68 65 6e 20 74 68 69 73 20 69 73 pen when this is
11bf4 20 63 61 6c 6c 65 64 2e 20 41 20 6a 6f 75 72 6e called. A journ
11bf5 61 6c 20 68 65 61 64 65 72 20 66 69 6c 65 0a 2a al header file.*
11bf6 2a 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 * (JOURNAL_HDR_S
11bf7 5a 20 62 79 74 65 73 29 20 69 73 20 72 65 61 64 Z bytes) is read
11bf8 20 66 72 6f 6d 20 74 68 65 20 63 75 72 72 65 6e from the curren
11bf9 74 20 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 74 68 t location in th
11bfa 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c e journal.** fil
11bfb 65 2e 20 54 68 65 20 63 75 72 72 65 6e 74 20 6c e. The current l
11bfc 6f 63 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 6a ocation in the j
11bfd 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 67 ournal file is g
11bfe 69 76 65 6e 20 62 79 0a 2a 2a 20 70 50 61 67 65 iven by.** pPage
11bff 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2e 20 53 r->journalOff. S
11c00 65 65 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 ee comments abov
11c01 65 20 66 75 6e 63 74 69 6f 6e 20 77 72 69 74 65 e function write
11c02 4a 6f 75 72 6e 61 6c 48 64 72 28 29 20 66 6f 72 JournalHdr() for
11c03 0a 2a 2a 20 61 20 64 65 73 63 72 69 70 74 69 6f .** a descriptio
11c04 6e 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c n of the journal
11c05 20 68 65 61 64 65 72 20 66 6f 72 6d 61 74 2e 0a header format..
11c06 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 68 65 61 **.** If the hea
11c07 64 65 72 20 69 73 20 72 65 61 64 20 73 75 63 63 der is read succ
11c08 65 73 73 66 75 6c 6c 79 2c 20 2a 70 4e 52 65 63 essfully, *pNRec
11c09 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e is set to the n
11c0a 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 70 61 67 65 umber of.** page
11c0b 20 72 65 63 6f 72 64 73 20 66 6f 6c 6c 6f 77 69 records followi
11c0c 6e 67 20 74 68 69 73 20 68 65 61 64 65 72 20 61 ng this header a
11c0d 6e 64 20 2a 70 44 62 53 69 7a 65 20 69 73 20 73 nd *pDbSize is s
11c0e 65 74 20 74 6f 20 74 68 65 20 73 69 7a 65 20 6f et to the size o
11c0f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 f the.** databas
11c10 65 20 62 65 66 6f 72 65 20 74 68 65 20 74 72 61 e before the tra
11c11 6e 73 61 63 74 69 6f 6e 20 62 65 67 61 6e 2c 20 nsaction began,
11c12 69 6e 20 70 61 67 65 73 2e 20 41 6c 73 6f 2c 20 in pages. Also,
11c13 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 pPager->cksumIni
11c14 74 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 74 t.** is set to t
11c15 68 65 20 76 61 6c 75 65 20 72 65 61 64 20 66 72 he value read fr
11c16 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 om the journal h
11c17 65 61 64 65 72 2e 20 53 51 4c 49 54 45 5f 4f 4b eader. SQLITE_OK
11c18 20 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 is returned.**
11c19 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a in this case..**
11c1a 0a 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e .** If the journ
11c1b 61 6c 20 68 65 61 64 65 72 20 66 69 6c 65 20 61 al header file a
11c1c 70 70 65 61 72 73 20 74 6f 20 62 65 20 63 6f 72 ppears to be cor
11c1d 72 75 70 74 65 64 2c 20 53 51 4c 49 54 45 5f 44 rupted, SQLITE_D
11c1e 4f 4e 45 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e ONE is.** return
11c1f 65 64 20 61 6e 64 20 2a 70 4e 52 65 63 20 61 6e ed and *pNRec an
11c20 64 20 2a 50 44 62 53 69 7a 65 20 61 72 65 20 75 d *PDbSize are u
11c21 6e 64 65 66 69 6e 65 64 2e 20 20 49 66 20 4a 4f ndefined. If JO
11c22 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 62 79 74 URNAL_HDR_SZ byt
11c23 65 73 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 es.** cannot be
11c24 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f read from the jo
11c25 75 72 6e 61 6c 20 66 69 6c 65 20 61 6e 20 65 72 urnal file an er
11c26 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 ror code is retu
11c27 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 rned..*/.static
11c28 69 6e 74 20 72 65 61 64 4a 6f 75 72 6e 61 6c 48 int readJournalH
11c29 64 72 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 dr(. Pager *pPa
11c2a 67 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 ger,
11c2b 20 20 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 /* Pager obje
11c2c 63 74 20 2a 2f 0a 20 20 69 6e 74 20 69 73 48 6f ct */. int isHo
11c2d 74 2c 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61 6c t,. i64 journal
11c2e 53 69 7a 65 2c 20 20 20 20 20 20 20 20 20 20 20 Size,
11c2f 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 /* Size of the
11c30 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 open journal fi
11c31 6c 65 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 le in bytes */.
11c32 20 75 33 32 20 2a 70 4e 52 65 63 2c 20 20 20 20 u32 *pNRec,
11c33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
11c34 20 4f 55 54 3a 20 56 61 6c 75 65 20 72 65 61 64 OUT: Value read
11c35 20 66 72 6f 6d 20 74 68 65 20 6e 52 65 63 20 66 from the nRec f
11c36 69 65 6c 64 20 2a 2f 0a 20 20 75 33 32 20 2a 70 ield */. u32 *p
11c37 44 62 53 69 7a 65 20 20 20 20 20 20 20 20 20 20 DbSize
11c38 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 56 /* OUT: V
11c39 61 6c 75 65 20 6f 66 20 6f 72 69 67 69 6e 61 6c alue of original
11c3a 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 66 database size f
11c3b 69 65 6c 64 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 ield */.){. int
11c3c 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 rc;
11c3d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
11c3e 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 75 6e urn code */. un
11c3f 73 69 67 6e 65 64 20 63 68 61 72 20 61 4d 61 67 signed char aMag
11c40 69 63 5b 38 5d 3b 20 20 20 20 20 2f 2a 20 41 20 ic[8]; /* A
11c41 62 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 74 buffer to hold t
11c42 68 65 20 6d 61 67 69 63 20 68 65 61 64 65 72 20 he magic header
11c43 2a 2f 0a 20 20 69 36 34 20 69 48 64 72 4f 66 66 */. i64 iHdrOff
11c44 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
11c45 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 6a /* Offset of j
11c46 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 62 65 ournal header be
11c47 69 6e 67 20 72 65 61 64 20 2a 2f 0a 0a 20 20 61 ing read */.. a
11c48 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 ssert( isOpen(pP
11c49 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 20 20 20 ager->jfd) );
11c4a 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c 20 66 69 /* Journal fi
11c4b 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 2e le must be open.
11c4c 20 2a 2f 0a 0a 20 20 2f 2a 20 41 64 76 61 6e 63 */.. /* Advanc
11c4d 65 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f e Pager.journalO
11c4e 66 66 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 ff to the start
11c4f 6f 66 20 74 68 65 20 6e 65 78 74 20 73 65 63 74 of the next sect
11c50 6f 72 2e 20 49 66 20 74 68 65 0a 20 20 2a 2a 20 or. If the. **
11c51 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 journal file is
11c52 74 6f 6f 20 73 6d 61 6c 6c 20 66 6f 72 20 74 68 too small for th
11c53 65 72 65 20 74 6f 20 62 65 20 61 20 68 65 61 64 ere to be a head
11c54 65 72 20 73 74 6f 72 65 64 20 61 74 20 74 68 69 er stored at thi
11c55 73 0a 20 20 2a 2a 20 70 6f 69 6e 74 2c 20 72 65 s. ** point, re
11c56 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 turn SQLITE_DONE
11c57 2e 0a 20 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d .. */. pPager-
11c58 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 6a 6f >journalOff = jo
11c59 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 28 70 urnalHdrOffset(p
11c5a 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20 70 50 Pager);. if( pP
11c5b 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
11c5c 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 +JOURNAL_HDR_SZ(
11c5d 70 50 61 67 65 72 29 20 3e 20 6a 6f 75 72 6e 61 pPager) > journa
11c5e 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 65 74 lSize ){. ret
11c5f 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b urn SQLITE_DONE;
11c60 0a 20 20 7d 0a 20 20 69 48 64 72 4f 66 66 20 3d . }. iHdrOff =
11c61 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
11c62 4f 66 66 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20 Off;.. /* Read
11c63 69 6e 20 74 68 65 20 66 69 72 73 74 20 38 20 62 in the first 8 b
11c64 79 74 65 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 ytes of the jour
11c65 6e 61 6c 20 68 65 61 64 65 72 2e 20 49 66 20 74 nal header. If t
11c66 68 65 79 20 64 6f 20 6e 6f 74 20 6d 61 74 63 68 hey do not match
11c67 0a 20 20 2a 2a 20 74 68 65 20 20 6d 61 67 69 63 . ** the magic
11c68 20 73 74 72 69 6e 67 20 66 6f 75 6e 64 20 61 74 string found at
11c69 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 65 61 the start of ea
11c6a 63 68 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 ch journal heade
11c6b 72 2c 20 72 65 74 75 72 6e 0a 20 20 2a 2a 20 53 r, return. ** S
11c6c 51 4c 49 54 45 5f 44 4f 4e 45 2e 20 49 66 20 61 QLITE_DONE. If a
11c6d 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 n IO error occur
11c6e 73 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 s, return an err
11c6f 6f 72 20 63 6f 64 65 2e 20 4f 74 68 65 72 77 69 or code. Otherwi
11c70 73 65 2c 0a 20 20 2a 2a 20 70 72 6f 63 65 65 64 se,. ** proceed
11c71 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 48 .. */. if( isH
11c72 6f 74 20 7c 7c 20 69 48 64 72 4f 66 66 21 3d 70 ot || iHdrOff!=p
11c73 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 Pager->journalHd
11c74 72 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 r ){. rc = sq
11c75 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 67 lite3OsRead(pPag
11c76 65 72 2d 3e 6a 66 64 2c 20 61 4d 61 67 69 63 2c er->jfd, aMagic,
11c77 20 73 69 7a 65 6f 66 28 61 4d 61 67 69 63 29 2c sizeof(aMagic),
11c78 20 69 48 64 72 4f 66 66 29 3b 0a 20 20 20 20 69 iHdrOff);. i
11c79 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 f( rc ){. r
11c7a 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
11c7b 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 61 if( memcmp(a
11c7c 4d 61 67 69 63 2c 20 61 4a 6f 75 72 6e 61 6c 4d Magic, aJournalM
11c7d 61 67 69 63 2c 20 73 69 7a 65 6f 66 28 61 4d 61 agic, sizeof(aMa
11c7e 67 69 63 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 gic))!=0 ){.
11c7f 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
11c80 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a DONE;. }. }.
11c81 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 66 . /* Read the f
11c82 69 72 73 74 20 74 68 72 65 65 20 33 32 2d 62 69 irst three 32-bi
11c83 74 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 t fields of the
11c84 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 3a 20 journal header:
11c85 54 68 65 20 6e 52 65 63 0a 20 20 2a 2a 20 66 69 The nRec. ** fi
11c86 65 6c 64 2c 20 74 68 65 20 63 68 65 63 6b 73 75 eld, the checksu
11c87 6d 2d 69 6e 69 74 69 61 6c 69 7a 65 72 20 61 6e m-initializer an
11c88 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 d the database s
11c89 69 7a 65 20 61 74 20 74 68 65 20 73 74 61 72 74 ize at the start
11c8a 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 74 72 61 . ** of the tra
11c8b 6e 73 61 63 74 69 6f 6e 2e 20 52 65 74 75 72 6e nsaction. Return
11c8c 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 an error code i
11c8d 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 20 f anything goes
11c8e 77 72 6f 6e 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 wrong.. */. if
11c8f 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 ( SQLITE_OK!=(rc
11c90 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 70 50 = read32bits(pP
11c91 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f ager->jfd, iHdrO
11c92 66 66 2b 38 2c 20 70 4e 52 65 63 29 29 0a 20 20 ff+8, pNRec)).
11c93 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 || SQLITE_OK!=(
11c94 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 rc = read32bits(
11c95 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 pPager->jfd, iHd
11c96 72 4f 66 66 2b 31 32 2c 20 26 70 50 61 67 65 72 rOff+12, &pPager
11c97 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 29 0a 20 20 ->cksumInit)).
11c98 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 || SQLITE_OK!=(
11c99 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 rc = read32bits(
11c9a 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 pPager->jfd, iHd
11c9b 72 4f 66 66 2b 31 36 2c 20 70 44 62 53 69 7a 65 rOff+16, pDbSize
11c9c 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 )). ){. retu
11c9d 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 69 66 rn rc;. }.. if
11c9e 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 ( pPager->journa
11c9f 6c 4f 66 66 3d 3d 30 20 29 7b 0a 20 20 20 20 75 lOff==0 ){. u
11ca0 33 32 20 69 50 61 67 65 53 69 7a 65 3b 20 20 20 32 iPageSize;
11ca1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
11ca2 61 67 65 2d 73 69 7a 65 20 66 69 65 6c 64 20 6f age-size field o
11ca3 66 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 f journal header
11ca4 20 2a 2f 0a 20 20 20 20 75 33 32 20 69 53 65 63 */. u32 iSec
11ca5 74 6f 72 53 69 7a 65 3b 20 20 20 20 20 20 20 20 torSize;
11ca6 20 20 20 20 20 2f 2a 20 53 65 63 74 6f 72 2d 73 /* Sector-s
11ca7 69 7a 65 20 66 69 65 6c 64 20 6f 66 20 6a 6f 75 ize field of jou
11ca8 72 6e 61 6c 20 68 65 61 64 65 72 20 2a 2f 0a 20 rnal header */.
11ca9 20 20 20 75 31 36 20 69 50 61 67 65 53 69 7a 65 u16 iPageSize
11caa 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 16;
11cab 2f 2a 20 43 6f 70 79 20 6f 66 20 69 50 61 67 65 /* Copy of iPage
11cac 53 69 7a 65 20 69 6e 20 31 36 2d 62 69 74 20 76 Size in 16-bit v
11cad 61 72 69 61 62 6c 65 20 2a 2f 0a 0a 20 20 20 20 ariable */..
11cae 2f 2a 20 52 65 61 64 20 74 68 65 20 70 61 67 65 /* Read the page
11caf 2d 73 69 7a 65 20 61 6e 64 20 73 65 63 74 6f 72 -size and sector
11cb0 2d 73 69 7a 65 20 6a 6f 75 72 6e 61 6c 20 68 65 -size journal he
11cb1 61 64 65 72 20 66 69 65 6c 64 73 2e 20 2a 2f 0a ader fields. */.
11cb2 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f if( SQLITE_O
11cb3 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 62 K!=(rc = read32b
11cb4 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c its(pPager->jfd,
11cb5 20 69 48 64 72 4f 66 66 2b 32 30 2c 20 26 69 53 iHdrOff+20, &iS
11cb6 65 63 74 6f 72 53 69 7a 65 29 29 0a 20 20 20 20 ectorSize)).
11cb7 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 || SQLITE_OK!=(
11cb8 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 rc = read32bits(
11cb9 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 pPager->jfd, iHd
11cba 72 4f 66 66 2b 32 34 2c 20 26 69 50 61 67 65 53 rOff+24, &iPageS
11cbb 69 7a 65 29 29 0a 20 20 20 20 29 7b 0a 20 20 20 ize)). ){.
11cbc 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
11cbd 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 }.. /* Chec
11cbe 6b 20 74 68 61 74 20 74 68 65 20 76 61 6c 75 65 k that the value
11cbf 73 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 s read from the
11cc0 70 61 67 65 2d 73 69 7a 65 20 61 6e 64 20 73 65 page-size and se
11cc1 63 74 6f 72 2d 73 69 7a 65 20 66 69 65 6c 64 73 ctor-size fields
11cc2 0a 20 20 20 20 2a 2a 20 61 72 65 20 77 69 74 68 . ** are with
11cc3 69 6e 20 72 61 6e 67 65 2e 20 54 6f 20 62 65 20 in range. To be
11cc4 27 69 6e 20 72 61 6e 67 65 27 2c 20 62 6f 74 68 'in range', both
11cc5 20 76 61 6c 75 65 73 20 6e 65 65 64 20 74 6f 20 values need to
11cc6 62 65 20 61 20 70 6f 77 65 72 0a 20 20 20 20 2a be a power. *
11cc7 2a 20 6f 66 20 74 77 6f 20 67 72 65 61 74 65 72 * of two greater
11cc8 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 than or equal t
11cc9 6f 20 35 31 32 2c 20 61 6e 64 20 6e 6f 74 20 67 o 512, and not g
11cca 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 69 reater than thei
11ccb 72 20 0a 20 20 20 20 2a 2a 20 72 65 73 70 65 63 r . ** respec
11ccc 74 69 76 65 20 63 6f 6d 70 69 6c 65 20 74 69 6d tive compile tim
11ccd 65 20 6d 61 78 69 6d 75 6d 20 6c 69 6d 69 74 73 e maximum limits
11cce 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
11ccf 20 69 50 61 67 65 53 69 7a 65 3c 35 31 32 20 20 iPageSize<512
11cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11cd1 7c 7c 20 69 53 65 63 74 6f 72 53 69 7a 65 3c 35 || iSectorSize<5
11cd2 31 32 0a 20 20 20 20 20 7c 7c 20 69 50 61 67 65 12. || iPage
11cd3 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f Size>SQLITE_MAX_
11cd4 50 41 47 45 5f 53 49 5a 45 20 7c 7c 20 69 53 65 PAGE_SIZE || iSe
11cd5 63 74 6f 72 53 69 7a 65 3e 4d 41 58 5f 53 45 43 ctorSize>MAX_SEC
11cd6 54 4f 52 5f 53 49 5a 45 0a 20 20 20 20 20 7c 7c TOR_SIZE. ||
11cd7 20 28 28 69 50 61 67 65 53 69 7a 65 2d 31 29 26 ((iPageSize-1)&
11cd8 69 50 61 67 65 53 69 7a 65 29 21 3d 30 20 20 20 iPageSize)!=0
11cd9 7c 7c 20 28 28 69 53 65 63 74 6f 72 53 69 7a 65 || ((iSectorSize
11cda 2d 31 29 26 69 53 65 63 74 6f 72 53 69 7a 65 29 -1)&iSectorSize)
11cdb 21 3d 30 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 !=0 . ){.
11cdc 20 20 2f 2a 20 49 66 20 74 68 65 20 65 69 74 68 /* If the eith
11cdd 65 72 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 er the page-size
11cde 20 6f 72 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 or sector-size
11cdf 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 68 in the journal-h
11ce0 65 61 64 65 72 20 69 73 20 0a 20 20 20 20 20 20 eader is .
11ce1 2a 2a 20 69 6e 76 61 6c 69 64 2c 20 74 68 65 6e ** invalid, then
11ce2 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 68 61 the process tha
11ce3 74 20 77 72 6f 74 65 20 74 68 65 20 6a 6f 75 72 t wrote the jour
11ce4 6e 61 6c 2d 68 65 61 64 65 72 20 6d 75 73 74 20 nal-header must
11ce5 68 61 76 65 20 0a 20 20 20 20 20 20 2a 2a 20 63 have . ** c
11ce6 72 61 73 68 65 64 20 62 65 66 6f 72 65 20 74 68 rashed before th
11ce7 65 20 68 65 61 64 65 72 20 77 61 73 20 73 79 6e e header was syn
11ce8 63 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 ced. In this cas
11ce9 65 20 73 74 6f 70 20 72 65 61 64 69 6e 67 20 0a e stop reading .
11cea 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f 75 ** the jou
11ceb 72 6e 61 6c 20 66 69 6c 65 20 68 65 72 65 2e 0a rnal file here..
11cec 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 */. r
11ced 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e eturn SQLITE_DON
11cee 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a E;. }.. /*
11cef 20 55 70 64 61 74 65 20 74 68 65 20 70 61 67 65 Update the page
11cf0 2d 73 69 7a 65 20 74 6f 20 6d 61 74 63 68 20 74 -size to match t
11cf1 68 65 20 76 61 6c 75 65 20 72 65 61 64 20 66 72 he value read fr
11cf2 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 om the journal.
11cf3 0a 20 20 20 20 2a 2a 20 55 73 65 20 61 20 74 65 . ** Use a te
11cf4 73 74 63 61 73 65 28 29 20 6d 61 63 72 6f 20 74 stcase() macro t
11cf5 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 o make sure that
11cf6 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 malloc failure
11cf7 77 69 74 68 69 6e 20 0a 20 20 20 20 2a 2a 20 50 within . ** P
11cf8 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 agerSetPagesize(
11cf9 29 20 69 73 20 74 65 73 74 65 64 2e 0a 20 20 20 ) is tested..
11cfa 20 2a 2f 0a 20 20 20 20 69 50 61 67 65 53 69 7a */. iPageSiz
11cfb 65 31 36 20 3d 20 28 75 31 36 29 69 50 61 67 65 e16 = (u16)iPage
11cfc 53 69 7a 65 3b 0a 20 20 20 20 72 63 20 3d 20 73 Size;. rc = s
11cfd 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61 qlite3PagerSetPa
11cfe 67 65 73 69 7a 65 28 70 50 61 67 65 72 2c 20 26 gesize(pPager, &
11cff 69 50 61 67 65 53 69 7a 65 31 36 2c 20 2d 31 29 iPageSize16, -1)
11d00 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 ;. testcase(
11d01 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b rc!=SQLITE_OK );
11d02 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 21 . assert( rc!
11d03 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 50 =SQLITE_OK || iP
11d04 61 67 65 53 69 7a 65 31 36 3d 3d 28 75 31 36 29 ageSize16==(u16)
11d05 69 50 61 67 65 53 69 7a 65 20 29 3b 0a 0a 20 20 iPageSize );..
11d06 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 /* Update the
11d07 61 73 73 75 6d 65 64 20 73 65 63 74 6f 72 2d 73 assumed sector-s
11d08 69 7a 65 20 74 6f 20 6d 61 74 63 68 20 74 68 65 ize to match the
11d09 20 76 61 6c 75 65 20 75 73 65 64 20 62 79 20 0a value used by .
11d0a 20 20 20 20 2a 2a 20 74 68 65 20 70 72 6f 63 65 ** the proce
11d0b 73 73 20 74 68 61 74 20 63 72 65 61 74 65 64 20 ss that created
11d0c 74 68 69 73 20 6a 6f 75 72 6e 61 6c 2e 20 49 66 this journal. If
11d0d 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 77 61 this journal wa
11d0e 73 0a 20 20 20 20 2a 2a 20 63 72 65 61 74 65 64 s. ** created
11d0f 20 62 79 20 61 20 70 72 6f 63 65 73 73 20 6f 74 by a process ot
11d10 68 65 72 20 74 68 61 6e 20 74 68 69 73 20 6f 6e her than this on
11d11 65 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 e, then this rou
11d12 74 69 6e 65 0a 20 20 20 20 2a 2a 20 69 73 20 62 tine. ** is b
11d13 65 69 6e 67 20 63 61 6c 6c 65 64 20 66 72 6f 6d eing called from
11d14 20 77 69 74 68 69 6e 20 70 61 67 65 72 5f 70 6c within pager_pl
11d15 61 79 62 61 63 6b 28 29 2e 20 54 68 65 20 6c 6f ayback(). The lo
11d16 63 61 6c 20 76 61 6c 75 65 0a 20 20 20 20 2a 2a cal value. **
11d17 20 6f 66 20 50 61 67 65 72 2e 73 65 63 74 6f 72 of Pager.sector
11d18 53 69 7a 65 20 69 73 20 72 65 73 74 6f 72 65 64 Size is restored
11d19 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 at the end of t
11d1a 68 61 74 20 72 6f 75 74 69 6e 65 2e 0a 20 20 20 hat routine..
11d1b 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d 3e */. pPager->
11d1c 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 69 53 65 sectorSize = iSe
11d1d 63 74 6f 72 53 69 7a 65 3b 0a 20 20 7d 0a 0a 20 ctorSize;. }..
11d1e 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
11d1f 4f 66 66 20 2b 3d 20 4a 4f 55 52 4e 41 4c 5f 48 Off += JOURNAL_H
11d20 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 DR_SZ(pPager);.
11d21 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a return rc;.}...
11d22 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 /*.** Write the
11d23 73 75 70 70 6c 69 65 64 20 6d 61 73 74 65 72 20 supplied master
11d24 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 69 6e 74 journal name int
11d25 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 o the journal fi
11d26 6c 65 20 66 6f 72 20 70 61 67 65 72 0a 2a 2a 20 le for pager.**
11d27 70 50 61 67 65 72 20 61 74 20 74 68 65 20 63 75 pPager at the cu
11d28 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 2e 20 rrent location.
11d29 54 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e The master journ
11d2a 61 6c 20 6e 61 6d 65 20 6d 75 73 74 20 62 65 20 al name must be
11d2b 74 68 65 20 6c 61 73 74 0a 2a 2a 20 74 68 69 6e the last.** thin
11d2c 67 20 77 72 69 74 74 65 6e 20 74 6f 20 61 20 6a g written to a j
11d2d 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 ournal file. If
11d2e 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 the pager is in
11d2f 66 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 full-sync mode,
11d30 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 the.** journal f
11d31 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 ile descriptor i
11d32 73 20 61 64 76 61 6e 63 65 64 20 74 6f 20 74 68 s advanced to th
11d33 65 20 6e 65 78 74 20 73 65 63 74 6f 72 20 62 6f e next sector bo
11d34 75 6e 64 61 72 79 20 62 65 66 6f 72 65 0a 2a 2a undary before.**
11d35 20 61 6e 79 74 68 69 6e 67 20 69 73 20 77 72 69 anything is wri
11d36 74 74 65 6e 2e 20 54 68 65 20 66 6f 72 6d 61 74 tten. The format
11d37 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2b 20 34 is:.**.** + 4
11d38 20 62 79 74 65 73 3a 20 50 41 47 45 52 5f 4d 4a bytes: PAGER_MJ
11d39 5f 50 47 4e 4f 2e 0a 2a 2a 20 20 20 2b 20 4e 20 _PGNO..** + N
11d3a 62 79 74 65 73 3a 20 4d 61 73 74 65 72 20 6a 6f bytes: Master jo
11d3b 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20 69 urnal filename i
11d3c 6e 20 75 74 66 2d 38 2e 0a 2a 2a 20 20 20 2b 20 n utf-8..** +
11d3d 34 20 62 79 74 65 73 3a 20 4e 20 28 6c 65 6e 67 4 bytes: N (leng
11d3e 74 68 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 th of master jou
11d3f 72 6e 61 6c 20 6e 61 6d 65 20 69 6e 20 62 79 74 rnal name in byt
11d40 65 73 2c 20 6e 6f 20 6e 75 6c 2d 74 65 72 6d 69 es, no nul-termi
11d41 6e 61 74 6f 72 29 2e 0a 2a 2a 20 20 20 2b 20 34 nator)..** + 4
11d42 20 62 79 74 65 73 3a 20 4d 61 73 74 65 72 20 6a bytes: Master j
11d43 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 63 68 65 63 ournal name chec
11d44 6b 73 75 6d 2e 0a 2a 2a 20 20 20 2b 20 38 20 62 ksum..** + 8 b
11d45 79 74 65 73 3a 20 61 4a 6f 75 72 6e 61 6c 4d 61 ytes: aJournalMa
11d46 67 69 63 5b 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 gic[]..**.** The
11d47 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
11d48 70 61 67 65 20 63 68 65 63 6b 73 75 6d 20 69 73 page checksum is
11d49 20 74 68 65 20 73 75 6d 20 6f 66 20 74 68 65 20 the sum of the
11d4a 62 79 74 65 73 20 69 6e 20 74 68 65 20 6d 61 73 bytes in the mas
11d4b 74 65 72 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 6e ter.** journal n
11d4c 61 6d 65 2c 20 77 68 65 72 65 20 65 61 63 68 20 ame, where each
11d4d 62 79 74 65 20 69 73 20 69 6e 74 65 72 70 72 65 byte is interpre
11d4e 74 65 64 20 61 73 20 61 20 73 69 67 6e 65 64 20 ted as a signed
11d4f 38 2d 62 69 74 20 69 6e 74 65 67 65 72 2e 0a 2a 8-bit integer..*
11d50 2a 0a 2a 2a 20 49 66 20 7a 4d 61 73 74 65 72 20 *.** If zMaster
11d51 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 is a NULL pointe
11d52 72 20 28 6f 63 63 75 72 73 20 66 6f 72 20 61 20 r (occurs for a
11d53 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 single database
11d54 74 72 61 6e 73 61 63 74 69 6f 6e 29 2c 20 0a 2a transaction), .*
11d55 2a 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20 61 * this call is a
11d56 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 no-op..*/.stati
11d57 63 20 69 6e 74 20 77 72 69 74 65 4d 61 73 74 65 c int writeMaste
11d58 72 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a rJournal(Pager *
11d59 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 63 68 pPager, const ch
11d5a 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20 ar *zMaster){.
11d5b 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 int rc;
11d5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11d5d 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 /* Return code
11d5e 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61 73 74 65 72 */. int nMaster
11d5f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
11d60 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 /* Length
11d61 6f 66 20 73 74 72 69 6e 67 20 7a 4d 61 73 74 65 of string zMaste
11d62 72 20 2a 2f 0a 20 20 69 36 34 20 69 48 64 72 4f r */. i64 iHdrO
11d63 66 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ff;
11d64 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 /* Offse
11d65 74 20 6f 66 20 68 65 61 64 65 72 20 69 6e 20 6a t of header in j
11d66 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 ournal file */.
11d67 20 69 36 34 20 6a 72 6e 6c 53 69 7a 65 3b 20 20 i64 jrnlSize;
11d68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11d69 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6a 6f 75 /* Size of jou
11d6a 72 6e 61 6c 20 66 69 6c 65 20 6f 6e 20 64 69 73 rnal file on dis
11d6b 6b 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d k */. u32 cksum
11d6c 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
11d6d 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b /* Check
11d6e 73 75 6d 20 6f 66 20 73 74 72 69 6e 67 20 7a 4d sum of string zM
11d6f 61 73 74 65 72 20 2a 2f 0a 0a 20 20 69 66 28 20 aster */.. if(
11d70 21 7a 4d 61 73 74 65 72 20 7c 7c 20 70 50 61 67 !zMaster || pPag
11d71 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 0a 20 20 er->setMaster.
11d72 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 || pPager->jour
11d73 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a nalMode==PAGER_J
11d74 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 OURNALMODE_MEMOR
11d75 59 20 0a 20 20 20 7c 7c 20 70 50 61 67 65 72 2d Y . || pPager-
11d76 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 >journalMode==PA
11d77 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
11d78 4f 46 46 20 0a 20 20 29 7b 0a 20 20 20 20 72 65 OFF . ){. re
11d79 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
11d7a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 73 65 }. pPager->se
11d7b 74 4d 61 73 74 65 72 20 3d 20 31 3b 0a 20 20 61 tMaster = 1;. a
11d7c 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 ssert( isOpen(pP
11d7d 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 0a 20 ager->jfd) );..
11d7e 20 2f 2a 20 43 61 6c 63 75 6c 61 74 65 20 74 68 /* Calculate th
11d7f 65 20 6c 65 6e 67 74 68 20 69 6e 20 62 79 74 65 e length in byte
11d80 73 20 61 6e 64 20 74 68 65 20 63 68 65 63 6b 73 s and the checks
11d81 75 6d 20 6f 66 20 7a 4d 61 73 74 65 72 20 2a 2f um of zMaster */
11d82 0a 20 20 66 6f 72 28 6e 4d 61 73 74 65 72 3d 30 . for(nMaster=0
11d83 3b 20 7a 4d 61 73 74 65 72 5b 6e 4d 61 73 74 65 ; zMaster[nMaste
11d84 72 5d 3b 20 6e 4d 61 73 74 65 72 2b 2b 29 7b 0a r]; nMaster++){.
11d85 20 20 20 20 63 6b 73 75 6d 20 2b 3d 20 7a 4d 61 cksum += zMa
11d86 73 74 65 72 5b 6e 4d 61 73 74 65 72 5d 3b 0a 20 ster[nMaster];.
11d87 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 69 6e 20 66 }.. /* If in f
11d88 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 61 ull-sync mode, a
11d89 64 76 61 6e 63 65 20 74 6f 20 74 68 65 20 6e 65 dvance to the ne
11d8a 78 74 20 64 69 73 6b 20 73 65 63 74 6f 72 20 62 xt disk sector b
11d8b 65 66 6f 72 65 20 77 72 69 74 69 6e 67 0a 20 20 efore writing.
11d8c 2a 2a 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f ** the master jo
11d8d 75 72 6e 61 6c 20 6e 61 6d 65 2e 20 54 68 69 73 urnal name. This
11d8e 20 69 73 20 69 6e 20 63 61 73 65 20 74 68 65 20 is in case the
11d8f 70 72 65 76 69 6f 75 73 20 70 61 67 65 20 77 72 previous page wr
11d90 69 74 74 65 6e 20 74 6f 0a 20 20 2a 2a 20 74 68 itten to. ** th
11d91 65 20 6a 6f 75 72 6e 61 6c 20 68 61 73 20 61 6c e journal has al
11d92 72 65 61 64 79 20 62 65 65 6e 20 73 79 6e 63 65 ready been synce
11d93 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 d.. */. if( pP
11d94 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 29 ager->fullSync )
11d95 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f {. pPager->jo
11d96 75 72 6e 61 6c 4f 66 66 20 3d 20 6a 6f 75 72 6e urnalOff = journ
11d97 61 6c 48 64 72 4f 66 66 73 65 74 28 70 50 61 67 alHdrOffset(pPag
11d98 65 72 29 3b 0a 20 20 7d 0a 20 20 69 48 64 72 4f er);. }. iHdrO
11d99 66 66 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 ff = pPager->jou
11d9a 72 6e 61 6c 4f 66 66 3b 0a 0a 20 20 2f 2a 20 57 rnalOff;.. /* W
11d9b 72 69 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 rite the master
11d9c 6a 6f 75 72 6e 61 6c 20 64 61 74 61 20 74 6f 20 journal data to
11d9d 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6a the end of the j
11d9e 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 0a ournal file. If.
11d9f 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 63 ** an error oc
11da0 63 75 72 73 2c 20 72 65 74 75 72 6e 20 74 68 65 curs, return the
11da1 20 65 72 72 6f 72 20 63 6f 64 65 20 74 6f 20 74 error code to t
11da2 68 65 20 63 61 6c 6c 65 72 2e 0a 20 20 2a 2f 0a he caller.. */.
11da3 20 20 69 66 28 20 28 30 20 21 3d 20 28 72 63 20 if( (0 != (rc
11da4 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 70 50 = write32bits(pP
11da5 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f ager->jfd, iHdrO
11da6 66 66 2c 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e ff, PAGER_MJ_PGN
11da7 4f 28 70 50 61 67 65 72 29 29 29 29 0a 20 20 20 O(pPager)))).
11da8 7c 7c 20 28 30 20 21 3d 20 28 72 63 20 3d 20 73 || (0 != (rc = s
11da9 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 qlite3OsWrite(pP
11daa 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74 ager->jfd, zMast
11dab 65 72 2c 20 6e 4d 61 73 74 65 72 2c 20 69 48 64 er, nMaster, iHd
11dac 72 4f 66 66 2b 34 29 29 29 0a 20 20 20 7c 7c 20 rOff+4))). ||
11dad 28 30 20 21 3d 20 28 72 63 20 3d 20 77 72 69 74 (0 != (rc = writ
11dae 65 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e e32bits(pPager->
11daf 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 34 2b 6e jfd, iHdrOff+4+n
11db0 4d 61 73 74 65 72 2c 20 6e 4d 61 73 74 65 72 29 Master, nMaster)
11db1 29 29 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20 28 )). || (0 != (
11db2 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 74 73 rc = write32bits
11db3 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 (pPager->jfd, iH
11db4 64 72 4f 66 66 2b 34 2b 6e 4d 61 73 74 65 72 2b drOff+4+nMaster+
11db5 34 2c 20 63 6b 73 75 6d 29 29 29 0a 20 20 20 7c 4, cksum))). |
11db6 7c 20 28 30 20 21 3d 20 28 72 63 20 3d 20 73 71 | (0 != (rc = sq
11db7 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 lite3OsWrite(pPa
11db8 67 65 72 2d 3e 6a 66 64 2c 20 61 4a 6f 75 72 6e ger->jfd, aJourn
11db9 61 6c 4d 61 67 69 63 2c 20 38 2c 20 69 48 64 72 alMagic, 8, iHdr
11dba 4f 66 66 2b 34 2b 6e 4d 61 73 74 65 72 2b 38 29 Off+4+nMaster+8)
11dbb 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 )). ){. retu
11dbc 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 50 61 rn rc;. }. pPa
11dbd 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 ger->journalOff
11dbe 2b 3d 20 28 6e 4d 61 73 74 65 72 2b 32 30 29 3b += (nMaster+20);
11dbf 0a 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 . pPager->needS
11dc0 79 6e 63 20 3d 20 21 70 50 61 67 65 72 2d 3e 6e ync = !pPager->n
11dc1 6f 53 79 6e 63 3b 0a 0a 20 20 2f 2a 20 49 66 20 oSync;.. /* If
11dc2 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 the pager is in
11dc3 70 65 72 69 73 74 65 6e 74 2d 6a 6f 75 72 6e 61 peristent-journa
11dc4 6c 20 6d 6f 64 65 2c 20 74 68 65 6e 20 74 68 65 l mode, then the
11dc5 20 70 68 79 73 69 63 61 6c 20 0a 20 20 2a 2a 20 physical . **
11dc6 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 20 6d 61 79 journal-file may
11dc7 20 65 78 74 65 6e 64 20 70 61 73 74 20 74 68 65 extend past the
11dc8 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 61 73 74 end of the mast
11dc9 65 72 2d 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 0a er-journal name.
11dca 20 20 2a 2a 20 61 6e 64 20 38 20 62 79 74 65 73 ** and 8 bytes
11dcb 20 6f 66 20 6d 61 67 69 63 20 64 61 74 61 20 6a of magic data j
11dcc 75 73 74 20 77 72 69 74 74 65 6e 20 74 6f 20 74 ust written to t
11dcd 68 65 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73 he file. This is
11dce 20 0a 20 20 2a 2a 20 64 61 6e 67 65 72 6f 75 73 . ** dangerous
11dcf 20 62 65 63 61 75 73 65 20 74 68 65 20 63 6f 64 because the cod
11dd0 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 61 20 e to rollback a
11dd1 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 hot-journal file
11dd2 0a 20 20 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 . ** will not b
11dd3 65 20 61 62 6c 65 20 74 6f 20 66 69 6e 64 20 74 e able to find t
11dd4 68 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 he master-journa
11dd5 6c 20 6e 61 6d 65 20 74 6f 20 64 65 74 65 72 6d l name to determ
11dd6 69 6e 65 20 0a 20 20 2a 2a 20 77 68 65 74 68 65 ine . ** whethe
11dd7 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 6a 6f 75 r or not the jou
11dd8 72 6e 61 6c 20 69 73 20 68 6f 74 2e 20 0a 20 20 rnal is hot. .
11dd9 2a 2a 0a 20 20 2a 2a 20 45 61 73 69 65 73 74 20 **. ** Easiest
11dda 74 68 69 6e 67 20 74 6f 20 64 6f 20 69 6e 20 74 thing to do in t
11ddb 68 69 73 20 73 63 65 6e 61 72 69 6f 20 69 73 20 his scenario is
11ddc 74 6f 20 74 72 75 6e 63 61 74 65 20 74 68 65 20 to truncate the
11ddd 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 66 69 journal . ** fi
11dde 6c 65 20 74 6f 20 74 68 65 20 72 65 71 75 69 72 le to the requir
11ddf 65 64 20 73 69 7a 65 2e 0a 20 20 2a 2f 20 0a 20 ed size.. */ .
11de0 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d if( SQLITE_OK==
11de1 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 (rc = sqlite3OsF
11de2 69 6c 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e ileSize(pPager->
11de3 6a 66 64 2c 20 26 6a 72 6e 6c 53 69 7a 65 29 29 jfd, &jrnlSize))
11de4 0a 20 20 20 26 26 20 6a 72 6e 6c 53 69 7a 65 3e . && jrnlSize>
11de5 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
11de6 66 66 0a 20 20 29 7b 0a 20 20 20 20 72 63 20 3d ff. ){. rc =
11de7 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 sqlite3OsTrunca
11de8 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 te(pPager->jfd,
11de9 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
11dea 66 66 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ff);. }. retur
11deb 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 n rc;.}../*.** F
11dec 69 6e 64 20 61 20 70 61 67 65 20 69 6e 20 74 68 ind a page in th
11ded 65 20 68 61 73 68 20 74 61 62 6c 65 20 67 69 76 e hash table giv
11dee 65 6e 20 69 74 73 20 70 61 67 65 20 6e 75 6d 62 en its page numb
11def 65 72 2e 20 52 65 74 75 72 6e 0a 2a 2a 20 61 20 er. Return.** a
11df0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 pointer to the p
11df1 61 67 65 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 age or NULL if t
11df2 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61 67 he requested pag
11df3 65 20 69 73 20 6e 6f 74 20 0a 2a 2a 20 61 6c 72 e is not .** alr
11df4 65 61 64 79 20 69 6e 20 6d 65 6d 6f 72 79 2e 0a eady in memory..
11df5 2a 2f 0a 73 74 61 74 69 63 20 50 67 48 64 72 20 */.static PgHdr
11df6 2a 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 50 61 *pager_lookup(Pa
11df7 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e ger *pPager, Pgn
11df8 6f 20 70 67 6e 6f 29 7b 0a 20 20 50 67 48 64 72 o pgno){. PgHdr
11df9 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 *p;
11dfa 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
11dfb 52 65 74 75 72 6e 20 76 61 6c 75 65 20 2a 2f 0a Return value */.
11dfc 0a 20 20 2f 2a 20 49 74 20 69 73 20 6e 6f 74 20 . /* It is not
11dfd 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 61 20 63 possible for a c
11dfe 61 6c 6c 20 74 6f 20 50 63 61 63 68 65 46 65 74 all to PcacheFet
11dff 63 68 28 29 20 77 69 74 68 20 63 72 65 61 74 65 ch() with create
11e00 46 6c 61 67 3d 3d 30 20 74 6f 0a 20 20 2a 2a 20 Flag==0 to. **
11e01 66 61 69 6c 2c 20 73 69 6e 63 65 20 6e 6f 20 61 fail, since no a
11e02 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 61 ttempt to alloca
11e03 74 65 20 64 79 6e 61 6d 69 63 20 6d 65 6d 6f 72 te dynamic memor
11e04 79 20 77 69 6c 6c 20 62 65 20 6d 61 64 65 2e 0a y will be made..
11e05 20 20 2a 2f 0a 20 20 28 76 6f 69 64 29 73 71 6c */. (void)sql
11e06 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68 28 ite3PcacheFetch(
11e07 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c pPager->pPCache,
11e08 20 70 67 6e 6f 2c 20 30 2c 20 26 70 29 3b 0a 20 pgno, 0, &p);.
11e09 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a return p;.}../*
11e0a 0a 2a 2a 20 55 6e 6c 65 73 73 20 74 68 65 20 70 .** Unless the p
11e0b 61 67 65 72 20 69 73 20 69 6e 20 65 72 72 6f 72 ager is in error
11e0c 2d 73 74 61 74 65 2c 20 64 69 73 63 61 72 64 20 -state, discard
11e0d 61 6c 6c 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 all in-memory pa
11e0e 67 65 73 2e 20 49 66 0a 2a 2a 20 74 68 65 20 70 ges. If.** the p
11e0f 61 67 65 72 20 69 73 20 69 6e 20 65 72 72 6f 72 ager is in error
11e10 2d 73 74 61 74 65 2c 20 74 68 65 6e 20 74 68 69 -state, then thi
11e11 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f s call is a no-o
11e12 70 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 3a 20 57 p..**.** TODO: W
11e13 68 79 20 63 61 6e 20 77 65 20 6e 6f 74 20 72 65 hy can we not re
11e14 73 65 74 20 74 68 65 20 70 61 67 65 72 20 77 68 set the pager wh
11e15 69 6c 65 20 69 6e 20 65 72 72 6f 72 20 73 74 61 ile in error sta
11e16 74 65 3f 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f te?.*/.static vo
11e17 69 64 20 70 61 67 65 72 5f 72 65 73 65 74 28 50 id pager_reset(P
11e18 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 ager *pPager){.
11e19 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d if( SQLITE_OK==
11e1a 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 pPager->errCode
11e1b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 61 ){. sqlite3Ba
11e1c 63 6b 75 70 52 65 73 74 61 72 74 28 70 50 61 67 ckupRestart(pPag
11e1d 65 72 2d 3e 70 42 61 63 6b 75 70 29 3b 0a 20 20 er->pBackup);.
11e1e 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 sqlite3PcacheC
11e1f 6c 65 61 72 28 70 50 61 67 65 72 2d 3e 70 50 43 lear(pPager->pPC
11e20 61 63 68 65 29 3b 0a 20 20 20 20 70 50 61 67 65 ache);. pPage
11e21 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 3d r->dbSizeValid =
11e22 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 0;. }.}../*.**
11e23 20 46 72 65 65 20 61 6c 6c 20 73 74 72 75 63 74 Free all struct
11e24 75 72 65 73 20 69 6e 20 74 68 65 20 50 61 67 65 ures in the Page
11e25 72 2e 61 53 61 76 65 70 6f 69 6e 74 5b 5d 20 61 r.aSavepoint[] a
11e26 72 72 61 79 20 61 6e 64 20 73 65 74 20 62 6f 74 rray and set bot
11e27 68 0a 2a 2a 20 50 61 67 65 72 2e 61 53 61 76 65 h.** Pager.aSave
11e28 70 6f 69 6e 74 20 61 6e 64 20 50 61 67 65 72 2e point and Pager.
11e29 6e 53 61 76 65 70 6f 69 6e 74 20 74 6f 20 7a 65 nSavepoint to ze
11e2a 72 6f 2e 20 43 6c 6f 73 65 20 74 68 65 20 73 75 ro. Close the su
11e2b 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 69 66 20 b-journal.** if
11e2c 69 74 20 69 73 20 6f 70 65 6e 20 61 6e 64 20 74 it is open and t
11e2d 68 65 20 70 61 67 65 72 20 69 73 20 6e 6f 74 20 he pager is not
11e2e 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 in exclusive mod
11e2f 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 e..*/.static voi
11e30 64 20 72 65 6c 65 61 73 65 41 6c 6c 53 61 76 65 d releaseAllSave
11e31 70 6f 69 6e 74 73 28 50 61 67 65 72 20 2a 70 50 points(Pager *pP
11e32 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 69 69 3b ager){. int ii;
11e33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
11e34 2a 20 49 74 65 72 61 74 6f 72 20 66 6f 72 20 6c * Iterator for l
11e35 6f 6f 70 69 6e 67 20 74 68 72 6f 75 67 68 20 50 ooping through P
11e36 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 20 ager.aSavepoint
11e37 2a 2f 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 */. for(ii=0; i
11e38 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 i<pPager->nSavep
11e39 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 oint; ii++){.
11e3a 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 sqlite3BitvecDe
11e3b 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e 61 53 stroy(pPager->aS
11e3c 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 70 49 6e avepoint[ii].pIn
11e3d 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 7d 0a Savepoint);. }.
11e3e 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 65 if( !pPager->e
11e3f 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 7c 7c 20 xclusiveMode ||
11e40 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72 sqlite3IsMemJour
11e41 6e 61 6c 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 nal(pPager->sjfd
11e42 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ) ){. sqlite3
11e43 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e OsClose(pPager->
11e44 73 6a 66 64 29 3b 0a 20 20 7d 0a 20 20 73 71 6c sjfd);. }. sql
11e45 69 74 65 33 5f 66 72 65 65 28 70 50 61 67 65 72 ite3_free(pPager
11e46 2d 3e 61 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 ->aSavepoint);.
11e47 20 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f pPager->aSavepo
11e48 69 6e 74 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 int = 0;. pPage
11e49 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 r->nSavepoint =
11e4a 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 53 75 0;. pPager->nSu
11e4b 62 52 65 63 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a bRec = 0;.}../*.
11e4c 2a 2a 20 53 65 74 20 74 68 65 20 62 69 74 20 6e ** Set the bit n
11e4d 75 6d 62 65 72 20 70 67 6e 6f 20 69 6e 20 74 68 umber pgno in th
11e4e 65 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 e PagerSavepoint
11e4f 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 20 0a 2a .pInSavepoint .*
11e50 2a 20 62 69 74 76 65 63 73 20 6f 66 20 61 6c 6c * bitvecs of all
11e51 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 73 open savepoints
11e52 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f . Return SQLITE_
11e53 4f 4b 20 69 66 20 73 75 63 63 65 73 73 66 75 6c OK if successful
11e54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f .** or SQLITE_NO
11e55 4d 45 4d 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 MEM if a malloc
11e56 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73 2e 0a failure occurs..
11e57 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 64 */.static int ad
11e58 64 54 6f 53 61 76 65 70 6f 69 6e 74 42 69 74 76 dToSavepointBitv
11e59 65 63 73 28 50 61 67 65 72 20 2a 70 50 61 67 65 ecs(Pager *pPage
11e5a 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 r, Pgno pgno){.
11e5b 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 int ii;
11e5c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f /* Lo
11e5d 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 op counter */.
11e5e 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
11e5f 4f 4b 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 73 OK; /* Res
11e60 75 6c 74 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 66 ult code */.. f
11e61 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 50 61 67 or(ii=0; ii<pPag
11e62 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20 er->nSavepoint;
11e63 69 69 2b 2b 29 7b 0a 20 20 20 20 50 61 67 65 72 ii++){. Pager
11e64 53 61 76 65 70 6f 69 6e 74 20 2a 70 20 3d 20 26 Savepoint *p = &
11e65 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 pPager->aSavepoi
11e66 6e 74 5b 69 69 5d 3b 0a 20 20 20 20 69 66 28 20 nt[ii];. if(
11e67 70 67 6e 6f 3c 3d 70 2d 3e 6e 4f 72 69 67 20 29 pgno<=p->nOrig )
11e68 7b 0a 20 20 20 20 20 20 72 63 20 7c 3d 20 73 71 {. rc |= sq
11e69 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 70 lite3BitvecSet(p
11e6a 2d 3e 70 49 6e 53 61 76 65 70 6f 69 6e 74 2c 20 ->pInSavepoint,
11e6b 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 74 65 73 pgno);. tes
11e6c 74 63 61 73 65 28 20 72 63 3d 3d 53 51 4c 49 54 tcase( rc==SQLIT
11e6d 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 E_NOMEM );.
11e6e 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c assert( rc==SQL
11e6f 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51 ITE_OK || rc==SQ
11e70 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 LITE_NOMEM );.
11e71 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
11e72 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e rc;.}../*.** Un
11e73 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 73 lock the databas
11e74 65 20 66 69 6c 65 2e 20 54 68 69 73 20 66 75 6e e file. This fun
11e75 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 ction is a no-op
11e76 20 69 66 20 74 68 65 20 70 61 67 65 72 0a 2a 2a if the pager.**
11e77 20 69 73 20 69 6e 20 65 78 63 6c 75 73 69 76 65 is in exclusive
11e78 20 6d 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 mode..**.** If
11e79 74 68 65 20 70 61 67 65 72 20 69 73 20 63 75 72 the pager is cur
11e7a 72 65 6e 74 6c 79 20 69 6e 20 65 72 72 6f 72 20 rently in error
11e7b 73 74 61 74 65 2c 20 64 69 73 63 61 72 64 20 74 state, discard t
11e7c 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 0a he contents of .
11e7d 2a 2a 20 74 68 65 20 63 61 63 68 65 20 61 6e 64 ** the cache and
11e7e 20 72 65 73 65 74 20 74 68 65 20 50 61 67 65 72 reset the Pager
11e7f 20 73 74 72 75 63 74 75 72 65 20 69 6e 74 65 72 structure inter
11e80 6e 61 6c 20 73 74 61 74 65 2e 20 49 66 20 74 68 nal state. If th
11e81 65 72 65 20 69 73 0a 2a 2a 20 61 6e 20 6f 70 65 ere is.** an ope
11e82 6e 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 2c 20 n journal-file,
11e83 74 68 65 6e 20 74 68 65 20 6e 65 78 74 20 74 69 then the next ti
11e84 6d 65 20 61 20 73 68 61 72 65 64 2d 6c 6f 63 6b me a shared-lock
11e85 20 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 is obtained.**
11e86 6f 6e 20 74 68 65 20 70 61 67 65 72 20 66 69 6c on the pager fil
11e87 65 20 28 62 79 20 74 68 69 73 20 6f 72 20 61 6e e (by this or an
11e88 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 29 y other process)
11e89 2c 20 69 74 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 , it will be.**
11e8a 74 72 65 61 74 65 64 20 61 73 20 61 20 68 6f 74 treated as a hot
11e8b 2d 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 72 6f 6c -journal and rol
11e8c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2f 0a 73 74 61 led back..*/.sta
11e8d 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 5f 75 tic void pager_u
11e8e 6e 6c 6f 63 6b 28 50 61 67 65 72 20 2a 70 50 61 nlock(Pager *pPa
11e8f 67 65 72 29 7b 0a 20 20 69 66 28 20 21 70 50 61 ger){. if( !pPa
11e90 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f ger->exclusiveMo
11e91 64 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 de ){. int rc
11e92 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
11e93 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e /* Return
11e94 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 20 20 2f 2a code */.. /*
11e95 20 41 6c 77 61 79 73 20 63 6c 6f 73 65 20 74 68 Always close th
11e96 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 e journal file w
11e97 68 65 6e 20 64 72 6f 70 70 69 6e 67 20 74 68 65 hen dropping the
11e98 20 64 61 74 61 62 61 73 65 20 6c 6f 63 6b 2e 0a database lock..
11e99 20 20 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 ** Otherwise
11e9a 2c 20 61 6e 6f 74 68 65 72 20 63 6f 6e 6e 65 63 , another connec
11e9b 74 69 6f 6e 20 77 69 74 68 20 6a 6f 75 72 6e 61 tion with journa
11e9c 6c 5f 6d 6f 64 65 3d 64 65 6c 65 74 65 20 6d 69 l_mode=delete mi
11e9d 67 68 74 0a 20 20 20 20 2a 2a 20 64 65 6c 65 74 ght. ** delet
11e9e 65 20 74 68 65 20 66 69 6c 65 20 6f 75 74 20 66 e the file out f
11e9f 72 6f 6d 20 75 6e 64 65 72 20 75 73 2e 0a 20 20 rom under us..
11ea0 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 */. sqlite3
11ea1 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e OsClose(pPager->
11ea2 6a 66 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 jfd);. sqlite
11ea3 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 3BitvecDestroy(p
11ea4 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 Pager->pInJourna
11ea5 6c 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e l);. pPager->
11ea6 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a pInJournal = 0;.
11ea7 20 20 20 20 72 65 6c 65 61 73 65 41 6c 6c 53 61 releaseAllSa
11ea8 76 65 70 6f 69 6e 74 73 28 70 50 61 67 65 72 29 vepoints(pPager)
11ea9 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 ;.. /* If the
11eaa 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63 6b 65 file is unlocke
11eab 64 2c 20 73 6f 6d 65 62 6f 64 79 20 65 6c 73 65 d, somebody else
11eac 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 69 74 might change it
11ead 2e 20 54 68 65 0a 20 20 20 20 2a 2a 20 76 61 6c . The. ** val
11eae 75 65 73 20 73 74 6f 72 65 64 20 69 6e 20 50 61 ues stored in Pa
11eaf 67 65 72 2e 64 62 53 69 7a 65 20 65 74 63 2e 20 ger.dbSize etc.
11eb0 6d 69 67 68 74 20 62 65 63 6f 6d 65 20 69 6e 76 might become inv
11eb1 61 6c 69 64 20 69 66 0a 20 20 20 20 2a 2a 20 74 alid if. ** t
11eb2 68 69 73 20 68 61 70 70 65 6e 73 2e 20 54 4f 44 his happens. TOD
11eb3 4f 3a 20 52 65 61 6c 6c 79 2c 20 74 68 69 73 20 O: Really, this
11eb4 64 6f 65 73 6e 27 74 20 6e 65 65 64 20 74 6f 20 doesn't need to
11eb5 62 65 20 63 6c 65 61 72 65 64 0a 20 20 20 20 2a be cleared. *
11eb6 2a 20 75 6e 74 69 6c 20 74 68 65 20 63 68 61 6e * until the chan
11eb7 67 65 2d 63 6f 75 6e 74 65 72 20 63 68 65 63 6b ge-counter check
11eb8 20 66 61 69 6c 73 20 69 6e 20 50 61 67 65 72 53 fails in PagerS
11eb9 68 61 72 65 64 4c 6f 63 6b 28 29 2e 0a 20 20 20 haredLock()..
11eba 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d 3e */. pPager->
11ebb 64 62 53 69 7a 65 56 61 6c 69 64 20 3d 20 30 3b dbSizeValid = 0;
11ebc 0a 0a 20 20 20 20 72 63 20 3d 20 6f 73 55 6e 6c .. rc = osUnl
11ebd 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 ock(pPager->fd,
11ebe 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 NO_LOCK);. if
11ebf 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 70 50 ( rc ){. pP
11ec0 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 ager->errCode =
11ec1 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 49 4f rc;. }. IO
11ec2 54 52 41 43 45 28 28 22 55 4e 4c 4f 43 4b 20 25 TRACE(("UNLOCK %
11ec3 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 0a 0a p\n", pPager))..
11ec4 20 20 20 20 2f 2a 20 49 66 20 50 61 67 65 72 2e /* If Pager.
11ec5 65 72 72 43 6f 64 65 20 69 73 20 73 65 74 2c 20 errCode is set,
11ec6 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
11ec7 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65 20 the pager cache
11ec8 63 61 6e 6e 6f 74 20 62 65 0a 20 20 20 20 2a 2a cannot be. **
11ec9 20 74 72 75 73 74 65 64 2e 20 4e 6f 77 20 74 68 trusted. Now th
11eca 61 74 20 74 68 65 20 70 61 67 65 72 20 66 69 6c at the pager fil
11ecb 65 20 69 73 20 75 6e 6c 6f 63 6b 65 64 2c 20 74 e is unlocked, t
11ecc 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 he contents of t
11ecd 68 65 0a 20 20 20 20 2a 2a 20 63 61 63 68 65 20 he. ** cache
11ece 63 61 6e 20 62 65 20 64 69 73 63 61 72 64 65 64 can be discarded
11ecf 20 61 6e 64 20 74 68 65 20 65 72 72 6f 72 20 63 and the error c
11ed0 6f 64 65 20 73 61 66 65 6c 79 20 63 6c 65 61 72 ode safely clear
11ed1 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ed.. */. i
11ed2 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f f( pPager->errCo
11ed3 64 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 de ){. if(
11ed4 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
11ed5 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d . pPager-
11ed6 3e 65 72 72 43 6f 64 65 20 3d 20 53 51 4c 49 54 >errCode = SQLIT
11ed7 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 E_OK;. }.
11ed8 20 20 20 20 70 61 67 65 72 5f 72 65 73 65 74 28 pager_reset(
11ed9 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 0a pPager);. }..
11eda 20 20 20 20 70 50 61 67 65 72 2d 3e 63 68 61 6e pPager->chan
11edb 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 30 3b geCountDone = 0;
11edc 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 . pPager->sta
11edd 74 65 20 3d 20 50 41 47 45 52 5f 55 4e 4c 4f 43 te = PAGER_UNLOC
11ede 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 K;. }.}../*.**
11edf 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 This function sh
11ee0 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 77 ould be called w
11ee1 68 65 6e 20 61 6e 20 49 4f 45 52 52 2c 20 43 4f hen an IOERR, CO
11ee2 52 52 55 50 54 20 6f 72 20 46 55 4c 4c 20 65 72 RRUPT or FULL er
11ee3 72 6f 72 0a 2a 2a 20 6d 61 79 20 68 61 76 65 20 ror.** may have
11ee4 6f 63 63 75 72 72 65 64 2e 20 54 68 65 20 66 69 occurred. The fi
11ee5 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 rst argument is
11ee6 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
11ee7 20 70 61 67 65 72 20 0a 2a 2a 20 73 74 72 75 63 pager .** struc
11ee8 74 75 72 65 2c 20 74 68 65 20 73 65 63 6f 6e 64 ture, the second
11ee9 20 74 68 65 20 65 72 72 6f 72 2d 63 6f 64 65 20 the error-code
11eea 61 62 6f 75 74 20 74 6f 20 62 65 20 72 65 74 75 about to be retu
11eeb 72 6e 65 64 20 62 79 20 61 20 70 61 67 65 72 20 rned by a pager
11eec 0a 2a 2a 20 41 50 49 20 66 75 6e 63 74 69 6f 6e .** API function
11eed 2e 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 . The value retu
11eee 72 6e 65 64 20 69 73 20 61 20 63 6f 70 79 20 6f rned is a copy o
11eef 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 f the second arg
11ef0 75 6d 65 6e 74 20 0a 2a 2a 20 74 6f 20 74 68 69 ument .** to thi
11ef1 73 20 66 75 6e 63 74 69 6f 6e 2e 20 0a 2a 2a 0a s function. .**.
11ef2 2a 2a 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64 ** If the second
11ef3 20 61 72 67 75 6d 65 6e 74 20 69 73 20 53 51 4c argument is SQL
11ef4 49 54 45 5f 49 4f 45 52 52 2c 20 53 51 4c 49 54 ITE_IOERR, SQLIT
11ef5 45 5f 43 4f 52 52 55 50 54 2c 20 6f 72 20 53 51 E_CORRUPT, or SQ
11ef6 4c 49 54 45 5f 46 55 4c 4c 0a 2a 2a 20 74 68 65 LITE_FULL.** the
11ef7 20 65 72 72 6f 72 20 62 65 63 6f 6d 65 73 20 70 error becomes p
11ef8 65 72 73 69 73 74 65 6e 74 2e 20 55 6e 74 69 6c ersistent. Until
11ef9 20 74 68 65 20 70 65 72 73 69 73 74 65 6e 20 65 the persisten e
11efa 72 72 6f 72 20 69 73 20 63 6c 65 61 72 65 64 2c rror is cleared,
11efb 0a 2a 2a 20 73 75 62 73 65 71 75 65 6e 74 20 41 .** subsequent A
11efc 50 49 20 63 61 6c 6c 73 20 6f 6e 20 74 68 69 73 PI calls on this
11efd 20 50 61 67 65 72 20 77 69 6c 6c 20 69 6d 6d 65 Pager will imme
11efe 64 69 61 74 65 6c 79 20 72 65 74 75 72 6e 20 74 diately return t
11eff 68 65 20 73 61 6d 65 20 0a 2a 2a 20 65 72 72 6f he same .** erro
11f00 72 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 r code..**.** A
11f01 70 65 72 73 69 73 74 65 6e 74 20 65 72 72 6f 72 persistent error
11f02 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 indicates that
11f03 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
11f04 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 65 20 the pager-cache
11f05 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 74 72 .** cannot be tr
11f06 75 73 74 65 64 2e 20 54 68 69 73 20 73 74 61 74 usted. This stat
11f07 65 20 63 61 6e 20 62 65 20 63 6c 65 61 72 65 64 e can be cleared
11f08 20 62 79 20 63 6f 6d 70 6c 65 74 65 6c 79 20 64 by completely d
11f09 69 73 63 61 72 64 69 6e 67 20 0a 2a 2a 20 74 68 iscarding .** th
11f0a 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 e contents of th
11f0b 65 20 70 61 67 65 72 2d 63 61 63 68 65 2e 20 49 e pager-cache. I
11f0c 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 f a transaction
11f0d 77 61 73 20 61 63 74 69 76 65 20 77 68 65 6e 0a was active when.
11f0e 2a 2a 20 74 68 65 20 70 65 72 73 69 73 74 65 6e ** the persisten
11f0f 74 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 t error occurred
11f10 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 6c 6c 62 , then the rollb
11f11 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 6d 61 79 20 ack journal may
11f12 6e 65 65 64 0a 2a 2a 20 74 6f 20 62 65 20 72 65 need.** to be re
11f13 70 6c 61 79 65 64 20 74 6f 20 72 65 73 74 6f 72 played to restor
11f14 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f e the contents o
11f15 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 f the database f
11f16 69 6c 65 20 28 61 73 20 69 66 0a 2a 2a 20 69 74 ile (as if.** it
11f17 20 77 65 72 65 20 61 20 68 6f 74 2d 6a 6f 75 72 were a hot-jour
11f18 6e 61 6c 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 nal)..*/.static
11f19 69 6e 74 20 70 61 67 65 72 5f 65 72 72 6f 72 28 int pager_error(
11f1a 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 Pager *pPager, i
11f1b 6e 74 20 72 63 29 7b 0a 20 20 69 6e 74 20 72 63 nt rc){. int rc
11f1c 32 20 3d 20 72 63 20 26 20 30 78 66 66 3b 0a 20 2 = rc & 0xff;.
11f1d 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c assert( rc==SQL
11f1e 49 54 45 5f 4f 4b 20 7c 7c 20 21 4d 45 4d 44 42 ITE_OK || !MEMDB
11f1f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 0a 20 20 );. assert(.
11f20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72 pPager->err
11f21 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 46 55 4c Code==SQLITE_FUL
11f22 4c 20 7c 7c 0a 20 20 20 20 20 20 20 70 50 61 67 L ||. pPag
11f23 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c er->errCode==SQL
11f24 49 54 45 5f 4f 4b 20 7c 7c 0a 20 20 20 20 20 20 ITE_OK ||.
11f25 20 28 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 (pPager->errCod
11f26 65 20 26 20 30 78 66 66 29 3d 3d 53 51 4c 49 54 e & 0xff)==SQLIT
11f27 45 5f 49 4f 45 52 52 0a 20 20 29 3b 0a 20 20 69 E_IOERR. );. i
11f28 66 28 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f 46 f( rc2==SQLITE_F
11f29 55 4c 4c 20 7c 7c 20 72 63 32 3d 3d 53 51 4c 49 ULL || rc2==SQLI
11f2a 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20 20 20 TE_IOERR ){.
11f2b 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 pPager->errCode
11f2c 3d 20 72 63 3b 0a 20 20 7d 0a 20 20 72 65 74 75 = rc;. }. retu
11f2d 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
11f2e 45 78 65 63 75 74 65 20 61 20 72 6f 6c 6c 62 61 Execute a rollba
11f2f 63 6b 20 69 66 20 61 20 74 72 61 6e 73 61 63 74 ck if a transact
11f30 69 6f 6e 20 69 73 20 61 63 74 69 76 65 20 61 6e ion is active an
11f31 64 20 75 6e 6c 6f 63 6b 20 74 68 65 20 0a 2a 2a d unlock the .**
11f32 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
11f33 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 .**.** If the pa
11f34 67 65 72 20 68 61 73 20 61 6c 72 65 61 64 79 20 ger has already
11f35 65 6e 74 65 72 65 64 20 74 68 65 20 65 72 72 6f entered the erro
11f36 72 20 73 74 61 74 65 2c 20 64 6f 20 6e 6f 74 20 r state, do not
11f37 61 74 74 65 6d 70 74 20 0a 2a 2a 20 74 68 65 20 attempt .** the
11f38 72 6f 6c 6c 62 61 63 6b 20 61 74 20 74 68 69 73 rollback at this
11f39 20 74 69 6d 65 2e 20 49 6e 73 74 65 61 64 2c 20 time. Instead,
11f3a 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 69 pager_unlock() i
11f3b 73 20 63 61 6c 6c 65 64 2e 20 54 68 65 0a 2a 2a s called. The.**
11f3c 20 63 61 6c 6c 20 74 6f 20 70 61 67 65 72 5f 75 call to pager_u
11f3d 6e 6c 6f 63 6b 28 29 20 77 69 6c 6c 20 64 69 73 nlock() will dis
11f3e 63 61 72 64 20 61 6c 6c 20 69 6e 2d 6d 65 6d 6f card all in-memo
11f3f 72 79 20 70 61 67 65 73 2c 20 75 6e 6c 6f 63 6b ry pages, unlock
11f40 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 .** the database
11f41 20 66 69 6c 65 20 61 6e 64 20 63 6c 65 61 72 20 file and clear
11f42 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2e the error state.
11f43 20 49 66 20 74 68 69 73 20 6d 65 61 6e 73 20 74 If this means t
11f44 68 61 74 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 hat.** there is
11f45 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 6c 65 a hot-journal le
11f46 66 74 20 69 6e 20 74 68 65 20 66 69 6c 65 2d 73 ft in the file-s
11f47 79 73 74 65 6d 2c 20 74 68 65 20 6e 65 78 74 20 ystem, the next
11f48 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 74 6f connection.** to
11f49 20 6f 62 74 61 69 6e 20 61 20 73 68 61 72 65 64 obtain a shared
11f4a 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 70 61 67 lock on the pag
11f4b 65 72 20 28 77 68 69 63 68 20 6d 61 79 20 62 65 er (which may be
11f4c 20 74 68 69 73 20 6f 6e 65 29 20 77 69 6c 6c 0a this one) will.
11f4d 2a 2a 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b 2e ** roll it back.
11f4e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 .**.** If the pa
11f4f 67 65 72 20 68 61 73 20 6e 6f 74 20 61 6c 72 65 ger has not alre
11f50 61 64 79 20 65 6e 74 65 72 65 64 20 74 68 65 20 ady entered the
11f51 65 72 72 6f 72 20 73 74 61 74 65 2c 20 62 75 74 error state, but
11f52 20 61 6e 20 49 4f 20 6f 72 0a 2a 2a 20 6d 61 6c an IO or.** mal
11f53 6c 6f 63 20 65 72 72 6f 72 20 6f 63 63 75 72 73 loc error occurs
11f54 20 64 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61 during a rollba
11f55 63 6b 2c 20 74 68 65 6e 20 74 68 69 73 20 77 69 ck, then this wi
11f56 6c 6c 20 69 74 73 65 6c 66 20 63 61 75 73 65 20 ll itself cause
11f57 0a 2a 2a 20 74 68 65 20 70 61 67 65 72 20 74 6f .** the pager to
11f58 20 65 6e 74 65 72 20 74 68 65 20 65 72 72 6f 72 enter the error
11f59 20 73 74 61 74 65 2e 20 57 68 69 63 68 20 77 69 state. Which wi
11f5a 6c 6c 20 62 65 20 63 6c 65 61 72 65 64 20 62 79 ll be cleared by
11f5b 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 the.** call to
11f5c 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 2c 20 pager_unlock(),
11f5d 61 73 20 64 65 73 63 72 69 62 65 64 20 61 62 6f as described abo
11f5e 76 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ve..*/.static vo
11f5f 69 64 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 6e id pagerUnlockAn
11f60 64 52 6f 6c 6c 62 61 63 6b 28 50 61 67 65 72 20 dRollback(Pager
11f61 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28 20 *pPager){. if(
11f62 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d pPager->errCode=
11f63 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 =SQLITE_OK && pP
11f64 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 ager->state>=PAG
11f65 45 52 5f 52 45 53 45 52 56 45 44 20 29 7b 0a 20 ER_RESERVED ){.
11f66 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 sqlite3BeginB
11f67 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 enignMalloc();.
11f68 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 sqlite3PagerR
11f69 6f 6c 6c 62 61 63 6b 28 70 50 61 67 65 72 29 3b ollback(pPager);
11f6a 0a 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64 42 . sqlite3EndB
11f6b 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 enignMalloc();.
11f6c 20 7d 0a 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63 }. pager_unloc
11f6d 6b 28 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a k(pPager);.}../*
11f6e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
11f6f 20 65 6e 64 73 20 61 20 74 72 61 6e 73 61 63 74 ends a transact
11f70 69 6f 6e 2e 20 41 20 74 72 61 6e 73 61 63 74 69 ion. A transacti
11f71 6f 6e 20 69 73 20 75 73 75 61 6c 6c 79 20 65 6e on is usually en
11f72 64 65 64 20 62 79 20 0a 2a 2a 20 65 69 74 68 65 ded by .** eithe
11f73 72 20 61 20 43 4f 4d 4d 49 54 20 6f 72 20 61 20 r a COMMIT or a
11f74 52 4f 4c 4c 42 41 43 4b 20 6f 70 65 72 61 74 69 ROLLBACK operati
11f75 6f 6e 2e 20 54 68 69 73 20 72 6f 75 74 69 6e 65 on. This routine
11f76 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 20 0a may be called .
11f77 2a 2a 20 61 66 74 65 72 20 72 6f 6c 6c 62 61 63 ** after rollbac
11f78 6b 20 6f 66 20 61 20 68 6f 74 2d 6a 6f 75 72 6e k of a hot-journ
11f79 61 6c 2c 20 6f 72 20 69 66 20 61 6e 20 65 72 72 al, or if an err
11f7a 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 or occurs while
11f7b 6f 70 65 6e 69 6e 67 0a 2a 2a 20 74 68 65 20 6a opening.** the j
11f7c 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f 72 20 77 ournal file or w
11f7d 72 69 74 69 6e 67 20 74 68 65 20 76 65 72 79 20 riting the very
11f7e 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c 2d 68 65 first journal-he
11f7f 61 64 65 72 20 6f 66 20 61 0a 2a 2a 20 64 61 74 ader of a.** dat
11f80 61 62 61 73 65 20 74 72 61 6e 73 61 63 74 69 6f abase transactio
11f81 6e 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 74 68 65 n..** .** If the
11f82 20 70 61 67 65 72 20 69 73 20 69 6e 20 50 41 47 pager is in PAG
11f83 45 52 5f 53 48 41 52 45 44 20 6f 72 20 50 41 47 ER_SHARED or PAG
11f84 45 52 5f 55 4e 4c 4f 43 4b 20 73 74 61 74 65 20 ER_UNLOCK state
11f85 77 68 65 6e 20 74 68 69 73 0a 2a 2a 20 72 6f 75 when this.** rou
11f86 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 tine is called,
11f87 69 74 20 69 73 20 61 20 6e 6f 2d 6f 70 20 28 72 it is a no-op (r
11f88 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b eturns SQLITE_OK
11f89 29 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 )..**.** Otherwi
11f8a 73 65 2c 20 61 6e 79 20 61 63 74 69 76 65 20 73 se, any active s
11f8b 61 76 65 70 6f 69 6e 74 73 20 61 72 65 20 72 65 avepoints are re
11f8c 6c 65 61 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 leased..**.** If
11f8d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
11f8e 65 20 69 73 20 6f 70 65 6e 2c 20 74 68 65 6e 20 e is open, then
11f8f 69 74 20 69 73 20 22 66 69 6e 61 6c 69 7a 65 64 it is "finalized
11f90 22 2e 20 4f 6e 63 65 20 61 20 6a 6f 75 72 6e 61 ". Once a journa
11f91 6c 20 0a 2a 2a 20 66 69 6c 65 20 68 61 73 20 62 l .** file has b
11f92 65 65 6e 20 66 69 6e 61 6c 69 7a 65 64 20 69 74 een finalized it
11f93 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 is not possible
11f94 20 74 6f 20 75 73 65 20 69 74 20 74 6f 20 72 6f to use it to ro
11f95 6c 6c 20 62 61 63 6b 20 61 20 0a 2a 2a 20 74 72 ll back a .** tr
11f96 61 6e 73 61 63 74 69 6f 6e 2e 20 4e 6f 72 20 77 ansaction. Nor w
11f97 69 6c 6c 20 69 74 20 62 65 20 63 6f 6e 73 69 64 ill it be consid
11f98 65 72 65 64 20 74 6f 20 62 65 20 61 20 68 6f 74 ered to be a hot
11f99 2d 6a 6f 75 72 6e 61 6c 20 62 79 20 74 68 69 73 -journal by this
11f9a 0a 2a 2a 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 .** or any other
11f9b 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
11f9c 74 69 6f 6e 2e 20 45 78 61 63 74 6c 79 20 68 6f tion. Exactly ho
11f9d 77 20 61 20 6a 6f 75 72 6e 61 6c 20 69 73 20 66 w a journal is f
11f9e 69 6e 61 6c 69 7a 65 64 0a 2a 2a 20 64 65 70 65 inalized.** depe
11f9f 6e 64 73 20 6f 6e 20 77 68 65 74 68 65 72 20 6f nds on whether o
11fa0 72 20 6e 6f 74 20 74 68 65 20 70 61 67 65 72 20 r not the pager
11fa1 69 73 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 is running in ex
11fa2 63 6c 75 73 69 76 65 20 6d 6f 64 65 20 61 6e 64 clusive mode and
11fa3 0a 2a 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 .** the current
11fa4 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 28 50 61 journal-mode (Pa
11fa5 67 65 72 2e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 ger.journalMode
11fa6 76 61 6c 75 65 29 2c 20 61 73 20 66 6f 6c 6c 6f value), as follo
11fa7 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f 75 72 ws:.**.** jour
11fa8 6e 61 6c 4d 6f 64 65 3d 3d 4d 45 4d 4f 52 59 0a nalMode==MEMORY.
11fa9 2a 2a 20 20 20 20 20 4a 6f 75 72 6e 61 6c 20 66 ** Journal f
11faa 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 ile descriptor i
11fab 73 20 73 69 6d 70 6c 79 20 63 6c 6f 73 65 64 2e s simply closed.
11fac 20 54 68 69 73 20 64 65 73 74 72 6f 79 73 20 61 This destroys a
11fad 6e 20 0a 2a 2a 20 20 20 20 20 69 6e 2d 6d 65 6d n .** in-mem
11fae 6f 72 79 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a ory journal..**.
11faf 2a 2a 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 ** journalMode
11fb0 3d 3d 54 52 55 4e 43 41 54 45 0a 2a 2a 20 20 20 ==TRUNCATE.**
11fb1 20 20 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 Journal file i
11fb2 73 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20 7a s truncated to z
11fb3 65 72 6f 20 62 79 74 65 73 20 69 6e 20 73 69 7a ero bytes in siz
11fb4 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f 75 72 6e e..**.** journ
11fb5 61 6c 4d 6f 64 65 3d 3d 50 45 52 53 49 53 54 0a alMode==PERSIST.
11fb6 2a 2a 20 20 20 20 20 54 68 65 20 66 69 72 73 74 ** The first
11fb7 20 32 38 20 62 79 74 65 73 20 6f 66 20 74 68 65 28 bytes of the
11fb8 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 72 journal file ar
11fb9 65 20 7a 65 72 6f 65 64 2e 20 54 68 69 73 20 69 e zeroed. This i
11fba 6e 76 61 6c 69 64 61 74 65 73 0a 2a 2a 20 20 20 nvalidates.**
11fbb 20 20 74 68 65 20 66 69 72 73 74 20 6a 6f 75 72 the first jour
11fbc 6e 61 6c 20 68 65 61 64 65 72 20 69 6e 20 74 68 nal header in th
11fbd 65 20 66 69 6c 65 2c 20 61 6e 64 20 68 65 6e 63 e file, and henc
11fbe 65 20 74 68 65 20 65 6e 74 69 72 65 20 6a 6f 75 e the entire jou
11fbf 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 66 69 6c 65 rnal.** file
11fc0 2e 20 41 6e 20 69 6e 76 61 6c 69 64 20 6a 6f 75 . An invalid jou
11fc1 72 6e 61 6c 20 66 69 6c 65 20 63 61 6e 6e 6f 74 rnal file cannot
11fc2 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e be rolled back.
11fc3 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f 75 72 6e 61 6c .**.** journal
11fc4 4d 6f 64 65 3d 3d 44 45 4c 45 54 45 0a 2a 2a 20 Mode==DELETE.**
11fc5 20 20 20 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 The journal
11fc6 66 69 6c 65 20 69 73 20 63 6c 6f 73 65 64 20 61 file is closed a
11fc7 6e 64 20 64 65 6c 65 74 65 64 20 75 73 69 6e 67 nd deleted using
11fc8 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 sqlite3OsDelete
11fc9 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 49 66 ()..**.** If
11fca 20 74 68 65 20 70 61 67 65 72 20 69 73 20 72 75 the pager is ru
11fcb 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 69 nning in exclusi
11fcc 76 65 20 6d 6f 64 65 2c 20 74 68 69 73 20 6d 65 ve mode, this me
11fcd 74 68 6f 64 20 6f 66 20 66 69 6e 61 6c 69 7a 69 thod of finalizi
11fce 6e 67 0a 2a 2a 20 20 20 20 20 74 68 65 20 6a 6f ng.** the jo
11fcf 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e 65 urnal file is ne
11fd0 76 65 72 20 75 73 65 64 2e 20 49 6e 73 74 65 61 ver used. Instea
11fd1 64 2c 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 d, if the journa
11fd2 6c 4d 6f 64 65 20 69 73 0a 2a 2a 20 20 20 20 20 lMode is.**
11fd3 44 45 4c 45 54 45 20 61 6e 64 20 74 68 65 20 70 DELETE and the p
11fd4 61 67 65 72 20 69 73 20 69 6e 20 65 78 63 6c 75 ager is in exclu
11fd5 73 69 76 65 20 6d 6f 64 65 2c 20 74 68 65 20 6d sive mode, the m
11fd6 65 74 68 6f 64 20 64 65 73 63 72 69 62 65 64 20 ethod described
11fd7 75 6e 64 65 72 0a 2a 2a 20 20 20 20 20 6a 6f 75 under.** jou
11fd8 72 6e 61 6c 4d 6f 64 65 3d 3d 50 45 52 53 49 53 rnalMode==PERSIS
11fd9 54 20 69 73 20 75 73 65 64 20 69 6e 73 74 65 61 T is used instea
11fda 64 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 74 d..**.** After t
11fdb 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 66 69 he journal is fi
11fdc 6e 61 6c 69 7a 65 64 2c 20 69 66 20 72 75 6e 6e nalized, if runn
11fdd 69 6e 67 20 69 6e 20 6e 6f 6e 2d 65 78 63 6c 75 ing in non-exclu
11fde 73 69 76 65 20 6d 6f 64 65 2c 20 74 68 65 0a 2a sive mode, the.*
11fdf 2a 20 70 61 67 65 72 20 6d 6f 76 65 73 20 74 6f * pager moves to
11fe0 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 73 74 PAGER_SHARED st
11fe1 61 74 65 20 28 61 6e 64 20 64 6f 77 6e 67 72 61 ate (and downgra
11fe2 64 65 73 20 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 des the lock on
11fe3 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 the.** database
11fe4 66 69 6c 65 20 61 63 63 6f 72 64 69 6e 67 6c 79 file accordingly
11fe5 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 )..**.** If the
11fe6 70 61 67 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 pager is running
11fe7 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f in exclusive mo
11fe8 64 65 20 61 6e 64 20 69 73 20 69 6e 20 50 41 47 de and is in PAG
11fe9 45 52 5f 53 59 4e 43 45 44 20 73 74 61 74 65 2c ER_SYNCED state,
11fea 0a 2a 2a 20 69 74 20 6d 6f 76 65 73 20 74 6f 20 .** it moves to
11feb 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 2e PAGER_EXCLUSIVE.
11fec 20 4e 6f 20 6c 6f 63 6b 73 20 61 72 65 20 64 6f No locks are do
11fed 77 6e 67 72 61 64 65 64 20 77 68 65 6e 20 72 75 wngraded when ru
11fee 6e 6e 69 6e 67 20 69 6e 0a 2a 2a 20 65 78 63 6c nning in.** excl
11fef 75 73 69 76 65 20 6d 6f 64 65 2e 0a 2a 2a 0a 2a usive mode..**.*
11ff0 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 * SQLITE_OK is r
11ff1 65 74 75 72 6e 65 64 20 69 66 20 6e 6f 20 65 72 eturned if no er
11ff2 72 6f 72 20 6f 63 63 75 72 73 2e 20 49 66 20 61 ror occurs. If a
11ff3 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 n error occurs d
11ff4 75 72 69 6e 67 0a 2a 2a 20 61 6e 79 20 6f 66 20 uring.** any of
11ff5 74 68 65 20 49 4f 20 6f 70 65 72 61 74 69 6f 6e the IO operation
11ff6 73 20 74 6f 20 66 69 6e 61 6c 69 7a 65 20 74 68 s to finalize th
11ff7 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f e journal file o
11ff8 72 20 75 6e 6c 6f 63 6b 20 74 68 65 0a 2a 2a 20 r unlock the.**
11ff9 64 61 74 61 62 61 73 65 20 74 68 65 6e 20 74 68 database then th
11ffa 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 e IO error code
11ffb 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 is returned to t
11ffc 68 65 20 75 73 65 72 2e 20 49 66 20 74 68 65 20 he user. If the
11ffd 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f .** operation to
11ffe 20 66 69 6e 61 6c 69 7a 65 20 74 68 65 20 6a 6f finalize the jo
11fff 75 72 6e 61 6c 20 66 69 6c 65 20 66 61 69 6c 73 urnal file fails
12000 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 64 65 20 , then the code
12001 73 74 69 6c 6c 0a 2a 2a 20 74 72 69 65 73 20 74 still.** tries t
12002 6f 20 75 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74 o unlock the dat
12003 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 6e 6f abase file if no
12004 74 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d t in exclusive m
12005 6f 64 65 2e 20 49 66 20 74 68 65 0a 2a 2a 20 75 ode. If the.** u
12006 6e 6c 6f 63 6b 20 6f 70 65 72 61 74 69 6f 6e 20 nlock operation
12007 66 61 69 6c 73 20 61 73 20 77 65 6c 6c 2c 20 74 fails as well, t
12008 68 65 6e 20 74 68 65 20 66 69 72 73 74 20 65 72 hen the first er
12009 72 6f 72 20 63 6f 64 65 20 72 65 6c 61 74 65 64 ror code related
1200a 0a 2a 2a 20 74 6f 20 74 68 65 20 66 69 72 73 74 .** to the first
1200b 20 65 72 72 6f 72 20 65 6e 63 6f 75 6e 74 65 72 error encounter
1200c 65 64 20 28 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ed (the journal
1200d 66 69 6e 61 6c 69 7a 61 74 69 6f 6e 20 6f 6e 65 finalization one
1200e 29 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 ) is.** returned
1200f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
12010 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 pager_end_transa
12011 63 74 69 6f 6e 28 50 61 67 65 72 20 2a 70 50 61 ction(Pager *pPa
12012 67 65 72 2c 20 69 6e 74 20 68 61 73 4d 61 73 74 ger, int hasMast
12013 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 er){. int rc =
12014 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 SQLITE_OK;
12015 2f 2a 20 45 72 72 6f 72 20 63 6f 64 65 20 66 72 /* Error code fr
12016 6f 6d 20 6a 6f 75 72 6e 61 6c 20 66 69 6e 61 6c om journal final
12017 69 7a 61 74 69 6f 6e 20 6f 70 65 72 61 74 69 6f ization operatio
12018 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 32 20 3d n */. int rc2 =
12019 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 SQLITE_OK;
1201a 2f 2a 20 45 72 72 6f 72 20 63 6f 64 65 20 66 72 /* Error code fr
1201b 6f 6d 20 64 62 20 66 69 6c 65 20 75 6e 6c 6f 63 om db file unloc
1201c 6b 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 0a k operation */..
1201d 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 if( pPager->st
1201e 61 74 65 3c 50 41 47 45 52 5f 52 45 53 45 52 56 ate<PAGER_RESERV
1201f 45 44 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e ED ){. return
12020 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
12021 20 20 72 65 6c 65 61 73 65 41 6c 6c 53 61 76 65 releaseAllSave
12022 70 6f 69 6e 74 73 28 70 50 61 67 65 72 29 3b 0a points(pPager);.
12023 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 . assert( isOpe
12024 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 7c n(pPager->jfd) |
12025 7c 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 | pPager->pInJou
12026 72 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 69 66 28 rnal==0 );. if(
12027 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e isOpen(pPager->
12028 6a 66 64 29 20 29 7b 0a 0a 20 20 20 20 2f 2a 20 jfd) ){.. /*
12029 46 69 6e 61 6c 69 7a 65 20 74 68 65 20 6a 6f 75 Finalize the jou
1202a 72 6e 61 6c 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 rnal file. */.
1202b 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 4d if( sqlite3IsM
1202c 65 6d 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 emJournal(pPager
1202d 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20 ->jfd) ){.
1202e 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
1202f 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 journalMode==PAG
12030 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d ER_JOURNALMODE_M
12031 45 4d 4f 52 59 20 29 3b 0a 20 20 20 20 20 20 73 EMORY );. s
12032 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 qlite3OsClose(pP
12033 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 ager->jfd);.
12034 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67 65 72 }else if( pPager
12035 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 ->journalMode==P
12036 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
12037 5f 54 52 55 4e 43 41 54 45 20 29 7b 0a 20 20 20 _TRUNCATE ){.
12038 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a if( pPager->j
12039 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 20 29 7b 0a ournalOff==0 ){.
1203a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
1203b 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 65 ITE_OK;. }e
1203c 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 lse{. rc
1203d 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 = sqlite3OsTrunc
1203e 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c ate(pPager->jfd,
1203f 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 0);. }.
12040 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e pPager->journ
12041 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 20 alOff = 0;.
12042 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
12043 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20 20 20 Started = 0;.
12044 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67 65 }else if( pPage
12045 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 r->exclusiveMode
12046 20 0a 20 20 20 20 20 7c 7c 20 70 50 61 67 65 72 . || pPager
12047 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 ->journalMode==P
12048 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
12049 5f 50 45 52 53 49 53 54 0a 20 20 20 20 29 7b 0a _PERSIST. ){.
1204a 20 20 20 20 20 20 72 63 20 3d 20 7a 65 72 6f 4a rc = zeroJ
1204b 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72 ournalHdr(pPager
1204c 2c 20 68 61 73 4d 61 73 74 65 72 29 3b 0a 20 20 , hasMaster);.
1204d 20 20 20 20 70 61 67 65 72 5f 65 72 72 6f 72 28 pager_error(
1204e 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20 pPager, rc);.
1204f 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e pPager->journ
12050 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 20 alOff = 0;.
12051 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
12052 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20 20 20 Started = 0;.
12053 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a }else{. /*
12054 20 54 68 69 73 20 62 72 61 6e 63 68 20 6d 61 79 This branch may
12055 20 62 65 20 65 78 65 63 75 74 65 64 20 77 69 74 be executed wit
12056 68 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4d h Pager.journalM
12057 6f 64 65 3d 3d 4d 45 4d 4f 52 59 20 69 66 0a 20 ode==MEMORY if.
12058 20 20 20 20 20 2a 2a 20 61 20 68 6f 74 2d 6a 6f ** a hot-jo
12059 75 72 6e 61 6c 20 77 61 73 20 6a 75 73 74 20 72 urnal was just r
1205a 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 49 6e 20 74 olled back. In t
1205b 68 69 73 20 63 61 73 65 20 74 68 65 20 6a 6f 75 his case the jou
1205c 72 6e 61 6c 0a 20 20 20 20 20 20 2a 2a 20 66 69 rnal. ** fi
1205d 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 63 6c 6f le should be clo
1205e 73 65 64 20 61 6e 64 20 64 65 6c 65 74 65 64 2e sed and deleted.
1205f 20 49 66 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 If this connect
12060 69 6f 6e 20 77 72 69 74 65 73 20 74 6f 0a 20 20 ion writes to.
12061 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 ** the datab
12062 61 73 65 20 66 69 6c 65 2c 20 69 74 20 77 69 6c ase file, it wil
12063 6c 20 64 6f 20 73 6f 20 75 73 69 6e 67 20 61 6e l do so using an
12064 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e in-memory journ
12065 61 6c 2e 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 al. */. as
12066 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f sert( pPager->jo
12067 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 urnalMode==PAGER
12068 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c _JOURNALMODE_DEL
12069 45 54 45 20 0a 20 20 20 20 20 20 20 20 20 20 20 ETE .
1206a 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e || pPager->journ
1206b 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f alMode==PAGER_JO
1206c 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 URNALMODE_MEMORY
1206d 20 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 . );.
1206e 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 sqlite3OsClose(
1206f 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 pPager->jfd);.
12070 20 20 20 20 69 66 28 20 21 70 50 61 67 65 72 2d if( !pPager-
12071 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20 >tempFile ){.
12072 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
12073 33 4f 73 44 65 6c 65 74 65 28 70 50 61 67 65 72 3OsDelete(pPager
12074 2d 3e 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e ->pVfs, pPager->
12075 7a 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 zJournal, 0);.
12076 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 23 69 66 }. }..#if
12077 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b def SQLITE_CHECK
12078 5f 50 41 47 45 53 0a 20 20 20 20 73 71 6c 69 74 _PAGES. sqlit
12079 65 33 50 63 61 63 68 65 49 74 65 72 61 74 65 44 e3PcacheIterateD
1207a 69 72 74 79 28 70 50 61 67 65 72 2d 3e 70 50 43 irty(pPager->pPC
1207b 61 63 68 65 2c 20 70 61 67 65 72 5f 73 65 74 5f ache, pager_set_
1207c 70 61 67 65 68 61 73 68 29 3b 0a 23 65 6e 64 69 pagehash);.#endi
1207d 66 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 f.. sqlite3Pc
1207e 61 63 68 65 43 6c 65 61 6e 41 6c 6c 28 70 50 61 acheCleanAll(pPa
1207f 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 ger->pPCache);.
12080 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 sqlite3Bitvec
12081 44 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e Destroy(pPager->
12082 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 pInJournal);.
12083 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 pPager->pInJour
12084 6e 61 6c 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 nal = 0;. pPa
12085 67 65 72 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a 20 ger->nRec = 0;.
12086 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65 }.. if( !pPage
12087 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 r->exclusiveMode
12088 20 29 7b 0a 20 20 20 20 72 63 32 20 3d 20 6f 73 ){. rc2 = os
12089 55 6e 6c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 Unlock(pPager->f
1208a 64 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b d, SHARED_LOCK);
1208b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 . pPager->sta
1208c 74 65 20 3d 20 50 41 47 45 52 5f 53 48 41 52 45 te = PAGER_SHARE
1208d 44 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 63 D;. pPager->c
1208e 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d hangeCountDone =
1208f 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 0;. }else if(
12090 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 pPager->state==P
12091 41 47 45 52 5f 53 59 4e 43 45 44 20 29 7b 0a 20 AGER_SYNCED ){.
12092 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 pPager->state
12093 20 3d 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 = PAGER_EXCLUSI
12094 56 45 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 VE;. }. pPager
12095 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d 20 30 3b ->setMaster = 0;
12096 0a 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 . pPager->needS
12097 79 6e 63 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 ync = 0;. pPage
12098 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 3d 20 r->dbModified =
12099 30 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 3a 20 49 0;.. /* TODO: I
1209a 73 20 74 68 69 73 20 6f 70 74 69 6d 61 6c 3f 20 s this optimal?
1209b 57 68 79 20 69 73 20 74 68 65 20 64 62 20 73 69 Why is the db si
1209c 7a 65 20 69 6e 76 61 6c 69 64 61 74 65 64 20 68 ze invalidated h
1209d 65 72 65 20 0a 20 20 2a 2a 20 77 68 65 6e 20 74 ere . ** when t
1209e 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
1209f 20 69 73 20 6e 6f 74 20 75 6e 6c 6f 63 6b 65 64 is not unlocked
120a0 3f 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 64 ? */. pPager->d
120a1 62 4f 72 69 67 53 69 7a 65 20 3d 20 30 3b 0a 20 bOrigSize = 0;.
120a2 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 54 72 sqlite3PcacheTr
120a3 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e 70 uncate(pPager->p
120a4 50 43 61 63 68 65 2c 20 70 50 61 67 65 72 2d 3e PCache, pPager->
120a5 64 62 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 21 dbSize);. if( !
120a6 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 70 50 61 MEMDB ){. pPa
120a7 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 ger->dbSizeValid
120a8 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 = 0;. }.. ret
120a9 75 72 6e 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f urn (rc==SQLITE_
120aa 4f 4b 3f 72 63 32 3a 72 63 29 3b 0a 7d 0a 0a 2f OK?rc2:rc);.}../
120ab 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 61 *.** Parameter a
120ac 44 61 74 61 20 6d 75 73 74 20 70 6f 69 6e 74 20 Data must point
120ad 74 6f 20 61 20 62 75 66 66 65 72 20 6f 66 20 70 to a buffer of p
120ae 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 Pager->pageSize
120af 62 79 74 65 73 0a 2a 2a 20 6f 66 20 64 61 74 61 bytes.** of data
120b0 2e 20 43 6f 6d 70 75 74 65 20 61 6e 64 20 72 65 . Compute and re
120b1 74 75 72 6e 20 61 20 63 68 65 63 6b 73 75 6d 20 turn a checksum
120b2 62 61 73 65 64 20 6f 6e 74 20 74 68 65 20 63 6f based ont the co
120b3 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 0a 2a ntents of the .*
120b4 2a 20 70 61 67 65 20 6f 66 20 64 61 74 61 20 61 * page of data a
120b5 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 nd the current v
120b6 61 6c 75 65 20 6f 66 20 70 50 61 67 65 72 2d 3e alue of pPager->
120b7 63 6b 73 75 6d 49 6e 69 74 2e 0a 2a 2a 0a 2a 2a cksumInit..**.**
120b8 20 54 68 69 73 20 69 73 20 6e 6f 74 20 61 20 72 This is not a r
120b9 65 61 6c 20 63 68 65 63 6b 73 75 6d 2e 20 49 74 eal checksum. It
120ba 20 69 73 20 72 65 61 6c 6c 79 20 6a 75 73 74 20 is really just
120bb 74 68 65 20 73 75 6d 20 6f 66 20 74 68 65 20 0a the sum of the .
120bc 2a 2a 20 72 61 6e 64 6f 6d 20 69 6e 69 74 69 61 ** random initia
120bd 6c 20 76 61 6c 75 65 20 28 70 50 61 67 65 72 2d l value (pPager-
120be 3e 63 6b 73 75 6d 49 6e 69 74 29 20 61 6e 64 20 >cksumInit) and
120bf 65 76 65 72 79 20 32 30 30 74 68 20 62 79 74 65 every 200th byte
120c0 0a 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 20 .** of the page
120c1 64 61 74 61 2c 20 73 74 61 72 74 69 6e 67 20 77 data, starting w
120c2 69 74 68 20 62 79 74 65 20 6f 66 66 73 65 74 20 ith byte offset
120c3 28 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a (pPager->pageSiz
120c4 65 25 32 30 30 29 2e 0a 2a 2a 20 45 61 63 68 20 e%200)..** Each
120c5 62 79 74 65 20 69 73 20 69 6e 74 65 72 70 72 65 byte is interpre
120c6 74 65 64 20 61 73 20 61 6e 20 38 2d 62 69 74 20 ted as an 8-bit
120c7 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 unsigned integer
120c8 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 ..**.** Changing
120c9 20 74 68 65 20 66 6f 72 6d 75 6c 61 20 75 73 65 the formula use
120ca 64 20 74 6f 20 63 6f 6d 70 75 74 65 20 74 68 69 d to compute thi
120cb 73 20 63 68 65 63 6b 73 75 6d 20 72 65 73 75 6c s checksum resul
120cc 74 73 20 69 6e 20 61 6e 0a 2a 2a 20 69 6e 63 6f ts in an.** inco
120cd 6d 70 61 74 69 62 6c 65 20 6a 6f 75 72 6e 61 6c mpatible journal
120ce 20 66 69 6c 65 20 66 6f 72 6d 61 74 2e 0a 2a 2a file format..**
120cf 0a 2a 2a 20 49 66 20 6a 6f 75 72 6e 61 6c 20 63 .** If journal c
120d0 6f 72 72 75 70 74 69 6f 6e 20 6f 63 63 75 72 73 orruption occurs
120d1 20 64 75 65 20 74 6f 20 61 20 70 6f 77 65 72 20 due to a power
120d2 66 61 69 6c 75 72 65 2c 20 74 68 65 20 6d 6f 73 failure, the mos
120d3 74 20 6c 69 6b 65 6c 79 20 0a 2a 2a 20 73 63 65 t likely .** sce
120d4 6e 61 72 69 6f 20 69 73 20 74 68 61 74 20 6f 6e nario is that on
120d5 65 20 65 6e 64 20 6f 72 20 74 68 65 20 6f 74 68 e end or the oth
120d6 65 72 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 er of the record
120d7 20 77 69 6c 6c 20 62 65 20 63 68 61 6e 67 65 64 will be changed
120d8 2e 20 0a 2a 2a 20 49 74 20 69 73 20 6d 75 63 68 . .** It is much
120d9 20 6c 65 73 73 20 6c 69 6b 65 6c 79 20 74 68 61 less likely tha
120da 74 20 74 68 65 20 74 77 6f 20 65 6e 64 73 20 6f t the two ends o
120db 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 72 65 f the journal re
120dc 63 6f 72 64 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 cord will be.**
120dd 63 6f 72 72 65 63 74 20 61 6e 64 20 74 68 65 20 correct and the
120de 6d 69 64 64 6c 65 20 62 65 20 63 6f 72 72 75 70 middle be corrup
120df 74 2e 20 20 54 68 75 73 2c 20 74 68 69 73 20 22 t. Thus, this "
120e0 63 68 65 63 6b 73 75 6d 22 20 73 63 68 65 6d 65 checksum" scheme
120e1 2c 0a 2a 2a 20 74 68 6f 75 67 68 20 66 61 73 74 ,.** though fast
120e2 20 61 6e 64 20 73 69 6d 70 6c 65 2c 20 63 61 74 and simple, cat
120e3 63 68 65 73 20 74 68 65 20 6d 6f 73 74 6c 79 20 ches the mostly
120e4 6c 69 6b 65 6c 79 20 6b 69 6e 64 20 6f 66 20 63 likely kind of c
120e5 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 orruption..*/.st
120e6 61 74 69 63 20 75 33 32 20 70 61 67 65 72 5f 63 atic u32 pager_c
120e7 6b 73 75 6d 28 50 61 67 65 72 20 2a 70 50 61 67 ksum(Pager *pPag
120e8 65 72 2c 20 63 6f 6e 73 74 20 75 38 20 2a 61 44 er, const u8 *aD
120e9 61 74 61 29 7b 0a 20 20 75 33 32 20 63 6b 73 75 ata){. u32 cksu
120ea 6d 20 3d 20 70 50 61 67 65 72 2d 3e 63 6b 73 75 m = pPager->cksu
120eb 6d 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 2f mInit; /
120ec 2a 20 43 68 65 63 6b 73 75 6d 20 76 61 6c 75 65 * Checksum value
120ed 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 to return */.
120ee 69 6e 74 20 69 20 3d 20 70 50 61 67 65 72 2d 3e int i = pPager->
120ef 70 61 67 65 53 69 7a 65 2d 32 30 30 3b 20 20 20 pageSize-200;
120f0 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 /* Loop c
120f1 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 77 68 69 6c ounter */. whil
120f2 65 28 20 69 3e 30 20 29 7b 0a 20 20 20 20 63 6b e( i>0 ){. ck
120f3 73 75 6d 20 2b 3d 20 61 44 61 74 61 5b 69 5d 3b sum += aData[i];
120f4 0a 20 20 20 20 69 20 2d 3d 20 32 30 30 3b 0a 20 . i -= 200;.
120f5 20 7d 0a 20 20 72 65 74 75 72 6e 20 63 6b 73 75 }. return cksu
120f6 6d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 m;.}../*.** Read
120f7 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 66 a single page f
120f8 72 6f 6d 20 65 69 74 68 65 72 20 74 68 65 20 6a rom either the j
120f9 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 69 66 20 ournal file (if
120fa 69 73 4d 61 69 6e 4a 72 6e 6c 3d 3d 31 29 20 6f isMainJrnl==1) o
120fb 72 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 73 75 r.** from the su
120fc 62 2d 6a 6f 75 72 6e 61 6c 20 28 69 66 20 69 73 b-journal (if is
120fd 4d 61 69 6e 4a 72 6e 6c 3d 3d 30 29 20 61 6e 64 MainJrnl==0) and
120fe 20 70 6c 61 79 62 61 63 6b 20 74 68 61 74 20 70 playback that p
120ff 61 67 65 2e 0a 2a 2a 20 54 68 65 20 70 61 67 65 age..** The page
12100 20 62 65 67 69 6e 73 20 61 74 20 6f 66 66 73 65 begins at offse
12101 74 20 2a 70 4f 66 66 73 65 74 20 69 6e 74 6f 20 t *pOffset into
12102 74 68 65 20 66 69 6c 65 2e 20 54 68 65 20 2a 70 the file. The *p
12103 4f 66 66 73 65 74 0a 2a 2a 20 76 61 6c 75 65 20 Offset.** value
12104 69 73 20 69 6e 63 72 65 61 73 65 64 20 74 6f 20 is increased to
12105 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 the start of the
12106 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20 74 68 next page in th
12107 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a e journal..**.**
12108 20 54 68 65 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 The isMainJrnl
12109 66 6c 61 67 20 69 73 20 74 72 75 65 20 69 66 20 flag is true if
1210a 74 68 69 73 20 69 73 20 74 68 65 20 6d 61 69 6e this is the main
1210b 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 rollback journa
1210c 6c 20 61 6e 64 0a 2a 2a 20 66 61 6c 73 65 20 66 l and.** false f
1210d 6f 72 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 or the statement
1210e 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 20 6d journal. The m
1210f 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 ain rollback jou
12110 72 6e 61 6c 20 75 73 65 73 0a 2a 2a 20 63 68 65 rnal uses.** che
12111 63 6b 73 75 6d 73 20 2d 20 74 68 65 20 73 74 61 cksums - the sta
12112 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 64 tement journal d
12113 6f 65 73 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 49 oes not..**.** I
12114 66 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 f the page numbe
12115 72 20 6f 66 20 74 68 65 20 70 61 67 65 20 72 65 r of the page re
12116 63 6f 72 64 20 72 65 61 64 20 66 72 6f 6d 20 74 cord read from t
12117 68 65 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61 6c he (sub-)journal
12118 20 66 69 6c 65 0a 2a 2a 20 69 73 20 67 72 65 61 file.** is grea
12119 74 65 72 20 74 68 61 6e 20 74 68 65 20 63 75 72 ter than the cur
1211a 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 50 61 rent value of Pa
1211b 67 65 72 2e 64 62 53 69 7a 65 2c 20 74 68 65 6e ger.dbSize, then
1211c 20 70 6c 61 79 62 61 63 6b 20 69 73 0a 2a 2a 20 playback is.**
1211d 73 6b 69 70 70 65 64 20 61 6e 64 20 53 51 4c 49 skipped and SQLI
1211e 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
1211f 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 44 6f 6e d..**.** If pDon
12120 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 e is not NULL, t
12121 68 65 6e 20 69 74 20 69 73 20 61 20 72 65 63 6f hen it is a reco
12122 72 64 20 6f 66 20 70 61 67 65 73 20 74 68 61 74 rd of pages that
12123 20 68 61 76 65 20 61 6c 72 65 61 64 79 0a 2a 2a have already.**
12124 20 62 65 65 6e 20 70 6c 61 79 65 64 20 62 61 63 been played bac
12125 6b 2e 20 20 49 66 20 74 68 65 20 70 61 67 65 20 k. If the page
12126 61 74 20 2a 70 4f 66 66 73 65 74 20 68 61 73 20 at *pOffset has
12127 61 6c 72 65 61 64 79 20 62 65 65 6e 20 70 6c 61 already been pla
12128 79 65 64 20 62 61 63 6b 0a 2a 2a 20 28 69 66 20 yed back.** (if
12129 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e the correspondin
1212a 67 20 70 44 6f 6e 65 20 62 69 74 20 69 73 20 73 g pDone bit is s
1212b 65 74 29 20 74 68 65 6e 20 73 6b 69 70 20 74 68 et) then skip th
1212c 65 20 70 6c 61 79 62 61 63 6b 2e 0a 2a 2a 20 4d e playback..** M
1212d 61 6b 65 20 73 75 72 65 20 74 68 65 20 70 44 6f ake sure the pDo
1212e 6e 65 20 62 69 74 20 63 6f 72 72 65 73 70 6f 6e ne bit correspon
1212f 64 69 6e 67 20 74 6f 20 74 68 65 20 2a 70 4f 66 ding to the *pOf
12130 66 73 65 74 20 70 61 67 65 20 69 73 20 73 65 74 fset page is set
12131 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 72 65 74 .** prior to ret
12132 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 urning..**.** If
12133 20 74 68 65 20 70 61 67 65 20 72 65 63 6f 72 64 the page record
12134 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 is successfully
12135 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 28 read from the (
12136 73 75 62 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 6c sub-)journal fil
12137 65 0a 2a 2a 20 61 6e 64 20 70 6c 61 79 65 64 20 e.** and played
12138 62 61 63 6b 2c 20 74 68 65 6e 20 53 51 4c 49 54 back, then SQLIT
12139 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 E_OK is returned
1213a 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 . If an IO error
1213b 20 6f 63 63 75 72 73 0a 2a 2a 20 77 68 69 6c 65 occurs.** while
1213c 20 72 65 61 64 69 6e 67 20 74 68 65 20 72 65 63 reading the rec
1213d 6f 72 64 20 66 72 6f 6d 20 74 68 65 20 28 73 75 ord from the (su
1213e 62 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 b-)journal file
1213f 6f 72 20 77 68 69 6c 65 20 77 72 69 74 69 6e 67 or while writing
12140 0a 2a 2a 20 74 6f 20 74 68 65 20 64 61 74 61 62 .** to the datab
12141 61 73 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 74 ase file, then t
12142 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 he IO error code
12143 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 is returned. If
12144 20 64 61 74 61 0a 2a 2a 20 69 73 20 73 75 63 63 data.** is succ
12145 65 73 73 66 75 6c 6c 79 20 72 65 61 64 20 66 72 essfully read fr
12146 6f 6d 20 74 68 65 20 28 73 75 62 2d 29 6a 6f 75 om the (sub-)jou
12147 72 6e 61 6c 20 66 69 6c 65 20 62 75 74 20 61 70 rnal file but ap
12148 70 65 61 72 73 20 74 6f 20 62 65 0a 2a 2a 20 63 pears to be.** c
12149 6f 72 72 75 70 74 65 64 2c 20 53 51 4c 49 54 45 orrupted, SQLITE
1214a 5f 44 4f 4e 45 20 69 73 20 72 65 74 75 72 6e 65 _DONE is returne
1214b 64 2e 20 44 61 74 61 20 69 73 20 63 6f 6e 73 69 d. Data is consi
1214c 64 65 72 65 64 20 63 6f 72 72 75 70 74 65 64 20 dered corrupted
1214d 69 6e 0a 2a 2a 20 74 77 6f 20 63 69 72 63 75 6d in.** two circum
1214e 73 74 61 6e 63 65 73 3a 0a 2a 2a 20 0a 2a 2a 20 stances:.** .**
1214f 20 20 2a 20 49 66 20 74 68 65 20 72 65 63 6f 72 * If the recor
12150 64 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 69 73 d page-number is
12151 20 69 6c 6c 65 67 61 6c 20 28 30 20 6f 72 20 50 illegal (0 or P
12152 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 29 2c 20 6f AGER_MJ_PGNO), o
12153 72 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65 20 r.** * If the
12154 72 65 63 6f 72 64 20 69 73 20 62 65 69 6e 67 20 record is being
12155 72 6f 6c 6c 65 64 20 62 61 63 6b 20 66 72 6f 6d rolled back from
12156 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 the main journa
12157 6c 20 66 69 6c 65 0a 2a 2a 20 20 20 20 20 61 6e l file.** an
12158 64 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 66 d the checksum f
12159 69 65 6c 64 20 64 6f 65 73 20 6e 6f 74 20 6d 61 ield does not ma
1215a 74 63 68 20 74 68 65 20 72 65 63 6f 72 64 20 63 tch the record c
1215b 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4e 65 ontent..**.** Ne
1215c 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 74 ither of these t
1215d 77 6f 20 73 63 65 6e 61 72 69 6f 73 20 61 72 65 wo scenarios are
1215e 20 70 6f 73 73 69 62 6c 65 20 64 75 72 69 6e 67 possible during
1215f 20 61 20 73 61 76 65 70 6f 69 6e 74 20 72 6f 6c a savepoint rol
12160 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 lback..**.** If
12161 74 68 69 73 20 69 73 20 61 20 73 61 76 65 70 6f this is a savepo
12162 69 6e 74 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 int rollback, th
12163 65 6e 20 6d 65 6d 6f 72 79 20 6d 61 79 20 68 61 en memory may ha
12164 76 65 20 74 6f 20 62 65 20 64 79 6e 61 6d 69 63 ve to be dynamic
12165 61 6c 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 ally.** allocate
12166 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 d by this functi
12167 6f 6e 2e 20 49 66 20 74 68 69 73 20 69 73 20 74 on. If this is t
12168 68 65 20 63 61 73 65 20 61 6e 64 20 61 6e 20 61 he case and an a
12169 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c llocation fails,
1216a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d .** SQLITE_NOMEM
1216b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f is returned..*/
1216c 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 .static int page
1216d 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 r_playback_one_p
1216e 61 67 65 28 0a 20 20 50 61 67 65 72 20 2a 70 50 age(. Pager *pP
1216f 61 67 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 ager,
12170 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 /* The page
12171 72 20 62 65 69 6e 67 20 70 6c 61 79 65 64 20 62 r being played b
12172 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4d ack */. int isM
12173 61 69 6e 4a 72 6e 6c 2c 20 20 20 20 20 20 20 20 ainJrnl,
12174 20 20 20 20 20 20 20 2f 2a 20 31 20 2d 3e 20 6d /* 1 -> m
12175 61 69 6e 20 6a 6f 75 72 6e 61 6c 2e 20 30 20 2d ain journal. 0 -
12176 3e 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 2a > sub-journal. *
12177 2f 0a 20 20 69 6e 74 20 69 73 55 6e 73 79 6e 63 /. int isUnsync
12178 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
12179 20 20 2f 2a 20 54 72 75 65 20 69 66 20 72 65 61 /* True if rea
1217a 64 69 6e 67 20 66 72 6f 6d 20 75 6e 73 79 6e 63 ding from unsync
1217b 65 64 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 ed main journal
1217c 2a 2f 0a 20 20 69 36 34 20 2a 70 4f 66 66 73 65 */. i64 *pOffse
1217d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t,
1217e 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 /* Offset of
1217f 72 65 63 6f 72 64 20 74 6f 20 70 6c 61 79 62 61 record to playba
12180 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 73 53 61 ck */. int isSa
12181 76 65 70 6e 74 2c 20 20 20 20 20 20 20 20 20 20 vepnt,
12182 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f /* True fo
12183 72 20 61 20 73 61 76 65 70 6f 69 6e 74 20 72 6f r a savepoint ro
12184 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 42 69 74 76 llback */. Bitv
12185 65 63 20 2a 70 44 6f 6e 65 20 20 20 20 20 20 20 ec *pDone
12186 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 69 74 /* Bit
12187 76 65 63 20 6f 66 20 70 61 67 65 73 20 61 6c 72 vec of pages alr
12188 65 61 64 79 20 70 6c 61 79 65 64 20 62 61 63 6b eady played back
12189 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b */.){. int rc;
1218a 0a 20 20 50 67 48 64 72 20 2a 70 50 67 3b 20 20 . PgHdr *pPg;
1218b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1218c 20 2f 2a 20 41 6e 20 65 78 69 73 74 69 6e 67 20 /* An existing
1218d 70 61 67 65 20 69 6e 20 74 68 65 20 63 61 63 68 page in the cach
1218e 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f e */. Pgno pgno
1218f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
12190 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 /* The page
12191 20 6e 75 6d 62 65 72 20 6f 66 20 61 20 70 61 67 number of a pag
12192 65 20 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a e in journal */.
12193 20 20 75 33 32 20 63 6b 73 75 6d 3b 20 20 20 20 u32 cksum;
12194 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12195 2f 2a 20 43 68 65 63 6b 73 75 6d 20 75 73 65 64 /* Checksum used
12196 20 66 6f 72 20 73 61 6e 69 74 79 20 63 68 65 63 for sanity chec
12197 6b 69 6e 67 20 2a 2f 0a 20 20 75 38 20 2a 61 44 king */. u8 *aD
12198 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 ata;
12199 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f /* Tempo
1219a 72 61 72 79 20 73 74 6f 72 61 67 65 20 66 6f 72 rary storage for
1219b 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 73 the page */. s
1219c 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 6a 66 64 qlite3_file *jfd
1219d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
1219e 54 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 The file descrip
1219f 74 6f 72 20 66 6f 72 20 74 68 65 20 6a 6f 75 72 tor for the jour
121a0 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 61 nal file */.. a
121a1 73 73 65 72 74 28 20 28 69 73 4d 61 69 6e 4a 72 ssert( (isMainJr
121a2 6e 6c 26 7e 31 29 3d 3d 30 20 29 3b 20 20 20 20 nl&~1)==0 );
121a3 20 20 2f 2a 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 /* isMainJrnl
121a4 69 73 20 30 20 6f 72 20 31 20 2a 2f 0a 20 20 61 is 0 or 1 */. a
121a5 73 73 65 72 74 28 20 28 69 73 53 61 76 65 70 6e ssert( (isSavepn
121a6 74 26 7e 31 29 3d 3d 30 20 29 3b 20 20 20 20 20 t&~1)==0 );
121a7 20 20 2f 2a 20 69 73 53 61 76 65 70 6e 74 20 69 /* isSavepnt i
121a8 73 20 30 20 6f 72 20 31 20 2a 2f 0a 20 20 61 73 s 0 or 1 */. as
121a9 73 65 72 74 28 20 69 73 4d 61 69 6e 4a 72 6e 6c sert( isMainJrnl
121aa 20 7c 7c 20 70 44 6f 6e 65 20 29 3b 20 20 20 20 || pDone );
121ab 20 2f 2a 20 70 44 6f 6e 65 20 61 6c 77 61 79 73 /* pDone always
121ac 20 75 73 65 64 20 6f 6e 20 73 75 62 2d 6a 6f 75 used on sub-jou
121ad 72 6e 61 6c 73 20 2a 2f 0a 20 20 61 73 73 65 72 rnals */. asser
121ae 74 28 20 69 73 53 61 76 65 70 6e 74 20 7c 7c 20 t( isSavepnt ||
121af 70 44 6f 6e 65 3d 3d 30 20 29 3b 20 20 20 2f 2a pDone==0 ); /*
121b0 20 70 44 6f 6e 65 20 6e 65 76 65 72 20 75 73 65 pDone never use
121b1 64 20 6f 6e 20 6e 6f 6e 2d 73 61 76 65 70 6f 69 d on non-savepoi
121b2 6e 74 20 2a 2f 0a 0a 20 20 61 44 61 74 61 20 3d nt */.. aData =
121b3 20 28 75 38 2a 29 70 50 61 67 65 72 2d 3e 70 54 (u8*)pPager->pT
121b4 6d 70 53 70 61 63 65 3b 0a 20 20 61 73 73 65 72 mpSpace;. asser
121b5 74 28 20 61 44 61 74 61 20 29 3b 20 20 20 20 20 t( aData );
121b6 20 20 20 20 2f 2a 20 54 65 6d 70 20 73 74 6f 72 /* Temp stor
121b7 61 67 65 20 6d 75 73 74 20 68 61 76 65 20 61 6c age must have al
121b8 72 65 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 ready been alloc
121b9 61 74 65 64 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 ated */.. /* Re
121ba 61 64 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 ad the page numb
121bb 65 72 20 61 6e 64 20 70 61 67 65 20 64 61 74 61 er and page data
121bc 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 from the journa
121bd 6c 20 6f 72 20 73 75 62 2d 6a 6f 75 72 6e 61 6c l or sub-journal
121be 0a 20 20 2a 2a 20 66 69 6c 65 2e 20 52 65 74 75 . ** file. Retu
121bf 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 rn an error code
121c0 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 20 69 to the caller i
121c1 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 f an IO error oc
121c2 63 75 72 73 2e 0a 20 20 2a 2f 0a 20 20 6a 66 64 curs.. */. jfd
121c3 20 3d 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 3f 20 = isMainJrnl ?
121c4 70 50 61 67 65 72 2d 3e 6a 66 64 20 3a 20 70 50 pPager->jfd : pP
121c5 61 67 65 72 2d 3e 73 6a 66 64 3b 0a 20 20 72 63 ager->sjfd;. rc
121c6 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 6a 66 = read32bits(jf
121c7 64 2c 20 2a 70 4f 66 66 73 65 74 2c 20 26 70 67 d, *pOffset, &pg
121c8 6e 6f 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 no);. if( rc!=S
121c9 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 QLITE_OK ) retur
121ca 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c n rc;. rc = sql
121cb 69 74 65 33 4f 73 52 65 61 64 28 6a 66 64 2c 20 ite3OsRead(jfd,
121cc 61 44 61 74 61 2c 20 70 50 61 67 65 72 2d 3e 70 aData, pPager->p
121cd 61 67 65 53 69 7a 65 2c 20 28 2a 70 4f 66 66 73 ageSize, (*pOffs
121ce 65 74 29 2b 34 29 3b 0a 20 20 69 66 28 20 72 63 et)+4);. if( rc
121cf 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 !=SQLITE_OK ) re
121d0 74 75 72 6e 20 72 63 3b 0a 20 20 2a 70 4f 66 66 turn rc;. *pOff
121d1 73 65 74 20 2b 3d 20 70 50 61 67 65 72 2d 3e 70 set += pPager->p
121d2 61 67 65 53 69 7a 65 20 2b 20 34 20 2b 20 69 73 ageSize + 4 + is
121d3 4d 61 69 6e 4a 72 6e 6c 2a 34 3b 0a 0a 20 20 2f MainJrnl*4;.. /
121d4 2a 20 53 61 6e 69 74 79 20 63 68 65 63 6b 69 6e * Sanity checkin
121d5 67 20 6f 6e 20 74 68 65 20 70 61 67 65 2e 20 20 g on the page.
121d6 54 68 69 73 20 69 73 20 6d 6f 72 65 20 69 6d 70 This is more imp
121d7 6f 72 74 61 6e 74 20 74 68 61 74 20 49 20 6f 72 ortant that I or
121d8 69 67 69 6e 61 6c 6c 79 0a 20 20 2a 2a 20 74 68 iginally. ** th
121d9 6f 75 67 68 74 2e 20 20 49 66 20 61 20 70 6f 77 ought. If a pow
121da 65 72 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 er failure occur
121db 73 20 77 68 69 6c 65 20 74 68 65 20 6a 6f 75 72 s while the jour
121dc 6e 61 6c 20 69 73 20 62 65 69 6e 67 20 77 72 69 nal is being wri
121dd 74 74 65 6e 2c 0a 20 20 2a 2a 20 69 74 20 63 6f tten,. ** it co
121de 75 6c 64 20 63 61 75 73 65 20 69 6e 76 61 6c 69 uld cause invali
121df 64 20 64 61 74 61 20 74 6f 20 62 65 20 77 72 69 d data to be wri
121e0 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f tten into the jo
121e1 75 72 6e 61 6c 2e 20 20 57 65 20 6e 65 65 64 20 urnal. We need
121e2 74 6f 0a 20 20 2a 2a 20 64 65 74 65 63 74 20 74 to. ** detect t
121e3 68 69 73 20 69 6e 76 61 6c 69 64 20 64 61 74 61 his invalid data
121e4 20 28 77 69 74 68 20 68 69 67 68 20 70 72 6f 62 (with high prob
121e5 61 62 69 6c 69 74 79 29 20 61 6e 64 20 69 67 6e ability) and ign
121e6 6f 72 65 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 ore it.. */. i
121e7 66 28 20 70 67 6e 6f 3d 3d 30 20 7c 7c 20 70 67 f( pgno==0 || pg
121e8 6e 6f 3d 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e no==PAGER_MJ_PGN
121e9 4f 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 O(pPager) ){.
121ea 20 61 73 73 65 72 74 28 20 21 69 73 53 61 76 65 assert( !isSave
121eb 70 6e 74 20 29 3b 0a 20 20 20 20 72 65 74 75 72 pnt );. retur
121ec 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 n SQLITE_DONE;.
121ed 20 7d 0a 20 20 69 66 28 20 70 67 6e 6f 3e 28 50 }. if( pgno>(P
121ee 67 6e 6f 29 70 50 61 67 65 72 2d 3e 64 62 53 69 gno)pPager->dbSi
121ef 7a 65 20 7c 7c 20 73 71 6c 69 74 65 33 42 69 74 ze || sqlite3Bit
121f0 76 65 63 54 65 73 74 28 70 44 6f 6e 65 2c 20 70 vecTest(pDone, p
121f1 67 6e 6f 29 20 29 7b 0a 20 20 20 20 72 65 74 75 gno) ){. retu
121f2 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
121f3 7d 0a 20 20 69 66 28 20 69 73 4d 61 69 6e 4a 72 }. if( isMainJr
121f4 6e 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 72 nl ){. rc = r
121f5 65 61 64 33 32 62 69 74 73 28 6a 66 64 2c 20 28 ead32bits(jfd, (
121f6 2a 70 4f 66 66 73 65 74 29 2d 34 2c 20 26 63 6b *pOffset)-4, &ck
121f7 73 75 6d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 sum);. if( rc
121f8 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
121f9 20 20 69 66 28 20 21 69 73 53 61 76 65 70 6e 74 if( !isSavepnt
121fa 20 26 26 20 70 61 67 65 72 5f 63 6b 73 75 6d 28 && pager_cksum(
121fb 70 50 61 67 65 72 2c 20 61 44 61 74 61 29 21 3d pPager, aData)!=
121fc 63 6b 73 75 6d 20 29 7b 0a 20 20 20 20 20 20 72 cksum ){. r
121fd 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e eturn SQLITE_DON
121fe 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 E;. }. }..
121ff 69 66 28 20 70 44 6f 6e 65 20 26 26 20 28 72 63 if( pDone && (rc
12200 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 = sqlite3Bitvec
12201 53 65 74 28 70 44 6f 6e 65 2c 20 70 67 6e 6f 29 Set(pDone, pgno)
12202 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a )!=SQLITE_OK ){.
12203 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
12204 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 }.. assert( pP
12205 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 ager->state==PAG
12206 45 52 5f 52 45 53 45 52 56 45 44 20 7c 7c 20 70 ER_RESERVED || p
12207 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 Pager->state>=PA
12208 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 29 3b GER_EXCLUSIVE );
12209 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 .. /* If the pa
1220a 67 65 72 20 69 73 20 69 6e 20 52 45 53 45 52 56 ger is in RESERV
1220b 45 44 20 73 74 61 74 65 2c 20 74 68 65 6e 20 74 ED state, then t
1220c 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 20 63 here must be a c
1220d 6f 70 79 20 6f 66 20 74 68 69 73 0a 20 20 2a 2a opy of this. **
1220e 20 70 61 67 65 20 69 6e 20 74 68 65 20 70 61 67 page in the pag
1220f 65 72 20 63 61 63 68 65 2e 20 49 6e 20 74 68 69 er cache. In thi
12210 73 20 63 61 73 65 20 6a 75 73 74 20 75 70 64 61 s case just upda
12211 74 65 20 74 68 65 20 70 61 67 65 72 20 63 61 63 te the pager cac
12212 68 65 2c 0a 20 20 2a 2a 20 6e 6f 74 20 74 68 65 he,. ** not the
12213 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
12214 54 68 65 20 70 61 67 65 20 69 73 20 6c 65 66 74 The page is left
12215 20 6d 61 72 6b 65 64 20 64 69 72 74 79 20 69 6e marked dirty in
12216 20 74 68 69 73 20 63 61 73 65 2e 0a 20 20 2a 2a this case.. **
12217 0a 20 20 2a 2a 20 41 6e 20 65 78 63 65 70 74 69 . ** An excepti
12218 6f 6e 20 74 6f 20 74 68 65 20 61 62 6f 76 65 20 on to the above
12219 72 75 6c 65 3a 20 49 66 20 74 68 65 20 64 61 74 rule: If the dat
1221a 61 62 61 73 65 20 69 73 20 69 6e 20 6e 6f 2d 73 abase is in no-s
1221b 79 6e 63 20 6d 6f 64 65 0a 20 20 2a 2a 20 61 6e ync mode. ** an
1221c 64 20 61 20 70 61 67 65 20 69 73 20 6d 6f 76 65 d a page is move
1221d 64 20 64 75 72 69 6e 67 20 61 6e 20 69 6e 63 72 d during an incr
1221e 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 74 emental vacuum t
1221f 68 65 6e 20 74 68 65 20 70 61 67 65 20 6d 61 79 hen the page may
12220 0a 20 20 2a 2a 20 6e 6f 74 20 62 65 20 69 6e 20 . ** not be in
12221 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65 2e the pager cache.
12222 20 4c 61 74 65 72 3a 20 69 66 20 61 20 6d 61 6c Later: if a mal
12223 6c 6f 63 28 29 20 6f 72 20 49 4f 20 65 72 72 6f loc() or IO erro
12224 72 20 6f 63 63 75 72 73 0a 20 20 2a 2a 20 64 75 r occurs. ** du
12225 72 69 6e 67 20 61 20 4d 6f 76 65 70 61 67 65 28 ring a Movepage(
12226 29 20 63 61 6c 6c 2c 20 74 68 65 6e 20 74 68 65 ) call, then the
12227 20 70 61 67 65 20 6d 61 79 20 6e 6f 74 20 62 65 page may not be
12228 20 69 6e 20 74 68 65 20 63 61 63 68 65 0a 20 20 in the cache.
12229 2a 2a 20 65 69 74 68 65 72 2e 20 53 6f 20 74 68 ** either. So th
1222a 65 20 63 6f 6e 64 69 74 69 6f 6e 20 64 65 73 63 e condition desc
1222b 72 69 62 65 64 20 69 6e 20 74 68 65 20 61 62 6f ribed in the abo
1222c 76 65 20 70 61 72 61 67 72 61 70 68 20 69 73 20 ve paragraph is
1222d 6e 6f 74 0a 20 20 2a 2a 20 61 73 73 65 72 74 28 not. ** assert(
1222e 29 61 62 6c 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a )able.. **. **
1222f 20 49 66 20 69 6e 20 45 58 43 4c 55 53 49 56 45 If in EXCLUSIVE
12230 20 73 74 61 74 65 2c 20 74 68 65 6e 20 77 65 20 state, then we
12231 75 70 64 61 74 65 20 74 68 65 20 70 61 67 65 72 update the pager
12232 20 63 61 63 68 65 20 69 66 20 69 74 20 65 78 69 cache if it exi
12233 73 74 73 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 sts. ** and the
12234 20 6d 61 69 6e 20 66 69 6c 65 2e 20 54 68 65 20 main file. The
12235 70 61 67 65 20 69 73 20 74 68 65 6e 20 6d 61 72 page is then mar
12236 6b 65 64 20 6e 6f 74 20 64 69 72 74 79 2e 0a 20 ked not dirty..
12237 20 2a 2a 0a 20 20 2a 2a 20 54 69 63 6b 65 74 20 **. ** Ticket
12238 23 31 31 37 31 3a 20 20 54 68 65 20 73 74 61 74 #1171: The stat
12239 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 6d 69 ement journal mi
1223a 67 68 74 20 63 6f 6e 74 61 69 6e 20 70 61 67 65 ght contain page
1223b 20 63 6f 6e 74 65 6e 74 20 74 68 61 74 20 69 73 content that is
1223c 0a 20 20 2a 2a 20 64 69 66 66 65 72 65 6e 74 20 . ** different
1223d 66 72 6f 6d 20 74 68 65 20 70 61 67 65 20 63 6f from the page co
1223e 6e 74 65 6e 74 20 61 74 20 74 68 65 20 73 74 61 ntent at the sta
1223f 72 74 20 6f 66 20 74 68 65 20 74 72 61 6e 73 61 rt of the transa
12240 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 54 68 69 73 ction.. ** This
12241 20 6f 63 63 75 72 73 20 77 68 65 6e 20 61 20 70 occurs when a p
12242 61 67 65 20 69 73 20 63 68 61 6e 67 65 64 20 70 age is changed p
12243 72 69 6f 72 20 74 6f 20 74 68 65 20 73 74 61 72 rior to the star
12244 74 20 6f 66 20 61 20 73 74 61 74 65 6d 65 6e 74 t of a statement
12245 0a 20 20 2a 2a 20 74 68 65 6e 20 63 68 61 6e 67 . ** then chang
12246 65 64 20 61 67 61 69 6e 20 77 69 74 68 69 6e 20 ed again within
12247 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 the statement.
12248 57 68 65 6e 20 72 6f 6c 6c 69 6e 67 20 62 61 63 When rolling bac
12249 6b 20 73 75 63 68 20 61 0a 20 20 2a 2a 20 73 74 k such a. ** st
1224a 61 74 65 6d 65 6e 74 20 77 65 20 6d 75 73 74 20 atement we must
1224b 6e 6f 74 20 77 72 69 74 65 20 74 6f 20 74 68 65 not write to the
1224c 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 62 61 original databa
1224d 73 65 20 75 6e 6c 65 73 73 20 77 65 20 6b 6e 6f se unless we kno
1224e 77 0a 20 20 2a 2a 20 66 6f 72 20 63 65 72 74 61 w. ** for certa
1224f 69 6e 20 74 68 61 74 20 6f 72 69 67 69 6e 61 6c in that original
12250 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 73 20 61 page contents a
12251 72 65 20 73 79 6e 63 65 64 20 69 6e 74 6f 20 74 re synced into t
12252 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b he main rollback
12253 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 2e 20 20 . ** journal.
12254 4f 74 68 65 72 77 69 73 65 2c 20 61 20 70 6f 77 Otherwise, a pow
12255 65 72 20 6c 6f 73 73 20 6d 69 67 68 74 20 6c 65 er loss might le
12256 61 76 65 20 6d 6f 64 69 66 69 65 64 20 64 61 74 ave modified dat
12257 61 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 a in the. ** da
12258 74 61 62 61 73 65 20 66 69 6c 65 20 77 69 74 68 tabase file with
12259 6f 75 74 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 out an entry in
1225a 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 the rollback jou
1225b 72 6e 61 6c 20 74 68 61 74 20 63 61 6e 0a 20 20 rnal that can.
1225c 2a 2a 20 72 65 73 74 6f 72 65 20 74 68 65 20 64 ** restore the d
1225d 61 74 61 62 61 73 65 20 74 6f 20 69 74 73 20 6f atabase to its o
1225e 72 69 67 69 6e 61 6c 20 66 6f 72 6d 2e 20 20 54 riginal form. T
1225f 77 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 20 6d 75 wo conditions mu
12260 73 74 20 62 65 0a 20 20 2a 2a 20 6d 65 74 20 62 st be. ** met b
12261 65 66 6f 72 65 20 77 72 69 74 69 6e 67 20 74 6f efore writing to
12262 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
12263 6c 65 73 2e 20 28 31 29 20 74 68 65 20 64 61 74 les. (1) the dat
12264 61 62 61 73 65 20 6d 75 73 74 20 62 65 0a 20 20 abase must be.
12265 2a 2a 20 6c 6f 63 6b 65 64 2e 20 20 28 32 29 20 ** locked. (2)
12266 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 we know that the
12267 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 20 63 original page c
12268 6f 6e 74 65 6e 74 20 69 73 20 66 75 6c 6c 79 20 ontent is fully
12269 73 79 6e 63 65 64 0a 20 20 2a 2a 20 69 6e 20 74 synced. ** in t
1226a 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 he main journal
1226b 65 69 74 68 65 72 20 62 65 63 61 75 73 65 20 74 either because t
1226c 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69 he page is not i
1226d 6e 20 63 61 63 68 65 20 6f 72 20 65 6c 73 65 0a n cache or else.
1226e 20 20 2a 2a 20 74 68 65 20 70 61 67 65 20 69 73 ** the page is
1226f 20 6d 61 72 6b 65 64 20 61 73 20 6e 65 65 64 53 marked as needS
12270 79 6e 63 3d 3d 30 2e 0a 20 20 2a 2a 0a 20 20 2a ync==0.. **. *
12271 2a 20 32 30 30 38 2d 30 34 2d 31 34 3a 20 20 57 * 2008-04-14: W
12272 68 65 6e 20 61 74 74 65 6d 70 74 69 6e 67 20 74 hen attempting t
12273 6f 20 76 61 63 75 75 6d 20 61 20 63 6f 72 72 75 o vacuum a corru
12274 70 74 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 pt database file
12275 2c 20 69 74 0a 20 20 2a 2a 20 69 73 20 70 6f 73 , it. ** is pos
12276 73 69 62 6c 65 20 74 6f 20 66 61 69 6c 20 61 20 sible to fail a
12277 73 74 61 74 65 6d 65 6e 74 20 6f 6e 20 61 20 64 statement on a d
12278 61 74 61 62 61 73 65 20 74 68 61 74 20 64 6f 65 atabase that doe
12279 73 20 6e 6f 74 20 79 65 74 20 65 78 69 73 74 2e s not yet exist.
1227a 0a 20 20 2a 2a 20 44 6f 20 6e 6f 74 20 61 74 74 . ** Do not att
1227b 65 6d 70 74 20 74 6f 20 77 72 69 74 65 20 69 66 empt to write if
1227c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 database file h
1227d 61 73 20 6e 65 76 65 72 20 62 65 65 6e 20 6f 70 as never been op
1227e 65 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 70 50 67 ened.. */. pPg
1227f 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 = pager_lookup(
12280 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 3b 0a 20 pPager, pgno);.
12281 20 61 73 73 65 72 74 28 20 70 50 67 20 7c 7c 20 assert( pPg ||
12282 21 4d 45 4d 44 42 20 29 3b 0a 20 20 50 41 47 45 !MEMDB );. PAGE
12283 52 54 52 41 43 45 28 28 22 50 4c 41 59 42 41 43 RTRACE(("PLAYBAC
12284 4b 20 25 64 20 70 61 67 65 20 25 64 20 68 61 73 K %d page %d has
12285 68 28 25 30 38 78 29 20 25 73 5c 6e 22 2c 0a 20 h(%08x) %s\n",.
12286 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 41 PA
12287 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 GERID(pPager), p
12288 67 6e 6f 2c 20 70 61 67 65 72 5f 64 61 74 61 68 gno, pager_datah
12289 61 73 68 28 70 50 61 67 65 72 2d 3e 70 61 67 65 ash(pPager->page
1228a 53 69 7a 65 2c 20 61 44 61 74 61 29 2c 0a 20 20 Size, aData),.
1228b 20 20 20 20 20 20 20 20 20 20 20 20 20 28 69 73 (is
1228c 4d 61 69 6e 4a 72 6e 6c 3f 22 6d 61 69 6e 2d 6a MainJrnl?"main-j
1228d 6f 75 72 6e 61 6c 22 3a 22 73 75 62 2d 6a 6f 75 ournal":"sub-jou
1228e 72 6e 61 6c 22 29 0a 20 20 29 29 3b 0a 20 20 69 rnal"). ));. i
1228f 66 28 20 28 70 50 61 67 65 72 2d 3e 73 74 61 74 f( (pPager->stat
12290 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 e>=PAGER_EXCLUSI
12291 56 45 29 0a 20 20 20 26 26 20 28 70 50 67 3d 3d VE). && (pPg==
12292 30 20 7c 7c 20 30 3d 3d 28 70 50 67 2d 3e 66 6c 0 || 0==(pPg->fl
12293 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 ags&PGHDR_NEED_S
12294 59 4e 43 29 29 0a 20 20 20 26 26 20 69 73 4f 70 YNC)). && isOp
12295 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 0a 20 en(pPager->fd).
12296 20 20 26 26 20 21 69 73 55 6e 73 79 6e 63 0a 20 && !isUnsync.
12297 20 29 7b 0a 20 20 20 20 69 36 34 20 6f 66 73 74 ){. i64 ofst
12298 20 3d 20 28 70 67 6e 6f 2d 31 29 2a 28 69 36 34 = (pgno-1)*(i64
12299 29 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a )pPager->pageSiz
1229a 65 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 e;. rc = sqli
1229b 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 te3OsWrite(pPage
1229c 72 2d 3e 66 64 2c 20 61 44 61 74 61 2c 20 70 50 r->fd, aData, pP
1229d 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 ager->pageSize,
1229e 6f 66 73 74 29 3b 0a 20 20 20 20 69 66 28 20 70 ofst);. if( p
1229f 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 62 46 69 gno>pPager->dbFi
122a0 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 leSize ){.
122a1 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 pPager->dbFileSi
122a2 7a 65 20 3d 20 70 67 6e 6f 3b 0a 20 20 20 20 7d ze = pgno;. }
122a3 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d . if( pPager-
122a4 3e 70 42 61 63 6b 75 70 20 29 7b 0a 20 20 20 20 >pBackup ){.
122a5 20 20 43 4f 44 45 43 31 28 70 50 61 67 65 72 2c CODEC1(pPager,
122a6 20 61 44 61 74 61 2c 20 70 67 6e 6f 2c 20 33 2c aData, pgno, 3,
122a7 20 72 63 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d rc=SQLITE_NOMEM
122a8 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
122a9 42 61 63 6b 75 70 55 70 64 61 74 65 28 70 50 61 BackupUpdate(pPa
122aa 67 65 72 2d 3e 70 42 61 63 6b 75 70 2c 20 70 67 ger->pBackup, pg
122ab 6e 6f 2c 20 61 44 61 74 61 29 3b 0a 20 20 20 20 no, aData);.
122ac 20 20 43 4f 44 45 43 31 28 70 50 61 67 65 72 2c CODEC1(pPager,
122ad 20 61 44 61 74 61 2c 20 70 67 6e 6f 2c 20 30 2c aData, pgno, 0,
122ae 20 72 63 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d rc=SQLITE_NOMEM
122af 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 );. }. }else
122b0 20 69 66 28 20 21 69 73 4d 61 69 6e 4a 72 6e 6c if( !isMainJrnl
122b1 20 26 26 20 70 50 67 3d 3d 30 20 29 7b 0a 20 20 && pPg==0 ){.
122b2 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 /* If this is
122b3 61 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 61 20 a rollback of a
122b4 73 61 76 65 70 6f 69 6e 74 20 61 6e 64 20 64 61 savepoint and da
122b5 74 61 20 77 61 73 20 6e 6f 74 20 77 72 69 74 74 ta was not writt
122b6 65 6e 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 en to. ** the
122b7 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 74 68 database and th
122b8 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69 6e e page is not in
122b9 2d 6d 65 6d 6f 72 79 2c 20 74 68 65 72 65 20 69 -memory, there i
122ba 73 20 61 20 70 6f 74 65 6e 74 69 61 6c 0a 20 20 s a potential.
122bb 20 20 2a 2a 20 70 72 6f 62 6c 65 6d 2e 20 57 68 ** problem. Wh
122bc 65 6e 20 74 68 65 20 70 61 67 65 20 69 73 20 6e en the page is n
122bd 65 78 74 20 66 65 74 63 68 65 64 20 62 79 20 74 ext fetched by t
122be 68 65 20 62 2d 74 72 65 65 20 6c 61 79 65 72 2c he b-tree layer,
122bf 20 69 74 20 0a 20 20 20 20 2a 2a 20 77 69 6c 6c it . ** will
122c0 20 62 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68 be read from th
122c1 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c e database file,
122c2 20 77 68 69 63 68 20 6d 61 79 20 6f 72 20 6d 61 which may or ma
122c3 79 20 6e 6f 74 20 62 65 20 0a 20 20 20 20 2a 2a y not be . **
122c4 20 63 75 72 72 65 6e 74 2e 20 0a 20 20 20 20 2a current. . *
122c5 2a 0a 20 20 20 20 2a 2a 20 54 68 65 72 65 20 61 *. ** There a
122c6 72 65 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 64 re a couple of d
122c7 69 66 66 65 72 65 6e 74 20 77 61 79 73 20 74 68 ifferent ways th
122c8 69 73 20 63 61 6e 20 68 61 70 70 65 6e 2e 20 41 is can happen. A
122c9 6c 6c 20 61 72 65 20 71 75 69 74 65 0a 20 20 20 ll are quite.
122ca 20 2a 2a 20 6f 62 73 63 75 72 65 2e 20 57 68 65 ** obscure. Whe
122cb 6e 20 72 75 6e 6e 69 6e 67 20 69 6e 20 73 79 6e n running in syn
122cc 63 68 72 6f 6e 6f 75 73 20 6d 6f 64 65 2c 20 74 chronous mode, t
122cd 68 69 73 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70 his can only hap
122ce 70 65 6e 20 0a 20 20 20 20 2a 2a 20 69 66 20 74 pen . ** if t
122cf 68 65 20 70 61 67 65 20 69 73 20 6f 6e 20 74 68 he page is on th
122d0 65 20 66 72 65 65 2d 6c 69 73 74 20 61 74 20 74 e free-list at t
122d1 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 he start of the
122d2 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 transaction, the
122d3 6e 0a 20 20 20 20 2a 2a 20 70 6f 70 75 6c 61 74 n. ** populat
122d4 65 64 2c 20 74 68 65 6e 20 6d 6f 76 65 64 20 75 ed, then moved u
122d5 73 69 6e 67 20 73 71 6c 69 74 65 33 50 61 67 65 sing sqlite3Page
122d6 72 4d 6f 76 65 70 61 67 65 28 29 2e 0a 20 20 20 rMovepage()..
122d7 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 **. ** The s
122d8 6f 6c 75 74 69 6f 6e 20 69 73 20 74 6f 20 61 64 olution is to ad
122d9 64 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 d an in-memory p
122da 61 67 65 20 74 6f 20 74 68 65 20 63 61 63 68 65 age to the cache
122db 20 63 6f 6e 74 61 69 6e 69 6e 67 0a 20 20 20 20 containing.
122dc 2a 2a 20 74 68 65 20 64 61 74 61 20 6a 75 73 74 ** the data just
122dd 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 73 read from the s
122de 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 4d 61 72 6b ub-journal. Mark
122df 20 74 68 65 20 70 61 67 65 20 61 73 20 64 69 72 the page as dir
122e0 74 79 20 0a 20 20 20 20 2a 2a 20 61 6e 64 20 69 ty . ** and i
122e1 66 20 74 68 65 20 70 61 67 65 72 20 72 65 71 75 f the pager requ
122e2 69 72 65 73 20 61 20 6a 6f 75 72 6e 61 6c 2d 73 ires a journal-s
122e3 79 6e 63 2c 20 74 68 65 6e 20 6d 61 72 6b 20 74 ync, then mark t
122e4 68 65 20 70 61 67 65 20 61 73 20 0a 20 20 20 20 he page as .
122e5 2a 2a 20 72 65 71 75 69 72 69 6e 67 20 61 20 6a ** requiring a j
122e6 6f 75 72 6e 61 6c 2d 73 79 6e 63 20 62 65 66 6f ournal-sync befo
122e7 72 65 20 69 74 20 69 73 20 77 72 69 74 74 65 6e re it is written
122e8 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 .. */. ass
122e9 65 72 74 28 20 69 73 53 61 76 65 70 6e 74 20 29 ert( isSavepnt )
122ea 3b 0a 20 20 20 20 69 66 28 20 28 72 63 20 3d 20 ;. if( (rc =
122eb 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 75 sqlite3PagerAcqu
122ec 69 72 65 28 70 50 61 67 65 72 2c 20 70 67 6e 6f ire(pPager, pgno
122ed 2c 20 26 70 50 67 2c 20 31 29 29 21 3d 53 51 4c , &pPg, 1))!=SQL
122ee 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
122ef 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d return rc;. }
122f0 0a 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 . pPg->flags
122f1 26 3d 20 7e 50 47 48 44 52 5f 4e 45 45 44 5f 52 &= ~PGHDR_NEED_R
122f2 45 41 44 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 EAD;. sqlite3
122f3 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79 28 PcacheMakeDirty(
122f4 70 50 67 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 pPg);. }. if(
122f5 70 50 67 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f pPg ){. /* No
122f6 20 70 61 67 65 20 73 68 6f 75 6c 64 20 65 76 65 page should eve
122f7 72 20 62 65 20 65 78 70 6c 69 63 69 74 6c 79 20 r be explicitly
122f8 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 68 61 74 rolled back that
122f9 20 69 73 20 69 6e 20 75 73 65 2c 20 65 78 63 65 is in use, exce
122fa 70 74 0a 20 20 20 20 2a 2a 20 66 6f 72 20 70 61 pt. ** for pa
122fb 67 65 20 31 20 77 68 69 63 68 20 69 73 20 68 65 ge 1 which is he
122fc 6c 64 20 69 6e 20 75 73 65 20 69 6e 20 6f 72 64 ld in use in ord
122fd 65 72 20 74 6f 20 6b 65 65 70 20 74 68 65 20 6c er to keep the l
122fe 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 20 20 20 2a ock on the. *
122ff 2a 20 64 61 74 61 62 61 73 65 20 61 63 74 69 76 * database activ
12300 65 2e 20 48 6f 77 65 76 65 72 20 73 75 63 68 20 e. However such
12301 61 20 70 61 67 65 20 6d 61 79 20 62 65 20 72 6f a page may be ro
12302 6c 6c 65 64 20 62 61 63 6b 20 61 73 20 61 20 72 lled back as a r
12303 65 73 75 6c 74 0a 20 20 20 20 2a 2a 20 6f 66 20 esult. ** of
12304 61 6e 20 69 6e 74 65 72 6e 61 6c 20 65 72 72 6f an internal erro
12305 72 20 72 65 73 75 6c 74 69 6e 67 20 69 6e 20 61 r resulting in a
12306 6e 20 61 75 74 6f 6d 61 74 69 63 20 63 61 6c 6c n automatic call
12307 20 74 6f 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 to. ** sqlit
12308 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 e3PagerRollback(
12309 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 76 6f ).. */. vo
1230a 69 64 20 2a 70 44 61 74 61 3b 0a 20 20 20 20 70 id *pData;. p
1230b 44 61 74 61 20 3d 20 70 50 67 2d 3e 70 44 61 74 Data = pPg->pDat
1230c 61 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 44 a;. memcpy(pD
1230d 61 74 61 2c 20 61 44 61 74 61 2c 20 70 50 61 67 ata, aData, pPag
1230e 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 er->pageSize);.
1230f 20 20 20 70 50 61 67 65 72 2d 3e 78 52 65 69 6e pPager->xRein
12310 69 74 65 72 28 70 50 67 29 3b 0a 20 20 20 20 69 iter(pPg);. i
12311 66 28 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 26 26 f( isMainJrnl &&
12312 20 28 21 69 73 53 61 76 65 70 6e 74 20 7c 7c 20 (!isSavepnt ||
12313 2a 70 4f 66 66 73 65 74 3c 3d 70 50 61 67 65 72 *pOffset<=pPager
12314 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 29 20 29 7b ->journalHdr) ){
12315 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 . /* If the
12316 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 69 contents of thi
12317 73 20 70 61 67 65 20 77 65 72 65 20 6a 75 73 74 s page were just
12318 20 72 65 73 74 6f 72 65 64 20 66 72 6f 6d 20 74 restored from t
12319 68 65 20 6d 61 69 6e 20 0a 20 20 20 20 20 20 2a he main . *
1231a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 * journal file,
1231b 74 68 65 6e 20 69 74 73 20 63 6f 6e 74 65 6e 74 then its content
1231c 20 6d 75 73 74 20 62 65 20 61 73 20 74 68 65 79 must be as they
1231d 20 77 65 72 65 20 77 68 65 6e 20 74 68 65 20 0a were when the .
1231e 20 20 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 ** transac
1231f 74 69 6f 6e 20 77 61 73 20 66 69 72 73 74 20 6f tion was first o
12320 70 65 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 63 pened. In this c
12321 61 73 65 20 77 65 20 63 61 6e 20 6d 61 72 6b 20 ase we can mark
12322 74 68 65 20 70 61 67 65 0a 20 20 20 20 20 20 2a the page. *
12323 2a 20 61 73 20 63 6c 65 61 6e 2c 20 73 69 6e 63 * as clean, sinc
12324 65 20 74 68 65 72 65 20 77 69 6c 6c 20 62 65 20 e there will be
12325 6e 6f 20 6e 65 65 64 20 74 6f 20 77 72 69 74 65 no need to write
12326 20 69 74 20 6f 75 74 20 74 6f 20 74 68 65 2e 0a it out to the..
12327 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a **. *
12328 2a 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20 65 * There is one e
12329 78 63 65 70 74 69 6f 6e 20 74 6f 20 74 68 69 73 xception to this
1232a 20 72 75 6c 65 2e 20 49 66 20 74 68 65 20 70 61 rule. If the pa
1232b 67 65 20 69 73 20 62 65 69 6e 67 20 72 6f 6c 6c ge is being roll
1232c 65 64 0a 20 20 20 20 20 20 2a 2a 20 62 61 63 6b ed. ** back
1232d 20 61 73 20 70 61 72 74 20 6f 66 20 61 20 73 61 as part of a sa
1232e 76 65 70 6f 69 6e 74 20 28 6f 72 20 73 74 61 74 vepoint (or stat
1232f 65 6d 65 6e 74 29 20 72 6f 6c 6c 62 61 63 6b 20 ement) rollback
12330 66 72 6f 6d 20 61 6e 20 0a 20 20 20 20 20 20 2a from an . *
12331 2a 20 75 6e 73 79 6e 63 65 64 20 70 6f 72 74 69 * unsynced porti
12332 6f 6e 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a on of the main j
12333 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68 65 ournal file, the
12334 6e 20 69 74 20 69 73 20 6e 6f 74 20 73 61 66 65 n it is not safe
12335 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 6d 61 72 . ** to mar
12336 6b 20 74 68 65 20 70 61 67 65 20 61 73 20 63 6c k the page as cl
12337 65 61 6e 2e 20 54 68 69 73 20 69 73 20 62 65 63 ean. This is bec
12338 61 75 73 65 20 6d 61 72 6b 69 6e 67 20 74 68 65 ause marking the
12339 20 70 61 67 65 20 61 73 0a 20 20 20 20 20 20 2a page as. *
1233a 2a 20 63 6c 65 61 6e 20 77 69 6c 6c 20 63 6c 65 * clean will cle
1233b 61 72 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45 ar the PGHDR_NEE
1233c 44 5f 53 59 4e 43 20 66 6c 61 67 2e 20 53 69 6e D_SYNC flag. Sin
1233d 63 65 20 74 68 65 20 70 61 67 65 20 69 73 0a 20 ce the page is.
1233e 20 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 ** already
1233f 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 in the journal f
12340 69 6c 65 20 28 72 65 63 6f 72 64 65 64 20 69 6e ile (recorded in
12341 20 50 61 67 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 Pager.pInJourna
12342 6c 29 20 61 6e 64 0a 20 20 20 20 20 20 2a 2a 20 l) and. **
12343 74 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 the PGHDR_NEED_S
12344 59 4e 43 20 66 6c 61 67 20 69 73 20 63 6c 65 61 YNC flag is clea
12345 72 65 64 2c 20 69 66 20 74 68 65 20 70 61 67 65 red, if the page
12346 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 0a 20 is written to.
12347 20 20 20 20 20 2a 2a 20 61 67 61 69 6e 20 77 69 ** again wi
12348 74 68 69 6e 20 74 68 69 73 20 74 72 61 6e 73 61 thin this transa
12349 63 74 69 6f 6e 2c 20 69 74 20 77 69 6c 6c 20 62 ction, it will b
1234a 65 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72 74 e marked as dirt
1234b 79 20 62 75 74 0a 20 20 20 20 20 20 2a 2a 20 74 y but. ** t
1234c 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 he PGHDR_NEED_SY
1234d 4e 43 20 66 6c 61 67 20 77 69 6c 6c 20 6e 6f 74 NC flag will not
1234e 20 62 65 20 73 65 74 2e 20 49 74 20 63 6f 75 6c be set. It coul
1234f 64 20 74 68 65 6e 20 70 6f 74 65 6e 74 69 61 6c d then potential
12350 6c 79 0a 20 20 20 20 20 20 2a 2a 20 62 65 20 77 ly. ** be w
12351 72 69 74 74 65 6e 20 6f 75 74 20 69 6e 74 6f 20 ritten out into
12352 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
12353 65 20 62 65 66 6f 72 65 20 69 74 73 20 6a 6f 75 e before its jou
12354 72 6e 61 6c 20 66 69 6c 65 0a 20 20 20 20 20 20 rnal file.
12355 2a 2a 20 73 65 67 6d 65 6e 74 20 69 73 20 73 79 ** segment is sy
12356 6e 63 65 64 2e 20 49 66 20 61 20 63 72 61 73 68 nced. If a crash
12357 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 6f occurs during o
12358 72 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 69 73 r following this
12359 2c 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62 ,. ** datab
1235a 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 6d ase corruption m
1235b 61 79 20 65 6e 73 75 65 2e 0a 20 20 20 20 20 20 ay ensue..
1235c 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 */. sqlite3
1235d 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 PcacheMakeClean(
1235e 70 50 67 29 3b 0a 20 20 20 20 7d 0a 23 69 66 64 pPg);. }.#ifd
1235f 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f ef SQLITE_CHECK_
12360 50 41 47 45 53 0a 20 20 20 20 70 50 67 2d 3e 70 PAGES. pPg->p
12361 61 67 65 48 61 73 68 20 3d 20 70 61 67 65 72 5f ageHash = pager_
12362 70 61 67 65 68 61 73 68 28 70 50 67 29 3b 0a 23 pagehash(pPg);.#
12363 65 6e 64 69 66 0a 20 20 20 20 2f 2a 20 49 66 20 endif. /* If
12364 74 68 69 73 20 77 61 73 20 70 61 67 65 20 31 2c this was page 1,
12365 20 74 68 65 6e 20 72 65 73 74 6f 72 65 20 74 68 then restore th
12366 65 20 76 61 6c 75 65 20 6f 66 20 50 61 67 65 72 e value of Pager
12367 2e 64 62 46 69 6c 65 56 65 72 73 2e 0a 20 20 20 .dbFileVers..
12368 20 2a 2a 20 44 6f 20 74 68 69 73 20 62 65 66 6f ** Do this befo
12369 72 65 20 61 6e 79 20 64 65 63 6f 64 69 6e 67 2e re any decoding.
1236a 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 67 6e 6f */. if( pgno
1236b 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d ==1 ){. mem
1236c 63 70 79 28 26 70 50 61 67 65 72 2d 3e 64 62 46 cpy(&pPager->dbF
1236d 69 6c 65 56 65 72 73 2c 20 26 28 28 75 38 2a 29 ileVers, &((u8*)
1236e 70 44 61 74 61 29 5b 32 34 5d 2c 73 69 7a 65 6f pData)[24],sizeo
1236f 66 28 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 f(pPager->dbFile
12370 56 65 72 73 29 29 3b 0a 20 20 20 20 7d 0a 0a 20 Vers));. }..
12371 20 20 20 2f 2a 20 44 65 63 6f 64 65 20 74 68 65 /* Decode the
12372 20 70 61 67 65 20 6a 75 73 74 20 72 65 61 64 20 page just read
12373 66 72 6f 6d 20 64 69 73 6b 20 2a 2f 0a 20 20 20 from disk */.
12374 20 43 4f 44 45 43 31 28 70 50 61 67 65 72 2c 20 CODEC1(pPager,
12375 70 44 61 74 61 2c 20 70 50 67 2d 3e 70 67 6e 6f pData, pPg->pgno
12376 2c 20 33 2c 20 72 63 3d 53 51 4c 49 54 45 5f 4e , 3, rc=SQLITE_N
12377 4f 4d 45 4d 29 3b 0a 20 20 20 20 73 71 6c 69 74 OMEM);. sqlit
12378 65 33 50 63 61 63 68 65 52 65 6c 65 61 73 65 28 e3PcacheRelease(
12379 70 50 67 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 pPg);. }. retu
1237a 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
1237b 50 61 72 61 6d 65 74 65 72 20 7a 4d 61 73 74 65 Parameter zMaste
1237c 72 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 r is the name of
1237d 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 a master journa
1237e 6c 20 66 69 6c 65 2e 20 41 20 73 69 6e 67 6c 65 l file. A single
1237f 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 journal.** file
12380 20 74 68 61 74 20 72 65 66 65 72 72 65 64 20 74 that referred t
12381 6f 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 o the master jou
12382 72 6e 61 6c 20 66 69 6c 65 20 68 61 73 20 6a 75 rnal file has ju
12383 73 74 20 62 65 65 6e 20 72 6f 6c 6c 65 64 20 62 st been rolled b
12384 61 63 6b 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ack..** This rou
12385 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 69 tine checks if i
12386 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 6f t is possible to
12387 20 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74 delete the mast
12388 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c er journal file,
12389 0a 2a 2a 20 61 6e 64 20 64 6f 65 73 20 73 6f 20 .** and does so
1238a 69 66 20 69 74 20 69 73 2e 0a 2a 2a 0a 2a 2a 20 if it is..**.**
1238b 41 72 67 75 6d 65 6e 74 20 7a 4d 61 73 74 65 72 Argument zMaster
1238c 20 6d 61 79 20 70 6f 69 6e 74 20 74 6f 20 50 61 may point to Pa
1238d 67 65 72 2e 70 54 6d 70 53 70 61 63 65 2e 20 53 ger.pTmpSpace. S
1238e 6f 20 74 68 61 74 20 62 75 66 66 65 72 20 69 73 o that buffer is
1238f 20 6e 6f 74 20 0a 2a 2a 20 61 76 61 69 6c 61 62 not .** availab
12390 6c 65 20 66 6f 72 20 75 73 65 20 77 69 74 68 69 le for use withi
12391 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e n this function.
12392 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 6d 61 .**.** When a ma
12393 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
12394 65 20 69 73 20 63 72 65 61 74 65 64 2c 20 69 74 e is created, it
12395 20 69 73 20 70 6f 70 75 6c 61 74 65 64 20 77 69 is populated wi
12396 74 68 20 74 68 65 20 6e 61 6d 65 73 20 0a 2a 2a th the names .**
12397 20 6f 66 20 61 6c 6c 20 6f 66 20 69 74 73 20 63 of all of its c
12398 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 73 2c 20 6f hild journals, o
12399 6e 65 20 61 66 74 65 72 20 61 6e 6f 74 68 65 72 ne after another
1239a 2c 20 66 6f 72 6d 61 74 74 65 64 20 61 73 20 75 , formatted as u
1239b 74 66 2d 38 20 0a 2a 2a 20 65 6e 63 6f 64 65 64 tf-8 .** encoded
1239c 20 74 65 78 74 2e 20 54 68 65 20 65 6e 64 20 6f text. The end o
1239d 66 20 65 61 63 68 20 63 68 69 6c 64 20 6a 6f 75 f each child jou
1239e 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6d 61 72 rnal file is mar
1239f 6b 65 64 20 77 69 74 68 20 61 20 0a 2a 2a 20 6e ked with a .** n
123a0 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 20 62 79 ul-terminator by
123a1 74 65 20 28 30 78 30 30 29 2e 20 69 2e 65 2e 20 te (0x00). i.e.
123a2 74 68 65 20 65 6e 74 69 72 65 20 63 6f 6e 74 65 the entire conte
123a3 6e 74 73 20 6f 66 20 61 20 6d 61 73 74 65 72 20 nts of a master
123a4 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 journal.** file
123a5 66 6f 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f for a transactio
123a6 6e 20 69 6e 76 6f 6c 76 69 6e 67 20 74 77 6f 20 n involving two
123a7 64 61 74 61 62 61 73 65 73 20 6d 69 67 68 74 20 databases might
123a8 62 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 22 2f 68 6f be:.**.** "/ho
123a9 6d 65 2f 62 69 6c 6c 2f 61 2e 64 62 2d 6a 6f 75 me/bill/a.db-jou
123aa 72 6e 61 6c 5c 78 30 30 2f 68 6f 6d 65 2f 62 69 rnal\x00/home/bi
123ab 6c 6c 2f 62 2e 64 62 2d 6a 6f 75 72 6e 61 6c 5c ll/b.db-journal\
123ac 78 30 30 22 0a 2a 2a 0a 2a 2a 20 41 20 6d 61 73 x00".**.** A mas
123ad 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
123ae 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 64 65 6c may only be del
123af 65 74 65 64 20 6f 6e 63 65 20 61 6c 6c 20 6f 66 eted once all of
123b0 20 69 74 73 20 63 68 69 6c 64 20 0a 2a 2a 20 6a its child .** j
123b1 6f 75 72 6e 61 6c 73 20 68 61 76 65 20 62 65 65 ournals have bee
123b2 6e 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a n rolled back..*
123b3 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
123b4 6f 6e 20 72 65 61 64 73 20 74 68 65 20 63 6f 6e on reads the con
123b5 74 65 6e 74 73 20 6f 66 20 74 68 65 20 6d 61 73 tents of the mas
123b6 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter-journal file
123b7 20 69 6e 74 6f 20 0a 2a 2a 20 6d 65 6d 6f 72 79 into .** memory
123b8 20 61 6e 64 20 6c 6f 6f 70 73 20 74 68 72 6f 75 and loops throu
123b9 67 68 20 65 61 63 68 20 6f 66 20 74 68 65 20 63 gh each of the c
123ba 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d hild journal nam
123bb 65 73 2e 20 46 6f 72 0a 2a 2a 20 65 61 63 68 20 es. For.** each
123bc 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 2c 20 69 child journal, i
123bd 74 20 63 68 65 63 6b 73 20 69 66 3a 0a 2a 2a 0a t checks if:.**.
123be 2a 2a 20 20 20 2a 20 69 66 20 74 68 65 20 63 68 ** * if the ch
123bf 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 65 78 69 73 ild journal exis
123c0 74 73 2c 20 61 6e 64 20 69 66 20 73 6f 0a 2a 2a ts, and if so.**
123c1 20 20 20 2a 20 69 66 20 74 68 65 20 63 68 69 6c * if the chil
123c2 64 20 6a 6f 75 72 6e 61 6c 20 63 6f 6e 74 61 69 d journal contai
123c3 6e 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 ns a reference t
123c4 6f 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c o master journal
123c5 20 0a 2a 2a 20 20 20 20 20 66 69 6c 65 20 7a 4d .** file zM
123c6 61 73 74 65 72 0a 2a 2a 0a 2a 2a 20 49 66 20 61 aster.**.** If a
123c7 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 63 child journal c
123c8 61 6e 20 62 65 20 66 6f 75 6e 64 20 74 68 61 74 an be found that
123c9 20 6d 61 74 63 68 65 73 20 62 6f 74 68 20 6f 66 matches both of
123ca 20 74 68 65 20 63 72 69 74 65 72 69 61 0a 2a 2a the criteria.**
123cb 20 61 62 6f 76 65 2c 20 74 68 69 73 20 66 75 6e above, this fun
123cc 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 77 69 ction returns wi
123cd 74 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e 79 74 thout doing anyt
123ce 68 69 6e 67 2e 20 4f 74 68 65 72 77 69 73 65 2c hing. Otherwise,
123cf 20 69 66 0a 2a 2a 20 6e 6f 20 73 75 63 68 20 63 if.** no such c
123d0 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 63 61 6e hild journal can
123d1 20 62 65 20 66 6f 75 6e 64 2c 20 66 69 6c 65 20 be found, file
123d2 7a 4d 61 73 74 65 72 20 69 73 20 64 65 6c 65 74 zMaster is delet
123d3 65 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 66 ed from.** the f
123d4 69 6c 65 2d 73 79 73 74 65 6d 20 75 73 69 6e 67 ile-system using
123d5 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 sqlite3OsDelete
123d6 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 ()..**.** If an
123d7 49 4f 20 65 72 72 6f 72 20 77 69 74 68 69 6e 20 IO error within
123d8 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61 this function, a
123d9 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 n error code is
123da 72 65 74 75 72 6e 65 64 2e 20 54 68 69 73 0a 2a returned. This.*
123db 2a 20 66 75 6e 63 74 69 6f 6e 20 61 6c 6c 6f 63 * function alloc
123dc 61 74 65 73 20 6d 65 6d 6f 72 79 20 62 79 20 63 ates memory by c
123dd 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 4d 61 alling sqlite3Ma
123de 6c 6c 6f 63 28 29 2e 20 49 66 20 61 6e 20 61 6c lloc(). If an al
123df 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 66 61 69 6c location.** fail
123e0 73 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 s, SQLITE_NOMEM
123e1 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 is returned. Oth
123e2 65 72 77 69 73 65 2c 20 69 66 20 6e 6f 20 49 4f erwise, if no IO
123e3 20 6f 72 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 or malloc error
123e4 73 20 0a 2a 2a 20 6f 63 63 75 72 2c 20 53 51 4c s .** occur, SQL
123e5 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
123e6 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 3a 20 ed..**.** TODO:
123e7 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6c This function al
123e8 6c 6f 63 61 74 65 73 20 61 20 73 69 6e 67 6c 65 locates a single
123e9 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 block of memory
123ea 20 74 6f 20 6c 6f 61 64 0a 2a 2a 20 74 68 65 20 to load.** the
123eb 65 6e 74 69 72 65 20 63 6f 6e 74 65 6e 74 73 20 entire contents
123ec 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f of the master jo
123ed 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 69 73 urnal file. This
123ee 20 63 6f 75 6c 64 20 62 65 0a 2a 2a 20 61 20 63 could be.** a c
123ef 6f 75 70 6c 65 20 6f 66 20 6b 69 6c 6f 62 79 74 ouple of kilobyt
123f0 65 73 20 6f 72 20 73 6f 20 2d 20 70 6f 74 65 6e es or so - poten
123f1 74 69 61 6c 6c 79 20 6c 61 72 67 65 72 20 74 68 tially larger th
123f2 61 6e 20 74 68 65 20 70 61 67 65 20 0a 2a 2a 20 an the page .**
123f3 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 size..*/.static
123f4 69 6e 74 20 70 61 67 65 72 5f 64 65 6c 6d 61 73 int pager_delmas
123f5 74 65 72 28 50 61 67 65 72 20 2a 70 50 61 67 65 ter(Pager *pPage
123f6 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a r, const char *z
123f7 4d 61 73 74 65 72 29 7b 0a 20 20 73 71 6c 69 74 Master){. sqlit
123f8 65 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 70 e3_vfs *pVfs = p
123f9 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20 69 Pager->pVfs;. i
123fa 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 nt rc;
123fb 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 /* Retu
123fc 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 73 71 6c rn code */. sql
123fd 69 74 65 33 5f 66 69 6c 65 20 2a 70 4d 61 73 74 ite3_file *pMast
123fe 65 72 3b 20 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 er; /* Malloc
123ff 27 64 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 'd master-journa
12400 6c 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f l file descripto
12401 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 r */. sqlite3_f
12402 69 6c 65 20 2a 70 4a 6f 75 72 6e 61 6c 3b 20 20 ile *pJournal;
12403 20 2f 2a 20 4d 61 6c 6c 6f 63 27 64 20 63 68 69 /* Malloc'd chi
12404 6c 64 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 ld-journal file
12405 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 descriptor */.
12406 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 4a 6f 75 char *zMasterJou
12407 72 6e 61 6c 20 3d 20 30 3b 20 2f 2a 20 43 6f 6e rnal = 0; /* Con
12408 74 65 6e 74 73 20 6f 66 20 6d 61 73 74 65 72 20 tents of master
12409 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a journal file */.
1240a 20 20 69 36 34 20 6e 4d 61 73 74 65 72 4a 6f 75 i64 nMasterJou
1240b 72 6e 61 6c 3b 20 20 20 20 20 20 20 2f 2a 20 53 rnal; /* S
1240c 69 7a 65 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f ize of master jo
1240d 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 0a 20 urnal file */..
1240e 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 /* Allocate spa
1240f 63 65 20 66 6f 72 20 62 6f 74 68 20 74 68 65 20 ce for both the
12410 70 4a 6f 75 72 6e 61 6c 20 61 6e 64 20 70 4d 61 pJournal and pMa
12411 73 74 65 72 20 66 69 6c 65 20 64 65 73 63 72 69 ster file descri
12412 70 74 6f 72 73 2e 0a 20 20 2a 2a 20 49 66 20 73 ptors.. ** If s
12413 75 63 63 65 73 73 66 75 6c 2c 20 6f 70 65 6e 20 uccessful, open
12414 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e the master journ
12415 61 6c 20 66 69 6c 65 20 66 6f 72 20 72 65 61 64 al file for read
12416 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 70 4d 61 73 ing.. */. pMas
12417 74 65 72 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 ter = (sqlite3_f
12418 69 6c 65 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c ile *)sqlite3Mal
12419 6c 6f 63 5a 65 72 6f 28 70 56 66 73 2d 3e 73 7a locZero(pVfs->sz
1241a 4f 73 46 69 6c 65 20 2a 20 32 29 3b 0a 20 20 70 OsFile * 2);. p
1241b 4a 6f 75 72 6e 61 6c 20 3d 20 28 73 71 6c 69 74 Journal = (sqlit
1241c 65 33 5f 66 69 6c 65 20 2a 29 28 28 28 75 38 20 e3_file *)(((u8
1241d 2a 29 70 4d 61 73 74 65 72 29 20 2b 20 70 56 66 *)pMaster) + pVf
1241e 73 2d 3e 73 7a 4f 73 46 69 6c 65 29 3b 0a 20 20 s->szOsFile);.
1241f 69 66 28 20 21 70 4d 61 73 74 65 72 20 29 7b 0a if( !pMaster ){.
12420 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
12421 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73 65 7b 0a NOMEM;. }else{.
12422 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 66 6c const int fl
12423 61 67 73 20 3d 20 28 53 51 4c 49 54 45 5f 4f 50 ags = (SQLITE_OP
12424 45 4e 5f 52 45 41 44 4f 4e 4c 59 7c 53 51 4c 49 EN_READONLY|SQLI
12425 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a TE_OPEN_MASTER_J
12426 4f 55 52 4e 41 4c 29 3b 0a 20 20 20 20 72 63 20 OURNAL);. rc
12427 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 = sqlite3OsOpen(
12428 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 70 pVfs, zMaster, p
12429 4d 61 73 74 65 72 2c 20 66 6c 61 67 73 2c 20 30 Master, flags, 0
1242a 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 21 );. }. if( rc!
1242b 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 =SQLITE_OK ) got
1242c 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b o delmaster_out;
1242d 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 .. rc = sqlite3
1242e 4f 73 46 69 6c 65 53 69 7a 65 28 70 4d 61 73 74 OsFileSize(pMast
1242f 65 72 2c 20 26 6e 4d 61 73 74 65 72 4a 6f 75 72 er, &nMasterJour
12430 6e 61 6c 29 3b 0a 20 20 69 66 28 20 72 63 21 3d nal);. if( rc!=
12431 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f SQLITE_OK ) goto
12432 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a delmaster_out;.
12433 0a 20 20 69 66 28 20 6e 4d 61 73 74 65 72 4a 6f . if( nMasterJo
12434 75 72 6e 61 6c 3e 30 20 29 7b 0a 20 20 20 20 63 urnal>0 ){. c
12435 68 61 72 20 2a 7a 4a 6f 75 72 6e 61 6c 3b 0a 20 har *zJournal;.
12436 20 20 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 char *zMaster
12437 50 74 72 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 Ptr = 0;. int
12438 20 6e 4d 61 73 74 65 72 50 74 72 20 3d 20 70 56 nMasterPtr = pV
12439 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 fs->mxPathname+1
1243a 3b 0a 0a 20 20 20 20 2f 2a 20 4c 6f 61 64 20 74 ;.. /* Load t
1243b 68 65 20 65 6e 74 69 72 65 20 6d 61 73 74 65 72 he entire master
1243c 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e journal file in
1243d 74 6f 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65 to space obtaine
1243e 64 20 66 72 6f 6d 0a 20 20 20 20 2a 2a 20 73 71 d from. ** sq
1243f 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 61 lite3_malloc() a
12440 6e 64 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 nd pointed to by
12441 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 2e zMasterJournal.
12442 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7a 4d 61 . */. zMa
12443 73 74 65 72 4a 6f 75 72 6e 61 6c 20 3d 20 73 71 sterJournal = sq
12444 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 28 69 6e 74 lite3Malloc((int
12445 29 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20 )nMasterJournal
12446 2b 20 6e 4d 61 73 74 65 72 50 74 72 20 2b 20 31 + nMasterPtr + 1
12447 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 4d 61 73 );. if( !zMas
12448 74 65 72 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 terJournal ){.
12449 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1244a 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 67 6f 74 NOMEM;. got
1244b 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b o delmaster_out;
1244c 0a 20 20 20 20 7d 0a 20 20 20 20 7a 4d 61 73 74 . }. zMast
1244d 65 72 50 74 72 20 3d 20 26 7a 4d 61 73 74 65 72 erPtr = &zMaster
1244e 4a 6f 75 72 6e 61 6c 5b 6e 4d 61 73 74 65 72 4a Journal[nMasterJ
1244f 6f 75 72 6e 61 6c 2b 31 5d 3b 0a 20 20 20 20 72 ournal+1];. r
12450 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 c = sqlite3OsRea
12451 64 28 70 4d 61 73 74 65 72 2c 20 7a 4d 61 73 74 d(pMaster, zMast
12452 65 72 4a 6f 75 72 6e 61 6c 2c 20 28 69 6e 74 29 erJournal, (int)
12453 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 2c 20 nMasterJournal,
12454 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 0);. if( rc!=
12455 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f SQLITE_OK ) goto
12456 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a delmaster_out;.
12457 20 20 20 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e zMasterJourn
12458 61 6c 5b 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 al[nMasterJourna
12459 6c 5d 20 3d 20 30 3b 0a 0a 20 20 20 20 7a 4a 6f l] = 0;.. zJo
1245a 75 72 6e 61 6c 20 3d 20 7a 4d 61 73 74 65 72 4a urnal = zMasterJ
1245b 6f 75 72 6e 61 6c 3b 0a 20 20 20 20 77 68 69 6c ournal;. whil
1245c 65 28 20 28 7a 4a 6f 75 72 6e 61 6c 2d 7a 4d 61 e( (zJournal-zMa
1245d 73 74 65 72 4a 6f 75 72 6e 61 6c 29 3c 6e 4d 61 sterJournal)<nMa
1245e 73 74 65 72 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 sterJournal ){.
1245f 20 20 20 20 20 69 6e 74 20 65 78 69 73 74 73 3b int exists;
12460 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
12461 74 65 33 4f 73 41 63 63 65 73 73 28 70 56 66 73 te3OsAccess(pVfs
12462 2c 20 7a 4a 6f 75 72 6e 61 6c 2c 20 53 51 4c 49 , zJournal, SQLI
12463 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 TE_ACCESS_EXISTS
12464 2c 20 26 65 78 69 73 74 73 29 3b 0a 20 20 20 20 , &exists);.
12465 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
12466 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 _OK ){. g
12467 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 oto delmaster_ou
12468 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 t;. }.
12469 20 69 66 28 20 65 78 69 73 74 73 20 29 7b 0a 20 if( exists ){.
1246a 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 /* One of
1246b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 73 20 70 6f the journals po
1246c 69 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65 20 inted to by the
1246d 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 65 master journal e
1246e 78 69 73 74 73 2e 0a 20 20 20 20 20 20 20 20 2a xists.. *
1246f 2a 20 4f 70 65 6e 20 69 74 20 61 6e 64 20 63 68 * Open it and ch
12470 65 63 6b 20 69 66 20 69 74 20 70 6f 69 6e 74 73 eck if it points
12471 20 61 74 20 74 68 65 20 6d 61 73 74 65 72 20 6a at the master j
12472 6f 75 72 6e 61 6c 2e 20 49 66 0a 20 20 20 20 20 ournal. If.
12473 20 20 20 2a 2a 20 73 6f 2c 20 72 65 74 75 72 6e ** so, return
12474 20 77 69 74 68 6f 75 74 20 64 65 6c 65 74 69 6e without deletin
12475 67 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 g the master jou
12476 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20 20 20 rnal file..
12477 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e */. in
12478 74 20 63 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 t c;. int
12479 20 66 6c 61 67 73 20 3d 20 28 53 51 4c 49 54 45 flags = (SQLITE
1247a 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 7c 53 _OPEN_READONLY|S
1247b 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f QLITE_OPEN_MAIN_
1247c 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20 20 20 20 20 JOURNAL);.
1247d 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
1247e 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4a 6f 75 72 Open(pVfs, zJour
1247f 6e 61 6c 2c 20 70 4a 6f 75 72 6e 61 6c 2c 20 66 nal, pJournal, f
12480 6c 61 67 73 2c 20 30 29 3b 0a 20 20 20 20 20 20 lags, 0);.
12481 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
12482 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
12483 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f goto delmaster_
12484 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a out;. }..
12485 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 65 61 rc = rea
12486 64 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 70 dMasterJournal(p
12487 4a 6f 75 72 6e 61 6c 2c 20 7a 4d 61 73 74 65 72 Journal, zMaster
12488 50 74 72 2c 20 6e 4d 61 73 74 65 72 50 74 72 29 Ptr, nMasterPtr)
12489 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
1248a 33 4f 73 43 6c 6f 73 65 28 70 4a 6f 75 72 6e 61 3OsClose(pJourna
1248b 6c 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 l);. if(
1248c 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1248d 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 . goto
1248e 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 delmaster_out;.
1248f 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 }..
12490 20 20 63 20 3d 20 7a 4d 61 73 74 65 72 50 74 72 c = zMasterPtr
12491 5b 30 5d 21 3d 30 20 26 26 20 73 74 72 63 6d 70 [0]!=0 && strcmp
12492 28 7a 4d 61 73 74 65 72 50 74 72 2c 20 7a 4d 61 (zMasterPtr, zMa
12493 73 74 65 72 29 3d 3d 30 3b 0a 20 20 20 20 20 20 ster)==0;.
12494 20 20 69 66 28 20 63 20 29 7b 0a 20 20 20 20 20 if( c ){.
12495 20 20 20 20 20 2f 2a 20 57 65 20 68 61 76 65 20 /* We have
12496 61 20 6d 61 74 63 68 2e 20 44 6f 20 6e 6f 74 20 a match. Do not
12497 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65 delete the maste
12498 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 r journal file.
12499 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 */. got
1249a 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b o delmaster_out;
1249b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1249c 20 7d 0a 20 20 20 20 20 20 7a 4a 6f 75 72 6e 61 }. zJourna
1249d 6c 20 2b 3d 20 28 73 71 6c 69 74 65 33 53 74 72 l += (sqlite3Str
1249e 6c 65 6e 33 30 28 7a 4a 6f 75 72 6e 61 6c 29 2b len30(zJournal)+
1249f 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 1);. }. }.
124a0 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f . rc = sqlite3O
124a1 73 44 65 6c 65 74 65 28 70 56 66 73 2c 20 7a 4d sDelete(pVfs, zM
124a2 61 73 74 65 72 2c 20 30 29 3b 0a 0a 64 65 6c 6d aster, 0);..delm
124a3 61 73 74 65 72 5f 6f 75 74 3a 0a 20 20 69 66 28 aster_out:. if(
124a4 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20 zMasterJournal
124a5 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 ){. sqlite3_f
124a6 72 65 65 28 7a 4d 61 73 74 65 72 4a 6f 75 72 6e ree(zMasterJourn
124a7 61 6c 29 3b 0a 20 20 7d 20 20 0a 20 20 69 66 28 al);. } . if(
124a8 20 70 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 pMaster ){.
124a9 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 sqlite3OsClose(p
124aa 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 61 73 73 Master);. ass
124ab 65 72 74 28 20 21 69 73 4f 70 65 6e 28 70 4a 6f ert( !isOpen(pJo
124ac 75 72 6e 61 6c 29 20 29 3b 0a 20 20 7d 0a 20 20 urnal) );. }.
124ad 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4d 61 sqlite3_free(pMa
124ae 73 74 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 ster);. return
124af 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 rc;.}.../*.** Th
124b0 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 is function is u
124b1 73 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74 68 sed to change th
124b2 65 20 61 63 74 75 61 6c 20 73 69 7a 65 20 6f 66 e actual size of
124b3 20 74 68 65 20 64 61 74 61 62 61 73 65 20 0a 2a the database .*
124b4 2a 20 66 69 6c 65 20 69 6e 20 74 68 65 20 66 69 * file in the fi
124b5 6c 65 2d 73 79 73 74 65 6d 2e 20 54 68 69 73 20 le-system. This
124b6 6f 6e 6c 79 20 68 61 70 70 65 6e 73 20 77 68 65 only happens whe
124b7 6e 20 63 6f 6d 6d 69 74 74 69 6e 67 20 61 20 74 n committing a t
124b8 72 61 6e 73 61 63 74 69 6f 6e 2c 0a 2a 2a 20 6f ransaction,.** o
124b9 72 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 r rolling back a
124ba 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 28 69 6e transaction (in
124bb 63 6c 75 64 69 6e 67 20 72 6f 6c 6c 69 6e 67 20 cluding rolling
124bc 62 61 63 6b 20 61 20 68 6f 74 2d 6a 6f 75 72 6e back a hot-journ
124bd 61 6c 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 al)..**.** If th
124be 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 e main database
124bf 66 69 6c 65 20 69 73 20 6e 6f 74 20 6f 70 65 6e file is not open
124c0 2c 20 6f 72 20 61 6e 20 65 78 63 6c 75 73 69 76 , or an exclusiv
124c1 65 20 6c 6f 63 6b 20 69 73 20 6e 6f 74 0a 2a 2a e lock is not.**
124c2 20 68 65 6c 64 2c 20 74 68 69 73 20 66 75 6e 63 held, this func
124c3 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e tion is a no-op.
124c4 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 Otherwise, the
124c5 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 size of the file
124c6 20 69 73 0a 2a 2a 20 63 68 61 6e 67 65 64 20 74 is.** changed t
124c7 6f 20 6e 50 61 67 65 20 70 61 67 65 73 20 28 6e o nPage pages (n
124c8 50 61 67 65 2a 70 50 61 67 65 72 2d 3e 70 61 67 Page*pPager->pag
124c9 65 53 69 7a 65 20 62 79 74 65 73 29 2e 20 49 66 eSize bytes). If
124ca 20 74 68 65 20 66 69 6c 65 0a 2a 2a 20 6f 6e 20 the file.** on
124cb 64 69 73 6b 20 69 73 20 63 75 72 72 65 6e 74 6c disk is currentl
124cc 79 20 6c 61 72 67 65 72 20 74 68 61 6e 20 6e 50 y larger than nP
124cd 61 67 65 20 70 61 67 65 73 2c 20 74 68 65 6e 20 age pages, then
124ce 75 73 65 20 74 68 65 20 56 46 53 0a 2a 2a 20 78 use the VFS.** x
124cf 54 72 75 6e 63 61 74 65 28 29 20 6d 65 74 68 6f Truncate() metho
124d0 64 20 74 6f 20 74 72 75 6e 63 61 74 65 20 69 74 d to truncate it
124d1 2e 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20 69 74 20 6d ..**.** Or, it m
124d2 69 67 68 74 20 6d 69 67 68 74 20 62 65 20 74 68 ight might be th
124d3 65 20 63 61 73 65 20 74 68 61 74 20 74 68 65 20 e case that the
124d4 66 69 6c 65 20 6f 6e 20 64 69 73 6b 20 69 73 20 file on disk is
124d5 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 0a 2a 2a smaller than .**
124d6 20 6e 50 61 67 65 20 70 61 67 65 73 2e 20 53 6f nPage pages. So
124d7 6d 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 me operating sys
124d8 74 65 6d 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 tem implementati
124d9 6f 6e 73 20 63 61 6e 20 67 65 74 20 63 6f 6e 66 ons can get conf
124da 75 73 65 64 20 69 66 20 0a 2a 2a 20 79 6f 75 20 used if .** you
124db 74 72 79 20 74 6f 20 74 72 75 6e 63 61 74 65 20 try to truncate
124dc 61 20 66 69 6c 65 20 74 6f 20 73 6f 6d 65 20 73 a file to some s
124dd 69 7a 65 20 74 68 61 74 20 69 73 20 6c 61 72 67 ize that is larg
124de 65 72 20 74 68 61 6e 20 69 74 20 0a 2a 2a 20 63 er than it .** c
124df 75 72 72 65 6e 74 6c 79 20 69 73 2c 20 73 6f 20 urrently is, so
124e0 64 65 74 65 63 74 20 74 68 69 73 20 63 61 73 65 detect this case
124e1 20 61 6e 64 20 77 72 69 74 65 20 61 20 73 69 6e and write a sin
124e2 67 6c 65 20 7a 65 72 6f 20 62 79 74 65 20 74 6f gle zero byte to
124e3 20 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66 20 .** the end of
124e4 74 68 65 20 6e 65 77 20 66 69 6c 65 20 69 6e 73 the new file ins
124e5 74 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 tead..**.** If s
124e6 75 63 63 65 73 73 66 75 6c 2c 20 72 65 74 75 72 uccessful, retur
124e7 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 n SQLITE_OK. If
124e8 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 an IO error occu
124e9 72 73 20 77 68 69 6c 65 20 6d 6f 64 69 66 79 69 rs while modifyi
124ea 6e 67 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 ng.** the databa
124eb 73 65 20 66 69 6c 65 2c 20 72 65 74 75 72 6e 20 se file, return
124ec 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 74 the error code t
124ed 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2f o the caller..*/
124ee 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 .static int page
124ef 72 5f 74 72 75 6e 63 61 74 65 28 50 61 67 65 72 r_truncate(Pager
124f0 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 6e *pPager, Pgno n
124f1 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 Page){. int rc
124f2 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 = SQLITE_OK;. i
124f3 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 f( pPager->state
124f4 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 >=PAGER_EXCLUSIV
124f5 45 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 E && isOpen(pPag
124f6 65 72 2d 3e 66 64 29 20 29 7b 0a 20 20 20 20 69 er->fd) ){. i
124f7 36 34 20 63 75 72 72 65 6e 74 53 69 7a 65 2c 20 64 currentSize,
124f8 6e 65 77 53 69 7a 65 3b 0a 20 20 20 20 2f 2a 20 newSize;. /*
124f9 54 4f 44 4f 3a 20 49 73 20 69 74 20 73 61 66 65 TODO: Is it safe
124fa 20 74 6f 20 75 73 65 20 50 61 67 65 72 2e 64 62 to use Pager.db
124fb 46 69 6c 65 53 69 7a 65 20 68 65 72 65 3f 20 2a FileSize here? *
124fc 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 /. rc = sqlit
124fd 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 61 e3OsFileSize(pPa
124fe 67 65 72 2d 3e 66 64 2c 20 26 63 75 72 72 65 6e ger->fd, ¤
124ff 74 53 69 7a 65 29 3b 0a 20 20 20 20 6e 65 77 53 tSize);. newS
12500 69 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e 70 61 ize = pPager->pa
12501 67 65 53 69 7a 65 2a 28 69 36 34 29 6e 50 61 67 geSize*(i64)nPag
12502 65 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 e;. if( rc==S
12503 51 4c 49 54 45 5f 4f 4b 20 26 26 20 63 75 72 72 QLITE_OK && curr
12504 65 6e 74 53 69 7a 65 21 3d 6e 65 77 53 69 7a 65 entSize!=newSize
12505 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 63 75 ){. if( cu
12506 72 72 65 6e 74 53 69 7a 65 3e 6e 65 77 53 69 7a rrentSize>newSiz
12507 65 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 e ){. rc
12508 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 = sqlite3OsTrunc
12509 61 74 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 ate(pPager->fd,
1250a 6e 65 77 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 newSize);.
1250b 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 }else{. r
1250c 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 c = sqlite3OsWri
1250d 74 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 22 te(pPager->fd, "
1250e 22 2c 20 31 2c 20 6e 65 77 53 69 7a 65 2d 31 29 ", 1, newSize-1)
1250f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
12510 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
12511 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50 61 K ){. pPa
12512 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 ger->dbFileSize
12513 3d 20 6e 50 61 67 65 3b 0a 20 20 20 20 20 20 7d = nPage;. }
12514 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
12515 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
12516 20 53 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f Set the value o
12517 66 20 74 68 65 20 50 61 67 65 72 2e 73 65 63 74 f the Pager.sect
12518 6f 72 53 69 7a 65 20 76 61 72 69 61 62 6c 65 20 orSize variable
12519 66 6f 72 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a for the given.**
1251a 20 70 61 67 65 72 20 62 61 73 65 64 20 6f 6e 20 pager based on
1251b 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e the value return
1251c 65 64 20 62 79 20 74 68 65 20 78 53 65 63 74 6f ed by the xSecto
1251d 72 53 69 7a 65 20 6d 65 74 68 6f 64 0a 2a 2a 20 rSize method.**
1251e 6f 66 20 74 68 65 20 6f 70 65 6e 20 64 61 74 61 of the open data
1251f 62 61 73 65 20 66 69 6c 65 2e 20 54 68 65 20 73 base file. The s
12520 65 63 74 6f 72 20 73 69 7a 65 20 77 69 6c 6c 20 ector size will
12521 62 65 20 75 73 65 64 20 75 73 65 64 20 0a 2a 2a be used used .**
12522 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 to determine th
12523 65 20 73 69 7a 65 20 61 6e 64 20 61 6c 69 67 6e e size and align
12524 6d 65 6e 74 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 ment of journal
12525 68 65 61 64 65 72 20 61 6e 64 20 0a 2a 2a 20 6d header and .** m
12526 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 6f aster journal po
12527 69 6e 74 65 72 73 20 77 69 74 68 69 6e 20 63 72 inters within cr
12528 65 61 74 65 64 20 6a 6f 75 72 6e 61 6c 20 66 69 eated journal fi
12529 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 les..**.** For t
1252a 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20 74 emporary files t
1252b 68 65 20 65 66 66 65 63 74 69 76 65 20 73 65 63 he effective sec
1252c 74 6f 72 20 73 69 7a 65 20 69 73 20 61 6c 77 61 tor size is alwa
1252d 79 73 20 35 31 32 20 62 79 74 65 73 2e 0a 2a 2a ys 512 bytes..**
1252e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 66 .** Otherwise, f
1252f 6f 72 20 6e 6f 6e 2d 74 65 6d 70 6f 72 61 72 79 or non-temporary
12530 20 66 69 6c 65 73 2c 20 74 68 65 20 65 66 66 65 files, the effe
12531 63 74 69 76 65 20 73 65 63 74 6f 72 20 73 69 7a ctive sector siz
12532 65 20 69 73 0a 2a 2a 20 74 68 65 20 76 61 6c 75 e is.** the valu
12533 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 e returned by th
12534 65 20 78 53 65 63 74 6f 72 53 69 7a 65 28 29 20 e xSectorSize()
12535 6d 65 74 68 6f 64 20 72 6f 75 6e 64 65 64 20 75 method rounded u
12536 70 20 74 6f 20 35 31 32 20 69 66 0a 2a 2a 20 69 p to 512 if.** i
12537 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 35 t is less than 5
12538 31 32 2c 20 6f 72 20 72 6f 75 6e 64 65 64 20 64 12, or rounded d
12539 6f 77 6e 20 74 6f 20 4d 41 58 5f 53 45 43 54 4f own to MAX_SECTO
1253a 52 5f 53 49 5a 45 20 69 66 20 69 74 0a 2a 2a 20 R_SIZE if it.**
1253b 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 is greater than
1253c 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 2e MAX_SECTOR_SIZE.
1253d 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
1253e 73 65 74 53 65 63 74 6f 72 53 69 7a 65 28 50 61 setSectorSize(Pa
1253f 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
12540 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 assert( isOpen(p
12541 50 61 67 65 72 2d 3e 66 64 29 20 7c 7c 20 70 50 Pager->fd) || pP
12542 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 ager->tempFile )
12543 3b 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 ;.. if( !pPager
12544 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 ->tempFile ){.
12545 20 20 2f 2a 20 53 65 63 74 6f 72 20 73 69 7a 65 /* Sector size
12546 20 64 6f 65 73 6e 27 74 20 6d 61 74 74 65 72 20 doesn't matter
12547 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 66 69 for temporary fi
12548 6c 65 73 2e 20 41 6c 73 6f 2c 20 74 68 65 20 66 les. Also, the f
12549 69 6c 65 0a 20 20 20 20 2a 2a 20 6d 61 79 20 6e ile. ** may n
1254a 6f 74 20 68 61 76 65 20 62 65 65 6e 20 6f 70 65 ot have been ope
1254b 6e 65 64 20 79 65 74 2c 20 69 6e 20 77 68 69 63 ned yet, in whic
1254c 68 20 63 61 73 65 20 74 68 65 20 4f 73 53 65 63 h case the OsSec
1254d 74 6f 72 53 69 7a 65 28 29 0a 20 20 20 20 2a 2a torSize(). **
1254e 20 63 61 6c 6c 20 77 69 6c 6c 20 73 65 67 66 61 call will segfa
1254f 75 6c 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ult.. */.
12550 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 pPager->sectorSi
12551 7a 65 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 65 ze = sqlite3OsSe
12552 63 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72 2d ctorSize(pPager-
12553 3e 66 64 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 >fd);. }. if(
12554 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 pPager->sectorSi
12555 7a 65 3c 35 31 32 20 29 7b 0a 20 20 20 20 70 50 ze<512 ){. pP
12556 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 ager->sectorSize
12557 20 3d 20 35 31 32 3b 0a 20 20 7d 0a 20 20 69 66 = 512;. }. if
12558 28 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 ( pPager->sector
12559 53 69 7a 65 3e 4d 41 58 5f 53 45 43 54 4f 52 5f Size>MAX_SECTOR_
1255a 53 49 5a 45 20 29 7b 0a 20 20 20 20 61 73 73 65 SIZE ){. asse
1255b 72 74 28 20 4d 41 58 5f 53 45 43 54 4f 52 5f 53 rt( MAX_SECTOR_S
1255c 49 5a 45 3e 3d 35 31 32 20 29 3b 0a 20 20 20 20 IZE>=512 );.
1255d 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 pPager->sectorSi
1255e 7a 65 20 3d 20 4d 41 58 5f 53 45 43 54 4f 52 5f ze = MAX_SECTOR_
1255f 53 49 5a 45 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a SIZE;. }.}../*.
12560 2a 2a 20 50 6c 61 79 62 61 63 6b 20 74 68 65 20 ** Playback the
12561 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74 68 75 73 journal and thus
12562 20 72 65 73 74 6f 72 65 20 74 68 65 20 64 61 74 restore the dat
12563 61 62 61 73 65 20 66 69 6c 65 20 74 6f 0a 2a 2a abase file to.**
12564 20 74 68 65 20 73 74 61 74 65 20 69 74 20 77 61 the state it wa
12565 73 20 69 6e 20 62 65 66 6f 72 65 20 77 65 20 73 s in before we s
12566 74 61 72 74 65 64 20 6d 61 6b 69 6e 67 20 63 68 tarted making ch
12567 61 6e 67 65 73 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 anges. .**.** T
12568 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
12569 66 6f 72 6d 61 74 20 69 73 20 61 73 20 66 6f 6c format is as fol
1256a 6c 6f 77 73 3a 20 0a 2a 2a 0a 2a 2a 20 20 28 31 lows: .**.** (1
1256b 29 20 20 38 20 62 79 74 65 20 70 72 65 66 69 78 ) 8 byte prefix
1256c 2e 20 20 41 20 63 6f 70 79 20 6f 66 20 61 4a 6f . A copy of aJo
1256d 75 72 6e 61 6c 4d 61 67 69 63 5b 5d 2e 0a 2a 2a urnalMagic[]..**
1256e 20 20 28 32 29 20 20 34 20 62 79 74 65 20 62 69 (2) 4 byte bi
1256f 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 g-endian integer
12570 20 77 68 69 63 68 20 69 73 20 74 68 65 20 6e 75 which is the nu
12571 6d 62 65 72 20 6f 66 20 76 61 6c 69 64 20 70 61 mber of valid pa
12572 67 65 20 72 65 63 6f 72 64 73 0a 2a 2a 20 20 20 ge records.**
12573 20 20 20 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e in the journ
12574 61 6c 2e 20 20 49 66 20 74 68 69 73 20 76 61 6c al. If this val
12575 75 65 20 69 73 20 30 78 66 66 66 66 66 66 66 66 ue is 0xffffffff
12576 2c 20 74 68 65 6e 20 63 6f 6d 70 75 74 65 20 74 , then compute t
12577 68 65 0a 2a 2a 20 20 20 20 20 20 20 6e 75 6d 62 he.** numb
12578 65 72 20 6f 66 20 70 61 67 65 20 72 65 63 6f 72 er of page recor
12579 64 73 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 ds from the jour
1257a 6e 61 6c 20 73 69 7a 65 2e 0a 2a 2a 20 20 28 33 nal size..** (3
1257b 29 20 20 34 20 62 79 74 65 20 62 69 67 2d 65 6e ) 4 byte big-en
1257c 64 69 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 dian integer whi
1257d 63 68 20 69 73 20 74 68 65 20 69 6e 69 74 69 61 ch is the initia
1257e 6c 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 l value for the
1257f 0a 2a 2a 20 20 20 20 20 20 20 73 61 6e 69 74 79 .** sanity
12580 20 63 68 65 63 6b 73 75 6d 2e 0a 2a 2a 20 20 28 checksum..** (
12581 34 29 20 20 34 20 62 79 74 65 20 69 6e 74 65 67 4) 4 byte integ
12582 65 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 er which is the
12583 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 number of pages
12584 74 6f 20 74 72 75 6e 63 61 74 65 20 74 68 65 0a to truncate the.
12585 2a 2a 20 20 20 20 20 20 20 64 61 74 61 62 61 73 ** databas
12586 65 20 74 6f 20 64 75 72 69 6e 67 20 61 20 72 6f e to during a ro
12587 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 20 28 35 29 20 llback..** (5)
12588 20 34 20 62 79 74 65 20 62 69 67 2d 65 6e 64 69 4 byte big-endi
12589 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 an integer which
1258a 20 69 73 20 74 68 65 20 73 65 63 74 6f 72 20 73 is the sector s
1258b 69 7a 65 2e 20 20 54 68 65 20 68 65 61 64 65 72 ize. The header
1258c 0a 2a 2a 20 20 20 20 20 20 20 69 73 20 74 68 69 .** is thi
1258d 73 20 6d 61 6e 79 20 62 79 74 65 73 20 69 6e 20 s many bytes in
1258e 73 69 7a 65 2e 0a 2a 2a 20 20 28 36 29 20 20 34 size..** (6) 4
1258f 20 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e byte big-endian
12590 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 integer which i
12591 73 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 2e s the page size.
12592 0a 2a 2a 20 20 28 37 29 20 20 7a 65 72 6f 20 70 .** (7) zero p
12593 61 64 64 69 6e 67 20 6f 75 74 20 74 6f 20 74 68 adding out to th
12594 65 20 6e 65 78 74 20 73 65 63 74 6f 72 20 73 69 e next sector si
12595 7a 65 2e 0a 2a 2a 20 20 28 38 29 20 20 5a 65 72 ze..** (8) Zer
12596 6f 20 6f 72 20 6d 6f 72 65 20 70 61 67 65 73 20 o or more pages
12597 69 6e 73 74 61 6e 63 65 73 2c 20 65 61 63 68 20 instances, each
12598 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 20 20 as follows:.**
12599 20 20 20 20 20 20 2b 20 20 34 20 62 79 74 65 20 + 4 byte
1259a 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 page number..**
1259b 20 20 20 20 20 20 20 2b 20 20 70 50 61 67 65 72 + pPager
1259c 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73 ->pageSize bytes
1259d 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 20 20 20 20 of data..**
1259e 20 20 20 20 2b 20 20 34 20 62 79 74 65 20 63 68 + 4 byte ch
1259f 65 63 6b 73 75 6d 0a 2a 2a 0a 2a 2a 20 57 68 65 ecksum.**.** Whe
125a0 6e 20 77 65 20 73 70 65 61 6b 20 6f 66 20 74 68 n we speak of th
125a1 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 e journal header
125a2 2c 20 77 65 20 6d 65 61 6e 20 74 68 65 20 66 69 , we mean the fi
125a3 72 73 74 20 37 20 69 74 65 6d 73 20 61 62 6f 76 rst 7 items abov
125a4 65 2e 0a 2a 2a 20 45 61 63 68 20 65 6e 74 72 79 e..** Each entry
125a5 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 in the journal
125a6 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f is an instance o
125a7 66 20 74 68 65 20 38 74 68 20 69 74 65 6d 2e 0a f the 8th item..
125a8 2a 2a 0a 2a 2a 20 43 61 6c 6c 20 74 68 65 20 76 **.** Call the v
125a9 61 6c 75 65 20 66 72 6f 6d 20 74 68 65 20 73 65 alue from the se
125aa 63 6f 6e 64 20 62 75 6c 6c 65 74 20 22 6e 52 65 cond bullet "nRe
125ab 63 22 2e 20 20 6e 52 65 63 20 69 73 20 74 68 65 c". nRec is the
125ac 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 76 61 number of.** va
125ad 6c 69 64 20 70 61 67 65 20 65 6e 74 72 69 65 73 lid page entries
125ae 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e in the journal.
125af 20 20 49 6e 20 6d 6f 73 74 20 63 61 73 65 73 2c In most cases,
125b0 20 79 6f 75 20 63 61 6e 20 63 6f 6d 70 75 74 65 you can compute
125b1 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 the.** value of
125b2 20 6e 52 65 63 20 66 72 6f 6d 20 74 68 65 20 73 nRec from the s
125b3 69 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e ize of the journ
125b4 61 6c 20 66 69 6c 65 2e 20 20 42 75 74 20 69 66 al file. But if
125b5 20 61 20 70 6f 77 65 72 0a 2a 2a 20 66 61 69 6c a power.** fail
125b6 75 72 65 20 6f 63 63 75 72 72 65 64 20 77 68 69 ure occurred whi
125b7 6c 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77 le the journal w
125b8 61 73 20 62 65 69 6e 67 20 77 72 69 74 74 65 6e as being written
125b9 2c 20 69 74 20 63 6f 75 6c 64 20 62 65 20 74 68 , it could be th
125ba 65 0a 2a 2a 20 63 61 73 65 20 74 68 61 74 20 74 e.** case that t
125bb 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6a he size of the j
125bc 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 64 20 ournal file had
125bd 61 6c 72 65 61 64 79 20 62 65 65 6e 20 69 6e 63 already been inc
125be 72 65 61 73 65 64 20 62 75 74 0a 2a 2a 20 74 68 reased but.** th
125bf 65 20 65 78 74 72 61 20 65 6e 74 72 69 65 73 20 e extra entries
125c0 68 61 64 20 6e 6f 74 20 79 65 74 20 6d 61 64 65 had not yet made
125c1 20 69 74 20 73 61 66 65 6c 79 20 74 6f 20 64 69 it safely to di
125c2 73 6b 2e 20 20 49 6e 20 73 75 63 68 20 61 20 63 sk. In such a c
125c3 61 73 65 2c 0a 2a 2a 20 74 68 65 20 76 61 6c 75 ase,.** the valu
125c4 65 20 6f 66 20 6e 52 65 63 20 63 6f 6d 70 75 74 e of nRec comput
125c5 65 64 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 ed from the file
125c6 20 73 69 7a 65 20 77 6f 75 6c 64 20 62 65 20 74 size would be t
125c7 6f 6f 20 6c 61 72 67 65 2e 20 20 46 6f 72 0a 2a oo large. For.*
125c8 2a 20 74 68 61 74 20 72 65 61 73 6f 6e 2c 20 77 * that reason, w
125c9 65 20 61 6c 77 61 79 73 20 75 73 65 20 74 68 65 e always use the
125ca 20 6e 52 65 63 20 76 61 6c 75 65 20 69 6e 20 74 nRec value in t
125cb 68 65 20 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a he header..**.**
125cc 20 49 66 20 74 68 65 20 6e 52 65 63 20 76 61 6c If the nRec val
125cd 75 65 20 69 73 20 30 78 66 66 66 66 66 66 66 66 ue is 0xffffffff
125ce 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 6e it means that n
125cf 52 65 63 20 73 68 6f 75 6c 64 20 62 65 20 63 6f Rec should be co
125d0 6d 70 75 74 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 mputed.** from t
125d1 68 65 20 66 69 6c 65 20 73 69 7a 65 2e 20 20 54 he file size. T
125d2 68 69 73 20 76 61 6c 75 65 20 69 73 20 75 73 65 his value is use
125d3 64 20 77 68 65 6e 20 74 68 65 20 75 73 65 72 20 d when the user
125d4 73 65 6c 65 63 74 73 20 74 68 65 0a 2a 2a 20 6e selects the.** n
125d5 6f 2d 73 79 6e 63 20 6f 70 74 69 6f 6e 20 66 6f o-sync option fo
125d6 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 r the journal.
125d7 41 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 A power failure
125d8 63 6f 75 6c 64 20 6c 65 61 64 20 74 6f 20 63 6f could lead to co
125d9 72 72 75 70 74 69 6f 6e 0a 2a 2a 20 69 6e 20 74 rruption.** in t
125da 68 69 73 20 63 61 73 65 2e 20 20 42 75 74 20 66 his case. But f
125db 6f 72 20 74 68 69 6e 67 73 20 6c 69 6b 65 20 74 or things like t
125dc 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 28 emporary table (
125dd 77 68 69 63 68 20 77 69 6c 6c 20 62 65 0a 2a 2a which will be.**
125de 20 64 65 6c 65 74 65 64 20 77 68 65 6e 20 74 68 deleted when th
125df 65 20 70 6f 77 65 72 20 69 73 20 72 65 73 74 6f e power is resto
125e0 72 65 64 29 20 77 65 20 64 6f 6e 27 74 20 63 61 red) we don't ca
125e1 72 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 re. .**.** If t
125e2 68 65 20 66 69 6c 65 20 6f 70 65 6e 65 64 20 61 he file opened a
125e3 73 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 s the journal fi
125e4 6c 65 20 69 73 20 6e 6f 74 20 61 20 77 65 6c 6c le is not a well
125e5 2d 66 6f 72 6d 65 64 0a 2a 2a 20 6a 6f 75 72 6e -formed.** journ
125e6 61 6c 20 66 69 6c 65 20 74 68 65 6e 20 61 6c 6c al file then all
125e7 20 70 61 67 65 73 20 75 70 20 74 6f 20 74 68 65 pages up to the
125e8 20 66 69 72 73 74 20 63 6f 72 72 75 70 74 65 64 first corrupted
125e9 20 70 61 67 65 20 61 72 65 20 72 6f 6c 6c 65 64 page are rolled
125ea 0a 2a 2a 20 62 61 63 6b 20 28 6f 72 20 6e 6f 20 .** back (or no
125eb 70 61 67 65 73 20 69 66 20 74 68 65 20 6a 6f 75 pages if the jou
125ec 72 6e 61 6c 20 68 65 61 64 65 72 20 69 73 20 63 rnal header is c
125ed 6f 72 72 75 70 74 65 64 29 2e 20 54 68 65 20 6a orrupted). The j
125ee 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 69 ournal file.** i
125ef 73 20 74 68 65 6e 20 64 65 6c 65 74 65 64 20 61 s then deleted a
125f0 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 nd SQLITE_OK ret
125f1 75 72 6e 65 64 2c 20 6a 75 73 74 20 61 73 20 69 urned, just as i
125f2 66 20 6e 6f 20 63 6f 72 72 75 70 74 69 6f 6e 20 f no corruption
125f3 68 61 64 0a 2a 2a 20 62 65 65 6e 20 65 6e 63 6f had.** been enco
125f4 75 6e 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 untered..**.** I
125f5 66 20 61 6e 20 49 2f 4f 20 6f 72 20 6d 61 6c 6c f an I/O or mall
125f6 6f 63 28 29 20 65 72 72 6f 72 20 6f 63 63 75 72 oc() error occur
125f7 73 2c 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 66 s, the journal-f
125f8 69 6c 65 20 69 73 20 6e 6f 74 20 64 65 6c 65 74 ile is not delet
125f9 65 64 0a 2a 2a 20 61 6e 64 20 61 6e 20 65 72 72 ed.** and an err
125fa 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 or code is retur
125fb 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 ned..**.** The i
125fc 73 48 6f 74 20 70 61 72 61 6d 65 74 65 72 20 69 sHot parameter i
125fd 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 77 65 ndicates that we
125fe 20 61 72 65 20 74 72 79 69 6e 67 20 74 6f 20 72 are trying to r
125ff 6f 6c 6c 62 61 63 6b 20 61 20 6a 6f 75 72 6e 61 ollback a journa
12600 6c 0a 2a 2a 20 74 68 61 74 20 6d 69 67 68 74 20 l.** that might
12601 62 65 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c be a hot journal
12602 2e 20 20 4f 72 2c 20 69 74 20 63 6f 75 6c 64 20 . Or, it could
12603 62 65 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 be that the jour
12604 6e 61 6c 20 69 73 20 0a 2a 2a 20 70 72 65 73 65 nal is .** prese
12605 72 76 65 64 20 62 65 63 61 75 73 65 20 6f 66 20 rved because of
12606 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 JOURNALMODE_PERS
12607 49 53 54 20 6f 72 20 4a 4f 55 52 4e 41 4c 4d 4f IST or JOURNALMO
12608 44 45 5f 54 52 55 4e 43 41 54 45 2e 0a 2a 2a 20 DE_TRUNCATE..**
12609 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 72 If the journal r
1260a 65 61 6c 6c 79 20 69 73 20 68 6f 74 2c 20 72 65 eally is hot, re
1260b 73 65 74 20 74 68 65 20 70 61 67 65 72 20 63 61 set the pager ca
1260c 63 68 65 20 70 72 69 6f 72 20 72 6f 6c 6c 69 6e che prior rollin
1260d 67 0a 2a 2a 20 62 61 63 6b 20 61 6e 79 20 63 6f g.** back any co
1260e 6e 74 65 6e 74 2e 20 20 49 66 20 74 68 65 20 6a ntent. If the j
1260f 6f 75 72 6e 61 6c 20 69 73 20 6d 65 72 65 6c 79 ournal is merely
12610 20 70 65 72 73 69 73 74 65 6e 74 2c 20 6e 6f 20 persistent, no
12611 72 65 73 65 74 20 69 73 0a 2a 2a 20 6e 65 65 64 reset is.** need
12612 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ed..*/.static in
12613 74 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b t pager_playback
12614 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
12615 69 6e 74 20 69 73 48 6f 74 29 7b 0a 20 20 73 71 int isHot){. sq
12616 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 20 lite3_vfs *pVfs
12617 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a = pPager->pVfs;.
12618 20 20 69 36 34 20 73 7a 4a 3b 20 20 20 20 20 20 i64 szJ;
12619 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
1261a 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 ze of the journa
1261b 6c 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 20 l file in bytes
1261c 2a 2f 0a 20 20 75 33 32 20 6e 52 65 63 3b 20 20 */. u32 nRec;
1261d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1261e 20 4e 75 6d 62 65 72 20 6f 66 20 52 65 63 6f 72 Number of Recor
1261f 64 73 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 ds in the journa
12620 6c 20 2a 2f 0a 20 20 75 33 32 20 75 3b 20 20 20 l */. u32 u;
12621 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12622 2f 2a 20 55 6e 73 69 67 6e 65 64 20 6c 6f 6f 70 /* Unsigned loop
12623 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 50 67 counter */. Pg
12624 6e 6f 20 6d 78 50 67 20 3d 20 30 3b 20 20 20 20 no mxPg = 0;
12625 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
12626 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 66 f the original f
12627 69 6c 65 20 69 6e 20 70 61 67 65 73 20 2a 2f 0a ile in pages */.
12628 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 int rc;
12629 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
1262a 73 75 6c 74 20 63 6f 64 65 20 6f 66 20 61 20 73 sult code of a s
1262b 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 ubroutine */. i
1262c 6e 74 20 72 65 73 20 3d 20 31 3b 20 20 20 20 20 nt res = 1;
1262d 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 /* Value
1262e 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c returned by sql
1262f 69 74 65 33 4f 73 41 63 63 65 73 73 28 29 20 2a ite3OsAccess() *
12630 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 /. char *zMaste
12631 72 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 r = 0; /*
12632 4e 61 6d 65 20 6f 66 20 6d 61 73 74 65 72 20 6a Name of master j
12633 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 66 20 61 ournal file if a
12634 6e 79 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 65 64 ny */. int need
12635 50 61 67 65 72 52 65 73 65 74 3b 20 20 20 20 20 PagerReset;
12636 20 2f 2a 20 54 72 75 65 20 74 6f 20 72 65 73 65 /* True to rese
12637 74 20 70 61 67 65 20 70 72 69 6f 72 20 74 6f 20 t page prior to
12638 66 69 72 73 74 20 70 61 67 65 20 72 6f 6c 6c 62 first page rollb
12639 61 63 6b 20 2a 2f 0a 0a 20 20 2f 2a 20 46 69 67 ack */.. /* Fig
1263a 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 ure out how many
1263b 20 72 65 63 6f 72 64 73 20 61 72 65 20 69 6e 20 records are in
1263c 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 41 62 the journal. Ab
1263d 6f 72 74 20 65 61 72 6c 79 20 69 66 0a 20 20 2a ort early if. *
1263e 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 * the journal is
1263f 20 65 6d 70 74 79 2e 0a 20 20 2a 2f 0a 20 20 61 empty.. */. a
12640 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 ssert( isOpen(pP
12641 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 20 20 ager->jfd) );.
12642 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 rc = sqlite3OsFi
12643 6c 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 6a leSize(pPager->j
12644 66 64 2c 20 26 73 7a 4a 29 3b 0a 20 20 69 66 28 fd, &szJ);. if(
12645 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c rc!=SQLITE_OK |
12646 7c 20 73 7a 4a 3d 3d 30 20 29 7b 0a 20 20 20 20 | szJ==0 ){.
12647 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 goto end_playbac
12648 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 k;. }.. /* Rea
12649 64 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 d the master jou
1264a 72 6e 61 6c 20 6e 61 6d 65 20 66 72 6f 6d 20 74 rnal name from t
1264b 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 69 66 20 69 he journal, if i
1264c 74 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 t is present..
1264d 2a 2a 20 49 66 20 61 20 6d 61 73 74 65 72 20 6a ** If a master j
1264e 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 ournal file name
1264f 20 69 73 20 73 70 65 63 69 66 69 65 64 2c 20 62 is specified, b
12650 75 74 20 74 68 65 20 66 69 6c 65 20 69 73 20 6e ut the file is n
12651 6f 74 0a 20 20 2a 2a 20 70 72 65 73 65 6e 74 20 ot. ** present
12652 6f 6e 20 64 69 73 6b 2c 20 74 68 65 6e 20 74 68 on disk, then th
12653 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6e 6f 74 e journal is not
12654 20 68 6f 74 20 61 6e 64 20 64 6f 65 73 20 6e 6f hot and does no
12655 74 20 6e 65 65 64 20 74 6f 20 62 65 0a 20 20 2a t need to be. *
12656 2a 20 70 6c 61 79 65 64 20 62 61 63 6b 2e 0a 20 * played back..
12657 20 2a 2a 0a 20 20 2a 2a 20 54 4f 44 4f 3a 20 54 **. ** TODO: T
12658 65 63 68 6e 69 63 61 6c 6c 79 20 74 68 65 20 66 echnically the f
12659 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 61 6e 20 65 ollowing is an e
1265a 72 72 6f 72 20 62 65 63 61 75 73 65 20 69 74 20 rror because it
1265b 61 73 73 75 6d 65 73 20 74 68 61 74 0a 20 20 2a assumes that. *
1265c 2a 20 62 75 66 66 65 72 20 50 61 67 65 72 2e 70 * buffer Pager.p
1265d 54 6d 70 53 70 61 63 65 20 69 73 20 28 6d 78 50 TmpSpace is (mxP
1265e 61 74 68 6e 61 6d 65 2b 31 29 20 62 79 74 65 73 athname+1) bytes
1265f 20 6f 72 20 6c 61 72 67 65 72 2e 20 69 2e 65 2e or larger. i.e.
12660 20 74 68 61 74 0a 20 20 2a 2a 20 28 70 50 61 67 that. ** (pPag
12661 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 3e 3d 20 er->pageSize >=
12662 70 50 61 67 65 72 2d 3e 70 56 66 73 2d 3e 6d 78 pPager->pVfs->mx
12663 50 61 74 68 6e 61 6d 65 2b 31 29 2e 20 55 73 69 Pathname+1). Usi
12664 6e 67 20 6f 73 5f 75 6e 69 78 2e 63 2c 0a 20 20 ng os_unix.c,.
12665 2a 2a 20 20 6d 78 50 61 74 68 6e 61 6d 65 20 69 ** mxPathname i
12666 73 20 35 31 32 2c 20 77 68 69 63 68 20 69 73 20 s 512, which is
12667 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 the same as the
12668 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 77 61 62 6c minimum allowabl
12669 65 20 76 61 6c 75 65 0a 20 20 2a 2a 20 66 6f 72 e value. ** for
1266a 20 70 61 67 65 53 69 7a 65 2e 0a 20 20 2a 2f 0a pageSize.. */.
1266b 20 20 7a 4d 61 73 74 65 72 20 3d 20 70 50 61 67 zMaster = pPag
1266c 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 er->pTmpSpace;.
1266d 20 72 63 20 3d 20 72 65 61 64 4d 61 73 74 65 72 rc = readMaster
1266e 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e Journal(pPager->
1266f 6a 66 64 2c 20 7a 4d 61 73 74 65 72 2c 20 70 50 jfd, zMaster, pP
12670 61 67 65 72 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 ager->pVfs->mxPa
12671 74 68 6e 61 6d 65 2b 31 29 3b 0a 20 20 69 66 28 thname+1);. if(
12672 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
12673 26 20 7a 4d 61 73 74 65 72 5b 30 5d 20 29 7b 0a & zMaster[0] ){.
12674 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
12675 4f 73 41 63 63 65 73 73 28 70 56 66 73 2c 20 7a OsAccess(pVfs, z
12676 4d 61 73 74 65 72 2c 20 53 51 4c 49 54 45 5f 41 Master, SQLITE_A
12677 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20 26 72 CCESS_EXISTS, &r
12678 65 73 29 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73 74 es);. }. zMast
12679 65 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 72 63 er = 0;. if( rc
1267a 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 21 !=SQLITE_OK || !
1267b 72 65 73 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 res ){. goto
1267c 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 end_playback;.
1267d 7d 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 }. pPager->jour
1267e 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 6e 65 nalOff = 0;. ne
1267f 65 64 50 61 67 65 72 52 65 73 65 74 20 3d 20 69 edPagerReset = i
12680 73 48 6f 74 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 sHot;.. /* This
12681 20 6c 6f 6f 70 20 74 65 72 6d 69 6e 61 74 65 73 loop terminates
12682 20 65 69 74 68 65 72 20 77 68 65 6e 20 61 20 72 either when a r
12683 65 61 64 4a 6f 75 72 6e 61 6c 48 64 72 28 29 20 eadJournalHdr()
12684 6f 72 20 0a 20 20 2a 2a 20 70 61 67 65 72 5f 70 or . ** pager_p
12685 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 layback_one_page
12686 28 29 20 63 61 6c 6c 20 72 65 74 75 72 6e 73 20 () call returns
12687 53 51 4c 49 54 45 5f 44 4f 4e 45 20 6f 72 20 61 SQLITE_DONE or a
12688 6e 20 49 4f 20 65 72 72 6f 72 20 0a 20 20 2a 2a n IO error . **
12689 20 6f 63 63 75 72 73 2e 20 0a 20 20 2a 2f 0a 20 occurs. . */.
1268a 20 77 68 69 6c 65 28 20 31 20 29 7b 0a 20 20 20 while( 1 ){.
1268b 20 69 6e 74 20 69 73 55 6e 73 79 6e 63 20 3d 20 int isUnsync =
1268c 30 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 0;.. /* Read
1268d 74 68 65 20 6e 65 78 74 20 6a 6f 75 72 6e 61 6c the next journal
1268e 20 68 65 61 64 65 72 20 66 72 6f 6d 20 74 68 65 header from the
1268f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20 journal file.
12690 49 66 20 74 68 65 72 65 20 61 72 65 0a 20 20 20 If there are.
12691 20 2a 2a 20 6e 6f 74 20 65 6e 6f 75 67 68 20 62 ** not enough b
12692 79 74 65 73 20 6c 65 66 74 20 69 6e 20 74 68 65 ytes left in the
12693 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f journal file fo
12694 72 20 61 20 63 6f 6d 70 6c 65 74 65 20 68 65 61 r a complete hea
12695 64 65 72 2c 20 6f 72 0a 20 20 20 20 2a 2a 20 69 der, or. ** i
12696 74 20 69 73 20 63 6f 72 72 75 70 74 65 64 2c 20 t is corrupted,
12697 74 68 65 6e 20 61 20 70 72 6f 63 65 73 73 20 6d then a process m
12698 75 73 74 20 6f 66 20 66 61 69 6c 65 64 20 77 68 ust of failed wh
12699 69 6c 65 20 77 72 69 74 69 6e 67 20 69 74 2e 0a ile writing it..
1269a 20 20 20 20 2a 2a 20 54 68 69 73 20 69 6e 64 69 ** This indi
1269b 63 61 74 65 73 20 6e 6f 74 68 69 6e 67 20 6d 6f cates nothing mo
1269c 72 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 re needs to be r
1269d 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 20 20 20 20 olled back..
1269e 2a 2f 0a 20 20 20 20 72 63 20 3d 20 72 65 61 64 */. rc = read
1269f 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65 JournalHdr(pPage
126a0 72 2c 20 69 73 48 6f 74 2c 20 73 7a 4a 2c 20 26 r, isHot, szJ, &
126a1 6e 52 65 63 2c 20 26 6d 78 50 67 29 3b 0a 20 20 nRec, &mxPg);.
126a2 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
126a3 5f 4f 4b 20 29 7b 20 0a 20 20 20 20 20 20 69 66 _OK ){ . if
126a4 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e ( rc==SQLITE_DON
126a5 45 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 E ){. rc
126a6 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
126a7 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f 20 }. goto
126a8 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 end_playback;.
126a9 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 6e }.. /* If n
126aa 52 65 63 20 69 73 20 30 78 66 66 66 66 66 66 66 Rec is 0xfffffff
126ab 66 2c 20 74 68 65 6e 20 74 68 69 73 20 6a 6f 75 f, then this jou
126ac 72 6e 61 6c 20 77 61 73 20 63 72 65 61 74 65 64 rnal was created
126ad 20 62 79 20 61 20 70 72 6f 63 65 73 73 0a 20 20 by a process.
126ae 20 20 2a 2a 20 77 6f 72 6b 69 6e 67 20 69 6e 20 ** working in
126af 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 2e 20 54 68 no-sync mode. Th
126b0 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 is means that th
126b1 65 20 72 65 73 74 20 6f 66 20 74 68 65 20 6a 6f e rest of the jo
126b2 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 66 69 6c urnal. ** fil
126b3 65 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 70 61 e consists of pa
126b4 67 65 73 2c 20 74 68 65 72 65 20 61 72 65 20 6e ges, there are n
126b5 6f 20 6d 6f 72 65 20 6a 6f 75 72 6e 61 6c 20 68 o more journal h
126b6 65 61 64 65 72 73 2e 20 43 6f 6d 70 75 74 65 0a eaders. Compute.
126b7 20 20 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 ** the value
126b8 20 6f 66 20 6e 52 65 63 20 62 61 73 65 64 20 6f of nRec based o
126b9 6e 20 74 68 69 73 20 61 73 73 75 6d 70 74 69 6f n this assumptio
126ba 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 n.. */. if
126bb 28 20 6e 52 65 63 3d 3d 30 78 66 66 66 66 66 66 ( nRec==0xffffff
126bc 66 66 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 ff ){. asse
126bd 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 rt( pPager->jour
126be 6e 61 6c 4f 66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f nalOff==JOURNAL_
126bf 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 29 HDR_SZ(pPager) )
126c0 3b 0a 20 20 20 20 20 20 6e 52 65 63 20 3d 20 28 ;. nRec = (
126c1 69 6e 74 29 28 28 73 7a 4a 20 2d 20 4a 4f 55 52 int)((szJ - JOUR
126c2 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 NAL_HDR_SZ(pPage
126c3 72 29 29 2f 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 r))/JOURNAL_PG_S
126c4 5a 28 70 50 61 67 65 72 29 29 3b 0a 20 20 20 20 Z(pPager));.
126c5 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 6e 52 65 }.. /* If nRe
126c6 63 20 69 73 20 30 20 61 6e 64 20 74 68 69 73 20 c is 0 and this
126c7 72 6f 6c 6c 62 61 63 6b 20 69 73 20 6f 66 20 61 rollback is of a
126c8 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 72 65 transaction cre
126c9 61 74 65 64 20 62 79 20 74 68 69 73 0a 20 20 20 ated by this.
126ca 20 2a 2a 20 70 72 6f 63 65 73 73 20 61 6e 64 20 ** process and
126cb 69 66 20 74 68 69 73 20 69 73 20 74 68 65 20 66 if this is the f
126cc 69 6e 61 6c 20 68 65 61 64 65 72 20 69 6e 20 74 inal header in t
126cd 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 65 6e he journal, then
126ce 20 69 74 20 6d 65 61 6e 73 0a 20 20 20 20 2a 2a it means. **
126cf 20 74 68 61 74 20 74 68 69 73 20 70 61 72 74 20 that this part
126d0 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77 of the journal w
126d1 61 73 20 62 65 69 6e 67 20 66 69 6c 6c 65 64 20 as being filled
126d2 62 75 74 20 68 61 73 20 6e 6f 74 20 79 65 74 20 but has not yet
126d3 62 65 65 6e 0a 20 20 20 20 2a 2a 20 73 79 6e 63 been. ** sync
126d4 65 64 20 74 6f 20 64 69 73 6b 2e 20 20 43 6f 6d ed to disk. Com
126d5 70 75 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 pute the number
126d6 6f 66 20 70 61 67 65 73 20 62 61 73 65 64 20 6f of pages based o
126d7 6e 20 74 68 65 20 72 65 6d 61 69 6e 69 6e 67 0a n the remaining.
126d8 20 20 20 20 2a 2a 20 73 69 7a 65 20 6f 66 20 74 ** size of t
126d9 68 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a he file.. **.
126da 20 20 20 20 2a 2a 20 54 68 65 20 74 68 69 72 64 ** The third
126db 20 74 65 72 6d 20 6f 66 20 74 68 65 20 74 65 73 term of the tes
126dc 74 20 77 61 73 20 61 64 64 65 64 20 74 6f 20 66 t was added to f
126dd 69 78 20 74 69 63 6b 65 74 20 23 32 35 36 35 2e ix ticket #2565.
126de 0a 20 20 20 20 2a 2a 20 57 68 65 6e 20 72 6f 6c . ** When rol
126df 6c 69 6e 67 20 62 61 63 6b 20 61 20 68 6f 74 20 ling back a hot
126e0 6a 6f 75 72 6e 61 6c 2c 20 6e 52 65 63 3d 3d 30 journal, nRec==0
126e1 20 61 6c 77 61 79 73 20 6d 65 61 6e 73 20 74 68 always means th
126e2 61 74 20 74 68 65 20 6e 65 78 74 0a 20 20 20 20 at the next.
126e3 2a 2a 20 63 68 75 6e 6b 20 6f 66 20 74 68 65 20 ** chunk of the
126e4 6a 6f 75 72 6e 61 6c 20 63 6f 6e 74 61 69 6e 73 journal contains
126e5 20 7a 65 72 6f 20 70 61 67 65 73 20 74 6f 20 62 zero pages to b
126e6 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 20 e rolled back.
126e7 42 75 74 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20 But. ** when
126e8 64 6f 69 6e 67 20 61 20 52 4f 4c 4c 42 41 43 4b doing a ROLLBACK
126e9 20 61 6e 64 20 74 68 65 20 6e 52 65 63 3d 3d 30 and the nRec==0
126ea 20 63 68 75 6e 6b 20 69 73 20 74 68 65 20 6c 61 chunk is the la
126eb 73 74 20 63 68 75 6e 6b 20 69 6e 0a 20 20 20 20 st chunk in.
126ec 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 ** the journal,
126ed 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 it means that th
126ee 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 68 74 20 e journal might
126ef 63 6f 6e 74 61 69 6e 20 61 64 64 69 74 69 6f 6e contain addition
126f0 61 6c 0a 20 20 20 20 2a 2a 20 70 61 67 65 73 20 al. ** pages
126f1 74 68 61 74 20 6e 65 65 64 20 74 6f 20 62 65 20 that need to be
126f2 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 6e 64 20 rolled back and
126f3 74 68 61 74 20 74 68 65 20 6e 75 6d 62 65 72 20 that the number
126f4 6f 66 20 70 61 67 65 73 20 0a 20 20 20 20 2a 2a of pages . **
126f5 20 73 68 6f 75 6c 64 20 62 65 20 63 6f 6d 70 75 should be compu
126f6 74 65 64 20 62 61 73 65 64 20 6f 6e 20 74 68 65 ted based on the
126f7 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73 69 journal file si
126f8 7a 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ze.. */. i
126f9 66 28 20 6e 52 65 63 3d 3d 30 20 26 26 20 21 69 f( nRec==0 && !i
126fa 73 48 6f 74 20 26 26 0a 20 20 20 20 20 20 20 20 sHot &&.
126fb 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 pPager->journalH
126fc 64 72 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 dr+JOURNAL_HDR_S
126fd 5a 28 70 50 61 67 65 72 29 3d 3d 70 50 61 67 65 Z(pPager)==pPage
126fe 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 29 7b r->journalOff ){
126ff 0a 20 20 20 20 20 20 6e 52 65 63 20 3d 20 28 69 . nRec = (i
12700 6e 74 29 28 28 73 7a 4a 20 2d 20 70 50 61 67 65 nt)((szJ - pPage
12701 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 20 2f r->journalOff) /
12702 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 JOURNAL_PG_SZ(p
12703 50 61 67 65 72 29 29 3b 0a 20 20 20 20 20 20 69 Pager));. i
12704 73 55 6e 73 79 6e 63 20 3d 20 31 3b 0a 20 20 20 sUnsync = 1;.
12705 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 }.. /* If th
12706 69 73 20 69 73 20 74 68 65 20 66 69 72 73 74 20 is is the first
12707 68 65 61 64 65 72 20 72 65 61 64 20 66 72 6f 6d header read from
12708 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 74 72 the journal, tr
12709 75 6e 63 61 74 65 20 74 68 65 0a 20 20 20 20 2a uncate the. *
1270a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 * database file
1270b 62 61 63 6b 20 74 6f 20 69 74 73 20 6f 72 69 67 back to its orig
1270c 69 6e 61 6c 20 73 69 7a 65 2e 0a 20 20 20 20 2a inal size.. *
1270d 2f 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 /. if( pPager
1270e 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 4a 4f ->journalOff==JO
1270f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 URNAL_HDR_SZ(pPa
12710 67 65 72 29 20 29 7b 0a 20 20 20 20 20 20 72 63 ger) ){. rc
12711 20 3d 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74 = pager_truncat
12712 65 28 70 50 61 67 65 72 2c 20 6d 78 50 67 29 3b e(pPager, mxPg);
12713 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
12714 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
12715 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 goto end_pla
12716 79 62 61 63 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 yback;. }.
12717 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 pPager->dbS
12718 69 7a 65 20 3d 20 6d 78 50 67 3b 0a 20 20 20 20 ize = mxPg;.
12719 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f }.. /* Copy o
1271a 72 69 67 69 6e 61 6c 20 70 61 67 65 73 20 6f 75 riginal pages ou
1271b 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c t of the journal
1271c 20 61 6e 64 20 62 61 63 6b 20 69 6e 74 6f 20 74 and back into t
1271d 68 65 20 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 he . ** datab
1271e 61 73 65 20 66 69 6c 65 20 61 6e 64 2f 6f 72 20 ase file and/or
1271f 70 61 67 65 20 63 61 63 68 65 2e 0a 20 20 20 20 page cache..
12720 2a 2f 0a 20 20 20 20 66 6f 72 28 75 3d 30 3b 20 */. for(u=0;
12721 75 3c 6e 52 65 63 3b 20 75 2b 2b 29 7b 0a 20 20 u<nRec; u++){.
12722 20 20 20 20 69 66 28 20 6e 65 65 64 50 61 67 65 if( needPage
12723 72 52 65 73 65 74 20 29 7b 0a 20 20 20 20 20 20 rReset ){.
12724 20 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 50 pager_reset(pP
12725 61 67 65 72 29 3b 0a 20 20 20 20 20 20 20 20 6e ager);. n
12726 65 65 64 50 61 67 65 72 52 65 73 65 74 20 3d 20 eedPagerReset =
12727 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 0;. }.
12728 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 rc = pager_play
12729 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50 back_one_page(pP
1272a 61 67 65 72 2c 31 2c 69 73 55 6e 73 79 6e 63 2c ager,1,isUnsync,
1272b 26 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c &pPager->journal
1272c 4f 66 66 2c 30 2c 30 29 3b 0a 20 20 20 20 20 20 Off,0,0);.
1272d 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
1272e 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 K ){. if(
1272f 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 rc==SQLITE_DONE
12730 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 ){. rc
12731 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
12732 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e pPager->
12733 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 73 7a 4a journalOff = szJ
12734 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 ;. brea
12735 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 k;. }else
12736 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 {. /* I
12737 66 20 77 65 20 61 72 65 20 75 6e 61 62 6c 65 20 f we are unable
12738 74 6f 20 72 6f 6c 6c 62 61 63 6b 2c 20 71 75 69 to rollback, qui
12739 74 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 t and return the
1273a 20 65 72 72 6f 72 0a 20 20 20 20 20 20 20 20 20 error.
1273b 20 2a 2a 20 63 6f 64 65 2e 20 20 54 68 69 73 20 ** code. This
1273c 77 69 6c 6c 20 63 61 75 73 65 20 74 68 65 20 70 will cause the p
1273d 61 67 65 72 20 74 6f 20 65 6e 74 65 72 20 74 68 ager to enter th
1273e 65 20 65 72 72 6f 72 20 73 74 61 74 65 0a 20 20 e error state.
1273f 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 20 74 68 ** so th
12740 61 74 20 6e 6f 20 66 75 72 74 68 65 72 20 68 61 at no further ha
12741 72 6d 20 77 69 6c 6c 20 62 65 20 64 6f 6e 65 2e rm will be done.
12742 20 20 50 65 72 68 61 70 73 20 74 68 65 20 6e 65 Perhaps the ne
12743 78 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 xt. **
12744 70 72 6f 63 65 73 73 20 74 6f 20 63 6f 6d 65 20 process to come
12745 61 6c 6f 6e 67 20 77 69 6c 6c 20 62 65 20 61 62 along will be ab
12746 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 le to rollback t
12747 68 65 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20 he database..
12748 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
12749 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 goto end_pla
1274a 79 62 61 63 6b 3b 0a 20 20 20 20 20 20 20 20 7d yback;. }
1274b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
1274c 20 7d 0a 20 20 2f 2a 4e 4f 54 52 45 41 43 48 45 }. /*NOTREACHE
1274d 44 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 30 20 D*/. assert( 0
1274e 29 3b 0a 0a 65 6e 64 5f 70 6c 61 79 62 61 63 6b );..end_playback
1274f 3a 0a 20 20 2f 2a 20 46 6f 6c 6c 6f 77 69 6e 67 :. /* Following
12750 20 61 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 a rollback, the
12751 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 73 database file s
12752 68 6f 75 6c 64 20 62 65 20 62 61 63 6b 20 69 6e hould be back in
12753 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 0a 20 20 its original.
12754 2a 2a 20 73 74 61 74 65 20 70 72 69 6f 72 20 74 ** state prior t
12755 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 o the start of t
12756 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 he transaction,
12757 73 6f 20 69 6e 76 6f 6b 65 20 74 68 65 0a 20 20 so invoke the.
12758 2a 2a 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f ** SQLITE_FCNTL_
12759 44 42 5f 55 4e 43 48 41 4e 47 45 44 20 66 69 6c DB_UNCHANGED fil
1275a 65 2d 63 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 e-control method
1275b 20 74 6f 20 64 69 73 61 62 6c 65 20 74 68 65 0a to disable the.
1275c 20 20 2a 2a 20 61 73 73 65 72 74 69 6f 6e 20 74 ** assertion t
1275d 68 61 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 hat the transact
1275e 69 6f 6e 20 63 6f 75 6e 74 65 72 20 77 61 73 20 ion counter was
1275f 6d 6f 64 69 66 69 65 64 2e 0a 20 20 2a 2f 0a 20 modified.. */.
12760 20 61 73 73 65 72 74 28 0a 20 20 20 20 70 50 61 assert(. pPa
12761 67 65 72 2d 3e 66 64 2d 3e 70 4d 65 74 68 6f 64 ger->fd->pMethod
12762 73 3d 3d 30 20 7c 7c 0a 20 20 20 20 73 71 6c 69 s==0 ||. sqli
12763 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c te3OsFileControl
12764 28 70 50 61 67 65 72 2d 3e 66 64 2c 53 51 4c 49 (pPager->fd,SQLI
12765 54 45 5f 46 43 4e 54 4c 5f 44 42 5f 55 4e 43 48 TE_FCNTL_DB_UNCH
12766 41 4e 47 45 44 2c 30 29 3e 3d 53 51 4c 49 54 45 ANGED,0)>=SQLITE
12767 5f 4f 4b 0a 20 20 29 3b 0a 0a 20 20 2f 2a 20 49 _OK. );.. /* I
12768 66 20 74 68 69 73 20 70 6c 61 79 62 61 63 6b 20 f this playback
12769 69 73 20 68 61 70 70 65 6e 69 6e 67 20 61 75 74 is happening aut
1276a 6f 6d 61 74 69 63 61 6c 6c 79 20 61 73 20 61 20 omatically as a
1276b 72 65 73 75 6c 74 20 6f 66 20 61 6e 20 49 4f 20 result of an IO
1276c 6f 72 20 0a 20 20 2a 2a 20 6d 61 6c 6c 6f 63 20 or . ** malloc
1276d 65 72 72 6f 72 20 74 68 61 74 20 6f 63 63 75 72 error that occur
1276e 72 65 64 20 61 66 74 65 72 20 74 68 65 20 63 68 red after the ch
1276f 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 77 61 73 ange-counter was
12770 20 75 70 64 61 74 65 64 20 62 75 74 20 0a 20 20 updated but .
12771 2a 2a 20 62 65 66 6f 72 65 20 74 68 65 20 74 72 ** before the tr
12772 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 63 6f ansaction was co
12773 6d 6d 69 74 74 65 64 2c 20 74 68 65 6e 20 74 68 mmitted, then th
12774 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 e change-counter
12775 20 0a 20 20 2a 2a 20 6d 6f 64 69 66 69 63 61 74 . ** modificat
12776 69 6f 6e 20 6d 61 79 20 6a 75 73 74 20 68 61 76 ion may just hav
12777 65 20 62 65 65 6e 20 72 65 76 65 72 74 65 64 2e e been reverted.
12778 20 49 66 20 74 68 69 73 20 68 61 70 70 65 6e 73 If this happens
12779 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 0a 20 in exclusive .
1277a 20 2a 2a 20 6d 6f 64 65 2c 20 74 68 65 6e 20 73 ** mode, then s
1277b 75 62 73 65 71 75 65 6e 74 20 74 72 61 6e 73 61 ubsequent transa
1277c 63 74 69 6f 6e 73 20 70 65 72 66 6f 72 6d 65 64 ctions performed
1277d 20 62 79 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 by the connecti
1277e 6f 6e 20 77 69 6c 6c 20 6e 6f 74 0a 20 20 2a 2a on will not. **
1277f 20 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e update the chan
12780 67 65 2d 63 6f 75 6e 74 65 72 20 61 74 20 61 6c ge-counter at al
12781 6c 2e 20 54 68 69 73 20 6d 61 79 20 6c 65 61 64 l. This may lead
12782 20 74 6f 20 63 61 63 68 65 20 69 6e 63 6f 6e 73 to cache incons
12783 69 73 74 65 6e 63 79 0a 20 20 2a 2a 20 70 72 6f istency. ** pro
12784 62 6c 65 6d 73 20 66 6f 72 20 6f 74 68 65 72 20 blems for other
12785 70 72 6f 63 65 73 73 65 73 20 61 74 20 73 6f 6d processes at som
12786 65 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 66 e point in the f
12787 75 74 75 72 65 2e 20 53 6f 2c 20 6a 75 73 74 0a uture. So, just.
12788 20 20 2a 2a 20 69 6e 20 63 61 73 65 20 74 68 69 ** in case thi
12789 73 20 68 61 73 20 68 61 70 70 65 6e 65 64 2c 20 s has happened,
1278a 63 6c 65 61 72 20 74 68 65 20 63 68 61 6e 67 65 clear the change
1278b 43 6f 75 6e 74 44 6f 6e 65 20 66 6c 61 67 20 6e CountDone flag n
1278c 6f 77 2e 0a 20 20 2a 2f 0a 20 20 70 50 61 67 65 ow.. */. pPage
1278d 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f r->changeCountDo
1278e 6e 65 20 3d 20 70 50 61 67 65 72 2d 3e 74 65 6d ne = pPager->tem
1278f 70 46 69 6c 65 3b 0a 0a 20 20 69 66 28 20 72 63 pFile;.. if( rc
12790 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
12791 20 20 20 7a 4d 61 73 74 65 72 20 3d 20 70 50 61 zMaster = pPa
12792 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a ger->pTmpSpace;.
12793 20 20 20 20 72 63 20 3d 20 72 65 61 64 4d 61 73 rc = readMas
12794 74 65 72 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 terJournal(pPage
12795 72 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74 65 72 2c r->jfd, zMaster,
12796 20 70 50 61 67 65 72 2d 3e 70 56 66 73 2d 3e 6d pPager->pVfs->m
12797 78 50 61 74 68 6e 61 6d 65 2b 31 29 3b 0a 20 20 xPathname+1);.
12798 20 20 74 65 73 74 63 61 73 65 28 20 72 63 21 3d testcase( rc!=
12799 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d SQLITE_OK );. }
1279a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
1279b 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d E_OK ){. rc =
1279c 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 pager_end_trans
1279d 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c 20 7a action(pPager, z
1279e 4d 61 73 74 65 72 5b 30 5d 21 3d 27 5c 30 27 29 Master[0]!='\0')
1279f 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 ;. testcase(
127a0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b rc!=SQLITE_OK );
127a1 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 . }. if( rc==S
127a2 51 4c 49 54 45 5f 4f 4b 20 26 26 20 7a 4d 61 73 QLITE_OK && zMas
127a3 74 65 72 5b 30 5d 20 26 26 20 72 65 73 20 29 7b ter[0] && res ){
127a4 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 . /* If there
127a5 20 77 61 73 20 61 20 6d 61 73 74 65 72 20 6a 6f was a master jo
127a6 75 72 6e 61 6c 20 61 6e 64 20 74 68 69 73 20 72 urnal and this r
127a7 6f 75 74 69 6e 65 20 77 69 6c 6c 20 72 65 74 75 outine will retu
127a8 72 6e 20 73 75 63 63 65 73 73 2c 0a 20 20 20 20 rn success,.
127a9 2a 2a 20 73 65 65 20 69 66 20 69 74 20 69 73 20 ** see if it is
127aa 70 6f 73 73 69 62 6c 65 20 74 6f 20 64 65 6c 65 possible to dele
127ab 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f te the master jo
127ac 75 72 6e 61 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 urnal.. */.
127ad 20 20 72 63 20 3d 20 70 61 67 65 72 5f 64 65 6c rc = pager_del
127ae 6d 61 73 74 65 72 28 70 50 61 67 65 72 2c 20 7a master(pPager, z
127af 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 74 65 73 Master);. tes
127b0 74 63 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54 tcase( rc!=SQLIT
127b1 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f E_OK );. }.. /
127b2 2a 20 54 68 65 20 50 61 67 65 72 2e 73 65 63 74 * The Pager.sect
127b3 6f 72 53 69 7a 65 20 76 61 72 69 61 62 6c 65 20 orSize variable
127b4 6d 61 79 20 68 61 76 65 20 62 65 65 6e 20 75 70 may have been up
127b5 64 61 74 65 64 20 77 68 69 6c 65 20 72 6f 6c 6c dated while roll
127b6 69 6e 67 0a 20 20 2a 2a 20 62 61 63 6b 20 61 20 ing. ** back a
127b7 6a 6f 75 72 6e 61 6c 20 63 72 65 61 74 65 64 20 journal created
127b8 62 79 20 61 20 70 72 6f 63 65 73 73 20 77 69 74 by a process wit
127b9 68 20 61 20 64 69 66 66 65 72 65 6e 74 20 73 65 h a different se
127ba 63 74 6f 72 20 73 69 7a 65 0a 20 20 2a 2a 20 76 ctor size. ** v
127bb 61 6c 75 65 2e 20 52 65 73 65 74 20 69 74 20 74 alue. Reset it t
127bc 6f 20 74 68 65 20 63 6f 72 72 65 63 74 20 76 61 o the correct va
127bd 6c 75 65 20 66 6f 72 20 74 68 69 73 20 70 72 6f lue for this pro
127be 63 65 73 73 2e 0a 20 20 2a 2f 0a 20 20 73 65 74 cess.. */. set
127bf 53 65 63 74 6f 72 53 69 7a 65 28 70 50 61 67 65 SectorSize(pPage
127c0 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b r);. return rc;
127c1 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6c 61 79 62 61 .}../*.** Playba
127c2 63 6b 20 73 61 76 65 70 6f 69 6e 74 20 70 53 61 ck savepoint pSa
127c3 76 65 70 6f 69 6e 74 2e 20 4f 72 2c 20 69 66 20 vepoint. Or, if
127c4 70 53 61 76 65 70 6f 69 6e 74 3d 3d 4e 55 4c 4c pSavepoint==NULL
127c5 2c 20 74 68 65 6e 20 70 6c 61 79 62 61 63 6b 0a , then playback.
127c6 2a 2a 20 74 68 65 20 65 6e 74 69 72 65 20 6d 61 ** the entire ma
127c7 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
127c8 65 2e 20 54 68 65 20 63 61 73 65 20 70 53 61 76 e. The case pSav
127c9 65 70 6f 69 6e 74 3d 3d 4e 55 4c 4c 20 6f 63 63 epoint==NULL occ
127ca 75 72 73 20 77 68 65 6e 20 0a 2a 2a 20 61 20 52 urs when .** a R
127cb 4f 4c 4c 42 41 43 4b 20 54 4f 20 63 6f 6d 6d 61 OLLBACK TO comma
127cc 6e 64 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e nd is invoked on
127cd 20 61 20 53 41 56 45 50 4f 49 4e 54 20 74 68 61 a SAVEPOINT tha
127ce 74 20 69 73 20 61 20 74 72 61 6e 73 61 63 74 69 t is a transacti
127cf 6f 6e 20 0a 2a 2a 20 73 61 76 65 70 6f 69 6e 74 on .** savepoint
127d0 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 70 53 61 ..**.** When pSa
127d1 76 65 70 6f 69 6e 74 20 69 73 20 6e 6f 74 20 4e vepoint is not N
127d2 55 4c 4c 20 28 6d 65 61 6e 69 6e 67 20 61 20 6e ULL (meaning a n
127d3 6f 6e 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 on-transaction s
127d4 61 76 65 70 6f 69 6e 74 20 69 73 20 0a 2a 2a 20 avepoint is .**
127d5 62 65 69 6e 67 20 72 6f 6c 6c 65 64 20 62 61 63 being rolled bac
127d6 6b 29 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 6c k), then the rol
127d7 6c 62 61 63 6b 20 63 6f 6e 73 69 73 74 73 20 6f lback consists o
127d8 66 20 75 70 20 74 6f 20 74 68 72 65 65 20 73 74 f up to three st
127d9 61 67 65 73 2c 0a 2a 2a 20 70 65 72 66 6f 72 6d ages,.** perform
127da 65 64 20 69 6e 20 74 68 65 20 6f 72 64 65 72 20 ed in the order
127db 73 70 65 63 69 66 69 65 64 3a 0a 2a 2a 0a 2a 2a specified:.**.**
127dc 20 20 20 2a 20 50 61 67 65 73 20 61 72 65 20 70 * Pages are p
127dd 6c 61 79 65 64 20 62 61 63 6b 20 66 72 6f 6d 20 layed back from
127de 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c the main journal
127df 20 73 74 61 72 74 69 6e 67 20 61 74 20 62 79 74 starting at byt
127e0 65 0a 2a 2a 20 20 20 20 20 6f 66 66 73 65 74 20 e.** offset
127e1 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 PagerSavepoint.i
127e2 4f 66 66 73 65 74 20 61 6e 64 20 63 6f 6e 74 69 Offset and conti
127e3 6e 75 69 6e 67 20 74 6f 20 0a 2a 2a 20 20 20 20 nuing to .**
127e4 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e PagerSavepoint.
127e5 69 48 64 72 4f 66 66 73 65 74 2c 20 6f 72 20 74 iHdrOffset, or t
127e6 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 o the end of the
127e7 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 0a 2a 2a main journal.**
127e8 20 20 20 20 20 66 69 6c 65 20 69 66 20 50 61 67 file if Pag
127e9 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 erSavepoint.iHdr
127ea 4f 66 66 73 65 74 20 69 73 20 7a 65 72 6f 2e 0a Offset is zero..
127eb 2a 2a 0a 2a 2a 20 20 20 2a 20 49 66 20 50 61 67 **.** * If Pag
127ec 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 erSavepoint.iHdr
127ed 4f 66 66 73 65 74 20 69 73 20 6e 6f 74 20 7a 65 Offset is not ze
127ee 72 6f 2c 20 74 68 65 6e 20 70 61 67 65 73 20 61 ro, then pages a
127ef 72 65 20 70 6c 61 79 65 64 0a 2a 2a 20 20 20 20 re played.**
127f0 20 62 61 63 6b 20 73 74 61 72 74 69 6e 67 20 66 back starting f
127f1 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 rom the journal
127f2 68 65 61 64 65 72 20 69 6d 6d 65 64 69 61 74 65 header immediate
127f3 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a ly following .**
127f4 20 20 20 20 20 50 61 67 65 72 53 61 76 65 70 6f PagerSavepo
127f5 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 20 74 int.iHdrOffset t
127f6 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 o the end of the
127f7 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 main journal fi
127f8 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 50 61 le..**.** * Pa
127f9 67 65 73 20 61 72 65 20 74 68 65 6e 20 70 6c 61 ges are then pla
127fa 79 65 64 20 62 61 63 6b 20 66 72 6f 6d 20 74 68 yed back from th
127fb 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 e sub-journal fi
127fc 6c 65 2c 20 73 74 61 72 74 69 6e 67 0a 2a 2a 20 le, starting.**
127fd 20 20 20 20 77 69 74 68 20 74 68 65 20 50 61 67 with the Pag
127fe 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 53 75 62 erSavepoint.iSub
127ff 52 65 63 20 61 6e 64 20 63 6f 6e 74 69 6e 75 69 Rec and continui
12800 6e 67 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 ng to the end of
12801 0a 2a 2a 20 20 20 20 20 74 68 65 20 6a 6f 75 72 .** the jour
12802 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 nal file..**.**
12803 54 68 72 6f 75 67 68 6f 75 74 20 74 68 65 20 72 Throughout the r
12804 6f 6c 6c 62 61 63 6b 20 70 72 6f 63 65 73 73 2c ollback process,
12805 20 65 61 63 68 20 74 69 6d 65 20 61 20 70 61 67 each time a pag
12806 65 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b e is rolled back
12807 2c 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73 70 , the.** corresp
12808 6f 6e 64 69 6e 67 20 62 69 74 20 69 73 20 73 65 onding bit is se
12809 74 20 69 6e 20 61 20 62 69 74 76 65 63 20 73 74 t in a bitvec st
1280a 72 75 63 74 75 72 65 20 28 76 61 72 69 61 62 6c ructure (variabl
1280b 65 20 70 44 6f 6e 65 20 69 6e 20 74 68 65 0a 2a e pDone in the.*
1280c 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * implementation
1280d 20 62 65 6c 6f 77 29 2e 20 54 68 69 73 20 69 73 below). This is
1280e 20 75 73 65 64 20 74 6f 20 65 6e 73 75 72 65 20 used to ensure
1280f 74 68 61 74 20 61 20 70 61 67 65 20 69 73 20 6f that a page is o
12810 6e 6c 79 0a 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 nly.** rolled ba
12811 63 6b 20 74 68 65 20 66 69 72 73 74 20 74 69 6d ck the first tim
12812 65 20 69 74 20 69 73 20 65 6e 63 6f 75 6e 74 65 e it is encounte
12813 72 65 64 20 69 6e 20 65 69 74 68 65 72 20 6a 6f red in either jo
12814 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 urnal..**.** If
12815 70 53 61 76 65 70 6f 69 6e 74 20 69 73 20 4e 55 pSavepoint is NU
12816 4c 4c 2c 20 74 68 65 6e 20 70 61 67 65 73 20 61 LL, then pages a
12817 72 65 20 6f 6e 6c 79 20 70 6c 61 79 65 64 20 62 re only played b
12818 61 63 6b 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 ack from the mai
12819 6e 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c n.** journal fil
1281a 65 2e 20 54 68 65 72 65 20 69 73 20 6e 6f 20 6e e. There is no n
1281b 65 65 64 20 66 6f 72 20 61 20 62 69 74 76 65 63 eed for a bitvec
1281c 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a in this case..*
1281d 2a 0a 2a 2a 20 49 6e 20 65 69 74 68 65 72 20 63 *.** In either c
1281e 61 73 65 2c 20 62 65 66 6f 72 65 20 70 6c 61 79 ase, before play
1281f 62 61 63 6b 20 63 6f 6d 6d 65 6e 63 65 73 20 74 back commences t
12820 68 65 20 50 61 67 65 72 2e 64 62 53 69 7a 65 20 he Pager.dbSize
12821 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69 73 20 72 variable.** is r
12822 65 73 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75 eset to the valu
12823 65 20 74 68 61 74 20 69 74 20 68 65 6c 64 20 61 e that it held a
12824 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 t the start of t
12825 68 65 20 73 61 76 65 70 6f 69 6e 74 20 0a 2a 2a he savepoint .**
12826 20 28 6f 72 20 74 72 61 6e 73 61 63 74 69 6f 6e (or transaction
12827 29 2e 20 4e 6f 20 70 61 67 65 20 77 69 74 68 20 ). No page with
12828 61 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 67 72 a page-number gr
12829 65 61 74 65 72 20 74 68 61 6e 20 74 68 69 73 20 eater than this
1282a 76 61 6c 75 65 0a 2a 2a 20 69 73 20 70 6c 61 79 value.** is play
1282b 65 64 20 62 61 63 6b 2e 20 49 66 20 6f 6e 65 20 ed back. If one
1282c 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 69 is encountered i
1282d 74 20 69 73 20 73 69 6d 70 6c 79 20 73 6b 69 70 t is simply skip
1282e 70 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ped..*/.static i
1282f 6e 74 20 70 61 67 65 72 50 6c 61 79 62 61 63 6b nt pagerPlayback
12830 53 61 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 Savepoint(Pager
12831 2a 70 50 61 67 65 72 2c 20 50 61 67 65 72 53 61 *pPager, PagerSa
12832 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 70 6f vepoint *pSavepo
12833 69 6e 74 29 7b 0a 20 20 69 36 34 20 73 7a 4a 3b int){. i64 szJ;
12834 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12835 20 2f 2a 20 45 66 66 65 63 74 69 76 65 20 73 69 /* Effective si
12836 7a 65 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a ze of the main j
12837 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20 ournal */. i64
12838 69 48 64 72 4f 66 66 3b 20 20 20 20 20 20 20 20 iHdrOff;
12839 20 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 66 /* End of f
1283a 69 72 73 74 20 73 65 67 6d 65 6e 74 20 6f 66 20 irst segment of
1283b 6d 61 69 6e 2d 6a 6f 75 72 6e 61 6c 20 72 65 63 main-journal rec
1283c 6f 72 64 73 20 2a 2f 0a 20 20 69 6e 74 20 72 63 ords */. int rc
1283d 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 = SQLITE_OK;
1283e 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 /* Return cod
1283f 65 20 2a 2f 0a 20 20 42 69 74 76 65 63 20 2a 70 e */. Bitvec *p
12840 44 6f 6e 65 20 3d 20 30 3b 20 20 20 20 20 20 20 Done = 0;
12841 2f 2a 20 42 69 74 76 65 63 20 74 6f 20 65 6e 73 /* Bitvec to ens
12842 75 72 65 20 70 61 67 65 73 20 70 6c 61 79 65 64 ure pages played
12843 20 62 61 63 6b 20 6f 6e 6c 79 20 6f 6e 63 65 20 back only once
12844 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 */.. assert( pP
12845 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 ager->state>=PAG
12846 45 52 5f 53 48 41 52 45 44 20 29 3b 0a 0a 20 20 ER_SHARED );..
12847 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 62 69 /* Allocate a bi
12848 74 76 65 63 20 74 6f 20 75 73 65 20 74 6f 20 73 tvec to use to s
12849 74 6f 72 65 20 74 68 65 20 73 65 74 20 6f 66 20 tore the set of
1284a 70 61 67 65 73 20 72 6f 6c 6c 65 64 20 62 61 63 pages rolled bac
1284b 6b 20 2a 2f 0a 20 20 69 66 28 20 70 53 61 76 65 k */. if( pSave
1284c 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 70 44 6f point ){. pDo
1284d 6e 65 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 ne = sqlite3Bitv
1284e 65 63 43 72 65 61 74 65 28 70 53 61 76 65 70 6f ecCreate(pSavepo
1284f 69 6e 74 2d 3e 6e 4f 72 69 67 29 3b 0a 20 20 20 int->nOrig);.
12850 20 69 66 28 20 21 70 44 6f 6e 65 20 29 7b 0a 20 if( !pDone ){.
12851 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
12852 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a TE_NOMEM;. }.
12853 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 }.. /* Set th
12854 65 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 e database size
12855 62 61 63 6b 20 74 6f 20 74 68 65 20 76 61 6c 75 back to the valu
12856 65 20 69 74 20 77 61 73 20 62 65 66 6f 72 65 20 e it was before
12857 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20 0a 20 the savepoint .
12858 20 2a 2a 20 62 65 69 6e 67 20 72 65 76 65 72 74 ** being revert
12859 65 64 20 77 61 73 20 6f 70 65 6e 65 64 2e 0a 20 ed was opened..
1285a 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 */. pPager->db
1285b 53 69 7a 65 20 3d 20 70 53 61 76 65 70 6f 69 6e Size = pSavepoin
1285c 74 20 3f 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e t ? pSavepoint->
1285d 6e 4f 72 69 67 20 3a 20 70 50 61 67 65 72 2d 3e nOrig : pPager->
1285e 64 62 4f 72 69 67 53 69 7a 65 3b 0a 0a 20 20 2f dbOrigSize;.. /
1285f 2a 20 55 73 65 20 70 50 61 67 65 72 2d 3e 6a 6f * Use pPager->jo
12860 75 72 6e 61 6c 4f 66 66 20 61 73 20 74 68 65 20 urnalOff as the
12861 65 66 66 65 63 74 69 76 65 20 73 69 7a 65 20 6f effective size o
12862 66 20 74 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 f the main rollb
12863 61 63 6b 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c ack. ** journal
12864 2e 20 20 54 68 65 20 61 63 74 75 61 6c 20 66 69 . The actual fi
12865 6c 65 20 6d 69 67 68 74 20 62 65 20 6c 61 72 67 le might be larg
12866 65 72 20 74 68 61 6e 20 74 68 69 73 20 69 6e 0a er than this in.
12867 20 20 2a 2a 20 50 41 47 45 52 5f 4a 4f 55 52 4e ** PAGER_JOURN
12868 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 20 ALMODE_TRUNCATE
12869 6f 72 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c or PAGER_JOURNAL
1286a 4d 4f 44 45 5f 50 45 52 53 49 53 54 2e 20 20 42 MODE_PERSIST. B
1286b 75 74 20 61 6e 79 74 68 69 6e 67 0a 20 20 2a 2a ut anything. **
1286c 20 70 61 73 74 20 70 50 61 67 65 72 2d 3e 6a 6f past pPager->jo
1286d 75 72 6e 61 6c 4f 66 66 20 69 73 20 6f 66 66 2d urnalOff is off-
1286e 6c 69 6d 69 74 73 20 74 6f 20 75 73 2e 0a 20 20 limits to us..
1286f 2a 2f 0a 20 20 73 7a 4a 20 3d 20 70 50 61 67 65 */. szJ = pPage
12870 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 0a r->journalOff;..
12871 20 20 2f 2a 20 42 65 67 69 6e 20 62 79 20 72 6f /* Begin by ro
12872 6c 6c 69 6e 67 20 62 61 63 6b 20 72 65 63 6f 72 lling back recor
12873 64 73 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e ds from the main
12874 20 6a 6f 75 72 6e 61 6c 20 73 74 61 72 74 69 6e journal startin
12875 67 20 61 74 0a 20 20 2a 2a 20 50 61 67 65 72 53 g at. ** PagerS
12876 61 76 65 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 avepoint.iOffset
12877 20 61 6e 64 20 63 6f 6e 74 69 6e 75 69 6e 67 20 and continuing
12878 74 6f 20 74 68 65 20 6e 65 78 74 20 6a 6f 75 72 to the next jour
12879 6e 61 6c 20 68 65 61 64 65 72 2e 0a 20 20 2a 2a nal header.. **
1287a 20 54 68 65 72 65 20 6d 69 67 68 74 20 62 65 20 There might be
1287b 72 65 63 6f 72 64 73 20 69 6e 20 74 68 65 20 6d records in the m
1287c 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 74 68 61 74 ain journal that
1287d 20 68 61 76 65 20 61 20 70 61 67 65 20 6e 75 6d have a page num
1287e 62 65 72 0a 20 20 2a 2a 20 67 72 65 61 74 65 72 ber. ** greater
1287f 20 74 68 61 6e 20 74 68 65 20 63 75 72 72 65 6e than the curren
12880 74 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 t database size
12881 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 (pPager->dbSize)
12882 20 62 75 74 20 74 68 6f 73 65 0a 20 20 2a 2a 20 but those. **
12883 77 69 6c 6c 20 62 65 20 73 6b 69 70 70 65 64 20 will be skipped
12884 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 20 20 automatically.
12885 50 61 67 65 73 20 61 72 65 20 61 64 64 65 64 20 Pages are added
12886 74 6f 20 70 44 6f 6e 65 20 61 73 20 74 68 65 79 to pDone as they
12887 0a 20 20 2a 2a 20 61 72 65 20 70 6c 61 79 65 64 . ** are played
12888 20 62 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 back.. */. if
12889 28 20 70 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a ( pSavepoint ){.
1288a 20 20 20 20 69 48 64 72 4f 66 66 20 3d 20 70 53 iHdrOff = pS
1288b 61 76 65 70 6f 69 6e 74 2d 3e 69 48 64 72 4f 66 avepoint->iHdrOf
1288c 66 73 65 74 20 3f 20 70 53 61 76 65 70 6f 69 6e fset ? pSavepoin
1288d 74 2d 3e 69 48 64 72 4f 66 66 73 65 74 20 3a 20 t->iHdrOffset :
1288e 73 7a 4a 3b 0a 20 20 20 20 70 50 61 67 65 72 2d szJ;. pPager-
1288f 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 70 53 >journalOff = pS
12890 61 76 65 70 6f 69 6e 74 2d 3e 69 4f 66 66 73 65 avepoint->iOffse
12891 74 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 72 63 t;. while( rc
12892 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 ==SQLITE_OK && p
12893 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
12894 66 3c 69 48 64 72 4f 66 66 20 29 7b 0a 20 20 20 f<iHdrOff ){.
12895 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c rc = pager_pl
12896 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 ayback_one_page(
12897 70 50 61 67 65 72 2c 20 31 2c 20 30 2c 20 26 70 pPager, 1, 0, &p
12898 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
12899 66 2c 20 31 2c 20 70 44 6f 6e 65 29 3b 0a 20 20 f, 1, pDone);.
1289a 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
1289b 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 rc!=SQLITE_DONE
1289c 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
1289d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
1289e 66 66 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f ff = 0;. }.. /
1289f 2a 20 43 6f 6e 74 69 6e 75 65 20 72 6f 6c 6c 69 * Continue rolli
128a0 6e 67 20 62 61 63 6b 20 72 65 63 6f 72 64 73 20 ng back records
128a1 6f 75 74 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 out of the main
128a2 6a 6f 75 72 6e 61 6c 20 73 74 61 72 74 69 6e 67 journal starting
128a3 20 61 74 0a 20 20 2a 2a 20 74 68 65 20 66 69 72 at. ** the fir
128a4 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 st journal heade
128a5 72 20 73 65 65 6e 20 61 6e 64 20 63 6f 6e 74 69 r seen and conti
128a6 6e 75 69 6e 67 20 75 6e 74 69 6c 20 74 68 65 20 nuing until the
128a7 65 66 66 65 63 74 69 76 65 20 65 6e 64 0a 20 20 effective end.
128a8 2a 2a 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a ** of the main j
128a9 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20 43 6f ournal file. Co
128aa 6e 74 69 6e 75 65 20 74 6f 20 73 6b 69 70 20 6f ntinue to skip o
128ab 75 74 2d 6f 66 2d 72 61 6e 67 65 20 70 61 67 65 ut-of-range page
128ac 73 20 61 6e 64 0a 20 20 2a 2a 20 63 6f 6e 74 69 s and. ** conti
128ad 6e 75 65 20 61 64 64 69 6e 67 20 70 61 67 65 73 nue adding pages
128ae 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 6f 20 rolled back to
128af 70 44 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 77 68 pDone.. */. wh
128b0 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f ile( rc==SQLITE_
128b1 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f OK && pPager->jo
128b2 75 72 6e 61 6c 4f 66 66 3c 73 7a 4a 20 29 7b 0a urnalOff<szJ ){.
128b3 20 20 20 20 75 33 32 20 69 69 3b 20 20 20 20 20 u32 ii;
128b4 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 /* Loop c
128b5 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 75 33 ounter */. u3
128b6 32 20 6e 4a 52 65 63 20 3d 20 30 3b 20 20 20 20 2 nJRec = 0;
128b7 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 4a 6f /* Number of Jo
128b8 75 72 6e 61 6c 20 52 65 63 6f 72 64 73 20 2a 2f urnal Records */
128b9 0a 20 20 20 20 75 33 32 20 64 75 6d 6d 79 3b 0a . u32 dummy;.
128ba 20 20 20 20 72 63 20 3d 20 72 65 61 64 4a 6f 75 rc = readJou
128bb 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72 2c 20 rnalHdr(pPager,
128bc 30 2c 20 73 7a 4a 2c 20 26 6e 4a 52 65 63 2c 20 0, szJ, &nJRec,
128bd 26 64 75 6d 6d 79 29 3b 0a 20 20 20 20 61 73 73 &dummy);. ass
128be 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f ert( rc!=SQLITE_
128bf 44 4f 4e 45 20 29 3b 0a 0a 20 20 20 20 2f 2a 0a DONE );.. /*.
128c0 20 20 20 20 2a 2a 20 54 68 65 20 22 70 50 61 67 ** The "pPag
128c1 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a er->journalHdr+J
128c2 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 OURNAL_HDR_SZ(pP
128c3 61 67 65 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a ager)==pPager->j
128c4 6f 75 72 6e 61 6c 4f 66 66 22 0a 20 20 20 20 2a ournalOff". *
128c5 2a 20 74 65 73 74 20 69 73 20 72 65 6c 61 74 65 * test is relate
128c6 64 20 74 6f 20 74 69 63 6b 65 74 20 23 32 35 36 d to ticket #256
128c7 35 2e 20 20 53 65 65 20 74 68 65 20 64 69 73 63 5. See the disc
128c8 75 73 73 69 6f 6e 20 69 6e 20 74 68 65 0a 20 20 ussion in the.
128c9 20 20 2a 2a 20 70 61 67 65 72 5f 70 6c 61 79 62 ** pager_playb
128ca 61 63 6b 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 ack() function f
128cb 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e or additional in
128cc 66 6f 72 6d 61 74 69 6f 6e 2e 0a 20 20 20 20 2a formation.. *
128cd 2f 0a 20 20 20 20 69 66 28 20 6e 4a 52 65 63 3d /. if( nJRec=
128ce 3d 30 20 0a 20 20 20 20 20 26 26 20 70 50 61 67 =0 . && pPag
128cf 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a er->journalHdr+J
128d0 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 OURNAL_HDR_SZ(pP
128d1 61 67 65 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a ager)==pPager->j
128d2 6f 75 72 6e 61 6c 4f 66 66 0a 20 20 20 20 29 7b ournalOff. ){
128d3 0a 20 20 20 20 20 20 6e 4a 52 65 63 20 3d 20 28 . nJRec = (
128d4 75 33 32 29 28 28 73 7a 4a 20 2d 20 70 50 61 67 u32)((szJ - pPag
128d5 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 2f er->journalOff)/
128d6 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 JOURNAL_PG_SZ(pP
128d7 61 67 65 72 29 29 3b 0a 20 20 20 20 7d 0a 20 20 ager));. }.
128d8 20 20 66 6f 72 28 69 69 3d 30 3b 20 72 63 3d 3d for(ii=0; rc==
128d9 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c SQLITE_OK && ii<
128da 6e 4a 52 65 63 20 26 26 20 70 50 61 67 65 72 2d nJRec && pPager-
128db 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3c 73 7a 4a 3b >journalOff<szJ;
128dc 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 63 ii++){. rc
128dd 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 = pager_playbac
128de 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65 k_one_page(pPage
128df 72 2c 20 31 2c 20 30 2c 20 26 70 50 61 67 65 72 r, 1, 0, &pPager
128e0 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 31 2c ->journalOff, 1,
128e1 20 70 44 6f 6e 65 29 3b 0a 20 20 20 20 7d 0a 20 pDone);. }.
128e2 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 assert( rc!=S
128e3 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 QLITE_DONE );.
128e4 7d 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 3d }. assert( rc!=
128e5 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 SQLITE_OK || pPa
128e6 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d ger->journalOff=
128e7 3d 73 7a 4a 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 =szJ );.. /* Fi
128e8 6e 61 6c 6c 79 2c 20 20 72 6f 6c 6c 62 61 63 6b nally, rollback
128e9 20 70 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20 pages from the
128ea 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 20 50 61 sub-journal. Pa
128eb 67 65 20 74 68 61 74 20 77 65 72 65 0a 20 20 2a ge that were. *
128ec 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 72 6f 6c * previously rol
128ed 6c 65 64 20 62 61 63 6b 20 6f 75 74 20 6f 66 20 led back out of
128ee 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c the main journal
128ef 20 28 61 6e 64 20 61 72 65 20 68 65 6e 63 65 20 (and are hence
128f0 69 6e 20 70 44 6f 6e 65 29 0a 20 20 2a 2a 20 77 in pDone). ** w
128f1 69 6c 6c 20 62 65 20 73 6b 69 70 70 65 64 2e 20 ill be skipped.
128f2 20 4f 75 74 2d 6f 66 2d 72 61 6e 67 65 20 70 61 Out-of-range pa
128f3 67 65 73 20 61 72 65 20 61 6c 73 6f 20 73 6b 69 ges are also ski
128f4 70 70 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 pped.. */. if(
128f5 20 70 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 pSavepoint ){.
128f6 20 20 20 75 33 32 20 69 69 3b 20 20 20 20 20 20 u32 ii;
128f7 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f /* Loop co
128f8 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 69 36 34 unter */. i64
128f9 20 6f 66 66 73 65 74 20 3d 20 70 53 61 76 65 70 offset = pSavep
128fa 6f 69 6e 74 2d 3e 69 53 75 62 52 65 63 2a 28 34 oint->iSubRec*(4
128fb 2b 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a +pPager->pageSiz
128fc 65 29 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d 70 e);. for(ii=p
128fd 53 61 76 65 70 6f 69 6e 74 2d 3e 69 53 75 62 52 Savepoint->iSubR
128fe 65 63 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f ec; rc==SQLITE_O
128ff 4b 20 26 26 20 69 69 3c 70 50 61 67 65 72 2d 3e K && ii<pPager->
12900 6e 53 75 62 52 65 63 3b 20 69 69 2b 2b 29 7b 0a nSubRec; ii++){.
12901 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6f 66 assert( of
12902 66 73 65 74 3d 3d 69 69 2a 28 34 2b 70 50 61 67 fset==ii*(4+pPag
12903 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 20 29 3b er->pageSize) );
12904 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 . rc = page
12905 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 r_playback_one_p
12906 61 67 65 28 70 50 61 67 65 72 2c 20 30 2c 20 30 age(pPager, 0, 0
12907 2c 20 26 6f 66 66 73 65 74 2c 20 31 2c 20 70 44 , &offset, 1, pD
12908 6f 6e 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 one);. }.
12909 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 assert( rc!=SQLI
1290a 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 7d 0a 0a TE_DONE );. }..
1290b 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 sqlite3BitvecD
1290c 65 73 74 72 6f 79 28 70 44 6f 6e 65 29 3b 0a 20 estroy(pDone);.
1290d 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1290e 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 OK ){. pPager
1290f 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 73 ->journalOff = s
12910 7a 4a 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e zJ;. }. return
12911 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 rc;.}../*.** Ch
12912 61 6e 67 65 20 74 68 65 20 6d 61 78 69 6d 75 6d ange the maximum
12913 20 6e 75 6d 62 65 72 20 6f 66 20 69 6e 2d 6d 65 number of in-me
12914 6d 6f 72 79 20 70 61 67 65 73 20 74 68 61 74 20 mory pages that
12915 61 72 65 20 61 6c 6c 6f 77 65 64 2e 0a 2a 2f 0a are allowed..*/.
12916 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
12917 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 oid sqlite3Pager
12918 53 65 74 43 61 63 68 65 73 69 7a 65 28 50 61 67 SetCachesize(Pag
12919 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 er *pPager, int
1291a 6d 78 50 61 67 65 29 7b 0a 20 20 73 71 6c 69 74 mxPage){. sqlit
1291b 65 33 50 63 61 63 68 65 53 65 74 43 61 63 68 65 e3PcacheSetCache
1291c 73 69 7a 65 28 70 50 61 67 65 72 2d 3e 70 50 43 size(pPager->pPC
1291d 61 63 68 65 2c 20 6d 78 50 61 67 65 29 3b 0a 7d ache, mxPage);.}
1291e 0a 0a 2f 2a 0a 2a 2a 20 41 64 6a 75 73 74 20 74 ../*.** Adjust t
1291f 68 65 20 72 6f 62 75 73 74 6e 65 73 73 20 6f 66 he robustness of
12920 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f the database to
12921 20 64 61 6d 61 67 65 20 64 75 65 20 74 6f 20 4f damage due to O
12922 53 20 63 72 61 73 68 65 73 0a 2a 2a 20 6f 72 20 S crashes.** or
12923 70 6f 77 65 72 20 66 61 69 6c 75 72 65 73 20 62 power failures b
12924 79 20 63 68 61 6e 67 69 6e 67 20 74 68 65 20 6e y changing the n
12925 75 6d 62 65 72 20 6f 66 20 73 79 6e 63 73 28 29 umber of syncs()
12926 73 20 77 68 65 6e 20 77 72 69 74 69 6e 67 0a 2a s when writing.*
12927 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a * the rollback j
12928 6f 75 72 6e 61 6c 2e 20 20 54 68 65 72 65 20 61 ournal. There a
12929 72 65 20 74 68 72 65 65 20 6c 65 76 65 6c 73 3a re three levels:
1292a 0a 2a 2a 0a 2a 2a 20 20 20 20 4f 46 46 20 20 20 .**.** OFF
1292b 20 20 20 20 73 71 6c 69 74 65 33 4f 73 53 79 6e sqlite3OsSyn
1292c 63 28 29 20 69 73 20 6e 65 76 65 72 20 63 61 6c c() is never cal
1292d 6c 65 64 2e 20 20 54 68 69 73 20 69 73 20 74 68 led. This is th
1292e 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20 20 20 20 e default.**
1292f 20 20 20 20 20 20 20 20 20 20 66 6f 72 20 74 65 for te
12930 6d 70 6f 72 61 72 79 20 61 6e 64 20 74 72 61 6e mporary and tran
12931 73 69 65 6e 74 20 66 69 6c 65 73 2e 0a 2a 2a 0a sient files..**.
12932 2a 2a 20 20 20 20 4e 4f 52 4d 41 4c 20 20 20 20 ** NORMAL
12933 54 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 The journal is s
12934 79 6e 63 65 64 20 6f 6e 63 65 20 62 65 66 6f 72 ynced once befor
12935 65 20 77 72 69 74 65 73 20 62 65 67 69 6e 20 6f e writes begin o
12936 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 n the.**
12937 20 20 20 20 20 20 64 61 74 61 62 61 73 65 2e 20 database.
12938 20 54 68 69 73 20 69 73 20 6e 6f 72 6d 61 6c 6c This is normall
12939 79 20 61 64 65 71 75 61 74 65 20 70 72 6f 74 65 y adequate prote
1293a 63 74 69 6f 6e 2c 20 62 75 74 0a 2a 2a 20 20 20 ction, but.**
1293b 20 20 20 20 20 20 20 20 20 20 20 69 74 20 69 73 it is
1293c 20 74 68 65 6f 72 65 74 69 63 61 6c 6c 79 20 70 theoretically p
1293d 6f 73 73 69 62 6c 65 2c 20 74 68 6f 75 67 68 20 ossible, though
1293e 76 65 72 79 20 75 6e 6c 69 6b 65 6c 79 2c 0a 2a very unlikely,.*
1293f 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 * t
12940 68 61 74 20 61 6e 20 69 6e 6f 70 65 72 74 75 6e hat an inopertun
12941 65 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 e power failure
12942 63 6f 75 6c 64 20 6c 65 61 76 65 20 74 68 65 20 could leave the
12943 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 journal.**
12944 20 20 20 20 20 20 20 20 69 6e 20 61 20 73 74 61 in a sta
12945 74 65 20 77 68 69 63 68 20 77 6f 75 6c 64 20 63 te which would c
12946 61 75 73 65 20 64 61 6d 61 67 65 20 74 6f 20 74 ause damage to t
12947 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 he database.**
12948 20 20 20 20 20 20 20 20 20 20 20 20 77 68 65 6e when
12949 20 69 74 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 it is rolled ba
1294a 63 6b 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 46 55 4c ck..**.** FUL
1294b 4c 20 20 20 20 20 20 54 68 65 20 6a 6f 75 72 6e L The journ
1294c 61 6c 20 69 73 20 73 79 6e 63 65 64 20 74 77 69 al is synced twi
1294d 63 65 20 62 65 66 6f 72 65 20 77 72 69 74 65 73 ce before writes
1294e 20 62 65 67 69 6e 20 6f 6e 20 74 68 65 0a 2a 2a begin on the.**
1294f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 61 da
12950 74 61 62 61 73 65 20 28 77 69 74 68 20 73 6f 6d tabase (with som
12951 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 e additional inf
12952 6f 72 6d 61 74 69 6f 6e 20 2d 20 74 68 65 20 6e ormation - the n
12953 52 65 63 20 66 69 65 6c 64 0a 2a 2a 20 20 20 20 Rec field.**
12954 20 20 20 20 20 20 20 20 20 20 6f 66 20 74 68 65 of the
12955 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 journal header
12956 2d 20 62 65 69 6e 67 20 77 72 69 74 74 65 6e 20 - being written
12957 69 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 74 in between the t
12958 77 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 wo.**
12959 20 20 20 73 79 6e 63 73 29 2e 20 20 49 66 20 77 syncs). If w
1295a 65 20 61 73 73 75 6d 65 20 74 68 61 74 20 77 72 e assume that wr
1295b 69 74 69 6e 67 20 61 0a 2a 2a 20 20 20 20 20 20 iting a.**
1295c 20 20 20 20 20 20 20 20 73 69 6e 67 6c 65 20 64 single d
1295d 69 73 6b 20 73 65 63 74 6f 72 20 69 73 20 61 74 isk sector is at
1295e 6f 6d 69 63 2c 20 74 68 65 6e 20 74 68 69 73 20 omic, then this
1295f 6d 6f 64 65 20 70 72 6f 76 69 64 65 73 0a 2a 2a mode provides.**
12960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 73 as
12961 73 75 72 61 6e 63 65 20 74 68 61 74 20 74 68 65 surance that the
12962 20 6a 6f 75 72 6e 61 6c 20 77 69 6c 6c 20 6e 6f journal will no
12963 74 20 62 65 20 63 6f 72 72 75 70 74 65 64 20 74 t be corrupted t
12964 6f 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 o the.**
12965 20 20 20 20 20 20 70 6f 69 6e 74 20 6f 66 20 63 point of c
12966 61 75 73 69 6e 67 20 64 61 6d 61 67 65 20 74 6f ausing damage to
12967 20 74 68 65 20 64 61 74 61 62 61 73 65 20 64 75 the database du
12968 72 69 6e 67 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a ring rollback..*
12969 2a 0a 2a 2a 20 4e 75 6d 65 72 69 63 20 76 61 6c *.** Numeric val
1296a 75 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 ues associated w
1296b 69 74 68 20 74 68 65 73 65 20 73 74 61 74 65 73 ith these states
1296c 20 61 72 65 20 4f 46 46 3d 3d 31 2c 20 4e 4f 52 are OFF==1, NOR
1296d 4d 41 4c 3d 32 2c 0a 2a 2a 20 61 6e 64 20 46 55 MAL=2,.** and FU
1296e 4c 4c 3d 33 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 LL=3..*/.#ifndef
1296f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 SQLITE_OMIT_PAG
12970 45 52 5f 50 52 41 47 4d 41 53 0a 53 51 4c 49 54 ER_PRAGMAS.SQLIT
12971 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
12972 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 53 61 qlite3PagerSetSa
12973 66 65 74 79 4c 65 76 65 6c 28 50 61 67 65 72 20 fetyLevel(Pager
12974 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6c 65 76 *pPager, int lev
12975 65 6c 2c 20 69 6e 74 20 62 46 75 6c 6c 46 73 79 el, int bFullFsy
12976 6e 63 29 7b 0a 20 20 70 50 61 67 65 72 2d 3e 6e nc){. pPager->n
12977 6f 53 79 6e 63 20 3d 20 20 28 6c 65 76 65 6c 3d oSync = (level=
12978 3d 31 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 =1 || pPager->te
12979 6d 70 46 69 6c 65 29 20 3f 31 3a 30 3b 0a 20 20 mpFile) ?1:0;.
1297a 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 pPager->fullSync
1297b 20 3d 20 28 6c 65 76 65 6c 3d 3d 33 20 26 26 20 = (level==3 &&
1297c 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c !pPager->tempFil
1297d 65 29 20 3f 31 3a 30 3b 0a 20 20 70 50 61 67 65 e) ?1:0;. pPage
1297e 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 20 3d 20 r->sync_flags =
1297f 28 62 46 75 6c 6c 46 73 79 6e 63 3f 53 51 4c 49 (bFullFsync?SQLI
12980 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 3a 53 51 4c TE_SYNC_FULL:SQL
12981 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 29 ITE_SYNC_NORMAL)
12982 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e ;. if( pPager->
12983 6e 6f 53 79 6e 63 20 29 20 70 50 61 67 65 72 2d noSync ) pPager-
12984 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 7d >needSync = 0;.}
12985 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
12986 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f he following glo
12987 62 61 6c 20 76 61 72 69 61 62 6c 65 20 69 73 20 bal variable is
12988 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 68 65 6e incremented when
12989 65 76 65 72 20 74 68 65 20 6c 69 62 72 61 72 79 ever the library
1298a 0a 2a 2a 20 61 74 74 65 6d 70 74 73 20 74 6f 20 .** attempts to
1298b 6f 70 65 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 open a temporary
1298c 20 66 69 6c 65 2e 20 20 54 68 69 73 20 69 6e 66 file. This inf
1298d 6f 72 6d 61 74 69 6f 6e 20 69 73 20 75 73 65 64 ormation is used
1298e 20 66 6f 72 0a 2a 2a 20 74 65 73 74 69 6e 67 20 for.** testing
1298f 61 6e 64 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c and analysis onl
12990 79 2e 20 20 0a 2a 2f 0a 23 69 66 64 65 66 20 53 y. .*/.#ifdef S
12991 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 QLITE_TEST.SQLIT
12992 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
12993 33 5f 6f 70 65 6e 74 65 6d 70 5f 63 6f 75 6e 74 3_opentemp_count
12994 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a = 0;.#endif../*
12995 0a 2a 2a 20 4f 70 65 6e 20 61 20 74 65 6d 70 6f .** Open a tempo
12996 72 61 72 79 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a rary file..**.**
12997 20 57 72 69 74 65 20 74 68 65 20 66 69 6c 65 20 Write the file
12998 64 65 73 63 72 69 70 74 6f 72 20 69 6e 74 6f 20 descriptor into
12999 2a 70 46 69 6c 65 2e 20 52 65 74 75 72 6e 20 53 *pFile. Return S
1299a 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 QLITE_OK on succ
1299b 65 73 73 20 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 ess .** or some
1299c 6f 74 68 65 72 20 65 72 72 6f 72 20 63 6f 64 65 other error code
1299d 20 69 66 20 77 65 20 66 61 69 6c 2e 20 54 68 65 if we fail. The
1299e 20 4f 53 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74 OS will automat
1299f 69 63 61 6c 6c 79 20 0a 2a 2a 20 64 65 6c 65 74 ically .** delet
129a0 65 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 e the temporary
129a1 66 69 6c 65 20 77 68 65 6e 20 69 74 20 69 73 20 file when it is
129a2 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 closed..**.** Th
129a3 65 20 66 6c 61 67 73 20 70 61 73 73 65 64 20 74 e flags passed t
129a4 6f 20 74 68 65 20 56 46 53 20 6c 61 79 65 72 20 o the VFS layer
129a5 78 4f 70 65 6e 28 29 20 63 61 6c 6c 20 61 72 65 xOpen() call are
129a6 20 74 68 6f 73 65 20 73 70 65 63 69 66 69 65 64 those specified
129a7 0a 2a 2a 20 62 79 20 70 61 72 61 6d 65 74 65 72 .** by parameter
129a8 20 76 66 73 46 6c 61 67 73 20 4f 52 65 64 20 77 vfsFlags ORed w
129a9 69 74 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e ith the followin
129aa 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 51 4c g:.**.** SQL
129ab 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 ITE_OPEN_READWRI
129ac 54 45 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 TE.** SQLITE
129ad 5f 4f 50 45 4e 5f 43 52 45 41 54 45 0a 2a 2a 20 _OPEN_CREATE.**
129ae 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f SQLITE_OPEN_
129af 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 EXCLUSIVE.**
129b0 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c SQLITE_OPEN_DEL
129b1 45 54 45 4f 4e 43 4c 4f 53 45 0a 2a 2f 0a 73 74 ETEONCLOSE.*/.st
129b2 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 4f 70 atic int pagerOp
129b3 65 6e 74 65 6d 70 28 0a 20 20 50 61 67 65 72 20 entemp(. Pager
129b4 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 20 20 *pPager,
129b5 2f 2a 20 54 68 65 20 70 61 67 65 72 20 6f 62 6a /* The pager obj
129b6 65 63 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ect */. sqlite3
129b7 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20 2f _file *pFile, /
129b8 2a 20 57 72 69 74 65 20 74 68 65 20 66 69 6c 65 * Write the file
129b9 20 64 65 73 63 72 69 70 74 6f 72 20 68 65 72 65 descriptor here
129ba 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 */. int vfsFla
129bb 67 73 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 gs /* F
129bc 6c 61 67 73 20 70 61 73 73 65 64 20 74 68 72 6f lags passed thro
129bd 75 67 68 20 74 6f 20 74 68 65 20 56 46 53 20 2a ugh to the VFS *
129be 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 /.){. int rc;
129bf 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
129c0 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a Return code */..
129c1 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
129c2 53 54 0a 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 ST. sqlite3_ope
129c3 6e 74 65 6d 70 5f 63 6f 75 6e 74 2b 2b 3b 20 20 ntemp_count++;
129c4 2f 2a 20 55 73 65 64 20 66 6f 72 20 74 65 73 74 /* Used for test
129c5 69 6e 67 20 61 6e 64 20 61 6e 61 6c 79 73 69 73 ing and analysis
129c6 20 6f 6e 6c 79 20 2a 2f 0a 23 65 6e 64 69 66 0a only */.#endif.
129c7 0a 20 20 76 66 73 46 6c 61 67 73 20 7c 3d 20 20 . vfsFlags |=
129c8 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
129c9 57 52 49 54 45 20 7c 20 53 51 4c 49 54 45 5f 4f WRITE | SQLITE_O
129ca 50 45 4e 5f 43 52 45 41 54 45 20 7c 0a 20 20 20 PEN_CREATE |.
129cb 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f SQLITE_
129cc 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 7c OPEN_EXCLUSIVE |
129cd 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c SQLITE_OPEN_DEL
129ce 45 54 45 4f 4e 43 4c 4f 53 45 3b 0a 20 20 72 63 ETEONCLOSE;. rc
129cf 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e = sqlite3OsOpen
129d0 28 70 50 61 67 65 72 2d 3e 70 56 66 73 2c 20 30 (pPager->pVfs, 0
129d1 2c 20 70 46 69 6c 65 2c 20 76 66 73 46 6c 61 67 , pFile, vfsFlag
129d2 73 2c 20 30 29 3b 0a 20 20 61 73 73 65 72 74 28 s, 0);. assert(
129d3 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c rc!=SQLITE_OK |
129d4 7c 20 69 73 4f 70 65 6e 28 70 46 69 6c 65 29 20 | isOpen(pFile)
129d5 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
129d6 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 }../*.** Set the
129d7 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 66 75 busy handler fu
129d8 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 nction..**.** Th
129d9 65 20 70 61 67 65 72 20 69 6e 76 6f 6b 65 73 20 e pager invokes
129da 74 68 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 the busy-handler
129db 20 69 66 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 if sqlite3OsLoc
129dc 6b 28 29 20 72 65 74 75 72 6e 73 20 0a 2a 2a 20 k() returns .**
129dd 53 51 4c 49 54 45 5f 42 55 53 59 20 77 68 65 6e SQLITE_BUSY when
129de 20 74 72 79 69 6e 67 20 74 6f 20 75 70 67 72 61 trying to upgra
129df 64 65 20 66 72 6f 6d 20 6e 6f 2d 6c 6f 63 6b 20 de from no-lock
129e0 74 6f 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b to a SHARED lock
129e1 2c 0a 2a 2a 20 6f 72 20 77 68 65 6e 20 74 72 79 ,.** or when try
129e2 69 6e 67 20 74 6f 20 75 70 67 72 61 64 65 20 66 ing to upgrade f
129e3 72 6f 6d 20 61 20 52 45 53 45 52 56 45 44 20 6c rom a RESERVED l
129e4 6f 63 6b 20 74 6f 20 61 6e 20 45 58 43 4c 55 53 ock to an EXCLUS
129e5 49 56 45 20 0a 2a 2a 20 6c 6f 63 6b 2e 20 49 74 IVE .** lock. It
129e6 20 64 6f 65 73 20 2a 6e 6f 74 2a 20 69 6e 76 6f does *not* invo
129e7 6b 65 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 ke the busy hand
129e8 6c 65 72 20 77 68 65 6e 20 75 70 67 72 61 64 69 ler when upgradi
129e9 6e 67 20 66 72 6f 6d 0a 2a 2a 20 53 48 41 52 45 ng from.** SHARE
129ea 44 20 74 6f 20 52 45 53 45 52 56 45 44 2c 20 6f D to RESERVED, o
129eb 72 20 77 68 65 6e 20 75 70 67 72 61 64 69 6e 67 r when upgrading
129ec 20 66 72 6f 6d 20 53 48 41 52 45 44 20 74 6f 20 from SHARED to
129ed 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 28 77 68 EXCLUSIVE.** (wh
129ee 69 63 68 20 6f 63 63 75 72 73 20 64 75 72 69 6e ich occurs durin
129ef 67 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f g hot-journal ro
129f0 6c 6c 62 61 63 6b 29 2e 20 53 75 6d 6d 61 72 79 llback). Summary
129f1 3a 0a 2a 2a 0a 2a 2a 20 20 20 54 72 61 6e 73 69 :.**.** Transi
129f2 74 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 tion
129f3 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 49 6e | In
129f4 76 6f 6b 65 73 20 78 42 75 73 79 48 61 6e 64 6c vokes xBusyHandl
129f5 65 72 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d er.** --------
129f6 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
129f7 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
129f8 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
129f9 0a 2a 2a 20 20 20 4e 4f 5f 4c 4f 43 4b 20 20 20 .** NO_LOCK
129fa 20 20 20 20 2d 3e 20 53 48 41 52 45 44 5f 4c 4f -> SHARED_LO
129fb 43 4b 20 20 20 20 20 20 7c 20 59 65 73 0a 2a 2a CK | Yes.**
129fc 20 20 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 20 SHARED_LOCK
129fd 20 2d 3e 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 -> RESERVED_LOC
129fe 4b 20 20 20 20 7c 20 4e 6f 0a 2a 2a 20 20 20 53 K | No.** S
129ff 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20 2d 3e 20 HARED_LOCK ->
12a00 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 20 EXCLUSIVE_LOCK
12a01 20 7c 20 4e 6f 0a 2a 2a 20 20 20 52 45 53 45 52 | No.** RESER
12a02 56 45 44 5f 4c 4f 43 4b 20 2d 3e 20 45 58 43 4c VED_LOCK -> EXCL
12a03 55 53 49 56 45 5f 4c 4f 43 4b 20 20 20 7c 20 59 USIVE_LOCK | Y
12a04 65 73 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 es.**.** If the
12a05 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 63 61 6c busy-handler cal
12a06 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f lback returns no
12a07 6e 2d 7a 65 72 6f 2c 20 74 68 65 20 6c 6f 63 6b n-zero, the lock
12a08 20 69 73 20 0a 2a 2a 20 72 65 74 72 69 65 64 2e is .** retried.
12a09 20 49 66 20 69 74 20 72 65 74 75 72 6e 73 20 7a If it returns z
12a0a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 53 51 ero, then the SQ
12a0b 4c 49 54 45 5f 42 55 53 59 20 65 72 72 6f 72 20 LITE_BUSY error
12a0c 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 74 is.** returned t
12a0d 6f 20 74 68 65 20 63 61 6c 6c 65 72 20 6f 66 20 o the caller of
12a0e 74 68 65 20 70 61 67 65 72 20 41 50 49 20 66 75 the pager API fu
12a0f 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 nction..*/.SQLIT
12a10 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
12a11 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 42 75 qlite3PagerSetBu
12a12 73 79 68 61 6e 64 6c 65 72 28 0a 20 20 50 61 67 syhandler(. Pag
12a13 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 er *pPager,
12a14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12a15 20 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 /* Pager objec
12a16 74 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 42 75 t */. int (*xBu
12a17 73 79 48 61 6e 64 6c 65 72 29 28 76 6f 69 64 20 syHandler)(void
12a18 2a 29 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 *), /* P
12a19 6f 69 6e 74 65 72 20 74 6f 20 62 75 73 79 2d 68 ointer to busy-h
12a1a 61 6e 64 6c 65 72 20 66 75 6e 63 74 69 6f 6e 20 andler function
12a1b 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 73 79 */. void *pBusy
12a1c 48 61 6e 64 6c 65 72 41 72 67 20 20 20 20 20 20 HandlerArg
12a1d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 /* Arg
12a1e 75 6d 65 6e 74 20 74 6f 20 70 61 73 73 20 74 6f ument to pass to
12a1f 20 78 42 75 73 79 48 61 6e 64 6c 65 72 20 2a 2f xBusyHandler */
12a20 0a 29 7b 20 20 0a 20 20 70 50 61 67 65 72 2d 3e .){ . pPager->
12a21 78 42 75 73 79 48 61 6e 64 6c 65 72 20 3d 20 78 xBusyHandler = x
12a22 42 75 73 79 48 61 6e 64 6c 65 72 3b 0a 20 20 70 BusyHandler;. p
12a23 50 61 67 65 72 2d 3e 70 42 75 73 79 48 61 6e 64 Pager->pBusyHand
12a24 6c 65 72 41 72 67 20 3d 20 70 42 75 73 79 48 61 lerArg = pBusyHa
12a25 6e 64 6c 65 72 41 72 67 3b 0a 7d 0a 0a 2f 2a 0a ndlerArg;.}../*.
12a26 2a 2a 20 52 65 70 6f 72 74 20 74 68 65 20 63 75 ** Report the cu
12a27 72 72 65 6e 74 20 70 61 67 65 20 73 69 7a 65 20 rrent page size
12a28 61 6e 64 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 and number of re
12a29 73 65 72 76 65 64 20 62 79 74 65 73 20 62 61 63 served bytes bac
12a2a 6b 0a 2a 2a 20 74 6f 20 74 68 65 20 63 6f 64 65 k.** to the code
12a2b 63 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c c..*/.#ifdef SQL
12a2c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 73 74 ITE_HAS_CODEC.st
12a2d 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 52 atic void pagerR
12a2e 65 70 6f 72 74 53 69 7a 65 28 50 61 67 65 72 20 eportSize(Pager
12a2f 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28 20 *pPager){. if(
12a30 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 53 69 pPager->xCodecSi
12a31 7a 65 43 68 6e 67 20 29 7b 0a 20 20 20 20 70 50 zeChng ){. pP
12a32 61 67 65 72 2d 3e 78 43 6f 64 65 63 53 69 7a 65 ager->xCodecSize
12a33 43 68 6e 67 28 70 50 61 67 65 72 2d 3e 70 43 6f Chng(pPager->pCo
12a34 64 65 63 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 dec, pPager->pag
12a35 65 53 69 7a 65 2c 0a 20 20 20 20 20 20 20 20 20 eSize,.
12a36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12a37 20 20 28 69 6e 74 29 70 50 61 67 65 72 2d 3e 6e (int)pPager->n
12a38 52 65 73 65 72 76 65 29 3b 0a 20 20 7d 0a 7d 0a Reserve);. }.}.
12a39 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 70 #else.# define p
12a3a 61 67 65 72 52 65 70 6f 72 74 53 69 7a 65 28 58 agerReportSize(X
12a3b 29 20 20 20 20 20 2f 2a 20 4e 6f 2d 6f 70 20 69 ) /* No-op i
12a3c 66 20 77 65 20 64 6f 20 6e 6f 74 20 73 75 70 70 f we do not supp
12a3d 6f 72 74 20 61 20 63 6f 64 65 63 20 2a 2f 0a 23 ort a codec */.#
12a3e 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 endif../*.** Cha
12a3f 6e 67 65 20 74 68 65 20 70 61 67 65 20 73 69 7a nge the page siz
12a40 65 20 75 73 65 64 20 62 79 20 74 68 65 20 50 61 e used by the Pa
12a41 67 65 72 20 6f 62 6a 65 63 74 2e 20 54 68 65 20 ger object. The
12a42 6e 65 77 20 70 61 67 65 20 73 69 7a 65 20 0a 2a new page size .*
12a43 2a 20 69 73 20 70 61 73 73 65 64 20 69 6e 20 2a * is passed in *
12a44 70 50 61 67 65 53 69 7a 65 2e 0a 2a 2a 0a 2a 2a pPageSize..**.**
12a45 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 If the pager is
12a46 20 69 6e 20 74 68 65 20 65 72 72 6f 72 20 73 74 in the error st
12a47 61 74 65 20 77 68 65 6e 20 74 68 69 73 20 66 75 ate when this fu
12a48 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
12a49 2c 20 69 74 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d , it.** is a no-
12a4a 6f 70 2e 20 54 68 65 20 76 61 6c 75 65 20 72 65 op. The value re
12a4b 74 75 72 6e 65 64 20 69 73 20 74 68 65 20 65 72 turned is the er
12a4c 72 6f 72 20 73 74 61 74 65 20 65 72 72 6f 72 20 ror state error
12a4d 63 6f 64 65 20 28 69 2e 65 2e 20 0a 2a 2a 20 6f code (i.e. .** o
12a4e 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 49 4f 45 ne of SQLITE_IOE
12a4f 52 52 2c 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 RR, SQLITE_CORRU
12a50 50 54 20 6f 72 20 53 51 4c 49 54 45 5f 46 55 4c PT or SQLITE_FUL
12a51 4c 29 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 L)..**.** Otherw
12a52 69 73 65 2c 20 69 66 20 61 6c 6c 20 6f 66 20 74 ise, if all of t
12a53 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 he following are
12a54 20 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a true:.**.** *
12a55 20 74 68 65 20 6e 65 77 20 70 61 67 65 20 73 69 the new page si
12a56 7a 65 20 28 76 61 6c 75 65 20 6f 66 20 2a 70 50 ze (value of *pP
12a57 61 67 65 53 69 7a 65 29 20 69 73 20 76 61 6c 69 ageSize) is vali
12a58 64 20 28 61 20 70 6f 77 65 72 20 0a 2a 2a 20 20 d (a power .**
12a59 20 20 20 6f 66 20 74 77 6f 20 62 65 74 77 65 65 of two betwee
12a5a 6e 20 35 31 32 20 61 6e 64 20 53 51 4c 49 54 45 n 512 and SQLITE
12a5b 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 2c 20 _MAX_PAGE_SIZE,
12a5c 69 6e 63 6c 75 73 69 76 65 29 2c 20 61 6e 64 0a inclusive), and.
12a5d 2a 2a 0a 2a 2a 20 20 20 2a 20 74 68 65 72 65 20 **.** * there
12a5e 61 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 are no outstandi
12a5f 6e 67 20 70 61 67 65 20 72 65 66 65 72 65 6e 63 ng page referenc
12a60 65 73 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 es, and.**.**
12a61 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 * the database i
12a62 73 20 65 69 74 68 65 72 20 6e 6f 74 20 61 6e 20 s either not an
12a63 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 in-memory databa
12a64 73 65 20 6f 72 20 69 74 20 69 73 0a 2a 2a 20 20 se or it is.**
12a65 20 20 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 an in-memory
12a66 64 61 74 61 62 61 73 65 20 74 68 61 74 20 63 75 database that cu
12a67 72 72 65 6e 74 6c 79 20 63 6f 6e 73 69 73 74 73 rrently consists
12a68 20 6f 66 20 7a 65 72 6f 20 70 61 67 65 73 2e 0a of zero pages..
12a69 2a 2a 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 70 **.** then the p
12a6a 61 67 65 72 20 6f 62 6a 65 63 74 20 70 61 67 65 ager object page
12a6b 20 73 69 7a 65 20 69 73 20 73 65 74 20 74 6f 20 size is set to
12a6c 2a 70 50 61 67 65 53 69 7a 65 2e 0a 2a 2a 0a 2a *pPageSize..**.*
12a6d 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 73 69 * If the page si
12a6e 7a 65 20 69 73 20 63 68 61 6e 67 65 64 2c 20 74 ze is changed, t
12a6f 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f hen this functio
12a70 6e 20 75 73 65 73 20 73 71 6c 69 74 65 33 50 61 n uses sqlite3Pa
12a71 67 65 72 4d 61 6c 6c 6f 63 28 29 20 0a 2a 2a 20 gerMalloc() .**
12a72 74 6f 20 6f 62 74 61 69 6e 20 61 20 6e 65 77 20 to obtain a new
12a73 50 61 67 65 72 2e 70 54 6d 70 53 70 61 63 65 20 Pager.pTmpSpace
12a74 62 75 66 66 65 72 2e 20 49 66 20 74 68 69 73 20 buffer. If this
12a75 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 74 65 6d allocation attem
12a76 70 74 20 0a 2a 2a 20 66 61 69 6c 73 2c 20 53 51 pt .** fails, SQ
12a77 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 LITE_NOMEM is re
12a78 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65 20 70 turned and the p
12a79 61 67 65 20 73 69 7a 65 20 72 65 6d 61 69 6e 73 age size remains
12a7a 20 75 6e 63 68 61 6e 67 65 64 2e 20 0a 2a 2a 20 unchanged. .**
12a7b 49 6e 20 61 6c 6c 20 6f 74 68 65 72 20 63 61 73 In all other cas
12a7c 65 73 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 es, SQLITE_OK is
12a7d 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a returned..**.**
12a7e 20 49 66 20 74 68 65 20 70 61 67 65 20 73 69 7a If the page siz
12a7f 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 e is not changed
12a80 2c 20 65 69 74 68 65 72 20 62 65 63 61 75 73 65 , either because
12a81 20 6f 6e 65 20 6f 66 20 74 68 65 20 65 6e 75 6d one of the enum
12a82 65 72 61 74 65 64 0a 2a 2a 20 63 6f 6e 64 69 74 erated.** condit
12a83 69 6f 6e 73 20 61 62 6f 76 65 20 69 73 20 6e 6f ions above is no
12a84 74 20 74 72 75 65 2c 20 74 68 65 20 70 61 67 65 t true, the page
12a85 72 20 77 61 73 20 69 6e 20 65 72 72 6f 72 20 73 r was in error s
12a86 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 0a 2a tate when this.*
12a87 2a 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 63 * function was c
12a88 61 6c 6c 65 64 2c 20 6f 72 20 62 65 63 61 75 73 alled, or becaus
12a89 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c e the memory all
12a8a 6f 63 61 74 69 6f 6e 20 61 74 74 65 6d 70 74 20 ocation attempt
12a8b 66 61 69 6c 65 64 2c 20 0a 2a 2a 20 74 68 65 6e failed, .** then
12a8c 20 2a 70 50 61 67 65 53 69 7a 65 20 69 73 20 73 *pPageSize is s
12a8d 65 74 20 74 6f 20 74 68 65 20 6f 6c 64 2c 20 72 et to the old, r
12a8e 65 74 61 69 6e 65 64 20 70 61 67 65 20 73 69 7a etained page siz
12a8f 65 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 e before returni
12a90 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ng..*/.SQLITE_PR
12a91 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
12a92 33 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 3PagerSetPagesiz
12a93 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c e(Pager *pPager,
12a94 20 75 31 36 20 2a 70 50 61 67 65 53 69 7a 65 2c u16 *pPageSize,
12a95 20 69 6e 74 20 6e 52 65 73 65 72 76 65 29 7b 0a int nReserve){.
12a96 20 20 69 6e 74 20 72 63 20 3d 20 70 50 61 67 65 int rc = pPage
12a97 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 0a 20 20 69 r->errCode;.. i
12a98 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
12a99 20 29 7b 0a 20 20 20 20 75 31 36 20 70 61 67 65 ){. u16 page
12a9a 53 69 7a 65 20 3d 20 2a 70 50 61 67 65 53 69 7a Size = *pPageSiz
12a9b 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 e;. assert( p
12a9c 61 67 65 53 69 7a 65 3d 3d 30 20 7c 7c 20 28 70 ageSize==0 || (p
12a9d 61 67 65 53 69 7a 65 3e 3d 35 31 32 20 26 26 20 ageSize>=512 &&
12a9e 70 61 67 65 53 69 7a 65 3c 3d 53 51 4c 49 54 45 pageSize<=SQLITE
12a9f 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 29 20 _MAX_PAGE_SIZE)
12aa0 29 3b 0a 20 20 20 20 69 66 28 20 28 70 50 61 67 );. if( (pPag
12aa1 65 72 2d 3e 6d 65 6d 44 62 3d 3d 30 20 7c 7c 20 er->memDb==0 ||
12aa2 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d pPager->dbSize==
12aa3 30 29 0a 20 20 20 20 20 26 26 20 73 71 6c 69 74 0). && sqlit
12aa4 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 e3PcacheRefCount
12aa5 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 (pPager->pPCache
12aa6 29 3d 3d 30 20 0a 20 20 20 20 20 26 26 20 70 61 )==0 . && pa
12aa7 67 65 53 69 7a 65 20 26 26 20 70 61 67 65 53 69 geSize && pageSi
12aa8 7a 65 21 3d 70 50 61 67 65 72 2d 3e 70 61 67 65 ze!=pPager->page
12aa9 53 69 7a 65 20 0a 20 20 20 20 29 7b 0a 20 20 20 Size . ){.
12aaa 20 20 20 63 68 61 72 20 2a 70 4e 65 77 20 3d 20 char *pNew =
12aab 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 50 (char *)sqlite3P
12aac 61 67 65 4d 61 6c 6c 6f 63 28 70 61 67 65 53 69 ageMalloc(pageSi
12aad 7a 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 ze);. if( !
12aae 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 pNew ){.
12aaf 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 rc = SQLITE_NOME
12ab0 4d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a M;. }else{.
12ab1 20 20 20 20 20 20 20 20 70 61 67 65 72 5f 72 65 pager_re
12ab2 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20 set(pPager);.
12ab3 20 20 20 20 20 70 50 61 67 65 72 2d 3e 70 61 67 pPager->pag
12ab4 65 53 69 7a 65 20 3d 20 70 61 67 65 53 69 7a 65 eSize = pageSize
12ab5 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
12ab6 33 50 61 67 65 46 72 65 65 28 70 50 61 67 65 72 3PageFree(pPager
12ab7 2d 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 ->pTmpSpace);.
12ab8 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 70 54 pPager->pT
12ab9 6d 70 53 70 61 63 65 20 3d 20 70 4e 65 77 3b 0a mpSpace = pNew;.
12aba 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 sqlite3P
12abb 63 61 63 68 65 53 65 74 50 61 67 65 53 69 7a 65 cacheSetPageSize
12abc 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 (pPager->pPCache
12abd 2c 20 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 , pageSize);.
12abe 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 2a }. }. *
12abf 70 50 61 67 65 53 69 7a 65 20 3d 20 28 75 31 36 pPageSize = (u16
12ac0 29 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a )pPager->pageSiz
12ac1 65 3b 0a 20 20 20 20 69 66 28 20 6e 52 65 73 65 e;. if( nRese
12ac2 72 76 65 3c 30 20 29 20 6e 52 65 73 65 72 76 65 rve<0 ) nReserve
12ac3 20 3d 20 70 50 61 67 65 72 2d 3e 6e 52 65 73 65 = pPager->nRese
12ac4 72 76 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 rve;. assert(
12ac5 20 6e 52 65 73 65 72 76 65 3e 3d 30 20 26 26 20 nReserve>=0 &&
12ac6 6e 52 65 73 65 72 76 65 3c 31 30 30 30 20 29 3b nReserve<1000 );
12ac7 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 . pPager->nRe
12ac8 73 65 72 76 65 20 3d 20 28 69 31 36 29 6e 52 65 serve = (i16)nRe
12ac9 73 65 72 76 65 3b 0a 20 20 20 20 70 61 67 65 72 serve;. pager
12aca 52 65 70 6f 72 74 53 69 7a 65 28 70 50 61 67 65 ReportSize(pPage
12acb 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e r);. }. return
12acc 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 rc;.}../*.** Re
12acd 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 turn a pointer t
12ace 6f 20 74 68 65 20 22 74 65 6d 70 6f 72 61 72 79 o the "temporary
12acf 20 70 61 67 65 22 20 62 75 66 66 65 72 20 68 65 page" buffer he
12ad0 6c 64 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a ld internally.**
12ad1 20 62 79 20 74 68 65 20 70 61 67 65 72 2e 20 20 by the pager.
12ad2 54 68 69 73 20 69 73 20 61 20 62 75 66 66 65 72 This is a buffer
12ad3 20 74 68 61 74 20 69 73 20 62 69 67 20 65 6e 6f that is big eno
12ad4 75 67 68 20 74 6f 20 68 6f 6c 64 20 74 68 65 0a ugh to hold the.
12ad5 2a 2a 20 65 6e 74 69 72 65 20 63 6f 6e 74 65 6e ** entire conten
12ad6 74 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 t of a database
12ad7 70 61 67 65 2e 20 20 54 68 69 73 20 62 75 66 66 page. This buff
12ad8 65 72 20 69 73 20 75 73 65 64 20 69 6e 74 65 72 er is used inter
12ad9 6e 61 6c 6c 79 0a 2a 2a 20 64 75 72 69 6e 67 20 nally.** during
12ada 72 6f 6c 6c 62 61 63 6b 20 61 6e 64 20 77 69 6c rollback and wil
12adb 6c 20 62 65 20 6f 76 65 72 77 72 69 74 74 65 6e l be overwritten
12adc 20 77 68 65 6e 65 76 65 72 20 61 20 72 6f 6c 6c whenever a roll
12add 62 61 63 6b 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 back.** occurs.
12ade 20 42 75 74 20 6f 74 68 65 72 20 6d 6f 64 75 6c But other modul
12adf 65 73 20 61 72 65 20 66 72 65 65 20 74 6f 20 75 es are free to u
12ae0 73 65 20 69 74 20 74 6f 6f 2c 20 61 73 20 6c 6f se it too, as lo
12ae1 6e 67 20 61 73 0a 2a 2a 20 6e 6f 20 72 6f 6c 6c ng as.** no roll
12ae2 62 61 63 6b 73 20 61 72 65 20 68 61 70 70 65 6e backs are happen
12ae3 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ing..*/.SQLITE_P
12ae4 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c RIVATE void *sql
12ae5 69 74 65 33 50 61 67 65 72 54 65 6d 70 53 70 61 ite3PagerTempSpa
12ae6 63 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 ce(Pager *pPager
12ae7 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 ){. return pPag
12ae8 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 7d er->pTmpSpace;.}
12ae9 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 ../*.** Attempt
12aea 74 6f 20 73 65 74 20 74 68 65 20 6d 61 78 69 6d to set the maxim
12aeb 75 6d 20 64 61 74 61 62 61 73 65 20 70 61 67 65 um database page
12aec 20 63 6f 75 6e 74 20 69 66 20 6d 78 50 61 67 65 count if mxPage
12aed 20 69 73 20 70 6f 73 69 74 69 76 65 2e 20 0a 2a is positive. .*
12aee 2a 20 4d 61 6b 65 20 6e 6f 20 63 68 61 6e 67 65 * Make no change
12aef 73 20 69 66 20 6d 78 50 61 67 65 20 69 73 20 7a s if mxPage is z
12af0 65 72 6f 20 6f 72 20 6e 65 67 61 74 69 76 65 2e ero or negative.
12af1 20 20 41 6e 64 20 6e 65 76 65 72 20 72 65 64 75 And never redu
12af2 63 65 20 74 68 65 0a 2a 2a 20 6d 61 78 69 6d 75 ce the.** maximu
12af3 6d 20 70 61 67 65 20 63 6f 75 6e 74 20 62 65 6c m page count bel
12af4 6f 77 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 ow the current s
12af5 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ize of the datab
12af6 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 67 61 72 ase..**.** Regar
12af7 64 6c 65 73 73 20 6f 66 20 6d 78 50 61 67 65 2c dless of mxPage,
12af8 20 72 65 74 75 72 6e 20 74 68 65 20 63 75 72 72 return the curr
12af9 65 6e 74 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 ent maximum page
12afa 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 count..*/.SQLIT
12afb 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
12afc 6c 69 74 65 33 50 61 67 65 72 4d 61 78 50 61 67 lite3PagerMaxPag
12afd 65 43 6f 75 6e 74 28 50 61 67 65 72 20 2a 70 50 eCount(Pager *pP
12afe 61 67 65 72 2c 20 69 6e 74 20 6d 78 50 61 67 65 ager, int mxPage
12aff 29 7b 0a 20 20 69 66 28 20 6d 78 50 61 67 65 3e ){. if( mxPage>
12b00 30 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 0 ){. pPager-
12b01 3e 6d 78 50 67 6e 6f 20 3d 20 6d 78 50 61 67 65 >mxPgno = mxPage
12b02 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 50 ;. }. sqlite3P
12b03 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 agerPagecount(pP
12b04 61 67 65 72 2c 20 30 29 3b 0a 20 20 72 65 74 75 ager, 0);. retu
12b05 72 6e 20 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e rn pPager->mxPgn
12b06 6f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 o;.}../*.** The
12b07 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 74 20 6f 66 following set of
12b08 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 routines are us
12b09 65 64 20 74 6f 20 64 69 73 61 62 6c 65 20 74 68 ed to disable th
12b0a 65 20 73 69 6d 75 6c 61 74 65 64 0a 2a 2a 20 49 e simulated.** I
12b0b 2f 4f 20 65 72 72 6f 72 20 6d 65 63 68 61 6e 69 /O error mechani
12b0c 73 6d 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 sm. These routi
12b0d 6e 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 nes are used to
12b0e 61 76 6f 69 64 20 73 69 6d 75 6c 61 74 65 64 0a avoid simulated.
12b0f 2a 2a 20 65 72 72 6f 72 73 20 69 6e 20 70 6c 61 ** errors in pla
12b10 63 65 73 20 77 68 65 72 65 20 77 65 20 64 6f 20 ces where we do
12b11 6e 6f 74 20 63 61 72 65 20 61 62 6f 75 74 20 65 not care about e
12b12 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c rrors..**.** Unl
12b13 65 73 73 20 2d 44 53 51 4c 49 54 45 5f 54 45 53 ess -DSQLITE_TES
12b14 54 3d 31 20 69 73 20 75 73 65 64 2c 20 74 68 65 T=1 is used, the
12b15 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 se routines are
12b16 61 6c 6c 20 6e 6f 2d 6f 70 73 0a 2a 2a 20 61 6e all no-ops.** an
12b17 64 20 67 65 6e 65 72 61 74 65 20 6e 6f 20 63 6f d generate no co
12b18 64 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 de..*/.#ifdef SQ
12b19 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 LITE_TEST.SQLITE
12b1a 5f 41 50 49 20 65 78 74 65 72 6e 20 69 6e 74 20 _API extern int
12b1b 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
12b1c 5f 70 65 6e 64 69 6e 67 3b 0a 53 51 4c 49 54 45 _pending;.SQLITE
12b1d 5f 41 50 49 20 65 78 74 65 72 6e 20 69 6e 74 20 _API extern int
12b1e 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
12b1f 5f 68 69 74 3b 0a 73 74 61 74 69 63 20 69 6e 74 _hit;.static int
12b20 20 73 61 76 65 64 5f 63 6e 74 3b 0a 76 6f 69 64 saved_cnt;.void
12b21 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 disable_simulat
12b22 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69 ed_io_errors(voi
12b23 64 29 7b 0a 20 20 73 61 76 65 64 5f 63 6e 74 20 d){. saved_cnt
12b24 3d 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 = sqlite3_io_err
12b25 6f 72 5f 70 65 6e 64 69 6e 67 3b 0a 20 20 73 71 or_pending;. sq
12b26 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 lite3_io_error_p
12b27 65 6e 64 69 6e 67 20 3d 20 2d 31 3b 0a 7d 0a 76 ending = -1;.}.v
12b28 6f 69 64 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c oid enable_simul
12b29 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 76 ated_io_errors(v
12b2a 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f oid){. sqlite3_
12b2b 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 io_error_pending
12b2c 20 3d 20 73 61 76 65 64 5f 63 6e 74 3b 0a 7d 0a = saved_cnt;.}.
12b2d 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 64 #else.# define d
12b2e 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 isable_simulated
12b2f 5f 69 6f 5f 65 72 72 6f 72 73 28 29 0a 23 20 64 _io_errors().# d
12b30 65 66 69 6e 65 20 65 6e 61 62 6c 65 5f 73 69 6d efine enable_sim
12b31 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 ulated_io_errors
12b32 28 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a ().#endif../*.**
12b33 20 52 65 61 64 20 74 68 65 20 66 69 72 73 74 20 Read the first
12b34 4e 20 62 79 74 65 73 20 66 72 6f 6d 20 74 68 65 N bytes from the
12b35 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 beginning of th
12b36 65 20 66 69 6c 65 20 69 6e 74 6f 20 6d 65 6d 6f e file into memo
12b37 72 79 0a 2a 2a 20 74 68 61 74 20 70 44 65 73 74 ry.** that pDest
12b38 20 70 6f 69 6e 74 73 20 74 6f 2e 20 0a 2a 2a 0a points to. .**.
12b39 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 ** If the pager
12b3a 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 was opened on a
12b3b 74 72 61 6e 73 69 65 6e 74 20 66 69 6c 65 20 28 transient file (
12b3c 7a 46 69 6c 65 6e 61 6d 65 3d 3d 22 22 29 2c 20 zFilename==""),
12b3d 6f 72 0a 2a 2a 20 6f 70 65 6e 65 64 20 6f 6e 20 or.** opened on
12b3e 61 20 66 69 6c 65 20 6c 65 73 73 20 74 68 61 6e a file less than
12b3f 20 4e 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 N bytes in size
12b40 2c 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 , the output buf
12b41 66 65 72 20 69 73 0a 2a 2a 20 7a 65 72 6f 65 64 fer is.** zeroed
12b42 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 and SQLITE_OK r
12b43 65 74 75 72 6e 65 64 2e 20 54 68 65 20 72 61 74 eturned. The rat
12b44 69 6f 6e 61 6c 65 20 66 6f 72 20 74 68 69 73 20 ionale for this
12b45 69 73 20 74 68 61 74 20 74 68 69 73 20 0a 2a 2a is that this .**
12b46 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 function is use
12b47 64 20 74 6f 20 72 65 61 64 20 64 61 74 61 62 61 d to read databa
12b48 73 65 20 68 65 61 64 65 72 73 2c 20 61 6e 64 20 se headers, and
12b49 61 20 6e 65 77 20 74 72 61 6e 73 69 65 6e 74 20 a new transient
12b4a 6f 72 0a 2a 2a 20 7a 65 72 6f 20 73 69 7a 65 64 or.** zero sized
12b4b 20 64 61 74 61 62 61 73 65 20 68 61 73 20 61 20 database has a
12b4c 68 65 61 64 65 72 20 74 68 61 6e 20 63 6f 6e 73 header than cons
12b4d 69 73 74 73 20 65 6e 74 69 72 65 6c 79 20 6f 66 ists entirely of
12b4e 20 7a 65 72 6f 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 zeroes..**.** I
12b4f 66 20 61 6e 79 20 49 4f 20 65 72 72 6f 72 20 61 f any IO error a
12b50 70 61 72 74 20 66 72 6f 6d 20 53 51 4c 49 54 45 part from SQLITE
12b51 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 _IOERR_SHORT_REA
12b52 44 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 D is encountered
12b53 2c 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 63 ,.** the error c
12b54 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 ode is returned
12b55 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 20 61 6e to the caller an
12b56 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f d the contents o
12b57 66 20 74 68 65 0a 2a 2a 20 6f 75 74 70 75 74 20 f the.** output
12b58 62 75 66 66 65 72 20 75 6e 64 65 66 69 6e 65 64 buffer undefined
12b59 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
12b5a 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
12b5b 61 67 65 72 52 65 61 64 46 69 6c 65 68 65 61 64 agerReadFilehead
12b5c 65 72 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 er(Pager *pPager
12b5d 2c 20 69 6e 74 20 4e 2c 20 75 6e 73 69 67 6e 65 , int N, unsigne
12b5e 64 20 63 68 61 72 20 2a 70 44 65 73 74 29 7b 0a d char *pDest){.
12b5f 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
12b60 45 5f 4f 4b 3b 0a 20 20 6d 65 6d 73 65 74 28 70 E_OK;. memset(p
12b61 44 65 73 74 2c 20 30 2c 20 4e 29 3b 0a 20 20 61 Dest, 0, N);. a
12b62 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 ssert( isOpen(pP
12b63 61 67 65 72 2d 3e 66 64 29 20 7c 7c 20 70 50 61 ager->fd) || pPa
12b64 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b ger->tempFile );
12b65 0a 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 . if( isOpen(pP
12b66 61 67 65 72 2d 3e 66 64 29 20 29 7b 0a 20 20 20 ager->fd) ){.
12b67 20 49 4f 54 52 41 43 45 28 28 22 44 42 48 44 52 IOTRACE(("DBHDR
12b68 20 25 70 20 30 20 25 64 5c 6e 22 2c 20 70 50 61 %p 0 %d\n", pPa
12b69 67 65 72 2c 20 4e 29 29 0a 20 20 20 20 72 63 20 ger, N)). rc
12b6a 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 = sqlite3OsRead(
12b6b 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 44 65 73 pPager->fd, pDes
12b6c 74 2c 20 4e 2c 20 30 29 3b 0a 20 20 20 20 69 66 t, N, 0);. if
12b6d 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 ( rc==SQLITE_IOE
12b6e 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 29 7b RR_SHORT_READ ){
12b6f 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
12b70 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d TE_OK;. }. }
12b71 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
12b72 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
12b73 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f e total number o
12b74 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 f pages in the d
12b75 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 73 73 atabase file ass
12b76 6f 63 69 61 74 65 64 20 0a 2a 2a 20 77 69 74 68 ociated .** with
12b77 20 70 50 61 67 65 72 2e 20 4e 6f 72 6d 61 6c 6c pPager. Normall
12b78 79 2c 20 74 68 69 73 20 69 73 20 63 61 6c 63 75 y, this is calcu
12b79 6c 61 74 65 64 20 61 73 20 28 3c 64 62 20 66 69 lated as (<db fi
12b7a 6c 65 20 73 69 7a 65 3e 2f 3c 70 61 67 65 2d 73 le size>/<page-s
12b7b 69 7a 65 3e 29 2e 0a 2a 2a 20 48 6f 77 65 76 65 ize>)..** Howeve
12b7c 72 2c 20 69 66 20 74 68 65 20 66 69 6c 65 20 69 r, if the file i
12b7d 73 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 s between 1 and
12b7e 3c 70 61 67 65 2d 73 69 7a 65 3e 20 62 79 74 65 <page-size> byte
12b7f 73 20 69 6e 20 73 69 7a 65 2c 20 74 68 65 6e 20 s in size, then
12b80 0a 2a 2a 20 74 68 69 73 20 69 73 20 63 6f 6e 73 .** this is cons
12b81 69 64 65 72 65 64 20 61 20 31 20 70 61 67 65 20 idered a 1 page
12b82 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 file..**.** If t
12b83 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 65 he pager is in e
12b84 72 72 6f 72 20 73 74 61 74 65 20 77 68 65 6e 20 rror state when
12b85 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
12b86 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 74 68 called, then th
12b87 65 0a 2a 2a 20 65 72 72 6f 72 20 73 74 61 74 65 e.** error state
12b88 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 error code is r
12b89 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 6e 50 eturned and *pnP
12b8a 61 67 65 20 6c 65 66 74 20 75 6e 63 68 61 6e 67 age left unchang
12b8b 65 64 2e 20 4f 72 2c 0a 2a 2a 20 69 66 20 74 68 ed. Or,.** if th
12b8c 65 20 66 69 6c 65 20 73 79 73 74 65 6d 20 68 61 e file system ha
12b8d 73 20 74 6f 20 62 65 20 71 75 65 72 69 65 64 20 s to be queried
12b8e 66 6f 72 20 74 68 65 20 73 69 7a 65 20 6f 66 20 for the size of
12b8f 74 68 65 20 66 69 6c 65 20 61 6e 64 0a 2a 2a 20 the file and.**
12b90 74 68 65 20 71 75 65 72 79 20 61 74 74 65 6d 70 the query attemp
12b91 74 20 72 65 74 75 72 6e 73 20 61 6e 20 49 4f 20 t returns an IO
12b92 65 72 72 6f 72 2c 20 74 68 65 20 49 4f 20 65 72 error, the IO er
12b93 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 ror code is retu
12b94 72 6e 65 64 0a 2a 2a 20 61 6e 64 20 2a 70 6e 50 rned.** and *pnP
12b95 61 67 65 20 69 73 20 6c 65 66 74 20 75 6e 63 68 age is left unch
12b96 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 anged..**.** Oth
12b97 65 72 77 69 73 65 2c 20 69 66 20 65 76 65 72 79 erwise, if every
12b98 74 68 69 6e 67 20 69 73 20 73 75 63 63 65 73 73 thing is success
12b99 66 75 6c 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 ful, then SQLITE
12b9a 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 0a _OK is returned.
12b9b 2a 2a 20 61 6e 64 20 2a 70 6e 50 61 67 65 20 69 ** and *pnPage i
12b9c 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d s set to the num
12b9d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 ber of pages in
12b9e 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f the database..*/
12b9f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
12ba0 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
12ba1 50 61 67 65 63 6f 75 6e 74 28 50 61 67 65 72 20 Pagecount(Pager
12ba2 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 2a 70 6e *pPager, int *pn
12ba3 50 61 67 65 29 7b 0a 20 20 50 67 6e 6f 20 6e 50 Page){. Pgno nP
12ba4 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 age;
12ba5 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 /* Value to r
12ba6 65 74 75 72 6e 20 76 69 61 20 2a 70 6e 50 61 67 eturn via *pnPag
12ba7 65 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 e */.. /* If th
12ba8 65 20 70 61 67 65 72 20 69 73 20 61 6c 72 65 61 e pager is alrea
12ba9 64 79 20 69 6e 20 74 68 65 20 65 72 72 6f 72 20 dy in the error
12baa 73 74 61 74 65 2c 20 72 65 74 75 72 6e 20 74 68 state, return th
12bab 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 2a 2f e error code. */
12bac 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 . if( pPager->e
12bad 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 72 65 rrCode ){. re
12bae 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 turn pPager->err
12baf 43 6f 64 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 Code;. }.. /*
12bb0 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20 6e 75 Determine the nu
12bb1 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e mber of pages in
12bb2 20 74 68 65 20 66 69 6c 65 2e 20 53 74 6f 72 65 the file. Store
12bb3 20 74 68 69 73 20 69 6e 20 6e 50 61 67 65 2e 20 this in nPage.
12bb4 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d */. if( pPager-
12bb5 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 29 7b 0a >dbSizeValid ){.
12bb6 20 20 20 20 6e 50 61 67 65 20 3d 20 70 50 61 67 nPage = pPag
12bb7 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 20 20 7d 65 er->dbSize;. }e
12bb8 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b lse{. int rc;
12bb9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12bba 20 2f 2a 20 45 72 72 6f 72 20 72 65 74 75 72 6e /* Error return
12bbb 65 64 20 62 79 20 4f 73 46 69 6c 65 53 69 7a 65 ed by OsFileSize
12bbc 28 29 20 2a 2f 0a 20 20 20 20 69 36 34 20 6e 20 () */. i64 n
12bbd 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 = 0;
12bbe 20 20 2f 2a 20 46 69 6c 65 20 73 69 7a 65 20 69 /* File size i
12bbf 6e 20 62 79 74 65 73 20 72 65 74 75 72 6e 65 64 n bytes returned
12bc0 20 62 79 20 4f 73 46 69 6c 65 53 69 7a 65 28 29 by OsFileSize()
12bc1 20 2a 2f 0a 0a 20 20 20 20 61 73 73 65 72 74 28 */.. assert(
12bc2 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e isOpen(pPager->
12bc3 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74 fd) || pPager->t
12bc4 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20 20 20 69 empFile );. i
12bc5 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 f( isOpen(pPager
12bc6 2d 3e 66 64 29 20 26 26 20 28 30 20 21 3d 20 28 ->fd) && (0 != (
12bc7 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 rc = sqlite3OsFi
12bc8 6c 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 66 leSize(pPager->f
12bc9 64 2c 20 26 6e 29 29 29 20 29 7b 0a 20 20 20 20 d, &n))) ){.
12bca 20 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 pager_error(pP
12bcb 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20 20 20 ager, rc);.
12bcc 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
12bcd 7d 0a 20 20 20 20 69 66 28 20 6e 3e 30 20 26 26 }. if( n>0 &&
12bce 20 6e 3c 70 50 61 67 65 72 2d 3e 70 61 67 65 53 n<pPager->pageS
12bcf 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 6e 50 61 ize ){. nPa
12bd0 67 65 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 ge = 1;. }els
12bd1 65 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 20 3d e{. nPage =
12bd2 20 28 50 67 6e 6f 29 28 6e 20 2f 20 70 50 61 67 (Pgno)(n / pPag
12bd3 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 er->pageSize);.
12bd4 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 50 61 }. if( pPa
12bd5 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 45 ger->state!=PAGE
12bd6 52 5f 55 4e 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 R_UNLOCK ){.
12bd7 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 pPager->dbSize
12bd8 20 3d 20 6e 50 61 67 65 3b 0a 20 20 20 20 20 20 = nPage;.
12bd9 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 pPager->dbFileSi
12bda 7a 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20 20 20 ze = nPage;.
12bdb 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 pPager->dbSize
12bdc 56 61 6c 69 64 20 3d 20 31 3b 0a 20 20 20 20 7d Valid = 1;. }
12bdd 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 . }.. /* If th
12bde 65 20 63 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 e current number
12bdf 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 of pages in the
12be0 20 66 69 6c 65 20 69 73 20 67 72 65 61 74 65 72 file is greater
12be1 20 74 68 61 6e 20 74 68 65 20 0a 20 20 2a 2a 20 than the . **
12be2 63 6f 6e 66 69 67 75 72 65 64 20 6d 61 78 69 6d configured maxim
12be3 75 6d 20 70 61 67 65 72 20 6e 75 6d 62 65 72 2c um pager number,
12be4 20 69 6e 63 72 65 61 73 65 20 74 68 65 20 61 6c increase the al
12be5 6c 6f 77 65 64 20 6c 69 6d 69 74 20 73 6f 0a 20 lowed limit so.
12be6 20 2a 2a 20 74 68 61 74 20 74 68 65 20 66 69 6c ** that the fil
12be7 65 20 63 61 6e 20 62 65 20 72 65 61 64 2e 0a 20 e can be read..
12be8 20 2a 2f 0a 20 20 69 66 28 20 6e 50 61 67 65 3e */. if( nPage>
12be9 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 29 pPager->mxPgno )
12bea 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6d 78 {. pPager->mx
12beb 50 67 6e 6f 20 3d 20 28 50 67 6e 6f 29 6e 50 61 Pgno = (Pgno)nPa
12bec 67 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 ge;. }.. /* Se
12bed 74 20 74 68 65 20 6f 75 74 70 75 74 20 76 61 72 t the output var
12bee 69 61 62 6c 65 20 61 6e 64 20 72 65 74 75 72 6e iable and return
12bef 20 53 51 4c 49 54 45 5f 4f 4b 20 2a 2f 0a 20 20 SQLITE_OK */.
12bf0 69 66 28 20 70 6e 50 61 67 65 20 29 7b 0a 20 20 if( pnPage ){.
12bf1 20 20 2a 70 6e 50 61 67 65 20 3d 20 6e 50 61 67 *pnPage = nPag
12bf2 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 e;. }. return
12bf3 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f SQLITE_OK;.}.../
12bf4 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 6f 62 74 61 *.** Try to obta
12bf5 69 6e 20 61 20 6c 6f 63 6b 20 6f 66 20 74 79 70 in a lock of typ
12bf6 65 20 6c 6f 63 6b 74 79 70 65 20 6f 6e 20 74 68 e locktype on th
12bf7 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
12bf8 20 49 66 0a 2a 2a 20 61 20 73 69 6d 69 6c 61 72 If.** a similar
12bf9 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b or greater lock
12bfa 20 69 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64 is already held
12bfb 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 , this function
12bfc 69 73 20 61 20 6e 6f 2d 6f 70 0a 2a 2a 20 28 72 is a no-op.** (r
12bfd 65 74 75 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f eturning SQLITE_
12bfe 4f 4b 20 69 6d 6d 65 64 69 61 74 65 6c 79 29 2e OK immediately).
12bff 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 .**.** Otherwise
12c00 2c 20 61 74 74 65 6d 70 74 20 74 6f 20 6f 62 74 , attempt to obt
12c01 61 69 6e 20 74 68 65 20 6c 6f 63 6b 20 75 73 69 ain the lock usi
12c02 6e 67 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b ng sqlite3OsLock
12c03 28 29 2e 20 49 6e 76 6f 6b 65 20 0a 2a 2a 20 74 (). Invoke .** t
12c04 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b he busy callback
12c05 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 if the lock is
12c06 63 75 72 72 65 6e 74 6c 79 20 6e 6f 74 20 61 76 currently not av
12c07 61 69 6c 61 62 6c 65 2e 20 52 65 70 65 61 74 20 ailable. Repeat
12c08 0a 2a 2a 20 75 6e 74 69 6c 20 74 68 65 20 62 75 .** until the bu
12c09 73 79 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 sy callback retu
12c0a 72 6e 73 20 66 61 6c 73 65 20 6f 72 20 75 6e 74 rns false or unt
12c0b 69 6c 20 74 68 65 20 61 74 74 65 6d 70 74 20 74 il the attempt t
12c0c 6f 20 0a 2a 2a 20 6f 62 74 61 69 6e 20 74 68 65 o .** obtain the
12c0d 20 6c 6f 63 6b 20 73 75 63 63 65 65 64 73 2e 0a lock succeeds..
12c0e 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c **.** Return SQL
12c0f 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 ITE_OK on succes
12c10 73 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 s and an error c
12c11 6f 64 65 20 69 66 20 77 65 20 63 61 6e 6e 6f 74 ode if we cannot
12c12 20 6f 62 74 61 69 6e 0a 2a 2a 20 74 68 65 20 6c obtain.** the l
12c13 6f 63 6b 2e 20 49 66 20 74 68 65 20 6c 6f 63 6b ock. If the lock
12c14 20 69 73 20 6f 62 74 61 69 6e 65 64 20 73 75 63 is obtained suc
12c15 63 65 73 73 66 75 6c 6c 79 2c 20 73 65 74 20 74 cessfully, set t
12c16 68 65 20 50 61 67 65 72 2e 73 74 61 74 65 20 0a he Pager.state .
12c17 2a 2a 20 76 61 72 69 61 62 6c 65 20 74 6f 20 6c ** variable to l
12c18 6f 63 6b 74 79 70 65 20 62 65 66 6f 72 65 20 72 ocktype before r
12c19 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 eturning..*/.sta
12c1a 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 77 61 tic int pager_wa
12c1b 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 50 61 67 65 72 it_on_lock(Pager
12c1c 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6c 6f *pPager, int lo
12c1d 63 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74 20 72 cktype){. int r
12c1e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
12c1f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12c20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a /* Return code *
12c21 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 4f 53 20 6c /.. /* The OS l
12c22 6f 63 6b 20 76 61 6c 75 65 73 20 6d 75 73 74 20 ock values must
12c23 62 65 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 be the same as t
12c24 68 65 20 50 61 67 65 72 20 6c 6f 63 6b 20 76 61 he Pager lock va
12c25 6c 75 65 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 lues */. assert
12c26 28 20 50 41 47 45 52 5f 53 48 41 52 45 44 3d 3d ( PAGER_SHARED==
12c27 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 SHARED_LOCK );.
12c28 20 61 73 73 65 72 74 28 20 50 41 47 45 52 5f 52 assert( PAGER_R
12c29 45 53 45 52 56 45 44 3d 3d 52 45 53 45 52 56 45 ESERVED==RESERVE
12c2a 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 D_LOCK );. asse
12c2b 72 74 28 20 50 41 47 45 52 5f 45 58 43 4c 55 53 rt( PAGER_EXCLUS
12c2c 49 56 45 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c IVE==EXCLUSIVE_L
12c2d 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 OCK );.. /* If
12c2e 74 68 65 20 66 69 6c 65 20 69 73 20 63 75 72 72 the file is curr
12c2f 65 6e 74 6c 79 20 75 6e 6c 6f 63 6b 65 64 20 74 ently unlocked t
12c30 68 65 6e 20 74 68 65 20 73 69 7a 65 20 6d 75 73 hen the size mus
12c31 74 20 62 65 20 75 6e 6b 6e 6f 77 6e 20 2a 2f 0a t be unknown */.
12c32 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
12c33 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 ->state>=PAGER_S
12c34 48 41 52 45 44 20 7c 7c 20 70 50 61 67 65 72 2d HARED || pPager-
12c35 3e 64 62 53 69 7a 65 56 61 6c 69 64 3d 3d 30 20 >dbSizeValid==0
12c36 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 );.. /* Check t
12c37 68 61 74 20 74 68 69 73 20 69 73 20 65 69 74 68 hat this is eith
12c38 65 72 20 61 20 6e 6f 2d 6f 70 20 28 62 65 63 61 er a no-op (beca
12c39 75 73 65 20 74 68 65 20 72 65 71 75 65 73 74 65 use the requeste
12c3a 64 20 6c 6f 63 6b 20 69 73 20 0a 20 20 2a 2a 20 d lock is . **
12c3b 61 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 6f 72 already held, or
12c3c 20 6f 6e 65 20 6f 66 20 74 68 65 20 74 72 61 6e one of the tran
12c3d 73 69 73 74 69 6f 6e 73 20 74 68 61 74 20 74 68 sistions that th
12c3e 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 0a 20 e busy-handler.
12c3f 20 2a 2a 20 6d 61 79 20 62 65 20 69 6e 76 6f 6b ** may be invok
12c40 65 64 20 64 75 72 69 6e 67 2c 20 61 63 63 6f 72 ed during, accor
12c41 64 69 6e 67 20 74 6f 20 74 68 65 20 63 6f 6d 6d ding to the comm
12c42 65 6e 74 20 61 62 6f 76 65 0a 20 20 2a 2a 20 73 ent above. ** s
12c43 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 42 75 qlite3PagerSetBu
12c44 73 79 68 61 6e 64 6c 65 72 28 29 2e 0a 20 20 2a syhandler().. *
12c45 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70 50 61 /. assert( (pPa
12c46 67 65 72 2d 3e 73 74 61 74 65 3e 3d 6c 6f 63 6b ger->state>=lock
12c47 74 79 70 65 29 0a 20 20 20 20 20 20 20 7c 7c 20 type). ||
12c48 28 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d (pPager->state==
12c49 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 26 26 20 PAGER_UNLOCK &&
12c4a 6c 6f 63 6b 74 79 70 65 3d 3d 50 41 47 45 52 5f locktype==PAGER_
12c4b 53 48 41 52 45 44 29 0a 20 20 20 20 20 20 20 7c SHARED). |
12c4c 7c 20 28 70 50 61 67 65 72 2d 3e 73 74 61 74 65 | (pPager->state
12c4d 3d 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 ==PAGER_RESERVED
12c4e 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d 3d 50 41 && locktype==PA
12c4f 47 45 52 5f 45 58 43 4c 55 53 49 56 45 29 0a 20 GER_EXCLUSIVE).
12c50 20 29 3b 0a 0a 20 20 69 66 28 20 70 50 61 67 65 );.. if( pPage
12c51 72 2d 3e 73 74 61 74 65 3e 3d 6c 6f 63 6b 74 79 r->state>=lockty
12c52 70 65 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 pe ){. rc = S
12c53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 QLITE_OK;. }els
12c54 65 7b 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 e{. do {.
12c55 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
12c56 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c Lock(pPager->fd,
12c57 20 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 20 20 locktype);.
12c58 7d 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 }while( rc==SQLI
12c59 54 45 5f 42 55 53 59 20 26 26 20 70 50 61 67 65 TE_BUSY && pPage
12c5a 72 2d 3e 78 42 75 73 79 48 61 6e 64 6c 65 72 28 r->xBusyHandler(
12c5b 70 50 61 67 65 72 2d 3e 70 42 75 73 79 48 61 6e pPager->pBusyHan
12c5c 64 6c 65 72 41 72 67 29 20 29 3b 0a 20 20 20 20 dlerArg) );.
12c5d 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
12c5e 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 K ){. pPage
12c5f 72 2d 3e 73 74 61 74 65 20 3d 20 28 75 38 29 6c r->state = (u8)l
12c60 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 20 20 49 ocktype;. I
12c61 4f 54 52 41 43 45 28 28 22 4c 4f 43 4b 20 25 70 OTRACE(("LOCK %p
12c62 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 %d\n", pPager,
12c63 6c 6f 63 6b 74 79 70 65 29 29 0a 20 20 20 20 7d locktype)). }
12c64 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
12c65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 ;.}../*.** Funct
12c66 69 6f 6e 20 61 73 73 65 72 74 54 72 75 6e 63 61 ion assertTrunca
12c67 74 65 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 61 teConstraint(pPa
12c68 67 65 72 29 20 63 68 65 63 6b 73 20 74 68 61 74 ger) checks that
12c69 20 6f 6e 65 20 6f 66 20 74 68 65 20 0a 2a 2a 20 one of the .**
12c6a 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 74 72 75 following is tru
12c6b 65 20 66 6f 72 20 61 6c 6c 20 64 69 72 74 79 20 e for all dirty
12c6c 70 61 67 65 73 20 63 75 72 72 65 6e 74 6c 79 20 pages currently
12c6d 69 6e 20 74 68 65 20 70 61 67 65 2d 63 61 63 68 in the page-cach
12c6e 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 29 20 54 68 e:.**.** a) Th
12c6f 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 e page number is
12c70 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 less than or eq
12c71 75 61 6c 20 74 6f 20 74 68 65 20 73 69 7a 65 20 ual to the size
12c72 6f 66 20 74 68 65 20 0a 2a 2a 20 20 20 20 20 20 of the .**
12c73 63 75 72 72 65 6e 74 20 64 61 74 61 62 61 73 65 current database
12c74 20 69 6d 61 67 65 2c 20 69 6e 20 70 61 67 65 73 image, in pages
12c75 2c 20 4f 52 0a 2a 2a 0a 2a 2a 20 20 20 62 29 20 , OR.**.** b)
12c76 69 66 20 74 68 65 20 70 61 67 65 20 63 6f 6e 74 if the page cont
12c77 65 6e 74 20 77 65 72 65 20 77 72 69 74 74 65 6e ent were written
12c78 20 61 74 20 74 68 69 73 20 74 69 6d 65 2c 20 69 at this time, i
12c79 74 20 77 6f 75 6c 64 20 6e 6f 74 0a 2a 2a 20 20 t would not.**
12c7a 20 20 20 20 62 65 20 6e 65 63 65 73 73 61 72 79 be necessary
12c7b 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 63 75 to write the cu
12c7c 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 20 6f 75 rrent content ou
12c7d 74 20 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 t to the sub-jou
12c7e 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 28 61 73 rnal.** (as
12c7f 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 66 determined by f
12c80 75 6e 63 74 69 6f 6e 20 73 75 62 6a 52 65 71 75 unction subjRequ
12c81 69 72 65 73 50 61 67 65 28 29 29 2e 0a 2a 2a 0a iresPage())..**.
12c82 2a 2a 20 49 66 20 74 68 65 20 63 6f 6e 64 69 74 ** If the condit
12c83 69 6f 6e 20 61 73 73 65 72 74 65 64 20 62 79 20 ion asserted by
12c84 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 65 this function we
12c85 72 65 20 6e 6f 74 20 74 72 75 65 2c 20 61 6e 64 re not true, and
12c86 20 74 68 65 0a 2a 2a 20 64 69 72 74 79 20 70 61 the.** dirty pa
12c87 67 65 20 77 65 72 65 20 74 6f 20 62 65 20 64 69 ge were to be di
12c88 73 63 61 72 64 65 64 20 66 72 6f 6d 20 74 68 65 scarded from the
12c89 20 63 61 63 68 65 20 76 69 61 20 74 68 65 20 70 cache via the p
12c8a 61 67 65 72 53 74 72 65 73 73 28 29 0a 2a 2a 20 agerStress().**
12c8b 72 6f 75 74 69 6e 65 2c 20 70 61 67 65 72 53 74 routine, pagerSt
12c8c 72 65 73 73 28 29 20 77 6f 75 6c 64 20 6e 6f 74 ress() would not
12c8d 20 77 72 69 74 65 20 74 68 65 20 63 75 72 72 65 write the curre
12c8e 6e 74 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 nt page content
12c8f 74 6f 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 to.** the databa
12c90 73 65 20 66 69 6c 65 2e 20 49 66 20 61 20 73 61 se file. If a sa
12c91 76 65 70 6f 69 6e 74 20 74 72 61 6e 73 61 63 74 vepoint transact
12c92 69 6f 6e 20 77 65 72 65 20 72 6f 6c 6c 65 64 20 ion were rolled
12c93 62 61 63 6b 20 61 66 74 65 72 0a 2a 2a 20 74 68 back after.** th
12c94 69 73 20 68 61 70 70 65 6e 65 64 2c 20 74 68 65 is happened, the
12c95 20 63 6f 72 72 65 63 74 20 62 65 68 61 76 69 6f correct behavio
12c96 75 72 20 77 6f 75 6c 64 20 62 65 20 74 6f 20 72 ur would be to r
12c97 65 73 74 6f 72 65 20 74 68 65 20 63 75 72 72 65 estore the curre
12c98 6e 74 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 nt.** content of
12c99 20 74 68 65 20 70 61 67 65 2e 20 48 6f 77 65 76 the page. Howev
12c9a 65 72 2c 20 73 69 6e 63 65 20 74 68 69 73 20 63 er, since this c
12c9b 6f 6e 74 65 6e 74 20 69 73 20 6e 6f 74 20 70 72 ontent is not pr
12c9c 65 73 65 6e 74 20 69 6e 20 65 69 74 68 65 72 0a esent in either.
12c9d 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ** the database
12c9e 66 69 6c 65 20 6f 72 20 74 68 65 20 70 6f 72 74 file or the port
12c9f 69 6f 6e 20 6f 66 20 74 68 65 20 72 6f 6c 6c 62 ion of the rollb
12ca0 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 ack journal and
12ca1 0a 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 .** sub-journal
12ca2 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 68 65 20 rolled back the
12ca3 63 6f 6e 74 65 6e 74 20 63 6f 75 6c 64 20 6e 6f content could no
12ca4 74 20 62 65 20 72 65 73 74 6f 72 65 64 20 61 6e t be restored an
12ca5 64 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 d the.** databas
12ca6 65 20 69 6d 61 67 65 20 77 6f 75 6c 64 20 62 65 e image would be
12ca7 63 6f 6d 65 20 63 6f 72 72 75 70 74 2e 20 49 74 come corrupt. It
12ca8 20 69 73 20 74 68 65 72 65 66 6f 72 65 20 66 6f is therefore fo
12ca9 72 74 75 6e 61 74 65 20 74 68 61 74 20 0a 2a 2a rtunate that .**
12caa 20 74 68 69 73 20 63 69 72 63 75 6d 73 74 61 6e this circumstan
12cab 63 65 20 63 61 6e 6e 6f 74 20 61 72 69 73 65 2e ce cannot arise.
12cac 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 .*/.#if defined(
12cad 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 73 74 SQLITE_DEBUG).st
12cae 61 74 69 63 20 76 6f 69 64 20 61 73 73 65 72 74 atic void assert
12caf 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 TruncateConstrai
12cb0 6e 74 43 62 28 50 67 48 64 72 20 2a 70 50 67 29 ntCb(PgHdr *pPg)
12cb1 7b 0a 20 20 61 73 73 65 72 74 28 20 70 50 67 2d {. assert( pPg-
12cb2 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 >flags&PGHDR_DIR
12cb3 54 59 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 TY );. assert(
12cb4 21 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 67 !subjRequiresPag
12cb5 65 28 70 50 67 29 20 7c 7c 20 70 50 67 2d 3e 70 e(pPg) || pPg->p
12cb6 67 6e 6f 3c 3d 70 50 67 2d 3e 70 50 61 67 65 72 gno<=pPg->pPager
12cb7 2d 3e 64 62 53 69 7a 65 20 29 3b 0a 7d 0a 73 74 ->dbSize );.}.st
12cb8 61 74 69 63 20 76 6f 69 64 20 61 73 73 65 72 74 atic void assert
12cb9 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 TruncateConstrai
12cba 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 nt(Pager *pPager
12cbb 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 ){. sqlite3Pcac
12cbc 68 65 49 74 65 72 61 74 65 44 69 72 74 79 28 70 heIterateDirty(p
12cbd 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 Pager->pPCache,
12cbe 61 73 73 65 72 74 54 72 75 6e 63 61 74 65 43 6f assertTruncateCo
12cbf 6e 73 74 72 61 69 6e 74 43 62 29 3b 0a 7d 0a 23 nstraintCb);.}.#
12cc0 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 61 73 else.# define as
12cc1 73 65 72 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 sertTruncateCons
12cc2 74 72 61 69 6e 74 28 70 50 61 67 65 72 29 0a 23 traint(pPager).#
12cc3 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 endif../*.** Tru
12cc4 6e 63 61 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d ncate the in-mem
12cc5 6f 72 79 20 64 61 74 61 62 61 73 65 20 66 69 6c ory database fil
12cc6 65 20 69 6d 61 67 65 20 74 6f 20 6e 50 61 67 65 e image to nPage
12cc7 20 70 61 67 65 73 2e 20 54 68 69 73 20 0a 2a 2a pages. This .**
12cc8 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e function does n
12cc9 6f 74 20 61 63 74 75 61 6c 6c 79 20 6d 6f 64 69 ot actually modi
12cca 66 79 20 74 68 65 20 64 61 74 61 62 61 73 65 20 fy the database
12ccb 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e 20 49 74 file on disk. It
12ccc 20 0a 2a 2a 20 6a 75 73 74 20 73 65 74 73 20 74 .** just sets t
12ccd 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 74 61 74 he internal stat
12cce 65 20 6f 66 20 74 68 65 20 70 61 67 65 72 20 6f e of the pager o
12ccf 62 6a 65 63 74 20 73 6f 20 74 68 61 74 20 74 68 bject so that th
12cd0 65 20 0a 2a 2a 20 74 72 75 6e 63 61 74 69 6f 6e e .** truncation
12cd1 20 77 69 6c 6c 20 62 65 20 64 6f 6e 65 20 77 68 will be done wh
12cd2 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 en the current t
12cd3 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f ransaction is co
12cd4 6d 6d 69 74 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 mmitted..*/.SQLI
12cd5 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
12cd6 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e sqlite3PagerTrun
12cd7 63 61 74 65 49 6d 61 67 65 28 50 61 67 65 72 20 cateImage(Pager
12cd8 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 6e 50 *pPager, Pgno nP
12cd9 61 67 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 age){. assert(
12cda 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 pPager->dbSizeVa
12cdb 6c 69 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28 lid );. assert(
12cdc 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3e pPager->dbSize>
12cdd 3d 6e 50 61 67 65 20 29 3b 0a 20 20 61 73 73 65 =nPage );. asse
12cde 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 rt( pPager->stat
12cdf 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 e>=PAGER_RESERVE
12ce0 44 20 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 64 D );. pPager->d
12ce1 62 53 69 7a 65 20 3d 20 6e 50 61 67 65 3b 0a 20 bSize = nPage;.
12ce2 20 61 73 73 65 72 74 54 72 75 6e 63 61 74 65 43 assertTruncateC
12ce3 6f 6e 73 74 72 61 69 6e 74 28 70 50 61 67 65 72 onstraint(pPager
12ce4 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68 75 74 );.}../*.** Shut
12ce5 64 6f 77 6e 20 74 68 65 20 70 61 67 65 20 63 61 down the page ca
12ce6 63 68 65 2e 20 20 46 72 65 65 20 61 6c 6c 20 6d che. Free all m
12ce7 65 6d 6f 72 79 20 61 6e 64 20 63 6c 6f 73 65 20 emory and close
12ce8 61 6c 6c 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a all files..**.**
12ce9 20 49 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f If a transactio
12cea 6e 20 77 61 73 20 69 6e 20 70 72 6f 67 72 65 73 n was in progres
12ceb 73 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 s when this rout
12cec 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 ine is called, t
12ced 68 61 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 hat.** transacti
12cee 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 on is rolled bac
12cef 6b 2e 20 20 41 6c 6c 20 6f 75 74 73 74 61 6e 64 k. All outstand
12cf0 69 6e 67 20 70 61 67 65 73 20 61 72 65 20 69 6e ing pages are in
12cf1 76 61 6c 69 64 61 74 65 64 0a 2a 2a 20 61 6e 64 validated.** and
12cf2 20 74 68 65 69 72 20 6d 65 6d 6f 72 79 20 69 73 their memory is
12cf3 20 66 72 65 65 64 2e 20 20 41 6e 79 20 61 74 74 freed. Any att
12cf4 65 6d 70 74 20 74 6f 20 75 73 65 20 61 20 70 61 empt to use a pa
12cf5 67 65 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a ge associated.**
12cf6 20 77 69 74 68 20 74 68 69 73 20 70 61 67 65 20 with this page
12cf7 63 61 63 68 65 20 61 66 74 65 72 20 74 68 69 73 cache after this
12cf8 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
12cf9 73 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 0a 2a 2a s will likely.**
12cfa 20 72 65 73 75 6c 74 20 69 6e 20 61 20 63 6f 72 result in a cor
12cfb 65 64 75 6d 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 edump..**.** Thi
12cfc 73 20 66 75 6e 63 74 69 6f 6e 20 61 6c 77 61 79 s function alway
12cfd 73 20 73 75 63 63 65 65 64 73 2e 20 49 66 20 61 s succeeds. If a
12cfe 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
12cff 61 63 74 69 76 65 20 61 6e 20 61 74 74 65 6d 70 active an attemp
12d00 74 0a 2a 2a 20 69 73 20 6d 61 64 65 20 74 6f 20 t.** is made to
12d01 72 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 20 49 66 roll it back. If
12d02 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
12d03 20 64 75 72 69 6e 67 20 74 68 65 20 72 6f 6c 6c during the roll
12d04 62 61 63 6b 20 0a 2a 2a 20 61 20 68 6f 74 20 6a back .** a hot j
12d05 6f 75 72 6e 61 6c 20 6d 61 79 20 62 65 20 6c 65 ournal may be le
12d06 66 74 20 69 6e 20 74 68 65 20 66 69 6c 65 73 79 ft in the filesy
12d07 73 74 65 6d 20 62 75 74 20 6e 6f 20 65 72 72 6f stem but no erro
12d08 72 20 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a r is returned.**
12d09 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a to the caller..
12d0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
12d0b 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
12d0c 65 72 43 6c 6f 73 65 28 50 61 67 65 72 20 2a 70 erClose(Pager *p
12d0d 50 61 67 65 72 29 7b 0a 20 20 64 69 73 61 62 6c Pager){. disabl
12d0e 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 e_simulated_io_e
12d0f 72 72 6f 72 73 28 29 3b 0a 20 20 73 71 6c 69 74 rrors();. sqlit
12d10 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c e3BeginBenignMal
12d11 6c 6f 63 28 29 3b 0a 20 20 70 50 61 67 65 72 2d loc();. pPager-
12d12 3e 65 72 72 43 6f 64 65 20 3d 20 30 3b 0a 20 20 >errCode = 0;.
12d13 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 pPager->exclusiv
12d14 65 4d 6f 64 65 20 3d 20 30 3b 0a 20 20 70 61 67 eMode = 0;. pag
12d15 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 29 er_reset(pPager)
12d16 3b 0a 20 20 69 66 28 20 4d 45 4d 44 42 20 29 7b ;. if( MEMDB ){
12d17 0a 20 20 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63 . pager_unloc
12d18 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 65 6c k(pPager);. }el
12d19 73 65 7b 0a 20 20 20 20 2f 2a 20 53 65 74 20 50 se{. /* Set P
12d1a 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 48 64 72 20 ager.journalHdr
12d1b 74 6f 20 2d 31 20 66 6f 72 20 74 68 65 20 62 65 to -1 for the be
12d1c 6e 65 66 69 74 20 6f 66 20 74 68 65 20 70 61 67 nefit of the pag
12d1d 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 20 0a 20 er_playback() .
12d1e 20 20 20 2a 2a 20 63 61 6c 6c 20 77 68 69 63 68 ** call which
12d1f 20 6d 61 79 20 62 65 20 6d 61 64 65 20 66 72 6f may be made fro
12d20 6d 20 77 69 74 68 69 6e 20 70 61 67 65 72 55 6e m within pagerUn
12d21 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b 28 lockAndRollback(
12d22 29 2e 20 49 66 20 69 74 0a 20 20 20 20 2a 2a 20 ). If it. **
12d23 69 73 20 6e 6f 74 20 2d 31 2c 20 74 68 65 6e 20 is not -1, then
12d24 74 68 65 20 75 6e 73 79 6e 63 65 64 20 70 6f 72 the unsynced por
12d25 74 69 6f 6e 20 6f 66 20 61 6e 20 6f 70 65 6e 20 tion of an open
12d26 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 61 79 journal file may
12d27 0a 20 20 20 20 2a 2a 20 62 65 20 70 6c 61 79 65 . ** be playe
12d28 64 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 d back into the
12d29 64 61 74 61 62 61 73 65 2e 20 49 66 20 61 20 70 database. If a p
12d2a 6f 77 65 72 20 66 61 69 6c 75 72 65 20 6f 63 63 ower failure occ
12d2b 75 72 73 20 77 68 69 6c 65 0a 20 20 20 20 2a 2a urs while. **
12d2c 20 74 68 69 73 20 69 73 20 68 61 70 70 65 6e 69 this is happeni
12d2d 6e 67 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 ng, the database
12d2e 20 6d 61 79 20 62 65 63 6f 6d 65 20 63 6f 72 72 may become corr
12d2f 75 70 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 upt.. */.
12d30 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 pPager->journalH
12d31 64 72 20 3d 20 2d 31 3b 0a 20 20 20 20 70 61 67 dr = -1;. pag
12d32 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 erUnlockAndRollb
12d33 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 7d ack(pPager);. }
12d34 0a 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e . sqlite3EndBen
12d35 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 65 ignMalloc();. e
12d36 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f nable_simulated_
12d37 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a 20 20 50 io_errors();. P
12d38 41 47 45 52 54 52 41 43 45 28 28 22 43 4c 4f 53 AGERTRACE(("CLOS
12d39 45 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 E %d\n", PAGERID
12d3a 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 49 4f (pPager)));. IO
12d3b 54 52 41 43 45 28 28 22 43 4c 4f 53 45 20 25 70 TRACE(("CLOSE %p
12d3c 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 0a 20 20 \n", pPager)).
12d3d 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 sqlite3OsClose(p
12d3e 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 73 71 Pager->fd);. sq
12d3f 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 70 50 lite3PageFree(pP
12d40 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 29 ager->pTmpSpace)
12d41 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 ;. sqlite3Pcach
12d42 65 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 70 eClose(pPager->p
12d43 50 43 61 63 68 65 29 3b 0a 0a 23 69 66 64 65 66 PCache);..#ifdef
12d44 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 SQLITE_HAS_CODE
12d45 43 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e C. if( pPager->
12d46 78 43 6f 64 65 63 46 72 65 65 20 29 20 70 50 61 xCodecFree ) pPa
12d47 67 65 72 2d 3e 78 43 6f 64 65 63 46 72 65 65 28 ger->xCodecFree(
12d48 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 29 3b pPager->pCodec);
12d49 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 .#endif.. asser
12d4a 74 28 20 21 70 50 61 67 65 72 2d 3e 61 53 61 76 t( !pPager->aSav
12d4b 65 70 6f 69 6e 74 20 26 26 20 21 70 50 61 67 65 epoint && !pPage
12d4c 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 29 3b r->pInJournal );
12d4d 0a 20 20 61 73 73 65 72 74 28 20 21 69 73 4f 70 . assert( !isOp
12d4e 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
12d4f 26 26 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 && !isOpen(pPage
12d50 72 2d 3e 73 6a 66 64 29 20 29 3b 0a 0a 20 20 73 r->sjfd) );.. s
12d51 71 6c 69 74 65 33 5f 66 72 65 65 28 70 50 61 67 qlite3_free(pPag
12d52 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 er);. return SQ
12d53 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 LITE_OK;.}..#if
12d54 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 !defined(NDEBUG)
12d55 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 || defined(SQLI
12d56 54 45 5f 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20 52 TE_TEST)./*.** R
12d57 65 74 75 72 6e 20 74 68 65 20 70 61 67 65 20 6e eturn the page n
12d58 75 6d 62 65 72 20 66 6f 72 20 70 61 67 65 20 70 umber for page p
12d59 50 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 Pg..*/.SQLITE_PR
12d5a 49 56 41 54 45 20 50 67 6e 6f 20 73 71 6c 69 74 IVATE Pgno sqlit
12d5b 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65 e3PagerPagenumbe
12d5c 72 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a r(DbPage *pPg){.
12d5d 20 20 72 65 74 75 72 6e 20 70 50 67 2d 3e 70 67 return pPg->pg
12d5e 6e 6f 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a no;.}.#endif../*
12d5f 0a 2a 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 .** Increment th
12d60 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e e reference coun
12d61 74 20 66 6f 72 20 70 61 67 65 20 70 50 67 2e 0a t for page pPg..
12d62 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
12d63 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 E void sqlite3Pa
12d64 67 65 72 52 65 66 28 44 62 50 61 67 65 20 2a 70 gerRef(DbPage *p
12d65 50 67 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 63 Pg){. sqlite3Pc
12d66 61 63 68 65 52 65 66 28 70 50 67 29 3b 0a 7d 0a acheRef(pPg);.}.
12d67 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 65 20 ./*.** Sync the
12d68 6a 6f 75 72 6e 61 6c 2e 20 49 6e 20 6f 74 68 65 journal. In othe
12d69 72 20 77 6f 72 64 73 2c 20 6d 61 6b 65 20 73 75 r words, make su
12d6a 72 65 20 61 6c 6c 20 74 68 65 20 70 61 67 65 73 re all the pages
12d6b 20 74 68 61 74 20 68 61 76 65 0a 2a 2a 20 62 65 that have.** be
12d6c 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 en written to th
12d6d 65 20 6a 6f 75 72 6e 61 6c 20 68 61 76 65 20 61 e journal have a
12d6e 63 74 75 61 6c 6c 79 20 72 65 61 63 68 65 64 20 ctually reached
12d6f 74 68 65 20 73 75 72 66 61 63 65 20 6f 66 20 74 the surface of t
12d70 68 65 0a 2a 2a 20 64 69 73 6b 20 61 6e 64 20 63 he.** disk and c
12d71 61 6e 20 62 65 20 72 65 73 74 6f 72 65 64 20 69 an be restored i
12d72 6e 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61 n the event of a
12d73 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c hot-journal rol
12d74 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 lback..**.** If
12d75 74 68 65 20 50 61 67 65 72 2e 6e 65 65 64 53 79 the Pager.needSy
12d76 6e 63 20 66 6c 61 67 20 69 73 20 6e 6f 74 20 73 nc flag is not s
12d77 65 74 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 et, then this fu
12d78 6e 63 74 69 6f 6e 20 69 73 20 61 0a 2a 2a 20 6e nction is a.** n
12d79 6f 2d 6f 70 2e 20 4f 74 68 65 72 77 69 73 65 2c o-op. Otherwise,
12d7a 20 74 68 65 20 61 63 74 69 6f 6e 73 20 72 65 71 the actions req
12d7b 75 69 72 65 64 20 64 65 70 65 6e 64 20 6f 6e 20 uired depend on
12d7c 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 the journal-mode
12d7d 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 65 76 69 .** and the devi
12d7e 63 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69 ce characteristi
12d7f 63 73 20 6f 66 20 74 68 65 20 74 68 65 20 66 69 cs of the the fi
12d80 6c 65 2d 73 79 73 74 65 6d 2c 20 61 73 20 66 6f le-system, as fo
12d81 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a llows:.**.** *
12d82 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 If the journal
12d83 66 69 6c 65 20 69 73 20 61 6e 20 69 6e 2d 6d 65 file is an in-me
12d84 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c mory journal fil
12d85 65 2c 20 6e 6f 20 61 63 74 69 6f 6e 20 6e 65 65 e, no action nee
12d86 64 0a 2a 2a 20 20 20 20 20 62 65 20 74 61 6b 65 d.** be take
12d87 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 4f 74 68 n..**.** * Oth
12d88 65 72 77 69 73 65 2c 20 69 66 20 74 68 65 20 64 erwise, if the d
12d89 65 76 69 63 65 20 64 6f 65 73 20 6e 6f 74 20 73 evice does not s
12d8a 75 70 70 6f 72 74 20 74 68 65 20 53 41 46 45 5f upport the SAFE_
12d8b 41 50 50 45 4e 44 20 70 72 6f 70 65 72 74 79 2c APPEND property,
12d8c 0a 2a 2a 20 20 20 20 20 74 68 65 6e 20 74 68 65 .** then the
12d8d 20 6e 52 65 63 20 66 69 65 6c 64 20 6f 66 20 74 nRec field of t
12d8e 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 he most recently
12d8f 20 77 72 69 74 74 65 6e 20 6a 6f 75 72 6e 61 6c written journal
12d90 20 68 65 61 64 65 72 0a 2a 2a 20 20 20 20 20 69 header.** i
12d91 73 20 75 70 64 61 74 65 64 20 74 6f 20 63 6f 6e s updated to con
12d92 74 61 69 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 tain the number
12d93 6f 66 20 6a 6f 75 72 6e 61 6c 20 72 65 63 6f 72 of journal recor
12d94 64 73 20 74 68 61 74 20 68 61 76 65 0a 2a 2a 20 ds that have.**
12d95 20 20 20 20 62 65 65 6e 20 77 72 69 74 74 65 6e been written
12d96 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 74 2e 20 49 following it. I
12d97 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 6f f the pager is o
12d98 70 65 72 61 74 69 6e 67 20 69 6e 20 66 75 6c 6c perating in full
12d99 2d 73 79 6e 63 0a 2a 2a 20 20 20 20 20 6d 6f 64 -sync.** mod
12d9a 65 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 e, then the jour
12d9b 6e 61 6c 20 66 69 6c 65 20 69 73 20 73 79 6e 63 nal file is sync
12d9c 65 64 20 62 65 66 6f 72 65 20 74 68 69 73 20 66 ed before this f
12d9d 69 65 6c 64 20 69 73 20 75 70 64 61 74 65 64 2e ield is updated.
12d9e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 .**.** * If th
12d9f 65 20 64 65 76 69 63 65 20 64 6f 65 73 20 6e 6f e device does no
12da0 74 20 73 75 70 70 6f 72 74 20 74 68 65 20 53 45 t support the SE
12da1 51 55 45 4e 54 49 41 4c 20 70 72 6f 70 65 72 74 QUENTIAL propert
12da2 79 2c 20 74 68 65 6e 20 0a 2a 2a 20 20 20 20 20 y, then .**
12da3 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 journal file is
12da4 73 79 6e 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 72 synced..**.** Or
12da5 2c 20 69 6e 20 70 73 65 75 64 6f 2d 63 6f 64 65 , in pseudo-code
12da6 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 66 28 20 4e 4f :.**.** if( NO
12da7 54 20 3c 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 T <in-memory jou
12da8 72 6e 61 6c 3e 20 29 7b 0a 2a 2a 20 20 20 20 20 rnal> ){.**
12da9 69 66 28 20 4e 4f 54 20 53 41 46 45 5f 41 50 50 if( NOT SAFE_APP
12daa 45 4e 44 20 29 7b 0a 2a 2a 20 20 20 20 20 20 20 END ){.**
12dab 69 66 28 20 3c 66 75 6c 6c 2d 73 79 6e 63 20 6d if( <full-sync m
12dac 6f 64 65 3e 20 29 20 78 53 79 6e 63 28 3c 6a 6f ode> ) xSync(<jo
12dad 75 72 6e 61 6c 20 66 69 6c 65 3e 29 3b 0a 2a 2a urnal file>);.**
12dae 20 20 20 20 20 20 20 3c 75 70 64 61 74 65 20 6e <update n
12daf 52 65 63 20 66 69 65 6c 64 3e 0a 2a 2a 20 20 20 Rec field>.**
12db0 20 20 7d 20 0a 2a 2a 20 20 20 20 20 69 66 28 20 } .** if(
12db1 4e 4f 54 20 53 45 51 55 45 4e 54 49 41 4c 20 29 NOT SEQUENTIAL )
12db2 20 78 53 79 6e 63 28 3c 6a 6f 75 72 6e 61 6c 20 xSync(<journal
12db3 66 69 6c 65 3e 29 3b 0a 2a 2a 20 20 20 7d 0a 2a file>);.** }.*
12db4 2a 0a 2a 2a 20 54 68 65 20 50 61 67 65 72 2e 6e *.** The Pager.n
12db5 65 65 64 53 79 6e 63 20 66 6c 61 67 20 69 73 20 eedSync flag is
12db6 6e 65 76 65 72 20 62 65 20 73 65 74 20 66 6f 72 never be set for
12db7 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 temporary files
12db8 2c 20 6f 72 20 61 6e 79 0a 2a 2a 20 66 69 6c 65 , or any.** file
12db9 20 6f 70 65 72 61 74 69 6e 67 20 69 6e 20 6e 6f operating in no
12dba 2d 73 79 6e 63 20 6d 6f 64 65 20 28 50 61 67 65 -sync mode (Page
12dbb 72 2e 6e 6f 53 79 6e 63 20 73 65 74 20 74 6f 20 r.noSync set to
12dbc 6e 6f 6e 2d 7a 65 72 6f 29 2e 0a 2a 2a 0a 2a 2a non-zero)..**.**
12dbd 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 If successful,
12dbe 74 68 69 73 20 72 6f 75 74 69 6e 65 20 63 6c 65 this routine cle
12dbf 61 72 73 20 74 68 65 20 50 47 48 44 52 5f 4e 45 ars the PGHDR_NE
12dc0 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 6f 66 20 ED_SYNC flag of
12dc1 65 76 65 72 79 20 0a 2a 2a 20 70 61 67 65 20 63 every .** page c
12dc2 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 20 69 6e urrently held in
12dc3 20 6d 65 6d 6f 72 79 20 62 65 66 6f 72 65 20 72 memory before r
12dc4 65 74 75 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f eturning SQLITE_
12dc5 4f 4b 2e 20 49 66 20 61 6e 20 49 4f 0a 2a 2a 20 OK. If an IO.**
12dc6 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 error is encount
12dc7 65 72 65 64 2c 20 74 68 65 6e 20 74 68 65 20 49 ered, then the I
12dc8 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 O error code is
12dc9 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 returned to the
12dca 63 61 6c 6c 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 caller..*/.stati
12dcb 63 20 69 6e 74 20 73 79 6e 63 4a 6f 75 72 6e 61 c int syncJourna
12dcc 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 l(Pager *pPager)
12dcd 7b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e {. if( pPager->
12dce 6e 65 65 64 53 79 6e 63 20 29 7b 0a 20 20 20 20 needSync ){.
12dcf 61 73 73 65 72 74 28 20 21 70 50 61 67 65 72 2d assert( !pPager-
12dd0 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20 20 >tempFile );.
12dd1 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 if( pPager->jou
12dd2 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f rnalMode!=PAGER_
12dd3 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f JOURNALMODE_MEMO
12dd4 52 59 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 RY ){. int
12dd5 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rc;
12dd6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12dd7 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 /* Return code
12dd8 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 69 */. const i
12dd9 6e 74 20 69 44 63 20 3d 20 73 71 6c 69 74 65 33 nt iDc = sqlite3
12dda 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65 OsDeviceCharacte
12ddb 72 69 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e ristics(pPager->
12ddc 66 64 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 fd);. asser
12ddd 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 t( isOpen(pPager
12dde 2d 3e 6a 66 64 29 20 29 3b 0a 0a 20 20 20 20 20 ->jfd) );..
12ddf 20 69 66 28 20 30 3d 3d 28 69 44 63 26 53 51 4c if( 0==(iDc&SQL
12de0 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 ITE_IOCAP_SAFE_A
12de1 50 50 45 4e 44 29 20 29 7b 0a 20 20 20 20 20 20 PPEND) ){.
12de2 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 /* This block
12de3 64 65 61 6c 73 20 77 69 74 68 20 61 6e 20 6f 62 deals with an ob
12de4 73 63 75 72 65 20 70 72 6f 62 6c 65 6d 2e 20 49 scure problem. I
12de5 66 20 74 68 65 20 6c 61 73 74 20 63 6f 6e 6e 65 f the last conne
12de6 63 74 69 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a ction. **
12de7 20 74 68 61 74 20 77 72 6f 74 65 20 74 6f 20 74 that wrote to t
12de8 68 69 73 20 64 61 74 61 62 61 73 65 20 77 61 73 his database was
12de9 20 6f 70 65 72 61 74 69 6e 67 20 69 6e 20 70 65 operating in pe
12dea 72 73 69 73 74 65 6e 74 2d 6a 6f 75 72 6e 61 6c rsistent-journal
12deb 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d 6f 64 65 . ** mode
12dec 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e , then the journ
12ded 61 6c 20 66 69 6c 65 20 6d 61 79 20 61 74 20 74 al file may at t
12dee 68 69 73 20 70 6f 69 6e 74 20 61 63 74 75 61 6c his point actual
12def 6c 79 20 62 65 20 6c 61 72 67 65 72 0a 20 20 20 ly be larger.
12df0 20 20 20 20 20 2a 2a 20 74 68 61 6e 20 50 61 67 ** than Pag
12df1 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 20 62 79 er.journalOff by
12df2 74 65 73 2e 20 49 66 20 74 68 65 20 6e 65 78 74 tes. If the next
12df3 20 74 68 69 6e 67 20 69 6e 20 74 68 65 20 6a 6f thing in the jo
12df4 75 72 6e 61 6c 0a 20 20 20 20 20 20 20 20 2a 2a urnal. **
12df5 20 66 69 6c 65 20 68 61 70 70 65 6e 73 20 74 6f file happens to
12df6 20 62 65 20 61 20 6a 6f 75 72 6e 61 6c 2d 68 65 be a journal-he
12df7 61 64 65 72 20 28 77 72 69 74 74 65 6e 20 61 73 ader (written as
12df8 20 70 61 72 74 20 6f 66 20 74 68 65 0a 20 20 20 part of the.
12df9 20 20 20 20 20 2a 2a 20 70 72 65 76 69 6f 75 73 ** previous
12dfa 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 72 61 connections tra
12dfb 6e 73 61 63 74 69 6f 6e 29 2c 20 61 6e 64 20 61 nsaction), and a
12dfc 20 63 72 61 73 68 20 6f 72 20 70 6f 77 65 72 2d crash or power-
12dfd 66 61 69 6c 75 72 65 20 0a 20 20 20 20 20 20 20 failure .
12dfe 20 2a 2a 20 6f 63 63 75 72 73 20 61 66 74 65 72 ** occurs after
12dff 20 6e 52 65 63 20 69 73 20 75 70 64 61 74 65 64 nRec is updated
12e00 20 62 75 74 20 62 65 66 6f 72 65 20 74 68 69 73 but before this
12e01 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 72 69 74 connection writ
12e02 65 73 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 es . ** a
12e03 6e 79 74 68 69 6e 67 20 65 6c 73 65 20 74 6f 20 nything else to
12e04 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
12e05 20 28 6f 72 20 63 6f 6d 6d 69 74 73 2f 72 6f 6c (or commits/rol
12e06 6c 73 20 62 61 63 6b 20 69 74 73 20 0a 20 20 20 ls back its .
12e07 20 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 ** transact
12e08 69 6f 6e 29 2c 20 74 68 65 6e 20 53 51 4c 69 74 ion), then SQLit
12e09 65 20 6d 61 79 20 62 65 63 6f 6d 65 20 63 6f 6e e may become con
12e0a 66 75 73 65 64 20 77 68 65 6e 20 64 6f 69 6e 67 fused when doing
12e0b 20 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a the . **
12e0c 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c hot-journal rol
12e0d 6c 62 61 63 6b 20 66 6f 6c 6c 6f 77 69 6e 67 20 lback following
12e0e 72 65 63 6f 76 65 72 79 2e 20 49 74 20 6d 61 79 recovery. It may
12e0f 20 72 6f 6c 6c 20 62 61 63 6b 20 61 6c 6c 0a 20 roll back all.
12e10 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 69 ** of thi
12e11 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 64 61 s connections da
12e12 74 61 2c 20 74 68 65 6e 20 70 72 6f 63 65 65 64 ta, then proceed
12e13 20 74 6f 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b to rolling back
12e14 20 74 68 65 20 6f 6c 64 2c 0a 20 20 20 20 20 20 the old,.
12e15 20 20 2a 2a 20 6f 75 74 2d 6f 66 2d 64 61 74 65 ** out-of-date
12e16 20 64 61 74 61 20 74 68 61 74 20 66 6f 6c 6c 6f data that follo
12e17 77 73 20 69 74 2e 20 44 61 74 61 62 61 73 65 20 ws it. Database
12e18 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 20 20 20 20 corruption..
12e19 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a **. *
12e1a 2a 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 * To work around
12e1b 20 74 68 69 73 2c 20 69 66 20 74 68 65 20 6a 6f this, if the jo
12e1c 75 72 6e 61 6c 20 66 69 6c 65 20 64 6f 65 73 20 urnal file does
12e1d 61 70 70 65 61 72 20 74 6f 20 63 6f 6e 74 61 69 appear to contai
12e1e 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 20 76 n. ** a v
12e1f 61 6c 69 64 20 68 65 61 64 65 72 20 66 6f 6c 6c alid header foll
12e20 6f 77 69 6e 67 20 50 61 67 65 72 2e 6a 6f 75 72 owing Pager.jour
12e21 6e 61 6c 4f 66 66 2c 20 74 68 65 6e 20 77 72 69 nalOff, then wri
12e22 74 65 20 61 20 30 78 30 30 0a 20 20 20 20 20 20 te a 0x00.
12e23 20 20 2a 2a 20 62 79 74 65 20 74 6f 20 74 68 65 ** byte to the
12e24 20 73 74 61 72 74 20 6f 66 20 69 74 20 74 6f 20 start of it to
12e25 70 72 65 76 65 6e 74 20 69 74 20 66 72 6f 6d 20 prevent it from
12e26 62 65 69 6e 67 20 72 65 63 6f 67 6e 69 7a 65 64 being recognized
12e27 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 .. **.
12e28 20 20 20 20 20 2a 2a 20 56 61 72 69 61 62 6c 65 ** Variable
12e29 20 69 4e 65 78 74 48 64 72 4f 66 66 73 65 74 20 iNextHdrOffset
12e2a 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6f 66 is set to the of
12e2b 66 73 65 74 20 61 74 20 77 68 69 63 68 20 74 68 fset at which th
12e2c 69 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 72 is. ** pr
12e2d 6f 62 6c 65 6d 61 74 69 63 20 68 65 61 64 65 72 oblematic header
12e2e 20 77 69 6c 6c 20 6f 63 63 75 72 2c 20 69 66 20 will occur, if
12e2f 69 74 20 65 78 69 73 74 73 2e 20 61 4d 61 67 69 it exists. aMagi
12e30 63 20 69 73 20 75 73 65 64 20 0a 20 20 20 20 20 c is used .
12e31 20 20 20 2a 2a 20 61 73 20 61 20 74 65 6d 70 6f ** as a tempo
12e32 72 61 72 79 20 62 75 66 66 65 72 20 74 6f 20 69 rary buffer to i
12e33 6e 73 70 65 63 74 20 74 68 65 20 66 69 72 73 74 nspect the first
12e34 20 63 6f 75 70 6c 65 20 6f 66 20 62 79 74 65 73 couple of bytes
12e35 20 6f 66 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 of. ** t
12e36 68 65 20 70 6f 74 65 6e 74 69 61 6c 20 6a 6f 75 he potential jou
12e37 72 6e 61 6c 20 68 65 61 64 65 72 2e 0a 20 20 20 rnal header..
12e38 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
12e39 69 36 34 20 69 4e 65 78 74 48 64 72 4f 66 66 73 i64 iNextHdrOffs
12e3a 65 74 3b 0a 20 20 20 20 20 20 20 20 75 38 20 61 et;. u8 a
12e3b 4d 61 67 69 63 5b 38 5d 3b 0a 09 75 38 20 7a 48 Magic[8];..u8 zH
12e3c 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f eader[sizeof(aJo
12e3d 75 72 6e 61 6c 4d 61 67 69 63 29 2b 34 5d 3b 0a urnalMagic)+4];.
12e3e 0a 09 6d 65 6d 63 70 79 28 7a 48 65 61 64 65 72 ..memcpy(zHeader
12e3f 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c , aJournalMagic,
12e40 20 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c sizeof(aJournal
12e41 4d 61 67 69 63 29 29 3b 0a 09 70 75 74 33 32 62 Magic));..put32b
12e42 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a its(&zHeader[siz
12e43 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 eof(aJournalMagi
12e44 63 29 5d 2c 20 70 50 61 67 65 72 2d 3e 6e 52 65 c)], pPager->nRe
12e45 63 29 3b 0a 0a 20 20 20 20 20 20 20 20 69 4e 65 c);.. iNe
12e46 78 74 48 64 72 4f 66 66 73 65 74 20 3d 20 6a 6f xtHdrOffset = jo
12e47 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 28 70 urnalHdrOffset(p
12e48 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 20 20 Pager);.
12e49 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 rc = sqlite3OsRe
12e4a 61 64 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 ad(pPager->jfd,
12e4b 61 4d 61 67 69 63 2c 20 38 2c 20 69 4e 65 78 74 aMagic, 8, iNext
12e4c 48 64 72 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 HdrOffset);.
12e4d 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
12e4e 54 45 5f 4f 4b 20 26 26 20 30 3d 3d 6d 65 6d 63 TE_OK && 0==memc
12e4f 6d 70 28 61 4d 61 67 69 63 2c 20 61 4a 6f 75 72 mp(aMagic, aJour
12e50 6e 61 6c 4d 61 67 69 63 2c 20 38 29 20 29 7b 0a nalMagic, 8) ){.
12e51 20 20 20 20 20 20 20 20 20 20 73 74 61 74 69 63 static
12e52 20 63 6f 6e 73 74 20 75 38 20 7a 65 72 6f 62 79 const u8 zeroby
12e53 74 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 te = 0;.
12e54 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
12e55 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 Write(pPager->jf
12e56 64 2c 20 26 7a 65 72 6f 62 79 74 65 2c 20 31 2c d, &zerobyte, 1,
12e57 20 69 4e 65 78 74 48 64 72 4f 66 66 73 65 74 29 iNextHdrOffset)
12e58 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
12e59 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
12e5a 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51 4c TE_OK && rc!=SQL
12e5b 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f ITE_IOERR_SHORT_
12e5c 52 45 41 44 20 29 7b 0a 20 20 20 20 20 20 20 20 READ ){.
12e5d 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
12e5e 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 }..
12e5f 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 52 65 /* Write the nRe
12e60 63 20 76 61 6c 75 65 20 69 6e 74 6f 20 74 68 65 c value into the
12e61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 65 journal file he
12e62 61 64 65 72 2e 20 49 66 20 69 6e 0a 20 20 20 20 ader. If in.
12e63 20 20 20 20 2a 2a 20 66 75 6c 6c 2d 73 79 6e 63 ** full-sync
12e64 68 72 6f 6e 6f 75 73 20 6d 6f 64 65 2c 20 73 79 hronous mode, sy
12e65 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 nc the journal f
12e66 69 72 73 74 2e 20 54 68 69 73 20 65 6e 73 75 72 irst. This ensur
12e67 65 73 20 74 68 61 74 0a 20 20 20 20 20 20 20 20 es that.
12e68 2a 2a 20 61 6c 6c 20 64 61 74 61 20 68 61 73 20 ** all data has
12e69 72 65 61 6c 6c 79 20 68 69 74 20 74 68 65 20 64 really hit the d
12e6a 69 73 6b 20 62 65 66 6f 72 65 20 6e 52 65 63 20 isk before nRec
12e6b 69 73 20 75 70 64 61 74 65 64 20 74 6f 20 6d 61 is updated to ma
12e6c 72 6b 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 74 rk. ** it
12e6d 20 61 73 20 61 20 63 61 6e 64 69 64 61 74 65 20 as a candidate
12e6e 66 6f 72 20 72 6f 6c 6c 62 61 63 6b 2e 0a 20 20 for rollback..
12e6f 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 **.
12e70 20 2a 2a 20 54 68 69 73 20 69 73 20 6e 6f 74 20 ** This is not
12e71 72 65 71 75 69 72 65 64 20 69 66 20 74 68 65 20 required if the
12e72 70 65 72 73 69 73 74 65 6e 74 20 6d 65 64 69 61 persistent media
12e73 20 73 75 70 70 6f 72 74 73 20 74 68 65 0a 20 20 supports the.
12e74 20 20 20 20 20 20 2a 2a 20 53 41 46 45 5f 41 50 ** SAFE_AP
12e75 50 45 4e 44 20 70 72 6f 70 65 72 74 79 2e 20 42 PEND property. B
12e76 65 63 61 75 73 65 20 69 6e 20 74 68 69 73 20 63 ecause in this c
12e77 61 73 65 20 69 74 20 69 73 20 6e 6f 74 20 70 6f ase it is not po
12e78 73 73 69 62 6c 65 20 0a 20 20 20 20 20 20 20 20 ssible .
12e79 2a 2a 20 66 6f 72 20 67 61 72 62 61 67 65 20 64 ** for garbage d
12e7a 61 74 61 20 74 6f 20 62 65 20 61 70 70 65 6e 64 ata to be append
12e7b 65 64 20 74 6f 20 74 68 65 20 66 69 6c 65 2c 20 ed to the file,
12e7c 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64 0a 20 the nRec field.
12e7d 20 20 20 20 20 20 20 2a 2a 20 69 73 20 70 6f 70 ** is pop
12e7e 75 6c 61 74 65 64 20 77 69 74 68 20 30 78 46 46 ulated with 0xFF
12e7f 46 46 46 46 46 46 20 77 68 65 6e 20 74 68 65 20 FFFFFF when the
12e80 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69 journal header i
12e81 73 20 77 72 69 74 74 65 6e 0a 20 20 20 20 20 20 s written.
12e82 20 20 2a 2a 20 61 6e 64 20 6e 65 76 65 72 20 6e ** and never n
12e83 65 65 64 73 20 74 6f 20 62 65 20 75 70 64 61 74 eeds to be updat
12e84 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 ed.. */.
12e85 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 if( pPage
12e86 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 26 26 20 30 r->fullSync && 0
12e87 3d 3d 28 69 44 63 26 53 51 4c 49 54 45 5f 49 4f ==(iDc&SQLITE_IO
12e88 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 29 20 CAP_SEQUENTIAL)
12e89 29 7b 0a 20 20 20 20 20 20 20 20 20 20 50 41 47 ){. PAG
12e8a 45 52 54 52 41 43 45 28 28 22 53 59 4e 43 20 6a ERTRACE(("SYNC j
12e8b 6f 75 72 6e 61 6c 20 6f 66 20 25 64 5c 6e 22 2c ournal of %d\n",
12e8c 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 PAGERID(pPager)
12e8d 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 49 4f ));. IO
12e8e 54 52 41 43 45 28 28 22 4a 53 59 4e 43 20 25 70 TRACE(("JSYNC %p
12e8f 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 0a 20 20 \n", pPager)).
12e90 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
12e91 69 74 65 33 4f 73 53 79 6e 63 28 70 50 61 67 65 ite3OsSync(pPage
12e92 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e r->jfd, pPager->
12e93 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a 20 20 20 sync_flags);.
12e94 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 if( rc!=S
12e95 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 QLITE_OK ) retur
12e96 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a n rc;. }.
12e97 20 20 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 IOTRACE(
12e98 28 22 4a 48 44 52 20 25 70 20 25 6c 6c 64 5c 6e ("JHDR %p %lld\n
12e99 22 2c 20 70 50 61 67 65 72 2c 20 70 50 61 67 65 ", pPager, pPage
12e9a 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 29 29 3b r->journalHdr));
12e9b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
12e9c 6c 69 74 65 33 4f 73 57 72 69 74 65 28 0a 20 20 lite3OsWrite(.
12e9d 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72 pPager
12e9e 2d 3e 6a 66 64 2c 20 7a 48 65 61 64 65 72 2c 20 ->jfd, zHeader,
12e9f 73 69 7a 65 6f 66 28 7a 48 65 61 64 65 72 29 2c sizeof(zHeader),
12ea0 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
12ea1 48 64 72 0a 09 29 3b 0a 20 20 20 20 20 20 20 20 Hdr..);.
12ea2 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
12ea3 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 K ) return rc;.
12ea4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
12ea5 20 30 3d 3d 28 69 44 63 26 53 51 4c 49 54 45 5f 0==(iDc&SQLITE_
12ea6 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c IOCAP_SEQUENTIAL
12ea7 29 20 29 7b 0a 20 20 20 20 20 20 20 20 50 41 47 ) ){. PAG
12ea8 45 52 54 52 41 43 45 28 28 22 53 59 4e 43 20 6a ERTRACE(("SYNC j
12ea9 6f 75 72 6e 61 6c 20 6f 66 20 25 64 5c 6e 22 2c ournal of %d\n",
12eaa 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 PAGERID(pPager)
12eab 29 29 3b 0a 20 20 20 20 20 20 20 20 49 4f 54 52 ));. IOTR
12eac 41 43 45 28 28 22 4a 53 59 4e 43 20 25 70 5c 6e ACE(("JSYNC %p\n
12ead 22 2c 20 70 50 61 67 65 72 29 29 0a 20 20 20 20 ", pPager)).
12eae 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
12eaf 4f 73 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 6a OsSync(pPager->j
12eb0 66 64 2c 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 fd, pPager->sync
12eb1 5f 66 6c 61 67 73 7c 20 0a 20 20 20 20 20 20 20 _flags| .
12eb2 20 20 20 28 70 50 61 67 65 72 2d 3e 73 79 6e 63 (pPager->sync
12eb3 5f 66 6c 61 67 73 3d 3d 53 51 4c 49 54 45 5f 53 _flags==SQLITE_S
12eb4 59 4e 43 5f 46 55 4c 4c 3f 53 51 4c 49 54 45 5f YNC_FULL?SQLITE_
12eb5 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 3a 30 29 SYNC_DATAONLY:0)
12eb6 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 . );.
12eb7 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
12eb8 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 TE_OK ) return r
12eb9 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d c;. }. }
12eba 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75 .. /* The jou
12ebb 72 6e 61 6c 20 66 69 6c 65 20 77 61 73 20 6a 75 rnal file was ju
12ebc 73 74 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 st successfully
12ebd 73 79 6e 63 65 64 2e 20 53 65 74 20 50 61 67 65 synced. Set Page
12ebe 72 2e 6e 65 65 64 53 79 6e 63 20 0a 20 20 20 20 r.needSync .
12ebf 2a 2a 20 74 6f 20 7a 65 72 6f 20 61 6e 64 20 63 ** to zero and c
12ec0 6c 65 61 72 20 74 68 65 20 50 47 48 44 52 5f 4e lear the PGHDR_N
12ec1 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 6f 6e EED_SYNC flag on
12ec2 20 61 6c 6c 20 70 61 67 65 73 73 2e 0a 20 20 20 all pagess..
12ec3 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d 3e */. pPager->
12ec4 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20 needSync = 0;.
12ec5 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
12ec6 6c 53 74 61 72 74 65 64 20 3d 20 31 3b 0a 20 20 lStarted = 1;.
12ec7 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 sqlite3PcacheC
12ec8 6c 65 61 72 53 79 6e 63 46 6c 61 67 73 28 70 50 learSyncFlags(pP
12ec9 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a ager->pPCache);.
12eca 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 }.. return SQ
12ecb 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
12ecc 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 69 * The argument i
12ecd 73 20 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 s the first in a
12ece 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 linked list of
12ecf 64 69 72 74 79 20 70 61 67 65 73 20 63 6f 6e 6e dirty pages conn
12ed0 65 63 74 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 ected.** by the
12ed1 50 67 48 64 72 2e 70 44 69 72 74 79 20 70 6f 69 PgHdr.pDirty poi
12ed2 6e 74 65 72 2e 20 54 68 69 73 20 66 75 6e 63 74 nter. This funct
12ed3 69 6f 6e 20 77 72 69 74 65 73 20 65 61 63 68 20 ion writes each
12ed4 6f 6e 65 20 6f 66 20 74 68 65 0a 2a 2a 20 69 6e one of the.** in
12ed5 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73 20 69 6e -memory pages in
12ed6 20 74 68 65 20 6c 69 73 74 20 74 6f 20 74 68 65 the list to the
12ed7 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
12ed8 54 68 65 20 61 72 67 75 6d 65 6e 74 20 6d 61 79 The argument may
12ed9 0a 2a 2a 20 62 65 20 4e 55 4c 4c 2c 20 72 65 70 .** be NULL, rep
12eda 72 65 73 65 6e 74 69 6e 67 20 61 6e 20 65 6d 70 resenting an emp
12edb 74 79 20 6c 69 73 74 2e 20 49 6e 20 74 68 69 73 ty list. In this
12edc 20 63 61 73 65 20 74 68 69 73 20 66 75 6e 63 74 case this funct
12edd 69 6f 6e 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d 6f ion is.** a no-o
12ede 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 p..**.** The pag
12edf 65 72 20 6d 75 73 74 20 68 6f 6c 64 20 61 74 20 er must hold at
12ee0 6c 65 61 73 74 20 61 20 52 45 53 45 52 56 45 44 least a RESERVED
12ee1 20 6c 6f 63 6b 20 77 68 65 6e 20 74 68 69 73 20 lock when this
12ee2 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 63 function.** is c
12ee3 61 6c 6c 65 64 2e 20 42 65 66 6f 72 65 20 77 72 alled. Before wr
12ee4 69 74 69 6e 67 20 61 6e 79 74 68 69 6e 67 20 74 iting anything t
12ee5 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 o the database f
12ee6 69 6c 65 2c 20 74 68 69 73 20 6c 6f 63 6b 0a 2a ile, this lock.*
12ee7 2a 20 69 73 20 75 70 67 72 61 64 65 64 20 74 6f * is upgraded to
12ee8 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f an EXCLUSIVE lo
12ee9 63 6b 2e 20 49 66 20 74 68 65 20 6c 6f 63 6b 20 ck. If the lock
12eea 63 61 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e cannot be obtain
12eeb 65 64 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 ed,.** SQLITE_BU
12eec 53 59 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 SY is returned a
12eed 6e 64 20 6e 6f 20 64 61 74 61 20 69 73 20 77 72 nd no data is wr
12eee 69 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61 74 itten to the dat
12eef 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 0a abase file..** .
12ef0 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 ** If the pager
12ef1 69 73 20 61 20 74 65 6d 70 2d 66 69 6c 65 20 70 is a temp-file p
12ef2 61 67 65 72 20 61 6e 64 20 74 68 65 20 61 63 74 ager and the act
12ef3 75 61 6c 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 ual file-system
12ef4 66 69 6c 65 0a 2a 2a 20 69 73 20 6e 6f 74 20 79 file.** is not y
12ef5 65 74 20 6f 70 65 6e 2c 20 69 74 20 69 73 20 63 et open, it is c
12ef6 72 65 61 74 65 64 20 61 6e 64 20 6f 70 65 6e 65 reated and opene
12ef7 64 20 62 65 66 6f 72 65 20 61 6e 79 20 64 61 74 d before any dat
12ef8 61 20 69 73 20 0a 2a 2a 20 77 72 69 74 74 65 6e a is .** written
12ef9 20 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 out..**.** Once
12efa 20 74 68 65 20 6c 6f 63 6b 20 68 61 73 20 62 65 the lock has be
12efb 65 6e 20 75 70 67 72 61 64 65 64 20 61 6e 64 2c en upgraded and,
12efc 20 69 66 20 6e 65 63 65 73 73 61 72 79 2c 20 74 if necessary, t
12efd 68 65 20 66 69 6c 65 20 6f 70 65 6e 65 64 2c 0a he file opened,.
12efe 2a 2a 20 74 68 65 20 70 61 67 65 73 20 61 72 65 ** the pages are
12eff 20 77 72 69 74 74 65 6e 20 6f 75 74 20 74 6f 20 written out to
12f00 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
12f01 65 20 69 6e 20 6c 69 73 74 20 6f 72 64 65 72 2e e in list order.
12f02 20 57 72 69 74 69 6e 67 0a 2a 2a 20 61 20 70 61 Writing.** a pa
12f03 67 65 20 69 73 20 73 6b 69 70 70 65 64 20 69 66 ge is skipped if
12f04 20 69 74 20 6d 65 65 74 73 20 65 69 74 68 65 72 it meets either
12f05 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
12f06 67 20 63 72 69 74 65 72 69 61 3a 0a 2a 2a 0a 2a g criteria:.**.*
12f07 2a 20 20 20 2a 20 54 68 65 20 70 61 67 65 20 6e * * The page n
12f08 75 6d 62 65 72 20 69 73 20 67 72 65 61 74 65 72 umber is greater
12f09 20 74 68 61 6e 20 50 61 67 65 72 2e 64 62 53 69 than Pager.dbSi
12f0a 7a 65 2c 20 6f 72 0a 2a 2a 20 20 20 2a 20 54 68 ze, or.** * Th
12f0b 65 20 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 e PGHDR_DONT_WRI
12f0c 54 45 20 66 6c 61 67 20 69 73 20 73 65 74 20 6f TE flag is set o
12f0d 6e 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a n the page..**.*
12f0e 2a 20 49 66 20 77 72 69 74 69 6e 67 20 6f 75 74 * If writing out
12f0f 20 61 20 70 61 67 65 20 63 61 75 73 65 73 20 74 a page causes t
12f10 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
12f11 20 74 6f 20 67 72 6f 77 2c 20 50 61 67 65 72 2e to grow, Pager.
12f12 64 62 46 69 6c 65 53 69 7a 65 0a 2a 2a 20 69 73 dbFileSize.** is
12f13 20 75 70 64 61 74 65 64 20 61 63 63 6f 72 64 69 updated accordi
12f14 6e 67 6c 79 2e 20 49 66 20 70 61 67 65 20 31 20 ngly. If page 1
12f15 69 73 20 77 72 69 74 74 65 6e 20 6f 75 74 2c 20 is written out,
12f16 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 63 then the value c
12f17 61 63 68 65 64 0a 2a 2a 20 69 6e 20 50 61 67 65 ached.** in Page
12f18 72 2e 64 62 46 69 6c 65 56 65 72 73 5b 5d 20 69 r.dbFileVers[] i
12f19 73 20 75 70 64 61 74 65 64 20 74 6f 20 6d 61 74 s updated to mat
12f1a 63 68 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 ch the new value
12f1b 20 73 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 74 68 stored in.** th
12f1c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
12f1d 0a 2a 2a 0a 2a 2a 20 49 66 20 65 76 65 72 79 74 .**.** If everyt
12f1e 68 69 6e 67 20 69 73 20 73 75 63 63 65 73 73 66 hing is successf
12f1f 75 6c 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 ul, SQLITE_OK is
12f20 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e returned. If an
12f21 20 49 4f 20 65 72 72 6f 72 20 0a 2a 2a 20 6f 63 IO error .** oc
12f22 63 75 72 73 2c 20 61 6e 20 49 4f 20 65 72 72 6f curs, an IO erro
12f23 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e r code is return
12f24 65 64 2e 20 4f 72 2c 20 69 66 20 74 68 65 20 45 ed. Or, if the E
12f25 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 63 61 XCLUSIVE lock ca
12f26 6e 6e 6f 74 0a 2a 2a 20 62 65 20 6f 62 74 61 69 nnot.** be obtai
12f27 6e 65 64 2c 20 53 51 4c 49 54 45 5f 42 55 53 59 ned, SQLITE_BUSY
12f28 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f is returned..*/
12f29 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 .static int page
12f2a 72 5f 77 72 69 74 65 5f 70 61 67 65 6c 69 73 74 r_write_pagelist
12f2b 28 50 67 48 64 72 20 2a 70 4c 69 73 74 29 7b 0a (PgHdr *pList){.
12f2c 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b Pager *pPager;
12f2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12f2e 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72 20 /* Pager
12f2f 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 object */. int
12f30 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rc;
12f31 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12f32 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 /* Return code
12f33 2a 2f 0a 0a 20 20 69 66 28 20 4e 45 56 45 52 28 */.. if( NEVER(
12f34 70 4c 69 73 74 3d 3d 30 29 20 29 20 72 65 74 75 pList==0) ) retu
12f35 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
12f36 70 50 61 67 65 72 20 3d 20 70 4c 69 73 74 2d 3e pPager = pList->
12f37 70 50 61 67 65 72 3b 0a 0a 20 20 2f 2a 20 41 74 pPager;.. /* At
12f38 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65 72 this point ther
12f39 65 20 6d 61 79 20 62 65 20 65 69 74 68 65 72 20 e may be either
12f3a 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 45 58 a RESERVED or EX
12f3b 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 CLUSIVE lock on
12f3c 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 the. ** databas
12f3d 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65 72 65 e file. If there
12f3e 20 69 73 20 61 6c 72 65 61 64 79 20 61 6e 20 45 is already an E
12f3f 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2c 20 74 XCLUSIVE lock, t
12f40 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a he following. *
12f41 2a 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f * call is a no-o
12f42 70 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4d 6f 76 p.. **. ** Mov
12f43 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 66 72 6f ing the lock fro
12f44 6d 20 52 45 53 45 52 56 45 44 20 74 6f 20 45 58 m RESERVED to EX
12f45 43 4c 55 53 49 56 45 20 61 63 74 75 61 6c 6c 79 CLUSIVE actually
12f46 20 69 6e 76 6f 6c 76 65 73 20 67 6f 69 6e 67 0a involves going.
12f47 20 20 2a 2a 20 74 68 72 6f 75 67 68 20 61 6e 20 ** through an
12f48 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 intermediate sta
12f49 74 65 20 50 45 4e 44 49 4e 47 2e 20 20 20 41 20 te PENDING. A
12f4a 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 70 72 65 PENDING lock pre
12f4b 76 65 6e 74 73 20 6e 65 77 0a 20 20 2a 2a 20 72 vents new. ** r
12f4c 65 61 64 65 72 73 20 66 72 6f 6d 20 61 74 74 61 eaders from atta
12f4d 63 68 69 6e 67 20 74 6f 20 74 68 65 20 64 61 74 ching to the dat
12f4e 61 62 61 73 65 20 62 75 74 20 69 73 20 75 6e 73 abase but is uns
12f4f 75 66 66 69 63 69 65 6e 74 20 66 6f 72 20 75 73 ufficient for us
12f50 20 74 6f 0a 20 20 2a 2a 20 77 72 69 74 65 2e 20 to. ** write.
12f51 20 54 68 65 20 69 64 65 61 20 6f 66 20 61 20 50 The idea of a P
12f52 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 74 ENDING lock is t
12f53 6f 20 70 72 65 76 65 6e 74 20 6e 65 77 20 72 65 o prevent new re
12f54 61 64 65 72 73 20 66 72 6f 6d 0a 20 20 2a 2a 20 aders from. **
12f55 63 6f 6d 69 6e 67 20 69 6e 20 77 68 69 6c 65 20 coming in while
12f56 77 65 20 77 61 69 74 20 66 6f 72 20 65 78 69 73 we wait for exis
12f57 74 69 6e 67 20 72 65 61 64 65 72 73 20 74 6f 20 ting readers to
12f58 63 6c 65 61 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a clear.. **. **
12f59 20 57 68 69 6c 65 20 74 68 65 20 70 61 67 65 72 While the pager
12f5a 20 69 73 20 69 6e 20 74 68 65 20 52 45 53 45 52 is in the RESER
12f5b 56 45 44 20 73 74 61 74 65 2c 20 74 68 65 20 6f VED state, the o
12f5c 72 69 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 riginal database
12f5d 20 66 69 6c 65 0a 20 20 2a 2a 20 69 73 20 75 6e file. ** is un
12f5e 63 68 61 6e 67 65 64 20 61 6e 64 20 77 65 20 63 changed and we c
12f5f 61 6e 20 72 6f 6c 6c 62 61 63 6b 20 77 69 74 68 an rollback with
12f60 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 70 6c out having to pl
12f61 61 79 62 61 63 6b 20 74 68 65 0a 20 20 2a 2a 20 ayback the. **
12f62 6a 6f 75 72 6e 61 6c 20 69 6e 74 6f 20 74 68 65 journal into the
12f63 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 62 61 original databa
12f64 73 65 20 66 69 6c 65 2e 20 20 4f 6e 63 65 20 77 se file. Once w
12f65 65 20 74 72 61 6e 73 69 74 69 6f 6e 20 74 6f 0a e transition to.
12f66 20 20 2a 2a 20 45 58 43 4c 55 53 49 56 45 2c 20 ** EXCLUSIVE,
12f67 69 74 20 6d 65 61 6e 73 20 74 68 65 20 64 61 74 it means the dat
12f68 61 62 61 73 65 20 66 69 6c 65 20 68 61 73 20 62 abase file has b
12f69 65 65 6e 20 63 68 61 6e 67 65 64 20 61 6e 64 20 een changed and
12f6a 61 6e 79 20 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a any rollback. *
12f6b 2a 20 77 69 6c 6c 20 72 65 71 75 69 72 65 20 61 * will require a
12f6c 20 6a 6f 75 72 6e 61 6c 20 70 6c 61 79 62 61 63 journal playbac
12f6d 6b 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 k.. */. assert
12f6e 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e ( pPager->state>
12f6f 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 =PAGER_RESERVED
12f70 29 3b 0a 20 20 72 63 20 3d 20 70 61 67 65 72 5f );. rc = pager_
12f71 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 wait_on_lock(pPa
12f72 67 65 72 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c ger, EXCLUSIVE_L
12f73 4f 43 4b 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 OCK);.. /* If t
12f74 68 65 20 66 69 6c 65 20 69 73 20 61 20 74 65 6d he file is a tem
12f75 70 2d 66 69 6c 65 20 68 61 73 20 6e 6f 74 20 79 p-file has not y
12f76 65 74 20 62 65 65 6e 20 6f 70 65 6e 65 64 2c 20 et been opened,
12f77 6f 70 65 6e 20 69 74 20 6e 6f 77 2e 20 49 74 0a open it now. It.
12f78 20 20 2a 2a 20 69 73 20 6e 6f 74 20 70 6f 73 73 ** is not poss
12f79 69 62 6c 65 20 66 6f 72 20 72 63 20 74 6f 20 62 ible for rc to b
12f7a 65 20 6f 74 68 65 72 20 74 68 61 6e 20 53 51 4c e other than SQL
12f7b 49 54 45 5f 4f 4b 20 69 66 20 74 68 69 73 20 62 ITE_OK if this b
12f7c 72 61 6e 63 68 0a 20 20 2a 2a 20 69 73 20 74 61 ranch. ** is ta
12f7d 6b 65 6e 2c 20 61 73 20 70 61 67 65 72 5f 77 61 ken, as pager_wa
12f7e 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 29 20 69 73 20 it_on_lock() is
12f7f 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 74 65 6d 70 a no-op for temp
12f80 2d 66 69 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 -files.. */. i
12f81 66 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 f( !isOpen(pPage
12f82 72 2d 3e 66 64 29 20 29 7b 0a 20 20 20 20 61 73 r->fd) ){. as
12f83 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 74 65 sert( pPager->te
12f84 6d 70 46 69 6c 65 20 26 26 20 72 63 3d 3d 53 51 mpFile && rc==SQ
12f85 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 72 LITE_OK );. r
12f86 63 20 3d 20 70 61 67 65 72 4f 70 65 6e 74 65 6d c = pagerOpentem
12f87 70 28 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 p(pPager, pPager
12f88 2d 3e 66 64 2c 20 70 50 61 67 65 72 2d 3e 76 66 ->fd, pPager->vf
12f89 73 46 6c 61 67 73 29 3b 0a 20 20 7d 0a 0a 20 20 sFlags);. }..
12f8a 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 while( rc==SQLIT
12f8b 45 5f 4f 4b 20 26 26 20 70 4c 69 73 74 20 29 7b E_OK && pList ){
12f8c 0a 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f 20 3d . Pgno pgno =
12f8d 20 70 4c 69 73 74 2d 3e 70 67 6e 6f 3b 0a 0a 20 pList->pgno;..
12f8e 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 /* If there a
12f8f 72 65 20 64 69 72 74 79 20 70 61 67 65 73 20 69 re dirty pages i
12f90 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 n the page cache
12f91 20 77 69 74 68 20 70 61 67 65 20 6e 75 6d 62 65 with page numbe
12f92 72 73 20 67 72 65 61 74 65 72 0a 20 20 20 20 2a rs greater. *
12f93 2a 20 74 68 61 6e 20 50 61 67 65 72 2e 64 62 53 * than Pager.dbS
12f94 69 7a 65 2c 20 74 68 69 73 20 6d 65 61 6e 73 20 ize, this means
12f95 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e sqlite3PagerTrun
12f96 63 61 74 65 49 6d 61 67 65 28 29 20 77 61 73 20 cateImage() was
12f97 63 61 6c 6c 65 64 20 74 6f 0a 20 20 20 20 2a 2a called to. **
12f98 20 6d 61 6b 65 20 74 68 65 20 66 69 6c 65 20 73 make the file s
12f99 6d 61 6c 6c 65 72 20 28 70 72 65 73 75 6d 61 62 maller (presumab
12f9a 6c 79 20 62 79 20 61 75 74 6f 2d 76 61 63 75 75 ly by auto-vacuu
12f9b 6d 20 63 6f 64 65 29 2e 20 44 6f 20 6e 6f 74 20 m code). Do not
12f9c 77 72 69 74 65 0a 20 20 20 20 2a 2a 20 61 6e 79 write. ** any
12f9d 20 73 75 63 68 20 70 61 67 65 73 20 74 6f 20 74 such pages to t
12f9e 68 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a he file.. **.
12f9f 20 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 64 6f 20 ** Also, do
12fa0 6e 6f 74 20 77 72 69 74 65 20 6f 75 74 20 61 6e not write out an
12fa1 79 20 70 61 67 65 20 74 68 61 74 20 68 61 73 20 y page that has
12fa2 74 68 65 20 50 47 48 44 52 5f 44 4f 4e 54 5f 57 the PGHDR_DONT_W
12fa3 52 49 54 45 20 66 6c 61 67 0a 20 20 20 20 2a 2a RITE flag. **
12fa4 20 73 65 74 20 28 73 65 74 20 62 79 20 73 71 6c set (set by sql
12fa5 69 74 65 33 50 61 67 65 72 44 6f 6e 74 57 72 69 ite3PagerDontWri
12fa6 74 65 28 29 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 te()).. */.
12fa7 20 20 69 66 28 20 70 67 6e 6f 3c 3d 70 50 61 67 if( pgno<=pPag
12fa8 65 72 2d 3e 64 62 53 69 7a 65 20 26 26 20 30 3d er->dbSize && 0=
12fa9 3d 28 70 4c 69 73 74 2d 3e 66 6c 61 67 73 26 50 =(pList->flags&P
12faa 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 29 GHDR_DONT_WRITE)
12fab 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 6f 66 ){. i64 of
12fac 66 73 65 74 20 3d 20 28 70 67 6e 6f 2d 31 29 2a fset = (pgno-1)*
12fad 28 69 36 34 29 70 50 61 67 65 72 2d 3e 70 61 67 (i64)pPager->pag
12fae 65 53 69 7a 65 3b 20 20 20 2f 2a 20 4f 66 66 73 eSize; /* Offs
12faf 65 74 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 et to write */.
12fb0 20 20 20 20 20 63 68 61 72 20 2a 70 44 61 74 61 char *pData
12fb1 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
12fb2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12fb3 20 20 20 20 2f 2a 20 44 61 74 61 20 74 6f 20 77 /* Data to w
12fb4 72 69 74 65 20 2a 2f 20 20 20 20 0a 0a 20 20 20 rite */ ..
12fb5 20 20 20 2f 2a 20 45 6e 63 6f 64 65 20 74 68 65 /* Encode the
12fb6 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 20 database */.
12fb7 20 20 20 43 4f 44 45 43 32 28 70 50 61 67 65 72 CODEC2(pPager
12fb8 2c 20 70 4c 69 73 74 2d 3e 70 44 61 74 61 2c 20 , pList->pData,
12fb9 70 67 6e 6f 2c 20 36 2c 20 72 65 74 75 72 6e 20 pgno, 6, return
12fba 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 70 44 SQLITE_NOMEM, pD
12fbb 61 74 61 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 ata);.. /*
12fbc 57 72 69 74 65 20 6f 75 74 20 74 68 65 20 70 61 Write out the pa
12fbd 67 65 20 64 61 74 61 2e 20 2a 2f 0a 20 20 20 20 ge data. */.
12fbe 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
12fbf 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 66 64 Write(pPager->fd
12fc0 2c 20 70 44 61 74 61 2c 20 70 50 61 67 65 72 2d , pData, pPager-
12fc1 3e 70 61 67 65 53 69 7a 65 2c 20 6f 66 66 73 65 >pageSize, offse
12fc2 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 t);.. /* If
12fc3 20 70 61 67 65 20 31 20 77 61 73 20 6a 75 73 74 page 1 was just
12fc4 20 77 72 69 74 74 65 6e 2c 20 75 70 64 61 74 65 written, update
12fc5 20 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65 72 Pager.dbFileVer
12fc6 73 20 74 6f 20 6d 61 74 63 68 0a 20 20 20 20 20 s to match.
12fc7 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6e 6f ** the value no
12fc8 77 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 w stored in the
12fc9 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 database file. I
12fca 66 20 77 72 69 74 69 6e 67 20 74 68 69 73 20 0a f writing this .
12fcb 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 63 61 ** page ca
12fcc 75 73 65 64 20 74 68 65 20 64 61 74 61 62 61 73 used the databas
12fcd 65 20 66 69 6c 65 20 74 6f 20 67 72 6f 77 2c 20 e file to grow,
12fce 75 70 64 61 74 65 20 64 62 46 69 6c 65 53 69 7a update dbFileSiz
12fcf 65 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 e. . */.
12fd0 20 20 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29 if( pgno==1 )
12fd1 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 {. memcpy
12fd2 28 26 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 (&pPager->dbFile
12fd3 56 65 72 73 2c 20 26 70 44 61 74 61 5b 32 34 5d Vers, &pData[24]
12fd4 2c 20 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d , sizeof(pPager-
12fd5 3e 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 >dbFileVers));.
12fd6 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
12fd7 20 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 62 pgno>pPager->db
12fd8 46 69 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 FileSize ){.
12fd9 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 46 69 pPager->dbFi
12fda 6c 65 53 69 7a 65 20 3d 20 70 67 6e 6f 3b 0a 20 leSize = pgno;.
12fdb 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a }.. /*
12fdc 20 55 70 64 61 74 65 20 61 6e 79 20 62 61 63 6b Update any back
12fdd 75 70 20 6f 62 6a 65 63 74 73 20 63 6f 70 79 69 up objects copyi
12fde 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 ng the contents
12fdf 6f 66 20 74 68 69 73 20 70 61 67 65 72 2e 20 2a of this pager. *
12fe0 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 /. sqlite3B
12fe1 61 63 6b 75 70 55 70 64 61 74 65 28 70 50 61 67 ackupUpdate(pPag
12fe2 65 72 2d 3e 70 42 61 63 6b 75 70 2c 20 70 67 6e er->pBackup, pgn
12fe3 6f 2c 20 28 75 38 2a 29 70 4c 69 73 74 2d 3e 70 o, (u8*)pList->p
12fe4 44 61 74 61 29 3b 0a 0a 20 20 20 20 20 20 50 41 Data);.. PA
12fe5 47 45 52 54 52 41 43 45 28 28 22 53 54 4f 52 45 GERTRACE(("STORE
12fe6 20 25 64 20 70 61 67 65 20 25 64 20 68 61 73 68 %d page %d hash
12fe7 28 25 30 38 78 29 5c 6e 22 2c 0a 20 20 20 20 20 (%08x)\n",.
12fe8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 41 PA
12fe9 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 GERID(pPager), p
12fea 67 6e 6f 2c 20 70 61 67 65 72 5f 70 61 67 65 68 gno, pager_pageh
12feb 61 73 68 28 70 4c 69 73 74 29 29 29 3b 0a 20 20 ash(pList)));.
12fec 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 50 47 IOTRACE(("PG
12fed 4f 55 54 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 OUT %p %d\n", pP
12fee 61 67 65 72 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 ager, pgno));.
12fef 20 20 20 20 50 41 47 45 52 5f 49 4e 43 52 28 73 PAGER_INCR(s
12ff0 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 77 72 69 qlite3_pager_wri
12ff1 74 65 64 62 5f 63 6f 75 6e 74 29 3b 0a 20 20 20 tedb_count);.
12ff2 20 20 20 50 41 47 45 52 5f 49 4e 43 52 28 70 50 PAGER_INCR(pP
12ff3 61 67 65 72 2d 3e 6e 57 72 69 74 65 29 3b 0a 20 ager->nWrite);.
12ff4 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
12ff5 50 41 47 45 52 54 52 41 43 45 28 28 22 4e 4f 53 PAGERTRACE(("NOS
12ff6 54 4f 52 45 20 25 64 20 70 61 67 65 20 25 64 5c TORE %d page %d\
12ff7 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 n", PAGERID(pPag
12ff8 65 72 29 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 20 er), pgno));.
12ff9 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 }.#ifdef SQLITE
12ffa 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 _CHECK_PAGES.
12ffb 20 70 4c 69 73 74 2d 3e 70 61 67 65 48 61 73 68 pList->pageHash
12ffc 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 = pager_pagehas
12ffd 68 28 70 4c 69 73 74 29 3b 0a 23 65 6e 64 69 66 h(pList);.#endif
12ffe 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 70 4c 69 . pList = pLi
12fff 73 74 2d 3e 70 44 69 72 74 79 3b 0a 20 20 7d 0a st->pDirty;. }.
13000 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
13001 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 61 20 ./*.** Append a
13002 72 65 63 6f 72 64 20 6f 66 20 74 68 65 20 63 75 record of the cu
13003 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66 20 70 rrent state of p
13004 61 67 65 20 70 50 67 20 74 6f 20 74 68 65 20 73 age pPg to the s
13005 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 0a 2a 2a 20 ub-journal. .**
13006 49 74 20 69 73 20 74 68 65 20 63 61 6c 6c 65 72 It is the caller
13007 73 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 s responsibility
13008 20 74 6f 20 75 73 65 20 73 75 62 6a 52 65 71 75 to use subjRequ
13009 69 72 65 73 50 61 67 65 28 29 20 74 6f 20 63 68 iresPage() to ch
1300a 65 63 6b 20 0a 2a 2a 20 74 68 61 74 20 69 74 20 eck .** that it
1300b 69 73 20 72 65 61 6c 6c 79 20 72 65 71 75 69 72 is really requir
1300c 65 64 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e ed before callin
1300d 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e g this function.
1300e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 .**.** If succes
1300f 73 66 75 6c 2c 20 73 65 74 20 74 68 65 20 62 69 sful, set the bi
13010 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 t corresponding
13011 74 6f 20 70 50 67 2d 3e 70 67 6e 6f 20 69 6e 20 to pPg->pgno in
13012 74 68 65 20 62 69 74 76 65 63 73 0a 2a 2a 20 66 the bitvecs.** f
13013 6f 72 20 61 6c 6c 20 6f 70 65 6e 20 73 61 76 65 or all open save
13014 70 6f 69 6e 74 73 20 62 65 66 6f 72 65 20 72 65 points before re
13015 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 turning..**.** T
13016 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 his function ret
13017 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 20 69 urns SQLITE_OK i
13018 66 20 65 76 65 72 79 74 68 69 6e 67 20 69 73 20 f everything is
13019 73 75 63 63 65 73 73 66 75 6c 2c 20 61 6e 20 49 successful, an I
1301a 4f 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 O.** error code
1301b 69 66 20 74 68 65 20 61 74 74 65 6d 70 74 20 74 if the attempt t
1301c 6f 20 77 72 69 74 65 20 74 6f 20 74 68 65 20 73 o write to the s
1301d 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 61 69 6c 73 ub-journal fails
1301e 2c 20 6f 72 20 0a 2a 2a 20 53 51 4c 49 54 45 5f , or .** SQLITE_
1301f 4e 4f 4d 45 4d 20 69 66 20 61 20 6d 61 6c 6c 6f NOMEM if a mallo
13020 63 20 66 61 69 6c 73 20 77 68 69 6c 65 20 73 65 c fails while se
13021 74 74 69 6e 67 20 61 20 62 69 74 20 69 6e 20 61 tting a bit in a
13022 20 73 61 76 65 70 6f 69 6e 74 0a 2a 2a 20 62 69 savepoint.** bi
13023 74 76 65 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 tvec..*/.static
13024 69 6e 74 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61 int subjournalPa
13025 67 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a ge(PgHdr *pPg){.
13026 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
13027 45 5f 4f 4b 3b 0a 20 20 50 61 67 65 72 20 2a 70 E_OK;. Pager *p
13028 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 Pager = pPg->pPa
13029 67 65 72 3b 0a 20 20 69 66 28 20 69 73 4f 70 65 ger;. if( isOpe
1302a 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 20 n(pPager->sjfd)
1302b 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 70 44 61 ){. void *pDa
1302c 74 61 20 3d 20 70 50 67 2d 3e 70 44 61 74 61 3b ta = pPg->pData;
1302d 0a 20 20 20 20 69 36 34 20 6f 66 66 73 65 74 20 . i64 offset
1302e 3d 20 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 = pPager->nSubRe
1302f 63 2a 28 34 2b 70 50 61 67 65 72 2d 3e 70 61 67 c*(4+pPager->pag
13030 65 53 69 7a 65 29 3b 0a 20 20 20 20 63 68 61 72 eSize);. char
13031 20 2a 70 44 61 74 61 32 3b 0a 0a 20 20 20 20 43 *pData2;.. C
13032 4f 44 45 43 32 28 70 50 61 67 65 72 2c 20 70 44 ODEC2(pPager, pD
13033 61 74 61 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 ata, pPg->pgno,
13034 37 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 7, return SQLITE
13035 5f 4e 4f 4d 45 4d 2c 20 70 44 61 74 61 32 29 3b _NOMEM, pData2);
13036 0a 20 20 20 20 50 41 47 45 52 54 52 41 43 45 28 . PAGERTRACE(
13037 28 22 53 54 4d 54 2d 4a 4f 55 52 4e 41 4c 20 25 ("STMT-JOURNAL %
13038 64 20 70 61 67 65 20 25 64 5c 6e 22 2c 20 50 41 d page %d\n", PA
13039 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 GERID(pPager), p
1303a 50 67 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 0a 20 Pg->pgno));. .
1303b 20 20 20 61 73 73 65 72 74 28 20 70 61 67 65 49 assert( pageI
1303c 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20 7c 7c nJournal(pPg) ||
1303d 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 67 65 pPg->pgno>pPage
1303e 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 3b r->dbOrigSize );
1303f 0a 20 20 20 20 72 63 20 3d 20 77 72 69 74 65 33 . rc = write3
13040 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 73 6a 2bits(pPager->sj
13041 66 64 2c 20 6f 66 66 73 65 74 2c 20 70 50 67 2d fd, offset, pPg-
13042 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 69 66 28 20 >pgno);. if(
13043 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
13044 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
13045 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 te3OsWrite(pPage
13046 72 2d 3e 73 6a 66 64 2c 20 70 44 61 74 61 32 2c r->sjfd, pData2,
13047 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a pPager->pageSiz
13048 65 2c 20 6f 66 66 73 65 74 2b 34 29 3b 0a 20 20 e, offset+4);.
13049 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 }. }. if( rc
1304a 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1304b 20 20 20 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 pPager->nSubR
1304c 65 63 2b 2b 3b 0a 20 20 20 20 61 73 73 65 72 74 ec++;. assert
1304d 28 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 ( pPager->nSavep
1304e 6f 69 6e 74 3e 30 20 29 3b 0a 20 20 20 20 72 63 oint>0 );. rc
1304f 20 3d 20 61 64 64 54 6f 53 61 76 65 70 6f 69 6e = addToSavepoin
13050 74 42 69 74 76 65 63 73 28 70 50 61 67 65 72 2c tBitvecs(pPager,
13051 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 7d pPg->pgno);. }
13052 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
13053 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
13054 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 ction is called
13055 62 79 20 74 68 65 20 70 63 61 63 68 65 20 6c 61 by the pcache la
13056 79 65 72 20 77 68 65 6e 20 69 74 20 68 61 73 20 yer when it has
13057 72 65 61 63 68 65 64 20 73 6f 6d 65 0a 2a 2a 20 reached some.**
13058 73 6f 66 74 20 6d 65 6d 6f 72 79 20 6c 69 6d 69 soft memory limi
13059 74 2e 20 54 68 65 20 66 69 72 73 74 20 61 72 67 t. The first arg
1305a 75 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 ument is a point
1305b 65 72 20 74 6f 20 61 20 50 61 67 65 72 20 6f 62 er to a Pager ob
1305c 6a 65 63 74 0a 2a 2a 20 28 63 61 73 74 20 61 73 ject.** (cast as
1305d 20 61 20 76 6f 69 64 2a 29 2e 20 54 68 65 20 70 a void*). The p
1305e 61 67 65 72 20 69 73 20 61 6c 77 61 79 73 20 27 ager is always '
1305f 70 75 72 67 65 61 62 6c 65 27 20 28 6e 6f 74 20 purgeable' (not
13060 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 an in-memory.**
13061 64 61 74 61 62 61 73 65 29 2e 20 54 68 65 20 73 database). The s
13062 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 econd argument i
13063 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f s a reference to
13064 20 61 20 70 61 67 65 20 74 68 61 74 20 69 73 20 a page that is
13065 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 64 69 .** currently di
13066 72 74 79 20 62 75 74 20 68 61 73 20 6e 6f 20 6f rty but has no o
13067 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72 utstanding refer
13068 65 6e 63 65 73 2e 20 54 68 65 20 70 61 67 65 0a ences. The page.
13069 2a 2a 20 69 73 20 61 6c 77 61 79 73 20 61 73 73 ** is always ass
1306a 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 ociated with the
1306b 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 70 61 Pager object pa
1306c 73 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 ssed as the firs
1306d 74 20 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 0a t .** argument..
1306e 2a 2a 0a 2a 2a 20 54 68 65 20 6a 6f 62 20 6f 66 **.** The job of
1306f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
13070 73 20 74 6f 20 6d 61 6b 65 20 70 50 67 20 63 6c s to make pPg cl
13071 65 61 6e 20 62 79 20 77 72 69 74 69 6e 67 20 69 ean by writing i
13072 74 73 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 6f ts contents.** o
13073 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ut to the databa
13074 73 65 20 66 69 6c 65 2c 20 69 66 20 70 6f 73 73 se file, if poss
13075 69 62 6c 65 2e 20 54 68 69 73 20 6d 61 79 20 69 ible. This may i
13076 6e 76 6f 6c 76 65 20 73 79 6e 63 69 6e 67 20 74 nvolve syncing t
13077 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 he.** journal fi
13078 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 le. .**.** If su
13079 63 63 65 73 73 66 75 6c 2c 20 73 71 6c 69 74 65 ccessful, sqlite
1307a 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 3PcacheMakeClean
1307b 28 29 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 () is called on
1307c 74 68 65 20 70 61 67 65 20 61 6e 64 0a 2a 2a 20 the page and.**
1307d 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e SQLITE_OK return
1307e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 ed. If an IO err
1307f 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 or occurs while
13080 74 72 79 69 6e 67 20 74 6f 20 6d 61 6b 65 20 74 trying to make t
13081 68 65 0a 2a 2a 20 70 61 67 65 20 63 6c 65 61 6e he.** page clean
13082 2c 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20 63 , the IO error c
13083 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e ode is returned.
13084 20 49 66 20 74 68 65 20 70 61 67 65 20 63 61 6e If the page can
13085 6e 6f 74 20 62 65 0a 2a 2a 20 6d 61 64 65 20 63 not be.** made c
13086 6c 65 61 6e 20 66 6f 72 20 73 6f 6d 65 20 6f 74 lean for some ot
13087 68 65 72 20 72 65 61 73 6f 6e 2c 20 62 75 74 20 her reason, but
13088 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c no error occurs,
13089 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a then SQLITE_OK.
1308a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20 62 ** is returned b
1308b 79 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d y sqlite3PcacheM
1308c 61 6b 65 43 6c 65 61 6e 28 29 20 69 73 20 6e 6f akeClean() is no
1308d 74 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 t called..*/.sta
1308e 74 69 63 20 69 6e 74 20 70 61 67 65 72 53 74 72 tic int pagerStr
1308f 65 73 73 28 76 6f 69 64 20 2a 70 2c 20 50 67 48 ess(void *p, PgH
13090 64 72 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 dr *pPg){. Page
13091 72 20 2a 70 50 61 67 65 72 20 3d 20 28 50 61 67 r *pPager = (Pag
13092 65 72 20 2a 29 70 3b 0a 20 20 69 6e 74 20 72 63 er *)p;. int rc
13093 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 = SQLITE_OK;..
13094 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 70 50 assert( pPg->pP
13095 61 67 65 72 3d 3d 70 50 61 67 65 72 20 29 3b 0a ager==pPager );.
13096 20 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 66 assert( pPg->f
13097 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 lags&PGHDR_DIRTY
13098 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 64 6f );.. /* The do
13099 4e 6f 74 53 79 6e 63 20 66 6c 61 67 20 69 73 20 NotSync flag is
1309a 73 65 74 20 62 79 20 74 68 65 20 73 71 6c 69 74 set by the sqlit
1309b 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20 66 e3PagerWrite() f
1309c 75 6e 63 74 69 6f 6e 20 77 68 69 6c 65 20 69 74 unction while it
1309d 0a 20 20 2a 2a 20 69 73 20 6a 6f 75 72 6e 61 6c . ** is journal
1309e 6c 69 6e 67 20 61 20 73 65 74 20 6f 66 20 74 77 ling a set of tw
1309f 6f 20 6f 72 20 6d 6f 72 65 20 64 61 74 61 62 61 o or more databa
130a0 73 65 20 70 61 67 65 73 20 74 68 61 74 20 61 72 se pages that ar
130a1 65 20 73 74 6f 72 65 64 0a 20 20 2a 2a 20 6f 6e e stored. ** on
130a2 20 74 68 65 20 73 61 6d 65 20 64 69 73 6b 20 73 the same disk s
130a3 65 63 74 6f 72 2e 20 53 79 6e 63 69 6e 67 20 74 ector. Syncing t
130a4 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6e 6f he journal is no
130a5 74 20 61 6c 6c 6f 77 65 64 20 77 68 69 6c 65 0a t allowed while.
130a6 20 20 2a 2a 20 74 68 69 73 20 69 73 20 68 61 70 ** this is hap
130a7 70 65 6e 69 6e 67 20 61 73 20 69 74 20 69 73 20 pening as it is
130a8 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20 61 important that a
130a9 6c 6c 20 6d 65 6d 62 65 72 73 20 6f 66 20 73 75 ll members of su
130aa 63 68 20 61 0a 20 20 2a 2a 20 73 65 74 20 6f 66 ch a. ** set of
130ab 20 70 61 67 65 73 20 61 72 65 20 73 79 6e 63 65 pages are synce
130ac 64 20 74 6f 20 64 69 73 6b 20 74 6f 67 65 74 68 d to disk togeth
130ad 65 72 2e 20 53 6f 2c 20 69 66 20 74 68 65 20 70 er. So, if the p
130ae 61 67 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f age this functio
130af 6e 0a 20 20 2a 2a 20 69 73 20 74 72 79 69 6e 67 n. ** is trying
130b0 20 74 6f 20 6d 61 6b 65 20 63 6c 65 61 6e 20 77 to make clean w
130b1 69 6c 6c 20 72 65 71 75 69 72 65 20 61 20 6a 6f ill require a jo
130b2 75 72 6e 61 6c 20 73 79 6e 63 20 61 6e 64 20 74 urnal sync and t
130b3 68 65 20 64 6f 4e 6f 74 53 79 6e 63 0a 20 20 2a he doNotSync. *
130b4 2a 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 72 * flag is set, r
130b5 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 64 6f eturn without do
130b6 69 6e 67 20 61 6e 79 74 68 69 6e 67 2e 20 54 68 ing anything. Th
130b7 65 20 70 63 61 63 68 65 20 6c 61 79 65 72 20 77 e pcache layer w
130b8 69 6c 6c 0a 20 20 2a 2a 20 6a 75 73 74 20 68 61 ill. ** just ha
130b9 76 65 20 74 6f 20 67 6f 20 61 68 65 61 64 20 61 ve to go ahead a
130ba 6e 64 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 nd allocate a ne
130bb 77 20 70 61 67 65 20 62 75 66 66 65 72 20 69 6e w page buffer in
130bc 73 74 65 61 64 20 6f 66 0a 20 20 2a 2a 20 72 65 stead of. ** re
130bd 75 73 69 6e 67 20 70 50 67 2e 0a 20 20 2a 2a 0a using pPg.. **.
130be 20 20 2a 2a 20 53 69 6d 69 6c 61 72 6c 79 2c 20 ** Similarly,
130bf 69 66 20 74 68 65 20 70 61 67 65 72 20 68 61 73 if the pager has
130c0 20 61 6c 72 65 61 64 79 20 65 6e 74 65 72 65 64 already entered
130c1 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 the error state
130c2 2c 20 64 6f 20 6e 6f 74 0a 20 20 2a 2a 20 74 72 , do not. ** tr
130c3 79 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 63 y to write the c
130c4 6f 6e 74 65 6e 74 73 20 6f 66 20 70 50 67 20 74 ontents of pPg t
130c5 6f 20 64 69 73 6b 2e 0a 20 20 2a 2f 0a 20 20 69 o disk.. */. i
130c6 66 28 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d f( NEVER(pPager-
130c7 3e 65 72 72 43 6f 64 65 29 0a 20 20 20 7c 7c 20 >errCode). ||
130c8 28 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 (pPager->doNotSy
130c9 6e 63 20 26 26 20 70 50 67 2d 3e 66 6c 61 67 73 nc && pPg->flags
130ca 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 &PGHDR_NEED_SYNC
130cb 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ). ){. retur
130cc 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
130cd 0a 0a 20 20 2f 2a 20 53 79 6e 63 20 74 68 65 20 .. /* Sync the
130ce 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 66 20 journal file if
130cf 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69 required. */. i
130d0 66 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 f( pPg->flags&PG
130d1 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 29 7b HDR_NEED_SYNC ){
130d2 0a 20 20 20 20 72 63 20 3d 20 73 79 6e 63 4a 6f . rc = syncJo
130d3 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 urnal(pPager);.
130d4 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
130d5 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e E_OK && pPager->
130d6 66 75 6c 6c 53 79 6e 63 20 26 26 20 0a 20 20 20 fullSync && .
130d7 20 20 20 21 28 70 50 61 67 65 72 2d 3e 6a 6f 75 !(pPager->jou
130d8 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f rnalMode==PAGER_
130d9 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f JOURNALMODE_MEMO
130da 52 59 29 20 26 26 0a 20 20 20 20 20 20 21 28 73 RY) &&. !(s
130db 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 qlite3OsDeviceCh
130dc 61 72 61 63 74 65 72 69 73 74 69 63 73 28 70 50 aracteristics(pP
130dd 61 67 65 72 2d 3e 66 64 29 26 53 51 4c 49 54 45 ager->fd)&SQLITE
130de 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 _IOCAP_SAFE_APPE
130df 4e 44 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 ND). ){.
130e0 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d 20 pPager->nRec =
130e1 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 77 72 0;. rc = wr
130e2 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 iteJournalHdr(pP
130e3 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d ager);. }. }
130e4 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 .. /* If the pa
130e5 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 69 ge number of thi
130e6 73 20 70 61 67 65 20 69 73 20 6c 61 72 67 65 72 s page is larger
130e7 20 74 68 61 6e 20 74 68 65 20 63 75 72 72 65 6e than the curren
130e8 74 20 73 69 7a 65 20 6f 66 0a 20 20 2a 2a 20 74 t size of. ** t
130e9 68 65 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 he database imag
130ea 65 2c 20 69 74 20 6d 61 79 20 6e 65 65 64 20 74 e, it may need t
130eb 6f 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 20 o be written to
130ec 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e the sub-journal.
130ed 0a 20 20 2a 2a 20 54 68 69 73 20 69 73 20 62 65 . ** This is be
130ee 63 61 75 73 65 20 74 68 65 20 63 61 6c 6c 20 74 cause the call t
130ef 6f 20 70 61 67 65 72 5f 77 72 69 74 65 5f 70 61 o pager_write_pa
130f0 67 65 6c 69 73 74 28 29 20 62 65 6c 6f 77 20 77 gelist() below w
130f1 69 6c 6c 20 6e 6f 74 0a 20 20 2a 2a 20 61 63 74 ill not. ** act
130f2 75 61 6c 6c 79 20 77 72 69 74 65 20 64 61 74 61 ually write data
130f3 20 74 6f 20 74 68 65 20 66 69 6c 65 20 69 6e 20 to the file in
130f4 74 68 69 73 20 63 61 73 65 2e 0a 20 20 2a 2a 0a this case.. **.
130f5 20 20 2a 2a 20 43 6f 6e 73 69 64 65 72 20 74 68 ** Consider th
130f6 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 71 75 e following sequ
130f7 65 6e 63 65 20 6f 66 20 65 76 65 6e 74 73 3a 0a ence of events:.
130f8 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 42 45 47 49 **. ** BEGI
130f9 4e 3b 0a 20 20 2a 2a 20 20 20 20 20 3c 6a 6f 75 N;. ** <jou
130fa 72 6e 61 6c 20 70 61 67 65 20 58 3e 0a 20 20 2a rnal page X>. *
130fb 2a 20 20 20 20 20 3c 6d 6f 64 69 66 79 20 70 61 * <modify pa
130fc 67 65 20 58 3e 0a 20 20 2a 2a 20 20 20 20 20 53 ge X>. ** S
130fd 41 56 45 50 4f 49 4e 54 20 73 70 3b 0a 20 20 2a AVEPOINT sp;. *
130fe 2a 20 20 20 20 20 20 20 3c 73 68 72 69 6e 6b 20 * <shrink
130ff 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f database file to
13100 20 59 20 70 61 67 65 73 3e 0a 20 20 2a 2a 20 20 Y pages>. **
13101 20 20 20 20 20 70 61 67 65 72 53 74 72 65 73 73 pagerStress
13102 28 70 61 67 65 20 58 29 0a 20 20 2a 2a 20 20 20 (page X). **
13103 20 20 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 73 70 ROLLBACK TO sp
13104 3b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 28 ;. **. ** If (
13105 58 3e 59 29 2c 20 74 68 65 6e 20 77 68 65 6e 20 X>Y), then when
13106 70 61 67 65 72 53 74 72 65 73 73 20 69 73 20 63 pagerStress is c
13107 61 6c 6c 65 64 20 70 61 67 65 20 58 20 77 69 6c alled page X wil
13108 6c 20 6e 6f 74 20 62 65 20 77 72 69 74 74 65 6e l not be written
13109 0a 20 20 2a 2a 20 6f 75 74 20 74 6f 20 74 68 65 . ** out to the
1310a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 database file,
1310b 62 75 74 20 77 69 6c 6c 20 62 65 20 64 72 6f 70 but will be drop
1310c 70 65 64 20 66 72 6f 6d 20 74 68 65 20 63 61 63 ped from the cac
1310d 68 65 2e 20 54 68 65 6e 2c 0a 20 20 2a 2a 20 66 he. Then,. ** f
1310e 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 22 52 4f ollowing the "RO
1310f 4c 4c 42 41 43 4b 20 54 4f 20 73 70 22 20 73 74 LLBACK TO sp" st
13110 61 74 65 6d 65 6e 74 2c 20 72 65 61 64 69 6e 67 atement, reading
13111 20 70 61 67 65 20 58 20 77 69 6c 6c 20 72 65 61 page X will rea
13112 64 0a 20 20 2a 2a 20 64 61 74 61 20 66 72 6f 6d d. ** data from
13113 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
13114 6c 65 2e 20 54 68 69 73 20 77 69 6c 6c 20 62 65 le. This will be
13115 20 74 68 65 20 63 6f 70 79 20 6f 66 20 70 61 67 the copy of pag
13116 65 20 58 20 61 73 20 69 74 0a 20 20 2a 2a 20 77 e X as it. ** w
13117 61 73 20 77 68 65 6e 20 74 68 65 20 74 72 61 6e as when the tran
13118 73 61 63 74 69 6f 6e 20 73 74 61 72 74 65 64 2c saction started,
13119 20 6e 6f 74 20 61 73 20 69 74 20 77 61 73 20 77 not as it was w
1311a 68 65 6e 20 22 53 41 56 45 50 4f 49 4e 54 20 73 hen "SAVEPOINT s
1311b 70 22 0a 20 20 2a 2a 20 77 61 73 20 65 78 65 63 p". ** was exec
1311c 75 74 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 uted.. **. **
1311d 54 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20 The solution is
1311e 74 6f 20 77 72 69 74 65 20 74 68 65 20 63 75 72 to write the cur
1311f 72 65 6e 74 20 64 61 74 61 20 66 6f 72 20 70 61 rent data for pa
13120 67 65 20 58 20 69 6e 74 6f 20 74 68 65 20 0a 20 ge X into the .
13121 20 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 ** sub-journal
13122 66 69 6c 65 20 6e 6f 77 20 28 69 66 20 69 74 20 file now (if it
13123 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 74 is not already t
13124 68 65 72 65 29 2c 20 73 6f 20 74 68 61 74 20 69 here), so that i
13125 74 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 72 t will. ** be r
13126 65 73 74 6f 72 65 64 20 74 6f 20 69 74 73 20 63 estored to its c
13127 75 72 72 65 6e 74 20 76 61 6c 75 65 20 77 68 65 urrent value whe
13128 6e 20 74 68 65 20 22 52 4f 4c 4c 42 41 43 4b 20 n the "ROLLBACK
13129 54 4f 20 73 70 22 20 69 73 20 0a 20 20 2a 2a 20 TO sp" is . **
1312a 65 78 65 63 75 74 65 64 2e 0a 20 20 2a 2f 0a 20 executed.. */.
1312b 20 69 66 28 20 4e 45 56 45 52 28 0a 20 20 20 20 if( NEVER(.
1312c 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 rc==SQLITE_OK
1312d 26 26 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 && pPg->pgno>pPa
1312e 67 65 72 2d 3e 64 62 53 69 7a 65 20 26 26 20 73 ger->dbSize && s
1312f 75 62 6a 52 65 71 75 69 72 65 73 50 61 67 65 28 ubjRequiresPage(
13130 70 50 67 29 0a 20 20 29 20 29 7b 0a 20 20 20 20 pPg). ) ){.
13131 72 63 20 3d 20 73 75 62 6a 6f 75 72 6e 61 6c 50 rc = subjournalP
13132 61 67 65 28 70 50 67 29 3b 0a 20 20 7d 0a 0a 20 age(pPg);. }..
13133 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f /* Write the co
13134 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 ntents of the pa
13135 67 65 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 ge out to the da
13136 74 61 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a tabase file. */.
13137 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
13138 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 67 2d 3e _OK ){. pPg->
13139 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 20 20 pDirty = 0;.
1313a 72 63 20 3d 20 70 61 67 65 72 5f 77 72 69 74 65 rc = pager_write
1313b 5f 70 61 67 65 6c 69 73 74 28 70 50 67 29 3b 0a _pagelist(pPg);.
1313c 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 72 6b 20 74 }.. /* Mark t
1313d 68 65 20 70 61 67 65 20 61 73 20 63 6c 65 61 6e he page as clean
1313e 2e 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 . */. if( rc==S
1313f 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
13140 50 41 47 45 52 54 52 41 43 45 28 28 22 53 54 52 PAGERTRACE(("STR
13141 45 53 53 20 25 64 20 70 61 67 65 20 25 64 5c 6e ESS %d page %d\n
13142 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65 ", PAGERID(pPage
13143 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 29 3b r), pPg->pgno));
13144 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 . sqlite3Pcac
13145 68 65 4d 61 6b 65 43 6c 65 61 6e 28 70 50 67 29 heMakeClean(pPg)
13146 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 ;. }.. return
13147 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 pager_error(pPag
13148 65 72 2c 20 72 63 29 3b 0a 7d 0a 0a 0a 2f 2a 0a er, rc);.}.../*.
13149 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 ** Allocate and
1314a 69 6e 69 74 69 61 6c 69 7a 65 20 61 20 6e 65 77 initialize a new
1314b 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 61 6e Pager object an
1314c 64 20 70 75 74 20 61 20 70 6f 69 6e 74 65 72 20 d put a pointer
1314d 74 6f 20 69 74 0a 2a 2a 20 69 6e 20 2a 70 70 50 to it.** in *ppP
1314e 61 67 65 72 2e 20 54 68 65 20 70 61 67 65 72 20 ager. The pager
1314f 73 68 6f 75 6c 64 20 65 76 65 6e 74 75 61 6c 6c should eventuall
13150 79 20 62 65 20 66 72 65 65 64 20 62 79 20 70 61 y be freed by pa
13151 73 73 69 6e 67 20 69 74 0a 2a 2a 20 74 6f 20 73 ssing it.** to s
13152 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65 qlite3PagerClose
13153 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 46 ()..**.** The zF
13154 69 6c 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 ilename argument
13155 20 69 73 20 74 68 65 20 70 61 74 68 20 74 6f 20 is the path to
13156 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
13157 65 20 74 6f 20 6f 70 65 6e 2e 0a 2a 2a 20 49 66 e to open..** If
13158 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 4e 55 zFilename is NU
13159 4c 4c 20 74 68 65 6e 20 61 20 72 61 6e 64 6f 6d LL then a random
1315a 6c 79 2d 6e 61 6d 65 64 20 74 65 6d 70 6f 72 61 ly-named tempora
1315b 72 79 20 66 69 6c 65 20 69 73 20 63 72 65 61 74 ry file is creat
1315c 65 64 0a 2a 2a 20 61 6e 64 20 75 73 65 64 20 61 ed.** and used a
1315d 73 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62 65 s the file to be
1315e 20 63 61 63 68 65 64 2e 20 54 65 6d 70 6f 72 61 cached. Tempora
1315f 72 79 20 66 69 6c 65 73 20 61 72 65 20 62 65 20 ry files are be
13160 64 65 6c 65 74 65 64 0a 2a 2a 20 61 75 74 6f 6d deleted.** autom
13161 61 74 69 63 61 6c 6c 79 20 77 68 65 6e 20 74 68 atically when th
13162 65 79 20 61 72 65 20 63 6c 6f 73 65 64 2e 20 49 ey are closed. I
13163 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 22 f zFilename is "
13164 3a 6d 65 6d 6f 72 79 3a 22 20 74 68 65 6e 20 0a :memory:" then .
13165 2a 2a 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 ** all informati
13166 6f 6e 20 69 73 20 68 65 6c 64 20 69 6e 20 63 61 on is held in ca
13167 63 68 65 2e 20 49 74 20 69 73 20 6e 65 76 65 72 che. It is never
13168 20 77 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b written to disk
13169 2e 20 0a 2a 2a 20 54 68 69 73 20 63 61 6e 20 62 . .** This can b
1316a 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d e used to implem
1316b 65 6e 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 ent an in-memory
1316c 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a database..**.**
1316d 20 54 68 65 20 6e 45 78 74 72 61 20 70 61 72 61 The nExtra para
1316e 6d 65 74 65 72 20 73 70 65 63 69 66 69 65 73 20 meter specifies
1316f 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
13170 74 65 73 20 6f 66 20 73 70 61 63 65 20 61 6c 6c tes of space all
13171 6f 63 61 74 65 64 0a 2a 2a 20 61 6c 6f 6e 67 20 ocated.** along
13172 77 69 74 68 20 65 61 63 68 20 70 61 67 65 20 72 with each page r
13173 65 66 65 72 65 6e 63 65 2e 20 54 68 69 73 20 73 eference. This s
13174 70 61 63 65 20 69 73 20 61 76 61 69 6c 61 62 6c pace is availabl
13175 65 20 74 6f 20 74 68 65 20 75 73 65 72 0a 2a 2a e to the user.**
13176 20 76 69 61 20 74 68 65 20 73 71 6c 69 74 65 33 via the sqlite3
13177 50 61 67 65 72 47 65 74 45 78 74 72 61 28 29 20 PagerGetExtra()
13178 41 50 49 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 API..**.** The f
13179 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20 69 73 lags argument is
1317a 20 75 73 65 64 20 74 6f 20 73 70 65 63 69 66 79 used to specify
1317b 20 70 72 6f 70 65 72 74 69 65 73 20 74 68 61 74 properties that
1317c 20 61 66 66 65 63 74 20 74 68 65 0a 2a 2a 20 6f affect the.** o
1317d 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 peration of the
1317e 70 61 67 65 72 2e 20 49 74 20 73 68 6f 75 6c 64 pager. It should
1317f 20 62 65 20 70 61 73 73 65 64 20 73 6f 6d 65 20 be passed some
13180 62 69 74 77 69 73 65 20 63 6f 6d 62 69 6e 61 74 bitwise combinat
13181 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 50 41 ion.** of the PA
13182 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c GER_OMIT_JOURNAL
13183 20 61 6e 64 20 50 41 47 45 52 5f 4e 4f 5f 52 45 and PAGER_NO_RE
13184 41 44 4c 4f 43 4b 20 66 6c 61 67 73 2e 0a 2a 2a ADLOCK flags..**
13185 0a 2a 2a 20 54 68 65 20 76 66 73 46 6c 61 67 73 .** The vfsFlags
13186 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 20 parameter is a
13187 62 69 74 6d 61 73 6b 20 74 6f 20 70 61 73 73 20 bitmask to pass
13188 74 6f 20 74 68 65 20 66 6c 61 67 73 20 70 61 72 to the flags par
13189 61 6d 65 74 65 72 0a 2a 2a 20 6f 66 20 74 68 65 ameter.** of the
1318a 20 78 4f 70 65 6e 28 29 20 6d 65 74 68 6f 64 20 xOpen() method
1318b 6f 66 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 of the supplied
1318c 56 46 53 20 77 68 65 6e 20 6f 70 65 6e 69 6e 67 VFS when opening
1318d 20 66 69 6c 65 73 2e 20 0a 2a 2a 0a 2a 2a 20 49 files. .**.** I
1318e 66 20 74 68 65 20 70 61 67 65 72 20 6f 62 6a 65 f the pager obje
1318f 63 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 ct is allocated
13190 61 6e 64 20 74 68 65 20 73 70 65 63 69 66 69 65 and the specifie
13191 64 20 66 69 6c 65 20 6f 70 65 6e 65 64 20 0a 2a d file opened .*
13192 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 * successfully,
13193 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
13194 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 50 61 67 urned and *ppPag
13195 65 72 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 er set to point
13196 74 6f 0a 2a 2a 20 74 68 65 20 6e 65 77 20 70 61 to.** the new pa
13197 67 65 72 20 6f 62 6a 65 63 74 2e 20 49 66 20 61 ger object. If a
13198 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 n error occurs,
13199 2a 70 70 50 61 67 65 72 20 69 73 20 73 65 74 20 *ppPager is set
1319a 74 6f 20 4e 55 4c 4c 0a 2a 2a 20 61 6e 64 20 65 to NULL.** and e
1319b 72 72 6f 72 20 63 6f 64 65 20 72 65 74 75 72 6e rror code return
1319c 65 64 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f ed. This functio
1319d 6e 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c n may return SQL
1319e 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20 28 73 71 ITE_NOMEM.** (sq
1319f 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 20 69 73 lite3Malloc() is
131a0 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 used to allocat
131a1 65 20 6d 65 6d 6f 72 79 29 2c 20 53 51 4c 49 54 e memory), SQLIT
131a2 45 5f 43 41 4e 54 4f 50 45 4e 20 6f 72 20 0a 2a E_CANTOPEN or .*
131a3 2a 20 76 61 72 69 6f 75 73 20 53 51 4c 49 54 45 * various SQLITE
131a4 5f 49 4f 5f 58 58 58 20 65 72 72 6f 72 73 2e 0a _IO_XXX errors..
131a5 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
131a6 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
131a7 65 72 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 erOpen(. sqlite
131a8 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 3_vfs *pVfs,
131a9 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 /* The virtua
131aa 6c 20 66 69 6c 65 20 73 79 73 74 65 6d 20 74 6f l file system to
131ab 20 75 73 65 20 2a 2f 0a 20 20 50 61 67 65 72 20 use */. Pager
131ac 2a 2a 70 70 50 61 67 65 72 2c 20 20 20 20 20 20 **ppPager,
131ad 20 20 20 2f 2a 20 4f 55 54 3a 20 52 65 74 75 72 /* OUT: Retur
131ae 6e 20 74 68 65 20 50 61 67 65 72 20 73 74 72 75 n the Pager stru
131af 63 74 75 72 65 20 68 65 72 65 20 2a 2f 0a 20 20 cture here */.
131b0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c const char *zFil
131b1 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 4e 61 6d 65 ename, /* Name
131b2 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
131b3 20 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f file to open */
131b4 0a 20 20 69 6e 74 20 6e 45 78 74 72 61 2c 20 20 . int nExtra,
131b5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 /* E
131b6 78 74 72 61 20 62 79 74 65 73 20 61 70 70 65 6e xtra bytes appen
131b7 64 20 74 6f 20 65 61 63 68 20 69 6e 2d 6d 65 6d d to each in-mem
131b8 6f 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e ory page */. in
131b9 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 t flags,
131ba 20 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 /* flags
131bb 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 69 73 controlling this
131bc 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 76 file */. int v
131bd 66 73 46 6c 61 67 73 2c 20 20 20 20 20 20 20 20 fsFlags,
131be 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 70 61 73 /* flags pas
131bf 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 73 sed through to s
131c0 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e qlite3_vfs.xOpen
131c1 28 29 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 () */. void (*x
131c2 52 65 69 6e 69 74 29 28 44 62 50 61 67 65 2a 29 Reinit)(DbPage*)
131c3 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 /* Function to
131c4 72 65 69 6e 69 74 69 61 6c 69 7a 65 20 70 61 67 reinitialize pag
131c5 65 73 20 2a 2f 0a 29 7b 0a 20 20 75 38 20 2a 70 es */.){. u8 *p
131c6 50 74 72 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 Ptr;. Pager *pP
131c7 61 67 65 72 20 3d 20 30 3b 20 20 20 20 20 20 20 ager = 0;
131c8 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 /* Pager object
131c9 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 to allocate and
131ca 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 return */. int
131cb 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 rc = SQLITE_OK;
131cc 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 /* Return c
131cd 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 74 65 6d ode */. int tem
131ce 70 46 69 6c 65 20 3d 20 30 3b 20 20 20 20 20 20 pFile = 0;
131cf 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 74 65 /* True for te
131d0 6d 70 20 66 69 6c 65 73 20 28 69 6e 63 6c 2e 20 mp files (incl.
131d1 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 73 29 in-memory files)
131d2 20 2a 2f 0a 20 20 69 6e 74 20 6d 65 6d 44 62 20 */. int memDb
131d3 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f = 0; /
131d4 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 69 * True if this i
131d5 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 s an in-memory f
131d6 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 61 ile */. int rea
131d7 64 4f 6e 6c 79 20 3d 20 30 3b 20 20 20 20 20 20 dOnly = 0;
131d8 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 /* True if thi
131d9 73 20 69 73 20 61 20 72 65 61 64 2d 6f 6e 6c 79 s is a read-only
131da 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6a file */. int j
131db 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 3b 20 ournalFileSize;
131dc 20 20 20 20 2f 2a 20 42 79 74 65 73 20 74 6f 20 /* Bytes to
131dd 61 6c 6c 6f 63 61 74 65 20 66 6f 72 20 65 61 63 allocate for eac
131de 68 20 6a 6f 75 72 6e 61 6c 20 66 64 20 2a 2f 0a h journal fd */.
131df 20 20 63 68 61 72 20 2a 7a 50 61 74 68 6e 61 6d char *zPathnam
131e0 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 46 75 e = 0; /* Fu
131e1 6c 6c 20 70 61 74 68 20 74 6f 20 64 61 74 61 62 ll path to datab
131e2 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e ase file */. in
131e3 74 20 6e 50 61 74 68 6e 61 6d 65 20 3d 20 30 3b t nPathname = 0;
131e4 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
131e5 20 6f 66 20 62 79 74 65 73 20 69 6e 20 7a 50 61 of bytes in zPa
131e6 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 thname */. int
131e7 75 73 65 4a 6f 75 72 6e 61 6c 20 3d 20 28 66 6c useJournal = (fl
131e8 61 67 73 20 26 20 50 41 47 45 52 5f 4f 4d 49 54 ags & PAGER_OMIT
131e9 5f 4a 4f 55 52 4e 41 4c 29 3d 3d 30 3b 20 2f 2a _JOURNAL)==0; /*
131ea 20 46 61 6c 73 65 20 74 6f 20 6f 6d 69 74 20 6a False to omit j
131eb 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 6e 74 20 ournal */. int
131ec 6e 6f 52 65 61 64 6c 6f 63 6b 20 3d 20 28 66 6c noReadlock = (fl
131ed 61 67 73 20 26 20 50 41 47 45 52 5f 4e 4f 5f 52 ags & PAGER_NO_R
131ee 45 41 44 4c 4f 43 4b 29 21 3d 30 3b 20 20 2f 2a EADLOCK)!=0; /*
131ef 20 54 72 75 65 20 74 6f 20 6f 6d 69 74 20 72 65 True to omit re
131f0 61 64 2d 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 ad-lock */. int
131f1 20 70 63 61 63 68 65 53 69 7a 65 20 3d 20 73 71 pcacheSize = sq
131f2 6c 69 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 lite3PcacheSize(
131f3 29 3b 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 ); /* Byte
131f4 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 66 6f s to allocate fo
131f5 72 20 50 43 61 63 68 65 20 2a 2f 0a 20 20 75 31 r PCache */. u1
131f6 36 20 73 7a 50 61 67 65 44 66 6c 74 20 3d 20 53 6 szPageDflt = S
131f7 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 QLITE_DEFAULT_PA
131f8 47 45 5f 53 49 5a 45 3b 20 20 2f 2a 20 44 65 66 GE_SIZE; /* Def
131f9 61 75 6c 74 20 70 61 67 65 20 73 69 7a 65 20 2a ault page size *
131fa 2f 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f /.. /* Figure o
131fb 75 74 20 68 6f 77 20 6d 75 63 68 20 73 70 61 63 ut how much spac
131fc 65 20 69 73 20 72 65 71 75 69 72 65 64 20 66 6f e is required fo
131fd 72 20 65 61 63 68 20 6a 6f 75 72 6e 61 6c 20 66 r each journal f
131fe 69 6c 65 2d 68 61 6e 64 6c 65 0a 20 20 2a 2a 20 ile-handle. **
131ff 28 74 68 65 72 65 20 61 72 65 20 74 77 6f 20 6f (there are two o
13200 66 20 74 68 65 6d 2c 20 74 68 65 20 6d 61 69 6e f them, the main
13201 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74 68 65 journal and the
13202 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 29 2e 20 54 sub-journal). T
13203 68 69 73 0a 20 20 2a 2a 20 69 73 20 74 68 65 20 his. ** is the
13204 6d 61 78 69 6d 75 6d 20 73 70 61 63 65 20 72 65 maximum space re
13205 71 75 69 72 65 64 20 66 6f 72 20 61 6e 20 69 6e quired for an in
13206 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 -memory journal
13207 66 69 6c 65 20 68 61 6e 64 6c 65 20 0a 20 20 2a file handle . *
13208 2a 20 61 6e 64 20 61 20 72 65 67 75 6c 61 72 20 * and a regular
13209 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2d 68 61 6e journal file-han
1320a 64 6c 65 2e 20 4e 6f 74 65 20 74 68 61 74 20 61 dle. Note that a
1320b 20 22 72 65 67 75 6c 61 72 20 6a 6f 75 72 6e 61 "regular journa
1320c 6c 2d 68 61 6e 64 6c 65 22 0a 20 20 2a 2a 20 6d l-handle". ** m
1320d 61 79 20 62 65 20 61 20 77 72 61 70 70 65 72 20 ay be a wrapper
1320e 63 61 70 61 62 6c 65 20 6f 66 20 63 61 63 68 69 capable of cachi
1320f 6e 67 20 74 68 65 20 66 69 72 73 74 20 70 6f 72 ng the first por
13210 74 69 6f 6e 20 6f 66 20 74 68 65 20 6a 6f 75 72 tion of the jour
13211 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 69 6e nal. ** file in
13212 20 6d 65 6d 6f 72 79 20 74 6f 20 69 6d 70 6c 65 memory to imple
13213 6d 65 6e 74 20 74 68 65 20 61 74 6f 6d 69 63 2d ment the atomic-
13214 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 write optimizati
13215 6f 6e 20 28 73 65 65 20 0a 20 20 2a 2a 20 73 6f on (see . ** so
13216 75 72 63 65 20 66 69 6c 65 20 6a 6f 75 72 6e 61 urce file journa
13217 6c 2e 63 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 l.c).. */. if(
13218 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 sqlite3JournalS
13219 69 7a 65 28 70 56 66 73 29 3e 73 71 6c 69 74 65 ize(pVfs)>sqlite
1321a 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 3MemJournalSize(
1321b 29 20 29 7b 0a 20 20 20 20 6a 6f 75 72 6e 61 6c ) ){. journal
1321c 46 69 6c 65 53 69 7a 65 20 3d 20 52 4f 55 4e 44 FileSize = ROUND
1321d 38 28 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 8(sqlite3Journal
1321e 53 69 7a 65 28 70 56 66 73 29 29 3b 0a 20 20 7d Size(pVfs));. }
1321f 65 6c 73 65 7b 0a 20 20 20 20 6a 6f 75 72 6e 61 else{. journa
13220 6c 46 69 6c 65 53 69 7a 65 20 3d 20 52 4f 55 4e lFileSize = ROUN
13221 44 38 28 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 D8(sqlite3MemJou
13222 72 6e 61 6c 53 69 7a 65 28 29 29 3b 0a 20 20 7d rnalSize());. }
13223 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6f .. /* Set the o
13224 75 74 70 75 74 20 76 61 72 69 61 62 6c 65 20 74 utput variable t
13225 6f 20 4e 55 4c 4c 20 69 6e 20 63 61 73 65 20 61 o NULL in case a
13226 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 20 n error occurs.
13227 2a 2f 0a 20 20 2a 70 70 50 61 67 65 72 20 3d 20 */. *ppPager =
13228 30 3b 0a 0a 20 20 2f 2a 20 43 6f 6d 70 75 74 65 0;.. /* Compute
13229 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 66 and store the f
1322a 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20 69 6e 20 ull pathname in
1322b 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 an allocated buf
1322c 66 65 72 20 70 6f 69 6e 74 65 64 0a 20 20 2a 2a fer pointed. **
1322d 20 74 6f 20 62 79 20 7a 50 61 74 68 6e 61 6d 65 to by zPathname
1322e 2c 20 6c 65 6e 67 74 68 20 6e 50 61 74 68 6e 61 , length nPathna
1322f 6d 65 2e 20 4f 72 2c 20 69 66 20 74 68 69 73 20 me. Or, if this
13230 69 73 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 is a temporary f
13231 69 6c 65 2c 0a 20 20 2a 2a 20 6c 65 61 76 65 20 ile,. ** leave
13232 62 6f 74 68 20 6e 50 61 74 68 6e 61 6d 65 20 61 both nPathname a
13233 6e 64 20 7a 50 61 74 68 6e 61 6d 65 20 73 65 74 nd zPathname set
13234 20 74 6f 20 30 2e 0a 20 20 2a 2f 0a 20 20 69 66 to 0.. */. if
13235 28 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 7a ( zFilename && z
13236 46 69 6c 65 6e 61 6d 65 5b 30 5d 20 29 7b 0a 20 Filename[0] ){.
13237 20 20 20 6e 50 61 74 68 6e 61 6d 65 20 3d 20 70 nPathname = p
13238 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b Vfs->mxPathname+
13239 31 3b 0a 20 20 20 20 7a 50 61 74 68 6e 61 6d 65 1;. zPathname
1323a 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 = sqlite3Malloc
1323b 28 6e 50 61 74 68 6e 61 6d 65 2a 32 29 3b 0a 20 (nPathname*2);.
1323c 20 20 20 69 66 28 20 7a 50 61 74 68 6e 61 6d 65 if( zPathname
1323d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 ==0 ){. ret
1323e 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
1323f 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 ;. }.#ifndef
13240 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f SQLITE_OMIT_MEMO
13241 52 59 44 42 0a 20 20 20 20 69 66 28 20 73 74 72 RYDB. if( str
13242 63 6d 70 28 7a 46 69 6c 65 6e 61 6d 65 2c 22 3a cmp(zFilename,":
13243 6d 65 6d 6f 72 79 3a 22 29 3d 3d 30 20 29 7b 0a memory:")==0 ){.
13244 20 20 20 20 20 20 6d 65 6d 44 62 20 3d 20 31 3b memDb = 1;
13245 0a 20 20 20 20 20 20 7a 50 61 74 68 6e 61 6d 65 . zPathname
13246 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c [0] = 0;. }el
13247 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b 0a se.#endif. {.
13248 20 20 20 20 20 20 7a 50 61 74 68 6e 61 6d 65 5b zPathname[
13249 30 5d 20 3d 20 30 3b 20 2f 2a 20 4d 61 6b 65 20 0] = 0; /* Make
1324a 73 75 72 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 sure initialized
1324b 20 65 76 65 6e 20 69 66 20 46 75 6c 6c 50 61 74 even if FullPat
1324c 68 6e 61 6d 65 28 29 20 66 61 69 6c 73 20 2a 2f hname() fails */
1324d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
1324e 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d te3OsFullPathnam
1324f 65 28 70 56 66 73 2c 20 7a 46 69 6c 65 6e 61 6d e(pVfs, zFilenam
13250 65 2c 20 6e 50 61 74 68 6e 61 6d 65 2c 20 7a 50 e, nPathname, zP
13251 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a athname);. }.
13252 0a 20 20 20 20 6e 50 61 74 68 6e 61 6d 65 20 3d . nPathname =
13253 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
13254 28 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 (zPathname);.
13255 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
13256 4f 4b 20 26 26 20 6e 50 61 74 68 6e 61 6d 65 2b OK && nPathname+
13257 38 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 8>pVfs->mxPathna
13258 6d 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 me ){. /* T
13259 68 69 73 20 62 72 61 6e 63 68 20 69 73 20 74 61 his branch is ta
1325a 6b 65 6e 20 77 68 65 6e 20 74 68 65 20 6a 6f 75 ken when the jou
1325b 72 6e 61 6c 20 70 61 74 68 20 72 65 71 75 69 72 rnal path requir
1325c 65 64 20 62 79 0a 20 20 20 20 20 20 2a 2a 20 74 ed by. ** t
1325d 68 65 20 64 61 74 61 62 61 73 65 20 62 65 69 6e he database bein
1325e 67 20 6f 70 65 6e 65 64 20 77 69 6c 6c 20 62 65 g opened will be
1325f 20 6d 6f 72 65 20 74 68 61 6e 20 70 56 66 73 2d more than pVfs-
13260 3e 6d 78 50 61 74 68 6e 61 6d 65 0a 20 20 20 20 >mxPathname.
13261 20 20 2a 2a 20 62 79 74 65 73 20 69 6e 20 6c 65 ** bytes in le
13262 6e 67 74 68 2e 20 54 68 69 73 20 6d 65 61 6e 73 ngth. This means
13263 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 61 the database ca
13264 6e 6e 6f 74 20 62 65 20 6f 70 65 6e 65 64 2c 0a nnot be opened,.
13265 20 20 20 20 20 20 2a 2a 20 61 73 20 69 74 20 77 ** as it w
13266 69 6c 6c 20 6e 6f 74 20 62 65 20 70 6f 73 73 69 ill not be possi
13267 62 6c 65 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 ble to open the
13268 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f 72 20 journal file or
13269 65 76 65 6e 0a 20 20 20 20 20 20 2a 2a 20 63 68 even. ** ch
1326a 65 63 6b 20 66 6f 72 20 61 20 68 6f 74 2d 6a 6f eck for a hot-jo
1326b 75 72 6e 61 6c 20 62 65 66 6f 72 65 20 72 65 61 urnal before rea
1326c 64 69 6e 67 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 ding.. */.
1326d 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
1326e 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d _CANTOPEN;. }
1326f 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
13270 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
13271 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 50 61 sqlite3_free(zPa
13272 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 thname);. r
13273 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
13274 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 }.. /* Alloca
13275 74 65 20 6d 65 6d 6f 72 79 20 66 6f 72 20 74 68 te memory for th
13276 65 20 50 61 67 65 72 20 73 74 72 75 63 74 75 72 e Pager structur
13277 65 2c 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74 e, PCache object
13278 2c 20 74 68 65 0a 20 20 2a 2a 20 74 68 72 65 65 , the. ** three
13279 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
1327a 73 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 s, the database
1327b 66 69 6c 65 20 6e 61 6d 65 20 61 6e 64 20 74 68 file name and th
1327c 65 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 e journal . **
1327d 66 69 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20 6c file name. The l
1327e 61 79 6f 75 74 20 69 6e 20 6d 65 6d 6f 72 79 20 ayout in memory
1327f 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 is as follows:.
13280 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 50 61 67 **. ** Pag
13281 65 72 20 6f 62 6a 65 63 74 20 20 20 20 20 20 20 er object
13282 20 20 20 20 20 20 20 20 20 20 20 20 20 28 73 69 (si
13283 7a 65 6f 66 28 50 61 67 65 72 29 20 62 79 74 65 zeof(Pager) byte
13284 73 29 0a 20 20 2a 2a 20 20 20 20 20 50 43 61 63 s). ** PCac
13285 68 65 20 6f 62 6a 65 63 74 20 20 20 20 20 20 20 he object
13286 20 20 20 20 20 20 20 20 20 20 20 20 28 73 71 6c (sql
13287 69 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 29 ite3PcacheSize()
13288 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 bytes). **
13289 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 Database file h
1328a 61 6e 64 6c 65 20 20 20 20 20 20 20 20 20 20 20 andle
1328b 20 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 (pVfs->szOsFile
1328c 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 bytes). **
1328d 20 53 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c Sub-journal fil
1328e 65 20 68 61 6e 64 6c 65 20 20 20 20 20 20 20 20 e handle
1328f 20 28 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a (journalFileSiz
13290 65 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 e bytes). **
13291 20 20 4d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 Main journal f
13292 69 6c 65 20 68 61 6e 64 6c 65 20 20 20 20 20 20 ile handle
13293 20 20 28 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 (journalFileSi
13294 7a 65 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 ze bytes). **
13295 20 20 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 Database file
13296 20 6e 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 name
13297 20 20 20 28 6e 50 61 74 68 6e 61 6d 65 2b 31 20 (nPathname+1
13298 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 bytes). **
13299 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d Journal file nam
1329a 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e
1329b 28 6e 50 61 74 68 6e 61 6d 65 2b 38 2b 31 20 62 (nPathname+8+1 b
1329c 79 74 65 73 29 0a 20 20 2a 2f 0a 20 20 70 50 74 ytes). */. pPt
1329d 72 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65 r = (u8 *)sqlite
1329e 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 0a 20 20 20 3MallocZero(.
1329f 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a ROUND8(sizeof(*
132a0 70 50 61 67 65 72 29 29 20 2b 20 20 20 20 20 20 pPager)) +
132a1 2f 2a 20 50 61 67 65 72 20 73 74 72 75 63 74 75 /* Pager structu
132a2 72 65 20 2a 2f 0a 20 20 20 20 52 4f 55 4e 44 38 re */. ROUND8
132a3 28 70 63 61 63 68 65 53 69 7a 65 29 20 2b 20 20 (pcacheSize) +
132a4 20 20 20 20 20 20 20 20 20 2f 2a 20 50 43 61 63 /* PCac
132a5 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20 he object */.
132a6 20 52 4f 55 4e 44 38 28 70 56 66 73 2d 3e 73 7a ROUND8(pVfs->sz
132a7 4f 73 46 69 6c 65 29 20 2b 20 20 20 20 20 20 20 OsFile) +
132a8 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64 62 20 66 /* The main db f
132a9 69 6c 65 20 2a 2f 0a 20 20 20 20 6a 6f 75 72 6e ile */. journ
132aa 61 6c 46 69 6c 65 53 69 7a 65 20 2a 20 32 20 2b alFileSize * 2 +
132ab 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
132ac 20 74 77 6f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c two journal fil
132ad 65 73 20 2a 2f 20 0a 20 20 20 20 6e 50 61 74 68 es */ . nPath
132ae 6e 61 6d 65 20 2b 20 31 20 2b 20 20 20 20 20 20 name + 1 +
132af 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 46 69 /* zFi
132b0 6c 65 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 6e 50 lename */. nP
132b1 61 74 68 6e 61 6d 65 20 2b 20 38 20 2b 20 31 20 athname + 8 + 1
132b2 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
132b3 7a 4a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 29 3b zJournal */. );
132b4 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54 . assert( EIGHT
132b5 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 _BYTE_ALIGNMENT(
132b6 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 SQLITE_INT_TO_PT
132b7 52 28 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a R(journalFileSiz
132b8 65 29 29 20 29 3b 0a 20 20 69 66 28 20 21 70 50 e)) );. if( !pP
132b9 74 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 tr ){. sqlite
132ba 33 5f 66 72 65 65 28 7a 50 61 74 68 6e 61 6d 65 3_free(zPathname
132bb 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 );. return SQ
132bc 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a LITE_NOMEM;. }.
132bd 20 20 70 50 61 67 65 72 20 3d 20 20 20 20 20 20 pPager =
132be 20 20 20 20 20 20 20 20 28 50 61 67 65 72 2a 29 (Pager*)
132bf 28 70 50 74 72 29 3b 0a 20 20 70 50 61 67 65 72 (pPtr);. pPager
132c0 2d 3e 70 50 43 61 63 68 65 20 3d 20 20 20 20 28 ->pPCache = (
132c1 50 43 61 63 68 65 2a 29 28 70 50 74 72 20 2b 3d PCache*)(pPtr +=
132c2 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a ROUND8(sizeof(*
132c3 70 50 61 67 65 72 29 29 29 3b 0a 20 20 70 50 61 pPager)));. pPa
132c4 67 65 72 2d 3e 66 64 20 3d 20 20 20 28 73 71 6c ger->fd = (sql
132c5 69 74 65 33 5f 66 69 6c 65 2a 29 28 70 50 74 72 ite3_file*)(pPtr
132c6 20 2b 3d 20 52 4f 55 4e 44 38 28 70 63 61 63 68 += ROUND8(pcach
132c7 65 53 69 7a 65 29 29 3b 0a 20 20 70 50 61 67 65 eSize));. pPage
132c8 72 2d 3e 73 6a 66 64 20 3d 20 28 73 71 6c 69 74 r->sjfd = (sqlit
132c9 65 33 5f 66 69 6c 65 2a 29 28 70 50 74 72 20 2b e3_file*)(pPtr +
132ca 3d 20 52 4f 55 4e 44 38 28 70 56 66 73 2d 3e 73 = ROUND8(pVfs->s
132cb 7a 4f 73 46 69 6c 65 29 29 3b 0a 20 20 70 50 61 zOsFile));. pPa
132cc 67 65 72 2d 3e 6a 66 64 20 3d 20 20 28 73 71 6c ger->jfd = (sql
132cd 69 74 65 33 5f 66 69 6c 65 2a 29 28 70 50 74 72 ite3_file*)(pPtr
132ce 20 2b 3d 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 += journalFileS
132cf 69 7a 65 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e ize);. pPager->
132d0 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 20 20 20 28 zFilename = (
132d1 63 68 61 72 2a 29 28 70 50 74 72 20 2b 3d 20 6a char*)(pPtr += j
132d2 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 29 3b ournalFileSize);
132d3 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54 . assert( EIGHT
132d4 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 _BYTE_ALIGNMENT(
132d5 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a pPager->jfd) );.
132d6 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 . /* Fill in th
132d7 65 20 50 61 67 65 72 2e 7a 46 69 6c 65 6e 61 6d e Pager.zFilenam
132d8 65 20 61 6e 64 20 50 61 67 65 72 2e 7a 4a 6f 75 e and Pager.zJou
132d9 72 6e 61 6c 20 62 75 66 66 65 72 73 2c 20 69 66 rnal buffers, if
132da 20 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 required. */.
132db 69 66 28 20 7a 50 61 74 68 6e 61 6d 65 20 29 7b if( zPathname ){
132dc 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 7a 4a 6f . pPager->zJo
132dd 75 72 6e 61 6c 20 3d 20 20 20 28 63 68 61 72 2a urnal = (char*
132de 29 28 70 50 74 72 20 2b 3d 20 6e 50 61 74 68 6e )(pPtr += nPathn
132df 61 6d 65 20 2b 20 31 29 3b 0a 20 20 20 20 6d 65 ame + 1);. me
132e0 6d 63 70 79 28 70 50 61 67 65 72 2d 3e 7a 46 69 mcpy(pPager->zFi
132e1 6c 65 6e 61 6d 65 2c 20 7a 50 61 74 68 6e 61 6d lename, zPathnam
132e2 65 2c 20 6e 50 61 74 68 6e 61 6d 65 29 3b 0a 20 e, nPathname);.
132e3 20 20 20 6d 65 6d 63 70 79 28 70 50 61 67 65 72 memcpy(pPager
132e4 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 7a 50 61 74 ->zJournal, zPat
132e5 68 6e 61 6d 65 2c 20 6e 50 61 74 68 6e 61 6d 65 hname, nPathname
132e6 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 );. memcpy(&p
132e7 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 5b Pager->zJournal[
132e8 6e 50 61 74 68 6e 61 6d 65 5d 2c 20 22 2d 6a 6f nPathname], "-jo
132e9 75 72 6e 61 6c 22 2c 20 38 29 3b 0a 20 20 20 20 urnal", 8);.
132ea 69 66 28 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c if( pPager->zFil
132eb 65 6e 61 6d 65 5b 30 5d 3d 3d 30 20 29 20 70 50 ename[0]==0 ) pP
132ec 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 5b 30 ager->zJournal[0
132ed 5d 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 ] = 0;. sqlit
132ee 65 33 5f 66 72 65 65 28 7a 50 61 74 68 6e 61 6d e3_free(zPathnam
132ef 65 29 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 e);. }. pPager
132f0 2d 3e 70 56 66 73 20 3d 20 70 56 66 73 3b 0a 20 ->pVfs = pVfs;.
132f1 20 70 50 61 67 65 72 2d 3e 76 66 73 46 6c 61 67 pPager->vfsFlag
132f2 73 20 3d 20 76 66 73 46 6c 61 67 73 3b 0a 0a 20 s = vfsFlags;..
132f3 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 70 61 67 /* Open the pag
132f4 65 72 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 er file.. */.
132f5 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 if( zFilename &&
132f6 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 20 26 26 zFilename[0] &&
132f7 20 21 6d 65 6d 44 62 20 29 7b 0a 20 20 20 20 69 !memDb ){. i
132f8 6e 74 20 66 6f 75 74 20 3d 20 30 3b 20 20 20 20 nt fout = 0;
132f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
132fa 2f 2a 20 56 46 53 20 66 6c 61 67 73 20 72 65 74 /* VFS flags ret
132fb 75 72 6e 65 64 20 62 79 20 78 4f 70 65 6e 28 29 urned by xOpen()
132fc 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c */. rc = sql
132fd 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c ite3OsOpen(pVfs,
132fe 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 pPager->zFilena
132ff 6d 65 2c 20 70 50 61 67 65 72 2d 3e 66 64 2c 20 me, pPager->fd,
13300 76 66 73 46 6c 61 67 73 2c 20 26 66 6f 75 74 29 vfsFlags, &fout)
13301 3b 0a 20 20 20 20 72 65 61 64 4f 6e 6c 79 20 3d ;. readOnly =
13302 20 28 66 6f 75 74 26 53 51 4c 49 54 45 5f 4f 50 (fout&SQLITE_OP
13303 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a 0a 20 EN_READONLY);..
13304 20 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c /* If the fil
13305 65 20 77 61 73 20 73 75 63 63 65 73 73 66 75 6c e was successful
13306 6c 79 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 ly opened for re
13307 61 64 2f 77 72 69 74 65 20 61 63 63 65 73 73 2c ad/write access,
13308 0a 20 20 20 20 2a 2a 20 63 68 6f 6f 73 65 20 61 . ** choose a
13309 20 64 65 66 61 75 6c 74 20 70 61 67 65 20 73 69 default page si
1330a 7a 65 20 69 6e 20 63 61 73 65 20 77 65 20 68 61 ze in case we ha
1330b 76 65 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 ve to create the
1330c 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 . ** database
1330d 20 66 69 6c 65 2e 20 54 68 65 20 64 65 66 61 75 file. The defau
1330e 6c 74 20 70 61 67 65 20 73 69 7a 65 20 69 73 20 lt page size is
1330f 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66 3a 0a the maximum of:.
13310 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 **. **
13311 20 2b 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c + SQLITE_DEFAUL
13312 54 5f 50 41 47 45 5f 53 49 5a 45 2c 0a 20 20 20 T_PAGE_SIZE,.
13313 20 2a 2a 20 20 20 20 2b 20 54 68 65 20 76 61 6c ** + The val
13314 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 ue returned by s
13315 71 6c 69 74 65 33 4f 73 53 65 63 74 6f 72 53 69 qlite3OsSectorSi
13316 7a 65 28 29 0a 20 20 20 20 2a 2a 20 20 20 20 2b ze(). ** +
13317 20 54 68 65 20 6c 61 72 67 65 73 74 20 70 61 67 The largest pag
13318 65 20 73 69 7a 65 20 74 68 61 74 20 63 61 6e 20 e size that can
13319 62 65 20 77 72 69 74 74 65 6e 20 61 74 6f 6d 69 be written atomi
1331a 63 61 6c 6c 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 cally.. */.
1331b 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1331c 5f 4f 4b 20 26 26 20 21 72 65 61 64 4f 6e 6c 79 _OK && !readOnly
1331d 20 29 7b 0a 20 20 20 20 20 20 73 65 74 53 65 63 ){. setSec
1331e 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72 29 3b torSize(pPager);
1331f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 53 51 . assert(SQ
13320 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 LITE_DEFAULT_PAG
13321 45 5f 53 49 5a 45 3c 3d 53 51 4c 49 54 45 5f 4d E_SIZE<=SQLITE_M
13322 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f AX_DEFAULT_PAGE_
13323 53 49 5a 45 29 3b 0a 20 20 20 20 20 20 69 66 28 SIZE);. if(
13324 20 73 7a 50 61 67 65 44 66 6c 74 3c 70 50 61 67 szPageDflt<pPag
13325 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 29 er->sectorSize )
13326 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 {. if( pP
13327 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 ager->sectorSize
13328 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 >SQLITE_MAX_DEFA
13329 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 20 29 7b ULT_PAGE_SIZE ){
1332a 0a 20 20 20 20 20 20 20 20 20 20 73 7a 50 61 67 . szPag
1332b 65 44 66 6c 74 20 3d 20 53 51 4c 49 54 45 5f 4d eDflt = SQLITE_M
1332c 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f AX_DEFAULT_PAGE_
1332d 53 49 5a 45 3b 0a 20 20 20 20 20 20 20 20 7d 65 SIZE;. }e
1332e 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 73 lse{. s
1332f 7a 50 61 67 65 44 66 6c 74 20 3d 20 28 75 31 36 zPageDflt = (u16
13330 29 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 )pPager->sectorS
13331 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 ize;. }.
13332 20 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 }.#ifdef SQ
13333 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d LITE_ENABLE_ATOM
13334 49 43 5f 57 52 49 54 45 0a 20 20 20 20 20 20 7b IC_WRITE. {
13335 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 44 63 . int iDc
13336 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 76 69 = sqlite3OsDevi
13337 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 ceCharacteristic
13338 73 28 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 s(pPager->fd);.
13339 20 20 20 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 int ii;.
1333a 20 20 20 20 20 20 20 61 73 73 65 72 74 28 53 51 assert(SQ
1333b 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 LITE_IOCAP_ATOMI
1333c 43 35 31 32 3d 3d 28 35 31 32 3e 3e 38 29 29 3b C512==(512>>8));
1333d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
1333e 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f SQLITE_IOCAP_ATO
1333f 4d 49 43 36 34 4b 3d 3d 28 36 35 35 33 36 3e 3e MIC64K==(65536>>
13340 38 29 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 8));. ass
13341 65 72 74 28 53 51 4c 49 54 45 5f 4d 41 58 5f 44 ert(SQLITE_MAX_D
13342 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 EFAULT_PAGE_SIZE
13343 3c 3d 36 35 35 33 36 29 3b 0a 20 20 20 20 20 20 <=65536);.
13344 20 20 66 6f 72 28 69 69 3d 73 7a 50 61 67 65 44 for(ii=szPageD
13345 66 6c 74 3b 20 69 69 3c 3d 53 51 4c 49 54 45 5f flt; ii<=SQLITE_
13346 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 MAX_DEFAULT_PAGE
13347 5f 53 49 5a 45 3b 20 69 69 3d 69 69 2a 32 29 7b _SIZE; ii=ii*2){
13348 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69 . if( i
13349 44 63 26 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 Dc&(SQLITE_IOCAP
1334a 5f 41 54 4f 4d 49 43 7c 28 69 69 3e 3e 38 29 29 _ATOMIC|(ii>>8))
1334b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1334c 73 7a 50 61 67 65 44 66 6c 74 20 3d 20 69 69 3b szPageDflt = ii;
1334d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
1334e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23 }. }.#
1334f 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 7d 65 endif. }. }e
13350 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 61 lse{. /* If a
13351 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 temporary file
13352 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 69 74 is requested, it
13353 20 69 73 20 6e 6f 74 20 6f 70 65 6e 65 64 20 69 is not opened i
13354 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 20 20 20 20 mmediately..
13355 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 ** In this case
13356 77 65 20 61 63 63 65 70 74 20 74 68 65 20 64 65 we accept the de
13357 66 61 75 6c 74 20 70 61 67 65 20 73 69 7a 65 20 fault page size
13358 61 6e 64 20 64 65 6c 61 79 20 61 63 74 75 61 6c and delay actual
13359 6c 79 0a 20 20 20 20 2a 2a 20 6f 70 65 6e 69 6e ly. ** openin
1335a 67 20 74 68 65 20 66 69 6c 65 20 75 6e 74 69 6c g the file until
1335b 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20 the first call
1335c 74 6f 20 4f 73 57 72 69 74 65 28 29 2e 0a 20 20 to OsWrite()..
1335d 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 69 73 **. ** This
1335e 20 62 72 61 6e 63 68 20 69 73 20 61 6c 73 6f 20 branch is also
1335f 72 75 6e 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 run for an in-me
13360 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2e 20 41 mory database. A
13361 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 20 20 20 20 n in-memory.
13362 2a 2a 20 64 61 74 61 62 61 73 65 20 69 73 20 74 ** database is t
13363 68 65 20 73 61 6d 65 20 61 73 20 61 20 74 65 6d he same as a tem
13364 70 2d 66 69 6c 65 20 74 68 61 74 20 69 73 20 6e p-file that is n
13365 65 76 65 72 20 77 72 69 74 74 65 6e 20 6f 75 74 ever written out
13366 20 74 6f 0a 20 20 20 20 2a 2a 20 64 69 73 6b 20 to. ** disk
13367 61 6e 64 20 75 73 65 73 20 61 6e 20 69 6e 2d 6d and uses an in-m
13368 65 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 6b 20 6a emory rollback j
13369 6f 75 72 6e 61 6c 2e 0a 20 20 20 20 2a 2f 20 0a ournal.. */ .
1336a 20 20 20 20 74 65 6d 70 46 69 6c 65 20 3d 20 31 tempFile = 1
1336b 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 ;. pPager->st
1336c 61 74 65 20 3d 20 50 41 47 45 52 5f 45 58 43 4c ate = PAGER_EXCL
1336d 55 53 49 56 45 3b 0a 20 20 20 20 72 65 61 64 4f USIVE;. readO
1336e 6e 6c 79 20 3d 20 28 76 66 73 46 6c 61 67 73 26 nly = (vfsFlags&
1336f 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
13370 4f 4e 4c 59 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a ONLY);. }.. /*
13371 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 The following c
13372 61 6c 6c 20 74 6f 20 50 61 67 65 72 53 65 74 50 all to PagerSetP
13373 61 67 65 73 69 7a 65 28 29 20 73 65 72 76 65 73 agesize() serves
13374 20 74 6f 20 73 65 74 20 74 68 65 20 76 61 6c 75 to set the valu
13375 65 20 6f 66 20 0a 20 20 2a 2a 20 50 61 67 65 72 e of . ** Pager
13376 2e 70 61 67 65 53 69 7a 65 20 61 6e 64 20 74 6f .pageSize and to
13377 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 50 61 allocate the Pa
13378 67 65 72 2e 70 54 6d 70 53 70 61 63 65 20 62 75 ger.pTmpSpace bu
13379 66 66 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 ffer.. */. if(
1337a 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1337b 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 {. assert( pP
1337c 61 67 65 72 2d 3e 6d 65 6d 44 62 3d 3d 30 20 29 ager->memDb==0 )
1337d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
1337e 65 33 50 61 67 65 72 53 65 74 50 61 67 65 73 69 e3PagerSetPagesi
1337f 7a 65 28 70 50 61 67 65 72 2c 20 26 73 7a 50 61 ze(pPager, &szPa
13380 67 65 44 66 6c 74 2c 20 2d 31 29 3b 0a 20 20 20 geDflt, -1);.
13381 20 74 65 73 74 63 61 73 65 28 20 72 63 21 3d 53 testcase( rc!=S
13382 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a QLITE_OK );. }.
13383 0a 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 72 6f . /* If an erro
13384 72 20 6f 63 63 75 72 72 65 64 20 69 6e 20 65 69 r occurred in ei
13385 74 68 65 72 20 6f 66 20 74 68 65 20 62 6c 6f 63 ther of the bloc
13386 6b 73 20 61 62 6f 76 65 2c 20 66 72 65 65 20 74 ks above, free t
13387 68 65 20 0a 20 20 2a 2a 20 50 61 67 65 72 20 73 he . ** Pager s
13388 74 72 75 63 74 75 72 65 20 61 6e 64 20 63 6c 6f tructure and clo
13389 73 65 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 2a se the file.. *
1338a 2f 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 /. if( rc!=SQLI
1338b 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 TE_OK ){. ass
1338c 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e 70 54 ert( !pPager->pT
1338d 6d 70 53 70 61 63 65 20 29 3b 0a 20 20 20 20 73 mpSpace );. s
1338e 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 qlite3OsClose(pP
1338f 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20 73 ager->fd);. s
13390 71 6c 69 74 65 33 5f 66 72 65 65 28 70 50 61 67 qlite3_free(pPag
13391 65 72 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 er);. return
13392 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e rc;. }.. /* In
13393 69 74 69 61 6c 69 7a 65 20 74 68 65 20 50 43 61 itialize the PCa
13394 63 68 65 20 6f 62 6a 65 63 74 2e 20 2a 2f 0a 20 che object. */.
13395 20 61 73 73 65 72 74 28 20 6e 45 78 74 72 61 3c assert( nExtra<
13396 31 30 30 30 20 29 3b 0a 20 20 6e 45 78 74 72 61 1000 );. nExtra
13397 20 3d 20 52 4f 55 4e 44 38 28 6e 45 78 74 72 61 = ROUND8(nExtra
13398 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 );. sqlite3Pcac
13399 68 65 4f 70 65 6e 28 73 7a 50 61 67 65 44 66 6c heOpen(szPageDfl
1339a 74 2c 20 6e 45 78 74 72 61 2c 20 21 6d 65 6d 44 t, nExtra, !memD
1339b 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 b,.
1339c 20 20 20 20 20 20 20 21 6d 65 6d 44 62 3f 70 61 !memDb?pa
1339d 67 65 72 53 74 72 65 73 73 3a 30 2c 20 28 76 6f gerStress:0, (vo
1339e 69 64 20 2a 29 70 50 61 67 65 72 2c 20 70 50 61 id *)pPager, pPa
1339f 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 0a ger->pPCache);..
133a0 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 4f PAGERTRACE(("O
133a1 50 45 4e 20 25 64 20 25 73 5c 6e 22 2c 20 46 49 PEN %d %s\n", FI
133a2 4c 45 48 41 4e 44 4c 45 49 44 28 70 50 61 67 65 LEHANDLEID(pPage
133a3 72 2d 3e 66 64 29 2c 20 70 50 61 67 65 72 2d 3e r->fd), pPager->
133a4 7a 46 69 6c 65 6e 61 6d 65 29 29 3b 0a 20 20 49 zFilename));. I
133a5 4f 54 52 41 43 45 28 28 22 4f 50 45 4e 20 25 70 OTRACE(("OPEN %p
133a6 20 25 73 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 %s\n", pPager,
133a7 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d pPager->zFilenam
133a8 65 29 29 0a 0a 20 20 70 50 61 67 65 72 2d 3e 75 e)).. pPager->u
133a9 73 65 4a 6f 75 72 6e 61 6c 20 3d 20 28 75 38 29 seJournal = (u8)
133aa 75 73 65 4a 6f 75 72 6e 61 6c 3b 0a 20 20 70 50 useJournal;. pP
133ab 61 67 65 72 2d 3e 6e 6f 52 65 61 64 6c 6f 63 6b ager->noReadlock
133ac 20 3d 20 28 6e 6f 52 65 61 64 6c 6f 63 6b 20 26 = (noReadlock &
133ad 26 20 72 65 61 64 4f 6e 6c 79 29 20 3f 31 3a 30 & readOnly) ?1:0
133ae 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 ;. /* pPager->s
133af 74 6d 74 4f 70 65 6e 20 3d 20 30 3b 20 2a 2f 0a tmtOpen = 0; */.
133b0 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 6d /* pPager->stm
133b1 74 49 6e 55 73 65 20 3d 20 30 3b 20 2a 2f 0a 20 tInUse = 0; */.
133b2 20 2f 2a 20 70 50 61 67 65 72 2d 3e 6e 52 65 66 /* pPager->nRef
133b3 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 65 = 0; */. pPage
133b4 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 3d r->dbSizeValid =
133b5 20 28 75 38 29 6d 65 6d 44 62 3b 0a 20 20 2f 2a (u8)memDb;. /*
133b6 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 53 69 7a pPager->stmtSiz
133b7 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 e = 0; */. /* p
133b8 50 61 67 65 72 2d 3e 73 74 6d 74 4a 53 69 7a 65 Pager->stmtJSize
133b9 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 = 0; */. /* pP
133ba 61 67 65 72 2d 3e 6e 50 61 67 65 20 3d 20 30 3b ager->nPage = 0;
133bb 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 6d 78 */. pPager->mx
133bc 50 67 6e 6f 20 3d 20 53 51 4c 49 54 45 5f 4d 41 Pgno = SQLITE_MA
133bd 58 5f 50 41 47 45 5f 43 4f 55 4e 54 3b 0a 20 20 X_PAGE_COUNT;.
133be 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 /* pPager->state
133bf 20 3d 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 3b = PAGER_UNLOCK;
133c0 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 */. assert( pP
133c1 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 3d 20 28 ager->state == (
133c2 74 65 6d 70 46 69 6c 65 20 3f 20 50 41 47 45 52 tempFile ? PAGER
133c3 5f 45 58 43 4c 55 53 49 56 45 20 3a 20 50 41 47 _EXCLUSIVE : PAG
133c4 45 52 5f 55 4e 4c 4f 43 4b 29 20 29 3b 0a 20 20 ER_UNLOCK) );.
133c5 2f 2a 20 70 50 61 67 65 72 2d 3e 65 72 72 4d 61 /* pPager->errMa
133c6 73 6b 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 sk = 0; */. pPa
133c7 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 3d 20 ger->tempFile =
133c8 28 75 38 29 74 65 6d 70 46 69 6c 65 3b 0a 20 20 (u8)tempFile;.
133c9 61 73 73 65 72 74 28 20 74 65 6d 70 46 69 6c 65 assert( tempFile
133ca 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d ==PAGER_LOCKINGM
133cb 4f 44 45 5f 4e 4f 52 4d 41 4c 20 0a 20 20 20 20 ODE_NORMAL .
133cc 20 20 20 20 20 20 7c 7c 20 74 65 6d 70 46 69 6c || tempFil
133cd 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 e==PAGER_LOCKING
133ce 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 20 29 MODE_EXCLUSIVE )
133cf 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 45 ;. assert( PAGE
133d0 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 R_LOCKINGMODE_EX
133d1 43 4c 55 53 49 56 45 3d 3d 31 20 29 3b 0a 20 20 CLUSIVE==1 );.
133d2 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 pPager->exclusiv
133d3 65 4d 6f 64 65 20 3d 20 28 75 38 29 74 65 6d 70 eMode = (u8)temp
133d4 46 69 6c 65 3b 20 0a 20 20 70 50 61 67 65 72 2d File; . pPager-
133d5 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 >changeCountDone
133d6 20 3d 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 = pPager->tempF
133d7 69 6c 65 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6d ile;. pPager->m
133d8 65 6d 44 62 20 3d 20 28 75 38 29 6d 65 6d 44 62 emDb = (u8)memDb
133d9 3b 0a 20 20 70 50 61 67 65 72 2d 3e 72 65 61 64 ;. pPager->read
133da 4f 6e 6c 79 20 3d 20 28 75 38 29 72 65 61 64 4f Only = (u8)readO
133db 6e 6c 79 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 nly;. /* pPager
133dc 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 20 ->needSync = 0;
133dd 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 75 73 65 */. assert( use
133de 4a 6f 75 72 6e 61 6c 20 7c 7c 20 70 50 61 67 65 Journal || pPage
133df 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 r->tempFile );.
133e0 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 pPager->noSync
133e1 3d 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 = pPager->tempFi
133e2 6c 65 3b 0a 20 20 70 50 61 67 65 72 2d 3e 66 75 le;. pPager->fu
133e3 6c 6c 53 79 6e 63 20 3d 20 70 50 61 67 65 72 2d llSync = pPager-
133e4 3e 6e 6f 53 79 6e 63 20 3f 30 3a 31 3b 0a 20 20 >noSync ?0:1;.
133e5 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 pPager->sync_fla
133e6 67 73 20 3d 20 53 51 4c 49 54 45 5f 53 59 4e 43 gs = SQLITE_SYNC
133e7 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 2f 2a 20 70 50 _NORMAL;. /* pP
133e8 61 67 65 72 2d 3e 70 46 69 72 73 74 20 3d 20 30 ager->pFirst = 0
133e9 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 ; */. /* pPager
133ea 2d 3e 70 46 69 72 73 74 53 79 6e 63 65 64 20 3d ->pFirstSynced =
133eb 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 0; */. /* pPag
133ec 65 72 2d 3e 70 4c 61 73 74 20 3d 20 30 3b 20 2a er->pLast = 0; *
133ed 2f 0a 20 20 70 50 61 67 65 72 2d 3e 6e 45 78 74 /. pPager->nExt
133ee 72 61 20 3d 20 28 75 31 36 29 6e 45 78 74 72 61 ra = (u16)nExtra
133ef 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 ;. pPager->jour
133f0 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 3d 20 53 nalSizeLimit = S
133f1 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4a 4f QLITE_DEFAULT_JO
133f2 55 52 4e 41 4c 5f 53 49 5a 45 5f 4c 49 4d 49 54 URNAL_SIZE_LIMIT
133f3 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 ;. assert( isOp
133f4 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 7c en(pPager->fd) |
133f5 7c 20 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20 | tempFile );.
133f6 73 65 74 53 65 63 74 6f 72 53 69 7a 65 28 70 50 setSectorSize(pP
133f7 61 67 65 72 29 3b 0a 20 20 69 66 28 20 21 75 73 ager);. if( !us
133f8 65 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 eJournal ){.
133f9 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
133fa 6f 64 65 20 3d 20 50 41 47 45 52 5f 4a 4f 55 52 ode = PAGER_JOUR
133fb 4e 41 4c 4d 4f 44 45 5f 4f 46 46 3b 0a 20 20 7d NALMODE_OFF;. }
133fc 65 6c 73 65 20 69 66 28 20 6d 65 6d 44 62 20 29 else if( memDb )
133fd 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f {. pPager->jo
133fe 75 72 6e 61 6c 4d 6f 64 65 20 3d 20 50 41 47 45 urnalMode = PAGE
133ff 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 R_JOURNALMODE_ME
13400 4d 4f 52 59 3b 0a 20 20 7d 0a 20 20 2f 2a 20 70 MORY;. }. /* p
13401 50 61 67 65 72 2d 3e 78 42 75 73 79 48 61 6e 64 Pager->xBusyHand
13402 6c 65 72 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a ler = 0; */. /*
13403 20 70 50 61 67 65 72 2d 3e 70 42 75 73 79 48 61 pPager->pBusyHa
13404 6e 64 6c 65 72 41 72 67 20 3d 20 30 3b 20 2a 2f ndlerArg = 0; */
13405 0a 20 20 70 50 61 67 65 72 2d 3e 78 52 65 69 6e . pPager->xRein
13406 69 74 65 72 20 3d 20 78 52 65 69 6e 69 74 3b 0a iter = xReinit;.
13407 20 20 2f 2a 20 6d 65 6d 73 65 74 28 70 50 61 67 /* memset(pPag
13408 65 72 2d 3e 61 48 61 73 68 2c 20 30 2c 20 73 69 er->aHash, 0, si
13409 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 61 48 61 zeof(pPager->aHa
1340a 73 68 29 29 3b 20 2a 2f 0a 20 20 2a 70 70 50 61 sh)); */. *ppPa
1340b 67 65 72 20 3d 20 70 50 61 67 65 72 3b 0a 20 20 ger = pPager;.
1340c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
1340d 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 ;.}..../*.** Thi
1340e 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
1340f 6c 6c 65 64 20 61 66 74 65 72 20 74 72 61 6e 73 lled after trans
13410 69 74 69 6f 6e 69 6e 67 20 66 72 6f 6d 20 50 41 itioning from PA
13411 47 45 52 5f 55 4e 4c 4f 43 4b 20 74 6f 0a 2a 2a GER_UNLOCK to.**
13412 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 73 74 PAGER_SHARED st
13413 61 74 65 2e 20 49 74 20 74 65 73 74 73 20 69 66 ate. It tests if
13414 20 74 68 65 72 65 20 69 73 20 61 20 68 6f 74 20 there is a hot
13415 6a 6f 75 72 6e 61 6c 20 70 72 65 73 65 6e 74 20 journal present
13416 69 6e 0a 2a 2a 20 74 68 65 20 66 69 6c 65 2d 73 in.** the file-s
13417 79 73 74 65 6d 20 66 6f 72 20 74 68 65 20 67 69 ystem for the gi
13418 76 65 6e 20 70 61 67 65 72 2e 20 41 20 68 6f 74 ven pager. A hot
13419 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 6e 65 20 journal is one
1341a 74 68 61 74 20 0a 2a 2a 20 6e 65 65 64 73 20 74 that .** needs t
1341b 6f 20 62 65 20 70 6c 61 79 65 64 20 62 61 63 6b o be played back
1341c 2e 20 41 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 . According to t
1341d 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61 20 his function, a
1341e 68 6f 74 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 hot-journal.** f
1341f 69 6c 65 20 65 78 69 73 74 73 20 69 66 20 74 68 ile exists if th
13420 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 72 69 74 e following crit
13421 65 72 69 61 20 61 72 65 20 6d 65 74 3a 0a 2a 2a eria are met:.**
13422 0a 2a 2a 20 20 20 2a 20 54 68 65 20 6a 6f 75 72 .** * The jour
13423 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74 73 20 nal file exists
13424 69 6e 20 74 68 65 20 66 69 6c 65 20 73 79 73 74 in the file syst
13425 65 6d 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 4e em, and.** * N
13426 6f 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 o process holds
13427 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 67 72 a RESERVED or gr
13428 65 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 68 eater lock on th
13429 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c e database file,
1342a 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 54 68 65 20 and.** * The
1342b 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 74 database file it
1342c 73 65 6c 66 20 69 73 20 67 72 65 61 74 65 72 20 self is greater
1342d 74 68 61 6e 20 30 20 62 79 74 65 73 20 69 6e 20 than 0 bytes in
1342e 73 69 7a 65 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a size, and.** *
1342f 20 54 68 65 20 66 69 72 73 74 20 62 79 74 65 20 The first byte
13430 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 of the journal f
13431 69 6c 65 20 65 78 69 73 74 73 20 61 6e 64 20 69 ile exists and i
13432 73 20 6e 6f 74 20 30 78 30 30 2e 0a 2a 2a 0a 2a s not 0x00..**.*
13433 2a 20 49 66 20 74 68 65 20 63 75 72 72 65 6e 74 * If the current
13434 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 size of the dat
13435 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 30 20 abase file is 0
13436 62 75 74 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 but a journal fi
13437 6c 65 0a 2a 2a 20 65 78 69 73 74 73 2c 20 74 68 le.** exists, th
13438 61 74 20 69 73 20 70 72 6f 62 61 62 6c 79 20 61 at is probably a
13439 6e 20 6f 6c 64 20 6a 6f 75 72 6e 61 6c 20 6c 65 n old journal le
1343a 66 74 20 6f 76 65 72 20 66 72 6f 6d 20 61 20 70 ft over from a p
1343b 72 69 6f 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 rior.** database
1343c 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 6e with the same n
1343d 61 6d 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 ame. In this cas
1343e 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 e the journal fi
1343f 6c 65 20 69 73 0a 2a 2a 20 6a 75 73 74 20 64 65 le is.** just de
13440 6c 65 74 65 64 20 75 73 69 6e 67 20 4f 73 44 65 leted using OsDe
13441 6c 65 74 65 2c 20 2a 70 45 78 69 73 74 73 20 69 lete, *pExists i
13442 73 20 73 65 74 20 74 6f 20 30 20 61 6e 64 20 53 s set to 0 and S
13443 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 69 73 20 72 QLITE_OK.** is r
13444 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 eturned..**.** T
13445 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 his routine does
13446 20 6e 6f 74 20 63 68 65 63 6b 20 69 66 20 74 68 not check if th
13447 65 72 65 20 69 73 20 61 20 6d 61 73 74 65 72 20 ere is a master
13448 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 journal filename
13449 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20 6f .** at the end o
1344a 66 20 74 68 65 20 66 69 6c 65 2e 20 49 66 20 74 f the file. If t
1344b 68 65 72 65 20 69 73 2c 20 61 6e 64 20 74 68 61 here is, and tha
1344c 74 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c t master journal
1344d 20 66 69 6c 65 0a 2a 2a 20 64 6f 65 73 20 6e 6f file.** does no
1344e 74 20 65 78 69 73 74 2c 20 74 68 65 6e 20 74 68 t exist, then th
1344f 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 e journal file i
13450 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 68 6f 74 s not really hot
13451 2e 20 49 6e 20 74 68 69 73 0a 2a 2a 20 63 61 73 . In this.** cas
13452 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 e this routine w
13453 69 6c 6c 20 72 65 74 75 72 6e 20 61 20 66 61 6c ill return a fal
13454 73 65 2d 70 6f 73 69 74 69 76 65 2e 20 54 68 65 se-positive. The
13455 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 pager_playback(
13456 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 77 69 6c ).** routine wil
13457 6c 20 64 69 73 63 6f 76 65 72 20 74 68 61 74 20 l discover that
13458 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
13459 20 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 68 is not really h
1345a 6f 74 20 61 6e 64 20 0a 2a 2a 20 77 69 6c 6c 20 ot and .** will
1345b 6e 6f 74 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b not roll it back
1345c 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 68 6f . .**.** If a ho
1345d 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 t-journal file i
1345e 73 20 66 6f 75 6e 64 20 74 6f 20 65 78 69 73 74 s found to exist
1345f 2c 20 2a 70 45 78 69 73 74 73 20 69 73 20 73 65 , *pExists is se
13460 74 20 74 6f 20 31 20 61 6e 64 20 0a 2a 2a 20 53 t to 1 and .** S
13461 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 QLITE_OK returne
13462 64 2e 20 49 66 20 6e 6f 20 68 6f 74 2d 6a 6f 75 d. If no hot-jou
13463 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 70 72 65 rnal file is pre
13464 73 65 6e 74 2c 20 2a 70 45 78 69 73 74 73 20 69 sent, *pExists i
13465 73 0a 2a 2a 20 73 65 74 20 74 6f 20 30 20 61 6e s.** set to 0 an
13466 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 d SQLITE_OK retu
13467 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 65 rned. If an IO e
13468 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c rror occurs whil
13469 65 20 74 72 79 69 6e 67 0a 2a 2a 20 74 6f 20 64 e trying.** to d
1346a 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 etermine whether
1346b 20 6f 72 20 6e 6f 74 20 61 20 68 6f 74 2d 6a 6f or not a hot-jo
1346c 75 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74 urnal file exist
1346d 73 2c 20 74 68 65 20 49 4f 20 65 72 72 6f 72 0a s, the IO error.
1346e 2a 2a 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 ** code is retur
1346f 6e 65 64 20 61 6e 64 20 74 68 65 20 76 61 6c 75 ned and the valu
13470 65 20 6f 66 20 2a 70 45 78 69 73 74 73 20 69 73 e of *pExists is
13471 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 undefined..*/.s
13472 74 61 74 69 63 20 69 6e 74 20 68 61 73 48 6f 74 tatic int hasHot
13473 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 Journal(Pager *p
13474 50 61 67 65 72 2c 20 69 6e 74 20 2a 70 45 78 69 Pager, int *pExi
13475 73 74 73 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f sts){. sqlite3_
13476 76 66 73 20 2a 20 63 6f 6e 73 74 20 70 56 66 73 vfs * const pVfs
13477 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b = pPager->pVfs;
13478 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 . int rc;
13479 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1347a 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 /* Return code
1347b 2a 2f 0a 20 20 69 6e 74 20 65 78 69 73 74 73 3b */. int exists;
1347c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1347d 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 20 /* True if a
1347e 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 journal file is
1347f 70 72 65 73 65 6e 74 20 2a 2f 0a 0a 20 20 61 73 present */.. as
13480 73 65 72 74 28 20 70 50 61 67 65 72 21 3d 30 20 sert( pPager!=0
13481 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 );. assert( pPa
13482 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 ger->useJournal
13483 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f );. assert( isO
13484 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 pen(pPager->fd)
13485 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 69 73 );. assert( !is
13486 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 Open(pPager->jfd
13487 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
13488 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3c 3d 20 Pager->state <=
13489 50 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b 0a PAGER_SHARED );.
1348a 0a 20 20 2a 70 45 78 69 73 74 73 20 3d 20 30 3b . *pExists = 0;
1348b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f . rc = sqlite3O
1348c 73 41 63 63 65 73 73 28 70 56 66 73 2c 20 70 50 sAccess(pVfs, pP
1348d 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 ager->zJournal,
1348e 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 SQLITE_ACCESS_EX
1348f 49 53 54 53 2c 20 26 65 78 69 73 74 73 29 3b 0a ISTS, &exists);.
13490 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
13491 5f 4f 4b 20 26 26 20 65 78 69 73 74 73 20 29 7b _OK && exists ){
13492 0a 20 20 20 20 69 6e 74 20 6c 6f 63 6b 65 64 3b . int locked;
13493 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13494 20 2f 2a 20 54 72 75 65 20 69 66 20 73 6f 6d 65 /* True if some
13495 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 61 process holds a
13496 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 2a RESERVED lock *
13497 2f 0a 0a 20 20 20 20 2f 2a 20 52 61 63 65 20 63 /.. /* Race c
13498 6f 6e 64 69 74 69 6f 6e 20 68 65 72 65 3a 20 20 ondition here:
13499 41 6e 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 Another process
1349a 6d 69 67 68 74 20 68 61 76 65 20 62 65 65 6e 20 might have been
1349b 68 6f 6c 64 69 6e 67 20 74 68 65 0a 20 20 20 20 holding the.
1349c 2a 2a 20 74 68 65 20 52 45 53 45 52 56 45 44 20 ** the RESERVED
1349d 6c 6f 63 6b 20 61 6e 64 20 68 61 76 65 20 61 20 lock and have a
1349e 6a 6f 75 72 6e 61 6c 20 6f 70 65 6e 20 61 74 20 journal open at
1349f 74 68 65 20 73 71 6c 69 74 65 33 4f 73 41 63 63 the sqlite3OsAcc
134a0 65 73 73 28 29 20 0a 20 20 20 20 2a 2a 20 63 61 ess() . ** ca
134a1 6c 6c 20 61 62 6f 76 65 2c 20 62 75 74 20 74 68 ll above, but th
134a2 65 6e 20 64 65 6c 65 74 65 20 74 68 65 20 6a 6f en delete the jo
134a3 75 72 6e 61 6c 20 61 6e 64 20 64 72 6f 70 20 74 urnal and drop t
134a4 68 65 20 6c 6f 63 6b 20 62 65 66 6f 72 65 0a 20 he lock before.
134a5 20 20 20 2a 2a 20 77 65 20 67 65 74 20 74 6f 20 ** we get to
134a6 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 71 the following sq
134a7 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 65 73 65 lite3OsCheckRese
134a8 72 76 65 64 4c 6f 63 6b 28 29 20 63 61 6c 6c 2e rvedLock() call.
134a9 20 20 49 66 20 74 68 61 74 0a 20 20 20 20 2a 2a If that. **
134aa 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74 68 is the case, th
134ab 69 73 20 72 6f 75 74 69 6e 65 20 6d 69 67 68 74 is routine might
134ac 20 74 68 69 6e 6b 20 74 68 65 72 65 20 69 73 20 think there is
134ad 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 77 68 a hot journal wh
134ae 65 6e 0a 20 20 20 20 2a 2a 20 69 6e 20 66 61 63 en. ** in fac
134af 74 20 74 68 65 72 65 20 69 73 20 6e 6f 6e 65 2e t there is none.
134b0 20 20 54 68 69 73 20 72 65 73 75 6c 74 73 20 69 This results i
134b1 6e 20 61 20 66 61 6c 73 65 2d 70 6f 73 69 74 69 n a false-positi
134b2 76 65 20 77 68 69 63 68 20 77 69 6c 6c 0a 20 20 ve which will.
134b3 20 20 2a 2a 20 62 65 20 64 65 61 6c 74 20 77 69 ** be dealt wi
134b4 74 68 20 62 79 20 74 68 65 20 70 6c 61 79 62 61 th by the playba
134b5 63 6b 20 72 6f 75 74 69 6e 65 2e 20 20 54 69 63 ck routine. Tic
134b6 6b 65 74 20 23 33 38 38 33 2e 0a 20 20 20 20 2a ket #3883.. *
134b7 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 /. rc = sqlit
134b8 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72 76 65 e3OsCheckReserve
134b9 64 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 dLock(pPager->fd
134ba 2c 20 26 6c 6f 63 6b 65 64 29 3b 0a 20 20 20 20 , &locked);.
134bb 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
134bc 4b 20 26 26 20 21 6c 6f 63 6b 65 64 20 29 7b 0a K && !locked ){.
134bd 20 20 20 20 20 20 69 6e 74 20 6e 50 61 67 65 3b int nPage;
134be 0a 0a 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b .. /* Check
134bf 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
134c0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
134c1 49 66 20 69 74 20 63 6f 6e 73 69 73 74 73 20 6f If it consists o
134c2 66 20 30 20 70 61 67 65 73 2c 0a 20 20 20 20 20 f 0 pages,.
134c3 20 2a 2a 20 74 68 65 6e 20 64 65 6c 65 74 65 20 ** then delete
134c4 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
134c5 2e 20 53 65 65 20 74 68 65 20 68 65 61 64 65 72 . See the header
134c6 20 63 6f 6d 6d 65 6e 74 20 61 62 6f 76 65 20 66 comment above f
134c7 6f 72 20 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 or . ** the
134c8 20 72 65 61 73 6f 6e 69 6e 67 20 68 65 72 65 2e reasoning here.
134c9 20 20 44 65 6c 65 74 65 20 74 68 65 20 6f 62 73 Delete the obs
134ca 6f 6c 65 74 65 20 6a 6f 75 72 6e 61 6c 20 66 69 olete journal fi
134cb 6c 65 20 75 6e 64 65 72 0a 20 20 20 20 20 20 2a le under. *
134cc 2a 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 * a RESERVED loc
134cd 6b 20 74 6f 20 61 76 6f 69 64 20 72 61 63 65 20 k to avoid race
134ce 63 6f 6e 64 69 74 69 6f 6e 73 20 61 6e 64 20 74 conditions and t
134cf 6f 20 61 76 6f 69 64 20 76 69 6f 6c 61 74 69 6e o avoid violatin
134d0 67 0a 20 20 20 20 20 20 2a 2a 20 5b 48 33 33 30 g. ** [H330
134d1 32 30 5d 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20].. */.
134d2 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
134d3 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 PagerPagecount(p
134d4 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a Pager, &nPage);.
134d5 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
134d6 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
134d7 20 20 20 69 66 28 20 6e 50 61 67 65 3d 3d 30 20 if( nPage==0
134d8 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c ){. sql
134d9 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d ite3BeginBenignM
134da 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 20 alloc();.
134db 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 73 if( sqlite3Os
134dc 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c Lock(pPager->fd,
134dd 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 3d RESERVED_LOCK)=
134de 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
134df 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
134e0 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73 2c 20 3OsDelete(pVfs,
134e1 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c pPager->zJournal
134e2 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 , 0);.
134e3 20 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 sqlite3OsUnloc
134e4 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 53 48 k(pPager->fd, SH
134e5 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 ARED_LOCK);.
134e6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
134e7 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 sqlite3EndBeni
134e8 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 gnMalloc();.
134e9 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
134ea 20 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 /* The jour
134eb 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74 73 20 nal file exists
134ec 61 6e 64 20 6e 6f 20 6f 74 68 65 72 20 63 6f 6e and no other con
134ed 6e 65 63 74 69 6f 6e 20 68 61 73 20 61 20 72 65 nection has a re
134ee 73 65 72 76 65 64 0a 20 20 20 20 20 20 20 20 20 served.
134ef 20 2a 2a 20 6f 72 20 67 72 65 61 74 65 72 20 6c ** or greater l
134f0 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 ock on the datab
134f1 61 73 65 20 66 69 6c 65 2e 20 4e 6f 77 20 63 68 ase file. Now ch
134f2 65 63 6b 20 74 68 61 74 20 74 68 65 72 65 20 69 eck that there i
134f3 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 s. ** a
134f4 74 20 6c 65 61 73 74 20 6f 6e 65 20 6e 6f 6e 2d t least one non-
134f5 7a 65 72 6f 20 62 79 74 65 73 20 61 74 20 74 68 zero bytes at th
134f6 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 6a e start of the j
134f7 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20 ournal file..
134f8 20 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65 ** If the
134f9 72 65 20 69 73 2c 20 74 68 65 6e 20 77 65 20 63 re is, then we c
134fa 6f 6e 73 69 64 65 72 20 74 68 69 73 20 6a 6f 75 onsider this jou
134fb 72 6e 61 6c 20 74 6f 20 62 65 20 68 6f 74 2e 20 rnal to be hot.
134fc 49 66 20 6e 6f 74 2c 20 0a 20 20 20 20 20 20 20 If not, .
134fd 20 20 20 2a 2a 20 69 74 20 63 61 6e 20 62 65 20 ** it can be
134fe 69 67 6e 6f 72 65 64 2e 0a 20 20 20 20 20 20 20 ignored..
134ff 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 */.
13500 69 6e 74 20 66 20 3d 20 53 51 4c 49 54 45 5f 4f int f = SQLITE_O
13501 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 7c 53 51 4c PEN_READONLY|SQL
13502 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f ITE_OPEN_MAIN_JO
13503 55 52 4e 41 4c 3b 0a 20 20 20 20 20 20 20 20 20 URNAL;.
13504 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f rc = sqlite3OsO
13505 70 65 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72 pen(pVfs, pPager
13506 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 ->zJournal, pPag
13507 65 72 2d 3e 6a 66 64 2c 20 66 2c 20 26 66 29 3b er->jfd, f, &f);
13508 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 . if( r
13509 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
1350a 20 20 20 20 20 20 20 20 20 20 20 20 75 38 20 66 u8 f
1350b 69 72 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 irst = 0;.
1350c 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1350d 65 33 4f 73 52 65 61 64 28 70 50 61 67 65 72 2d e3OsRead(pPager-
1350e 3e 6a 66 64 2c 20 28 76 6f 69 64 20 2a 29 26 66 >jfd, (void *)&f
1350f 69 72 73 74 2c 20 31 2c 20 30 29 3b 0a 20 20 20 irst, 1, 0);.
13510 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d if( rc=
13511 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 =SQLITE_IOERR_SH
13512 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 ORT_READ ){.
13513 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 rc = S
13514 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 QLITE_OK;.
13515 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
13516 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f sqlite3OsClo
13517 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b se(pPager->jfd);
13518 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 45 . *pE
13519 78 69 73 74 73 20 3d 20 28 66 69 72 73 74 21 3d xists = (first!=
1351a 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 0);. }e
1351b 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 lse if( rc==SQLI
1351c 54 45 5f 43 41 4e 54 4f 50 45 4e 20 29 7b 0a 20 TE_CANTOPEN ){.
1351d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 /* If
1351e 20 77 65 20 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 we cannot open
1351f 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 the rollback jou
13520 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20 6f 72 64 rnal file in ord
13521 65 72 20 74 6f 20 73 65 65 20 69 66 0a 20 20 20 er to see if.
13522 20 20 20 20 20 20 20 20 20 2a 2a 20 69 74 73 20 ** its
13523 68 61 73 20 61 20 7a 65 72 6f 20 68 65 61 64 65 has a zero heade
13524 72 2c 20 74 68 61 74 20 6d 69 67 68 74 20 62 65 r, that might be
13525 20 64 75 65 20 74 6f 20 61 6e 20 49 2f 4f 20 65 due to an I/O e
13526 72 72 6f 72 2c 20 6f 72 0a 20 20 20 20 20 20 20 rror, or.
13527 20 20 20 20 20 2a 2a 20 69 74 20 6d 69 67 68 74 ** it might
13528 20 62 65 20 64 75 65 20 74 6f 20 74 68 65 20 72 be due to the r
13529 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 20 64 65 ace condition de
1352a 73 63 72 69 62 65 64 20 61 62 6f 76 65 20 61 6e scribed above an
1352b 64 20 69 6e 0a 20 20 20 20 20 20 20 20 20 20 20 d in.
1352c 20 2a 2a 20 74 69 63 6b 65 74 20 23 33 38 38 33 ** ticket #3883
1352d 2e 20 20 45 69 74 68 65 72 20 77 61 79 2c 20 61 . Either way, a
1352e 73 73 75 6d 65 20 74 68 61 74 20 74 68 65 20 6a ssume that the j
1352f 6f 75 72 6e 61 6c 20 69 73 20 68 6f 74 2e 0a 20 ournal is hot..
13530 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 54 68 ** Th
13531 69 73 20 6d 69 67 68 74 20 62 65 20 61 20 66 61 is might be a fa
13532 6c 73 65 20 70 6f 73 69 74 69 76 65 2e 20 20 42 lse positive. B
13533 75 74 20 69 66 20 69 74 20 69 73 2c 20 74 68 65 ut if it is, the
13534 6e 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20 n the.
13535 20 20 2a 2a 20 61 75 74 6f 6d 61 74 69 63 20 6a ** automatic j
13536 6f 75 72 6e 61 6c 20 70 6c 61 79 62 61 63 6b 20 ournal playback
13537 61 6e 64 20 72 65 63 6f 76 65 72 79 20 6d 65 63 and recovery mec
13538 68 61 6e 69 73 6d 20 77 69 6c 6c 20 64 65 61 6c hanism will deal
13539 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 . **
1353a 77 69 74 68 20 69 74 20 75 6e 64 65 72 20 61 6e with it under an
1353b 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 EXCLUSIVE lock
1353c 77 68 65 72 65 20 77 65 20 64 6f 20 6e 6f 74 20 where we do not
1353d 6e 65 65 64 20 74 6f 0a 20 20 20 20 20 20 20 20 need to.
1353e 20 20 20 20 2a 2a 20 77 6f 72 72 79 20 73 6f 20 ** worry so
1353f 6d 75 63 68 20 77 69 74 68 20 72 61 63 65 20 63 much with race c
13540 6f 6e 64 69 74 69 6f 6e 73 2e 0a 20 20 20 20 20 onditions..
13541 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
13542 20 20 20 20 20 20 2a 70 45 78 69 73 74 73 20 3d *pExists =
13543 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 1;.
13544 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
13545 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
13546 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
13547 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 }. }.. retur
13548 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 n rc;.}../*.** R
13549 65 61 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 ead the content
1354a 66 6f 72 20 70 61 67 65 20 70 50 67 20 6f 75 74 for page pPg out
1354b 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
1354c 20 66 69 6c 65 20 61 6e 64 20 69 6e 74 6f 20 0a file and into .
1354d 2a 2a 20 70 50 67 2d 3e 70 44 61 74 61 2e 20 41 ** pPg->pData. A
1354e 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 6f 72 20 shared lock or
1354f 67 72 65 61 74 65 72 20 6d 75 73 74 20 62 65 20 greater must be
13550 68 65 6c 64 20 6f 6e 20 74 68 65 20 64 61 74 61 held on the data
13551 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 62 65 66 base.** file bef
13552 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f ore this functio
13553 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a n is called..**.
13554 2a 2a 20 49 66 20 70 61 67 65 20 31 20 69 73 20 ** If page 1 is
13555 72 65 61 64 2c 20 74 68 65 6e 20 74 68 65 20 76 read, then the v
13556 61 6c 75 65 20 6f 66 20 50 61 67 65 72 2e 64 62 alue of Pager.db
13557 46 69 6c 65 56 65 72 73 5b 5d 20 69 73 20 73 65 FileVers[] is se
13558 74 20 74 6f 0a 2a 2a 20 74 68 65 20 76 61 6c 75 t to.** the valu
13559 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 e read from the
1355a 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a database file..*
1355b 2a 0a 2a 2a 20 49 66 20 61 6e 20 49 4f 20 65 72 *.** If an IO er
1355c 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65 6e ror occurs, then
1355d 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20 69 73 the IO error is
1355e 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 returned to the
1355f 20 63 61 6c 6c 65 72 2e 0a 2a 2a 20 4f 74 68 65 caller..** Othe
13560 72 77 69 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b rwise, SQLITE_OK
13561 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f is returned..*/
13562 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 61 64 .static int read
13563 44 62 50 61 67 65 28 50 67 48 64 72 20 2a 70 50 DbPage(PgHdr *pP
13564 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 g){. Pager *pPa
13565 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 ger = pPg->pPage
13566 72 3b 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 r; /* Pager obje
13567 63 74 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ct associated wi
13568 74 68 20 70 61 67 65 20 70 50 67 20 2a 2f 0a 20 th page pPg */.
13569 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 50 67 Pgno pgno = pPg
1356a 2d 3e 70 67 6e 6f 3b 20 20 20 20 20 20 20 2f 2a ->pgno; /*
1356b 20 50 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 Page number to
1356c 72 65 61 64 20 2a 2f 0a 20 20 69 6e 74 20 72 63 read */. int rc
1356d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1356e 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e /* Return
1356f 20 63 6f 64 65 20 2a 2f 0a 20 20 69 36 34 20 69 code */. i64 i
13570 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 Offset;
13571 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 20 /* Byte
13572 6f 66 66 73 65 74 20 6f 66 20 66 69 6c 65 20 74 offset of file t
13573 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 0a o read from */..
13574 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
13575 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 ->state>=PAGER_S
13576 48 41 52 45 44 20 26 26 20 21 4d 45 4d 44 42 20 HARED && !MEMDB
13577 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f );. assert( isO
13578 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 pen(pPager->fd)
13579 29 3b 0a 0a 20 20 69 66 28 20 4e 45 56 45 52 28 );.. if( NEVER(
1357a 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e !isOpen(pPager->
1357b 66 64 29 29 20 29 7b 0a 20 20 20 20 61 73 73 65 fd)) ){. asse
1357c 72 74 28 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 rt( pPager->temp
1357d 46 69 6c 65 20 29 3b 0a 20 20 20 20 6d 65 6d 73 File );. mems
1357e 65 74 28 70 50 67 2d 3e 70 44 61 74 61 2c 20 30 et(pPg->pData, 0
1357f 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 , pPager->pageSi
13580 7a 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 ze);. return
13581 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 SQLITE_OK;. }.
13582 20 69 4f 66 66 73 65 74 20 3d 20 28 70 67 6e 6f iOffset = (pgno
13583 2d 31 29 2a 28 69 36 34 29 70 50 61 67 65 72 2d -1)*(i64)pPager-
13584 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 72 63 20 >pageSize;. rc
13585 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 = sqlite3OsRead(
13586 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 67 2d pPager->fd, pPg-
13587 3e 70 44 61 74 61 2c 20 70 50 61 67 65 72 2d 3e >pData, pPager->
13588 70 61 67 65 53 69 7a 65 2c 20 69 4f 66 66 73 65 pageSize, iOffse
13589 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 t);. if( rc==SQ
1358a 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 LITE_IOERR_SHORT
1358b 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 72 63 20 _READ ){. rc
1358c 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d = SQLITE_OK;. }
1358d 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29 . if( pgno==1 )
1358e 7b 0a 20 20 20 20 75 38 20 2a 64 62 46 69 6c 65 {. u8 *dbFile
1358f 56 65 72 73 20 3d 20 26 28 28 75 38 2a 29 70 50 Vers = &((u8*)pP
13590 67 2d 3e 70 44 61 74 61 29 5b 32 34 5d 3b 0a 20 g->pData)[24];.
13591 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65 memcpy(&pPage
13592 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 64 r->dbFileVers, d
13593 62 46 69 6c 65 56 65 72 73 2c 20 73 69 7a 65 6f bFileVers, sizeo
13594 66 28 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 f(pPager->dbFile
13595 56 65 72 73 29 29 3b 0a 20 20 7d 0a 20 20 43 4f Vers));. }. CO
13596 44 45 43 31 28 70 50 61 67 65 72 2c 20 70 50 67 DEC1(pPager, pPg
13597 2d 3e 70 44 61 74 61 2c 20 70 67 6e 6f 2c 20 33 ->pData, pgno, 3
13598 2c 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f , rc = SQLITE_NO
13599 4d 45 4d 29 3b 0a 0a 20 20 50 41 47 45 52 5f 49 MEM);.. PAGER_I
1359a 4e 43 52 28 73 71 6c 69 74 65 33 5f 70 61 67 65 NCR(sqlite3_page
1359b 72 5f 72 65 61 64 64 62 5f 63 6f 75 6e 74 29 3b r_readdb_count);
1359c 0a 20 20 50 41 47 45 52 5f 49 4e 43 52 28 70 50 . PAGER_INCR(pP
1359d 61 67 65 72 2d 3e 6e 52 65 61 64 29 3b 0a 20 20 ager->nRead);.
1359e 49 4f 54 52 41 43 45 28 28 22 50 47 49 4e 20 25 IOTRACE(("PGIN %
1359f 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c p %d\n", pPager,
135a0 20 70 67 6e 6f 29 29 3b 0a 20 20 50 41 47 45 52 pgno));. PAGER
135a1 54 52 41 43 45 28 28 22 46 45 54 43 48 20 25 64 TRACE(("FETCH %d
135a2 20 70 61 67 65 20 25 64 20 68 61 73 68 28 25 30 page %d hash(%0
135a3 38 78 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 8x)\n",.
135a4 20 20 20 20 20 20 20 50 41 47 45 52 49 44 28 70 PAGERID(p
135a5 50 61 67 65 72 29 2c 20 70 67 6e 6f 2c 20 70 61 Pager), pgno, pa
135a6 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 67 ger_pagehash(pPg
135a7 29 29 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 )));.. return r
135a8 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 c;.}../*.** This
135a9 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c function is cal
135aa 6c 65 64 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 led to obtain a
135ab 73 68 61 72 65 64 20 6c 6f 63 6b 20 6f 6e 20 74 shared lock on t
135ac 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
135ad 2e 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 ..** It is illeg
135ae 61 6c 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74 al to call sqlit
135af 65 33 50 61 67 65 72 41 63 71 75 69 72 65 28 29 e3PagerAcquire()
135b0 20 75 6e 74 69 6c 20 61 66 74 65 72 20 74 68 69 until after thi
135b1 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 68 61 s function.** ha
135b2 73 20 62 65 65 6e 20 73 75 63 63 65 73 73 66 75 s been successfu
135b3 6c 6c 79 20 63 61 6c 6c 65 64 2e 20 49 66 20 61 lly called. If a
135b4 20 73 68 61 72 65 64 2d 6c 6f 63 6b 20 69 73 20 shared-lock is
135b5 61 6c 72 65 61 64 79 20 68 65 6c 64 20 77 68 65 already held whe
135b6 6e 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 n.** this functi
135b7 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 69 74 on is called, it
135b8 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a is a no-op..**.
135b9 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
135ba 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65 20 operations are
135bb 61 6c 73 6f 20 70 65 72 66 6f 72 6d 65 64 20 62 also performed b
135bc 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e y this function.
135bd 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 49 66 20 74 .**.** 1) If t
135be 68 65 20 70 61 67 65 72 20 69 73 20 63 75 72 72 he pager is curr
135bf 65 6e 74 6c 79 20 69 6e 20 50 41 47 45 52 5f 55 ently in PAGER_U
135c0 4e 4c 4f 43 4b 20 73 74 61 74 65 20 28 6e 6f 20 NLOCK state (no
135c1 6c 6f 63 6b 20 68 65 6c 64 0a 2a 2a 20 20 20 20 lock held.**
135c2 20 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 on the databas
135c3 65 20 66 69 6c 65 29 2c 20 74 68 65 6e 20 61 6e e file), then an
135c4 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 attempt is made
135c5 20 74 6f 20 6f 62 74 61 69 6e 20 61 0a 2a 2a 20 to obtain a.**
135c6 20 20 20 20 20 53 48 41 52 45 44 20 6c 6f 63 6b SHARED lock
135c7 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
135c8 20 66 69 6c 65 2e 20 49 6d 6d 65 64 69 61 74 65 file. Immediate
135c9 6c 79 20 61 66 74 65 72 20 6f 62 74 61 69 6e 69 ly after obtaini
135ca 6e 67 0a 2a 2a 20 20 20 20 20 20 74 68 65 20 53 ng.** the S
135cb 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20 HARED lock, the
135cc 66 69 6c 65 2d 73 79 73 74 65 6d 20 69 73 20 63 file-system is c
135cd 68 65 63 6b 65 64 20 66 6f 72 20 61 20 68 6f 74 hecked for a hot
135ce 2d 6a 6f 75 72 6e 61 6c 2c 0a 2a 2a 20 20 20 20 -journal,.**
135cf 20 20 77 68 69 63 68 20 69 73 20 70 6c 61 79 65 which is playe
135d0 64 20 62 61 63 6b 20 69 66 20 70 72 65 73 65 6e d back if presen
135d1 74 2e 20 46 6f 6c 6c 6f 77 69 6e 67 20 61 6e 79 t. Following any
135d2 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 0a 2a 2a hot-journal .**
135d3 20 20 20 20 20 20 72 6f 6c 6c 62 61 63 6b 2c 20 rollback,
135d4 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
135d5 74 68 65 20 63 61 63 68 65 20 61 72 65 20 76 61 the cache are va
135d6 6c 69 64 61 74 65 64 20 62 79 20 63 68 65 63 6b lidated by check
135d7 69 6e 67 0a 2a 2a 20 20 20 20 20 20 74 68 65 20 ing.** the
135d8 27 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 27 'change-counter'
135d9 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 64 61 field of the da
135da 74 61 62 61 73 65 20 66 69 6c 65 20 68 65 61 64 tabase file head
135db 65 72 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 64 er and.** d
135dc 69 73 63 61 72 64 65 64 20 69 66 20 74 68 65 79 iscarded if they
135dd 20 61 72 65 20 66 6f 75 6e 64 20 74 6f 20 62 65 are found to be
135de 20 69 6e 76 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20 invalid..**.**
135df 20 20 32 29 20 49 66 20 74 68 65 20 70 61 67 65 2) If the page
135e0 72 20 69 73 20 72 75 6e 6e 69 6e 67 20 69 6e 20 r is running in
135e1 65 78 63 6c 75 73 69 76 65 2d 6d 6f 64 65 2c 20 exclusive-mode,
135e2 61 6e 64 20 74 68 65 72 65 20 61 72 65 20 63 75 and there are cu
135e3 72 72 65 6e 74 6c 79 0a 2a 2a 20 20 20 20 20 20 rrently.**
135e4 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 no outstanding r
135e5 65 66 65 72 65 6e 63 65 73 20 74 6f 20 61 6e 79 eferences to any
135e6 20 70 61 67 65 73 2c 20 61 6e 64 20 69 73 20 69 pages, and is i
135e7 6e 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 n the error stat
135e8 65 2c 0a 2a 2a 20 20 20 20 20 20 74 68 65 6e 20 e,.** then
135e9 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 an attempt is ma
135ea 64 65 20 74 6f 20 63 6c 65 61 72 20 74 68 65 20 de to clear the
135eb 65 72 72 6f 72 20 73 74 61 74 65 20 62 79 20 64 error state by d
135ec 69 73 63 61 72 64 69 6e 67 0a 2a 2a 20 20 20 20 iscarding.**
135ed 20 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f the contents o
135ee 66 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 f the page cache
135ef 20 61 6e 64 20 72 6f 6c 6c 69 6e 67 20 62 61 63 and rolling bac
135f0 6b 20 61 6e 79 20 6f 70 65 6e 20 6a 6f 75 72 6e k any open journ
135f1 61 6c 0a 2a 2a 20 20 20 20 20 20 66 69 6c 65 2e al.** file.
135f2 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6f 70 .**.** If the op
135f3 65 72 61 74 69 6f 6e 20 64 65 73 63 72 69 62 65 eration describe
135f4 64 20 62 79 20 28 32 29 20 61 62 6f 76 65 20 69 d by (2) above i
135f5 73 20 6e 6f 74 20 61 74 74 65 6d 70 74 65 64 2c s not attempted,
135f6 20 61 6e 64 20 69 66 20 74 68 65 0a 2a 2a 20 70 and if the.** p
135f7 61 67 65 72 20 69 73 20 69 6e 20 61 6e 20 65 72 ager is in an er
135f8 72 6f 72 20 73 74 61 74 65 20 6f 74 68 65 72 20 ror state other
135f9 74 68 61 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c than SQLITE_FULL
135fa 20 77 68 65 6e 20 74 68 69 73 20 69 73 20 63 61 when this is ca
135fb 6c 6c 65 64 2c 0a 2a 2a 20 74 68 65 20 65 72 72 lled,.** the err
135fc 6f 72 20 73 74 61 74 65 20 65 72 72 6f 72 20 63 or state error c
135fd 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e ode is returned.
135fe 20 49 74 20 69 73 20 70 65 72 6d 69 74 74 65 64 It is permitted
135ff 20 74 6f 20 72 65 61 64 20 74 68 65 0a 2a 2a 20 to read the.**
13600 64 61 74 61 62 61 73 65 20 77 68 65 6e 20 69 6e database when in
13601 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 65 72 72 SQLITE_FULL err
13602 6f 72 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 or state..**.**
13603 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 65 76 Otherwise, if ev
13604 65 72 79 74 68 69 6e 67 20 69 73 20 73 75 63 63 erything is succ
13605 65 73 73 66 75 6c 2c 20 53 51 4c 49 54 45 5f 4f essful, SQLITE_O
13606 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 K is returned. I
13607 66 20 61 6e 0a 2a 2a 20 49 4f 20 65 72 72 6f 72 f an.** IO error
13608 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 6c 6f occurs while lo
13609 63 6b 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 cking the databa
1360a 73 65 2c 20 63 68 65 63 6b 69 6e 67 20 66 6f 72 se, checking for
1360b 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 0a 2a a hot-journal.*
1360c 2a 20 66 69 6c 65 20 6f 72 20 72 6f 6c 6c 69 6e * file or rollin
1360d 67 20 62 61 63 6b 20 61 20 6a 6f 75 72 6e 61 6c g back a journal
1360e 20 66 69 6c 65 2c 20 74 68 65 20 49 4f 20 65 72 file, the IO er
1360f 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 ror code is retu
13610 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f rned..*/.SQLITE_
13611 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
13612 74 65 33 50 61 67 65 72 53 68 61 72 65 64 4c 6f te3PagerSharedLo
13613 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 ck(Pager *pPager
13614 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
13615 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 LITE_OK;
13616 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
13617 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 n code */. int
13618 69 73 45 72 72 6f 72 52 65 73 65 74 20 3d 20 30 isErrorReset = 0
13619 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
1361a 2a 20 54 72 75 65 20 69 66 20 72 65 63 6f 76 65 * True if recove
1361b 72 69 6e 67 20 66 72 6f 6d 20 65 72 72 6f 72 20 ring from error
1361c 73 74 61 74 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54 state */.. /* T
1361d 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6f his routine is o
1361e 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 nly called from
1361f 62 2d 74 72 65 65 20 61 6e 64 20 6f 6e 6c 79 20 b-tree and only
13620 77 68 65 6e 20 74 68 65 72 65 20 61 72 65 20 6e when there are n
13621 6f 0a 20 20 2a 2a 20 6f 75 74 73 74 61 6e 64 69 o. ** outstandi
13622 6e 67 20 70 61 67 65 73 20 2a 2f 0a 20 20 61 73 ng pages */. as
13623 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 63 61 sert( sqlite3Pca
13624 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67 cheRefCount(pPag
13625 65 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d 30 20 er->pPCache)==0
13626 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 4d );. if( NEVER(M
13627 45 4d 44 42 20 26 26 20 70 50 61 67 65 72 2d 3e EMDB && pPager->
13628 65 72 72 43 6f 64 65 29 20 29 7b 20 72 65 74 75 errCode) ){ retu
13629 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f rn pPager->errCo
1362a 64 65 3b 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 de; }.. /* If t
1362b 68 69 73 20 64 61 74 61 62 61 73 65 20 69 73 20 his database is
1362c 69 6e 20 61 6e 20 65 72 72 6f 72 2d 73 74 61 74 in an error-stat
1362d 65 2c 20 6e 6f 77 20 69 73 20 61 20 63 68 61 6e e, now is a chan
1362e 63 65 20 74 6f 20 63 6c 65 61 72 0a 20 20 2a 2a ce to clear. **
1362f 20 74 68 65 20 65 72 72 6f 72 2e 20 44 69 73 63 the error. Disc
13630 61 72 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 ard the contents
13631 20 6f 66 20 74 68 65 20 70 61 67 65 72 2d 63 61 of the pager-ca
13632 63 68 65 20 61 6e 64 20 72 6f 6c 6c 62 61 63 6b che and rollback
13633 0a 20 20 2a 2a 20 61 6e 79 20 68 6f 74 20 6a 6f . ** any hot jo
13634 75 72 6e 61 6c 20 69 6e 20 74 68 65 20 66 69 6c urnal in the fil
13635 65 2d 73 79 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20 e-system.. */.
13636 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 if( pPager->err
13637 43 6f 64 65 20 29 7b 0a 20 20 20 20 69 66 28 20 Code ){. if(
13638 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a isOpen(pPager->j
13639 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 7a fd) || pPager->z
1363a 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 20 Journal ){.
1363b 20 69 73 45 72 72 6f 72 52 65 73 65 74 20 3d 20 isErrorReset =
1363c 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 1;. }. pPa
1363d 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 53 ger->errCode = S
1363e 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 70 61 QLITE_OK;. pa
1363f 67 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 ger_reset(pPager
13640 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 );. }.. if( pP
13641 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 ager->state==PAG
13642 45 52 5f 55 4e 4c 4f 43 4b 20 7c 7c 20 69 73 45 ER_UNLOCK || isE
13643 72 72 6f 72 52 65 73 65 74 20 29 7b 0a 20 20 20 rrorReset ){.
13644 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 20 63 sqlite3_vfs * c
13645 6f 6e 73 74 20 70 56 66 73 20 3d 20 70 50 61 67 onst pVfs = pPag
13646 65 72 2d 3e 70 56 66 73 3b 0a 20 20 20 20 69 6e er->pVfs;. in
13647 74 20 69 73 48 6f 74 4a 6f 75 72 6e 61 6c 20 3d t isHotJournal =
13648 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 0;. assert(
13649 21 4d 45 4d 44 42 20 29 3b 0a 20 20 20 20 61 73 !MEMDB );. as
1364a 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 63 61 sert( sqlite3Pca
1364b 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67 cheRefCount(pPag
1364c 65 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d 30 20 er->pPCache)==0
1364d 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 );. if( pPage
1364e 72 2d 3e 6e 6f 52 65 61 64 6c 6f 63 6b 20 29 7b r->noReadlock ){
1364f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
13650 50 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 20 Pager->readOnly
13651 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d );. pPager-
13652 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 53 >state = PAGER_S
13653 48 41 52 45 44 3b 0a 20 20 20 20 7d 65 6c 73 65 HARED;. }else
13654 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 {. rc = pag
13655 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 er_wait_on_lock(
13656 70 50 61 67 65 72 2c 20 53 48 41 52 45 44 5f 4c pPager, SHARED_L
13657 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 OCK);. if(
13658 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
13659 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
1365a 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d pPager->state==
1365b 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a PAGER_UNLOCK );.
1365c 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 return p
1365d 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 ager_error(pPage
1365e 72 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 7d 0a r, rc);. }.
1365f 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 }. assert
13660 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e ( pPager->state>
13661 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a =SHARED_LOCK );.
13662 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 6a 6f 75 . /* If a jou
13663 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74 73 rnal file exists
13664 2c 20 61 6e 64 20 74 68 65 72 65 20 69 73 20 6e , and there is n
13665 6f 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 o RESERVED lock
13666 6f 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 on the. ** da
13667 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 tabase file, the
13668 6e 20 69 74 20 65 69 74 68 65 72 20 6e 65 65 64 n it either need
13669 73 20 74 6f 20 62 65 20 70 6c 61 79 65 64 20 62 s to be played b
1366a 61 63 6b 20 6f 72 20 64 65 6c 65 74 65 64 2e 0a ack or deleted..
1366b 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 */. if( !
1366c 69 73 45 72 72 6f 72 52 65 73 65 74 20 29 7b 0a isErrorReset ){.
1366d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 assert( pP
1366e 61 67 65 72 2d 3e 73 74 61 74 65 20 3c 3d 20 50 ager->state <= P
1366f 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b 0a 20 AGER_SHARED );.
13670 20 20 20 20 20 72 63 20 3d 20 68 61 73 48 6f 74 rc = hasHot
13671 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2c 20 Journal(pPager,
13672 26 69 73 48 6f 74 4a 6f 75 72 6e 61 6c 29 3b 0a &isHotJournal);.
13673 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
13674 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
13675 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a goto failed;.
13676 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
13677 20 20 69 66 28 20 69 73 45 72 72 6f 72 52 65 73 if( isErrorRes
13678 65 74 20 7c 7c 20 69 73 48 6f 74 4a 6f 75 72 6e et || isHotJourn
13679 61 6c 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 47 al ){. /* G
1367a 65 74 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 et an EXCLUSIVE
1367b 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 lock on the data
1367c 62 61 73 65 20 66 69 6c 65 2e 20 41 74 20 74 68 base file. At th
1367d 69 73 20 70 6f 69 6e 74 20 69 74 20 69 73 0a 20 is point it is.
1367e 20 20 20 20 20 2a 2a 20 69 6d 70 6f 72 74 61 6e ** importan
1367f 74 20 74 68 61 74 20 61 20 52 45 53 45 52 56 45 t that a RESERVE
13680 44 20 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 6f 62 D lock is not ob
13681 74 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 77 61 tained on the wa
13682 79 20 74 6f 20 74 68 65 0a 20 20 20 20 20 20 2a y to the. *
13683 2a 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b * EXCLUSIVE lock
13684 2e 20 49 66 20 69 74 20 77 65 72 65 2c 20 61 6e . If it were, an
13685 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 6d 69 other process mi
13686 67 68 74 20 6f 70 65 6e 20 74 68 65 0a 20 20 20 ght open the.
13687 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 ** database f
13688 69 6c 65 2c 20 64 65 74 65 63 74 20 74 68 65 20 ile, detect the
13689 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 2c 20 61 RESERVED lock, a
1368a 6e 64 20 63 6f 6e 63 6c 75 64 65 20 74 68 61 74 nd conclude that
1368b 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 64 61 the. ** da
1368c 74 61 62 61 73 65 20 69 73 20 73 61 66 65 20 74 tabase is safe t
1368d 6f 20 72 65 61 64 20 77 68 69 6c 65 20 74 68 69 o read while thi
1368e 73 20 70 72 6f 63 65 73 73 20 69 73 20 73 74 69 s process is sti
1368f 6c 6c 20 72 6f 6c 6c 69 6e 67 20 74 68 65 20 0a ll rolling the .
13690 20 20 20 20 20 20 2a 2a 20 68 6f 74 2d 6a 6f 75 ** hot-jou
13691 72 6e 61 6c 20 62 61 63 6b 2e 0a 20 20 20 20 20 rnal back..
13692 20 2a 2a 20 0a 20 20 20 20 20 20 2a 2a 20 42 65 ** . ** Be
13693 63 61 75 73 65 20 74 68 65 20 69 6e 74 65 72 6d cause the interm
13694 65 64 69 61 74 65 20 52 45 53 45 52 56 45 44 20 ediate RESERVED
13695 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 72 65 71 75 lock is not requ
13696 65 73 74 65 64 2c 20 61 6e 79 0a 20 20 20 20 20 ested, any.
13697 20 2a 2a 20 6f 74 68 65 72 20 70 72 6f 63 65 73 ** other proces
13698 73 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 s attempting to
13699 61 63 63 65 73 73 20 74 68 65 20 64 61 74 61 62 access the datab
1369a 61 73 65 20 66 69 6c 65 20 77 69 6c 6c 20 67 65 ase file will ge
1369b 74 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20 74 t to . ** t
1369c 68 69 73 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 his point in the
1369d 20 63 6f 64 65 20 61 6e 64 20 66 61 69 6c 20 74 code and fail t
1369e 6f 20 6f 62 74 61 69 6e 20 69 74 73 20 6f 77 6e o obtain its own
1369f 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 EXCLUSIVE lock
136a0 0a 20 20 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 . ** on the
136a1 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a database file..
136a2 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 */. i
136a3 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 f( pPager->state
136a4 3c 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 <EXCLUSIVE_LOCK
136a5 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
136a6 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70 50 sqlite3OsLock(pP
136a7 61 67 65 72 2d 3e 66 64 2c 20 45 58 43 4c 55 53 ager->fd, EXCLUS
136a8 49 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 IVE_LOCK);.
136a9 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
136aa 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
136ab 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 72 72 rc = pager_err
136ac 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a or(pPager, rc);.
136ad 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 goto f
136ae 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 20 20 7d ailed;. }
136af 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d . pPager-
136b0 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 45 >state = PAGER_E
136b1 58 43 4c 55 53 49 56 45 3b 0a 20 20 20 20 20 20 XCLUSIVE;.
136b2 7d 0a 20 0a 20 20 20 20 20 20 2f 2a 20 4f 70 65 }. . /* Ope
136b3 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 6f n the journal fo
136b4 72 20 72 65 61 64 2f 77 72 69 74 65 20 61 63 63 r read/write acc
136b5 65 73 73 2e 20 54 68 69 73 20 69 73 20 62 65 63 ess. This is bec
136b6 61 75 73 65 20 69 6e 20 0a 20 20 20 20 20 20 2a ause in . *
136b7 2a 20 65 78 63 6c 75 73 69 76 65 2d 61 63 63 65 * exclusive-acce
136b8 73 73 20 6d 6f 64 65 20 74 68 65 20 66 69 6c 65 ss mode the file
136b9 20 64 65 73 63 72 69 70 74 6f 72 20 77 69 6c 6c descriptor will
136ba 20 62 65 20 6b 65 70 74 20 6f 70 65 6e 20 61 6e be kept open an
136bb 64 0a 20 20 20 20 20 20 2a 2a 20 70 6f 73 73 69 d. ** possi
136bc 62 6c 79 20 75 73 65 64 20 66 6f 72 20 61 20 74 bly used for a t
136bd 72 61 6e 73 61 63 74 69 6f 6e 20 6c 61 74 65 72 ransaction later
136be 20 6f 6e 2e 20 4f 6e 20 73 6f 6d 65 20 73 79 73 on. On some sys
136bf 74 65 6d 73 2c 20 74 68 65 0a 20 20 20 20 20 20 tems, the.
136c0 2a 2a 20 4f 73 54 72 75 6e 63 61 74 65 28 29 20 ** OsTruncate()
136c1 63 61 6c 6c 20 75 73 65 64 20 69 6e 20 65 78 63 call used in exc
136c2 6c 75 73 69 76 65 2d 61 63 63 65 73 73 20 6d 6f lusive-access mo
136c3 64 65 20 61 6c 73 6f 20 72 65 71 75 69 72 65 73 de also requires
136c4 0a 20 20 20 20 20 20 2a 2a 20 61 20 72 65 61 64 . ** a read
136c5 2f 77 72 69 74 65 20 66 69 6c 65 20 68 61 6e 64 /write file hand
136c6 6c 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 le.. */.
136c7 20 20 20 69 66 28 20 21 69 73 4f 70 65 6e 28 70 if( !isOpen(p
136c8 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 Pager->jfd) ){.
136c9 20 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a int res;.
136ca 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
136cb 69 74 65 33 4f 73 41 63 63 65 73 73 28 70 56 66 ite3OsAccess(pVf
136cc 73 2c 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e s,pPager->zJourn
136cd 61 6c 2c 53 51 4c 49 54 45 5f 41 43 43 45 53 53 al,SQLITE_ACCESS
136ce 5f 45 58 49 53 54 53 2c 26 72 65 73 29 3b 0a 20 _EXISTS,&res);.
136cf 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 if( rc==S
136d0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
136d1 20 20 20 20 20 20 69 66 28 20 72 65 73 20 29 7b if( res ){
136d2 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 . int
136d3 20 66 6f 75 74 20 3d 20 30 3b 0a 20 20 20 20 20 fout = 0;.
136d4 20 20 20 20 20 20 20 69 6e 74 20 66 20 3d 20 53 int f = S
136d5 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 QLITE_OPEN_READW
136d6 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e RITE|SQLITE_OPEN
136d7 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 3b 0a 20 _MAIN_JOURNAL;.
136d8 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 asser
136d9 74 28 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 t( !pPager->temp
136da 46 69 6c 65 20 29 3b 0a 20 20 20 20 20 20 20 20 File );.
136db 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
136dc 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 50 61 OsOpen(pVfs, pPa
136dd 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 ger->zJournal, p
136de 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66 2c 20 26 Pager->jfd, f, &
136df 66 6f 75 74 29 3b 0a 20 20 20 20 20 20 20 20 20 fout);.
136e0 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 assert( rc!=S
136e1 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 73 4f 70 QLITE_OK || isOp
136e2 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
136e3 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 );. i
136e4 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
136e5 20 26 26 20 66 6f 75 74 26 53 51 4c 49 54 45 5f && fout&SQLITE_
136e6 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 20 29 7b OPEN_READONLY ){
136e7 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 . r
136e8 63 20 3d 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f c = SQLITE_CANTO
136e9 50 45 4e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 PEN;.
136ea 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 sqlite3OsClos
136eb 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a e(pPager->jfd);.
136ec 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
136ed 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
136ee 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 /* If
136ef 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 64 6f 65 the journal doe
136f0 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 69 74 20 s not exist, it
136f1 75 73 75 61 6c 6c 79 20 6d 65 61 6e 73 20 74 68 usually means th
136f2 61 74 20 73 6f 6d 65 20 0a 20 20 20 20 20 20 20 at some .
136f3 20 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 63 6f ** other co
136f4 6e 6e 65 63 74 69 6f 6e 20 6d 61 6e 61 67 65 64 nnection managed
136f5 20 74 6f 20 67 65 74 20 69 6e 20 61 6e 64 20 72 to get in and r
136f6 6f 6c 6c 20 69 74 20 62 61 63 6b 20 62 65 66 6f oll it back befo
136f7 72 65 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 re .
136f8 2a 2a 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 ** this connecti
136f9 6f 6e 20 6f 62 74 61 69 6e 65 64 20 74 68 65 20 on obtained the
136fa 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 61 exclusive lock a
136fb 62 6f 76 65 2e 20 4f 72 2c 20 69 74 20 0a 20 20 bove. Or, it .
136fc 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6d 61 79 ** may
136fd 20 6d 65 61 6e 20 74 68 61 74 20 74 68 65 20 70 mean that the p
136fe 61 67 65 72 20 77 61 73 20 69 6e 20 74 68 65 20 ager was in the
136ff 65 72 72 6f 72 2d 73 74 61 74 65 20 77 68 65 6e error-state when
13700 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 20 20 this.
13701 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 77 61 ** function wa
13702 73 20 63 61 6c 6c 65 64 20 61 6e 64 20 74 68 65 s called and the
13703 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 6f journal file do
13704 65 73 20 6e 6f 74 20 65 78 69 73 74 2e 20 20 2a es not exist. *
13705 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 /. rc
13706 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 = pager_end_tra
13707 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c nsaction(pPager,
13708 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0);. }
13709 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1370a 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 }. if( rc!
1370b 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1370c 20 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 goto faile
1370d 64 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 d;. }..
1370e 20 20 2f 2a 20 54 4f 44 4f 3a 20 57 68 79 20 61 /* TODO: Why a
1370f 72 65 20 74 68 65 73 65 20 63 6c 65 61 72 65 64 re these cleared
13710 20 68 65 72 65 3f 20 49 73 20 69 74 20 6e 65 63 here? Is it nec
13711 65 73 73 61 72 79 3f 20 2a 2f 0a 20 20 20 20 20 essary? */.
13712 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
13713 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20 20 20 Started = 0;.
13714 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e pPager->journ
13715 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 20 alOff = 0;.
13716 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 pPager->setMast
13717 65 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 er = 0;. pP
13718 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 ager->journalHdr
13719 20 3d 20 30 3b 0a 20 0a 20 20 20 20 20 20 2f 2a = 0;. . /*
1371a 20 50 6c 61 79 62 61 63 6b 20 61 6e 64 20 64 65 Playback and de
1371b 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c lete the journal
1371c 2e 20 20 44 72 6f 70 20 74 68 65 20 64 61 74 61 . Drop the data
1371d 62 61 73 65 20 77 72 69 74 65 0a 20 20 20 20 20 base write.
1371e 20 2a 2a 20 6c 6f 63 6b 20 61 6e 64 20 72 65 61 ** lock and rea
1371f 63 71 75 69 72 65 20 74 68 65 20 72 65 61 64 20 cquire the read
13720 6c 6f 63 6b 2e 20 50 75 72 67 65 20 74 68 65 20 lock. Purge the
13721 63 61 63 68 65 20 62 65 66 6f 72 65 0a 20 20 20 cache before.
13722 20 20 20 2a 2a 20 70 6c 61 79 69 6e 67 20 62 61 ** playing ba
13723 63 6b 20 74 68 65 20 68 6f 74 2d 6a 6f 75 72 6e ck the hot-journ
13724 61 6c 20 73 6f 20 74 68 61 74 20 77 65 20 64 6f al so that we do
13725 6e 27 74 20 65 6e 64 20 75 70 20 77 69 74 68 0a n't end up with.
13726 20 20 20 20 20 20 2a 2a 20 61 6e 20 69 6e 63 6f ** an inco
13727 6e 73 69 73 74 65 6e 74 20 63 61 63 68 65 2e 0a nsistent cache..
13728 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 */. i
13729 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 f( isOpen(pPager
1372a 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20 ->jfd) ){.
1372b 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 rc = pager_pla
1372c 79 62 61 63 6b 28 70 50 61 67 65 72 2c 20 31 29 yback(pPager, 1)
1372d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 ;. if( rc
1372e 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1372f 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 rc = pa
13730 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 ger_error(pPager
13731 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 20 20 20 , rc);.
13732 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 goto failed;.
13733 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
13734 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 assert( (p
13735 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 Pager->state==PA
13736 47 45 52 5f 53 48 41 52 45 44 29 0a 20 20 20 20 GER_SHARED).
13737 20 20 20 20 20 20 20 7c 7c 20 28 70 50 61 67 65 || (pPage
13738 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 r->exclusiveMode
13739 20 26 26 20 70 50 61 67 65 72 2d 3e 73 74 61 74 && pPager->stat
1373a 65 3e 50 41 47 45 52 5f 53 48 41 52 45 44 29 0a e>PAGER_SHARED).
1373b 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 0a );. }..
1373c 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e if( pPager->
1373d 70 42 61 63 6b 75 70 20 7c 7c 20 73 71 6c 69 74 pBackup || sqlit
1373e 65 33 50 63 61 63 68 65 50 61 67 65 63 6f 75 6e e3PcachePagecoun
1373f 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 t(pPager->pPCach
13740 65 29 3e 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a e)>0 ){. /*
13741 20 54 68 65 20 73 68 61 72 65 64 2d 6c 6f 63 6b The shared-lock
13742 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 61 has just been a
13743 63 71 75 69 72 65 64 20 6f 6e 20 74 68 65 20 64 cquired on the d
13744 61 74 61 62 61 73 65 20 66 69 6c 65 0a 20 20 20 atabase file.
13745 20 20 20 2a 2a 20 61 6e 64 20 74 68 65 72 65 20 ** and there
13746 61 72 65 20 61 6c 72 65 61 64 79 20 70 61 67 65 are already page
13747 73 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 28 s in the cache (
13748 66 72 6f 6d 20 61 20 70 72 65 76 69 6f 75 73 0a from a previous.
13749 20 20 20 20 20 20 2a 2a 20 72 65 61 64 20 6f 72 ** read or
1374a 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 write transacti
1374b 6f 6e 29 2e 20 20 43 68 65 63 6b 20 74 6f 20 73 on). Check to s
1374c 65 65 20 69 66 20 74 68 65 20 64 61 74 61 62 61 ee if the databa
1374d 73 65 0a 20 20 20 20 20 20 2a 2a 20 68 61 73 20 se. ** has
1374e 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 2e 20 20 been modified.
1374f 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 If the database
13750 68 61 73 20 63 68 61 6e 67 65 64 2c 20 66 6c 75 has changed, flu
13751 73 68 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 sh the. **
13752 63 61 63 68 65 2e 0a 20 20 20 20 20 20 2a 2a 0a cache.. **.
13753 20 20 20 20 20 20 2a 2a 20 44 61 74 61 62 61 73 ** Databas
13754 65 20 63 68 61 6e 67 65 73 20 69 73 20 64 65 74 e changes is det
13755 65 63 74 65 64 20 62 79 20 6c 6f 6f 6b 69 6e 67 ected by looking
13756 20 61 74 20 31 35 20 62 79 74 65 73 20 62 65 67 at 15 bytes beg
13757 69 6e 6e 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 inning. **
13758 61 74 20 6f 66 66 73 65 74 20 32 34 20 69 6e 74 at offset 24 int
13759 6f 20 74 68 65 20 66 69 6c 65 2e 20 20 54 68 65 o the file. The
1375a 20 66 69 72 73 74 20 34 20 6f 66 20 74 68 65 73 first 4 of thes
1375b 65 20 31 36 20 62 79 74 65 73 20 61 72 65 0a 20 e 16 bytes are.
1375c 20 20 20 20 20 2a 2a 20 61 20 33 32 2d 62 69 74 ** a 32-bit
1375d 20 63 6f 75 6e 74 65 72 20 74 68 61 74 20 69 73 counter that is
1375e 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 69 74 incremented wit
1375f 68 20 65 61 63 68 20 63 68 61 6e 67 65 2e 20 20 h each change.
13760 54 68 65 0a 20 20 20 20 20 20 2a 2a 20 6f 74 68 The. ** oth
13761 65 72 20 62 79 74 65 73 20 63 68 61 6e 67 65 20 er bytes change
13762 72 61 6e 64 6f 6d 6c 79 20 77 69 74 68 20 65 61 randomly with ea
13763 63 68 20 66 69 6c 65 20 63 68 61 6e 67 65 20 77 ch file change w
13764 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 61 20 63 hen. ** a c
13765 6f 64 65 63 20 69 73 20 69 6e 20 75 73 65 2e 0a odec is in use..
13766 20 20 20 20 20 20 2a 2a 20 0a 20 20 20 20 20 20 ** .
13767 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20 76 61 ** There is a va
13768 6e 69 73 68 69 6e 67 6c 79 20 73 6d 61 6c 6c 20 nishingly small
13769 63 68 61 6e 63 65 20 74 68 61 74 20 61 20 63 68 chance that a ch
1376a 61 6e 67 65 20 77 69 6c 6c 20 6e 6f 74 20 62 65 ange will not be
1376b 20 0a 20 20 20 20 20 20 2a 2a 20 64 65 74 65 63 . ** detec
1376c 74 65 64 2e 20 20 54 68 65 20 63 68 61 6e 63 65 ted. The chance
1376d 20 6f 66 20 61 6e 20 75 6e 64 65 74 65 63 74 65 of an undetecte
1376e 64 20 63 68 61 6e 67 65 20 69 73 20 73 6f 20 73 d change is so s
1376f 6d 61 6c 6c 20 74 68 61 74 0a 20 20 20 20 20 20 mall that.
13770 2a 2a 20 69 74 20 63 61 6e 20 62 65 20 6e 65 67 ** it can be neg
13771 6c 65 63 74 65 64 2e 0a 20 20 20 20 20 20 2a 2f lected.. */
13772 0a 20 20 20 20 20 20 63 68 61 72 20 64 62 46 69 . char dbFi
13773 6c 65 56 65 72 73 5b 73 69 7a 65 6f 66 28 70 50 leVers[sizeof(pP
13774 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 ager->dbFileVers
13775 29 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 )];. sqlite
13776 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 3PagerPagecount(
13777 70 50 61 67 65 72 2c 20 30 29 3b 0a 0a 20 20 20 pPager, 0);..
13778 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 if( pPager->e
13779 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 rrCode ){.
1377a 20 20 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 rc = pPager->e
1377b 72 72 43 6f 64 65 3b 0a 20 20 20 20 20 20 20 20 rrCode;.
1377c 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 goto failed;.
1377d 20 20 20 7d 0a 0a 20 20 20 20 20 20 61 73 73 65 }.. asse
1377e 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 rt( pPager->dbSi
1377f 7a 65 56 61 6c 69 64 20 29 3b 0a 20 20 20 20 20 zeValid );.
13780 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53 if( pPager->dbS
13781 69 7a 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 ize>0 ){.
13782 20 49 4f 54 52 41 43 45 28 28 22 43 4b 56 45 52 IOTRACE(("CKVER
13783 53 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 S %p %d\n", pPag
13784 65 72 2c 20 73 69 7a 65 6f 66 28 64 62 46 69 6c er, sizeof(dbFil
13785 65 56 65 72 73 29 29 29 3b 0a 20 20 20 20 20 20 eVers)));.
13786 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
13787 52 65 61 64 28 70 50 61 67 65 72 2d 3e 66 64 2c Read(pPager->fd,
13788 20 26 64 62 46 69 6c 65 56 65 72 73 2c 20 73 69 &dbFileVers, si
13789 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 72 73 29 zeof(dbFileVers)
1378a 2c 20 32 34 29 3b 0a 20 20 20 20 20 20 20 20 69 , 24);. i
1378b 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1378c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f ){. go
1378d 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 to failed;.
1378e 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
1378f 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 {. memset
13790 28 64 62 46 69 6c 65 56 65 72 73 2c 20 30 2c 20 (dbFileVers, 0,
13791 73 69 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 72 sizeof(dbFileVer
13792 73 29 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 s));. }..
13793 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 70 if( memcmp(p
13794 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 Pager->dbFileVer
13795 73 2c 20 64 62 46 69 6c 65 56 65 72 73 2c 20 73 s, dbFileVers, s
13796 69 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 72 73 izeof(dbFileVers
13797 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ))!=0 ){.
13798 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 50 61 pager_reset(pPa
13799 67 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ger);. }.
1379a 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
1379b 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 pPager->exclusiv
1379c 65 4d 6f 64 65 20 7c 7c 20 70 50 61 67 65 72 2d eMode || pPager-
1379d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 48 >state==PAGER_SH
1379e 41 52 45 44 20 29 3b 0a 20 20 7d 0a 0a 20 66 61 ARED );. }.. fa
1379f 69 6c 65 64 3a 0a 20 20 69 66 28 20 72 63 21 3d iled:. if( rc!=
137a0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
137a1 20 2f 2a 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b /* pager_unlock
137a2 28 29 20 69 73 20 61 20 6e 6f 2d 6f 70 20 66 6f () is a no-op fo
137a3 72 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 r exclusive mode
137a4 20 61 6e 64 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 and in-memory d
137a5 61 74 61 62 61 73 65 73 2e 20 2a 2f 0a 20 20 20 atabases. */.
137a6 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 50 pager_unlock(pP
137a7 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 ager);. }. ret
137a8 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
137a9 20 49 66 20 74 68 65 20 72 65 66 65 72 65 6e 63 If the referenc
137aa 65 20 63 6f 75 6e 74 20 68 61 73 20 72 65 61 63 e count has reac
137ab 68 65 64 20 7a 65 72 6f 2c 20 72 6f 6c 6c 62 61 hed zero, rollba
137ac 63 6b 20 61 6e 79 20 61 63 74 69 76 65 0a 2a 2a ck any active.**
137ad 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 transaction and
137ae 20 75 6e 6c 6f 63 6b 20 74 68 65 20 70 61 67 65 unlock the page
137af 72 2e 0a 2a 2a 0a 2a 2a 20 45 78 63 65 70 74 2c r..**.** Except,
137b0 20 69 6e 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 in locking_mode
137b1 3d 45 58 43 4c 55 53 49 56 45 20 77 68 65 6e 20 =EXCLUSIVE when
137b2 74 68 65 72 65 20 69 73 20 6e 6f 74 68 69 6e 67 there is nothing
137b3 20 74 6f 20 69 6e 0a 2a 2a 20 74 68 65 20 72 6f to in.** the ro
137b4 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2c 20 llback journal,
137b5 74 68 65 20 75 6e 6c 6f 63 6b 20 69 73 20 6e 6f the unlock is no
137b6 74 20 70 65 72 66 6f 72 6d 65 64 20 61 6e 64 20 t performed and
137b7 74 68 65 72 65 20 69 73 0a 2a 2a 20 6e 6f 74 68 there is.** noth
137b8 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 2c ing to rollback,
137b9 20 73 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 so this routine
137ba 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 20 is a no-op..*/
137bb 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 .static void pag
137bc 65 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 65 64 erUnlockIfUnused
137bd 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
137be 0a 20 20 69 66 28 20 28 73 71 6c 69 74 65 33 50 . if( (sqlite3P
137bf 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 cacheRefCount(pP
137c0 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d ager->pPCache)==
137c1 30 29 0a 20 20 20 26 26 20 28 21 70 50 61 67 65 0). && (!pPage
137c2 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 r->exclusiveMode
137c3 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 || pPager->jour
137c4 6e 61 6c 4f 66 66 3e 30 29 20 0a 20 20 29 7b 0a nalOff>0) . ){.
137c5 20 20 20 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 pagerUnlockA
137c6 6e 64 52 6f 6c 6c 62 61 63 6b 28 70 50 61 67 65 ndRollback(pPage
137c7 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a r);. }.}../*.**
137c8 20 41 63 71 75 69 72 65 20 61 20 72 65 66 65 72 Acquire a refer
137c9 65 6e 63 65 20 74 6f 20 70 61 67 65 20 6e 75 6d ence to page num
137ca 62 65 72 20 70 67 6e 6f 20 69 6e 20 70 61 67 65 ber pgno in page
137cb 72 20 70 50 61 67 65 72 20 28 61 20 70 61 67 65 r pPager (a page
137cc 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 20 68 61 .** reference ha
137cd 73 20 74 79 70 65 20 44 62 50 61 67 65 2a 29 2e s type DbPage*).
137ce 20 49 66 20 74 68 65 20 72 65 71 75 65 73 74 65 If the requeste
137cf 64 20 72 65 66 65 72 65 6e 63 65 20 69 73 20 0a d reference is .
137d0 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 ** successfully
137d1 6f 62 74 61 69 6e 65 64 2c 20 69 74 20 69 73 20 obtained, it is
137d2 63 6f 70 69 65 64 20 74 6f 20 2a 70 70 50 61 67 copied to *ppPag
137d3 65 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 e and SQLITE_OK
137d4 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 returned..**.**
137d5 49 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 If the requested
137d6 20 70 61 67 65 20 69 73 20 61 6c 72 65 61 64 79 page is already
137d7 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 69 in the cache, i
137d8 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 0a t is returned. .
137d9 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 61 20 ** Otherwise, a
137da 6e 65 77 20 70 61 67 65 20 6f 62 6a 65 63 74 20 new page object
137db 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 is allocated and
137dc 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 populated with
137dd 64 61 74 61 0a 2a 2a 20 72 65 61 64 20 66 72 6f data.** read fro
137de 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 m the database f
137df 69 6c 65 2e 20 49 6e 20 73 6f 6d 65 20 63 61 73 ile. In some cas
137e0 65 73 2c 20 74 68 65 20 70 63 61 63 68 65 20 6d es, the pcache m
137e1 6f 64 75 6c 65 20 6d 61 79 0a 2a 2a 20 63 68 6f odule may.** cho
137e2 6f 73 65 20 6e 6f 74 20 74 6f 20 61 6c 6c 6f 63 ose not to alloc
137e3 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 20 6f ate a new page o
137e4 62 6a 65 63 74 20 61 6e 64 20 6d 61 79 20 72 65 bject and may re
137e5 75 73 65 20 61 6e 20 65 78 69 73 74 69 6e 67 0a use an existing.
137e6 2a 2a 20 6f 62 6a 65 63 74 20 77 69 74 68 20 6e ** object with n
137e7 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65 o outstanding re
137e8 66 65 72 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 ferences..**.**
137e9 54 68 65 20 65 78 74 72 61 20 64 61 74 61 20 61 The extra data a
137ea 70 70 65 6e 64 65 64 20 74 6f 20 61 20 70 61 67 ppended to a pag
137eb 65 20 69 73 20 61 6c 77 61 79 73 20 69 6e 69 74 e is always init
137ec 69 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72 6f 73 ialized to zeros
137ed 20 74 68 65 20 0a 2a 2a 20 66 69 72 73 74 20 74 the .** first t
137ee 69 6d 65 20 61 20 70 61 67 65 20 69 73 20 6c 6f ime a page is lo
137ef 61 64 65 64 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 aded into memory
137f0 2e 20 49 66 20 74 68 65 20 70 61 67 65 20 72 65 . If the page re
137f1 71 75 65 73 74 65 64 20 69 73 20 0a 2a 2a 20 61 quested is .** a
137f2 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 63 61 lready in the ca
137f3 63 68 65 20 77 68 65 6e 20 74 68 69 73 20 66 75 che when this fu
137f4 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
137f5 2c 20 74 68 65 6e 20 74 68 65 20 65 78 74 72 61 , then the extra
137f6 0a 2a 2a 20 64 61 74 61 20 69 73 20 6c 65 66 74 .** data is left
137f7 20 61 73 20 69 74 20 77 61 73 20 77 68 65 6e 20 as it was when
137f8 74 68 65 20 70 61 67 65 20 6f 62 6a 65 63 74 20 the page object
137f9 77 61 73 20 6c 61 73 74 20 75 73 65 64 2e 0a 2a was last used..*
137fa 2a 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61 *.** If the data
137fb 62 61 73 65 20 69 6d 61 67 65 20 69 73 20 73 6d base image is sm
137fc 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 65 20 72 aller than the r
137fd 65 71 75 65 73 74 65 64 20 70 61 67 65 20 6f 72 equested page or
137fe 20 69 66 20 61 20 0a 2a 2a 20 6e 6f 6e 2d 7a 65 if a .** non-ze
137ff 72 6f 20 76 61 6c 75 65 20 69 73 20 70 61 73 73 ro value is pass
13800 65 64 20 61 73 20 74 68 65 20 6e 6f 43 6f 6e 74 ed as the noCont
13801 65 6e 74 20 70 61 72 61 6d 65 74 65 72 20 61 6e ent parameter an
13802 64 20 74 68 65 20 0a 2a 2a 20 72 65 71 75 65 73 d the .** reques
13803 74 65 64 20 70 61 67 65 20 69 73 20 6e 6f 74 20 ted page is not
13804 61 6c 72 65 61 64 79 20 73 74 6f 72 65 64 20 69 already stored i
13805 6e 20 74 68 65 20 63 61 63 68 65 2c 20 74 68 65 n the cache, the
13806 6e 20 6e 6f 20 0a 2a 2a 20 61 63 74 75 61 6c 20 n no .** actual
13807 64 69 73 6b 20 72 65 61 64 20 6f 63 63 75 72 73 disk read occurs
13808 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 . In this case t
13809 68 65 20 6d 65 6d 6f 72 79 20 69 6d 61 67 65 20 he memory image
1380a 6f 66 20 74 68 65 20 0a 2a 2a 20 70 61 67 65 20 of the .** page
1380b 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 is initialized t
1380c 6f 20 61 6c 6c 20 7a 65 72 6f 73 2e 20 0a 2a 2a o all zeros. .**
1380d 0a 2a 2a 20 49 66 20 6e 6f 43 6f 6e 74 65 6e 74 .** If noContent
1380e 20 69 73 20 74 72 75 65 2c 20 69 74 20 6d 65 61 is true, it mea
1380f 6e 73 20 74 68 61 74 20 77 65 20 64 6f 20 6e 6f ns that we do no
13810 74 20 63 61 72 65 20 61 62 6f 75 74 20 74 68 65 t care about the
13811 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 6f 66 20 contents.** of
13812 74 68 65 20 70 61 67 65 2e 20 54 68 69 73 20 6f the page. This o
13813 63 63 75 72 73 20 69 6e 20 74 77 6f 20 73 65 70 ccurs in two sep
13814 65 72 61 74 65 20 73 63 65 6e 61 72 69 6f 73 3a erate scenarios:
13815 0a 2a 2a 0a 2a 2a 20 20 20 61 29 20 57 68 65 6e .**.** a) When
13816 20 72 65 61 64 69 6e 67 20 61 20 66 72 65 65 2d reading a free-
13817 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 20 66 list leaf page f
13818 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 rom the database
13819 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 62 29 , and.**.** b)
1381a 20 57 68 65 6e 20 61 20 73 61 76 65 70 6f 69 6e When a savepoin
1381b 74 20 69 73 20 62 65 69 6e 67 20 72 6f 6c 6c 65 t is being rolle
1381c 64 20 62 61 63 6b 20 61 6e 64 20 77 65 20 6e 65 d back and we ne
1381d 65 64 20 74 6f 20 6c 6f 61 64 0a 2a 2a 20 20 20 ed to load.**
1381e 20 20 20 61 20 6e 65 77 20 70 61 67 65 20 69 6e a new page in
1381f 74 6f 20 74 68 65 20 63 61 63 68 65 20 74 6f 20 to the cache to
13820 70 6f 70 75 6c 61 74 65 20 77 69 74 68 20 74 68 populate with th
13821 65 20 64 61 74 61 20 72 65 61 64 0a 2a 2a 20 20 e data read.**
13822 20 20 20 20 66 72 6f 6d 20 74 68 65 20 73 61 76 from the sav
13823 65 70 6f 69 6e 74 20 6a 6f 75 72 6e 61 6c 2e 0a epoint journal..
13824 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 43 6f 6e 74 65 **.** If noConte
13825 6e 74 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e nt is true, then
13826 20 74 68 65 20 64 61 74 61 20 72 65 74 75 72 6e the data return
13827 65 64 20 69 73 20 7a 65 72 6f 65 64 20 69 6e 73 ed is zeroed ins
13828 74 65 61 64 20 6f 66 0a 2a 2a 20 62 65 69 6e 67 tead of.** being
13829 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 64 read from the d
1382a 61 74 61 62 61 73 65 2e 20 41 64 64 69 74 69 6f atabase. Additio
1382b 6e 61 6c 6c 79 2c 20 74 68 65 20 62 69 74 73 20 nally, the bits
1382c 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a corresponding.**
1382d 20 74 6f 20 70 67 6e 6f 20 69 6e 20 50 61 67 65 to pgno in Page
1382e 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 28 62 69 r.pInJournal (bi
1382f 74 76 65 63 20 6f 66 20 70 61 67 65 73 20 61 6c tvec of pages al
13830 72 65 61 64 79 20 77 72 69 74 74 65 6e 20 74 6f ready written to
13831 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 the.** journal
13832 66 69 6c 65 29 20 61 6e 64 20 74 68 65 20 50 61 file) and the Pa
13833 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 70 49 6e gerSavepoint.pIn
13834 53 61 76 65 70 6f 69 6e 74 20 62 69 74 76 65 63 Savepoint bitvec
13835 73 20 6f 66 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a s of any open.**
13836 20 73 61 76 65 70 6f 69 6e 74 73 20 61 72 65 20 savepoints are
13837 73 65 74 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 set. This means
13838 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6d if the page is m
13839 61 64 65 20 77 72 69 74 61 62 6c 65 20 61 74 20 ade writable at
1383a 61 6e 79 0a 2a 2a 20 70 6f 69 6e 74 20 69 6e 20 any.** point in
1383b 74 68 65 20 66 75 74 75 72 65 2c 20 75 73 69 6e the future, usin
1383c 67 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 g a call to sqli
1383d 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29 2c te3PagerWrite(),
1383e 20 69 74 73 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a its contents.**
1383f 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 6a 6f 75 will not be jou
13840 72 6e 61 6c 65 64 2e 20 54 68 69 73 20 73 61 76 rnaled. This sav
13841 65 73 20 49 4f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 es IO..**.** The
13842 20 61 63 71 75 69 73 69 74 69 6f 6e 20 6d 69 67 acquisition mig
13843 68 74 20 66 61 69 6c 20 66 6f 72 20 73 65 76 65 ht fail for seve
13844 72 61 6c 20 72 65 61 73 6f 6e 73 2e 20 20 49 6e ral reasons. In
13845 20 61 6c 6c 20 63 61 73 65 73 2c 0a 2a 2a 20 61 all cases,.** a
13846 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 65 72 n appropriate er
13847 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 ror code is retu
13848 72 6e 65 64 20 61 6e 64 20 2a 70 70 50 61 67 65 rned and *ppPage
13849 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e is set to NULL.
1384a 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 .**.** See also
1384b 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b sqlite3PagerLook
1384c 75 70 28 29 2e 20 20 42 6f 74 68 20 74 68 69 73 up(). Both this
1384d 20 72 6f 75 74 69 6e 65 20 61 6e 64 20 4c 6f 6f routine and Loo
1384e 6b 75 70 28 29 20 61 74 74 65 6d 70 74 0a 2a 2a kup() attempt.**
1384f 20 74 6f 20 66 69 6e 64 20 61 20 70 61 67 65 20 to find a page
13850 69 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 in the in-memory
13851 20 63 61 63 68 65 20 66 69 72 73 74 2e 20 20 49 cache first. I
13852 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f f the page is no
13853 74 20 61 6c 72 65 61 64 79 0a 2a 2a 20 69 6e 20 t already.** in
13854 6d 65 6d 6f 72 79 2c 20 74 68 69 73 20 72 6f 75 memory, this rou
13855 74 69 6e 65 20 67 6f 65 73 20 74 6f 20 64 69 73 tine goes to dis
13856 6b 20 74 6f 20 72 65 61 64 20 69 74 20 69 6e 20 k to read it in
13857 77 68 65 72 65 61 73 20 4c 6f 6f 6b 75 70 28 29 whereas Lookup()
13858 0a 2a 2a 20 6a 75 73 74 20 72 65 74 75 72 6e 73 .** just returns
13859 20 30 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 0. This routin
1385a 65 20 61 63 71 75 69 72 65 73 20 61 20 72 65 61 e acquires a rea
1385b 64 2d 6c 6f 63 6b 20 74 68 65 20 66 69 72 73 74 d-lock the first
1385c 20 74 69 6d 65 20 69 74 0a 2a 2a 20 68 61 73 20 time it.** has
1385d 74 6f 20 67 6f 20 74 6f 20 64 69 73 6b 2c 20 61 to go to disk, a
1385e 6e 64 20 63 6f 75 6c 64 20 61 6c 73 6f 20 70 6c nd could also pl
1385f 61 79 62 61 63 6b 20 61 6e 20 6f 6c 64 20 6a 6f ayback an old jo
13860 75 72 6e 61 6c 20 69 66 20 6e 65 63 65 73 73 61 urnal if necessa
13861 72 79 2e 0a 2a 2a 20 53 69 6e 63 65 20 4c 6f 6f ry..** Since Loo
13862 6b 75 70 28 29 20 6e 65 76 65 72 20 67 6f 65 73 kup() never goes
13863 20 74 6f 20 64 69 73 6b 2c 20 69 74 20 6e 65 76 to disk, it nev
13864 65 72 20 68 61 73 20 74 6f 20 64 65 61 6c 20 77 er has to deal w
13865 69 74 68 20 6c 6f 63 6b 73 0a 2a 2a 20 6f 72 20 ith locks.** or
13866 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a journal files..*
13867 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
13868 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
13869 72 41 63 71 75 69 72 65 28 0a 20 20 50 61 67 65 rAcquire(. Page
1386a 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 r *pPager,
1386b 2f 2a 20 54 68 65 20 70 61 67 65 72 20 6f 70 65 /* The pager ope
1386c 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 n on the databas
1386d 65 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f e file */. Pgno
1386e 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 pgno,
1386f 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 74 /* Page number t
13870 6f 20 66 65 74 63 68 20 2a 2f 0a 20 20 44 62 50 o fetch */. DbP
13871 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 20 20 age **ppPage,
13872 20 2f 2a 20 57 72 69 74 65 20 61 20 70 6f 69 6e /* Write a poin
13873 74 65 72 20 74 6f 20 74 68 65 20 70 61 67 65 20 ter to the page
13874 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f here */. int no
13875 43 6f 6e 74 65 6e 74 20 20 20 20 20 20 20 2f 2a Content /*
13876 20 44 6f 20 6e 6f 74 20 62 6f 74 68 65 72 20 72 Do not bother r
13877 65 61 64 69 6e 67 20 63 6f 6e 74 65 6e 74 20 66 eading content f
13878 72 6f 6d 20 64 69 73 6b 20 69 66 20 74 72 75 65 rom disk if true
13879 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b */.){. int rc;
1387a 0a 20 20 50 67 48 64 72 20 2a 70 50 67 3b 0a 0a . PgHdr *pPg;..
1387b 20 20 61 73 73 65 72 74 28 20 61 73 73 65 72 74 assert( assert
1387c 5f 70 61 67 65 72 5f 73 74 61 74 65 28 70 50 61 _pager_state(pPa
1387d 67 65 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 ger) );. assert
1387e 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e ( pPager->state>
1387f 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a PAGER_UNLOCK );.
13880 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20 29 . if( pgno==0 )
13881 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
13882 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
13883 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 ;. }.. /* If t
13884 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 74 he pager is in t
13885 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20 he error state,
13886 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 return an error
13887 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 0a 20 20 immediately. .
13888 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 ** Otherwise, re
13889 71 75 65 73 74 20 74 68 65 20 70 61 67 65 20 66 quest the page f
1388a 72 6f 6d 20 74 68 65 20 50 43 61 63 68 65 20 6c rom the PCache l
1388b 61 79 65 72 2e 20 2a 2f 0a 20 20 69 66 28 20 70 ayer. */. if( p
1388c 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 21 3d Pager->errCode!=
1388d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 SQLITE_OK && pPa
1388e 67 65 72 2d 3e 65 72 72 43 6f 64 65 21 3d 53 51 ger->errCode!=SQ
1388f 4c 49 54 45 5f 46 55 4c 4c 20 29 7b 0a 20 20 20 LITE_FULL ){.
13890 20 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 rc = pPager->er
13891 72 43 6f 64 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a rCode;. }else{.
13892 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
13893 50 63 61 63 68 65 46 65 74 63 68 28 70 50 61 67 PcacheFetch(pPag
13894 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 67 6e er->pPCache, pgn
13895 6f 2c 20 31 2c 20 70 70 50 61 67 65 29 3b 0a 20 o, 1, ppPage);.
13896 20 7d 0a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 }.. if( rc!=SQ
13897 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f LITE_OK ){. /
13898 2a 20 45 69 74 68 65 72 20 74 68 65 20 63 61 6c * Either the cal
13899 6c 20 74 6f 20 73 71 6c 69 74 65 33 50 63 61 63 l to sqlite3Pcac
1389a 68 65 46 65 74 63 68 28 29 20 72 65 74 75 72 6e heFetch() return
1389b 65 64 20 61 6e 20 65 72 72 6f 72 20 6f 72 20 74 ed an error or t
1389c 68 65 0a 20 20 20 20 2a 2a 20 70 61 67 65 72 20 he. ** pager
1389d 77 61 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 was already in t
1389e 68 65 20 65 72 72 6f 72 2d 73 74 61 74 65 20 77 he error-state w
1389f 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f hen this functio
138a0 6e 20 77 61 73 20 63 61 6c 6c 65 64 2e 0a 20 20 n was called..
138a1 20 20 2a 2a 20 53 65 74 20 70 50 67 20 74 6f 20 ** Set pPg to
138a2 30 20 61 6e 64 20 6a 75 6d 70 20 74 6f 20 74 68 0 and jump to th
138a3 65 20 65 78 63 65 70 74 69 6f 6e 20 68 61 6e 64 e exception hand
138a4 6c 65 72 2e 20 20 2a 2f 0a 20 20 20 20 70 50 67 ler. */. pPg
138a5 20 3d 20 30 3b 0a 20 20 20 20 67 6f 74 6f 20 70 = 0;. goto p
138a6 61 67 65 72 5f 61 63 71 75 69 72 65 5f 65 72 72 ager_acquire_err
138a7 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
138a8 28 2a 70 70 50 61 67 65 29 2d 3e 70 67 6e 6f 3d (*ppPage)->pgno=
138a9 3d 70 67 6e 6f 20 29 3b 0a 20 20 61 73 73 65 72 =pgno );. asser
138aa 74 28 20 28 2a 70 70 50 61 67 65 29 2d 3e 70 50 t( (*ppPage)->pP
138ab 61 67 65 72 3d 3d 70 50 61 67 65 72 20 7c 7c 20 ager==pPager ||
138ac 28 2a 70 70 50 61 67 65 29 2d 3e 70 50 61 67 65 (*ppPage)->pPage
138ad 72 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 28 r==0 );.. if( (
138ae 2a 70 70 50 61 67 65 29 2d 3e 70 50 61 67 65 72 *ppPage)->pPager
138af 20 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 20 74 68 ){. /* In th
138b0 69 73 20 63 61 73 65 20 74 68 65 20 70 63 61 63 is case the pcac
138b1 68 65 20 61 6c 72 65 61 64 79 20 63 6f 6e 74 61 he already conta
138b2 69 6e 73 20 61 6e 20 69 6e 69 74 69 61 6c 69 7a ins an initializ
138b3 65 64 20 63 6f 70 79 20 6f 66 0a 20 20 20 20 2a ed copy of. *
138b4 2a 20 74 68 65 20 70 61 67 65 2e 20 52 65 74 75 * the page. Retu
138b5 72 6e 20 77 69 74 68 6f 75 74 20 66 75 72 74 68 rn without furth
138b6 65 72 20 61 64 6f 2e 20 20 2a 2f 0a 20 20 20 20 er ado. */.
138b7 61 73 73 65 72 74 28 20 70 67 6e 6f 3c 3d 50 41 assert( pgno<=PA
138b8 47 45 52 5f 4d 41 58 5f 50 47 4e 4f 20 26 26 20 GER_MAX_PGNO &&
138b9 70 67 6e 6f 21 3d 50 41 47 45 52 5f 4d 4a 5f 50 pgno!=PAGER_MJ_P
138ba 47 4e 4f 28 70 50 61 67 65 72 29 20 29 3b 0a 20 GNO(pPager) );.
138bb 20 20 20 50 41 47 45 52 5f 49 4e 43 52 28 70 50 PAGER_INCR(pP
138bc 61 67 65 72 2d 3e 6e 48 69 74 29 3b 0a 20 20 20 ager->nHit);.
138bd 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
138be 4b 3b 0a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 K;.. }else{.
138bf 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20 63 61 /* The pager ca
138c0 63 68 65 20 68 61 73 20 63 72 65 61 74 65 64 20 che has created
138c1 61 20 6e 65 77 20 70 61 67 65 2e 20 49 74 73 20 a new page. Its
138c2 63 6f 6e 74 65 6e 74 20 6e 65 65 64 73 20 74 6f content needs to
138c3 20 0a 20 20 20 20 2a 2a 20 62 65 20 69 6e 69 74 . ** be init
138c4 69 61 6c 69 7a 65 64 2e 20 20 2a 2f 0a 20 20 20 ialized. */.
138c5 20 69 6e 74 20 6e 4d 61 78 3b 0a 0a 20 20 20 20 int nMax;..
138c6 50 41 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65 PAGER_INCR(pPage
138c7 72 2d 3e 6e 4d 69 73 73 29 3b 0a 20 20 20 20 70 r->nMiss);. p
138c8 50 67 20 3d 20 2a 70 70 50 61 67 65 3b 0a 20 20 Pg = *ppPage;.
138c9 20 20 70 50 67 2d 3e 70 50 61 67 65 72 20 3d 20 pPg->pPager =
138ca 70 50 61 67 65 72 3b 0a 0a 20 20 20 20 2f 2a 20 pPager;.. /*
138cb 54 68 65 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 The maximum page
138cc 20 6e 75 6d 62 65 72 20 69 73 20 32 5e 33 31 2e number is 2^31.
138cd 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 Return SQLITE_C
138ce 4f 52 52 55 50 54 20 69 66 20 61 20 70 61 67 65 ORRUPT if a page
138cf 0a 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 67 . ** number g
138d0 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 69 73 reater than this
138d1 2c 20 6f 72 20 74 68 65 20 75 6e 75 73 65 64 20 , or the unused
138d2 6c 6f 63 6b 69 6e 67 2d 70 61 67 65 2c 20 69 73 locking-page, is
138d3 20 72 65 71 75 65 73 74 65 64 2e 20 2a 2f 0a 20 requested. */.
138d4 20 20 20 69 66 28 20 70 67 6e 6f 3e 50 41 47 45 if( pgno>PAGE
138d5 52 5f 4d 41 58 5f 50 47 4e 4f 20 7c 7c 20 70 67 R_MAX_PGNO || pg
138d6 6e 6f 3d 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e no==PAGER_MJ_PGN
138d7 4f 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 O(pPager) ){.
138d8 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 rc = SQLITE_C
138d9 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
138da 20 20 20 67 6f 74 6f 20 70 61 67 65 72 5f 61 63 goto pager_ac
138db 71 75 69 72 65 5f 65 72 72 3b 0a 20 20 20 20 7d quire_err;. }
138dc 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 .. rc = sqlit
138dd 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 e3PagerPagecount
138de 28 70 50 61 67 65 72 2c 20 26 6e 4d 61 78 29 3b (pPager, &nMax);
138df 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
138e0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
138e1 67 6f 74 6f 20 70 61 67 65 72 5f 61 63 71 75 69 goto pager_acqui
138e2 72 65 5f 65 72 72 3b 0a 20 20 20 20 7d 0a 0a 20 re_err;. }..
138e3 20 20 20 69 66 28 20 6e 4d 61 78 3c 28 69 6e 74 if( nMax<(int
138e4 29 70 67 6e 6f 20 7c 7c 20 4d 45 4d 44 42 20 7c )pgno || MEMDB |
138e5 7c 20 6e 6f 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 | noContent ){.
138e6 20 20 20 20 20 69 66 28 20 70 67 6e 6f 3e 70 50 if( pgno>pP
138e7 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 29 7b 0a ager->mxPgno ){.
138e8 09 72 63 20 3d 20 53 51 4c 49 54 45 5f 46 55 4c .rc = SQLITE_FUL
138e9 4c 3b 0a 09 67 6f 74 6f 20 70 61 67 65 72 5f 61 L;..goto pager_a
138ea 63 71 75 69 72 65 5f 65 72 72 3b 0a 20 20 20 20 cquire_err;.
138eb 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6e 6f }. if( no
138ec 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 20 Content ){.
138ed 20 20 20 2f 2a 20 46 61 69 6c 75 72 65 20 74 6f /* Failure to
138ee 20 73 65 74 20 74 68 65 20 62 69 74 73 20 69 6e set the bits in
138ef 20 74 68 65 20 49 6e 4a 6f 75 72 6e 61 6c 20 62 the InJournal b
138f0 69 74 2d 76 65 63 74 6f 72 73 20 69 73 20 62 65 it-vectors is be
138f1 6e 69 67 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2a nign.. **
138f2 20 49 74 20 6d 65 72 65 6c 79 20 6d 65 61 6e 73 It merely means
138f3 20 74 68 61 74 20 77 65 20 6d 69 67 68 74 20 64 that we might d
138f4 6f 20 73 6f 6d 65 20 65 78 74 72 61 20 77 6f 72 o some extra wor
138f5 6b 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 61 20 0a k to journal a .
138f6 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 ** page
138f7 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65 that does not ne
138f8 65 64 20 74 6f 20 62 65 20 6a 6f 75 72 6e 61 6c ed to be journal
138f9 65 64 2e 20 20 4e 65 76 65 72 74 68 65 6c 65 73 ed. Nevertheles
138fa 73 2c 20 62 65 20 73 75 72 65 20 0a 20 20 20 20 s, be sure .
138fb 20 20 20 20 2a 2a 20 74 6f 20 74 65 73 74 20 74 ** to test t
138fc 68 65 20 63 61 73 65 20 77 68 65 72 65 20 61 20 he case where a
138fd 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 20 6f 63 63 malloc error occ
138fe 75 72 73 20 77 68 69 6c 65 20 74 72 79 69 6e 67 urs while trying
138ff 20 74 6f 20 73 65 74 20 0a 20 20 20 20 20 20 20 to set .
13900 20 2a 2a 20 61 20 62 69 74 20 69 6e 20 61 20 62 ** a bit in a b
13901 69 74 20 76 65 63 74 6f 72 2e 0a 20 20 20 20 20 it vector..
13902 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 */. sq
13903 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e lite3BeginBenign
13904 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 Malloc();.
13905 20 20 69 66 28 20 70 67 6e 6f 3c 3d 70 50 61 67 if( pgno<=pPag
13906 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 er->dbOrigSize )
13907 7b 0a 20 20 20 20 20 20 20 20 20 20 54 45 53 54 {. TEST
13908 4f 4e 4c 59 28 20 72 63 20 3d 20 29 20 73 71 6c ONLY( rc = ) sql
13909 69 74 65 33 42 69 74 76 65 63 53 65 74 28 70 50 ite3BitvecSet(pP
1390a 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c ager->pInJournal
1390b 2c 20 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 , pgno);.
1390c 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63 3d testcase( rc=
1390d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b =SQLITE_NOMEM );
1390e 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1390f 20 20 20 54 45 53 54 4f 4e 4c 59 28 20 72 63 20 TESTONLY( rc
13910 3d 20 29 20 61 64 64 54 6f 53 61 76 65 70 6f 69 = ) addToSavepoi
13911 6e 74 42 69 74 76 65 63 73 28 70 50 61 67 65 72 ntBitvecs(pPager
13912 2c 20 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 , pgno);.
13913 20 74 65 73 74 63 61 73 65 28 20 72 63 3d 3d 53 testcase( rc==S
13914 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 QLITE_NOMEM );.
13915 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 6e sqlite3En
13916 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b dBenignMalloc();
13917 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
13918 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70 50 67 memset(pPg
13919 2d 3e 70 44 61 74 61 2c 20 30 2c 20 70 50 61 67 ->pData, 0, pPag
1391a 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 er->pageSize);.
1391b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 49 4f 54 }. IOT
1391c 52 41 43 45 28 28 22 5a 45 52 4f 20 25 70 20 25 RACE(("ZERO %p %
1391d 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 67 d\n", pPager, pg
1391e 6e 6f 29 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b no));. }else{
1391f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
13920 50 67 2d 3e 70 50 61 67 65 72 3d 3d 70 50 61 67 Pg->pPager==pPag
13921 65 72 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d er );. rc =
13922 20 72 65 61 64 44 62 50 61 67 65 28 70 50 67 29 readDbPage(pPg)
13923 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d ;. if( rc!=
13924 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
13925 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 72 5f goto pager_
13926 61 63 71 75 69 72 65 5f 65 72 72 3b 0a 20 20 20 acquire_err;.
13927 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 64 65 }. }.#ifde
13928 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 f SQLITE_CHECK_P
13929 41 47 45 53 0a 20 20 20 20 70 50 67 2d 3e 70 61 AGES. pPg->pa
1392a 67 65 48 61 73 68 20 3d 20 70 61 67 65 72 5f 70 geHash = pager_p
1392b 61 67 65 68 61 73 68 28 70 50 67 29 3b 0a 23 65 agehash(pPg);.#e
1392c 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 72 65 74 75 ndif. }.. retu
1392d 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 70 rn SQLITE_OK;..p
1392e 61 67 65 72 5f 61 63 71 75 69 72 65 5f 65 72 72 ager_acquire_err
1392f 3a 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 3d :. assert( rc!=
13930 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 69 SQLITE_OK );. i
13931 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 73 71 f( pPg ){. sq
13932 6c 69 74 65 33 50 63 61 63 68 65 44 72 6f 70 28 lite3PcacheDrop(
13933 70 50 67 29 3b 0a 20 20 7d 0a 20 20 70 61 67 65 pPg);. }. page
13934 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 65 64 28 rUnlockIfUnused(
13935 70 50 61 67 65 72 29 3b 0a 0a 20 20 2a 70 70 50 pPager);.. *ppP
13936 61 67 65 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 age = 0;. retur
13937 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 n rc;.}../*.** A
13938 63 71 75 69 72 65 20 61 20 70 61 67 65 20 69 66 cquire a page if
13939 20 69 74 20 69 73 20 61 6c 72 65 61 64 79 20 69 it is already i
1393a 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 n the in-memory
1393b 63 61 63 68 65 2e 20 20 44 6f 0a 2a 2a 20 6e 6f cache. Do.** no
1393c 74 20 72 65 61 64 20 74 68 65 20 70 61 67 65 20 t read the page
1393d 66 72 6f 6d 20 64 69 73 6b 2e 20 20 52 65 74 75 from disk. Retu
1393e 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 rn a pointer to
1393f 74 68 65 20 70 61 67 65 2c 0a 2a 2a 20 6f 72 20 the page,.** or
13940 30 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 0 if the page is
13941 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 2e 20 41 not in cache. A
13942 6c 73 6f 2c 20 72 65 74 75 72 6e 20 30 20 69 66 lso, return 0 if
13943 20 74 68 65 20 0a 2a 2a 20 70 61 67 65 72 20 69 the .** pager i
13944 73 20 69 6e 20 50 41 47 45 52 5f 55 4e 4c 4f 43 s in PAGER_UNLOC
13945 4b 20 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 K state when thi
13946 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
13947 6c 6c 65 64 2c 0a 2a 2a 20 6f 72 20 69 66 20 74 lled,.** or if t
13948 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 61 he pager is in a
13949 6e 20 65 72 72 6f 72 20 73 74 61 74 65 20 6f 74 n error state ot
1394a 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f her than SQLITE_
1394b 46 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 FULL..**.** See
1394c 61 6c 73 6f 20 73 71 6c 69 74 65 33 50 61 67 65 also sqlite3Page
1394d 72 47 65 74 28 29 2e 20 20 54 68 65 20 64 69 66 rGet(). The dif
1394e 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 ference between
1394f 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 this routine.**
13950 61 6e 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 and sqlite3Pager
13951 47 65 74 28 29 20 69 73 20 74 68 61 74 20 5f 67 Get() is that _g
13952 65 74 28 29 20 77 69 6c 6c 20 67 6f 20 74 6f 20 et() will go to
13953 74 68 65 20 64 69 73 6b 20 61 6e 64 20 72 65 61 the disk and rea
13954 64 0a 2a 2a 20 69 6e 20 74 68 65 20 70 61 67 65 d.** in the page
13955 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20 if the page is
13956 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 6e 20 63 not already in c
13957 61 63 68 65 2e 20 20 54 68 69 73 20 72 6f 75 74 ache. This rout
13958 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 73 20 4e ine.** returns N
13959 55 4c 4c 20 69 66 20 74 68 65 20 70 61 67 65 20 ULL if the page
1395a 69 73 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 20 is not in cache
1395b 6f 72 20 69 66 20 61 20 64 69 73 6b 20 49 2f 4f or if a disk I/O
1395c 20 65 72 72 6f 72 20 0a 2a 2a 20 68 61 73 20 65 error .** has e
1395d 76 65 72 20 68 61 70 70 65 6e 65 64 2e 0a 2a 2f ver happened..*/
1395e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1395f 44 62 50 61 67 65 20 2a 73 71 6c 69 74 65 33 50 DbPage *sqlite3P
13960 61 67 65 72 4c 6f 6f 6b 75 70 28 50 61 67 65 72 agerLookup(Pager
13961 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 70 *pPager, Pgno p
13962 67 6e 6f 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 gno){. PgHdr *p
13963 50 67 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 Pg = 0;. assert
13964 28 20 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 ( pPager!=0 );.
13965 20 61 73 73 65 72 74 28 20 70 67 6e 6f 21 3d 30 assert( pgno!=0
13966 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
13967 61 67 65 72 2d 3e 70 50 43 61 63 68 65 21 3d 30 ager->pPCache!=0
13968 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
13969 61 67 65 72 2d 3e 73 74 61 74 65 20 3e 20 50 41 ager->state > PA
1396a 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20 GER_UNLOCK );.
1396b 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 sqlite3PcacheFet
1396c 63 68 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 ch(pPager->pPCac
1396d 68 65 2c 20 70 67 6e 6f 2c 20 30 2c 20 26 70 50 he, pgno, 0, &pP
1396e 67 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 67 g);. return pPg
1396f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 ;.}../*.** Relea
13970 73 65 20 61 20 70 61 67 65 20 72 65 66 65 72 65 se a page refere
13971 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 nce..**.** If th
13972 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 e number of refe
13973 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61 rences to the pa
13974 67 65 20 64 72 6f 70 20 74 6f 20 7a 65 72 6f 2c ge drop to zero,
13975 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 70 61 67 then the.** pag
13976 65 20 69 73 20 61 64 64 65 64 20 74 6f 20 74 68 e is added to th
13977 65 20 4c 52 55 20 6c 69 73 74 2e 20 20 57 68 65 e LRU list. Whe
13978 6e 20 61 6c 6c 20 72 65 66 65 72 65 6e 63 65 73 n all references
13979 20 74 6f 20 61 6c 6c 20 70 61 67 65 73 0a 2a 2a to all pages.**
1397a 20 61 72 65 20 72 65 6c 65 61 73 65 64 2c 20 61 are released, a
1397b 20 72 6f 6c 6c 62 61 63 6b 20 6f 63 63 75 72 73 rollback occurs
1397c 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20 6f 6e and the lock on
1397d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 the database is
1397e 0a 2a 2a 20 72 65 6d 6f 76 65 64 2e 0a 2a 2f 0a .** removed..*/.
1397f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
13980 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 oid sqlite3Pager
13981 55 6e 72 65 66 28 44 62 50 61 67 65 20 2a 70 50 Unref(DbPage *pP
13982 67 29 7b 0a 20 20 69 66 28 20 70 50 67 20 29 7b g){. if( pPg ){
13983 0a 20 20 20 20 50 61 67 65 72 20 2a 70 50 61 67 . Pager *pPag
13984 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 er = pPg->pPager
13985 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 ;. sqlite3Pca
13986 63 68 65 52 65 6c 65 61 73 65 28 70 50 67 29 3b cheRelease(pPg);
13987 0a 20 20 20 20 70 61 67 65 72 55 6e 6c 6f 63 6b . pagerUnlock
13988 49 66 55 6e 75 73 65 64 28 70 50 61 67 65 72 29 IfUnused(pPager)
13989 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 ;. }.}../*.** I
1398a 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e f the main journ
1398b 61 6c 20 66 69 6c 65 20 68 61 73 20 61 6c 72 65 al file has alre
1398c 61 64 79 20 62 65 65 6e 20 6f 70 65 6e 65 64 2c ady been opened,
1398d 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 ensure that the
1398e 0a 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 .** sub-journal
1398f 66 69 6c 65 20 69 73 20 6f 70 65 6e 20 74 6f 6f file is open too
13990 2e 20 49 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f . If the main jo
13991 75 72 6e 61 6c 20 69 73 20 6e 6f 74 20 6f 70 65 urnal is not ope
13992 6e 2c 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 n,.** this funct
13993 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a ion is a no-op..
13994 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 **.** SQLITE_OK
13995 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 65 is returned if e
13996 76 65 72 79 74 68 69 6e 67 20 67 6f 65 73 20 61 verything goes a
13997 63 63 6f 72 64 69 6e 67 20 74 6f 20 70 6c 61 6e ccording to plan
13998 2e 20 0a 2a 2a 20 41 6e 20 53 51 4c 49 54 45 5f . .** An SQLITE_
13999 49 4f 45 52 52 5f 58 58 58 20 65 72 72 6f 72 20 IOERR_XXX error
1399a 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 code is returned
1399b 20 69 66 20 61 20 63 61 6c 6c 20 74 6f 20 0a 2a if a call to .*
1399c 2a 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 * sqlite3OsOpen(
1399d 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 ) fails..*/.stat
1399e 69 63 20 69 6e 74 20 6f 70 65 6e 53 75 62 4a 6f ic int openSubJo
1399f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61 urnal(Pager *pPa
139a0 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d ger){. int rc =
139a1 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 SQLITE_OK;. if
139a2 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d ( isOpen(pPager-
139a3 3e 6a 66 64 29 20 26 26 20 21 69 73 4f 70 65 6e >jfd) && !isOpen
139a4 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 20 29 (pPager->sjfd) )
139a5 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 {. if( pPager
139a6 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 ->journalMode==P
139a7 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
139a8 5f 4d 45 4d 4f 52 59 20 7c 7c 20 70 50 61 67 65 _MEMORY || pPage
139a9 72 2d 3e 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 r->subjInMemory
139aa 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
139ab 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 70 MemJournalOpen(p
139ac 50 61 67 65 72 2d 3e 73 6a 66 64 29 3b 0a 20 20 Pager->sjfd);.
139ad 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 }else{. r
139ae 63 20 3d 20 70 61 67 65 72 4f 70 65 6e 74 65 6d c = pagerOpentem
139af 70 28 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 p(pPager, pPager
139b0 2d 3e 73 6a 66 64 2c 20 53 51 4c 49 54 45 5f 4f ->sjfd, SQLITE_O
139b1 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 29 3b PEN_SUBJOURNAL);
139b2 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
139b3 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
139b4 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
139b5 73 20 63 61 6c 6c 65 64 20 61 74 20 74 68 65 20 s called at the
139b6 73 74 61 72 74 20 6f 66 20 65 76 65 72 79 20 77 start of every w
139b7 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e rite transaction
139b8 2e 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 74 20 ..** There must
139b9 61 6c 72 65 61 64 79 20 62 65 20 61 20 52 45 53 already be a RES
139ba 45 52 56 45 44 20 6f 72 20 45 58 43 4c 55 53 49 ERVED or EXCLUSI
139bb 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 VE lock on the d
139bc 61 74 61 62 61 73 65 20 0a 2a 2a 20 66 69 6c 65 atabase .** file
139bd 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 when this routi
139be 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a ne is called..**
139bf 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 6a 6f 75 .** Open the jou
139c0 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 70 61 rnal file for pa
139c1 67 65 72 20 70 50 61 67 65 72 20 61 6e 64 20 77 ger pPager and w
139c2 72 69 74 65 20 61 20 6a 6f 75 72 6e 61 6c 20 68 rite a journal h
139c3 65 61 64 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 eader.** to the
139c4 73 74 61 72 74 20 6f 66 20 69 74 2e 20 49 66 20 start of it. If
139c5 74 68 65 72 65 20 61 72 65 20 61 63 74 69 76 65 there are active
139c6 20 73 61 76 65 70 6f 69 6e 74 73 2c 20 6f 70 65 savepoints, ope
139c7 6e 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 n the sub-journa
139c8 6c 0a 2a 2a 20 61 73 20 77 65 6c 6c 2e 20 54 68 l.** as well. Th
139c9 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f is function is o
139ca 6e 6c 79 20 75 73 65 64 20 77 68 65 6e 20 74 68 nly used when th
139cb 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 e journal file i
139cc 73 20 62 65 69 6e 67 20 0a 2a 2a 20 6f 70 65 6e s being .** open
139cd 65 64 20 74 6f 20 77 72 69 74 65 20 61 20 72 6f ed to write a ro
139ce 6c 6c 62 61 63 6b 20 6c 6f 67 20 66 6f 72 20 61 llback log for a
139cf 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49 74 transaction. It
139d0 20 69 73 20 6e 6f 74 20 75 73 65 64 20 0a 2a 2a is not used .**
139d1 20 77 68 65 6e 20 6f 70 65 6e 69 6e 67 20 61 20 when opening a
139d2 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 hot journal file
139d3 20 74 6f 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b to roll it back
139d4 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6a ..**.** If the j
139d5 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 61 ournal file is a
139d6 6c 72 65 61 64 79 20 6f 70 65 6e 20 28 61 73 20 lready open (as
139d7 69 74 20 6d 61 79 20 62 65 20 69 6e 20 65 78 63 it may be in exc
139d8 6c 75 73 69 76 65 20 6d 6f 64 65 29 2c 0a 2a 2a lusive mode),.**
139d9 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 then this funct
139da 69 6f 6e 20 6a 75 73 74 20 77 72 69 74 65 73 20 ion just writes
139db 61 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 a journal header
139dc 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 to the start of
139dd 20 74 68 65 0a 2a 2a 20 61 6c 72 65 61 64 79 20 the.** already
139de 6f 70 65 6e 20 66 69 6c 65 2e 20 0a 2a 2a 0a 2a open file. .**.*
139df 2a 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 * Whether or not
139e0 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
139e1 65 20 69 73 20 6f 70 65 6e 65 64 20 62 79 20 74 e is opened by t
139e2 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68 his function, th
139e3 65 0a 2a 2a 20 50 61 67 65 72 2e 70 49 6e 4a 6f e.** Pager.pInJo
139e4 75 72 6e 61 6c 20 62 69 74 76 65 63 20 73 74 72 urnal bitvec str
139e5 75 63 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 ucture is alloca
139e6 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 ted..**.** Retur
139e7 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65 n SQLITE_OK if e
139e8 76 65 72 79 74 68 69 6e 67 20 69 73 20 73 75 63 verything is suc
139e9 63 65 73 73 66 75 6c 2e 20 4f 74 68 65 72 77 69 cessful. Otherwi
139ea 73 65 2c 20 72 65 74 75 72 6e 20 0a 2a 2a 20 53 se, return .** S
139eb 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 74 QLITE_NOMEM if t
139ec 68 65 20 61 74 74 65 6d 70 74 20 74 6f 20 61 6c he attempt to al
139ed 6c 6f 63 61 74 65 20 50 61 67 65 72 2e 70 49 6e locate Pager.pIn
139ee 4a 6f 75 72 6e 61 6c 20 66 61 69 6c 73 2c 20 6f Journal fails, o
139ef 72 20 0a 2a 2a 20 61 6e 20 49 4f 20 65 72 72 6f r .** an IO erro
139f0 72 20 63 6f 64 65 20 69 66 20 6f 70 65 6e 69 6e r code if openin
139f1 67 20 6f 72 20 77 72 69 74 69 6e 67 20 74 68 65 g or writing the
139f2 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 61 journal file fa
139f3 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ils..*/.static i
139f4 6e 74 20 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f nt pager_open_jo
139f5 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61 urnal(Pager *pPa
139f6 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d ger){. int rc =
139f7 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 SQLITE_OK;
139f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
139f9 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 /* Return cod
139fa 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 e */. sqlite3_v
139fb 66 73 20 2a 20 63 6f 6e 73 74 20 70 56 66 73 20 fs * const pVfs
139fc 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 20 = pPager->pVfs;
139fd 20 20 2f 2a 20 4c 6f 63 61 6c 20 63 61 63 68 65 /* Local cache
139fe 20 6f 66 20 76 66 73 20 70 6f 69 6e 74 65 72 20 of vfs pointer
139ff 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 */.. assert( pP
13a00 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 ager->state>=PAG
13a01 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 20 ER_RESERVED );.
13a02 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
13a03 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 >useJournal );.
13a04 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
13a05 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 >journalMode!=PA
13a06 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
13a07 4f 46 46 20 29 3b 0a 20 20 61 73 73 65 72 74 28 OFF );. assert(
13a08 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 pPager->pInJour
13a09 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 0a 20 20 2f nal==0 );. . /
13a0a 2a 20 49 66 20 61 6c 72 65 61 64 79 20 69 6e 20 * If already in
13a0b 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2c the error state,
13a0c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
13a0d 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 42 75 74 20 s a no-op. But
13a0e 6f 6e 0a 20 20 2a 2a 20 74 68 65 20 6f 74 68 65 on. ** the othe
13a0f 72 20 68 61 6e 64 2c 20 74 68 69 73 20 72 6f 75 r hand, this rou
13a10 74 69 6e 65 20 69 73 20 6e 65 76 65 72 20 63 61 tine is never ca
13a11 6c 6c 65 64 20 69 66 20 77 65 20 61 72 65 20 61 lled if we are a
13a12 6c 72 65 61 64 79 20 69 6e 0a 20 20 2a 2a 20 61 lready in. ** a
13a13 6e 20 65 72 72 6f 72 20 73 74 61 74 65 2e 20 2a n error state. *
13a14 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 50 /. if( NEVER(pP
13a15 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 29 20 29 ager->errCode) )
13a16 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e return pPager->
13a17 65 72 72 43 6f 64 65 3b 0a 0a 20 20 2f 2a 20 54 errCode;.. /* T
13a18 4f 44 4f 3a 20 49 73 20 69 74 20 72 65 61 6c 6c ODO: Is it reall
13a19 79 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 67 65 y possible to ge
13a1a 74 20 68 65 72 65 20 77 69 74 68 20 64 62 53 69 t here with dbSi
13a1b 7a 65 56 61 6c 69 64 3d 3d 30 3f 20 49 66 20 6e zeValid==0? If n
13a1c 6f 74 2c 0a 20 20 2a 2a 20 74 68 65 20 63 61 6c ot,. ** the cal
13a1d 6c 20 74 6f 20 50 61 67 65 72 50 61 67 65 63 6f l to PagerPageco
13a1e 75 6e 74 28 29 20 63 61 6e 20 62 65 20 72 65 6d unt() can be rem
13a1f 6f 76 65 64 2e 0a 20 20 2a 2f 0a 20 20 74 65 73 oved.. */. tes
13a20 74 63 61 73 65 28 20 70 50 61 67 65 72 2d 3e 64 tcase( pPager->d
13a21 62 53 69 7a 65 56 61 6c 69 64 3d 3d 30 20 29 3b bSizeValid==0 );
13a22 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 . sqlite3PagerP
13a23 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c agecount(pPager,
13a24 20 30 29 3b 0a 0a 20 20 70 50 61 67 65 72 2d 3e 0);.. pPager->
13a25 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 73 71 6c pInJournal = sql
13a26 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65 ite3BitvecCreate
13a27 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 (pPager->dbSize)
13a28 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e ;. if( pPager->
13a29 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 7b pInJournal==0 ){
13a2a 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
13a2b 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 TE_NOMEM;. }..
13a2c 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 6a 6f 75 /* Open the jou
13a2d 72 6e 61 6c 20 66 69 6c 65 20 69 66 20 69 74 20 rnal file if it
13a2e 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f is not already o
13a2f 70 65 6e 2e 20 2a 2f 0a 20 20 69 66 28 20 21 69 pen. */. if( !i
13a30 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 sOpen(pPager->jf
13a31 64 29 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 d) ){. if( pP
13a32 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 ager->journalMod
13a33 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c e==PAGER_JOURNAL
13a34 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20 MODE_MEMORY ){.
13a35 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4a sqlite3MemJ
13a36 6f 75 72 6e 61 6c 4f 70 65 6e 28 70 50 61 67 65 ournalOpen(pPage
13a37 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 7d 65 6c r->jfd);. }el
13a38 73 65 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 se{. const
13a39 69 6e 74 20 66 6c 61 67 73 20 3d 20 20 20 20 20 int flags =
13a3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
13a3b 20 56 46 53 20 66 6c 61 67 73 20 74 6f 20 6f 70 VFS flags to op
13a3c 65 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 en journal file
13a3d 2a 2f 0a 20 20 20 20 20 20 20 20 53 51 4c 49 54 */. SQLIT
13a3e 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 E_OPEN_READWRITE
13a3f 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 |SQLITE_OPEN_CRE
13a40 41 54 45 7c 0a 20 20 20 20 20 20 20 20 28 70 50 ATE|. (pP
13a41 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 3f ager->tempFile ?
13a42 20 0a 20 20 20 20 20 20 20 20 20 20 28 53 51 4c . (SQL
13a43 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f ITE_OPEN_DELETEO
13a44 4e 43 4c 4f 53 45 7c 53 51 4c 49 54 45 5f 4f 50 NCLOSE|SQLITE_OP
13a45 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 29 EN_TEMP_JOURNAL)
13a46 3a 0a 20 20 20 20 20 20 20 20 20 20 28 53 51 4c :. (SQL
13a47 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f ITE_OPEN_MAIN_JO
13a48 55 52 4e 41 4c 29 0a 20 20 20 20 20 20 20 20 29 URNAL). )
13a49 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
13a4a 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 ENABLE_ATOMIC_WR
13a4b 49 54 45 0a 20 20 20 20 20 20 72 63 20 3d 20 73 ITE. rc = s
13a4c 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4f 70 65 qlite3JournalOpe
13a4d 6e 28 0a 20 20 20 20 20 20 20 20 20 20 70 56 66 n(. pVf
13a4e 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 s, pPager->zJour
13a4f 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64 nal, pPager->jfd
13a50 2c 20 66 6c 61 67 73 2c 20 6a 72 6e 6c 42 75 66 , flags, jrnlBuf
13a51 66 65 72 53 69 7a 65 28 70 50 61 67 65 72 29 0a ferSize(pPager).
13a52 20 20 20 20 20 20 29 3b 0a 23 65 6c 73 65 0a 20 );.#else.
13a53 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
13a54 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 50 3OsOpen(pVfs, pP
13a55 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 ager->zJournal,
13a56 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66 6c 61 pPager->jfd, fla
13a57 67 73 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20 gs, 0);.#endif.
13a58 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 }. assert(
13a59 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c rc!=SQLITE_OK |
13a5a 7c 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d | isOpen(pPager-
13a5b 3e 6a 66 64 29 20 29 3b 0a 20 20 7d 0a 0a 0a 20 >jfd) );. }...
13a5c 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 66 69 /* Write the fi
13a5d 72 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 rst journal head
13a5e 65 72 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 er to the journa
13a5f 6c 20 66 69 6c 65 20 61 6e 64 20 6f 70 65 6e 20 l file and open
13a60 0a 20 20 2a 2a 20 74 68 65 20 73 75 62 2d 6a 6f . ** the sub-jo
13a61 75 72 6e 61 6c 20 69 66 20 6e 65 63 65 73 73 61 urnal if necessa
13a62 72 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 ry.. */. if( r
13a63 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
13a64 20 20 20 20 2f 2a 20 54 4f 44 4f 3a 20 43 68 65 /* TODO: Che
13a65 63 6b 20 69 66 20 61 6c 6c 20 6f 66 20 74 68 65 ck if all of the
13a66 73 65 20 61 72 65 20 72 65 61 6c 6c 79 20 72 65 se are really re
13a67 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 20 20 70 quired. */. p
13a68 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a Pager->dbOrigSiz
13a69 65 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69 e = pPager->dbSi
13a6a 7a 65 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e ze;. pPager->
13a6b 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d journalStarted =
13a6c 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 0;. pPager->
13a6d 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20 needSync = 0;.
13a6e 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d pPager->nRec =
13a6f 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 0;. pPager->
13a70 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a journalOff = 0;.
13a71 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 74 4d pPager->setM
13a72 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 70 aster = 0;. p
13a73 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 Pager->journalHd
13a74 72 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 r = 0;. rc =
13a75 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 writeJournalHdr(
13a76 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 69 pPager);. }. i
13a77 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
13a78 20 26 26 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 && pPager->nSav
13a79 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 72 63 epoint ){. rc
13a7a 20 3d 20 6f 70 65 6e 53 75 62 4a 6f 75 72 6e 61 = openSubJourna
13a7b 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a l(pPager);. }..
13a7c 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
13a7d 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 _OK ){. sqlit
13a7e 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 e3BitvecDestroy(
13a7f 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e pPager->pInJourn
13a80 61 6c 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d al);. pPager-
13a81 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b >pInJournal = 0;
13a82 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
13a83 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65 67 69 6e ;.}../*.** Begin
13a84 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 a write-transac
13a85 74 69 6f 6e 20 6f 6e 20 74 68 65 20 73 70 65 63 tion on the spec
13a86 69 66 69 65 64 20 70 61 67 65 72 20 6f 62 6a 65 ified pager obje
13a87 63 74 2e 20 49 66 20 61 20 0a 2a 2a 20 77 72 69 ct. If a .** wri
13a88 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 te-transaction h
13a89 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 as already been
13a8a 6f 70 65 6e 65 64 2c 20 74 68 69 73 20 66 75 6e opened, this fun
13a8b 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 ction is a no-op
13a8c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 ..**.** If the e
13a8d 78 46 6c 61 67 20 61 72 67 75 6d 65 6e 74 20 69 xFlag argument i
13a8e 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 61 63 s false, then ac
13a8f 71 75 69 72 65 20 61 74 20 6c 65 61 73 74 20 61 quire at least a
13a90 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 6c 6f 63 RESERVED.** loc
13a91 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 k on the databas
13a92 65 20 66 69 6c 65 2e 20 49 66 20 65 78 46 6c 61 e file. If exFla
13a93 67 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 g is true, then
13a94 61 63 71 75 69 72 65 20 61 74 20 6c 65 61 73 74 acquire at least
13a95 0a 2a 2a 20 61 6e 20 45 58 43 4c 55 53 49 56 45 .** an EXCLUSIVE
13a96 20 6c 6f 63 6b 2e 20 49 66 20 73 75 63 68 20 61 lock. If such a
13a97 20 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 64 79 lock is already
13a98 20 68 65 6c 64 2c 20 6e 6f 20 6c 6f 63 6b 69 6e held, no lockin
13a99 67 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 g .** functions
13a9a 6e 65 65 64 20 62 65 20 63 61 6c 6c 65 64 2e 0a need be called..
13a9b 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 69 73 **.** If this is
13a9c 20 6e 6f 74 20 61 20 74 65 6d 70 6f 72 61 72 79 not a temporary
13a9d 20 6f 72 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 or in-memory fi
13a9e 6c 65 20 61 6e 64 2c 20 74 68 65 20 6a 6f 75 72 le and, the jour
13a9f 6e 61 6c 20 66 69 6c 65 20 69 73 20 0a 2a 2a 20 nal file is .**
13aa0 6f 70 65 6e 65 64 20 69 66 20 69 74 20 68 61 73 opened if it has
13aa1 20 6e 6f 74 20 62 65 65 6e 20 61 6c 72 65 61 64 not been alread
13aa2 79 2e 20 46 6f 72 20 61 20 74 65 6d 70 6f 72 61 y. For a tempora
13aa3 72 79 20 66 69 6c 65 2c 20 74 68 65 20 6f 70 65 ry file, the ope
13aa4 6e 69 6e 67 20 0a 2a 2a 20 6f 66 20 74 68 65 20 ning .** of the
13aa5 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 journal file is
13aa6 64 65 66 65 72 72 65 64 20 75 6e 74 69 6c 20 74 deferred until t
13aa7 68 65 72 65 20 69 73 20 61 6e 20 61 63 74 75 61 here is an actua
13aa8 6c 20 6e 65 65 64 20 74 6f 20 0a 2a 2a 20 77 72 l need to .** wr
13aa9 69 74 65 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e ite to the journ
13aaa 61 6c 2e 20 54 4f 44 4f 3a 20 57 68 79 20 68 61 al. TODO: Why ha
13aab 6e 64 6c 65 20 74 65 6d 70 6f 72 61 72 79 20 66 ndle temporary f
13aac 69 6c 65 73 20 64 69 66 66 65 72 65 6e 74 6c 79 iles differently
13aad 3f 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6a ?.**.** If the j
13aae 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f ournal file is o
13aaf 70 65 6e 65 64 20 28 6f 72 20 69 66 20 69 74 20 pened (or if it
13ab0 69 73 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 29 is already open)
13ab1 2c 20 74 68 65 6e 20 61 0a 2a 2a 20 6a 6f 75 72 , then a.** jour
13ab2 6e 61 6c 2d 68 65 61 64 65 72 20 69 73 20 77 72 nal-header is wr
13ab3 69 74 74 65 6e 20 74 6f 20 74 68 65 20 73 74 61 itten to the sta
13ab4 72 74 20 6f 66 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 rt of it..**.**
13ab5 49 66 20 74 68 65 20 73 75 62 6a 49 6e 4d 65 6d If the subjInMem
13ab6 6f 72 79 20 61 72 67 75 6d 65 6e 74 20 69 73 20 ory argument is
13ab7 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 non-zero, then a
13ab8 6e 79 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 6f ny sub-journal o
13ab9 70 65 6e 65 64 0a 2a 2a 20 77 69 74 68 69 6e 20 pened.** within
13aba 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e this transaction
13abb 20 77 69 6c 6c 20 62 65 20 6f 70 65 6e 65 64 20 will be opened
13abc 61 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 as an in-memory
13abd 66 69 6c 65 2e 20 54 68 69 73 0a 2a 2a 20 68 61 file. This.** ha
13abe 73 20 6e 6f 20 65 66 66 65 63 74 20 69 66 20 74 s no effect if t
13abf 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 he sub-journal i
13ac0 73 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 65 64 s already opened
13ac1 20 28 61 73 20 69 74 20 6d 61 79 20 62 65 20 77 (as it may be w
13ac2 68 65 6e 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 69 hen.** running i
13ac3 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 n exclusive mode
13ac4 29 20 6f 72 20 69 66 20 74 68 65 20 74 72 61 6e ) or if the tran
13ac5 73 61 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 saction does not
13ac6 20 72 65 71 75 69 72 65 20 61 0a 2a 2a 20 73 75 require a.** su
13ac7 62 2d 6a 6f 75 72 6e 61 6c 2e 20 49 66 20 74 68 b-journal. If th
13ac8 65 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 61 e subjInMemory a
13ac9 72 67 75 6d 65 6e 74 20 69 73 20 7a 65 72 6f 2c rgument is zero,
13aca 20 74 68 65 6e 20 61 6e 79 20 72 65 71 75 69 72 then any requir
13acb 65 64 0a 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 ed.** sub-journa
13acc 6c 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 l is implemented
13acd 20 69 6e 2d 6d 65 6d 6f 72 79 20 69 66 20 70 50 in-memory if pP
13ace 61 67 65 72 20 69 73 20 61 6e 20 69 6e 2d 6d 65 ager is an in-me
13acf 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c 20 0a mory database, .
13ad0 2a 2a 20 6f 72 20 75 73 69 6e 67 20 61 20 74 65 ** or using a te
13ad1 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6f 74 68 mporary file oth
13ad2 65 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 erwise..*/.SQLIT
13ad3 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
13ad4 6c 69 74 65 33 50 61 67 65 72 42 65 67 69 6e 28 lite3PagerBegin(
13ad5 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 Pager *pPager, i
13ad6 6e 74 20 65 78 46 6c 61 67 2c 20 69 6e 74 20 73 nt exFlag, int s
13ad7 75 62 6a 49 6e 4d 65 6d 6f 72 79 29 7b 0a 20 20 ubjInMemory){.
13ad8 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
13ad9 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 OK;. assert( pP
13ada 61 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 ager->state!=PAG
13adb 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20 70 ER_UNLOCK );. p
13adc 50 61 67 65 72 2d 3e 73 75 62 6a 49 6e 4d 65 6d Pager->subjInMem
13add 6f 72 79 20 3d 20 28 75 38 29 73 75 62 6a 49 6e ory = (u8)subjIn
13ade 4d 65 6d 6f 72 79 3b 0a 20 20 69 66 28 20 70 50 Memory;. if( pP
13adf 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 ager->state==PAG
13ae0 45 52 5f 53 48 41 52 45 44 20 29 7b 0a 20 20 20 ER_SHARED ){.
13ae1 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
13ae2 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 >pInJournal==0 )
13ae3 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 4d ;. assert( !M
13ae4 45 4d 44 42 20 26 26 20 21 70 50 61 67 65 72 2d EMDB && !pPager-
13ae5 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 0a 20 20 >tempFile );..
13ae6 20 20 2f 2a 20 4f 62 74 61 69 6e 20 61 20 52 45 /* Obtain a RE
13ae7 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 SERVED lock on t
13ae8 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
13ae9 2e 20 49 66 20 74 68 65 20 65 78 46 6c 61 67 20 . If the exFlag
13aea 70 61 72 61 6d 65 74 65 72 0a 20 20 20 20 2a 2a parameter. **
13aeb 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 69 is true, then i
13aec 6d 6d 65 64 69 61 74 65 6c 79 20 75 70 67 72 61 mmediately upgra
13aed 64 65 20 74 68 69 73 20 74 6f 20 61 6e 20 45 58 de this to an EX
13aee 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 54 68 CLUSIVE lock. Th
13aef 65 0a 20 20 20 20 2a 2a 20 62 75 73 79 2d 68 61 e. ** busy-ha
13af0 6e 64 6c 65 72 20 63 61 6c 6c 62 61 63 6b 20 63 ndler callback c
13af1 61 6e 20 62 65 20 75 73 65 64 20 77 68 65 6e 20 an be used when
13af2 75 70 67 72 61 64 69 6e 67 20 74 6f 20 74 68 65 upgrading to the
13af3 20 45 58 43 4c 55 53 49 56 45 0a 20 20 20 20 2a EXCLUSIVE. *
13af4 2a 20 6c 6f 63 6b 2c 20 62 75 74 20 6e 6f 74 20 * lock, but not
13af5 77 68 65 6e 20 6f 62 74 61 69 6e 69 6e 67 20 74 when obtaining t
13af6 68 65 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b he RESERVED lock
13af7 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 .. */. rc
13af8 3d 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 = sqlite3OsLock(
13af9 70 50 61 67 65 72 2d 3e 66 64 2c 20 52 45 53 45 pPager->fd, RESE
13afa 52 56 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 RVED_LOCK);.
13afb 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
13afc 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 K ){. pPage
13afd 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 r->state = PAGER
13afe 5f 52 45 53 45 52 56 45 44 3b 0a 20 20 20 20 20 _RESERVED;.
13aff 20 69 66 28 20 65 78 46 6c 61 67 20 29 7b 0a 20 if( exFlag ){.
13b00 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 rc = page
13b01 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 r_wait_on_lock(p
13b02 50 61 67 65 72 2c 20 45 58 43 4c 55 53 49 56 45 Pager, EXCLUSIVE
13b03 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 7d 0a _LOCK);. }.
13b04 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 }.. /* If
13b05 20 74 68 65 20 72 65 71 75 69 72 65 64 20 6c 6f the required lo
13b06 63 6b 73 20 77 65 72 65 20 73 75 63 63 65 73 73 cks were success
13b07 66 75 6c 6c 79 20 6f 62 74 61 69 6e 65 64 2c 20 fully obtained,
13b08 6f 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c open the journal
13b09 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 61 6e 64 . ** file and
13b0a 20 77 72 69 74 65 20 74 68 65 20 66 69 72 73 74 write the first
13b0b 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 journal-header
13b0c 74 6f 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 to it.. */.
13b0d 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
13b0e 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 6a _OK && pPager->j
13b0f 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 ournalMode!=PAGE
13b10 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 R_JOURNALMODE_OF
13b11 46 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 F ){. rc =
13b12 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e pager_open_journ
13b13 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 al(pPager);.
13b14 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69 73 }. }else if( is
13b15 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 Open(pPager->jfd
13b16 29 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 ) && pPager->jou
13b17 72 6e 61 6c 4f 66 66 3d 3d 30 20 29 7b 0a 20 20 rnalOff==0 ){.
13b18 20 20 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e /* This happen
13b19 73 20 77 68 65 6e 20 74 68 65 20 70 61 67 65 72 s when the pager
13b1a 20 77 61 73 20 69 6e 20 65 78 63 6c 75 73 69 76 was in exclusiv
13b1b 65 2d 61 63 63 65 73 73 20 6d 6f 64 65 20 74 68 e-access mode th
13b1c 65 20 6c 61 73 74 0a 20 20 20 20 2a 2a 20 74 69 e last. ** ti
13b1d 6d 65 20 61 20 28 72 65 61 64 20 6f 72 20 77 72 me a (read or wr
13b1e 69 74 65 29 20 74 72 61 6e 73 61 63 74 69 6f 6e ite) transaction
13b1f 20 77 61 73 20 73 75 63 63 65 73 73 66 75 6c 6c was successfull
13b20 79 20 63 6f 6e 63 6c 75 64 65 64 0a 20 20 20 20 y concluded.
13b21 2a 2a 20 62 79 20 74 68 69 73 20 63 6f 6e 6e 65 ** by this conne
13b22 63 74 69 6f 6e 2e 20 49 6e 73 74 65 61 64 20 6f ction. Instead o
13b23 66 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 6a f deleting the j
13b24 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 74 20 77 ournal file it w
13b25 61 73 20 0a 20 20 20 20 2a 2a 20 6b 65 70 74 20 as . ** kept
13b26 6f 70 65 6e 20 61 6e 64 20 65 69 74 68 65 72 20 open and either
13b27 77 61 73 20 74 72 75 6e 63 61 74 65 64 20 74 6f was truncated to
13b28 20 30 20 62 79 74 65 73 20 6f 72 20 69 74 73 20 0 bytes or its
13b29 68 65 61 64 65 72 20 77 61 73 0a 20 20 20 20 2a header was. *
13b2a 2a 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 69 * overwritten wi
13b2b 74 68 20 7a 65 72 6f 73 2e 0a 20 20 20 20 2a 2f th zeros.. */
13b2c 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
13b2d 67 65 72 2d 3e 6e 52 65 63 3d 3d 30 20 29 3b 0a ger->nRec==0 );.
13b2e 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
13b2f 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 3d 3d er->dbOrigSize==
13b30 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 0 );. assert(
13b31 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 pPager->pInJour
13b32 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 72 63 nal==0 );. rc
13b33 20 3d 20 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f = pager_open_jo
13b34 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 urnal(pPager);.
13b35 20 7d 0a 0a 20 20 50 41 47 45 52 54 52 41 43 45 }.. PAGERTRACE
13b36 28 28 22 54 52 41 4e 53 41 43 54 49 4f 4e 20 25 (("TRANSACTION %
13b37 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 d\n", PAGERID(pP
13b38 61 67 65 72 29 29 29 3b 0a 20 20 61 73 73 65 72 ager)));. asser
13b39 74 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 t( !isOpen(pPage
13b3a 72 2d 3e 6a 66 64 29 20 7c 7c 20 70 50 61 67 65 r->jfd) || pPage
13b3b 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e 30 20 r->journalOff>0
13b3c 7c 7c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b || rc!=SQLITE_OK
13b3d 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 );. if( rc!=SQ
13b3e 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 LITE_OK ){. a
13b3f 73 73 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e ssert( !pPager->
13b40 64 62 4d 6f 64 69 66 69 65 64 20 29 3b 0a 20 20 dbModified );.
13b41 20 20 2f 2a 20 49 67 6e 6f 72 65 20 61 6e 79 20 /* Ignore any
13b42 49 4f 20 65 72 72 6f 72 20 74 68 61 74 20 6f 63 IO error that oc
13b43 63 75 72 73 20 77 69 74 68 69 6e 20 70 61 67 65 curs within page
13b44 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f r_end_transactio
13b45 6e 28 29 2e 20 54 68 65 0a 20 20 20 20 2a 2a 20 n(). The. **
13b46 70 75 72 70 6f 73 65 20 6f 66 20 74 68 69 73 20 purpose of this
13b47 63 61 6c 6c 20 69 73 20 74 6f 20 72 65 73 65 74 call is to reset
13b48 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 74 the internal st
13b49 61 74 65 20 6f 66 20 74 68 65 20 70 61 67 65 72 ate of the pager
13b4a 0a 20 20 20 20 2a 2a 20 73 75 62 2d 73 79 73 74 . ** sub-syst
13b4b 65 6d 2e 20 49 74 20 64 6f 65 73 6e 27 74 20 6d em. It doesn't m
13b4c 61 74 74 65 72 20 69 66 20 74 68 65 20 6a 6f 75 atter if the jou
13b4d 72 6e 61 6c 2d 66 69 6c 65 20 69 73 20 6e 6f 74 rnal-file is not
13b4e 20 70 72 6f 70 65 72 6c 79 0a 20 20 20 20 2a 2a properly. **
13b4f 20 66 69 6e 61 6c 69 7a 65 64 20 61 74 20 74 68 finalized at th
13b50 69 73 20 70 6f 69 6e 74 20 28 73 69 6e 63 65 20 is point (since
13b51 69 74 20 69 73 20 6e 6f 74 20 61 20 76 61 6c 69 it is not a vali
13b52 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 d journal file a
13b53 6e 79 77 61 79 29 2e 0a 20 20 20 20 2a 2f 0a 20 nyway).. */.
13b54 20 20 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 pager_end_tra
13b55 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c nsaction(pPager,
13b56 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 0);. }. retur
13b57 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d n rc;.}../*.** M
13b58 61 72 6b 20 61 20 73 69 6e 67 6c 65 20 64 61 74 ark a single dat
13b59 61 20 70 61 67 65 20 61 73 20 77 72 69 74 65 61 a page as writea
13b5a 62 6c 65 2e 20 54 68 65 20 70 61 67 65 20 69 73 ble. The page is
13b5b 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 written into th
13b5c 65 20 0a 2a 2a 20 6d 61 69 6e 20 6a 6f 75 72 6e e .** main journ
13b5d 61 6c 20 6f 72 20 73 75 62 2d 6a 6f 75 72 6e 61 al or sub-journa
13b5e 6c 20 61 73 20 72 65 71 75 69 72 65 64 2e 20 49 l as required. I
13b5f 66 20 74 68 65 20 70 61 67 65 20 69 73 20 77 72 f the page is wr
13b60 69 74 74 65 6e 20 69 6e 74 6f 0a 2a 2a 20 6f 6e itten into.** on
13b61 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c e of the journal
13b62 73 2c 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e s, the correspon
13b63 64 69 6e 67 20 62 69 74 20 69 73 20 73 65 74 20 ding bit is set
13b64 69 6e 20 74 68 65 20 0a 2a 2a 20 50 61 67 65 72 in the .** Pager
13b65 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 62 69 74 76 .pInJournal bitv
13b66 65 63 20 61 6e 64 20 74 68 65 20 50 61 67 65 72 ec and the Pager
13b67 53 61 76 65 70 6f 69 6e 74 2e 70 49 6e 53 61 76 Savepoint.pInSav
13b68 65 70 6f 69 6e 74 20 62 69 74 76 65 63 73 0a 2a epoint bitvecs.*
13b69 2a 20 6f 66 20 61 6e 79 20 6f 70 65 6e 20 73 61 * of any open sa
13b6a 76 65 70 6f 69 6e 74 73 20 61 73 20 61 70 70 72 vepoints as appr
13b6b 6f 70 72 69 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 opriate..*/.stat
13b6c 69 63 20 69 6e 74 20 70 61 67 65 72 5f 77 72 69 ic int pager_wri
13b6d 74 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a te(PgHdr *pPg){.
13b6e 20 20 76 6f 69 64 20 2a 70 44 61 74 61 20 3d 20 void *pData =
13b6f 70 50 67 2d 3e 70 44 61 74 61 3b 0a 20 20 50 61 pPg->pData;. Pa
13b70 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 ger *pPager = pP
13b71 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 69 6e 74 g->pPager;. int
13b72 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
13b73 0a 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 .. /* This rout
13b74 69 6e 65 20 69 73 20 6e 6f 74 20 63 61 6c 6c 65 ine is not calle
13b75 64 20 75 6e 6c 65 73 73 20 61 20 74 72 61 6e 73 d unless a trans
13b76 61 63 74 69 6f 6e 20 68 61 73 20 61 6c 72 65 61 action has alrea
13b77 64 79 20 62 65 65 6e 0a 20 20 2a 2a 20 73 74 61 dy been. ** sta
13b78 72 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 rted.. */. ass
13b79 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 ert( pPager->sta
13b7a 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 56 te>=PAGER_RESERV
13b7b 45 44 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 ED );.. /* If a
13b7c 6e 20 65 72 72 6f 72 20 68 61 73 20 62 65 65 6e n error has been
13b7d 20 70 72 65 76 69 6f 75 73 6c 79 20 64 65 74 65 previously dete
13b7e 63 74 65 64 2c 20 77 65 20 73 68 6f 75 6c 64 20 cted, we should
13b7f 6e 6f 74 20 62 65 0a 20 20 2a 2a 20 63 61 6c 6c not be. ** call
13b80 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 ing this routine
13b81 2e 20 20 52 65 70 65 61 74 20 74 68 65 20 65 72 . Repeat the er
13b82 72 6f 72 20 66 6f 72 20 72 6f 62 75 73 74 6e 65 ror for robustne
13b83 73 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4e ss.. */. if( N
13b84 45 56 45 52 28 70 50 61 67 65 72 2d 3e 65 72 72 EVER(pPager->err
13b85 43 6f 64 65 29 20 29 20 20 72 65 74 75 72 6e 20 Code) ) return
13b86 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b pPager->errCode;
13b87 0a 0a 20 20 2f 2a 20 48 69 67 68 65 72 2d 6c 65 .. /* Higher-le
13b88 76 65 6c 20 72 6f 75 74 69 6e 65 73 20 6e 65 76 vel routines nev
13b89 65 72 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e er call this fun
13b8a 63 74 69 6f 6e 20 69 66 20 64 61 74 61 62 61 73 ction if databas
13b8b 65 20 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 77 72 e is not. ** wr
13b8c 69 74 61 62 6c 65 2e 20 20 42 75 74 20 63 68 65 itable. But che
13b8d 63 6b 20 61 6e 79 77 61 79 2c 20 6a 75 73 74 20 ck anyway, just
13b8e 66 6f 72 20 72 6f 62 75 73 74 6e 65 73 73 2e 20 for robustness.
13b8f 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 */. if( NEVER(p
13b90 50 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 29 Pager->readOnly)
13b91 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ) return SQLITE
13b92 5f 50 45 52 4d 3b 0a 0a 20 20 61 73 73 65 72 74 _PERM;.. assert
13b93 28 20 21 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 ( !pPager->setMa
13b94 73 74 65 72 20 29 3b 0a 0a 20 20 43 48 45 43 4b ster );.. CHECK
13b95 5f 50 41 47 45 28 70 50 67 29 3b 0a 0a 20 20 2f _PAGE(pPg);.. /
13b96 2a 20 4d 61 72 6b 20 74 68 65 20 70 61 67 65 20 * Mark the page
13b97 61 73 20 64 69 72 74 79 2e 20 20 49 66 20 74 68 as dirty. If th
13b98 65 20 70 61 67 65 20 68 61 73 20 61 6c 72 65 61 e page has alrea
13b99 64 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e 0a dy been written.
13b9a 20 20 2a 2a 20 74 6f 20 74 68 65 20 6a 6f 75 72 ** to the jour
13b9b 6e 61 6c 20 74 68 65 6e 20 77 65 20 63 61 6e 20 nal then we can
13b9c 72 65 74 75 72 6e 20 72 69 67 68 74 20 61 77 61 return right awa
13b9d 79 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 y.. */. sqlite
13b9e 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79 3PcacheMakeDirty
13b9f 28 70 50 67 29 3b 0a 20 20 69 66 28 20 70 61 67 (pPg);. if( pag
13ba0 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20 eInJournal(pPg)
13ba1 26 26 20 21 73 75 62 6a 52 65 71 75 69 72 65 73 && !subjRequires
13ba2 50 61 67 65 28 70 50 67 29 20 29 7b 0a 20 20 20 Page(pPg) ){.
13ba3 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 pPager->dbModif
13ba4 69 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 ied = 1;. }else
13ba5 7b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 {.. /* If we
13ba6 67 65 74 20 74 68 69 73 20 66 61 72 2c 20 69 74 get this far, it
13ba7 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 means that the
13ba8 70 61 67 65 20 6e 65 65 64 73 20 74 6f 20 62 65 page needs to be
13ba9 0a 20 20 20 20 2a 2a 20 77 72 69 74 74 65 6e 20 . ** written
13baa 74 6f 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 to the transacti
13bab 6f 6e 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 74 68 on journal or th
13bac 65 20 63 6b 65 63 6b 70 6f 69 6e 74 20 6a 6f 75 e ckeckpoint jou
13bad 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 6f 72 20 62 rnal. ** or b
13bae 6f 74 68 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 oth.. **.
13baf 2a 2a 20 48 69 67 68 65 72 20 6c 65 76 65 6c 20 ** Higher level
13bb0 72 6f 75 74 69 6e 65 73 20 73 68 6f 75 6c 64 20 routines should
13bb1 68 61 76 65 20 61 6c 72 65 61 64 79 20 73 74 61 have already sta
13bb2 72 74 65 64 20 61 20 74 72 61 6e 73 61 63 74 69 rted a transacti
13bb3 6f 6e 2c 0a 20 20 20 20 2a 2a 20 77 68 69 63 68 on,. ** which
13bb4 20 6d 65 61 6e 73 20 74 68 65 79 20 68 61 76 65 means they have
13bb5 20 61 63 71 75 69 72 65 64 20 74 68 65 20 6e 65 acquired the ne
13bb6 63 65 73 73 61 72 79 20 6c 6f 63 6b 73 20 61 6e cessary locks an
13bb7 64 20 6f 70 65 6e 65 64 0a 20 20 20 20 2a 2a 20 d opened. **
13bb8 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e a rollback journ
13bb9 61 6c 2e 20 20 44 6f 75 62 6c 65 2d 63 68 65 63 al. Double-chec
13bba 6b 20 74 6f 20 6d 61 6b 65 73 20 73 75 72 65 20 k to makes sure
13bbb 74 68 69 73 20 69 73 20 74 68 65 20 63 61 73 65 this is the case
13bbc 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 .. */. rc
13bbd 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 42 65 = sqlite3PagerBe
13bbe 67 69 6e 28 70 50 61 67 65 72 2c 20 30 2c 20 70 gin(pPager, 0, p
13bbf 50 61 67 65 72 2d 3e 73 75 62 6a 49 6e 4d 65 6d Pager->subjInMem
13bc0 6f 72 79 29 3b 0a 20 20 20 20 69 66 28 20 4e 45 ory);. if( NE
13bc1 56 45 52 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f VER(rc!=SQLITE_O
13bc2 4b 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 K) ){. retu
13bc3 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 rn rc;. }.
13bc4 20 69 66 28 20 21 69 73 4f 70 65 6e 28 70 50 61 if( !isOpen(pPa
13bc5 67 65 72 2d 3e 6a 66 64 29 20 26 26 20 70 50 61 ger->jfd) && pPa
13bc6 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 ger->journalMode
13bc7 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d !=PAGER_JOURNALM
13bc8 4f 44 45 5f 4f 46 46 20 29 7b 0a 20 20 20 20 20 ODE_OFF ){.
13bc9 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
13bca 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 >useJournal );.
13bcb 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f rc = pager_
13bcc 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 70 50 61 open_journal(pPa
13bcd 67 65 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ger);. if(
13bce 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 rc!=SQLITE_OK )
13bcf 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d return rc;. }
13bd0 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 4d . pPager->dbM
13bd1 6f 64 69 66 69 65 64 20 3d 20 31 3b 0a 20 20 0a odified = 1;. .
13bd2 20 20 20 20 2f 2a 20 54 68 65 20 74 72 61 6e 73 /* The trans
13bd3 61 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 6e action journal n
13bd4 6f 77 20 65 78 69 73 74 73 20 61 6e 64 20 77 65 ow exists and we
13bd5 20 68 61 76 65 20 61 20 52 45 53 45 52 56 45 44 have a RESERVED
13bd6 20 6f 72 20 61 6e 0a 20 20 20 20 2a 2a 20 45 58 or an. ** EX
13bd7 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 CLUSIVE lock on
13bd8 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 the main databas
13bd9 65 20 66 69 6c 65 2e 20 20 57 72 69 74 65 20 74 e file. Write t
13bda 68 65 20 63 75 72 72 65 6e 74 20 70 61 67 65 20 he current page
13bdb 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 74 72 to. ** the tr
13bdc 61 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 ansaction journa
13bdd 6c 20 69 66 20 69 74 20 69 73 20 6e 6f 74 20 74 l if it is not t
13bde 68 65 72 65 20 61 6c 72 65 61 64 79 2e 0a 20 20 here already..
13bdf 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 70 61 */. if( !pa
13be0 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 geInJournal(pPg)
13be1 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 && isOpen(pPage
13be2 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 r->jfd) ){.
13be3 20 69 66 28 20 70 50 67 2d 3e 70 67 6e 6f 3c 3d if( pPg->pgno<=
13be4 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 pPager->dbOrigSi
13be5 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 75 33 ze ){. u3
13be6 32 20 63 6b 73 75 6d 3b 0a 20 20 20 20 20 20 20 2 cksum;.
13be7 20 63 68 61 72 20 2a 70 44 61 74 61 32 3b 0a 0a char *pData2;..
13be8 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20 73 68 /* We sh
13be9 6f 75 6c 64 20 6e 65 76 65 72 20 77 72 69 74 65 ould never write
13bea 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 to the journal
13beb 66 69 6c 65 20 74 68 65 20 70 61 67 65 20 74 68 file the page th
13bec 61 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 6f at. ** co
13bed 6e 74 61 69 6e 73 20 74 68 65 20 64 61 74 61 62 ntains the datab
13bee 61 73 65 20 6c 6f 63 6b 73 2e 20 20 54 68 65 20 ase locks. The
13bef 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74 following assert
13bf0 20 76 65 72 69 66 69 65 73 0a 20 20 20 20 20 20 verifies.
13bf1 20 20 2a 2a 20 74 68 61 74 20 77 65 20 64 6f 20 ** that we do
13bf2 6e 6f 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 not. */.
13bf3 61 73 73 65 72 74 28 20 70 50 67 2d 3e 70 67 6e assert( pPg->pgn
13bf4 6f 21 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f o!=PAGER_MJ_PGNO
13bf5 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 20 20 (pPager) );.
13bf6 20 20 20 20 43 4f 44 45 43 32 28 70 50 61 67 65 CODEC2(pPage
13bf7 72 2c 20 70 44 61 74 61 2c 20 70 50 67 2d 3e 70 r, pData, pPg->p
13bf8 67 6e 6f 2c 20 37 2c 20 72 65 74 75 72 6e 20 53 gno, 7, return S
13bf9 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 70 44 61 QLITE_NOMEM, pDa
13bfa 74 61 32 29 3b 0a 20 20 20 20 20 20 20 20 63 6b ta2);. ck
13bfb 73 75 6d 20 3d 20 70 61 67 65 72 5f 63 6b 73 75 sum = pager_cksu
13bfc 6d 28 70 50 61 67 65 72 2c 20 28 75 38 2a 29 70 m(pPager, (u8*)p
13bfd 44 61 74 61 32 29 3b 0a 20 20 20 20 20 20 20 20 Data2);.
13bfe 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 74 73 rc = write32bits
13bff 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 (pPager->jfd, pP
13c00 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
13c01 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 , pPg->pgno);.
13c02 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
13c03 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
13c04 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
13c05 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3OsWrite(pPager-
13c06 3e 6a 66 64 2c 20 70 44 61 74 61 32 2c 20 70 50 >jfd, pData2, pP
13c07 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 0a ager->pageSize,.
13c08 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13c09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 50 pP
13c0a 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
13c0b 20 2b 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20 + 4);.
13c0c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
13c0d 4f 66 66 20 2b 3d 20 70 50 61 67 65 72 2d 3e 70 Off += pPager->p
13c0e 61 67 65 53 69 7a 65 2b 34 3b 0a 20 20 20 20 20 ageSize+4;.
13c0f 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 }. if(
13c10 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
13c11 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d {. rc =
13c12 20 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61 write32bits(pPa
13c13 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 ger->jfd, pPager
13c14 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 63 6b ->journalOff, ck
13c15 73 75 6d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 sum);.
13c16 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
13c17 66 66 20 2b 3d 20 34 3b 0a 20 20 20 20 20 20 20 ff += 4;.
13c18 20 7d 0a 20 20 20 20 20 20 20 20 49 4f 54 52 41 }. IOTRA
13c19 43 45 28 28 22 4a 4f 55 54 20 25 70 20 25 64 20 CE(("JOUT %p %d
13c1a 25 6c 6c 64 20 25 64 5c 6e 22 2c 20 70 50 61 67 %lld %d\n", pPag
13c1b 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 0a er, pPg->pgno, .
13c1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13c1d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
13c1e 4f 66 66 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 Off, pPager->pag
13c1f 65 53 69 7a 65 29 29 3b 0a 20 20 20 20 20 20 20 eSize));.
13c20 20 50 41 47 45 52 5f 49 4e 43 52 28 73 71 6c 69 PAGER_INCR(sqli
13c21 74 65 33 5f 70 61 67 65 72 5f 77 72 69 74 65 6a te3_pager_writej
13c22 5f 63 6f 75 6e 74 29 3b 0a 20 20 20 20 20 20 20 _count);.
13c23 20 50 41 47 45 52 54 52 41 43 45 28 28 22 4a 4f PAGERTRACE(("JO
13c24 55 52 4e 41 4c 20 25 64 20 70 61 67 65 20 25 64 URNAL %d page %d
13c25 20 6e 65 65 64 53 79 6e 63 3d 25 64 20 68 61 73 needSync=%d has
13c26 68 28 25 30 38 78 29 5c 6e 22 2c 0a 20 20 20 20 h(%08x)\n",.
13c27 20 20 20 20 20 20 20 20 20 50 41 47 45 52 49 44 PAGERID
13c28 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 (pPager), pPg->p
13c29 67 6e 6f 2c 20 0a 20 20 20 20 20 20 20 20 20 20 gno, .
13c2a 20 20 20 28 28 70 50 67 2d 3e 66 6c 61 67 73 26 ((pPg->flags&
13c2b 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 PGHDR_NEED_SYNC)
13c2c 3f 31 3a 30 29 2c 20 70 61 67 65 72 5f 70 61 67 ?1:0), pager_pag
13c2d 65 68 61 73 68 28 70 50 67 29 29 29 3b 0a 0a 20 ehash(pPg)));..
13c2e 20 20 20 20 20 20 20 2f 2a 20 45 76 65 6e 20 69 /* Even i
13c2f 66 20 61 6e 20 49 4f 20 6f 72 20 64 69 73 6b 66 f an IO or diskf
13c30 75 6c 6c 20 65 72 72 6f 72 20 6f 63 63 75 72 72 ull error occurr
13c31 65 64 20 77 68 69 6c 65 20 6a 6f 75 72 6e 61 6c ed while journal
13c32 6c 69 6e 67 20 74 68 65 0a 20 20 20 20 20 20 20 ling the.
13c33 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 65 20 ** page in the
13c34 62 6c 6f 63 6b 20 61 62 6f 76 65 2c 20 73 65 74 block above, set
13c35 20 74 68 65 20 6e 65 65 64 2d 73 79 6e 63 20 66 the need-sync f
13c36 6c 61 67 20 66 6f 72 20 74 68 65 20 70 61 67 65 lag for the page
13c37 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 4f 74 68 .. ** Oth
13c38 65 72 77 69 73 65 2c 20 77 68 65 6e 20 74 68 65 erwise, when the
13c39 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
13c3a 72 6f 6c 6c 65 64 20 62 61 63 6b 2c 20 74 68 65 rolled back, the
13c3b 20 6c 6f 67 69 63 20 69 6e 0a 20 20 20 20 20 20 logic in.
13c3c 20 20 2a 2a 20 70 6c 61 79 62 61 63 6b 5f 6f 6e ** playback_on
13c3d 65 5f 70 61 67 65 28 29 20 77 69 6c 6c 20 74 68 e_page() will th
13c3e 69 6e 6b 20 74 68 61 74 20 74 68 65 20 70 61 67 ink that the pag
13c3f 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 e needs to be re
13c40 73 74 6f 72 65 64 0a 20 20 20 20 20 20 20 20 2a stored. *
13c41 2a 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 * in the databas
13c42 65 20 66 69 6c 65 2e 20 41 6e 64 20 69 66 20 61 e file. And if a
13c43 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 n IO error occur
13c44 73 20 77 68 69 6c 65 20 64 6f 69 6e 67 20 73 6f s while doing so
13c45 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 ,. ** the
13c46 6e 20 63 6f 72 72 75 70 74 69 6f 6e 20 6d 61 79 n corruption may
13c47 20 66 6f 6c 6c 6f 77 2e 0a 20 20 20 20 20 20 20 follow..
13c48 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 */. if(
13c49 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 !pPager->noSync
13c4a 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 50 67 ){. pPg
13c4b 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 ->flags |= PGHDR
13c4c 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20 _NEED_SYNC;.
13c4d 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 65 pPager->ne
13c4e 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 edSync = 1;.
13c4f 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 2f }.. /
13c50 2a 20 41 6e 20 65 72 72 6f 72 20 68 61 73 20 6f * An error has o
13c51 63 63 75 72 72 65 64 20 77 72 69 74 69 6e 67 20 ccurred writing
13c52 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 to the journal f
13c53 69 6c 65 2e 20 54 68 65 20 0a 20 20 20 20 20 20 ile. The .
13c54 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e ** transaction
13c55 20 77 69 6c 6c 20 62 65 20 72 6f 6c 6c 65 64 20 will be rolled
13c56 62 61 63 6b 20 62 79 20 74 68 65 20 6c 61 79 65 back by the laye
13c57 72 20 61 62 6f 76 65 2e 0a 20 20 20 20 20 20 20 r above..
13c58 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 */. if(
13c59 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
13c5a 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 . retur
13c5b 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a n rc;. }.
13c5c 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d . pPager-
13c5d 3e 6e 52 65 63 2b 2b 3b 0a 20 20 20 20 20 20 20 >nRec++;.
13c5e 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
13c5f 3e 70 49 6e 4a 6f 75 72 6e 61 6c 21 3d 30 20 29 >pInJournal!=0 )
13c60 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 ;. rc = s
13c61 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 qlite3BitvecSet(
13c62 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e pPager->pInJourn
13c63 61 6c 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a al, pPg->pgno);.
13c64 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 testcase
13c65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d ( rc==SQLITE_NOM
13c66 45 4d 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 EM );. as
13c67 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 sert( rc==SQLITE
13c68 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 _OK || rc==SQLIT
13c69 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 E_NOMEM );.
13c6a 20 20 20 72 63 20 7c 3d 20 61 64 64 54 6f 53 61 rc |= addToSa
13c6b 76 65 70 6f 69 6e 74 42 69 74 76 65 63 73 28 70 vepointBitvecs(p
13c6c 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f Pager, pPg->pgno
13c6d 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 );. if( r
13c6e 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
13c6f 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 assert
13c70 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d ( rc==SQLITE_NOM
13c71 45 4d 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 EM );.
13c72 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
13c73 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
13c74 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70 {. if( !p
13c75 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 Pager->journalSt
13c76 61 72 74 65 64 20 26 26 20 21 70 50 61 67 65 72 arted && !pPager
13c77 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20 ->noSync ){.
13c78 20 20 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 pPg->flags
13c79 20 7c 3d 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 |= PGHDR_NEED_S
13c7a 59 4e 43 3b 0a 20 20 20 20 20 20 20 20 20 20 70 YNC;. p
13c7b 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 Pager->needSync
13c7c 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 = 1;. }.
13c7d 20 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43 PAGERTRAC
13c7e 45 28 28 22 41 50 50 45 4e 44 20 25 64 20 70 61 E(("APPEND %d pa
13c7f 67 65 20 25 64 20 6e 65 65 64 53 79 6e 63 3d 25 ge %d needSync=%
13c80 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 d\n",.
13c81 20 20 20 20 20 20 50 41 47 45 52 49 44 28 70 50 PAGERID(pP
13c82 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f ager), pPg->pgno
13c83 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
13c84 20 28 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 ((pPg->flags&PG
13c85 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 3f 31 HDR_NEED_SYNC)?1
13c86 3a 30 29 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 :0)));. }.
13c87 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 }. . /* I
13c88 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 f the statement
13c89 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 70 65 6e 20 journal is open
13c8a 61 6e 64 20 74 68 65 20 70 61 67 65 20 69 73 20 and the page is
13c8b 6e 6f 74 20 69 6e 20 69 74 2c 0a 20 20 20 20 2a not in it,. *
13c8c 2a 20 74 68 65 6e 20 77 72 69 74 65 20 74 68 65 * then write the
13c8d 20 63 75 72 72 65 6e 74 20 70 61 67 65 20 74 6f current page to
13c8e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a the statement j
13c8f 6f 75 72 6e 61 6c 2e 20 20 4e 6f 74 65 20 74 68 ournal. Note th
13c90 61 74 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 74 at. ** the st
13c91 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 atement journal
13c92 66 6f 72 6d 61 74 20 64 69 66 66 65 72 73 20 66 format differs f
13c93 72 6f 6d 20 74 68 65 20 73 74 61 6e 64 61 72 64 rom the standard
13c94 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 0a journal format.
13c95 20 20 20 20 2a 2a 20 69 6e 20 74 68 61 74 20 69 ** in that i
13c96 74 20 6f 6d 69 74 73 20 74 68 65 20 63 68 65 63 t omits the chec
13c97 6b 73 75 6d 73 20 61 6e 64 20 74 68 65 20 68 65 ksums and the he
13c98 61 64 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ader.. */.
13c99 20 69 66 28 20 73 75 62 6a 52 65 71 75 69 72 65 if( subjRequire
13c9a 73 50 61 67 65 28 70 50 67 29 20 29 7b 0a 20 20 sPage(pPg) ){.
13c9b 20 20 20 20 72 63 20 3d 20 73 75 62 6a 6f 75 72 rc = subjour
13c9c 6e 61 6c 50 61 67 65 28 70 50 67 29 3b 0a 20 20 nalPage(pPg);.
13c9d 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 }. }.. /* Up
13c9e 64 61 74 65 20 74 68 65 20 64 61 74 61 62 61 73 date the databas
13c9f 65 20 73 69 7a 65 20 61 6e 64 20 72 65 74 75 72 e size and retur
13ca0 6e 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 n.. */. assert
13ca1 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e ( pPager->state>
13ca2 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b =PAGER_SHARED );
13ca3 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 . if( pPager->d
13ca4 62 53 69 7a 65 3c 70 50 67 2d 3e 70 67 6e 6f 20 bSize<pPg->pgno
13ca5 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 ){. pPager->d
13ca6 62 53 69 7a 65 20 3d 20 70 50 67 2d 3e 70 67 6e bSize = pPg->pgn
13ca7 6f 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 o;. }. return
13ca8 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 rc;.}../*.** Mar
13ca9 6b 20 61 20 64 61 74 61 20 70 61 67 65 20 61 73 k a data page as
13caa 20 77 72 69 74 65 61 62 6c 65 2e 20 54 68 69 73 writeable. This
13cab 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 62 65 routine must be
13cac 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 65 20 0a called before .
13cad 2a 2a 20 6d 61 6b 69 6e 67 20 63 68 61 6e 67 65 ** making change
13cae 73 20 74 6f 20 61 20 70 61 67 65 2e 20 54 68 65 s to a page. The
13caf 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 63 68 65 caller must che
13cb0 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 ck the return va
13cb1 6c 75 65 20 0a 2a 2a 20 6f 66 20 74 68 69 73 20 lue .** of this
13cb2 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 62 65 20 function and be
13cb3 63 61 72 65 66 75 6c 20 6e 6f 74 20 74 6f 20 63 careful not to c
13cb4 68 61 6e 67 65 20 61 6e 79 20 70 61 67 65 20 64 hange any page d
13cb5 61 74 61 20 75 6e 6c 65 73 73 20 0a 2a 2a 20 74 ata unless .** t
13cb6 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 his routine retu
13cb7 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a rns SQLITE_OK..*
13cb8 2a 0a 2a 2a 20 54 68 65 20 64 69 66 66 65 72 65 *.** The differe
13cb9 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68 69 73 nce between this
13cba 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 70 61 function and pa
13cbb 67 65 72 5f 77 72 69 74 65 28 29 20 69 73 20 74 ger_write() is t
13cbc 68 61 74 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 hat this.** func
13cbd 74 69 6f 6e 20 61 6c 73 6f 20 64 65 61 6c 73 20 tion also deals
13cbe 77 69 74 68 20 74 68 65 20 73 70 65 63 69 61 6c with the special
13cbf 20 63 61 73 65 20 77 68 65 72 65 20 32 20 6f 72 case where 2 or
13cc0 20 6d 6f 72 65 20 70 61 67 65 73 0a 2a 2a 20 66 more pages.** f
13cc1 69 74 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 64 it on a single d
13cc2 69 73 6b 20 73 65 63 74 6f 72 2e 20 49 6e 20 74 isk sector. In t
13cc3 68 69 73 20 63 61 73 65 20 61 6c 6c 20 63 6f 2d his case all co-
13cc4 72 65 73 69 64 65 6e 74 20 70 61 67 65 73 0a 2a resident pages.*
13cc5 2a 20 6d 75 73 74 20 68 61 76 65 20 62 65 65 6e * must have been
13cc6 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 written to the
13cc7 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 62 65 66 journal file bef
13cc8 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a ore returning..*
13cc9 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 *.** If an error
13cca 20 6f 63 63 75 72 73 2c 20 53 51 4c 49 54 45 5f occurs, SQLITE_
13ccb 4e 4f 4d 45 4d 20 6f 72 20 61 6e 20 49 4f 20 65 NOMEM or an IO e
13ccc 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 rror code is ret
13ccd 75 72 6e 65 64 0a 2a 2a 20 61 73 20 61 70 70 72 urned.** as appr
13cce 6f 70 72 69 61 74 65 2e 20 4f 74 68 65 72 77 69 opriate. Otherwi
13ccf 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a se, SQLITE_OK..*
13cd0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
13cd1 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
13cd2 72 57 72 69 74 65 28 44 62 50 61 67 65 20 2a 70 rWrite(DbPage *p
13cd3 44 62 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 72 DbPage){. int r
13cd4 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a c = SQLITE_OK;..
13cd5 20 20 50 67 48 64 72 20 2a 70 50 67 20 3d 20 70 PgHdr *pPg = p
13cd6 44 62 50 61 67 65 3b 0a 20 20 50 61 67 65 72 20 DbPage;. Pager
13cd7 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 *pPager = pPg->p
13cd8 50 61 67 65 72 3b 0a 20 20 50 67 6e 6f 20 6e 50 Pager;. Pgno nP
13cd9 61 67 65 50 65 72 53 65 63 74 6f 72 20 3d 20 28 agePerSector = (
13cda 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 pPager->sectorSi
13cdb 7a 65 2f 70 50 61 67 65 72 2d 3e 70 61 67 65 53 ze/pPager->pageS
13cdc 69 7a 65 29 3b 0a 0a 20 20 69 66 28 20 6e 50 61 ize);.. if( nPa
13cdd 67 65 50 65 72 53 65 63 74 6f 72 3e 31 20 29 7b gePerSector>1 ){
13cde 0a 20 20 20 20 50 67 6e 6f 20 6e 50 61 67 65 43 . Pgno nPageC
13cdf 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20 2f ount; /
13ce0 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f * Total number o
13ce1 66 20 70 61 67 65 73 20 69 6e 20 64 61 74 61 62 f pages in datab
13ce2 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 ase file */.
13ce3 50 67 6e 6f 20 70 67 31 3b 20 20 20 20 20 20 20 Pgno pg1;
13ce4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 /* Fir
13ce5 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 73 st page of the s
13ce6 65 63 74 6f 72 20 70 50 67 20 69 73 20 6c 6f 63 ector pPg is loc
13ce7 61 74 65 64 20 6f 6e 2e 20 2a 2f 0a 20 20 20 20 ated on. */.
13ce8 69 6e 74 20 6e 50 61 67 65 3b 20 20 20 20 20 20 int nPage;
13ce9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
13cea 62 65 72 20 6f 66 20 70 61 67 65 73 20 73 74 61 ber of pages sta
13ceb 72 74 69 6e 67 20 61 74 20 70 67 31 20 74 6f 20 rting at pg1 to
13cec 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 20 20 69 journal */. i
13ced 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 nt ii;
13cee 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 /* Loop
13cef 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 counter */.
13cf0 69 6e 74 20 6e 65 65 64 53 79 6e 63 20 3d 20 30 int needSync = 0
13cf1 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 ; /* Tru
13cf2 65 20 69 66 20 61 6e 79 20 70 61 67 65 20 68 61 e if any page ha
13cf3 73 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e s PGHDR_NEED_SYN
13cf4 43 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 53 65 74 C */.. /* Set
13cf5 20 74 68 65 20 64 6f 4e 6f 74 53 79 6e 63 20 66 the doNotSync f
13cf6 6c 61 67 20 74 6f 20 31 2e 20 54 68 69 73 20 69 lag to 1. This i
13cf7 73 20 62 65 63 61 75 73 65 20 77 65 20 63 61 6e s because we can
13cf8 6e 6f 74 20 61 6c 6c 6f 77 20 61 20 6a 6f 75 72 not allow a jour
13cf9 6e 61 6c 0a 20 20 20 20 2a 2a 20 68 65 61 64 65 nal. ** heade
13cfa 72 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 r to be written
13cfb 62 65 74 77 65 65 6e 20 74 68 65 20 70 61 67 65 between the page
13cfc 73 20 6a 6f 75 72 6e 61 6c 65 64 20 62 79 20 74 s journaled by t
13cfd 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 his function..
13cfe 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 */. assert(
13cff 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 20 20 61 !MEMDB );. a
13d00 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 ssert( pPager->d
13d01 6f 4e 6f 74 53 79 6e 63 3d 3d 30 20 29 3b 0a 20 oNotSync==0 );.
13d02 20 20 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 pPager->doNot
13d03 53 79 6e 63 20 3d 20 31 3b 0a 0a 20 20 20 20 2f Sync = 1;.. /
13d04 2a 20 54 68 69 73 20 74 72 69 63 6b 20 61 73 73 * This trick ass
13d05 75 6d 65 73 20 74 68 61 74 20 62 6f 74 68 20 74 umes that both t
13d06 68 65 20 70 61 67 65 2d 73 69 7a 65 20 61 6e 64 he page-size and
13d07 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 61 72 65 sector-size are
13d08 0a 20 20 20 20 2a 2a 20 61 6e 20 69 6e 74 65 67 . ** an integ
13d09 65 72 20 70 6f 77 65 72 20 6f 66 20 32 2e 20 49 er power of 2. I
13d0a 74 20 73 65 74 73 20 76 61 72 69 61 62 6c 65 20 t sets variable
13d0b 70 67 31 20 74 6f 20 74 68 65 20 69 64 65 6e 74 pg1 to the ident
13d0c 69 66 69 65 72 0a 20 20 20 20 2a 2a 20 6f 66 20 ifier. ** of
13d0d 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20 6f the first page o
13d0e 66 20 74 68 65 20 73 65 63 74 6f 72 20 70 50 67 f the sector pPg
13d0f 20 69 73 20 6c 6f 63 61 74 65 64 20 6f 6e 2e 0a is located on..
13d10 20 20 20 20 2a 2f 0a 20 20 20 20 70 67 31 20 3d */. pg1 =
13d11 20 28 28 70 50 67 2d 3e 70 67 6e 6f 2d 31 29 20 ((pPg->pgno-1)
13d12 26 20 7e 28 6e 50 61 67 65 50 65 72 53 65 63 74 & ~(nPagePerSect
13d13 6f 72 2d 31 29 29 20 2b 20 31 3b 0a 0a 20 20 20 or-1)) + 1;..
13d14 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 sqlite3PagerPag
13d15 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 28 ecount(pPager, (
13d16 69 6e 74 20 2a 29 26 6e 50 61 67 65 43 6f 75 6e int *)&nPageCoun
13d17 74 29 3b 0a 20 20 20 20 69 66 28 20 70 50 67 2d t);. if( pPg-
13d18 3e 70 67 6e 6f 3e 6e 50 61 67 65 43 6f 75 6e 74 >pgno>nPageCount
13d19 20 29 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 20 ){. nPage
13d1a 3d 20 28 70 50 67 2d 3e 70 67 6e 6f 20 2d 20 70 = (pPg->pgno - p
13d1b 67 31 29 2b 31 3b 0a 20 20 20 20 7d 65 6c 73 65 g1)+1;. }else
13d1c 20 69 66 28 20 28 70 67 31 2b 6e 50 61 67 65 50 if( (pg1+nPageP
13d1d 65 72 53 65 63 74 6f 72 2d 31 29 3e 6e 50 61 67 erSector-1)>nPag
13d1e 65 43 6f 75 6e 74 20 29 7b 0a 20 20 20 20 20 20 eCount ){.
13d1f 6e 50 61 67 65 20 3d 20 6e 50 61 67 65 43 6f 75 nPage = nPageCou
13d20 6e 74 2b 31 2d 70 67 31 3b 0a 20 20 20 20 7d 65 nt+1-pg1;. }e
13d21 6c 73 65 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 lse{. nPage
13d22 20 3d 20 6e 50 61 67 65 50 65 72 53 65 63 74 6f = nPagePerSecto
13d23 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 r;. }. ass
13d24 65 72 74 28 6e 50 61 67 65 3e 30 29 3b 0a 20 20 ert(nPage>0);.
13d25 20 20 61 73 73 65 72 74 28 70 67 31 3c 3d 70 50 assert(pg1<=pP
13d26 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 61 73 g->pgno);. as
13d27 73 65 72 74 28 28 70 67 31 2b 6e 50 61 67 65 29 sert((pg1+nPage)
13d28 3e 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20 >pPg->pgno);..
13d29 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e for(ii=0; ii<n
13d2a 50 61 67 65 20 26 26 20 72 63 3d 3d 53 51 4c 49 Page && rc==SQLI
13d2b 54 45 5f 4f 4b 3b 20 69 69 2b 2b 29 7b 0a 20 20 TE_OK; ii++){.
13d2c 20 20 20 20 50 67 6e 6f 20 70 67 20 3d 20 70 67 Pgno pg = pg
13d2d 31 2b 69 69 3b 0a 20 20 20 20 20 20 50 67 48 64 1+ii;. PgHd
13d2e 72 20 2a 70 50 61 67 65 3b 0a 20 20 20 20 20 20 r *pPage;.
13d2f 69 66 28 20 70 67 3d 3d 70 50 67 2d 3e 70 67 6e if( pg==pPg->pgn
13d30 6f 20 7c 7c 20 21 73 71 6c 69 74 65 33 42 69 74 o || !sqlite3Bit
13d31 76 65 63 54 65 73 74 28 70 50 61 67 65 72 2d 3e vecTest(pPager->
13d32 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 67 29 20 pInJournal, pg)
13d33 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 ){. if( p
13d34 67 21 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f g!=PAGER_MJ_PGNO
13d35 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 (pPager) ){.
13d36 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
13d37 65 33 50 61 67 65 72 47 65 74 28 70 50 61 67 65 e3PagerGet(pPage
13d38 72 2c 20 70 67 2c 20 26 70 50 61 67 65 29 3b 0a r, pg, &pPage);.
13d39 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 if( rc
13d3a 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
13d3b 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 rc =
13d3c 70 61 67 65 72 5f 77 72 69 74 65 28 70 50 61 67 pager_write(pPag
13d3d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 e);.
13d3e 69 66 28 20 70 50 61 67 65 2d 3e 66 6c 61 67 73 if( pPage->flags
13d3f 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 &PGHDR_NEED_SYNC
13d40 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
13d41 20 20 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a needSync = 1;.
13d42 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 73 as
13d43 73 65 72 74 28 70 50 61 67 65 72 2d 3e 6e 65 65 sert(pPager->nee
13d44 64 53 79 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 dSync);.
13d45 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
13d46 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e sqlite3PagerUn
13d47 72 65 66 28 70 50 61 67 65 29 3b 0a 20 20 20 20 ref(pPage);.
13d48 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
13d49 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 }. }else if
13d4a 28 20 28 70 50 61 67 65 20 3d 20 70 61 67 65 72 ( (pPage = pager
13d4b 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 _lookup(pPager,
13d4c 70 67 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 pg))!=0 ){.
13d4d 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 66 6c if( pPage->fl
13d4e 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 ags&PGHDR_NEED_S
13d4f 59 4e 43 20 29 7b 0a 20 20 20 20 20 20 20 20 20 YNC ){.
13d50 20 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 needSync = 1;.
13d51 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
13d52 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 sqlite3PagerUnr
13d53 65 66 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 ef(pPage);.
13d54 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a }. }.. /*
13d55 20 49 66 20 74 68 65 20 50 47 48 44 52 5f 4e 45 If the PGHDR_NE
13d56 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 69 73 20 ED_SYNC flag is
13d57 73 65 74 20 66 6f 72 20 61 6e 79 20 6f 66 20 74 set for any of t
13d58 68 65 20 6e 50 61 67 65 20 70 61 67 65 73 20 0a he nPage pages .
13d59 20 20 20 20 2a 2a 20 73 74 61 72 74 69 6e 67 20 ** starting
13d5a 61 74 20 70 67 31 2c 20 74 68 65 6e 20 69 74 20 at pg1, then it
13d5b 6e 65 65 64 73 20 74 6f 20 62 65 20 73 65 74 20 needs to be set
13d5c 66 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65 6d 2e for all of them.
13d5d 20 42 65 63 61 75 73 65 0a 20 20 20 20 2a 2a 20 Because. **
13d5e 77 72 69 74 69 6e 67 20 74 6f 20 61 6e 79 20 6f writing to any o
13d5f 66 20 74 68 65 73 65 20 6e 50 61 67 65 20 70 61 f these nPage pa
13d60 67 65 73 20 6d 61 79 20 64 61 6d 61 67 65 20 74 ges may damage t
13d61 68 65 20 6f 74 68 65 72 73 2c 20 74 68 65 0a 20 he others, the.
13d62 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 ** journal fi
13d63 6c 65 20 6d 75 73 74 20 63 6f 6e 74 61 69 6e 20 le must contain
13d64 73 79 6e 63 28 29 65 64 20 63 6f 70 69 65 73 20 sync()ed copies
13d65 6f 66 20 61 6c 6c 20 6f 66 20 74 68 65 6d 0a 20 of all of them.
13d66 20 20 20 2a 2a 20 62 65 66 6f 72 65 20 61 6e 79 ** before any
13d67 20 6f 66 20 74 68 65 6d 20 63 61 6e 20 62 65 20 of them can be
13d68 77 72 69 74 74 65 6e 20 6f 75 74 20 74 6f 20 74 written out to t
13d69 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
13d6a 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
13d6b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
13d6c 26 20 6e 65 65 64 53 79 6e 63 20 29 7b 0a 20 20 & needSync ){.
13d6d 20 20 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d assert( !MEM
13d6e 44 42 20 26 26 20 70 50 61 67 65 72 2d 3e 6e 6f DB && pPager->no
13d6f 53 79 6e 63 3d 3d 30 20 29 3b 0a 20 20 20 20 20 Sync==0 );.
13d70 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 50 for(ii=0; ii<nP
13d71 61 67 65 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 age; ii++){.
13d72 20 20 20 20 50 67 48 64 72 20 2a 70 50 61 67 65 PgHdr *pPage
13d73 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 = pager_lookup(
13d74 70 50 61 67 65 72 2c 20 70 67 31 2b 69 69 29 3b pPager, pg1+ii);
13d75 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61 . if( pPa
13d76 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ge ){.
13d77 70 50 61 67 65 2d 3e 66 6c 61 67 73 20 7c 3d 20 pPage->flags |=
13d78 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b PGHDR_NEED_SYNC;
13d79 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
13d7a 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 61 e3PagerUnref(pPa
13d7b 67 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 ge);. }.
13d7c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 }. ass
13d7d 65 72 74 28 70 50 61 67 65 72 2d 3e 6e 65 65 64 ert(pPager->need
13d7e 53 79 6e 63 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 Sync);. }..
13d7f 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
13d80 2d 3e 64 6f 4e 6f 74 53 79 6e 63 3d 3d 31 20 29 ->doNotSync==1 )
13d81 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 6f ;. pPager->do
13d82 4e 6f 74 53 79 6e 63 20 3d 20 30 3b 0a 20 20 7d NotSync = 0;. }
13d83 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 70 else{. rc = p
13d84 61 67 65 72 5f 77 72 69 74 65 28 70 44 62 50 61 ager_write(pDbPa
13d85 67 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ge);. }. retur
13d86 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 n rc;.}../*.** R
13d87 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 eturn TRUE if th
13d88 65 20 70 61 67 65 20 67 69 76 65 6e 20 69 6e 20 e page given in
13d89 74 68 65 20 61 72 67 75 6d 65 6e 74 20 77 61 73 the argument was
13d8a 20 70 72 65 76 69 6f 75 73 6c 79 20 70 61 73 73 previously pass
13d8b 65 64 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 ed.** to sqlite3
13d8c 50 61 67 65 72 57 72 69 74 65 28 29 2e 20 20 49 PagerWrite(). I
13d8d 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 72 n other words, r
13d8e 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 69 74 eturn TRUE if it
13d8f 20 69 73 20 6f 6b 0a 2a 2a 20 74 6f 20 63 68 61 is ok.** to cha
13d90 6e 67 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 nge the content
13d91 6f 66 20 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a of the page..*/.
13d92 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 53 #ifndef NDEBUG.S
13d93 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
13d94 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 t sqlite3PagerIs
13d95 77 72 69 74 65 61 62 6c 65 28 44 62 50 61 67 65 writeable(DbPage
13d96 20 2a 70 50 67 29 7b 0a 20 20 72 65 74 75 72 6e *pPg){. return
13d97 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 pPg->flags&PGHD
13d98 52 5f 44 49 52 54 59 3b 0a 7d 0a 23 65 6e 64 69 R_DIRTY;.}.#endi
13d99 66 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 f../*.** A call
13d9a 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 to this routine
13d9b 74 65 6c 6c 73 20 74 68 65 20 70 61 67 65 72 20 tells the pager
13d9c 74 68 61 74 20 69 74 20 69 73 20 6e 6f 74 20 6e that it is not n
13d9d 65 63 65 73 73 61 72 79 20 74 6f 0a 2a 2a 20 77 ecessary to.** w
13d9e 72 69 74 65 20 74 68 65 20 69 6e 66 6f 72 6d 61 rite the informa
13d9f 74 69 6f 6e 20 6f 6e 20 70 61 67 65 20 70 50 67 tion on page pPg
13da0 20 62 61 63 6b 20 74 6f 20 74 68 65 20 64 69 73 back to the dis
13da1 6b 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 0a 2a k, even though.*
13da2 2a 20 74 68 61 74 20 70 61 67 65 20 6d 69 67 68 * that page migh
13da3 74 20 62 65 20 6d 61 72 6b 65 64 20 61 73 20 64 t be marked as d
13da4 69 72 74 79 2e 20 20 54 68 69 73 20 68 61 70 70 irty. This happ
13da5 65 6e 73 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 ens, for example
13da6 2c 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 70 61 , when.** the pa
13da7 67 65 20 68 61 73 20 62 65 65 6e 20 61 64 64 65 ge has been adde
13da8 64 20 61 73 20 61 20 6c 65 61 66 20 6f 66 20 74 d as a leaf of t
13da9 68 65 20 66 72 65 65 6c 69 73 74 20 61 6e 64 20 he freelist and
13daa 73 6f 20 69 74 73 0a 2a 2a 20 63 6f 6e 74 65 6e so its.** conten
13dab 74 20 6e 6f 20 6c 6f 6e 67 65 72 20 6d 61 74 74 t no longer matt
13dac 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f ers..**.** The o
13dad 76 65 72 6c 79 69 6e 67 20 73 6f 66 74 77 61 72 verlying softwar
13dae 65 20 6c 61 79 65 72 20 63 61 6c 6c 73 20 74 68 e layer calls th
13daf 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 is routine when
13db0 61 6c 6c 20 6f 66 20 74 68 65 20 64 61 74 61 0a all of the data.
13db1 2a 2a 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 ** on the given
13db2 70 61 67 65 20 69 73 20 75 6e 75 73 65 64 2e 20 page is unused.
13db3 54 68 65 20 70 61 67 65 72 20 6d 61 72 6b 73 20 The pager marks
13db4 74 68 65 20 70 61 67 65 20 61 73 20 63 6c 65 61 the page as clea
13db5 6e 20 73 6f 0a 2a 2a 20 74 68 61 74 20 69 74 20 n so.** that it
13db6 64 6f 65 73 20 6e 6f 74 20 67 65 74 20 77 72 69 does not get wri
13db7 74 74 65 6e 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a tten to disk..**
13db8 0a 2a 2a 20 54 65 73 74 73 20 73 68 6f 77 20 74 .** Tests show t
13db9 68 61 74 20 74 68 69 73 20 6f 70 74 69 6d 69 7a hat this optimiz
13dba 61 74 69 6f 6e 20 63 61 6e 20 71 75 61 64 72 75 ation can quadru
13dbb 70 6c 65 20 74 68 65 20 73 70 65 65 64 20 6f 66 ple the speed of
13dbc 20 6c 61 72 67 65 20 0a 2a 2a 20 44 45 4c 45 54 large .** DELET
13dbd 45 20 6f 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2f E operations..*/
13dbe 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
13dbf 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 void sqlite3Page
13dc0 72 44 6f 6e 74 57 72 69 74 65 28 50 67 48 64 72 rDontWrite(PgHdr
13dc1 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 *pPg){. Pager
13dc2 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 *pPager = pPg->p
13dc3 50 61 67 65 72 3b 0a 20 20 69 66 28 20 28 70 50 Pager;. if( (pP
13dc4 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 g->flags&PGHDR_D
13dc5 49 52 54 59 29 20 26 26 20 70 50 61 67 65 72 2d IRTY) && pPager-
13dc6 3e 6e 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 29 >nSavepoint==0 )
13dc7 7b 0a 20 20 20 20 50 41 47 45 52 54 52 41 43 45 {. PAGERTRACE
13dc8 28 28 22 44 4f 4e 54 5f 57 52 49 54 45 20 70 61 (("DONT_WRITE pa
13dc9 67 65 20 25 64 20 6f 66 20 25 64 5c 6e 22 2c 20 ge %d of %d\n",
13dca 70 50 67 2d 3e 70 67 6e 6f 2c 20 50 41 47 45 52 pPg->pgno, PAGER
13dcb 49 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 ID(pPager)));.
13dcc 20 20 49 4f 54 52 41 43 45 28 28 22 43 4c 45 41 IOTRACE(("CLEA
13dcd 4e 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 N %p %d\n", pPag
13dce 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 29 0a er, pPg->pgno)).
13dcf 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c pPg->flags |
13dd0 3d 20 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 = PGHDR_DONT_WRI
13dd1 54 45 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 TE;.#ifdef SQLIT
13dd2 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 E_CHECK_PAGES.
13dd3 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73 68 20 pPg->pageHash
13dd4 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 = pager_pagehash
13dd5 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20 (pPg);.#endif.
13dd6 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 }.}../*.** This
13dd7 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
13dd8 64 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74 20 74 d to increment t
13dd9 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 he value of the
13dda 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 0a 2a database file .*
13ddb 2a 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 * change-counter
13ddc 2c 20 73 74 6f 72 65 64 20 61 73 20 61 20 34 2d , stored as a 4-
13ddd 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 byte big-endian
13dde 69 6e 74 65 67 65 72 20 73 74 61 72 74 69 6e 67 integer starting
13ddf 20 61 74 20 0a 2a 2a 20 62 79 74 65 20 6f 66 66 at .** byte off
13de0 73 65 74 20 32 34 20 6f 66 20 74 68 65 20 70 61 set 24 of the pa
13de1 67 65 72 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 ger file..**.**
13de2 49 66 20 74 68 65 20 69 73 44 69 72 65 63 74 4d If the isDirectM
13de3 6f 64 65 20 66 6c 61 67 20 69 73 20 7a 65 72 6f ode flag is zero
13de4 2c 20 74 68 65 6e 20 74 68 69 73 20 69 73 20 64 , then this is d
13de5 6f 6e 65 20 62 79 20 63 61 6c 6c 69 6e 67 20 0a one by calling .
13de6 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 ** sqlite3PagerW
13de7 72 69 74 65 28 29 20 6f 6e 20 70 61 67 65 20 31 rite() on page 1
13de8 2c 20 74 68 65 6e 20 6d 6f 64 69 66 79 69 6e 67 , then modifying
13de9 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 the contents of
13dea 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 64 61 74 the.** page dat
13deb 61 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 a. In this case
13dec 74 68 65 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 the file will be
13ded 20 75 70 64 61 74 65 64 20 77 68 65 6e 20 74 68 updated when th
13dee 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 74 72 61 e current.** tra
13def 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d nsaction is comm
13df0 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 itted..**.** The
13df1 20 69 73 44 69 72 65 63 74 4d 6f 64 65 20 66 6c isDirectMode fl
13df2 61 67 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 6e ag may only be n
13df3 6f 6e 2d 7a 65 72 6f 20 69 66 20 74 68 65 20 6c on-zero if the l
13df4 69 62 72 61 72 79 20 77 61 73 20 63 6f 6d 70 69 ibrary was compi
13df5 6c 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 led.** with the
13df6 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 SQLITE_ENABLE_AT
13df7 4f 4d 49 43 5f 57 52 49 54 45 20 6d 61 63 72 6f OMIC_WRITE macro
13df8 20 64 65 66 69 6e 65 64 2e 20 49 6e 20 74 68 69 defined. In thi
13df9 73 20 63 61 73 65 2c 0a 2a 2a 20 69 66 20 69 73 s case,.** if is
13dfa 44 69 72 65 63 74 20 69 73 20 6e 6f 6e 2d 7a 65 Direct is non-ze
13dfb 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 64 61 74 ro, then the dat
13dfc 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 75 70 abase file is up
13dfd 64 61 74 65 64 20 64 69 72 65 63 74 6c 79 0a 2a dated directly.*
13dfe 2a 20 62 79 20 77 72 69 74 69 6e 67 20 61 6e 20 * by writing an
13dff 75 70 64 61 74 65 64 20 76 65 72 73 69 6f 6e 20 updated version
13e00 6f 66 20 70 61 67 65 20 31 20 75 73 69 6e 67 20 of page 1 using
13e01 61 20 63 61 6c 6c 20 74 6f 20 74 68 65 20 0a 2a a call to the .*
13e02 2a 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 * sqlite3OsWrite
13e03 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a () function..*/.
13e04 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 static int pager
13e05 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e _incr_changecoun
13e06 74 65 72 28 50 61 67 65 72 20 2a 70 50 61 67 65 ter(Pager *pPage
13e07 72 2c 20 69 6e 74 20 69 73 44 69 72 65 63 74 4d r, int isDirectM
13e08 6f 64 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d ode){. int rc =
13e09 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f SQLITE_OK;.. /
13e0a 2a 20 44 65 63 6c 61 72 65 20 61 6e 64 20 69 6e * Declare and in
13e0b 69 74 69 61 6c 69 7a 65 20 63 6f 6e 73 74 61 6e itialize constan
13e0c 74 20 69 6e 74 65 67 65 72 20 27 69 73 44 69 72 t integer 'isDir
13e0d 65 63 74 27 2e 20 49 66 20 74 68 65 0a 20 20 2a ect'. If the. *
13e0e 2a 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f * atomic-write o
13e0f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 65 ptimization is e
13e10 6e 61 62 6c 65 64 20 69 6e 20 74 68 69 73 20 62 nabled in this b
13e11 75 69 6c 64 2c 20 74 68 65 6e 20 69 73 44 69 72 uild, then isDir
13e12 65 63 74 0a 20 20 2a 2a 20 69 73 20 69 6e 69 74 ect. ** is init
13e13 69 61 6c 69 7a 65 64 20 74 6f 20 74 68 65 20 76 ialized to the v
13e14 61 6c 75 65 20 70 61 73 73 65 64 20 61 73 20 74 alue passed as t
13e15 68 65 20 69 73 44 69 72 65 63 74 4d 6f 64 65 20 he isDirectMode
13e16 70 61 72 61 6d 65 74 65 72 0a 20 20 2a 2a 20 74 parameter. ** t
13e17 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e o this function.
13e18 20 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20 69 Otherwise, it i
13e19 73 20 61 6c 77 61 79 73 20 73 65 74 20 74 6f 20 s always set to
13e1a 7a 65 72 6f 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 zero.. **. **
13e1b 54 68 65 20 69 64 65 61 20 69 73 20 74 68 61 74 The idea is that
13e1c 20 69 66 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 if the atomic-w
13e1d 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f rite optimizatio
13e1e 6e 20 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 65 6e n is not. ** en
13e1f 61 62 6c 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 abled at compile
13e20 20 74 69 6d 65 2c 20 74 68 65 20 63 6f 6d 70 69 time, the compi
13e21 6c 65 72 20 63 61 6e 20 6f 6d 69 74 20 74 68 65 ler can omit the
13e22 20 74 65 73 74 73 20 6f 66 0a 20 20 2a 2a 20 27 tests of. ** '
13e23 69 73 44 69 72 65 63 74 27 20 62 65 6c 6f 77 2c isDirect' below,
13e24 20 61 73 20 77 65 6c 6c 20 61 73 20 74 68 65 20 as well as the
13e25 62 6c 6f 63 6b 20 65 6e 63 6c 6f 73 65 64 20 69 block enclosed i
13e26 6e 20 74 68 65 0a 20 20 2a 2a 20 22 69 66 28 20 n the. ** "if(
13e27 69 73 44 69 72 65 63 74 20 29 22 20 63 6f 6e 64 isDirect )" cond
13e28 69 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 23 69 66 6e ition.. */.#ifn
13e29 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c def SQLITE_ENABL
13e2a 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 23 E_ATOMIC_WRITE.#
13e2b 20 64 65 66 69 6e 65 20 44 49 52 45 43 54 5f 4d define DIRECT_M
13e2c 4f 44 45 20 30 0a 20 20 61 73 73 65 72 74 28 20 ODE 0. assert(
13e2d 69 73 44 69 72 65 63 74 4d 6f 64 65 3d 3d 30 20 isDirectMode==0
13e2e 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 );. UNUSED_PARA
13e2f 4d 45 54 45 52 28 69 73 44 69 72 65 63 74 4d 6f METER(isDirectMo
13e30 64 65 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 de);.#else.# def
13e31 69 6e 65 20 44 49 52 45 43 54 5f 4d 4f 44 45 20 ine DIRECT_MODE
13e32 69 73 44 69 72 65 63 74 4d 6f 64 65 0a 23 65 6e isDirectMode.#en
13e33 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 70 dif.. assert( p
13e34 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 Pager->state>=PA
13e35 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a GER_RESERVED );.
13e36 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 63 if( !pPager->c
13e37 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 26 hangeCountDone &
13e38 26 20 41 4c 57 41 59 53 28 70 50 61 67 65 72 2d & ALWAYS(pPager-
13e39 3e 64 62 53 69 7a 65 3e 30 29 20 29 7b 0a 20 20 >dbSize>0) ){.
13e3a 20 20 50 67 48 64 72 20 2a 70 50 67 48 64 72 3b PgHdr *pPgHdr;
13e3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13e3c 2f 2a 20 52 65 66 65 72 65 6e 63 65 20 74 6f 20 /* Reference to
13e3d 70 61 67 65 20 31 20 2a 2f 0a 20 20 20 20 75 33 page 1 */. u3
13e3e 32 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 2 change_counter
13e3f 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 ; /* I
13e40 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 6f 66 20 nitial value of
13e41 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 66 change-counter f
13e42 69 65 6c 64 20 2a 2f 0a 0a 20 20 20 20 61 73 73 ield */.. ass
13e43 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e 74 65 ert( !pPager->te
13e44 6d 70 46 69 6c 65 20 26 26 20 69 73 4f 70 65 6e mpFile && isOpen
13e45 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 3b 0a (pPager->fd) );.
13e46 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20 70 61 67 . /* Open pag
13e47 65 20 31 20 6f 66 20 74 68 65 20 66 69 6c 65 20 e 1 of the file
13e48 66 6f 72 20 77 72 69 74 69 6e 67 2e 20 2a 2f 0a for writing. */.
13e49 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
13e4a 50 61 67 65 72 47 65 74 28 70 50 61 67 65 72 2c PagerGet(pPager,
13e4b 20 31 2c 20 26 70 50 67 48 64 72 29 3b 0a 20 20 1, &pPgHdr);.
13e4c 20 20 61 73 73 65 72 74 28 20 70 50 67 48 64 72 assert( pPgHdr
13e4d 3d 3d 30 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 ==0 || rc==SQLIT
13e4e 45 5f 4f 4b 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 E_OK );.. /*
13e4f 49 66 20 70 61 67 65 20 6f 6e 65 20 77 61 73 20 If page one was
13e50 66 65 74 63 68 65 64 20 73 75 63 63 65 73 73 66 fetched successf
13e51 75 6c 6c 79 2c 20 61 6e 64 20 74 68 69 73 20 66 ully, and this f
13e52 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 20 unction is not.
13e53 20 20 20 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20 ** operating
13e54 69 6e 20 64 69 72 65 63 74 2d 6d 6f 64 65 2c 20 in direct-mode,
13e55 6d 61 6b 65 20 70 61 67 65 20 31 20 77 72 69 74 make page 1 writ
13e56 61 62 6c 65 2e 20 20 57 68 65 6e 20 6e 6f 74 20 able. When not
13e57 69 6e 20 0a 20 20 20 20 2a 2a 20 64 69 72 65 63 in . ** direc
13e58 74 20 6d 6f 64 65 2c 20 70 61 67 65 20 31 20 69 t mode, page 1 i
13e59 73 20 61 6c 77 61 79 73 20 68 65 6c 64 20 69 6e s always held in
13e5a 20 63 61 63 68 65 20 61 6e 64 20 68 65 6e 63 65 cache and hence
13e5b 20 74 68 65 20 50 61 67 65 72 47 65 74 28 29 0a the PagerGet().
13e5c 20 20 20 20 2a 2a 20 61 62 6f 76 65 20 69 73 20 ** above is
13e5d 61 6c 77 61 79 73 20 73 75 63 63 65 73 73 66 75 always successfu
13e5e 6c 20 2d 20 68 65 6e 63 65 20 74 68 65 20 41 4c l - hence the AL
13e5f 57 41 59 53 20 6f 6e 20 72 63 3d 3d 53 51 4c 49 WAYS on rc==SQLI
13e60 54 45 5f 4f 4b 2e 0a 20 20 20 20 2a 2f 0a 20 20 TE_OK.. */.
13e61 20 20 69 66 28 20 21 44 49 52 45 43 54 5f 4d 4f if( !DIRECT_MO
13e62 44 45 20 26 26 20 41 4c 57 41 59 53 28 72 63 3d DE && ALWAYS(rc=
13e63 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 7b 0a 20 =SQLITE_OK) ){.
13e64 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
13e65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 67 48 3PagerWrite(pPgH
13e66 64 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 dr);. }..
13e67 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
13e68 4b 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6e K ){. /* In
13e69 63 72 65 6d 65 6e 74 20 74 68 65 20 76 61 6c 75 crement the valu
13e6a 65 20 6a 75 73 74 20 72 65 61 64 20 61 6e 64 20 e just read and
13e6b 77 72 69 74 65 20 69 74 20 62 61 63 6b 20 74 6f write it back to
13e6c 20 62 79 74 65 20 32 34 2e 20 2a 2f 0a 20 20 20 byte 24. */.
13e6d 20 20 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 change_counte
13e6e 72 20 3d 20 73 71 6c 69 74 65 33 47 65 74 34 62 r = sqlite3Get4b
13e6f 79 74 65 28 28 75 38 2a 29 70 50 61 67 65 72 2d yte((u8*)pPager-
13e70 3e 64 62 46 69 6c 65 56 65 72 73 29 3b 0a 20 20 >dbFileVers);.
13e71 20 20 20 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 change_count
13e72 65 72 2b 2b 3b 0a 20 20 20 20 20 20 70 75 74 33 er++;. put3
13e73 32 62 69 74 73 28 28 28 63 68 61 72 2a 29 70 50 2bits(((char*)pP
13e74 67 48 64 72 2d 3e 70 44 61 74 61 29 2b 32 34 2c gHdr->pData)+24,
13e75 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 29 change_counter)
13e76 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 72 ;.. /* If r
13e77 75 6e 6e 69 6e 67 20 69 6e 20 64 69 72 65 63 74 unning in direct
13e78 20 6d 6f 64 65 2c 20 77 72 69 74 65 20 74 68 65 mode, write the
13e79 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 61 67 contents of pag
13e7a 65 20 31 20 74 6f 20 74 68 65 20 66 69 6c 65 2e e 1 to the file.
13e7b 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 44 49 */. if( DI
13e7c 52 45 43 54 5f 4d 4f 44 45 20 29 7b 0a 20 20 20 RECT_MODE ){.
13e7d 20 20 20 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 const void
13e7e 2a 7a 42 75 66 20 3d 20 70 50 67 48 64 72 2d 3e *zBuf = pPgHdr->
13e7f 70 44 61 74 61 3b 0a 20 20 20 20 20 20 20 20 61 pData;. a
13e80 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 ssert( pPager->d
13e81 62 46 69 6c 65 53 69 7a 65 3e 30 20 29 3b 0a 20 bFileSize>0 );.
13e82 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
13e83 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 te3OsWrite(pPage
13e84 72 2d 3e 66 64 2c 20 7a 42 75 66 2c 20 70 50 61 r->fd, zBuf, pPa
13e85 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 30 ger->pageSize, 0
13e86 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 );. if( r
13e87 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
13e88 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72 pPager
13e89 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e ->changeCountDon
13e8a 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d e = 1;. }
13e8b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
13e8c 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 63 68 pPager->ch
13e8d 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 angeCountDone =
13e8e 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 1;. }. }
13e8f 0a 0a 20 20 20 20 2f 2a 20 52 65 6c 65 61 73 65 .. /* Release
13e90 20 74 68 65 20 70 61 67 65 20 72 65 66 65 72 65 the page refere
13e91 6e 63 65 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 nce. */. sqli
13e92 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 te3PagerUnref(pP
13e93 67 48 64 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 gHdr);. }. ret
13e94 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
13e95 20 53 79 6e 63 20 74 68 65 20 70 61 67 65 72 20 Sync the pager
13e96 66 69 6c 65 20 74 6f 20 64 69 73 6b 2e 20 54 68 file to disk. Th
13e97 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 66 6f is is a no-op fo
13e98 72 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 r in-memory file
13e99 73 0a 2a 2a 20 6f 72 20 70 61 67 65 73 20 77 69 s.** or pages wi
13e9a 74 68 20 74 68 65 20 50 61 67 65 72 2e 6e 6f 53 th the Pager.noS
13e9b 79 6e 63 20 66 6c 61 67 20 73 65 74 2e 0a 2a 2a ync flag set..**
13e9c 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 .** If successfu
13e9d 6c 2c 20 6f 72 20 63 61 6c 6c 65 64 20 6f 6e 20 l, or called on
13e9e 61 20 70 61 67 65 72 20 66 6f 72 20 77 68 69 63 a pager for whic
13e9f 68 20 69 74 20 69 73 20 61 20 6e 6f 2d 6f 70 2c h it is a no-op,
13ea0 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f this.** functio
13ea1 6e 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 n returns SQLITE
13ea2 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 _OK. Otherwise,
13ea3 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 an IO error code
13ea4 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f is returned..*/
13ea5 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
13ea6 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
13ea7 53 79 6e 63 28 50 61 67 65 72 20 2a 70 50 61 67 Sync(Pager *pPag
13ea8 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 er){. int rc;
13ea9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13eaa 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
13eab 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 eturn code */.
13eac 61 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20 29 assert( !MEMDB )
13ead 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e ;. if( pPager->
13eae 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20 72 63 noSync ){. rc
13eaf 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
13eb0 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 }else{. rc =
13eb1 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 50 sqlite3OsSync(pP
13eb2 61 67 65 72 2d 3e 66 64 2c 20 70 50 61 67 65 72 ager->fd, pPager
13eb3 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a 20 ->sync_flags);.
13eb4 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
13eb5 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 }../*.** Sync th
13eb6 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
13eb7 66 6f 72 20 74 68 65 20 70 61 67 65 72 20 70 50 for the pager pP
13eb8 61 67 65 72 2e 20 7a 4d 61 73 74 65 72 20 70 6f ager. zMaster po
13eb9 69 6e 74 73 20 74 6f 20 74 68 65 20 6e 61 6d 65 ints to the name
13eba 0a 2a 2a 20 6f 66 20 61 20 6d 61 73 74 65 72 20 .** of a master
13ebb 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68 61 journal file tha
13ebc 74 20 73 68 6f 75 6c 64 20 62 65 20 77 72 69 74 t should be writ
13ebd 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 69 6e 64 ten into the ind
13ebe 69 76 69 64 75 61 6c 0a 2a 2a 20 6a 6f 75 72 6e ividual.** journ
13ebf 61 6c 20 66 69 6c 65 2e 20 7a 4d 61 73 74 65 72 al file. zMaster
13ec0 20 6d 61 79 20 62 65 20 4e 55 4c 4c 2c 20 77 68 may be NULL, wh
13ec1 69 63 68 20 69 73 20 69 6e 74 65 72 70 72 65 74 ich is interpret
13ec2 65 64 20 61 73 20 6e 6f 20 6d 61 73 74 65 72 0a ed as no master.
13ec3 2a 2a 20 6a 6f 75 72 6e 61 6c 20 28 61 20 73 69 ** journal (a si
13ec4 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 74 72 ngle database tr
13ec5 61 6e 73 61 63 74 69 6f 6e 29 2e 0a 2a 2a 0a 2a ansaction)..**.*
13ec6 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 * This routine e
13ec7 6e 73 75 72 65 73 20 74 68 61 74 3a 0a 2a 2a 0a nsures that:.**.
13ec8 2a 2a 20 20 20 2a 20 54 68 65 20 64 61 74 61 62 ** * The datab
13ec9 61 73 65 20 66 69 6c 65 20 63 68 61 6e 67 65 2d ase file change-
13eca 63 6f 75 6e 74 65 72 20 69 73 20 75 70 64 61 74 counter is updat
13ecb 65 64 2c 0a 2a 2a 20 20 20 2a 20 74 68 65 20 6a ed,.** * the j
13ecc 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 ournal is synced
13ecd 20 28 75 6e 6c 65 73 73 20 74 68 65 20 61 74 6f (unless the ato
13ece 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 mic-write optimi
13ecf 7a 61 74 69 6f 6e 20 69 73 20 75 73 65 64 29 2c zation is used),
13ed0 0a 2a 2a 20 20 20 2a 20 61 6c 6c 20 64 69 72 74 .** * all dirt
13ed1 79 20 70 61 67 65 73 20 61 72 65 20 77 72 69 74 y pages are writ
13ed2 74 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61 62 ten to the datab
13ed3 61 73 65 20 66 69 6c 65 2c 20 0a 2a 2a 20 20 20 ase file, .**
13ed4 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 * the database f
13ed5 69 6c 65 20 69 73 20 74 72 75 6e 63 61 74 65 64 ile is truncated
13ed6 20 28 69 66 20 72 65 71 75 69 72 65 64 29 2c 20 (if required),
13ed7 61 6e 64 0a 2a 2a 20 20 20 2a 20 74 68 65 20 64 and.** * the d
13ed8 61 74 61 62 61 73 65 20 66 69 6c 65 20 73 79 6e atabase file syn
13ed9 63 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ced. .**.** The
13eda 6f 6e 6c 79 20 74 68 69 6e 67 20 74 68 61 74 20 only thing that
13edb 72 65 6d 61 69 6e 73 20 74 6f 20 63 6f 6d 6d 69 remains to commi
13edc 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f t the transactio
13edd 6e 20 69 73 20 74 6f 20 66 69 6e 61 6c 69 7a 65 n is to finalize
13ede 20 0a 2a 2a 20 28 64 65 6c 65 74 65 2c 20 74 72 .** (delete, tr
13edf 75 6e 63 61 74 65 20 6f 72 20 7a 65 72 6f 20 74 uncate or zero t
13ee0 68 65 20 66 69 72 73 74 20 70 61 72 74 20 6f 66 he first part of
13ee1 29 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 ) the journal fi
13ee2 6c 65 20 28 6f 72 20 0a 2a 2a 20 64 65 6c 65 74 le (or .** delet
13ee3 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 e the master jou
13ee4 72 6e 61 6c 20 66 69 6c 65 20 69 66 20 73 70 65 rnal file if spe
13ee5 63 69 66 69 65 64 29 2e 0a 2a 2a 0a 2a 2a 20 4e cified)..**.** N
13ee6 6f 74 65 20 74 68 61 74 20 69 66 20 7a 4d 61 73 ote that if zMas
13ee7 74 65 72 3d 3d 4e 55 4c 4c 2c 20 74 68 69 73 20 ter==NULL, this
13ee8 64 6f 65 73 20 6e 6f 74 20 6f 76 65 72 77 72 69 does not overwri
13ee9 74 65 20 61 20 70 72 65 76 69 6f 75 73 20 76 61 te a previous va
13eea 6c 75 65 0a 2a 2a 20 70 61 73 73 65 64 20 74 6f lue.** passed to
13eeb 20 61 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72 an sqlite3Pager
13eec 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 CommitPhaseOne()
13eed 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 call..**.** If
13eee 74 68 65 20 66 69 6e 61 6c 20 70 61 72 61 6d 65 the final parame
13eef 74 65 72 20 2d 20 6e 6f 53 79 6e 63 20 2d 20 69 ter - noSync - i
13ef0 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 s true, then the
13ef1 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 database file i
13ef2 74 73 65 6c 66 0a 2a 2a 20 69 73 20 6e 6f 74 20 tself.** is not
13ef3 73 79 6e 63 65 64 2e 20 54 68 65 20 63 61 6c 6c synced. The call
13ef4 65 72 20 6d 75 73 74 20 63 61 6c 6c 20 73 71 6c er must call sql
13ef5 69 74 65 33 50 61 67 65 72 53 79 6e 63 28 29 20 ite3PagerSync()
13ef6 64 69 72 65 63 74 6c 79 20 74 6f 0a 2a 2a 20 73 directly to.** s
13ef7 79 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 65 ync the database
13ef8 20 66 69 6c 65 20 62 65 66 6f 72 65 20 63 61 6c file before cal
13ef9 6c 69 6e 67 20 43 6f 6d 6d 69 74 50 68 61 73 65 ling CommitPhase
13efa 54 77 6f 28 29 20 74 6f 20 64 65 6c 65 74 65 20 Two() to delete
13efb 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 the.** journal f
13efc 69 6c 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 ile in this case
13efd 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
13efe 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
13eff 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f agerCommitPhaseO
13f00 6e 65 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 ne(. Pager *pPa
13f01 67 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 ger,
13f02 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72 20 6f /* Pager o
13f03 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 bject */. const
13f04 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 2c 20 char *zMaster,
13f05 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 /* If
13f06 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 20 6d not NULL, the m
13f07 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 aster journal na
13f08 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 53 79 me */. int noSy
13f09 6e 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 nc
13f0a 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
13f0b 74 6f 20 6f 6d 69 74 20 74 68 65 20 78 53 79 6e to omit the xSyn
13f0c 63 20 6f 6e 20 74 68 65 20 64 62 20 66 69 6c 65 c on the db file
13f0d 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 */.){. int rc
13f0e 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 = SQLITE_OK;
13f0f 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 /* Retu
13f10 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f 2a rn code */.. /*
13f11 20 54 68 65 20 64 62 4f 72 69 67 53 69 7a 65 20 The dbOrigSize
13f12 69 73 20 6e 65 76 65 72 20 73 65 74 20 69 66 20 is never set if
13f13 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 4f 46 46 journal_mode=OFF
13f14 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 */. assert( pP
13f15 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 ager->journalMod
13f16 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c e!=PAGER_JOURNAL
13f17 4d 4f 44 45 5f 4f 46 46 20 7c 7c 20 70 50 61 67 MODE_OFF || pPag
13f18 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 3d 3d er->dbOrigSize==
13f19 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 20 0 );.. /* If a
13f1a 70 72 69 6f 72 20 65 72 72 6f 72 20 6f 63 63 75 prior error occu
13f1b 72 72 65 64 2c 20 74 68 69 73 20 72 6f 75 74 69 rred, this routi
13f1c 6e 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 ne should not be
13f1d 20 63 61 6c 6c 65 64 2e 20 20 52 4f 4c 4c 42 41 called. ROLLBA
13f1e 43 4b 0a 20 20 2a 2a 20 69 73 20 74 68 65 20 61 CK. ** is the a
13f1f 70 70 72 6f 70 72 69 61 74 65 20 72 65 73 70 6f ppropriate respo
13f20 6e 73 65 20 74 6f 20 61 6e 20 65 72 72 6f 72 2c nse to an error,
13f21 20 6e 6f 74 20 43 4f 4d 4d 49 54 2e 20 20 47 75 not COMMIT. Gu
13f22 61 72 64 20 61 67 61 69 6e 73 74 0a 20 20 2a 2a ard against. **
13f23 20 63 6f 64 69 6e 67 20 65 72 72 6f 72 73 20 62 coding errors b
13f24 79 20 72 65 70 65 61 74 69 6e 67 20 74 68 65 20 y repeating the
13f25 70 72 69 6f 72 20 65 72 72 6f 72 2e 20 2a 2f 0a prior error. */.
13f26 20 20 69 66 28 20 4e 45 56 45 52 28 70 50 61 67 if( NEVER(pPag
13f27 65 72 2d 3e 65 72 72 43 6f 64 65 29 20 29 20 72 er->errCode) ) r
13f28 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 eturn pPager->er
13f29 72 43 6f 64 65 3b 0a 0a 20 20 50 41 47 45 52 54 rCode;.. PAGERT
13f2a 52 41 43 45 28 28 22 44 41 54 41 42 41 53 45 20 RACE(("DATABASE
13f2b 53 59 4e 43 3a 20 46 69 6c 65 3d 25 73 20 7a 4d SYNC: File=%s zM
13f2c 61 73 74 65 72 3d 25 73 20 6e 53 69 7a 65 3d 25 aster=%s nSize=%
13f2d 64 5c 6e 22 2c 20 0a 20 20 20 20 20 20 70 50 61 d\n", . pPa
13f2e 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 ger->zFilename,
13f2f 7a 4d 61 73 74 65 72 2c 20 70 50 61 67 65 72 2d zMaster, pPager-
13f30 3e 64 62 53 69 7a 65 29 29 3b 0a 0a 20 20 69 66 >dbSize));.. if
13f31 28 20 4d 45 4d 44 42 20 26 26 20 70 50 61 67 65 ( MEMDB && pPage
13f32 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 29 7b r->dbModified ){
13f33 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 . /* If this
13f34 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 is an in-memory
13f35 64 62 2c 20 6f 72 20 6e 6f 20 70 61 67 65 73 20 db, or no pages
13f36 68 61 76 65 20 62 65 65 6e 20 77 72 69 74 74 65 have been writte
13f37 6e 20 74 6f 2c 20 6f 72 20 74 68 69 73 0a 20 20 n to, or this.
13f38 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 68 61 ** function ha
13f39 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 s already been c
13f3a 61 6c 6c 65 64 2c 20 69 74 20 69 73 20 6d 6f 73 alled, it is mos
13f3b 74 6c 79 20 61 20 6e 6f 2d 6f 70 2e 20 20 48 6f tly a no-op. Ho
13f3c 77 65 76 65 72 2c 20 61 6e 79 0a 20 20 20 20 2a wever, any. *
13f3d 2a 20 62 61 63 6b 75 70 20 69 6e 20 70 72 6f 67 * backup in prog
13f3e 72 65 73 73 20 6e 65 65 64 73 20 74 6f 20 62 65 ress needs to be
13f3f 20 72 65 73 74 61 72 74 65 64 2e 0a 20 20 20 20 restarted..
13f40 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 61 */. sqlite3Ba
13f41 63 6b 75 70 52 65 73 74 61 72 74 28 70 50 61 67 ckupRestart(pPag
13f42 65 72 2d 3e 70 42 61 63 6b 75 70 29 3b 0a 20 20 er->pBackup);.
13f43 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67 65 72 }else if( pPager
13f44 2d 3e 73 74 61 74 65 21 3d 50 41 47 45 52 5f 53 ->state!=PAGER_S
13f45 59 4e 43 45 44 20 26 26 20 70 50 61 67 65 72 2d YNCED && pPager-
13f46 3e 64 62 4d 6f 64 69 66 69 65 64 20 29 7b 0a 0a >dbModified ){..
13f47 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f /* The follo
13f48 77 69 6e 67 20 62 6c 6f 63 6b 20 75 70 64 61 74 wing block updat
13f49 65 73 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f es the change-co
13f4a 75 6e 74 65 72 2e 20 45 78 61 63 74 6c 79 20 68 unter. Exactly h
13f4b 6f 77 20 69 74 0a 20 20 20 20 2a 2a 20 64 6f 65 ow it. ** doe
13f4c 73 20 74 68 69 73 20 64 65 70 65 6e 64 73 20 6f s this depends o
13f4d 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 n whether or not
13f4e 20 74 68 65 20 61 74 6f 6d 69 63 2d 75 70 64 61 the atomic-upda
13f4f 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 0a te optimization.
13f50 20 20 20 20 2a 2a 20 77 61 73 20 65 6e 61 62 6c ** was enabl
13f51 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 ed at compile ti
13f52 6d 65 2c 20 61 6e 64 20 69 66 20 74 68 69 73 20 me, and if this
13f53 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 65 65 74 transaction meet
13f54 73 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 72 75 s the . ** ru
13f55 6e 74 69 6d 65 20 63 72 69 74 65 72 69 61 20 74 ntime criteria t
13f56 6f 20 75 73 65 20 74 68 65 20 6f 70 65 72 61 74 o use the operat
13f57 69 6f 6e 3a 20 0a 20 20 20 20 2a 2a 0a 20 20 20 ion: . **.
13f58 20 2a 2a 20 20 20 20 2a 20 54 68 65 20 66 69 6c ** * The fil
13f59 65 2d 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 e-system support
13f5a 73 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 s the atomic-wri
13f5b 74 65 20 70 72 6f 70 65 72 74 79 20 66 6f 72 0a te property for.
13f5c 20 20 20 20 2a 2a 20 20 20 20 20 20 62 6c 6f 63 ** bloc
13f5d 6b 73 20 6f 66 20 73 69 7a 65 20 70 61 67 65 2d ks of size page-
13f5e 73 69 7a 65 2c 20 61 6e 64 20 0a 20 20 20 20 2a size, and . *
13f5f 2a 20 20 20 20 2a 20 54 68 69 73 20 63 6f 6d 6d * * This comm
13f60 69 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f it is not part o
13f61 66 20 61 20 6d 75 6c 74 69 2d 66 69 6c 65 20 74 f a multi-file t
13f62 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 6e 64 0a ransaction, and.
13f63 20 20 20 20 2a 2a 20 20 20 20 2a 20 45 78 61 63 ** * Exac
13f64 74 6c 79 20 6f 6e 65 20 70 61 67 65 20 68 61 73 tly one page has
13f65 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 61 been modified a
13f66 6e 64 20 73 74 6f 72 65 20 69 6e 20 74 68 65 20 nd store in the
13f67 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 journal file..
13f68 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 **. ** If t
13f69 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 he optimization
13f6a 77 61 73 20 6e 6f 74 20 65 6e 61 62 6c 65 64 20 was not enabled
13f6b 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2c at compile time,
13f6c 20 74 68 65 6e 20 74 68 65 0a 20 20 20 20 2a 2a then the. **
13f6d 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e pager_incr_chan
13f6e 67 65 63 6f 75 6e 74 65 72 28 29 20 66 75 6e 63 gecounter() func
13f6f 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 tion is called t
13f70 6f 20 75 70 64 61 74 65 20 74 68 65 20 63 68 61 o update the cha
13f71 6e 67 65 0a 20 20 20 20 2a 2a 20 63 6f 75 6e 74 nge. ** count
13f72 65 72 20 69 6e 20 27 69 6e 64 69 72 65 63 74 2d er in 'indirect-
13f73 6d 6f 64 65 27 2e 20 49 66 20 74 68 65 20 6f 70 mode'. If the op
13f74 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 63 6f timization is co
13f75 6d 70 69 6c 65 64 20 69 6e 20 62 75 74 0a 20 20 mpiled in but.
13f76 20 20 2a 2a 20 69 73 20 6e 6f 74 20 61 70 70 6c ** is not appl
13f77 69 63 61 62 6c 65 20 74 6f 20 74 68 69 73 20 74 icable to this t
13f78 72 61 6e 73 61 63 74 69 6f 6e 2c 20 63 61 6c 6c ransaction, call
13f79 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43 sqlite3JournalC
13f7a 72 65 61 74 65 28 29 0a 20 20 20 20 2a 2a 20 74 reate(). ** t
13f7b 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 o make sure the
13f7c 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 73 journal file has
13f7d 20 61 63 74 75 61 6c 6c 79 20 62 65 65 6e 20 63 actually been c
13f7e 72 65 61 74 65 64 2c 20 74 68 65 6e 20 63 61 6c reated, then cal
13f7f 6c 0a 20 20 20 20 2a 2a 20 70 61 67 65 72 5f 69 l. ** pager_i
13f80 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 ncr_changecounte
13f81 72 28 29 20 74 6f 20 75 70 64 61 74 65 20 74 68 r() to update th
13f82 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 e change-counter
13f83 20 69 6e 20 69 6e 64 69 72 65 63 74 0a 20 20 20 in indirect.
13f84 20 2a 2a 20 6d 6f 64 65 2e 20 0a 20 20 20 20 2a ** mode. . *
13f85 2a 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72 77 69 *. ** Otherwi
13f86 73 65 2c 20 69 66 20 74 68 65 20 6f 70 74 69 6d se, if the optim
13f87 69 7a 61 74 69 6f 6e 20 69 73 20 62 6f 74 68 20 ization is both
13f88 65 6e 61 62 6c 65 64 20 61 6e 64 20 61 70 70 6c enabled and appl
13f89 69 63 61 62 6c 65 2c 0a 20 20 20 20 2a 2a 20 74 icable,. ** t
13f8a 68 65 6e 20 63 61 6c 6c 20 70 61 67 65 72 5f 69 hen call pager_i
13f8b 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 ncr_changecounte
13f8c 72 28 29 20 74 6f 20 75 70 64 61 74 65 20 74 68 r() to update th
13f8d 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 e change-counter
13f8e 0a 20 20 20 20 2a 2a 20 69 6e 20 27 64 69 72 65 . ** in 'dire
13f8f 63 74 27 20 6d 6f 64 65 2e 20 49 6e 20 74 68 69 ct' mode. In thi
13f90 73 20 63 61 73 65 20 74 68 65 20 6a 6f 75 72 6e s case the journ
13f91 61 6c 20 66 69 6c 65 20 77 69 6c 6c 20 6e 65 76 al file will nev
13f92 65 72 20 62 65 0a 20 20 20 20 2a 2a 20 63 72 65 er be. ** cre
13f93 61 74 65 64 20 66 6f 72 20 74 68 69 73 20 74 72 ated for this tr
13f94 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 20 20 2a ansaction.. *
13f95 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
13f96 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 ENABLE_ATOMIC_WR
13f97 49 54 45 0a 20 20 20 20 50 67 48 64 72 20 2a 70 ITE. PgHdr *p
13f98 50 67 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 Pg;. assert(
13f99 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a isOpen(pPager->j
13f9a 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a fd) || pPager->j
13f9b 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 ournalMode==PAGE
13f9c 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 R_JOURNALMODE_OF
13f9d 46 20 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 4d F );. if( !zM
13f9e 61 73 74 65 72 20 26 26 20 69 73 4f 70 65 6e 28 aster && isOpen(
13f9f 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 0a 20 20 pPager->jfd) .
13fa0 20 20 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f && pPager->jo
13fa1 75 72 6e 61 6c 4f 66 66 3d 3d 6a 72 6e 6c 42 75 urnalOff==jrnlBu
13fa2 66 66 65 72 53 69 7a 65 28 70 50 61 67 65 72 29 fferSize(pPager)
13fa3 20 0a 20 20 20 20 20 26 26 20 70 50 61 67 65 72 . && pPager
13fa4 2d 3e 64 62 53 69 7a 65 3e 3d 70 50 61 67 65 72 ->dbSize>=pPager
13fa5 2d 3e 64 62 46 69 6c 65 53 69 7a 65 0a 20 20 20 ->dbFileSize.
13fa6 20 20 26 26 20 28 30 3d 3d 28 70 50 67 20 3d 20 && (0==(pPg =
13fa7 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 69 72 sqlite3PcacheDir
13fa8 74 79 4c 69 73 74 28 70 50 61 67 65 72 2d 3e 70 tyList(pPager->p
13fa9 50 43 61 63 68 65 29 29 20 7c 7c 20 30 3d 3d 70 PCache)) || 0==p
13faa 50 67 2d 3e 70 44 69 72 74 79 29 0a 20 20 20 20 Pg->pDirty).
13fab 29 7b 0a 20 20 20 20 20 20 2f 2a 20 55 70 64 61 ){. /* Upda
13fac 74 65 20 74 68 65 20 64 62 20 66 69 6c 65 20 63 te the db file c
13fad 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 76 69 hange counter vi
13fae 61 20 74 68 65 20 64 69 72 65 63 74 2d 77 72 69 a the direct-wri
13faf 74 65 20 6d 65 74 68 6f 64 2e 20 54 68 65 20 0a te method. The .
13fb0 20 20 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 ** followi
13fb1 6e 67 20 63 61 6c 6c 20 77 69 6c 6c 20 6d 6f 64 ng call will mod
13fb2 69 66 79 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 ify the in-memor
13fb3 79 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e y representation
13fb4 20 6f 66 20 70 61 67 65 20 31 20 0a 20 20 20 20 of page 1 .
13fb5 20 20 2a 2a 20 74 6f 20 69 6e 63 6c 75 64 65 20 ** to include
13fb6 74 68 65 20 75 70 64 61 74 65 64 20 63 68 61 6e the updated chan
13fb7 67 65 20 63 6f 75 6e 74 65 72 20 61 6e 64 20 74 ge counter and t
13fb8 68 65 6e 20 77 72 69 74 65 20 70 61 67 65 20 31 hen write page 1
13fb9 20 0a 20 20 20 20 20 20 2a 2a 20 64 69 72 65 63 . ** direc
13fba 74 6c 79 20 74 6f 20 74 68 65 20 64 61 74 61 62 tly to the datab
13fbb 61 73 65 20 66 69 6c 65 2e 20 42 65 63 61 75 73 ase file. Becaus
13fbc 65 20 6f 66 20 74 68 65 20 61 74 6f 6d 69 63 2d e of the atomic-
13fbd 77 72 69 74 65 20 0a 20 20 20 20 20 20 2a 2a 20 write . **
13fbe 70 72 6f 70 65 72 74 79 20 6f 66 20 74 68 65 20 property of the
13fbf 68 6f 73 74 20 66 69 6c 65 2d 73 79 73 74 65 6d host file-system
13fc0 2c 20 74 68 69 73 20 69 73 20 73 61 66 65 2e 0a , this is safe..
13fc1 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 */. r
13fc2 63 20 3d 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 c = pager_incr_c
13fc3 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 70 50 61 hangecounter(pPa
13fc4 67 65 72 2c 20 31 29 3b 0a 20 20 20 20 7d 65 6c ger, 1);. }el
13fc5 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 se{. rc = s
13fc6 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 qlite3JournalCre
13fc7 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 ate(pPager->jfd)
13fc8 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d ;. if( rc==
13fc9 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
13fca 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f rc = pager_
13fcb 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 incr_changecount
13fcc 65 72 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 er(pPager, 0);.
13fcd 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6c }. }.#el
13fce 73 65 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 se. rc = page
13fcf 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 r_incr_changecou
13fd0 6e 74 65 72 28 70 50 61 67 65 72 2c 20 30 29 3b nter(pPager, 0);
13fd1 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20 .#endif. if(
13fd2 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 rc!=SQLITE_OK )
13fd3 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73 goto commit_phas
13fd4 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 0a 20 20 20 e_one_exit;..
13fd5 20 2f 2a 20 49 66 20 74 68 69 73 20 74 72 61 6e /* If this tran
13fd6 73 61 63 74 69 6f 6e 20 68 61 73 20 6d 61 64 65 saction has made
13fd7 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 6d the database sm
13fd8 61 6c 6c 65 72 2c 20 74 68 65 6e 20 61 6c 6c 20 aller, then all
13fd9 70 61 67 65 73 0a 20 20 20 20 2a 2a 20 62 65 69 pages. ** bei
13fda 6e 67 20 64 69 73 63 61 72 64 65 64 20 62 79 20 ng discarded by
13fdb 74 68 65 20 74 72 75 6e 63 61 74 69 6f 6e 20 6d the truncation m
13fdc 75 73 74 20 62 65 20 77 72 69 74 74 65 6e 20 74 ust be written t
13fdd 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 o the journal.
13fde 20 20 2a 2a 20 66 69 6c 65 2e 20 54 68 69 73 20 ** file. This
13fdf 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 can only happen
13fe0 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d in auto-vacuum m
13fe1 6f 64 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 ode.. **.
13fe2 2a 2a 20 42 65 66 6f 72 65 20 72 65 61 64 69 6e ** Before readin
13fe3 67 20 74 68 65 20 70 61 67 65 73 20 77 69 74 68 g the pages with
13fe4 20 70 61 67 65 20 6e 75 6d 62 65 72 73 20 6c 61 page numbers la
13fe5 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 0a 20 rger than the .
13fe6 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 76 61 ** current va
13fe7 6c 75 65 20 6f 66 20 50 61 67 65 72 2e 64 62 53 lue of Pager.dbS
13fe8 69 7a 65 2c 20 73 65 74 20 64 62 53 69 7a 65 20 ize, set dbSize
13fe9 62 61 63 6b 20 74 6f 20 74 68 65 20 76 61 6c 75 back to the valu
13fea 65 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 69 74 e. ** that it
13feb 20 74 6f 6f 6b 20 61 74 20 74 68 65 20 73 74 61 took at the sta
13fec 72 74 20 6f 66 20 74 68 65 20 74 72 61 6e 73 61 rt of the transa
13fed 63 74 69 6f 6e 2e 20 4f 74 68 65 72 77 69 73 65 ction. Otherwise
13fee 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 61 6c , the. ** cal
13fef 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 ls to sqlite3Pag
13ff0 65 72 47 65 74 28 29 20 72 65 74 75 72 6e 20 7a erGet() return z
13ff1 65 72 6f 65 64 20 70 61 67 65 73 20 69 6e 73 74 eroed pages inst
13ff2 65 61 64 20 6f 66 20 0a 20 20 20 20 2a 2a 20 72 ead of . ** r
13ff3 65 61 64 69 6e 67 20 64 61 74 61 20 66 72 6f 6d eading data from
13ff4 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
13ff5 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a le.. **. *
13ff6 2a 20 57 68 65 6e 20 6a 6f 75 72 6e 61 6c 5f 6d * When journal_m
13ff7 6f 64 65 3d 3d 4f 46 46 20 74 68 65 20 64 62 4f ode==OFF the dbO
13ff8 72 69 67 53 69 7a 65 20 69 73 20 61 6c 77 61 79 rigSize is alway
13ff9 73 20 7a 65 72 6f 2c 20 73 6f 20 74 68 69 73 0a s zero, so this.
13ffa 20 20 20 20 2a 2a 20 62 6c 6f 63 6b 20 6e 65 76 ** block nev
13ffb 65 72 20 72 75 6e 73 20 69 66 20 6a 6f 75 72 6e er runs if journ
13ffc 61 6c 5f 6d 6f 64 65 3d 4f 46 46 2e 0a 20 20 20 al_mode=OFF..
13ffd 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 */.#ifndef SQLI
13ffe 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
13fff 55 4d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 UM. if( pPage
14000 72 2d 3e 64 62 53 69 7a 65 3c 70 50 61 67 65 72 r->dbSize<pPager
14001 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 0a 20 20 ->dbOrigSize .
14002 20 20 20 26 26 20 41 4c 57 41 59 53 28 70 50 61 && ALWAYS(pPa
14003 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 ger->journalMode
14004 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d !=PAGER_JOURNALM
14005 4f 44 45 5f 4f 46 46 29 0a 20 20 20 20 29 7b 0a ODE_OFF). ){.
14006 20 20 20 20 20 20 50 67 6e 6f 20 69 3b 20 20 20 Pgno i;
14007 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14008 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14009 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 /* Iterator vari
1400a 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 63 6f able */. co
1400b 6e 73 74 20 50 67 6e 6f 20 69 53 6b 69 70 20 3d nst Pgno iSkip =
1400c 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 PAGER_MJ_PGNO(p
1400d 50 61 67 65 72 29 3b 20 2f 2a 20 50 65 6e 64 69 Pager); /* Pendi
1400e 6e 67 20 6c 6f 63 6b 20 70 61 67 65 20 2a 2f 0a ng lock page */.
1400f 20 20 20 20 20 20 63 6f 6e 73 74 20 50 67 6e 6f const Pgno
14010 20 64 62 53 69 7a 65 20 3d 20 70 50 61 67 65 72 dbSize = pPager
14011 2d 3e 64 62 53 69 7a 65 3b 20 20 20 20 20 20 20 ->dbSize;
14012 2f 2a 20 44 61 74 61 62 61 73 65 20 69 6d 61 67 /* Database imag
14013 65 20 73 69 7a 65 20 2a 2f 20 0a 20 20 20 20 20 e size */ .
14014 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 pPager->dbSize
14015 3d 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 = pPager->dbOrig
14016 53 69 7a 65 3b 0a 20 20 20 20 20 20 66 6f 72 28 Size;. for(
14017 20 69 3d 64 62 53 69 7a 65 2b 31 3b 20 69 3c 3d i=dbSize+1; i<=
14018 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 pPager->dbOrigSi
14019 7a 65 3b 20 69 2b 2b 20 29 7b 0a 20 20 20 20 20 ze; i++ ){.
1401a 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 42 if( !sqlite3B
1401b 69 74 76 65 63 54 65 73 74 28 70 50 61 67 65 72 itvecTest(pPager
1401c 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 69 29 ->pInJournal, i)
1401d 20 26 26 20 69 21 3d 69 53 6b 69 70 20 29 7b 0a && i!=iSkip ){.
1401e 20 20 20 20 20 20 20 20 20 20 50 67 48 64 72 20 PgHdr
1401f 2a 70 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 *pPage;
14020 20 20 20 20 2f 2a 20 50 61 67 65 20 74 6f 20 6a /* Page to j
14021 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 20 20 20 20 ournal */.
14022 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
14023 50 61 67 65 72 47 65 74 28 70 50 61 67 65 72 2c PagerGet(pPager,
14024 20 69 2c 20 26 70 50 61 67 65 29 3b 0a 20 20 20 i, &pPage);.
14025 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 if( rc!=S
14026 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 QLITE_OK ) goto
14027 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 commit_phase_one
14028 5f 65 78 69 74 3b 0a 20 20 20 20 20 20 20 20 20 _exit;.
14029 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
1402a 65 72 57 72 69 74 65 28 70 50 61 67 65 29 3b 0a erWrite(pPage);.
1402b 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
1402c 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 61 67 3PagerUnref(pPag
1402d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 e);. if
1402e 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1402f 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 ) goto commit_ph
14030 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20 20 ase_one_exit;.
14031 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 20 }. }
14032 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 . pPager->d
14033 62 53 69 7a 65 20 3d 20 64 62 53 69 7a 65 3b 0a bSize = dbSize;.
14034 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 }.#endif..
14035 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6d /* Write the m
14036 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 aster journal na
14037 6d 65 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 me into the jour
14038 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 61 20 6d nal file. If a m
14039 61 73 74 65 72 20 0a 20 20 20 20 2a 2a 20 6a 6f aster . ** jo
1403a 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 urnal file name
1403b 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e has already been
1403c 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 written to the
1403d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 0a 20 journal file, .
1403e 20 20 20 2a 2a 20 6f 72 20 69 66 20 7a 4d 61 73 ** or if zMas
1403f 74 65 72 20 69 73 20 4e 55 4c 4c 20 28 6e 6f 20 ter is NULL (no
14040 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 29 2c master journal),
14041 20 74 68 65 6e 20 74 68 69 73 20 63 61 6c 6c 20 then this call
14042 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 20 20 is a no-op..
14043 2a 2f 0a 20 20 20 20 72 63 20 3d 20 77 72 69 74 */. rc = writ
14044 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 70 eMasterJournal(p
14045 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 29 3b Pager, zMaster);
14046 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
14047 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f ITE_OK ) goto co
14048 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 mmit_phase_one_e
14049 78 69 74 3b 0a 0a 20 20 20 20 2f 2a 20 53 79 6e xit;.. /* Syn
1404a 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 c the journal fi
1404b 6c 65 2e 20 49 66 20 74 68 65 20 61 74 6f 6d 69 le. If the atomi
1404c 63 2d 75 70 64 61 74 65 20 6f 70 74 69 6d 69 7a c-update optimiz
1404d 61 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 0a 20 ation is being.
1404e 20 20 20 2a 2a 20 75 73 65 64 2c 20 74 68 69 73 ** used, this
1404f 20 63 61 6c 6c 20 77 69 6c 6c 20 6e 6f 74 20 63 call will not c
14050 72 65 61 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 reate the journa
14051 6c 20 66 69 6c 65 20 6f 72 20 70 65 72 66 6f 72 l file or perfor
14052 6d 20 61 6e 79 0a 20 20 20 20 2a 2a 20 72 65 61 m any. ** rea
14053 6c 20 49 4f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 l IO.. */.
14054 20 72 63 20 3d 20 73 79 6e 63 4a 6f 75 72 6e 61 rc = syncJourna
14055 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69 l(pPager);. i
14056 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
14057 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 ) goto commit_p
14058 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 0a hase_one_exit;..
14059 20 20 20 20 2f 2a 20 57 72 69 74 65 20 61 6c 6c /* Write all
1405a 20 64 69 72 74 79 20 70 61 67 65 73 20 74 6f 20 dirty pages to
1405b 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
1405c 65 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 e. */. rc = p
1405d 61 67 65 72 5f 77 72 69 74 65 5f 70 61 67 65 6c ager_write_pagel
1405e 69 73 74 28 73 71 6c 69 74 65 33 50 63 61 63 68 ist(sqlite3Pcach
1405f 65 44 69 72 74 79 4c 69 73 74 28 70 50 61 67 65 eDirtyList(pPage
14060 72 2d 3e 70 50 43 61 63 68 65 29 29 3b 0a 20 20 r->pPCache));.
14061 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
14062 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61 73 73 _OK ){. ass
14063 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f ert( rc!=SQLITE_
14064 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 29 3b IOERR_BLOCKED );
14065 0a 20 20 20 20 20 20 67 6f 74 6f 20 63 6f 6d 6d . goto comm
14066 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 it_phase_one_exi
14067 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c t;. }. sql
14068 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 6e 41 ite3PcacheCleanA
14069 6c 6c 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 ll(pPager->pPCac
1406a 68 65 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 he);.. /* If
1406b 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b the file on disk
1406c 20 69 73 20 6e 6f 74 20 74 68 65 20 73 61 6d 65 is not the same
1406d 20 73 69 7a 65 20 61 73 20 74 68 65 20 64 61 74 size as the dat
1406e 61 62 61 73 65 20 69 6d 61 67 65 2c 0a 20 20 20 abase image,.
1406f 20 2a 2a 20 74 68 65 6e 20 75 73 65 20 70 61 67 ** then use pag
14070 65 72 5f 74 72 75 6e 63 61 74 65 20 74 6f 20 67 er_truncate to g
14071 72 6f 77 20 6f 72 20 73 68 72 69 6e 6b 20 74 68 row or shrink th
14072 65 20 66 69 6c 65 20 68 65 72 65 2e 0a 20 20 20 e file here..
14073 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61 67 */. if( pPag
14074 65 72 2d 3e 64 62 53 69 7a 65 21 3d 70 50 61 67 er->dbSize!=pPag
14075 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 29 er->dbFileSize )
14076 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 6e 4e 65 {. Pgno nNe
14077 77 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69 w = pPager->dbSi
14078 7a 65 20 2d 20 28 70 50 61 67 65 72 2d 3e 64 62 ze - (pPager->db
14079 53 69 7a 65 3d 3d 50 41 47 45 52 5f 4d 4a 5f 50 Size==PAGER_MJ_P
1407a 47 4e 4f 28 70 50 61 67 65 72 29 29 3b 0a 20 20 GNO(pPager));.
1407b 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
1407c 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 er->state>=PAGER
1407d 5f 45 58 43 4c 55 53 49 56 45 20 29 3b 0a 20 20 _EXCLUSIVE );.
1407e 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 74 rc = pager_t
1407f 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2c 20 runcate(pPager,
14080 6e 4e 65 77 29 3b 0a 20 20 20 20 20 20 69 66 28 nNew);. if(
14081 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
14082 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 goto commit_pha
14083 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20 20 20 se_one_exit;.
14084 20 7d 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 61 6c }.. /* Final
14085 6c 79 2c 20 73 79 6e 63 20 74 68 65 20 64 61 74 ly, sync the dat
14086 61 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 abase file. */.
14087 20 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e if( !pPager->
14088 6e 6f 53 79 6e 63 20 26 26 20 21 6e 6f 53 79 6e noSync && !noSyn
14089 63 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 c ){. rc =
1408a 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 50 sqlite3OsSync(pP
1408b 61 67 65 72 2d 3e 66 64 2c 20 70 50 61 67 65 72 ager->fd, pPager
1408c 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a 20 ->sync_flags);.
1408d 20 20 20 7d 0a 20 20 20 20 49 4f 54 52 41 43 45 }. IOTRACE
1408e 28 28 22 44 42 53 59 4e 43 20 25 70 5c 6e 22 2c (("DBSYNC %p\n",
1408f 20 70 50 61 67 65 72 29 29 0a 0a 20 20 20 20 70 pPager)).. p
14090 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 Pager->state = P
14091 41 47 45 52 5f 53 59 4e 43 45 44 3b 0a 20 20 7d AGER_SYNCED;. }
14092 0a 0a 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f ..commit_phase_o
14093 6e 65 5f 65 78 69 74 3a 0a 20 20 72 65 74 75 72 ne_exit:. retur
14094 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 n rc;.}.../*.**
14095 57 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 When this functi
14096 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 on is called, th
14097 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
14098 68 61 73 20 62 65 65 6e 20 63 6f 6d 70 6c 65 74 has been complet
14099 65 6c 79 0a 2a 2a 20 75 70 64 61 74 65 64 20 74 ely.** updated t
1409a 6f 20 72 65 66 6c 65 63 74 20 74 68 65 20 63 68 o reflect the ch
1409b 61 6e 67 65 73 20 6d 61 64 65 20 62 79 20 74 68 anges made by th
1409c 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 e current transa
1409d 63 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 73 79 6e ction and.** syn
1409e 63 65 64 20 74 6f 20 64 69 73 6b 2e 20 54 68 65 ced to disk. The
1409f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73 74 journal file st
140a0 69 6c 6c 20 65 78 69 73 74 73 20 69 6e 20 74 68 ill exists in th
140a1 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 0a 2a e file-system .*
140a2 2a 20 74 68 6f 75 67 68 2c 20 61 6e 64 20 69 66 * though, and if
140a3 20 61 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 a failure occur
140a4 73 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 20 s at this point
140a5 69 74 20 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c it will eventual
140a6 6c 79 0a 2a 2a 20 62 65 20 75 73 65 64 20 61 73 ly.** be used as
140a7 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 61 a hot-journal a
140a8 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 nd the current t
140a9 72 61 6e 73 61 63 74 69 6f 6e 20 72 6f 6c 6c 65 ransaction rolle
140aa 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 d back..**.** Th
140ab 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 69 6e 61 is function fina
140ac 6c 69 7a 65 73 20 74 68 65 20 6a 6f 75 72 6e 61 lizes the journa
140ad 6c 20 66 69 6c 65 2c 20 65 69 74 68 65 72 20 62 l file, either b
140ae 79 20 64 65 6c 65 74 69 6e 67 2c 20 0a 2a 2a 20 y deleting, .**
140af 74 72 75 6e 63 61 74 69 6e 67 20 6f 72 20 70 61 truncating or pa
140b0 72 74 69 61 6c 6c 79 20 7a 65 72 6f 69 6e 67 20 rtially zeroing
140b1 69 74 2c 20 73 6f 20 74 68 61 74 20 69 74 20 63 it, so that it c
140b2 61 6e 6e 6f 74 20 62 65 20 75 73 65 64 20 0a 2a annot be used .*
140b3 2a 20 66 6f 72 20 68 6f 74 2d 6a 6f 75 72 6e 61 * for hot-journa
140b4 6c 20 72 6f 6c 6c 62 61 63 6b 2e 20 4f 6e 63 65 l rollback. Once
140b5 20 74 68 69 73 20 69 73 20 64 6f 6e 65 20 74 68 this is done th
140b6 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 e transaction is
140b7 0a 2a 2a 20 69 72 72 65 76 6f 63 61 62 6c 79 20 .** irrevocably
140b8 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a committed..**.**
140b9 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 If an error occ
140ba 75 72 73 2c 20 61 6e 20 49 4f 20 65 72 72 6f 72 urs, an IO error
140bb 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 code is returne
140bc 64 20 61 6e 64 20 74 68 65 20 70 61 67 65 72 0a d and the pager.
140bd 2a 2a 20 6d 6f 76 65 73 20 69 6e 74 6f 20 74 68 ** moves into th
140be 65 20 65 72 72 6f 72 20 73 74 61 74 65 2e 20 4f e error state. O
140bf 74 68 65 72 77 69 73 65 2c 20 53 51 4c 49 54 45 therwise, SQLITE
140c0 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e _OK is returned.
140c1 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
140c2 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
140c3 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 gerCommitPhaseTw
140c4 6f 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 o(Pager *pPager)
140c5 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
140c6 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 ITE_OK;
140c7 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 /* Retu
140c8 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f 2a rn code */.. /*
140c9 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 This routine sh
140ca 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 61 6c 6c ould not be call
140cb 65 64 20 69 66 20 61 20 70 72 69 6f 72 20 65 72 ed if a prior er
140cc 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 64 ror has occurred
140cd 2e 0a 20 20 2a 2a 20 42 75 74 20 69 66 20 28 64 .. ** But if (d
140ce 75 65 20 74 6f 20 61 20 63 6f 64 69 6e 67 20 65 ue to a coding e
140cf 72 72 6f 72 20 65 6c 73 65 77 68 65 72 65 20 69 rror elsewhere i
140d0 6e 20 74 68 65 20 73 79 73 74 65 6d 29 20 69 74 n the system) it
140d1 20 64 6f 65 73 20 67 65 74 0a 20 20 2a 2a 20 63 does get. ** c
140d2 61 6c 6c 65 64 2c 20 6a 75 73 74 20 72 65 74 75 alled, just retu
140d3 72 6e 20 74 68 65 20 73 61 6d 65 20 65 72 72 6f rn the same erro
140d4 72 20 63 6f 64 65 20 77 69 74 68 6f 75 74 20 64 r code without d
140d5 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 2e 20 2a oing anything. *
140d6 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 50 /. if( NEVER(pP
140d7 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 29 20 29 ager->errCode) )
140d8 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e return pPager->
140d9 65 72 72 43 6f 64 65 3b 0a 0a 20 20 2f 2a 20 54 errCode;.. /* T
140da 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f his function sho
140db 75 6c 64 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 uld not be calle
140dc 64 20 69 66 20 74 68 65 20 70 61 67 65 72 20 69 d if the pager i
140dd 73 20 6e 6f 74 20 69 6e 20 61 74 20 6c 65 61 73 s not in at leas
140de 74 0a 20 20 2a 2a 20 50 41 47 45 52 5f 52 45 53 t. ** PAGER_RES
140df 45 52 56 45 44 20 73 74 61 74 65 2e 20 41 6e 64 ERVED state. And
140e0 20 69 6e 64 65 65 64 20 53 51 4c 69 74 65 20 6e indeed SQLite n
140e1 65 76 65 72 20 64 6f 65 73 20 74 68 69 73 2e 20 ever does this.
140e2 42 75 74 20 69 74 20 69 73 0a 20 20 2a 2a 20 6e But it is. ** n
140e3 69 63 65 20 74 6f 20 68 61 76 65 20 74 68 69 73 ice to have this
140e4 20 64 65 66 65 6e 73 69 76 65 20 74 65 73 74 20 defensive test
140e5 68 65 72 65 20 61 6e 79 77 61 79 2e 0a 20 20 2a here anyway.. *
140e6 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 50 /. if( NEVER(pP
140e7 61 67 65 72 2d 3e 73 74 61 74 65 3c 50 41 47 45 ager->state<PAGE
140e8 52 5f 52 45 53 45 52 56 45 44 29 20 29 20 72 65 R_RESERVED) ) re
140e9 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f turn SQLITE_ERRO
140ea 52 3b 0a 0a 20 20 2f 2a 20 41 6e 20 6f 70 74 69 R;.. /* An opti
140eb 6d 69 7a 61 74 69 6f 6e 2e 20 49 66 20 74 68 65 mization. If the
140ec 20 64 61 74 61 62 61 73 65 20 77 61 73 20 6e 6f database was no
140ed 74 20 61 63 74 75 61 6c 6c 79 20 6d 6f 64 69 66 t actually modif
140ee 69 65 64 20 64 75 72 69 6e 67 0a 20 20 2a 2a 20 ied during. **
140ef 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e this transaction
140f0 2c 20 74 68 65 20 70 61 67 65 72 20 69 73 20 72 , the pager is r
140f1 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 unning in exclus
140f2 69 76 65 2d 6d 6f 64 65 20 61 6e 64 20 69 73 0a ive-mode and is.
140f3 20 20 2a 2a 20 75 73 69 6e 67 20 70 65 72 73 69 ** using persi
140f4 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 73 2c 20 stent journals,
140f5 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 then this functi
140f6 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 on is a no-op..
140f7 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 74 61 **. ** The sta
140f8 72 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 rt of the journa
140f9 6c 20 66 69 6c 65 20 63 75 72 72 65 6e 74 6c 79 l file currently
140fa 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e 67 contains a sing
140fb 6c 65 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a le journal . **
140fc 20 68 65 61 64 65 72 20 77 69 74 68 20 74 68 65 header with the
140fd 20 6e 52 65 63 20 66 69 65 6c 64 20 73 65 74 20 nRec field set
140fe 74 6f 20 30 2e 20 49 66 20 73 75 63 68 20 61 20 to 0. If such a
140ff 6a 6f 75 72 6e 61 6c 20 69 73 20 75 73 65 64 20 journal is used
14100 61 73 0a 20 20 2a 2a 20 61 20 68 6f 74 2d 6a 6f as. ** a hot-jo
14101 75 72 6e 61 6c 20 64 75 72 69 6e 67 20 68 6f 74 urnal during hot
14102 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 -journal rollbac
14103 6b 2c 20 30 20 63 68 61 6e 67 65 73 20 77 69 6c k, 0 changes wil
14104 6c 20 62 65 20 6d 61 64 65 0a 20 20 2a 2a 20 74 l be made. ** t
14105 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 o the database f
14106 69 6c 65 2e 20 53 6f 20 74 68 65 72 65 20 69 73 ile. So there is
14107 20 6e 6f 20 6e 65 65 64 20 74 6f 20 7a 65 72 6f no need to zero
14108 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 the journal .
14109 2a 2a 20 68 65 61 64 65 72 2e 20 53 69 6e 63 65 ** header. Since
1410a 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e the pager is in
1410b 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c exclusive mode,
1410c 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 there is no nee
1410d 64 0a 20 20 2a 2a 20 74 6f 20 64 72 6f 70 20 61 d. ** to drop a
1410e 6e 79 20 6c 6f 63 6b 73 20 65 69 74 68 65 72 2e ny locks either.
1410f 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 . */. if( pPag
14110 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 3d 3d er->dbModified==
14111 30 20 26 26 20 70 50 61 67 65 72 2d 3e 65 78 63 0 && pPager->exc
14112 6c 75 73 69 76 65 4d 6f 64 65 20 0a 20 20 20 26 lusiveMode . &
14113 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 & pPager->journa
14114 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 lMode==PAGER_JOU
14115 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 RNALMODE_PERSIST
14116 0a 20 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 . ){. assert
14117 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 ( pPager->journa
14118 6c 4f 66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44 lOff==JOURNAL_HD
14119 52 5f 53 5a 28 70 50 61 67 65 72 29 20 29 3b 0a R_SZ(pPager) );.
1411a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1411b 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 50 41 47 E_OK;. }.. PAG
1411c 45 52 54 52 41 43 45 28 28 22 43 4f 4d 4d 49 54 ERTRACE(("COMMIT
1411d 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 %d\n", PAGERID(
1411e 70 50 61 67 65 72 29 29 29 3b 0a 20 20 61 73 73 pPager)));. ass
1411f 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 ert( pPager->sta
14120 74 65 3d 3d 50 41 47 45 52 5f 53 59 4e 43 45 44 te==PAGER_SYNCED
14121 20 7c 7c 20 4d 45 4d 44 42 20 7c 7c 20 21 70 50 || MEMDB || !pP
14122 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 ager->dbModified
14123 20 29 3b 0a 20 20 72 63 20 3d 20 70 61 67 65 72 );. rc = pager
14124 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e _end_transaction
14125 28 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d (pPager, pPager-
14126 3e 73 65 74 4d 61 73 74 65 72 29 3b 0a 20 20 72 >setMaster);. r
14127 65 74 75 72 6e 20 70 61 67 65 72 5f 65 72 72 6f eturn pager_erro
14128 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 7d r(pPager, rc);.}
14129 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b ../*.** Rollback
1412a 20 61 6c 6c 20 63 68 61 6e 67 65 73 2e 20 54 68 all changes. Th
1412b 65 20 64 61 74 61 62 61 73 65 20 66 61 6c 6c 73 e database falls
1412c 20 62 61 63 6b 20 74 6f 20 50 41 47 45 52 5f 53 back to PAGER_S
1412d 48 41 52 45 44 20 6d 6f 64 65 2e 0a 2a 2a 0a 2a HARED mode..**.*
1412e 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
1412f 70 65 72 66 6f 72 6d 73 20 74 77 6f 20 74 61 73 performs two tas
14130 6b 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 49 ks:.**.** 1) I
14131 74 20 72 6f 6c 6c 73 20 62 61 63 6b 20 74 68 65 t rolls back the
14132 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 72 journal file, r
14133 65 73 74 6f 72 69 6e 67 20 61 6c 6c 20 64 61 74 estoring all dat
14134 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 0a abase file and .
14135 2a 2a 20 20 20 20 20 20 69 6e 2d 6d 65 6d 6f 72 ** in-memor
14136 79 20 63 61 63 68 65 20 70 61 67 65 73 20 74 6f y cache pages to
14137 20 74 68 65 20 73 74 61 74 65 20 74 68 65 79 20 the state they
14138 77 65 72 65 20 69 6e 20 77 68 65 6e 20 74 68 65 were in when the
14139 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 transaction.**
1413a 20 20 20 20 20 77 61 73 20 6f 70 65 6e 65 64 2c was opened,
1413b 20 61 6e 64 0a 2a 2a 20 20 20 32 29 20 49 74 20 and.** 2) It
1413c 66 69 6e 61 6c 69 7a 65 73 20 74 68 65 20 6a 6f finalizes the jo
1413d 75 72 6e 61 6c 20 66 69 6c 65 2c 20 73 6f 20 74 urnal file, so t
1413e 68 61 74 20 69 74 20 69 73 20 6e 6f 74 20 75 73 hat it is not us
1413f 65 64 20 66 6f 72 20 68 6f 74 0a 2a 2a 20 20 20 ed for hot.**
14140 20 20 20 72 6f 6c 6c 62 61 63 6b 20 61 74 20 61 rollback at a
14141 6e 79 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 ny point in the
14142 66 75 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 73 75 future..**.** su
14143 62 6a 65 63 74 20 74 6f 20 74 68 65 20 66 6f 6c bject to the fol
14144 6c 6f 77 69 6e 67 20 71 75 61 6c 69 66 69 63 61 lowing qualifica
14145 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 2a 20 49 tions:.**.** * I
14146 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 f the journal fi
14147 6c 65 20 69 73 20 6e 6f 74 20 79 65 74 20 6f 70 le is not yet op
14148 65 6e 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e en when this fun
14149 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c ction is called,
1414a 0a 2a 2a 20 20 20 74 68 65 6e 20 6f 6e 6c 79 20 .** then only
1414b 28 32 29 20 69 73 20 70 65 72 66 6f 72 6d 65 64 (2) is performed
1414c 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 . In this case t
1414d 68 65 72 65 20 69 73 20 6e 6f 20 6a 6f 75 72 6e here is no journ
1414e 61 6c 20 66 69 6c 65 0a 2a 2a 20 20 20 74 6f 20 al file.** to
1414f 72 6f 6c 6c 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a roll back..**.**
14150 20 2a 20 49 66 20 69 6e 20 61 6e 20 65 72 72 6f * If in an erro
14151 72 20 73 74 61 74 65 20 6f 74 68 65 72 20 74 68 r state other th
14152 61 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 2c 20 an SQLITE_FULL,
14153 74 68 65 6e 20 74 61 73 6b 20 28 31 29 20 69 73 then task (1) is
14154 20 0a 2a 2a 20 20 20 70 65 72 66 6f 72 6d 65 64 .** performed
14155 2e 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c . If successful,
14156 20 74 61 73 6b 20 28 32 29 2e 20 52 65 67 61 72 task (2). Regar
14157 64 6c 65 73 73 20 6f 66 20 74 68 65 20 6f 75 74 dless of the out
14158 63 6f 6d 65 0a 2a 2a 20 20 20 6f 66 20 65 69 74 come.** of eit
14159 68 65 72 2c 20 74 68 65 20 65 72 72 6f 72 20 73 her, the error s
1415a 74 61 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 tate error code
1415b 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 is returned to t
1415c 68 65 20 63 61 6c 6c 65 72 0a 2a 2a 20 20 20 28 he caller.** (
1415d 69 2e 65 2e 20 65 69 74 68 65 72 20 53 51 4c 49 i.e. either SQLI
1415e 54 45 5f 49 4f 45 52 52 20 6f 72 20 53 51 4c 49 TE_IOERR or SQLI
1415f 54 45 5f 43 4f 52 52 55 50 54 29 2e 0a 2a 2a 0a TE_CORRUPT)..**.
14160 2a 2a 20 2a 20 49 66 20 74 68 65 20 70 61 67 65 ** * If the page
14161 72 20 69 73 20 69 6e 20 50 41 47 45 52 5f 52 45 r is in PAGER_RE
14162 53 45 52 56 45 44 20 73 74 61 74 65 2c 20 74 68 SERVED state, th
14163 65 6e 20 61 74 74 65 6d 70 74 20 28 31 29 2e 20 en attempt (1).
14164 57 68 65 74 68 65 72 0a 2a 2a 20 20 20 6f 72 20 Whether.** or
14165 6e 6f 74 20 28 31 29 20 69 73 20 73 75 63 63 75 not (1) is succu
14166 73 73 66 75 6c 2c 20 61 6c 73 6f 20 61 74 74 65 ssful, also atte
14167 6d 70 74 20 28 32 29 2e 20 49 66 20 73 75 63 63 mpt (2). If succ
14168 65 73 73 66 75 6c 2c 20 72 65 74 75 72 6e 0a 2a essful, return.*
14169 2a 20 20 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 4f * SQLITE_OK. O
1416a 74 68 65 72 77 69 73 65 2c 20 65 6e 74 65 72 20 therwise, enter
1416b 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 20 the error state
1416c 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 66 and return the f
1416d 69 72 73 74 20 0a 2a 2a 20 20 20 65 72 72 6f 72 irst .** error
1416e 20 63 6f 64 65 20 65 6e 63 6f 75 6e 74 65 72 65 code encountere
1416f 64 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 49 6e 20 74 d. .**.** In t
14170 68 69 73 20 63 61 73 65 20 74 68 65 72 65 20 69 his case there i
14171 73 20 6e 6f 20 63 68 61 6e 63 65 20 74 68 61 74 s no chance that
14172 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77 61 the database wa
14173 73 20 77 72 69 74 74 65 6e 20 74 6f 2e 20 0a 2a s written to. .*
14174 2a 20 20 20 53 6f 20 69 73 20 73 61 66 65 20 74 * So is safe t
14175 6f 20 66 69 6e 61 6c 69 7a 65 20 74 68 65 20 6a o finalize the j
14176 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 76 65 6e ournal file even
14177 20 69 66 20 74 68 65 20 70 6c 61 79 62 61 63 6b if the playback
14178 20 0a 2a 2a 20 20 20 28 6f 70 65 72 61 74 69 6f .** (operatio
14179 6e 20 31 29 20 66 61 69 6c 65 64 2e 20 48 6f 77 n 1) failed. How
1417a 65 76 65 72 20 74 68 65 20 70 61 67 65 72 20 6d ever the pager m
1417b 75 73 74 20 65 6e 74 65 72 20 74 68 65 20 65 72 ust enter the er
1417c 72 6f 72 20 73 74 61 74 65 0a 2a 2a 20 20 20 61 ror state.** a
1417d 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f s the contents o
1417e 66 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 f the in-memory
1417f 63 61 63 68 65 20 61 72 65 20 6e 6f 77 20 73 75 cache are now su
14180 73 70 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 2a 20 46 spect..**.** * F
14181 69 6e 61 6c 6c 79 2c 20 69 66 20 69 6e 20 50 41 inally, if in PA
14182 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 73 74 GER_EXCLUSIVE st
14183 61 74 65 2c 20 74 68 65 6e 20 61 74 74 65 6d 70 ate, then attemp
14184 74 20 28 31 29 2e 20 4f 6e 6c 79 0a 2a 2a 20 20 t (1). Only.**
14185 20 61 74 74 65 6d 70 74 20 28 32 29 20 69 66 20 attempt (2) if
14186 28 31 29 20 69 73 20 73 75 63 63 65 73 73 66 75 (1) is successfu
14187 6c 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 l. Return SQLITE
14188 5f 4f 4b 20 69 66 20 73 75 63 63 65 73 73 66 75 _OK if successfu
14189 6c 2c 0a 2a 2a 20 20 20 6f 74 68 65 72 77 69 73 l,.** otherwis
1418a 65 20 65 6e 74 65 72 20 74 68 65 20 65 72 72 6f e enter the erro
1418b 72 20 73 74 61 74 65 20 61 6e 64 20 72 65 74 75 r state and retu
1418c 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 rn the error cod
1418d 65 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 20 e from the .**
1418e 20 66 61 69 6c 69 6e 67 20 6f 70 65 72 61 74 69 failing operati
1418f 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 49 6e 20 74 on..**.** In t
14190 68 69 73 20 63 61 73 65 20 74 68 65 20 64 61 74 his case the dat
14191 61 62 61 73 65 20 66 69 6c 65 20 6d 61 79 20 68 abase file may h
14192 61 76 65 20 62 65 65 6e 20 77 72 69 74 74 65 6e ave been written
14193 20 74 6f 2e 20 53 6f 20 69 66 20 74 68 65 0a 2a to. So if the.*
14194 2a 20 20 20 70 6c 61 79 62 61 63 6b 20 6f 70 65 * playback ope
14195 72 61 74 69 6f 6e 20 64 69 64 20 6e 6f 74 20 73 ration did not s
14196 75 63 63 65 65 64 20 69 74 20 77 6f 75 6c 64 20 ucceed it would
14197 6e 6f 74 20 62 65 20 73 61 66 65 20 74 6f 20 66 not be safe to f
14198 69 6e 61 6c 69 7a 65 0a 2a 2a 20 20 20 74 68 65 inalize.** the
14199 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 journal file. I
1419a 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 6c 65 t needs to be le
1419b 66 74 20 69 6e 20 74 68 65 20 66 69 6c 65 2d 73 ft in the file-s
1419c 79 73 74 65 6d 20 73 6f 20 74 68 61 74 0a 2a 2a ystem so that.**
1419d 20 20 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 some other pr
1419e 6f 63 65 73 73 20 63 61 6e 20 75 73 65 20 69 74 ocess can use it
1419f 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68 65 20 to restore the
141a0 64 61 74 61 62 61 73 65 20 73 74 61 74 65 20 28 database state (
141a1 62 79 0a 2a 2a 20 20 20 68 6f 74 2d 6a 6f 75 72 by.** hot-jour
141a2 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 29 2e 0a 2a nal rollback)..*
141a3 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
141a4 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
141a5 72 52 6f 6c 6c 62 61 63 6b 28 50 61 67 65 72 20 rRollback(Pager
141a6 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 *pPager){. int
141a7 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 rc = SQLITE_OK;
141a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
141a9 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 /* Return code
141aa 2a 2f 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 */. PAGERTRACE(
141ab 28 22 52 4f 4c 4c 42 41 43 4b 20 25 64 5c 6e 22 ("ROLLBACK %d\n"
141ac 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 , PAGERID(pPager
141ad 29 29 29 3b 0a 20 20 69 66 28 20 21 70 50 61 67 )));. if( !pPag
141ae 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 7c er->dbModified |
141af 7c 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 | !isOpen(pPager
141b0 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 72 63 ->jfd) ){. rc
141b1 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 = pager_end_tra
141b2 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c nsaction(pPager,
141b3 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 pPager->setMast
141b4 65 72 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 er);. }else if(
141b5 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
141b6 20 26 26 20 70 50 61 67 65 72 2d 3e 65 72 72 43 && pPager->errC
141b7 6f 64 65 21 3d 53 51 4c 49 54 45 5f 46 55 4c 4c ode!=SQLITE_FULL
141b8 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67 ){. if( pPag
141b9 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 er->state>=PAGER
141ba 5f 45 58 43 4c 55 53 49 56 45 20 29 7b 0a 20 20 _EXCLUSIVE ){.
141bb 20 20 20 20 70 61 67 65 72 5f 70 6c 61 79 62 61 pager_playba
141bc 63 6b 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 ck(pPager, 0);.
141bd 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 70 50 }. rc = pP
141be 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 ager->errCode;.
141bf 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 }else{. if(
141c0 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 pPager->state==P
141c1 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 7b AGER_RESERVED ){
141c2 0a 20 20 20 20 20 20 69 6e 74 20 72 63 32 3b 0a . int rc2;.
141c3 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 rc = pager
141c4 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 67 65 72 _playback(pPager
141c5 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 63 32 20 , 0);. rc2
141c6 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e = pager_end_tran
141c7 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c 20 saction(pPager,
141c8 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 pPager->setMaste
141c9 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 r);. if( rc
141ca 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
141cb 20 20 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b rc = rc2;
141cc 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c . }. }el
141cd 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 se{. rc = p
141ce 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 70 50 ager_playback(pP
141cf 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 7d 0a ager, 0);. }.
141d0 0a 20 20 20 20 69 66 28 20 21 4d 45 4d 44 42 20 . if( !MEMDB
141d1 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d ){. pPager-
141d2 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 3d 20 30 >dbSizeValid = 0
141d3 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
141d4 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 If an error occu
141d5 72 73 20 64 75 72 69 6e 67 20 61 20 52 4f 4c 4c rs during a ROLL
141d6 42 41 43 4b 2c 20 77 65 20 63 61 6e 20 6e 6f 20 BACK, we can no
141d7 6c 6f 6e 67 65 72 20 74 72 75 73 74 20 74 68 65 longer trust the
141d8 20 70 61 67 65 72 0a 20 20 20 20 2a 2a 20 63 61 pager. ** ca
141d9 63 68 65 2e 20 53 6f 20 63 61 6c 6c 20 70 61 67 che. So call pag
141da 65 72 5f 65 72 72 6f 72 28 29 20 6f 6e 20 74 68 er_error() on th
141db 65 20 77 61 79 20 6f 75 74 20 74 6f 20 6d 61 6b e way out to mak
141dc 65 20 61 6e 79 20 65 72 72 6f 72 20 0a 20 20 20 e any error .
141dd 20 2a 2a 20 70 65 72 73 69 73 74 65 6e 74 2e 0a ** persistent..
141de 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 */. rc =
141df 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 pager_error(pPag
141e0 65 72 2c 20 72 63 29 3b 0a 20 20 7d 0a 20 20 72 er, rc);. }. r
141e1 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
141e2 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 ** Return TRUE i
141e3 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 f the database f
141e4 69 6c 65 20 69 73 20 6f 70 65 6e 65 64 20 72 65 ile is opened re
141e5 61 64 2d 6f 6e 6c 79 2e 20 20 52 65 74 75 72 6e ad-only. Return
141e6 20 46 41 4c 53 45 0a 2a 2a 20 69 66 20 74 68 65 FALSE.** if the
141e7 20 64 61 74 61 62 61 73 65 20 69 73 20 28 69 6e database is (in
141e8 20 74 68 65 6f 72 79 29 20 77 72 69 74 61 62 6c theory) writabl
141e9 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
141ea 56 41 54 45 20 75 38 20 73 71 6c 69 74 65 33 50 VATE u8 sqlite3P
141eb 61 67 65 72 49 73 72 65 61 64 6f 6e 6c 79 28 50 agerIsreadonly(P
141ec 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 ager *pPager){.
141ed 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e return pPager->
141ee 72 65 61 64 4f 6e 6c 79 3b 0a 7d 0a 0a 2f 2a 0a readOnly;.}../*.
141ef 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
141f0 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 mber of referenc
141f1 65 73 20 74 6f 20 74 68 65 20 70 61 67 65 72 2e es to the pager.
141f2 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
141f3 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
141f4 67 65 72 52 65 66 63 6f 75 6e 74 28 50 61 67 65 gerRefcount(Page
141f5 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 r *pPager){. re
141f6 74 75 72 6e 20 73 71 6c 69 74 65 33 50 63 61 63 turn sqlite3Pcac
141f7 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 heRefCount(pPage
141f8 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 7d 0a 0a r->pPCache);.}..
141f9 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
141fa 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 number of refer
141fb 65 6e 63 65 73 20 74 6f 20 74 68 65 20 73 70 65 ences to the spe
141fc 63 69 66 69 65 64 20 70 61 67 65 2e 0a 2a 2f 0a cified page..*/.
141fd 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
141fe 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 nt sqlite3PagerP
141ff 61 67 65 52 65 66 63 6f 75 6e 74 28 44 62 50 61 ageRefcount(DbPa
14200 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 72 65 ge *pPage){. re
14201 74 75 72 6e 20 73 71 6c 69 74 65 33 50 63 61 63 turn sqlite3Pcac
14202 68 65 50 61 67 65 52 65 66 63 6f 75 6e 74 28 70 hePageRefcount(p
14203 50 61 67 65 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 Page);.}..#ifdef
14204 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a SQLITE_TEST./*.
14205 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
14206 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 is used for test
14207 69 6e 67 20 61 6e 64 20 61 6e 61 6c 79 73 69 73 ing and analysis
14208 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 only..*/.SQLITE
14209 5f 50 52 49 56 41 54 45 20 69 6e 74 20 2a 73 71 _PRIVATE int *sq
1420a 6c 69 74 65 33 50 61 67 65 72 53 74 61 74 73 28 lite3PagerStats(
1420b 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
1420c 20 20 73 74 61 74 69 63 20 69 6e 74 20 61 5b 31 static int a[1
1420d 31 5d 3b 0a 20 20 61 5b 30 5d 20 3d 20 73 71 6c 1];. a[0] = sql
1420e 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 ite3PcacheRefCou
1420f 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 nt(pPager->pPCac
14210 68 65 29 3b 0a 20 20 61 5b 31 5d 20 3d 20 73 71 he);. a[1] = sq
14211 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 65 63 lite3PcachePagec
14212 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 ount(pPager->pPC
14213 61 63 68 65 29 3b 0a 20 20 61 5b 32 5d 20 3d 20 ache);. a[2] =
14214 73 71 6c 69 74 65 33 50 63 61 63 68 65 47 65 74 sqlite3PcacheGet
14215 43 61 63 68 65 73 69 7a 65 28 70 50 61 67 65 72 Cachesize(pPager
14216 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 61 5b ->pPCache);. a[
14217 33 5d 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 3] = pPager->dbS
14218 69 7a 65 56 61 6c 69 64 20 3f 20 28 69 6e 74 29 izeValid ? (int)
14219 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 pPager->dbSize
1421a 3a 20 2d 31 3b 0a 20 20 61 5b 34 5d 20 3d 20 70 : -1;. a[4] = p
1421b 50 61 67 65 72 2d 3e 73 74 61 74 65 3b 0a 20 20 Pager->state;.
1421c 61 5b 35 5d 20 3d 20 70 50 61 67 65 72 2d 3e 65 a[5] = pPager->e
1421d 72 72 43 6f 64 65 3b 0a 20 20 61 5b 36 5d 20 3d rrCode;. a[6] =
1421e 20 70 50 61 67 65 72 2d 3e 6e 48 69 74 3b 0a 20 pPager->nHit;.
1421f 20 61 5b 37 5d 20 3d 20 70 50 61 67 65 72 2d 3e a[7] = pPager->
14220 6e 4d 69 73 73 3b 0a 20 20 61 5b 38 5d 20 3d 20 nMiss;. a[8] =
14221 30 3b 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 62 0; /* Used to b
14222 65 20 70 50 61 67 65 72 2d 3e 6e 4f 76 66 6c 20 e pPager->nOvfl
14223 2a 2f 0a 20 20 61 5b 39 5d 20 3d 20 70 50 61 67 */. a[9] = pPag
14224 65 72 2d 3e 6e 52 65 61 64 3b 0a 20 20 61 5b 31 er->nRead;. a[1
14225 30 5d 20 3d 20 70 50 61 67 65 72 2d 3e 6e 57 72 0] = pPager->nWr
14226 69 74 65 3b 0a 20 20 72 65 74 75 72 6e 20 61 3b ite;. return a;
14227 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a .}.#endif../*.**
14228 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 Return true if
14229 74 68 69 73 20 69 73 20 61 6e 20 69 6e 2d 6d 65 this is an in-me
1422a 6d 6f 72 79 20 70 61 67 65 72 2e 0a 2a 2f 0a 53 mory pager..*/.S
1422b 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1422c 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 t sqlite3PagerIs
1422d 4d 65 6d 64 62 28 50 61 67 65 72 20 2a 70 50 61 Memdb(Pager *pPa
1422e 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 4d ger){. return M
1422f 45 4d 44 42 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 EMDB;.}../*.** C
14230 68 65 63 6b 20 74 68 61 74 20 74 68 65 72 65 20 heck that there
14231 61 72 65 20 61 74 20 6c 65 61 73 74 20 6e 53 61 are at least nSa
14232 76 65 70 6f 69 6e 74 20 73 61 76 65 70 6f 69 6e vepoint savepoin
14233 74 73 20 6f 70 65 6e 2e 20 49 66 20 74 68 65 72 ts open. If ther
14234 65 20 61 72 65 0a 2a 2a 20 63 75 72 72 65 6e 74 e are.** current
14235 6c 79 20 6c 65 73 73 20 74 68 61 6e 20 6e 53 61 ly less than nSa
14236 76 65 70 6f 69 6e 74 73 20 6f 70 65 6e 2c 20 74 vepoints open, t
14237 68 65 6e 20 6f 70 65 6e 20 6f 6e 65 20 6f 72 20 hen open one or
14238 6d 6f 72 65 20 73 61 76 65 70 6f 69 6e 74 73 0a more savepoints.
14239 2a 2a 20 74 6f 20 6d 61 6b 65 20 75 70 20 74 68 ** to make up th
1423a 65 20 64 69 66 66 65 72 65 6e 63 65 2e 20 49 66 e difference. If
1423b 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 the number of s
1423c 61 76 65 70 6f 69 6e 74 73 20 69 73 20 61 6c 72 avepoints is alr
1423d 65 61 64 79 0a 2a 2a 20 65 71 75 61 6c 20 74 6f eady.** equal to
1423e 20 6e 53 61 76 65 70 6f 69 6e 74 2c 20 74 68 65 nSavepoint, the
1423f 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 n this function
14240 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a is a no-op..**.*
14241 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c * If a memory al
14242 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 location fails,
14243 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 SQLITE_NOMEM is
14244 72 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 returned. If an
14245 65 72 72 6f 72 20 0a 2a 2a 20 6f 63 63 75 72 73 error .** occurs
14246 20 77 68 69 6c 65 20 6f 70 65 6e 69 6e 67 20 74 while opening t
14247 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 he sub-journal f
14248 69 6c 65 2c 20 74 68 65 6e 20 61 6e 20 49 4f 20 ile, then an IO
14249 65 72 72 6f 72 20 63 6f 64 65 20 69 73 0a 2a 2a error code is.**
1424a 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 returned. Other
1424b 77 69 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 2e wise, SQLITE_OK.
1424c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1424d 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
1424e 67 65 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e 74 gerOpenSavepoint
1424f 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
14250 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e 74 29 7b int nSavepoint){
14251 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
14252 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 TE_OK;
14253 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
14254 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 Return code */.
14255 20 69 6e 74 20 6e 43 75 72 72 65 6e 74 20 3d 20 int nCurrent =
14256 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 pPager->nSavepoi
14257 6e 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 75 nt; /* Cu
14258 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 rrent number of
14259 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 0a 20 savepoints */..
1425a 20 69 66 28 20 6e 53 61 76 65 70 6f 69 6e 74 3e if( nSavepoint>
1425b 6e 43 75 72 72 65 6e 74 20 26 26 20 70 50 61 67 nCurrent && pPag
1425c 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 er->useJournal )
1425d 7b 0a 20 20 20 20 69 6e 74 20 69 69 3b 20 20 20 {. int ii;
1425e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1425f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
14260 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 Iterator variab
14261 6c 65 20 2a 2f 0a 20 20 20 20 50 61 67 65 72 53 le */. PagerS
14262 61 76 65 70 6f 69 6e 74 20 2a 61 4e 65 77 3b 20 avepoint *aNew;
14263 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14264 20 20 2f 2a 20 4e 65 77 20 50 61 67 65 72 2e 61 /* New Pager.a
14265 53 61 76 65 70 6f 69 6e 74 20 61 72 72 61 79 20 Savepoint array
14266 2a 2f 0a 0a 20 20 20 20 2f 2a 20 45 69 74 68 65 */.. /* Eithe
14267 72 20 74 68 65 72 65 20 69 73 20 6e 6f 20 61 63 r there is no ac
14268 74 69 76 65 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 tive journal or
14269 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 the sub-journal
1426a 69 73 20 6f 70 65 6e 20 6f 72 20 0a 20 20 20 20 is open or .
1426b 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 ** the journal i
1426c 73 20 61 6c 77 61 79 73 20 73 74 6f 72 65 64 20 s always stored
1426d 69 6e 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 20 in memory */.
1426e 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
1426f 3e 6e 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 7c >nSavepoint==0 |
14270 7c 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d | isOpen(pPager-
14271 3e 73 6a 66 64 29 20 7c 7c 0a 20 20 20 20 20 20 >sjfd) ||.
14272 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f pPager->jo
14273 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 urnalMode==PAGER
14274 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d _JOURNALMODE_MEM
14275 4f 52 59 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 47 ORY );.. /* G
14276 72 6f 77 20 74 68 65 20 50 61 67 65 72 2e 61 53 row the Pager.aS
14277 61 76 65 70 6f 69 6e 74 20 61 72 72 61 79 20 75 avepoint array u
14278 73 69 6e 67 20 72 65 61 6c 6c 6f 63 28 29 2e 20 sing realloc().
14279 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f Return SQLITE_NO
1427a 4d 45 4d 0a 20 20 20 20 2a 2a 20 69 66 20 74 68 MEM. ** if th
1427b 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 e allocation fai
1427c 6c 73 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 7a ls. Otherwise, z
1427d 65 72 6f 20 74 68 65 20 6e 65 77 20 70 6f 72 74 ero the new port
1427e 69 6f 6e 20 69 6e 20 63 61 73 65 20 61 20 0a 20 ion in case a .
1427f 20 20 20 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 ** malloc fai
14280 6c 75 72 65 20 6f 63 63 75 72 73 20 77 68 69 6c lure occurs whil
14281 65 20 70 6f 70 75 6c 61 74 69 6e 67 20 69 74 20 e populating it
14282 69 6e 20 74 68 65 20 66 6f 72 28 2e 2e 2e 29 20 in the for(...)
14283 6c 6f 6f 70 20 62 65 6c 6f 77 2e 0a 20 20 20 20 loop below..
14284 2a 2f 0a 20 20 20 20 61 4e 65 77 20 3d 20 28 50 */. aNew = (P
14285 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 29 agerSavepoint *)
14286 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 0a sqlite3Realloc(.
14287 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e pPager->
14288 61 53 61 76 65 70 6f 69 6e 74 2c 20 73 69 7a 65 aSavepoint, size
14289 6f 66 28 50 61 67 65 72 53 61 76 65 70 6f 69 6e of(PagerSavepoin
1428a 74 29 2a 6e 53 61 76 65 70 6f 69 6e 74 0a 20 20 t)*nSavepoint.
1428b 20 20 29 3b 0a 20 20 20 20 69 66 28 20 21 61 4e );. if( !aN
1428c 65 77 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 ew ){. retu
1428d 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
1428e 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 65 . }. memse
1428f 74 28 26 61 4e 65 77 5b 6e 43 75 72 72 65 6e 74 t(&aNew[nCurrent
14290 5d 2c 20 30 2c 20 28 6e 53 61 76 65 70 6f 69 6e ], 0, (nSavepoin
14291 74 2d 6e 43 75 72 72 65 6e 74 29 20 2a 20 73 69 t-nCurrent) * si
14292 7a 65 6f 66 28 50 61 67 65 72 53 61 76 65 70 6f zeof(PagerSavepo
14293 69 6e 74 29 29 3b 0a 20 20 20 20 70 50 61 67 65 int));. pPage
14294 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 20 3d 20 r->aSavepoint =
14295 61 4e 65 77 3b 0a 20 20 20 20 70 50 61 67 65 72 aNew;. pPager
14296 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 6e ->nSavepoint = n
14297 53 61 76 65 70 6f 69 6e 74 3b 0a 0a 20 20 20 20 Savepoint;..
14298 2f 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 /* Populate the
14299 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 73 PagerSavepoint s
1429a 74 72 75 63 74 75 72 65 73 20 6a 75 73 74 20 61 tructures just a
1429b 6c 6c 6f 63 61 74 65 64 2e 20 2a 2f 0a 20 20 20 llocated. */.
1429c 20 66 6f 72 28 69 69 3d 6e 43 75 72 72 65 6e 74 for(ii=nCurrent
1429d 3b 20 69 69 3c 6e 53 61 76 65 70 6f 69 6e 74 3b ; ii<nSavepoint;
1429e 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 ii++){. as
1429f 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 sert( pPager->db
142a0 53 69 7a 65 56 61 6c 69 64 20 29 3b 0a 20 20 20 SizeValid );.
142a1 20 20 20 61 4e 65 77 5b 69 69 5d 2e 6e 4f 72 69 aNew[ii].nOri
142a2 67 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69 g = pPager->dbSi
142a3 7a 65 3b 0a 20 20 20 20 20 20 69 66 28 20 69 73 ze;. if( is
142a4 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 Open(pPager->jfd
142a5 29 20 26 26 20 41 4c 57 41 59 53 28 70 50 61 67 ) && ALWAYS(pPag
142a6 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e 30 er->journalOff>0
142a7 29 20 29 7b 0a 20 20 20 20 20 20 20 20 61 4e 65 ) ){. aNe
142a8 77 5b 69 69 5d 2e 69 4f 66 66 73 65 74 20 3d 20 w[ii].iOffset =
142a9 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
142aa 66 66 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b ff;. }else{
142ab 0a 20 20 20 20 20 20 20 20 61 4e 65 77 5b 69 69 . aNew[ii
142ac 5d 2e 69 4f 66 66 73 65 74 20 3d 20 4a 4f 55 52 ].iOffset = JOUR
142ad 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 NAL_HDR_SZ(pPage
142ae 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 r);. }.
142af 20 20 61 4e 65 77 5b 69 69 5d 2e 69 53 75 62 52 aNew[ii].iSubR
142b0 65 63 20 3d 20 70 50 61 67 65 72 2d 3e 6e 53 75 ec = pPager->nSu
142b1 62 52 65 63 3b 0a 20 20 20 20 20 20 61 4e 65 77 bRec;. aNew
142b2 5b 69 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69 6e [ii].pInSavepoin
142b3 74 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 t = sqlite3Bitve
142b4 63 43 72 65 61 74 65 28 70 50 61 67 65 72 2d 3e cCreate(pPager->
142b5 64 62 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 69 dbSize);. i
142b6 66 28 20 21 61 4e 65 77 5b 69 69 5d 2e 70 49 6e f( !aNew[ii].pIn
142b7 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 Savepoint ){.
142b8 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
142b9 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 TE_NOMEM;.
142ba 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 }. }.. /*
142bb 4f 70 65 6e 20 74 68 65 20 73 75 62 2d 6a 6f 75 Open the sub-jou
142bc 72 6e 61 6c 2c 20 69 66 20 69 74 20 69 73 20 6e rnal, if it is n
142bd 6f 74 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 65 ot already opene
142be 64 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 6f d. */. rc = o
142bf 70 65 6e 53 75 62 4a 6f 75 72 6e 61 6c 28 70 50 penSubJournal(pP
142c0 61 67 65 72 29 3b 0a 20 20 20 20 61 73 73 65 72 ager);. asser
142c1 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 tTruncateConstra
142c2 69 6e 74 28 70 50 61 67 65 72 29 3b 0a 20 20 7d int(pPager);. }
142c3 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d .. return rc;.}
142c4 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
142c5 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 ction is called
142c6 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 6f 72 20 72 to rollback or r
142c7 65 6c 65 61 73 65 20 28 63 6f 6d 6d 69 74 29 20 elease (commit)
142c8 61 20 73 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a 20 a savepoint..**
142c9 54 68 65 20 73 61 76 65 70 6f 69 6e 74 20 74 6f The savepoint to
142ca 20 72 65 6c 65 61 73 65 20 6f 72 20 72 6f 6c 6c release or roll
142cb 62 61 63 6b 20 6e 65 65 64 20 6e 6f 74 20 62 65 back need not be
142cc 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 the most recent
142cd 6c 79 20 0a 2a 2a 20 63 72 65 61 74 65 64 20 73 ly .** created s
142ce 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 avepoint..**.**
142cf 50 61 72 61 6d 65 74 65 72 20 6f 70 20 69 73 20 Parameter op is
142d0 61 6c 77 61 79 73 20 65 69 74 68 65 72 20 53 41 always either SA
142d1 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b VEPOINT_ROLLBACK
142d2 20 6f 72 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 or SAVEPOINT_RE
142d3 4c 45 41 53 45 2e 0a 2a 2a 20 49 66 20 69 74 20 LEASE..** If it
142d4 69 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c is SAVEPOINT_REL
142d5 45 41 53 45 2c 20 74 68 65 6e 20 72 65 6c 65 61 EASE, then relea
142d6 73 65 20 61 6e 64 20 64 65 73 74 72 6f 79 20 74 se and destroy t
142d7 68 65 20 73 61 76 65 70 6f 69 6e 74 20 77 69 74 he savepoint wit
142d8 68 0a 2a 2a 20 69 6e 64 65 78 20 69 53 61 76 65 h.** index iSave
142d9 70 6f 69 6e 74 2e 20 49 66 20 69 74 20 69 73 20 point. If it is
142da 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 SAVEPOINT_ROLLBA
142db 43 4b 2c 20 74 68 65 6e 20 72 6f 6c 6c 62 61 63 CK, then rollbac
142dc 6b 20 61 6c 6c 20 63 68 61 6e 67 65 73 0a 2a 2a k all changes.**
142dd 20 74 68 61 74 20 68 61 76 65 20 6f 63 63 75 72 that have occur
142de 72 65 64 20 73 69 6e 63 65 20 74 68 65 20 73 70 red since the sp
142df 65 63 69 66 69 65 64 20 73 61 76 65 70 6f 69 6e ecified savepoin
142e0 74 20 77 61 73 20 63 72 65 61 74 65 64 2e 0a 2a t was created..*
142e1 2a 0a 2a 2a 20 54 68 65 20 73 61 76 65 70 6f 69 *.** The savepoi
142e2 6e 74 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 6f nt to rollback o
142e3 72 20 72 65 6c 65 61 73 65 20 69 73 20 69 64 65 r release is ide
142e4 6e 74 69 66 69 65 64 20 62 79 20 70 61 72 61 6d ntified by param
142e5 65 74 65 72 20 0a 2a 2a 20 69 53 61 76 65 70 6f eter .** iSavepo
142e6 69 6e 74 2e 20 41 20 76 61 6c 75 65 20 6f 66 20 int. A value of
142e7 30 20 6d 65 61 6e 73 20 74 6f 20 6f 70 65 72 61 0 means to opera
142e8 74 65 20 6f 6e 20 74 68 65 20 6f 75 74 65 72 6d te on the outerm
142e9 6f 73 74 20 73 61 76 65 70 6f 69 6e 74 0a 2a 2a ost savepoint.**
142ea 20 28 74 68 65 20 66 69 72 73 74 20 63 72 65 61 (the first crea
142eb 74 65 64 29 2e 20 41 20 76 61 6c 75 65 20 6f 66 ted). A value of
142ec 20 28 50 61 67 65 72 2e 6e 53 61 76 65 70 6f 69 (Pager.nSavepoi
142ed 6e 74 2d 31 29 20 6d 65 61 6e 73 20 6f 70 65 72 nt-1) means oper
142ee 61 74 65 0a 2a 2a 20 6f 6e 20 74 68 65 20 6d 6f ate.** on the mo
142ef 73 74 20 72 65 63 65 6e 74 6c 79 20 63 72 65 61 st recently crea
142f0 74 65 64 20 73 61 76 65 70 6f 69 6e 74 2e 20 49 ted savepoint. I
142f1 66 20 69 53 61 76 65 70 6f 69 6e 74 20 69 73 20 f iSavepoint is
142f2 67 72 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a 20 greater than.**
142f3 28 50 61 67 65 72 2e 6e 53 61 76 65 70 6f 69 6e (Pager.nSavepoin
142f4 74 2d 31 29 2c 20 74 68 65 6e 20 74 68 69 73 20 t-1), then this
142f5 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f function is a no
142f6 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 -op..**.** If a
142f7 6e 65 67 61 74 69 76 65 20 76 61 6c 75 65 20 69 negative value i
142f8 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 s passed to this
142f9 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68 65 6e 20 function, then
142fa 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 74 the current.** t
142fb 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f ransaction is ro
142fc 6c 6c 65 64 20 62 61 63 6b 2e 20 54 68 69 73 20 lled back. This
142fd 69 73 20 64 69 66 66 65 72 65 6e 74 20 74 6f 20 is different to
142fe 63 61 6c 6c 69 6e 67 20 0a 2a 2a 20 73 71 6c 69 calling .** sqli
142ff 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b te3PagerRollback
14300 28 29 20 62 65 63 61 75 73 65 20 74 68 69 73 20 () because this
14301 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f function does no
14302 74 20 74 65 72 6d 69 6e 61 74 65 0a 2a 2a 20 74 t terminate.** t
14303 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f he transaction o
14304 72 20 75 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74 r unlock the dat
14305 61 62 61 73 65 2c 20 69 74 20 6a 75 73 74 20 72 abase, it just r
14306 65 73 74 6f 72 65 73 20 74 68 65 20 0a 2a 2a 20 estores the .**
14307 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 contents of the
14308 64 61 74 61 62 61 73 65 20 74 6f 20 69 74 73 20 database to its
14309 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 65 2e 20 original state.
1430a 0a 2a 2a 0a 2a 2a 20 49 6e 20 61 6e 79 20 63 61 .**.** In any ca
1430b 73 65 2c 20 61 6c 6c 20 73 61 76 65 70 6f 69 6e se, all savepoin
1430c 74 73 20 77 69 74 68 20 61 6e 20 69 6e 64 65 78 ts with an index
1430d 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 69 53 greater than iS
1430e 61 76 65 70 6f 69 6e 74 20 0a 2a 2a 20 61 72 65 avepoint .** are
1430f 20 64 65 73 74 72 6f 79 65 64 2e 20 49 66 20 74 destroyed. If t
14310 68 69 73 20 69 73 20 61 20 72 65 6c 65 61 73 65 his is a release
14311 20 6f 70 65 72 61 74 69 6f 6e 20 28 6f 70 3d 3d operation (op==
14312 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 SAVEPOINT_RELEAS
14313 45 29 2c 0a 2a 2a 20 74 68 65 6e 20 73 61 76 65 E),.** then save
14314 70 6f 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 point iSavepoint
14315 20 69 73 20 61 6c 73 6f 20 64 65 73 74 72 6f 79 is also destroy
14316 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ed..**.** This f
14317 75 6e 63 74 69 6f 6e 20 6d 61 79 20 72 65 74 75 unction may retu
14318 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 rn SQLITE_NOMEM
14319 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f if a memory allo
1431a 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 0a 2a 2a cation fails,.**
1431b 20 6f 72 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 or an IO error
1431c 63 6f 64 65 20 69 66 20 61 6e 20 49 4f 20 65 72 code if an IO er
1431d 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 ror occurs while
1431e 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 rolling back a
1431f 0a 2a 2a 20 73 61 76 65 70 6f 69 6e 74 2e 20 49 .** savepoint. I
14320 66 20 6e 6f 20 65 72 72 6f 72 73 20 6f 63 63 75 f no errors occu
14321 72 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 r, SQLITE_OK is
14322 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 20 0a 53 51 returned..*/ .SQ
14323 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
14324 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 61 76 sqlite3PagerSav
14325 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 50 epoint(Pager *pP
14326 61 67 65 72 2c 20 69 6e 74 20 6f 70 2c 20 69 6e ager, int op, in
14327 74 20 69 53 61 76 65 70 6f 69 6e 74 29 7b 0a 20 t iSavepoint){.
14328 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
14329 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 _OK;.. assert(
1432a 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 op==SAVEPOINT_RE
1432b 4c 45 41 53 45 20 7c 7c 20 6f 70 3d 3d 53 41 56 LEASE || op==SAV
1432c 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 EPOINT_ROLLBACK
1432d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 53 61 );. assert( iSa
1432e 76 65 70 6f 69 6e 74 3e 3d 30 20 7c 7c 20 6f 70 vepoint>=0 || op
1432f 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c ==SAVEPOINT_ROLL
14330 42 41 43 4b 20 29 3b 0a 0a 20 20 69 66 28 20 69 BACK );.. if( i
14331 53 61 76 65 70 6f 69 6e 74 3c 70 50 61 67 65 72 Savepoint<pPager
14332 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a ->nSavepoint ){.
14333 20 20 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20 int ii;
14334 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 /* Iterat
14335 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 or variable */.
14336 20 20 20 69 6e 74 20 6e 4e 65 77 3b 20 20 20 20 int nNew;
14337 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
14338 6f 66 20 72 65 6d 61 69 6e 69 6e 67 20 73 61 76 of remaining sav
14339 65 70 6f 69 6e 74 73 20 61 66 74 65 72 20 74 68 epoints after th
1433a 69 73 20 6f 70 2e 20 2a 2f 0a 0a 20 20 20 20 2f is op. */.. /
1433b 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f 77 * Figure out how
1433c 20 6d 61 6e 79 20 73 61 76 65 70 6f 69 6e 74 73 many savepoints
1433d 20 77 69 6c 6c 20 73 74 69 6c 6c 20 62 65 20 61 will still be a
1433e 63 74 69 76 65 20 61 66 74 65 72 20 74 68 69 73 ctive after this
1433f 0a 20 20 20 20 2a 2a 20 6f 70 65 72 61 74 69 6f . ** operatio
14340 6e 2e 20 53 74 6f 72 65 20 74 68 69 73 20 76 61 n. Store this va
14341 6c 75 65 20 69 6e 20 6e 4e 65 77 2e 20 54 68 65 lue in nNew. The
14342 6e 20 66 72 65 65 20 72 65 73 6f 75 72 63 65 73 n free resources
14343 20 61 73 73 6f 63 69 61 74 65 64 20 0a 20 20 20 associated .
14344 20 2a 2a 20 77 69 74 68 20 61 6e 79 20 73 61 76 ** with any sav
14345 65 70 6f 69 6e 74 73 20 74 68 61 74 20 61 72 65 epoints that are
14346 20 64 65 73 74 72 6f 79 65 64 20 62 79 20 74 68 destroyed by th
14347 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 20 20 is operation..
14348 20 20 2a 2f 0a 20 20 20 20 6e 4e 65 77 20 3d 20 */. nNew =
14349 69 53 61 76 65 70 6f 69 6e 74 20 2b 20 28 6f 70 iSavepoint + (op
1434a 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c ==SAVEPOINT_ROLL
1434b 42 41 43 4b 29 3b 0a 20 20 20 20 66 6f 72 28 69 BACK);. for(i
1434c 69 3d 6e 4e 65 77 3b 20 69 69 3c 70 50 61 67 65 i=nNew; ii<pPage
1434d 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 r->nSavepoint; i
1434e 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 i++){. sqli
1434f 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 te3BitvecDestroy
14350 28 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f (pPager->aSavepo
14351 69 6e 74 5b 69 69 5d 2e 70 49 6e 53 61 76 65 70 int[ii].pInSavep
14352 6f 69 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 oint);. }.
14353 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f pPager->nSavepo
14354 69 6e 74 20 3d 20 6e 4e 65 77 3b 0a 0a 20 20 20 int = nNew;..
14355 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 /* If this is a
14356 20 72 6f 6c 6c 62 61 63 6b 20 6f 70 65 72 61 74 rollback operat
14357 69 6f 6e 2c 20 70 6c 61 79 62 61 63 6b 20 74 68 ion, playback th
14358 65 20 73 70 65 63 69 66 69 65 64 20 73 61 76 65 e specified save
14359 70 6f 69 6e 74 2e 0a 20 20 20 20 2a 2a 20 49 66 point.. ** If
1435a 20 74 68 69 73 20 69 73 20 61 20 74 65 6d 70 2d this is a temp-
1435b 66 69 6c 65 2c 20 69 74 20 69 73 20 70 6f 73 73 file, it is poss
1435c 69 62 6c 65 20 74 68 61 74 20 74 68 65 20 6a 6f ible that the jo
1435d 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 73 0a 20 urnal file has.
1435e 20 20 20 2a 2a 20 6e 6f 74 20 79 65 74 20 62 65 ** not yet be
1435f 65 6e 20 6f 70 65 6e 65 64 2e 20 49 6e 20 74 68 en opened. In th
14360 69 73 20 63 61 73 65 20 74 68 65 72 65 20 68 61 is case there ha
14361 76 65 20 62 65 65 6e 20 6e 6f 20 63 68 61 6e 67 ve been no chang
14362 65 73 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 es to. ** the
14363 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 database file,
14364 73 6f 20 74 68 65 20 70 6c 61 79 62 61 63 6b 20 so the playback
14365 6f 70 65 72 61 74 69 6f 6e 20 63 61 6e 20 62 65 operation can be
14366 20 73 6b 69 70 70 65 64 2e 0a 20 20 20 20 2a 2f skipped.. */
14367 0a 20 20 20 20 69 66 28 20 6f 70 3d 3d 53 41 56 . if( op==SAV
14368 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 EPOINT_ROLLBACK
14369 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 && isOpen(pPager
1436a 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20 ->jfd) ){.
1436b 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a PagerSavepoint *
1436c 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 28 6e 4e pSavepoint = (nN
1436d 65 77 3d 3d 30 29 3f 30 3a 26 70 50 61 67 65 72 ew==0)?0:&pPager
1436e 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 6e 4e 65 ->aSavepoint[nNe
1436f 77 2d 31 5d 3b 0a 20 20 20 20 20 20 72 63 20 3d w-1];. rc =
14370 20 70 61 67 65 72 50 6c 61 79 62 61 63 6b 53 61 pagerPlaybackSa
14371 76 65 70 6f 69 6e 74 28 70 50 61 67 65 72 2c 20 vepoint(pPager,
14372 70 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 pSavepoint);.
14373 20 20 20 61 73 73 65 72 74 28 72 63 21 3d 53 51 assert(rc!=SQ
14374 4c 49 54 45 5f 44 4f 4e 45 29 3b 0a 20 20 20 20 LITE_DONE);.
14375 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 }. . /* If t
14376 68 69 73 20 69 73 20 61 20 72 65 6c 65 61 73 65 his is a release
14377 20 6f 66 20 74 68 65 20 6f 75 74 65 72 6d 6f 73 of the outermos
14378 74 20 73 61 76 65 70 6f 69 6e 74 2c 20 74 72 75 t savepoint, tru
14379 6e 63 61 74 65 20 0a 20 20 20 20 2a 2a 20 74 68 ncate . ** th
1437a 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 74 6f e sub-journal to
1437b 20 7a 65 72 6f 20 62 79 74 65 73 20 69 6e 20 73 zero bytes in s
1437c 69 7a 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 ize. */. if(
1437d 6e 4e 65 77 3d 3d 30 20 26 26 20 6f 70 3d 3d 53 nNew==0 && op==S
1437e 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 AVEPOINT_RELEASE
1437f 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 && isOpen(pPage
14380 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20 20 r->sjfd) ){.
14381 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 assert( rc==SQ
14382 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 20 LITE_OK );.
14383 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 rc = sqlite3OsT
14384 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e runcate(pPager->
14385 73 6a 66 64 2c 20 30 29 3b 0a 20 20 20 20 20 20 sjfd, 0);.
14386 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 20 pPager->nSubRec
14387 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 = 0;. }. }.
14388 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
14389 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
1438a 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20 6f 66 full pathname of
1438b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
1438c 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 le..*/.SQLITE_PR
1438d 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 IVATE const char
1438e 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 *sqlite3PagerFi
1438f 6c 65 6e 61 6d 65 28 50 61 67 65 72 20 2a 70 50 lename(Pager *pP
14390 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 ager){. return
14391 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d pPager->zFilenam
14392 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 e;.}../*.** Retu
14393 72 6e 20 74 68 65 20 56 46 53 20 73 74 72 75 63 rn the VFS struc
14394 74 75 72 65 20 66 6f 72 20 74 68 65 20 70 61 67 ture for the pag
14395 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 er..*/.SQLITE_PR
14396 49 56 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 IVATE const sqli
14397 74 65 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 te3_vfs *sqlite3
14398 50 61 67 65 72 56 66 73 28 50 61 67 65 72 20 2a PagerVfs(Pager *
14399 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 pPager){. retur
1439a 6e 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a n pPager->pVfs;.
1439b 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
1439c 74 68 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 the file handle
1439d 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 65 for the database
1439e 20 66 69 6c 65 20 61 73 73 6f 63 69 61 74 65 64 file associated
1439f 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 70 61 67 .** with the pag
143a0 65 72 2e 20 20 54 68 69 73 20 6d 69 67 68 74 20 er. This might
143a1 72 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 74 return NULL if t
143a2 68 65 20 66 69 6c 65 20 68 61 73 0a 2a 2a 20 6e he file has.** n
143a3 6f 74 20 79 65 74 20 62 65 65 6e 20 6f 70 65 6e ot yet been open
143a4 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
143a5 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 66 69 IVATE sqlite3_fi
143a6 6c 65 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 le *sqlite3Pager
143a7 46 69 6c 65 28 50 61 67 65 72 20 2a 70 50 61 67 File(Pager *pPag
143a8 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 er){. return pP
143a9 61 67 65 72 2d 3e 66 64 3b 0a 7d 0a 0a 2f 2a 0a ager->fd;.}../*.
143aa 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 66 75 ** Return the fu
143ab 6c 6c 20 70 61 74 68 6e 61 6d 65 20 6f 66 20 74 ll pathname of t
143ac 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e he journal file.
143ad 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
143ae 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 TE const char *s
143af 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e qlite3PagerJourn
143b0 61 6c 6e 61 6d 65 28 50 61 67 65 72 20 2a 70 50 alname(Pager *pP
143b1 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 ager){. return
143b2 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c pPager->zJournal
143b3 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
143b4 6e 20 74 72 75 65 20 69 66 20 66 73 79 6e 63 28 n true if fsync(
143b5 29 20 63 61 6c 6c 73 20 61 72 65 20 64 69 73 61 ) calls are disa
143b6 62 6c 65 64 20 66 6f 72 20 74 68 69 73 20 70 61 bled for this pa
143b7 67 65 72 2e 20 20 52 65 74 75 72 6e 20 46 41 4c ger. Return FAL
143b8 53 45 0a 2a 2a 20 69 66 20 66 73 79 6e 63 28 29 SE.** if fsync()
143b9 73 20 61 72 65 20 65 78 65 63 75 74 65 64 20 6e s are executed n
143ba 6f 72 6d 61 6c 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 ormally..*/.SQLI
143bb 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
143bc 71 6c 69 74 65 33 50 61 67 65 72 4e 6f 73 79 6e qlite3PagerNosyn
143bd 63 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 c(Pager *pPager)
143be 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 {. return pPage
143bf 72 2d 3e 6e 6f 53 79 6e 63 3b 0a 7d 0a 0a 23 69 r->noSync;.}..#i
143c0 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f fdef SQLITE_HAS_
143c1 43 4f 44 45 43 0a 2f 2a 0a 2a 2a 20 53 65 74 20 CODEC./*.** Set
143c2 6f 72 20 72 65 74 72 69 65 76 65 20 74 68 65 20 or retrieve the
143c3 63 6f 64 65 63 20 66 6f 72 20 74 68 69 73 20 70 codec for this p
143c4 61 67 65 72 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ager.*/.static v
143c5 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 oid sqlite3Pager
143c6 53 65 74 43 6f 64 65 63 28 0a 20 20 50 61 67 65 SetCodec(. Page
143c7 72 20 2a 70 50 61 67 65 72 2c 0a 20 20 76 6f 69 r *pPager,. voi
143c8 64 20 2a 28 2a 78 43 6f 64 65 63 29 28 76 6f 69 d *(*xCodec)(voi
143c9 64 2a 2c 76 6f 69 64 2a 2c 50 67 6e 6f 2c 69 6e d*,void*,Pgno,in
143ca 74 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 43 6f t),. void (*xCo
143cb 64 65 63 53 69 7a 65 43 68 6e 67 29 28 76 6f 69 decSizeChng)(voi
143cc 64 2a 2c 69 6e 74 2c 69 6e 74 29 2c 0a 20 20 76 d*,int,int),. v
143cd 6f 69 64 20 28 2a 78 43 6f 64 65 63 46 72 65 65 oid (*xCodecFree
143ce 29 28 76 6f 69 64 2a 29 2c 0a 20 20 76 6f 69 64 )(void*),. void
143cf 20 2a 70 43 6f 64 65 63 0a 29 7b 0a 20 20 69 66 *pCodec.){. if
143d0 28 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 ( pPager->xCodec
143d1 46 72 65 65 20 29 20 70 50 61 67 65 72 2d 3e 78 Free ) pPager->x
143d2 43 6f 64 65 63 46 72 65 65 28 70 50 61 67 65 72 CodecFree(pPager
143d3 2d 3e 70 43 6f 64 65 63 29 3b 0a 20 20 70 50 61 ->pCodec);. pPa
143d4 67 65 72 2d 3e 78 43 6f 64 65 63 20 3d 20 78 43 ger->xCodec = xC
143d5 6f 64 65 63 3b 0a 20 20 70 50 61 67 65 72 2d 3e odec;. pPager->
143d6 78 43 6f 64 65 63 53 69 7a 65 43 68 6e 67 20 3d xCodecSizeChng =
143d7 20 78 43 6f 64 65 63 53 69 7a 65 43 68 6e 67 3b xCodecSizeChng;
143d8 0a 20 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 . pPager->xCode
143d9 63 46 72 65 65 20 3d 20 78 43 6f 64 65 63 46 72 cFree = xCodecFr
143da 65 65 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70 43 ee;. pPager->pC
143db 6f 64 65 63 20 3d 20 70 43 6f 64 65 63 3b 0a 20 odec = pCodec;.
143dc 20 70 61 67 65 72 52 65 70 6f 72 74 53 69 7a 65 pagerReportSize
143dd 28 70 50 61 67 65 72 29 3b 0a 7d 0a 73 74 61 74 (pPager);.}.stat
143de 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 ic void *sqlite3
143df 50 61 67 65 72 47 65 74 43 6f 64 65 63 28 50 61 PagerGetCodec(Pa
143e0 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
143e1 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 70 return pPager->p
143e2 43 6f 64 65 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a Codec;.}.#endif.
143e3 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
143e4 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
143e5 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 70 /*.** Move the p
143e6 61 67 65 20 70 50 67 20 74 6f 20 6c 6f 63 61 74 age pPg to locat
143e7 69 6f 6e 20 70 67 6e 6f 20 69 6e 20 74 68 65 20 ion pgno in the
143e8 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 file..**.** Ther
143e9 65 20 6d 75 73 74 20 62 65 20 6e 6f 20 72 65 66 e must be no ref
143ea 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70 erences to the p
143eb 61 67 65 20 70 72 65 76 69 6f 75 73 6c 79 20 6c age previously l
143ec 6f 63 61 74 65 64 20 61 74 0a 2a 2a 20 70 67 6e ocated at.** pgn
143ed 6f 20 28 77 68 69 63 68 20 77 65 20 63 61 6c 6c o (which we call
143ee 20 70 50 67 4f 6c 64 29 20 74 68 6f 75 67 68 20 pPgOld) though
143ef 74 68 61 74 20 70 61 67 65 20 69 73 20 61 6c 6c that page is all
143f0 6f 77 65 64 20 74 6f 20 62 65 0a 2a 2a 20 69 6e owed to be.** in
143f1 20 63 61 63 68 65 2e 20 20 49 66 20 74 68 65 20 cache. If the
143f2 70 61 67 65 20 70 72 65 76 69 6f 75 73 6c 79 20 page previously
143f3 6c 6f 63 61 74 65 64 20 61 74 20 70 67 6e 6f 20 located at pgno
143f4 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 0a 2a is not already.*
143f5 2a 20 69 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 * in the rollbac
143f6 6b 20 6a 6f 75 72 6e 61 6c 2c 20 69 74 20 69 73 k journal, it is
143f7 20 6e 6f 74 20 70 75 74 20 74 68 65 72 65 20 62 not put there b
143f8 79 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e y by this routin
143f9 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 66 65 72 65 6e e..**.** Referen
143fa 63 65 73 20 74 6f 20 74 68 65 20 70 61 67 65 20 ces to the page
143fb 70 50 67 20 72 65 6d 61 69 6e 20 76 61 6c 69 64 pPg remain valid
143fc 2e 20 55 70 64 61 74 69 6e 67 20 61 6e 79 0a 2a . Updating any.*
143fd 2a 20 6d 65 74 61 2d 64 61 74 61 20 61 73 73 6f * meta-data asso
143fe 63 69 61 74 65 64 20 77 69 74 68 20 70 50 67 20 ciated with pPg
143ff 28 69 2e 65 2e 20 64 61 74 61 20 73 74 6f 72 65 (i.e. data store
14400 64 20 69 6e 20 74 68 65 20 6e 45 78 74 72 61 20 d in the nExtra
14401 62 79 74 65 73 0a 2a 2a 20 61 6c 6c 6f 63 61 74 bytes.** allocat
14402 65 64 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 ed along with th
14403 65 20 70 61 67 65 29 20 69 73 20 74 68 65 20 72 e page) is the r
14404 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 esponsibility of
14405 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2a 0a the caller..**.
14406 2a 2a 20 41 20 74 72 61 6e 73 61 63 74 69 6f 6e ** A transaction
14407 20 6d 75 73 74 20 62 65 20 61 63 74 69 76 65 20 must be active
14408 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e when this routin
14409 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 49 74 20 e is called. It
1440a 75 73 65 64 20 74 6f 20 62 65 0a 2a 2a 20 72 65 used to be.** re
1440b 71 75 69 72 65 64 20 74 68 61 74 20 61 20 73 74 quired that a st
1440c 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 atement transact
1440d 69 6f 6e 20 77 61 73 20 6e 6f 74 20 61 63 74 69 ion was not acti
1440e 76 65 2c 20 62 75 74 20 74 68 69 73 20 72 65 73 ve, but this res
1440f 74 72 69 63 74 69 6f 6e 0a 2a 2a 20 68 61 73 20 triction.** has
14410 62 65 65 6e 20 72 65 6d 6f 76 65 64 20 28 43 52 been removed (CR
14411 45 41 54 45 20 49 4e 44 45 58 20 6e 65 65 64 73 EATE INDEX needs
14412 20 74 6f 20 6d 6f 76 65 20 61 20 70 61 67 65 20 to move a page
14413 77 68 65 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 when a statement
14414 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 .** transaction
14415 69 73 20 61 63 74 69 76 65 29 2e 0a 2a 2a 0a 2a is active)..**.*
14416 2a 20 49 66 20 74 68 65 20 66 6f 75 72 74 68 20 * If the fourth
14417 61 72 67 75 6d 65 6e 74 2c 20 69 73 43 6f 6d 6d argument, isComm
14418 69 74 2c 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c it, is non-zero,
14419 20 74 68 65 6e 20 74 68 69 73 20 70 61 67 65 20 then this page
1441a 69 73 20 62 65 69 6e 67 0a 2a 2a 20 6d 6f 76 65 is being.** move
1441b 64 20 61 73 20 70 61 72 74 20 6f 66 20 61 20 64 d as part of a d
1441c 61 74 61 62 61 73 65 20 72 65 6f 72 67 61 6e 69 atabase reorgani
1441d 7a 61 74 69 6f 6e 20 6a 75 73 74 20 62 65 66 6f zation just befo
1441e 72 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 re the transacti
1441f 6f 6e 20 0a 2a 2a 20 69 73 20 62 65 69 6e 67 20 on .** is being
14420 63 6f 6d 6d 69 74 74 65 64 2e 20 49 6e 20 74 68 committed. In th
14421 69 73 20 63 61 73 65 2c 20 69 74 20 69 73 20 67 is case, it is g
14422 75 61 72 61 6e 74 65 65 64 20 74 68 61 74 20 74 uaranteed that t
14423 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 he database page
14424 20 0a 2a 2a 20 70 50 67 20 72 65 66 65 72 73 20 .** pPg refers
14425 74 6f 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 77 to will not be w
14426 72 69 74 74 65 6e 20 74 6f 20 61 67 61 69 6e 20 ritten to again
14427 77 69 74 68 69 6e 20 74 68 69 73 20 74 72 61 6e within this tran
14428 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 saction..**.** T
14429 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 his function may
1442a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
1442b 4f 4d 45 4d 20 6f 72 20 61 6e 20 49 4f 20 65 72 OMEM or an IO er
1442c 72 6f 72 20 63 6f 64 65 20 69 66 20 61 6e 20 65 ror code if an e
1442d 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 rror.** occurs.
1442e 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20 72 65 Otherwise, it re
1442f 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e turns SQLITE_OK.
14430 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
14431 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
14432 67 65 72 4d 6f 76 65 70 61 67 65 28 50 61 67 65 gerMovepage(Page
14433 72 20 2a 70 50 61 67 65 72 2c 20 44 62 50 61 67 r *pPager, DbPag
14434 65 20 2a 70 50 67 2c 20 50 67 6e 6f 20 70 67 6e e *pPg, Pgno pgn
14435 6f 2c 20 69 6e 74 20 69 73 43 6f 6d 6d 69 74 29 o, int isCommit)
14436 7b 0a 20 20 50 67 48 64 72 20 2a 70 50 67 4f 6c {. PgHdr *pPgOl
14437 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d;
14438 20 2f 2a 20 54 68 65 20 70 61 67 65 20 62 65 69 /* The page bei
14439 6e 67 20 6f 76 65 72 77 72 69 74 74 65 6e 2e 20 ng overwritten.
1443a 2a 2f 0a 20 20 50 67 6e 6f 20 6e 65 65 64 53 79 */. Pgno needSy
1443b 6e 63 50 67 6e 6f 20 3d 20 30 3b 20 20 20 20 20 ncPgno = 0;
1443c 20 20 2f 2a 20 4f 6c 64 20 76 61 6c 75 65 20 6f /* Old value o
1443d 66 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 69 66 20 f pPg->pgno, if
1443e 73 79 6e 63 20 69 73 20 72 65 71 75 69 72 65 64 sync is required
1443f 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 */. int rc;
14440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14441 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 /* Return cod
14442 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 6f 72 69 67 e */. Pgno orig
14443 50 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 Pgno;
14444 20 20 20 20 2f 2a 20 54 68 65 20 6f 72 69 67 69 /* The origi
14445 6e 61 6c 20 70 61 67 65 20 6e 75 6d 62 65 72 20 nal page number
14446 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 */.. assert( pP
14447 67 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 0a 20 20 g->nRef>0 );..
14448 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 62 /* If the page b
14449 65 69 6e 67 20 6d 6f 76 65 64 20 69 73 20 64 69 eing moved is di
1444a 72 74 79 20 61 6e 64 20 68 61 73 20 6e 6f 74 20 rty and has not
1444b 62 65 65 6e 20 73 61 76 65 64 20 62 79 20 74 68 been saved by th
1444c 65 20 6c 61 74 65 73 74 0a 20 20 2a 2a 20 73 61 e latest. ** sa
1444d 76 65 70 6f 69 6e 74 2c 20 74 68 65 6e 20 73 61 vepoint, then sa
1444e 76 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 ve the current c
1444f 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 ontents of the p
14450 61 67 65 20 69 6e 74 6f 20 74 68 65 20 0a 20 20 age into the .
14451 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 6e ** sub-journal n
14452 6f 77 2e 20 54 68 69 73 20 69 73 20 72 65 71 75 ow. This is requ
14453 69 72 65 64 20 74 6f 20 68 61 6e 64 6c 65 20 74 ired to handle t
14454 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 63 65 he following sce
14455 6e 61 72 69 6f 3a 0a 20 20 2a 2a 0a 20 20 2a 2a nario:. **. **
14456 20 20 20 42 45 47 49 4e 3b 0a 20 20 2a 2a 20 20 BEGIN;. **
14457 20 20 20 3c 6a 6f 75 72 6e 61 6c 20 70 61 67 65 <journal page
14458 20 58 2c 20 74 68 65 6e 20 6d 6f 64 69 66 79 20 X, then modify
14459 69 74 20 69 6e 20 6d 65 6d 6f 72 79 3e 0a 20 20 it in memory>.
1445a 2a 2a 20 20 20 20 20 53 41 56 45 50 4f 49 4e 54 ** SAVEPOINT
1445b 20 6f 6e 65 3b 0a 20 20 2a 2a 20 20 20 20 20 20 one;. **
1445c 20 3c 4d 6f 76 65 20 70 61 67 65 20 58 20 74 6f <Move page X to
1445d 20 6c 6f 63 61 74 69 6f 6e 20 59 3e 0a 20 20 2a location Y>. *
1445e 2a 20 20 20 20 20 52 4f 4c 4c 42 41 43 4b 20 54 * ROLLBACK T
1445f 4f 20 6f 6e 65 3b 0a 20 20 2a 2a 0a 20 20 2a 2a O one;. **. **
14460 20 49 66 20 70 61 67 65 20 58 20 77 65 72 65 20 If page X were
14461 6e 6f 74 20 77 72 69 74 74 65 6e 20 74 6f 20 74 not written to t
14462 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 68 he sub-journal h
14463 65 72 65 2c 20 69 74 20 77 6f 75 6c 64 20 6e 6f ere, it would no
14464 74 0a 20 20 2a 2a 20 62 65 20 70 6f 73 73 69 62 t. ** be possib
14465 6c 65 20 74 6f 20 72 65 73 74 6f 72 65 20 69 74 le to restore it
14466 73 20 63 6f 6e 74 65 6e 74 73 20 77 68 65 6e 20 s contents when
14467 74 68 65 20 22 52 4f 4c 4c 42 41 43 4b 20 54 4f the "ROLLBACK TO
14468 20 6f 6e 65 22 0a 20 20 2a 2a 20 73 74 61 74 65 one". ** state
14469 6d 65 6e 74 20 77 65 72 65 20 69 73 20 70 72 6f ment were is pro
1446a 63 65 73 73 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a cessed.. **. *
1446b 2a 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65 * subjournalPage
1446c 28 29 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 61 () may need to a
1446d 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 74 6f llocate space to
1446e 20 73 74 6f 72 65 20 70 50 67 2d 3e 70 67 6e 6f store pPg->pgno
1446f 20 69 6e 74 6f 0a 20 20 2a 2a 20 6f 6e 65 20 6f into. ** one o
14470 72 20 6d 6f 72 65 20 73 61 76 65 70 6f 69 6e 74 r more savepoint
14471 20 62 69 74 76 65 63 73 2e 20 54 68 69 73 20 69 bitvecs. This i
14472 73 20 74 68 65 20 72 65 61 73 6f 6e 20 74 68 69 s the reason thi
14473 73 20 66 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 s function. **
14474 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 may return SQLIT
14475 45 5f 4e 4f 4d 45 4d 2e 0a 20 20 2a 2f 0a 20 20 E_NOMEM.. */.
14476 69 66 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 if( pPg->flags&P
14477 47 48 44 52 5f 44 49 52 54 59 20 0a 20 20 20 26 GHDR_DIRTY . &
14478 26 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 & subjRequiresPa
14479 67 65 28 70 50 67 29 0a 20 20 20 26 26 20 53 51 ge(pPg). && SQ
1447a 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 LITE_OK!=(rc = s
1447b 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28 70 50 ubjournalPage(pP
1447c 67 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 g)). ){. ret
1447d 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 50 urn rc;. }.. P
1447e 41 47 45 52 54 52 41 43 45 28 28 22 4d 4f 56 45 AGERTRACE(("MOVE
1447f 20 25 64 20 70 61 67 65 20 25 64 20 28 6e 65 65 %d page %d (nee
14480 64 53 79 6e 63 3d 25 64 29 20 6d 6f 76 65 73 20 dSync=%d) moves
14481 74 6f 20 25 64 5c 6e 22 2c 20 0a 20 20 20 20 20 to %d\n", .
14482 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 PAGERID(pPager)
14483 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 28 70 50 , pPg->pgno, (pP
14484 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e g->flags&PGHDR_N
14485 45 45 44 5f 53 59 4e 43 29 3f 31 3a 30 2c 20 70 EED_SYNC)?1:0, p
14486 67 6e 6f 29 29 3b 0a 20 20 49 4f 54 52 41 43 45 gno));. IOTRACE
14487 28 28 22 4d 4f 56 45 20 25 70 20 25 64 20 25 64 (("MOVE %p %d %d
14488 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 67 \n", pPager, pPg
14489 2d 3e 70 67 6e 6f 2c 20 70 67 6e 6f 29 29 0a 0a ->pgno, pgno))..
1448a 20 20 2f 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 /* If the jour
1448b 6e 61 6c 20 6e 65 65 64 73 20 74 6f 20 62 65 20 nal needs to be
1448c 73 79 6e 63 28 29 65 64 20 62 65 66 6f 72 65 20 sync()ed before
1448d 70 61 67 65 20 70 50 67 2d 3e 70 67 6e 6f 20 63 page pPg->pgno c
1448e 61 6e 0a 20 20 2a 2a 20 62 65 20 77 72 69 74 74 an. ** be writt
1448f 65 6e 20 74 6f 2c 20 73 74 6f 72 65 20 70 50 67 en to, store pPg
14490 2d 3e 70 67 6e 6f 20 69 6e 20 6c 6f 63 61 6c 20 ->pgno in local
14491 76 61 72 69 61 62 6c 65 20 6e 65 65 64 53 79 6e variable needSyn
14492 63 50 67 6e 6f 2e 0a 20 20 2a 2a 0a 20 20 2a 2a cPgno.. **. **
14493 20 49 66 20 74 68 65 20 69 73 43 6f 6d 6d 69 74 If the isCommit
14494 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 flag is set, th
14495 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 ere is no need t
14496 6f 20 72 65 6d 65 6d 62 65 72 20 74 68 61 74 0a o remember that.
14497 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c ** the journal
14498 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73 79 6e needs to be syn
14499 63 28 29 65 64 20 62 65 66 6f 72 65 20 64 61 74 c()ed before dat
1449a 61 62 61 73 65 20 70 61 67 65 20 70 50 67 2d 3e abase page pPg->
1449b 70 67 6e 6f 20 0a 20 20 2a 2a 20 63 61 6e 20 62 pgno . ** can b
1449c 65 20 77 72 69 74 74 65 6e 20 74 6f 2e 20 54 68 e written to. Th
1449d 65 20 63 61 6c 6c 65 72 20 68 61 73 20 61 6c 72 e caller has alr
1449e 65 61 64 79 20 70 72 6f 6d 69 73 65 64 20 6e 6f eady promised no
1449f 74 20 74 6f 20 77 72 69 74 65 20 74 6f 20 69 74 t to write to it
144a0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 50 .. */. if( (pP
144a1 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e g->flags&PGHDR_N
144a2 45 45 44 5f 53 59 4e 43 29 20 26 26 20 21 69 73 EED_SYNC) && !is
144a3 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 6e 65 Commit ){. ne
144a4 65 64 53 79 6e 63 50 67 6e 6f 20 3d 20 70 50 67 edSyncPgno = pPg
144a5 2d 3e 70 67 6e 6f 3b 0a 20 20 20 20 61 73 73 65 ->pgno;. asse
144a6 72 74 28 20 70 61 67 65 49 6e 4a 6f 75 72 6e 61 rt( pageInJourna
144a7 6c 28 70 50 67 29 20 7c 7c 20 70 50 67 2d 3e 70 l(pPg) || pPg->p
144a8 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 62 4f 72 gno>pPager->dbOr
144a9 69 67 53 69 7a 65 20 29 3b 0a 20 20 20 20 61 73 igSize );. as
144aa 73 65 72 74 28 20 70 50 67 2d 3e 66 6c 61 67 73 sert( pPg->flags
144ab 26 50 47 48 44 52 5f 44 49 52 54 59 20 29 3b 0a &PGHDR_DIRTY );.
144ac 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
144ad 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 29 3b 0a er->needSync );.
144ae 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 }.. /* If the
144af 20 63 61 63 68 65 20 63 6f 6e 74 61 69 6e 73 20 cache contains
144b0 61 20 70 61 67 65 20 77 69 74 68 20 70 61 67 65 a page with page
144b1 2d 6e 75 6d 62 65 72 20 70 67 6e 6f 2c 20 72 65 -number pgno, re
144b2 6d 6f 76 65 20 69 74 0a 20 20 2a 2a 20 66 72 6f move it. ** fro
144b3 6d 20 69 74 73 20 68 61 73 68 20 63 68 61 69 6e m its hash chain
144b4 2e 20 41 6c 73 6f 2c 20 69 66 20 74 68 65 20 50 . Also, if the P
144b5 67 48 64 72 2e 6e 65 65 64 53 79 6e 63 20 77 61 gHdr.needSync wa
144b6 73 20 73 65 74 20 66 6f 72 20 0a 20 20 2a 2a 20 s set for . **
144b7 70 61 67 65 20 70 67 6e 6f 20 62 65 66 6f 72 65 page pgno before
144b8 20 74 68 65 20 27 6d 6f 76 65 27 20 6f 70 65 72 the 'move' oper
144b9 61 74 69 6f 6e 2c 20 69 74 20 6e 65 65 64 73 20 ation, it needs
144ba 74 6f 20 62 65 20 72 65 74 61 69 6e 65 64 20 0a to be retained .
144bb 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 70 61 67 ** for the pag
144bc 65 20 6d 6f 76 65 64 20 74 68 65 72 65 2e 0a 20 e moved there..
144bd 20 2a 2f 0a 20 20 70 50 67 2d 3e 66 6c 61 67 73 */. pPg->flags
144be 20 26 3d 20 7e 50 47 48 44 52 5f 4e 45 45 44 5f &= ~PGHDR_NEED_
144bf 53 59 4e 43 3b 0a 20 20 70 50 67 4f 6c 64 20 3d SYNC;. pPgOld =
144c0 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 pager_lookup(pP
144c1 61 67 65 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 61 ager, pgno);. a
144c2 73 73 65 72 74 28 20 21 70 50 67 4f 6c 64 20 7c ssert( !pPgOld |
144c3 7c 20 70 50 67 4f 6c 64 2d 3e 6e 52 65 66 3d 3d | pPgOld->nRef==
144c4 31 20 29 3b 0a 20 20 69 66 28 20 70 50 67 4f 6c 1 );. if( pPgOl
144c5 64 20 29 7b 0a 20 20 20 20 70 50 67 2d 3e 66 6c d ){. pPg->fl
144c6 61 67 73 20 7c 3d 20 28 70 50 67 4f 6c 64 2d 3e ags |= (pPgOld->
144c7 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 flags&PGHDR_NEED
144c8 5f 53 59 4e 43 29 3b 0a 20 20 20 20 73 71 6c 69 _SYNC);. sqli
144c9 74 65 33 50 63 61 63 68 65 44 72 6f 70 28 70 50 te3PcacheDrop(pP
144ca 67 4f 6c 64 29 3b 0a 20 20 7d 0a 0a 20 20 6f 72 gOld);. }.. or
144cb 69 67 50 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 igPgno = pPg->pg
144cc 6e 6f 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 no;. sqlite3Pca
144cd 63 68 65 4d 6f 76 65 28 70 50 67 2c 20 70 67 6e cheMove(pPg, pgn
144ce 6f 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 o);. sqlite3Pca
144cf 63 68 65 4d 61 6b 65 44 69 72 74 79 28 70 50 67 cheMakeDirty(pPg
144d0 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 4d );. pPager->dbM
144d1 6f 64 69 66 69 65 64 20 3d 20 31 3b 0a 0a 20 20 odified = 1;..
144d2 69 66 28 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f if( needSyncPgno
144d3 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 6e 65 ){. /* If ne
144d4 65 64 53 79 6e 63 50 67 6e 6f 20 69 73 20 6e 6f edSyncPgno is no
144d5 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 n-zero, then the
144d6 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 65 journal file ne
144d7 65 64 73 20 74 6f 20 62 65 20 0a 20 20 20 20 2a eds to be . *
144d8 2a 20 73 79 6e 63 28 29 65 64 20 62 65 66 6f 72 * sync()ed befor
144d9 65 20 61 6e 79 20 64 61 74 61 20 69 73 20 77 72 e any data is wr
144da 69 74 74 65 6e 20 74 6f 20 64 61 74 61 62 61 73 itten to databas
144db 65 20 66 69 6c 65 20 70 61 67 65 20 6e 65 65 64 e file page need
144dc 53 79 6e 63 50 67 6e 6f 2e 0a 20 20 20 20 2a 2a SyncPgno.. **
144dd 20 43 75 72 72 65 6e 74 6c 79 2c 20 6e 6f 20 73 Currently, no s
144de 75 63 68 20 70 61 67 65 20 65 78 69 73 74 73 20 uch page exists
144df 69 6e 20 74 68 65 20 70 61 67 65 2d 63 61 63 68 in the page-cach
144e0 65 20 61 6e 64 20 74 68 65 20 0a 20 20 20 20 2a e and the . *
144e1 2a 20 22 69 73 20 6a 6f 75 72 6e 61 6c 65 64 22 * "is journaled"
144e2 20 62 69 74 76 65 63 20 66 6c 61 67 20 68 61 73 bitvec flag has
144e3 20 62 65 65 6e 20 73 65 74 2e 20 54 68 69 73 20 been set. This
144e4 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 6d 65 needs to be reme
144e5 64 69 65 64 20 62 79 0a 20 20 20 20 2a 2a 20 6c died by. ** l
144e6 6f 61 64 69 6e 67 20 74 68 65 20 70 61 67 65 20 oading the page
144e7 69 6e 74 6f 20 74 68 65 20 70 61 67 65 72 2d 63 into the pager-c
144e8 61 63 68 65 20 61 6e 64 20 73 65 74 74 69 6e 67 ache and setting
144e9 20 74 68 65 20 50 67 48 64 72 2e 6e 65 65 64 53 the PgHdr.needS
144ea 79 6e 63 20 0a 20 20 20 20 2a 2a 20 66 6c 61 67 ync . ** flag
144eb 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
144ec 49 66 20 74 68 65 20 61 74 74 65 6d 70 74 20 74 If the attempt t
144ed 6f 20 6c 6f 61 64 20 74 68 65 20 70 61 67 65 20 o load the page
144ee 69 6e 74 6f 20 74 68 65 20 70 61 67 65 2d 63 61 into the page-ca
144ef 63 68 65 20 66 61 69 6c 73 2c 20 28 64 75 65 0a che fails, (due.
144f0 20 20 20 20 2a 2a 20 74 6f 20 61 20 6d 61 6c 6c ** to a mall
144f1 6f 63 28 29 20 6f 72 20 49 4f 20 66 61 69 6c 75 oc() or IO failu
144f2 72 65 29 2c 20 63 6c 65 61 72 20 74 68 65 20 62 re), clear the b
144f3 69 74 20 69 6e 20 74 68 65 20 70 49 6e 4a 6f 75 it in the pInJou
144f4 72 6e 61 6c 5b 5d 0a 20 20 20 20 2a 2a 20 61 72 rnal[]. ** ar
144f5 72 61 79 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 ray. Otherwise,
144f6 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6c if the page is l
144f7 6f 61 64 65 64 20 61 6e 64 20 77 72 69 74 74 65 oaded and writte
144f8 6e 20 61 67 61 69 6e 20 69 6e 0a 20 20 20 20 2a n again in. *
144f9 2a 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 * this transacti
144fa 6f 6e 2c 20 69 74 20 6d 61 79 20 62 65 20 77 72 on, it may be wr
144fb 69 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61 74 itten to the dat
144fc 61 62 61 73 65 20 66 69 6c 65 20 62 65 66 6f 72 abase file befor
144fd 65 0a 20 20 20 20 2a 2a 20 69 74 20 69 73 20 73 e. ** it is s
144fe 79 6e 63 65 64 20 69 6e 74 6f 20 74 68 65 20 6a ynced into the j
144ff 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 69 ournal file. Thi
14500 73 20 77 61 79 2c 20 69 74 20 6d 61 79 20 65 6e s way, it may en
14501 64 20 75 70 20 69 6e 0a 20 20 20 20 2a 2a 20 74 d up in. ** t
14502 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
14503 74 77 69 63 65 2c 20 62 75 74 20 74 68 61 74 20 twice, but that
14504 69 73 20 6e 6f 74 20 61 20 70 72 6f 62 6c 65 6d is not a problem
14505 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
14506 54 68 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 The sqlite3Pager
14507 47 65 74 28 29 20 63 61 6c 6c 20 6d 61 79 20 63 Get() call may c
14508 61 75 73 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c ause the journal
14509 20 74 6f 20 73 79 6e 63 2e 20 53 6f 20 6d 61 6b to sync. So mak
1450a 65 0a 20 20 20 20 2a 2a 20 73 75 72 65 20 74 68 e. ** sure th
1450b 65 20 50 61 67 65 72 2e 6e 65 65 64 53 79 6e 63 e Pager.needSync
1450c 20 66 6c 61 67 20 69 73 20 73 65 74 20 74 6f 6f flag is set too
1450d 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 50 67 48 .. */. PgH
1450e 64 72 20 2a 70 50 67 48 64 72 3b 0a 20 20 20 20 dr *pPgHdr;.
1450f 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
14510 6e 65 65 64 53 79 6e 63 20 29 3b 0a 20 20 20 20 needSync );.
14511 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
14512 72 47 65 74 28 70 50 61 67 65 72 2c 20 6e 65 65 rGet(pPager, nee
14513 64 53 79 6e 63 50 67 6e 6f 2c 20 26 70 50 67 48 dSyncPgno, &pPgH
14514 64 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 dr);. if( rc!
14515 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
14516 20 20 20 20 69 66 28 20 6e 65 65 64 53 79 6e 63 if( needSync
14517 50 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62 Pgno<=pPager->db
14518 4f 72 69 67 53 69 7a 65 20 29 7b 0a 20 20 20 20 OrigSize ){.
14519 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
1451a 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 21 3d 30 er->pTmpSpace!=0
1451b 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 );. sqli
1451c 74 65 33 42 69 74 76 65 63 43 6c 65 61 72 28 70 te3BitvecClear(p
1451d 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 Pager->pInJourna
1451e 6c 2c 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 2c l, needSyncPgno,
1451f 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 pPager->pTmpSpa
14520 63 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ce);. }.
14521 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
14522 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e }. pPager->
14523 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 needSync = 1;.
14524 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
14525 2d 3e 6e 6f 53 79 6e 63 3d 3d 30 20 26 26 20 21 ->noSync==0 && !
14526 4d 45 4d 44 42 20 29 3b 0a 20 20 20 20 70 50 67 MEMDB );. pPg
14527 48 64 72 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 Hdr->flags |= PG
14528 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 HDR_NEED_SYNC;.
14529 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 sqlite3Pcache
1452a 4d 61 6b 65 44 69 72 74 79 28 70 50 67 48 64 72 MakeDirty(pPgHdr
1452b 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 );. sqlite3Pa
1452c 67 65 72 55 6e 72 65 66 28 70 50 67 48 64 72 29 gerUnref(pPgHdr)
1452d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a ;. }.. /*. **
1452e 20 46 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 For an in-memor
1452f 79 20 64 61 74 61 62 61 73 65 2c 20 6d 61 6b 65 y database, make
14530 20 73 75 72 65 20 74 68 65 20 6f 72 69 67 69 6e sure the origin
14531 61 6c 20 70 61 67 65 20 63 6f 6e 74 69 6e 75 65 al page continue
14532 73 0a 20 20 2a 2a 20 74 6f 20 65 78 69 73 74 2c s. ** to exist,
14533 20 69 6e 20 63 61 73 65 20 74 68 65 20 74 72 61 in case the tra
14534 6e 73 61 63 74 69 6f 6e 20 6e 65 65 64 73 20 74 nsaction needs t
14535 6f 20 72 6f 6c 6c 20 62 61 63 6b 2e 20 20 57 65 o roll back. We
14536 20 61 6c 6c 6f 63 61 74 65 0a 20 20 2a 2a 20 74 allocate. ** t
14537 68 65 20 70 61 67 65 20 6e 6f 77 2c 20 69 6e 73 he page now, ins
14538 74 65 61 64 20 6f 66 20 61 74 20 72 6f 6c 6c 62 tead of at rollb
14539 61 63 6b 2c 20 62 65 63 61 75 73 65 20 77 65 20 ack, because we
1453a 63 61 6e 20 62 65 74 74 65 72 20 64 65 61 6c 0a can better deal.
1453b 20 20 2a 2a 20 77 69 74 68 20 61 6e 20 6f 75 74 ** with an out
1453c 2d 6f 66 2d 6d 65 6d 6f 72 79 20 65 72 72 6f 72 -of-memory error
1453d 20 6e 6f 77 2e 20 20 54 69 63 6b 65 74 20 23 33 now. Ticket #3
1453e 37 36 31 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 761.. */. if(
1453f 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 44 62 50 MEMDB ){. DbP
14540 61 67 65 20 2a 70 4e 65 77 3b 0a 20 20 20 20 72 age *pNew;. r
14541 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
14542 41 63 71 75 69 72 65 28 70 50 61 67 65 72 2c 20 Acquire(pPager,
14543 6f 72 69 67 50 67 6e 6f 2c 20 26 70 4e 65 77 2c origPgno, &pNew,
14544 20 31 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 1);. if( rc!
14545 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
14546 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 sqlite3Pcach
14547 65 4d 6f 76 65 28 70 50 67 2c 20 6f 72 69 67 50 eMove(pPg, origP
14548 67 6e 6f 29 3b 0a 20 20 20 20 20 20 72 65 74 75 gno);. retu
14549 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 rn rc;. }.
1454a 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 sqlite3PagerUnr
1454b 65 66 28 70 4e 65 77 29 3b 0a 20 20 7d 0a 0a 20 ef(pNew);. }..
1454c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
1454d 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a K;.}.#endif../*.
1454e 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e ** Return a poin
1454f 74 65 72 20 74 6f 20 74 68 65 20 64 61 74 61 20 ter to the data
14550 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 65 for the specifie
14551 64 20 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 d page..*/.SQLIT
14552 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a E_PRIVATE void *
14553 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 sqlite3PagerGetD
14554 61 74 61 28 44 62 50 61 67 65 20 2a 70 50 67 29 ata(DbPage *pPg)
14555 7b 0a 20 20 61 73 73 65 72 74 28 20 70 50 67 2d {. assert( pPg-
14556 3e 6e 52 65 66 3e 30 20 7c 7c 20 70 50 67 2d 3e >nRef>0 || pPg->
14557 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 20 29 3b pPager->memDb );
14558 0a 20 20 72 65 74 75 72 6e 20 70 50 67 2d 3e 70 . return pPg->p
14559 44 61 74 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 Data;.}../*.** R
1455a 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 eturn a pointer
1455b 74 6f 20 74 68 65 20 50 61 67 65 72 2e 6e 45 78 to the Pager.nEx
1455c 74 72 61 20 62 79 74 65 73 20 6f 66 20 22 65 78 tra bytes of "ex
1455d 74 72 61 22 20 73 70 61 63 65 20 0a 2a 2a 20 61 tra" space .** a
1455e 6c 6c 6f 63 61 74 65 64 20 61 6c 6f 6e 67 20 77 llocated along w
1455f 69 74 68 20 74 68 65 20 73 70 65 63 69 66 69 65 ith the specifie
14560 64 20 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 d page..*/.SQLIT
14561 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a E_PRIVATE void *
14562 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 sqlite3PagerGetE
14563 78 74 72 61 28 44 62 50 61 67 65 20 2a 70 50 67 xtra(DbPage *pPg
14564 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 67 2d ){. return pPg-
14565 3e 70 45 78 74 72 61 3b 0a 7d 0a 0a 2f 2a 0a 2a >pExtra;.}../*.*
14566 2a 20 47 65 74 2f 73 65 74 20 74 68 65 20 6c 6f * Get/set the lo
14567 63 6b 69 6e 67 2d 6d 6f 64 65 20 66 6f 72 20 74 cking-mode for t
14568 68 69 73 20 70 61 67 65 72 2e 20 50 61 72 61 6d his pager. Param
14569 65 74 65 72 20 65 4d 6f 64 65 20 6d 75 73 74 20 eter eMode must
1456a 62 65 20 6f 6e 65 0a 2a 2a 20 6f 66 20 50 41 47 be one.** of PAG
1456b 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 ER_LOCKINGMODE_Q
1456c 55 45 52 59 2c 20 50 41 47 45 52 5f 4c 4f 43 4b UERY, PAGER_LOCK
1456d 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 6f INGMODE_NORMAL o
1456e 72 20 0a 2a 2a 20 50 41 47 45 52 5f 4c 4f 43 4b r .** PAGER_LOCK
1456f 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 INGMODE_EXCLUSIV
14570 45 2e 20 49 66 20 74 68 65 20 70 61 72 61 6d 65 E. If the parame
14571 74 65 72 20 69 73 20 6e 6f 74 20 5f 51 55 45 52 ter is not _QUER
14572 59 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 6c Y, then.** the l
14573 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 69 73 20 73 ocking-mode is s
14574 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 et to the value
14575 73 70 65 63 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a specified..**.**
14576 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 76 61 The returned va
14577 6c 75 65 20 69 73 20 65 69 74 68 65 72 20 50 41 lue is either PA
14578 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f GER_LOCKINGMODE_
14579 4e 4f 52 4d 41 4c 20 6f 72 0a 2a 2a 20 50 41 47 NORMAL or.** PAG
1457a 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 ER_LOCKINGMODE_E
1457b 58 43 4c 55 53 49 56 45 2c 20 69 6e 64 69 63 61 XCLUSIVE, indica
1457c 74 69 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74 ting the current
1457d 20 28 70 6f 73 73 69 62 6c 79 20 75 70 64 61 74 (possibly updat
1457e 65 64 29 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 2d 6d ed).** locking-m
1457f 6f 64 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ode..*/.SQLITE_P
14580 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
14581 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f e3PagerLockingMo
14582 64 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 de(Pager *pPager
14583 2c 20 69 6e 74 20 65 4d 6f 64 65 29 7b 0a 20 20 , int eMode){.
14584 61 73 73 65 72 74 28 20 65 4d 6f 64 65 3d 3d 50 assert( eMode==P
14585 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 AGER_LOCKINGMODE
14586 5f 51 55 45 52 59 0a 20 20 20 20 20 20 20 20 20 _QUERY.
14587 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 || eMode==PAG
14588 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e ER_LOCKINGMODE_N
14589 4f 52 4d 41 4c 0a 20 20 20 20 20 20 20 20 20 20 ORMAL.
1458a 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 || eMode==PAGE
1458b 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 R_LOCKINGMODE_EX
1458c 43 4c 55 53 49 56 45 20 29 3b 0a 20 20 61 73 73 CLUSIVE );. ass
1458d 65 72 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49 ert( PAGER_LOCKI
1458e 4e 47 4d 4f 44 45 5f 51 55 45 52 59 3c 30 20 29 NGMODE_QUERY<0 )
1458f 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 45 ;. assert( PAGE
14590 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f R_LOCKINGMODE_NO
14591 52 4d 41 4c 3e 3d 30 20 26 26 20 50 41 47 45 52 RMAL>=0 && PAGER
14592 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 _LOCKINGMODE_EXC
14593 4c 55 53 49 56 45 3e 3d 30 20 29 3b 0a 20 20 69 LUSIVE>=0 );. i
14594 66 28 20 65 4d 6f 64 65 3e 3d 30 20 26 26 20 21 f( eMode>=0 && !
14595 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 pPager->tempFile
14596 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e ){. pPager->
14597 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20 exclusiveMode =
14598 28 75 38 29 65 4d 6f 64 65 3b 0a 20 20 7d 0a 20 (u8)eMode;. }.
14599 20 72 65 74 75 72 6e 20 28 69 6e 74 29 70 50 61 return (int)pPa
1459a 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f ger->exclusiveMo
1459b 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 de;.}../*.** Get
1459c 2f 73 65 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c /set the journal
1459d 2d 6d 6f 64 65 20 66 6f 72 20 74 68 69 73 20 70 -mode for this p
1459e 61 67 65 72 2e 20 50 61 72 61 6d 65 74 65 72 20 ager. Parameter
1459f 65 4d 6f 64 65 20 6d 75 73 74 20 62 65 20 6f 6e eMode must be on
145a0 65 20 6f 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 50 e of:.**.** P
145a1 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
145a2 5f 51 55 45 52 59 0a 2a 2a 20 20 20 20 50 41 47 _QUERY.** PAG
145a3 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 ER_JOURNALMODE_D
145a4 45 4c 45 54 45 0a 2a 2a 20 20 20 20 50 41 47 45 ELETE.** PAGE
145a5 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 R_JOURNALMODE_TR
145a6 55 4e 43 41 54 45 0a 2a 2a 20 20 20 20 50 41 47 UNCATE.** PAG
145a7 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 ER_JOURNALMODE_P
145a8 45 52 53 49 53 54 0a 2a 2a 20 20 20 20 50 41 47 ERSIST.** PAG
145a9 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f ER_JOURNALMODE_O
145aa 46 46 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a FF.** PAGER_J
145ab 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 OURNALMODE_MEMOR
145ac 59 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 Y.**.** If the p
145ad 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 arameter is not
145ae 5f 51 55 45 52 59 2c 20 74 68 65 6e 20 74 68 65 _QUERY, then the
145af 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 20 69 73 journal_mode is
145b0 20 73 65 74 20 74 6f 20 74 68 65 0a 2a 2a 20 76 set to the.** v
145b1 61 6c 75 65 20 73 70 65 63 69 66 69 65 64 20 69 alue specified i
145b2 66 20 74 68 65 20 63 68 61 6e 67 65 20 69 73 20 f the change is
145b3 61 6c 6c 6f 77 65 64 2e 20 20 54 68 65 20 63 68 allowed. The ch
145b4 61 6e 67 65 20 69 73 20 64 69 73 61 6c 6c 6f 77 ange is disallow
145b5 65 64 0a 2a 2a 20 66 6f 72 20 74 68 65 20 66 6f ed.** for the fo
145b6 6c 6c 6f 77 69 6e 67 20 72 65 61 73 6f 6e 73 3a llowing reasons:
145b7 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 41 6e 20 69 .**.** * An i
145b8 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 n-memory databas
145b9 65 20 63 61 6e 20 6f 6e 6c 79 20 68 61 76 65 20 e can only have
145ba 69 74 73 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 its journal_mode
145bb 20 73 65 74 20 74 6f 20 5f 4f 46 46 0a 2a 2a 20 set to _OFF.**
145bc 20 20 20 20 20 6f 72 20 5f 4d 45 4d 4f 52 59 2e or _MEMORY.
145bd 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 54 68 65 20 .**.** * The
145be 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 20 6d 61 79 journal mode may
145bf 20 6e 6f 74 20 62 65 20 63 68 61 6e 67 65 64 20 not be changed
145c0 77 68 69 6c 65 20 61 20 74 72 61 6e 73 61 63 74 while a transact
145c1 69 6f 6e 20 69 73 20 61 63 74 69 76 65 2e 0a 2a ion is active..*
145c2 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 *.** The returne
145c3 64 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 63 d indicate the c
145c4 75 72 72 65 6e 74 20 28 70 6f 73 73 69 62 6c 79 urrent (possibly
145c5 20 75 70 64 61 74 65 64 29 20 6a 6f 75 72 6e 61 updated) journa
145c6 6c 2d 6d 6f 64 65 2e 0a 2a 2f 0a 53 51 4c 49 54 l-mode..*/.SQLIT
145c7 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
145c8 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 lite3PagerJourna
145c9 6c 4d 6f 64 65 28 50 61 67 65 72 20 2a 70 50 61 lMode(Pager *pPa
145ca 67 65 72 2c 20 69 6e 74 20 65 4d 6f 64 65 29 7b ger, int eMode){
145cb 0a 20 20 61 73 73 65 72 74 28 20 65 4d 6f 64 65 . assert( eMode
145cc 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d ==PAGER_JOURNALM
145cd 4f 44 45 5f 51 55 45 52 59 0a 20 20 20 20 20 20 ODE_QUERY.
145ce 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d || eMode==
145cf 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
145d0 45 5f 44 45 4c 45 54 45 0a 20 20 20 20 20 20 20 E_DELETE.
145d1 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 || eMode==P
145d2 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
145d3 5f 54 52 55 4e 43 41 54 45 0a 20 20 20 20 20 20 _TRUNCATE.
145d4 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d || eMode==
145d5 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
145d6 45 5f 50 45 52 53 49 53 54 0a 20 20 20 20 20 20 E_PERSIST.
145d7 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d || eMode==
145d8 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
145d9 45 5f 4f 46 46 20 0a 20 20 20 20 20 20 20 20 20 E_OFF .
145da 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 || eMode==PAG
145db 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d ER_JOURNALMODE_M
145dc 45 4d 4f 52 59 20 29 3b 0a 20 20 61 73 73 65 72 EMORY );. asser
145dd 74 28 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c t( PAGER_JOURNAL
145de 4d 4f 44 45 5f 51 55 45 52 59 3c 30 20 29 3b 0a MODE_QUERY<0 );.
145df 20 20 69 66 28 20 65 4d 6f 64 65 3e 3d 30 0a 20 if( eMode>=0.
145e0 20 20 26 26 20 28 21 4d 45 4d 44 42 20 7c 7c 20 && (!MEMDB ||
145e1 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 eMode==PAGER_JOU
145e2 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 RNALMODE_MEMORY
145e3 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c . |
145e4 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a | eMode==PAGER_J
145e5 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 29 0a OURNALMODE_OFF).
145e6 20 20 20 26 26 20 21 70 50 61 67 65 72 2d 3e 64 && !pPager->d
145e7 62 4d 6f 64 69 66 69 65 64 0a 20 20 20 26 26 20 bModified. &&
145e8 28 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d (!isOpen(pPager-
145e9 3e 6a 66 64 29 20 7c 7c 20 30 3d 3d 70 50 61 67 >jfd) || 0==pPag
145ea 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 0a er->journalOff).
145eb 20 20 29 7b 0a 20 20 20 20 69 66 28 20 69 73 4f ){. if( isO
145ec 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 pen(pPager->jfd)
145ed 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
145ee 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3OsClose(pPager-
145ef 3e 6a 66 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 >jfd);. }.
145f0 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
145f1 4d 6f 64 65 20 3d 20 28 75 38 29 65 4d 6f 64 65 Mode = (u8)eMode
145f2 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 ;. }. return (
145f3 69 6e 74 29 70 50 61 67 65 72 2d 3e 6a 6f 75 72 int)pPager->jour
145f4 6e 61 6c 4d 6f 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a nalMode;.}../*.*
145f5 2a 20 47 65 74 2f 73 65 74 20 74 68 65 20 73 69 * Get/set the si
145f6 7a 65 2d 6c 69 6d 69 74 20 75 73 65 64 20 66 6f ze-limit used fo
145f7 72 20 70 65 72 73 69 73 74 65 6e 74 20 6a 6f 75 r persistent jou
145f8 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a rnal files..**.*
145f9 2a 20 53 65 74 74 69 6e 67 20 74 68 65 20 73 69 * Setting the si
145fa 7a 65 20 6c 69 6d 69 74 20 74 6f 20 2d 31 20 6d ze limit to -1 m
145fb 65 61 6e 73 20 6e 6f 20 6c 69 6d 69 74 20 69 73 eans no limit is
145fc 20 65 6e 66 6f 72 63 65 64 2e 0a 2a 2a 20 41 6e enforced..** An
145fd 20 61 74 74 65 6d 70 74 20 74 6f 20 73 65 74 20 attempt to set
145fe 61 20 6c 69 6d 69 74 20 73 6d 61 6c 6c 65 72 20 a limit smaller
145ff 74 68 61 6e 20 2d 31 20 69 73 20 61 20 6e 6f 2d than -1 is a no-
14600 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 op..*/.SQLITE_PR
14601 49 56 41 54 45 20 69 36 34 20 73 71 6c 69 74 65 IVATE i64 sqlite
14602 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 53 69 7a 3PagerJournalSiz
14603 65 4c 69 6d 69 74 28 50 61 67 65 72 20 2a 70 50 eLimit(Pager *pP
14604 61 67 65 72 2c 20 69 36 34 20 69 4c 69 6d 69 74 ager, i64 iLimit
14605 29 7b 0a 20 20 69 66 28 20 69 4c 69 6d 69 74 3e ){. if( iLimit>
14606 3d 2d 31 20 29 7b 0a 20 20 20 20 70 50 61 67 65 =-1 ){. pPage
14607 72 2d 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 r->journalSizeLi
14608 6d 69 74 20 3d 20 69 4c 69 6d 69 74 3b 0a 20 20 mit = iLimit;.
14609 7d 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 }. return pPage
1460a 72 2d 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 r->journalSizeLi
1460b 6d 69 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 mit;.}../*.** Re
1460c 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 turn a pointer t
1460d 6f 20 74 68 65 20 70 50 61 67 65 72 2d 3e 70 42 o the pPager->pB
1460e 61 63 6b 75 70 20 76 61 72 69 61 62 6c 65 2e 20 ackup variable.
1460f 54 68 65 20 62 61 63 6b 75 70 20 6d 6f 64 75 6c The backup modul
14610 65 0a 2a 2a 20 69 6e 20 62 61 63 6b 75 70 2e 63 e.** in backup.c
14611 20 6d 61 69 6e 74 61 69 6e 73 20 74 68 65 20 63 maintains the c
14612 6f 6e 74 65 6e 74 20 6f 66 20 74 68 69 73 20 76 ontent of this v
14613 61 72 69 61 62 6c 65 2e 20 54 68 69 73 20 6d 6f ariable. This mo
14614 64 75 6c 65 0a 2a 2a 20 75 73 65 73 20 69 74 20 dule.** uses it
14615 6f 70 61 71 75 65 6c 79 20 61 73 20 61 6e 20 61 opaquely as an a
14616 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 rgument to sqlit
14617 65 33 42 61 63 6b 75 70 52 65 73 74 61 72 74 28 e3BackupRestart(
14618 29 20 61 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33 ) and.** sqlite3
14619 42 61 63 6b 75 70 55 70 64 61 74 65 28 29 20 6f BackupUpdate() o
1461a 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 nly..*/.SQLITE_P
1461b 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 62 RIVATE sqlite3_b
1461c 61 63 6b 75 70 20 2a 2a 73 71 6c 69 74 65 33 50 ackup **sqlite3P
1461d 61 67 65 72 42 61 63 6b 75 70 50 74 72 28 50 61 agerBackupPtr(Pa
1461e 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
1461f 72 65 74 75 72 6e 20 26 70 50 61 67 65 72 2d 3e return &pPager->
14620 70 42 61 63 6b 75 70 3b 0a 7d 0a 0a 23 65 6e 64 pBackup;.}..#end
14621 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
14622 54 5f 44 49 53 4b 49 4f 20 2a 2f 0a 0a 2f 2a 2a T_DISKIO */../**
14623 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
14624 20 6f 66 20 70 61 67 65 72 2e 63 20 2a 2a 2a 2a of pager.c ****
14625 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14626 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14627 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
14628 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
14629 69 6e 20 66 69 6c 65 20 62 74 6d 75 74 65 78 2e in file btmutex.
1462a 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
1462b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1462c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
1462d 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 32 ** 2007 August 2
1462e 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 7.**.** The auth
1462f 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
14630 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
14631 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
14632 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
14633 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
14634 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
14635 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
14636 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
14637 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
14638 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
14639 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
1463a 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
1463b 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
1463c 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
1463d 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
1463e 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
1463f 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
14640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14641 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
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 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 62 74 6d 75 .**.** $Id: btmu
14645 74 65 78 2e 63 2c 76 20 31 2e 31 37 20 32 30 30 tex.c,v 1.17 200
14646 39 2f 30 37 2f 32 30 20 31 32 3a 33 33 3a 33 33 9/07/20 12:33:33
14647 20 64 72 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a drh Exp $.**.**
14648 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
14649 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f ins code used to
1464a 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 implement mutex
1464b 65 73 20 6f 6e 20 42 74 72 65 65 20 6f 62 6a 65 es on Btree obje
1464c 63 74 73 2e 0a 2a 2a 20 54 68 69 73 20 63 6f 64 cts..** This cod
1464d 65 20 72 65 61 6c 6c 79 20 62 65 6c 6f 6e 67 73 e really belongs
1464e 20 69 6e 20 62 74 72 65 65 2e 63 2e 20 20 42 75 in btree.c. Bu
1464f 74 20 62 74 72 65 65 2e 63 20 69 73 20 67 65 74 t btree.c is get
14650 74 69 6e 67 20 74 6f 6f 0a 2a 2a 20 62 69 67 20 ting too.** big
14651 61 6e 64 20 77 65 20 77 61 6e 74 20 74 6f 20 62 and we want to b
14652 72 65 61 6b 20 69 74 20 64 6f 77 6e 20 73 6f 6d reak it down som
14653 65 2e 20 20 54 68 69 73 20 70 61 63 6b 61 67 65 e. This package
14654 64 20 73 65 65 6d 65 64 20 6c 69 6b 65 0a 2a 2a d seemed like.**
14655 20 61 20 67 6f 6f 64 20 62 72 65 61 6b 6f 75 74 a good breakout
14656 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..*/./**********
14657 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 62 74 72 **** Include btr
14658 65 65 49 6e 74 2e 68 20 69 6e 20 74 68 65 20 6d eeInt.h in the m
14659 69 64 64 6c 65 20 6f 66 20 62 74 6d 75 74 65 78 iddle of btmutex
1465a 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
1465b 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
1465c 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
1465d 62 74 72 65 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a btreeInt.h *****
1465e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1465f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14660 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 ***/./*.** 2004
14661 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68 April 6.**.** Th
14662 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
14663 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
14664 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
14665 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
14666 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
14667 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
14668 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
14669 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
1466a 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
1466b 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
1466c 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
1466d 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
1466e 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
1466f 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
14670 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
14671 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
14672 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
14673 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14674 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14675 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14676 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14677 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 62 ******.** $Id: b
14678 74 72 65 65 49 6e 74 2e 68 2c 76 20 31 2e 35 32 treeInt.h,v 1.52
14679 20 32 30 30 39 2f 30 37 2f 31 35 20 31 37 3a 32 2009/07/15 17:2
1467a 35 3a 34 36 20 64 72 68 20 45 78 70 20 24 0a 2a 5:46 drh Exp $.*
1467b 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 *.** This file i
1467c 6d 70 6c 65 6d 65 6e 74 73 20 61 20 65 78 74 65 mplements a exte
1467d 72 6e 61 6c 20 28 64 69 73 6b 2d 62 61 73 65 64 rnal (disk-based
1467e 29 20 64 61 74 61 62 61 73 65 20 75 73 69 6e 67 ) database using
1467f 20 42 54 72 65 65 73 2e 0a 2a 2a 20 46 6f 72 20 BTrees..** For
14680 61 20 64 65 74 61 69 6c 65 64 20 64 69 73 63 75 a detailed discu
14681 73 73 69 6f 6e 20 6f 66 20 42 54 72 65 65 73 2c ssion of BTrees,
14682 20 72 65 66 65 72 20 74 6f 0a 2a 2a 0a 2a 2a 20 refer to.**.**
14683 20 20 20 20 44 6f 6e 61 6c 64 20 45 2e 20 4b 6e Donald E. Kn
14684 75 74 68 2c 20 54 48 45 20 41 52 54 20 4f 46 20 uth, THE ART OF
14685 43 4f 4d 50 55 54 45 52 20 50 52 4f 47 52 41 4d COMPUTER PROGRAM
14686 4d 49 4e 47 2c 20 56 6f 6c 75 6d 65 20 33 3a 0a MING, Volume 3:.
14687 2a 2a 20 20 20 20 20 22 53 6f 72 74 69 6e 67 20 ** "Sorting
14688 41 6e 64 20 53 65 61 72 63 68 69 6e 67 22 2c 20 And Searching",
14689 70 61 67 65 73 20 34 37 33 2d 34 38 30 2e 20 41 pages 473-480. A
1468a 64 64 69 73 6f 6e 2d 57 65 73 6c 65 79 0a 2a 2a ddison-Wesley.**
1468b 20 20 20 20 20 50 75 62 6c 69 73 68 69 6e 67 20 Publishing
1468c 43 6f 6d 70 61 6e 79 2c 20 52 65 61 64 69 6e 67 Company, Reading
1468d 2c 20 4d 61 73 73 61 63 68 75 73 65 74 74 73 2e , Massachusetts.
1468e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62 61 73 69 63 .**.** The basic
1468f 20 69 64 65 61 20 69 73 20 74 68 61 74 20 65 61 idea is that ea
14690 63 68 20 70 61 67 65 20 6f 66 20 74 68 65 20 66 ch page of the f
14691 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 4e 20 64 ile contains N d
14692 61 74 61 62 61 73 65 0a 2a 2a 20 65 6e 74 72 69 atabase.** entri
14693 65 73 20 61 6e 64 20 4e 2b 31 20 70 6f 69 6e 74 es and N+1 point
14694 65 72 73 20 74 6f 20 73 75 62 70 61 67 65 73 2e ers to subpages.
14695 0a 2a 2a 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d .**.** -------
14696 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
14697 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
14698 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
14699 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 7c ---------.** |
1469a 20 20 50 74 72 28 30 29 20 7c 20 4b 65 79 28 30 Ptr(0) | Key(0
1469b 29 20 7c 20 50 74 72 28 31 29 20 7c 20 4b 65 79 ) | Ptr(1) | Key
1469c 28 31 29 20 7c 20 2e 2e 2e 20 7c 20 4b 65 79 28 (1) | ... | Key(
1469d 4e 2d 31 29 20 7c 20 50 74 72 28 4e 29 20 7c 0a N-1) | Ptr(N) |.
1469e 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ** -----------
1469f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
146a0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
146a1 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
146a2 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 -----.**.** All
146a3 6f 66 20 74 68 65 20 6b 65 79 73 20 6f 6e 20 74 of the keys on t
146a4 68 65 20 70 61 67 65 20 74 68 61 74 20 50 74 72 he page that Ptr
146a5 28 30 29 20 70 6f 69 6e 74 73 20 74 6f 20 68 61 (0) points to ha
146a6 76 65 20 76 61 6c 75 65 73 20 6c 65 73 73 0a 2a ve values less.*
146a7 2a 20 74 68 61 6e 20 4b 65 79 28 30 29 2e 20 20 * than Key(0).
146a8 41 6c 6c 20 6f 66 20 74 68 65 20 6b 65 79 73 20 All of the keys
146a9 6f 6e 20 70 61 67 65 20 50 74 72 28 31 29 20 61 on page Ptr(1) a
146aa 6e 64 20 69 74 73 20 73 75 62 70 61 67 65 73 20 nd its subpages
146ab 68 61 76 65 0a 2a 2a 20 76 61 6c 75 65 73 20 67 have.** values g
146ac 72 65 61 74 65 72 20 74 68 61 6e 20 4b 65 79 28 reater than Key(
146ad 30 29 20 61 6e 64 20 6c 65 73 73 20 74 68 61 6e 0) and less than
146ae 20 4b 65 79 28 31 29 2e 20 20 41 6c 6c 20 6f 66 Key(1). All of
146af 20 74 68 65 20 6b 65 79 73 0a 2a 2a 20 6f 6e 20 the keys.** on
146b0 50 74 72 28 4e 29 20 61 6e 64 20 69 74 73 20 73 Ptr(N) and its s
146b1 75 62 70 61 67 65 73 20 68 61 76 65 20 76 61 6c ubpages have val
146b2 75 65 73 20 67 72 65 61 74 65 72 20 74 68 61 6e ues greater than
146b3 20 4b 65 79 28 4e 2d 31 29 2e 20 20 41 6e 64 0a Key(N-1). And.
146b4 2a 2a 20 73 6f 20 66 6f 72 74 68 2e 0a 2a 2a 0a ** so forth..**.
146b5 2a 2a 20 46 69 6e 64 69 6e 67 20 61 20 70 61 72 ** Finding a par
146b6 74 69 63 75 6c 61 72 20 6b 65 79 20 72 65 71 75 ticular key requ
146b7 69 72 65 73 20 72 65 61 64 69 6e 67 20 4f 28 6c ires reading O(l
146b8 6f 67 28 4d 29 29 20 70 61 67 65 73 20 66 72 6f og(M)) pages fro
146b9 6d 20 74 68 65 20 0a 2a 2a 20 64 69 73 6b 20 77 m the .** disk w
146ba 68 65 72 65 20 4d 20 69 73 20 74 68 65 20 6e 75 here M is the nu
146bb 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 mber of entries
146bc 69 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a 2a 0a in the tree..**.
146bd 2a 2a 20 49 6e 20 74 68 69 73 20 69 6d 70 6c 65 ** In this imple
146be 6d 65 6e 74 61 74 69 6f 6e 2c 20 61 20 73 69 6e mentation, a sin
146bf 67 6c 65 20 66 69 6c 65 20 63 61 6e 20 68 6f 6c gle file can hol
146c0 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 73 65 d one or more se
146c1 70 61 72 61 74 65 20 0a 2a 2a 20 42 54 72 65 65 parate .** BTree
146c2 73 2e 20 20 45 61 63 68 20 42 54 72 65 65 20 69 s. Each BTree i
146c3 73 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 s identified by
146c4 74 68 65 20 69 6e 64 65 78 20 6f 66 20 69 74 73 the index of its
146c5 20 72 6f 6f 74 20 70 61 67 65 2e 20 20 54 68 65 root page. The
146c6 0a 2a 2a 20 6b 65 79 20 61 6e 64 20 64 61 74 61 .** key and data
146c7 20 66 6f 72 20 61 6e 79 20 65 6e 74 72 79 20 61 for any entry a
146c8 72 65 20 63 6f 6d 62 69 6e 65 64 20 74 6f 20 66 re combined to f
146c9 6f 72 6d 20 74 68 65 20 22 70 61 79 6c 6f 61 64 orm the "payload
146ca 22 2e 20 20 41 0a 2a 2a 20 66 69 78 65 64 20 61 ". A.** fixed a
146cb 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 mount of payload
146cc 20 63 61 6e 20 62 65 20 63 61 72 72 69 65 64 20 can be carried
146cd 64 69 72 65 63 74 6c 79 20 6f 6e 20 74 68 65 20 directly on the
146ce 64 61 74 61 62 61 73 65 0a 2a 2a 20 70 61 67 65 database.** page
146cf 2e 20 20 49 66 20 74 68 65 20 70 61 79 6c 6f 61 . If the payloa
146d0 64 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e d is larger than
146d1 20 74 68 65 20 70 72 65 73 65 74 20 61 6d 6f 75 the preset amou
146d2 6e 74 20 74 68 65 6e 20 73 75 72 70 6c 75 73 0a nt then surplus.
146d3 2a 2a 20 62 79 74 65 73 20 61 72 65 20 73 74 6f ** bytes are sto
146d4 72 65 64 20 6f 6e 20 6f 76 65 72 66 6c 6f 77 20 red on overflow
146d5 70 61 67 65 73 2e 20 20 54 68 65 20 70 61 79 6c pages. The payl
146d6 6f 61 64 20 66 6f 72 20 61 6e 20 65 6e 74 72 79 oad for an entry
146d7 0a 2a 2a 20 61 6e 64 20 74 68 65 20 70 72 65 63 .** and the prec
146d8 65 64 69 6e 67 20 70 6f 69 6e 74 65 72 20 61 72 eding pointer ar
146d9 65 20 63 6f 6d 62 69 6e 65 64 20 74 6f 20 66 6f e combined to fo
146da 72 6d 20 61 20 22 43 65 6c 6c 22 2e 20 20 45 61 rm a "Cell". Ea
146db 63 68 20 0a 2a 2a 20 70 61 67 65 20 68 61 73 20 ch .** page has
146dc 61 20 73 6d 61 6c 6c 20 68 65 61 64 65 72 20 77 a small header w
146dd 68 69 63 68 20 63 6f 6e 74 61 69 6e 73 20 74 68 hich contains th
146de 65 20 50 74 72 28 4e 29 20 70 6f 69 6e 74 65 72 e Ptr(N) pointer
146df 20 61 6e 64 20 6f 74 68 65 72 0a 2a 2a 20 69 6e and other.** in
146e0 66 6f 72 6d 61 74 69 6f 6e 20 73 75 63 68 20 61 formation such a
146e1 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 6b 65 s the size of ke
146e2 79 20 61 6e 64 20 64 61 74 61 2e 0a 2a 2a 0a 2a y and data..**.*
146e3 2a 20 46 4f 52 4d 41 54 20 44 45 54 41 49 4c 53 * FORMAT DETAILS
146e4 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 .**.** The file
146e5 69 73 20 64 69 76 69 64 65 64 20 69 6e 74 6f 20 is divided into
146e6 70 61 67 65 73 2e 20 20 54 68 65 20 66 69 72 73 pages. The firs
146e7 74 20 70 61 67 65 20 69 73 20 63 61 6c 6c 65 64 t page is called
146e8 20 70 61 67 65 20 31 2c 0a 2a 2a 20 74 68 65 20 page 1,.** the
146e9 73 65 63 6f 6e 64 20 69 73 20 70 61 67 65 20 32 second is page 2
146ea 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 , and so forth.
146eb 20 41 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f A page number o
146ec 66 20 7a 65 72 6f 20 69 6e 64 69 63 61 74 65 73 f zero indicates
146ed 0a 2a 2a 20 22 6e 6f 20 73 75 63 68 20 70 61 67 .** "no such pag
146ee 65 22 2e 20 20 54 68 65 20 70 61 67 65 20 73 69 e". The page si
146ef 7a 65 20 63 61 6e 20 62 65 20 61 6e 79 20 70 6f ze can be any po
146f0 77 65 72 20 6f 66 20 32 20 62 65 74 77 65 65 6e wer of 2 between
146f1 20 35 31 32 20 61 6e 64 20 33 32 37 36 38 2e 0a 512 and 32768..
146f2 2a 2a 20 45 61 63 68 20 70 61 67 65 20 63 61 6e ** Each page can
146f3 20 62 65 20 65 69 74 68 65 72 20 61 20 62 74 72 be either a btr
146f4 65 65 20 70 61 67 65 2c 20 61 20 66 72 65 65 6c ee page, a freel
146f5 69 73 74 20 70 61 67 65 2c 20 61 6e 20 6f 76 65 ist page, an ove
146f6 72 66 6c 6f 77 0a 2a 2a 20 70 61 67 65 2c 20 6f rflow.** page, o
146f7 72 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 r a pointer-map
146f8 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 page..**.** The
146f9 66 69 72 73 74 20 70 61 67 65 20 69 73 20 61 6c first page is al
146fa 77 61 79 73 20 61 20 62 74 72 65 65 20 70 61 67 ways a btree pag
146fb 65 2e 20 20 54 68 65 20 66 69 72 73 74 20 31 30 e. The first 10
146fc 30 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 66 0 bytes of the f
146fd 69 72 73 74 0a 2a 2a 20 70 61 67 65 20 63 6f 6e irst.** page con
146fe 74 61 69 6e 20 61 20 73 70 65 63 69 61 6c 20 68 tain a special h
146ff 65 61 64 65 72 20 28 74 68 65 20 22 66 69 6c 65 eader (the "file
14700 20 68 65 61 64 65 72 22 29 20 74 68 61 74 20 64 header") that d
14701 65 73 63 72 69 62 65 73 20 74 68 65 20 66 69 6c escribes the fil
14702 65 2e 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 61 74 e..** The format
14703 20 6f 66 20 74 68 65 20 66 69 6c 65 20 68 65 61 of the file hea
14704 64 65 72 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 der is as follow
14705 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 46 46 53 45 s:.**.** OFFSE
14706 54 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 T SIZE DESC
14707 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 RIPTION.**
14708 30 20 20 20 20 20 20 31 36 20 20 20 20 20 48 65 0 16 He
14709 61 64 65 72 20 73 74 72 69 6e 67 3a 20 22 53 51 ader string: "SQ
1470a 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 5c 30 30 Lite format 3\00
1470b 30 22 0a 2a 2a 20 20 20 20 20 31 36 20 20 20 20 0".** 16
1470c 20 20 20 32 20 20 20 20 20 50 61 67 65 20 73 69 2 Page si
1470d 7a 65 20 69 6e 20 62 79 74 65 73 2e 20 20 0a 2a ze in bytes. .*
1470e 2a 20 20 20 20 20 31 38 20 20 20 20 20 20 20 31 * 18 1
1470f 20 20 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 74 File format
14710 20 77 72 69 74 65 20 76 65 72 73 69 6f 6e 0a 2a write version.*
14711 2a 20 20 20 20 20 31 39 20 20 20 20 20 20 20 31 * 19 1
14712 20 20 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 74 File format
14713 20 72 65 61 64 20 76 65 72 73 69 6f 6e 0a 2a 2a read version.**
14714 20 20 20 20 20 32 30 20 20 20 20 20 20 20 31 20 20 1
14715 20 20 20 20 42 79 74 65 73 20 6f 66 20 75 6e 75 Bytes of unu
14716 73 65 64 20 73 70 61 63 65 20 61 74 20 74 68 65 sed space at the
14717 20 65 6e 64 20 6f 66 20 65 61 63 68 20 70 61 67 end of each pag
14718 65 0a 2a 2a 20 20 20 20 20 32 31 20 20 20 20 20 e.** 21
14719 20 20 31 20 20 20 20 20 4d 61 78 20 65 6d 62 65 1 Max embe
1471a 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 dded payload fra
1471b 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 32 32 20 ction.** 22
1471c 20 20 20 20 20 20 31 20 20 20 20 20 4d 69 6e 20 1 Min
1471d 65 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 embedded payload
1471e 20 66 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 fraction.**
1471f 20 32 33 20 20 20 20 20 20 20 31 20 20 20 20 20 23 1
14720 4d 69 6e 20 6c 65 61 66 20 70 61 79 6c 6f 61 64 Min leaf payload
14721 20 66 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 fraction.**
14722 20 32 34 20 20 20 20 20 20 20 34 20 20 20 20 20 24 4
14723 46 69 6c 65 20 63 68 61 6e 67 65 20 63 6f 75 6e File change coun
14724 74 65 72 0a 2a 2a 20 20 20 20 20 32 38 20 20 20 ter.** 28
14725 20 20 20 20 34 20 20 20 20 20 52 65 73 65 72 76 4 Reserv
14726 65 64 20 66 6f 72 20 66 75 74 75 72 65 20 75 73 ed for future us
14727 65 0a 2a 2a 20 20 20 20 20 33 32 20 20 20 20 20 e.** 32
14728 20 20 34 20 20 20 20 20 46 69 72 73 74 20 66 72 4 First fr
14729 65 65 6c 69 73 74 20 70 61 67 65 0a 2a 2a 20 20 eelist page.**
1472a 20 20 20 33 36 20 20 20 20 20 20 20 34 20 20 20 36 4
1472b 20 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 Number of free
1472c 6c 69 73 74 20 70 61 67 65 73 20 69 6e 20 74 68 list pages in th
1472d 65 20 66 69 6c 65 0a 2a 2a 20 20 20 20 20 34 30 e file.** 40
1472e 20 20 20 20 20 20 36 30 20 20 20 20 20 31 35 20 60 15
1472f 34 2d 62 79 74 65 20 6d 65 74 61 20 76 61 6c 75 4-byte meta valu
14730 65 73 20 70 61 73 73 65 64 20 74 6f 20 68 69 67 es passed to hig
14731 68 65 72 20 6c 61 79 65 72 73 0a 2a 2a 0a 2a 2a her layers.**.**
14732 20 20 20 20 20 34 30 20 20 20 20 20 20 20 34 20 40 4
14733 20 20 20 20 53 63 68 65 6d 61 20 63 6f 6f 6b 69 Schema cooki
14734 65 0a 2a 2a 20 20 20 20 20 34 34 20 20 20 20 20 e.** 44
14735 20 20 34 20 20 20 20 20 46 69 6c 65 20 66 6f 72 4 File for
14736 6d 61 74 20 6f 66 20 73 63 68 65 6d 61 20 6c 61 mat of schema la
14737 79 65 72 0a 2a 2a 20 20 20 20 20 34 38 20 20 20 yer.** 48
14738 20 20 20 20 34 20 20 20 20 20 53 69 7a 65 20 6f 4 Size o
14739 66 20 70 61 67 65 20 63 61 63 68 65 0a 2a 2a 20 f page cache.**
1473a 20 20 20 20 35 32 20 20 20 20 20 20 20 34 20 20 52 4
1473b 20 20 20 4c 61 72 67 65 73 74 20 72 6f 6f 74 2d Largest root-
1473c 70 61 67 65 20 28 61 75 74 6f 2f 69 6e 63 72 5f page (auto/incr_
1473d 76 61 63 75 75 6d 29 0a 2a 2a 20 20 20 20 20 35 vacuum).** 5
1473e 36 20 20 20 20 20 20 20 34 20 20 20 20 20 31 3d 6 4 1=
1473f 55 54 46 2d 38 20 32 3d 55 54 46 31 36 6c 65 20 UTF-8 2=UTF16le
14740 33 3d 55 54 46 31 36 62 65 0a 2a 2a 20 20 20 20 3=UTF16be.**
14741 20 36 30 20 20 20 20 20 20 20 34 20 20 20 20 20 60 4
14742 55 73 65 72 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 User version.**
14743 20 20 20 20 36 34 20 20 20 20 20 20 20 34 20 20 64 4
14744 20 20 20 49 6e 63 72 65 6d 65 6e 74 61 6c 20 76 Incremental v
14745 61 63 75 75 6d 20 6d 6f 64 65 0a 2a 2a 20 20 20 acuum mode.**
14746 20 20 36 38 20 20 20 20 20 20 20 34 20 20 20 20 68 4
14747 20 75 6e 75 73 65 64 0a 2a 2a 20 20 20 20 20 37 unused.** 7
14748 32 20 20 20 20 20 20 20 34 20 20 20 20 20 75 6e 2 4 un
14749 75 73 65 64 0a 2a 2a 20 20 20 20 20 37 36 20 20 used.** 76
1474a 20 20 20 20 20 34 20 20 20 20 20 75 6e 75 73 65 4 unuse
1474b 64 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 d.**.** All of t
1474c 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 he integer value
1474d 73 20 61 72 65 20 62 69 67 2d 65 6e 64 69 61 6e s are big-endian
1474e 20 28 6d 6f 73 74 20 73 69 67 6e 69 66 69 63 61 (most significa
1474f 6e 74 20 62 79 74 65 20 66 69 72 73 74 29 2e 0a nt byte first)..
14750 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 63 **.** The file c
14751 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 69 73 hange counter is
14752 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 68 65 incremented whe
14753 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 n the database i
14754 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 54 68 69 s changed.** Thi
14755 73 20 63 6f 75 6e 74 65 72 20 61 6c 6c 6f 77 73 s counter allows
14756 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 other processes
14757 20 74 6f 20 6b 6e 6f 77 20 77 68 65 6e 20 74 68 to know when th
14758 65 20 66 69 6c 65 20 68 61 73 20 63 68 61 6e 67 e file has chang
14759 65 64 0a 2a 2a 20 61 6e 64 20 74 68 75 73 20 77 ed.** and thus w
1475a 68 65 6e 20 74 68 65 79 20 6e 65 65 64 20 74 6f hen they need to
1475b 20 66 6c 75 73 68 20 74 68 65 69 72 20 63 61 63 flush their cac
1475c 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 he..**.** The ma
1475d 78 20 65 6d 62 65 64 64 65 64 20 70 61 79 6c 6f x embedded paylo
1475e 61 64 20 66 72 61 63 74 69 6f 6e 20 69 73 20 74 ad fraction is t
1475f 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 68 65 he amount of the
14760 20 74 6f 74 61 6c 20 75 73 61 62 6c 65 0a 2a 2a total usable.**
14761 20 73 70 61 63 65 20 69 6e 20 61 20 70 61 67 65 space in a page
14762 20 74 68 61 74 20 63 61 6e 20 62 65 20 63 6f 6e that can be con
14763 73 75 6d 65 64 20 62 79 20 61 20 73 69 6e 67 6c sumed by a singl
14764 65 20 63 65 6c 6c 20 66 6f 72 20 73 74 61 6e 64 e cell for stand
14765 61 72 64 0a 2a 2a 20 42 2d 74 72 65 65 20 28 6e ard.** B-tree (n
14766 6f 6e 2d 4c 45 41 46 44 41 54 41 29 20 74 61 62 on-LEAFDATA) tab
14767 6c 65 73 2e 20 20 41 20 76 61 6c 75 65 20 6f 66 les. A value of
14768 20 32 35 35 20 6d 65 61 6e 73 20 31 30 30 25 2e 255 means 100%.
14769 20 20 54 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a The default.**
1476a 20 69 73 20 74 6f 20 6c 69 6d 69 74 20 74 68 65 is to limit the
1476b 20 6d 61 78 69 6d 75 6d 20 63 65 6c 6c 20 73 69 maximum cell si
1476c 7a 65 20 73 6f 20 74 68 61 74 20 61 74 20 6c 65 ze so that at le
1476d 61 73 74 20 34 20 63 65 6c 6c 73 20 77 69 6c 6c ast 4 cells will
1476e 20 66 69 74 0a 2a 2a 20 6f 6e 20 6f 6e 65 20 70 fit.** on one p
1476f 61 67 65 2e 20 20 54 68 75 73 20 74 68 65 20 64 age. Thus the d
14770 65 66 61 75 6c 74 20 6d 61 78 20 65 6d 62 65 64 efault max embed
14771 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 ded payload frac
14772 74 69 6f 6e 20 69 73 20 36 34 2e 0a 2a 2a 0a 2a tion is 64..**.*
14773 2a 20 49 66 20 74 68 65 20 70 61 79 6c 6f 61 64 * If the payload
14774 20 66 6f 72 20 61 20 63 65 6c 6c 20 69 73 20 6c for a cell is l
14775 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 6d arger than the m
14776 61 78 20 70 61 79 6c 6f 61 64 2c 20 74 68 65 6e ax payload, then
14777 20 65 78 74 72 61 0a 2a 2a 20 70 61 79 6c 6f 61 extra.** payloa
14778 64 20 69 73 20 73 70 69 6c 6c 65 64 20 74 6f 20 d is spilled to
14779 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 overflow pages.
1477a 20 4f 6e 63 65 20 61 6e 20 6f 76 65 72 66 6c 6f Once an overflo
1477b 77 20 70 61 67 65 20 69 73 20 61 6c 6c 6f 63 61 w page is alloca
1477c 74 65 64 2c 0a 2a 2a 20 61 73 20 6d 61 6e 79 20 ted,.** as many
1477d 62 79 74 65 73 20 61 73 20 70 6f 73 73 69 62 6c bytes as possibl
1477e 65 20 61 72 65 20 6d 6f 76 65 64 20 69 6e 74 6f e are moved into
1477f 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 the overflow pa
14780 67 65 73 20 77 69 74 68 6f 75 74 20 6c 65 74 74 ges without lett
14781 69 6e 67 0a 2a 2a 20 74 68 65 20 63 65 6c 6c 20 ing.** the cell
14782 73 69 7a 65 20 64 72 6f 70 20 62 65 6c 6f 77 20 size drop below
14783 74 68 65 20 6d 69 6e 20 65 6d 62 65 64 64 65 64 the min embedded
14784 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f payload fractio
14785 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 69 6e n..**.** The min
14786 20 6c 65 61 66 20 70 61 79 6c 6f 61 64 20 66 72 leaf payload fr
14787 61 63 74 69 6f 6e 20 69 73 20 6c 69 6b 65 20 74 action is like t
14788 68 65 20 6d 69 6e 20 65 6d 62 65 64 64 65 64 20 he min embedded
14789 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e payload fraction
1478a 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 61 74 20 .** except that
1478b 69 74 20 61 70 70 6c 69 65 73 20 74 6f 20 6c 65 it applies to le
1478c 61 66 20 6e 6f 64 65 73 20 69 6e 20 61 20 4c 45 af nodes in a LE
1478d 41 46 44 41 54 41 20 74 72 65 65 2e 20 20 54 68 AFDATA tree. Th
1478e 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 70 61 79 e maximum.** pay
1478f 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 66 6f load fraction fo
14790 72 20 61 20 4c 45 41 46 44 41 54 41 20 74 72 65 r a LEAFDATA tre
14791 65 20 69 73 20 61 6c 77 61 79 73 20 31 30 30 25 e is always 100%
14792 20 28 6f 72 20 32 35 35 29 20 61 6e 64 20 69 74 (or 255) and it
14793 0a 2a 2a 20 6e 6f 74 20 73 70 65 63 69 66 69 65 .** not specifie
14794 64 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 2e d in the header.
14795 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 62 74 72 65 .**.** Each btre
14796 65 20 70 61 67 65 73 20 69 73 20 64 69 76 69 64 e pages is divid
14797 65 64 20 69 6e 74 6f 20 74 68 72 65 65 20 73 65 ed into three se
14798 63 74 69 6f 6e 73 3a 20 20 54 68 65 20 68 65 61 ctions: The hea
14799 64 65 72 2c 20 74 68 65 0a 2a 2a 20 63 65 6c 6c der, the.** cell
1479a 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 2c 20 pointer array,
1479b 61 6e 64 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e and the cell con
1479c 74 65 6e 74 20 61 72 65 61 2e 20 20 50 61 67 65 tent area. Page
1479d 20 31 20 61 6c 73 6f 20 68 61 73 20 61 20 31 30 1 also has a 10
1479e 30 2d 62 79 74 65 0a 2a 2a 20 66 69 6c 65 20 68 0-byte.** file h
1479f 65 61 64 65 72 20 74 68 61 74 20 6f 63 63 75 72 eader that occur
147a0 73 20 62 65 66 6f 72 65 20 74 68 65 20 70 61 67 s before the pag
147a1 65 20 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 e header..**.**
147a2 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |----------
147a3 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 ------|.**
147a4 7c 20 66 69 6c 65 20 68 65 61 64 65 72 20 20 20 | file header
147a5 20 7c 20 20 20 31 30 30 20 62 79 74 65 73 2e 20 | 100 bytes.
147a6 20 50 61 67 65 20 31 20 6f 6e 6c 79 2e 0a 2a 2a Page 1 only..**
147a7 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d |---------
147a8 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 20 -------|.**
147a9 20 7c 20 70 61 67 65 20 68 65 61 64 65 72 20 20 | page header
147aa 20 20 7c 20 20 20 38 20 62 79 74 65 73 20 66 6f | 8 bytes fo
147ab 72 20 6c 65 61 76 65 73 2e 20 20 31 32 20 62 79 r leaves. 12 by
147ac 74 65 73 20 66 6f 72 20 69 6e 74 65 72 69 6f 72 tes for interior
147ad 20 6e 6f 64 65 73 0a 2a 2a 20 20 20 20 20 20 7c nodes.** |
147ae 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
147af 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 63 65 6c 6c |.** | cell
147b0 20 70 6f 69 6e 74 65 72 20 20 20 7c 20 20 20 7c pointer | |
147b1 20 20 32 20 62 79 74 65 73 20 70 65 72 20 63 65 2 bytes per ce
147b2 6c 6c 2e 20 20 53 6f 72 74 65 64 20 6f 72 64 65 ll. Sorted orde
147b3 72 2e 0a 2a 2a 20 20 20 20 20 20 7c 20 61 72 72 r..** | arr
147b4 61 79 20 20 20 20 20 20 20 20 20 20 7c 20 20 20 ay |
147b5 7c 20 20 47 72 6f 77 73 20 64 6f 77 6e 77 61 72 | Grows downwar
147b6 64 0a 2a 2a 20 20 20 20 20 20 7c 20 20 20 20 20 d.** |
147b7 20 20 20 20 20 20 20 20 20 20 20 7c 20 20 20 76 | v
147b8 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d .** |------
147b9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 ----------|.**
147ba 20 20 20 20 7c 20 75 6e 61 6c 6c 6f 63 61 74 65 | unallocate
147bb 64 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20 20 7c d |.** |
147bc 20 73 70 61 63 65 20 20 20 20 20 20 20 20 20 20 space
147bd 7c 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d |.** |-----
147be 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 20 20 20 5e -----------| ^
147bf 20 20 47 72 6f 77 73 20 75 70 77 61 72 64 73 0a Grows upwards.
147c0 2a 2a 20 20 20 20 20 20 7c 20 63 65 6c 6c 20 63 ** | cell c
147c1 6f 6e 74 65 6e 74 20 20 20 7c 20 20 20 7c 20 20 ontent | |
147c2 41 72 62 69 74 72 61 72 79 20 6f 72 64 65 72 20 Arbitrary order
147c3 69 6e 74 65 72 73 70 65 72 73 65 64 20 77 69 74 interspersed wit
147c4 68 20 66 72 65 65 62 6c 6f 63 6b 73 2e 0a 2a 2a h freeblocks..**
147c5 20 20 20 20 20 20 7c 20 61 72 65 61 20 20 20 20 | area
147c6 20 20 20 20 20 20 20 7c 20 20 20 7c 20 20 61 6e | | an
147c7 64 20 66 72 65 65 20 73 70 61 63 65 20 66 72 61 d free space fra
147c8 67 6d 65 6e 74 73 2e 0a 2a 2a 20 20 20 20 20 20 gments..**
147c9 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |---------------
147ca 2d 7c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 -|.**.** The pag
147cb 65 20 68 65 61 64 65 72 73 20 6c 6f 6f 6b 73 20 e headers looks
147cc 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a like this:.**.**
147cd 20 20 20 4f 46 46 53 45 54 20 20 20 53 49 5a 45 OFFSET SIZE
147ce 20 20 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e DESCRIPTION
147cf 0a 2a 2a 20 20 20 20 20 20 30 20 20 20 20 20 20 .** 0
147d0 20 31 20 20 20 20 20 20 46 6c 61 67 73 2e 20 31 1 Flags. 1
147d1 3a 20 69 6e 74 6b 65 79 2c 20 32 3a 20 7a 65 72 : intkey, 2: zer
147d2 6f 64 61 74 61 2c 20 34 3a 20 6c 65 61 66 64 61 odata, 4: leafda
147d3 74 61 2c 20 38 3a 20 6c 65 61 66 0a 2a 2a 20 20 ta, 8: leaf.**
147d4 20 20 20 20 31 20 20 20 20 20 20 20 32 20 20 20 1 2
147d5 20 20 20 62 79 74 65 20 6f 66 66 73 65 74 20 74 byte offset t
147d6 6f 20 74 68 65 20 66 69 72 73 74 20 66 72 65 65 o the first free
147d7 62 6c 6f 63 6b 0a 2a 2a 20 20 20 20 20 20 33 20 block.** 3
147d8 20 20 20 20 20 20 32 20 20 20 20 20 20 6e 75 6d 2 num
147d9 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 ber of cells on
147da 74 68 69 73 20 70 61 67 65 0a 2a 2a 20 20 20 20 this page.**
147db 20 20 35 20 20 20 20 20 20 20 32 20 20 20 20 20 5 2
147dc 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 first byte of t
147dd 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 he cell content
147de 61 72 65 61 0a 2a 2a 20 20 20 20 20 20 37 20 20 area.** 7
147df 20 20 20 20 20 31 20 20 20 20 20 20 6e 75 6d 62 1 numb
147e0 65 72 20 6f 66 20 66 72 61 67 6d 65 6e 74 65 64 er of fragmented
147e1 20 66 72 65 65 20 62 79 74 65 73 0a 2a 2a 20 20 free bytes.**
147e2 20 20 20 20 38 20 20 20 20 20 20 20 34 20 20 20 8 4
147e3 20 20 20 52 69 67 68 74 20 63 68 69 6c 64 20 28 Right child (
147e4 74 68 65 20 50 74 72 28 4e 29 20 76 61 6c 75 65 the Ptr(N) value
147e5 29 2e 20 20 4f 6d 69 74 74 65 64 20 6f 6e 20 6c ). Omitted on l
147e6 65 61 76 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 eaves..**.** The
147e7 20 66 6c 61 67 73 20 64 65 66 69 6e 65 20 74 68 flags define th
147e8 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 68 69 73 e format of this
147e9 20 62 74 72 65 65 20 70 61 67 65 2e 20 20 54 68 btree page. Th
147ea 65 20 6c 65 61 66 20 66 6c 61 67 20 6d 65 61 6e e leaf flag mean
147eb 73 20 74 68 61 74 0a 2a 2a 20 74 68 69 73 20 70 s that.** this p
147ec 61 67 65 20 68 61 73 20 6e 6f 20 63 68 69 6c 64 age has no child
147ed 72 65 6e 2e 20 20 54 68 65 20 7a 65 72 6f 64 61 ren. The zeroda
147ee 74 61 20 66 6c 61 67 20 6d 65 61 6e 73 20 74 68 ta flag means th
147ef 61 74 20 74 68 69 73 20 70 61 67 65 20 63 61 72 at this page car
147f0 72 69 65 73 0a 2a 2a 20 6f 6e 6c 79 20 6b 65 79 ries.** only key
147f1 73 20 61 6e 64 20 6e 6f 20 64 61 74 61 2e 20 20 s and no data.
147f2 54 68 65 20 69 6e 74 6b 65 79 20 66 6c 61 67 20 The intkey flag
147f3 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 6b means that the k
147f4 65 79 20 69 73 20 61 20 69 6e 74 65 67 65 72 0a ey is a integer.
147f5 2a 2a 20 77 68 69 63 68 20 69 73 20 73 74 6f 72 ** which is stor
147f6 65 64 20 69 6e 20 74 68 65 20 6b 65 79 20 73 69 ed in the key si
147f7 7a 65 20 65 6e 74 72 79 20 6f 66 20 74 68 65 20 ze entry of the
147f8 63 65 6c 6c 20 68 65 61 64 65 72 20 72 61 74 68 cell header rath
147f9 65 72 20 74 68 61 6e 20 69 6e 0a 2a 2a 20 74 68 er than in.** th
147fa 65 20 70 61 79 6c 6f 61 64 20 61 72 65 61 2e 0a e payload area..
147fb 2a 2a 0a 2a 2a 20 54 68 65 20 63 65 6c 6c 20 70 **.** The cell p
147fc 6f 69 6e 74 65 72 20 61 72 72 61 79 20 62 65 67 ointer array beg
147fd 69 6e 73 20 6f 6e 20 74 68 65 20 66 69 72 73 74 ins on the first
147fe 20 62 79 74 65 20 61 66 74 65 72 20 74 68 65 20 byte after the
147ff 70 61 67 65 20 68 65 61 64 65 72 2e 0a 2a 2a 20 page header..**
14800 54 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 The cell pointer
14801 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e 73 20 array contains
14802 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 32 2d 62 zero or more 2-b
14803 79 74 65 20 6e 75 6d 62 65 72 73 20 77 68 69 63 yte numbers whic
14804 68 20 61 72 65 0a 2a 2a 20 6f 66 66 73 65 74 73 h are.** offsets
14805 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e from the beginn
14806 69 6e 67 20 6f 66 20 74 68 65 20 70 61 67 65 20 ing of the page
14807 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 to the cell cont
14808 65 6e 74 20 69 6e 20 74 68 65 20 63 65 6c 6c 0a ent in the cell.
14809 2a 2a 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e ** content area.
1480a 20 20 54 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 The cell point
1480b 65 72 73 20 6f 63 63 75 72 20 69 6e 20 73 6f 72 ers occur in sor
1480c 74 65 64 20 6f 72 64 65 72 2e 20 20 54 68 65 20 ted order. The
1480d 73 79 73 74 65 6d 20 73 74 72 69 76 65 73 0a 2a system strives.*
1480e 2a 20 74 6f 20 6b 65 65 70 20 66 72 65 65 20 73 * to keep free s
1480f 70 61 63 65 20 61 66 74 65 72 20 74 68 65 20 6c pace after the l
14810 61 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 ast cell pointer
14811 20 73 6f 20 74 68 61 74 20 6e 65 77 20 63 65 6c so that new cel
14812 6c 73 20 63 61 6e 0a 2a 2a 20 62 65 20 65 61 73 ls can.** be eas
14813 69 6c 79 20 61 64 64 65 64 20 77 69 74 68 6f 75 ily added withou
14814 74 20 68 61 76 69 6e 67 20 74 6f 20 64 65 66 72 t having to defr
14815 61 67 6d 65 6e 74 20 74 68 65 20 70 61 67 65 2e agment the page.
14816 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 20 63 6f 6e 74 .**.** Cell cont
14817 65 6e 74 20 69 73 20 73 74 6f 72 65 64 20 61 74 ent is stored at
14818 20 74 68 65 20 76 65 72 79 20 65 6e 64 20 6f 66 the very end of
14819 20 74 68 65 20 70 61 67 65 20 61 6e 64 20 67 72 the page and gr
1481a 6f 77 73 20 74 6f 77 61 72 64 20 74 68 65 0a 2a ows toward the.*
1481b 2a 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 * beginning of t
1481c 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 55 he page..**.** U
1481d 6e 75 73 65 64 20 73 70 61 63 65 20 77 69 74 68 nused space with
1481e 69 6e 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 in the cell cont
1481f 65 6e 74 20 61 72 65 61 20 69 73 20 63 6f 6c 6c ent area is coll
14820 65 63 74 65 64 20 69 6e 74 6f 20 61 20 6c 69 6e ected into a lin
14821 6b 65 64 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 66 ked list of.** f
14822 72 65 65 62 6c 6f 63 6b 73 2e 20 20 45 61 63 68 reeblocks. Each
14823 20 66 72 65 65 62 6c 6f 63 6b 20 69 73 20 61 74 freeblock is at
14824 20 6c 65 61 73 74 20 34 20 62 79 74 65 73 20 69 least 4 bytes i
14825 6e 20 73 69 7a 65 2e 20 20 54 68 65 20 62 79 74 n size. The byt
14826 65 20 6f 66 66 73 65 74 0a 2a 2a 20 74 6f 20 74 e offset.** to t
14827 68 65 20 66 69 72 73 74 20 66 72 65 65 62 6c 6f he first freeblo
14828 63 6b 20 69 73 20 67 69 76 65 6e 20 69 6e 20 74 ck is given in t
14829 68 65 20 68 65 61 64 65 72 2e 20 20 46 72 65 65 he header. Free
1482a 62 6c 6f 63 6b 73 20 6f 63 63 75 72 20 69 6e 0a blocks occur in.
1482b 2a 2a 20 69 6e 63 72 65 61 73 69 6e 67 20 6f 72 ** increasing or
1482c 64 65 72 2e 20 20 42 65 63 61 75 73 65 20 61 20 der. Because a
1482d 66 72 65 65 62 6c 6f 63 6b 20 6d 75 73 74 20 62 freeblock must b
1482e 65 20 61 74 20 6c 65 61 73 74 20 34 20 62 79 74 e at least 4 byt
1482f 65 73 20 69 6e 20 73 69 7a 65 2c 0a 2a 2a 20 61 es in size,.** a
14830 6e 79 20 67 72 6f 75 70 20 6f 66 20 33 20 6f 72 ny group of 3 or
14831 20 66 65 77 65 72 20 75 6e 75 73 65 64 20 62 79 fewer unused by
14832 74 65 73 20 69 6e 20 74 68 65 20 63 65 6c 6c 20 tes in the cell
14833 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 63 61 6e content area can
14834 6e 6f 74 0a 2a 2a 20 65 78 69 73 74 20 6f 6e 20 not.** exist on
14835 74 68 65 20 66 72 65 65 62 6c 6f 63 6b 20 63 68 the freeblock ch
14836 61 69 6e 2e 20 20 41 20 67 72 6f 75 70 20 6f 66 ain. A group of
14837 20 33 20 6f 72 20 66 65 77 65 72 20 66 72 65 65 3 or fewer free
14838 20 62 79 74 65 73 20 69 73 20 63 61 6c 6c 65 64 bytes is called
14839 0a 2a 2a 20 61 20 66 72 61 67 6d 65 6e 74 2e 20 .** a fragment.
1483a 20 54 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 The total numbe
1483b 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 61 6c r of bytes in al
1483c 6c 20 66 72 61 67 6d 65 6e 74 73 20 69 73 20 72 l fragments is r
1483d 65 63 6f 72 64 65 64 2e 0a 2a 2a 20 69 6e 20 74 ecorded..** in t
1483e 68 65 20 70 61 67 65 20 68 65 61 64 65 72 20 61 he page header a
1483f 74 20 6f 66 66 73 65 74 20 37 2e 0a 2a 2a 0a 2a t offset 7..**.*
14840 2a 20 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 * SIZE DES
14841 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 CRIPTION.**
14842 20 32 20 20 20 20 20 42 79 74 65 20 6f 66 66 73 2 Byte offs
14843 65 74 20 6f 66 20 74 68 65 20 6e 65 78 74 20 66 et of the next f
14844 72 65 65 62 6c 6f 63 6b 0a 2a 2a 20 20 20 20 20 reeblock.**
14845 20 32 20 20 20 20 20 42 79 74 65 73 20 69 6e 20 2 Bytes in
14846 74 68 69 73 20 66 72 65 65 62 6c 6f 63 6b 0a 2a this freeblock.*
14847 2a 0a 2a 2a 20 43 65 6c 6c 73 20 61 72 65 20 6f *.** Cells are o
14848 66 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 f variable lengt
14849 68 2e 20 20 43 65 6c 6c 73 20 61 72 65 20 73 74 h. Cells are st
1484a 6f 72 65 64 20 69 6e 20 74 68 65 20 63 65 6c 6c ored in the cell
1484b 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 61 74 content area at
1484c 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66 20 74 .** the end of t
1484d 68 65 20 70 61 67 65 2e 20 20 50 6f 69 6e 74 65 he page. Pointe
1484e 72 73 20 74 6f 20 74 68 65 20 63 65 6c 6c 73 20 rs to the cells
1484f 61 72 65 20 69 6e 20 74 68 65 20 63 65 6c 6c 20 are in the cell
14850 70 6f 69 6e 74 65 72 20 61 72 72 61 79 0a 2a 2a pointer array.**
14851 20 74 68 61 74 20 69 6d 6d 65 64 69 61 74 65 6c that immediatel
14852 79 20 66 6f 6c 6c 6f 77 73 20 74 68 65 20 70 61 y follows the pa
14853 67 65 20 68 65 61 64 65 72 2e 20 20 43 65 6c 6c ge header. Cell
14854 73 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 s is not necessa
14855 72 69 6c 79 0a 2a 2a 20 63 6f 6e 74 69 67 75 6f rily.** contiguo
14856 75 73 20 6f 72 20 69 6e 20 6f 72 64 65 72 2c 20 us or in order,
14857 62 75 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 but cell pointer
14858 73 20 61 72 65 20 63 6f 6e 74 69 67 75 6f 75 73 s are contiguous
14859 20 61 6e 64 20 69 6e 20 6f 72 64 65 72 2e 0a 2a and in order..*
1485a 2a 0a 2a 2a 20 43 65 6c 6c 20 63 6f 6e 74 65 6e *.** Cell conten
1485b 74 20 6d 61 6b 65 73 20 75 73 65 20 6f 66 20 76 t makes use of v
1485c 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 69 ariable length i
1485d 6e 74 65 67 65 72 73 2e 20 20 41 20 76 61 72 69 ntegers. A vari
1485e 61 62 6c 65 0a 2a 2a 20 6c 65 6e 67 74 68 20 69 able.** length i
1485f 6e 74 65 67 65 72 20 69 73 20 31 20 74 6f 20 39 nteger is 1 to 9
14860 20 62 79 74 65 73 20 77 68 65 72 65 20 74 68 65 bytes where the
14861 20 6c 6f 77 65 72 20 37 20 62 69 74 73 20 6f 66 lower 7 bits of
14862 20 65 61 63 68 20 0a 2a 2a 20 62 79 74 65 20 61 each .** byte a
14863 72 65 20 75 73 65 64 2e 20 20 54 68 65 20 69 6e re used. The in
14864 74 65 67 65 72 20 63 6f 6e 73 69 73 74 73 20 6f teger consists o
14865 66 20 61 6c 6c 20 62 79 74 65 73 20 74 68 61 74 f all bytes that
14866 20 68 61 76 65 20 62 69 74 20 38 20 73 65 74 20 have bit 8 set
14867 61 6e 64 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 and.** the first
14868 20 62 79 74 65 20 77 69 74 68 20 62 69 74 20 38 byte with bit 8
14869 20 63 6c 65 61 72 2e 20 20 54 68 65 20 6d 6f 73 clear. The mos
1486a 74 20 73 69 67 6e 69 66 69 63 61 6e 74 20 62 79 t significant by
1486b 74 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 te of the intege
1486c 72 0a 2a 2a 20 61 70 70 65 61 72 73 20 66 69 72 r.** appears fir
1486d 73 74 2e 20 20 41 20 76 61 72 69 61 62 6c 65 2d st. A variable-
1486e 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 6d length integer m
1486f 61 79 20 6e 6f 74 20 62 65 20 6d 6f 72 65 20 74 ay not be more t
14870 68 61 6e 20 39 20 62 79 74 65 73 20 6c 6f 6e 67 han 9 bytes long
14871 2e 0a 2a 2a 20 41 73 20 61 20 73 70 65 63 69 61 ..** As a specia
14872 6c 20 63 61 73 65 2c 20 61 6c 6c 20 38 20 62 79 l case, all 8 by
14873 74 65 73 20 6f 66 20 74 68 65 20 39 74 68 20 62 tes of the 9th b
14874 79 74 65 20 61 72 65 20 75 73 65 64 20 61 73 20 yte are used as
14875 64 61 74 61 2e 20 20 54 68 69 73 0a 2a 2a 20 61 data. This.** a
14876 6c 6c 6f 77 73 20 61 20 36 34 2d 62 69 74 20 69 llows a 64-bit i
14877 6e 74 65 67 65 72 20 74 6f 20 62 65 20 65 6e 63 nteger to be enc
14878 6f 64 65 64 20 69 6e 20 39 20 62 79 74 65 73 2e oded in 9 bytes.
14879 0a 2a 2a 0a 2a 2a 20 20 20 20 30 78 30 30 20 20 .**.** 0x00
1487a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1487b 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30 becomes 0x0
1487c 30 30 30 30 30 30 30 0a 2a 2a 20 20 20 20 30 78 0000000.** 0x
1487d 37 66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7f
1487e 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 becomes
1487f 20 30 78 30 30 30 30 30 30 37 66 0a 2a 2a 20 20 0x0000007f.**
14880 20 20 30 78 38 31 20 30 78 30 30 20 20 20 20 20 0x81 0x00
14881 20 20 20 20 20 20 20 20 20 20 20 20 62 65 63 6f beco
14882 6d 65 73 20 20 30 78 30 30 30 30 30 30 38 30 0a mes 0x00000080.
14883 2a 2a 20 20 20 20 30 78 38 32 20 30 78 30 30 20 ** 0x82 0x00
14884 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14885 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30 30 30 becomes 0x00000
14886 31 30 30 0a 2a 2a 20 20 20 20 30 78 38 30 20 30 100.** 0x80 0
14887 78 37 66 20 20 20 20 20 20 20 20 20 20 20 20 20 x7f
14888 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30 becomes 0x0
14889 30 30 30 30 30 37 66 0a 2a 2a 20 20 20 20 30 78 000007f.** 0x
1488a 38 61 20 30 78 39 31 20 30 78 64 31 20 30 78 61 8a 0x91 0xd1 0xa
1488b 63 20 30 78 37 38 20 20 62 65 63 6f 6d 65 73 20 c 0x78 becomes
1488c 20 30 78 31 32 33 34 35 36 37 38 0a 2a 2a 20 20 0x12345678.**
1488d 20 20 30 78 38 31 20 30 78 38 31 20 30 78 38 31 0x81 0x81 0x81
1488e 20 30 78 38 31 20 30 78 30 31 20 20 62 65 63 6f 0x81 0x01 beco
1488f 6d 65 73 20 20 30 78 31 30 32 30 34 30 38 31 0a mes 0x10204081.
14890 2a 2a 0a 2a 2a 20 56 61 72 69 61 62 6c 65 20 6c **.** Variable l
14891 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73 20 61 ength integers a
14892 72 65 20 75 73 65 64 20 66 6f 72 20 72 6f 77 69 re used for rowi
14893 64 73 20 61 6e 64 20 74 6f 20 68 6f 6c 64 20 74 ds and to hold t
14894 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 he number of.**
14895 62 79 74 65 73 20 6f 66 20 6b 65 79 20 61 6e 64 bytes of key and
14896 20 64 61 74 61 20 69 6e 20 61 20 62 74 72 65 65 data in a btree
14897 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 cell..**.** The
14898 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 63 65 content of a ce
14899 6c 6c 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 ll looks like th
1489a 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a is:.**.** SIZ
1489b 45 20 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e E DESCRIPTION
1489c 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 20 20 50 .** 4 P
1489d 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 age number of th
1489e 65 20 6c 65 66 74 20 63 68 69 6c 64 2e 20 4f 6d e left child. Om
1489f 69 74 74 65 64 20 69 66 20 6c 65 61 66 20 66 6c itted if leaf fl
148a0 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20 ag is set..**
148a1 20 20 76 61 72 20 20 20 20 4e 75 6d 62 65 72 20 var Number
148a2 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 of bytes of data
148a3 2e 20 4f 6d 69 74 74 65 64 20 69 66 20 74 68 65 . Omitted if the
148a4 20 7a 65 72 6f 64 61 74 61 20 66 6c 61 67 20 69 zerodata flag i
148a5 73 20 73 65 74 2e 0a 2a 2a 20 20 20 20 20 76 61 s set..** va
148a6 72 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 62 r Number of b
148a7 79 74 65 73 20 6f 66 20 6b 65 79 2e 20 4f 72 20 ytes of key. Or
148a8 74 68 65 20 6b 65 79 20 69 74 73 65 6c 66 20 69 the key itself i
148a9 66 20 69 6e 74 6b 65 79 20 66 6c 61 67 20 69 73 f intkey flag is
148aa 20 73 65 74 2e 0a 2a 2a 20 20 20 20 20 20 2a 20 set..** *
148ab 20 20 20 20 50 61 79 6c 6f 61 64 0a 2a 2a 20 20 Payload.**
148ac 20 20 20 20 34 20 20 20 20 20 46 69 72 73 74 20 4 First
148ad 70 61 67 65 20 6f 66 20 74 68 65 20 6f 76 65 72 page of the over
148ae 66 6c 6f 77 20 63 68 61 69 6e 2e 20 20 4f 6d 69 flow chain. Omi
148af 74 74 65 64 20 69 66 20 6e 6f 20 6f 76 65 72 66 tted if no overf
148b0 6c 6f 77 0a 2a 2a 0a 2a 2a 20 4f 76 65 72 66 6c low.**.** Overfl
148b1 6f 77 20 70 61 67 65 73 20 66 6f 72 6d 20 61 20 ow pages form a
148b2 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 20 20 45 61 linked list. Ea
148b3 63 68 20 70 61 67 65 20 65 78 63 65 70 74 20 74 ch page except t
148b4 68 65 20 6c 61 73 74 20 69 73 20 63 6f 6d 70 6c he last is compl
148b5 65 74 65 6c 79 0a 2a 2a 20 66 69 6c 6c 65 64 20 etely.** filled
148b6 77 69 74 68 20 64 61 74 61 20 28 70 61 67 65 73 with data (pages
148b7 69 7a 65 20 2d 20 34 20 62 79 74 65 73 29 2e 20 ize - 4 bytes).
148b8 20 54 68 65 20 6c 61 73 74 20 70 61 67 65 20 63 The last page c
148b9 61 6e 20 68 61 76 65 20 61 73 20 6c 69 74 74 6c an have as littl
148ba 65 0a 2a 2a 20 61 73 20 31 20 62 79 74 65 20 6f e.** as 1 byte o
148bb 66 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 20 20 f data..**.**
148bc 20 53 49 5a 45 20 20 20 20 44 45 53 43 52 49 50 SIZE DESCRIP
148bd 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 20 20 TION.** 4
148be 20 20 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f Page number o
148bf 66 20 6e 65 78 74 20 6f 76 65 72 66 6c 6f 77 20 f next overflow
148c0 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 2a 20 20 page.** *
148c1 20 20 20 44 61 74 61 0a 2a 2a 0a 2a 2a 20 46 72 Data.**.** Fr
148c2 65 65 6c 69 73 74 20 70 61 67 65 73 20 63 6f 6d eelist pages com
148c3 65 20 69 6e 20 74 77 6f 20 73 75 62 74 79 70 65 e in two subtype
148c4 73 3a 20 74 72 75 6e 6b 20 70 61 67 65 73 20 61 s: trunk pages a
148c5 6e 64 20 6c 65 61 66 20 70 61 67 65 73 2e 20 20 nd leaf pages.
148c6 54 68 65 0a 2a 2a 20 66 69 6c 65 20 68 65 61 64 The.** file head
148c7 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 er points to the
148c8 20 66 69 72 73 74 20 69 6e 20 61 20 6c 69 6e 6b first in a link
148c9 65 64 20 6c 69 73 74 20 6f 66 20 74 72 75 6e 6b ed list of trunk
148ca 20 70 61 67 65 2e 20 20 45 61 63 68 20 74 72 75 page. Each tru
148cb 6e 6b 0a 2a 2a 20 70 61 67 65 20 70 6f 69 6e 74 nk.** page point
148cc 73 20 74 6f 20 6d 75 6c 74 69 70 6c 65 20 6c 65 s to multiple le
148cd 61 66 20 70 61 67 65 73 2e 20 20 54 68 65 20 63 af pages. The c
148ce 6f 6e 74 65 6e 74 20 6f 66 20 61 20 6c 65 61 66 ontent of a leaf
148cf 20 70 61 67 65 20 69 73 0a 2a 2a 20 75 6e 73 70 page is.** unsp
148d0 65 63 69 66 69 65 64 2e 20 20 41 20 74 72 75 6e ecified. A trun
148d1 6b 20 70 61 67 65 20 6c 6f 6f 6b 73 20 6c 69 6b k page looks lik
148d2 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 e this:.**.**
148d3 20 53 49 5a 45 20 20 20 20 44 45 53 43 52 49 50 SIZE DESCRIP
148d4 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 20 20 TION.** 4
148d5 20 20 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f Page number o
148d6 66 20 6e 65 78 74 20 74 72 75 6e 6b 20 70 61 67 f next trunk pag
148d7 65 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 20 20 e.** 4
148d8 4e 75 6d 62 65 72 20 6f 66 20 6c 65 61 66 20 70 Number of leaf p
148d9 6f 69 6e 74 65 72 73 20 6f 6e 20 74 68 69 73 20 ointers on this
148da 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 2a 20 20 page.** *
148db 20 20 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 zero or more
148dc 70 61 67 65 73 20 6e 75 6d 62 65 72 73 20 6f 66 pages numbers of
148dd 20 6c 65 61 76 65 73 0a 2a 2f 0a 0a 0a 2f 2a 20 leaves.*/.../*
148de 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 The following va
148df 6c 75 65 20 69 73 20 74 68 65 20 6d 61 78 69 6d lue is the maxim
148e0 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 61 73 73 um cell size ass
148e1 75 6d 69 6e 67 20 61 20 6d 61 78 69 6d 75 6d 20 uming a maximum
148e2 70 61 67 65 0a 2a 2a 20 73 69 7a 65 20 67 69 76 page.** size giv
148e3 65 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 64 65 66 e above..*/.#def
148e4 69 6e 65 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 ine MX_CELL_SIZE
148e5 28 70 42 74 29 20 20 28 70 42 74 2d 3e 70 61 67 (pBt) (pBt->pag
148e6 65 53 69 7a 65 2d 38 29 0a 0a 2f 2a 20 54 68 65 eSize-8)../* The
148e7 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 maximum number
148e8 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 61 20 73 69 of cells on a si
148e9 6e 67 6c 65 20 70 61 67 65 20 6f 66 20 74 68 65 ngle page of the
148ea 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 database. This
148eb 0a 2a 2a 20 61 73 73 75 6d 65 73 20 61 20 6d 69 .** assumes a mi
148ec 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 nimum cell size
148ed 6f 66 20 36 20 62 79 74 65 73 20 20 28 34 20 62 of 6 bytes (4 b
148ee 79 74 65 73 20 66 6f 72 20 74 68 65 20 63 65 6c ytes for the cel
148ef 6c 20 69 74 73 65 6c 66 0a 2a 2a 20 70 6c 75 73 l itself.** plus
148f0 20 32 20 62 79 74 65 73 20 66 6f 72 20 74 68 65 2 bytes for the
148f1 20 69 6e 64 65 78 20 74 6f 20 74 68 65 20 63 65 index to the ce
148f2 6c 6c 20 69 6e 20 74 68 65 20 70 61 67 65 20 68 ll in the page h
148f3 65 61 64 65 72 29 2e 20 20 53 75 63 68 0a 2a 2a eader). Such.**
148f4 20 73 6d 61 6c 6c 20 63 65 6c 6c 73 20 77 69 6c small cells wil
148f5 6c 20 62 65 20 72 61 72 65 2c 20 62 75 74 20 74 l be rare, but t
148f6 68 65 79 20 61 72 65 20 70 6f 73 73 69 62 6c 65 hey are possible
148f7 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f ..*/.#define MX_
148f8 43 45 4c 4c 28 70 42 74 29 20 28 28 70 42 74 2d CELL(pBt) ((pBt-
148f9 3e 70 61 67 65 53 69 7a 65 2d 38 29 2f 36 29 0a >pageSize-8)/6).
148fa 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c ./* Forward decl
148fb 61 72 61 74 69 6f 6e 73 20 2a 2f 0a 74 79 70 65 arations */.type
148fc 64 65 66 20 73 74 72 75 63 74 20 4d 65 6d 50 61 def struct MemPa
148fd 67 65 20 4d 65 6d 50 61 67 65 3b 0a 74 79 70 65 ge MemPage;.type
148fe 64 65 66 20 73 74 72 75 63 74 20 42 74 4c 6f 63 def struct BtLoc
148ff 6b 20 42 74 4c 6f 63 6b 3b 0a 0a 2f 2a 0a 2a 2a k BtLock;../*.**
14900 20 54 68 69 73 20 69 73 20 61 20 6d 61 67 69 63 This is a magic
14901 20 73 74 72 69 6e 67 20 74 68 61 74 20 61 70 70 string that app
14902 65 61 72 73 20 61 74 20 74 68 65 20 62 65 67 69 ears at the begi
14903 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72 79 0a 2a nning of every.*
14904 2a 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 * SQLite databas
14905 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 69 64 e in order to id
14906 65 6e 74 69 66 79 20 74 68 65 20 66 69 6c 65 20 entify the file
14907 61 73 20 61 20 72 65 61 6c 20 64 61 74 61 62 61 as a real databa
14908 73 65 2e 0a 2a 2a 0a 2a 2a 20 59 6f 75 20 63 61 se..**.** You ca
14909 6e 20 63 68 61 6e 67 65 20 74 68 69 73 20 76 61 n change this va
1490a 6c 75 65 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 lue at compile-t
1490b 69 6d 65 20 62 79 20 73 70 65 63 69 66 79 69 6e ime by specifyin
1490c 67 20 61 0a 2a 2a 20 2d 44 53 51 4c 49 54 45 5f g a.** -DSQLITE_
1490d 46 49 4c 45 5f 48 45 41 44 45 52 3d 22 2e 2e 2e FILE_HEADER="...
1490e 22 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 " on the compile
1490f 72 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 2e 20 r command-line.
14910 20 54 68 65 0a 2a 2a 20 68 65 61 64 65 72 20 6d The.** header m
14911 75 73 74 20 62 65 20 65 78 61 63 74 6c 79 20 31 ust be exactly 1
14912 36 20 62 79 74 65 73 20 69 6e 63 6c 75 64 69 6e 6 bytes includin
14913 67 20 74 68 65 20 7a 65 72 6f 2d 74 65 72 6d 69 g the zero-termi
14914 6e 61 74 6f 72 20 73 6f 0a 2a 2a 20 74 68 65 20 nator so.** the
14915 73 74 72 69 6e 67 20 69 74 73 65 6c 66 20 73 68 string itself sh
14916 6f 75 6c 64 20 62 65 20 31 35 20 63 68 61 72 61 ould be 15 chara
14917 63 74 65 72 73 20 6c 6f 6e 67 2e 20 20 49 66 20 cters long. If
14918 79 6f 75 20 63 68 61 6e 67 65 0a 2a 2a 20 74 68 you change.** th
14919 65 20 68 65 61 64 65 72 2c 20 74 68 65 6e 20 79 e header, then y
1491a 6f 75 72 20 63 75 73 74 6f 6d 20 6c 69 62 72 61 our custom libra
1491b 72 79 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 ry will not be a
1491c 62 6c 65 20 74 6f 20 72 65 61 64 20 0a 2a 2a 20 ble to read .**
1491d 64 61 74 61 62 61 73 65 73 20 67 65 6e 65 72 61 databases genera
1491e 74 65 64 20 62 79 20 74 68 65 20 73 74 61 6e 64 ted by the stand
1491f 61 72 64 20 74 6f 6f 6c 73 20 61 6e 64 20 74 68 ard tools and th
14920 65 20 73 74 61 6e 64 61 72 64 20 74 6f 6f 6c 73 e standard tools
14921 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 .** will not be
14922 61 62 6c 65 20 74 6f 20 72 65 61 64 20 64 61 74 able to read dat
14923 61 62 61 73 65 73 20 63 72 65 61 74 65 64 20 62 abases created b
14924 79 20 79 6f 75 72 20 63 75 73 74 6f 6d 20 6c 69 y your custom li
14925 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 6e 64 65 brary..*/.#ifnde
14926 66 20 53 51 4c 49 54 45 5f 46 49 4c 45 5f 48 45 f SQLITE_FILE_HE
14927 41 44 45 52 20 2f 2a 20 31 32 33 34 35 36 37 38 ADER /* 12345678
14928 39 20 31 32 33 34 35 36 20 2a 2f 0a 23 20 20 64 9 123456 */.# d
14929 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 49 4c efine SQLITE_FIL
1492a 45 5f 48 45 41 44 45 52 20 22 53 51 4c 69 74 65 E_HEADER "SQLite
1492b 20 66 6f 72 6d 61 74 20 33 22 0a 23 65 6e 64 69 format 3".#endi
1492c 66 0a 0a 2f 2a 0a 2a 2a 20 50 61 67 65 20 74 79 f../*.** Page ty
1492d 70 65 20 66 6c 61 67 73 2e 20 20 41 6e 20 4f 52 pe flags. An OR
1492e 65 64 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f ed combination o
1492f 66 20 74 68 65 73 65 20 66 6c 61 67 73 20 61 70 f these flags ap
14930 70 65 61 72 20 61 73 20 74 68 65 0a 2a 2a 20 66 pear as the.** f
14931 69 72 73 74 20 62 79 74 65 20 6f 66 20 6f 6e 2d irst byte of on-
14932 64 69 73 6b 20 69 6d 61 67 65 20 6f 66 20 65 76 disk image of ev
14933 65 72 79 20 42 54 72 65 65 20 70 61 67 65 2e 0a ery BTree page..
14934 2a 2f 0a 23 64 65 66 69 6e 65 20 50 54 46 5f 49 */.#define PTF_I
14935 4e 54 4b 45 59 20 20 20 20 30 78 30 31 0a 23 64 NTKEY 0x01.#d
14936 65 66 69 6e 65 20 50 54 46 5f 5a 45 52 4f 44 41 efine PTF_ZERODA
14937 54 41 20 20 30 78 30 32 0a 23 64 65 66 69 6e 65 TA 0x02.#define
14938 20 50 54 46 5f 4c 45 41 46 44 41 54 41 20 20 30 PTF_LEAFDATA 0
14939 78 30 34 0a 23 64 65 66 69 6e 65 20 50 54 46 5f x04.#define PTF_
1493a 4c 45 41 46 20 20 20 20 20 20 30 78 30 38 0a 0a LEAF 0x08..
1493b 2f 2a 0a 2a 2a 20 41 73 20 65 61 63 68 20 70 61 /*.** As each pa
1493c 67 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 ge of the file i
1493d 73 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20 6d 65 s loaded into me
1493e 6d 6f 72 79 2c 20 61 6e 20 69 6e 73 74 61 6e 63 mory, an instanc
1493f 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
14940 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 ng.** structure
14941 69 73 20 61 70 70 65 6e 64 65 64 20 61 6e 64 20 is appended and
14942 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 7a initialized to z
14943 65 72 6f 2e 20 20 54 68 69 73 20 73 74 72 75 63 ero. This struc
14944 74 75 72 65 20 73 74 6f 72 65 73 0a 2a 2a 20 69 ture stores.** i
14945 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 nformation about
14946 20 74 68 65 20 70 61 67 65 20 74 68 61 74 20 69 the page that i
14947 73 20 64 65 63 6f 64 65 64 20 66 72 6f 6d 20 74 s decoded from t
14948 68 65 20 72 61 77 20 66 69 6c 65 20 70 61 67 65 he raw file page
14949 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 50 61 72 ..**.** The pPar
1494a 65 6e 74 20 66 69 65 6c 64 20 70 6f 69 6e 74 73 ent field points
1494b 20 62 61 63 6b 20 74 6f 20 74 68 65 20 70 61 72 back to the par
1494c 65 6e 74 20 70 61 67 65 2e 20 20 54 68 69 73 20 ent page. This
1494d 61 6c 6c 6f 77 73 20 75 73 20 74 6f 0a 2a 2a 20 allows us to.**
1494e 77 61 6c 6b 20 75 70 20 74 68 65 20 42 54 72 65 walk up the BTre
1494f 65 20 66 72 6f 6d 20 61 6e 79 20 6c 65 61 66 20 e from any leaf
14950 74 6f 20 74 68 65 20 72 6f 6f 74 2e 20 20 43 61 to the root. Ca
14951 72 65 20 6d 75 73 74 20 62 65 20 74 61 6b 65 6e re must be taken
14952 20 74 6f 0a 2a 2a 20 75 6e 72 65 66 28 29 20 74 to.** unref() t
14953 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 70 he parent page p
14954 6f 69 6e 74 65 72 20 77 68 65 6e 20 74 68 69 73 ointer when this
14955 20 70 61 67 65 20 69 73 20 6e 6f 20 6c 6f 6e 67 page is no long
14956 65 72 20 72 65 66 65 72 65 6e 63 65 64 2e 0a 2a er referenced..*
14957 2a 20 54 68 65 20 70 61 67 65 44 65 73 74 72 75 * The pageDestru
14958 63 74 6f 72 28 29 20 72 6f 75 74 69 6e 65 20 68 ctor() routine h
14959 61 6e 64 6c 65 73 20 74 68 61 74 20 63 68 6f 72 andles that chor
1495a 65 2e 0a 2a 2a 0a 2a 2a 20 41 63 63 65 73 73 20 e..**.** Access
1495b 74 6f 20 61 6c 6c 20 66 69 65 6c 64 73 20 6f 66 to all fields of
1495c 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 this structure
1495d 69 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 is controlled by
1495e 20 74 68 65 20 6d 75 74 65 78 0a 2a 2a 20 73 74 the mutex.** st
1495f 6f 72 65 64 20 69 6e 20 4d 65 6d 50 61 67 65 2e ored in MemPage.
14960 70 42 74 2d 3e 6d 75 74 65 78 2e 0a 2a 2f 0a 73 pBt->mutex..*/.s
14961 74 72 75 63 74 20 4d 65 6d 50 61 67 65 20 7b 0a truct MemPage {.
14962 20 20 75 38 20 69 73 49 6e 69 74 3b 20 20 20 20 u8 isInit;
14963 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
14964 66 20 70 72 65 76 69 6f 75 73 6c 79 20 69 6e 69 f previously ini
14965 74 69 61 6c 69 7a 65 64 2e 20 4d 55 53 54 20 42 tialized. MUST B
14966 45 20 46 49 52 53 54 21 20 2a 2f 0a 20 20 75 38 E FIRST! */. u8
14967 20 6e 4f 76 65 72 66 6c 6f 77 3b 20 20 20 20 20 nOverflow;
14968 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
14969 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 62 6f overflow cell bo
1496a 64 69 65 73 20 69 6e 20 61 43 65 6c 6c 5b 5d 20 dies in aCell[]
1496b 2a 2f 0a 20 20 75 38 20 69 6e 74 4b 65 79 3b 20 */. u8 intKey;
1496c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
1496d 65 20 69 66 20 69 6e 74 6b 65 79 20 66 6c 61 67 e if intkey flag
1496e 20 69 73 20 73 65 74 20 2a 2f 0a 20 20 75 38 20 is set */. u8
1496f 6c 65 61 66 3b 20 20 20 20 20 20 20 20 20 20 20 leaf;
14970 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6c 65 61 /* True if lea
14971 66 20 66 6c 61 67 20 69 73 20 73 65 74 20 2a 2f f flag is set */
14972 0a 20 20 75 38 20 68 61 73 44 61 74 61 3b 20 20 . u8 hasData;
14973 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
14974 69 66 20 74 68 69 73 20 70 61 67 65 20 73 74 6f if this page sto
14975 72 65 73 20 64 61 74 61 20 2a 2f 0a 20 20 75 38 res data */. u8
14976 20 68 64 72 4f 66 66 73 65 74 3b 20 20 20 20 20 hdrOffset;
14977 20 20 20 2f 2a 20 31 30 30 20 66 6f 72 20 70 61 /* 100 for pa
14978 67 65 20 31 2e 20 20 30 20 6f 74 68 65 72 77 69 ge 1. 0 otherwi
14979 73 65 20 2a 2f 0a 20 20 75 38 20 63 68 69 6c 64 se */. u8 child
1497a 50 74 72 53 69 7a 65 3b 20 20 20 20 20 2f 2a 20 PtrSize; /*
1497b 30 20 69 66 20 6c 65 61 66 3d 3d 31 2e 20 20 34 0 if leaf==1. 4
1497c 20 69 66 20 6c 65 61 66 3d 3d 30 20 2a 2f 0a 20 if leaf==0 */.
1497d 20 75 31 36 20 6d 61 78 4c 6f 63 61 6c 3b 20 20 u16 maxLocal;
1497e 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 /* Copy of
1497f 20 42 74 53 68 61 72 65 64 2e 6d 61 78 4c 6f 63 BtShared.maxLoc
14980 61 6c 20 6f 72 20 42 74 53 68 61 72 65 64 2e 6d al or BtShared.m
14981 61 78 4c 65 61 66 20 2a 2f 0a 20 20 75 31 36 20 axLeaf */. u16
14982 6d 69 6e 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20 minLocal;
14983 20 2f 2a 20 43 6f 70 79 20 6f 66 20 42 74 53 68 /* Copy of BtSh
14984 61 72 65 64 2e 6d 69 6e 4c 6f 63 61 6c 20 6f 72 ared.minLocal or
14985 20 42 74 53 68 61 72 65 64 2e 6d 69 6e 4c 65 61 BtShared.minLea
14986 66 20 2a 2f 0a 20 20 75 31 36 20 63 65 6c 6c 4f f */. u16 cellO
14987 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 49 ffset; /* I
14988 6e 64 65 78 20 69 6e 20 61 44 61 74 61 20 6f 66 ndex in aData of
14989 20 66 69 72 73 74 20 63 65 6c 6c 20 70 6f 69 6e first cell poin
1498a 74 65 72 20 2a 2f 0a 20 20 75 31 36 20 6e 46 72 ter */. u16 nFr
1498b 65 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a ee; /*
1498c 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 20 Number of free
1498d 62 79 74 65 73 20 6f 6e 20 74 68 65 20 70 61 67 bytes on the pag
1498e 65 20 2a 2f 0a 20 20 75 31 36 20 6e 43 65 6c 6c e */. u16 nCell
1498f 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e ; /* N
14990 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f umber of cells o
14991 6e 20 74 68 69 73 20 70 61 67 65 2c 20 6c 6f 63 n this page, loc
14992 61 6c 20 61 6e 64 20 6f 76 66 6c 20 2a 2f 0a 20 al and ovfl */.
14993 20 75 31 36 20 6d 61 73 6b 50 61 67 65 3b 20 20 u16 maskPage;
14994 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 66 6f /* Mask fo
14995 72 20 70 61 67 65 20 6f 66 66 73 65 74 20 2a 2f r page offset */
14996 0a 20 20 73 74 72 75 63 74 20 5f 4f 76 66 6c 43 . struct _OvflC
14997 65 6c 6c 20 7b 20 20 20 2f 2a 20 43 65 6c 6c 73 ell { /* Cells
14998 20 74 68 61 74 20 77 69 6c 6c 20 6e 6f 74 20 66 that will not f
14999 69 74 20 6f 6e 20 61 44 61 74 61 5b 5d 20 2a 2f it on aData[] */
1499a 0a 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 3b 20 . u8 *pCell;
1499b 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e /* Poin
1499c 74 65 72 73 20 74 6f 20 74 68 65 20 62 6f 64 79 ters to the body
1499d 20 6f 66 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 of the overflow
1499e 20 63 65 6c 6c 20 2a 2f 0a 20 20 20 20 75 31 36 cell */. u16
1499f 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 idx;
149a0 20 2f 2a 20 49 6e 73 65 72 74 20 74 68 69 73 20 /* Insert this
149a1 63 65 6c 6c 20 62 65 66 6f 72 65 20 69 64 78 2d cell before idx-
149a2 74 68 20 6e 6f 6e 2d 6f 76 65 72 66 6c 6f 77 20 th non-overflow
149a3 63 65 6c 6c 20 2a 2f 0a 20 20 7d 20 61 4f 76 66 cell */. } aOvf
149a4 6c 5b 35 5d 3b 0a 20 20 42 74 53 68 61 72 65 64 l[5];. BtShared
149a5 20 2a 70 42 74 3b 20 20 20 20 20 20 20 2f 2a 20 *pBt; /*
149a6 50 6f 69 6e 74 65 72 20 74 6f 20 42 74 53 68 61 Pointer to BtSha
149a7 72 65 64 20 74 68 61 74 20 74 68 69 73 20 70 61 red that this pa
149a8 67 65 20 69 73 20 70 61 72 74 20 6f 66 20 2a 2f ge is part of */
149a9 0a 20 20 75 38 20 2a 61 44 61 74 61 3b 20 20 20 . u8 *aData;
149aa 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 /* Point
149ab 65 72 20 74 6f 20 64 69 73 6b 20 69 6d 61 67 65 er to disk image
149ac 20 6f 66 20 74 68 65 20 70 61 67 65 20 64 61 74 of the page dat
149ad 61 20 2a 2f 0a 20 20 44 62 50 61 67 65 20 2a 70 a */. DbPage *p
149ae 44 62 50 61 67 65 3b 20 20 20 20 20 2f 2a 20 50 DbPage; /* P
149af 61 67 65 72 20 70 61 67 65 20 68 61 6e 64 6c 65 ager page handle
149b0 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b */. Pgno pgno;
149b1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
149b2 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 ge number for th
149b3 69 73 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a 0a 2f is page */.};../
149b4 2a 0a 2a 2a 20 54 68 65 20 69 6e 2d 6d 65 6d 6f *.** The in-memo
149b5 72 79 20 69 6d 61 67 65 20 6f 66 20 61 20 64 69 ry image of a di
149b6 73 6b 20 70 61 67 65 20 68 61 73 20 74 68 65 20 sk page has the
149b7 61 75 78 69 6c 69 61 72 79 20 69 6e 66 6f 72 6d auxiliary inform
149b8 61 74 69 6f 6e 20 61 70 70 65 6e 64 65 64 0a 2a ation appended.*
149b9 2a 20 74 6f 20 74 68 65 20 65 6e 64 2e 20 20 45 * to the end. E
149ba 58 54 52 41 5f 53 49 5a 45 20 69 73 20 74 68 65 XTRA_SIZE is the
149bb 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
149bc 20 6f 66 20 73 70 61 63 65 20 6e 65 65 64 65 64 of space needed
149bd 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20 74 68 61 74 to hold.** that
149be 20 65 78 74 72 61 20 69 6e 66 6f 72 6d 61 74 69 extra informati
149bf 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 on..*/.#define E
149c0 58 54 52 41 5f 53 49 5a 45 20 73 69 7a 65 6f 66 XTRA_SIZE sizeof
149c1 28 4d 65 6d 50 61 67 65 29 0a 0a 2f 2a 0a 2a 2a (MemPage)../*.**
149c2 20 41 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f A linked list o
149c3 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
149c4 73 74 72 75 63 74 75 72 65 73 20 69 73 20 73 74 structures is st
149c5 6f 72 65 64 20 61 74 20 42 74 53 68 61 72 65 64 ored at BtShared
149c6 2e 70 4c 6f 63 6b 2e 0a 2a 2a 20 4c 6f 63 6b 73 .pLock..** Locks
149c7 20 61 72 65 20 61 64 64 65 64 20 28 6f 72 20 75 are added (or u
149c8 70 67 72 61 64 65 64 20 66 72 6f 6d 20 52 45 41 pgraded from REA
149c9 44 5f 4c 4f 43 4b 20 74 6f 20 57 52 49 54 45 5f D_LOCK to WRITE_
149ca 4c 4f 43 4b 29 20 77 68 65 6e 20 61 20 63 75 72 LOCK) when a cur
149cb 73 6f 72 20 0a 2a 2a 20 69 73 20 6f 70 65 6e 65 sor .** is opene
149cc 64 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 77 d on the table w
149cd 69 74 68 20 72 6f 6f 74 20 70 61 67 65 20 42 74 ith root page Bt
149ce 53 68 61 72 65 64 2e 69 54 61 62 6c 65 2e 20 4c Shared.iTable. L
149cf 6f 63 6b 73 20 61 72 65 20 72 65 6d 6f 76 65 64 ocks are removed
149d0 0a 2a 2a 20 66 72 6f 6d 20 74 68 69 73 20 6c 69 .** from this li
149d1 73 74 20 77 68 65 6e 20 61 20 74 72 61 6e 73 61 st when a transa
149d2 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 ction is committ
149d3 65 64 20 6f 72 20 72 6f 6c 6c 65 64 20 62 61 63 ed or rolled bac
149d4 6b 2c 20 6f 72 20 77 68 65 6e 0a 2a 2a 20 61 20 k, or when.** a
149d5 62 74 72 65 65 20 68 61 6e 64 6c 65 20 69 73 20 btree handle is
149d6 63 6c 6f 73 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 closed..*/.struc
149d7 74 20 42 74 4c 6f 63 6b 20 7b 0a 20 20 42 74 72 t BtLock {. Btr
149d8 65 65 20 2a 70 42 74 72 65 65 3b 20 20 20 20 20 ee *pBtree;
149d9 20 20 20 2f 2a 20 42 74 72 65 65 20 68 61 6e 64 /* Btree hand
149da 6c 65 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 le holding this
149db 6c 6f 63 6b 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 lock */. Pgno i
149dc 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 Table;
149dd 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 /* Root page of
149de 74 61 62 6c 65 20 2a 2f 0a 20 20 75 38 20 65 4c table */. u8 eL
149df 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 ock;
149e0 20 2f 2a 20 52 45 41 44 5f 4c 4f 43 4b 20 6f 72 /* READ_LOCK or
149e1 20 57 52 49 54 45 5f 4c 4f 43 4b 20 2a 2f 0a 20 WRITE_LOCK */.
149e2 20 42 74 4c 6f 63 6b 20 2a 70 4e 65 78 74 3b 20 BtLock *pNext;
149e3 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 69 /* Next i
149e4 6e 20 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b n BtShared.pLock
149e5 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 list */.};../*
149e6 43 61 6e 64 69 64 61 74 65 20 76 61 6c 75 65 73 Candidate values
149e7 20 66 6f 72 20 42 74 4c 6f 63 6b 2e 65 4c 6f 63 for BtLock.eLoc
149e8 6b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 52 45 41 k */.#define REA
149e9 44 5f 4c 4f 43 4b 20 20 20 20 20 31 0a 23 64 65 D_LOCK 1.#de
149ea 66 69 6e 65 20 57 52 49 54 45 5f 4c 4f 43 4b 20 fine WRITE_LOCK
149eb 20 20 20 32 0a 0a 2f 2a 20 41 20 42 74 72 65 65 2../* A Btree
149ec 20 68 61 6e 64 6c 65 0a 2a 2a 0a 2a 2a 20 41 20 handle.**.** A
149ed 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
149ee 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 ion contains a p
149ef 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 ointer to an ins
149f0 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 69 73 tance of.** this
149f1 20 6f 62 6a 65 63 74 20 66 6f 72 20 65 76 65 72 object for ever
149f2 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 y database file
149f3 74 68 61 74 20 69 74 20 68 61 73 20 6f 70 65 6e that it has open
149f4 2e 20 20 54 68 69 73 20 73 74 72 75 63 74 75 72 . This structur
149f5 65 0a 2a 2a 20 69 73 20 6f 70 61 71 75 65 20 74 e.** is opaque t
149f6 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 o the database c
149f7 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 onnection. The
149f8 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
149f9 69 6f 6e 20 63 61 6e 6e 6f 74 0a 2a 2a 20 73 65 ion cannot.** se
149fa 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 73 20 e the internals
149fb 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 of this structur
149fc 65 20 61 6e 64 20 6f 6e 6c 79 20 64 65 61 6c 73 e and only deals
149fd 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 with pointers t
149fe 6f 0a 2a 2a 20 74 68 69 73 20 73 74 72 75 63 74 o.** this struct
149ff 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 73 ure..**.** For s
14a00 6f 6d 65 20 64 61 74 61 62 61 73 65 20 66 69 6c ome database fil
14a01 65 73 2c 20 74 68 65 20 73 61 6d 65 20 75 6e 64 es, the same und
14a02 65 72 6c 79 69 6e 67 20 64 61 74 61 62 61 73 65 erlying database
14a03 20 63 61 63 68 65 20 6d 69 67 68 74 20 62 65 20 cache might be
14a04 0a 2a 2a 20 73 68 61 72 65 64 20 62 65 74 77 65 .** shared betwe
14a05 65 6e 20 6d 75 6c 74 69 70 6c 65 20 63 6f 6e 6e en multiple conn
14a06 65 63 74 69 6f 6e 73 2e 20 20 49 6e 20 74 68 61 ections. In tha
14a07 74 20 63 61 73 65 2c 20 65 61 63 68 20 63 6f 6e t case, each con
14a08 6e 65 63 74 69 6f 6e 0a 2a 2a 20 68 61 73 20 69 nection.** has i
14a09 74 20 6f 77 6e 20 69 6e 73 74 61 6e 63 65 20 6f t own instance o
14a0a 66 20 74 68 69 73 20 6f 62 6a 65 63 74 2e 20 20 f this object.
14a0b 42 75 74 20 65 61 63 68 20 69 6e 73 74 61 6e 63 But each instanc
14a0c 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 e of this object
14a0d 0a 2a 2a 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 .** points to th
14a0e 65 20 73 61 6d 65 20 42 74 53 68 61 72 65 64 20 e same BtShared
14a0f 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 64 61 74 object. The dat
14a10 61 62 61 73 65 20 63 61 63 68 65 20 61 6e 64 20 abase cache and
14a11 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 61 73 the.** schema as
14a12 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 sociated with th
14a13 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
14a14 61 72 65 20 61 6c 6c 20 63 6f 6e 74 61 69 6e 65 are all containe
14a15 64 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 d within.** the
14a16 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 2e BtShared object.
14a17 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 66 69 65 6c 64 .**.** All field
14a18 73 20 69 6e 20 74 68 69 73 20 73 74 72 75 63 74 s in this struct
14a19 75 72 65 20 61 72 65 20 61 63 63 65 73 73 65 64 ure are accessed
14a1a 20 75 6e 64 65 72 20 73 71 6c 69 74 65 33 2e 6d under sqlite3.m
14a1b 75 74 65 78 2e 0a 2a 2a 20 54 68 65 20 70 42 74 utex..** The pBt
14a1c 20 70 6f 69 6e 74 65 72 20 69 74 73 65 6c 66 20 pointer itself
14a1d 6d 61 79 20 6e 6f 74 20 62 65 20 63 68 61 6e 67 may not be chang
14a1e 65 64 20 77 68 69 6c 65 20 74 68 65 72 65 20 65 ed while there e
14a1f 78 69 73 74 73 20 63 75 72 73 6f 72 73 20 0a 2a xists cursors .*
14a20 2a 20 69 6e 20 74 68 65 20 72 65 66 65 72 65 6e * in the referen
14a21 63 65 64 20 42 74 53 68 61 72 65 64 20 74 68 61 ced BtShared tha
14a22 74 20 70 6f 69 6e 74 20 62 61 63 6b 20 74 6f 20 t point back to
14a23 74 68 69 73 20 42 74 72 65 65 20 73 69 6e 63 65 this Btree since
14a24 20 74 68 6f 73 65 0a 2a 2a 20 63 75 72 73 6f 72 those.** cursor
14a25 73 20 68 61 76 65 20 74 6f 20 64 6f 20 67 6f 20 s have to do go
14a26 74 68 72 6f 75 67 68 20 74 68 69 73 20 42 74 72 through this Btr
14a27 65 65 20 74 6f 20 66 69 6e 64 20 74 68 65 69 72 ee to find their
14a28 20 42 74 53 68 61 72 65 64 20 61 6e 64 0a 2a 2a BtShared and.**
14a29 20 74 68 65 79 20 6f 66 74 65 6e 20 64 6f 20 73 they often do s
14a2a 6f 20 77 69 74 68 6f 75 74 20 68 6f 6c 64 69 6e o without holdin
14a2b 67 20 73 71 6c 69 74 65 33 2e 6d 75 74 65 78 2e g sqlite3.mutex.
14a2c 0a 2a 2f 0a 73 74 72 75 63 74 20 42 74 72 65 65 .*/.struct Btree
14a2d 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 {. sqlite3 *db
14a2e 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 ; /* The d
14a2f 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
14a30 6f 6e 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 on holding this
14a31 62 74 72 65 65 20 2a 2f 0a 20 20 42 74 53 68 61 btree */. BtSha
14a32 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 2f 2a red *pBt; /*
14a33 20 53 68 61 72 61 62 6c 65 20 63 6f 6e 74 65 6e Sharable conten
14a34 74 20 6f 66 20 74 68 69 73 20 62 74 72 65 65 20 t of this btree
14a35 2a 2f 0a 20 20 75 38 20 69 6e 54 72 61 6e 73 3b */. u8 inTrans;
14a36 20 20 20 20 20 20 20 20 2f 2a 20 54 52 41 4e 53 /* TRANS
14a37 5f 4e 4f 4e 45 2c 20 54 52 41 4e 53 5f 52 45 41 _NONE, TRANS_REA
14a38 44 20 6f 72 20 54 52 41 4e 53 5f 57 52 49 54 45 D or TRANS_WRITE
14a39 20 2a 2f 0a 20 20 75 38 20 73 68 61 72 61 62 6c */. u8 sharabl
14a3a 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 e; /* True
14a3b 20 69 66 20 77 65 20 63 61 6e 20 73 68 61 72 65 if we can share
14a3c 20 70 42 74 20 77 69 74 68 20 61 6e 6f 74 68 65 pBt with anothe
14a3d 72 20 64 62 20 2a 2f 0a 20 20 75 38 20 6c 6f 63 r db */. u8 loc
14a3e 6b 65 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 ked; /*
14a3f 54 72 75 65 20 69 66 20 64 62 20 63 75 72 72 65 True if db curre
14a40 6e 74 6c 79 20 68 61 73 20 70 42 74 20 6c 6f 63 ntly has pBt loc
14a41 6b 65 64 20 2a 2f 0a 20 20 69 6e 74 20 77 61 6e ked */. int wan
14a42 74 54 6f 4c 6f 63 6b 3b 20 20 20 20 2f 2a 20 4e tToLock; /* N
14a43 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 64 20 umber of nested
14a44 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 calls to sqlite3
14a45 42 74 72 65 65 45 6e 74 65 72 28 29 20 2a 2f 0a BtreeEnter() */.
14a46 20 20 69 6e 74 20 6e 42 61 63 6b 75 70 3b 20 20 int nBackup;
14a47 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
14a48 66 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 f backup operati
14a49 6f 6e 73 20 72 65 61 64 69 6e 67 20 74 68 69 73 ons reading this
14a4a 20 62 74 72 65 65 20 2a 2f 0a 20 20 42 74 72 65 btree */. Btre
14a4b 65 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 2f e *pNext; /
14a4c 2a 20 4c 69 73 74 20 6f 66 20 6f 74 68 65 72 20 * List of other
14a4d 73 68 61 72 61 62 6c 65 20 42 74 72 65 65 73 20 sharable Btrees
14a4e 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 64 62 from the same db
14a4f 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 50 72 */. Btree *pPr
14a50 65 76 3b 20 20 20 20 20 20 2f 2a 20 42 61 63 6b ev; /* Back
14a51 20 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 65 20 pointer of the
14a52 73 61 6d 65 20 6c 69 73 74 20 2a 2f 0a 23 69 66 same list */.#if
14a53 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
14a54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 _SHARED_CACHE.
14a55 42 74 4c 6f 63 6b 20 6c 6f 63 6b 3b 20 20 20 20 BtLock lock;
14a56 20 20 20 2f 2a 20 4f 62 6a 65 63 74 20 75 73 65 /* Object use
14a57 64 20 74 6f 20 6c 6f 63 6b 20 70 61 67 65 20 31 d to lock page 1
14a58 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f */.#endif.};../
14a59 2a 0a 2a 2a 20 42 74 72 65 65 2e 69 6e 54 72 61 *.** Btree.inTra
14a5a 6e 73 20 6d 61 79 20 74 61 6b 65 20 6f 6e 65 20 ns may take one
14a5b 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
14a5c 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 values..**.** I
14a5d 66 20 74 68 65 20 73 68 61 72 65 64 2d 64 61 74 f the shared-dat
14a5e 61 20 65 78 74 65 6e 73 69 6f 6e 20 69 73 20 65 a extension is e
14a5f 6e 61 62 6c 65 64 2c 20 74 68 65 72 65 20 6d 61 nabled, there ma
14a60 79 20 62 65 20 6d 75 6c 74 69 70 6c 65 20 75 73 y be multiple us
14a61 65 72 73 0a 2a 2a 20 6f 66 20 74 68 65 20 42 74 ers.** of the Bt
14a62 72 65 65 20 73 74 72 75 63 74 75 72 65 2e 20 41 ree structure. A
14a63 74 20 6d 6f 73 74 20 6f 6e 65 20 6f 66 20 74 68 t most one of th
14a64 65 73 65 20 6d 61 79 20 6f 70 65 6e 20 61 20 77 ese may open a w
14a65 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e rite transaction
14a66 2c 0a 2a 2a 20 62 75 74 20 61 6e 79 20 6e 75 6d ,.** but any num
14a67 62 65 72 20 6d 61 79 20 68 61 76 65 20 61 63 74 ber may have act
14a68 69 76 65 20 72 65 61 64 20 74 72 61 6e 73 61 63 ive read transac
14a69 74 69 6f 6e 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e tions..*/.#defin
14a6a 65 20 54 52 41 4e 53 5f 4e 4f 4e 45 20 20 30 0a e TRANS_NONE 0.
14a6b 23 64 65 66 69 6e 65 20 54 52 41 4e 53 5f 52 45 #define TRANS_RE
14a6c 41 44 20 20 31 0a 23 64 65 66 69 6e 65 20 54 52 AD 1.#define TR
14a6d 41 4e 53 5f 57 52 49 54 45 20 32 0a 0a 2f 2a 0a ANS_WRITE 2../*.
14a6e 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f ** An instance o
14a6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 72 65 f this object re
14a70 70 72 65 73 65 6e 74 73 20 61 20 73 69 6e 67 6c presents a singl
14a71 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
14a72 0a 2a 2a 20 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 .** .** A single
14a73 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 63 database file c
14a74 61 6e 20 62 65 20 69 6e 20 75 73 65 20 61 73 20 an be in use as
14a75 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 62 79 the same time by
14a76 20 74 77 6f 0a 2a 2a 20 6f 72 20 6d 6f 72 65 20 two.** or more
14a77 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
14a78 69 6f 6e 73 2e 20 20 57 68 65 6e 20 74 77 6f 20 ions. When two
14a79 6f 72 20 6d 6f 72 65 20 63 6f 6e 6e 65 63 74 69 or more connecti
14a7a 6f 6e 73 20 61 72 65 0a 2a 2a 20 73 68 61 72 69 ons are.** shari
14a7b 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 ng the same data
14a7c 62 61 73 65 20 66 69 6c 65 2c 20 65 61 63 68 20 base file, each
14a7d 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 73 20 69 connection has i
14a7e 74 20 6f 77 6e 0a 2a 2a 20 70 72 69 76 61 74 65 t own.** private
14a7f 20 42 74 72 65 65 20 6f 62 6a 65 63 74 20 66 6f Btree object fo
14a80 72 20 74 68 65 20 66 69 6c 65 20 61 6e 64 20 65 r the file and e
14a81 61 63 68 20 6f 66 20 74 68 6f 73 65 20 42 74 72 ach of those Btr
14a82 65 65 73 20 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f ees points.** to
14a83 20 74 68 69 73 20 6f 6e 65 20 42 74 53 68 61 72 this one BtShar
14a84 65 64 20 6f 62 6a 65 63 74 2e 20 20 42 74 53 68 ed object. BtSh
14a85 61 72 65 64 2e 6e 52 65 66 20 69 73 20 74 68 65 ared.nRef is the
14a86 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 6f number of.** co
14a87 6e 6e 65 63 74 69 6f 6e 73 20 63 75 72 72 65 6e nnections curren
14a88 74 6c 79 20 73 68 61 72 69 6e 67 20 74 68 69 73 tly sharing this
14a89 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a database file..
14a8a 2a 2a 0a 2a 2a 20 46 69 65 6c 64 73 20 69 6e 20 **.** Fields in
14a8b 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 61 this structure a
14a8c 72 65 20 61 63 63 65 73 73 65 64 20 75 6e 64 65 re accessed unde
14a8d 72 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 6d r the BtShared.m
14a8e 75 74 65 78 0a 2a 2a 20 6d 75 74 65 78 2c 20 65 utex.** mutex, e
14a8f 78 63 65 70 74 20 66 6f 72 20 6e 52 65 66 20 61 xcept for nRef a
14a90 6e 64 20 70 4e 65 78 74 20 77 68 69 63 68 20 61 nd pNext which a
14a91 72 65 20 61 63 63 65 73 73 65 64 20 75 6e 64 65 re accessed unde
14a92 72 20 74 68 65 0a 2a 2a 20 67 6c 6f 62 61 6c 20 r the.** global
14a93 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
14a94 54 49 43 5f 4d 41 53 54 45 52 20 6d 75 74 65 78 TIC_MASTER mutex
14a95 2e 20 20 54 68 65 20 70 50 61 67 65 72 20 66 69 . The pPager fi
14a96 65 6c 64 0a 2a 2a 20 6d 61 79 20 6e 6f 74 20 62 eld.** may not b
14a97 65 20 6d 6f 64 69 66 69 65 64 20 6f 6e 63 65 20 e modified once
14a98 69 74 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 it is initially
14a99 73 65 74 20 61 73 20 6c 6f 6e 67 20 61 73 20 6e set as long as n
14a9a 52 65 66 3e 30 2e 0a 2a 2a 20 54 68 65 20 70 53 Ref>0..** The pS
14a9b 63 68 65 6d 61 20 66 69 65 6c 64 20 6d 61 79 20 chema field may
14a9c 62 65 20 73 65 74 20 6f 6e 63 65 20 75 6e 64 65 be set once unde
14a9d 72 20 42 74 53 68 61 72 65 64 2e 6d 75 74 65 78 r BtShared.mutex
14a9e 20 61 6e 64 0a 2a 2a 20 74 68 65 72 65 61 66 74 and.** thereaft
14a9f 65 72 20 69 73 20 75 6e 63 68 61 6e 67 65 64 20 er is unchanged
14aa0 61 73 20 6c 6f 6e 67 20 61 73 20 6e 52 65 66 3e as long as nRef>
14aa1 30 2e 0a 2a 2a 0a 2a 2a 20 69 73 50 65 6e 64 69 0..**.** isPendi
14aa2 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 49 66 20 61 ng:.**.** If a
14aa3 20 42 74 53 68 61 72 65 64 20 63 6c 69 65 6e 74 BtShared client
14aa4 20 66 61 69 6c 73 20 74 6f 20 6f 62 74 61 69 6e fails to obtain
14aa5 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e a write-lock on
14aa6 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 a database.**
14aa7 20 74 61 62 6c 65 20 28 62 65 63 61 75 73 65 20 table (because
14aa8 74 68 65 72 65 20 65 78 69 73 74 73 20 6f 6e 65 there exists one
14aa9 20 6f 72 20 6d 6f 72 65 20 72 65 61 64 2d 6c 6f or more read-lo
14aaa 63 6b 73 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 cks on the table
14aab 29 2c 0a 2a 2a 20 20 20 74 68 65 20 73 68 61 72 ),.** the shar
14aac 65 64 2d 63 61 63 68 65 20 65 6e 74 65 72 73 20 ed-cache enters
14aad 27 70 65 6e 64 69 6e 67 2d 6c 6f 63 6b 27 20 73 'pending-lock' s
14aae 74 61 74 65 20 61 6e 64 20 69 73 50 65 6e 64 69 tate and isPendi
14aaf 6e 67 20 69 73 0a 2a 2a 20 20 20 73 65 74 20 74 ng is.** set t
14ab0 6f 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 o true..**.**
14ab1 54 68 65 20 73 68 61 72 65 64 2d 63 61 63 68 65 The shared-cache
14ab2 20 6c 65 61 76 65 73 20 74 68 65 20 27 70 65 6e leaves the 'pen
14ab3 64 69 6e 67 20 6c 6f 63 6b 27 20 73 74 61 74 65 ding lock' state
14ab4 20 77 68 65 6e 20 65 69 74 68 65 72 20 6f 66 0a when either of.
14ab5 2a 2a 20 20 20 74 68 65 20 66 6f 6c 6c 6f 77 69 ** the followi
14ab6 6e 67 20 6f 63 63 75 72 3a 0a 2a 2a 0a 2a 2a 20 ng occur:.**.**
14ab7 20 20 20 20 31 29 20 54 68 65 20 63 75 72 72 65 1) The curre
14ab8 6e 74 20 77 72 69 74 65 72 20 28 42 74 53 68 61 nt writer (BtSha
14ab9 72 65 64 2e 70 57 72 69 74 65 72 29 20 63 6f 6e red.pWriter) con
14aba 63 6c 75 64 65 73 20 69 74 73 20 74 72 61 6e 73 cludes its trans
14abb 61 63 74 69 6f 6e 2c 20 4f 52 0a 2a 2a 20 20 20 action, OR.**
14abc 20 20 32 29 20 54 68 65 20 6e 75 6d 62 65 72 20 2) The number
14abd 6f 66 20 6c 6f 63 6b 73 20 68 65 6c 64 20 62 79 of locks held by
14abe 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f other connectio
14abf 6e 73 20 64 72 6f 70 73 20 74 6f 20 7a 65 72 6f ns drops to zero
14ac0 2e 0a 2a 2a 0a 2a 2a 20 20 20 77 68 69 6c 65 20 ..**.** while
14ac1 69 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67 2d in the 'pending-
14ac2 6c 6f 63 6b 27 20 73 74 61 74 65 2c 20 6e 6f 20 lock' state, no
14ac3 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 61 79 20 73 connection may s
14ac4 74 61 72 74 20 61 20 6e 65 77 0a 2a 2a 20 20 20 tart a new.**
14ac5 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a transaction..**.
14ac6 2a 2a 20 20 20 54 68 69 73 20 66 65 61 74 75 72 ** This featur
14ac7 65 20 69 73 20 69 6e 63 6c 75 64 65 64 20 74 6f e is included to
14ac8 20 68 65 6c 70 20 70 72 65 76 65 6e 74 20 77 72 help prevent wr
14ac9 69 74 65 72 2d 73 74 61 72 76 61 74 69 6f 6e 2e iter-starvation.
14aca 0a 2a 2f 0a 73 74 72 75 63 74 20 42 74 53 68 61 .*/.struct BtSha
14acb 72 65 64 20 7b 0a 20 20 50 61 67 65 72 20 2a 70 red {. Pager *p
14acc 50 61 67 65 72 3b 20 20 20 20 20 20 20 20 2f 2a Pager; /*
14acd 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20 The page cache
14ace 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 */. sqlite3 *db
14acf 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 ; /* Da
14ad0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
14ad1 6e 20 63 75 72 72 65 6e 74 6c 79 20 75 73 69 6e n currently usin
14ad2 67 20 74 68 69 73 20 42 74 72 65 65 20 2a 2f 0a g this Btree */.
14ad3 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 BtCursor *pCur
14ad4 73 6f 72 3b 20 20 20 20 2f 2a 20 41 20 6c 69 73 sor; /* A lis
14ad5 74 20 6f 66 20 61 6c 6c 20 6f 70 65 6e 20 63 75 t of all open cu
14ad6 72 73 6f 72 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 rsors */. MemPa
14ad7 67 65 20 2a 70 50 61 67 65 31 3b 20 20 20 20 20 ge *pPage1;
14ad8 20 2f 2a 20 46 69 72 73 74 20 70 61 67 65 20 6f /* First page o
14ad9 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a f the database *
14ada 2f 0a 20 20 75 38 20 72 65 61 64 4f 6e 6c 79 3b /. u8 readOnly;
14adb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
14adc 65 20 69 66 20 74 68 65 20 75 6e 64 65 72 6c 79 e if the underly
14add 69 6e 67 20 66 69 6c 65 20 69 73 20 72 65 61 64 ing file is read
14ade 6f 6e 6c 79 20 2a 2f 0a 20 20 75 38 20 70 61 67 only */. u8 pag
14adf 65 53 69 7a 65 46 69 78 65 64 3b 20 20 20 20 20 eSizeFixed;
14ae0 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 70 /* True if the p
14ae1 61 67 65 20 73 69 7a 65 20 63 61 6e 20 6e 6f 20 age size can no
14ae2 6c 6f 6e 67 65 72 20 62 65 20 63 68 61 6e 67 65 longer be change
14ae3 64 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c d */.#ifndef SQL
14ae4 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
14ae5 55 55 4d 0a 20 20 75 38 20 61 75 74 6f 56 61 63 UUM. u8 autoVac
14ae6 75 75 6d 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 uum; /* T
14ae7 72 75 65 20 69 66 20 61 75 74 6f 2d 76 61 63 75 rue if auto-vacu
14ae8 75 6d 20 69 73 20 65 6e 61 62 6c 65 64 20 2a 2f um is enabled */
14ae9 0a 20 20 75 38 20 69 6e 63 72 56 61 63 75 75 6d . u8 incrVacuum
14aea 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 ; /* True
14aeb 20 69 66 20 69 6e 63 72 2d 76 61 63 75 75 6d 20 if incr-vacuum
14aec 69 73 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 23 65 is enabled */.#e
14aed 6e 64 69 66 0a 20 20 75 31 36 20 70 61 67 65 53 ndif. u16 pageS
14aee 69 7a 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 ize; /*
14aef 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 Total number of
14af0 62 79 74 65 73 20 6f 6e 20 61 20 70 61 67 65 20 bytes on a page
14af1 2a 2f 0a 20 20 75 31 36 20 75 73 61 62 6c 65 53 */. u16 usableS
14af2 69 7a 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 ize; /* Nu
14af3 6d 62 65 72 20 6f 66 20 75 73 61 62 6c 65 20 62 mber of usable b
14af4 79 74 65 73 20 6f 6e 20 65 61 63 68 20 70 61 67 ytes on each pag
14af5 65 20 2a 2f 0a 20 20 75 31 36 20 6d 61 78 4c 6f e */. u16 maxLo
14af6 63 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 cal; /*
14af7 4d 61 78 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 Maximum local pa
14af8 79 6c 6f 61 64 20 69 6e 20 6e 6f 6e 2d 4c 45 41 yload in non-LEA
14af9 46 44 41 54 41 20 74 61 62 6c 65 73 20 2a 2f 0a FDATA tables */.
14afa 20 20 75 31 36 20 6d 69 6e 4c 6f 63 61 6c 3b 20 u16 minLocal;
14afb 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d /* Minim
14afc 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 um local payload
14afd 20 69 6e 20 6e 6f 6e 2d 4c 45 41 46 44 41 54 41 in non-LEAFDATA
14afe 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 75 31 36 tables */. u16
14aff 20 6d 61 78 4c 65 61 66 3b 20 20 20 20 20 20 20 maxLeaf;
14b00 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 6f /* Maximum lo
14b01 63 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e 20 61 cal payload in a
14b02 20 4c 45 41 46 44 41 54 41 20 74 61 62 6c 65 20 LEAFDATA table
14b03 2a 2f 0a 20 20 75 31 36 20 6d 69 6e 4c 65 61 66 */. u16 minLeaf
14b04 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 ; /* Mi
14b05 6e 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c nimum local payl
14b06 6f 61 64 20 69 6e 20 61 20 4c 45 41 46 44 41 54 oad in a LEAFDAT
14b07 41 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75 38 20 A table */. u8
14b08 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3b 20 20 inTransaction;
14b09 20 20 20 2f 2a 20 54 72 61 6e 73 61 63 74 69 6f /* Transactio
14b0a 6e 20 73 74 61 74 65 20 2a 2f 0a 20 20 69 6e 74 n state */. int
14b0b 20 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3b 20 20 nTransaction;
14b0c 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
14b0d 6f 70 65 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e open transaction
14b0e 73 20 28 72 65 61 64 20 2b 20 77 72 69 74 65 29 s (read + write)
14b0f 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 63 68 */. void *pSch
14b10 65 6d 61 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 ema; /* P
14b11 6f 69 6e 74 65 72 20 74 6f 20 73 70 61 63 65 20 ointer to space
14b12 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 73 71 6c allocated by sql
14b13 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 28 ite3BtreeSchema(
14b14 29 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46 ) */. void (*xF
14b15 72 65 65 53 63 68 65 6d 61 29 28 76 6f 69 64 2a reeSchema)(void*
14b16 29 3b 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f ); /* Destructo
14b17 72 20 66 6f 72 20 42 74 53 68 61 72 65 64 2e 70 r for BtShared.p
14b18 53 63 68 65 6d 61 20 2a 2f 0a 20 20 73 71 6c 69 Schema */. sqli
14b19 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 te3_mutex *mutex
14b1a 3b 20 2f 2a 20 4e 6f 6e 2d 72 65 63 75 72 73 69 ; /* Non-recursi
14b1b 76 65 20 6d 75 74 65 78 20 72 65 71 75 69 72 65 ve mutex require
14b1c 64 20 74 6f 20 61 63 63 65 73 73 20 74 68 69 73 d to access this
14b1d 20 73 74 72 75 63 74 20 2a 2f 0a 20 20 42 69 74 struct */. Bit
14b1e 76 65 63 20 2a 70 48 61 73 43 6f 6e 74 65 6e 74 vec *pHasContent
14b1f 3b 20 20 2f 2a 20 53 65 74 20 6f 66 20 70 61 67 ; /* Set of pag
14b20 65 73 20 6d 6f 76 65 64 20 74 6f 20 66 72 65 65 es moved to free
14b21 2d 6c 69 73 74 20 74 68 69 73 20 74 72 61 6e 73 -list this trans
14b22 61 63 74 69 6f 6e 20 2a 2f 0a 23 69 66 6e 64 65 action */.#ifnde
14b23 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 f SQLITE_OMIT_SH
14b24 41 52 45 44 5f 43 41 43 48 45 0a 20 20 69 6e 74 ARED_CACHE. int
14b25 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 nRef;
14b26 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
14b27 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 references to th
14b28 69 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a is structure */.
14b29 20 20 42 74 53 68 61 72 65 64 20 2a 70 4e 65 78 BtShared *pNex
14b2a 74 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 t; /* Next
14b2b 6f 6e 20 61 20 6c 69 73 74 20 6f 66 20 73 68 61 on a list of sha
14b2c 72 61 62 6c 65 20 42 74 53 68 61 72 65 64 20 73 rable BtShared s
14b2d 74 72 75 63 74 73 20 2a 2f 0a 20 20 42 74 4c 6f tructs */. BtLo
14b2e 63 6b 20 2a 70 4c 6f 63 6b 3b 20 20 20 20 20 20 ck *pLock;
14b2f 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 6c 6f 63 /* List of loc
14b30 6b 73 20 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 ks held on this
14b31 73 68 61 72 65 64 2d 62 74 72 65 65 20 73 74 72 shared-btree str
14b32 75 63 74 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a uct */. Btree *
14b33 70 57 72 69 74 65 72 3b 20 20 20 20 20 20 20 2f pWriter; /
14b34 2a 20 42 74 72 65 65 20 77 69 74 68 20 63 75 72 * Btree with cur
14b35 72 65 6e 74 6c 79 20 6f 70 65 6e 20 77 72 69 74 rently open writ
14b36 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f e transaction */
14b37 0a 20 20 75 38 20 69 73 45 78 63 6c 75 73 69 76 . u8 isExclusiv
14b38 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 e; /* True
14b39 20 69 66 20 70 57 72 69 74 65 72 20 68 61 73 20 if pWriter has
14b3a 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 an EXCLUSIVE loc
14b3b 6b 20 6f 6e 20 74 68 65 20 64 62 20 2a 2f 0a 20 k on the db */.
14b3c 20 75 38 20 69 73 50 65 6e 64 69 6e 67 3b 20 20 u8 isPending;
14b3d 20 20 20 20 20 20 20 2f 2a 20 49 66 20 77 61 69 /* If wai
14b3e 74 69 6e 67 20 66 6f 72 20 72 65 61 64 2d 6c 6f ting for read-lo
14b3f 63 6b 73 20 74 6f 20 63 6c 65 61 72 20 2a 2f 0a cks to clear */.
14b40 23 65 6e 64 69 66 0a 20 20 75 38 20 2a 70 54 6d #endif. u8 *pTm
14b41 70 53 70 61 63 65 3b 20 20 20 20 20 20 20 20 2f pSpace; /
14b42 2a 20 42 74 53 68 61 72 65 64 2e 70 61 67 65 53 * BtShared.pageS
14b43 69 7a 65 20 62 79 74 65 73 20 6f 66 20 73 70 61 ize bytes of spa
14b44 63 65 20 66 6f 72 20 74 6d 70 20 75 73 65 20 2a ce for tmp use *
14b45 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 /.};../*.** An i
14b46 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
14b47 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
14b48 72 65 20 69 73 20 75 73 65 64 20 74 6f 20 68 6f re is used to ho
14b49 6c 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a ld information.*
14b4a 2a 20 61 62 6f 75 74 20 61 20 63 65 6c 6c 2e 20 * about a cell.
14b4b 20 54 68 65 20 70 61 72 73 65 43 65 6c 6c 50 74 The parseCellPt
14b4c 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 69 6c r() function fil
14b4d 6c 73 20 69 6e 20 74 68 69 73 20 73 74 72 75 63 ls in this struc
14b4e 74 75 72 65 0a 2a 2a 20 62 61 73 65 64 20 6f 6e ture.** based on
14b4f 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 65 78 74 information ext
14b50 72 61 63 74 20 66 72 6f 6d 20 74 68 65 20 72 61 ract from the ra
14b51 77 20 64 69 73 6b 20 70 61 67 65 2e 0a 2a 2f 0a w disk page..*/.
14b52 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 43 typedef struct C
14b53 65 6c 6c 49 6e 66 6f 20 43 65 6c 6c 49 6e 66 6f ellInfo CellInfo
14b54 3b 0a 73 74 72 75 63 74 20 43 65 6c 6c 49 6e 66 ;.struct CellInf
14b55 6f 20 7b 0a 20 20 75 38 20 2a 70 43 65 6c 6c 3b o {. u8 *pCell;
14b56 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
14b57 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 to the start of
14b58 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a cell content */.
14b59 20 20 69 36 34 20 6e 4b 65 79 3b 20 20 20 20 20 i64 nKey;
14b5a 20 2f 2a 20 54 68 65 20 6b 65 79 20 66 6f 72 20 /* The key for
14b5b 49 4e 54 4b 45 59 20 74 61 62 6c 65 73 2c 20 6f INTKEY tables, o
14b5c 72 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 r number of byte
14b5d 73 20 69 6e 20 6b 65 79 20 2a 2f 0a 20 20 75 33 s in key */. u3
14b5e 32 20 6e 44 61 74 61 3b 20 20 20 20 20 2f 2a 20 2 nData; /*
14b5f 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
14b60 6f 66 20 64 61 74 61 20 2a 2f 0a 20 20 75 33 32 of data */. u32
14b61 20 6e 50 61 79 6c 6f 61 64 3b 20 20 2f 2a 20 54 nPayload; /* T
14b62 6f 74 61 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 70 otal amount of p
14b63 61 79 6c 6f 61 64 20 2a 2f 0a 20 20 75 31 36 20 ayload */. u16
14b64 6e 48 65 61 64 65 72 3b 20 20 20 2f 2a 20 53 69 nHeader; /* Si
14b65 7a 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 ze of the cell c
14b66 6f 6e 74 65 6e 74 20 68 65 61 64 65 72 20 69 6e ontent header in
14b67 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 31 36 20 bytes */. u16
14b68 6e 4c 6f 63 61 6c 3b 20 20 20 20 2f 2a 20 41 6d nLocal; /* Am
14b69 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 ount of payload
14b6a 68 65 6c 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a held locally */.
14b6b 20 20 75 31 36 20 69 4f 76 65 72 66 6c 6f 77 3b u16 iOverflow;
14b6c 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 6f 76 /* Offset to ov
14b6d 65 72 66 6c 6f 77 20 70 61 67 65 20 6e 75 6d 62 erflow page numb
14b6e 65 72 2e 20 20 5a 65 72 6f 20 69 66 20 6e 6f 20 er. Zero if no
14b6f 6f 76 65 72 66 6c 6f 77 20 2a 2f 0a 20 20 75 31 overflow */. u1
14b70 36 20 6e 53 69 7a 65 3b 20 20 20 20 20 2f 2a 20 6 nSize; /*
14b71 53 69 7a 65 20 6f 66 20 74 68 65 20 63 65 6c 6c Size of the cell
14b72 20 63 6f 6e 74 65 6e 74 20 6f 6e 20 74 68 65 20 content on the
14b73 6d 61 69 6e 20 62 2d 74 72 65 65 20 70 61 67 65 main b-tree page
14b74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 */.};../*.** Ma
14b75 78 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 61 ximum depth of a
14b76 6e 20 53 51 4c 69 74 65 20 42 2d 54 72 65 65 20 n SQLite B-Tree
14b77 73 74 72 75 63 74 75 72 65 2e 20 41 6e 79 20 42 structure. Any B
14b78 2d 54 72 65 65 20 64 65 65 70 65 72 20 74 68 61 -Tree deeper tha
14b79 6e 0a 2a 2a 20 74 68 69 73 20 77 69 6c 6c 20 62 n.** this will b
14b7a 65 20 64 65 63 6c 61 72 65 64 20 63 6f 72 72 75 e declared corru
14b7b 70 74 2e 20 54 68 69 73 20 76 61 6c 75 65 20 69 pt. This value i
14b7c 73 20 63 61 6c 63 75 6c 61 74 65 64 20 62 61 73 s calculated bas
14b7d 65 64 20 6f 6e 20 61 0a 2a 2a 20 6d 61 78 69 6d ed on a.** maxim
14b7e 75 6d 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 um database size
14b7f 20 6f 66 20 32 5e 33 31 20 70 61 67 65 73 20 61 of 2^31 pages a
14b80 20 6d 69 6e 69 6d 75 6d 20 66 61 6e 6f 75 74 20 minimum fanout
14b81 6f 66 20 32 20 66 6f 72 20 61 0a 2a 2a 20 72 6f of 2 for a.** ro
14b82 6f 74 2d 6e 6f 64 65 20 61 6e 64 20 33 20 66 6f ot-node and 3 fo
14b83 72 20 61 6c 6c 20 6f 74 68 65 72 20 69 6e 74 65 r all other inte
14b84 72 6e 61 6c 20 6e 6f 64 65 73 2e 0a 2a 2a 0a 2a rnal nodes..**.*
14b85 2a 20 49 66 20 61 20 74 72 65 65 20 74 68 61 74 * If a tree that
14b86 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 20 74 appears to be t
14b87 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 69 73 20 aller than this
14b88 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20 is encountered,
14b89 69 74 20 69 73 0a 2a 2a 20 61 73 73 75 6d 65 64 it is.** assumed
14b8a 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62 61 that the databa
14b8b 73 65 20 69 73 20 63 6f 72 72 75 70 74 2e 0a 2a se is corrupt..*
14b8c 2f 0a 23 64 65 66 69 6e 65 20 42 54 43 55 52 53 /.#define BTCURS
14b8d 4f 52 5f 4d 41 58 5f 44 45 50 54 48 20 32 30 0a OR_MAX_DEPTH 20.
14b8e 0a 2f 2a 0a 2a 2a 20 41 20 63 75 72 73 6f 72 20 ./*.** A cursor
14b8f 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
14b90 61 20 70 61 72 74 69 63 75 6c 61 72 20 65 6e 74 a particular ent
14b91 72 79 20 77 69 74 68 69 6e 20 61 20 70 61 72 74 ry within a part
14b92 69 63 75 6c 61 72 0a 2a 2a 20 62 2d 74 72 65 65 icular.** b-tree
14b93 20 77 69 74 68 69 6e 20 61 20 64 61 74 61 62 61 within a databa
14b94 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 se file..**.** T
14b95 68 65 20 65 6e 74 72 79 20 69 73 20 69 64 65 6e he entry is iden
14b96 74 69 66 69 65 64 20 62 79 20 69 74 73 20 4d 65 tified by its Me
14b97 6d 50 61 67 65 20 61 6e 64 20 74 68 65 20 69 6e mPage and the in
14b98 64 65 78 20 69 6e 0a 2a 2a 20 4d 65 6d 50 61 67 dex in.** MemPag
14b99 65 2e 61 43 65 6c 6c 5b 5d 20 6f 66 20 74 68 65 e.aCell[] of the
14b9a 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 41 20 entry..**.** A
14b9b 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 single database
14b9c 66 69 6c 65 20 63 61 6e 20 73 68 61 72 65 64 20 file can shared
14b9d 62 79 20 74 77 6f 20 6d 6f 72 65 20 64 61 74 61 by two more data
14b9e 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 base connections
14b9f 2c 0a 2a 2a 20 62 75 74 20 63 75 72 73 6f 72 73 ,.** but cursors
14ba0 20 63 61 6e 6e 6f 74 20 62 65 20 73 68 61 72 65 cannot be share
14ba1 64 2e 20 20 45 61 63 68 20 63 75 72 73 6f 72 20 d. Each cursor
14ba2 69 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 is associated wi
14ba3 74 68 20 61 0a 2a 2a 20 70 61 72 74 69 63 75 6c th a.** particul
14ba4 61 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e ar database conn
14ba5 65 63 74 69 6f 6e 20 69 64 65 6e 74 69 66 69 65 ection identifie
14ba6 64 20 42 74 43 75 72 73 6f 72 2e 70 42 74 72 65 d BtCursor.pBtre
14ba7 65 2e 64 62 2e 0a 2a 2a 0a 2a 2a 20 46 69 65 6c e.db..**.** Fiel
14ba8 64 73 20 69 6e 20 74 68 69 73 20 73 74 72 75 63 ds in this struc
14ba9 74 75 72 65 20 61 72 65 20 61 63 63 65 73 73 65 ture are accesse
14baa 64 20 75 6e 64 65 72 20 74 68 65 20 42 74 53 68 d under the BtSh
14bab 61 72 65 64 2e 6d 75 74 65 78 0a 2a 2a 20 66 6f ared.mutex.** fo
14bac 75 6e 64 20 61 74 20 73 65 6c 66 2d 3e 70 42 74 und at self->pBt
14bad 2d 3e 6d 75 74 65 78 2e 20 0a 2a 2f 0a 73 74 72 ->mutex. .*/.str
14bae 75 63 74 20 42 74 43 75 72 73 6f 72 20 7b 0a 20 uct BtCursor {.
14baf 20 42 74 72 65 65 20 2a 70 42 74 72 65 65 3b 20 Btree *pBtree;
14bb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
14bb1 65 20 42 74 72 65 65 20 74 6f 20 77 68 69 63 68 e Btree to which
14bb2 20 74 68 69 73 20 63 75 72 73 6f 72 20 62 65 6c this cursor bel
14bb3 6f 6e 67 73 20 2a 2f 0a 20 20 42 74 53 68 61 72 ongs */. BtShar
14bb4 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20 ed *pBt;
14bb5 20 20 20 20 2f 2a 20 54 68 65 20 42 74 53 68 61 /* The BtSha
14bb6 72 65 64 20 74 68 69 73 20 63 75 72 73 6f 72 20 red this cursor
14bb7 70 6f 69 6e 74 73 20 74 6f 20 2a 2f 0a 20 20 42 points to */. B
14bb8 74 43 75 72 73 6f 72 20 2a 70 4e 65 78 74 2c 20 tCursor *pNext,
14bb9 2a 70 50 72 65 76 3b 20 20 2f 2a 20 46 6f 72 6d *pPrev; /* Form
14bba 73 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 s a linked list
14bbb 6f 66 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 2a of all cursors *
14bbc 2f 0a 20 20 73 74 72 75 63 74 20 4b 65 79 49 6e /. struct KeyIn
14bbd 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20 2f 2a fo *pKeyInfo; /*
14bbe 20 41 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64 Argument passed
14bbf 20 74 6f 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 to comparison f
14bc0 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 50 67 6e unction */. Pgn
14bc1 6f 20 70 67 6e 6f 52 6f 6f 74 3b 20 20 20 20 20 o pgnoRoot;
14bc2 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 6f /* The ro
14bc3 6f 74 20 70 61 67 65 20 6f 66 20 74 68 69 73 20 ot page of this
14bc4 74 72 65 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 tree */. sqlite
14bc5 33 5f 69 6e 74 36 34 20 63 61 63 68 65 64 52 6f 3_int64 cachedRo
14bc6 77 69 64 3b 20 2f 2a 20 4e 65 78 74 20 72 6f 77 wid; /* Next row
14bc7 69 64 20 63 61 63 68 65 2e 20 20 30 20 6d 65 61 id cache. 0 mea
14bc8 6e 73 20 6e 6f 74 20 76 61 6c 69 64 20 2a 2f 0a ns not valid */.
14bc9 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b CellInfo info;
14bca 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
14bcb 20 70 61 72 73 65 20 6f 66 20 74 68 65 20 63 65 parse of the ce
14bcc 6c 6c 20 77 65 20 61 72 65 20 70 6f 69 6e 74 69 ll we are pointi
14bcd 6e 67 20 61 74 20 2a 2f 0a 20 20 75 38 20 77 72 ng at */. u8 wr
14bce 46 6c 61 67 3b 20 20 20 20 20 20 20 20 20 20 20 Flag;
14bcf 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
14bd0 77 72 69 74 61 62 6c 65 20 2a 2f 0a 20 20 75 38 writable */. u8
14bd1 20 61 74 4c 61 73 74 3b 20 20 20 20 20 20 20 20 atLast;
14bd2 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f /* Curso
14bd3 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 r pointing to th
14bd4 65 20 6c 61 73 74 20 65 6e 74 72 79 20 2a 2f 0a e last entry */.
14bd5 20 20 75 38 20 76 61 6c 69 64 4e 4b 65 79 3b 20 u8 validNKey;
14bd6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
14bd7 72 75 65 20 69 66 20 69 6e 66 6f 2e 6e 4b 65 79 rue if info.nKey
14bd8 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20 75 is valid */. u
14bd9 38 20 65 53 74 61 74 65 3b 20 20 20 20 20 20 20 8 eState;
14bda 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 /* One
14bdb 6f 66 20 74 68 65 20 43 55 52 53 4f 52 5f 58 58 of the CURSOR_XX
14bdc 58 20 63 6f 6e 73 74 61 6e 74 73 20 28 73 65 65 X constants (see
14bdd 20 62 65 6c 6f 77 29 20 2a 2f 0a 20 20 76 6f 69 below) */. voi
14bde 64 20 2a 70 4b 65 79 3b 20 20 20 20 20 20 2f 2a d *pKey; /*
14bdf 20 53 61 76 65 64 20 6b 65 79 20 74 68 61 74 20 Saved key that
14be0 77 61 73 20 63 75 72 73 6f 72 27 73 20 6c 61 73 was cursor's las
14be1 74 20 6b 6e 6f 77 6e 20 70 6f 73 69 74 69 6f 6e t known position
14be2 20 2a 2f 0a 20 20 69 36 34 20 6e 4b 65 79 3b 20 */. i64 nKey;
14be3 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
14be4 66 20 70 4b 65 79 2c 20 6f 72 20 6c 61 73 74 20 f pKey, or last
14be5 69 6e 74 65 67 65 72 20 6b 65 79 20 2a 2f 0a 20 integer key */.
14be6 20 69 6e 74 20 73 6b 69 70 4e 65 78 74 3b 20 20 int skipNext;
14be7 20 20 2f 2a 20 50 72 65 76 28 29 20 69 73 20 6e /* Prev() is n
14be8 6f 6f 70 20 69 66 20 6e 65 67 61 74 69 76 65 2e oop if negative.
14be9 20 4e 65 78 74 28 29 20 69 73 20 6e 6f 6f 70 20 Next() is noop
14bea 69 66 20 70 6f 73 69 74 69 76 65 20 2a 2f 0a 23 if positive */.#
14beb 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
14bec 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 75 38 IT_INCRBLOB. u8
14bed 20 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c isIncrblobHandl
14bee 65 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 e; /* True
14bef 69 66 20 74 68 69 73 20 63 75 72 73 6f 72 20 69 if this cursor i
14bf0 73 20 61 6e 20 69 6e 63 72 2e 20 69 6f 20 68 61 s an incr. io ha
14bf1 6e 64 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 2a ndle */. Pgno *
14bf2 61 4f 76 65 72 66 6c 6f 77 3b 20 20 20 20 20 20 aOverflow;
14bf3 20 20 20 20 2f 2a 20 43 61 63 68 65 20 6f 66 20 /* Cache of
14bf4 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6c 6f overflow page lo
14bf5 63 61 74 69 6f 6e 73 20 2a 2f 0a 23 65 6e 64 69 cations */.#endi
14bf6 66 0a 20 20 69 31 36 20 69 50 61 67 65 3b 20 20 f. i16 iPage;
14bf7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14bf8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 /* Ind
14bf9 65 78 20 6f 66 20 63 75 72 72 65 6e 74 20 70 61 ex of current pa
14bfa 67 65 20 69 6e 20 61 70 50 61 67 65 20 2a 2f 0a ge in apPage */.
14bfb 20 20 4d 65 6d 50 61 67 65 20 2a 61 70 50 61 67 MemPage *apPag
14bfc 65 5b 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 e[BTCURSOR_MAX_D
14bfd 45 50 54 48 5d 3b 20 20 2f 2a 20 50 61 67 65 73 EPTH]; /* Pages
14bfe 20 66 72 6f 6d 20 72 6f 6f 74 20 74 6f 20 63 75 from root to cu
14bff 72 72 65 6e 74 20 70 61 67 65 20 2a 2f 0a 20 20 rrent page */.
14c00 75 31 36 20 61 69 49 64 78 5b 42 54 43 55 52 53 u16 aiIdx[BTCURS
14c01 4f 52 5f 4d 41 58 5f 44 45 50 54 48 5d 3b 20 20 OR_MAX_DEPTH];
14c02 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 /* Current
14c03 20 69 6e 64 65 78 20 69 6e 20 61 70 50 61 67 65 index in apPage
14c04 5b 69 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a [i] */.};../*.**
14c05 20 50 6f 74 65 6e 74 69 61 6c 20 76 61 6c 75 65 Potential value
14c06 73 20 66 6f 72 20 42 74 43 75 72 73 6f 72 2e 65 s for BtCursor.e
14c07 53 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 State..**.** CUR
14c08 53 4f 52 5f 56 41 4c 49 44 3a 0a 2a 2a 20 20 20 SOR_VALID:.**
14c09 43 75 72 73 6f 72 20 70 6f 69 6e 74 73 20 74 6f Cursor points to
14c0a 20 61 20 76 61 6c 69 64 20 65 6e 74 72 79 2e 20 a valid entry.
14c0b 67 65 74 50 61 79 6c 6f 61 64 28 29 20 65 74 63 getPayload() etc
14c0c 2e 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 2e . may be called.
14c0d 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f 49 4e .**.** CURSOR_IN
14c0e 56 41 4c 49 44 3a 0a 2a 2a 20 20 20 43 75 72 73 VALID:.** Curs
14c0f 6f 72 20 64 6f 65 73 20 6e 6f 74 20 70 6f 69 6e or does not poin
14c10 74 20 74 6f 20 61 20 76 61 6c 69 64 20 65 6e 74 t to a valid ent
14c11 72 79 2e 20 54 68 69 73 20 63 61 6e 20 68 61 70 ry. This can hap
14c12 70 65 6e 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 pen (for example
14c13 29 20 0a 2a 2a 20 20 20 62 65 63 61 75 73 65 20 ) .** because
14c14 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 the table is emp
14c15 74 79 20 6f 72 20 62 65 63 61 75 73 65 20 42 74 ty or because Bt
14c16 72 65 65 43 75 72 73 6f 72 46 69 72 73 74 28 29 reeCursorFirst()
14c17 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 0a 2a 2a has not been.**
14c18 20 20 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a called..**.**
14c19 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 CURSOR_REQUIRES
14c1a 45 45 4b 3a 0a 2a 2a 20 20 20 54 68 65 20 74 61 EEK:.** The ta
14c1b 62 6c 65 20 74 68 61 74 20 74 68 69 73 20 63 75 ble that this cu
14c1c 72 73 6f 72 20 77 61 73 20 6f 70 65 6e 65 64 20 rsor was opened
14c1d 6f 6e 20 73 74 69 6c 6c 20 65 78 69 73 74 73 2c on still exists,
14c1e 20 62 75 74 20 68 61 73 20 62 65 65 6e 20 0a 2a but has been .*
14c1f 2a 20 20 20 6d 6f 64 69 66 69 65 64 20 73 69 6e * modified sin
14c20 63 65 20 74 68 65 20 63 75 72 73 6f 72 20 77 61 ce the cursor wa
14c21 73 20 6c 61 73 74 20 75 73 65 64 2e 20 54 68 65 s last used. The
14c22 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e cursor position
14c23 20 69 73 20 73 61 76 65 64 0a 2a 2a 20 20 20 69 is saved.** i
14c24 6e 20 76 61 72 69 61 62 6c 65 73 20 42 74 43 75 n variables BtCu
14c25 72 73 6f 72 2e 70 4b 65 79 20 61 6e 64 20 42 74 rsor.pKey and Bt
14c26 43 75 72 73 6f 72 2e 6e 4b 65 79 2e 20 57 68 65 Cursor.nKey. Whe
14c27 6e 20 61 20 63 75 72 73 6f 72 20 69 73 20 69 6e n a cursor is in
14c28 20 0a 2a 2a 20 20 20 74 68 69 73 20 73 74 61 74 .** this stat
14c29 65 2c 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 e, restoreCursor
14c2a 50 6f 73 69 74 69 6f 6e 28 29 20 63 61 6e 20 62 Position() can b
14c2b 65 20 63 61 6c 6c 65 64 20 74 6f 20 61 74 74 65 e called to atte
14c2c 6d 70 74 20 74 6f 0a 2a 2a 20 20 20 73 65 65 6b mpt to.** seek
14c2d 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 the cursor to t
14c2e 68 65 20 73 61 76 65 64 20 70 6f 73 69 74 69 6f he saved positio
14c2f 6e 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f n..**.** CURSOR_
14c30 46 41 55 4c 54 3a 0a 2a 2a 20 20 20 41 20 75 6e FAULT:.** A un
14c31 72 65 63 6f 76 65 72 61 62 6c 65 20 65 72 72 6f recoverable erro
14c32 72 20 28 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 r (an I/O error
14c33 6f 72 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c or a malloc fail
14c34 75 72 65 29 20 68 61 73 20 6f 63 63 75 72 72 65 ure) has occurre
14c35 64 0a 2a 2a 20 20 20 6f 6e 20 61 20 64 69 66 66 d.** on a diff
14c36 65 72 65 6e 74 20 63 6f 6e 6e 65 63 74 69 6f 6e erent connection
14c37 20 74 68 61 74 20 73 68 61 72 65 73 20 74 68 65 that shares the
14c38 20 42 74 53 68 61 72 65 64 20 63 61 63 68 65 20 BtShared cache
14c39 77 69 74 68 20 74 68 69 73 0a 2a 2a 20 20 20 63 with this.** c
14c3a 75 72 73 6f 72 2e 20 20 54 68 65 20 65 72 72 6f ursor. The erro
14c3b 72 20 68 61 73 20 6c 65 66 74 20 74 68 65 20 63 r has left the c
14c3c 61 63 68 65 20 69 6e 20 61 6e 20 69 6e 63 6f 6e ache in an incon
14c3d 73 69 73 74 65 6e 74 20 73 74 61 74 65 2e 0a 2a sistent state..*
14c3e 2a 20 20 20 44 6f 20 6e 6f 74 68 69 6e 67 20 65 * Do nothing e
14c3f 6c 73 65 20 77 69 74 68 20 74 68 69 73 20 63 75 lse with this cu
14c40 72 73 6f 72 2e 20 20 41 6e 79 20 61 74 74 65 6d rsor. Any attem
14c41 70 74 20 74 6f 20 75 73 65 20 74 68 65 20 63 75 pt to use the cu
14c42 72 73 6f 72 0a 2a 2a 20 20 20 73 68 6f 75 6c 64 rsor.** should
14c43 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f return the erro
14c44 72 20 63 6f 64 65 20 73 74 6f 72 65 64 20 69 6e r code stored in
14c45 20 42 74 43 75 72 73 6f 72 2e 73 6b 69 70 0a 2a BtCursor.skip.*
14c46 2f 0a 23 64 65 66 69 6e 65 20 43 55 52 53 4f 52 /.#define CURSOR
14c47 5f 49 4e 56 41 4c 49 44 20 20 20 20 20 20 20 20 _INVALID
14c48 20 20 20 30 0a 23 64 65 66 69 6e 65 20 43 55 52 0.#define CUR
14c49 53 4f 52 5f 56 41 4c 49 44 20 20 20 20 20 20 20 SOR_VALID
14c4a 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 1.#define
14c4b 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 CURSOR_REQUIRESE
14c4c 45 4b 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 EK 2.#defi
14c4d 6e 65 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 ne CURSOR_FAULT
14c4e 20 20 20 20 20 20 20 20 20 20 20 20 33 0a 0a 2f 3../
14c4f 2a 20 0a 2a 2a 20 54 68 65 20 64 61 74 61 62 61 * .** The databa
14c50 73 65 20 70 61 67 65 20 74 68 65 20 50 45 4e 44 se page the PEND
14c51 49 4e 47 5f 42 59 54 45 20 6f 63 63 75 70 69 65 ING_BYTE occupie
14c52 73 2e 20 54 68 69 73 20 70 61 67 65 20 69 73 20 s. This page is
14c53 6e 65 76 65 72 20 75 73 65 64 2e 0a 2a 2f 0a 23 never used..*/.#
14c54 20 64 65 66 69 6e 65 20 50 45 4e 44 49 4e 47 5f define PENDING_
14c55 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 50 BYTE_PAGE(pBt) P
14c56 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 42 74 AGER_MJ_PGNO(pBt
14c57 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 6d )../*.** These m
14c58 61 63 72 6f 73 20 64 65 66 69 6e 65 20 74 68 65 acros define the
14c59 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 65 location of the
14c5a 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 pointer-map ent
14c5b 72 79 20 66 6f 72 20 61 20 0a 2a 2a 20 64 61 74 ry for a .** dat
14c5c 61 62 61 73 65 20 70 61 67 65 2e 20 54 68 65 20 abase page. The
14c5d 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 first argument t
14c5e 6f 20 65 61 63 68 20 69 73 20 74 68 65 20 6e 75 o each is the nu
14c5f 6d 62 65 72 20 6f 66 20 75 73 61 62 6c 65 0a 2a mber of usable.*
14c60 2a 20 62 79 74 65 73 20 6f 6e 20 65 61 63 68 20 * bytes on each
14c61 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 page of the data
14c62 62 61 73 65 20 28 6f 66 74 65 6e 20 31 30 32 34 base (often 1024
14c63 29 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 69 73 ). The second is
14c64 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 6e 75 6d the.** page num
14c65 62 65 72 20 74 6f 20 6c 6f 6f 6b 20 75 70 20 69 ber to look up i
14c66 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 n the pointer ma
14c67 70 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f p..**.** PTRMAP_
14c68 50 41 47 45 4e 4f 20 72 65 74 75 72 6e 73 20 74 PAGENO returns t
14c69 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 he database page
14c6a 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 number of the p
14c6b 6f 69 6e 74 65 72 2d 6d 61 70 0a 2a 2a 20 70 61 ointer-map.** pa
14c6c 67 65 20 74 68 61 74 20 73 74 6f 72 65 73 20 74 ge that stores t
14c6d 68 65 20 72 65 71 75 69 72 65 64 20 70 6f 69 6e he required poin
14c6e 74 65 72 2e 20 50 54 52 4d 41 50 5f 50 54 52 4f ter. PTRMAP_PTRO
14c6f 46 46 53 45 54 20 72 65 74 75 72 6e 73 0a 2a 2a FFSET returns.**
14c70 20 74 68 65 20 6f 66 66 73 65 74 20 6f 66 20 74 the offset of t
14c71 68 65 20 72 65 71 75 65 73 74 65 64 20 6d 61 70 he requested map
14c72 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 entry..**.** If
14c73 20 74 68 65 20 70 67 6e 6f 20 61 72 67 75 6d 65 the pgno argume
14c74 6e 74 20 70 61 73 73 65 64 20 74 6f 20 50 54 52 nt passed to PTR
14c75 4d 41 50 5f 50 41 47 45 4e 4f 20 69 73 20 61 20 MAP_PAGENO is a
14c76 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 pointer-map page
14c77 2c 0a 2a 2a 20 74 68 65 6e 20 70 67 6e 6f 20 69 ,.** then pgno i
14c78 73 20 72 65 74 75 72 6e 65 64 2e 20 53 6f 20 28 s returned. So (
14c79 70 67 6e 6f 3d 3d 50 54 52 4d 41 50 5f 50 41 47 pgno==PTRMAP_PAG
14c7a 45 4e 4f 28 70 67 73 7a 2c 20 70 67 6e 6f 29 29 ENO(pgsz, pgno))
14c7b 20 63 61 6e 20 62 65 0a 2a 2a 20 75 73 65 64 20 can be.** used
14c7c 74 6f 20 74 65 73 74 20 69 66 20 70 67 6e 6f 20 to test if pgno
14c7d 69 73 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 is a pointer-map
14c7e 20 70 61 67 65 2e 20 50 54 52 4d 41 50 5f 49 53 page. PTRMAP_IS
14c7f 50 41 47 45 20 69 6d 70 6c 65 6d 65 6e 74 73 0a PAGE implements.
14c80 2a 2a 20 74 68 69 73 20 74 65 73 74 2e 0a 2a 2f ** this test..*/
14c81 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f .#define PTRMAP_
14c82 50 41 47 45 4e 4f 28 70 42 74 2c 20 70 67 6e 6f PAGENO(pBt, pgno
14c83 29 20 70 74 72 6d 61 70 50 61 67 65 6e 6f 28 70 ) ptrmapPageno(p
14c84 42 74 2c 20 70 67 6e 6f 29 0a 23 64 65 66 69 6e Bt, pgno).#defin
14c85 65 20 50 54 52 4d 41 50 5f 50 54 52 4f 46 46 53 e PTRMAP_PTROFFS
14c86 45 54 28 70 67 70 74 72 6d 61 70 2c 20 70 67 6e ET(pgptrmap, pgn
14c87 6f 29 20 28 35 2a 28 70 67 6e 6f 2d 70 67 70 74 o) (5*(pgno-pgpt
14c88 72 6d 61 70 2d 31 29 29 0a 23 64 65 66 69 6e 65 rmap-1)).#define
14c89 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 PTRMAP_ISPAGE(p
14c8a 42 74 2c 20 70 67 6e 6f 29 20 28 50 54 52 4d 41 Bt, pgno) (PTRMA
14c8b 50 5f 50 41 47 45 4e 4f 28 28 70 42 74 29 2c 28 P_PAGENO((pBt),(
14c8c 70 67 6e 6f 29 29 3d 3d 28 70 67 6e 6f 29 29 0a pgno))==(pgno)).
14c8d 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 ./*.** The point
14c8e 65 72 20 6d 61 70 20 69 73 20 61 20 6c 6f 6f 6b er map is a look
14c8f 75 70 20 74 61 62 6c 65 20 74 68 61 74 20 69 64 up table that id
14c90 65 6e 74 69 66 69 65 73 20 74 68 65 20 70 61 72 entifies the par
14c91 65 6e 74 20 70 61 67 65 20 66 6f 72 0a 2a 2a 20 ent page for.**
14c92 65 61 63 68 20 63 68 69 6c 64 20 70 61 67 65 20 each child page
14c93 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
14c94 66 69 6c 65 2e 20 20 54 68 65 20 70 61 72 65 6e file. The paren
14c95 74 20 70 61 67 65 20 69 73 20 74 68 65 20 70 61 t page is the pa
14c96 67 65 20 74 68 61 74 0a 2a 2a 20 63 6f 6e 74 61 ge that.** conta
14c97 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f ins a pointer to
14c98 20 74 68 65 20 63 68 69 6c 64 2e 20 20 45 76 65 the child. Eve
14c99 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 ry page in the d
14c9a 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 73 atabase contains
14c9b 0a 2a 2a 20 30 20 6f 72 20 31 20 70 61 72 65 6e .** 0 or 1 paren
14c9c 74 20 70 61 67 65 73 2e 20 20 28 49 6e 20 74 68 t pages. (In th
14c9d 69 73 20 63 6f 6e 74 65 78 74 20 27 64 61 74 61 is context 'data
14c9e 62 61 73 65 20 70 61 67 65 27 20 72 65 66 65 72 base page' refer
14c9f 73 0a 2a 2a 20 74 6f 20 61 6e 79 20 70 61 67 65 s.** to any page
14ca0 20 74 68 61 74 20 69 73 20 6e 6f 74 20 70 61 72 that is not par
14ca1 74 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 t of the pointer
14ca2 20 6d 61 70 20 69 74 73 65 6c 66 2e 29 20 20 45 map itself.) E
14ca3 61 63 68 20 70 6f 69 6e 74 65 72 20 6d 61 70 0a ach pointer map.
14ca4 2a 2a 20 65 6e 74 72 79 20 63 6f 6e 73 69 73 74 ** entry consist
14ca5 73 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 62 79 s of a single by
14ca6 74 65 20 27 74 79 70 65 27 20 61 6e 64 20 61 20 te 'type' and a
14ca7 34 20 62 79 74 65 20 70 61 72 65 6e 74 20 70 61 4 byte parent pa
14ca8 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 54 68 ge number..** Th
14ca9 65 20 50 54 52 4d 41 50 5f 58 58 58 20 69 64 65 e PTRMAP_XXX ide
14caa 6e 74 69 66 69 65 72 73 20 62 65 6c 6f 77 20 61 ntifiers below a
14cab 72 65 20 74 68 65 20 76 61 6c 69 64 20 74 79 70 re the valid typ
14cac 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 75 es..**.** The pu
14cad 72 70 6f 73 65 20 6f 66 20 74 68 65 20 70 6f 69 rpose of the poi
14cae 6e 74 65 72 20 6d 61 70 20 69 73 20 74 6f 20 66 nter map is to f
14caf 61 63 69 6c 69 74 79 20 6d 6f 76 69 6e 67 20 70 acility moving p
14cb0 61 67 65 73 20 66 72 6f 6d 20 6f 6e 65 0a 2a 2a ages from one.**
14cb1 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74 68 65 position in the
14cb2 20 66 69 6c 65 20 74 6f 20 61 6e 6f 74 68 65 72 file to another
14cb3 20 61 73 20 70 61 72 74 20 6f 66 20 61 75 74 6f as part of auto
14cb4 76 61 63 75 75 6d 2e 20 20 57 68 65 6e 20 61 20 vacuum. When a
14cb5 70 61 67 65 0a 2a 2a 20 69 73 20 6d 6f 76 65 64 page.** is moved
14cb6 2c 20 74 68 65 20 70 6f 69 6e 74 65 72 20 69 6e , the pointer in
14cb7 20 69 74 73 20 70 61 72 65 6e 74 20 6d 75 73 74 its parent must
14cb8 20 62 65 20 75 70 64 61 74 65 64 20 74 6f 20 70 be updated to p
14cb9 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20 6e oint to the.** n
14cba 65 77 20 6c 6f 63 61 74 69 6f 6e 2e 20 20 54 68 ew location. Th
14cbb 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 69 73 e pointer map is
14cbc 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74 65 20 used to locate
14cbd 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 the parent page
14cbe 71 75 69 63 6b 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50 quickly..**.** P
14cbf 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 3a 20 TRMAP_ROOTPAGE:
14cc0 54 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 The database pag
14cc1 65 20 69 73 20 61 20 72 6f 6f 74 2d 70 61 67 65 e is a root-page
14cc2 2e 20 54 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 . The page-numbe
14cc3 72 20 69 73 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 r is not.**
14cc4 20 20 20 20 20 20 20 20 20 20 20 20 20 75 73 65 use
14cc5 64 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a d in this case..
14cc6 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 46 52 45 **.** PTRMAP_FRE
14cc7 45 50 41 47 45 3a 20 54 68 65 20 64 61 74 61 62 EPAGE: The datab
14cc8 61 73 65 20 70 61 67 65 20 69 73 20 61 6e 20 75 ase page is an u
14cc9 6e 75 73 65 64 20 28 66 72 65 65 29 20 70 61 67 nused (free) pag
14cca 65 2e 20 54 68 65 20 70 61 67 65 2d 6e 75 6d 62 e. The page-numb
14ccb 65 72 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 er .**
14ccc 20 20 20 20 20 20 20 20 69 73 20 6e 6f 74 20 75 is not u
14ccd 73 65 64 20 69 6e 20 74 68 69 73 20 63 61 73 65 sed in this case
14cce 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f ..**.** PTRMAP_O
14ccf 56 45 52 46 4c 4f 57 31 3a 20 54 68 65 20 64 61 VERFLOW1: The da
14cd0 74 61 62 61 73 65 20 70 61 67 65 20 69 73 20 74 tabase page is t
14cd1 68 65 20 66 69 72 73 74 20 70 61 67 65 20 69 6e he first page in
14cd2 20 61 20 6c 69 73 74 20 6f 66 20 0a 2a 2a 20 20 a list of .**
14cd3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14cd4 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e overflow pages.
14cd5 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 The page number
14cd6 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 identifies the
14cd7 70 61 67 65 20 74 68 61 74 0a 2a 2a 20 20 20 20 page that.**
14cd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 c
14cd9 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 65 6c 6c ontains the cell
14cda 20 77 69 74 68 20 61 20 70 6f 69 6e 74 65 72 20 with a pointer
14cdb 74 6f 20 74 68 69 73 20 6f 76 65 72 66 6c 6f 77 to this overflow
14cdc 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 page..**.** PTR
14cdd 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 3a 20 54 MAP_OVERFLOW2: T
14cde 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 he database page
14cdf 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20 6f is the second o
14ce0 72 20 6c 61 74 65 72 20 70 61 67 65 20 69 6e 20 r later page in
14ce1 61 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 20 20 20 a list of.**
14ce2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f o
14ce3 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 54 verflow pages. T
14ce4 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 69 he page-number i
14ce5 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 70 72 dentifies the pr
14ce6 65 76 69 6f 75 73 0a 2a 2a 20 20 20 20 20 20 20 evious.**
14ce7 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67 65 page
14ce8 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 in the overflow
14ce9 20 70 61 67 65 20 6c 69 73 74 2e 0a 2a 2a 0a 2a page list..**.*
14cea 2a 20 50 54 52 4d 41 50 5f 42 54 52 45 45 3a 20 * PTRMAP_BTREE:
14ceb 54 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 The database pag
14cec 65 20 69 73 20 61 20 6e 6f 6e 2d 72 6f 6f 74 20 e is a non-root
14ced 62 74 72 65 65 20 70 61 67 65 2e 20 54 68 65 20 btree page. The
14cee 70 61 67 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 page number.**
14cef 20 20 20 20 20 20 20 20 20 20 20 20 20 69 64 65 ide
14cf0 6e 74 69 66 69 65 73 20 74 68 65 20 70 61 72 65 ntifies the pare
14cf1 6e 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 62 nt page in the b
14cf2 74 72 65 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 tree..*/.#define
14cf3 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 PTRMAP_ROOTPAGE
14cf4 20 31 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 1.#define PTRMA
14cf5 50 5f 46 52 45 45 50 41 47 45 20 32 0a 23 64 65 P_FREEPAGE 2.#de
14cf6 66 69 6e 65 20 50 54 52 4d 41 50 5f 4f 56 45 52 fine PTRMAP_OVER
14cf7 46 4c 4f 57 31 20 33 0a 23 64 65 66 69 6e 65 20 FLOW1 3.#define
14cf8 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 PTRMAP_OVERFLOW2
14cf9 20 34 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 4.#define PTRMA
14cfa 50 5f 42 54 52 45 45 20 35 0a 0a 2f 2a 20 41 20 P_BTREE 5../* A
14cfb 62 75 6e 63 68 20 6f 66 20 61 73 73 65 72 74 28 bunch of assert(
14cfc 29 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 ) statements to
14cfd 63 68 65 63 6b 20 74 68 65 20 74 72 61 6e 73 61 check the transa
14cfe 63 74 69 6f 6e 20 73 74 61 74 65 20 76 61 72 69 ction state vari
14cff 61 62 6c 65 73 0a 2a 2a 20 6f 66 20 68 61 6e 64 ables.** of hand
14d00 6c 65 20 70 20 28 74 79 70 65 20 42 74 72 65 65 le p (type Btree
14d01 2a 29 20 61 72 65 20 69 6e 74 65 72 6e 61 6c 6c *) are internall
14d02 79 20 63 6f 6e 73 69 73 74 65 6e 74 2e 0a 2a 2f y consistent..*/
14d03 0a 23 64 65 66 69 6e 65 20 62 74 72 65 65 49 6e .#define btreeIn
14d04 74 65 67 72 69 74 79 28 70 29 20 5c 0a 20 20 61 tegrity(p) \. a
14d05 73 73 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 69 ssert( p->pBt->i
14d06 6e 54 72 61 6e 73 61 63 74 69 6f 6e 21 3d 54 52 nTransaction!=TR
14d07 41 4e 53 5f 4e 4f 4e 45 20 7c 7c 20 70 2d 3e 70 ANS_NONE || p->p
14d08 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e Bt->nTransaction
14d09 3d 3d 30 20 29 3b 20 5c 0a 20 20 61 73 73 65 72 ==0 ); \. asser
14d0a 74 28 20 70 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 t( p->pBt->inTra
14d0b 6e 73 61 63 74 69 6f 6e 3e 3d 70 2d 3e 69 6e 54 nsaction>=p->inT
14d0c 72 61 6e 73 20 29 3b 20 0a 0a 0a 2f 2a 0a 2a 2a rans ); .../*.**
14d0d 20 54 68 65 20 49 53 41 55 54 4f 56 41 43 55 55 The ISAUTOVACUU
14d0e 4d 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64 20 M macro is used
14d0f 77 69 74 68 69 6e 20 62 61 6c 61 6e 63 65 5f 6e within balance_n
14d10 6f 6e 72 6f 6f 74 28 29 20 74 6f 20 64 65 74 65 onroot() to dete
14d11 72 6d 69 6e 65 0a 2a 2a 20 69 66 20 74 68 65 20 rmine.** if the
14d12 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72 74 database support
14d13 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 6f 72 s auto-vacuum or
14d14 20 6e 6f 74 2e 20 42 65 63 61 75 73 65 20 69 74 not. Because it
14d15 20 69 73 20 75 73 65 64 0a 2a 2a 20 77 69 74 68 is used.** with
14d16 69 6e 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e in an expression
14d17 20 74 68 61 74 20 69 73 20 61 6e 20 61 72 67 75 that is an argu
14d18 6d 65 6e 74 20 74 6f 20 61 6e 6f 74 68 65 72 20 ment to another
14d19 6d 61 63 72 6f 20 0a 2a 2a 20 28 73 71 6c 69 74 macro .** (sqlit
14d1a 65 4d 61 6c 6c 6f 63 52 61 77 29 2c 20 69 74 20 eMallocRaw), it
14d1b 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 is not possible
14d1c 74 6f 20 75 73 65 20 63 6f 6e 64 69 74 69 6f 6e to use condition
14d1d 61 6c 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 2e 0a al compilation..
14d1e 2a 2a 20 53 6f 2c 20 74 68 69 73 20 6d 61 63 72 ** So, this macr
14d1f 6f 20 69 73 20 64 65 66 69 6e 65 64 20 69 6e 73 o is defined ins
14d20 74 65 61 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 tead..*/.#ifndef
14d21 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
14d22 4f 56 41 43 55 55 4d 0a 23 64 65 66 69 6e 65 20 OVACUUM.#define
14d23 49 53 41 55 54 4f 56 41 43 55 55 4d 20 28 70 42 ISAUTOVACUUM (pB
14d24 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 0a 23 t->autoVacuum).#
14d25 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 49 53 41 else.#define ISA
14d26 55 54 4f 56 41 43 55 55 4d 20 30 0a 23 65 6e 64 UTOVACUUM 0.#end
14d27 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 if.../*.** This
14d28 73 74 72 75 63 74 75 72 65 20 69 73 20 70 61 73 structure is pas
14d29 73 65 64 20 61 72 6f 75 6e 64 20 74 68 72 6f 75 sed around throu
14d2a 67 68 20 61 6c 6c 20 74 68 65 20 73 61 6e 69 74 gh all the sanit
14d2b 79 20 63 68 65 63 6b 69 6e 67 20 72 6f 75 74 69 y checking routi
14d2c 6e 65 73 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20 nes.** in order
14d2d 74 6f 20 6b 65 65 70 20 74 72 61 63 6b 20 6f 66 to keep track of
14d2e 20 73 6f 6d 65 20 67 6c 6f 62 61 6c 20 73 74 61 some global sta
14d2f 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a te information..
14d30 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
14d31 74 20 49 6e 74 65 67 72 69 74 79 43 6b 20 49 6e t IntegrityCk In
14d32 74 65 67 72 69 74 79 43 6b 3b 0a 73 74 72 75 63 tegrityCk;.struc
14d33 74 20 49 6e 74 65 67 72 69 74 79 43 6b 20 7b 0a t IntegrityCk {.
14d34 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b BtShared *pBt;
14d35 20 20 20 20 2f 2a 20 54 68 65 20 74 72 65 65 20 /* The tree
14d36 62 65 69 6e 67 20 63 68 65 63 6b 65 64 20 6f 75 being checked ou
14d37 74 20 2a 2f 0a 20 20 50 61 67 65 72 20 2a 70 50 t */. Pager *pP
14d38 61 67 65 72 3b 20 20 20 20 2f 2a 20 54 68 65 20 ager; /* The
14d39 61 73 73 6f 63 69 61 74 65 64 20 70 61 67 65 72 associated pager
14d3a 2e 20 20 41 6c 73 6f 20 61 63 63 65 73 73 69 62 . Also accessib
14d3b 6c 65 20 62 79 20 70 42 74 2d 3e 70 50 61 67 65 le by pBt->pPage
14d3c 72 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 50 61 67 r */. Pgno nPag
14d3d 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 e; /* Numb
14d3e 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 er of pages in t
14d3f 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 he database */.
14d40 20 69 6e 74 20 2a 61 6e 52 65 66 3b 20 20 20 20 int *anRef;
14d41 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
14d42 74 69 6d 65 73 20 65 61 63 68 20 70 61 67 65 20 times each page
14d43 69 73 20 72 65 66 65 72 65 6e 63 65 64 20 2a 2f is referenced */
14d44 0a 20 20 69 6e 74 20 6d 78 45 72 72 3b 20 20 20 . int mxErr;
14d45 20 20 20 20 20 2f 2a 20 53 74 6f 70 20 61 63 63 /* Stop acc
14d46 75 6d 75 6c 61 74 69 6e 67 20 65 72 72 6f 72 73 umulating errors
14d47 20 77 68 65 6e 20 74 68 69 73 20 72 65 61 63 68 when this reach
14d48 65 73 20 7a 65 72 6f 20 2a 2f 0a 20 20 69 6e 74 es zero */. int
14d49 20 6e 45 72 72 3b 20 20 20 20 20 20 20 20 20 2f nErr; /
14d4a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 73 73 * Number of mess
14d4b 61 67 65 73 20 77 72 69 74 74 65 6e 20 74 6f 20 ages written to
14d4c 7a 45 72 72 4d 73 67 20 73 6f 20 66 61 72 20 2a zErrMsg so far *
14d4d 2f 0a 20 20 69 6e 74 20 6d 61 6c 6c 6f 63 46 61 /. int mallocFa
14d4e 69 6c 65 64 3b 20 2f 2a 20 41 20 6d 65 6d 6f 72 iled; /* A memor
14d4f 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 y allocation err
14d50 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 64 20 or has occurred
14d51 2a 2f 0a 20 20 53 74 72 41 63 63 75 6d 20 65 72 */. StrAccum er
14d52 72 4d 73 67 3b 20 20 2f 2a 20 41 63 63 75 6d 75 rMsg; /* Accumu
14d53 6c 61 74 65 20 74 68 65 20 65 72 72 6f 72 20 6d late the error m
14d54 65 73 73 61 67 65 20 74 65 78 74 20 68 65 72 65 essage text here
14d55 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 */.};../*.** Re
14d56 61 64 20 6f 72 20 77 72 69 74 65 20 61 20 74 77 ad or write a tw
14d57 6f 2d 20 61 6e 64 20 66 6f 75 72 2d 62 79 74 65 o- and four-byte
14d58 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 big-endian inte
14d59 67 65 72 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a 23 ger values..*/.#
14d5a 64 65 66 69 6e 65 20 67 65 74 32 62 79 74 65 28 define get2byte(
14d5b 78 29 20 20 20 28 28 78 29 5b 30 5d 3c 3c 38 20 x) ((x)[0]<<8
14d5c 7c 20 28 78 29 5b 31 5d 29 0a 23 64 65 66 69 6e | (x)[1]).#defin
14d5d 65 20 70 75 74 32 62 79 74 65 28 70 2c 76 29 20 e put2byte(p,v)
14d5e 28 28 70 29 5b 30 5d 20 3d 20 28 75 38 29 28 28 ((p)[0] = (u8)((
14d5f 76 29 3e 3e 38 29 2c 20 28 70 29 5b 31 5d 20 3d v)>>8), (p)[1] =
14d60 20 28 75 38 29 28 76 29 29 0a 23 64 65 66 69 6e (u8)(v)).#defin
14d61 65 20 67 65 74 34 62 79 74 65 20 73 71 6c 69 74 e get4byte sqlit
14d62 65 33 47 65 74 34 62 79 74 65 0a 23 64 65 66 69 e3Get4byte.#defi
14d63 6e 65 20 70 75 74 34 62 79 74 65 20 73 71 6c 69 ne put4byte sqli
14d64 74 65 33 50 75 74 34 62 79 74 65 0a 0a 2f 2a 2a te3Put4byte../**
14d65 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
14d66 20 6f 66 20 62 74 72 65 65 49 6e 74 2e 68 20 2a of btreeInt.h *
14d67 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14d68 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14d69 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
14d6a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e ************ Con
14d6b 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 tinuing where we
14d6c 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 62 74 6d left off in btm
14d6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a utex.c *********
14d6e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 69 66 ***********/.#if
14d6f 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
14d70 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 23 69 _SHARED_CACHE.#i
14d71 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 f SQLITE_THREADS
14d72 41 46 45 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 AFE../*.** Obtai
14d73 6e 20 74 68 65 20 42 74 53 68 61 72 65 64 20 6d n the BtShared m
14d74 75 74 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 utex associated
14d75 77 69 74 68 20 42 2d 54 72 65 65 20 68 61 6e 64 with B-Tree hand
14d76 6c 65 20 70 2e 20 41 6c 73 6f 2c 0a 2a 2a 20 73 le p. Also,.** s
14d77 65 74 20 42 74 53 68 61 72 65 64 2e 64 62 20 74 et BtShared.db t
14d78 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 o the database h
14d79 61 6e 64 6c 65 20 61 73 73 6f 63 69 61 74 65 64 andle associated
14d7a 20 77 69 74 68 20 70 20 61 6e 64 20 74 68 65 0a with p and the.
14d7b 2a 2a 20 70 2d 3e 6c 6f 63 6b 65 64 20 62 6f 6f ** p->locked boo
14d7c 6c 65 61 6e 20 74 6f 20 74 72 75 65 2e 0a 2a 2f lean to true..*/
14d7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 63 .static void loc
14d7e 6b 42 74 72 65 65 4d 75 74 65 78 28 42 74 72 65 kBtreeMutex(Btre
14d7f 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 e *p){. assert(
14d80 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29 3b p->locked==0 );
14d81 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
14d82 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 e3_mutex_notheld
14d83 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 (p->pBt->mutex)
14d84 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
14d85 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
14d86 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b p->db->mutex) );
14d87 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 .. sqlite3_mute
14d88 78 5f 65 6e 74 65 72 28 70 2d 3e 70 42 74 2d 3e x_enter(p->pBt->
14d89 6d 75 74 65 78 29 3b 0a 20 20 70 2d 3e 70 42 74 mutex);. p->pBt
14d8a 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 ->db = p->db;.
14d8b 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 31 3b 0a 7d p->locked = 1;.}
14d8c 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 ../*.** Release
14d8d 74 68 65 20 42 74 53 68 61 72 65 64 20 6d 75 74 the BtShared mut
14d8e 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ex associated wi
14d8f 74 68 20 42 2d 54 72 65 65 20 68 61 6e 64 6c 65 th B-Tree handle
14d90 20 70 20 61 6e 64 0a 2a 2a 20 63 6c 65 61 72 20 p and.** clear
14d91 74 68 65 20 70 2d 3e 6c 6f 63 6b 65 64 20 62 6f the p->locked bo
14d92 6f 6c 65 61 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 olean..*/.static
14d93 20 76 6f 69 64 20 75 6e 6c 6f 63 6b 42 74 72 65 void unlockBtre
14d94 65 4d 75 74 65 78 28 42 74 72 65 65 20 2a 70 29 eMutex(Btree *p)
14d95 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6c {. assert( p->l
14d96 6f 63 6b 65 64 3d 3d 31 20 29 3b 0a 20 20 61 73 ocked==1 );. as
14d97 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
14d98 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 42 74 2d tex_held(p->pBt-
14d99 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 >mutex) );. ass
14d9a 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
14d9b 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d ex_held(p->db->m
14d9c 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
14d9d 74 28 20 70 2d 3e 64 62 3d 3d 70 2d 3e 70 42 74 t( p->db==p->pBt
14d9e 2d 3e 64 62 20 29 3b 0a 0a 20 20 73 71 6c 69 74 ->db );.. sqlit
14d9f 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 e3_mutex_leave(p
14da0 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 ->pBt->mutex);.
14da1 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 30 3b 0a p->locked = 0;.
14da2 7d 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 72 20 61 }../*.** Enter a
14da3 20 6d 75 74 65 78 20 6f 6e 20 74 68 65 20 67 69 mutex on the gi
14da4 76 65 6e 20 42 54 72 65 65 20 6f 62 6a 65 63 74 ven BTree object
14da5 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6f ..**.** If the o
14da6 62 6a 65 63 74 20 69 73 20 6e 6f 74 20 73 68 61 bject is not sha
14da7 72 61 62 6c 65 2c 20 74 68 65 6e 20 6e 6f 20 6d rable, then no m
14da8 75 74 65 78 20 69 73 20 65 76 65 72 20 72 65 71 utex is ever req
14da9 75 69 72 65 64 0a 2a 2a 20 61 6e 64 20 74 68 69 uired.** and thi
14daa 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e s routine is a n
14dab 6f 2d 6f 70 2e 20 20 54 68 65 20 75 6e 64 65 72 o-op. The under
14dac 6c 79 69 6e 67 20 6d 75 74 65 78 20 69 73 20 6e lying mutex is n
14dad 6f 6e 2d 72 65 63 75 72 73 69 76 65 2e 0a 2a 2a on-recursive..**
14dae 20 42 75 74 20 77 65 20 6b 65 65 70 20 61 20 72 But we keep a r
14daf 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 69 eference count i
14db0 6e 20 42 74 72 65 65 2e 77 61 6e 74 54 6f 4c 6f n Btree.wantToLo
14db1 63 6b 20 73 6f 20 74 68 65 20 62 65 68 61 76 69 ck so the behavi
14db2 6f 72 0a 2a 2a 20 6f 66 20 74 68 69 73 20 69 6e or.** of this in
14db3 74 65 72 66 61 63 65 20 69 73 20 72 65 63 75 72 terface is recur
14db4 73 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 sive..**.** To a
14db5 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b 73 2c 20 void deadlocks,
14db6 6d 75 6c 74 69 70 6c 65 20 42 74 72 65 65 73 20 multiple Btrees
14db7 61 72 65 20 6c 6f 63 6b 65 64 20 69 6e 20 74 68 are locked in th
14db8 65 20 73 61 6d 65 20 6f 72 64 65 72 0a 2a 2a 20 e same order.**
14db9 62 79 20 61 6c 6c 20 64 61 74 61 62 61 73 65 20 by all database
14dba 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 54 68 connections. Th
14dbb 65 20 70 2d 3e 70 4e 65 78 74 20 69 73 20 61 20 e p->pNext is a
14dbc 6c 69 73 74 20 6f 66 20 6f 74 68 65 72 0a 2a 2a list of other.**
14dbd 20 42 74 72 65 65 73 20 62 65 6c 6f 6e 67 69 6e Btrees belongin
14dbe 67 20 74 6f 20 74 68 65 20 73 61 6d 65 20 64 61 g to the same da
14dbf 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
14dc0 6e 20 61 73 20 74 68 65 20 70 20 42 74 72 65 65 n as the p Btree
14dc1 0a 2a 2a 20 77 68 69 63 68 20 6e 65 65 64 20 74 .** which need t
14dc2 6f 20 62 65 20 6c 6f 63 6b 65 64 20 61 66 74 65 o be locked afte
14dc3 72 20 70 2e 20 20 49 66 20 77 65 20 63 61 6e 6e r p. If we cann
14dc4 6f 74 20 67 65 74 20 61 20 6c 6f 63 6b 20 6f 6e ot get a lock on
14dc5 0a 2a 2a 20 70 2c 20 74 68 65 6e 20 66 69 72 73 .** p, then firs
14dc6 74 20 75 6e 6c 6f 63 6b 20 61 6c 6c 20 6f 66 20 t unlock all of
14dc7 74 68 65 20 6f 74 68 65 72 73 20 6f 6e 20 70 2d the others on p-
14dc8 3e 70 4e 65 78 74 2c 20 74 68 65 6e 20 77 61 69 >pNext, then wai
14dc9 74 0a 2a 2a 20 66 6f 72 20 74 68 65 20 6c 6f 63 t.** for the loc
14dca 6b 20 74 6f 20 62 65 63 6f 6d 65 20 61 76 61 69 k to become avai
14dcb 6c 61 62 6c 65 20 6f 6e 20 70 2c 20 74 68 65 6e lable on p, then
14dcc 20 72 65 6c 6f 63 6b 20 61 6c 6c 20 6f 66 20 74 relock all of t
14dcd 68 65 0a 2a 2a 20 73 75 62 73 65 71 75 65 6e 74 he.** subsequent
14dce 20 42 74 72 65 65 73 20 74 68 61 74 20 64 65 73 Btrees that des
14dcf 69 72 65 20 61 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 ire a lock..*/.S
14dd0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
14dd1 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 id sqlite3BtreeE
14dd2 6e 74 65 72 28 42 74 72 65 65 20 2a 70 29 7b 0a nter(Btree *p){.
14dd3 20 20 42 74 72 65 65 20 2a 70 4c 61 74 65 72 3b Btree *pLater;
14dd4 0a 0a 20 20 2f 2a 20 53 6f 6d 65 20 62 61 73 69 .. /* Some basi
14dd5 63 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e c sanity checkin
14dd6 67 20 6f 6e 20 74 68 65 20 42 74 72 65 65 2e 20 g on the Btree.
14dd7 20 54 68 65 20 6c 69 73 74 20 6f 66 20 42 74 72 The list of Btr
14dd8 65 65 73 0a 20 20 2a 2a 20 63 6f 6e 6e 65 63 74 ees. ** connect
14dd9 65 64 20 62 79 20 70 4e 65 78 74 20 61 6e 64 20 ed by pNext and
14dda 70 50 72 65 76 20 73 68 6f 75 6c 64 20 62 65 20 pPrev should be
14ddb 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 20 in sorted order
14ddc 62 79 0a 20 20 2a 2a 20 42 74 72 65 65 2e 70 42 by. ** Btree.pB
14ddd 74 20 76 61 6c 75 65 2e 20 41 6c 6c 20 65 6c 65 t value. All ele
14dde 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 6c 69 73 ments of the lis
14ddf 74 20 73 68 6f 75 6c 64 20 62 65 6c 6f 6e 67 20 t should belong
14de0 74 6f 0a 20 20 2a 2a 20 74 68 65 20 73 61 6d 65 to. ** the same
14de1 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 4f 6e 6c connection. Onl
14de2 79 20 73 68 61 72 65 64 20 42 74 72 65 65 73 20 y shared Btrees
14de3 61 72 65 20 6f 6e 20 74 68 65 20 6c 69 73 74 2e are on the list.
14de4 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d */. assert( p-
14de5 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c 20 70 2d 3e >pNext==0 || p->
14de6 70 4e 65 78 74 2d 3e 70 42 74 3e 70 2d 3e 70 42 pNext->pBt>p->pB
14de7 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 t );. assert( p
14de8 2d 3e 70 50 72 65 76 3d 3d 30 20 7c 7c 20 70 2d ->pPrev==0 || p-
14de9 3e 70 50 72 65 76 2d 3e 70 42 74 3c 70 2d 3e 70 >pPrev->pBt<p->p
14dea 42 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 Bt );. assert(
14deb 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c 20 70 p->pNext==0 || p
14dec 2d 3e 70 4e 65 78 74 2d 3e 64 62 3d 3d 70 2d 3e ->pNext->db==p->
14ded 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 db );. assert(
14dee 70 2d 3e 70 50 72 65 76 3d 3d 30 20 7c 7c 20 70 p->pPrev==0 || p
14def 2d 3e 70 50 72 65 76 2d 3e 64 62 3d 3d 70 2d 3e ->pPrev->db==p->
14df0 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 db );. assert(
14df1 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20 28 p->sharable || (
14df2 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 26 26 20 70 p->pNext==0 && p
14df3 2d 3e 70 50 72 65 76 3d 3d 30 29 20 29 3b 0a 0a ->pPrev==0) );..
14df4 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 6c /* Check for l
14df5 6f 63 6b 69 6e 67 20 63 6f 6e 73 69 73 74 65 6e ocking consisten
14df6 63 79 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 cy */. assert(
14df7 21 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20 70 2d !p->locked || p-
14df8 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b >wantToLock>0 );
14df9 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 . assert( p->sh
14dfa 61 72 61 62 6c 65 20 7c 7c 20 70 2d 3e 77 61 6e arable || p->wan
14dfb 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 3b 0a 0a 20 tToLock==0 );..
14dfc 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20 61 6c /* We should al
14dfd 72 65 61 64 79 20 68 6f 6c 64 20 61 20 6c 6f 63 ready hold a loc
14dfe 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 k on the databas
14dff 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a e connection */.
14e00 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
14e01 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 3_mutex_held(p->
14e02 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 db->mutex) );..
14e03 20 2f 2a 20 55 6e 6c 65 73 73 20 74 68 65 20 64 /* Unless the d
14e04 61 74 61 62 61 73 65 20 69 73 20 73 68 61 72 61 atabase is shara
14e05 62 6c 65 20 61 6e 64 20 75 6e 6c 6f 63 6b 65 64 ble and unlocked
14e06 2c 20 74 68 65 6e 20 42 74 53 68 61 72 65 64 2e , then BtShared.
14e07 64 62 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20 61 db. ** should a
14e08 6c 72 65 61 64 79 20 62 65 20 73 65 74 20 63 6f lready be set co
14e09 72 72 65 63 74 6c 79 2e 20 2a 2f 0a 20 20 61 73 rrectly. */. as
14e0a 73 65 72 74 28 20 28 70 2d 3e 6c 6f 63 6b 65 64 sert( (p->locked
14e0b 3d 3d 30 20 26 26 20 70 2d 3e 73 68 61 72 61 62 ==0 && p->sharab
14e0c 6c 65 29 20 7c 7c 20 70 2d 3e 70 42 74 2d 3e 64 le) || p->pBt->d
14e0d 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a 0a 20 20 69 b==p->db );.. i
14e0e 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c 65 20 f( !p->sharable
14e0f 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 2d 3e 77 ) return;. p->w
14e10 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 69 antToLock++;. i
14e11 66 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 29 20 72 f( p->locked ) r
14e12 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 49 6e 20 eturn;.. /* In
14e13 6d 6f 73 74 20 63 61 73 65 73 2c 20 77 65 20 73 most cases, we s
14e14 68 6f 75 6c 64 20 62 65 20 61 62 6c 65 20 74 6f hould be able to
14e15 20 61 63 71 75 69 72 65 20 74 68 65 20 6c 6f 63 acquire the loc
14e16 6b 20 77 65 0a 20 20 2a 2a 20 77 61 6e 74 20 77 k we. ** want w
14e17 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f ithout having to
14e18 20 67 6f 20 74 68 72 6f 75 67 68 74 20 74 68 65 go throught the
14e19 20 61 73 63 65 6e 64 69 6e 67 20 6c 6f 63 6b 0a ascending lock.
14e1a 20 20 2a 2a 20 70 72 6f 63 65 64 75 72 65 20 74 ** procedure t
14e1b 68 61 74 20 66 6f 6c 6c 6f 77 73 2e 20 20 4a 75 hat follows. Ju
14e1c 73 74 20 62 65 20 73 75 72 65 20 6e 6f 74 20 74 st be sure not t
14e1d 6f 20 62 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 o block.. */.
14e1e 69 66 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 if( sqlite3_mute
14e1f 78 5f 74 72 79 28 70 2d 3e 70 42 74 2d 3e 6d 75 x_try(p->pBt->mu
14e20 74 65 78 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 tex)==SQLITE_OK
14e21 29 7b 0a 20 20 20 20 70 2d 3e 70 42 74 2d 3e 64 ){. p->pBt->d
14e22 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 70 b = p->db;. p
14e23 2d 3e 6c 6f 63 6b 65 64 20 3d 20 31 3b 0a 20 20 ->locked = 1;.
14e24 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 return;. }..
14e25 20 2f 2a 20 54 6f 20 61 76 6f 69 64 20 64 65 61 /* To avoid dea
14e26 64 6c 6f 63 6b 2c 20 66 69 72 73 74 20 72 65 6c dlock, first rel
14e27 65 61 73 65 20 61 6c 6c 20 6c 6f 63 6b 73 20 77 ease all locks w
14e28 69 74 68 20 61 20 6c 61 72 67 65 72 0a 20 20 2a ith a larger. *
14e29 2a 20 42 74 53 68 61 72 65 64 20 61 64 64 72 65 * BtShared addre
14e2a 73 73 2e 20 20 54 68 65 6e 20 61 63 71 75 69 72 ss. Then acquir
14e2b 65 20 6f 75 72 20 6c 6f 63 6b 2e 20 20 54 68 65 e our lock. The
14e2c 6e 20 72 65 61 63 71 75 69 72 65 0a 20 20 2a 2a n reacquire. **
14e2d 20 74 68 65 20 6f 74 68 65 72 20 42 74 53 68 61 the other BtSha
14e2e 72 65 64 20 6c 6f 63 6b 73 20 74 68 61 74 20 77 red locks that w
14e2f 65 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 69 e used to hold i
14e30 6e 20 61 73 63 65 6e 64 69 6e 67 0a 20 20 2a 2a n ascending. **
14e31 20 6f 72 64 65 72 2e 0a 20 20 2a 2f 0a 20 20 66 order.. */. f
14e32 6f 72 28 70 4c 61 74 65 72 3d 70 2d 3e 70 4e 65 or(pLater=p->pNe
14e33 78 74 3b 20 70 4c 61 74 65 72 3b 20 70 4c 61 74 xt; pLater; pLat
14e34 65 72 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74 er=pLater->pNext
14e35 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
14e36 4c 61 74 65 72 2d 3e 73 68 61 72 61 62 6c 65 20 Later->sharable
14e37 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
14e38 4c 61 74 65 72 2d 3e 70 4e 65 78 74 3d 3d 30 20 Later->pNext==0
14e39 7c 7c 20 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74 || pLater->pNext
14e3a 2d 3e 70 42 74 3e 70 4c 61 74 65 72 2d 3e 70 42 ->pBt>pLater->pB
14e3b 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 t );. assert(
14e3c 20 21 70 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 64 !pLater->locked
14e3d 20 7c 7c 20 70 4c 61 74 65 72 2d 3e 77 61 6e 74 || pLater->want
14e3e 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 20 20 ToLock>0 );.
14e3f 69 66 28 20 70 4c 61 74 65 72 2d 3e 6c 6f 63 6b if( pLater->lock
14e40 65 64 20 29 7b 0a 20 20 20 20 20 20 75 6e 6c 6f ed ){. unlo
14e41 63 6b 42 74 72 65 65 4d 75 74 65 78 28 70 4c 61 ckBtreeMutex(pLa
14e42 74 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ter);. }. }.
14e43 20 20 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 lockBtreeMutex
14e44 28 70 29 3b 0a 20 20 66 6f 72 28 70 4c 61 74 65 (p);. for(pLate
14e45 72 3d 70 2d 3e 70 4e 65 78 74 3b 20 70 4c 61 74 r=p->pNext; pLat
14e46 65 72 3b 20 70 4c 61 74 65 72 3d 70 4c 61 74 65 er; pLater=pLate
14e47 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 r->pNext){. i
14e48 66 28 20 70 4c 61 74 65 72 2d 3e 77 61 6e 74 54 f( pLater->wantT
14e49 6f 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 6c oLock ){. l
14e4a 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 70 4c ockBtreeMutex(pL
14e4b 61 74 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d ater);. }. }
14e4c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 69 74 20 74 .}../*.** Exit t
14e4d 68 65 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 he recursive mut
14e4e 65 78 20 6f 6e 20 61 20 42 74 72 65 65 2e 0a 2a ex on a Btree..*
14e4f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
14e50 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 void sqlite3Btr
14e51 65 65 4c 65 61 76 65 28 42 74 72 65 65 20 2a 70 eeLeave(Btree *p
14e52 29 7b 0a 20 20 69 66 28 20 70 2d 3e 73 68 61 72 ){. if( p->shar
14e53 61 62 6c 65 20 29 7b 0a 20 20 20 20 61 73 73 65 able ){. asse
14e54 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 rt( p->wantToLoc
14e55 6b 3e 30 20 29 3b 0a 20 20 20 20 70 2d 3e 77 61 k>0 );. p->wa
14e56 6e 74 54 6f 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 ntToLock--;.
14e57 69 66 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 if( p->wantToLoc
14e58 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 75 6e k==0 ){. un
14e59 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 70 lockBtreeMutex(p
14e5a 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a );. }. }.}..
14e5b 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f #ifndef NDEBUG./
14e5c 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 *.** Return true
14e5d 20 69 66 20 74 68 65 20 42 74 53 68 61 72 65 64 if the BtShared
14e5e 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64 20 6f mutex is held o
14e5f 6e 20 74 68 65 20 62 74 72 65 65 2c 20 6f 72 20 n the btree, or
14e60 69 66 20 74 68 65 0a 2a 2a 20 42 2d 54 72 65 65 if the.** B-Tree
14e61 20 69 73 20 6e 6f 74 20 6d 61 72 6b 65 64 20 61 is not marked a
14e62 73 20 73 68 61 72 61 62 6c 65 2e 0a 2a 2a 0a 2a s sharable..**.*
14e63 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
14e64 73 20 75 73 65 64 20 6f 6e 6c 79 20 66 72 6f 6d s used only from
14e65 20 77 69 74 68 69 6e 20 61 73 73 65 72 74 28 29 within assert()
14e66 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a statements..*/.
14e67 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
14e68 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 nt sqlite3BtreeH
14e69 6f 6c 64 73 4d 75 74 65 78 28 42 74 72 65 65 20 oldsMutex(Btree
14e6a 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 *p){. assert( p
14e6b 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20 7c 7c ->sharable==0 ||
14e6c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 7c 7c p->locked==0 ||
14e6d 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 p->wantToLock>0
14e6e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d );. assert( p-
14e6f 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20 7c 7c 20 >sharable==0 ||
14e70 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 7c 7c 20 p->locked==0 ||
14e71 70 2d 3e 64 62 3d 3d 70 2d 3e 70 42 74 2d 3e 64 p->db==p->pBt->d
14e72 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 b );. assert( p
14e73 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20 7c 7c ->sharable==0 ||
14e74 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 7c 7c p->locked==0 ||
14e75 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
14e76 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 eld(p->pBt->mute
14e77 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
14e78 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20 7c p->sharable==0 |
14e79 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 7c | p->locked==0 |
14e7a 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f | sqlite3_mutex_
14e7b 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 held(p->db->mute
14e7c 78 29 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 x) );.. return
14e7d 28 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20 (p->sharable==0
14e7e 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 29 3b 0a 7d || p->locked);.}
14e7f 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 65 .#endif...#ifnde
14e80 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e f SQLITE_OMIT_IN
14e81 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 45 6e 74 CRBLOB./*.** Ent
14e82 65 72 20 61 6e 64 20 6c 65 61 76 65 20 61 20 6d er and leave a m
14e83 75 74 65 78 20 6f 6e 20 61 20 42 74 72 65 65 20 utex on a Btree
14e84 67 69 76 65 6e 20 61 20 63 75 72 73 6f 72 20 6f given a cursor o
14e85 77 6e 65 64 20 62 79 20 74 68 61 74 0a 2a 2a 20 wned by that.**
14e86 42 74 72 65 65 2e 20 20 54 68 65 73 65 20 65 6e Btree. These en
14e87 74 72 79 20 70 6f 69 6e 74 73 20 61 72 65 20 75 try points are u
14e88 73 65 64 20 62 79 20 69 6e 63 72 65 6d 65 6e 74 sed by increment
14e89 61 6c 20 49 2f 4f 20 61 6e 64 20 63 61 6e 20 62 al I/O and can b
14e8a 65 0a 2a 2a 20 6f 6d 69 74 74 65 64 20 69 66 20 e.** omitted if
14e8b 74 68 61 74 20 6d 6f 64 75 6c 65 20 69 73 20 6e that module is n
14e8c 6f 74 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 ot used..*/.SQLI
14e8d 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
14e8e 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
14e8f 72 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 rCursor(BtCursor
14e90 20 2a 70 43 75 72 29 7b 0a 20 20 73 71 6c 69 74 *pCur){. sqlit
14e91 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 43 75 e3BtreeEnter(pCu
14e92 72 2d 3e 70 42 74 72 65 65 29 3b 0a 7d 0a 53 51 r->pBtree);.}.SQ
14e93 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
14e94 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 d sqlite3BtreeLe
14e95 61 76 65 43 75 72 73 6f 72 28 42 74 43 75 72 73 aveCursor(BtCurs
14e96 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 73 71 6c or *pCur){. sql
14e97 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
14e98 43 75 72 2d 3e 70 42 74 72 65 65 29 3b 0a 7d 0a Cur->pBtree);.}.
14e99 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
14e9a 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 20 2a _OMIT_INCRBLOB *
14e9b 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 72 20 /.../*.** Enter
14e9c 74 68 65 20 6d 75 74 65 78 20 6f 6e 20 65 76 65 the mutex on eve
14e9d 72 79 20 42 74 72 65 65 20 61 73 73 6f 63 69 61 ry Btree associa
14e9e 74 65 64 20 77 69 74 68 20 61 20 64 61 74 61 62 ted with a datab
14e9f 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f ase.** connectio
14ea0 6e 2e 20 20 54 68 69 73 20 69 73 20 6e 65 65 64 n. This is need
14ea1 65 64 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 29 ed (for example)
14ea2 20 70 72 69 6f 72 20 74 6f 20 70 61 72 73 69 6e prior to parsin
14ea3 67 0a 2a 2a 20 61 20 73 74 61 74 65 6d 65 6e 74 g.** a statement
14ea4 20 73 69 6e 63 65 20 77 65 20 77 69 6c 6c 20 62 since we will b
14ea5 65 20 63 6f 6d 70 61 72 69 6e 67 20 74 61 62 6c e comparing tabl
14ea6 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d e and column nam
14ea7 65 73 0a 2a 2a 20 61 67 61 69 6e 73 74 20 61 6c es.** against al
14ea8 6c 20 73 63 68 65 6d 61 73 20 61 6e 64 20 77 65 l schemas and we
14ea9 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 68 6f do not want tho
14eaa 73 65 20 73 63 68 65 6d 61 73 20 62 65 69 6e 67 se schemas being
14eab 0a 2a 2a 20 72 65 73 65 74 20 6f 75 74 20 66 72 .** reset out fr
14eac 6f 6d 20 75 6e 64 65 72 20 75 73 2e 0a 2a 2a 0a om under us..**.
14ead 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20 63 6f ** There is a co
14eae 72 72 65 73 70 6f 6e 64 69 6e 67 20 6c 65 61 76 rresponding leav
14eaf 65 2d 61 6c 6c 20 70 72 6f 63 65 64 75 72 65 73 e-all procedures
14eb0 2e 0a 2a 2a 0a 2a 2a 20 45 6e 74 65 72 20 74 68 ..**.** Enter th
14eb1 65 20 6d 75 74 65 78 65 73 20 69 6e 20 61 63 63 e mutexes in acc
14eb2 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 79 20 ending order by
14eb3 42 74 53 68 61 72 65 64 20 70 6f 69 6e 74 65 72 BtShared pointer
14eb4 20 61 64 64 72 65 73 73 0a 2a 2a 20 74 6f 20 61 address.** to a
14eb5 76 6f 69 64 20 74 68 65 20 70 6f 73 73 69 62 69 void the possibi
14eb6 6c 69 74 79 20 6f 66 20 64 65 61 64 6c 6f 63 6b lity of deadlock
14eb7 20 77 68 65 6e 20 74 77 6f 20 74 68 72 65 61 64 when two thread
14eb8 73 20 77 69 74 68 0a 2a 2a 20 74 77 6f 20 6f 72 s with.** two or
14eb9 20 6d 6f 72 65 20 62 74 72 65 65 73 20 69 6e 20 more btrees in
14eba 63 6f 6d 6d 6f 6e 20 62 6f 74 68 20 74 72 79 20 common both try
14ebb 74 6f 20 6c 6f 63 6b 20 61 6c 6c 20 74 68 65 69 to lock all thei
14ebc 72 20 62 74 72 65 65 73 0a 2a 2a 20 61 74 20 74 r btrees.** at t
14ebd 68 65 20 73 61 6d 65 20 69 6e 73 74 61 6e 74 2e he same instant.
14ebe 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
14ebf 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
14ec0 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 73 71 6c treeEnterAll(sql
14ec1 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 ite3 *db){. int
14ec2 20 69 3b 0a 20 20 42 74 72 65 65 20 2a 70 2c 20 i;. Btree *p,
14ec3 2a 70 4c 61 74 65 72 3b 0a 20 20 61 73 73 65 72 *pLater;. asser
14ec4 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
14ec5 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 _held(db->mutex)
14ec6 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 );. for(i=0; i
14ec7 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a <db->nDb; i++){.
14ec8 20 20 20 20 70 20 3d 20 64 62 2d 3e 61 44 62 5b p = db->aDb[
14ec9 69 5d 2e 70 42 74 3b 0a 20 20 20 20 61 73 73 65 i].pBt;. asse
14eca 72 74 28 20 21 70 20 7c 7c 20 28 70 2d 3e 6c 6f rt( !p || (p->lo
14ecb 63 6b 65 64 3d 3d 30 20 26 26 20 70 2d 3e 73 68 cked==0 && p->sh
14ecc 61 72 61 62 6c 65 29 20 7c 7c 20 70 2d 3e 70 42 arable) || p->pB
14ecd 74 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a t->db==p->db );.
14ece 20 20 20 20 69 66 28 20 70 20 26 26 20 70 2d 3e if( p && p->
14ecf 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 sharable ){.
14ed0 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2b p->wantToLock+
14ed1 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 2d +;. if( !p-
14ed2 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20 20 20 >locked ){.
14ed3 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61 assert( p->wa
14ed4 6e 74 54 6f 4c 6f 63 6b 3d 3d 31 20 29 3b 0a 20 ntToLock==1 );.
14ed5 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 2d while( p-
14ed6 3e 70 50 72 65 76 20 29 20 70 20 3d 20 70 2d 3e >pPrev ) p = p->
14ed7 70 50 72 65 76 3b 0a 20 20 20 20 20 20 20 20 2f pPrev;. /
14ed8 2a 20 52 65 61 73 6f 6e 20 66 6f 72 20 41 4c 57 * Reason for ALW
14ed9 41 59 53 3a 20 20 54 68 65 72 65 20 6d 75 73 74 AYS: There must
14eda 20 62 65 20 61 74 20 6c 65 61 73 74 20 6f 6e 20 be at least on
14edb 75 6e 6c 6f 63 6b 65 64 20 42 74 72 65 65 20 69 unlocked Btree i
14edc 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 n. ** the
14edd 20 63 68 61 69 6e 2e 20 20 4f 74 68 65 72 77 69 chain. Otherwi
14ede 73 65 20 74 68 65 20 21 70 2d 3e 6c 6f 63 6b 65 se the !p->locke
14edf 64 20 74 65 73 74 20 61 62 6f 76 65 20 77 6f 75 d test above wou
14ee0 6c 64 20 68 61 76 65 20 66 61 69 6c 65 64 20 2a ld have failed *
14ee1 2f 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 /. while(
14ee2 20 70 2d 3e 6c 6f 63 6b 65 64 20 26 26 20 41 4c p->locked && AL
14ee3 57 41 59 53 28 70 2d 3e 70 4e 65 78 74 29 20 29 WAYS(p->pNext) )
14ee4 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 p = p->pNext;.
14ee5 20 20 20 20 20 20 20 66 6f 72 28 70 4c 61 74 65 for(pLate
14ee6 72 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 20 70 4c r = p->pNext; pL
14ee7 61 74 65 72 3b 20 70 4c 61 74 65 72 3d 70 4c 61 ater; pLater=pLa
14ee8 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 ter->pNext){.
14ee9 20 20 20 20 20 20 20 69 66 28 20 70 4c 61 74 65 if( pLate
14eea 72 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20 r->locked ){.
14eeb 20 20 20 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 unlockB
14eec 74 72 65 65 4d 75 74 65 78 28 70 4c 61 74 65 72 treeMutex(pLater
14eed 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 );. }.
14eee 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
14eef 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20 while( p ){.
14ef0 20 20 20 20 20 20 20 6c 6f 63 6b 42 74 72 65 65 lockBtree
14ef1 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20 20 20 Mutex(p);.
14ef2 20 20 20 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 p = p->pNext
14ef3 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
14ef4 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a }. }. }.}.
14ef5 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
14ef6 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 oid sqlite3Btree
14ef7 4c 65 61 76 65 41 6c 6c 28 73 71 6c 69 74 65 33 LeaveAll(sqlite3
14ef8 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a *db){. int i;.
14ef9 20 20 42 74 72 65 65 20 2a 70 3b 0a 20 20 61 73 Btree *p;. as
14efa 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
14efb 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 tex_held(db->mut
14efc 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 ex) );. for(i=0
14efd 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b ; i<db->nDb; i++
14efe 29 7b 0a 20 20 20 20 70 20 3d 20 64 62 2d 3e 61 ){. p = db->a
14eff 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 Db[i].pBt;. i
14f00 66 28 20 70 20 26 26 20 70 2d 3e 73 68 61 72 61 f( p && p->shara
14f01 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 61 73 73 ble ){. ass
14f02 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f ert( p->wantToLo
14f03 63 6b 3e 30 20 29 3b 0a 20 20 20 20 20 20 70 2d ck>0 );. p-
14f04 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2d 2d 3b 0a 20 >wantToLock--;.
14f05 20 20 20 20 20 69 66 28 20 70 2d 3e 77 61 6e 74 if( p->want
14f06 54 6f 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 ToLock==0 ){.
14f07 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 unlockBtree
14f08 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20 20 20 Mutex(p);.
14f09 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 }. }. }.}..#
14f0a 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a ifndef NDEBUG./*
14f0b 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 .** Return true
14f0c 69 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 if the current t
14f0d 68 72 65 61 64 20 68 6f 6c 64 73 20 74 68 65 20 hread holds the
14f0e 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
14f0f 69 6f 6e 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 ion.** mutex and
14f10 20 61 6c 6c 20 72 65 71 75 69 72 65 64 20 42 74 all required Bt
14f11 53 68 61 72 65 64 20 6d 75 74 65 78 65 73 2e 0a Shared mutexes..
14f12 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
14f13 6e 65 20 69 73 20 75 73 65 64 20 69 6e 73 69 64 ne is used insid
14f14 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 e assert() state
14f15 6d 65 6e 74 73 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 ments only..*/.S
14f16 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
14f17 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f t sqlite3BtreeHo
14f18 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 73 71 ldsAllMutexes(sq
14f19 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e lite3 *db){. in
14f1a 74 20 69 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 t i;. if( !sqli
14f1b 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 te3_mutex_held(d
14f1c 62 2d 3e 6d 75 74 65 78 29 20 29 7b 0a 20 20 20 b->mutex) ){.
14f1d 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
14f1e 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e for(i=0; i<db->
14f1f 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 nDb; i++){. B
14f20 74 72 65 65 20 2a 70 3b 0a 20 20 20 20 70 20 3d tree *p;. p =
14f21 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b db->aDb[i].pBt;
14f22 0a 20 20 20 20 69 66 28 20 70 20 26 26 20 70 2d . if( p && p-
14f23 3e 73 68 61 72 61 62 6c 65 20 26 26 0a 20 20 20 >sharable &&.
14f24 20 20 20 20 20 20 28 70 2d 3e 77 61 6e 74 54 6f (p->wantTo
14f25 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 21 73 71 6c 69 Lock==0 || !sqli
14f26 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
14f27 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 29 20 29 ->pBt->mutex)) )
14f28 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 {. return 0
14f29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
14f2a 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 turn 1;.}.#endif
14f2b 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 2f /* NDEBUG */../
14f2c 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 42 *.** Add a new B
14f2d 74 72 65 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 tree pointer to
14f2e 61 20 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 a BtreeMutexArra
14f2f 79 2e 20 0a 2a 2a 20 69 66 20 74 68 65 20 70 6f y. .** if the po
14f30 69 6e 74 65 72 20 63 61 6e 20 70 6f 73 73 69 62 inter can possib
14f31 6c 79 20 62 65 20 73 68 61 72 65 64 20 77 69 74 ly be shared wit
14f32 68 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 64 61 74 h.** another dat
14f33 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
14f34 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e ..**.** The poin
14f35 74 65 72 73 20 61 72 65 20 6b 65 70 74 20 69 6e ters are kept in
14f36 20 73 6f 72 74 65 64 20 6f 72 64 65 72 20 62 79 sorted order by
14f37 20 70 42 74 72 65 65 2d 3e 70 42 74 2e 20 20 54 pBtree->pBt. T
14f38 68 61 74 0a 2a 2a 20 77 61 79 20 77 68 65 6e 20 hat.** way when
14f39 77 65 20 67 6f 20 74 6f 20 65 6e 74 65 72 20 61 we go to enter a
14f3a 6c 6c 20 74 68 65 20 6d 75 74 65 78 65 73 2c 20 ll the mutexes,
14f3b 77 65 20 63 61 6e 20 65 6e 74 65 72 20 74 68 65 we can enter the
14f3c 6d 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20 77 69 m.** in order wi
14f3d 74 68 6f 75 74 20 65 76 65 72 79 20 68 61 76 69 thout every havi
14f3e 6e 67 20 74 6f 20 62 61 63 6b 75 70 20 61 6e 64 ng to backup and
14f3f 20 72 65 74 72 79 20 61 6e 64 20 77 69 74 68 6f retry and witho
14f40 75 74 0a 2a 2a 20 77 6f 72 72 79 69 6e 67 20 61 ut.** worrying a
14f41 62 6f 75 74 20 64 65 61 64 6c 6f 63 6b 2e 0a 2a bout deadlock..*
14f42 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 *.** The number
14f43 6f 66 20 73 68 61 72 65 64 20 62 74 72 65 65 73 of shared btrees
14f44 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 20 will always be
14f45 73 6d 61 6c 6c 20 28 75 73 75 61 6c 6c 79 20 30 small (usually 0
14f46 20 6f 72 20 31 29 0a 2a 2a 20 73 6f 20 61 6e 20 or 1).** so an
14f47 69 6e 73 65 72 74 69 6f 6e 20 73 6f 72 74 20 69 insertion sort i
14f48 73 20 61 6e 20 61 64 65 71 75 61 74 65 20 61 6c s an adequate al
14f49 67 6f 72 69 74 68 6d 20 68 65 72 65 2e 0a 2a 2f gorithm here..*/
14f4a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
14f4b 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
14f4c 65 4d 75 74 65 78 41 72 72 61 79 49 6e 73 65 72 eMutexArrayInser
14f4d 74 28 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 t(BtreeMutexArra
14f4e 79 20 2a 70 41 72 72 61 79 2c 20 42 74 72 65 65 y *pArray, Btree
14f4f 20 2a 70 42 74 72 65 65 29 7b 0a 20 20 69 6e 74 *pBtree){. int
14f50 20 69 2c 20 6a 3b 0a 20 20 42 74 53 68 61 72 65 i, j;. BtShare
14f51 64 20 2a 70 42 74 3b 0a 20 20 69 66 28 20 70 42 d *pBt;. if( pB
14f52 74 72 65 65 3d 3d 30 20 7c 7c 20 70 42 74 72 65 tree==0 || pBtre
14f53 65 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20 29 e->sharable==0 )
14f54 20 72 65 74 75 72 6e 3b 0a 23 69 66 6e 64 65 66 return;.#ifndef
14f55 20 4e 44 45 42 55 47 0a 20 20 7b 0a 20 20 20 20 NDEBUG. {.
14f56 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41 72 72 61 for(i=0; i<pArra
14f57 79 2d 3e 6e 4d 75 74 65 78 3b 20 69 2b 2b 29 7b y->nMutex; i++){
14f58 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
14f59 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d Array->aBtree[i]
14f5a 21 3d 70 42 74 72 65 65 20 29 3b 0a 20 20 20 20 !=pBtree );.
14f5b 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 61 }. }.#endif. a
14f5c 73 73 65 72 74 28 20 70 41 72 72 61 79 2d 3e 6e ssert( pArray->n
14f5d 4d 75 74 65 78 3e 3d 30 20 29 3b 0a 20 20 61 73 Mutex>=0 );. as
14f5e 73 65 72 74 28 20 70 41 72 72 61 79 2d 3e 6e 4d sert( pArray->nM
14f5f 75 74 65 78 3c 41 72 72 61 79 53 69 7a 65 28 70 utex<ArraySize(p
14f60 41 72 72 61 79 2d 3e 61 42 74 72 65 65 29 2d 31 Array->aBtree)-1
14f61 20 29 3b 0a 20 20 70 42 74 20 3d 20 70 42 74 72 );. pBt = pBtr
14f62 65 65 2d 3e 70 42 74 3b 0a 20 20 66 6f 72 28 69 ee->pBt;. for(i
14f63 3d 30 3b 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d =0; i<pArray->nM
14f64 75 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 utex; i++){.
14f65 61 73 73 65 72 74 28 20 70 41 72 72 61 79 2d 3e assert( pArray->
14f66 61 42 74 72 65 65 5b 69 5d 21 3d 70 42 74 72 65 aBtree[i]!=pBtre
14f67 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70 41 72 e );. if( pAr
14f68 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d 2d 3e ray->aBtree[i]->
14f69 70 42 74 3e 70 42 74 20 29 7b 0a 20 20 20 20 20 pBt>pBt ){.
14f6a 20 66 6f 72 28 6a 3d 70 41 72 72 61 79 2d 3e 6e for(j=pArray->n
14f6b 4d 75 74 65 78 3b 20 6a 3e 69 3b 20 6a 2d 2d 29 Mutex; j>i; j--)
14f6c 7b 0a 20 20 20 20 20 20 20 20 70 41 72 72 61 79 {. pArray
14f6d 2d 3e 61 42 74 72 65 65 5b 6a 5d 20 3d 20 70 41 ->aBtree[j] = pA
14f6e 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 6a 2d 31 rray->aBtree[j-1
14f6f 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 ];. }.
14f70 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b pArray->aBtree[
14f71 69 5d 20 3d 20 70 42 74 72 65 65 3b 0a 20 20 20 i] = pBtree;.
14f72 20 20 20 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 pArray->nMute
14f73 78 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74 75 72 x++;. retur
14f74 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 n;. }. }. p
14f75 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 70 41 Array->aBtree[pA
14f76 72 72 61 79 2d 3e 6e 4d 75 74 65 78 2b 2b 5d 20 rray->nMutex++]
14f77 3d 20 70 42 74 72 65 65 3b 0a 7d 0a 0a 2f 2a 0a = pBtree;.}../*.
14f78 2a 2a 20 45 6e 74 65 72 20 74 68 65 20 6d 75 74 ** Enter the mut
14f79 65 78 20 6f 66 20 65 76 65 72 79 20 62 74 72 65 ex of every btre
14f7a 65 20 69 6e 20 74 68 65 20 61 72 72 61 79 2e 20 e in the array.
14f7b 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
14f7c 0a 2a 2a 20 63 61 6c 6c 65 64 20 61 74 20 74 68 .** called at th
14f7d 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 73 e beginning of s
14f7e 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 29 qlite3VdbeExec()
14f7f 2e 20 20 54 68 65 20 6d 75 74 65 78 65 73 20 61 . The mutexes a
14f80 72 65 0a 2a 2a 20 65 78 69 74 65 64 20 61 74 20 re.** exited at
14f81 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 73 the end of the s
14f82 61 6d 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f ame function..*/
14f83 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
14f84 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
14f85 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65 72 eMutexArrayEnter
14f86 28 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 (BtreeMutexArray
14f87 20 2a 70 41 72 72 61 79 29 7b 0a 20 20 69 6e 74 *pArray){. int
14f88 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 i;. for(i=0; i
14f89 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b <pArray->nMutex;
14f8a 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65 65 i++){. Btree
14f8b 20 2a 70 20 3d 20 70 41 72 72 61 79 2d 3e 61 42 *p = pArray->aB
14f8c 74 72 65 65 5b 69 5d 3b 0a 20 20 20 20 2f 2a 20 tree[i];. /*
14f8d 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e 69 74 Some basic sanit
14f8e 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20 y checking */.
14f8f 20 20 61 73 73 65 72 74 28 20 69 3d 3d 30 20 7c assert( i==0 |
14f90 7c 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 | pArray->aBtree
14f91 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70 2d 3e 70 42 [i-1]->pBt<p->pB
14f92 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 t );. assert(
14f93 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20 70 !p->locked || p
14f94 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 ->wantToLock>0 )
14f95 3b 0a 0a 20 20 20 20 2f 2a 20 57 65 20 73 68 6f ;.. /* We sho
14f96 75 6c 64 20 61 6c 72 65 61 64 79 20 68 6f 6c 64 uld already hold
14f97 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 a lock on the d
14f98 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
14f99 6f 6e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 on */. assert
14f9a 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
14f9b 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 held(p->db->mute
14f9c 78 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 x) );.. /* Th
14f9d 65 20 42 74 72 65 65 20 69 73 20 73 68 61 72 61 e Btree is shara
14f9e 62 6c 65 20 62 65 63 61 75 73 65 20 6f 6e 6c 79 ble because only
14f9f 20 73 68 61 72 61 62 6c 65 20 42 74 72 65 65 73 sharable Btrees
14fa0 20 61 72 65 20 65 6e 74 65 72 65 64 0a 20 20 20 are entered.
14fa1 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 61 72 72 ** into the arr
14fa2 61 79 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 ay in the first
14fa3 70 6c 61 63 65 2e 20 2a 2f 0a 20 20 20 20 61 73 place. */. as
14fa4 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c sert( p->sharabl
14fa5 65 20 29 3b 0a 0a 20 20 20 20 70 2d 3e 77 61 6e e );.. p->wan
14fa6 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 69 tToLock++;. i
14fa7 66 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 29 7b f( !p->locked ){
14fa8 0a 20 20 20 20 20 20 6c 6f 63 6b 42 74 72 65 65 . lockBtree
14fa9 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20 7d 0a Mutex(p);. }.
14faa 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 65 61 }.}../*.** Lea
14fab 76 65 20 74 68 65 20 6d 75 74 65 78 20 6f 66 20 ve the mutex of
14fac 65 76 65 72 79 20 62 74 72 65 65 20 69 6e 20 74 every btree in t
14fad 68 65 20 67 72 6f 75 70 2e 0a 2a 2f 0a 53 51 4c he group..*/.SQL
14fae 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
14faf 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 sqlite3BtreeMut
14fb0 65 78 41 72 72 61 79 4c 65 61 76 65 28 42 74 72 exArrayLeave(Btr
14fb1 65 65 4d 75 74 65 78 41 72 72 61 79 20 2a 70 41 eeMutexArray *pA
14fb2 72 72 61 79 29 7b 0a 20 20 69 6e 74 20 69 3b 0a rray){. int i;.
14fb3 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41 72 for(i=0; i<pAr
14fb4 72 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 69 2b 2b ray->nMutex; i++
14fb5 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 20 ){. Btree *p
14fb6 3d 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 = pArray->aBtree
14fb7 5b 69 5d 3b 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 [i];. /* Some
14fb8 20 62 61 73 69 63 20 73 61 6e 69 74 79 20 63 68 basic sanity ch
14fb9 65 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 61 73 ecking */. as
14fba 73 65 72 74 28 20 69 3d 3d 30 20 7c 7c 20 70 41 sert( i==0 || pA
14fbb 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 2d 31 rray->aBtree[i-1
14fbc 5d 2d 3e 70 42 74 3c 70 2d 3e 70 42 74 20 29 3b ]->pBt<p->pBt );
14fbd 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e . assert( p->
14fbe 6c 6f 63 6b 65 64 20 29 3b 0a 20 20 20 20 61 73 locked );. as
14fbf 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c sert( p->wantToL
14fc0 6f 63 6b 3e 30 20 29 3b 0a 0a 20 20 20 20 2f 2a ock>0 );.. /*
14fc1 20 57 65 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 We should alrea
14fc2 64 79 20 68 6f 6c 64 20 61 20 6c 6f 63 6b 20 6f dy hold a lock o
14fc3 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 n the database c
14fc4 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 onnection */.
14fc5 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
14fc6 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 _mutex_held(p->d
14fc7 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 b->mutex) );..
14fc8 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2d p->wantToLock-
14fc9 2d 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 77 61 -;. if( p->wa
14fca 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 ntToLock==0 ){.
14fcb 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 unlockBtree
14fcc 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20 7d 0a Mutex(p);. }.
14fcd 20 20 7d 0a 7d 0a 0a 23 65 6c 73 65 0a 53 51 4c }.}..#else.SQL
14fce 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
14fcf 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
14fd0 65 72 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 er(Btree *p){.
14fd1 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e p->pBt->db = p->
14fd2 64 62 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 db;.}.SQLITE_PRI
14fd3 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
14fd4 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 73 3BtreeEnterAll(s
14fd5 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 qlite3 *db){. i
14fd6 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b nt i;. for(i=0;
14fd7 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 i<db->nDb; i++)
14fd8 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 20 3d {. Btree *p =
14fd9 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b db->aDb[i].pBt;
14fda 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20 . if( p ){.
14fdb 20 20 20 20 70 2d 3e 70 42 74 2d 3e 64 62 20 3d p->pBt->db =
14fdc 20 70 2d 3e 64 62 3b 0a 20 20 20 20 7d 0a 20 20 p->db;. }.
14fdd 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 69 66 }.}.#endif /* if
14fde 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 SQLITE_THREADSA
14fdf 46 45 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 FE */.#endif /*
14fe0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
14fe1 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 20 IT_SHARED_CACHE
14fe2 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
14fe3 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 6d 75 74 *** End of btmut
14fe4 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ex.c ***********
14fe5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14fe6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14fe7 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
14fe8 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 62 *** Begin file b
14fe9 74 72 65 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a tree.c *********
14fea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14feb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14fec 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 41 **/./*.** 2004 A
14fed 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 pril 6.**.** The
14fee 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
14fef 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
14ff0 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
14ff1 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
14ff2 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
14ff3 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
14ff4 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
14ff5 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
14ff6 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
14ff7 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
14ff8 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
14ff9 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
14ffa 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
14ffb 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
14ffc 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
14ffd 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
14ffe 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
14fff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15001 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15002 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15003 2a 2a 2a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 62 74 *****.** $Id: bt
15004 72 65 65 2e 63 2c 76 20 31 2e 37 30 35 20 32 30 ree.c,v 1.705 20
15005 30 39 2f 30 38 2f 31 30 20 30 33 3a 35 37 3a 35 09/08/10 03:57:5
15006 38 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 2a 8 shane Exp $.**
15007 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d .** This file im
15008 70 6c 65 6d 65 6e 74 73 20 61 20 65 78 74 65 72 plements a exter
15009 6e 61 6c 20 28 64 69 73 6b 2d 62 61 73 65 64 29 nal (disk-based)
1500a 20 64 61 74 61 62 61 73 65 20 75 73 69 6e 67 20 database using
1500b 42 54 72 65 65 73 2e 0a 2a 2a 20 53 65 65 20 74 BTrees..** See t
1500c 68 65 20 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e he header commen
1500d 74 20 6f 6e 20 22 62 74 72 65 65 49 6e 74 2e 68 t on "btreeInt.h
1500e 22 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c " for additional
1500f 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a information..**
15010 20 49 6e 63 6c 75 64 69 6e 67 20 61 20 64 65 73 Including a des
15011 63 72 69 70 74 69 6f 6e 20 6f 66 20 66 69 6c 65 cription of file
15012 20 66 6f 72 6d 61 74 20 61 6e 64 20 61 6e 20 6f format and an o
15013 76 65 72 76 69 65 77 20 6f 66 20 6f 70 65 72 61 verview of opera
15014 74 69 6f 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 tion..*/../*.**
15015 54 68 65 20 68 65 61 64 65 72 20 73 74 72 69 6e The header strin
15016 67 20 74 68 61 74 20 61 70 70 65 61 72 73 20 61 g that appears a
15017 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 t the beginning
15018 6f 66 20 65 76 65 72 79 0a 2a 2a 20 53 51 4c 69 of every.** SQLi
15019 74 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a te database..*/.
1501a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 static const cha
1501b 72 20 7a 4d 61 67 69 63 48 65 61 64 65 72 5b 5d r zMagicHeader[]
1501c 20 3d 20 53 51 4c 49 54 45 5f 46 49 4c 45 5f 48 = SQLITE_FILE_H
1501d 45 41 44 45 52 3b 0a 0a 2f 2a 0a 2a 2a 20 53 65 EADER;../*.** Se
1501e 74 20 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 61 t this global va
1501f 72 69 61 62 6c 65 20 74 6f 20 31 20 74 6f 20 65 riable to 1 to e
15020 6e 61 62 6c 65 20 74 72 61 63 69 6e 67 20 75 73 nable tracing us
15021 69 6e 67 20 74 68 65 20 54 52 41 43 45 0a 2a 2a ing the TRACE.**
15022 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 20 30 macro..*/.#if 0
15023 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 .int sqlite3Btre
15024 65 54 72 61 63 65 3d 31 3b 20 20 2f 2a 20 54 72 eTrace=1; /* Tr
15025 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 74 72 61 ue to enable tra
15026 63 69 6e 67 20 2a 2f 0a 23 20 64 65 66 69 6e 65 cing */.# define
15027 20 54 52 41 43 45 28 58 29 20 20 69 66 28 73 71 TRACE(X) if(sq
15028 6c 69 74 65 33 42 74 72 65 65 54 72 61 63 65 29 lite3BtreeTrace)
15029 7b 70 72 69 6e 74 66 20 58 3b 66 66 6c 75 73 68 {printf X;fflush
1502a 28 73 74 64 6f 75 74 29 3b 7d 0a 23 65 6c 73 65 (stdout);}.#else
1502b 0a 23 20 64 65 66 69 6e 65 20 54 52 41 43 45 28 .# define TRACE(
1502c 58 29 0a 23 65 6e 64 69 66 0a 0a 0a 0a 23 69 66 X).#endif....#if
1502d 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1502e 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a _SHARED_CACHE./*
1502f 0a 2a 2a 20 41 20 6c 69 73 74 20 6f 66 20 42 74 .** A list of Bt
15030 53 68 61 72 65 64 20 6f 62 6a 65 63 74 73 20 74 Shared objects t
15031 68 61 74 20 61 72 65 20 65 6c 69 67 69 62 6c 65 hat are eligible
15032 20 66 6f 72 20 70 61 72 74 69 63 69 70 61 74 69 for participati
15033 6f 6e 0a 2a 2a 20 69 6e 20 73 68 61 72 65 64 20 on.** in shared
15034 63 61 63 68 65 2e 20 20 54 68 69 73 20 76 61 72 cache. This var
15035 69 61 62 6c 65 20 68 61 73 20 66 69 6c 65 20 73 iable has file s
15036 63 6f 70 65 20 64 75 72 69 6e 67 20 6e 6f 72 6d cope during norm
15037 61 6c 20 62 75 69 6c 64 73 2c 0a 2a 2a 20 62 75 al builds,.** bu
15038 74 20 74 68 65 20 74 65 73 74 20 68 61 72 6e 65 t the test harne
15039 73 73 20 6e 65 65 64 73 20 74 6f 20 61 63 63 65 ss needs to acce
1503a 73 73 20 69 74 20 73 6f 20 77 65 20 6d 61 6b 65 ss it so we make
1503b 20 69 74 20 67 6c 6f 62 61 6c 20 66 6f 72 20 0a it global for .
1503c 2a 2a 20 74 65 73 74 20 62 75 69 6c 64 73 2e 0a ** test builds..
1503d 2a 2a 0a 2a 2a 20 41 63 63 65 73 73 20 74 6f 20 **.** Access to
1503e 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 69 73 this variable is
1503f 20 70 72 6f 74 65 63 74 65 64 20 62 79 20 53 51 protected by SQ
15040 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
15041 43 5f 4d 41 53 54 45 52 2e 0a 2a 2f 0a 23 69 66 C_MASTER..*/.#if
15042 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
15043 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 42 SQLITE_PRIVATE B
15044 74 53 68 61 72 65 64 20 2a 53 51 4c 49 54 45 5f tShared *SQLITE_
15045 57 53 44 20 73 71 6c 69 74 65 33 53 68 61 72 65 WSD sqlite3Share
15046 64 43 61 63 68 65 4c 69 73 74 20 3d 20 30 3b 0a dCacheList = 0;.
15047 23 65 6c 73 65 0a 73 74 61 74 69 63 20 42 74 53 #else.static BtS
15048 68 61 72 65 64 20 2a 53 51 4c 49 54 45 5f 57 53 hared *SQLITE_WS
15049 44 20 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 D sqlite3SharedC
1504a 61 63 68 65 4c 69 73 74 20 3d 20 30 3b 0a 23 65 acheList = 0;.#e
1504b 6e 64 69 66 0a 23 65 6e 64 69 66 20 2f 2a 20 53 ndif.#endif /* S
1504c 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
1504d 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e D_CACHE */..#ifn
1504e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1504f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a SHARED_CACHE./*.
15050 2a 2a 20 45 6e 61 62 6c 65 20 6f 72 20 64 69 73 ** Enable or dis
15051 61 62 6c 65 20 74 68 65 20 73 68 61 72 65 64 20 able the shared
15052 70 61 67 65 72 20 61 6e 64 20 73 63 68 65 6d 61 pager and schema
15053 20 66 65 61 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a features..**.**
15054 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 68 61 This routine ha
15055 73 20 6e 6f 20 65 66 66 65 63 74 20 6f 6e 20 65 s no effect on e
15056 78 69 73 74 69 6e 67 20 64 61 74 61 62 61 73 65 xisting database
15057 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 0a 2a 2a connections..**
15058 20 54 68 65 20 73 68 61 72 65 64 20 63 61 63 68 The shared cach
15059 65 20 73 65 74 74 69 6e 67 20 65 66 66 65 63 74 e setting effect
1505a 73 20 6f 6e 6c 79 20 66 75 74 75 72 65 20 63 61 s only future ca
1505b 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 lls to.** sqlite
1505c 33 5f 6f 70 65 6e 28 29 2c 20 73 71 6c 69 74 65 3_open(), sqlite
1505d 33 5f 6f 70 65 6e 31 36 28 29 2c 20 6f 72 20 73 3_open16(), or s
1505e 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 qlite3_open_v2()
1505f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
15060 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 int sqlite3_enab
15061 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68 65 28 le_shared_cache(
15062 69 6e 74 20 65 6e 61 62 6c 65 29 7b 0a 20 20 73 int enable){. s
15063 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
15064 69 67 2e 73 68 61 72 65 64 43 61 63 68 65 45 6e ig.sharedCacheEn
15065 61 62 6c 65 64 20 3d 20 65 6e 61 62 6c 65 3b 0a abled = enable;.
15066 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
15067 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 0a OK;.}.#endif....
15068 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d #ifdef SQLITE_OM
15069 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a IT_SHARED_CACHE.
1506a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 66 75 /*. ** The fu
1506b 6e 63 74 69 6f 6e 73 20 71 75 65 72 79 53 68 61 nctions querySha
1506c 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 redCacheTableLoc
1506d 6b 28 29 2c 20 73 65 74 53 68 61 72 65 64 43 61 k(), setSharedCa
1506e 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 2c 0a cheTableLock(),.
1506f 20 20 2a 2a 20 61 6e 64 20 63 6c 65 61 72 41 6c ** and clearAl
15070 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c lSharedCacheTabl
15071 65 4c 6f 63 6b 73 28 29 0a 20 20 2a 2a 20 6d 61 eLocks(). ** ma
15072 6e 69 70 75 6c 61 74 65 20 65 6e 74 72 69 65 73 nipulate entries
15073 20 69 6e 20 74 68 65 20 42 74 53 68 61 72 65 64 in the BtShared
15074 2e 70 4c 6f 63 6b 20 6c 69 6e 6b 65 64 20 6c 69 .pLock linked li
15075 73 74 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 st used to store
15076 0a 20 20 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 . ** shared-cac
15077 68 65 20 74 61 62 6c 65 20 6c 65 76 65 6c 20 6c he table level l
15078 6f 63 6b 73 2e 20 49 66 20 74 68 65 20 6c 69 62 ocks. If the lib
15079 72 61 72 79 20 69 73 20 63 6f 6d 70 69 6c 65 64 rary is compiled
1507a 20 77 69 74 68 20 74 68 65 0a 20 20 2a 2a 20 73 with the. ** s
1507b 68 61 72 65 64 2d 63 61 63 68 65 20 66 65 61 74 hared-cache feat
1507c 75 72 65 20 64 69 73 61 62 6c 65 64 2c 20 74 68 ure disabled, th
1507d 65 6e 20 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 en there is only
1507e 20 65 76 65 72 20 6f 6e 65 20 75 73 65 72 0a 20 ever one user.
1507f 20 2a 2a 20 6f 66 20 65 61 63 68 20 42 74 53 68 ** of each BtSh
15080 61 72 65 64 20 73 74 72 75 63 74 75 72 65 20 61 ared structure a
15081 6e 64 20 73 6f 20 74 68 69 73 20 6c 6f 63 6b 69 nd so this locki
15082 6e 67 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 ng is not necess
15083 61 72 79 2e 20 0a 20 20 2a 2a 20 53 6f 20 64 65 ary. . ** So de
15084 66 69 6e 65 20 74 68 65 20 6c 6f 63 6b 20 72 65 fine the lock re
15085 6c 61 74 65 64 20 66 75 6e 63 74 69 6f 6e 73 20 lated functions
15086 61 73 20 6e 6f 2d 6f 70 73 2e 0a 20 20 2a 2f 0a as no-ops.. */.
15087 20 20 23 64 65 66 69 6e 65 20 71 75 65 72 79 53 #define queryS
15088 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c haredCacheTableL
15089 6f 63 6b 28 61 2c 62 2c 63 29 20 53 51 4c 49 54 ock(a,b,c) SQLIT
1508a 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e 65 20 73 E_OK. #define s
1508b 65 74 53 68 61 72 65 64 43 61 63 68 65 54 61 62 etSharedCacheTab
1508c 6c 65 4c 6f 63 6b 28 61 2c 62 2c 63 29 20 53 51 leLock(a,b,c) SQ
1508d 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e LITE_OK. #defin
1508e 65 20 63 6c 65 61 72 41 6c 6c 53 68 61 72 65 64 e clearAllShared
1508f 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 CacheTableLocks(
15090 61 29 0a 20 20 23 64 65 66 69 6e 65 20 64 6f 77 a). #define dow
15091 6e 67 72 61 64 65 41 6c 6c 53 68 61 72 65 64 43 ngradeAllSharedC
15092 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 61 acheTableLocks(a
15093 29 0a 20 20 23 64 65 66 69 6e 65 20 68 61 73 53 ). #define hasS
15094 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c haredCacheTableL
15095 6f 63 6b 28 61 2c 62 2c 63 2c 64 29 20 31 0a 20 ock(a,b,c,d) 1.
15096 20 23 64 65 66 69 6e 65 20 68 61 73 52 65 61 64 #define hasRead
15097 43 6f 6e 66 6c 69 63 74 73 28 61 2c 20 62 29 20 Conflicts(a, b)
15098 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 0.#endif..#ifnde
15099 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 f SQLITE_OMIT_SH
1509a 41 52 45 44 5f 43 41 43 48 45 0a 0a 23 69 66 64 ARED_CACHE..#ifd
1509b 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
1509c 2f 2a 0a 2a 2a 2a 2a 20 54 68 69 73 20 66 75 6e /*.**** This fun
1509d 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73 ction is only us
1509e 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 61 6e ed as part of an
1509f 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d assert() statem
150a0 65 6e 74 2e 20 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 43 ent. ***.**.** C
150a1 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 70 heck to see if p
150a2 42 74 72 65 65 20 68 6f 6c 64 73 20 74 68 65 20 Btree holds the
150a3 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 73 20 74 required locks t
150a4 6f 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 o read or write
150a5 74 6f 20 74 68 65 20 0a 2a 2a 20 74 61 62 6c 65 to the .** table
150a6 20 77 69 74 68 20 72 6f 6f 74 20 70 61 67 65 20 with root page
150a7 69 52 6f 6f 74 2e 20 20 20 52 65 74 75 72 6e 20 iRoot. Return
150a8 31 20 69 66 20 69 74 20 64 6f 65 73 20 61 6e 64 1 if it does and
150a9 20 30 20 69 66 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 0 if not..**.**
150aa 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77 68 For example, wh
150ab 65 6e 20 77 72 69 74 69 6e 67 20 74 6f 20 61 20 en writing to a
150ac 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d table with root-
150ad 70 61 67 65 20 69 52 6f 6f 74 20 76 69 61 20 0a page iRoot via .
150ae 2a 2a 20 42 74 72 65 65 20 63 6f 6e 6e 65 63 74 ** Btree connect
150af 69 6f 6e 20 70 42 74 72 65 65 3a 0a 2a 2a 0a 2a ion pBtree:.**.*
150b0 2a 20 20 20 20 61 73 73 65 72 74 28 20 68 61 73 * assert( has
150b1 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 SharedCacheTable
150b2 4c 6f 63 6b 28 70 42 74 72 65 65 2c 20 69 52 6f Lock(pBtree, iRo
150b3 6f 74 2c 20 30 2c 20 57 52 49 54 45 5f 4c 4f 43 ot, 0, WRITE_LOC
150b4 4b 29 20 29 3b 0a 2a 2a 0a 2a 2a 20 57 68 65 6e K) );.**.** When
150b5 20 77 72 69 74 69 6e 67 20 74 6f 20 61 6e 20 69 writing to an i
150b6 6e 64 65 78 20 74 68 61 74 20 72 65 73 69 64 65 ndex that reside
150b7 73 20 69 6e 20 61 20 73 68 61 72 61 62 6c 65 20 s in a sharable
150b8 64 61 74 61 62 61 73 65 2c 20 74 68 65 20 0a 2a database, the .*
150b9 2a 20 63 61 6c 6c 65 72 20 73 68 6f 75 6c 64 20 * caller should
150ba 68 61 76 65 20 66 69 72 73 74 20 6f 62 74 61 69 have first obtai
150bb 6e 65 64 20 61 20 6c 6f 63 6b 20 73 70 65 63 69 ned a lock speci
150bc 66 79 69 6e 67 20 74 68 65 20 72 6f 6f 74 20 70 fying the root p
150bd 61 67 65 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f age of.** the co
150be 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 61 62 6c rresponding tabl
150bf 65 2e 20 54 68 69 73 20 6d 61 6b 65 73 20 74 68 e. This makes th
150c0 69 6e 67 73 20 61 20 62 69 74 20 6d 6f 72 65 20 ings a bit more
150c1 63 6f 6d 70 6c 69 63 61 74 65 64 2c 0a 2a 2a 20 complicated,.**
150c2 61 73 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 74 as this module t
150c3 72 65 61 74 73 20 65 61 63 68 20 74 61 62 6c 65 reats each table
150c4 20 61 73 20 61 20 73 65 70 61 72 61 74 65 20 73 as a separate s
150c5 74 72 75 63 74 75 72 65 2e 20 54 6f 20 64 65 74 tructure. To det
150c6 65 72 6d 69 6e 65 0a 2a 2a 20 74 68 65 20 74 61 ermine.** the ta
150c7 62 6c 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e ble correspondin
150c8 67 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 62 g to the index b
150c9 65 69 6e 67 20 77 72 69 74 74 65 6e 2c 20 74 68 eing written, th
150ca 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 68 is.** function h
150cb 61 73 20 74 6f 20 73 65 61 72 63 68 20 74 68 72 as to search thr
150cc 6f 75 67 68 20 74 68 65 20 64 61 74 61 62 61 73 ough the databas
150cd 65 20 73 63 68 65 6d 61 2e 0a 2a 2a 0a 2a 2a 20 e schema..**.**
150ce 49 6e 73 74 65 61 64 20 6f 66 20 61 20 6c 6f 63 Instead of a loc
150cf 6b 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 2f 69 k on the table/i
150d0 6e 64 65 78 20 72 6f 6f 74 65 64 20 61 74 20 70 ndex rooted at p
150d1 61 67 65 20 69 52 6f 6f 74 2c 20 74 68 65 20 63 age iRoot, the c
150d2 61 6c 6c 65 72 20 6d 61 79 0a 2a 2a 20 68 6f 6c aller may.** hol
150d3 64 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f d a write-lock o
150d4 6e 20 74 68 65 20 73 63 68 65 6d 61 20 74 61 62 n the schema tab
150d5 6c 65 20 28 72 6f 6f 74 20 70 61 67 65 20 31 29 le (root page 1)
150d6 2e 20 54 68 69 73 20 69 73 20 61 6c 73 6f 0a 2a . This is also.*
150d7 2a 20 61 63 63 65 70 74 61 62 6c 65 2e 0a 2a 2f * acceptable..*/
150d8 0a 73 74 61 74 69 63 20 69 6e 74 20 68 61 73 53 .static int hasS
150d9 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c haredCacheTableL
150da 6f 63 6b 28 0a 20 20 42 74 72 65 65 20 2a 70 42 ock(. Btree *pB
150db 74 72 65 65 2c 20 20 20 20 20 20 20 20 20 2f 2a tree, /*
150dc 20 48 61 6e 64 6c 65 20 74 68 61 74 20 6d 75 73 Handle that mus
150dd 74 20 68 6f 6c 64 20 6c 6f 63 6b 20 2a 2f 0a 20 t hold lock */.
150de 20 50 67 6e 6f 20 69 52 6f 6f 74 2c 20 20 20 20 Pgno iRoot,
150df 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 /* Root
150e0 70 61 67 65 20 6f 66 20 62 2d 74 72 65 65 20 2a page of b-tree *
150e1 2f 0a 20 20 69 6e 74 20 69 73 49 6e 64 65 78 2c /. int isIndex,
150e2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
150e3 75 65 20 69 66 20 69 52 6f 6f 74 20 69 73 20 74 ue if iRoot is t
150e4 68 65 20 72 6f 6f 74 20 6f 66 20 61 6e 20 69 6e he root of an in
150e5 64 65 78 20 62 2d 74 72 65 65 20 2a 2f 0a 20 20 dex b-tree */.
150e6 69 6e 74 20 65 4c 6f 63 6b 54 79 70 65 20 20 20 int eLockType
150e7 20 20 20 20 20 20 20 2f 2a 20 52 65 71 75 69 72 /* Requir
150e8 65 64 20 6c 6f 63 6b 20 74 79 70 65 20 28 52 45 ed lock type (RE
150e9 41 44 5f 4c 4f 43 4b 20 6f 72 20 57 52 49 54 45 AD_LOCK or WRITE
150ea 5f 4c 4f 43 4b 29 20 2a 2f 0a 29 7b 0a 20 20 53 _LOCK) */.){. S
150eb 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d chema *pSchema =
150ec 20 28 53 63 68 65 6d 61 20 2a 29 70 42 74 72 65 (Schema *)pBtre
150ed 65 2d 3e 70 42 74 2d 3e 70 53 63 68 65 6d 61 3b e->pBt->pSchema;
150ee 0a 20 20 50 67 6e 6f 20 69 54 61 62 20 3d 20 30 . Pgno iTab = 0
150ef 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 ;. BtLock *pLoc
150f0 6b 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 k;.. /* If this
150f1 20 64 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74 database is not
150f2 20 73 68 61 72 65 61 62 6c 65 2c 20 6f 72 20 69 shareable, or i
150f3 66 20 74 68 65 20 63 6c 69 65 6e 74 20 69 73 20 f the client is
150f4 72 65 61 64 69 6e 67 0a 20 20 2a 2a 20 61 6e 64 reading. ** and
150f5 20 68 61 73 20 74 68 65 20 72 65 61 64 2d 75 6e has the read-un
150f6 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 20 73 committed flag s
150f7 65 74 2c 20 74 68 65 6e 20 6e 6f 20 6c 6f 63 6b et, then no lock
150f8 20 69 73 20 72 65 71 75 69 72 65 64 2e 20 0a 20 is required. .
150f9 20 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 ** Return true
150fa 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 20 20 2a immediately.. *
150fb 2f 0a 20 20 69 66 28 20 28 70 42 74 72 65 65 2d /. if( (pBtree-
150fc 3e 73 68 61 72 61 62 6c 65 3d 3d 30 29 0a 20 20 >sharable==0).
150fd 20 7c 7c 20 28 65 4c 6f 63 6b 54 79 70 65 3d 3d || (eLockType==
150fe 52 45 41 44 5f 4c 4f 43 4b 20 26 26 20 28 70 42 READ_LOCK && (pB
150ff 74 72 65 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 tree->db->flags
15100 26 20 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 & SQLITE_ReadUnc
15101 6f 6d 6d 69 74 74 65 64 29 29 0a 20 20 29 7b 0a ommitted)). ){.
15102 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 return 1;.
15103 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 }.. /* If the c
15104 6c 69 65 6e 74 20 69 73 20 72 65 61 64 69 6e 67 lient is reading
15105 20 20 6f 72 20 77 72 69 74 69 6e 67 20 61 6e 20 or writing an
15106 69 6e 64 65 78 20 61 6e 64 20 74 68 65 20 73 63 index and the sc
15107 68 65 6d 61 20 69 73 0a 20 20 2a 2a 20 6e 6f 74 hema is. ** not
15108 20 6c 6f 61 64 65 64 2c 20 74 68 65 6e 20 69 74 loaded, then it
15109 20 69 73 20 74 6f 6f 20 64 69 66 66 69 63 75 6c is too difficul
1510a 74 20 74 6f 20 61 63 74 75 61 6c 6c 79 20 63 68 t to actually ch
1510b 65 63 6b 20 74 6f 20 73 65 65 20 69 66 0a 20 20 eck to see if.
1510c 2a 2a 20 74 68 65 20 63 6f 72 72 65 63 74 20 6c ** the correct l
1510d 6f 63 6b 73 20 61 72 65 20 68 65 6c 64 2e 20 20 ocks are held.
1510e 53 6f 20 64 6f 20 6e 6f 74 20 62 6f 74 68 65 72 So do not bother
1510f 20 2d 20 6a 75 73 74 20 72 65 74 75 72 6e 20 74 - just return t
15110 72 75 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 63 rue.. ** This c
15111 61 73 65 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6d ase does not com
15112 65 20 75 70 20 76 65 72 79 20 6f 66 74 65 6e 20 e up very often
15113 61 6e 79 68 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 anyhow.. */. i
15114 66 28 20 69 73 49 6e 64 65 78 20 26 26 20 28 21 f( isIndex && (!
15115 70 53 63 68 65 6d 61 20 7c 7c 20 28 70 53 63 68 pSchema || (pSch
15116 65 6d 61 2d 3e 66 6c 61 67 73 26 44 42 5f 53 63 ema->flags&DB_Sc
15117 68 65 6d 61 4c 6f 61 64 65 64 29 3d 3d 30 29 20 hemaLoaded)==0)
15118 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b ){. return 1;
15119 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 67 75 72 . }.. /* Figur
1511a 65 20 6f 75 74 20 74 68 65 20 72 6f 6f 74 2d 70 e out the root-p
1511b 61 67 65 20 74 68 61 74 20 74 68 65 20 6c 6f 63 age that the loc
1511c 6b 20 73 68 6f 75 6c 64 20 62 65 20 68 65 6c 64 k should be held
1511d 20 6f 6e 2e 20 46 6f 72 20 74 61 62 6c 65 0a 20 on. For table.
1511e 20 2a 2a 20 62 2d 74 72 65 65 73 2c 20 74 68 69 ** b-trees, thi
1511f 73 20 69 73 20 6a 75 73 74 20 74 68 65 20 72 6f s is just the ro
15120 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 62 ot page of the b
15121 2d 74 72 65 65 20 62 65 69 6e 67 20 72 65 61 64 -tree being read
15122 20 6f 72 0a 20 20 2a 2a 20 77 72 69 74 74 65 6e or. ** written
15123 2e 20 46 6f 72 20 69 6e 64 65 78 20 62 2d 74 72 . For index b-tr
15124 65 65 73 2c 20 69 74 20 69 73 20 74 68 65 20 72 ees, it is the r
15125 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 oot page of the
15126 61 73 73 6f 63 69 61 74 65 64 0a 20 20 2a 2a 20 associated. **
15127 74 61 62 6c 65 2e 20 20 2a 2f 0a 20 20 69 66 28 table. */. if(
15128 20 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 isIndex ){.
15129 48 61 73 68 45 6c 65 6d 20 2a 70 3b 0a 20 20 20 HashElem *p;.
1512a 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 48 61 73 for(p=sqliteHas
1512b 68 46 69 72 73 74 28 26 70 53 63 68 65 6d 61 2d hFirst(&pSchema-
1512c 3e 69 64 78 48 61 73 68 29 3b 20 70 3b 20 70 3d >idxHash); p; p=
1512d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70 sqliteHashNext(p
1512e 29 29 7b 0a 20 20 20 20 20 20 49 6e 64 65 78 20 )){. Index
1512f 2a 70 49 64 78 20 3d 20 28 49 6e 64 65 78 20 2a *pIdx = (Index *
15130 29 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 )sqliteHashData(
15131 70 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 p);. if( pI
15132 64 78 2d 3e 74 6e 75 6d 3d 3d 28 69 6e 74 29 69 dx->tnum==(int)i
15133 52 6f 6f 74 20 29 7b 0a 20 20 20 20 20 20 20 20 Root ){.
15134 69 54 61 62 20 3d 20 70 49 64 78 2d 3e 70 54 61 iTab = pIdx->pTa
15135 62 6c 65 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20 20 ble->tnum;.
15136 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 }. }. }else
15137 7b 0a 20 20 20 20 69 54 61 62 20 3d 20 69 52 6f {. iTab = iRo
15138 6f 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 ot;. }.. /* Se
15139 61 72 63 68 20 66 6f 72 20 74 68 65 20 72 65 71 arch for the req
1513a 75 69 72 65 64 20 6c 6f 63 6b 2e 20 45 69 74 68 uired lock. Eith
1513b 65 72 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 er a write-lock
1513c 6f 6e 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 on root-page iTa
1513d 62 2c 20 61 20 0a 20 20 2a 2a 20 77 72 69 74 65 b, a . ** write
1513e 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 73 63 68 -lock on the sch
1513f 65 6d 61 20 74 61 62 6c 65 2c 20 6f 72 20 28 69 ema table, or (i
15140 66 20 74 68 65 20 63 6c 69 65 6e 74 20 69 73 20 f the client is
15141 72 65 61 64 69 6e 67 29 20 61 0a 20 20 2a 2a 20 reading) a. **
15142 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 69 54 61 read-lock on iTa
15143 62 20 77 69 6c 6c 20 73 75 66 66 69 63 65 2e 20 b will suffice.
15144 52 65 74 75 72 6e 20 31 20 69 66 20 61 6e 79 20 Return 1 if any
15145 6f 66 20 74 68 65 73 65 20 61 72 65 20 66 6f 75 of these are fou
15146 6e 64 2e 20 20 2a 2f 0a 20 20 66 6f 72 28 70 4c nd. */. for(pL
15147 6f 63 6b 3d 70 42 74 72 65 65 2d 3e 70 42 74 2d ock=pBtree->pBt-
15148 3e 70 4c 6f 63 6b 3b 20 70 4c 6f 63 6b 3b 20 70 >pLock; pLock; p
15149 4c 6f 63 6b 3d 70 4c 6f 63 6b 2d 3e 70 4e 65 78 Lock=pLock->pNex
1514a 74 29 7b 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 t){. if( pLoc
1514b 6b 2d 3e 70 42 74 72 65 65 3d 3d 70 42 74 72 65 k->pBtree==pBtre
1514c 65 20 0a 20 20 20 20 20 26 26 20 28 70 4c 6f 63 e . && (pLoc
1514d 6b 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 20 k->iTable==iTab
1514e 7c 7c 20 28 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b || (pLock->eLock
1514f 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 26 26 20 ==WRITE_LOCK &&
15150 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c 65 3d 3d 31 pLock->iTable==1
15151 29 29 0a 20 20 20 20 20 26 26 20 70 4c 6f 63 6b )). && pLock
15152 2d 3e 65 4c 6f 63 6b 3e 3d 65 4c 6f 63 6b 54 79 ->eLock>=eLockTy
15153 70 65 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 pe . ){.
15154 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d return 1;. }
15155 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 61 69 6c 65 . }.. /* Faile
15156 64 20 74 6f 20 66 69 6e 64 20 74 68 65 20 72 65 d to find the re
15157 71 75 69 72 65 64 20 6c 6f 63 6b 2e 20 2a 2f 0a quired lock. */.
15158 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 return 0;.}.#e
15159 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 44 ndif /* SQLITE_D
1515a 45 42 55 47 20 2a 2f 0a 0a 23 69 66 64 65 66 20 EBUG */..#ifdef
1515b 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a SQLITE_DEBUG./*.
1515c 2a 2a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 **** This functi
1515d 6f 6e 20 6d 61 79 20 62 65 20 75 73 65 64 20 61 on may be used a
1515e 73 20 70 61 72 74 20 6f 66 20 61 73 73 65 72 74 s part of assert
1515f 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e () statements on
15160 6c 79 2e 20 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 52 ly. ****.**.** R
15161 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 69 74 eturn true if it
15162 20 77 6f 75 6c 64 20 62 65 20 69 6c 6c 65 67 61 would be illega
15163 6c 20 66 6f 72 20 70 42 74 72 65 65 20 74 6f 20 l for pBtree to
15164 77 72 69 74 65 20 69 6e 74 6f 20 74 68 65 0a 2a write into the.*
15165 2a 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 * table or index
15166 20 72 6f 6f 74 65 64 20 61 74 20 69 52 6f 6f 74 rooted at iRoot
15167 20 62 65 63 61 75 73 65 20 6f 74 68 65 72 20 73 because other s
15168 68 61 72 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e hared connection
15169 73 20 61 72 65 0a 2a 2a 20 73 69 6d 75 6c 74 61 s are.** simulta
1516a 6e 65 6f 75 73 6c 79 20 72 65 61 64 69 6e 67 20 neously reading
1516b 74 68 61 74 20 73 61 6d 65 20 74 61 62 6c 65 20 that same table
1516c 6f 72 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 or index..**.**
1516d 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 66 6f It is illegal fo
1516e 72 20 70 42 74 72 65 65 20 74 6f 20 77 72 69 74 r pBtree to writ
1516f 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 e if some other
15170 42 74 72 65 65 20 6f 62 6a 65 63 74 20 74 68 61 Btree object tha
15171 74 0a 2a 2a 20 73 68 61 72 65 73 20 74 68 65 20 t.** shares the
15172 73 61 6d 65 20 42 74 53 68 61 72 65 64 20 6f 62 same BtShared ob
15173 6a 65 63 74 20 69 73 20 63 75 72 72 65 6e 74 6c ject is currentl
15174 79 20 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69 y reading or wri
15175 74 69 6e 67 0a 2a 2a 20 74 68 65 20 69 52 6f 6f ting.** the iRoo
15176 74 20 74 61 62 6c 65 2e 20 20 45 78 63 65 70 74 t table. Except
15177 2c 20 69 66 20 74 68 65 20 6f 74 68 65 72 20 42 , if the other B
15178 74 72 65 65 20 6f 62 6a 65 63 74 20 68 61 73 20 tree object has
15179 74 68 65 0a 2a 2a 20 72 65 61 64 2d 75 6e 63 6f the.** read-unco
1517a 6d 6d 69 74 74 65 64 20 66 6c 61 67 20 73 65 74 mmitted flag set
1517b 2c 20 74 68 65 6e 20 69 74 20 69 73 20 4f 4b 20 , then it is OK
1517c 66 6f 72 20 74 68 65 20 6f 74 68 65 72 20 6f 62 for the other ob
1517d 6a 65 63 74 20 74 6f 0a 2a 2a 20 68 61 76 65 20 ject to.** have
1517e 61 20 72 65 61 64 20 63 75 72 73 6f 72 2e 0a 2a a read cursor..*
1517f 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 *.** For example
15180 2c 20 62 65 66 6f 72 65 20 77 72 69 74 69 6e 67 , before writing
15181 20 74 6f 20 61 6e 79 20 70 61 72 74 20 6f 66 20 to any part of
15182 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 the table or ind
15183 65 78 0a 2a 2a 20 72 6f 6f 74 65 64 20 61 74 20 ex.** rooted at
15184 70 61 67 65 20 69 52 6f 6f 74 2c 20 6f 6e 65 20 page iRoot, one
15185 73 68 6f 75 6c 64 20 63 61 6c 6c 3a 0a 2a 2a 0a should call:.**.
15186 2a 2a 20 20 20 20 61 73 73 65 72 74 28 20 21 68 ** assert( !h
15187 61 73 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 asReadConflicts(
15188 70 42 74 72 65 65 2c 20 69 52 6f 6f 74 29 20 29 pBtree, iRoot) )
15189 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ;.*/.static int
1518a 68 61 73 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 hasReadConflicts
1518b 28 42 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20 (Btree *pBtree,
1518c 50 67 6e 6f 20 69 52 6f 6f 74 29 7b 0a 20 20 42 Pgno iRoot){. B
1518d 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 66 6f tCursor *p;. fo
1518e 72 28 70 3d 70 42 74 72 65 65 2d 3e 70 42 74 2d r(p=pBtree->pBt-
1518f 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 >pCursor; p; p=p
15190 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 ->pNext){. if
15191 28 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 69 ( p->pgnoRoot==i
15192 52 6f 6f 74 20 0a 20 20 20 20 20 26 26 20 70 2d Root . && p-
15193 3e 70 42 74 72 65 65 21 3d 70 42 74 72 65 65 0a >pBtree!=pBtree.
15194 20 20 20 20 20 26 26 20 30 3d 3d 28 70 2d 3e 70 && 0==(p->p
15195 42 74 72 65 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 Btree->db->flags
15196 20 26 20 53 51 4c 49 54 45 5f 52 65 61 64 55 6e & SQLITE_ReadUn
15197 63 6f 6d 6d 69 74 74 65 64 29 0a 20 20 20 20 29 committed). )
15198 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 {. return 1
15199 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
1519a 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 turn 0;.}.#endif
1519b 20 20 20 20 2f 2a 20 23 69 66 64 65 66 20 53 51 /* #ifdef SQ
1519c 4c 49 54 45 5f 44 45 42 55 47 20 2a 2f 0a 0a 2f LITE_DEBUG */../
1519d 2a 0a 2a 2a 20 51 75 65 72 79 20 74 6f 20 73 65 *.** Query to se
1519e 65 20 69 66 20 42 74 72 65 65 20 68 61 6e 64 6c e if Btree handl
1519f 65 20 70 20 6d 61 79 20 6f 62 74 61 69 6e 20 61 e p may obtain a
151a0 20 6c 6f 63 6b 20 6f 66 20 74 79 70 65 20 65 4c lock of type eL
151a1 6f 63 6b 20 0a 2a 2a 20 28 52 45 41 44 5f 4c 4f ock .** (READ_LO
151a2 43 4b 20 6f 72 20 57 52 49 54 45 5f 4c 4f 43 4b CK or WRITE_LOCK
151a3 29 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 77 ) on the table w
151a4 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 ith root-page iT
151a5 61 62 2e 20 52 65 74 75 72 6e 0a 2a 2a 20 53 51 ab. Return.** SQ
151a6 4c 49 54 45 5f 4f 4b 20 69 66 20 74 68 65 20 6c LITE_OK if the l
151a7 6f 63 6b 20 6d 61 79 20 62 65 20 6f 62 74 61 69 ock may be obtai
151a8 6e 65 64 20 28 62 79 20 63 61 6c 6c 69 6e 67 0a ned (by calling.
151a9 2a 2a 20 73 65 74 53 68 61 72 65 64 43 61 63 68 ** setSharedCach
151aa 65 54 61 62 6c 65 4c 6f 63 6b 28 29 29 2c 20 6f eTableLock()), o
151ab 72 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 r SQLITE_LOCKED
151ac 69 66 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 if not..*/.stati
151ad 63 20 69 6e 74 20 71 75 65 72 79 53 68 61 72 65 c int queryShare
151ae 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 dCacheTableLock(
151af 42 74 72 65 65 20 2a 70 2c 20 50 67 6e 6f 20 69 Btree *p, Pgno i
151b0 54 61 62 2c 20 75 38 20 65 4c 6f 63 6b 29 7b 0a Tab, u8 eLock){.
151b1 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
151b2 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 74 4c 6f = p->pBt;. BtLo
151b3 63 6b 20 2a 70 49 74 65 72 3b 0a 0a 20 20 61 73 ck *pIter;.. as
151b4 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 sert( sqlite3Btr
151b5 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 eeHoldsMutex(p)
151b6 29 3b 0a 20 20 61 73 73 65 72 74 28 20 65 4c 6f );. assert( eLo
151b7 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c ck==READ_LOCK ||
151b8 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f eLock==WRITE_LO
151b9 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 CK );. assert(
151ba 70 2d 3e 64 62 21 3d 30 20 29 3b 0a 20 20 61 73 p->db!=0 );. as
151bb 73 65 72 74 28 20 21 28 70 2d 3e 64 62 2d 3e 66 sert( !(p->db->f
151bc 6c 61 67 73 26 53 51 4c 49 54 45 5f 52 65 61 64 lags&SQLITE_Read
151bd 55 6e 63 6f 6d 6d 69 74 74 65 64 29 7c 7c 65 4c Uncommitted)||eL
151be 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 7c ock==WRITE_LOCK|
151bf 7c 69 54 61 62 3d 3d 31 20 29 3b 0a 20 20 0a 20 |iTab==1 );. .
151c0 20 2f 2a 20 49 66 20 72 65 71 75 65 73 74 69 6e /* If requestin
151c1 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 2c 20 g a write-lock,
151c2 74 68 65 6e 20 74 68 65 20 42 74 72 65 65 20 6d then the Btree m
151c3 75 73 74 20 68 61 76 65 20 61 6e 20 6f 70 65 6e ust have an open
151c4 20 77 72 69 74 65 0a 20 20 2a 2a 20 74 72 61 6e write. ** tran
151c5 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 69 73 20 saction on this
151c6 66 69 6c 65 2e 20 41 6e 64 2c 20 6f 62 76 69 6f file. And, obvio
151c7 75 73 6c 79 2c 20 66 6f 72 20 74 68 69 73 20 74 usly, for this t
151c8 6f 20 62 65 20 73 6f 20 74 68 65 72 65 20 0a 20 o be so there .
151c9 20 2a 2a 20 6d 75 73 74 20 62 65 20 61 6e 20 6f ** must be an o
151ca 70 65 6e 20 77 72 69 74 65 20 74 72 61 6e 73 61 pen write transa
151cb 63 74 69 6f 6e 20 6f 6e 20 74 68 65 20 66 69 6c ction on the fil
151cc 65 20 69 74 73 65 6c 66 2e 0a 20 20 2a 2f 0a 20 e itself.. */.
151cd 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d assert( eLock==
151ce 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 28 70 3d READ_LOCK || (p=
151cf 3d 70 42 74 2d 3e 70 57 72 69 74 65 72 20 26 26 =pBt->pWriter &&
151d0 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 p->inTrans==TRA
151d1 4e 53 5f 57 52 49 54 45 29 20 29 3b 0a 20 20 61 NS_WRITE) );. a
151d2 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 ssert( eLock==RE
151d3 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 42 74 2d 3e AD_LOCK || pBt->
151d4 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 inTransaction==T
151d5 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 RANS_WRITE );.
151d6 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 . /* This routi
151d7 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 ne is a no-op if
151d8 20 74 68 65 20 73 68 61 72 65 64 2d 63 61 63 68 the shared-cach
151d9 65 20 69 73 20 6e 6f 74 20 65 6e 61 62 6c 65 64 e is not enabled
151da 20 2a 2f 0a 20 20 69 66 28 20 21 70 2d 3e 73 68 */. if( !p->sh
151db 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 72 65 arable ){. re
151dc 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
151dd 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 73 6f 6d }.. /* If som
151de 65 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 e other connecti
151df 6f 6e 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 6e on is holding an
151e0 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2c exclusive lock,
151e1 20 74 68 65 0a 20 20 2a 2a 20 72 65 71 75 65 73 the. ** reques
151e2 74 65 64 20 6c 6f 63 6b 20 6d 61 79 20 6e 6f 74 ted lock may not
151e3 20 62 65 20 6f 62 74 61 69 6e 65 64 2e 0a 20 20 be obtained..
151e4 2a 2f 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 57 */. if( pBt->pW
151e5 72 69 74 65 72 21 3d 70 20 26 26 20 70 42 74 2d riter!=p && pBt-
151e6 3e 69 73 45 78 63 6c 75 73 69 76 65 20 29 7b 0a >isExclusive ){.
151e7 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 sqlite3Conne
151e8 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 70 2d 3e ctionBlocked(p->
151e9 64 62 2c 20 70 42 74 2d 3e 70 57 72 69 74 65 72 db, pBt->pWriter
151ea 2d 3e 64 62 29 3b 0a 20 20 20 20 72 65 74 75 72 ->db);. retur
151eb 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f n SQLITE_LOCKED_
151ec 53 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20 7d SHAREDCACHE;. }
151ed 0a 0a 20 20 66 6f 72 28 70 49 74 65 72 3d 70 42 .. for(pIter=pB
151ee 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49 74 65 72 3b t->pLock; pIter;
151ef 20 70 49 74 65 72 3d 70 49 74 65 72 2d 3e 70 4e pIter=pIter->pN
151f0 65 78 74 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 ext){. /* The
151f1 20 63 6f 6e 64 69 74 69 6f 6e 20 28 70 49 74 65 condition (pIte
151f2 72 2d 3e 65 4c 6f 63 6b 21 3d 65 4c 6f 63 6b 29 r->eLock!=eLock)
151f3 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e in the followin
151f4 67 20 69 66 28 2e 2e 2e 29 20 0a 20 20 20 20 2a g if(...) . *
151f5 2a 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 * statement is a
151f6 20 73 69 6d 70 6c 69 66 69 63 61 74 69 6f 6e 20 simplification
151f7 6f 66 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a of:. **. *
151f8 2a 20 20 20 28 65 4c 6f 63 6b 3d 3d 57 52 49 54 * (eLock==WRIT
151f9 45 5f 4c 4f 43 4b 20 7c 7c 20 70 49 74 65 72 2d E_LOCK || pIter-
151fa 3e 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f >eLock==WRITE_LO
151fb 43 4b 29 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a CK). **. *
151fc 2a 20 73 69 6e 63 65 20 77 65 20 6b 6e 6f 77 20 * since we know
151fd 74 68 61 74 20 69 66 20 65 4c 6f 63 6b 3d 3d 57 that if eLock==W
151fe 52 49 54 45 5f 4c 4f 43 4b 2c 20 74 68 65 6e 20 RITE_LOCK, then
151ff 6e 6f 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 no other connect
15200 69 6f 6e 0a 20 20 20 20 2a 2a 20 6d 61 79 20 68 ion. ** may h
15201 6f 6c 64 20 61 20 57 52 49 54 45 5f 4c 4f 43 4b old a WRITE_LOCK
15202 20 6f 6e 20 61 6e 79 20 74 61 62 6c 65 20 69 6e on any table in
15203 20 74 68 69 73 20 66 69 6c 65 20 28 73 69 6e 63 this file (sinc
15204 65 20 74 68 65 72 65 20 63 61 6e 0a 20 20 20 20 e there can.
15205 2a 2a 20 6f 6e 6c 79 20 62 65 20 61 20 73 69 6e ** only be a sin
15206 67 6c 65 20 77 72 69 74 65 72 29 2e 0a 20 20 20 gle writer)..
15207 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 */. assert(
15208 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 52 45 pIter->eLock==RE
15209 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 49 74 65 72 AD_LOCK || pIter
1520a 2d 3e 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c ->eLock==WRITE_L
1520b 4f 43 4b 20 29 3b 0a 20 20 20 20 61 73 73 65 72 OCK );. asser
1520c 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c t( eLock==READ_L
1520d 4f 43 4b 20 7c 7c 20 70 49 74 65 72 2d 3e 70 42 OCK || pIter->pB
1520e 74 72 65 65 3d 3d 70 20 7c 7c 20 70 49 74 65 72 tree==p || pIter
1520f 2d 3e 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f ->eLock==READ_LO
15210 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 70 49 74 CK);. if( pIt
15211 65 72 2d 3e 70 42 74 72 65 65 21 3d 70 20 26 26 er->pBtree!=p &&
15212 20 70 49 74 65 72 2d 3e 69 54 61 62 6c 65 3d 3d pIter->iTable==
15213 69 54 61 62 20 26 26 20 70 49 74 65 72 2d 3e 65 iTab && pIter->e
15214 4c 6f 63 6b 21 3d 65 4c 6f 63 6b 20 29 7b 0a 20 Lock!=eLock ){.
15215 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e sqlite3Conn
15216 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 70 2d ectionBlocked(p-
15217 3e 64 62 2c 20 70 49 74 65 72 2d 3e 70 42 74 72 >db, pIter->pBtr
15218 65 65 2d 3e 64 62 29 3b 0a 20 20 20 20 20 20 69 ee->db);. i
15219 66 28 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f f( eLock==WRITE_
1521a 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 LOCK ){.
1521b 61 73 73 65 72 74 28 20 70 3d 3d 70 42 74 2d 3e assert( p==pBt->
1521c 70 57 72 69 74 65 72 20 29 3b 0a 20 20 20 20 20 pWriter );.
1521d 20 20 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e pBt->isPendin
1521e 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 g = 1;. }.
1521f 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
15220 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 TE_LOCKED_SHARED
15221 43 41 43 48 45 3b 0a 20 20 20 20 7d 0a 20 20 7d CACHE;. }. }
15222 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
15223 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a _OK;.}.#endif /*
15224 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 !SQLITE_OMIT_SH
15225 41 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 ARED_CACHE */..#
15226 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
15227 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a IT_SHARED_CACHE.
15228 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6c 6f 63 6b /*.** Add a lock
15229 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69 on the table wi
1522a 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 th root-page iTa
1522b 62 6c 65 20 74 6f 20 74 68 65 20 73 68 61 72 65 ble to the share
1522c 64 2d 62 74 72 65 65 20 75 73 65 64 0a 2a 2a 20 d-btree used.**
1522d 62 79 20 42 74 72 65 65 20 68 61 6e 64 6c 65 20 by Btree handle
1522e 70 2e 20 50 61 72 61 6d 65 74 65 72 20 65 4c 6f p. Parameter eLo
1522f 63 6b 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 ck must be eithe
15230 72 20 52 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 0a r READ_LOCK or .
15231 2a 2a 20 57 52 49 54 45 5f 4c 4f 43 4b 2e 0a 2a ** WRITE_LOCK..*
15232 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
15233 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 65 20 66 on assumes the f
15234 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 ollowing:.**.**
15235 20 20 28 61 29 20 54 68 65 20 73 70 65 63 69 66 (a) The specif
15236 69 65 64 20 42 74 72 65 65 20 6f 62 6a 65 63 74 ied Btree object
15237 20 70 20 69 73 20 63 6f 6e 6e 65 63 74 65 64 20 p is connected
15238 74 6f 20 61 20 73 68 61 72 61 62 6c 65 0a 2a 2a to a sharable.**
15239 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65 20 database
1523a 28 6f 6e 65 20 77 69 74 68 20 74 68 65 20 42 74 (one with the Bt
1523b 53 68 61 72 65 64 2e 73 68 61 72 61 62 6c 65 20 Shared.sharable
1523c 66 6c 61 67 20 73 65 74 29 2c 20 61 6e 64 0a 2a flag set), and.*
1523d 2a 0a 2a 2a 20 20 20 28 62 29 20 4e 6f 20 6f 74 *.** (b) No ot
1523e 68 65 72 20 42 74 72 65 65 20 6f 62 6a 65 63 74 her Btree object
1523f 73 20 68 6f 6c 64 20 61 20 6c 6f 63 6b 20 74 68 s hold a lock th
15240 61 74 20 63 6f 6e 66 6c 69 63 74 73 0a 2a 2a 20 at conflicts.**
15241 20 20 20 20 20 20 77 69 74 68 20 74 68 65 20 72 with the r
15242 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 20 28 69 equested lock (i
15243 2e 65 2e 20 71 75 65 72 79 53 68 61 72 65 64 43 .e. querySharedC
15244 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 20 acheTableLock()
15245 68 61 73 0a 2a 2a 20 20 20 20 20 20 20 61 6c 72 has.** alr
15246 65 61 64 79 20 62 65 65 6e 20 63 61 6c 6c 65 64 eady been called
15247 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20 53 51 and returned SQ
15248 4c 49 54 45 5f 4f 4b 29 2e 0a 2a 2a 0a 2a 2a 20 LITE_OK)..**.**
15249 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
1524a 75 72 6e 65 64 20 69 66 20 74 68 65 20 6c 6f 63 urned if the loc
1524b 6b 20 69 73 20 61 64 64 65 64 20 73 75 63 63 65 k is added succe
1524c 73 73 66 75 6c 6c 79 2e 20 53 51 4c 49 54 45 5f ssfully. SQLITE_
1524d 4e 4f 4d 45 4d 20 0a 2a 2a 20 69 73 20 72 65 74 NOMEM .** is ret
1524e 75 72 6e 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f urned if a mallo
1524f 63 20 61 74 74 65 6d 70 74 20 66 61 69 6c 73 2e c attempt fails.
15250 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 .*/.static int s
15251 65 74 53 68 61 72 65 64 43 61 63 68 65 54 61 62 etSharedCacheTab
15252 6c 65 4c 6f 63 6b 28 42 74 72 65 65 20 2a 70 2c leLock(Btree *p,
15253 20 50 67 6e 6f 20 69 54 61 62 6c 65 2c 20 75 38 Pgno iTable, u8
15254 20 65 4c 6f 63 6b 29 7b 0a 20 20 42 74 53 68 61 eLock){. BtSha
15255 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
15256 74 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f t;. BtLock *pLo
15257 63 6b 20 3d 20 30 3b 0a 20 20 42 74 4c 6f 63 6b ck = 0;. BtLock
15258 20 2a 70 49 74 65 72 3b 0a 0a 20 20 61 73 73 65 *pIter;.. asse
15259 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 rt( sqlite3Btree
1525a 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b HoldsMutex(p) );
1525b 0a 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b . assert( eLock
1525c 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 65 ==READ_LOCK || e
1525d 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b Lock==WRITE_LOCK
1525e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d );. assert( p-
1525f 3e 64 62 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 >db!=0 );.. /*
15260 41 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 74 A connection wit
15261 68 20 74 68 65 20 72 65 61 64 2d 75 6e 63 6f 6d h the read-uncom
15262 6d 69 74 74 65 64 20 66 6c 61 67 20 73 65 74 20 mitted flag set
15263 77 69 6c 6c 20 6e 65 76 65 72 20 74 72 79 20 74 will never try t
15264 6f 0a 20 20 2a 2a 20 6f 62 74 61 69 6e 20 61 20 o. ** obtain a
15265 72 65 61 64 2d 6c 6f 63 6b 20 75 73 69 6e 67 20 read-lock using
15266 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 54 this function. T
15267 68 65 20 6f 6e 6c 79 20 72 65 61 64 2d 6c 6f 63 he only read-loc
15268 6b 20 6f 62 74 61 69 6e 65 64 0a 20 20 2a 2a 20 k obtained. **
15269 62 79 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 by a connection
1526a 69 6e 20 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 74 in read-uncommit
1526b 74 65 64 20 6d 6f 64 65 20 69 73 20 6f 6e 20 74 ted mode is on t
1526c 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 he sqlite_master
1526d 20 0a 20 20 2a 2a 20 74 61 62 6c 65 2c 20 61 6e . ** table, an
1526e 64 20 74 68 61 74 20 6c 6f 63 6b 20 69 73 20 6f d that lock is o
1526f 62 74 61 69 6e 65 64 20 69 6e 20 42 74 72 65 65 btained in Btree
15270 42 65 67 69 6e 54 72 61 6e 73 28 29 2e 20 20 2a BeginTrans(). *
15271 2f 0a 20 20 61 73 73 65 72 74 28 20 30 3d 3d 28 /. assert( 0==(
15272 70 2d 3e 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c p->db->flags&SQL
15273 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 ITE_ReadUncommit
15274 74 65 64 29 20 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 ted) || eLock==W
15275 52 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 RITE_LOCK );..
15276 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e /* This function
15277 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 62 65 20 should only be
15278 63 61 6c 6c 65 64 20 6f 6e 20 61 20 73 68 61 72 called on a shar
15279 61 62 6c 65 20 62 2d 74 72 65 65 20 61 66 74 65 able b-tree afte
1527a 72 20 69 74 20 0a 20 20 2a 2a 20 68 61 73 20 62 r it . ** has b
1527b 65 65 6e 20 64 65 74 65 72 6d 69 6e 65 64 20 74 een determined t
1527c 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 62 2d 74 hat no other b-t
1527d 72 65 65 20 68 6f 6c 64 73 20 61 20 63 6f 6e 66 ree holds a conf
1527e 6c 69 63 74 69 6e 67 20 6c 6f 63 6b 2e 20 20 2a licting lock. *
1527f 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73 /. assert( p->s
15280 68 61 72 61 62 6c 65 20 29 3b 0a 20 20 61 73 73 harable );. ass
15281 65 72 74 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d ert( SQLITE_OK==
15282 71 75 65 72 79 53 68 61 72 65 64 43 61 63 68 65 querySharedCache
15283 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 54 61 TableLock(p, iTa
15284 62 6c 65 2c 20 65 4c 6f 63 6b 29 20 29 3b 0a 0a ble, eLock) );..
15285 20 20 2f 2a 20 46 69 72 73 74 20 73 65 61 72 63 /* First searc
15286 68 20 74 68 65 20 6c 69 73 74 20 66 6f 72 20 61 h the list for a
15287 6e 20 65 78 69 73 74 69 6e 67 20 6c 6f 63 6b 20 n existing lock
15288 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2e 20 2a on this table. *
15289 2f 0a 20 20 66 6f 72 28 70 49 74 65 72 3d 70 42 /. for(pIter=pB
1528a 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49 74 65 72 3b t->pLock; pIter;
1528b 20 70 49 74 65 72 3d 70 49 74 65 72 2d 3e 70 4e pIter=pIter->pN
1528c 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 49 ext){. if( pI
1528d 74 65 72 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 ter->iTable==iTa
1528e 62 6c 65 20 26 26 20 70 49 74 65 72 2d 3e 70 42 ble && pIter->pB
1528f 74 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 tree==p ){.
15290 20 70 4c 6f 63 6b 20 3d 20 70 49 74 65 72 3b 0a pLock = pIter;.
15291 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
15292 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 }. }.. /* If
15293 74 68 65 20 61 62 6f 76 65 20 73 65 61 72 63 68 the above search
15294 20 64 69 64 20 6e 6f 74 20 66 69 6e 64 20 61 20 did not find a
15295 42 74 4c 6f 63 6b 20 73 74 72 75 63 74 20 61 73 BtLock struct as
15296 73 6f 63 69 61 74 69 6e 67 20 42 74 72 65 65 20 sociating Btree
15297 70 0a 20 20 2a 2a 20 77 69 74 68 20 74 61 62 6c p. ** with tabl
15298 65 20 69 54 61 62 6c 65 2c 20 61 6c 6c 6f 63 61 e iTable, alloca
15299 74 65 20 6f 6e 65 20 61 6e 64 20 6c 69 6e 6b 20 te one and link
1529a 69 74 20 69 6e 74 6f 20 74 68 65 20 6c 69 73 74 it into the list
1529b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 4c .. */. if( !pL
1529c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b ock ){. pLock
1529d 20 3d 20 28 42 74 4c 6f 63 6b 20 2a 29 73 71 6c = (BtLock *)sql
1529e 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 ite3MallocZero(s
1529f 69 7a 65 6f 66 28 42 74 4c 6f 63 6b 29 29 3b 0a izeof(BtLock));.
152a0 20 20 20 20 69 66 28 20 21 70 4c 6f 63 6b 20 29 if( !pLock )
152a1 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
152a2 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
152a3 20 7d 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 69 54 }. pLock->iT
152a4 61 62 6c 65 20 3d 20 69 54 61 62 6c 65 3b 0a 20 able = iTable;.
152a5 20 20 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 pLock->pBtree
152a6 20 3d 20 70 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d = p;. pLock-
152a7 3e 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 4c >pNext = pBt->pL
152a8 6f 63 6b 3b 0a 20 20 20 20 70 42 74 2d 3e 70 4c ock;. pBt->pL
152a9 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 7d ock = pLock;. }
152aa 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 42 .. /* Set the B
152ab 74 4c 6f 63 6b 2e 65 4c 6f 63 6b 20 76 61 72 69 tLock.eLock vari
152ac 61 62 6c 65 20 74 6f 20 74 68 65 20 6d 61 78 69 able to the maxi
152ad 6d 75 6d 20 6f 66 20 74 68 65 20 63 75 72 72 65 mum of the curre
152ae 6e 74 20 6c 6f 63 6b 0a 20 20 2a 2a 20 61 6e 64 nt lock. ** and
152af 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c the requested l
152b0 6f 63 6b 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 ock. This means
152b1 69 66 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 if a write-lock
152b2 77 61 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64 was already held
152b3 0a 20 20 2a 2a 20 61 6e 64 20 61 20 72 65 61 64 . ** and a read
152b4 2d 6c 6f 63 6b 20 72 65 71 75 65 73 74 65 64 2c -lock requested,
152b5 20 77 65 20 64 6f 6e 27 74 20 69 6e 63 6f 72 72 we don't incorr
152b6 65 63 74 6c 79 20 64 6f 77 6e 67 72 61 64 65 20 ectly downgrade
152b7 74 68 65 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 the lock.. */.
152b8 20 61 73 73 65 72 74 28 20 57 52 49 54 45 5f 4c assert( WRITE_L
152b9 4f 43 4b 3e 52 45 41 44 5f 4c 4f 43 4b 20 29 3b OCK>READ_LOCK );
152ba 0a 20 20 69 66 28 20 65 4c 6f 63 6b 3e 70 4c 6f . if( eLock>pLo
152bb 63 6b 2d 3e 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 ck->eLock ){.
152bc 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 3d 20 pLock->eLock =
152bd 65 4c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20 72 65 eLock;. }.. re
152be 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
152bf 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c }.#endif /* !SQL
152c0 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
152c1 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 CACHE */..#ifnde
152c2 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 f SQLITE_OMIT_SH
152c3 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a ARED_CACHE./*.**
152c4 20 52 65 6c 65 61 73 65 20 61 6c 6c 20 74 68 65 Release all the
152c5 20 74 61 62 6c 65 20 6c 6f 63 6b 73 20 28 6c 6f table locks (lo
152c6 63 6b 73 20 6f 62 74 61 69 6e 65 64 20 76 69 61 cks obtained via
152c7 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 74 68 65 calls to.** the
152c8 20 73 65 74 53 68 61 72 65 64 43 61 63 68 65 54 setSharedCacheT
152c9 61 62 6c 65 4c 6f 63 6b 28 29 20 70 72 6f 63 65 ableLock() proce
152ca 64 75 72 65 29 20 68 65 6c 64 20 62 79 20 42 74 dure) held by Bt
152cb 72 65 65 20 6f 62 6a 65 63 74 20 70 2e 0a 2a 2a ree object p..**
152cc 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
152cd 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 42 n assumes that B
152ce 74 72 65 65 20 70 20 68 61 73 20 61 6e 20 6f 70 tree p has an op
152cf 65 6e 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 en read or write
152d0 20 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e .** transaction
152d1 2e 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 . If it does not
152d2 2c 20 74 68 65 6e 20 74 68 65 20 42 74 53 68 61 , then the BtSha
152d3 72 65 64 2e 69 73 50 65 6e 64 69 6e 67 20 76 61 red.isPending va
152d4 72 69 61 62 6c 65 0a 2a 2a 20 6d 61 79 20 62 65 riable.** may be
152d5 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 63 6c 65 incorrectly cle
152d6 61 72 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ared..*/.static
152d7 76 6f 69 64 20 63 6c 65 61 72 41 6c 6c 53 68 61 void clearAllSha
152d8 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 redCacheTableLoc
152d9 6b 73 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 ks(Btree *p){.
152da 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
152db 70 2d 3e 70 42 74 3b 0a 20 20 42 74 4c 6f 63 6b p->pBt;. BtLock
152dc 20 2a 2a 70 70 49 74 65 72 20 3d 20 26 70 42 74 **ppIter = &pBt
152dd 2d 3e 70 4c 6f 63 6b 3b 0a 0a 20 20 61 73 73 65 ->pLock;.. asse
152de 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 rt( sqlite3Btree
152df 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b HoldsMutex(p) );
152e0 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 . assert( p->sh
152e1 61 72 61 62 6c 65 20 7c 7c 20 30 3d 3d 2a 70 70 arable || 0==*pp
152e2 49 74 65 72 20 29 3b 0a 20 20 61 73 73 65 72 74 Iter );. assert
152e3 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3e 30 20 29 ( p->inTrans>0 )
152e4 3b 0a 0a 20 20 77 68 69 6c 65 28 20 2a 70 70 49 ;.. while( *ppI
152e5 74 65 72 20 29 7b 0a 20 20 20 20 42 74 4c 6f 63 ter ){. BtLoc
152e6 6b 20 2a 70 4c 6f 63 6b 20 3d 20 2a 70 70 49 74 k *pLock = *ppIt
152e7 65 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 er;. assert(
152e8 70 42 74 2d 3e 69 73 45 78 63 6c 75 73 69 76 65 pBt->isExclusive
152e9 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 70 57 72 69 ==0 || pBt->pWri
152ea 74 65 72 3d 3d 70 4c 6f 63 6b 2d 3e 70 42 74 72 ter==pLock->pBtr
152eb 65 65 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 ee );. assert
152ec 28 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 2d ( pLock->pBtree-
152ed 3e 69 6e 54 72 61 6e 73 3e 3d 70 4c 6f 63 6b 2d >inTrans>=pLock-
152ee 3e 65 4c 6f 63 6b 20 29 3b 0a 20 20 20 20 69 66 >eLock );. if
152ef 28 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 3d ( pLock->pBtree=
152f0 3d 70 20 29 7b 0a 20 20 20 20 20 20 2a 70 70 49 =p ){. *ppI
152f1 74 65 72 20 3d 20 70 4c 6f 63 6b 2d 3e 70 4e 65 ter = pLock->pNe
152f2 78 74 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 xt;. assert
152f3 28 20 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c 65 21 ( pLock->iTable!
152f4 3d 31 20 7c 7c 20 70 4c 6f 63 6b 3d 3d 26 70 2d =1 || pLock==&p-
152f5 3e 6c 6f 63 6b 20 29 3b 0a 20 20 20 20 20 20 69 >lock );. i
152f6 66 28 20 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c 65 f( pLock->iTable
152f7 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 73 !=1 ){. s
152f8 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4c 6f 63 qlite3_free(pLoc
152f9 6b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 k);. }.
152fa 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 70 49 }else{. ppI
152fb 74 65 72 20 3d 20 26 70 4c 6f 63 6b 2d 3e 70 4e ter = &pLock->pN
152fc 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a ext;. }. }..
152fd 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 assert( pBt->i
152fe 73 50 65 6e 64 69 6e 67 3d 3d 30 20 7c 7c 20 70 sPending==0 || p
152ff 42 74 2d 3e 70 57 72 69 74 65 72 20 29 3b 0a 20 Bt->pWriter );.
15300 20 69 66 28 20 70 42 74 2d 3e 70 57 72 69 74 65 if( pBt->pWrite
15301 72 3d 3d 70 20 29 7b 0a 20 20 20 20 70 42 74 2d r==p ){. pBt-
15302 3e 70 57 72 69 74 65 72 20 3d 20 30 3b 0a 20 20 >pWriter = 0;.
15303 20 20 70 42 74 2d 3e 69 73 45 78 63 6c 75 73 69 pBt->isExclusi
15304 76 65 20 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d ve = 0;. pBt-
15305 3e 69 73 50 65 6e 64 69 6e 67 20 3d 20 30 3b 0a >isPending = 0;.
15306 20 20 7d 65 6c 73 65 20 69 66 28 20 70 42 74 2d }else if( pBt-
15307 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 32 >nTransaction==2
15308 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 ){. /* This
15309 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
1530a 65 64 20 77 68 65 6e 20 42 74 72 65 65 20 70 20 ed when Btree p
1530b 69 73 20 63 6f 6e 63 6c 75 64 69 6e 67 20 69 74 is concluding it
1530c 73 20 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 s . ** transa
1530d 63 74 69 6f 6e 2e 20 49 66 20 74 68 65 72 65 20 ction. If there
1530e 63 75 72 72 65 6e 74 6c 79 20 65 78 69 73 74 73 currently exists
1530f 20 61 20 77 72 69 74 65 72 2c 20 61 6e 64 20 70 a writer, and p
15310 20 69 73 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 74 is not. ** t
15311 68 61 74 20 77 72 69 74 65 72 2c 20 74 68 65 6e hat writer, then
15312 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c the number of l
15313 6f 63 6b 73 20 68 65 6c 64 20 62 79 20 63 6f 6e ocks held by con
15314 6e 65 63 74 69 6f 6e 73 20 6f 74 68 65 72 0a 20 nections other.
15315 20 20 20 2a 2a 20 74 68 61 6e 20 74 68 65 20 77 ** than the w
15316 72 69 74 65 72 20 6d 75 73 74 20 62 65 20 61 62 riter must be ab
15317 6f 75 74 20 74 6f 20 64 72 6f 70 20 74 6f 20 7a out to drop to z
15318 65 72 6f 2e 20 49 6e 20 74 68 69 73 20 63 61 73 ero. In this cas
15319 65 0a 20 20 20 20 2a 2a 20 73 65 74 20 74 68 65 e. ** set the
1531a 20 69 73 50 65 6e 64 69 6e 67 20 66 6c 61 67 20 isPending flag
1531b 74 6f 20 30 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 to 0.. **.
1531c 20 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 ** If there is
1531d 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61 20 not currently a
1531e 77 72 69 74 65 72 2c 20 74 68 65 6e 20 42 74 53 writer, then BtS
1531f 68 61 72 65 64 2e 69 73 50 65 6e 64 69 6e 67 20 hared.isPending
15320 6d 75 73 74 0a 20 20 20 20 2a 2a 20 62 65 20 7a must. ** be z
15321 65 72 6f 20 61 6c 72 65 61 64 79 2e 20 53 6f 20 ero already. So
15322 74 68 69 73 20 6e 65 78 74 20 6c 69 6e 65 20 69 this next line i
15323 73 20 68 61 72 6d 6c 65 73 73 20 69 6e 20 74 68 s harmless in th
15324 61 74 20 63 61 73 65 2e 0a 20 20 20 20 2a 2f 0a at case.. */.
15325 20 20 20 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 pBt->isPendi
15326 6e 67 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f ng = 0;. }.}../
15327 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
15328 6f 6e 20 63 68 61 6e 67 65 73 20 61 6c 6c 20 77 on changes all w
15329 72 69 74 65 2d 6c 6f 63 6b 73 20 68 65 6c 64 20 rite-locks held
1532a 62 79 20 42 74 72 65 65 20 70 20 69 6e 74 6f 20 by Btree p into
1532b 72 65 61 64 2d 6c 6f 63 6b 73 2e 0a 2a 2f 0a 73 read-locks..*/.s
1532c 74 61 74 69 63 20 76 6f 69 64 20 64 6f 77 6e 67 tatic void downg
1532d 72 61 64 65 41 6c 6c 53 68 61 72 65 64 43 61 63 radeAllSharedCac
1532e 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 42 74 72 heTableLocks(Btr
1532f 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 ee *p){. BtShar
15330 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
15331 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 57 72 ;. if( pBt->pWr
15332 69 74 65 72 3d 3d 70 20 29 7b 0a 20 20 20 20 42 iter==p ){. B
15333 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 3b 0a 20 20 tLock *pLock;.
15334 20 20 70 42 74 2d 3e 70 57 72 69 74 65 72 20 3d pBt->pWriter =
15335 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e 69 73 45 0;. pBt->isE
15336 78 63 6c 75 73 69 76 65 20 3d 20 30 3b 0a 20 20 xclusive = 0;.
15337 20 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 pBt->isPending
15338 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 70 4c = 0;. for(pL
15339 6f 63 6b 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 ock=pBt->pLock;
1533a 70 4c 6f 63 6b 3b 20 70 4c 6f 63 6b 3d 70 4c 6f pLock; pLock=pLo
1533b 63 6b 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 ck->pNext){.
1533c 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d assert( pLock-
1533d 3e 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 >eLock==READ_LOC
1533e 4b 20 7c 7c 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 K || pLock->pBtr
1533f 65 65 3d 3d 70 20 29 3b 0a 20 20 20 20 20 20 70 ee==p );. p
15340 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 3d 20 52 45 Lock->eLock = RE
15341 41 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 AD_LOCK;. }.
15342 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 }.}..#endif /*
15343 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
15344 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 73 74 61 ED_CACHE */..sta
15345 74 69 63 20 76 6f 69 64 20 72 65 6c 65 61 73 65 tic void release
15346 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 Page(MemPage *pP
15347 61 67 65 29 3b 20 20 2f 2a 20 46 6f 72 77 61 72 age); /* Forwar
15348 64 20 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a 0a d reference */..
15349 2f 2a 0a 2a 2a 2a 2a 2a 20 54 68 69 73 20 72 6f /*.***** This ro
1534a 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 69 6e utine is used in
1534b 73 69 64 65 20 6f 66 20 61 73 73 65 72 74 28 29 side of assert()
1534c 20 6f 6e 6c 79 20 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a only ****.**.**
1534d 20 56 65 72 69 66 79 20 74 68 61 74 20 74 68 65 Verify that the
1534e 20 63 75 72 73 6f 72 20 68 6f 6c 64 73 20 74 68 cursor holds th
1534f 65 20 6d 75 74 65 78 20 6f 6e 20 69 74 73 20 42 e mutex on its B
15350 74 53 68 61 72 65 64 0a 2a 2f 0a 23 69 66 64 65 tShared.*/.#ifde
15351 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 73 f SQLITE_DEBUG.s
15352 74 61 74 69 63 20 69 6e 74 20 63 75 72 73 6f 72 tatic int cursor
15353 48 6f 6c 64 73 4d 75 74 65 78 28 42 74 43 75 72 HoldsMutex(BtCur
15354 73 6f 72 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 sor *p){. retur
15355 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f n sqlite3_mutex_
15356 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 held(p->pBt->mut
15357 65 78 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a ex);.}.#endif...
15358 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
15359 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 2f 2a 0a MIT_INCRBLOB./*.
1535a 2a 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 74 68 ** Invalidate th
1535b 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d e overflow page-
1535c 6c 69 73 74 20 63 61 63 68 65 20 66 6f 72 20 63 list cache for c
1535d 75 72 73 6f 72 20 70 43 75 72 2c 20 69 66 20 61 ursor pCur, if a
1535e 6e 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ny..*/.static vo
1535f 69 64 20 69 6e 76 61 6c 69 64 61 74 65 4f 76 65 id invalidateOve
15360 72 66 6c 6f 77 43 61 63 68 65 28 42 74 43 75 72 rflowCache(BtCur
15361 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 sor *pCur){. as
15362 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 sert( cursorHold
15363 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a sMutex(pCur) );.
15364 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
15365 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 29 3b Cur->aOverflow);
15366 0a 20 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c . pCur->aOverfl
15367 6f 77 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ow = 0;.}../*.**
15368 20 49 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20 Invalidate the
15369 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 overflow page-li
1536a 73 74 20 63 61 63 68 65 20 66 6f 72 20 61 6c 6c st cache for all
1536b 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 65 64 0a cursors opened.
1536c 2a 2a 20 6f 6e 20 74 68 65 20 73 68 61 72 65 64 ** on the shared
1536d 20 62 74 72 65 65 20 73 74 72 75 63 74 75 72 65 btree structure
1536e 20 70 42 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 pBt..*/.static
1536f 76 6f 69 64 20 69 6e 76 61 6c 69 64 61 74 65 41 void invalidateA
15370 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 llOverflowCache(
15371 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a BtShared *pBt){.
15372 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 BtCursor *p;.
15373 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
15374 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d _mutex_held(pBt-
15375 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 >mutex) );. for
15376 28 70 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b (p=pBt->pCursor;
15377 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b p; p=p->pNext){
15378 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 4f . invalidateO
15379 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 29 3b verflowCache(p);
1537a 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 . }.}../*.** Th
1537b 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 is function is c
1537c 61 6c 6c 65 64 20 62 65 66 6f 72 65 20 6d 6f 64 alled before mod
1537d 69 66 79 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 ifying the conte
1537e 6e 74 73 20 6f 66 20 61 20 74 61 62 6c 65 0a 2a nts of a table.*
1537f 2a 20 74 6f 20 69 6e 76 61 6c 69 64 61 74 65 20 * to invalidate
15380 61 6e 79 20 69 6e 63 72 62 6c 6f 62 20 63 75 72 any incrblob cur
15381 73 6f 72 73 20 74 68 61 74 20 61 72 65 20 6f 70 sors that are op
15382 65 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20 72 6f 77 en on the.** row
15383 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 72 or one of the r
15384 6f 77 73 20 62 65 69 6e 67 20 6d 6f 64 69 66 69 ows being modifi
15385 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 72 67 ed..**.** If arg
15386 75 6d 65 6e 74 20 69 73 43 6c 65 61 72 54 61 62 ument isClearTab
15387 6c 65 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e le is true, then
15388 20 74 68 65 20 65 6e 74 69 72 65 20 63 6f 6e 74 the entire cont
15389 65 6e 74 73 20 6f 66 20 74 68 65 0a 2a 2a 20 74 ents of the.** t
1538a 61 62 6c 65 20 69 73 20 61 62 6f 75 74 20 74 6f able is about to
1538b 20 62 65 20 64 65 6c 65 74 65 64 2e 20 49 6e 20 be deleted. In
1538c 74 68 69 73 20 63 61 73 65 20 69 6e 76 61 6c 69 this case invali
1538d 64 61 74 65 20 61 6c 6c 20 69 6e 63 72 62 6c 6f date all incrblo
1538e 62 0a 2a 2a 20 63 75 72 73 6f 72 73 20 6f 70 65 b.** cursors ope
1538f 6e 20 6f 6e 20 61 6e 79 20 72 6f 77 20 77 69 74 n on any row wit
15390 68 69 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69 hin the table wi
15391 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 70 67 6e th root-page pgn
15392 6f 52 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 oRoot..**.** Oth
15393 65 72 77 69 73 65 2c 20 69 66 20 61 72 67 75 6d erwise, if argum
15394 65 6e 74 20 69 73 43 6c 65 61 72 54 61 62 6c 65 ent isClearTable
15395 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 is false, then
15396 74 68 65 20 72 6f 77 20 77 69 74 68 0a 2a 2a 20 the row with.**
15397 72 6f 77 69 64 20 69 52 6f 77 20 69 73 20 62 65 rowid iRow is be
15398 69 6e 67 20 72 65 70 6c 61 63 65 64 20 6f 72 20 ing replaced or
15399 64 65 6c 65 74 65 64 2e 20 49 6e 20 74 68 69 73 deleted. In this
1539a 20 63 61 73 65 20 69 6e 76 61 6c 69 64 61 74 65 case invalidate
1539b 0a 2a 2a 20 6f 6e 6c 79 20 74 68 6f 73 65 20 69 .** only those i
1539c 6e 63 72 62 6c 6f 62 20 63 75 72 73 6f 72 73 20 ncrblob cursors
1539d 6f 70 65 6e 20 6f 6e 20 74 68 61 74 20 73 70 65 open on that spe
1539e 63 69 66 69 63 20 72 6f 77 2e 0a 2a 2f 0a 73 74 cific row..*/.st
1539f 61 74 69 63 20 76 6f 69 64 20 69 6e 76 61 6c 69 atic void invali
153a0 64 61 74 65 49 6e 63 72 62 6c 6f 62 43 75 72 73 dateIncrblobCurs
153a1 6f 72 73 28 0a 20 20 42 74 72 65 65 20 2a 70 42 ors(. Btree *pB
153a2 74 72 65 65 2c 20 20 20 20 20 20 20 20 20 20 2f tree, /
153a3 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 66 * The database f
153a4 69 6c 65 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a ile to check */.
153a5 20 20 69 36 34 20 69 52 6f 77 2c 20 20 20 20 20 i64 iRow,
153a6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
153a7 20 72 6f 77 69 64 20 74 68 61 74 20 6d 69 67 68 rowid that migh
153a8 74 20 62 65 20 63 68 61 6e 67 69 6e 67 20 2a 2f t be changing */
153a9 0a 20 20 69 6e 74 20 69 73 43 6c 65 61 72 54 61 . int isClearTa
153aa 62 6c 65 20 20 20 20 20 20 20 20 2f 2a 20 54 72 ble /* Tr
153ab 75 65 20 69 66 20 61 6c 6c 20 72 6f 77 73 20 61 ue if all rows a
153ac 72 65 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64 re being deleted
153ad 20 2a 2f 0a 29 7b 0a 20 20 42 74 43 75 72 73 6f */.){. BtCurso
153ae 72 20 2a 70 3b 0a 20 20 42 74 53 68 61 72 65 64 r *p;. BtShared
153af 20 2a 70 42 74 20 3d 20 70 42 74 72 65 65 2d 3e *pBt = pBtree->
153b0 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 pBt;. assert( s
153b1 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 qlite3BtreeHolds
153b2 4d 75 74 65 78 28 70 42 74 72 65 65 29 20 29 3b Mutex(pBtree) );
153b3 0a 20 20 66 6f 72 28 70 3d 70 42 74 2d 3e 70 43 . for(p=pBt->pC
153b4 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 ursor; p; p=p->p
153b5 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 Next){. if( p
153b6 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 ->isIncrblobHand
153b7 6c 65 20 26 26 20 28 69 73 43 6c 65 61 72 54 61 le && (isClearTa
153b8 62 6c 65 20 7c 7c 20 70 2d 3e 69 6e 66 6f 2e 6e ble || p->info.n
153b9 4b 65 79 3d 3d 69 52 6f 77 29 20 29 7b 0a 20 20 Key==iRow) ){.
153ba 20 20 20 20 70 2d 3e 65 53 74 61 74 65 20 3d 20 p->eState =
153bb 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a CURSOR_INVALID;.
153bc 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 65 6c }. }.}..#el
153bd 73 65 0a 20 20 2f 2a 20 53 74 75 62 20 66 75 6e se. /* Stub fun
153be 63 74 69 6f 6e 73 20 77 68 65 6e 20 49 4e 43 52 ctions when INCR
153bf 42 4c 4f 42 20 69 73 20 6f 6d 69 74 74 65 64 20 BLOB is omitted
153c0 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 69 6e 76 */. #define inv
153c1 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 alidateOverflowC
153c2 61 63 68 65 28 78 29 0a 20 20 23 64 65 66 69 6e ache(x). #defin
153c3 65 20 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f e invalidateAllO
153c4 76 65 72 66 6c 6f 77 43 61 63 68 65 28 78 29 0a verflowCache(x).
153c5 20 20 23 64 65 66 69 6e 65 20 69 6e 76 61 6c 69 #define invali
153c6 64 61 74 65 49 6e 63 72 62 6c 6f 62 43 75 72 73 dateIncrblobCurs
153c7 6f 72 73 28 78 2c 79 2c 7a 29 0a 23 65 6e 64 69 ors(x,y,z).#endi
153c8 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
153c9 5f 49 4e 43 52 42 4c 4f 42 20 2a 2f 0a 0a 2f 2a _INCRBLOB */../*
153ca 0a 2a 2a 20 53 65 74 20 62 69 74 20 70 67 6e 6f .** Set bit pgno
153cb 20 6f 66 20 74 68 65 20 42 74 53 68 61 72 65 64 of the BtShared
153cc 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 .pHasContent bit
153cd 76 65 63 2e 20 54 68 69 73 20 69 73 20 63 61 6c vec. This is cal
153ce 6c 65 64 20 0a 2a 2a 20 77 68 65 6e 20 61 20 70 led .** when a p
153cf 61 67 65 20 74 68 61 74 20 70 72 65 76 69 6f 75 age that previou
153d0 73 6c 79 20 63 6f 6e 74 61 69 6e 65 64 20 64 61 sly contained da
153d1 74 61 20 62 65 63 6f 6d 65 73 20 61 20 66 72 65 ta becomes a fre
153d2 65 2d 6c 69 73 74 20 6c 65 61 66 20 0a 2a 2a 20 e-list leaf .**
153d3 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 page..**.** The
153d4 42 74 53 68 61 72 65 64 2e 70 48 61 73 43 6f 6e BtShared.pHasCon
153d5 74 65 6e 74 20 62 69 74 76 65 63 20 65 78 69 73 tent bitvec exis
153d6 74 73 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75 6e ts to work aroun
153d7 64 20 61 6e 20 6f 62 73 63 75 72 65 0a 2a 2a 20 d an obscure.**
153d8 62 75 67 20 63 61 75 73 65 64 20 62 79 20 74 68 bug caused by th
153d9 65 20 69 6e 74 65 72 61 63 74 69 6f 6e 20 6f 66 e interaction of
153da 20 74 77 6f 20 75 73 65 66 75 6c 20 49 4f 20 6f two useful IO o
153db 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20 73 75 72 ptimizations sur
153dc 72 6f 75 6e 64 69 6e 67 0a 2a 2a 20 66 72 65 65 rounding.** free
153dd 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 73 -list leaf pages
153de 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 57 68 65 :.**.** 1) Whe
153df 6e 20 61 6c 6c 20 64 61 74 61 20 69 73 20 64 65 n all data is de
153e0 6c 65 74 65 64 20 66 72 6f 6d 20 61 20 70 61 67 leted from a pag
153e1 65 20 61 6e 64 20 74 68 65 20 70 61 67 65 20 62 e and the page b
153e2 65 63 6f 6d 65 73 0a 2a 2a 20 20 20 20 20 20 61 ecomes.** a
153e3 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 free-list leaf
153e4 70 61 67 65 2c 20 74 68 65 20 70 61 67 65 20 69 page, the page i
153e5 73 20 6e 6f 74 20 77 72 69 74 74 65 6e 20 74 6f s not written to
153e6 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a the database.**
153e7 20 20 20 20 20 20 28 61 73 20 66 72 65 65 2d 6c (as free-l
153e8 69 73 74 20 6c 65 61 66 20 70 61 67 65 73 20 63 ist leaf pages c
153e9 6f 6e 74 61 69 6e 20 6e 6f 20 6d 65 61 6e 69 6e ontain no meanin
153ea 67 66 75 6c 20 64 61 74 61 29 2e 20 53 6f 6d 65 gful data). Some
153eb 74 69 6d 65 73 0a 2a 2a 20 20 20 20 20 20 73 75 times.** su
153ec 63 68 20 61 20 70 61 67 65 20 69 73 20 6e 6f 74 ch a page is not
153ed 20 65 76 65 6e 20 6a 6f 75 72 6e 61 6c 6c 65 64 even journalled
153ee 20 28 61 73 20 69 74 20 77 69 6c 6c 20 6e 6f 74 (as it will not
153ef 20 62 65 20 6d 6f 64 69 66 69 65 64 2c 0a 2a 2a be modified,.**
153f0 20 20 20 20 20 20 77 68 79 20 62 6f 74 68 65 72 why bother
153f1 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20 69 74 3f journalling it?
153f2 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20 57 68 )..**.** 2) Wh
153f3 65 6e 20 61 20 66 72 65 65 2d 6c 69 73 74 20 6c en a free-list l
153f4 65 61 66 20 70 61 67 65 20 69 73 20 72 65 75 73 eaf page is reus
153f5 65 64 2c 20 69 74 73 20 63 6f 6e 74 65 6e 74 20 ed, its content
153f6 69 73 20 6e 6f 74 20 72 65 61 64 0a 2a 2a 20 20 is not read.**
153f7 20 20 20 20 66 72 6f 6d 20 74 68 65 20 64 61 74 from the dat
153f8 61 62 61 73 65 20 6f 72 20 77 72 69 74 74 65 6e abase or written
153f9 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 to the journal
153fa 66 69 6c 65 20 28 77 68 79 20 73 68 6f 75 6c 64 file (why should
153fb 20 69 74 0a 2a 2a 20 20 20 20 20 20 62 65 2c 20 it.** be,
153fc 69 66 20 69 74 20 69 73 20 6e 6f 74 20 61 74 20 if it is not at
153fd 61 6c 6c 20 6d 65 61 6e 69 6e 67 66 75 6c 3f 29 all meaningful?)
153fe 2e 0a 2a 2a 0a 2a 2a 20 42 79 20 74 68 65 6d 73 ..**.** By thems
153ff 65 6c 76 65 73 2c 20 74 68 65 73 65 20 6f 70 74 elves, these opt
15400 69 6d 69 7a 61 74 69 6f 6e 73 20 77 6f 72 6b 20 imizations work
15401 66 69 6e 65 20 61 6e 64 20 70 72 6f 76 69 64 65 fine and provide
15402 20 61 20 68 61 6e 64 79 0a 2a 2a 20 70 65 72 66 a handy.** perf
15403 6f 72 6d 61 6e 63 65 20 62 6f 6f 73 74 20 74 6f ormance boost to
15404 20 62 75 6c 6b 20 64 65 6c 65 74 65 20 6f 72 20 bulk delete or
15405 69 6e 73 65 72 74 20 6f 70 65 72 61 74 69 6f 6e insert operation
15406 73 2e 20 48 6f 77 65 76 65 72 2c 20 69 66 0a 2a s. However, if.*
15407 2a 20 61 20 70 61 67 65 20 69 73 20 6d 6f 76 65 * a page is move
15408 64 20 74 6f 20 74 68 65 20 66 72 65 65 2d 6c 69 d to the free-li
15409 73 74 20 61 6e 64 20 74 68 65 6e 20 72 65 75 73 st and then reus
1540a 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 73 61 ed within the sa
1540b 6d 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f me.** transactio
1540c 6e 2c 20 61 20 70 72 6f 62 6c 65 6d 20 63 6f 6d n, a problem com
1540d 65 73 20 75 70 2e 20 49 66 20 74 68 65 20 70 61 es up. If the pa
1540e 67 65 20 69 73 20 6e 6f 74 20 6a 6f 75 72 6e 61 ge is not journa
1540f 6c 6c 65 64 20 77 68 65 6e 0a 2a 2a 20 69 74 20 lled when.** it
15410 69 73 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 20 is moved to the
15411 66 72 65 65 2d 6c 69 73 74 20 61 6e 64 20 69 74 free-list and it
15412 20 69 73 20 61 6c 73 6f 20 6e 6f 74 20 6a 6f 75 is also not jou
15413 72 6e 61 6c 6c 65 64 20 77 68 65 6e 20 69 74 0a rnalled when it.
15414 2a 2a 20 69 73 20 65 78 74 72 61 63 74 65 64 20 ** is extracted
15415 66 72 6f 6d 20 74 68 65 20 66 72 65 65 2d 6c 69 from the free-li
15416 73 74 20 61 6e 64 20 72 65 75 73 65 64 2c 20 74 st and reused, t
15417 68 65 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c hen the original
15418 20 64 61 74 61 0a 2a 2a 20 6d 61 79 20 62 65 20 data.** may be
15419 6c 6f 73 74 2e 20 49 6e 20 74 68 65 20 65 76 65 lost. In the eve
1541a 6e 74 20 6f 66 20 61 20 72 6f 6c 6c 62 61 63 6b nt of a rollback
1541b 2c 20 69 74 20 6d 61 79 20 6e 6f 74 20 62 65 20 , it may not be
1541c 70 6f 73 73 69 62 6c 65 0a 2a 2a 20 74 6f 20 72 possible.** to r
1541d 65 73 74 6f 72 65 20 74 68 65 20 64 61 74 61 62 estore the datab
1541e 61 73 65 20 74 6f 20 69 74 73 20 6f 72 69 67 69 ase to its origi
1541f 6e 61 6c 20 63 6f 6e 66 69 67 75 72 61 74 69 6f nal configuratio
15420 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 6f 6c n..**.** The sol
15421 75 74 69 6f 6e 20 69 73 20 74 68 65 20 42 74 53 ution is the BtS
15422 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e hared.pHasConten
15423 74 20 62 69 74 76 65 63 2e 20 57 68 65 6e 65 76 t bitvec. Whenev
15424 65 72 20 61 20 70 61 67 65 20 69 73 20 0a 2a 2a er a page is .**
15425 20 6d 6f 76 65 64 20 74 6f 20 62 65 63 6f 6d 65 moved to become
15426 20 61 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 a free-list lea
15427 66 20 70 61 67 65 2c 20 74 68 65 20 63 6f 72 72 f page, the corr
15428 65 73 70 6f 6e 64 69 6e 67 20 62 69 74 20 69 73 esponding bit is
15429 0a 2a 2a 20 73 65 74 20 69 6e 20 74 68 65 20 62 .** set in the b
1542a 69 74 76 65 63 2e 20 57 68 65 6e 65 76 65 72 20 itvec. Whenever
1542b 61 20 6c 65 61 66 20 70 61 67 65 20 69 73 20 65 a leaf page is e
1542c 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20 74 68 xtracted from th
1542d 65 20 66 72 65 65 2d 6c 69 73 74 2c 0a 2a 2a 20 e free-list,.**
1542e 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 32 20 61 optimization 2 a
1542f 62 6f 76 65 20 69 73 20 6f 6d 69 74 74 65 64 20 bove is omitted
15430 69 66 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e if the correspon
15431 64 69 6e 67 20 62 69 74 20 69 73 20 61 6c 72 65 ding bit is alre
15432 61 64 79 0a 2a 2a 20 73 65 74 20 69 6e 20 42 74 ady.** set in Bt
15433 53 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 Shared.pHasConte
15434 6e 74 2e 20 54 68 65 20 63 6f 6e 74 65 6e 74 73 nt. The contents
15435 20 6f 66 20 74 68 65 20 62 69 74 76 65 63 20 61 of the bitvec a
15436 72 65 20 63 6c 65 61 72 65 64 0a 2a 2a 20 61 74 re cleared.** at
15437 20 74 68 65 20 65 6e 64 20 6f 66 20 65 76 65 72 the end of ever
15438 79 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a y transaction..*
15439 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 /.static int btr
1543a 65 65 53 65 74 48 61 73 43 6f 6e 74 65 6e 74 28 eeSetHasContent(
1543b 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 BtShared *pBt, P
1543c 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 69 6e 74 gno pgno){. int
1543d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
1543e 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 70 48 61 . if( !pBt->pHa
1543f 73 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 sContent ){.
15440 69 6e 74 20 6e 50 61 67 65 20 3d 20 31 30 30 3b int nPage = 100;
15441 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 . sqlite3Page
15442 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e rPagecount(pBt->
15443 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29 3b pPager, &nPage);
15444 0a 20 20 20 20 2f 2a 20 49 66 20 73 71 6c 69 74 . /* If sqlit
15445 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 e3PagerPagecount
15446 28 29 20 66 61 69 6c 73 20 74 68 65 72 65 20 69 () fails there i
15447 73 20 6e 6f 20 68 61 72 6d 20 62 65 63 61 75 73 s no harm becaus
15448 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 6e 50 61 e the. ** nPa
15449 67 65 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 ge variable is u
1544a 6e 63 68 61 6e 67 65 64 20 66 72 6f 6d 20 69 74 nchanged from it
1544b 73 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 s default value
1544c 6f 66 20 31 30 30 20 2a 2f 0a 20 20 20 20 70 42 of 100 */. pB
1544d 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 20 3d t->pHasContent =
1544e 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 sqlite3BitvecCr
1544f 65 61 74 65 28 28 75 33 32 29 6e 50 61 67 65 29 eate((u32)nPage)
15450 3b 0a 20 20 20 20 69 66 28 20 21 70 42 74 2d 3e ;. if( !pBt->
15451 70 48 61 73 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 pHasContent ){.
15452 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
15453 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 _NOMEM;. }.
15454 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 }. if( rc==SQLI
15455 54 45 5f 4f 4b 20 26 26 20 70 67 6e 6f 3c 3d 73 TE_OK && pgno<=s
15456 71 6c 69 74 65 33 42 69 74 76 65 63 53 69 7a 65 qlite3BitvecSize
15457 28 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e (pBt->pHasConten
15458 74 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 t) ){. rc = s
15459 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 qlite3BitvecSet(
1545a 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 pBt->pHasContent
1545b 2c 20 70 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20 72 , pgno);. }. r
1545c 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
1545d 2a 2a 20 51 75 65 72 79 20 74 68 65 20 42 74 53 ** Query the BtS
1545e 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e hared.pHasConten
1545f 74 20 76 65 63 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 t vector..**.**
15460 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
15461 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 20 66 called when a f
15462 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 ree-list leaf pa
15463 67 65 20 69 73 20 72 65 6d 6f 76 65 64 20 66 72 ge is removed fr
15464 6f 6d 20 74 68 65 0a 2a 2a 20 66 72 65 65 2d 6c om the.** free-l
15465 69 73 74 20 66 6f 72 20 72 65 75 73 65 2e 20 49 ist for reuse. I
15466 74 20 72 65 74 75 72 6e 73 20 66 61 6c 73 65 20 t returns false
15467 69 66 20 69 74 20 69 73 20 73 61 66 65 20 74 6f if it is safe to
15468 20 72 65 74 72 69 65 76 65 20 74 68 65 0a 2a 2a retrieve the.**
15469 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 70 page from the p
1546a 61 67 65 72 20 6c 61 79 65 72 20 77 69 74 68 20 ager layer with
1546b 74 68 65 20 27 6e 6f 2d 63 6f 6e 74 65 6e 74 27 the 'no-content'
1546c 20 66 6c 61 67 20 73 65 74 2e 20 54 72 75 65 20 flag set. True
1546d 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 73 74 otherwise..*/.st
1546e 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 47 65 atic int btreeGe
1546f 74 48 61 73 43 6f 6e 74 65 6e 74 28 42 74 53 68 tHasContent(BtSh
15470 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 ared *pBt, Pgno
15471 70 67 6e 6f 29 7b 0a 20 20 42 69 74 76 65 63 20 pgno){. Bitvec
15472 2a 70 20 3d 20 70 42 74 2d 3e 70 48 61 73 43 6f *p = pBt->pHasCo
15473 6e 74 65 6e 74 3b 0a 20 20 72 65 74 75 72 6e 20 ntent;. return
15474 28 70 20 26 26 20 28 70 67 6e 6f 3e 73 71 6c 69 (p && (pgno>sqli
15475 74 65 33 42 69 74 76 65 63 53 69 7a 65 28 70 29 te3BitvecSize(p)
15476 20 7c 7c 20 73 71 6c 69 74 65 33 42 69 74 76 65 || sqlite3Bitve
15477 63 54 65 73 74 28 70 2c 20 70 67 6e 6f 29 29 29 cTest(p, pgno)))
15478 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 ;.}../*.** Clear
15479 20 28 64 65 73 74 72 6f 79 29 20 74 68 65 20 42 (destroy) the B
1547a 74 53 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 tShared.pHasCont
1547b 65 6e 74 20 62 69 74 76 65 63 2e 20 54 68 69 73 ent bitvec. This
1547c 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 69 6e should be.** in
1547d 76 6f 6b 65 64 20 61 74 20 74 68 65 20 63 6f 6e voked at the con
1547e 63 6c 75 73 69 6f 6e 20 6f 66 20 65 61 63 68 20 clusion of each
1547f 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f write-transactio
15480 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 n..*/.static voi
15481 64 20 62 74 72 65 65 43 6c 65 61 72 48 61 73 43 d btreeClearHasC
15482 6f 6e 74 65 6e 74 28 42 74 53 68 61 72 65 64 20 ontent(BtShared
15483 2a 70 42 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 *pBt){. sqlite3
15484 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 42 BitvecDestroy(pB
15485 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 29 3b t->pHasContent);
15486 0a 20 20 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 . pBt->pHasCont
15487 65 6e 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a ent = 0;.}../*.*
15488 2a 20 53 61 76 65 20 74 68 65 20 63 75 72 72 65 * Save the curre
15489 6e 74 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69 nt cursor positi
1548a 6f 6e 20 69 6e 20 74 68 65 20 76 61 72 69 61 62 on in the variab
1548b 6c 65 73 20 42 74 43 75 72 73 6f 72 2e 6e 4b 65 les BtCursor.nKe
1548c 79 20 0a 2a 2a 20 61 6e 64 20 42 74 43 75 72 73 y .** and BtCurs
1548d 6f 72 2e 70 4b 65 79 2e 20 54 68 65 20 63 75 72 or.pKey. The cur
1548e 73 6f 72 27 73 20 73 74 61 74 65 20 69 73 20 73 sor's state is s
1548f 65 74 20 74 6f 20 43 55 52 53 4f 52 5f 52 45 51 et to CURSOR_REQ
15490 55 49 52 45 53 45 45 4b 2e 0a 2a 2a 0a 2a 2a 20 UIRESEEK..**.**
15491 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 The caller must
15492 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 20 ensure that the
15493 63 75 72 73 6f 72 20 69 73 20 76 61 6c 69 64 20 cursor is valid
15494 28 68 61 73 20 65 53 74 61 74 65 3d 3d 43 55 52 (has eState==CUR
15495 53 4f 52 5f 56 41 4c 49 44 29 0a 2a 2a 20 70 72 SOR_VALID).** pr
15496 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 20 74 ior to calling t
15497 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 0a 2a his routine. .*
15498 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 61 76 /.static int sav
15499 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 eCursorPosition(
1549a 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b BtCursor *pCur){
1549b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 . int rc;.. as
1549c 73 65 72 74 28 20 43 55 52 53 4f 52 5f 56 41 4c sert( CURSOR_VAL
1549d 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 ID==pCur->eState
1549e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 30 3d );. assert( 0=
1549f 3d 70 43 75 72 2d 3e 70 4b 65 79 20 29 3b 0a 20 =pCur->pKey );.
154a0 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 assert( cursorH
154a1 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 oldsMutex(pCur)
154a2 29 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 );.. rc = sqlit
154a3 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 70 e3BtreeKeySize(p
154a4 43 75 72 2c 20 26 70 43 75 72 2d 3e 6e 4b 65 79 Cur, &pCur->nKey
154a5 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d );. assert( rc=
154a6 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 2f =SQLITE_OK ); /
154a7 2a 20 4b 65 79 53 69 7a 65 28 29 20 63 61 6e 6e * KeySize() cann
154a8 6f 74 20 66 61 69 6c 20 2a 2f 0a 0a 20 20 2f 2a ot fail */.. /*
154a9 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 69 If this is an i
154aa 6e 74 4b 65 79 20 74 61 62 6c 65 2c 20 74 68 65 ntKey table, the
154ab 6e 20 74 68 65 20 61 62 6f 76 65 20 63 61 6c 6c n the above call
154ac 20 74 6f 20 42 74 72 65 65 4b 65 79 53 69 7a 65 to BtreeKeySize
154ad 28 29 0a 20 20 2a 2a 20 73 74 6f 72 65 73 20 74 (). ** stores t
154ae 68 65 20 69 6e 74 65 67 65 72 20 6b 65 79 20 69 he integer key i
154af 6e 20 70 43 75 72 2d 3e 6e 4b 65 79 2e 20 49 6e n pCur->nKey. In
154b0 20 74 68 69 73 20 63 61 73 65 20 74 68 69 73 20 this case this
154b1 76 61 6c 75 65 20 69 73 0a 20 20 2a 2a 20 61 6c value is. ** al
154b2 6c 20 74 68 61 74 20 69 73 20 72 65 71 75 69 72 l that is requir
154b3 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 ed. Otherwise, i
154b4 66 20 70 43 75 72 20 69 73 20 6e 6f 74 20 6f 70 f pCur is not op
154b5 65 6e 20 6f 6e 20 61 6e 20 69 6e 74 4b 65 79 0a en on an intKey.
154b6 20 20 2a 2a 20 74 61 62 6c 65 2c 20 74 68 65 6e ** table, then
154b7 20 6d 61 6c 6c 6f 63 20 73 70 61 63 65 20 66 6f malloc space fo
154b8 72 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 r and store the
154b9 70 43 75 72 2d 3e 6e 4b 65 79 20 62 79 74 65 73 pCur->nKey bytes
154ba 20 6f 66 20 6b 65 79 20 0a 20 20 2a 2a 20 64 61 of key . ** da
154bb 74 61 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 30 ta.. */. if( 0
154bc 3d 3d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 ==pCur->apPage[0
154bd 5d 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 ]->intKey ){.
154be 20 76 6f 69 64 20 2a 70 4b 65 79 20 3d 20 73 71 void *pKey = sq
154bf 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 28 69 6e lite3Malloc( (in
154c0 74 29 70 43 75 72 2d 3e 6e 4b 65 79 20 29 3b 0a t)pCur->nKey );.
154c1 20 20 20 20 69 66 28 20 70 4b 65 79 20 29 7b 0a if( pKey ){.
154c2 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
154c3 65 33 42 74 72 65 65 4b 65 79 28 70 43 75 72 2c e3BtreeKey(pCur,
154c4 20 30 2c 20 28 69 6e 74 29 70 43 75 72 2d 3e 6e 0, (int)pCur->n
154c5 4b 65 79 2c 20 70 4b 65 79 29 3b 0a 20 20 20 20 Key, pKey);.
154c6 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
154c7 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 70 _OK ){. p
154c8 43 75 72 2d 3e 70 4b 65 79 20 3d 20 70 4b 65 79 Cur->pKey = pKey
154c9 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
154ca 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 sqlite3_f
154cb 72 65 65 28 70 4b 65 79 29 3b 0a 20 20 20 20 20 ree(pKey);.
154cc 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
154cd 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
154ce 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d NOMEM;. }. }
154cf 0a 20 20 61 73 73 65 72 74 28 20 21 70 43 75 72 . assert( !pCur
154d0 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 ->apPage[0]->int
154d1 4b 65 79 20 7c 7c 20 21 70 43 75 72 2d 3e 70 4b Key || !pCur->pK
154d2 65 79 20 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d ey );.. if( rc=
154d3 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
154d4 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 int i;. for
154d5 28 69 3d 30 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 (i=0; i<=pCur->i
154d6 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 Page; i++){.
154d7 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 releasePage(pC
154d8 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0a ur->apPage[i]);.
154d9 20 20 20 20 20 20 70 43 75 72 2d 3e 61 70 50 61 pCur->apPa
154da 67 65 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 7d ge[i] = 0;. }
154db 0a 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67 65 . pCur->iPage
154dc 20 3d 20 2d 31 3b 0a 20 20 20 20 70 43 75 72 2d = -1;. pCur-
154dd 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 >eState = CURSOR
154de 5f 52 45 51 55 49 52 45 53 45 45 4b 3b 0a 20 20 _REQUIRESEEK;.
154df 7d 0a 0a 20 20 69 6e 76 61 6c 69 64 61 74 65 4f }.. invalidateO
154e0 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 43 75 verflowCache(pCu
154e1 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b r);. return rc;
154e2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 61 76 65 20 74 .}../*.** Save t
154e3 68 65 20 70 6f 73 69 74 69 6f 6e 73 20 6f 66 20 he positions of
154e4 61 6c 6c 20 63 75 72 73 6f 72 73 20 28 65 78 63 all cursors (exc
154e5 65 70 74 20 70 45 78 63 65 70 74 29 20 74 68 61 ept pExcept) tha
154e6 74 20 61 72 65 20 6f 70 65 6e 20 6f 6e 0a 2a 2a t are open on.**
154e7 20 74 68 65 20 74 61 62 6c 65 20 20 77 69 74 68 the table with
154e8 20 72 6f 6f 74 2d 70 61 67 65 20 69 52 6f 6f 74 root-page iRoot
154e9 2e 20 55 73 75 61 6c 6c 79 2c 20 74 68 69 73 20 . Usually, this
154ea 69 73 20 63 61 6c 6c 65 64 20 6a 75 73 74 20 62 is called just b
154eb 65 66 6f 72 65 20 63 75 72 73 6f 72 0a 2a 2a 20 efore cursor.**
154ec 70 45 78 63 65 70 74 20 69 73 20 75 73 65 64 20 pExcept is used
154ed 74 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 74 61 to modify the ta
154ee 62 6c 65 20 28 42 74 72 65 65 44 65 6c 65 74 65 ble (BtreeDelete
154ef 28 29 20 6f 72 20 42 74 72 65 65 49 6e 73 65 72 () or BtreeInser
154f0 74 28 29 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 t())..*/.static
154f1 69 6e 74 20 73 61 76 65 41 6c 6c 43 75 72 73 6f int saveAllCurso
154f2 72 73 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 rs(BtShared *pBt
154f3 2c 20 50 67 6e 6f 20 69 52 6f 6f 74 2c 20 42 74 , Pgno iRoot, Bt
154f4 43 75 72 73 6f 72 20 2a 70 45 78 63 65 70 74 29 Cursor *pExcept)
154f5 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b {. BtCursor *p;
154f6 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
154f7 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 e3_mutex_held(pB
154f8 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 t->mutex) );. a
154f9 73 73 65 72 74 28 20 70 45 78 63 65 70 74 3d 3d ssert( pExcept==
154fa 30 20 7c 7c 20 70 45 78 63 65 70 74 2d 3e 70 42 0 || pExcept->pB
154fb 74 3d 3d 70 42 74 20 29 3b 0a 20 20 66 6f 72 28 t==pBt );. for(
154fc 70 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 p=pBt->pCursor;
154fd 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a p; p=p->pNext){.
154fe 20 20 20 20 69 66 28 20 70 21 3d 70 45 78 63 65 if( p!=pExce
154ff 70 74 20 26 26 20 28 30 3d 3d 69 52 6f 6f 74 20 pt && (0==iRoot
15500 7c 7c 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d || p->pgnoRoot==
15501 69 52 6f 6f 74 29 20 26 26 20 0a 20 20 20 20 20 iRoot) && .
15502 20 20 20 70 2d 3e 65 53 74 61 74 65 3d 3d 43 55 p->eState==CU
15503 52 53 4f 52 5f 56 41 4c 49 44 20 29 7b 0a 20 20 RSOR_VALID ){.
15504 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73 61 76 int rc = sav
15505 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 eCursorPosition(
15506 70 29 3b 0a 20 20 20 20 20 20 69 66 28 20 53 51 p);. if( SQ
15507 4c 49 54 45 5f 4f 4b 21 3d 72 63 20 29 7b 0a 20 LITE_OK!=rc ){.
15508 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
15509 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1550a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
1550b 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
1550c 20 43 6c 65 61 72 20 74 68 65 20 63 75 72 72 65 Clear the curre
1550d 6e 74 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69 nt cursor positi
1550e 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 on..*/.SQLITE_PR
1550f 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
15510 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 73 e3BtreeClearCurs
15511 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 or(BtCursor *pCu
15512 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75 r){. assert( cu
15513 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
15514 43 75 72 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 Cur) );. sqlite
15515 33 5f 66 72 65 65 28 70 43 75 72 2d 3e 70 4b 65 3_free(pCur->pKe
15516 79 29 3b 0a 20 20 70 43 75 72 2d 3e 70 4b 65 79 y);. pCur->pKey
15517 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 65 53 = 0;. pCur->eS
15518 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e tate = CURSOR_IN
15519 56 41 4c 49 44 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 VALID;.}../*.**
1551a 49 6e 20 74 68 69 73 20 76 65 72 73 69 6f 6e 20 In this version
1551b 6f 66 20 42 74 72 65 65 4d 6f 76 65 74 6f 2c 20 of BtreeMoveto,
1551c 70 4b 65 79 20 69 73 20 61 20 70 61 63 6b 65 64 pKey is a packed
1551d 20 69 6e 64 65 78 20 72 65 63 6f 72 64 0a 2a 2a index record.**
1551e 20 73 75 63 68 20 61 73 20 69 73 20 67 65 6e 65 such as is gene
1551f 72 61 74 65 64 20 62 79 20 74 68 65 20 4f 50 5f rated by the OP_
15520 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 MakeRecord opcod
15521 65 2e 20 20 55 6e 70 61 63 6b 20 74 68 65 0a 2a e. Unpack the.*
15522 2a 20 72 65 63 6f 72 64 20 61 6e 64 20 74 68 65 * record and the
15523 6e 20 63 61 6c 6c 20 42 74 72 65 65 4d 6f 76 65 n call BtreeMove
15524 74 6f 55 6e 70 61 63 6b 65 64 28 29 20 74 6f 20 toUnpacked() to
15525 64 6f 20 74 68 65 20 77 6f 72 6b 2e 0a 2a 2f 0a do the work..*/.
15526 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 static int btree
15527 4d 6f 76 65 74 6f 28 0a 20 20 42 74 43 75 72 73 Moveto(. BtCurs
15528 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 2f 2a or *pCur, /*
15529 20 43 75 72 73 6f 72 20 6f 70 65 6e 20 6f 6e 20 Cursor open on
1552a 74 68 65 20 62 74 72 65 65 20 74 6f 20 62 65 20 the btree to be
1552b 73 65 61 72 63 68 65 64 20 2a 2f 0a 20 20 63 6f searched */. co
1552c 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 nst void *pKey,
1552d 20 20 2f 2a 20 50 61 63 6b 65 64 20 6b 65 79 20 /* Packed key
1552e 69 66 20 74 68 65 20 62 74 72 65 65 20 69 73 20 if the btree is
1552f 61 6e 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 36 an index */. i6
15530 34 20 6e 4b 65 79 2c 20 20 20 20 20 20 20 20 20 4 nKey,
15531 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 6b 65 79 /* Integer key
15532 20 66 6f 72 20 74 61 62 6c 65 73 2e 20 20 53 69 for tables. Si
15533 7a 65 20 6f 66 20 70 4b 65 79 20 66 6f 72 20 69 ze of pKey for i
15534 6e 64 69 63 65 73 20 2a 2f 0a 20 20 69 6e 74 20 ndices */. int
15535 62 69 61 73 2c 20 20 20 20 20 20 20 20 20 20 20 bias,
15536 2f 2a 20 42 69 61 73 20 73 65 61 72 63 68 20 74 /* Bias search t
15537 6f 20 74 68 65 20 68 69 67 68 20 65 6e 64 20 2a o the high end *
15538 2f 0a 20 20 69 6e 74 20 2a 70 52 65 73 20 20 20 /. int *pRes
15539 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 /* Write
1553a 20 73 65 61 72 63 68 20 72 65 73 75 6c 74 73 20 search results
1553b 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 here */.){. int
1553c 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 rc;
1553d 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 75 /* Statu
1553e 73 20 63 6f 64 65 20 2a 2f 0a 20 20 55 6e 70 61 s code */. Unpa
1553f 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 49 64 78 ckedRecord *pIdx
15540 4b 65 79 3b 20 20 20 2f 2a 20 55 6e 70 61 63 6b Key; /* Unpack
15541 65 64 20 69 6e 64 65 78 20 6b 65 79 20 2a 2f 0a ed index key */.
15542 20 20 63 68 61 72 20 61 53 70 61 63 65 5b 31 35 char aSpace[15
15543 30 5d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 0]; /*
15544 54 65 6d 70 20 73 70 61 63 65 20 66 6f 72 20 70 Temp space for p
15545 49 64 78 4b 65 79 20 2d 20 74 6f 20 61 76 6f 69 IdxKey - to avoi
15546 64 20 61 20 6d 61 6c 6c 6f 63 20 2a 2f 0a 0a 20 d a malloc */..
15547 20 69 66 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 if( pKey ){.
15548 20 61 73 73 65 72 74 28 20 6e 4b 65 79 3d 3d 28 assert( nKey==(
15549 69 36 34 29 28 69 6e 74 29 6e 4b 65 79 20 29 3b i64)(int)nKey );
1554a 0a 20 20 20 20 70 49 64 78 4b 65 79 20 3d 20 73 . pIdxKey = s
1554b 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 qlite3VdbeRecord
1554c 55 6e 70 61 63 6b 28 70 43 75 72 2d 3e 70 4b 65 Unpack(pCur->pKe
1554d 79 49 6e 66 6f 2c 20 28 69 6e 74 29 6e 4b 65 79 yInfo, (int)nKey
1554e 2c 20 70 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 , pKey,.
1554f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 53 aS
15551 70 61 63 65 2c 20 73 69 7a 65 6f 66 28 61 53 70 pace, sizeof(aSp
15552 61 63 65 29 29 3b 0a 20 20 20 20 69 66 28 20 70 ace));. if( p
15553 49 64 78 4b 65 79 3d 3d 30 20 29 20 72 65 74 75 IdxKey==0 ) retu
15554 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
15555 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 . }else{. pI
15556 64 78 4b 65 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 dxKey = 0;. }.
15557 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
15558 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 eeMovetoUnpacked
15559 28 70 43 75 72 2c 20 70 49 64 78 4b 65 79 2c 20 (pCur, pIdxKey,
1555a 6e 4b 65 79 2c 20 62 69 61 73 2c 20 70 52 65 73 nKey, bias, pRes
1555b 29 3b 0a 20 20 69 66 28 20 70 4b 65 79 20 29 7b );. if( pKey ){
1555c 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1555d 44 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 DeleteUnpackedRe
1555e 63 6f 72 64 28 70 49 64 78 4b 65 79 29 3b 0a 20 cord(pIdxKey);.
1555f 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
15560 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 74 6f 72 65 }../*.** Restore
15561 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 the cursor to t
15562 68 65 20 70 6f 73 69 74 69 6f 6e 20 69 74 20 77 he position it w
15563 61 73 20 69 6e 20 28 6f 72 20 61 73 20 63 6c 6f as in (or as clo
15564 73 65 20 74 6f 20 61 73 20 70 6f 73 73 69 62 6c se to as possibl
15565 65 29 0a 2a 2a 20 77 68 65 6e 20 73 61 76 65 43 e).** when saveC
15566 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 ursorPosition()
15567 77 61 73 20 63 61 6c 6c 65 64 2e 20 4e 6f 74 65 was called. Note
15568 20 74 68 61 74 20 74 68 69 73 20 63 61 6c 6c 20 that this call
15569 64 65 6c 65 74 65 73 20 74 68 65 20 0a 2a 2a 20 deletes the .**
1556a 73 61 76 65 64 20 70 6f 73 69 74 69 6f 6e 20 69 saved position i
1556b 6e 66 6f 20 73 74 6f 72 65 64 20 62 79 20 73 61 nfo stored by sa
1556c 76 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e veCursorPosition
1556d 28 29 2c 20 73 6f 20 74 68 65 72 65 20 63 61 6e (), so there can
1556e 20 62 65 0a 2a 2a 20 61 74 20 6d 6f 73 74 20 6f be.** at most o
1556f 6e 65 20 65 66 66 65 63 74 69 76 65 20 72 65 73 ne effective res
15570 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 toreCursorPositi
15571 6f 6e 28 29 20 63 61 6c 6c 20 61 66 74 65 72 20 on() call after
15572 65 61 63 68 20 0a 2a 2a 20 73 61 76 65 43 75 72 each .** saveCur
15573 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 2e 0a 2a sorPosition()..*
15574 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 /.static int btr
15575 65 65 52 65 73 74 6f 72 65 43 75 72 73 6f 72 50 eeRestoreCursorP
15576 6f 73 69 74 69 6f 6e 28 42 74 43 75 72 73 6f 72 osition(BtCursor
15577 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e 74 20 72 *pCur){. int r
15578 63 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 c;. assert( cur
15579 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 sorHoldsMutex(pC
1557a 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ur) );. assert(
1557b 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3e 3d 43 pCur->eState>=C
1557c 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 URSOR_REQUIRESEE
1557d 4b 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d K );. if( pCur-
1557e 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
1557f 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 72 65 74 FAULT ){. ret
15580 75 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 urn pCur->skipNe
15581 78 74 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e xt;. }. pCur->
15582 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f eState = CURSOR_
15583 49 4e 56 41 4c 49 44 3b 0a 20 20 72 63 20 3d 20 INVALID;. rc =
15584 62 74 72 65 65 4d 6f 76 65 74 6f 28 70 43 75 72 btreeMoveto(pCur
15585 2c 20 70 43 75 72 2d 3e 70 4b 65 79 2c 20 70 43 , pCur->pKey, pC
15586 75 72 2d 3e 6e 4b 65 79 2c 20 30 2c 20 26 70 43 ur->nKey, 0, &pC
15587 75 72 2d 3e 73 6b 69 70 4e 65 78 74 29 3b 0a 20 ur->skipNext);.
15588 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
15589 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 OK ){. sqlite
1558a 33 5f 66 72 65 65 28 70 43 75 72 2d 3e 70 4b 65 3_free(pCur->pKe
1558b 79 29 3b 0a 20 20 20 20 70 43 75 72 2d 3e 70 4b y);. pCur->pK
1558c 65 79 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 ey = 0;. asse
1558d 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 rt( pCur->eState
1558e 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 7c ==CURSOR_VALID |
1558f 7c 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d | pCur->eState==
15590 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 CURSOR_INVALID )
15591 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
15592 63 3b 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 72 65 c;.}..#define re
15593 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 storeCursorPosit
15594 69 6f 6e 28 70 29 20 5c 0a 20 20 28 70 2d 3e 65 ion(p) \. (p->e
15595 53 74 61 74 65 3e 3d 43 55 52 53 4f 52 5f 52 45 State>=CURSOR_RE
15596 51 55 49 52 45 53 45 45 4b 20 3f 20 5c 0a 20 20 QUIRESEEK ? \.
15597 20 20 20 20 20 20 20 62 74 72 65 65 52 65 73 74 btreeRest
15598 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f oreCursorPositio
15599 6e 28 70 29 20 3a 20 5c 0a 20 20 20 20 20 20 20 n(p) : \.
1559a 20 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 0a 2f 2a SQLITE_OK)../*
1559b 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 77 68 .** Determine wh
1559c 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 20 63 ether or not a c
1559d 75 72 73 6f 72 20 68 61 73 20 6d 6f 76 65 64 20 ursor has moved
1559e 66 72 6f 6d 20 74 68 65 20 70 6f 73 69 74 69 6f from the positio
1559f 6e 20 69 74 0a 2a 2a 20 77 61 73 20 6c 61 73 74 n it.** was last
155a0 20 70 6c 61 63 65 64 20 61 74 2e 20 20 43 75 72 placed at. Cur
155a1 73 6f 72 73 20 63 61 6e 20 6d 6f 76 65 20 77 68 sors can move wh
155a2 65 6e 20 74 68 65 20 72 6f 77 20 74 68 65 79 20 en the row they
155a3 61 72 65 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a 20 are pointing.**
155a4 61 74 20 69 73 20 64 65 6c 65 74 65 64 20 6f 75 at is deleted ou
155a5 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 74 68 65 t from under the
155a6 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f m..**.** This ro
155a7 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61 6e utine returns an
155a8 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 73 error code if s
155a9 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 omething goes wr
155aa 6f 6e 67 2e 20 20 54 68 65 0a 2a 2a 20 69 6e 74 ong. The.** int
155ab 65 67 65 72 20 2a 70 48 61 73 4d 6f 76 65 64 20 eger *pHasMoved
155ac 69 73 20 73 65 74 20 74 6f 20 6f 6e 65 20 69 66 is set to one if
155ad 20 74 68 65 20 63 75 72 73 6f 72 20 68 61 73 20 the cursor has
155ae 6d 6f 76 65 64 20 61 6e 64 20 30 20 69 66 20 6e moved and 0 if n
155af 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ot..*/.SQLITE_PR
155b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
155b1 33 42 74 72 65 65 43 75 72 73 6f 72 48 61 73 4d 3BtreeCursorHasM
155b2 6f 76 65 64 28 42 74 43 75 72 73 6f 72 20 2a 70 oved(BtCursor *p
155b3 43 75 72 2c 20 69 6e 74 20 2a 70 48 61 73 4d 6f Cur, int *pHasMo
155b4 76 65 64 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a ved){. int rc;.
155b5 0a 20 20 72 63 20 3d 20 72 65 73 74 6f 72 65 43 . rc = restoreC
155b6 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 ursorPosition(pC
155b7 75 72 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b ur);. if( rc ){
155b8 0a 20 20 20 20 2a 70 48 61 73 4d 6f 76 65 64 20 . *pHasMoved
155b9 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 = 1;. return
155ba 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 rc;. }. if( pC
155bb 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53 ur->eState!=CURS
155bc 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 OR_VALID || pCur
155bd 2d 3e 73 6b 69 70 4e 65 78 74 21 3d 30 20 29 7b ->skipNext!=0 ){
155be 0a 20 20 20 20 2a 70 48 61 73 4d 6f 76 65 64 20 . *pHasMoved
155bf 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 = 1;. }else{.
155c0 20 20 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20 30 *pHasMoved = 0
155c1 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 ;. }. return S
155c2 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 QLITE_OK;.}..#if
155c3 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
155c4 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a _AUTOVACUUM./*.*
155c5 2a 20 47 69 76 65 6e 20 61 20 70 61 67 65 20 6e * Given a page n
155c6 75 6d 62 65 72 20 6f 66 20 61 20 72 65 67 75 6c umber of a regul
155c7 61 72 20 64 61 74 61 62 61 73 65 20 70 61 67 65 ar database page
155c8 2c 20 72 65 74 75 72 6e 20 74 68 65 20 70 61 67 , return the pag
155c9 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 e.** number for
155ca 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 the pointer-map
155cb 70 61 67 65 20 74 68 61 74 20 63 6f 6e 74 61 69 page that contai
155cc 6e 73 20 74 68 65 20 65 6e 74 72 79 20 66 6f 72 ns the entry for
155cd 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 20 70 61 the.** input pa
155ce 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 ge number..*/.st
155cf 61 74 69 63 20 50 67 6e 6f 20 70 74 72 6d 61 70 atic Pgno ptrmap
155d0 50 61 67 65 6e 6f 28 42 74 53 68 61 72 65 64 20 Pageno(BtShared
155d1 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 *pBt, Pgno pgno)
155d2 7b 0a 20 20 69 6e 74 20 6e 50 61 67 65 73 50 65 {. int nPagesPe
155d3 72 4d 61 70 50 61 67 65 3b 0a 20 20 50 67 6e 6f rMapPage;. Pgno
155d4 20 69 50 74 72 4d 61 70 2c 20 72 65 74 3b 0a 20 iPtrMap, ret;.
155d5 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
155d6 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d _mutex_held(pBt-
155d7 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 6e 50 61 >mutex) );. nPa
155d8 67 65 73 50 65 72 4d 61 70 50 61 67 65 20 3d 20 gesPerMapPage =
155d9 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 (pBt->usableSize
155da 2f 35 29 2b 31 3b 0a 20 20 69 50 74 72 4d 61 70 /5)+1;. iPtrMap
155db 20 3d 20 28 70 67 6e 6f 2d 32 29 2f 6e 50 61 67 = (pgno-2)/nPag
155dc 65 73 50 65 72 4d 61 70 50 61 67 65 3b 0a 20 20 esPerMapPage;.
155dd 72 65 74 20 3d 20 28 69 50 74 72 4d 61 70 2a 6e ret = (iPtrMap*n
155de 50 61 67 65 73 50 65 72 4d 61 70 50 61 67 65 29 PagesPerMapPage)
155df 20 2b 20 32 3b 20 0a 20 20 69 66 28 20 72 65 74 + 2; . if( ret
155e0 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 ==PENDING_BYTE_P
155e1 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 AGE(pBt) ){.
155e2 72 65 74 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 ret++;. }. ret
155e3 75 72 6e 20 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a urn ret;.}../*.*
155e4 2a 20 57 72 69 74 65 20 61 6e 20 65 6e 74 72 79 * Write an entry
155e5 20 69 6e 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 into the pointe
155e6 72 20 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 r map..**.** Thi
155e7 73 20 72 6f 75 74 69 6e 65 20 75 70 64 61 74 65 s routine update
155e8 73 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 s the pointer ma
155e9 70 20 65 6e 74 72 79 20 66 6f 72 20 70 61 67 65 p entry for page
155ea 20 6e 75 6d 62 65 72 20 27 6b 65 79 27 0a 2a 2a number 'key'.**
155eb 20 73 6f 20 74 68 61 74 20 69 74 20 6d 61 70 73 so that it maps
155ec 20 74 6f 20 74 79 70 65 20 27 65 54 79 70 65 27 to type 'eType'
155ed 20 61 6e 64 20 70 61 72 65 6e 74 20 70 61 67 65 and parent page
155ee 20 6e 75 6d 62 65 72 20 27 70 67 6e 6f 27 2e 0a number 'pgno'..
155ef 2a 2a 0a 2a 2a 20 49 66 20 2a 70 52 43 20 69 73 **.** If *pRC is
155f0 20 69 6e 69 74 69 61 6c 6c 79 20 6e 6f 6e 2d 7a initially non-z
155f1 65 72 6f 20 28 6e 6f 6e 2d 53 51 4c 49 54 45 5f ero (non-SQLITE_
155f2 4f 4b 29 20 74 68 65 6e 20 74 68 69 73 20 72 6f OK) then this ro
155f3 75 74 69 6e 65 20 69 73 0a 2a 2a 20 61 20 6e 6f utine is.** a no
155f4 2d 6f 70 2e 20 20 49 66 20 61 6e 20 65 72 72 6f -op. If an erro
155f5 72 20 6f 63 63 75 72 73 2c 20 74 68 65 20 61 70 r occurs, the ap
155f6 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20 propriate error
155f7 63 6f 64 65 20 69 73 20 77 72 69 74 74 65 6e 0a code is written.
155f8 2a 2a 20 69 6e 74 6f 20 2a 70 52 43 2e 0a 2a 2f ** into *pRC..*/
155f9 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 74 72 .static void ptr
155fa 6d 61 70 50 75 74 28 42 74 53 68 61 72 65 64 20 mapPut(BtShared
155fb 2a 70 42 74 2c 20 50 67 6e 6f 20 6b 65 79 2c 20 *pBt, Pgno key,
155fc 75 38 20 65 54 79 70 65 2c 20 50 67 6e 6f 20 70 u8 eType, Pgno p
155fd 61 72 65 6e 74 2c 20 69 6e 74 20 2a 70 52 43 29 arent, int *pRC)
155fe 7b 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 {. DbPage *pDbP
155ff 61 67 65 3b 20 20 2f 2a 20 54 68 65 20 70 6f 69 age; /* The poi
15600 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 2a 2f nter map page */
15601 0a 20 20 75 38 20 2a 70 50 74 72 6d 61 70 3b 20 . u8 *pPtrmap;
15602 20 20 20 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e /* The poin
15603 74 65 72 20 6d 61 70 20 64 61 74 61 20 2a 2f 0a ter map data */.
15604 20 20 50 67 6e 6f 20 69 50 74 72 6d 61 70 3b 20 Pgno iPtrmap;
15605 20 20 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 /* The point
15606 65 72 20 6d 61 70 20 70 61 67 65 20 6e 75 6d 62 er map page numb
15607 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 73 er */. int offs
15608 65 74 3b 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 et; /* Off
15609 73 65 74 20 69 6e 20 70 6f 69 6e 74 65 72 20 6d set in pointer m
1560a 61 70 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 ap page */. int
1560b 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 2f rc; /
1560c 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72 * Return code fr
1560d 6f 6d 20 73 75 62 66 75 6e 63 74 69 6f 6e 73 20 om subfunctions
1560e 2a 2f 0a 0a 20 20 69 66 28 20 2a 70 52 43 20 29 */.. if( *pRC )
1560f 20 72 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 return;.. asse
15610 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
15611 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 x_held(pBt->mute
15612 78 29 20 29 3b 0a 20 20 2f 2a 20 54 68 65 20 6d x) );. /* The m
15613 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 70 61 aster-journal pa
15614 67 65 20 6e 75 6d 62 65 72 20 6d 75 73 74 20 6e ge number must n
15615 65 76 65 72 20 62 65 20 75 73 65 64 20 61 73 20 ever be used as
15616 61 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 a pointer map pa
15617 67 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 ge */. assert(
15618 30 3d 3d 50 54 52 4d 41 50 5f 49 53 50 41 47 45 0==PTRMAP_ISPAGE
15619 28 70 42 74 2c 20 50 45 4e 44 49 4e 47 5f 42 59 (pBt, PENDING_BY
1561a 54 45 5f 50 41 47 45 28 70 42 74 29 29 20 29 3b TE_PAGE(pBt)) );
1561b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d .. assert( pBt-
1561c 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 3b 0a 20 >autoVacuum );.
1561d 20 69 66 28 20 6b 65 79 3d 3d 30 20 29 7b 0a 20 if( key==0 ){.
1561e 20 20 20 2a 70 52 43 20 3d 20 53 51 4c 49 54 45 *pRC = SQLITE
1561f 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
15620 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 return;. }.
15621 20 69 50 74 72 6d 61 70 20 3d 20 50 54 52 4d 41 iPtrmap = PTRMA
15622 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 6b 65 P_PAGENO(pBt, ke
15623 79 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 y);. rc = sqlit
15624 65 33 50 61 67 65 72 47 65 74 28 70 42 74 2d 3e e3PagerGet(pBt->
15625 70 50 61 67 65 72 2c 20 69 50 74 72 6d 61 70 2c pPager, iPtrmap,
15626 20 26 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 &pDbPage);. if
15627 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
15628 29 7b 0a 20 20 20 20 2a 70 52 43 20 3d 20 72 63 ){. *pRC = rc
15629 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 ;. return;.
1562a 7d 0a 20 20 6f 66 66 73 65 74 20 3d 20 50 54 52 }. offset = PTR
1562b 4d 41 50 5f 50 54 52 4f 46 46 53 45 54 28 69 50 MAP_PTROFFSET(iP
1562c 74 72 6d 61 70 2c 20 6b 65 79 29 3b 0a 20 20 69 trmap, key);. i
1562d 66 28 20 6f 66 66 73 65 74 3c 30 20 29 7b 0a 20 f( offset<0 ){.
1562e 20 20 20 2a 70 52 43 20 3d 20 53 51 4c 49 54 45 *pRC = SQLITE
1562f 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
15630 20 20 20 67 6f 74 6f 20 70 74 72 6d 61 70 5f 65 goto ptrmap_e
15631 78 69 74 3b 0a 20 20 7d 0a 20 20 70 50 74 72 6d xit;. }. pPtrm
15632 61 70 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 ap = (u8 *)sqlit
15633 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 e3PagerGetData(p
15634 44 62 50 61 67 65 29 3b 0a 0a 20 20 69 66 28 20 DbPage);.. if(
15635 65 54 79 70 65 21 3d 70 50 74 72 6d 61 70 5b 6f eType!=pPtrmap[o
15636 66 66 73 65 74 5d 20 7c 7c 20 67 65 74 34 62 79 ffset] || get4by
15637 74 65 28 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 te(&pPtrmap[offs
15638 65 74 2b 31 5d 29 21 3d 70 61 72 65 6e 74 20 29 et+1])!=parent )
15639 7b 0a 20 20 20 20 54 52 41 43 45 28 28 22 50 54 {. TRACE(("PT
1563a 52 4d 41 50 5f 55 50 44 41 54 45 3a 20 25 64 2d RMAP_UPDATE: %d-
1563b 3e 28 25 64 2c 25 64 29 5c 6e 22 2c 20 6b 65 79 >(%d,%d)\n", key
1563c 2c 20 65 54 79 70 65 2c 20 70 61 72 65 6e 74 29 , eType, parent)
1563d 29 3b 0a 20 20 20 20 2a 70 52 43 3d 20 72 63 20 );. *pRC= rc
1563e 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
1563f 69 74 65 28 70 44 62 50 61 67 65 29 3b 0a 20 20 ite(pDbPage);.
15640 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
15641 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 74 _OK ){. pPt
15642 72 6d 61 70 5b 6f 66 66 73 65 74 5d 20 3d 20 65 rmap[offset] = e
15643 54 79 70 65 3b 0a 20 20 20 20 20 20 70 75 74 34 Type;. put4
15644 62 79 74 65 28 26 70 50 74 72 6d 61 70 5b 6f 66 byte(&pPtrmap[of
15645 66 73 65 74 2b 31 5d 2c 20 70 61 72 65 6e 74 29 fset+1], parent)
15646 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 70 74 72 ;. }. }..ptr
15647 6d 61 70 5f 65 78 69 74 3a 0a 20 20 73 71 6c 69 map_exit:. sqli
15648 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 44 te3PagerUnref(pD
15649 62 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a bPage);.}../*.**
1564a 20 52 65 61 64 20 61 6e 20 65 6e 74 72 79 20 66 Read an entry f
1564b 72 6f 6d 20 74 68 65 20 70 6f 69 6e 74 65 72 20 rom the pointer
1564c 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 map..**.** This
1564d 72 6f 75 74 69 6e 65 20 72 65 74 72 69 65 76 65 routine retrieve
1564e 73 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 s the pointer ma
1564f 70 20 65 6e 74 72 79 20 66 6f 72 20 70 61 67 65 p entry for page
15650 20 27 6b 65 79 27 2c 20 77 72 69 74 69 6e 67 0a 'key', writing.
15651 2a 2a 20 74 68 65 20 74 79 70 65 20 61 6e 64 20 ** the type and
15652 70 61 72 65 6e 74 20 70 61 67 65 20 6e 75 6d 62 parent page numb
15653 65 72 20 74 6f 20 2a 70 45 54 79 70 65 20 61 6e er to *pEType an
15654 64 20 2a 70 50 67 6e 6f 20 72 65 73 70 65 63 74 d *pPgno respect
15655 69 76 65 6c 79 2e 0a 2a 2a 20 41 6e 20 65 72 72 ively..** An err
15656 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 or code is retur
15657 6e 65 64 20 69 66 20 73 6f 6d 65 74 68 69 6e 67 ned if something
15658 20 67 6f 65 73 20 77 72 6f 6e 67 2c 20 6f 74 68 goes wrong, oth
15659 65 72 77 69 73 65 20 53 51 4c 49 54 45 5f 4f 4b erwise SQLITE_OK
1565a 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
1565b 70 74 72 6d 61 70 47 65 74 28 42 74 53 68 61 72 ptrmapGet(BtShar
1565c 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 6b 65 ed *pBt, Pgno ke
1565d 79 2c 20 75 38 20 2a 70 45 54 79 70 65 2c 20 50 y, u8 *pEType, P
1565e 67 6e 6f 20 2a 70 50 67 6e 6f 29 7b 0a 20 20 44 gno *pPgno){. D
1565f 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b 20 bPage *pDbPage;
15660 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 /* The pointer
15661 20 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20 20 69 map page */. i
15662 6e 74 20 69 50 74 72 6d 61 70 3b 20 20 20 20 20 nt iPtrmap;
15663 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70 /* Pointer map
15664 20 70 61 67 65 20 69 6e 64 65 78 20 2a 2f 0a 20 page index */.
15665 20 75 38 20 2a 70 50 74 72 6d 61 70 3b 20 20 20 u8 *pPtrmap;
15666 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 6d /* Pointer m
15667 61 70 20 70 61 67 65 20 64 61 74 61 20 2a 2f 0a ap page data */.
15668 20 20 69 6e 74 20 6f 66 66 73 65 74 3b 20 20 20 int offset;
15669 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f /* Offset o
1566a 66 20 65 6e 74 72 79 20 69 6e 20 70 6f 69 6e 74 f entry in point
1566b 65 72 20 6d 61 70 20 2a 2f 0a 20 20 69 6e 74 20 er map */. int
1566c 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 rc;.. assert( s
1566d 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
1566e 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b d(pBt->mutex) );
1566f 0a 0a 20 20 69 50 74 72 6d 61 70 20 3d 20 50 54 .. iPtrmap = PT
15670 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c RMAP_PAGENO(pBt,
15671 20 6b 65 79 29 3b 0a 20 20 72 63 20 3d 20 73 71 key);. rc = sq
15672 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 42 lite3PagerGet(pB
15673 74 2d 3e 70 50 61 67 65 72 2c 20 69 50 74 72 6d t->pPager, iPtrm
15674 61 70 2c 20 26 70 44 62 50 61 67 65 29 3b 0a 20 ap, &pDbPage);.
15675 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 if( rc!=0 ){.
15676 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d return rc;. }
15677 0a 20 20 70 50 74 72 6d 61 70 20 3d 20 28 75 38 . pPtrmap = (u8
15678 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 *)sqlite3PagerG
15679 65 74 44 61 74 61 28 70 44 62 50 61 67 65 29 3b etData(pDbPage);
1567a 0a 0a 20 20 6f 66 66 73 65 74 20 3d 20 50 54 52 .. offset = PTR
1567b 4d 41 50 5f 50 54 52 4f 46 46 53 45 54 28 69 50 MAP_PTROFFSET(iP
1567c 74 72 6d 61 70 2c 20 6b 65 79 29 3b 0a 20 20 61 trmap, key);. a
1567d 73 73 65 72 74 28 20 70 45 54 79 70 65 21 3d 30 ssert( pEType!=0
1567e 20 29 3b 0a 20 20 2a 70 45 54 79 70 65 20 3d 20 );. *pEType =
1567f 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 5d 3b pPtrmap[offset];
15680 0a 20 20 69 66 28 20 70 50 67 6e 6f 20 29 20 2a . if( pPgno ) *
15681 70 50 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 pPgno = get4byte
15682 28 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 (&pPtrmap[offset
15683 2b 31 5d 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 +1]);.. sqlite3
15684 50 61 67 65 72 55 6e 72 65 66 28 70 44 62 50 61 PagerUnref(pDbPa
15685 67 65 29 3b 0a 20 20 69 66 28 20 2a 70 45 54 79 ge);. if( *pETy
15686 70 65 3c 31 20 7c 7c 20 2a 70 45 54 79 70 65 3e pe<1 || *pEType>
15687 35 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 5 ) return SQLIT
15688 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
15689 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1568a 4f 4b 3b 0a 7d 0a 0a 23 65 6c 73 65 20 2f 2a 20 OK;.}..#else /*
1568b 69 66 20 64 65 66 69 6e 65 64 20 53 51 4c 49 54 if defined SQLIT
1568c 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
1568d 4d 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 70 M */. #define p
1568e 74 72 6d 61 70 50 75 74 28 77 2c 78 2c 79 2c 7a trmapPut(w,x,y,z
1568f 2c 72 63 29 0a 20 20 23 64 65 66 69 6e 65 20 70 ,rc). #define p
15690 74 72 6d 61 70 47 65 74 28 77 2c 78 2c 79 2c 7a trmapGet(w,x,y,z
15691 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 ) SQLITE_OK. #d
15692 65 66 69 6e 65 20 70 74 72 6d 61 70 50 75 74 4f efine ptrmapPutO
15693 76 66 6c 50 74 72 28 78 2c 20 79 2c 20 72 63 29 vflPtr(x, y, rc)
15694 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 .#endif../*.** G
15695 69 76 65 6e 20 61 20 62 74 72 65 65 20 70 61 67 iven a btree pag
15696 65 20 61 6e 64 20 61 20 63 65 6c 6c 20 69 6e 64 e and a cell ind
15697 65 78 20 28 30 20 6d 65 61 6e 73 20 74 68 65 20 ex (0 means the
15698 66 69 72 73 74 20 63 65 6c 6c 20 6f 6e 0a 2a 2a first cell on.**
15699 20 74 68 65 20 70 61 67 65 2c 20 31 20 6d 65 61 the page, 1 mea
1569a 6e 73 20 74 68 65 20 73 65 63 6f 6e 64 20 63 65 ns the second ce
1569b 6c 6c 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 ll, and so forth
1569c 29 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 ) return a point
1569d 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 63 65 6c er.** to the cel
1569e 6c 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a 2a 2a l content..**.**
1569f 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 6f This routine wo
156a0 72 6b 73 20 6f 6e 6c 79 20 66 6f 72 20 70 61 67 rks only for pag
156a1 65 73 20 74 68 61 74 20 64 6f 20 6e 6f 74 20 63 es that do not c
156a2 6f 6e 74 61 69 6e 20 6f 76 65 72 66 6c 6f 77 20 ontain overflow
156a3 63 65 6c 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e cells..*/.#defin
156a4 65 20 66 69 6e 64 43 65 6c 6c 28 50 2c 49 29 20 e findCell(P,I)
156a5 5c 0a 20 20 28 28 50 29 2d 3e 61 44 61 74 61 20 \. ((P)->aData
156a6 2b 20 28 28 50 29 2d 3e 6d 61 73 6b 50 61 67 65 + ((P)->maskPage
156a7 20 26 20 67 65 74 32 62 79 74 65 28 26 28 50 29 & get2byte(&(P)
156a8 2d 3e 61 44 61 74 61 5b 28 50 29 2d 3e 63 65 6c ->aData[(P)->cel
156a9 6c 4f 66 66 73 65 74 2b 32 2a 28 49 29 5d 29 29 lOffset+2*(I)]))
156aa 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 61 20 )../*.** This a
156ab 6d 6f 72 65 20 63 6f 6d 70 6c 65 78 20 76 65 72 more complex ver
156ac 73 69 6f 6e 20 6f 66 20 66 69 6e 64 43 65 6c 6c sion of findCell
156ad 28 29 20 74 68 61 74 20 77 6f 72 6b 73 20 66 6f () that works fo
156ae 72 0a 2a 2a 20 70 61 67 65 73 20 74 68 61 74 20 r.** pages that
156af 64 6f 20 63 6f 6e 74 61 69 6e 20 6f 76 65 72 66 do contain overf
156b0 6c 6f 77 20 63 65 6c 6c 73 2e 0a 2a 2f 0a 73 74 low cells..*/.st
156b1 61 74 69 63 20 75 38 20 2a 66 69 6e 64 4f 76 65 atic u8 *findOve
156b2 72 66 6c 6f 77 43 65 6c 6c 28 4d 65 6d 50 61 67 rflowCell(MemPag
156b3 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 69 43 e *pPage, int iC
156b4 65 6c 6c 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 ell){. int i;.
156b5 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
156b6 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 _mutex_held(pPag
156b7 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 e->pBt->mutex) )
156b8 3b 0a 20 20 66 6f 72 28 69 3d 70 50 61 67 65 2d ;. for(i=pPage-
156b9 3e 6e 4f 76 65 72 66 6c 6f 77 2d 31 3b 20 69 3e >nOverflow-1; i>
156ba 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 69 6e =0; i--){. in
156bb 74 20 6b 3b 0a 20 20 20 20 73 74 72 75 63 74 20 t k;. struct
156bc 5f 4f 76 66 6c 43 65 6c 6c 20 2a 70 4f 76 66 6c _OvflCell *pOvfl
156bd 3b 0a 20 20 20 20 70 4f 76 66 6c 20 3d 20 26 70 ;. pOvfl = &p
156be 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 69 5d 3b 0a Page->aOvfl[i];.
156bf 20 20 20 20 6b 20 3d 20 70 4f 76 66 6c 2d 3e 69 k = pOvfl->i
156c0 64 78 3b 0a 20 20 20 20 69 66 28 20 6b 3c 3d 69 dx;. if( k<=i
156c1 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 Cell ){. if
156c2 28 20 6b 3d 3d 69 43 65 6c 6c 20 29 7b 0a 20 20 ( k==iCell ){.
156c3 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 4f 76 return pOv
156c4 66 6c 2d 3e 70 43 65 6c 6c 3b 0a 20 20 20 20 20 fl->pCell;.
156c5 20 7d 0a 20 20 20 20 20 20 69 43 65 6c 6c 2d 2d }. iCell--
156c6 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
156c7 74 75 72 6e 20 66 69 6e 64 43 65 6c 6c 28 70 50 turn findCell(pP
156c8 61 67 65 2c 20 69 43 65 6c 6c 29 3b 0a 7d 0a 0a age, iCell);.}..
156c9 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 61 20 63 65 /*.** Parse a ce
156ca 6c 6c 20 63 6f 6e 74 65 6e 74 20 62 6c 6f 63 6b ll content block
156cb 20 61 6e 64 20 66 69 6c 6c 20 69 6e 20 74 68 65 and fill in the
156cc 20 43 65 6c 6c 49 6e 66 6f 20 73 74 72 75 63 74 CellInfo struct
156cd 75 72 65 2e 20 20 54 68 65 72 65 0a 2a 2a 20 61 ure. There.** a
156ce 72 65 20 74 77 6f 20 76 65 72 73 69 6f 6e 73 20 re two versions
156cf 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e of this function
156d0 2e 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c . btreeParseCel
156d1 6c 28 29 20 74 61 6b 65 73 20 61 20 0a 2a 2a 20 l() takes a .**
156d2 63 65 6c 6c 20 69 6e 64 65 78 20 61 73 20 74 68 cell index as th
156d3 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
156d4 74 20 61 6e 64 20 62 74 72 65 65 50 61 72 73 65 t and btreeParse
156d5 43 65 6c 6c 50 74 72 28 29 20 0a 2a 2a 20 74 61 CellPtr() .** ta
156d6 6b 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f kes a pointer to
156d7 20 74 68 65 20 62 6f 64 79 20 6f 66 20 74 68 65 the body of the
156d8 20 63 65 6c 6c 20 61 73 20 69 74 73 20 73 65 63 cell as its sec
156d9 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a ond argument..**
156da 0a 2a 2a 20 57 69 74 68 69 6e 20 74 68 69 73 20 .** Within this
156db 66 69 6c 65 2c 20 74 68 65 20 70 61 72 73 65 43 file, the parseC
156dc 65 6c 6c 28 29 20 6d 61 63 72 6f 20 63 61 6e 20 ell() macro can
156dd 62 65 20 63 61 6c 6c 65 64 20 69 6e 73 74 65 61 be called instea
156de 64 20 6f 66 0a 2a 2a 20 62 74 72 65 65 50 61 72 d of.** btreePar
156df 73 65 43 65 6c 6c 50 74 72 28 29 2e 20 55 73 69 seCellPtr(). Usi
156e0 6e 67 20 73 6f 6d 65 20 63 6f 6d 70 69 6c 65 72 ng some compiler
156e1 73 2c 20 74 68 69 73 20 77 69 6c 6c 20 62 65 20 s, this will be
156e2 66 61 73 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 faster..*/.stati
156e3 63 20 76 6f 69 64 20 62 74 72 65 65 50 61 72 73 c void btreePars
156e4 65 43 65 6c 6c 50 74 72 28 0a 20 20 4d 65 6d 50 eCellPtr(. MemP
156e5 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 20 20 age *pPage,
156e6 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 /* Page cont
156e7 61 69 6e 69 6e 67 20 74 68 65 20 63 65 6c 6c 20 aining the cell
156e8 2a 2f 0a 20 20 75 38 20 2a 70 43 65 6c 6c 2c 20 */. u8 *pCell,
156e9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
156ea 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 Pointer to the c
156eb 65 6c 6c 20 74 65 78 74 2e 20 2a 2f 0a 20 20 43 ell text. */. C
156ec 65 6c 6c 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 20 ellInfo *pInfo
156ed 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 /* Fill i
156ee 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 n this structure
156ef 20 2a 2f 0a 29 7b 0a 20 20 75 31 36 20 6e 3b 20 */.){. u16 n;
156f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
156f1 20 2f 2a 20 4e 75 6d 62 65 72 20 62 79 74 65 73 /* Number bytes
156f2 20 69 6e 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 in cell content
156f3 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 header */. u32
156f4 20 6e 50 61 79 6c 6f 61 64 3b 20 20 20 20 20 20 nPayload;
156f5 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
156f6 66 20 62 79 74 65 73 20 6f 66 20 63 65 6c 6c 20 f bytes of cell
156f7 70 61 79 6c 6f 61 64 20 2a 2f 0a 0a 20 20 61 73 payload */.. as
156f8 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
156f9 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e tex_held(pPage->
156fa 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a pBt->mutex) );..
156fb 20 20 70 49 6e 66 6f 2d 3e 70 43 65 6c 6c 20 3d pInfo->pCell =
156fc 20 70 43 65 6c 6c 3b 0a 20 20 61 73 73 65 72 74 pCell;. assert
156fd 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 30 ( pPage->leaf==0
156fe 20 7c 7c 20 70 50 61 67 65 2d 3e 6c 65 61 66 3d || pPage->leaf=
156ff 3d 31 20 29 3b 0a 20 20 6e 20 3d 20 70 50 61 67 =1 );. n = pPag
15700 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 3b e->childPtrSize;
15701 0a 20 20 61 73 73 65 72 74 28 20 6e 3d 3d 34 2d . assert( n==4-
15702 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 4*pPage->leaf );
15703 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e . if( pPage->in
15704 74 4b 65 79 20 29 7b 0a 20 20 20 20 69 66 28 20 tKey ){. if(
15705 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20 29 pPage->hasData )
15706 7b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 67 65 74 {. n += get
15707 56 61 72 69 6e 74 33 32 28 26 70 43 65 6c 6c 5b Varint32(&pCell[
15708 6e 5d 2c 20 6e 50 61 79 6c 6f 61 64 29 3b 0a 20 n], nPayload);.
15709 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1570a 6e 50 61 79 6c 6f 61 64 20 3d 20 30 3b 0a 20 20 nPayload = 0;.
1570b 20 20 7d 0a 20 20 20 20 6e 20 2b 3d 20 67 65 74 }. n += get
1570c 56 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b 6e 5d Varint(&pCell[n]
1570d 2c 20 28 75 36 34 2a 29 26 70 49 6e 66 6f 2d 3e , (u64*)&pInfo->
1570e 6e 4b 65 79 29 3b 0a 20 20 20 20 70 49 6e 66 6f nKey);. pInfo
1570f 2d 3e 6e 44 61 74 61 20 3d 20 6e 50 61 79 6c 6f ->nData = nPaylo
15710 61 64 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ad;. }else{.
15711 20 70 49 6e 66 6f 2d 3e 6e 44 61 74 61 20 3d 20 pInfo->nData =
15712 30 3b 0a 20 20 20 20 6e 20 2b 3d 20 67 65 74 56 0;. n += getV
15713 61 72 69 6e 74 33 32 28 26 70 43 65 6c 6c 5b 6e arint32(&pCell[n
15714 5d 2c 20 6e 50 61 79 6c 6f 61 64 29 3b 0a 20 20 ], nPayload);.
15715 20 20 70 49 6e 66 6f 2d 3e 6e 4b 65 79 20 3d 20 pInfo->nKey =
15716 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 7d 0a 20 20 nPayload;. }.
15717 70 49 6e 66 6f 2d 3e 6e 50 61 79 6c 6f 61 64 20 pInfo->nPayload
15718 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 70 49 = nPayload;. pI
15719 6e 66 6f 2d 3e 6e 48 65 61 64 65 72 20 3d 20 6e nfo->nHeader = n
1571a 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 50 ;. testcase( nP
1571b 61 79 6c 6f 61 64 3d 3d 70 50 61 67 65 2d 3e 6d ayload==pPage->m
1571c 61 78 4c 6f 63 61 6c 20 29 3b 0a 20 20 74 65 73 axLocal );. tes
1571d 74 63 61 73 65 28 20 6e 50 61 79 6c 6f 61 64 3d tcase( nPayload=
1571e 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c =pPage->maxLocal
1571f 2b 31 20 29 3b 0a 20 20 69 66 28 20 6c 69 6b 65 +1 );. if( like
15720 6c 79 28 6e 50 61 79 6c 6f 61 64 3c 3d 70 50 61 ly(nPayload<=pPa
15721 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 29 20 29 7b ge->maxLocal) ){
15722 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 . /* This is
15723 74 68 65 20 28 65 61 73 79 29 20 63 6f 6d 6d 6f the (easy) commo
15724 6e 20 63 61 73 65 20 77 68 65 72 65 20 74 68 65 n case where the
15725 20 65 6e 74 69 72 65 20 70 61 79 6c 6f 61 64 20 entire payload
15726 66 69 74 73 0a 20 20 20 20 2a 2a 20 6f 6e 20 74 fits. ** on t
15727 68 65 20 6c 6f 63 61 6c 20 70 61 67 65 2e 20 20 he local page.
15728 4e 6f 20 6f 76 65 72 66 6c 6f 77 20 69 73 20 72 No overflow is r
15729 65 71 75 69 72 65 64 2e 0a 20 20 20 20 2a 2f 0a equired.. */.
1572a 20 20 20 20 69 6e 74 20 6e 53 69 7a 65 3b 20 20 int nSize;
1572b 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c /* Total
1572c 20 73 69 7a 65 20 6f 66 20 63 65 6c 6c 20 63 6f size of cell co
1572d 6e 74 65 6e 74 20 69 6e 20 62 79 74 65 73 20 2a ntent in bytes *
1572e 2f 0a 20 20 20 20 6e 53 69 7a 65 20 3d 20 6e 50 /. nSize = nP
1572f 61 79 6c 6f 61 64 20 2b 20 6e 3b 0a 20 20 20 20 ayload + n;.
15730 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 pInfo->nLocal =
15731 28 75 31 36 29 6e 50 61 79 6c 6f 61 64 3b 0a 20 (u16)nPayload;.
15732 20 20 20 70 49 6e 66 6f 2d 3e 69 4f 76 65 72 66 pInfo->iOverf
15733 6c 6f 77 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 low = 0;. if(
15734 20 28 6e 53 69 7a 65 20 26 20 7e 33 29 3d 3d 30 (nSize & ~3)==0
15735 20 29 7b 0a 20 20 20 20 20 20 6e 53 69 7a 65 20 ){. nSize
15736 3d 20 34 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d = 4; /* M
15737 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 inimum cell size
15738 20 69 73 20 34 20 2a 2f 0a 20 20 20 20 7d 0a 20 is 4 */. }.
15739 20 20 20 70 49 6e 66 6f 2d 3e 6e 53 69 7a 65 20 pInfo->nSize
1573a 3d 20 28 75 31 36 29 6e 53 69 7a 65 3b 0a 20 20 = (u16)nSize;.
1573b 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 }else{. /* If
1573c 20 74 68 65 20 70 61 79 6c 6f 61 64 20 77 69 6c the payload wil
1573d 6c 20 6e 6f 74 20 66 69 74 20 63 6f 6d 70 6c 65 l not fit comple
1573e 74 65 6c 79 20 6f 6e 20 74 68 65 20 6c 6f 63 61 tely on the loca
1573f 6c 20 70 61 67 65 2c 20 77 65 20 68 61 76 65 0a l page, we have.
15740 20 20 20 20 2a 2a 20 74 6f 20 64 65 63 69 64 65 ** to decide
15741 20 68 6f 77 20 6d 75 63 68 20 74 6f 20 73 74 6f how much to sto
15742 72 65 20 6c 6f 63 61 6c 6c 79 20 61 6e 64 20 68 re locally and h
15743 6f 77 20 6d 75 63 68 20 74 6f 20 73 70 69 6c 6c ow much to spill
15744 20 6f 6e 74 6f 0a 20 20 20 20 2a 2a 20 6f 76 65 onto. ** ove
15745 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 20 54 68 rflow pages. Th
15746 65 20 73 74 72 61 74 65 67 79 20 69 73 20 74 6f e strategy is to
15747 20 6d 69 6e 69 6d 69 7a 65 20 74 68 65 20 61 6d minimize the am
15748 6f 75 6e 74 20 6f 66 20 75 6e 75 73 65 64 0a 20 ount of unused.
15749 20 20 20 2a 2a 20 73 70 61 63 65 20 6f 6e 20 6f ** space on o
1574a 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 77 68 verflow pages wh
1574b 69 6c 65 20 6b 65 65 70 69 6e 67 20 74 68 65 20 ile keeping the
1574c 61 6d 6f 75 6e 74 20 6f 66 20 6c 6f 63 61 6c 20 amount of local
1574d 73 74 6f 72 61 67 65 0a 20 20 20 20 2a 2a 20 69 storage. ** i
1574e 6e 20 62 65 74 77 65 65 6e 20 6d 69 6e 4c 6f 63 n between minLoc
1574f 61 6c 20 61 6e 64 20 6d 61 78 4c 6f 63 61 6c 2e al and maxLocal.
15750 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 57 . **. ** W
15751 61 72 6e 69 6e 67 3a 20 20 63 68 61 6e 67 69 6e arning: changin
15752 67 20 74 68 65 20 77 61 79 20 6f 76 65 72 66 6c g the way overfl
15753 6f 77 20 70 61 79 6c 6f 61 64 20 69 73 20 64 69 ow payload is di
15754 73 74 72 69 62 75 74 65 64 20 69 6e 20 61 6e 79 stributed in any
15755 0a 20 20 20 20 2a 2a 20 77 61 79 20 77 69 6c 6c . ** way will
15756 20 72 65 73 75 6c 74 20 69 6e 20 61 6e 20 69 6e result in an in
15757 63 6f 6d 70 61 74 69 62 6c 65 20 66 69 6c 65 20 compatible file
15758 66 6f 72 6d 61 74 2e 0a 20 20 20 20 2a 2f 0a 20 format.. */.
15759 20 20 20 69 6e 74 20 6d 69 6e 4c 6f 63 61 6c 3b int minLocal;
1575a 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 61 6d 6f /* Minimum amo
1575b 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 unt of payload h
1575c 65 6c 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 eld locally */.
1575d 20 20 20 69 6e 74 20 6d 61 78 4c 6f 63 61 6c 3b int maxLocal;
1575e 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 6d 6f /* Maximum amo
1575f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 unt of payload h
15760 65 6c 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 eld locally */.
15761 20 20 20 69 6e 74 20 73 75 72 70 6c 75 73 3b 20 int surplus;
15762 20 20 2f 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 /* Overflow pa
15763 79 6c 6f 61 64 20 61 76 61 69 6c 61 62 6c 65 20 yload available
15764 66 6f 72 20 6c 6f 63 61 6c 20 73 74 6f 72 61 67 for local storag
15765 65 20 2a 2f 0a 0a 20 20 20 20 6d 69 6e 4c 6f 63 e */.. minLoc
15766 61 6c 20 3d 20 70 50 61 67 65 2d 3e 6d 69 6e 4c al = pPage->minL
15767 6f 63 61 6c 3b 0a 20 20 20 20 6d 61 78 4c 6f 63 ocal;. maxLoc
15768 61 6c 20 3d 20 70 50 61 67 65 2d 3e 6d 61 78 4c al = pPage->maxL
15769 6f 63 61 6c 3b 0a 20 20 20 20 73 75 72 70 6c 75 ocal;. surplu
1576a 73 20 3d 20 6d 69 6e 4c 6f 63 61 6c 20 2b 20 28 s = minLocal + (
1576b 6e 50 61 79 6c 6f 61 64 20 2d 20 6d 69 6e 4c 6f nPayload - minLo
1576c 63 61 6c 29 25 28 70 50 61 67 65 2d 3e 70 42 74 cal)%(pPage->pBt
1576d 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 ->usableSize - 4
1576e 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 );. testcase(
1576f 20 73 75 72 70 6c 75 73 3d 3d 6d 61 78 4c 6f 63 surplus==maxLoc
15770 61 6c 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 al );. testca
15771 73 65 28 20 73 75 72 70 6c 75 73 3d 3d 6d 61 78 se( surplus==max
15772 4c 6f 63 61 6c 2b 31 20 29 3b 0a 20 20 20 20 69 Local+1 );. i
15773 66 28 20 73 75 72 70 6c 75 73 20 3c 3d 20 6d 61 f( surplus <= ma
15774 78 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 xLocal ){.
15775 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 pInfo->nLocal =
15776 28 75 31 36 29 73 75 72 70 6c 75 73 3b 0a 20 20 (u16)surplus;.
15777 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 }else{. p
15778 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 Info->nLocal = (
15779 75 31 36 29 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 u16)minLocal;.
1577a 20 20 7d 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 69 }. pInfo->i
1577b 4f 76 65 72 66 6c 6f 77 20 3d 20 28 75 31 36 29 Overflow = (u16)
1577c 28 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 2b (pInfo->nLocal +
1577d 20 6e 29 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e n);. pInfo->
1577e 6e 53 69 7a 65 20 3d 20 70 49 6e 66 6f 2d 3e 69 nSize = pInfo->i
1577f 4f 76 65 72 66 6c 6f 77 20 2b 20 34 3b 0a 20 20 Overflow + 4;.
15780 7d 0a 7d 0a 23 64 65 66 69 6e 65 20 70 61 72 73 }.}.#define pars
15781 65 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 eCell(pPage, iCe
15782 6c 6c 2c 20 70 49 6e 66 6f 29 20 5c 0a 20 20 62 ll, pInfo) \. b
15783 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 treeParseCellPtr
15784 28 28 70 50 61 67 65 29 2c 20 66 69 6e 64 43 65 ((pPage), findCe
15785 6c 6c 28 28 70 50 61 67 65 29 2c 20 28 69 43 65 ll((pPage), (iCe
15786 6c 6c 29 29 2c 20 28 70 49 6e 66 6f 29 29 0a 73 ll)), (pInfo)).s
15787 74 61 74 69 63 20 76 6f 69 64 20 62 74 72 65 65 tatic void btree
15788 50 61 72 73 65 43 65 6c 6c 28 0a 20 20 4d 65 6d ParseCell(. Mem
15789 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 20 Page *pPage,
1578a 20 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e /* Page con
1578b 74 61 69 6e 69 6e 67 20 74 68 65 20 63 65 6c 6c taining the cell
1578c 20 2a 2f 0a 20 20 69 6e 74 20 69 43 65 6c 6c 2c */. int iCell,
1578d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1578e 20 54 68 65 20 63 65 6c 6c 20 69 6e 64 65 78 2e The cell index.
1578f 20 20 46 69 72 73 74 20 63 65 6c 6c 20 69 73 20 First cell is
15790 30 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 0 */. CellInfo
15791 2a 70 49 6e 66 6f 20 20 20 20 20 20 20 20 20 2f *pInfo /
15792 2a 20 46 69 6c 6c 20 69 6e 20 74 68 69 73 20 73 * Fill in this s
15793 74 72 75 63 74 75 72 65 20 2a 2f 0a 29 7b 0a 20 tructure */.){.
15794 20 70 61 72 73 65 43 65 6c 6c 28 70 50 61 67 65 parseCell(pPage
15795 2c 20 69 43 65 6c 6c 2c 20 70 49 6e 66 6f 29 3b , iCell, pInfo);
15796 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 .}../*.** Comput
15797 65 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 e the total numb
15798 65 72 20 6f 66 20 62 79 74 65 73 20 74 68 61 74 er of bytes that
15799 20 61 20 43 65 6c 6c 20 6e 65 65 64 73 20 69 6e a Cell needs in
1579a 20 74 68 65 20 63 65 6c 6c 0a 2a 2a 20 64 61 74 the cell.** dat
1579b 61 20 61 72 65 61 20 6f 66 20 74 68 65 20 62 74 a area of the bt
1579c 72 65 65 2d 70 61 67 65 2e 20 20 54 68 65 20 72 ree-page. The r
1579d 65 74 75 72 6e 20 6e 75 6d 62 65 72 20 69 6e 63 eturn number inc
1579e 6c 75 64 65 73 20 74 68 65 20 63 65 6c 6c 0a 2a ludes the cell.*
1579f 2a 20 64 61 74 61 20 68 65 61 64 65 72 20 61 6e * data header an
157a0 64 20 74 68 65 20 6c 6f 63 61 6c 20 70 61 79 6c d the local payl
157a1 6f 61 64 2c 20 62 75 74 20 6e 6f 74 20 61 6e 79 oad, but not any
157a2 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6f overflow page o
157a3 72 0a 2a 2a 20 74 68 65 20 73 70 61 63 65 20 75 r.** the space u
157a4 73 65 64 20 62 79 20 74 68 65 20 63 65 6c 6c 20 sed by the cell
157a5 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 pointer..*/.stat
157a6 69 63 20 75 31 36 20 63 65 6c 6c 53 69 7a 65 50 ic u16 cellSizeP
157a7 74 72 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 tr(MemPage *pPag
157a8 65 2c 20 75 38 20 2a 70 43 65 6c 6c 29 7b 0a 20 e, u8 *pCell){.
157a9 20 75 38 20 2a 70 49 74 65 72 20 3d 20 26 70 43 u8 *pIter = &pC
157aa 65 6c 6c 5b 70 50 61 67 65 2d 3e 63 68 69 6c 64 ell[pPage->child
157ab 50 74 72 53 69 7a 65 5d 3b 0a 20 20 75 33 32 20 PtrSize];. u32
157ac 6e 53 69 7a 65 3b 0a 0a 23 69 66 64 65 66 20 53 nSize;..#ifdef S
157ad 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 2f 2a QLITE_DEBUG. /*
157ae 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 The value retur
157af 6e 65 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 ned by this func
157b0 74 69 6f 6e 20 73 68 6f 75 6c 64 20 61 6c 77 61 tion should alwa
157b1 79 73 20 62 65 20 74 68 65 20 73 61 6d 65 20 61 ys be the same a
157b2 73 0a 20 20 2a 2a 20 74 68 65 20 28 43 65 6c 6c s. ** the (Cell
157b3 49 6e 66 6f 2e 6e 53 69 7a 65 29 20 76 61 6c 75 Info.nSize) valu
157b4 65 20 66 6f 75 6e 64 20 62 79 20 64 6f 69 6e 67 e found by doing
157b5 20 61 20 66 75 6c 6c 20 70 61 72 73 65 20 6f 66 a full parse of
157b6 20 74 68 65 0a 20 20 2a 2a 20 63 65 6c 6c 2e 20 the. ** cell.
157b7 49 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 If SQLITE_DEBUG
157b8 69 73 20 64 65 66 69 6e 65 64 2c 20 61 6e 20 61 is defined, an a
157b9 73 73 65 72 74 28 29 20 61 74 20 74 68 65 20 62 ssert() at the b
157ba 6f 74 74 6f 6d 20 6f 66 0a 20 20 2a 2a 20 74 68 ottom of. ** th
157bb 69 73 20 66 75 6e 63 74 69 6f 6e 20 76 65 72 69 is function veri
157bc 66 69 65 73 20 74 68 61 74 20 74 68 69 73 20 69 fies that this i
157bd 6e 76 61 72 69 61 6e 74 20 69 73 20 6e 6f 74 20 nvariant is not
157be 76 69 6f 6c 61 74 65 64 2e 20 2a 2f 0a 20 20 43 violated. */. C
157bf 65 6c 6c 49 6e 66 6f 20 64 65 62 75 67 69 6e 66 ellInfo debuginf
157c0 6f 3b 0a 20 20 62 74 72 65 65 50 61 72 73 65 43 o;. btreeParseC
157c1 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 ellPtr(pPage, pC
157c2 65 6c 6c 2c 20 26 64 65 62 75 67 69 6e 66 6f 29 ell, &debuginfo)
157c3 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 ;.#endif.. if(
157c4 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b pPage->intKey ){
157c5 0a 20 20 20 20 75 38 20 2a 70 45 6e 64 3b 0a 20 . u8 *pEnd;.
157c6 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 68 61 if( pPage->ha
157c7 73 44 61 74 61 20 29 7b 0a 20 20 20 20 20 20 70 sData ){. p
157c8 49 74 65 72 20 2b 3d 20 67 65 74 56 61 72 69 6e Iter += getVarin
157c9 74 33 32 28 70 49 74 65 72 2c 20 6e 53 69 7a 65 t32(pIter, nSize
157ca 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
157cb 20 20 20 20 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 nSize = 0;.
157cc 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 70 49 74 }.. /* pIt
157cd 65 72 20 6e 6f 77 20 70 6f 69 6e 74 73 20 61 74 er now points at
157ce 20 74 68 65 20 36 34 2d 62 69 74 20 69 6e 74 65 the 64-bit inte
157cf 67 65 72 20 6b 65 79 20 76 61 6c 75 65 2c 20 61 ger key value, a
157d0 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 variable length
157d1 20 0a 20 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 . ** integer
157d2 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 . The following
157d3 62 6c 6f 63 6b 20 6d 6f 76 65 73 20 70 49 74 65 block moves pIte
157d4 72 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 r to point at th
157d5 65 20 66 69 72 73 74 20 62 79 74 65 0a 20 20 20 e first byte.
157d6 20 2a 2a 20 70 61 73 74 20 74 68 65 20 65 6e 64 ** past the end
157d7 20 6f 66 20 74 68 65 20 6b 65 79 20 76 61 6c 75 of the key valu
157d8 65 2e 20 2a 2f 0a 20 20 20 20 70 45 6e 64 20 3d e. */. pEnd =
157d9 20 26 70 49 74 65 72 5b 39 5d 3b 0a 20 20 20 20 &pIter[9];.
157da 77 68 69 6c 65 28 20 28 2a 70 49 74 65 72 2b 2b while( (*pIter++
157db 29 26 30 78 38 30 20 26 26 20 70 49 74 65 72 3c )&0x80 && pIter<
157dc 70 45 6e 64 20 29 3b 0a 20 20 7d 65 6c 73 65 7b pEnd );. }else{
157dd 0a 20 20 20 20 70 49 74 65 72 20 2b 3d 20 67 65 . pIter += ge
157de 74 56 61 72 69 6e 74 33 32 28 70 49 74 65 72 2c tVarint32(pIter,
157df 20 6e 53 69 7a 65 29 3b 0a 20 20 7d 0a 0a 20 20 nSize);. }..
157e0 74 65 73 74 63 61 73 65 28 20 6e 53 69 7a 65 3d testcase( nSize=
157e1 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c =pPage->maxLocal
157e2 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 );. testcase(
157e3 6e 53 69 7a 65 3d 3d 70 50 61 67 65 2d 3e 6d 61 nSize==pPage->ma
157e4 78 4c 6f 63 61 6c 2b 31 20 29 3b 0a 20 20 69 66 xLocal+1 );. if
157e5 28 20 6e 53 69 7a 65 3e 70 50 61 67 65 2d 3e 6d ( nSize>pPage->m
157e6 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 69 axLocal ){. i
157e7 6e 74 20 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 50 nt minLocal = pP
157e8 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 age->minLocal;.
157e9 20 20 20 6e 53 69 7a 65 20 3d 20 6d 69 6e 4c 6f nSize = minLo
157ea 63 61 6c 20 2b 20 28 6e 53 69 7a 65 20 2d 20 6d cal + (nSize - m
157eb 69 6e 4c 6f 63 61 6c 29 20 25 20 28 70 50 61 67 inLocal) % (pPag
157ec 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 e->pBt->usableSi
157ed 7a 65 20 2d 20 34 29 3b 0a 20 20 20 20 74 65 73 ze - 4);. tes
157ee 74 63 61 73 65 28 20 6e 53 69 7a 65 3d 3d 70 50 tcase( nSize==pP
157ef 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 3b age->maxLocal );
157f0 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6e . testcase( n
157f1 53 69 7a 65 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 Size==pPage->max
157f2 4c 6f 63 61 6c 2b 31 20 29 3b 0a 20 20 20 20 69 Local+1 );. i
157f3 66 28 20 6e 53 69 7a 65 3e 70 50 61 67 65 2d 3e f( nSize>pPage->
157f4 6d 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 maxLocal ){.
157f5 20 20 6e 53 69 7a 65 20 3d 20 6d 69 6e 4c 6f 63 nSize = minLoc
157f6 61 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 53 al;. }. nS
157f7 69 7a 65 20 2b 3d 20 34 3b 0a 20 20 7d 0a 20 20 ize += 4;. }.
157f8 6e 53 69 7a 65 20 2b 3d 20 28 75 33 32 29 28 70 nSize += (u32)(p
157f9 49 74 65 72 20 2d 20 70 43 65 6c 6c 29 3b 0a 0a Iter - pCell);..
157fa 20 20 2f 2a 20 54 68 65 20 6d 69 6e 69 6d 75 6d /* The minimum
157fb 20 73 69 7a 65 20 6f 66 20 61 6e 79 20 63 65 6c size of any cel
157fc 6c 20 69 73 20 34 20 62 79 74 65 73 2e 20 2a 2f l is 4 bytes. */
157fd 0a 20 20 69 66 28 20 6e 53 69 7a 65 3c 34 20 29 . if( nSize<4 )
157fe 7b 0a 20 20 20 20 6e 53 69 7a 65 20 3d 20 34 3b {. nSize = 4;
157ff 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 . }.. assert(
15800 6e 53 69 7a 65 3d 3d 64 65 62 75 67 69 6e 66 6f nSize==debuginfo
15801 2e 6e 53 69 7a 65 20 29 3b 0a 20 20 72 65 74 75 .nSize );. retu
15802 72 6e 20 28 75 31 36 29 6e 53 69 7a 65 3b 0a 7d rn (u16)nSize;.}
15803 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
15804 44 45 42 55 47 0a 2f 2a 20 54 68 69 73 20 76 61 DEBUG./* This va
15805 72 69 61 74 69 6f 6e 20 6f 6e 20 63 65 6c 6c 53 riation on cellS
15806 69 7a 65 50 74 72 28 29 20 69 73 20 75 73 65 64 izePtr() is used
15807 20 69 6e 73 69 64 65 20 6f 66 20 61 73 73 65 72 inside of asser
15808 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a t() statements.*
15809 2a 20 6f 6e 6c 79 2e 20 2a 2f 0a 73 74 61 74 69 * only. */.stati
1580a 63 20 75 31 36 20 63 65 6c 6c 53 69 7a 65 28 4d c u16 cellSize(M
1580b 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69 emPage *pPage, i
1580c 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 20 72 65 74 nt iCell){. ret
1580d 75 72 6e 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 urn cellSizePtr(
1580e 70 50 61 67 65 2c 20 66 69 6e 64 43 65 6c 6c 28 pPage, findCell(
1580f 70 50 61 67 65 2c 20 69 43 65 6c 6c 29 29 3b 0a pPage, iCell));.
15810 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 }.#endif..#ifnde
15811 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
15812 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 49 TOVACUUM./*.** I
15813 66 20 74 68 65 20 63 65 6c 6c 20 70 43 65 6c 6c f the cell pCell
15814 2c 20 70 61 72 74 20 6f 66 20 70 61 67 65 20 70 , part of page p
15815 50 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 Page contains a
15816 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 6e pointer.** to an
15817 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2c 20 overflow page,
15818 69 6e 73 65 72 74 20 61 6e 20 65 6e 74 72 79 20 insert an entry
15819 69 6e 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 into the pointer
1581a 2d 6d 61 70 0a 2a 2a 20 66 6f 72 20 74 68 65 20 -map.** for the
1581b 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 0a 2a overflow page..*
1581c 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 74 /.static void pt
1581d 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28 4d rmapPutOvflPtr(M
1581e 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 75 emPage *pPage, u
1581f 38 20 2a 70 43 65 6c 6c 2c 20 69 6e 74 20 2a 70 8 *pCell, int *p
15820 52 43 29 7b 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 RC){. CellInfo
15821 69 6e 66 6f 3b 0a 20 20 69 66 28 20 2a 70 52 43 info;. if( *pRC
15822 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 ) return;. ass
15823 65 72 74 28 20 70 43 65 6c 6c 21 3d 30 20 29 3b ert( pCell!=0 );
15824 0a 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c . btreeParseCel
15825 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c lPtr(pPage, pCel
15826 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 61 73 73 l, &info);. ass
15827 65 72 74 28 20 28 69 6e 66 6f 2e 6e 44 61 74 61 ert( (info.nData
15828 2b 28 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 3f +(pPage->intKey?
15829 30 3a 69 6e 66 6f 2e 6e 4b 65 79 29 29 3d 3d 69 0:info.nKey))==i
1582a 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 29 3b 0a nfo.nPayload );.
1582b 20 20 69 66 28 20 69 6e 66 6f 2e 69 4f 76 65 72 if( info.iOver
1582c 66 6c 6f 77 20 29 7b 0a 20 20 20 20 50 67 6e 6f flow ){. Pgno
1582d 20 6f 76 66 6c 20 3d 20 67 65 74 34 62 79 74 65 ovfl = get4byte
1582e 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 (&pCell[info.iOv
1582f 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 20 20 70 74 erflow]);. pt
15830 72 6d 61 70 50 75 74 28 70 50 61 67 65 2d 3e 70 rmapPut(pPage->p
15831 42 74 2c 20 6f 76 66 6c 2c 20 50 54 52 4d 41 50 Bt, ovfl, PTRMAP
15832 5f 4f 56 45 52 46 4c 4f 57 31 2c 20 70 50 61 67 _OVERFLOW1, pPag
15833 65 2d 3e 70 67 6e 6f 2c 20 70 52 43 29 3b 0a 20 e->pgno, pRC);.
15834 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a }.}.#endif.../*
15835 0a 2a 2a 20 44 65 66 72 61 67 6d 65 6e 74 20 74 .** Defragment t
15836 68 65 20 70 61 67 65 20 67 69 76 65 6e 2e 20 20 he page given.
15837 41 6c 6c 20 43 65 6c 6c 73 20 61 72 65 20 6d 6f All Cells are mo
15838 76 65 64 20 74 6f 20 74 68 65 0a 2a 2a 20 65 6e ved to the.** en
15839 64 20 6f 66 20 74 68 65 20 70 61 67 65 20 61 6e d of the page an
1583a 64 20 61 6c 6c 20 66 72 65 65 20 73 70 61 63 65 d all free space
1583b 20 69 73 20 63 6f 6c 6c 65 63 74 65 64 20 69 6e is collected in
1583c 74 6f 20 6f 6e 65 0a 2a 2a 20 62 69 67 20 46 72 to one.** big Fr
1583d 65 65 42 6c 6b 20 74 68 61 74 20 6f 63 63 75 72 eeBlk that occur
1583e 73 20 69 6e 20 62 65 74 77 65 65 6e 20 74 68 65 s in between the
1583f 20 68 65 61 64 65 72 20 61 6e 64 20 63 65 6c 6c header and cell
15840 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 61 72 72 61 .** pointer arra
15841 79 20 61 6e 64 20 74 68 65 20 63 65 6c 6c 20 63 y and the cell c
15842 6f 6e 74 65 6e 74 20 61 72 65 61 2e 0a 2a 2f 0a ontent area..*/.
15843 73 74 61 74 69 63 20 69 6e 74 20 64 65 66 72 61 static int defra
15844 67 6d 65 6e 74 50 61 67 65 28 4d 65 6d 50 61 67 gmentPage(MemPag
15845 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 69 6e 74 e *pPage){. int
15846 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
15847 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 /* Loop
15848 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 counter */. int
15849 20 70 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 pc;
1584a 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 /* Addre
1584b 73 73 20 6f 66 20 61 20 69 2d 74 68 20 63 65 6c ss of a i-th cel
1584c 6c 20 2a 2f 0a 20 20 69 6e 74 20 68 64 72 3b 20 l */. int hdr;
1584d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1584e 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 74 /* Offset to t
1584f 68 65 20 70 61 67 65 20 68 65 61 64 65 72 20 2a he page header *
15850 2f 0a 20 20 69 6e 74 20 73 69 7a 65 3b 20 20 20 /. int size;
15851 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
15852 2a 20 53 69 7a 65 20 6f 66 20 61 20 63 65 6c 6c * Size of a cell
15853 20 2a 2f 0a 20 20 69 6e 74 20 75 73 61 62 6c 65 */. int usable
15854 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 Size;
15855 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 /* Number of us
15856 61 62 6c 65 20 62 79 74 65 73 20 6f 6e 20 61 20 able bytes on a
15857 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 63 65 page */. int ce
15858 6c 6c 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 llOffset;
15859 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 /* Offset t
1585a 6f 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 o the cell point
1585b 65 72 20 61 72 72 61 79 20 2a 2f 0a 20 20 69 6e er array */. in
1585c 74 20 63 62 72 6b 3b 20 20 20 20 20 20 20 20 20 t cbrk;
1585d 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 /* Offs
1585e 65 74 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 et to the cell c
1585f 6f 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f 0a 20 ontent area */.
15860 20 69 6e 74 20 6e 43 65 6c 6c 3b 20 20 20 20 20 int nCell;
15861 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
15862 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f umber of cells o
15863 6e 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 n the page */.
15864 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64 unsigned char *d
15865 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 ata; /* Th
15866 65 20 70 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 e page data */.
15867 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
15868 74 65 6d 70 3b 20 20 20 20 20 20 20 2f 2a 20 54 temp; /* T
15869 65 6d 70 20 61 72 65 61 20 66 6f 72 20 63 65 6c emp area for cel
1586a 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 69 l content */. i
1586b 6e 74 20 69 43 65 6c 6c 46 69 72 73 74 3b 20 20 nt iCellFirst;
1586c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 /* Fir
1586d 73 74 20 61 6c 6c 6f 77 61 62 6c 65 20 63 65 6c st allowable cel
1586e 6c 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 l index */. int
1586f 20 69 43 65 6c 6c 4c 61 73 74 3b 20 20 20 20 20 iCellLast;
15870 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 /* Last
15871 70 6f 73 73 69 62 6c 65 20 63 65 6c 6c 20 69 6e possible cell in
15872 64 65 78 20 2a 2f 0a 0a 0a 20 20 61 73 73 65 72 dex */... asser
15873 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 t( sqlite3PagerI
15874 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 swriteable(pPage
15875 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 ->pDbPage) );.
15876 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 assert( pPage->p
15877 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 Bt!=0 );. asser
15878 74 28 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 t( pPage->pBt->u
15879 73 61 62 6c 65 53 69 7a 65 20 3c 3d 20 53 51 4c sableSize <= SQL
1587a 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a ITE_MAX_PAGE_SIZ
1587b 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 E );. assert( p
1587c 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d Page->nOverflow=
1587d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
1587e 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
1587f 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d ld(pPage->pBt->m
15880 75 74 65 78 29 20 29 3b 0a 20 20 74 65 6d 70 20 utex) );. temp
15881 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 65 = sqlite3PagerTe
15882 6d 70 53 70 61 63 65 28 70 50 61 67 65 2d 3e 70 mpSpace(pPage->p
15883 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 64 Bt->pPager);. d
15884 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 ata = pPage->aDa
15885 74 61 3b 0a 20 20 68 64 72 20 3d 20 70 50 61 67 ta;. hdr = pPag
15886 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 e->hdrOffset;.
15887 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 70 50 61 cellOffset = pPa
15888 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 3b 0a ge->cellOffset;.
15889 20 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d nCell = pPage-
1588a 3e 6e 43 65 6c 6c 3b 0a 20 20 61 73 73 65 72 74 >nCell;. assert
1588b 28 20 6e 43 65 6c 6c 3d 3d 67 65 74 32 62 79 74 ( nCell==get2byt
1588c 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 29 20 e(&data[hdr+3])
1588d 29 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a 65 20 );. usableSize
1588e 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 = pPage->pBt->us
1588f 61 62 6c 65 53 69 7a 65 3b 0a 20 20 63 62 72 6b ableSize;. cbrk
15890 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 = get2byte(&dat
15891 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 6d 65 6d a[hdr+5]);. mem
15892 63 70 79 28 26 74 65 6d 70 5b 63 62 72 6b 5d 2c cpy(&temp[cbrk],
15893 20 26 64 61 74 61 5b 63 62 72 6b 5d 2c 20 75 73 &data[cbrk], us
15894 61 62 6c 65 53 69 7a 65 20 2d 20 63 62 72 6b 29 ableSize - cbrk)
15895 3b 0a 20 20 63 62 72 6b 20 3d 20 75 73 61 62 6c ;. cbrk = usabl
15896 65 53 69 7a 65 3b 0a 20 20 69 43 65 6c 6c 46 69 eSize;. iCellFi
15897 72 73 74 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 rst = cellOffset
15898 20 2b 20 32 2a 6e 43 65 6c 6c 3b 0a 20 20 69 43 + 2*nCell;. iC
15899 65 6c 6c 4c 61 73 74 20 3d 20 75 73 61 62 6c 65 ellLast = usable
1589a 53 69 7a 65 20 2d 20 34 3b 0a 20 20 66 6f 72 28 Size - 4;. for(
1589b 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b i=0; i<nCell; i+
1589c 2b 29 7b 0a 20 20 20 20 75 38 20 2a 70 41 64 64 +){. u8 *pAdd
1589d 72 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 69 2d r; /* The i-
1589e 74 68 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 th cell pointer
1589f 2a 2f 0a 20 20 20 20 70 41 64 64 72 20 3d 20 26 */. pAddr = &
158a0 64 61 74 61 5b 63 65 6c 6c 4f 66 66 73 65 74 20 data[cellOffset
158a1 2b 20 69 2a 32 5d 3b 0a 20 20 20 20 70 63 20 3d + i*2];. pc =
158a2 20 67 65 74 32 62 79 74 65 28 70 41 64 64 72 29 get2byte(pAddr)
158a3 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 ;. testcase(
158a4 70 63 3d 3d 69 43 65 6c 6c 46 69 72 73 74 20 29 pc==iCellFirst )
158a5 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 ;. testcase(
158a6 70 63 3d 3d 69 43 65 6c 6c 4c 61 73 74 20 29 3b pc==iCellLast );
158a7 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
158a8 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4f 56 45 52 LITE_ENABLE_OVER
158a9 53 49 5a 45 5f 43 45 4c 4c 5f 43 48 45 43 4b 29 SIZE_CELL_CHECK)
158aa 0a 20 20 20 20 2f 2a 20 54 68 65 73 65 20 63 6f . /* These co
158ab 6e 64 69 74 69 6f 6e 73 20 68 61 76 65 20 61 6c nditions have al
158ac 72 65 61 64 79 20 62 65 65 6e 20 76 65 72 69 66 ready been verif
158ad 69 65 64 20 69 6e 20 62 74 72 65 65 49 6e 69 74 ied in btreeInit
158ae 50 61 67 65 28 29 0a 20 20 20 20 2a 2a 20 69 66 Page(). ** if
158af 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4f SQLITE_ENABLE_O
158b0 56 45 52 53 49 5a 45 5f 43 45 4c 4c 5f 43 48 45 VERSIZE_CELL_CHE
158b1 43 4b 20 69 73 20 64 65 66 69 6e 65 64 20 0a 20 CK is defined .
158b2 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 63 */. if( pc
158b3 3c 69 43 65 6c 6c 46 69 72 73 74 20 7c 7c 20 70 <iCellFirst || p
158b4 63 3e 69 43 65 6c 6c 4c 61 73 74 20 29 7b 0a 20 c>iCellLast ){.
158b5 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
158b6 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
158b7 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 . }.#endif.
158b8 20 20 61 73 73 65 72 74 28 20 70 63 3e 3d 69 43 assert( pc>=iC
158b9 65 6c 6c 46 69 72 73 74 20 26 26 20 70 63 3c 3d ellFirst && pc<=
158ba 69 43 65 6c 6c 4c 61 73 74 20 29 3b 0a 20 20 20 iCellLast );.
158bb 20 73 69 7a 65 20 3d 20 63 65 6c 6c 53 69 7a 65 size = cellSize
158bc 50 74 72 28 70 50 61 67 65 2c 20 26 74 65 6d 70 Ptr(pPage, &temp
158bd 5b 70 63 5d 29 3b 0a 20 20 20 20 63 62 72 6b 20 [pc]);. cbrk
158be 2d 3d 20 73 69 7a 65 3b 0a 23 69 66 20 64 65 66 -= size;.#if def
158bf 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 ined(SQLITE_ENAB
158c0 4c 45 5f 4f 56 45 52 53 49 5a 45 5f 43 45 4c 4c LE_OVERSIZE_CELL
158c1 5f 43 48 45 43 4b 29 0a 20 20 20 20 69 66 28 20 _CHECK). if(
158c2 63 62 72 6b 3c 69 43 65 6c 6c 46 69 72 73 74 20 cbrk<iCellFirst
158c3 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
158c4 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
158c5 4b 50 54 3b 0a 20 20 20 20 7d 0a 23 65 6c 73 65 KPT;. }.#else
158c6 0a 20 20 20 20 69 66 28 20 63 62 72 6b 3c 69 43 . if( cbrk<iC
158c7 65 6c 6c 46 69 72 73 74 20 7c 7c 20 70 63 2b 73 ellFirst || pc+s
158c8 69 7a 65 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 ize>usableSize )
158c9 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
158ca 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
158cb 50 54 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 PT;. }.#endif
158cc 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 62 72 . assert( cbr
158cd 6b 2b 73 69 7a 65 3c 3d 75 73 61 62 6c 65 53 69 k+size<=usableSi
158ce 7a 65 20 26 26 20 63 62 72 6b 3e 3d 69 43 65 6c ze && cbrk>=iCel
158cf 6c 46 69 72 73 74 20 29 3b 0a 20 20 20 20 74 65 lFirst );. te
158d0 73 74 63 61 73 65 28 20 63 62 72 6b 2b 73 69 7a stcase( cbrk+siz
158d1 65 3d 3d 75 73 61 62 6c 65 53 69 7a 65 20 29 3b e==usableSize );
158d2 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 . testcase( p
158d3 63 2b 73 69 7a 65 3d 3d 75 73 61 62 6c 65 53 69 c+size==usableSi
158d4 7a 65 20 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 ze );. memcpy
158d5 28 26 64 61 74 61 5b 63 62 72 6b 5d 2c 20 26 74 (&data[cbrk], &t
158d6 65 6d 70 5b 70 63 5d 2c 20 73 69 7a 65 29 3b 0a emp[pc], size);.
158d7 20 20 20 20 70 75 74 32 62 79 74 65 28 70 41 64 put2byte(pAd
158d8 64 72 2c 20 63 62 72 6b 29 3b 0a 20 20 7d 0a 20 dr, cbrk);. }.
158d9 20 61 73 73 65 72 74 28 20 63 62 72 6b 3e 3d 69 assert( cbrk>=i
158da 43 65 6c 6c 46 69 72 73 74 20 29 3b 0a 20 20 70 CellFirst );. p
158db 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 ut2byte(&data[hd
158dc 72 2b 35 5d 2c 20 63 62 72 6b 29 3b 0a 20 20 64 r+5], cbrk);. d
158dd 61 74 61 5b 68 64 72 2b 31 5d 20 3d 20 30 3b 0a ata[hdr+1] = 0;.
158de 20 20 64 61 74 61 5b 68 64 72 2b 32 5d 20 3d 20 data[hdr+2] =
158df 30 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 37 5d 0;. data[hdr+7]
158e0 20 3d 20 30 3b 0a 20 20 6d 65 6d 73 65 74 28 26 = 0;. memset(&
158e1 64 61 74 61 5b 69 43 65 6c 6c 46 69 72 73 74 5d data[iCellFirst]
158e2 2c 20 30 2c 20 63 62 72 6b 2d 69 43 65 6c 6c 46 , 0, cbrk-iCellF
158e3 69 72 73 74 29 3b 0a 20 20 61 73 73 65 72 74 28 irst);. assert(
158e4 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 sqlite3PagerIsw
158e5 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e riteable(pPage->
158e6 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 69 66 pDbPage) );. if
158e7 28 20 63 62 72 6b 2d 69 43 65 6c 6c 46 69 72 73 ( cbrk-iCellFirs
158e8 74 21 3d 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 t!=pPage->nFree
158e9 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
158ea 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
158eb 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 T;. }. return
158ec 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
158ed 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 .** Allocate nBy
158ee 74 65 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 te bytes of spac
158ef 65 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 e from within th
158f0 65 20 42 2d 54 72 65 65 20 70 61 67 65 20 70 61 e B-Tree page pa
158f1 73 73 65 64 0a 2a 2a 20 61 73 20 74 68 65 20 66 ssed.** as the f
158f2 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 57 irst argument. W
158f3 72 69 74 65 20 69 6e 74 6f 20 2a 70 49 64 78 20 rite into *pIdx
158f4 74 68 65 20 69 6e 64 65 78 20 69 6e 74 6f 20 70 the index into p
158f5 50 61 67 65 2d 3e 61 44 61 74 61 5b 5d 0a 2a 2a Page->aData[].**
158f6 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 62 79 of the first by
158f7 74 65 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 te of allocated
158f8 73 70 61 63 65 2e 20 52 65 74 75 72 6e 20 65 69 space. Return ei
158f9 74 68 65 72 20 53 51 4c 49 54 45 5f 4f 4b 20 6f ther SQLITE_OK o
158fa 72 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 63 6f r.** an error co
158fb 64 65 20 28 75 73 75 61 6c 6c 79 20 53 51 4c 49 de (usually SQLI
158fc 54 45 5f 43 4f 52 52 55 50 54 29 2e 0a 2a 2a 0a TE_CORRUPT)..**.
158fd 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 67 75 ** The caller gu
158fe 61 72 61 6e 74 65 65 73 20 74 68 61 74 20 74 68 arantees that th
158ff 65 72 65 20 69 73 20 73 75 66 66 69 63 69 65 6e ere is sufficien
15900 74 20 73 70 61 63 65 20 74 6f 20 6d 61 6b 65 20 t space to make
15901 74 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f the.** allocatio
15902 6e 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 n. This routine
15903 20 6d 69 67 68 74 20 6e 65 65 64 20 74 6f 20 64 might need to d
15904 65 66 72 61 67 6d 65 6e 74 20 69 6e 20 6f 72 64 efragment in ord
15905 65 72 20 74 6f 20 62 72 69 6e 67 0a 2a 2a 20 61 er to bring.** a
15906 6c 6c 20 74 68 65 20 73 70 61 63 65 20 74 6f 67 ll the space tog
15907 65 74 68 65 72 2c 20 68 6f 77 65 76 65 72 2e 20 ether, however.
15908 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 This routine wi
15909 6c 6c 20 61 76 6f 69 64 20 75 73 69 6e 67 0a 2a ll avoid using.*
1590a 2a 20 74 68 65 20 66 69 72 73 74 20 74 77 6f 20 * the first two
1590b 62 79 74 65 73 20 70 61 73 74 20 74 68 65 20 63 bytes past the c
1590c 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 65 61 ell pointer area
1590d 20 73 69 6e 63 65 20 70 72 65 73 75 6d 61 62 6c since presumabl
1590e 79 20 74 68 69 73 0a 2a 2a 20 61 6c 6c 6f 63 61 y this.** alloca
1590f 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 6d 61 tion is being ma
15910 64 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 69 de in order to i
15911 6e 73 65 72 74 20 61 20 6e 65 77 20 63 65 6c 6c nsert a new cell
15912 2c 20 73 6f 20 77 65 20 77 69 6c 6c 0a 2a 2a 20 , so we will.**
15913 61 6c 73 6f 20 65 6e 64 20 75 70 20 6e 65 65 64 also end up need
15914 69 6e 67 20 61 20 6e 65 77 20 63 65 6c 6c 20 70 ing a new cell p
15915 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 ointer..*/.stati
15916 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65 53 70 c int allocateSp
15917 61 63 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 ace(MemPage *pPa
15918 67 65 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 69 ge, int nByte, i
15919 6e 74 20 2a 70 49 64 78 29 7b 0a 20 20 63 6f 6e nt *pIdx){. con
1591a 73 74 20 69 6e 74 20 68 64 72 20 3d 20 70 50 61 st int hdr = pPa
1591b 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 20 20 ge->hdrOffset;
1591c 20 20 2f 2a 20 4c 6f 63 61 6c 20 63 61 63 68 65 /* Local cache
1591d 20 6f 66 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 of pPage->hdrOf
1591e 66 73 65 74 20 2a 2f 0a 20 20 75 38 20 2a 20 63 fset */. u8 * c
1591f 6f 6e 73 74 20 64 61 74 61 20 3d 20 70 50 61 67 onst data = pPag
15920 65 2d 3e 61 44 61 74 61 3b 20 20 20 20 20 20 2f e->aData; /
15921 2a 20 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f 66 * Local cache of
15922 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 2a 2f pPage->aData */
15923 0a 20 20 69 6e 74 20 6e 46 72 61 67 3b 20 20 20 . int nFrag;
15924 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15925 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
15926 72 20 6f 66 20 66 72 61 67 6d 65 6e 74 65 64 20 r of fragmented
15927 62 79 74 65 73 20 6f 6e 20 70 50 61 67 65 20 2a bytes on pPage *
15928 2f 0a 20 20 69 6e 74 20 74 6f 70 3b 20 20 20 20 /. int top;
15929 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1592a 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 /* Firs
1592b 74 20 62 79 74 65 20 6f 66 20 63 65 6c 6c 20 63 t byte of cell c
1592c 6f 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f 0a 20 ontent area */.
1592d 20 69 6e 74 20 67 61 70 3b 20 20 20 20 20 20 20 int gap;
1592e 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f /* First byte o
1592f 66 20 67 61 70 20 62 65 74 77 65 65 6e 20 63 65 f gap between ce
15930 6c 6c 20 70 6f 69 6e 74 65 72 73 20 61 6e 64 20 ll pointers and
15931 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a cell content */.
15932 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 int rc;
15933 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 72 65 74 /* Integer ret
15934 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 0a 20 urn code */. .
15935 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
15936 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 PagerIswriteable
15937 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
15938 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
15939 61 67 65 2d 3e 70 42 74 20 29 3b 0a 20 20 61 73 age->pBt );. as
1593a 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1593b 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e tex_held(pPage->
1593c 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
1593d 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 3e 3d assert( nByte>=
1593e 30 20 29 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 0 ); /* Minimum
1593f 20 63 65 6c 6c 20 73 69 7a 65 20 69 73 20 34 20 cell size is 4
15940 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 */. assert( pPa
15941 67 65 2d 3e 6e 46 72 65 65 3e 3d 6e 42 79 74 65 ge->nFree>=nByte
15942 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
15943 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d age->nOverflow==
15944 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 0 );. assert( n
15945 42 79 74 65 3c 70 50 61 67 65 2d 3e 70 42 74 2d Byte<pPage->pBt-
15946 3e 75 73 61 62 6c 65 53 69 7a 65 2d 38 20 29 3b >usableSize-8 );
15947 0a 0a 20 20 6e 46 72 61 67 20 3d 20 64 61 74 61 .. nFrag = data
15948 5b 68 64 72 2b 37 5d 3b 0a 20 20 61 73 73 65 72 [hdr+7];. asser
15949 74 28 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 t( pPage->cellOf
1594a 66 73 65 74 20 3d 3d 20 68 64 72 20 2b 20 31 32 fset == hdr + 12
1594b 20 2d 20 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 - 4*pPage->leaf
1594c 20 29 3b 0a 20 20 67 61 70 20 3d 20 70 50 61 67 );. gap = pPag
1594d 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 e->cellOffset +
1594e 32 2a 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 2*pPage->nCell;.
1594f 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79 74 65 top = get2byte
15950 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a (&data[hdr+5]);.
15951 20 20 69 66 28 20 67 61 70 3e 74 6f 70 20 29 20 if( gap>top )
15952 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
15953 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 74 65 RRUPT_BKPT;. te
15954 73 74 63 61 73 65 28 20 67 61 70 2b 32 3d 3d 74 stcase( gap+2==t
15955 6f 70 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 op );. testcase
15956 28 20 67 61 70 2b 31 3d 3d 74 6f 70 20 29 3b 0a ( gap+1==top );.
15957 20 20 74 65 73 74 63 61 73 65 28 20 67 61 70 3d testcase( gap=
15958 3d 74 6f 70 20 29 3b 0a 0a 20 20 69 66 28 20 6e =top );.. if( n
15959 46 72 61 67 3e 3d 36 30 20 29 7b 0a 20 20 20 20 Frag>=60 ){.
1595a 2f 2a 20 41 6c 77 61 79 73 20 64 65 66 72 61 67 /* Always defrag
1595b 6d 65 6e 74 20 68 69 67 68 6c 79 20 66 72 61 67 ment highly frag
1595c 6d 65 6e 74 65 64 20 70 61 67 65 73 20 2a 2f 0a mented pages */.
1595d 20 20 20 20 72 63 20 3d 20 64 65 66 72 61 67 6d rc = defragm
1595e 65 6e 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a entPage(pPage);.
1595f 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 if( rc ) ret
15960 75 72 6e 20 72 63 3b 0a 20 20 20 20 74 6f 70 20 urn rc;. top
15961 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 = get2byte(&data
15962 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 7d 65 6c 73 [hdr+5]);. }els
15963 65 20 69 66 28 20 67 61 70 2b 32 3c 3d 74 6f 70 e if( gap+2<=top
15964 20 29 7b 0a 20 20 20 20 2f 2a 20 53 65 61 72 63 ){. /* Searc
15965 68 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 6c h the freelist l
15966 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 66 72 65 ooking for a fre
15967 65 20 73 6c 6f 74 20 62 69 67 20 65 6e 6f 75 67 e slot big enoug
15968 68 20 74 6f 20 73 61 74 69 73 66 79 20 0a 20 20 h to satisfy .
15969 20 20 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 ** the request
1596a 2e 20 54 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e . The allocation
1596b 20 69 73 20 6d 61 64 65 20 66 72 6f 6d 20 74 68 is made from th
1596c 65 20 66 69 72 73 74 20 66 72 65 65 20 73 6c 6f e first free slo
1596d 74 20 69 6e 20 0a 20 20 20 20 2a 2a 20 74 68 65 t in . ** the
1596e 20 6c 69 73 74 20 74 68 61 74 20 69 73 20 6c 61 list that is la
1596f 72 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 61 63 rge enough to ac
15970 63 6f 6d 61 64 61 74 65 20 69 74 2e 0a 20 20 20 comadate it..
15971 20 2a 2f 0a 20 20 20 20 69 6e 74 20 70 63 2c 20 */. int pc,
15972 61 64 64 72 3b 0a 20 20 20 20 66 6f 72 28 61 64 addr;. for(ad
15973 64 72 3d 68 64 72 2b 31 3b 20 28 70 63 20 3d 20 dr=hdr+1; (pc =
15974 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 61 get2byte(&data[a
15975 64 64 72 5d 29 29 3e 30 3b 20 61 64 64 72 3d 70 ddr]))>0; addr=p
15976 63 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 73 69 c){. int si
15977 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 ze = get2byte(&d
15978 61 74 61 5b 70 63 2b 32 5d 29 3b 20 20 20 20 20 ata[pc+2]);
15979 2f 2a 20 53 69 7a 65 20 6f 66 20 66 72 65 65 20 /* Size of free
1597a 73 6c 6f 74 20 2a 2f 0a 20 20 20 20 20 20 69 66 slot */. if
1597b 28 20 73 69 7a 65 3e 3d 6e 42 79 74 65 20 29 7b ( size>=nByte ){
1597c 0a 20 20 20 20 20 20 20 20 69 6e 74 20 78 20 3d . int x =
1597d 20 73 69 7a 65 20 2d 20 6e 42 79 74 65 3b 0a 20 size - nByte;.
1597e 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 testcase(
1597f 20 78 3d 3d 34 20 29 3b 0a 20 20 20 20 20 20 20 x==4 );.
15980 20 74 65 73 74 63 61 73 65 28 20 78 3d 3d 33 20 testcase( x==3
15981 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 );. if( x
15982 3c 34 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 <4 ){.
15983 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 73 6c /* Remove the sl
15984 6f 74 20 66 72 6f 6d 20 74 68 65 20 66 72 65 65 ot from the free
15985 2d 6c 69 73 74 2e 20 55 70 64 61 74 65 20 74 68 -list. Update th
15986 65 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20 20 20 e number of.
15987 20 20 20 20 20 20 2a 2a 20 66 72 61 67 6d 65 6e ** fragmen
15988 74 65 64 20 62 79 74 65 73 20 77 69 74 68 69 6e ted bytes within
15989 20 74 68 65 20 70 61 67 65 2e 20 2a 2f 0a 20 20 the page. */.
1598a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 memcpy(&
1598b 64 61 74 61 5b 61 64 64 72 5d 2c 20 26 64 61 74 data[addr], &dat
1598c 61 5b 70 63 5d 2c 20 32 29 3b 0a 20 20 20 20 20 a[pc], 2);.
1598d 20 20 20 20 20 64 61 74 61 5b 68 64 72 2b 37 5d data[hdr+7]
1598e 20 3d 20 28 75 38 29 28 6e 46 72 61 67 20 2b 20 = (u8)(nFrag +
1598f 78 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 x);. }els
15990 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 e{. /*
15991 54 68 65 20 73 6c 6f 74 20 72 65 6d 61 69 6e 73 The slot remains
15992 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 on the free-lis
15993 74 2e 20 52 65 64 75 63 65 20 69 74 73 20 73 69 t. Reduce its si
15994 7a 65 20 74 6f 20 61 63 63 6f 75 6e 74 0a 20 20 ze to account.
15995 20 20 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 74 ** for t
15996 68 65 20 70 6f 72 74 69 6f 6e 20 75 73 65 64 20 he portion used
15997 62 79 20 74 68 65 20 6e 65 77 20 61 6c 6c 6f 63 by the new alloc
15998 61 74 69 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 ation. */.
15999 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64 61 put2byte(&da
1599a 74 61 5b 70 63 2b 32 5d 2c 20 78 29 3b 0a 20 20 ta[pc+2], x);.
1599b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1599c 2a 70 49 64 78 20 3d 20 70 63 20 2b 20 78 3b 0a *pIdx = pc + x;.
1599d 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 return S
1599e 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 QLITE_OK;.
1599f 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f }. }. }.. /
159a0 2a 20 43 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20 * Check to make
159a1 73 75 72 65 20 74 68 65 72 65 20 69 73 20 65 6e sure there is en
159a2 6f 75 67 68 20 73 70 61 63 65 20 69 6e 20 74 68 ough space in th
159a3 65 20 67 61 70 20 74 6f 20 73 61 74 69 73 66 79 e gap to satisfy
159a4 0a 20 20 2a 2a 20 74 68 65 20 61 6c 6c 6f 63 61 . ** the alloca
159a5 74 69 6f 6e 2e 20 20 49 66 20 6e 6f 74 2c 20 64 tion. If not, d
159a6 65 66 72 61 67 6d 65 6e 74 2e 0a 20 20 2a 2f 0a efragment.. */.
159a7 20 20 74 65 73 74 63 61 73 65 28 20 67 61 70 2b testcase( gap+
159a8 32 2b 6e 42 79 74 65 3d 3d 74 6f 70 20 29 3b 0a 2+nByte==top );.
159a9 20 20 69 66 28 20 67 61 70 2b 32 2b 6e 42 79 74 if( gap+2+nByt
159aa 65 3e 74 6f 70 20 29 7b 0a 20 20 20 20 72 63 20 e>top ){. rc
159ab 3d 20 64 65 66 72 61 67 6d 65 6e 74 50 61 67 65 = defragmentPage
159ac 28 70 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 (pPage);. if(
159ad 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
159ae 0a 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32 62 . top = get2b
159af 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d yte(&data[hdr+5]
159b0 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 67 );. assert( g
159b1 61 70 2b 6e 42 79 74 65 3c 3d 74 6f 70 20 29 3b ap+nByte<=top );
159b2 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 41 6c 6c 6f . }... /* Allo
159b3 63 61 74 65 20 6d 65 6d 6f 72 79 20 66 72 6f 6d cate memory from
159b4 20 74 68 65 20 67 61 70 20 69 6e 20 62 65 74 77 the gap in betw
159b5 65 65 6e 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 een the cell poi
159b6 6e 74 65 72 20 61 72 72 61 79 0a 20 20 2a 2a 20 nter array. **
159b7 61 6e 64 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e and the cell con
159b8 74 65 6e 74 20 61 72 65 61 2e 20 20 54 68 65 20 tent area. The
159b9 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 btreeInitPage()
159ba 63 61 6c 6c 20 68 61 73 20 61 6c 72 65 61 64 79 call has already
159bb 0a 20 20 2a 2a 20 76 61 6c 69 64 61 74 65 64 20 . ** validated
159bc 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 47 the freelist. G
159bd 69 76 65 6e 20 74 68 61 74 20 74 68 65 20 66 72 iven that the fr
159be 65 65 6c 69 73 74 20 69 73 20 76 61 6c 69 64 2c eelist is valid,
159bf 20 74 68 65 72 65 0a 20 20 2a 2a 20 69 73 20 6e there. ** is n
159c0 6f 20 77 61 79 20 74 68 61 74 20 74 68 65 20 61 o way that the a
159c1 6c 6c 6f 63 61 74 69 6f 6e 20 63 61 6e 20 65 78 llocation can ex
159c2 74 65 6e 64 20 6f 66 66 20 74 68 65 20 65 6e 64 tend off the end
159c3 20 6f 66 20 74 68 65 20 70 61 67 65 2e 0a 20 20 of the page..
159c4 2a 2a 20 54 68 65 20 61 73 73 65 72 74 28 29 20 ** The assert()
159c5 62 65 6c 6f 77 20 76 65 72 69 66 69 65 73 20 74 below verifies t
159c6 68 65 20 70 72 65 76 69 6f 75 73 20 73 65 6e 74 he previous sent
159c7 65 6e 63 65 2e 0a 20 20 2a 2f 0a 20 20 74 6f 70 ence.. */. top
159c8 20 2d 3d 20 6e 42 79 74 65 3b 0a 20 20 70 75 74 -= nByte;. put
159c9 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 2byte(&data[hdr+
159ca 35 5d 2c 20 74 6f 70 29 3b 0a 20 20 61 73 73 65 5], top);. asse
159cb 72 74 28 20 74 6f 70 2b 6e 42 79 74 65 20 3c 3d rt( top+nByte <=
159cc 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 pPage->pBt->usa
159cd 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 2a 70 49 bleSize );. *pI
159ce 64 78 20 3d 20 74 6f 70 3b 0a 20 20 72 65 74 75 dx = top;. retu
159cf 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
159d0 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 ./*.** Return a
159d1 73 65 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 section of the p
159d2 50 61 67 65 2d 3e 61 44 61 74 61 20 74 6f 20 74 Page->aData to t
159d3 68 65 20 66 72 65 65 6c 69 73 74 2e 0a 2a 2a 20 he freelist..**
159d4 54 68 65 20 66 69 72 73 74 20 62 79 74 65 20 6f The first byte o
159d5 66 20 74 68 65 20 6e 65 77 20 66 72 65 65 20 62 f the new free b
159d6 6c 6f 63 6b 20 69 73 20 70 50 61 67 65 2d 3e 61 lock is pPage->a
159d7 44 69 73 6b 5b 73 74 61 72 74 5d 0a 2a 2a 20 61 Disk[start].** a
159d8 6e 64 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 nd the size of t
159d9 68 65 20 62 6c 6f 63 6b 20 69 73 20 22 73 69 7a he block is "siz
159da 65 22 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 e" bytes..**.**
159db 4d 6f 73 74 20 6f 66 20 74 68 65 20 65 66 66 6f Most of the effo
159dc 72 74 20 68 65 72 65 20 69 73 20 69 6e 76 6f 6c rt here is invol
159dd 76 65 64 20 69 6e 20 63 6f 61 6c 65 73 69 6e 67 ved in coalesing
159de 20 61 64 6a 61 63 65 6e 74 0a 2a 2a 20 66 72 65 adjacent.** fre
159df 65 20 62 6c 6f 63 6b 73 20 69 6e 74 6f 20 61 20 e blocks into a
159e0 73 69 6e 67 6c 65 20 62 69 67 20 66 72 65 65 20 single big free
159e1 62 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 block..*/.static
159e2 20 69 6e 74 20 66 72 65 65 53 70 61 63 65 28 4d int freeSpace(M
159e3 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69 emPage *pPage, i
159e4 6e 74 20 73 74 61 72 74 2c 20 69 6e 74 20 73 69 nt start, int si
159e5 7a 65 29 7b 0a 20 20 69 6e 74 20 61 64 64 72 2c ze){. int addr,
159e6 20 70 62 65 67 69 6e 2c 20 68 64 72 3b 0a 20 20 pbegin, hdr;.
159e7 69 6e 74 20 69 4c 61 73 74 3b 20 20 20 20 20 20 int iLast;
159e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
159e9 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 70 6f 73 /* Largest pos
159ea 73 69 62 6c 65 20 66 72 65 65 62 6c 6f 63 6b 20 sible freeblock
159eb 6f 66 66 73 65 74 20 2a 2f 0a 20 20 75 6e 73 69 offset */. unsi
159ec 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61 20 gned char *data
159ed 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a = pPage->aData;.
159ee 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
159ef 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 ->pBt!=0 );. as
159f0 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 sert( sqlite3Pag
159f1 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 erIswriteable(pP
159f2 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b age->pDbPage) );
159f3 0a 20 20 61 73 73 65 72 74 28 20 73 74 61 72 74 . assert( start
159f4 3e 3d 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 >=pPage->hdrOffs
159f5 65 74 2b 36 2b 70 50 61 67 65 2d 3e 63 68 69 6c et+6+pPage->chil
159f6 64 50 74 72 53 69 7a 65 20 29 3b 0a 20 20 61 73 dPtrSize );. as
159f7 73 65 72 74 28 20 28 73 74 61 72 74 20 2b 20 73 sert( (start + s
159f8 69 7a 65 29 3c 3d 70 50 61 67 65 2d 3e 70 42 74 ize)<=pPage->pBt
159f9 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a ->usableSize );.
159fa 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
159fb 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 3_mutex_held(pPa
159fc 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 ge->pBt->mutex)
159fd 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a );. assert( siz
159fe 65 3e 3d 30 20 29 3b 20 20 20 2f 2a 20 4d 69 6e e>=0 ); /* Min
159ff 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 69 imum cell size i
15a00 73 20 34 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 s 4 */..#ifdef S
15a01 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 45 4c QLITE_SECURE_DEL
15a02 45 54 45 0a 20 20 2f 2a 20 4f 76 65 72 77 72 69 ETE. /* Overwri
15a03 74 65 20 64 65 6c 65 74 65 64 20 69 6e 66 6f 72 te deleted infor
15a04 6d 61 74 69 6f 6e 20 77 69 74 68 20 7a 65 72 6f mation with zero
15a05 73 20 77 68 65 6e 20 74 68 65 20 53 45 43 55 52 s when the SECUR
15a06 45 5f 44 45 4c 45 54 45 20 0a 20 20 2a 2a 20 6f E_DELETE . ** o
15a07 70 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 ption is enabled
15a08 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 at compile-time
15a09 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 */. memset(&da
15a0a 74 61 5b 73 74 61 72 74 5d 2c 20 30 2c 20 73 69 ta[start], 0, si
15a0b 7a 65 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f ze);.#endif.. /
15a0c 2a 20 41 64 64 20 74 68 65 20 73 70 61 63 65 20 * Add the space
15a0d 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 6c 69 back into the li
15a0e 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 66 72 65 nked list of fre
15a0f 65 62 6c 6f 63 6b 73 2e 20 20 4e 6f 74 65 20 74 eblocks. Note t
15a10 68 61 74 0a 20 20 2a 2a 20 65 76 65 6e 20 74 68 hat. ** even th
15a11 6f 75 67 68 20 74 68 65 20 66 72 65 65 62 6c 6f ough the freeblo
15a12 63 6b 20 6c 69 73 74 20 77 61 73 20 63 68 65 63 ck list was chec
15a13 6b 65 64 20 62 79 20 62 74 72 65 65 49 6e 69 74 ked by btreeInit
15a14 50 61 67 65 28 29 2c 0a 20 20 2a 2a 20 62 74 72 Page(),. ** btr
15a15 65 65 49 6e 69 74 50 61 67 65 28 29 20 64 69 64 eeInitPage() did
15a16 20 6e 6f 74 20 64 65 74 65 63 74 20 6f 76 65 72 not detect over
15a17 6c 61 70 70 69 6e 67 20 63 65 6c 6c 73 20 6f 72 lapping cells or
15a18 0a 20 20 2a 2a 20 66 72 65 65 62 6c 6f 63 6b 73 . ** freeblocks
15a19 20 74 68 61 74 20 6f 76 65 72 6c 61 70 70 65 64 that overlapped
15a1a 20 63 65 6c 6c 73 2e 20 20 20 4e 6f 72 20 64 6f cells. Nor do
15a1b 65 73 20 69 74 20 64 65 74 65 63 74 20 77 68 65 es it detect whe
15a1c 6e 20 74 68 65 0a 20 20 2a 2a 20 63 65 6c 6c 20 n the. ** cell
15a1d 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 65 78 63 content area exc
15a1e 65 65 64 73 20 74 68 65 20 76 61 6c 75 65 20 69 eeds the value i
15a1f 6e 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 n the page heade
15a20 72 2e 20 20 49 66 20 74 68 65 73 65 0a 20 20 2a r. If these. *
15a21 2a 20 73 69 74 75 61 74 69 6f 6e 73 20 61 72 69 * situations ari
15a22 73 65 2c 20 74 68 65 6e 20 73 75 62 73 65 71 75 se, then subsequ
15a23 65 6e 74 20 69 6e 73 65 72 74 20 6f 70 65 72 61 ent insert opera
15a24 74 69 6f 6e 73 20 6d 69 67 68 74 20 63 6f 72 72 tions might corr
15a25 75 70 74 0a 20 20 2a 2a 20 74 68 65 20 66 72 65 upt. ** the fre
15a26 65 6c 69 73 74 2e 20 20 53 6f 20 77 65 20 64 6f elist. So we do
15a27 20 6e 65 65 64 20 74 6f 20 63 68 65 63 6b 20 66 need to check f
15a28 6f 72 20 63 6f 72 72 75 70 74 69 6f 6e 20 77 68 or corruption wh
15a29 69 6c 65 20 73 63 61 6e 6e 69 6e 67 0a 20 20 2a ile scanning. *
15a2a 2a 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 0a * the freelist..
15a2b 20 20 2a 2f 0a 20 20 68 64 72 20 3d 20 70 50 61 */. hdr = pPa
15a2c 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 ge->hdrOffset;.
15a2d 20 61 64 64 72 20 3d 20 68 64 72 20 2b 20 31 3b addr = hdr + 1;
15a2e 0a 20 20 69 4c 61 73 74 20 3d 20 70 50 61 67 65 . iLast = pPage
15a2f 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a ->pBt->usableSiz
15a30 65 20 2d 20 34 3b 0a 20 20 61 73 73 65 72 74 28 e - 4;. assert(
15a31 20 73 74 61 72 74 3c 3d 69 4c 61 73 74 20 29 3b start<=iLast );
15a32 0a 20 20 77 68 69 6c 65 28 20 28 70 62 65 67 69 . while( (pbegi
15a33 6e 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 n = get2byte(&da
15a34 74 61 5b 61 64 64 72 5d 29 29 3c 73 74 61 72 74 ta[addr]))<start
15a35 20 26 26 20 70 62 65 67 69 6e 3e 30 20 29 7b 0a && pbegin>0 ){.
15a36 20 20 20 20 69 66 28 20 70 62 65 67 69 6e 3c 61 if( pbegin<a
15a37 64 64 72 2b 34 20 29 7b 0a 20 20 20 20 20 20 72 ddr+4 ){. r
15a38 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
15a39 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d RUPT_BKPT;. }
15a3a 0a 20 20 20 20 61 64 64 72 20 3d 20 70 62 65 67 . addr = pbeg
15a3b 69 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 62 in;. }. if( pb
15a3c 65 67 69 6e 3e 69 4c 61 73 74 20 29 7b 0a 20 20 egin>iLast ){.
15a3d 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
15a3e 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
15a3f 7d 0a 20 20 61 73 73 65 72 74 28 20 70 62 65 67 }. assert( pbeg
15a40 69 6e 3e 61 64 64 72 20 7c 7c 20 70 62 65 67 69 in>addr || pbegi
15a41 6e 3d 3d 30 20 29 3b 0a 20 20 70 75 74 32 62 79 n==0 );. put2by
15a42 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d 2c 20 te(&data[addr],
15a43 73 74 61 72 74 29 3b 0a 20 20 70 75 74 32 62 79 start);. put2by
15a44 74 65 28 26 64 61 74 61 5b 73 74 61 72 74 5d 2c te(&data[start],
15a45 20 70 62 65 67 69 6e 29 3b 0a 20 20 70 75 74 32 pbegin);. put2
15a46 62 79 74 65 28 26 64 61 74 61 5b 73 74 61 72 74 byte(&data[start
15a47 2b 32 5d 2c 20 73 69 7a 65 29 3b 0a 20 20 70 50 +2], size);. pP
15a48 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20 70 50 61 age->nFree = pPa
15a49 67 65 2d 3e 6e 46 72 65 65 20 2b 20 28 75 31 36 ge->nFree + (u16
15a4a 29 73 69 7a 65 3b 0a 0a 20 20 2f 2a 20 43 6f 61 )size;.. /* Coa
15a4b 6c 65 73 63 65 20 61 64 6a 61 63 65 6e 74 20 66 lesce adjacent f
15a4c 72 65 65 20 62 6c 6f 63 6b 73 20 2a 2f 0a 20 20 ree blocks */.
15a4d 61 64 64 72 20 3d 20 68 64 72 20 2b 20 31 3b 0a addr = hdr + 1;.
15a4e 20 20 77 68 69 6c 65 28 20 28 70 62 65 67 69 6e while( (pbegin
15a4f 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 = get2byte(&dat
15a50 61 5b 61 64 64 72 5d 29 29 3e 30 20 29 7b 0a 20 a[addr]))>0 ){.
15a51 20 20 20 69 6e 74 20 70 6e 65 78 74 2c 20 70 73 int pnext, ps
15a52 69 7a 65 2c 20 78 3b 0a 20 20 20 20 61 73 73 65 ize, x;. asse
15a53 72 74 28 20 70 62 65 67 69 6e 3e 61 64 64 72 20 rt( pbegin>addr
15a54 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
15a55 62 65 67 69 6e 3c 3d 70 50 61 67 65 2d 3e 70 42 begin<=pPage->pB
15a56 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 34 20 t->usableSize-4
15a57 29 3b 0a 20 20 20 20 70 6e 65 78 74 20 3d 20 67 );. pnext = g
15a58 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 62 et2byte(&data[pb
15a59 65 67 69 6e 5d 29 3b 0a 20 20 20 20 70 73 69 7a egin]);. psiz
15a5a 65 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 e = get2byte(&da
15a5b 74 61 5b 70 62 65 67 69 6e 2b 32 5d 29 3b 0a 20 ta[pbegin+2]);.
15a5c 20 20 20 69 66 28 20 70 62 65 67 69 6e 20 2b 20 if( pbegin +
15a5d 70 73 69 7a 65 20 2b 20 33 20 3e 3d 20 70 6e 65 psize + 3 >= pne
15a5e 78 74 20 26 26 20 70 6e 65 78 74 3e 30 20 29 7b xt && pnext>0 ){
15a5f 0a 20 20 20 20 20 20 69 6e 74 20 66 72 61 67 20 . int frag
15a60 3d 20 70 6e 65 78 74 20 2d 20 28 70 62 65 67 69 = pnext - (pbegi
15a61 6e 2b 70 73 69 7a 65 29 3b 0a 20 20 20 20 20 20 n+psize);.
15a62 69 66 28 20 28 66 72 61 67 3c 30 29 20 7c 7c 20 if( (frag<0) ||
15a63 28 66 72 61 67 3e 28 69 6e 74 29 64 61 74 61 5b (frag>(int)data[
15a64 68 64 72 2b 37 5d 29 20 29 7b 0a 20 20 20 20 20 hdr+7]) ){.
15a65 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
15a66 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
15a67 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 61 74 }. dat
15a68 61 5b 68 64 72 2b 37 5d 20 2d 3d 20 28 75 38 29 a[hdr+7] -= (u8)
15a69 66 72 61 67 3b 0a 20 20 20 20 20 20 78 20 3d 20 frag;. x =
15a6a 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 get2byte(&data[p
15a6b 6e 65 78 74 5d 29 3b 0a 20 20 20 20 20 20 70 75 next]);. pu
15a6c 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 62 65 t2byte(&data[pbe
15a6d 67 69 6e 5d 2c 20 78 29 3b 0a 20 20 20 20 20 20 gin], x);.
15a6e 78 20 3d 20 70 6e 65 78 74 20 2b 20 67 65 74 32 x = pnext + get2
15a6f 62 79 74 65 28 26 64 61 74 61 5b 70 6e 65 78 74 byte(&data[pnext
15a70 2b 32 5d 29 20 2d 20 70 62 65 67 69 6e 3b 0a 20 +2]) - pbegin;.
15a71 20 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64 put2byte(&d
15a72 61 74 61 5b 70 62 65 67 69 6e 2b 32 5d 2c 20 78 ata[pbegin+2], x
15a73 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
15a74 20 20 20 20 61 64 64 72 20 3d 20 70 62 65 67 69 addr = pbegi
15a75 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 n;. }. }..
15a76 2f 2a 20 49 66 20 74 68 65 20 63 65 6c 6c 20 63 /* If the cell c
15a77 6f 6e 74 65 6e 74 20 61 72 65 61 20 62 65 67 69 ontent area begi
15a78 6e 73 20 77 69 74 68 20 61 20 66 72 65 65 62 6c ns with a freebl
15a79 6f 63 6b 2c 20 72 65 6d 6f 76 65 20 69 74 2e 20 ock, remove it.
15a7a 2a 2f 0a 20 20 69 66 28 20 64 61 74 61 5b 68 64 */. if( data[hd
15a7b 72 2b 31 5d 3d 3d 64 61 74 61 5b 68 64 72 2b 35 r+1]==data[hdr+5
15a7c 5d 20 26 26 20 64 61 74 61 5b 68 64 72 2b 32 5d ] && data[hdr+2]
15a7d 3d 3d 64 61 74 61 5b 68 64 72 2b 36 5d 20 29 7b ==data[hdr+6] ){
15a7e 0a 20 20 20 20 69 6e 74 20 74 6f 70 3b 0a 20 20 . int top;.
15a7f 20 20 70 62 65 67 69 6e 20 3d 20 67 65 74 32 62 pbegin = get2b
15a80 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 31 5d yte(&data[hdr+1]
15a81 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 64 );. memcpy(&d
15a82 61 74 61 5b 68 64 72 2b 31 5d 2c 20 26 64 61 74 ata[hdr+1], &dat
15a83 61 5b 70 62 65 67 69 6e 5d 2c 20 32 29 3b 0a 20 a[pbegin], 2);.
15a84 20 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79 74 top = get2byt
15a85 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 20 e(&data[hdr+5])
15a86 2b 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 + get2byte(&data
15a87 5b 70 62 65 67 69 6e 2b 32 5d 29 3b 0a 20 20 20 [pbegin+2]);.
15a88 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b put2byte(&data[
15a89 68 64 72 2b 35 5d 2c 20 74 6f 70 29 3b 0a 20 20 hdr+5], top);.
15a8a 7d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 }. assert( sqli
15a8b 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 te3PagerIswritea
15a8c 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ble(pPage->pDbPa
15a8d 67 65 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 ge) );. return
15a8e 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
15a8f 0a 2a 2a 20 44 65 63 6f 64 65 20 74 68 65 20 66 .** Decode the f
15a90 6c 61 67 73 20 62 79 74 65 20 28 74 68 65 20 66 lags byte (the f
15a91 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68 65 irst byte of the
15a92 20 68 65 61 64 65 72 29 20 66 6f 72 20 61 20 70 header) for a p
15a93 61 67 65 0a 2a 2a 20 61 6e 64 20 69 6e 69 74 69 age.** and initi
15a94 61 6c 69 7a 65 20 66 69 65 6c 64 73 20 6f 66 20 alize fields of
15a95 74 68 65 20 4d 65 6d 50 61 67 65 20 73 74 72 75 the MemPage stru
15a96 63 74 75 72 65 20 61 63 63 6f 72 64 69 6e 67 6c cture accordingl
15a97 79 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 74 68 y..**.** Only th
15a98 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6d 62 e following comb
15a99 69 6e 61 74 69 6f 6e 73 20 61 72 65 20 73 75 70 inations are sup
15a9a 70 6f 72 74 65 64 2e 20 20 41 6e 79 74 68 69 6e ported. Anythin
15a9b 67 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 69 g different.** i
15a9c 6e 64 69 63 61 74 65 73 20 61 20 63 6f 72 72 75 ndicates a corru
15a9d 70 74 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 pt database file
15a9e 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 s:.**.**
15a9f 20 50 54 46 5f 5a 45 52 4f 44 41 54 41 0a 2a 2a PTF_ZERODATA.**
15aa0 20 20 20 20 20 20 20 20 20 50 54 46 5f 5a 45 52 PTF_ZER
15aa1 4f 44 41 54 41 20 7c 20 50 54 46 5f 4c 45 41 46 ODATA | PTF_LEAF
15aa2 0a 2a 2a 20 20 20 20 20 20 20 20 20 50 54 46 5f .** PTF_
15aa3 4c 45 41 46 44 41 54 41 20 7c 20 50 54 46 5f 49 LEAFDATA | PTF_I
15aa4 4e 54 4b 45 59 0a 2a 2a 20 20 20 20 20 20 20 20 NTKEY.**
15aa5 20 50 54 46 5f 4c 45 41 46 44 41 54 41 20 7c 20 PTF_LEAFDATA |
15aa6 50 54 46 5f 49 4e 54 4b 45 59 20 7c 20 50 54 46 PTF_INTKEY | PTF
15aa7 5f 4c 45 41 46 0a 2a 2f 0a 73 74 61 74 69 63 20 _LEAF.*/.static
15aa8 69 6e 74 20 64 65 63 6f 64 65 46 6c 61 67 73 28 int decodeFlags(
15aa9 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 MemPage *pPage,
15aaa 69 6e 74 20 66 6c 61 67 42 79 74 65 29 7b 0a 20 int flagByte){.
15aab 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20 BtShared *pBt;
15aac 20 20 20 20 2f 2a 20 41 20 63 6f 70 79 20 6f 66 /* A copy of
15aad 20 70 50 61 67 65 2d 3e 70 42 74 20 2a 2f 0a 0a pPage->pBt */..
15aae 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
15aaf 3e 68 64 72 4f 66 66 73 65 74 3d 3d 28 70 50 61 >hdrOffset==(pPa
15ab0 67 65 2d 3e 70 67 6e 6f 3d 3d 31 20 3f 20 31 30 ge->pgno==1 ? 10
15ab1 30 20 3a 20 30 29 20 29 3b 0a 20 20 61 73 73 65 0 : 0) );. asse
15ab2 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
15ab3 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 x_held(pPage->pB
15ab4 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 t->mutex) );. p
15ab5 50 61 67 65 2d 3e 6c 65 61 66 20 3d 20 28 75 38 Page->leaf = (u8
15ab6 29 28 66 6c 61 67 42 79 74 65 3e 3e 33 29 3b 20 )(flagByte>>3);
15ab7 20 61 73 73 65 72 74 28 20 50 54 46 5f 4c 45 41 assert( PTF_LEA
15ab8 46 20 3d 3d 20 31 3c 3c 33 20 29 3b 0a 20 20 66 F == 1<<3 );. f
15ab9 6c 61 67 42 79 74 65 20 26 3d 20 7e 50 54 46 5f lagByte &= ~PTF_
15aba 4c 45 41 46 3b 0a 20 20 70 50 61 67 65 2d 3e 63 LEAF;. pPage->c
15abb 68 69 6c 64 50 74 72 53 69 7a 65 20 3d 20 34 2d hildPtrSize = 4-
15abc 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a 20 4*pPage->leaf;.
15abd 20 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 pBt = pPage->pB
15abe 74 3b 0a 20 20 69 66 28 20 66 6c 61 67 42 79 74 t;. if( flagByt
15abf 65 3d 3d 28 50 54 46 5f 4c 45 41 46 44 41 54 41 e==(PTF_LEAFDATA
15ac0 20 7c 20 50 54 46 5f 49 4e 54 4b 45 59 29 20 29 | PTF_INTKEY) )
15ac1 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 6e 74 {. pPage->int
15ac2 4b 65 79 20 3d 20 31 3b 0a 20 20 20 20 70 50 61 Key = 1;. pPa
15ac3 67 65 2d 3e 68 61 73 44 61 74 61 20 3d 20 70 50 ge->hasData = pP
15ac4 61 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 20 20 70 age->leaf;. p
15ac5 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 3d Page->maxLocal =
15ac6 20 70 42 74 2d 3e 6d 61 78 4c 65 61 66 3b 0a 20 pBt->maxLeaf;.
15ac7 20 20 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 pPage->minLoc
15ac8 61 6c 20 3d 20 70 42 74 2d 3e 6d 69 6e 4c 65 61 al = pBt->minLea
15ac9 66 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66 f;. }else if( f
15aca 6c 61 67 42 79 74 65 3d 3d 50 54 46 5f 5a 45 52 lagByte==PTF_ZER
15acb 4f 44 41 54 41 20 29 7b 0a 20 20 20 20 70 50 61 ODATA ){. pPa
15acc 67 65 2d 3e 69 6e 74 4b 65 79 20 3d 20 30 3b 0a ge->intKey = 0;.
15acd 20 20 20 20 70 50 61 67 65 2d 3e 68 61 73 44 61 pPage->hasDa
15ace 74 61 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 ta = 0;. pPag
15acf 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20 70 42 e->maxLocal = pB
15ad0 74 2d 3e 6d 61 78 4c 6f 63 61 6c 3b 0a 20 20 20 t->maxLocal;.
15ad1 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 6c pPage->minLocal
15ad2 20 3d 20 70 42 74 2d 3e 6d 69 6e 4c 6f 63 61 6c = pBt->minLocal
15ad3 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 ;. }else{. r
15ad4 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
15ad5 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 RUPT_BKPT;. }.
15ad6 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
15ad7 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 K;.}../*.** Init
15ad8 69 61 6c 69 7a 65 20 74 68 65 20 61 75 78 69 6c ialize the auxil
15ad9 69 61 72 79 20 69 6e 66 6f 72 6d 61 74 69 6f 6e iary information
15ada 20 66 6f 72 20 61 20 64 69 73 6b 20 62 6c 6f 63 for a disk bloc
15adb 6b 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 k..**.** Return
15adc 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 SQLITE_OK on suc
15add 63 65 73 73 2e 20 20 49 66 20 77 65 20 73 65 65 cess. If we see
15ade 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 64 that the page d
15adf 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61 oes.** not conta
15ae0 69 6e 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 in a well-formed
15ae1 20 64 61 74 61 62 61 73 65 20 70 61 67 65 2c 20 database page,
15ae2 74 68 65 6e 20 72 65 74 75 72 6e 20 0a 2a 2a 20 then return .**
15ae3 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2e 20 SQLITE_CORRUPT.
15ae4 20 4e 6f 74 65 20 74 68 61 74 20 61 20 72 65 74 Note that a ret
15ae5 75 72 6e 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4b urn of SQLITE_OK
15ae6 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 67 75 61 does not.** gua
15ae7 72 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 rantee that the
15ae8 70 61 67 65 20 69 73 20 77 65 6c 6c 2d 66 6f 72 page is well-for
15ae9 6d 65 64 2e 20 20 49 74 20 6f 6e 6c 79 20 73 68 med. It only sh
15aea 6f 77 73 20 74 68 61 74 0a 2a 2a 20 77 65 20 66 ows that.** we f
15aeb 61 69 6c 65 64 20 74 6f 20 64 65 74 65 63 74 20 ailed to detect
15aec 61 6e 79 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a any corruption..
15aed 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 */.static int bt
15aee 72 65 65 49 6e 69 74 50 61 67 65 28 4d 65 6d 50 reeInitPage(MemP
15aef 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 0a 20 20 age *pPage){..
15af0 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 assert( pPage->p
15af1 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 Bt!=0 );. asser
15af2 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
15af3 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 _held(pPage->pBt
15af4 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
15af5 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 67 6e sert( pPage->pgn
15af6 6f 3d 3d 73 71 6c 69 74 65 33 50 61 67 65 72 50 o==sqlite3PagerP
15af7 61 67 65 6e 75 6d 62 65 72 28 70 50 61 67 65 2d agenumber(pPage-
15af8 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 >pDbPage) );. a
15af9 73 73 65 72 74 28 20 70 50 61 67 65 20 3d 3d 20 ssert( pPage ==
15afa 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 sqlite3PagerGetE
15afb 78 74 72 61 28 70 50 61 67 65 2d 3e 70 44 62 50 xtra(pPage->pDbP
15afc 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 age) );. assert
15afd 28 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 3d ( pPage->aData =
15afe 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 = sqlite3PagerGe
15aff 74 44 61 74 61 28 70 50 61 67 65 2d 3e 70 44 62 tData(pPage->pDb
15b00 50 61 67 65 29 20 29 3b 0a 0a 20 20 69 66 28 20 Page) );.. if(
15b01 21 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29 !pPage->isInit )
15b02 7b 0a 20 20 20 20 75 31 36 20 70 63 3b 20 20 20 {. u16 pc;
15b03 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 /* Addr
15b04 65 73 73 20 6f 66 20 61 20 66 72 65 65 62 6c 6f ess of a freeblo
15b05 63 6b 20 77 69 74 68 69 6e 20 70 50 61 67 65 2d ck within pPage-
15b06 3e 61 44 61 74 61 5b 5d 20 2a 2f 0a 20 20 20 20 >aData[] */.
15b07 75 38 20 68 64 72 3b 20 20 20 20 20 20 20 20 20 u8 hdr;
15b08 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 /* Offset to
15b09 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 70 61 67 beginning of pag
15b0a 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 e header */.
15b0b 75 38 20 2a 64 61 74 61 3b 20 20 20 20 20 20 20 u8 *data;
15b0c 20 20 20 2f 2a 20 45 71 75 61 6c 20 74 6f 20 70 /* Equal to p
15b0d 50 61 67 65 2d 3e 61 44 61 74 61 20 2a 2f 0a 20 Page->aData */.
15b0e 20 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 BtShared *pBt
15b0f 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ; /* The
15b10 6d 61 69 6e 20 62 74 72 65 65 20 73 74 72 75 63 main btree struc
15b11 74 75 72 65 20 2a 2f 0a 20 20 20 20 75 31 36 20 ture */. u16
15b12 75 73 61 62 6c 65 53 69 7a 65 3b 20 20 20 20 2f usableSize; /
15b13 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 75 73 61 62 * Amount of usab
15b14 6c 65 20 73 70 61 63 65 20 6f 6e 20 65 61 63 68 le space on each
15b15 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 75 31 36 page */. u16
15b16 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20 20 20 cellOffset;
15b17 2f 2a 20 4f 66 66 73 65 74 20 66 72 6f 6d 20 73 /* Offset from s
15b18 74 61 72 74 20 6f 66 20 70 61 67 65 20 74 6f 20 tart of page to
15b19 66 69 72 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 first cell point
15b1a 65 72 20 2a 2f 0a 20 20 20 20 75 31 36 20 6e 46 er */. u16 nF
15b1b 72 65 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 ree; /*
15b1c 4e 75 6d 62 65 72 20 6f 66 20 75 6e 75 73 65 64 Number of unused
15b1d 20 62 79 74 65 73 20 6f 6e 20 74 68 65 20 70 61 bytes on the pa
15b1e 67 65 20 2a 2f 0a 20 20 20 20 75 31 36 20 74 6f ge */. u16 to
15b1f 70 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 p; /*
15b20 46 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68 First byte of th
15b21 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 e cell content a
15b22 72 65 61 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 rea */. int i
15b23 43 65 6c 6c 46 69 72 73 74 3b 20 20 20 20 2f 2a CellFirst; /*
15b24 20 46 69 72 73 74 20 61 6c 6c 6f 77 61 62 6c 65 First allowable
15b25 20 63 65 6c 6c 20 6f 72 20 66 72 65 65 62 6c 6f cell or freeblo
15b26 63 6b 20 6f 66 66 73 65 74 20 2a 2f 0a 20 20 20 ck offset */.
15b27 20 69 6e 74 20 69 43 65 6c 6c 4c 61 73 74 3b 20 int iCellLast;
15b28 20 20 20 20 2f 2a 20 4c 61 73 74 20 70 6f 73 73 /* Last poss
15b29 69 62 6c 65 20 63 65 6c 6c 20 6f 72 20 66 72 65 ible cell or fre
15b2a 65 62 6c 6f 63 6b 20 6f 66 66 73 65 74 20 2a 2f eblock offset */
15b2b 0a 0a 20 20 20 20 70 42 74 20 3d 20 70 50 61 67 .. pBt = pPag
15b2c 65 2d 3e 70 42 74 3b 0a 0a 20 20 20 20 68 64 72 e->pBt;.. hdr
15b2d 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 = pPage->hdrOff
15b2e 73 65 74 3b 0a 20 20 20 20 64 61 74 61 20 3d 20 set;. data =
15b2f 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 pPage->aData;.
15b30 20 20 69 66 28 20 64 65 63 6f 64 65 46 6c 61 67 if( decodeFlag
15b31 73 28 70 50 61 67 65 2c 20 64 61 74 61 5b 68 64 s(pPage, data[hd
15b32 72 5d 29 20 29 20 72 65 74 75 72 6e 20 53 51 4c r]) ) return SQL
15b33 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
15b34 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42 ;. assert( pB
15b35 74 2d 3e 70 61 67 65 53 69 7a 65 3e 3d 35 31 32 t->pageSize>=512
15b36 20 26 26 20 70 42 74 2d 3e 70 61 67 65 53 69 7a && pBt->pageSiz
15b37 65 3c 3d 33 32 37 36 38 20 29 3b 0a 20 20 20 20 e<=32768 );.
15b38 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67 65 20 pPage->maskPage
15b39 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 = pBt->pageSize
15b3a 2d 20 31 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e - 1;. pPage->
15b3b 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 nOverflow = 0;.
15b3c 20 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 usableSize =
15b3d 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b pBt->usableSize;
15b3e 0a 20 20 20 20 70 50 61 67 65 2d 3e 63 65 6c 6c . pPage->cell
15b3f 4f 66 66 73 65 74 20 3d 20 63 65 6c 6c 4f 66 66 Offset = cellOff
15b40 73 65 74 20 3d 20 68 64 72 20 2b 20 31 32 20 2d set = hdr + 12 -
15b41 20 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a 4*pPage->leaf;.
15b42 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79 top = get2by
15b43 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 te(&data[hdr+5])
15b44 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 43 65 ;. pPage->nCe
15b45 6c 6c 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 ll = get2byte(&d
15b46 61 74 61 5b 68 64 72 2b 33 5d 29 3b 0a 20 20 20 ata[hdr+3]);.
15b47 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c if( pPage->nCel
15b48 6c 3e 4d 58 5f 43 45 4c 4c 28 70 42 74 29 20 29 l>MX_CELL(pBt) )
15b49 7b 0a 20 20 20 20 20 20 2f 2a 20 54 6f 20 6d 61 {. /* To ma
15b4a 6e 79 20 63 65 6c 6c 73 20 66 6f 72 20 61 20 73 ny cells for a s
15b4b 69 6e 67 6c 65 20 70 61 67 65 2e 20 20 54 68 65 ingle page. The
15b4c 20 70 61 67 65 20 6d 75 73 74 20 62 65 20 63 6f page must be co
15b4d 72 72 75 70 74 20 2a 2f 0a 20 20 20 20 20 20 72 rrupt */. r
15b4e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
15b4f 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d RUPT_BKPT;. }
15b50 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 . testcase( p
15b51 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 4d 58 5f Page->nCell==MX_
15b52 43 45 4c 4c 28 70 42 74 29 20 29 3b 0a 0a 20 20 CELL(pBt) );..
15b53 20 20 2f 2a 20 41 20 6d 61 6c 66 6f 72 6d 65 64 /* A malformed
15b54 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 6d database page m
15b55 69 67 68 74 20 63 61 75 73 65 20 75 73 20 74 6f ight cause us to
15b56 20 72 65 61 64 20 70 61 73 74 20 74 68 65 20 65 read past the e
15b57 6e 64 0a 20 20 20 20 2a 2a 20 6f 66 20 70 61 67 nd. ** of pag
15b58 65 20 77 68 65 6e 20 70 61 72 73 69 6e 67 20 61 e when parsing a
15b59 20 63 65 6c 6c 2e 20 20 0a 20 20 20 20 2a 2a 0a cell. . **.
15b5a 20 20 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f ** The follo
15b5b 77 69 6e 67 20 62 6c 6f 63 6b 20 6f 66 20 63 6f wing block of co
15b5c 64 65 20 63 68 65 63 6b 73 20 65 61 72 6c 79 20 de checks early
15b5d 74 6f 20 73 65 65 20 69 66 20 61 20 63 65 6c 6c to see if a cell
15b5e 20 65 78 74 65 6e 64 73 0a 20 20 20 20 2a 2a 20 extends. **
15b5f 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 past the end of
15b60 61 20 70 61 67 65 20 62 6f 75 6e 64 61 72 79 20 a page boundary
15b61 61 6e 64 20 63 61 75 73 65 73 20 53 51 4c 49 54 and causes SQLIT
15b62 45 5f 43 4f 52 52 55 50 54 20 74 6f 20 62 65 20 E_CORRUPT to be
15b63 0a 20 20 20 20 2a 2a 20 72 65 74 75 72 6e 65 64 . ** returned
15b64 20 69 66 20 69 74 20 64 6f 65 73 2e 0a 20 20 20 if it does..
15b65 20 2a 2f 0a 20 20 20 20 69 43 65 6c 6c 46 69 72 */. iCellFir
15b66 73 74 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 20 st = cellOffset
15b67 2b 20 32 2a 70 50 61 67 65 2d 3e 6e 43 65 6c 6c + 2*pPage->nCell
15b68 3b 0a 20 20 20 20 69 43 65 6c 6c 4c 61 73 74 20 ;. iCellLast
15b69 3d 20 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 = usableSize - 4
15b6a 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 ;.#if defined(SQ
15b6b 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4f 56 45 52 LITE_ENABLE_OVER
15b6c 53 49 5a 45 5f 43 45 4c 4c 5f 43 48 45 43 4b 29 SIZE_CELL_CHECK)
15b6d 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 69 6e 74 . {. int
15b6e 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f i; /
15b6f 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20 74 68 65 * Index into the
15b70 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 cell pointer ar
15b71 72 61 79 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 ray */. int
15b72 20 73 7a 3b 20 20 20 20 20 20 20 20 20 20 20 2f sz; /
15b73 2a 20 53 69 7a 65 20 6f 66 20 61 20 63 65 6c 6c * Size of a cell
15b74 20 2a 2f 0a 0a 20 20 20 20 20 20 69 66 28 20 21 */.. if( !
15b75 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 20 69 43 pPage->leaf ) iC
15b76 65 6c 6c 4c 61 73 74 2d 2d 3b 0a 20 20 20 20 20 ellLast--;.
15b77 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 67 for(i=0; i<pPag
15b78 65 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a e->nCell; i++){.
15b79 20 20 20 20 20 20 20 20 70 63 20 3d 20 67 65 74 pc = get
15b7a 32 62 79 74 65 28 26 64 61 74 61 5b 63 65 6c 6c 2byte(&data[cell
15b7b 4f 66 66 73 65 74 2b 69 2a 32 5d 29 3b 0a 20 20 Offset+i*2]);.
15b7c 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
15b7d 70 63 3d 3d 69 43 65 6c 6c 46 69 72 73 74 20 29 pc==iCellFirst )
15b7e 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 ;. testca
15b7f 73 65 28 20 70 63 3d 3d 69 43 65 6c 6c 4c 61 73 se( pc==iCellLas
15b80 74 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 t );. if(
15b81 20 70 63 3c 69 43 65 6c 6c 46 69 72 73 74 20 7c pc<iCellFirst |
15b82 7c 20 70 63 3e 69 43 65 6c 6c 4c 61 73 74 20 29 | pc>iCellLast )
15b83 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 {. retu
15b84 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
15b85 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 T_BKPT;.
15b86 7d 0a 20 20 20 20 20 20 20 20 73 7a 20 3d 20 63 }. sz = c
15b87 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 ellSizePtr(pPage
15b88 2c 20 26 64 61 74 61 5b 70 63 5d 29 3b 0a 20 20 , &data[pc]);.
15b89 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
15b8a 70 63 2b 73 7a 3d 3d 75 73 61 62 6c 65 53 69 7a pc+sz==usableSiz
15b8b 65 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 e );. if(
15b8c 20 70 63 2b 73 7a 3e 75 73 61 62 6c 65 53 69 7a pc+sz>usableSiz
15b8d 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 e ){. r
15b8e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
15b8f 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 RUPT_BKPT;.
15b90 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
15b91 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c if( !pPage->l
15b92 65 61 66 20 29 20 69 43 65 6c 6c 4c 61 73 74 2b eaf ) iCellLast+
15b93 2b 3b 0a 20 20 20 20 7d 20 20 0a 23 65 6e 64 69 +;. } .#endi
15b94 66 0a 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 75 74 f.. /* Comput
15b95 65 20 74 68 65 20 74 6f 74 61 6c 20 66 72 65 65 e the total free
15b96 20 73 70 61 63 65 20 6f 6e 20 74 68 65 20 70 61 space on the pa
15b97 67 65 20 2a 2f 0a 20 20 20 20 70 63 20 3d 20 67 ge */. pc = g
15b98 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 et2byte(&data[hd
15b99 72 2b 31 5d 29 3b 0a 20 20 20 20 6e 46 72 65 65 r+1]);. nFree
15b9a 20 3d 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 2b = data[hdr+7] +
15b9b 20 74 6f 70 3b 0a 20 20 20 20 77 68 69 6c 65 28 top;. while(
15b9c 20 70 63 3e 30 20 29 7b 0a 20 20 20 20 20 20 75 pc>0 ){. u
15b9d 31 36 20 6e 65 78 74 2c 20 73 69 7a 65 3b 0a 20 16 next, size;.
15b9e 20 20 20 20 20 69 66 28 20 70 63 3c 69 43 65 6c if( pc<iCel
15b9f 6c 46 69 72 73 74 20 7c 7c 20 70 63 3e 69 43 65 lFirst || pc>iCe
15ba0 6c 6c 4c 61 73 74 20 29 7b 0a 20 20 20 20 20 20 llLast ){.
15ba1 20 20 2f 2a 20 53 74 61 72 74 20 6f 66 20 66 72 /* Start of fr
15ba2 65 65 20 62 6c 6f 63 6b 20 69 73 20 6f 66 66 20 ee block is off
15ba3 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 the page */.
15ba4 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
15ba5 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 20 E_CORRUPT_BKPT;
15ba6 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6e . }. n
15ba7 65 78 74 20 3d 20 67 65 74 32 62 79 74 65 28 26 ext = get2byte(&
15ba8 64 61 74 61 5b 70 63 5d 29 3b 0a 20 20 20 20 20 data[pc]);.
15ba9 20 73 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65 size = get2byte
15baa 28 26 64 61 74 61 5b 70 63 2b 32 5d 29 3b 0a 20 (&data[pc+2]);.
15bab 20 20 20 20 20 69 66 28 20 28 6e 65 78 74 3e 30 if( (next>0
15bac 20 26 26 20 6e 65 78 74 3c 3d 70 63 2b 73 69 7a && next<=pc+siz
15bad 65 2b 33 29 20 7c 7c 20 70 63 2b 73 69 7a 65 3e e+3) || pc+size>
15bae 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 usableSize ){.
15baf 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20 62 6c /* Free bl
15bb0 6f 63 6b 73 20 6d 75 73 74 20 62 65 20 69 6e 20 ocks must be in
15bb1 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e ascending order.
15bb2 20 41 6e 64 20 74 68 65 20 6c 61 73 74 20 62 79 And the last by
15bb3 74 65 20 6f 66 0a 09 2a 2a 20 74 68 65 20 66 72 te of..** the fr
15bb4 65 65 2d 62 6c 6f 63 6b 20 6d 75 73 74 20 6c 69 ee-block must li
15bb5 65 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 e on the databas
15bb6 65 20 70 61 67 65 2e 20 20 2a 2f 0a 20 20 20 20 e page. */.
15bb7 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
15bb8 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 20 E_CORRUPT_BKPT;
15bb9 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6e . }. n
15bba 46 72 65 65 20 3d 20 6e 46 72 65 65 20 2b 20 73 Free = nFree + s
15bbb 69 7a 65 3b 0a 20 20 20 20 20 20 70 63 20 3d 20 ize;. pc =
15bbc 6e 65 78 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 next;. }..
15bbd 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e /* At this poin
15bbe 74 2c 20 6e 46 72 65 65 20 63 6f 6e 74 61 69 6e t, nFree contain
15bbf 73 20 74 68 65 20 73 75 6d 20 6f 66 20 74 68 65 s the sum of the
15bc0 20 6f 66 66 73 65 74 20 74 6f 20 74 68 65 20 73 offset to the s
15bc1 74 61 72 74 0a 20 20 20 20 2a 2a 20 6f 66 20 74 tart. ** of t
15bc2 68 65 20 63 65 6c 6c 2d 63 6f 6e 74 65 6e 74 20 he cell-content
15bc3 61 72 65 61 20 70 6c 75 73 20 74 68 65 20 6e 75 area plus the nu
15bc4 6d 62 65 72 20 6f 66 20 66 72 65 65 20 62 79 74 mber of free byt
15bc5 65 73 20 77 69 74 68 69 6e 0a 20 20 20 20 2a 2a es within. **
15bc6 20 74 68 65 20 63 65 6c 6c 2d 63 6f 6e 74 65 6e the cell-conten
15bc7 74 20 61 72 65 61 2e 20 49 66 20 74 68 69 73 20 t area. If this
15bc8 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 is greater than
15bc9 74 68 65 20 75 73 61 62 6c 65 2d 73 69 7a 65 0a the usable-size.
15bca 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 70 61 ** of the pa
15bcb 67 65 2c 20 74 68 65 6e 20 74 68 65 20 70 61 67 ge, then the pag
15bcc 65 20 6d 75 73 74 20 62 65 20 63 6f 72 72 75 70 e must be corrup
15bcd 74 65 64 2e 20 54 68 69 73 20 63 68 65 63 6b 20 ted. This check
15bce 61 6c 73 6f 0a 20 20 20 20 2a 2a 20 73 65 72 76 also. ** serv
15bcf 65 73 20 74 6f 20 76 65 72 69 66 79 20 74 68 61 es to verify tha
15bd0 74 20 74 68 65 20 6f 66 66 73 65 74 20 74 6f 20 t the offset to
15bd1 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 the start of the
15bd2 20 63 65 6c 6c 2d 63 6f 6e 74 65 6e 74 0a 20 20 cell-content.
15bd3 20 20 2a 2a 20 61 72 65 61 2c 20 61 63 63 6f 72 ** area, accor
15bd4 64 69 6e 67 20 74 6f 20 74 68 65 20 70 61 67 65 ding to the page
15bd5 20 68 65 61 64 65 72 2c 20 6c 69 65 73 20 77 69 header, lies wi
15bd6 74 68 69 6e 20 74 68 65 20 70 61 67 65 2e 0a 20 thin the page..
15bd7 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 46 */. if( nF
15bd8 72 65 65 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 ree>usableSize )
15bd9 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
15bda 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
15bdb 50 54 3b 20 0a 20 20 20 20 7d 0a 20 20 20 20 70 PT; . }. p
15bdc 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20 28 75 Page->nFree = (u
15bdd 31 36 29 28 6e 46 72 65 65 20 2d 20 69 43 65 6c 16)(nFree - iCel
15bde 6c 46 69 72 73 74 29 3b 0a 20 20 20 20 70 50 61 lFirst);. pPa
15bdf 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 31 3b 0a ge->isInit = 1;.
15be0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
15be1 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
15be2 20 53 65 74 20 75 70 20 61 20 72 61 77 20 70 61 Set up a raw pa
15be3 67 65 20 73 6f 20 74 68 61 74 20 69 74 20 6c 6f ge so that it lo
15be4 6f 6b 73 20 6c 69 6b 65 20 61 20 64 61 74 61 62 oks like a datab
15be5 61 73 65 20 70 61 67 65 20 68 6f 6c 64 69 6e 67 ase page holding
15be6 0a 2a 2a 20 6e 6f 20 65 6e 74 72 69 65 73 2e 0a .** no entries..
15be7 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 7a */.static void z
15be8 65 72 6f 50 61 67 65 28 4d 65 6d 50 61 67 65 20 eroPage(MemPage
15be9 2a 70 50 61 67 65 2c 20 69 6e 74 20 66 6c 61 67 *pPage, int flag
15bea 73 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 s){. unsigned c
15beb 68 61 72 20 2a 64 61 74 61 20 3d 20 70 50 61 67 har *data = pPag
15bec 65 2d 3e 61 44 61 74 61 3b 0a 20 20 42 74 53 68 e->aData;. BtSh
15bed 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 ared *pBt = pPag
15bee 65 2d 3e 70 42 74 3b 0a 20 20 75 38 20 68 64 72 e->pBt;. u8 hdr
15bef 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 = pPage->hdrOff
15bf0 73 65 74 3b 0a 20 20 75 31 36 20 66 69 72 73 74 set;. u16 first
15bf1 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
15bf2 69 74 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d ite3PagerPagenum
15bf3 62 65 72 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ber(pPage->pDbPa
15bf4 67 65 29 3d 3d 70 50 61 67 65 2d 3e 70 67 6e 6f ge)==pPage->pgno
15bf5 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
15bf6 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74 lite3PagerGetExt
15bf7 72 61 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 ra(pPage->pDbPag
15bf8 65 29 20 3d 3d 20 28 76 6f 69 64 2a 29 70 50 61 e) == (void*)pPa
15bf9 67 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ge );. assert(
15bfa 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 sqlite3PagerGetD
15bfb 61 74 61 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ata(pPage->pDbPa
15bfc 67 65 29 20 3d 3d 20 64 61 74 61 20 29 3b 0a 20 ge) == data );.
15bfd 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
15bfe 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 PagerIswriteable
15bff 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
15c00 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
15c01 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
15c02 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a (pBt->mutex) );.
15c03 20 20 2f 2a 6d 65 6d 73 65 74 28 26 64 61 74 61 /*memset(&data
15c04 5b 68 64 72 5d 2c 20 30 2c 20 70 42 74 2d 3e 75 [hdr], 0, pBt->u
15c05 73 61 62 6c 65 53 69 7a 65 20 2d 20 68 64 72 29 sableSize - hdr)
15c06 3b 2a 2f 0a 20 20 64 61 74 61 5b 68 64 72 5d 20 ;*/. data[hdr]
15c07 3d 20 28 63 68 61 72 29 66 6c 61 67 73 3b 0a 20 = (char)flags;.
15c08 20 66 69 72 73 74 20 3d 20 68 64 72 20 2b 20 38 first = hdr + 8
15c09 20 2b 20 34 2a 28 28 66 6c 61 67 73 26 50 54 46 + 4*((flags&PTF
15c0a 5f 4c 45 41 46 29 3d 3d 30 20 3f 31 3a 30 29 3b _LEAF)==0 ?1:0);
15c0b 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 74 61 5b . memset(&data[
15c0c 68 64 72 2b 31 5d 2c 20 30 2c 20 34 29 3b 0a 20 hdr+1], 0, 4);.
15c0d 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 3d 20 30 data[hdr+7] = 0
15c0e 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 ;. put2byte(&da
15c0f 74 61 5b 68 64 72 2b 35 5d 2c 20 70 42 74 2d 3e ta[hdr+5], pBt->
15c10 75 73 61 62 6c 65 53 69 7a 65 29 3b 0a 20 20 70 usableSize);. p
15c11 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20 70 42 Page->nFree = pB
15c12 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 t->usableSize -
15c13 66 69 72 73 74 3b 0a 20 20 64 65 63 6f 64 65 46 first;. decodeF
15c14 6c 61 67 73 28 70 50 61 67 65 2c 20 66 6c 61 67 lags(pPage, flag
15c15 73 29 3b 0a 20 20 70 50 61 67 65 2d 3e 68 64 72 s);. pPage->hdr
15c16 4f 66 66 73 65 74 20 3d 20 68 64 72 3b 0a 20 20 Offset = hdr;.
15c17 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 pPage->cellOffse
15c18 74 20 3d 20 66 69 72 73 74 3b 0a 20 20 70 50 61 t = first;. pPa
15c19 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 ge->nOverflow =
15c1a 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 0;. assert( pBt
15c1b 2d 3e 70 61 67 65 53 69 7a 65 3e 3d 35 31 32 20 ->pageSize>=512
15c1c 26 26 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 && pBt->pageSize
15c1d 3c 3d 33 32 37 36 38 20 29 3b 0a 20 20 70 50 61 <=32768 );. pPa
15c1e 67 65 2d 3e 6d 61 73 6b 50 61 67 65 20 3d 20 70 ge->maskPage = p
15c1f 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 31 Bt->pageSize - 1
15c20 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c ;. pPage->nCell
15c21 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 2d 3e 69 = 0;. pPage->i
15c22 73 49 6e 69 74 20 3d 20 31 3b 0a 7d 0a 0a 0a 2f sInit = 1;.}.../
15c23 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 44 *.** Convert a D
15c24 62 50 61 67 65 20 6f 62 74 61 69 6e 65 64 20 66 bPage obtained f
15c25 72 6f 6d 20 74 68 65 20 70 61 67 65 72 20 69 6e rom the pager in
15c26 74 6f 20 61 20 4d 65 6d 50 61 67 65 20 75 73 65 to a MemPage use
15c27 64 20 62 79 0a 2a 2a 20 74 68 65 20 62 74 72 65 d by.** the btre
15c28 65 20 6c 61 79 65 72 2e 0a 2a 2f 0a 73 74 61 74 e layer..*/.stat
15c29 69 63 20 4d 65 6d 50 61 67 65 20 2a 62 74 72 65 ic MemPage *btre
15c2a 65 50 61 67 65 46 72 6f 6d 44 62 50 61 67 65 28 ePageFromDbPage(
15c2b 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 2c DbPage *pDbPage,
15c2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 42 74 53 68 Pgno pgno, BtSh
15c2d 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 4d 65 ared *pBt){. Me
15c2e 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 28 mPage *pPage = (
15c2f 4d 65 6d 50 61 67 65 2a 29 73 71 6c 69 74 65 33 MemPage*)sqlite3
15c30 50 61 67 65 72 47 65 74 45 78 74 72 61 28 70 44 PagerGetExtra(pD
15c31 62 50 61 67 65 29 3b 0a 20 20 70 50 61 67 65 2d bPage);. pPage-
15c32 3e 61 44 61 74 61 20 3d 20 73 71 6c 69 74 65 33 >aData = sqlite3
15c33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 44 62 PagerGetData(pDb
15c34 50 61 67 65 29 3b 0a 20 20 70 50 61 67 65 2d 3e Page);. pPage->
15c35 70 44 62 50 61 67 65 20 3d 20 70 44 62 50 61 67 pDbPage = pDbPag
15c36 65 3b 0a 20 20 70 50 61 67 65 2d 3e 70 42 74 20 e;. pPage->pBt
15c37 3d 20 70 42 74 3b 0a 20 20 70 50 61 67 65 2d 3e = pBt;. pPage->
15c38 70 67 6e 6f 20 3d 20 70 67 6e 6f 3b 0a 20 20 70 pgno = pgno;. p
15c39 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 20 Page->hdrOffset
15c3a 3d 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d 31 = pPage->pgno==1
15c3b 20 3f 20 31 30 30 20 3a 20 30 3b 0a 20 20 72 65 ? 100 : 0;. re
15c3c 74 75 72 6e 20 70 50 61 67 65 3b 20 0a 7d 0a 0a turn pPage; .}..
15c3d 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 70 61 67 65 /*.** Get a page
15c3e 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72 2e from the pager.
15c3f 20 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 Initialize the
15c40 20 4d 65 6d 50 61 67 65 2e 70 42 74 20 61 6e 64 MemPage.pBt and
15c41 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 44 61 74 .** MemPage.aDat
15c42 61 20 65 6c 65 6d 65 6e 74 73 20 69 66 20 6e 65 a elements if ne
15c43 65 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 eded..**.** If t
15c44 68 65 20 6e 6f 43 6f 6e 74 65 6e 74 20 66 6c 61 he noContent fla
15c45 67 20 69 73 20 73 65 74 2c 20 69 74 20 6d 65 61 g is set, it mea
15c46 6e 73 20 74 68 61 74 20 77 65 20 64 6f 20 6e 6f ns that we do no
15c47 74 20 63 61 72 65 20 61 62 6f 75 74 0a 2a 2a 20 t care about.**
15c48 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 the content of t
15c49 68 65 20 70 61 67 65 20 61 74 20 74 68 69 73 20 he page at this
15c4a 74 69 6d 65 2e 20 20 53 6f 20 64 6f 20 6e 6f 74 time. So do not
15c4b 20 67 6f 20 74 6f 20 74 68 65 20 64 69 73 6b 0a go to the disk.
15c4c 2a 2a 20 74 6f 20 66 65 74 63 68 20 74 68 65 20 ** to fetch the
15c4d 63 6f 6e 74 65 6e 74 2e 20 20 4a 75 73 74 20 66 content. Just f
15c4e 69 6c 6c 20 69 6e 20 74 68 65 20 63 6f 6e 74 65 ill in the conte
15c4f 6e 74 20 77 69 74 68 20 7a 65 72 6f 73 20 66 6f nt with zeros fo
15c50 72 20 6e 6f 77 2e 0a 2a 2a 20 49 66 20 69 6e 20 r now..** If in
15c51 74 68 65 20 66 75 74 75 72 65 20 77 65 20 63 61 the future we ca
15c52 6c 6c 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 ll sqlite3PagerW
15c53 72 69 74 65 28 29 20 6f 6e 20 74 68 69 73 20 70 rite() on this p
15c54 61 67 65 2c 20 74 68 61 74 0a 2a 2a 20 6d 65 61 age, that.** mea
15c55 6e 73 20 77 65 20 68 61 76 65 20 73 74 61 72 74 ns we have start
15c56 65 64 20 74 6f 20 62 65 20 63 6f 6e 63 65 72 6e ed to be concern
15c57 65 64 20 61 62 6f 75 74 20 63 6f 6e 74 65 6e 74 ed about content
15c58 20 61 6e 64 20 74 68 65 20 64 69 73 6b 0a 2a 2a and the disk.**
15c59 20 72 65 61 64 20 73 68 6f 75 6c 64 20 6f 63 63 read should occ
15c5a 75 72 20 61 74 20 74 68 61 74 20 70 6f 69 6e 74 ur at that point
15c5b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
15c5c 62 74 72 65 65 47 65 74 50 61 67 65 28 0a 20 20 btreeGetPage(.
15c5d 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 20 BtShared *pBt,
15c5e 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65 /* The btre
15c5f 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f e */. Pgno pgno
15c60 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e , /* N
15c61 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61 67 umber of the pag
15c62 65 20 74 6f 20 66 65 74 63 68 20 2a 2f 0a 20 20 e to fetch */.
15c63 4d 65 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 65 MemPage **ppPage
15c64 2c 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 74 , /* Return t
15c65 68 65 20 70 61 67 65 20 69 6e 20 74 68 69 73 20 he page in this
15c66 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20 20 69 parameter */. i
15c67 6e 74 20 6e 6f 43 6f 6e 74 65 6e 74 20 20 20 20 nt noContent
15c68 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 6c 6f /* Do not lo
15c69 61 64 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 ad page content
15c6a 69 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 if true */.){.
15c6b 69 6e 74 20 72 63 3b 0a 20 20 44 62 50 61 67 65 int rc;. DbPage
15c6c 20 2a 70 44 62 50 61 67 65 3b 0a 0a 20 20 61 73 *pDbPage;.. as
15c6d 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
15c6e 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 tex_held(pBt->mu
15c6f 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 tex) );. rc = s
15c70 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 75 69 qlite3PagerAcqui
15c71 72 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 re(pBt->pPager,
15c72 70 67 6e 6f 2c 20 28 44 62 50 61 67 65 2a 2a 29 pgno, (DbPage**)
15c73 26 70 44 62 50 61 67 65 2c 20 6e 6f 43 6f 6e 74 &pDbPage, noCont
15c74 65 6e 74 29 3b 0a 20 20 69 66 28 20 72 63 20 29 ent);. if( rc )
15c75 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 2a 70 return rc;. *p
15c76 70 50 61 67 65 20 3d 20 62 74 72 65 65 50 61 67 pPage = btreePag
15c77 65 46 72 6f 6d 44 62 50 61 67 65 28 70 44 62 50 eFromDbPage(pDbP
15c78 61 67 65 2c 20 70 67 6e 6f 2c 20 70 42 74 29 3b age, pgno, pBt);
15c79 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
15c7a 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 _OK;.}../*.** Re
15c7b 74 72 69 65 76 65 20 61 20 70 61 67 65 20 66 72 trieve a page fr
15c7c 6f 6d 20 74 68 65 20 70 61 67 65 72 20 63 61 63 om the pager cac
15c7d 68 65 2e 20 49 66 20 74 68 65 20 72 65 71 75 65 he. If the reque
15c7e 73 74 65 64 20 70 61 67 65 20 69 73 20 6e 6f 74 sted page is not
15c7f 0a 2a 2a 20 61 6c 72 65 61 64 79 20 69 6e 20 74 .** already in t
15c80 68 65 20 70 61 67 65 72 20 63 61 63 68 65 20 72 he pager cache r
15c81 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 49 6e 69 74 eturn NULL. Init
15c82 69 61 6c 69 7a 65 20 74 68 65 20 4d 65 6d 50 61 ialize the MemPa
15c83 67 65 2e 70 42 74 20 61 6e 64 0a 2a 2a 20 4d 65 ge.pBt and.** Me
15c84 6d 50 61 67 65 2e 61 44 61 74 61 20 65 6c 65 6d mPage.aData elem
15c85 65 6e 74 73 20 69 66 20 6e 65 65 64 65 64 2e 0a ents if needed..
15c86 2a 2f 0a 73 74 61 74 69 63 20 4d 65 6d 50 61 67 */.static MemPag
15c87 65 20 2a 62 74 72 65 65 50 61 67 65 4c 6f 6f 6b e *btreePageLook
15c88 75 70 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 up(BtShared *pBt
15c89 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 , Pgno pgno){.
15c8a 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b DbPage *pDbPage;
15c8b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
15c8c 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 e3_mutex_held(pB
15c8d 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 t->mutex) );. p
15c8e 44 62 50 61 67 65 20 3d 20 73 71 6c 69 74 65 33 DbPage = sqlite3
15c8f 50 61 67 65 72 4c 6f 6f 6b 75 70 28 70 42 74 2d PagerLookup(pBt-
15c90 3e 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 3b 0a >pPager, pgno);.
15c91 20 20 69 66 28 20 70 44 62 50 61 67 65 20 29 7b if( pDbPage ){
15c92 0a 20 20 20 20 72 65 74 75 72 6e 20 62 74 72 65 . return btre
15c93 65 50 61 67 65 46 72 6f 6d 44 62 50 61 67 65 28 ePageFromDbPage(
15c94 70 44 62 50 61 67 65 2c 20 70 67 6e 6f 2c 20 70 pDbPage, pgno, p
15c95 42 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 Bt);. }. retur
15c96 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 n 0;.}../*.** Re
15c97 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 turn the size of
15c98 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
15c99 6c 65 20 69 6e 20 70 61 67 65 73 2e 20 49 66 20 le in pages. If
15c9a 74 68 65 72 65 20 69 73 20 61 6e 79 20 6b 69 6e there is any kin
15c9b 64 20 6f 66 0a 2a 2a 20 65 72 72 6f 72 2c 20 72 d of.** error, r
15c9c 65 74 75 72 6e 20 28 28 75 6e 73 69 67 6e 65 64 eturn ((unsigned
15c9d 20 69 6e 74 29 2d 31 29 2e 0a 2a 2f 0a 73 74 61 int)-1)..*/.sta
15c9e 74 69 63 20 50 67 6e 6f 20 70 61 67 65 72 50 61 tic Pgno pagerPa
15c9f 67 65 63 6f 75 6e 74 28 42 74 53 68 61 72 65 64 gecount(BtShared
15ca0 20 2a 70 42 74 29 7b 0a 20 20 69 6e 74 20 6e 50 *pBt){. int nP
15ca1 61 67 65 20 3d 20 2d 31 3b 0a 20 20 69 6e 74 20 age = -1;. int
15ca2 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 rc;. assert( pB
15ca3 74 2d 3e 70 50 61 67 65 31 20 29 3b 0a 20 20 72 t->pPage1 );. r
15ca4 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
15ca5 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 Pagecount(pBt->p
15ca6 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a Pager, &nPage);.
15ca7 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 assert( rc==SQ
15ca8 4c 49 54 45 5f 4f 4b 20 7c 7c 20 6e 50 61 67 65 LITE_OK || nPage
15ca9 3d 3d 2d 31 20 29 3b 0a 20 20 72 65 74 75 72 6e ==-1 );. return
15caa 20 28 50 67 6e 6f 29 6e 50 61 67 65 3b 0a 7d 0a (Pgno)nPage;.}.
15cab 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 70 61 67 ./*.** Get a pag
15cac 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72 e from the pager
15cad 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 and initialize
15cae 69 74 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e it. This routin
15caf 65 20 69 73 20 6a 75 73 74 20 61 0a 2a 2a 20 63 e is just a.** c
15cb0 6f 6e 76 65 6e 69 65 6e 63 65 20 77 72 61 70 70 onvenience wrapp
15cb1 65 72 20 61 72 6f 75 6e 64 20 73 65 70 61 72 61 er around separa
15cb2 74 65 20 63 61 6c 6c 73 20 74 6f 20 62 74 72 65 te calls to btre
15cb3 65 47 65 74 50 61 67 65 28 29 20 61 6e 64 20 0a eGetPage() and .
15cb4 2a 2a 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 ** btreeInitPage
15cb5 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 ()..**.** If an
15cb6 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 error occurs, th
15cb7 65 6e 20 74 68 65 20 76 61 6c 75 65 20 2a 70 70 en the value *pp
15cb8 50 61 67 65 20 69 73 20 73 65 74 20 74 6f 20 69 Page is set to i
15cb9 73 20 75 6e 64 65 66 69 6e 65 64 2e 20 49 74 0a s undefined. It.
15cba 2a 2a 20 6d 61 79 20 72 65 6d 61 69 6e 20 75 6e ** may remain un
15cbb 63 68 61 6e 67 65 64 2c 20 6f 72 20 69 74 20 6d changed, or it m
15cbc 61 79 20 62 65 20 73 65 74 20 74 6f 20 61 6e 20 ay be set to an
15cbd 69 6e 76 61 6c 69 64 20 76 61 6c 75 65 2e 0a 2a invalid value..*
15cbe 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 /.static int get
15cbf 41 6e 64 49 6e 69 74 50 61 67 65 28 0a 20 20 42 AndInitPage(. B
15cc0 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 20 20 tShared *pBt,
15cc1 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 /* The da
15cc2 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 tabase file */.
15cc3 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 Pgno pgno,
15cc4 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
15cc5 6f 66 20 74 68 65 20 70 61 67 65 20 74 6f 20 67 of the page to g
15cc6 65 74 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 et */. MemPage
15cc7 2a 2a 70 70 50 61 67 65 20 20 20 20 20 2f 2a 20 **ppPage /*
15cc8 57 72 69 74 65 20 74 68 65 20 70 61 67 65 20 70 Write the page p
15cc9 6f 69 6e 74 65 72 20 68 65 72 65 20 2a 2f 0a 29 ointer here */.)
15cca 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 54 45 {. int rc;. TE
15ccb 53 54 4f 4e 4c 59 28 20 50 67 6e 6f 20 69 4c 61 STONLY( Pgno iLa
15ccc 73 74 50 67 20 3d 20 70 61 67 65 72 50 61 67 65 stPg = pagerPage
15ccd 63 6f 75 6e 74 28 70 42 74 29 3b 20 29 0a 20 20 count(pBt); ).
15cce 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
15ccf 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e mutex_held(pBt->
15cd0 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 72 63 20 mutex) );.. rc
15cd1 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 = btreeGetPage(p
15cd2 42 74 2c 20 70 67 6e 6f 2c 20 70 70 50 61 67 65 Bt, pgno, ppPage
15cd3 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d , 0);. if( rc==
15cd4 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
15cd5 20 72 63 20 3d 20 62 74 72 65 65 49 6e 69 74 50 rc = btreeInitP
15cd6 61 67 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 age(*ppPage);.
15cd7 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
15cd8 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 6c _OK ){. rel
15cd9 65 61 73 65 50 61 67 65 28 2a 70 70 50 61 67 65 easePage(*ppPage
15cda 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 );. }. }..
15cdb 2f 2a 20 49 66 20 74 68 65 20 72 65 71 75 65 73 /* If the reques
15cdc 74 65 64 20 70 61 67 65 20 6e 75 6d 62 65 72 20 ted page number
15cdd 77 61 73 20 65 69 74 68 65 72 20 30 20 6f 72 20 was either 0 or
15cde 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 greater than the
15cdf 20 70 61 67 65 0a 20 20 2a 2a 20 6e 75 6d 62 65 page. ** numbe
15ce0 72 20 6f 66 20 74 68 65 20 6c 61 73 74 20 70 61 r of the last pa
15ce1 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 ge in the databa
15ce2 73 65 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f se, this functio
15ce3 6e 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 0a n should return.
15ce4 20 20 2a 2a 20 53 51 4c 49 54 45 5f 43 4f 52 52 ** SQLITE_CORR
15ce5 55 50 54 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 UPT or some othe
15ce6 72 20 65 72 72 6f 72 20 28 69 2e 65 2e 20 53 51 r error (i.e. SQ
15ce7 4c 49 54 45 5f 46 55 4c 4c 29 2e 20 43 68 65 63 LITE_FULL). Chec
15ce8 6b 20 74 68 61 74 20 74 68 69 73 0a 20 20 2a 2a k that this. **
15ce9 20 69 73 20 74 68 65 20 63 61 73 65 2e 20 20 2a is the case. *
15cea 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70 67 6e /. assert( (pgn
15ceb 6f 3e 30 20 26 26 20 70 67 6e 6f 3c 3d 69 4c 61 o>0 && pgno<=iLa
15cec 73 74 50 67 29 20 7c 7c 20 72 63 21 3d 53 51 4c stPg) || rc!=SQL
15ced 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 74 65 73 74 ITE_OK );. test
15cee 63 61 73 65 28 20 70 67 6e 6f 3d 3d 30 20 29 3b case( pgno==0 );
15cef 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 67 6e . testcase( pgn
15cf0 6f 3d 3d 69 4c 61 73 74 50 67 20 29 3b 0a 0a 20 o==iLastPg );..
15cf1 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
15cf2 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 20 4d *.** Release a M
15cf3 65 6d 50 61 67 65 2e 20 20 54 68 69 73 20 73 68 emPage. This sh
15cf4 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 6f ould be called o
15cf5 6e 63 65 20 66 6f 72 20 65 61 63 68 20 70 72 69 nce for each pri
15cf6 6f 72 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 62 74 or.** call to bt
15cf7 72 65 65 47 65 74 50 61 67 65 2e 0a 2a 2f 0a 73 reeGetPage..*/.s
15cf8 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c 65 61 tatic void relea
15cf9 73 65 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a sePage(MemPage *
15cfa 70 50 61 67 65 29 7b 0a 20 20 69 66 28 20 70 50 pPage){. if( pP
15cfb 61 67 65 20 29 7b 0a 20 20 20 20 61 73 73 65 72 age ){. asser
15cfc 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 t( pPage->nOverf
15cfd 6c 6f 77 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 low==0 || sqlite
15cfe 33 50 61 67 65 72 50 61 67 65 52 65 66 63 6f 75 3PagerPageRefcou
15cff 6e 74 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 nt(pPage->pDbPag
15d00 65 29 3e 31 20 29 3b 0a 20 20 20 20 61 73 73 65 e)>1 );. asse
15d01 72 74 28 20 70 50 61 67 65 2d 3e 61 44 61 74 61 rt( pPage->aData
15d02 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
15d03 70 50 61 67 65 2d 3e 70 42 74 20 29 3b 0a 20 20 pPage->pBt );.
15d04 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
15d05 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28 70 3PagerGetExtra(p
15d06 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 3d Page->pDbPage) =
15d07 3d 20 28 76 6f 69 64 2a 29 70 50 61 67 65 20 29 = (void*)pPage )
15d08 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 ;. assert( sq
15d09 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 lite3PagerGetDat
15d0a 61 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 a(pPage->pDbPage
15d0b 29 3d 3d 70 50 61 67 65 2d 3e 61 44 61 74 61 20 )==pPage->aData
15d0c 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 );. assert( s
15d0d 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
15d0e 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 d(pPage->pBt->mu
15d0f 74 65 78 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 tex) );. sqli
15d10 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 te3PagerUnref(pP
15d11 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 age->pDbPage);.
15d12 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 75 72 69 }.}../*.** Duri
15d13 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2c 20 77 ng a rollback, w
15d14 68 65 6e 20 74 68 65 20 70 61 67 65 72 20 72 65 hen the pager re
15d15 6c 6f 61 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f loads informatio
15d16 6e 20 69 6e 74 6f 20 74 68 65 20 63 61 63 68 65 n into the cache
15d17 0a 2a 2a 20 73 6f 20 74 68 61 74 20 74 68 65 20 .** so that the
15d18 63 61 63 68 65 20 69 73 20 72 65 73 74 6f 72 65 cache is restore
15d19 64 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 d to its origina
15d1a 6c 20 73 74 61 74 65 20 61 74 20 74 68 65 20 73 l state at the s
15d1b 74 61 72 74 20 6f 66 0a 2a 2a 20 74 68 65 20 74 tart of.** the t
15d1c 72 61 6e 73 61 63 74 69 6f 6e 2c 20 66 6f 72 20 ransaction, for
15d1d 65 61 63 68 20 70 61 67 65 20 72 65 73 74 6f 72 each page restor
15d1e 65 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 ed this routine
15d1f 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a is called..**.**
15d20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6e 65 This routine ne
15d21 65 64 73 20 74 6f 20 72 65 73 65 74 20 74 68 65 eds to reset the
15d22 20 65 78 74 72 61 20 64 61 74 61 20 73 65 63 74 extra data sect
15d23 69 6f 6e 20 61 74 20 74 68 65 20 65 6e 64 20 6f ion at the end o
15d24 66 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 74 6f f the.** page to
15d25 20 61 67 72 65 65 20 77 69 74 68 20 74 68 65 20 agree with the
15d26 72 65 73 74 6f 72 65 64 20 64 61 74 61 2e 0a 2a restored data..*
15d27 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 /.static void pa
15d28 67 65 52 65 69 6e 69 74 28 44 62 50 61 67 65 20 geReinit(DbPage
15d29 2a 70 44 61 74 61 29 7b 0a 20 20 4d 65 6d 50 61 *pData){. MemPa
15d2a 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 70 50 61 ge *pPage;. pPa
15d2b 67 65 20 3d 20 28 4d 65 6d 50 61 67 65 20 2a 29 ge = (MemPage *)
15d2c 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 sqlite3PagerGetE
15d2d 78 74 72 61 28 70 44 61 74 61 29 3b 0a 20 20 61 xtra(pData);. a
15d2e 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
15d2f 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28 gerPageRefcount(
15d30 70 44 61 74 61 29 3e 30 20 29 3b 0a 20 20 69 66 pData)>0 );. if
15d31 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 ( pPage->isInit
15d32 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 ){. assert( s
15d33 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
15d34 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 d(pPage->pBt->mu
15d35 74 65 78 29 20 29 3b 0a 20 20 20 20 70 50 61 67 tex) );. pPag
15d36 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 e->isInit = 0;.
15d37 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 50 61 if( sqlite3Pa
15d38 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28 gerPageRefcount(
15d39 70 44 61 74 61 29 3e 31 20 29 7b 0a 20 20 20 20 pData)>1 ){.
15d3a 20 20 2f 2a 20 70 50 61 67 65 20 6d 69 67 68 74 /* pPage might
15d3b 20 6e 6f 74 20 62 65 20 61 20 62 74 72 65 65 20 not be a btree
15d3c 70 61 67 65 3b 20 20 69 74 20 6d 69 67 68 74 20 page; it might
15d3d 62 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 be an overflow p
15d3e 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 6f 72 20 age. ** or
15d3f 70 74 72 6d 61 70 20 70 61 67 65 20 6f 72 20 61 ptrmap page or a
15d40 20 66 72 65 65 20 70 61 67 65 2e 20 20 49 6e 20 free page. In
15d41 74 68 6f 73 65 20 63 61 73 65 73 2c 20 74 68 65 those cases, the
15d42 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 20 20 20 following.
15d43 20 2a 2a 20 63 61 6c 6c 20 74 6f 20 62 74 72 65 ** call to btre
15d44 65 49 6e 69 74 50 61 67 65 28 29 20 77 69 6c 6c eInitPage() will
15d45 20 6c 69 6b 65 6c 79 20 72 65 74 75 72 6e 20 53 likely return S
15d46 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2e 0a 20 QLITE_CORRUPT..
15d47 20 20 20 20 20 2a 2a 20 42 75 74 20 6e 6f 20 68 ** But no h
15d48 61 72 6d 20 69 73 20 64 6f 6e 65 20 62 79 20 74 arm is done by t
15d49 68 69 73 2e 20 20 41 6e 64 20 69 74 20 69 73 20 his. And it is
15d4a 76 65 72 79 20 69 6d 70 6f 72 74 61 6e 74 20 74 very important t
15d4b 68 61 74 0a 20 20 20 20 20 20 2a 2a 20 62 74 72 hat. ** btr
15d4c 65 65 49 6e 69 74 50 61 67 65 28 29 20 62 65 20 eeInitPage() be
15d4d 63 61 6c 6c 65 64 20 6f 6e 20 65 76 65 72 79 20 called on every
15d4e 62 74 72 65 65 20 70 61 67 65 20 73 6f 20 77 65 btree page so we
15d4f 20 6d 61 6b 65 0a 20 20 20 20 20 20 2a 2a 20 74 make. ** t
15d50 68 65 20 63 61 6c 6c 20 66 6f 72 20 65 76 65 72 he call for ever
15d51 79 20 70 61 67 65 20 74 68 61 74 20 63 6f 6d 65 y page that come
15d52 73 20 69 6e 20 66 6f 72 20 72 65 2d 69 6e 69 74 s in for re-init
15d53 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 20 20 62 74 ing. */. bt
15d54 72 65 65 49 6e 69 74 50 61 67 65 28 70 50 61 67 reeInitPage(pPag
15d55 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a e);. }. }.}.
15d56 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 ./*.** Invoke th
15d57 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 66 e busy handler f
15d58 6f 72 20 61 20 62 74 72 65 65 2e 0a 2a 2f 0a 73 or a btree..*/.s
15d59 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 49 tatic int btreeI
15d5a 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 nvokeBusyHandler
15d5b 28 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 (void *pArg){.
15d5c 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
15d5d 28 42 74 53 68 61 72 65 64 2a 29 70 41 72 67 3b (BtShared*)pArg;
15d5e 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e . assert( pBt->
15d5f 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 db );. assert(
15d60 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
15d61 6c 64 28 70 42 74 2d 3e 64 62 2d 3e 6d 75 74 65 ld(pBt->db->mute
15d62 78 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 x) );. return s
15d63 71 6c 69 74 65 33 49 6e 76 6f 6b 65 42 75 73 79 qlite3InvokeBusy
15d64 48 61 6e 64 6c 65 72 28 26 70 42 74 2d 3e 64 62 Handler(&pBt->db
15d65 2d 3e 62 75 73 79 48 61 6e 64 6c 65 72 29 3b 0a ->busyHandler);.
15d66 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 }../*.** Open a
15d67 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a database file..*
15d68 2a 20 0a 2a 2a 20 7a 46 69 6c 65 6e 61 6d 65 20 * .** zFilename
15d69 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 is the name of t
15d6a 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
15d6b 2e 20 20 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20 . If zFilename
15d6c 69 73 20 4e 55 4c 4c 0a 2a 2a 20 61 20 6e 65 77 is NULL.** a new
15d6d 20 64 61 74 61 62 61 73 65 20 77 69 74 68 20 61 database with a
15d6e 20 72 61 6e 64 6f 6d 20 6e 61 6d 65 20 69 73 20 random name is
15d6f 63 72 65 61 74 65 64 2e 20 20 54 68 69 73 20 72 created. This r
15d70 61 6e 64 6f 6d 6c 79 20 6e 61 6d 65 64 0a 2a 2a andomly named.**
15d71 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 database file w
15d72 69 6c 6c 20 62 65 20 64 65 6c 65 74 65 64 20 77 ill be deleted w
15d73 68 65 6e 20 73 71 6c 69 74 65 33 42 74 72 65 65 hen sqlite3Btree
15d74 43 6c 6f 73 65 28 29 20 69 73 20 63 61 6c 6c 65 Close() is calle
15d75 64 2e 0a 2a 2a 20 49 66 20 7a 46 69 6c 65 6e 61 d..** If zFilena
15d76 6d 65 20 69 73 20 22 3a 6d 65 6d 6f 72 79 3a 22 me is ":memory:"
15d77 20 74 68 65 6e 20 61 6e 20 69 6e 2d 6d 65 6d 6f then an in-memo
15d78 72 79 20 64 61 74 61 62 61 73 65 20 69 73 20 63 ry database is c
15d79 72 65 61 74 65 64 0a 2a 2a 20 74 68 61 74 20 69 reated.** that i
15d7a 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 s automatically
15d7b 64 65 73 74 72 6f 79 65 64 20 77 68 65 6e 20 69 destroyed when i
15d7c 74 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a t is closed..**.
15d7d 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 ** If the databa
15d7e 73 65 20 69 73 20 61 6c 72 65 61 64 79 20 6f 70 se is already op
15d7f 65 6e 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 ened in the same
15d80 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
15d81 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 77 65 20 61 tion.** and we a
15d82 72 65 20 69 6e 20 73 68 61 72 65 64 20 63 61 63 re in shared cac
15d83 68 65 20 6d 6f 64 65 2c 20 74 68 65 6e 20 74 68 he mode, then th
15d84 65 20 6f 70 65 6e 20 77 69 6c 6c 20 66 61 69 6c e open will fail
15d85 20 77 69 74 68 20 61 6e 0a 2a 2a 20 53 51 4c 49 with an.** SQLI
15d86 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 65 72 TE_CONSTRAINT er
15d87 72 6f 72 2e 20 20 57 65 20 63 61 6e 6e 6f 74 20 ror. We cannot
15d88 61 6c 6c 6f 77 20 74 77 6f 20 6f 72 20 6d 6f 72 allow two or mor
15d89 65 20 42 74 53 68 61 72 65 64 0a 2a 2a 20 6f 62 e BtShared.** ob
15d8a 6a 65 63 74 73 20 69 6e 20 74 68 65 20 73 61 6d jects in the sam
15d8b 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
15d8c 63 74 69 6f 6e 20 73 69 6e 63 65 20 64 6f 69 6e ction since doin
15d8d 67 20 73 6f 20 77 69 6c 6c 20 6c 65 61 64 0a 2a g so will lead.*
15d8e 2a 20 74 6f 20 70 72 6f 62 6c 65 6d 73 20 77 69 * to problems wi
15d8f 74 68 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2f 0a 53 th locking..*/.S
15d90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
15d91 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4f 70 t sqlite3BtreeOp
15d92 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 en(. const char
15d93 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 2f 2a *zFilename, /*
15d94 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c Name of the fil
15d95 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 e containing the
15d96 20 42 54 72 65 65 20 64 61 74 61 62 61 73 65 20 BTree database
15d97 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 */. sqlite3 *db
15d98 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
15d99 41 73 73 6f 63 69 61 74 65 64 20 64 61 74 61 62 Associated datab
15d9a 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 ase handle */.
15d9b 42 74 72 65 65 20 2a 2a 70 70 42 74 72 65 65 2c Btree **ppBtree,
15d9c 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 /* Point
15d9d 65 72 20 74 6f 20 6e 65 77 20 42 74 72 65 65 20 er to new Btree
15d9e 6f 62 6a 65 63 74 20 77 72 69 74 74 65 6e 20 68 object written h
15d9f 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 ere */. int fla
15da0 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 gs,
15da1 20 2f 2a 20 4f 70 74 69 6f 6e 73 20 2a 2f 0a 20 /* Options */.
15da2 20 69 6e 74 20 76 66 73 46 6c 61 67 73 20 20 20 int vfsFlags
15da3 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 /* Flag
15da4 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 s passed through
15da5 20 74 6f 20 73 71 6c 69 74 65 33 5f 76 66 73 2e to sqlite3_vfs.
15da6 78 4f 70 65 6e 28 29 20 2a 2f 0a 29 7b 0a 20 20 xOpen() */.){.
15da7 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
15da8 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f s; /
15da9 2a 20 54 68 65 20 56 46 53 20 74 6f 20 75 73 65 * The VFS to use
15daa 20 66 6f 72 20 74 68 69 73 20 62 74 72 65 65 20 for this btree
15dab 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 */. BtShared *p
15dac 42 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 Bt = 0;
15dad 20 20 20 20 2f 2a 20 53 68 61 72 65 64 20 70 61 /* Shared pa
15dae 72 74 20 6f 66 20 62 74 72 65 65 20 73 74 72 75 rt of btree stru
15daf 63 74 75 72 65 20 2a 2f 0a 20 20 42 74 72 65 65 cture */. Btree
15db0 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 *p;
15db1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e /* Han
15db2 64 6c 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f dle to return */
15db3 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
15db4 20 2a 6d 75 74 65 78 4f 70 65 6e 20 3d 20 30 3b *mutexOpen = 0;
15db5 20 20 2f 2a 20 50 72 65 76 65 6e 74 73 20 61 20 /* Prevents a
15db6 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 2e 20 race condition.
15db7 54 69 63 6b 65 74 20 23 33 35 33 37 20 2a 2f 0a Ticket #3537 */.
15db8 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
15db9 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 E_OK;
15dba 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20 /* Result code
15dbb 66 72 6f 6d 20 74 68 69 73 20 66 75 6e 63 74 69 from this functi
15dbc 6f 6e 20 2a 2f 0a 20 20 75 38 20 6e 52 65 73 65 on */. u8 nRese
15dbd 72 76 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 rve;
15dbe 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 20 6f /* Byte o
15dbf 66 20 75 6e 75 73 65 64 20 73 70 61 63 65 20 6f f unused space o
15dc0 6e 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 n each page */.
15dc1 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 7a unsigned char z
15dc2 44 62 48 65 61 64 65 72 5b 31 30 30 5d 3b 20 20 DbHeader[100];
15dc3 2f 2a 20 44 61 74 61 62 61 73 65 20 68 65 61 64 /* Database head
15dc4 65 72 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 0a 20 er content */..
15dc5 20 2f 2a 20 53 65 74 20 74 68 65 20 76 61 72 69 /* Set the vari
15dc6 61 62 6c 65 20 69 73 4d 65 6d 64 62 20 74 6f 20 able isMemdb to
15dc7 74 72 75 65 20 66 6f 72 20 61 6e 20 69 6e 2d 6d true for an in-m
15dc8 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c 20 emory database,
15dc9 6f 72 20 0a 20 20 2a 2a 20 66 61 6c 73 65 20 66 or . ** false f
15dca 6f 72 20 61 20 66 69 6c 65 2d 62 61 73 65 64 20 or a file-based
15dcb 64 61 74 61 62 61 73 65 2e 20 54 68 69 73 20 73 database. This s
15dcc 79 6d 62 6f 6c 20 69 73 20 6f 6e 6c 79 20 72 65 ymbol is only re
15dcd 71 75 69 72 65 64 20 69 66 0a 20 20 2a 2a 20 65 quired if. ** e
15dce 69 74 68 65 72 20 6f 66 20 74 68 65 20 73 68 61 ither of the sha
15dcf 72 65 64 2d 64 61 74 61 20 6f 72 20 61 75 74 6f red-data or auto
15dd0 76 61 63 75 75 6d 20 66 65 61 74 75 72 65 73 20 vacuum features
15dd1 61 72 65 20 63 6f 6d 70 69 6c 65 64 20 0a 20 20 are compiled .
15dd2 2a 2a 20 69 6e 74 6f 20 74 68 65 20 6c 69 62 72 ** into the libr
15dd3 61 72 79 2e 0a 20 20 2a 2f 0a 23 69 66 20 21 64 ary.. */.#if !d
15dd4 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
15dd5 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 29 IT_SHARED_CACHE)
15dd6 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c || !defined(SQL
15dd7 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
15dd8 55 55 4d 29 0a 20 20 23 69 66 64 65 66 20 53 51 UUM). #ifdef SQ
15dd9 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 LITE_OMIT_MEMORY
15dda 44 42 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 DB. const int
15ddb 20 69 73 4d 65 6d 64 62 20 3d 20 30 3b 0a 20 20 isMemdb = 0;.
15ddc 23 65 6c 73 65 0a 20 20 20 20 63 6f 6e 73 74 20 #else. const
15ddd 69 6e 74 20 69 73 4d 65 6d 64 62 20 3d 20 7a 46 int isMemdb = zF
15dde 69 6c 65 6e 61 6d 65 20 26 26 20 21 73 74 72 63 ilename && !strc
15ddf 6d 70 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 22 3a mp(zFilename, ":
15de0 6d 65 6d 6f 72 79 3a 22 29 3b 0a 20 20 23 65 6e memory:");. #en
15de1 64 69 66 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73 dif.#endif.. as
15de2 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a 20 sert( db!=0 );.
15de3 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
15de4 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e _mutex_held(db->
15de5 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 70 56 66 mutex) );.. pVf
15de6 73 20 3d 20 64 62 2d 3e 70 56 66 73 3b 0a 20 20 s = db->pVfs;.
15de7 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f p = sqlite3Mallo
15de8 63 5a 65 72 6f 28 73 69 7a 65 6f 66 28 42 74 72 cZero(sizeof(Btr
15de9 65 65 29 29 3b 0a 20 20 69 66 28 20 21 70 20 29 ee));. if( !p )
15dea 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
15deb 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 ITE_NOMEM;. }.
15dec 20 70 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 54 52 p->inTrans = TR
15ded 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 70 2d 3e 64 ANS_NONE;. p->d
15dee 62 20 3d 20 64 62 3b 0a 23 69 66 6e 64 65 66 20 b = db;.#ifndef
15def 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
15df0 45 44 5f 43 41 43 48 45 0a 20 20 70 2d 3e 6c 6f ED_CACHE. p->lo
15df1 63 6b 2e 70 42 74 72 65 65 20 3d 20 70 3b 0a 20 ck.pBtree = p;.
15df2 20 70 2d 3e 6c 6f 63 6b 2e 69 54 61 62 6c 65 20 p->lock.iTable
15df3 3d 20 31 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 = 1;.#endif..#if
15df4 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
15df5 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
15df6 48 45 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 HE) && !defined(
15df7 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b SQLITE_OMIT_DISK
15df8 49 4f 29 0a 20 20 2f 2a 0a 20 20 2a 2a 20 49 66 IO). /*. ** If
15df9 20 74 68 69 73 20 42 74 72 65 65 20 69 73 20 61 this Btree is a
15dfa 20 63 61 6e 64 69 64 61 74 65 20 66 6f 72 20 73 candidate for s
15dfb 68 61 72 65 64 20 63 61 63 68 65 2c 20 74 72 79 hared cache, try
15dfc 20 74 6f 20 66 69 6e 64 20 61 6e 0a 20 20 2a 2a to find an. **
15dfd 20 65 78 69 73 74 69 6e 67 20 42 74 53 68 61 72 existing BtShar
15dfe 65 64 20 6f 62 6a 65 63 74 20 74 68 61 74 20 77 ed object that w
15dff 65 20 63 61 6e 20 73 68 61 72 65 20 77 69 74 68 e can share with
15e00 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 4d 65 . */. if( isMe
15e01 6d 64 62 3d 3d 30 20 26 26 20 7a 46 69 6c 65 6e mdb==0 && zFilen
15e02 61 6d 65 20 26 26 20 7a 46 69 6c 65 6e 61 6d 65 ame && zFilename
15e03 5b 30 5d 20 29 7b 0a 20 20 20 20 69 66 28 20 76 [0] ){. if( v
15e04 66 73 46 6c 61 67 73 20 26 20 53 51 4c 49 54 45 fsFlags & SQLITE
15e05 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41 43 48 _OPEN_SHAREDCACH
15e06 45 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e E ){. int n
15e07 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 3d 20 70 FullPathname = p
15e08 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b Vfs->mxPathname+
15e09 31 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 1;. char *z
15e0a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 3d 20 73 FullPathname = s
15e0b 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 46 75 qlite3Malloc(nFu
15e0c 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 llPathname);.
15e0d 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
15e0e 20 2a 6d 75 74 65 78 53 68 61 72 65 64 3b 0a 20 *mutexShared;.
15e0f 20 20 20 20 20 70 2d 3e 73 68 61 72 61 62 6c 65 p->sharable
15e10 20 3d 20 31 3b 0a 20 20 20 20 20 20 69 66 28 20 = 1;. if(
15e11 21 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 29 !zFullPathname )
15e12 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
15e13 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 20 20 20 3_free(p);.
15e14 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
15e15 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a _NOMEM;. }.
15e16 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 46 sqlite3OsF
15e17 75 6c 6c 50 61 74 68 6e 61 6d 65 28 70 56 66 73 ullPathname(pVfs
15e18 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 46 75 , zFilename, nFu
15e19 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 7a 46 75 6c llPathname, zFul
15e1a 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 lPathname);.
15e1b 20 20 6d 75 74 65 78 4f 70 65 6e 20 3d 20 73 71 mutexOpen = sq
15e1c 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 lite3MutexAlloc(
15e1d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
15e1e 54 49 43 5f 4f 50 45 4e 29 3b 0a 20 20 20 20 20 TIC_OPEN);.
15e1f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
15e20 6e 74 65 72 28 6d 75 74 65 78 4f 70 65 6e 29 3b nter(mutexOpen);
15e21 0a 20 20 20 20 20 20 6d 75 74 65 78 53 68 61 72 . mutexShar
15e22 65 64 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 ed = sqlite3Mute
15e23 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 xAlloc(SQLITE_MU
15e24 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 TEX_STATIC_MASTE
15e25 52 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 R);. sqlite
15e26 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 3_mutex_enter(mu
15e27 74 65 78 53 68 61 72 65 64 29 3b 0a 20 20 20 20 texShared);.
15e28 20 20 66 6f 72 28 70 42 74 3d 47 4c 4f 42 41 4c for(pBt=GLOBAL
15e29 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 (BtShared*,sqlit
15e2a 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 e3SharedCacheLis
15e2b 74 29 3b 20 70 42 74 3b 20 70 42 74 3d 70 42 74 t); pBt; pBt=pBt
15e2c 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 ->pNext){.
15e2d 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 6e assert( pBt->n
15e2e 52 65 66 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 Ref>0 );.
15e2f 20 69 66 28 20 30 3d 3d 73 74 72 63 6d 70 28 7a if( 0==strcmp(z
15e30 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 73 71 FullPathname, sq
15e31 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 6e 61 lite3PagerFilena
15e32 6d 65 28 70 42 74 2d 3e 70 50 61 67 65 72 29 29 me(pBt->pPager))
15e33 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
15e34 20 20 26 26 20 73 71 6c 69 74 65 33 50 61 67 65 && sqlite3Page
15e35 72 56 66 73 28 70 42 74 2d 3e 70 50 61 67 65 72 rVfs(pBt->pPager
15e36 29 3d 3d 70 56 66 73 20 29 7b 0a 20 20 20 20 20 )==pVfs ){.
15e37 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 int iDb;.
15e38 20 20 20 20 20 20 20 20 66 6f 72 28 69 44 62 3d for(iDb=
15e39 64 62 2d 3e 6e 44 62 2d 31 3b 20 69 44 62 3e 3d db->nDb-1; iDb>=
15e3a 30 3b 20 69 44 62 2d 2d 29 7b 0a 20 20 20 20 20 0; iDb--){.
15e3b 20 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 45 Btree *pE
15e3c 78 69 73 74 69 6e 67 20 3d 20 64 62 2d 3e 61 44 xisting = db->aD
15e3d 62 5b 69 44 62 5d 2e 70 42 74 3b 0a 20 20 20 20 b[iDb].pBt;.
15e3e 20 20 20 20 20 20 20 20 69 66 28 20 70 45 78 69 if( pExi
15e3f 73 74 69 6e 67 20 26 26 20 70 45 78 69 73 74 69 sting && pExisti
15e40 6e 67 2d 3e 70 42 74 3d 3d 70 42 74 20 29 7b 0a ng->pBt==pBt ){.
15e41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 sq
15e42 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
15e43 65 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b 0a e(mutexShared);.
15e44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 sq
15e45 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
15e46 65 28 6d 75 74 65 78 4f 70 65 6e 29 3b 0a 20 20 e(mutexOpen);.
15e47 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 sqli
15e48 74 65 33 5f 66 72 65 65 28 7a 46 75 6c 6c 50 61 te3_free(zFullPa
15e49 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 thname);.
15e4a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 sqlite3_f
15e4b 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 ree(p);.
15e4c 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
15e4d 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 3b 0a ITE_CONSTRAINT;.
15e4e 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
15e4f 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
15e50 20 20 20 20 70 2d 3e 70 42 74 20 3d 20 70 42 74 p->pBt = pBt
15e51 3b 0a 20 20 20 20 20 20 20 20 20 20 70 42 74 2d ;. pBt-
15e52 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 20 20 20 >nRef++;.
15e53 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
15e54 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
15e55 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
15e56 6c 65 61 76 65 28 6d 75 74 65 78 53 68 61 72 65 leave(mutexShare
15e57 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 d);. sqlite
15e58 33 5f 66 72 65 65 28 7a 46 75 6c 6c 50 61 74 68 3_free(zFullPath
15e59 6e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 23 69 66 name);. }.#if
15e5a 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
15e5b 0a 20 20 20 20 65 6c 73 65 7b 0a 20 20 20 20 20 . else{.
15e5c 20 2f 2a 20 49 6e 20 64 65 62 75 67 20 6d 6f 64 /* In debug mod
15e5d 65 2c 20 77 65 20 6d 61 72 6b 20 61 6c 6c 20 70 e, we mark all p
15e5e 65 72 73 69 73 74 65 6e 74 20 64 61 74 61 62 61 ersistent databa
15e5f 73 65 73 20 61 73 20 73 68 61 72 61 62 6c 65 0a ses as sharable.
15e60 20 20 20 20 20 20 2a 2a 20 65 76 65 6e 20 77 68 ** even wh
15e61 65 6e 20 74 68 65 79 20 61 72 65 20 6e 6f 74 2e en they are not.
15e62 20 20 54 68 69 73 20 65 78 65 72 63 69 73 65 73 This exercises
15e63 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 64 the locking cod
15e64 65 20 61 6e 64 0a 20 20 20 20 20 20 2a 2a 20 67 e and. ** g
15e65 69 76 65 73 20 6d 6f 72 65 20 6f 70 70 6f 72 74 ives more opport
15e66 75 6e 69 74 79 20 66 6f 72 20 61 73 73 65 72 74 unity for assert
15e67 73 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f s(sqlite3_mutex_
15e68 68 65 6c 64 28 29 29 0a 20 20 20 20 20 20 2a 2a held()). **
15e69 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 66 statements to f
15e6a 69 6e 64 20 6c 6f 63 6b 69 6e 67 20 70 72 6f 62 ind locking prob
15e6b 6c 65 6d 73 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 lems.. */.
15e6c 20 20 20 20 20 70 2d 3e 73 68 61 72 61 62 6c 65 p->sharable
15e6d 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 = 1;. }.#end
15e6e 69 66 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 if. }.#endif.
15e6f 69 66 28 20 70 42 74 3d 3d 30 20 29 7b 0a 20 20 if( pBt==0 ){.
15e70 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 /*. ** The
15e71 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74 following assert
15e72 73 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 s make sure that
15e73 20 73 74 72 75 63 74 75 72 65 73 20 75 73 65 64 structures used
15e74 20 62 79 20 74 68 65 20 62 74 72 65 65 20 61 72 by the btree ar
15e75 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 72 69 67 e. ** the rig
15e76 68 74 20 73 69 7a 65 2e 20 20 54 68 69 73 20 69 ht size. This i
15e77 73 20 74 6f 20 67 75 61 72 64 20 61 67 61 69 6e s to guard again
15e78 73 74 20 73 69 7a 65 20 63 68 61 6e 67 65 73 20 st size changes
15e79 74 68 61 74 20 72 65 73 75 6c 74 0a 20 20 20 20 that result.
15e7a 2a 2a 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e ** when compilin
15e7b 67 20 6f 6e 20 61 20 64 69 66 66 65 72 65 6e 74 g on a different
15e7c 20 61 72 63 68 69 74 65 63 74 75 72 65 2e 0a 20 architecture..
15e7d 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 */. assert
15e7e 28 20 73 69 7a 65 6f 66 28 69 36 34 29 3d 3d 38 ( sizeof(i64)==8
15e7f 20 7c 7c 20 73 69 7a 65 6f 66 28 69 36 34 29 3d || sizeof(i64)=
15e80 3d 34 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 =4 );. assert
15e81 28 20 73 69 7a 65 6f 66 28 75 36 34 29 3d 3d 38 ( sizeof(u64)==8
15e82 20 7c 7c 20 73 69 7a 65 6f 66 28 75 36 34 29 3d || sizeof(u64)=
15e83 3d 34 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 =4 );. assert
15e84 28 20 73 69 7a 65 6f 66 28 75 33 32 29 3d 3d 34 ( sizeof(u32)==4
15e85 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
15e86 73 69 7a 65 6f 66 28 75 31 36 29 3d 3d 32 20 29 sizeof(u16)==2 )
15e87 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 69 ;. assert( si
15e88 7a 65 6f 66 28 50 67 6e 6f 29 3d 3d 34 20 29 3b zeof(Pgno)==4 );
15e89 0a 20 20 0a 20 20 20 20 70 42 74 20 3d 20 73 71 . . pBt = sq
15e8a 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 lite3MallocZero(
15e8b 20 73 69 7a 65 6f 66 28 2a 70 42 74 29 20 29 3b sizeof(*pBt) );
15e8c 0a 20 20 20 20 69 66 28 20 70 42 74 3d 3d 30 20 . if( pBt==0
15e8d 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 ){. rc = SQ
15e8e 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 LITE_NOMEM;.
15e8f 20 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f 70 65 goto btree_ope
15e90 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 n_out;. }.
15e91 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
15e92 65 72 4f 70 65 6e 28 70 56 66 73 2c 20 26 70 42 erOpen(pVfs, &pB
15e93 74 2d 3e 70 50 61 67 65 72 2c 20 7a 46 69 6c 65 t->pPager, zFile
15e94 6e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20 name,.
15e95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15e96 45 58 54 52 41 5f 53 49 5a 45 2c 20 66 6c 61 67 EXTRA_SIZE, flag
15e97 73 2c 20 76 66 73 46 6c 61 67 73 2c 20 70 61 67 s, vfsFlags, pag
15e98 65 52 65 69 6e 69 74 29 3b 0a 20 20 20 20 69 66 eReinit);. if
15e99 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
15e9a 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 ){. rc = sq
15e9b 6c 69 74 65 33 50 61 67 65 72 52 65 61 64 46 69 lite3PagerReadFi
15e9c 6c 65 68 65 61 64 65 72 28 70 42 74 2d 3e 70 50 leheader(pBt->pP
15e9d 61 67 65 72 2c 73 69 7a 65 6f 66 28 7a 44 62 48 ager,sizeof(zDbH
15e9e 65 61 64 65 72 29 2c 7a 44 62 48 65 61 64 65 72 eader),zDbHeader
15e9f 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 );. }. if(
15ea0 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
15ea1 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 74 72 {. goto btr
15ea2 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 ee_open_out;.
15ea3 20 7d 0a 20 20 20 20 70 42 74 2d 3e 64 62 20 3d }. pBt->db =
15ea4 20 64 62 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 db;. sqlite3
15ea5 50 61 67 65 72 53 65 74 42 75 73 79 68 61 6e 64 PagerSetBusyhand
15ea6 6c 65 72 28 70 42 74 2d 3e 70 50 61 67 65 72 2c ler(pBt->pPager,
15ea7 20 62 74 72 65 65 49 6e 76 6f 6b 65 42 75 73 79 btreeInvokeBusy
15ea8 48 61 6e 64 6c 65 72 2c 20 70 42 74 29 3b 0a 20 Handler, pBt);.
15ea9 20 20 20 70 2d 3e 70 42 74 20 3d 20 70 42 74 3b p->pBt = pBt;
15eaa 0a 20 20 0a 20 20 20 20 70 42 74 2d 3e 70 43 75 . . pBt->pCu
15eab 72 73 6f 72 20 3d 20 30 3b 0a 20 20 20 20 70 42 rsor = 0;. pB
15eac 74 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 t->pPage1 = 0;.
15ead 20 20 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 pBt->readOnly
15eae 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 = sqlite3PagerI
15eaf 73 72 65 61 64 6f 6e 6c 79 28 70 42 74 2d 3e 70 sreadonly(pBt->p
15eb0 50 61 67 65 72 29 3b 0a 20 20 20 20 70 42 74 2d Pager);. pBt-
15eb1 3e 70 61 67 65 53 69 7a 65 20 3d 20 67 65 74 32 >pageSize = get2
15eb2 62 79 74 65 28 26 7a 44 62 48 65 61 64 65 72 5b byte(&zDbHeader[
15eb3 31 36 5d 29 3b 0a 20 20 20 20 69 66 28 20 70 42 16]);. if( pB
15eb4 74 2d 3e 70 61 67 65 53 69 7a 65 3c 35 31 32 20 t->pageSize<512
15eb5 7c 7c 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 || pBt->pageSize
15eb6 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 >SQLITE_MAX_PAGE
15eb7 5f 53 49 5a 45 0a 20 20 20 20 20 20 20 20 20 7c _SIZE. |
15eb8 7c 20 28 28 70 42 74 2d 3e 70 61 67 65 53 69 7a | ((pBt->pageSiz
15eb9 65 2d 31 29 26 70 42 74 2d 3e 70 61 67 65 53 69 e-1)&pBt->pageSi
15eba 7a 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 ze)!=0 ){.
15ebb 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 pBt->pageSize =
15ebc 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 0;.#ifndef SQLIT
15ebd 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
15ebe 4d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 M. /* If th
15ebf 65 20 6d 61 67 69 63 20 6e 61 6d 65 20 22 3a 6d e magic name ":m
15ec0 65 6d 6f 72 79 3a 22 20 77 69 6c 6c 20 63 72 65 emory:" will cre
15ec1 61 74 65 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 ate an in-memory
15ec2 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 0a database, then.
15ec3 20 20 20 20 20 20 2a 2a 20 6c 65 61 76 65 20 74 ** leave t
15ec4 68 65 20 61 75 74 6f 56 61 63 75 75 6d 20 6d 6f he autoVacuum mo
15ec5 64 65 20 61 74 20 30 20 28 64 6f 20 6e 6f 74 20 de at 0 (do not
15ec6 61 75 74 6f 2d 76 61 63 75 75 6d 29 2c 20 65 76 auto-vacuum), ev
15ec7 65 6e 20 69 66 0a 20 20 20 20 20 20 2a 2a 20 53 en if. ** S
15ec8 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 QLITE_DEFAULT_AU
15ec9 54 4f 56 41 43 55 55 4d 20 69 73 20 74 72 75 65 TOVACUUM is true
15eca 2e 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 . On the other h
15ecb 61 6e 64 2c 20 69 66 0a 20 20 20 20 20 20 2a 2a and, if. **
15ecc 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d SQLITE_OMIT_MEM
15ecd 4f 52 59 44 42 20 68 61 73 20 62 65 65 6e 20 64 ORYDB has been d
15ece 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 22 3a 6d efined, then ":m
15ecf 65 6d 6f 72 79 3a 22 20 69 73 20 6a 75 73 74 20 emory:" is just
15ed0 61 0a 20 20 20 20 20 20 2a 2a 20 72 65 67 75 6c a. ** regul
15ed1 61 72 20 66 69 6c 65 2d 6e 61 6d 65 2e 20 49 6e ar file-name. In
15ed2 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 61 this case the a
15ed3 75 74 6f 2d 76 61 63 75 75 6d 20 61 70 70 6c 69 uto-vacuum appli
15ed4 65 73 20 61 73 20 70 65 72 20 6e 6f 72 6d 61 6c es as per normal
15ed5 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
15ed6 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 20 26 if( zFilename &
15ed7 26 20 21 69 73 4d 65 6d 64 62 20 29 7b 0a 20 20 & !isMemdb ){.
15ed8 20 20 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56 pBt->autoV
15ed9 61 63 75 75 6d 20 3d 20 28 53 51 4c 49 54 45 5f acuum = (SQLITE_
15eda 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 DEFAULT_AUTOVACU
15edb 55 4d 20 3f 20 31 20 3a 20 30 29 3b 0a 20 20 20 UM ? 1 : 0);.
15edc 20 20 20 20 20 70 42 74 2d 3e 69 6e 63 72 56 61 pBt->incrVa
15edd 63 75 75 6d 20 3d 20 28 53 51 4c 49 54 45 5f 44 cuum = (SQLITE_D
15ede 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 EFAULT_AUTOVACUU
15edf 4d 3d 3d 32 20 3f 20 31 20 3a 20 30 29 3b 0a 20 M==2 ? 1 : 0);.
15ee0 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 }.#endif.
15ee1 20 20 20 20 6e 52 65 73 65 72 76 65 20 3d 20 30 nReserve = 0
15ee2 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
15ee3 20 20 20 6e 52 65 73 65 72 76 65 20 3d 20 7a 44 nReserve = zD
15ee4 62 48 65 61 64 65 72 5b 32 30 5d 3b 0a 20 20 20 bHeader[20];.
15ee5 20 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 pBt->pageSize
15ee6 46 69 78 65 64 20 3d 20 31 3b 0a 23 69 66 6e 64 Fixed = 1;.#ifnd
15ee7 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
15ee8 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 UTOVACUUM.
15ee9 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 pBt->autoVacuum
15eea 3d 20 28 67 65 74 34 62 79 74 65 28 26 7a 44 62 = (get4byte(&zDb
15eeb 48 65 61 64 65 72 5b 33 36 20 2b 20 34 2a 34 5d Header[36 + 4*4]
15eec 29 3f 31 3a 30 29 3b 0a 20 20 20 20 20 20 70 42 )?1:0);. pB
15eed 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 t->incrVacuum =
15eee 28 67 65 74 34 62 79 74 65 28 26 7a 44 62 48 65 (get4byte(&zDbHe
15eef 61 64 65 72 5b 33 36 20 2b 20 37 2a 34 5d 29 3f ader[36 + 7*4])?
15ef0 31 3a 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 1:0);.#endif.
15ef1 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 }. rc = sqli
15ef2 74 65 33 50 61 67 65 72 53 65 74 50 61 67 65 73 te3PagerSetPages
15ef3 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c ize(pBt->pPager,
15ef4 20 26 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2c &pBt->pageSize,
15ef5 20 6e 52 65 73 65 72 76 65 29 3b 0a 20 20 20 20 nReserve);.
15ef6 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 62 74 if( rc ) goto bt
15ef7 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 ree_open_out;.
15ef8 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a pBt->usableSiz
15ef9 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a e = pBt->pageSiz
15efa 65 20 2d 20 6e 52 65 73 65 72 76 65 3b 0a 20 20 e - nReserve;.
15efb 20 20 61 73 73 65 72 74 28 20 28 70 42 74 2d 3e assert( (pBt->
15efc 70 61 67 65 53 69 7a 65 20 26 20 37 29 3d 3d 30 pageSize & 7)==0
15efd 20 29 3b 20 20 2f 2a 20 38 2d 62 79 74 65 20 61 ); /* 8-byte a
15efe 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 70 61 67 65 lignment of page
15eff 53 69 7a 65 20 2a 2f 0a 20 20 20 0a 23 69 66 20 Size */. .#if
15f00 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
15f01 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
15f02 45 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 E) && !defined(S
15f03 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 QLITE_OMIT_DISKI
15f04 4f 29 0a 20 20 20 20 2f 2a 20 41 64 64 20 74 68 O). /* Add th
15f05 65 20 6e 65 77 20 42 74 53 68 61 72 65 64 20 6f e new BtShared o
15f06 62 6a 65 63 74 20 74 6f 20 74 68 65 20 6c 69 6e bject to the lin
15f07 6b 65 64 20 6c 69 73 74 20 73 68 61 72 61 62 6c ked list sharabl
15f08 65 20 42 74 53 68 61 72 65 64 73 2e 0a 20 20 20 e BtShareds..
15f09 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 73 */. if( p->s
15f0a 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 harable ){.
15f0b 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a sqlite3_mutex *
15f0c 6d 75 74 65 78 53 68 61 72 65 64 3b 0a 20 20 20 mutexShared;.
15f0d 20 20 20 70 42 74 2d 3e 6e 52 65 66 20 3d 20 31 pBt->nRef = 1
15f0e 3b 0a 20 20 20 20 20 20 6d 75 74 65 78 53 68 61 ;. mutexSha
15f0f 72 65 64 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 red = sqlite3Mut
15f10 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d exAlloc(SQLITE_M
15f11 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 UTEX_STATIC_MAST
15f12 45 52 29 3b 0a 20 20 20 20 20 20 69 66 28 20 53 ER);. if( S
15f13 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
15f14 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 && sqlite3Globa
15f15 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 lConfig.bCoreMut
15f16 65 78 20 29 7b 0a 20 20 20 20 20 20 20 20 70 42 ex ){. pB
15f17 74 2d 3e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 t->mutex = sqlit
15f18 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c e3MutexAlloc(SQL
15f19 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 29 3b ITE_MUTEX_FAST);
15f1a 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 42 74 . if( pBt
15f1b 2d 3e 6d 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20 ->mutex==0 ){.
15f1c 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
15f1d 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 ITE_NOMEM;.
15f1e 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 db->mallocF
15f1f 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20 20 ailed = 0;.
15f20 20 20 20 20 20 67 6f 74 6f 20 62 74 72 65 65 5f goto btree_
15f21 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 open_out;.
15f22 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
15f23 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
15f24 65 6e 74 65 72 28 6d 75 74 65 78 53 68 61 72 65 enter(mutexShare
15f25 64 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 d);. pBt->p
15f26 4e 65 78 74 20 3d 20 47 4c 4f 42 41 4c 28 42 74 Next = GLOBAL(Bt
15f27 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 Shared*,sqlite3S
15f28 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 3b haredCacheList);
15f29 0a 20 20 20 20 20 20 47 4c 4f 42 41 4c 28 42 74 . GLOBAL(Bt
15f2a 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 Shared*,sqlite3S
15f2b 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 20 haredCacheList)
15f2c 3d 20 70 42 74 3b 0a 20 20 20 20 20 20 73 71 6c = pBt;. sql
15f2d 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
15f2e 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b 0a 20 (mutexShared);.
15f2f 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a }.#endif. }.
15f30 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
15f31 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
15f32 5f 43 41 43 48 45 29 20 26 26 20 21 64 65 66 69 _CACHE) && !defi
15f33 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
15f34 44 49 53 4b 49 4f 29 0a 20 20 2f 2a 20 49 66 20 DISKIO). /* If
15f35 74 68 65 20 6e 65 77 20 42 74 72 65 65 20 75 73 the new Btree us
15f36 65 73 20 61 20 73 68 61 72 61 62 6c 65 20 70 42 es a sharable pB
15f37 74 53 68 61 72 65 64 2c 20 74 68 65 6e 20 6c 69 tShared, then li
15f38 6e 6b 20 74 68 65 20 6e 65 77 0a 20 20 2a 2a 20 nk the new. **
15f39 42 74 72 65 65 20 69 6e 74 6f 20 74 68 65 20 6c Btree into the l
15f3a 69 73 74 20 6f 66 20 61 6c 6c 20 73 68 61 72 61 ist of all shara
15f3b 62 6c 65 20 42 74 72 65 65 73 20 66 6f 72 20 74 ble Btrees for t
15f3c 68 65 20 73 61 6d 65 20 63 6f 6e 6e 65 63 74 69 he same connecti
15f3d 6f 6e 2e 0a 20 20 2a 2a 20 54 68 65 20 6c 69 73 on.. ** The lis
15f3e 74 20 69 73 20 6b 65 70 74 20 69 6e 20 61 73 63 t is kept in asc
15f3f 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 79 20 ending order by
15f40 70 42 74 20 61 64 64 72 65 73 73 2e 0a 20 20 2a pBt address.. *
15f41 2f 0a 20 20 69 66 28 20 70 2d 3e 73 68 61 72 61 /. if( p->shara
15f42 62 6c 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 ble ){. int i
15f43 3b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 53 69 ;. Btree *pSi
15f44 62 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 b;. for(i=0;
15f45 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b i<db->nDb; i++){
15f46 0a 20 20 20 20 20 20 69 66 28 20 28 70 53 69 62 . if( (pSib
15f47 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 = db->aDb[i].pB
15f48 74 29 21 3d 30 20 26 26 20 70 53 69 62 2d 3e 73 t)!=0 && pSib->s
15f49 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 harable ){.
15f4a 20 20 20 77 68 69 6c 65 28 20 70 53 69 62 2d 3e while( pSib->
15f4b 70 50 72 65 76 20 29 7b 20 70 53 69 62 20 3d 20 pPrev ){ pSib =
15f4c 70 53 69 62 2d 3e 70 50 72 65 76 3b 20 7d 0a 20 pSib->pPrev; }.
15f4d 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 42 if( p->pB
15f4e 74 3c 70 53 69 62 2d 3e 70 42 74 20 29 7b 0a 20 t<pSib->pBt ){.
15f4f 20 20 20 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 p->pNex
15f50 74 20 3d 20 70 53 69 62 3b 0a 20 20 20 20 20 20 t = pSib;.
15f51 20 20 20 20 70 2d 3e 70 50 72 65 76 20 3d 20 30 p->pPrev = 0
15f52 3b 0a 20 20 20 20 20 20 20 20 20 20 70 53 69 62 ;. pSib
15f53 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20 20 20 ->pPrev = p;.
15f54 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
15f55 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 53 69 while( pSi
15f56 62 2d 3e 70 4e 65 78 74 20 26 26 20 70 53 69 62 b->pNext && pSib
15f57 2d 3e 70 4e 65 78 74 2d 3e 70 42 74 3c 70 2d 3e ->pNext->pBt<p->
15f58 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 pBt ){.
15f59 20 20 20 70 53 69 62 20 3d 20 70 53 69 62 2d 3e pSib = pSib->
15f5a 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 20 pNext;.
15f5b 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e }. p->
15f5c 70 4e 65 78 74 20 3d 20 70 53 69 62 2d 3e 70 4e pNext = pSib->pN
15f5d 65 78 74 3b 0a 20 20 20 20 20 20 20 20 20 20 70 ext;. p
15f5e 2d 3e 70 50 72 65 76 20 3d 20 70 53 69 62 3b 0a ->pPrev = pSib;.
15f5f 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 2d if( p-
15f60 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 >pNext ){.
15f61 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 2d 3e p->pNext->
15f62 70 50 72 65 76 20 3d 20 70 3b 0a 20 20 20 20 20 pPrev = p;.
15f63 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
15f64 20 70 53 69 62 2d 3e 70 4e 65 78 74 20 3d 20 70 pSib->pNext = p
15f65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
15f66 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
15f67 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e }. }. }.#en
15f68 64 69 66 0a 20 20 2a 70 70 42 74 72 65 65 20 3d dif. *ppBtree =
15f69 20 70 3b 0a 0a 62 74 72 65 65 5f 6f 70 65 6e 5f p;..btree_open_
15f6a 6f 75 74 3a 0a 20 20 69 66 28 20 72 63 21 3d 53 out:. if( rc!=S
15f6b 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
15f6c 69 66 28 20 70 42 74 20 26 26 20 70 42 74 2d 3e if( pBt && pBt->
15f6d 70 50 61 67 65 72 20 29 7b 0a 20 20 20 20 20 20 pPager ){.
15f6e 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f 73 sqlite3PagerClos
15f6f 65 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a e(pBt->pPager);.
15f70 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
15f71 33 5f 66 72 65 65 28 70 42 74 29 3b 0a 20 20 20 3_free(pBt);.
15f72 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 sqlite3_free(p)
15f73 3b 0a 20 20 20 20 2a 70 70 42 74 72 65 65 20 3d ;. *ppBtree =
15f74 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6d 75 0;. }. if( mu
15f75 74 65 78 4f 70 65 6e 20 29 7b 0a 20 20 20 20 61 texOpen ){. a
15f76 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
15f77 75 74 65 78 5f 68 65 6c 64 28 6d 75 74 65 78 4f utex_held(mutexO
15f78 70 65 6e 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 pen) );. sqli
15f79 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
15f7a 6d 75 74 65 78 4f 70 65 6e 29 3b 0a 20 20 7d 0a mutexOpen);. }.
15f7b 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
15f7c 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20 /*.** Decrement
15f7d 74 68 65 20 42 74 53 68 61 72 65 64 2e 6e 52 65 the BtShared.nRe
15f7e 66 20 63 6f 75 6e 74 65 72 2e 20 20 57 68 65 6e f counter. When
15f7f 20 69 74 20 72 65 61 63 68 65 73 20 7a 65 72 6f it reaches zero
15f80 2c 0a 2a 2a 20 72 65 6d 6f 76 65 20 74 68 65 20 ,.** remove the
15f81 42 74 53 68 61 72 65 64 20 73 74 72 75 63 74 75 BtShared structu
15f82 72 65 20 66 72 6f 6d 20 74 68 65 20 73 68 61 72 re from the shar
15f83 69 6e 67 20 6c 69 73 74 2e 20 20 52 65 74 75 72 ing list. Retur
15f84 6e 0a 2a 2a 20 74 72 75 65 20 69 66 20 74 68 65 n.** true if the
15f85 20 42 74 53 68 61 72 65 64 2e 6e 52 65 66 20 63 BtShared.nRef c
15f86 6f 75 6e 74 65 72 20 72 65 61 63 68 65 73 20 7a ounter reaches z
15f87 65 72 6f 20 61 6e 64 20 72 65 74 75 72 6e 0a 2a ero and return.*
15f88 2a 20 66 61 6c 73 65 20 69 66 20 69 74 20 69 73 * false if it is
15f89 20 73 74 69 6c 6c 20 70 6f 73 69 74 69 76 65 2e still positive.
15f8a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 .*/.static int r
15f8b 65 6d 6f 76 65 46 72 6f 6d 53 68 61 72 69 6e 67 emoveFromSharing
15f8c 4c 69 73 74 28 42 74 53 68 61 72 65 64 20 2a 70 List(BtShared *p
15f8d 42 74 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c Bt){.#ifndef SQL
15f8e 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
15f8f 43 41 43 48 45 0a 20 20 73 71 6c 69 74 65 33 5f CACHE. sqlite3_
15f90 6d 75 74 65 78 20 2a 70 4d 61 73 74 65 72 3b 0a mutex *pMaster;.
15f91 20 20 42 74 53 68 61 72 65 64 20 2a 70 4c 69 73 BtShared *pLis
15f92 74 3b 0a 20 20 69 6e 74 20 72 65 6d 6f 76 65 64 t;. int removed
15f93 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 = 0;.. assert(
15f94 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e sqlite3_mutex_n
15f95 6f 74 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 otheld(pBt->mute
15f96 78 29 20 29 3b 0a 20 20 70 4d 61 73 74 65 72 20 x) );. pMaster
15f97 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c = sqlite3MutexAl
15f98 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 loc(SQLITE_MUTEX
15f99 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b _STATIC_MASTER);
15f9a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
15f9b 5f 65 6e 74 65 72 28 70 4d 61 73 74 65 72 29 3b _enter(pMaster);
15f9c 0a 20 20 70 42 74 2d 3e 6e 52 65 66 2d 2d 3b 0a . pBt->nRef--;.
15f9d 20 20 69 66 28 20 70 42 74 2d 3e 6e 52 65 66 3c if( pBt->nRef<
15f9e 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 47 4c =0 ){. if( GL
15f9f 4f 42 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 OBAL(BtShared*,s
15fa0 71 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 68 qlite3SharedCach
15fa1 65 4c 69 73 74 29 3d 3d 70 42 74 20 29 7b 0a 20 eList)==pBt ){.
15fa2 20 20 20 20 20 47 4c 4f 42 41 4c 28 42 74 53 68 GLOBAL(BtSh
15fa3 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 ared*,sqlite3Sha
15fa4 72 65 64 43 61 63 68 65 4c 69 73 74 29 20 3d 20 redCacheList) =
15fa5 70 42 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 pBt->pNext;.
15fa6 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 4c 69 }else{. pLi
15fa7 73 74 20 3d 20 47 4c 4f 42 41 4c 28 42 74 53 68 st = GLOBAL(BtSh
15fa8 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 ared*,sqlite3Sha
15fa9 72 65 64 43 61 63 68 65 4c 69 73 74 29 3b 0a 20 redCacheList);.
15faa 20 20 20 20 20 77 68 69 6c 65 28 20 41 4c 57 41 while( ALWA
15fab 59 53 28 70 4c 69 73 74 29 20 26 26 20 70 4c 69 YS(pList) && pLi
15fac 73 74 2d 3e 70 4e 65 78 74 21 3d 70 42 74 20 29 st->pNext!=pBt )
15fad 7b 0a 20 20 20 20 20 20 20 20 70 4c 69 73 74 3d {. pList=
15fae 70 4c 69 73 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 pList->pNext;.
15faf 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
15fb0 41 4c 57 41 59 53 28 70 4c 69 73 74 29 20 29 7b ALWAYS(pList) ){
15fb1 0a 20 20 20 20 20 20 20 20 70 4c 69 73 74 2d 3e . pList->
15fb2 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 4e 65 pNext = pBt->pNe
15fb3 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 xt;. }.
15fb4 7d 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 }. if( SQLITE
15fb5 5f 54 48 52 45 41 44 53 41 46 45 20 29 7b 0a 20 _THREADSAFE ){.
15fb6 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 sqlite3_mut
15fb7 65 78 5f 66 72 65 65 28 70 42 74 2d 3e 6d 75 74 ex_free(pBt->mut
15fb8 65 78 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 ex);. }. r
15fb9 65 6d 6f 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a emoved = 1;. }.
15fba 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
15fbb 6c 65 61 76 65 28 70 4d 61 73 74 65 72 29 3b 0a leave(pMaster);.
15fbc 20 20 72 65 74 75 72 6e 20 72 65 6d 6f 76 65 64 return removed
15fbd 3b 0a 23 65 6c 73 65 0a 20 20 72 65 74 75 72 6e ;.#else. return
15fbe 20 31 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 1;.#endif.}../*
15fbf 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 70 42 .** Make sure pB
15fc0 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 70 6f 69 t->pTmpSpace poi
15fc1 6e 74 73 20 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 nts to an alloca
15fc2 74 69 6f 6e 20 6f 66 20 0a 2a 2a 20 4d 58 5f 43 tion of .** MX_C
15fc3 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 62 79 ELL_SIZE(pBt) by
15fc4 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 tes..*/.static v
15fc5 6f 69 64 20 61 6c 6c 6f 63 61 74 65 54 65 6d 70 oid allocateTemp
15fc6 53 70 61 63 65 28 42 74 53 68 61 72 65 64 20 2a Space(BtShared *
15fc7 70 42 74 29 7b 0a 20 20 69 66 28 20 21 70 42 74 pBt){. if( !pBt
15fc8 2d 3e 70 54 6d 70 53 70 61 63 65 20 29 7b 0a 20 ->pTmpSpace ){.
15fc9 20 20 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 pBt->pTmpSpac
15fca 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 4d e = sqlite3PageM
15fcb 61 6c 6c 6f 63 28 20 70 42 74 2d 3e 70 61 67 65 alloc( pBt->page
15fcc 53 69 7a 65 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f Size );. }.}../
15fcd 2a 0a 2a 2a 20 46 72 65 65 20 74 68 65 20 70 42 *.** Free the pB
15fce 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 61 6c 6c t->pTmpSpace all
15fcf 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69 ocation.*/.stati
15fd0 63 20 76 6f 69 64 20 66 72 65 65 54 65 6d 70 53 c void freeTempS
15fd1 70 61 63 65 28 42 74 53 68 61 72 65 64 20 2a 70 pace(BtShared *p
15fd2 42 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 61 Bt){. sqlite3Pa
15fd3 67 65 46 72 65 65 28 20 70 42 74 2d 3e 70 54 6d geFree( pBt->pTm
15fd4 70 53 70 61 63 65 29 3b 0a 20 20 70 42 74 2d 3e pSpace);. pBt->
15fd5 70 54 6d 70 53 70 61 63 65 20 3d 20 30 3b 0a 7d pTmpSpace = 0;.}
15fd6 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6e ../*.** Close an
15fd7 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 61 open database a
15fd8 6e 64 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6c nd invalidate al
15fd9 6c 20 63 75 72 73 6f 72 73 2e 0a 2a 2f 0a 53 51 l cursors..*/.SQ
15fda 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
15fdb 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f sqlite3BtreeClo
15fdc 73 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 se(Btree *p){.
15fdd 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
15fde 70 2d 3e 70 42 74 3b 0a 20 20 42 74 43 75 72 73 p->pBt;. BtCurs
15fdf 6f 72 20 2a 70 43 75 72 3b 0a 0a 20 20 2f 2a 20 or *pCur;.. /*
15fe0 43 6c 6f 73 65 20 61 6c 6c 20 63 75 72 73 6f 72 Close all cursor
15fe1 73 20 6f 70 65 6e 65 64 20 76 69 61 20 74 68 69 s opened via thi
15fe2 73 20 68 61 6e 64 6c 65 2e 20 20 2a 2f 0a 20 20 s handle. */.
15fe3 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
15fe4 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 mutex_held(p->db
15fe5 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 ->mutex) );. sq
15fe6 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
15fe7 70 29 3b 0a 20 20 70 43 75 72 20 3d 20 70 42 74 p);. pCur = pBt
15fe8 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 77 68 69 ->pCursor;. whi
15fe9 6c 65 28 20 70 43 75 72 20 29 7b 0a 20 20 20 20 le( pCur ){.
15fea 42 74 43 75 72 73 6f 72 20 2a 70 54 6d 70 20 3d BtCursor *pTmp =
15feb 20 70 43 75 72 3b 0a 20 20 20 20 70 43 75 72 20 pCur;. pCur
15fec 3d 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b 0a 20 = pCur->pNext;.
15fed 20 20 20 69 66 28 20 70 54 6d 70 2d 3e 70 42 74 if( pTmp->pBt
15fee 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 20 ree==p ){.
15fef 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 sqlite3BtreeClos
15ff0 65 43 75 72 73 6f 72 28 70 54 6d 70 29 3b 0a 20 eCursor(pTmp);.
15ff1 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 }. }.. /* R
15ff2 6f 6c 6c 62 61 63 6b 20 61 6e 79 20 61 63 74 69 ollback any acti
15ff3 76 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 ve transaction a
15ff4 6e 64 20 66 72 65 65 20 74 68 65 20 68 61 6e 64 nd free the hand
15ff5 6c 65 20 73 74 72 75 63 74 75 72 65 2e 0a 20 20 le structure..
15ff6 2a 2a 20 54 68 65 20 63 61 6c 6c 20 74 6f 20 73 ** The call to s
15ff7 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 qlite3BtreeRollb
15ff8 61 63 6b 28 29 20 64 72 6f 70 73 20 61 6e 79 20 ack() drops any
15ff9 74 61 62 6c 65 2d 6c 6f 63 6b 73 20 68 65 6c 64 table-locks held
15ffa 20 62 79 0a 20 20 2a 2a 20 74 68 69 73 20 68 61 by. ** this ha
15ffb 6e 64 6c 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c ndle.. */. sql
15ffc 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 ite3BtreeRollbac
15ffd 6b 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 k(p);. sqlite3B
15ffe 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 0a 20 treeLeave(p);..
15fff 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 /* If there are
16000 20 73 74 69 6c 6c 20 6f 74 68 65 72 20 6f 75 74 still other out
16001 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e standing referen
16002 63 65 73 20 74 6f 20 74 68 65 20 73 68 61 72 65 ces to the share
16003 64 2d 62 74 72 65 65 0a 20 20 2a 2a 20 73 74 72 d-btree. ** str
16004 75 63 74 75 72 65 2c 20 72 65 74 75 72 6e 20 6e ucture, return n
16005 6f 77 2e 20 54 68 65 20 72 65 6d 61 69 6e 64 65 ow. The remainde
16006 72 20 6f 66 20 74 68 69 73 20 70 72 6f 63 65 64 r of this proced
16007 75 72 65 20 63 6c 65 61 6e 73 20 0a 20 20 2a 2a ure cleans . **
16008 20 75 70 20 74 68 65 20 73 68 61 72 65 64 2d 62 up the shared-b
16009 74 72 65 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 tree.. */. ass
1600a 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f ert( p->wantToLo
1600b 63 6b 3d 3d 30 20 26 26 20 70 2d 3e 6c 6f 63 6b ck==0 && p->lock
1600c 65 64 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 21 ed==0 );. if( !
1600d 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20 72 p->sharable || r
1600e 65 6d 6f 76 65 46 72 6f 6d 53 68 61 72 69 6e 67 emoveFromSharing
1600f 4c 69 73 74 28 70 42 74 29 20 29 7b 0a 20 20 20 List(pBt) ){.
16010 20 2f 2a 20 54 68 65 20 70 42 74 20 69 73 20 6e /* The pBt is n
16011 6f 20 6c 6f 6e 67 65 72 20 6f 6e 20 74 68 65 20 o longer on the
16012 73 68 61 72 69 6e 67 20 6c 69 73 74 2c 20 73 6f sharing list, so
16013 20 77 65 20 63 61 6e 20 61 63 63 65 73 73 0a 20 we can access.
16014 20 20 20 2a 2a 20 69 74 20 77 69 74 68 6f 75 74 ** it without
16015 20 68 61 76 69 6e 67 20 74 6f 20 68 6f 6c 64 20 having to hold
16016 74 68 65 20 6d 75 74 65 78 2e 0a 20 20 20 20 2a the mutex.. *
16017 2a 0a 20 20 20 20 2a 2a 20 43 6c 65 61 6e 20 6f *. ** Clean o
16018 75 74 20 61 6e 64 20 64 65 6c 65 74 65 20 74 68 ut and delete th
16019 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 e BtShared objec
1601a 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 t.. */. as
1601b 73 65 72 74 28 20 21 70 42 74 2d 3e 70 43 75 72 sert( !pBt->pCur
1601c 73 6f 72 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 sor );. sqlit
1601d 65 33 50 61 67 65 72 43 6c 6f 73 65 28 70 42 74 e3PagerClose(pBt
1601e 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69 ->pPager);. i
1601f 66 28 20 70 42 74 2d 3e 78 46 72 65 65 53 63 68 f( pBt->xFreeSch
16020 65 6d 61 20 26 26 20 70 42 74 2d 3e 70 53 63 68 ema && pBt->pSch
16021 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 70 42 74 ema ){. pBt
16022 2d 3e 78 46 72 65 65 53 63 68 65 6d 61 28 70 42 ->xFreeSchema(pB
16023 74 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 t->pSchema);.
16024 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 }. sqlite3_f
16025 72 65 65 28 70 42 74 2d 3e 70 53 63 68 65 6d 61 ree(pBt->pSchema
16026 29 3b 0a 20 20 20 20 66 72 65 65 54 65 6d 70 53 );. freeTempS
16027 70 61 63 65 28 70 42 74 29 3b 0a 20 20 20 20 73 pace(pBt);. s
16028 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 74 29 qlite3_free(pBt)
16029 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 ;. }..#ifndef S
1602a 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
1602b 44 5f 43 41 43 48 45 0a 20 20 61 73 73 65 72 74 D_CACHE. assert
1602c 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d ( p->wantToLock=
1602d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
1602e 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29 3b 0a p->locked==0 );.
1602f 20 20 69 66 28 20 70 2d 3e 70 50 72 65 76 20 29 if( p->pPrev )
16030 20 70 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 p->pPrev->pNext
16031 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 69 = p->pNext;. i
16032 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 20 70 2d f( p->pNext ) p-
16033 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 >pNext->pPrev =
16034 70 2d 3e 70 50 72 65 76 3b 0a 23 65 6e 64 69 66 p->pPrev;.#endif
16035 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 .. sqlite3_free
16036 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 (p);. return SQ
16037 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
16038 2a 20 43 68 61 6e 67 65 20 74 68 65 20 6c 69 6d * Change the lim
16039 69 74 20 6f 6e 20 74 68 65 20 6e 75 6d 62 65 72 it on the number
1603a 20 6f 66 20 70 61 67 65 73 20 61 6c 6c 6f 77 65 of pages allowe
1603b 64 20 69 6e 20 74 68 65 20 63 61 63 68 65 2e 0a d in the cache..
1603c 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 **.** The maximu
1603d 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 63 68 m number of cach
1603e 65 20 70 61 67 65 73 20 69 73 20 73 65 74 20 74 e pages is set t
1603f 6f 20 74 68 65 20 61 62 73 6f 6c 75 74 65 0a 2a o the absolute.*
16040 2a 20 76 61 6c 75 65 20 6f 66 20 6d 78 50 61 67 * value of mxPag
16041 65 2e 20 20 49 66 20 6d 78 50 61 67 65 20 69 73 e. If mxPage is
16042 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 20 70 negative, the p
16043 61 67 65 72 20 77 69 6c 6c 0a 2a 2a 20 6f 70 65 ager will.** ope
16044 72 61 74 65 20 61 73 79 6e 63 68 72 6f 6e 6f 75 rate asynchronou
16045 73 6c 79 20 2d 20 69 74 20 77 69 6c 6c 20 6e 6f sly - it will no
16046 74 20 73 74 6f 70 20 74 6f 20 64 6f 20 66 73 79 t stop to do fsy
16047 6e 63 28 29 73 0a 2a 2a 20 74 6f 20 69 6e 73 75 nc()s.** to insu
16048 72 65 20 64 61 74 61 20 69 73 20 77 72 69 74 74 re data is writt
16049 65 6e 20 74 6f 20 74 68 65 20 64 69 73 6b 20 73 en to the disk s
1604a 75 72 66 61 63 65 20 62 65 66 6f 72 65 0a 2a 2a urface before.**
1604b 20 63 6f 6e 74 69 6e 75 69 6e 67 2e 20 20 54 72 continuing. Tr
1604c 61 6e 73 61 63 74 69 6f 6e 73 20 73 74 69 6c 6c ansactions still
1604d 20 77 6f 72 6b 20 69 66 20 73 79 6e 63 68 72 6f work if synchro
1604e 6e 6f 75 73 20 69 73 20 6f 66 66 2c 0a 2a 2a 20 nous is off,.**
1604f 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 65 and the database
16050 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 72 72 75 cannot be corru
16051 70 74 65 64 20 69 66 20 74 68 69 73 20 70 72 6f pted if this pro
16052 67 72 61 6d 0a 2a 2a 20 63 72 61 73 68 65 73 2e gram.** crashes.
16053 20 20 42 75 74 20 69 66 20 74 68 65 20 6f 70 65 But if the ope
16054 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 63 72 rating system cr
16055 61 73 68 65 73 20 6f 72 20 74 68 65 72 65 20 69 ashes or there i
16056 73 0a 2a 2a 20 61 6e 20 61 62 72 75 70 74 20 70 s.** an abrupt p
16057 6f 77 65 72 20 66 61 69 6c 75 72 65 20 77 68 65 ower failure whe
16058 6e 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73 n synchronous is
16059 20 6f 66 66 2c 20 74 68 65 20 64 61 74 61 62 61 off, the databa
1605a 73 65 0a 2a 2a 20 63 6f 75 6c 64 20 62 65 20 6c se.** could be l
1605b 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 eft in an incons
1605c 69 73 74 65 6e 74 20 61 6e 64 20 75 6e 72 65 63 istent and unrec
1605d 6f 76 65 72 61 62 6c 65 20 73 74 61 74 65 2e 0a overable state..
1605e 2a 2a 20 53 79 6e 63 68 72 6f 6e 6f 75 73 20 69 ** Synchronous i
1605f 73 20 6f 6e 20 62 79 20 64 65 66 61 75 6c 74 20 s on by default
16060 73 6f 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 so database corr
16061 75 70 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 2a 2a uption is not.**
16062 20 6e 6f 72 6d 61 6c 6c 79 20 61 20 77 6f 72 72 normally a worr
16063 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 y..*/.SQLITE_PRI
16064 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
16065 42 74 72 65 65 53 65 74 43 61 63 68 65 53 69 7a BtreeSetCacheSiz
16066 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 e(Btree *p, int
16067 6d 78 50 61 67 65 29 7b 0a 20 20 42 74 53 68 61 mxPage){. BtSha
16068 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
16069 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c t;. assert( sql
1606a 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1606b 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b p->db->mutex) );
1606c 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
1606d 6e 74 65 72 28 70 29 3b 0a 20 20 73 71 6c 69 74 nter(p);. sqlit
1606e 65 33 50 61 67 65 72 53 65 74 43 61 63 68 65 73 e3PagerSetCaches
1606f 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c ize(pBt->pPager,
16070 20 6d 78 50 61 67 65 29 3b 0a 20 20 73 71 6c 69 mxPage);. sqli
16071 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
16072 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
16073 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 E_OK;.}../*.** C
16074 68 61 6e 67 65 20 74 68 65 20 77 61 79 20 64 61 hange the way da
16075 74 61 20 69 73 20 73 79 6e 63 65 64 20 74 6f 20 ta is synced to
16076 64 69 73 6b 20 69 6e 20 6f 72 64 65 72 20 74 6f disk in order to
16077 20 69 6e 63 72 65 61 73 65 20 6f 72 20 64 65 63 increase or dec
16078 72 65 61 73 65 0a 2a 2a 20 68 6f 77 20 77 65 6c rease.** how wel
16079 6c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 72 l the database r
1607a 65 73 69 73 74 73 20 64 61 6d 61 67 65 20 64 75 esists damage du
1607b 65 20 74 6f 20 4f 53 20 63 72 61 73 68 65 73 20 e to OS crashes
1607c 61 6e 64 20 70 6f 77 65 72 0a 2a 2a 20 66 61 69 and power.** fai
1607d 6c 75 72 65 73 2e 20 20 4c 65 76 65 6c 20 31 20 lures. Level 1
1607e 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 61 is the same as a
1607f 73 79 6e 63 68 72 6f 6e 6f 75 73 20 28 6e 6f 20 synchronous (no
16080 73 79 6e 63 73 28 29 20 6f 63 63 75 72 20 61 6e syncs() occur an
16081 64 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 61 20 d.** there is a
16082 68 69 67 68 20 70 72 6f 62 61 62 69 6c 69 74 79 high probability
16083 20 6f 66 20 64 61 6d 61 67 65 29 20 20 4c 65 76 of damage) Lev
16084 65 6c 20 32 20 69 73 20 74 68 65 20 64 65 66 61 el 2 is the defa
16085 75 6c 74 2e 20 20 54 68 65 72 65 0a 2a 2a 20 69 ult. There.** i
16086 73 20 61 20 76 65 72 79 20 6c 6f 77 20 62 75 74 s a very low but
16087 20 6e 6f 6e 2d 7a 65 72 6f 20 70 72 6f 62 61 62 non-zero probab
16088 69 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 65 2e ility of damage.
16089 20 20 4c 65 76 65 6c 20 33 20 72 65 64 75 63 65 Level 3 reduce
1608a 73 20 74 68 65 0a 2a 2a 20 70 72 6f 62 61 62 69 s the.** probabi
1608b 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 65 20 74 lity of damage t
1608c 6f 20 6e 65 61 72 20 7a 65 72 6f 20 62 75 74 20 o near zero but
1608d 77 69 74 68 20 61 20 77 72 69 74 65 20 70 65 72 with a write per
1608e 66 6f 72 6d 61 6e 63 65 20 72 65 64 75 63 74 69 formance reducti
1608f 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 on..*/.#ifndef S
16090 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 QLITE_OMIT_PAGER
16091 5f 50 52 41 47 4d 41 53 0a 53 51 4c 49 54 45 5f _PRAGMAS.SQLITE_
16092 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
16093 74 65 33 42 74 72 65 65 53 65 74 53 61 66 65 74 te3BtreeSetSafet
16094 79 4c 65 76 65 6c 28 42 74 72 65 65 20 2a 70 2c yLevel(Btree *p,
16095 20 69 6e 74 20 6c 65 76 65 6c 2c 20 69 6e 74 20 int level, int
16096 66 75 6c 6c 53 79 6e 63 29 7b 0a 20 20 42 74 53 fullSync){. BtS
16097 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e hared *pBt = p->
16098 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 pBt;. assert( s
16099 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
1609a 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 d(p->db->mutex)
1609b 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 );. sqlite3Btre
1609c 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 73 71 6c eEnter(p);. sql
1609d 69 74 65 33 50 61 67 65 72 53 65 74 53 61 66 65 ite3PagerSetSafe
1609e 74 79 4c 65 76 65 6c 28 70 42 74 2d 3e 70 50 61 tyLevel(pBt->pPa
1609f 67 65 72 2c 20 6c 65 76 65 6c 2c 20 66 75 6c 6c ger, level, full
160a0 53 79 6e 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 Sync);. sqlite3
160a1 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
160a2 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
160a3 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a K;.}.#endif../*.
160a4 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 ** Return TRUE i
160a5 66 20 74 68 65 20 67 69 76 65 6e 20 62 74 72 65 f the given btre
160a6 65 20 69 73 20 73 65 74 20 74 6f 20 73 61 66 65 e is set to safe
160a7 74 79 20 6c 65 76 65 6c 20 31 2e 20 20 49 6e 20 ty level 1. In
160a8 6f 74 68 65 72 0a 2a 2a 20 77 6f 72 64 73 2c 20 other.** words,
160a9 72 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 6e return TRUE if n
160aa 6f 20 73 79 6e 63 28 29 20 6f 63 63 75 72 73 20 o sync() occurs
160ab 6f 6e 20 74 68 65 20 64 69 73 6b 20 66 69 6c 65 on the disk file
160ac 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
160ad 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
160ae 42 74 72 65 65 53 79 6e 63 44 69 73 61 62 6c 65 BtreeSyncDisable
160af 64 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 d(Btree *p){. B
160b0 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
160b1 2d 3e 70 42 74 3b 0a 20 20 69 6e 74 20 72 63 3b ->pBt;. int rc;
160b2 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
160b3 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d e3_mutex_held(p-
160b4 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 20 20 >db->mutex) );
160b5 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
160b6 6e 74 65 72 28 70 29 3b 0a 20 20 61 73 73 65 72 nter(p);. asser
160b7 74 28 20 70 42 74 20 26 26 20 70 42 74 2d 3e 70 t( pBt && pBt->p
160b8 50 61 67 65 72 20 29 3b 0a 20 20 72 63 20 3d 20 Pager );. rc =
160b9 73 71 6c 69 74 65 33 50 61 67 65 72 4e 6f 73 79 sqlite3PagerNosy
160ba 6e 63 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b nc(pBt->pPager);
160bb 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
160bc 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 eave(p);. retur
160bd 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 n rc;.}..#if !de
160be 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
160bf 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 29 T_PAGER_PRAGMAS)
160c0 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c || !defined(SQL
160c1 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 29 ITE_OMIT_VACUUM)
160c2 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 ./*.** Change th
160c3 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 73 20 e default pages
160c4 73 69 7a 65 20 61 6e 64 20 74 68 65 20 6e 75 6d size and the num
160c5 62 65 72 20 6f 66 20 72 65 73 65 72 76 65 64 20 ber of reserved
160c6 62 79 74 65 73 20 70 65 72 20 70 61 67 65 2e 0a bytes per page..
160c7 2a 2a 20 4f 72 2c 20 69 66 20 74 68 65 20 70 61 ** Or, if the pa
160c8 67 65 20 73 69 7a 65 20 68 61 73 20 61 6c 72 65 ge size has alre
160c9 61 64 79 20 62 65 65 6e 20 66 69 78 65 64 2c 20 ady been fixed,
160ca 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 return SQLITE_RE
160cb 41 44 4f 4e 4c 59 20 0a 2a 2a 20 77 69 74 68 6f ADONLY .** witho
160cc 75 74 20 63 68 61 6e 67 69 6e 67 20 61 6e 79 74 ut changing anyt
160cd 68 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 hing..**.** The
160ce 70 61 67 65 20 73 69 7a 65 20 6d 75 73 74 20 62 page size must b
160cf 65 20 61 20 70 6f 77 65 72 20 6f 66 20 32 20 62 e a power of 2 b
160d0 65 74 77 65 65 6e 20 35 31 32 20 61 6e 64 20 36 etween 512 and 6
160d1 35 35 33 36 2e 20 20 49 66 20 74 68 65 20 70 61 5536. If the pa
160d2 67 65 0a 2a 2a 20 73 69 7a 65 20 73 75 70 70 6c ge.** size suppl
160d3 69 65 64 20 64 6f 65 73 20 6e 6f 74 20 6d 65 65 ied does not mee
160d4 74 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e t this constrain
160d5 74 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 20 t then the page
160d6 73 69 7a 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 63 size is not.** c
160d7 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 61 hanged..**.** Pa
160d8 67 65 20 73 69 7a 65 73 20 61 72 65 20 63 6f 6e ge sizes are con
160d9 73 74 72 61 69 6e 65 64 20 74 6f 20 62 65 20 61 strained to be a
160da 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 20 73 6f power of two so
160db 20 74 68 61 74 20 74 68 65 20 72 65 67 69 6f 6e that the region
160dc 0a 2a 2a 20 6f 66 20 74 68 65 20 64 61 74 61 62 .** of the datab
160dd 61 73 65 20 66 69 6c 65 20 75 73 65 64 20 66 6f ase file used fo
160de 72 20 6c 6f 63 6b 69 6e 67 20 28 62 65 67 69 6e r locking (begin
160df 6e 69 6e 67 20 61 74 20 50 45 4e 44 49 4e 47 5f ning at PENDING_
160e0 42 59 54 45 2c 0a 2a 2a 20 74 68 65 20 66 69 72 BYTE,.** the fir
160e1 73 74 20 62 79 74 65 20 70 61 73 74 20 74 68 65 st byte past the
160e2 20 31 47 42 20 62 6f 75 6e 64 61 72 79 2c 20 30 1GB boundary, 0
160e3 78 34 30 30 30 30 30 30 30 29 20 6e 65 65 64 73 x40000000) needs
160e4 20 74 6f 20 6f 63 63 75 72 0a 2a 2a 20 61 74 20 to occur.** at
160e5 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 the beginning of
160e6 20 61 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 a page..**.** I
160e7 66 20 70 61 72 61 6d 65 74 65 72 20 6e 52 65 73 f parameter nRes
160e8 65 72 76 65 20 69 73 20 6c 65 73 73 20 74 68 61 erve is less tha
160e9 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 n zero, then the
160ea 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 65 72 number of reser
160eb 76 65 64 0a 2a 2a 20 62 79 74 65 73 20 70 65 72 ved.** bytes per
160ec 20 70 61 67 65 20 69 73 20 6c 65 66 74 20 75 6e page is left un
160ed 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 changed..**.** I
160ee 66 20 74 68 65 20 69 46 69 78 21 3d 30 20 74 68 f the iFix!=0 th
160ef 65 6e 20 74 68 65 20 70 61 67 65 53 69 7a 65 46 en the pageSizeF
160f0 69 78 65 64 20 66 6c 61 67 20 69 73 20 73 65 74 ixed flag is set
160f1 20 73 6f 20 74 68 61 74 20 74 68 65 20 70 61 67 so that the pag
160f2 65 20 73 69 7a 65 0a 2a 2a 20 61 6e 64 20 61 75 e size.** and au
160f3 74 6f 76 61 63 75 75 6d 20 6d 6f 64 65 20 63 61 tovacuum mode ca
160f4 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20 62 65 20 63 n no longer be c
160f5 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 hanged..*/.SQLIT
160f6 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
160f7 6c 69 74 65 33 42 74 72 65 65 53 65 74 50 61 67 lite3BtreeSetPag
160f8 65 53 69 7a 65 28 42 74 72 65 65 20 2a 70 2c 20 eSize(Btree *p,
160f9 69 6e 74 20 70 61 67 65 53 69 7a 65 2c 20 69 6e int pageSize, in
160fa 74 20 6e 52 65 73 65 72 76 65 2c 20 69 6e 74 20 t nReserve, int
160fb 69 46 69 78 29 7b 0a 20 20 69 6e 74 20 72 63 20 iFix){. int rc
160fc 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 42 = SQLITE_OK;. B
160fd 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
160fe 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 ->pBt;. assert(
160ff 20 6e 52 65 73 65 72 76 65 3e 3d 2d 31 20 26 26 nReserve>=-1 &&
16100 20 6e 52 65 73 65 72 76 65 3c 3d 32 35 35 20 29 nReserve<=255 )
16101 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
16102 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 28 20 Enter(p);. if(
16103 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 pBt->pageSizeFix
16104 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ed ){. sqlite
16105 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
16106 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
16107 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a E_READONLY;. }.
16108 20 20 69 66 28 20 6e 52 65 73 65 72 76 65 3c 30 if( nReserve<0
16109 20 29 7b 0a 20 20 20 20 6e 52 65 73 65 72 76 65 ){. nReserve
1610a 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 = pBt->pageSize
1610b 20 2d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 - pBt->usableSi
1610c 7a 65 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 ze;. }. assert
1610d 28 20 6e 52 65 73 65 72 76 65 3e 3d 30 20 26 26 ( nReserve>=0 &&
1610e 20 6e 52 65 73 65 72 76 65 3c 3d 32 35 35 20 29 nReserve<=255 )
1610f 3b 0a 20 20 69 66 28 20 70 61 67 65 53 69 7a 65 ;. if( pageSize
16110 3e 3d 35 31 32 20 26 26 20 70 61 67 65 53 69 7a >=512 && pageSiz
16111 65 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 e<=SQLITE_MAX_PA
16112 47 45 5f 53 49 5a 45 20 26 26 0a 20 20 20 20 20 GE_SIZE &&.
16113 20 20 20 28 28 70 61 67 65 53 69 7a 65 2d 31 29 ((pageSize-1)
16114 26 70 61 67 65 53 69 7a 65 29 3d 3d 30 20 29 7b &pageSize)==0 ){
16115 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 61 . assert( (pa
16116 67 65 53 69 7a 65 20 26 20 37 29 3d 3d 30 20 29 geSize & 7)==0 )
16117 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70 ;. assert( !p
16118 42 74 2d 3e 70 50 61 67 65 31 20 26 26 20 21 70 Bt->pPage1 && !p
16119 42 74 2d 3e 70 43 75 72 73 6f 72 20 29 3b 0a 20 Bt->pCursor );.
1611a 20 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 pBt->pageSize
1611b 20 3d 20 28 75 31 36 29 70 61 67 65 53 69 7a 65 = (u16)pageSize
1611c 3b 0a 20 20 20 20 66 72 65 65 54 65 6d 70 53 70 ;. freeTempSp
1611d 61 63 65 28 70 42 74 29 3b 0a 20 20 7d 0a 20 20 ace(pBt);. }.
1611e 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
1611f 72 53 65 74 50 61 67 65 73 69 7a 65 28 70 42 74 rSetPagesize(pBt
16120 2d 3e 70 50 61 67 65 72 2c 20 26 70 42 74 2d 3e ->pPager, &pBt->
16121 70 61 67 65 53 69 7a 65 2c 20 6e 52 65 73 65 72 pageSize, nReser
16122 76 65 29 3b 0a 20 20 70 42 74 2d 3e 75 73 61 62 ve);. pBt->usab
16123 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 70 61 leSize = pBt->pa
16124 67 65 53 69 7a 65 20 2d 20 28 75 31 36 29 6e 52 geSize - (u16)nR
16125 65 73 65 72 76 65 3b 0a 20 20 69 66 28 20 69 46 eserve;. if( iF
16126 69 78 20 29 20 70 42 74 2d 3e 70 61 67 65 53 69 ix ) pBt->pageSi
16127 7a 65 46 69 78 65 64 20 3d 20 31 3b 0a 20 20 73 zeFixed = 1;. s
16128 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
16129 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 (p);. return rc
1612a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
1612b 6e 20 74 68 65 20 63 75 72 72 65 6e 74 6c 79 20 n the currently
1612c 64 65 66 69 6e 65 64 20 70 61 67 65 20 73 69 7a defined page siz
1612d 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 e.*/.SQLITE_PRIV
1612e 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
1612f 74 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 treeGetPageSize(
16130 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 72 65 74 Btree *p){. ret
16131 75 72 6e 20 70 2d 3e 70 42 74 2d 3e 70 61 67 65 urn p->pBt->page
16132 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 Size;.}../*.** R
16133 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
16134 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70 61 of bytes of spa
16135 63 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 ce at the end of
16136 20 65 76 65 72 79 20 70 61 67 65 20 74 68 61 74 every page that
16137 0a 2a 2a 20 61 72 65 20 69 6e 74 65 6e 74 75 61 .** are intentua
16138 6c 6c 79 20 6c 65 66 74 20 75 6e 75 73 65 64 2e lly left unused.
16139 20 20 54 68 69 73 20 69 73 20 74 68 65 20 22 72 This is the "r
1613a 65 73 65 72 76 65 64 22 20 73 70 61 63 65 20 74 eserved" space t
1613b 68 61 74 20 69 73 0a 2a 2a 20 73 6f 6d 65 74 69 hat is.** someti
1613c 6d 65 73 20 75 73 65 64 20 62 79 20 65 78 74 65 mes used by exte
1613d 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 nsions..*/.SQLIT
1613e 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1613f 6c 69 74 65 33 42 74 72 65 65 47 65 74 52 65 73 lite3BtreeGetRes
16140 65 72 76 65 28 42 74 72 65 65 20 2a 70 29 7b 0a erve(Btree *p){.
16141 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74 int n;. sqlit
16142 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
16143 0a 20 20 6e 20 3d 20 70 2d 3e 70 42 74 2d 3e 70 . n = p->pBt->p
16144 61 67 65 53 69 7a 65 20 2d 20 70 2d 3e 70 42 74 ageSize - p->pBt
16145 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 ->usableSize;.
16146 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
16147 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e e(p);. return n
16148 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 ;.}../*.** Set t
16149 68 65 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 he maximum page
1614a 63 6f 75 6e 74 20 66 6f 72 20 61 20 64 61 74 61 count for a data
1614b 62 61 73 65 20 69 66 20 6d 78 50 61 67 65 20 69 base if mxPage i
1614c 73 20 70 6f 73 69 74 69 76 65 2e 0a 2a 2a 20 4e s positive..** N
1614d 6f 20 63 68 61 6e 67 65 73 20 61 72 65 20 6d 61 o changes are ma
1614e 64 65 20 69 66 20 6d 78 50 61 67 65 20 69 73 20 de if mxPage is
1614f 30 20 6f 72 20 6e 65 67 61 74 69 76 65 2e 0a 2a 0 or negative..*
16150 2a 20 52 65 67 61 72 64 6c 65 73 73 20 6f 66 20 * Regardless of
16151 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6d 78 50 the value of mxP
16152 61 67 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20 age, return the
16153 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 63 6f 75 maximum page cou
16154 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 nt..*/.SQLITE_PR
16155 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
16156 33 42 74 72 65 65 4d 61 78 50 61 67 65 43 6f 75 3BtreeMaxPageCou
16157 6e 74 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 nt(Btree *p, int
16158 20 6d 78 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 mxPage){. int
16159 6e 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 n;. sqlite3Btre
1615a 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 6e 20 3d eEnter(p);. n =
1615b 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 61 78 sqlite3PagerMax
1615c 50 61 67 65 43 6f 75 6e 74 28 70 2d 3e 70 42 74 PageCount(p->pBt
1615d 2d 3e 70 50 61 67 65 72 2c 20 6d 78 50 61 67 65 ->pPager, mxPage
1615e 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 );. sqlite3Btre
1615f 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 eLeave(p);. ret
16160 75 72 6e 20 6e 3b 0a 7d 0a 23 65 6e 64 69 66 20 urn n;.}.#endif
16161 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 /* !defined(SQLI
16162 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 TE_OMIT_PAGER_PR
16163 41 47 4d 41 53 29 20 7c 7c 20 21 64 65 66 69 6e AGMAS) || !defin
16164 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ed(SQLITE_OMIT_V
16165 41 43 55 55 4d 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a ACUUM) */../*.**
16166 20 43 68 61 6e 67 65 20 74 68 65 20 27 61 75 74 Change the 'aut
16167 6f 2d 76 61 63 75 75 6d 27 20 70 72 6f 70 65 72 o-vacuum' proper
16168 74 79 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ty of the databa
16169 73 65 2e 20 49 66 20 74 68 65 20 27 61 75 74 6f se. If the 'auto
1616a 56 61 63 75 75 6d 27 0a 2a 2a 20 70 61 72 61 6d Vacuum'.** param
1616b 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f eter is non-zero
1616c 2c 20 74 68 65 6e 20 61 75 74 6f 2d 76 61 63 75 , then auto-vacu
1616d 75 6d 20 6d 6f 64 65 20 69 73 20 65 6e 61 62 6c um mode is enabl
1616e 65 64 2e 20 49 66 20 7a 65 72 6f 2c 20 69 74 0a ed. If zero, it.
1616f 2a 2a 20 69 73 20 64 69 73 61 62 6c 65 64 2e 20 ** is disabled.
16170 54 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 The default valu
16171 65 20 66 6f 72 20 74 68 65 20 61 75 74 6f 2d 76 e for the auto-v
16172 61 63 75 75 6d 20 70 72 6f 70 65 72 74 79 20 69 acuum property i
16173 73 20 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65 64 s .** determined
16174 20 62 79 20 74 68 65 20 53 51 4c 49 54 45 5f 44 by the SQLITE_D
16175 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 EFAULT_AUTOVACUU
16176 4d 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 53 51 4c 49 M macro..*/.SQLI
16177 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
16178 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 41 75 qlite3BtreeSetAu
16179 74 6f 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a toVacuum(Btree *
1617a 70 2c 20 69 6e 74 20 61 75 74 6f 56 61 63 75 75 p, int autoVacuu
1617b 6d 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 m){.#ifdef SQLIT
1617c 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
1617d 4d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 M. return SQLIT
1617e 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 23 65 6c 73 E_READONLY;.#els
1617f 65 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 e. BtShared *pB
16180 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 69 6e t = p->pBt;. in
16181 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
16182 3b 0a 20 20 75 38 20 61 76 20 3d 20 28 75 38 29 ;. u8 av = (u8)
16183 61 75 74 6f 56 61 63 75 75 6d 3b 0a 0a 20 20 73 autoVacuum;.. s
16184 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
16185 28 70 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e (p);. if( pBt->
16186 70 61 67 65 53 69 7a 65 46 69 78 65 64 20 26 26 pageSizeFixed &&
16187 20 28 61 76 20 3f 31 3a 30 29 21 3d 70 42 74 2d (av ?1:0)!=pBt-
16188 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 >autoVacuum ){.
16189 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 rc = SQLITE_R
1618a 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 65 6c 73 65 EADONLY;. }else
1618b 7b 0a 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56 {. pBt->autoV
1618c 61 63 75 75 6d 20 3d 20 61 76 20 3f 31 3a 30 3b acuum = av ?1:0;
1618d 0a 20 20 20 20 70 42 74 2d 3e 69 6e 63 72 56 61 . pBt->incrVa
1618e 63 75 75 6d 20 3d 20 61 76 3d 3d 32 20 3f 31 3a cuum = av==2 ?1:
1618f 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 0;. }. sqlite3
16190 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
16191 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64 return rc;.#end
16192 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 if.}../*.** Retu
16193 72 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 rn the value of
16194 74 68 65 20 27 61 75 74 6f 2d 76 61 63 75 75 6d the 'auto-vacuum
16195 27 20 70 72 6f 70 65 72 74 79 2e 20 49 66 20 61 ' property. If a
16196 75 74 6f 2d 76 61 63 75 75 6d 20 69 73 20 0a 2a uto-vacuum is .*
16197 2a 20 65 6e 61 62 6c 65 64 20 31 20 69 73 20 72 * enabled 1 is r
16198 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 eturned. Otherwi
16199 73 65 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f se 0..*/.SQLITE_
1619a 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1619b 74 65 33 42 74 72 65 65 47 65 74 41 75 74 6f 56 te3BtreeGetAutoV
1619c 61 63 75 75 6d 28 42 74 72 65 65 20 2a 70 29 7b acuum(Btree *p){
1619d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f .#ifdef SQLITE_O
1619e 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
1619f 20 72 65 74 75 72 6e 20 42 54 52 45 45 5f 41 55 return BTREE_AU
161a0 54 4f 56 41 43 55 55 4d 5f 4e 4f 4e 45 3b 0a 23 TOVACUUM_NONE;.#
161a1 65 6c 73 65 0a 20 20 69 6e 74 20 72 63 3b 0a 20 else. int rc;.
161a2 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
161a3 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 28 0a er(p);. rc = (.
161a4 20 20 20 20 28 21 70 2d 3e 70 42 74 2d 3e 61 75 (!p->pBt->au
161a5 74 6f 56 61 63 75 75 6d 29 3f 42 54 52 45 45 5f toVacuum)?BTREE_
161a6 41 55 54 4f 56 41 43 55 55 4d 5f 4e 4f 4e 45 3a AUTOVACUUM_NONE:
161a7 0a 20 20 20 20 28 21 70 2d 3e 70 42 74 2d 3e 69 . (!p->pBt->i
161a8 6e 63 72 56 61 63 75 75 6d 29 3f 42 54 52 45 45 ncrVacuum)?BTREE
161a9 5f 41 55 54 4f 56 41 43 55 55 4d 5f 46 55 4c 4c _AUTOVACUUM_FULL
161aa 3a 0a 20 20 20 20 42 54 52 45 45 5f 41 55 54 4f :. BTREE_AUTO
161ab 56 41 43 55 55 4d 5f 49 4e 43 52 0a 20 20 29 3b VACUUM_INCR. );
161ac 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
161ad 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 eave(p);. retur
161ae 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a n rc;.#endif.}..
161af 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 72 65 66 ./*.** Get a ref
161b0 65 72 65 6e 63 65 20 74 6f 20 70 50 61 67 65 31 erence to pPage1
161b1 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
161b2 20 66 69 6c 65 2e 20 20 54 68 69 73 20 77 69 6c file. This wil
161b3 6c 0a 2a 2a 20 61 6c 73 6f 20 61 63 71 75 69 72 l.** also acquir
161b4 65 20 61 20 72 65 61 64 6c 6f 63 6b 20 6f 6e 20 e a readlock on
161b5 74 68 61 74 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a that file..**.**
161b6 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
161b7 74 75 72 6e 65 64 20 6f 6e 20 73 75 63 63 65 73 turned on succes
161b8 73 2e 20 20 49 66 20 74 68 65 20 66 69 6c 65 20 s. If the file
161b9 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 77 65 6c 6c is not a.** well
161ba 2d 66 6f 72 6d 65 64 20 64 61 74 61 62 61 73 65 -formed database
161bb 20 66 69 6c 65 2c 20 74 68 65 6e 20 53 51 4c 49 file, then SQLI
161bc 54 45 5f 43 4f 52 52 55 50 54 20 69 73 20 72 65 TE_CORRUPT is re
161bd 74 75 72 6e 65 64 2e 0a 2a 2a 20 53 51 4c 49 54 turned..** SQLIT
161be 45 5f 42 55 53 59 20 69 73 20 72 65 74 75 72 6e E_BUSY is return
161bf 65 64 20 69 66 20 74 68 65 20 64 61 74 61 62 61 ed if the databa
161c0 73 65 20 69 73 20 6c 6f 63 6b 65 64 2e 20 20 53 se is locked. S
161c1 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20 69 QLITE_NOMEM.** i
161c2 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 77 65 s returned if we
161c3 20 72 75 6e 20 6f 75 74 20 6f 66 20 6d 65 6d 6f run out of memo
161c4 72 79 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ry. .*/.static i
161c5 6e 74 20 6c 6f 63 6b 42 74 72 65 65 28 42 74 53 nt lockBtree(BtS
161c6 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 69 hared *pBt){. i
161c7 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 50 61 67 65 nt rc;. MemPage
161c8 20 2a 70 50 61 67 65 31 3b 0a 20 20 69 6e 74 20 *pPage1;. int
161c9 6e 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 nPage;.. assert
161ca 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
161cb 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 held(pBt->mutex)
161cc 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 );. assert( pB
161cd 74 2d 3e 70 50 61 67 65 31 3d 3d 30 20 29 3b 0a t->pPage1==0 );.
161ce 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
161cf 67 65 72 53 68 61 72 65 64 4c 6f 63 6b 28 70 42 gerSharedLock(pB
161d0 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 69 66 t->pPager);. if
161d1 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
161d2 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 ) return rc;. r
161d3 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 c = btreeGetPage
161d4 28 70 42 74 2c 20 31 2c 20 26 70 50 61 67 65 31 (pBt, 1, &pPage1
161d5 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 21 3d , 0);. if( rc!=
161d6 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 SQLITE_OK ) retu
161d7 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 44 6f 20 rn rc;.. /* Do
161d8 73 6f 6d 65 20 63 68 65 63 6b 69 6e 67 20 74 6f some checking to
161d9 20 68 65 6c 70 20 69 6e 73 75 72 65 20 74 68 65 help insure the
161da 20 66 69 6c 65 20 77 65 20 6f 70 65 6e 65 64 20 file we opened
161db 72 65 61 6c 6c 79 20 69 73 0a 20 20 2a 2a 20 61 really is. ** a
161dc 20 76 61 6c 69 64 20 64 61 74 61 62 61 73 65 20 valid database
161dd 66 69 6c 65 2e 20 0a 20 20 2a 2f 0a 20 20 72 63 file. . */. rc
161de 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 = sqlite3PagerP
161df 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 agecount(pBt->pP
161e0 61 67 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a 20 ager, &nPage);.
161e1 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
161e2 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 70 OK ){. goto p
161e3 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 age1_init_failed
161e4 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 50 ;. }else if( nP
161e5 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 age>0 ){. int
161e6 20 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 69 pageSize;. i
161e7 6e 74 20 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 nt usableSize;.
161e8 20 20 20 75 38 20 2a 70 61 67 65 31 20 3d 20 70 u8 *page1 = p
161e9 50 61 67 65 31 2d 3e 61 44 61 74 61 3b 0a 20 20 Page1->aData;.
161ea 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f rc = SQLITE_NO
161eb 54 41 44 42 3b 0a 20 20 20 20 69 66 28 20 6d 65 TADB;. if( me
161ec 6d 63 6d 70 28 70 61 67 65 31 2c 20 7a 4d 61 67 mcmp(page1, zMag
161ed 69 63 48 65 61 64 65 72 2c 20 31 36 29 21 3d 30 icHeader, 16)!=0
161ee 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 ){. goto p
161ef 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 age1_init_failed
161f0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
161f1 70 61 67 65 31 5b 31 38 5d 3e 31 20 29 7b 0a 20 page1[18]>1 ){.
161f2 20 20 20 20 20 70 42 74 2d 3e 72 65 61 64 4f 6e pBt->readOn
161f3 6c 79 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 ly = 1;. }.
161f4 20 20 69 66 28 20 70 61 67 65 31 5b 31 39 5d 3e if( page1[19]>
161f5 31 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 1 ){. goto
161f6 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 page1_init_faile
161f7 64 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a d;. }.. /*
161f8 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 65 6d 62 The maximum emb
161f9 65 64 64 65 64 20 66 72 61 63 74 69 6f 6e 20 6d edded fraction m
161fa 75 73 74 20 62 65 20 65 78 61 63 74 6c 79 20 32 ust be exactly 2
161fb 35 25 2e 20 20 41 6e 64 20 74 68 65 20 6d 69 6e 5%. And the min
161fc 69 6d 75 6d 0a 20 20 20 20 2a 2a 20 65 6d 62 65 imum. ** embe
161fd 64 64 65 64 20 66 72 61 63 74 69 6f 6e 20 6d 75 dded fraction mu
161fe 73 74 20 62 65 20 31 32 2e 35 25 20 66 6f 72 20 st be 12.5% for
161ff 62 6f 74 68 20 6c 65 61 66 2d 64 61 74 61 20 61 both leaf-data a
16200 6e 64 20 6e 6f 6e 2d 6c 65 61 66 2d 64 61 74 61 nd non-leaf-data
16201 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 6f 72 69 .. ** The ori
16202 67 69 6e 61 6c 20 64 65 73 69 67 6e 20 61 6c 6c ginal design all
16203 6f 77 65 64 20 74 68 65 73 65 20 61 6d 6f 75 6e owed these amoun
16204 74 73 20 74 6f 20 76 61 72 79 2c 20 62 75 74 20 ts to vary, but
16205 61 73 20 6f 66 0a 20 20 20 20 2a 2a 20 76 65 72 as of. ** ver
16206 73 69 6f 6e 20 33 2e 36 2e 30 2c 20 77 65 20 72 sion 3.6.0, we r
16207 65 71 75 69 72 65 20 74 68 65 6d 20 74 6f 20 62 equire them to b
16208 65 20 66 69 78 65 64 2e 0a 20 20 20 20 2a 2f 0a e fixed.. */.
16209 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 26 if( memcmp(&
1620a 70 61 67 65 31 5b 32 31 5d 2c 20 22 5c 31 30 30 page1[21], "\100
1620b 5c 30 34 30 5c 30 34 30 22 2c 33 29 21 3d 30 20 \040\040",3)!=0
1620c 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 ){. goto pa
1620d 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b ge1_init_failed;
1620e 0a 20 20 20 20 7d 0a 20 20 20 20 70 61 67 65 53 . }. pageS
1620f 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28 26 ize = get2byte(&
16210 70 61 67 65 31 5b 31 36 5d 29 3b 0a 20 20 20 20 page1[16]);.
16211 69 66 28 20 28 28 70 61 67 65 53 69 7a 65 2d 31 if( ((pageSize-1
16212 29 26 70 61 67 65 53 69 7a 65 29 21 3d 30 20 7c )&pageSize)!=0 |
16213 7c 20 70 61 67 65 53 69 7a 65 3c 35 31 32 20 7c | pageSize<512 |
16214 7c 0a 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 |. (SQLIT
16215 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 3c E_MAX_PAGE_SIZE<
16216 33 32 37 36 38 20 26 26 20 70 61 67 65 53 69 7a 32768 && pageSiz
16217 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 e>SQLITE_MAX_PAG
16218 45 5f 53 49 5a 45 29 0a 20 20 20 20 29 7b 0a 20 E_SIZE). ){.
16219 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f goto page1_
1621a 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 init_failed;.
1621b 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 }. assert( (
1621c 70 61 67 65 53 69 7a 65 20 26 20 37 29 3d 3d 30 pageSize & 7)==0
1621d 20 29 3b 0a 20 20 20 20 75 73 61 62 6c 65 53 69 );. usableSi
1621e 7a 65 20 3d 20 70 61 67 65 53 69 7a 65 20 2d 20 ze = pageSize -
1621f 70 61 67 65 31 5b 32 30 5d 3b 0a 20 20 20 20 69 page1[20];. i
16220 66 28 20 70 61 67 65 53 69 7a 65 21 3d 70 42 74 f( pageSize!=pBt
16221 2d 3e 70 61 67 65 53 69 7a 65 20 29 7b 0a 20 20 ->pageSize ){.
16222 20 20 20 20 2f 2a 20 41 66 74 65 72 20 72 65 61 /* After rea
16223 64 69 6e 67 20 74 68 65 20 66 69 72 73 74 20 70 ding the first p
16224 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 age of the datab
16225 61 73 65 20 61 73 73 75 6d 69 6e 67 20 61 20 70 ase assuming a p
16226 61 67 65 20 73 69 7a 65 0a 20 20 20 20 20 20 2a age size. *
16227 2a 20 6f 66 20 42 74 53 68 61 72 65 64 2e 70 61 * of BtShared.pa
16228 67 65 53 69 7a 65 2c 20 77 65 20 68 61 76 65 20 geSize, we have
16229 64 69 73 63 6f 76 65 72 65 64 20 74 68 61 74 20 discovered that
1622a 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 69 73 the page-size is
1622b 0a 20 20 20 20 20 20 2a 2a 20 61 63 74 75 61 6c . ** actual
1622c 6c 79 20 70 61 67 65 53 69 7a 65 2e 20 55 6e 6c ly pageSize. Unl
1622d 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 ock the database
1622e 2c 20 6c 65 61 76 65 20 70 42 74 2d 3e 70 50 61 , leave pBt->pPa
1622f 67 65 31 20 61 74 0a 20 20 20 20 20 20 2a 2a 20 ge1 at. **
16230 7a 65 72 6f 20 61 6e 64 20 72 65 74 75 72 6e 20 zero and return
16231 53 51 4c 49 54 45 5f 4f 4b 2e 20 54 68 65 20 63 SQLITE_OK. The c
16232 61 6c 6c 65 72 20 77 69 6c 6c 20 63 61 6c 6c 20 aller will call
16233 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 20 20 this function.
16234 20 20 20 20 2a 2a 20 61 67 61 69 6e 20 77 69 74 ** again wit
16235 68 20 74 68 65 20 63 6f 72 72 65 63 74 20 70 61 h the correct pa
16236 67 65 2d 73 69 7a 65 2e 0a 20 20 20 20 20 20 2a ge-size.. *
16237 2f 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 /. releaseP
16238 61 67 65 28 70 50 61 67 65 31 29 3b 0a 20 20 20 age(pPage1);.
16239 20 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 pBt->usableSi
1623a 7a 65 20 3d 20 28 75 31 36 29 75 73 61 62 6c 65 ze = (u16)usable
1623b 53 69 7a 65 3b 0a 20 20 20 20 20 20 70 42 74 2d Size;. pBt-
1623c 3e 70 61 67 65 53 69 7a 65 20 3d 20 28 75 31 36 >pageSize = (u16
1623d 29 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 20 )pageSize;.
1623e 20 66 72 65 65 54 65 6d 70 53 70 61 63 65 28 70 freeTempSpace(p
1623f 42 74 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 Bt);. rc =
16240 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 sqlite3PagerSetP
16241 61 67 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 agesize(pBt->pPa
16242 67 65 72 2c 20 26 70 42 74 2d 3e 70 61 67 65 53 ger, &pBt->pageS
16243 69 7a 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ize,.
16244 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16245 20 20 20 20 20 20 20 20 70 61 67 65 53 69 7a 65 pageSize
16246 2d 75 73 61 62 6c 65 53 69 7a 65 29 3b 0a 20 20 -usableSize);.
16247 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
16248 20 20 20 7d 0a 20 20 20 20 69 66 28 20 75 73 61 }. if( usa
16249 62 6c 65 53 69 7a 65 3c 34 38 30 20 29 7b 0a 20 bleSize<480 ){.
1624a 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f goto page1_
1624b 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 init_failed;.
1624c 20 7d 0a 20 20 20 20 70 42 74 2d 3e 70 61 67 65 }. pBt->page
1624d 53 69 7a 65 20 3d 20 28 75 31 36 29 70 61 67 65 Size = (u16)page
1624e 53 69 7a 65 3b 0a 20 20 20 20 70 42 74 2d 3e 75 Size;. pBt->u
1624f 73 61 62 6c 65 53 69 7a 65 20 3d 20 28 75 31 36 sableSize = (u16
16250 29 75 73 61 62 6c 65 53 69 7a 65 3b 0a 23 69 66 )usableSize;.#if
16251 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
16252 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 _AUTOVACUUM.
16253 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 pBt->autoVacuum
16254 3d 20 28 67 65 74 34 62 79 74 65 28 26 70 61 67 = (get4byte(&pag
16255 65 31 5b 33 36 20 2b 20 34 2a 34 5d 29 3f 31 3a e1[36 + 4*4])?1:
16256 30 29 3b 0a 20 20 20 20 70 42 74 2d 3e 69 6e 63 0);. pBt->inc
16257 72 56 61 63 75 75 6d 20 3d 20 28 67 65 74 34 62 rVacuum = (get4b
16258 79 74 65 28 26 70 61 67 65 31 5b 33 36 20 2b 20 yte(&page1[36 +
16259 37 2a 34 5d 29 3f 31 3a 30 29 3b 0a 23 65 6e 64 7*4])?1:0);.#end
1625a 69 66 0a 20 20 7d 0a 0a 20 20 2f 2a 20 6d 61 78 if. }.. /* max
1625b 4c 6f 63 61 6c 20 69 73 20 74 68 65 20 6d 61 78 Local is the max
1625c 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 70 imum amount of p
1625d 61 79 6c 6f 61 64 20 74 6f 20 73 74 6f 72 65 20 ayload to store
1625e 6c 6f 63 61 6c 6c 79 20 66 6f 72 0a 20 20 2a 2a locally for. **
1625f 20 61 20 63 65 6c 6c 2e 20 20 4d 61 6b 65 20 73 a cell. Make s
16260 75 72 65 20 69 74 20 69 73 20 73 6d 61 6c 6c 20 ure it is small
16261 65 6e 6f 75 67 68 20 73 6f 20 74 68 61 74 20 61 enough so that a
16262 74 20 6c 65 61 73 74 20 6d 69 6e 46 61 6e 6f 75 t least minFanou
16263 74 0a 20 20 2a 2a 20 63 65 6c 6c 73 20 63 61 6e t. ** cells can
16264 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20 6f 6e 65 will fit on one
16265 20 70 61 67 65 2e 20 20 57 65 20 61 73 73 75 6d page. We assum
16266 65 20 61 20 31 30 2d 62 79 74 65 20 70 61 67 65 e a 10-byte page
16267 20 68 65 61 64 65 72 2e 0a 20 20 2a 2a 20 42 65 header.. ** Be
16268 73 69 64 65 73 20 74 68 65 20 70 61 79 6c 6f 61 sides the payloa
16269 64 2c 20 74 68 65 20 63 65 6c 6c 20 6d 75 73 74 d, the cell must
1626a 20 73 74 6f 72 65 3a 0a 20 20 2a 2a 20 20 20 20 store:. **
1626b 20 32 2d 62 79 74 65 20 70 6f 69 6e 74 65 72 20 2-byte pointer
1626c 74 6f 20 74 68 65 20 63 65 6c 6c 0a 20 20 2a 2a to the cell. **
1626d 20 20 20 20 20 34 2d 62 79 74 65 20 63 68 69 6c 4-byte chil
1626e 64 20 70 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 20 d pointer. **
1626f 20 20 20 39 2d 62 79 74 65 20 6e 4b 65 79 20 76 9-byte nKey v
16270 61 6c 75 65 0a 20 20 2a 2a 20 20 20 20 20 34 2d alue. ** 4-
16271 62 79 74 65 20 6e 44 61 74 61 20 76 61 6c 75 65 byte nData value
16272 0a 20 20 2a 2a 20 20 20 20 20 34 2d 62 79 74 65 . ** 4-byte
16273 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 70 overflow page p
16274 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 53 6f 20 61 ointer. ** So a
16275 20 63 65 6c 6c 20 63 6f 6e 73 69 73 74 73 20 6f cell consists o
16276 66 20 61 20 32 2d 62 79 74 65 20 70 6f 69 6e 65 f a 2-byte poine
16277 72 2c 20 61 20 68 65 61 64 65 72 20 77 68 69 63 r, a header whic
16278 68 20 69 73 20 61 73 20 6d 75 63 68 20 61 73 0a h is as much as.
16279 20 20 2a 2a 20 31 37 20 62 79 74 65 73 20 6c 6f ** 17 bytes lo
1627a 6e 67 2c 20 30 20 74 6f 20 4e 20 62 79 74 65 73 ng, 0 to N bytes
1627b 20 6f 66 20 70 61 79 6c 6f 61 64 2c 20 61 6e 64 of payload, and
1627c 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 34 20 62 an optional 4 b
1627d 79 74 65 20 6f 76 65 72 66 6c 6f 77 0a 20 20 2a yte overflow. *
1627e 2a 20 70 61 67 65 20 70 6f 69 6e 74 65 72 2e 0a * page pointer..
1627f 20 20 2a 2f 0a 20 20 70 42 74 2d 3e 6d 61 78 4c */. pBt->maxL
16280 6f 63 61 6c 20 3d 20 28 70 42 74 2d 3e 75 73 61 ocal = (pBt->usa
16281 62 6c 65 53 69 7a 65 2d 31 32 29 2a 36 34 2f 32 bleSize-12)*64/2
16282 35 35 20 2d 20 32 33 3b 0a 20 20 70 42 74 2d 3e 55 - 23;. pBt->
16283 6d 69 6e 4c 6f 63 61 6c 20 3d 20 28 70 42 74 2d minLocal = (pBt-
16284 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 32 29 2a >usableSize-12)*
16285 33 32 2f 32 35 35 20 2d 20 32 33 3b 0a 20 20 70 32/255 - 23;. p
16286 42 74 2d 3e 6d 61 78 4c 65 61 66 20 3d 20 70 42 Bt->maxLeaf = pB
16287 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 t->usableSize -
16288 33 35 3b 0a 20 20 70 42 74 2d 3e 6d 69 6e 4c 65 35;. pBt->minLe
16289 61 66 20 3d 20 28 70 42 74 2d 3e 75 73 61 62 6c af = (pBt->usabl
1628a 65 53 69 7a 65 2d 31 32 29 2a 33 32 2f 32 35 35 eSize-12)*32/255
1628b 20 2d 20 32 33 3b 0a 20 20 61 73 73 65 72 74 28 - 23;. assert(
1628c 20 70 42 74 2d 3e 6d 61 78 4c 65 61 66 20 2b 20 pBt->maxLeaf +
1628d 32 33 20 3c 3d 20 4d 58 5f 43 45 4c 4c 5f 53 49 23 <= MX_CELL_SI
1628e 5a 45 28 70 42 74 29 20 29 3b 0a 20 20 70 42 74 ZE(pBt) );. pBt
1628f 2d 3e 70 50 61 67 65 31 20 3d 20 70 50 61 67 65 ->pPage1 = pPage
16290 31 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 1;. return SQLI
16291 54 45 5f 4f 4b 3b 0a 0a 70 61 67 65 31 5f 69 6e TE_OK;..page1_in
16292 69 74 5f 66 61 69 6c 65 64 3a 0a 20 20 72 65 6c it_failed:. rel
16293 65 61 73 65 50 61 67 65 28 70 50 61 67 65 31 29 easePage(pPage1)
16294 3b 0a 20 20 70 42 74 2d 3e 70 50 61 67 65 31 20 ;. pBt->pPage1
16295 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 = 0;. return rc
16296 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 ;.}../*.** If th
16297 65 72 65 20 61 72 65 20 6e 6f 20 6f 75 74 73 74 ere are no outst
16298 61 6e 64 69 6e 67 20 63 75 72 73 6f 72 73 20 61 anding cursors a
16299 6e 64 20 77 65 20 61 72 65 20 6e 6f 74 20 69 6e nd we are not in
1629a 20 74 68 65 20 6d 69 64 64 6c 65 0a 2a 2a 20 6f the middle.** o
1629b 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 f a transaction
1629c 62 75 74 20 74 68 65 72 65 20 69 73 20 61 20 72 but there is a r
1629d 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 ead lock on the
1629e 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 0a 2a database, then.*
1629f 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 75 * this routine u
162a0 6e 72 65 66 73 20 74 68 65 20 66 69 72 73 74 20 nrefs the first
162a1 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 page of the data
162a2 62 61 73 65 20 66 69 6c 65 20 77 68 69 63 68 20 base file which
162a3 0a 2a 2a 20 68 61 73 20 74 68 65 20 65 66 66 65 .** has the effe
162a4 63 74 20 6f 66 20 72 65 6c 65 61 73 69 6e 67 20 ct of releasing
162a5 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a the read lock..*
162a6 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 *.** If there is
162a7 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 a transaction i
162a8 6e 20 70 72 6f 67 72 65 73 73 2c 20 74 68 69 73 n progress, this
162a9 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f routine is a no
162aa 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 -op..*/.static v
162ab 6f 69 64 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 oid unlockBtreeI
162ac 66 55 6e 75 73 65 64 28 42 74 53 68 61 72 65 64 fUnused(BtShared
162ad 20 2a 70 42 74 29 7b 0a 20 20 61 73 73 65 72 74 *pBt){. assert
162ae 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
162af 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 held(pBt->mutex)
162b0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 );. assert( pB
162b1 74 2d 3e 70 43 75 72 73 6f 72 3d 3d 30 20 7c 7c t->pCursor==0 ||
162b2 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 pBt->inTransact
162b3 69 6f 6e 3e 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 ion>TRANS_NONE )
162b4 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 69 6e 54 ;. if( pBt->inT
162b5 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e ransaction==TRAN
162b6 53 5f 4e 4f 4e 45 20 26 26 20 70 42 74 2d 3e 70 S_NONE && pBt->p
162b7 50 61 67 65 31 21 3d 30 20 29 7b 0a 20 20 20 20 Page1!=0 ){.
162b8 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50 61 assert( pBt->pPa
162b9 67 65 31 2d 3e 61 44 61 74 61 20 29 3b 0a 20 20 ge1->aData );.
162ba 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
162bb 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70 3PagerRefcount(p
162bc 42 74 2d 3e 70 50 61 67 65 72 29 3d 3d 31 20 29 Bt->pPager)==1 )
162bd 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42 ;. assert( pB
162be 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 t->pPage1->aData
162bf 20 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 );. releaseP
162c0 61 67 65 28 70 42 74 2d 3e 70 50 61 67 65 31 29 age(pBt->pPage1)
162c1 3b 0a 20 20 20 20 70 42 74 2d 3e 70 50 61 67 65 ;. pBt->pPage
162c2 31 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 1 = 0;. }.}../*
162c3 0a 2a 2a 20 49 66 20 70 42 74 20 70 6f 69 6e 74 .** If pBt point
162c4 73 20 74 6f 20 61 6e 20 65 6d 70 74 79 20 66 69 s to an empty fi
162c5 6c 65 20 74 68 65 6e 20 63 6f 6e 76 65 72 74 20 le then convert
162c6 74 68 61 74 20 65 6d 70 74 79 20 66 69 6c 65 0a that empty file.
162c7 2a 2a 20 69 6e 74 6f 20 61 20 6e 65 77 20 65 6d ** into a new em
162c8 70 74 79 20 64 61 74 61 62 61 73 65 20 62 79 20 pty database by
162c9 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 initializing the
162ca 20 66 69 72 73 74 20 70 61 67 65 20 6f 66 0a 2a first page of.*
162cb 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a * the database..
162cc 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 65 */.static int ne
162cd 77 44 61 74 61 62 61 73 65 28 42 74 53 68 61 72 wDatabase(BtShar
162ce 65 64 20 2a 70 42 74 29 7b 0a 20 20 4d 65 6d 50 ed *pBt){. MemP
162cf 61 67 65 20 2a 70 50 31 3b 0a 20 20 75 6e 73 69 age *pP1;. unsi
162d0 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61 3b gned char *data;
162d1 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 . int rc;. int
162d2 20 6e 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 nPage;.. asser
162d3 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
162d4 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 _held(pBt->mutex
162d5 29 20 29 3b 0a 20 20 2f 2a 20 54 68 65 20 64 61 ) );. /* The da
162d6 74 61 62 61 73 65 20 73 69 7a 65 20 68 61 73 20 tabase size has
162d7 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6d 65 61 already been mea
162d8 73 75 72 65 64 20 61 6e 64 20 63 61 63 68 65 64 sured and cached
162d9 2c 20 73 6f 20 66 61 69 6c 75 72 65 0a 20 20 2a , so failure. *
162da 2a 20 69 73 20 69 6d 70 6f 73 73 69 62 6c 65 20 * is impossible
162db 68 65 72 65 2e 20 20 49 66 20 74 68 65 20 6f 72 here. If the or
162dc 69 67 69 6e 61 6c 20 73 69 7a 65 20 6d 65 61 73 iginal size meas
162dd 75 72 65 6d 65 6e 74 20 66 61 69 6c 65 64 2c 20 urement failed,
162de 74 68 65 6e 0a 20 20 2a 2a 20 70 72 6f 63 65 73 then. ** proces
162df 73 69 6e 67 20 61 62 6f 72 74 73 20 62 65 66 6f sing aborts befo
162e0 72 65 20 65 6e 74 65 72 69 6e 67 20 74 68 69 73 re entering this
162e1 20 72 6f 75 74 69 6e 65 2e 20 2a 2f 0a 20 20 72 routine. */. r
162e2 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
162e3 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 Pagecount(pBt->p
162e4 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a Pager, &nPage);.
162e5 20 20 69 66 28 20 4e 45 56 45 52 28 72 63 21 3d if( NEVER(rc!=
162e6 53 51 4c 49 54 45 5f 4f 4b 29 20 7c 7c 20 6e 50 SQLITE_OK) || nP
162e7 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 72 65 74 age>0 ){. ret
162e8 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 50 urn rc;. }. pP
162e9 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 1 = pBt->pPage1;
162ea 0a 20 20 61 73 73 65 72 74 28 20 70 50 31 21 3d . assert( pP1!=
162eb 30 20 29 3b 0a 20 20 64 61 74 61 20 3d 20 70 50 0 );. data = pP
162ec 31 2d 3e 61 44 61 74 61 3b 0a 20 20 72 63 20 3d 1->aData;. rc =
162ed 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
162ee 74 65 28 70 50 31 2d 3e 70 44 62 50 61 67 65 29 te(pP1->pDbPage)
162ef 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 ;. if( rc ) ret
162f0 75 72 6e 20 72 63 3b 0a 20 20 6d 65 6d 63 70 79 urn rc;. memcpy
162f1 28 64 61 74 61 2c 20 7a 4d 61 67 69 63 48 65 61 (data, zMagicHea
162f2 64 65 72 2c 20 73 69 7a 65 6f 66 28 7a 4d 61 67 der, sizeof(zMag
162f3 69 63 48 65 61 64 65 72 29 29 3b 0a 20 20 61 73 icHeader));. as
162f4 73 65 72 74 28 20 73 69 7a 65 6f 66 28 7a 4d 61 sert( sizeof(zMa
162f5 67 69 63 48 65 61 64 65 72 29 3d 3d 31 36 20 29 gicHeader)==16 )
162f6 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 ;. put2byte(&da
162f7 74 61 5b 31 36 5d 2c 20 70 42 74 2d 3e 70 61 67 ta[16], pBt->pag
162f8 65 53 69 7a 65 29 3b 0a 20 20 64 61 74 61 5b 31 eSize);. data[1
162f9 38 5d 20 3d 20 31 3b 0a 20 20 64 61 74 61 5b 31 8] = 1;. data[1
162fa 39 5d 20 3d 20 31 3b 0a 20 20 61 73 73 65 72 74 9] = 1;. assert
162fb 28 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a ( pBt->usableSiz
162fc 65 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 e<=pBt->pageSize
162fd 20 26 26 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 && pBt->usableS
162fe 69 7a 65 2b 32 35 35 3e 3d 70 42 74 2d 3e 70 61 ize+255>=pBt->pa
162ff 67 65 53 69 7a 65 29 3b 0a 20 20 64 61 74 61 5b geSize);. data[
16300 32 30 5d 20 3d 20 28 75 38 29 28 70 42 74 2d 3e 20] = (u8)(pBt->
16301 70 61 67 65 53 69 7a 65 20 2d 20 70 42 74 2d 3e pageSize - pBt->
16302 75 73 61 62 6c 65 53 69 7a 65 29 3b 0a 20 20 64 usableSize);. d
16303 61 74 61 5b 32 31 5d 20 3d 20 36 34 3b 0a 20 20 ata[21] = 64;.
16304 64 61 74 61 5b 32 32 5d 20 3d 20 33 32 3b 0a 20 data[22] = 32;.
16305 20 64 61 74 61 5b 32 33 5d 20 3d 20 33 32 3b 0a data[23] = 32;.
16306 20 20 6d 65 6d 73 65 74 28 26 64 61 74 61 5b 32 memset(&data[2
16307 34 5d 2c 20 30 2c 20 31 30 30 2d 32 34 29 3b 0a 4], 0, 100-24);.
16308 20 20 7a 65 72 6f 50 61 67 65 28 70 50 31 2c 20 zeroPage(pP1,
16309 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46 5f 4c PTF_INTKEY|PTF_L
1630a 45 41 46 7c 50 54 46 5f 4c 45 41 46 44 41 54 41 EAF|PTF_LEAFDATA
1630b 20 29 3b 0a 20 20 70 42 74 2d 3e 70 61 67 65 53 );. pBt->pageS
1630c 69 7a 65 46 69 78 65 64 20 3d 20 31 3b 0a 23 69 izeFixed = 1;.#i
1630d 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1630e 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 61 T_AUTOVACUUM. a
1630f 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75 74 6f ssert( pBt->auto
16310 56 61 63 75 75 6d 3d 3d 31 20 7c 7c 20 70 42 74 Vacuum==1 || pBt
16311 2d 3e 61 75 74 6f 56 61 63 75 75 6d 3d 3d 30 20 ->autoVacuum==0
16312 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 );. assert( pBt
16313 2d 3e 69 6e 63 72 56 61 63 75 75 6d 3d 3d 31 20 ->incrVacuum==1
16314 7c 7c 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 || pBt->incrVacu
16315 75 6d 3d 3d 30 20 29 3b 0a 20 20 70 75 74 34 62 um==0 );. put4b
16316 79 74 65 28 26 64 61 74 61 5b 33 36 20 2b 20 34 yte(&data[36 + 4
16317 2a 34 5d 2c 20 70 42 74 2d 3e 61 75 74 6f 56 61 *4], pBt->autoVa
16318 63 75 75 6d 29 3b 0a 20 20 70 75 74 34 62 79 74 cuum);. put4byt
16319 65 28 26 64 61 74 61 5b 33 36 20 2b 20 37 2a 34 e(&data[36 + 7*4
1631a 5d 2c 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 ], pBt->incrVacu
1631b 75 6d 29 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 um);.#endif. re
1631c 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1631d 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 }../*.** Attempt
1631e 20 74 6f 20 73 74 61 72 74 20 61 20 6e 65 77 20 to start a new
1631f 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 41 20 77 transaction. A w
16320 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e rite-transaction
16321 0a 2a 2a 20 69 73 20 73 74 61 72 74 65 64 20 69 .** is started i
16322 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 f the second arg
16323 75 6d 65 6e 74 20 69 73 20 6e 6f 6e 7a 65 72 6f ument is nonzero
16324 2c 20 6f 74 68 65 72 77 69 73 65 20 61 20 72 65 , otherwise a re
16325 61 64 2d 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 ad-.** transacti
16326 6f 6e 2e 20 20 49 66 20 74 68 65 20 73 65 63 6f on. If the seco
16327 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 32 nd argument is 2
16328 20 6f 72 20 6d 6f 72 65 20 61 6e 64 20 65 78 63 or more and exc
16329 6c 75 73 69 76 65 0a 2a 2a 20 74 72 61 6e 73 61 lusive.** transa
1632a 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74 65 64 ction is started
1632b 2c 20 6d 65 61 6e 69 6e 67 20 74 68 61 74 20 6e , meaning that n
1632c 6f 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 o other process
1632d 69 73 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 6f is allowed.** to
1632e 20 61 63 63 65 73 73 20 74 68 65 20 64 61 74 61 access the data
1632f 62 61 73 65 2e 20 20 41 20 70 72 65 65 78 69 73 base. A preexis
16330 74 69 6e 67 20 74 72 61 6e 73 61 63 74 69 6f 6e ting transaction
16331 20 6d 61 79 20 6e 6f 74 20 62 65 0a 2a 2a 20 75 may not be.** u
16332 70 67 72 61 64 65 64 20 74 6f 20 65 78 63 6c 75 pgraded to exclu
16333 73 69 76 65 20 62 79 20 63 61 6c 6c 69 6e 67 20 sive by calling
16334 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 20 73 this routine a s
16335 65 63 6f 6e 64 20 74 69 6d 65 20 2d 20 74 68 65 econd time - the
16336 0a 2a 2a 20 65 78 63 6c 75 73 69 76 69 74 79 20 .** exclusivity
16337 66 6c 61 67 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 flag only works
16338 66 6f 72 20 61 20 6e 65 77 20 74 72 61 6e 73 61 for a new transa
16339 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 20 77 ction..**.** A w
1633a 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e rite-transaction
1633b 20 6d 75 73 74 20 62 65 20 73 74 61 72 74 65 64 must be started
1633c 20 62 65 66 6f 72 65 20 61 74 74 65 6d 70 74 69 before attempti
1633d 6e 67 20 61 6e 79 20 0a 2a 2a 20 63 68 61 6e 67 ng any .** chang
1633e 65 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 es to the databa
1633f 73 65 2e 20 20 4e 6f 6e 65 20 6f 66 20 74 68 65 se. None of the
16340 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 following routi
16341 6e 65 73 20 0a 2a 2a 20 77 69 6c 6c 20 77 6f 72 nes .** will wor
16342 6b 20 75 6e 6c 65 73 73 20 61 20 74 72 61 6e 73 k unless a trans
16343 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74 65 action is starte
16344 64 20 66 69 72 73 74 3a 0a 2a 2a 0a 2a 2a 20 20 d first:.**.**
16345 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
16346 43 72 65 61 74 65 54 61 62 6c 65 28 29 0a 2a 2a CreateTable().**
16347 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 sqlite3Btr
16348 65 65 43 72 65 61 74 65 49 6e 64 65 78 28 29 0a eeCreateIndex().
16349 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 ** sqlite3B
1634a 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65 28 29 treeClearTable()
1634b 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 .** sqlite3
1634c 42 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28 29 BtreeDropTable()
1634d 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 .** sqlite3
1634e 42 74 72 65 65 49 6e 73 65 72 74 28 29 0a 2a 2a BtreeInsert().**
1634f 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 sqlite3Btr
16350 65 65 44 65 6c 65 74 65 28 29 0a 2a 2a 20 20 20 eeDelete().**
16351 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 sqlite3BtreeU
16352 70 64 61 74 65 4d 65 74 61 28 29 0a 2a 2a 0a 2a pdateMeta().**.*
16353 2a 20 49 66 20 61 6e 20 69 6e 69 74 69 61 6c 20 * If an initial
16354 61 74 74 65 6d 70 74 20 74 6f 20 61 63 71 75 69 attempt to acqui
16355 72 65 20 74 68 65 20 6c 6f 63 6b 20 66 61 69 6c re the lock fail
16356 73 20 62 65 63 61 75 73 65 20 6f 66 20 6c 6f 63 s because of loc
16357 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 0a 2a 2a 20 k contention.**
16358 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 65 and the database
16359 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 was previously
1635a 75 6e 6c 6f 63 6b 65 64 2c 20 74 68 65 6e 20 69 unlocked, then i
1635b 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 68 nvoke the busy h
1635c 61 6e 64 6c 65 72 0a 2a 2a 20 69 66 20 74 68 65 andler.** if the
1635d 72 65 20 69 73 20 6f 6e 65 2e 20 20 42 75 74 20 re is one. But
1635e 69 66 20 74 68 65 72 65 20 77 61 73 20 70 72 65 if there was pre
1635f 76 69 6f 75 73 6c 79 20 61 20 72 65 61 64 2d 6c viously a read-l
16360 6f 63 6b 2c 20 64 6f 20 6e 6f 74 0a 2a 2a 20 69 ock, do not.** i
16361 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 68 nvoke the busy h
16362 61 6e 64 6c 65 72 20 2d 20 6a 75 73 74 20 72 65 andler - just re
16363 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 turn SQLITE_BUSY
16364 2e 20 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 . SQLITE_BUSY i
16365 73 20 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 77 s .** returned w
16366 68 65 6e 20 74 68 65 72 65 20 69 73 20 61 6c 72 hen there is alr
16367 65 61 64 79 20 61 20 72 65 61 64 2d 6c 6f 63 6b eady a read-lock
16368 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 61 76 6f in order to avo
16369 69 64 20 61 20 64 65 61 64 6c 6f 63 6b 2e 0a 2a id a deadlock..*
1636a 2a 0a 2a 2a 20 53 75 70 70 6f 73 65 20 74 68 65 *.** Suppose the
1636b 72 65 20 61 72 65 20 74 77 6f 20 70 72 6f 63 65 re are two proce
1636c 73 73 65 73 20 41 20 61 6e 64 20 42 2e 20 20 41 sses A and B. A
1636d 20 68 61 73 20 61 20 72 65 61 64 20 6c 6f 63 6b has a read lock
1636e 20 61 6e 64 20 42 20 68 61 73 0a 2a 2a 20 61 20 and B has.** a
1636f 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 2e 20 20 reserved lock.
16370 42 20 74 72 69 65 73 20 74 6f 20 70 72 6f 6d 6f B tries to promo
16371 74 65 20 74 6f 20 65 78 63 6c 75 73 69 76 65 20 te to exclusive
16372 62 75 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 but is blocked b
16373 65 63 61 75 73 65 0a 2a 2a 20 6f 66 20 41 27 73 ecause.** of A's
16374 20 72 65 61 64 20 6c 6f 63 6b 2e 20 20 41 20 74 read lock. A t
16375 72 69 65 73 20 74 6f 20 70 72 6f 6d 6f 74 65 20 ries to promote
16376 74 6f 20 72 65 73 65 72 76 65 64 20 62 75 74 20 to reserved but
16377 69 73 20 62 6c 6f 63 6b 65 64 20 62 79 20 42 2e is blocked by B.
16378 0a 2a 2a 20 4f 6e 65 20 6f 72 20 74 68 65 20 6f .** One or the o
16379 74 68 65 72 20 6f 66 20 74 68 65 20 74 77 6f 20 ther of the two
1637a 70 72 6f 63 65 73 73 65 73 20 6d 75 73 74 20 67 processes must g
1637b 69 76 65 20 77 61 79 20 6f 72 20 74 68 65 72 65 ive way or there
1637c 20 63 61 6e 20 62 65 0a 2a 2a 20 6e 6f 20 70 72 can be.** no pr
1637d 6f 67 72 65 73 73 2e 20 20 42 79 20 72 65 74 75 ogress. By retu
1637e 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f 42 55 53 rning SQLITE_BUS
1637f 59 20 61 6e 64 20 6e 6f 74 20 69 6e 76 6f 6b 69 Y and not invoki
16380 6e 67 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c ng the busy call
16381 62 61 63 6b 0a 2a 2a 20 77 68 65 6e 20 41 20 61 back.** when A a
16382 6c 72 65 61 64 79 20 68 61 73 20 61 20 72 65 61 lready has a rea
16383 64 20 6c 6f 63 6b 2c 20 77 65 20 65 6e 63 6f 75 d lock, we encou
16384 72 61 67 65 20 41 20 74 6f 20 67 69 76 65 20 75 rage A to give u
16385 70 20 61 6e 64 20 6c 65 74 20 42 0a 2a 2a 20 70 p and let B.** p
16386 72 6f 63 65 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 roceed..*/.SQLIT
16387 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
16388 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 lite3BtreeBeginT
16389 72 61 6e 73 28 42 74 72 65 65 20 2a 70 2c 20 69 rans(Btree *p, i
1638a 6e 74 20 77 72 66 6c 61 67 29 7b 0a 20 20 73 71 nt wrflag){. sq
1638b 6c 69 74 65 33 20 2a 70 42 6c 6f 63 6b 20 3d 20 lite3 *pBlock =
1638c 30 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 0;. BtShared *p
1638d 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 69 Bt = p->pBt;. i
1638e 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
1638f 4b 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 K;.. sqlite3Btr
16390 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 62 74 eeEnter(p);. bt
16391 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 29 3b reeIntegrity(p);
16392 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 62 74 .. /* If the bt
16393 72 65 65 20 69 73 20 61 6c 72 65 61 64 79 20 69 ree is already i
16394 6e 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 n a write-transa
16395 63 74 69 6f 6e 2c 20 6f 72 20 69 74 0a 20 20 2a ction, or it. *
16396 2a 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 * is already in
16397 61 20 72 65 61 64 2d 74 72 61 6e 73 61 63 74 69 a read-transacti
16398 6f 6e 20 61 6e 64 20 61 20 72 65 61 64 2d 74 72 on and a read-tr
16399 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a 2a 20 69 ansaction. ** i
1639a 73 20 72 65 71 75 65 73 74 65 64 2c 20 74 68 69 s requested, thi
1639b 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 s is a no-op..
1639c 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 */. if( p->inTr
1639d 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ans==TRANS_WRITE
1639e 20 7c 7c 20 28 70 2d 3e 69 6e 54 72 61 6e 73 3d || (p->inTrans=
1639f 3d 54 52 41 4e 53 5f 52 45 41 44 20 26 26 20 21 =TRANS_READ && !
163a0 77 72 66 6c 61 67 29 20 29 7b 0a 20 20 20 20 67 wrflag) ){. g
163a1 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75 6e 3b oto trans_begun;
163a2 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 72 69 74 65 . }.. /* Write
163a3 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 61 72 transactions ar
163a4 65 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 6f e not possible o
163a5 6e 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 64 61 n a read-only da
163a6 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 66 28 20 tabase */. if(
163a7 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 26 26 pBt->readOnly &&
163a8 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 20 72 wrflag ){. r
163a9 63 20 3d 20 53 51 4c 49 54 45 5f 52 45 41 44 4f c = SQLITE_READO
163aa 4e 4c 59 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72 NLY;. goto tr
163ab 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 7d 0a 0a ans_begun;. }..
163ac 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
163ad 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 MIT_SHARED_CACHE
163ae 0a 20 20 2f 2a 20 49 66 20 61 6e 6f 74 68 65 72 . /* If another
163af 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 database handle
163b0 20 68 61 73 20 61 6c 72 65 61 64 79 20 6f 70 65 has already ope
163b1 6e 65 64 20 61 20 77 72 69 74 65 20 74 72 61 6e ned a write tran
163b2 73 61 63 74 69 6f 6e 20 0a 20 20 2a 2a 20 6f 6e saction . ** on
163b3 20 74 68 69 73 20 73 68 61 72 65 64 2d 62 74 72 this shared-btr
163b4 65 65 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 ee structure and
163b5 20 61 20 73 65 63 6f 6e 64 20 77 72 69 74 65 20 a second write
163b6 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 20 transaction is.
163b7 20 2a 2a 20 72 65 71 75 65 73 74 65 64 2c 20 72 ** requested, r
163b8 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 eturn SQLITE_LOC
163b9 4b 45 44 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 KED.. */. if(
163ba 28 77 72 66 6c 61 67 20 26 26 20 70 42 74 2d 3e (wrflag && pBt->
163bb 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 inTransaction==T
163bc 52 41 4e 53 5f 57 52 49 54 45 29 20 7c 7c 20 70 RANS_WRITE) || p
163bd 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20 29 7b Bt->isPending ){
163be 0a 20 20 20 20 70 42 6c 6f 63 6b 20 3d 20 70 42 . pBlock = pB
163bf 74 2d 3e 70 57 72 69 74 65 72 2d 3e 64 62 3b 0a t->pWriter->db;.
163c0 20 20 7d 65 6c 73 65 20 69 66 28 20 77 72 66 6c }else if( wrfl
163c1 61 67 3e 31 20 29 7b 0a 20 20 20 20 42 74 4c 6f ag>1 ){. BtLo
163c2 63 6b 20 2a 70 49 74 65 72 3b 0a 20 20 20 20 66 ck *pIter;. f
163c3 6f 72 28 70 49 74 65 72 3d 70 42 74 2d 3e 70 4c or(pIter=pBt->pL
163c4 6f 63 6b 3b 20 70 49 74 65 72 3b 20 70 49 74 65 ock; pIter; pIte
163c5 72 3d 70 49 74 65 72 2d 3e 70 4e 65 78 74 29 7b r=pIter->pNext){
163c6 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 65 72 . if( pIter
163c7 2d 3e 70 42 74 72 65 65 21 3d 70 20 29 7b 0a 20 ->pBtree!=p ){.
163c8 20 20 20 20 20 20 20 70 42 6c 6f 63 6b 20 3d 20 pBlock =
163c9 70 49 74 65 72 2d 3e 70 42 74 72 65 65 2d 3e 64 pIter->pBtree->d
163ca 62 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b b;. break
163cb 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
163cc 20 20 7d 0a 20 20 69 66 28 20 70 42 6c 6f 63 6b }. if( pBlock
163cd 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 ){. sqlite3C
163ce 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 onnectionBlocked
163cf 28 70 2d 3e 64 62 2c 20 70 42 6c 6f 63 6b 29 3b (p->db, pBlock);
163d0 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
163d1 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 _LOCKED_SHAREDCA
163d2 43 48 45 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72 CHE;. goto tr
163d3 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 7d 0a 23 ans_begun;. }.#
163d4 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 6e 79 20 endif.. /* Any
163d5 72 65 61 64 2d 6f 6e 6c 79 20 6f 72 20 72 65 61 read-only or rea
163d6 64 2d 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 d-write transact
163d7 69 6f 6e 20 69 6d 70 6c 69 65 73 20 61 20 72 65 ion implies a re
163d8 61 64 2d 6c 6f 63 6b 20 6f 6e 20 0a 20 20 2a 2a ad-lock on . **
163d9 20 70 61 67 65 20 31 2e 20 53 6f 20 69 66 20 73 page 1. So if s
163da 6f 6d 65 20 6f 74 68 65 72 20 73 68 61 72 65 64 ome other shared
163db 2d 63 61 63 68 65 20 63 6c 69 65 6e 74 20 61 6c -cache client al
163dc 72 65 61 64 79 20 68 61 73 20 61 20 77 72 69 74 ready has a writ
163dd 65 2d 6c 6f 63 6b 20 0a 20 20 2a 2a 20 6f 6e 20 e-lock . ** on
163de 70 61 67 65 20 31 2c 20 74 68 65 20 74 72 61 6e page 1, the tran
163df 73 61 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 62 saction cannot b
163e0 65 20 6f 70 65 6e 65 64 2e 20 2a 2f 0a 20 20 72 e opened. */. r
163e1 63 20 3d 20 71 75 65 72 79 53 68 61 72 65 64 43 c = querySharedC
163e2 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c acheTableLock(p,
163e3 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 52 45 MASTER_ROOT, RE
163e4 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 69 66 28 20 AD_LOCK);. if(
163e5 53 51 4c 49 54 45 5f 4f 4b 21 3d 72 63 20 29 20 SQLITE_OK!=rc )
163e6 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75 6e goto trans_begun
163e7 3b 0a 0a 20 20 64 6f 20 7b 0a 20 20 20 20 2f 2a ;.. do {. /*
163e8 20 43 61 6c 6c 20 6c 6f 63 6b 42 74 72 65 65 28 Call lockBtree(
163e9 29 20 75 6e 74 69 6c 20 65 69 74 68 65 72 20 70 ) until either p
163ea 42 74 2d 3e 70 50 61 67 65 31 20 69 73 20 70 6f Bt->pPage1 is po
163eb 70 75 6c 61 74 65 64 20 6f 72 0a 20 20 20 20 2a pulated or. *
163ec 2a 20 6c 6f 63 6b 42 74 72 65 65 28 29 20 72 65 * lockBtree() re
163ed 74 75 72 6e 73 20 73 6f 6d 65 74 68 69 6e 67 20 turns something
163ee 6f 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 other than SQLIT
163ef 45 5f 4f 4b 2e 20 6c 6f 63 6b 42 74 72 65 65 28 E_OK. lockBtree(
163f0 29 0a 20 20 20 20 2a 2a 20 6d 61 79 20 72 65 74 ). ** may ret
163f1 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 62 75 urn SQLITE_OK bu
163f2 74 20 6c 65 61 76 65 20 70 42 74 2d 3e 70 50 61 t leave pBt->pPa
163f3 67 65 31 20 73 65 74 20 74 6f 20 30 20 69 66 20 ge1 set to 0 if
163f4 61 66 74 65 72 0a 20 20 20 20 2a 2a 20 72 65 61 after. ** rea
163f5 64 69 6e 67 20 70 61 67 65 20 31 20 69 74 20 64 ding page 1 it d
163f6 69 73 63 6f 76 65 72 73 20 74 68 61 74 20 74 68 iscovers that th
163f7 65 20 70 61 67 65 2d 73 69 7a 65 20 6f 66 20 74 e page-size of t
163f8 68 65 20 64 61 74 61 62 61 73 65 20 0a 20 20 20 he database .
163f9 20 2a 2a 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 ** file is not
163fa 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2e 20 49 pBt->pageSize. I
163fb 6e 20 74 68 69 73 20 63 61 73 65 20 6c 6f 63 6b n this case lock
163fc 42 74 72 65 65 28 29 20 77 69 6c 6c 20 75 70 64 Btree() will upd
163fd 61 74 65 0a 20 20 20 20 2a 2a 20 70 42 74 2d 3e ate. ** pBt->
163fe 70 61 67 65 53 69 7a 65 20 74 6f 20 74 68 65 20 pageSize to the
163ff 70 61 67 65 2d 73 69 7a 65 20 6f 66 20 74 68 65 page-size of the
16400 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e 0a 20 file on disk..
16401 20 20 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 */. while(
16402 20 70 42 74 2d 3e 70 50 61 67 65 31 3d 3d 30 20 pBt->pPage1==0
16403 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 && SQLITE_OK==(r
16404 63 20 3d 20 6c 6f 63 6b 42 74 72 65 65 28 70 42 c = lockBtree(pB
16405 74 29 29 20 29 3b 0a 0a 20 20 20 20 69 66 28 20 t)) );.. if(
16406 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
16407 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 20 20 wrflag ){.
16408 20 69 66 28 20 70 42 74 2d 3e 72 65 61 64 4f 6e if( pBt->readOn
16409 6c 79 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 ly ){. rc
1640a 20 3d 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e = SQLITE_READON
1640b 4c 59 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b LY;. }else{
1640c 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
1640d 6c 69 74 65 33 50 61 67 65 72 42 65 67 69 6e 28 lite3PagerBegin(
1640e 70 42 74 2d 3e 70 50 61 67 65 72 2c 77 72 66 6c pBt->pPager,wrfl
1640f 61 67 3e 31 2c 73 71 6c 69 74 65 33 54 65 6d 70 ag>1,sqlite3Temp
16410 49 6e 4d 65 6d 6f 72 79 28 70 2d 3e 64 62 29 29 InMemory(p->db))
16411 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 ;. if( rc
16412 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
16413 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 6e 65 rc = ne
16414 77 44 61 74 61 62 61 73 65 28 70 42 74 29 3b 0a wDatabase(pBt);.
16415 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
16416 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 69 }. }. . i
16417 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
16418 20 29 7b 0a 20 20 20 20 20 20 75 6e 6c 6f 63 6b ){. unlock
16419 42 74 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 BtreeIfUnused(pB
1641a 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 77 68 69 t);. }. }whi
1641b 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 le( rc==SQLITE_B
1641c 55 53 59 20 26 26 20 70 42 74 2d 3e 69 6e 54 72 USY && pBt->inTr
1641d 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 ansaction==TRANS
1641e 5f 4e 4f 4e 45 20 26 26 0a 20 20 20 20 20 20 20 _NONE &&.
1641f 20 20 20 62 74 72 65 65 49 6e 76 6f 6b 65 42 75 btreeInvokeBu
16420 73 79 48 61 6e 64 6c 65 72 28 70 42 74 29 20 29 syHandler(pBt) )
16421 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c ;.. if( rc==SQL
16422 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 ITE_OK ){. if
16423 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 ( p->inTrans==TR
16424 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 ANS_NONE ){.
16425 20 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 pBt->nTransact
16426 69 6f 6e 2b 2b 3b 0a 23 69 66 6e 64 65 66 20 53 ion++;.#ifndef S
16427 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
16428 44 5f 43 41 43 48 45 0a 20 20 20 20 20 20 69 66 D_CACHE. if
16429 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b ( p->sharable ){
1642a 0a 09 61 73 73 65 72 74 28 20 70 2d 3e 6c 6f 63 ..assert( p->loc
1642b 6b 2e 70 42 74 72 65 65 3d 3d 70 20 26 26 20 70 k.pBtree==p && p
1642c 2d 3e 6c 6f 63 6b 2e 69 54 61 62 6c 65 3d 3d 31 ->lock.iTable==1
1642d 20 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6c );. p->l
1642e 6f 63 6b 2e 65 4c 6f 63 6b 20 3d 20 52 45 41 44 ock.eLock = READ
1642f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 20 20 70 _LOCK;. p
16430 2d 3e 6c 6f 63 6b 2e 70 4e 65 78 74 20 3d 20 70 ->lock.pNext = p
16431 42 74 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 20 20 20 Bt->pLock;.
16432 20 20 20 70 42 74 2d 3e 70 4c 6f 63 6b 20 3d 20 pBt->pLock =
16433 26 70 2d 3e 6c 6f 63 6b 3b 0a 20 20 20 20 20 20 &p->lock;.
16434 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 }.#endif. }.
16435 20 20 20 70 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 p->inTrans =
16436 28 77 72 66 6c 61 67 3f 54 52 41 4e 53 5f 57 52 (wrflag?TRANS_WR
16437 49 54 45 3a 54 52 41 4e 53 5f 52 45 41 44 29 3b ITE:TRANS_READ);
16438 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 . if( p->inTr
16439 61 6e 73 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 ans>pBt->inTrans
1643a 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 action ){.
1643b 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 pBt->inTransacti
1643c 6f 6e 20 3d 20 70 2d 3e 69 6e 54 72 61 6e 73 3b on = p->inTrans;
1643d 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 . }.#ifndef S
1643e 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
1643f 44 5f 43 41 43 48 45 0a 20 20 20 20 69 66 28 20 D_CACHE. if(
16440 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 wrflag ){.
16441 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e 70 57 assert( !pBt->pW
16442 72 69 74 65 72 20 29 3b 0a 20 20 20 20 20 20 70 riter );. p
16443 42 74 2d 3e 70 57 72 69 74 65 72 20 3d 20 70 3b Bt->pWriter = p;
16444 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 73 45 78 . pBt->isEx
16445 63 6c 75 73 69 76 65 20 3d 20 28 75 38 29 28 77 clusive = (u8)(w
16446 72 66 6c 61 67 3e 31 29 3b 0a 20 20 20 20 7d 0a rflag>1);. }.
16447 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 0a 74 72 61 #endif. }...tra
16448 6e 73 5f 62 65 67 75 6e 3a 0a 20 20 69 66 28 20 ns_begun:. if(
16449 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
1644a 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 20 2f wrflag ){. /
1644b 2a 20 54 68 69 73 20 63 61 6c 6c 20 6d 61 6b 65 * This call make
1644c 73 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20 s sure that the
1644d 70 61 67 65 72 20 68 61 73 20 74 68 65 20 63 6f pager has the co
1644e 72 72 65 63 74 20 6e 75 6d 62 65 72 20 6f 66 0a rrect number of.
1644f 20 20 20 20 2a 2a 20 6f 70 65 6e 20 73 61 76 65 ** open save
16450 70 6f 69 6e 74 73 2e 20 49 66 20 74 68 65 20 73 points. If the s
16451 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 econd parameter
16452 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 is greater than
16453 30 20 61 6e 64 0a 20 20 20 20 2a 2a 20 74 68 65 0 and. ** the
16454 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 73 20 sub-journal is
16455 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f 70 65 6e not already open
16456 2c 20 74 68 65 6e 20 69 74 20 77 69 6c 6c 20 62 , then it will b
16457 65 20 6f 70 65 6e 65 64 20 68 65 72 65 2e 0a 20 e opened here..
16458 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 */. rc = s
16459 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 53 qlite3PagerOpenS
1645a 61 76 65 70 6f 69 6e 74 28 70 42 74 2d 3e 70 50 avepoint(pBt->pP
1645b 61 67 65 72 2c 20 70 2d 3e 64 62 2d 3e 6e 53 61 ager, p->db->nSa
1645c 76 65 70 6f 69 6e 74 29 3b 0a 20 20 7d 0a 0a 20 vepoint);. }..
1645d 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 btreeIntegrity(
1645e 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 p);. sqlite3Btr
1645f 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 eeLeave(p);. re
16460 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e turn rc;.}..#ifn
16461 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
16462 41 55 54 4f 56 41 43 55 55 4d 0a 0a 2f 2a 0a 2a AUTOVACUUM../*.*
16463 2a 20 53 65 74 20 74 68 65 20 70 6f 69 6e 74 65 * Set the pointe
16464 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f r-map entries fo
16465 72 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e 20 6f r all children o
16466 66 20 70 61 67 65 20 70 50 61 67 65 2e 20 41 6c f page pPage. Al
16467 73 6f 2c 20 69 66 0a 2a 2a 20 70 50 61 67 65 20 so, if.** pPage
16468 63 6f 6e 74 61 69 6e 73 20 63 65 6c 6c 73 20 74 contains cells t
16469 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 6f 76 65 hat point to ove
1646a 72 66 6c 6f 77 20 70 61 67 65 73 2c 20 73 65 74 rflow pages, set
1646b 20 74 68 65 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 the pointer.**
1646c 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f 72 20 map entries for
1646d 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 the overflow pag
1646e 65 73 20 61 73 20 77 65 6c 6c 2e 0a 2a 2f 0a 73 es as well..*/.s
1646f 74 61 74 69 63 20 69 6e 74 20 73 65 74 43 68 69 tatic int setChi
16470 6c 64 50 74 72 6d 61 70 73 28 4d 65 6d 50 61 67 ldPtrmaps(MemPag
16471 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 69 6e 74 e *pPage){. int
16472 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
16473 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16474 2f 2a 20 43 6f 75 6e 74 65 72 20 76 61 72 69 61 /* Counter varia
16475 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 65 ble */. int nCe
16476 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ll;
16477 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
16478 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 69 umber of cells i
16479 6e 20 70 61 67 65 20 70 50 61 67 65 20 2a 2f 0a n page pPage */.
1647a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 int rc;
1647b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1647c 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 /* Return c
1647d 6f 64 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 ode */. BtShare
1647e 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e d *pBt = pPage->
1647f 70 42 74 3b 0a 20 20 75 38 20 69 73 49 6e 69 74 pBt;. u8 isInit
16480 4f 72 69 67 20 3d 20 70 50 61 67 65 2d 3e 69 73 Orig = pPage->is
16481 49 6e 69 74 3b 0a 20 20 50 67 6e 6f 20 70 67 6e Init;. Pgno pgn
16482 6f 20 3d 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3b o = pPage->pgno;
16483 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 .. assert( sqli
16484 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
16485 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 Page->pBt->mutex
16486 29 20 29 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 ) );. rc = btre
16487 65 49 6e 69 74 50 61 67 65 28 70 50 61 67 65 29 eInitPage(pPage)
16488 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 ;. if( rc!=SQLI
16489 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 TE_OK ){. got
1648a 6f 20 73 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d o set_child_ptrm
1648b 61 70 73 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 6e aps_out;. }. n
1648c 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 6e 43 Cell = pPage->nC
1648d 65 6c 6c 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b ell;.. for(i=0;
1648e 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a i<nCell; i++){.
1648f 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 20 3d 20 u8 *pCell =
16490 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 findCell(pPage,
16491 69 29 3b 0a 0a 20 20 20 20 70 74 72 6d 61 70 50 i);.. ptrmapP
16492 75 74 4f 76 66 6c 50 74 72 28 70 50 61 67 65 2c utOvflPtr(pPage,
16493 20 70 43 65 6c 6c 2c 20 26 72 63 29 3b 0a 0a 20 pCell, &rc);..
16494 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c if( !pPage->l
16495 65 61 66 20 29 7b 0a 20 20 20 20 20 20 50 67 6e eaf ){. Pgn
16496 6f 20 63 68 69 6c 64 50 67 6e 6f 20 3d 20 67 65 o childPgno = ge
16497 74 34 62 79 74 65 28 70 43 65 6c 6c 29 3b 0a 20 t4byte(pCell);.
16498 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 ptrmapPut(p
16499 42 74 2c 20 63 68 69 6c 64 50 67 6e 6f 2c 20 50 Bt, childPgno, P
1649a 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 67 6e TRMAP_BTREE, pgn
1649b 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 7d 0a 20 o, &rc);. }.
1649c 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65 }.. if( !pPage
1649d 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 50 67 ->leaf ){. Pg
1649e 6e 6f 20 63 68 69 6c 64 50 67 6e 6f 20 3d 20 67 no childPgno = g
1649f 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e et4byte(&pPage->
164a0 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 aData[pPage->hdr
164a1 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 Offset+8]);.
164a2 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 63 ptrmapPut(pBt, c
164a3 68 69 6c 64 50 67 6e 6f 2c 20 50 54 52 4d 41 50 hildPgno, PTRMAP
164a4 5f 42 54 52 45 45 2c 20 70 67 6e 6f 2c 20 26 72 _BTREE, pgno, &r
164a5 63 29 3b 0a 20 20 7d 0a 0a 73 65 74 5f 63 68 69 c);. }..set_chi
164a6 6c 64 5f 70 74 72 6d 61 70 73 5f 6f 75 74 3a 0a ld_ptrmaps_out:.
164a7 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 pPage->isInit
164a8 3d 20 69 73 49 6e 69 74 4f 72 69 67 3b 0a 20 20 = isInitOrig;.
164a9 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
164aa 0a 2a 2a 20 53 6f 6d 65 77 68 65 72 65 20 6f 6e .** Somewhere on
164ab 20 70 50 61 67 65 20 69 73 20 61 20 70 6f 69 6e pPage is a poin
164ac 74 65 72 20 74 6f 20 70 61 67 65 20 69 46 72 6f ter to page iFro
164ad 6d 2e 20 20 4d 6f 64 69 66 79 20 74 68 69 73 20 m. Modify this
164ae 70 6f 69 6e 74 65 72 20 73 6f 0a 2a 2a 20 74 68 pointer so.** th
164af 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 at it points to
164b0 69 54 6f 2e 20 50 61 72 61 6d 65 74 65 72 20 65 iTo. Parameter e
164b1 54 79 70 65 20 64 65 73 63 72 69 62 65 73 20 74 Type describes t
164b2 68 65 20 74 79 70 65 20 6f 66 20 70 6f 69 6e 74 he type of point
164b3 65 72 20 74 6f 0a 2a 2a 20 62 65 20 6d 6f 64 69 er to.** be modi
164b4 66 69 65 64 2c 20 61 73 20 20 66 6f 6c 6c 6f 77 fied, as follow
164b5 73 3a 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f s:.**.** PTRMAP_
164b6 42 54 52 45 45 3a 20 20 20 20 20 70 50 61 67 65 BTREE: pPage
164b7 20 69 73 20 61 20 62 74 72 65 65 2d 70 61 67 65 is a btree-page
164b8 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 70 6f . The pointer po
164b9 69 6e 74 73 20 61 74 20 61 20 63 68 69 6c 64 20 ints at a child
164ba 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
164bb 20 20 20 20 20 20 70 61 67 65 20 6f 66 20 70 50 page of pP
164bc 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 age..**.** PTRMA
164bd 50 5f 4f 56 45 52 46 4c 4f 57 31 3a 20 70 50 61 P_OVERFLOW1: pPa
164be 67 65 20 69 73 20 61 20 62 74 72 65 65 2d 70 61 ge is a btree-pa
164bf 67 65 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 ge. The pointer
164c0 70 6f 69 6e 74 73 20 61 74 20 61 6e 20 6f 76 65 points at an ove
164c1 72 66 6c 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20 rflow.**
164c2 20 20 20 20 20 20 20 20 20 20 20 70 61 67 65 20 page
164c3 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 6f 6e pointed to by on
164c4 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 73 20 6f e of the cells o
164c5 6e 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 n pPage..**.** P
164c6 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 3a TRMAP_OVERFLOW2:
164c7 20 70 50 61 67 65 20 69 73 20 61 6e 20 6f 76 65 pPage is an ove
164c8 72 66 6c 6f 77 2d 70 61 67 65 2e 20 54 68 65 20 rflow-page. The
164c9 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 61 pointer points a
164ca 74 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 20 20 t the next.**
164cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
164cc 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e overflow page in
164cd 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 the list..*/.st
164ce 61 74 69 63 20 69 6e 74 20 6d 6f 64 69 66 79 50 atic int modifyP
164cf 61 67 65 50 6f 69 6e 74 65 72 28 4d 65 6d 50 61 agePointer(MemPa
164d0 67 65 20 2a 70 50 61 67 65 2c 20 50 67 6e 6f 20 ge *pPage, Pgno
164d1 69 46 72 6f 6d 2c 20 50 67 6e 6f 20 69 54 6f 2c iFrom, Pgno iTo,
164d2 20 75 38 20 65 54 79 70 65 29 7b 0a 20 20 61 73 u8 eType){. as
164d3 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
164d4 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e tex_held(pPage->
164d5 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
164d6 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
164d7 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 PagerIswriteable
164d8 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
164d9 20 29 3b 0a 20 20 69 66 28 20 65 54 79 70 65 3d );. if( eType=
164da 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 =PTRMAP_OVERFLOW
164db 32 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 2 ){. /* The
164dc 70 6f 69 6e 74 65 72 20 69 73 20 61 6c 77 61 79 pointer is alway
164dd 73 20 74 68 65 20 66 69 72 73 74 20 34 20 62 79 s the first 4 by
164de 74 65 73 20 6f 66 20 74 68 65 20 70 61 67 65 20 tes of the page
164df 69 6e 20 74 68 69 73 20 63 61 73 65 2e 20 20 2a in this case. *
164e0 2f 0a 20 20 20 20 69 66 28 20 67 65 74 34 62 79 /. if( get4by
164e1 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74 61 29 te(pPage->aData)
164e2 21 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 !=iFrom ){.
164e3 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
164e4 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
164e5 20 7d 0a 20 20 20 20 70 75 74 34 62 79 74 65 28 }. put4byte(
164e6 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 69 54 pPage->aData, iT
164e7 6f 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 o);. }else{.
164e8 20 75 38 20 69 73 49 6e 69 74 4f 72 69 67 20 3d u8 isInitOrig =
164e9 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 3b 0a pPage->isInit;.
164ea 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 69 int i;. i
164eb 6e 74 20 6e 43 65 6c 6c 3b 0a 0a 20 20 20 20 62 nt nCell;.. b
164ec 74 72 65 65 49 6e 69 74 50 61 67 65 28 70 50 61 treeInitPage(pPa
164ed 67 65 29 3b 0a 20 20 20 20 6e 43 65 6c 6c 20 3d ge);. nCell =
164ee 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 0a pPage->nCell;..
164ef 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e for(i=0; i<n
164f0 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 Cell; i++){.
164f1 20 20 75 38 20 2a 70 43 65 6c 6c 20 3d 20 66 69 u8 *pCell = fi
164f2 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 29 ndCell(pPage, i)
164f3 3b 0a 20 20 20 20 20 20 69 66 28 20 65 54 79 70 ;. if( eTyp
164f4 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c e==PTRMAP_OVERFL
164f5 4f 57 31 20 29 7b 0a 20 20 20 20 20 20 20 20 43 OW1 ){. C
164f6 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 ellInfo info;.
164f7 20 20 20 20 20 20 62 74 72 65 65 50 61 72 73 65 btreeParse
164f8 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 CellPtr(pPage, p
164f9 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 Cell, &info);.
164fa 20 20 20 20 20 20 69 66 28 20 69 6e 66 6f 2e 69 if( info.i
164fb 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 Overflow ){.
164fc 20 20 20 20 20 20 69 66 28 20 69 46 72 6f 6d 3d if( iFrom=
164fd 3d 67 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c =get4byte(&pCell
164fe 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d [info.iOverflow]
164ff 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 ) ){.
16500 20 70 75 74 34 62 79 74 65 28 26 70 43 65 6c 6c put4byte(&pCell
16501 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d [info.iOverflow]
16502 2c 20 69 54 6f 29 3b 0a 20 20 20 20 20 20 20 20 , iTo);.
16503 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
16504 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
16505 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
16506 20 20 20 20 20 20 69 66 28 20 67 65 74 34 62 79 if( get4by
16507 74 65 28 70 43 65 6c 6c 29 3d 3d 69 46 72 6f 6d te(pCell)==iFrom
16508 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 75 ){. pu
16509 74 34 62 79 74 65 28 70 43 65 6c 6c 2c 20 69 54 t4byte(pCell, iT
1650a 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 o);. br
1650b 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 eak;. }.
1650c 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a }. }. .
1650d 20 20 20 20 69 66 28 20 69 3d 3d 6e 43 65 6c 6c if( i==nCell
1650e 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 65 54 ){. if( eT
1650f 79 70 65 21 3d 50 54 52 4d 41 50 5f 42 54 52 45 ype!=PTRMAP_BTRE
16510 45 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 20 20 E || .
16511 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d get4byte(&pPage-
16512 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 >aData[pPage->hd
16513 72 4f 66 66 73 65 74 2b 38 5d 29 21 3d 69 46 72 rOffset+8])!=iFr
16514 6f 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 om ){. re
16515 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
16516 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 UPT_BKPT;.
16517 7d 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65 }. put4byte
16518 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 (&pPage->aData[p
16519 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b Page->hdrOffset+
1651a 38 5d 2c 20 69 54 6f 29 3b 0a 20 20 20 20 7d 0a 8], iTo);. }.
1651b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 73 49 6e . pPage->isIn
1651c 69 74 20 3d 20 69 73 49 6e 69 74 4f 72 69 67 3b it = isInitOrig;
1651d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
1651e 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a LITE_OK;.}.../*.
1651f 2a 2a 20 4d 6f 76 65 20 74 68 65 20 6f 70 65 6e ** Move the open
16520 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 70 database page p
16521 44 62 50 61 67 65 20 74 6f 20 6c 6f 63 61 74 69 DbPage to locati
16522 6f 6e 20 69 46 72 65 65 50 61 67 65 20 69 6e 20 on iFreePage in
16523 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 the .** database
16524 2e 20 54 68 65 20 70 44 62 50 61 67 65 20 72 65 . The pDbPage re
16525 66 65 72 65 6e 63 65 20 72 65 6d 61 69 6e 73 20 ference remains
16526 76 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 valid..**.** The
16527 20 69 73 43 6f 6d 6d 69 74 20 66 6c 61 67 20 69 isCommit flag i
16528 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 74 68 ndicates that th
16529 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 ere is no need t
1652a 6f 20 72 65 6d 65 6d 62 65 72 20 74 68 61 74 0a o remember that.
1652b 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6e ** the journal n
1652c 65 65 64 73 20 74 6f 20 62 65 20 73 79 6e 63 28 eeds to be sync(
1652d 29 65 64 20 62 65 66 6f 72 65 20 64 61 74 61 62 )ed before datab
1652e 61 73 65 20 70 61 67 65 20 70 44 62 50 61 67 65 ase page pDbPage
1652f 2d 3e 70 67 6e 6f 20 0a 2a 2a 20 63 61 6e 20 62 ->pgno .** can b
16530 65 20 77 72 69 74 74 65 6e 20 74 6f 2e 20 54 68 e written to. Th
16531 65 20 63 61 6c 6c 65 72 20 68 61 73 20 61 6c 72 e caller has alr
16532 65 61 64 79 20 70 72 6f 6d 69 73 65 64 20 6e 6f eady promised no
16533 74 20 74 6f 20 77 72 69 74 65 20 74 6f 20 74 68 t to write to th
16534 61 74 0a 2a 2a 20 70 61 67 65 2e 0a 2a 2f 0a 73 at.** page..*/.s
16535 74 61 74 69 63 20 69 6e 74 20 72 65 6c 6f 63 61 tatic int reloca
16536 74 65 50 61 67 65 28 0a 20 20 42 74 53 68 61 72 tePage(. BtShar
16537 65 64 20 2a 70 42 74 2c 20 20 20 20 20 20 20 20 ed *pBt,
16538 20 20 20 2f 2a 20 42 74 72 65 65 20 2a 2f 0a 20 /* Btree */.
16539 20 4d 65 6d 50 61 67 65 20 2a 70 44 62 50 61 67 MemPage *pDbPag
1653a 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 e, /* Ope
1653b 6e 20 70 61 67 65 20 74 6f 20 6d 6f 76 65 20 2a n page to move *
1653c 2f 0a 20 20 75 38 20 65 54 79 70 65 2c 20 20 20 /. u8 eType,
1653d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1653e 50 6f 69 6e 74 65 72 20 6d 61 70 20 27 74 79 70 Pointer map 'typ
1653f 65 27 20 65 6e 74 72 79 20 66 6f 72 20 70 44 62 e' entry for pDb
16540 50 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 Page */. Pgno i
16541 50 74 72 50 61 67 65 2c 20 20 20 20 20 20 20 20 PtrPage,
16542 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 6d 61 /* Pointer ma
16543 70 20 27 70 61 67 65 2d 6e 6f 27 20 65 6e 74 72 p 'page-no' entr
16544 79 20 66 6f 72 20 70 44 62 50 61 67 65 20 2a 2f y for pDbPage */
16545 0a 20 20 50 67 6e 6f 20 69 46 72 65 65 50 61 67 . Pgno iFreePag
16546 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 e, /* T
16547 68 65 20 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 6d he location to m
16548 6f 76 65 20 70 44 62 50 61 67 65 20 74 6f 20 2a ove pDbPage to *
16549 2f 0a 20 20 69 6e 74 20 69 73 43 6f 6d 6d 69 74 /. int isCommit
1654a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1654b 69 73 43 6f 6d 6d 69 74 20 66 6c 61 67 20 70 61 isCommit flag pa
1654c 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33 50 ssed to sqlite3P
1654d 61 67 65 72 4d 6f 76 65 70 61 67 65 20 2a 2f 0a agerMovepage */.
1654e 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 ){. MemPage *pP
1654f 74 72 50 61 67 65 3b 20 20 20 2f 2a 20 54 68 65 trPage; /* The
16550 20 70 61 67 65 20 74 68 61 74 20 63 6f 6e 74 61 page that conta
16551 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f ins a pointer to
16552 20 70 44 62 50 61 67 65 20 2a 2f 0a 20 20 50 67 pDbPage */. Pg
16553 6e 6f 20 69 44 62 50 61 67 65 20 3d 20 70 44 62 no iDbPage = pDb
16554 50 61 67 65 2d 3e 70 67 6e 6f 3b 0a 20 20 50 61 Page->pgno;. Pa
16555 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 42 ger *pPager = pB
16556 74 2d 3e 70 50 61 67 65 72 3b 0a 20 20 69 6e 74 t->pPager;. int
16557 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 rc;.. assert(
16558 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 eType==PTRMAP_OV
16559 45 52 46 4c 4f 57 32 20 7c 7c 20 65 54 79 70 65 ERFLOW2 || eType
1655a 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f ==PTRMAP_OVERFLO
1655b 57 31 20 7c 7c 20 0a 20 20 20 20 20 20 65 54 79 W1 || . eTy
1655c 70 65 3d 3d 50 54 52 4d 41 50 5f 42 54 52 45 45 pe==PTRMAP_BTREE
1655d 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 || eType==PTRMA
1655e 50 5f 52 4f 4f 54 50 41 47 45 20 29 3b 0a 20 20 P_ROOTPAGE );.
1655f 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
16560 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e mutex_held(pBt->
16561 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
16562 72 74 28 20 70 44 62 50 61 67 65 2d 3e 70 42 74 rt( pDbPage->pBt
16563 3d 3d 70 42 74 20 29 3b 0a 0a 20 20 2f 2a 20 4d ==pBt );.. /* M
16564 6f 76 65 20 70 61 67 65 20 69 44 62 50 61 67 65 ove page iDbPage
16565 20 66 72 6f 6d 20 69 74 73 20 63 75 72 72 65 6e from its curren
16566 74 20 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 70 61 t location to pa
16567 67 65 20 6e 75 6d 62 65 72 20 69 46 72 65 65 50 ge number iFreeP
16568 61 67 65 20 2a 2f 0a 20 20 54 52 41 43 45 28 28 age */. TRACE((
16569 22 41 55 54 4f 56 41 43 55 55 4d 3a 20 4d 6f 76 "AUTOVACUUM: Mov
1656a 69 6e 67 20 25 64 20 74 6f 20 66 72 65 65 20 70 ing %d to free p
1656b 61 67 65 20 25 64 20 28 70 74 72 20 70 61 67 65 age %d (ptr page
1656c 20 25 64 20 74 79 70 65 20 25 64 29 5c 6e 22 2c %d type %d)\n",
1656d 20 0a 20 20 20 20 20 20 69 44 62 50 61 67 65 2c . iDbPage,
1656e 20 69 46 72 65 65 50 61 67 65 2c 20 69 50 74 72 iFreePage, iPtr
1656f 50 61 67 65 2c 20 65 54 79 70 65 29 29 3b 0a 20 Page, eType));.
16570 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
16571 65 72 4d 6f 76 65 70 61 67 65 28 70 50 61 67 65 erMovepage(pPage
16572 72 2c 20 70 44 62 50 61 67 65 2d 3e 70 44 62 50 r, pDbPage->pDbP
16573 61 67 65 2c 20 69 46 72 65 65 50 61 67 65 2c 20 age, iFreePage,
16574 69 73 43 6f 6d 6d 69 74 29 3b 0a 20 20 69 66 28 isCommit);. if(
16575 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
16576 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b {. return rc;
16577 0a 20 20 7d 0a 20 20 70 44 62 50 61 67 65 2d 3e . }. pDbPage->
16578 70 67 6e 6f 20 3d 20 69 46 72 65 65 50 61 67 65 pgno = iFreePage
16579 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 44 62 50 61 ;.. /* If pDbPa
1657a 67 65 20 77 61 73 20 61 20 62 74 72 65 65 2d 70 ge was a btree-p
1657b 61 67 65 2c 20 74 68 65 6e 20 69 74 20 6d 61 79 age, then it may
1657c 20 68 61 76 65 20 63 68 69 6c 64 20 70 61 67 65 have child page
1657d 73 20 61 6e 64 2f 6f 72 20 63 65 6c 6c 73 0a 20 s and/or cells.
1657e 20 2a 2a 20 74 68 61 74 20 70 6f 69 6e 74 20 74 ** that point t
1657f 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 o overflow pages
16580 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 . The pointer ma
16581 70 20 65 6e 74 72 69 65 73 20 66 6f 72 20 61 6c p entries for al
16582 6c 20 74 68 65 73 65 0a 20 20 2a 2a 20 70 61 67 l these. ** pag
16583 65 73 20 6e 65 65 64 20 74 6f 20 62 65 20 63 68 es need to be ch
16584 61 6e 67 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a anged.. **. **
16585 20 49 66 20 70 44 62 50 61 67 65 20 69 73 20 61 If pDbPage is a
16586 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2c n overflow page,
16587 20 74 68 65 6e 20 74 68 65 20 66 69 72 73 74 20 then the first
16588 34 20 62 79 74 65 73 20 6d 61 79 20 73 74 6f 72 4 bytes may stor
16589 65 20 61 0a 20 20 2a 2a 20 70 6f 69 6e 74 65 72 e a. ** pointer
1658a 20 74 6f 20 61 20 73 75 62 73 65 71 75 65 6e 74 to a subsequent
1658b 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 20 overflow page.
1658c 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63 If this is the c
1658d 61 73 65 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 74 ase, then. ** t
1658e 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 6e he pointer map n
1658f 65 65 64 73 20 74 6f 20 62 65 20 75 70 64 61 74 eeds to be updat
16590 65 64 20 66 6f 72 20 74 68 65 20 73 75 62 73 65 ed for the subse
16591 71 75 65 6e 74 20 6f 76 65 72 66 6c 6f 77 20 70 quent overflow p
16592 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 age.. */. if(
16593 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 42 54 eType==PTRMAP_BT
16594 52 45 45 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 REE || eType==PT
16595 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 7b RMAP_ROOTPAGE ){
16596 0a 20 20 20 20 72 63 20 3d 20 73 65 74 43 68 69 . rc = setChi
16597 6c 64 50 74 72 6d 61 70 73 28 70 44 62 50 61 67 ldPtrmaps(pDbPag
16598 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d e);. if( rc!=
16599 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1659a 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
1659b 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
1659c 20 50 67 6e 6f 20 6e 65 78 74 4f 76 66 6c 20 3d Pgno nextOvfl =
1659d 20 67 65 74 34 62 79 74 65 28 70 44 62 50 61 67 get4byte(pDbPag
1659e 65 2d 3e 61 44 61 74 61 29 3b 0a 20 20 20 20 69 e->aData);. i
1659f 66 28 20 6e 65 78 74 4f 76 66 6c 21 3d 30 20 29 f( nextOvfl!=0 )
165a0 7b 0a 20 20 20 20 20 20 70 74 72 6d 61 70 50 75 {. ptrmapPu
165a1 74 28 70 42 74 2c 20 6e 65 78 74 4f 76 66 6c 2c t(pBt, nextOvfl,
165a2 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 PTRMAP_OVERFLOW
165a3 32 2c 20 69 46 72 65 65 50 61 67 65 2c 20 26 72 2, iFreePage, &r
165a4 63 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 c);. if( rc
165a5 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
165a6 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
165a7 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
165a8 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 78 20 74 68 }.. /* Fix th
165a9 65 20 64 61 74 61 62 61 73 65 20 70 6f 69 6e 74 e database point
165aa 65 72 20 6f 6e 20 70 61 67 65 20 69 50 74 72 50 er on page iPtrP
165ab 61 67 65 20 74 68 61 74 20 70 6f 69 6e 74 65 64 age that pointed
165ac 20 61 74 20 69 44 62 50 61 67 65 20 73 6f 0a 20 at iDbPage so.
165ad 20 2a 2a 20 74 68 61 74 20 69 74 20 70 6f 69 6e ** that it poin
165ae 74 73 20 61 74 20 69 46 72 65 65 50 61 67 65 2e ts at iFreePage.
165af 20 41 6c 73 6f 20 66 69 78 20 74 68 65 20 70 6f Also fix the po
165b0 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 79 20 inter map entry
165b1 66 6f 72 0a 20 20 2a 2a 20 69 50 74 72 50 61 67 for. ** iPtrPag
165b2 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 65 54 e.. */. if( eT
165b3 79 70 65 21 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 ype!=PTRMAP_ROOT
165b4 50 41 47 45 20 29 7b 0a 20 20 20 20 72 63 20 3d PAGE ){. rc =
165b5 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42 btreeGetPage(pB
165b6 74 2c 20 69 50 74 72 50 61 67 65 2c 20 26 70 50 t, iPtrPage, &pP
165b7 74 72 50 61 67 65 2c 20 30 29 3b 0a 20 20 20 20 trPage, 0);.
165b8 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
165b9 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 K ){. retur
165ba 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 n rc;. }.
165bb 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
165bc 72 57 72 69 74 65 28 70 50 74 72 50 61 67 65 2d rWrite(pPtrPage-
165bd 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 >pDbPage);. i
165be 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
165bf 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 ){. releas
165c0 65 50 61 67 65 28 70 50 74 72 50 61 67 65 29 3b ePage(pPtrPage);
165c1 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
165c2 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d ;. }. rc =
165c3 20 6d 6f 64 69 66 79 50 61 67 65 50 6f 69 6e 74 modifyPagePoint
165c4 65 72 28 70 50 74 72 50 61 67 65 2c 20 69 44 62 er(pPtrPage, iDb
165c5 50 61 67 65 2c 20 69 46 72 65 65 50 61 67 65 2c Page, iFreePage,
165c6 20 65 54 79 70 65 29 3b 0a 20 20 20 20 72 65 6c eType);. rel
165c7 65 61 73 65 50 61 67 65 28 70 50 74 72 50 61 67 easePage(pPtrPag
165c8 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d e);. if( rc==
165c9 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
165ca 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 ptrmapPut(pBt
165cb 2c 20 69 46 72 65 65 50 61 67 65 2c 20 65 54 79 , iFreePage, eTy
165cc 70 65 2c 20 69 50 74 72 50 61 67 65 2c 20 26 72 pe, iPtrPage, &r
165cd 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 c);. }. }.
165ce 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
165cf 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 Forward declara
165d0 74 69 6f 6e 20 72 65 71 75 69 72 65 64 20 62 79 tion required by
165d1 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70 28 incrVacuumStep(
165d2 29 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 ). */.static int
165d3 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 allocateBtreePa
165d4 67 65 28 42 74 53 68 61 72 65 64 20 2a 2c 20 4d ge(BtShared *, M
165d5 65 6d 50 61 67 65 20 2a 2a 2c 20 50 67 6e 6f 20 emPage **, Pgno
165d6 2a 2c 20 50 67 6e 6f 2c 20 75 38 29 3b 0a 0a 2f *, Pgno, u8);../
165d7 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20 73 *.** Perform a s
165d8 69 6e 67 6c 65 20 73 74 65 70 20 6f 66 20 61 6e ingle step of an
165d9 20 69 6e 63 72 65 6d 65 6e 74 61 6c 2d 76 61 63 incremental-vac
165da 75 75 6d 2e 20 49 66 20 73 75 63 63 65 73 73 66 uum. If successf
165db 75 6c 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 53 51 ul,.** return SQ
165dc 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 74 68 65 72 LITE_OK. If ther
165dd 65 20 69 73 20 6e 6f 20 77 6f 72 6b 20 74 6f 20 e is no work to
165de 64 6f 20 28 61 6e 64 20 74 68 65 72 65 66 6f 72 do (and therefor
165df 65 20 6e 6f 0a 2a 2a 20 70 6f 69 6e 74 20 69 6e e no.** point in
165e0 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 66 75 calling this fu
165e1 6e 63 74 69 6f 6e 20 61 67 61 69 6e 29 2c 20 72 nction again), r
165e2 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e eturn SQLITE_DON
165e3 45 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 72 65 20 73 70 E..**.** More sp
165e4 65 63 69 66 69 63 6c 79 2c 20 74 68 69 73 20 66 ecificly, this f
165e5 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 unction attempts
165e6 20 74 6f 20 72 65 2d 6f 72 67 61 6e 69 7a 65 20 to re-organize
165e7 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 the .** database
165e8 20 73 6f 20 74 68 61 74 20 74 68 65 20 6c 61 73 so that the las
165e9 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 66 69 t page of the fi
165ea 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 le currently in
165eb 75 73 65 0a 2a 2a 20 69 73 20 6e 6f 20 6c 6f 6e use.** is no lon
165ec 67 65 72 20 69 6e 20 75 73 65 2e 0a 2a 2a 0a 2a ger in use..**.*
165ed 2a 20 49 66 20 74 68 65 20 6e 46 69 6e 20 70 61 * If the nFin pa
165ee 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a rameter is non-z
165ef 65 72 6f 2c 20 74 68 69 73 20 66 75 6e 63 74 69 ero, this functi
165f0 6f 6e 20 61 73 73 75 6d 65 73 0a 2a 2a 20 74 68 on assumes.** th
165f1 61 74 20 74 68 65 20 63 61 6c 6c 65 72 20 77 69 at the caller wi
165f2 6c 6c 20 6b 65 65 70 20 63 61 6c 6c 69 6e 67 20 ll keep calling
165f3 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70 28 29 incrVacuumStep()
165f4 20 75 6e 74 69 6c 0a 2a 2a 20 69 74 20 72 65 74 until.** it ret
165f5 75 72 6e 73 20 53 51 4c 49 54 45 5f 44 4f 4e 45 urns SQLITE_DONE
165f6 20 6f 72 20 61 6e 20 65 72 72 6f 72 2c 20 61 6e or an error, an
165f7 64 20 74 68 61 74 20 6e 46 69 6e 20 69 73 20 74 d that nFin is t
165f8 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 he.** number of
165f9 70 61 67 65 73 20 74 68 65 20 64 61 74 61 62 61 pages the databa
165fa 73 65 20 66 69 6c 65 20 77 69 6c 6c 20 63 6f 6e se file will con
165fb 74 61 69 6e 20 61 66 74 65 72 20 74 68 69 73 20 tain after this
165fc 0a 2a 2a 20 70 72 6f 63 65 73 73 20 69 73 20 63 .** process is c
165fd 6f 6d 70 6c 65 74 65 2e 20 20 49 66 20 6e 46 69 omplete. If nFi
165fe 6e 20 69 73 20 7a 65 72 6f 2c 20 69 74 20 69 73 n is zero, it is
165ff 20 61 73 73 75 6d 65 64 20 74 68 61 74 0a 2a 2a assumed that.**
16600 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70 28 incrVacuumStep(
16601 29 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 ) will be called
16602 20 61 20 66 69 6e 69 74 65 20 61 6d 6f 75 6e 74 a finite amount
16603 20 6f 66 20 74 69 6d 65 73 0a 2a 2a 20 77 68 69 of times.** whi
16604 63 68 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f ch may or may no
16605 74 20 65 6d 70 74 79 20 74 68 65 20 66 72 65 65 t empty the free
16606 6c 69 73 74 2e 20 20 41 20 66 75 6c 6c 20 61 75 list. A full au
16607 74 6f 76 61 63 75 75 6d 0a 2a 2a 20 68 61 73 20 tovacuum.** has
16608 6e 46 69 6e 3e 30 2e 20 20 41 20 22 50 52 41 47 nFin>0. A "PRAG
16609 4d 41 20 69 6e 63 72 65 6d 65 6e 74 61 6c 5f 76 MA incremental_v
1660a 61 63 75 75 6d 22 20 68 61 73 20 6e 46 69 6e 3d acuum" has nFin=
1660b 3d 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e =0..*/.static in
1660c 74 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70 t incrVacuumStep
1660d 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 (BtShared *pBt,
1660e 50 67 6e 6f 20 6e 46 69 6e 2c 20 50 67 6e 6f 20 Pgno nFin, Pgno
1660f 69 4c 61 73 74 50 67 29 7b 0a 20 20 50 67 6e 6f iLastPg){. Pgno
16610 20 6e 46 72 65 65 4c 69 73 74 3b 20 20 20 20 20 nFreeList;
16611 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
16612 6f 66 20 70 61 67 65 73 20 73 74 69 6c 6c 20 6f of pages still o
16613 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 n the free-list
16614 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 */.. assert( sq
16615 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
16616 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a (pBt->mutex) );.
16617 20 20 61 73 73 65 72 74 28 20 69 4c 61 73 74 50 assert( iLastP
16618 67 3e 6e 46 69 6e 20 29 3b 0a 0a 20 20 69 66 28 g>nFin );.. if(
16619 20 21 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 !PTRMAP_ISPAGE(
1661a 70 42 74 2c 20 69 4c 61 73 74 50 67 29 20 26 26 pBt, iLastPg) &&
1661b 20 69 4c 61 73 74 50 67 21 3d 50 45 4e 44 49 4e iLastPg!=PENDIN
1661c 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 G_BYTE_PAGE(pBt)
1661d 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a ){. int rc;.
1661e 20 20 20 20 75 38 20 65 54 79 70 65 3b 0a 20 20 u8 eType;.
1661f 20 20 50 67 6e 6f 20 69 50 74 72 50 61 67 65 3b Pgno iPtrPage;
16620 0a 0a 20 20 20 20 6e 46 72 65 65 4c 69 73 74 20 .. nFreeList
16621 3d 20 67 65 74 34 62 79 74 65 28 26 70 42 74 2d = get4byte(&pBt-
16622 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 >pPage1->aData[3
16623 36 5d 29 3b 0a 20 20 20 20 69 66 28 20 6e 46 72 6]);. if( nFr
16624 65 65 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 eeList==0 ){.
16625 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
16626 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 _DONE;. }..
16627 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47 65 74 rc = ptrmapGet
16628 28 70 42 74 2c 20 69 4c 61 73 74 50 67 2c 20 26 (pBt, iLastPg, &
16629 65 54 79 70 65 2c 20 26 69 50 74 72 50 61 67 65 eType, &iPtrPage
1662a 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
1662b 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1662c 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
1662d 20 7d 0a 20 20 20 20 69 66 28 20 65 54 79 70 65 }. if( eType
1662e 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 ==PTRMAP_ROOTPAG
1662f 45 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 E ){. retur
16630 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
16631 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 0a 20 20 _BKPT;. }..
16632 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 if( eType==PTR
16633 4d 41 50 5f 46 52 45 45 50 41 47 45 20 29 7b 0a MAP_FREEPAGE ){.
16634 20 20 20 20 20 20 69 66 28 20 6e 46 69 6e 3d 3d if( nFin==
16635 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 0 ){. /*
16636 52 65 6d 6f 76 65 20 74 68 65 20 70 61 67 65 20 Remove the page
16637 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 73 20 66 from the files f
16638 72 65 65 2d 6c 69 73 74 2e 20 54 68 69 73 20 69 ree-list. This i
16639 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 0a 20 s not required.
1663a 20 20 20 20 20 20 20 2a 2a 20 69 66 20 6e 46 69 ** if nFi
1663b 6e 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 49 n is non-zero. I
1663c 6e 20 74 68 61 74 20 63 61 73 65 2c 20 74 68 65 n that case, the
1663d 20 66 72 65 65 2d 6c 69 73 74 20 77 69 6c 6c 20 free-list will
1663e 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 72 be. ** tr
1663f 75 6e 63 61 74 65 64 20 74 6f 20 7a 65 72 6f 20 uncated to zero
16640 61 66 74 65 72 20 74 68 69 73 20 66 75 6e 63 74 after this funct
16641 69 6f 6e 20 72 65 74 75 72 6e 73 2c 20 73 6f 20 ion returns, so
16642 69 74 20 64 6f 65 73 6e 27 74 20 0a 20 20 20 20 it doesn't .
16643 20 20 20 20 2a 2a 20 6d 61 74 74 65 72 20 69 66 ** matter if
16644 20 69 74 20 73 74 69 6c 6c 20 63 6f 6e 74 61 69 it still contai
16645 6e 73 20 73 6f 6d 65 20 67 61 72 62 61 67 65 20 ns some garbage
16646 65 6e 74 72 69 65 73 2e 0a 20 20 20 20 20 20 20 entries..
16647 20 2a 2f 0a 20 20 20 20 20 20 20 20 50 67 6e 6f */. Pgno
16648 20 69 46 72 65 65 50 67 3b 0a 20 20 20 20 20 20 iFreePg;.
16649 20 20 4d 65 6d 50 61 67 65 20 2a 70 46 72 65 65 MemPage *pFree
1664a 50 67 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d Pg;. rc =
1664b 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 allocateBtreePa
1664c 67 65 28 70 42 74 2c 20 26 70 46 72 65 65 50 67 ge(pBt, &pFreePg
1664d 2c 20 26 69 46 72 65 65 50 67 2c 20 69 4c 61 73 , &iFreePg, iLas
1664e 74 50 67 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 tPg, 1);.
1664f 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
16650 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
16651 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
16652 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61 73 73 }. ass
16653 65 72 74 28 20 69 46 72 65 65 50 67 3d 3d 69 4c ert( iFreePg==iL
16654 61 73 74 50 67 20 29 3b 0a 20 20 20 20 20 20 20 astPg );.
16655 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 46 72 releasePage(pFr
16656 65 65 50 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 eePg);. }.
16657 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 } else {.
16658 20 20 50 67 6e 6f 20 69 46 72 65 65 50 67 3b 20 Pgno iFreePg;
16659 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
1665a 6e 64 65 78 20 6f 66 20 66 72 65 65 20 70 61 67 ndex of free pag
1665b 65 20 74 6f 20 6d 6f 76 65 20 70 4c 61 73 74 50 e to move pLastP
1665c 67 20 74 6f 20 2a 2f 0a 20 20 20 20 20 20 4d 65 g to */. Me
1665d 6d 50 61 67 65 20 2a 70 4c 61 73 74 50 67 3b 0a mPage *pLastPg;.
1665e 0a 20 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 . rc = btre
1665f 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 4c eGetPage(pBt, iL
16660 61 73 74 50 67 2c 20 26 70 4c 61 73 74 50 67 2c astPg, &pLastPg,
16661 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 0);. if( r
16662 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
16663 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 return r
16664 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 c;. }..
16665 20 20 2f 2a 20 49 66 20 6e 46 69 6e 20 69 73 20 /* If nFin is
16666 7a 65 72 6f 2c 20 74 68 69 73 20 6c 6f 6f 70 20 zero, this loop
16667 72 75 6e 73 20 65 78 61 63 74 6c 79 20 6f 6e 63 runs exactly onc
16668 65 20 61 6e 64 20 70 61 67 65 20 70 4c 61 73 74 e and page pLast
16669 50 67 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 73 Pg. ** is s
1666a 77 61 70 70 65 64 20 77 69 74 68 20 74 68 65 20 wapped with the
1666b 66 69 72 73 74 20 66 72 65 65 20 70 61 67 65 20 first free page
1666c 70 75 6c 6c 65 64 20 6f 66 66 20 74 68 65 20 66 pulled off the f
1666d 72 65 65 20 6c 69 73 74 2e 0a 20 20 20 20 20 20 ree list..
1666e 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4f 6e 20 74 **. ** On t
1666f 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c 20 69 he other hand, i
16670 66 20 6e 46 69 6e 20 69 73 20 67 72 65 61 74 65 f nFin is greate
16671 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 r than zero, the
16672 6e 20 6b 65 65 70 0a 20 20 20 20 20 20 2a 2a 20 n keep. **
16673 6c 6f 6f 70 69 6e 67 20 75 6e 74 69 6c 20 61 20 looping until a
16674 66 72 65 65 2d 70 61 67 65 20 6c 6f 63 61 74 65 free-page locate
16675 64 20 77 69 74 68 69 6e 20 74 68 65 20 66 69 72 d within the fir
16676 73 74 20 6e 46 69 6e 20 70 61 67 65 73 0a 20 20 st nFin pages.
16677 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 66 69 ** of the fi
16678 6c 65 20 69 73 20 66 6f 75 6e 64 2e 0a 20 20 20 le is found..
16679 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 6f 20 7b */. do {
1667a 0a 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65 . MemPage
1667b 20 2a 70 46 72 65 65 50 67 3b 0a 20 20 20 20 20 *pFreePg;.
1667c 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 rc = allocate
1667d 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 BtreePage(pBt, &
1667e 70 46 72 65 65 50 67 2c 20 26 69 46 72 65 65 50 pFreePg, &iFreeP
1667f 67 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 g, 0, 0);.
16680 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
16681 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
16682 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4c 61 releasePage(pLa
16683 73 74 50 67 29 3b 0a 20 20 20 20 20 20 20 20 20 stPg);.
16684 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
16685 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 65 }. re
16686 6c 65 61 73 65 50 61 67 65 28 70 46 72 65 65 50 leasePage(pFreeP
16687 67 29 3b 0a 20 20 20 20 20 20 7d 77 68 69 6c 65 g);. }while
16688 28 20 6e 46 69 6e 21 3d 30 20 26 26 20 69 46 72 ( nFin!=0 && iFr
16689 65 65 50 67 3e 6e 46 69 6e 20 29 3b 0a 20 20 20 eePg>nFin );.
1668a 20 20 20 61 73 73 65 72 74 28 20 69 46 72 65 65 assert( iFree
1668b 50 67 3c 69 4c 61 73 74 50 67 20 29 3b 0a 20 20 Pg<iLastPg );.
1668c 20 20 20 20 0a 20 20 20 20 20 20 72 63 20 3d 20 . rc =
1668d 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
1668e 65 28 70 4c 61 73 74 50 67 2d 3e 70 44 62 50 61 e(pLastPg->pDbPa
1668f 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 ge);. if( r
16690 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
16691 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 65 6c rc = rel
16692 6f 63 61 74 65 50 61 67 65 28 70 42 74 2c 20 70 ocatePage(pBt, p
16693 4c 61 73 74 50 67 2c 20 65 54 79 70 65 2c 20 69 LastPg, eType, i
16694 50 74 72 50 61 67 65 2c 20 69 46 72 65 65 50 67 PtrPage, iFreePg
16695 2c 20 6e 46 69 6e 21 3d 30 29 3b 0a 20 20 20 20 , nFin!=0);.
16696 20 20 7d 0a 20 20 20 20 20 20 72 65 6c 65 61 73 }. releas
16697 65 50 61 67 65 28 70 4c 61 73 74 50 67 29 3b 0a ePage(pLastPg);.
16698 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
16699 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1669a 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
1669b 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
1669c 0a 20 20 69 66 28 20 6e 46 69 6e 3d 3d 30 20 29 . if( nFin==0 )
1669d 7b 0a 20 20 20 20 69 4c 61 73 74 50 67 2d 2d 3b {. iLastPg--;
1669e 0a 20 20 20 20 77 68 69 6c 65 28 20 69 4c 61 73 . while( iLas
1669f 74 50 67 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 tPg==PENDING_BYT
166a0 45 5f 50 41 47 45 28 70 42 74 29 7c 7c 50 54 52 E_PAGE(pBt)||PTR
166a1 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 MAP_ISPAGE(pBt,
166a2 69 4c 61 73 74 50 67 29 20 29 7b 0a 20 20 20 20 iLastPg) ){.
166a3 20 20 69 66 28 20 50 54 52 4d 41 50 5f 49 53 50 if( PTRMAP_ISP
166a4 41 47 45 28 70 42 74 2c 20 69 4c 61 73 74 50 67 AGE(pBt, iLastPg
166a5 29 20 29 7b 0a 20 20 20 20 20 20 20 20 4d 65 6d ) ){. Mem
166a6 50 61 67 65 20 2a 70 50 67 3b 0a 20 20 20 20 20 Page *pPg;.
166a7 20 20 20 69 6e 74 20 72 63 20 3d 20 62 74 72 65 int rc = btre
166a8 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 4c eGetPage(pBt, iL
166a9 61 73 74 50 67 2c 20 26 70 50 67 2c 20 30 29 3b astPg, &pPg, 0);
166aa 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 . if( rc!
166ab 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
166ac 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 return r
166ad 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 c;. }.
166ae 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
166af 33 50 61 67 65 72 57 72 69 74 65 28 70 50 67 2d 3PagerWrite(pPg-
166b0 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 >pDbPage);.
166b1 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
166b2 50 67 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 Pg);. if(
166b3 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
166b4 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 {. retu
166b5 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d rn rc;. }
166b6 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
166b7 4c 61 73 74 50 67 2d 2d 3b 0a 20 20 20 20 7d 0a LastPg--;. }.
166b8 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
166b9 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28 70 42 TruncateImage(pB
166ba 74 2d 3e 70 50 61 67 65 72 2c 20 69 4c 61 73 74 t->pPager, iLast
166bb 50 67 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 Pg);. }. retur
166bc 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
166bd 2f 2a 0a 2a 2a 20 41 20 77 72 69 74 65 2d 74 72 /*.** A write-tr
166be 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 ansaction must b
166bf 65 20 6f 70 65 6e 65 64 20 62 65 66 6f 72 65 20 e opened before
166c0 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 66 75 6e calling this fun
166c1 63 74 69 6f 6e 2e 0a 2a 2a 20 49 74 20 70 65 72 ction..** It per
166c2 66 6f 72 6d 73 20 61 20 73 69 6e 67 6c 65 20 75 forms a single u
166c3 6e 69 74 20 6f 66 20 77 6f 72 6b 20 74 6f 77 61 nit of work towa
166c4 72 64 73 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74 rds an increment
166c5 61 6c 20 76 61 63 75 75 6d 2e 0a 2a 2a 0a 2a 2a al vacuum..**.**
166c6 20 49 66 20 74 68 65 20 69 6e 63 72 65 6d 65 6e If the incremen
166c7 74 61 6c 20 76 61 63 75 75 6d 20 69 73 20 66 69 tal vacuum is fi
166c8 6e 69 73 68 65 64 20 61 66 74 65 72 20 74 68 69 nished after thi
166c9 73 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 72 s function has r
166ca 75 6e 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 44 4f un,.** SQLITE_DO
166cb 4e 45 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 NE is returned.
166cc 49 66 20 69 74 20 69 73 20 6e 6f 74 20 66 69 6e If it is not fin
166cd 69 73 68 65 64 2c 20 62 75 74 20 6e 6f 20 65 72 ished, but no er
166ce 72 6f 72 20 6f 63 63 75 72 72 65 64 2c 0a 2a 2a ror occurred,.**
166cf 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
166d0 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 turned. Otherwis
166d1 65 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f e an SQLite erro
166d2 72 20 63 6f 64 65 2e 20 0a 2a 2f 0a 53 51 4c 49 r code. .*/.SQLI
166d3 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
166d4 71 6c 69 74 65 33 42 74 72 65 65 49 6e 63 72 56 qlite3BtreeIncrV
166d5 61 63 75 75 6d 28 42 74 72 65 65 20 2a 70 29 7b acuum(Btree *p){
166d6 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 . int rc;. BtS
166d7 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e hared *pBt = p->
166d8 70 42 74 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 pBt;.. sqlite3B
166d9 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 treeEnter(p);.
166da 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 assert( pBt->inT
166db 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e ransaction==TRAN
166dc 53 5f 57 52 49 54 45 20 26 26 20 70 2d 3e 69 6e S_WRITE && p->in
166dd 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 Trans==TRANS_WRI
166de 54 45 20 29 3b 0a 20 20 69 66 28 20 21 70 42 74 TE );. if( !pBt
166df 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a ->autoVacuum ){.
166e0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
166e1 44 4f 4e 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 DONE;. }else{.
166e2 20 20 20 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c invalidateAll
166e3 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 42 OverflowCache(pB
166e4 74 29 3b 0a 20 20 20 20 72 63 20 3d 20 69 6e 63 t);. rc = inc
166e5 72 56 61 63 75 75 6d 53 74 65 70 28 70 42 74 2c rVacuumStep(pBt,
166e6 20 30 2c 20 70 61 67 65 72 50 61 67 65 63 6f 75 0, pagerPagecou
166e7 6e 74 28 70 42 74 29 29 3b 0a 20 20 7d 0a 20 20 nt(pBt));. }.
166e8 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
166e9 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 e(p);. return r
166ea 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 c;.}../*.** This
166eb 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c routine is call
166ec 65 64 20 70 72 69 6f 72 20 74 6f 20 73 71 6c 69 ed prior to sqli
166ed 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 20 77 te3PagerCommit w
166ee 68 65 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f hen a transactio
166ef 6e 0a 2a 2a 20 69 73 20 63 6f 6d 6d 69 74 65 64 n.** is commited
166f0 20 66 6f 72 20 61 6e 20 61 75 74 6f 2d 76 61 63 for an auto-vac
166f1 75 75 6d 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a uum database..**
166f2 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f 4b .** If SQLITE_OK
166f3 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68 is returned, th
166f4 65 6e 20 2a 70 6e 54 72 75 6e 63 20 69 73 20 73 en *pnTrunc is s
166f5 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 et to the number
166f6 20 6f 66 20 70 61 67 65 73 0a 2a 2a 20 74 68 65 of pages.** the
166f7 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 73 database file s
166f8 68 6f 75 6c 64 20 62 65 20 74 72 75 6e 63 61 74 hould be truncat
166f9 65 64 20 74 6f 20 64 75 72 69 6e 67 20 74 68 65 ed to during the
166fa 20 63 6f 6d 6d 69 74 20 70 72 6f 63 65 73 73 2e commit process.
166fb 20 0a 2a 2a 20 69 2e 65 2e 20 74 68 65 20 64 61 .** i.e. the da
166fc 74 61 62 61 73 65 20 68 61 73 20 62 65 65 6e 20 tabase has been
166fd 72 65 6f 72 67 61 6e 69 7a 65 64 20 73 6f 20 74 reorganized so t
166fe 68 61 74 20 6f 6e 6c 79 20 74 68 65 20 66 69 72 hat only the fir
166ff 73 74 20 2a 70 6e 54 72 75 6e 63 0a 2a 2a 20 70 st *pnTrunc.** p
16700 61 67 65 73 20 61 72 65 20 69 6e 20 75 73 65 2e ages are in use.
16701 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 .*/.static int a
16702 75 74 6f 56 61 63 75 75 6d 43 6f 6d 6d 69 74 28 utoVacuumCommit(
16703 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a BtShared *pBt){.
16704 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
16705 45 5f 4f 4b 3b 0a 20 20 50 61 67 65 72 20 2a 70 E_OK;. Pager *p
16706 50 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 50 61 Pager = pBt->pPa
16707 67 65 72 3b 0a 20 20 56 56 41 5f 4f 4e 4c 59 28 ger;. VVA_ONLY(
16708 20 69 6e 74 20 6e 52 65 66 20 3d 20 73 71 6c 69 int nRef = sqli
16709 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 te3PagerRefcount
1670a 28 70 50 61 67 65 72 29 20 29 3b 0a 0a 20 20 61 (pPager) );.. a
1670b 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
1670c 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d utex_held(pBt->m
1670d 75 74 65 78 29 20 29 3b 0a 20 20 69 6e 76 61 6c utex) );. inval
1670e 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 idateAllOverflow
1670f 43 61 63 68 65 28 70 42 74 29 3b 0a 20 20 61 73 Cache(pBt);. as
16710 73 65 72 74 28 70 42 74 2d 3e 61 75 74 6f 56 61 sert(pBt->autoVa
16711 63 75 75 6d 29 3b 0a 20 20 69 66 28 20 21 70 42 cuum);. if( !pB
16712 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 29 7b t->incrVacuum ){
16713 0a 20 20 20 20 50 67 6e 6f 20 6e 46 69 6e 3b 20 . Pgno nFin;
16714 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
16715 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 64 61 r of pages in da
16716 74 61 62 61 73 65 20 61 66 74 65 72 20 61 75 74 tabase after aut
16717 6f 76 61 63 75 75 6d 69 6e 67 20 2a 2f 0a 20 20 ovacuuming */.
16718 20 20 50 67 6e 6f 20 6e 46 72 65 65 3b 20 20 20 Pgno nFree;
16719 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1671a 66 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20 66 f pages on the f
1671b 72 65 65 6c 69 73 74 20 69 6e 69 74 69 61 6c 6c reelist initiall
1671c 79 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 6e 50 y */. Pgno nP
1671d 74 72 6d 61 70 3b 20 20 20 20 20 20 2f 2a 20 4e trmap; /* N
1671e 75 6d 62 65 72 20 6f 66 20 50 74 72 4d 61 70 20 umber of PtrMap
1671f 70 61 67 65 73 20 74 6f 20 62 65 20 66 72 65 65 pages to be free
16720 64 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 69 46 d */. Pgno iF
16721 72 65 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 ree; /* T
16722 68 65 20 6e 65 78 74 20 70 61 67 65 20 74 6f 20 he next page to
16723 62 65 20 66 72 65 65 64 20 2a 2f 0a 20 20 20 20 be freed */.
16724 69 6e 74 20 6e 45 6e 74 72 79 3b 20 20 20 20 20 int nEntry;
16725 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
16726 65 6e 74 72 69 65 73 20 6f 6e 20 6f 6e 65 20 70 entries on one p
16727 74 72 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20 20 trmap page */.
16728 20 20 50 67 6e 6f 20 6e 4f 72 69 67 3b 20 20 20 Pgno nOrig;
16729 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 /* Database
1672a 20 73 69 7a 65 20 62 65 66 6f 72 65 20 66 72 65 size before fre
1672b 65 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 6e 4f 72 eing */.. nOr
1672c 69 67 20 3d 20 70 61 67 65 72 50 61 67 65 63 6f ig = pagerPageco
1672d 75 6e 74 28 70 42 74 29 3b 0a 20 20 20 20 69 66 unt(pBt);. if
1672e 28 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 ( PTRMAP_ISPAGE(
1672f 70 42 74 2c 20 6e 4f 72 69 67 29 20 7c 7c 20 6e pBt, nOrig) || n
16730 4f 72 69 67 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 Orig==PENDING_BY
16731 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a TE_PAGE(pBt) ){.
16732 20 20 20 20 20 20 2f 2a 20 49 74 20 69 73 20 6e /* It is n
16733 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 63 ot possible to c
16734 72 65 61 74 65 20 61 20 64 61 74 61 62 61 73 65 reate a database
16735 20 66 6f 72 20 77 68 69 63 68 20 74 68 65 20 66 for which the f
16736 69 6e 61 6c 20 70 61 67 65 0a 20 20 20 20 20 20 inal page.
16737 2a 2a 20 69 73 20 65 69 74 68 65 72 20 61 20 70 ** is either a p
16738 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 20 ointer-map page
16739 6f 72 20 74 68 65 20 70 65 6e 64 69 6e 67 2d 62 or the pending-b
1673a 79 74 65 20 70 61 67 65 2e 20 49 66 20 6f 6e 65 yte page. If one
1673b 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 65 6e 63 . ** is enc
1673c 6f 75 6e 74 65 72 65 64 2c 20 74 68 69 73 20 69 ountered, this i
1673d 6e 64 69 63 61 74 65 73 20 63 6f 72 72 75 70 74 ndicates corrupt
1673e 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ion.. */.
1673f 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
16740 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
16741 20 20 20 20 7d 0a 0a 20 20 20 20 6e 46 72 65 65 }.. nFree
16742 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 42 74 = get4byte(&pBt
16743 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b ->pPage1->aData[
16744 33 36 5d 29 3b 0a 20 20 20 20 6e 45 6e 74 72 79 36]);. nEntry
16745 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 = pBt->usableSi
16746 7a 65 2f 35 3b 0a 20 20 20 20 6e 50 74 72 6d 61 ze/5;. nPtrma
16747 70 20 3d 20 28 6e 46 72 65 65 2d 6e 4f 72 69 67 p = (nFree-nOrig
16748 2b 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 +PTRMAP_PAGENO(p
16749 42 74 2c 20 6e 4f 72 69 67 29 2b 6e 45 6e 74 72 Bt, nOrig)+nEntr
1674a 79 29 2f 6e 45 6e 74 72 79 3b 0a 20 20 20 20 6e y)/nEntry;. n
1674b 46 69 6e 20 3d 20 6e 4f 72 69 67 20 2d 20 6e 46 Fin = nOrig - nF
1674c 72 65 65 20 2d 20 6e 50 74 72 6d 61 70 3b 0a 20 ree - nPtrmap;.
1674d 20 20 20 69 66 28 20 6e 4f 72 69 67 3e 50 45 4e if( nOrig>PEN
1674e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 DING_BYTE_PAGE(p
1674f 42 74 29 20 26 26 20 6e 46 69 6e 3c 50 45 4e 44 Bt) && nFin<PEND
16750 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 ING_BYTE_PAGE(pB
16751 74 29 20 29 7b 0a 20 20 20 20 20 20 6e 46 69 6e t) ){. nFin
16752 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 77 68 --;. }. wh
16753 69 6c 65 28 20 50 54 52 4d 41 50 5f 49 53 50 41 ile( PTRMAP_ISPA
16754 47 45 28 70 42 74 2c 20 6e 46 69 6e 29 20 7c 7c GE(pBt, nFin) ||
16755 20 6e 46 69 6e 3d 3d 50 45 4e 44 49 4e 47 5f 42 nFin==PENDING_B
16756 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b YTE_PAGE(pBt) ){
16757 0a 20 20 20 20 20 20 6e 46 69 6e 2d 2d 3b 0a 20 . nFin--;.
16758 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 46 69 }. if( nFi
16759 6e 3e 6e 4f 72 69 67 20 29 20 72 65 74 75 72 6e n>nOrig ) return
1675a 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
1675b 42 4b 50 54 3b 0a 0a 20 20 20 20 66 6f 72 28 69 BKPT;.. for(i
1675c 46 72 65 65 3d 6e 4f 72 69 67 3b 20 69 46 72 65 Free=nOrig; iFre
1675d 65 3e 6e 46 69 6e 20 26 26 20 72 63 3d 3d 53 51 e>nFin && rc==SQ
1675e 4c 49 54 45 5f 4f 4b 3b 20 69 46 72 65 65 2d 2d LITE_OK; iFree--
1675f 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 69 6e ){. rc = in
16760 63 72 56 61 63 75 75 6d 53 74 65 70 28 70 42 74 crVacuumStep(pBt
16761 2c 20 6e 46 69 6e 2c 20 69 46 72 65 65 29 3b 0a , nFin, iFree);.
16762 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 72 }. if( (r
16763 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 7c c==SQLITE_DONE |
16764 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 | rc==SQLITE_OK)
16765 20 26 26 20 6e 46 72 65 65 3e 30 20 29 7b 0a 20 && nFree>0 ){.
16766 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
16767 5f 4f 4b 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 _OK;. rc =
16768 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
16769 65 28 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 70 e(pBt->pPage1->p
1676a 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 70 DbPage);. p
1676b 75 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 ut4byte(&pBt->pP
1676c 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c age1->aData[32],
1676d 20 30 29 3b 0a 20 20 20 20 20 20 70 75 74 34 62 0);. put4b
1676e 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 yte(&pBt->pPage1
1676f 2d 3e 61 44 61 74 61 5b 33 36 5d 2c 20 30 29 3b ->aData[36], 0);
16770 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 . sqlite3Pa
16771 67 65 72 54 72 75 6e 63 61 74 65 49 6d 61 67 65 gerTruncateImage
16772 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6e 46 (pBt->pPager, nF
16773 69 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 in);. }. i
16774 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
16775 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
16776 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70 3PagerRollback(p
16777 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 Pager);. }.
16778 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 52 65 }.. assert( nRe
16779 66 3d 3d 73 71 6c 69 74 65 33 50 61 67 65 72 52 f==sqlite3PagerR
1677a 65 66 63 6f 75 6e 74 28 70 50 61 67 65 72 29 20 efcount(pPager)
1677b 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
1677c 7d 0a 0a 23 65 6c 73 65 20 2f 2a 20 69 66 6e 64 }..#else /* ifnd
1677d 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
1677e 55 54 4f 56 41 43 55 55 4d 20 2a 2f 0a 23 20 64 UTOVACUUM */.# d
1677f 65 66 69 6e 65 20 73 65 74 43 68 69 6c 64 50 74 efine setChildPt
16780 72 6d 61 70 73 28 78 29 20 53 51 4c 49 54 45 5f rmaps(x) SQLITE_
16781 4f 4b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a OK.#endif../*.**
16782 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f This routine do
16783 65 73 20 74 68 65 20 66 69 72 73 74 20 70 68 61 es the first pha
16784 73 65 20 6f 66 20 61 20 74 77 6f 2d 70 68 61 73 se of a two-phas
16785 65 20 63 6f 6d 6d 69 74 2e 20 20 54 68 69 73 20 e commit. This
16786 72 6f 75 74 69 6e 65 0a 2a 2a 20 63 61 75 73 65 routine.** cause
16787 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 s a rollback jou
16788 72 6e 61 6c 20 74 6f 20 62 65 20 63 72 65 61 74 rnal to be creat
16789 65 64 20 28 69 66 20 69 74 20 64 6f 65 73 20 6e ed (if it does n
1678a 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 ot already exist
1678b 29 0a 2a 2a 20 61 6e 64 20 70 6f 70 75 6c 61 74 ).** and populat
1678c 65 64 20 77 69 74 68 20 65 6e 6f 75 67 68 20 69 ed with enough i
1678d 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 6f 20 74 68 nformation so th
1678e 61 74 20 69 66 20 61 20 70 6f 77 65 72 20 6c 6f at if a power lo
1678f 73 73 20 6f 63 63 75 72 73 0a 2a 2a 20 74 68 65 ss occurs.** the
16790 20 64 61 74 61 62 61 73 65 20 63 61 6e 20 62 65 database can be
16791 20 72 65 73 74 6f 72 65 64 20 74 6f 20 69 74 73 restored to its
16792 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 65 20 original state
16793 62 79 20 70 6c 61 79 69 6e 67 20 62 61 63 6b 0a by playing back.
16794 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 ** the journal.
16795 20 54 68 65 6e 20 74 68 65 20 63 6f 6e 74 65 6e Then the conten
16796 74 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 ts of the journa
16797 6c 20 61 72 65 20 66 6c 75 73 68 65 64 20 6f 75 l are flushed ou
16798 74 20 74 6f 0a 2a 2a 20 74 68 65 20 64 69 73 6b t to.** the disk
16799 2e 20 20 41 66 74 65 72 20 74 68 65 20 6a 6f 75 . After the jou
1679a 72 6e 61 6c 20 69 73 20 73 61 66 65 6c 79 20 6f rnal is safely o
1679b 6e 20 6f 78 69 64 65 2c 20 74 68 65 20 63 68 61 n oxide, the cha
1679c 6e 67 65 73 20 74 6f 20 74 68 65 0a 2a 2a 20 64 nges to the.** d
1679d 61 74 61 62 61 73 65 20 61 72 65 20 77 72 69 74 atabase are writ
1679e 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 64 61 74 ten into the dat
1679f 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 66 abase file and f
167a0 6c 75 73 68 65 64 20 74 6f 20 6f 78 69 64 65 2e lushed to oxide.
167a1 0a 2a 2a 20 41 74 20 74 68 65 20 65 6e 64 20 6f .** At the end o
167a2 66 20 74 68 69 73 20 63 61 6c 6c 2c 20 74 68 65 f this call, the
167a3 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 rollback journa
167a4 6c 20 73 74 69 6c 6c 20 65 78 69 73 74 73 20 6f l still exists o
167a5 6e 20 74 68 65 0a 2a 2a 20 64 69 73 6b 20 61 6e n the.** disk an
167a6 64 20 77 65 20 61 72 65 20 73 74 69 6c 6c 20 68 d we are still h
167a7 6f 6c 64 69 6e 67 20 61 6c 6c 20 6c 6f 63 6b 73 olding all locks
167a8 2c 20 73 6f 20 74 68 65 20 74 72 61 6e 73 61 63 , so the transac
167a9 74 69 6f 6e 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 tion has not.**
167aa 63 6f 6d 6d 69 74 74 65 64 2e 20 20 53 65 65 20 committed. See
167ab 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d sqlite3BtreeComm
167ac 69 74 50 68 61 73 65 54 77 6f 28 29 20 66 6f 72 itPhaseTwo() for
167ad 20 74 68 65 20 73 65 63 6f 6e 64 20 70 68 61 73 the second phas
167ae 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 6d 6d e of the.** comm
167af 69 74 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a it process..**.*
167b0 2a 20 54 68 69 73 20 63 61 6c 6c 20 69 73 20 61 * This call is a
167b1 20 6e 6f 2d 6f 70 20 69 66 20 6e 6f 20 77 72 69 no-op if no wri
167b2 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 te-transaction i
167b3 73 20 63 75 72 72 65 6e 74 6c 79 20 61 63 74 69 s currently acti
167b4 76 65 20 6f 6e 20 70 42 74 2e 0a 2a 2a 0a 2a 2a ve on pBt..**.**
167b5 20 4f 74 68 65 72 77 69 73 65 2c 20 73 79 6e 63 Otherwise, sync
167b6 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
167b7 6c 65 20 66 6f 72 20 74 68 65 20 62 74 72 65 65 le for the btree
167b8 20 70 42 74 2e 20 7a 4d 61 73 74 65 72 20 70 6f pBt. zMaster po
167b9 69 6e 74 73 20 74 6f 0a 2a 2a 20 74 68 65 20 6e ints to.** the n
167ba 61 6d 65 20 6f 66 20 61 20 6d 61 73 74 65 72 20 ame of a master
167bb 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68 61 journal file tha
167bc 74 20 73 68 6f 75 6c 64 20 62 65 20 77 72 69 74 t should be writ
167bd 74 65 6e 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 ten into the.**
167be 69 6e 64 69 76 69 64 75 61 6c 20 6a 6f 75 72 6e individual journ
167bf 61 6c 20 66 69 6c 65 2c 20 6f 72 20 69 73 20 4e al file, or is N
167c0 55 4c 4c 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 ULL, indicating
167c1 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 no master journa
167c2 6c 20 66 69 6c 65 20 0a 2a 2a 20 28 73 69 6e 67 l file .** (sing
167c3 6c 65 20 64 61 74 61 62 61 73 65 20 74 72 61 6e le database tran
167c4 73 61 63 74 69 6f 6e 29 2e 0a 2a 2a 0a 2a 2a 20 saction)..**.**
167c5 57 68 65 6e 20 74 68 69 73 20 69 73 20 63 61 6c When this is cal
167c6 6c 65 64 2c 20 74 68 65 20 6d 61 73 74 65 72 20 led, the master
167c7 6a 6f 75 72 6e 61 6c 20 73 68 6f 75 6c 64 20 61 journal should a
167c8 6c 72 65 61 64 79 20 68 61 76 65 20 62 65 65 6e lready have been
167c9 0a 2a 2a 20 63 72 65 61 74 65 64 2c 20 70 6f 70 .** created, pop
167ca 75 6c 61 74 65 64 20 77 69 74 68 20 74 68 69 73 ulated with this
167cb 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e 74 65 72 journal pointer
167cc 20 61 6e 64 20 73 79 6e 63 65 64 20 74 6f 20 64 and synced to d
167cd 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 isk..**.** Once
167ce 74 68 69 73 20 69 73 20 72 6f 75 74 69 6e 65 20 this is routine
167cf 68 61 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68 has returned, th
167d0 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 72 65 71 e only thing req
167d1 75 69 72 65 64 20 74 6f 20 63 6f 6d 6d 69 74 0a uired to commit.
167d2 2a 2a 20 74 68 65 20 77 72 69 74 65 2d 74 72 61 ** the write-tra
167d3 6e 73 61 63 74 69 6f 6e 20 66 6f 72 20 74 68 69 nsaction for thi
167d4 73 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 s database file
167d5 69 73 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65 is to delete the
167d6 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2f 0a 53 51 4c journal..*/.SQL
167d7 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
167d8 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d sqlite3BtreeComm
167d9 69 74 50 68 61 73 65 4f 6e 65 28 42 74 72 65 65 itPhaseOne(Btree
167da 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 *p, const char
167db 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20 69 6e 74 *zMaster){. int
167dc 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
167dd 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e . if( p->inTran
167de 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 s==TRANS_WRITE )
167df 7b 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 2a {. BtShared *
167e0 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
167e1 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
167e2 74 65 72 28 70 29 3b 0a 23 69 66 6e 64 65 66 20 ter(p);.#ifndef
167e3 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
167e4 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70 VACUUM. if( p
167e5 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 Bt->autoVacuum )
167e6 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61 75 74 {. rc = aut
167e7 6f 56 61 63 75 75 6d 43 6f 6d 6d 69 74 28 70 42 oVacuumCommit(pB
167e8 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 t);. if( rc
167e9 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
167ea 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 sqlite3Bt
167eb 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 reeLeave(p);.
167ec 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
167ed 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 }. }.#e
167ee 6e 64 69 66 0a 20 20 20 20 72 63 20 3d 20 73 71 ndif. rc = sq
167ef 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 lite3PagerCommit
167f0 50 68 61 73 65 4f 6e 65 28 70 42 74 2d 3e 70 50 PhaseOne(pBt->pP
167f1 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 2c 20 30 ager, zMaster, 0
167f2 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 );. sqlite3Bt
167f3 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 7d reeLeave(p);. }
167f4 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
167f5 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
167f6 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 66 tion is called f
167f7 72 6f 6d 20 62 6f 74 68 20 42 74 72 65 65 43 6f rom both BtreeCo
167f8 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20 61 mmitPhaseTwo() a
167f9 6e 64 20 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b nd BtreeRollback
167fa 28 29 0a 2a 2a 20 61 74 20 74 68 65 20 63 6f 6e ().** at the con
167fb 63 6c 75 73 69 6f 6e 20 6f 66 20 61 20 74 72 61 clusion of a tra
167fc 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 nsaction..*/.sta
167fd 74 69 63 20 76 6f 69 64 20 62 74 72 65 65 45 6e tic void btreeEn
167fe 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 42 74 72 dTransaction(Btr
167ff 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 ee *p){. BtShar
16800 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
16801 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
16802 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 te3BtreeHoldsMut
16803 65 78 28 70 29 20 29 3b 0a 0a 20 20 62 74 72 65 ex(p) );.. btre
16804 65 43 6c 65 61 72 48 61 73 43 6f 6e 74 65 6e 74 eClearHasContent
16805 28 70 42 74 29 3b 0a 20 20 69 66 28 20 70 2d 3e (pBt);. if( p->
16806 69 6e 54 72 61 6e 73 3e 54 52 41 4e 53 5f 4e 4f inTrans>TRANS_NO
16807 4e 45 20 26 26 20 70 2d 3e 64 62 2d 3e 61 63 74 NE && p->db->act
16808 69 76 65 56 64 62 65 43 6e 74 3e 31 20 29 7b 0a iveVdbeCnt>1 ){.
16809 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 /* If there
1680a 61 72 65 20 6f 74 68 65 72 20 61 63 74 69 76 65 are other active
1680b 20 73 74 61 74 65 6d 65 6e 74 73 20 74 68 61 74 statements that
1680c 20 62 65 6c 6f 6e 67 20 74 6f 20 74 68 69 73 20 belong to this
1680d 64 61 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 database. **
1680e 68 61 6e 64 6c 65 2c 20 64 6f 77 6e 67 72 61 64 handle, downgrad
1680f 65 20 74 6f 20 61 20 72 65 61 64 2d 6f 6e 6c 79 e to a read-only
16810 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 transaction. Th
16811 65 20 6f 74 68 65 72 20 73 74 61 74 65 6d 65 6e e other statemen
16812 74 73 0a 20 20 20 20 2a 2a 20 6d 61 79 20 73 74 ts. ** may st
16813 69 6c 6c 20 62 65 20 72 65 61 64 69 6e 67 20 66 ill be reading f
16814 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 rom the database
16815 2e 20 20 2a 2f 0a 20 20 20 20 64 6f 77 6e 67 72 . */. downgr
16816 61 64 65 41 6c 6c 53 68 61 72 65 64 43 61 63 68 adeAllSharedCach
16817 65 54 61 62 6c 65 4c 6f 63 6b 73 28 70 29 3b 0a eTableLocks(p);.
16818 20 20 20 20 70 2d 3e 69 6e 54 72 61 6e 73 20 3d p->inTrans =
16819 20 54 52 41 4e 53 5f 52 45 41 44 3b 0a 20 20 7d TRANS_READ;. }
1681a 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 else{. /* If
1681b 74 68 65 20 68 61 6e 64 6c 65 20 68 61 64 20 61 the handle had a
1681c 6e 79 20 6b 69 6e 64 20 6f 66 20 74 72 61 6e 73 ny kind of trans
1681d 61 63 74 69 6f 6e 20 6f 70 65 6e 2c 20 64 65 63 action open, dec
1681e 72 65 6d 65 6e 74 20 74 68 65 20 0a 20 20 20 20 rement the .
1681f 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 ** transaction c
16820 6f 75 6e 74 20 6f 66 20 74 68 65 20 73 68 61 72 ount of the shar
16821 65 64 20 62 74 72 65 65 2e 20 49 66 20 74 68 65 ed btree. If the
16822 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 transaction cou
16823 6e 74 20 0a 20 20 20 20 2a 2a 20 72 65 61 63 68 nt . ** reach
16824 65 73 20 30 2c 20 73 65 74 20 74 68 65 20 73 68 es 0, set the sh
16825 61 72 65 64 20 73 74 61 74 65 20 74 6f 20 54 52 ared state to TR
16826 41 4e 53 5f 4e 4f 4e 45 2e 20 54 68 65 20 75 6e ANS_NONE. The un
16827 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 lockBtreeIfUnuse
16828 64 28 29 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20 d(). ** call
16829 62 65 6c 6f 77 20 77 69 6c 6c 20 75 6e 6c 6f 63 below will unloc
1682a 6b 20 74 68 65 20 70 61 67 65 72 2e 20 20 2a 2f k the pager. */
1682b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 . if( p->inTr
1682c 61 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 ans!=TRANS_NONE
1682d 29 7b 0a 20 20 20 20 20 20 63 6c 65 61 72 41 6c ){. clearAl
1682e 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c lSharedCacheTabl
1682f 65 4c 6f 63 6b 73 28 70 29 3b 0a 20 20 20 20 20 eLocks(p);.
16830 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 pBt->nTransacti
16831 6f 6e 2d 2d 3b 0a 20 20 20 20 20 20 69 66 28 20 on--;. if(
16832 30 3d 3d 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 0==pBt->nTransac
16833 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 tion ){.
16834 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 pBt->inTransacti
16835 6f 6e 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b on = TRANS_NONE;
16836 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a . }. }..
16837 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 63 /* Set the c
16838 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 urrent transacti
16839 6f 6e 20 73 74 61 74 65 20 74 6f 20 54 52 41 4e on state to TRAN
1683a 53 5f 4e 4f 4e 45 20 61 6e 64 20 75 6e 6c 6f 63 S_NONE and unloc
1683b 6b 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 70 61 k the . ** pa
1683c 67 65 72 20 69 66 20 74 68 69 73 20 63 61 6c 6c ger if this call
1683d 20 63 6c 6f 73 65 64 20 74 68 65 20 6f 6e 6c 79 closed the only
1683e 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 read or write t
1683f 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 2a 2f 0a ransaction. */.
16840 20 20 20 20 70 2d 3e 69 6e 54 72 61 6e 73 20 3d p->inTrans =
16841 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 20 TRANS_NONE;.
16842 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e unlockBtreeIfUn
16843 75 73 65 64 28 70 42 74 29 3b 0a 20 20 7d 0a 0a used(pBt);. }..
16844 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 btreeIntegrity
16845 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f (p);.}../*.** Co
16846 6d 6d 69 74 20 74 68 65 20 74 72 61 6e 73 61 63 mmit the transac
16847 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 69 tion currently i
16848 6e 20 70 72 6f 67 72 65 73 73 2e 0a 2a 2a 0a 2a n progress..**.*
16849 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
1684a 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 73 65 mplements the se
1684b 63 6f 6e 64 20 70 68 61 73 65 20 6f 66 20 61 20 cond phase of a
1684c 32 2d 70 68 61 73 65 20 63 6f 6d 6d 69 74 2e 20 2-phase commit.
1684d 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 42 The.** sqlite3B
1684e 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f treeCommitPhaseO
1684f 6e 65 28 29 20 72 6f 75 74 69 6e 65 20 64 6f 65 ne() routine doe
16850 73 20 74 68 65 20 66 69 72 73 74 20 70 68 61 73 s the first phas
16851 65 20 61 6e 64 20 73 68 6f 75 6c 64 0a 2a 2a 20 e and should.**
16852 62 65 20 69 6e 76 6f 6b 65 64 20 70 72 69 6f 72 be invoked prior
16853 20 74 6f 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 to calling this
16854 20 72 6f 75 74 69 6e 65 2e 20 20 54 68 65 20 73 routine. The s
16855 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 qlite3BtreeCommi
16856 74 50 68 61 73 65 4f 6e 65 28 29 0a 2a 2a 20 72 tPhaseOne().** r
16857 6f 75 74 69 6e 65 20 64 69 64 20 61 6c 6c 20 74 outine did all t
16858 68 65 20 77 6f 72 6b 20 6f 66 20 77 72 69 74 69 he work of writi
16859 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f ng information o
1685a 75 74 20 74 6f 20 64 69 73 6b 20 61 6e 64 20 66 ut to disk and f
1685b 6c 75 73 68 69 6e 67 20 74 68 65 0a 2a 2a 20 63 lushing the.** c
1685c 6f 6e 74 65 6e 74 73 20 73 6f 20 74 68 61 74 20 ontents so that
1685d 74 68 65 79 20 61 72 65 20 77 72 69 74 74 65 6e they are written
1685e 20 6f 6e 74 6f 20 74 68 65 20 64 69 73 6b 20 70 onto the disk p
1685f 6c 61 74 74 65 72 2e 20 20 41 6c 6c 20 74 68 69 latter. All thi
16860 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 68 61 73 s.** routine has
16861 20 74 6f 20 64 6f 20 69 73 20 64 65 6c 65 74 65 to do is delete
16862 20 6f 72 20 74 72 75 6e 63 61 74 65 20 6f 72 20 or truncate or
16863 7a 65 72 6f 20 74 68 65 20 68 65 61 64 65 72 20 zero the header
16864 69 6e 20 74 68 65 0a 2a 2a 20 74 68 65 20 72 6f in the.** the ro
16865 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 28 llback journal (
16866 77 68 69 63 68 20 63 61 75 73 65 73 20 74 68 65 which causes the
16867 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20 transaction to
16868 63 6f 6d 6d 69 74 29 20 61 6e 64 0a 2a 2a 20 64 commit) and.** d
16869 72 6f 70 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a rop locks..**.**
1686a 20 54 68 69 73 20 77 69 6c 6c 20 72 65 6c 65 61 This will relea
1686b 73 65 20 74 68 65 20 77 72 69 74 65 20 6c 6f 63 se the write loc
1686c 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 k on the databas
1686d 65 20 66 69 6c 65 2e 20 20 49 66 20 74 68 65 72 e file. If ther
1686e 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 69 e.** are no acti
1686f 76 65 20 63 75 72 73 6f 72 73 2c 20 69 74 20 61 ve cursors, it a
16870 6c 73 6f 20 72 65 6c 65 61 73 65 73 20 74 68 65 lso releases the
16871 20 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 read lock..*/.S
16872 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
16873 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f t sqlite3BtreeCo
16874 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 42 74 72 mmitPhaseTwo(Btr
16875 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 ee *p){. BtShar
16876 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
16877 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 ;.. sqlite3Btre
16878 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 62 74 72 eEnter(p);. btr
16879 65 65 49 6e 74 65 67 72 69 74 79 28 70 29 3b 0a eeIntegrity(p);.
1687a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 68 61 6e . /* If the han
1687b 64 6c 65 20 68 61 73 20 61 20 77 72 69 74 65 2d dle has a write-
1687c 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e transaction open
1687d 2c 20 63 6f 6d 6d 69 74 20 74 68 65 20 73 68 61 , commit the sha
1687e 72 65 64 2d 62 74 72 65 65 73 20 0a 20 20 2a 2a red-btrees . **
1687f 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 transaction and
16880 20 73 65 74 20 74 68 65 20 73 68 61 72 65 64 20 set the shared
16881 73 74 61 74 65 20 74 6f 20 54 52 41 4e 53 5f 52 state to TRANS_R
16882 45 41 44 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 EAD.. */. if(
16883 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e p->inTrans==TRAN
16884 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20 69 S_WRITE ){. i
16885 6e 74 20 72 63 3b 0a 20 20 20 20 61 73 73 65 72 nt rc;. asser
16886 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 t( pBt->inTransa
16887 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 ction==TRANS_WRI
16888 54 45 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 TE );. assert
16889 28 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 ( pBt->nTransact
1688a 69 6f 6e 3e 30 20 29 3b 0a 20 20 20 20 72 63 20 ion>0 );. rc
1688b 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f = sqlite3PagerCo
1688c 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70 42 74 mmitPhaseTwo(pBt
1688d 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69 ->pPager);. i
1688e 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1688f 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
16890 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
16891 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
16892 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 74 2d 3e . }. pBt->
16893 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 inTransaction =
16894 54 52 41 4e 53 5f 52 45 41 44 3b 0a 20 20 7d 0a TRANS_READ;. }.
16895 0a 20 20 62 74 72 65 65 45 6e 64 54 72 61 6e 73 . btreeEndTrans
16896 61 63 74 69 6f 6e 28 70 29 3b 0a 20 20 73 71 6c action(p);. sql
16897 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
16898 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
16899 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
1689a 44 6f 20 62 6f 74 68 20 70 68 61 73 65 73 20 6f Do both phases o
1689b 66 20 61 20 63 6f 6d 6d 69 74 2e 0a 2a 2f 0a 53 f a commit..*/.S
1689c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1689d 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f t sqlite3BtreeCo
1689e 6d 6d 69 74 28 42 74 72 65 65 20 2a 70 29 7b 0a mmit(Btree *p){.
1689f 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 int rc;. sqli
168a0 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 te3BtreeEnter(p)
168a1 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
168a2 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 BtreeCommitPhase
168a3 4f 6e 65 28 70 2c 20 30 29 3b 0a 20 20 69 66 28 One(p, 0);. if(
168a4 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
168a5 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
168a6 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 e3BtreeCommitPha
168a7 73 65 54 77 6f 28 70 29 3b 0a 20 20 7d 0a 20 20 seTwo(p);. }.
168a8 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
168a9 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 e(p);. return r
168aa 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 c;.}..#ifndef ND
168ab 45 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 EBUG./*.** Retur
168ac 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 n the number of
168ad 77 72 69 74 65 2d 63 75 72 73 6f 72 73 20 6f 70 write-cursors op
168ae 65 6e 20 6f 6e 20 74 68 69 73 20 68 61 6e 64 6c en on this handl
168af 65 2e 20 54 68 69 73 20 69 73 20 66 6f 72 20 75 e. This is for u
168b0 73 65 0a 2a 2a 20 69 6e 20 61 73 73 65 72 74 28 se.** in assert(
168b1 29 20 65 78 70 72 65 73 73 69 6f 6e 73 2c 20 73 ) expressions, s
168b2 6f 20 69 74 20 69 73 20 6f 6e 6c 79 20 63 6f 6d o it is only com
168b3 70 69 6c 65 64 20 69 66 20 4e 44 45 42 55 47 20 piled if NDEBUG
168b4 69 73 20 6e 6f 74 0a 2a 2a 20 64 65 66 69 6e 65 is not.** define
168b5 64 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 d..**.** For the
168b6 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69 purposes of thi
168b7 73 20 72 6f 75 74 69 6e 65 2c 20 61 20 77 72 69 s routine, a wri
168b8 74 65 2d 63 75 72 73 6f 72 20 69 73 20 61 6e 79 te-cursor is any
168b9 20 63 75 72 73 6f 72 20 74 68 61 74 0a 2a 2a 20 cursor that.**
168ba 69 73 20 63 61 70 61 62 6c 65 20 6f 66 20 77 72 is capable of wr
168bb 69 74 69 6e 67 20 74 6f 20 74 68 65 20 64 61 74 iting to the dat
168bc 61 62 73 65 2e 20 20 54 68 61 74 20 6d 65 61 6e abse. That mean
168bd 73 20 74 68 65 20 63 75 72 73 6f 72 20 77 61 73 s the cursor was
168be 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 6c 79 20 6f .** originally o
168bf 70 65 6e 65 64 20 66 6f 72 20 77 72 69 74 69 6e pened for writin
168c0 67 20 61 6e 64 20 74 68 65 20 63 75 72 73 6f 72 g and the cursor
168c1 20 68 61 73 20 6e 6f 74 20 62 65 20 64 69 73 61 has not be disa
168c2 62 6c 65 64 0a 2a 2a 20 62 79 20 68 61 76 69 6e bled.** by havin
168c3 67 20 69 74 73 20 73 74 61 74 65 20 63 68 61 6e g its state chan
168c4 67 65 64 20 74 6f 20 43 55 52 53 4f 52 5f 46 41 ged to CURSOR_FA
168c5 55 4c 54 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ULT..*/.static i
168c6 6e 74 20 63 6f 75 6e 74 57 72 69 74 65 43 75 72 nt countWriteCur
168c7 73 6f 72 73 28 42 74 53 68 61 72 65 64 20 2a 70 sors(BtShared *p
168c8 42 74 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 Bt){. BtCursor
168c9 2a 70 43 75 72 3b 0a 20 20 69 6e 74 20 72 20 3d *pCur;. int r =
168ca 20 30 3b 0a 20 20 66 6f 72 28 70 43 75 72 3d 70 0;. for(pCur=p
168cb 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 43 75 Bt->pCursor; pCu
168cc 72 3b 20 70 43 75 72 3d 70 43 75 72 2d 3e 70 4e r; pCur=pCur->pN
168cd 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 43 ext){. if( pC
168ce 75 72 2d 3e 77 72 46 6c 61 67 20 26 26 20 70 43 ur->wrFlag && pC
168cf 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53 ur->eState!=CURS
168d0 4f 52 5f 46 41 55 4c 54 20 29 20 72 2b 2b 3b 20 OR_FAULT ) r++;
168d1 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 3b . }. return r;
168d2 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a .}.#endif../*.**
168d3 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 This routine se
168d4 74 73 20 74 68 65 20 73 74 61 74 65 20 74 6f 20 ts the state to
168d5 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 61 6e 64 CURSOR_FAULT and
168d6 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f the error.** co
168d7 64 65 20 74 6f 20 65 72 72 43 6f 64 65 20 66 6f de to errCode fo
168d8 72 20 65 76 65 72 79 20 63 75 72 73 6f 72 20 6f r every cursor o
168d9 6e 20 42 74 53 68 61 72 65 64 20 74 68 61 74 20 n BtShared that
168da 70 42 74 72 65 65 0a 2a 2a 20 72 65 66 65 72 65 pBtree.** refere
168db 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 45 76 65 72 nces..**.** Ever
168dc 79 20 63 75 72 73 6f 72 20 69 73 20 74 72 69 70 y cursor is trip
168dd 70 65 64 2c 20 69 6e 63 6c 75 64 69 6e 67 20 63 ped, including c
168de 75 72 73 6f 72 73 20 74 68 61 74 20 62 65 6c 6f ursors that belo
168df 6e 67 0a 2a 2a 20 74 6f 20 6f 74 68 65 72 20 64 ng.** to other d
168e0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
168e1 6f 6e 73 20 74 68 61 74 20 68 61 70 70 65 6e 20 ons that happen
168e2 74 6f 20 62 65 20 73 68 61 72 69 6e 67 0a 2a 2a to be sharing.**
168e3 20 74 68 65 20 63 61 63 68 65 20 77 69 74 68 20 the cache with
168e4 70 42 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 pBtree..**.** Th
168e5 69 73 20 72 6f 75 74 69 6e 65 20 67 65 74 73 20 is routine gets
168e6 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 20 72 6f called when a ro
168e7 6c 6c 62 61 63 6b 20 6f 63 63 75 72 73 2e 0a 2a llback occurs..*
168e8 2a 20 41 6c 6c 20 63 75 72 73 6f 72 73 20 75 73 * All cursors us
168e9 69 6e 67 20 74 68 65 20 73 61 6d 65 20 63 61 63 ing the same cac
168ea 68 65 20 6d 75 73 74 20 62 65 20 74 72 69 70 70 he must be tripp
168eb 65 64 0a 2a 2a 20 74 6f 20 70 72 65 76 65 6e 74 ed.** to prevent
168ec 20 74 68 65 6d 20 66 72 6f 6d 20 74 72 79 69 6e them from tryin
168ed 67 20 74 6f 20 75 73 65 20 74 68 65 20 62 74 72 g to use the btr
168ee 65 65 20 61 66 74 65 72 0a 2a 2a 20 74 68 65 20 ee after.** the
168ef 72 6f 6c 6c 62 61 63 6b 2e 20 20 54 68 65 20 72 rollback. The r
168f0 6f 6c 6c 62 61 63 6b 20 6d 61 79 20 68 61 76 65 ollback may have
168f1 20 64 65 6c 65 74 65 64 20 74 61 62 6c 65 73 0a deleted tables.
168f2 2a 2a 20 6f 72 20 6d 6f 76 65 64 20 72 6f 6f 74 ** or moved root
168f3 20 70 61 67 65 73 2c 20 73 6f 20 69 74 20 69 73 pages, so it is
168f4 20 6e 6f 74 20 73 75 66 66 69 63 69 65 6e 74 20 not sufficient
168f5 74 6f 0a 2a 2a 20 73 61 76 65 20 74 68 65 20 73 to.** save the s
168f6 74 61 74 65 20 6f 66 20 74 68 65 20 63 75 72 73 tate of the curs
168f7 6f 72 2e 20 20 54 68 65 20 63 75 72 73 6f 72 20 or. The cursor
168f8 6d 75 73 74 20 62 65 0a 2a 2a 20 69 6e 76 61 6c must be.** inval
168f9 69 64 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 idated..*/.SQLIT
168fa 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
168fb 71 6c 69 74 65 33 42 74 72 65 65 54 72 69 70 41 qlite3BtreeTripA
168fc 6c 6c 43 75 72 73 6f 72 73 28 42 74 72 65 65 20 llCursors(Btree
168fd 2a 70 42 74 72 65 65 2c 20 69 6e 74 20 65 72 72 *pBtree, int err
168fe 43 6f 64 65 29 7b 0a 20 20 42 74 43 75 72 73 6f Code){. BtCurso
168ff 72 20 2a 70 3b 0a 20 20 73 71 6c 69 74 65 33 42 r *p;. sqlite3B
16900 74 72 65 65 45 6e 74 65 72 28 70 42 74 72 65 65 treeEnter(pBtree
16901 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 72 65 );. for(p=pBtre
16902 65 2d 3e 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b e->pBt->pCursor;
16903 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b p; p=p->pNext){
16904 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 . int i;.
16905 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 sqlite3BtreeClea
16906 72 43 75 72 73 6f 72 28 70 29 3b 0a 20 20 20 20 rCursor(p);.
16907 70 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 p->eState = CURS
16908 4f 52 5f 46 41 55 4c 54 3b 0a 20 20 20 20 70 2d OR_FAULT;. p-
16909 3e 73 6b 69 70 4e 65 78 74 20 3d 20 65 72 72 43 >skipNext = errC
1690a 6f 64 65 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 ode;. for(i=0
1690b 3b 20 69 3c 3d 70 2d 3e 69 50 61 67 65 3b 20 69 ; i<=p->iPage; i
1690c 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 ++){. relea
1690d 73 65 50 61 67 65 28 70 2d 3e 61 70 50 61 67 65 sePage(p->apPage
1690e 5b 69 5d 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61 [i]);. p->a
1690f 70 50 61 67 65 5b 69 5d 20 3d 20 30 3b 0a 20 20 pPage[i] = 0;.
16910 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 }. }. sqlite
16911 33 42 74 72 65 65 4c 65 61 76 65 28 70 42 74 72 3BtreeLeave(pBtr
16912 65 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f ee);.}../*.** Ro
16913 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 61 6e 73 llback the trans
16914 61 63 74 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65 action in progre
16915 73 73 2e 20 20 41 6c 6c 20 63 75 72 73 6f 72 73 ss. All cursors
16916 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 69 6e 76 61 will be.** inva
16917 6c 69 64 65 64 20 62 79 20 74 68 69 73 20 6f 70 lided by this op
16918 65 72 61 74 69 6f 6e 2e 20 20 41 6e 79 20 61 74 eration. Any at
16919 74 65 6d 70 74 20 74 6f 20 75 73 65 20 61 20 63 tempt to use a c
1691a 75 72 73 6f 72 0a 2a 2a 20 74 68 61 74 20 77 61 ursor.** that wa
1691b 73 20 6f 70 65 6e 20 61 74 20 74 68 65 20 62 65 s open at the be
1691c 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 69 73 20 ginning of this
1691d 6f 70 65 72 61 74 69 6f 6e 20 77 69 6c 6c 20 72 operation will r
1691e 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 61 6e 20 65 esult.** in an e
1691f 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 rror..**.** This
16920 20 77 69 6c 6c 20 72 65 6c 65 61 73 65 20 74 68 will release th
16921 65 20 77 72 69 74 65 20 6c 6f 63 6b 20 6f 6e 20 e write lock on
16922 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
16923 65 2e 20 20 49 66 20 74 68 65 72 65 0a 2a 2a 20 e. If there.**
16924 61 72 65 20 6e 6f 20 61 63 74 69 76 65 20 63 75 are no active cu
16925 72 73 6f 72 73 2c 20 69 74 20 61 6c 73 6f 20 72 rsors, it also r
16926 65 6c 65 61 73 65 73 20 74 68 65 20 72 65 61 64 eleases the read
16927 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 lock..*/.SQLITE
16928 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
16929 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 ite3BtreeRollbac
1692a 6b 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 k(Btree *p){. i
1692b 6e 74 20 72 63 3b 0a 20 20 42 74 53 68 61 72 65 nt rc;. BtShare
1692c 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b d *pBt = p->pBt;
1692d 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 . MemPage *pPag
1692e 65 31 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 e1;.. sqlite3Bt
1692f 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 reeEnter(p);. r
16930 63 20 3d 20 73 61 76 65 41 6c 6c 43 75 72 73 6f c = saveAllCurso
16931 72 73 28 70 42 74 2c 20 30 2c 20 30 29 3b 0a 23 rs(pBt, 0, 0);.#
16932 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
16933 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a IT_SHARED_CACHE.
16934 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
16935 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 _OK ){. /* Th
16936 69 73 20 69 73 20 61 20 68 6f 72 72 69 62 6c 65 is is a horrible
16937 20 73 69 74 75 61 74 69 6f 6e 2e 20 41 6e 20 49 situation. An I
16938 4f 20 6f 72 20 6d 61 6c 6c 6f 63 28 29 20 65 72 O or malloc() er
16939 72 6f 72 20 6f 63 63 75 72 72 65 64 20 77 68 69 ror occurred whi
1693a 6c 73 74 0a 20 20 20 20 2a 2a 20 74 72 79 69 6e lst. ** tryin
1693b 67 20 74 6f 20 73 61 76 65 20 63 75 72 73 6f 72 g to save cursor
1693c 20 70 6f 73 69 74 69 6f 6e 73 2e 20 49 66 20 74 positions. If t
1693d 68 69 73 20 69 73 20 61 6e 20 61 75 74 6f 6d 61 his is an automa
1693e 74 69 63 20 72 6f 6c 6c 62 61 63 6b 20 28 61 73 tic rollback (as
1693f 0a 20 20 20 20 2a 2a 20 74 68 65 20 72 65 73 75 . ** the resu
16940 6c 74 20 6f 66 20 61 20 63 6f 6e 73 74 72 61 69 lt of a constrai
16941 6e 74 2c 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 nt, malloc() fai
16942 6c 75 72 65 20 6f 72 20 49 4f 20 65 72 72 6f 72 lure or IO error
16943 29 20 74 68 65 6e 20 0a 20 20 20 20 2a 2a 20 74 ) then . ** t
16944 68 65 20 63 61 63 68 65 20 6d 61 79 20 62 65 20 he cache may be
16945 69 6e 74 65 72 6e 61 6c 6c 79 20 69 6e 63 6f 6e internally incon
16946 73 69 73 74 65 6e 74 20 28 6e 6f 74 20 63 6f 6e sistent (not con
16947 74 61 69 6e 20 76 61 6c 69 64 20 74 72 65 65 73 tain valid trees
16948 29 20 73 6f 0a 20 20 20 20 2a 2a 20 77 65 20 63 ) so. ** we c
16949 61 6e 6e 6f 74 20 73 69 6d 70 6c 79 20 72 65 74 annot simply ret
1694a 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20 74 6f urn the error to
1694b 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 49 6e 73 the caller. Ins
1694c 74 65 61 64 2c 20 61 62 6f 72 74 20 0a 20 20 20 tead, abort .
1694d 20 2a 2a 20 61 6c 6c 20 71 75 65 72 69 65 73 20 ** all queries
1694e 74 68 61 74 20 6d 61 79 20 62 65 20 75 73 69 6e that may be usin
1694f 67 20 61 6e 79 20 6f 66 20 74 68 65 20 63 75 72 g any of the cur
16950 73 6f 72 73 20 74 68 61 74 20 66 61 69 6c 65 64 sors that failed
16951 20 74 6f 20 73 61 76 65 2e 0a 20 20 20 20 2a 2f to save.. */
16952 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
16953 65 54 72 69 70 41 6c 6c 43 75 72 73 6f 72 73 28 eTripAllCursors(
16954 70 2c 20 72 63 29 3b 0a 20 20 7d 0a 23 65 6e 64 p, rc);. }.#end
16955 69 66 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 if. btreeIntegr
16956 69 74 79 28 70 29 3b 0a 0a 20 20 69 66 28 20 70 ity(p);.. if( p
16957 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 ->inTrans==TRANS
16958 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20 69 6e _WRITE ){. in
16959 74 20 72 63 32 3b 0a 0a 20 20 20 20 61 73 73 65 t rc2;.. asse
1695a 72 74 28 20 54 52 41 4e 53 5f 57 52 49 54 45 3d rt( TRANS_WRITE=
1695b 3d 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 =pBt->inTransact
1695c 69 6f 6e 20 29 3b 0a 20 20 20 20 72 63 32 20 3d ion );. rc2 =
1695d 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c sqlite3PagerRol
1695e 6c 62 61 63 6b 28 70 42 74 2d 3e 70 50 61 67 65 lback(pBt->pPage
1695f 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 32 21 r);. if( rc2!
16960 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
16961 20 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 rc = rc2;.
16962 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 }.. /* The
16963 72 6f 6c 6c 62 61 63 6b 20 6d 61 79 20 68 61 76 rollback may hav
16964 65 20 64 65 73 74 72 6f 79 65 64 20 74 68 65 20 e destroyed the
16965 70 50 61 67 65 31 2d 3e 61 44 61 74 61 20 76 61 pPage1->aData va
16966 6c 75 65 2e 20 20 53 6f 0a 20 20 20 20 2a 2a 20 lue. So. **
16967 63 61 6c 6c 20 62 74 72 65 65 47 65 74 50 61 67 call btreeGetPag
16968 65 28 29 20 6f 6e 20 70 61 67 65 20 31 20 61 67 e() on page 1 ag
16969 61 69 6e 20 74 6f 20 6d 61 6b 65 0a 20 20 20 20 ain to make.
1696a 2a 2a 20 73 75 72 65 20 70 50 61 67 65 31 2d 3e ** sure pPage1->
1696b 61 44 61 74 61 20 69 73 20 73 65 74 20 63 6f 72 aData is set cor
1696c 72 65 63 74 6c 79 2e 20 2a 2f 0a 20 20 20 20 69 rectly. */. i
1696d 66 28 20 62 74 72 65 65 47 65 74 50 61 67 65 28 f( btreeGetPage(
1696e 70 42 74 2c 20 31 2c 20 26 70 50 61 67 65 31 2c pBt, 1, &pPage1,
1696f 20 30 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 0)==SQLITE_OK )
16970 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 {. releaseP
16971 61 67 65 28 70 50 61 67 65 31 29 3b 0a 20 20 20 age(pPage1);.
16972 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 }. assert( c
16973 6f 75 6e 74 57 72 69 74 65 43 75 72 73 6f 72 73 ountWriteCursors
16974 28 70 42 74 29 3d 3d 30 20 29 3b 0a 20 20 20 20 (pBt)==0 );.
16975 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 pBt->inTransacti
16976 6f 6e 20 3d 20 54 52 41 4e 53 5f 52 45 41 44 3b on = TRANS_READ;
16977 0a 20 20 7d 0a 0a 20 20 62 74 72 65 65 45 6e 64 . }.. btreeEnd
16978 54 72 61 6e 73 61 63 74 69 6f 6e 28 70 29 3b 0a Transaction(p);.
16979 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
1697a 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ave(p);. return
1697b 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 74 rc;.}../*.** St
1697c 61 72 74 20 61 20 73 74 61 74 65 6d 65 6e 74 20 art a statement
1697d 73 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 subtransaction.
1697e 54 68 65 20 73 75 62 74 72 61 6e 73 61 63 74 69 The subtransacti
1697f 6f 6e 20 63 61 6e 20 63 61 6e 20 62 65 20 72 6f on can can be ro
16980 6c 6c 65 64 0a 2a 2a 20 62 61 63 6b 20 69 6e 64 lled.** back ind
16981 65 70 65 6e 64 65 6e 74 6c 79 20 6f 66 20 74 68 ependently of th
16982 65 20 6d 61 69 6e 20 74 72 61 6e 73 61 63 74 69 e main transacti
16983 6f 6e 2e 20 59 6f 75 20 6d 75 73 74 20 73 74 61 on. You must sta
16984 72 74 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e rt a transaction
16985 20 0a 2a 2a 20 62 65 66 6f 72 65 20 73 74 61 72 .** before star
16986 74 69 6e 67 20 61 20 73 75 62 74 72 61 6e 73 61 ting a subtransa
16987 63 74 69 6f 6e 2e 20 54 68 65 20 73 75 62 74 72 ction. The subtr
16988 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 65 6e 64 ansaction is end
16989 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 ed automatically
1698a 20 0a 2a 2a 20 69 66 20 74 68 65 20 6d 61 69 6e .** if the main
1698b 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 6d transaction com
1698c 6d 69 74 73 20 6f 72 20 72 6f 6c 6c 73 20 62 61 mits or rolls ba
1698d 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 74 61 74 65 6d ck..**.** Statem
1698e 65 6e 74 20 73 75 62 74 72 61 6e 73 61 63 74 69 ent subtransacti
1698f 6f 6e 73 20 61 72 65 20 75 73 65 64 20 61 72 6f ons are used aro
16990 75 6e 64 20 69 6e 64 69 76 69 64 75 61 6c 20 53 und individual S
16991 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a QL statements.**
16992 20 74 68 61 74 20 61 72 65 20 63 6f 6e 74 61 69 that are contai
16993 6e 65 64 20 77 69 74 68 69 6e 20 61 20 42 45 47 ned within a BEG
16994 49 4e 2e 2e 2e 43 4f 4d 4d 49 54 20 62 6c 6f 63 IN...COMMIT bloc
16995 6b 2e 20 20 49 66 20 61 20 63 6f 6e 73 74 72 61 k. If a constra
16996 69 6e 74 0a 2a 2a 20 65 72 72 6f 72 20 6f 63 63 int.** error occ
16997 75 72 73 20 77 69 74 68 69 6e 20 74 68 65 20 73 urs within the s
16998 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 20 65 66 tatement, the ef
16999 66 65 63 74 20 6f 66 20 74 68 61 74 20 6f 6e 65 fect of that one
1699a 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 63 61 statement.** ca
1699b 6e 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b n be rolled back
1699c 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 without having
1699d 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 to rollback the
1699e 65 6e 74 69 72 65 20 74 72 61 6e 73 61 63 74 69 entire transacti
1699f 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 74 61 74 on..**.** A stat
169a0 65 6d 65 6e 74 20 73 75 62 2d 74 72 61 6e 73 61 ement sub-transa
169a1 63 74 69 6f 6e 20 69 73 20 69 6d 70 6c 65 6d 65 ction is impleme
169a2 6e 74 65 64 20 61 73 20 61 6e 20 61 6e 6f 6e 79 nted as an anony
169a3 6d 6f 75 73 20 73 61 76 65 70 6f 69 6e 74 2e 20 mous savepoint.
169a4 54 68 65 0a 2a 2a 20 76 61 6c 75 65 20 70 61 73 The.** value pas
169a5 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e sed as the secon
169a6 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 d parameter is t
169a7 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 he total number
169a8 6f 66 20 73 61 76 65 70 6f 69 6e 74 73 2c 0a 2a of savepoints,.*
169a9 2a 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 * including the
169aa 6e 65 77 20 61 6e 6f 6e 79 6d 6f 75 73 20 73 61 new anonymous sa
169ab 76 65 70 6f 69 6e 74 2c 20 6f 70 65 6e 20 6f 6e vepoint, open on
169ac 20 74 68 65 20 42 2d 54 72 65 65 2e 20 69 2e 65 the B-Tree. i.e
169ad 2e 20 69 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 . if there.** ar
169ae 65 20 6e 6f 20 61 63 74 69 76 65 20 73 61 76 65 e no active save
169af 70 6f 69 6e 74 73 20 61 6e 64 20 6e 6f 20 6f 74 points and no ot
169b0 68 65 72 20 73 74 61 74 65 6d 65 6e 74 2d 74 72 her statement-tr
169b1 61 6e 73 61 63 74 69 6f 6e 73 20 6f 70 65 6e 2c ansactions open,
169b2 0a 2a 2a 20 69 53 74 61 74 65 6d 65 6e 74 20 69 .** iStatement i
169b3 73 20 31 2e 20 54 68 69 73 20 61 6e 6f 6e 79 6d s 1. This anonym
169b4 6f 75 73 20 73 61 76 65 70 6f 69 6e 74 20 63 61 ous savepoint ca
169b5 6e 20 62 65 20 72 65 6c 65 61 73 65 64 20 6f 72 n be released or
169b6 20 72 6f 6c 6c 65 64 20 62 61 63 6b 0a 2a 2a 20 rolled back.**
169b7 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 using the sqlite
169b8 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e 74 28 3BtreeSavepoint(
169b9 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 ) function..*/.S
169ba 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
169bb 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 t sqlite3BtreeBe
169bc 67 69 6e 53 74 6d 74 28 42 74 72 65 65 20 2a 70 ginStmt(Btree *p
169bd 2c 20 69 6e 74 20 69 53 74 61 74 65 6d 65 6e 74 , int iStatement
169be 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 ){. int rc;. B
169bf 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
169c0 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74 65 33 ->pBt;. sqlite3
169c1 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 BtreeEnter(p);.
169c2 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 assert( p->inTr
169c3 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ans==TRANS_WRITE
169c4 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 );. assert( pB
169c5 74 2d 3e 72 65 61 64 4f 6e 6c 79 3d 3d 30 20 29 t->readOnly==0 )
169c6 3b 0a 20 20 61 73 73 65 72 74 28 20 69 53 74 61 ;. assert( iSta
169c7 74 65 6d 65 6e 74 3e 30 20 29 3b 0a 20 20 61 73 tement>0 );. as
169c8 73 65 72 74 28 20 69 53 74 61 74 65 6d 65 6e 74 sert( iStatement
169c9 3e 70 2d 3e 64 62 2d 3e 6e 53 61 76 65 70 6f 69 >p->db->nSavepoi
169ca 6e 74 20 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 nt );. if( NEVE
169cb 52 28 70 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52 R(p->inTrans!=TR
169cc 41 4e 53 5f 57 52 49 54 45 20 7c 7c 20 70 42 74 ANS_WRITE || pBt
169cd 2d 3e 72 65 61 64 4f 6e 6c 79 29 20 29 7b 0a 20 ->readOnly) ){.
169ce 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 rc = SQLITE_I
169cf 4e 54 45 52 4e 41 4c 3b 0a 20 20 7d 65 6c 73 65 NTERNAL;. }else
169d0 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42 {. assert( pB
169d1 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e t->inTransaction
169d2 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b ==TRANS_WRITE );
169d3 0a 20 20 20 20 2f 2a 20 41 74 20 74 68 65 20 70 . /* At the p
169d4 61 67 65 72 20 6c 65 76 65 6c 2c 20 61 20 73 74 ager level, a st
169d5 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 atement transact
169d6 69 6f 6e 20 69 73 20 61 20 73 61 76 65 70 6f 69 ion is a savepoi
169d7 6e 74 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 61 nt with. ** a
169d8 6e 20 69 6e 64 65 78 20 67 72 65 61 74 65 72 20 n index greater
169d9 74 68 61 6e 20 61 6c 6c 20 73 61 76 65 70 6f 69 than all savepoi
169da 6e 74 73 20 63 72 65 61 74 65 64 20 65 78 70 6c nts created expl
169db 69 63 69 74 6c 79 20 75 73 69 6e 67 0a 20 20 20 icitly using.
169dc 20 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e ** SQL statemen
169dd 74 73 2e 20 49 74 20 69 73 20 69 6c 6c 65 67 61 ts. It is illega
169de 6c 20 74 6f 20 6f 70 65 6e 2c 20 72 65 6c 65 61 l to open, relea
169df 73 65 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 61 se or rollback a
169e0 6e 79 0a 20 20 20 20 2a 2a 20 73 75 63 68 20 73 ny. ** such s
169e1 61 76 65 70 6f 69 6e 74 73 20 77 68 69 6c 65 20 avepoints while
169e2 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 72 the statement tr
169e3 61 6e 73 61 63 74 69 6f 6e 20 73 61 76 65 70 6f ansaction savepo
169e4 69 6e 74 20 69 73 20 61 63 74 69 76 65 2e 0a 20 int is active..
169e5 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 */. rc = s
169e6 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 53 qlite3PagerOpenS
169e7 61 76 65 70 6f 69 6e 74 28 70 42 74 2d 3e 70 50 avepoint(pBt->pP
169e8 61 67 65 72 2c 20 69 53 74 61 74 65 6d 65 6e 74 ager, iStatement
169e9 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 );. }. sqlite3
169ea 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
169eb 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
169ec 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 *.** The second
169ed 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 argument to this
169ee 20 66 75 6e 63 74 69 6f 6e 2c 20 6f 70 2c 20 69 function, op, i
169ef 73 20 61 6c 77 61 79 73 20 53 41 56 45 50 4f 49 s always SAVEPOI
169f0 4e 54 5f 52 4f 4c 4c 42 41 43 4b 0a 2a 2a 20 6f NT_ROLLBACK.** o
169f1 72 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 r SAVEPOINT_RELE
169f2 41 53 45 2e 20 54 68 69 73 20 66 75 6e 63 74 69 ASE. This functi
169f3 6f 6e 20 65 69 74 68 65 72 20 72 65 6c 65 61 73 on either releas
169f4 65 73 20 6f 72 20 72 6f 6c 6c 73 20 62 61 63 6b es or rolls back
169f5 20 74 68 65 0a 2a 2a 20 73 61 76 65 70 6f 69 6e the.** savepoin
169f6 74 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 t identified by
169f7 70 61 72 61 6d 65 74 65 72 20 69 53 61 76 65 70 parameter iSavep
169f8 6f 69 6e 74 2c 20 64 65 70 65 6e 64 69 6e 67 20 oint, depending
169f9 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 0a 2a 2a on the value .**
169fa 20 6f 66 20 6f 70 2e 0a 2a 2a 0a 2a 2a 20 4e 6f of op..**.** No
169fb 72 6d 61 6c 6c 79 2c 20 69 53 61 76 65 70 6f 69 rmally, iSavepoi
169fc 6e 74 20 69 73 20 67 72 65 61 74 65 72 20 74 68 nt is greater th
169fd 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 7a an or equal to z
169fe 65 72 6f 2e 20 48 6f 77 65 76 65 72 2c 20 69 66 ero. However, if
169ff 20 6f 70 20 69 73 0a 2a 2a 20 53 41 56 45 50 4f op is.** SAVEPO
16a00 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20 74 68 INT_ROLLBACK, th
16a01 65 6e 20 69 53 61 76 65 70 6f 69 6e 74 20 6d 61 en iSavepoint ma
16a02 79 20 61 6c 73 6f 20 62 65 20 2d 31 2e 20 49 6e y also be -1. In
16a03 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 0a this case the .
16a04 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 ** contents of t
16a05 68 65 20 65 6e 74 69 72 65 20 74 72 61 6e 73 61 he entire transa
16a06 63 74 69 6f 6e 20 61 72 65 20 72 6f 6c 6c 65 64 ction are rolled
16a07 20 62 61 63 6b 2e 20 54 68 69 73 20 69 73 20 64 back. This is d
16a08 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66 72 6f 6d ifferent.** from
16a09 20 61 20 6e 6f 72 6d 61 6c 20 74 72 61 6e 73 61 a normal transa
16a0a 63 74 69 6f 6e 20 72 6f 6c 6c 62 61 63 6b 2c 20 ction rollback,
16a0b 61 73 20 6e 6f 20 6c 6f 63 6b 73 20 61 72 65 20 as no locks are
16a0c 72 65 6c 65 61 73 65 64 20 61 6e 64 20 74 68 65 released and the
16a0d 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 .** transaction
16a0e 72 65 6d 61 69 6e 73 20 6f 70 65 6e 2e 0a 2a 2f remains open..*/
16a0f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
16a10 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
16a11 53 61 76 65 70 6f 69 6e 74 28 42 74 72 65 65 20 Savepoint(Btree
16a12 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 *p, int op, int
16a13 69 53 61 76 65 70 6f 69 6e 74 29 7b 0a 20 20 69 iSavepoint){. i
16a14 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
16a15 4b 3b 0a 20 20 69 66 28 20 70 20 26 26 20 70 2d K;. if( p && p-
16a16 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f >inTrans==TRANS_
16a17 57 52 49 54 45 20 29 7b 0a 20 20 20 20 42 74 53 WRITE ){. BtS
16a18 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e hared *pBt = p->
16a19 70 42 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 pBt;. assert(
16a1a 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 op==SAVEPOINT_R
16a1b 45 4c 45 41 53 45 20 7c 7c 20 6f 70 3d 3d 53 41 ELEASE || op==SA
16a1c 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b VEPOINT_ROLLBACK
16a1d 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
16a1e 69 53 61 76 65 70 6f 69 6e 74 3e 3d 30 20 7c 7c iSavepoint>=0 ||
16a1f 20 28 69 53 61 76 65 70 6f 69 6e 74 3d 3d 2d 31 (iSavepoint==-1
16a20 20 26 26 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e && op==SAVEPOIN
16a21 54 5f 52 4f 4c 4c 42 41 43 4b 29 20 29 3b 0a 20 T_ROLLBACK) );.
16a22 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 sqlite3BtreeE
16a23 6e 74 65 72 28 70 29 3b 0a 20 20 20 20 72 63 20 nter(p);. rc
16a24 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 61 = sqlite3PagerSa
16a25 76 65 70 6f 69 6e 74 28 70 42 74 2d 3e 70 50 61 vepoint(pBt->pPa
16a26 67 65 72 2c 20 6f 70 2c 20 69 53 61 76 65 70 6f ger, op, iSavepo
16a27 69 6e 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63 int);. if( rc
16a28 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
16a29 20 20 20 20 20 72 63 20 3d 20 6e 65 77 44 61 74 rc = newDat
16a2a 61 62 61 73 65 28 70 42 74 29 3b 0a 20 20 20 20 abase(pBt);.
16a2b 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 }. sqlite3Btr
16a2c 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 7d 0a eeLeave(p);. }.
16a2d 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
16a2e 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e /*.** Create a n
16a2f 65 77 20 63 75 72 73 6f 72 20 66 6f 72 20 74 68 ew cursor for th
16a30 65 20 42 54 72 65 65 20 77 68 6f 73 65 20 72 6f e BTree whose ro
16a31 6f 74 20 69 73 20 6f 6e 20 74 68 65 20 70 61 67 ot is on the pag
16a32 65 0a 2a 2a 20 69 54 61 62 6c 65 2e 20 49 66 20 e.** iTable. If
16a33 61 20 72 65 61 64 2d 6f 6e 6c 79 20 63 75 72 73 a read-only curs
16a34 6f 72 20 69 73 20 72 65 71 75 65 73 74 65 64 2c or is requested,
16a35 20 69 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 it is assumed t
16a36 68 61 74 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65 hat.** the calle
16a37 72 20 61 6c 72 65 61 64 79 20 68 61 73 20 61 74 r already has at
16a38 20 6c 65 61 73 74 20 61 20 72 65 61 64 2d 6f 6e least a read-on
16a39 6c 79 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f ly transaction o
16a3a 70 65 6e 0a 2a 2a 20 6f 6e 20 74 68 65 20 64 61 pen.** on the da
16a3b 74 61 62 61 73 65 20 61 6c 72 65 61 64 79 2e 20 tabase already.
16a3c 49 66 20 61 20 77 72 69 74 65 2d 63 75 72 73 6f If a write-curso
16a3d 72 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 r is requested,
16a3e 74 68 65 6e 0a 2a 2a 20 74 68 65 20 63 61 6c 6c then.** the call
16a3f 65 72 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f er is assumed to
16a40 20 68 61 76 65 20 61 6e 20 6f 70 65 6e 20 77 72 have an open wr
16a41 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e ite transaction.
16a42 0a 2a 2a 0a 2a 2a 20 49 66 20 77 72 46 6c 61 67 .**.** If wrFlag
16a43 3d 3d 30 2c 20 74 68 65 6e 20 74 68 65 20 63 75 ==0, then the cu
16a44 72 73 6f 72 20 63 61 6e 20 6f 6e 6c 79 20 62 65 rsor can only be
16a45 20 75 73 65 64 20 66 6f 72 20 72 65 61 64 69 6e used for readin
16a46 67 2e 0a 2a 2a 20 49 66 20 77 72 46 6c 61 67 3d g..** If wrFlag=
16a47 3d 31 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 =1, then the cur
16a48 73 6f 72 20 63 61 6e 20 62 65 20 75 73 65 64 20 sor can be used
16a49 66 6f 72 20 72 65 61 64 69 6e 67 20 6f 72 20 66 for reading or f
16a4a 6f 72 0a 2a 2a 20 77 72 69 74 69 6e 67 20 69 66 or.** writing if
16a4b 20 6f 74 68 65 72 20 63 6f 6e 64 69 74 69 6f 6e other condition
16a4c 73 20 66 6f 72 20 77 72 69 74 69 6e 67 20 61 72 s for writing ar
16a4d 65 20 61 6c 73 6f 20 6d 65 74 2e 20 20 54 68 65 e also met. The
16a4e 73 65 0a 2a 2a 20 61 72 65 20 74 68 65 20 63 6f se.** are the co
16a4f 6e 64 69 74 69 6f 6e 73 20 74 68 61 74 20 6d 75 nditions that mu
16a50 73 74 20 62 65 20 6d 65 74 20 69 6e 20 6f 72 64 st be met in ord
16a51 65 72 20 66 6f 72 20 77 72 69 74 69 6e 67 20 74 er for writing t
16a52 6f 0a 2a 2a 20 62 65 20 61 6c 6c 6f 77 65 64 3a o.** be allowed:
16a53 0a 2a 2a 0a 2a 2a 20 31 3a 20 20 54 68 65 20 63 .**.** 1: The c
16a54 75 72 73 6f 72 20 6d 75 73 74 20 68 61 76 65 20 ursor must have
16a55 62 65 65 6e 20 6f 70 65 6e 65 64 20 77 69 74 68 been opened with
16a56 20 77 72 46 6c 61 67 3d 3d 31 0a 2a 2a 0a 2a 2a wrFlag==1.**.**
16a57 20 32 3a 20 20 4f 74 68 65 72 20 64 61 74 61 62 2: Other datab
16a58 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 ase connections
16a59 74 68 61 74 20 73 68 61 72 65 20 74 68 65 20 73 that share the s
16a5a 61 6d 65 20 70 61 67 65 72 20 63 61 63 68 65 0a ame pager cache.
16a5b 2a 2a 20 20 20 20 20 62 75 74 20 77 68 69 63 68 ** but which
16a5c 20 61 72 65 20 6e 6f 74 20 69 6e 20 74 68 65 20 are not in the
16a5d 52 45 41 44 5f 55 4e 43 4f 4d 4d 49 54 54 45 44 READ_UNCOMMITTED
16a5e 20 73 74 61 74 65 20 6d 61 79 20 6e 6f 74 20 68 state may not h
16a5f 61 76 65 0a 2a 2a 20 20 20 20 20 63 75 72 73 6f ave.** curso
16a60 72 73 20 6f 70 65 6e 20 77 69 74 68 20 77 72 46 rs open with wrF
16a61 6c 61 67 3d 3d 30 20 6f 6e 20 74 68 65 20 73 61 lag==0 on the sa
16a62 6d 65 20 74 61 62 6c 65 2e 20 20 4f 74 68 65 72 me table. Other
16a63 77 69 73 65 0a 2a 2a 20 20 20 20 20 74 68 65 20 wise.** the
16a64 63 68 61 6e 67 65 73 20 6d 61 64 65 20 62 79 20 changes made by
16a65 74 68 69 73 20 77 72 69 74 65 20 63 75 72 73 6f this write curso
16a66 72 20 77 6f 75 6c 64 20 62 65 20 76 69 73 69 62 r would be visib
16a67 6c 65 20 74 6f 0a 2a 2a 20 20 20 20 20 74 68 65 le to.** the
16a68 20 72 65 61 64 20 63 75 72 73 6f 72 73 20 69 6e read cursors in
16a69 20 74 68 65 20 6f 74 68 65 72 20 64 61 74 61 62 the other datab
16a6a 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a ase connection..
16a6b 2a 2a 0a 2a 2a 20 33 3a 20 20 54 68 65 20 64 61 **.** 3: The da
16a6c 74 61 62 61 73 65 20 6d 75 73 74 20 62 65 20 77 tabase must be w
16a6d 72 69 74 61 62 6c 65 20 28 6e 6f 74 20 6f 6e 20 ritable (not on
16a6e 72 65 61 64 2d 6f 6e 6c 79 20 6d 65 64 69 61 29 read-only media)
16a6f 0a 2a 2a 0a 2a 2a 20 34 3a 20 20 54 68 65 72 65 .**.** 4: There
16a70 20 6d 75 73 74 20 62 65 20 61 6e 20 61 63 74 69 must be an acti
16a71 76 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a ve transaction..
16a72 2a 2a 0a 2a 2a 20 4e 6f 20 63 68 65 63 6b 69 6e **.** No checkin
16a73 67 20 69 73 20 64 6f 6e 65 20 74 6f 20 6d 61 6b g is done to mak
16a74 65 20 73 75 72 65 20 74 68 61 74 20 70 61 67 65 e sure that page
16a75 20 69 54 61 62 6c 65 20 72 65 61 6c 6c 79 20 69 iTable really i
16a76 73 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 70 61 s the.** root pa
16a77 67 65 20 6f 66 20 61 20 62 2d 74 72 65 65 2e 20 ge of a b-tree.
16a78 20 49 66 20 69 74 20 69 73 20 6e 6f 74 2c 20 74 If it is not, t
16a79 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 61 hen the cursor a
16a7a 63 71 75 69 72 65 64 0a 2a 2a 20 77 69 6c 6c 20 cquired.** will
16a7b 6e 6f 74 20 77 6f 72 6b 20 63 6f 72 72 65 63 74 not work correct
16a7c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 ly..**.** It is
16a7d 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 assumed that the
16a7e 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 sqlite3BtreeCur
16a7f 73 6f 72 53 69 7a 65 28 29 20 62 79 74 65 73 20 sorSize() bytes
16a80 6f 66 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 70 6f of memory .** po
16a81 69 6e 74 65 64 20 74 6f 20 62 79 20 70 43 75 72 inted to by pCur
16a82 20 68 61 76 65 20 62 65 65 6e 20 7a 65 72 6f 65 have been zeroe
16a83 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e d by the caller.
16a84 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 .*/.static int b
16a85 74 72 65 65 43 75 72 73 6f 72 28 0a 20 20 42 74 treeCursor(. Bt
16a86 72 65 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 ree *p,
16a87 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16a88 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65 /* The btre
16a89 65 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c e */. int iTabl
16a8a 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
16a8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
16a8c 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 61 Root page of ta
16a8d 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 ble to open */.
16a8e 20 69 6e 74 20 77 72 46 6c 61 67 2c 20 20 20 20 int wrFlag,
16a8f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16a90 20 20 20 20 20 20 20 20 2f 2a 20 31 20 74 6f 20 /* 1 to
16a91 77 72 69 74 65 2e 20 30 20 72 65 61 64 2d 6f 6e write. 0 read-on
16a92 6c 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 4b ly */. struct K
16a93 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f eyInfo *pKeyInfo
16a94 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
16a95 2a 20 46 69 72 73 74 20 61 72 67 20 74 6f 20 63 * First arg to c
16a96 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 omparison functi
16a97 6f 6e 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 on */. BtCursor
16a98 20 2a 70 43 75 72 20 20 20 20 20 20 20 20 20 20 *pCur
16a99 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
16a9a 2a 20 53 70 61 63 65 20 66 6f 72 20 6e 65 77 20 * Space for new
16a9b 63 75 72 73 6f 72 20 2a 2f 0a 29 7b 0a 20 20 42 cursor */.){. B
16a9c 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
16a9d 2d 3e 70 42 74 3b 20 20 20 20 20 20 20 20 20 20 ->pBt;
16a9e 20 20 20 20 20 20 2f 2a 20 53 68 61 72 65 64 20 /* Shared
16a9f 62 2d 74 72 65 65 20 68 61 6e 64 6c 65 20 2a 2f b-tree handle */
16aa0 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 .. assert( sqli
16aa1 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 te3BtreeHoldsMut
16aa2 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65 72 ex(p) );. asser
16aa3 74 28 20 77 72 46 6c 61 67 3d 3d 30 20 7c 7c 20 t( wrFlag==0 ||
16aa4 77 72 46 6c 61 67 3d 3d 31 20 29 3b 0a 0a 20 20 wrFlag==1 );..
16aa5 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 /* The following
16aa6 20 61 73 73 65 72 74 20 73 74 61 74 65 6d 65 6e assert statemen
16aa7 74 73 20 76 65 72 69 66 79 20 74 68 61 74 20 69 ts verify that i
16aa8 66 20 74 68 69 73 20 69 73 20 61 20 73 68 61 72 f this is a shar
16aa9 61 62 6c 65 20 0a 20 20 2a 2a 20 62 2d 74 72 65 able . ** b-tre
16aaa 65 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 20 e database, the
16aab 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 68 6f connection is ho
16aac 6c 64 69 6e 67 20 74 68 65 20 72 65 71 75 69 72 lding the requir
16aad 65 64 20 74 61 62 6c 65 20 6c 6f 63 6b 73 2c 20 ed table locks,
16aae 0a 20 20 2a 2a 20 61 6e 64 20 74 68 61 74 20 6e . ** and that n
16aaf 6f 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 o other connecti
16ab0 6f 6e 20 68 61 73 20 61 6e 79 20 6f 70 65 6e 20 on has any open
16ab1 63 75 72 73 6f 72 20 74 68 61 74 20 63 6f 6e 66 cursor that conf
16ab2 6c 69 63 74 73 20 77 69 74 68 20 0a 20 20 2a 2a licts with . **
16ab3 20 74 68 69 73 20 6c 6f 63 6b 2e 20 20 2a 2f 0a this lock. */.
16ab4 20 20 61 73 73 65 72 74 28 20 68 61 73 53 68 61 assert( hasSha
16ab5 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 redCacheTableLoc
16ab6 6b 28 70 2c 20 69 54 61 62 6c 65 2c 20 70 4b 65 k(p, iTable, pKe
16ab7 79 49 6e 66 6f 21 3d 30 2c 20 77 72 46 6c 61 67 yInfo!=0, wrFlag
16ab8 2b 31 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 +1) );. assert(
16ab9 20 77 72 46 6c 61 67 3d 3d 30 20 7c 7c 20 21 68 wrFlag==0 || !h
16aba 61 73 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 asReadConflicts(
16abb 70 2c 20 69 54 61 62 6c 65 29 20 29 3b 0a 0a 20 p, iTable) );..
16abc 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 /* Assert that
16abd 74 68 65 20 63 61 6c 6c 65 72 20 68 61 73 20 6f the caller has o
16abe 70 65 6e 65 64 20 74 68 65 20 72 65 71 75 69 72 pened the requir
16abf 65 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 ed transaction.
16ac0 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e */. assert( p->
16ac1 69 6e 54 72 61 6e 73 3e 54 52 41 4e 53 5f 4e 4f inTrans>TRANS_NO
16ac2 4e 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 NE );. assert(
16ac3 77 72 46 6c 61 67 3d 3d 30 20 7c 7c 20 70 2d 3e wrFlag==0 || p->
16ac4 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 inTrans==TRANS_W
16ac5 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74 RITE );. assert
16ac6 28 20 70 42 74 2d 3e 70 50 61 67 65 31 20 26 26 ( pBt->pPage1 &&
16ac7 20 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 pBt->pPage1->aD
16ac8 61 74 61 20 29 3b 0a 0a 20 20 69 66 28 20 4e 45 ata );.. if( NE
16ac9 56 45 52 28 77 72 46 6c 61 67 20 26 26 20 70 42 VER(wrFlag && pB
16aca 74 2d 3e 72 65 61 64 4f 6e 6c 79 29 20 29 7b 0a t->readOnly) ){.
16acb 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
16acc 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a E_READONLY;. }.
16acd 20 20 69 66 28 20 69 54 61 62 6c 65 3d 3d 31 20 if( iTable==1
16ace 26 26 20 70 61 67 65 72 50 61 67 65 63 6f 75 6e && pagerPagecoun
16acf 74 28 70 42 74 29 3d 3d 30 20 29 7b 0a 20 20 20 t(pBt)==0 ){.
16ad0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 return SQLITE_E
16ad1 4d 50 54 59 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 MPTY;. }.. /*
16ad2 4e 6f 77 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 Now that no othe
16ad3 72 20 65 72 72 6f 72 73 20 63 61 6e 20 6f 63 63 r errors can occ
16ad4 75 72 2c 20 66 69 6e 69 73 68 20 66 69 6c 6c 69 ur, finish filli
16ad5 6e 67 20 69 6e 20 74 68 65 20 42 74 43 75 72 73 ng in the BtCurs
16ad6 6f 72 0a 20 20 2a 2a 20 76 61 72 69 61 62 6c 65 or. ** variable
16ad7 73 20 61 6e 64 20 6c 69 6e 6b 20 74 68 65 20 63 s and link the c
16ad8 75 72 73 6f 72 20 69 6e 74 6f 20 74 68 65 20 42 ursor into the B
16ad9 74 53 68 61 72 65 64 20 6c 69 73 74 2e 20 20 2a tShared list. *
16ada 2f 0a 20 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f /. pCur->pgnoRo
16adb 6f 74 20 3d 20 28 50 67 6e 6f 29 69 54 61 62 6c ot = (Pgno)iTabl
16adc 65 3b 0a 20 20 70 43 75 72 2d 3e 69 50 61 67 65 e;. pCur->iPage
16add 20 3d 20 2d 31 3b 0a 20 20 70 43 75 72 2d 3e 70 = -1;. pCur->p
16ade 4b 65 79 49 6e 66 6f 20 3d 20 70 4b 65 79 49 6e KeyInfo = pKeyIn
16adf 66 6f 3b 0a 20 20 70 43 75 72 2d 3e 70 42 74 72 fo;. pCur->pBtr
16ae0 65 65 20 3d 20 70 3b 0a 20 20 70 43 75 72 2d 3e ee = p;. pCur->
16ae1 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 70 43 75 pBt = pBt;. pCu
16ae2 72 2d 3e 77 72 46 6c 61 67 20 3d 20 28 75 38 29 r->wrFlag = (u8)
16ae3 77 72 46 6c 61 67 3b 0a 20 20 70 43 75 72 2d 3e wrFlag;. pCur->
16ae4 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 43 75 pNext = pBt->pCu
16ae5 72 73 6f 72 3b 0a 20 20 69 66 28 20 70 43 75 72 rsor;. if( pCur
16ae6 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 70 ->pNext ){. p
16ae7 43 75 72 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 Cur->pNext->pPre
16ae8 76 20 3d 20 70 43 75 72 3b 0a 20 20 7d 0a 20 20 v = pCur;. }.
16ae9 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 3d 20 70 pBt->pCursor = p
16aea 43 75 72 3b 0a 20 20 70 43 75 72 2d 3e 65 53 74 Cur;. pCur->eSt
16aeb 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 ate = CURSOR_INV
16aec 41 4c 49 44 3b 0a 20 20 70 43 75 72 2d 3e 63 61 ALID;. pCur->ca
16aed 63 68 65 64 52 6f 77 69 64 20 3d 20 30 3b 0a 20 chedRowid = 0;.
16aee 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
16aef 4b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 K;.}.SQLITE_PRIV
16af0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
16af1 74 72 65 65 43 75 72 73 6f 72 28 0a 20 20 42 74 treeCursor(. Bt
16af2 72 65 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 ree *p,
16af3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16af4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
16af5 20 62 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 btree */. int
16af6 69 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20 iTable,
16af7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16af8 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 /* Root
16af9 70 61 67 65 20 6f 66 20 74 61 62 6c 65 20 74 6f page of table to
16afa 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 77 open */. int w
16afb 72 46 6c 61 67 2c 20 20 20 20 20 20 20 20 20 20 rFlag,
16afc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16afd 20 20 20 20 20 20 20 2f 2a 20 31 20 74 6f 20 77 /* 1 to w
16afe 72 69 74 65 2e 20 30 20 72 65 61 64 2d 6f 6e 6c rite. 0 read-onl
16aff 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 4b 65 y */. struct Ke
16b00 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c yInfo *pKeyInfo,
16b01 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16b02 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 20 /* First arg
16b03 74 6f 20 78 43 6f 6d 70 61 72 65 28 29 20 2a 2f to xCompare() */
16b04 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 . BtCursor *pCu
16b05 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r
16b06 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
16b07 2a 20 57 72 69 74 65 20 6e 65 77 20 63 75 72 73 * Write new curs
16b08 6f 72 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 or here */.){.
16b09 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 int rc;. sqlite
16b0a 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
16b0b 20 20 72 63 20 3d 20 62 74 72 65 65 43 75 72 73 rc = btreeCurs
16b0c 6f 72 28 70 2c 20 69 54 61 62 6c 65 2c 20 77 72 or(p, iTable, wr
16b0d 46 6c 61 67 2c 20 70 4b 65 79 49 6e 66 6f 2c 20 Flag, pKeyInfo,
16b0e 70 43 75 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 pCur);. sqlite3
16b0f 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
16b10 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
16b11 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
16b12 73 69 7a 65 20 6f 66 20 61 20 42 74 43 75 72 73 size of a BtCurs
16b13 6f 72 20 6f 62 6a 65 63 74 20 69 6e 20 62 79 74 or object in byt
16b14 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 es..**.** This i
16b15 6e 74 65 72 66 61 63 65 73 20 69 73 20 6e 65 65 nterfaces is nee
16b16 64 65 64 20 73 6f 20 74 68 61 74 20 75 73 65 72 ded so that user
16b17 73 20 6f 66 20 63 75 72 73 6f 72 73 20 63 61 6e s of cursors can
16b18 20 70 72 65 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20 preallocate.**
16b19 73 75 66 66 69 63 69 65 6e 74 20 73 74 6f 72 61 sufficient stora
16b1a 67 65 20 74 6f 20 68 6f 6c 64 20 61 20 63 75 72 ge to hold a cur
16b1b 73 6f 72 2e 20 20 54 68 65 20 42 74 43 75 72 73 sor. The BtCurs
16b1c 6f 72 20 6f 62 6a 65 63 74 20 69 73 20 6f 70 61 or object is opa
16b1d 71 75 65 0a 2a 2a 20 74 6f 20 75 73 65 72 73 20 que.** to users
16b1e 73 6f 20 74 68 65 79 20 63 61 6e 6e 6f 74 20 64 so they cannot d
16b1f 6f 20 74 68 65 20 73 69 7a 65 6f 66 28 29 20 74 o the sizeof() t
16b20 68 65 6d 73 65 6c 76 65 73 20 2d 20 74 68 65 79 hemselves - they
16b21 20 6d 75 73 74 20 63 61 6c 6c 0a 2a 2a 20 74 68 must call.** th
16b22 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 53 is routine..*/.S
16b23 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
16b24 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 t sqlite3BtreeCu
16b25 72 73 6f 72 53 69 7a 65 28 76 6f 69 64 29 7b 0a rsorSize(void){.
16b26 20 20 72 65 74 75 72 6e 20 73 69 7a 65 6f 66 28 return sizeof(
16b27 42 74 43 75 72 73 6f 72 29 3b 0a 7d 0a 0a 2f 2a BtCursor);.}../*
16b28 0a 2a 2a 20 53 65 74 20 74 68 65 20 63 61 63 68 .** Set the cach
16b29 65 64 20 72 6f 77 69 64 20 76 61 6c 75 65 20 6f ed rowid value o
16b2a 66 20 65 76 65 72 79 20 63 75 72 73 6f 72 20 69 f every cursor i
16b2b 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 n the same datab
16b2c 61 73 65 20 66 69 6c 65 0a 2a 2a 20 61 73 20 70 ase file.** as p
16b2d 43 75 72 20 61 6e 64 20 68 61 76 69 6e 67 20 74 Cur and having t
16b2e 68 65 20 73 61 6d 65 20 72 6f 6f 74 20 70 61 67 he same root pag
16b2f 65 20 6e 75 6d 62 65 72 20 61 73 20 70 43 75 72 e number as pCur
16b30 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 73 0a . The value is.
16b31 2a 2a 20 73 65 74 20 74 6f 20 69 52 6f 77 69 64 ** set to iRowid
16b32 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 70 6f 73 ..**.** Only pos
16b33 69 74 69 76 65 20 72 6f 77 69 64 20 76 61 6c 75 itive rowid valu
16b34 65 73 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 es are considere
16b35 64 20 76 61 6c 69 64 20 66 6f 72 20 74 68 69 73 d valid for this
16b36 20 63 61 63 68 65 2e 0a 2a 2a 20 54 68 65 20 63 cache..** The c
16b37 61 63 68 65 20 69 73 20 69 6e 69 74 69 61 6c 69 ache is initiali
16b38 7a 65 64 20 74 6f 20 7a 65 72 6f 2c 20 69 6e 64 zed to zero, ind
16b39 69 63 61 74 69 6e 67 20 61 6e 20 69 6e 76 61 6c icating an inval
16b3a 69 64 20 63 61 63 68 65 2e 0a 2a 2a 20 41 20 62 id cache..** A b
16b3b 74 72 65 65 20 77 69 6c 6c 20 77 6f 72 6b 20 66 tree will work f
16b3c 69 6e 65 20 77 69 74 68 20 7a 65 72 6f 20 6f 72 ine with zero or
16b3d 20 6e 65 67 61 74 69 76 65 20 72 6f 77 69 64 73 negative rowids
16b3e 2e 20 20 57 65 20 6a 75 73 74 20 63 61 6e 6e 6f . We just canno
16b3f 74 0a 2a 2a 20 63 61 63 68 65 20 7a 65 72 6f 20 t.** cache zero
16b40 6f 72 20 6e 65 67 61 74 69 76 65 20 72 6f 77 69 or negative rowi
16b41 64 73 2c 20 77 68 69 63 68 20 6d 65 61 6e 73 20 ds, which means
16b42 74 61 62 6c 65 73 20 74 68 61 74 20 75 73 65 20 tables that use
16b43 7a 65 72 6f 20 6f 72 0a 2a 2a 20 6e 65 67 61 74 zero or.** negat
16b44 69 76 65 20 72 6f 77 69 64 73 20 6d 69 67 68 74 ive rowids might
16b45 20 72 75 6e 20 61 20 6c 69 74 74 6c 65 20 73 6c run a little sl
16b46 6f 77 65 72 2e 20 20 42 75 74 20 69 6e 20 70 72 ower. But in pr
16b47 61 63 74 69 63 65 2c 20 7a 65 72 6f 0a 2a 2a 20 actice, zero.**
16b48 6f 72 20 6e 65 67 61 74 69 76 65 20 72 6f 77 69 or negative rowi
16b49 64 73 20 61 72 65 20 76 65 72 79 20 75 6e 63 6f ds are very unco
16b4a 6d 6d 6f 6e 20 73 6f 20 74 68 69 73 20 73 68 6f mmon so this sho
16b4b 75 6c 64 20 6e 6f 74 20 62 65 20 61 20 70 72 6f uld not be a pro
16b4c 62 6c 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f blem..*/.SQLITE_
16b4d 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
16b4e 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63 68 ite3BtreeSetCach
16b4f 65 64 52 6f 77 69 64 28 42 74 43 75 72 73 6f 72 edRowid(BtCursor
16b50 20 2a 70 43 75 72 2c 20 73 71 6c 69 74 65 33 5f *pCur, sqlite3_
16b51 69 6e 74 36 34 20 69 52 6f 77 69 64 29 7b 0a 20 int64 iRowid){.
16b52 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 BtCursor *p;.
16b53 66 6f 72 28 70 3d 70 43 75 72 2d 3e 70 42 74 2d for(p=pCur->pBt-
16b54 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 >pCursor; p; p=p
16b55 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 ->pNext){. if
16b56 28 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 70 ( p->pgnoRoot==p
16b57 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 20 29 20 Cur->pgnoRoot )
16b58 70 2d 3e 63 61 63 68 65 64 52 6f 77 69 64 20 3d p->cachedRowid =
16b59 20 69 52 6f 77 69 64 3b 0a 20 20 7d 0a 20 20 61 iRowid;. }. a
16b5a 73 73 65 72 74 28 20 70 43 75 72 2d 3e 63 61 63 ssert( pCur->cac
16b5b 68 65 64 52 6f 77 69 64 3d 3d 69 52 6f 77 69 64 hedRowid==iRowid
16b5c 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 );.}../*.** Ret
16b5d 75 72 6e 20 74 68 65 20 63 61 63 68 65 64 20 72 urn the cached r
16b5e 6f 77 69 64 20 66 6f 72 20 74 68 65 20 67 69 76 owid for the giv
16b5f 65 6e 20 63 75 72 73 6f 72 2e 20 20 41 20 6e 65 en cursor. A ne
16b60 67 61 74 69 76 65 20 6f 72 20 7a 65 72 6f 0a 2a gative or zero.*
16b61 2a 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 * return value i
16b62 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 74 68 ndicates that th
16b63 65 20 72 6f 77 69 64 20 63 61 63 68 65 20 69 73 e rowid cache is
16b64 20 69 6e 76 61 6c 69 64 20 61 6e 64 20 73 68 6f invalid and sho
16b65 75 6c 64 20 62 65 0a 2a 2a 20 69 67 6e 6f 72 65 uld be.** ignore
16b66 64 2e 20 20 49 66 20 74 68 65 20 72 6f 77 69 64 d. If the rowid
16b67 20 63 61 63 68 65 20 68 61 73 20 6e 65 76 65 72 cache has never
16b68 20 62 65 66 6f 72 65 20 62 65 65 6e 20 73 65 74 before been set
16b69 2c 20 74 68 65 6e 20 61 0a 2a 2a 20 7a 65 72 6f , then a.** zero
16b6a 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f is returned..*/
16b6b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
16b6c 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 sqlite3_int64 sq
16b6d 6c 69 74 65 33 42 74 72 65 65 47 65 74 43 61 63 lite3BtreeGetCac
16b6e 68 65 64 52 6f 77 69 64 28 42 74 43 75 72 73 6f hedRowid(BtCurso
16b6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 72 65 74 75 r *pCur){. retu
16b70 72 6e 20 70 43 75 72 2d 3e 63 61 63 68 65 64 52 rn pCur->cachedR
16b71 6f 77 69 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 owid;.}../*.** C
16b72 6c 6f 73 65 20 61 20 63 75 72 73 6f 72 2e 20 20 lose a cursor.
16b73 54 68 65 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e The read lock on
16b74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
16b75 6c 65 20 69 73 20 72 65 6c 65 61 73 65 64 0a 2a le is released.*
16b76 2a 20 77 68 65 6e 20 74 68 65 20 6c 61 73 74 20 * when the last
16b77 63 75 72 73 6f 72 20 69 73 20 63 6c 6f 73 65 64 cursor is closed
16b78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
16b79 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
16b7a 74 72 65 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 treeCloseCursor(
16b7b 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b BtCursor *pCur){
16b7c 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72 65 65 . Btree *pBtree
16b7d 20 3d 20 70 43 75 72 2d 3e 70 42 74 72 65 65 3b = pCur->pBtree;
16b7e 0a 20 20 69 66 28 20 70 42 74 72 65 65 20 29 7b . if( pBtree ){
16b7f 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 . int i;.
16b80 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
16b81 70 43 75 72 2d 3e 70 42 74 3b 0a 20 20 20 20 73 pCur->pBt;. s
16b82 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
16b83 28 70 42 74 72 65 65 29 3b 0a 20 20 20 20 73 71 (pBtree);. sq
16b84 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 lite3BtreeClearC
16b85 75 72 73 6f 72 28 70 43 75 72 29 3b 0a 20 20 20 ursor(pCur);.
16b86 20 69 66 28 20 70 43 75 72 2d 3e 70 50 72 65 76 if( pCur->pPrev
16b87 20 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e ){. pCur->
16b88 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 pPrev->pNext = p
16b89 43 75 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 Cur->pNext;.
16b8a 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 42 74 }else{. pBt
16b8b 2d 3e 70 43 75 72 73 6f 72 20 3d 20 70 43 75 72 ->pCursor = pCur
16b8c 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 ->pNext;. }.
16b8d 20 20 20 69 66 28 20 70 43 75 72 2d 3e 70 4e 65 if( pCur->pNe
16b8e 78 74 20 29 7b 0a 20 20 20 20 20 20 70 43 75 72 xt ){. pCur
16b8f 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d ->pNext->pPrev =
16b90 20 70 43 75 72 2d 3e 70 50 72 65 76 3b 0a 20 20 pCur->pPrev;.
16b91 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b }. for(i=0;
16b92 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61 67 65 3b i<=pCur->iPage;
16b93 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c i++){. rel
16b94 65 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61 easePage(pCur->a
16b95 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20 7d pPage[i]);. }
16b96 0a 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 . unlockBtree
16b97 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 20 IfUnused(pBt);.
16b98 20 20 20 69 6e 76 61 6c 69 64 61 74 65 4f 76 65 invalidateOve
16b99 72 66 6c 6f 77 43 61 63 68 65 28 70 43 75 72 29 rflowCache(pCur)
16b9a 3b 0a 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 ;. /* sqlite3
16b9b 5f 66 72 65 65 28 70 43 75 72 29 3b 20 2a 2f 0a _free(pCur); */.
16b9c 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
16b9d 4c 65 61 76 65 28 70 42 74 72 65 65 29 3b 0a 20 Leave(pBtree);.
16b9e 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }. return SQLI
16b9f 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
16ba0 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 42 74 Make sure the Bt
16ba1 43 75 72 73 6f 72 2a 20 67 69 76 65 6e 20 69 6e Cursor* given in
16ba2 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 68 61 the argument ha
16ba3 73 20 61 20 76 61 6c 69 64 0a 2a 2a 20 42 74 43 s a valid.** BtC
16ba4 75 72 73 6f 72 2e 69 6e 66 6f 20 73 74 72 75 63 ursor.info struc
16ba5 74 75 72 65 2e 20 20 49 66 20 69 74 20 69 73 20 ture. If it is
16ba6 6e 6f 74 20 61 6c 72 65 61 64 79 20 76 61 6c 69 not already vali
16ba7 64 2c 20 63 61 6c 6c 0a 2a 2a 20 62 74 72 65 65 d, call.** btree
16ba8 50 61 72 73 65 43 65 6c 6c 28 29 20 74 6f 20 66 ParseCell() to f
16ba9 69 6c 6c 20 69 74 20 69 6e 2e 0a 2a 2a 0a 2a 2a ill it in..**.**
16baa 20 42 74 43 75 72 73 6f 72 2e 69 6e 66 6f 20 69 BtCursor.info i
16bab 73 20 61 20 63 61 63 68 65 20 6f 66 20 74 68 65 s a cache of the
16bac 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 information in
16bad 74 68 65 20 63 75 72 72 65 6e 74 20 63 65 6c 6c the current cell
16bae 2e 0a 2a 2a 20 55 73 69 6e 67 20 74 68 69 73 20 ..** Using this
16baf 63 61 63 68 65 20 72 65 64 75 63 65 73 20 74 68 cache reduces th
16bb0 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 6c 6c e number of call
16bb1 73 20 74 6f 20 62 74 72 65 65 50 61 72 73 65 43 s to btreeParseC
16bb2 65 6c 6c 28 29 2e 0a 2a 2a 0a 2a 2a 20 32 30 30 ell()..**.** 200
16bb3 37 2d 30 36 2d 32 35 3a 20 20 54 68 65 72 65 20 7-06-25: There
16bb4 69 73 20 61 20 62 75 67 20 69 6e 20 73 6f 6d 65 is a bug in some
16bb5 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 4d 53 56 versions of MSV
16bb6 43 20 74 68 61 74 20 63 61 75 73 65 20 74 68 65 C that cause the
16bb7 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 20 74 6f 20 .** compiler to
16bb8 63 72 61 73 68 20 77 68 65 6e 20 67 65 74 43 65 crash when getCe
16bb9 6c 6c 49 6e 66 6f 28 29 20 69 73 20 69 6d 70 6c llInfo() is impl
16bba 65 6d 65 6e 74 65 64 20 61 73 20 61 20 6d 61 63 emented as a mac
16bbb 72 6f 2e 0a 2a 2a 20 42 75 74 20 74 68 65 72 65 ro..** But there
16bbc 20 69 73 20 61 20 6d 65 61 73 75 72 65 61 62 6c is a measureabl
16bbd 65 20 73 70 65 65 64 20 61 64 76 61 6e 74 61 67 e speed advantag
16bbe 65 20 74 6f 20 75 73 69 6e 67 20 74 68 65 20 6d e to using the m
16bbf 61 63 72 6f 20 6f 6e 20 67 63 63 0a 2a 2a 20 28 acro on gcc.** (
16bc0 77 68 65 6e 20 6c 65 73 73 20 63 6f 6d 70 69 6c when less compil
16bc1 65 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 er optimizations
16bc2 20 6c 69 6b 65 20 2d 4f 73 20 6f 72 20 2d 4f 30 like -Os or -O0
16bc3 20 61 72 65 20 75 73 65 64 20 61 6e 64 20 74 68 are used and th
16bc4 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 20 69 73 e.** compiler is
16bc5 20 6e 6f 74 20 64 6f 69 6e 67 20 61 67 72 65 73 not doing agres
16bc6 73 69 76 65 20 69 6e 6c 69 6e 69 6e 67 2e 29 20 sive inlining.)
16bc7 20 53 6f 20 77 65 20 75 73 65 20 61 20 72 65 61 So we use a rea
16bc8 6c 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 66 6f l function.** fo
16bc9 72 20 4d 53 56 43 20 61 6e 64 20 61 20 6d 61 63 r MSVC and a mac
16bca 72 6f 20 66 6f 72 20 65 76 65 72 79 74 68 69 6e ro for everythin
16bcb 67 20 65 6c 73 65 2e 20 20 54 69 63 6b 65 74 20 g else. Ticket
16bcc 23 32 34 35 37 2e 0a 2a 2f 0a 23 69 66 6e 64 65 #2457..*/.#ifnde
16bcd 66 20 4e 44 45 42 55 47 0a 20 20 73 74 61 74 69 f NDEBUG. stati
16bce 63 20 76 6f 69 64 20 61 73 73 65 72 74 43 65 6c c void assertCel
16bcf 6c 49 6e 66 6f 28 42 74 43 75 72 73 6f 72 20 2a lInfo(BtCursor *
16bd0 70 43 75 72 29 7b 0a 20 20 20 20 43 65 6c 6c 49 pCur){. CellI
16bd1 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 20 69 6e nfo info;. in
16bd2 74 20 69 50 61 67 65 20 3d 20 70 43 75 72 2d 3e t iPage = pCur->
16bd3 69 50 61 67 65 3b 0a 20 20 20 20 6d 65 6d 73 65 iPage;. memse
16bd4 74 28 26 69 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 t(&info, 0, size
16bd5 6f 66 28 69 6e 66 6f 29 29 3b 0a 20 20 20 20 62 of(info));. b
16bd6 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 70 43 treeParseCell(pC
16bd7 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 67 65 ur->apPage[iPage
16bd8 5d 2c 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 ], pCur->aiIdx[i
16bd9 50 61 67 65 5d 2c 20 26 69 6e 66 6f 29 3b 0a 20 Page], &info);.
16bda 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d 63 6d assert( memcm
16bdb 70 28 26 69 6e 66 6f 2c 20 26 70 43 75 72 2d 3e p(&info, &pCur->
16bdc 69 6e 66 6f 2c 20 73 69 7a 65 6f 66 28 69 6e 66 info, sizeof(inf
16bdd 6f 29 29 3d 3d 30 20 29 3b 0a 20 20 7d 0a 23 65 o))==0 );. }.#e
16bde 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 61 73 lse. #define as
16bdf 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28 78 29 0a sertCellInfo(x).
16be0 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 5f 4d #endif.#ifdef _M
16be1 53 43 5f 56 45 52 0a 20 20 2f 2a 20 55 73 65 20 SC_VER. /* Use
16be2 61 20 72 65 61 6c 20 66 75 6e 63 74 69 6f 6e 20 a real function
16be3 69 6e 20 4d 53 56 43 20 74 6f 20 77 6f 72 6b 20 in MSVC to work
16be4 61 72 6f 75 6e 64 20 62 75 67 73 20 69 6e 20 74 around bugs in t
16be5 68 61 74 20 63 6f 6d 70 69 6c 65 72 2e 20 2a 2f hat compiler. */
16be6 0a 20 20 73 74 61 74 69 63 20 76 6f 69 64 20 67 . static void g
16be7 65 74 43 65 6c 6c 49 6e 66 6f 28 42 74 43 75 72 etCellInfo(BtCur
16be8 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 20 20 sor *pCur){.
16be9 69 66 28 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e if( pCur->info.n
16bea 53 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 Size==0 ){.
16beb 20 69 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75 int iPage = pCu
16bec 72 2d 3e 69 50 61 67 65 3b 0a 20 20 20 20 20 20 r->iPage;.
16bed 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 70 btreeParseCell(p
16bee 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 67 Cur->apPage[iPag
16bef 65 5d 2c 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 e],pCur->aiIdx[i
16bf0 50 61 67 65 5d 2c 26 70 43 75 72 2d 3e 69 6e 66 Page],&pCur->inf
16bf1 6f 29 3b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e o);. pCur->
16bf2 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b 0a 20 validNKey = 1;.
16bf3 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
16bf4 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28 70 assertCellInfo(p
16bf5 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a Cur);. }. }.
16bf6 23 65 6c 73 65 20 2f 2a 20 69 66 20 6e 6f 74 20 #else /* if not
16bf7 5f 4d 53 43 5f 56 45 52 20 2a 2f 0a 20 20 2f 2a _MSC_VER */. /*
16bf8 20 55 73 65 20 61 20 6d 61 63 72 6f 20 69 6e 20 Use a macro in
16bf9 61 6c 6c 20 6f 74 68 65 72 20 63 6f 6d 70 69 6c all other compil
16bfa 65 72 73 20 73 6f 20 74 68 61 74 20 74 68 65 20 ers so that the
16bfb 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 6c 69 function is inli
16bfc 6e 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 67 ned */.#define g
16bfd 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 etCellInfo(pCur)
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 20 20 20 20 20 20 20 20
16c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c01 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 70 43 \. if( pC
16c02 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 3d 3d ur->info.nSize==
16c03 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 0 ){
16c04 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c05 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c06 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69 6e 74 \. int
16c07 20 69 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 69 iPage = pCur->i
16c08 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 Page;
16c09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c0b 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 62 74 \. bt
16c0c 72 65 65 50 61 72 73 65 43 65 6c 6c 28 70 43 75 reeParseCell(pCu
16c0d 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 67 65 5d r->apPage[iPage]
16c0e 2c 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 50 61 ,pCur->aiIdx[iPa
16c0f 67 65 5d 2c 26 70 43 75 72 2d 3e 69 6e 66 6f 29 ge],&pCur->info)
16c10 3b 20 5c 0a 20 20 20 20 70 43 75 72 2d 3e 76 61 ; \. pCur->va
16c11 6c 69 64 4e 4b 65 79 20 3d 20 31 3b 20 20 20 20 lidNKey = 1;
16c12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c15 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20 20 20 \. }else{
16c16 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c17 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c18 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c19 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c1a 20 20 20 20 5c 0a 20 20 20 20 61 73 73 65 72 74 \. assert
16c1b 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b 20 CellInfo(pCur);
16c1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c1d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c1f 20 20 20 20 20 5c 0a 20 20 7d 0a 23 65 6e 64 69 \. }.#endi
16c20 66 20 2f 2a 20 5f 4d 53 43 5f 56 45 52 20 2a 2f f /* _MSC_VER */
16c21 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 ..#ifndef NDEBUG
16c22 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 72 6f /* The next ro
16c23 75 74 69 6e 65 20 75 73 65 64 20 6f 6e 6c 79 20 utine used only
16c24 77 69 74 68 69 6e 20 61 73 73 65 72 74 28 29 20 within assert()
16c25 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a 2f 2a statements */./*
16c26 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 .** Return true
16c27 69 66 20 74 68 65 20 67 69 76 65 6e 20 42 74 43 if the given BtC
16c28 75 72 73 6f 72 20 69 73 20 76 61 6c 69 64 2e 20 ursor is valid.
16c29 20 41 20 76 61 6c 69 64 20 63 75 72 73 6f 72 20 A valid cursor
16c2a 69 73 20 6f 6e 65 0a 2a 2a 20 74 68 61 74 20 69 is one.** that i
16c2b 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e s currently poin
16c2c 74 69 6e 67 20 74 6f 20 61 20 72 6f 77 20 69 6e ting to a row in
16c2d 20 61 20 28 6e 6f 6e 2d 65 6d 70 74 79 29 20 74 a (non-empty) t
16c2e 61 62 6c 65 2e 0a 2a 2a 20 54 68 69 73 20 69 73 able..** This is
16c2f 20 61 20 76 65 72 69 66 69 63 61 74 69 6f 6e 20 a verification
16c30 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 routine is used
16c31 6f 6e 6c 79 20 77 69 74 68 69 6e 20 61 73 73 65 only within asse
16c32 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e rt() statements.
16c33 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
16c34 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
16c35 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64 reeCursorIsValid
16c36 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 (BtCursor *pCur)
16c37 7b 0a 20 20 72 65 74 75 72 6e 20 70 43 75 72 20 {. return pCur
16c38 26 26 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d && pCur->eState=
16c39 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 3b 0a 7d =CURSOR_VALID;.}
16c3a 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 .#endif /* NDEBU
16c3b 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 G */../*.** Set
16c3c 2a 70 53 69 7a 65 20 74 6f 20 74 68 65 20 73 69 *pSize to the si
16c3d 7a 65 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 ze of the buffer
16c3e 20 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c 64 20 needed to hold
16c3f 74 68 65 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 the value of.**
16c40 74 68 65 20 6b 65 79 20 66 6f 72 20 74 68 65 20 the key for the
16c41 63 75 72 72 65 6e 74 20 65 6e 74 72 79 2e 20 20 current entry.
16c42 49 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 If the cursor is
16c43 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a not pointing.**
16c44 20 74 6f 20 61 20 76 61 6c 69 64 20 65 6e 74 72 to a valid entr
16c45 79 2c 20 2a 70 53 69 7a 65 20 69 73 20 73 65 74 y, *pSize is set
16c46 20 74 6f 20 30 2e 20 0a 2a 2a 0a 2a 2a 20 46 6f to 0. .**.** Fo
16c47 72 20 61 20 74 61 62 6c 65 20 77 69 74 68 20 74 r a table with t
16c48 68 65 20 49 4e 54 4b 45 59 20 66 6c 61 67 20 73 he INTKEY flag s
16c49 65 74 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 et, this routine
16c4a 20 72 65 74 75 72 6e 73 20 74 68 65 20 6b 65 79 returns the key
16c4b 0a 2a 2a 20 69 74 73 65 6c 66 2c 20 6e 6f 74 20 .** itself, not
16c4c 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
16c4d 74 65 73 20 69 6e 20 74 68 65 20 6b 65 79 2e 0a tes in the key..
16c4e 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 **.** The caller
16c4f 20 6d 75 73 74 20 70 6f 73 69 74 69 6f 6e 20 74 must position t
16c50 68 65 20 63 75 72 73 6f 72 20 70 72 69 6f 72 20 he cursor prior
16c51 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69 73 to invoking this
16c52 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 20 0a 2a 2a routine..** .**
16c53 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 This routine ca
16c54 6e 6e 6f 74 20 66 61 69 6c 2e 20 20 49 74 20 61 nnot fail. It a
16c55 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 53 51 lways returns SQ
16c56 4c 49 54 45 5f 4f 4b 2e 20 20 0a 2a 2f 0a 53 51 LITE_OK. .*/.SQ
16c57 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
16c58 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 sqlite3BtreeKey
16c59 53 69 7a 65 28 42 74 43 75 72 73 6f 72 20 2a 70 Size(BtCursor *p
16c5a 43 75 72 2c 20 69 36 34 20 2a 70 53 69 7a 65 29 Cur, i64 *pSize)
16c5b 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 {. assert( curs
16c5c 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 orHoldsMutex(pCu
16c5d 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 r) );. assert(
16c5e 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 pCur->eState==CU
16c5f 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 7c 7c 20 RSOR_INVALID ||
16c60 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 pCur->eState==CU
16c61 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 RSOR_VALID );.
16c62 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 if( pCur->eState
16c63 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 !=CURSOR_VALID )
16c64 7b 0a 20 20 20 20 2a 70 53 69 7a 65 20 3d 20 30 {. *pSize = 0
16c65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 67 ;. }else{. g
16c66 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 etCellInfo(pCur)
16c67 3b 0a 20 20 20 20 2a 70 53 69 7a 65 20 3d 20 70 ;. *pSize = p
16c68 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3b 0a Cur->info.nKey;.
16c69 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
16c6a 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
16c6b 20 53 65 74 20 2a 70 53 69 7a 65 20 74 6f 20 74 Set *pSize to t
16c6c 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 he number of byt
16c6d 65 73 20 6f 66 20 64 61 74 61 20 69 6e 20 74 68 es of data in th
16c6e 65 20 65 6e 74 72 79 20 74 68 65 0a 2a 2a 20 63 e entry the.** c
16c6f 75 72 73 6f 72 20 63 75 72 72 65 6e 74 6c 79 20 ursor currently
16c70 70 6f 69 6e 74 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a points to..**.**
16c71 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 The caller must
16c72 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 guarantee that
16c73 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 70 6f the cursor is po
16c74 69 6e 74 69 6e 67 20 74 6f 20 61 20 6e 6f 6e 2d inting to a non-
16c75 4e 55 4c 4c 0a 2a 2a 20 76 61 6c 69 64 20 65 6e NULL.** valid en
16c76 74 72 79 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 try. In other w
16c77 6f 72 64 73 2c 20 74 68 65 20 63 61 6c 6c 69 6e ords, the callin
16c78 67 20 70 72 6f 63 65 64 75 72 65 20 6d 75 73 74 g procedure must
16c79 20 67 75 61 72 61 6e 74 65 65 0a 2a 2a 20 74 68 guarantee.** th
16c7a 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 68 61 at the cursor ha
16c7b 73 20 43 75 72 73 6f 72 2e 65 53 74 61 74 65 3d s Cursor.eState=
16c7c 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 2e 0a 2a =CURSOR_VALID..*
16c7d 2a 0a 2a 2a 20 46 61 69 6c 75 72 65 20 69 73 20 *.** Failure is
16c7e 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2e 20 20 54 not possible. T
16c7f 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6c 77 his function alw
16c80 61 79 73 20 72 65 74 75 72 6e 73 20 53 51 4c 49 ays returns SQLI
16c81 54 45 5f 4f 4b 2e 0a 2a 2a 20 49 74 20 6d 69 67 TE_OK..** It mig
16c82 68 74 20 6a 75 73 74 20 61 73 20 77 65 6c 6c 20 ht just as well
16c83 62 65 20 61 20 70 72 6f 63 65 64 75 72 65 20 28 be a procedure (
16c84 72 65 74 75 72 6e 69 6e 67 20 76 6f 69 64 29 20 returning void)
16c85 62 75 74 20 77 65 20 63 6f 6e 74 69 6e 75 65 0a but we continue.
16c86 2a 2a 20 74 6f 20 72 65 74 75 72 6e 20 61 6e 20 ** to return an
16c87 69 6e 74 65 67 65 72 20 72 65 73 75 6c 74 20 63 integer result c
16c88 6f 64 65 20 66 6f 72 20 68 69 73 74 6f 72 69 63 ode for historic
16c89 61 6c 20 72 65 61 73 6f 6e 73 2e 0a 2a 2f 0a 53 al reasons..*/.S
16c8a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
16c8b 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 t sqlite3BtreeDa
16c8c 74 61 53 69 7a 65 28 42 74 43 75 72 73 6f 72 20 taSize(BtCursor
16c8d 2a 70 43 75 72 2c 20 75 33 32 20 2a 70 53 69 7a *pCur, u32 *pSiz
16c8e 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75 e){. assert( cu
16c8f 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
16c90 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 Cur) );. assert
16c91 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d ( pCur->eState==
16c92 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a CURSOR_VALID );.
16c93 20 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 getCellInfo(pC
16c94 75 72 29 3b 0a 20 20 2a 70 53 69 7a 65 20 3d 20 ur);. *pSize =
16c95 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 44 61 74 61 pCur->info.nData
16c96 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
16c97 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 E_OK;.}../*.** G
16c98 69 76 65 6e 20 74 68 65 20 70 61 67 65 20 6e 75 iven the page nu
16c99 6d 62 65 72 20 6f 66 20 61 6e 20 6f 76 65 72 66 mber of an overf
16c9a 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 68 65 20 low page in the
16c9b 64 61 74 61 62 61 73 65 20 28 70 61 72 61 6d 65 database (parame
16c9c 74 65 72 0a 2a 2a 20 6f 76 66 6c 29 2c 20 74 68 ter.** ovfl), th
16c9d 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 69 6e 64 is function find
16c9e 73 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 s the page numbe
16c9f 72 20 6f 66 20 74 68 65 20 6e 65 78 74 20 70 61 r of the next pa
16ca0 67 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20 6c 69 ge in the .** li
16ca1 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 6f 76 65 nked list of ove
16ca2 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 49 66 20 rflow pages. If
16ca3 70 6f 73 73 69 62 6c 65 2c 20 69 74 20 75 73 65 possible, it use
16ca4 73 20 74 68 65 20 61 75 74 6f 2d 76 61 63 75 75 s the auto-vacuu
16ca5 6d 0a 2a 2a 20 70 6f 69 6e 74 65 72 2d 6d 61 70 m.** pointer-map
16ca6 20 64 61 74 61 20 69 6e 73 74 65 61 64 20 6f 66 data instead of
16ca7 20 72 65 61 64 69 6e 67 20 74 68 65 20 63 6f 6e reading the con
16ca8 74 65 6e 74 20 6f 66 20 70 61 67 65 20 6f 76 66 tent of page ovf
16ca9 6c 20 74 6f 20 64 6f 20 73 6f 2e 20 0a 2a 2a 0a l to do so. .**.
16caa 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f ** If an error o
16cab 63 63 75 72 73 20 61 6e 20 53 51 4c 69 74 65 20 ccurs an SQLite
16cac 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 error code is re
16cad 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 turned. Otherwis
16cae 65 3a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 e:.**.** The pag
16caf 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 e number of the
16cb0 6e 65 78 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 next overflow pa
16cb1 67 65 20 69 6e 20 74 68 65 20 6c 69 6e 6b 65 64 ge in the linked
16cb2 20 6c 69 73 74 20 69 73 20 0a 2a 2a 20 77 72 69 list is .** wri
16cb3 74 74 65 6e 20 74 6f 20 2a 70 50 67 6e 6f 4e 65 tten to *pPgnoNe
16cb4 78 74 2e 20 49 66 20 70 61 67 65 20 6f 76 66 6c xt. If page ovfl
16cb5 20 69 73 20 74 68 65 20 6c 61 73 74 20 70 61 67 is the last pag
16cb6 65 20 69 6e 20 69 74 73 20 6c 69 6e 6b 65 64 20 e in its linked
16cb7 0a 2a 2a 20 6c 69 73 74 2c 20 2a 70 50 67 6e 6f .** list, *pPgno
16cb8 4e 65 78 74 20 69 73 20 73 65 74 20 74 6f 20 7a Next is set to z
16cb9 65 72 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 70 ero. .**.** If p
16cba 70 50 61 67 65 20 69 73 20 6e 6f 74 20 4e 55 4c pPage is not NUL
16cbb 4c 2c 20 61 6e 64 20 61 20 72 65 66 65 72 65 6e L, and a referen
16cbc 63 65 20 74 6f 20 74 68 65 20 4d 65 6d 50 61 67 ce to the MemPag
16cbd 65 20 6f 62 6a 65 63 74 20 63 6f 72 72 65 73 70 e object corresp
16cbe 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f 20 70 61 67 onding.** to pag
16cbf 65 20 6e 75 6d 62 65 72 20 70 4f 76 66 6c 20 77 e number pOvfl w
16cc0 61 73 20 6f 62 74 61 69 6e 65 64 2c 20 74 68 65 as obtained, the
16cc1 6e 20 2a 70 70 50 61 67 65 20 69 73 20 73 65 74 n *ppPage is set
16cc2 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 61 to point to tha
16cc3 74 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 2e 20 t.** reference.
16cc4 49 74 20 69 73 20 74 68 65 20 72 65 73 70 6f 6e It is the respon
16cc5 73 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65 20 sibility of the
16cc6 63 61 6c 6c 65 72 20 74 6f 20 63 61 6c 6c 20 72 caller to call r
16cc7 65 6c 65 61 73 65 50 61 67 65 28 29 0a 2a 2a 20 eleasePage().**
16cc8 6f 6e 20 2a 70 70 50 61 67 65 20 74 6f 20 66 72 on *ppPage to fr
16cc9 65 65 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 ee the reference
16cca 2e 20 49 6e 20 6e 6f 20 72 65 66 65 72 65 6e 63 . In no referenc
16ccb 65 20 77 61 73 20 6f 62 74 61 69 6e 65 64 20 28 e was obtained (
16ccc 62 65 63 61 75 73 65 0a 2a 2a 20 74 68 65 20 70 because.** the p
16ccd 6f 69 6e 74 65 72 2d 6d 61 70 20 77 61 73 20 75 ointer-map was u
16cce 73 65 64 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 sed to obtain th
16ccf 65 20 76 61 6c 75 65 20 66 6f 72 20 2a 70 50 67 e value for *pPg
16cd0 6e 6f 4e 65 78 74 29 2c 20 74 68 65 6e 0a 2a 2a noNext), then.**
16cd1 20 2a 70 70 50 61 67 65 20 69 73 20 73 65 74 20 *ppPage is set
16cd2 74 6f 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 to zero..*/.stat
16cd3 69 63 20 69 6e 74 20 67 65 74 4f 76 65 72 66 6c ic int getOverfl
16cd4 6f 77 50 61 67 65 28 0a 20 20 42 74 53 68 61 72 owPage(. BtShar
16cd5 65 64 20 2a 70 42 74 2c 20 20 20 20 20 20 20 20 ed *pBt,
16cd6 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 /* The da
16cd7 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 tabase file */.
16cd8 20 50 67 6e 6f 20 6f 76 66 6c 2c 20 20 20 20 20 Pgno ovfl,
16cd9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
16cda 20 43 75 72 72 65 6e 74 20 6f 76 65 72 66 6c 6f Current overflo
16cdb 77 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f w page number */
16cdc 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70 50 . MemPage **ppP
16cdd 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 age,
16cde 2f 2a 20 4f 55 54 3a 20 4d 65 6d 50 61 67 65 20 /* OUT: MemPage
16cdf 68 61 6e 64 6c 65 20 28 6d 61 79 20 62 65 20 4e handle (may be N
16ce0 55 4c 4c 29 20 2a 2f 0a 20 20 50 67 6e 6f 20 2a ULL) */. Pgno *
16ce1 70 50 67 6e 6f 4e 65 78 74 20 20 20 20 20 20 20 pPgnoNext
16ce2 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e /* OUT: N
16ce3 65 78 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 ext overflow pag
16ce4 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 29 7b 0a 20 e number */.){.
16ce5 20 50 67 6e 6f 20 6e 65 78 74 20 3d 20 30 3b 0a Pgno next = 0;.
16ce6 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
16ce7 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d = 0;. int rc =
16ce8 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 SQLITE_OK;.. a
16ce9 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
16cea 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d utex_held(pBt->m
16ceb 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
16cec 74 28 70 50 67 6e 6f 4e 65 78 74 29 3b 0a 0a 23 t(pPgnoNext);..#
16ced 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
16cee 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
16cef 2f 2a 20 54 72 79 20 74 6f 20 66 69 6e 64 20 74 /* Try to find t
16cf0 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20 he next page in
16cf1 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 6c 69 73 the overflow lis
16cf2 74 20 75 73 69 6e 67 20 74 68 65 0a 20 20 2a 2a t using the. **
16cf3 20 61 75 74 6f 76 61 63 75 75 6d 20 70 6f 69 6e autovacuum poin
16cf4 74 65 72 2d 6d 61 70 20 70 61 67 65 73 2e 20 47 ter-map pages. G
16cf5 75 65 73 73 20 74 68 61 74 20 74 68 65 20 6e 65 uess that the ne
16cf6 78 74 20 70 61 67 65 20 69 6e 20 0a 20 20 2a 2a xt page in . **
16cf7 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 6c 69 the overflow li
16cf8 73 74 20 69 73 20 70 61 67 65 20 6e 75 6d 62 65 st is page numbe
16cf9 72 20 28 6f 76 66 6c 2b 31 29 2e 20 49 66 20 74 r (ovfl+1). If t
16cfa 68 61 74 20 67 75 65 73 73 20 74 75 72 6e 73 20 hat guess turns
16cfb 0a 20 20 2a 2a 20 6f 75 74 20 74 6f 20 62 65 20 . ** out to be
16cfc 77 72 6f 6e 67 2c 20 66 61 6c 6c 20 62 61 63 6b wrong, fall back
16cfd 20 74 6f 20 6c 6f 61 64 69 6e 67 20 74 68 65 20 to loading the
16cfe 64 61 74 61 20 6f 66 20 70 61 67 65 20 0a 20 20 data of page .
16cff 2a 2a 20 6e 75 6d 62 65 72 20 6f 76 66 6c 20 74 ** number ovfl t
16d00 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 o determine the
16d01 6e 65 78 74 20 70 61 67 65 20 6e 75 6d 62 65 72 next page number
16d02 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 42 74 .. */. if( pBt
16d03 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a ->autoVacuum ){.
16d04 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 Pgno pgno;.
16d05 20 20 20 50 67 6e 6f 20 69 47 75 65 73 73 20 3d Pgno iGuess =
16d06 20 6f 76 66 6c 2b 31 3b 0a 20 20 20 20 75 38 20 ovfl+1;. u8
16d07 65 54 79 70 65 3b 0a 0a 20 20 20 20 77 68 69 6c eType;.. whil
16d08 65 28 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 e( PTRMAP_ISPAGE
16d09 28 70 42 74 2c 20 69 47 75 65 73 73 29 20 7c 7c (pBt, iGuess) ||
16d0a 20 69 47 75 65 73 73 3d 3d 50 45 4e 44 49 4e 47 iGuess==PENDING
16d0b 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 _BYTE_PAGE(pBt)
16d0c 29 7b 0a 20 20 20 20 20 20 69 47 75 65 73 73 2b ){. iGuess+
16d0d 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 +;. }.. if
16d0e 28 20 69 47 75 65 73 73 3c 3d 70 61 67 65 72 50 ( iGuess<=pagerP
16d0f 61 67 65 63 6f 75 6e 74 28 70 42 74 29 20 29 7b agecount(pBt) ){
16d10 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d . rc = ptrm
16d11 61 70 47 65 74 28 70 42 74 2c 20 69 47 75 65 73 apGet(pBt, iGues
16d12 73 2c 20 26 65 54 79 70 65 2c 20 26 70 67 6e 6f s, &eType, &pgno
16d13 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d );. if( rc=
16d14 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 65 54 =SQLITE_OK && eT
16d15 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 ype==PTRMAP_OVER
16d16 46 4c 4f 57 32 20 26 26 20 70 67 6e 6f 3d 3d 6f FLOW2 && pgno==o
16d17 76 66 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 6e vfl ){. n
16d18 65 78 74 20 3d 20 69 47 75 65 73 73 3b 0a 20 20 ext = iGuess;.
16d19 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
16d1a 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20 20 7d 0a E_DONE;. }.
16d1b 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 }. }.#endif
16d1c 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 65 78 74 .. assert( next
16d1d 3d 3d 30 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 ==0 || rc==SQLIT
16d1e 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 69 66 28 20 E_DONE );. if(
16d1f 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
16d20 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 . rc = btreeG
16d21 65 74 50 61 67 65 28 70 42 74 2c 20 6f 76 66 6c etPage(pBt, ovfl
16d22 2c 20 26 70 50 61 67 65 2c 20 30 29 3b 0a 20 20 , &pPage, 0);.
16d23 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 assert( rc==SQ
16d24 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 LITE_OK || pPage
16d25 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 72 ==0 );. if( r
16d26 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
16d27 20 20 20 20 20 20 6e 65 78 74 20 3d 20 67 65 74 next = get
16d28 34 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44 61 4byte(pPage->aDa
16d29 74 61 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a ta);. }. }..
16d2a 20 20 2a 70 50 67 6e 6f 4e 65 78 74 20 3d 20 6e *pPgnoNext = n
16d2b 65 78 74 3b 0a 20 20 69 66 28 20 70 70 50 61 67 ext;. if( ppPag
16d2c 65 20 29 7b 0a 20 20 20 20 2a 70 70 50 61 67 65 e ){. *ppPage
16d2d 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d 65 6c 73 = pPage;. }els
16d2e 65 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 e{. releasePa
16d2f 67 65 28 70 50 61 67 65 29 3b 0a 20 20 7d 0a 20 ge(pPage);. }.
16d30 20 72 65 74 75 72 6e 20 28 72 63 3d 3d 53 51 4c return (rc==SQL
16d31 49 54 45 5f 44 4f 4e 45 20 3f 20 53 51 4c 49 54 ITE_DONE ? SQLIT
16d32 45 5f 4f 4b 20 3a 20 72 63 29 3b 0a 7d 0a 0a 2f E_OK : rc);.}../
16d33 2a 0a 2a 2a 20 43 6f 70 79 20 64 61 74 61 20 66 *.** Copy data f
16d34 72 6f 6d 20 61 20 62 75 66 66 65 72 20 74 6f 20 rom a buffer to
16d35 61 20 70 61 67 65 2c 20 6f 72 20 66 72 6f 6d 20 a page, or from
16d36 61 20 70 61 67 65 20 74 6f 20 61 20 62 75 66 66 a page to a buff
16d37 65 72 2e 0a 2a 2a 0a 2a 2a 20 70 50 61 79 6c 6f er..**.** pPaylo
16d38 61 64 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 ad is a pointer
16d39 74 6f 20 64 61 74 61 20 73 74 6f 72 65 64 20 6f to data stored o
16d3a 6e 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 n database page
16d3b 70 44 62 50 61 67 65 2e 0a 2a 2a 20 49 66 20 61 pDbPage..** If a
16d3c 72 67 75 6d 65 6e 74 20 65 4f 70 20 69 73 20 66 rgument eOp is f
16d3d 61 6c 73 65 2c 20 74 68 65 6e 20 6e 42 79 74 65 alse, then nByte
16d3e 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 61 bytes of data a
16d3f 72 65 20 63 6f 70 69 65 64 0a 2a 2a 20 66 72 6f re copied.** fro
16d40 6d 20 70 50 61 79 6c 6f 61 64 20 74 6f 20 74 68 m pPayload to th
16d41 65 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 e buffer pointed
16d42 20 61 74 20 62 79 20 70 42 75 66 2e 20 49 66 20 at by pBuf. If
16d43 65 4f 70 20 69 73 20 74 72 75 65 2c 0a 2a 2a 20 eOp is true,.**
16d44 74 68 65 6e 20 73 71 6c 69 74 65 33 50 61 67 65 then sqlite3Page
16d45 72 57 72 69 74 65 28 29 20 69 73 20 63 61 6c 6c rWrite() is call
16d46 65 64 20 6f 6e 20 70 44 62 50 61 67 65 20 61 6e ed on pDbPage an
16d47 64 20 6e 42 79 74 65 20 62 79 74 65 73 0a 2a 2a d nByte bytes.**
16d48 20 6f 66 20 64 61 74 61 20 61 72 65 20 63 6f 70 of data are cop
16d49 69 65 64 20 66 72 6f 6d 20 74 68 65 20 62 75 66 ied from the buf
16d4a 66 65 72 20 70 42 75 66 20 74 6f 20 70 50 61 79 fer pBuf to pPay
16d4b 6c 6f 61 64 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 load..**.** SQLI
16d4c 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
16d4d 64 20 6f 6e 20 73 75 63 63 65 73 73 2c 20 6f 74 d on success, ot
16d4e 68 65 72 77 69 73 65 20 61 6e 20 65 72 72 6f 72 herwise an error
16d4f 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 code..*/.static
16d50 20 69 6e 74 20 63 6f 70 79 50 61 79 6c 6f 61 64 int copyPayload
16d51 28 0a 20 20 76 6f 69 64 20 2a 70 50 61 79 6c 6f (. void *pPaylo
16d52 61 64 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a ad, /*
16d53 20 50 6f 69 6e 74 65 72 20 74 6f 20 70 61 67 65 Pointer to page
16d54 20 64 61 74 61 20 2a 2f 0a 20 20 76 6f 69 64 20 data */. void
16d55 2a 70 42 75 66 2c 20 20 20 20 20 20 20 20 20 20 *pBuf,
16d56 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
16d57 74 6f 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 to buffer */. i
16d58 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 20 20 20 nt nByte,
16d59 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
16d5a 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 63 er of bytes to c
16d5b 6f 70 79 20 2a 2f 0a 20 20 69 6e 74 20 65 4f 70 opy */. int eOp
16d5c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
16d5d 20 20 20 2f 2a 20 30 20 2d 3e 20 63 6f 70 79 20 /* 0 -> copy
16d5e 66 72 6f 6d 20 70 61 67 65 2c 20 31 20 2d 3e 20 from page, 1 ->
16d5f 63 6f 70 79 20 74 6f 20 70 61 67 65 20 2a 2f 0a copy to page */.
16d60 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 DbPage *pDbPag
16d61 65 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 e /* P
16d62 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70 age containing p
16d63 50 61 79 6c 6f 61 64 20 2a 2f 0a 29 7b 0a 20 20 Payload */.){.
16d64 69 66 28 20 65 4f 70 20 29 7b 0a 20 20 20 20 2f if( eOp ){. /
16d65 2a 20 43 6f 70 79 20 64 61 74 61 20 66 72 6f 6d * Copy data from
16d66 20 62 75 66 66 65 72 20 74 6f 20 70 61 67 65 20 buffer to page
16d67 28 61 20 77 72 69 74 65 20 6f 70 65 72 61 74 69 (a write operati
16d68 6f 6e 29 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 on) */. int r
16d69 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
16d6a 57 72 69 74 65 28 70 44 62 50 61 67 65 29 3b 0a Write(pDbPage);.
16d6b 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
16d6c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 TE_OK ){. r
16d6d 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
16d6e 20 20 20 20 6d 65 6d 63 70 79 28 70 50 61 79 6c memcpy(pPayl
16d6f 6f 61 64 2c 20 70 42 75 66 2c 20 6e 42 79 74 65 oad, pBuf, nByte
16d70 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
16d71 2f 2a 20 43 6f 70 79 20 64 61 74 61 20 66 72 6f /* Copy data fro
16d72 6d 20 70 61 67 65 20 74 6f 20 62 75 66 66 65 72 m page to buffer
16d73 20 28 61 20 72 65 61 64 20 6f 70 65 72 61 74 69 (a read operati
16d74 6f 6e 29 20 2a 2f 0a 20 20 20 20 6d 65 6d 63 70 on) */. memcp
16d75 79 28 70 42 75 66 2c 20 70 50 61 79 6c 6f 61 64 y(pBuf, pPayload
16d76 2c 20 6e 42 79 74 65 29 3b 0a 20 20 7d 0a 20 20 , nByte);. }.
16d77 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
16d78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
16d79 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 function is used
16d7a 20 74 6f 20 72 65 61 64 20 6f 72 20 6f 76 65 72 to read or over
16d7b 77 72 69 74 65 20 70 61 79 6c 6f 61 64 20 69 6e write payload in
16d7c 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 66 6f 72 formation.** for
16d7d 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 the entry that
16d7e 74 68 65 20 70 43 75 72 20 63 75 72 73 6f 72 20 the pCur cursor
16d7f 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 is pointing to.
16d80 49 66 20 74 68 65 20 65 4f 70 0a 2a 2a 20 70 61 If the eOp.** pa
16d81 72 61 6d 65 74 65 72 20 69 73 20 30 2c 20 74 68 rameter is 0, th
16d82 69 73 20 69 73 20 61 20 72 65 61 64 20 6f 70 65 is is a read ope
16d83 72 61 74 69 6f 6e 20 28 64 61 74 61 20 63 6f 70 ration (data cop
16d84 69 65 64 20 69 6e 74 6f 0a 2a 2a 20 62 75 66 66 ied into.** buff
16d85 65 72 20 70 42 75 66 29 2e 20 49 66 20 69 74 20 er pBuf). If it
16d86 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 61 20 77 is non-zero, a w
16d87 72 69 74 65 20 28 64 61 74 61 20 63 6f 70 69 65 rite (data copie
16d88 64 20 66 72 6f 6d 0a 2a 2a 20 62 75 66 66 65 72 d from.** buffer
16d89 20 70 42 75 66 29 2e 0a 2a 2a 0a 2a 2a 20 41 20 pBuf)..**.** A
16d8a 74 6f 74 61 6c 20 6f 66 20 22 61 6d 74 22 20 62 total of "amt" b
16d8b 79 74 65 73 20 61 72 65 20 72 65 61 64 20 6f 72 ytes are read or
16d8c 20 77 72 69 74 74 65 6e 20 62 65 67 69 6e 6e 69 written beginni
16d8d 6e 67 20 61 74 20 22 6f 66 66 73 65 74 22 2e 0a ng at "offset"..
16d8e 2a 2a 20 44 61 74 61 20 69 73 20 72 65 61 64 20 ** Data is read
16d8f 74 6f 20 6f 72 20 66 72 6f 6d 20 74 68 65 20 62 to or from the b
16d90 75 66 66 65 72 20 70 42 75 66 2e 0a 2a 2a 0a 2a uffer pBuf..**.*
16d91 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 62 65 * The content be
16d92 69 6e 67 20 72 65 61 64 20 6f 72 20 77 72 69 74 ing read or writ
16d93 74 65 6e 20 6d 69 67 68 74 20 61 70 70 65 61 72 ten might appear
16d94 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 70 61 67 on the main pag
16d95 65 0a 2a 2a 20 6f 72 20 62 65 20 73 63 61 74 74 e.** or be scatt
16d96 65 72 65 64 20 6f 75 74 20 6f 6e 20 6d 75 6c 74 ered out on mult
16d97 69 70 6c 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 iple overflow pa
16d98 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ges..**.** If th
16d99 65 20 42 74 43 75 72 73 6f 72 2e 69 73 49 6e 63 e BtCursor.isInc
16d9a 72 62 6c 6f 62 48 61 6e 64 6c 65 20 66 6c 61 67 rblobHandle flag
16d9b 20 69 73 20 73 65 74 2c 20 61 6e 64 20 74 68 65 is set, and the
16d9c 20 63 75 72 72 65 6e 74 0a 2a 2a 20 63 75 72 73 current.** curs
16d9d 6f 72 20 65 6e 74 72 79 20 75 73 65 73 20 6f 6e or entry uses on
16d9e 65 20 6f 72 20 6d 6f 72 65 20 6f 76 65 72 66 6c e or more overfl
16d9f 6f 77 20 70 61 67 65 73 2c 20 74 68 69 73 20 66 ow pages, this f
16da0 75 6e 63 74 69 6f 6e 0a 2a 2a 20 61 6c 6c 6f 63 unction.** alloc
16da1 61 74 65 73 20 73 70 61 63 65 20 66 6f 72 20 61 ates space for a
16da2 6e 64 20 6c 61 7a 69 6c 79 20 70 6f 70 6c 75 61 nd lazily poplua
16da3 74 65 73 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 tes the overflow
16da4 20 70 61 67 65 2d 6c 69 73 74 20 0a 2a 2a 20 63 page-list .** c
16da5 61 63 68 65 20 61 72 72 61 79 20 28 42 74 43 75 ache array (BtCu
16da6 72 73 6f 72 2e 61 4f 76 65 72 66 6c 6f 77 29 2e rsor.aOverflow).
16da7 20 53 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c Subsequent call
16da8 73 20 75 73 65 20 74 68 69 73 0a 2a 2a 20 63 61 s use this.** ca
16da9 63 68 65 20 74 6f 20 6d 61 6b 65 20 73 65 65 6b che to make seek
16daa 69 6e 67 20 74 6f 20 74 68 65 20 73 75 70 70 6c ing to the suppl
16dab 69 65 64 20 6f 66 66 73 65 74 20 6d 6f 72 65 20 ied offset more
16dac 65 66 66 69 63 69 65 6e 74 2e 0a 2a 2a 0a 2a 2a efficient..**.**
16dad 20 4f 6e 63 65 20 61 6e 20 6f 76 65 72 66 6c 6f Once an overflo
16dae 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 w page-list cach
16daf 65 20 68 61 73 20 62 65 65 6e 20 61 6c 6c 6f 63 e has been alloc
16db0 61 74 65 64 2c 20 69 74 20 6d 61 79 20 62 65 0a ated, it may be.
16db1 2a 2a 20 69 6e 76 61 6c 69 64 61 74 65 64 20 69 ** invalidated i
16db2 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 63 75 72 f some other cur
16db3 73 6f 72 20 77 72 69 74 65 73 20 74 6f 20 74 68 sor writes to th
16db4 65 20 73 61 6d 65 20 74 61 62 6c 65 2c 20 6f 72 e same table, or
16db5 20 69 66 0a 2a 2a 20 74 68 65 20 63 75 72 73 6f if.** the curso
16db6 72 20 69 73 20 6d 6f 76 65 64 20 74 6f 20 61 20 r is moved to a
16db7 64 69 66 66 65 72 65 6e 74 20 72 6f 77 2e 20 41 different row. A
16db8 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 69 6e 20 dditionally, in
16db9 61 75 74 6f 2d 76 61 63 75 75 6d 0a 2a 2a 20 6d auto-vacuum.** m
16dba 6f 64 65 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 ode, the followi
16dbb 6e 67 20 65 76 65 6e 74 73 20 6d 61 79 20 69 6e ng events may in
16dbc 76 61 6c 69 64 61 74 65 20 61 6e 20 6f 76 65 72 validate an over
16dbd 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 flow page-list c
16dbe 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 ache..**.** *
16dbf 41 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 An incremental v
16dc0 61 63 75 75 6d 2c 0a 2a 2a 20 20 20 2a 20 41 20 acuum,.** * A
16dc1 63 6f 6d 6d 69 74 20 69 6e 20 61 75 74 6f 5f 76 commit in auto_v
16dc2 61 63 75 75 6d 3d 22 66 75 6c 6c 22 20 6d 6f 64 acuum="full" mod
16dc3 65 2c 0a 2a 2a 20 20 20 2a 20 43 72 65 61 74 69 e,.** * Creati
16dc4 6e 67 20 61 20 74 61 62 6c 65 20 28 6d 61 79 20 ng a table (may
16dc5 72 65 71 75 69 72 65 20 6d 6f 76 69 6e 67 20 61 require moving a
16dc6 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 29 n overflow page)
16dc7 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
16dc8 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28 0a 20 accessPayload(.
16dc9 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c BtCursor *pCur,
16dca 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 /* Cursor
16dcb 70 6f 69 6e 74 69 6e 67 20 74 6f 20 65 6e 74 72 pointing to entr
16dcc 79 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a y to read from *
16dcd 2f 0a 20 20 75 33 32 20 6f 66 66 73 65 74 2c 20 /. u32 offset,
16dce 20 20 20 20 20 20 20 20 20 2f 2a 20 42 65 67 69 /* Begi
16dcf 6e 20 72 65 61 64 69 6e 67 20 74 68 69 73 20 66 n reading this f
16dd0 61 72 20 69 6e 74 6f 20 70 61 79 6c 6f 61 64 20 ar into payload
16dd1 2a 2f 0a 20 20 75 33 32 20 61 6d 74 2c 20 20 20 */. u32 amt,
16dd2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 61 /* Rea
16dd3 64 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74 65 d this many byte
16dd4 73 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 s */. unsigned
16dd5 63 68 61 72 20 2a 70 42 75 66 2c 20 2f 2a 20 57 char *pBuf, /* W
16dd6 72 69 74 65 20 74 68 65 20 62 79 74 65 73 20 69 rite the bytes i
16dd7 6e 74 6f 20 74 68 69 73 20 62 75 66 66 65 72 20 nto this buffer
16dd8 2a 2f 20 0a 20 20 69 6e 74 20 65 4f 70 20 20 20 */ . int eOp
16dd9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 65 /* ze
16dda 72 6f 20 74 6f 20 72 65 61 64 2e 20 6e 6f 6e 2d ro to read. non-
16ddb 7a 65 72 6f 20 74 6f 20 77 72 69 74 65 2e 20 2a zero to write. *
16ddc 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 /.){. unsigned
16ddd 63 68 61 72 20 2a 61 50 61 79 6c 6f 61 64 3b 0a char *aPayload;.
16dde 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
16ddf 45 5f 4f 4b 3b 0a 20 20 75 33 32 20 6e 4b 65 79 E_OK;. u32 nKey
16de0 3b 0a 20 20 69 6e 74 20 69 49 64 78 20 3d 20 30 ;. int iIdx = 0
16de1 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 ;. MemPage *pPa
16de2 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 ge = pCur->apPag
16de3 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 20 e[pCur->iPage];
16de4 2f 2a 20 42 74 72 65 65 20 70 61 67 65 20 6f 66 /* Btree page of
16de5 20 63 75 72 72 65 6e 74 20 65 6e 74 72 79 20 2a current entry *
16de6 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 /. BtShared *pB
16de7 74 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b 20 20 t = pCur->pBt;
16de8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16de9 2f 2a 20 42 74 72 65 65 20 74 68 69 73 20 63 75 /* Btree this cu
16dea 72 73 6f 72 20 62 65 6c 6f 6e 67 73 20 74 6f 20 rsor belongs to
16deb 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 */.. assert( pP
16dec 61 67 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 age );. assert(
16ded 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
16dee 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 URSOR_VALID );.
16def 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 assert( pCur->a
16df0 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
16df1 5d 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 ]<pPage->nCell )
16df2 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 ;. assert( curs
16df3 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 orHoldsMutex(pCu
16df4 72 29 20 29 3b 0a 0a 20 20 67 65 74 43 65 6c 6c r) );.. getCell
16df5 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 61 50 Info(pCur);. aP
16df6 61 79 6c 6f 61 64 20 3d 20 70 43 75 72 2d 3e 69 ayload = pCur->i
16df7 6e 66 6f 2e 70 43 65 6c 6c 20 2b 20 70 43 75 72 nfo.pCell + pCur
16df8 2d 3e 69 6e 66 6f 2e 6e 48 65 61 64 65 72 3b 0a ->info.nHeader;.
16df9 20 20 6e 4b 65 79 20 3d 20 28 70 50 61 67 65 2d nKey = (pPage-
16dfa 3e 69 6e 74 4b 65 79 20 3f 20 30 20 3a 20 28 69 >intKey ? 0 : (i
16dfb 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b nt)pCur->info.nK
16dfc 65 79 29 3b 0a 0a 20 20 69 66 28 20 4e 45 56 45 ey);.. if( NEVE
16dfd 52 28 6f 66 66 73 65 74 2b 61 6d 74 20 3e 20 6e R(offset+amt > n
16dfe 4b 65 79 2b 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e Key+pCur->info.n
16dff 44 61 74 61 29 20 0a 20 20 20 7c 7c 20 26 61 50 Data) . || &aP
16e00 61 79 6c 6f 61 64 5b 70 43 75 72 2d 3e 69 6e 66 ayload[pCur->inf
16e01 6f 2e 6e 4c 6f 63 61 6c 5d 20 3e 20 26 70 50 61 o.nLocal] > &pPa
16e02 67 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d 3e 75 ge->aData[pBt->u
16e03 73 61 62 6c 65 53 69 7a 65 5d 0a 20 20 29 7b 0a sableSize]. ){.
16e04 20 20 20 20 2f 2a 20 54 72 79 69 6e 67 20 74 6f /* Trying to
16e05 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 70 read or write p
16e06 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 ast the end of t
16e07 68 65 20 64 61 74 61 20 69 73 20 61 6e 20 65 72 he data is an er
16e08 72 6f 72 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 ror */. retur
16e09 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
16e0a 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20 2f 2a _BKPT;. }.. /*
16e0b 20 43 68 65 63 6b 20 69 66 20 64 61 74 61 20 6d Check if data m
16e0c 75 73 74 20 62 65 20 72 65 61 64 2f 77 72 69 74 ust be read/writ
16e0d 74 65 6e 20 74 6f 2f 66 72 6f 6d 20 74 68 65 20 ten to/from the
16e0e 62 74 72 65 65 20 70 61 67 65 20 69 74 73 65 6c btree page itsel
16e0f 66 2e 20 2a 2f 0a 20 20 69 66 28 20 6f 66 66 73 f. */. if( offs
16e10 65 74 3c 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c et<pCur->info.nL
16e11 6f 63 61 6c 20 29 7b 0a 20 20 20 20 69 6e 74 20 ocal ){. int
16e12 61 20 3d 20 61 6d 74 3b 0a 20 20 20 20 69 66 28 a = amt;. if(
16e13 20 61 2b 6f 66 66 73 65 74 3e 70 43 75 72 2d 3e a+offset>pCur->
16e14 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29 7b 0a 20 info.nLocal ){.
16e15 20 20 20 20 20 61 20 3d 20 70 43 75 72 2d 3e 69 a = pCur->i
16e16 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2d 20 6f 66 66 nfo.nLocal - off
16e17 73 65 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 set;. }. r
16e18 63 20 3d 20 63 6f 70 79 50 61 79 6c 6f 61 64 28 c = copyPayload(
16e19 26 61 50 61 79 6c 6f 61 64 5b 6f 66 66 73 65 74 &aPayload[offset
16e1a 5d 2c 20 70 42 75 66 2c 20 61 2c 20 65 4f 70 2c ], pBuf, a, eOp,
16e1b 20 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 pPage->pDbPage)
16e1c 3b 0a 20 20 20 20 6f 66 66 73 65 74 20 3d 20 30 ;. offset = 0
16e1d 3b 0a 20 20 20 20 70 42 75 66 20 2b 3d 20 61 3b ;. pBuf += a;
16e1e 0a 20 20 20 20 61 6d 74 20 2d 3d 20 61 3b 0a 20 . amt -= a;.
16e1f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 66 66 73 }else{. offs
16e20 65 74 20 2d 3d 20 70 43 75 72 2d 3e 69 6e 66 6f et -= pCur->info
16e21 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 0a 0a 20 20 .nLocal;. }..
16e22 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
16e23 4b 20 26 26 20 61 6d 74 3e 30 20 29 7b 0a 20 20 K && amt>0 ){.
16e24 20 20 63 6f 6e 73 74 20 75 33 32 20 6f 76 66 6c const u32 ovfl
16e25 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 Size = pBt->usab
16e26 6c 65 53 69 7a 65 20 2d 20 34 3b 20 20 2f 2a 20 leSize - 4; /*
16e27 42 79 74 65 73 20 63 6f 6e 74 65 6e 74 20 70 65 Bytes content pe
16e28 72 20 6f 76 66 6c 20 70 61 67 65 20 2a 2f 0a 20 r ovfl page */.
16e29 20 20 20 50 67 6e 6f 20 6e 65 78 74 50 61 67 65 Pgno nextPage
16e2a 3b 0a 0a 20 20 20 20 6e 65 78 74 50 61 67 65 20 ;.. nextPage
16e2b 3d 20 67 65 74 34 62 79 74 65 28 26 61 50 61 79 = get4byte(&aPay
16e2c 6c 6f 61 64 5b 70 43 75 72 2d 3e 69 6e 66 6f 2e load[pCur->info.
16e2d 6e 4c 6f 63 61 6c 5d 29 3b 0a 0a 23 69 66 6e 64 nLocal]);..#ifnd
16e2e 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 ef SQLITE_OMIT_I
16e2f 4e 43 52 42 4c 4f 42 0a 20 20 20 20 2f 2a 20 49 NCRBLOB. /* I
16e30 66 20 74 68 65 20 69 73 49 6e 63 72 62 6c 6f 62 f the isIncrblob
16e31 48 61 6e 64 6c 65 20 66 6c 61 67 20 69 73 20 73 Handle flag is s
16e32 65 74 20 61 6e 64 20 74 68 65 20 42 74 43 75 72 et and the BtCur
16e33 73 6f 72 2e 61 4f 76 65 72 66 6c 6f 77 5b 5d 0a sor.aOverflow[].
16e34 20 20 20 20 2a 2a 20 68 61 73 20 6e 6f 74 20 62 ** has not b
16e35 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2c 20 61 een allocated, a
16e36 6c 6c 6f 63 61 74 65 20 69 74 20 6e 6f 77 2e 20 llocate it now.
16e37 54 68 65 20 61 72 72 61 79 20 69 73 20 73 69 7a The array is siz
16e38 65 64 20 61 74 0a 20 20 20 20 2a 2a 20 6f 6e 65 ed at. ** one
16e39 20 65 6e 74 72 79 20 66 6f 72 20 65 61 63 68 20 entry for each
16e3a 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e overflow page in
16e3b 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 68 the overflow ch
16e3c 61 69 6e 2e 20 54 68 65 0a 20 20 20 20 2a 2a 20 ain. The. **
16e3d 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 page number of t
16e3e 68 65 20 66 69 72 73 74 20 6f 76 65 72 66 6c 6f he first overflo
16e3f 77 20 70 61 67 65 20 69 73 20 73 74 6f 72 65 64 w page is stored
16e40 20 69 6e 20 61 4f 76 65 72 66 6c 6f 77 5b 30 5d in aOverflow[0]
16e41 2c 0a 20 20 20 20 2a 2a 20 65 74 63 2e 20 41 20 ,. ** etc. A
16e42 76 61 6c 75 65 20 6f 66 20 30 20 69 6e 20 74 68 value of 0 in th
16e43 65 20 61 4f 76 65 72 66 6c 6f 77 5b 5d 20 61 72 e aOverflow[] ar
16e44 72 61 79 20 6d 65 61 6e 73 20 22 6e 6f 74 20 79 ray means "not y
16e45 65 74 20 6b 6e 6f 77 6e 22 0a 20 20 20 20 2a 2a et known". **
16e46 20 28 74 68 65 20 63 61 63 68 65 20 69 73 20 6c (the cache is l
16e47 61 7a 69 6c 79 20 70 6f 70 75 6c 61 74 65 64 29 azily populated)
16e48 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
16e49 20 70 43 75 72 2d 3e 69 73 49 6e 63 72 62 6c 6f pCur->isIncrblo
16e4a 62 48 61 6e 64 6c 65 20 26 26 20 21 70 43 75 72 bHandle && !pCur
16e4b 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 ->aOverflow ){.
16e4c 20 20 20 20 20 69 6e 74 20 6e 4f 76 66 6c 20 3d int nOvfl =
16e4d 20 28 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 50 61 (pCur->info.nPa
16e4e 79 6c 6f 61 64 2d 70 43 75 72 2d 3e 69 6e 66 6f yload-pCur->info
16e4f 2e 6e 4c 6f 63 61 6c 2b 6f 76 66 6c 53 69 7a 65 .nLocal+ovflSize
16e50 2d 31 29 2f 6f 76 66 6c 53 69 7a 65 3b 0a 20 20 -1)/ovflSize;.
16e51 20 20 20 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 pCur->aOverf
16e52 6c 6f 77 20 3d 20 28 50 67 6e 6f 20 2a 29 73 71 low = (Pgno *)sq
16e53 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 lite3MallocZero(
16e54 73 69 7a 65 6f 66 28 50 67 6e 6f 29 2a 6e 4f 76 sizeof(Pgno)*nOv
16e55 66 6c 29 3b 0a 20 20 20 20 20 20 2f 2a 20 6e 4f fl);. /* nO
16e56 76 66 6c 20 69 73 20 61 6c 77 61 79 73 20 70 6f vfl is always po
16e57 73 69 74 69 76 65 2e 20 20 49 66 20 69 74 20 77 sitive. If it w
16e58 65 72 65 20 7a 65 72 6f 2c 20 66 65 74 63 68 50 ere zero, fetchP
16e59 61 79 6c 6f 61 64 20 77 6f 75 6c 64 20 68 61 76 ayload would hav
16e5a 65 0a 20 20 20 20 20 20 2a 2a 20 62 65 65 6e 20 e. ** been
16e5b 75 73 65 64 20 69 6e 73 74 65 61 64 20 6f 66 20 used instead of
16e5c 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 2a 2f this routine. */
16e5d 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57 41 59 . if( ALWAY
16e5e 53 28 6e 4f 76 66 6c 29 20 26 26 20 21 70 43 75 S(nOvfl) && !pCu
16e5f 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 29 7b 0a r->aOverflow ){.
16e60 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
16e61 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 ITE_NOMEM;.
16e62 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a }. }.. /*
16e63 20 49 66 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 If the overflow
16e64 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 page-list cache
16e65 20 68 61 73 20 62 65 65 6e 20 61 6c 6c 6f 63 61 has been alloca
16e66 74 65 64 20 61 6e 64 20 74 68 65 0a 20 20 20 20 ted and the.
16e67 2a 2a 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 ** entry for the
16e68 20 66 69 72 73 74 20 72 65 71 75 69 72 65 64 20 first required
16e69 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 73 overflow page is
16e6a 20 76 61 6c 69 64 2c 20 73 6b 69 70 0a 20 20 20 valid, skip.
16e6b 20 2a 2a 20 64 69 72 65 63 74 6c 79 20 74 6f 20 ** directly to
16e6c 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 it.. */. i
16e6d 66 28 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c f( pCur->aOverfl
16e6e 6f 77 20 26 26 20 70 43 75 72 2d 3e 61 4f 76 65 ow && pCur->aOve
16e6f 72 66 6c 6f 77 5b 6f 66 66 73 65 74 2f 6f 76 66 rflow[offset/ovf
16e70 6c 53 69 7a 65 5d 20 29 7b 0a 20 20 20 20 20 20 lSize] ){.
16e71 69 49 64 78 20 3d 20 28 6f 66 66 73 65 74 2f 6f iIdx = (offset/o
16e72 76 66 6c 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 vflSize);.
16e73 6e 65 78 74 50 61 67 65 20 3d 20 70 43 75 72 2d nextPage = pCur-
16e74 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 5d >aOverflow[iIdx]
16e75 3b 0a 20 20 20 20 20 20 6f 66 66 73 65 74 20 3d ;. offset =
16e76 20 28 6f 66 66 73 65 74 25 6f 76 66 6c 53 69 7a (offset%ovflSiz
16e77 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 e);. }.#endif
16e78 0a 0a 20 20 20 20 66 6f 72 28 20 3b 20 72 63 3d .. for( ; rc=
16e79 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 61 6d =SQLITE_OK && am
16e7a 74 3e 30 20 26 26 20 6e 65 78 74 50 61 67 65 3b t>0 && nextPage;
16e7b 20 69 49 64 78 2b 2b 29 7b 0a 0a 23 69 66 6e 64 iIdx++){..#ifnd
16e7c 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 ef SQLITE_OMIT_I
16e7d 4e 43 52 42 4c 4f 42 0a 20 20 20 20 20 20 2f 2a NCRBLOB. /*
16e7e 20 49 66 20 72 65 71 75 69 72 65 64 2c 20 70 6f If required, po
16e7f 70 75 6c 61 74 65 20 74 68 65 20 6f 76 65 72 66 pulate the overf
16e80 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 low page-list ca
16e81 63 68 65 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 che. */. if
16e82 28 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f ( pCur->aOverflo
16e83 77 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 w ){. ass
16e84 65 72 74 28 21 70 43 75 72 2d 3e 61 4f 76 65 72 ert(!pCur->aOver
16e85 66 6c 6f 77 5b 69 49 64 78 5d 20 7c 7c 20 70 43 flow[iIdx] || pC
16e86 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 ur->aOverflow[iI
16e87 64 78 5d 3d 3d 6e 65 78 74 50 61 67 65 29 3b 0a dx]==nextPage);.
16e88 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 61 4f pCur->aO
16e89 76 65 72 66 6c 6f 77 5b 69 49 64 78 5d 20 3d 20 verflow[iIdx] =
16e8a 6e 65 78 74 50 61 67 65 3b 0a 20 20 20 20 20 20 nextPage;.
16e8b 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 }.#endif..
16e8c 69 66 28 20 6f 66 66 73 65 74 3e 3d 6f 76 66 6c if( offset>=ovfl
16e8d 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 Size ){.
16e8e 2f 2a 20 54 68 65 20 6f 6e 6c 79 20 72 65 61 73 /* The only reas
16e8f 6f 6e 20 74 6f 20 72 65 61 64 20 74 68 69 73 20 on to read this
16e90 70 61 67 65 20 69 73 20 74 6f 20 6f 62 74 61 69 page is to obtai
16e91 6e 20 74 68 65 20 70 61 67 65 0a 20 20 20 20 20 n the page.
16e92 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72 ** number for
16e93 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 the next page i
16e94 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 n the overflow c
16e95 68 61 69 6e 2e 20 54 68 65 20 70 61 67 65 0a 20 hain. The page.
16e96 20 20 20 20 20 20 20 2a 2a 20 64 61 74 61 20 69 ** data i
16e97 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 2e 20 s not required.
16e98 53 6f 20 66 69 72 73 74 20 74 72 79 20 74 6f 20 So first try to
16e99 6c 6f 6f 6b 75 70 20 74 68 65 20 6f 76 65 72 66 lookup the overf
16e9a 6c 6f 77 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 low. ** p
16e9b 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 2c 20 age-list cache,
16e9c 69 66 20 61 6e 79 2c 20 74 68 65 6e 20 66 61 6c if any, then fal
16e9d 6c 20 62 61 63 6b 20 74 6f 20 74 68 65 20 67 65 l back to the ge
16e9e 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 29 0a tOverflowPage().
16e9f 20 20 20 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 ** funct
16ea0 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a ion.. */.
16ea1 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
16ea2 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 20 MIT_INCRBLOB.
16ea3 20 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 61 if( pCur->a
16ea4 4f 76 65 72 66 6c 6f 77 20 26 26 20 70 43 75 72 Overflow && pCur
16ea5 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 ->aOverflow[iIdx
16ea6 2b 31 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 +1] ){.
16ea7 20 6e 65 78 74 50 61 67 65 20 3d 20 70 43 75 72 nextPage = pCur
16ea8 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 ->aOverflow[iIdx
16ea9 2b 31 5d 3b 0a 20 20 20 20 20 20 20 20 7d 20 65 +1];. } e
16eaa 6c 73 65 20 0a 23 65 6e 64 69 66 0a 20 20 20 20 lse .#endif.
16eab 20 20 20 20 20 20 72 63 20 3d 20 67 65 74 4f 76 rc = getOv
16eac 65 72 66 6c 6f 77 50 61 67 65 28 70 42 74 2c 20 erflowPage(pBt,
16ead 6e 65 78 74 50 61 67 65 2c 20 30 2c 20 26 6e 65 nextPage, 0, &ne
16eae 78 74 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 xtPage);.
16eaf 20 6f 66 66 73 65 74 20 2d 3d 20 6f 76 66 6c 53 offset -= ovflS
16eb0 69 7a 65 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 ize;. }else
16eb1 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 65 {. /* Nee
16eb2 64 20 74 6f 20 72 65 61 64 20 74 68 69 73 20 70 d to read this p
16eb3 61 67 65 20 70 72 6f 70 65 72 6c 79 2e 20 49 74 age properly. It
16eb4 20 63 6f 6e 74 61 69 6e 73 20 73 6f 6d 65 20 6f contains some o
16eb5 66 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a f the. **
16eb6 20 72 61 6e 67 65 20 6f 66 20 64 61 74 61 20 74 range of data t
16eb7 68 61 74 20 69 73 20 62 65 69 6e 67 20 72 65 61 hat is being rea
16eb8 64 20 28 65 4f 70 3d 3d 30 29 20 6f 72 20 77 72 d (eOp==0) or wr
16eb9 69 74 74 65 6e 20 28 65 4f 70 21 3d 30 29 2e 0a itten (eOp!=0)..
16eba 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
16ebb 20 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 DbPage *pDbPa
16ebc 67 65 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 ge;. int
16ebd 61 20 3d 20 61 6d 74 3b 0a 20 20 20 20 20 20 20 a = amt;.
16ebe 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
16ebf 65 72 47 65 74 28 70 42 74 2d 3e 70 50 61 67 65 erGet(pBt->pPage
16ec0 72 2c 20 6e 65 78 74 50 61 67 65 2c 20 26 70 44 r, nextPage, &pD
16ec1 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 bPage);.
16ec2 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
16ec3 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 K ){. a
16ec4 50 61 79 6c 6f 61 64 20 3d 20 73 71 6c 69 74 65 Payload = sqlite
16ec5 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 44 3PagerGetData(pD
16ec6 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 bPage);.
16ec7 20 20 6e 65 78 74 50 61 67 65 20 3d 20 67 65 74 nextPage = get
16ec8 34 62 79 74 65 28 61 50 61 79 6c 6f 61 64 29 3b 4byte(aPayload);
16ec9 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 61 . if( a
16eca 20 2b 20 6f 66 66 73 65 74 20 3e 20 6f 76 66 6c + offset > ovfl
16ecb 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 Size ){.
16ecc 20 20 20 20 61 20 3d 20 6f 76 66 6c 53 69 7a 65 a = ovflSize
16ecd 20 2d 20 6f 66 66 73 65 74 3b 0a 20 20 20 20 20 - offset;.
16ece 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
16ecf 20 72 63 20 3d 20 63 6f 70 79 50 61 79 6c 6f 61 rc = copyPayloa
16ed0 64 28 26 61 50 61 79 6c 6f 61 64 5b 6f 66 66 73 d(&aPayload[offs
16ed1 65 74 2b 34 5d 2c 20 70 42 75 66 2c 20 61 2c 20 et+4], pBuf, a,
16ed2 65 4f 70 2c 20 70 44 62 50 61 67 65 29 3b 0a 20 eOp, pDbPage);.
16ed3 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
16ed4 50 61 67 65 72 55 6e 72 65 66 28 70 44 62 50 61 PagerUnref(pDbPa
16ed5 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6f ge);. o
16ed6 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 20 ffset = 0;.
16ed7 20 20 20 20 20 61 6d 74 20 2d 3d 20 61 3b 0a 20 amt -= a;.
16ed8 20 20 20 20 20 20 20 20 20 70 42 75 66 20 2b 3d pBuf +=
16ed9 20 61 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 a;. }.
16eda 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
16edb 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
16edc 45 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20 29 7b E_OK && amt>0 ){
16edd 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
16ede 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
16edf 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
16ee0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 ;.}../*.** Read
16ee1 70 61 72 74 20 6f 66 20 74 68 65 20 6b 65 79 20 part of the key
16ee2 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
16ee3 63 75 72 73 6f 72 20 70 43 75 72 2e 20 20 45 78 cursor pCur. Ex
16ee4 61 63 74 6c 79 0a 2a 2a 20 22 61 6d 74 22 20 62 actly.** "amt" b
16ee5 79 74 65 73 20 77 69 6c 6c 20 62 65 20 74 72 61 ytes will be tra
16ee6 6e 73 66 65 72 65 64 20 69 6e 74 6f 20 70 42 75 nsfered into pBu
16ee7 66 5b 5d 2e 20 20 54 68 65 20 74 72 61 6e 73 66 f[]. The transf
16ee8 65 72 0a 2a 2a 20 62 65 67 69 6e 73 20 61 74 20 er.** begins at
16ee9 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 0a 2a 2a 20 "offset"..**.**
16eea 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 The caller must
16eeb 65 6e 73 75 72 65 20 74 68 61 74 20 70 43 75 72 ensure that pCur
16eec 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 is pointing to
16eed 61 20 76 61 6c 69 64 20 72 6f 77 0a 2a 2a 20 69 a valid row.** i
16eee 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a n the table..**.
16eef 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 ** Return SQLITE
16ef0 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 6f _OK on success o
16ef1 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 r an error code
16ef2 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 if anything goes
16ef3 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20 41 6e 20 65 .** wrong. An e
16ef4 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 rror is returned
16ef5 20 69 66 20 22 6f 66 66 73 65 74 2b 61 6d 74 22 if "offset+amt"
16ef6 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 0a is larger than.
16ef7 2a 2a 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 ** the available
16ef8 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f 0a 53 51 4c payload..*/.SQL
16ef9 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
16efa 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 28 sqlite3BtreeKey(
16efb 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 BtCursor *pCur,
16efc 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 u32 offset, u32
16efd 61 6d 74 2c 20 76 6f 69 64 20 2a 70 42 75 66 29 amt, void *pBuf)
16efe 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 {. assert( curs
16eff 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 orHoldsMutex(pCu
16f00 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 r) );. assert(
16f01 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 pCur->eState==CU
16f02 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 RSOR_VALID );.
16f03 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50 assert( pCur->iP
16f04 61 67 65 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e age>=0 && pCur->
16f05 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
16f06 67 65 5d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ge] );. assert(
16f07 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 pCur->aiIdx[pCu
16f08 72 2d 3e 69 50 61 67 65 5d 3c 70 43 75 72 2d 3e r->iPage]<pCur->
16f09 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
16f0a 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 ge]->nCell );.
16f0b 72 65 74 75 72 6e 20 61 63 63 65 73 73 50 61 79 return accessPay
16f0c 6c 6f 61 64 28 70 43 75 72 2c 20 6f 66 66 73 65 load(pCur, offse
16f0d 74 2c 20 61 6d 74 2c 20 28 75 6e 73 69 67 6e 65 t, amt, (unsigne
16f0e 64 20 63 68 61 72 2a 29 70 42 75 66 2c 20 30 29 d char*)pBuf, 0)
16f0f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 ;.}../*.** Read
16f10 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61 part of the data
16f11 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
16f12 20 63 75 72 73 6f 72 20 70 43 75 72 2e 20 20 45 cursor pCur. E
16f13 78 61 63 74 6c 79 0a 2a 2a 20 22 61 6d 74 22 20 xactly.** "amt"
16f14 62 79 74 65 73 20 77 69 6c 6c 20 62 65 20 74 72 bytes will be tr
16f15 61 6e 73 66 65 72 65 64 20 69 6e 74 6f 20 70 42 ansfered into pB
16f16 75 66 5b 5d 2e 20 20 54 68 65 20 74 72 61 6e 73 uf[]. The trans
16f17 66 65 72 0a 2a 2a 20 62 65 67 69 6e 73 20 61 74 fer.** begins at
16f18 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 0a 2a 2a "offset"..**.**
16f19 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f Return SQLITE_O
16f1a 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20 K on success or
16f1b 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 an error code if
16f1c 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a anything goes.*
16f1d 2a 20 77 72 6f 6e 67 2e 20 20 41 6e 20 65 72 72 * wrong. An err
16f1e 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 or is returned i
16f1f 66 20 22 6f 66 66 73 65 74 2b 61 6d 74 22 20 69 f "offset+amt" i
16f20 73 20 6c 61 72 67 65 72 20 74 68 61 6e 0a 2a 2a s larger than.**
16f21 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20 70 the available p
16f22 61 79 6c 6f 61 64 2e 0a 2a 2f 0a 53 51 4c 49 54 ayload..*/.SQLIT
16f23 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
16f24 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 28 42 lite3BtreeData(B
16f25 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 75 tCursor *pCur, u
16f26 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61 32 offset, u32 a
16f27 6d 74 2c 20 76 6f 69 64 20 2a 70 42 75 66 29 7b mt, void *pBuf){
16f28 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 23 69 66 6e . int rc;..#ifn
16f29 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
16f2a 49 4e 43 52 42 4c 4f 42 0a 20 20 69 66 20 28 20 INCRBLOB. if (
16f2b 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 pCur->eState==CU
16f2c 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b 0a RSOR_INVALID ){.
16f2d 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
16f2e 45 5f 41 42 4f 52 54 3b 0a 20 20 7d 0a 23 65 6e E_ABORT;. }.#en
16f2f 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 63 dif.. assert( c
16f30 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
16f31 70 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 pCur) );. rc =
16f32 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 restoreCursorPos
16f33 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 ition(pCur);. i
16f34 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
16f35 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
16f36 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 pCur->eState==CU
16f37 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 RSOR_VALID );.
16f38 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
16f39 69 50 61 67 65 3e 3d 30 20 26 26 20 70 43 75 72 iPage>=0 && pCur
16f3a 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
16f3b 50 61 67 65 5d 20 29 3b 0a 20 20 20 20 61 73 73 Page] );. ass
16f3c 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 ert( pCur->aiIdx
16f3d 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 43 [pCur->iPage]<pC
16f3e 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
16f3f 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 >iPage]->nCell )
16f40 3b 0a 20 20 20 20 72 63 20 3d 20 61 63 63 65 73 ;. rc = acces
16f41 73 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 6f sPayload(pCur, o
16f42 66 66 73 65 74 2c 20 61 6d 74 2c 20 70 42 75 66 ffset, amt, pBuf
16f43 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 , 0);. }. retu
16f44 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
16f45 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 Return a pointer
16f46 20 74 6f 20 70 61 79 6c 6f 61 64 20 69 6e 66 6f to payload info
16f47 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65 rmation from the
16f48 20 65 6e 74 72 79 20 74 68 61 74 20 74 68 65 20 entry that the
16f49 0a 2a 2a 20 70 43 75 72 20 63 75 72 73 6f 72 20 .** pCur cursor
16f4a 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 is pointing to.
16f4b 20 54 68 65 20 70 6f 69 6e 74 65 72 20 69 73 20 The pointer is
16f4c 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 to the beginning
16f4d 20 6f 66 0a 2a 2a 20 74 68 65 20 6b 65 79 20 69 of.** the key i
16f4e 66 20 73 6b 69 70 4b 65 79 3d 3d 30 20 61 6e 64 f skipKey==0 and
16f4f 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 it points to th
16f50 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 64 e beginning of d
16f51 61 74 61 20 69 66 0a 2a 2a 20 73 6b 69 70 4b 65 ata if.** skipKe
16f52 79 3d 3d 31 2e 20 20 54 68 65 20 6e 75 6d 62 65 y==1. The numbe
16f53 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 61 76 r of bytes of av
16f54 61 69 6c 61 62 6c 65 20 6b 65 79 2f 64 61 74 61 ailable key/data
16f55 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 69 is written.** i
16f56 6e 74 6f 20 2a 70 41 6d 74 2e 20 20 49 66 20 2a nto *pAmt. If *
16f57 70 41 6d 74 3d 3d 30 2c 20 74 68 65 6e 20 74 68 pAmt==0, then th
16f58 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 e value returned
16f59 20 77 69 6c 6c 20 6e 6f 74 20 62 65 0a 2a 2a 20 will not be.**
16f5a 61 20 76 61 6c 69 64 20 70 6f 69 6e 74 65 72 2e a valid pointer.
16f5b 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
16f5c 69 6e 65 20 69 73 20 61 6e 20 6f 70 74 69 6d 69 ine is an optimi
16f5d 7a 61 74 69 6f 6e 2e 20 20 49 74 20 69 73 20 63 zation. It is c
16f5e 6f 6d 6d 6f 6e 20 66 6f 72 20 74 68 65 20 65 6e ommon for the en
16f5f 74 69 72 65 20 6b 65 79 0a 2a 2a 20 61 6e 64 20 tire key.** and
16f60 64 61 74 61 20 74 6f 20 66 69 74 20 6f 6e 20 74 data to fit on t
16f61 68 65 20 6c 6f 63 61 6c 20 70 61 67 65 20 61 6e he local page an
16f62 64 20 66 6f 72 20 74 68 65 72 65 20 74 6f 20 62 d for there to b
16f63 65 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a e no overflow.**
16f64 20 70 61 67 65 73 2e 20 20 57 68 65 6e 20 74 68 pages. When th
16f65 61 74 20 69 73 20 73 6f 2c 20 74 68 69 73 20 72 at is so, this r
16f66 6f 75 74 69 6e 65 20 63 61 6e 20 62 65 20 75 73 outine can be us
16f67 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 ed to access the
16f68 0a 2a 2a 20 6b 65 79 20 61 6e 64 20 64 61 74 61 .** key and data
16f69 20 77 69 74 68 6f 75 74 20 6d 61 6b 69 6e 67 20 without making
16f6a 61 20 63 6f 70 79 2e 20 20 49 66 20 74 68 65 20 a copy. If the
16f6b 6b 65 79 20 61 6e 64 2f 6f 72 20 64 61 74 61 20 key and/or data
16f6c 73 70 69 6c 6c 73 0a 2a 2a 20 6f 6e 74 6f 20 6f spills.** onto o
16f6d 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2c 20 74 verflow pages, t
16f6e 68 65 6e 20 61 63 63 65 73 73 50 61 79 6c 6f 61 hen accessPayloa
16f6f 64 28 29 20 6d 75 73 74 20 62 65 20 75 73 65 64 d() must be used
16f70 20 74 6f 20 72 65 61 73 73 65 6d 62 6c 65 0a 2a to reassemble.*
16f71 2a 20 74 68 65 20 6b 65 79 2f 64 61 74 61 20 61 * the key/data a
16f72 6e 64 20 63 6f 70 79 20 69 74 20 69 6e 74 6f 20 nd copy it into
16f73 61 20 70 72 65 61 6c 6c 6f 63 61 74 65 64 20 62 a preallocated b
16f74 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 uffer..**.** The
16f75 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e 65 pointer returne
16f76 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e d by this routin
16f77 65 20 6c 6f 6f 6b 73 20 64 69 72 65 63 74 6c 79 e looks directly
16f78 20 69 6e 74 6f 20 74 68 65 20 63 61 63 68 65 64 into the cached
16f79 0a 2a 2a 20 70 61 67 65 20 6f 66 20 74 68 65 20 .** page of the
16f7a 64 61 74 61 62 61 73 65 2e 20 20 54 68 65 20 64 database. The d
16f7b 61 74 61 20 6d 69 67 68 74 20 63 68 61 6e 67 65 ata might change
16f7c 20 6f 72 20 6d 6f 76 65 20 74 68 65 20 6e 65 78 or move the nex
16f7d 74 20 74 69 6d 65 0a 2a 2a 20 61 6e 79 20 62 74 t time.** any bt
16f7e 72 65 65 20 72 6f 75 74 69 6e 65 20 69 73 20 63 ree routine is c
16f7f 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 alled..*/.static
16f80 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 const unsigned
16f81 63 68 61 72 20 2a 66 65 74 63 68 50 61 79 6c 6f char *fetchPaylo
16f82 61 64 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a ad(. BtCursor *
16f83 70 43 75 72 2c 20 20 20 20 20 20 2f 2a 20 43 75 pCur, /* Cu
16f84 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f rsor pointing to
16f85 20 65 6e 74 72 79 20 74 6f 20 72 65 61 64 20 66 entry to read f
16f86 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 41 rom */. int *pA
16f87 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a mt, /*
16f88 20 57 72 69 74 65 20 74 68 65 20 6e 75 6d 62 65 Write the numbe
16f89 72 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 62 r of available b
16f8a 79 74 65 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 ytes here */. i
16f8b 6e 74 20 73 6b 69 70 4b 65 79 20 20 20 20 20 20 nt skipKey
16f8c 20 20 20 20 2f 2a 20 72 65 61 64 20 62 65 67 69 /* read begi
16f8d 6e 6e 69 6e 67 20 61 74 20 64 61 74 61 20 69 66 nning at data if
16f8e 20 74 68 69 73 20 69 73 20 74 72 75 65 20 2a 2f this is true */
16f8f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 .){. unsigned c
16f90 68 61 72 20 2a 61 50 61 79 6c 6f 61 64 3b 0a 20 har *aPayload;.
16f91 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b MemPage *pPage;
16f92 0a 20 20 75 33 32 20 6e 4b 65 79 3b 0a 20 20 75 . u32 nKey;. u
16f93 33 32 20 6e 4c 6f 63 61 6c 3b 0a 0a 20 20 61 73 32 nLocal;.. as
16f94 73 65 72 74 28 20 70 43 75 72 21 3d 30 20 26 26 sert( pCur!=0 &&
16f95 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d 30 20 pCur->iPage>=0
16f96 26 26 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b && pCur->apPage[
16f97 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 3b 0a 20 pCur->iPage]);.
16f98 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 assert( pCur->e
16f99 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 State==CURSOR_VA
16f9a 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28 LID );. assert(
16f9b 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
16f9c 78 28 70 43 75 72 29 20 29 3b 0a 20 20 70 50 61 x(pCur) );. pPa
16f9d 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 ge = pCur->apPag
16f9e 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a e[pCur->iPage];.
16f9f 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
16fa0 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 aiIdx[pCur->iPag
16fa1 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 e]<pPage->nCell
16fa2 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 );. if( NEVER(p
16fa3 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 3d Cur->info.nSize=
16fa4 3d 30 29 20 29 7b 0a 20 20 20 20 62 74 72 65 65 =0) ){. btree
16fa5 50 61 72 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e ParseCell(pCur->
16fa6 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
16fa7 67 65 5d 2c 20 70 43 75 72 2d 3e 61 69 49 64 78 ge], pCur->aiIdx
16fa8 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2c 0a 20 [pCur->iPage],.
16fa9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16faa 20 20 26 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 0a &pCur->info);.
16fab 20 20 7d 0a 20 20 61 50 61 79 6c 6f 61 64 20 3d }. aPayload =
16fac 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 70 43 65 6c pCur->info.pCel
16fad 6c 3b 0a 20 20 61 50 61 79 6c 6f 61 64 20 2b 3d l;. aPayload +=
16fae 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 48 65 61 pCur->info.nHea
16faf 64 65 72 3b 0a 20 20 69 66 28 20 70 50 61 67 65 der;. if( pPage
16fb0 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 ->intKey ){.
16fb1 6e 4b 65 79 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 nKey = 0;. }els
16fb2 65 7b 0a 20 20 20 20 6e 4b 65 79 20 3d 20 28 69 e{. nKey = (i
16fb3 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b nt)pCur->info.nK
16fb4 65 79 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 6b ey;. }. if( sk
16fb5 69 70 4b 65 79 20 29 7b 0a 20 20 20 20 61 50 61 ipKey ){. aPa
16fb6 79 6c 6f 61 64 20 2b 3d 20 6e 4b 65 79 3b 0a 20 yload += nKey;.
16fb7 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70 43 75 72 nLocal = pCur
16fb8 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2d 20 ->info.nLocal -
16fb9 6e 4b 65 79 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 nKey;. }else{.
16fba 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70 43 75 72 nLocal = pCur
16fbb 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 ->info.nLocal;.
16fbc 20 20 20 61 73 73 65 72 74 28 20 6e 4c 6f 63 61 assert( nLoca
16fbd 6c 3c 3d 6e 4b 65 79 20 29 3b 0a 20 20 7d 0a 20 l<=nKey );. }.
16fbe 20 2a 70 41 6d 74 20 3d 20 6e 4c 6f 63 61 6c 3b *pAmt = nLocal;
16fbf 0a 20 20 72 65 74 75 72 6e 20 61 50 61 79 6c 6f . return aPaylo
16fc0 61 64 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 46 6f ad;.}.../*.** Fo
16fc1 72 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 r the entry that
16fc2 20 63 75 72 73 6f 72 20 70 43 75 72 20 69 73 20 cursor pCur is
16fc3 70 6f 69 6e 74 20 74 6f 2c 20 72 65 74 75 72 6e point to, return
16fc4 20 61 73 0a 2a 2a 20 6d 61 6e 79 20 62 79 74 65 as.** many byte
16fc5 73 20 6f 66 20 74 68 65 20 6b 65 79 20 6f 72 20 s of the key or
16fc6 64 61 74 61 20 61 73 20 61 72 65 20 61 76 61 69 data as are avai
16fc7 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 6c 6f 63 lable on the loc
16fc8 61 6c 0a 2a 2a 20 62 2d 74 72 65 65 20 70 61 67 al.** b-tree pag
16fc9 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 6e 75 e. Write the nu
16fca 6d 62 65 72 20 6f 66 20 61 76 61 69 6c 61 62 6c mber of availabl
16fcb 65 20 62 79 74 65 73 20 69 6e 74 6f 20 2a 70 41 e bytes into *pA
16fcc 6d 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f mt..**.** The po
16fcd 69 6e 74 65 72 20 72 65 74 75 72 6e 65 64 20 69 inter returned i
16fce 73 20 65 70 68 65 6d 65 72 61 6c 2e 20 20 54 68 s ephemeral. Th
16fcf 65 20 6b 65 79 2f 64 61 74 61 20 6d 61 79 20 6d e key/data may m
16fd0 6f 76 65 0a 2a 2a 20 6f 72 20 62 65 20 64 65 73 ove.** or be des
16fd1 74 72 6f 79 65 64 20 6f 6e 20 74 68 65 20 6e 65 troyed on the ne
16fd2 78 74 20 63 61 6c 6c 20 74 6f 20 61 6e 79 20 42 xt call to any B
16fd3 74 72 65 65 20 72 6f 75 74 69 6e 65 2c 0a 2a 2a tree routine,.**
16fd4 20 69 6e 63 6c 75 64 69 6e 67 20 63 61 6c 6c 73 including calls
16fd5 20 66 72 6f 6d 20 6f 74 68 65 72 20 74 68 72 65 from other thre
16fd6 61 64 73 20 61 67 61 69 6e 73 74 20 74 68 65 20 ads against the
16fd7 73 61 6d 65 20 63 61 63 68 65 2e 0a 2a 2a 20 48 same cache..** H
16fd8 65 6e 63 65 2c 20 61 20 6d 75 74 65 78 20 6f 6e ence, a mutex on
16fd9 20 74 68 65 20 42 74 53 68 61 72 65 64 20 73 68 the BtShared sh
16fda 6f 75 6c 64 20 62 65 20 68 65 6c 64 20 70 72 69 ould be held pri
16fdb 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 0a 2a 2a or to calling.**
16fdc 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a this routine..*
16fdd 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 *.** These routi
16fde 6e 65 73 20 69 73 20 75 73 65 64 20 74 6f 20 67 nes is used to g
16fdf 65 74 20 71 75 69 63 6b 20 61 63 63 65 73 73 20 et quick access
16fe0 74 6f 20 6b 65 79 20 61 6e 64 20 64 61 74 61 0a to key and data.
16fe1 2a 2a 20 69 6e 20 74 68 65 20 63 6f 6d 6d 6f 6e ** in the common
16fe2 20 63 61 73 65 20 77 68 65 72 65 20 6e 6f 20 6f case where no o
16fe3 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 61 72 verflow pages ar
16fe4 65 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 e used..*/.SQLIT
16fe5 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 E_PRIVATE const
16fe6 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 void *sqlite3Btr
16fe7 65 65 4b 65 79 46 65 74 63 68 28 42 74 43 75 72 eeKeyFetch(BtCur
16fe8 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a sor *pCur, int *
16fe9 70 41 6d 74 29 7b 0a 20 20 63 6f 6e 73 74 20 76 pAmt){. const v
16fea 6f 69 64 20 2a 70 20 3d 20 30 3b 0a 20 20 61 73 oid *p = 0;. as
16feb 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
16fec 74 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 tex_held(pCur->p
16fed 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 Btree->db->mutex
16fee 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 ) );. assert( c
16fef 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
16ff0 70 43 75 72 29 20 29 3b 0a 20 20 69 66 28 20 41 pCur) );. if( A
16ff1 4c 57 41 59 53 28 70 43 75 72 2d 3e 65 53 74 61 LWAYS(pCur->eSta
16ff2 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 te==CURSOR_VALID
16ff3 29 20 29 7b 0a 20 20 20 20 70 20 3d 20 28 63 6f ) ){. p = (co
16ff4 6e 73 74 20 76 6f 69 64 2a 29 66 65 74 63 68 50 nst void*)fetchP
16ff5 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 70 41 6d ayload(pCur, pAm
16ff6 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 t, 0);. }. ret
16ff7 75 72 6e 20 70 3b 0a 7d 0a 53 51 4c 49 54 45 5f urn p;.}.SQLITE_
16ff8 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f PRIVATE const vo
16ff9 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 id *sqlite3Btree
16ffa 44 61 74 61 46 65 74 63 68 28 42 74 43 75 72 73 DataFetch(BtCurs
16ffb 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 or *pCur, int *p
16ffc 41 6d 74 29 7b 0a 20 20 63 6f 6e 73 74 20 76 6f Amt){. const vo
16ffd 69 64 20 2a 70 20 3d 20 30 3b 0a 20 20 61 73 73 id *p = 0;. ass
16ffe 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
16fff 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 ex_held(pCur->pB
17000 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 tree->db->mutex)
17001 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75 );. assert( cu
17002 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
17003 43 75 72 29 20 29 3b 0a 20 20 69 66 28 20 41 4c Cur) );. if( AL
17004 57 41 59 53 28 70 43 75 72 2d 3e 65 53 74 61 74 WAYS(pCur->eStat
17005 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 29 e==CURSOR_VALID)
17006 20 29 7b 0a 20 20 20 20 70 20 3d 20 28 63 6f 6e ){. p = (con
17007 73 74 20 76 6f 69 64 2a 29 66 65 74 63 68 50 61 st void*)fetchPa
17008 79 6c 6f 61 64 28 70 43 75 72 2c 20 70 41 6d 74 yload(pCur, pAmt
17009 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 , 1);. }. retu
1700a 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 rn p;.}.../*.**
1700b 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 Move the cursor
1700c 64 6f 77 6e 20 74 6f 20 61 20 6e 65 77 20 63 68 down to a new ch
1700d 69 6c 64 20 70 61 67 65 2e 20 20 54 68 65 20 6e ild page. The n
1700e 65 77 50 67 6e 6f 20 61 72 67 75 6d 65 6e 74 20 ewPgno argument
1700f 69 73 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 6e is the.** page n
17010 75 6d 62 65 72 20 6f 66 20 74 68 65 20 63 68 69 umber of the chi
17011 6c 64 20 70 61 67 65 20 74 6f 20 6d 6f 76 65 20 ld page to move
17012 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 to..**.** This f
17013 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 unction returns
17014 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 69 SQLITE_CORRUPT i
17015 66 20 74 68 65 20 70 61 67 65 2d 68 65 61 64 65 f the page-heade
17016 72 20 66 6c 61 67 73 20 66 69 65 6c 64 20 6f 66 r flags field of
17017 0a 2a 2a 20 74 68 65 20 6e 65 77 20 63 68 69 6c .** the new chil
17018 64 20 70 61 67 65 20 64 6f 65 73 20 6e 6f 74 20 d page does not
17019 6d 61 74 63 68 20 74 68 65 20 66 6c 61 67 73 20 match the flags
1701a 66 69 65 6c 64 20 6f 66 20 74 68 65 20 70 61 72 field of the par
1701b 65 6e 74 20 28 69 2e 65 2e 0a 2a 2a 20 69 66 20 ent (i.e..** if
1701c 61 6e 20 69 6e 74 6b 65 79 20 70 61 67 65 20 61 an intkey page a
1701d 70 70 65 61 72 73 20 74 6f 20 62 65 20 74 68 65 ppears to be the
1701e 20 70 61 72 65 6e 74 20 6f 66 20 61 20 6e 6f 6e parent of a non
1701f 2d 69 6e 74 6b 65 79 20 70 61 67 65 2c 20 6f 72 -intkey page, or
17020 0a 2a 2a 20 76 69 63 65 2d 76 65 72 73 61 29 2e .** vice-versa).
17021 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d .*/.static int m
17022 6f 76 65 54 6f 43 68 69 6c 64 28 42 74 43 75 72 oveToChild(BtCur
17023 73 6f 72 20 2a 70 43 75 72 2c 20 75 33 32 20 6e sor *pCur, u32 n
17024 65 77 50 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 72 ewPgno){. int r
17025 63 3b 0a 20 20 69 6e 74 20 69 20 3d 20 70 43 75 c;. int i = pCu
17026 72 2d 3e 69 50 61 67 65 3b 0a 20 20 4d 65 6d 50 r->iPage;. MemP
17027 61 67 65 20 2a 70 4e 65 77 50 61 67 65 3b 0a 20 age *pNewPage;.
17028 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
17029 20 70 43 75 72 2d 3e 70 42 74 3b 0a 0a 20 20 61 pCur->pBt;.. a
1702a 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c ssert( cursorHol
1702b 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b dsMutex(pCur) );
1702c 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d . assert( pCur-
1702d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
1702e 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72 VALID );. asser
1702f 74 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3c 42 t( pCur->iPage<B
17030 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 TCURSOR_MAX_DEPT
17031 48 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d H );. if( pCur-
17032 3e 69 50 61 67 65 3e 3d 28 42 54 43 55 52 53 4f >iPage>=(BTCURSO
17033 52 5f 4d 41 58 5f 44 45 50 54 48 2d 31 29 20 29 R_MAX_DEPTH-1) )
17034 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
17035 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
17036 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 67 65 74 ;. }. rc = get
17037 41 6e 64 49 6e 69 74 50 61 67 65 28 70 42 74 2c AndInitPage(pBt,
17038 20 6e 65 77 50 67 6e 6f 2c 20 26 70 4e 65 77 50 newPgno, &pNewP
17039 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 age);. if( rc )
1703a 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 70 43 return rc;. pC
1703b 75 72 2d 3e 61 70 50 61 67 65 5b 69 2b 31 5d 20 ur->apPage[i+1]
1703c 3d 20 70 4e 65 77 50 61 67 65 3b 0a 20 20 70 43 = pNewPage;. pC
1703d 75 72 2d 3e 61 69 49 64 78 5b 69 2b 31 5d 20 3d ur->aiIdx[i+1] =
1703e 20 30 3b 0a 20 20 70 43 75 72 2d 3e 69 50 61 67 0;. pCur->iPag
1703f 65 2b 2b 3b 0a 0a 20 20 70 43 75 72 2d 3e 69 6e e++;.. pCur->in
17040 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 fo.nSize = 0;.
17041 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 pCur->validNKey
17042 3d 20 30 3b 0a 20 20 69 66 28 20 70 4e 65 77 50 = 0;. if( pNewP
17043 61 67 65 2d 3e 6e 43 65 6c 6c 3c 31 20 7c 7c 20 age->nCell<1 ||
17044 70 4e 65 77 50 61 67 65 2d 3e 69 6e 74 4b 65 79 pNewPage->intKey
17045 21 3d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 !=pCur->apPage[i
17046 5d 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 ]->intKey ){.
17047 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
17048 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d ORRUPT_BKPT;. }
17049 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
1704a 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 _OK;.}..#ifndef
1704b 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 50 61 67 NDEBUG./*.** Pag
1704c 65 20 70 50 61 72 65 6e 74 20 69 73 20 61 6e 20 e pParent is an
1704d 69 6e 74 65 72 6e 61 6c 20 28 6e 6f 6e 2d 6c 65 internal (non-le
1704e 61 66 29 20 74 72 65 65 20 70 61 67 65 2e 20 54 af) tree page. T
1704f 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 0a 2a 2a his function .**
17050 20 61 73 73 65 72 74 73 20 74 68 61 74 20 70 61 asserts that pa
17051 67 65 20 6e 75 6d 62 65 72 20 69 43 68 69 6c 64 ge number iChild
17052 20 69 73 20 74 68 65 20 6c 65 66 74 2d 63 68 69 is the left-chi
17053 6c 64 20 69 66 20 74 68 65 20 69 49 64 78 27 74 ld if the iIdx't
17054 68 0a 2a 2a 20 63 65 6c 6c 20 69 6e 20 70 61 67 h.** cell in pag
17055 65 20 70 50 61 72 65 6e 74 2e 20 4f 72 2c 20 69 e pParent. Or, i
17056 66 20 69 49 64 78 20 69 73 20 65 71 75 61 6c 20 f iIdx is equal
17057 74 6f 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d to the total num
17058 62 65 72 20 6f 66 0a 2a 2a 20 63 65 6c 6c 73 20 ber of.** cells
17059 69 6e 20 70 50 61 72 65 6e 74 2c 20 74 68 61 74 in pParent, that
1705a 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 43 68 page number iCh
1705b 69 6c 64 20 69 73 20 74 68 65 20 72 69 67 68 74 ild is the right
1705c 2d 63 68 69 6c 64 20 6f 66 0a 2a 2a 20 74 68 65 -child of.** the
1705d 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 page..*/.static
1705e 20 76 6f 69 64 20 61 73 73 65 72 74 50 61 72 65 void assertPare
1705f 6e 74 49 6e 64 65 78 28 4d 65 6d 50 61 67 65 20 ntIndex(MemPage
17060 2a 70 50 61 72 65 6e 74 2c 20 69 6e 74 20 69 49 *pParent, int iI
17061 64 78 2c 20 50 67 6e 6f 20 69 43 68 69 6c 64 29 dx, Pgno iChild)
17062 7b 0a 20 20 61 73 73 65 72 74 28 20 69 49 64 78 {. assert( iIdx
17063 3c 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c <=pParent->nCell
17064 20 29 3b 0a 20 20 69 66 28 20 69 49 64 78 3d 3d );. if( iIdx==
17065 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 pParent->nCell )
17066 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 67 65 {. assert( ge
17067 74 34 62 79 74 65 28 26 70 50 61 72 65 6e 74 2d t4byte(&pParent-
17068 3e 61 44 61 74 61 5b 70 50 61 72 65 6e 74 2d 3e >aData[pParent->
17069 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3d 3d 69 hdrOffset+8])==i
1706a 43 68 69 6c 64 20 29 3b 0a 20 20 7d 65 6c 73 65 Child );. }else
1706b 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 67 65 {. assert( ge
1706c 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 t4byte(findCell(
1706d 70 50 61 72 65 6e 74 2c 20 69 49 64 78 29 29 3d pParent, iIdx))=
1706e 3d 69 43 68 69 6c 64 20 29 3b 0a 20 20 7d 0a 7d =iChild );. }.}
1706f 0a 23 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 .#else.# define
17070 20 61 73 73 65 72 74 50 61 72 65 6e 74 49 6e 64 assertParentInd
17071 65 78 28 78 2c 79 2c 7a 29 20 0a 23 65 6e 64 69 ex(x,y,z) .#endi
17072 66 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 f../*.** Move th
17073 65 20 63 75 72 73 6f 72 20 75 70 20 74 6f 20 74 e cursor up to t
17074 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e 0a he parent page..
17075 2a 2a 0a 2a 2a 20 70 43 75 72 2d 3e 69 64 78 20 **.** pCur->idx
17076 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 63 65 is set to the ce
17077 6c 6c 20 69 6e 64 65 78 20 74 68 61 74 20 63 6f ll index that co
17078 6e 74 61 69 6e 73 20 74 68 65 20 70 6f 69 6e 74 ntains the point
17079 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 70 61 67 er.** to the pag
1707a 65 20 77 65 20 61 72 65 20 63 6f 6d 69 6e 67 20 e we are coming
1707b 66 72 6f 6d 2e 20 20 49 66 20 77 65 20 61 72 65 from. If we are
1707c 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 74 68 65 coming from the
1707d 0a 2a 2a 20 72 69 67 68 74 2d 6d 6f 73 74 20 63 .** right-most c
1707e 68 69 6c 64 20 70 61 67 65 20 74 68 65 6e 20 70 hild page then p
1707f 43 75 72 2d 3e 69 64 78 20 69 73 20 73 65 74 20 Cur->idx is set
17080 74 6f 20 6f 6e 65 20 6d 6f 72 65 20 74 68 61 6e to one more than
17081 0a 2a 2a 20 74 68 65 20 6c 61 72 67 65 73 74 20 .** the largest
17082 63 65 6c 6c 20 69 6e 64 65 78 2e 0a 2a 2f 0a 73 cell index..*/.s
17083 74 61 74 69 63 20 76 6f 69 64 20 6d 6f 76 65 54 tatic void moveT
17084 6f 50 61 72 65 6e 74 28 42 74 43 75 72 73 6f 72 oParent(BtCursor
17085 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73 65 72 *pCur){. asser
17086 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 t( cursorHoldsMu
17087 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 tex(pCur) );. a
17088 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 ssert( pCur->eSt
17089 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 ate==CURSOR_VALI
1708a 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 D );. assert( p
1708b 43 75 72 2d 3e 69 50 61 67 65 3e 30 20 29 3b 0a Cur->iPage>0 );.
1708c 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
1708d 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
1708e 67 65 5d 20 29 3b 0a 20 20 61 73 73 65 72 74 50 ge] );. assertP
1708f 61 72 65 6e 74 49 6e 64 65 78 28 0a 20 20 20 20 arentIndex(.
17090 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
17091 72 2d 3e 69 50 61 67 65 2d 31 5d 2c 20 0a 20 20 r->iPage-1], .
17092 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 pCur->aiIdx[pC
17093 75 72 2d 3e 69 50 61 67 65 2d 31 5d 2c 20 0a 20 ur->iPage-1], .
17094 20 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b pCur->apPage[
17095 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 70 67 pCur->iPage]->pg
17096 6e 6f 0a 20 20 29 3b 0a 20 20 72 65 6c 65 61 73 no. );. releas
17097 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50 61 ePage(pCur->apPa
17098 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 ge[pCur->iPage])
17099 3b 0a 20 20 70 43 75 72 2d 3e 69 50 61 67 65 2d ;. pCur->iPage-
1709a 2d 3b 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e -;. pCur->info.
1709b 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 nSize = 0;. pCu
1709c 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 r->validNKey = 0
1709d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 ;.}../*.** Move
1709e 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 70 6f the cursor to po
1709f 69 6e 74 20 74 6f 20 74 68 65 20 72 6f 6f 74 20 int to the root
170a0 70 61 67 65 20 6f 66 20 69 74 73 20 62 2d 74 72 page of its b-tr
170a1 65 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a ee structure..**
170a2 0a 2a 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 .** If the table
170a3 20 68 61 73 20 61 20 76 69 72 74 75 61 6c 20 72 has a virtual r
170a4 6f 6f 74 20 70 61 67 65 2c 20 74 68 65 6e 20 74 oot page, then t
170a5 68 65 20 63 75 72 73 6f 72 20 69 73 20 6d 6f 76 he cursor is mov
170a6 65 64 20 74 6f 20 70 6f 69 6e 74 0a 2a 2a 20 74 ed to point.** t
170a7 6f 20 74 68 65 20 76 69 72 74 75 61 6c 20 72 6f o the virtual ro
170a8 6f 74 20 70 61 67 65 20 69 6e 73 74 65 61 64 20 ot page instead
170a9 6f 66 20 74 68 65 20 61 63 74 75 61 6c 20 72 6f of the actual ro
170aa 6f 74 20 70 61 67 65 2e 20 41 20 74 61 62 6c 65 ot page. A table
170ab 20 68 61 73 20 61 0a 2a 2a 20 76 69 72 74 75 61 has a.** virtua
170ac 6c 20 72 6f 6f 74 20 70 61 67 65 20 77 68 65 6e l root page when
170ad 20 74 68 65 20 61 63 74 75 61 6c 20 72 6f 6f 74 the actual root
170ae 20 70 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 6e page contains n
170af 6f 20 63 65 6c 6c 73 20 61 6e 64 20 61 20 0a 2a o cells and a .*
170b0 2a 20 73 69 6e 67 6c 65 20 63 68 69 6c 64 20 70 * single child p
170b1 61 67 65 2e 20 54 68 69 73 20 63 61 6e 20 6f 6e age. This can on
170b2 6c 79 20 68 61 70 70 65 6e 20 77 69 74 68 20 74 ly happen with t
170b3 68 65 20 74 61 62 6c 65 20 72 6f 6f 74 65 64 20 he table rooted
170b4 61 74 20 70 61 67 65 20 31 2e 0a 2a 2a 0a 2a 2a at page 1..**.**
170b5 20 49 66 20 74 68 65 20 62 2d 74 72 65 65 20 73 If the b-tree s
170b6 74 72 75 63 74 75 72 65 20 69 73 20 65 6d 70 74 tructure is empt
170b7 79 2c 20 74 68 65 20 63 75 72 73 6f 72 20 73 74 y, the cursor st
170b8 61 74 65 20 69 73 20 73 65 74 20 74 6f 20 0a 2a ate is set to .*
170b9 2a 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 * CURSOR_INVALID
170ba 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 . Otherwise, the
170bb 20 63 75 72 73 6f 72 20 69 73 20 73 65 74 20 74 cursor is set t
170bc 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 66 o point to the f
170bd 69 72 73 74 0a 2a 2a 20 63 65 6c 6c 20 6c 6f 63 irst.** cell loc
170be 61 74 65 64 20 6f 6e 20 74 68 65 20 72 6f 6f 74 ated on the root
170bf 20 28 6f 72 20 76 69 72 74 75 61 6c 20 72 6f 6f (or virtual roo
170c0 74 29 20 70 61 67 65 20 61 6e 64 20 74 68 65 20 t) page and the
170c1 63 75 72 73 6f 72 20 73 74 61 74 65 0a 2a 2a 20 cursor state.**
170c2 69 73 20 73 65 74 20 74 6f 20 43 55 52 53 4f 52 is set to CURSOR
170c3 5f 56 41 4c 49 44 2e 0a 2a 2a 0a 2a 2a 20 49 66 _VALID..**.** If
170c4 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 this function r
170c5 65 74 75 72 6e 73 20 73 75 63 63 65 73 73 66 75 eturns successfu
170c6 6c 6c 79 2c 20 69 74 20 6d 61 79 20 62 65 20 61 lly, it may be a
170c7 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 0a ssumed that the.
170c8 2a 2a 20 70 61 67 65 2d 68 65 61 64 65 72 20 66 ** page-header f
170c9 6c 61 67 73 20 69 6e 64 69 63 61 74 65 20 74 68 lags indicate th
170ca 61 74 20 74 68 65 20 5b 76 69 72 74 75 61 6c 5d at the [virtual]
170cb 20 72 6f 6f 74 2d 70 61 67 65 20 69 73 20 74 68 root-page is th
170cc 65 20 65 78 70 65 63 74 65 64 20 0a 2a 2a 20 6b e expected .** k
170cd 69 6e 64 20 6f 66 20 62 2d 74 72 65 65 20 70 61 ind of b-tree pa
170ce 67 65 20 28 69 2e 65 2e 20 69 66 20 77 68 65 6e ge (i.e. if when
170cf 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 63 75 72 opening the cur
170d0 73 6f 72 20 74 68 65 20 63 61 6c 6c 65 72 20 64 sor the caller d
170d1 69 64 20 6e 6f 74 0a 2a 2a 20 73 70 65 63 69 66 id not.** specif
170d2 79 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 y a KeyInfo stru
170d3 63 74 75 72 65 20 74 68 65 20 66 6c 61 67 73 20 cture the flags
170d4 62 79 74 65 20 69 73 20 73 65 74 20 74 6f 20 30 byte is set to 0
170d5 78 30 35 20 6f 72 20 30 78 30 44 2c 0a 2a 2a 20 x05 or 0x0D,.**
170d6 69 6e 64 69 63 61 74 69 6e 67 20 61 20 74 61 62 indicating a tab
170d7 6c 65 20 62 2d 74 72 65 65 2c 20 6f 72 20 69 66 le b-tree, or if
170d8 20 74 68 65 20 63 61 6c 6c 65 72 20 64 69 64 20 the caller did
170d9 73 70 65 63 69 66 79 20 61 20 4b 65 79 49 6e 66 specify a KeyInf
170da 6f 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 o .** structure
170db 74 68 65 20 66 6c 61 67 73 20 62 79 74 65 20 69 the flags byte i
170dc 73 20 73 65 74 20 74 6f 20 30 78 30 32 20 6f 72 s set to 0x02 or
170dd 20 30 78 30 41 2c 20 69 6e 64 69 63 61 74 69 6e 0x0A, indicatin
170de 67 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 62 2d g an index.** b-
170df 74 72 65 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 tree)..*/.static
170e0 20 69 6e 74 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 int moveToRoot(
170e1 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b BtCursor *pCur){
170e2 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 52 6f 6f . MemPage *pRoo
170e3 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 t;. int rc = SQ
170e4 4c 49 54 45 5f 4f 4b 3b 0a 20 20 42 74 72 65 65 LITE_OK;. Btree
170e5 20 2a 70 20 3d 20 70 43 75 72 2d 3e 70 42 74 72 *p = pCur->pBtr
170e6 65 65 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a ee;. BtShared *
170e7 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 pBt = p->pBt;..
170e8 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 assert( cursorH
170e9 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 oldsMutex(pCur)
170ea 29 3b 0a 20 20 61 73 73 65 72 74 28 20 43 55 52 );. assert( CUR
170eb 53 4f 52 5f 49 4e 56 41 4c 49 44 20 3c 20 43 55 SOR_INVALID < CU
170ec 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b RSOR_REQUIRESEEK
170ed 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 43 55 );. assert( CU
170ee 52 53 4f 52 5f 56 41 4c 49 44 20 20 20 3c 20 43 RSOR_VALID < C
170ef 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 URSOR_REQUIRESEE
170f0 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 43 K );. assert( C
170f1 55 52 53 4f 52 5f 46 41 55 4c 54 20 20 20 3e 20 URSOR_FAULT >
170f2 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 CURSOR_REQUIRESE
170f3 45 4b 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 EK );. if( pCur
170f4 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52 53 4f 52 ->eState>=CURSOR
170f5 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 7b 0a _REQUIRESEEK ){.
170f6 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 if( pCur->eS
170f7 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46 41 55 tate==CURSOR_FAU
170f8 4c 54 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 LT ){. asse
170f9 72 74 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 rt( pCur->skipNe
170fa 78 74 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b xt!=SQLITE_OK );
170fb 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 43 . return pC
170fc 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b 0a 20 20 ur->skipNext;.
170fd 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 }. sqlite3B
170fe 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 treeClearCursor(
170ff 70 43 75 72 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 pCur);. }.. if
17100 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d 30 ( pCur->iPage>=0
17101 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 ){. int i;.
17102 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 70 for(i=1; i<=p
17103 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 2b 2b 29 Cur->iPage; i++)
17104 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 {. releaseP
17105 61 67 65 28 70 43 75 72 2d 3e 61 70 50 61 67 65 age(pCur->apPage
17106 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 [i]);. }.
17107 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20 30 3b pCur->iPage = 0;
17108 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 . }else{. rc
17109 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 = getAndInitPag
1710a 65 28 70 42 74 2c 20 70 43 75 72 2d 3e 70 67 6e e(pBt, pCur->pgn
1710b 6f 52 6f 6f 74 2c 20 26 70 43 75 72 2d 3e 61 70 oRoot, &pCur->ap
1710c 50 61 67 65 5b 30 5d 29 3b 0a 20 20 20 20 69 66 Page[0]);. if
1710d 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1710e 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 65 ){. pCur->e
1710f 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 State = CURSOR_I
17110 4e 56 41 4c 49 44 3b 0a 20 20 20 20 20 20 72 65 NVALID;. re
17111 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 turn rc;. }.
17112 20 20 20 70 43 75 72 2d 3e 69 50 61 67 65 20 3d pCur->iPage =
17113 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70 0;.. /* If p
17114 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 69 73 Cur->pKeyInfo is
17115 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 not NULL, then
17116 74 68 65 20 63 61 6c 6c 65 72 20 74 68 61 74 20 the caller that
17117 6f 70 65 6e 65 64 20 74 68 69 73 20 63 75 72 73 opened this curs
17118 6f 72 0a 20 20 20 20 2a 2a 20 65 78 70 65 63 74 or. ** expect
17119 65 64 20 74 6f 20 6f 70 65 6e 20 69 74 20 6f 6e ed to open it on
1711a 20 61 6e 20 69 6e 64 65 78 20 62 2d 74 72 65 65 an index b-tree
1711b 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 . Otherwise, if
1711c 70 4b 65 79 49 6e 66 6f 20 69 73 0a 20 20 20 20 pKeyInfo is.
1711d 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 20 63 61 6c ** NULL, the cal
1711e 6c 65 72 20 65 78 70 65 63 74 73 20 61 20 74 61 ler expects a ta
1711f 62 6c 65 20 62 2d 74 72 65 65 2e 20 49 66 20 74 ble b-tree. If t
17120 68 69 73 20 69 73 20 6e 6f 74 20 74 68 65 20 63 his is not the c
17121 61 73 65 2c 0a 20 20 20 20 2a 2a 20 72 65 74 75 ase,. ** retu
17122 72 6e 20 61 6e 20 53 51 4c 49 54 45 5f 43 4f 52 rn an SQLITE_COR
17123 52 55 50 54 20 65 72 72 6f 72 2e 20 20 2a 2f 0a RUPT error. */.
17124 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 assert( pCur
17125 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 ->apPage[0]->int
17126 4b 65 79 3d 3d 31 20 7c 7c 20 70 43 75 72 2d 3e Key==1 || pCur->
17127 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 apPage[0]->intKe
17128 79 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 y==0 );. if(
17129 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d (pCur->pKeyInfo=
1712a 3d 30 29 21 3d 70 43 75 72 2d 3e 61 70 50 61 67 =0)!=pCur->apPag
1712b 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a e[0]->intKey ){.
1712c 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
1712d 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
1712e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f ;. }. }.. /
1712f 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 74 68 * Assert that th
17130 65 20 72 6f 6f 74 20 70 61 67 65 20 69 73 20 6f e root page is o
17131 66 20 74 68 65 20 63 6f 72 72 65 63 74 20 74 79 f the correct ty
17132 70 65 2e 20 54 68 69 73 20 6d 75 73 74 20 62 65 pe. This must be
17133 20 74 68 65 0a 20 20 2a 2a 20 63 61 73 65 20 61 the. ** case a
17134 73 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 74 68 s the call to th
17135 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 is function that
17136 20 6c 6f 61 64 65 64 20 74 68 65 20 72 6f 6f 74 loaded the root
17137 2d 70 61 67 65 20 28 65 69 74 68 65 72 0a 20 20 -page (either.
17138 2a 2a 20 74 68 69 73 20 63 61 6c 6c 20 6f 72 20 ** this call or
17139 61 20 70 72 65 76 69 6f 75 73 20 69 6e 76 6f 63 a previous invoc
1713a 61 74 69 6f 6e 29 20 77 6f 75 6c 64 20 68 61 76 ation) would hav
1713b 65 20 64 65 74 65 63 74 65 64 20 63 6f 72 72 75 e detected corru
1713c 70 74 69 6f 6e 20 0a 20 20 2a 2a 20 69 66 20 74 ption . ** if t
1713d 68 65 20 61 73 73 75 6d 70 74 69 6f 6e 20 77 65 he assumption we
1713e 72 65 20 6e 6f 74 20 74 72 75 65 2c 20 61 6e 64 re not true, and
1713f 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 it is not possi
17140 62 6c 65 20 66 6f 72 20 74 68 65 20 66 6c 61 67 ble for the flag
17141 73 20 0a 20 20 2a 2a 20 62 79 74 65 20 74 6f 20 s . ** byte to
17142 68 61 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 have been modifi
17143 65 64 20 77 68 69 6c 65 20 74 68 69 73 20 63 75 ed while this cu
17144 72 73 6f 72 20 69 73 20 68 6f 6c 64 69 6e 67 20 rsor is holding
17145 61 20 72 65 66 65 72 65 6e 63 65 0a 20 20 2a 2a a reference. **
17146 20 74 6f 20 74 68 65 20 70 61 67 65 2e 20 20 2a to the page. *
17147 2f 0a 20 20 70 52 6f 6f 74 20 3d 20 70 43 75 72 /. pRoot = pCur
17148 2d 3e 61 70 50 61 67 65 5b 30 5d 3b 0a 20 20 61 ->apPage[0];. a
17149 73 73 65 72 74 28 20 70 52 6f 6f 74 2d 3e 70 67 ssert( pRoot->pg
1714a 6e 6f 3d 3d 70 43 75 72 2d 3e 70 67 6e 6f 52 6f no==pCur->pgnoRo
1714b 6f 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ot );. assert(
1714c 70 52 6f 6f 74 2d 3e 69 73 49 6e 69 74 20 26 26 pRoot->isInit &&
1714d 20 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f (pCur->pKeyInfo
1714e 3d 3d 30 29 3d 3d 70 52 6f 6f 74 2d 3e 69 6e 74 ==0)==pRoot->int
1714f 4b 65 79 20 29 3b 0a 0a 20 20 70 43 75 72 2d 3e Key );.. pCur->
17150 61 69 49 64 78 5b 30 5d 20 3d 20 30 3b 0a 20 20 aiIdx[0] = 0;.
17151 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 pCur->info.nSize
17152 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 61 74 = 0;. pCur->at
17153 4c 61 73 74 20 3d 20 30 3b 0a 20 20 70 43 75 72 Last = 0;. pCur
17154 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b ->validNKey = 0;
17155 0a 0a 20 20 69 66 28 20 70 52 6f 6f 74 2d 3e 6e .. if( pRoot->n
17156 43 65 6c 6c 3d 3d 30 20 26 26 20 21 70 52 6f 6f Cell==0 && !pRoo
17157 74 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 50 t->leaf ){. P
17158 67 6e 6f 20 73 75 62 70 61 67 65 3b 0a 20 20 20 gno subpage;.
17159 20 69 66 28 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f if( pRoot->pgno
1715a 21 3d 31 20 29 20 72 65 74 75 72 6e 20 53 51 4c !=1 ) return SQL
1715b 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
1715c 3b 0a 20 20 20 20 73 75 62 70 61 67 65 20 3d 20 ;. subpage =
1715d 67 65 74 34 62 79 74 65 28 26 70 52 6f 6f 74 2d get4byte(&pRoot-
1715e 3e 61 44 61 74 61 5b 70 52 6f 6f 74 2d 3e 68 64 >aData[pRoot->hd
1715f 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 rOffset+8]);.
17160 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 pCur->eState =
17161 43 55 52 53 4f 52 5f 56 41 4c 49 44 3b 0a 20 20 CURSOR_VALID;.
17162 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 rc = moveToChi
17163 6c 64 28 70 43 75 72 2c 20 73 75 62 70 61 67 65 ld(pCur, subpage
17164 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
17165 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 28 pCur->eState = (
17166 28 70 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 3e 30 29 (pRoot->nCell>0)
17167 3f 43 55 52 53 4f 52 5f 56 41 4c 49 44 3a 43 55 ?CURSOR_VALID:CU
17168 52 53 4f 52 5f 49 4e 56 41 4c 49 44 29 3b 0a 20 RSOR_INVALID);.
17169 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
1716a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 }../*.** Move th
1716b 65 20 63 75 72 73 6f 72 20 64 6f 77 6e 20 74 6f e cursor down to
1716c 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 6c the left-most l
1716d 65 61 66 20 65 6e 74 72 79 20 62 65 6e 65 61 74 eaf entry beneat
1716e 68 20 74 68 65 0a 2a 2a 20 65 6e 74 72 79 20 74 h the.** entry t
1716f 6f 20 77 68 69 63 68 20 69 74 20 69 73 20 63 75 o which it is cu
17170 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 rrently pointing
17171 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 65 66 74 ..**.** The left
17172 2d 6d 6f 73 74 20 6c 65 61 66 20 69 73 20 74 68 -most leaf is th
17173 65 20 6f 6e 65 20 77 69 74 68 20 74 68 65 20 73 e one with the s
17174 6d 61 6c 6c 65 73 74 20 6b 65 79 20 2d 20 74 68 mallest key - th
17175 65 20 66 69 72 73 74 0a 2a 2a 20 69 6e 20 61 73 e first.** in as
17176 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 0a 2a cending order..*
17177 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 /.static int mov
17178 65 54 6f 4c 65 66 74 6d 6f 73 74 28 42 74 43 75 eToLeftmost(BtCu
17179 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 50 rsor *pCur){. P
1717a 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 69 6e 74 20 gno pgno;. int
1717b 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1717c 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
1717d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 ;.. assert( cur
1717e 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 sorHoldsMutex(pC
1717f 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ur) );. assert(
17180 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
17181 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 URSOR_VALID );.
17182 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 while( rc==SQLI
17183 54 45 5f 4f 4b 20 26 26 20 21 28 70 50 61 67 65 TE_OK && !(pPage
17184 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b = pCur->apPage[
17185 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 2d 3e 6c pCur->iPage])->l
17186 65 61 66 20 29 7b 0a 20 20 20 20 61 73 73 65 72 eaf ){. asser
17187 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 t( pCur->aiIdx[p
17188 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 50 61 67 Cur->iPage]<pPag
17189 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 e->nCell );.
1718a 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 pgno = get4byte(
1718b 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 findCell(pPage,
1718c 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 pCur->aiIdx[pCur
1718d 2d 3e 69 50 61 67 65 5d 29 29 3b 0a 20 20 20 20 ->iPage]));.
1718e 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 rc = moveToChild
1718f 28 70 43 75 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 (pCur, pgno);.
17190 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
17191 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 ../*.** Move the
17192 20 63 75 72 73 6f 72 20 64 6f 77 6e 20 74 6f 20 cursor down to
17193 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 6c the right-most l
17194 65 61 66 20 65 6e 74 72 79 20 62 65 6e 65 61 74 eaf entry beneat
17195 68 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 74 6f h the.** page to
17196 20 77 68 69 63 68 20 69 74 20 69 73 20 63 75 72 which it is cur
17197 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 2e rently pointing.
17198 20 20 4e 6f 74 69 63 65 20 74 68 65 20 64 69 66 Notice the dif
17199 66 65 72 65 6e 63 65 0a 2a 2a 20 62 65 74 77 65 ference.** betwe
1719a 65 6e 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 en moveToLeftmos
1719b 74 28 29 20 61 6e 64 20 6d 6f 76 65 54 6f 52 69 t() and moveToRi
1719c 67 68 74 6d 6f 73 74 28 29 2e 20 20 6d 6f 76 65 ghtmost(). move
1719d 54 6f 4c 65 66 74 6d 6f 73 74 28 29 0a 2a 2a 20 ToLeftmost().**
1719e 66 69 6e 64 73 20 74 68 65 20 6c 65 66 74 2d 6d finds the left-m
1719f 6f 73 74 20 65 6e 74 72 79 20 62 65 6e 65 61 74 ost entry beneat
171a0 68 20 74 68 65 20 2a 65 6e 74 72 79 2a 20 77 68 h the *entry* wh
171a1 65 72 65 61 73 20 6d 6f 76 65 54 6f 52 69 67 68 ereas moveToRigh
171a2 74 6d 6f 73 74 28 29 0a 2a 2a 20 66 69 6e 64 73 tmost().** finds
171a3 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 the right-most
171a4 65 6e 74 72 79 20 62 65 6e 65 61 74 68 20 74 68 entry beneath th
171a5 65 20 2a 70 61 67 65 2a 2e 0a 2a 2a 0a 2a 2a 20 e *page*..**.**
171a6 54 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 65 The right-most e
171a7 6e 74 72 79 20 69 73 20 74 68 65 20 6f 6e 65 20 ntry is the one
171a8 77 69 74 68 20 74 68 65 20 6c 61 72 67 65 73 74 with the largest
171a9 20 6b 65 79 20 2d 20 74 68 65 20 6c 61 73 74 0a key - the last.
171aa 2a 2a 20 6b 65 79 20 69 6e 20 61 73 63 65 6e 64 ** key in ascend
171ab 69 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f 0a 73 74 ing order..*/.st
171ac 61 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f 52 atic int moveToR
171ad 69 67 68 74 6d 6f 73 74 28 42 74 43 75 72 73 6f ightmost(BtCurso
171ae 72 20 2a 70 43 75 72 29 7b 0a 20 20 50 67 6e 6f r *pCur){. Pgno
171af 20 70 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 63 20 pgno;. int rc
171b0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4d = SQLITE_OK;. M
171b1 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 emPage *pPage =
171b2 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 0;.. assert( cu
171b3 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
171b4 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 Cur) );. assert
171b5 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d ( pCur->eState==
171b6 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a CURSOR_VALID );.
171b7 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c while( rc==SQL
171b8 49 54 45 5f 4f 4b 20 26 26 20 21 28 70 50 61 67 ITE_OK && !(pPag
171b9 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 e = pCur->apPage
171ba 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 2d 3e [pCur->iPage])->
171bb 6c 65 61 66 20 29 7b 0a 20 20 20 20 70 67 6e 6f leaf ){. pgno
171bc 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 = get4byte(&pPa
171bd 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d ge->aData[pPage-
171be 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a >hdrOffset+8]);.
171bf 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b pCur->aiIdx[
171c0 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 70 pCur->iPage] = p
171c1 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 20 Page->nCell;.
171c2 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c rc = moveToChil
171c3 64 28 70 43 75 72 2c 20 70 67 6e 6f 29 3b 0a 20 d(pCur, pgno);.
171c4 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c }. if( rc==SQL
171c5 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 43 ITE_OK ){. pC
171c6 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e ur->aiIdx[pCur->
171c7 69 50 61 67 65 5d 20 3d 20 70 50 61 67 65 2d 3e iPage] = pPage->
171c8 6e 43 65 6c 6c 2d 31 3b 0a 20 20 20 20 70 43 75 nCell-1;. pCu
171c9 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 r->info.nSize =
171ca 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e 76 61 6c 0;. pCur->val
171cb 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 7d 0a idNKey = 0;. }.
171cc 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
171cd 2f 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 /* Move the curs
171ce 6f 72 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 or to the first
171cf 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61 62 entry in the tab
171d0 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 le. Return SQLI
171d1 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 63 63 TE_OK.** on succ
171d2 65 73 73 2e 20 20 53 65 74 20 2a 70 52 65 73 20 ess. Set *pRes
171d3 74 6f 20 30 20 69 66 20 74 68 65 20 63 75 72 73 to 0 if the curs
171d4 6f 72 20 61 63 74 75 61 6c 6c 79 20 70 6f 69 6e or actually poin
171d5 74 73 20 74 6f 20 73 6f 6d 65 74 68 69 6e 67 0a ts to something.
171d6 2a 2a 20 6f 72 20 73 65 74 20 2a 70 52 65 73 20 ** or set *pRes
171d7 74 6f 20 31 20 69 66 20 74 68 65 20 74 61 62 6c to 1 if the tabl
171d8 65 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2f 0a 53 e is empty..*/.S
171d9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
171da 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 69 t sqlite3BtreeFi
171db 72 73 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 rst(BtCursor *pC
171dc 75 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a ur, int *pRes){.
171dd 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 int rc;.. ass
171de 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 ert( cursorHolds
171df 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 Mutex(pCur) );.
171e0 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
171e1 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72 _mutex_held(pCur
171e2 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 ->pBtree->db->mu
171e3 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 6d tex) );. rc = m
171e4 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b oveToRoot(pCur);
171e5 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
171e6 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 E_OK ){. if(
171e7 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 pCur->eState==CU
171e8 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b 0a RSOR_INVALID ){.
171e9 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 assert( pC
171ea 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
171eb 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d >iPage]->nCell==
171ec 30 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 0 );. *pRes
171ed 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 63 20 3d = 1;. rc =
171ee 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
171ef 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 }else{. ass
171f0 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 ert( pCur->apPag
171f1 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e e[pCur->iPage]->
171f2 6e 43 65 6c 6c 3e 30 20 29 3b 0a 20 20 20 20 20 nCell>0 );.
171f3 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 *pRes = 0;.
171f4 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 rc = moveToLef
171f5 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 20 tmost(pCur);.
171f6 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
171f7 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65 20 74 rc;.}../* Move t
171f8 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 he cursor to the
171f9 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e 20 74 last entry in t
171fa 68 65 20 74 61 62 6c 65 2e 20 20 52 65 74 75 72 he table. Retur
171fb 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f n SQLITE_OK.** o
171fc 6e 20 73 75 63 63 65 73 73 2e 20 20 53 65 74 20 n success. Set
171fd 2a 70 52 65 73 20 74 6f 20 30 20 69 66 20 74 68 *pRes to 0 if th
171fe 65 20 63 75 72 73 6f 72 20 61 63 74 75 61 6c 6c e cursor actuall
171ff 79 20 70 6f 69 6e 74 73 20 74 6f 20 73 6f 6d 65 y points to some
17200 74 68 69 6e 67 0a 2a 2a 20 6f 72 20 73 65 74 20 thing.** or set
17201 2a 70 52 65 73 20 74 6f 20 31 20 69 66 20 74 68 *pRes to 1 if th
17202 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79 e table is empty
17203 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
17204 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
17205 74 72 65 65 4c 61 73 74 28 42 74 43 75 72 73 6f treeLast(BtCurso
17206 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 r *pCur, int *pR
17207 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 es){. int rc;.
17208 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f . assert( curso
17209 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 rHoldsMutex(pCur
1720a 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 ) );. assert( s
1720b 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
1720c 64 28 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e d(pCur->pBtree->
1720d 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 db->mutex) );..
1720e 20 2f 2a 20 49 66 20 74 68 65 20 63 75 72 73 6f /* If the curso
1720f 72 20 61 6c 72 65 61 64 79 20 70 6f 69 6e 74 73 r already points
17210 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74 to the last ent
17211 72 79 2c 20 74 68 69 73 20 69 73 20 61 20 6e 6f ry, this is a no
17212 2d 6f 70 2e 20 2a 2f 0a 20 20 69 66 28 20 43 55 -op. */. if( CU
17213 52 53 4f 52 5f 56 41 4c 49 44 3d 3d 70 43 75 72 RSOR_VALID==pCur
17214 2d 3e 65 53 74 61 74 65 20 26 26 20 70 43 75 72 ->eState && pCur
17215 2d 3e 61 74 4c 61 73 74 20 29 7b 0a 23 69 66 64 ->atLast ){.#ifd
17216 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
17217 20 20 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 /* This bloc
17218 6b 20 73 65 72 76 65 73 20 74 6f 20 61 73 73 65 k serves to asse
17219 72 74 28 29 20 74 68 61 74 20 74 68 65 20 63 75 rt() that the cu
1721a 72 73 6f 72 20 72 65 61 6c 6c 79 20 64 6f 65 73 rsor really does
1721b 20 70 6f 69 6e 74 20 0a 20 20 20 20 2a 2a 20 74 point . ** t
1721c 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 o the last entry
1721d 20 69 6e 20 74 68 65 20 62 2d 74 72 65 65 2e 20 in the b-tree.
1721e 2a 2f 0a 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 */. int ii;.
1721f 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c for(ii=0; ii<
17220 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 69 2b pCur->iPage; ii+
17221 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 +){. assert
17222 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 69 ( pCur->aiIdx[ii
17223 5d 3d 3d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b ]==pCur->apPage[
17224 69 69 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 ii]->nCell );.
17225 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
17226 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 pCur->aiIdx[pCur
17227 2d 3e 69 50 61 67 65 5d 3d 3d 70 43 75 72 2d 3e ->iPage]==pCur->
17228 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
17229 67 65 5d 2d 3e 6e 43 65 6c 6c 2d 31 20 29 3b 0a ge]->nCell-1 );.
1722a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 assert( pCur
1722b 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
1722c 50 61 67 65 5d 2d 3e 6c 65 61 66 20 29 3b 0a 23 Page]->leaf );.#
1722d 65 6e 64 69 66 0a 20 20 20 20 72 65 74 75 72 6e endif. return
1722e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
1722f 0a 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 6f . rc = moveToRo
17230 6f 74 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 ot(pCur);. if(
17231 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
17232 0a 20 20 20 20 69 66 28 20 43 55 52 53 4f 52 5f . if( CURSOR_
17233 49 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 INVALID==pCur->e
17234 53 74 61 74 65 20 29 7b 0a 20 20 20 20 20 20 61 State ){. a
17235 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 ssert( pCur->apP
17236 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
17237 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20 ->nCell==0 );.
17238 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 *pRes = 1;.
17239 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1723a 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 assert( pCur->eS
1723b 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c tate==CURSOR_VAL
1723c 49 44 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 ID );. *pRe
1723d 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 s = 0;. rc
1723e 3d 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 = moveToRightmos
1723f 74 28 70 43 75 72 29 3b 0a 20 20 20 20 20 20 70 t(pCur);. p
17240 43 75 72 2d 3e 61 74 4c 61 73 74 20 3d 20 72 63 Cur->atLast = rc
17241 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 31 3a 30 ==SQLITE_OK ?1:0
17242 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
17243 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d turn rc;.}../* M
17244 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 73 ove the cursor s
17245 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 o that it points
17246 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 6e 65 61 to an entry nea
17247 72 20 74 68 65 20 6b 65 79 20 0a 2a 2a 20 73 70 r the key .** sp
17248 65 63 69 66 69 65 64 20 62 79 20 70 49 64 78 4b ecified by pIdxK
17249 65 79 20 6f 72 20 69 6e 74 4b 65 79 2e 20 20 20 ey or intKey.
1724a 52 65 74 75 72 6e 20 61 20 73 75 63 63 65 73 73 Return a success
1724b 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 code..**.** For
1724c 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 73 2c 20 INTKEY tables,
1724d 74 68 65 20 69 6e 74 4b 65 79 20 70 61 72 61 6d the intKey param
1724e 65 74 65 72 20 69 73 20 75 73 65 64 2e 20 20 70 eter is used. p
1724f 49 64 78 4b 65 79 20 0a 2a 2a 20 6d 75 73 74 20 IdxKey .** must
17250 62 65 20 4e 55 4c 4c 2e 20 20 46 6f 72 20 69 6e be NULL. For in
17251 64 65 78 20 74 61 62 6c 65 73 2c 20 70 49 64 78 dex tables, pIdx
17252 4b 65 79 20 69 73 20 75 73 65 64 20 61 6e 64 20 Key is used and
17253 69 6e 74 4b 65 79 0a 2a 2a 20 69 73 20 69 67 6e intKey.** is ign
17254 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 ored..**.** If a
17255 6e 20 65 78 61 63 74 20 6d 61 74 63 68 20 69 73 n exact match is
17256 20 6e 6f 74 20 66 6f 75 6e 64 2c 20 74 68 65 6e not found, then
17257 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 61 the cursor is a
17258 6c 77 61 79 73 0a 2a 2a 20 6c 65 66 74 20 70 6f lways.** left po
17259 69 6e 74 69 6e 67 20 61 74 20 61 20 6c 65 61 66 inting at a leaf
1725a 20 70 61 67 65 20 77 68 69 63 68 20 77 6f 75 6c page which woul
1725b 64 20 68 6f 6c 64 20 74 68 65 20 65 6e 74 72 79 d hold the entry
1725c 20 69 66 20 69 74 0a 2a 2a 20 77 65 72 65 20 70 if it.** were p
1725d 72 65 73 65 6e 74 2e 20 20 54 68 65 20 63 75 72 resent. The cur
1725e 73 6f 72 20 6d 69 67 68 74 20 70 6f 69 6e 74 20 sor might point
1725f 74 6f 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 to an entry that
17260 20 63 6f 6d 65 73 0a 2a 2a 20 62 65 66 6f 72 65 comes.** before
17261 20 6f 72 20 61 66 74 65 72 20 74 68 65 20 6b 65 or after the ke
17262 79 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 74 65 y..**.** An inte
17263 67 65 72 20 69 73 20 77 72 69 74 74 65 6e 20 69 ger is written i
17264 6e 74 6f 20 2a 70 52 65 73 20 77 68 69 63 68 20 nto *pRes which
17265 69 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 is the result of
17266 0a 2a 2a 20 63 6f 6d 70 61 72 69 6e 67 20 74 68 .** comparing th
17267 65 20 6b 65 79 20 77 69 74 68 20 74 68 65 20 65 e key with the e
17268 6e 74 72 79 20 74 6f 20 77 68 69 63 68 20 74 68 ntry to which th
17269 65 20 63 75 72 73 6f 72 20 69 73 20 0a 2a 2a 20 e cursor is .**
1726a 70 6f 69 6e 74 69 6e 67 2e 20 20 54 68 65 20 6d pointing. The m
1726b 65 61 6e 69 6e 67 20 6f 66 20 74 68 65 20 69 6e eaning of the in
1726c 74 65 67 65 72 20 77 72 69 74 74 65 6e 20 69 6e teger written in
1726d 74 6f 0a 2a 2a 20 2a 70 52 65 73 20 69 73 20 61 to.** *pRes is a
1726e 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a s follows:.**.**
1726f 20 20 20 20 20 2a 70 52 65 73 3c 30 20 20 20 20 *pRes<0
17270 20 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 The cursor is
17271 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 left pointing at
17272 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 0a 2a an entry that.*
17273 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
17274 20 20 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 is smaller th
17275 61 6e 20 69 6e 74 4b 65 79 2f 70 49 64 78 4b 65 an intKey/pIdxKe
17276 79 20 6f 72 20 69 66 20 74 68 65 20 74 61 62 6c y or if the tabl
17277 65 20 69 73 20 65 6d 70 74 79 0a 2a 2a 20 20 20 e is empty.**
17278 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 a
17279 6e 64 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 nd the cursor is
1727a 20 74 68 65 72 65 66 6f 72 65 20 6c 65 66 74 20 therefore left
1727b 70 6f 69 6e 74 20 74 6f 20 6e 6f 74 68 69 6e 67 point to nothing
1727c 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 70 52 65 ..**.** *pRe
1727d 73 3d 3d 30 20 20 20 20 20 54 68 65 20 63 75 72 s==0 The cur
1727e 73 6f 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e sor is left poin
1727f 74 69 6e 67 20 61 74 20 61 6e 20 65 6e 74 72 79 ting at an entry
17280 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 that.**
17281 20 20 20 20 20 20 20 20 20 20 65 78 61 63 74 6c exactl
17282 79 20 6d 61 74 63 68 65 73 20 69 6e 74 4b 65 79 y matches intKey
17283 2f 70 49 64 78 4b 65 79 2e 0a 2a 2a 0a 2a 2a 20 /pIdxKey..**.**
17284 20 20 20 20 2a 70 52 65 73 3e 30 20 20 20 20 20 *pRes>0
17285 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c The cursor is l
17286 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 eft pointing at
17287 61 6e 20 65 6e 74 72 79 20 74 68 61 74 0a 2a 2a an entry that.**
17288 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17289 20 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e is larger than
1728a 20 69 6e 74 4b 65 79 2f 70 49 64 78 4b 65 79 2e intKey/pIdxKey.
1728b 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 .**.*/.SQLITE_PR
1728c 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1728d 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 3BtreeMovetoUnpa
1728e 63 6b 65 64 28 0a 20 20 42 74 43 75 72 73 6f 72 cked(. BtCursor
1728f 20 2a 70 43 75 72 2c 20 20 20 20 20 20 20 20 20 *pCur,
17290 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 74 /* The cursor t
17291 6f 20 62 65 20 6d 6f 76 65 64 20 2a 2f 0a 20 20 o be moved */.
17292 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a UnpackedRecord *
17293 70 49 64 78 4b 65 79 2c 20 2f 2a 20 55 6e 70 61 pIdxKey, /* Unpa
17294 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 20 2a cked index key *
17295 2f 0a 20 20 69 36 34 20 69 6e 74 4b 65 79 2c 20 /. i64 intKey,
17296 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
17297 54 68 65 20 74 61 62 6c 65 20 6b 65 79 20 2a 2f The table key */
17298 0a 20 20 69 6e 74 20 62 69 61 73 52 69 67 68 74 . int biasRight
17299 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 , /* I
1729a 66 20 74 72 75 65 2c 20 62 69 61 73 20 74 68 65 f true, bias the
1729b 20 73 65 61 72 63 68 20 74 6f 20 74 68 65 20 68 search to the h
1729c 69 67 68 20 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 igh end */. int
1729d 20 2a 70 52 65 73 20 20 20 20 20 20 20 20 20 20 *pRes
1729e 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 73 /* Write s
1729f 65 61 72 63 68 20 72 65 73 75 6c 74 73 20 68 65 earch results he
172a0 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 re */.){. int r
172a1 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 c;.. assert( cu
172a2 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
172a3 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 Cur) );. assert
172a4 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
172a5 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 65 held(pCur->pBtre
172a6 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b e->db->mutex) );
172a7 0a 20 20 61 73 73 65 72 74 28 20 70 52 65 73 20 . assert( pRes
172a8 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 49 );. assert( (pI
172a9 64 78 4b 65 79 3d 3d 30 29 3d 3d 28 70 43 75 72 dxKey==0)==(pCur
172aa 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 29 20 29 ->pKeyInfo==0) )
172ab 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 ;.. /* If the c
172ac 75 72 73 6f 72 20 69 73 20 61 6c 72 65 61 64 79 ursor is already
172ad 20 70 6f 73 69 74 69 6f 6e 65 64 20 61 74 20 74 positioned at t
172ae 68 65 20 70 6f 69 6e 74 20 77 65 20 61 72 65 20 he point we are
172af 74 72 79 69 6e 67 0a 20 20 2a 2a 20 74 6f 20 6d trying. ** to m
172b0 6f 76 65 20 74 6f 2c 20 74 68 65 6e 20 6a 75 73 ove to, then jus
172b1 74 20 72 65 74 75 72 6e 20 77 69 74 68 6f 75 74 t return without
172b2 20 64 6f 69 6e 67 20 61 6e 79 20 77 6f 72 6b 20 doing any work
172b3 2a 2f 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 */. if( pCur->e
172b4 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 State==CURSOR_VA
172b5 4c 49 44 20 26 26 20 70 43 75 72 2d 3e 76 61 6c LID && pCur->val
172b6 69 64 4e 4b 65 79 20 0a 20 20 20 26 26 20 70 43 idNKey . && pC
172b7 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 ur->apPage[0]->i
172b8 6e 74 4b 65 79 20 0a 20 20 29 7b 0a 20 20 20 20 ntKey . ){.
172b9 69 66 28 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e if( pCur->info.n
172ba 4b 65 79 3d 3d 69 6e 74 4b 65 79 20 29 7b 0a 20 Key==intKey ){.
172bb 20 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a *pRes = 0;.
172bc 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
172bd 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 ITE_OK;. }.
172be 20 20 69 66 28 20 70 43 75 72 2d 3e 61 74 4c 61 if( pCur->atLa
172bf 73 74 20 26 26 20 70 43 75 72 2d 3e 69 6e 66 6f st && pCur->info
172c0 2e 6e 4b 65 79 3c 69 6e 74 4b 65 79 20 29 7b 0a .nKey<intKey ){.
172c1 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 2d 31 *pRes = -1
172c2 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 ;. return S
172c3 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a QLITE_OK;. }.
172c4 20 20 7d 0a 0a 20 20 72 63 20 3d 20 6d 6f 76 65 }.. rc = move
172c5 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 ToRoot(pCur);.
172c6 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 if( rc ){. re
172c7 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 turn rc;. }. a
172c8 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 ssert( pCur->apP
172c9 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
172ca 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 );. assert( pC
172cb 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
172cc 3e 69 50 61 67 65 5d 2d 3e 69 73 49 6e 69 74 20 >iPage]->isInit
172cd 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 );. assert( pCu
172ce 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
172cf 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3e 30 20 iPage]->nCell>0
172d0 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d || pCur->eState=
172d1 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 =CURSOR_INVALID
172d2 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 );. if( pCur->e
172d3 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e State==CURSOR_IN
172d4 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 2a 70 52 VALID ){. *pR
172d5 65 73 20 3d 20 2d 31 3b 0a 20 20 20 20 61 73 73 es = -1;. ass
172d6 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 ert( pCur->apPag
172d7 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e e[pCur->iPage]->
172d8 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 nCell==0 );.
172d9 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
172da 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
172db 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d pCur->apPage[0]-
172dc 3e 69 6e 74 4b 65 79 20 7c 7c 20 70 49 64 78 4b >intKey || pIdxK
172dd 65 79 20 29 3b 0a 20 20 66 6f 72 28 3b 3b 29 7b ey );. for(;;){
172de 0a 20 20 20 20 69 6e 74 20 6c 77 72 2c 20 75 70 . int lwr, up
172df 72 3b 0a 20 20 20 20 50 67 6e 6f 20 63 68 6c 64 r;. Pgno chld
172e0 50 67 3b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 Pg;. MemPage
172e1 2a 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 *pPage = pCur->a
172e2 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
172e3 65 5d 3b 0a 20 20 20 20 69 6e 74 20 63 3b 0a 0a e];. int c;..
172e4 20 20 20 20 2f 2a 20 70 50 61 67 65 2d 3e 6e 43 /* pPage->nC
172e5 65 6c 6c 20 6d 75 73 74 20 62 65 20 67 72 65 61 ell must be grea
172e6 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2e 20 49 ter than zero. I
172e7 66 20 74 68 69 73 20 69 73 20 74 68 65 20 72 6f f this is the ro
172e8 6f 74 2d 70 61 67 65 0a 20 20 20 20 2a 2a 20 74 ot-page. ** t
172e9 68 65 20 63 75 72 73 6f 72 20 77 6f 75 6c 64 20 he cursor would
172ea 68 61 76 65 20 62 65 65 6e 20 49 4e 56 41 4c 49 have been INVALI
172eb 44 20 61 62 6f 76 65 20 61 6e 64 20 74 68 69 73 D above and this
172ec 20 66 6f 72 28 3b 3b 29 20 6c 6f 6f 70 0a 20 20 for(;;) loop.
172ed 20 20 2a 2a 20 6e 6f 74 20 72 75 6e 2e 20 49 66 ** not run. If
172ee 20 74 68 69 73 20 69 73 20 6e 6f 74 20 74 68 65 this is not the
172ef 20 72 6f 6f 74 2d 70 61 67 65 2c 20 74 68 65 6e root-page, then
172f0 20 74 68 65 20 6d 6f 76 65 54 6f 43 68 69 6c 64 the moveToChild
172f1 28 29 20 72 6f 75 74 69 6e 65 0a 20 20 20 20 2a () routine. *
172f2 2a 20 77 6f 75 6c 64 20 68 61 76 65 20 61 6c 72 * would have alr
172f3 65 61 64 79 20 64 65 74 65 63 74 65 64 20 64 62 eady detected db
172f4 20 63 6f 72 72 75 70 74 69 6f 6e 2e 20 53 69 6d corruption. Sim
172f5 69 6c 61 72 6c 79 2c 20 70 50 61 67 65 20 6d 75 ilarly, pPage mu
172f6 73 74 0a 20 20 20 20 2a 2a 20 62 65 20 74 68 65 st. ** be the
172f7 20 72 69 67 68 74 20 6b 69 6e 64 20 28 69 6e 64 right kind (ind
172f8 65 78 20 6f 72 20 74 61 62 6c 65 29 20 6f 66 20 ex or table) of
172f9 62 2d 74 72 65 65 20 70 61 67 65 2e 20 4f 74 68 b-tree page. Oth
172fa 65 72 77 69 73 65 0a 20 20 20 20 2a 2a 20 61 20 erwise. ** a
172fb 6d 6f 76 65 54 6f 43 68 69 6c 64 28 29 20 6f 72 moveToChild() or
172fc 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 29 20 63 61 moveToRoot() ca
172fd 6c 6c 20 77 6f 75 6c 64 20 68 61 76 65 20 64 65 ll would have de
172fe 74 65 63 74 65 64 20 63 6f 72 72 75 70 74 69 6f tected corruptio
172ff 6e 2e 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 n. */. asser
17300 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e t( pPage->nCell>
17301 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 0 );. assert(
17302 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 3d 3d pPage->intKey==
17303 28 70 49 64 78 4b 65 79 3d 3d 30 29 20 29 3b 0a (pIdxKey==0) );.
17304 20 20 20 20 6c 77 72 20 3d 20 30 3b 0a 20 20 20 lwr = 0;.
17305 20 75 70 72 20 3d 20 70 50 61 67 65 2d 3e 6e 43 upr = pPage->nC
17306 65 6c 6c 2d 31 3b 0a 20 20 20 20 69 66 28 20 62 ell-1;. if( b
17307 69 61 73 52 69 67 68 74 20 29 7b 0a 20 20 20 20 iasRight ){.
17308 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 pCur->aiIdx[pC
17309 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 28 75 31 ur->iPage] = (u1
1730a 36 29 75 70 72 3b 0a 20 20 20 20 7d 65 6c 73 65 6)upr;. }else
1730b 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 69 {. pCur->ai
1730c 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d Idx[pCur->iPage]
1730d 20 3d 20 28 75 31 36 29 28 28 75 70 72 2b 6c 77 = (u16)((upr+lw
1730e 72 29 2f 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20 r)/2);. }.
1730f 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 20 20 20 20 for(;;){.
17310 69 6e 74 20 69 64 78 20 3d 20 70 43 75 72 2d 3e int idx = pCur->
17311 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 aiIdx[pCur->iPag
17312 65 5d 3b 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 e]; /* Index of
17313 63 75 72 72 65 6e 74 20 63 65 6c 6c 20 69 6e 20 current cell in
17314 70 50 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 75 pPage */. u
17315 38 20 2a 70 43 65 6c 6c 3b 20 20 20 20 20 20 20 8 *pCell;
17316 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17317 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
17318 20 63 75 72 72 65 6e 74 20 63 65 6c 6c 20 69 6e current cell in
17319 20 70 50 61 67 65 20 2a 2f 0a 0a 20 20 20 20 20 pPage */..
1731a 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a pCur->info.nSiz
1731b 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 43 65 e = 0;. pCe
1731c 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 ll = findCell(pP
1731d 61 67 65 2c 20 69 64 78 29 20 2b 20 70 50 61 67 age, idx) + pPag
1731e 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 3b e->childPtrSize;
1731f 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 . if( pPage
17320 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 ->intKey ){.
17321 20 20 20 20 69 36 34 20 6e 43 65 6c 6c 4b 65 79 i64 nCellKey
17322 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 ;. if( pP
17323 61 67 65 2d 3e 68 61 73 44 61 74 61 20 29 7b 0a age->hasData ){.
17324 20 20 20 20 20 20 20 20 20 20 75 33 32 20 64 75 u32 du
17325 6d 6d 79 3b 0a 20 20 20 20 20 20 20 20 20 20 70 mmy;. p
17326 43 65 6c 6c 20 2b 3d 20 67 65 74 56 61 72 69 6e Cell += getVarin
17327 74 33 32 28 70 43 65 6c 6c 2c 20 64 75 6d 6d 79 t32(pCell, dummy
17328 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
17329 20 20 20 20 20 67 65 74 56 61 72 69 6e 74 28 70 getVarint(p
1732a 43 65 6c 6c 2c 20 28 75 36 34 2a 29 26 6e 43 65 Cell, (u64*)&nCe
1732b 6c 6c 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 llKey);.
1732c 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3d 3d 69 6e if( nCellKey==in
1732d 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 tKey ){.
1732e 20 20 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 c = 0;.
1732f 20 7d 65 6c 73 65 20 69 66 28 20 6e 43 65 6c 6c }else if( nCell
17330 4b 65 79 3c 69 6e 74 4b 65 79 20 29 7b 0a 20 20 Key<intKey ){.
17331 20 20 20 20 20 20 20 20 63 20 3d 20 2d 31 3b 0a c = -1;.
17332 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
17333 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 assert(
17334 20 6e 43 65 6c 6c 4b 65 79 3e 69 6e 74 4b 65 79 nCellKey>intKey
17335 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 63 20 );. c
17336 3d 20 2b 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a = +1;. }.
17337 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 76 61 pCur->va
17338 6c 69 64 4e 4b 65 79 20 3d 20 31 3b 0a 20 20 20 lidNKey = 1;.
17339 20 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e pCur->info.
1733a 6e 4b 65 79 20 3d 20 6e 43 65 6c 6c 4b 65 79 3b nKey = nCellKey;
1733b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
1733c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 78 /* The max
1733d 69 6d 75 6d 20 73 75 70 70 6f 72 74 65 64 20 70 imum supported p
1733e 61 67 65 2d 73 69 7a 65 20 69 73 20 33 32 37 36 age-size is 3276
1733f 38 20 62 79 74 65 73 2e 20 54 68 69 73 20 6d 65 8 bytes. This me
17340 61 6e 73 20 74 68 61 74 0a 20 20 20 20 20 20 20 ans that.
17341 20 2a 2a 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 ** the maximum
17342 6e 75 6d 62 65 72 20 6f 66 20 72 65 63 6f 72 64 number of record
17343 20 62 79 74 65 73 20 73 74 6f 72 65 64 20 6f 6e bytes stored on
17344 20 61 6e 20 69 6e 64 65 78 20 42 2d 54 72 65 65 an index B-Tree
17345 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67 65 . ** page
17346 20 69 73 20 61 74 20 6d 6f 73 74 20 38 31 39 38 is at most 8198
17347 20 62 79 74 65 73 2c 20 77 68 69 63 68 20 6d 61 bytes, which ma
17348 79 20 62 65 20 73 74 6f 72 65 64 20 61 73 20 61 y be stored as a
17349 20 32 2d 62 79 74 65 0a 20 20 20 20 20 20 20 20 2-byte.
1734a 2a 2a 20 76 61 72 69 6e 74 2e 20 54 68 69 73 20 ** varint. This
1734b 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 75 information is u
1734c 73 65 64 20 74 6f 20 61 74 74 65 6d 70 74 20 74 sed to attempt t
1734d 6f 20 61 76 6f 69 64 20 70 61 72 73 69 6e 67 20 o avoid parsing
1734e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 . ** the
1734f 65 6e 74 69 72 65 20 63 65 6c 6c 20 62 79 20 63 entire cell by c
17350 68 65 63 6b 69 6e 67 20 66 6f 72 20 74 68 65 20 hecking for the
17351 63 61 73 65 73 20 77 68 65 72 65 20 74 68 65 20 cases where the
17352 72 65 63 6f 72 64 20 69 73 20 0a 20 20 20 20 20 record is .
17353 20 20 20 2a 2a 20 73 74 6f 72 65 64 20 65 6e 74 ** stored ent
17354 69 72 65 6c 79 20 77 69 74 68 69 6e 20 74 68 65 irely within the
17355 20 62 2d 74 72 65 65 20 70 61 67 65 20 62 79 20 b-tree page by
17356 69 6e 73 70 65 63 74 69 6e 67 20 74 68 65 20 66 inspecting the f
17357 69 72 73 74 20 0a 20 20 20 20 20 20 20 20 2a 2a irst . **
17358 20 32 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 2 bytes of the
17359 63 65 6c 6c 2e 0a 20 20 20 20 20 20 20 20 2a 2f cell.. */
1735a 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 43 65 . int nCe
1735b 6c 6c 20 3d 20 70 43 65 6c 6c 5b 30 5d 3b 0a 20 ll = pCell[0];.
1735c 20 20 20 20 20 20 20 69 66 28 20 21 28 6e 43 65 if( !(nCe
1735d 6c 6c 20 26 20 30 78 38 30 29 20 26 26 20 6e 43 ll & 0x80) && nC
1735e 65 6c 6c 3c 3d 70 50 61 67 65 2d 3e 6d 61 78 4c ell<=pPage->maxL
1735f 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 ocal ){.
17360 20 20 2f 2a 20 54 68 69 73 20 62 72 61 6e 63 68 /* This branch
17361 20 72 75 6e 73 20 69 66 20 74 68 65 20 72 65 63 runs if the rec
17362 6f 72 64 2d 73 69 7a 65 20 66 69 65 6c 64 20 6f ord-size field o
17363 66 20 74 68 65 20 63 65 6c 6c 20 69 73 20 61 0a f the cell is a.
17364 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73 69 6e ** sin
17365 67 6c 65 20 62 79 74 65 20 76 61 72 69 6e 74 20 gle byte varint
17366 61 6e 64 20 74 68 65 20 72 65 63 6f 72 64 20 66 and the record f
17367 69 74 73 20 65 6e 74 69 72 65 6c 79 20 6f 6e 20 its entirely on
17368 74 68 65 20 6d 61 69 6e 0a 20 20 20 20 20 20 20 the main.
17369 20 20 20 2a 2a 20 62 2d 74 72 65 65 20 70 61 67 ** b-tree pag
1736a 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 e. */.
1736b 20 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 c = sqlite3Vdbe
1736c 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 6e 43 RecordCompare(nC
1736d 65 6c 6c 2c 20 28 76 6f 69 64 2a 29 26 70 43 65 ell, (void*)&pCe
1736e 6c 6c 5b 31 5d 2c 20 70 49 64 78 4b 65 79 29 3b ll[1], pIdxKey);
1736f 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 . }else i
17370 66 28 20 21 28 70 43 65 6c 6c 5b 31 5d 20 26 20 f( !(pCell[1] &
17371 30 78 38 30 29 20 0a 20 20 20 20 20 20 20 20 20 0x80) .
17372 20 26 26 20 28 6e 43 65 6c 6c 20 3d 20 28 28 6e && (nCell = ((n
17373 43 65 6c 6c 26 30 78 37 66 29 3c 3c 37 29 20 2b Cell&0x7f)<<7) +
17374 20 70 43 65 6c 6c 5b 31 5d 29 3c 3d 70 50 61 67 pCell[1])<=pPag
17375 65 2d 3e 6d 61 78 4c 6f 63 61 6c 0a 20 20 20 20 e->maxLocal.
17376 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ){.
17377 20 2f 2a 20 54 68 65 20 72 65 63 6f 72 64 2d 73 /* The record-s
17378 69 7a 65 20 66 69 65 6c 64 20 69 73 20 61 20 32 ize field is a 2
17379 20 62 79 74 65 20 76 61 72 69 6e 74 20 61 6e 64 byte varint and
1737a 20 74 68 65 20 72 65 63 6f 72 64 20 0a 20 20 20 the record .
1737b 20 20 20 20 20 20 20 2a 2a 20 66 69 74 73 20 65 ** fits e
1737c 6e 74 69 72 65 6c 79 20 6f 6e 20 74 68 65 20 6d ntirely on the m
1737d 61 69 6e 20 62 2d 74 72 65 65 20 70 61 67 65 2e ain b-tree page.
1737e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 63 */. c
1737f 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 = sqlite3VdbeRe
17380 63 6f 72 64 43 6f 6d 70 61 72 65 28 6e 43 65 6c cordCompare(nCel
17381 6c 2c 20 28 76 6f 69 64 2a 29 26 70 43 65 6c 6c l, (void*)&pCell
17382 5b 32 5d 2c 20 70 49 64 78 4b 65 79 29 3b 0a 20 [2], pIdxKey);.
17383 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
17384 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 /* The r
17385 65 63 6f 72 64 20 66 6c 6f 77 73 20 6f 76 65 72 ecord flows over
17386 20 6f 6e 74 6f 20 6f 6e 65 20 6f 72 20 6d 6f 72 onto one or mor
17387 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 e overflow pages
17388 2e 20 49 6e 0a 20 20 20 20 20 20 20 20 20 20 2a . In. *
17389 2a 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 * this case the
1738a 77 68 6f 6c 65 20 63 65 6c 6c 20 6e 65 65 64 73 whole cell needs
1738b 20 74 6f 20 62 65 20 70 61 72 73 65 64 2c 20 61 to be parsed, a
1738c 20 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 buffer allocate
1738d 64 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 d. ** a
1738e 6e 64 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 nd accessPayload
1738f 28 29 20 75 73 65 64 20 74 6f 20 72 65 74 72 69 () used to retri
17390 65 76 65 20 74 68 65 20 72 65 63 6f 72 64 20 69 eve the record i
17391 6e 74 6f 20 74 68 65 0a 20 20 20 20 20 20 20 20 nto the.
17392 20 20 2a 2a 20 62 75 66 66 65 72 20 62 65 66 6f ** buffer befo
17393 72 65 20 56 64 62 65 52 65 63 6f 72 64 43 6f 6d re VdbeRecordCom
17394 70 61 72 65 28 29 20 63 61 6e 20 62 65 20 63 61 pare() can be ca
17395 6c 6c 65 64 2e 20 2a 2f 0a 20 20 20 20 20 20 20 lled. */.
17396 20 20 20 76 6f 69 64 20 2a 70 43 65 6c 6c 4b 65 void *pCellKe
17397 79 3b 0a 20 20 20 20 20 20 20 20 20 20 75 38 20 y;. u8
17398 2a 20 63 6f 6e 73 74 20 70 43 65 6c 6c 42 6f 64 * const pCellBod
17399 79 20 3d 20 70 43 65 6c 6c 20 2d 20 70 50 61 67 y = pCell - pPag
1739a 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 3b e->childPtrSize;
1739b 0a 20 20 20 20 20 20 20 20 20 20 62 74 72 65 65 . btree
1739c 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 ParseCellPtr(pPa
1739d 67 65 2c 20 70 43 65 6c 6c 42 6f 64 79 2c 20 26 ge, pCellBody, &
1739e 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 0a 20 20 20 pCur->info);.
1739f 20 20 20 20 20 20 20 6e 43 65 6c 6c 20 3d 20 28 nCell = (
173a0 69 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e int)pCur->info.n
173a1 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 20 20 70 Key;. p
173a2 43 65 6c 6c 4b 65 79 20 3d 20 73 71 6c 69 74 65 CellKey = sqlite
173a3 33 4d 61 6c 6c 6f 63 28 20 6e 43 65 6c 6c 20 29 3Malloc( nCell )
173a4 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
173a5 70 43 65 6c 6c 4b 65 79 3d 3d 30 20 29 7b 0a 20 pCellKey==0 ){.
173a6 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 rc =
173a7 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
173a8 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d goto m
173a9 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 oveto_finish;.
173aa 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
173ab 20 20 20 20 72 63 20 3d 20 61 63 63 65 73 73 50 rc = accessP
173ac 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 30 2c 20 ayload(pCur, 0,
173ad 6e 43 65 6c 6c 2c 20 28 75 6e 73 69 67 6e 65 64 nCell, (unsigned
173ae 20 63 68 61 72 2a 29 70 43 65 6c 6c 4b 65 79 2c char*)pCellKey,
173af 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 0);. i
173b0 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 f( rc ){.
173b1 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 sqlite3_fre
173b2 65 28 70 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 20 e(pCellKey);.
173b3 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 6f goto mo
173b4 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 20 veto_finish;.
173b5 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
173b6 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 56 64 c = sqlite3Vd
173b7 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 beRecordCompare(
173b8 6e 43 65 6c 6c 2c 20 70 43 65 6c 6c 4b 65 79 2c nCell, pCellKey,
173b9 20 70 49 64 78 4b 65 79 29 3b 0a 20 20 20 20 20 pIdxKey);.
173ba 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 sqlite3_fre
173bb 65 28 70 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 20 e(pCellKey);.
173bc 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
173bd 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20 29 7b if( c==0 ){
173be 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61 . if( pPa
173bf 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26 20 21 70 ge->intKey && !p
173c0 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 Page->leaf ){.
173c1 20 20 20 20 20 20 20 20 6c 77 72 20 3d 20 69 64 lwr = id
173c2 78 3b 0a 20 20 20 20 20 20 20 20 20 20 75 70 72 x;. upr
173c3 20 3d 20 6c 77 72 20 2d 20 31 3b 0a 20 20 20 20 = lwr - 1;.
173c4 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
173c5 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
173c6 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b *pRes = 0;
173c7 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
173c8 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 SQLITE_OK;.
173c9 20 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f goto moveto
173ca 5f 66 69 6e 69 73 68 3b 0a 20 20 20 20 20 20 20 _finish;.
173cb 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }. }.
173cc 20 69 66 28 20 63 3c 30 20 29 7b 0a 20 20 20 20 if( c<0 ){.
173cd 20 20 20 20 6c 77 72 20 3d 20 69 64 78 2b 31 3b lwr = idx+1;
173ce 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
173cf 20 20 20 20 20 20 75 70 72 20 3d 20 69 64 78 2d upr = idx-
173d0 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 1;. }.
173d1 20 69 66 28 20 6c 77 72 3e 75 70 72 20 29 7b 0a if( lwr>upr ){.
173d2 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
173d3 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 43 75 }. pCu
173d4 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
173d5 50 61 67 65 5d 20 3d 20 28 75 31 36 29 28 28 6c Page] = (u16)((l
173d6 77 72 2b 75 70 72 29 2f 32 29 3b 0a 20 20 20 20 wr+upr)/2);.
173d7 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 6c 77 }. assert( lw
173d8 72 3d 3d 75 70 72 2b 31 20 29 3b 0a 20 20 20 20 r==upr+1 );.
173d9 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 assert( pPage->i
173da 73 49 6e 69 74 20 29 3b 0a 20 20 20 20 69 66 28 sInit );. if(
173db 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a pPage->leaf ){.
173dc 20 20 20 20 20 20 63 68 6c 64 50 67 20 3d 20 30 chldPg = 0
173dd 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
173de 6c 77 72 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 6c lwr>=pPage->nCel
173df 6c 20 29 7b 0a 20 20 20 20 20 20 63 68 6c 64 50 l ){. chldP
173e0 67 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 g = get4byte(&pP
173e1 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 age->aData[pPage
173e2 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b ->hdrOffset+8]);
173e3 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
173e4 20 20 63 68 6c 64 50 67 20 3d 20 67 65 74 34 62 chldPg = get4b
173e5 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 61 yte(findCell(pPa
173e6 67 65 2c 20 6c 77 72 29 29 3b 0a 20 20 20 20 7d ge, lwr));. }
173e7 0a 20 20 20 20 69 66 28 20 63 68 6c 64 50 67 3d . if( chldPg=
173e8 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 =0 ){. asse
173e9 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b rt( pCur->aiIdx[
173ea 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 43 75 pCur->iPage]<pCu
173eb 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
173ec 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b iPage]->nCell );
173ed 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 63 . *pRes = c
173ee 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c ;. rc = SQL
173ef 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 67 6f ITE_OK;. go
173f0 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 to moveto_finish
173f1 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 75 72 ;. }. pCur
173f2 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 ->aiIdx[pCur->iP
173f3 61 67 65 5d 20 3d 20 28 75 31 36 29 6c 77 72 3b age] = (u16)lwr;
173f4 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e . pCur->info.
173f5 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 70 nSize = 0;. p
173f6 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d Cur->validNKey =
173f7 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 0;. rc = mov
173f8 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 63 eToChild(pCur, c
173f9 68 6c 64 50 67 29 3b 0a 20 20 20 20 69 66 28 20 hldPg);. if(
173fa 72 63 20 29 20 67 6f 74 6f 20 6d 6f 76 65 74 6f rc ) goto moveto
173fb 5f 66 69 6e 69 73 68 3b 0a 20 20 7d 0a 6d 6f 76 _finish;. }.mov
173fc 65 74 6f 5f 66 69 6e 69 73 68 3a 0a 20 20 72 65 eto_finish:. re
173fd 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a turn rc;.}.../*.
173fe 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 ** Return TRUE i
173ff 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 f the cursor is
17400 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 not pointing at
17401 61 6e 20 65 6e 74 72 79 20 6f 66 20 74 68 65 20 an entry of the
17402 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 52 55 table..**.** TRU
17403 45 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e E will be return
17404 65 64 20 61 66 74 65 72 20 61 20 63 61 6c 6c 20 ed after a call
17405 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e to sqlite3BtreeN
17406 65 78 74 28 29 20 6d 6f 76 65 73 0a 2a 2a 20 70 ext() moves.** p
17407 61 73 74 20 74 68 65 20 6c 61 73 74 20 65 6e 74 ast the last ent
17408 72 79 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 ry in the table
17409 6f 72 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 or sqlite3BtreeP
1740a 72 65 76 28 29 20 6d 6f 76 65 73 20 70 61 73 74 rev() moves past
1740b 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 65 6e .** the first en
1740c 74 72 79 2e 20 20 54 52 55 45 20 69 73 20 61 6c try. TRUE is al
1740d 73 6f 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 so returned if t
1740e 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74 he table is empt
1740f 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 y..*/.SQLITE_PRI
17410 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
17411 42 74 72 65 65 45 6f 66 28 42 74 43 75 72 73 6f BtreeEof(BtCurso
17412 72 20 2a 70 43 75 72 29 7b 0a 20 20 2f 2a 20 54 r *pCur){. /* T
17413 4f 44 4f 3a 20 57 68 61 74 20 69 66 20 74 68 65 ODO: What if the
17414 20 63 75 72 73 6f 72 20 69 73 20 69 6e 20 43 55 cursor is in CU
17415 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b RSOR_REQUIRESEEK
17416 20 62 75 74 20 61 6c 6c 20 74 61 62 6c 65 20 65 but all table e
17417 6e 74 72 69 65 73 0a 20 20 2a 2a 20 68 61 76 65 ntries. ** have
17418 20 62 65 65 6e 20 64 65 6c 65 74 65 64 3f 20 54 been deleted? T
17419 68 69 73 20 41 50 49 20 77 69 6c 6c 20 6e 65 65 his API will nee
1741a 64 20 74 6f 20 63 68 61 6e 67 65 20 74 6f 20 72 d to change to r
1741b 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 eturn an error c
1741c 6f 64 65 0a 20 20 2a 2a 20 61 73 20 77 65 6c 6c ode. ** as well
1741d 20 61 73 20 74 68 65 20 62 6f 6f 6c 65 61 6e 20 as the boolean
1741e 72 65 73 75 6c 74 20 76 61 6c 75 65 2e 0a 20 20 result value..
1741f 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 43 55 52 */. return (CUR
17420 53 4f 52 5f 56 41 4c 49 44 21 3d 70 43 75 72 2d SOR_VALID!=pCur-
17421 3e 65 53 74 61 74 65 29 3b 0a 7d 0a 0a 2f 2a 0a >eState);.}../*.
17422 2a 2a 20 41 64 76 61 6e 63 65 20 74 68 65 20 63 ** Advance the c
17423 75 72 73 6f 72 20 74 6f 20 74 68 65 20 6e 65 78 ursor to the nex
17424 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 t entry in the d
17425 61 74 61 62 61 73 65 2e 20 20 49 66 0a 2a 2a 20 atabase. If.**
17426 73 75 63 63 65 73 73 66 75 6c 20 74 68 65 6e 20 successful then
17427 73 65 74 20 2a 70 52 65 73 3d 30 2e 20 20 49 66 set *pRes=0. If
17428 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 77 the cursor.** w
17429 61 73 20 61 6c 72 65 61 64 79 20 70 6f 69 6e 74 as already point
1742a 69 6e 67 20 74 6f 20 74 68 65 20 6c 61 73 74 20 ing to the last
1742b 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61 74 entry in the dat
1742c 61 62 61 73 65 20 62 65 66 6f 72 65 0a 2a 2a 20 abase before.**
1742d 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61 73 this routine was
1742e 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 73 65 called, then se
1742f 74 20 2a 70 52 65 73 3d 31 2e 0a 2a 2f 0a 53 51 t *pRes=1..*/.SQ
17430 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
17431 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 sqlite3BtreeNex
17432 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 t(BtCursor *pCur
17433 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a 20 20 , int *pRes){.
17434 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 69 64 int rc;. int id
17435 78 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 x;. MemPage *pP
17436 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 age;.. assert(
17437 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
17438 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d (pCur) );. rc =
17439 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f restoreCursorPo
1743a 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 sition(pCur);.
1743b 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
1743c 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 K ){. return
1743d 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 rc;. }. assert
1743e 28 20 70 52 65 73 21 3d 30 20 29 3b 0a 20 20 69 ( pRes!=0 );. i
1743f 66 28 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 f( CURSOR_INVALI
17440 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 20 D==pCur->eState
17441 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 31 ){. *pRes = 1
17442 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
17443 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 ITE_OK;. }. if
17444 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 ( pCur->skipNext
17445 3e 30 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e >0 ){. pCur->
17446 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 skipNext = 0;.
17447 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 *pRes = 0;.
17448 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
17449 4b 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 73 K;. }. pCur->s
1744a 6b 69 70 4e 65 78 74 20 3d 20 30 3b 0a 0a 20 20 kipNext = 0;..
1744b 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 pPage = pCur->ap
1744c 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
1744d 5d 3b 0a 20 20 69 64 78 20 3d 20 2b 2b 70 43 75 ];. idx = ++pCu
1744e 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
1744f 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 Page];. assert(
17450 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29 pPage->isInit )
17451 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 78 3c ;. assert( idx<
17452 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b =pPage->nCell );
17453 0a 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e .. pCur->info.n
17454 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 Size = 0;. pCur
17455 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b ->validNKey = 0;
17456 0a 20 20 69 66 28 20 69 64 78 3e 3d 70 50 61 67 . if( idx>=pPag
17457 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 e->nCell ){.
17458 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 if( !pPage->leaf
17459 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d ){. rc = m
1745a 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c oveToChild(pCur,
1745b 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 get4byte(&pPage
1745c 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 ->aData[pPage->h
1745d 64 72 4f 66 66 73 65 74 2b 38 5d 29 29 3b 0a 20 drOffset+8]));.
1745e 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 if( rc ) re
1745f 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 72 turn rc;. r
17460 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f c = moveToLeftmo
17461 73 74 28 70 43 75 72 29 3b 0a 20 20 20 20 20 20 st(pCur);.
17462 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 *pRes = 0;.
17463 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
17464 7d 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 }. do{.
17465 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3d if( pCur->iPage=
17466 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 70 =0 ){. *p
17467 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 Res = 1;.
17468 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 pCur->eState =
17469 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a CURSOR_INVALID;.
1746a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 return S
1746b 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 QLITE_OK;.
1746c 7d 0a 20 20 20 20 20 20 6d 6f 76 65 54 6f 50 61 }. moveToPa
1746d 72 65 6e 74 28 70 43 75 72 29 3b 0a 20 20 20 20 rent(pCur);.
1746e 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e pPage = pCur->
1746f 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
17470 67 65 5d 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 ge];. }while(
17471 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 pCur->aiIdx[pCu
17472 72 2d 3e 69 50 61 67 65 5d 3e 3d 70 50 61 67 65 r->iPage]>=pPage
17473 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 2a ->nCell );. *
17474 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 69 66 pRes = 0;. if
17475 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 ( pPage->intKey
17476 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 ){. rc = sq
17477 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 70 lite3BtreeNext(p
17478 43 75 72 2c 20 70 52 65 73 29 3b 0a 20 20 20 20 Cur, pRes);.
17479 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 }else{. rc
1747a 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
1747b 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 }. return rc
1747c 3b 0a 20 20 7d 0a 20 20 2a 70 52 65 73 20 3d 20 ;. }. *pRes =
1747d 30 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 0;. if( pPage->
1747e 6c 65 61 66 20 29 7b 0a 20 20 20 20 72 65 74 75 leaf ){. retu
1747f 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
17480 7d 0a 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 4c }. rc = moveToL
17481 65 66 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 eftmost(pCur);.
17482 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a return rc;.}...
17483 2f 2a 0a 2a 2a 20 53 74 65 70 20 74 68 65 20 63 /*.** Step the c
17484 75 72 73 6f 72 20 74 6f 20 74 68 65 20 62 61 63 ursor to the bac
17485 6b 20 74 6f 20 74 68 65 20 70 72 65 76 69 6f 75 k to the previou
17486 73 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 s entry in the d
17487 61 74 61 62 61 73 65 2e 20 20 49 66 0a 2a 2a 20 atabase. If.**
17488 73 75 63 63 65 73 73 66 75 6c 20 74 68 65 6e 20 successful then
17489 73 65 74 20 2a 70 52 65 73 3d 30 2e 20 20 49 66 set *pRes=0. If
1748a 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 77 the cursor.** w
1748b 61 73 20 61 6c 72 65 61 64 79 20 70 6f 69 6e 74 as already point
1748c 69 6e 67 20 74 6f 20 74 68 65 20 66 69 72 73 74 ing to the first
1748d 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61 entry in the da
1748e 74 61 62 61 73 65 20 62 65 66 6f 72 65 0a 2a 2a tabase before.**
1748f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61 this routine wa
17490 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 73 s called, then s
17491 65 74 20 2a 70 52 65 73 3d 31 2e 0a 2a 2f 0a 53 et *pRes=1..*/.S
17492 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
17493 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 72 t sqlite3BtreePr
17494 65 76 69 6f 75 73 28 42 74 43 75 72 73 6f 72 20 evious(BtCursor
17495 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 73 *pCur, int *pRes
17496 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d ){. int rc;. M
17497 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 0a emPage *pPage;..
17498 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 assert( cursor
17499 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 HoldsMutex(pCur)
1749a 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73 74 6f );. rc = resto
1749b 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e reCursorPosition
1749c 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 (pCur);. if( rc
1749d 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1749e 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
1749f 7d 0a 20 20 70 43 75 72 2d 3e 61 74 4c 61 73 74 }. pCur->atLast
174a0 20 3d 20 30 3b 0a 20 20 69 66 28 20 43 55 52 53 = 0;. if( CURS
174a1 4f 52 5f 49 4e 56 41 4c 49 44 3d 3d 70 43 75 72 OR_INVALID==pCur
174a2 2d 3e 65 53 74 61 74 65 20 29 7b 0a 20 20 20 20 ->eState ){.
174a3 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 72 *pRes = 1;. r
174a4 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
174a5 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 75 72 2d . }. if( pCur-
174a6 3e 73 6b 69 70 4e 65 78 74 3c 30 20 29 7b 0a 20 >skipNext<0 ){.
174a7 20 20 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 pCur->skipNex
174a8 74 20 3d 20 30 3b 0a 20 20 20 20 2a 70 52 65 73 t = 0;. *pRes
174a9 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e = 0;. return
174aa 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
174ab 20 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 pCur->skipNext
174ac 20 3d 20 30 3b 0a 0a 20 20 70 50 61 67 65 20 3d = 0;.. pPage =
174ad 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 pCur->apPage[pC
174ae 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73 ur->iPage];. as
174af 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 sert( pPage->isI
174b0 6e 69 74 20 29 3b 0a 20 20 69 66 28 20 21 70 50 nit );. if( !pP
174b1 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 age->leaf ){.
174b2 20 69 6e 74 20 69 64 78 20 3d 20 70 43 75 72 2d int idx = pCur-
174b3 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
174b4 67 65 5d 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f ge];. rc = mo
174b5 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 veToChild(pCur,
174b6 67 65 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c get4byte(findCel
174b7 6c 28 70 50 61 67 65 2c 20 69 64 78 29 29 29 3b l(pPage, idx)));
174b8 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 . if( rc ){.
174b9 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
174ba 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 6d }. rc = m
174bb 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 70 oveToRightmost(p
174bc 43 75 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 Cur);. }else{.
174bd 20 20 20 77 68 69 6c 65 28 20 70 43 75 72 2d 3e while( pCur->
174be 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 aiIdx[pCur->iPag
174bf 65 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 e]==0 ){. i
174c0 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3d 3d f( pCur->iPage==
174c1 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 75 0 ){. pCu
174c2 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 r->eState = CURS
174c3 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 OR_INVALID;.
174c4 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 *pRes = 1;.
174c5 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 return SQ
174c6 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d LITE_OK;. }
174c7 0a 20 20 20 20 20 20 6d 6f 76 65 54 6f 50 61 72 . moveToPar
174c8 65 6e 74 28 70 43 75 72 29 3b 0a 20 20 20 20 7d ent(pCur);. }
174c9 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e . pCur->info.
174ca 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 70 nSize = 0;. p
174cb 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d Cur->validNKey =
174cc 20 30 3b 0a 0a 20 20 20 20 70 43 75 72 2d 3e 61 0;.. pCur->a
174cd 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
174ce 5d 2d 2d 3b 0a 20 20 20 20 70 50 61 67 65 20 3d ]--;. pPage =
174cf 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 pCur->apPage[pC
174d0 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 ur->iPage];.
174d1 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 if( pPage->intKe
174d2 79 20 26 26 20 21 70 50 61 67 65 2d 3e 6c 65 61 y && !pPage->lea
174d3 66 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 f ){. rc =
174d4 73 71 6c 69 74 65 33 42 74 72 65 65 50 72 65 76 sqlite3BtreePrev
174d5 69 6f 75 73 28 70 43 75 72 2c 20 70 52 65 73 29 ious(pCur, pRes)
174d6 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
174d7 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
174d8 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a K;. }. }. *
174d9 70 52 65 73 20 3d 20 30 3b 0a 20 20 72 65 74 75 pRes = 0;. retu
174da 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
174db 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 Allocate a new p
174dc 61 67 65 20 66 72 6f 6d 20 74 68 65 20 64 61 74 age from the dat
174dd 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a abase file..**.*
174de 2a 20 54 68 65 20 6e 65 77 20 70 61 67 65 20 69 * The new page i
174df 73 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72 74 s marked as dirt
174e0 79 2e 20 20 28 49 6e 20 6f 74 68 65 72 20 77 6f y. (In other wo
174e1 72 64 73 2c 20 73 71 6c 69 74 65 33 50 61 67 65 rds, sqlite3Page
174e2 72 57 72 69 74 65 28 29 0a 2a 2a 20 68 61 73 20 rWrite().** has
174e3 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 61 6c already been cal
174e4 6c 65 64 20 6f 6e 20 74 68 65 20 6e 65 77 20 70 led on the new p
174e5 61 67 65 2e 29 20 20 54 68 65 20 6e 65 77 20 70 age.) The new p
174e6 61 67 65 20 68 61 73 20 61 6c 73 6f 0a 2a 2a 20 age has also.**
174e7 62 65 65 6e 20 72 65 66 65 72 65 6e 63 65 64 20 been referenced
174e8 61 6e 64 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 and the calling
174e9 72 6f 75 74 69 6e 65 20 69 73 20 72 65 73 70 6f routine is respo
174ea 6e 73 69 62 6c 65 20 66 6f 72 20 63 61 6c 6c 69 nsible for calli
174eb 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 ng.** sqlite3Pag
174ec 65 72 55 6e 72 65 66 28 29 20 6f 6e 20 74 68 65 erUnref() on the
174ed 20 6e 65 77 20 70 61 67 65 20 77 68 65 6e 20 69 new page when i
174ee 74 20 69 73 20 64 6f 6e 65 2e 0a 2a 2a 0a 2a 2a t is done..**.**
174ef 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
174f0 74 75 72 6e 65 64 20 6f 6e 20 73 75 63 63 65 73 turned on succes
174f1 73 2e 20 20 41 6e 79 20 6f 74 68 65 72 20 72 65 s. Any other re
174f2 74 75 72 6e 20 76 61 6c 75 65 20 69 6e 64 69 63 turn value indic
174f3 61 74 65 73 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 ates.** an error
174f4 2e 20 20 2a 70 70 50 61 67 65 20 61 6e 64 20 2a . *ppPage and *
174f5 70 50 67 6e 6f 20 61 72 65 20 75 6e 64 65 66 69 pPgno are undefi
174f6 6e 65 64 20 69 6e 20 74 68 65 20 65 76 65 6e 74 ned in the event
174f7 20 6f 66 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a of an error..**
174f8 20 44 6f 20 6e 6f 74 20 69 6e 76 6f 6b 65 20 73 Do not invoke s
174f9 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 qlite3PagerUnref
174fa 28 29 20 6f 6e 20 2a 70 70 50 61 67 65 20 69 66 () on *ppPage if
174fb 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 an error is ret
174fc 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 urned..**.** If
174fd 74 68 65 20 22 6e 65 61 72 62 79 22 20 70 61 72 the "nearby" par
174fe 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 30 2c ameter is not 0,
174ff 20 74 68 65 6e 20 61 20 28 66 65 65 62 6c 65 29 then a (feeble)
17500 20 65 66 66 6f 72 74 20 69 73 20 6d 61 64 65 20 effort is made
17501 74 6f 20 0a 2a 2a 20 6c 6f 63 61 74 65 20 61 20 to .** locate a
17502 70 61 67 65 20 63 6c 6f 73 65 20 74 6f 20 74 68 page close to th
17503 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 22 6e e page number "n
17504 65 61 72 62 79 22 2e 20 20 54 68 69 73 20 63 61 earby". This ca
17505 6e 20 62 65 20 75 73 65 64 20 69 6e 20 61 6e 0a n be used in an.
17506 2a 2a 20 61 74 74 65 6d 70 74 20 74 6f 20 6b 65 ** attempt to ke
17507 65 70 20 72 65 6c 61 74 65 64 20 70 61 67 65 73 ep related pages
17508 20 63 6c 6f 73 65 20 74 6f 20 65 61 63 68 20 6f close to each o
17509 74 68 65 72 20 69 6e 20 74 68 65 20 64 61 74 61 ther in the data
1750a 62 61 73 65 20 66 69 6c 65 2c 0a 2a 2a 20 77 68 base file,.** wh
1750b 69 63 68 20 69 6e 20 74 75 72 6e 20 63 61 6e 20 ich in turn can
1750c 6d 61 6b 65 20 64 61 74 61 62 61 73 65 20 61 63 make database ac
1750d 63 65 73 73 20 66 61 73 74 65 72 2e 0a 2a 2a 0a cess faster..**.
1750e 2a 2a 20 49 66 20 74 68 65 20 22 65 78 61 63 74 ** If the "exact
1750f 22 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e " parameter is n
17510 6f 74 20 30 2c 20 61 6e 64 20 74 68 65 20 70 61 ot 0, and the pa
17511 67 65 2d 6e 75 6d 62 65 72 20 6e 65 61 72 62 79 ge-number nearby
17512 20 65 78 69 73 74 73 20 0a 2a 2a 20 61 6e 79 77 exists .** anyw
17513 68 65 72 65 20 6f 6e 20 74 68 65 20 66 72 65 65 here on the free
17514 2d 6c 69 73 74 2c 20 74 68 65 6e 20 69 74 20 69 -list, then it i
17515 73 20 67 75 61 72 65 6e 74 65 65 64 20 74 6f 20 s guarenteed to
17516 62 65 20 72 65 74 75 72 6e 65 64 2e 20 54 68 69 be returned. Thi
17517 73 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 75 73 65 s.** is only use
17518 64 20 62 79 20 61 75 74 6f 2d 76 61 63 75 75 6d d by auto-vacuum
17519 20 64 61 74 61 62 61 73 65 73 20 77 68 65 6e 20 databases when
1751a 61 6c 6c 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 allocating a new
1751b 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 table..*/.stati
1751c 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65 42 74 c int allocateBt
1751d 72 65 65 50 61 67 65 28 0a 20 20 42 74 53 68 61 reePage(. BtSha
1751e 72 65 64 20 2a 70 42 74 2c 20 0a 20 20 4d 65 6d red *pBt, . Mem
1751f 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 0a Page **ppPage, .
17520 20 20 50 67 6e 6f 20 2a 70 50 67 6e 6f 2c 20 0a Pgno *pPgno, .
17521 20 20 50 67 6e 6f 20 6e 65 61 72 62 79 2c 0a 20 Pgno nearby,.
17522 20 75 38 20 65 78 61 63 74 0a 29 7b 0a 20 20 4d u8 exact.){. M
17523 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 3b 0a emPage *pPage1;.
17524 20 20 69 6e 74 20 72 63 3b 0a 20 20 75 33 32 20 int rc;. u32
17525 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 n; /* Number
17526 20 6f 66 20 70 61 67 65 73 20 6f 6e 20 74 68 65 of pages on the
17527 20 66 72 65 65 6c 69 73 74 20 2a 2f 0a 20 20 75 freelist */. u
17528 33 32 20 6b 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 32 k; /* Num
17529 62 65 72 20 6f 66 20 6c 65 61 76 65 73 20 6f 6e ber of leaves on
1752a 20 74 68 65 20 74 72 75 6e 6b 20 6f 66 20 74 68 the trunk of th
1752b 65 20 66 72 65 65 6c 69 73 74 20 2a 2f 0a 20 20 e freelist */.
1752c 4d 65 6d 50 61 67 65 20 2a 70 54 72 75 6e 6b 20 MemPage *pTrunk
1752d 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a = 0;. MemPage *
1752e 70 50 72 65 76 54 72 75 6e 6b 20 3d 20 30 3b 0a pPrevTrunk = 0;.
1752f 20 20 50 67 6e 6f 20 6d 78 50 61 67 65 3b 20 20 Pgno mxPage;
17530 20 20 20 2f 2a 20 54 6f 74 61 6c 20 73 69 7a 65 /* Total size
17531 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
17532 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 61 73 73 65 file */.. asse
17533 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
17534 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 x_held(pBt->mute
17535 78 29 20 29 3b 0a 20 20 70 50 61 67 65 31 20 3d x) );. pPage1 =
17536 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 0a 20 20 pBt->pPage1;.
17537 6d 78 50 61 67 65 20 3d 20 70 61 67 65 72 50 61 mxPage = pagerPa
17538 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 0a 20 20 gecount(pBt);.
17539 6e 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 n = get4byte(&pP
1753a 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 age1->aData[36])
1753b 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 3d ;. testcase( n=
1753c 3d 6d 78 50 61 67 65 2d 31 20 29 3b 0a 20 20 69 =mxPage-1 );. i
1753d 66 28 20 6e 3e 3d 6d 78 50 61 67 65 20 29 7b 0a f( n>=mxPage ){.
1753e 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1753f 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
17540 20 20 7d 0a 20 20 69 66 28 20 6e 3e 30 20 29 7b }. if( n>0 ){
17541 0a 20 20 20 20 2f 2a 20 54 68 65 72 65 20 61 72 . /* There ar
17542 65 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20 66 e pages on the f
17543 72 65 65 6c 69 73 74 2e 20 20 52 65 75 73 65 20 reelist. Reuse
17544 6f 6e 65 20 6f 66 20 74 68 6f 73 65 20 70 61 67 one of those pag
17545 65 73 2e 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 es. */. Pgno
17546 69 54 72 75 6e 6b 3b 0a 20 20 20 20 75 38 20 73 iTrunk;. u8 s
17547 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 20 2f earchList = 0; /
17548 2a 20 49 66 20 74 68 65 20 66 72 65 65 2d 6c 69 * If the free-li
17549 73 74 20 6d 75 73 74 20 62 65 20 73 65 61 72 63 st must be searc
1754a 68 65 64 20 66 6f 72 20 27 6e 65 61 72 62 79 27 hed for 'nearby'
1754b 20 2a 2f 0a 20 20 20 20 0a 20 20 20 20 2f 2a 20 */. . /*
1754c 49 66 20 74 68 65 20 27 65 78 61 63 74 27 20 70 If the 'exact' p
1754d 61 72 61 6d 65 74 65 72 20 77 61 73 20 74 72 75 arameter was tru
1754e 65 20 61 6e 64 20 61 20 71 75 65 72 79 20 6f 66 e and a query of
1754f 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 the pointer-map
17550 0a 20 20 20 20 2a 2a 20 73 68 6f 77 73 20 74 68 . ** shows th
17551 61 74 20 74 68 65 20 70 61 67 65 20 27 6e 65 61 at the page 'nea
17552 72 62 79 27 20 69 73 20 73 6f 6d 65 77 68 65 72 rby' is somewher
17553 65 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 e on the free-li
17554 73 74 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20 st, then. **
17555 74 68 65 20 65 6e 74 69 72 65 2d 6c 69 73 74 20 the entire-list
17556 77 69 6c 6c 20 62 65 20 73 65 61 72 63 68 65 64 will be searched
17557 20 66 6f 72 20 74 68 61 74 20 70 61 67 65 2e 0a for that page..
17558 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 */.#ifndef S
17559 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
1755a 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 65 78 ACUUM. if( ex
1755b 61 63 74 20 26 26 20 6e 65 61 72 62 79 3c 3d 6d act && nearby<=m
1755c 78 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 75 xPage ){. u
1755d 38 20 65 54 79 70 65 3b 0a 20 20 20 20 20 20 61 8 eType;. a
1755e 73 73 65 72 74 28 20 6e 65 61 72 62 79 3e 30 20 ssert( nearby>0
1755f 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
17560 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d pBt->autoVacuum
17561 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 );. rc = p
17562 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 6e 65 trmapGet(pBt, ne
17563 61 72 62 79 2c 20 26 65 54 79 70 65 2c 20 30 29 arby, &eType, 0)
17564 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 ;. if( rc )
17565 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
17566 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 if( eType==PTR
17567 4d 41 50 5f 46 52 45 45 50 41 47 45 20 29 7b 0a MAP_FREEPAGE ){.
17568 20 20 20 20 20 20 20 20 73 65 61 72 63 68 4c 69 searchLi
17569 73 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a st = 1;. }.
1756a 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 6e *pPgno = n
1756b 65 61 72 62 79 3b 0a 20 20 20 20 7d 0a 23 65 6e earby;. }.#en
1756c 64 69 66 0a 0a 20 20 20 20 2f 2a 20 44 65 63 72 dif.. /* Decr
1756d 65 6d 65 6e 74 20 74 68 65 20 66 72 65 65 2d 6c ement the free-l
1756e 69 73 74 20 63 6f 75 6e 74 20 62 79 20 31 2e 20 ist count by 1.
1756f 53 65 74 20 69 54 72 75 6e 6b 20 74 6f 20 74 68 Set iTrunk to th
17570 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 0a 20 e index of the.
17571 20 20 20 2a 2a 20 66 69 72 73 74 20 66 72 65 65 ** first free
17572 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 -list trunk page
17573 2e 20 69 50 72 65 76 54 72 75 6e 6b 20 69 73 20 . iPrevTrunk is
17574 69 6e 69 74 69 61 6c 6c 79 20 31 2e 0a 20 20 20 initially 1..
17575 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c */. rc = sql
17576 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
17577 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b Page1->pDbPage);
17578 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 . if( rc ) re
17579 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 70 75 74 turn rc;. put
1757a 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 4byte(&pPage1->a
1757b 44 61 74 61 5b 33 36 5d 2c 20 6e 2d 31 29 3b 0a Data[36], n-1);.
1757c 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 64 65 . /* The code
1757d 20 77 69 74 68 69 6e 20 74 68 69 73 20 6c 6f 6f within this loo
1757e 70 20 69 73 20 72 75 6e 20 6f 6e 6c 79 20 6f 6e p is run only on
1757f 63 65 20 69 66 20 74 68 65 20 27 73 65 61 72 63 ce if the 'searc
17580 68 4c 69 73 74 27 20 76 61 72 69 61 62 6c 65 0a hList' variable.
17581 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74 20 74 72 ** is not tr
17582 75 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 ue. Otherwise, i
17583 74 20 72 75 6e 73 20 6f 6e 63 65 20 66 6f 72 20 t runs once for
17584 65 61 63 68 20 74 72 75 6e 6b 2d 70 61 67 65 20 each trunk-page
17585 6f 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 72 on the. ** fr
17586 65 65 2d 6c 69 73 74 20 75 6e 74 69 6c 20 74 68 ee-list until th
17587 65 20 70 61 67 65 20 27 6e 65 61 72 62 79 27 20 e page 'nearby'
17588 69 73 20 6c 6f 63 61 74 65 64 2e 0a 20 20 20 20 is located..
17589 2a 2f 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 */. do {.
1758a 20 20 70 50 72 65 76 54 72 75 6e 6b 20 3d 20 70 pPrevTrunk = p
1758b 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 69 66 28 Trunk;. if(
1758c 20 70 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a 20 pPrevTrunk ){.
1758d 20 20 20 20 20 20 20 69 54 72 75 6e 6b 20 3d 20 iTrunk =
1758e 67 65 74 34 62 79 74 65 28 26 70 50 72 65 76 54 get4byte(&pPrevT
1758f 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 29 3b runk->aData[0]);
17590 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
17591 20 20 20 20 20 20 69 54 72 75 6e 6b 20 3d 20 67 iTrunk = g
17592 65 74 34 62 79 74 65 28 26 70 50 61 67 65 31 2d et4byte(&pPage1-
17593 3e 61 44 61 74 61 5b 33 32 5d 29 3b 0a 20 20 20 >aData[32]);.
17594 20 20 20 7d 0a 20 20 20 20 20 20 74 65 73 74 63 }. testc
17595 61 73 65 28 20 69 54 72 75 6e 6b 3d 3d 6d 78 50 ase( iTrunk==mxP
17596 61 67 65 20 29 3b 0a 20 20 20 20 20 20 69 66 28 age );. if(
17597 20 69 54 72 75 6e 6b 3e 6d 78 50 61 67 65 20 29 iTrunk>mxPage )
17598 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 {. rc = S
17599 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
1759a 50 54 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b PT;. }else{
1759b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 74 . rc = bt
1759c 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 reeGetPage(pBt,
1759d 69 54 72 75 6e 6b 2c 20 26 70 54 72 75 6e 6b 2c iTrunk, &pTrunk,
1759e 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 0);. }.
1759f 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 if( rc ){.
175a0 20 20 20 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b pTrunk = 0;
175a1 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e . goto en
175a2 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b d_allocate_page;
175a3 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 . }..
175a4 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 54 k = get4byte(&pT
175a5 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 29 3b runk->aData[4]);
175a6 0a 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 30 20 . if( k==0
175a7 26 26 20 21 73 65 61 72 63 68 4c 69 73 74 20 29 && !searchList )
175a8 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 {. /* The
175a9 20 74 72 75 6e 6b 20 68 61 73 20 6e 6f 20 6c 65 trunk has no le
175aa 61 76 65 73 20 61 6e 64 20 74 68 65 20 6c 69 73 aves and the lis
175ab 74 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 20 73 t is not being s
175ac 65 61 72 63 68 65 64 2e 20 0a 20 20 20 20 20 20 earched. .
175ad 20 20 2a 2a 20 53 6f 20 65 78 74 72 61 63 74 20 ** So extract
175ae 74 68 65 20 74 72 75 6e 6b 20 70 61 67 65 20 69 the trunk page i
175af 74 73 65 6c 66 20 61 6e 64 20 75 73 65 20 69 74 tself and use it
175b0 20 61 73 20 74 68 65 20 6e 65 77 6c 79 20 0a 20 as the newly .
175b1 20 20 20 20 20 20 20 2a 2a 20 61 6c 6c 6f 63 61 ** alloca
175b2 74 65 64 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 ted page */.
175b3 20 20 20 20 61 73 73 65 72 74 28 20 70 50 72 65 assert( pPre
175b4 76 54 72 75 6e 6b 3d 3d 30 20 29 3b 0a 20 20 20 vTrunk==0 );.
175b5 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
175b6 33 50 61 67 65 72 57 72 69 74 65 28 70 54 72 75 3PagerWrite(pTru
175b7 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 nk->pDbPage);.
175b8 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a if( rc ){.
175b9 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 goto e
175ba 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 nd_allocate_page
175bb 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
175bc 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 69 54 72 *pPgno = iTr
175bd 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d unk;. mem
175be 63 70 79 28 26 70 50 61 67 65 31 2d 3e 61 44 61 cpy(&pPage1->aDa
175bf 74 61 5b 33 32 5d 2c 20 26 70 54 72 75 6e 6b 2d ta[32], &pTrunk-
175c0 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 >aData[0], 4);.
175c1 20 20 20 20 20 20 20 2a 70 70 50 61 67 65 20 3d *ppPage =
175c2 20 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 pTrunk;.
175c3 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 pTrunk = 0;.
175c4 20 20 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c TRACE(("ALL
175c5 4f 43 41 54 45 3a 20 25 64 20 74 72 75 6e 6b 20 OCATE: %d trunk
175c6 2d 20 25 64 20 66 72 65 65 20 70 61 67 65 73 20 - %d free pages
175c7 6c 65 66 74 5c 6e 22 2c 20 2a 70 50 67 6e 6f 2c left\n", *pPgno,
175c8 20 6e 2d 31 29 29 3b 0a 20 20 20 20 20 20 7d 65 n-1));. }e
175c9 6c 73 65 20 69 66 28 20 6b 3e 28 75 33 32 29 28 lse if( k>(u32)(
175ca 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f pBt->usableSize/
175cb 34 20 2d 20 32 29 20 29 7b 0a 20 20 20 20 20 20 4 - 2) ){.
175cc 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 20 6b 20 /* Value of k
175cd 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2e is out of range.
175ce 20 20 44 61 74 61 62 61 73 65 20 63 6f 72 72 75 Database corru
175cf 70 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 20 ption */.
175d0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 rc = SQLITE_COR
175d1 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 RUPT_BKPT;.
175d2 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f goto end_allo
175d3 63 61 74 65 5f 70 61 67 65 3b 0a 23 69 66 6e 64 cate_page;.#ifnd
175d4 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
175d5 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 UTOVACUUM.
175d6 7d 65 6c 73 65 20 69 66 28 20 73 65 61 72 63 68 }else if( search
175d7 4c 69 73 74 20 26 26 20 6e 65 61 72 62 79 3d 3d List && nearby==
175d8 69 54 72 75 6e 6b 20 29 7b 0a 20 20 20 20 20 20 iTrunk ){.
175d9 20 20 2f 2a 20 54 68 65 20 6c 69 73 74 20 69 73 /* The list is
175da 20 62 65 69 6e 67 20 73 65 61 72 63 68 65 64 20 being searched
175db 61 6e 64 20 74 68 69 73 20 74 72 75 6e 6b 20 70 and this trunk p
175dc 61 67 65 20 69 73 20 74 68 65 20 70 61 67 65 0a age is the page.
175dd 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 61 6c ** to al
175de 6c 6f 63 61 74 65 2c 20 72 65 67 61 72 64 6c 65 locate, regardle
175df 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 69 74 ss of whether it
175e0 20 68 61 73 20 6c 65 61 76 65 73 2e 0a 20 20 20 has leaves..
175e1 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
175e2 61 73 73 65 72 74 28 20 2a 70 50 67 6e 6f 3d 3d assert( *pPgno==
175e3 69 54 72 75 6e 6b 20 29 3b 0a 20 20 20 20 20 20 iTrunk );.
175e4 20 20 2a 70 70 50 61 67 65 20 3d 20 70 54 72 75 *ppPage = pTru
175e5 6e 6b 3b 0a 20 20 20 20 20 20 20 20 73 65 61 72 nk;. sear
175e6 63 68 4c 69 73 74 20 3d 20 30 3b 0a 20 20 20 20 chList = 0;.
175e7 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
175e8 50 61 67 65 72 57 72 69 74 65 28 70 54 72 75 6e PagerWrite(pTrun
175e9 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 k->pDbPage);.
175ea 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 if( rc ){.
175eb 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e goto en
175ec 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b d_allocate_page;
175ed 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
175ee 20 20 20 69 66 28 20 6b 3d 3d 30 20 29 7b 0a 20 if( k==0 ){.
175ef 20 20 20 20 20 20 20 20 20 69 66 28 20 21 70 50 if( !pP
175f0 72 65 76 54 72 75 6e 6b 20 29 7b 0a 20 20 20 20 revTrunk ){.
175f1 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 memcpy(&
175f2 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 pPage1->aData[32
175f3 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 ], &pTrunk->aDat
175f4 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 a[0], 4);.
175f5 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
175f6 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 memcpy(&p
175f7 50 72 65 76 54 72 75 6e 6b 2d 3e 61 44 61 74 61 PrevTrunk->aData
175f8 5b 30 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 [0], &pTrunk->aD
175f9 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 20 ata[0], 4);.
175fa 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
175fb 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
175fc 20 2f 2a 20 54 68 65 20 74 72 75 6e 6b 20 70 61 /* The trunk pa
175fd 67 65 20 69 73 20 72 65 71 75 69 72 65 64 20 62 ge is required b
175fe 79 20 74 68 65 20 63 61 6c 6c 65 72 20 62 75 74 y the caller but
175ff 20 69 74 20 63 6f 6e 74 61 69 6e 73 20 0a 20 20 it contains .
17600 20 20 20 20 20 20 20 20 2a 2a 20 70 6f 69 6e 74 ** point
17601 65 72 73 20 74 6f 20 66 72 65 65 2d 6c 69 73 74 ers to free-list
17602 20 6c 65 61 76 65 73 2e 20 54 68 65 20 66 69 72 leaves. The fir
17603 73 74 20 6c 65 61 66 20 62 65 63 6f 6d 65 73 20 st leaf becomes
17604 61 20 74 72 75 6e 6b 0a 20 20 20 20 20 20 20 20 a trunk.
17605 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 69 ** page in thi
17606 73 20 63 61 73 65 2e 0a 20 20 20 20 20 20 20 20 s case..
17607 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 4d */. M
17608 65 6d 50 61 67 65 20 2a 70 4e 65 77 54 72 75 6e emPage *pNewTrun
17609 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 50 67 6e k;. Pgn
1760a 6f 20 69 4e 65 77 54 72 75 6e 6b 20 3d 20 67 65 o iNewTrunk = ge
1760b 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e t4byte(&pTrunk->
1760c 61 44 61 74 61 5b 38 5d 29 3b 0a 20 20 20 20 20 aData[8]);.
1760d 20 20 20 20 20 69 66 28 20 69 4e 65 77 54 72 75 if( iNewTru
1760e 6e 6b 3e 6d 78 50 61 67 65 20 29 7b 20 0a 20 20 nk>mxPage ){ .
1760f 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 rc = S
17610 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
17611 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 PT;.
17612 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 goto end_allocat
17613 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 e_page;.
17614 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 74 65 }. te
17615 73 74 63 61 73 65 28 20 69 4e 65 77 54 72 75 6e stcase( iNewTrun
17616 6b 3d 3d 6d 78 50 61 67 65 20 29 3b 0a 20 20 20 k==mxPage );.
17617 20 20 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 rc = btre
17618 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 4e eGetPage(pBt, iN
17619 65 77 54 72 75 6e 6b 2c 20 26 70 4e 65 77 54 72 ewTrunk, &pNewTr
1761a 75 6e 6b 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 unk, 0);.
1761b 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
1761c 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
1761d 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c goto end_all
1761e 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 ocate_page;.
1761f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
17620 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
17621 67 65 72 57 72 69 74 65 28 70 4e 65 77 54 72 75 gerWrite(pNewTru
17622 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 nk->pDbPage);.
17623 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
17624 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
17625 20 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 release
17626 50 61 67 65 28 70 4e 65 77 54 72 75 6e 6b 29 3b Page(pNewTrunk);
17627 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 . got
17628 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 o end_allocate_p
17629 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d age;. }
1762a 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 . memcp
1762b 79 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 y(&pNewTrunk->aD
1762c 61 74 61 5b 30 5d 2c 20 26 70 54 72 75 6e 6b 2d ata[0], &pTrunk-
1762d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 >aData[0], 4);.
1762e 20 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74 put4byt
1762f 65 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 e(&pNewTrunk->aD
17630 61 74 61 5b 34 5d 2c 20 6b 2d 31 29 3b 0a 20 20 ata[4], k-1);.
17631 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 memcpy(&
17632 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 61 74 61 pNewTrunk->aData
17633 5b 38 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 [8], &pTrunk->aD
17634 61 74 61 5b 31 32 5d 2c 20 28 6b 2d 31 29 2a 34 ata[12], (k-1)*4
17635 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 6c );. rel
17636 65 61 73 65 50 61 67 65 28 70 4e 65 77 54 72 75 easePage(pNewTru
17637 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 nk);. i
17638 66 28 20 21 70 50 72 65 76 54 72 75 6e 6b 20 29 f( !pPrevTrunk )
17639 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73 {. as
1763a 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 sert( sqlite3Pag
1763b 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 erIswriteable(pP
1763c 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 20 29 age1->pDbPage) )
1763d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 75 ;. pu
1763e 74 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e t4byte(&pPage1->
1763f 61 44 61 74 61 5b 33 32 5d 2c 20 69 4e 65 77 54 aData[32], iNewT
17640 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 runk);.
17641 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
17642 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
17643 50 61 67 65 72 57 72 69 74 65 28 70 50 72 65 76 PagerWrite(pPrev
17644 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b Trunk->pDbPage);
17645 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 . if(
17646 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 rc ){.
17647 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c goto end_al
17648 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 locate_page;.
17649 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
1764a 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 put4byte(
1764b 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61 44 61 &pPrevTrunk->aDa
1764c 74 61 5b 30 5d 2c 20 69 4e 65 77 54 72 75 6e 6b ta[0], iNewTrunk
1764d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 );. }.
1764e 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1764f 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 pTrunk = 0;.
17650 20 20 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c TRACE(("ALL
17651 4f 43 41 54 45 3a 20 25 64 20 74 72 75 6e 6b 20 OCATE: %d trunk
17652 2d 20 25 64 20 66 72 65 65 20 70 61 67 65 73 20 - %d free pages
17653 6c 65 66 74 5c 6e 22 2c 20 2a 70 50 67 6e 6f 2c left\n", *pPgno,
17654 20 6e 2d 31 29 29 3b 0a 23 65 6e 64 69 66 0a 20 n-1));.#endif.
17655 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6b }else if( k
17656 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a >0 ){. /*
17657 20 45 78 74 72 61 63 74 20 61 20 6c 65 61 66 20 Extract a leaf
17658 66 72 6f 6d 20 74 68 65 20 74 72 75 6e 6b 20 2a from the trunk *
17659 2f 0a 20 20 20 20 20 20 20 20 75 33 32 20 63 6c /. u32 cl
1765a 6f 73 65 73 74 3b 0a 20 20 20 20 20 20 20 20 50 osest;. P
1765b 67 6e 6f 20 69 50 61 67 65 3b 0a 20 20 20 20 20 gno iPage;.
1765c 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 unsigned char
1765d 20 2a 61 44 61 74 61 20 3d 20 70 54 72 75 6e 6b *aData = pTrunk
1765e 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 20 20 20 ->aData;.
1765f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
17660 65 72 57 72 69 74 65 28 70 54 72 75 6e 6b 2d 3e erWrite(pTrunk->
17661 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 pDbPage);.
17662 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 if( rc ){.
17663 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 goto end_a
17664 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 llocate_page;.
17665 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
17666 69 66 28 20 6e 65 61 72 62 79 3e 30 20 29 7b 0a if( nearby>0 ){.
17667 20 20 20 20 20 20 20 20 20 20 75 33 32 20 69 3b u32 i;
17668 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 64 . int d
17669 69 73 74 3b 0a 20 20 20 20 20 20 20 20 20 20 63 ist;. c
1766a 6c 6f 73 65 73 74 20 3d 20 30 3b 0a 20 20 20 20 losest = 0;.
1766b 20 20 20 20 20 20 64 69 73 74 20 3d 20 67 65 74 dist = get
1766c 34 62 79 74 65 28 26 61 44 61 74 61 5b 38 5d 29 4byte(&aData[8])
1766d 20 2d 20 6e 65 61 72 62 79 3b 0a 20 20 20 20 20 - nearby;.
1766e 20 20 20 20 20 69 66 28 20 64 69 73 74 3c 30 20 if( dist<0
1766f 29 20 64 69 73 74 20 3d 20 2d 64 69 73 74 3b 0a ) dist = -dist;.
17670 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d for(i=
17671 31 3b 20 69 3c 6b 3b 20 69 2b 2b 29 7b 0a 20 20 1; i<k; i++){.
17672 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 64 32 int d2
17673 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 44 61 = get4byte(&aDa
17674 74 61 5b 38 2b 69 2a 34 5d 29 20 2d 20 6e 65 61 ta[8+i*4]) - nea
17675 72 62 79 3b 0a 20 20 20 20 20 20 20 20 20 20 20 rby;.
17676 20 69 66 28 20 64 32 3c 30 20 29 20 64 32 20 3d if( d2<0 ) d2 =
17677 20 2d 64 32 3b 0a 20 20 20 20 20 20 20 20 20 20 -d2;.
17678 20 20 69 66 28 20 64 32 3c 64 69 73 74 20 29 7b if( d2<dist ){
17679 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 . c
1767a 6c 6f 73 65 73 74 20 3d 20 69 3b 0a 20 20 20 20 losest = i;.
1767b 20 20 20 20 20 20 20 20 20 20 64 69 73 74 20 3d dist =
1767c 20 64 32 3b 0a 20 20 20 20 20 20 20 20 20 20 20 d2;.
1767d 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
1767e 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
1767f 20 20 20 20 20 20 20 20 63 6c 6f 73 65 73 74 20 closest
17680 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a = 0;. }..
17681 20 20 20 20 20 20 20 20 69 50 61 67 65 20 3d 20 iPage =
17682 67 65 74 34 62 79 74 65 28 26 61 44 61 74 61 5b get4byte(&aData[
17683 38 2b 63 6c 6f 73 65 73 74 2a 34 5d 29 3b 0a 20 8+closest*4]);.
17684 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 testcase(
17685 20 69 50 61 67 65 3d 3d 6d 78 50 61 67 65 20 29 iPage==mxPage )
17686 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 50 ;. if( iP
17687 61 67 65 3e 6d 78 50 61 67 65 20 29 7b 0a 20 20 age>mxPage ){.
17688 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
17689 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
1768a 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f ;. goto
1768b 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 end_allocate_pa
1768c 67 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ge;. }.
1768d 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1768e 69 50 61 67 65 3d 3d 6d 78 50 61 67 65 20 29 3b iPage==mxPage );
1768f 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 73 65 . if( !se
17690 61 72 63 68 4c 69 73 74 20 7c 7c 20 69 50 61 67 archList || iPag
17691 65 3d 3d 6e 65 61 72 62 79 20 29 7b 0a 20 20 20 e==nearby ){.
17692 20 20 20 20 20 20 20 69 6e 74 20 6e 6f 43 6f 6e int noCon
17693 74 65 6e 74 3b 0a 20 20 20 20 20 20 20 20 20 20 tent;.
17694 2a 70 50 67 6e 6f 20 3d 20 69 50 61 67 65 3b 0a *pPgno = iPage;.
17695 20 20 20 20 20 20 20 20 20 20 54 52 41 43 45 28 TRACE(
17696 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 77 ("ALLOCATE: %d w
17697 61 73 20 6c 65 61 66 20 25 64 20 6f 66 20 25 64 as leaf %d of %d
17698 20 6f 6e 20 74 72 75 6e 6b 20 25 64 22 0a 20 20 on trunk %d".
17699 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 "
1769a 3a 20 25 64 20 6d 6f 72 65 20 66 72 65 65 20 70 : %d more free p
1769b 61 67 65 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 ages\n",.
1769c 20 20 20 20 20 20 20 20 20 20 2a 70 50 67 6e 6f *pPgno
1769d 2c 20 63 6c 6f 73 65 73 74 2b 31 2c 20 6b 2c 20 , closest+1, k,
1769e 70 54 72 75 6e 6b 2d 3e 70 67 6e 6f 2c 20 6e 2d pTrunk->pgno, n-
1769f 31 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 1));. i
176a0 66 28 20 63 6c 6f 73 65 73 74 3c 6b 2d 31 20 29 f( closest<k-1 )
176a1 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 {. me
176a2 6d 63 70 79 28 26 61 44 61 74 61 5b 38 2b 63 6c mcpy(&aData[8+cl
176a3 6f 73 65 73 74 2a 34 5d 2c 20 26 61 44 61 74 61 osest*4], &aData
176a4 5b 34 2b 6b 2a 34 5d 2c 20 34 29 3b 0a 20 20 20 [4+k*4], 4);.
176a5 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
176a6 20 20 20 70 75 74 34 62 79 74 65 28 26 61 44 61 put4byte(&aDa
176a7 74 61 5b 34 5d 2c 20 6b 2d 31 29 3b 0a 20 20 20 ta[4], k-1);.
176a8 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 assert( s
176a9 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 qlite3PagerIswri
176aa 74 65 61 62 6c 65 28 70 54 72 75 6e 6b 2d 3e 70 teable(pTrunk->p
176ab 44 62 50 61 67 65 29 20 29 3b 0a 20 20 20 20 20 DbPage) );.
176ac 20 20 20 20 20 6e 6f 43 6f 6e 74 65 6e 74 20 3d noContent =
176ad 20 21 62 74 72 65 65 47 65 74 48 61 73 43 6f 6e !btreeGetHasCon
176ae 74 65 6e 74 28 70 42 74 2c 20 2a 70 50 67 6e 6f tent(pBt, *pPgno
176af 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 );. rc
176b0 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 = btreeGetPage(p
176b1 42 74 2c 20 2a 70 50 67 6e 6f 2c 20 70 70 50 61 Bt, *pPgno, ppPa
176b2 67 65 2c 20 6e 6f 43 6f 6e 74 65 6e 74 29 3b 0a ge, noContent);.
176b3 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 if( rc
176b4 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
176b5 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 rc =
176b6 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
176b7 65 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 44 62 e((*ppPage)->pDb
176b8 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 Page);.
176b9 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
176ba 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
176bb 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 releasePag
176bc 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 20 e(*ppPage);.
176bd 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
176be 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
176bf 73 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 0a searchList = 0;.
176c0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
176c1 7d 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 }. releaseP
176c2 61 67 65 28 70 50 72 65 76 54 72 75 6e 6b 29 3b age(pPrevTrunk);
176c3 0a 20 20 20 20 20 20 70 50 72 65 76 54 72 75 6e . pPrevTrun
176c4 6b 20 3d 20 30 3b 0a 20 20 20 20 7d 77 68 69 6c k = 0;. }whil
176c5 65 28 20 73 65 61 72 63 68 4c 69 73 74 20 29 3b e( searchList );
176c6 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
176c7 20 54 68 65 72 65 20 61 72 65 20 6e 6f 20 70 61 There are no pa
176c8 67 65 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c ges on the freel
176c9 69 73 74 2c 20 73 6f 20 63 72 65 61 74 65 20 61 ist, so create a
176ca 20 6e 65 77 20 70 61 67 65 20 61 74 20 74 68 65 new page at the
176cb 0a 20 20 20 20 2a 2a 20 65 6e 64 20 6f 66 20 74 . ** end of t
176cc 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 69 he file */. i
176cd 6e 74 20 6e 50 61 67 65 20 3d 20 70 61 67 65 72 nt nPage = pager
176ce 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 0a Pagecount(pBt);.
176cf 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 6e 50 61 *pPgno = nPa
176d0 67 65 20 2b 20 31 3b 0a 0a 20 20 20 20 69 66 28 ge + 1;.. if(
176d1 20 2a 70 50 67 6e 6f 3d 3d 50 45 4e 44 49 4e 47 *pPgno==PENDING
176d2 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 _BYTE_PAGE(pBt)
176d3 29 7b 0a 20 20 20 20 20 20 28 2a 70 50 67 6e 6f ){. (*pPgno
176d4 29 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 6e )++;. }..#ifn
176d5 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
176d6 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 AUTOVACUUM. i
176d7 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 f( pBt->autoVacu
176d8 75 6d 20 26 26 20 50 54 52 4d 41 50 5f 49 53 50 um && PTRMAP_ISP
176d9 41 47 45 28 70 42 74 2c 20 2a 70 50 67 6e 6f 29 AGE(pBt, *pPgno)
176da 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 ){. /* If
176db 2a 70 50 67 6e 6f 20 72 65 66 65 72 73 20 74 6f *pPgno refers to
176dc 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 a pointer-map p
176dd 61 67 65 2c 20 61 6c 6c 6f 63 61 74 65 20 74 77 age, allocate tw
176de 6f 20 6e 65 77 20 70 61 67 65 73 0a 20 20 20 20 o new pages.
176df 20 20 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20 ** at the end
176e0 6f 66 20 74 68 65 20 66 69 6c 65 20 69 6e 73 74 of the file inst
176e1 65 61 64 20 6f 66 20 6f 6e 65 2e 20 54 68 65 20 ead of one. The
176e2 66 69 72 73 74 20 61 6c 6c 6f 63 61 74 65 64 20 first allocated
176e3 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 62 65 page. ** be
176e4 63 6f 6d 65 73 20 61 20 6e 65 77 20 70 6f 69 6e comes a new poin
176e5 74 65 72 2d 6d 61 70 20 70 61 67 65 2c 20 74 68 ter-map page, th
176e6 65 20 73 65 63 6f 6e 64 20 69 73 20 75 73 65 64 e second is used
176e7 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a by the caller..
176e8 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 4d */. M
176e9 65 6d 50 61 67 65 20 2a 70 50 67 20 3d 20 30 3b emPage *pPg = 0;
176ea 0a 20 20 20 20 20 20 54 52 41 43 45 28 28 22 41 . TRACE(("A
176eb 4c 4c 4f 43 41 54 45 3a 20 25 64 20 66 72 6f 6d LLOCATE: %d from
176ec 20 65 6e 64 20 6f 66 20 66 69 6c 65 20 28 70 6f end of file (po
176ed 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 29 5c inter-map page)\
176ee 6e 22 2c 20 2a 70 50 67 6e 6f 29 29 3b 0a 20 20 n", *pPgno));.
176ef 20 20 20 20 61 73 73 65 72 74 28 20 2a 70 50 67 assert( *pPg
176f0 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 no!=PENDING_BYTE
176f1 5f 50 41 47 45 28 70 42 74 29 20 29 3b 0a 20 20 _PAGE(pBt) );.
176f2 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 rc = btreeGe
176f3 74 50 61 67 65 28 70 42 74 2c 20 2a 70 50 67 6e tPage(pBt, *pPgn
176f4 6f 2c 20 26 70 50 67 2c 20 30 29 3b 0a 20 20 20 o, &pPg, 0);.
176f5 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
176f6 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
176f7 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
176f8 72 57 72 69 74 65 28 70 50 67 2d 3e 70 44 62 50 rWrite(pPg->pDbP
176f9 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 72 65 age);. re
176fa 6c 65 61 73 65 50 61 67 65 28 70 50 67 29 3b 0a leasePage(pPg);.
176fb 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
176fc 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 ( rc ) return rc
176fd 3b 0a 20 20 20 20 20 20 28 2a 70 50 67 6e 6f 29 ;. (*pPgno)
176fe 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 2a 70 ++;. if( *p
176ff 50 67 6e 6f 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 Pgno==PENDING_BY
17700 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 20 TE_PAGE(pBt) ){
17701 28 2a 70 50 67 6e 6f 29 2b 2b 3b 20 7d 0a 20 20 (*pPgno)++; }.
17702 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 }.#endif..
17703 61 73 73 65 72 74 28 20 2a 70 50 67 6e 6f 21 3d assert( *pPgno!=
17704 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 PENDING_BYTE_PAG
17705 45 28 70 42 74 29 20 29 3b 0a 20 20 20 20 72 63 E(pBt) );. rc
17706 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 = btreeGetPage(
17707 70 42 74 2c 20 2a 70 50 67 6e 6f 2c 20 70 70 50 pBt, *pPgno, ppP
17708 61 67 65 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 age, 0);. if(
17709 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
1770a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1770b 33 50 61 67 65 72 57 72 69 74 65 28 28 2a 70 70 3PagerWrite((*pp
1770c 50 61 67 65 29 2d 3e 70 44 62 50 61 67 65 29 3b Page)->pDbPage);
1770d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
1770e 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
1770f 72 65 6c 65 61 73 65 50 61 67 65 28 2a 70 70 50 releasePage(*ppP
17710 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 age);. }.
17711 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 TRACE(("ALLOCATE
17712 3a 20 25 64 20 66 72 6f 6d 20 65 6e 64 20 6f 66 : %d from end of
17713 20 66 69 6c 65 5c 6e 22 2c 20 2a 70 50 67 6e 6f file\n", *pPgno
17714 29 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 ));. }.. asser
17715 74 28 20 2a 70 50 67 6e 6f 21 3d 50 45 4e 44 49 t( *pPgno!=PENDI
17716 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 NG_BYTE_PAGE(pBt
17717 29 20 29 3b 0a 0a 65 6e 64 5f 61 6c 6c 6f 63 61 ) );..end_alloca
17718 74 65 5f 70 61 67 65 3a 0a 20 20 72 65 6c 65 61 te_page:. relea
17719 73 65 50 61 67 65 28 70 54 72 75 6e 6b 29 3b 0a sePage(pTrunk);.
1771a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 releasePage(pP
1771b 72 65 76 54 72 75 6e 6b 29 3b 0a 20 20 69 66 28 revTrunk);. if(
1771c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1771d 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 {. if( sqlite
1771e 33 50 61 67 65 72 50 61 67 65 52 65 66 63 6f 75 3PagerPageRefcou
1771f 6e 74 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 44 nt((*ppPage)->pD
17720 62 50 61 67 65 29 3e 31 20 29 7b 0a 20 20 20 20 bPage)>1 ){.
17721 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a 70 releasePage(*p
17722 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 72 65 pPage);. re
17723 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
17724 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a UPT_BKPT;. }.
17725 20 20 20 20 28 2a 70 70 50 61 67 65 29 2d 3e 69 (*ppPage)->i
17726 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 7d 65 6c sInit = 0;. }el
17727 73 65 7b 0a 20 20 20 20 2a 70 70 50 61 67 65 20 se{. *ppPage
17728 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 = 0;. }. retur
17729 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 n rc;.}../*.** T
1772a 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
1772b 75 73 65 64 20 74 6f 20 61 64 64 20 70 61 67 65 used to add page
1772c 20 69 50 61 67 65 20 74 6f 20 74 68 65 20 64 61 iPage to the da
1772d 74 61 62 61 73 65 20 66 69 6c 65 20 66 72 65 65 tabase file free
1772e 2d 6c 69 73 74 2e 20 0a 2a 2a 20 49 74 20 69 73 -list. .** It is
1772f 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 assumed that th
17730 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c e page is not al
17731 72 65 61 64 79 20 61 20 70 61 72 74 20 6f 66 20 ready a part of
17732 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2e 0a 2a the free-list..*
17733 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 70 *.** The value p
17734 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63 assed as the sec
17735 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 ond argument to
17736 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
17737 20 6f 70 74 69 6f 6e 61 6c 2e 0a 2a 2a 20 49 66 optional..** If
17738 20 74 68 65 20 63 61 6c 6c 65 72 20 68 61 70 70 the caller happ
17739 65 6e 73 20 74 6f 20 68 61 76 65 20 61 20 70 6f ens to have a po
1773a 69 6e 74 65 72 20 74 6f 20 74 68 65 20 4d 65 6d inter to the Mem
1773b 50 61 67 65 20 6f 62 6a 65 63 74 20 0a 2a 2a 20 Page object .**
1773c 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f corresponding to
1773d 20 70 61 67 65 20 69 50 61 67 65 20 68 61 6e 64 page iPage hand
1773e 79 2c 20 69 74 20 6d 61 79 20 70 61 73 73 20 69 y, it may pass i
1773f 74 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 t as the second
17740 76 61 6c 75 65 2e 20 0a 2a 2a 20 4f 74 68 65 72 value. .** Other
17741 77 69 73 65 2c 20 69 74 20 6d 61 79 20 70 61 73 wise, it may pas
17742 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 49 66 s NULL..**.** If
17743 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
17744 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63 74 20 69 MemPage object i
17745 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 s passed as the
17746 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c second argument,
17747 0a 2a 2a 20 69 74 73 20 72 65 66 65 72 65 6e 63 .** its referenc
17748 65 20 63 6f 75 6e 74 20 69 73 20 6e 6f 74 20 61 e count is not a
17749 6c 74 65 72 65 64 20 62 79 20 74 68 69 73 20 66 ltered by this f
1774a 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 unction..*/.stat
1774b 69 63 20 69 6e 74 20 66 72 65 65 50 61 67 65 32 ic int freePage2
1774c 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 (BtShared *pBt,
1774d 4d 65 6d 50 61 67 65 20 2a 70 4d 65 6d 50 61 67 MemPage *pMemPag
1774e 65 2c 20 50 67 6e 6f 20 69 50 61 67 65 29 7b 0a e, Pgno iPage){.
1774f 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 72 75 6e MemPage *pTrun
17750 6b 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 k = 0;
17751 20 20 20 20 20 20 2f 2a 20 46 72 65 65 2d 6c 69 /* Free-li
17752 73 74 20 74 72 75 6e 6b 20 70 61 67 65 20 2a 2f st trunk page */
17753 0a 20 20 50 67 6e 6f 20 69 54 72 75 6e 6b 20 3d . Pgno iTrunk =
17754 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
17755 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e /* Page n
17756 75 6d 62 65 72 20 6f 66 20 66 72 65 65 2d 6c 69 umber of free-li
17757 73 74 20 74 72 75 6e 6b 20 70 61 67 65 20 2a 2f st trunk page */
17758 20 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 . MemPage *pPa
17759 67 65 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 ge1 = pBt->pPage
1775a 31 3b 20 20 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 1; /* Local
1775b 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 70 61 reference to pa
1775c 67 65 20 31 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 ge 1 */. MemPag
1775d 65 20 2a 70 50 61 67 65 3b 20 20 20 20 20 20 20 e *pPage;
1775e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1775f 20 50 61 67 65 20 62 65 69 6e 67 20 66 72 65 65 Page being free
17760 64 2e 20 4d 61 79 20 62 65 20 4e 55 4c 4c 2e 20 d. May be NULL.
17761 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 */. int rc;
17762 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17763 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 /* Retu
17764 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 rn Code */. int
17765 20 6e 46 72 65 65 3b 20 20 20 20 20 20 20 20 20 nFree;
17766 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17767 20 2f 2a 20 49 6e 69 74 69 61 6c 20 6e 75 6d 62 /* Initial numb
17768 65 72 20 6f 66 20 70 61 67 65 73 20 6f 6e 20 66 er of pages on f
17769 72 65 65 2d 6c 69 73 74 20 2a 2f 0a 0a 20 20 61 ree-list */.. a
1776a 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
1776b 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d utex_held(pBt->m
1776c 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
1776d 74 28 20 69 50 61 67 65 3e 31 20 29 3b 0a 20 20 t( iPage>1 );.
1776e 61 73 73 65 72 74 28 20 21 70 4d 65 6d 50 61 67 assert( !pMemPag
1776f 65 20 7c 7c 20 70 4d 65 6d 50 61 67 65 2d 3e 70 e || pMemPage->p
17770 67 6e 6f 3d 3d 69 50 61 67 65 20 29 3b 0a 0a 20 gno==iPage );..
17771 20 69 66 28 20 70 4d 65 6d 50 61 67 65 20 29 7b if( pMemPage ){
17772 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70 4d 65 . pPage = pMe
17773 6d 50 61 67 65 3b 0a 20 20 20 20 73 71 6c 69 74 mPage;. sqlit
17774 65 33 50 61 67 65 72 52 65 66 28 70 50 61 67 65 e3PagerRef(pPage
17775 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 7d 65 ->pDbPage);. }e
17776 6c 73 65 7b 0a 20 20 20 20 70 50 61 67 65 20 3d lse{. pPage =
17777 20 62 74 72 65 65 50 61 67 65 4c 6f 6f 6b 75 70 btreePageLookup
17778 28 70 42 74 2c 20 69 50 61 67 65 29 3b 0a 20 20 (pBt, iPage);.
17779 7d 0a 0a 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e }.. /* Incremen
1777a 74 20 74 68 65 20 66 72 65 65 20 70 61 67 65 20 t the free page
1777b 63 6f 75 6e 74 20 6f 6e 20 70 50 61 67 65 31 20 count on pPage1
1777c 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 */. rc = sqlite
1777d 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 3PagerWrite(pPag
1777e 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 e1->pDbPage);.
1777f 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 66 72 if( rc ) goto fr
17780 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 6e 46 eepage_out;. nF
17781 72 65 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 ree = get4byte(&
17782 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 pPage1->aData[36
17783 5d 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26 ]);. put4byte(&
17784 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 pPage1->aData[36
17785 5d 2c 20 6e 46 72 65 65 2b 31 29 3b 0a 0a 23 69 ], nFree+1);..#i
17786 66 64 65 66 20 53 51 4c 49 54 45 5f 53 45 43 55 fdef SQLITE_SECU
17787 52 45 5f 44 45 4c 45 54 45 0a 20 20 2f 2a 20 49 RE_DELETE. /* I
17788 66 20 74 68 65 20 53 51 4c 49 54 45 5f 53 45 43 f the SQLITE_SEC
17789 55 52 45 5f 44 45 4c 45 54 45 20 63 6f 6d 70 69 URE_DELETE compi
1778a 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 20 69 le-time option i
1778b 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 6e 0a s enabled, then.
1778c 20 20 2a 2a 20 61 6c 77 61 79 73 20 66 75 6c 6c ** always full
1778d 79 20 6f 76 65 72 77 72 69 74 65 20 64 65 6c 65 y overwrite dele
1778e 74 65 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 ted information
1778f 77 69 74 68 20 7a 65 72 6f 73 2e 0a 20 20 2a 2f with zeros.. */
17790 0a 20 20 69 66 28 20 28 21 70 50 61 67 65 20 26 . if( (!pPage &
17791 26 20 28 72 63 20 3d 20 62 74 72 65 65 47 65 74 & (rc = btreeGet
17792 50 61 67 65 28 70 42 74 2c 20 69 50 61 67 65 2c Page(pBt, iPage,
17793 20 26 70 50 61 67 65 2c 20 30 29 29 29 0a 20 20 &pPage, 0))).
17794 20 7c 7c 20 20 20 20 20 20 20 20 20 20 20 20 28 || (
17795 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
17796 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 rWrite(pPage->pD
17797 62 50 61 67 65 29 29 0a 20 20 29 7b 0a 20 20 20 bPage)). ){.
17798 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f goto freepage_o
17799 75 74 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 ut;. }. memset
1779a 28 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 30 (pPage->aData, 0
1779b 2c 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 70 61 , pPage->pBt->pa
1779c 67 65 53 69 7a 65 29 3b 0a 23 65 6e 64 69 66 0a geSize);.#endif.
1779d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 61 74 . /* If the dat
1779e 61 62 61 73 65 20 73 75 70 70 6f 72 74 73 20 61 abase supports a
1779f 75 74 6f 2d 76 61 63 75 75 6d 2c 20 77 72 69 74 uto-vacuum, writ
177a0 65 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 74 68 e an entry in th
177a1 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 20 20 e pointer-map.
177a2 2a 2a 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 ** to indicate t
177a3 68 61 74 20 74 68 65 20 70 61 67 65 20 69 73 20 hat the page is
177a4 66 72 65 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 free.. */. if(
177a5 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29 7b ISAUTOVACUUM ){
177a6 0a 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 . ptrmapPut(p
177a7 42 74 2c 20 69 50 61 67 65 2c 20 50 54 52 4d 41 Bt, iPage, PTRMA
177a8 50 5f 46 52 45 45 50 41 47 45 2c 20 30 2c 20 26 P_FREEPAGE, 0, &
177a9 72 63 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 rc);. if( rc
177aa 29 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f ) goto freepage_
177ab 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e out;. }.. /* N
177ac 6f 77 20 6d 61 6e 69 70 75 6c 61 74 65 20 74 68 ow manipulate th
177ad 65 20 61 63 74 75 61 6c 20 64 61 74 61 62 61 73 e actual databas
177ae 65 20 66 72 65 65 2d 6c 69 73 74 20 73 74 72 75 e free-list stru
177af 63 74 75 72 65 2e 20 54 68 65 72 65 20 61 72 65 cture. There are
177b0 20 74 77 6f 0a 20 20 2a 2a 20 70 6f 73 73 69 62 two. ** possib
177b1 69 6c 69 74 69 65 73 2e 20 49 66 20 74 68 65 20 ilities. If the
177b2 66 72 65 65 2d 6c 69 73 74 20 69 73 20 63 75 72 free-list is cur
177b3 72 65 6e 74 6c 79 20 65 6d 70 74 79 2c 20 6f 72 rently empty, or
177b4 20 69 66 20 74 68 65 20 66 69 72 73 74 0a 20 20 if the first.
177b5 2a 2a 20 74 72 75 6e 6b 20 70 61 67 65 20 69 6e ** trunk page in
177b6 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 69 the free-list i
177b7 73 20 66 75 6c 6c 2c 20 74 68 65 6e 20 74 68 69 s full, then thi
177b8 73 20 70 61 67 65 20 77 69 6c 6c 20 62 65 63 6f s page will beco
177b9 6d 65 20 61 0a 20 20 2a 2a 20 6e 65 77 20 66 72 me a. ** new fr
177ba 65 65 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 ee-list trunk pa
177bb 67 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 ge. Otherwise, i
177bc 74 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 61 20 t will become a
177bd 6c 65 61 66 20 6f 66 20 74 68 65 0a 20 20 2a 2a leaf of the. **
177be 20 66 69 72 73 74 20 74 72 75 6e 6b 20 70 61 67 first trunk pag
177bf 65 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 e in the current
177c0 20 66 72 65 65 2d 6c 69 73 74 2e 20 54 68 69 73 free-list. This
177c1 20 62 6c 6f 63 6b 20 74 65 73 74 73 20 69 66 20 block tests if
177c2 69 74 0a 20 20 2a 2a 20 69 73 20 70 6f 73 73 69 it. ** is possi
177c3 62 6c 65 20 74 6f 20 61 64 64 20 74 68 65 20 70 ble to add the p
177c4 61 67 65 20 61 73 20 61 20 6e 65 77 20 66 72 65 age as a new fre
177c5 65 2d 6c 69 73 74 20 6c 65 61 66 2e 0a 20 20 2a e-list leaf.. *
177c6 2f 0a 20 20 69 66 28 20 6e 46 72 65 65 21 3d 30 /. if( nFree!=0
177c7 20 29 7b 0a 20 20 20 20 75 33 32 20 6e 4c 65 61 ){. u32 nLea
177c8 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 f;
177c9 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 6e 75 6d /* Initial num
177ca 62 65 72 20 6f 66 20 6c 65 61 66 20 63 65 6c 6c ber of leaf cell
177cb 73 20 6f 6e 20 74 72 75 6e 6b 20 70 61 67 65 20 s on trunk page
177cc 2a 2f 0a 0a 20 20 20 20 69 54 72 75 6e 6b 20 3d */.. iTrunk =
177cd 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 get4byte(&pPage
177ce 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29 3b 0a 20 1->aData[32]);.
177cf 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 rc = btreeGet
177d0 50 61 67 65 28 70 42 74 2c 20 69 54 72 75 6e 6b Page(pBt, iTrunk
177d1 2c 20 26 70 54 72 75 6e 6b 2c 20 30 29 3b 0a 20 , &pTrunk, 0);.
177d2 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
177d3 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f E_OK ){. go
177d4 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b to freepage_out;
177d5 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e 4c 65 61 . }.. nLea
177d6 66 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 54 f = get4byte(&pT
177d7 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 29 3b runk->aData[4]);
177d8 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42 74 . assert( pBt
177d9 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3e 33 32 20 ->usableSize>32
177da 29 3b 0a 20 20 20 20 69 66 28 20 6e 4c 65 61 66 );. if( nLeaf
177db 20 3e 20 28 75 33 32 29 70 42 74 2d 3e 75 73 61 > (u32)pBt->usa
177dc 62 6c 65 53 69 7a 65 2f 34 20 2d 20 32 20 29 7b bleSize/4 - 2 ){
177dd 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
177de 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
177df 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 72 65 65 . goto free
177e0 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a page_out;. }.
177e1 20 20 20 20 69 66 28 20 6e 4c 65 61 66 20 3c 20 if( nLeaf <
177e2 28 75 33 32 29 70 42 74 2d 3e 75 73 61 62 6c 65 (u32)pBt->usable
177e3 53 69 7a 65 2f 34 20 2d 20 38 20 29 7b 0a 20 20 Size/4 - 8 ){.
177e4 20 20 20 20 2f 2a 20 49 6e 20 74 68 69 73 20 63 /* In this c
177e5 61 73 65 20 74 68 65 72 65 20 69 73 20 72 6f 6f ase there is roo
177e6 6d 20 6f 6e 20 74 68 65 20 74 72 75 6e 6b 20 70 m on the trunk p
177e7 61 67 65 20 74 6f 20 69 6e 73 65 72 74 20 74 68 age to insert th
177e8 65 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 e page. **
177e9 62 65 69 6e 67 20 66 72 65 65 64 20 61 73 20 61 being freed as a
177ea 20 6e 65 77 20 6c 65 61 66 2e 0a 20 20 20 20 20 new leaf..
177eb 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4e 6f 74 **. ** Not
177ec 65 20 74 68 61 74 20 74 68 65 20 74 72 75 6e 6b e that the trunk
177ed 20 70 61 67 65 20 69 73 20 6e 6f 74 20 72 65 61 page is not rea
177ee 6c 6c 79 20 66 75 6c 6c 20 75 6e 74 69 6c 20 69 lly full until i
177ef 74 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20 20 20 t contains.
177f0 20 2a 2a 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 ** usableSize/4
177f1 20 2d 20 32 20 65 6e 74 72 69 65 73 2c 20 6e 6f - 2 entries, no
177f2 74 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d t usableSize/4 -
177f3 20 38 20 65 6e 74 72 69 65 73 20 61 73 20 77 65 8 entries as we
177f4 20 68 61 76 65 0a 20 20 20 20 20 20 2a 2a 20 63 have. ** c
177f5 6f 64 65 64 2e 20 20 42 75 74 20 64 75 65 20 74 oded. But due t
177f6 6f 20 61 20 63 6f 64 69 6e 67 20 65 72 72 6f 72 o a coding error
177f7 20 69 6e 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 in versions of
177f8 53 51 4c 69 74 65 20 70 72 69 6f 72 20 74 6f 0a SQLite prior to.
177f9 20 20 20 20 20 20 2a 2a 20 33 2e 36 2e 30 2c 20 ** 3.6.0,
177fa 64 61 74 61 62 61 73 65 73 20 77 69 74 68 20 66 databases with f
177fb 72 65 65 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 reelist trunk pa
177fc 67 65 73 20 68 6f 6c 64 69 6e 67 20 6d 6f 72 65 ges holding more
177fd 20 74 68 61 6e 0a 20 20 20 20 20 20 2a 2a 20 75 than. ** u
177fe 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20 sableSize/4 - 8
177ff 65 6e 74 72 69 65 73 20 77 69 6c 6c 20 62 65 20 entries will be
17800 72 65 70 6f 72 74 65 64 20 61 73 20 63 6f 72 72 reported as corr
17801 75 70 74 2e 20 20 49 6e 20 6f 72 64 65 72 0a 20 upt. In order.
17802 20 20 20 20 20 2a 2a 20 74 6f 20 6d 61 69 6e 74 ** to maint
17803 61 69 6e 20 62 61 63 6b 77 61 72 64 73 20 63 6f ain backwards co
17804 6d 70 61 74 69 62 69 6c 69 74 79 20 77 69 74 68 mpatibility with
17805 20 6f 6c 64 65 72 20 76 65 72 73 69 6f 6e 73 20 older versions
17806 6f 66 20 53 51 4c 69 74 65 2c 0a 20 20 20 20 20 of SQLite,.
17807 20 2a 2a 20 77 65 20 77 69 6c 6c 20 63 6f 6e 74 ** we will cont
17808 69 6e 75 65 20 74 6f 20 72 65 73 74 72 69 63 74 inue to restrict
17809 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 the number of e
1780a 6e 74 72 69 65 73 20 74 6f 20 75 73 61 62 6c 65 ntries to usable
1780b 53 69 7a 65 2f 34 20 2d 20 38 0a 20 20 20 20 20 Size/4 - 8.
1780c 20 2a 2a 20 66 6f 72 20 6e 6f 77 2e 20 20 41 74 ** for now. At
1780d 20 73 6f 6d 65 20 70 6f 69 6e 74 20 69 6e 20 74 some point in t
1780e 68 65 20 66 75 74 75 72 65 20 28 6f 6e 63 65 20 he future (once
1780f 65 76 65 72 79 6f 6e 65 20 68 61 73 20 75 70 67 everyone has upg
17810 72 61 64 65 64 0a 20 20 20 20 20 20 2a 2a 20 74 raded. ** t
17811 6f 20 33 2e 36 2e 30 20 6f 72 20 6c 61 74 65 72 o 3.6.0 or later
17812 29 20 77 65 20 73 68 6f 75 6c 64 20 63 6f 6e 73 ) we should cons
17813 69 64 65 72 20 66 69 78 69 6e 67 20 74 68 65 20 ider fixing the
17814 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 61 62 6f 76 conditional abov
17815 65 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 72 65 e. ** to re
17816 61 64 20 22 75 73 61 62 6c 65 53 69 7a 65 2f 34 ad "usableSize/4
17817 2d 32 22 20 69 6e 73 74 65 61 64 20 6f 66 20 22 -2" instead of "
17818 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d 38 22 2e usableSize/4-8".
17819 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
1781a 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
1781b 72 57 72 69 74 65 28 70 54 72 75 6e 6b 2d 3e 70 rWrite(pTrunk->p
1781c 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 DbPage);. i
1781d 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1781e 20 29 7b 0a 20 20 20 20 20 20 20 20 70 75 74 34 ){. put4
1781f 62 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 byte(&pTrunk->aD
17820 61 74 61 5b 34 5d 2c 20 6e 4c 65 61 66 2b 31 29 ata[4], nLeaf+1)
17821 3b 0a 20 20 20 20 20 20 20 20 70 75 74 34 62 79 ;. put4by
17822 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 te(&pTrunk->aDat
17823 61 5b 38 2b 6e 4c 65 61 66 2a 34 5d 2c 20 69 50 a[8+nLeaf*4], iP
17824 61 67 65 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 age);.#ifndef SQ
17825 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 LITE_SECURE_DELE
17826 54 45 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 TE. if( p
17827 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20 Page ){.
17828 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f sqlite3PagerDo
17829 6e 74 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 ntWrite(pPage->p
1782a 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 DbPage);.
1782b 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 }.#endif.
1782c 20 20 72 63 20 3d 20 62 74 72 65 65 53 65 74 48 rc = btreeSetH
1782d 61 73 43 6f 6e 74 65 6e 74 28 70 42 74 2c 20 69 asContent(pBt, i
1782e 50 61 67 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 Page);. }.
1782f 20 20 20 20 20 54 52 41 43 45 28 28 22 46 52 45 TRACE(("FRE
17830 45 2d 50 41 47 45 3a 20 25 64 20 6c 65 61 66 20 E-PAGE: %d leaf
17831 6f 6e 20 74 72 75 6e 6b 20 70 61 67 65 20 25 64 on trunk page %d
17832 5c 6e 22 2c 70 50 61 67 65 2d 3e 70 67 6e 6f 2c \n",pPage->pgno,
17833 70 54 72 75 6e 6b 2d 3e 70 67 6e 6f 29 29 3b 0a pTrunk->pgno));.
17834 20 20 20 20 20 20 67 6f 74 6f 20 66 72 65 65 70 goto freep
17835 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 age_out;. }.
17836 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 63 6f 6e 74 }.. /* If cont
17837 72 6f 6c 20 66 6c 6f 77 73 20 74 6f 20 74 68 69 rol flows to thi
17838 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e 20 69 74 s point, then it
17839 20 77 61 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c was not possibl
1783a 65 20 74 6f 20 61 64 64 20 74 68 65 0a 20 20 2a e to add the. *
1783b 2a 20 74 68 65 20 70 61 67 65 20 62 65 69 6e 67 * the page being
1783c 20 66 72 65 65 64 20 61 73 20 61 20 6c 65 61 66 freed as a leaf
1783d 20 70 61 67 65 20 6f 66 20 74 68 65 20 66 69 72 page of the fir
1783e 73 74 20 74 72 75 6e 6b 20 69 6e 20 74 68 65 20 st trunk in the
1783f 66 72 65 65 2d 6c 69 73 74 2e 0a 20 20 2a 2a 20 free-list.. **
17840 50 6f 73 73 69 62 6c 79 20 62 65 63 61 75 73 65 Possibly because
17841 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 69 the free-list i
17842 73 20 65 6d 70 74 79 2c 20 6f 72 20 70 6f 73 73 s empty, or poss
17843 69 62 6c 79 20 62 65 63 61 75 73 65 20 74 68 65 ibly because the
17844 20 0a 20 20 2a 2a 20 66 69 72 73 74 20 74 72 75 . ** first tru
17845 6e 6b 20 69 6e 20 74 68 65 20 66 72 65 65 2d 6c nk in the free-l
17846 69 73 74 20 69 73 20 66 75 6c 6c 2e 20 45 69 74 ist is full. Eit
17847 68 65 72 20 77 61 79 2c 20 74 68 65 20 70 61 67 her way, the pag
17848 65 20 62 65 69 6e 67 20 66 72 65 65 64 0a 20 20 e being freed.
17849 2a 2a 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74 ** will become t
1784a 68 65 20 6e 65 77 20 66 69 72 73 74 20 74 72 75 he new first tru
1784b 6e 6b 20 70 61 67 65 20 69 6e 20 74 68 65 20 66 nk page in the f
1784c 72 65 65 2d 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 ree-list.. */.
1784d 20 69 66 28 20 70 50 61 67 65 3d 3d 30 20 26 26 if( pPage==0 &&
1784e 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 SQLITE_OK!=(rc
1784f 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 = btreeGetPage(p
17850 42 74 2c 20 69 50 61 67 65 2c 20 26 70 50 61 67 Bt, iPage, &pPag
17851 65 2c 20 30 29 29 20 29 7b 0a 20 20 20 20 67 6f e, 0)) ){. go
17852 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b to freepage_out;
17853 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 . }. rc = sqli
17854 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 te3PagerWrite(pP
17855 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 age->pDbPage);.
17856 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
17857 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66 OK ){. goto f
17858 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d reepage_out;. }
17859 0a 20 20 70 75 74 34 62 79 74 65 28 70 50 61 67 . put4byte(pPag
1785a 65 2d 3e 61 44 61 74 61 2c 20 69 54 72 75 6e 6b e->aData, iTrunk
1785b 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26 70 );. put4byte(&p
1785c 50 61 67 65 2d 3e 61 44 61 74 61 5b 34 5d 2c 20 Page->aData[4],
1785d 30 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26 0);. put4byte(&
1785e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 pPage1->aData[32
1785f 5d 2c 20 69 50 61 67 65 29 3b 0a 20 20 54 52 41 ], iPage);. TRA
17860 43 45 28 28 22 46 52 45 45 2d 50 41 47 45 3a 20 CE(("FREE-PAGE:
17861 25 64 20 6e 65 77 20 74 72 75 6e 6b 20 70 61 67 %d new trunk pag
17862 65 20 72 65 70 6c 61 63 69 6e 67 20 25 64 5c 6e e replacing %d\n
17863 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 ", pPage->pgno,
17864 69 54 72 75 6e 6b 29 29 3b 0a 0a 66 72 65 65 70 iTrunk));..freep
17865 61 67 65 5f 6f 75 74 3a 0a 20 20 69 66 28 20 70 age_out:. if( p
17866 50 61 67 65 20 29 7b 0a 20 20 20 20 70 50 61 67 Page ){. pPag
17867 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 e->isInit = 0;.
17868 20 7d 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 }. releasePage
17869 28 70 50 61 67 65 29 3b 0a 20 20 72 65 6c 65 61 (pPage);. relea
1786a 73 65 50 61 67 65 28 70 54 72 75 6e 6b 29 3b 0a sePage(pTrunk);.
1786b 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 73 return rc;.}.s
1786c 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 65 50 tatic void freeP
1786d 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 age(MemPage *pPa
1786e 67 65 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a 20 ge, int *pRC){.
1786f 20 69 66 28 20 28 2a 70 52 43 29 3d 3d 53 51 4c if( (*pRC)==SQL
17870 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a 70 ITE_OK ){. *p
17871 52 43 20 3d 20 66 72 65 65 50 61 67 65 32 28 70 RC = freePage2(p
17872 50 61 67 65 2d 3e 70 42 74 2c 20 70 50 61 67 65 Page->pBt, pPage
17873 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a , pPage->pgno);.
17874 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 }.}../*.** Fre
17875 65 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20 70 e any overflow p
17876 61 67 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 ages associated
17877 77 69 74 68 20 74 68 65 20 67 69 76 65 6e 20 43 with the given C
17878 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ell..*/.static i
17879 6e 74 20 63 6c 65 61 72 43 65 6c 6c 28 4d 65 6d nt clearCell(Mem
1787a 50 61 67 65 20 2a 70 50 61 67 65 2c 20 75 6e 73 Page *pPage, uns
1787b 69 67 6e 65 64 20 63 68 61 72 20 2a 70 43 65 6c igned char *pCel
1787c 6c 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a l){. BtShared *
1787d 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 pBt = pPage->pBt
1787e 3b 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 ;. CellInfo inf
1787f 6f 3b 0a 20 20 50 67 6e 6f 20 6f 76 66 6c 50 67 o;. Pgno ovflPg
17880 6e 6f 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 no;. int rc;.
17881 69 6e 74 20 6e 4f 76 66 6c 3b 0a 20 20 75 31 36 int nOvfl;. u16
17882 20 6f 76 66 6c 50 61 67 65 53 69 7a 65 3b 0a 0a ovflPageSize;..
17883 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
17884 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 3_mutex_held(pPa
17885 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 ge->pBt->mutex)
17886 29 3b 0a 20 20 62 74 72 65 65 50 61 72 73 65 43 );. btreeParseC
17887 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 ellPtr(pPage, pC
17888 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 69 ell, &info);. i
17889 66 28 20 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f f( info.iOverflo
1788a 77 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 w==0 ){. retu
1788b 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 2f rn SQLITE_OK; /
1788c 2a 20 4e 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 * No overflow pa
1788d 67 65 73 2e 20 52 65 74 75 72 6e 20 77 69 74 68 ges. Return with
1788e 6f 75 74 20 64 6f 69 6e 67 20 61 6e 79 74 68 69 out doing anythi
1788f 6e 67 20 2a 2f 0a 20 20 7d 0a 20 20 6f 76 66 6c ng */. }. ovfl
17890 50 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 Pgno = get4byte(
17891 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 &pCell[info.iOve
17892 72 66 6c 6f 77 5d 29 3b 0a 20 20 61 73 73 65 72 rflow]);. asser
17893 74 28 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 t( pBt->usableSi
17894 7a 65 20 3e 20 34 20 29 3b 0a 20 20 6f 76 66 6c ze > 4 );. ovfl
17895 50 61 67 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e PageSize = pBt->
17896 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 0a usableSize - 4;.
17897 20 20 6e 4f 76 66 6c 20 3d 20 28 69 6e 66 6f 2e nOvfl = (info.
17898 6e 50 61 79 6c 6f 61 64 20 2d 20 69 6e 66 6f 2e nPayload - info.
17899 6e 4c 6f 63 61 6c 20 2b 20 6f 76 66 6c 50 61 67 nLocal + ovflPag
1789a 65 53 69 7a 65 20 2d 20 31 29 2f 6f 76 66 6c 50 eSize - 1)/ovflP
1789b 61 67 65 53 69 7a 65 3b 0a 20 20 61 73 73 65 72 ageSize;. asser
1789c 74 28 20 6f 76 66 6c 50 67 6e 6f 3d 3d 30 20 7c t( ovflPgno==0 |
1789d 7c 20 6e 4f 76 66 6c 3e 30 20 29 3b 0a 20 20 77 | nOvfl>0 );. w
1789e 68 69 6c 65 28 20 6e 4f 76 66 6c 2d 2d 20 29 7b hile( nOvfl-- ){
1789f 0a 20 20 20 20 50 67 6e 6f 20 69 4e 65 78 74 20 . Pgno iNext
178a0 3d 20 30 3b 0a 20 20 20 20 4d 65 6d 50 61 67 65 = 0;. MemPage
178a1 20 2a 70 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 20 *pOvfl = 0;.
178a2 20 69 66 28 20 6f 76 66 6c 50 67 6e 6f 3c 32 20 if( ovflPgno<2
178a3 7c 7c 20 6f 76 66 6c 50 67 6e 6f 3e 70 61 67 65 || ovflPgno>page
178a4 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 20 rPagecount(pBt)
178a5 29 7b 0a 20 20 20 20 20 20 2f 2a 20 30 20 69 73 ){. /* 0 is
178a6 20 6e 6f 74 20 61 20 6c 65 67 61 6c 20 70 61 67 not a legal pag
178a7 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 70 61 67 e number and pag
178a8 65 20 31 20 63 61 6e 6e 6f 74 20 62 65 20 61 6e e 1 cannot be an
178a9 20 0a 20 20 20 20 20 20 2a 2a 20 6f 76 65 72 66 . ** overf
178aa 6c 6f 77 20 70 61 67 65 2e 20 54 68 65 72 65 66 low page. Theref
178ab 6f 72 65 20 69 66 20 6f 76 66 6c 50 67 6e 6f 3c ore if ovflPgno<
178ac 32 20 6f 72 20 70 61 73 74 20 74 68 65 20 65 6e 2 or past the en
178ad 64 20 6f 66 20 74 68 65 20 0a 20 20 20 20 20 20 d of the .
178ae 2a 2a 20 66 69 6c 65 20 74 68 65 20 64 61 74 61 ** file the data
178af 62 61 73 65 20 6d 75 73 74 20 62 65 20 63 6f 72 base must be cor
178b0 72 75 70 74 2e 20 2a 2f 0a 20 20 20 20 20 20 72 rupt. */. r
178b1 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
178b2 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d RUPT_BKPT;. }
178b3 0a 20 20 20 20 69 66 28 20 6e 4f 76 66 6c 20 29 . if( nOvfl )
178b4 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 67 65 74 {. rc = get
178b5 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 70 42 74 OverflowPage(pBt
178b6 2c 20 6f 76 66 6c 50 67 6e 6f 2c 20 26 70 4f 76 , ovflPgno, &pOv
178b7 66 6c 2c 20 26 69 4e 65 78 74 29 3b 0a 20 20 20 fl, &iNext);.
178b8 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 if( rc ) retu
178b9 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 rn rc;. }.
178ba 20 72 63 20 3d 20 66 72 65 65 50 61 67 65 32 28 rc = freePage2(
178bb 70 42 74 2c 20 70 4f 76 66 6c 2c 20 6f 76 66 6c pBt, pOvfl, ovfl
178bc 50 67 6e 6f 29 3b 0a 20 20 20 20 69 66 28 20 70 Pgno);. if( p
178bd 4f 76 66 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 Ovfl ){. sq
178be 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 lite3PagerUnref(
178bf 70 4f 76 66 6c 2d 3e 70 44 62 50 61 67 65 29 3b pOvfl->pDbPage);
178c0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 . }. if( r
178c1 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 c ) return rc;.
178c2 20 20 20 6f 76 66 6c 50 67 6e 6f 20 3d 20 69 4e ovflPgno = iN
178c3 65 78 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ext;. }. retur
178c4 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
178c5 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 74 68 65 /*.** Create the
178c6 20 62 79 74 65 20 73 65 71 75 65 6e 63 65 20 75 byte sequence u
178c7 73 65 64 20 74 6f 20 72 65 70 72 65 73 65 6e 74 sed to represent
178c8 20 61 20 63 65 6c 6c 20 6f 6e 20 70 61 67 65 20 a cell on page
178c9 70 50 61 67 65 0a 2a 2a 20 61 6e 64 20 77 72 69 pPage.** and wri
178ca 74 65 20 74 68 61 74 20 62 79 74 65 20 73 65 71 te that byte seq
178cb 75 65 6e 63 65 20 69 6e 74 6f 20 70 43 65 6c 6c uence into pCell
178cc 5b 5d 2e 20 20 4f 76 65 72 66 6c 6f 77 20 70 61 []. Overflow pa
178cd 67 65 73 20 61 72 65 0a 2a 2a 20 61 6c 6c 6f 63 ges are.** alloc
178ce 61 74 65 64 20 61 6e 64 20 66 69 6c 6c 65 64 20 ated and filled
178cf 69 6e 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e in as necessary.
178d0 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 70 72 The calling pr
178d1 6f 63 65 64 75 72 65 0a 2a 2a 20 69 73 20 72 65 ocedure.** is re
178d2 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 6d 61 sponsible for ma
178d3 6b 69 6e 67 20 73 75 72 65 20 73 75 66 66 69 63 king sure suffic
178d4 69 65 6e 74 20 73 70 61 63 65 20 68 61 73 20 62 ient space has b
178d5 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a een allocated.**
178d6 20 66 6f 72 20 70 43 65 6c 6c 5b 5d 2e 0a 2a 2a for pCell[]..**
178d7 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 70 43 .** Note that pC
178d8 65 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 6e 65 63 ell does not nec
178d9 65 73 73 61 72 79 20 6e 65 65 64 20 74 6f 20 70 essary need to p
178da 6f 69 6e 74 20 74 6f 20 74 68 65 20 70 50 61 67 oint to the pPag
178db 65 2d 3e 61 44 61 74 61 0a 2a 2a 20 61 72 65 61 e->aData.** area
178dc 2e 20 20 70 43 65 6c 6c 20 6d 69 67 68 74 20 70 . pCell might p
178dd 6f 69 6e 74 20 74 6f 20 73 6f 6d 65 20 74 65 6d oint to some tem
178de 70 6f 72 61 72 79 20 73 74 6f 72 61 67 65 2e 20 porary storage.
178df 20 54 68 65 20 63 65 6c 6c 20 77 69 6c 6c 0a 2a The cell will.*
178e0 2a 20 62 65 20 63 6f 6e 73 74 72 75 63 74 65 64 * be constructed
178e1 20 69 6e 20 74 68 69 73 20 74 65 6d 70 6f 72 61 in this tempora
178e2 72 79 20 61 72 65 61 20 74 68 65 6e 20 63 6f 70 ry area then cop
178e3 69 65 64 20 69 6e 74 6f 20 70 50 61 67 65 2d 3e ied into pPage->
178e4 61 44 61 74 61 0a 2a 2a 20 6c 61 74 65 72 2e 0a aData.** later..
178e5 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 */.static int fi
178e6 6c 6c 49 6e 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 llInCell(. MemP
178e7 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 20 20 age *pPage,
178e8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
178e9 65 20 70 61 67 65 20 74 68 61 74 20 63 6f 6e 74 e page that cont
178ea 61 69 6e 73 20 74 68 65 20 63 65 6c 6c 20 2a 2f ains the cell */
178eb 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
178ec 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20 20 20 20 *pCell,
178ed 20 20 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 74 65 /* Complete te
178ee 78 74 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 2a xt of the cell *
178ef 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a /. const void *
178f0 70 4b 65 79 2c 20 69 36 34 20 6e 4b 65 79 2c 20 pKey, i64 nKey,
178f1 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 2a 2f /* The key */
178f2 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 . const void *p
178f3 44 61 74 61 2c 69 6e 74 20 6e 44 61 74 61 2c 20 Data,int nData,
178f4 20 20 2f 2a 20 54 68 65 20 64 61 74 61 20 2a 2f /* The data */
178f5 0a 20 20 69 6e 74 20 6e 5a 65 72 6f 2c 20 20 20 . int nZero,
178f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
178f7 20 20 2f 2a 20 45 78 74 72 61 20 7a 65 72 6f 20 /* Extra zero
178f8 62 79 74 65 73 20 74 6f 20 61 70 70 65 6e 64 20 bytes to append
178f9 74 6f 20 70 44 61 74 61 20 2a 2f 0a 20 20 69 6e to pData */. in
178fa 74 20 2a 70 6e 53 69 7a 65 20 20 20 20 20 20 20 t *pnSize
178fb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
178fc 57 72 69 74 65 20 63 65 6c 6c 20 73 69 7a 65 20 Write cell size
178fd 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 here */.){. int
178fe 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 63 6f 6e nPayload;. con
178ff 73 74 20 75 38 20 2a 70 53 72 63 3b 0a 20 20 69 st u8 *pSrc;. i
17900 6e 74 20 6e 53 72 63 2c 20 6e 2c 20 72 63 3b 0a nt nSrc, n, rc;.
17901 20 20 69 6e 74 20 73 70 61 63 65 4c 65 66 74 3b int spaceLeft;
17902 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 4f 76 66 . MemPage *pOvf
17903 6c 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67 65 l = 0;. MemPage
17904 20 2a 70 54 6f 52 65 6c 65 61 73 65 20 3d 20 30 *pToRelease = 0
17905 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 ;. unsigned cha
17906 72 20 2a 70 50 72 69 6f 72 3b 0a 20 20 75 6e 73 r *pPrior;. uns
17907 69 67 6e 65 64 20 63 68 61 72 20 2a 70 50 61 79 igned char *pPay
17908 6c 6f 61 64 3b 0a 20 20 42 74 53 68 61 72 65 64 load;. BtShared
17909 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 *pBt = pPage->p
1790a 42 74 3b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 4f Bt;. Pgno pgnoO
1790b 76 66 6c 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e vfl = 0;. int n
1790c 48 65 61 64 65 72 3b 0a 20 20 43 65 6c 6c 49 6e Header;. CellIn
1790d 66 6f 20 69 6e 66 6f 3b 0a 0a 20 20 61 73 73 65 fo info;.. asse
1790e 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
1790f 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 x_held(pPage->pB
17910 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 t->mutex) );..
17911 2f 2a 20 70 50 61 67 65 20 69 73 20 6e 6f 74 20 /* pPage is not
17912 6e 65 63 65 73 73 61 72 69 6c 79 20 77 72 69 74 necessarily writ
17913 65 61 62 6c 65 20 73 69 6e 63 65 20 70 43 65 6c eable since pCel
17914 6c 20 6d 69 67 68 74 20 62 65 20 61 75 78 69 6c l might be auxil
17915 69 61 72 79 0a 20 20 2a 2a 20 62 75 66 66 65 72 iary. ** buffer
17916 20 73 70 61 63 65 20 74 68 61 74 20 69 73 20 73 space that is s
17917 65 70 61 72 61 74 65 20 66 72 6f 6d 20 74 68 65 eparate from the
17918 20 70 50 61 67 65 20 62 75 66 66 65 72 20 61 72 pPage buffer ar
17919 65 61 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 ea */. assert(
1791a 70 43 65 6c 6c 3c 70 50 61 67 65 2d 3e 61 44 61 pCell<pPage->aDa
1791b 74 61 20 7c 7c 20 70 43 65 6c 6c 3e 3d 26 70 50 ta || pCell>=&pP
1791c 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d 3e age->aData[pBt->
1791d 70 61 67 65 53 69 7a 65 5d 0a 20 20 20 20 20 20 pageSize].
1791e 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 || sqlite3
1791f 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 PagerIswriteable
17920 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
17921 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 );.. /* Fill i
17922 6e 20 74 68 65 20 68 65 61 64 65 72 2e 20 2a 2f n the header. */
17923 0a 20 20 6e 48 65 61 64 65 72 20 3d 20 30 3b 0a . nHeader = 0;.
17924 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 if( !pPage->le
17925 61 66 20 29 7b 0a 20 20 20 20 6e 48 65 61 64 65 af ){. nHeade
17926 72 20 2b 3d 20 34 3b 0a 20 20 7d 0a 20 20 69 66 r += 4;. }. if
17927 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 ( pPage->hasData
17928 20 29 7b 0a 20 20 20 20 6e 48 65 61 64 65 72 20 ){. nHeader
17929 2b 3d 20 70 75 74 56 61 72 69 6e 74 28 26 70 43 += putVarint(&pC
1792a 65 6c 6c 5b 6e 48 65 61 64 65 72 5d 2c 20 6e 44 ell[nHeader], nD
1792b 61 74 61 2b 6e 5a 65 72 6f 29 3b 0a 20 20 7d 65 ata+nZero);. }e
1792c 6c 73 65 7b 0a 20 20 20 20 6e 44 61 74 61 20 3d lse{. nData =
1792d 20 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 7d 0a nZero = 0;. }.
1792e 20 20 6e 48 65 61 64 65 72 20 2b 3d 20 70 75 74 nHeader += put
1792f 56 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b 6e 48 Varint(&pCell[nH
17930 65 61 64 65 72 5d 2c 20 2a 28 75 36 34 2a 29 26 eader], *(u64*)&
17931 6e 4b 65 79 29 3b 0a 20 20 62 74 72 65 65 50 61 nKey);. btreePa
17932 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 rseCellPtr(pPage
17933 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b , pCell, &info);
17934 0a 20 20 61 73 73 65 72 74 28 20 69 6e 66 6f 2e . assert( info.
17935 6e 48 65 61 64 65 72 3d 3d 6e 48 65 61 64 65 72 nHeader==nHeader
17936 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 6e );. assert( in
17937 66 6f 2e 6e 4b 65 79 3d 3d 6e 4b 65 79 20 29 3b fo.nKey==nKey );
17938 0a 20 20 61 73 73 65 72 74 28 20 69 6e 66 6f 2e . assert( info.
17939 6e 44 61 74 61 3d 3d 28 75 33 32 29 28 6e 44 61 nData==(u32)(nDa
1793a 74 61 2b 6e 5a 65 72 6f 29 20 29 3b 0a 20 20 0a ta+nZero) );. .
1793b 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 65 /* Fill in the
1793c 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 20 6e 50 payload */. nP
1793d 61 79 6c 6f 61 64 20 3d 20 6e 44 61 74 61 20 2b ayload = nData +
1793e 20 6e 5a 65 72 6f 3b 0a 20 20 69 66 28 20 70 50 nZero;. if( pP
1793f 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 age->intKey ){.
17940 20 20 20 70 53 72 63 20 3d 20 70 44 61 74 61 3b pSrc = pData;
17941 0a 20 20 20 20 6e 53 72 63 20 3d 20 6e 44 61 74 . nSrc = nDat
17942 61 3b 0a 20 20 20 20 6e 44 61 74 61 20 3d 20 30 a;. nData = 0
17943 3b 0a 20 20 7d 65 6c 73 65 7b 20 0a 20 20 20 20 ;. }else{ .
17944 69 66 28 20 4e 45 56 45 52 28 6e 4b 65 79 3e 30 if( NEVER(nKey>0
17945 78 37 66 66 66 66 66 66 66 20 7c 7c 20 70 4b 65 x7fffffff || pKe
17946 79 3d 3d 30 29 20 29 7b 0a 20 20 20 20 20 20 72 y==0) ){. r
17947 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
17948 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d RUPT_BKPT;. }
17949 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64 20 2b 3d . nPayload +=
1794a 20 28 69 6e 74 29 6e 4b 65 79 3b 0a 20 20 20 20 (int)nKey;.
1794b 70 53 72 63 20 3d 20 70 4b 65 79 3b 0a 20 20 20 pSrc = pKey;.
1794c 20 6e 53 72 63 20 3d 20 28 69 6e 74 29 6e 4b 65 nSrc = (int)nKe
1794d 79 3b 0a 20 20 7d 0a 20 20 2a 70 6e 53 69 7a 65 y;. }. *pnSize
1794e 20 3d 20 69 6e 66 6f 2e 6e 53 69 7a 65 3b 0a 20 = info.nSize;.
1794f 20 73 70 61 63 65 4c 65 66 74 20 3d 20 69 6e 66 spaceLeft = inf
17950 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 70 50 61 79 o.nLocal;. pPay
17951 6c 6f 61 64 20 3d 20 26 70 43 65 6c 6c 5b 6e 48 load = &pCell[nH
17952 65 61 64 65 72 5d 3b 0a 20 20 70 50 72 69 6f 72 eader];. pPrior
17953 20 3d 20 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 = &pCell[info.i
17954 4f 76 65 72 66 6c 6f 77 5d 3b 0a 0a 20 20 77 68 Overflow];.. wh
17955 69 6c 65 28 20 6e 50 61 79 6c 6f 61 64 3e 30 20 ile( nPayload>0
17956 29 7b 0a 20 20 20 20 69 66 28 20 73 70 61 63 65 ){. if( space
17957 4c 65 66 74 3d 3d 30 20 29 7b 0a 23 69 66 6e 64 Left==0 ){.#ifnd
17958 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
17959 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 UTOVACUUM.
1795a 50 67 6e 6f 20 70 67 6e 6f 50 74 72 6d 61 70 20 Pgno pgnoPtrmap
1795b 3d 20 70 67 6e 6f 4f 76 66 6c 3b 20 2f 2a 20 4f = pgnoOvfl; /* O
1795c 76 65 72 66 6c 6f 77 20 70 61 67 65 20 70 6f 69 verflow page poi
1795d 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 79 20 70 nter-map entry p
1795e 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 age */. if(
1795f 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d pBt->autoVacuum
17960 20 29 7b 0a 20 20 20 20 20 20 20 20 64 6f 7b 0a ){. do{.
17961 20 20 20 20 20 20 20 20 20 20 70 67 6e 6f 4f 76 pgnoOv
17962 66 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 20 fl++;. }
17963 77 68 69 6c 65 28 20 0a 20 20 20 20 20 20 20 20 while( .
17964 20 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 PTRMAP_ISPAGE(
17965 70 42 74 2c 20 70 67 6e 6f 4f 76 66 6c 29 20 7c pBt, pgnoOvfl) |
17966 7c 20 70 67 6e 6f 4f 76 66 6c 3d 3d 50 45 4e 44 | pgnoOvfl==PEND
17967 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 ING_BYTE_PAGE(pB
17968 74 29 20 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 t) . );.
17969 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 }.#endif.
1796a 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 rc = allocat
1796b 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 eBtreePage(pBt,
1796c 26 70 4f 76 66 6c 2c 20 26 70 67 6e 6f 4f 76 66 &pOvfl, &pgnoOvf
1796d 6c 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 30 29 3b l, pgnoOvfl, 0);
1796e 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1796f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
17970 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 /* If the
17971 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72 74 database support
17972 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 2c 20 61 s auto-vacuum, a
17973 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20 6f 72 nd the second or
17974 20 73 75 62 73 65 71 75 65 6e 74 0a 20 20 20 20 subsequent.
17975 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 70 61 ** overflow pa
17976 67 65 20 69 73 20 62 65 69 6e 67 20 61 6c 6c 6f ge is being allo
17977 63 61 74 65 64 2c 20 61 64 64 20 61 6e 20 65 6e cated, add an en
17978 74 72 79 20 74 6f 20 74 68 65 20 70 6f 69 6e 74 try to the point
17979 65 72 2d 6d 61 70 0a 20 20 20 20 20 20 2a 2a 20 er-map. **
1797a 66 6f 72 20 74 68 61 74 20 70 61 67 65 20 6e 6f for that page no
1797b 77 2e 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 w. . **.
1797c 20 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69 73 ** If this is
1797d 20 74 68 65 20 66 69 72 73 74 20 6f 76 65 72 66 the first overf
1797e 6c 6f 77 20 70 61 67 65 2c 20 74 68 65 6e 20 77 low page, then w
1797f 72 69 74 65 20 61 20 70 61 72 74 69 61 6c 20 65 rite a partial e
17980 6e 74 72 79 20 0a 20 20 20 20 20 20 2a 2a 20 74 ntry . ** t
17981 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 o the pointer-ma
17982 70 2e 20 49 66 20 77 65 20 77 72 69 74 65 20 6e p. If we write n
17983 6f 74 68 69 6e 67 20 74 6f 20 74 68 69 73 20 70 othing to this p
17984 6f 69 6e 74 65 72 2d 6d 61 70 20 73 6c 6f 74 2c ointer-map slot,
17985 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 . ** then t
17986 68 65 20 6f 70 74 69 6d 69 73 74 69 63 20 6f 76 he optimistic ov
17987 65 72 66 6c 6f 77 20 63 68 61 69 6e 20 70 72 6f erflow chain pro
17988 63 65 73 73 69 6e 67 20 69 6e 20 63 6c 65 61 72 cessing in clear
17989 43 65 6c 6c 28 29 0a 20 20 20 20 20 20 2a 2a 20 Cell(). **
1798a 6d 61 79 20 6d 69 73 69 6e 74 65 72 70 72 65 74 may misinterpret
1798b 20 74 68 65 20 75 6e 69 6e 69 74 69 61 6c 69 73 the uninitialis
1798c 65 64 20 76 61 6c 75 65 73 20 61 6e 64 20 64 65 ed values and de
1798d 6c 65 74 65 20 74 68 65 0a 20 20 20 20 20 20 2a lete the. *
1798e 2a 20 77 72 6f 6e 67 20 70 61 67 65 73 20 66 72 * wrong pages fr
1798f 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 2e om the database.
17990 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
17991 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 if( pBt->autoVac
17992 75 75 6d 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 uum && rc==SQLIT
17993 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
17994 75 38 20 65 54 79 70 65 20 3d 20 28 70 67 6e 6f u8 eType = (pgno
17995 50 74 72 6d 61 70 3f 50 54 52 4d 41 50 5f 4f 56 Ptrmap?PTRMAP_OV
17996 45 52 46 4c 4f 57 32 3a 50 54 52 4d 41 50 5f 4f ERFLOW2:PTRMAP_O
17997 56 45 52 46 4c 4f 57 31 29 3b 0a 20 20 20 20 20 VERFLOW1);.
17998 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 ptrmapPut(pBt
17999 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 65 54 79 70 , pgnoOvfl, eTyp
1799a 65 2c 20 70 67 6e 6f 50 74 72 6d 61 70 2c 20 26 e, pgnoPtrmap, &
1799b 72 63 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 rc);. if(
1799c 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 rc ){.
1799d 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4f 76 releasePage(pOv
1799e 66 6c 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 fl);. }.
1799f 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 }.#endif.
179a0 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 if( rc ){.
179a1 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 releasePag
179a2 65 28 70 54 6f 52 65 6c 65 61 73 65 29 3b 0a 20 e(pToRelease);.
179a3 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
179a4 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 ;. }..
179a5 20 2f 2a 20 49 66 20 70 54 6f 52 65 6c 65 61 73 /* If pToReleas
179a6 65 20 69 73 20 6e 6f 74 20 7a 65 72 6f 20 74 68 e is not zero th
179a7 61 6e 20 70 50 72 69 6f 72 20 70 6f 69 6e 74 73 an pPrior points
179a8 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 20 61 into the data a
179a9 72 65 61 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 rea. ** of
179aa 70 54 6f 52 65 6c 65 61 73 65 2e 20 20 4d 61 6b pToRelease. Mak
179ab 65 20 73 75 72 65 20 70 54 6f 52 65 6c 65 61 73 e sure pToReleas
179ac 65 20 69 73 20 73 74 69 6c 6c 20 77 72 69 74 65 e is still write
179ad 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 61 able. */. a
179ae 73 73 65 72 74 28 20 70 54 6f 52 65 6c 65 61 73 ssert( pToReleas
179af 65 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 50 e==0 || sqlite3P
179b0 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 agerIswriteable(
179b1 70 54 6f 52 65 6c 65 61 73 65 2d 3e 70 44 62 50 pToRelease->pDbP
179b2 61 67 65 29 20 29 3b 0a 0a 20 20 20 20 20 20 2f age) );.. /
179b3 2a 20 49 66 20 70 50 72 69 6f 72 20 69 73 20 70 * If pPrior is p
179b4 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61 20 art of the data
179b5 61 72 65 61 20 6f 66 20 70 50 61 67 65 2c 20 74 area of pPage, t
179b6 68 65 6e 20 6d 61 6b 65 20 73 75 72 65 20 70 50 hen make sure pP
179b7 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 age. ** is
179b8 73 74 69 6c 6c 20 77 72 69 74 65 61 62 6c 65 20 still writeable
179b9 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 */. assert(
179ba 20 70 50 72 69 6f 72 3c 70 50 61 67 65 2d 3e 61 pPrior<pPage->a
179bb 44 61 74 61 20 7c 7c 20 70 50 72 69 6f 72 3e 3d Data || pPrior>=
179bc 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 &pPage->aData[pB
179bd 74 2d 3e 70 61 67 65 53 69 7a 65 5d 0a 20 20 20 t->pageSize].
179be 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 || sqli
179bf 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 te3PagerIswritea
179c0 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ble(pPage->pDbPa
179c1 67 65 29 20 29 3b 0a 0a 20 20 20 20 20 20 70 75 ge) );.. pu
179c2 74 34 62 79 74 65 28 70 50 72 69 6f 72 2c 20 70 t4byte(pPrior, p
179c3 67 6e 6f 4f 76 66 6c 29 3b 0a 20 20 20 20 20 20 gnoOvfl);.
179c4 72 65 6c 65 61 73 65 50 61 67 65 28 70 54 6f 52 releasePage(pToR
179c5 65 6c 65 61 73 65 29 3b 0a 20 20 20 20 20 20 70 elease);. p
179c6 54 6f 52 65 6c 65 61 73 65 20 3d 20 70 4f 76 66 ToRelease = pOvf
179c7 6c 3b 0a 20 20 20 20 20 20 70 50 72 69 6f 72 20 l;. pPrior
179c8 3d 20 70 4f 76 66 6c 2d 3e 61 44 61 74 61 3b 0a = pOvfl->aData;.
179c9 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 70 put4byte(p
179ca 50 72 69 6f 72 2c 20 30 29 3b 0a 20 20 20 20 20 Prior, 0);.
179cb 20 70 50 61 79 6c 6f 61 64 20 3d 20 26 70 4f 76 pPayload = &pOv
179cc 66 6c 2d 3e 61 44 61 74 61 5b 34 5d 3b 0a 20 20 fl->aData[4];.
179cd 20 20 20 20 73 70 61 63 65 4c 65 66 74 20 3d 20 spaceLeft =
179ce 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 pBt->usableSize
179cf 2d 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e - 4;. }. n
179d0 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 20 = nPayload;.
179d1 20 69 66 28 20 6e 3e 73 70 61 63 65 4c 65 66 74 if( n>spaceLeft
179d2 20 29 20 6e 20 3d 20 73 70 61 63 65 4c 65 66 74 ) n = spaceLeft
179d3 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70 54 6f ;.. /* If pTo
179d4 52 65 6c 65 61 73 65 20 69 73 20 6e 6f 74 20 7a Release is not z
179d5 65 72 6f 20 74 68 61 6e 20 70 50 61 79 6c 6f 61 ero than pPayloa
179d6 64 20 70 6f 69 6e 74 73 20 69 6e 74 6f 20 74 68 d points into th
179d7 65 20 64 61 74 61 20 61 72 65 61 0a 20 20 20 20 e data area.
179d8 2a 2a 20 6f 66 20 70 54 6f 52 65 6c 65 61 73 65 ** of pToRelease
179d9 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 70 54 6f . Make sure pTo
179da 52 65 6c 65 61 73 65 20 69 73 20 73 74 69 6c 6c Release is still
179db 20 77 72 69 74 65 61 62 6c 65 2e 20 2a 2f 0a 20 writeable. */.
179dc 20 20 20 61 73 73 65 72 74 28 20 70 54 6f 52 65 assert( pToRe
179dd 6c 65 61 73 65 3d 3d 30 20 7c 7c 20 73 71 6c 69 lease==0 || sqli
179de 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 te3PagerIswritea
179df 62 6c 65 28 70 54 6f 52 65 6c 65 61 73 65 2d 3e ble(pToRelease->
179e0 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 pDbPage) );..
179e1 20 2f 2a 20 49 66 20 70 50 61 79 6c 6f 61 64 20 /* If pPayload
179e2 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 is part of the d
179e3 61 74 61 20 61 72 65 61 20 6f 66 20 70 50 61 67 ata area of pPag
179e4 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20 73 75 72 e, then make sur
179e5 65 20 70 50 61 67 65 0a 20 20 20 20 2a 2a 20 69 e pPage. ** i
179e6 73 20 73 74 69 6c 6c 20 77 72 69 74 65 61 62 6c s still writeabl
179e7 65 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 e */. assert(
179e8 20 70 50 61 79 6c 6f 61 64 3c 70 50 61 67 65 2d pPayload<pPage-
179e9 3e 61 44 61 74 61 20 7c 7c 20 70 50 61 79 6c 6f >aData || pPaylo
179ea 61 64 3e 3d 26 70 50 61 67 65 2d 3e 61 44 61 74 ad>=&pPage->aDat
179eb 61 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d a[pBt->pageSize]
179ec 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 . ||
179ed 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 sqlite3PagerIswr
179ee 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 iteable(pPage->p
179ef 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 20 DbPage) );..
179f0 69 66 28 20 6e 53 72 63 3e 30 20 29 7b 0a 20 20 if( nSrc>0 ){.
179f1 20 20 20 20 69 66 28 20 6e 3e 6e 53 72 63 20 29 if( n>nSrc )
179f2 20 6e 20 3d 20 6e 53 72 63 3b 0a 20 20 20 20 20 n = nSrc;.
179f3 20 61 73 73 65 72 74 28 20 70 53 72 63 20 29 3b assert( pSrc );
179f4 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 50 . memcpy(pP
179f5 61 79 6c 6f 61 64 2c 20 70 53 72 63 2c 20 6e 29 ayload, pSrc, n)
179f6 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
179f7 20 20 20 6d 65 6d 73 65 74 28 70 50 61 79 6c 6f memset(pPaylo
179f8 61 64 2c 20 30 2c 20 6e 29 3b 0a 20 20 20 20 7d ad, 0, n);. }
179f9 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64 20 2d 3d . nPayload -=
179fa 20 6e 3b 0a 20 20 20 20 70 50 61 79 6c 6f 61 64 n;. pPayload
179fb 20 2b 3d 20 6e 3b 0a 20 20 20 20 70 53 72 63 20 += n;. pSrc
179fc 2b 3d 20 6e 3b 0a 20 20 20 20 6e 53 72 63 20 2d += n;. nSrc -
179fd 3d 20 6e 3b 0a 20 20 20 20 73 70 61 63 65 4c 65 = n;. spaceLe
179fe 66 74 20 2d 3d 20 6e 3b 0a 20 20 20 20 69 66 28 ft -= n;. if(
179ff 20 6e 53 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 nSrc==0 ){.
17a00 20 20 6e 53 72 63 20 3d 20 6e 44 61 74 61 3b 0a nSrc = nData;.
17a01 20 20 20 20 20 20 70 53 72 63 20 3d 20 70 44 61 pSrc = pDa
17a02 74 61 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 ta;. }. }.
17a03 72 65 6c 65 61 73 65 50 61 67 65 28 70 54 6f 52 releasePage(pToR
17a04 65 6c 65 61 73 65 29 3b 0a 20 20 72 65 74 75 72 elease);. retur
17a05 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
17a06 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65 /*.** Remove the
17a07 20 69 2d 74 68 20 63 65 6c 6c 20 66 72 6f 6d 20 i-th cell from
17a08 70 50 61 67 65 2e 20 20 54 68 69 73 20 72 6f 75 pPage. This rou
17a09 74 69 6e 65 20 65 66 66 65 63 74 73 20 70 50 61 tine effects pPa
17a0a 67 65 20 6f 6e 6c 79 2e 0a 2a 2a 20 54 68 65 20 ge only..** The
17a0b 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 cell content is
17a0c 6e 6f 74 20 66 72 65 65 64 20 6f 72 20 64 65 61 not freed or dea
17a0d 6c 6c 6f 63 61 74 65 64 2e 20 20 49 74 20 69 73 llocated. It is
17a0e 20 61 73 73 75 6d 65 64 20 74 68 61 74 0a 2a 2a assumed that.**
17a0f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e the cell conten
17a10 74 20 68 61 73 20 62 65 65 6e 20 63 6f 70 69 65 t has been copie
17a11 64 20 73 6f 6d 65 70 6c 61 63 65 20 65 6c 73 65 d someplace else
17a12 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 . This routine
17a13 6a 75 73 74 0a 2a 2a 20 72 65 6d 6f 76 65 73 20 just.** removes
17a14 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 74 6f the reference to
17a15 20 74 68 65 20 63 65 6c 6c 20 66 72 6f 6d 20 70 the cell from p
17a16 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 22 73 7a 22 Page..**.** "sz"
17a17 20 6d 75 73 74 20 62 65 20 74 68 65 20 6e 75 6d must be the num
17a18 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 ber of bytes in
17a19 74 68 65 20 63 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 the cell..*/.sta
17a1a 74 69 63 20 76 6f 69 64 20 64 72 6f 70 43 65 6c tic void dropCel
17a1b 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 l(MemPage *pPage
17a1c 2c 20 69 6e 74 20 69 64 78 2c 20 69 6e 74 20 73 , int idx, int s
17a1d 7a 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a 20 20 z, int *pRC){.
17a1e 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 int i;
17a1f 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 /* Loop counter
17a20 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20 20 20 20 */. int pc;
17a21 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 /* Offset t
17a22 6f 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 6f o cell content o
17a23 66 20 63 65 6c 6c 20 62 65 69 6e 67 20 64 65 6c f cell being del
17a24 65 74 65 64 20 2a 2f 0a 20 20 75 38 20 2a 64 61 eted */. u8 *da
17a25 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 70 50 61 ta; /* pPa
17a26 67 65 2d 3e 61 44 61 74 61 20 2a 2f 0a 20 20 75 ge->aData */. u
17a27 38 20 2a 70 74 72 3b 20 20 20 20 20 20 20 20 2f 8 *ptr; /
17a28 2a 20 55 73 65 64 20 74 6f 20 6d 6f 76 65 20 62 * Used to move b
17a29 79 74 65 73 20 61 72 6f 75 6e 64 20 77 69 74 68 ytes around with
17a2a 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 69 in data[] */. i
17a2b 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 2f nt rc; /
17a2c 2a 20 54 68 65 20 72 65 74 75 72 6e 20 63 6f 64 * The return cod
17a2d 65 20 2a 2f 0a 20 20 69 6e 74 20 68 64 72 3b 20 e */. int hdr;
17a2e 20 20 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 6e /* Beginn
17a2f 69 6e 67 20 6f 66 20 74 68 65 20 68 65 61 64 65 ing of the heade
17a30 72 2e 20 20 30 20 6d 6f 73 74 20 70 61 67 65 73 r. 0 most pages
17a31 2e 20 20 31 30 30 20 70 61 67 65 20 31 20 2a 2f . 100 page 1 */
17a32 0a 0a 20 20 69 66 28 20 2a 70 52 43 20 29 20 72 .. if( *pRC ) r
17a33 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74 eturn;.. assert
17a34 28 20 69 64 78 3e 3d 30 20 26 26 20 69 64 78 3c ( idx>=0 && idx<
17a35 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a pPage->nCell );.
17a36 20 20 61 73 73 65 72 74 28 20 73 7a 3d 3d 63 65 assert( sz==ce
17a37 6c 6c 53 69 7a 65 28 70 50 61 67 65 2c 20 69 64 llSize(pPage, id
17a38 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
17a39 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 sqlite3PagerIswr
17a3a 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 iteable(pPage->p
17a3b 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 DbPage) );. ass
17a3c 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
17a3d 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 ex_held(pPage->p
17a3e 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
17a3f 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 data = pPage->aD
17a40 61 74 61 3b 0a 20 20 70 74 72 20 3d 20 26 64 61 ata;. ptr = &da
17a41 74 61 5b 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 ta[pPage->cellOf
17a42 66 73 65 74 20 2b 20 32 2a 69 64 78 5d 3b 0a 20 fset + 2*idx];.
17a43 20 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 70 pc = get2byte(p
17a44 74 72 29 3b 0a 20 20 68 64 72 20 3d 20 70 50 61 tr);. hdr = pPa
17a45 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 ge->hdrOffset;.
17a46 20 74 65 73 74 63 61 73 65 28 20 70 63 3d 3d 67 testcase( pc==g
17a47 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 et2byte(&data[hd
17a48 72 2b 35 5d 29 20 29 3b 0a 20 20 74 65 73 74 63 r+5]) );. testc
17a49 61 73 65 28 20 70 63 2b 73 7a 3d 3d 70 50 61 67 ase( pc+sz==pPag
17a4a 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 e->pBt->usableSi
17a4b 7a 65 20 29 3b 0a 20 20 69 66 28 20 70 63 20 3c ze );. if( pc <
17a4c 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b get2byte(&data[
17a4d 68 64 72 2b 35 5d 29 20 7c 7c 20 70 63 2b 73 7a hdr+5]) || pc+sz
17a4e 20 3e 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 > pPage->pBt->u
17a4f 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 sableSize ){.
17a50 20 2a 70 52 43 20 3d 20 53 51 4c 49 54 45 5f 43 *pRC = SQLITE_C
17a51 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
17a52 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 72 return;. }. r
17a53 63 20 3d 20 66 72 65 65 53 70 61 63 65 28 70 50 c = freeSpace(pP
17a54 61 67 65 2c 20 70 63 2c 20 73 7a 29 3b 0a 20 20 age, pc, sz);.
17a55 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 2a 70 if( rc ){. *p
17a56 52 43 20 3d 20 72 63 3b 0a 20 20 20 20 72 65 74 RC = rc;. ret
17a57 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 urn;. }. for(i
17a58 3d 69 64 78 2b 31 3b 20 69 3c 70 50 61 67 65 2d =idx+1; i<pPage-
17a59 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 2c 20 70 74 72 >nCell; i++, ptr
17a5a 2b 3d 32 29 7b 0a 20 20 20 20 70 74 72 5b 30 5d +=2){. ptr[0]
17a5b 20 3d 20 70 74 72 5b 32 5d 3b 0a 20 20 20 20 70 = ptr[2];. p
17a5c 74 72 5b 31 5d 20 3d 20 70 74 72 5b 33 5d 3b 0a tr[1] = ptr[3];.
17a5d 20 20 7d 0a 20 20 70 50 61 67 65 2d 3e 6e 43 65 }. pPage->nCe
17a5e 6c 6c 2d 2d 3b 0a 20 20 70 75 74 32 62 79 74 65 ll--;. put2byte
17a5f 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 2c 20 70 (&data[hdr+3], p
17a60 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 3b 0a 20 20 Page->nCell);.
17a61 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 2b 3d 20 pPage->nFree +=
17a62 32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 2;.}../*.** Inse
17a63 72 74 20 61 20 6e 65 77 20 63 65 6c 6c 20 6f 6e rt a new cell on
17a64 20 70 50 61 67 65 20 61 74 20 63 65 6c 6c 20 69 pPage at cell i
17a65 6e 64 65 78 20 22 69 22 2e 20 20 70 43 65 6c 6c ndex "i". pCell
17a66 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 0a 2a points to the.*
17a67 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 * content of the
17a68 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 cell..**.** If
17a69 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 the cell content
17a6a 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20 74 68 65 will fit on the
17a6b 20 70 61 67 65 2c 20 74 68 65 6e 20 70 75 74 20 page, then put
17a6c 69 74 20 74 68 65 72 65 2e 20 20 49 66 20 69 74 it there. If it
17a6d 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74 .** will not fit
17a6e 2c 20 74 68 65 6e 20 6d 61 6b 65 20 61 20 63 6f , then make a co
17a6f 70 79 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 py of the cell c
17a70 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 70 54 65 6d ontent into pTem
17a71 70 20 69 66 0a 2a 2a 20 70 54 65 6d 70 20 69 73 p if.** pTemp is
17a72 20 6e 6f 74 20 6e 75 6c 6c 2e 20 20 52 65 67 61 not null. Rega
17a73 72 64 6c 65 73 73 20 6f 66 20 70 54 65 6d 70 2c rdless of pTemp,
17a74 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 allocate a new
17a75 65 6e 74 72 79 0a 2a 2a 20 69 6e 20 70 50 61 67 entry.** in pPag
17a76 65 2d 3e 61 4f 76 66 6c 5b 5d 20 61 6e 64 20 6d e->aOvfl[] and m
17a77 61 6b 65 20 69 74 20 70 6f 69 6e 74 20 74 6f 20 ake it point to
17a78 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 the cell content
17a79 20 28 65 69 74 68 65 72 0a 2a 2a 20 69 6e 20 70 (either.** in p
17a7a 54 65 6d 70 20 6f 72 20 74 68 65 20 6f 72 69 67 Temp or the orig
17a7b 69 6e 61 6c 20 70 43 65 6c 6c 29 20 61 6e 64 20 inal pCell) and
17a7c 61 6c 73 6f 20 72 65 63 6f 72 64 20 69 74 73 20 also record its
17a7d 69 6e 64 65 78 2e 20 0a 2a 2a 20 41 6c 6c 6f 63 index. .** Alloc
17a7e 61 74 69 6e 67 20 61 20 6e 65 77 20 65 6e 74 72 ating a new entr
17a7f 79 20 69 6e 20 70 50 61 67 65 2d 3e 61 43 65 6c y in pPage->aCel
17a80 6c 5b 5d 20 69 6d 70 6c 69 65 73 20 74 68 61 74 l[] implies that
17a81 20 0a 2a 2a 20 70 50 61 67 65 2d 3e 6e 4f 76 65 .** pPage->nOve
17a82 72 66 6c 6f 77 20 69 73 20 69 6e 63 72 65 6d 65 rflow is increme
17a83 6e 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e nted..**.** If n
17a84 53 6b 69 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f Skip is non-zero
17a85 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 63 6f , then do not co
17a86 70 79 20 74 68 65 20 66 69 72 73 74 20 6e 53 6b py the first nSk
17a87 69 70 20 62 79 74 65 73 20 6f 66 20 74 68 65 0a ip bytes of the.
17a88 2a 2a 20 63 65 6c 6c 2e 20 54 68 65 20 63 61 6c ** cell. The cal
17a89 6c 65 72 20 77 69 6c 6c 20 6f 76 65 72 77 72 69 ler will overwri
17a8a 74 65 20 74 68 65 6d 20 61 66 74 65 72 20 74 68 te them after th
17a8b 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 is function retu
17a8c 72 6e 73 2e 20 49 66 0a 2a 2a 20 6e 53 6b 69 70 rns. If.** nSkip
17a8d 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 is non-zero, th
17a8e 65 6e 20 70 43 65 6c 6c 20 6d 61 79 20 6e 6f 74 en pCell may not
17a8f 20 70 6f 69 6e 74 20 74 6f 20 61 6e 20 69 6e 76 point to an inv
17a90 61 6c 69 64 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 alid memory loca
17a91 74 69 6f 6e 20 0a 2a 2a 20 28 62 75 74 20 70 43 tion .** (but pC
17a92 65 6c 6c 2b 6e 53 6b 69 70 20 69 73 20 61 6c 77 ell+nSkip is alw
17a93 61 79 73 20 76 61 6c 69 64 29 2e 0a 2a 2f 0a 73 ays valid)..*/.s
17a94 74 61 74 69 63 20 76 6f 69 64 20 69 6e 73 65 72 tatic void inser
17a95 74 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 65 tCell(. MemPage
17a96 20 2a 70 50 61 67 65 2c 20 20 20 2f 2a 20 50 61 *pPage, /* Pa
17a97 67 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77 65 ge into which we
17a98 20 61 72 65 20 63 6f 70 79 69 6e 67 20 2a 2f 0a are copying */.
17a99 20 20 69 6e 74 20 69 2c 20 20 20 20 20 20 20 20 int i,
17a9a 20 20 20 20 2f 2a 20 4e 65 77 20 63 65 6c 6c 20 /* New cell
17a9b 62 65 63 6f 6d 65 73 20 74 68 65 20 69 2d 74 68 becomes the i-th
17a9c 20 63 65 6c 6c 20 6f 66 20 74 68 65 20 70 61 67 cell of the pag
17a9d 65 20 2a 2f 0a 20 20 75 38 20 2a 70 43 65 6c 6c e */. u8 *pCell
17a9e 2c 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 , /* Cont
17a9f 65 6e 74 20 6f 66 20 74 68 65 20 6e 65 77 20 63 ent of the new c
17aa0 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 2c ell */. int sz,
17aa1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 /* By
17aa2 74 65 73 20 6f 66 20 63 6f 6e 74 65 6e 74 20 69 tes of content i
17aa3 6e 20 70 43 65 6c 6c 20 2a 2f 0a 20 20 75 38 20 n pCell */. u8
17aa4 2a 70 54 65 6d 70 2c 20 20 20 20 20 20 20 20 2f *pTemp, /
17aa5 2a 20 54 65 6d 70 20 73 74 6f 72 61 67 65 20 73 * Temp storage s
17aa6 70 61 63 65 20 66 6f 72 20 70 43 65 6c 6c 2c 20 pace for pCell,
17aa7 69 66 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20 50 if needed */. P
17aa8 67 6e 6f 20 69 43 68 69 6c 64 2c 20 20 20 20 20 gno iChild,
17aa9 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f 2c /* If non-zero,
17aaa 20 72 65 70 6c 61 63 65 20 66 69 72 73 74 20 34 replace first 4
17aab 20 62 79 74 65 73 20 77 69 74 68 20 74 68 69 73 bytes with this
17aac 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69 6e 74 20 value */. int
17aad 2a 70 52 43 20 20 20 20 20 20 20 20 20 20 2f 2a *pRC /*
17aae 20 52 65 61 64 20 61 6e 64 20 77 72 69 74 65 20 Read and write
17aaf 72 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d return code from
17ab0 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e here */.){. in
17ab1 74 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 20 t idx;
17ab2 2f 2a 20 57 68 65 72 65 20 74 6f 20 77 72 69 74 /* Where to writ
17ab3 65 20 6e 65 77 20 63 65 6c 6c 20 63 6f 6e 74 65 e new cell conte
17ab4 6e 74 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a nt in data[] */.
17ab5 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 20 20 20 int j;
17ab6 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e /* Loop coun
17ab7 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 65 6e 64 ter */. int end
17ab8 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 ; /* Fi
17ab9 72 73 74 20 62 79 74 65 20 70 61 73 74 20 74 68 rst byte past th
17aba 65 20 6c 61 73 74 20 63 65 6c 6c 20 70 6f 69 6e e last cell poin
17abb 74 65 72 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f ter in data[] */
17abc 0a 20 20 69 6e 74 20 69 6e 73 3b 20 20 20 20 20 . int ins;
17abd 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e /* Index in
17abe 20 64 61 74 61 5b 5d 20 77 68 65 72 65 20 6e 65 data[] where ne
17abf 77 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 69 w cell pointer i
17ac0 73 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 s inserted */.
17ac1 69 6e 74 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20 int cellOffset;
17ac2 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 /* Address of
17ac3 66 69 72 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 first cell point
17ac4 65 72 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a er in data[] */.
17ac5 20 20 75 38 20 2a 64 61 74 61 3b 20 20 20 20 20 u8 *data;
17ac6 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 74 65 /* The conte
17ac7 6e 74 20 6f 66 20 74 68 65 20 77 68 6f 6c 65 20 nt of the whole
17ac8 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 70 74 page */. u8 *pt
17ac9 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 r; /* U
17aca 73 65 64 20 66 6f 72 20 6d 6f 76 69 6e 67 20 69 sed for moving i
17acb 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 72 6f 75 6e nformation aroun
17acc 64 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 0a d in data[] */..
17acd 20 20 69 6e 74 20 6e 53 6b 69 70 20 3d 20 28 69 int nSkip = (i
17ace 43 68 69 6c 64 20 3f 20 34 20 3a 20 30 29 3b 0a Child ? 4 : 0);.
17acf 0a 20 20 69 66 28 20 2a 70 52 43 20 29 20 72 65 . if( *pRC ) re
17ad0 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28 turn;.. assert(
17ad1 20 69 3e 3d 30 20 26 26 20 69 3c 3d 70 50 61 67 i>=0 && i<=pPag
17ad2 65 2d 3e 6e 43 65 6c 6c 2b 70 50 61 67 65 2d 3e e->nCell+pPage->
17ad3 6e 4f 76 65 72 66 6c 6f 77 20 29 3b 0a 20 20 61 nOverflow );. a
17ad4 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 43 ssert( pPage->nC
17ad5 65 6c 6c 3c 3d 4d 58 5f 43 45 4c 4c 28 70 50 61 ell<=MX_CELL(pPa
17ad6 67 65 2d 3e 70 42 74 29 20 26 26 20 4d 58 5f 43 ge->pBt) && MX_C
17ad7 45 4c 4c 28 70 50 61 67 65 2d 3e 70 42 74 29 3c ELL(pPage->pBt)<
17ad8 3d 35 34 36 30 20 29 3b 0a 20 20 61 73 73 65 72 =5460 );. asser
17ad9 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 t( pPage->nOverf
17ada 6c 6f 77 3c 3d 41 72 72 61 79 53 69 7a 65 28 70 low<=ArraySize(p
17adb 50 61 67 65 2d 3e 61 4f 76 66 6c 29 20 29 3b 0a Page->aOvfl) );.
17adc 20 20 61 73 73 65 72 74 28 20 73 7a 3d 3d 63 65 assert( sz==ce
17add 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c llSizePtr(pPage,
17ade 20 70 43 65 6c 6c 29 20 29 3b 0a 20 20 61 73 73 pCell) );. ass
17adf 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
17ae0 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 ex_held(pPage->p
17ae1 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
17ae2 69 66 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 if( pPage->nOver
17ae3 66 6c 6f 77 20 7c 7c 20 73 7a 2b 32 3e 70 50 61 flow || sz+2>pPa
17ae4 67 65 2d 3e 6e 46 72 65 65 20 29 7b 0a 20 20 20 ge->nFree ){.
17ae5 20 69 66 28 20 70 54 65 6d 70 20 29 7b 0a 20 20 if( pTemp ){.
17ae6 20 20 20 20 6d 65 6d 63 70 79 28 70 54 65 6d 70 memcpy(pTemp
17ae7 2b 6e 53 6b 69 70 2c 20 70 43 65 6c 6c 2b 6e 53 +nSkip, pCell+nS
17ae8 6b 69 70 2c 20 73 7a 2d 6e 53 6b 69 70 29 3b 0a kip, sz-nSkip);.
17ae9 20 20 20 20 20 20 70 43 65 6c 6c 20 3d 20 70 54 pCell = pT
17aea 65 6d 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 emp;. }. i
17aeb 66 28 20 69 43 68 69 6c 64 20 29 7b 0a 20 20 20 f( iChild ){.
17aec 20 20 20 70 75 74 34 62 79 74 65 28 70 43 65 6c put4byte(pCel
17aed 6c 2c 20 69 43 68 69 6c 64 29 3b 0a 20 20 20 20 l, iChild);.
17aee 7d 0a 20 20 20 20 6a 20 3d 20 70 50 61 67 65 2d }. j = pPage-
17aef 3e 6e 4f 76 65 72 66 6c 6f 77 2b 2b 3b 0a 20 20 >nOverflow++;.
17af0 20 20 61 73 73 65 72 74 28 20 6a 3c 28 69 6e 74 assert( j<(int
17af1 29 28 73 69 7a 65 6f 66 28 70 50 61 67 65 2d 3e )(sizeof(pPage->
17af2 61 4f 76 66 6c 29 2f 73 69 7a 65 6f 66 28 70 50 aOvfl)/sizeof(pP
17af3 61 67 65 2d 3e 61 4f 76 66 6c 5b 30 5d 29 29 20 age->aOvfl[0]))
17af4 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 61 4f );. pPage->aO
17af5 76 66 6c 5b 6a 5d 2e 70 43 65 6c 6c 20 3d 20 70 vfl[j].pCell = p
17af6 43 65 6c 6c 3b 0a 20 20 20 20 70 50 61 67 65 2d Cell;. pPage-
17af7 3e 61 4f 76 66 6c 5b 6a 5d 2e 69 64 78 20 3d 20 >aOvfl[j].idx =
17af8 28 75 31 36 29 69 3b 0a 20 20 7d 65 6c 73 65 7b (u16)i;. }else{
17af9 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71 . int rc = sq
17afa 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
17afb 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b pPage->pDbPage);
17afc 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
17afd 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
17afe 2a 70 52 43 20 3d 20 72 63 3b 0a 20 20 20 20 20 *pRC = rc;.
17aff 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 return;. }.
17b00 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 assert( sqlit
17b01 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
17b02 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 le(pPage->pDbPag
17b03 65 29 20 29 3b 0a 20 20 20 20 64 61 74 61 20 3d e) );. data =
17b04 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 pPage->aData;.
17b05 20 20 20 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 cellOffset =
17b06 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 pPage->cellOffse
17b07 74 3b 0a 20 20 20 20 65 6e 64 20 3d 20 63 65 6c t;. end = cel
17b08 6c 4f 66 66 73 65 74 20 2b 20 32 2a 70 50 61 67 lOffset + 2*pPag
17b09 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20 69 6e e->nCell;. in
17b0a 73 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b s = cellOffset +
17b0b 20 32 2a 69 3b 0a 20 20 20 20 72 63 20 3d 20 61 2*i;. rc = a
17b0c 6c 6c 6f 63 61 74 65 53 70 61 63 65 28 70 50 61 llocateSpace(pPa
17b0d 67 65 2c 20 73 7a 2c 20 26 69 64 78 29 3b 0a 20 ge, sz, &idx);.
17b0e 20 20 20 69 66 28 20 72 63 20 29 7b 20 2a 70 52 if( rc ){ *pR
17b0f 43 20 3d 20 72 63 3b 20 72 65 74 75 72 6e 3b 20 C = rc; return;
17b10 7d 0a 20 20 20 20 2f 2a 20 54 68 65 20 61 6c 6c }. /* The all
17b11 6f 63 61 74 65 53 70 61 63 65 28 29 20 72 6f 75 ocateSpace() rou
17b12 74 69 6e 65 20 67 75 61 72 61 6e 74 65 65 73 20 tine guarantees
17b13 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 the following tw
17b14 6f 20 70 72 6f 70 65 72 74 69 65 73 0a 20 20 20 o properties.
17b15 20 2a 2a 20 69 66 20 69 74 20 72 65 74 75 72 6e ** if it return
17b16 73 20 73 75 63 63 65 73 73 20 2a 2f 0a 20 20 20 s success */.
17b17 20 61 73 73 65 72 74 28 20 69 64 78 20 3e 3d 20 assert( idx >=
17b18 65 6e 64 2b 32 20 29 3b 0a 20 20 20 20 61 73 73 end+2 );. ass
17b19 65 72 74 28 20 69 64 78 2b 73 7a 20 3c 3d 20 70 ert( idx+sz <= p
17b1a 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c Page->pBt->usabl
17b1b 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 70 50 61 eSize );. pPa
17b1c 67 65 2d 3e 6e 43 65 6c 6c 2b 2b 3b 0a 20 20 20 ge->nCell++;.
17b1d 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 2d 3d pPage->nFree -=
17b1e 20 28 75 31 36 29 28 32 20 2b 20 73 7a 29 3b 0a (u16)(2 + sz);.
17b1f 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61 memcpy(&data
17b20 5b 69 64 78 2b 6e 53 6b 69 70 5d 2c 20 70 43 65 [idx+nSkip], pCe
17b21 6c 6c 2b 6e 53 6b 69 70 2c 20 73 7a 2d 6e 53 6b ll+nSkip, sz-nSk
17b22 69 70 29 3b 0a 20 20 20 20 69 66 28 20 69 43 68 ip);. if( iCh
17b23 69 6c 64 20 29 7b 0a 20 20 20 20 20 20 70 75 74 ild ){. put
17b24 34 62 79 74 65 28 26 64 61 74 61 5b 69 64 78 5d 4byte(&data[idx]
17b25 2c 20 69 43 68 69 6c 64 29 3b 0a 20 20 20 20 7d , iChild);. }
17b26 0a 20 20 20 20 66 6f 72 28 6a 3d 65 6e 64 2c 20 . for(j=end,
17b27 70 74 72 3d 26 64 61 74 61 5b 6a 5d 3b 20 6a 3e ptr=&data[j]; j>
17b28 69 6e 73 3b 20 6a 2d 3d 32 2c 20 70 74 72 2d 3d ins; j-=2, ptr-=
17b29 32 29 7b 0a 20 20 20 20 20 20 70 74 72 5b 30 5d 2){. ptr[0]
17b2a 20 3d 20 70 74 72 5b 2d 32 5d 3b 0a 20 20 20 20 = ptr[-2];.
17b2b 20 20 70 74 72 5b 31 5d 20 3d 20 70 74 72 5b 2d ptr[1] = ptr[-
17b2c 31 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 75 1];. }. pu
17b2d 74 32 62 79 74 65 28 26 64 61 74 61 5b 69 6e 73 t2byte(&data[ins
17b2e 5d 2c 20 69 64 78 29 3b 0a 20 20 20 20 70 75 74 ], idx);. put
17b2f 32 62 79 74 65 28 26 64 61 74 61 5b 70 50 61 67 2byte(&data[pPag
17b30 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 33 5d 2c e->hdrOffset+3],
17b31 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 3b 0a pPage->nCell);.
17b32 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
17b33 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
17b34 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 42 if( pPage->pB
17b35 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b t->autoVacuum ){
17b36 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 65 . /* The ce
17b37 6c 6c 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 61 ll may contain a
17b38 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 6f pointer to an o
17b39 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 20 49 66 verflow page. If
17b3a 20 73 6f 2c 20 77 72 69 74 65 0a 20 20 20 20 20 so, write.
17b3b 20 2a 2a 20 74 68 65 20 65 6e 74 72 79 20 66 6f ** the entry fo
17b3c 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 r the overflow p
17b3d 61 67 65 20 69 6e 74 6f 20 74 68 65 20 70 6f 69 age into the poi
17b3e 6e 74 65 72 20 6d 61 70 2e 0a 20 20 20 20 20 20 nter map..
17b3f 2a 2f 0a 20 20 20 20 20 20 70 74 72 6d 61 70 50 */. ptrmapP
17b40 75 74 4f 76 66 6c 50 74 72 28 70 50 61 67 65 2c utOvflPtr(pPage,
17b41 20 70 43 65 6c 6c 2c 20 70 52 43 29 3b 0a 20 20 pCell, pRC);.
17b42 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 7d }.#endif. }.}
17b43 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6c 69 ../*.** Add a li
17b44 73 74 20 6f 66 20 63 65 6c 6c 73 20 74 6f 20 61 st of cells to a
17b45 20 70 61 67 65 2e 20 20 54 68 65 20 70 61 67 65 page. The page
17b46 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 69 74 69 should be initi
17b47 61 6c 6c 79 20 65 6d 70 74 79 2e 0a 2a 2a 20 54 ally empty..** T
17b48 68 65 20 63 65 6c 6c 73 20 61 72 65 20 67 75 61 he cells are gua
17b49 72 61 6e 74 65 65 64 20 74 6f 20 66 69 74 20 6f ranteed to fit o
17b4a 6e 20 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 73 n the page..*/.s
17b4b 74 61 74 69 63 20 76 6f 69 64 20 61 73 73 65 6d tatic void assem
17b4c 62 6c 65 50 61 67 65 28 0a 20 20 4d 65 6d 50 61 blePage(. MemPa
17b4d 67 65 20 2a 70 50 61 67 65 2c 20 20 20 2f 2a 20 ge *pPage, /*
17b4e 54 68 65 20 70 61 67 65 20 74 6f 20 62 65 20 61 The page to be a
17b4f 73 73 65 6d 62 6c 69 65 64 20 2a 2f 0a 20 20 69 ssemblied */. i
17b50 6e 74 20 6e 43 65 6c 6c 2c 20 20 20 20 20 20 20 nt nCell,
17b51 20 2f 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f /* The number o
17b52 66 20 63 65 6c 6c 73 20 74 6f 20 61 64 64 20 74 f cells to add t
17b53 6f 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 o this page */.
17b54 20 75 38 20 2a 2a 61 70 43 65 6c 6c 2c 20 20 20 u8 **apCell,
17b55 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74 /* Pointers t
17b56 6f 20 63 65 6c 6c 20 62 6f 64 69 65 73 20 2a 2f o cell bodies */
17b57 0a 20 20 75 31 36 20 2a 61 53 69 7a 65 20 20 20 . u16 *aSize
17b58 20 20 20 20 20 2f 2a 20 53 69 7a 65 73 20 6f 66 /* Sizes of
17b59 20 74 68 65 20 63 65 6c 6c 73 20 2a 2f 0a 29 7b the cells */.){
17b5a 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 . int i;
17b5b 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 /* Loop cou
17b5c 6e 74 65 72 20 2a 2f 0a 20 20 75 38 20 2a 70 43 nter */. u8 *pC
17b5d 65 6c 6c 70 74 72 3b 20 20 20 20 20 2f 2a 20 41 ellptr; /* A
17b5e 64 64 72 65 73 73 20 6f 66 20 6e 65 78 74 20 63 ddress of next c
17b5f 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 ell pointer */.
17b60 20 69 6e 74 20 63 65 6c 6c 62 6f 64 79 3b 20 20 int cellbody;
17b61 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 /* Address of
17b62 20 6e 65 78 74 20 63 65 6c 6c 20 62 6f 64 79 20 next cell body
17b63 2a 2f 0a 20 20 75 38 20 2a 20 63 6f 6e 73 74 20 */. u8 * const
17b64 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 data = pPage->aD
17b65 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 ata;
17b66 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 /* Pointer to d
17b67 61 74 61 20 66 6f 72 20 70 50 61 67 65 20 2a 2f ata for pPage */
17b68 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 68 64 72 . const int hdr
17b69 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 = pPage->hdrOff
17b6a 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f set; /
17b6b 2a 20 4f 66 66 73 65 74 20 6f 66 20 68 65 61 64 * Offset of head
17b6c 65 72 20 6f 6e 20 70 50 61 67 65 20 2a 2f 0a 20 er on pPage */.
17b6d 20 63 6f 6e 73 74 20 69 6e 74 20 6e 55 73 61 62 const int nUsab
17b6e 6c 65 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d le = pPage->pBt-
17b6f 3e 75 73 61 62 6c 65 53 69 7a 65 3b 20 2f 2a 20 >usableSize; /*
17b70 55 73 61 62 6c 65 20 73 69 7a 65 20 6f 66 20 70 Usable size of p
17b71 61 67 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 age */.. assert
17b72 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c ( pPage->nOverfl
17b73 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 ow==0 );. asser
17b74 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
17b75 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 _held(pPage->pBt
17b76 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
17b77 73 65 72 74 28 20 6e 43 65 6c 6c 3e 3d 30 20 26 sert( nCell>=0 &
17b78 26 20 6e 43 65 6c 6c 3c 3d 4d 58 5f 43 45 4c 4c & nCell<=MX_CELL
17b79 28 70 50 61 67 65 2d 3e 70 42 74 29 20 26 26 20 (pPage->pBt) &&
17b7a 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e 70 MX_CELL(pPage->p
17b7b 42 74 29 3c 3d 35 34 36 30 20 29 3b 0a 20 20 61 Bt)<=5460 );. a
17b7c 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
17b7d 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 gerIswriteable(p
17b7e 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 Page->pDbPage) )
17b7f 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 ;.. /* Check th
17b80 61 74 20 74 68 65 20 70 61 67 65 20 68 61 73 20 at the page has
17b81 6a 75 73 74 20 62 65 65 6e 20 7a 65 72 6f 65 64 just been zeroed
17b82 20 62 79 20 7a 65 72 6f 50 61 67 65 28 29 20 2a by zeroPage() *
17b83 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 /. assert( pPag
17b84 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 e->nCell==0 );.
17b85 20 61 73 73 65 72 74 28 20 67 65 74 32 62 79 74 assert( get2byt
17b86 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3d e(&data[hdr+5])=
17b87 3d 6e 55 73 61 62 6c 65 20 29 3b 0a 0a 20 20 70 =nUsable );.. p
17b88 43 65 6c 6c 70 74 72 20 3d 20 26 64 61 74 61 5b Cellptr = &data[
17b89 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 pPage->cellOffse
17b8a 74 20 2b 20 6e 43 65 6c 6c 2a 32 5d 3b 0a 20 20 t + nCell*2];.
17b8b 63 65 6c 6c 62 6f 64 79 20 3d 20 6e 55 73 61 62 cellbody = nUsab
17b8c 6c 65 3b 0a 20 20 66 6f 72 28 69 3d 6e 43 65 6c le;. for(i=nCel
17b8d 6c 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b l-1; i>=0; i--){
17b8e 0a 20 20 20 20 70 43 65 6c 6c 70 74 72 20 2d 3d . pCellptr -=
17b8f 20 32 3b 0a 20 20 20 20 63 65 6c 6c 62 6f 64 79 2;. cellbody
17b90 20 2d 3d 20 61 53 69 7a 65 5b 69 5d 3b 0a 20 20 -= aSize[i];.
17b91 20 20 70 75 74 32 62 79 74 65 28 70 43 65 6c 6c put2byte(pCell
17b92 70 74 72 2c 20 63 65 6c 6c 62 6f 64 79 29 3b 0a ptr, cellbody);.
17b93 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61 memcpy(&data
17b94 5b 63 65 6c 6c 62 6f 64 79 5d 2c 20 61 70 43 65 [cellbody], apCe
17b95 6c 6c 5b 69 5d 2c 20 61 53 69 7a 65 5b 69 5d 29 ll[i], aSize[i])
17b96 3b 0a 20 20 7d 0a 20 20 70 75 74 32 62 79 74 65 ;. }. put2byte
17b97 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 2c 20 6e (&data[hdr+3], n
17b98 43 65 6c 6c 29 3b 0a 20 20 70 75 74 32 62 79 74 Cell);. put2byt
17b99 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 e(&data[hdr+5],
17b9a 63 65 6c 6c 62 6f 64 79 29 3b 0a 20 20 70 50 61 cellbody);. pPa
17b9b 67 65 2d 3e 6e 46 72 65 65 20 2d 3d 20 28 6e 43 ge->nFree -= (nC
17b9c 65 6c 6c 2a 32 20 2b 20 6e 55 73 61 62 6c 65 20 ell*2 + nUsable
17b9d 2d 20 63 65 6c 6c 62 6f 64 79 29 3b 0a 20 20 70 - cellbody);. p
17b9e 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 28 75 Page->nCell = (u
17b9f 31 36 29 6e 43 65 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 16)nCell;.}../*.
17ba0 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
17ba1 20 70 61 72 61 6d 65 74 65 72 73 20 64 65 74 65 parameters dete
17ba2 72 6d 69 6e 65 20 68 6f 77 20 6d 61 6e 79 20 61 rmine how many a
17ba3 64 6a 61 63 65 6e 74 20 70 61 67 65 73 20 67 65 djacent pages ge
17ba4 74 20 69 6e 76 6f 6c 76 65 64 0a 2a 2a 20 69 6e t involved.** in
17ba5 20 61 20 62 61 6c 61 6e 63 69 6e 67 20 6f 70 65 a balancing ope
17ba6 72 61 74 69 6f 6e 2e 20 20 4e 4e 20 69 73 20 74 ration. NN is t
17ba7 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6e 65 69 he number of nei
17ba8 67 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 ghbors on either
17ba9 20 73 69 64 65 0a 2a 2a 20 6f 66 20 74 68 65 20 side.** of the
17baa 70 61 67 65 20 74 68 61 74 20 70 61 72 74 69 63 page that partic
17bab 69 70 61 74 65 20 69 6e 20 74 68 65 20 62 61 6c ipate in the bal
17bac 61 6e 63 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e ancing operation
17bad 2e 20 20 4e 42 20 69 73 20 74 68 65 0a 2a 2a 20 . NB is the.**
17bae 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 total number of
17baf 70 61 67 65 73 20 74 68 61 74 20 70 61 72 74 69 pages that parti
17bb0 63 69 70 61 74 65 2c 20 69 6e 63 6c 75 64 69 6e cipate, includin
17bb1 67 20 74 68 65 20 74 61 72 67 65 74 20 70 61 67 g the target pag
17bb2 65 20 61 6e 64 0a 2a 2a 20 4e 4e 20 6e 65 69 67 e and.** NN neig
17bb3 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 20 hbors on either
17bb4 73 69 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 side..**.** The
17bb5 6d 69 6e 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 minimum value of
17bb6 20 4e 4e 20 69 73 20 31 20 28 6f 66 20 63 6f 75 NN is 1 (of cou
17bb7 72 73 65 29 2e 20 20 49 6e 63 72 65 61 73 69 6e rse). Increasin
17bb8 67 20 4e 4e 20 61 62 6f 76 65 20 31 0a 2a 2a 20 g NN above 1.**
17bb9 28 74 6f 20 32 20 6f 72 20 33 29 20 67 69 76 65 (to 2 or 3) give
17bba 73 20 61 20 6d 6f 64 65 73 74 20 69 6d 70 72 6f s a modest impro
17bbb 76 65 6d 65 6e 74 20 69 6e 20 53 45 4c 45 43 54 vement in SELECT
17bbc 20 61 6e 64 20 44 45 4c 45 54 45 20 70 65 72 66 and DELETE perf
17bbd 6f 72 6d 61 6e 63 65 0a 2a 2a 20 69 6e 20 65 78 ormance.** in ex
17bbe 63 68 61 6e 67 65 20 66 6f 72 20 61 20 6c 61 72 change for a lar
17bbf 67 65 72 20 64 65 67 72 61 64 61 74 69 6f 6e 20 ger degradation
17bc0 69 6e 20 49 4e 53 45 52 54 20 61 6e 64 20 55 50 in INSERT and UP
17bc1 44 41 54 45 20 70 65 72 66 6f 72 6d 61 6e 63 65 DATE performance
17bc2 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f ..** The value o
17bc3 66 20 4e 4e 20 61 70 70 65 61 72 73 20 74 6f 20 f NN appears to
17bc4 67 69 76 65 20 74 68 65 20 62 65 73 74 20 72 65 give the best re
17bc5 73 75 6c 74 73 20 6f 76 65 72 61 6c 6c 2e 0a 2a sults overall..*
17bc6 2f 0a 23 64 65 66 69 6e 65 20 4e 4e 20 31 20 20 /.#define NN 1
17bc7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
17bc8 6d 62 65 72 20 6f 66 20 6e 65 69 67 68 62 6f 72 mber of neighbor
17bc9 73 20 6f 6e 20 65 69 74 68 65 72 20 73 69 64 65 s on either side
17bca 20 6f 66 20 70 50 61 67 65 20 2a 2f 0a 23 64 65 of pPage */.#de
17bcb 66 69 6e 65 20 4e 42 20 28 4e 4e 2a 32 2b 31 29 fine NB (NN*2+1)
17bcc 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 70 /* Total p
17bcd 61 67 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e ages involved in
17bce 20 74 68 65 20 62 61 6c 61 6e 63 65 20 2a 2f 0a the balance */.
17bcf 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
17bd0 5f 4f 4d 49 54 5f 51 55 49 43 4b 42 41 4c 41 4e _OMIT_QUICKBALAN
17bd1 43 45 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 CE./*.** This ve
17bd2 72 73 69 6f 6e 20 6f 66 20 62 61 6c 61 6e 63 65 rsion of balance
17bd3 28 29 20 68 61 6e 64 6c 65 73 20 74 68 65 20 63 () handles the c
17bd4 6f 6d 6d 6f 6e 20 73 70 65 63 69 61 6c 20 63 61 ommon special ca
17bd5 73 65 20 77 68 65 72 65 0a 2a 2a 20 61 20 6e 65 se where.** a ne
17bd6 77 20 65 6e 74 72 79 20 69 73 20 62 65 69 6e 67 w entry is being
17bd7 20 69 6e 73 65 72 74 65 64 20 6f 6e 20 74 68 65 inserted on the
17bd8 20 65 78 74 72 65 6d 65 20 72 69 67 68 74 2d 65 extreme right-e
17bd9 6e 64 20 6f 66 20 74 68 65 0a 2a 2a 20 74 72 65 nd of the.** tre
17bda 65 2c 20 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 e, in other word
17bdb 73 2c 20 77 68 65 6e 20 74 68 65 20 6e 65 77 20 s, when the new
17bdc 65 6e 74 72 79 20 77 69 6c 6c 20 62 65 63 6f 6d entry will becom
17bdd 65 20 74 68 65 20 6c 61 72 67 65 73 74 0a 2a 2a e the largest.**
17bde 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 72 entry in the tr
17bdf 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 73 74 65 61 ee..**.** Instea
17be0 64 20 6f 66 20 74 72 79 69 6e 67 20 74 6f 20 62 d of trying to b
17be1 61 6c 61 6e 63 65 20 74 68 65 20 33 20 72 69 67 alance the 3 rig
17be2 68 74 2d 6d 6f 73 74 20 6c 65 61 66 20 70 61 67 ht-most leaf pag
17be3 65 73 2c 20 6a 75 73 74 20 61 64 64 0a 2a 2a 20 es, just add.**
17be4 61 20 6e 65 77 20 70 61 67 65 20 74 6f 20 74 68 a new page to th
17be5 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 e right-hand sid
17be6 65 20 61 6e 64 20 70 75 74 20 74 68 65 20 6f 6e e and put the on
17be7 65 20 6e 65 77 20 65 6e 74 72 79 20 69 6e 0a 2a e new entry in.*
17be8 2a 20 74 68 61 74 20 70 61 67 65 2e 20 20 54 68 * that page. Th
17be9 69 73 20 6c 65 61 76 65 73 20 74 68 65 20 72 69 is leaves the ri
17bea 67 68 74 20 73 69 64 65 20 6f 66 20 74 68 65 20 ght side of the
17beb 74 72 65 65 20 73 6f 6d 65 77 68 61 74 0a 2a 2a tree somewhat.**
17bec 20 75 6e 62 61 6c 61 6e 63 65 64 2e 20 20 42 75 unbalanced. Bu
17bed 74 20 6f 64 64 73 20 61 72 65 20 74 68 61 74 20 t odds are that
17bee 77 65 20 77 69 6c 6c 20 62 65 20 69 6e 73 65 72 we will be inser
17bef 74 69 6e 67 20 6e 65 77 20 65 6e 74 72 69 65 73 ting new entries
17bf0 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20 73 .** at the end s
17bf1 6f 6f 6e 20 61 66 74 65 72 77 61 72 64 73 20 73 oon afterwards s
17bf2 6f 20 74 68 65 20 6e 65 61 72 6c 79 20 65 6d 70 o the nearly emp
17bf3 74 79 20 70 61 67 65 20 77 69 6c 6c 20 71 75 69 ty page will qui
17bf4 63 6b 6c 79 0a 2a 2a 20 66 69 6c 6c 20 75 70 2e ckly.** fill up.
17bf5 20 20 4f 6e 20 61 76 65 72 61 67 65 2e 0a 2a 2a On average..**
17bf6 0a 2a 2a 20 70 50 61 67 65 20 69 73 20 74 68 65 .** pPage is the
17bf7 20 6c 65 61 66 20 70 61 67 65 20 77 68 69 63 68 leaf page which
17bf8 20 69 73 20 74 68 65 20 72 69 67 68 74 2d 6d 6f is the right-mo
17bf9 73 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 74 st page in the t
17bfa 72 65 65 2e 0a 2a 2a 20 70 50 61 72 65 6e 74 20 ree..** pParent
17bfb 69 73 20 69 74 73 20 70 61 72 65 6e 74 2e 20 20 is its parent.
17bfc 70 50 61 67 65 20 6d 75 73 74 20 68 61 76 65 20 pPage must have
17bfd 61 20 73 69 6e 67 6c 65 20 6f 76 65 72 66 6c 6f a single overflo
17bfe 77 20 65 6e 74 72 79 0a 2a 2a 20 77 68 69 63 68 w entry.** which
17bff 20 69 73 20 61 6c 73 6f 20 74 68 65 20 72 69 67 is also the rig
17c00 68 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 6f 6e ht-most entry on
17c01 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a the page..**.**
17c02 20 54 68 65 20 70 53 70 61 63 65 20 62 75 66 66 The pSpace buff
17c03 65 72 20 69 73 20 75 73 65 64 20 74 6f 20 73 74 er is used to st
17c04 6f 72 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 ore a temporary
17c05 63 6f 70 79 20 6f 66 20 74 68 65 20 64 69 76 69 copy of the divi
17c06 64 65 72 0a 2a 2a 20 63 65 6c 6c 20 74 68 61 74 der.** cell that
17c07 20 77 69 6c 6c 20 62 65 20 69 6e 73 65 72 74 65 will be inserte
17c08 64 20 69 6e 74 6f 20 70 50 61 72 65 6e 74 2e 20 d into pParent.
17c09 53 75 63 68 20 61 20 63 65 6c 6c 20 63 6f 6e 73 Such a cell cons
17c0a 69 73 74 73 20 6f 66 20 61 20 34 0a 2a 2a 20 62 ists of a 4.** b
17c0b 79 74 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 yte page number
17c0c 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 20 76 61 followed by a va
17c0d 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e riable length in
17c0e 74 65 67 65 72 2e 20 49 6e 20 6f 74 68 65 72 0a teger. In other.
17c0f 2a 2a 20 77 6f 72 64 73 2c 20 61 74 20 6d 6f 73 ** words, at mos
17c10 74 20 31 33 20 62 79 74 65 73 2e 20 48 65 6e 63 t 13 bytes. Henc
17c11 65 20 74 68 65 20 70 53 70 61 63 65 20 62 75 66 e the pSpace buf
17c12 66 65 72 20 6d 75 73 74 20 62 65 20 61 74 0a 2a fer must be at.*
17c13 2a 20 6c 65 61 73 74 20 31 33 20 62 79 74 65 73 * least 13 bytes
17c14 20 69 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 in size..*/.sta
17c15 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f tic int balance_
17c16 71 75 69 63 6b 28 4d 65 6d 50 61 67 65 20 2a 70 quick(MemPage *p
17c17 50 61 72 65 6e 74 2c 20 4d 65 6d 50 61 67 65 20 Parent, MemPage
17c18 2a 70 50 61 67 65 2c 20 75 38 20 2a 70 53 70 61 *pPage, u8 *pSpa
17c19 63 65 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 ce){. BtShared
17c1a 2a 63 6f 6e 73 74 20 70 42 74 20 3d 20 70 50 61 *const pBt = pPa
17c1b 67 65 2d 3e 70 42 74 3b 20 20 20 20 2f 2a 20 42 ge->pBt; /* B
17c1c 2d 54 72 65 65 20 44 61 74 61 62 61 73 65 20 2a -Tree Database *
17c1d 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 /. MemPage *pNe
17c1e 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 w;
17c1f 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 77 6c /* Newl
17c20 79 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 y allocated page
17c21 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 */. int rc;
17c22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17c23 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
17c24 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 50 turn Code */. P
17c25 67 6e 6f 20 70 67 6e 6f 4e 65 77 3b 20 20 20 20 gno pgnoNew;
17c26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17c27 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 /* Page numb
17c28 65 72 20 6f 66 20 70 4e 65 77 20 2a 2f 0a 0a 20 er of pNew */..
17c29 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
17c2a 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 _mutex_held(pPag
17c2b 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 e->pBt->mutex) )
17c2c 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
17c2d 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 te3PagerIswritea
17c2e 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e 70 44 62 ble(pParent->pDb
17c2f 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 Page) );. asser
17c30 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 t( pPage->nOverf
17c31 6c 6f 77 3d 3d 31 20 29 3b 0a 0a 20 20 69 66 28 low==1 );.. if(
17c32 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3c 3d 30 pPage->nCell<=0
17c33 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ) return SQLITE
17c34 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 0a _CORRUPT_BKPT;..
17c35 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 /* Allocate a
17c36 6e 65 77 20 70 61 67 65 2e 20 54 68 69 73 20 70 new page. This p
17c37 61 67 65 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 age will become
17c38 74 68 65 20 72 69 67 68 74 2d 73 69 62 6c 69 6e the right-siblin
17c39 67 20 6f 66 20 0a 20 20 2a 2a 20 70 50 61 67 65 g of . ** pPage
17c3a 2e 20 4d 61 6b 65 20 74 68 65 20 70 61 72 65 6e . Make the paren
17c3b 74 20 70 61 67 65 20 77 72 69 74 61 62 6c 65 2c t page writable,
17c3c 20 73 6f 20 74 68 61 74 20 74 68 65 20 6e 65 77 so that the new
17c3d 20 64 69 76 69 64 65 72 20 63 65 6c 6c 0a 20 20 divider cell.
17c3e 2a 2a 20 6d 61 79 20 62 65 20 69 6e 73 65 72 74 ** may be insert
17c3f 65 64 2e 20 49 66 20 62 6f 74 68 20 74 68 65 73 ed. If both thes
17c40 65 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65 e operations are
17c41 20 73 75 63 63 65 73 73 66 75 6c 2c 20 70 72 6f successful, pro
17c42 63 65 65 64 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 ceed.. */. rc
17c43 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 = allocateBtreeP
17c44 61 67 65 28 70 42 74 2c 20 26 70 4e 65 77 2c 20 age(pBt, &pNew,
17c45 26 70 67 6e 6f 4e 65 77 2c 20 30 2c 20 30 29 3b &pgnoNew, 0, 0);
17c46 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 .. if( rc==SQLI
17c47 54 45 5f 4f 4b 20 29 7b 0a 0a 20 20 20 20 75 38 TE_OK ){.. u8
17c48 20 2a 70 4f 75 74 20 3d 20 26 70 53 70 61 63 65 *pOut = &pSpace
17c49 5b 34 5d 3b 0a 20 20 20 20 75 38 20 2a 70 43 65 [4];. u8 *pCe
17c4a 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 61 4f 76 66 ll = pPage->aOvf
17c4b 6c 5b 30 5d 2e 70 43 65 6c 6c 3b 0a 20 20 20 20 l[0].pCell;.
17c4c 75 31 36 20 73 7a 43 65 6c 6c 20 3d 20 63 65 6c u16 szCell = cel
17c4d 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 lSizePtr(pPage,
17c4e 70 43 65 6c 6c 29 3b 0a 20 20 20 20 75 38 20 2a pCell);. u8 *
17c4f 70 53 74 6f 70 3b 0a 0a 20 20 20 20 61 73 73 65 pStop;.. asse
17c50 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 rt( sqlite3Pager
17c51 49 73 77 72 69 74 65 61 62 6c 65 28 70 4e 65 77 Iswriteable(pNew
17c52 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 ->pDbPage) );.
17c53 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
17c54 3e 61 44 61 74 61 5b 30 5d 3d 3d 28 50 54 46 5f >aData[0]==(PTF_
17c55 49 4e 54 4b 45 59 7c 50 54 46 5f 4c 45 41 46 44 INTKEY|PTF_LEAFD
17c56 41 54 41 7c 50 54 46 5f 4c 45 41 46 29 20 29 3b ATA|PTF_LEAF) );
17c57 0a 20 20 20 20 7a 65 72 6f 50 61 67 65 28 70 4e . zeroPage(pN
17c58 65 77 2c 20 50 54 46 5f 49 4e 54 4b 45 59 7c 50 ew, PTF_INTKEY|P
17c59 54 46 5f 4c 45 41 46 44 41 54 41 7c 50 54 46 5f TF_LEAFDATA|PTF_
17c5a 4c 45 41 46 29 3b 0a 20 20 20 20 61 73 73 65 6d LEAF);. assem
17c5b 62 6c 65 50 61 67 65 28 70 4e 65 77 2c 20 31 2c blePage(pNew, 1,
17c5c 20 26 70 43 65 6c 6c 2c 20 26 73 7a 43 65 6c 6c &pCell, &szCell
17c5d 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 );.. /* If th
17c5e 69 73 20 69 73 20 61 6e 20 61 75 74 6f 2d 76 61 is is an auto-va
17c5f 63 75 75 6d 20 64 61 74 61 62 61 73 65 2c 20 75 cuum database, u
17c60 70 64 61 74 65 20 74 68 65 20 70 6f 69 6e 74 65 pdate the pointe
17c61 72 20 6d 61 70 0a 20 20 20 20 2a 2a 20 77 69 74 r map. ** wit
17c62 68 20 65 6e 74 72 69 65 73 20 66 6f 72 20 74 68 h entries for th
17c63 65 20 6e 65 77 20 70 61 67 65 2c 20 61 6e 64 20 e new page, and
17c64 61 6e 79 20 70 6f 69 6e 74 65 72 20 66 72 6f 6d any pointer from
17c65 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 63 65 6c the . ** cel
17c66 6c 20 6f 6e 20 74 68 65 20 70 61 67 65 20 74 6f l on the page to
17c67 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 an overflow pag
17c68 65 2e 20 49 66 20 65 69 74 68 65 72 20 6f 66 20 e. If either of
17c69 74 68 65 73 65 0a 20 20 20 20 2a 2a 20 6f 70 65 these. ** ope
17c6a 72 61 74 69 6f 6e 73 20 66 61 69 6c 73 2c 20 74 rations fails, t
17c6b 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 69 he return code i
17c6c 73 20 73 65 74 2c 20 62 75 74 20 74 68 65 20 63 s set, but the c
17c6d 6f 6e 74 65 6e 74 73 0a 20 20 20 20 2a 2a 20 6f ontents. ** o
17c6e 66 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 f the parent pag
17c6f 65 20 61 72 65 20 73 74 69 6c 6c 20 6d 61 6e 69 e are still mani
17c70 70 75 6c 61 74 65 64 20 62 79 20 74 68 68 20 63 pulated by thh c
17c71 6f 64 65 20 62 65 6c 6f 77 2e 0a 20 20 20 20 2a ode below.. *
17c72 2a 20 54 68 61 74 20 69 73 20 4f 6b 2c 20 61 74 * That is Ok, at
17c73 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65 20 this point the
17c74 70 61 72 65 6e 74 20 70 61 67 65 20 69 73 20 67 parent page is g
17c75 75 61 72 61 6e 74 65 65 64 20 74 6f 0a 20 20 20 uaranteed to.
17c76 20 2a 2a 20 62 65 20 6d 61 72 6b 65 64 20 61 73 ** be marked as
17c77 20 64 69 72 74 79 2e 20 52 65 74 75 72 6e 69 6e dirty. Returnin
17c78 67 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 g an error code
17c79 77 69 6c 6c 20 63 61 75 73 65 20 61 0a 20 20 20 will cause a.
17c7a 20 2a 2a 20 72 6f 6c 6c 62 61 63 6b 2c 20 75 6e ** rollback, un
17c7b 64 6f 69 6e 67 20 61 6e 79 20 63 68 61 6e 67 65 doing any change
17c7c 73 20 6d 61 64 65 20 74 6f 20 74 68 65 20 70 61 s made to the pa
17c7d 72 65 6e 74 20 70 61 67 65 2e 0a 20 20 20 20 2a rent page.. *
17c7e 2f 0a 20 20 20 20 69 66 28 20 49 53 41 55 54 4f /. if( ISAUTO
17c7f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 VACUUM ){.
17c80 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 70 ptrmapPut(pBt, p
17c81 67 6e 6f 4e 65 77 2c 20 50 54 52 4d 41 50 5f 42 gnoNew, PTRMAP_B
17c82 54 52 45 45 2c 20 70 50 61 72 65 6e 74 2d 3e 70 TREE, pParent->p
17c83 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 gno, &rc);.
17c84 20 69 66 28 20 73 7a 43 65 6c 6c 3e 70 4e 65 77 if( szCell>pNew
17c85 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 ->minLocal ){.
17c86 20 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 4f ptrmapPutO
17c87 76 66 6c 50 74 72 28 70 4e 65 77 2c 20 70 43 65 vflPtr(pNew, pCe
17c88 6c 6c 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 ll, &rc);.
17c89 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f }. }. . /
17c8a 2a 20 43 72 65 61 74 65 20 61 20 64 69 76 69 64 * Create a divid
17c8b 65 72 20 63 65 6c 6c 20 74 6f 20 69 6e 73 65 72 er cell to inser
17c8c 74 20 69 6e 74 6f 20 70 50 61 72 65 6e 74 2e 20 t into pParent.
17c8d 54 68 65 20 64 69 76 69 64 65 72 20 63 65 6c 6c The divider cell
17c8e 0a 20 20 20 20 2a 2a 20 63 6f 6e 73 69 73 74 73 . ** consists
17c8f 20 6f 66 20 61 20 34 2d 62 79 74 65 20 70 61 67 of a 4-byte pag
17c90 65 20 6e 75 6d 62 65 72 20 28 74 68 65 20 70 61 e number (the pa
17c91 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 50 61 ge number of pPa
17c92 67 65 29 20 61 6e 64 0a 20 20 20 20 2a 2a 20 61 ge) and. ** a
17c93 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 variable length
17c94 20 6b 65 79 20 76 61 6c 75 65 20 28 77 68 69 63 key value (whic
17c95 68 20 6d 75 73 74 20 62 65 20 74 68 65 20 73 61 h must be the sa
17c96 6d 65 20 76 61 6c 75 65 20 61 73 20 74 68 65 0a me value as the.
17c97 20 20 20 20 2a 2a 20 6c 61 72 67 65 73 74 20 6b ** largest k
17c98 65 79 20 6f 6e 20 70 50 61 67 65 29 2e 0a 20 20 ey on pPage)..
17c99 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 6f 20 66 **. ** To f
17c9a 69 6e 64 20 74 68 65 20 6c 61 72 67 65 73 74 20 ind the largest
17c9b 6b 65 79 20 76 61 6c 75 65 20 6f 6e 20 70 50 61 key value on pPa
17c9c 67 65 2c 20 66 69 72 73 74 20 66 69 6e 64 20 74 ge, first find t
17c9d 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 0a 20 he right-most .
17c9e 20 20 20 2a 2a 20 63 65 6c 6c 20 6f 6e 20 70 50 ** cell on pP
17c9f 61 67 65 2e 20 54 68 65 20 66 69 72 73 74 20 74 age. The first t
17ca0 77 6f 20 66 69 65 6c 64 73 20 6f 66 20 74 68 69 wo fields of thi
17ca1 73 20 63 65 6c 6c 20 61 72 65 20 74 68 65 20 0a s cell are the .
17ca2 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 2d 6c 65 ** record-le
17ca3 6e 67 74 68 20 28 61 20 76 61 72 69 61 62 6c 65 ngth (a variable
17ca4 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 length integer
17ca5 61 74 20 6d 6f 73 74 20 33 32 2d 62 69 74 73 20 at most 32-bits
17ca6 69 6e 20 73 69 7a 65 29 0a 20 20 20 20 2a 2a 20 in size). **
17ca7 61 6e 64 20 74 68 65 20 6b 65 79 20 76 61 6c 75 and the key valu
17ca8 65 20 28 61 20 76 61 72 69 61 62 6c 65 20 6c 65 e (a variable le
17ca9 6e 67 74 68 20 69 6e 74 65 67 65 72 2c 20 6d 61 ngth integer, ma
17caa 79 20 68 61 76 65 20 61 6e 79 20 76 61 6c 75 65 y have any value
17cab 29 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 66 69 ).. ** The fi
17cac 72 73 74 20 6f 66 20 74 68 65 20 77 68 69 6c 65 rst of the while
17cad 28 2e 2e 2e 29 20 6c 6f 6f 70 73 20 62 65 6c 6f (...) loops belo
17cae 77 20 73 6b 69 70 73 20 6f 76 65 72 20 74 68 65 w skips over the
17caf 20 72 65 63 6f 72 64 2d 6c 65 6e 67 74 68 0a 20 record-length.
17cb0 20 20 20 2a 2a 20 66 69 65 6c 64 2e 20 54 68 65 ** field. The
17cb1 20 73 65 63 6f 6e 64 20 77 68 69 6c 65 28 2e 2e second while(..
17cb2 2e 29 20 6c 6f 6f 70 20 63 6f 70 69 65 73 20 74 .) loop copies t
17cb3 68 65 20 6b 65 79 20 76 61 6c 75 65 20 66 72 6f he key value fro
17cb4 6d 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 65 6c m the. ** cel
17cb5 6c 20 6f 6e 20 70 50 61 67 65 20 69 6e 74 6f 20 l on pPage into
17cb6 74 68 65 20 70 53 70 61 63 65 20 62 75 66 66 65 the pSpace buffe
17cb7 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 43 r.. */. pC
17cb8 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 ell = findCell(p
17cb9 50 61 67 65 2c 20 70 50 61 67 65 2d 3e 6e 43 65 Page, pPage->nCe
17cba 6c 6c 2d 31 29 3b 0a 20 20 20 20 70 53 74 6f 70 ll-1);. pStop
17cbb 20 3d 20 26 70 43 65 6c 6c 5b 39 5d 3b 0a 20 20 = &pCell[9];.
17cbc 20 20 77 68 69 6c 65 28 20 28 2a 28 70 43 65 6c while( (*(pCel
17cbd 6c 2b 2b 29 26 30 78 38 30 29 20 26 26 20 70 43 l++)&0x80) && pC
17cbe 65 6c 6c 3c 70 53 74 6f 70 20 29 3b 0a 20 20 20 ell<pStop );.
17cbf 20 70 53 74 6f 70 20 3d 20 26 70 43 65 6c 6c 5b pStop = &pCell[
17cc0 39 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 9];. while( (
17cc1 28 2a 28 70 4f 75 74 2b 2b 29 20 3d 20 2a 28 70 (*(pOut++) = *(p
17cc2 43 65 6c 6c 2b 2b 29 29 26 30 78 38 30 29 20 26 Cell++))&0x80) &
17cc3 26 20 70 43 65 6c 6c 3c 70 53 74 6f 70 20 29 3b & pCell<pStop );
17cc4 0a 0a 20 20 20 20 2f 2a 20 49 6e 73 65 72 74 20 .. /* Insert
17cc5 74 68 65 20 6e 65 77 20 64 69 76 69 64 65 72 20 the new divider
17cc6 63 65 6c 6c 20 69 6e 74 6f 20 70 50 61 72 65 6e cell into pParen
17cc7 74 2e 20 2a 2f 0a 20 20 20 20 69 6e 73 65 72 74 t. */. insert
17cc8 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 70 50 Cell(pParent, pP
17cc9 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 2c 20 70 53 arent->nCell, pS
17cca 70 61 63 65 2c 20 28 69 6e 74 29 28 70 4f 75 74 pace, (int)(pOut
17ccb 2d 70 53 70 61 63 65 29 2c 0a 20 20 20 20 20 20 -pSpace),.
17ccc 20 20 20 20 20 20 20 20 20 30 2c 20 70 50 61 67 0, pPag
17ccd 65 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 0a e->pgno, &rc);..
17cce 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 72 /* Set the r
17ccf 69 67 68 74 2d 63 68 69 6c 64 20 70 6f 69 6e 74 ight-child point
17cd0 65 72 20 6f 66 20 70 50 61 72 65 6e 74 20 74 6f er of pParent to
17cd1 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 6e 65 point to the ne
17cd2 77 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20 20 70 w page. */. p
17cd3 75 74 34 62 79 74 65 28 26 70 50 61 72 65 6e 74 ut4byte(&pParent
17cd4 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 6e 74 2d ->aData[pParent-
17cd5 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 2c 20 70 >hdrOffset+8], p
17cd6 67 6e 6f 4e 65 77 29 3b 0a 20 20 0a 20 20 20 20 gnoNew);. .
17cd7 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20 72 /* Release the r
17cd8 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 eference to the
17cd9 6e 65 77 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20 new page. */.
17cda 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4e 65 releasePage(pNe
17cdb 77 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 w);. }.. retur
17cdc 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f n rc;.}.#endif /
17cdd 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 51 55 * SQLITE_OMIT_QU
17cde 49 43 4b 42 41 4c 41 4e 43 45 20 2a 2f 0a 0a 23 ICKBALANCE */..#
17cdf 69 66 20 30 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 if 0./*.** This
17ce0 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f function does no
17ce1 74 20 63 6f 6e 74 72 69 62 75 74 65 20 61 6e 79 t contribute any
17ce2 74 68 69 6e 67 20 74 6f 20 74 68 65 20 6f 70 65 thing to the ope
17ce3 72 61 74 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 ration of SQLite
17ce4 2e 0a 2a 2a 20 69 74 20 69 73 20 73 6f 6d 65 74 ..** it is somet
17ce5 69 6d 65 73 20 61 63 74 69 76 61 74 65 64 20 74 imes activated t
17ce6 65 6d 70 6f 72 61 72 69 6c 79 20 77 68 69 6c 65 emporarily while
17ce7 20 64 65 62 75 67 67 69 6e 67 20 63 6f 64 65 20 debugging code
17ce8 72 65 73 70 6f 6e 73 69 62 6c 65 20 0a 2a 2a 20 responsible .**
17ce9 66 6f 72 20 73 65 74 74 69 6e 67 20 70 6f 69 6e for setting poin
17cea 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 2e ter-map entries.
17ceb 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
17cec 74 72 6d 61 70 43 68 65 63 6b 50 61 67 65 73 28 trmapCheckPages(
17ced 4d 65 6d 50 61 67 65 20 2a 2a 61 70 50 61 67 65 MemPage **apPage
17cee 2c 20 69 6e 74 20 6e 50 61 67 65 29 7b 0a 20 20 , int nPage){.
17cef 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 66 6f 72 28 int i, j;. for(
17cf0 69 3d 30 3b 20 69 3c 6e 50 61 67 65 3b 20 69 2b i=0; i<nPage; i+
17cf1 2b 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6e 3b 0a +){. Pgno n;.
17cf2 20 20 20 20 75 38 20 65 3b 0a 20 20 20 20 4d 65 u8 e;. Me
17cf3 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 61 mPage *pPage = a
17cf4 70 50 61 67 65 5b 69 5d 3b 0a 20 20 20 20 42 74 pPage[i];. Bt
17cf5 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50 Shared *pBt = pP
17cf6 61 67 65 2d 3e 70 42 74 3b 0a 20 20 20 20 61 73 age->pBt;. as
17cf7 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 sert( pPage->isI
17cf8 6e 69 74 20 29 3b 0a 0a 20 20 20 20 66 6f 72 28 nit );.. for(
17cf9 6a 3d 30 3b 20 6a 3c 70 50 61 67 65 2d 3e 6e 43 j=0; j<pPage->nC
17cfa 65 6c 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 ell; j++){.
17cfb 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a CellInfo info;.
17cfc 20 20 20 20 20 20 75 38 20 2a 7a 3b 0a 20 20 20 u8 *z;.
17cfd 20 20 0a 20 20 20 20 20 20 7a 20 3d 20 66 69 6e . z = fin
17cfe 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 6a 29 3b dCell(pPage, j);
17cff 0a 20 20 20 20 20 20 62 74 72 65 65 50 61 72 73 . btreePars
17d00 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 eCellPtr(pPage,
17d01 7a 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 20 z, &info);.
17d02 20 69 66 28 20 69 6e 66 6f 2e 69 4f 76 65 72 66 if( info.iOverf
17d03 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 20 50 low ){. P
17d04 67 6e 6f 20 6f 76 66 6c 20 3d 20 67 65 74 34 62 gno ovfl = get4b
17d05 79 74 65 28 26 7a 5b 69 6e 66 6f 2e 69 4f 76 65 yte(&z[info.iOve
17d06 72 66 6c 6f 77 5d 29 3b 0a 20 20 20 20 20 20 20 rflow]);.
17d07 20 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 ptrmapGet(pBt,
17d08 6f 76 66 6c 2c 20 26 65 2c 20 26 6e 29 3b 0a 20 ovfl, &e, &n);.
17d09 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e assert( n
17d0a 3d 3d 70 50 61 67 65 2d 3e 70 67 6e 6f 20 26 26 ==pPage->pgno &&
17d0b 20 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 e==PTRMAP_OVERF
17d0c 4c 4f 57 31 20 29 3b 0a 20 20 20 20 20 20 7d 0a LOW1 );. }.
17d0d 20 20 20 20 20 20 69 66 28 20 21 70 50 61 67 65 if( !pPage
17d0e 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 ->leaf ){.
17d0f 20 20 50 67 6e 6f 20 63 68 69 6c 64 20 3d 20 67 Pgno child = g
17d10 65 74 34 62 79 74 65 28 7a 29 3b 0a 20 20 20 20 et4byte(z);.
17d11 20 20 20 20 70 74 72 6d 61 70 47 65 74 28 70 42 ptrmapGet(pB
17d12 74 2c 20 63 68 69 6c 64 2c 20 26 65 2c 20 26 6e t, child, &e, &n
17d13 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 );. asser
17d14 74 28 20 6e 3d 3d 70 50 61 67 65 2d 3e 70 67 6e t( n==pPage->pgn
17d15 6f 20 26 26 20 65 3d 3d 50 54 52 4d 41 50 5f 42 o && e==PTRMAP_B
17d16 54 52 45 45 20 29 3b 0a 20 20 20 20 20 20 7d 0a TREE );. }.
17d17 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 70 }. if( !p
17d18 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 Page->leaf ){.
17d19 20 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 20 3d Pgno child =
17d1a 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 get4byte(&pPage
17d1b 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 ->aData[pPage->h
17d1c 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 drOffset+8]);.
17d1d 20 20 20 20 70 74 72 6d 61 70 47 65 74 28 70 42 ptrmapGet(pB
17d1e 74 2c 20 63 68 69 6c 64 2c 20 26 65 2c 20 26 6e t, child, &e, &n
17d1f 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
17d20 20 6e 3d 3d 70 50 61 67 65 2d 3e 70 67 6e 6f 20 n==pPage->pgno
17d21 26 26 20 65 3d 3d 50 54 52 4d 41 50 5f 42 54 52 && e==PTRMAP_BTR
17d22 45 45 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a EE );. }. }.
17d23 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 return 1;.}.#e
17d24 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 ndif../*.** This
17d25 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 function is use
17d26 64 20 74 6f 20 63 6f 70 79 20 74 68 65 20 63 6f d to copy the co
17d27 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 62 2d ntents of the b-
17d28 74 72 65 65 20 6e 6f 64 65 20 73 74 6f 72 65 64 tree node stored
17d29 20 0a 2a 2a 20 6f 6e 20 70 61 67 65 20 70 46 72 .** on page pFr
17d2a 6f 6d 20 74 6f 20 70 61 67 65 20 70 54 6f 2e 20 om to page pTo.
17d2b 49 66 20 70 61 67 65 20 70 46 72 6f 6d 20 77 61 If page pFrom wa
17d2c 73 20 6e 6f 74 20 61 20 6c 65 61 66 20 70 61 67 s not a leaf pag
17d2d 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 70 e, then.** the p
17d2e 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 ointer-map entri
17d2f 65 73 20 66 6f 72 20 65 61 63 68 20 63 68 69 6c es for each chil
17d30 64 20 70 61 67 65 20 61 72 65 20 75 70 64 61 74 d page are updat
17d31 65 64 20 73 6f 20 74 68 61 74 20 74 68 65 0a 2a ed so that the.*
17d32 2a 20 70 61 72 65 6e 74 20 70 61 67 65 20 73 74 * parent page st
17d33 6f 72 65 64 20 69 6e 20 74 68 65 20 70 6f 69 6e ored in the poin
17d34 74 65 72 20 6d 61 70 20 69 73 20 70 61 67 65 20 ter map is page
17d35 70 54 6f 2e 20 49 66 20 70 46 72 6f 6d 20 63 6f pTo. If pFrom co
17d36 6e 74 61 69 6e 65 64 0a 2a 2a 20 61 6e 79 20 63 ntained.** any c
17d37 65 6c 6c 73 20 77 69 74 68 20 6f 76 65 72 66 6c ells with overfl
17d38 6f 77 20 70 61 67 65 20 70 6f 69 6e 74 65 72 73 ow page pointers
17d39 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 72 72 65 , then the corre
17d3a 73 70 6f 6e 64 69 6e 67 20 70 6f 69 6e 74 65 72 sponding pointer
17d3b 0a 2a 2a 20 6d 61 70 20 65 6e 74 72 69 65 73 20 .** map entries
17d3c 61 72 65 20 61 6c 73 6f 20 75 70 64 61 74 65 64 are also updated
17d3d 20 73 6f 20 74 68 61 74 20 74 68 65 20 70 61 72 so that the par
17d3e 65 6e 74 20 70 61 67 65 20 69 73 20 70 61 67 65 ent page is page
17d3f 20 70 54 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 pTo..**.** If p
17d40 46 72 6f 6d 20 69 73 20 63 75 72 72 65 6e 74 6c From is currentl
17d41 79 20 63 61 72 72 79 69 6e 67 20 61 6e 79 20 6f y carrying any o
17d42 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 20 28 65 verflow cells (e
17d43 6e 74 72 69 65 73 20 69 6e 20 74 68 65 0a 2a 2a ntries in the.**
17d44 20 4d 65 6d 50 61 67 65 2e 61 4f 76 66 6c 5b 5d MemPage.aOvfl[]
17d45 20 61 72 72 61 79 29 2c 20 74 68 65 79 20 61 72 array), they ar
17d46 65 20 6e 6f 74 20 63 6f 70 69 65 64 20 74 6f 20 e not copied to
17d47 70 54 6f 2e 20 0a 2a 2a 0a 2a 2a 20 42 65 66 6f pTo. .**.** Befo
17d48 72 65 20 72 65 74 75 72 6e 69 6e 67 2c 20 70 61 re returning, pa
17d49 67 65 20 70 54 6f 20 69 73 20 72 65 69 6e 69 74 ge pTo is reinit
17d4a 69 61 6c 69 7a 65 64 20 75 73 69 6e 67 20 62 74 ialized using bt
17d4b 72 65 65 49 6e 69 74 50 61 67 65 28 29 2e 0a 2a reeInitPage()..*
17d4c 2a 0a 2a 2a 20 54 68 65 20 70 65 72 66 6f 72 6d *.** The perform
17d4d 61 6e 63 65 20 6f 66 20 74 68 69 73 20 66 75 6e ance of this fun
17d4e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 63 72 69 ction is not cri
17d4f 74 69 63 61 6c 2e 20 49 74 20 69 73 20 6f 6e 6c tical. It is onl
17d50 79 20 75 73 65 64 20 62 79 20 0a 2a 2a 20 74 68 y used by .** th
17d51 65 20 62 61 6c 61 6e 63 65 5f 73 68 61 6c 6c 6f e balance_shallo
17d52 77 65 72 28 29 20 61 6e 64 20 62 61 6c 61 6e 63 wer() and balanc
17d53 65 5f 64 65 65 70 65 72 28 29 20 70 72 6f 63 65 e_deeper() proce
17d54 64 75 72 65 73 2c 20 6e 65 69 74 68 65 72 20 6f dures, neither o
17d55 66 0a 2a 2a 20 77 68 69 63 68 20 61 72 65 20 63 f.** which are c
17d56 61 6c 6c 65 64 20 6f 66 74 65 6e 20 75 6e 64 65 alled often unde
17d57 72 20 6e 6f 72 6d 61 6c 20 63 69 72 63 75 6d 73 r normal circums
17d58 74 61 6e 63 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 tances..*/.stati
17d59 63 20 76 6f 69 64 20 63 6f 70 79 4e 6f 64 65 43 c void copyNodeC
17d5a 6f 6e 74 65 6e 74 28 4d 65 6d 50 61 67 65 20 2a ontent(MemPage *
17d5b 70 46 72 6f 6d 2c 20 4d 65 6d 50 61 67 65 20 2a pFrom, MemPage *
17d5c 70 54 6f 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a pTo, int *pRC){.
17d5d 20 20 69 66 28 20 28 2a 70 52 43 29 3d 3d 53 51 if( (*pRC)==SQ
17d5e 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 42 LITE_OK ){. B
17d5f 74 53 68 61 72 65 64 20 2a 20 63 6f 6e 73 74 20 tShared * const
17d60 70 42 74 20 3d 20 70 46 72 6f 6d 2d 3e 70 42 74 pBt = pFrom->pBt
17d61 3b 0a 20 20 20 20 75 38 20 2a 20 63 6f 6e 73 74 ;. u8 * const
17d62 20 61 46 72 6f 6d 20 3d 20 70 46 72 6f 6d 2d 3e aFrom = pFrom->
17d63 61 44 61 74 61 3b 0a 20 20 20 20 75 38 20 2a 20 aData;. u8 *
17d64 63 6f 6e 73 74 20 61 54 6f 20 3d 20 70 54 6f 2d const aTo = pTo-
17d65 3e 61 44 61 74 61 3b 0a 20 20 20 20 69 6e 74 20 >aData;. int
17d66 63 6f 6e 73 74 20 69 46 72 6f 6d 48 64 72 20 3d const iFromHdr =
17d67 20 70 46 72 6f 6d 2d 3e 68 64 72 4f 66 66 73 65 pFrom->hdrOffse
17d68 74 3b 0a 20 20 20 20 69 6e 74 20 63 6f 6e 73 74 t;. int const
17d69 20 69 54 6f 48 64 72 20 3d 20 28 28 70 54 6f 2d iToHdr = ((pTo-
17d6a 3e 70 67 6e 6f 3d 3d 31 29 20 3f 20 31 30 30 20 >pgno==1) ? 100
17d6b 3a 20 30 29 3b 0a 20 20 20 20 54 45 53 54 4f 4e : 0);. TESTON
17d6c 4c 59 28 69 6e 74 20 72 63 3b 29 0a 20 20 20 20 LY(int rc;).
17d6d 69 6e 74 20 69 44 61 74 61 3b 0a 20 20 0a 20 20 int iData;. .
17d6e 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46 72 . assert( pFr
17d6f 6f 6d 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 om->isInit );.
17d70 20 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d assert( pFrom-
17d71 3e 6e 46 72 65 65 3e 3d 69 54 6f 48 64 72 20 29 >nFree>=iToHdr )
17d72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 67 65 ;. assert( ge
17d73 74 32 62 79 74 65 28 26 61 46 72 6f 6d 5b 69 46 t2byte(&aFrom[iF
17d74 72 6f 6d 48 64 72 2b 35 5d 29 3c 3d 70 42 74 2d romHdr+5])<=pBt-
17d75 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 >usableSize );.
17d76 20 0a 20 20 20 20 2f 2a 20 43 6f 70 79 20 74 68 . /* Copy th
17d77 65 20 62 2d 74 72 65 65 20 6e 6f 64 65 20 63 6f e b-tree node co
17d78 6e 74 65 6e 74 20 66 72 6f 6d 20 70 61 67 65 20 ntent from page
17d79 70 46 72 6f 6d 20 74 6f 20 70 61 67 65 20 70 54 pFrom to page pT
17d7a 6f 2e 20 2a 2f 0a 20 20 20 20 69 44 61 74 61 20 o. */. iData
17d7b 3d 20 67 65 74 32 62 79 74 65 28 26 61 46 72 6f = get2byte(&aFro
17d7c 6d 5b 69 46 72 6f 6d 48 64 72 2b 35 5d 29 3b 0a m[iFromHdr+5]);.
17d7d 20 20 20 20 6d 65 6d 63 70 79 28 26 61 54 6f 5b memcpy(&aTo[
17d7e 69 44 61 74 61 5d 2c 20 26 61 46 72 6f 6d 5b 69 iData], &aFrom[i
17d7f 44 61 74 61 5d 2c 20 70 42 74 2d 3e 75 73 61 62 Data], pBt->usab
17d80 6c 65 53 69 7a 65 2d 69 44 61 74 61 29 3b 0a 20 leSize-iData);.
17d81 20 20 20 6d 65 6d 63 70 79 28 26 61 54 6f 5b 69 memcpy(&aTo[i
17d82 54 6f 48 64 72 5d 2c 20 26 61 46 72 6f 6d 5b 69 ToHdr], &aFrom[i
17d83 46 72 6f 6d 48 64 72 5d 2c 20 70 46 72 6f 6d 2d FromHdr], pFrom-
17d84 3e 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a >cellOffset + 2*
17d85 70 46 72 6f 6d 2d 3e 6e 43 65 6c 6c 29 3b 0a 20 pFrom->nCell);.
17d86 20 0a 20 20 20 20 2f 2a 20 52 65 69 6e 69 74 69 . /* Reiniti
17d87 61 6c 69 7a 65 20 70 61 67 65 20 70 54 6f 20 73 alize page pTo s
17d88 6f 20 74 68 61 74 20 74 68 65 20 63 6f 6e 74 65 o that the conte
17d89 6e 74 73 20 6f 66 20 74 68 65 20 4d 65 6d 50 61 nts of the MemPa
17d8a 67 65 20 73 74 72 75 63 74 75 72 65 0a 20 20 20 ge structure.
17d8b 20 2a 2a 20 6d 61 74 63 68 20 74 68 65 20 6e 65 ** match the ne
17d8c 77 20 64 61 74 61 2e 20 54 68 65 20 69 6e 69 74 w data. The init
17d8d 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 70 54 ialization of pT
17d8e 6f 20 22 63 61 6e 6e 6f 74 22 20 66 61 69 6c 2c o "cannot" fail,
17d8f 20 61 73 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 as the. ** d
17d90 61 74 61 20 63 6f 70 69 65 64 20 66 72 6f 6d 20 ata copied from
17d91 70 46 72 6f 6d 20 69 73 20 6b 6e 6f 77 6e 20 74 pFrom is known t
17d92 6f 20 62 65 20 76 61 6c 69 64 2e 20 20 2a 2f 0a o be valid. */.
17d93 20 20 20 20 70 54 6f 2d 3e 69 73 49 6e 69 74 20 pTo->isInit
17d94 3d 20 30 3b 0a 20 20 20 20 54 45 53 54 4f 4e 4c = 0;. TESTONL
17d95 59 28 72 63 20 3d 20 29 20 62 74 72 65 65 49 6e Y(rc = ) btreeIn
17d96 69 74 50 61 67 65 28 70 54 6f 29 3b 0a 20 20 20 itPage(pTo);.
17d97 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c assert( rc==SQL
17d98 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 0a 20 20 20 ITE_OK );. .
17d99 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 /* If this is a
17d9a 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 n auto-vacuum da
17d9b 74 61 62 61 73 65 2c 20 75 70 64 61 74 65 20 74 tabase, update t
17d9c 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 he pointer-map e
17d9d 6e 74 72 69 65 73 0a 20 20 20 20 2a 2a 20 66 6f ntries. ** fo
17d9e 72 20 61 6e 79 20 62 2d 74 72 65 65 20 6f 72 20 r any b-tree or
17d9f 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 74 overflow pages t
17da0 68 61 74 20 70 54 6f 20 6e 6f 77 20 63 6f 6e 74 hat pTo now cont
17da1 61 69 6e 73 20 74 68 65 20 70 6f 69 6e 74 65 72 ains the pointer
17da2 73 20 74 6f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 s to.. */.
17da3 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 if( ISAUTOVACUU
17da4 4d 20 29 7b 0a 20 20 20 20 20 20 2a 70 52 43 20 M ){. *pRC
17da5 3d 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70 = setChildPtrmap
17da6 73 28 70 54 6f 29 3b 0a 20 20 20 20 7d 0a 20 20 s(pTo);. }.
17da7 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 }.}../*.** This
17da8 72 6f 75 74 69 6e 65 20 72 65 64 69 73 74 72 69 routine redistri
17da9 62 75 74 65 73 20 63 65 6c 6c 73 20 6f 6e 20 74 butes cells on t
17daa 68 65 20 69 50 61 72 65 6e 74 49 64 78 27 74 68 he iParentIdx'th
17dab 20 63 68 69 6c 64 20 6f 66 20 70 50 61 72 65 6e child of pParen
17dac 74 0a 2a 2a 20 28 68 65 72 65 61 66 74 65 72 20 t.** (hereafter
17dad 22 74 68 65 20 70 61 67 65 22 29 20 61 6e 64 20 "the page") and
17dae 75 70 20 74 6f 20 32 20 73 69 62 6c 69 6e 67 73 up to 2 siblings
17daf 20 73 6f 20 74 68 61 74 20 61 6c 6c 20 70 61 67 so that all pag
17db0 65 73 20 68 61 76 65 20 61 62 6f 75 74 20 74 68 es have about th
17db1 65 0a 2a 2a 20 73 61 6d 65 20 61 6d 6f 75 6e 74 e.** same amount
17db2 20 6f 66 20 66 72 65 65 20 73 70 61 63 65 2e 20 of free space.
17db3 55 73 75 61 6c 6c 79 20 61 20 73 69 6e 67 6c 65 Usually a single
17db4 20 73 69 62 6c 69 6e 67 20 6f 6e 20 65 69 74 68 sibling on eith
17db5 65 72 20 73 69 64 65 20 6f 66 20 74 68 65 0a 2a er side of the.*
17db6 2a 20 70 61 67 65 20 61 72 65 20 75 73 65 64 20 * page are used
17db7 69 6e 20 74 68 65 20 62 61 6c 61 6e 63 69 6e 67 in the balancing
17db8 2c 20 74 68 6f 75 67 68 20 62 6f 74 68 20 73 69 , though both si
17db9 62 6c 69 6e 67 73 20 6d 69 67 68 74 20 63 6f 6d blings might com
17dba 65 20 66 72 6f 6d 20 6f 6e 65 0a 2a 2a 20 73 69 e from one.** si
17dbb 64 65 20 69 66 20 74 68 65 20 70 61 67 65 20 69 de if the page i
17dbc 73 20 74 68 65 20 66 69 72 73 74 20 6f 72 20 6c s the first or l
17dbd 61 73 74 20 63 68 69 6c 64 20 6f 66 20 69 74 73 ast child of its
17dbe 20 70 61 72 65 6e 74 2e 20 49 66 20 74 68 65 20 parent. If the
17dbf 70 61 67 65 20 0a 2a 2a 20 68 61 73 20 66 65 77 page .** has few
17dc0 65 72 20 74 68 61 6e 20 32 20 73 69 62 6c 69 6e er than 2 siblin
17dc1 67 73 20 28 73 6f 6d 65 74 68 69 6e 67 20 77 68 gs (something wh
17dc2 69 63 68 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70 ich can only hap
17dc3 70 65 6e 20 69 66 20 74 68 65 20 70 61 67 65 0a pen if the page.
17dc4 2a 2a 20 69 73 20 61 20 72 6f 6f 74 20 70 61 67 ** is a root pag
17dc5 65 20 6f 72 20 61 20 63 68 69 6c 64 20 6f 66 20 e or a child of
17dc6 61 20 72 6f 6f 74 20 70 61 67 65 29 20 74 68 65 a root page) the
17dc7 6e 20 61 6c 6c 20 61 76 61 69 6c 61 62 6c 65 20 n all available
17dc8 73 69 62 6c 69 6e 67 73 0a 2a 2a 20 70 61 72 74 siblings.** part
17dc9 69 63 69 70 61 74 65 20 69 6e 20 74 68 65 20 62 icipate in the b
17dca 61 6c 61 6e 63 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 alancing..**.**
17dcb 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 69 The number of si
17dcc 62 6c 69 6e 67 73 20 6f 66 20 74 68 65 20 70 61 blings of the pa
17dcd 67 65 20 6d 69 67 68 74 20 62 65 20 69 6e 63 72 ge might be incr
17dce 65 61 73 65 64 20 6f 72 20 64 65 63 72 65 61 73 eased or decreas
17dcf 65 64 20 62 79 20 0a 2a 2a 20 6f 6e 65 20 6f 72 ed by .** one or
17dd0 20 74 77 6f 20 69 6e 20 61 6e 20 65 66 66 6f 72 two in an effor
17dd1 74 20 74 6f 20 6b 65 65 70 20 70 61 67 65 73 20 t to keep pages
17dd2 6e 65 61 72 6c 79 20 66 75 6c 6c 20 62 75 74 20 nearly full but
17dd3 6e 6f 74 20 6f 76 65 72 20 66 75 6c 6c 2e 20 0a not over full. .
17dd4 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 **.** Note that
17dd5 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e when this routin
17dd6 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 73 6f 6d e is called, som
17dd7 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 73 20 6f e of the cells o
17dd8 6e 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 6d 69 n the page.** mi
17dd9 67 68 74 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 ght not actually
17dda 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 4d 65 be stored in Me
17ddb 6d 50 61 67 65 2e 61 44 61 74 61 5b 5d 2e 20 54 mPage.aData[]. T
17ddc 68 69 73 20 63 61 6e 20 68 61 70 70 65 6e 0a 2a his can happen.*
17ddd 2a 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 * if the page is
17dde 20 6f 76 65 72 66 75 6c 6c 2e 20 54 68 69 73 20 overfull. This
17ddf 72 6f 75 74 69 6e 65 20 65 6e 73 75 72 65 73 20 routine ensures
17de0 74 68 61 74 20 61 6c 6c 20 63 65 6c 6c 73 20 61 that all cells a
17de1 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 74 6f 20 74 llocated.** to t
17de2 68 65 20 70 61 67 65 20 61 6e 64 20 69 74 73 20 he page and its
17de3 73 69 62 6c 69 6e 67 73 20 66 69 74 20 69 6e 74 siblings fit int
17de4 6f 20 4d 65 6d 50 61 67 65 2e 61 44 61 74 61 5b o MemPage.aData[
17de5 5d 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 ] before returni
17de6 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 ng..**.** In the
17de7 20 63 6f 75 72 73 65 20 6f 66 20 62 61 6c 61 6e course of balan
17de8 63 69 6e 67 20 74 68 65 20 70 61 67 65 20 61 6e cing the page an
17de9 64 20 69 74 73 20 73 69 62 6c 69 6e 67 73 2c 20 d its siblings,
17dea 63 65 6c 6c 73 20 6d 61 79 20 62 65 0a 2a 2a 20 cells may be.**
17deb 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 6f 72 inserted into or
17dec 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 removed from th
17ded 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 28 70 e parent page (p
17dee 50 61 72 65 6e 74 29 2e 20 44 6f 69 6e 67 20 73 Parent). Doing s
17def 6f 0a 2a 2a 20 6d 61 79 20 63 61 75 73 65 20 74 o.** may cause t
17df0 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 74 he parent page t
17df1 6f 20 62 65 63 6f 6d 65 20 6f 76 65 72 66 75 6c o become overful
17df2 6c 20 6f 72 20 75 6e 64 65 72 66 75 6c 6c 2e 20 l or underfull.
17df3 49 66 20 74 68 69 73 0a 2a 2a 20 68 61 70 70 65 If this.** happe
17df4 6e 73 2c 20 69 74 20 69 73 20 74 68 65 20 72 65 ns, it is the re
17df5 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 20 sponsibility of
17df6 74 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 69 6e the caller to in
17df7 76 6f 6b 65 20 74 68 65 20 63 6f 72 72 65 63 74 voke the correct
17df8 0a 2a 2a 20 62 61 6c 61 6e 63 69 6e 67 20 72 6f .** balancing ro
17df9 75 74 69 6e 65 20 74 6f 20 66 69 78 20 74 68 69 utine to fix thi
17dfa 73 20 70 72 6f 62 6c 65 6d 20 28 73 65 65 20 74 s problem (see t
17dfb 68 65 20 62 61 6c 61 6e 63 65 28 29 20 72 6f 75 he balance() rou
17dfc 74 69 6e 65 29 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 tine). .**.** If
17dfd 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66 61 this routine fa
17dfe 69 6c 73 20 66 6f 72 20 61 6e 79 20 72 65 61 73 ils for any reas
17dff 6f 6e 2c 20 69 74 20 6d 69 67 68 74 20 6c 65 61 on, it might lea
17e00 76 65 20 74 68 65 20 64 61 74 61 62 61 73 65 0a ve the database.
17e01 2a 2a 20 69 6e 20 61 20 63 6f 72 72 75 70 74 65 ** in a corrupte
17e02 64 20 73 74 61 74 65 2e 20 53 6f 20 69 66 20 74 d state. So if t
17e03 68 69 73 20 72 6f 75 74 69 6e 65 20 66 61 69 6c his routine fail
17e04 73 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 s, the database
17e05 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 72 6f 6c should.** be rol
17e06 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 led back..**.**
17e07 54 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 The third argume
17e08 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 nt to this funct
17e09 69 6f 6e 2c 20 61 4f 76 66 6c 53 70 61 63 65 2c ion, aOvflSpace,
17e0a 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
17e0b 20 61 0a 2a 2a 20 62 75 66 66 65 72 20 62 69 67 a.** buffer big
17e0c 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 enough to hold
17e0d 6f 6e 65 20 70 61 67 65 2e 20 49 66 20 77 68 69 one page. If whi
17e0e 6c 65 20 69 6e 73 65 72 74 69 6e 67 20 63 65 6c le inserting cel
17e0f 6c 73 20 69 6e 74 6f 20 74 68 65 20 70 61 72 65 ls into the pare
17e10 6e 74 0a 2a 2a 20 70 61 67 65 20 28 70 50 61 72 nt.** page (pPar
17e11 65 6e 74 29 20 74 68 65 20 70 61 72 65 6e 74 20 ent) the parent
17e12 70 61 67 65 20 62 65 63 6f 6d 65 73 20 6f 76 65 page becomes ove
17e13 72 66 75 6c 6c 2c 20 74 68 69 73 20 62 75 66 66 rfull, this buff
17e14 65 72 20 69 73 0a 2a 2a 20 75 73 65 64 20 74 6f er is.** used to
17e15 20 73 74 6f 72 65 20 74 68 65 20 70 61 72 65 6e store the paren
17e16 74 27 73 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c t's overflow cel
17e17 6c 73 2e 20 42 65 63 61 75 73 65 20 74 68 69 73 ls. Because this
17e18 20 66 75 6e 63 74 69 6f 6e 20 69 6e 73 65 72 74 function insert
17e19 73 0a 2a 2a 20 61 20 6d 61 78 69 6d 75 6d 20 6f s.** a maximum o
17e1a 66 20 66 6f 75 72 20 64 69 76 69 64 65 72 20 63 f four divider c
17e1b 65 6c 6c 73 20 69 6e 74 6f 20 74 68 65 20 70 61 ells into the pa
17e1c 72 65 6e 74 20 70 61 67 65 2c 20 61 6e 64 20 74 rent page, and t
17e1d 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 73 69 he maximum.** si
17e1e 7a 65 20 6f 66 20 61 20 63 65 6c 6c 20 73 74 6f ze of a cell sto
17e1f 72 65 64 20 77 69 74 68 69 6e 20 61 6e 20 69 6e red within an in
17e20 74 65 72 6e 61 6c 20 6e 6f 64 65 20 69 73 20 61 ternal node is a
17e21 6c 77 61 79 73 20 6c 65 73 73 20 74 68 61 6e 20 lways less than
17e22 31 2f 34 0a 2a 2a 20 6f 66 20 74 68 65 20 70 61 1/4.** of the pa
17e23 67 65 2d 73 69 7a 65 2c 20 74 68 65 20 61 4f 76 ge-size, the aOv
17e24 66 6c 53 70 61 63 65 5b 5d 20 62 75 66 66 65 72 flSpace[] buffer
17e25 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 is guaranteed t
17e26 6f 20 62 65 20 6c 61 72 67 65 0a 2a 2a 20 65 6e o be large.** en
17e27 6f 75 67 68 20 66 6f 72 20 61 6c 6c 20 6f 76 65 ough for all ove
17e28 72 66 6c 6f 77 20 63 65 6c 6c 73 2e 0a 2a 2a 0a rflow cells..**.
17e29 2a 2a 20 49 66 20 61 4f 76 66 6c 53 70 61 63 65 ** If aOvflSpace
17e2a 20 69 73 20 73 65 74 20 74 6f 20 61 20 6e 75 6c is set to a nul
17e2b 6c 20 70 6f 69 6e 74 65 72 2c 20 74 68 69 73 20 l pointer, this
17e2c 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 function returns
17e2d 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 .** SQLITE_NOME
17e2e 4d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 M..*/.static int
17e2f 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 balance_nonroot
17e30 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 (. MemPage *pPa
17e31 72 65 6e 74 2c 20 20 20 20 20 20 20 20 20 20 20 rent,
17e32 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 70 61 /* Parent pa
17e33 67 65 20 6f 66 20 73 69 62 6c 69 6e 67 73 20 62 ge of siblings b
17e34 65 69 6e 67 20 62 61 6c 61 6e 63 65 64 20 2a 2f eing balanced */
17e35 0a 20 20 69 6e 74 20 69 50 61 72 65 6e 74 49 64 . int iParentId
17e36 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 x,
17e37 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 22 /* Index of "
17e38 74 68 65 20 70 61 67 65 22 20 69 6e 20 70 50 61 the page" in pPa
17e39 72 65 6e 74 20 2a 2f 0a 20 20 75 38 20 2a 61 4f rent */. u8 *aO
17e3a 76 66 6c 53 70 61 63 65 2c 20 20 20 20 20 20 20 vflSpace,
17e3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 61 67 /* pag
17e3c 65 2d 73 69 7a 65 20 62 79 74 65 73 20 6f 66 20 e-size bytes of
17e3d 73 70 61 63 65 20 66 6f 72 20 70 61 72 65 6e 74 space for parent
17e3e 20 6f 76 66 6c 20 2a 2f 0a 20 20 69 6e 74 20 69 ovfl */. int i
17e3f 73 52 6f 6f 74 20 20 20 20 20 20 20 20 20 20 20 sRoot
17e40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
17e41 75 65 20 69 66 20 70 50 61 72 65 6e 74 20 69 73 ue if pParent is
17e42 20 61 20 72 6f 6f 74 2d 70 61 67 65 20 2a 2f 0a a root-page */.
17e43 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 ){. BtShared *p
17e44 42 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 Bt;
17e45 20 20 2f 2a 20 54 68 65 20 77 68 6f 6c 65 20 64 /* The whole d
17e46 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 atabase */. int
17e47 20 6e 43 65 6c 6c 20 3d 20 30 3b 20 20 20 20 20 nCell = 0;
17e48 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
17e49 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20 ber of cells in
17e4a 61 70 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 69 6e apCell[] */. in
17e4b 74 20 6e 4d 61 78 43 65 6c 6c 73 20 3d 20 30 3b t nMaxCells = 0;
17e4c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c /* Al
17e4d 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20 located size of
17e4e 61 70 43 65 6c 6c 2c 20 73 7a 43 65 6c 6c 2c 20 apCell, szCell,
17e4f 61 46 72 6f 6d 2e 20 2a 2f 0a 20 20 69 6e 74 20 aFrom. */. int
17e50 6e 4e 65 77 20 3d 20 30 3b 20 20 20 20 20 20 20 nNew = 0;
17e51 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
17e52 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 er of pages in a
17e53 70 4e 65 77 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 pNew[] */. int
17e54 6e 4f 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 nOld;
17e55 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
17e56 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 er of pages in a
17e57 70 4f 6c 64 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 pOld[] */. int
17e58 69 2c 20 6a 2c 20 6b 3b 20 20 20 20 20 20 20 20 i, j, k;
17e59 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 /* Loop
17e5a 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69 counters */. i
17e5b 6e 74 20 6e 78 44 69 76 3b 20 20 20 20 20 20 20 nt nxDiv;
17e5c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
17e5d 65 78 74 20 64 69 76 69 64 65 72 20 73 6c 6f 74 ext divider slot
17e5e 20 69 6e 20 70 50 61 72 65 6e 74 2d 3e 61 43 65 in pParent->aCe
17e5f 6c 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 72 63 ll[] */. int rc
17e60 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 = SQLITE_OK;
17e61 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 /* The re
17e62 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 75 turn code */. u
17e63 31 36 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 16 leafCorrectio
17e64 6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 34 n; /* 4
17e65 20 69 66 20 70 50 61 67 65 20 69 73 20 61 20 6c if pPage is a l
17e66 65 61 66 2e 20 20 30 20 69 66 20 6e 6f 74 20 2a eaf. 0 if not *
17e67 2f 0a 20 20 69 6e 74 20 6c 65 61 66 44 61 74 61 /. int leafData
17e68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
17e69 20 2f 2a 20 54 72 75 65 20 69 66 20 70 50 61 67 /* True if pPag
17e6a 65 20 69 73 20 61 20 6c 65 61 66 20 6f 66 20 61 e is a leaf of a
17e6b 20 4c 45 41 46 44 41 54 41 20 74 72 65 65 20 2a LEAFDATA tree *
17e6c 2f 0a 20 20 69 6e 74 20 75 73 61 62 6c 65 53 70 /. int usableSp
17e6d 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ace;
17e6e 20 2f 2a 20 42 79 74 65 73 20 69 6e 20 70 50 61 /* Bytes in pPa
17e6f 67 65 20 62 65 79 6f 6e 64 20 74 68 65 20 68 65 ge beyond the he
17e70 61 64 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 61 ader */. int pa
17e71 67 65 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 geFlags;
17e72 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 /* Value
17e73 6f 66 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b of pPage->aData[
17e74 30 5d 20 2a 2f 0a 20 20 69 6e 74 20 73 75 62 74 0] */. int subt
17e75 6f 74 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20 otal;
17e76 20 20 20 20 20 2f 2a 20 53 75 62 74 6f 74 61 6c /* Subtotal
17e77 20 6f 66 20 62 79 74 65 73 20 69 6e 20 63 65 6c of bytes in cel
17e78 6c 73 20 6f 6e 20 6f 6e 65 20 70 61 67 65 20 2a ls on one page *
17e79 2f 0a 20 20 69 6e 74 20 69 53 70 61 63 65 31 20 /. int iSpace1
17e7a 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 = 0;
17e7b 20 2f 2a 20 46 69 72 73 74 20 75 6e 75 73 65 64 /* First unused
17e7c 20 62 79 74 65 20 6f 66 20 61 53 70 61 63 65 31 byte of aSpace1
17e7d 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 76 66 [] */. int iOvf
17e7e 6c 53 70 61 63 65 20 3d 20 30 3b 20 20 20 20 20 lSpace = 0;
17e7f 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 75 6e /* First un
17e80 75 73 65 64 20 62 79 74 65 20 6f 66 20 61 4f 76 used byte of aOv
17e81 66 6c 53 70 61 63 65 5b 5d 20 2a 2f 0a 20 20 69 flSpace[] */. i
17e82 6e 74 20 73 7a 53 63 72 61 74 63 68 3b 20 20 20 nt szScratch;
17e83 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
17e84 69 7a 65 20 6f 66 20 73 63 72 61 74 63 68 20 6d ize of scratch m
17e85 65 6d 6f 72 79 20 72 65 71 75 65 73 74 65 64 20 emory requested
17e86 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 61 70 */. MemPage *ap
17e87 4f 6c 64 5b 4e 42 5d 3b 20 20 20 20 20 20 20 20 Old[NB];
17e88 20 20 2f 2a 20 70 50 61 67 65 20 61 6e 64 20 75 /* pPage and u
17e89 70 20 74 6f 20 74 77 6f 20 73 69 62 6c 69 6e 67 p to two sibling
17e8a 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a s */. MemPage *
17e8b 61 70 43 6f 70 79 5b 4e 42 5d 3b 20 20 20 20 20 apCopy[NB];
17e8c 20 20 20 20 2f 2a 20 50 72 69 76 61 74 65 20 63 /* Private c
17e8d 6f 70 69 65 73 20 6f 66 20 61 70 4f 6c 64 5b 5d opies of apOld[]
17e8e 20 70 61 67 65 73 20 2a 2f 0a 20 20 4d 65 6d 50 pages */. MemP
17e8f 61 67 65 20 2a 61 70 4e 65 77 5b 4e 42 2b 32 5d age *apNew[NB+2]
17e90 3b 20 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67 ; /* pPag
17e91 65 20 61 6e 64 20 75 70 20 74 6f 20 4e 42 20 73 e and up to NB s
17e92 69 62 6c 69 6e 67 73 20 61 66 74 65 72 20 62 61 iblings after ba
17e93 6c 61 6e 63 69 6e 67 20 2a 2f 0a 20 20 75 38 20 lancing */. u8
17e94 2a 70 52 69 67 68 74 3b 20 20 20 20 20 20 20 20 *pRight;
17e95 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 63 /* Loc
17e96 61 74 69 6f 6e 20 69 6e 20 70 61 72 65 6e 74 20 ation in parent
17e97 6f 66 20 72 69 67 68 74 2d 73 69 62 6c 69 6e 67 of right-sibling
17e98 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 75 38 pointer */. u8
17e99 20 2a 61 70 44 69 76 5b 4e 42 2d 31 5d 3b 20 20 *apDiv[NB-1];
17e9a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69 /* Di
17e9b 76 69 64 65 72 20 63 65 6c 6c 73 20 69 6e 20 70 vider cells in p
17e9c 50 61 72 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 Parent */. int
17e9d 63 6e 74 4e 65 77 5b 4e 42 2b 32 5d 3b 20 20 20 cntNew[NB+2];
17e9e 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 /* Inde
17e9f 78 20 69 6e 20 61 43 65 6c 6c 5b 5d 20 6f 66 20 x in aCell[] of
17ea0 63 65 6c 6c 20 61 66 74 65 72 20 69 2d 74 68 20 cell after i-th
17ea1 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a page */. int sz
17ea2 4e 65 77 5b 4e 42 2b 32 5d 3b 20 20 20 20 20 20 New[NB+2];
17ea3 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d 62 69 6e /* Combin
17ea4 65 64 20 73 69 7a 65 20 6f 66 20 63 65 6c 6c 73 ed size of cells
17ea5 20 70 6c 61 63 65 20 6f 6e 20 69 2d 74 68 20 70 place on i-th p
17ea6 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 2a 61 70 age */. u8 **ap
17ea7 43 65 6c 6c 20 3d 20 30 3b 20 20 20 20 20 20 20 Cell = 0;
17ea8 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 63 65 6c /* All cel
17ea9 6c 73 20 62 65 67 69 6e 20 62 61 6c 61 6e 63 65 ls begin balance
17eaa 64 20 2a 2f 0a 20 20 75 31 36 20 2a 73 7a 43 65 d */. u16 *szCe
17eab 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ll;
17eac 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 73 69 7a /* Local siz
17ead 65 20 6f 66 20 61 6c 6c 20 63 65 6c 6c 73 20 69 e of all cells i
17eae 6e 20 61 70 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 n apCell[] */.
17eaf 75 38 20 2a 61 53 70 61 63 65 31 3b 20 20 20 20 u8 *aSpace1;
17eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
17eb1 53 70 61 63 65 20 66 6f 72 20 63 6f 70 69 65 73 Space for copies
17eb2 20 6f 66 20 64 69 76 69 64 65 72 73 20 63 65 6c of dividers cel
17eb3 6c 73 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e ls */. Pgno pgn
17eb4 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 o;
17eb5 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 76 61 72 /* Temp var
17eb6 20 74 6f 20 73 74 6f 72 65 20 61 20 70 61 67 65 to store a page
17eb7 20 6e 75 6d 62 65 72 20 69 6e 20 2a 2f 0a 0a 20 number in */..
17eb8 20 70 42 74 20 3d 20 70 50 61 72 65 6e 74 2d 3e pBt = pParent->
17eb9 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 pBt;. assert( s
17eba 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
17ebb 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b d(pBt->mutex) );
17ebc 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
17ebd 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
17ebe 6c 65 28 70 50 61 72 65 6e 74 2d 3e 70 44 62 50 le(pParent->pDbP
17ebf 61 67 65 29 20 29 3b 0a 0a 23 69 66 20 30 0a 20 age) );..#if 0.
17ec0 20 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45 TRACE(("BALANCE
17ec1 3a 20 62 65 67 69 6e 20 70 61 67 65 20 25 64 20 : begin page %d
17ec2 63 68 69 6c 64 20 6f 66 20 25 64 5c 6e 22 2c 20 child of %d\n",
17ec3 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 70 50 61 pPage->pgno, pPa
17ec4 72 65 6e 74 2d 3e 70 67 6e 6f 29 29 3b 0a 23 65 rent->pgno));.#e
17ec5 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 74 20 74 68 ndif.. /* At th
17ec6 69 73 20 70 6f 69 6e 74 20 70 50 61 72 65 6e 74 is point pParent
17ec7 20 6d 61 79 20 68 61 76 65 20 61 74 20 6d 6f 73 may have at mos
17ec8 74 20 6f 6e 65 20 6f 76 65 72 66 6c 6f 77 20 63 t one overflow c
17ec9 65 6c 6c 2e 20 41 6e 64 20 69 66 0a 20 20 2a 2a ell. And if. **
17eca 20 74 68 69 73 20 6f 76 65 72 66 6c 6f 77 20 63 this overflow c
17ecb 65 6c 6c 20 69 73 20 70 72 65 73 65 6e 74 2c 20 ell is present,
17ecc 69 74 20 6d 75 73 74 20 62 65 20 74 68 65 20 63 it must be the c
17ecd 65 6c 6c 20 77 69 74 68 20 0a 20 20 2a 2a 20 69 ell with . ** i
17ece 6e 64 65 78 20 69 50 61 72 65 6e 74 49 64 78 2e ndex iParentIdx.
17ecf 20 54 68 69 73 20 73 63 65 6e 61 72 69 6f 20 63 This scenario c
17ed0 6f 6d 65 73 20 61 62 6f 75 74 20 77 68 65 6e 20 omes about when
17ed1 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 20 20 this function.
17ed2 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 28 69 6e ** is called (in
17ed3 64 69 72 65 63 74 6c 79 29 20 66 72 6f 6d 20 73 directly) from s
17ed4 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65 74 qlite3BtreeDelet
17ed5 65 28 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 e().. */. asse
17ed6 72 74 28 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 rt( pParent->nOv
17ed7 65 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20 70 50 61 erflow==0 || pPa
17ed8 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d rent->nOverflow=
17ed9 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =1 );. assert(
17eda 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c pParent->nOverfl
17edb 6f 77 3d 3d 30 20 7c 7c 20 70 50 61 72 65 6e 74 ow==0 || pParent
17edc 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64 78 3d 3d ->aOvfl[0].idx==
17edd 69 50 61 72 65 6e 74 49 64 78 20 29 3b 0a 0a 20 iParentIdx );..
17ede 20 69 66 28 20 21 61 4f 76 66 6c 53 70 61 63 65 if( !aOvflSpace
17edf 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
17ee0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d QLITE_NOMEM;. }
17ee1 0a 0a 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 20 .. /* Find the
17ee2 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 74 6f sibling pages to
17ee3 20 62 61 6c 61 6e 63 65 2e 20 41 6c 73 6f 20 6c balance. Also l
17ee4 6f 63 61 74 65 20 74 68 65 20 63 65 6c 6c 73 20 ocate the cells
17ee5 69 6e 20 70 50 61 72 65 6e 74 20 0a 20 20 2a 2a in pParent . **
17ee6 20 74 68 61 74 20 64 69 76 69 64 65 20 74 68 65 that divide the
17ee7 20 73 69 62 6c 69 6e 67 73 2e 20 41 6e 20 61 74 siblings. An at
17ee8 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f tempt is made to
17ee9 20 66 69 6e 64 20 4e 4e 20 73 69 62 6c 69 6e 67 find NN sibling
17eea 73 20 6f 6e 20 0a 20 20 2a 2a 20 65 69 74 68 65 s on . ** eithe
17eeb 72 20 73 69 64 65 20 6f 66 20 70 50 61 67 65 2e r side of pPage.
17eec 20 4d 6f 72 65 20 73 69 62 6c 69 6e 67 73 20 61 More siblings a
17eed 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 6f 6e re taken from on
17eee 65 20 73 69 64 65 2c 20 68 6f 77 65 76 65 72 2c e side, however,
17eef 20 0a 20 20 2a 2a 20 69 66 20 74 68 65 72 65 20 . ** if there
17ef0 61 72 65 20 66 65 77 65 72 20 74 68 61 6e 20 4e are fewer than N
17ef1 4e 20 73 69 62 6c 69 6e 67 73 20 6f 6e 20 74 68 N siblings on th
17ef2 65 20 6f 74 68 65 72 20 73 69 64 65 2e 20 49 66 e other side. If
17ef3 20 70 50 61 72 65 6e 74 0a 20 20 2a 2a 20 68 61 pParent. ** ha
17ef4 73 20 4e 42 20 6f 72 20 66 65 77 65 72 20 63 68 s NB or fewer ch
17ef5 69 6c 64 72 65 6e 20 74 68 65 6e 20 61 6c 6c 20 ildren then all
17ef6 63 68 69 6c 64 72 65 6e 20 6f 66 20 70 50 61 72 children of pPar
17ef7 65 6e 74 20 61 72 65 20 74 61 6b 65 6e 2e 20 20 ent are taken.
17ef8 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 . **. ** This
17ef9 6c 6f 6f 70 20 61 6c 73 6f 20 64 72 6f 70 73 20 loop also drops
17efa 74 68 65 20 64 69 76 69 64 65 72 20 63 65 6c 6c the divider cell
17efb 73 20 66 72 6f 6d 20 74 68 65 20 70 61 72 65 6e s from the paren
17efc 74 20 70 61 67 65 2e 20 54 68 69 73 0a 20 20 2a t page. This. *
17efd 2a 20 77 61 79 2c 20 74 68 65 20 72 65 6d 61 69 * way, the remai
17efe 6e 64 65 72 20 6f 66 20 74 68 65 20 66 75 6e 63 nder of the func
17eff 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 68 61 tion does not ha
17f00 76 65 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 ve to deal with
17f01 61 6e 79 0a 20 20 2a 2a 20 6f 76 65 72 66 6c 6f any. ** overflo
17f02 77 20 63 65 6c 6c 73 20 69 6e 20 74 68 65 20 70 w cells in the p
17f03 61 72 65 6e 74 20 70 61 67 65 2c 20 73 69 6e 63 arent page, sinc
17f04 65 20 69 66 20 61 6e 79 20 65 78 69 73 74 65 64 e if any existed
17f05 20 74 68 65 79 20 77 69 6c 6c 0a 20 20 2a 2a 20 they will. **
17f06 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 have already bee
17f07 6e 20 72 65 6d 6f 76 65 64 2e 0a 20 20 2a 2f 0a n removed.. */.
17f08 20 20 69 20 3d 20 70 50 61 72 65 6e 74 2d 3e 6e i = pParent->n
17f09 4f 76 65 72 66 6c 6f 77 20 2b 20 70 50 61 72 65 Overflow + pPare
17f0a 6e 74 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 69 66 28 nt->nCell;. if(
17f0b 20 69 3c 32 20 29 7b 0a 20 20 20 20 6e 78 44 69 i<2 ){. nxDi
17f0c 76 20 3d 20 30 3b 0a 20 20 20 20 6e 4f 6c 64 20 v = 0;. nOld
17f0d 3d 20 69 2b 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a = i+1;. }else{.
17f0e 20 20 20 20 6e 4f 6c 64 20 3d 20 33 3b 0a 20 20 nOld = 3;.
17f0f 20 20 69 66 28 20 69 50 61 72 65 6e 74 49 64 78 if( iParentIdx
17f10 3d 3d 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 ==0 ){
17f11 20 20 20 20 20 20 20 0a 20 20 20 20 20 20 6e 78 . nx
17f12 44 69 76 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c Div = 0;. }el
17f13 73 65 20 69 66 28 20 69 50 61 72 65 6e 74 49 64 se if( iParentId
17f14 78 3d 3d 69 20 29 7b 0a 20 20 20 20 20 20 6e 78 x==i ){. nx
17f15 44 69 76 20 3d 20 69 2d 32 3b 0a 20 20 20 20 7d Div = i-2;. }
17f16 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 78 44 69 else{. nxDi
17f17 76 20 3d 20 69 50 61 72 65 6e 74 49 64 78 2d 31 v = iParentIdx-1
17f18 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 20 3d 20 ;. }. i =
17f19 32 3b 0a 20 20 7d 0a 20 20 69 66 28 20 28 69 2b 2;. }. if( (i+
17f1a 6e 78 44 69 76 2d 70 50 61 72 65 6e 74 2d 3e 6e nxDiv-pParent->n
17f1b 4f 76 65 72 66 6c 6f 77 29 3d 3d 70 50 61 72 65 Overflow)==pPare
17f1c 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 nt->nCell ){.
17f1d 20 70 52 69 67 68 74 20 3d 20 26 70 50 61 72 65 pRight = &pPare
17f1e 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 6e nt->aData[pParen
17f1f 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 3b t->hdrOffset+8];
17f20 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 52 . }else{. pR
17f21 69 67 68 74 20 3d 20 66 69 6e 64 43 65 6c 6c 28 ight = findCell(
17f22 70 50 61 72 65 6e 74 2c 20 69 2b 6e 78 44 69 76 pParent, i+nxDiv
17f23 2d 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 -pParent->nOverf
17f24 6c 6f 77 29 3b 0a 20 20 7d 0a 20 20 70 67 6e 6f low);. }. pgno
17f25 20 3d 20 67 65 74 34 62 79 74 65 28 70 52 69 67 = get4byte(pRig
17f26 68 74 29 3b 0a 20 20 77 68 69 6c 65 28 20 31 20 ht);. while( 1
17f27 29 7b 0a 20 20 20 20 72 63 20 3d 20 67 65 74 41 ){. rc = getA
17f28 6e 64 49 6e 69 74 50 61 67 65 28 70 42 74 2c 20 ndInitPage(pBt,
17f29 70 67 6e 6f 2c 20 26 61 70 4f 6c 64 5b 69 5d 29 pgno, &apOld[i])
17f2a 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a ;. if( rc ){.
17f2b 20 20 20 20 20 20 6d 65 6d 73 65 74 28 61 70 4f memset(apO
17f2c 6c 64 2c 20 30 2c 20 28 69 2b 31 29 2a 73 69 7a ld, 0, (i+1)*siz
17f2d 65 6f 66 28 4d 65 6d 50 61 67 65 2a 29 29 3b 0a eof(MemPage*));.
17f2e 20 20 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e goto balan
17f2f 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 ce_cleanup;.
17f30 7d 0a 20 20 20 20 6e 4d 61 78 43 65 6c 6c 73 20 }. nMaxCells
17f31 2b 3d 20 31 2b 61 70 4f 6c 64 5b 69 5d 2d 3e 6e += 1+apOld[i]->n
17f32 43 65 6c 6c 2b 61 70 4f 6c 64 5b 69 5d 2d 3e 6e Cell+apOld[i]->n
17f33 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 69 66 Overflow;. if
17f34 28 20 28 69 2d 2d 29 3d 3d 30 20 29 20 62 72 65 ( (i--)==0 ) bre
17f35 61 6b 3b 0a 0a 20 20 20 20 69 66 28 20 69 2b 6e ak;.. if( i+n
17f36 78 44 69 76 3d 3d 70 50 61 72 65 6e 74 2d 3e 61 xDiv==pParent->a
17f37 4f 76 66 6c 5b 30 5d 2e 69 64 78 20 26 26 20 70 Ovfl[0].idx && p
17f38 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f Parent->nOverflo
17f39 77 20 29 7b 0a 20 20 20 20 20 20 61 70 44 69 76 w ){. apDiv
17f3a 5b 69 5d 20 3d 20 70 50 61 72 65 6e 74 2d 3e 61 [i] = pParent->a
17f3b 4f 76 66 6c 5b 30 5d 2e 70 43 65 6c 6c 3b 0a 20 Ovfl[0].pCell;.
17f3c 20 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 pgno = get4
17f3d 62 79 74 65 28 61 70 44 69 76 5b 69 5d 29 3b 0a byte(apDiv[i]);.
17f3e 20 20 20 20 20 20 73 7a 4e 65 77 5b 69 5d 20 3d szNew[i] =
17f3f 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 cellSizePtr(pPa
17f40 72 65 6e 74 2c 20 61 70 44 69 76 5b 69 5d 29 3b rent, apDiv[i]);
17f41 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e . pParent->
17f42 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 nOverflow = 0;.
17f43 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
17f44 61 70 44 69 76 5b 69 5d 20 3d 20 66 69 6e 64 43 apDiv[i] = findC
17f45 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 69 2b 6e ell(pParent, i+n
17f46 78 44 69 76 2d 70 50 61 72 65 6e 74 2d 3e 6e 4f xDiv-pParent->nO
17f47 76 65 72 66 6c 6f 77 29 3b 0a 20 20 20 20 20 20 verflow);.
17f48 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 pgno = get4byte(
17f49 61 70 44 69 76 5b 69 5d 29 3b 0a 20 20 20 20 20 apDiv[i]);.
17f4a 20 73 7a 4e 65 77 5b 69 5d 20 3d 20 63 65 6c 6c szNew[i] = cell
17f4b 53 69 7a 65 50 74 72 28 70 50 61 72 65 6e 74 2c SizePtr(pParent,
17f4c 20 61 70 44 69 76 5b 69 5d 29 3b 0a 0a 20 20 20 apDiv[i]);..
17f4d 20 20 20 2f 2a 20 44 72 6f 70 20 74 68 65 20 63 /* Drop the c
17f4e 65 6c 6c 20 66 72 6f 6d 20 74 68 65 20 70 61 72 ell from the par
17f4f 65 6e 74 20 70 61 67 65 2e 20 61 70 44 69 76 5b ent page. apDiv[
17f50 69 5d 20 73 74 69 6c 6c 20 70 6f 69 6e 74 73 20 i] still points
17f51 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 to. ** the
17f52 63 65 6c 6c 20 77 69 74 68 69 6e 20 74 68 65 20 cell within the
17f53 70 61 72 65 6e 74 2c 20 65 76 65 6e 20 74 68 6f parent, even tho
17f54 75 67 68 20 69 74 20 68 61 73 20 62 65 65 6e 20 ugh it has been
17f55 64 72 6f 70 70 65 64 2e 0a 20 20 20 20 20 20 2a dropped.. *
17f56 2a 20 54 68 69 73 20 69 73 20 73 61 66 65 20 62 * This is safe b
17f57 65 63 61 75 73 65 20 64 72 6f 70 70 69 6e 67 20 ecause dropping
17f58 61 20 63 65 6c 6c 20 6f 6e 6c 79 20 6f 76 65 72 a cell only over
17f59 77 72 69 74 65 73 20 74 68 65 20 66 69 72 73 74 writes the first
17f5a 0a 20 20 20 20 20 20 2a 2a 20 66 6f 75 72 20 62 . ** four b
17f5b 79 74 65 73 20 6f 66 20 69 74 2c 20 61 6e 64 20 ytes of it, and
17f5c 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 6f this function do
17f5d 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 68 65 20 es not need the
17f5e 66 69 72 73 74 0a 20 20 20 20 20 20 2a 2a 20 66 first. ** f
17f5f 6f 75 72 20 62 79 74 65 73 20 6f 66 20 74 68 65 our bytes of the
17f60 20 64 69 76 69 64 65 72 20 63 65 6c 6c 2e 20 53 divider cell. S
17f61 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 20 69 73 o the pointer is
17f62 20 73 61 66 65 20 74 6f 20 75 73 65 0a 20 20 20 safe to use.
17f63 20 20 20 2a 2a 20 6c 61 74 65 72 20 6f 6e 2e 20 ** later on.
17f64 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 . **.
17f65 20 2a 2a 20 55 6e 6c 65 73 73 20 53 51 4c 69 74 ** Unless SQLit
17f66 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 69 6e e is compiled in
17f67 20 73 65 63 75 72 65 2d 64 65 6c 65 74 65 20 6d secure-delete m
17f68 6f 64 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 ode. In this cas
17f69 65 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 e,. ** the
17f6a 64 72 6f 70 43 65 6c 6c 28 29 20 72 6f 75 74 69 dropCell() routi
17f6b 6e 65 20 77 69 6c 6c 20 6f 76 65 72 77 72 69 74 ne will overwrit
17f6c 65 20 74 68 65 20 65 6e 74 69 72 65 20 63 65 6c e the entire cel
17f6d 6c 20 77 69 74 68 20 7a 65 72 6f 65 73 2e 0a 20 l with zeroes..
17f6e 20 20 20 20 20 2a 2a 20 49 6e 20 74 68 69 73 20 ** In this
17f6f 63 61 73 65 2c 20 74 65 6d 70 6f 72 61 72 69 6c case, temporaril
17f70 79 20 63 6f 70 79 20 74 68 65 20 63 65 6c 6c 20 y copy the cell
17f71 69 6e 74 6f 20 74 68 65 20 61 4f 76 66 6c 53 70 into the aOvflSp
17f72 61 63 65 5b 5d 0a 20 20 20 20 20 20 2a 2a 20 62 ace[]. ** b
17f73 75 66 66 65 72 2e 20 49 74 20 77 69 6c 6c 20 62 uffer. It will b
17f74 65 20 63 6f 70 69 65 64 20 6f 75 74 20 61 67 61 e copied out aga
17f75 69 6e 20 61 73 20 73 6f 6f 6e 20 61 73 20 74 68 in as soon as th
17f76 65 20 61 53 70 61 63 65 5b 5d 20 62 75 66 66 65 e aSpace[] buffe
17f77 72 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 61 6c r. ** is al
17f78 6c 6f 63 61 74 65 64 2e 20 20 2a 2f 0a 23 69 66 located. */.#if
17f79 64 65 66 20 53 51 4c 49 54 45 5f 53 45 43 55 52 def SQLITE_SECUR
17f7a 45 5f 44 45 4c 45 54 45 0a 20 20 20 20 20 20 6d E_DELETE. m
17f7b 65 6d 63 70 79 28 26 61 4f 76 66 6c 53 70 61 63 emcpy(&aOvflSpac
17f7c 65 5b 61 70 44 69 76 5b 69 5d 2d 70 50 61 72 65 e[apDiv[i]-pPare
17f7d 6e 74 2d 3e 61 44 61 74 61 5d 2c 20 61 70 44 69 nt->aData], apDi
17f7e 76 5b 69 5d 2c 20 73 7a 4e 65 77 5b 69 5d 29 3b v[i], szNew[i]);
17f7f 0a 20 20 20 20 20 20 61 70 44 69 76 5b 69 5d 20 . apDiv[i]
17f80 3d 20 26 61 4f 76 66 6c 53 70 61 63 65 5b 61 70 = &aOvflSpace[ap
17f81 44 69 76 5b 69 5d 2d 70 50 61 72 65 6e 74 2d 3e Div[i]-pParent->
17f82 61 44 61 74 61 5d 3b 0a 23 65 6e 64 69 66 0a 20 aData];.#endif.
17f83 20 20 20 20 20 64 72 6f 70 43 65 6c 6c 28 70 50 dropCell(pP
17f84 61 72 65 6e 74 2c 20 69 2b 6e 78 44 69 76 2d 70 arent, i+nxDiv-p
17f85 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f Parent->nOverflo
17f86 77 2c 20 73 7a 4e 65 77 5b 69 5d 2c 20 26 72 63 w, szNew[i], &rc
17f87 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 );. }. }..
17f88 2f 2a 20 4d 61 6b 65 20 6e 4d 61 78 43 65 6c 6c /* Make nMaxCell
17f89 73 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 s a multiple of
17f8a 34 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 70 72 4 in order to pr
17f8b 65 73 65 72 76 65 20 38 2d 62 79 74 65 0a 20 20 eserve 8-byte.
17f8c 2a 2a 20 61 6c 69 67 6e 6d 65 6e 74 20 2a 2f 0a ** alignment */.
17f8d 20 20 6e 4d 61 78 43 65 6c 6c 73 20 3d 20 28 6e nMaxCells = (n
17f8e 4d 61 78 43 65 6c 6c 73 20 2b 20 33 29 26 7e 33 MaxCells + 3)&~3
17f8f 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c 6c ;.. /*. ** All
17f90 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 20 ocate space for
17f91 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65 memory structure
17f92 73 0a 20 20 2a 2f 0a 20 20 6b 20 3d 20 70 42 74 s. */. k = pBt
17f93 2d 3e 70 61 67 65 53 69 7a 65 20 2b 20 52 4f 55 ->pageSize + ROU
17f94 4e 44 38 28 73 69 7a 65 6f 66 28 4d 65 6d 50 61 ND8(sizeof(MemPa
17f95 67 65 29 29 3b 0a 20 20 73 7a 53 63 72 61 74 63 ge));. szScratc
17f96 68 20 3d 0a 20 20 20 20 20 20 20 6e 4d 61 78 43 h =. nMaxC
17f97 65 6c 6c 73 2a 73 69 7a 65 6f 66 28 75 38 2a 29 ells*sizeof(u8*)
17f98 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17f99 20 20 20 20 20 20 20 2f 2a 20 61 70 43 65 6c 6c /* apCell
17f9a 20 2a 2f 0a 20 20 20 20 20 2b 20 6e 4d 61 78 43 */. + nMaxC
17f9b 65 6c 6c 73 2a 73 69 7a 65 6f 66 28 75 31 36 29 ells*sizeof(u16)
17f9c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17f9d 20 20 20 20 20 20 20 2f 2a 20 73 7a 43 65 6c 6c /* szCell
17f9e 20 2a 2f 0a 20 20 20 20 20 2b 20 70 42 74 2d 3e */. + pBt->
17f9f 70 61 67 65 53 69 7a 65 20 20 20 20 20 20 20 20 pageSize
17fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17fa1 20 20 20 20 20 20 20 2f 2a 20 61 53 70 61 63 65 /* aSpace
17fa2 31 20 2a 2f 0a 20 20 20 20 20 2b 20 6b 2a 6e 4f 1 */. + k*nO
17fa3 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ld;
17fa4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17fa5 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 /* Page
17fa6 63 6f 70 69 65 73 20 28 61 70 43 6f 70 79 29 20 copies (apCopy)
17fa7 2a 2f 0a 20 20 61 70 43 65 6c 6c 20 3d 20 73 71 */. apCell = sq
17fa8 6c 69 74 65 33 53 63 72 61 74 63 68 4d 61 6c 6c lite3ScratchMall
17fa9 6f 63 28 20 73 7a 53 63 72 61 74 63 68 20 29 3b oc( szScratch );
17faa 20 0a 20 20 69 66 28 20 61 70 43 65 6c 6c 3d 3d . if( apCell==
17fab 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 0 ){. rc = SQ
17fac 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 LITE_NOMEM;.
17fad 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 goto balance_cle
17fae 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 73 7a 43 65 anup;. }. szCe
17faf 6c 6c 20 3d 20 28 75 31 36 2a 29 26 61 70 43 65 ll = (u16*)&apCe
17fb0 6c 6c 5b 6e 4d 61 78 43 65 6c 6c 73 5d 3b 0a 20 ll[nMaxCells];.
17fb1 20 61 53 70 61 63 65 31 20 3d 20 28 75 38 2a 29 aSpace1 = (u8*)
17fb2 26 73 7a 43 65 6c 6c 5b 6e 4d 61 78 43 65 6c 6c &szCell[nMaxCell
17fb3 73 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 s];. assert( EI
17fb4 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 GHT_BYTE_ALIGNME
17fb5 4e 54 28 61 53 70 61 63 65 31 29 20 29 3b 0a 0a NT(aSpace1) );..
17fb6 20 20 2f 2a 0a 20 20 2a 2a 20 4c 6f 61 64 20 70 /*. ** Load p
17fb7 6f 69 6e 74 65 72 73 20 74 6f 20 61 6c 6c 20 63 ointers to all c
17fb8 65 6c 6c 73 20 6f 6e 20 73 69 62 6c 69 6e 67 20 ells on sibling
17fb9 70 61 67 65 73 20 61 6e 64 20 74 68 65 20 64 69 pages and the di
17fba 76 69 64 65 72 20 63 65 6c 6c 73 0a 20 20 2a 2a vider cells. **
17fbb 20 69 6e 74 6f 20 74 68 65 20 6c 6f 63 61 6c 20 into the local
17fbc 61 70 43 65 6c 6c 5b 5d 20 61 72 72 61 79 2e 20 apCell[] array.
17fbd 20 4d 61 6b 65 20 63 6f 70 69 65 73 20 6f 66 20 Make copies of
17fbe 74 68 65 20 64 69 76 69 64 65 72 20 63 65 6c 6c the divider cell
17fbf 73 0a 20 20 2a 2a 20 69 6e 74 6f 20 73 70 61 63 s. ** into spac
17fc0 65 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 e obtained from
17fc1 61 53 70 61 63 65 31 5b 5d 20 61 6e 64 20 72 65 aSpace1[] and re
17fc2 6d 6f 76 65 20 74 68 65 20 74 68 65 20 64 69 76 move the the div
17fc3 69 64 65 72 20 43 65 6c 6c 73 0a 20 20 2a 2a 20 ider Cells. **
17fc4 66 72 6f 6d 20 70 50 61 72 65 6e 74 2e 0a 20 20 from pParent..
17fc5 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 73 **. ** If the s
17fc6 69 62 6c 69 6e 67 73 20 61 72 65 20 6f 6e 20 6c iblings are on l
17fc7 65 61 66 20 70 61 67 65 73 2c 20 74 68 65 6e 20 eaf pages, then
17fc8 74 68 65 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 the child pointe
17fc9 72 73 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 64 rs of the. ** d
17fca 69 76 69 64 65 72 20 63 65 6c 6c 73 20 61 72 65 ivider cells are
17fcb 20 73 74 72 69 70 70 65 64 20 66 72 6f 6d 20 74 stripped from t
17fcc 68 65 20 63 65 6c 6c 73 20 62 65 66 6f 72 65 20 he cells before
17fcd 74 68 65 79 20 61 72 65 20 63 6f 70 69 65 64 0a they are copied.
17fce 20 20 2a 2a 20 69 6e 74 6f 20 61 53 70 61 63 65 ** into aSpace
17fcf 31 5b 5d 2e 20 20 49 6e 20 74 68 69 73 20 77 61 1[]. In this wa
17fd0 79 2c 20 61 6c 6c 20 63 65 6c 6c 73 20 69 6e 20 y, all cells in
17fd1 61 70 43 65 6c 6c 5b 5d 20 61 72 65 20 77 69 74 apCell[] are wit
17fd2 68 6f 75 74 0a 20 20 2a 2a 20 63 68 69 6c 64 20 hout. ** child
17fd3 70 6f 69 6e 74 65 72 73 2e 20 20 49 66 20 73 69 pointers. If si
17fd4 62 6c 69 6e 67 73 20 61 72 65 20 6e 6f 74 20 6c blings are not l
17fd5 65 61 76 65 73 2c 20 74 68 65 6e 20 61 6c 6c 20 eaves, then all
17fd6 63 65 6c 6c 20 69 6e 0a 20 20 2a 2a 20 61 70 43 cell in. ** apC
17fd7 65 6c 6c 5b 5d 20 69 6e 63 6c 75 64 65 20 63 68 ell[] include ch
17fd8 69 6c 64 20 70 6f 69 6e 74 65 72 73 2e 20 20 45 ild pointers. E
17fd9 69 74 68 65 72 20 77 61 79 2c 20 61 6c 6c 20 63 ither way, all c
17fda 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d ells in apCell[]
17fdb 0a 20 20 2a 2a 20 61 72 65 20 61 6c 69 6b 65 2e . ** are alike.
17fdc 0a 20 20 2a 2a 0a 20 20 2a 2a 20 6c 65 61 66 43 . **. ** leafC
17fdd 6f 72 72 65 63 74 69 6f 6e 3a 20 20 34 20 69 66 orrection: 4 if
17fde 20 70 50 61 67 65 20 69 73 20 61 20 6c 65 61 66 pPage is a leaf
17fdf 2e 20 20 30 20 69 66 20 70 50 61 67 65 20 69 73 . 0 if pPage is
17fe0 20 6e 6f 74 20 61 20 6c 65 61 66 2e 0a 20 20 2a not a leaf.. *
17fe1 2a 20 20 20 20 20 20 20 6c 65 61 66 44 61 74 61 * leafData
17fe2 3a 20 20 31 20 69 66 20 70 50 61 67 65 20 68 6f : 1 if pPage ho
17fe3 6c 64 73 20 6b 65 79 2b 64 61 74 61 20 61 6e 64 lds key+data and
17fe4 20 70 50 61 72 65 6e 74 20 68 6f 6c 64 73 20 6f pParent holds o
17fe5 6e 6c 79 20 6b 65 79 73 2e 0a 20 20 2a 2f 0a 20 nly keys.. */.
17fe6 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 20 leafCorrection
17fe7 3d 20 61 70 4f 6c 64 5b 30 5d 2d 3e 6c 65 61 66 = apOld[0]->leaf
17fe8 2a 34 3b 0a 20 20 6c 65 61 66 44 61 74 61 20 3d *4;. leafData =
17fe9 20 61 70 4f 6c 64 5b 30 5d 2d 3e 68 61 73 44 61 apOld[0]->hasDa
17fea 74 61 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 ta;. for(i=0; i
17feb 3c 6e 4f 6c 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 <nOld; i++){.
17fec 20 69 6e 74 20 6c 69 6d 69 74 3b 0a 20 20 20 20 int limit;.
17fed 0a 20 20 20 20 2f 2a 20 42 65 66 6f 72 65 20 64 . /* Before d
17fee 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 20 65 6c oing anything el
17fef 73 65 2c 20 74 61 6b 65 20 61 20 63 6f 70 79 20 se, take a copy
17ff0 6f 66 20 74 68 65 20 69 27 74 68 20 6f 72 69 67 of the i'th orig
17ff1 69 6e 61 6c 20 73 69 62 6c 69 6e 67 0a 20 20 20 inal sibling.
17ff2 20 2a 2a 20 54 68 65 20 72 65 73 74 20 6f 66 20 ** The rest of
17ff3 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 69 this function wi
17ff4 6c 6c 20 75 73 65 20 64 61 74 61 20 66 72 6f 6d ll use data from
17ff5 20 74 68 65 20 63 6f 70 69 65 73 20 72 61 74 68 the copies rath
17ff6 65 72 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 74 er. ** that t
17ff7 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 he original page
17ff8 73 20 73 69 6e 63 65 20 74 68 65 20 6f 72 69 67 s since the orig
17ff9 69 6e 61 6c 20 70 61 67 65 73 20 77 69 6c 6c 20 inal pages will
17ffa 62 65 20 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a be in the. **
17ffb 20 70 72 6f 63 65 73 73 20 6f 66 20 62 65 69 6e process of bein
17ffc 67 20 6f 76 65 72 77 72 69 74 74 65 6e 2e 20 20 g overwritten.
17ffd 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a */. MemPage *
17ffe 70 4f 6c 64 20 3d 20 61 70 43 6f 70 79 5b 69 5d pOld = apCopy[i]
17fff 20 3d 20 28 4d 65 6d 50 61 67 65 2a 29 26 61 53 = (MemPage*)&aS
18000 70 61 63 65 31 5b 70 42 74 2d 3e 70 61 67 65 53 pace1[pBt->pageS
18001 69 7a 65 20 2b 20 6b 2a 69 5d 3b 0a 20 20 20 20 ize + k*i];.
18002 6d 65 6d 63 70 79 28 70 4f 6c 64 2c 20 61 70 4f memcpy(pOld, apO
18003 6c 64 5b 69 5d 2c 20 73 69 7a 65 6f 66 28 4d 65 ld[i], sizeof(Me
18004 6d 50 61 67 65 29 29 3b 0a 20 20 20 20 70 4f 6c mPage));. pOl
18005 64 2d 3e 61 44 61 74 61 20 3d 20 28 76 6f 69 64 d->aData = (void
18006 2a 29 26 70 4f 6c 64 5b 31 5d 3b 0a 20 20 20 20 *)&pOld[1];.
18007 6d 65 6d 63 70 79 28 70 4f 6c 64 2d 3e 61 44 61 memcpy(pOld->aDa
18008 74 61 2c 20 61 70 4f 6c 64 5b 69 5d 2d 3e 61 44 ta, apOld[i]->aD
18009 61 74 61 2c 20 70 42 74 2d 3e 70 61 67 65 53 69 ata, pBt->pageSi
1800a 7a 65 29 3b 0a 0a 20 20 20 20 6c 69 6d 69 74 20 ze);.. limit
1800b 3d 20 70 4f 6c 64 2d 3e 6e 43 65 6c 6c 2b 70 4f = pOld->nCell+pO
1800c 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 ld->nOverflow;.
1800d 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6c 69 for(j=0; j<li
1800e 6d 69 74 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 mit; j++){.
1800f 20 61 73 73 65 72 74 28 20 6e 43 65 6c 6c 3c 6e assert( nCell<n
18010 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 MaxCells );.
18011 20 20 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 apCell[nCell]
18012 3d 20 66 69 6e 64 4f 76 65 72 66 6c 6f 77 43 65 = findOverflowCe
18013 6c 6c 28 70 4f 6c 64 2c 20 6a 29 3b 0a 20 20 20 ll(pOld, j);.
18014 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d szCell[nCell]
18015 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 = cellSizePtr(p
18016 4f 6c 64 2c 20 61 70 43 65 6c 6c 5b 6e 43 65 6c Old, apCell[nCel
18017 6c 5d 29 3b 0a 20 20 20 20 20 20 6e 43 65 6c 6c l]);. nCell
18018 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 ++;. }. if
18019 28 20 69 3c 6e 4f 6c 64 2d 31 20 26 26 20 21 6c ( i<nOld-1 && !l
1801a 65 61 66 44 61 74 61 29 7b 0a 20 20 20 20 20 20 eafData){.
1801b 75 31 36 20 73 7a 20 3d 20 28 75 31 36 29 73 7a u16 sz = (u16)sz
1801c 4e 65 77 5b 69 5d 3b 0a 20 20 20 20 20 20 75 38 New[i];. u8
1801d 20 2a 70 54 65 6d 70 3b 0a 20 20 20 20 20 20 61 *pTemp;. a
1801e 73 73 65 72 74 28 20 6e 43 65 6c 6c 3c 6e 4d 61 ssert( nCell<nMa
1801f 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 20 20 xCells );.
18020 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 szCell[nCell] =
18021 73 7a 3b 0a 20 20 20 20 20 20 70 54 65 6d 70 20 sz;. pTemp
18022 3d 20 26 61 53 70 61 63 65 31 5b 69 53 70 61 63 = &aSpace1[iSpac
18023 65 31 5d 3b 0a 20 20 20 20 20 20 69 53 70 61 63 e1];. iSpac
18024 65 31 20 2b 3d 20 73 7a 3b 0a 20 20 20 20 20 20 e1 += sz;.
18025 61 73 73 65 72 74 28 20 73 7a 3c 3d 70 42 74 2d assert( sz<=pBt-
18026 3e 70 61 67 65 53 69 7a 65 2f 34 20 29 3b 0a 20 >pageSize/4 );.
18027 20 20 20 20 20 61 73 73 65 72 74 28 20 69 53 70 assert( iSp
18028 61 63 65 31 3c 3d 70 42 74 2d 3e 70 61 67 65 53 ace1<=pBt->pageS
18029 69 7a 65 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d ize );. mem
1802a 63 70 79 28 70 54 65 6d 70 2c 20 61 70 44 69 76 cpy(pTemp, apDiv
1802b 5b 69 5d 2c 20 73 7a 29 3b 0a 20 20 20 20 20 20 [i], sz);.
1802c 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 apCell[nCell] =
1802d 70 54 65 6d 70 2b 6c 65 61 66 43 6f 72 72 65 63 pTemp+leafCorrec
1802e 74 69 6f 6e 3b 0a 20 20 20 20 20 20 61 73 73 65 tion;. asse
1802f 72 74 28 20 6c 65 61 66 43 6f 72 72 65 63 74 69 rt( leafCorrecti
18030 6f 6e 3d 3d 30 20 7c 7c 20 6c 65 61 66 43 6f 72 on==0 || leafCor
18031 72 65 63 74 69 6f 6e 3d 3d 34 20 29 3b 0a 20 20 rection==4 );.
18032 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c szCell[nCell
18033 5d 20 3d 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c ] = szCell[nCell
18034 5d 20 2d 20 6c 65 61 66 43 6f 72 72 65 63 74 69 ] - leafCorrecti
18035 6f 6e 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 on;. if( !p
18036 4f 6c 64 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 Old->leaf ){.
18037 20 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 61 assert( lea
18038 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 30 20 29 fCorrection==0 )
18039 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 ;. assert
1803a 28 20 70 4f 6c 64 2d 3e 68 64 72 4f 66 66 73 65 ( pOld->hdrOffse
1803b 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 t==0 );.
1803c 2f 2a 20 54 68 65 20 72 69 67 68 74 20 70 6f 69 /* The right poi
1803d 6e 74 65 72 20 6f 66 20 74 68 65 20 63 68 69 6c nter of the chil
1803e 64 20 70 61 67 65 20 70 4f 6c 64 20 62 65 63 6f d page pOld beco
1803f 6d 65 73 20 74 68 65 20 6c 65 66 74 0a 20 20 20 mes the left.
18040 20 20 20 20 20 2a 2a 20 70 6f 69 6e 74 65 72 20 ** pointer
18041 6f 66 20 74 68 65 20 64 69 76 69 64 65 72 20 63 of the divider c
18042 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 20 20 20 6d ell */. m
18043 65 6d 63 70 79 28 61 70 43 65 6c 6c 5b 6e 43 65 emcpy(apCell[nCe
18044 6c 6c 5d 2c 20 26 70 4f 6c 64 2d 3e 61 44 61 74 ll], &pOld->aDat
18045 61 5b 38 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 a[8], 4);.
18046 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 }else{. a
18047 73 73 65 72 74 28 20 6c 65 61 66 43 6f 72 72 65 ssert( leafCorre
18048 63 74 69 6f 6e 3d 3d 34 20 29 3b 0a 20 20 20 20 ction==4 );.
18049 20 20 20 20 69 66 28 20 73 7a 43 65 6c 6c 5b 6e if( szCell[n
1804a 43 65 6c 6c 5d 3c 34 20 29 7b 0a 20 20 20 20 20 Cell]<4 ){.
1804b 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 61 /* Do not a
1804c 6c 6c 6f 77 20 61 6e 79 20 63 65 6c 6c 73 20 73 llow any cells s
1804d 6d 61 6c 6c 65 72 20 74 68 61 6e 20 34 20 62 79 maller than 4 by
1804e 74 65 73 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 tes. */.
1804f 20 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 szCell[nCell]
18050 3d 20 34 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 = 4;. }.
18051 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6e 43 65 }. nCe
18052 6c 6c 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ll++;. }. }.
18053 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46 69 67 75 72 . /*. ** Figur
18054 65 20 6f 75 74 20 74 68 65 20 6e 75 6d 62 65 72 e out the number
18055 20 6f 66 20 70 61 67 65 73 20 6e 65 65 64 65 64 of pages needed
18056 20 74 6f 20 68 6f 6c 64 20 61 6c 6c 20 6e 43 65 to hold all nCe
18057 6c 6c 20 63 65 6c 6c 73 2e 0a 20 20 2a 2a 20 53 ll cells.. ** S
18058 74 6f 72 65 20 74 68 69 73 20 6e 75 6d 62 65 72 tore this number
18059 20 69 6e 20 22 6b 22 2e 20 20 41 6c 73 6f 20 63 in "k". Also c
1805a 6f 6d 70 75 74 65 20 73 7a 4e 65 77 5b 5d 20 77 ompute szNew[] w
1805b 68 69 63 68 20 69 73 20 74 68 65 20 74 6f 74 61 hich is the tota
1805c 6c 0a 20 20 2a 2a 20 73 69 7a 65 20 6f 66 20 61 l. ** size of a
1805d 6c 6c 20 63 65 6c 6c 73 20 6f 6e 20 74 68 65 20 ll cells on the
1805e 69 2d 74 68 20 70 61 67 65 20 61 6e 64 20 63 6e i-th page and cn
1805f 74 4e 65 77 5b 5d 20 77 68 69 63 68 20 69 73 20 tNew[] which is
18060 74 68 65 20 69 6e 64 65 78 0a 20 20 2a 2a 20 69 the index. ** i
18061 6e 20 61 70 43 65 6c 6c 5b 5d 20 6f 66 20 74 68 n apCell[] of th
18062 65 20 63 65 6c 6c 20 74 68 61 74 20 64 69 76 69 e cell that divi
18063 64 65 73 20 70 61 67 65 20 69 20 66 72 6f 6d 20 des page i from
18064 70 61 67 65 20 69 2b 31 2e 20 20 0a 20 20 2a 2a page i+1. . **
18065 20 63 6e 74 4e 65 77 5b 6b 5d 20 73 68 6f 75 6c cntNew[k] shoul
18066 64 20 65 71 75 61 6c 20 6e 43 65 6c 6c 2e 0a 20 d equal nCell..
18067 20 2a 2a 0a 20 20 2a 2a 20 56 61 6c 75 65 73 20 **. ** Values
18068 63 6f 6d 70 75 74 65 64 20 62 79 20 74 68 69 73 computed by this
18069 20 62 6c 6f 63 6b 3a 0a 20 20 2a 2a 0a 20 20 2a block:. **. *
1806a 2a 20 20 20 20 20 20 20 20 20 20 20 6b 3a 20 54 * k: T
1806b 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 he total number
1806c 6f 66 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73 of sibling pages
1806d 0a 20 20 2a 2a 20 20 20 20 73 7a 4e 65 77 5b 69 . ** szNew[i
1806e 5d 3a 20 53 70 61 63 65 64 20 75 73 65 64 20 6f ]: Spaced used o
1806f 6e 20 74 68 65 20 69 2d 74 68 20 73 69 62 6c 69 n the i-th sibli
18070 6e 67 20 70 61 67 65 2e 0a 20 20 2a 2a 20 20 20 ng page.. **
18071 63 6e 74 4e 65 77 5b 69 5d 3a 20 49 6e 64 65 78 cntNew[i]: Index
18072 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 61 6e 64 in apCell[] and
18073 20 73 7a 43 65 6c 6c 5b 5d 20 66 6f 72 20 74 68 szCell[] for th
18074 65 20 66 69 72 73 74 20 63 65 6c 6c 20 74 6f 0a e first cell to.
18075 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 **
18076 20 20 74 68 65 20 72 69 67 68 74 20 6f 66 20 74 the right of t
18077 68 65 20 69 2d 74 68 20 73 69 62 6c 69 6e 67 20 he i-th sibling
18078 70 61 67 65 2e 0a 20 20 2a 2a 20 75 73 61 62 6c page.. ** usabl
18079 65 53 70 61 63 65 3a 20 4e 75 6d 62 65 72 20 6f eSpace: Number o
1807a 66 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 f bytes of space
1807b 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 65 61 available on ea
1807c 63 68 20 73 69 62 6c 69 6e 67 2e 0a 20 20 2a 2a ch sibling.. **
1807d 20 0a 20 20 2a 2f 0a 20 20 75 73 61 62 6c 65 53 . */. usableS
1807e 70 61 63 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 pace = pBt->usab
1807f 6c 65 53 69 7a 65 20 2d 20 31 32 20 2b 20 6c 65 leSize - 12 + le
18080 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b 0a 20 20 afCorrection;.
18081 66 6f 72 28 73 75 62 74 6f 74 61 6c 3d 6b 3d 69 for(subtotal=k=i
18082 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b =0; i<nCell; i++
18083 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 ){. assert( i
18084 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 <nMaxCells );.
18085 20 20 73 75 62 74 6f 74 61 6c 20 2b 3d 20 73 7a subtotal += sz
18086 43 65 6c 6c 5b 69 5d 20 2b 20 32 3b 0a 20 20 20 Cell[i] + 2;.
18087 20 69 66 28 20 73 75 62 74 6f 74 61 6c 20 3e 20 if( subtotal >
18088 75 73 61 62 6c 65 53 70 61 63 65 20 29 7b 0a 20 usableSpace ){.
18089 20 20 20 20 20 73 7a 4e 65 77 5b 6b 5d 20 3d 20 szNew[k] =
1808a 73 75 62 74 6f 74 61 6c 20 2d 20 73 7a 43 65 6c subtotal - szCel
1808b 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 63 6e 74 4e l[i];. cntN
1808c 65 77 5b 6b 5d 20 3d 20 69 3b 0a 20 20 20 20 20 ew[k] = i;.
1808d 20 69 66 28 20 6c 65 61 66 44 61 74 61 20 29 7b if( leafData ){
1808e 20 69 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 73 75 i--; }. su
1808f 62 74 6f 74 61 6c 20 3d 20 30 3b 0a 20 20 20 20 btotal = 0;.
18090 20 20 6b 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 k++;. if(
18091 20 6b 3e 4e 42 2b 31 20 29 7b 20 72 63 20 3d 20 k>NB+1 ){ rc =
18092 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 3b 20 SQLITE_CORRUPT;
18093 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 goto balance_cle
18094 61 6e 75 70 3b 20 7d 0a 20 20 20 20 7d 0a 20 20 anup; }. }.
18095 7d 0a 20 20 73 7a 4e 65 77 5b 6b 5d 20 3d 20 73 }. szNew[k] = s
18096 75 62 74 6f 74 61 6c 3b 0a 20 20 63 6e 74 4e 65 ubtotal;. cntNe
18097 77 5b 6b 5d 20 3d 20 6e 43 65 6c 6c 3b 0a 20 20 w[k] = nCell;.
18098 6b 2b 2b 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 k++;.. /*. **
18099 54 68 65 20 70 61 63 6b 69 6e 67 20 63 6f 6d 70 The packing comp
1809a 75 74 65 64 20 62 79 20 74 68 65 20 70 72 65 76 uted by the prev
1809b 69 6f 75 73 20 62 6c 6f 63 6b 20 69 73 20 62 69 ious block is bi
1809c 61 73 65 64 20 74 6f 77 61 72 64 20 74 68 65 20 ased toward the
1809d 73 69 62 6c 69 6e 67 73 0a 20 20 2a 2a 20 6f 6e siblings. ** on
1809e 20 74 68 65 20 6c 65 66 74 20 73 69 64 65 2e 20 the left side.
1809f 20 54 68 65 20 6c 65 66 74 20 73 69 62 6c 69 6e The left siblin
180a0 67 73 20 61 72 65 20 61 6c 77 61 79 73 20 6e 65 gs are always ne
180a1 61 72 6c 79 20 66 75 6c 6c 2c 20 77 68 69 6c 65 arly full, while
180a2 20 74 68 65 0a 20 20 2a 2a 20 72 69 67 68 74 2d the. ** right-
180a3 6d 6f 73 74 20 73 69 62 6c 69 6e 67 20 6d 69 67 most sibling mig
180a4 68 74 20 62 65 20 6e 65 61 72 6c 79 20 65 6d 70 ht be nearly emp
180a5 74 79 2e 20 20 54 68 69 73 20 62 6c 6f 63 6b 20 ty. This block
180a6 6f 66 20 63 6f 64 65 20 61 74 74 65 6d 70 74 73 of code attempts
180a7 0a 20 20 2a 2a 20 74 6f 20 61 64 6a 75 73 74 20 . ** to adjust
180a8 74 68 65 20 70 61 63 6b 69 6e 67 20 6f 66 20 73 the packing of s
180a9 69 62 6c 69 6e 67 73 20 74 6f 20 67 65 74 20 61 iblings to get a
180aa 20 62 65 74 74 65 72 20 62 61 6c 61 6e 63 65 2e better balance.
180ab 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 . **. ** This
180ac 61 64 6a 75 73 74 6d 65 6e 74 20 69 73 20 6d 6f adjustment is mo
180ad 72 65 20 74 68 61 6e 20 61 6e 20 6f 70 74 69 6d re than an optim
180ae 69 7a 61 74 69 6f 6e 2e 20 20 54 68 65 20 70 61 ization. The pa
180af 63 6b 69 6e 67 20 61 62 6f 76 65 20 6d 69 67 68 cking above migh
180b0 74 0a 20 20 2a 2a 20 62 65 20 73 6f 20 6f 75 74 t. ** be so out
180b1 20 6f 66 20 62 61 6c 61 6e 63 65 20 61 73 20 74 of balance as t
180b2 6f 20 62 65 20 69 6c 6c 65 67 61 6c 2e 20 20 46 o be illegal. F
180b3 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 20 or example, the
180b4 72 69 67 68 74 2d 6d 6f 73 74 0a 20 20 2a 2a 20 right-most. **
180b5 73 69 62 6c 69 6e 67 20 6d 69 67 68 74 20 62 65 sibling might be
180b6 20 63 6f 6d 70 6c 65 74 65 6c 79 20 65 6d 70 74 completely empt
180b7 79 2e 20 20 54 68 69 73 20 61 64 6a 75 73 74 6d y. This adjustm
180b8 65 6e 74 20 69 73 20 6e 6f 74 20 6f 70 74 69 6f ent is not optio
180b9 6e 61 6c 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 nal.. */. for(
180ba 69 3d 6b 2d 31 3b 20 69 3e 30 3b 20 69 2d 2d 29 i=k-1; i>0; i--)
180bb 7b 0a 20 20 20 20 69 6e 74 20 73 7a 52 69 67 68 {. int szRigh
180bc 74 20 3d 20 73 7a 4e 65 77 5b 69 5d 3b 20 20 2f t = szNew[i]; /
180bd 2a 20 53 69 7a 65 20 6f 66 20 73 69 62 6c 69 6e * Size of siblin
180be 67 20 6f 6e 20 74 68 65 20 72 69 67 68 74 20 2a g on the right *
180bf 2f 0a 20 20 20 20 69 6e 74 20 73 7a 4c 65 66 74 /. int szLeft
180c0 20 3d 20 73 7a 4e 65 77 5b 69 2d 31 5d 3b 20 2f = szNew[i-1]; /
180c1 2a 20 53 69 7a 65 20 6f 66 20 73 69 62 6c 69 6e * Size of siblin
180c2 67 20 6f 6e 20 74 68 65 20 6c 65 66 74 20 2a 2f g on the left */
180c3 0a 20 20 20 20 69 6e 74 20 72 3b 20 20 20 20 20 . int r;
180c4 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 /* Inde
180c5 78 20 6f 66 20 72 69 67 68 74 2d 6d 6f 73 74 20 x of right-most
180c6 63 65 6c 6c 20 69 6e 20 6c 65 66 74 20 73 69 62 cell in left sib
180c7 6c 69 6e 67 20 2a 2f 0a 20 20 20 20 69 6e 74 20 ling */. int
180c8 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d;
180c9 2f 2a 20 49 6e 64 65 78 20 6f 66 20 66 69 72 73 /* Index of firs
180ca 74 20 63 65 6c 6c 20 74 6f 20 74 68 65 20 6c 65 t cell to the le
180cb 66 74 20 6f 66 20 72 69 67 68 74 20 73 69 62 6c ft of right sibl
180cc 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 72 20 3d 20 ing */.. r =
180cd 63 6e 74 4e 65 77 5b 69 2d 31 5d 20 2d 20 31 3b cntNew[i-1] - 1;
180ce 0a 20 20 20 20 64 20 3d 20 72 20 2b 20 31 20 2d . d = r + 1 -
180cf 20 6c 65 61 66 44 61 74 61 3b 0a 20 20 20 20 61 leafData;. a
180d0 73 73 65 72 74 28 20 64 3c 6e 4d 61 78 43 65 6c ssert( d<nMaxCel
180d1 6c 73 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 ls );. assert
180d2 28 20 72 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b ( r<nMaxCells );
180d3 0a 20 20 20 20 77 68 69 6c 65 28 20 73 7a 52 69 . while( szRi
180d4 67 68 74 3d 3d 30 20 7c 7c 20 73 7a 52 69 67 68 ght==0 || szRigh
180d5 74 2b 73 7a 43 65 6c 6c 5b 64 5d 2b 32 3c 3d 73 t+szCell[d]+2<=s
180d6 7a 4c 65 66 74 2d 28 73 7a 43 65 6c 6c 5b 72 5d zLeft-(szCell[r]
180d7 2b 32 29 20 29 7b 0a 20 20 20 20 20 20 73 7a 52 +2) ){. szR
180d8 69 67 68 74 20 2b 3d 20 73 7a 43 65 6c 6c 5b 64 ight += szCell[d
180d9 5d 20 2b 20 32 3b 0a 20 20 20 20 20 20 73 7a 4c ] + 2;. szL
180da 65 66 74 20 2d 3d 20 73 7a 43 65 6c 6c 5b 72 5d eft -= szCell[r]
180db 20 2b 20 32 3b 0a 20 20 20 20 20 20 63 6e 74 4e + 2;. cntN
180dc 65 77 5b 69 2d 31 5d 2d 2d 3b 0a 20 20 20 20 20 ew[i-1]--;.
180dd 20 72 20 3d 20 63 6e 74 4e 65 77 5b 69 2d 31 5d r = cntNew[i-1]
180de 20 2d 20 31 3b 0a 20 20 20 20 20 20 64 20 3d 20 - 1;. d =
180df 72 20 2b 20 31 20 2d 20 6c 65 61 66 44 61 74 61 r + 1 - leafData
180e0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 7a 4e 65 ;. }. szNe
180e1 77 5b 69 5d 20 3d 20 73 7a 52 69 67 68 74 3b 0a w[i] = szRight;.
180e2 20 20 20 20 73 7a 4e 65 77 5b 69 2d 31 5d 20 3d szNew[i-1] =
180e3 20 73 7a 4c 65 66 74 3b 0a 20 20 7d 0a 0a 20 20 szLeft;. }..
180e4 2f 2a 20 45 69 74 68 65 72 20 77 65 20 66 6f 75 /* Either we fou
180e5 6e 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 63 nd one or more c
180e6 65 6c 6c 73 20 28 63 6e 74 6e 65 77 5b 30 5d 29 ells (cntnew[0])
180e7 3e 30 29 20 6f 72 20 70 50 61 67 65 20 69 73 0a >0) or pPage is.
180e8 20 20 2a 2a 20 61 20 76 69 72 74 75 61 6c 20 72 ** a virtual r
180e9 6f 6f 74 20 70 61 67 65 2e 20 20 41 20 76 69 72 oot page. A vir
180ea 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 20 69 tual root page i
180eb 73 20 77 68 65 6e 20 74 68 65 20 72 65 61 6c 20 s when the real
180ec 72 6f 6f 74 0a 20 20 2a 2a 20 70 61 67 65 20 69 root. ** page i
180ed 73 20 70 61 67 65 20 31 20 61 6e 64 20 77 65 20 s page 1 and we
180ee 61 72 65 20 74 68 65 20 6f 6e 6c 79 20 63 68 69 are the only chi
180ef 6c 64 20 6f 66 20 74 68 61 74 20 70 61 67 65 2e ld of that page.
180f0 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
180f1 63 6e 74 4e 65 77 5b 30 5d 3e 30 20 7c 7c 20 28 cntNew[0]>0 || (
180f2 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f 3d 3d 31 pParent->pgno==1
180f3 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 && pParent->nCe
180f4 6c 6c 3d 3d 30 29 20 29 3b 0a 0a 20 20 54 52 41 ll==0) );.. TRA
180f5 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 6f 6c CE(("BALANCE: ol
180f6 64 3a 20 25 64 20 25 64 20 25 64 20 20 22 2c 0a d: %d %d %d ",.
180f7 20 20 20 20 61 70 4f 6c 64 5b 30 5d 2d 3e 70 67 apOld[0]->pg
180f8 6e 6f 2c 20 0a 20 20 20 20 6e 4f 6c 64 3e 3d 32 no, . nOld>=2
180f9 20 3f 20 61 70 4f 6c 64 5b 31 5d 2d 3e 70 67 6e ? apOld[1]->pgn
180fa 6f 20 3a 20 30 2c 0a 20 20 20 20 6e 4f 6c 64 3e o : 0,. nOld>
180fb 3d 33 20 3f 20 61 70 4f 6c 64 5b 32 5d 2d 3e 70 =3 ? apOld[2]->p
180fc 67 6e 6f 20 3a 20 30 0a 20 20 29 29 3b 0a 0a 20 gno : 0. ));..
180fd 20 2f 2a 0a 20 20 2a 2a 20 41 6c 6c 6f 63 61 74 /*. ** Allocat
180fe 65 20 6b 20 6e 65 77 20 70 61 67 65 73 2e 20 20 e k new pages.
180ff 52 65 75 73 65 20 6f 6c 64 20 70 61 67 65 73 20 Reuse old pages
18100 77 68 65 72 65 20 70 6f 73 73 69 62 6c 65 2e 0a where possible..
18101 20 20 2a 2f 0a 20 20 69 66 28 20 61 70 4f 6c 64 */. if( apOld
18102 5b 30 5d 2d 3e 70 67 6e 6f 3c 3d 31 20 29 7b 0a [0]->pgno<=1 ){.
18103 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
18104 43 4f 52 52 55 50 54 3b 0a 20 20 20 20 67 6f 74 CORRUPT;. got
18105 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 o balance_cleanu
18106 70 3b 0a 20 20 7d 0a 20 20 70 61 67 65 46 6c 61 p;. }. pageFla
18107 67 73 20 3d 20 61 70 4f 6c 64 5b 30 5d 2d 3e 61 gs = apOld[0]->a
18108 44 61 74 61 5b 30 5d 3b 0a 20 20 66 6f 72 28 69 Data[0];. for(i
18109 3d 30 3b 20 69 3c 6b 3b 20 69 2b 2b 29 7b 0a 20 =0; i<k; i++){.
1810a 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 MemPage *pNew
1810b 3b 0a 20 20 20 20 69 66 28 20 69 3c 6e 4f 6c 64 ;. if( i<nOld
1810c 20 29 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d ){. pNew =
1810d 20 61 70 4e 65 77 5b 69 5d 20 3d 20 61 70 4f 6c apNew[i] = apOl
1810e 64 5b 69 5d 3b 0a 20 20 20 20 20 20 61 70 4f 6c d[i];. apOl
1810f 64 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 d[i] = 0;.
18110 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
18111 72 57 72 69 74 65 28 70 4e 65 77 2d 3e 70 44 62 rWrite(pNew->pDb
18112 50 61 67 65 29 3b 0a 20 20 20 20 20 20 6e 4e 65 Page);. nNe
18113 77 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 72 w++;. if( r
18114 63 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 c ) goto balance
18115 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d 65 _cleanup;. }e
18116 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 lse{. asser
18117 74 28 20 69 3e 30 20 29 3b 0a 20 20 20 20 20 20 t( i>0 );.
18118 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 rc = allocateBtr
18119 65 65 50 61 67 65 28 70 42 74 2c 20 26 70 4e 65 eePage(pBt, &pNe
1811a 77 2c 20 26 70 67 6e 6f 2c 20 70 67 6e 6f 2c 20 w, &pgno, pgno,
1811b 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 0);. if( rc
1811c 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f ) goto balance_
1811d 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 61 cleanup;. a
1811e 70 4e 65 77 5b 69 5d 20 3d 20 70 4e 65 77 3b 0a pNew[i] = pNew;.
1811f 20 20 20 20 20 20 6e 4e 65 77 2b 2b 3b 0a 0a 20 nNew++;..
18120 20 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 /* Set the
18121 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 pointer-map entr
18122 79 20 66 6f 72 20 74 68 65 20 6e 65 77 20 73 69 y for the new si
18123 62 6c 69 6e 67 20 70 61 67 65 2e 20 2a 2f 0a 20 bling page. */.
18124 20 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 if( ISAUTOV
18125 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 20 ACUUM ){.
18126 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 ptrmapPut(pBt,
18127 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20 50 54 52 4d pNew->pgno, PTRM
18128 41 50 5f 42 54 52 45 45 2c 20 70 50 61 72 65 6e AP_BTREE, pParen
18129 74 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 t->pgno, &rc);.
1812a 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 if( rc!=S
1812b 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1812c 20 20 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e goto balan
1812d 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 ce_cleanup;.
1812e 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
1812f 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 72 }. }.. /* Fr
18130 65 65 20 61 6e 79 20 6f 6c 64 20 70 61 67 65 73 ee any old pages
18131 20 74 68 61 74 20 77 65 72 65 20 6e 6f 74 20 72 that were not r
18132 65 75 73 65 64 20 61 73 20 6e 65 77 20 70 61 67 eused as new pag
18133 65 73 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 es.. */. while
18134 28 20 69 3c 6e 4f 6c 64 20 29 7b 0a 20 20 20 20 ( i<nOld ){.
18135 66 72 65 65 50 61 67 65 28 61 70 4f 6c 64 5b 69 freePage(apOld[i
18136 5d 2c 20 26 72 63 29 3b 0a 20 20 20 20 69 66 28 ], &rc);. if(
18137 20 72 63 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e rc ) goto balan
18138 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 ce_cleanup;.
18139 72 65 6c 65 61 73 65 50 61 67 65 28 61 70 4f 6c releasePage(apOl
1813a 64 5b 69 5d 29 3b 0a 20 20 20 20 61 70 4f 6c 64 d[i]);. apOld
1813b 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 69 2b 2b [i] = 0;. i++
1813c 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a ;. }.. /*. **
1813d 20 50 75 74 20 74 68 65 20 6e 65 77 20 70 61 67 Put the new pag
1813e 65 73 20 69 6e 20 61 63 63 65 6e 64 69 6e 67 20 es in accending
1813f 6f 72 64 65 72 2e 20 20 54 68 69 73 20 68 65 6c order. This hel
18140 70 73 20 74 6f 0a 20 20 2a 2a 20 6b 65 65 70 20 ps to. ** keep
18141 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 64 entries in the d
18142 69 73 6b 20 66 69 6c 65 20 69 6e 20 6f 72 64 65 isk file in orde
18143 72 20 73 6f 20 74 68 61 74 20 61 20 73 63 61 6e r so that a scan
18144 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 74 61 62 . ** of the tab
18145 6c 65 20 69 73 20 61 20 6c 69 6e 65 61 72 20 73 le is a linear s
18146 63 61 6e 20 74 68 72 6f 75 67 68 20 74 68 65 20 can through the
18147 66 69 6c 65 2e 20 20 54 68 61 74 0a 20 20 2a 2a file. That. **
18148 20 69 6e 20 74 75 72 6e 20 68 65 6c 70 73 20 74 in turn helps t
18149 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 he operating sys
1814a 74 65 6d 20 74 6f 20 64 65 6c 69 76 65 72 20 70 tem to deliver p
1814b 61 67 65 73 0a 20 20 2a 2a 20 66 72 6f 6d 20 74 ages. ** from t
1814c 68 65 20 64 69 73 6b 20 6d 6f 72 65 20 72 61 70 he disk more rap
1814d 69 64 6c 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 idly.. **. **
1814e 41 6e 20 4f 28 6e 5e 32 29 20 69 6e 73 65 72 74 An O(n^2) insert
1814f 69 6f 6e 20 73 6f 72 74 20 61 6c 67 6f 72 69 74 ion sort algorit
18150 68 6d 20 69 73 20 75 73 65 64 2c 20 62 75 74 20 hm is used, but
18151 73 69 6e 63 65 0a 20 20 2a 2a 20 6e 20 69 73 20 since. ** n is
18152 6e 65 76 65 72 20 6d 6f 72 65 20 74 68 61 6e 20 never more than
18153 4e 42 20 28 61 20 73 6d 61 6c 6c 20 63 6f 6e 73 NB (a small cons
18154 74 61 6e 74 29 2c 20 74 68 61 74 20 73 68 6f 75 tant), that shou
18155 6c 64 0a 20 20 2a 2a 20 6e 6f 74 20 62 65 20 61 ld. ** not be a
18156 20 70 72 6f 62 6c 65 6d 2e 0a 20 20 2a 2a 0a 20 problem.. **.
18157 20 2a 2a 20 57 68 65 6e 20 4e 42 3d 3d 33 2c 20 ** When NB==3,
18158 74 68 69 73 20 6f 6e 65 20 6f 70 74 69 6d 69 7a this one optimiz
18159 61 74 69 6f 6e 20 6d 61 6b 65 73 20 74 68 65 20 ation makes the
1815a 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 61 62 database. ** ab
1815b 6f 75 74 20 32 35 25 20 66 61 73 74 65 72 20 66 out 25% faster f
1815c 6f 72 20 6c 61 72 67 65 20 69 6e 73 65 72 74 69 or large inserti
1815d 6f 6e 73 20 61 6e 64 20 64 65 6c 65 74 69 6f 6e ons and deletion
1815e 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d s.. */. for(i=
1815f 30 3b 20 69 3c 6b 2d 31 3b 20 69 2b 2b 29 7b 0a 0; i<k-1; i++){.
18160 20 20 20 20 69 6e 74 20 6d 69 6e 56 20 3d 20 61 int minV = a
18161 70 4e 65 77 5b 69 5d 2d 3e 70 67 6e 6f 3b 0a 20 pNew[i]->pgno;.
18162 20 20 20 69 6e 74 20 6d 69 6e 49 20 3d 20 69 3b int minI = i;
18163 0a 20 20 20 20 66 6f 72 28 6a 3d 69 2b 31 3b 20 . for(j=i+1;
18164 6a 3c 6b 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 j<k; j++){.
18165 20 69 66 28 20 61 70 4e 65 77 5b 6a 5d 2d 3e 70 if( apNew[j]->p
18166 67 6e 6f 3c 28 75 6e 73 69 67 6e 65 64 29 6d 69 gno<(unsigned)mi
18167 6e 56 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 69 nV ){. mi
18168 6e 49 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 nI = j;.
18169 6d 69 6e 56 20 3d 20 61 70 4e 65 77 5b 6a 5d 2d minV = apNew[j]-
1816a 3e 70 67 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 >pgno;. }.
1816b 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 69 6e }. if( min
1816c 49 3e 69 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 I>i ){. int
1816d 20 74 3b 0a 20 20 20 20 20 20 4d 65 6d 50 61 67 t;. MemPag
1816e 65 20 2a 70 54 3b 0a 20 20 20 20 20 20 74 20 3d e *pT;. t =
1816f 20 61 70 4e 65 77 5b 69 5d 2d 3e 70 67 6e 6f 3b apNew[i]->pgno;
18170 0a 20 20 20 20 20 20 70 54 20 3d 20 61 70 4e 65 . pT = apNe
18171 77 5b 69 5d 3b 0a 20 20 20 20 20 20 61 70 4e 65 w[i];. apNe
18172 77 5b 69 5d 20 3d 20 61 70 4e 65 77 5b 6d 69 6e w[i] = apNew[min
18173 49 5d 3b 0a 20 20 20 20 20 20 61 70 4e 65 77 5b I];. apNew[
18174 6d 69 6e 49 5d 20 3d 20 70 54 3b 0a 20 20 20 20 minI] = pT;.
18175 7d 0a 20 20 7d 0a 20 20 54 52 41 43 45 28 28 22 }. }. TRACE(("
18176 6e 65 77 3a 20 25 64 28 25 64 29 20 25 64 28 25 new: %d(%d) %d(%
18177 64 29 20 25 64 28 25 64 29 20 25 64 28 25 64 29 d) %d(%d) %d(%d)
18178 20 25 64 28 25 64 29 5c 6e 22 2c 0a 20 20 20 20 %d(%d)\n",.
18179 61 70 4e 65 77 5b 30 5d 2d 3e 70 67 6e 6f 2c 20 apNew[0]->pgno,
1817a 73 7a 4e 65 77 5b 30 5d 2c 0a 20 20 20 20 6e 4e szNew[0],. nN
1817b 65 77 3e 3d 32 20 3f 20 61 70 4e 65 77 5b 31 5d ew>=2 ? apNew[1]
1817c 2d 3e 70 67 6e 6f 20 3a 20 30 2c 20 6e 4e 65 77 ->pgno : 0, nNew
1817d 3e 3d 32 20 3f 20 73 7a 4e 65 77 5b 31 5d 20 3a >=2 ? szNew[1] :
1817e 20 30 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d 33 20 0,. nNew>=3
1817f 3f 20 61 70 4e 65 77 5b 32 5d 2d 3e 70 67 6e 6f ? apNew[2]->pgno
18180 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d 33 20 3f 20 : 0, nNew>=3 ?
18181 73 7a 4e 65 77 5b 32 5d 20 3a 20 30 2c 0a 20 20 szNew[2] : 0,.
18182 20 20 6e 4e 65 77 3e 3d 34 20 3f 20 61 70 4e 65 nNew>=4 ? apNe
18183 77 5b 33 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c 20 w[3]->pgno : 0,
18184 6e 4e 65 77 3e 3d 34 20 3f 20 73 7a 4e 65 77 5b nNew>=4 ? szNew[
18185 33 5d 20 3a 20 30 2c 0a 20 20 20 20 6e 4e 65 77 3] : 0,. nNew
18186 3e 3d 35 20 3f 20 61 70 4e 65 77 5b 34 5d 2d 3e >=5 ? apNew[4]->
18187 70 67 6e 6f 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d pgno : 0, nNew>=
18188 35 20 3f 20 73 7a 4e 65 77 5b 34 5d 20 3a 20 30 5 ? szNew[4] : 0
18189 29 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 ));.. assert( s
1818a 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 qlite3PagerIswri
1818b 74 65 61 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e teable(pParent->
1818c 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 70 75 pDbPage) );. pu
1818d 74 34 62 79 74 65 28 70 52 69 67 68 74 2c 20 61 t4byte(pRight, a
1818e 70 4e 65 77 5b 6e 4e 65 77 2d 31 5d 2d 3e 70 67 pNew[nNew-1]->pg
1818f 6e 6f 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 no);.. /*. **
18190 45 76 65 6e 6c 79 20 64 69 73 74 72 69 62 75 74 Evenly distribut
18191 65 20 74 68 65 20 64 61 74 61 20 69 6e 20 61 70 e the data in ap
18192 43 65 6c 6c 5b 5d 20 61 63 72 6f 73 73 20 74 68 Cell[] across th
18193 65 20 6e 65 77 20 70 61 67 65 73 2e 0a 20 20 2a e new pages.. *
18194 2a 20 49 6e 73 65 72 74 20 64 69 76 69 64 65 72 * Insert divider
18195 20 63 65 6c 6c 73 20 69 6e 74 6f 20 70 50 61 72 cells into pPar
18196 65 6e 74 20 61 73 20 6e 65 63 65 73 73 61 72 79 ent as necessary
18197 2e 0a 20 20 2a 2f 0a 20 20 6a 20 3d 20 30 3b 0a .. */. j = 0;.
18198 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4e 65 for(i=0; i<nNe
18199 77 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 2f 2a 20 w; i++){. /*
1819a 41 73 73 65 6d 62 6c 65 20 74 68 65 20 6e 65 77 Assemble the new
1819b 20 73 69 62 6c 69 6e 67 20 70 61 67 65 2e 20 2a sibling page. *
1819c 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 /. MemPage *p
1819d 4e 65 77 20 3d 20 61 70 4e 65 77 5b 69 5d 3b 0a New = apNew[i];.
1819e 20 20 20 20 61 73 73 65 72 74 28 20 6a 3c 6e 4d assert( j<nM
1819f 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 7a axCells );. z
181a0 65 72 6f 50 61 67 65 28 70 4e 65 77 2c 20 70 61 eroPage(pNew, pa
181a1 67 65 46 6c 61 67 73 29 3b 0a 20 20 20 20 61 73 geFlags);. as
181a2 73 65 6d 62 6c 65 50 61 67 65 28 70 4e 65 77 2c semblePage(pNew,
181a3 20 63 6e 74 4e 65 77 5b 69 5d 2d 6a 2c 20 26 61 cntNew[i]-j, &a
181a4 70 43 65 6c 6c 5b 6a 5d 2c 20 26 73 7a 43 65 6c pCell[j], &szCel
181a5 6c 5b 6a 5d 29 3b 0a 20 20 20 20 61 73 73 65 72 l[j]);. asser
181a6 74 28 20 70 4e 65 77 2d 3e 6e 43 65 6c 6c 3e 30 t( pNew->nCell>0
181a7 20 7c 7c 20 28 6e 4e 65 77 3d 3d 31 20 26 26 20 || (nNew==1 &&
181a8 63 6e 74 4e 65 77 5b 30 5d 3d 3d 30 29 20 29 3b cntNew[0]==0) );
181a9 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 65 . assert( pNe
181aa 77 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 w->nOverflow==0
181ab 29 3b 0a 0a 20 20 20 20 6a 20 3d 20 63 6e 74 4e );.. j = cntN
181ac 65 77 5b 69 5d 3b 0a 0a 20 20 20 20 2f 2a 20 49 ew[i];.. /* I
181ad 66 20 74 68 65 20 73 69 62 6c 69 6e 67 20 70 61 f the sibling pa
181ae 67 65 20 61 73 73 65 6d 62 6c 65 64 20 61 62 6f ge assembled abo
181af 76 65 20 77 61 73 20 6e 6f 74 20 74 68 65 20 72 ve was not the r
181b0 69 67 68 74 2d 6d 6f 73 74 20 73 69 62 6c 69 6e ight-most siblin
181b1 67 2c 0a 20 20 20 20 2a 2a 20 69 6e 73 65 72 74 g,. ** insert
181b2 20 61 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 a divider cell
181b3 69 6e 74 6f 20 74 68 65 20 70 61 72 65 6e 74 20 into the parent
181b4 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 page.. */.
181b5 20 61 73 73 65 72 74 28 20 69 3c 6e 4e 65 77 2d assert( i<nNew-
181b6 31 20 7c 7c 20 6a 3d 3d 6e 43 65 6c 6c 20 29 3b 1 || j==nCell );
181b7 0a 20 20 20 20 69 66 28 20 6a 3c 6e 43 65 6c 6c . if( j<nCell
181b8 20 29 7b 0a 20 20 20 20 20 20 75 38 20 2a 70 43 ){. u8 *pC
181b9 65 6c 6c 3b 0a 20 20 20 20 20 20 75 38 20 2a 70 ell;. u8 *p
181ba 54 65 6d 70 3b 0a 20 20 20 20 20 20 69 6e 74 20 Temp;. int
181bb 73 7a 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 sz;.. asser
181bc 74 28 20 6a 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 t( j<nMaxCells )
181bd 3b 0a 20 20 20 20 20 20 70 43 65 6c 6c 20 3d 20 ;. pCell =
181be 61 70 43 65 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 20 apCell[j];.
181bf 20 73 7a 20 3d 20 73 7a 43 65 6c 6c 5b 6a 5d 20 sz = szCell[j]
181c0 2b 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e + leafCorrection
181c1 3b 0a 20 20 20 20 20 20 70 54 65 6d 70 20 3d 20 ;. pTemp =
181c2 26 61 4f 76 66 6c 53 70 61 63 65 5b 69 4f 76 66 &aOvflSpace[iOvf
181c3 6c 53 70 61 63 65 5d 3b 0a 20 20 20 20 20 20 69 lSpace];. i
181c4 66 28 20 21 70 4e 65 77 2d 3e 6c 65 61 66 20 29 f( !pNew->leaf )
181c5 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 {. memcpy
181c6 28 26 70 4e 65 77 2d 3e 61 44 61 74 61 5b 38 5d (&pNew->aData[8]
181c7 2c 20 70 43 65 6c 6c 2c 20 34 29 3b 0a 20 20 20 , pCell, 4);.
181c8 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 65 61 }else if( lea
181c9 66 44 61 74 61 20 29 7b 0a 20 20 20 20 20 20 20 fData ){.
181ca 20 2f 2a 20 49 66 20 74 68 65 20 74 72 65 65 20 /* If the tree
181cb 69 73 20 61 20 6c 65 61 66 2d 64 61 74 61 20 74 is a leaf-data t
181cc 72 65 65 2c 20 61 6e 64 20 74 68 65 20 73 69 62 ree, and the sib
181cd 6c 69 6e 67 73 20 61 72 65 20 6c 65 61 76 65 73 lings are leaves
181ce 2c 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 , . ** th
181cf 65 6e 20 74 68 65 72 65 20 69 73 20 6e 6f 20 64 en there is no d
181d0 69 76 69 64 65 72 20 63 65 6c 6c 20 69 6e 20 61 ivider cell in a
181d1 70 43 65 6c 6c 5b 5d 2e 20 49 6e 73 74 65 61 64 pCell[]. Instead
181d2 2c 20 74 68 65 20 64 69 76 69 64 65 72 20 0a 20 , the divider .
181d3 20 20 20 20 20 20 20 2a 2a 20 63 65 6c 6c 20 63 ** cell c
181d4 6f 6e 73 69 73 74 73 20 6f 66 20 74 68 65 20 69 onsists of the i
181d5 6e 74 65 67 65 72 20 6b 65 79 20 66 6f 72 20 74 nteger key for t
181d6 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 63 65 he right-most ce
181d7 6c 6c 20 6f 66 20 0a 20 20 20 20 20 20 20 20 2a ll of . *
181d8 2a 20 74 68 65 20 73 69 62 6c 69 6e 67 2d 70 61 * the sibling-pa
181d9 67 65 20 61 73 73 65 6d 62 6c 65 64 20 61 62 6f ge assembled abo
181da 76 65 20 6f 6e 6c 79 2e 0a 20 20 20 20 20 20 20 ve only..
181db 20 2a 2f 0a 20 20 20 20 20 20 20 20 43 65 6c 6c */. Cell
181dc 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 20 20 Info info;.
181dd 20 20 20 6a 2d 2d 3b 0a 20 20 20 20 20 20 20 20 j--;.
181de 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 btreeParseCellPt
181df 72 28 70 4e 65 77 2c 20 61 70 43 65 6c 6c 5b 6a r(pNew, apCell[j
181e0 5d 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 20 ], &info);.
181e1 20 20 20 70 43 65 6c 6c 20 3d 20 70 54 65 6d 70 pCell = pTemp
181e2 3b 0a 20 20 20 20 20 20 20 20 73 7a 20 3d 20 34 ;. sz = 4
181e3 20 2b 20 70 75 74 56 61 72 69 6e 74 28 26 70 43 + putVarint(&pC
181e4 65 6c 6c 5b 34 5d 2c 20 69 6e 66 6f 2e 6e 4b 65 ell[4], info.nKe
181e5 79 29 3b 0a 20 20 20 20 20 20 20 20 70 54 65 6d y);. pTem
181e6 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c p = 0;. }el
181e7 73 65 7b 0a 20 20 20 20 20 20 20 20 70 43 65 6c se{. pCel
181e8 6c 20 2d 3d 20 34 3b 0a 20 20 20 20 20 20 20 20 l -= 4;.
181e9 2f 2a 20 4f 62 73 63 75 72 65 20 63 61 73 65 20 /* Obscure case
181ea 66 6f 72 20 6e 6f 6e 2d 6c 65 61 66 2d 64 61 74 for non-leaf-dat
181eb 61 20 74 72 65 65 73 3a 20 49 66 20 74 68 65 20 a trees: If the
181ec 63 65 6c 6c 20 61 74 20 70 43 65 6c 6c 20 77 61 cell at pCell wa
181ed 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 72 65 s. ** pre
181ee 76 69 6f 75 73 6c 79 20 73 74 6f 72 65 64 20 6f viously stored o
181ef 6e 20 61 20 6c 65 61 66 20 6e 6f 64 65 2c 20 61 n a leaf node, a
181f0 6e 64 20 69 74 73 20 72 65 70 6f 72 74 65 64 20 nd its reported
181f1 73 69 7a 65 20 77 61 73 20 34 0a 20 20 20 20 20 size was 4.
181f2 20 20 20 2a 2a 20 62 79 74 65 73 2c 20 74 68 65 ** bytes, the
181f3 6e 20 69 74 20 6d 61 79 20 61 63 74 75 61 6c 6c n it may actuall
181f4 79 20 62 65 20 73 6d 61 6c 6c 65 72 20 74 68 61 y be smaller tha
181f5 6e 20 74 68 69 73 20 0a 20 20 20 20 20 20 20 20 n this .
181f6 2a 2a 20 28 73 65 65 20 62 74 72 65 65 50 61 72 ** (see btreePar
181f7 73 65 43 65 6c 6c 50 74 72 28 29 2c 20 34 20 62 seCellPtr(), 4 b
181f8 79 74 65 73 20 69 73 20 74 68 65 20 6d 69 6e 69 ytes is the mini
181f9 6d 75 6d 20 73 69 7a 65 20 6f 66 0a 20 20 20 20 mum size of.
181fa 20 20 20 20 2a 2a 20 61 6e 79 20 63 65 6c 6c 29 ** any cell)
181fb 2e 20 42 75 74 20 69 74 20 69 73 20 69 6d 70 6f . But it is impo
181fc 72 74 61 6e 74 20 74 6f 20 70 61 73 73 20 74 68 rtant to pass th
181fd 65 20 63 6f 72 72 65 63 74 20 73 69 7a 65 20 74 e correct size t
181fe 6f 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e o . ** in
181ff 73 65 72 74 43 65 6c 6c 28 29 2c 20 73 6f 20 72 sertCell(), so r
18200 65 70 61 72 73 65 20 74 68 65 20 63 65 6c 6c 20 eparse the cell
18201 6e 6f 77 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a now.. **.
18202 20 20 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 ** Note
18203 74 68 61 74 20 74 68 69 73 20 63 61 6e 20 6e 65 that this can ne
18204 76 65 72 20 68 61 70 70 65 6e 20 69 6e 20 61 6e ver happen in an
18205 20 53 51 4c 69 74 65 20 64 61 74 61 20 66 69 6c SQLite data fil
18206 65 2c 20 61 73 20 61 6c 6c 0a 20 20 20 20 20 20 e, as all.
18207 20 20 2a 2a 20 63 65 6c 6c 73 20 61 72 65 20 61 ** cells are a
18208 74 20 6c 65 61 73 74 20 34 20 62 79 74 65 73 2e t least 4 bytes.
18209 20 49 74 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73 It only happens
1820a 20 69 6e 20 62 2d 74 72 65 65 73 20 75 73 65 64 in b-trees used
1820b 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 65 . ** to e
1820c 76 61 6c 75 61 74 65 20 22 49 4e 20 28 53 45 4c valuate "IN (SEL
1820d 45 43 54 20 2e 2e 2e 29 22 20 61 6e 64 20 73 69 ECT ...)" and si
1820e 6d 69 6c 61 72 20 63 6c 61 75 73 65 73 2e 0a 20 milar clauses..
1820f 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
18210 20 20 69 66 28 20 73 7a 43 65 6c 6c 5b 6a 5d 3d if( szCell[j]=
18211 3d 34 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =4 ){.
18212 61 73 73 65 72 74 28 6c 65 61 66 43 6f 72 72 65 assert(leafCorre
18213 63 74 69 6f 6e 3d 3d 34 29 3b 0a 20 20 20 20 20 ction==4);.
18214 20 20 20 20 20 73 7a 20 3d 20 63 65 6c 6c 53 69 sz = cellSi
18215 7a 65 50 74 72 28 70 50 61 72 65 6e 74 2c 20 70 zePtr(pParent, p
18216 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20 7d Cell);. }
18217 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
18218 4f 76 66 6c 53 70 61 63 65 20 2b 3d 20 73 7a 3b OvflSpace += sz;
18219 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 . assert( s
1821a 7a 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 z<=pBt->pageSize
1821b 2f 34 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 /4 );. asse
1821c 72 74 28 20 69 4f 76 66 6c 53 70 61 63 65 3c 3d rt( iOvflSpace<=
1821d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 29 3b pBt->pageSize );
1821e 0a 20 20 20 20 20 20 69 6e 73 65 72 74 43 65 6c . insertCel
1821f 6c 28 70 50 61 72 65 6e 74 2c 20 6e 78 44 69 76 l(pParent, nxDiv
18220 2c 20 70 43 65 6c 6c 2c 20 73 7a 2c 20 70 54 65 , pCell, sz, pTe
18221 6d 70 2c 20 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20 mp, pNew->pgno,
18222 26 72 63 29 3b 0a 20 20 20 20 20 20 69 66 28 20 &rc);. if(
18223 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 rc!=SQLITE_OK )
18224 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 goto balance_cle
18225 61 6e 75 70 3b 0a 20 20 20 20 20 20 61 73 73 65 anup;. asse
18226 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 rt( sqlite3Pager
18227 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 72 Iswriteable(pPar
18228 65 6e 74 2d 3e 70 44 62 50 61 67 65 29 20 29 3b ent->pDbPage) );
18229 0a 0a 20 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 .. j++;.
1822a 20 20 20 6e 78 44 69 76 2b 2b 3b 0a 20 20 20 20 nxDiv++;.
1822b 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 }. }. assert(
1822c 6a 3d 3d 6e 43 65 6c 6c 20 29 3b 0a 20 20 61 73 j==nCell );. as
1822d 73 65 72 74 28 20 6e 4f 6c 64 3e 30 20 29 3b 0a sert( nOld>0 );.
1822e 20 20 61 73 73 65 72 74 28 20 6e 4e 65 77 3e 30 assert( nNew>0
1822f 20 29 3b 0a 20 20 69 66 28 20 28 70 61 67 65 46 );. if( (pageF
18230 6c 61 67 73 20 26 20 50 54 46 5f 4c 45 41 46 29 lags & PTF_LEAF)
18231 3d 3d 30 20 29 7b 0a 20 20 20 20 75 38 20 2a 7a ==0 ){. u8 *z
18232 43 68 69 6c 64 20 3d 20 26 61 70 43 6f 70 79 5b Child = &apCopy[
18233 6e 4f 6c 64 2d 31 5d 2d 3e 61 44 61 74 61 5b 38 nOld-1]->aData[8
18234 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 61 ];. memcpy(&a
18235 70 4e 65 77 5b 6e 4e 65 77 2d 31 5d 2d 3e 61 44 pNew[nNew-1]->aD
18236 61 74 61 5b 38 5d 2c 20 7a 43 68 69 6c 64 2c 20 ata[8], zChild,
18237 34 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69 4);. }.. if( i
18238 73 52 6f 6f 74 20 26 26 20 70 50 61 72 65 6e 74 sRoot && pParent
18239 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 26 26 20 70 50 ->nCell==0 && pP
1823a 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 arent->hdrOffset
1823b 3c 3d 61 70 4e 65 77 5b 30 5d 2d 3e 6e 46 72 65 <=apNew[0]->nFre
1823c 65 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 e ){. /* The
1823d 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 root page of the
1823e 20 62 2d 74 72 65 65 20 6e 6f 77 20 63 6f 6e 74 b-tree now cont
1823f 61 69 6e 73 20 6e 6f 20 63 65 6c 6c 73 2e 20 54 ains no cells. T
18240 68 65 20 6f 6e 6c 79 20 73 69 62 6c 69 6e 67 0a he only sibling.
18241 20 20 20 20 2a 2a 20 70 61 67 65 20 69 73 20 74 ** page is t
18242 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 6f he right-child o
18243 66 20 74 68 65 20 70 61 72 65 6e 74 2e 20 43 6f f the parent. Co
18244 70 79 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 py the contents
18245 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 68 of the. ** ch
18246 69 6c 64 20 70 61 67 65 20 69 6e 74 6f 20 74 68 ild page into th
18247 65 20 70 61 72 65 6e 74 2c 20 64 65 63 72 65 61 e parent, decrea
18248 73 69 6e 67 20 74 68 65 20 6f 76 65 72 61 6c 6c sing the overall
18249 20 68 65 69 67 68 74 20 6f 66 20 74 68 65 0a 20 height of the.
1824a 20 20 20 2a 2a 20 62 2d 74 72 65 65 20 73 74 72 ** b-tree str
1824b 75 63 74 75 72 65 20 62 79 20 6f 6e 65 2e 20 54 ucture by one. T
1824c 68 69 73 20 69 73 20 64 65 73 63 72 69 62 65 64 his is described
1824d 20 61 73 20 74 68 65 20 22 62 61 6c 61 6e 63 65 as the "balance
1824e 2d 73 68 61 6c 6c 6f 77 65 72 22 0a 20 20 20 20 -shallower".
1824f 2a 2a 20 73 75 62 2d 61 6c 67 6f 72 69 74 68 6d ** sub-algorithm
18250 20 69 6e 20 73 6f 6d 65 20 64 6f 63 75 6d 65 6e in some documen
18251 74 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2a 0a 20 tation.. **.
18252 20 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69 73 ** If this is
18253 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 an auto-vacuum
18254 64 61 74 61 62 61 73 65 2c 20 74 68 65 20 63 61 database, the ca
18255 6c 6c 20 74 6f 20 63 6f 70 79 4e 6f 64 65 43 6f ll to copyNodeCo
18256 6e 74 65 6e 74 28 29 20 0a 20 20 20 20 2a 2a 20 ntent() . **
18257 73 65 74 73 20 61 6c 6c 20 70 6f 69 6e 74 65 72 sets all pointer
18258 2d 6d 61 70 20 65 6e 74 72 69 65 73 20 63 6f 72 -map entries cor
18259 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 64 61 responding to da
1825a 74 61 62 61 73 65 20 69 6d 61 67 65 20 70 61 67 tabase image pag
1825b 65 73 20 0a 20 20 20 20 2a 2a 20 66 6f 72 20 77 es . ** for w
1825c 68 69 63 68 20 74 68 65 20 70 6f 69 6e 74 65 72 hich the pointer
1825d 20 69 73 20 73 74 6f 72 65 64 20 77 69 74 68 69 is stored withi
1825e 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 62 65 n the content be
1825f 69 6e 67 20 63 6f 70 69 65 64 2e 0a 20 20 20 20 ing copied..
18260 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 65 **. ** The se
18261 63 6f 6e 64 20 61 73 73 65 72 74 20 62 65 6c 6f cond assert belo
18262 77 20 76 65 72 69 66 69 65 73 20 74 68 61 74 20 w verifies that
18263 74 68 65 20 63 68 69 6c 64 20 70 61 67 65 20 69 the child page i
18264 73 20 64 65 66 72 61 67 6d 65 6e 74 65 64 0a 20 s defragmented.
18265 20 20 20 2a 2a 20 28 69 74 20 6d 75 73 74 20 62 ** (it must b
18266 65 2c 20 61 73 20 69 74 20 77 61 73 20 6a 75 73 e, as it was jus
18267 74 20 72 65 63 6f 6e 73 74 72 75 63 74 65 64 20 t reconstructed
18268 75 73 69 6e 67 20 61 73 73 65 6d 62 6c 65 50 61 using assemblePa
18269 67 65 28 29 29 2e 20 54 68 69 73 0a 20 20 20 20 ge()). This.
1826a 2a 2a 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 ** is important
1826b 69 66 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 if the parent pa
1826c 67 65 20 68 61 70 70 65 6e 73 20 74 6f 20 62 65 ge happens to be
1826d 20 70 61 67 65 20 31 20 6f 66 20 74 68 65 20 64 page 1 of the d
1826e 61 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 69 atabase. ** i
1826f 6d 61 67 65 2e 20 20 2a 2f 0a 20 20 20 20 61 73 mage. */. as
18270 73 65 72 74 28 20 6e 4e 65 77 3d 3d 31 20 29 3b sert( nNew==1 );
18271 0a 20 20 20 20 61 73 73 65 72 74 28 20 61 70 4e . assert( apN
18272 65 77 5b 30 5d 2d 3e 6e 46 72 65 65 20 3d 3d 20 ew[0]->nFree ==
18273 0a 20 20 20 20 20 20 20 20 28 67 65 74 32 62 79 . (get2by
18274 74 65 28 26 61 70 4e 65 77 5b 30 5d 2d 3e 61 44 te(&apNew[0]->aD
18275 61 74 61 5b 35 5d 29 2d 61 70 4e 65 77 5b 30 5d ata[5])-apNew[0]
18276 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 2d 61 70 4e ->cellOffset-apN
18277 65 77 5b 30 5d 2d 3e 6e 43 65 6c 6c 2a 32 29 20 ew[0]->nCell*2)
18278 0a 20 20 20 20 29 3b 0a 20 20 20 20 63 6f 70 79 . );. copy
18279 4e 6f 64 65 43 6f 6e 74 65 6e 74 28 61 70 4e 65 NodeContent(apNe
1827a 77 5b 30 5d 2c 20 70 50 61 72 65 6e 74 2c 20 26 w[0], pParent, &
1827b 72 63 29 3b 0a 20 20 20 20 66 72 65 65 50 61 67 rc);. freePag
1827c 65 28 61 70 4e 65 77 5b 30 5d 2c 20 26 72 63 29 e(apNew[0], &rc)
1827d 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 49 53 ;. }else if( IS
1827e 41 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 AUTOVACUUM ){.
1827f 20 20 2f 2a 20 46 69 78 20 74 68 65 20 70 6f 69 /* Fix the poi
18280 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 nter-map entries
18281 20 66 6f 72 20 61 6c 6c 20 74 68 65 20 63 65 6c for all the cel
18282 6c 73 20 74 68 61 74 20 77 65 72 65 20 73 68 69 ls that were shi
18283 66 74 65 64 20 61 72 6f 75 6e 64 2e 20 0a 20 20 fted around. .
18284 20 20 2a 2a 20 54 68 65 72 65 20 61 72 65 20 73 ** There are s
18285 65 76 65 72 61 6c 20 64 69 66 66 65 72 65 6e 74 everal different
18286 20 74 79 70 65 73 20 6f 66 20 70 6f 69 6e 74 65 types of pointe
18287 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 20 74 68 r-map entries th
18288 61 74 20 6e 65 65 64 20 74 6f 0a 20 20 20 20 2a at need to. *
18289 2a 20 62 65 20 64 65 61 6c 74 20 77 69 74 68 20 * be dealt with
1828a 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e by this routine.
1828b 20 53 6f 6d 65 20 6f 66 20 74 68 65 73 65 20 68 Some of these h
1828c 61 76 65 20 62 65 65 6e 20 73 65 74 20 61 6c 72 ave been set alr
1828d 65 61 64 79 2c 20 62 75 74 0a 20 20 20 20 2a 2a eady, but. **
1828e 20 6d 61 6e 79 20 68 61 76 65 20 6e 6f 74 2e 20 many have not.
1828f 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 The following is
18290 20 61 20 73 75 6d 6d 61 72 79 3a 0a 20 20 20 20 a summary:.
18291 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 31 29 20 54 **. ** 1) T
18292 68 65 20 65 6e 74 72 69 65 73 20 61 73 73 6f 63 he entries assoc
18293 69 61 74 65 64 20 77 69 74 68 20 6e 65 77 20 73 iated with new s
18294 69 62 6c 69 6e 67 20 70 61 67 65 73 20 74 68 61 ibling pages tha
18295 74 20 77 65 72 65 20 6e 6f 74 0a 20 20 20 20 2a t were not. *
18296 2a 20 20 20 20 20 20 73 69 62 6c 69 6e 67 73 20 * siblings
18297 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 when this functi
18298 6f 6e 20 77 61 73 20 63 61 6c 6c 65 64 2e 20 54 on was called. T
18299 68 65 73 65 20 68 61 76 65 20 61 6c 72 65 61 64 hese have alread
1829a 79 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 62 65 y. ** be
1829b 65 6e 20 73 65 74 2e 20 57 65 20 64 6f 6e 27 74 en set. We don't
1829c 20 6e 65 65 64 20 74 6f 20 77 6f 72 72 79 20 61 need to worry a
1829d 62 6f 75 74 20 6f 6c 64 20 73 69 62 6c 69 6e 67 bout old sibling
1829e 73 20 74 68 61 74 20 77 65 72 65 0a 20 20 20 20 s that were.
1829f 2a 2a 20 20 20 20 20 20 6d 6f 76 65 64 20 74 6f ** moved to
182a0 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 2d the free-list -
182a1 20 74 68 65 20 66 72 65 65 50 61 67 65 28 29 20 the freePage()
182a2 63 6f 64 65 20 68 61 73 20 74 61 6b 65 6e 20 63 code has taken c
182a3 61 72 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 are. **
182a4 6f 66 20 74 68 6f 73 65 2e 0a 20 20 20 20 2a 2a of those.. **
182a5 0a 20 20 20 20 2a 2a 20 20 20 32 29 20 54 68 65 . ** 2) The
182a6 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 pointer-map ent
182a7 72 69 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 ries associated
182a8 77 69 74 68 20 74 68 65 20 66 69 72 73 74 20 6f with the first o
182a9 76 65 72 66 6c 6f 77 0a 20 20 20 20 2a 2a 20 20 verflow. **
182aa 20 20 20 20 70 61 67 65 20 69 6e 20 61 6e 79 20 page in any
182ab 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 73 20 overflow chains
182ac 75 73 65 64 20 62 79 20 6e 65 77 20 64 69 76 69 used by new divi
182ad 64 65 72 20 63 65 6c 6c 73 2e 20 54 68 65 73 65 der cells. These
182ae 20 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 68 61 . ** ha
182af 76 65 20 61 6c 73 6f 20 61 6c 72 65 61 64 79 20 ve also already
182b0 62 65 65 6e 20 74 61 6b 65 6e 20 63 61 72 65 20 been taken care
182b1 6f 66 20 62 79 20 74 68 65 20 69 6e 73 65 72 74 of by the insert
182b2 43 65 6c 6c 28 29 20 63 6f 64 65 2e 0a 20 20 20 Cell() code..
182b3 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 33 29 20 **. ** 3)
182b4 49 66 20 74 68 65 20 73 69 62 6c 69 6e 67 20 70 If the sibling p
182b5 61 67 65 73 20 61 72 65 20 6e 6f 74 20 6c 65 61 ages are not lea
182b6 76 65 73 2c 20 74 68 65 6e 20 74 68 65 20 63 68 ves, then the ch
182b7 69 6c 64 20 70 61 67 65 73 20 6f 66 0a 20 20 20 ild pages of.
182b8 20 2a 2a 20 20 20 20 20 20 63 65 6c 6c 73 20 73 ** cells s
182b9 74 6f 72 65 64 20 6f 6e 20 74 68 65 20 73 69 62 tored on the sib
182ba 6c 69 6e 67 20 70 61 67 65 73 20 6d 61 79 20 6e ling pages may n
182bb 65 65 64 20 74 6f 20 62 65 20 75 70 64 61 74 65 eed to be update
182bc 64 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a d.. **. **
182bd 20 20 20 34 29 20 49 66 20 74 68 65 20 73 69 62 4) If the sib
182be 6c 69 6e 67 20 70 61 67 65 73 20 61 72 65 20 6e ling pages are n
182bf 6f 74 20 69 6e 74 65 72 6e 61 6c 20 69 6e 74 6b ot internal intk
182c0 65 79 20 6e 6f 64 65 73 2c 20 74 68 65 6e 20 61 ey nodes, then a
182c1 6e 79 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 6f ny. ** o
182c2 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 75 73 verflow pages us
182c3 65 64 20 62 79 20 74 68 65 73 65 20 63 65 6c 6c ed by these cell
182c4 73 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 62 65 s may need to be
182c5 20 75 70 64 61 74 65 64 0a 20 20 20 20 2a 2a 20 updated. **
182c6 20 20 20 20 20 28 69 6e 74 65 72 6e 61 6c 20 69 (internal i
182c7 6e 74 6b 65 79 20 6e 6f 64 65 73 20 6e 65 76 65 ntkey nodes neve
182c8 72 20 63 6f 6e 74 61 69 6e 20 70 6f 69 6e 74 65 r contain pointe
182c9 72 73 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 rs to overflow p
182ca 61 67 65 73 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 ages).. **.
182cb 20 20 2a 2a 20 20 20 35 29 20 49 66 20 74 68 65 ** 5) If the
182cc 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 61 sibling pages a
182cd 72 65 20 6e 6f 74 20 6c 65 61 76 65 73 2c 20 74 re not leaves, t
182ce 68 65 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 2d hen the pointer-
182cf 6d 61 70 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 map. **
182d0 65 6e 74 72 69 65 73 20 66 6f 72 20 74 68 65 20 entries for the
182d1 72 69 67 68 74 2d 63 68 69 6c 64 20 70 61 67 65 right-child page
182d2 73 20 6f 66 20 65 61 63 68 20 73 69 62 6c 69 6e s of each siblin
182d3 67 20 6d 61 79 20 6e 65 65 64 0a 20 20 20 20 2a g may need. *
182d4 2a 20 20 20 20 20 20 74 6f 20 62 65 20 75 70 64 * to be upd
182d5 61 74 65 64 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 ated.. **.
182d6 20 2a 2a 20 43 61 73 65 73 20 31 20 61 6e 64 20 ** Cases 1 and
182d7 32 20 61 72 65 20 64 65 61 6c 74 20 77 69 74 68 2 are dealt with
182d8 20 61 62 6f 76 65 20 62 79 20 6f 74 68 65 72 20 above by other
182d9 63 6f 64 65 2e 20 54 68 65 20 6e 65 78 74 0a 20 code. The next.
182da 20 20 20 2a 2a 20 62 6c 6f 63 6b 20 64 65 61 6c ** block deal
182db 73 20 77 69 74 68 20 63 61 73 65 73 20 33 20 61 s with cases 3 a
182dc 6e 64 20 34 20 61 6e 64 20 74 68 65 20 6f 6e 65 nd 4 and the one
182dd 20 61 66 74 65 72 20 74 68 61 74 2c 20 63 61 73 after that, cas
182de 65 20 35 2e 20 53 69 6e 63 65 0a 20 20 20 20 2a e 5. Since. *
182df 2a 20 73 65 74 74 69 6e 67 20 61 20 70 6f 69 6e * setting a poin
182e0 74 65 72 20 6d 61 70 20 65 6e 74 72 79 20 69 73 ter map entry is
182e1 20 61 20 72 65 6c 61 74 69 76 65 6c 79 20 65 78 a relatively ex
182e2 70 65 6e 73 69 76 65 20 6f 70 65 72 61 74 69 6f pensive operatio
182e3 6e 2c 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 63 n, this. ** c
182e4 6f 64 65 20 6f 6e 6c 79 20 73 65 74 73 20 70 6f ode only sets po
182e5 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 69 65 inter map entrie
182e6 73 20 66 6f 72 20 63 68 69 6c 64 20 6f 72 20 6f s for child or o
182e7 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 74 68 verflow pages th
182e8 61 74 20 68 61 76 65 0a 20 20 20 20 2a 2a 20 61 at have. ** a
182e9 63 74 75 61 6c 6c 79 20 6d 6f 76 65 64 20 62 65 ctually moved be
182ea 74 77 65 65 6e 20 70 61 67 65 73 2e 20 20 2a 2f tween pages. */
182eb 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e . MemPage *pN
182ec 65 77 20 3d 20 61 70 4e 65 77 5b 30 5d 3b 0a 20 ew = apNew[0];.
182ed 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4f 6c 64 MemPage *pOld
182ee 20 3d 20 61 70 43 6f 70 79 5b 30 5d 3b 0a 20 20 = apCopy[0];.
182ef 20 20 69 6e 74 20 6e 4f 76 65 72 66 6c 6f 77 20 int nOverflow
182f0 3d 20 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f = pOld->nOverflo
182f1 77 3b 0a 20 20 20 20 69 6e 74 20 69 4e 65 78 74 w;. int iNext
182f2 4f 6c 64 20 3d 20 70 4f 6c 64 2d 3e 6e 43 65 6c Old = pOld->nCel
182f3 6c 20 2b 20 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 l + nOverflow;.
182f4 20 20 20 69 6e 74 20 69 4f 76 65 72 66 6c 6f 77 int iOverflow
182f5 20 3d 20 28 6e 4f 76 65 72 66 6c 6f 77 20 3f 20 = (nOverflow ?
182f6 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 pOld->aOvfl[0].i
182f7 64 78 20 3a 20 2d 31 29 3b 0a 20 20 20 20 6a 20 dx : -1);. j
182f8 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 = 0;
182f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
182fa 20 2f 2a 20 43 75 72 72 65 6e 74 20 27 6f 6c 64 /* Current 'old
182fb 27 20 73 69 62 6c 69 6e 67 20 70 61 67 65 20 2a ' sibling page *
182fc 2f 0a 20 20 20 20 6b 20 3d 20 30 3b 20 20 20 20 /. k = 0;
182fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
182fe 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 /* Curr
182ff 65 6e 74 20 27 6e 65 77 27 20 73 69 62 6c 69 6e ent 'new' siblin
18300 67 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 66 6f g page */. fo
18301 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 r(i=0; i<nCell;
18302 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 i++){. int
18303 69 73 44 69 76 69 64 65 72 20 3d 20 30 3b 0a 20 isDivider = 0;.
18304 20 20 20 20 20 77 68 69 6c 65 28 20 69 3d 3d 69 while( i==i
18305 4e 65 78 74 4f 6c 64 20 29 7b 0a 20 20 20 20 20 NextOld ){.
18306 20 20 20 2f 2a 20 43 65 6c 6c 20 69 20 69 73 20 /* Cell i is
18307 74 68 65 20 63 65 6c 6c 20 69 6d 6d 65 64 69 61 the cell immedia
18308 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 tely following t
18309 68 65 20 6c 61 73 74 20 63 65 6c 6c 20 6f 6e 20 he last cell on
1830a 6f 6c 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 old. ** s
1830b 69 62 6c 69 6e 67 20 70 61 67 65 20 6a 2e 20 49 ibling page j. I
1830c 66 20 74 68 65 20 73 69 62 6c 69 6e 67 73 20 61 f the siblings a
1830d 72 65 20 6e 6f 74 20 6c 65 61 66 20 70 61 67 65 re not leaf page
1830e 73 20 6f 66 20 61 6e 0a 20 20 20 20 20 20 20 20 s of an.
1830f 2a 2a 20 69 6e 74 6b 65 79 20 62 2d 74 72 65 65 ** intkey b-tree
18310 2c 20 74 68 65 6e 20 63 65 6c 6c 20 69 20 77 61 , then cell i wa
18311 73 20 61 20 64 69 76 69 64 65 72 20 63 65 6c 6c s a divider cell
18312 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 4f 6c . */. pOl
18313 64 20 3d 20 61 70 43 6f 70 79 5b 2b 2b 6a 5d 3b d = apCopy[++j];
18314 0a 20 20 20 20 20 20 20 20 69 4e 65 78 74 4f 6c . iNextOl
18315 64 20 3d 20 69 20 2b 20 21 6c 65 61 66 44 61 74 d = i + !leafDat
18316 61 20 2b 20 70 4f 6c 64 2d 3e 6e 43 65 6c 6c 20 a + pOld->nCell
18317 2b 20 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f + pOld->nOverflo
18318 77 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 w;. if( p
18319 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 Old->nOverflow )
1831a 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 4f 76 65 {. nOve
1831b 72 66 6c 6f 77 20 3d 20 70 4f 6c 64 2d 3e 6e 4f rflow = pOld->nO
1831c 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 20 20 20 verflow;.
1831d 20 20 20 69 4f 76 65 72 66 6c 6f 77 20 3d 20 69 iOverflow = i
1831e 20 2b 20 21 6c 65 61 66 44 61 74 61 20 2b 20 70 + !leafData + p
1831f 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64 Old->aOvfl[0].id
18320 78 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 x;. }.
18321 20 20 20 20 20 69 73 44 69 76 69 64 65 72 20 3d isDivider =
18322 20 21 6c 65 61 66 44 61 74 61 3b 20 20 0a 20 20 !leafData; .
18323 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 61 73 73 }.. ass
18324 65 72 74 28 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 ert(nOverflow>0
18325 7c 7c 20 69 4f 76 65 72 66 6c 6f 77 3c 69 20 29 || iOverflow<i )
18326 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 6e ;. assert(n
18327 4f 76 65 72 66 6c 6f 77 3c 32 20 7c 7c 20 70 4f Overflow<2 || pO
18328 6c 64 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64 78 ld->aOvfl[0].idx
18329 3d 3d 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 31 5d ==pOld->aOvfl[1]
1832a 2e 69 64 78 2d 31 29 3b 0a 20 20 20 20 20 20 61 .idx-1);. a
1832b 73 73 65 72 74 28 6e 4f 76 65 72 66 6c 6f 77 3c ssert(nOverflow<
1832c 33 20 7c 7c 20 70 4f 6c 64 2d 3e 61 4f 76 66 6c 3 || pOld->aOvfl
1832d 5b 31 5d 2e 69 64 78 3d 3d 70 4f 6c 64 2d 3e 61 [1].idx==pOld->a
1832e 4f 76 66 6c 5b 32 5d 2e 69 64 78 2d 31 29 3b 0a Ovfl[2].idx-1);.
1832f 20 20 20 20 20 20 69 66 28 20 69 3d 3d 69 4f 76 if( i==iOv
18330 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 erflow ){.
18331 20 20 69 73 44 69 76 69 64 65 72 20 3d 20 31 3b isDivider = 1;
18332 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 2d 2d . if( (--
18333 6e 4f 76 65 72 66 6c 6f 77 29 3e 30 20 29 7b 0a nOverflow)>0 ){.
18334 20 20 20 20 20 20 20 20 20 20 69 4f 76 65 72 66 iOverf
18335 6c 6f 77 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d low++;. }
18336 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 . }..
18337 69 66 28 20 69 3d 3d 63 6e 74 4e 65 77 5b 6b 5d if( i==cntNew[k]
18338 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 ){. /* C
18339 65 6c 6c 20 69 20 69 73 20 74 68 65 20 63 65 6c ell i is the cel
1833a 6c 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f l immediately fo
1833b 6c 6c 6f 77 69 6e 67 20 74 68 65 20 6c 61 73 74 llowing the last
1833c 20 63 65 6c 6c 20 6f 6e 20 6e 65 77 0a 20 20 20 cell on new.
1833d 20 20 20 20 20 2a 2a 20 73 69 62 6c 69 6e 67 20 ** sibling
1833e 70 61 67 65 20 6b 2e 20 49 66 20 74 68 65 20 73 page k. If the s
1833f 69 62 6c 69 6e 67 73 20 61 72 65 20 6e 6f 74 20 iblings are not
18340 6c 65 61 66 20 70 61 67 65 73 20 6f 66 20 61 6e leaf pages of an
18341 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 74 6b . ** intk
18342 65 79 20 62 2d 74 72 65 65 2c 20 74 68 65 6e 20 ey b-tree, then
18343 63 65 6c 6c 20 69 20 69 73 20 61 20 64 69 76 69 cell i is a divi
18344 64 65 72 20 63 65 6c 6c 2e 20 20 2a 2f 0a 20 20 der cell. */.
18345 20 20 20 20 20 20 70 4e 65 77 20 3d 20 61 70 4e pNew = apN
18346 65 77 5b 2b 2b 6b 5d 3b 0a 20 20 20 20 20 20 20 ew[++k];.
18347 20 69 66 28 20 21 6c 65 61 66 44 61 74 61 20 29 if( !leafData )
18348 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 continue;.
18349 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 }. assert(
1834a 20 6a 3c 6e 4f 6c 64 20 29 3b 0a 20 20 20 20 20 j<nOld );.
1834b 20 61 73 73 65 72 74 28 20 6b 3c 6e 4e 65 77 20 assert( k<nNew
1834c 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 );.. /* If
1834d 74 68 65 20 63 65 6c 6c 20 77 61 73 20 6f 72 69 the cell was ori
1834e 67 69 6e 61 6c 6c 79 20 64 69 76 69 64 65 72 20 ginally divider
1834f 63 65 6c 6c 20 28 61 6e 64 20 69 73 20 6e 6f 74 cell (and is not
18350 20 6e 6f 77 29 20 6f 72 0a 20 20 20 20 20 20 2a now) or. *
18351 2a 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 * an overflow ce
18352 6c 6c 2c 20 6f 72 20 69 66 20 74 68 65 20 63 65 ll, or if the ce
18353 6c 6c 20 77 61 73 20 6c 6f 63 61 74 65 64 20 6f ll was located o
18354 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 73 69 n a different si
18355 62 6c 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 70 bling. ** p
18356 61 67 65 20 62 65 66 6f 72 65 20 74 68 65 20 62 age before the b
18357 61 6c 61 6e 63 69 6e 67 2c 20 74 68 65 6e 20 74 alancing, then t
18358 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 he pointer map e
18359 6e 74 72 69 65 73 20 61 73 73 6f 63 69 61 74 65 ntries associate
1835a 64 0a 20 20 20 20 20 20 2a 2a 20 77 69 74 68 20 d. ** with
1835b 61 6e 79 20 63 68 69 6c 64 20 6f 72 20 6f 76 65 any child or ove
1835c 72 66 6c 6f 77 20 70 61 67 65 73 20 6e 65 65 64 rflow pages need
1835d 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 2e 20 to be updated.
1835e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69 73 */. if( is
1835f 44 69 76 69 64 65 72 20 7c 7c 20 70 4f 6c 64 2d Divider || pOld-
18360 3e 70 67 6e 6f 21 3d 70 4e 65 77 2d 3e 70 67 6e >pgno!=pNew->pgn
18361 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 o ){. if(
18362 20 21 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e !leafCorrection
18363 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 74 ){. pt
18364 72 6d 61 70 50 75 74 28 70 42 74 2c 20 67 65 74 rmapPut(pBt, get
18365 34 62 79 74 65 28 61 70 43 65 6c 6c 5b 69 5d 29 4byte(apCell[i])
18366 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 , PTRMAP_BTREE,
18367 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 pNew->pgno, &rc)
18368 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
18369 20 20 20 20 69 66 28 20 73 7a 43 65 6c 6c 5b 69 if( szCell[i
1836a 5d 3e 70 4e 65 77 2d 3e 6d 69 6e 4c 6f 63 61 6c ]>pNew->minLocal
1836b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 74 ){. pt
1836c 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28 70 rmapPutOvflPtr(p
1836d 4e 65 77 2c 20 61 70 43 65 6c 6c 5b 69 5d 2c 20 New, apCell[i],
1836e 26 72 63 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a &rc);. }.
1836f 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 }. }..
18370 20 20 20 69 66 28 20 21 6c 65 61 66 43 6f 72 72 if( !leafCorr
18371 65 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 ection ){.
18372 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4e 65 77 3b for(i=0; i<nNew;
18373 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 75 i++){. u
18374 33 32 20 6b 65 79 20 3d 20 67 65 74 34 62 79 74 32 key = get4byt
18375 65 28 26 61 70 4e 65 77 5b 69 5d 2d 3e 61 44 61 e(&apNew[i]->aDa
18376 74 61 5b 38 5d 29 3b 0a 20 20 20 20 20 20 20 20 ta[8]);.
18377 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 6b ptrmapPut(pBt, k
18378 65 79 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 ey, PTRMAP_BTREE
18379 2c 20 61 70 4e 65 77 5b 69 5d 2d 3e 70 67 6e 6f , apNew[i]->pgno
1837a 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 7d 0a , &rc);. }.
1837b 20 20 20 20 7d 0a 0a 23 69 66 20 30 0a 20 20 20 }..#if 0.
1837c 20 2f 2a 20 54 68 65 20 70 74 72 6d 61 70 43 68 /* The ptrmapCh
1837d 65 63 6b 50 61 67 65 73 28 29 20 63 6f 6e 74 61 eckPages() conta
1837e 69 6e 73 20 61 73 73 65 72 74 28 29 20 73 74 61 ins assert() sta
1837f 74 65 6d 65 6e 74 73 20 74 68 61 74 20 76 65 72 tements that ver
18380 69 66 79 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 ify that. **
18381 61 6c 6c 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 all pointer map
18382 70 61 67 65 73 20 61 72 65 20 73 65 74 20 63 6f pages are set co
18383 72 72 65 63 74 6c 79 2e 20 54 68 69 73 20 69 73 rrectly. This is
18384 20 68 65 6c 70 66 75 6c 20 77 68 69 6c 65 20 0a helpful while .
18385 20 20 20 20 2a 2a 20 64 65 62 75 67 67 69 6e 67 ** debugging
18386 2e 20 54 68 69 73 20 69 73 20 75 73 75 61 6c 6c . This is usuall
18387 79 20 64 69 73 61 62 6c 65 64 20 62 65 63 61 75 y disabled becau
18388 73 65 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 se a corrupt dat
18389 61 62 61 73 65 20 6d 61 79 0a 20 20 20 20 2a 2a abase may. **
1838a 20 63 61 75 73 65 20 61 6e 20 61 73 73 65 72 74 cause an assert
1838b 28 29 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 () statement to
1838c 66 61 69 6c 2e 20 20 2a 2f 0a 20 20 20 20 70 74 fail. */. pt
1838d 72 6d 61 70 43 68 65 63 6b 50 61 67 65 73 28 61 rmapCheckPages(a
1838e 70 4e 65 77 2c 20 6e 4e 65 77 29 3b 0a 20 20 20 pNew, nNew);.
1838f 20 70 74 72 6d 61 70 43 68 65 63 6b 50 61 67 65 ptrmapCheckPage
18390 73 28 26 70 50 61 72 65 6e 74 2c 20 31 29 3b 0a s(&pParent, 1);.
18391 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 61 73 #endif. }.. as
18392 73 65 72 74 28 20 70 50 61 72 65 6e 74 2d 3e 69 sert( pParent->i
18393 73 49 6e 69 74 20 29 3b 0a 20 20 54 52 41 43 45 sInit );. TRACE
18394 28 28 22 42 41 4c 41 4e 43 45 3a 20 66 69 6e 69 (("BALANCE: fini
18395 73 68 65 64 3a 20 6f 6c 64 3d 25 64 20 6e 65 77 shed: old=%d new
18396 3d 25 64 20 63 65 6c 6c 73 3d 25 64 5c 6e 22 2c =%d cells=%d\n",
18397 0a 20 20 20 20 20 20 20 20 20 20 6e 4f 6c 64 2c . nOld,
18398 20 6e 4e 65 77 2c 20 6e 43 65 6c 6c 29 29 3b 0a nNew, nCell));.
18399 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 6c 65 61 6e . /*. ** Clean
1839a 75 70 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e up before return
1839b 69 6e 67 2e 0a 20 20 2a 2f 0a 62 61 6c 61 6e 63 ing.. */.balanc
1839c 65 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71 6c e_cleanup:. sql
1839d 69 74 65 33 53 63 72 61 74 63 68 46 72 65 65 28 ite3ScratchFree(
1839e 61 70 43 65 6c 6c 29 3b 0a 20 20 66 6f 72 28 69 apCell);. for(i
1839f 3d 30 3b 20 69 3c 6e 4f 6c 64 3b 20 69 2b 2b 29 =0; i<nOld; i++)
183a0 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 {. releasePag
183a1 65 28 61 70 4f 6c 64 5b 69 5d 29 3b 0a 20 20 7d e(apOld[i]);. }
183a2 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4e . for(i=0; i<nN
183a3 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 72 65 ew; i++){. re
183a4 6c 65 61 73 65 50 61 67 65 28 61 70 4e 65 77 5b leasePage(apNew[
183a5 69 5d 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 i]);. }.. retu
183a6 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a rn rc;.}.../*.**
183a7 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
183a8 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 s called when th
183a9 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 61 e root page of a
183aa 20 62 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 b-tree structur
183ab 65 20 69 73 0a 2a 2a 20 6f 76 65 72 66 75 6c 6c e is.** overfull
183ac 20 28 68 61 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 (has one or mor
183ad 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 e overflow pages
183ae 29 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 77 20 63 )..**.** A new c
183af 68 69 6c 64 20 70 61 67 65 20 69 73 20 61 6c 6c hild page is all
183b0 6f 63 61 74 65 64 20 61 6e 64 20 74 68 65 20 63 ocated and the c
183b1 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63 ontents of the c
183b2 75 72 72 65 6e 74 20 72 6f 6f 74 0a 2a 2a 20 70 urrent root.** p
183b3 61 67 65 2c 20 69 6e 63 6c 75 64 69 6e 67 20 6f age, including o
183b4 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 2c 20 61 verflow cells, a
183b5 72 65 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 re copied into t
183b6 68 65 20 63 68 69 6c 64 2e 20 54 68 65 20 72 6f he child. The ro
183b7 6f 74 0a 2a 2a 20 70 61 67 65 20 69 73 20 74 68 ot.** page is th
183b8 65 6e 20 6f 76 65 72 77 72 69 74 74 65 6e 20 74 en overwritten t
183b9 6f 20 6d 61 6b 65 20 69 74 20 61 6e 20 65 6d 70 o make it an emp
183ba 74 79 20 70 61 67 65 20 77 69 74 68 20 74 68 65 ty page with the
183bb 20 72 69 67 68 74 2d 63 68 69 6c 64 20 0a 2a 2a right-child .**
183bc 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 69 6e pointer pointin
183bd 67 20 74 6f 20 74 68 65 20 6e 65 77 20 70 61 67 g to the new pag
183be 65 2e 0a 2a 2a 0a 2a 2a 20 42 65 66 6f 72 65 20 e..**.** Before
183bf 72 65 74 75 72 6e 69 6e 67 2c 20 61 6c 6c 20 70 returning, all p
183c0 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 ointer-map entri
183c1 65 73 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 es corresponding
183c2 20 74 6f 20 70 61 67 65 73 20 0a 2a 2a 20 74 68 to pages .** th
183c3 61 74 20 74 68 65 20 6e 65 77 20 63 68 69 6c 64 at the new child
183c4 2d 70 61 67 65 20 6e 6f 77 20 63 6f 6e 74 61 69 -page now contai
183c5 6e 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 61 ns pointers to a
183c6 72 65 20 75 70 64 61 74 65 64 2e 20 54 68 65 0a re updated. The.
183c7 2a 2a 20 65 6e 74 72 79 20 63 6f 72 72 65 73 70 ** entry corresp
183c8 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 6e 65 onding to the ne
183c9 77 20 72 69 67 68 74 2d 63 68 69 6c 64 20 70 6f w right-child po
183ca 69 6e 74 65 72 20 6f 66 20 74 68 65 20 72 6f 6f inter of the roo
183cb 74 0a 2a 2a 20 70 61 67 65 20 69 73 20 61 6c 73 t.** page is als
183cc 6f 20 75 70 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a o updated..**.**
183cd 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 If successful,
183ce 2a 70 70 43 68 69 6c 64 20 69 73 20 73 65 74 20 *ppChild is set
183cf 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20 72 65 66 to contain a ref
183d0 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 63 68 erence to the ch
183d1 69 6c 64 20 0a 2a 2a 20 70 61 67 65 20 61 6e 64 ild .** page and
183d2 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
183d3 74 75 72 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 turned. In this
183d4 63 61 73 65 20 74 68 65 20 63 61 6c 6c 65 72 20 case the caller
183d5 69 73 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 74 is required.** t
183d6 6f 20 63 61 6c 6c 20 72 65 6c 65 61 73 65 50 61 o call releasePa
183d7 67 65 28 29 20 6f 6e 20 2a 70 70 43 68 69 6c 64 ge() on *ppChild
183d8 20 65 78 61 63 74 6c 79 20 6f 6e 63 65 2e 20 49 exactly once. I
183d9 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
183da 73 2c 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 63 s,.** an error c
183db 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 ode is returned
183dc 61 6e 64 20 2a 70 70 43 68 69 6c 64 20 69 73 20 and *ppChild is
183dd 73 65 74 20 74 6f 20 30 2e 0a 2a 2f 0a 73 74 61 set to 0..*/.sta
183de 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f tic int balance_
183df 64 65 65 70 65 72 28 4d 65 6d 50 61 67 65 20 2a deeper(MemPage *
183e0 70 52 6f 6f 74 2c 20 4d 65 6d 50 61 67 65 20 2a pRoot, MemPage *
183e1 2a 70 70 43 68 69 6c 64 29 7b 0a 20 20 69 6e 74 *ppChild){. int
183e2 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 rc;
183e3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
183e4 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d eturn value from
183e5 20 73 75 62 70 72 6f 63 65 64 75 72 65 73 20 2a subprocedures *
183e6 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 43 68 /. MemPage *pCh
183e7 69 6c 64 20 3d 20 30 3b 20 20 20 20 20 20 20 20 ild = 0;
183e8 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
183e9 20 61 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67 a new child pag
183ea 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f e */. Pgno pgno
183eb 43 68 69 6c 64 20 3d 20 30 3b 20 20 20 20 20 20 Child = 0;
183ec 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 /* Page nu
183ed 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 77 20 mber of the new
183ee 63 68 69 6c 64 20 70 61 67 65 20 2a 2f 0a 20 20 child page */.
183ef 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
183f0 70 52 6f 6f 74 2d 3e 70 42 74 3b 20 20 20 20 2f pRoot->pBt; /
183f1 2a 20 54 68 65 20 42 54 72 65 65 20 2a 2f 0a 0a * The BTree */..
183f2 20 20 61 73 73 65 72 74 28 20 70 52 6f 6f 74 2d assert( pRoot-
183f3 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 29 3b 0a >nOverflow>0 );.
183f4 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
183f5 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 3_mutex_held(pBt
183f6 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f ->mutex) );.. /
183f7 2a 20 4d 61 6b 65 20 70 52 6f 6f 74 2c 20 74 68 * Make pRoot, th
183f8 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 e root page of t
183f9 68 65 20 62 2d 74 72 65 65 2c 20 77 72 69 74 61 he b-tree, writa
183fa 62 6c 65 2e 20 41 6c 6c 6f 63 61 74 65 20 61 20 ble. Allocate a
183fb 6e 65 77 20 0a 20 20 2a 2a 20 70 61 67 65 20 74 new . ** page t
183fc 68 61 74 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 hat will become
183fd 74 68 65 20 6e 65 77 20 72 69 67 68 74 2d 63 68 the new right-ch
183fe 69 6c 64 20 6f 66 20 70 50 61 67 65 2e 20 43 6f ild of pPage. Co
183ff 70 79 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 0a py the contents.
18400 20 20 2a 2a 20 6f 66 20 74 68 65 20 6e 6f 64 65 ** of the node
18401 20 73 74 6f 72 65 64 20 6f 6e 20 70 52 6f 6f 74 stored on pRoot
18402 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 20 63 68 into the new ch
18403 69 6c 64 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 ild page.. */.
18404 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
18405 65 72 57 72 69 74 65 28 70 52 6f 6f 74 2d 3e 70 erWrite(pRoot->p
18406 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 DbPage);. if( r
18407 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
18408 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 rc = allocat
18409 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 26 eBtreePage(pBt,&
1840a 70 43 68 69 6c 64 2c 26 70 67 6e 6f 43 68 69 6c pChild,&pgnoChil
1840b 64 2c 70 52 6f 6f 74 2d 3e 70 67 6e 6f 2c 30 29 d,pRoot->pgno,0)
1840c 3b 0a 20 20 20 20 63 6f 70 79 4e 6f 64 65 43 6f ;. copyNodeCo
1840d 6e 74 65 6e 74 28 70 52 6f 6f 74 2c 20 70 43 68 ntent(pRoot, pCh
1840e 69 6c 64 2c 20 26 72 63 29 3b 0a 20 20 20 20 69 ild, &rc);. i
1840f 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 f( ISAUTOVACUUM
18410 29 7b 0a 20 20 20 20 20 20 70 74 72 6d 61 70 50 ){. ptrmapP
18411 75 74 28 70 42 74 2c 20 70 67 6e 6f 43 68 69 6c ut(pBt, pgnoChil
18412 64 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c d, PTRMAP_BTREE,
18413 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f 2c 20 26 72 pRoot->pgno, &r
18414 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 c);. }. }.
18415 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 2a 70 if( rc ){. *p
18416 70 43 68 69 6c 64 20 3d 20 30 3b 0a 20 20 20 20 pChild = 0;.
18417 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 68 69 releasePage(pChi
18418 6c 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 ld);. return
18419 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 rc;. }. assert
1841a 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 ( sqlite3PagerIs
1841b 77 72 69 74 65 61 62 6c 65 28 70 43 68 69 6c 64 writeable(pChild
1841c 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 ->pDbPage) );.
1841d 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
1841e 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 agerIswriteable(
1841f 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 20 pRoot->pDbPage)
18420 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 68 );. assert( pCh
18421 69 6c 64 2d 3e 6e 43 65 6c 6c 3d 3d 70 52 6f 6f ild->nCell==pRoo
18422 74 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20 54 t->nCell );.. T
18423 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 RACE(("BALANCE:
18424 63 6f 70 79 20 72 6f 6f 74 20 25 64 20 69 6e 74 copy root %d int
18425 6f 20 25 64 5c 6e 22 2c 20 70 52 6f 6f 74 2d 3e o %d\n", pRoot->
18426 70 67 6e 6f 2c 20 70 43 68 69 6c 64 2d 3e 70 67 pgno, pChild->pg
18427 6e 6f 29 29 3b 0a 0a 20 20 2f 2a 20 43 6f 70 79 no));.. /* Copy
18428 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 65 the overflow ce
18429 6c 6c 73 20 66 72 6f 6d 20 70 52 6f 6f 74 20 74 lls from pRoot t
1842a 6f 20 70 43 68 69 6c 64 20 2a 2f 0a 20 20 6d 65 o pChild */. me
1842b 6d 63 70 79 28 70 43 68 69 6c 64 2d 3e 61 4f 76 mcpy(pChild->aOv
1842c 66 6c 2c 20 70 52 6f 6f 74 2d 3e 61 4f 76 66 6c fl, pRoot->aOvfl
1842d 2c 20 70 52 6f 6f 74 2d 3e 6e 4f 76 65 72 66 6c , pRoot->nOverfl
1842e 6f 77 2a 73 69 7a 65 6f 66 28 70 52 6f 6f 74 2d ow*sizeof(pRoot-
1842f 3e 61 4f 76 66 6c 5b 30 5d 29 29 3b 0a 20 20 70 >aOvfl[0]));. p
18430 43 68 69 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 Child->nOverflow
18431 20 3d 20 70 52 6f 6f 74 2d 3e 6e 4f 76 65 72 66 = pRoot->nOverf
18432 6c 6f 77 3b 0a 0a 20 20 2f 2a 20 5a 65 72 6f 20 low;.. /* Zero
18433 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
18434 70 52 6f 6f 74 2e 20 54 68 65 6e 20 69 6e 73 74 pRoot. Then inst
18435 61 6c 6c 20 70 43 68 69 6c 64 20 61 73 20 74 68 all pChild as th
18436 65 20 72 69 67 68 74 2d 63 68 69 6c 64 2e 20 2a e right-child. *
18437 2f 0a 20 20 7a 65 72 6f 50 61 67 65 28 70 52 6f /. zeroPage(pRo
18438 6f 74 2c 20 70 43 68 69 6c 64 2d 3e 61 44 61 74 ot, pChild->aDat
18439 61 5b 30 5d 20 26 20 7e 50 54 46 5f 4c 45 41 46 a[0] & ~PTF_LEAF
1843a 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26 70 );. put4byte(&p
1843b 52 6f 6f 74 2d 3e 61 44 61 74 61 5b 70 52 6f 6f Root->aData[pRoo
1843c 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 2c t->hdrOffset+8],
1843d 20 70 67 6e 6f 43 68 69 6c 64 29 3b 0a 0a 20 20 pgnoChild);..
1843e 2a 70 70 43 68 69 6c 64 20 3d 20 70 43 68 69 6c *ppChild = pChil
1843f 64 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 d;. return SQLI
18440 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
18441 54 68 65 20 70 61 67 65 20 74 68 61 74 20 70 43 The page that pC
18442 75 72 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 ur currently poi
18443 6e 74 73 20 74 6f 20 68 61 73 20 6a 75 73 74 20 nts to has just
18444 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 69 6e been modified in
18445 0a 2a 2a 20 73 6f 6d 65 20 77 61 79 2e 20 54 68 .** some way. Th
18446 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 69 67 75 is function figu
18447 72 65 73 20 6f 75 74 20 69 66 20 74 68 69 73 20 res out if this
18448 6d 6f 64 69 66 69 63 61 74 69 6f 6e 20 6d 65 61 modification mea
18449 6e 73 20 74 68 65 0a 2a 2a 20 74 72 65 65 20 6e ns the.** tree n
1844a 65 65 64 73 20 74 6f 20 62 65 20 62 61 6c 61 6e eeds to be balan
1844b 63 65 64 2c 20 61 6e 64 20 69 66 20 73 6f 20 63 ced, and if so c
1844c 61 6c 6c 73 20 74 68 65 20 61 70 70 72 6f 70 72 alls the appropr
1844d 69 61 74 65 20 62 61 6c 61 6e 63 69 6e 67 20 0a iate balancing .
1844e 2a 2a 20 72 6f 75 74 69 6e 65 2e 20 42 61 6c 61 ** routine. Bala
1844f 6e 63 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 61 ncing routines a
18450 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 62 61 6c 61 re:.**.** bala
18451 6e 63 65 5f 71 75 69 63 6b 28 29 0a 2a 2a 20 20 nce_quick().**
18452 20 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 28 balance_deeper(
18453 29 0a 2a 2a 20 20 20 62 61 6c 61 6e 63 65 5f 6e ).** balance_n
18454 6f 6e 72 6f 6f 74 28 29 0a 2a 2f 0a 73 74 61 74 onroot().*/.stat
18455 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 65 28 42 ic int balance(B
18456 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a tCursor *pCur){.
18457 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
18458 45 5f 4f 4b 3b 0a 20 20 63 6f 6e 73 74 20 69 6e E_OK;. const in
18459 74 20 6e 4d 69 6e 20 3d 20 70 43 75 72 2d 3e 70 t nMin = pCur->p
1845a 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2a Bt->usableSize *
1845b 20 32 20 2f 20 33 3b 0a 20 20 75 38 20 61 42 61 2 / 3;. u8 aBa
1845c 6c 61 6e 63 65 51 75 69 63 6b 53 70 61 63 65 5b lanceQuickSpace[
1845d 31 33 5d 3b 0a 20 20 75 38 20 2a 70 46 72 65 65 13];. u8 *pFree
1845e 20 3d 20 30 3b 0a 0a 20 20 54 45 53 54 4f 4e 4c = 0;.. TESTONL
1845f 59 28 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f 71 Y( int balance_q
18460 75 69 63 6b 5f 63 61 6c 6c 65 64 20 3d 20 30 20 uick_called = 0
18461 29 3b 0a 20 20 54 45 53 54 4f 4e 4c 59 28 20 69 );. TESTONLY( i
18462 6e 74 20 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 nt balance_deepe
18463 72 5f 63 61 6c 6c 65 64 20 3d 20 30 20 29 3b 0a r_called = 0 );.
18464 0a 20 20 64 6f 20 7b 0a 20 20 20 20 69 6e 74 20 . do {. int
18465 69 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 69 50 iPage = pCur->iP
18466 61 67 65 3b 0a 20 20 20 20 4d 65 6d 50 61 67 65 age;. MemPage
18467 20 2a 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e *pPage = pCur->
18468 61 70 50 61 67 65 5b 69 50 61 67 65 5d 3b 0a 0a apPage[iPage];..
18469 20 20 20 20 69 66 28 20 69 50 61 67 65 3d 3d 30 if( iPage==0
1846a 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 50 ){. if( pP
1846b 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 age->nOverflow )
1846c 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 {. /* The
1846d 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 root page of th
1846e 65 20 62 2d 74 72 65 65 20 69 73 20 6f 76 65 72 e b-tree is over
1846f 66 75 6c 6c 2e 20 49 6e 20 74 68 69 73 20 63 61 full. In this ca
18470 73 65 20 63 61 6c 6c 20 74 68 65 0a 20 20 20 20 se call the.
18471 20 20 20 20 2a 2a 20 62 61 6c 61 6e 63 65 5f 64 ** balance_d
18472 65 65 70 65 72 28 29 20 66 75 6e 63 74 69 6f 6e eeper() function
18473 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77 to create a new
18474 20 63 68 69 6c 64 20 66 6f 72 20 74 68 65 20 72 child for the r
18475 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 20 20 20 oot-page.
18476 20 2a 2a 20 61 6e 64 20 63 6f 70 79 20 74 68 65 ** and copy the
18477 20 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 current content
18478 73 20 6f 66 20 74 68 65 20 72 6f 6f 74 2d 70 61 s of the root-pa
18479 67 65 20 74 6f 20 69 74 2e 20 54 68 65 0a 20 20 ge to it. The.
1847a 20 20 20 20 20 20 2a 2a 20 6e 65 78 74 20 69 74 ** next it
1847b 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 eration of the d
1847c 6f 2d 6c 6f 6f 70 20 77 69 6c 6c 20 62 61 6c 61 o-loop will bala
1847d 6e 63 65 20 74 68 65 20 63 68 69 6c 64 20 70 61 nce the child pa
1847e 67 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 20 0a ge.. */ .
1847f 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
18480 28 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 5f (balance_deeper_
18481 63 61 6c 6c 65 64 2b 2b 29 3d 3d 30 20 29 3b 0a called++)==0 );.
18482 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 61 6c rc = bal
18483 61 6e 63 65 5f 64 65 65 70 65 72 28 70 50 61 67 ance_deeper(pPag
18484 65 2c 20 26 70 43 75 72 2d 3e 61 70 50 61 67 65 e, &pCur->apPage
18485 5b 31 5d 29 3b 0a 20 20 20 20 20 20 20 20 69 66 [1]);. if
18486 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
18487 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 43 75 ){. pCu
18488 72 2d 3e 69 50 61 67 65 20 3d 20 31 3b 0a 20 20 r->iPage = 1;.
18489 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 61 69 pCur->ai
1848a 49 64 78 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 Idx[0] = 0;.
1848b 20 20 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 pCur->aiId
1848c 78 5b 31 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 x[1] = 0;.
1848d 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 assert( pCur
1848e 2d 3e 61 70 50 61 67 65 5b 31 5d 2d 3e 6e 4f 76 ->apPage[1]->nOv
1848f 65 72 66 6c 6f 77 20 29 3b 0a 20 20 20 20 20 20 erflow );.
18490 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b }. }else{
18491 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
18492 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 }. }els
18493 65 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 4f 76 e if( pPage->nOv
18494 65 72 66 6c 6f 77 3d 3d 30 20 26 26 20 70 50 61 erflow==0 && pPa
18495 67 65 2d 3e 6e 46 72 65 65 3c 3d 6e 4d 69 6e 20 ge->nFree<=nMin
18496 29 7b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a ){. break;.
18497 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
18498 20 4d 65 6d 50 61 67 65 20 2a 20 63 6f 6e 73 74 MemPage * const
18499 20 70 50 61 72 65 6e 74 20 3d 20 70 43 75 72 2d pParent = pCur-
1849a 3e 61 70 50 61 67 65 5b 69 50 61 67 65 2d 31 5d >apPage[iPage-1]
1849b 3b 0a 20 20 20 20 20 20 69 6e 74 20 63 6f 6e 73 ;. int cons
1849c 74 20 69 49 64 78 20 3d 20 70 43 75 72 2d 3e 61 t iIdx = pCur->a
1849d 69 49 64 78 5b 69 50 61 67 65 2d 31 5d 3b 0a 0a iIdx[iPage-1];..
1849e 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1849f 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 e3PagerWrite(pPa
184a0 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a rent->pDbPage);.
184a1 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
184a2 4c 49 54 45 5f 4f 4b 20 29 7b 0a 23 69 66 6e 64 LITE_OK ){.#ifnd
184a3 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 51 ef SQLITE_OMIT_Q
184a4 55 49 43 4b 42 41 4c 41 4e 43 45 0a 20 20 20 20 UICKBALANCE.
184a5 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 68 if( pPage->h
184a6 61 73 44 61 74 61 0a 20 20 20 20 20 20 20 20 20 asData.
184a7 26 26 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 && pPage->nOverf
184a8 6c 6f 77 3d 3d 31 0a 20 20 20 20 20 20 20 20 20 low==1.
184a9 26 26 20 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b && pPage->aOvfl[
184aa 30 5d 2e 69 64 78 3d 3d 70 50 61 67 65 2d 3e 6e 0].idx==pPage->n
184ab 43 65 6c 6c 0a 20 20 20 20 20 20 20 20 20 26 26 Cell. &&
184ac 20 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f 21 3d pParent->pgno!=
184ad 31 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 50 1. && pP
184ae 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3d 3d 69 49 arent->nCell==iI
184af 64 78 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 20 dx. ){.
184b0 20 20 20 20 20 20 20 20 2f 2a 20 43 61 6c 6c 20 /* Call
184b1 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 29 20 balance_quick()
184b2 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 to create a new
184b3 73 69 62 6c 69 6e 67 20 6f 66 20 70 50 61 67 65 sibling of pPage
184b4 20 6f 6e 20 77 68 69 63 68 0a 20 20 20 20 20 20 on which.
184b5 20 20 20 20 2a 2a 20 74 6f 20 73 74 6f 72 65 20 ** to store
184b6 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c the overflow cel
184b7 6c 2e 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b l. balance_quick
184b8 28 29 20 69 6e 73 65 72 74 73 20 61 20 6e 65 77 () inserts a new
184b9 20 63 65 6c 6c 0a 20 20 20 20 20 20 20 20 20 20 cell.
184ba 2a 2a 20 69 6e 74 6f 20 70 50 61 72 65 6e 74 2c ** into pParent,
184bb 20 77 68 69 63 68 20 6d 61 79 20 63 61 75 73 65 which may cause
184bc 20 70 50 61 72 65 6e 74 20 6f 76 65 72 66 6c 6f pParent overflo
184bd 77 2e 20 49 66 20 74 68 69 73 0a 20 20 20 20 20 w. If this.
184be 20 20 20 20 20 2a 2a 20 68 61 70 70 65 6e 73 2c ** happens,
184bf 20 74 68 65 20 6e 65 78 74 20 69 6e 74 65 72 61 the next intera
184c0 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 6f 2d 6c tion of the do-l
184c1 6f 6f 70 20 77 69 6c 6c 20 62 61 6c 61 6e 63 65 oop will balance
184c2 20 70 50 61 72 65 6e 74 20 0a 20 20 20 20 20 20 pParent .
184c3 20 20 20 20 2a 2a 20 75 73 65 20 65 69 74 68 65 ** use eithe
184c4 72 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f r balance_nonroo
184c5 74 28 29 20 6f 72 20 62 61 6c 61 6e 63 65 5f 64 t() or balance_d
184c6 65 65 70 65 72 28 29 2e 20 55 6e 74 69 6c 20 74 eeper(). Until t
184c7 68 69 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a his. **
184c8 20 68 61 70 70 65 6e 73 2c 20 74 68 65 20 6f 76 happens, the ov
184c9 65 72 66 6c 6f 77 20 63 65 6c 6c 20 69 73 20 73 erflow cell is s
184ca 74 6f 72 65 64 20 69 6e 20 74 68 65 20 61 42 61 tored in the aBa
184cb 6c 61 6e 63 65 51 75 69 63 6b 53 70 61 63 65 5b lanceQuickSpace[
184cc 5d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62 ]. ** b
184cd 75 66 66 65 72 2e 20 0a 20 20 20 20 20 20 20 20 uffer. .
184ce 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 20 20 2a **. *
184cf 2a 20 54 68 65 20 70 75 72 70 6f 73 65 20 6f 66 * The purpose of
184d0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 the following a
184d1 73 73 65 72 74 28 29 20 69 73 20 74 6f 20 63 68 ssert() is to ch
184d2 65 63 6b 20 74 68 61 74 20 6f 6e 6c 79 20 61 0a eck that only a.
184d3 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73 69 6e ** sin
184d4 67 6c 65 20 63 61 6c 6c 20 74 6f 20 62 61 6c 61 gle call to bala
184d5 6e 63 65 5f 71 75 69 63 6b 28 29 20 69 73 20 6d nce_quick() is m
184d6 61 64 65 20 66 6f 72 20 65 61 63 68 20 63 61 6c ade for each cal
184d7 6c 20 74 6f 20 74 68 69 73 0a 20 20 20 20 20 20 l to this.
184d8 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e ** function.
184d9 20 49 66 20 74 68 69 73 20 77 65 72 65 20 6e 6f If this were no
184da 74 20 76 65 72 69 66 69 65 64 2c 20 61 20 73 75 t verified, a su
184db 62 74 6c 65 20 62 75 67 20 69 6e 76 6f 6c 76 69 btle bug involvi
184dc 6e 67 20 72 65 75 73 65 0a 20 20 20 20 20 20 20 ng reuse.
184dd 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 61 42 61 ** of the aBa
184de 6c 61 6e 63 65 51 75 69 63 6b 53 70 61 63 65 5b lanceQuickSpace[
184df 5d 20 6d 69 67 68 74 20 73 6e 65 61 6b 20 69 6e ] might sneak in
184e0 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 .. */.
184e1 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 assert(
184e2 20 28 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 5f (balance_quick_
184e3 63 61 6c 6c 65 64 2b 2b 29 3d 3d 30 20 29 3b 0a called++)==0 );.
184e4 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 rc = b
184e5 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 70 50 61 alance_quick(pPa
184e6 72 65 6e 74 2c 20 70 50 61 67 65 2c 20 61 42 61 rent, pPage, aBa
184e7 6c 61 6e 63 65 51 75 69 63 6b 53 70 61 63 65 29 lanceQuickSpace)
184e8 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 0a ;. }else.
184e9 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 7b #endif. {
184ea 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e . /* In
184eb 20 74 68 69 73 20 63 61 73 65 2c 20 63 61 6c 6c this case, call
184ec 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 balance_nonroot
184ed 28 29 20 74 6f 20 72 65 64 69 73 74 72 69 62 75 () to redistribu
184ee 74 65 20 63 65 6c 6c 73 0a 20 20 20 20 20 20 20 te cells.
184ef 20 20 20 2a 2a 20 62 65 74 77 65 65 6e 20 70 50 ** between pP
184f0 61 67 65 20 61 6e 64 20 75 70 20 74 6f 20 32 20 age and up to 2
184f1 6f 66 20 69 74 73 20 73 69 62 6c 69 6e 67 20 70 of its sibling p
184f2 61 67 65 73 2e 20 54 68 69 73 20 69 6e 76 6f 6c ages. This invol
184f3 76 65 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a ves. **
184f4 20 6d 6f 64 69 66 79 69 6e 67 20 74 68 65 20 63 modifying the c
184f5 6f 6e 74 65 6e 74 73 20 6f 66 20 70 50 61 72 65 ontents of pPare
184f6 6e 74 2c 20 77 68 69 63 68 20 6d 61 79 20 63 61 nt, which may ca
184f7 75 73 65 20 70 50 61 72 65 6e 74 20 74 6f 0a 20 use pParent to.
184f8 20 20 20 20 20 20 20 20 20 2a 2a 20 62 65 63 6f ** beco
184f9 6d 65 20 6f 76 65 72 66 75 6c 6c 20 6f 72 20 75 me overfull or u
184fa 6e 64 65 72 66 75 6c 6c 2e 20 54 68 65 20 6e 65 nderfull. The ne
184fb 78 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 xt iteration of
184fc 74 68 65 20 64 6f 2d 6c 6f 6f 70 0a 20 20 20 20 the do-loop.
184fd 20 20 20 20 20 20 2a 2a 20 77 69 6c 6c 20 62 61 ** will ba
184fe 6c 61 6e 63 65 20 74 68 65 20 70 61 72 65 6e 74 lance the parent
184ff 20 70 61 67 65 20 74 6f 20 63 6f 72 72 65 63 74 page to correct
18500 20 74 68 69 73 2e 0a 20 20 20 20 20 20 20 20 20 this..
18501 20 2a 2a 20 0a 20 20 20 20 20 20 20 20 20 20 2a ** . *
18502 2a 20 49 66 20 74 68 65 20 70 61 72 65 6e 74 20 * If the parent
18503 70 61 67 65 20 62 65 63 6f 6d 65 73 20 6f 76 65 page becomes ove
18504 72 66 75 6c 6c 2c 20 74 68 65 20 6f 76 65 72 66 rfull, the overf
18505 6c 6f 77 20 63 65 6c 6c 20 6f 72 20 63 65 6c 6c low cell or cell
18506 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 s. ** a
18507 72 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 re stored in the
18508 20 70 53 70 61 63 65 20 62 75 66 66 65 72 20 61 pSpace buffer a
18509 6c 6c 6f 63 61 74 65 64 20 69 6d 6d 65 64 69 61 llocated immedia
1850a 74 65 6c 79 20 62 65 6c 6f 77 2e 20 0a 20 20 20 tely below. .
1850b 20 20 20 20 20 20 20 2a 2a 20 41 20 73 75 62 73 ** A subs
1850c 65 71 75 65 6e 74 20 69 74 65 72 61 74 69 6f 6e equent iteration
1850d 20 6f 66 20 74 68 65 20 64 6f 2d 6c 6f 6f 70 20 of the do-loop
1850e 77 69 6c 6c 20 64 65 61 6c 20 77 69 74 68 20 74 will deal with t
1850f 68 69 73 20 62 79 0a 20 20 20 20 20 20 20 20 20 his by.
18510 20 2a 2a 20 63 61 6c 6c 69 6e 67 20 62 61 6c 61 ** calling bala
18511 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 20 28 62 nce_nonroot() (b
18512 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 28 29 20 alance_deeper()
18513 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 20 66 69 may be called fi
18514 72 73 74 2c 0a 20 20 20 20 20 20 20 20 20 20 2a rst,. *
18515 2a 20 62 75 74 20 69 74 20 64 6f 65 73 6e 27 74 * but it doesn't
18516 20 64 65 61 6c 20 77 69 74 68 20 6f 76 65 72 66 deal with overf
18517 6c 6f 77 20 63 65 6c 6c 73 20 2d 20 6a 75 73 74 low cells - just
18518 20 6d 6f 76 65 73 20 74 68 65 6d 20 74 6f 20 61 moves them to a
18519 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 64 69 . ** di
1851a 66 66 65 72 65 6e 74 20 70 61 67 65 29 2e 20 4f fferent page). O
1851b 6e 63 65 20 74 68 69 73 20 73 75 62 73 65 71 75 nce this subsequ
1851c 65 6e 74 20 63 61 6c 6c 20 74 6f 20 62 61 6c 61 ent call to bala
1851d 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 20 0a 20 nce_nonroot() .
1851e 20 20 20 20 20 20 20 20 20 2a 2a 20 68 61 73 20 ** has
1851f 63 6f 6d 70 6c 65 74 65 64 2c 20 69 74 20 69 73 completed, it is
18520 20 73 61 66 65 20 74 6f 20 72 65 6c 65 61 73 65 safe to release
18521 20 74 68 65 20 70 53 70 61 63 65 20 62 75 66 66 the pSpace buff
18522 65 72 20 75 73 65 64 20 62 79 0a 20 20 20 20 20 er used by.
18523 20 20 20 20 20 2a 2a 20 74 68 65 20 70 72 65 76 ** the prev
18524 69 6f 75 73 20 63 61 6c 6c 2c 20 61 73 20 74 68 ious call, as th
18525 65 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 e overflow cell
18526 64 61 74 61 20 77 69 6c 6c 20 68 61 76 65 20 62 data will have b
18527 65 65 6e 20 0a 20 20 20 20 20 20 20 20 20 20 2a een . *
18528 2a 20 63 6f 70 69 65 64 20 65 69 74 68 65 72 20 * copied either
18529 69 6e 74 6f 20 74 68 65 20 62 6f 64 79 20 6f 66 into the body of
1852a 20 61 20 64 61 74 61 62 61 73 65 20 70 61 67 65 a database page
1852b 20 6f 72 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 or into the new
1852c 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 53 . ** pS
1852d 70 61 63 65 20 62 75 66 66 65 72 20 70 61 73 73 pace buffer pass
1852e 65 64 20 74 6f 20 74 68 65 20 6c 61 74 74 65 72 ed to the latter
1852f 20 63 61 6c 6c 20 74 6f 20 62 61 6c 61 6e 63 65 call to balance
18530 5f 6e 6f 6e 72 6f 6f 74 28 29 2e 0a 20 20 20 20 _nonroot()..
18531 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
18532 20 20 20 75 38 20 2a 70 53 70 61 63 65 20 3d 20 u8 *pSpace =
18533 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f sqlite3PageMallo
18534 63 28 70 43 75 72 2d 3e 70 42 74 2d 3e 70 61 67 c(pCur->pBt->pag
18535 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 eSize);.
18536 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f 6e rc = balance_n
18537 6f 6e 72 6f 6f 74 28 70 50 61 72 65 6e 74 2c 20 onroot(pParent,
18538 69 49 64 78 2c 20 70 53 70 61 63 65 2c 20 69 50 iIdx, pSpace, iP
18539 61 67 65 3d 3d 31 29 3b 0a 20 20 20 20 20 20 20 age==1);.
1853a 20 20 20 69 66 28 20 70 46 72 65 65 20 29 7b 0a if( pFree ){.
1853b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
1853c 66 20 70 46 72 65 65 20 69 73 20 6e 6f 74 20 4e f pFree is not N
1853d 55 4c 4c 2c 20 69 74 20 70 6f 69 6e 74 73 20 74 ULL, it points t
1853e 6f 20 74 68 65 20 70 53 70 61 63 65 20 62 75 66 o the pSpace buf
1853f 66 65 72 20 75 73 65 64 20 0a 20 20 20 20 20 20 fer used .
18540 20 20 20 20 20 20 2a 2a 20 62 79 20 61 20 70 72 ** by a pr
18541 65 76 69 6f 75 73 20 63 61 6c 6c 20 74 6f 20 62 evious call to b
18542 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 alance_nonroot()
18543 2e 20 49 74 73 20 63 6f 6e 74 65 6e 74 73 20 61 . Its contents a
18544 72 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a re. *
18545 2a 20 6e 6f 77 20 73 74 6f 72 65 64 20 65 69 74 * now stored eit
18546 68 65 72 20 6f 6e 20 72 65 61 6c 20 64 61 74 61 her on real data
18547 62 61 73 65 20 70 61 67 65 73 20 6f 72 20 77 69 base pages or wi
18548 74 68 69 6e 20 74 68 65 20 0a 20 20 20 20 20 20 thin the .
18549 20 20 20 20 20 20 2a 2a 20 6e 65 77 20 70 53 70 ** new pSp
1854a 61 63 65 20 62 75 66 66 65 72 2c 20 73 6f 20 69 ace buffer, so i
1854b 74 20 6d 61 79 20 62 65 20 73 61 66 65 6c 79 20 t may be safely
1854c 66 72 65 65 64 20 68 65 72 65 2e 20 2a 2f 0a 20 freed here. */.
1854d 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
1854e 65 33 50 61 67 65 46 72 65 65 28 70 46 72 65 65 e3PageFree(pFree
1854f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 0a );. }..
18550 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
18551 20 70 53 70 61 63 65 20 62 75 66 66 65 72 20 77 pSpace buffer w
18552 69 6c 6c 20 62 65 20 66 72 65 65 64 20 61 66 74 ill be freed aft
18553 65 72 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c er the next call
18554 20 74 6f 0a 20 20 20 20 20 20 20 20 20 20 2a 2a to. **
18555 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 balance_nonroot
18556 28 29 2c 20 6f 72 20 6a 75 73 74 20 62 65 66 6f (), or just befo
18557 72 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e re this function
18558 20 72 65 74 75 72 6e 73 2c 20 77 68 69 63 68 65 returns, whiche
18559 76 65 72 0a 20 20 20 20 20 20 20 20 20 20 2a 2a ver. **
1855a 20 63 6f 6d 65 73 20 66 69 72 73 74 2e 20 2a 2f comes first. */
1855b 0a 20 20 20 20 20 20 20 20 20 20 70 46 72 65 65 . pFree
1855c 20 3d 20 70 53 70 61 63 65 3b 0a 20 20 20 20 20 = pSpace;.
1855d 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 }. }..
1855e 20 20 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 pPage->nOver
1855f 66 6c 6f 77 20 3d 20 30 3b 0a 0a 20 20 20 20 20 flow = 0;..
18560 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 69 74 65 /* The next ite
18561 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 6f ration of the do
18562 2d 6c 6f 6f 70 20 62 61 6c 61 6e 63 65 73 20 74 -loop balances t
18563 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e 20 he parent page.
18564 2a 2f 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 */. release
18565 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 Page(pPage);.
18566 20 20 20 70 43 75 72 2d 3e 69 50 61 67 65 2d 2d pCur->iPage--
18567 3b 0a 20 20 20 20 7d 0a 20 20 7d 77 68 69 6c 65 ;. }. }while
18568 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
18569 29 3b 0a 0a 20 20 69 66 28 20 70 46 72 65 65 20 );.. if( pFree
1856a 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 ){. sqlite3Pa
1856b 67 65 46 72 65 65 28 70 46 72 65 65 29 3b 0a 20 geFree(pFree);.
1856c 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
1856d 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 }.../*.** Insert
1856e 20 61 20 6e 65 77 20 72 65 63 6f 72 64 20 69 6e a new record in
1856f 74 6f 20 74 68 65 20 42 54 72 65 65 2e 20 20 54 to the BTree. T
18570 68 65 20 6b 65 79 20 69 73 20 67 69 76 65 6e 20 he key is given
18571 62 79 20 28 70 4b 65 79 2c 6e 4b 65 79 29 0a 2a by (pKey,nKey).*
18572 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61 20 69 * and the data i
18573 73 20 67 69 76 65 6e 20 62 79 20 28 70 44 61 74 s given by (pDat
18574 61 2c 6e 44 61 74 61 29 2e 20 20 54 68 65 20 63 a,nData). The c
18575 75 72 73 6f 72 20 69 73 20 75 73 65 64 20 6f 6e ursor is used on
18576 6c 79 20 74 6f 0a 2a 2a 20 64 65 66 69 6e 65 20 ly to.** define
18577 77 68 61 74 20 74 61 62 6c 65 20 74 68 65 20 72 what table the r
18578 65 63 6f 72 64 20 73 68 6f 75 6c 64 20 62 65 20 ecord should be
18579 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 2e 20 20 inserted into.
1857a 54 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69 73 The cursor.** is
1857b 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 left pointing a
1857c 74 20 61 20 72 61 6e 64 6f 6d 20 6c 6f 63 61 74 t a random locat
1857d 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 ion..**.** For a
1857e 6e 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 2c 20 n INTKEY table,
1857f 6f 6e 6c 79 20 74 68 65 20 6e 4b 65 79 20 76 61 only the nKey va
18580 6c 75 65 20 6f 66 20 74 68 65 20 6b 65 79 20 69 lue of the key i
18581 73 20 75 73 65 64 2e 20 20 70 4b 65 79 20 69 73 s used. pKey is
18582 0a 2a 2a 20 69 67 6e 6f 72 65 64 2e 20 20 46 6f .** ignored. Fo
18583 72 20 61 20 5a 45 52 4f 44 41 54 41 20 74 61 62 r a ZERODATA tab
18584 6c 65 2c 20 74 68 65 20 70 44 61 74 61 20 61 6e le, the pData an
18585 64 20 6e 44 61 74 61 20 61 72 65 20 62 6f 74 68 d nData are both
18586 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 ignored..**.**
18587 49 66 20 74 68 65 20 73 65 65 6b 52 65 73 75 6c If the seekResul
18588 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e t parameter is n
18589 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 20 on-zero, then a
1858a 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 successful call
1858b 74 6f 0a 2a 2a 20 4d 6f 76 65 74 6f 55 6e 70 61 to.** MovetoUnpa
1858c 63 6b 65 64 28 29 20 74 6f 20 73 65 65 6b 20 63 cked() to seek c
1858d 75 72 73 6f 72 20 70 43 75 72 20 74 6f 20 28 70 ursor pCur to (p
1858e 4b 65 79 2c 20 6e 4b 65 79 29 20 68 61 73 20 61 Key, nKey) has a
1858f 6c 72 65 61 64 79 0a 2a 2a 20 62 65 65 6e 20 70 lready.** been p
18590 65 72 66 6f 72 6d 65 64 2e 20 73 65 65 6b 52 65 erformed. seekRe
18591 73 75 6c 74 20 69 73 20 74 68 65 20 73 65 61 72 sult is the sear
18592 63 68 20 72 65 73 75 6c 74 20 72 65 74 75 72 6e ch result return
18593 65 64 20 28 61 20 6e 65 67 61 74 69 76 65 0a 2a ed (a negative.*
18594 2a 20 6e 75 6d 62 65 72 20 69 66 20 70 43 75 72 * number if pCur
18595 20 70 6f 69 6e 74 73 20 61 74 20 61 6e 20 65 6e points at an en
18596 74 72 79 20 74 68 61 74 20 69 73 20 73 6d 61 6c try that is smal
18597 6c 65 72 20 74 68 61 6e 20 28 70 4b 65 79 2c 20 ler than (pKey,
18598 6e 4b 65 79 29 2c 20 6f 72 0a 2a 2a 20 61 20 70 nKey), or.** a p
18599 6f 73 69 74 69 76 65 20 76 61 6c 75 65 20 69 66 ositive value if
1859a 20 70 43 75 72 20 70 6f 69 6e 74 73 20 61 74 20 pCur points at
1859b 61 6e 20 65 74 72 79 20 74 68 61 74 20 69 73 20 an etry that is
1859c 6c 61 72 67 65 72 20 74 68 61 6e 20 0a 2a 2a 20 larger than .**
1859d 28 70 4b 65 79 2c 20 6e 4b 65 79 29 29 2e 20 0a (pKey, nKey)). .
1859e 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 65 65 **.** If the see
1859f 6b 52 65 73 75 6c 74 20 70 61 72 61 6d 65 74 65 kResult paramete
185a0 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 r is non-zero, t
185a1 68 65 6e 20 74 68 65 20 63 61 6c 6c 65 72 20 67 hen the caller g
185a2 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 0a 2a uarantees that.*
185a3 2a 20 63 75 72 73 6f 72 20 70 43 75 72 20 69 73 * cursor pCur is
185a4 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 74 68 65 pointing at the
185a5 20 65 78 69 73 74 69 6e 67 20 63 6f 70 79 20 6f existing copy o
185a6 66 20 61 20 72 6f 77 20 74 68 61 74 20 69 73 20 f a row that is
185a7 74 6f 20 62 65 0a 2a 2a 20 6f 76 65 72 77 72 69 to be.** overwri
185a8 74 74 65 6e 2e 20 20 49 66 20 74 68 65 20 73 65 tten. If the se
185a9 65 6b 52 65 73 75 6c 74 20 70 61 72 61 6d 65 74 ekResult paramet
185aa 65 72 20 69 73 20 30 2c 20 74 68 65 6e 20 63 75 er is 0, then cu
185ab 72 73 6f 72 20 70 43 75 72 20 6d 61 79 0a 2a 2a rsor pCur may.**
185ac 20 70 6f 69 6e 74 20 74 6f 20 61 6e 79 20 65 6e point to any en
185ad 74 72 79 20 6f 72 20 74 6f 20 6e 6f 20 65 6e 74 try or to no ent
185ae 72 79 20 61 74 20 61 6c 6c 20 61 6e 64 20 73 6f ry at all and so
185af 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 68 this function h
185b0 61 73 20 74 6f 20 73 65 65 6b 0a 2a 2a 20 74 68 as to seek.** th
185b1 65 20 63 75 72 73 6f 72 20 62 65 66 6f 72 65 20 e cursor before
185b2 74 68 65 20 6e 65 77 20 6b 65 79 20 63 61 6e 20 the new key can
185b3 62 65 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2f 0a be inserted..*/.
185b4 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
185b5 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 nt sqlite3BtreeI
185b6 6e 73 65 72 74 28 0a 20 20 42 74 43 75 72 73 6f nsert(. BtCurso
185b7 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 20 20 r *pCur,
185b8 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 73 65 72 /* Inser
185b9 74 20 64 61 74 61 20 69 6e 74 6f 20 74 68 65 20 t data into the
185ba 74 61 62 6c 65 20 6f 66 20 74 68 69 73 20 63 75 table of this cu
185bb 72 73 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 rsor */. const
185bc 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 36 34 20 void *pKey, i64
185bd 6e 4b 65 79 2c 20 20 20 20 2f 2a 20 54 68 65 20 nKey, /* The
185be 6b 65 79 20 6f 66 20 74 68 65 20 6e 65 77 20 72 key of the new r
185bf 65 63 6f 72 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 ecord */. const
185c0 20 76 6f 69 64 20 2a 70 44 61 74 61 2c 20 69 6e void *pData, in
185c1 74 20 6e 44 61 74 61 2c 20 20 2f 2a 20 54 68 65 t nData, /* The
185c2 20 64 61 74 61 20 6f 66 20 74 68 65 20 6e 65 77 data of the new
185c3 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 record */. int
185c4 20 6e 5a 65 72 6f 2c 20 20 20 20 20 20 20 20 20 nZero,
185c5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
185c6 75 6d 62 65 72 20 6f 66 20 65 78 74 72 61 20 30 umber of extra 0
185c7 20 62 79 74 65 73 20 74 6f 20 61 70 70 65 6e 64 bytes to append
185c8 20 74 6f 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e to data */. in
185c9 74 20 61 70 70 65 6e 64 42 69 61 73 2c 20 20 20 t appendBias,
185ca 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
185cb 54 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20 True if this is
185cc 6c 69 6b 65 6c 79 20 61 6e 20 61 70 70 65 6e 64 likely an append
185cd 20 2a 2f 0a 20 20 69 6e 74 20 73 65 65 6b 52 65 */. int seekRe
185ce 73 75 6c 74 20 20 20 20 20 20 20 20 20 20 20 20 sult
185cf 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f /* Result o
185d0 66 20 70 72 69 6f 72 20 4d 6f 76 65 74 6f 55 6e f prior MovetoUn
185d1 70 61 63 6b 65 64 28 29 20 63 61 6c 6c 20 2a 2f packed() call */
185d2 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 .){. int rc;.
185d3 69 6e 74 20 6c 6f 63 20 3d 20 73 65 65 6b 52 65 int loc = seekRe
185d4 73 75 6c 74 3b 20 20 20 20 20 20 20 20 20 20 2f sult; /
185d5 2a 20 2d 31 3a 20 62 65 66 6f 72 65 20 64 65 73 * -1: before des
185d6 69 72 65 64 20 6c 6f 63 61 74 69 6f 6e 20 20 2b ired location +
185d7 31 3a 20 61 66 74 65 72 20 2a 2f 0a 20 20 69 6e 1: after */. in
185d8 74 20 73 7a 4e 65 77 20 3d 20 30 3b 0a 20 20 69 t szNew = 0;. i
185d9 6e 74 20 69 64 78 3b 0a 20 20 4d 65 6d 50 61 67 nt idx;. MemPag
185da 65 20 2a 70 50 61 67 65 3b 0a 20 20 42 74 72 65 e *pPage;. Btre
185db 65 20 2a 70 20 3d 20 70 43 75 72 2d 3e 70 42 74 e *p = pCur->pBt
185dc 72 65 65 3b 0a 20 20 42 74 53 68 61 72 65 64 20 ree;. BtShared
185dd 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 *pBt = p->pBt;.
185de 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
185df 6f 6c 64 43 65 6c 6c 3b 0a 20 20 75 6e 73 69 67 oldCell;. unsig
185e0 6e 65 64 20 63 68 61 72 20 2a 6e 65 77 43 65 6c ned char *newCel
185e1 6c 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 70 43 l = 0;.. if( pC
185e2 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 ur->eState==CURS
185e3 4f 52 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 OR_FAULT ){.
185e4 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 73 6b assert( pCur->sk
185e5 69 70 4e 65 78 74 21 3d 53 51 4c 49 54 45 5f 4f ipNext!=SQLITE_O
185e6 4b 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 K );. return
185e7 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b 0a pCur->skipNext;.
185e8 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 63 }.. assert( c
185e9 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
185ea 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 pCur) );. asser
185eb 74 28 20 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 t( pCur->wrFlag
185ec 26 26 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 && pBt->inTransa
185ed 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 ction==TRANS_WRI
185ee 54 45 20 26 26 20 21 70 42 74 2d 3e 72 65 61 64 TE && !pBt->read
185ef 4f 6e 6c 79 20 29 3b 0a 20 20 61 73 73 65 72 74 Only );. assert
185f0 28 20 68 61 73 53 68 61 72 65 64 43 61 63 68 65 ( hasSharedCache
185f1 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 70 43 75 TableLock(p, pCu
185f2 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 r->pgnoRoot, pCu
185f3 72 2d 3e 70 4b 65 79 49 6e 66 6f 21 3d 30 2c 20 r->pKeyInfo!=0,
185f4 32 29 20 29 3b 0a 0a 20 20 2f 2a 20 41 73 73 65 2) );.. /* Asse
185f5 72 74 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c rt that the call
185f6 65 72 20 68 61 73 20 62 65 65 6e 20 63 6f 6e 73 er has been cons
185f7 69 73 74 65 6e 74 2e 20 49 66 20 74 68 69 73 20 istent. If this
185f8 63 75 72 73 6f 72 20 77 61 73 20 6f 70 65 6e 65 cursor was opene
185f9 64 0a 20 20 2a 2a 20 65 78 70 65 63 74 69 6e 67 d. ** expecting
185fa 20 61 6e 20 69 6e 64 65 78 20 62 2d 74 72 65 65 an index b-tree
185fb 2c 20 74 68 65 6e 20 74 68 65 20 63 61 6c 6c 65 , then the calle
185fc 72 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 73 65 r should be inse
185fd 72 74 69 6e 67 20 62 6c 6f 62 0a 20 20 2a 2a 20 rting blob. **
185fe 6b 65 79 73 20 77 69 74 68 20 6e 6f 20 61 73 73 keys with no ass
185ff 6f 63 69 61 74 65 64 20 64 61 74 61 2e 20 49 66 ociated data. If
18600 20 74 68 65 20 63 75 72 73 6f 72 20 77 61 73 20 the cursor was
18601 6f 70 65 6e 65 64 20 65 78 70 65 63 74 69 6e 67 opened expecting
18602 20 61 6e 0a 20 20 2a 2a 20 69 6e 74 6b 65 79 20 an. ** intkey
18603 74 61 62 6c 65 2c 20 74 68 65 20 63 61 6c 6c 65 table, the calle
18604 72 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 73 65 r should be inse
18605 72 74 69 6e 67 20 69 6e 74 65 67 65 72 20 6b 65 rting integer ke
18606 79 73 20 77 69 74 68 20 61 0a 20 20 2a 2a 20 62 ys with a. ** b
18607 6c 6f 62 20 6f 66 20 61 73 73 6f 63 69 61 74 65 lob of associate
18608 64 20 64 61 74 61 2e 20 20 2a 2f 0a 20 20 61 73 d data. */. as
18609 73 65 72 74 28 20 28 70 4b 65 79 3d 3d 30 29 3d sert( (pKey==0)=
1860a 3d 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f =(pCur->pKeyInfo
1860b 3d 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 ==0) );.. /* If
1860c 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e 73 65 this is an inse
1860d 72 74 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 20 rt into a table
1860e 62 2d 74 72 65 65 2c 20 69 6e 76 61 6c 69 64 61 b-tree, invalida
1860f 74 65 20 61 6e 79 20 69 6e 63 72 62 6c 6f 62 20 te any incrblob
18610 0a 20 20 2a 2a 20 63 75 72 73 6f 72 73 20 6f 70 . ** cursors op
18611 65 6e 20 6f 6e 20 74 68 65 20 72 6f 77 20 62 65 en on the row be
18612 69 6e 67 20 72 65 70 6c 61 63 65 64 20 28 61 73 ing replaced (as
18613 73 75 6d 69 6e 67 20 74 68 69 73 20 69 73 20 61 suming this is a
18614 20 72 65 70 6c 61 63 65 0a 20 20 2a 2a 20 6f 70 replace. ** op
18615 65 72 61 74 69 6f 6e 20 2d 20 69 66 20 69 74 20 eration - if it
18616 69 73 20 6e 6f 74 2c 20 74 68 65 20 66 6f 6c 6c is not, the foll
18617 6f 77 69 6e 67 20 69 73 20 61 20 6e 6f 2d 6f 70 owing is a no-op
18618 29 2e 20 20 2a 2f 0a 20 20 69 66 28 20 70 43 75 ). */. if( pCu
18619 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 20 29 r->pKeyInfo==0 )
1861a 7b 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 {. invalidate
1861b 49 6e 63 72 62 6c 6f 62 43 75 72 73 6f 72 73 28 IncrblobCursors(
1861c 70 2c 20 6e 4b 65 79 2c 20 30 29 3b 0a 20 20 7d p, nKey, 0);. }
1861d 0a 0a 20 20 2f 2a 20 53 61 76 65 20 74 68 65 20 .. /* Save the
1861e 70 6f 73 69 74 69 6f 6e 73 20 6f 66 20 61 6e 79 positions of any
1861f 20 6f 74 68 65 72 20 63 75 72 73 6f 72 73 20 6f other cursors o
18620 70 65 6e 20 6f 6e 20 74 68 69 73 20 74 61 62 6c pen on this tabl
18621 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20 e.. **. ** In
18622 73 6f 6d 65 20 63 61 73 65 73 2c 20 74 68 65 20 some cases, the
18623 63 61 6c 6c 20 74 6f 20 62 74 72 65 65 4d 6f 76 call to btreeMov
18624 65 74 6f 28 29 20 62 65 6c 6f 77 20 69 73 20 61 eto() below is a
18625 20 6e 6f 2d 6f 70 2e 20 46 6f 72 0a 20 20 2a 2a no-op. For. **
18626 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 20 69 example, when i
18627 6e 73 65 72 74 69 6e 67 20 64 61 74 61 20 69 6e nserting data in
18628 74 6f 20 61 20 74 61 62 6c 65 20 77 69 74 68 20 to a table with
18629 61 75 74 6f 2d 67 65 6e 65 72 61 74 65 64 20 69 auto-generated i
1862a 6e 74 65 67 65 72 0a 20 20 2a 2a 20 6b 65 79 73 nteger. ** keys
1862b 2c 20 74 68 65 20 56 44 42 45 20 6c 61 79 65 72 , the VDBE layer
1862c 20 69 6e 76 6f 6b 65 73 20 73 71 6c 69 74 65 33 invokes sqlite3
1862d 42 74 72 65 65 4c 61 73 74 28 29 20 74 6f 20 66 BtreeLast() to f
1862e 69 67 75 72 65 20 6f 75 74 20 74 68 65 20 0a 20 igure out the .
1862f 20 2a 2a 20 69 6e 74 65 67 65 72 20 6b 65 79 20 ** integer key
18630 74 6f 20 75 73 65 2e 20 49 74 20 74 68 65 6e 20 to use. It then
18631 63 61 6c 6c 73 20 74 68 69 73 20 66 75 6e 63 74 calls this funct
18632 69 6f 6e 20 74 6f 20 61 63 74 75 61 6c 6c 79 20 ion to actually
18633 69 6e 73 65 72 74 20 74 68 65 20 0a 20 20 2a 2a insert the . **
18634 20 64 61 74 61 20 69 6e 74 6f 20 74 68 65 20 69 data into the i
18635 6e 74 6b 65 79 20 42 2d 54 72 65 65 2e 20 49 6e ntkey B-Tree. In
18636 20 74 68 69 73 20 63 61 73 65 20 62 74 72 65 65 this case btree
18637 4d 6f 76 65 74 6f 28 29 20 72 65 63 6f 67 6e 69 Moveto() recogni
18638 7a 65 73 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 zes. ** that th
18639 65 20 63 75 72 73 6f 72 20 69 73 20 61 6c 72 65 e cursor is alre
1863a 61 64 79 20 77 68 65 72 65 20 69 74 20 6e 65 65 ady where it nee
1863b 64 73 20 74 6f 20 62 65 20 61 6e 64 20 72 65 74 ds to be and ret
1863c 75 72 6e 73 20 77 69 74 68 6f 75 74 0a 20 20 2a urns without. *
1863d 2a 20 64 6f 69 6e 67 20 61 6e 79 20 77 6f 72 6b * doing any work
1863e 2e 20 54 6f 20 61 76 6f 69 64 20 74 68 77 61 72 . To avoid thwar
1863f 74 69 6e 67 20 74 68 65 73 65 20 6f 70 74 69 6d ting these optim
18640 69 7a 61 74 69 6f 6e 73 2c 20 69 74 20 69 73 20 izations, it is
18641 69 6d 70 6f 72 74 61 6e 74 0a 20 20 2a 2a 20 6e important. ** n
18642 6f 74 20 74 6f 20 63 6c 65 61 72 20 74 68 65 20 ot to clear the
18643 63 75 72 73 6f 72 20 68 65 72 65 2e 0a 20 20 2a cursor here.. *
18644 2f 0a 20 20 72 63 20 3d 20 73 61 76 65 41 6c 6c /. rc = saveAll
18645 43 75 72 73 6f 72 73 28 70 42 74 2c 20 70 43 75 Cursors(pBt, pCu
18646 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 r->pgnoRoot, pCu
18647 72 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 r);. if( rc ) r
18648 65 74 75 72 6e 20 72 63 3b 0a 20 20 69 66 28 20 eturn rc;. if(
18649 21 6c 6f 63 20 29 7b 0a 20 20 20 20 72 63 20 3d !loc ){. rc =
1864a 20 62 74 72 65 65 4d 6f 76 65 74 6f 28 70 43 75 btreeMoveto(pCu
1864b 72 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20 61 r, pKey, nKey, a
1864c 70 70 65 6e 64 42 69 61 73 2c 20 26 6c 6f 63 29 ppendBias, &loc)
1864d 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 ;. if( rc ) r
1864e 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 eturn rc;. }.
1864f 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 assert( pCur->eS
18650 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c tate==CURSOR_VAL
18651 49 44 20 7c 7c 20 28 70 43 75 72 2d 3e 65 53 74 ID || (pCur->eSt
18652 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 ate==CURSOR_INVA
18653 4c 49 44 20 26 26 20 6c 6f 63 29 20 29 3b 0a 0a LID && loc) );..
18654 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e pPage = pCur->
18655 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
18656 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ge];. assert( p
18657 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20 Page->intKey ||
18658 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 61 73 73 nKey>=0 );. ass
18659 65 72 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 ert( pPage->leaf
1865a 20 7c 7c 20 21 70 50 61 67 65 2d 3e 69 6e 74 4b || !pPage->intK
1865b 65 79 20 29 3b 0a 0a 20 20 54 52 41 43 45 28 28 ey );.. TRACE((
1865c 22 49 4e 53 45 52 54 3a 20 74 61 62 6c 65 3d 25 "INSERT: table=%
1865d 64 20 6e 6b 65 79 3d 25 6c 6c 64 20 6e 64 61 74 d nkey=%lld ndat
1865e 61 3d 25 64 20 70 61 67 65 3d 25 64 20 25 73 5c a=%d page=%d %s\
1865f 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70 43 n",. pC
18660 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 6e 4b ur->pgnoRoot, nK
18661 65 79 2c 20 6e 44 61 74 61 2c 20 70 50 61 67 65 ey, nData, pPage
18662 2d 3e 70 67 6e 6f 2c 0a 20 20 20 20 20 20 20 20 ->pgno,.
18663 20 20 6c 6f 63 3d 3d 30 20 3f 20 22 6f 76 65 72 loc==0 ? "over
18664 77 72 69 74 65 22 20 3a 20 22 6e 65 77 20 65 6e write" : "new en
18665 74 72 79 22 29 29 3b 0a 20 20 61 73 73 65 72 74 try"));. assert
18666 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 ( pPage->isInit
18667 29 3b 0a 20 20 61 6c 6c 6f 63 61 74 65 54 65 6d );. allocateTem
18668 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 6e pSpace(pBt);. n
18669 65 77 43 65 6c 6c 20 3d 20 70 42 74 2d 3e 70 54 ewCell = pBt->pT
1866a 6d 70 53 70 61 63 65 3b 0a 20 20 69 66 28 20 6e mpSpace;. if( n
1866b 65 77 43 65 6c 6c 3d 3d 30 20 29 20 72 65 74 75 ewCell==0 ) retu
1866c 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
1866d 0a 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e 43 65 . rc = fillInCe
1866e 6c 6c 28 70 50 61 67 65 2c 20 6e 65 77 43 65 6c ll(pPage, newCel
1866f 6c 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20 70 l, pKey, nKey, p
18670 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 6e 5a 65 Data, nData, nZe
18671 72 6f 2c 20 26 73 7a 4e 65 77 29 3b 0a 20 20 69 ro, &szNew);. i
18672 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64 f( rc ) goto end
18673 5f 69 6e 73 65 72 74 3b 0a 20 20 61 73 73 65 72 _insert;. asser
18674 74 28 20 73 7a 4e 65 77 3d 3d 63 65 6c 6c 53 69 t( szNew==cellSi
18675 7a 65 50 74 72 28 70 50 61 67 65 2c 20 6e 65 77 zePtr(pPage, new
18676 43 65 6c 6c 29 20 29 3b 0a 20 20 61 73 73 65 72 Cell) );. asser
18677 74 28 20 73 7a 4e 65 77 3c 3d 4d 58 5f 43 45 4c t( szNew<=MX_CEL
18678 4c 5f 53 49 5a 45 28 70 42 74 29 20 29 3b 0a 20 L_SIZE(pBt) );.
18679 20 69 64 78 20 3d 20 70 43 75 72 2d 3e 61 69 49 idx = pCur->aiI
1867a 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b dx[pCur->iPage];
1867b 0a 20 20 69 66 28 20 6c 6f 63 3d 3d 30 20 29 7b . if( loc==0 ){
1867c 0a 20 20 20 20 75 31 36 20 73 7a 4f 6c 64 3b 0a . u16 szOld;.
1867d 20 20 20 20 61 73 73 65 72 74 28 20 69 64 78 3c assert( idx<
1867e 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a pPage->nCell );.
1867f 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
18680 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 PagerWrite(pPage
18681 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 ->pDbPage);.
18682 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 if( rc ){.
18683 67 6f 74 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b goto end_insert;
18684 0a 20 20 20 20 7d 0a 20 20 20 20 6f 6c 64 43 65 . }. oldCe
18685 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 ll = findCell(pP
18686 61 67 65 2c 20 69 64 78 29 3b 0a 20 20 20 20 69 age, idx);. i
18687 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 f( !pPage->leaf
18688 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 ){. memcpy(
18689 6e 65 77 43 65 6c 6c 2c 20 6f 6c 64 43 65 6c 6c newCell, oldCell
1868a 2c 20 34 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 , 4);. }.
1868b 73 7a 4f 6c 64 20 3d 20 63 65 6c 6c 53 69 7a 65 szOld = cellSize
1868c 50 74 72 28 70 50 61 67 65 2c 20 6f 6c 64 43 65 Ptr(pPage, oldCe
1868d 6c 6c 29 3b 0a 20 20 20 20 72 63 20 3d 20 63 6c ll);. rc = cl
1868e 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 6f earCell(pPage, o
1868f 6c 64 43 65 6c 6c 29 3b 0a 20 20 20 20 64 72 6f ldCell);. dro
18690 70 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 pCell(pPage, idx
18691 2c 20 73 7a 4f 6c 64 2c 20 26 72 63 29 3b 0a 20 , szOld, &rc);.
18692 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f if( rc ) goto
18693 20 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 20 7d end_insert;. }
18694 65 6c 73 65 20 69 66 28 20 6c 6f 63 3c 30 20 26 else if( loc<0 &
18695 26 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e 30 & pPage->nCell>0
18696 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
18697 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 pPage->leaf );.
18698 20 20 20 69 64 78 20 3d 20 2b 2b 70 43 75 72 2d idx = ++pCur-
18699 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
1869a 67 65 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ge];. }else{.
1869b 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
1869c 3e 6c 65 61 66 20 29 3b 0a 20 20 7d 0a 20 20 69 >leaf );. }. i
1869d 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 67 65 2c nsertCell(pPage,
1869e 20 69 64 78 2c 20 6e 65 77 43 65 6c 6c 2c 20 73 idx, newCell, s
1869f 7a 4e 65 77 2c 20 30 2c 20 30 2c 20 26 72 63 29 zNew, 0, 0, &rc)
186a0 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 3d ;. assert( rc!=
186a1 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 SQLITE_OK || pPa
186a2 67 65 2d 3e 6e 43 65 6c 6c 3e 30 20 7c 7c 20 70 ge->nCell>0 || p
186a3 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e Page->nOverflow>
186a4 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 0 );.. /* If no
186a5 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 error has occur
186a6 65 64 20 61 6e 64 20 70 50 61 67 65 20 68 61 73 ed and pPage has
186a7 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c an overflow cel
186a8 6c 2c 20 63 61 6c 6c 20 62 61 6c 61 6e 63 65 28 l, call balance(
186a9 29 20 0a 20 20 2a 2a 20 74 6f 20 72 65 64 69 73 ) . ** to redis
186aa 74 72 69 62 75 74 65 20 74 68 65 20 63 65 6c 6c tribute the cell
186ab 73 20 77 69 74 68 69 6e 20 74 68 65 20 74 72 65 s within the tre
186ac 65 2e 20 53 69 6e 63 65 20 62 61 6c 61 6e 63 65 e. Since balance
186ad 28 29 20 6d 61 79 20 6d 6f 76 65 0a 20 20 2a 2a () may move. **
186ae 20 74 68 65 20 63 75 72 73 6f 72 2c 20 7a 65 72 the cursor, zer
186af 6f 20 74 68 65 20 42 74 43 75 72 73 6f 72 2e 69 o the BtCursor.i
186b0 6e 66 6f 2e 6e 53 69 7a 65 20 61 6e 64 20 42 74 nfo.nSize and Bt
186b1 43 75 72 73 6f 72 2e 76 61 6c 69 64 4e 4b 65 79 Cursor.validNKey
186b2 0a 20 20 2a 2a 20 76 61 72 69 61 62 6c 65 73 2e . ** variables.
186b3 0a 20 20 2a 2a 0a 20 20 2a 2a 20 50 72 65 76 69 . **. ** Previ
186b4 6f 75 73 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 ous versions of
186b5 53 51 4c 69 74 65 20 63 61 6c 6c 65 64 20 6d 6f SQLite called mo
186b6 76 65 54 6f 52 6f 6f 74 28 29 20 74 6f 20 6d 6f veToRoot() to mo
186b7 76 65 20 74 68 65 20 63 75 72 73 6f 72 0a 20 20 ve the cursor.
186b8 2a 2a 20 62 61 63 6b 20 74 6f 20 74 68 65 20 72 ** back to the r
186b9 6f 6f 74 20 70 61 67 65 20 61 73 20 62 61 6c 61 oot page as bala
186ba 6e 63 65 28 29 20 75 73 65 64 20 74 6f 20 69 6e nce() used to in
186bb 76 61 6c 69 64 61 74 65 20 74 68 65 20 63 6f 6e validate the con
186bc 74 65 6e 74 73 0a 20 20 2a 2a 20 6f 66 20 42 74 tents. ** of Bt
186bd 43 75 72 73 6f 72 2e 61 70 50 61 67 65 5b 5d 20 Cursor.apPage[]
186be 61 6e 64 20 42 74 43 75 72 73 6f 72 2e 61 69 49 and BtCursor.aiI
186bf 64 78 5b 5d 2e 20 49 6e 73 74 65 61 64 20 6f 66 dx[]. Instead of
186c0 20 64 6f 69 6e 67 20 74 68 61 74 2c 0a 20 20 2a doing that,. *
186c1 2a 20 73 65 74 20 74 68 65 20 63 75 72 73 6f 72 * set the cursor
186c2 20 73 74 61 74 65 20 74 6f 20 22 69 6e 76 61 6c state to "inval
186c3 69 64 22 2e 20 54 68 69 73 20 6d 61 6b 65 73 20 id". This makes
186c4 63 6f 6d 6d 6f 6e 20 69 6e 73 65 72 74 20 6f 70 common insert op
186c5 65 72 61 74 69 6f 6e 73 0a 20 20 2a 2a 20 73 6c erations. ** sl
186c6 69 67 68 74 6c 79 20 66 61 73 74 65 72 2e 0a 20 ightly faster..
186c7 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 72 65 20 69 **. ** There i
186c8 73 20 61 20 73 75 62 74 6c 65 20 62 75 74 20 69 s a subtle but i
186c9 6d 70 6f 72 74 61 6e 74 20 6f 70 74 69 6d 69 7a mportant optimiz
186ca 61 74 69 6f 6e 20 68 65 72 65 20 74 6f 6f 2e 20 ation here too.
186cb 57 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 0a 20 When inserting.
186cc 20 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 72 65 63 ** multiple rec
186cd 6f 72 64 73 20 69 6e 74 6f 20 61 6e 20 69 6e 74 ords into an int
186ce 6b 65 79 20 62 2d 74 72 65 65 20 75 73 69 6e 67 key b-tree using
186cf 20 61 20 73 69 6e 67 6c 65 20 63 75 72 73 6f 72 a single cursor
186d0 20 28 61 73 20 63 61 6e 0a 20 20 2a 2a 20 68 61 (as can. ** ha
186d1 70 70 65 6e 20 77 68 69 6c 65 20 70 72 6f 63 65 ppen while proce
186d2 73 73 69 6e 67 20 61 6e 20 22 49 4e 53 45 52 54 ssing an "INSERT
186d3 20 49 4e 54 4f 20 2e 2e 2e 20 53 45 4c 45 43 54 INTO ... SELECT
186d4 22 20 73 74 61 74 65 6d 65 6e 74 29 2c 20 69 74 " statement), it
186d5 0a 20 20 2a 2a 20 69 73 20 61 64 76 61 6e 74 61 . ** is advanta
186d6 67 65 6f 75 73 20 74 6f 20 6c 65 61 76 65 20 74 geous to leave t
186d7 68 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 he cursor pointi
186d8 6e 67 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 ng to the last e
186d9 6e 74 72 79 20 69 6e 0a 20 20 2a 2a 20 74 68 65 ntry in. ** the
186da 20 62 2d 74 72 65 65 20 69 66 20 70 6f 73 73 69 b-tree if possi
186db 62 6c 65 2e 20 49 66 20 74 68 65 20 63 75 72 73 ble. If the curs
186dc 6f 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 or is left point
186dd 69 6e 67 20 74 6f 20 74 68 65 20 6c 61 73 74 0a ing to the last.
186de 20 20 2a 2a 20 65 6e 74 72 79 20 69 6e 20 74 68 ** entry in th
186df 65 20 74 61 62 6c 65 2c 20 61 6e 64 20 74 68 65 e table, and the
186e0 20 6e 65 78 74 20 72 6f 77 20 69 6e 73 65 72 74 next row insert
186e1 65 64 20 68 61 73 20 61 6e 20 69 6e 74 65 67 65 ed has an intege
186e2 72 20 6b 65 79 0a 20 20 2a 2a 20 6c 61 72 67 65 r key. ** large
186e3 72 20 74 68 61 6e 20 74 68 65 20 6c 61 72 67 65 r than the large
186e4 73 74 20 65 78 69 73 74 69 6e 67 20 6b 65 79 2c st existing key,
186e5 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 it is possible
186e6 74 6f 20 69 6e 73 65 72 74 20 74 68 65 0a 20 20 to insert the.
186e7 2a 2a 20 72 6f 77 20 77 69 74 68 6f 75 74 20 73 ** row without s
186e8 65 65 6b 69 6e 67 20 74 68 65 20 63 75 72 73 6f eeking the curso
186e9 72 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20 61 r. This can be a
186ea 20 62 69 67 20 70 65 72 66 6f 72 6d 61 6e 63 65 big performance
186eb 20 62 6f 6f 73 74 2e 0a 20 20 2a 2f 0a 20 20 70 boost.. */. p
186ec 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 Cur->info.nSize
186ed 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 76 61 6c = 0;. pCur->val
186ee 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 69 66 idNKey = 0;. if
186ef 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
186f0 26 26 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 && pPage->nOverf
186f1 6c 6f 77 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 low ){. rc =
186f2 62 61 6c 61 6e 63 65 28 70 43 75 72 29 3b 0a 0a balance(pCur);..
186f3 20 20 20 20 2f 2a 20 4d 75 73 74 20 6d 61 6b 65 /* Must make
186f4 20 73 75 72 65 20 6e 4f 76 65 72 66 6c 6f 77 20 sure nOverflow
186f5 69 73 20 72 65 73 65 74 20 74 6f 20 7a 65 72 6f is reset to zero
186f6 20 65 76 65 6e 20 69 66 20 74 68 65 20 62 61 6c even if the bal
186f7 61 6e 63 65 28 29 0a 20 20 20 20 2a 2a 20 66 61 ance(). ** fa
186f8 69 6c 73 2e 20 49 6e 74 65 72 6e 61 6c 20 64 61 ils. Internal da
186f9 74 61 20 73 74 72 75 63 74 75 72 65 20 63 6f 72 ta structure cor
186fa 72 75 70 74 69 6f 6e 20 77 69 6c 6c 20 72 65 73 ruption will res
186fb 75 6c 74 20 6f 74 68 65 72 77 69 73 65 2e 20 0a ult otherwise. .
186fc 20 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 73 65 74 ** Also, set
186fd 20 74 68 65 20 63 75 72 73 6f 72 20 73 74 61 74 the cursor stat
186fe 65 20 74 6f 20 69 6e 76 61 6c 69 64 2e 20 54 68 e to invalid. Th
186ff 69 73 20 73 74 6f 70 73 20 73 61 76 65 43 75 72 is stops saveCur
18700 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 0a 20 20 sorPosition().
18701 20 20 2a 2a 20 66 72 6f 6d 20 74 72 79 69 6e 67 ** from trying
18702 20 74 6f 20 73 61 76 65 20 74 68 65 20 63 75 72 to save the cur
18703 72 65 6e 74 20 70 6f 73 69 74 69 6f 6e 20 6f 66 rent position of
18704 20 74 68 65 20 63 75 72 73 6f 72 2e 20 20 2a 2f the cursor. */
18705 0a 20 20 20 20 70 43 75 72 2d 3e 61 70 50 61 67 . pCur->apPag
18706 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e e[pCur->iPage]->
18707 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 nOverflow = 0;.
18708 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 pCur->eState
18709 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 = CURSOR_INVALID
1870a 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
1870b 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
1870c 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 4f 76 65 72 r->iPage]->nOver
1870d 66 6c 6f 77 3d 3d 30 20 29 3b 0a 0a 65 6e 64 5f flow==0 );..end_
1870e 69 6e 73 65 72 74 3a 0a 20 20 72 65 74 75 72 6e insert:. return
1870f 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 rc;.}../*.** De
18710 6c 65 74 65 20 74 68 65 20 65 6e 74 72 79 20 74 lete the entry t
18711 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 69 hat the cursor i
18712 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 s pointing to.
18713 54 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69 73 The cursor.** is
18714 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 left pointing a
18715 74 20 61 20 61 72 62 69 74 72 61 72 79 20 6c 6f t a arbitrary lo
18716 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 cation..*/.SQLIT
18717 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
18718 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65 74 65 lite3BtreeDelete
18719 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 (BtCursor *pCur)
1871a 7b 0a 20 20 42 74 72 65 65 20 2a 70 20 3d 20 70 {. Btree *p = p
1871b 43 75 72 2d 3e 70 42 74 72 65 65 3b 0a 20 20 42 Cur->pBtree;. B
1871c 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
1871d 2d 3e 70 42 74 3b 20 20 20 20 20 20 20 20 20 20 ->pBt;
1871e 20 20 20 20 0a 20 20 69 6e 74 20 72 63 3b 20 20 . int rc;
1871f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18720 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
18721 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 eturn code */.
18722 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 20 MemPage *pPage;
18723 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18724 20 20 20 20 20 2f 2a 20 50 61 67 65 20 74 6f 20 /* Page to
18725 64 65 6c 65 74 65 20 63 65 6c 6c 20 66 72 6f 6d delete cell from
18726 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 */. unsigned c
18727 68 61 72 20 2a 70 43 65 6c 6c 3b 20 20 20 20 20 har *pCell;
18728 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f /* Po
18729 69 6e 74 65 72 20 74 6f 20 63 65 6c 6c 20 74 6f inter to cell to
1872a 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74 delete */. int
1872b 20 69 43 65 6c 6c 49 64 78 3b 20 20 20 20 20 20 iCellIdx;
1872c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1872d 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 65 /* Index of ce
1872e 6c 6c 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a ll to delete */.
1872f 20 20 69 6e 74 20 69 43 65 6c 6c 44 65 70 74 68 int iCellDepth
18730 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
18731 20 20 20 20 20 20 20 2f 2a 20 44 65 70 74 68 20 /* Depth
18732 6f 66 20 6e 6f 64 65 20 63 6f 6e 74 61 69 6e 69 of node containi
18733 6e 67 20 70 43 65 6c 6c 20 2a 2f 20 0a 0a 20 20 ng pCell */ ..
18734 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f assert( cursorHo
18735 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 ldsMutex(pCur) )
18736 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d ;. assert( pBt-
18737 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d >inTransaction==
18738 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 TRANS_WRITE );.
18739 20 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e 72 assert( !pBt->r
1873a 65 61 64 4f 6e 6c 79 20 29 3b 0a 20 20 61 73 73 eadOnly );. ass
1873b 65 72 74 28 20 70 43 75 72 2d 3e 77 72 46 6c 61 ert( pCur->wrFla
1873c 67 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 68 g );. assert( h
1873d 61 73 53 68 61 72 65 64 43 61 63 68 65 54 61 62 asSharedCacheTab
1873e 6c 65 4c 6f 63 6b 28 70 2c 20 70 43 75 72 2d 3e leLock(p, pCur->
1873f 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 2d 3e pgnoRoot, pCur->
18740 70 4b 65 79 49 6e 66 6f 21 3d 30 2c 20 32 29 20 pKeyInfo!=0, 2)
18741 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 68 61 );. assert( !ha
18742 73 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 sReadConflicts(p
18743 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 , pCur->pgnoRoot
18744 29 20 29 3b 0a 0a 20 20 69 66 28 20 4e 45 56 45 ) );.. if( NEVE
18745 52 28 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 R(pCur->aiIdx[pC
18746 75 72 2d 3e 69 50 61 67 65 5d 3e 3d 70 43 75 72 ur->iPage]>=pCur
18747 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
18748 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 29 20 0a 20 Page]->nCell) .
18749 20 20 7c 7c 20 4e 45 56 45 52 28 70 43 75 72 2d || NEVER(pCur-
1874a 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f >eState!=CURSOR_
1874b 56 41 4c 49 44 29 0a 20 20 29 7b 0a 20 20 20 20 VALID). ){.
1874c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 return SQLITE_ER
1874d 52 4f 52 3b 20 20 2f 2a 20 53 6f 6d 65 74 68 69 ROR; /* Somethi
1874e 6e 67 20 68 61 73 20 67 6f 6e 65 20 61 77 72 79 ng has gone awry
1874f 2e 20 2a 2f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 . */. }.. /* I
18750 66 20 74 68 69 73 20 69 73 20 61 20 64 65 6c 65 f this is a dele
18751 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 te operation to
18752 72 65 6d 6f 76 65 20 61 20 72 6f 77 20 66 72 6f remove a row fro
18753 6d 20 61 20 74 61 62 6c 65 20 62 2d 74 72 65 65 m a table b-tree
18754 2c 0a 20 20 2a 2a 20 69 6e 76 61 6c 69 64 61 74 ,. ** invalidat
18755 65 20 61 6e 79 20 69 6e 63 72 62 6c 6f 62 20 63 e any incrblob c
18756 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74 ursors open on t
18757 68 65 20 72 6f 77 20 62 65 69 6e 67 20 64 65 6c he row being del
18758 65 74 65 64 2e 20 20 2a 2f 0a 20 20 69 66 28 20 eted. */. if(
18759 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d pCur->pKeyInfo==
1875a 30 20 29 7b 0a 20 20 20 20 69 6e 76 61 6c 69 64 0 ){. invalid
1875b 61 74 65 49 6e 63 72 62 6c 6f 62 43 75 72 73 6f ateIncrblobCurso
1875c 72 73 28 70 2c 20 70 43 75 72 2d 3e 69 6e 66 6f rs(p, pCur->info
1875d 2e 6e 4b 65 79 2c 20 30 29 3b 0a 20 20 7d 0a 0a .nKey, 0);. }..
1875e 20 20 69 43 65 6c 6c 44 65 70 74 68 20 3d 20 70 iCellDepth = p
1875f 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 69 43 Cur->iPage;. iC
18760 65 6c 6c 49 64 78 20 3d 20 70 43 75 72 2d 3e 61 ellIdx = pCur->a
18761 69 49 64 78 5b 69 43 65 6c 6c 44 65 70 74 68 5d iIdx[iCellDepth]
18762 3b 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 ;. pPage = pCur
18763 2d 3e 61 70 50 61 67 65 5b 69 43 65 6c 6c 44 65 ->apPage[iCellDe
18764 70 74 68 5d 3b 0a 20 20 70 43 65 6c 6c 20 3d 20 pth];. pCell =
18765 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 findCell(pPage,
18766 69 43 65 6c 6c 49 64 78 29 3b 0a 0a 20 20 2f 2a iCellIdx);.. /*
18767 20 49 66 20 74 68 65 20 70 61 67 65 20 63 6f 6e If the page con
18768 74 61 69 6e 69 6e 67 20 74 68 65 20 65 6e 74 72 taining the entr
18769 79 20 74 6f 20 64 65 6c 65 74 65 20 69 73 20 6e y to delete is n
1876a 6f 74 20 61 20 6c 65 61 66 20 70 61 67 65 2c 20 ot a leaf page,
1876b 6d 6f 76 65 0a 20 20 2a 2a 20 74 68 65 20 63 75 move. ** the cu
1876c 72 73 6f 72 20 74 6f 20 74 68 65 20 6c 61 72 67 rsor to the larg
1876d 65 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 est entry in the
1876e 20 74 72 65 65 20 74 68 61 74 20 69 73 20 73 6d tree that is sm
1876f 61 6c 6c 65 72 20 74 68 61 6e 0a 20 20 2a 2a 20 aller than. **
18770 74 68 65 20 65 6e 74 72 79 20 62 65 69 6e 67 20 the entry being
18771 64 65 6c 65 74 65 64 2e 20 54 68 69 73 20 63 65 deleted. This ce
18772 6c 6c 20 77 69 6c 6c 20 72 65 70 6c 61 63 65 20 ll will replace
18773 74 68 65 20 63 65 6c 6c 20 62 65 69 6e 67 20 64 the cell being d
18774 65 6c 65 74 65 64 0a 20 20 2a 2a 20 66 72 6f 6d eleted. ** from
18775 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f the internal no
18776 64 65 2e 20 54 68 65 20 27 70 72 65 76 69 6f 75 de. The 'previou
18777 73 27 20 65 6e 74 72 79 20 69 73 20 75 73 65 64 s' entry is used
18778 20 66 6f 72 20 74 68 69 73 20 69 6e 73 74 65 61 for this instea
18779 64 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 27 6e d. ** of the 'n
1877a 65 78 74 27 20 65 6e 74 72 79 2c 20 61 73 20 74 ext' entry, as t
1877b 68 65 20 70 72 65 76 69 6f 75 73 20 65 6e 74 72 he previous entr
1877c 79 20 69 73 20 61 6c 77 61 79 73 20 61 20 70 61 y is always a pa
1877d 72 74 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 73 rt of the. ** s
1877e 75 62 2d 74 72 65 65 20 68 65 61 64 65 64 20 62 ub-tree headed b
1877f 79 20 74 68 65 20 63 68 69 6c 64 20 70 61 67 65 y the child page
18780 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 62 65 69 of the cell bei
18781 6e 67 20 64 65 6c 65 74 65 64 2e 20 54 68 69 73 ng deleted. This
18782 20 6d 61 6b 65 73 0a 20 20 2a 2a 20 62 61 6c 61 makes. ** bala
18783 6e 63 69 6e 67 20 74 68 65 20 74 72 65 65 20 66 ncing the tree f
18784 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 64 65 6c ollowing the del
18785 65 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 65 61 ete operation ea
18786 73 69 65 72 2e 20 20 2a 2f 0a 20 20 69 66 28 20 sier. */. if(
18787 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a !pPage->leaf ){.
18788 20 20 20 20 69 6e 74 20 6e 6f 74 55 73 65 64 3b int notUsed;
18789 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1878a 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 70 3BtreePrevious(p
1878b 43 75 72 2c 20 26 6e 6f 74 55 73 65 64 29 3b 0a Cur, ¬Used);.
1878c 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 if( rc ) ret
1878d 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f urn rc;. }.. /
1878e 2a 20 53 61 76 65 20 74 68 65 20 70 6f 73 69 74 * Save the posit
1878f 69 6f 6e 73 20 6f 66 20 61 6e 79 20 6f 74 68 65 ions of any othe
18790 72 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f r cursors open o
18791 6e 20 74 68 69 73 20 74 61 62 6c 65 20 62 65 66 n this table bef
18792 6f 72 65 0a 20 20 2a 2a 20 6d 61 6b 69 6e 67 20 ore. ** making
18793 61 6e 79 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e any modification
18794 73 2e 20 4d 61 6b 65 20 74 68 65 20 70 61 67 65 s. Make the page
18795 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
18796 65 6e 74 72 79 20 74 6f 20 62 65 20 0a 20 20 2a entry to be . *
18797 2a 20 64 65 6c 65 74 65 64 20 77 72 69 74 61 62 * deleted writab
18798 6c 65 2e 20 54 68 65 6e 20 66 72 65 65 20 61 6e le. Then free an
18799 79 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 y overflow pages
1879a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
1879b 20 74 68 65 20 0a 20 20 2a 2a 20 65 6e 74 72 79 the . ** entry
1879c 20 61 6e 64 20 66 69 6e 61 6c 6c 79 20 72 65 6d and finally rem
1879d 6f 76 65 20 74 68 65 20 63 65 6c 6c 20 69 74 73 ove the cell its
1879e 65 6c 66 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 elf from within
1879f 74 68 65 20 70 61 67 65 2e 20 20 0a 20 20 2a 2f the page. . */
187a0 0a 20 20 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 . rc = saveAllC
187a1 75 72 73 6f 72 73 28 70 42 74 2c 20 70 43 75 72 ursors(pBt, pCur
187a2 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 ->pgnoRoot, pCur
187a3 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 );. if( rc ) re
187a4 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20 turn rc;. rc =
187a5 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
187a6 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 e(pPage->pDbPage
187a7 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 );. if( rc ) re
187a8 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20 turn rc;. rc =
187a9 63 6c 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c clearCell(pPage,
187aa 20 70 43 65 6c 6c 29 3b 0a 20 20 64 72 6f 70 43 pCell);. dropC
187ab 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c ell(pPage, iCell
187ac 49 64 78 2c 20 63 65 6c 6c 53 69 7a 65 50 74 72 Idx, cellSizePtr
187ad 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 2c 20 (pPage, pCell),
187ae 26 72 63 29 3b 0a 20 20 69 66 28 20 72 63 20 29 &rc);. if( rc )
187af 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f return rc;.. /
187b0 2a 20 49 66 20 74 68 65 20 63 65 6c 6c 20 64 65 * If the cell de
187b1 6c 65 74 65 64 20 77 61 73 20 6e 6f 74 20 6c 6f leted was not lo
187b2 63 61 74 65 64 20 6f 6e 20 61 20 6c 65 61 66 20 cated on a leaf
187b3 70 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20 63 page, then the c
187b4 75 72 73 6f 72 0a 20 20 2a 2a 20 69 73 20 63 75 ursor. ** is cu
187b5 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 rrently pointing
187b6 20 74 6f 20 74 68 65 20 6c 61 72 67 65 73 74 20 to the largest
187b7 65 6e 74 72 79 20 69 6e 20 74 68 65 20 73 75 62 entry in the sub
187b8 2d 74 72 65 65 20 68 65 61 64 65 64 0a 20 20 2a -tree headed. *
187b9 2a 20 62 79 20 74 68 65 20 63 68 69 6c 64 2d 70 * by the child-p
187ba 61 67 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 age of the cell
187bb 74 68 61 74 20 77 61 73 20 6a 75 73 74 20 64 65 that was just de
187bc 6c 65 74 65 64 20 66 72 6f 6d 20 61 6e 20 69 6e leted from an in
187bd 74 65 72 6e 61 6c 0a 20 20 2a 2a 20 6e 6f 64 65 ternal. ** node
187be 2e 20 54 68 65 20 63 65 6c 6c 20 66 72 6f 6d 20 . The cell from
187bf 74 68 65 20 6c 65 61 66 20 6e 6f 64 65 20 6e 65 the leaf node ne
187c0 65 64 73 20 74 6f 20 62 65 20 6d 6f 76 65 64 20 eds to be moved
187c1 74 6f 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a to the internal.
187c2 20 20 2a 2a 20 6e 6f 64 65 20 74 6f 20 72 65 70 ** node to rep
187c3 6c 61 63 65 20 74 68 65 20 64 65 6c 65 74 65 64 lace the deleted
187c4 20 63 65 6c 6c 2e 20 20 2a 2f 0a 20 20 69 66 28 cell. */. if(
187c5 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b !pPage->leaf ){
187c6 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4c . MemPage *pL
187c7 65 61 66 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 eaf = pCur->apPa
187c8 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b ge[pCur->iPage];
187c9 0a 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a . int nCell;.
187ca 20 20 20 20 50 67 6e 6f 20 6e 20 3d 20 70 43 75 Pgno n = pCu
187cb 72 2d 3e 61 70 50 61 67 65 5b 69 43 65 6c 6c 44 r->apPage[iCellD
187cc 65 70 74 68 2b 31 5d 2d 3e 70 67 6e 6f 3b 0a 20 epth+1]->pgno;.
187cd 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 unsigned char
187ce 20 2a 70 54 6d 70 3b 0a 0a 20 20 20 20 70 43 65 *pTmp;.. pCe
187cf 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 4c ll = findCell(pL
187d0 65 61 66 2c 20 70 4c 65 61 66 2d 3e 6e 43 65 6c eaf, pLeaf->nCel
187d1 6c 2d 31 29 3b 0a 20 20 20 20 6e 43 65 6c 6c 20 l-1);. nCell
187d2 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 4c = cellSizePtr(pL
187d3 65 61 66 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 eaf, pCell);.
187d4 20 61 73 73 65 72 74 28 20 4d 58 5f 43 45 4c 4c assert( MX_CELL
187d5 5f 53 49 5a 45 28 70 42 74 29 3e 3d 6e 43 65 6c _SIZE(pBt)>=nCel
187d6 6c 20 29 3b 0a 0a 20 20 20 20 61 6c 6c 6f 63 61 l );.. alloca
187d7 74 65 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 teTempSpace(pBt)
187d8 3b 0a 20 20 20 20 70 54 6d 70 20 3d 20 70 42 74 ;. pTmp = pBt
187d9 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 0a 20 20 ->pTmpSpace;..
187da 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
187db 67 65 72 57 72 69 74 65 28 70 4c 65 61 66 2d 3e gerWrite(pLeaf->
187dc 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 6e pDbPage);. in
187dd 73 65 72 74 43 65 6c 6c 28 70 50 61 67 65 2c 20 sertCell(pPage,
187de 69 43 65 6c 6c 49 64 78 2c 20 70 43 65 6c 6c 2d iCellIdx, pCell-
187df 34 2c 20 6e 43 65 6c 6c 2b 34 2c 20 70 54 6d 70 4, nCell+4, pTmp
187e0 2c 20 6e 2c 20 26 72 63 29 3b 0a 20 20 20 20 64 , n, &rc);. d
187e1 72 6f 70 43 65 6c 6c 28 70 4c 65 61 66 2c 20 70 ropCell(pLeaf, p
187e2 4c 65 61 66 2d 3e 6e 43 65 6c 6c 2d 31 2c 20 6e Leaf->nCell-1, n
187e3 43 65 6c 6c 2c 20 26 72 63 29 3b 0a 20 20 20 20 Cell, &rc);.
187e4 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
187e5 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 61 rc;. }.. /* Ba
187e6 6c 61 6e 63 65 20 74 68 65 20 74 72 65 65 2e 20 lance the tree.
187e7 49 66 20 74 68 65 20 65 6e 74 72 79 20 64 65 6c If the entry del
187e8 65 74 65 64 20 77 61 73 20 6c 6f 63 61 74 65 64 eted was located
187e9 20 6f 6e 20 61 20 6c 65 61 66 20 70 61 67 65 2c on a leaf page,
187ea 0a 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 . ** then the c
187eb 75 72 73 6f 72 20 73 74 69 6c 6c 20 70 6f 69 6e ursor still poin
187ec 74 73 20 74 6f 20 74 68 61 74 20 70 61 67 65 2e ts to that page.
187ed 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 In this case th
187ee 65 20 66 69 72 73 74 0a 20 20 2a 2a 20 63 61 6c e first. ** cal
187ef 6c 20 74 6f 20 62 61 6c 61 6e 63 65 28 29 20 72 l to balance() r
187f0 65 70 61 69 72 73 20 74 68 65 20 74 72 65 65 2c epairs the tree,
187f1 20 61 6e 64 20 74 68 65 20 69 66 28 2e 2e 2e 29 and the if(...)
187f2 20 63 6f 6e 64 69 74 69 6f 6e 20 69 73 0a 20 20 condition is.
187f3 2a 2a 20 6e 65 76 65 72 20 74 72 75 65 2e 0a 20 ** never true..
187f4 20 2a 2a 0a 20 20 2a 2a 20 4f 74 68 65 72 77 69 **. ** Otherwi
187f5 73 65 2c 20 69 66 20 74 68 65 20 65 6e 74 72 79 se, if the entry
187f6 20 64 65 6c 65 74 65 64 20 77 61 73 20 6f 6e 20 deleted was on
187f7 61 6e 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 an internal node
187f8 20 70 61 67 65 2c 20 74 68 65 6e 0a 20 20 2a 2a page, then. **
187f9 20 70 43 75 72 20 69 73 20 70 6f 69 6e 74 69 6e pCur is pointin
187fa 67 20 74 6f 20 74 68 65 20 6c 65 61 66 20 70 61 g to the leaf pa
187fb 67 65 20 66 72 6f 6d 20 77 68 69 63 68 20 61 20 ge from which a
187fc 63 65 6c 6c 20 77 61 73 20 72 65 6d 6f 76 65 64 cell was removed
187fd 20 74 6f 0a 20 20 2a 2a 20 72 65 70 6c 61 63 65 to. ** replace
187fe 20 74 68 65 20 63 65 6c 6c 20 64 65 6c 65 74 65 the cell delete
187ff 64 20 66 72 6f 6d 20 74 68 65 20 69 6e 74 65 72 d from the inter
18800 6e 61 6c 20 6e 6f 64 65 2e 20 54 68 69 73 20 69 nal node. This i
18801 73 20 73 6c 69 67 68 74 6c 79 0a 20 20 2a 2a 20 s slightly. **
18802 74 72 69 63 6b 79 20 61 73 20 74 68 65 20 6c 65 tricky as the le
18803 61 66 20 6e 6f 64 65 20 6d 61 79 20 62 65 20 75 af node may be u
18804 6e 64 65 72 66 75 6c 6c 2c 20 61 6e 64 20 74 68 nderfull, and th
18805 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 e internal node
18806 6d 61 79 0a 20 20 2a 2a 20 62 65 20 65 69 74 68 may. ** be eith
18807 65 72 20 75 6e 64 65 72 20 6f 72 20 6f 76 65 72 er under or over
18808 66 75 6c 6c 2e 20 49 6e 20 74 68 69 73 20 63 61 full. In this ca
18809 73 65 20 72 75 6e 20 74 68 65 20 62 61 6c 61 6e se run the balan
1880a 63 69 6e 67 20 61 6c 67 6f 72 69 74 68 6d 0a 20 cing algorithm.
1880b 20 2a 2a 20 6f 6e 20 74 68 65 20 6c 65 61 66 20 ** on the leaf
1880c 6e 6f 64 65 20 66 69 72 73 74 2e 20 49 66 20 74 node first. If t
1880d 68 65 20 62 61 6c 61 6e 63 65 20 70 72 6f 63 65 he balance proce
1880e 65 64 73 20 66 61 72 20 65 6e 6f 75 67 68 20 75 eds far enough u
1880f 70 20 74 68 65 0a 20 20 2a 2a 20 74 72 65 65 20 p the. ** tree
18810 74 68 61 74 20 77 65 20 63 61 6e 20 62 65 20 73 that we can be s
18811 75 72 65 20 74 68 61 74 20 61 6e 79 20 70 72 6f ure that any pro
18812 62 6c 65 6d 20 69 6e 20 74 68 65 20 69 6e 74 65 blem in the inte
18813 72 6e 61 6c 20 6e 6f 64 65 20 68 61 73 0a 20 20 rnal node has.
18814 2a 2a 20 62 65 65 6e 20 63 6f 72 72 65 63 74 65 ** been correcte
18815 64 2c 20 73 6f 20 62 65 20 69 74 2e 20 4f 74 68 d, so be it. Oth
18816 65 72 77 69 73 65 2c 20 61 66 74 65 72 20 62 61 erwise, after ba
18817 6c 61 6e 63 69 6e 67 20 74 68 65 20 6c 65 61 66 lancing the leaf
18818 20 6e 6f 64 65 2c 0a 20 20 2a 2a 20 77 61 6c 6b node,. ** walk
18819 20 74 68 65 20 63 75 72 73 6f 72 20 75 70 20 74 the cursor up t
1881a 68 65 20 74 72 65 65 20 74 6f 20 74 68 65 20 69 he tree to the i
1881b 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 61 6e 64 nternal node and
1881c 20 62 61 6c 61 6e 63 65 20 69 74 20 61 73 20 0a balance it as .
1881d 20 20 2a 2a 20 77 65 6c 6c 2e 20 20 2a 2f 0a 20 ** well. */.
1881e 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 rc = balance(pC
1881f 75 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 ur);. if( rc==S
18820 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 43 75 72 QLITE_OK && pCur
18821 2d 3e 69 50 61 67 65 3e 69 43 65 6c 6c 44 65 70 ->iPage>iCellDep
18822 74 68 20 29 7b 0a 20 20 20 20 77 68 69 6c 65 28 th ){. while(
18823 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 69 43 65 pCur->iPage>iCe
18824 6c 6c 44 65 70 74 68 20 29 7b 0a 20 20 20 20 20 llDepth ){.
18825 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 75 releasePage(pCu
18826 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
18827 69 50 61 67 65 2d 2d 5d 29 3b 0a 20 20 20 20 7d iPage--]);. }
18828 0a 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 . rc = balanc
18829 65 28 70 43 75 72 29 3b 0a 20 20 7d 0a 0a 20 20 e(pCur);. }..
1882a 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1882b 4b 20 29 7b 0a 20 20 20 20 6d 6f 76 65 54 6f 52 K ){. moveToR
1882c 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 7d 0a 20 oot(pCur);. }.
1882d 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1882e 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 *.** Create a ne
1882f 77 20 42 54 72 65 65 20 74 61 62 6c 65 2e 20 20 w BTree table.
18830 57 72 69 74 65 20 69 6e 74 6f 20 2a 70 69 54 61 Write into *piTa
18831 62 6c 65 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 ble the page.**
18832 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 72 number for the r
18833 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 oot page of the
18834 6e 65 77 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a new table..**.**
18835 20 54 68 65 20 74 79 70 65 20 6f 66 20 74 79 70 The type of typ
18836 65 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 e is determined
18837 62 79 20 74 68 65 20 66 6c 61 67 73 20 70 61 72 by the flags par
18838 61 6d 65 74 65 72 2e 20 20 4f 6e 6c 79 20 74 68 ameter. Only th
18839 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 e.** following v
1883a 61 6c 75 65 73 20 6f 66 20 66 6c 61 67 73 20 61 alues of flags a
1883b 72 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 re currently in
1883c 75 73 65 2e 20 20 4f 74 68 65 72 20 76 61 6c 75 use. Other valu
1883d 65 73 20 66 6f 72 0a 2a 2a 20 66 6c 61 67 73 20 es for.** flags
1883e 6d 69 67 68 74 20 6e 6f 74 20 77 6f 72 6b 3a 0a might not work:.
1883f 2a 2a 0a 2a 2a 20 20 20 20 20 42 54 52 45 45 5f **.** BTREE_
18840 49 4e 54 4b 45 59 7c 42 54 52 45 45 5f 4c 45 41 INTKEY|BTREE_LEA
18841 46 44 41 54 41 20 20 20 20 20 55 73 65 64 20 66 FDATA Used f
18842 6f 72 20 53 51 4c 20 74 61 62 6c 65 73 20 77 69 or SQL tables wi
18843 74 68 20 72 6f 77 69 64 20 6b 65 79 73 0a 2a 2a th rowid keys.**
18844 20 20 20 20 20 42 54 52 45 45 5f 5a 45 52 4f 44 BTREE_ZEROD
18845 41 54 41 20 20 20 20 20 20 20 20 20 20 20 20 20 ATA
18846 20 20 20 20 20 55 73 65 64 20 66 6f 72 20 53 51 Used for SQ
18847 4c 20 69 6e 64 69 63 65 73 0a 2a 2f 0a 73 74 61 L indices.*/.sta
18848 74 69 63 20 69 6e 74 20 62 74 72 65 65 43 72 65 tic int btreeCre
18849 61 74 65 54 61 62 6c 65 28 42 74 72 65 65 20 2a ateTable(Btree *
1884a 70 2c 20 69 6e 74 20 2a 70 69 54 61 62 6c 65 2c p, int *piTable,
1884b 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 42 int flags){. B
1884c 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
1884d 2d 3e 70 42 74 3b 0a 20 20 4d 65 6d 50 61 67 65 ->pBt;. MemPage
1884e 20 2a 70 52 6f 6f 74 3b 0a 20 20 50 67 6e 6f 20 *pRoot;. Pgno
1884f 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20 69 6e 74 20 pgnoRoot;. int
18850 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 rc;.. assert( s
18851 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 qlite3BtreeHolds
18852 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 Mutex(p) );. as
18853 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 sert( pBt->inTra
18854 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f nsaction==TRANS_
18855 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65 72 WRITE );. asser
18856 74 28 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e 6c t( !pBt->readOnl
18857 79 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c y );..#ifdef SQL
18858 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
18859 55 55 4d 0a 20 20 72 63 20 3d 20 61 6c 6c 6f 63 UUM. rc = alloc
1885a 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 ateBtreePage(pBt
1885b 2c 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f 52 , &pRoot, &pgnoR
1885c 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 20 69 66 oot, 1, 0);. if
1885d 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75 ( rc ){. retu
1885e 72 6e 20 72 63 3b 0a 20 20 7d 0a 23 65 6c 73 65 rn rc;. }.#else
1885f 0a 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f . if( pBt->auto
18860 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 50 67 Vacuum ){. Pg
18861 6e 6f 20 70 67 6e 6f 4d 6f 76 65 3b 20 20 20 20 no pgnoMove;
18862 20 20 2f 2a 20 4d 6f 76 65 20 61 20 70 61 67 65 /* Move a page
18863 20 68 65 72 65 20 74 6f 20 6d 61 6b 65 20 72 6f here to make ro
18864 6f 6d 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 2d om for the root-
18865 70 61 67 65 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 page */. MemP
18866 61 67 65 20 2a 70 50 61 67 65 4d 6f 76 65 3b 20 age *pPageMove;
18867 2f 2a 20 54 68 65 20 70 61 67 65 20 74 6f 20 6d /* The page to m
18868 6f 76 65 20 74 6f 2e 20 2a 2f 0a 0a 20 20 20 20 ove to. */..
18869 2f 2a 20 43 72 65 61 74 69 6e 67 20 61 20 6e 65 /* Creating a ne
1886a 77 20 74 61 62 6c 65 20 6d 61 79 20 70 72 6f 62 w table may prob
1886b 61 62 6c 79 20 72 65 71 75 69 72 65 20 6d 6f 76 ably require mov
1886c 69 6e 67 20 61 6e 20 65 78 69 73 74 69 6e 67 20 ing an existing
1886d 64 61 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 database. **
1886e 74 6f 20 6d 61 6b 65 20 72 6f 6f 6d 20 66 6f 72 to make room for
1886f 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 73 20 the new tables
18870 72 6f 6f 74 20 70 61 67 65 2e 20 49 6e 20 63 61 root page. In ca
18871 73 65 20 74 68 69 73 20 70 61 67 65 20 74 75 72 se this page tur
18872 6e 73 0a 20 20 20 20 2a 2a 20 6f 75 74 20 74 6f ns. ** out to
18873 20 62 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 be an overflow
18874 70 61 67 65 2c 20 64 65 6c 65 74 65 20 61 6c 6c page, delete all
18875 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6d overflow page-m
18876 61 70 20 63 61 63 68 65 73 0a 20 20 20 20 2a 2a ap caches. **
18877 20 68 65 6c 64 20 62 79 20 6f 70 65 6e 20 63 75 held by open cu
18878 72 73 6f 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 rsors.. */.
18879 20 20 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f invalidateAllO
1887a 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 42 74 verflowCache(pBt
1887b 29 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 );.. /* Read
1887c 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6d 65 74 the value of met
1887d 61 5b 33 5d 20 66 72 6f 6d 20 74 68 65 20 64 61 a[3] from the da
1887e 74 61 62 61 73 65 20 74 6f 20 64 65 74 65 72 6d tabase to determ
1887f 69 6e 65 20 77 68 65 72 65 20 74 68 65 0a 20 20 ine where the.
18880 20 20 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 6f ** root page o
18881 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 f the new table
18882 73 68 6f 75 6c 64 20 67 6f 2e 20 6d 65 74 61 5b should go. meta[
18883 33 5d 20 69 73 20 74 68 65 20 6c 61 72 67 65 73 3] is the larges
18884 74 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 t root-page.
18885 2a 2a 20 63 72 65 61 74 65 64 20 73 6f 20 66 61 ** created so fa
18886 72 2c 20 73 6f 20 74 68 65 20 6e 65 77 20 72 6f r, so the new ro
18887 6f 74 2d 70 61 67 65 20 69 73 20 28 6d 65 74 61 ot-page is (meta
18888 5b 33 5d 2b 31 29 2e 0a 20 20 20 20 2a 2f 0a 20 [3]+1).. */.
18889 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 sqlite3BtreeG
1888a 65 74 4d 65 74 61 28 70 2c 20 42 54 52 45 45 5f etMeta(p, BTREE_
1888b 4c 41 52 47 45 53 54 5f 52 4f 4f 54 5f 50 41 47 LARGEST_ROOT_PAG
1888c 45 2c 20 26 70 67 6e 6f 52 6f 6f 74 29 3b 0a 20 E, &pgnoRoot);.
1888d 20 20 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a 0a pgnoRoot++;..
1888e 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 /* The new r
1888f 6f 6f 74 2d 70 61 67 65 20 6d 61 79 20 6e 6f 74 oot-page may not
18890 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 6f 6e be allocated on
18891 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 a pointer-map p
18892 61 67 65 2c 20 6f 72 20 74 68 65 0a 20 20 20 20 age, or the.
18893 2a 2a 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 ** PENDING_BYTE
18894 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 page.. */.
18895 20 77 68 69 6c 65 28 20 70 67 6e 6f 52 6f 6f 74 while( pgnoRoot
18896 3d 3d 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 ==PTRMAP_PAGENO(
18897 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 29 20 7c pBt, pgnoRoot) |
18898 7c 0a 20 20 20 20 20 20 20 20 70 67 6e 6f 52 6f |. pgnoRo
18899 6f 74 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 ot==PENDING_BYTE
1889a 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 _PAGE(pBt) ){.
1889b 20 20 20 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a pgnoRoot++;.
1889c 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 }. assert
1889d 28 20 70 67 6e 6f 52 6f 6f 74 3e 3d 33 20 29 3b ( pgnoRoot>=3 );
1889e 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 .. /* Allocat
1889f 65 20 61 20 70 61 67 65 2e 20 54 68 65 20 70 61 e a page. The pa
188a0 67 65 20 74 68 61 74 20 63 75 72 72 65 6e 74 6c ge that currentl
188a1 79 20 72 65 73 69 64 65 73 20 61 74 20 70 67 6e y resides at pgn
188a2 6f 52 6f 6f 74 20 77 69 6c 6c 0a 20 20 20 20 2a oRoot will. *
188a3 2a 20 62 65 20 6d 6f 76 65 64 20 74 6f 20 74 68 * be moved to th
188a4 65 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 e allocated page
188a5 20 28 75 6e 6c 65 73 73 20 74 68 65 20 61 6c 6c (unless the all
188a6 6f 63 61 74 65 64 20 70 61 67 65 20 68 61 70 70 ocated page happ
188a7 65 6e 73 0a 20 20 20 20 2a 2a 20 74 6f 20 72 65 ens. ** to re
188a8 73 69 64 65 20 61 74 20 70 67 6e 6f 52 6f 6f 74 side at pgnoRoot
188a9 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 ).. */. rc
188aa 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 = allocateBtree
188ab 50 61 67 65 28 70 42 74 2c 20 26 70 50 61 67 65 Page(pBt, &pPage
188ac 4d 6f 76 65 2c 20 26 70 67 6e 6f 4d 6f 76 65 2c Move, &pgnoMove,
188ad 20 70 67 6e 6f 52 6f 6f 74 2c 20 31 29 3b 0a 20 pgnoRoot, 1);.
188ae 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
188af 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 E_OK ){. re
188b0 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a turn rc;. }..
188b1 20 20 20 20 69 66 28 20 70 67 6e 6f 4d 6f 76 65 if( pgnoMove
188b2 21 3d 70 67 6e 6f 52 6f 6f 74 20 29 7b 0a 20 20 !=pgnoRoot ){.
188b3 20 20 20 20 2f 2a 20 70 67 6e 6f 52 6f 6f 74 20 /* pgnoRoot
188b4 69 73 20 74 68 65 20 70 61 67 65 20 74 68 61 74 is the page that
188b5 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 66 6f will be used fo
188b6 72 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 r the root-page
188b7 6f 66 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 of. ** the
188b8 6e 65 77 20 74 61 62 6c 65 20 28 61 73 73 75 6d new table (assum
188b9 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 64 69 64 ing an error did
188ba 20 6e 6f 74 20 6f 63 63 75 72 29 2e 20 42 75 74 not occur). But
188bb 20 77 65 20 77 65 72 65 0a 20 20 20 20 20 20 2a we were. *
188bc 2a 20 61 6c 6c 6f 63 61 74 65 64 20 70 67 6e 6f * allocated pgno
188bd 4d 6f 76 65 2e 20 49 66 20 72 65 71 75 69 72 65 Move. If require
188be 64 20 28 69 2e 65 2e 20 69 66 20 69 74 20 77 61 d (i.e. if it wa
188bf 73 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 64 0a s not allocated.
188c0 20 20 20 20 20 20 2a 2a 20 62 79 20 65 78 74 65 ** by exte
188c1 6e 64 69 6e 67 20 74 68 65 20 66 69 6c 65 29 2c nding the file),
188c2 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 61 67 the current pag
188c3 65 20 61 74 20 70 6f 73 69 74 69 6f 6e 20 70 67 e at position pg
188c4 6e 6f 4d 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20 noMove. **
188c5 69 73 20 61 6c 72 65 61 64 79 20 6a 6f 75 72 6e is already journ
188c6 61 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 aled.. */.
188c7 20 20 20 20 20 75 38 20 65 54 79 70 65 20 3d 20 u8 eType =
188c8 30 3b 0a 20 20 20 20 20 20 50 67 6e 6f 20 69 50 0;. Pgno iP
188c9 74 72 50 61 67 65 20 3d 20 30 3b 0a 0a 20 20 20 trPage = 0;..
188ca 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
188cb 50 61 67 65 4d 6f 76 65 29 3b 0a 0a 20 20 20 20 PageMove);..
188cc 20 20 2f 2a 20 4d 6f 76 65 20 74 68 65 20 70 61 /* Move the pa
188cd 67 65 20 63 75 72 72 65 6e 74 6c 79 20 61 74 20 ge currently at
188ce 70 67 6e 6f 52 6f 6f 74 20 74 6f 20 70 67 6e 6f pgnoRoot to pgno
188cf 4d 6f 76 65 2e 20 2a 2f 0a 20 20 20 20 20 20 72 Move. */. r
188d0 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 c = btreeGetPage
188d1 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 (pBt, pgnoRoot,
188d2 26 70 52 6f 6f 74 2c 20 30 29 3b 0a 20 20 20 20 &pRoot, 0);.
188d3 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
188d4 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 _OK ){. r
188d5 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
188d6 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 }. rc = ptr
188d7 6d 61 70 47 65 74 28 70 42 74 2c 20 70 67 6e 6f mapGet(pBt, pgno
188d8 52 6f 6f 74 2c 20 26 65 54 79 70 65 2c 20 26 69 Root, &eType, &i
188d9 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20 20 20 PtrPage);.
188da 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 if( eType==PTRMA
188db 50 5f 52 4f 4f 54 50 41 47 45 20 7c 7c 20 65 54 P_ROOTPAGE || eT
188dc 79 70 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45 45 ype==PTRMAP_FREE
188dd 50 41 47 45 20 29 7b 0a 20 20 20 20 20 20 20 20 PAGE ){.
188de 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 rc = SQLITE_CORR
188df 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 UPT_BKPT;.
188e0 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d }. if( rc!=
188e1 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
188e2 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
188e3 28 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20 20 20 (pRoot);.
188e4 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
188e5 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 }. assert
188e6 28 20 65 54 79 70 65 21 3d 50 54 52 4d 41 50 5f ( eType!=PTRMAP_
188e7 52 4f 4f 54 50 41 47 45 20 29 3b 0a 20 20 20 20 ROOTPAGE );.
188e8 20 20 61 73 73 65 72 74 28 20 65 54 79 70 65 21 assert( eType!
188e9 3d 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 =PTRMAP_FREEPAGE
188ea 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 72 );. rc = r
188eb 65 6c 6f 63 61 74 65 50 61 67 65 28 70 42 74 2c elocatePage(pBt,
188ec 20 70 52 6f 6f 74 2c 20 65 54 79 70 65 2c 20 69 pRoot, eType, i
188ed 50 74 72 50 61 67 65 2c 20 70 67 6e 6f 4d 6f 76 PtrPage, pgnoMov
188ee 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 65 6c e, 0);. rel
188ef 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b easePage(pRoot);
188f0 0a 0a 20 20 20 20 20 20 2f 2a 20 4f 62 74 61 69 .. /* Obtai
188f1 6e 20 74 68 65 20 70 61 67 65 20 61 74 20 70 67 n the page at pg
188f2 6e 6f 52 6f 6f 74 20 2a 2f 0a 20 20 20 20 20 20 noRoot */.
188f3 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
188f4 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 K ){. ret
188f5 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a urn rc;. }.
188f6 20 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 rc = btree
188f7 47 65 74 50 61 67 65 28 70 42 74 2c 20 70 67 6e GetPage(pBt, pgn
188f8 6f 52 6f 6f 74 2c 20 26 70 52 6f 6f 74 2c 20 30 oRoot, &pRoot, 0
188f9 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 );. if( rc!
188fa 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
188fb 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
188fc 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 . }. r
188fd 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
188fe 57 72 69 74 65 28 70 52 6f 6f 74 2d 3e 70 44 62 Write(pRoot->pDb
188ff 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 Page);. if(
18900 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
18901 7b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 {. releas
18902 65 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a 20 20 ePage(pRoot);.
18903 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
18904 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c . }. }el
18905 73 65 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74 20 se{. pRoot
18906 3d 20 70 50 61 67 65 4d 6f 76 65 3b 0a 20 20 20 = pPageMove;.
18907 20 7d 20 0a 0a 20 20 20 20 2f 2a 20 55 70 64 61 } .. /* Upda
18908 74 65 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d te the pointer-m
18909 61 70 20 61 6e 64 20 6d 65 74 61 2d 64 61 74 61 ap and meta-data
1890a 20 77 69 74 68 20 74 68 65 20 6e 65 77 20 72 6f with the new ro
1890b 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 ot-page number.
1890c 2a 2f 0a 20 20 20 20 70 74 72 6d 61 70 50 75 74 */. ptrmapPut
1890d 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 (pBt, pgnoRoot,
1890e 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 2c PTRMAP_ROOTPAGE,
1890f 20 30 2c 20 26 72 63 29 3b 0a 20 20 20 20 69 66 0, &rc);. if
18910 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 ( rc ){. re
18911 6c 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 leasePage(pRoot)
18912 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 ;. return r
18913 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 c;. }. rc
18914 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 = sqlite3BtreeUp
18915 64 61 74 65 4d 65 74 61 28 70 2c 20 34 2c 20 70 dateMeta(p, 4, p
18916 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 20 69 66 gnoRoot);. if
18917 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 ( rc ){. re
18918 6c 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 leasePage(pRoot)
18919 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 ;. return r
1891a 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 7d 65 6c 73 c;. }.. }els
1891b 65 7b 0a 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f e{. rc = allo
1891c 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 cateBtreePage(pB
1891d 74 2c 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f t, &pRoot, &pgno
1891e 52 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 20 20 Root, 1, 0);.
1891f 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
18920 20 72 63 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a rc;. }.#endif.
18921 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
18922 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 3PagerIswriteabl
18923 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 e(pRoot->pDbPage
18924 29 20 29 3b 0a 20 20 7a 65 72 6f 50 61 67 65 28 ) );. zeroPage(
18925 70 52 6f 6f 74 2c 20 66 6c 61 67 73 20 7c 20 50 pRoot, flags | P
18926 54 46 5f 4c 45 41 46 29 3b 0a 20 20 73 71 6c 69 TF_LEAF);. sqli
18927 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 52 te3PagerUnref(pR
18928 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 oot->pDbPage);.
18929 20 2a 70 69 54 61 62 6c 65 20 3d 20 28 69 6e 74 *piTable = (int
1892a 29 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20 72 65 74 )pgnoRoot;. ret
1892b 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1892c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1892d 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
1892e 43 72 65 61 74 65 54 61 62 6c 65 28 42 74 72 65 CreateTable(Btre
1892f 65 20 2a 70 2c 20 69 6e 74 20 2a 70 69 54 61 62 e *p, int *piTab
18930 6c 65 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a le, int flags){.
18931 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 int rc;. sqli
18932 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 te3BtreeEnter(p)
18933 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 65 43 72 ;. rc = btreeCr
18934 65 61 74 65 54 61 62 6c 65 28 70 2c 20 70 69 54 eateTable(p, piT
18935 61 62 6c 65 2c 20 66 6c 61 67 73 29 3b 0a 20 20 able, flags);.
18936 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
18937 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 e(p);. return r
18938 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 72 61 73 c;.}../*.** Eras
18939 65 20 74 68 65 20 67 69 76 65 6e 20 64 61 74 61 e the given data
1893a 62 61 73 65 20 70 61 67 65 20 61 6e 64 20 61 6c base page and al
1893b 6c 20 69 74 73 20 63 68 69 6c 64 72 65 6e 2e 20 l its children.
1893c 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 70 Return.** the p
1893d 61 67 65 20 74 6f 20 74 68 65 20 66 72 65 65 6c age to the freel
1893e 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ist..*/.static i
1893f 6e 74 20 63 6c 65 61 72 44 61 74 61 62 61 73 65 nt clearDatabase
18940 50 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 Page(. BtShared
18941 20 2a 70 42 74 2c 20 20 20 20 20 20 20 20 20 20 *pBt,
18942 20 2f 2a 20 54 68 65 20 42 54 72 65 65 20 74 68 /* The BTree th
18943 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 at contains the
18944 74 61 62 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 table */. Pgno
18945 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20 pgno,
18946 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 /* Page number
18947 74 6f 20 63 6c 65 61 72 20 2a 2f 0a 20 20 69 6e to clear */. in
18948 74 20 66 72 65 65 50 61 67 65 46 6c 61 67 2c 20 t freePageFlag,
18949 20 20 20 20 2f 2a 20 44 65 61 6c 6c 6f 63 61 74 /* Deallocat
1894a 65 20 70 61 67 65 20 69 66 20 74 72 75 65 20 2a e page if true *
1894b 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 68 61 6e 67 /. int *pnChang
1894c 65 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a e.){. MemPage *
1894d 70 50 61 67 65 3b 0a 20 20 69 6e 74 20 72 63 3b pPage;. int rc;
1894e 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
1894f 20 2a 70 43 65 6c 6c 3b 0a 20 20 69 6e 74 20 69 *pCell;. int i
18950 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
18951 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
18952 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
18953 20 69 66 28 20 70 67 6e 6f 3e 70 61 67 65 72 50 if( pgno>pagerP
18954 61 67 65 63 6f 75 6e 74 28 70 42 74 29 20 29 7b agecount(pBt) ){
18955 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
18956 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
18957 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 67 65 74 . }.. rc = get
18958 41 6e 64 49 6e 69 74 50 61 67 65 28 70 42 74 2c AndInitPage(pBt,
18959 20 70 67 6e 6f 2c 20 26 70 50 61 67 65 29 3b 0a pgno, &pPage);.
1895a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 if( rc ) retur
1895b 6e 20 72 63 3b 0a 20 20 66 6f 72 28 69 3d 30 3b n rc;. for(i=0;
1895c 20 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b i<pPage->nCell;
1895d 20 69 2b 2b 29 7b 0a 20 20 20 20 70 43 65 6c 6c i++){. pCell
1895e 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 = findCell(pPag
1895f 65 2c 20 69 29 3b 0a 20 20 20 20 69 66 28 20 21 e, i);. if( !
18960 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 pPage->leaf ){.
18961 20 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 44 rc = clearD
18962 61 74 61 62 61 73 65 50 61 67 65 28 70 42 74 2c atabasePage(pBt,
18963 20 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c 29 get4byte(pCell)
18964 2c 20 31 2c 20 70 6e 43 68 61 6e 67 65 29 3b 0a , 1, pnChange);.
18965 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 67 if( rc ) g
18966 6f 74 6f 20 63 6c 65 61 72 64 61 74 61 62 61 73 oto cleardatabas
18967 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 7d epage_out;. }
18968 0a 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 43 . rc = clearC
18969 65 6c 6c 28 70 50 61 67 65 2c 20 70 43 65 6c 6c ell(pPage, pCell
1896a 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 );. if( rc )
1896b 67 6f 74 6f 20 63 6c 65 61 72 64 61 74 61 62 61 goto cleardataba
1896c 73 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a sepage_out;. }.
1896d 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 if( !pPage->le
1896e 61 66 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 63 af ){. rc = c
1896f 6c 65 61 72 44 61 74 61 62 61 73 65 50 61 67 65 learDatabasePage
18970 28 70 42 74 2c 20 67 65 74 34 62 79 74 65 28 26 (pBt, get4byte(&
18971 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 38 5d 29 pPage->aData[8])
18972 2c 20 31 2c 20 70 6e 43 68 61 6e 67 65 29 3b 0a , 1, pnChange);.
18973 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 if( rc ) got
18974 6f 20 63 6c 65 61 72 64 61 74 61 62 61 73 65 70 o cleardatabasep
18975 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 65 6c 73 65 age_out;. }else
18976 20 69 66 28 20 70 6e 43 68 61 6e 67 65 20 29 7b if( pnChange ){
18977 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
18978 67 65 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a 20 20 ge->intKey );.
18979 20 20 2a 70 6e 43 68 61 6e 67 65 20 2b 3d 20 70 *pnChange += p
1897a 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 7d Page->nCell;. }
1897b 0a 20 20 69 66 28 20 66 72 65 65 50 61 67 65 46 . if( freePageF
1897c 6c 61 67 20 29 7b 0a 20 20 20 20 66 72 65 65 50 lag ){. freeP
1897d 61 67 65 28 70 50 61 67 65 2c 20 26 72 63 29 3b age(pPage, &rc);
1897e 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 28 72 63 . }else if( (rc
1897f 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 = sqlite3PagerW
18980 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 rite(pPage->pDbP
18981 61 67 65 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 age))==0 ){.
18982 7a 65 72 6f 50 61 67 65 28 70 50 61 67 65 2c 20 zeroPage(pPage,
18983 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d 20 pPage->aData[0]
18984 7c 20 50 54 46 5f 4c 45 41 46 29 3b 0a 20 20 7d | PTF_LEAF);. }
18985 0a 0a 63 6c 65 61 72 64 61 74 61 62 61 73 65 70 ..cleardatabasep
18986 61 67 65 5f 6f 75 74 3a 0a 20 20 72 65 6c 65 61 age_out:. relea
18987 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 sePage(pPage);.
18988 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
18989 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6c 6c 20 *.** Delete all
1898a 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d information from
1898b 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 a single table
1898c 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e in the database.
1898d 20 20 69 54 61 62 6c 65 20 69 73 0a 2a 2a 20 74 iTable is.** t
1898e 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f he page number o
1898f 66 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 f the root of th
18990 65 20 74 61 62 6c 65 2e 20 20 41 66 74 65 72 20 e table. After
18991 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 this routine ret
18992 75 72 6e 73 2c 0a 2a 2a 20 74 68 65 20 72 6f 6f urns,.** the roo
18993 74 20 70 61 67 65 20 69 73 20 65 6d 70 74 79 2c t page is empty,
18994 20 62 75 74 20 73 74 69 6c 6c 20 65 78 69 73 74 but still exist
18995 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f s..**.** This ro
18996 75 74 69 6e 65 20 77 69 6c 6c 20 66 61 69 6c 20 utine will fail
18997 77 69 74 68 20 53 51 4c 49 54 45 5f 4c 4f 43 4b with SQLITE_LOCK
18998 45 44 20 69 66 20 74 68 65 72 65 20 61 72 65 20 ED if there are
18999 61 6e 79 20 6f 70 65 6e 0a 2a 2a 20 72 65 61 64 any open.** read
1899a 20 63 75 72 73 6f 72 73 20 6f 6e 20 74 68 65 20 cursors on the
1899b 74 61 62 6c 65 2e 20 20 4f 70 65 6e 20 77 72 69 table. Open wri
1899c 74 65 20 63 75 72 73 6f 72 73 20 61 72 65 20 6d te cursors are m
1899d 6f 76 65 64 20 74 6f 20 74 68 65 0a 2a 2a 20 72 oved to the.** r
1899e 6f 6f 74 20 6f 66 20 74 68 65 20 74 61 62 6c 65 oot of the table
1899f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 6e 43 68 61 ..**.** If pnCha
189a0 6e 67 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c nge is not NULL,
189a1 20 74 68 65 6e 20 74 61 62 6c 65 20 69 54 61 62 then table iTab
189a2 6c 65 20 6d 75 73 74 20 62 65 20 61 6e 20 69 6e le must be an in
189a3 74 6b 65 79 20 74 61 62 6c 65 2e 20 54 68 65 0a tkey table. The.
189a4 2a 2a 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 ** integer value
189a5 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 pointed to by p
189a6 6e 43 68 61 6e 67 65 20 69 73 20 69 6e 63 72 65 nChange is incre
189a7 6d 65 6e 74 65 64 20 62 79 20 74 68 65 20 6e 75 mented by the nu
189a8 6d 62 65 72 20 6f 66 0a 2a 2a 20 65 6e 74 72 69 mber of.** entri
189a9 65 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e es in the table.
189aa 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
189ab 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
189ac 72 65 65 43 6c 65 61 72 54 61 62 6c 65 28 42 74 reeClearTable(Bt
189ad 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 54 61 62 ree *p, int iTab
189ae 6c 65 2c 20 69 6e 74 20 2a 70 6e 43 68 61 6e 67 le, int *pnChang
189af 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 e){. int rc;.
189b0 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
189b1 70 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74 65 p->pBt;. sqlite
189b2 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
189b3 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 assert( p->inT
189b4 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 rans==TRANS_WRIT
189b5 45 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e 76 61 6c E );.. /* Inval
189b6 69 64 61 74 65 20 61 6c 6c 20 69 6e 63 72 62 6c idate all incrbl
189b7 6f 62 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 ob cursors open
189b8 6f 6e 20 74 61 62 6c 65 20 69 54 61 62 6c 65 20 on table iTable
189b9 28 61 73 73 75 6d 69 6e 67 20 69 54 61 62 6c 65 (assuming iTable
189ba 0a 20 20 2a 2a 20 69 73 20 74 68 65 20 72 6f 6f . ** is the roo
189bb 74 20 6f 66 20 61 20 74 61 62 6c 65 20 62 2d 74 t of a table b-t
189bc 72 65 65 20 2d 20 69 66 20 69 74 20 69 73 20 6e ree - if it is n
189bd 6f 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e ot, the followin
189be 67 20 63 61 6c 6c 20 69 73 0a 20 20 2a 2a 20 61 g call is. ** a
189bf 20 6e 6f 2d 6f 70 29 2e 20 20 2a 2f 0a 20 20 69 no-op). */. i
189c0 6e 76 61 6c 69 64 61 74 65 49 6e 63 72 62 6c 6f nvalidateIncrblo
189c1 62 43 75 72 73 6f 72 73 28 70 2c 20 30 2c 20 31 bCursors(p, 0, 1
189c2 29 3b 0a 0a 20 20 72 63 20 3d 20 73 61 76 65 41 );.. rc = saveA
189c3 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 2c 20 28 llCursors(pBt, (
189c4 50 67 6e 6f 29 69 54 61 62 6c 65 2c 20 30 29 3b Pgno)iTable, 0);
189c5 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b . if( SQLITE_OK
189c6 3d 3d 72 63 20 29 7b 0a 20 20 20 20 72 63 20 3d ==rc ){. rc =
189c7 20 63 6c 65 61 72 44 61 74 61 62 61 73 65 50 61 clearDatabasePa
189c8 67 65 28 70 42 74 2c 20 28 50 67 6e 6f 29 69 54 ge(pBt, (Pgno)iT
189c9 61 62 6c 65 2c 20 30 2c 20 70 6e 43 68 61 6e 67 able, 0, pnChang
189ca 65 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 e);. }. sqlite
189cb 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
189cc 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
189cd 2f 2a 0a 2a 2a 20 45 72 61 73 65 20 61 6c 6c 20 /*.** Erase all
189ce 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 61 information in a
189cf 20 74 61 62 6c 65 20 61 6e 64 20 61 64 64 20 74 table and add t
189d0 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 he root of the t
189d1 61 62 6c 65 20 74 6f 0a 2a 2a 20 74 68 65 20 66 able to.** the f
189d2 72 65 65 6c 69 73 74 2e 20 20 45 78 63 65 70 74 reelist. Except
189d3 2c 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 , the root of th
189d4 65 20 70 72 69 6e 63 69 70 6c 65 20 74 61 62 6c e principle tabl
189d5 65 20 28 74 68 65 20 6f 6e 65 20 6f 6e 0a 2a 2a e (the one on.**
189d6 20 70 61 67 65 20 31 29 20 69 73 20 6e 65 76 65 page 1) is neve
189d7 72 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 66 r added to the f
189d8 72 65 65 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 reelist..**.** T
189d9 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c his routine will
189da 20 66 61 69 6c 20 77 69 74 68 20 53 51 4c 49 54 fail with SQLIT
189db 45 5f 4c 4f 43 4b 45 44 20 69 66 20 74 68 65 72 E_LOCKED if ther
189dc 65 20 61 72 65 20 61 6e 79 20 6f 70 65 6e 0a 2a e are any open.*
189dd 2a 20 63 75 72 73 6f 72 73 20 6f 6e 20 74 68 65 * cursors on the
189de 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 table..**.** If
189df 20 41 55 54 4f 56 41 43 55 55 4d 20 69 73 20 65 AUTOVACUUM is e
189e0 6e 61 62 6c 65 64 20 61 6e 64 20 74 68 65 20 70 nabled and the p
189e1 61 67 65 20 61 74 20 69 54 61 62 6c 65 20 69 73 age at iTable is
189e2 20 6e 6f 74 20 74 68 65 20 6c 61 73 74 0a 2a 2a not the last.**
189e3 20 72 6f 6f 74 20 70 61 67 65 20 69 6e 20 74 68 root page in th
189e4 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c e database file,
189e5 20 74 68 65 6e 20 74 68 65 20 6c 61 73 74 20 72 then the last r
189e6 6f 6f 74 20 70 61 67 65 20 0a 2a 2a 20 69 6e 20 oot page .** in
189e7 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
189e8 65 20 69 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 e is moved into
189e9 74 68 65 20 73 6c 6f 74 20 66 6f 72 6d 65 72 6c the slot formerl
189ea 79 20 6f 63 63 75 70 69 65 64 20 62 79 0a 2a 2a y occupied by.**
189eb 20 69 54 61 62 6c 65 20 61 6e 64 20 74 68 61 74 iTable and that
189ec 20 6c 61 73 74 20 73 6c 6f 74 20 66 6f 72 6d 65 last slot forme
189ed 72 6c 79 20 6f 63 63 75 70 69 65 64 20 62 79 20 rly occupied by
189ee 74 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 the last root pa
189ef 67 65 0a 2a 2a 20 69 73 20 61 64 64 65 64 20 74 ge.** is added t
189f0 6f 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 69 o the freelist i
189f1 6e 73 74 65 61 64 20 6f 66 20 69 54 61 62 6c 65 nstead of iTable
189f2 2e 20 20 49 6e 20 74 68 69 73 20 73 61 79 2c 20 . In this say,
189f3 61 6c 6c 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 all.** root page
189f4 73 20 61 72 65 20 6b 65 70 74 20 61 74 20 74 68 s are kept at th
189f5 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 e beginning of t
189f6 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
189f7 2c 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 6e 65 , which.** is ne
189f8 63 65 73 73 61 72 79 20 66 6f 72 20 41 55 54 4f cessary for AUTO
189f9 56 41 43 55 55 4d 20 74 6f 20 77 6f 72 6b 20 72 VACUUM to work r
189fa 69 67 68 74 2e 20 20 2a 70 69 4d 6f 76 65 64 20 ight. *piMoved
189fb 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 0a 2a is set to the .*
189fc 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 20 74 68 * page number th
189fd 61 74 20 75 73 65 64 20 74 6f 20 62 65 20 74 68 at used to be th
189fe 65 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 67 65 e last root page
189ff 20 69 6e 20 74 68 65 20 66 69 6c 65 20 62 65 66 in the file bef
18a00 6f 72 65 0a 2a 2a 20 74 68 65 20 6d 6f 76 65 2e ore.** the move.
18a01 20 20 49 66 20 6e 6f 20 70 61 67 65 20 67 65 74 If no page get
18a02 73 20 6d 6f 76 65 64 2c 20 2a 70 69 4d 6f 76 65 s moved, *piMove
18a03 64 20 69 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a d is set to 0..*
18a04 2a 20 54 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 * The last root
18a05 70 61 67 65 20 69 73 20 72 65 63 6f 72 64 65 64 page is recorded
18a06 20 69 6e 20 6d 65 74 61 5b 33 5d 20 61 6e 64 20 in meta[3] and
18a07 74 68 65 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 the value of.**
18a08 6d 65 74 61 5b 33 5d 20 69 73 20 75 70 64 61 74 meta[3] is updat
18a09 65 64 20 62 79 20 74 68 69 73 20 70 72 6f 63 65 ed by this proce
18a0a 64 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 dure..*/.static
18a0b 69 6e 74 20 62 74 72 65 65 44 72 6f 70 54 61 62 int btreeDropTab
18a0c 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 50 67 6e le(Btree *p, Pgn
18a0d 6f 20 69 54 61 62 6c 65 2c 20 69 6e 74 20 2a 70 o iTable, int *p
18a0e 69 4d 6f 76 65 64 29 7b 0a 20 20 69 6e 74 20 72 iMoved){. int r
18a0f 63 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 c;. MemPage *pP
18a10 61 67 65 20 3d 20 30 3b 0a 20 20 42 74 53 68 61 age = 0;. BtSha
18a11 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
18a12 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 t;.. assert( sq
18a13 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d lite3BtreeHoldsM
18a14 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 utex(p) );. ass
18a15 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d ert( p->inTrans=
18a16 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a =TRANS_WRITE );.
18a17 0a 20 20 2f 2a 20 49 74 20 69 73 20 69 6c 6c 65 . /* It is ille
18a18 67 61 6c 20 74 6f 20 64 72 6f 70 20 61 20 74 61 gal to drop a ta
18a19 62 6c 65 20 69 66 20 61 6e 79 20 63 75 72 73 6f ble if any curso
18a1a 72 73 20 61 72 65 20 6f 70 65 6e 20 6f 6e 20 74 rs are open on t
18a1b 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 he. ** database
18a1c 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 75 73 . This is becaus
18a1d 65 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d e in auto-vacuum
18a1e 20 6d 6f 64 65 20 74 68 65 20 62 61 63 6b 65 6e mode the backen
18a1f 64 20 6d 61 79 0a 20 20 2a 2a 20 6e 65 65 64 20 d may. ** need
18a20 74 6f 20 6d 6f 76 65 20 61 6e 6f 74 68 65 72 20 to move another
18a21 72 6f 6f 74 2d 70 61 67 65 20 74 6f 20 66 69 6c root-page to fil
18a22 6c 20 61 20 67 61 70 20 6c 65 66 74 20 62 79 20 l a gap left by
18a23 74 68 65 20 64 65 6c 65 74 65 64 0a 20 20 2a 2a the deleted. **
18a24 20 72 6f 6f 74 20 70 61 67 65 2e 20 49 66 20 61 root page. If a
18a25 6e 20 6f 70 65 6e 20 63 75 72 73 6f 72 20 77 61 n open cursor wa
18a26 73 20 75 73 69 6e 67 20 74 68 69 73 20 70 61 67 s using this pag
18a27 65 20 61 20 70 72 6f 62 6c 65 6d 20 77 6f 75 6c e a problem woul
18a28 64 20 0a 20 20 2a 2a 20 6f 63 63 75 72 2e 0a 20 d . ** occur..
18a29 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 65 72 **. ** This er
18a2a 72 6f 72 20 69 73 20 63 61 75 67 68 74 20 6c 6f ror is caught lo
18a2b 6e 67 20 62 65 66 6f 72 65 20 63 6f 6e 74 72 6f ng before contro
18a2c 6c 20 72 65 61 63 68 65 73 20 74 68 69 73 20 70 l reaches this p
18a2d 6f 69 6e 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 oint.. */. if(
18a2e 20 4e 45 56 45 52 28 70 42 74 2d 3e 70 43 75 72 NEVER(pBt->pCur
18a2f 73 6f 72 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 sor) ){. sqli
18a30 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f te3ConnectionBlo
18a31 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 42 74 2d cked(p->db, pBt-
18a32 3e 70 43 75 72 73 6f 72 2d 3e 70 42 74 72 65 65 >pCursor->pBtree
18a33 2d 3e 64 62 29 3b 0a 20 20 20 20 72 65 74 75 72 ->db);. retur
18a34 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f n SQLITE_LOCKED_
18a35 53 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20 7d SHAREDCACHE;. }
18a36 0a 0a 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 .. rc = btreeGe
18a37 74 50 61 67 65 28 70 42 74 2c 20 28 50 67 6e 6f tPage(pBt, (Pgno
18a38 29 69 54 61 62 6c 65 2c 20 26 70 50 61 67 65 2c )iTable, &pPage,
18a39 20 30 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 0);. if( rc )
18a3a 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 return rc;. rc
18a3b 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c = sqlite3BtreeCl
18a3c 65 61 72 54 61 62 6c 65 28 70 2c 20 69 54 61 62 earTable(p, iTab
18a3d 6c 65 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 le, 0);. if( rc
18a3e 20 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 ){. releaseP
18a3f 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 age(pPage);.
18a40 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a return rc;. }..
18a41 20 20 2a 70 69 4d 6f 76 65 64 20 3d 20 30 3b 0a *piMoved = 0;.
18a42 0a 20 20 69 66 28 20 69 54 61 62 6c 65 3e 31 20 . if( iTable>1
18a43 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 ){.#ifdef SQLITE
18a44 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
18a45 0a 20 20 20 20 66 72 65 65 50 61 67 65 28 70 50 . freePage(pP
18a46 61 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 72 age, &rc);. r
18a47 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 eleasePage(pPage
18a48 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 69 66 28 );.#else. if(
18a49 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d pBt->autoVacuum
18a4a 20 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 6d ){. Pgno m
18a4b 61 78 52 6f 6f 74 50 67 6e 6f 3b 0a 20 20 20 20 axRootPgno;.
18a4c 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 sqlite3BtreeGe
18a4d 74 4d 65 74 61 28 70 2c 20 42 54 52 45 45 5f 4c tMeta(p, BTREE_L
18a4e 41 52 47 45 53 54 5f 52 4f 4f 54 5f 50 41 47 45 ARGEST_ROOT_PAGE
18a4f 2c 20 26 6d 61 78 52 6f 6f 74 50 67 6e 6f 29 3b , &maxRootPgno);
18a50 0a 0a 20 20 20 20 20 20 69 66 28 20 69 54 61 62 .. if( iTab
18a51 6c 65 3d 3d 6d 61 78 52 6f 6f 74 50 67 6e 6f 20 le==maxRootPgno
18a52 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 ){. /* If
18a53 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 the table being
18a54 20 64 72 6f 70 70 65 64 20 69 73 20 74 68 65 20 dropped is the
18a55 74 61 62 6c 65 20 77 69 74 68 20 74 68 65 20 6c table with the l
18a56 61 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 argest root-page
18a57 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 . ** numb
18a58 65 72 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 er in the databa
18a59 73 65 2c 20 70 75 74 20 74 68 65 20 72 6f 6f 74 se, put the root
18a5a 20 70 61 67 65 20 6f 6e 20 74 68 65 20 66 72 65 page on the fre
18a5b 65 20 6c 69 73 74 2e 20 0a 20 20 20 20 20 20 20 e list. .
18a5c 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 72 65 65 */. free
18a5d 50 61 67 65 28 70 50 61 67 65 2c 20 26 72 63 29 Page(pPage, &rc)
18a5e 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 ;. releas
18a5f 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 ePage(pPage);.
18a60 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
18a61 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
18a62 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
18a63 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
18a64 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f }else{. /
18a65 2a 20 54 68 65 20 74 61 62 6c 65 20 62 65 69 6e * The table bein
18a66 67 20 64 72 6f 70 70 65 64 20 64 6f 65 73 20 6e g dropped does n
18a67 6f 74 20 68 61 76 65 20 74 68 65 20 6c 61 72 67 ot have the larg
18a68 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 est root-page.
18a69 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 ** number
18a6a 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e in the database.
18a6b 20 53 6f 20 6d 6f 76 65 20 74 68 65 20 70 61 67 So move the pag
18a6c 65 20 74 68 61 74 20 64 6f 65 73 20 69 6e 74 6f e that does into
18a6d 20 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a the . **
18a6e 20 67 61 70 20 6c 65 66 74 20 62 79 20 74 68 65 gap left by the
18a6f 20 64 65 6c 65 74 65 64 20 72 6f 6f 74 2d 70 61 deleted root-pa
18a70 67 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 ge.. */.
18a71 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a MemPage *
18a72 70 4d 6f 76 65 3b 0a 20 20 20 20 20 20 20 20 72 pMove;. r
18a73 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 eleasePage(pPage
18a74 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 );. rc =
18a75 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 btreeGetPage(pBt
18a76 2c 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2c 20 26 , maxRootPgno, &
18a77 70 4d 6f 76 65 2c 20 30 29 3b 0a 20 20 20 20 20 pMove, 0);.
18a78 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
18a79 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
18a7a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
18a7b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 }. r
18a7c 63 20 3d 20 72 65 6c 6f 63 61 74 65 50 61 67 65 c = relocatePage
18a7d 28 70 42 74 2c 20 70 4d 6f 76 65 2c 20 50 54 52 (pBt, pMove, PTR
18a7e 4d 41 50 5f 52 4f 4f 54 50 41 47 45 2c 20 30 2c MAP_ROOTPAGE, 0,
18a7f 20 69 54 61 62 6c 65 2c 20 30 29 3b 0a 20 20 20 iTable, 0);.
18a80 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
18a81 28 70 4d 6f 76 65 29 3b 0a 20 20 20 20 20 20 20 (pMove);.
18a82 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
18a83 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
18a84 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
18a85 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 4d 6f }. pMo
18a86 76 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 ve = 0;.
18a87 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 rc = btreeGetPag
18a88 65 28 70 42 74 2c 20 6d 61 78 52 6f 6f 74 50 67 e(pBt, maxRootPg
18a89 6e 6f 2c 20 26 70 4d 6f 76 65 2c 20 30 29 3b 0a no, &pMove, 0);.
18a8a 20 20 20 20 20 20 20 20 66 72 65 65 50 61 67 65 freePage
18a8b 28 70 4d 6f 76 65 2c 20 26 72 63 29 3b 0a 20 20 (pMove, &rc);.
18a8c 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 releasePag
18a8d 65 28 70 4d 6f 76 65 29 3b 0a 20 20 20 20 20 20 e(pMove);.
18a8e 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
18a8f 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
18a90 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
18a91 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2a 70 }. *p
18a92 69 4d 6f 76 65 64 20 3d 20 6d 61 78 52 6f 6f 74 iMoved = maxRoot
18a93 50 67 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 Pgno;. }..
18a94 20 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 /* Set the
18a95 6e 65 77 20 27 6d 61 78 2d 72 6f 6f 74 2d 70 61 new 'max-root-pa
18a96 67 65 27 20 76 61 6c 75 65 20 69 6e 20 74 68 65 ge' value in the
18a97 20 64 61 74 61 62 61 73 65 20 68 65 61 64 65 72 database header
18a98 2e 20 54 68 69 73 0a 20 20 20 20 20 20 2a 2a 20 . This. **
18a99 69 73 20 74 68 65 20 6f 6c 64 20 76 61 6c 75 65 is the old value
18a9a 20 6c 65 73 73 20 6f 6e 65 2c 20 6c 65 73 73 20 less one, less
18a9b 6f 6e 65 20 6d 6f 72 65 20 69 66 20 74 68 61 74 one more if that
18a9c 20 68 61 70 70 65 6e 73 20 74 6f 0a 20 20 20 20 happens to.
18a9d 20 20 2a 2a 20 62 65 20 61 20 72 6f 6f 74 2d 70 ** be a root-p
18a9e 61 67 65 20 6e 75 6d 62 65 72 2c 20 6c 65 73 73 age number, less
18a9f 20 6f 6e 65 20 61 67 61 69 6e 20 69 66 20 74 68 one again if th
18aa0 61 74 20 69 73 20 74 68 65 0a 20 20 20 20 20 20 at is the.
18aa1 2a 2a 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f ** PENDING_BYTE_
18aa2 50 41 47 45 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 PAGE.. */.
18aa3 20 20 20 20 20 6d 61 78 52 6f 6f 74 50 67 6e 6f maxRootPgno
18aa4 2d 2d 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 --;. while(
18aa5 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3d 3d 50 45 maxRootPgno==PE
18aa6 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
18aa7 70 42 74 29 0a 20 20 20 20 20 20 20 20 20 20 20 pBt).
18aa8 20 20 7c 7c 20 50 54 52 4d 41 50 5f 49 53 50 41 || PTRMAP_ISPA
18aa9 47 45 28 70 42 74 2c 20 6d 61 78 52 6f 6f 74 50 GE(pBt, maxRootP
18aaa 67 6e 6f 29 20 29 7b 0a 20 20 20 20 20 20 20 20 gno) ){.
18aab 6d 61 78 52 6f 6f 74 50 67 6e 6f 2d 2d 3b 0a 20 maxRootPgno--;.
18aac 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 }. ass
18aad 65 72 74 28 20 6d 61 78 52 6f 6f 74 50 67 6e 6f ert( maxRootPgno
18aae 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 !=PENDING_BYTE_P
18aaf 41 47 45 28 70 42 74 29 20 29 3b 0a 0a 20 20 20 AGE(pBt) );..
18ab0 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
18ab1 74 72 65 65 55 70 64 61 74 65 4d 65 74 61 28 70 treeUpdateMeta(p
18ab2 2c 20 34 2c 20 6d 61 78 52 6f 6f 74 50 67 6e 6f , 4, maxRootPgno
18ab3 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
18ab4 20 20 20 20 66 72 65 65 50 61 67 65 28 70 50 61 freePage(pPa
18ab5 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 ge, &rc);.
18ab6 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 releasePage(pPag
18ab7 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 e);. }.#endif
18ab8 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
18ab9 20 49 66 20 73 71 6c 69 74 65 33 42 74 72 65 65 If sqlite3Btree
18aba 44 72 6f 70 54 61 62 6c 65 20 77 61 73 20 63 61 DropTable was ca
18abb 6c 6c 65 64 20 6f 6e 20 70 61 67 65 20 31 2e 0a lled on page 1..
18abc 20 20 20 20 2a 2a 20 54 68 69 73 20 72 65 61 6c ** This real
18abd 6c 79 20 6e 65 76 65 72 20 73 68 6f 75 6c 64 20 ly never should
18abe 68 61 70 70 65 6e 20 65 78 63 65 70 74 20 69 6e happen except in
18abf 20 61 20 63 6f 72 72 75 70 74 0a 20 20 20 20 2a a corrupt. *
18ac0 2a 20 64 61 74 61 62 61 73 65 2e 20 0a 20 20 20 * database. .
18ac1 20 2a 2f 0a 20 20 20 20 7a 65 72 6f 50 61 67 65 */. zeroPage
18ac2 28 70 50 61 67 65 2c 20 50 54 46 5f 49 4e 54 4b (pPage, PTF_INTK
18ac3 45 59 7c 50 54 46 5f 4c 45 41 46 20 29 3b 0a 20 EY|PTF_LEAF );.
18ac4 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
18ac5 50 61 67 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 Page);. }. ret
18ac6 75 72 6e 20 72 63 3b 20 20 0a 7d 0a 53 51 4c 49 urn rc; .}.SQLI
18ac7 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
18ac8 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70 54 qlite3BtreeDropT
18ac9 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 69 able(Btree *p, i
18aca 6e 74 20 69 54 61 62 6c 65 2c 20 69 6e 74 20 2a nt iTable, int *
18acb 70 69 4d 6f 76 65 64 29 7b 0a 20 20 69 6e 74 20 piMoved){. int
18acc 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 rc;. sqlite3Btr
18acd 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 eeEnter(p);. rc
18ace 20 3d 20 62 74 72 65 65 44 72 6f 70 54 61 62 6c = btreeDropTabl
18acf 65 28 70 2c 20 69 54 61 62 6c 65 2c 20 70 69 4d e(p, iTable, piM
18ad0 6f 76 65 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 oved);. sqlite3
18ad1 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
18ad2 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a return rc;.}...
18ad3 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
18ad4 69 6f 6e 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 ion may only be
18ad5 63 61 6c 6c 65 64 20 69 66 20 74 68 65 20 62 2d called if the b-
18ad6 74 72 65 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 tree connection
18ad7 61 6c 72 65 61 64 79 0a 2a 2a 20 68 61 73 20 61 already.** has a
18ad8 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 read or write t
18ad9 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 20 ransaction open
18ada 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e on the database.
18adb 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 .**.** Read the
18adc 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e meta-information
18add 20 6f 75 74 20 6f 66 20 61 20 64 61 74 61 62 61 out of a databa
18ade 73 65 20 66 69 6c 65 2e 20 20 4d 65 74 61 5b 30 se file. Meta[0
18adf 5d 0a 2a 2a 20 69 73 20 74 68 65 20 6e 75 6d 62 ].** is the numb
18ae0 65 72 20 6f 66 20 66 72 65 65 20 70 61 67 65 73 er of free pages
18ae1 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 currently in th
18ae2 65 20 64 61 74 61 62 61 73 65 2e 20 20 4d 65 74 e database. Met
18ae3 61 5b 31 5d 0a 2a 2a 20 74 68 72 6f 75 67 68 20 a[1].** through
18ae4 6d 65 74 61 5b 31 35 5d 20 61 72 65 20 61 76 61 meta[15] are ava
18ae5 69 6c 61 62 6c 65 20 66 6f 72 20 75 73 65 20 62 ilable for use b
18ae6 79 20 68 69 67 68 65 72 20 6c 61 79 65 72 73 2e y higher layers.
18ae7 20 20 4d 65 74 61 5b 30 5d 0a 2a 2a 20 69 73 20 Meta[0].** is
18ae8 72 65 61 64 2d 6f 6e 6c 79 2c 20 74 68 65 20 6f read-only, the o
18ae9 74 68 65 72 73 20 61 72 65 20 72 65 61 64 2f 77 thers are read/w
18aea 72 69 74 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 rite..** .** The
18aeb 20 73 63 68 65 6d 61 20 6c 61 79 65 72 20 6e 75 schema layer nu
18aec 6d 62 65 72 73 20 6d 65 74 61 20 76 61 6c 75 65 mbers meta value
18aed 73 20 64 69 66 66 65 72 65 6e 74 6c 79 2e 20 20 s differently.
18aee 41 74 20 74 68 65 20 73 63 68 65 6d 61 0a 2a 2a At the schema.**
18aef 20 6c 61 79 65 72 20 28 61 6e 64 20 74 68 65 20 layer (and the
18af0 53 65 74 43 6f 6f 6b 69 65 20 61 6e 64 20 52 65 SetCookie and Re
18af1 61 64 43 6f 6f 6b 69 65 20 6f 70 63 6f 64 65 73 adCookie opcodes
18af2 29 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a ) the number of.
18af3 2a 2a 20 66 72 65 65 20 70 61 67 65 73 20 69 73 ** free pages is
18af4 20 6e 6f 74 20 76 69 73 69 62 6c 65 2e 20 20 53 not visible. S
18af5 6f 20 43 6f 6f 6b 69 65 5b 30 5d 20 69 73 20 74 o Cookie[0] is t
18af6 68 65 20 73 61 6d 65 20 61 73 20 4d 65 74 61 5b he same as Meta[
18af7 31 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 1]..*/.SQLITE_PR
18af8 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
18af9 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 42 e3BtreeGetMeta(B
18afa 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 64 78 tree *p, int idx
18afb 2c 20 75 33 32 20 2a 70 4d 65 74 61 29 7b 0a 20 , u32 *pMeta){.
18afc 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
18afd 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 73 71 6c 69 p->pBt;.. sqli
18afe 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 te3BtreeEnter(p)
18aff 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 ;. assert( p->i
18b00 6e 54 72 61 6e 73 3e 54 52 41 4e 53 5f 4e 4f 4e nTrans>TRANS_NON
18b01 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 53 E );. assert( S
18b02 51 4c 49 54 45 5f 4f 4b 3d 3d 71 75 65 72 79 53 QLITE_OK==queryS
18b03 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c haredCacheTableL
18b04 6f 63 6b 28 70 2c 20 4d 41 53 54 45 52 5f 52 4f ock(p, MASTER_RO
18b05 4f 54 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 20 29 OT, READ_LOCK) )
18b06 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d ;. assert( pBt-
18b07 3e 70 50 61 67 65 31 20 29 3b 0a 20 20 61 73 73 >pPage1 );. ass
18b08 65 72 74 28 20 69 64 78 3e 3d 30 20 26 26 20 69 ert( idx>=0 && i
18b09 64 78 3c 3d 31 35 20 29 3b 0a 0a 20 20 2a 70 4d dx<=15 );.. *pM
18b0a 65 74 61 20 3d 20 67 65 74 34 62 79 74 65 28 26 eta = get4byte(&
18b0b 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 pBt->pPage1->aDa
18b0c 74 61 5b 33 36 20 2b 20 69 64 78 2a 34 5d 29 3b ta[36 + idx*4]);
18b0d 0a 0a 20 20 2f 2a 20 49 66 20 61 75 74 6f 2d 76 .. /* If auto-v
18b0e 61 63 75 75 6d 20 69 73 20 64 69 73 61 62 6c 65 acuum is disable
18b0f 64 20 69 6e 20 74 68 69 73 20 62 75 69 6c 64 20 d in this build
18b10 61 6e 64 20 74 68 69 73 20 69 73 20 61 6e 20 61 and this is an a
18b11 75 74 6f 2d 76 61 63 75 75 6d 0a 20 20 2a 2a 20 uto-vacuum. **
18b12 64 61 74 61 62 61 73 65 2c 20 6d 61 72 6b 20 74 database, mark t
18b13 68 65 20 64 61 74 61 62 61 73 65 20 61 73 20 72 he database as r
18b14 65 61 64 2d 6f 6e 6c 79 2e 20 20 2a 2f 0a 23 69 ead-only. */.#i
18b15 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
18b16 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 69 66 _AUTOVACUUM. if
18b17 28 20 69 64 78 3d 3d 42 54 52 45 45 5f 4c 41 52 ( idx==BTREE_LAR
18b18 47 45 53 54 5f 52 4f 4f 54 5f 50 41 47 45 20 26 GEST_ROOT_PAGE &
18b19 26 20 2a 70 4d 65 74 61 3e 30 20 29 20 70 42 74 & *pMeta>0 ) pBt
18b1a 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a ->readOnly = 1;.
18b1b 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 #endif.. sqlite
18b1c 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
18b1d 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 6d }../*.** Write m
18b1e 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 eta-information
18b1f 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 64 61 back into the da
18b20 74 61 62 61 73 65 2e 20 20 4d 65 74 61 5b 30 5d tabase. Meta[0]
18b21 20 69 73 0a 2a 2a 20 72 65 61 64 2d 6f 6e 6c 79 is.** read-only
18b22 20 61 6e 64 20 6d 61 79 20 6e 6f 74 20 62 65 20 and may not be
18b23 77 72 69 74 74 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 written..*/.SQLI
18b24 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
18b25 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74 qlite3BtreeUpdat
18b26 65 4d 65 74 61 28 42 74 72 65 65 20 2a 70 2c 20 eMeta(Btree *p,
18b27 69 6e 74 20 69 64 78 2c 20 75 33 32 20 69 4d 65 int idx, u32 iMe
18b28 74 61 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 ta){. BtShared
18b29 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 *pBt = p->pBt;.
18b2a 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
18b2b 70 50 31 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 pP1;. int rc;.
18b2c 20 61 73 73 65 72 74 28 20 69 64 78 3e 3d 31 20 assert( idx>=1
18b2d 26 26 20 69 64 78 3c 3d 31 35 20 29 3b 0a 20 20 && idx<=15 );.
18b2e 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
18b2f 72 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 r(p);. assert(
18b30 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e p->inTrans==TRAN
18b31 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73 S_WRITE );. ass
18b32 65 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 ert( pBt->pPage1
18b33 21 3d 30 20 29 3b 0a 20 20 70 50 31 20 3d 20 70 !=0 );. pP1 = p
18b34 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 Bt->pPage1->aDat
18b35 61 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 a;. rc = sqlite
18b36 33 50 61 67 65 72 57 72 69 74 65 28 70 42 74 2d 3PagerWrite(pBt-
18b37 3e 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 >pPage1->pDbPage
18b38 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
18b39 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 75 ITE_OK ){. pu
18b3a 74 34 62 79 74 65 28 26 70 50 31 5b 33 36 20 2b t4byte(&pP1[36 +
18b3b 20 69 64 78 2a 34 5d 2c 20 69 4d 65 74 61 29 3b idx*4], iMeta);
18b3c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
18b3d 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
18b3e 20 20 20 20 69 66 28 20 69 64 78 3d 3d 42 54 52 if( idx==BTR
18b3f 45 45 5f 49 4e 43 52 5f 56 41 43 55 55 4d 20 29 EE_INCR_VACUUM )
18b40 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
18b41 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 pBt->autoVacuum
18b42 7c 7c 20 69 4d 65 74 61 3d 3d 30 20 29 3b 0a 20 || iMeta==0 );.
18b43 20 20 20 20 20 61 73 73 65 72 74 28 20 69 4d 65 assert( iMe
18b44 74 61 3d 3d 30 20 7c 7c 20 69 4d 65 74 61 3d 3d ta==0 || iMeta==
18b45 31 20 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 1 );. pBt->
18b46 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 75 38 incrVacuum = (u8
18b47 29 69 4d 65 74 61 3b 0a 20 20 20 20 7d 0a 23 65 )iMeta;. }.#e
18b48 6e 64 69 66 0a 20 20 7d 0a 20 20 73 71 6c 69 74 ndif. }. sqlit
18b49 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
18b4a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
18b4b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
18b4c 4f 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e 54 0a OMIT_BTREECOUNT.
18b4d 2f 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 /*.** The first
18b4e 61 72 67 75 6d 65 6e 74 2c 20 70 43 75 72 2c 20 argument, pCur,
18b4f 69 73 20 61 20 63 75 72 73 6f 72 20 6f 70 65 6e is a cursor open
18b50 65 64 20 6f 6e 20 73 6f 6d 65 20 62 2d 74 72 65 ed on some b-tre
18b51 65 2e 20 43 6f 75 6e 74 20 74 68 65 0a 2a 2a 20 e. Count the.**
18b52 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 number of entrie
18b53 73 20 69 6e 20 74 68 65 20 62 2d 74 72 65 65 20 s in the b-tree
18b54 61 6e 64 20 77 72 69 74 65 20 74 68 65 20 72 65 and write the re
18b55 73 75 6c 74 20 74 6f 20 2a 70 6e 45 6e 74 72 79 sult to *pnEntry
18b56 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f ..**.** SQLITE_O
18b57 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 K is returned if
18b58 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 the operation i
18b59 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 65 s successfully e
18b5a 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 4f 74 68 xecuted. .** Oth
18b5b 65 72 77 69 73 65 2c 20 69 66 20 61 6e 20 65 72 erwise, if an er
18b5c 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 ror is encounter
18b5d 65 64 20 28 69 2e 65 2e 20 61 6e 20 49 4f 20 65 ed (i.e. an IO e
18b5e 72 72 6f 72 20 6f 72 20 64 61 74 61 62 61 73 65 rror or database
18b5f 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e 29 20 .** corruption)
18b60 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 an SQLite error
18b61 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 code is returned
18b62 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
18b63 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
18b64 74 72 65 65 43 6f 75 6e 74 28 42 74 43 75 72 73 treeCount(BtCurs
18b65 6f 72 20 2a 70 43 75 72 2c 20 69 36 34 20 2a 70 or *pCur, i64 *p
18b66 6e 45 6e 74 72 79 29 7b 0a 20 20 69 36 34 20 6e nEntry){. i64 n
18b67 45 6e 74 72 79 20 3d 20 30 3b 20 20 20 20 20 20 Entry = 0;
18b68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18b69 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 /* Value to retu
18b6a 72 6e 20 69 6e 20 2a 70 6e 45 6e 74 72 79 20 2a rn in *pnEntry *
18b6b 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 /. int rc;
18b6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18b6d 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 /* Retu
18b6e 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 72 63 20 rn code */. rc
18b6f 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 = moveToRoot(pCu
18b70 72 29 3b 0a 0a 20 20 2f 2a 20 55 6e 6c 65 73 73 r);.. /* Unless
18b71 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
18b72 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 , the following
18b73 6c 6f 6f 70 20 72 75 6e 73 20 6f 6e 65 20 69 74 loop runs one it
18b74 65 72 61 74 69 6f 6e 20 66 6f 72 20 65 61 63 68 eration for each
18b75 0a 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 . ** page in th
18b76 65 20 42 2d 54 72 65 65 20 73 74 72 75 63 74 75 e B-Tree structu
18b77 72 65 20 28 6e 6f 74 20 69 6e 63 6c 75 64 69 6e re (not includin
18b78 67 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 g overflow pages
18b79 29 2e 20 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 ). . */. while
18b7a 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
18b7b 29 7b 0a 20 20 20 20 69 6e 74 20 69 49 64 78 3b ){. int iIdx;
18b7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18b7d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 /* Ind
18b7e 65 78 20 6f 66 20 63 68 69 6c 64 20 6e 6f 64 65 ex of child node
18b7f 20 69 6e 20 70 61 72 65 6e 74 20 2a 2f 0a 20 20 in parent */.
18b80 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
18b81 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
18b82 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 /* Current
18b83 70 61 67 65 20 6f 66 20 74 68 65 20 62 2d 74 72 page of the b-tr
18b84 65 65 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 49 66 ee */.. /* If
18b85 20 74 68 69 73 20 69 73 20 61 20 6c 65 61 66 20 this is a leaf
18b86 70 61 67 65 20 6f 72 20 74 68 65 20 74 72 65 65 page or the tree
18b87 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 2d 6b is not an int-k
18b88 65 79 20 74 72 65 65 2c 20 74 68 65 6e 20 0a 20 ey tree, then .
18b89 20 20 20 2a 2a 20 74 68 69 73 20 70 61 67 65 20 ** this page
18b8a 63 6f 6e 74 61 69 6e 73 20 63 6f 75 6e 74 61 62 contains countab
18b8b 6c 65 20 65 6e 74 72 69 65 73 2e 20 49 6e 63 72 le entries. Incr
18b8c 65 6d 65 6e 74 20 74 68 65 20 65 6e 74 72 79 20 ement the entry
18b8d 63 6f 75 6e 74 65 72 0a 20 20 20 20 2a 2a 20 61 counter. ** a
18b8e 63 63 6f 72 64 69 6e 67 6c 79 2e 0a 20 20 20 20 ccordingly..
18b8f 2a 2f 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70 */. pPage = p
18b90 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
18b91 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 69 66 ->iPage];. if
18b92 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 7c 7c ( pPage->leaf ||
18b93 20 21 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 !pPage->intKey
18b94 29 7b 0a 20 20 20 20 20 20 6e 45 6e 74 72 79 20 ){. nEntry
18b95 2b 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b += pPage->nCell;
18b96 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 70 . }.. /* p
18b97 50 61 67 65 20 69 73 20 61 20 6c 65 61 66 20 6e Page is a leaf n
18b98 6f 64 65 2e 20 54 68 69 73 20 6c 6f 6f 70 20 6e ode. This loop n
18b99 61 76 69 67 61 74 65 73 20 74 68 65 20 63 75 72 avigates the cur
18b9a 73 6f 72 20 73 6f 20 74 68 61 74 20 69 74 20 0a sor so that it .
18b9b 20 20 20 20 2a 2a 20 70 6f 69 6e 74 73 20 74 6f ** points to
18b9c 20 74 68 65 20 66 69 72 73 74 20 69 6e 74 65 72 the first inter
18b9d 69 6f 72 20 63 65 6c 6c 20 74 68 61 74 20 69 74 ior cell that it
18b9e 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 70 points to the p
18b9f 61 72 65 6e 74 20 6f 66 0a 20 20 20 20 2a 2a 20 arent of. **
18ba0 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e the next page in
18ba1 20 74 68 65 20 74 72 65 65 20 74 68 61 74 20 68 the tree that h
18ba2 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 as not yet been
18ba3 76 69 73 69 74 65 64 2e 20 54 68 65 0a 20 20 20 visited. The.
18ba4 20 2a 2a 20 70 43 75 72 2d 3e 61 69 49 64 78 5b ** pCur->aiIdx[
18ba5 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 76 61 6c pCur->iPage] val
18ba6 75 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 ue is set to the
18ba7 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 70 61 index of the pa
18ba8 72 65 6e 74 20 63 65 6c 6c 0a 20 20 20 20 2a 2a rent cell. **
18ba9 20 6f 66 20 74 68 65 20 70 61 67 65 2c 20 6f 72 of the page, or
18baa 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f to the number o
18bab 66 20 63 65 6c 6c 73 20 69 6e 20 74 68 65 20 70 f cells in the p
18bac 61 67 65 20 69 66 20 74 68 65 20 6e 65 78 74 20 age if the next
18bad 70 61 67 65 0a 20 20 20 20 2a 2a 20 74 6f 20 76 page. ** to v
18bae 69 73 69 74 20 69 73 20 74 68 65 20 72 69 67 68 isit is the righ
18baf 74 2d 63 68 69 6c 64 20 6f 66 20 69 74 73 20 70 t-child of its p
18bb0 61 72 65 6e 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 arent.. **.
18bb1 20 20 2a 2a 20 49 66 20 61 6c 6c 20 70 61 67 65 ** If all page
18bb2 73 20 69 6e 20 74 68 65 20 74 72 65 65 20 68 61 s in the tree ha
18bb3 76 65 20 62 65 65 6e 20 76 69 73 69 74 65 64 2c ve been visited,
18bb4 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
18bb5 4b 20 74 6f 20 74 68 65 0a 20 20 20 20 2a 2a 20 K to the. **
18bb6 63 61 6c 6c 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 caller.. */.
18bb7 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 if( pPage->le
18bb8 61 66 20 29 7b 0a 20 20 20 20 20 20 64 6f 20 7b af ){. do {
18bb9 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 43 75 . if( pCu
18bba 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 7b 0a 20 r->iPage==0 ){.
18bbb 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 /* All
18bbc 70 61 67 65 73 20 6f 66 20 74 68 65 20 62 2d 74 pages of the b-t
18bbd 72 65 65 20 68 61 76 65 20 62 65 65 6e 20 76 69 ree have been vi
18bbe 73 69 74 65 64 2e 20 52 65 74 75 72 6e 20 73 75 sited. Return su
18bbf 63 63 65 73 73 66 75 6c 6c 79 2e 20 2a 2f 0a 20 ccessfully. */.
18bc0 20 20 20 20 20 20 20 20 20 2a 70 6e 45 6e 74 72 *pnEntr
18bc1 79 20 3d 20 6e 45 6e 74 72 79 3b 0a 20 20 20 20 y = nEntry;.
18bc2 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
18bc3 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 ITE_OK;.
18bc4 7d 0a 20 20 20 20 20 20 20 20 6d 6f 76 65 54 6f }. moveTo
18bc5 50 61 72 65 6e 74 28 70 43 75 72 29 3b 0a 20 20 Parent(pCur);.
18bc6 20 20 20 20 7d 77 68 69 6c 65 20 28 20 70 43 75 }while ( pCu
18bc7 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
18bc8 50 61 67 65 5d 3e 3d 70 43 75 72 2d 3e 61 70 50 Page]>=pCur->apP
18bc9 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
18bca 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20 20 20 ->nCell );..
18bcb 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 pCur->aiIdx[pC
18bcc 75 72 2d 3e 69 50 61 67 65 5d 2b 2b 3b 0a 20 20 ur->iPage]++;.
18bcd 20 20 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 pPage = pCur
18bce 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
18bcf 50 61 67 65 5d 3b 0a 20 20 20 20 7d 0a 0a 20 20 Page];. }..
18bd0 20 20 2f 2a 20 44 65 73 63 65 6e 64 20 74 6f 20 /* Descend to
18bd1 74 68 65 20 63 68 69 6c 64 20 6e 6f 64 65 20 6f the child node o
18bd2 66 20 74 68 65 20 63 65 6c 6c 20 74 68 61 74 20 f the cell that
18bd3 74 68 65 20 63 75 72 73 6f 72 20 63 75 72 72 65 the cursor curre
18bd4 6e 74 6c 79 20 0a 20 20 20 20 2a 2a 20 70 6f 69 ntly . ** poi
18bd5 6e 74 73 20 61 74 2e 20 54 68 69 73 20 69 73 20 nts at. This is
18bd6 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 the right-child
18bd7 69 66 20 28 69 49 64 78 3d 3d 70 50 61 67 65 2d if (iIdx==pPage-
18bd8 3e 6e 43 65 6c 6c 29 2e 0a 20 20 20 20 2a 2f 0a >nCell).. */.
18bd9 20 20 20 20 69 49 64 78 20 3d 20 70 43 75 72 2d iIdx = pCur-
18bda 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
18bdb 67 65 5d 3b 0a 20 20 20 20 69 66 28 20 69 49 64 ge];. if( iId
18bdc 78 3d 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 x==pPage->nCell
18bdd 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d 6f ){. rc = mo
18bde 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 veToChild(pCur,
18bdf 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d get4byte(&pPage-
18be0 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 >aData[pPage->hd
18be1 72 4f 66 66 73 65 74 2b 38 5d 29 29 3b 0a 20 20 rOffset+8]));.
18be2 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 }else{. r
18be3 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 c = moveToChild(
18be4 70 43 75 72 2c 20 67 65 74 34 62 79 74 65 28 66 pCur, get4byte(f
18be5 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 indCell(pPage, i
18be6 49 64 78 29 29 29 3b 0a 20 20 20 20 7d 0a 20 20 Idx)));. }.
18be7 7d 0a 0a 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 }.. /* An error
18be8 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20 52 has occurred. R
18be9 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 eturn an error c
18bea 6f 64 65 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e ode. */. return
18beb 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f rc;.}.#endif../
18bec 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
18bed 70 61 67 65 72 20 61 73 73 6f 63 69 61 74 65 64 pager associated
18bee 20 77 69 74 68 20 61 20 42 54 72 65 65 2e 20 20 with a BTree.
18bef 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
18bf0 75 73 65 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74 used for.** test
18bf1 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e ing and debuggin
18bf2 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 g only..*/.SQLIT
18bf3 45 5f 50 52 49 56 41 54 45 20 50 61 67 65 72 20 E_PRIVATE Pager
18bf4 2a 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 *sqlite3BtreePag
18bf5 65 72 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 er(Btree *p){.
18bf6 72 65 74 75 72 6e 20 70 2d 3e 70 42 74 2d 3e 70 return p->pBt->p
18bf7 50 61 67 65 72 3b 0a 7d 0a 0a 23 69 66 6e 64 65 Pager;.}..#ifnde
18bf8 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e f SQLITE_OMIT_IN
18bf9 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a TEGRITY_CHECK./*
18bfa 0a 2a 2a 20 41 70 70 65 6e 64 20 61 20 6d 65 73 .** Append a mes
18bfb 73 61 67 65 20 74 6f 20 74 68 65 20 65 72 72 6f sage to the erro
18bfc 72 20 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67 r message string
18bfd 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
18bfe 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 checkAppendMsg(
18bff 0a 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a . IntegrityCk *
18c00 70 43 68 65 63 6b 2c 0a 20 20 63 68 61 72 20 2a pCheck,. char *
18c01 7a 4d 73 67 31 2c 0a 20 20 63 6f 6e 73 74 20 63 zMsg1,. const c
18c02 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 0a 20 20 har *zFormat,.
18c03 2e 2e 2e 0a 29 7b 0a 20 20 76 61 5f 6c 69 73 74 ....){. va_list
18c04 20 61 70 3b 0a 20 20 69 66 28 20 21 70 43 68 65 ap;. if( !pChe
18c05 63 6b 2d 3e 6d 78 45 72 72 20 29 20 72 65 74 75 ck->mxErr ) retu
18c06 72 6e 3b 0a 20 20 70 43 68 65 63 6b 2d 3e 6d 78 rn;. pCheck->mx
18c07 45 72 72 2d 2d 3b 0a 20 20 70 43 68 65 63 6b 2d Err--;. pCheck-
18c08 3e 6e 45 72 72 2b 2b 3b 0a 20 20 76 61 5f 73 74 >nErr++;. va_st
18c09 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 art(ap, zFormat)
18c0a 3b 0a 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e ;. if( pCheck->
18c0b 65 72 72 4d 73 67 2e 6e 43 68 61 72 20 29 7b 0a errMsg.nChar ){.
18c0c 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 sqlite3StrAc
18c0d 63 75 6d 41 70 70 65 6e 64 28 26 70 43 68 65 63 cumAppend(&pChec
18c0e 6b 2d 3e 65 72 72 4d 73 67 2c 20 22 5c 6e 22 2c k->errMsg, "\n",
18c0f 20 31 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 1);. }. if( z
18c10 4d 73 67 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 Msg1 ){. sqli
18c11 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e te3StrAccumAppen
18c12 64 28 26 70 43 68 65 63 6b 2d 3e 65 72 72 4d 73 d(&pCheck->errMs
18c13 67 2c 20 7a 4d 73 67 31 2c 20 2d 31 29 3b 0a 20 g, zMsg1, -1);.
18c14 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 }. sqlite3VXPr
18c15 69 6e 74 66 28 26 70 43 68 65 63 6b 2d 3e 65 72 intf(&pCheck->er
18c16 72 4d 73 67 2c 20 31 2c 20 7a 46 6f 72 6d 61 74 rMsg, 1, zFormat
18c17 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 , ap);. va_end(
18c18 61 70 29 3b 0a 20 20 69 66 28 20 70 43 68 65 63 ap);. if( pChec
18c19 6b 2d 3e 65 72 72 4d 73 67 2e 6d 61 6c 6c 6f 63 k->errMsg.malloc
18c1a 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 70 43 Failed ){. pC
18c1b 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c heck->mallocFail
18c1c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 23 65 ed = 1;. }.}.#e
18c1d 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
18c1e 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 MIT_INTEGRITY_CH
18c1f 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 ECK */..#ifndef
18c20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 SQLITE_OMIT_INTE
18c21 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a GRITY_CHECK./*.*
18c22 2a 20 41 64 64 20 31 20 74 6f 20 74 68 65 20 72 * Add 1 to the r
18c23 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 66 eference count f
18c24 6f 72 20 70 61 67 65 20 69 50 61 67 65 2e 20 20 or page iPage.
18c25 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 73 If this is the s
18c26 65 63 6f 6e 64 0a 2a 2a 20 72 65 66 65 72 65 6e econd.** referen
18c27 63 65 20 74 6f 20 74 68 65 20 70 61 67 65 2c 20 ce to the page,
18c28 61 64 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 add an error mes
18c29 73 61 67 65 20 74 6f 20 70 43 68 65 63 6b 2d 3e sage to pCheck->
18c2a 7a 45 72 72 4d 73 67 2e 0a 2a 2a 20 52 65 74 75 zErrMsg..** Retu
18c2b 72 6e 20 31 20 69 66 20 74 68 65 72 65 20 61 72 rn 1 if there ar
18c2c 65 20 32 20 6f 72 65 20 6d 6f 72 65 20 72 65 66 e 2 ore more ref
18c2d 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70 erences to the p
18c2e 61 67 65 20 61 6e 64 20 30 20 69 66 0a 2a 2a 20 age and 0 if.**
18c2f 69 66 20 74 68 69 73 20 69 73 20 74 68 65 20 66 if this is the f
18c30 69 72 73 74 20 72 65 66 65 72 65 6e 63 65 20 74 irst reference t
18c31 6f 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a o the page..**.*
18c32 2a 20 41 6c 73 6f 20 63 68 65 63 6b 20 74 68 61 * Also check tha
18c33 74 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 t the page numbe
18c34 72 20 69 73 20 69 6e 20 62 6f 75 6e 64 73 2e 0a r is in bounds..
18c35 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68 */.static int ch
18c36 65 63 6b 52 65 66 28 49 6e 74 65 67 72 69 74 79 eckRef(Integrity
18c37 43 6b 20 2a 70 43 68 65 63 6b 2c 20 50 67 6e 6f Ck *pCheck, Pgno
18c38 20 69 50 61 67 65 2c 20 63 68 61 72 20 2a 7a 43 iPage, char *zC
18c39 6f 6e 74 65 78 74 29 7b 0a 20 20 69 66 28 20 69 ontext){. if( i
18c3a 50 61 67 65 3d 3d 30 20 29 20 72 65 74 75 72 6e Page==0 ) return
18c3b 20 31 3b 0a 20 20 69 66 28 20 69 50 61 67 65 3e 1;. if( iPage>
18c3c 70 43 68 65 63 6b 2d 3e 6e 50 61 67 65 20 29 7b pCheck->nPage ){
18c3d 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 . checkAppend
18c3e 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e Msg(pCheck, zCon
18c3f 74 65 78 74 2c 20 22 69 6e 76 61 6c 69 64 20 70 text, "invalid p
18c40 61 67 65 20 6e 75 6d 62 65 72 20 25 64 22 2c 20 age number %d",
18c41 69 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75 iPage);. retu
18c42 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 rn 1;. }. if(
18c43 70 43 68 65 63 6b 2d 3e 61 6e 52 65 66 5b 69 50 pCheck->anRef[iP
18c44 61 67 65 5d 3d 3d 31 20 29 7b 0a 20 20 20 20 63 age]==1 ){. c
18c45 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 heckAppendMsg(pC
18c46 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 heck, zContext,
18c47 22 32 6e 64 20 72 65 66 65 72 65 6e 63 65 20 74 "2nd reference t
18c48 6f 20 70 61 67 65 20 25 64 22 2c 20 69 50 61 67 o page %d", iPag
18c49 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 e);. return 1
18c4a 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 20 ;. }. return
18c4b 28 70 43 68 65 63 6b 2d 3e 61 6e 52 65 66 5b 69 (pCheck->anRef[i
18c4c 50 61 67 65 5d 2b 2b 29 3e 31 3b 0a 7d 0a 0a 23 Page]++)>1;.}..#
18c4d 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
18c4e 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a IT_AUTOVACUUM./*
18c4f 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 .** Check that t
18c50 68 65 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 he entry in the
18c51 70 6f 69 6e 74 65 72 2d 6d 61 70 20 66 6f 72 20 pointer-map for
18c52 70 61 67 65 20 69 43 68 69 6c 64 20 6d 61 70 73 page iChild maps
18c53 20 74 6f 20 0a 2a 2a 20 70 61 67 65 20 69 50 61 to .** page iPa
18c54 72 65 6e 74 2c 20 70 6f 69 6e 74 65 72 20 74 79 rent, pointer ty
18c55 70 65 20 70 74 72 54 79 70 65 2e 20 49 66 20 6e pe ptrType. If n
18c56 6f 74 2c 20 61 70 70 65 6e 64 20 61 6e 20 65 72 ot, append an er
18c57 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 74 ror message.** t
18c58 6f 20 70 43 68 65 63 6b 2e 0a 2a 2f 0a 73 74 61 o pCheck..*/.sta
18c59 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b 50 74 tic void checkPt
18c5a 72 6d 61 70 28 0a 20 20 49 6e 74 65 67 72 69 74 rmap(. Integrit
18c5b 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20 20 20 2f yCk *pCheck, /
18c5c 2a 20 49 6e 74 65 67 72 69 74 79 20 63 68 65 63 * Integrity chec
18c5d 6b 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 50 k context */. P
18c5e 67 6e 6f 20 69 43 68 69 6c 64 2c 20 20 20 20 20 gno iChild,
18c5f 20 20 20 20 20 20 2f 2a 20 43 68 69 6c 64 20 70 /* Child p
18c60 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 age number */.
18c61 75 38 20 65 54 79 70 65 2c 20 20 20 20 20 20 20 u8 eType,
18c62 20 20 20 20 20 20 20 2f 2a 20 45 78 70 65 63 74 /* Expect
18c63 65 64 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 74 ed pointer map t
18c64 79 70 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 50 ype */. Pgno iP
18c65 61 72 65 6e 74 2c 20 20 20 20 20 20 20 20 20 20 arent,
18c66 2f 2a 20 45 78 70 65 63 74 65 64 20 70 6f 69 6e /* Expected poin
18c67 74 65 72 20 6d 61 70 20 70 61 72 65 6e 74 20 70 ter map parent p
18c68 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 age number */.
18c69 63 68 61 72 20 2a 7a 43 6f 6e 74 65 78 74 20 20 char *zContext
18c6a 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 /* Contex
18c6b 74 20 64 65 73 63 72 69 70 74 69 6f 6e 20 28 75 t description (u
18c6c 73 65 64 20 66 6f 72 20 65 72 72 6f 72 20 6d 73 sed for error ms
18c6d 67 29 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 g) */.){. int r
18c6e 63 3b 0a 20 20 75 38 20 65 50 74 72 6d 61 70 54 c;. u8 ePtrmapT
18c6f 79 70 65 3b 0a 20 20 50 67 6e 6f 20 69 50 74 72 ype;. Pgno iPtr
18c70 6d 61 70 50 61 72 65 6e 74 3b 0a 0a 20 20 72 63 mapParent;.. rc
18c71 20 3d 20 70 74 72 6d 61 70 47 65 74 28 70 43 68 = ptrmapGet(pCh
18c72 65 63 6b 2d 3e 70 42 74 2c 20 69 43 68 69 6c 64 eck->pBt, iChild
18c73 2c 20 26 65 50 74 72 6d 61 70 54 79 70 65 2c 20 , &ePtrmapType,
18c74 26 69 50 74 72 6d 61 70 50 61 72 65 6e 74 29 3b &iPtrmapParent);
18c75 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
18c76 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 E_OK ){. if(
18c77 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d rc==SQLITE_NOMEM
18c78 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 || rc==SQLITE_I
18c79 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 70 43 68 OERR_NOMEM ) pCh
18c7a 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 eck->mallocFaile
18c7b 64 20 3d 20 31 3b 0a 20 20 20 20 63 68 65 63 6b d = 1;. check
18c7c 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b AppendMsg(pCheck
18c7d 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 46 61 69 , zContext, "Fai
18c7e 6c 65 64 20 74 6f 20 72 65 61 64 20 70 74 72 6d led to read ptrm
18c7f 61 70 20 6b 65 79 3d 25 64 22 2c 20 69 43 68 69 ap key=%d", iChi
18c80 6c 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b ld);. return;
18c81 0a 20 20 7d 0a 0a 20 20 69 66 28 20 65 50 74 72 . }.. if( ePtr
18c82 6d 61 70 54 79 70 65 21 3d 65 54 79 70 65 20 7c mapType!=eType |
18c83 7c 20 69 50 74 72 6d 61 70 50 61 72 65 6e 74 21 | iPtrmapParent!
18c84 3d 69 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 =iParent ){.
18c85 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 checkAppendMsg(p
18c86 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c Check, zContext,
18c87 20 0a 20 20 20 20 20 20 22 42 61 64 20 70 74 72 . "Bad ptr
18c88 20 6d 61 70 20 65 6e 74 72 79 20 6b 65 79 3d 25 map entry key=%
18c89 64 20 65 78 70 65 63 74 65 64 3d 28 25 64 2c 25 d expected=(%d,%
18c8a 64 29 20 67 6f 74 3d 28 25 64 2c 25 64 29 22 2c d) got=(%d,%d)",
18c8b 20 0a 20 20 20 20 20 20 69 43 68 69 6c 64 2c 20 . iChild,
18c8c 65 54 79 70 65 2c 20 69 50 61 72 65 6e 74 2c 20 eType, iParent,
18c8d 65 50 74 72 6d 61 70 54 79 70 65 2c 20 69 50 74 ePtrmapType, iPt
18c8e 72 6d 61 70 50 61 72 65 6e 74 29 3b 0a 20 20 7d rmapParent);. }
18c8f 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a .}.#endif../*.**
18c90 20 43 68 65 63 6b 20 74 68 65 20 69 6e 74 65 67 Check the integ
18c91 72 69 74 79 20 6f 66 20 74 68 65 20 66 72 65 65 rity of the free
18c92 6c 69 73 74 20 6f 72 20 6f 66 20 61 6e 20 6f 76 list or of an ov
18c93 65 72 66 6c 6f 77 20 70 61 67 65 20 6c 69 73 74 erflow page list
18c94 2e 0a 2a 2a 20 56 65 72 69 66 79 20 74 68 61 74 ..** Verify that
18c95 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 the number of p
18c96 61 67 65 73 20 6f 6e 20 74 68 65 20 6c 69 73 74 ages on the list
18c97 20 69 73 20 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 is N..*/.static
18c98 20 76 6f 69 64 20 63 68 65 63 6b 4c 69 73 74 28 void checkList(
18c99 0a 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a . IntegrityCk *
18c9a 70 43 68 65 63 6b 2c 20 20 2f 2a 20 49 6e 74 65 pCheck, /* Inte
18c9b 67 72 69 74 79 20 63 68 65 63 6b 69 6e 67 20 63 grity checking c
18c9c 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 ontext */. int
18c9d 69 73 46 72 65 65 4c 69 73 74 2c 20 20 20 20 20 isFreeList,
18c9e 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 /* True for a
18c9f 66 72 65 65 6c 69 73 74 2e 20 20 46 61 6c 73 65 freelist. False
18ca0 20 66 6f 72 20 6f 76 65 72 66 6c 6f 77 20 70 61 for overflow pa
18ca1 67 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 ge list */. int
18ca2 20 69 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 iPage,
18ca3 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 /* Page numbe
18ca4 72 20 66 6f 72 20 66 69 72 73 74 20 70 61 67 65 r for first page
18ca5 20 69 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a in the list */.
18ca6 20 20 69 6e 74 20 4e 2c 20 20 20 20 20 20 20 20 int N,
18ca7 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 65 63 /* Expec
18ca8 74 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 ted number of pa
18ca9 67 65 73 20 69 6e 20 74 68 65 20 6c 69 73 74 20 ges in the list
18caa 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6e 74 */. char *zCont
18cab 65 78 74 20 20 20 20 20 20 20 20 2f 2a 20 43 6f ext /* Co
18cac 6e 74 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20 ntext for error
18cad 6d 65 73 73 61 67 65 73 20 2a 2f 0a 29 7b 0a 20 messages */.){.
18cae 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 65 78 int i;. int ex
18caf 70 65 63 74 65 64 20 3d 20 4e 3b 0a 20 20 69 6e pected = N;. in
18cb0 74 20 69 46 69 72 73 74 20 3d 20 69 50 61 67 65 t iFirst = iPage
18cb1 3b 0a 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 3e ;. while( N-- >
18cb2 20 30 20 26 26 20 70 43 68 65 63 6b 2d 3e 6d 78 0 && pCheck->mx
18cb3 45 72 72 20 29 7b 0a 20 20 20 20 44 62 50 61 67 Err ){. DbPag
18cb4 65 20 2a 70 4f 76 66 6c 50 61 67 65 3b 0a 20 20 e *pOvflPage;.
18cb5 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
18cb6 2a 70 4f 76 66 6c 44 61 74 61 3b 0a 20 20 20 20 *pOvflData;.
18cb7 69 66 28 20 69 50 61 67 65 3c 31 20 29 7b 0a 20 if( iPage<1 ){.
18cb8 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 checkAppend
18cb9 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e Msg(pCheck, zCon
18cba 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20 22 text,. "
18cbb 25 64 20 6f 66 20 25 64 20 70 61 67 65 73 20 6d %d of %d pages m
18cbc 69 73 73 69 6e 67 20 66 72 6f 6d 20 6f 76 65 72 issing from over
18cbd 66 6c 6f 77 20 6c 69 73 74 20 73 74 61 72 74 69 flow list starti
18cbe 6e 67 20 61 74 20 25 64 22 2c 0a 20 20 20 20 20 ng at %d",.
18cbf 20 20 20 20 20 4e 2b 31 2c 20 65 78 70 65 63 74 N+1, expect
18cc0 65 64 2c 20 69 46 69 72 73 74 29 3b 0a 20 20 20 ed, iFirst);.
18cc1 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
18cc2 20 20 20 20 69 66 28 20 63 68 65 63 6b 52 65 66 if( checkRef
18cc3 28 70 43 68 65 63 6b 2c 20 69 50 61 67 65 2c 20 (pCheck, iPage,
18cc4 7a 43 6f 6e 74 65 78 74 29 20 29 20 62 72 65 61 zContext) ) brea
18cc5 6b 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 k;. if( sqlit
18cc6 65 33 50 61 67 65 72 47 65 74 28 70 43 68 65 63 e3PagerGet(pChec
18cc7 6b 2d 3e 70 50 61 67 65 72 2c 20 28 50 67 6e 6f k->pPager, (Pgno
18cc8 29 69 50 61 67 65 2c 20 26 70 4f 76 66 6c 50 61 )iPage, &pOvflPa
18cc9 67 65 29 20 29 7b 0a 20 20 20 20 20 20 63 68 65 ge) ){. che
18cca 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 ckAppendMsg(pChe
18ccb 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 66 ck, zContext, "f
18ccc 61 69 6c 65 64 20 74 6f 20 67 65 74 20 70 61 67 ailed to get pag
18ccd 65 20 25 64 22 2c 20 69 50 61 67 65 29 3b 0a 20 e %d", iPage);.
18cce 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
18ccf 7d 0a 20 20 20 20 70 4f 76 66 6c 44 61 74 61 20 }. pOvflData
18cd0 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 = (unsigned char
18cd1 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 *)sqlite3PagerG
18cd2 65 74 44 61 74 61 28 70 4f 76 66 6c 50 61 67 65 etData(pOvflPage
18cd3 29 3b 0a 20 20 20 20 69 66 28 20 69 73 46 72 65 );. if( isFre
18cd4 65 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 69 eList ){. i
18cd5 6e 74 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28 nt n = get4byte(
18cd6 26 70 4f 76 66 6c 44 61 74 61 5b 34 5d 29 3b 0a &pOvflData[4]);.
18cd7 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
18cd8 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
18cd9 20 20 20 20 20 69 66 28 20 70 43 68 65 63 6b 2d if( pCheck-
18cda 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d >pBt->autoVacuum
18cdb 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 ){. chec
18cdc 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 kPtrmap(pCheck,
18cdd 69 50 61 67 65 2c 20 50 54 52 4d 41 50 5f 46 52 iPage, PTRMAP_FR
18cde 45 45 50 41 47 45 2c 20 30 2c 20 7a 43 6f 6e 74 EEPAGE, 0, zCont
18cdf 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 ext);. }.#e
18ce0 6e 64 69 66 0a 20 20 20 20 20 20 69 66 28 20 6e ndif. if( n
18ce1 3e 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 75 73 >pCheck->pBt->us
18ce2 61 62 6c 65 53 69 7a 65 2f 34 2d 32 20 29 7b 0a ableSize/4-2 ){.
18ce3 20 20 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 checkApp
18ce4 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a endMsg(pCheck, z
18ce5 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 Context,.
18ce6 20 20 20 20 22 66 72 65 65 6c 69 73 74 20 6c 65 "freelist le
18ce7 61 66 20 63 6f 75 6e 74 20 74 6f 6f 20 62 69 67 af count too big
18ce8 20 6f 6e 20 70 61 67 65 20 25 64 22 2c 20 69 50 on page %d", iP
18ce9 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 4e 2d age);. N-
18cea 2d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a -;. }else{.
18ceb 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b for(i=0;
18cec 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 i<n; i++){.
18ced 20 20 20 20 20 20 50 67 6e 6f 20 69 46 72 65 65 Pgno iFree
18cee 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 Page = get4byte(
18cef 26 70 4f 76 66 6c 44 61 74 61 5b 38 2b 69 2a 34 &pOvflData[8+i*4
18cf0 5d 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 ]);.#ifndef SQLI
18cf1 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
18cf2 55 4d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 UM. if(
18cf3 20 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 61 75 pCheck->pBt->au
18cf4 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 toVacuum ){.
18cf5 20 20 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 checkPtr
18cf6 6d 61 70 28 70 43 68 65 63 6b 2c 20 69 46 72 65 map(pCheck, iFre
18cf7 65 50 61 67 65 2c 20 50 54 52 4d 41 50 5f 46 52 ePage, PTRMAP_FR
18cf8 45 45 50 41 47 45 2c 20 30 2c 20 7a 43 6f 6e 74 EEPAGE, 0, zCont
18cf9 65 78 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ext);.
18cfa 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 }.#endif.
18cfb 20 20 20 63 68 65 63 6b 52 65 66 28 70 43 68 65 checkRef(pChe
18cfc 63 6b 2c 20 69 46 72 65 65 50 61 67 65 2c 20 7a ck, iFreePage, z
18cfd 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 Context);.
18cfe 20 20 7d 0a 20 20 20 20 20 20 20 20 4e 20 2d 3d }. N -=
18cff 20 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 n;. }.
18d00 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 }.#ifndef SQLITE
18d01 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
18d02 0a 20 20 20 20 65 6c 73 65 7b 0a 20 20 20 20 20 . else{.
18d03 20 2f 2a 20 49 66 20 74 68 69 73 20 64 61 74 61 /* If this data
18d04 62 61 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 base supports au
18d05 74 6f 2d 76 61 63 75 75 6d 20 61 6e 64 20 69 50 to-vacuum and iP
18d06 61 67 65 20 69 73 20 6e 6f 74 20 74 68 65 20 6c age is not the l
18d07 61 73 74 0a 20 20 20 20 20 20 2a 2a 20 70 61 67 ast. ** pag
18d08 65 20 69 6e 20 74 68 69 73 20 6f 76 65 72 66 6c e in this overfl
18d09 6f 77 20 6c 69 73 74 2c 20 63 68 65 63 6b 20 74 ow list, check t
18d0a 68 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72 2d hat the pointer-
18d0b 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 0a 20 20 map entry for.
18d0c 20 20 20 20 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f ** the follo
18d0d 77 69 6e 67 20 70 61 67 65 20 6d 61 74 63 68 65 wing page matche
18d0e 73 20 69 50 61 67 65 2e 0a 20 20 20 20 20 20 2a s iPage.. *
18d0f 2f 0a 20 20 20 20 20 20 69 66 28 20 70 43 68 65 /. if( pChe
18d10 63 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 ck->pBt->autoVac
18d11 75 75 6d 20 26 26 20 4e 3e 30 20 29 7b 0a 20 20 uum && N>0 ){.
18d12 20 20 20 20 20 20 69 20 3d 20 67 65 74 34 62 79 i = get4by
18d13 74 65 28 70 4f 76 66 6c 44 61 74 61 29 3b 0a 20 te(pOvflData);.
18d14 20 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d checkPtrm
18d15 61 70 28 70 43 68 65 63 6b 2c 20 69 2c 20 50 54 ap(pCheck, i, PT
18d16 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 2c 20 RMAP_OVERFLOW2,
18d17 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 iPage, zContext)
18d18 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
18d19 23 65 6e 64 69 66 0a 20 20 20 20 69 50 61 67 65 #endif. iPage
18d1a 20 3d 20 67 65 74 34 62 79 74 65 28 70 4f 76 66 = get4byte(pOvf
18d1b 6c 44 61 74 61 29 3b 0a 20 20 20 20 73 71 6c 69 lData);. sqli
18d1c 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 4f te3PagerUnref(pO
18d1d 76 66 6c 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a vflPage);. }.}.
18d1e 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
18d1f 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f _OMIT_INTEGRITY_
18d20 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 CHECK */..#ifnde
18d21 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e f SQLITE_OMIT_IN
18d22 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a TEGRITY_CHECK./*
18d23 0a 2a 2a 20 44 6f 20 76 61 72 69 6f 75 73 20 73 .** Do various s
18d24 61 6e 69 74 79 20 63 68 65 63 6b 73 20 6f 6e 20 anity checks on
18d25 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 6f 66 a single page of
18d26 20 61 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e a tree. Return
18d27 0a 2a 2a 20 74 68 65 20 74 72 65 65 20 64 65 70 .** the tree dep
18d28 74 68 2e 20 20 52 6f 6f 74 20 70 61 67 65 73 20 th. Root pages
18d29 72 65 74 75 72 6e 20 30 2e 20 20 50 61 72 65 6e return 0. Paren
18d2a 74 73 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 73 ts of root pages
18d2b 0a 2a 2a 20 72 65 74 75 72 6e 20 31 2c 20 61 6e .** return 1, an
18d2c 64 20 73 6f 20 66 6f 72 74 68 2e 0a 2a 2a 20 0a d so forth..** .
18d2d 2a 2a 20 54 68 65 73 65 20 63 68 65 63 6b 73 20 ** These checks
18d2e 61 72 65 20 64 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20 are done:.**.**
18d2f 20 20 20 20 20 31 2e 20 20 4d 61 6b 65 20 73 75 1. Make su
18d30 72 65 20 74 68 61 74 20 63 65 6c 6c 73 20 61 6e re that cells an
18d31 64 20 66 72 65 65 62 6c 6f 63 6b 73 20 64 6f 20 d freeblocks do
18d32 6e 6f 74 20 6f 76 65 72 6c 61 70 0a 2a 2a 20 20 not overlap.**
18d33 20 20 20 20 20 20 20 20 62 75 74 20 63 6f 6d 62 but comb
18d34 69 6e 65 20 74 6f 20 63 6f 6d 70 6c 65 74 65 6c ine to completel
18d35 79 20 63 6f 76 65 72 20 74 68 65 20 70 61 67 65 y cover the page
18d36 2e 0a 2a 2a 20 20 4e 4f 20 20 32 2e 20 20 4d 61 ..** NO 2. Ma
18d37 6b 65 20 73 75 72 65 20 63 65 6c 6c 20 6b 65 79 ke sure cell key
18d38 73 20 61 72 65 20 69 6e 20 6f 72 64 65 72 2e 0a s are in order..
18d39 2a 2a 20 20 4e 4f 20 20 33 2e 20 20 4d 61 6b 65 ** NO 3. Make
18d3a 20 73 75 72 65 20 6e 6f 20 6b 65 79 20 69 73 20 sure no key is
18d3b 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 less than or equ
18d3c 61 6c 20 74 6f 20 7a 4c 6f 77 65 72 42 6f 75 6e al to zLowerBoun
18d3d 64 2e 0a 2a 2a 20 20 4e 4f 20 20 34 2e 20 20 4d d..** NO 4. M
18d3e 61 6b 65 20 73 75 72 65 20 6e 6f 20 6b 65 79 20 ake sure no key
18d3f 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 is greater than
18d40 6f 72 20 65 71 75 61 6c 20 74 6f 20 7a 55 70 70 or equal to zUpp
18d41 65 72 42 6f 75 6e 64 2e 0a 2a 2a 20 20 20 20 20 erBound..**
18d42 20 35 2e 20 20 43 68 65 63 6b 20 74 68 65 20 69 5. Check the i
18d43 6e 74 65 67 72 69 74 79 20 6f 66 20 6f 76 65 72 ntegrity of over
18d44 66 6c 6f 77 20 70 61 67 65 73 2e 0a 2a 2a 20 20 flow pages..**
18d45 20 20 20 20 36 2e 20 20 52 65 63 75 72 73 69 76 6. Recursiv
18d46 65 6c 79 20 63 61 6c 6c 20 63 68 65 63 6b 54 72 ely call checkTr
18d47 65 65 50 61 67 65 20 6f 6e 20 61 6c 6c 20 63 68 eePage on all ch
18d48 69 6c 64 72 65 6e 2e 0a 2a 2a 20 20 20 20 20 20 ildren..**
18d49 37 2e 20 20 56 65 72 69 66 79 20 74 68 61 74 20 7. Verify that
18d4a 74 68 65 20 64 65 70 74 68 20 6f 66 20 61 6c 6c the depth of all
18d4b 20 63 68 69 6c 64 72 65 6e 20 69 73 20 74 68 65 children is the
18d4c 20 73 61 6d 65 2e 0a 2a 2a 20 20 20 20 20 20 38 same..** 8
18d4d 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 69 . Make sure thi
18d4e 73 20 70 61 67 65 20 69 73 20 61 74 20 6c 65 61 s page is at lea
18d4f 73 74 20 33 33 25 20 66 75 6c 6c 20 6f 72 20 65 st 33% full or e
18d50 6c 73 65 20 69 74 20 69 73 0a 2a 2a 20 20 20 20 lse it is.**
18d51 20 20 20 20 20 20 74 68 65 20 72 6f 6f 74 20 6f the root o
18d52 66 20 74 68 65 20 74 72 65 65 2e 0a 2a 2f 0a 73 f the tree..*/.s
18d53 74 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b 54 tatic int checkT
18d54 72 65 65 50 61 67 65 28 0a 20 20 49 6e 74 65 67 reePage(. Integ
18d55 72 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20 rityCk *pCheck,
18d56 20 2f 2a 20 43 6f 6e 74 65 78 74 20 66 6f 72 20 /* Context for
18d57 74 68 65 20 73 61 6e 69 74 79 20 63 68 65 63 6b the sanity check
18d58 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 67 65 2c */. int iPage,
18d59 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
18d5a 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 age number of th
18d5b 65 20 70 61 67 65 20 74 6f 20 63 68 65 63 6b 20 e page to check
18d5c 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 50 61 72 65 */. char *zPare
18d5d 6e 74 43 6f 6e 74 65 78 74 20 20 2f 2a 20 50 61 ntContext /* Pa
18d5e 72 65 6e 74 20 63 6f 6e 74 65 78 74 20 2a 2f 0a rent context */.
18d5f 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 ){. MemPage *pP
18d60 61 67 65 3b 0a 20 20 69 6e 74 20 69 2c 20 72 63 age;. int i, rc
18d61 2c 20 64 65 70 74 68 2c 20 64 32 2c 20 70 67 6e , depth, d2, pgn
18d62 6f 2c 20 63 6e 74 3b 0a 20 20 69 6e 74 20 68 64 o, cnt;. int hd
18d63 72 2c 20 63 65 6c 6c 53 74 61 72 74 3b 0a 20 20 r, cellStart;.
18d64 69 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20 75 38 20 int nCell;. u8
18d65 2a 64 61 74 61 3b 0a 20 20 42 74 53 68 61 72 65 *data;. BtShare
18d66 64 20 2a 70 42 74 3b 0a 20 20 69 6e 74 20 75 73 d *pBt;. int us
18d67 61 62 6c 65 53 69 7a 65 3b 0a 20 20 63 68 61 72 ableSize;. char
18d68 20 7a 43 6f 6e 74 65 78 74 5b 31 30 30 5d 3b 0a zContext[100];.
18d69 20 20 63 68 61 72 20 2a 68 69 74 20 3d 20 30 3b char *hit = 0;
18d6a 0a 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 .. sqlite3_snpr
18d6b 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e intf(sizeof(zCon
18d6c 74 65 78 74 29 2c 20 7a 43 6f 6e 74 65 78 74 2c text), zContext,
18d6d 20 22 50 61 67 65 20 25 64 3a 20 22 2c 20 69 50 "Page %d: ", iP
18d6e 61 67 65 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 age);.. /* Chec
18d6f 6b 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 k that the page
18d70 65 78 69 73 74 73 0a 20 20 2a 2f 0a 20 20 70 42 exists. */. pB
18d71 74 20 3d 20 70 43 68 65 63 6b 2d 3e 70 42 74 3b t = pCheck->pBt;
18d72 0a 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 . usableSize =
18d73 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b pBt->usableSize;
18d74 0a 20 20 69 66 28 20 69 50 61 67 65 3d 3d 30 20 . if( iPage==0
18d75 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 ) return 0;. if
18d76 28 20 63 68 65 63 6b 52 65 66 28 70 43 68 65 63 ( checkRef(pChec
18d77 6b 2c 20 69 50 61 67 65 2c 20 7a 50 61 72 65 6e k, iPage, zParen
18d78 74 43 6f 6e 74 65 78 74 29 20 29 20 72 65 74 75 tContext) ) retu
18d79 72 6e 20 30 3b 0a 20 20 69 66 28 20 28 72 63 20 rn 0;. if( (rc
18d7a 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 = btreeGetPage(p
18d7b 42 74 2c 20 28 50 67 6e 6f 29 69 50 61 67 65 2c Bt, (Pgno)iPage,
18d7c 20 26 70 50 61 67 65 2c 20 30 29 29 21 3d 30 20 &pPage, 0))!=0
18d7d 29 7b 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65 ){. checkAppe
18d7e 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 ndMsg(pCheck, zC
18d7f 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 22 ontext,. "
18d80 75 6e 61 62 6c 65 20 74 6f 20 67 65 74 20 74 68 unable to get th
18d81 65 20 70 61 67 65 2e 20 65 72 72 6f 72 20 63 6f e page. error co
18d82 64 65 3d 25 64 22 2c 20 72 63 29 3b 0a 20 20 20 de=%d", rc);.
18d83 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a return 0;. }..
18d84 20 20 2f 2a 20 43 6c 65 61 72 20 4d 65 6d 50 61 /* Clear MemPa
18d85 67 65 2e 69 73 49 6e 69 74 20 74 6f 20 6d 61 6b ge.isInit to mak
18d86 65 20 73 75 72 65 20 74 68 65 20 63 6f 72 72 75 e sure the corru
18d87 70 74 69 6f 6e 20 64 65 74 65 63 74 69 6f 6e 20 ption detection
18d88 63 6f 64 65 20 69 6e 0a 20 20 2a 2a 20 62 74 72 code in. ** btr
18d89 65 65 49 6e 69 74 50 61 67 65 28 29 20 69 73 20 eeInitPage() is
18d8a 65 78 65 63 75 74 65 64 2e 20 20 2a 2f 0a 20 20 executed. */.
18d8b 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 pPage->isInit =
18d8c 30 3b 0a 20 20 69 66 28 20 28 72 63 20 3d 20 62 0;. if( (rc = b
18d8d 74 72 65 65 49 6e 69 74 50 61 67 65 28 70 50 61 treeInitPage(pPa
18d8e 67 65 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 61 ge))!=0 ){. a
18d8f 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 ssert( rc==SQLIT
18d90 45 5f 43 4f 52 52 55 50 54 20 29 3b 20 20 2f 2a E_CORRUPT ); /*
18d91 20 54 68 65 20 6f 6e 6c 79 20 70 6f 73 73 69 62 The only possib
18d92 6c 65 20 65 72 72 6f 72 20 66 72 6f 6d 20 49 6e le error from In
18d93 69 74 50 61 67 65 20 2a 2f 0a 20 20 20 20 63 68 itPage */. ch
18d94 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 eckAppendMsg(pCh
18d95 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a eck, zContext, .
18d96 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18d97 20 20 20 22 62 74 72 65 65 49 6e 69 74 50 61 67 "btreeInitPag
18d98 65 28 29 20 72 65 74 75 72 6e 73 20 65 72 72 6f e() returns erro
18d99 72 20 63 6f 64 65 20 25 64 22 2c 20 72 63 29 3b r code %d", rc);
18d9a 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 . releasePage
18d9b 28 70 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 (pPage);. ret
18d9c 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a urn 0;. }.. /*
18d9d 20 43 68 65 63 6b 20 6f 75 74 20 61 6c 6c 20 74 Check out all t
18d9e 68 65 20 63 65 6c 6c 73 2e 0a 20 20 2a 2f 0a 20 he cells.. */.
18d9f 20 64 65 70 74 68 20 3d 20 30 3b 0a 20 20 66 6f depth = 0;. fo
18da0 72 28 69 3d 30 3b 20 69 3c 70 50 61 67 65 2d 3e r(i=0; i<pPage->
18da1 6e 43 65 6c 6c 20 26 26 20 70 43 68 65 63 6b 2d nCell && pCheck-
18da2 3e 6d 78 45 72 72 3b 20 69 2b 2b 29 7b 0a 20 20 >mxErr; i++){.
18da3 20 20 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 20 u8 *pCell;.
18da4 20 75 33 32 20 73 7a 3b 0a 20 20 20 20 43 65 6c u32 sz;. Cel
18da5 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20 20 20 lInfo info;..
18da6 20 2f 2a 20 43 68 65 63 6b 20 70 61 79 6c 6f 61 /* Check payloa
18da7 64 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 d overflow pages
18da8 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 . */. sqli
18da9 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a te3_snprintf(siz
18daa 65 6f 66 28 7a 43 6f 6e 74 65 78 74 29 2c 20 7a eof(zContext), z
18dab 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 Context,.
18dac 20 20 20 20 20 20 22 4f 6e 20 74 72 65 65 20 70 "On tree p
18dad 61 67 65 20 25 64 20 63 65 6c 6c 20 25 64 3a 20 age %d cell %d:
18dae 22 2c 20 69 50 61 67 65 2c 20 69 29 3b 0a 20 20 ", iPage, i);.
18daf 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 pCell = findCe
18db0 6c 6c 28 70 50 61 67 65 2c 69 29 3b 0a 20 20 20 ll(pPage,i);.
18db1 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 btreeParseCellP
18db2 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c tr(pPage, pCell,
18db3 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 73 7a 20 &info);. sz
18db4 3d 20 69 6e 66 6f 2e 6e 44 61 74 61 3b 0a 20 20 = info.nData;.
18db5 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 69 6e if( !pPage->in
18db6 74 4b 65 79 20 29 20 73 7a 20 2b 3d 20 28 69 6e tKey ) sz += (in
18db7 74 29 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 20 t)info.nKey;.
18db8 20 61 73 73 65 72 74 28 20 73 7a 3d 3d 69 6e 66 assert( sz==inf
18db9 6f 2e 6e 50 61 79 6c 6f 61 64 20 29 3b 0a 20 20 o.nPayload );.
18dba 20 20 69 66 28 20 28 73 7a 3e 69 6e 66 6f 2e 6e if( (sz>info.n
18dbb 4c 6f 63 61 6c 29 20 0a 20 20 20 20 20 26 26 20 Local) . &&
18dbc 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 (&pCell[info.iOv
18dbd 65 72 66 6c 6f 77 5d 3c 3d 26 70 50 61 67 65 2d erflow]<=&pPage-
18dbe 3e 61 44 61 74 61 5b 70 42 74 2d 3e 75 73 61 62 >aData[pBt->usab
18dbf 6c 65 53 69 7a 65 5d 29 0a 20 20 20 20 29 7b 0a leSize]). ){.
18dc0 20 20 20 20 20 20 69 6e 74 20 6e 50 61 67 65 20 int nPage
18dc1 3d 20 28 73 7a 20 2d 20 69 6e 66 6f 2e 6e 4c 6f = (sz - info.nLo
18dc2 63 61 6c 20 2b 20 75 73 61 62 6c 65 53 69 7a 65 cal + usableSize
18dc3 20 2d 20 35 29 2f 28 75 73 61 62 6c 65 53 69 7a - 5)/(usableSiz
18dc4 65 20 2d 20 34 29 3b 0a 20 20 20 20 20 20 50 67 e - 4);. Pg
18dc5 6e 6f 20 70 67 6e 6f 4f 76 66 6c 20 3d 20 67 65 no pgnoOvfl = ge
18dc6 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e t4byte(&pCell[in
18dc7 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a fo.iOverflow]);.
18dc8 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
18dc9 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
18dca 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 if( pBt->au
18dcb 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 toVacuum ){.
18dcc 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 checkPtrmap(
18dcd 70 43 68 65 63 6b 2c 20 70 67 6e 6f 4f 76 66 6c pCheck, pgnoOvfl
18dce 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f , PTRMAP_OVERFLO
18dcf 57 31 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 W1, iPage, zCont
18dd0 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 ext);. }.#e
18dd1 6e 64 69 66 0a 20 20 20 20 20 20 63 68 65 63 6b ndif. check
18dd2 4c 69 73 74 28 70 43 68 65 63 6b 2c 20 30 2c 20 List(pCheck, 0,
18dd3 70 67 6e 6f 4f 76 66 6c 2c 20 6e 50 61 67 65 2c pgnoOvfl, nPage,
18dd4 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 zContext);.
18dd5 7d 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 }.. /* Check
18dd6 73 61 6e 69 74 79 20 6f 66 20 6c 65 66 74 20 63 sanity of left c
18dd7 68 69 6c 64 20 70 61 67 65 2e 0a 20 20 20 20 2a hild page.. *
18dd8 2f 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 /. if( !pPage
18dd9 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 ->leaf ){.
18dda 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 pgno = get4byte(
18ddb 70 43 65 6c 6c 29 3b 0a 23 69 66 6e 64 65 66 20 pCell);.#ifndef
18ddc 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
18ddd 56 41 43 55 55 4d 0a 20 20 20 20 20 20 69 66 28 VACUUM. if(
18dde 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d pBt->autoVacuum
18ddf 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 ){. chec
18de0 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 kPtrmap(pCheck,
18de1 70 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 pgno, PTRMAP_BTR
18de2 45 45 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 EE, iPage, zCont
18de3 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 ext);. }.#e
18de4 6e 64 69 66 0a 20 20 20 20 20 20 64 32 20 3d 20 ndif. d2 =
18de5 63 68 65 63 6b 54 72 65 65 50 61 67 65 28 70 43 checkTreePage(pC
18de6 68 65 63 6b 2c 20 70 67 6e 6f 2c 20 7a 43 6f 6e heck, pgno, zCon
18de7 74 65 78 74 29 3b 0a 20 20 20 20 20 20 69 66 28 text);. if(
18de8 20 69 3e 30 20 26 26 20 64 32 21 3d 64 65 70 74 i>0 && d2!=dept
18de9 68 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 h ){. che
18dea 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 ckAppendMsg(pChe
18deb 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 43 ck, zContext, "C
18dec 68 69 6c 64 20 70 61 67 65 20 64 65 70 74 68 20 hild page depth
18ded 64 69 66 66 65 72 73 22 29 3b 0a 20 20 20 20 20 differs");.
18dee 20 7d 0a 20 20 20 20 20 20 64 65 70 74 68 20 3d }. depth =
18def 20 64 32 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 d2;. }. }.
18df0 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 if( !pPage->lea
18df1 66 20 29 7b 0a 20 20 20 20 70 67 6e 6f 20 3d 20 f ){. pgno =
18df2 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d get4byte(&pPage-
18df3 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 >aData[pPage->hd
18df4 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 rOffset+8]);.
18df5 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
18df6 66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 65 78 f(sizeof(zContex
18df7 74 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a 20 t), zContext, .
18df8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18df9 20 20 20 20 22 4f 6e 20 70 61 67 65 20 25 64 20 "On page %d
18dfa 61 74 20 72 69 67 68 74 20 63 68 69 6c 64 3a 20 at right child:
18dfb 22 2c 20 69 50 61 67 65 29 3b 0a 23 69 66 6e 64 ", iPage);.#ifnd
18dfc 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
18dfd 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 UTOVACUUM. if
18dfe 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 ( pBt->autoVacuu
18dff 6d 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b m ){. check
18e00 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 70 Ptrmap(pCheck, p
18e01 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 gno, PTRMAP_BTRE
18e02 45 2c 20 69 50 61 67 65 2c 20 30 29 3b 0a 20 20 E, iPage, 0);.
18e03 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 }.#endif. c
18e04 68 65 63 6b 54 72 65 65 50 61 67 65 28 70 43 68 heckTreePage(pCh
18e05 65 63 6b 2c 20 70 67 6e 6f 2c 20 7a 43 6f 6e 74 eck, pgno, zCont
18e06 65 78 74 29 3b 0a 20 20 7d 0a 20 0a 20 20 2f 2a ext);. }. . /*
18e07 20 43 68 65 63 6b 20 66 6f 72 20 63 6f 6d 70 6c Check for compl
18e08 65 74 65 20 63 6f 76 65 72 61 67 65 20 6f 66 20 ete coverage of
18e09 74 68 65 20 70 61 67 65 0a 20 20 2a 2f 0a 20 20 the page. */.
18e0a 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 data = pPage->aD
18e0b 61 74 61 3b 0a 20 20 68 64 72 20 3d 20 70 50 61 ata;. hdr = pPa
18e0c 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 ge->hdrOffset;.
18e0d 20 68 69 74 20 3d 20 73 71 6c 69 74 65 33 50 61 hit = sqlite3Pa
18e0e 67 65 4d 61 6c 6c 6f 63 28 20 70 42 74 2d 3e 70 geMalloc( pBt->p
18e0f 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 69 66 28 ageSize );. if(
18e10 20 68 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 hit==0 ){. p
18e11 43 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 Check->mallocFai
18e12 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 led = 1;. }else
18e13 7b 0a 20 20 20 20 75 31 36 20 63 6f 6e 74 65 6e {. u16 conten
18e14 74 4f 66 66 73 65 74 20 3d 20 67 65 74 32 62 79 tOffset = get2by
18e15 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 te(&data[hdr+5])
18e16 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 6f ;. assert( co
18e17 6e 74 65 6e 74 4f 66 66 73 65 74 3c 3d 75 73 61 ntentOffset<=usa
18e18 62 6c 65 53 69 7a 65 20 29 3b 20 20 2f 2a 20 45 bleSize ); /* E
18e19 6e 66 6f 72 63 65 64 20 62 79 20 62 74 72 65 65 nforced by btree
18e1a 49 6e 69 74 50 61 67 65 28 29 20 2a 2f 0a 20 20 InitPage() */.
18e1b 20 20 6d 65 6d 73 65 74 28 68 69 74 2b 63 6f 6e memset(hit+con
18e1c 74 65 6e 74 4f 66 66 73 65 74 2c 20 30 2c 20 75 tentOffset, 0, u
18e1d 73 61 62 6c 65 53 69 7a 65 2d 63 6f 6e 74 65 6e sableSize-conten
18e1e 74 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 6d 65 tOffset);. me
18e1f 6d 73 65 74 28 68 69 74 2c 20 31 2c 20 63 6f 6e mset(hit, 1, con
18e20 74 65 6e 74 4f 66 66 73 65 74 29 3b 0a 20 20 20 tentOffset);.
18e21 20 6e 43 65 6c 6c 20 3d 20 67 65 74 32 62 79 74 nCell = get2byt
18e22 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 29 3b e(&data[hdr+3]);
18e23 0a 20 20 20 20 63 65 6c 6c 53 74 61 72 74 20 3d . cellStart =
18e24 20 68 64 72 20 2b 20 31 32 20 2d 20 34 2a 70 50 hdr + 12 - 4*pP
18e25 61 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 20 20 66 age->leaf;. f
18e26 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b or(i=0; i<nCell;
18e27 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 i++){. int
18e28 20 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 26 pc = get2byte(&
18e29 64 61 74 61 5b 63 65 6c 6c 53 74 61 72 74 2b 69 data[cellStart+i
18e2a 2a 32 5d 29 3b 0a 20 20 20 20 20 20 75 31 36 20 *2]);. u16
18e2b 73 69 7a 65 20 3d 20 31 30 32 34 3b 0a 20 20 20 size = 1024;.
18e2c 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 int j;.
18e2d 69 66 28 20 70 63 3c 3d 75 73 61 62 6c 65 53 69 if( pc<=usableSi
18e2e 7a 65 2d 34 20 29 7b 0a 20 20 20 20 20 20 20 20 ze-4 ){.
18e2f 73 69 7a 65 20 3d 20 63 65 6c 6c 53 69 7a 65 50 size = cellSizeP
18e30 74 72 28 70 50 61 67 65 2c 20 26 64 61 74 61 5b tr(pPage, &data[
18e31 70 63 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 pc]);. }.
18e32 20 20 20 20 69 66 28 20 28 70 63 2b 73 69 7a 65 if( (pc+size
18e33 2d 31 29 3e 3d 75 73 61 62 6c 65 53 69 7a 65 20 -1)>=usableSize
18e34 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b ){. check
18e35 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b AppendMsg(pCheck
18e36 2c 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 , 0, .
18e37 20 20 22 43 6f 72 72 75 70 74 69 6f 6e 20 64 65 "Corruption de
18e38 74 65 63 74 65 64 20 69 6e 20 63 65 6c 6c 20 25 tected in cell %
18e39 64 20 6f 6e 20 70 61 67 65 20 25 64 22 2c 69 2c d on page %d",i,
18e3a 69 50 61 67 65 2c 30 29 3b 0a 20 20 20 20 20 20 iPage,0);.
18e3b 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 66 }else{. f
18e3c 6f 72 28 6a 3d 70 63 2b 73 69 7a 65 2d 31 3b 20 or(j=pc+size-1;
18e3d 6a 3e 3d 70 63 3b 20 6a 2d 2d 29 20 68 69 74 5b j>=pc; j--) hit[
18e3e 6a 5d 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 j]++;. }.
18e3f 20 20 7d 0a 20 20 20 20 69 20 3d 20 67 65 74 32 }. i = get2
18e40 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 31 byte(&data[hdr+1
18e41 5d 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 69 ]);. while( i
18e42 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 >0 ){. int
18e43 73 69 7a 65 2c 20 6a 3b 0a 20 20 20 20 20 20 61 size, j;. a
18e44 73 73 65 72 74 28 20 69 3c 3d 75 73 61 62 6c 65 ssert( i<=usable
18e45 53 69 7a 65 2d 34 20 29 3b 20 20 20 20 20 2f 2a Size-4 ); /*
18e46 20 45 6e 66 6f 72 63 65 64 20 62 79 20 62 74 72 Enforced by btr
18e47 65 65 49 6e 69 74 50 61 67 65 28 29 20 2a 2f 0a eeInitPage() */.
18e48 20 20 20 20 20 20 73 69 7a 65 20 3d 20 67 65 74 size = get
18e49 32 62 79 74 65 28 26 64 61 74 61 5b 69 2b 32 5d 2byte(&data[i+2]
18e4a 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
18e4b 20 69 2b 73 69 7a 65 3c 3d 75 73 61 62 6c 65 53 i+size<=usableS
18e4c 69 7a 65 20 29 3b 20 20 2f 2a 20 45 6e 66 6f 72 ize ); /* Enfor
18e4d 63 65 64 20 62 79 20 62 74 72 65 65 49 6e 69 74 ced by btreeInit
18e4e 50 61 67 65 28 29 20 2a 2f 0a 20 20 20 20 20 20 Page() */.
18e4f 66 6f 72 28 6a 3d 69 2b 73 69 7a 65 2d 31 3b 20 for(j=i+size-1;
18e50 6a 3e 3d 69 3b 20 6a 2d 2d 29 20 68 69 74 5b 6a j>=i; j--) hit[j
18e51 5d 2b 2b 3b 0a 20 20 20 20 20 20 6a 20 3d 20 67 ]++;. j = g
18e52 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 69 5d et2byte(&data[i]
18e53 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
18e54 20 6a 3d 3d 30 20 7c 7c 20 6a 3e 69 2b 73 69 7a j==0 || j>i+siz
18e55 65 20 29 3b 20 20 2f 2a 20 45 6e 66 6f 72 63 65 e ); /* Enforce
18e56 64 20 62 79 20 62 74 72 65 65 49 6e 69 74 50 61 d by btreeInitPa
18e57 67 65 28 29 20 2a 2f 0a 20 20 20 20 20 20 61 73 ge() */. as
18e58 73 65 72 74 28 20 6a 3c 3d 75 73 61 62 6c 65 53 sert( j<=usableS
18e59 69 7a 65 2d 34 20 29 3b 20 20 20 2f 2a 20 45 6e ize-4 ); /* En
18e5a 66 6f 72 63 65 64 20 62 79 20 62 74 72 65 65 49 forced by btreeI
18e5b 6e 69 74 50 61 67 65 28 29 20 2a 2f 0a 20 20 20 nitPage() */.
18e5c 20 20 20 69 20 3d 20 6a 3b 0a 20 20 20 20 7d 0a i = j;. }.
18e5d 20 20 20 20 66 6f 72 28 69 3d 63 6e 74 3d 30 3b for(i=cnt=0;
18e5e 20 69 3c 75 73 61 62 6c 65 53 69 7a 65 3b 20 69 i<usableSize; i
18e5f 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 68 ++){. if( h
18e60 69 74 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 it[i]==0 ){.
18e61 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 cnt++;.
18e62 20 7d 65 6c 73 65 20 69 66 28 20 68 69 74 5b 69 }else if( hit[i
18e63 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20 20 20 63 ]>1 ){. c
18e64 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 heckAppendMsg(pC
18e65 68 65 63 6b 2c 20 30 2c 0a 20 20 20 20 20 20 20 heck, 0,.
18e66 20 20 20 22 4d 75 6c 74 69 70 6c 65 20 75 73 65 "Multiple use
18e67 73 20 66 6f 72 20 62 79 74 65 20 25 64 20 6f 66 s for byte %d of
18e68 20 70 61 67 65 20 25 64 22 2c 20 69 2c 20 69 50 page %d", i, iP
18e69 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 62 72 age);. br
18e6a 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 eak;. }.
18e6b 20 7d 0a 20 20 20 20 69 66 28 20 63 6e 74 21 3d }. if( cnt!=
18e6c 64 61 74 61 5b 68 64 72 2b 37 5d 20 29 7b 0a 20 data[hdr+7] ){.
18e6d 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 checkAppend
18e6e 4d 73 67 28 70 43 68 65 63 6b 2c 20 30 2c 20 0a Msg(pCheck, 0, .
18e6f 20 20 20 20 20 20 20 20 20 20 22 46 72 61 67 6d "Fragm
18e70 65 6e 74 61 74 69 6f 6e 20 6f 66 20 25 64 20 62 entation of %d b
18e71 79 74 65 73 20 72 65 70 6f 72 74 65 64 20 61 73 ytes reported as
18e72 20 25 64 20 6f 6e 20 70 61 67 65 20 25 64 22 2c %d on page %d",
18e73 0a 20 20 20 20 20 20 20 20 20 20 63 6e 74 2c 20 . cnt,
18e74 64 61 74 61 5b 68 64 72 2b 37 5d 2c 20 69 50 61 data[hdr+7], iPa
18e75 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 ge);. }. }.
18e76 20 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 sqlite3PageFree
18e77 28 68 69 74 29 3b 0a 20 20 72 65 6c 65 61 73 65 (hit);. release
18e78 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 72 Page(pPage);. r
18e79 65 74 75 72 6e 20 64 65 70 74 68 2b 31 3b 0a 7d eturn depth+1;.}
18e7a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
18e7b 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 E_OMIT_INTEGRITY
18e7c 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 _CHECK */..#ifnd
18e7d 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 ef SQLITE_OMIT_I
18e7e 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f NTEGRITY_CHECK./
18e7f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
18e80 65 20 64 6f 65 73 20 61 20 63 6f 6d 70 6c 65 74 e does a complet
18e81 65 20 63 68 65 63 6b 20 6f 66 20 74 68 65 20 67 e check of the g
18e82 69 76 65 6e 20 42 54 72 65 65 20 66 69 6c 65 2e iven BTree file.
18e83 20 20 61 52 6f 6f 74 5b 5d 20 69 73 0a 2a 2a 20 aRoot[] is.**
18e84 61 6e 20 61 72 72 61 79 20 6f 66 20 70 61 67 65 an array of page
18e85 73 20 6e 75 6d 62 65 72 73 20 77 65 72 65 20 65 s numbers were e
18e86 61 63 68 20 70 61 67 65 20 6e 75 6d 62 65 72 20 ach page number
18e87 69 73 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 is the root page
18e88 20 6f 66 0a 2a 2a 20 61 20 74 61 62 6c 65 2e 20 of.** a table.
18e89 20 6e 52 6f 6f 74 20 69 73 20 74 68 65 20 6e 75 nRoot is the nu
18e8a 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 mber of entries
18e8b 69 6e 20 61 52 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20 in aRoot..**.**
18e8c 41 20 72 65 61 64 2d 6f 6e 6c 79 20 6f 72 20 72 A read-only or r
18e8d 65 61 64 2d 77 72 69 74 65 20 74 72 61 6e 73 61 ead-write transa
18e8e 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 6f 70 ction must be op
18e8f 65 6e 65 64 20 62 65 66 6f 72 65 20 63 61 6c 6c ened before call
18e90 69 6e 67 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 ing.** this func
18e91 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 72 69 74 tion..**.** Writ
18e92 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 e the number of
18e93 65 72 72 6f 72 20 73 65 65 6e 20 69 6e 20 2a 70 error seen in *p
18e94 6e 45 72 72 2e 20 20 45 78 63 65 70 74 20 66 6f nErr. Except fo
18e95 72 20 73 6f 6d 65 20 6d 65 6d 6f 72 79 0a 2a 2a r some memory.**
18e96 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f allocation erro
18e97 72 73 2c 20 20 61 6e 20 65 72 72 6f 72 20 6d 65 rs, an error me
18e98 73 73 61 67 65 20 68 65 6c 64 20 69 6e 20 6d 65 ssage held in me
18e99 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 mory obtained fr
18e9a 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 69 73 20 om.** malloc is
18e9b 72 65 74 75 72 6e 65 64 20 69 66 20 2a 70 6e 45 returned if *pnE
18e9c 72 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 rr is non-zero.
18e9d 20 49 66 20 2a 70 6e 45 72 72 3d 3d 30 20 74 68 If *pnErr==0 th
18e9e 65 6e 20 4e 55 4c 4c 20 69 73 0a 2a 2a 20 72 65 en NULL is.** re
18e9f 74 75 72 6e 65 64 2e 20 20 49 66 20 61 20 6d 65 turned. If a me
18ea0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
18ea1 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 4e 55 error occurs, NU
18ea2 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a LL is returned..
18ea3 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
18ea4 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 E char *sqlite3B
18ea5 74 72 65 65 49 6e 74 65 67 72 69 74 79 43 68 65 treeIntegrityChe
18ea6 63 6b 28 0a 20 20 42 74 72 65 65 20 2a 70 2c 20 ck(. Btree *p,
18ea7 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65 /* The btree
18ea8 20 74 6f 20 62 65 20 63 68 65 63 6b 65 64 20 2a to be checked *
18ea9 2f 0a 20 20 69 6e 74 20 2a 61 52 6f 6f 74 2c 20 /. int *aRoot,
18eaa 20 20 2f 2a 20 41 6e 20 61 72 72 61 79 20 6f 66 /* An array of
18eab 20 72 6f 6f 74 20 70 61 67 65 73 20 6e 75 6d 62 root pages numb
18eac 65 72 73 20 66 6f 72 20 69 6e 64 69 76 69 64 75 ers for individu
18ead 61 6c 20 74 72 65 65 73 20 2a 2f 0a 20 20 69 6e al trees */. in
18eae 74 20 6e 52 6f 6f 74 2c 20 20 20 20 2f 2a 20 4e t nRoot, /* N
18eaf 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 umber of entries
18eb0 20 69 6e 20 61 52 6f 6f 74 5b 5d 20 2a 2f 0a 20 in aRoot[] */.
18eb1 20 69 6e 74 20 6d 78 45 72 72 2c 20 20 20 20 2f int mxErr, /
18eb2 2a 20 53 74 6f 70 20 72 65 70 6f 72 74 69 6e 67 * Stop reporting
18eb3 20 65 72 72 6f 72 73 20 61 66 74 65 72 20 74 68 errors after th
18eb4 69 73 20 6d 61 6e 79 20 2a 2f 0a 20 20 69 6e 74 is many */. int
18eb5 20 2a 70 6e 45 72 72 20 20 20 20 2f 2a 20 57 72 *pnErr /* Wr
18eb6 69 74 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 ite number of er
18eb7 72 6f 72 73 20 73 65 65 6e 20 74 6f 20 74 68 69 rors seen to thi
18eb8 73 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 29 7b s variable */.){
18eb9 0a 20 20 50 67 6e 6f 20 69 3b 0a 20 20 69 6e 74 . Pgno i;. int
18eba 20 6e 52 65 66 3b 0a 20 20 49 6e 74 65 67 72 69 nRef;. Integri
18ebb 74 79 43 6b 20 73 43 68 65 63 6b 3b 0a 20 20 42 tyCk sCheck;. B
18ebc 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
18ebd 2d 3e 70 42 74 3b 0a 20 20 63 68 61 72 20 7a 45 ->pBt;. char zE
18ebe 72 72 5b 31 30 30 5d 3b 0a 0a 20 20 73 71 6c 69 rr[100];.. sqli
18ebf 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 te3BtreeEnter(p)
18ec0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 ;. assert( p->i
18ec1 6e 54 72 61 6e 73 3e 54 52 41 4e 53 5f 4e 4f 4e nTrans>TRANS_NON
18ec2 45 20 26 26 20 70 42 74 2d 3e 69 6e 54 72 61 6e E && pBt->inTran
18ec3 73 61 63 74 69 6f 6e 3e 54 52 41 4e 53 5f 4e 4f saction>TRANS_NO
18ec4 4e 45 20 29 3b 0a 20 20 6e 52 65 66 20 3d 20 73 NE );. nRef = s
18ec5 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f qlite3PagerRefco
18ec6 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29 unt(pBt->pPager)
18ec7 3b 0a 20 20 73 43 68 65 63 6b 2e 70 42 74 20 3d ;. sCheck.pBt =
18ec8 20 70 42 74 3b 0a 20 20 73 43 68 65 63 6b 2e 70 pBt;. sCheck.p
18ec9 50 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 50 61 Pager = pBt->pPa
18eca 67 65 72 3b 0a 20 20 73 43 68 65 63 6b 2e 6e 50 ger;. sCheck.nP
18ecb 61 67 65 20 3d 20 70 61 67 65 72 50 61 67 65 63 age = pagerPagec
18ecc 6f 75 6e 74 28 73 43 68 65 63 6b 2e 70 42 74 29 ount(sCheck.pBt)
18ecd 3b 0a 20 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 ;. sCheck.mxErr
18ece 20 3d 20 6d 78 45 72 72 3b 0a 20 20 73 43 68 65 = mxErr;. sChe
18ecf 63 6b 2e 6e 45 72 72 20 3d 20 30 3b 0a 20 20 73 ck.nErr = 0;. s
18ed0 43 68 65 63 6b 2e 6d 61 6c 6c 6f 63 46 61 69 6c Check.mallocFail
18ed1 65 64 20 3d 20 30 3b 0a 20 20 2a 70 6e 45 72 72 ed = 0;. *pnErr
18ed2 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 43 68 65 = 0;. if( sChe
18ed3 63 6b 2e 6e 50 61 67 65 3d 3d 30 20 29 7b 0a 20 ck.nPage==0 ){.
18ed4 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c sqlite3BtreeL
18ed5 65 61 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74 eave(p);. ret
18ed6 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 73 43 68 urn 0;. }. sCh
18ed7 65 63 6b 2e 61 6e 52 65 66 20 3d 20 73 71 6c 69 eck.anRef = sqli
18ed8 74 65 33 4d 61 6c 6c 6f 63 28 20 28 73 43 68 65 te3Malloc( (sChe
18ed9 63 6b 2e 6e 50 61 67 65 2b 31 29 2a 73 69 7a 65 ck.nPage+1)*size
18eda 6f 66 28 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b of(sCheck.anRef[
18edb 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 21 73 43 0]) );. if( !sC
18edc 68 65 63 6b 2e 61 6e 52 65 66 20 29 7b 0a 20 20 heck.anRef ){.
18edd 20 20 2a 70 6e 45 72 72 20 3d 20 31 3b 0a 20 20 *pnErr = 1;.
18ede 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
18edf 61 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74 75 ave(p);. retu
18ee0 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 rn 0;. }. for(
18ee1 69 3d 30 3b 20 69 3c 3d 73 43 68 65 63 6b 2e 6e i=0; i<=sCheck.n
18ee2 50 61 67 65 3b 20 69 2b 2b 29 7b 20 73 43 68 65 Page; i++){ sChe
18ee3 63 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d 20 30 3b ck.anRef[i] = 0;
18ee4 20 7d 0a 20 20 69 20 3d 20 50 45 4e 44 49 4e 47 }. i = PENDING
18ee5 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 3b _BYTE_PAGE(pBt);
18ee6 0a 20 20 69 66 28 20 69 3c 3d 73 43 68 65 63 6b . if( i<=sCheck
18ee7 2e 6e 50 61 67 65 20 29 7b 0a 20 20 20 20 73 43 .nPage ){. sC
18ee8 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d 20 heck.anRef[i] =
18ee9 31 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 1;. }. sqlite3
18eea 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26 73 43 StrAccumInit(&sC
18eeb 68 65 63 6b 2e 65 72 72 4d 73 67 2c 20 7a 45 72 heck.errMsg, zEr
18eec 72 2c 20 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c r, sizeof(zErr),
18eed 20 32 30 30 30 30 29 3b 0a 0a 20 20 2f 2a 20 43 20000);.. /* C
18eee 68 65 63 6b 20 74 68 65 20 69 6e 74 65 67 72 69 heck the integri
18eef 74 79 20 6f 66 20 74 68 65 20 66 72 65 65 6c 69 ty of the freeli
18ef0 73 74 0a 20 20 2a 2f 0a 20 20 63 68 65 63 6b 4c st. */. checkL
18ef1 69 73 74 28 26 73 43 68 65 63 6b 2c 20 31 2c 20 ist(&sCheck, 1,
18ef2 67 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 get4byte(&pBt->p
18ef3 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d Page1->aData[32]
18ef4 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 ),. g
18ef5 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 et4byte(&pBt->pP
18ef6 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 age1->aData[36])
18ef7 2c 20 22 4d 61 69 6e 20 66 72 65 65 6c 69 73 74 , "Main freelist
18ef8 3a 20 22 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 : ");.. /* Chec
18ef9 6b 20 61 6c 6c 20 74 68 65 20 74 61 62 6c 65 73 k all the tables
18efa 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 .. */. for(i=0
18efb 3b 20 28 69 6e 74 29 69 3c 6e 52 6f 6f 74 20 26 ; (int)i<nRoot &
18efc 26 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 3b 20 & sCheck.mxErr;
18efd 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 52 i++){. if( aR
18efe 6f 6f 74 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 oot[i]==0 ) cont
18eff 69 6e 75 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 inue;.#ifndef SQ
18f00 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
18f01 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70 42 74 CUUM. if( pBt
18f02 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 26 26 20 ->autoVacuum &&
18f03 61 52 6f 6f 74 5b 69 5d 3e 31 20 29 7b 0a 20 20 aRoot[i]>1 ){.
18f04 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 checkPtrmap(
18f05 26 73 43 68 65 63 6b 2c 20 61 52 6f 6f 74 5b 69 &sCheck, aRoot[i
18f06 5d 2c 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 ], PTRMAP_ROOTPA
18f07 47 45 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 7d GE, 0, 0);. }
18f08 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 68 65 63 .#endif. chec
18f09 6b 54 72 65 65 50 61 67 65 28 26 73 43 68 65 63 kTreePage(&sChec
18f0a 6b 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 22 4c 69 k, aRoot[i], "Li
18f0b 73 74 20 6f 66 20 74 72 65 65 20 72 6f 6f 74 73 st of tree roots
18f0c 3a 20 22 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 : ");. }.. /*
18f0d 4d 61 6b 65 20 73 75 72 65 20 65 76 65 72 79 20 Make sure every
18f0e 70 61 67 65 20 69 6e 20 74 68 65 20 66 69 6c 65 page in the file
18f0f 20 69 73 20 72 65 66 65 72 65 6e 63 65 64 0a 20 is referenced.
18f10 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 */. for(i=1; i
18f11 3c 3d 73 43 68 65 63 6b 2e 6e 50 61 67 65 20 26 <=sCheck.nPage &
18f12 26 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 3b 20 & sCheck.mxErr;
18f13 69 2b 2b 29 7b 0a 23 69 66 64 65 66 20 53 51 4c i++){.#ifdef SQL
18f14 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
18f15 55 55 4d 0a 20 20 20 20 69 66 28 20 73 43 68 65 UUM. if( sChe
18f16 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30 20 29 ck.anRef[i]==0 )
18f17 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 {. checkApp
18f18 65 6e 64 4d 73 67 28 26 73 43 68 65 63 6b 2c 20 endMsg(&sCheck,
18f19 30 2c 20 22 50 61 67 65 20 25 64 20 69 73 20 6e 0, "Page %d is n
18f1a 65 76 65 72 20 75 73 65 64 22 2c 20 69 29 3b 0a ever used", i);.
18f1b 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 }.#else.
18f1c 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 /* If the databa
18f1d 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 74 6f se supports auto
18f1e 2d 76 61 63 75 75 6d 2c 20 6d 61 6b 65 20 73 75 -vacuum, make su
18f1f 72 65 20 6e 6f 20 74 61 62 6c 65 73 20 63 6f 6e re no tables con
18f20 74 61 69 6e 0a 20 20 20 20 2a 2a 20 72 65 66 65 tain. ** refe
18f21 72 65 6e 63 65 73 20 74 6f 20 70 6f 69 6e 74 65 rences to pointe
18f22 72 2d 6d 61 70 20 70 61 67 65 73 2e 0a 20 20 20 r-map pages..
18f23 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 43 68 65 */. if( sChe
18f24 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30 20 26 ck.anRef[i]==0 &
18f25 26 20 0a 20 20 20 20 20 20 20 28 50 54 52 4d 41 & . (PTRMA
18f26 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 69 29 P_PAGENO(pBt, i)
18f27 21 3d 69 20 7c 7c 20 21 70 42 74 2d 3e 61 75 74 !=i || !pBt->aut
18f28 6f 56 61 63 75 75 6d 29 20 29 7b 0a 20 20 20 20 oVacuum) ){.
18f29 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 checkAppendMsg
18f2a 28 26 73 43 68 65 63 6b 2c 20 30 2c 20 22 50 61 (&sCheck, 0, "Pa
18f2b 67 65 20 25 64 20 69 73 20 6e 65 76 65 72 20 75 ge %d is never u
18f2c 73 65 64 22 2c 20 69 29 3b 0a 20 20 20 20 7d 0a sed", i);. }.
18f2d 20 20 20 20 69 66 28 20 73 43 68 65 63 6b 2e 61 if( sCheck.a
18f2e 6e 52 65 66 5b 69 5d 21 3d 30 20 26 26 20 0a 20 nRef[i]!=0 && .
18f2f 20 20 20 20 20 20 28 50 54 52 4d 41 50 5f 50 41 (PTRMAP_PA
18f30 47 45 4e 4f 28 70 42 74 2c 20 69 29 3d 3d 69 20 GENO(pBt, i)==i
18f31 26 26 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 && pBt->autoVacu
18f32 75 6d 29 20 29 7b 0a 20 20 20 20 20 20 63 68 65 um) ){. che
18f33 63 6b 41 70 70 65 6e 64 4d 73 67 28 26 73 43 68 ckAppendMsg(&sCh
18f34 65 63 6b 2c 20 30 2c 20 22 50 6f 69 6e 74 65 72 eck, 0, "Pointer
18f35 20 6d 61 70 20 70 61 67 65 20 25 64 20 69 73 20 map page %d is
18f36 72 65 66 65 72 65 6e 63 65 64 22 2c 20 69 29 3b referenced", i);
18f37 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 . }.#endif.
18f38 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 }.. /* Make sur
18f39 65 20 74 68 69 73 20 61 6e 61 6c 79 73 69 73 20 e this analysis
18f3a 64 69 64 20 6e 6f 74 20 6c 65 61 76 65 20 61 6e did not leave an
18f3b 79 20 75 6e 72 65 66 28 29 20 70 61 67 65 73 2e y unref() pages.
18f3c 0a 20 20 2a 2a 20 54 68 69 73 20 69 73 20 61 6e . ** This is an
18f3d 20 69 6e 74 65 72 6e 61 6c 20 63 6f 6e 73 69 73 internal consis
18f3e 74 65 6e 63 79 20 63 68 65 63 6b 3b 20 61 6e 20 tency check; an
18f3f 69 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b 0a integrity check.
18f40 20 20 2a 2a 20 6f 66 20 74 68 65 20 69 6e 74 65 ** of the inte
18f41 67 72 69 74 79 20 63 68 65 63 6b 2e 0a 20 20 2a grity check.. *
18f42 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 6e 52 /. if( NEVER(nR
18f43 65 66 20 21 3d 20 73 71 6c 69 74 65 33 50 61 67 ef != sqlite3Pag
18f44 65 72 52 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e erRefcount(pBt->
18f45 70 50 61 67 65 72 29 29 20 29 7b 0a 20 20 20 20 pPager)) ){.
18f46 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 26 checkAppendMsg(&
18f47 73 43 68 65 63 6b 2c 20 30 2c 20 0a 20 20 20 20 sCheck, 0, .
18f48 20 20 22 4f 75 74 73 74 61 6e 64 69 6e 67 20 70 "Outstanding p
18f49 61 67 65 20 63 6f 75 6e 74 20 67 6f 65 73 20 66 age count goes f
18f4a 72 6f 6d 20 25 64 20 74 6f 20 25 64 20 64 75 72 rom %d to %d dur
18f4b 69 6e 67 20 74 68 69 73 20 61 6e 61 6c 79 73 69 ing this analysi
18f4c 73 22 2c 0a 20 20 20 20 20 20 6e 52 65 66 2c 20 s",. nRef,
18f4d 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 sqlite3PagerRefc
18f4e 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 ount(pBt->pPager
18f4f 29 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 0a 20 20 ). );. }..
18f50 2f 2a 20 43 6c 65 61 6e 20 20 75 70 20 61 6e 64 /* Clean up and
18f51 20 72 65 70 6f 72 74 20 65 72 72 6f 72 73 2e 0a report errors..
18f52 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 74 */. sqlite3Bt
18f53 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 73 reeLeave(p);. s
18f54 71 6c 69 74 65 33 5f 66 72 65 65 28 73 43 68 65 qlite3_free(sChe
18f55 63 6b 2e 61 6e 52 65 66 29 3b 0a 20 20 69 66 28 ck.anRef);. if(
18f56 20 73 43 68 65 63 6b 2e 6d 61 6c 6c 6f 63 46 61 sCheck.mallocFa
18f57 69 6c 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 iled ){. sqli
18f58 74 65 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 te3StrAccumReset
18f59 28 26 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 29 (&sCheck.errMsg)
18f5a 3b 0a 20 20 20 20 2a 70 6e 45 72 72 20 3d 20 73 ;. *pnErr = s
18f5b 43 68 65 63 6b 2e 6e 45 72 72 2b 31 3b 0a 20 20 Check.nErr+1;.
18f5c 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
18f5d 20 20 2a 70 6e 45 72 72 20 3d 20 73 43 68 65 63 *pnErr = sChec
18f5e 6b 2e 6e 45 72 72 3b 0a 20 20 69 66 28 20 73 43 k.nErr;. if( sC
18f5f 68 65 63 6b 2e 6e 45 72 72 3d 3d 30 20 29 20 73 heck.nErr==0 ) s
18f60 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 65 qlite3StrAccumRe
18f61 73 65 74 28 26 73 43 68 65 63 6b 2e 65 72 72 4d set(&sCheck.errM
18f62 73 67 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 sg);. return sq
18f63 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e lite3StrAccumFin
18f64 69 73 68 28 26 73 43 68 65 63 6b 2e 65 72 72 4d ish(&sCheck.errM
18f65 73 67 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a sg);.}.#endif /*
18f66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 SQLITE_OMIT_INT
18f67 45 47 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a EGRITY_CHECK */.
18f68 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
18f69 65 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20 e full pathname
18f6a 6f 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e of the underlyin
18f6b 67 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e g database file.
18f6c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 .**.** The pager
18f6d 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 69 6e 76 filename is inv
18f6e 61 72 69 61 6e 74 20 61 73 20 6c 6f 6e 67 20 61 ariant as long a
18f6f 73 20 74 68 65 20 70 61 67 65 72 20 69 73 0a 2a s the pager is.*
18f70 2a 20 6f 70 65 6e 20 73 6f 20 69 74 20 69 73 20 * open so it is
18f71 73 61 66 65 20 74 6f 20 61 63 63 65 73 73 20 77 safe to access w
18f72 69 74 68 6f 75 74 20 74 68 65 20 42 74 53 68 61 ithout the BtSha
18f73 72 65 64 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51 red mutex..*/.SQ
18f74 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e LITE_PRIVATE con
18f75 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 st char *sqlite3
18f76 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 BtreeGetFilename
18f77 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 (Btree *p){. as
18f78 73 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 70 50 sert( p->pBt->pP
18f79 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 72 65 74 ager!=0 );. ret
18f7a 75 72 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72 urn sqlite3Pager
18f7b 46 69 6c 65 6e 61 6d 65 28 70 2d 3e 70 42 74 2d Filename(p->pBt-
18f7c 3e 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a >pPager);.}../*.
18f7d 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70 61 ** Return the pa
18f7e 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f thname of the jo
18f7f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 74 urnal file for t
18f80 68 69 73 20 64 61 74 61 62 61 73 65 2e 20 54 68 his database. Th
18f81 65 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 e return.** valu
18f82 65 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e e of this routin
18f83 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 72 65 e is the same re
18f84 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 gardless of whet
18f85 68 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 her the journal
18f86 66 69 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65 6e file.** has been
18f87 20 63 72 65 61 74 65 64 20 6f 72 20 6e 6f 74 2e created or not.
18f88 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 .**.** The pager
18f89 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d journal filenam
18f8a 65 20 69 73 20 69 6e 76 61 72 69 61 6e 74 20 61 e is invariant a
18f8b 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 70 61 s long as the pa
18f8c 67 65 72 20 69 73 0a 2a 2a 20 6f 70 65 6e 20 73 ger is.** open s
18f8d 6f 20 69 74 20 69 73 20 73 61 66 65 20 74 6f 20 o it is safe to
18f8e 61 63 63 65 73 73 20 77 69 74 68 6f 75 74 20 74 access without t
18f8f 68 65 20 42 74 53 68 61 72 65 64 20 6d 75 74 65 he BtShared mute
18f90 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 x..*/.SQLITE_PRI
18f91 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 VATE const char
18f92 2a 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 *sqlite3BtreeGet
18f93 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 42 74 72 65 Journalname(Btre
18f94 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 e *p){. assert(
18f95 20 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 21 p->pBt->pPager!
18f96 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 =0 );. return s
18f97 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e qlite3PagerJourn
18f98 61 6c 6e 61 6d 65 28 70 2d 3e 70 42 74 2d 3e 70 alname(p->pBt->p
18f99 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a Pager);.}../*.**
18f9a 20 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f Return non-zero
18f9b 20 69 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f if a transactio
18f9c 6e 20 69 73 20 61 63 74 69 76 65 2e 0a 2a 2f 0a n is active..*/.
18f9d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
18f9e 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 nt sqlite3BtreeI
18f9f 73 49 6e 54 72 61 6e 73 28 42 74 72 65 65 20 2a sInTrans(Btree *
18fa0 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 3d p){. assert( p=
18fa1 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 =0 || sqlite3_mu
18fa2 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e tex_held(p->db->
18fa3 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75 mutex) );. retu
18fa4 72 6e 20 28 70 20 26 26 20 28 70 2d 3e 69 6e 54 rn (p && (p->inT
18fa5 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 rans==TRANS_WRIT
18fa6 45 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 E));.}../*.** Re
18fa7 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 turn non-zero if
18fa8 20 61 20 72 65 61 64 20 28 6f 72 20 77 72 69 74 a read (or writ
18fa9 65 29 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 e) transaction i
18faa 73 20 61 63 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c s active..*/.SQL
18fab 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
18fac 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e sqlite3BtreeIsIn
18fad 52 65 61 64 54 72 61 6e 73 28 42 74 72 65 65 20 ReadTrans(Btree
18fae 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 *p){. assert( p
18faf 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
18fb0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
18fb1 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 (p->db->mutex) )
18fb2 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 69 6e ;. return p->in
18fb3 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e Trans!=TRANS_NON
18fb4 45 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 E;.}..SQLITE_PRI
18fb5 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
18fb6 42 74 72 65 65 49 73 49 6e 42 61 63 6b 75 70 28 BtreeIsInBackup(
18fb7 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 Btree *p){. ass
18fb8 65 72 74 28 20 70 20 29 3b 0a 20 20 61 73 73 65 ert( p );. asse
18fb9 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
18fba 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 x_held(p->db->mu
18fbb 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75 72 6e tex) );. return
18fbc 20 70 2d 3e 6e 42 61 63 6b 75 70 21 3d 30 3b 0a p->nBackup!=0;.
18fbd 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 }../*.** This fu
18fbe 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 nction returns a
18fbf 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 62 6c pointer to a bl
18fc0 6f 62 20 6f 66 20 6d 65 6d 6f 72 79 20 61 73 73 ob of memory ass
18fc1 6f 63 69 61 74 65 64 20 77 69 74 68 0a 2a 2a 20 ociated with.**
18fc2 61 20 73 69 6e 67 6c 65 20 73 68 61 72 65 64 2d a single shared-
18fc3 62 74 72 65 65 2e 20 54 68 65 20 6d 65 6d 6f 72 btree. The memor
18fc4 79 20 69 73 20 75 73 65 64 20 62 79 20 63 6c 69 y is used by cli
18fc5 65 6e 74 20 63 6f 64 65 20 66 6f 72 20 69 74 73 ent code for its
18fc6 20 6f 77 6e 0a 2a 2a 20 70 75 72 70 6f 73 65 73 own.** purposes
18fc7 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 (for example, t
18fc8 6f 20 73 74 6f 72 65 20 61 20 68 69 67 68 2d 6c o store a high-l
18fc9 65 76 65 6c 20 73 63 68 65 6d 61 20 61 73 73 6f evel schema asso
18fca 63 69 61 74 65 64 20 77 69 74 68 20 0a 2a 2a 20 ciated with .**
18fcb 74 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 the shared-btree
18fcc 29 2e 20 54 68 65 20 62 74 72 65 65 20 6c 61 79 ). The btree lay
18fcd 65 72 20 6d 61 6e 61 67 65 73 20 72 65 66 65 72 er manages refer
18fce 65 6e 63 65 20 63 6f 75 6e 74 69 6e 67 20 69 73 ence counting is
18fcf 73 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 sues..**.** The
18fd0 66 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 first time this
18fd1 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20 73 is called on a s
18fd2 68 61 72 65 64 2d 62 74 72 65 65 2c 20 6e 42 79 hared-btree, nBy
18fd3 74 65 73 20 62 79 74 65 73 20 6f 66 20 6d 65 6d tes bytes of mem
18fd4 6f 72 79 0a 2a 2a 20 61 72 65 20 61 6c 6c 6f 63 ory.** are alloc
18fd5 61 74 65 64 2c 20 7a 65 72 6f 65 64 2c 20 61 6e ated, zeroed, an
18fd6 64 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 d returned to th
18fd7 65 20 63 61 6c 6c 65 72 2e 20 46 6f 72 20 65 61 e caller. For ea
18fd8 63 68 20 73 75 62 73 65 71 75 65 6e 74 20 0a 2a ch subsequent .*
18fd9 2a 20 63 61 6c 6c 20 74 68 65 20 6e 42 79 74 65 * call the nByte
18fda 73 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 69 s parameter is i
18fdb 67 6e 6f 72 65 64 20 61 6e 64 20 61 20 70 6f 69 gnored and a poi
18fdc 6e 74 65 72 20 74 6f 20 74 68 65 20 73 61 6d 65 nter to the same
18fdd 20 62 6c 6f 62 0a 2a 2a 20 6f 66 20 6d 65 6d 6f blob.** of memo
18fde 72 79 20 72 65 74 75 72 6e 65 64 2e 20 0a 2a 2a ry returned. .**
18fdf 0a 2a 2a 20 49 66 20 74 68 65 20 6e 42 79 74 65 .** If the nByte
18fe0 73 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 30 s parameter is 0
18fe1 20 61 6e 64 20 74 68 65 20 62 6c 6f 62 20 6f 66 and the blob of
18fe2 20 6d 65 6d 6f 72 79 20 68 61 73 20 6e 6f 74 20 memory has not
18fe3 79 65 74 20 62 65 65 6e 0a 2a 2a 20 61 6c 6c 6f yet been.** allo
18fe4 63 61 74 65 64 2c 20 61 20 6e 75 6c 6c 20 70 6f cated, a null po
18fe5 69 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 inter is returne
18fe6 64 2e 20 49 66 20 74 68 65 20 62 6c 6f 62 20 68 d. If the blob h
18fe7 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 0a as already been.
18fe8 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20 69 74 ** allocated, it
18fe9 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 73 20 is returned as
18fea 6e 6f 72 6d 61 6c 2e 0a 2a 2a 0a 2a 2a 20 4a 75 normal..**.** Ju
18feb 73 74 20 62 65 66 6f 72 65 20 74 68 65 20 73 68 st before the sh
18fec 61 72 65 64 2d 62 74 72 65 65 20 69 73 20 63 6c ared-btree is cl
18fed 6f 73 65 64 2c 20 74 68 65 20 66 75 6e 63 74 69 osed, the functi
18fee 6f 6e 20 70 61 73 73 65 64 20 61 73 20 74 68 65 on passed as the
18fef 20 0a 2a 2a 20 78 46 72 65 65 20 61 72 67 75 6d .** xFree argum
18ff0 65 6e 74 20 77 68 65 6e 20 74 68 65 20 6d 65 6d ent when the mem
18ff1 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 ory allocation w
18ff2 61 73 20 6d 61 64 65 20 69 73 20 69 6e 76 6f 6b as made is invok
18ff3 65 64 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 62 6c ed on the .** bl
18ff4 6f 62 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 ob of allocated
18ff5 6d 65 6d 6f 72 79 2e 20 54 68 69 73 20 66 75 6e memory. This fun
18ff6 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 74 ction should not
18ff7 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 66 72 call sqlite3_fr
18ff8 65 65 28 29 0a 2a 2a 20 6f 6e 20 74 68 65 20 6d ee().** on the m
18ff9 65 6d 6f 72 79 2c 20 74 68 65 20 62 74 72 65 65 emory, the btree
18ffa 20 6c 61 79 65 72 20 64 6f 65 73 20 74 68 61 74 layer does that
18ffb 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
18ffc 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 ATE void *sqlite
18ffd 33 42 74 72 65 65 53 63 68 65 6d 61 28 42 74 72 3BtreeSchema(Btr
18ffe 65 65 20 2a 70 2c 20 69 6e 74 20 6e 42 79 74 65 ee *p, int nByte
18fff 73 2c 20 76 6f 69 64 28 2a 78 46 72 65 65 29 28 s, void(*xFree)(
19000 76 6f 69 64 20 2a 29 29 7b 0a 20 20 42 74 53 68 void *)){. BtSh
19001 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 ared *pBt = p->p
19002 42 74 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 Bt;. sqlite3Btr
19003 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 eeEnter(p);. if
19004 28 20 21 70 42 74 2d 3e 70 53 63 68 65 6d 61 20 ( !pBt->pSchema
19005 26 26 20 6e 42 79 74 65 73 20 29 7b 0a 20 20 20 && nBytes ){.
19006 20 70 42 74 2d 3e 70 53 63 68 65 6d 61 20 3d 20 pBt->pSchema =
19007 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 sqlite3MallocZer
19008 6f 28 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 70 o(nBytes);. p
19009 42 74 2d 3e 78 46 72 65 65 53 63 68 65 6d 61 20 Bt->xFreeSchema
1900a 3d 20 78 46 72 65 65 3b 0a 20 20 7d 0a 20 20 73 = xFree;. }. s
1900b 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
1900c 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 42 (p);. return pB
1900d 74 2d 3e 70 53 63 68 65 6d 61 3b 0a 7d 0a 0a 2f t->pSchema;.}../
1900e 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 *.** Return SQLI
1900f 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 TE_LOCKED_SHARED
19010 43 41 43 48 45 20 69 66 20 61 6e 6f 74 68 65 72 CACHE if another
19011 20 75 73 65 72 20 6f 66 20 74 68 65 20 73 61 6d user of the sam
19012 65 20 73 68 61 72 65 64 20 0a 2a 2a 20 62 74 72 e shared .** btr
19013 65 65 20 61 73 20 74 68 65 20 61 72 67 75 6d 65 ee as the argume
19014 6e 74 20 68 61 6e 64 6c 65 20 68 6f 6c 64 73 20 nt handle holds
19015 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 an exclusive loc
19016 6b 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 73 71 6c k on the .** sql
19017 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 ite_master table
19018 2e 20 4f 74 68 65 72 77 69 73 65 20 53 51 4c 49 . Otherwise SQLI
19019 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 TE_OK..*/.SQLITE
1901a 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1901b 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 4c ite3BtreeSchemaL
1901c 6f 63 6b 65 64 28 42 74 72 65 65 20 2a 70 29 7b ocked(Btree *p){
1901d 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 . int rc;. ass
1901e 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
1901f 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d ex_held(p->db->m
19020 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 utex) );. sqlit
19021 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
19022 0a 20 20 72 63 20 3d 20 71 75 65 72 79 53 68 61 . rc = querySha
19023 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 redCacheTableLoc
19024 6b 28 70 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 k(p, MASTER_ROOT
19025 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 , READ_LOCK);.
19026 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 assert( rc==SQLI
19027 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51 4c TE_OK || rc==SQL
19028 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 ITE_LOCKED_SHARE
19029 44 43 41 43 48 45 20 29 3b 0a 20 20 73 71 6c 69 DCACHE );. sqli
1902a 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
1902b 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
1902c 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ...#ifndef SQLIT
1902d 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
1902e 43 48 45 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e CHE./*.** Obtain
1902f 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74 a lock on the t
19030 61 62 6c 65 20 77 68 6f 73 65 20 72 6f 6f 74 20 able whose root
19031 70 61 67 65 20 69 73 20 69 54 61 62 2e 20 20 54 page is iTab. T
19032 68 65 0a 2a 2a 20 6c 6f 63 6b 20 69 73 20 61 20 he.** lock is a
19033 77 72 69 74 65 20 6c 6f 63 6b 20 69 66 20 69 73 write lock if is
19034 57 72 69 74 65 6c 6f 63 6b 20 69 73 20 74 72 75 Writelock is tru
19035 65 20 6f 72 20 61 20 72 65 61 64 20 6c 6f 63 6b e or a read lock
19036 0a 2a 2a 20 69 66 20 69 74 20 69 73 20 66 61 6c .** if it is fal
19037 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 se..*/.SQLITE_PR
19038 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
19039 33 42 74 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28 3BtreeLockTable(
1903a 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 54 Btree *p, int iT
1903b 61 62 2c 20 75 38 20 69 73 57 72 69 74 65 4c 6f ab, u8 isWriteLo
1903c 63 6b 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 ck){. int rc =
1903d 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 SQLITE_OK;. ass
1903e 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 21 ert( p->inTrans!
1903f 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b 0a 20 =TRANS_NONE );.
19040 20 69 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 if( p->sharable
19041 20 29 7b 0a 20 20 20 20 75 38 20 6c 6f 63 6b 54 ){. u8 lockT
19042 79 70 65 20 3d 20 52 45 41 44 5f 4c 4f 43 4b 20 ype = READ_LOCK
19043 2b 20 69 73 57 72 69 74 65 4c 6f 63 6b 3b 0a 20 + isWriteLock;.
19044 20 20 20 61 73 73 65 72 74 28 20 52 45 41 44 5f assert( READ_
19045 4c 4f 43 4b 2b 31 3d 3d 57 52 49 54 45 5f 4c 4f LOCK+1==WRITE_LO
19046 43 4b 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 CK );. assert
19047 28 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d 3d 30 ( isWriteLock==0
19048 20 7c 7c 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d || isWriteLock=
19049 3d 31 20 29 3b 0a 0a 20 20 20 20 73 71 6c 69 74 =1 );.. sqlit
1904a 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
1904b 0a 20 20 20 20 72 63 20 3d 20 71 75 65 72 79 53 . rc = queryS
1904c 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c haredCacheTableL
1904d 6f 63 6b 28 70 2c 20 69 54 61 62 2c 20 6c 6f 63 ock(p, iTab, loc
1904e 6b 54 79 70 65 29 3b 0a 20 20 20 20 69 66 28 20 kType);. if(
1904f 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
19050 0a 20 20 20 20 20 20 72 63 20 3d 20 73 65 74 53 . rc = setS
19051 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c haredCacheTableL
19052 6f 63 6b 28 70 2c 20 69 54 61 62 2c 20 6c 6f 63 ock(p, iTab, loc
19053 6b 54 79 70 65 29 3b 0a 20 20 20 20 7d 0a 20 20 kType);. }.
19054 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
19055 61 76 65 28 70 29 3b 0a 20 20 7d 0a 20 20 72 65 ave(p);. }. re
19056 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 turn rc;.}.#endi
19057 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 f..#ifndef SQLIT
19058 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a E_OMIT_INCRBLOB.
19059 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 70 /*.** Argument p
1905a 43 73 72 20 6d 75 73 74 20 62 65 20 61 20 63 75 Csr must be a cu
1905b 72 73 6f 72 20 6f 70 65 6e 65 64 20 66 6f 72 20 rsor opened for
1905c 77 72 69 74 69 6e 67 20 6f 6e 20 61 6e 20 0a 2a writing on an .*
1905d 2a 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 20 63 * INTKEY table c
1905e 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e urrently pointin
1905f 67 20 61 74 20 61 20 76 61 6c 69 64 20 74 61 62 g at a valid tab
19060 6c 65 20 65 6e 74 72 79 2e 20 0a 2a 2a 20 54 68 le entry. .** Th
19061 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 6f 64 69 is function modi
19062 66 69 65 73 20 74 68 65 20 64 61 74 61 20 73 74 fies the data st
19063 6f 72 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 ored as part of
19064 74 68 61 74 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a that entry..**.*
19065 2a 20 4f 6e 6c 79 20 74 68 65 20 64 61 74 61 20 * Only the data
19066 63 6f 6e 74 65 6e 74 20 6d 61 79 20 6f 6e 6c 79 content may only
19067 20 62 65 20 6d 6f 64 69 66 69 65 64 2c 20 69 74 be modified, it
19068 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 is not possible
19069 20 74 6f 20 0a 2a 2a 20 63 68 61 6e 67 65 20 74 to .** change t
1906a 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 he length of the
1906b 20 64 61 74 61 20 73 74 6f 72 65 64 2e 20 49 66 data stored. If
1906c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
1906d 73 20 63 61 6c 6c 65 64 20 77 69 74 68 0a 2a 2a s called with.**
1906e 20 70 61 72 61 6d 65 74 65 72 73 20 74 68 61 74 parameters that
1906f 20 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 attempt to writ
19070 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f e past the end o
19071 66 20 74 68 65 20 65 78 69 73 74 69 6e 67 20 64 f the existing d
19072 61 74 61 2c 0a 2a 2a 20 6e 6f 20 6d 6f 64 69 66 ata,.** no modif
19073 69 63 61 74 69 6f 6e 73 20 61 72 65 20 6d 61 64 ications are mad
19074 65 20 61 6e 64 20 53 51 4c 49 54 45 5f 43 4f 52 e and SQLITE_COR
19075 52 55 50 54 20 69 73 20 72 65 74 75 72 6e 65 64 RUPT is returned
19076 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
19077 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
19078 74 72 65 65 50 75 74 44 61 74 61 28 42 74 43 75 treePutData(BtCu
19079 72 73 6f 72 20 2a 70 43 73 72 2c 20 75 33 32 20 rsor *pCsr, u32
1907a 6f 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74 2c offset, u32 amt,
1907b 20 76 6f 69 64 20 2a 7a 29 7b 0a 20 20 69 6e 74 void *z){. int
1907c 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 63 rc;. assert( c
1907d 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
1907e 70 43 73 72 29 20 29 3b 0a 20 20 61 73 73 65 72 pCsr) );. asser
1907f 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
19080 5f 68 65 6c 64 28 70 43 73 72 2d 3e 70 42 74 72 _held(pCsr->pBtr
19081 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 ee->db->mutex) )
19082 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 73 72 ;. assert( pCsr
19083 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 ->isIncrblobHand
19084 6c 65 20 29 3b 0a 0a 20 20 72 63 20 3d 20 72 65 le );.. rc = re
19085 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 storeCursorPosit
19086 69 6f 6e 28 70 43 73 72 29 3b 0a 20 20 69 66 28 ion(pCsr);. if(
19087 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
19088 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b {. return rc;
19089 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 . }. assert( p
1908a 43 73 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 Csr->eState!=CUR
1908b 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 SOR_REQUIRESEEK
1908c 29 3b 0a 20 20 69 66 28 20 70 43 73 72 2d 3e 65 );. if( pCsr->e
1908d 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 State!=CURSOR_VA
1908e 4c 49 44 20 29 7b 0a 20 20 20 20 72 65 74 75 72 LID ){. retur
1908f 6e 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a n SQLITE_ABORT;.
19090 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 }.. /* Check
19091 73 6f 6d 65 20 61 73 73 75 6d 70 74 69 6f 6e 73 some assumptions
19092 3a 20 0a 20 20 2a 2a 20 20 20 28 61 29 20 74 68 : . ** (a) th
19093 65 20 63 75 72 73 6f 72 20 69 73 20 6f 70 65 6e e cursor is open
19094 20 66 6f 72 20 77 72 69 74 69 6e 67 2c 0a 20 20 for writing,.
19095 2a 2a 20 20 20 28 62 29 20 74 68 65 72 65 20 69 ** (b) there i
19096 73 20 61 20 72 65 61 64 2f 77 72 69 74 65 20 74 s a read/write t
19097 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 2c ransaction open,
19098 0a 20 20 2a 2a 20 20 20 28 63 29 20 74 68 65 20 . ** (c) the
19099 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 6f 6c 64 73 connection holds
1909a 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e a write-lock on
1909b 20 74 68 65 20 74 61 62 6c 65 20 28 69 66 20 72 the table (if r
1909c 65 71 75 69 72 65 64 29 2c 0a 20 20 2a 2a 20 20 equired),. **
1909d 20 28 64 29 20 74 68 65 72 65 20 61 72 65 20 6e (d) there are n
1909e 6f 20 63 6f 6e 66 6c 69 63 74 69 6e 67 20 72 65 o conflicting re
1909f 61 64 2d 6c 6f 63 6b 73 2c 20 61 6e 64 0a 20 20 ad-locks, and.
190a0 2a 2a 20 20 20 28 65 29 20 74 68 65 20 63 75 72 ** (e) the cur
190a1 73 6f 72 20 70 6f 69 6e 74 73 20 61 74 20 61 20 sor points at a
190a2 76 61 6c 69 64 20 72 6f 77 20 6f 66 20 61 6e 20 valid row of an
190a3 69 6e 74 4b 65 79 20 74 61 62 6c 65 2e 0a 20 20 intKey table..
190a4 2a 2f 0a 20 20 69 66 28 20 21 70 43 73 72 2d 3e */. if( !pCsr->
190a5 77 72 46 6c 61 67 20 29 7b 0a 20 20 20 20 72 65 wrFlag ){. re
190a6 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 turn SQLITE_READ
190a7 4f 4e 4c 59 3b 0a 20 20 7d 0a 20 20 61 73 73 65 ONLY;. }. asse
190a8 72 74 28 20 21 70 43 73 72 2d 3e 70 42 74 2d 3e rt( !pCsr->pBt->
190a9 72 65 61 64 4f 6e 6c 79 20 26 26 20 70 43 73 72 readOnly && pCsr
190aa 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 ->pBt->inTransac
190ab 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 tion==TRANS_WRIT
190ac 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 68 E );. assert( h
190ad 61 73 53 68 61 72 65 64 43 61 63 68 65 54 61 62 asSharedCacheTab
190ae 6c 65 4c 6f 63 6b 28 70 43 73 72 2d 3e 70 42 74 leLock(pCsr->pBt
190af 72 65 65 2c 20 70 43 73 72 2d 3e 70 67 6e 6f 52 ree, pCsr->pgnoR
190b0 6f 6f 74 2c 20 30 2c 20 32 29 20 29 3b 0a 20 20 oot, 0, 2) );.
190b1 61 73 73 65 72 74 28 20 21 68 61 73 52 65 61 64 assert( !hasRead
190b2 43 6f 6e 66 6c 69 63 74 73 28 70 43 73 72 2d 3e Conflicts(pCsr->
190b3 70 42 74 72 65 65 2c 20 70 43 73 72 2d 3e 70 67 pBtree, pCsr->pg
190b4 6e 6f 52 6f 6f 74 29 20 29 3b 0a 20 20 61 73 73 noRoot) );. ass
190b5 65 72 74 28 20 70 43 73 72 2d 3e 61 70 50 61 67 ert( pCsr->apPag
190b6 65 5b 70 43 73 72 2d 3e 69 50 61 67 65 5d 2d 3e e[pCsr->iPage]->
190b7 69 6e 74 4b 65 79 20 29 3b 0a 0a 20 20 72 65 74 intKey );.. ret
190b8 75 72 6e 20 61 63 63 65 73 73 50 61 79 6c 6f 61 urn accessPayloa
190b9 64 28 70 43 73 72 2c 20 6f 66 66 73 65 74 2c 20 d(pCsr, offset,
190ba 61 6d 74 2c 20 28 75 6e 73 69 67 6e 65 64 20 63 amt, (unsigned c
190bb 68 61 72 20 2a 29 7a 2c 20 31 29 3b 0a 7d 0a 0a har *)z, 1);.}..
190bc 2f 2a 20 0a 2a 2a 20 53 65 74 20 61 20 66 6c 61 /* .** Set a fla
190bd 67 20 6f 6e 20 74 68 69 73 20 63 75 72 73 6f 72 g on this cursor
190be 20 74 6f 20 63 61 63 68 65 20 74 68 65 20 6c 6f to cache the lo
190bf 63 61 74 69 6f 6e 73 20 6f 66 20 70 61 67 65 73 cations of pages
190c0 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 6f 76 from the .** ov
190c1 65 72 66 6c 6f 77 20 6c 69 73 74 20 66 6f 72 20 erflow list for
190c2 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 2e the current row.
190c3 20 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 This is used by
190c4 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 65 64 0a cursors opened.
190c5 2a 2a 20 66 6f 72 20 69 6e 63 72 65 6d 65 6e 74 ** for increment
190c6 61 6c 20 62 6c 6f 62 20 49 4f 20 6f 6e 6c 79 2e al blob IO only.
190c7 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
190c8 74 69 6f 6e 20 73 65 74 73 20 61 20 66 6c 61 67 tion sets a flag
190c9 20 6f 6e 6c 79 2e 20 54 68 65 20 61 63 74 75 61 only. The actua
190ca 6c 20 70 61 67 65 20 6c 6f 63 61 74 69 6f 6e 20 l page location
190cb 63 61 63 68 65 0a 2a 2a 20 28 73 74 6f 72 65 64 cache.** (stored
190cc 20 69 6e 20 42 74 43 75 72 73 6f 72 2e 61 4f 76 in BtCursor.aOv
190cd 65 72 66 6c 6f 77 5b 5d 29 20 69 73 20 61 6c 6c erflow[]) is all
190ce 6f 63 61 74 65 64 20 61 6e 64 20 75 73 65 64 20 ocated and used
190cf 62 79 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 61 by function.** a
190d0 63 63 65 73 73 50 61 79 6c 6f 61 64 28 29 20 28 ccessPayload() (
190d1 74 68 65 20 77 6f 72 6b 65 72 20 66 75 6e 63 74 the worker funct
190d2 69 6f 6e 20 66 6f 72 20 73 71 6c 69 74 65 33 42 ion for sqlite3B
190d3 74 72 65 65 44 61 74 61 28 29 20 61 6e 64 0a 2a treeData() and.*
190d4 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 75 * sqlite3BtreePu
190d5 74 44 61 74 61 28 29 29 2e 0a 2a 2f 0a 53 51 4c tData())..*/.SQL
190d6 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
190d7 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 61 63 sqlite3BtreeCac
190d8 68 65 4f 76 65 72 66 6c 6f 77 28 42 74 43 75 72 heOverflow(BtCur
190d9 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 sor *pCur){. as
190da 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 sert( cursorHold
190db 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a sMutex(pCur) );.
190dc 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
190dd 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 3_mutex_held(pCu
190de 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d r->pBtree->db->m
190df 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
190e0 74 28 21 70 43 75 72 2d 3e 69 73 49 6e 63 72 62 t(!pCur->isIncrb
190e1 6c 6f 62 48 61 6e 64 6c 65 29 3b 0a 20 20 61 73 lobHandle);. as
190e2 73 65 72 74 28 21 70 43 75 72 2d 3e 61 4f 76 65 sert(!pCur->aOve
190e3 72 66 6c 6f 77 29 3b 0a 20 20 70 43 75 72 2d 3e rflow);. pCur->
190e4 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 isIncrblobHandle
190e5 20 3d 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a = 1;.}.#endif..
190e6 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
190e7 45 6e 64 20 6f 66 20 62 74 72 65 65 2e 63 20 2a End of btree.c *
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 2f 0a **************/.
190eb 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
190ec 42 65 67 69 6e 20 66 69 6c 65 20 62 61 63 6b 75 Begin file backu
190ed 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a p.c ************
190ee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
190ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
190f0 2f 2a 0a 2a 2a 20 32 30 30 39 20 4a 61 6e 75 61 /*.** 2009 Janua
190f1 72 79 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ry 28.**.** The
190f2 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
190f3 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
190f4 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
190f5 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
190f6 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
190f7 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
190f8 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
190f9 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
190fa 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
190fb 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
190fc 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
190fd 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
190fe 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
190ff 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
19100 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
19101 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
19102 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
19103 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19104 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19105 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19106 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19107 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c ****.** This fil
19108 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 69 e contains the i
19109 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
1910a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 the sqlite3_bac
1910b 6b 75 70 5f 58 58 58 28 29 20 0a 2a 2a 20 41 50 kup_XXX() .** AP
1910c 49 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 I functions and
1910d 74 68 65 20 72 65 6c 61 74 65 64 20 66 65 61 74 the related feat
1910e 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a ures..**.** $Id:
1910f 20 62 61 63 6b 75 70 2e 63 2c 76 20 31 2e 31 39 backup.c,v 1.19
19110 20 32 30 30 39 2f 30 37 2f 30 36 20 31 39 3a 30 2009/07/06 19:0
19111 33 3a 31 33 20 64 72 68 20 45 78 70 20 24 0a 2a 3:13 drh Exp $.*
19112 2f 0a 0a 2f 2a 20 4d 61 63 72 6f 20 74 6f 20 66 /../* Macro to f
19113 69 6e 64 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 ind the minimum
19114 6f 66 20 74 77 6f 20 6e 75 6d 65 72 69 63 20 76 of two numeric v
19115 61 6c 75 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 alues..*/.#ifnde
19116 66 20 4d 49 4e 0a 23 20 64 65 66 69 6e 65 20 4d f MIN.# define M
19117 49 4e 28 78 2c 79 29 20 28 28 78 29 3c 28 79 29 IN(x,y) ((x)<(y)
19118 3f 28 78 29 3a 28 79 29 29 0a 23 65 6e 64 69 66 ?(x):(y)).#endif
19119 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63 74 75 72 ../*.** Structur
1911a 65 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 e allocated for
1911b 65 61 63 68 20 62 61 63 6b 75 70 20 6f 70 65 72 each backup oper
1911c 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 ation..*/.struct
1911d 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 sqlite3_backup
1911e 7b 0a 20 20 73 71 6c 69 74 65 33 2a 20 70 44 65 {. sqlite3* pDe
1911f 73 74 44 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 stDb; /*
19120 44 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 Destination data
19121 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 base handle */.
19122 20 42 74 72 65 65 20 2a 70 44 65 73 74 3b 20 20 Btree *pDest;
19123 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73 /* Des
19124 74 69 6e 61 74 69 6f 6e 20 62 2d 74 72 65 65 20 tination b-tree
19125 66 69 6c 65 20 2a 2f 0a 20 20 75 33 32 20 69 44 file */. u32 iD
19126 65 73 74 53 63 68 65 6d 61 3b 20 20 20 20 20 20 estSchema;
19127 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 73 /* Original s
19128 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 69 6e 20 chema cookie in
19129 64 65 73 74 69 6e 61 74 69 6f 6e 20 2a 2f 0a 20 destination */.
1912a 20 69 6e 74 20 62 44 65 73 74 4c 6f 63 6b 65 64 int bDestLocked
1912b 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 ; /* Tru
1912c 65 20 6f 6e 63 65 20 61 20 77 72 69 74 65 2d 74 e once a write-t
1912d 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 ransaction is op
1912e 65 6e 20 6f 6e 20 70 44 65 73 74 20 2a 2f 0a 0a en on pDest */..
1912f 20 20 50 67 6e 6f 20 69 4e 65 78 74 3b 20 20 20 Pgno iNext;
19130 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
19131 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 ge number of the
19132 20 6e 65 78 74 20 73 6f 75 72 63 65 20 70 61 67 next source pag
19133 65 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 73 e to copy */. s
19134 71 6c 69 74 65 33 2a 20 70 53 72 63 44 62 3b 20 qlite3* pSrcDb;
19135 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 /* Sourc
19136 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c e database handl
19137 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 53 e */. Btree *pS
19138 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rc;
19139 2f 2a 20 53 6f 75 72 63 65 20 62 2d 74 72 65 65 /* Source b-tree
1913a 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 69 6e 74 20 file */.. int
1913b 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rc;
1913c 20 20 20 20 20 2f 2a 20 42 61 63 6b 75 70 20 70 /* Backup p
1913d 72 6f 63 65 73 73 20 65 72 72 6f 72 20 63 6f 64 rocess error cod
1913e 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 73 65 e */.. /* These
1913f 20 74 77 6f 20 76 61 72 69 61 62 6c 65 73 20 61 two variables a
19140 72 65 20 73 65 74 20 62 79 20 65 76 65 72 79 20 re set by every
19141 63 61 6c 6c 20 74 6f 20 62 61 63 6b 75 70 5f 73 call to backup_s
19142 74 65 70 28 29 2e 20 54 68 65 79 20 61 72 65 0a tep(). They are.
19143 20 20 2a 2a 20 72 65 61 64 20 62 79 20 63 61 6c ** read by cal
19144 6c 73 20 74 6f 20 62 61 63 6b 75 70 5f 72 65 6d ls to backup_rem
19145 61 69 6e 69 6e 67 28 29 20 61 6e 64 20 62 61 63 aining() and bac
19146 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 2e kup_pagecount().
19147 0a 20 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 52 65 . */. Pgno nRe
19148 6d 61 69 6e 69 6e 67 3b 20 20 20 20 20 20 20 20 maining;
19149 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 /* Number of pa
1914a 67 65 73 20 6c 65 66 74 20 74 6f 20 63 6f 70 79 ges left to copy
1914b 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 50 61 67 65 */. Pgno nPage
1914c 63 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 2f count; /
1914d 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f * Total number o
1914e 66 20 70 61 67 65 73 20 74 6f 20 63 6f 70 79 20 f pages to copy
1914f 2a 2f 0a 0a 20 20 69 6e 74 20 69 73 41 74 74 61 */.. int isAtta
19150 63 68 65 64 3b 20 20 20 20 20 20 20 20 20 20 2f ched; /
19151 2a 20 54 72 75 65 20 6f 6e 63 65 20 62 61 63 6b * True once back
19152 75 70 20 68 61 73 20 62 65 65 6e 20 72 65 67 69 up has been regi
19153 73 74 65 72 65 64 20 77 69 74 68 20 70 61 67 65 stered with page
19154 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62 r */. sqlite3_b
19155 61 63 6b 75 70 20 2a 70 4e 65 78 74 3b 20 20 20 ackup *pNext;
19156 2f 2a 20 4e 65 78 74 20 62 61 63 6b 75 70 20 61 /* Next backup a
19157 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 73 ssociated with s
19158 6f 75 72 63 65 20 70 61 67 65 72 20 2a 2f 0a 7d ource pager */.}
19159 3b 0a 0a 2f 2a 0a 2a 2a 20 54 48 52 45 41 44 20 ;../*.** THREAD
1915a 53 41 46 45 54 59 20 4e 4f 54 45 53 3a 0a 2a 2a SAFETY NOTES:.**
1915b 0a 2a 2a 20 20 20 4f 6e 63 65 20 69 74 20 68 61 .** Once it ha
1915c 73 20 62 65 65 6e 20 63 72 65 61 74 65 64 20 75 s been created u
1915d 73 69 6e 67 20 62 61 63 6b 75 70 5f 69 6e 69 74 sing backup_init
1915e 28 29 2c 20 61 20 73 69 6e 67 6c 65 20 73 71 6c (), a single sql
1915f 69 74 65 33 5f 62 61 63 6b 75 70 0a 2a 2a 20 20 ite3_backup.**
19160 20 73 74 72 75 63 74 75 72 65 20 6d 61 79 20 62 structure may b
19161 65 20 61 63 63 65 73 73 65 64 20 76 69 61 20 74 e accessed via t
19162 77 6f 20 67 72 6f 75 70 73 20 6f 66 20 74 68 72 wo groups of thr
19163 65 61 64 2d 73 61 66 65 20 65 6e 74 72 79 20 70 ead-safe entry p
19164 6f 69 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 oints:.**.**
19165 20 2a 20 56 69 61 20 74 68 65 20 73 71 6c 69 74 * Via the sqlit
19166 65 33 5f 62 61 63 6b 75 70 5f 58 58 58 28 29 20 e3_backup_XXX()
19167 41 50 49 20 66 75 6e 63 74 69 6f 6e 20 62 61 63 API function bac
19168 6b 75 70 5f 73 74 65 70 28 29 20 61 6e 64 20 0a kup_step() and .
19169 2a 2a 20 20 20 20 20 20 20 62 61 63 6b 75 70 5f ** backup_
1916a 66 69 6e 69 73 68 28 29 2e 20 42 6f 74 68 20 74 finish(). Both t
1916b 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 6f hese functions o
1916c 62 74 61 69 6e 20 74 68 65 20 73 6f 75 72 63 65 btain the source
1916d 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 20 database.**
1916e 20 20 20 68 61 6e 64 6c 65 20 6d 75 74 65 78 20 handle mutex
1916f 61 6e 64 20 74 68 65 20 6d 75 74 65 78 20 61 73 and the mutex as
19170 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 sociated with th
19171 65 20 73 6f 75 72 63 65 20 42 74 53 68 61 72 65 e source BtShare
19172 64 20 0a 2a 2a 20 20 20 20 20 20 20 73 74 72 75 d .** stru
19173 63 74 75 72 65 2c 20 69 6e 20 74 68 61 74 20 6f cture, in that o
19174 72 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 rder..**.**
19175 2a 20 56 69 61 20 74 68 65 20 42 61 63 6b 75 70 * Via the Backup
19176 55 70 64 61 74 65 28 29 20 61 6e 64 20 42 61 63 Update() and Bac
19177 6b 75 70 52 65 73 74 61 72 74 28 29 20 66 75 6e kupRestart() fun
19178 63 74 69 6f 6e 73 2c 20 77 68 69 63 68 20 61 72 ctions, which ar
19179 65 0a 2a 2a 20 20 20 20 20 20 20 69 6e 76 6f 6b e.** invok
1917a 65 64 20 62 79 20 74 68 65 20 70 61 67 65 72 20 ed by the pager
1917b 6c 61 79 65 72 20 74 6f 20 72 65 70 6f 72 74 20 layer to report
1917c 76 61 72 69 6f 75 73 20 73 74 61 74 65 20 63 68 various state ch
1917d 61 6e 67 65 73 20 69 6e 0a 2a 2a 20 20 20 20 20 anges in.**
1917e 20 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 the page cache
1917f 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
19180 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 the source data
19181 62 61 73 65 2e 20 54 68 65 20 6d 75 74 65 78 0a base. The mutex.
19182 2a 2a 20 20 20 20 20 20 20 61 73 73 6f 63 69 61 ** associa
19183 74 65 64 20 77 69 74 68 20 74 68 65 20 73 6f 75 ted with the sou
19184 72 63 65 20 64 61 74 61 62 61 73 65 20 42 74 53 rce database BtS
19185 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65 20 hared structure
19186 77 69 6c 6c 20 61 6c 77 61 79 73 20 0a 2a 2a 20 will always .**
19187 20 20 20 20 20 20 62 65 20 68 65 6c 64 20 77 68 be held wh
19188 65 6e 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 en either of the
19189 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 se functions are
1918a 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 invoked..**.**
1918b 20 20 54 68 65 20 6f 74 68 65 72 20 73 71 6c 69 The other sqli
1918c 74 65 33 5f 62 61 63 6b 75 70 5f 58 58 58 28 29 te3_backup_XXX()
1918d 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 73 2c 20 API functions,
1918e 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 backup_remaining
1918f 28 29 20 61 6e 64 0a 2a 2a 20 20 20 62 61 63 6b () and.** back
19190 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 20 61 up_pagecount() a
19191 72 65 20 6e 6f 74 20 74 68 72 65 61 64 2d 73 61 re not thread-sa
19192 66 65 20 66 75 6e 63 74 69 6f 6e 73 2e 20 49 66 fe functions. If
19193 20 74 68 65 79 20 61 72 65 20 63 61 6c 6c 65 64 they are called
19194 0a 2a 2a 20 20 20 77 68 69 6c 65 20 73 6f 6d 65 .** while some
19195 20 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 73 other thread is
19196 20 63 61 6c 6c 69 6e 67 20 62 61 63 6b 75 70 5f calling backup_
19197 73 74 65 70 28 29 20 6f 72 20 62 61 63 6b 75 70 step() or backup
19198 5f 66 69 6e 69 73 68 28 29 2c 0a 2a 2a 20 20 20 _finish(),.**
19199 74 68 65 20 76 61 6c 75 65 73 20 72 65 74 75 72 the values retur
1919a 6e 65 64 20 6d 61 79 20 62 65 20 69 6e 76 61 6c ned may be inval
1919b 69 64 2e 20 54 68 65 72 65 20 69 73 20 6e 6f 20 id. There is no
1919c 77 61 79 20 66 6f 72 20 61 20 63 61 6c 6c 20 74 way for a call t
1919d 6f 0a 2a 2a 20 20 20 42 61 63 6b 75 70 55 70 64 o.** BackupUpd
1919e 61 74 65 28 29 20 6f 72 20 42 61 63 6b 75 70 52 ate() or BackupR
1919f 65 73 74 61 72 74 28 29 20 74 6f 20 69 6e 74 65 estart() to inte
191a0 72 66 65 72 65 20 77 69 74 68 20 62 61 63 6b 75 rfere with backu
191a1 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 0a 2a 2a p_remaining().**
191a2 20 20 20 6f 72 20 62 61 63 6b 75 70 5f 70 61 67 or backup_pag
191a3 65 63 6f 75 6e 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 ecount()..**.**
191a4 20 20 44 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 Depending on t
191a5 68 65 20 53 51 4c 69 74 65 20 63 6f 6e 66 69 67 he SQLite config
191a6 75 72 61 74 69 6f 6e 2c 20 74 68 65 20 64 61 74 uration, the dat
191a7 61 62 61 73 65 20 68 61 6e 64 6c 65 73 20 61 6e abase handles an
191a8 64 2f 6f 72 0a 2a 2a 20 20 20 74 68 65 20 42 74 d/or.** the Bt
191a9 72 65 65 20 6f 62 6a 65 63 74 73 20 6d 61 79 20 ree objects may
191aa 68 61 76 65 20 74 68 65 69 72 20 6f 77 6e 20 6d have their own m
191ab 75 74 65 78 65 73 20 74 68 61 74 20 72 65 71 75 utexes that requ
191ac 69 72 65 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 ire locking..**
191ad 20 20 4e 6f 6e 2d 73 68 61 72 61 62 6c 65 20 42 Non-sharable B
191ae 74 72 65 65 73 20 28 69 6e 2d 6d 65 6d 6f 72 79 trees (in-memory
191af 20 64 61 74 61 62 61 73 65 73 20 66 6f 72 20 65 databases for e
191b0 78 61 6d 70 6c 65 29 2c 20 64 6f 20 6e 6f 74 20 xample), do not
191b1 68 61 76 65 0a 2a 2a 20 20 20 61 73 73 6f 63 69 have.** associ
191b2 61 74 65 64 20 6d 75 74 65 78 65 73 2e 0a 2a 2f ated mutexes..*/
191b3 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 ../*.** Return a
191b4 20 70 6f 69 6e 74 65 72 20 63 6f 72 72 65 73 70 pointer corresp
191b5 6f 6e 64 69 6e 67 20 74 6f 20 64 61 74 61 62 61 onding to databa
191b6 73 65 20 7a 44 62 20 28 69 2e 65 2e 20 22 6d 61 se zDb (i.e. "ma
191b7 69 6e 22 2c 20 22 74 65 6d 70 22 29 0a 2a 2a 20 in", "temp").**
191b8 69 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 in connection ha
191b9 6e 64 6c 65 20 70 44 62 2e 20 49 66 20 73 75 63 ndle pDb. If suc
191ba 68 20 61 20 64 61 74 61 62 61 73 65 20 63 61 6e h a database can
191bb 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2c 20 72 65 not be found, re
191bc 74 75 72 6e 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70 turn.** a NULL p
191bd 6f 69 6e 74 65 72 20 61 6e 64 20 77 72 69 74 65 ointer and write
191be 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 an error messag
191bf 65 20 74 6f 20 70 45 72 72 6f 72 44 62 2e 0a 2a e to pErrorDb..*
191c0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22 74 65 6d *.** If the "tem
191c1 70 22 20 64 61 74 61 62 61 73 65 20 69 73 20 72 p" database is r
191c2 65 71 75 65 73 74 65 64 2c 20 69 74 20 6d 61 79 equested, it may
191c3 20 6e 65 65 64 20 74 6f 20 62 65 20 6f 70 65 6e need to be open
191c4 65 64 20 62 79 20 74 68 69 73 20 0a 2a 2a 20 66 ed by this .** f
191c5 75 6e 63 74 69 6f 6e 2e 20 49 66 20 61 6e 20 65 unction. If an e
191c6 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c rror occurs whil
191c7 65 20 64 6f 69 6e 67 20 73 6f 2c 20 72 65 74 75 e doing so, retu
191c8 72 6e 20 30 20 61 6e 64 20 77 72 69 74 65 20 61 rn 0 and write a
191c9 6e 20 0a 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73 n .** error mess
191ca 61 67 65 20 74 6f 20 70 45 72 72 6f 72 44 62 2e age to pErrorDb.
191cb 0a 2a 2f 0a 73 74 61 74 69 63 20 42 74 72 65 65 .*/.static Btree
191cc 20 2a 66 69 6e 64 42 74 72 65 65 28 73 71 6c 69 *findBtree(sqli
191cd 74 65 33 20 2a 70 45 72 72 6f 72 44 62 2c 20 73 te3 *pErrorDb, s
191ce 71 6c 69 74 65 33 20 2a 70 44 62 2c 20 63 6f 6e qlite3 *pDb, con
191cf 73 74 20 63 68 61 72 20 2a 7a 44 62 29 7b 0a 20 st char *zDb){.
191d0 20 69 6e 74 20 69 20 3d 20 73 71 6c 69 74 65 33 int i = sqlite3
191d1 46 69 6e 64 44 62 4e 61 6d 65 28 70 44 62 2c 20 FindDbName(pDb,
191d2 7a 44 62 29 3b 0a 0a 20 20 69 66 28 20 69 3d 3d zDb);.. if( i==
191d3 31 20 29 7b 0a 20 20 20 20 50 61 72 73 65 20 2a 1 ){. Parse *
191d4 70 50 61 72 73 65 3b 0a 20 20 20 20 69 6e 74 20 pParse;. int
191d5 72 63 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 72 rc = 0;. pPar
191d6 73 65 20 3d 20 73 71 6c 69 74 65 33 53 74 61 63 se = sqlite3Stac
191d7 6b 41 6c 6c 6f 63 5a 65 72 6f 28 70 45 72 72 6f kAllocZero(pErro
191d8 72 44 62 2c 20 73 69 7a 65 6f 66 28 2a 70 50 61 rDb, sizeof(*pPa
191d9 72 73 65 29 29 3b 0a 20 20 20 20 69 66 28 20 70 rse));. if( p
191da 50 61 72 73 65 3d 3d 30 20 29 7b 0a 20 20 20 20 Parse==0 ){.
191db 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 sqlite3Error(p
191dc 45 72 72 6f 72 44 62 2c 20 53 51 4c 49 54 45 5f ErrorDb, SQLITE_
191dd 4e 4f 4d 45 4d 2c 20 22 6f 75 74 20 6f 66 20 6d NOMEM, "out of m
191de 65 6d 6f 72 79 22 29 3b 0a 20 20 20 20 20 20 72 emory");. r
191df 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d c = SQLITE_NOMEM
191e0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
191e1 20 20 20 70 50 61 72 73 65 2d 3e 64 62 20 3d 20 pParse->db =
191e2 70 44 62 3b 0a 20 20 20 20 20 20 69 66 28 20 73 pDb;. if( s
191e3 71 6c 69 74 65 33 4f 70 65 6e 54 65 6d 70 44 61 qlite3OpenTempDa
191e4 74 61 62 61 73 65 28 70 50 61 72 73 65 29 20 29 tabase(pParse) )
191e5 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
191e6 33 45 72 72 6f 72 43 6c 65 61 72 28 70 50 61 72 3ErrorClear(pPar
191e7 73 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c se);. sql
191e8 69 74 65 33 45 72 72 6f 72 28 70 45 72 72 6f 72 ite3Error(pError
191e9 44 62 2c 20 70 50 61 72 73 65 2d 3e 72 63 2c 20 Db, pParse->rc,
191ea 22 25 73 22 2c 20 70 50 61 72 73 65 2d 3e 7a 45 "%s", pParse->zE
191eb 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 20 20 rrMsg);.
191ec 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f rc = SQLITE_ERRO
191ed 52 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 R;. }.
191ee 20 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65 sqlite3StackFre
191ef 65 28 70 45 72 72 6f 72 44 62 2c 20 70 50 61 72 e(pErrorDb, pPar
191f0 73 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 se);. }. i
191f1 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 f( rc ){. r
191f2 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 eturn 0;. }.
191f3 20 7d 0a 0a 20 20 69 66 28 20 69 3c 30 20 29 7b }.. if( i<0 ){
191f4 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f . sqlite3Erro
191f5 72 28 70 45 72 72 6f 72 44 62 2c 20 53 51 4c 49 r(pErrorDb, SQLI
191f6 54 45 5f 45 52 52 4f 52 2c 20 22 75 6e 6b 6e 6f TE_ERROR, "unkno
191f7 77 6e 20 64 61 74 61 62 61 73 65 20 25 73 22 2c wn database %s",
191f8 20 7a 44 62 29 3b 0a 20 20 20 20 72 65 74 75 72 zDb);. retur
191f9 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 n 0;. }.. retu
191fa 72 6e 20 70 44 62 2d 3e 61 44 62 5b 69 5d 2e 70 rn pDb->aDb[i].p
191fb 42 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 Bt;.}../*.** Cre
191fc 61 74 65 20 61 6e 20 73 71 6c 69 74 65 33 5f 62 ate an sqlite3_b
191fd 61 63 6b 75 70 20 70 72 6f 63 65 73 73 20 74 6f ackup process to
191fe 20 63 6f 70 79 20 74 68 65 20 63 6f 6e 74 65 6e copy the conten
191ff 74 73 20 6f 66 20 7a 53 72 63 44 62 20 66 72 6f ts of zSrcDb fro
19200 6d 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 m.** connection
19201 68 61 6e 64 6c 65 20 70 53 72 63 44 62 20 74 6f handle pSrcDb to
19202 20 7a 44 65 73 74 44 62 20 69 6e 20 70 44 65 73 zDestDb in pDes
19203 74 44 62 2e 20 49 66 20 73 75 63 63 65 73 73 66 tDb. If successf
19204 75 6c 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 ul, return.** a
19205 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e pointer to the n
19206 65 77 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 ew sqlite3_backu
19207 70 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 p object..**.**
19208 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 If an error occu
19209 72 73 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 rs, NULL is retu
1920a 72 6e 65 64 20 61 6e 64 20 61 6e 20 65 72 72 6f rned and an erro
1920b 72 20 63 6f 64 65 20 61 6e 64 20 65 72 72 6f 72 r code and error
1920c 20 6d 65 73 73 61 67 65 0a 2a 2a 20 73 74 6f 72 message.** stor
1920d 65 64 20 69 6e 20 64 61 74 61 62 61 73 65 20 68 ed in database h
1920e 61 6e 64 6c 65 20 70 44 65 73 74 44 62 2e 0a 2a andle pDestDb..*
1920f 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c /.SQLITE_API sql
19210 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 73 71 6c ite3_backup *sql
19211 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 ite3_backup_init
19212 28 0a 20 20 73 71 6c 69 74 65 33 2a 20 70 44 65 (. sqlite3* pDe
19213 73 74 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 stDb,
19214 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 /* Dat
19215 61 62 61 73 65 20 74 6f 20 77 72 69 74 65 20 74 abase to write t
19216 6f 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 o */. const cha
19217 72 20 2a 7a 44 65 73 74 44 62 2c 20 20 20 20 20 r *zDestDb,
19218 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
19219 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65 Name of database
1921a 20 77 69 74 68 69 6e 20 70 44 65 73 74 44 62 20 within pDestDb
1921b 2a 2f 0a 20 20 73 71 6c 69 74 65 33 2a 20 70 53 */. sqlite3* pS
1921c 72 63 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 rcDb,
1921d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 /* Da
1921e 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
1921f 6e 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a n to read from *
19220 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
19221 7a 53 72 63 44 62 20 20 20 20 20 20 20 20 20 20 zSrcDb
19222 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d /* Nam
19223 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 77 69 e of database wi
19224 74 68 69 6e 20 70 53 72 63 44 62 20 2a 2f 0a 29 thin pSrcDb */.)
19225 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b {. sqlite3_back
19226 75 70 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 up *p;
19227 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c /* Val
19228 75 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a ue to return */.
19229 0a 20 20 2f 2a 20 4c 6f 63 6b 20 74 68 65 20 73 . /* Lock the s
1922a 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 68 ource database h
1922b 61 6e 64 6c 65 2e 20 54 68 65 20 64 65 73 74 69 andle. The desti
1922c 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 0a nation database.
1922d 20 20 2a 2a 20 68 61 6e 64 6c 65 20 69 73 20 6e ** handle is n
1922e 6f 74 20 6c 6f 63 6b 65 64 20 69 6e 20 74 68 69 ot locked in thi
1922f 73 20 72 6f 75 74 69 6e 65 2c 20 62 75 74 20 69 s routine, but i
19230 74 20 69 73 20 6c 6f 63 6b 65 64 20 69 6e 0a 20 t is locked in.
19231 20 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b ** sqlite3_back
19232 75 70 5f 73 74 65 70 28 29 2e 20 54 68 65 20 75 up_step(). The u
19233 73 65 72 20 69 73 20 72 65 71 75 69 72 65 64 20 ser is required
19234 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 6e to ensure that n
19235 6f 0a 20 20 2a 2a 20 6f 74 68 65 72 20 74 68 72 o. ** other thr
19236 65 61 64 20 61 63 63 65 73 73 65 73 20 74 68 65 ead accesses the
19237 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 68 61 6e destination han
19238 64 6c 65 20 66 6f 72 20 74 68 65 20 64 75 72 61 dle for the dura
19239 74 69 6f 6e 0a 20 20 2a 2a 20 6f 66 20 74 68 65 tion. ** of the
1923a 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f backup operatio
1923b 6e 2e 20 20 41 6e 79 20 61 74 74 65 6d 70 74 20 n. Any attempt
1923c 74 6f 20 75 73 65 20 74 68 65 20 64 65 73 74 69 to use the desti
1923d 6e 61 74 69 6f 6e 0a 20 20 2a 2a 20 64 61 74 61 nation. ** data
1923e 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
1923f 77 68 69 6c 65 20 61 20 62 61 63 6b 75 70 20 69 while a backup i
19240 73 20 69 6e 20 70 72 6f 67 72 65 73 73 20 6d 61 s in progress ma
19241 79 20 63 61 75 73 65 0a 20 20 2a 2a 20 61 20 6d y cause. ** a m
19242 61 6c 66 75 6e 63 74 69 6f 6e 20 6f 72 20 61 20 alfunction or a
19243 64 65 61 64 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 deadlock.. */.
19244 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
19245 6e 74 65 72 28 70 53 72 63 44 62 2d 3e 6d 75 74 nter(pSrcDb->mut
19246 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d ex);. sqlite3_m
19247 75 74 65 78 5f 65 6e 74 65 72 28 70 44 65 73 74 utex_enter(pDest
19248 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 0a 20 20 69 Db->mutex);.. i
19249 66 28 20 70 53 72 63 44 62 3d 3d 70 44 65 73 74 f( pSrcDb==pDest
1924a 44 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 Db ){. sqlite
1924b 33 45 72 72 6f 72 28 0a 20 20 20 20 20 20 20 20 3Error(.
1924c 70 44 65 73 74 44 62 2c 20 53 51 4c 49 54 45 5f pDestDb, SQLITE_
1924d 45 52 52 4f 52 2c 20 22 73 6f 75 72 63 65 20 61 ERROR, "source a
1924e 6e 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 6d nd destination m
1924f 75 73 74 20 62 65 20 64 69 73 74 69 6e 63 74 22 ust be distinct"
19250 0a 20 20 20 20 29 3b 0a 20 20 20 20 70 20 3d 20 . );. p =
19251 30 3b 0a 20 20 7d 65 6c 73 65 20 7b 0a 20 20 20 0;. }else {.
19252 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 /* Allocate spa
19253 63 65 20 66 6f 72 20 61 20 6e 65 77 20 73 71 6c ce for a new sql
19254 69 74 65 33 5f 62 61 63 6b 75 70 20 6f 62 6a 65 ite3_backup obje
19255 63 74 20 2a 2f 0a 20 20 20 20 70 20 3d 20 28 73 ct */. p = (s
19256 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 29 qlite3_backup *)
19257 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 sqlite3_malloc(s
19258 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 62 61 izeof(sqlite3_ba
19259 63 6b 75 70 29 29 3b 0a 20 20 20 20 69 66 28 20 ckup));. if(
1925a 21 70 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 !p ){. sqli
1925b 74 65 33 45 72 72 6f 72 28 70 44 65 73 74 44 62 te3Error(pDestDb
1925c 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 , SQLITE_NOMEM,
1925d 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 0);. }. }..
1925e 20 2f 2a 20 49 66 20 74 68 65 20 61 6c 6c 6f 63 /* If the alloc
1925f 61 74 69 6f 6e 20 73 75 63 63 65 65 64 65 64 2c ation succeeded,
19260 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6e 65 populate the ne
19261 77 20 6f 62 6a 65 63 74 2e 20 2a 2f 0a 20 20 69 w object. */. i
19262 66 28 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73 f( p ){. mems
19263 65 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 et(p, 0, sizeof(
19264 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 29 29 sqlite3_backup))
19265 3b 0a 20 20 20 20 70 2d 3e 70 53 72 63 20 3d 20 ;. p->pSrc =
19266 66 69 6e 64 42 74 72 65 65 28 70 44 65 73 74 44 findBtree(pDestD
19267 62 2c 20 70 53 72 63 44 62 2c 20 7a 53 72 63 44 b, pSrcDb, zSrcD
19268 62 29 3b 0a 20 20 20 20 70 2d 3e 70 44 65 73 74 b);. p->pDest
19269 20 3d 20 66 69 6e 64 42 74 72 65 65 28 70 44 65 = findBtree(pDe
1926a 73 74 44 62 2c 20 70 44 65 73 74 44 62 2c 20 7a stDb, pDestDb, z
1926b 44 65 73 74 44 62 29 3b 0a 20 20 20 20 70 2d 3e DestDb);. p->
1926c 70 44 65 73 74 44 62 20 3d 20 70 44 65 73 74 44 pDestDb = pDestD
1926d 62 3b 0a 20 20 20 20 70 2d 3e 70 53 72 63 44 62 b;. p->pSrcDb
1926e 20 3d 20 70 53 72 63 44 62 3b 0a 20 20 20 20 70 = pSrcDb;. p
1926f 2d 3e 69 4e 65 78 74 20 3d 20 31 3b 0a 20 20 20 ->iNext = 1;.
19270 20 70 2d 3e 69 73 41 74 74 61 63 68 65 64 20 3d p->isAttached =
19271 20 30 3b 0a 0a 20 20 20 20 69 66 28 20 30 3d 3d 0;.. if( 0==
19272 70 2d 3e 70 53 72 63 20 7c 7c 20 30 3d 3d 70 2d p->pSrc || 0==p-
19273 3e 70 44 65 73 74 20 29 7b 0a 20 20 20 20 20 20 >pDest ){.
19274 2f 2a 20 4f 6e 65 20 28 6f 72 20 62 6f 74 68 29 /* One (or both)
19275 20 6f 66 20 74 68 65 20 6e 61 6d 65 64 20 64 61 of the named da
19276 74 61 62 61 73 65 73 20 64 69 64 20 6e 6f 74 20 tabases did not
19277 65 78 69 73 74 2e 20 41 6e 20 65 72 72 6f 72 20 exist. An error
19278 68 61 73 0a 20 20 20 20 20 20 2a 2a 20 61 6c 72 has. ** alr
19279 65 61 64 79 20 62 65 65 6e 20 77 72 69 74 74 65 eady been writte
1927a 6e 20 69 6e 74 6f 20 74 68 65 20 70 44 65 73 74 n into the pDest
1927b 44 62 20 68 61 6e 64 6c 65 2e 20 41 6c 6c 20 74 Db handle. All t
1927c 68 61 74 20 69 73 20 6c 65 66 74 0a 20 20 20 20 hat is left.
1927d 20 20 2a 2a 20 74 6f 20 64 6f 20 68 65 72 65 20 ** to do here
1927e 69 73 20 66 72 65 65 20 74 68 65 20 73 71 6c 69 is free the sqli
1927f 74 65 33 5f 62 61 63 6b 75 70 20 73 74 72 75 63 te3_backup struc
19280 74 75 72 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 ture.. */.
19281 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 sqlite3_fre
19282 65 28 70 29 3b 0a 20 20 20 20 20 20 70 20 3d 20 e(p);. p =
19283 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 0;. }. }. i
19284 66 28 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e 70 f( p ){. p->p
19285 53 72 63 2d 3e 6e 42 61 63 6b 75 70 2b 2b 3b 0a Src->nBackup++;.
19286 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d }.. sqlite3_m
19287 75 74 65 78 5f 6c 65 61 76 65 28 70 44 65 73 74 utex_leave(pDest
19288 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 Db->mutex);. sq
19289 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
1928a 65 28 70 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 e(pSrcDb->mutex)
1928b 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a ;. return p;.}.
1928c 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 ./*.** Argument
1928d 72 63 20 69 73 20 61 6e 20 53 51 4c 69 74 65 20 rc is an SQLite
1928e 65 72 72 6f 72 20 63 6f 64 65 2e 20 52 65 74 75 error code. Retu
1928f 72 6e 20 74 72 75 65 20 69 66 20 74 68 69 73 20 rn true if this
19290 65 72 72 6f 72 20 69 73 20 0a 2a 2a 20 63 6f 6e error is .** con
19291 73 69 64 65 72 65 64 20 66 61 74 61 6c 20 69 66 sidered fatal if
19292 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 64 75 72 encountered dur
19293 69 6e 67 20 61 20 62 61 63 6b 75 70 20 6f 70 65 ing a backup ope
19294 72 61 74 69 6f 6e 2e 20 41 6c 6c 20 65 72 72 6f ration. All erro
19295 72 73 0a 2a 2a 20 61 72 65 20 63 6f 6e 73 69 64 rs.** are consid
19296 65 72 65 64 20 66 61 74 61 6c 20 65 78 63 65 70 ered fatal excep
19297 74 20 66 6f 72 20 53 51 4c 49 54 45 5f 42 55 53 t for SQLITE_BUS
19298 59 20 61 6e 64 20 53 51 4c 49 54 45 5f 4c 4f 43 Y and SQLITE_LOC
19299 4b 45 44 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 KED..*/.static i
1929a 6e 74 20 69 73 46 61 74 61 6c 45 72 72 6f 72 28 nt isFatalError(
1929b 69 6e 74 20 72 63 29 7b 0a 20 20 72 65 74 75 72 int rc){. retur
1929c 6e 20 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b n (rc!=SQLITE_OK
1929d 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 42 && rc!=SQLITE_B
1929e 55 53 59 20 26 26 20 41 4c 57 41 59 53 28 72 63 USY && ALWAYS(rc
1929f 21 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 29 !=SQLITE_LOCKED)
192a0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 );.}../*.** Para
192a1 6d 65 74 65 72 20 7a 53 72 63 44 61 74 61 20 70 meter zSrcData p
192a2 6f 69 6e 74 73 20 74 6f 20 61 20 62 75 66 66 65 oints to a buffe
192a3 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 r containing the
192a4 20 64 61 74 61 20 66 6f 72 20 0a 2a 2a 20 70 61 data for .** pa
192a5 67 65 20 69 53 72 63 50 67 20 66 72 6f 6d 20 74 ge iSrcPg from t
192a6 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 he source databa
192a7 73 65 2e 20 43 6f 70 79 20 74 68 69 73 20 64 61 se. Copy this da
192a8 74 61 20 69 6e 74 6f 20 74 68 65 20 0a 2a 2a 20 ta into the .**
192a9 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 destination data
192aa 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 base..*/.static
192ab 69 6e 74 20 62 61 63 6b 75 70 4f 6e 65 50 61 67 int backupOnePag
192ac 65 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 e(sqlite3_backup
192ad 20 2a 70 2c 20 50 67 6e 6f 20 69 53 72 63 50 67 *p, Pgno iSrcPg
192ae 2c 20 63 6f 6e 73 74 20 75 38 20 2a 7a 53 72 63 , const u8 *zSrc
192af 44 61 74 61 29 7b 0a 20 20 50 61 67 65 72 20 2a Data){. Pager *
192b0 20 63 6f 6e 73 74 20 70 44 65 73 74 50 61 67 65 const pDestPage
192b1 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 r = sqlite3Btree
192b2 50 61 67 65 72 28 70 2d 3e 70 44 65 73 74 29 3b Pager(p->pDest);
192b3 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 53 72 . const int nSr
192b4 63 50 67 73 7a 20 3d 20 73 71 6c 69 74 65 33 42 cPgsz = sqlite3B
192b5 74 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 treeGetPageSize(
192b6 70 2d 3e 70 53 72 63 29 3b 0a 20 20 69 6e 74 20 p->pSrc);. int
192b7 6e 44 65 73 74 50 67 73 7a 20 3d 20 73 71 6c 69 nDestPgsz = sqli
192b8 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 53 te3BtreeGetPageS
192b9 69 7a 65 28 70 2d 3e 70 44 65 73 74 29 3b 0a 20 ize(p->pDest);.
192ba 20 63 6f 6e 73 74 20 69 6e 74 20 6e 43 6f 70 79 const int nCopy
192bb 20 3d 20 4d 49 4e 28 6e 53 72 63 50 67 73 7a 2c = MIN(nSrcPgsz,
192bc 20 6e 44 65 73 74 50 67 73 7a 29 3b 0a 20 20 63 nDestPgsz);. c
192bd 6f 6e 73 74 20 69 36 34 20 69 45 6e 64 20 3d 20 onst i64 iEnd =
192be 28 69 36 34 29 69 53 72 63 50 67 2a 28 69 36 34 (i64)iSrcPg*(i64
192bf 29 6e 53 72 63 50 67 73 7a 3b 0a 0a 20 20 69 6e )nSrcPgsz;.. in
192c0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
192c1 3b 0a 20 20 69 36 34 20 69 4f 66 66 3b 0a 0a 20 ;. i64 iOff;..
192c2 20 61 73 73 65 72 74 28 20 70 2d 3e 62 44 65 73 assert( p->bDes
192c3 74 4c 6f 63 6b 65 64 20 29 3b 0a 20 20 61 73 73 tLocked );. ass
192c4 65 72 74 28 20 21 69 73 46 61 74 61 6c 45 72 72 ert( !isFatalErr
192c5 6f 72 28 70 2d 3e 72 63 29 20 29 3b 0a 20 20 61 or(p->rc) );. a
192c6 73 73 65 72 74 28 20 69 53 72 63 50 67 21 3d 50 ssert( iSrcPg!=P
192c7 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 ENDING_BYTE_PAGE
192c8 28 70 2d 3e 70 53 72 63 2d 3e 70 42 74 29 20 29 (p->pSrc->pBt) )
192c9 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 53 72 63 ;. assert( zSrc
192ca 44 61 74 61 20 29 3b 0a 0a 20 20 2f 2a 20 43 61 Data );.. /* Ca
192cb 74 63 68 20 74 68 65 20 63 61 73 65 20 77 68 65 tch the case whe
192cc 72 65 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 re the destinati
192cd 6f 6e 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f on is an in-memo
192ce 72 79 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 ry database and
192cf 74 68 65 0a 20 20 2a 2a 20 70 61 67 65 20 73 69 the. ** page si
192d0 7a 65 73 20 6f 66 20 74 68 65 20 73 6f 75 72 63 zes of the sourc
192d1 65 20 61 6e 64 20 64 65 73 74 69 6e 61 74 69 6f e and destinatio
192d2 6e 20 64 69 66 66 65 72 2e 20 0a 20 20 2a 2f 0a n differ. . */.
192d3 20 20 69 66 28 20 6e 53 72 63 50 67 73 7a 21 3d if( nSrcPgsz!=
192d4 6e 44 65 73 74 50 67 73 7a 20 26 26 20 73 71 6c nDestPgsz && sql
192d5 69 74 65 33 50 61 67 65 72 49 73 4d 65 6d 64 62 ite3PagerIsMemdb
192d6 28 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 (sqlite3BtreePag
192d7 65 72 28 70 2d 3e 70 44 65 73 74 29 29 20 29 7b er(p->pDest)) ){
192d8 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
192d9 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 0a _READONLY;. }..
192da 20 20 2f 2a 20 54 68 69 73 20 6c 6f 6f 70 20 72 /* This loop r
192db 75 6e 73 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 uns once for eac
192dc 68 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 h destination pa
192dd 67 65 20 73 70 61 6e 6e 65 64 20 62 79 20 74 68 ge spanned by th
192de 65 20 73 6f 75 72 63 65 20 0a 20 20 2a 2a 20 70 e source . ** p
192df 61 67 65 2e 20 46 6f 72 20 65 61 63 68 20 69 74 age. For each it
192e0 65 72 61 74 69 6f 6e 2c 20 76 61 72 69 61 62 6c eration, variabl
192e1 65 20 69 4f 66 66 20 69 73 20 73 65 74 20 74 6f e iOff is set to
192e2 20 74 68 65 20 62 79 74 65 20 6f 66 66 73 65 74 the byte offset
192e3 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 73 . ** of the des
192e4 74 69 6e 61 74 69 6f 6e 20 70 61 67 65 2e 0a 20 tination page..
192e5 20 2a 2f 0a 20 20 66 6f 72 28 69 4f 66 66 3d 69 */. for(iOff=i
192e6 45 6e 64 2d 28 69 36 34 29 6e 53 72 63 50 67 73 End-(i64)nSrcPgs
192e7 7a 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b z; rc==SQLITE_OK
192e8 20 26 26 20 69 4f 66 66 3c 69 45 6e 64 3b 20 69 && iOff<iEnd; i
192e9 4f 66 66 2b 3d 6e 44 65 73 74 50 67 73 7a 29 7b Off+=nDestPgsz){
192ea 0a 20 20 20 20 44 62 50 61 67 65 20 2a 70 44 65 . DbPage *pDe
192eb 73 74 50 67 20 3d 20 30 3b 0a 20 20 20 20 50 67 stPg = 0;. Pg
192ec 6e 6f 20 69 44 65 73 74 20 3d 20 28 50 67 6e 6f no iDest = (Pgno
192ed 29 28 69 4f 66 66 2f 6e 44 65 73 74 50 67 73 7a )(iOff/nDestPgsz
192ee 29 2b 31 3b 0a 20 20 20 20 69 66 28 20 69 44 65 )+1;. if( iDe
192ef 73 74 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 st==PENDING_BYTE
192f0 5f 50 41 47 45 28 70 2d 3e 70 44 65 73 74 2d 3e _PAGE(p->pDest->
192f1 70 42 74 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b pBt) ) continue;
192f2 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f . if( SQLITE_
192f3 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 OK==(rc = sqlite
192f4 33 50 61 67 65 72 47 65 74 28 70 44 65 73 74 50 3PagerGet(pDestP
192f5 61 67 65 72 2c 20 69 44 65 73 74 2c 20 26 70 44 ager, iDest, &pD
192f6 65 73 74 50 67 29 29 0a 20 20 20 20 20 26 26 20 estPg)). &&
192f7 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d SQLITE_OK==(rc =
192f8 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
192f9 74 65 28 70 44 65 73 74 50 67 29 29 0a 20 20 20 te(pDestPg)).
192fa 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 ){. const
192fb 75 38 20 2a 7a 49 6e 20 3d 20 26 7a 53 72 63 44 u8 *zIn = &zSrcD
192fc 61 74 61 5b 69 4f 66 66 25 6e 53 72 63 50 67 73 ata[iOff%nSrcPgs
192fd 7a 5d 3b 0a 20 20 20 20 20 20 75 38 20 2a 7a 44 z];. u8 *zD
192fe 65 73 74 44 61 74 61 20 3d 20 73 71 6c 69 74 65 estData = sqlite
192ff 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 44 3PagerGetData(pD
19300 65 73 74 50 67 29 3b 0a 20 20 20 20 20 20 75 38 estPg);. u8
19301 20 2a 7a 4f 75 74 20 3d 20 26 7a 44 65 73 74 44 *zOut = &zDestD
19302 61 74 61 5b 69 4f 66 66 25 6e 44 65 73 74 50 67 ata[iOff%nDestPg
19303 73 7a 5d 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 sz];.. /* C
19304 6f 70 79 20 74 68 65 20 64 61 74 61 20 66 72 6f opy the data fro
19305 6d 20 74 68 65 20 73 6f 75 72 63 65 20 70 61 67 m the source pag
19306 65 20 69 6e 74 6f 20 74 68 65 20 64 65 73 74 69 e into the desti
19307 6e 61 74 69 6f 6e 20 70 61 67 65 2e 0a 20 20 20 nation page..
19308 20 20 20 2a 2a 20 54 68 65 6e 20 63 6c 65 61 72 ** Then clear
19309 20 74 68 65 20 42 74 72 65 65 20 6c 61 79 65 72 the Btree layer
1930a 20 4d 65 6d 50 61 67 65 2e 69 73 49 6e 69 74 20 MemPage.isInit
1930b 66 6c 61 67 2e 20 42 6f 74 68 20 74 68 69 73 20 flag. Both this
1930c 6d 6f 64 75 6c 65 0a 20 20 20 20 20 20 2a 2a 20 module. **
1930d 61 6e 64 20 74 68 65 20 70 61 67 65 72 20 63 6f and the pager co
1930e 64 65 20 75 73 65 20 74 68 69 73 20 74 72 69 63 de use this tric
1930f 6b 20 28 63 6c 65 61 72 69 6e 67 20 74 68 65 20 k (clearing the
19310 66 69 72 73 74 20 62 79 74 65 0a 20 20 20 20 20 first byte.
19311 20 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 20 ** of the page
19312 27 65 78 74 72 61 27 20 73 70 61 63 65 20 74 6f 'extra' space to
19313 20 69 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20 invalidate the
19314 42 74 72 65 65 20 6c 61 79 65 72 73 0a 20 20 20 Btree layers.
19315 20 20 20 2a 2a 20 63 61 63 68 65 64 20 70 61 72 ** cached par
19316 73 65 20 6f 66 20 74 68 65 20 70 61 67 65 29 2e se of the page).
19317 20 4d 65 6d 50 61 67 65 2e 69 73 49 6e 69 74 20 MemPage.isInit
19318 69 73 20 6d 61 72 6b 65 64 20 0a 20 20 20 20 20 is marked .
19319 20 2a 2a 20 22 4d 55 53 54 20 42 45 20 46 49 52 ** "MUST BE FIR
1931a 53 54 22 20 66 6f 72 20 74 68 69 73 20 70 75 72 ST" for this pur
1931b 70 6f 73 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 pose.. */.
1931c 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 4f 75 74 memcpy(zOut
1931d 2c 20 7a 49 6e 2c 20 6e 43 6f 70 79 29 3b 0a 20 , zIn, nCopy);.
1931e 20 20 20 20 20 28 28 75 38 20 2a 29 73 71 6c 69 ((u8 *)sqli
1931f 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 te3PagerGetExtra
19320 28 70 44 65 73 74 50 67 29 29 5b 30 5d 20 3d 20 (pDestPg))[0] =
19321 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 0;. }. sql
19322 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 ite3PagerUnref(p
19323 44 65 73 74 50 67 29 3b 0a 20 20 7d 0a 0a 20 20 DestPg);. }..
19324 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
19325 0a 2a 2a 20 49 66 20 70 46 69 6c 65 20 69 73 20 .** If pFile is
19326 63 75 72 72 65 6e 74 6c 79 20 6c 61 72 67 65 72 currently larger
19327 20 74 68 61 6e 20 69 53 69 7a 65 20 62 79 74 65 than iSize byte
19328 73 2c 20 74 68 65 6e 20 74 72 75 6e 63 61 74 65 s, then truncate
19329 20 69 74 20 74 6f 0a 2a 2a 20 65 78 61 63 74 6c it to.** exactl
1932a 79 20 69 53 69 7a 65 20 62 79 74 65 73 2e 20 49 y iSize bytes. I
1932b 66 20 70 46 69 6c 65 20 69 73 20 6e 6f 74 20 6c f pFile is not l
1932c 61 72 67 65 72 20 74 68 61 6e 20 69 53 69 7a 65 arger than iSize
1932d 20 62 79 74 65 73 2c 20 74 68 65 6e 0a 2a 2a 20 bytes, then.**
1932e 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
1932f 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 a no-op..**.**
19330 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b Return SQLITE_OK
19331 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 69 if everything i
19332 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 6f 72 s successful, or
19333 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 an SQLite error
19334 20 0a 2a 2a 20 63 6f 64 65 20 69 66 20 61 6e 20 .** code if an
19335 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2f error occurs..*/
19336 0a 73 74 61 74 69 63 20 69 6e 74 20 62 61 63 6b .static int back
19337 75 70 54 72 75 6e 63 61 74 65 46 69 6c 65 28 73 upTruncateFile(s
19338 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 qlite3_file *pFi
19339 6c 65 2c 20 69 36 34 20 69 53 69 7a 65 29 7b 0a le, i64 iSize){.
1933a 20 20 69 36 34 20 69 43 75 72 72 65 6e 74 3b 0a i64 iCurrent;.
1933b 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 int rc = sqlit
1933c 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 46 69 e3OsFileSize(pFi
1933d 6c 65 2c 20 26 69 43 75 72 72 65 6e 74 29 3b 0a le, &iCurrent);.
1933e 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1933f 5f 4f 4b 20 26 26 20 69 43 75 72 72 65 6e 74 3e _OK && iCurrent>
19340 69 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 63 20 iSize ){. rc
19341 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 = sqlite3OsTrunc
19342 61 74 65 28 70 46 69 6c 65 2c 20 69 53 69 7a 65 ate(pFile, iSize
19343 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
19344 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 rc;.}../*.** Reg
19345 69 73 74 65 72 20 74 68 69 73 20 62 61 63 6b 75 ister this backu
19346 70 20 6f 62 6a 65 63 74 20 77 69 74 68 20 74 68 p object with th
19347 65 20 61 73 73 6f 63 69 61 74 65 64 20 73 6f 75 e associated sou
19348 72 63 65 20 70 61 67 65 72 20 66 6f 72 0a 2a 2a rce pager for.**
19349 20 63 61 6c 6c 62 61 63 6b 73 20 77 68 65 6e 20 callbacks when
1934a 70 61 67 65 73 20 61 72 65 20 63 68 61 6e 67 65 pages are change
1934b 64 20 6f 72 20 74 68 65 20 63 61 63 68 65 20 69 d or the cache i
1934c 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2f 0a 73 nvalidated..*/.s
1934d 74 61 74 69 63 20 76 6f 69 64 20 61 74 74 61 63 tatic void attac
1934e 68 42 61 63 6b 75 70 4f 62 6a 65 63 74 28 73 71 hBackupObject(sq
1934f 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 29 lite3_backup *p)
19350 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b {. sqlite3_back
19351 75 70 20 2a 2a 70 70 3b 0a 20 20 61 73 73 65 72 up **pp;. asser
19352 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 t( sqlite3BtreeH
19353 6f 6c 64 73 4d 75 74 65 78 28 70 2d 3e 70 53 72 oldsMutex(p->pSr
19354 63 29 20 29 3b 0a 20 20 70 70 20 3d 20 73 71 6c c) );. pp = sql
19355 69 74 65 33 50 61 67 65 72 42 61 63 6b 75 70 50 ite3PagerBackupP
19356 74 72 28 73 71 6c 69 74 65 33 42 74 72 65 65 50 tr(sqlite3BtreeP
19357 61 67 65 72 28 70 2d 3e 70 53 72 63 29 29 3b 0a ager(p->pSrc));.
19358 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 2a 70 70 p->pNext = *pp
19359 3b 0a 20 20 2a 70 70 20 3d 20 70 3b 0a 20 20 70 ;. *pp = p;. p
1935a 2d 3e 69 73 41 74 74 61 63 68 65 64 20 3d 20 31 ->isAttached = 1
1935b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20 ;.}../*.** Copy
1935c 6e 50 61 67 65 20 70 61 67 65 73 20 66 72 6f 6d nPage pages from
1935d 20 74 68 65 20 73 6f 75 72 63 65 20 62 2d 74 72 the source b-tr
1935e 65 65 20 74 6f 20 74 68 65 20 64 65 73 74 69 6e ee to the destin
1935f 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ation..*/.SQLITE
19360 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
19361 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 73 71 6c _backup_step(sql
19362 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 2c 20 ite3_backup *p,
19363 69 6e 74 20 6e 50 61 67 65 29 7b 0a 20 20 69 6e int nPage){. in
19364 74 20 72 63 3b 0a 0a 20 20 73 71 6c 69 74 65 33 t rc;.. sqlite3
19365 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e _mutex_enter(p->
19366 70 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0a pSrcDb->mutex);.
19367 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
19368 74 65 72 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20 ter(p->pSrc);.
19369 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 29 if( p->pDestDb )
1936a 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 {. sqlite3_mu
1936b 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 44 65 tex_enter(p->pDe
1936c 73 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 stDb->mutex);.
1936d 7d 0a 0a 20 20 72 63 20 3d 20 70 2d 3e 72 63 3b }.. rc = p->rc;
1936e 0a 20 20 69 66 28 20 21 69 73 46 61 74 61 6c 45 . if( !isFatalE
1936f 72 72 6f 72 28 72 63 29 20 29 7b 0a 20 20 20 20 rror(rc) ){.
19370 50 61 67 65 72 20 2a 20 63 6f 6e 73 74 20 70 53 Pager * const pS
19371 72 63 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 rcPager = sqlite
19372 33 42 74 72 65 65 50 61 67 65 72 28 70 2d 3e 70 3BtreePager(p->p
19373 53 72 63 29 3b 20 20 20 20 20 2f 2a 20 53 6f 75 Src); /* Sou
19374 72 63 65 20 70 61 67 65 72 20 2a 2f 0a 20 20 20 rce pager */.
19375 20 50 61 67 65 72 20 2a 20 63 6f 6e 73 74 20 70 Pager * const p
19376 44 65 73 74 50 61 67 65 72 20 3d 20 73 71 6c 69 DestPager = sqli
19377 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70 2d te3BtreePager(p-
19378 3e 70 44 65 73 74 29 3b 20 20 20 2f 2a 20 44 65 >pDest); /* De
19379 73 74 20 70 61 67 65 72 20 2a 2f 0a 20 20 20 20 st pager */.
1937a 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 int ii;
1937b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1937c 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 /* Iterator v
1937d 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 ariable */. i
1937e 6e 74 20 6e 53 72 63 50 61 67 65 20 3d 20 2d 31 nt nSrcPage = -1
1937f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
19380 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 6f 75 /* Size of sou
19381 72 63 65 20 64 62 20 69 6e 20 70 61 67 65 73 20 rce db in pages
19382 2a 2f 0a 20 20 20 20 69 6e 74 20 62 43 6c 6f 73 */. int bClos
19383 65 54 72 61 6e 73 20 3d 20 30 3b 20 20 20 20 20 eTrans = 0;
19384 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
19385 65 20 69 66 20 73 72 63 20 64 62 20 72 65 71 75 e if src db requ
19386 69 72 65 73 20 75 6e 6c 6f 63 6b 69 6e 67 20 2a ires unlocking *
19387 2f 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 /.. /* If the
19388 20 73 6f 75 72 63 65 20 70 61 67 65 72 20 69 73 source pager is
19389 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 61 20 currently in a
1938a 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f write-transactio
1938b 6e 2c 20 72 65 74 75 72 6e 0a 20 20 20 20 2a 2a n, return. **
1938c 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 6d 6d SQLITE_BUSY imm
1938d 65 64 69 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2f ediately.. */
1938e 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 44 65 73 . if( p->pDes
1938f 74 44 62 20 26 26 20 70 2d 3e 70 53 72 63 2d 3e tDb && p->pSrc->
19390 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 pBt->inTransacti
19391 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 on==TRANS_WRITE
19392 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 ){. rc = SQ
19393 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d LITE_BUSY;. }
19394 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d else{. rc =
19395 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
19396 7d 0a 0a 20 20 20 20 2f 2a 20 4c 6f 63 6b 20 74 }.. /* Lock t
19397 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 he destination d
19398 61 74 61 62 61 73 65 2c 20 69 66 20 69 74 20 69 atabase, if it i
19399 73 20 6e 6f 74 20 6c 6f 63 6b 65 64 20 61 6c 72 s not locked alr
1939a 65 61 64 79 2e 20 2a 2f 0a 20 20 20 20 69 66 28 eady. */. if(
1939b 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 20 26 SQLITE_OK==rc &
1939c 26 20 70 2d 3e 62 44 65 73 74 4c 6f 63 6b 65 64 & p->bDestLocked
1939d 3d 3d 30 0a 20 20 20 20 20 26 26 20 53 51 4c 49 ==0. && SQLI
1939e 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c TE_OK==(rc = sql
1939f 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 72 ite3BtreeBeginTr
193a0 61 6e 73 28 70 2d 3e 70 44 65 73 74 2c 20 32 29 ans(p->pDest, 2)
193a1 29 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 ) . ){.
193a2 70 2d 3e 62 44 65 73 74 4c 6f 63 6b 65 64 20 3d p->bDestLocked =
193a3 20 31 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 1;. sqlite
193a4 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 70 2d 3BtreeGetMeta(p-
193a5 3e 70 44 65 73 74 2c 20 42 54 52 45 45 5f 53 43 >pDest, BTREE_SC
193a6 48 45 4d 41 5f 56 45 52 53 49 4f 4e 2c 20 26 70 HEMA_VERSION, &p
193a7 2d 3e 69 44 65 73 74 53 63 68 65 6d 61 29 3b 0a ->iDestSchema);.
193a8 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 }.. /* If
193a9 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6f 70 65 there is no ope
193aa 6e 20 72 65 61 64 2d 74 72 61 6e 73 61 63 74 69 n read-transacti
193ab 6f 6e 20 6f 6e 20 74 68 65 20 73 6f 75 72 63 65 on on the source
193ac 20 64 61 74 61 62 61 73 65 2c 20 6f 70 65 6e 0a database, open.
193ad 20 20 20 20 2a 2a 20 6f 6e 65 20 6e 6f 77 2e 20 ** one now.
193ae 49 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e If a transaction
193af 20 69 73 20 6f 70 65 6e 65 64 20 68 65 72 65 2c is opened here,
193b0 20 74 68 65 6e 20 69 74 20 77 69 6c 6c 20 62 65 then it will be
193b1 20 63 6c 6f 73 65 64 0a 20 20 20 20 2a 2a 20 62 closed. ** b
193b2 65 66 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74 efore this funct
193b3 69 6f 6e 20 65 78 69 74 73 2e 0a 20 20 20 20 2a ion exits.. *
193b4 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 /. if( rc==SQ
193b5 4c 49 54 45 5f 4f 4b 20 26 26 20 30 3d 3d 73 71 LITE_OK && 0==sq
193b6 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 52 65 lite3BtreeIsInRe
193b7 61 64 54 72 61 6e 73 28 70 2d 3e 70 53 72 63 29 adTrans(p->pSrc)
193b8 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 ){. rc = s
193b9 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e qlite3BtreeBegin
193ba 54 72 61 6e 73 28 70 2d 3e 70 53 72 63 2c 20 30 Trans(p->pSrc, 0
193bb 29 3b 0a 20 20 20 20 20 20 62 43 6c 6f 73 65 54 );. bCloseT
193bc 72 61 6e 73 20 3d 20 31 3b 0a 20 20 20 20 7d 0a rans = 1;. }.
193bd 20 20 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 74 68 . /* Now th
193be 61 74 20 74 68 65 72 65 20 69 73 20 61 20 72 65 at there is a re
193bf 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 73 ad-lock on the s
193c0 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 2c 20 ource database,
193c1 71 75 65 72 79 20 74 68 65 0a 20 20 20 20 2a 2a query the. **
193c2 20 73 6f 75 72 63 65 20 70 61 67 65 72 20 66 6f source pager fo
193c3 72 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 r the number of
193c4 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 pages in the dat
193c5 61 62 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 abase.. */.
193c6 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
193c7 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 _OK ){. rc
193c8 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 = sqlite3PagerPa
193c9 67 65 63 6f 75 6e 74 28 70 53 72 63 50 61 67 65 gecount(pSrcPage
193ca 72 2c 20 26 6e 53 72 63 50 61 67 65 29 3b 0a 20 r, &nSrcPage);.
193cb 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 69 3d }. for(ii=
193cc 30 3b 20 28 6e 50 61 67 65 3c 30 20 7c 7c 20 69 0; (nPage<0 || i
193cd 69 3c 6e 50 61 67 65 29 20 26 26 20 70 2d 3e 69 i<nPage) && p->i
193ce 4e 65 78 74 3c 3d 28 50 67 6e 6f 29 6e 53 72 63 Next<=(Pgno)nSrc
193cf 50 61 67 65 20 26 26 20 21 72 63 3b 20 69 69 2b Page && !rc; ii+
193d0 2b 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 +){. const
193d1 50 67 6e 6f 20 69 53 72 63 50 67 20 3d 20 70 2d Pgno iSrcPg = p-
193d2 3e 69 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 >iNext;
193d3 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 /* Sourc
193d4 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f e page number */
193d5 0a 20 20 20 20 20 20 69 66 28 20 69 53 72 63 50 . if( iSrcP
193d6 67 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f g!=PENDING_BYTE_
193d7 50 41 47 45 28 70 2d 3e 70 53 72 63 2d 3e 70 42 PAGE(p->pSrc->pB
193d8 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 44 62 t) ){. Db
193d9 50 61 67 65 20 2a 70 53 72 63 50 67 3b 20 20 20 Page *pSrcPg;
193da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
193db 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 /* Sou
193dc 72 63 65 20 70 61 67 65 20 6f 62 6a 65 63 74 20 rce page object
193dd 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 */. rc =
193de 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 sqlite3PagerGet(
193df 70 53 72 63 50 61 67 65 72 2c 20 69 53 72 63 50 pSrcPager, iSrcP
193e0 67 2c 20 26 70 53 72 63 50 67 29 3b 0a 20 20 20 g, &pSrcPg);.
193e1 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
193e2 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
193e3 20 20 20 20 72 63 20 3d 20 62 61 63 6b 75 70 4f rc = backupO
193e4 6e 65 50 61 67 65 28 70 2c 20 69 53 72 63 50 67 nePage(p, iSrcPg
193e5 2c 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 , sqlite3PagerGe
193e6 74 44 61 74 61 28 70 53 72 63 50 67 29 29 3b 0a tData(pSrcPg));.
193e7 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
193e8 33 50 61 67 65 72 55 6e 72 65 66 28 70 53 72 63 3PagerUnref(pSrc
193e9 50 67 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 Pg);. }.
193ea 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e }. p->
193eb 69 4e 65 78 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 iNext++;. }.
193ec 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
193ed 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 2d E_OK ){. p-
193ee 3e 6e 50 61 67 65 63 6f 75 6e 74 20 3d 20 6e 53 >nPagecount = nS
193ef 72 63 50 61 67 65 3b 0a 20 20 20 20 20 20 70 2d rcPage;. p-
193f0 3e 6e 52 65 6d 61 69 6e 69 6e 67 20 3d 20 6e 53 >nRemaining = nS
193f1 72 63 50 61 67 65 2b 31 2d 70 2d 3e 69 4e 65 78 rcPage+1-p->iNex
193f2 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e t;. if( p->
193f3 69 4e 65 78 74 3e 28 50 67 6e 6f 29 6e 53 72 63 iNext>(Pgno)nSrc
193f4 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20 Page ){.
193f5 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 rc = SQLITE_DONE
193f6 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 ;. }else if
193f7 28 20 21 70 2d 3e 69 73 41 74 74 61 63 68 65 64 ( !p->isAttached
193f8 20 29 7b 0a 20 20 20 20 20 20 20 20 61 74 74 61 ){. atta
193f9 63 68 42 61 63 6b 75 70 4f 62 6a 65 63 74 28 70 chBackupObject(p
193fa 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
193fb 0a 20 20 0a 20 20 20 20 2f 2a 20 55 70 64 61 74 . . /* Updat
193fc 65 20 74 68 65 20 73 63 68 65 6d 61 20 76 65 72 e the schema ver
193fd 73 69 6f 6e 20 66 69 65 6c 64 20 69 6e 20 74 68 sion field in th
193fe 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 e destination da
193ff 74 61 62 61 73 65 2e 20 54 68 69 73 0a 20 20 20 tabase. This.
19400 20 2a 2a 20 69 73 20 74 6f 20 6d 61 6b 65 20 73 ** is to make s
19401 75 72 65 20 74 68 61 74 20 74 68 65 20 73 63 68 ure that the sch
19402 65 6d 61 2d 76 65 72 73 69 6f 6e 20 72 65 61 6c ema-version real
19403 6c 79 20 64 6f 65 73 20 63 68 61 6e 67 65 20 69 ly does change i
19404 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 61 73 n. ** the cas
19405 65 20 77 68 65 72 65 20 74 68 65 20 73 6f 75 72 e where the sour
19406 63 65 20 61 6e 64 20 64 65 73 74 69 6e 61 74 69 ce and destinati
19407 6f 6e 20 64 61 74 61 62 61 73 65 73 20 68 61 76 on databases hav
19408 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 73 61 6d e the. ** sam
19409 65 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e e schema version
1940a 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
1940b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 rc==SQLITE_DONE
1940c 20 0a 20 20 20 20 20 26 26 20 28 72 63 20 3d 20 . && (rc =
1940d 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 sqlite3BtreeUpda
1940e 74 65 4d 65 74 61 28 70 2d 3e 70 44 65 73 74 2c teMeta(p->pDest,
1940f 31 2c 70 2d 3e 69 44 65 73 74 53 63 68 65 6d 61 1,p->iDestSchema
19410 2b 31 29 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 0a +1))==SQLITE_OK.
19411 20 20 20 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e ){. con
19412 73 74 20 69 6e 74 20 6e 53 72 63 50 61 67 65 73 st int nSrcPages
19413 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 42 74 72 ize = sqlite3Btr
19414 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 70 2d eeGetPageSize(p-
19415 3e 70 53 72 63 29 3b 0a 20 20 20 20 20 20 63 6f >pSrc);. co
19416 6e 73 74 20 69 6e 74 20 6e 44 65 73 74 50 61 67 nst int nDestPag
19417 65 73 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 42 esize = sqlite3B
19418 74 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 treeGetPageSize(
19419 70 2d 3e 70 44 65 73 74 29 3b 0a 20 20 20 20 20 p->pDest);.
1941a 20 69 6e 74 20 6e 44 65 73 74 54 72 75 6e 63 61 int nDestTrunca
1941b 74 65 3b 0a 20 20 0a 20 20 20 20 20 20 69 66 28 te;. . if(
1941c 20 70 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 p->pDestDb ){.
1941d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 sqlite3Re
1941e 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d setInternalSchem
1941f 61 28 70 2d 3e 70 44 65 73 74 44 62 2c 20 30 29 a(p->pDestDb, 0)
19420 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 ;. }..
19421 20 2f 2a 20 53 65 74 20 6e 44 65 73 74 54 72 75 /* Set nDestTru
19422 6e 63 61 74 65 20 74 6f 20 74 68 65 20 66 69 6e ncate to the fin
19423 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 al number of pag
19424 65 73 20 69 6e 20 74 68 65 20 64 65 73 74 69 6e es in the destin
19425 61 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 64 ation. ** d
19426 61 74 61 62 61 73 65 2e 20 54 68 65 20 63 6f 6d atabase. The com
19427 70 6c 69 63 61 74 69 6f 6e 20 68 65 72 65 20 69 plication here i
19428 73 20 74 68 61 74 20 74 68 65 20 64 65 73 74 69 s that the desti
19429 6e 61 74 69 6f 6e 20 70 61 67 65 0a 20 20 20 20 nation page.
1942a 20 20 2a 2a 20 73 69 7a 65 20 6d 61 79 20 62 65 ** size may be
1942b 20 64 69 66 66 65 72 65 6e 74 20 74 6f 20 74 68 different to th
1942c 65 20 73 6f 75 72 63 65 20 70 61 67 65 20 73 69 e source page si
1942d 7a 65 2e 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 ze. . **.
1942e 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 73 6f ** If the so
1942f 75 72 63 65 20 70 61 67 65 20 73 69 7a 65 20 69 urce page size i
19430 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 74 s smaller than t
19431 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 he destination p
19432 61 67 65 20 73 69 7a 65 2c 20 0a 20 20 20 20 20 age size, .
19433 20 2a 2a 20 72 6f 75 6e 64 20 75 70 2e 20 49 6e ** round up. In
19434 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 63 this case the c
19435 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 all to sqlite3Os
19436 54 72 75 6e 63 61 74 65 28 29 20 62 65 6c 6f 77 Truncate() below
19437 20 77 69 6c 6c 0a 20 20 20 20 20 20 2a 2a 20 66 will. ** f
19438 69 78 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 ix the size of t
19439 68 65 20 66 69 6c 65 2e 20 48 6f 77 65 76 65 72 he file. However
1943a 20 69 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 it is important
1943b 20 74 6f 20 63 61 6c 6c 0a 20 20 20 20 20 20 2a to call. *
1943c 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 * sqlite3PagerTr
1943d 75 6e 63 61 74 65 49 6d 61 67 65 28 29 20 68 65 uncateImage() he
1943e 72 65 20 73 6f 20 74 68 61 74 20 61 6e 79 20 70 re so that any p
1943f 61 67 65 73 20 69 6e 20 74 68 65 20 0a 20 20 20 ages in the .
19440 20 20 20 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f ** destinatio
19441 6e 20 66 69 6c 65 20 74 68 61 74 20 6c 69 65 20 n file that lie
19442 62 65 79 6f 6e 64 20 74 68 65 20 6e 44 65 73 74 beyond the nDest
19443 54 72 75 6e 63 61 74 65 20 70 61 67 65 20 6d 61 Truncate page ma
19444 72 6b 20 61 72 65 0a 20 20 20 20 20 20 2a 2a 20 rk are. **
19445 6a 6f 75 72 6e 61 6c 6c 65 64 20 62 79 20 50 61 journalled by Pa
19446 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e gerCommitPhaseOn
19447 65 28 29 20 62 65 66 6f 72 65 20 74 68 65 79 20 e() before they
19448 61 72 65 20 64 65 73 74 72 6f 79 65 64 0a 20 20 are destroyed.
19449 20 20 20 20 2a 2a 20 62 79 20 74 68 65 20 66 69 ** by the fi
1944a 6c 65 20 74 72 75 6e 63 61 74 69 6f 6e 2e 0a 20 le truncation..
1944b 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 */. if
1944c 28 20 6e 53 72 63 50 61 67 65 73 69 7a 65 3c 6e ( nSrcPagesize<n
1944d 44 65 73 74 50 61 67 65 73 69 7a 65 20 29 7b 0a DestPagesize ){.
1944e 20 20 20 20 20 20 20 20 69 6e 74 20 72 61 74 69 int rati
1944f 6f 20 3d 20 6e 44 65 73 74 50 61 67 65 73 69 7a o = nDestPagesiz
19450 65 2f 6e 53 72 63 50 61 67 65 73 69 7a 65 3b 0a e/nSrcPagesize;.
19451 20 20 20 20 20 20 20 20 6e 44 65 73 74 54 72 75 nDestTru
19452 6e 63 61 74 65 20 3d 20 28 6e 53 72 63 50 61 67 ncate = (nSrcPag
19453 65 2b 72 61 74 69 6f 2d 31 29 2f 72 61 74 69 6f e+ratio-1)/ratio
19454 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 44 ;. if( nD
19455 65 73 74 54 72 75 6e 63 61 74 65 3d 3d 28 69 6e estTruncate==(in
19456 74 29 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 t)PENDING_BYTE_P
19457 41 47 45 28 70 2d 3e 70 44 65 73 74 2d 3e 70 42 AGE(p->pDest->pB
19458 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 t) ){.
19459 6e 44 65 73 74 54 72 75 6e 63 61 74 65 2d 2d 3b nDestTruncate--;
1945a 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1945b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
1945c 6e 44 65 73 74 54 72 75 6e 63 61 74 65 20 3d 20 nDestTruncate =
1945d 6e 53 72 63 50 61 67 65 20 2a 20 28 6e 53 72 63 nSrcPage * (nSrc
1945e 50 61 67 65 73 69 7a 65 2f 6e 44 65 73 74 50 61 Pagesize/nDestPa
1945f 67 65 73 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d gesize);. }
19460 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 . sqlite3Pa
19461 67 65 72 54 72 75 6e 63 61 74 65 49 6d 61 67 65 gerTruncateImage
19462 28 70 44 65 73 74 50 61 67 65 72 2c 20 6e 44 65 (pDestPager, nDe
19463 73 74 54 72 75 6e 63 61 74 65 29 3b 0a 0a 20 20 stTruncate);..
19464 20 20 20 20 69 66 28 20 6e 53 72 63 50 61 67 65 if( nSrcPage
19465 73 69 7a 65 3c 6e 44 65 73 74 50 61 67 65 73 69 size<nDestPagesi
19466 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a ze ){. /*
19467 20 49 66 20 74 68 65 20 73 6f 75 72 63 65 20 70 If the source p
19468 61 67 65 2d 73 69 7a 65 20 69 73 20 73 6d 61 6c age-size is smal
19469 6c 65 72 20 74 68 61 6e 20 74 68 65 20 64 65 73 ler than the des
1946a 74 69 6e 61 74 69 6f 6e 20 70 61 67 65 2d 73 69 tination page-si
1946b 7a 65 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 ze,. ** t
1946c 77 6f 20 65 78 74 72 61 20 74 68 69 6e 67 73 20 wo extra things
1946d 6d 61 79 20 6e 65 65 64 20 74 6f 20 68 61 70 70 may need to happ
1946e 65 6e 3a 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 en:. **.
1946f 20 20 20 20 20 20 20 2a 2a 20 20 20 2a 20 54 68 ** * Th
19470 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 6d 61 e destination ma
19471 79 20 6e 65 65 64 20 74 6f 20 62 65 20 74 72 75 y need to be tru
19472 6e 63 61 74 65 64 2c 20 61 6e 64 0a 20 20 20 20 ncated, and.
19473 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a **. *
19474 2a 20 20 20 2a 20 44 61 74 61 20 73 74 6f 72 65 * * Data store
19475 64 20 6f 6e 20 74 68 65 20 70 61 67 65 73 20 69 d on the pages i
19476 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f mmediately follo
19477 77 69 6e 67 20 74 68 65 20 0a 20 20 20 20 20 20 wing the .
19478 20 20 2a 2a 20 20 20 20 20 70 65 6e 64 69 6e 67 ** pending
19479 2d 62 79 74 65 20 70 61 67 65 20 69 6e 20 74 68 -byte page in th
1947a 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 e source databas
1947b 65 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 62 65 e may need to be
1947c 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 20 20 . **
1947d 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65 20 copied into the
1947e 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 destination data
1947f 62 61 73 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f base.. */
19480 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 69 . const i
19481 36 34 20 69 53 69 7a 65 20 3d 20 28 69 36 34 29 64 iSize = (i64)
19482 6e 53 72 63 50 61 67 65 73 69 7a 65 20 2a 20 28 nSrcPagesize * (
19483 69 36 34 29 6e 53 72 63 50 61 67 65 3b 0a 20 20 i64)nSrcPage;.
19484 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 sqlite3_fi
19485 6c 65 20 2a 20 63 6f 6e 73 74 20 70 46 69 6c 65 le * const pFile
19486 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 46 = sqlite3PagerF
19487 69 6c 65 28 70 44 65 73 74 50 61 67 65 72 29 3b ile(pDestPager);
19488 0a 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 .. assert
19489 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 20 20 20 ( pFile );.
1948a 20 20 20 61 73 73 65 72 74 28 20 28 69 36 34 29 assert( (i64)
1948b 6e 44 65 73 74 54 72 75 6e 63 61 74 65 2a 28 69 nDestTruncate*(i
1948c 36 34 29 6e 44 65 73 74 50 61 67 65 73 69 7a 65 64)nDestPagesize
1948d 20 3e 3d 20 69 53 69 7a 65 20 7c 7c 20 28 0a 20 >= iSize || (.
1948e 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 44 65 nDe
1948f 73 74 54 72 75 6e 63 61 74 65 3d 3d 28 69 6e 74 stTruncate==(int
19490 29 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 )(PENDING_BYTE_P
19491 41 47 45 28 70 2d 3e 70 44 65 73 74 2d 3e 70 42 AGE(p->pDest->pB
19492 74 29 2d 31 29 0a 20 20 20 20 20 20 20 20 20 20 t)-1).
19493 20 26 26 20 69 53 69 7a 65 3e 3d 50 45 4e 44 49 && iSize>=PENDI
19494 4e 47 5f 42 59 54 45 20 26 26 20 69 53 69 7a 65 NG_BYTE && iSize
19495 3c 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 6e <=PENDING_BYTE+n
19496 44 65 73 74 50 61 67 65 73 69 7a 65 0a 20 20 20 DestPagesize.
19497 20 20 20 20 20 29 29 3b 0a 20 20 20 20 20 20 20 ));.
19498 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d if( SQLITE_OK==
19499 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 (rc = sqlite3Pag
1949a 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 erCommitPhaseOne
1949b 28 70 44 65 73 74 50 61 67 65 72 2c 20 30 2c 20 (pDestPager, 0,
1949c 31 29 29 0a 20 20 20 20 20 20 20 20 20 26 26 20 1)). &&
1949d 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d SQLITE_OK==(rc =
1949e 20 62 61 63 6b 75 70 54 72 75 6e 63 61 74 65 46 backupTruncateF
1949f 69 6c 65 28 70 46 69 6c 65 2c 20 69 53 69 7a 65 ile(pFile, iSize
194a0 29 29 0a 20 20 20 20 20 20 20 20 20 26 26 20 53 )). && S
194a1 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 QLITE_OK==(rc =
194a2 73 71 6c 69 74 65 33 50 61 67 65 72 53 79 6e 63 sqlite3PagerSync
194a3 28 70 44 65 73 74 50 61 67 65 72 29 29 0a 20 20 (pDestPager)).
194a4 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 ){.
194a5 20 20 20 69 36 34 20 69 4f 66 66 3b 0a 20 20 20 i64 iOff;.
194a6 20 20 20 20 20 20 20 69 36 34 20 69 45 6e 64 20 i64 iEnd
194a7 3d 20 4d 49 4e 28 50 45 4e 44 49 4e 47 5f 42 59 = MIN(PENDING_BY
194a8 54 45 20 2b 20 6e 44 65 73 74 50 61 67 65 73 69 TE + nDestPagesi
194a9 7a 65 2c 20 69 53 69 7a 65 29 3b 0a 20 20 20 20 ze, iSize);.
194aa 20 20 20 20 20 20 66 6f 72 28 0a 20 20 20 20 20 for(.
194ab 20 20 20 20 20 20 20 69 4f 66 66 3d 50 45 4e 44 iOff=PEND
194ac 49 4e 47 5f 42 59 54 45 2b 6e 53 72 63 50 61 67 ING_BYTE+nSrcPag
194ad 65 73 69 7a 65 3b 20 0a 20 20 20 20 20 20 20 20 esize; .
194ae 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f rc==SQLITE_O
194af 4b 20 26 26 20 69 4f 66 66 3c 69 45 6e 64 3b 20 K && iOff<iEnd;
194b0 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 4f 66 . iOf
194b1 66 2b 3d 6e 53 72 63 50 61 67 65 73 69 7a 65 0a f+=nSrcPagesize.
194b2 20 20 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 ){.
194b3 20 20 20 20 20 20 20 20 20 50 67 48 64 72 20 2a PgHdr *
194b4 70 53 72 63 50 67 20 3d 20 30 3b 0a 20 20 20 20 pSrcPg = 0;.
194b5 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 50 67 const Pg
194b6 6e 6f 20 69 53 72 63 50 67 20 3d 20 28 50 67 6e no iSrcPg = (Pgn
194b7 6f 29 28 28 69 4f 66 66 2f 6e 53 72 63 50 61 67 o)((iOff/nSrcPag
194b8 65 73 69 7a 65 29 2b 31 29 3b 0a 20 20 20 20 20 esize)+1);.
194b9 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
194ba 74 65 33 50 61 67 65 72 47 65 74 28 70 53 72 63 te3PagerGet(pSrc
194bb 50 61 67 65 72 2c 20 69 53 72 63 50 67 2c 20 26 Pager, iSrcPg, &
194bc 70 53 72 63 50 67 29 3b 0a 20 20 20 20 20 20 20 pSrcPg);.
194bd 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
194be 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
194bf 20 20 20 20 20 20 20 20 75 38 20 2a 7a 44 61 74 u8 *zDat
194c0 61 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 a = sqlite3Pager
194c1 47 65 74 44 61 74 61 28 70 53 72 63 50 67 29 3b GetData(pSrcPg);
194c2 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 . r
194c3 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 c = sqlite3OsWri
194c4 74 65 28 70 46 69 6c 65 2c 20 7a 44 61 74 61 2c te(pFile, zData,
194c5 20 6e 53 72 63 50 61 67 65 73 69 7a 65 2c 20 69 nSrcPagesize, i
194c6 4f 66 66 29 3b 0a 20 20 20 20 20 20 20 20 20 20 Off);.
194c7 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 }.
194c8 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 sqlite3PagerUnre
194c9 66 28 70 53 72 63 50 67 29 3b 0a 20 20 20 20 20 f(pSrcPg);.
194ca 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
194cb 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
194cc 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
194cd 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 e3PagerCommitPha
194ce 73 65 4f 6e 65 28 70 44 65 73 74 50 61 67 65 72 seOne(pDestPager
194cf 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d , 0, 0);. }
194d0 0a 20 20 0a 20 20 20 20 20 20 2f 2a 20 46 69 6e . . /* Fin
194d1 69 73 68 20 63 6f 6d 6d 69 74 74 69 6e 67 20 74 ish committing t
194d2 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 he transaction t
194d3 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f o the destinatio
194d4 6e 20 64 61 74 61 62 61 73 65 2e 20 2a 2f 0a 20 n database. */.
194d5 20 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f if( SQLITE_
194d6 4f 4b 3d 3d 72 63 0a 20 20 20 20 20 20 20 26 26 OK==rc. &&
194d7 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 SQLITE_OK==(rc
194d8 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f = sqlite3BtreeCo
194d9 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70 2d 3e mmitPhaseTwo(p->
194da 70 44 65 73 74 29 29 0a 20 20 20 20 20 20 29 7b pDest)). ){
194db 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 . rc = SQ
194dc 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20 LITE_DONE;.
194dd 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 }. }. .
194de 2f 2a 20 49 66 20 62 43 6c 6f 73 65 54 72 61 6e /* If bCloseTran
194df 73 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 s is true, then
194e0 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6f 70 this function op
194e1 65 6e 65 64 20 61 20 72 65 61 64 20 74 72 61 6e ened a read tran
194e2 73 61 63 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 6f saction. ** o
194e3 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 n the source dat
194e4 61 62 61 73 65 2e 20 43 6c 6f 73 65 20 74 68 65 abase. Close the
194e5 20 72 65 61 64 20 74 72 61 6e 73 61 63 74 69 6f read transactio
194e6 6e 20 68 65 72 65 2e 20 54 68 65 72 65 20 69 73 n here. There is
194e7 0a 20 20 20 20 2a 2a 20 6e 6f 20 6e 65 65 64 20 . ** no need
194e8 74 6f 20 63 68 65 63 6b 20 74 68 65 20 72 65 74 to check the ret
194e9 75 72 6e 20 76 61 6c 75 65 73 20 6f 66 20 74 68 urn values of th
194ea 65 20 62 74 72 65 65 20 6d 65 74 68 6f 64 73 20 e btree methods
194eb 68 65 72 65 2c 20 61 73 0a 20 20 20 20 2a 2a 20 here, as. **
194ec 22 63 6f 6d 6d 69 74 74 69 6e 67 22 20 61 20 72 "committing" a r
194ed 65 61 64 2d 6f 6e 6c 79 20 74 72 61 6e 73 61 63 ead-only transac
194ee 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c tion cannot fail
194ef 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
194f0 20 62 43 6c 6f 73 65 54 72 61 6e 73 20 29 7b 0a bCloseTrans ){.
194f1 20 20 20 20 20 20 54 45 53 54 4f 4e 4c 59 28 20 TESTONLY(
194f2 69 6e 74 20 72 63 32 20 29 3b 0a 20 20 20 20 20 int rc2 );.
194f3 20 54 45 53 54 4f 4e 4c 59 28 20 72 63 32 20 20 TESTONLY( rc2
194f4 3d 20 29 20 73 71 6c 69 74 65 33 42 74 72 65 65 = ) sqlite3Btree
194f5 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 CommitPhaseOne(p
194f6 2d 3e 70 53 72 63 2c 20 30 29 3b 0a 20 20 20 20 ->pSrc, 0);.
194f7 20 20 54 45 53 54 4f 4e 4c 59 28 20 72 63 32 20 TESTONLY( rc2
194f8 7c 3d 20 29 20 73 71 6c 69 74 65 33 42 74 72 65 |= ) sqlite3Btre
194f9 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 eCommitPhaseTwo(
194fa 70 2d 3e 70 53 72 63 29 3b 0a 20 20 20 20 20 20 p->pSrc);.
194fb 61 73 73 65 72 74 28 20 72 63 32 3d 3d 53 51 4c assert( rc2==SQL
194fc 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 7d 0a ITE_OK );. }.
194fd 20 20 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 . p->rc = r
194fe 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e c;. }. if( p->
194ff 70 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 73 pDestDb ){. s
19500 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
19501 76 65 28 70 2d 3e 70 44 65 73 74 44 62 2d 3e 6d ve(p->pDestDb->m
19502 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 73 71 6c utex);. }. sql
19503 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
19504 2d 3e 70 53 72 63 29 3b 0a 20 20 73 71 6c 69 74 ->pSrc);. sqlit
19505 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 e3_mutex_leave(p
19506 2d 3e 70 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 ->pSrcDb->mutex)
19507 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
19508 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 ../*.** Release
19509 61 6c 6c 20 72 65 73 6f 75 72 63 65 73 20 61 73 all resources as
1950a 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 6e sociated with an
1950b 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 2a sqlite3_backup*
1950c 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 handle..*/.SQLI
1950d 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
1950e 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 e3_backup_finish
1950f 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 (sqlite3_backup
19510 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62 *p){. sqlite3_b
19511 61 63 6b 75 70 20 2a 2a 70 70 3b 20 20 20 20 20 ackup **pp;
19512 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
19513 74 72 20 74 6f 20 68 65 61 64 20 6f 66 20 70 61 tr to head of pa
19514 67 65 72 73 20 62 61 63 6b 75 70 20 6c 69 73 74 gers backup list
19515 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 */. sqlite3_mu
19516 74 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 20 20 tex *mutex;
19517 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 /* Mu
19518 74 65 78 20 74 6f 20 70 72 6f 74 65 63 74 20 73 tex to protect s
19519 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 2a ource database *
1951a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 /. int rc;
1951b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1951c 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 /* Valu
1951d 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 0a e to return */..
1951e 20 20 2f 2a 20 45 6e 74 65 72 20 74 68 65 20 6d /* Enter the m
1951f 75 74 65 78 65 73 20 2a 2f 0a 20 20 69 66 28 20 utexes */. if(
19520 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 p==0 ) return SQ
19521 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 71 6c 69 74 LITE_OK;. sqlit
19522 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 e3_mutex_enter(p
19523 2d 3e 70 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 ->pSrcDb->mutex)
19524 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
19525 45 6e 74 65 72 28 70 2d 3e 70 53 72 63 29 3b 0a Enter(p->pSrc);.
19526 20 20 6d 75 74 65 78 20 3d 20 70 2d 3e 70 53 72 mutex = p->pSr
19527 63 44 62 2d 3e 6d 75 74 65 78 3b 0a 20 20 69 66 cDb->mutex;. if
19528 28 20 70 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a ( p->pDestDb ){.
19529 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
1952a 78 5f 65 6e 74 65 72 28 70 2d 3e 70 44 65 73 74 x_enter(p->pDest
1952b 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a Db->mutex);. }.
1952c 0a 20 20 2f 2a 20 44 65 74 61 63 68 20 74 68 69 . /* Detach thi
1952d 73 20 62 61 63 6b 75 70 20 66 72 6f 6d 20 74 68 s backup from th
1952e 65 20 73 6f 75 72 63 65 20 70 61 67 65 72 2e 20 e source pager.
1952f 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 44 65 73 */. if( p->pDes
19530 74 44 62 20 29 7b 0a 20 20 20 20 70 2d 3e 70 53 tDb ){. p->pS
19531 72 63 2d 3e 6e 42 61 63 6b 75 70 2d 2d 3b 0a 20 rc->nBackup--;.
19532 20 7d 0a 20 20 69 66 28 20 70 2d 3e 69 73 41 74 }. if( p->isAt
19533 74 61 63 68 65 64 20 29 7b 0a 20 20 20 20 70 70 tached ){. pp
19534 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 42 = sqlite3PagerB
19535 61 63 6b 75 70 50 74 72 28 73 71 6c 69 74 65 33 ackupPtr(sqlite3
19536 42 74 72 65 65 50 61 67 65 72 28 70 2d 3e 70 53 BtreePager(p->pS
19537 72 63 29 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 rc));. while(
19538 20 2a 70 70 21 3d 70 20 29 7b 0a 20 20 20 20 20 *pp!=p ){.
19539 20 70 70 20 3d 20 26 28 2a 70 70 29 2d 3e 70 4e pp = &(*pp)->pN
1953a 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a ext;. }. *
1953b 70 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 pp = p->pNext;.
1953c 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 20 74 72 }.. /* If a tr
1953d 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 69 ansaction is sti
1953e 6c 6c 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 42 ll open on the B
1953f 74 72 65 65 2c 20 72 6f 6c 6c 20 69 74 20 62 61 tree, roll it ba
19540 63 6b 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ck. */. sqlite3
19541 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 70 2d BtreeRollback(p-
19542 3e 70 44 65 73 74 29 3b 0a 0a 20 20 2f 2a 20 53 >pDest);.. /* S
19543 65 74 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 et the error cod
19544 65 20 6f 66 20 74 68 65 20 64 65 73 74 69 6e 61 e of the destina
19545 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 68 61 tion database ha
19546 6e 64 6c 65 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 ndle. */. rc =
19547 28 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 44 (p->rc==SQLITE_D
19548 4f 4e 45 29 20 3f 20 53 51 4c 49 54 45 5f 4f 4b ONE) ? SQLITE_OK
19549 20 3a 20 70 2d 3e 72 63 3b 0a 20 20 73 71 6c 69 : p->rc;. sqli
1954a 74 65 33 45 72 72 6f 72 28 70 2d 3e 70 44 65 73 te3Error(p->pDes
1954b 74 44 62 2c 20 72 63 2c 20 30 29 3b 0a 0a 20 20 tDb, rc, 0);..
1954c 2f 2a 20 45 78 69 74 20 74 68 65 20 6d 75 74 65 /* Exit the mute
1954d 78 65 73 20 61 6e 64 20 66 72 65 65 20 74 68 65 xes and free the
1954e 20 62 61 63 6b 75 70 20 63 6f 6e 74 65 78 74 20 backup context
1954f 73 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a 20 20 structure. */.
19550 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 29 if( p->pDestDb )
19551 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 {. sqlite3_mu
19552 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 70 44 65 tex_leave(p->pDe
19553 73 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 stDb->mutex);.
19554 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 }. sqlite3Btree
19555 4c 65 61 76 65 28 70 2d 3e 70 53 72 63 29 3b 0a Leave(p->pSrc);.
19556 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62 if( p->pDestDb
19557 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
19558 66 72 65 65 28 70 29 3b 0a 20 20 7d 0a 20 20 73 free(p);. }. s
19559 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
1955a 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 ve(mutex);. ret
1955b 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
1955c 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
1955d 65 72 20 6f 66 20 70 61 67 65 73 20 73 74 69 6c er of pages stil
1955e 6c 20 74 6f 20 62 65 20 62 61 63 6b 65 64 20 75 l to be backed u
1955f 70 20 61 73 20 6f 66 20 74 68 65 20 6d 6f 73 74 p as of the most
19560 20 72 65 63 65 6e 74 0a 2a 2a 20 63 61 6c 6c 20 recent.** call
19561 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 to sqlite3_backu
19562 70 5f 73 74 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c p_step()..*/.SQL
19563 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
19564 74 65 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69 te3_backup_remai
19565 6e 69 6e 67 28 73 71 6c 69 74 65 33 5f 62 61 63 ning(sqlite3_bac
19566 6b 75 70 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 kup *p){. retur
19567 6e 20 70 2d 3e 6e 52 65 6d 61 69 6e 69 6e 67 3b n p->nRemaining;
19568 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
19569 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 the total numbe
1956a 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 r of pages in th
1956b 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 e source databas
1956c 65 20 61 73 20 6f 66 20 74 68 65 20 6d 6f 73 74 e as of the most
1956d 20 0a 2a 2a 20 72 65 63 65 6e 74 20 63 61 6c 6c .** recent call
1956e 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b to sqlite3_back
1956f 75 70 5f 73 74 65 70 28 29 2e 0a 2a 2f 0a 53 51 up_step()..*/.SQ
19570 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
19571 69 74 65 33 5f 62 61 63 6b 75 70 5f 70 61 67 65 ite3_backup_page
19572 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 62 61 count(sqlite3_ba
19573 63 6b 75 70 20 2a 70 29 7b 0a 20 20 72 65 74 75 ckup *p){. retu
19574 72 6e 20 70 2d 3e 6e 50 61 67 65 63 6f 75 6e 74 rn p->nPagecount
19575 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
19576 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
19577 65 64 20 61 66 74 65 72 20 74 68 65 20 63 6f 6e ed after the con
19578 74 65 6e 74 73 20 6f 66 20 70 61 67 65 20 69 50 tents of page iP
19579 61 67 65 20 6f 66 20 74 68 65 0a 2a 2a 20 73 6f age of the.** so
1957a 75 72 63 65 20 64 61 74 61 62 61 73 65 20 68 61 urce database ha
1957b 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 ve been modified
1957c 2e 20 49 66 20 70 61 67 65 20 69 50 61 67 65 20 . If page iPage
1957d 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e has already been
1957e 20 0a 2a 2a 20 63 6f 70 69 65 64 20 69 6e 74 6f .** copied into
1957f 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e the destination
19580 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20 database, then
19581 74 68 65 20 64 61 74 61 20 77 72 69 74 74 65 6e the data written
19582 20 74 6f 20 74 68 65 0a 2a 2a 20 64 65 73 74 69 to the.** desti
19583 6e 61 74 69 6f 6e 20 69 73 20 6e 6f 77 20 69 6e nation is now in
19584 76 61 6c 69 64 61 74 65 64 2e 20 54 68 65 20 64 validated. The d
19585 65 73 74 69 6e 61 74 69 6f 6e 20 63 6f 70 79 20 estination copy
19586 6f 66 20 69 50 61 67 65 20 6e 65 65 64 73 0a 2a of iPage needs.*
19587 2a 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 20 * to be updated
19588 77 69 74 68 20 74 68 65 20 6e 65 77 20 64 61 74 with the new dat
19589 61 20 62 65 66 6f 72 65 20 74 68 65 20 62 61 63 a before the bac
1958a 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 kup operation is
1958b 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 2e 0a 2a 2a .** complete..**
1958c 0a 2a 2a 20 49 74 20 69 73 20 61 73 73 75 6d 65 .** It is assume
1958d 64 20 74 68 61 74 20 74 68 65 20 6d 75 74 65 78 d that the mutex
1958e 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
1958f 20 74 68 65 20 42 74 53 68 61 72 65 64 20 6f 62 the BtShared ob
19590 6a 65 63 74 0a 2a 2a 20 63 6f 72 72 65 73 70 6f ject.** correspo
19591 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 73 6f 75 nding to the sou
19592 72 63 65 20 64 61 74 61 62 61 73 65 20 69 73 20 rce database is
19593 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 held when this f
19594 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 63 61 unction is.** ca
19595 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f lled..*/.SQLITE_
19596 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
19597 69 74 65 33 42 61 63 6b 75 70 55 70 64 61 74 65 ite3BackupUpdate
19598 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 (sqlite3_backup
19599 2a 70 42 61 63 6b 75 70 2c 20 50 67 6e 6f 20 69 *pBackup, Pgno i
1959a 50 61 67 65 2c 20 63 6f 6e 73 74 20 75 38 20 2a Page, const u8 *
1959b 61 44 61 74 61 29 7b 0a 20 20 73 71 6c 69 74 65 aData){. sqlite
1959c 33 5f 62 61 63 6b 75 70 20 2a 70 3b 20 20 20 20 3_backup *p;
1959d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1959e 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 * Iterator varia
1959f 62 6c 65 20 2a 2f 0a 20 20 66 6f 72 28 70 3d 70 ble */. for(p=p
195a0 42 61 63 6b 75 70 3b 20 70 3b 20 70 3d 70 2d 3e Backup; p; p=p->
195a1 70 4e 65 78 74 29 7b 0a 20 20 20 20 61 73 73 65 pNext){. asse
195a2 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
195a3 78 5f 68 65 6c 64 28 70 2d 3e 70 53 72 63 2d 3e x_held(p->pSrc->
195a4 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
195a5 20 20 20 69 66 28 20 21 69 73 46 61 74 61 6c 45 if( !isFatalE
195a6 72 72 6f 72 28 70 2d 3e 72 63 29 20 26 26 20 69 rror(p->rc) && i
195a7 50 61 67 65 3c 70 2d 3e 69 4e 65 78 74 20 29 7b Page<p->iNext ){
195a8 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 61 . /* The ba
195a9 63 6b 75 70 20 70 72 6f 63 65 73 73 20 70 20 68 ckup process p h
195aa 61 73 20 61 6c 72 65 61 64 79 20 63 6f 70 69 65 as already copie
195ab 64 20 70 61 67 65 20 69 50 61 67 65 2e 20 42 75 d page iPage. Bu
195ac 74 20 6e 6f 77 20 69 74 0a 20 20 20 20 20 20 2a t now it. *
195ad 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 66 * has been modif
195ae 69 65 64 20 62 79 20 61 20 74 72 61 6e 73 61 63 ied by a transac
195af 74 69 6f 6e 20 6f 6e 20 74 68 65 20 73 6f 75 72 tion on the sour
195b0 63 65 20 70 61 67 65 72 2e 20 43 6f 70 79 0a 20 ce pager. Copy.
195b1 20 20 20 20 20 2a 2a 20 74 68 65 20 6e 65 77 20 ** the new
195b2 64 61 74 61 20 69 6e 74 6f 20 74 68 65 20 62 61 data into the ba
195b3 63 6b 75 70 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 ckup.. */.
195b4 20 20 20 20 20 69 6e 74 20 72 63 20 3d 20 62 61 int rc = ba
195b5 63 6b 75 70 4f 6e 65 50 61 67 65 28 70 2c 20 69 ckupOnePage(p, i
195b6 50 61 67 65 2c 20 61 44 61 74 61 29 3b 0a 20 20 Page, aData);.
195b7 20 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d assert( rc!=
195b8 53 51 4c 49 54 45 5f 42 55 53 59 20 26 26 20 72 SQLITE_BUSY && r
195b9 63 21 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 c!=SQLITE_LOCKED
195ba 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 );. if( rc
195bb 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
195bc 20 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 p->rc = r
195bd 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d c;. }. }
195be 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 . }.}../*.** Re
195bf 73 74 61 72 74 20 74 68 65 20 62 61 63 6b 75 70 start the backup
195c0 20 70 72 6f 63 65 73 73 2e 20 54 68 69 73 20 69 process. This i
195c1 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 s called when th
195c2 65 20 70 61 67 65 72 20 6c 61 79 65 72 0a 2a 2a e pager layer.**
195c3 20 64 65 74 65 63 74 73 20 74 68 61 74 20 74 68 detects that th
195c4 65 20 64 61 74 61 62 61 73 65 20 68 61 73 20 62 e database has b
195c5 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 62 79 20 een modified by
195c6 61 6e 20 65 78 74 65 72 6e 61 6c 20 64 61 74 61 an external data
195c7 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 base.** connecti
195c8 6f 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 on. In this case
195c9 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 there is no way
195ca 20 6f 66 20 6b 6e 6f 77 69 6e 67 20 77 68 69 63 of knowing whic
195cb 68 20 6f 66 20 74 68 65 0a 2a 2a 20 70 61 67 65 h of the.** page
195cc 73 20 74 68 61 74 20 68 61 76 65 20 62 65 65 6e s that have been
195cd 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65 copied into the
195ce 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 destination dat
195cf 61 62 61 73 65 20 61 72 65 20 73 74 69 6c 6c 20 abase are still
195d0 0a 2a 2a 20 76 61 6c 69 64 20 61 6e 64 20 77 68 .** valid and wh
195d1 69 63 68 20 61 72 65 20 6e 6f 74 2c 20 73 6f 20 ich are not, so
195d2 74 68 65 20 65 6e 74 69 72 65 20 70 72 6f 63 65 the entire proce
195d3 73 73 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 ss needs to be r
195d4 65 73 74 61 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 estarted..**.**
195d5 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 It is assumed th
195d6 61 74 20 74 68 65 20 6d 75 74 65 78 20 61 73 73 at the mutex ass
195d7 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 ociated with the
195d8 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 BtShared object
195d9 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e .** correspondin
195da 67 20 74 6f 20 74 68 65 20 73 6f 75 72 63 65 20 g to the source
195db 64 61 74 61 62 61 73 65 20 69 73 20 68 65 6c 64 database is held
195dc 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 when this funct
195dd 69 6f 6e 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 ion is.** called
195de 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
195df 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
195e0 42 61 63 6b 75 70 52 65 73 74 61 72 74 28 73 71 BackupRestart(sq
195e1 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 42 lite3_backup *pB
195e2 61 63 6b 75 70 29 7b 0a 20 20 73 71 6c 69 74 65 ackup){. sqlite
195e3 33 5f 62 61 63 6b 75 70 20 2a 70 3b 20 20 20 20 3_backup *p;
195e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
195e5 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 * Iterator varia
195e6 62 6c 65 20 2a 2f 0a 20 20 66 6f 72 28 70 3d 70 ble */. for(p=p
195e7 42 61 63 6b 75 70 3b 20 70 3b 20 70 3d 70 2d 3e Backup; p; p=p->
195e8 70 4e 65 78 74 29 7b 0a 20 20 20 20 61 73 73 65 pNext){. asse
195e9 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
195ea 78 5f 68 65 6c 64 28 70 2d 3e 70 53 72 63 2d 3e x_held(p->pSrc->
195eb 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
195ec 20 20 20 70 2d 3e 69 4e 65 78 74 20 3d 20 31 3b p->iNext = 1;
195ed 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 . }.}..#ifndef
195ee 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 SQLITE_OMIT_VACU
195ef 55 4d 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20 74 68 UM./*.** Copy th
195f0 65 20 63 6f 6d 70 6c 65 74 65 20 63 6f 6e 74 65 e complete conte
195f1 6e 74 20 6f 66 20 70 42 74 46 72 6f 6d 20 69 6e nt of pBtFrom in
195f2 74 6f 20 70 42 74 54 6f 2e 20 20 41 20 74 72 61 to pBtTo. A tra
195f3 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 nsaction.** must
195f4 20 62 65 20 61 63 74 69 76 65 20 66 6f 72 20 62 be active for b
195f5 6f 74 68 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a oth files..**.**
195f6 20 54 68 65 20 73 69 7a 65 20 6f 66 20 66 69 6c The size of fil
195f7 65 20 70 54 6f 20 6d 61 79 20 62 65 20 72 65 64 e pTo may be red
195f8 75 63 65 64 20 62 79 20 74 68 69 73 20 6f 70 65 uced by this ope
195f9 72 61 74 69 6f 6e 2e 20 49 66 20 61 6e 79 74 68 ration. If anyth
195fa 69 6e 67 20 0a 2a 2a 20 67 6f 65 73 20 77 72 6f ing .** goes wro
195fb 6e 67 2c 20 74 68 65 20 74 72 61 6e 73 61 63 74 ng, the transact
195fc 69 6f 6e 20 6f 6e 20 70 54 6f 20 69 73 20 72 6f ion on pTo is ro
195fd 6c 6c 65 64 20 62 61 63 6b 2e 20 49 66 20 73 75 lled back. If su
195fe 63 63 65 73 73 66 75 6c 2c 20 74 68 65 20 0a 2a ccessful, the .*
195ff 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 * transaction is
19600 20 63 6f 6d 6d 69 74 74 65 64 20 62 65 66 6f 72 committed befor
19601 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a e returning..*/.
19602 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
19603 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
19604 6f 70 79 46 69 6c 65 28 42 74 72 65 65 20 2a 70 opyFile(Btree *p
19605 54 6f 2c 20 42 74 72 65 65 20 2a 70 46 72 6f 6d To, Btree *pFrom
19606 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 ){. int rc;. s
19607 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 62 3b qlite3_backup b;
19608 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
19609 6e 74 65 72 28 70 54 6f 29 3b 0a 20 20 73 71 6c nter(pTo);. sql
1960a 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 ite3BtreeEnter(p
1960b 46 72 6f 6d 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 From);.. /* Set
1960c 20 75 70 20 61 6e 20 73 71 6c 69 74 65 33 5f 62 up an sqlite3_b
1960d 61 63 6b 75 70 20 6f 62 6a 65 63 74 2e 20 73 71 ackup object. sq
1960e 6c 69 74 65 33 5f 62 61 63 6b 75 70 2e 70 44 65 lite3_backup.pDe
1960f 73 74 44 62 20 6d 75 73 74 20 62 65 20 73 65 74 stDb must be set
19610 0a 20 20 2a 2a 20 74 6f 20 30 2e 20 54 68 69 73 . ** to 0. This
19611 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 is used by the
19612 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 implementations
19613 6f 66 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 of sqlite3_backu
19614 70 5f 73 74 65 70 28 29 0a 20 20 2a 2a 20 61 6e p_step(). ** an
19615 64 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 d sqlite3_backup
19616 5f 66 69 6e 69 73 68 28 29 20 74 6f 20 64 65 74 _finish() to det
19617 65 63 74 20 74 68 61 74 20 74 68 65 79 20 61 72 ect that they ar
19618 65 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 0a 20 e being called.
19619 20 2a 2a 20 66 72 6f 6d 20 74 68 69 73 20 66 75 ** from this fu
1961a 6e 63 74 69 6f 6e 2c 20 6e 6f 74 20 64 69 72 65 nction, not dire
1961b 63 74 6c 79 20 62 79 20 74 68 65 20 75 73 65 72 ctly by the user
1961c 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 .. */. memset(
1961d 26 62 2c 20 30 2c 20 73 69 7a 65 6f 66 28 62 29 &b, 0, sizeof(b)
1961e 29 3b 0a 20 20 62 2e 70 53 72 63 44 62 20 3d 20 );. b.pSrcDb =
1961f 70 46 72 6f 6d 2d 3e 64 62 3b 0a 20 20 62 2e 70 pFrom->db;. b.p
19620 53 72 63 20 3d 20 70 46 72 6f 6d 3b 0a 20 20 62 Src = pFrom;. b
19621 2e 70 44 65 73 74 20 3d 20 70 54 6f 3b 0a 20 20 .pDest = pTo;.
19622 62 2e 69 4e 65 78 74 20 3d 20 31 3b 0a 0a 20 20 b.iNext = 1;..
19623 2f 2a 20 30 78 37 46 46 46 46 46 46 46 20 69 73 /* 0x7FFFFFFF is
19624 20 74 68 65 20 68 61 72 64 20 6c 69 6d 69 74 20 the hard limit
19625 66 6f 72 20 74 68 65 20 6e 75 6d 62 65 72 20 6f for the number o
19626 66 20 70 61 67 65 73 20 69 6e 20 61 20 64 61 74 f pages in a dat
19627 61 62 61 73 65 0a 20 20 2a 2a 20 66 69 6c 65 2e abase. ** file.
19628 20 42 79 20 70 61 73 73 69 6e 67 20 74 68 69 73 By passing this
19629 20 61 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f as the number o
1962a 66 20 70 61 67 65 73 20 74 6f 20 63 6f 70 79 20 f pages to copy
1962b 74 6f 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f to. ** sqlite3_
1962c 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2c 20 77 backup_step(), w
1962d 65 20 63 61 6e 20 67 75 61 72 61 6e 74 65 65 20 e can guarantee
1962e 74 68 61 74 20 74 68 65 20 63 6f 70 79 20 66 69 that the copy fi
1962f 6e 69 73 68 65 73 20 0a 20 20 2a 2a 20 77 69 74 nishes . ** wit
19630 68 69 6e 20 61 20 73 69 6e 67 6c 65 20 63 61 6c hin a single cal
19631 6c 20 28 75 6e 6c 65 73 73 20 61 6e 20 65 72 72 l (unless an err
19632 6f 72 20 6f 63 63 75 72 73 29 2e 20 54 68 65 20 or occurs). The
19633 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 assert() stateme
19634 6e 74 0a 20 20 2a 2a 20 63 68 65 63 6b 73 20 74 nt. ** checks t
19635 68 69 73 20 61 73 73 75 6d 70 74 69 6f 6e 20 2d his assumption -
19636 20 28 70 2d 3e 72 63 29 20 73 68 6f 75 6c 64 20 (p->rc) should
19637 62 65 20 73 65 74 20 74 6f 20 65 69 74 68 65 72 be set to either
19638 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 0a 20 20 SQLITE_DONE .
19639 2a 2a 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 ** or an error c
1963a 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 ode.. */. sqli
1963b 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 te3_backup_step(
1963c 26 62 2c 20 30 78 37 46 46 46 46 46 46 46 29 3b &b, 0x7FFFFFFF);
1963d 0a 20 20 61 73 73 65 72 74 28 20 62 2e 72 63 21 . assert( b.rc!
1963e 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 =SQLITE_OK );.
1963f 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 61 63 rc = sqlite3_bac
19640 6b 75 70 5f 66 69 6e 69 73 68 28 26 62 29 3b 0a kup_finish(&b);.
19641 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
19642 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 54 6f 2d 3e _OK ){. pTo->
19643 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 pBt->pageSizeFix
19644 65 64 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 73 ed = 0;. }.. s
19645 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
19646 28 70 46 72 6f 6d 29 3b 0a 20 20 73 71 6c 69 74 (pFrom);. sqlit
19647 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 54 6f e3BtreeLeave(pTo
19648 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
19649 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
1964a 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 20 2a TE_OMIT_VACUUM *
1964b 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /../************
1964c 2a 2a 20 45 6e 64 20 6f 66 20 62 61 63 6b 75 70 ** End of backup
1964d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
1964e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1964f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19650 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
19651 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 ** Begin file vd
19652 62 65 6d 65 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a bemem.c ********
19653 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19654 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19655 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 */./*.** 2004 Ma
19656 79 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 y 26.**.** The a
19657 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
19658 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
19659 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
1965a 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
1965b 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
1965c 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
1965d 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
1965e 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
1965f 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
19660 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
19661 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
19662 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
19663 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
19664 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
19665 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
19666 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
19667 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
19668 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19669 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1966a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1966b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1966c 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ***.**.** This f
1966d 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 ile contains cod
1966e 65 20 75 73 65 20 74 6f 20 6d 61 6e 69 70 75 6c e use to manipul
1966f 61 74 65 20 22 4d 65 6d 22 20 73 74 72 75 63 74 ate "Mem" struct
19670 75 72 65 2e 20 20 41 20 22 4d 65 6d 22 0a 2a 2a ure. A "Mem".**
19671 20 73 74 6f 72 65 73 20 61 20 73 69 6e 67 6c 65 stores a single
19672 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 56 44 value in the VD
19673 42 45 2e 20 20 4d 65 6d 20 69 73 20 61 6e 20 6f BE. Mem is an o
19674 70 61 71 75 65 20 73 74 72 75 63 74 75 72 65 20 paque structure
19675 76 69 73 69 62 6c 65 0a 2a 2a 20 6f 6e 6c 79 20 visible.** only
19676 77 69 74 68 69 6e 20 74 68 65 20 56 44 42 45 2e within the VDBE.
19677 20 20 49 6e 74 65 72 66 61 63 65 20 72 6f 75 74 Interface rout
19678 69 6e 65 73 20 72 65 66 65 72 20 74 6f 20 61 20 ines refer to a
19679 4d 65 6d 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a Mem using the.**
1967a 20 6e 61 6d 65 20 73 71 6c 69 74 65 5f 76 61 6c name sqlite_val
1967b 75 65 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 ue.**.** $Id: vd
1967c 62 65 6d 65 6d 2e 63 2c 76 20 31 2e 31 35 32 20 bemem.c,v 1.152
1967d 32 30 30 39 2f 30 37 2f 32 32 20 31 38 3a 30 37 2009/07/22 18:07
1967e 3a 34 31 20 64 72 68 20 45 78 70 20 24 0a 2a 2f :41 drh Exp $.*/
1967f 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c ../*.** Call sql
19680 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e ite3VdbeMemExpan
19681 64 42 6c 6f 62 28 29 20 6f 6e 20 74 68 65 20 73 dBlob() on the s
19682 75 70 70 6c 69 65 64 20 76 61 6c 75 65 20 28 74 upplied value (t
19683 79 70 65 20 4d 65 6d 2a 29 0a 2a 2a 20 50 20 69 ype Mem*).** P i
19684 66 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 23 f required..*/.#
19685 64 65 66 69 6e 65 20 65 78 70 61 6e 64 42 6c 6f define expandBlo
19686 62 28 50 29 20 28 28 28 50 29 2d 3e 66 6c 61 67 b(P) (((P)->flag
19687 73 26 4d 45 4d 5f 5a 65 72 6f 29 3f 73 71 6c 69 s&MEM_Zero)?sqli
19688 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 te3VdbeMemExpand
19689 42 6c 6f 62 28 50 29 3a 30 29 0a 0a 2f 2a 0a 2a Blob(P):0)../*.*
1968a 2a 20 49 66 20 70 4d 65 6d 20 69 73 20 61 6e 20 * If pMem is an
1968b 6f 62 6a 65 63 74 20 77 69 74 68 20 61 20 76 61 object with a va
1968c 6c 69 64 20 73 74 72 69 6e 67 20 72 65 70 72 65 lid string repre
1968d 73 65 6e 74 61 74 69 6f 6e 2c 20 74 68 69 73 20 sentation, this
1968e 72 6f 75 74 69 6e 65 0a 2a 2a 20 65 6e 73 75 72 routine.** ensur
1968f 65 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 es the internal
19690 65 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74 68 65 encoding for the
19691 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e string represen
19692 74 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 27 64 65 tation is.** 'de
19693 73 69 72 65 64 45 6e 63 27 2c 20 6f 6e 65 20 6f siredEnc', one o
19694 66 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 f SQLITE_UTF8, S
19695 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 6f 72 QLITE_UTF16LE or
19696 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2e SQLITE_UTF16BE.
19697 0a 2a 2a 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69 .**.** If pMem i
19698 73 20 6e 6f 74 20 61 20 73 74 72 69 6e 67 20 6f s not a string o
19699 62 6a 65 63 74 2c 20 6f 72 20 74 68 65 20 65 6e bject, or the en
1969a 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 73 74 coding of the st
1969b 72 69 6e 67 0a 2a 2a 20 72 65 70 72 65 73 65 6e ring.** represen
1969c 74 61 74 69 6f 6e 20 69 73 20 61 6c 72 65 61 64 tation is alread
1969d 79 20 73 74 6f 72 65 64 20 75 73 69 6e 67 20 74 y stored using t
1969e 68 65 20 72 65 71 75 65 73 74 65 64 20 65 6e 63 he requested enc
1969f 6f 64 69 6e 67 2c 20 74 68 65 6e 20 74 68 69 73 oding, then this
196a0 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 61 .** routine is a
196a1 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 53 51 no-op..**.** SQ
196a2 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 LITE_OK is retur
196a3 6e 65 64 20 69 66 20 74 68 65 20 63 6f 6e 76 65 ned if the conve
196a4 72 73 69 6f 6e 20 69 73 20 73 75 63 63 65 73 73 rsion is success
196a5 66 75 6c 20 28 6f 72 20 6e 6f 74 20 72 65 71 75 ful (or not requ
196a6 69 72 65 64 29 2e 0a 2a 2a 20 53 51 4c 49 54 45 ired)..** SQLITE
196a7 5f 4e 4f 4d 45 4d 20 6d 61 79 20 62 65 20 72 65 _NOMEM may be re
196a8 74 75 72 6e 65 64 20 69 66 20 61 20 6d 61 6c 6c turned if a mall
196a9 6f 63 28 29 20 66 61 69 6c 73 20 64 75 72 69 6e oc() fails durin
196aa 67 20 63 6f 6e 76 65 72 73 69 6f 6e 0a 2a 2a 20 g conversion.**
196ab 62 65 74 77 65 65 6e 20 66 6f 72 6d 61 74 73 2e between formats.
196ac 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
196ad 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
196ae 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 beChangeEncoding
196af 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 (Mem *pMem, int
196b0 64 65 73 69 72 65 64 45 6e 63 29 7b 0a 20 20 69 desiredEnc){. i
196b1 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 nt rc;. assert(
196b2 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 (pMem->flags&ME
196b3 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a M_RowSet)==0 );.
196b4 20 20 61 73 73 65 72 74 28 20 64 65 73 69 72 65 assert( desire
196b5 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 dEnc==SQLITE_UTF
196b6 38 20 7c 7c 20 64 65 73 69 72 65 64 45 6e 63 3d 8 || desiredEnc=
196b7 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 0a =SQLITE_UTF16LE.
196b8 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 64 65 || de
196b9 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 siredEnc==SQLITE
196ba 5f 55 54 46 31 36 42 45 20 29 3b 0a 20 20 69 66 _UTF16BE );. if
196bb 28 20 21 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 ( !(pMem->flags&
196bc 4d 45 4d 5f 53 74 72 29 20 7c 7c 20 70 4d 65 6d MEM_Str) || pMem
196bd 2d 3e 65 6e 63 3d 3d 64 65 73 69 72 65 64 45 6e ->enc==desiredEn
196be 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 c ){. return
196bf 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 SQLITE_OK;. }.
196c0 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 assert( pMem->d
196c1 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f b==0 || sqlite3_
196c2 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d mutex_held(pMem-
196c3 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 23 >db->mutex) );.#
196c4 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ifdef SQLITE_OMI
196c5 54 5f 55 54 46 31 36 0a 20 20 72 65 74 75 72 6e T_UTF16. return
196c6 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 23 SQLITE_ERROR;.#
196c7 65 6c 73 65 0a 0a 20 20 2f 2a 20 4d 65 6d 54 72 else.. /* MemTr
196c8 61 6e 73 6c 61 74 65 28 29 20 6d 61 79 20 72 65 anslate() may re
196c9 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f turn SQLITE_OK o
196ca 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20 r SQLITE_NOMEM.
196cb 49 66 20 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 If NOMEM is retu
196cc 72 6e 65 64 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 rned,. ** then
196cd 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 the encoding of
196ce 74 68 65 20 76 61 6c 75 65 20 6d 61 79 20 6e 6f the value may no
196cf 74 20 68 61 76 65 20 63 68 61 6e 67 65 64 2e 0a t have changed..
196d0 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 */. rc = sqli
196d1 74 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c te3VdbeMemTransl
196d2 61 74 65 28 70 4d 65 6d 2c 20 28 75 38 29 64 65 ate(pMem, (u8)de
196d3 73 69 72 65 64 45 6e 63 29 3b 0a 20 20 61 73 73 siredEnc);. ass
196d4 65 72 74 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f ert(rc==SQLITE_O
196d5 4b 20 20 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 K || rc==SQLI
196d6 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 61 73 73 TE_NOMEM);. ass
196d7 65 72 74 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f ert(rc==SQLITE_O
196d8 4b 20 20 20 20 7c 7c 20 70 4d 65 6d 2d 3e 65 6e K || pMem->en
196d9 63 21 3d 64 65 73 69 72 65 64 45 6e 63 29 3b 0a c!=desiredEnc);.
196da 20 20 61 73 73 65 72 74 28 72 63 3d 3d 53 51 4c assert(rc==SQL
196db 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 70 4d 65 ITE_NOMEM || pMe
196dc 6d 2d 3e 65 6e 63 3d 3d 64 65 73 69 72 65 64 45 m->enc==desiredE
196dd 6e 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 nc);. return rc
196de 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a ;.#endif.}../*.*
196df 2a 20 4d 61 6b 65 20 73 75 72 65 20 70 4d 65 6d * Make sure pMem
196e0 2d 3e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 ->z points to a
196e1 77 72 69 74 61 62 6c 65 20 61 6c 6c 6f 63 61 74 writable allocat
196e2 69 6f 6e 20 6f 66 20 61 74 20 6c 65 61 73 74 20 ion of at least
196e3 0a 2a 2a 20 6e 20 62 79 74 65 73 2e 0a 2a 2a 0a .** n bytes..**.
196e4 2a 2a 20 49 66 20 74 68 65 20 6d 65 6d 6f 72 79 ** If the memory
196e5 20 63 65 6c 6c 20 63 75 72 72 65 6e 74 6c 79 20 cell currently
196e6 63 6f 6e 74 61 69 6e 73 20 73 74 72 69 6e 67 20 contains string
196e7 6f 72 20 62 6c 6f 62 20 64 61 74 61 0a 2a 2a 20 or blob data.**
196e8 61 6e 64 20 74 68 65 20 74 68 69 72 64 20 61 72 and the third ar
196e9 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f gument passed to
196ea 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
196eb 73 20 74 72 75 65 2c 20 74 68 65 20 0a 2a 2a 20 s true, the .**
196ec 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 20 current content
196ed 6f 66 20 74 68 65 20 63 65 6c 6c 20 69 73 20 70 of the cell is p
196ee 72 65 73 65 72 76 65 64 2e 20 4f 74 68 65 72 77 reserved. Otherw
196ef 69 73 65 2c 20 69 74 20 6d 61 79 0a 2a 2a 20 62 ise, it may.** b
196f0 65 20 64 69 73 63 61 72 64 65 64 2e 20 20 0a 2a e discarded. .*
196f1 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
196f2 6f 6e 20 73 65 74 73 20 74 68 65 20 4d 45 4d 5f on sets the MEM_
196f3 44 79 6e 20 66 6c 61 67 20 61 6e 64 20 63 6c 65 Dyn flag and cle
196f4 61 72 73 20 61 6e 79 20 78 44 65 6c 20 63 61 6c ars any xDel cal
196f5 6c 62 61 63 6b 2e 0a 2a 2a 20 49 74 20 61 6c 73 lback..** It als
196f6 6f 20 63 6c 65 61 72 73 20 4d 45 4d 5f 45 70 68 o clears MEM_Eph
196f7 65 6d 20 61 6e 64 20 4d 45 4d 5f 53 74 61 74 69 em and MEM_Stati
196f8 63 2e 20 49 66 20 74 68 65 20 70 72 65 73 65 72 c. If the preser
196f9 76 65 20 66 6c 61 67 20 69 73 20 0a 2a 2a 20 6e ve flag is .** n
196fa 6f 74 20 73 65 74 2c 20 4d 65 6d 2e 6e 20 69 73 ot set, Mem.n is
196fb 20 7a 65 72 6f 65 64 2e 0a 2a 2f 0a 53 51 4c 49 zeroed..*/.SQLI
196fc 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
196fd 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f qlite3VdbeMemGro
196fe 77 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 w(Mem *pMem, int
196ff 20 6e 2c 20 69 6e 74 20 70 72 65 73 65 72 76 65 n, int preserve
19700 29 7b 0a 20 20 61 73 73 65 72 74 28 20 31 20 3e ){. assert( 1 >
19701 3d 0a 20 20 20 20 28 28 70 4d 65 6d 2d 3e 7a 4d =. ((pMem->zM
19702 61 6c 6c 6f 63 20 26 26 20 70 4d 65 6d 2d 3e 7a alloc && pMem->z
19703 4d 61 6c 6c 6f 63 3d 3d 70 4d 65 6d 2d 3e 7a 29 Malloc==pMem->z)
19704 20 3f 20 31 20 3a 20 30 29 20 2b 0a 20 20 20 20 ? 1 : 0) +.
19705 28 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d (((pMem->flags&M
19706 45 4d 5f 44 79 6e 29 26 26 70 4d 65 6d 2d 3e 78 EM_Dyn)&&pMem->x
19707 44 65 6c 29 20 3f 20 31 20 3a 20 30 29 20 2b 20 Del) ? 1 : 0) +
19708 0a 20 20 20 20 28 28 70 4d 65 6d 2d 3e 66 6c 61 . ((pMem->fla
19709 67 73 26 4d 45 4d 5f 45 70 68 65 6d 29 20 3f 20 gs&MEM_Ephem) ?
1970a 31 20 3a 20 30 29 20 2b 20 0a 20 20 20 20 28 28 1 : 0) + . ((
1970b 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f pMem->flags&MEM_
1970c 53 74 61 74 69 63 29 20 3f 20 31 20 3a 20 30 29 Static) ? 1 : 0)
1970d 0a 20 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 . );. assert(
1970e 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d (pMem->flags&MEM
1970f 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 0a _RowSet)==0 );..
19710 20 20 69 66 28 20 6e 3c 33 32 20 29 20 6e 20 3d if( n<32 ) n =
19711 20 33 32 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 32;. if( sqlit
19712 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 e3DbMallocSize(p
19713 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a Mem->db, pMem->z
19714 4d 61 6c 6c 6f 63 29 3c 6e 20 29 7b 0a 20 20 20 Malloc)<n ){.
19715 20 69 66 28 20 70 72 65 73 65 72 76 65 20 26 26 if( preserve &&
19716 20 70 4d 65 6d 2d 3e 7a 3d 3d 70 4d 65 6d 2d 3e pMem->z==pMem->
19717 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20 zMalloc ){.
19718 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4d 65 6d 2d pMem->z = pMem-
19719 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 >zMalloc = sqlit
1971a 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 e3DbReallocOrFre
1971b 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d e(pMem->db, pMem
1971c 2d 3e 7a 2c 20 6e 29 3b 0a 20 20 20 20 20 20 70 ->z, n);. p
1971d 72 65 73 65 72 76 65 20 3d 20 30 3b 0a 20 20 20 reserve = 0;.
1971e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 }else{. sq
1971f 6c 69 74 65 33 44 62 46 72 65 65 28 70 4d 65 6d lite3DbFree(pMem
19720 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 6c ->db, pMem->zMal
19721 6c 6f 63 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d loc);. pMem
19722 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c 69 ->zMalloc = sqli
19723 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 te3DbMallocRaw(p
19724 4d 65 6d 2d 3e 64 62 2c 20 6e 29 3b 0a 20 20 20 Mem->db, n);.
19725 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 4d }. }.. if( pM
19726 65 6d 2d 3e 7a 20 26 26 20 70 72 65 73 65 72 76 em->z && preserv
19727 65 20 26 26 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c e && pMem->zMall
19728 6f 63 20 26 26 20 70 4d 65 6d 2d 3e 7a 21 3d 70 oc && pMem->z!=p
19729 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a Mem->zMalloc ){.
1972a 20 20 20 20 6d 65 6d 63 70 79 28 70 4d 65 6d 2d memcpy(pMem-
1972b 3e 7a 4d 61 6c 6c 6f 63 2c 20 70 4d 65 6d 2d 3e >zMalloc, pMem->
1972c 7a 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 7d z, pMem->n);. }
1972d 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 . if( pMem->fla
1972e 67 73 26 4d 45 4d 5f 44 79 6e 20 26 26 20 70 4d gs&MEM_Dyn && pM
1972f 65 6d 2d 3e 78 44 65 6c 20 29 7b 0a 20 20 20 20 em->xDel ){.
19730 70 4d 65 6d 2d 3e 78 44 65 6c 28 28 76 6f 69 64 pMem->xDel((void
19731 20 2a 29 28 70 4d 65 6d 2d 3e 7a 29 29 3b 0a 20 *)(pMem->z));.
19732 20 7d 0a 0a 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 }.. pMem->z =
19733 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 3b 0a 20 pMem->zMalloc;.
19734 20 69 66 28 20 70 4d 65 6d 2d 3e 7a 3d 3d 30 20 if( pMem->z==0
19735 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 ){. pMem->fla
19736 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 gs = MEM_Null;.
19737 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4d 65 6d }else{. pMem
19738 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d ->flags &= ~(MEM
19739 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 74 69 _Ephem|MEM_Stati
1973a 63 29 3b 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e c);. }. pMem->
1973b 78 44 65 6c 20 3d 20 30 3b 0a 20 20 72 65 74 75 xDel = 0;. retu
1973c 72 6e 20 28 70 4d 65 6d 2d 3e 7a 20 3f 20 53 51 rn (pMem->z ? SQ
1973d 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 LITE_OK : SQLITE
1973e 5f 4e 4f 4d 45 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a _NOMEM);.}../*.*
1973f 2a 20 4d 61 6b 65 20 74 68 65 20 67 69 76 65 6e * Make the given
19740 20 4d 65 6d 20 6f 62 6a 65 63 74 20 4d 45 4d 5f Mem object MEM_
19741 44 79 6e 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 Dyn. In other w
19742 6f 72 64 73 2c 20 6d 61 6b 65 20 69 74 20 73 6f ords, make it so
19743 0a 2a 2a 20 74 68 61 74 20 61 6e 79 20 54 45 58 .** that any TEX
19744 54 20 6f 72 20 42 4c 4f 42 20 63 6f 6e 74 65 6e T or BLOB conten
19745 74 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 6d t is stored in m
19746 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 emory obtained f
19747 72 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 2e rom.** malloc().
19748 20 20 49 6e 20 74 68 69 73 20 77 61 79 2c 20 77 In this way, w
19749 65 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 20 e know that the
1974a 6d 65 6d 6f 72 79 20 69 73 20 73 61 66 65 20 74 memory is safe t
1974b 6f 20 62 65 0a 2a 2a 20 6f 76 65 72 77 72 69 74 o be.** overwrit
1974c 74 65 6e 20 6f 72 20 61 6c 74 65 72 65 64 2e 0a ten or altered..
1974d 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c **.** Return SQL
1974e 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 ITE_OK on succes
1974f 73 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 s or SQLITE_NOME
19750 4d 20 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c M if malloc fail
19751 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
19752 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
19753 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 VdbeMemMakeWrite
19754 61 62 6c 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b able(Mem *pMem){
19755 0a 20 20 69 6e 74 20 66 3b 0a 20 20 61 73 73 65 . int f;. asse
19756 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 rt( pMem->db==0
19757 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 || sqlite3_mutex
19758 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e _held(pMem->db->
19759 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
1975a 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 rt( (pMem->flags
1975b 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 &MEM_RowSet)==0
1975c 29 3b 0a 20 20 65 78 70 61 6e 64 42 6c 6f 62 28 );. expandBlob(
1975d 70 4d 65 6d 29 3b 0a 20 20 66 20 3d 20 70 4d 65 pMem);. f = pMe
1975e 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66 28 20 m->flags;. if(
1975f 28 66 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f (f&(MEM_Str|MEM_
19760 42 6c 6f 62 29 29 20 26 26 20 70 4d 65 6d 2d 3e Blob)) && pMem->
19761 7a 21 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 z!=pMem->zMalloc
19762 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 ){. if( sqli
19763 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 te3VdbeMemGrow(p
19764 4d 65 6d 2c 20 70 4d 65 6d 2d 3e 6e 20 2b 20 32 Mem, pMem->n + 2
19765 2c 20 31 29 20 29 7b 0a 20 20 20 20 20 20 72 65 , 1) ){. re
19766 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
19767 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 M;. }. pMe
19768 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 m->z[pMem->n] =
19769 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70 0;. pMem->z[p
1976a 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 30 3b 0a 20 Mem->n+1] = 0;.
1976b 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c pMem->flags |
1976c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 7d 0a = MEM_Term;. }.
1976d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
1976e 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 _OK;.}../*.** If
1976f 20 74 68 65 20 67 69 76 65 6e 20 4d 65 6d 2a 20 the given Mem*
19770 68 61 73 20 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 has a zero-fille
19771 64 20 74 61 69 6c 2c 20 74 75 72 6e 20 69 74 20 d tail, turn it
19772 69 6e 74 6f 20 61 6e 20 6f 72 64 69 6e 61 72 79 into an ordinary
19773 0a 2a 2a 20 62 6c 6f 62 20 73 74 6f 72 65 64 20 .** blob stored
19774 69 6e 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 in dynamically a
19775 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 2e 0a llocated space..
19776 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
19777 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a E_OMIT_INCRBLOB.
19778 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
19779 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
1977a 6d 45 78 70 61 6e 64 42 6c 6f 62 28 4d 65 6d 20 mExpandBlob(Mem
1977b 2a 70 4d 65 6d 29 7b 0a 20 20 69 66 28 20 70 4d *pMem){. if( pM
1977c 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f em->flags & MEM_
1977d 5a 65 72 6f 20 29 7b 0a 20 20 20 20 69 6e 74 20 Zero ){. int
1977e 6e 42 79 74 65 3b 0a 20 20 20 20 61 73 73 65 72 nByte;. asser
1977f 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d t( pMem->flags&M
19780 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 61 EM_Blob );. a
19781 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c ssert( (pMem->fl
19782 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d ags&MEM_RowSet)=
19783 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 =0 );. assert
19784 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c ( pMem->db==0 ||
19785 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
19786 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 eld(pMem->db->mu
19787 74 65 78 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 tex) );.. /*
19788 53 65 74 20 6e 42 79 74 65 20 74 6f 20 74 68 65 Set nByte to the
19789 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
1978a 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f required to sto
1978b 72 65 20 74 68 65 20 65 78 70 61 6e 64 65 64 20 re the expanded
1978c 62 6c 6f 62 2e 20 2a 2f 0a 20 20 20 20 6e 42 79 blob. */. nBy
1978d 74 65 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2b 20 70 te = pMem->n + p
1978e 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 Mem->u.nZero;.
1978f 20 20 69 66 28 20 6e 42 79 74 65 3c 3d 30 20 29 if( nByte<=0 )
19790 7b 0a 20 20 20 20 20 20 6e 42 79 74 65 20 3d 20 {. nByte =
19791 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 1;. }. if(
19792 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 sqlite3VdbeMemG
19793 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c row(pMem, nByte,
19794 20 31 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 1) ){. ret
19795 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
19796 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6d 65 6d ;. }.. mem
19797 73 65 74 28 26 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 set(&pMem->z[pMe
19798 6d 2d 3e 6e 5d 2c 20 30 2c 20 70 4d 65 6d 2d 3e m->n], 0, pMem->
19799 75 2e 6e 5a 65 72 6f 29 3b 0a 20 20 20 20 70 4d u.nZero);. pM
1979a 65 6d 2d 3e 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75 em->n += pMem->u
1979b 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 70 4d 65 6d .nZero;. pMem
1979c 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d ->flags &= ~(MEM
1979d 5f 5a 65 72 6f 7c 4d 45 4d 5f 54 65 72 6d 29 3b _Zero|MEM_Term);
1979e 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
1979f 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 LITE_OK;.}.#endi
197a0 66 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 f.../*.** Make s
197a1 75 72 65 20 74 68 65 20 67 69 76 65 6e 20 4d 65 ure the given Me
197a2 6d 20 69 73 20 5c 75 30 30 30 30 20 74 65 72 6d m is \u0000 term
197a3 69 6e 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 inated..*/.SQLIT
197a4 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
197a5 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 lite3VdbeMemNulT
197a6 65 72 6d 69 6e 61 74 65 28 4d 65 6d 20 2a 70 4d erminate(Mem *pM
197a7 65 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 em){. assert( p
197a8 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 Mem->db==0 || sq
197a9 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
197aa 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 (pMem->db->mutex
197ab 29 20 29 3b 0a 20 20 69 66 28 20 28 70 4d 65 6d ) );. if( (pMem
197ac 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 54 65 ->flags & MEM_Te
197ad 72 6d 29 21 3d 30 20 7c 7c 20 28 70 4d 65 6d 2d rm)!=0 || (pMem-
197ae 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 >flags & MEM_Str
197af 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 )==0 ){. retu
197b0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 rn SQLITE_OK;
197b1 2f 2a 20 4e 6f 74 68 69 6e 67 20 74 6f 20 64 6f /* Nothing to do
197b2 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 */. }. if( sq
197b3 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 lite3VdbeMemGrow
197b4 28 70 4d 65 6d 2c 20 70 4d 65 6d 2d 3e 6e 2b 32 (pMem, pMem->n+2
197b5 2c 20 31 29 20 29 7b 0a 20 20 20 20 72 65 74 75 , 1) ){. retu
197b6 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
197b7 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 7a 5b 70 . }. pMem->z[p
197b8 4d 65 6d 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 20 70 Mem->n] = 0;. p
197b9 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31 Mem->z[pMem->n+1
197ba 5d 20 3d 20 30 3b 0a 20 20 70 4d 65 6d 2d 3e 66 ] = 0;. pMem->f
197bb 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d lags |= MEM_Term
197bc 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
197bd 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 E_OK;.}../*.** A
197be 64 64 20 4d 45 4d 5f 53 74 72 20 74 6f 20 74 68 dd MEM_Str to th
197bf 65 20 73 65 74 20 6f 66 20 72 65 70 72 65 73 65 e set of represe
197c0 6e 74 61 74 69 6f 6e 73 20 66 6f 72 20 74 68 65 ntations for the
197c1 20 67 69 76 65 6e 20 4d 65 6d 2e 20 20 4e 75 6d given Mem. Num
197c2 62 65 72 73 0a 2a 2a 20 61 72 65 20 63 6f 6e 76 bers.** are conv
197c3 65 72 74 65 64 20 75 73 69 6e 67 20 73 71 6c 69 erted using sqli
197c4 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 2e 20 te3_snprintf().
197c5 20 43 6f 6e 76 65 72 74 69 6e 67 20 61 20 42 4c Converting a BL
197c6 4f 42 20 74 6f 20 61 20 73 74 72 69 6e 67 0a 2a OB to a string.*
197c7 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a * is a no-op..**
197c8 0a 2a 2a 20 45 78 69 73 74 69 6e 67 20 72 65 70 .** Existing rep
197c9 72 65 73 65 6e 74 61 74 69 6f 6e 73 20 4d 45 4d resentations MEM
197ca 5f 49 6e 74 20 61 6e 64 20 4d 45 4d 5f 52 65 61 _Int and MEM_Rea
197cb 6c 20 61 72 65 20 2a 6e 6f 74 2a 20 69 6e 76 61 l are *not* inva
197cc 6c 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 lidated..**.** A
197cd 20 4d 45 4d 5f 4e 75 6c 6c 20 76 61 6c 75 65 20 MEM_Null value
197ce 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 70 61 will never be pa
197cf 73 73 65 64 20 74 6f 20 74 68 69 73 20 66 75 6e ssed to this fun
197d0 63 74 69 6f 6e 2e 20 54 68 69 73 20 66 75 6e 63 ction. This func
197d1 74 69 6f 6e 20 69 73 0a 2a 2a 20 75 73 65 64 20 tion is.** used
197d2 66 6f 72 20 63 6f 6e 76 65 72 74 69 6e 67 20 76 for converting v
197d3 61 6c 75 65 73 20 74 6f 20 74 65 78 74 20 66 6f alues to text fo
197d4 72 20 72 65 74 75 72 6e 69 6e 67 20 74 6f 20 74 r returning to t
197d5 68 65 20 75 73 65 72 20 28 69 2e 65 2e 20 76 69 he user (i.e. vi
197d6 61 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c a.** sqlite3_val
197d7 75 65 5f 74 65 78 74 28 29 29 2c 20 6f 72 20 66 ue_text()), or f
197d8 6f 72 20 65 6e 73 75 72 69 6e 67 20 74 68 61 74 or ensuring that
197d9 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20 75 73 values to be us
197da 65 64 20 61 73 20 62 74 72 65 65 0a 2a 2a 20 6b ed as btree.** k
197db 65 79 73 20 61 72 65 20 73 74 72 69 6e 67 73 2e eys are strings.
197dc 20 49 6e 20 74 68 65 20 66 6f 72 6d 65 72 20 63 In the former c
197dd 61 73 65 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 ase a NULL point
197de 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 er is returned t
197df 68 65 0a 2a 2a 20 75 73 65 72 20 61 6e 64 20 74 he.** user and t
197e0 68 65 20 6c 61 74 65 72 20 69 73 20 61 6e 20 69 he later is an i
197e1 6e 74 65 72 6e 61 6c 20 70 72 6f 67 72 61 6d 6d nternal programm
197e2 69 6e 67 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 ing error..*/.SQ
197e3 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
197e4 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
197e5 74 72 69 6e 67 69 66 79 28 4d 65 6d 20 2a 70 4d tringify(Mem *pM
197e6 65 6d 2c 20 69 6e 74 20 65 6e 63 29 7b 0a 20 20 em, int enc){.
197e7 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
197e8 4f 4b 3b 0a 20 20 69 6e 74 20 66 67 20 3d 20 70 OK;. int fg = p
197e9 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 63 6f Mem->flags;. co
197ea 6e 73 74 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 nst int nByte =
197eb 33 32 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 32;.. assert( p
197ec 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 Mem->db==0 || sq
197ed 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
197ee 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 (pMem->db->mutex
197ef 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 ) );. assert( !
197f0 28 66 67 26 4d 45 4d 5f 5a 65 72 6f 29 20 29 3b (fg&MEM_Zero) );
197f1 0a 20 20 61 73 73 65 72 74 28 20 21 28 66 67 26 . assert( !(fg&
197f2 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f (MEM_Str|MEM_Blo
197f3 62 29 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 b)) );. assert(
197f4 20 66 67 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d fg&(MEM_Int|MEM
197f5 5f 52 65 61 6c 29 20 29 3b 0a 20 20 61 73 73 65 _Real) );. asse
197f6 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 rt( (pMem->flags
197f7 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 &MEM_RowSet)==0
197f8 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 );. assert( EIG
197f9 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e HT_BYTE_ALIGNMEN
197fa 54 28 70 4d 65 6d 29 20 29 3b 0a 0a 0a 20 20 69 T(pMem) );... i
197fb 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 f( sqlite3VdbeMe
197fc 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 mGrow(pMem, nByt
197fd 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74 e, 0) ){. ret
197fe 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
197ff 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 6f 72 20 ;. }.. /* For
19800 61 20 52 65 61 6c 20 6f 72 20 49 6e 74 65 67 65 a Real or Intege
19801 72 2c 20 75 73 65 20 73 71 6c 69 74 65 33 5f 6d r, use sqlite3_m
19802 70 72 69 6e 74 66 28 29 20 74 6f 20 70 72 6f 64 printf() to prod
19803 75 63 65 20 74 68 65 20 55 54 46 2d 38 0a 20 20 uce the UTF-8.
19804 2a 2a 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 ** string repres
19805 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 entation of the
19806 76 61 6c 75 65 2e 20 54 68 65 6e 2c 20 69 66 20 value. Then, if
19807 74 68 65 20 72 65 71 75 69 72 65 64 20 65 6e 63 the required enc
19808 6f 64 69 6e 67 0a 20 20 2a 2a 20 69 73 20 55 54 oding. ** is UT
19809 46 2d 31 36 6c 65 20 6f 72 20 55 54 46 2d 31 36 F-16le or UTF-16
1980a 62 65 20 64 6f 20 61 20 74 72 61 6e 73 6c 61 74 be do a translat
1980b 69 6f 6e 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 ion.. ** . **
1980c 46 49 58 20 4d 45 3a 20 49 74 20 77 6f 75 6c 64 FIX ME: It would
1980d 20 62 65 20 62 65 74 74 65 72 20 69 66 20 73 71 be better if sq
1980e 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 lite3_snprintf()
1980f 20 63 6f 75 6c 64 20 64 6f 20 55 54 46 2d 31 36 could do UTF-16
19810 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 66 67 20 .. */. if( fg
19811 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 & MEM_Int ){.
19812 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
19813 66 28 6e 42 79 74 65 2c 20 70 4d 65 6d 2d 3e 7a f(nByte, pMem->z
19814 2c 20 22 25 6c 6c 64 22 2c 20 70 4d 65 6d 2d 3e , "%lld", pMem->
19815 75 2e 69 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 u.i);. }else{.
19816 20 20 20 61 73 73 65 72 74 28 20 66 67 20 26 20 assert( fg &
19817 4d 45 4d 5f 52 65 61 6c 20 29 3b 0a 20 20 20 20 MEM_Real );.
19818 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
19819 28 6e 42 79 74 65 2c 20 70 4d 65 6d 2d 3e 7a 2c (nByte, pMem->z,
1981a 20 22 25 21 2e 31 35 67 22 2c 20 70 4d 65 6d 2d "%!.15g", pMem-
1981b 3e 72 29 3b 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d >r);. }. pMem-
1981c 3e 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c >n = sqlite3Strl
1981d 65 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 en30(pMem->z);.
1981e 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c pMem->enc = SQL
1981f 49 54 45 5f 55 54 46 38 3b 0a 20 20 70 4d 65 6d ITE_UTF8;. pMem
19820 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 ->flags |= MEM_S
19821 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 73 tr|MEM_Term;. s
19822 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
19823 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 65 Encoding(pMem, e
19824 6e 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 nc);. return rc
19825 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 6d 6f 72 ;.}../*.** Memor
19826 79 20 63 65 6c 6c 20 70 4d 65 6d 20 63 6f 6e 74 y cell pMem cont
19827 61 69 6e 73 20 74 68 65 20 63 6f 6e 74 65 78 74 ains the context
19828 20 6f 66 20 61 6e 20 61 67 67 72 65 67 61 74 65 of an aggregate
19829 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 function..** Th
1982a 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6c 6c 73 is routine calls
1982b 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 20 6d 65 the finalize me
1982c 74 68 6f 64 20 66 6f 72 20 74 68 61 74 20 66 75 thod for that fu
1982d 6e 63 74 69 6f 6e 2e 20 20 54 68 65 0a 2a 2a 20 nction. The.**
1982e 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 61 67 result of the ag
1982f 67 72 65 67 61 74 65 20 69 73 20 73 74 6f 72 65 gregate is store
19830 64 20 62 61 63 6b 20 69 6e 74 6f 20 70 4d 65 6d d back into pMem
19831 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 ..**.** Return S
19832 51 4c 49 54 45 5f 45 52 52 4f 52 20 69 66 20 74 QLITE_ERROR if t
19833 68 65 20 66 69 6e 61 6c 69 7a 65 72 20 72 65 70 he finalizer rep
19834 6f 72 74 73 20 61 6e 20 65 72 72 6f 72 2e 20 20 orts an error.
19835 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 74 68 SQLITE_OK.** oth
19836 65 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 erwise..*/.SQLIT
19837 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
19838 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 69 6e 61 lite3VdbeMemFina
19839 6c 69 7a 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 lize(Mem *pMem,
1983a 46 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 29 7b FuncDef *pFunc){
1983b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
1983c 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 41 4c 57 TE_OK;. if( ALW
1983d 41 59 53 28 70 46 75 6e 63 20 26 26 20 70 46 75 AYS(pFunc && pFu
1983e 6e 63 2d 3e 78 46 69 6e 61 6c 69 7a 65 29 20 29 nc->xFinalize) )
1983f 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f {. sqlite3_co
19840 6e 74 65 78 74 20 63 74 78 3b 0a 20 20 20 20 61 ntext ctx;. a
19841 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c ssert( (pMem->fl
19842 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 21 ags & MEM_Null)!
19843 3d 30 20 7c 7c 20 70 46 75 6e 63 3d 3d 70 4d 65 =0 || pFunc==pMe
19844 6d 2d 3e 75 2e 70 44 65 66 20 29 3b 0a 20 20 20 m->u.pDef );.
19845 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 assert( pMem->d
19846 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f b==0 || sqlite3_
19847 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d mutex_held(pMem-
19848 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 >db->mutex) );.
19849 20 20 20 6d 65 6d 73 65 74 28 26 63 74 78 2c 20 memset(&ctx,
1984a 30 2c 20 73 69 7a 65 6f 66 28 63 74 78 29 29 3b 0, sizeof(ctx));
1984b 0a 20 20 20 20 63 74 78 2e 73 2e 66 6c 61 67 73 . ctx.s.flags
1984c 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 = MEM_Null;.
1984d 20 63 74 78 2e 73 2e 64 62 20 3d 20 70 4d 65 6d ctx.s.db = pMem
1984e 2d 3e 64 62 3b 0a 20 20 20 20 63 74 78 2e 70 4d ->db;. ctx.pM
1984f 65 6d 20 3d 20 70 4d 65 6d 3b 0a 20 20 20 20 63 em = pMem;. c
19850 74 78 2e 70 46 75 6e 63 20 3d 20 70 46 75 6e 63 tx.pFunc = pFunc
19851 3b 0a 20 20 20 20 70 46 75 6e 63 2d 3e 78 46 69 ;. pFunc->xFi
19852 6e 61 6c 69 7a 65 28 26 63 74 78 29 3b 0a 20 20 nalize(&ctx);.
19853 20 20 61 73 73 65 72 74 28 20 30 3d 3d 28 70 4d assert( 0==(pM
19854 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79 em->flags&MEM_Dy
19855 6e 29 20 26 26 20 21 70 4d 65 6d 2d 3e 78 44 65 n) && !pMem->xDe
19856 6c 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 l );. sqlite3
19857 44 62 46 72 65 65 28 70 4d 65 6d 2d 3e 64 62 2c DbFree(pMem->db,
19858 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b pMem->zMalloc);
19859 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4d 65 6d . memcpy(pMem
1985a 2c 20 26 63 74 78 2e 73 2c 20 73 69 7a 65 6f 66 , &ctx.s, sizeof
1985b 28 63 74 78 2e 73 29 29 3b 0a 20 20 20 20 72 63 (ctx.s));. rc
1985c 20 3d 20 63 74 78 2e 69 73 45 72 72 6f 72 3b 0a = ctx.isError;.
1985d 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
1985e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 .}../*.** If the
1985f 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 63 6f 6e memory cell con
19860 74 61 69 6e 73 20 61 20 73 74 72 69 6e 67 20 76 tains a string v
19861 61 6c 75 65 20 74 68 61 74 20 6d 75 73 74 20 62 alue that must b
19862 65 20 66 72 65 65 64 20 62 79 0a 2a 2a 20 69 6e e freed by.** in
19863 76 6f 6b 69 6e 67 20 61 6e 20 65 78 74 65 72 6e voking an extern
19864 61 6c 20 63 61 6c 6c 62 61 63 6b 2c 20 66 72 65 al callback, fre
19865 65 20 69 74 20 6e 6f 77 2e 20 43 61 6c 6c 69 6e e it now. Callin
19866 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a g this function.
19867 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 66 72 65 65 ** does not free
19868 20 61 6e 79 20 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 any Mem.zMalloc
19869 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 53 51 4c 49 buffer..*/.SQLI
1986a 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1986b 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
1986c 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 4d 65 leaseExternal(Me
1986d 6d 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 m *p){. assert(
1986e 20 70 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c p->db==0 || sql
1986f 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
19870 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b p->db->mutex) );
19871 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 2d 3e . testcase( p->
19872 66 6c 61 67 73 20 26 20 4d 45 4d 5f 41 67 67 20 flags & MEM_Agg
19873 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 );. testcase( p
19874 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 ->flags & MEM_Dy
19875 6e 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 n );. testcase(
19876 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f p->flags & MEM_
19877 52 6f 77 53 65 74 20 29 3b 0a 20 20 74 65 73 74 RowSet );. test
19878 63 61 73 65 28 20 70 2d 3e 66 6c 61 67 73 20 26 case( p->flags &
19879 20 4d 45 4d 5f 46 72 61 6d 65 20 29 3b 0a 20 20 MEM_Frame );.
1987a 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 28 4d 45 if( p->flags&(ME
1987b 4d 5f 41 67 67 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 M_Agg|MEM_Dyn|ME
1987c 4d 5f 52 6f 77 53 65 74 7c 4d 45 4d 5f 46 72 61 M_RowSet|MEM_Fra
1987d 6d 65 29 20 29 7b 0a 20 20 20 20 69 66 28 20 70 me) ){. if( p
1987e 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 41 67 67 20 ->flags&MEM_Agg
1987f 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
19880 56 64 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 VdbeMemFinalize(
19881 70 2c 20 70 2d 3e 75 2e 70 44 65 66 29 3b 0a 20 p, p->u.pDef);.
19882 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d assert( (p-
19883 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 41 67 67 >flags & MEM_Agg
19884 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 )==0 );. sq
19885 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
19886 61 73 65 28 70 29 3b 0a 20 20 20 20 7d 65 6c 73 ase(p);. }els
19887 65 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 4d e if( p->flags&M
19888 45 4d 5f 44 79 6e 20 26 26 20 70 2d 3e 78 44 65 EM_Dyn && p->xDe
19889 6c 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 l ){. asser
1988a 74 28 20 28 70 2d 3e 66 6c 61 67 73 26 4d 45 4d t( (p->flags&MEM
1988b 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 _RowSet)==0 );.
1988c 20 20 20 20 20 70 2d 3e 78 44 65 6c 28 28 76 6f p->xDel((vo
1988d 69 64 20 2a 29 70 2d 3e 7a 29 3b 0a 20 20 20 20 id *)p->z);.
1988e 20 20 70 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 p->xDel = 0;.
1988f 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e }else if( p->
19890 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 flags&MEM_RowSet
19891 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
19892 33 52 6f 77 53 65 74 43 6c 65 61 72 28 70 2d 3e 3RowSetClear(p->
19893 75 2e 70 52 6f 77 53 65 74 29 3b 0a 20 20 20 20 u.pRowSet);.
19894 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 }else if( p->fla
19895 67 73 26 4d 45 4d 5f 46 72 61 6d 65 20 29 7b 0a gs&MEM_Frame ){.
19896 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
19897 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 29 3b 0a eMemSetNull(p);.
19898 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
19899 2a 2a 20 52 65 6c 65 61 73 65 20 61 6e 79 20 6d ** Release any m
1989a 65 6d 6f 72 79 20 68 65 6c 64 20 62 79 20 74 68 emory held by th
1989b 65 20 4d 65 6d 2e 20 54 68 69 73 20 6d 61 79 20 e Mem. This may
1989c 6c 65 61 76 65 20 74 68 65 20 4d 65 6d 20 69 6e leave the Mem in
1989d 20 61 6e 0a 2a 2a 20 69 6e 63 6f 6e 73 69 73 74 an.** inconsist
1989e 65 6e 74 20 73 74 61 74 65 2c 20 66 6f 72 20 65 ent state, for e
1989f 78 61 6d 70 6c 65 20 77 69 74 68 20 28 4d 65 6d xample with (Mem
198a0 2e 7a 3d 3d 30 29 20 61 6e 64 0a 2a 2a 20 28 4d .z==0) and.** (M
198a1 65 6d 2e 74 79 70 65 3d 3d 53 51 4c 49 54 45 5f em.type==SQLITE_
198a2 54 45 58 54 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 TEXT)..*/.SQLITE
198a3 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
198a4 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
198a5 61 73 65 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 73 ase(Mem *p){. s
198a6 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
198a7 65 61 73 65 45 78 74 65 72 6e 61 6c 28 70 29 3b easeExternal(p);
198a8 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
198a9 28 70 2d 3e 64 62 2c 20 70 2d 3e 7a 4d 61 6c 6c (p->db, p->zMall
198aa 6f 63 29 3b 0a 20 20 70 2d 3e 7a 20 3d 20 30 3b oc);. p->z = 0;
198ab 0a 20 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 . p->zMalloc =
198ac 30 3b 0a 20 20 70 2d 3e 78 44 65 6c 20 3d 20 30 0;. p->xDel = 0
198ad 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 ;.}../*.** Conve
198ae 72 74 20 61 20 36 34 2d 62 69 74 20 49 45 45 45 rt a 64-bit IEEE
198af 20 64 6f 75 62 6c 65 20 69 6e 74 6f 20 61 20 36 double into a 6
198b0 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 4-bit signed int
198b1 65 67 65 72 2e 0a 2a 2a 20 49 66 20 74 68 65 20 eger..** If the
198b2 64 6f 75 62 6c 65 20 69 73 20 74 6f 6f 20 6c 61 double is too la
198b3 72 67 65 2c 20 72 65 74 75 72 6e 20 30 78 38 30 rge, return 0x80
198b4 30 30 30 30 30 30 30 30 30 30 30 30 30 30 2e 0a 00000000000000..
198b5 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 73 79 73 74 65 **.** Most syste
198b6 6d 73 20 61 70 70 65 61 72 20 74 6f 20 64 6f 20 ms appear to do
198b7 74 68 69 73 20 73 69 6d 70 6c 79 20 62 79 20 61 this simply by a
198b8 73 73 69 67 6e 69 6e 67 0a 2a 2a 20 76 61 72 69 ssigning.** vari
198b9 61 62 6c 65 73 20 61 6e 64 20 77 69 74 68 6f 75 ables and withou
198ba 74 20 74 68 65 20 65 78 74 72 61 20 72 61 6e 67 t the extra rang
198bb 65 20 74 65 73 74 73 2e 20 20 42 75 74 0a 2a 2a e tests. But.**
198bc 20 74 68 65 72 65 20 61 72 65 20 72 65 70 6f 72 there are repor
198bd 74 73 20 74 68 61 74 20 77 69 6e 64 6f 77 73 20 ts that windows
198be 74 68 72 6f 77 73 20 61 6e 20 65 78 70 65 63 74 throws an expect
198bf 69 6f 6e 0a 2a 2a 20 69 66 20 74 68 65 20 66 6c ion.** if the fl
198c0 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c oating point val
198c1 75 65 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e ue is out of ran
198c2 67 65 2e 20 28 53 65 65 20 74 69 63 6b 65 74 20 ge. (See ticket
198c3 23 32 38 38 30 2e 29 0a 2a 2a 20 42 65 63 61 75 #2880.).** Becau
198c4 73 65 20 77 65 20 64 6f 20 6e 6f 74 20 63 6f 6d se we do not com
198c5 70 6c 65 74 65 6c 79 20 75 6e 64 65 72 73 74 61 pletely understa
198c6 6e 64 20 74 68 65 20 70 72 6f 62 6c 65 6d 2c 20 nd the problem,
198c7 77 65 20 77 69 6c 6c 0a 2a 2a 20 74 61 6b 65 20 we will.** take
198c8 74 68 65 20 63 6f 6e 73 65 72 76 61 74 69 76 65 the conservative
198c9 20 61 70 70 72 6f 61 63 68 20 61 6e 64 20 61 6c approach and al
198ca 77 61 79 73 20 64 6f 20 72 61 6e 67 65 20 74 65 ways do range te
198cb 73 74 73 0a 2a 2a 20 62 65 66 6f 72 65 20 61 74 sts.** before at
198cc 74 65 6d 70 74 69 6e 67 20 74 68 65 20 63 6f 6e tempting the con
198cd 76 65 72 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 version..*/.stat
198ce 69 63 20 69 36 34 20 64 6f 75 62 6c 65 54 6f 49 ic i64 doubleToI
198cf 6e 74 36 34 28 64 6f 75 62 6c 65 20 72 29 7b 0a nt64(double r){.
198d0 20 20 2f 2a 0a 20 20 2a 2a 20 4d 61 6e 79 20 63 /*. ** Many c
198d1 6f 6d 70 69 6c 65 72 73 20 77 65 20 65 6e 63 6f ompilers we enco
198d2 75 6e 74 65 72 20 64 6f 20 6e 6f 74 20 64 65 66 unter do not def
198d3 69 6e 65 20 63 6f 6e 73 74 61 6e 74 73 20 66 6f ine constants fo
198d4 72 20 74 68 65 0a 20 20 2a 2a 20 6d 69 6e 69 6d r the. ** minim
198d5 75 6d 20 61 6e 64 20 6d 61 78 69 6d 75 6d 20 36 um and maximum 6
198d6 34 2d 62 69 74 20 69 6e 74 65 67 65 72 73 2c 20 4-bit integers,
198d7 6f 72 20 74 68 65 79 20 64 65 66 69 6e 65 20 74 or they define t
198d8 68 65 6d 0a 20 20 2a 2a 20 69 6e 63 6f 6e 73 69 hem. ** inconsi
198d9 73 74 65 6e 74 6c 79 2e 20 20 41 6e 64 20 6d 61 stently. And ma
198da 6e 79 20 64 6f 20 6e 6f 74 20 75 6e 64 65 72 73 ny do not unders
198db 74 61 6e 64 20 74 68 65 20 22 4c 4c 22 20 6e 6f tand the "LL" no
198dc 74 61 74 69 6f 6e 2e 0a 20 20 2a 2a 20 53 6f 20 tation.. ** So
198dd 77 65 20 64 65 66 69 6e 65 20 6f 75 72 20 6f 77 we define our ow
198de 6e 20 73 74 61 74 69 63 20 63 6f 6e 73 74 61 6e n static constan
198df 74 73 20 68 65 72 65 20 75 73 69 6e 67 20 6e 6f ts here using no
198e0 74 68 69 6e 67 0a 20 20 2a 2a 20 6c 61 72 67 65 thing. ** large
198e1 72 20 74 68 61 6e 20 61 20 33 32 2d 62 69 74 20 r than a 32-bit
198e2 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 integer constant
198e3 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 .. */. static
198e4 63 6f 6e 73 74 20 69 36 34 20 6d 61 78 49 6e 74 const i64 maxInt
198e5 20 3d 20 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 = LARGEST_INT64
198e6 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 ;. static const
198e7 20 69 36 34 20 6d 69 6e 49 6e 74 20 3d 20 53 4d i64 minInt = SM
198e8 41 4c 4c 45 53 54 5f 49 4e 54 36 34 3b 0a 0a 20 ALLEST_INT64;..
198e9 20 69 66 28 20 72 3c 28 64 6f 75 62 6c 65 29 6d if( r<(double)m
198ea 69 6e 49 6e 74 20 29 7b 0a 20 20 20 20 72 65 74 inInt ){. ret
198eb 75 72 6e 20 6d 69 6e 49 6e 74 3b 0a 20 20 7d 65 urn minInt;. }e
198ec 6c 73 65 20 69 66 28 20 72 3e 28 64 6f 75 62 6c lse if( r>(doubl
198ed 65 29 6d 61 78 49 6e 74 20 29 7b 0a 20 20 20 20 e)maxInt ){.
198ee 2f 2a 20 6d 69 6e 49 6e 74 20 69 73 20 63 6f 72 /* minInt is cor
198ef 72 65 63 74 20 68 65 72 65 20 2d 20 6e 6f 74 20 rect here - not
198f0 6d 61 78 49 6e 74 2e 20 20 49 74 20 74 75 72 6e maxInt. It turn
198f1 73 20 6f 75 74 20 74 68 61 74 20 61 73 73 69 67 s out that assig
198f2 6e 69 6e 67 0a 20 20 20 20 2a 2a 20 61 20 76 65 ning. ** a ve
198f3 72 79 20 6c 61 72 67 65 20 70 6f 73 69 74 69 76 ry large positiv
198f4 65 20 6e 75 6d 62 65 72 20 74 6f 20 61 6e 20 69 e number to an i
198f5 6e 74 65 67 65 72 20 72 65 73 75 6c 74 73 20 69 nteger results i
198f6 6e 20 61 20 76 65 72 79 20 6c 61 72 67 65 0a 20 n a very large.
198f7 20 20 20 2a 2a 20 6e 65 67 61 74 69 76 65 20 69 ** negative i
198f8 6e 74 65 67 65 72 2e 20 20 54 68 69 73 20 6d 61 nteger. This ma
198f9 6b 65 73 20 6e 6f 20 73 65 6e 73 65 2c 20 62 75 kes no sense, bu
198fa 74 20 69 74 20 69 73 20 77 68 61 74 20 78 38 36 t it is what x86
198fb 20 68 61 72 64 77 61 72 65 0a 20 20 20 20 2a 2a hardware. **
198fc 20 64 6f 65 73 20 73 6f 20 66 6f 72 20 63 6f 6d does so for com
198fd 70 61 74 69 62 69 6c 69 74 79 20 77 65 20 77 69 patibility we wi
198fe 6c 6c 20 64 6f 20 74 68 65 20 73 61 6d 65 20 69 ll do the same i
198ff 6e 20 73 6f 66 74 77 61 72 65 2e 20 2a 2f 0a 20 n software. */.
19900 20 20 20 72 65 74 75 72 6e 20 6d 69 6e 49 6e 74 return minInt
19901 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 ;. }else{. r
19902 65 74 75 72 6e 20 28 69 36 34 29 72 3b 0a 20 20 eturn (i64)r;.
19903 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 }.}../*.** Retur
19904 6e 20 73 6f 6d 65 20 6b 69 6e 64 20 6f 66 20 69 n some kind of i
19905 6e 74 65 67 65 72 20 76 61 6c 75 65 20 77 68 69 nteger value whi
19906 63 68 20 69 73 20 74 68 65 20 62 65 73 74 20 77 ch is the best w
19907 65 20 63 61 6e 20 64 6f 0a 2a 2a 20 61 74 20 72 e can do.** at r
19908 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 epresenting the
19909 76 61 6c 75 65 20 74 68 61 74 20 2a 70 4d 65 6d value that *pMem
1990a 20 64 65 73 63 72 69 62 65 73 20 61 73 20 61 6e describes as an
1990b 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 49 66 20 integer..** If
1990c 70 4d 65 6d 20 69 73 20 61 6e 20 69 6e 74 65 67 pMem is an integ
1990d 65 72 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c er, then the val
1990e 75 65 20 69 73 20 65 78 61 63 74 2e 20 20 49 66 ue is exact. If
1990f 20 70 4d 65 6d 20 69 73 0a 2a 2a 20 61 20 66 6c pMem is.** a fl
19910 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20 74 68 65 oating-point the
19911 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 n the value retu
19912 72 6e 65 64 20 69 73 20 74 68 65 20 69 6e 74 65 rned is the inte
19913 67 65 72 20 70 61 72 74 2e 0a 2a 2a 20 49 66 20 ger part..** If
19914 70 4d 65 6d 20 69 73 20 61 20 73 74 72 69 6e 67 pMem is a string
19915 20 6f 72 20 62 6c 6f 62 2c 20 74 68 65 6e 20 77 or blob, then w
19916 65 20 6d 61 6b 65 20 61 6e 20 61 74 74 65 6d 70 e make an attemp
19917 74 20 74 6f 20 63 6f 6e 76 65 72 74 0a 2a 2a 20 t to convert.**
19918 69 74 20 69 6e 74 6f 20 61 20 69 6e 74 65 67 65 it into a intege
19919 72 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 61 r and return tha
1991a 74 2e 20 20 49 66 20 70 4d 65 6d 20 72 65 70 72 t. If pMem repr
1991b 65 73 65 6e 74 73 20 61 6e 0a 2a 2a 20 61 6e 20 esents an.** an
1991c 53 51 4c 2d 4e 55 4c 4c 20 76 61 6c 75 65 2c 20 SQL-NULL value,
1991d 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 return 0..**.**
1991e 49 66 20 70 4d 65 6d 20 72 65 70 72 65 73 65 6e If pMem represen
1991f 74 73 20 61 20 73 74 72 69 6e 67 20 76 61 6c 75 ts a string valu
19920 65 2c 20 69 74 73 20 65 6e 63 6f 64 69 6e 67 20 e, its encoding
19921 6d 69 67 68 74 20 62 65 20 63 68 61 6e 67 65 64 might be changed
19922 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
19923 41 54 45 20 69 36 34 20 73 71 6c 69 74 65 33 56 ATE i64 sqlite3V
19924 64 62 65 49 6e 74 56 61 6c 75 65 28 4d 65 6d 20 dbeIntValue(Mem
19925 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 66 6c *pMem){. int fl
19926 61 67 73 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ags;. assert( p
19927 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 Mem->db==0 || sq
19928 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
19929 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 (pMem->db->mutex
1992a 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 ) );. assert( E
1992b 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d IGHT_BYTE_ALIGNM
1992c 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 20 20 66 ENT(pMem) );. f
1992d 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 lags = pMem->fla
1992e 67 73 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20 gs;. if( flags
1992f 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 & MEM_Int ){.
19930 20 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 75 2e return pMem->u.
19931 69 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66 i;. }else if( f
19932 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 lags & MEM_Real
19933 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 64 6f ){. return do
19934 75 62 6c 65 54 6f 49 6e 74 36 34 28 70 4d 65 6d ubleToInt64(pMem
19935 2d 3e 72 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 ->r);. }else if
19936 28 20 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 ( flags & (MEM_S
19937 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a tr|MEM_Blob) ){.
19938 20 20 20 20 69 36 34 20 76 61 6c 75 65 3b 0a 20 i64 value;.
19939 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c pMem->flags |
1993a 3d 20 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 20 69 = MEM_Str;. i
1993b 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 f( sqlite3VdbeCh
1993c 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65 angeEncoding(pMe
1993d 6d 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 0a m, SQLITE_UTF8).
1993e 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 || sqlite
1993f 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 3VdbeMemNulTermi
19940 6e 61 74 65 28 70 4d 65 6d 29 20 29 7b 0a 20 20 nate(pMem) ){.
19941 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
19942 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
19943 70 4d 65 6d 2d 3e 7a 20 29 3b 0a 20 20 20 20 73 pMem->z );. s
19944 71 6c 69 74 65 33 41 74 6f 69 36 34 28 70 4d 65 qlite3Atoi64(pMe
19945 6d 2d 3e 7a 2c 20 26 76 61 6c 75 65 29 3b 0a 20 m->z, &value);.
19946 20 20 20 72 65 74 75 72 6e 20 76 61 6c 75 65 3b return value;
19947 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 . }else{. re
19948 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f turn 0;. }.}../
19949 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
1994a 62 65 73 74 20 72 65 70 72 65 73 65 6e 74 61 74 best representat
1994b 69 6f 6e 20 6f 66 20 70 4d 65 6d 20 74 68 61 74 ion of pMem that
1994c 20 77 65 20 63 61 6e 20 67 65 74 20 69 6e 74 6f we can get into
1994d 20 61 0a 2a 2a 20 64 6f 75 62 6c 65 2e 20 20 49 a.** double. I
1994e 66 20 70 4d 65 6d 20 69 73 20 61 6c 72 65 61 64 f pMem is alread
1994f 79 20 61 20 64 6f 75 62 6c 65 20 6f 72 20 61 6e y a double or an
19950 20 69 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e integer, return
19951 20 69 74 73 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 its.** value.
19952 49 66 20 69 74 20 69 73 20 61 20 73 74 72 69 6e If it is a strin
19953 67 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 g or blob, try t
19954 6f 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 o convert it to
19955 61 20 64 6f 75 62 6c 65 2e 0a 2a 2a 20 49 66 20 a double..** If
19956 69 74 20 69 73 20 61 20 4e 55 4c 4c 2c 20 72 65 it is a NULL, re
19957 74 75 72 6e 20 30 2e 30 2e 0a 2a 2f 0a 53 51 4c turn 0.0..*/.SQL
19958 49 54 45 5f 50 52 49 56 41 54 45 20 64 6f 75 62 ITE_PRIVATE doub
19959 6c 65 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 le sqlite3VdbeRe
1995a 61 6c 56 61 6c 75 65 28 4d 65 6d 20 2a 70 4d 65 alValue(Mem *pMe
1995b 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d m){. assert( pM
1995c 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c em->db==0 || sql
1995d 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1995e 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 pMem->db->mutex)
1995f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 );. assert( EI
19960 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 GHT_BYTE_ALIGNME
19961 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 20 20 69 66 NT(pMem) );. if
19962 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 ( pMem->flags &
19963 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 MEM_Real ){.
19964 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 72 3b 0a return pMem->r;.
19965 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4d 65 6d }else if( pMem
19966 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e ->flags & MEM_In
19967 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 t ){. return
19968 28 64 6f 75 62 6c 65 29 70 4d 65 6d 2d 3e 75 2e (double)pMem->u.
19969 69 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 i;. }else if( p
1996a 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 Mem->flags & (ME
1996b 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 M_Str|MEM_Blob)
1996c 29 7b 0a 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c ){. /* (doubl
1996d 65 29 30 20 49 6e 20 63 61 73 65 20 6f 66 20 53 e)0 In case of S
1996e 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 QLITE_OMIT_FLOAT
1996f 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a ING_POINT... */.
19970 20 20 20 20 64 6f 75 62 6c 65 20 76 61 6c 20 3d double val =
19971 20 28 64 6f 75 62 6c 65 29 30 3b 0a 20 20 20 20 (double)0;.
19972 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d pMem->flags |= M
19973 45 4d 5f 53 74 72 3b 0a 20 20 20 20 69 66 28 20 EM_Str;. if(
19974 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
19975 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 eEncoding(pMem,
19976 53 51 4c 49 54 45 5f 55 54 46 38 29 0a 20 20 20 SQLITE_UTF8).
19977 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 56 64 || sqlite3Vd
19978 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 beMemNulTerminat
19979 65 28 70 4d 65 6d 29 20 29 7b 0a 20 20 20 20 20 e(pMem) ){.
1997a 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30 20 49 6e /* (double)0 In
1997b 20 63 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f case of SQLITE_
1997c 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f OMIT_FLOATING_PO
1997d 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 INT... */.
1997e 72 65 74 75 72 6e 20 28 64 6f 75 62 6c 65 29 30 return (double)0
1997f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 ;. }. asse
19980 72 74 28 20 70 4d 65 6d 2d 3e 7a 20 29 3b 0a 20 rt( pMem->z );.
19981 20 20 20 73 71 6c 69 74 65 33 41 74 6f 46 28 70 sqlite3AtoF(p
19982 4d 65 6d 2d 3e 7a 2c 20 26 76 61 6c 29 3b 0a 20 Mem->z, &val);.
19983 20 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 return val;.
19984 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 28 }else{. /* (
19985 64 6f 75 62 6c 65 29 30 20 49 6e 20 63 61 73 65 double)0 In case
19986 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f of SQLITE_OMIT_
19987 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e FLOATING_POINT..
19988 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 . */. return
19989 28 64 6f 75 62 6c 65 29 30 3b 0a 20 20 7d 0a 7d (double)0;. }.}
1998a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 4d 45 4d 20 ../*.** The MEM
1998b 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6c 72 structure is alr
1998c 65 61 64 79 20 61 20 4d 45 4d 5f 52 65 61 6c 2e eady a MEM_Real.
1998d 20 20 54 72 79 20 74 6f 20 61 6c 73 6f 20 6d 61 Try to also ma
1998e 6b 65 20 69 74 20 61 0a 2a 2a 20 4d 45 4d 5f 49 ke it a.** MEM_I
1998f 6e 74 20 69 66 20 77 65 20 63 61 6e 2e 0a 2a 2f nt if we can..*/
19990 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
19991 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
19992 49 6e 74 65 67 65 72 41 66 66 69 6e 69 74 79 28 IntegerAffinity(
19993 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 Mem *pMem){. as
19994 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 sert( pMem->flag
19995 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 3b 0a s & MEM_Real );.
19996 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d assert( (pMem-
19997 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 >flags & MEM_Row
19998 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 Set)==0 );. ass
19999 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 ert( pMem->db==0
1999a 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 || sqlite3_mute
1999b 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d x_held(pMem->db-
1999c 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 >mutex) );. ass
1999d 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f ert( EIGHT_BYTE_
1999e 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 ALIGNMENT(pMem)
1999f 29 3b 0a 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 );.. pMem->u.i
199a0 3d 20 64 6f 75 62 6c 65 54 6f 49 6e 74 36 34 28 = doubleToInt64(
199a1 70 4d 65 6d 2d 3e 72 29 3b 0a 0a 20 20 2f 2a 20 pMem->r);.. /*
199a2 4f 6e 6c 79 20 6d 61 72 6b 20 74 68 65 20 76 61 Only mark the va
199a3 6c 75 65 20 61 73 20 61 6e 20 69 6e 74 65 67 65 lue as an intege
199a4 72 20 69 66 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 r if. **. **
199a5 20 20 28 31 29 20 74 68 65 20 72 6f 75 6e 64 2d (1) the round-
199a6 74 72 69 70 20 63 6f 6e 76 65 72 73 69 6f 6e 20 trip conversion
199a7 72 65 61 6c 2d 3e 69 6e 74 2d 3e 72 65 61 6c 20 real->int->real
199a8 69 73 20 61 20 6e 6f 2d 6f 70 2c 20 61 6e 64 0a is a no-op, and.
199a9 20 20 2a 2a 20 20 20 20 28 32 29 20 54 68 65 20 ** (2) The
199aa 69 6e 74 65 67 65 72 20 69 73 20 6e 65 69 74 68 integer is neith
199ab 65 72 20 74 68 65 20 6c 61 72 67 65 73 74 20 6e er the largest n
199ac 6f 72 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 0a or the smallest.
199ad 20 20 2a 2a 20 20 20 20 20 20 20 20 70 6f 73 73 ** poss
199ae 69 62 6c 65 20 69 6e 74 65 67 65 72 20 28 74 69 ible integer (ti
199af 63 6b 65 74 20 23 33 39 32 32 29 0a 20 20 2a 2a cket #3922). **
199b0 0a 20 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 . ** The second
199b1 20 61 6e 64 20 74 68 69 72 64 20 74 65 72 6d 73 and third terms
199b2 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e in the followin
199b3 67 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 65 6e g conditional en
199b4 66 6f 72 63 65 73 0a 20 20 2a 2a 20 74 68 65 20 forces. ** the
199b5 73 65 63 6f 6e 64 20 63 6f 6e 64 69 74 69 6f 6e second condition
199b6 20 75 6e 64 65 72 20 74 68 65 20 61 73 73 75 6d under the assum
199b7 70 74 69 6f 6e 20 74 68 61 74 20 61 64 64 69 74 ption that addit
199b8 69 6f 6e 20 6f 76 65 72 66 6c 6f 77 20 63 61 75 ion overflow cau
199b9 73 65 73 0a 20 20 2a 2a 20 76 61 6c 75 65 73 20 ses. ** values
199ba 74 6f 20 77 72 61 70 20 61 72 6f 75 6e 64 2e 20 to wrap around.
199bb 20 4f 6e 20 78 38 36 20 68 61 72 64 77 61 72 65 On x86 hardware
199bc 2c 20 74 68 65 20 74 68 69 72 64 20 74 65 72 6d , the third term
199bd 20 69 73 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20 is always. **
199be 74 72 75 65 20 61 6e 64 20 63 6f 75 6c 64 20 62 true and could b
199bf 65 20 6f 6d 69 74 74 65 64 2e 20 20 42 75 74 20 e omitted. But
199c0 77 65 20 6c 65 61 76 65 20 69 74 20 69 6e 20 62 we leave it in b
199c1 65 63 61 75 73 65 20 6f 74 68 65 72 0a 20 20 2a ecause other. *
199c2 2a 20 61 72 63 68 69 74 65 63 74 75 72 65 73 20 * architectures
199c3 6d 69 67 68 74 20 62 65 68 61 76 65 20 64 69 66 might behave dif
199c4 66 65 72 65 6e 74 6c 79 2e 0a 20 20 2a 2f 0a 20 ferently.. */.
199c5 20 69 66 28 20 70 4d 65 6d 2d 3e 72 3d 3d 28 64 if( pMem->r==(d
199c6 6f 75 62 6c 65 29 70 4d 65 6d 2d 3e 75 2e 69 20 ouble)pMem->u.i
199c7 26 26 20 70 4d 65 6d 2d 3e 75 2e 69 3e 53 4d 41 && pMem->u.i>SMA
199c8 4c 4c 45 53 54 5f 49 4e 54 36 34 0a 20 20 20 20 LLEST_INT64.
199c9 20 20 26 26 20 41 4c 57 41 59 53 28 70 4d 65 6d && ALWAYS(pMem
199ca 2d 3e 75 2e 69 3c 4c 41 52 47 45 53 54 5f 49 4e ->u.i<LARGEST_IN
199cb 54 36 34 29 20 29 7b 0a 20 20 20 20 70 4d 65 6d T64) ){. pMem
199cc 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 49 ->flags |= MEM_I
199cd 6e 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a nt;. }.}../*.**
199ce 20 43 6f 6e 76 65 72 74 20 70 4d 65 6d 20 74 6f Convert pMem to
199cf 20 74 79 70 65 20 69 6e 74 65 67 65 72 2e 20 20 type integer.
199d0 49 6e 76 61 6c 69 64 61 74 65 20 61 6e 79 20 70 Invalidate any p
199d1 72 69 6f 72 20 72 65 70 72 65 73 65 6e 74 61 74 rior representat
199d2 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ions..*/.SQLITE_
199d3 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
199d4 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 te3VdbeMemIntege
199d5 72 69 66 79 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b rify(Mem *pMem){
199d6 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d . assert( pMem-
199d7 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 >db==0 || sqlite
199d8 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 3_mutex_held(pMe
199d9 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b m->db->mutex) );
199da 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d . assert( (pMem
199db 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f ->flags & MEM_Ro
199dc 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73 wSet)==0 );. as
199dd 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 sert( EIGHT_BYTE
199de 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 _ALIGNMENT(pMem)
199df 20 29 3b 0a 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 );.. pMem->u.i
199e0 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e = sqlite3VdbeIn
199e1 74 56 61 6c 75 65 28 70 4d 65 6d 29 3b 0a 20 20 tValue(pMem);.
199e2 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 MemSetTypeFlag(p
199e3 4d 65 6d 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 Mem, MEM_Int);.
199e4 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
199e5 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 K;.}../*.** Conv
199e6 65 72 74 20 70 4d 65 6d 20 73 6f 20 74 68 61 74 ert pMem so that
199e7 20 69 74 20 69 73 20 6f 66 20 74 79 70 65 20 4d it is of type M
199e8 45 4d 5f 52 65 61 6c 2e 0a 2a 2a 20 49 6e 76 61 EM_Real..** Inva
199e9 6c 69 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 lidate any prior
199ea 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 representations
199eb 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
199ec 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
199ed 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 4d 65 dbeMemRealify(Me
199ee 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 73 65 m *pMem){. asse
199ef 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 rt( pMem->db==0
199f0 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 || sqlite3_mutex
199f1 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e _held(pMem->db->
199f2 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
199f3 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 rt( EIGHT_BYTE_A
199f4 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 LIGNMENT(pMem) )
199f5 3b 0a 0a 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 73 ;.. pMem->r = s
199f6 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 qlite3VdbeRealVa
199f7 6c 75 65 28 70 4d 65 6d 29 3b 0a 20 20 4d 65 6d lue(pMem);. Mem
199f8 53 65 74 54 79 70 65 46 6c 61 67 28 70 4d 65 6d SetTypeFlag(pMem
199f9 2c 20 4d 45 4d 5f 52 65 61 6c 29 3b 0a 20 20 72 , MEM_Real);. r
199fa 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
199fb 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 .}../*.** Conver
199fc 74 20 70 4d 65 6d 20 73 6f 20 74 68 61 74 20 69 t pMem so that i
199fd 74 20 68 61 73 20 74 79 70 65 73 20 4d 45 4d 5f t has types MEM_
199fe 52 65 61 6c 20 6f 72 20 4d 45 4d 5f 49 6e 74 20 Real or MEM_Int
199ff 6f 72 20 62 6f 74 68 2e 0a 2a 2a 20 49 6e 76 61 or both..** Inva
19a00 6c 69 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 lidate any prior
19a01 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 representations
19a02 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
19a03 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
19a04 64 62 65 4d 65 6d 4e 75 6d 65 72 69 66 79 28 4d dbeMemNumerify(M
19a05 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 64 6f 75 em *pMem){. dou
19a06 62 6c 65 20 72 31 2c 20 72 32 3b 0a 20 20 69 36 ble r1, r2;. i6
19a07 34 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 28 4 i;. assert( (
19a08 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d pMem->flags & (M
19a09 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c EM_Int|MEM_Real|
19a0a 4d 45 4d 5f 4e 75 6c 6c 29 29 3d 3d 30 20 29 3b MEM_Null))==0 );
19a0b 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d . assert( (pMem
19a0c 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 42 ->flags & (MEM_B
19a0d 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 29 21 3d 30 lob|MEM_Str))!=0
19a0e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d );. assert( pM
19a0f 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c em->db==0 || sql
19a10 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
19a11 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 pMem->db->mutex)
19a12 20 29 3b 0a 20 20 72 31 20 3d 20 73 71 6c 69 74 );. r1 = sqlit
19a13 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 e3VdbeRealValue(
19a14 70 4d 65 6d 29 3b 0a 20 20 69 20 3d 20 64 6f 75 pMem);. i = dou
19a15 62 6c 65 54 6f 49 6e 74 36 34 28 72 31 29 3b 0a bleToInt64(r1);.
19a16 20 20 72 32 20 3d 20 28 64 6f 75 62 6c 65 29 69 r2 = (double)i
19a17 3b 0a 20 20 69 66 28 20 72 31 3d 3d 72 32 20 29 ;. if( r1==r2 )
19a18 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
19a19 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 eMemIntegerify(p
19a1a 4d 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 Mem);. }else{.
19a1b 20 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 72 31 3b pMem->r = r1;
19a1c 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 . MemSetTypeF
19a1d 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 52 65 lag(pMem, MEM_Re
19a1e 61 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 al);. }. retur
19a1f 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
19a20 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 /*.** Delete any
19a21 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 previous value
19a22 61 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c 75 and set the valu
19a23 65 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 e stored in *pMe
19a24 6d 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 m to NULL..*/.SQ
19a25 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
19a26 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d d sqlite3VdbeMem
19a27 53 65 74 4e 75 6c 6c 28 4d 65 6d 20 2a 70 4d 65 SetNull(Mem *pMe
19a28 6d 29 7b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e m){. if( pMem->
19a29 66 6c 61 67 73 20 26 20 4d 45 4d 5f 46 72 61 6d flags & MEM_Fram
19a2a 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 e ){. sqlite3
19a2b 56 64 62 65 46 72 61 6d 65 44 65 6c 65 74 65 28 VdbeFrameDelete(
19a2c 70 4d 65 6d 2d 3e 75 2e 70 46 72 61 6d 65 29 3b pMem->u.pFrame);
19a2d 0a 20 20 7d 0a 20 20 69 66 28 20 70 4d 65 6d 2d . }. if( pMem-
19a2e 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 >flags & MEM_Row
19a2f 53 65 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 Set ){. sqlit
19a30 65 33 52 6f 77 53 65 74 43 6c 65 61 72 28 70 4d e3RowSetClear(pM
19a31 65 6d 2d 3e 75 2e 70 52 6f 77 53 65 74 29 3b 0a em->u.pRowSet);.
19a32 20 20 7d 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 }. MemSetType
19a33 46 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 4e Flag(pMem, MEM_N
19a34 75 6c 6c 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 ull);. pMem->ty
19a35 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c pe = SQLITE_NULL
19a36 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 ;.}../*.** Delet
19a37 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20 76 e any previous v
19a38 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68 65 alue and set the
19a39 20 76 61 6c 75 65 20 74 6f 20 62 65 20 61 20 42 value to be a B
19a3a 4c 4f 42 20 6f 66 20 6c 65 6e 67 74 68 0a 2a 2a LOB of length.**
19a3b 20 6e 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6c n containing al
19a3c 6c 20 7a 65 72 6f 73 2e 0a 2a 2f 0a 53 51 4c 49 l zeros..*/.SQLI
19a3d 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
19a3e 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
19a3f 74 5a 65 72 6f 42 6c 6f 62 28 4d 65 6d 20 2a 70 tZeroBlob(Mem *p
19a40 4d 65 6d 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 73 Mem, int n){. s
19a41 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
19a42 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d ease(pMem);. pM
19a43 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f em->flags = MEM_
19a44 42 6c 6f 62 7c 4d 45 4d 5f 5a 65 72 6f 3b 0a 20 Blob|MEM_Zero;.
19a45 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 pMem->type = SQ
19a46 4c 49 54 45 5f 42 4c 4f 42 3b 0a 20 20 70 4d 65 LITE_BLOB;. pMe
19a47 6d 2d 3e 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20 m->n = 0;. if(
19a48 6e 3c 30 20 29 20 6e 20 3d 20 30 3b 0a 20 20 70 n<0 ) n = 0;. p
19a49 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 20 3d 20 6e Mem->u.nZero = n
19a4a 3b 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 ;. pMem->enc =
19a4b 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 0a 23 69 SQLITE_UTF8;..#i
19a4c 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
19a4d 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 73 71 6c 69 _INCRBLOB. sqli
19a4e 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 te3VdbeMemGrow(p
19a4f 4d 65 6d 2c 20 6e 2c 20 30 29 3b 0a 20 20 69 66 Mem, n, 0);. if
19a50 28 20 70 4d 65 6d 2d 3e 7a 20 29 7b 0a 20 20 20 ( pMem->z ){.
19a51 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6e 3b 0a 20 20 pMem->n = n;.
19a52 20 20 6d 65 6d 73 65 74 28 70 4d 65 6d 2d 3e 7a memset(pMem->z
19a53 2c 20 30 2c 20 6e 29 3b 0a 20 20 7d 0a 23 65 6e , 0, n);. }.#en
19a54 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c dif.}../*.** Del
19a55 65 74 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73 ete any previous
19a56 20 76 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74 value and set t
19a57 68 65 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 he value stored
19a58 69 6e 20 2a 70 4d 65 6d 20 74 6f 20 76 61 6c 2c in *pMem to val,
19a59 0a 2a 2a 20 6d 61 6e 69 66 65 73 74 20 74 79 70 .** manifest typ
19a5a 65 20 49 4e 54 45 47 45 52 2e 0a 2a 2f 0a 53 51 e INTEGER..*/.SQ
19a5b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
19a5c 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d d sqlite3VdbeMem
19a5d 53 65 74 49 6e 74 36 34 28 4d 65 6d 20 2a 70 4d SetInt64(Mem *pM
19a5e 65 6d 2c 20 69 36 34 20 76 61 6c 29 7b 0a 20 20 em, i64 val){.
19a5f 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
19a60 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 lease(pMem);. p
19a61 4d 65 6d 2d 3e 75 2e 69 20 3d 20 76 61 6c 3b 0a Mem->u.i = val;.
19a62 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 pMem->flags =
19a63 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70 4d 65 6d 2d MEM_Int;. pMem-
19a64 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 >type = SQLITE_I
19a65 4e 54 45 47 45 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a NTEGER;.}../*.**
19a66 20 44 65 6c 65 74 65 20 61 6e 79 20 70 72 65 76 Delete any prev
19a67 69 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 20 73 ious value and s
19a68 65 74 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f et the value sto
19a69 72 65 64 20 69 6e 20 2a 70 4d 65 6d 20 74 6f 20 red in *pMem to
19a6a 76 61 6c 2c 0a 2a 2a 20 6d 61 6e 69 66 65 73 74 val,.** manifest
19a6b 20 74 79 70 65 20 52 45 41 4c 2e 0a 2a 2f 0a 53 type REAL..*/.S
19a6c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
19a6d 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 id sqlite3VdbeMe
19a6e 6d 53 65 74 44 6f 75 62 6c 65 28 4d 65 6d 20 2a mSetDouble(Mem *
19a6f 70 4d 65 6d 2c 20 64 6f 75 62 6c 65 20 76 61 6c pMem, double val
19a70 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 ){. if( sqlite3
19a71 49 73 4e 61 4e 28 76 61 6c 29 20 29 7b 0a 20 20 IsNaN(val) ){.
19a72 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
19a73 53 65 74 4e 75 6c 6c 28 70 4d 65 6d 29 3b 0a 20 SetNull(pMem);.
19a74 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 }else{. sqli
19a75 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
19a76 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65 e(pMem);. pMe
19a77 6d 2d 3e 72 20 3d 20 76 61 6c 3b 0a 20 20 20 20 m->r = val;.
19a78 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 pMem->flags = ME
19a79 4d 5f 52 65 61 6c 3b 0a 20 20 20 20 70 4d 65 6d M_Real;. pMem
19a7a 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f ->type = SQLITE_
19a7b 46 4c 4f 41 54 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a FLOAT;. }.}../*
19a7c 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 70 .** Delete any p
19a7d 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 61 6e revious value an
19a7e 64 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 d set the value
19a7f 6f 66 20 70 4d 65 6d 20 74 6f 20 62 65 20 61 6e of pMem to be an
19a80 0a 2a 2a 20 65 6d 70 74 79 20 62 6f 6f 6c 65 61 .** empty boolea
19a81 6e 20 69 6e 64 65 78 2e 0a 2a 2f 0a 53 51 4c 49 n index..*/.SQLI
19a82 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
19a83 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
19a84 74 52 6f 77 53 65 74 28 4d 65 6d 20 2a 70 4d 65 tRowSet(Mem *pMe
19a85 6d 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 m){. sqlite3 *d
19a86 62 20 3d 20 70 4d 65 6d 2d 3e 64 62 3b 0a 20 20 b = pMem->db;.
19a87 61 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b assert( db!=0 );
19a88 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d . assert( (pMem
19a89 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f ->flags & MEM_Ro
19a8a 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 73 71 wSet)==0 );. sq
19a8b 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
19a8c 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 ase(pMem);. pMe
19a8d 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c m->zMalloc = sql
19a8e 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 ite3DbMallocRaw(
19a8f 64 62 2c 20 36 34 29 3b 0a 20 20 69 66 28 20 64 db, 64);. if( d
19a90 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
19a91 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 ){. pMem->fla
19a92 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 gs = MEM_Null;.
19a93 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 }else{. asse
19a94 72 74 28 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f rt( pMem->zMallo
19a95 63 20 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 75 c );. pMem->u
19a96 2e 70 52 6f 77 53 65 74 20 3d 20 73 71 6c 69 74 .pRowSet = sqlit
19a97 65 33 52 6f 77 53 65 74 49 6e 69 74 28 64 62 2c e3RowSetInit(db,
19a98 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 2c 20 pMem->zMalloc,
19a99 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
19a9a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19a9b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 sqlite3D
19a9c 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20 bMallocSize(db,
19a9d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 29 3b pMem->zMalloc));
19a9e 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 . assert( pMe
19a9f 6d 2d 3e 75 2e 70 52 6f 77 53 65 74 21 3d 30 20 m->u.pRowSet!=0
19aa0 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 );. pMem->fla
19aa1 67 73 20 3d 20 4d 45 4d 5f 52 6f 77 53 65 74 3b gs = MEM_RowSet;
19aa2 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 . }.}../*.** Re
19aa3 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 turn true if the
19aa4 20 4d 65 6d 20 6f 62 6a 65 63 74 20 63 6f 6e 74 Mem object cont
19aa5 61 69 6e 73 20 61 20 54 45 58 54 20 6f 72 20 42 ains a TEXT or B
19aa6 4c 4f 42 20 74 68 61 74 20 69 73 0a 2a 2a 20 74 LOB that is.** t
19aa7 6f 6f 20 6c 61 72 67 65 20 2d 20 77 68 6f 73 65 oo large - whose
19aa8 20 73 69 7a 65 20 65 78 63 65 65 64 73 20 53 51 size exceeds SQ
19aa9 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 2e LITE_MAX_LENGTH.
19aaa 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
19aab 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
19aac 62 65 4d 65 6d 54 6f 6f 42 69 67 28 4d 65 6d 20 beMemTooBig(Mem
19aad 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 *p){. assert( p
19aae 2d 3e 64 62 21 3d 30 20 29 3b 0a 20 20 69 66 28 ->db!=0 );. if(
19aaf 20 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d p->flags & (MEM
19ab0 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 _Str|MEM_Blob) )
19ab1 7b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 2d {. int n = p-
19ab2 3e 6e 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 >n;. if( p->f
19ab3 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 lags & MEM_Zero
19ab4 29 7b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 70 2d ){. n += p-
19ab5 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 7d 0a >u.nZero;. }.
19ab6 20 20 20 20 72 65 74 75 72 6e 20 6e 3e 70 2d 3e return n>p->
19ab7 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 db->aLimit[SQLIT
19ab8 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b E_LIMIT_LENGTH];
19ab9 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b . }. return 0;
19aba 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 69 7a 65 20 .}../*.** Size
19abb 6f 66 20 73 74 72 75 63 74 20 4d 65 6d 20 6e 6f of struct Mem no
19abc 74 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 t including the
19abd 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 6d 65 6d 62 Mem.zMalloc memb
19abe 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d er..*/.#define M
19abf 45 4d 43 45 4c 4c 53 49 5a 45 20 28 73 69 7a 65 EMCELLSIZE (size
19ac0 5f 74 29 28 26 28 28 28 4d 65 6d 20 2a 29 30 29 _t)(&(((Mem *)0)
19ac1 2d 3e 7a 4d 61 6c 6c 6f 63 29 29 0a 0a 2f 2a 0a ->zMalloc))../*.
19ac2 2a 2a 20 4d 61 6b 65 20 61 6e 20 73 68 61 6c 6c ** Make an shall
19ac3 6f 77 20 63 6f 70 79 20 6f 66 20 70 46 72 6f 6d ow copy of pFrom
19ac4 20 69 6e 74 6f 20 70 54 6f 2e 20 20 50 72 69 6f into pTo. Prio
19ac5 72 20 63 6f 6e 74 65 6e 74 73 20 6f 66 0a 2a 2a r contents of.**
19ac6 20 70 54 6f 20 61 72 65 20 66 72 65 65 64 2e 20 pTo are freed.
19ac7 20 54 68 65 20 70 46 72 6f 6d 2d 3e 7a 20 66 69 The pFrom->z fi
19ac8 65 6c 64 20 69 73 20 6e 6f 74 20 64 75 70 6c 69 eld is not dupli
19ac9 63 61 74 65 64 2e 20 20 49 66 0a 2a 2a 20 70 46 cated. If.** pF
19aca 72 6f 6d 2d 3e 7a 20 69 73 20 75 73 65 64 2c 20 rom->z is used,
19acb 74 68 65 6e 20 70 54 6f 2d 3e 7a 20 70 6f 69 6e then pTo->z poin
19acc 74 73 20 74 6f 20 74 68 65 20 73 61 6d 65 20 74 ts to the same t
19acd 68 69 6e 67 20 61 73 20 70 46 72 6f 6d 2d 3e 7a hing as pFrom->z
19ace 0a 2a 2a 20 61 6e 64 20 66 6c 61 67 73 20 67 65 .** and flags ge
19acf 74 73 20 73 72 63 54 79 70 65 20 28 65 69 74 68 ts srcType (eith
19ad0 65 72 20 4d 45 4d 5f 45 70 68 65 6d 20 6f 72 20 er MEM_Ephem or
19ad1 4d 45 4d 5f 53 74 61 74 69 63 29 2e 0a 2a 2f 0a MEM_Static)..*/.
19ad2 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
19ad3 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d oid sqlite3VdbeM
19ad4 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 4d 65 emShallowCopy(Me
19ad5 6d 20 2a 70 54 6f 2c 20 63 6f 6e 73 74 20 4d 65 m *pTo, const Me
19ad6 6d 20 2a 70 46 72 6f 6d 2c 20 69 6e 74 20 73 72 m *pFrom, int sr
19ad7 63 54 79 70 65 29 7b 0a 20 20 61 73 73 65 72 74 cType){. assert
19ad8 28 20 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 20 ( (pFrom->flags
19ad9 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 & MEM_RowSet)==0
19ada 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 );. sqlite3Vdb
19adb 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 eMemReleaseExter
19adc 6e 61 6c 28 70 54 6f 29 3b 0a 20 20 6d 65 6d 63 nal(pTo);. memc
19add 70 79 28 70 54 6f 2c 20 70 46 72 6f 6d 2c 20 4d py(pTo, pFrom, M
19ade 45 4d 43 45 4c 4c 53 49 5a 45 29 3b 0a 20 20 70 EMCELLSIZE);. p
19adf 54 6f 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 To->xDel = 0;.
19ae0 69 66 28 20 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 if( (pFrom->flag
19ae1 73 26 4d 45 4d 5f 44 79 6e 29 21 3d 30 20 7c 7c s&MEM_Dyn)!=0 ||
19ae2 20 70 46 72 6f 6d 2d 3e 7a 3d 3d 70 46 72 6f 6d pFrom->z==pFrom
19ae3 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 ->zMalloc ){.
19ae4 20 70 54 6f 2d 3e 66 6c 61 67 73 20 26 3d 20 7e pTo->flags &= ~
19ae5 28 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 53 74 61 (MEM_Dyn|MEM_Sta
19ae6 74 69 63 7c 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a tic|MEM_Ephem);.
19ae7 20 20 20 20 61 73 73 65 72 74 28 20 73 72 63 54 assert( srcT
19ae8 79 70 65 3d 3d 4d 45 4d 5f 45 70 68 65 6d 20 7c ype==MEM_Ephem |
19ae9 7c 20 73 72 63 54 79 70 65 3d 3d 4d 45 4d 5f 53 | srcType==MEM_S
19aea 74 61 74 69 63 20 29 3b 0a 20 20 20 20 70 54 6f tatic );. pTo
19aeb 2d 3e 66 6c 61 67 73 20 7c 3d 20 73 72 63 54 79 ->flags |= srcTy
19aec 70 65 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a pe;. }.}../*.**
19aed 20 4d 61 6b 65 20 61 20 66 75 6c 6c 20 63 6f 70 Make a full cop
19aee 79 20 6f 66 20 70 46 72 6f 6d 20 69 6e 74 6f 20 y of pFrom into
19aef 70 54 6f 2e 20 20 50 72 69 6f 72 20 63 6f 6e 74 pTo. Prior cont
19af0 65 6e 74 73 20 6f 66 20 70 54 6f 20 61 72 65 0a ents of pTo are.
19af1 2a 2a 20 66 72 65 65 64 20 62 65 66 6f 72 65 20 ** freed before
19af2 74 68 65 20 63 6f 70 79 20 69 73 20 6d 61 64 65 the copy is made
19af3 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
19af4 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
19af5 64 62 65 4d 65 6d 43 6f 70 79 28 4d 65 6d 20 2a dbeMemCopy(Mem *
19af6 70 54 6f 2c 20 63 6f 6e 73 74 20 4d 65 6d 20 2a pTo, const Mem *
19af7 70 46 72 6f 6d 29 7b 0a 20 20 69 6e 74 20 72 63 pFrom){. int rc
19af8 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 = SQLITE_OK;..
19af9 20 61 73 73 65 72 74 28 20 28 70 46 72 6f 6d 2d assert( (pFrom-
19afa 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 >flags & MEM_Row
19afb 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 73 71 6c Set)==0 );. sql
19afc 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
19afd 73 65 45 78 74 65 72 6e 61 6c 28 70 54 6f 29 3b seExternal(pTo);
19afe 0a 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 70 . memcpy(pTo, p
19aff 46 72 6f 6d 2c 20 4d 45 4d 43 45 4c 4c 53 49 5a From, MEMCELLSIZ
19b00 45 29 3b 0a 20 20 70 54 6f 2d 3e 66 6c 61 67 73 E);. pTo->flags
19b01 20 26 3d 20 7e 4d 45 4d 5f 44 79 6e 3b 0a 0a 20 &= ~MEM_Dyn;..
19b02 20 69 66 28 20 70 54 6f 2d 3e 66 6c 61 67 73 26 if( pTo->flags&
19b03 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f (MEM_Str|MEM_Blo
19b04 62 29 20 29 7b 0a 20 20 20 20 69 66 28 20 30 3d b) ){. if( 0=
19b05 3d 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 26 4d =(pFrom->flags&M
19b06 45 4d 5f 53 74 61 74 69 63 29 20 29 7b 0a 20 20 EM_Static) ){.
19b07 20 20 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20 7c pTo->flags |
19b08 3d 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 = MEM_Ephem;.
19b09 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 rc = sqlite3V
19b0a 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 dbeMemMakeWritea
19b0b 62 6c 65 28 70 54 6f 29 3b 0a 20 20 20 20 7d 0a ble(pTo);. }.
19b0c 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 }.. return rc
19b0d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 ;.}../*.** Trans
19b0e 66 65 72 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 fer the contents
19b0f 20 6f 66 20 70 46 72 6f 6d 20 74 6f 20 70 54 6f of pFrom to pTo
19b10 2e 20 41 6e 79 20 65 78 69 73 74 69 6e 67 20 76 . Any existing v
19b11 61 6c 75 65 20 69 6e 20 70 54 6f 20 69 73 0a 2a alue in pTo is.*
19b12 2a 20 66 72 65 65 64 2e 20 49 66 20 70 46 72 6f * freed. If pFro
19b13 6d 20 63 6f 6e 74 61 69 6e 73 20 65 70 68 65 6d m contains ephem
19b14 65 72 61 6c 20 64 61 74 61 2c 20 61 20 63 6f 70 eral data, a cop
19b15 79 20 69 73 20 6d 61 64 65 2e 0a 2a 2a 0a 2a 2a y is made..**.**
19b16 20 70 46 72 6f 6d 20 63 6f 6e 74 61 69 6e 73 20 pFrom contains
19b17 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 77 68 65 6e an SQL NULL when
19b18 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 this routine re
19b19 74 75 72 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 turns..*/.SQLITE
19b1a 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
19b1b 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 lite3VdbeMemMove
19b1c 28 4d 65 6d 20 2a 70 54 6f 2c 20 4d 65 6d 20 2a (Mem *pTo, Mem *
19b1d 70 46 72 6f 6d 29 7b 0a 20 20 61 73 73 65 72 74 pFrom){. assert
19b1e 28 20 70 46 72 6f 6d 2d 3e 64 62 3d 3d 30 20 7c ( pFrom->db==0 |
19b1f 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f | sqlite3_mutex_
19b20 68 65 6c 64 28 70 46 72 6f 6d 2d 3e 64 62 2d 3e held(pFrom->db->
19b21 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
19b22 72 74 28 20 70 54 6f 2d 3e 64 62 3d 3d 30 20 7c rt( pTo->db==0 |
19b23 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f | sqlite3_mutex_
19b24 68 65 6c 64 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 held(pTo->db->mu
19b25 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
19b26 28 20 70 46 72 6f 6d 2d 3e 64 62 3d 3d 30 20 7c ( pFrom->db==0 |
19b27 7c 20 70 54 6f 2d 3e 64 62 3d 3d 30 20 7c 7c 20 | pTo->db==0 ||
19b28 70 46 72 6f 6d 2d 3e 64 62 3d 3d 70 54 6f 2d 3e pFrom->db==pTo->
19b29 64 62 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 db );.. sqlite3
19b2a 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 VdbeMemRelease(p
19b2b 54 6f 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 54 To);. memcpy(pT
19b2c 6f 2c 20 70 46 72 6f 6d 2c 20 73 69 7a 65 6f 66 o, pFrom, sizeof
19b2d 28 4d 65 6d 29 29 3b 0a 20 20 70 46 72 6f 6d 2d (Mem));. pFrom-
19b2e 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c >flags = MEM_Nul
19b2f 6c 3b 0a 20 20 70 46 72 6f 6d 2d 3e 78 44 65 6c l;. pFrom->xDel
19b30 20 3d 20 30 3b 0a 20 20 70 46 72 6f 6d 2d 3e 7a = 0;. pFrom->z
19b31 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 7d 0a 0a 2f Malloc = 0;.}../
19b32 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 *.** Change the
19b33 76 61 6c 75 65 20 6f 66 20 61 20 4d 65 6d 20 74 value of a Mem t
19b34 6f 20 62 65 20 61 20 73 74 72 69 6e 67 20 6f 72 o be a string or
19b35 20 61 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20 54 a BLOB..**.** T
19b36 68 65 20 6d 65 6d 6f 72 79 20 6d 61 6e 61 67 65 he memory manage
19b37 6d 65 6e 74 20 73 74 72 61 74 65 67 79 20 64 65 ment strategy de
19b38 70 65 6e 64 73 20 6f 6e 20 74 68 65 20 76 61 6c pends on the val
19b39 75 65 20 6f 66 20 74 68 65 20 78 44 65 6c 0a 2a ue of the xDel.*
19b3a 2a 20 70 61 72 61 6d 65 74 65 72 2e 20 49 66 20 * parameter. If
19b3b 74 68 65 20 76 61 6c 75 65 20 70 61 73 73 65 64 the value passed
19b3c 20 69 73 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 is SQLITE_TRANS
19b3d 49 45 4e 54 2c 20 74 68 65 6e 20 74 68 65 20 0a IENT, then the .
19b3e 2a 2a 20 73 74 72 69 6e 67 20 69 73 20 63 6f 70 ** string is cop
19b3f 69 65 64 20 69 6e 74 6f 20 61 20 28 70 6f 73 73 ied into a (poss
19b40 69 62 6c 79 20 65 78 69 73 74 69 6e 67 29 20 62 ibly existing) b
19b41 75 66 66 65 72 20 6d 61 6e 61 67 65 64 20 62 79 uffer managed by
19b42 20 74 68 65 20 0a 2a 2a 20 4d 65 6d 20 73 74 72 the .** Mem str
19b43 75 63 74 75 72 65 2e 20 4f 74 68 65 72 77 69 73 ucture. Otherwis
19b44 65 2c 20 61 6e 79 20 65 78 69 73 74 69 6e 67 20 e, any existing
19b45 62 75 66 66 65 72 20 69 73 20 66 72 65 65 64 20 buffer is freed
19b46 61 6e 64 20 74 68 65 0a 2a 2a 20 70 6f 69 6e 74 and the.** point
19b47 65 72 20 63 6f 70 69 65 64 2e 0a 2a 2a 0a 2a 2a er copied..**.**
19b48 20 49 66 20 74 68 65 20 73 74 72 69 6e 67 20 69 If the string i
19b49 73 20 74 6f 6f 20 6c 61 72 67 65 20 28 69 66 20 s too large (if
19b4a 69 74 20 65 78 63 65 65 64 73 20 74 68 65 20 53 it exceeds the S
19b4b 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 QLITE_LIMIT_LENG
19b4c 54 48 0a 2a 2a 20 73 69 7a 65 20 6c 69 6d 69 74 TH.** size limit
19b4d 29 20 74 68 65 6e 20 6e 6f 20 6d 65 6d 6f 72 79 ) then no memory
19b4e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 63 63 75 allocation occu
19b4f 72 73 2e 20 20 49 66 20 74 68 65 20 73 74 72 69 rs. If the stri
19b50 6e 67 20 63 61 6e 20 62 65 0a 2a 2a 20 73 74 6f ng can be.** sto
19b51 72 65 64 20 77 69 74 68 6f 75 74 20 61 6c 6c 6f red without allo
19b52 63 61 74 69 6e 67 20 6d 65 6d 6f 72 79 2c 20 74 cating memory, t
19b53 68 65 6e 20 69 74 20 69 73 2e 20 20 49 66 20 61 hen it is. If a
19b54 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
19b55 6f 6e 0a 2a 2a 20 69 73 20 72 65 71 75 69 72 65 on.** is require
19b56 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 73 d to store the s
19b57 74 72 69 6e 67 2c 20 74 68 65 6e 20 76 61 6c 75 tring, then valu
19b58 65 20 6f 66 20 70 4d 65 6d 20 69 73 20 75 6e 63 e of pMem is unc
19b59 68 61 6e 67 65 64 2e 20 20 49 6e 0a 2a 2a 20 65 hanged. In.** e
19b5a 69 74 68 65 72 20 63 61 73 65 2c 20 53 51 4c 49 ither case, SQLI
19b5b 54 45 5f 54 4f 4f 42 49 47 20 69 73 20 72 65 74 TE_TOOBIG is ret
19b5c 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 urned..*/.SQLITE
19b5d 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
19b5e 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 ite3VdbeMemSetSt
19b5f 72 28 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 r(. Mem *pMem,
19b60 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f /* Memo
19b61 72 79 20 63 65 6c 6c 20 74 6f 20 73 65 74 20 74 ry cell to set t
19b62 6f 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 2a o string value *
19b63 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
19b64 7a 2c 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e z, /* Strin
19b65 67 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 69 g pointer */. i
19b66 6e 74 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 nt n,
19b67 20 20 20 2f 2a 20 42 79 74 65 73 20 69 6e 20 73 /* Bytes in s
19b68 74 72 69 6e 67 2c 20 6f 72 20 6e 65 67 61 74 69 tring, or negati
19b69 76 65 20 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20 ve */. u8 enc,
19b6a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 /* E
19b6b 6e 63 6f 64 69 6e 67 20 6f 66 20 7a 2e 20 20 30 ncoding of z. 0
19b6c 20 66 6f 72 20 42 4c 4f 42 73 20 2a 2f 0a 20 20 for BLOBs */.
19b6d 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 void (*xDel)(voi
19b6e 64 2a 29 20 2f 2a 20 44 65 73 74 72 75 63 74 6f d*) /* Destructo
19b6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b r function */.){
19b70 0a 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 6e . int nByte = n
19b71 3b 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 61 ; /* New va
19b72 6c 75 65 20 66 6f 72 20 70 4d 65 6d 2d 3e 6e 20 lue for pMem->n
19b73 2a 2f 0a 20 20 69 6e 74 20 69 4c 69 6d 69 74 3b */. int iLimit;
19b74 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 /* Maxi
19b75 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 73 74 72 69 mum allowed stri
19b76 6e 67 20 6f 72 20 62 6c 6f 62 20 73 69 7a 65 20 ng or blob size
19b77 2a 2f 0a 20 20 75 31 36 20 66 6c 61 67 73 20 3d */. u16 flags =
19b78 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 0; /* New
19b79 76 61 6c 75 65 20 66 6f 72 20 70 4d 65 6d 2d 3e value for pMem->
19b7a 66 6c 61 67 73 20 2a 2f 0a 0a 20 20 61 73 73 65 flags */.. asse
19b7b 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 rt( pMem->db==0
19b7c 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 || sqlite3_mutex
19b7d 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e _held(pMem->db->
19b7e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
19b7f 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 rt( (pMem->flags
19b80 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d & MEM_RowSet)==
19b81 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 7a 20 0 );.. /* If z
19b82 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 is a NULL pointe
19b83 72 2c 20 73 65 74 20 70 4d 65 6d 20 74 6f 20 63 r, set pMem to c
19b84 6f 6e 74 61 69 6e 20 61 6e 20 53 51 4c 20 4e 55 ontain an SQL NU
19b85 4c 4c 2e 20 2a 2f 0a 20 20 69 66 28 20 21 7a 20 LL. */. if( !z
19b86 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
19b87 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4d 65 beMemSetNull(pMe
19b88 6d 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 m);. return S
19b89 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 QLITE_OK;. }..
19b8a 20 69 66 28 20 70 4d 65 6d 2d 3e 64 62 20 29 7b if( pMem->db ){
19b8b 0a 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 70 4d . iLimit = pM
19b8c 65 6d 2d 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 em->db->aLimit[S
19b8d 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 QLITE_LIMIT_LENG
19b8e 54 48 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 TH];. }else{.
19b8f 20 20 69 4c 69 6d 69 74 20 3d 20 53 51 4c 49 54 iLimit = SQLIT
19b90 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 3b 0a 20 20 E_MAX_LENGTH;.
19b91 7d 0a 20 20 66 6c 61 67 73 20 3d 20 28 65 6e 63 }. flags = (enc
19b92 3d 3d 30 3f 4d 45 4d 5f 42 6c 6f 62 3a 4d 45 4d ==0?MEM_Blob:MEM
19b93 5f 53 74 72 29 3b 0a 20 20 69 66 28 20 6e 42 79 _Str);. if( nBy
19b94 74 65 3c 30 20 29 7b 0a 20 20 20 20 61 73 73 65 te<0 ){. asse
19b95 72 74 28 20 65 6e 63 21 3d 30 20 29 3b 0a 20 20 rt( enc!=0 );.
19b96 20 20 69 66 28 20 65 6e 63 3d 3d 53 51 4c 49 54 if( enc==SQLIT
19b97 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 20 20 E_UTF8 ){.
19b98 66 6f 72 28 6e 42 79 74 65 3d 30 3b 20 6e 42 79 for(nByte=0; nBy
19b99 74 65 3c 3d 69 4c 69 6d 69 74 20 26 26 20 7a 5b te<=iLimit && z[
19b9a 6e 42 79 74 65 5d 3b 20 6e 42 79 74 65 2b 2b 29 nByte]; nByte++)
19b9b 7b 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 {}. }else{.
19b9c 20 20 20 20 66 6f 72 28 6e 42 79 74 65 3d 30 3b for(nByte=0;
19b9d 20 6e 42 79 74 65 3c 3d 69 4c 69 6d 69 74 20 26 nByte<=iLimit &
19b9e 26 20 28 7a 5b 6e 42 79 74 65 5d 20 7c 20 7a 5b & (z[nByte] | z[
19b9f 6e 42 79 74 65 2b 31 5d 29 3b 20 6e 42 79 74 65 nByte+1]); nByte
19ba0 2b 3d 32 29 7b 7d 0a 20 20 20 20 7d 0a 20 20 20 +=2){}. }.
19ba1 20 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 flags |= MEM_Te
19ba2 72 6d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 rm;. }.. /* Th
19ba3 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 e following bloc
19ba4 6b 20 73 65 74 73 20 74 68 65 20 6e 65 77 20 76 k sets the new v
19ba5 61 6c 75 65 73 20 6f 66 20 4d 65 6d 2e 7a 20 61 alues of Mem.z a
19ba6 6e 64 20 4d 65 6d 2e 78 44 65 6c 2e 20 49 74 0a nd Mem.xDel. It.
19ba7 20 20 2a 2a 20 61 6c 73 6f 20 73 65 74 73 20 61 ** also sets a
19ba8 20 66 6c 61 67 20 69 6e 20 6c 6f 63 61 6c 20 76 flag in local v
19ba9 61 72 69 61 62 6c 65 20 22 66 6c 61 67 73 22 20 ariable "flags"
19baa 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 to indicate the
19bab 6d 65 6d 6f 72 79 0a 20 20 2a 2a 20 6d 61 6e 61 memory. ** mana
19bac 67 65 6d 65 6e 74 20 28 6f 6e 65 20 6f 66 20 4d gement (one of M
19bad 45 4d 5f 44 79 6e 20 6f 72 20 4d 45 4d 5f 53 74 EM_Dyn or MEM_St
19bae 61 74 69 63 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 atic).. */. if
19baf 28 20 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 54 ( xDel==SQLITE_T
19bb0 52 41 4e 53 49 45 4e 54 20 29 7b 0a 20 20 20 20 RANSIENT ){.
19bb1 69 6e 74 20 6e 41 6c 6c 6f 63 20 3d 20 6e 42 79 int nAlloc = nBy
19bb2 74 65 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 te;. if( flag
19bb3 73 26 4d 45 4d 5f 54 65 72 6d 20 29 7b 0a 20 20 s&MEM_Term ){.
19bb4 20 20 20 20 6e 41 6c 6c 6f 63 20 2b 3d 20 28 65 nAlloc += (e
19bb5 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 3f nc==SQLITE_UTF8?
19bb6 31 3a 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 1:2);. }.
19bb7 69 66 28 20 6e 42 79 74 65 3e 69 4c 69 6d 69 74 if( nByte>iLimit
19bb8 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
19bb9 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a SQLITE_TOOBIG;.
19bba 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 }. if( sq
19bbb 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 lite3VdbeMemGrow
19bbc 28 70 4d 65 6d 2c 20 6e 41 6c 6c 6f 63 2c 20 30 (pMem, nAlloc, 0
19bbd 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 ) ){. retur
19bbe 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
19bbf 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 }. memcpy
19bc0 28 70 4d 65 6d 2d 3e 7a 2c 20 7a 2c 20 6e 41 6c (pMem->z, z, nAl
19bc1 6c 6f 63 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 loc);. }else if
19bc2 28 20 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 44 ( xDel==SQLITE_D
19bc3 59 4e 41 4d 49 43 20 29 7b 0a 20 20 20 20 73 71 YNAMIC ){. sq
19bc4 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
19bc5 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70 ase(pMem);. p
19bc6 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 70 Mem->zMalloc = p
19bc7 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 20 2a Mem->z = (char *
19bc8 29 7a 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 78 44 )z;. pMem->xD
19bc9 65 6c 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b el = 0;. }else{
19bca 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
19bcb 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 MemRelease(pMem)
19bcc 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 ;. pMem->z =
19bcd 28 63 68 61 72 20 2a 29 7a 3b 0a 20 20 20 20 70 (char *)z;. p
19bce 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20 78 44 65 6c Mem->xDel = xDel
19bcf 3b 0a 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 28 ;. flags |= (
19bd0 28 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 53 54 (xDel==SQLITE_ST
19bd1 41 54 49 43 29 3f 4d 45 4d 5f 53 74 61 74 69 63 ATIC)?MEM_Static
19bd2 3a 4d 45 4d 5f 44 79 6e 29 3b 0a 20 20 7d 0a 0a :MEM_Dyn);. }..
19bd3 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6e 42 79 74 pMem->n = nByt
19bd4 65 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 e;. pMem->flags
19bd5 20 3d 20 66 6c 61 67 73 3b 0a 20 20 70 4d 65 6d = flags;. pMem
19bd6 2d 3e 65 6e 63 20 3d 20 28 65 6e 63 3d 3d 30 20 ->enc = (enc==0
19bd7 3f 20 53 51 4c 49 54 45 5f 55 54 46 38 20 3a 20 ? SQLITE_UTF8 :
19bd8 65 6e 63 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 enc);. pMem->ty
19bd9 70 65 20 3d 20 28 65 6e 63 3d 3d 30 20 3f 20 53 pe = (enc==0 ? S
19bda 51 4c 49 54 45 5f 42 4c 4f 42 20 3a 20 53 51 4c QLITE_BLOB : SQL
19bdb 49 54 45 5f 54 45 58 54 29 3b 0a 0a 23 69 66 6e ITE_TEXT);..#ifn
19bdc 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
19bdd 55 54 46 31 36 0a 20 20 69 66 28 20 70 4d 65 6d UTF16. if( pMem
19bde 2d 3e 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 ->enc!=SQLITE_UT
19bdf 46 38 20 26 26 20 73 71 6c 69 74 65 33 56 64 62 F8 && sqlite3Vdb
19be0 65 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 70 4d eMemHandleBom(pM
19be1 65 6d 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 em) ){. retur
19be2 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
19be3 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 }.#endif.. if
19be4 28 20 6e 42 79 74 65 3e 69 4c 69 6d 69 74 20 29 ( nByte>iLimit )
19be5 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
19be6 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 7d 0a ITE_TOOBIG;. }.
19be7 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
19be8 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f _OK;.}../*.** Co
19be9 6d 70 61 72 65 20 74 68 65 20 76 61 6c 75 65 73 mpare the values
19bea 20 63 6f 6e 74 61 69 6e 65 64 20 62 79 20 74 68 contained by th
19beb 65 20 74 77 6f 20 6d 65 6d 6f 72 79 20 63 65 6c e two memory cel
19bec 6c 73 2c 20 72 65 74 75 72 6e 69 6e 67 0a 2a 2a ls, returning.**
19bed 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f 20 negative, zero
19bee 6f 72 20 70 6f 73 69 74 69 76 65 20 69 66 20 70 or positive if p
19bef 4d 65 6d 31 20 69 73 20 6c 65 73 73 20 74 68 61 Mem1 is less tha
19bf0 6e 2c 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 n, equal to, or
19bf1 67 72 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20 greater.** than
19bf2 70 4d 65 6d 32 2e 20 53 6f 72 74 69 6e 67 20 6f pMem2. Sorting o
19bf3 72 64 65 72 20 69 73 20 4e 55 4c 4c 27 73 20 66 rder is NULL's f
19bf4 69 72 73 74 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 irst, followed b
19bf5 79 20 6e 75 6d 62 65 72 73 20 28 69 6e 74 65 67 y numbers (integ
19bf6 65 72 73 0a 2a 2a 20 61 6e 64 20 72 65 61 6c 73 ers.** and reals
19bf7 29 20 73 6f 72 74 65 64 20 6e 75 6d 65 72 69 63 ) sorted numeric
19bf8 61 6c 6c 79 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 ally, followed b
19bf9 79 20 74 65 78 74 20 6f 72 64 65 72 65 64 20 62 y text ordered b
19bfa 79 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 0a y the collating.
19bfb 2a 2a 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c ** sequence pCol
19bfc 6c 20 61 6e 64 20 66 69 6e 61 6c 6c 79 20 62 6c l and finally bl
19bfd 6f 62 27 73 20 6f 72 64 65 72 65 64 20 62 79 20 ob's ordered by
19bfe 6d 65 6d 63 6d 70 28 29 2e 0a 2a 2a 0a 2a 2a 20 memcmp()..**.**
19bff 54 77 6f 20 4e 55 4c 4c 20 76 61 6c 75 65 73 20 Two NULL values
19c00 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 65 are considered e
19c01 71 75 61 6c 20 62 79 20 74 68 69 73 20 66 75 6e qual by this fun
19c02 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ction..*/.SQLITE
19c03 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
19c04 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 63 ite3MemCompare(c
19c05 6f 6e 73 74 20 4d 65 6d 20 2a 70 4d 65 6d 31 2c onst Mem *pMem1,
19c06 20 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 4d 65 6d const Mem *pMem
19c07 32 2c 20 63 6f 6e 73 74 20 43 6f 6c 6c 53 65 71 2, const CollSeq
19c08 20 2a 70 43 6f 6c 6c 29 7b 0a 20 20 69 6e 74 20 *pColl){. int
19c09 72 63 3b 0a 20 20 69 6e 74 20 66 31 2c 20 66 32 rc;. int f1, f2
19c0a 3b 0a 20 20 69 6e 74 20 63 6f 6d 62 69 6e 65 64 ;. int combined
19c0b 5f 66 6c 61 67 73 3b 0a 0a 20 20 2f 2a 20 49 6e _flags;.. /* In
19c0c 74 65 72 63 68 61 6e 67 65 20 70 4d 65 6d 31 20 terchange pMem1
19c0d 61 6e 64 20 70 4d 65 6d 32 20 69 66 20 74 68 65 and pMem2 if the
19c0e 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 collating seque
19c0f 6e 63 65 20 73 70 65 63 69 66 69 65 73 0a 20 20 nce specifies.
19c10 2a 2a 20 44 45 53 43 20 6f 72 64 65 72 2e 0a 20 ** DESC order..
19c11 20 2a 2f 0a 20 20 66 31 20 3d 20 70 4d 65 6d 31 */. f1 = pMem1
19c12 2d 3e 66 6c 61 67 73 3b 0a 20 20 66 32 20 3d 20 ->flags;. f2 =
19c13 70 4d 65 6d 32 2d 3e 66 6c 61 67 73 3b 0a 20 20 pMem2->flags;.
19c14 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 20 3d combined_flags =
19c15 20 66 31 7c 66 32 3b 0a 20 20 61 73 73 65 72 74 f1|f2;. assert
19c16 28 20 28 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 ( (combined_flag
19c17 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d s & MEM_RowSet)=
19c18 3d 30 20 29 3b 0a 20 0a 20 20 2f 2a 20 49 66 20 =0 );. . /* If
19c19 6f 6e 65 20 76 61 6c 75 65 20 69 73 20 4e 55 4c one value is NUL
19c1a 4c 2c 20 69 74 20 69 73 20 6c 65 73 73 20 74 68 L, it is less th
19c1b 61 6e 20 74 68 65 20 6f 74 68 65 72 2e 20 49 66 an the other. If
19c1c 20 62 6f 74 68 20 76 61 6c 75 65 73 0a 20 20 2a both values. *
19c1d 2a 20 61 72 65 20 4e 55 4c 4c 2c 20 72 65 74 75 * are NULL, retu
19c1e 72 6e 20 30 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 rn 0.. */. if(
19c1f 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 26 combined_flags&
19c20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 MEM_Null ){.
19c21 72 65 74 75 72 6e 20 28 66 32 26 4d 45 4d 5f 4e return (f2&MEM_N
19c22 75 6c 6c 29 20 2d 20 28 66 31 26 4d 45 4d 5f 4e ull) - (f1&MEM_N
19c23 75 6c 6c 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 ull);. }.. /*
19c24 49 66 20 6f 6e 65 20 76 61 6c 75 65 20 69 73 20 If one value is
19c25 61 20 6e 75 6d 62 65 72 20 61 6e 64 20 74 68 65 a number and the
19c26 20 6f 74 68 65 72 20 69 73 20 6e 6f 74 2c 20 74 other is not, t
19c27 68 65 20 6e 75 6d 62 65 72 20 69 73 20 6c 65 73 he number is les
19c28 73 2e 0a 20 20 2a 2a 20 49 66 20 62 6f 74 68 20 s.. ** If both
19c29 61 72 65 20 6e 75 6d 62 65 72 73 2c 20 63 6f 6d are numbers, com
19c2a 70 61 72 65 20 61 73 20 72 65 61 6c 73 20 69 66 pare as reals if
19c2b 20 6f 6e 65 20 69 73 20 61 20 72 65 61 6c 2c 20 one is a real,
19c2c 6f 72 20 61 73 20 69 6e 74 65 67 65 72 73 0a 20 or as integers.
19c2d 20 2a 2a 20 69 66 20 62 6f 74 68 20 76 61 6c 75 ** if both valu
19c2e 65 73 20 61 72 65 20 69 6e 74 65 67 65 72 73 2e es are integers.
19c2f 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f 6d 62 . */. if( comb
19c30 69 6e 65 64 5f 66 6c 61 67 73 26 28 4d 45 4d 5f ined_flags&(MEM_
19c31 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 20 29 7b Int|MEM_Real) ){
19c32 0a 20 20 20 20 69 66 28 20 21 28 66 31 26 28 4d . if( !(f1&(M
19c33 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 EM_Int|MEM_Real)
19c34 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 ) ){. retur
19c35 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 n 1;. }. i
19c36 66 28 20 21 28 66 32 26 28 4d 45 4d 5f 49 6e 74 f( !(f2&(MEM_Int
19c37 7c 4d 45 4d 5f 52 65 61 6c 29 29 20 29 7b 0a 20 |MEM_Real)) ){.
19c38 20 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a return -1;.
19c39 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 66 }. if( (f
19c3a 31 20 26 20 66 32 20 26 20 4d 45 4d 5f 49 6e 74 1 & f2 & MEM_Int
19c3b 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 6f )==0 ){. do
19c3c 75 62 6c 65 20 72 31 2c 20 72 32 3b 0a 20 20 20 uble r1, r2;.
19c3d 20 20 20 69 66 28 20 28 66 31 26 4d 45 4d 5f 52 if( (f1&MEM_R
19c3e 65 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 eal)==0 ){.
19c3f 20 20 20 72 31 20 3d 20 28 64 6f 75 62 6c 65 29 r1 = (double)
19c40 70 4d 65 6d 31 2d 3e 75 2e 69 3b 0a 20 20 20 20 pMem1->u.i;.
19c41 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
19c42 20 72 31 20 3d 20 70 4d 65 6d 31 2d 3e 72 3b 0a r1 = pMem1->r;.
19c43 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
19c44 28 20 28 66 32 26 4d 45 4d 5f 52 65 61 6c 29 3d ( (f2&MEM_Real)=
19c45 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 32 =0 ){. r2
19c46 20 3d 20 28 64 6f 75 62 6c 65 29 70 4d 65 6d 32 = (double)pMem2
19c47 2d 3e 75 2e 69 3b 0a 20 20 20 20 20 20 7d 65 6c ->u.i;. }el
19c48 73 65 7b 0a 20 20 20 20 20 20 20 20 72 32 20 3d se{. r2 =
19c49 20 70 4d 65 6d 32 2d 3e 72 3b 0a 20 20 20 20 20 pMem2->r;.
19c4a 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 31 3c }. if( r1<
19c4b 72 32 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a r2 ) return -1;.
19c4c 20 20 20 20 20 20 69 66 28 20 72 31 3e 72 32 20 if( r1>r2
19c4d 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 ) return 1;.
19c4e 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 return 0;.
19c4f 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 }else{. ass
19c50 65 72 74 28 20 66 31 26 4d 45 4d 5f 49 6e 74 20 ert( f1&MEM_Int
19c51 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
19c52 20 66 32 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 f2&MEM_Int );.
19c53 20 20 20 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e if( pMem1->
19c54 75 2e 69 20 3c 20 70 4d 65 6d 32 2d 3e 75 2e 69 u.i < pMem2->u.i
19c55 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 ) return -1;.
19c56 20 20 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e 75 if( pMem1->u
19c57 2e 69 20 3e 20 70 4d 65 6d 32 2d 3e 75 2e 69 20 .i > pMem2->u.i
19c58 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 ) return 1;.
19c59 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 return 0;.
19c5a 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6f }. }.. /* If o
19c5b 6e 65 20 76 61 6c 75 65 20 69 73 20 61 20 73 74 ne value is a st
19c5c 72 69 6e 67 20 61 6e 64 20 74 68 65 20 6f 74 68 ring and the oth
19c5d 65 72 20 69 73 20 61 20 62 6c 6f 62 2c 20 74 68 er is a blob, th
19c5e 65 20 73 74 72 69 6e 67 20 69 73 20 6c 65 73 73 e string is less
19c5f 2e 0a 20 20 2a 2a 20 49 66 20 62 6f 74 68 20 61 .. ** If both a
19c60 72 65 20 73 74 72 69 6e 67 73 2c 20 63 6f 6d 70 re strings, comp
19c61 61 72 65 20 75 73 69 6e 67 20 74 68 65 20 63 6f are using the co
19c62 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e llating function
19c63 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f s.. */. if( co
19c64 6d 62 69 6e 65 64 5f 66 6c 61 67 73 26 4d 45 4d mbined_flags&MEM
19c65 5f 53 74 72 20 29 7b 0a 20 20 20 20 69 66 28 20 _Str ){. if(
19c66 28 66 31 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d (f1 & MEM_Str)==
19c67 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 0 ){. retur
19c68 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 n 1;. }. i
19c69 66 28 20 28 66 32 20 26 20 4d 45 4d 5f 53 74 72 f( (f2 & MEM_Str
19c6a 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 )==0 ){. re
19c6b 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 0a turn -1;. }..
19c6c 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d assert( pMem
19c6d 31 2d 3e 65 6e 63 3d 3d 70 4d 65 6d 32 2d 3e 65 1->enc==pMem2->e
19c6e 6e 63 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 nc );. assert
19c6f 28 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 53 51 ( pMem1->enc==SQ
19c70 4c 49 54 45 5f 55 54 46 38 20 7c 7c 20 0a 20 20 LITE_UTF8 || .
19c71 20 20 20 20 20 20 20 20 20 20 70 4d 65 6d 31 2d pMem1-
19c72 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 >enc==SQLITE_UTF
19c73 31 36 4c 45 20 7c 7c 20 70 4d 65 6d 31 2d 3e 65 16LE || pMem1->e
19c74 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 nc==SQLITE_UTF16
19c75 42 45 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 BE );.. /* Th
19c76 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 e collation sequ
19c77 65 6e 63 65 20 6d 75 73 74 20 62 65 20 64 65 66 ence must be def
19c78 69 6e 65 64 20 61 74 20 74 68 69 73 20 70 6f 69 ined at this poi
19c79 6e 74 2c 20 65 76 65 6e 20 69 66 0a 20 20 20 20 nt, even if.
19c7a 2a 2a 20 74 68 65 20 75 73 65 72 20 64 65 6c 65 ** the user dele
19c7b 74 65 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f tes the collatio
19c7c 6e 20 73 65 71 75 65 6e 63 65 20 61 66 74 65 72 n sequence after
19c7d 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 61 the vdbe progra
19c7e 6d 20 69 73 0a 20 20 20 20 2a 2a 20 63 6f 6d 70 m is. ** comp
19c7f 69 6c 65 64 20 28 74 68 69 73 20 77 61 73 20 6e iled (this was n
19c80 6f 74 20 61 6c 77 61 79 73 20 74 68 65 20 63 61 ot always the ca
19c81 73 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 se).. */.
19c82 61 73 73 65 72 74 28 20 21 70 43 6f 6c 6c 20 7c assert( !pColl |
19c83 7c 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 20 29 3b | pColl->xCmp );
19c84 0a 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 .. if( pColl
19c85 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65 ){. if( pMe
19c86 6d 31 2d 3e 65 6e 63 3d 3d 70 43 6f 6c 6c 2d 3e m1->enc==pColl->
19c87 65 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20 2f enc ){. /
19c88 2a 20 54 68 65 20 73 74 72 69 6e 67 73 20 61 72 * The strings ar
19c89 65 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 e already in the
19c8a 20 63 6f 72 72 65 63 74 20 65 6e 63 6f 64 69 6e correct encodin
19c8b 67 2e 20 20 43 61 6c 6c 20 74 68 65 0a 20 20 20 g. Call the.
19c8c 20 20 20 20 20 2a 2a 20 63 6f 6d 70 61 72 69 73 ** comparis
19c8d 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 64 69 72 65 on function dire
19c8e 63 74 6c 79 20 2a 2f 0a 20 20 20 20 20 20 20 20 ctly */.
19c8f 72 65 74 75 72 6e 20 70 43 6f 6c 6c 2d 3e 78 43 return pColl->xC
19c90 6d 70 28 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 2c mp(pColl->pUser,
19c91 70 4d 65 6d 31 2d 3e 6e 2c 70 4d 65 6d 31 2d 3e pMem1->n,pMem1->
19c92 7a 2c 70 4d 65 6d 32 2d 3e 6e 2c 70 4d 65 6d 32 z,pMem2->n,pMem2
19c93 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 ->z);. }els
19c94 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 e{. const
19c95 20 76 6f 69 64 20 2a 76 31 2c 20 2a 76 32 3b 0a void *v1, *v2;.
19c96 20 20 20 20 20 20 20 20 69 6e 74 20 6e 31 2c 20 int n1,
19c97 6e 32 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d 20 n2;. Mem
19c98 63 31 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d 20 c1;. Mem
19c99 63 32 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 c2;. mems
19c9a 65 74 28 26 63 31 2c 20 30 2c 20 73 69 7a 65 6f et(&c1, 0, sizeo
19c9b 66 28 63 31 29 29 3b 0a 20 20 20 20 20 20 20 20 f(c1));.
19c9c 6d 65 6d 73 65 74 28 26 63 32 2c 20 30 2c 20 73 memset(&c2, 0, s
19c9d 69 7a 65 6f 66 28 63 32 29 29 3b 0a 20 20 20 20 izeof(c2));.
19c9e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
19c9f 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 26 63 emShallowCopy(&c
19ca0 31 2c 20 70 4d 65 6d 31 2c 20 4d 45 4d 5f 45 70 1, pMem1, MEM_Ep
19ca1 68 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 73 71 hem);. sq
19ca2 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c lite3VdbeMemShal
19ca3 6c 6f 77 43 6f 70 79 28 26 63 32 2c 20 70 4d 65 lowCopy(&c2, pMe
19ca4 6d 32 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a m2, MEM_Ephem);.
19ca5 20 20 20 20 20 20 20 20 76 31 20 3d 20 73 71 6c v1 = sql
19ca6 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 28 73 ite3ValueText((s
19ca7 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 26 63 qlite3_value*)&c
19ca8 31 2c 20 70 43 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a 1, pColl->enc);.
19ca9 20 20 20 20 20 20 20 20 6e 31 20 3d 20 76 31 3d n1 = v1=
19caa 3d 30 20 3f 20 30 20 3a 20 63 31 2e 6e 3b 0a 20 =0 ? 0 : c1.n;.
19cab 20 20 20 20 20 20 20 76 32 20 3d 20 73 71 6c 69 v2 = sqli
19cac 74 65 33 56 61 6c 75 65 54 65 78 74 28 28 73 71 te3ValueText((sq
19cad 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 26 63 32 lite3_value*)&c2
19cae 2c 20 70 43 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a 20 , pColl->enc);.
19caf 20 20 20 20 20 20 20 6e 32 20 3d 20 76 32 3d 3d n2 = v2==
19cb0 30 20 3f 20 30 20 3a 20 63 32 2e 6e 3b 0a 20 20 0 ? 0 : c2.n;.
19cb1 20 20 20 20 20 20 72 63 20 3d 20 70 43 6f 6c 6c rc = pColl
19cb2 2d 3e 78 43 6d 70 28 70 43 6f 6c 6c 2d 3e 70 55 ->xCmp(pColl->pU
19cb3 73 65 72 2c 20 6e 31 2c 20 76 31 2c 20 6e 32 2c ser, n1, v1, n2,
19cb4 20 76 32 29 3b 0a 20 20 20 20 20 20 20 20 73 71 v2);. sq
19cb5 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
19cb6 61 73 65 28 26 63 31 29 3b 0a 20 20 20 20 20 20 ase(&c1);.
19cb7 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
19cb8 52 65 6c 65 61 73 65 28 26 63 32 29 3b 0a 20 20 Release(&c2);.
19cb9 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
19cba 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
19cbb 20 20 20 2f 2a 20 49 66 20 61 20 4e 55 4c 4c 20 /* If a NULL
19cbc 70 6f 69 6e 74 65 72 20 77 61 73 20 70 61 73 73 pointer was pass
19cbd 65 64 20 61 73 20 74 68 65 20 63 6f 6c 6c 61 74 ed as the collat
19cbe 65 20 66 75 6e 63 74 69 6f 6e 2c 20 66 61 6c 6c e function, fall
19cbf 20 74 68 72 6f 75 67 68 0a 20 20 20 20 2a 2a 20 through. **
19cc0 74 6f 20 74 68 65 20 62 6c 6f 62 20 63 61 73 65 to the blob case
19cc1 20 61 6e 64 20 75 73 65 20 6d 65 6d 63 6d 70 28 and use memcmp(
19cc2 29 2e 20 20 2a 2f 0a 20 20 7d 0a 20 0a 20 20 2f ). */. }. . /
19cc3 2a 20 42 6f 74 68 20 76 61 6c 75 65 73 20 6d 75 * Both values mu
19cc4 73 74 20 62 65 20 62 6c 6f 62 73 2e 20 20 43 6f st be blobs. Co
19cc5 6d 70 61 72 65 20 75 73 69 6e 67 20 6d 65 6d 63 mpare using memc
19cc6 6d 70 28 29 2e 20 20 2a 2f 0a 20 20 72 63 20 3d mp(). */. rc =
19cc7 20 6d 65 6d 63 6d 70 28 70 4d 65 6d 31 2d 3e 7a memcmp(pMem1->z
19cc8 2c 20 70 4d 65 6d 32 2d 3e 7a 2c 20 28 70 4d 65 , pMem2->z, (pMe
19cc9 6d 31 2d 3e 6e 3e 70 4d 65 6d 32 2d 3e 6e 29 3f m1->n>pMem2->n)?
19cca 70 4d 65 6d 32 2d 3e 6e 3a 70 4d 65 6d 31 2d 3e pMem2->n:pMem1->
19ccb 6e 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 30 20 n);. if( rc==0
19ccc 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 4d 65 6d ){. rc = pMem
19ccd 31 2d 3e 6e 20 2d 20 70 4d 65 6d 32 2d 3e 6e 3b 1->n - pMem2->n;
19cce 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
19ccf 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 ;.}../*.** Move
19cd0 64 61 74 61 20 6f 75 74 20 6f 66 20 61 20 62 74 data out of a bt
19cd1 72 65 65 20 6b 65 79 20 6f 72 20 64 61 74 61 20 ree key or data
19cd2 66 69 65 6c 64 20 61 6e 64 20 69 6e 74 6f 20 61 field and into a
19cd3 20 4d 65 6d 20 73 74 72 75 63 74 75 72 65 2e 0a Mem structure..
19cd4 2a 2a 20 54 68 65 20 64 61 74 61 20 6f 72 20 6b ** The data or k
19cd5 65 79 20 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d ey is taken from
19cd6 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 the entry that
19cd7 70 43 75 72 20 69 73 20 63 75 72 72 65 6e 74 6c pCur is currentl
19cd8 79 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a 20 74 6f y pointing.** to
19cd9 2e 20 20 6f 66 66 73 65 74 20 61 6e 64 20 61 6d . offset and am
19cda 74 20 64 65 74 65 72 6d 69 6e 65 20 77 68 61 74 t determine what
19cdb 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 portion of the
19cdc 64 61 74 61 20 6f 72 20 6b 65 79 20 74 6f 20 72 data or key to r
19cdd 65 74 72 69 65 76 65 2e 0a 2a 2a 20 6b 65 79 20 etrieve..** key
19cde 69 73 20 74 72 75 65 20 74 6f 20 67 65 74 20 74 is true to get t
19cdf 68 65 20 6b 65 79 20 6f 72 20 66 61 6c 73 65 20 he key or false
19ce0 74 6f 20 67 65 74 20 64 61 74 61 2e 20 20 54 68 to get data. Th
19ce1 65 20 72 65 73 75 6c 74 20 69 73 20 77 72 69 74 e result is writ
19ce2 74 65 6e 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 ten.** into the
19ce3 70 4d 65 6d 20 65 6c 65 6d 65 6e 74 2e 0a 2a 2a pMem element..**
19ce4 0a 2a 2a 20 54 68 65 20 70 4d 65 6d 20 73 74 72 .** The pMem str
19ce5 75 63 74 75 72 65 20 69 73 20 61 73 73 75 6d 65 ucture is assume
19ce6 64 20 74 6f 20 62 65 20 75 6e 69 6e 69 74 69 61 d to be uninitia
19ce7 6c 69 7a 65 64 2e 20 20 41 6e 79 20 70 72 69 6f lized. Any prio
19ce8 72 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 69 73 20 r content.** is
19ce9 6f 76 65 72 77 72 69 74 74 65 6e 20 77 69 74 68 overwritten with
19cea 6f 75 74 20 62 65 69 6e 67 20 66 72 65 65 64 2e out being freed.
19ceb 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 72 .**.** If this r
19cec 6f 75 74 69 6e 65 20 66 61 69 6c 73 20 66 6f 72 outine fails for
19ced 20 61 6e 79 20 72 65 61 73 6f 6e 20 28 6d 61 6c any reason (mal
19cee 6c 6f 63 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c loc returns NULL
19cef 20 6f 72 20 75 6e 61 62 6c 65 0a 2a 2a 20 74 6f or unable.** to
19cf0 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 64 read from the d
19cf1 69 73 6b 29 20 74 68 65 6e 20 74 68 65 20 70 4d isk) then the pM
19cf2 65 6d 20 69 73 20 6c 65 66 74 20 69 6e 20 61 6e em is left in an
19cf3 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73 74 inconsistent st
19cf4 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ate..*/.SQLITE_P
19cf5 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
19cf6 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 e3VdbeMemFromBtr
19cf7 65 65 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a ee(. BtCursor *
19cf8 70 43 75 72 2c 20 20 20 2f 2a 20 43 75 72 73 6f pCur, /* Curso
19cf9 72 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 72 65 r pointing at re
19cfa 63 6f 72 64 20 74 6f 20 72 65 74 72 69 65 76 65 cord to retrieve
19cfb 2e 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 73 65 . */. int offse
19cfc 74 2c 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 t, /* Offs
19cfd 65 74 20 66 72 6f 6d 20 74 68 65 20 73 74 61 72 et from the star
19cfe 74 20 6f 66 20 64 61 74 61 20 74 6f 20 72 65 74 t of data to ret
19cff 75 72 6e 20 62 79 74 65 73 20 66 72 6f 6d 2e 20 urn bytes from.
19d00 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20 20 */. int amt,
19d01 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
19d02 20 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 74 of bytes to ret
19d03 75 72 6e 2e 20 2a 2f 0a 20 20 69 6e 74 20 6b 65 urn. */. int ke
19d04 79 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 y, /* I
19d05 66 20 74 72 75 65 2c 20 72 65 74 72 69 65 76 65 f true, retrieve
19d06 20 66 72 6f 6d 20 74 68 65 20 62 74 72 65 65 20 from the btree
19d07 6b 65 79 2c 20 6e 6f 74 20 64 61 74 61 2e 20 2a key, not data. *
19d08 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 20 20 /. Mem *pMem
19d09 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 52 65 /* OUT: Re
19d0a 74 75 72 6e 20 64 61 74 61 20 69 6e 20 74 68 69 turn data in thi
19d0b 73 20 4d 65 6d 20 73 74 72 75 63 74 75 72 65 2e s Mem structure.
19d0c 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a */.){. char *z
19d0d 44 61 74 61 3b 20 20 20 20 20 20 20 20 2f 2a 20 Data; /*
19d0e 44 61 74 61 20 66 72 6f 6d 20 74 68 65 20 62 74 Data from the bt
19d0f 72 65 65 20 6c 61 79 65 72 20 2a 2f 0a 20 20 69 ree layer */. i
19d10 6e 74 20 61 76 61 69 6c 61 62 6c 65 20 3d 20 30 nt available = 0
19d11 3b 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 ; /* Number of
19d12 62 79 74 65 73 20 61 76 61 69 6c 61 62 6c 65 20 bytes available
19d13 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 62 74 72 on the local btr
19d14 65 65 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 ee page */. int
19d15 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
19d16 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 /* Return code
19d17 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 */.. assert( sq
19d18 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 lite3BtreeCursor
19d19 49 73 56 61 6c 69 64 28 70 43 75 72 29 20 29 3b IsValid(pCur) );
19d1a 0a 0a 20 20 2f 2a 20 4e 6f 74 65 3a 20 74 68 65 .. /* Note: the
19d1b 20 63 61 6c 6c 73 20 74 6f 20 42 74 72 65 65 4b calls to BtreeK
19d1c 65 79 46 65 74 63 68 28 29 20 61 6e 64 20 44 61 eyFetch() and Da
19d1d 74 61 46 65 74 63 68 28 29 20 62 65 6c 6f 77 20 taFetch() below
19d1e 61 73 73 65 72 74 28 29 20 0a 20 20 2a 2a 20 74 assert() . ** t
19d1f 68 61 74 20 62 6f 74 68 20 74 68 65 20 42 74 53 hat both the BtS
19d20 68 61 72 65 64 20 61 6e 64 20 64 61 74 61 62 61 hared and databa
19d21 73 65 20 68 61 6e 64 6c 65 20 6d 75 74 65 78 65 se handle mutexe
19d22 73 20 61 72 65 20 68 65 6c 64 2e 20 2a 2f 0a 20 s are held. */.
19d23 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e assert( (pMem->
19d24 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 flags & MEM_RowS
19d25 65 74 29 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 et)==0 );. if(
19d26 6b 65 79 20 29 7b 0a 20 20 20 20 7a 44 61 74 61 key ){. zData
19d27 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 = (char *)sqlit
19d28 65 33 42 74 72 65 65 4b 65 79 46 65 74 63 68 28 e3BtreeKeyFetch(
19d29 70 43 75 72 2c 20 26 61 76 61 69 6c 61 62 6c 65 pCur, &available
19d2a 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
19d2b 7a 44 61 74 61 20 3d 20 28 63 68 61 72 20 2a 29 zData = (char *)
19d2c 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 sqlite3BtreeData
19d2d 46 65 74 63 68 28 70 43 75 72 2c 20 26 61 76 61 Fetch(pCur, &ava
19d2e 69 6c 61 62 6c 65 29 3b 0a 20 20 7d 0a 20 20 61 ilable);. }. a
19d2f 73 73 65 72 74 28 20 7a 44 61 74 61 21 3d 30 20 ssert( zData!=0
19d30 29 3b 0a 0a 20 20 69 66 28 20 6f 66 66 73 65 74 );.. if( offset
19d31 2b 61 6d 74 3c 3d 61 76 61 69 6c 61 62 6c 65 20 +amt<=available
19d32 26 26 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 && (pMem->flags&
19d33 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 7b 0a 20 MEM_Dyn)==0 ){.
19d34 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
19d35 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a mRelease(pMem);.
19d36 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 26 7a pMem->z = &z
19d37 44 61 74 61 5b 6f 66 66 73 65 74 5d 3b 0a 20 20 Data[offset];.
19d38 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 pMem->flags =
19d39 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 45 70 68 MEM_Blob|MEM_Eph
19d3a 65 6d 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 em;. }else if(
19d3b 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d SQLITE_OK==(rc =
19d3c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 sqlite3VdbeMemG
19d3d 72 6f 77 28 70 4d 65 6d 2c 20 61 6d 74 2b 32 2c row(pMem, amt+2,
19d3e 20 30 29 29 20 29 7b 0a 20 20 20 20 70 4d 65 6d 0)) ){. pMem
19d3f 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c ->flags = MEM_Bl
19d40 6f 62 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 54 ob|MEM_Dyn|MEM_T
19d41 65 72 6d 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 erm;. pMem->e
19d42 6e 63 20 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d nc = 0;. pMem
19d43 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f ->type = SQLITE_
19d44 42 4c 4f 42 3b 0a 20 20 20 20 69 66 28 20 6b 65 BLOB;. if( ke
19d45 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 y ){. rc =
19d46 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 28 sqlite3BtreeKey(
19d47 70 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d pCur, offset, am
19d48 74 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 t, pMem->z);.
19d49 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 }else{. rc
19d4a 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 = sqlite3BtreeD
19d4b 61 74 61 28 70 43 75 72 2c 20 6f 66 66 73 65 74 ata(pCur, offset
19d4c 2c 20 61 6d 74 2c 20 70 4d 65 6d 2d 3e 7a 29 3b , amt, pMem->z);
19d4d 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d . }. pMem-
19d4e 3e 7a 5b 61 6d 74 5d 20 3d 20 30 3b 0a 20 20 20 >z[amt] = 0;.
19d4f 20 70 4d 65 6d 2d 3e 7a 5b 61 6d 74 2b 31 5d 20 pMem->z[amt+1]
19d50 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 72 63 21 = 0;. if( rc!
19d51 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
19d52 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
19d53 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b emRelease(pMem);
19d54 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 4d 65 . }. }. pMe
19d55 6d 2d 3e 6e 20 3d 20 61 6d 74 3b 0a 0a 20 20 72 m->n = amt;.. r
19d56 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 eturn rc;.}../*
19d57 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
19d58 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 only available
19d59 69 6e 74 65 72 6e 61 6c 6c 79 2c 20 69 74 20 69 internally, it i
19d5a 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68 s not part of th
19d5b 65 0a 2a 2a 20 65 78 74 65 72 6e 61 6c 20 41 50 e.** external AP
19d5c 49 2e 20 49 74 20 77 6f 72 6b 73 20 69 6e 20 61 I. It works in a
19d5d 20 73 69 6d 69 6c 61 72 20 77 61 79 20 74 6f 20 similar way to
19d5e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
19d5f 78 74 28 29 2c 0a 2a 2a 20 65 78 63 65 70 74 20 xt(),.** except
19d60 74 68 65 20 64 61 74 61 20 72 65 74 75 72 6e 65 the data returne
19d61 64 20 69 73 20 69 6e 20 74 68 65 20 65 6e 63 6f d is in the enco
19d62 64 69 6e 67 20 73 70 65 63 69 66 69 65 64 20 62 ding specified b
19d63 79 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 y the second.**
19d64 70 61 72 61 6d 65 74 65 72 2c 20 77 68 69 63 68 parameter, which
19d65 20 6d 75 73 74 20 62 65 20 6f 6e 65 20 6f 66 20 must be one of
19d66 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20 SQLITE_UTF16BE,
19d67 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 6f SQLITE_UTF16LE o
19d68 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 55 54 46 38 r.** SQLITE_UTF8
19d69 2e 0a 2a 2a 0a 2a 2a 20 28 32 30 30 36 2d 30 32 ..**.** (2006-02
19d6a 2d 31 36 3a 29 20 20 54 68 65 20 65 6e 63 20 76 -16:) The enc v
19d6b 61 6c 75 65 20 63 61 6e 20 62 65 20 6f 72 2d 65 alue can be or-e
19d6c 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f 55 54 d with SQLITE_UT
19d6d 46 31 36 5f 41 4c 49 47 4e 45 44 2e 0a 2a 2a 20 F16_ALIGNED..**
19d6e 49 66 20 74 68 61 74 20 69 73 20 74 68 65 20 63 If that is the c
19d6f 61 73 65 2c 20 74 68 65 6e 20 74 68 65 20 72 65 ase, then the re
19d70 73 75 6c 74 20 6d 75 73 74 20 62 65 20 61 6c 69 sult must be ali
19d71 67 6e 65 64 20 6f 6e 20 61 6e 20 65 76 65 6e 20 gned on an even
19d72 62 79 74 65 0a 2a 2a 20 62 6f 75 6e 64 61 72 79 byte.** boundary
19d73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
19d74 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a ATE const void *
19d75 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 sqlite3ValueText
19d76 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 20 (sqlite3_value*
19d77 70 56 61 6c 2c 20 75 38 20 65 6e 63 29 7b 0a 20 pVal, u8 enc){.
19d78 20 69 66 28 20 21 70 56 61 6c 20 29 20 72 65 74 if( !pVal ) ret
19d79 75 72 6e 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 urn 0;.. assert
19d7a 28 20 70 56 61 6c 2d 3e 64 62 3d 3d 30 20 7c 7c ( pVal->db==0 ||
19d7b 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
19d7c 65 6c 64 28 70 56 61 6c 2d 3e 64 62 2d 3e 6d 75 eld(pVal->db->mu
19d7d 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
19d7e 28 20 28 65 6e 63 26 33 29 3d 3d 28 65 6e 63 26 ( (enc&3)==(enc&
19d7f 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c ~SQLITE_UTF16_AL
19d80 49 47 4e 45 44 29 20 29 3b 0a 20 20 61 73 73 65 IGNED) );. asse
19d81 72 74 28 20 28 70 56 61 6c 2d 3e 66 6c 61 67 73 rt( (pVal->flags
19d82 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d & MEM_RowSet)==
19d83 30 20 29 3b 0a 0a 20 20 69 66 28 20 70 56 61 6c 0 );.. if( pVal
19d84 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c ->flags&MEM_Null
19d85 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 ){. return 0
19d86 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
19d87 28 4d 45 4d 5f 42 6c 6f 62 3e 3e 33 29 20 3d 3d (MEM_Blob>>3) ==
19d88 20 4d 45 4d 5f 53 74 72 20 29 3b 0a 20 20 70 56 MEM_Str );. pV
19d89 61 6c 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 70 56 al->flags |= (pV
19d8a 61 6c 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f al->flags & MEM_
19d8b 42 6c 6f 62 29 3e 3e 33 3b 0a 20 20 65 78 70 61 Blob)>>3;. expa
19d8c 6e 64 42 6c 6f 62 28 70 56 61 6c 29 3b 0a 20 20 ndBlob(pVal);.
19d8d 69 66 28 20 70 56 61 6c 2d 3e 66 6c 61 67 73 26 if( pVal->flags&
19d8e 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 73 MEM_Str ){. s
19d8f 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
19d90 45 6e 63 6f 64 69 6e 67 28 70 56 61 6c 2c 20 65 Encoding(pVal, e
19d91 6e 63 20 26 20 7e 53 51 4c 49 54 45 5f 55 54 46 nc & ~SQLITE_UTF
19d92 31 36 5f 41 4c 49 47 4e 45 44 29 3b 0a 20 20 20 16_ALIGNED);.
19d93 20 69 66 28 20 28 65 6e 63 20 26 20 53 51 4c 49 if( (enc & SQLI
19d94 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 TE_UTF16_ALIGNED
19d95 29 21 3d 30 20 26 26 20 31 3d 3d 28 31 26 53 51 )!=0 && 1==(1&SQ
19d96 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 LITE_PTR_TO_INT(
19d97 70 56 61 6c 2d 3e 7a 29 29 20 29 7b 0a 20 20 20 pVal->z)) ){.
19d98 20 20 20 61 73 73 65 72 74 28 20 28 70 56 61 6c assert( (pVal
19d99 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 45 ->flags & (MEM_E
19d9a 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 74 69 63 29 phem|MEM_Static)
19d9b 29 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 )!=0 );. if
19d9c 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d ( sqlite3VdbeMem
19d9d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70 56 MakeWriteable(pV
19d9e 61 6c 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 al)!=SQLITE_OK )
19d9f 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e {. return
19da0 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0;. }.
19da1 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 }. sqlite3Vdb
19da2 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 eMemNulTerminate
19da3 28 70 56 61 6c 29 3b 0a 20 20 7d 65 6c 73 65 7b (pVal);. }else{
19da4 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 56 . assert( (pV
19da5 61 6c 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 42 6c al->flags&MEM_Bl
19da6 6f 62 29 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 ob)==0 );. sq
19da7 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69 lite3VdbeMemStri
19da8 6e 67 69 66 79 28 70 56 61 6c 2c 20 65 6e 63 29 ngify(pVal, enc)
19da9 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 30 3d ;. assert( 0=
19daa 3d 28 31 26 53 51 4c 49 54 45 5f 50 54 52 5f 54 =(1&SQLITE_PTR_T
19dab 4f 5f 49 4e 54 28 70 56 61 6c 2d 3e 7a 29 29 20 O_INT(pVal->z))
19dac 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 );. }. assert(
19dad 70 56 61 6c 2d 3e 65 6e 63 3d 3d 28 65 6e 63 20 pVal->enc==(enc
19dae 26 20 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f & ~SQLITE_UTF16_
19daf 41 4c 49 47 4e 45 44 29 20 7c 7c 20 70 56 61 6c ALIGNED) || pVal
19db0 2d 3e 64 62 3d 3d 30 0a 20 20 20 20 20 20 20 20 ->db==0.
19db1 20 20 20 20 20 20 7c 7c 20 70 56 61 6c 2d 3e 64 || pVal->d
19db2 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
19db3 29 3b 0a 20 20 69 66 28 20 70 56 61 6c 2d 3e 65 );. if( pVal->e
19db4 6e 63 3d 3d 28 65 6e 63 20 26 20 7e 53 51 4c 49 nc==(enc & ~SQLI
19db5 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 TE_UTF16_ALIGNED
19db6 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ) ){. return
19db7 70 56 61 6c 2d 3e 7a 3b 0a 20 20 7d 65 6c 73 65 pVal->z;. }else
19db8 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a {. return 0;.
19db9 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 }.}../*.** Cre
19dba 61 74 65 20 61 20 6e 65 77 20 73 71 6c 69 74 65 ate a new sqlite
19dbb 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 2e 0a 3_value object..
19dbc 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
19dbd 45 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 E sqlite3_value
19dbe 2a 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 *sqlite3ValueNew
19dbf 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 (sqlite3 *db){.
19dc0 20 4d 65 6d 20 2a 70 20 3d 20 73 71 6c 69 74 65 Mem *p = sqlite
19dc1 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 3DbMallocZero(db
19dc2 2c 20 73 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 20 , sizeof(*p));.
19dc3 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70 2d if( p ){. p-
19dc4 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c >flags = MEM_Nul
19dc5 6c 3b 0a 20 20 20 20 70 2d 3e 74 79 70 65 20 3d l;. p->type =
19dc6 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a 20 20 SQLITE_NULL;.
19dc7 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 p->db = db;.
19dc8 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a }. return p;.}.
19dc9 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 ./*.** Create a
19dca 6e 65 77 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 new sqlite3_valu
19dcb 65 20 6f 62 6a 65 63 74 2c 20 63 6f 6e 74 61 69 e object, contai
19dcc 6e 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 6f ning the value o
19dcd 66 20 70 45 78 70 72 2e 0a 2a 2a 0a 2a 2a 20 54 f pExpr..**.** T
19dce 68 69 73 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 66 his only works f
19dcf 6f 72 20 76 65 72 79 20 73 69 6d 70 6c 65 20 65 or very simple e
19dd0 78 70 72 65 73 73 69 6f 6e 73 20 74 68 61 74 20 xpressions that
19dd1 63 6f 6e 73 69 73 74 20 6f 66 20 6f 6e 65 20 63 consist of one c
19dd2 6f 6e 73 74 61 6e 74 0a 2a 2a 20 74 6f 6b 65 6e onstant.** token
19dd3 20 28 69 2e 65 2e 20 22 35 22 2c 20 22 35 2e 31 (i.e. "5", "5.1
19dd4 22 2c 20 22 27 61 20 73 74 72 69 6e 67 27 22 29 ", "'a string'")
19dd5 2e 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 . If the express
19dd6 69 6f 6e 20 63 61 6e 0a 2a 2a 20 62 65 20 63 6f ion can.** be co
19dd7 6e 76 65 72 74 65 64 20 64 69 72 65 63 74 6c 79 nverted directly
19dd8 20 69 6e 74 6f 20 61 20 76 61 6c 75 65 2c 20 74 into a value, t
19dd9 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 69 73 hen the value is
19dda 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 0a 2a allocated and.*
19ddb 2a 20 61 20 70 6f 69 6e 74 65 72 20 77 72 69 74 * a pointer writ
19ddc 74 65 6e 20 74 6f 20 2a 70 70 56 61 6c 2e 20 54 ten to *ppVal. T
19ddd 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65 73 he caller is res
19dde 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 64 65 61 ponsible for dea
19ddf 6c 6c 6f 63 61 74 69 6e 67 0a 2a 2a 20 74 68 65 llocating.** the
19de0 20 76 61 6c 75 65 20 62 79 20 70 61 73 73 69 6e value by passin
19de1 67 20 69 74 20 74 6f 20 73 71 6c 69 74 65 33 56 g it to sqlite3V
19de2 61 6c 75 65 46 72 65 65 28 29 20 6c 61 74 65 72 alueFree() later
19de3 20 6f 6e 2e 20 49 66 20 74 68 65 20 65 78 70 72 on. If the expr
19de4 65 73 73 69 6f 6e 0a 2a 2a 20 63 61 6e 6e 6f 74 ession.** cannot
19de5 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 74 6f be converted to
19de6 20 61 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 2a a value, then *
19de7 70 70 56 61 6c 20 69 73 20 73 65 74 20 74 6f 20 ppVal is set to
19de8 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f NULL..*/.SQLITE_
19de9 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
19dea 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 te3ValueFromExpr
19deb 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c (. sqlite3 *db,
19dec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
19ded 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f The database co
19dee 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 nnection */. Ex
19def 70 72 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20 pr *pExpr,
19df0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 /* The e
19df1 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 65 76 61 xpression to eva
19df2 6c 75 61 74 65 20 2a 2f 0a 20 20 75 38 20 65 6e luate */. u8 en
19df3 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c,
19df4 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67 /* Encoding
19df5 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 75 38 20 to use */. u8
19df6 61 66 66 69 6e 69 74 79 2c 20 20 20 20 20 20 20 affinity,
19df7 20 20 20 20 20 20 20 2f 2a 20 41 66 66 69 6e 69 /* Affini
19df8 74 79 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 73 ty to use */. s
19df9 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70 qlite3_value **p
19dfa 70 56 61 6c 20 20 20 20 20 2f 2a 20 57 72 69 74 pVal /* Writ
19dfb 65 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 20 e the new value
19dfc 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 here */.){. int
19dfd 20 6f 70 3b 0a 20 20 63 68 61 72 20 2a 7a 56 61 op;. char *zVa
19dfe 6c 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 l = 0;. sqlite3
19dff 5f 76 61 6c 75 65 20 2a 70 56 61 6c 20 3d 20 30 _value *pVal = 0
19e00 3b 0a 0a 20 20 69 66 28 20 21 70 45 78 70 72 20 ;.. if( !pExpr
19e01 29 7b 0a 20 20 20 20 2a 70 70 56 61 6c 20 3d 20 ){. *ppVal =
19e02 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 0;. return SQ
19e03 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 6f LITE_OK;. }. o
19e04 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 p = pExpr->op;.
19e05 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 if( op==TK_REGI
19e06 53 54 45 52 20 29 7b 0a 20 20 20 20 6f 70 20 3d STER ){. op =
19e07 20 70 45 78 70 72 2d 3e 6f 70 32 3b 0a 20 20 7d pExpr->op2;. }
19e08 0a 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 53 .. if( op==TK_S
19e09 54 52 49 4e 47 20 7c 7c 20 6f 70 3d 3d 54 4b 5f TRING || op==TK_
19e0a 46 4c 4f 41 54 20 7c 7c 20 6f 70 3d 3d 54 4b 5f FLOAT || op==TK_
19e0b 49 4e 54 45 47 45 52 20 29 7b 0a 20 20 20 20 70 INTEGER ){. p
19e0c 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c Val = sqlite3Val
19e0d 75 65 4e 65 77 28 64 62 29 3b 0a 20 20 20 20 69 ueNew(db);. i
19e0e 66 28 20 70 56 61 6c 3d 3d 30 20 29 20 67 6f 74 f( pVal==0 ) got
19e0f 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 69 66 o no_mem;. if
19e10 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 ( ExprHasPropert
19e11 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 y(pExpr, EP_IntV
19e12 61 6c 75 65 29 20 29 7b 0a 20 20 20 20 20 20 73 alue) ){. s
19e13 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
19e14 49 6e 74 36 34 28 70 56 61 6c 2c 20 28 69 36 34 Int64(pVal, (i64
19e15 29 70 45 78 70 72 2d 3e 75 2e 69 56 61 6c 75 65 )pExpr->u.iValue
19e16 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
19e17 20 20 20 20 7a 56 61 6c 20 3d 20 73 71 6c 69 74 zVal = sqlit
19e18 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 e3DbStrDup(db, p
19e19 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b Expr->u.zToken);
19e1a 0a 20 20 20 20 20 20 69 66 28 20 7a 56 61 6c 3d . if( zVal=
19e1b 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d =0 ) goto no_mem
19e1c 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
19e1d 61 6c 75 65 53 65 74 53 74 72 28 70 56 61 6c 2c alueSetStr(pVal,
19e1e 20 2d 31 2c 20 7a 56 61 6c 2c 20 53 51 4c 49 54 -1, zVal, SQLIT
19e1f 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 44 E_UTF8, SQLITE_D
19e20 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 69 YNAMIC);. i
19e21 66 28 20 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 f( op==TK_FLOAT
19e22 29 20 70 56 61 6c 2d 3e 74 79 70 65 20 3d 20 53 ) pVal->type = S
19e23 51 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 20 20 QLITE_FLOAT;.
19e24 20 7d 0a 20 20 20 20 69 66 28 20 28 6f 70 3d 3d }. if( (op==
19e25 54 4b 5f 49 4e 54 45 47 45 52 20 7c 7c 20 6f 70 TK_INTEGER || op
19e26 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 20 26 26 20 ==TK_FLOAT ) &&
19e27 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 affinity==SQLITE
19e28 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 _AFF_NONE ){.
19e29 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 41 sqlite3ValueA
19e2a 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 56 61 pplyAffinity(pVa
19e2b 6c 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 l, SQLITE_AFF_NU
19e2c 4d 45 52 49 43 2c 20 53 51 4c 49 54 45 5f 55 54 MERIC, SQLITE_UT
19e2d 46 38 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a F8);. }else{.
19e2e 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c sqlite3Val
19e2f 75 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28 ueApplyAffinity(
19e30 70 56 61 6c 2c 20 61 66 66 69 6e 69 74 79 2c 20 pVal, affinity,
19e31 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 SQLITE_UTF8);.
19e32 20 20 7d 0a 20 20 20 20 69 66 28 20 65 6e 63 21 }. if( enc!
19e33 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a =SQLITE_UTF8 ){.
19e34 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
19e35 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 eChangeEncoding(
19e36 70 56 61 6c 2c 20 65 6e 63 29 3b 0a 20 20 20 20 pVal, enc);.
19e37 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 }. }else if( op
19e38 3d 3d 54 4b 5f 55 4d 49 4e 55 53 20 29 20 7b 0a ==TK_UMINUS ) {.
19e39 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f if( SQLITE_O
19e3a 4b 3d 3d 73 71 6c 69 74 65 33 56 61 6c 75 65 46 K==sqlite3ValueF
19e3b 72 6f 6d 45 78 70 72 28 64 62 2c 70 45 78 70 72 romExpr(db,pExpr
19e3c 2d 3e 70 4c 65 66 74 2c 65 6e 63 2c 61 66 66 69 ->pLeft,enc,affi
19e3d 6e 69 74 79 2c 26 70 56 61 6c 29 20 29 7b 0a 20 nity,&pVal) ){.
19e3e 20 20 20 20 20 70 56 61 6c 2d 3e 75 2e 69 20 3d pVal->u.i =
19e3f 20 2d 31 20 2a 20 70 56 61 6c 2d 3e 75 2e 69 3b -1 * pVal->u.i;
19e40 0a 20 20 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c . /* (doubl
19e41 65 29 2d 31 20 49 6e 20 63 61 73 65 20 6f 66 20 e)-1 In case of
19e42 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 SQLITE_OMIT_FLOA
19e43 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f TING_POINT... */
19e44 0a 20 20 20 20 20 20 70 56 61 6c 2d 3e 72 20 3d . pVal->r =
19e45 20 28 64 6f 75 62 6c 65 29 2d 31 20 2a 20 70 56 (double)-1 * pV
19e46 61 6c 2d 3e 72 3b 0a 20 20 20 20 7d 0a 20 20 7d al->r;. }. }
19e47 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
19e48 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 OMIT_BLOB_LITERA
19e49 4c 0a 20 20 65 6c 73 65 20 69 66 28 20 6f 70 3d L. else if( op=
19e4a 3d 54 4b 5f 42 4c 4f 42 20 29 7b 0a 20 20 20 20 =TK_BLOB ){.
19e4b 69 6e 74 20 6e 56 61 6c 3b 0a 20 20 20 20 61 73 int nVal;. as
19e4c 73 65 72 74 28 20 70 45 78 70 72 2d 3e 75 2e 7a sert( pExpr->u.z
19e4d 54 6f 6b 65 6e 5b 30 5d 3d 3d 27 78 27 20 7c 7c Token[0]=='x' ||
19e4e 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e pExpr->u.zToken
19e4f 5b 30 5d 3d 3d 27 58 27 20 29 3b 0a 20 20 20 20 [0]=='X' );.
19e50 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 75 assert( pExpr->u
19e51 2e 7a 54 6f 6b 65 6e 5b 31 5d 3d 3d 27 5c 27 27 .zToken[1]=='\''
19e52 20 29 3b 0a 20 20 20 20 70 56 61 6c 20 3d 20 73 );. pVal = s
19e53 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 64 qlite3ValueNew(d
19e54 62 29 3b 0a 20 20 20 20 69 66 28 20 21 70 56 61 b);. if( !pVa
19e55 6c 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b l ) goto no_mem;
19e56 0a 20 20 20 20 7a 56 61 6c 20 3d 20 26 70 45 78 . zVal = &pEx
19e57 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 32 5d 3b pr->u.zToken[2];
19e58 0a 20 20 20 20 6e 56 61 6c 20 3d 20 73 71 6c 69 . nVal = sqli
19e59 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 56 61 6c te3Strlen30(zVal
19e5a 29 2d 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28 )-1;. assert(
19e5b 20 7a 56 61 6c 5b 6e 56 61 6c 5d 3d 3d 27 5c 27 zVal[nVal]=='\'
19e5c 27 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 ' );. sqlite3
19e5d 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70 56 VdbeMemSetStr(pV
19e5e 61 6c 2c 20 73 71 6c 69 74 65 33 48 65 78 54 6f al, sqlite3HexTo
19e5f 42 6c 6f 62 28 64 62 2c 20 7a 56 61 6c 2c 20 6e Blob(db, zVal, n
19e60 56 61 6c 29 2c 20 6e 56 61 6c 2f 32 2c 0a 20 20 Val), nVal/2,.
19e61 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19e62 20 20 20 20 20 20 20 30 2c 20 53 51 4c 49 54 45 0, SQLITE
19e63 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 23 _DYNAMIC);. }.#
19e64 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 70 56 61 endif.. if( pVa
19e65 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 l ){. sqlite3
19e66 56 64 62 65 4d 65 6d 53 74 6f 72 65 54 79 70 65 VdbeMemStoreType
19e67 28 70 56 61 6c 29 3b 0a 20 20 7d 0a 20 20 2a 70 (pVal);. }. *p
19e68 70 56 61 6c 20 3d 20 70 56 61 6c 3b 0a 20 20 72 pVal = pVal;. r
19e69 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
19e6a 0a 0a 6e 6f 5f 6d 65 6d 3a 0a 20 20 64 62 2d 3e ..no_mem:. db->
19e6b 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 mallocFailed = 1
19e6c 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 ;. sqlite3DbFre
19e6d 65 28 64 62 2c 20 7a 56 61 6c 29 3b 0a 20 20 73 e(db, zVal);. s
19e6e 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 qlite3ValueFree(
19e6f 70 56 61 6c 29 3b 0a 20 20 2a 70 70 56 61 6c 20 pVal);. *ppVal
19e70 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 = 0;. return SQ
19e71 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 7d 0a 0a 2f LITE_NOMEM;.}../
19e72 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 *.** Change the
19e73 73 74 72 69 6e 67 20 76 61 6c 75 65 20 6f 66 20 string value of
19e74 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 an sqlite3_value
19e75 20 6f 62 6a 65 63 74 0a 2a 2f 0a 53 51 4c 49 54 object.*/.SQLIT
19e76 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
19e77 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 qlite3ValueSetSt
19e78 72 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c r(. sqlite3_val
19e79 75 65 20 2a 76 2c 20 20 20 20 20 2f 2a 20 56 61 ue *v, /* Va
19e7a 6c 75 65 20 74 6f 20 62 65 20 73 65 74 20 2a 2f lue to be set */
19e7b 0a 20 20 69 6e 74 20 6e 2c 20 20 20 20 20 20 20 . int n,
19e7c 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 /* Leng
19e7d 74 68 20 6f 66 20 73 74 72 69 6e 67 20 7a 20 2a th of string z *
19e7e 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a /. const void *
19e7f 7a 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 z, /* Tex
19e80 74 20 6f 66 20 74 68 65 20 6e 65 77 20 73 74 72 t of the new str
19e81 69 6e 67 20 2a 2f 0a 20 20 75 38 20 65 6e 63 2c ing */. u8 enc,
19e82 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
19e83 2a 20 45 6e 63 6f 64 69 6e 67 20 74 6f 20 75 73 * Encoding to us
19e84 65 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 e */. void (*xD
19e85 65 6c 29 28 76 6f 69 64 2a 29 20 20 20 2f 2a 20 el)(void*) /*
19e86 44 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20 74 Destructor for t
19e87 68 65 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a he string */.){.
19e88 20 20 69 66 28 20 76 20 29 20 73 71 6c 69 74 65 if( v ) sqlite
19e89 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 28 3VdbeMemSetStr((
19e8a 4d 65 6d 20 2a 29 76 2c 20 7a 2c 20 6e 2c 20 65 Mem *)v, z, n, e
19e8b 6e 63 2c 20 78 44 65 6c 29 3b 0a 7d 0a 0a 2f 2a nc, xDel);.}../*
19e8c 0a 2a 2a 20 46 72 65 65 20 61 6e 20 73 71 6c 69 .** Free an sqli
19e8d 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 te3_value object
19e8e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
19e8f 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
19e90 61 6c 75 65 46 72 65 65 28 73 71 6c 69 74 65 33 alueFree(sqlite3
19e91 5f 76 61 6c 75 65 20 2a 76 29 7b 0a 20 20 69 66 _value *v){. if
19e92 28 20 21 76 20 29 20 72 65 74 75 72 6e 3b 0a 20 ( !v ) return;.
19e93 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
19e94 65 6c 65 61 73 65 28 28 4d 65 6d 20 2a 29 76 29 elease((Mem *)v)
19e95 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 ;. sqlite3DbFre
19e96 65 28 28 28 4d 65 6d 2a 29 76 29 2d 3e 64 62 2c e(((Mem*)v)->db,
19e97 20 76 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 v);.}../*.** Re
19e98 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 turn the number
19e99 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 of bytes in the
19e9a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 sqlite3_value ob
19e9b 6a 65 63 74 20 61 73 73 75 6d 69 6e 67 0a 2a 2a ject assuming.**
19e9c 20 74 68 61 74 20 69 74 20 75 73 65 73 20 74 68 that it uses th
19e9d 65 20 65 6e 63 6f 64 69 6e 67 20 22 65 6e 63 22 e encoding "enc"
19e9e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
19e9f 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 TE int sqlite3Va
19ea0 6c 75 65 42 79 74 65 73 28 73 71 6c 69 74 65 33 lueBytes(sqlite3
19ea1 5f 76 61 6c 75 65 20 2a 70 56 61 6c 2c 20 75 38 _value *pVal, u8
19ea2 20 65 6e 63 29 7b 0a 20 20 4d 65 6d 20 2a 70 20 enc){. Mem *p
19ea3 3d 20 28 4d 65 6d 2a 29 70 56 61 6c 3b 0a 20 20 = (Mem*)pVal;.
19ea4 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 if( (p->flags &
19ea5 4d 45 4d 5f 42 6c 6f 62 29 21 3d 30 20 7c 7c 20 MEM_Blob)!=0 ||
19ea6 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 sqlite3ValueText
19ea7 28 70 56 61 6c 2c 20 65 6e 63 29 20 29 7b 0a 20 (pVal, enc) ){.
19ea8 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 if( p->flags
19ea9 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 & MEM_Zero ){.
19eaa 20 20 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 20 return p->n
19eab 2b 20 70 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 + p->u.nZero;.
19eac 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 }else{. r
19ead 65 74 75 72 6e 20 70 2d 3e 6e 3b 0a 20 20 20 20 eturn p->n;.
19eae 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 }. }. return 0
19eaf 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ;.}../**********
19eb0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 **** End of vdbe
19eb1 6d 65 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a mem.c **********
19eb2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19eb3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19eb4 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
19eb5 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
19eb6 76 64 62 65 61 75 78 2e 63 20 2a 2a 2a 2a 2a 2a vdbeaux.c ******
19eb7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19eb8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19eb9 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 ***/./*.** 2003
19eba 53 65 70 74 65 6d 62 65 72 20 36 0a 2a 2a 0a 2a September 6.**.*
19ebb 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
19ebc 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
19ebd 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
19ebe 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
19ebf 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
19ec0 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
19ec1 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
19ec2 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
19ec3 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
19ec4 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
19ec5 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
19ec6 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
19ec7 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
19ec8 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
19ec9 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
19eca 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
19ecb 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
19ecc 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
19ecd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19ece 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19ecf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 **********.** Th
19ed1 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
19ed2 20 63 6f 64 65 20 75 73 65 64 20 66 6f 72 20 63 code used for c
19ed3 72 65 61 74 69 6e 67 2c 20 64 65 73 74 72 6f 79 reating, destroy
19ed4 69 6e 67 2c 20 61 6e 64 20 70 6f 70 75 6c 61 74 ing, and populat
19ed5 69 6e 67 0a 2a 2a 20 61 20 56 44 42 45 20 28 6f ing.** a VDBE (o
19ed6 72 20 61 6e 20 22 73 71 6c 69 74 65 33 5f 73 74 r an "sqlite3_st
19ed7 6d 74 22 20 61 73 20 69 74 20 69 73 20 6b 6e 6f mt" as it is kno
19ed8 77 6e 20 74 6f 20 74 68 65 20 6f 75 74 73 69 64 wn to the outsid
19ed9 65 20 77 6f 72 6c 64 2e 29 20 20 50 72 69 6f 72 e world.) Prior
19eda 0a 2a 2a 20 74 6f 20 76 65 72 73 69 6f 6e 20 32 .** to version 2
19edb 2e 38 2e 37 2c 20 61 6c 6c 20 74 68 69 73 20 63 .8.7, all this c
19edc 6f 64 65 20 77 61 73 20 63 6f 6d 62 69 6e 65 64 ode was combined
19edd 20 69 6e 74 6f 20 74 68 65 20 76 64 62 65 2e 63 into the vdbe.c
19ede 20 73 6f 75 72 63 65 20 66 69 6c 65 2e 0a 2a 2a source file..**
19edf 20 42 75 74 20 74 68 61 74 20 66 69 6c 65 20 77 But that file w
19ee0 61 73 20 67 65 74 74 69 6e 67 20 74 6f 6f 20 62 as getting too b
19ee1 69 67 20 73 6f 20 74 68 69 73 20 73 75 62 72 6f ig so this subro
19ee2 75 74 69 6e 65 73 20 77 65 72 65 20 73 70 6c 69 utines were spli
19ee3 74 20 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 t out..**.** $Id
19ee4 3a 20 76 64 62 65 61 75 78 2e 63 2c 76 20 31 2e : vdbeaux.c,v 1.
19ee5 34 38 30 20 32 30 30 39 2f 30 38 2f 30 38 20 31 480 2009/08/08 1
19ee6 38 3a 30 31 3a 30 38 20 64 72 68 20 45 78 70 20 8:01:08 drh Exp
19ee7 24 0a 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 57 68 $.*/..../*.** Wh
19ee8 65 6e 20 64 65 62 75 67 67 69 6e 67 20 74 68 65 en debugging the
19ee9 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 20 code generator
19eea 69 6e 20 61 20 73 79 6d 62 6f 6c 69 63 20 64 65 in a symbolic de
19eeb 62 75 67 67 65 72 2c 20 6f 6e 65 20 63 61 6e 0a bugger, one can.
19eec 2a 2a 20 73 65 74 20 74 68 65 20 73 71 6c 69 74 ** set the sqlit
19eed 65 33 56 64 62 65 41 64 64 6f 70 54 72 61 63 65 e3VdbeAddopTrace
19eee 20 74 6f 20 31 20 61 6e 64 20 61 6c 6c 20 6f 70 to 1 and all op
19eef 63 6f 64 65 73 20 77 69 6c 6c 20 62 65 20 70 72 codes will be pr
19ef0 69 6e 74 65 64 0a 2a 2a 20 61 73 20 74 68 65 79 inted.** as they
19ef1 20 61 72 65 20 61 64 64 65 64 20 74 6f 20 74 68 are added to th
19ef2 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 73 74 e instruction st
19ef3 72 65 61 6d 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 ream..*/.#ifdef
19ef4 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c SQLITE_DEBUG.SQL
19ef5 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
19ef6 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 6f 70 sqlite3VdbeAddop
19ef7 54 72 61 63 65 20 3d 20 30 3b 0a 23 65 6e 64 69 Trace = 0;.#endi
19ef8 66 0a 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 f.../*.** Create
19ef9 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c 20 64 a new virtual d
19efa 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65 2e 0a atabase engine..
19efb 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
19efc 45 20 56 64 62 65 20 2a 73 71 6c 69 74 65 33 56 E Vdbe *sqlite3V
19efd 64 62 65 43 72 65 61 74 65 28 73 71 6c 69 74 65 dbeCreate(sqlite
19efe 33 20 2a 64 62 29 7b 0a 20 20 56 64 62 65 20 2a 3 *db){. Vdbe *
19eff 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 p;. p = sqlite3
19f00 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c DbMallocZero(db,
19f01 20 73 69 7a 65 6f 66 28 56 64 62 65 29 20 29 3b sizeof(Vdbe) );
19f02 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 . if( p==0 ) re
19f03 74 75 72 6e 20 30 3b 0a 20 20 70 2d 3e 64 62 20 turn 0;. p->db
19f04 3d 20 64 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e = db;. if( db->
19f05 70 56 64 62 65 20 29 7b 0a 20 20 20 20 64 62 2d pVdbe ){. db-
19f06 3e 70 56 64 62 65 2d 3e 70 50 72 65 76 20 3d 20 >pVdbe->pPrev =
19f07 70 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4e 65 78 p;. }. p->pNex
19f08 74 20 3d 20 64 62 2d 3e 70 56 64 62 65 3b 0a 20 t = db->pVdbe;.
19f09 20 70 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20 p->pPrev = 0;.
19f0a 20 64 62 2d 3e 70 56 64 62 65 20 3d 20 70 3b 0a db->pVdbe = p;.
19f0b 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 p->magic = VDB
19f0c 45 5f 4d 41 47 49 43 5f 49 4e 49 54 3b 0a 20 20 E_MAGIC_INIT;.
19f0d 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a return p;.}../*.
19f0e 2a 2a 20 52 65 6d 65 6d 62 65 72 20 74 68 65 20 ** Remember the
19f0f 53 51 4c 20 73 74 72 69 6e 67 20 66 6f 72 20 61 SQL string for a
19f10 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d prepared statem
19f11 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ent..*/.SQLITE_P
19f12 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
19f13 74 65 33 56 64 62 65 53 65 74 53 71 6c 28 56 64 te3VdbeSetSql(Vd
19f14 62 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 be *p, const cha
19f15 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 r *z, int n, int
19f16 20 69 73 50 72 65 70 61 72 65 56 32 29 7b 0a 20 isPrepareV2){.
19f17 20 61 73 73 65 72 74 28 20 69 73 50 72 65 70 61 assert( isPrepa
19f18 72 65 56 32 3d 3d 31 20 7c 7c 20 69 73 50 72 65 reV2==1 || isPre
19f19 70 61 72 65 56 32 3d 3d 30 20 29 3b 0a 20 20 69 pareV2==0 );. i
19f1a 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e f( p==0 ) return
19f1b 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
19f1c 4f 4d 49 54 5f 54 52 41 43 45 0a 20 20 69 66 28 OMIT_TRACE. if(
19f1d 20 21 69 73 50 72 65 70 61 72 65 56 32 20 29 20 !isPrepareV2 )
19f1e 72 65 74 75 72 6e 3b 0a 23 65 6e 64 69 66 0a 20 return;.#endif.
19f1f 20 61 73 73 65 72 74 28 20 70 2d 3e 7a 53 71 6c assert( p->zSql
19f20 3d 3d 30 20 29 3b 0a 20 20 70 2d 3e 7a 53 71 6c ==0 );. p->zSql
19f21 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e = sqlite3DbStrN
19f22 44 75 70 28 70 2d 3e 64 62 2c 20 7a 2c 20 6e 29 Dup(p->db, z, n)
19f23 3b 0a 20 20 70 2d 3e 69 73 50 72 65 70 61 72 65 ;. p->isPrepare
19f24 56 32 20 3d 20 28 75 38 29 69 73 50 72 65 70 61 V2 = (u8)isPrepa
19f25 72 65 56 32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 reV2;.}../*.** R
19f26 65 74 75 72 6e 20 74 68 65 20 53 51 4c 20 61 73 eturn the SQL as
19f27 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 sociated with a
19f28 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
19f29 6e 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 nt.*/.SQLITE_API
19f2a 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c const char *sql
19f2b 69 74 65 33 5f 73 71 6c 28 73 71 6c 69 74 65 33 ite3_sql(sqlite3
19f2c 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 _stmt *pStmt){.
19f2d 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 Vdbe *p = (Vdbe
19f2e 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72 65 74 75 *)pStmt;. retu
19f2f 72 6e 20 28 70 2d 3e 69 73 50 72 65 70 61 72 65 rn (p->isPrepare
19f30 56 32 20 3f 20 70 2d 3e 7a 53 71 6c 20 3a 20 30 V2 ? p->zSql : 0
19f31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 77 61 70 );.}../*.** Swap
19f32 20 61 6c 6c 20 63 6f 6e 74 65 6e 74 20 62 65 74 all content bet
19f33 77 65 65 6e 20 74 77 6f 20 56 44 42 45 20 73 74 ween two VDBE st
19f34 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a 53 51 4c ructures..*/.SQL
19f35 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
19f36 20 73 71 6c 69 74 65 33 56 64 62 65 53 77 61 70 sqlite3VdbeSwap
19f37 28 56 64 62 65 20 2a 70 41 2c 20 56 64 62 65 20 (Vdbe *pA, Vdbe
19f38 2a 70 42 29 7b 0a 20 20 56 64 62 65 20 74 6d 70 *pB){. Vdbe tmp
19f39 2c 20 2a 70 54 6d 70 3b 0a 20 20 63 68 61 72 20 , *pTmp;. char
19f3a 2a 7a 54 6d 70 3b 0a 20 20 74 6d 70 20 3d 20 2a *zTmp;. tmp = *
19f3b 70 41 3b 0a 20 20 2a 70 41 20 3d 20 2a 70 42 3b pA;. *pA = *pB;
19f3c 0a 20 20 2a 70 42 20 3d 20 74 6d 70 3b 0a 20 20 . *pB = tmp;.
19f3d 70 54 6d 70 20 3d 20 70 41 2d 3e 70 4e 65 78 74 pTmp = pA->pNext
19f3e 3b 0a 20 20 70 41 2d 3e 70 4e 65 78 74 20 3d 20 ;. pA->pNext =
19f3f 70 42 2d 3e 70 4e 65 78 74 3b 0a 20 20 70 42 2d pB->pNext;. pB-
19f40 3e 70 4e 65 78 74 20 3d 20 70 54 6d 70 3b 0a 20 >pNext = pTmp;.
19f41 20 70 54 6d 70 20 3d 20 70 41 2d 3e 70 50 72 65 pTmp = pA->pPre
19f42 76 3b 0a 20 20 70 41 2d 3e 70 50 72 65 76 20 3d v;. pA->pPrev =
19f43 20 70 42 2d 3e 70 50 72 65 76 3b 0a 20 20 70 42 pB->pPrev;. pB
19f44 2d 3e 70 50 72 65 76 20 3d 20 70 54 6d 70 3b 0a ->pPrev = pTmp;.
19f45 20 20 7a 54 6d 70 20 3d 20 70 41 2d 3e 7a 53 71 zTmp = pA->zSq
19f46 6c 3b 0a 20 20 70 41 2d 3e 7a 53 71 6c 20 3d 20 l;. pA->zSql =
19f47 70 42 2d 3e 7a 53 71 6c 3b 0a 20 20 70 42 2d 3e pB->zSql;. pB->
19f48 7a 53 71 6c 20 3d 20 7a 54 6d 70 3b 0a 20 20 70 zSql = zTmp;. p
19f49 42 2d 3e 69 73 50 72 65 70 61 72 65 56 32 20 3d B->isPrepareV2 =
19f4a 20 70 41 2d 3e 69 73 50 72 65 70 61 72 65 56 32 pA->isPrepareV2
19f4b 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 ;.}..#ifdef SQLI
19f4c 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 TE_DEBUG./*.** T
19f4d 75 72 6e 20 74 72 61 63 69 6e 67 20 6f 6e 20 6f urn tracing on o
19f4e 72 20 6f 66 66 0a 2a 2f 0a 53 51 4c 49 54 45 5f r off.*/.SQLITE_
19f4f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
19f50 69 74 65 33 56 64 62 65 54 72 61 63 65 28 56 64 ite3VdbeTrace(Vd
19f51 62 65 20 2a 70 2c 20 46 49 4c 45 20 2a 74 72 61 be *p, FILE *tra
19f52 63 65 29 7b 0a 20 20 70 2d 3e 74 72 61 63 65 20 ce){. p->trace
19f53 3d 20 74 72 61 63 65 3b 0a 7d 0a 23 65 6e 64 69 = trace;.}.#endi
19f54 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 69 7a 65 20 f../*.** Resize
19f55 74 68 65 20 56 64 62 65 2e 61 4f 70 20 61 72 72 the Vdbe.aOp arr
19f56 61 79 20 73 6f 20 74 68 61 74 20 69 74 20 69 73 ay so that it is
19f57 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 6f 70 at least one op
19f58 20 6c 61 72 67 65 72 20 74 68 61 6e 20 0a 2a 2a larger than .**
19f59 20 69 74 20 77 61 73 2e 0a 2a 2a 0a 2a 2a 20 49 it was..**.** I
19f5a 66 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d 65 6d 6f f an out-of-memo
19f5b 72 79 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 ry error occurs
19f5c 77 68 69 6c 65 20 72 65 73 69 7a 69 6e 67 20 74 while resizing t
19f5d 68 65 20 61 72 72 61 79 2c 20 72 65 74 75 72 6e he array, return
19f5e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d .** SQLITE_NOMEM
19f5f 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 56 . In this case V
19f60 64 62 65 2e 61 4f 70 20 61 6e 64 20 56 64 62 65 dbe.aOp and Vdbe
19f61 2e 6e 4f 70 41 6c 6c 6f 63 20 72 65 6d 61 69 6e .nOpAlloc remain
19f62 20 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64 20 28 .** unchanged (
19f63 74 68 69 73 20 69 73 20 73 6f 20 74 68 61 74 20 this is so that
19f64 61 6e 79 20 6f 70 63 6f 64 65 73 20 61 6c 72 65 any opcodes alre
19f65 61 64 79 20 61 6c 6c 6f 63 61 74 65 64 20 63 61 ady allocated ca
19f66 6e 20 62 65 20 0a 2a 2a 20 63 6f 72 72 65 63 74 n be .** correct
19f67 6c 79 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 61 ly deallocated a
19f68 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20 72 65 long with the re
19f69 73 74 20 6f 66 20 74 68 65 20 56 64 62 65 29 2e st of the Vdbe).
19f6a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 .*/.static int g
19f6b 72 6f 77 4f 70 41 72 72 61 79 28 56 64 62 65 20 rowOpArray(Vdbe
19f6c 2a 70 29 7b 0a 20 20 56 64 62 65 4f 70 20 2a 70 *p){. VdbeOp *p
19f6d 4e 65 77 3b 0a 20 20 69 6e 74 20 6e 4e 65 77 20 New;. int nNew
19f6e 3d 20 28 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 3f = (p->nOpAlloc ?
19f6f 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 2a 32 20 3a p->nOpAlloc*2 :
19f70 20 28 69 6e 74 29 28 31 30 32 34 2f 73 69 7a 65 (int)(1024/size
19f71 6f 66 28 4f 70 29 29 29 3b 0a 20 20 70 4e 65 77 of(Op)));. pNew
19f72 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c = sqlite3DbReal
19f73 6c 6f 63 28 70 2d 3e 64 62 2c 20 70 2d 3e 61 4f loc(p->db, p->aO
19f74 70 2c 20 6e 4e 65 77 2a 73 69 7a 65 6f 66 28 4f p, nNew*sizeof(O
19f75 70 29 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 20 p));. if( pNew
19f76 29 7b 0a 20 20 20 20 70 2d 3e 6e 4f 70 41 6c 6c ){. p->nOpAll
19f77 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 oc = sqlite3DbMa
19f78 6c 6c 6f 63 53 69 7a 65 28 70 2d 3e 64 62 2c 20 llocSize(p->db,
19f79 70 4e 65 77 29 2f 73 69 7a 65 6f 66 28 4f 70 29 pNew)/sizeof(Op)
19f7a 3b 0a 20 20 20 20 70 2d 3e 61 4f 70 20 3d 20 70 ;. p->aOp = p
19f7b 4e 65 77 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 New;. }. retur
19f7c 6e 20 28 70 4e 65 77 20 3f 20 53 51 4c 49 54 45 n (pNew ? SQLITE
19f7d 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 4e 4f 4d _OK : SQLITE_NOM
19f7e 45 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 EM);.}../*.** Ad
19f7f 64 20 61 20 6e 65 77 20 69 6e 73 74 72 75 63 74 d a new instruct
19f80 69 6f 6e 20 74 6f 20 74 68 65 20 6c 69 73 74 20 ion to the list
19f81 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 of instructions
19f82 63 75 72 72 65 6e 74 20 69 6e 20 74 68 65 0a 2a current in the.*
19f83 2a 20 56 44 42 45 2e 20 20 52 65 74 75 72 6e 20 * VDBE. Return
19f84 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 the address of t
19f85 68 65 20 6e 65 77 20 69 6e 73 74 72 75 63 74 69 he new instructi
19f86 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65 on..**.** Parame
19f87 74 65 72 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 70 ters:.**.** p
19f88 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 P
19f89 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 56 44 ointer to the VD
19f8a 42 45 0a 2a 2a 0a 2a 2a 20 20 20 20 6f 70 20 20 BE.**.** op
19f8b 20 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20 The
19f8c 6f 70 63 6f 64 65 20 66 6f 72 20 74 68 69 73 20 opcode for this
19f8d 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 0a 2a instruction.**.*
19f8e 2a 20 20 20 20 70 31 2c 20 70 32 2c 20 70 33 20 * p1, p2, p3
19f8f 20 20 20 20 20 4f 70 65 72 61 6e 64 73 0a 2a 2a Operands.**
19f90 0a 2a 2a 20 55 73 65 20 74 68 65 20 73 71 6c 69 .** Use the sqli
19f91 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 te3VdbeResolveLa
19f92 62 65 6c 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 bel() function t
19f93 6f 20 66 69 78 20 61 6e 20 61 64 64 72 65 73 73 o fix an address
19f94 20 61 6e 64 0a 2a 2a 20 74 68 65 20 73 71 6c 69 and.** the sqli
19f95 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 te3VdbeChangeP4(
19f96 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 68 ) function to ch
19f97 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f ange the value o
19f98 66 20 74 68 65 20 50 34 0a 2a 2a 20 6f 70 65 72 f the P4.** oper
19f99 61 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 and..*/.SQLITE_P
19f9a 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
19f9b 65 33 56 64 62 65 41 64 64 4f 70 33 28 56 64 62 e3VdbeAddOp3(Vdb
19f9c 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 6e e *p, int op, in
19f9d 74 20 70 31 2c 20 69 6e 74 20 70 32 2c 20 69 6e t p1, int p2, in
19f9e 74 20 70 33 29 7b 0a 20 20 69 6e 74 20 69 3b 0a t p3){. int i;.
19f9f 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 0a VdbeOp *pOp;..
19fa0 20 20 69 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 i = p->nOp;.
19fa1 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 assert( p->magic
19fa2 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 ==VDBE_MAGIC_INI
19fa3 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f T );. assert( o
19fa4 70 3e 30 20 26 26 20 6f 70 3c 30 78 66 66 20 29 p>0 && op<0xff )
19fa5 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 41 6c ;. if( p->nOpAl
19fa6 6c 6f 63 3c 3d 69 20 29 7b 0a 20 20 20 20 69 66 loc<=i ){. if
19fa7 28 20 67 72 6f 77 4f 70 41 72 72 61 79 28 70 29 ( growOpArray(p)
19fa8 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
19fa9 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 1;. }. }.
19faa 70 2d 3e 6e 4f 70 2b 2b 3b 0a 20 20 70 4f 70 20 p->nOp++;. pOp
19fab 3d 20 26 70 2d 3e 61 4f 70 5b 69 5d 3b 0a 20 20 = &p->aOp[i];.
19fac 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 28 75 pOp->opcode = (u
19fad 38 29 6f 70 3b 0a 20 20 70 4f 70 2d 3e 70 35 20 8)op;. pOp->p5
19fae 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e 70 31 20 3d = 0;. pOp->p1 =
19faf 20 70 31 3b 0a 20 20 70 4f 70 2d 3e 70 32 20 3d p1;. pOp->p2 =
19fb0 20 70 32 3b 0a 20 20 70 4f 70 2d 3e 70 33 20 3d p2;. pOp->p3 =
19fb1 20 70 33 3b 0a 20 20 70 4f 70 2d 3e 70 34 2e 70 p3;. pOp->p4.p
19fb2 20 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e 70 34 74 = 0;. pOp->p4t
19fb3 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 45 44 ype = P4_NOTUSED
19fb4 3b 0a 20 20 70 2d 3e 65 78 70 69 72 65 64 20 3d ;. p->expired =
19fb5 20 30 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 0;.#ifdef SQLIT
19fb6 45 5f 44 45 42 55 47 0a 20 20 70 4f 70 2d 3e 7a E_DEBUG. pOp->z
19fb7 43 6f 6d 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 69 Comment = 0;. i
19fb8 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 f( sqlite3VdbeAd
19fb9 64 6f 70 54 72 61 63 65 20 29 20 73 71 6c 69 74 dopTrace ) sqlit
19fba 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 30 2c e3VdbePrintOp(0,
19fbb 20 69 2c 20 26 70 2d 3e 61 4f 70 5b 69 5d 29 3b i, &p->aOp[i]);
19fbc 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 56 .#endif.#ifdef V
19fbd 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 70 4f DBE_PROFILE. pO
19fbe 70 2d 3e 63 79 63 6c 65 73 20 3d 20 30 3b 0a 20 p->cycles = 0;.
19fbf 20 70 4f 70 2d 3e 63 6e 74 20 3d 20 30 3b 0a 23 pOp->cnt = 0;.#
19fc0 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 69 endif. return i
19fc1 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
19fc2 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
19fc3 62 65 41 64 64 4f 70 30 28 56 64 62 65 20 2a 70 beAddOp0(Vdbe *p
19fc4 2c 20 69 6e 74 20 6f 70 29 7b 0a 20 20 72 65 74 , int op){. ret
19fc5 75 72 6e 20 73 71 6c 69 74 65 33 56 64 62 65 41 urn sqlite3VdbeA
19fc6 64 64 4f 70 33 28 70 2c 20 6f 70 2c 20 30 2c 20 ddOp3(p, op, 0,
19fc7 30 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 0, 0);.}.SQLITE_
19fc8 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
19fc9 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 56 64 te3VdbeAddOp1(Vd
19fca 62 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 be *p, int op, i
19fcb 6e 74 20 70 31 29 7b 0a 20 20 72 65 74 75 72 6e nt p1){. return
19fcc 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
19fcd 70 33 28 70 2c 20 6f 70 2c 20 70 31 2c 20 30 2c p3(p, op, p1, 0,
19fce 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 0);.}.SQLITE_PR
19fcf 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
19fd0 33 56 64 62 65 41 64 64 4f 70 32 28 56 64 62 65 3VdbeAddOp2(Vdbe
19fd1 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 *p, int op, int
19fd2 20 70 31 2c 20 69 6e 74 20 70 32 29 7b 0a 20 20 p1, int p2){.
19fd3 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 64 return sqlite3Vd
19fd4 62 65 41 64 64 4f 70 33 28 70 2c 20 6f 70 2c 20 beAddOp3(p, op,
19fd5 70 31 2c 20 70 32 2c 20 30 29 3b 0a 7d 0a 0a 0a p1, p2, 0);.}...
19fd6 2f 2a 0a 2a 2a 20 41 64 64 20 61 6e 20 6f 70 63 /*.** Add an opc
19fd7 6f 64 65 20 74 68 61 74 20 69 6e 63 6c 75 64 65 ode that include
19fd8 73 20 74 68 65 20 70 34 20 76 61 6c 75 65 20 61 s the p4 value a
19fd9 73 20 61 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a s a pointer..*/.
19fda 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
19fdb 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 nt sqlite3VdbeAd
19fdc 64 4f 70 34 28 0a 20 20 56 64 62 65 20 2a 70 2c dOp4(. Vdbe *p,
19fdd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
19fde 64 64 20 74 68 65 20 6f 70 63 6f 64 65 20 74 6f dd the opcode to
19fdf 20 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20 69 6e this VM */. in
19fe0 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 t op,
19fe1 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 6f 70 63 /* The new opc
19fe2 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 70 31 2c ode */. int p1,
19fe3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
19fe4 54 68 65 20 50 31 20 6f 70 65 72 61 6e 64 20 2a The P1 operand *
19fe5 2f 0a 20 20 69 6e 74 20 70 32 2c 20 20 20 20 20 /. int p2,
19fe6 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50 /* The P
19fe7 32 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 2 operand */. i
19fe8 6e 74 20 70 33 2c 20 20 20 20 20 20 20 20 20 20 nt p3,
19fe9 20 20 20 2f 2a 20 54 68 65 20 50 33 20 6f 70 65 /* The P3 ope
19fea 72 61 6e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 rand */. const
19feb 63 68 61 72 20 2a 7a 50 34 2c 20 20 20 20 2f 2a char *zP4, /*
19fec 20 54 68 65 20 50 34 20 6f 70 65 72 61 6e 64 20 The P4 operand
19fed 2a 2f 0a 20 20 69 6e 74 20 70 34 74 79 70 65 20 */. int p4type
19fee 20 20 20 20 20 20 20 20 20 2f 2a 20 50 34 20 6f /* P4 o
19fef 70 65 72 61 6e 64 20 74 79 70 65 20 2a 2f 0a 29 perand type */.)
19ff0 7b 0a 20 20 69 6e 74 20 61 64 64 72 20 3d 20 73 {. int addr = s
19ff1 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
19ff2 28 70 2c 20 6f 70 2c 20 70 31 2c 20 70 32 2c 20 (p, op, p1, p2,
19ff3 70 33 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 p3);. sqlite3Vd
19ff4 62 65 43 68 61 6e 67 65 50 34 28 70 2c 20 61 64 beChangeP4(p, ad
19ff5 64 72 2c 20 7a 50 34 2c 20 70 34 74 79 70 65 29 dr, zP4, p4type)
19ff6 3b 0a 20 20 72 65 74 75 72 6e 20 61 64 64 72 3b ;. return addr;
19ff7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 .}../*.** Create
19ff8 20 61 20 6e 65 77 20 73 79 6d 62 6f 6c 69 63 20 a new symbolic
19ff9 6c 61 62 65 6c 20 66 6f 72 20 61 6e 20 69 6e 73 label for an ins
19ffa 74 72 75 63 74 69 6f 6e 20 74 68 61 74 20 68 61 truction that ha
19ffb 73 20 79 65 74 20 74 6f 20 62 65 0a 2a 2a 20 63 s yet to be.** c
19ffc 6f 64 65 64 2e 20 20 54 68 65 20 73 79 6d 62 6f oded. The symbo
19ffd 6c 69 63 20 6c 61 62 65 6c 20 69 73 20 72 65 61 lic label is rea
19ffe 6c 6c 79 20 6a 75 73 74 20 61 20 6e 65 67 61 74 lly just a negat
19fff 69 76 65 20 6e 75 6d 62 65 72 2e 20 20 54 68 65 ive number. The
1a000 0a 2a 2a 20 6c 61 62 65 6c 20 63 61 6e 20 62 65 .** label can be
1a001 20 75 73 65 64 20 61 73 20 74 68 65 20 50 32 20 used as the P2
1a002 76 61 6c 75 65 20 6f 66 20 61 6e 20 6f 70 65 72 value of an oper
1a003 61 74 69 6f 6e 2e 20 20 4c 61 74 65 72 2c 20 77 ation. Later, w
1a004 68 65 6e 0a 2a 2a 20 74 68 65 20 6c 61 62 65 6c hen.** the label
1a005 20 69 73 20 72 65 73 6f 6c 76 65 64 20 74 6f 20 is resolved to
1a006 61 20 73 70 65 63 69 66 69 63 20 61 64 64 72 65 a specific addre
1a007 73 73 2c 20 74 68 65 20 56 44 42 45 20 77 69 6c ss, the VDBE wil
1a008 6c 20 73 63 61 6e 0a 2a 2a 20 74 68 72 6f 75 67 l scan.** throug
1a009 68 20 69 74 73 20 6f 70 65 72 61 74 69 6f 6e 20 h its operation
1a00a 6c 69 73 74 20 61 6e 64 20 63 68 61 6e 67 65 20 list and change
1a00b 61 6c 6c 20 76 61 6c 75 65 73 20 6f 66 20 50 32 all values of P2
1a00c 20 77 68 69 63 68 20 6d 61 74 63 68 0a 2a 2a 20 which match.**
1a00d 74 68 65 20 6c 61 62 65 6c 20 69 6e 74 6f 20 74 the label into t
1a00e 68 65 20 72 65 73 6f 6c 76 65 64 20 61 64 64 72 he resolved addr
1a00f 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56 ess..**.** The V
1a010 44 42 45 20 6b 6e 6f 77 73 20 74 68 61 74 20 61 DBE knows that a
1a011 20 50 32 20 76 61 6c 75 65 20 69 73 20 61 20 6c P2 value is a l
1a012 61 62 65 6c 20 62 65 63 61 75 73 65 20 6c 61 62 abel because lab
1a013 65 6c 73 20 61 72 65 0a 2a 2a 20 61 6c 77 61 79 els are.** alway
1a014 73 20 6e 65 67 61 74 69 76 65 20 61 6e 64 20 50 s negative and P
1a015 32 20 76 61 6c 75 65 73 20 61 72 65 20 73 75 70 2 values are sup
1a016 70 6f 73 65 20 74 6f 20 62 65 20 6e 6f 6e 2d 6e pose to be non-n
1a017 65 67 61 74 69 76 65 2e 0a 2a 2a 20 48 65 6e 63 egative..** Henc
1a018 65 2c 20 61 20 6e 65 67 61 74 69 76 65 20 50 32 e, a negative P2
1a019 20 76 61 6c 75 65 20 69 73 20 61 20 6c 61 62 65 value is a labe
1a01a 6c 20 74 68 61 74 20 68 61 73 20 79 65 74 20 74 l that has yet t
1a01b 6f 20 62 65 20 72 65 73 6f 6c 76 65 64 2e 0a 2a o be resolved..*
1a01c 2a 0a 2a 2a 20 5a 65 72 6f 20 69 73 20 72 65 74 *.** Zero is ret
1a01d 75 72 6e 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f urned if a mallo
1a01e 63 28 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 c() fails..*/.SQ
1a01f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1a020 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 sqlite3VdbeMake
1a021 4c 61 62 65 6c 28 56 64 62 65 20 2a 70 29 7b 0a Label(Vdbe *p){.
1a022 20 20 69 6e 74 20 69 3b 0a 20 20 69 20 3d 20 70 int i;. i = p
1a023 2d 3e 6e 4c 61 62 65 6c 2b 2b 3b 0a 20 20 61 73 ->nLabel++;. as
1a024 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d sert( p->magic==
1a025 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 VDBE_MAGIC_INIT
1a026 29 3b 0a 20 20 69 66 28 20 69 3e 3d 70 2d 3e 6e );. if( i>=p->n
1a027 4c 61 62 65 6c 41 6c 6c 6f 63 20 29 7b 0a 20 20 LabelAlloc ){.
1a028 20 20 69 6e 74 20 6e 20 3d 20 70 2d 3e 6e 4c 61 int n = p->nLa
1a029 62 65 6c 41 6c 6c 6f 63 2a 32 20 2b 20 35 3b 0a belAlloc*2 + 5;.
1a02a 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c 20 3d 20 p->aLabel =
1a02b 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 sqlite3DbRealloc
1a02c 4f 72 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d OrFree(p->db, p-
1a02d 3e 61 4c 61 62 65 6c 2c 0a 20 20 20 20 20 20 20 >aLabel,.
1a02e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a02f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a030 6e 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 4c 61 62 n*sizeof(p->aLab
1a031 65 6c 5b 30 5d 29 29 3b 0a 20 20 20 20 70 2d 3e el[0]));. p->
1a032 6e 4c 61 62 65 6c 41 6c 6c 6f 63 20 3d 20 73 71 nLabelAlloc = sq
1a033 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a lite3DbMallocSiz
1a034 65 28 70 2d 3e 64 62 2c 20 70 2d 3e 61 4c 61 62 e(p->db, p->aLab
1a035 65 6c 29 2f 73 69 7a 65 6f 66 28 70 2d 3e 61 4c el)/sizeof(p->aL
1a036 61 62 65 6c 5b 30 5d 29 3b 0a 20 20 7d 0a 20 20 abel[0]);. }.
1a037 69 66 28 20 70 2d 3e 61 4c 61 62 65 6c 20 29 7b if( p->aLabel ){
1a038 0a 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c 5b 69 . p->aLabel[i
1a039 5d 20 3d 20 2d 31 3b 0a 20 20 7d 0a 20 20 72 65 ] = -1;. }. re
1a03a 74 75 72 6e 20 2d 31 2d 69 3b 0a 7d 0a 0a 2f 2a turn -1-i;.}../*
1a03b 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 6c 61 62 65 .** Resolve labe
1a03c 6c 20 22 78 22 20 74 6f 20 62 65 20 74 68 65 20 l "x" to be the
1a03d 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 6e address of the n
1a03e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 ext instruction
1a03f 74 6f 0a 2a 2a 20 62 65 20 69 6e 73 65 72 74 65 to.** be inserte
1a040 64 2e 20 20 54 68 65 20 70 61 72 61 6d 65 74 65 d. The paramete
1a041 72 20 22 78 22 20 6d 75 73 74 20 68 61 76 65 20 r "x" must have
1a042 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 66 72 been obtained fr
1a043 6f 6d 0a 2a 2a 20 61 20 70 72 69 6f 72 20 63 61 om.** a prior ca
1a044 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 56 64 62 ll to sqlite3Vdb
1a045 65 4d 61 6b 65 4c 61 62 65 6c 28 29 2e 0a 2a 2f eMakeLabel()..*/
1a046 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1a047 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
1a048 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 56 64 62 ResolveLabel(Vdb
1a049 65 20 2a 70 2c 20 69 6e 74 20 78 29 7b 0a 20 20 e *p, int x){.
1a04a 69 6e 74 20 6a 20 3d 20 2d 31 2d 78 3b 0a 20 20 int j = -1-x;.
1a04b 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 assert( p->magic
1a04c 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 ==VDBE_MAGIC_INI
1a04d 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6a T );. assert( j
1a04e 3e 3d 30 20 26 26 20 6a 3c 70 2d 3e 6e 4c 61 62 >=0 && j<p->nLab
1a04f 65 6c 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 el );. if( p->a
1a050 4c 61 62 65 6c 20 29 7b 0a 20 20 20 20 70 2d 3e Label ){. p->
1a051 61 4c 61 62 65 6c 5b 6a 5d 20 3d 20 70 2d 3e 6e aLabel[j] = p->n
1a052 4f 70 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 64 65 Op;. }.}..#ifde
1a053 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 2f f SQLITE_DEBUG /
1a054 2a 20 73 71 6c 69 74 65 33 41 73 73 65 72 74 4d * sqlite3AssertM
1a055 61 79 41 62 6f 72 74 28 29 20 6c 6f 67 69 63 20 ayAbort() logic
1a056 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f */../*.** The fo
1a057 6c 6c 6f 77 69 6e 67 20 74 79 70 65 20 61 6e 64 llowing type and
1a058 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 75 73 function are us
1a059 65 64 20 74 6f 20 69 74 65 72 61 74 65 20 74 68 ed to iterate th
1a05a 72 6f 75 67 68 20 61 6c 6c 20 6f 70 63 6f 64 65 rough all opcode
1a05b 73 0a 2a 2a 20 69 6e 20 61 20 56 64 62 65 20 6d s.** in a Vdbe m
1a05c 61 69 6e 20 70 72 6f 67 72 61 6d 20 61 6e 64 20 ain program and
1a05d 65 61 63 68 20 6f 66 20 74 68 65 20 73 75 62 2d each of the sub-
1a05e 70 72 6f 67 72 61 6d 73 20 28 74 72 69 67 67 65 programs (trigge
1a05f 72 73 29 20 69 74 20 6d 61 79 20 0a 2a 2a 20 69 rs) it may .** i
1a060 6e 76 6f 6b 65 20 64 69 72 65 63 74 6c 79 20 6f nvoke directly o
1a061 72 20 69 6e 64 69 72 65 63 74 6c 79 2e 20 49 74 r indirectly. It
1a062 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 should be used
1a063 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a as follows:.**.*
1a064 2a 20 20 20 4f 70 20 2a 70 4f 70 3b 0a 2a 2a 20 * Op *pOp;.**
1a065 20 20 56 64 62 65 4f 70 49 74 65 72 20 73 49 74 VdbeOpIter sIt
1a066 65 72 3b 0a 2a 2a 0a 2a 2a 20 20 20 6d 65 6d 73 er;.**.** mems
1a067 65 74 28 26 73 49 74 65 72 2c 20 30 2c 20 73 69 et(&sIter, 0, si
1a068 7a 65 6f 66 28 73 49 74 65 72 29 29 3b 0a 2a 2a zeof(sIter));.**
1a069 20 20 20 73 49 74 65 72 2e 76 20 3d 20 76 3b 20 sIter.v = v;
1a06a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a06b 20 20 20 20 20 20 20 20 20 20 20 2f 2f 20 76 20 // v
1a06c 69 73 20 6f 66 20 74 79 70 65 20 56 64 62 65 2a is of type Vdbe*
1a06d 20 0a 2a 2a 20 20 20 77 68 69 6c 65 28 20 28 70 .** while( (p
1a06e 4f 70 20 3d 20 6f 70 49 74 65 72 4e 65 78 74 28 Op = opIterNext(
1a06f 26 73 49 74 65 72 29 29 20 29 7b 0a 2a 2a 20 20 &sIter)) ){.**
1a070 20 20 20 2f 2f 20 44 6f 20 73 6f 6d 65 74 68 69 // Do somethi
1a071 6e 67 20 77 69 74 68 20 70 4f 70 0a 2a 2a 20 20 ng with pOp.**
1a072 20 7d 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 44 }.** sqlite3D
1a073 62 46 72 65 65 28 76 2d 3e 64 62 2c 20 73 49 74 bFree(v->db, sIt
1a074 65 72 2e 61 70 53 75 62 29 3b 0a 2a 2a 20 0a 2a er.apSub);.** .*
1a075 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
1a076 20 56 64 62 65 4f 70 49 74 65 72 20 56 64 62 65 VdbeOpIter Vdbe
1a077 4f 70 49 74 65 72 3b 0a 73 74 72 75 63 74 20 56 OpIter;.struct V
1a078 64 62 65 4f 70 49 74 65 72 20 7b 0a 20 20 56 64 dbeOpIter {. Vd
1a079 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 be *v;
1a07a 20 20 20 20 20 20 20 20 20 2f 2a 20 56 64 62 65 /* Vdbe
1a07b 20 74 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f to iterate thro
1a07c 75 67 68 20 74 68 65 20 6f 70 63 6f 64 65 73 20 ugh the opcodes
1a07d 6f 66 20 2a 2f 0a 20 20 53 75 62 50 72 6f 67 72 of */. SubProgr
1a07e 61 6d 20 2a 2a 61 70 53 75 62 3b 20 20 20 20 20 am **apSub;
1a07f 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 73 /* Array of s
1a080 75 62 70 72 6f 67 72 61 6d 73 20 2a 2f 0a 20 20 ubprograms */.
1a081 69 6e 74 20 6e 53 75 62 3b 20 20 20 20 20 20 20 int nSub;
1a082 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
1a083 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 mber of entries
1a084 69 6e 20 61 70 53 75 62 20 2a 2f 0a 20 20 69 6e in apSub */. in
1a085 74 20 69 41 64 64 72 3b 20 20 20 20 20 20 20 20 t iAddr;
1a086 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 /* Addr
1a087 65 73 73 20 6f 66 20 6e 65 78 74 20 69 6e 73 74 ess of next inst
1a088 72 75 63 74 69 6f 6e 20 74 6f 20 72 65 74 75 72 ruction to retur
1a089 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 53 75 62 3b n */. int iSub;
1a08a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a08b 20 20 2f 2a 20 30 20 3d 20 6d 61 69 6e 20 70 72 /* 0 = main pr
1a08c 6f 67 72 61 6d 2c 20 31 20 3d 20 66 69 72 73 74 ogram, 1 = first
1a08d 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 65 74 63 sub-program etc
1a08e 2e 20 2a 2f 0a 7d 3b 0a 73 74 61 74 69 63 20 4f . */.};.static O
1a08f 70 20 2a 6f 70 49 74 65 72 4e 65 78 74 28 56 64 p *opIterNext(Vd
1a090 62 65 4f 70 49 74 65 72 20 2a 70 29 7b 0a 20 20 beOpIter *p){.
1a091 56 64 62 65 20 2a 76 20 3d 20 70 2d 3e 76 3b 0a Vdbe *v = p->v;.
1a092 20 20 4f 70 20 2a 70 52 65 74 20 3d 20 30 3b 0a Op *pRet = 0;.
1a093 20 20 4f 70 20 2a 61 4f 70 3b 0a 20 20 69 6e 74 Op *aOp;. int
1a094 20 6e 4f 70 3b 0a 0a 20 20 69 66 28 20 70 2d 3e nOp;.. if( p->
1a095 69 53 75 62 3c 3d 70 2d 3e 6e 53 75 62 20 29 7b iSub<=p->nSub ){
1a096 0a 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 53 75 .. if( p->iSu
1a097 62 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 4f b==0 ){. aO
1a098 70 20 3d 20 76 2d 3e 61 4f 70 3b 0a 20 20 20 20 p = v->aOp;.
1a099 20 20 6e 4f 70 20 3d 20 76 2d 3e 6e 4f 70 3b 0a nOp = v->nOp;.
1a09a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1a09b 20 61 4f 70 20 3d 20 70 2d 3e 61 70 53 75 62 5b aOp = p->apSub[
1a09c 70 2d 3e 69 53 75 62 2d 31 5d 2d 3e 61 4f 70 3b p->iSub-1]->aOp;
1a09d 0a 20 20 20 20 20 20 6e 4f 70 20 3d 20 70 2d 3e . nOp = p->
1a09e 61 70 53 75 62 5b 70 2d 3e 69 53 75 62 2d 31 5d apSub[p->iSub-1]
1a09f 2d 3e 6e 4f 70 3b 0a 20 20 20 20 7d 0a 20 20 20 ->nOp;. }.
1a0a0 20 61 73 73 65 72 74 28 20 70 2d 3e 69 41 64 64 assert( p->iAdd
1a0a1 72 3c 6e 4f 70 20 29 3b 0a 0a 20 20 20 20 70 52 r<nOp );.. pR
1a0a2 65 74 20 3d 20 26 61 4f 70 5b 70 2d 3e 69 41 64 et = &aOp[p->iAd
1a0a3 64 72 5d 3b 0a 20 20 20 20 70 2d 3e 69 41 64 64 dr];. p->iAdd
1a0a4 72 2b 2b 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e r++;. if( p->
1a0a5 69 41 64 64 72 3d 3d 6e 4f 70 20 29 7b 0a 20 20 iAddr==nOp ){.
1a0a6 20 20 20 20 70 2d 3e 69 53 75 62 2b 2b 3b 0a 20 p->iSub++;.
1a0a7 20 20 20 20 20 70 2d 3e 69 41 64 64 72 20 3d 20 p->iAddr =
1a0a8 30 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 0;. }. .
1a0a9 69 66 28 20 70 52 65 74 2d 3e 70 34 74 79 70 65 if( pRet->p4type
1a0aa 3d 3d 50 34 5f 53 55 42 50 52 4f 47 52 41 4d 20 ==P4_SUBPROGRAM
1a0ab 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 42 79 ){. int nBy
1a0ac 74 65 20 3d 20 28 70 2d 3e 6e 53 75 62 2b 31 29 te = (p->nSub+1)
1a0ad 2a 73 69 7a 65 6f 66 28 53 75 62 50 72 6f 67 72 *sizeof(SubProgr
1a0ae 61 6d 2a 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 am*);. int
1a0af 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 j;. for(j=0
1a0b0 3b 20 6a 3c 70 2d 3e 6e 53 75 62 3b 20 6a 2b 2b ; j<p->nSub; j++
1a0b1 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 ){. if( p
1a0b2 2d 3e 61 70 53 75 62 5b 6a 5d 3d 3d 70 52 65 74 ->apSub[j]==pRet
1a0b3 2d 3e 70 34 2e 70 50 72 6f 67 72 61 6d 20 29 20 ->p4.pProgram )
1a0b4 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 break;. }.
1a0b5 20 20 20 20 20 69 66 28 20 6a 3d 3d 70 2d 3e 6e if( j==p->n
1a0b6 53 75 62 20 29 7b 0a 20 20 20 20 20 20 20 20 70 Sub ){. p
1a0b7 2d 3e 61 70 53 75 62 20 3d 20 73 71 6c 69 74 65 ->apSub = sqlite
1a0b8 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 3DbReallocOrFree
1a0b9 28 76 2d 3e 64 62 2c 20 70 2d 3e 61 70 53 75 62 (v->db, p->apSub
1a0ba 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 , nByte);.
1a0bb 20 20 69 66 28 20 21 70 2d 3e 61 70 53 75 62 20 if( !p->apSub
1a0bc 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 52 65 ){. pRe
1a0bd 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d t = 0;. }
1a0be 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
1a0bf 70 2d 3e 61 70 53 75 62 5b 70 2d 3e 6e 53 75 62 p->apSub[p->nSub
1a0c0 2b 2b 5d 20 3d 20 70 52 65 74 2d 3e 70 34 2e 70 ++] = pRet->p4.p
1a0c1 50 72 6f 67 72 61 6d 3b 0a 20 20 20 20 20 20 20 Program;.
1a0c2 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d }. }. }
1a0c3 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 . }.. return p
1a0c4 52 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 Ret;.}../*.** Ch
1a0c5 65 63 6b 20 69 66 20 74 68 65 20 70 72 6f 67 72 eck if the progr
1a0c6 61 6d 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 am stored in the
1a0c7 20 56 4d 20 61 73 73 6f 63 69 61 74 65 64 20 77 VM associated w
1a0c8 69 74 68 20 70 50 61 72 73 65 20 6d 61 79 0a 2a ith pParse may.*
1a0c9 2a 20 74 68 72 6f 77 20 61 6e 20 41 42 4f 52 54 * throw an ABORT
1a0ca 20 65 78 63 65 70 74 69 6f 6e 20 28 63 61 75 73 exception (caus
1a0cb 69 6e 67 20 74 68 65 20 73 74 61 74 65 6d 65 6e ing the statemen
1a0cc 74 2c 20 62 75 74 20 6e 6f 74 20 65 6e 74 69 72 t, but not entir
1a0cd 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a e transaction.**
1a0ce 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 to be rolled ba
1a0cf 63 6b 29 2e 20 54 68 69 73 20 63 6f 6e 64 69 74 ck). This condit
1a0d0 69 6f 6e 20 69 73 20 74 72 75 65 20 69 66 20 74 ion is true if t
1a0d1 68 65 20 6d 61 69 6e 20 70 72 6f 67 72 61 6d 20 he main program
1a0d2 6f 72 20 61 6e 79 0a 2a 2a 20 73 75 62 2d 70 72 or any.** sub-pr
1a0d3 6f 67 72 61 6d 73 20 63 6f 6e 74 61 69 6e 73 20 ograms contains
1a0d4 61 6e 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f any of the follo
1a0d5 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 wing:.**.** *
1a0d6 20 4f 50 5f 48 61 6c 74 20 77 69 74 68 20 50 31 OP_Halt with P1
1a0d7 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 =SQLITE_CONSTRAI
1a0d8 4e 54 20 61 6e 64 20 50 32 3d 4f 45 5f 41 62 6f NT and P2=OE_Abo
1a0d9 72 74 2e 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 48 rt..** * OP_H
1a0da 61 6c 74 49 66 4e 75 6c 6c 20 77 69 74 68 20 50 altIfNull with P
1a0db 31 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 1=SQLITE_CONSTRA
1a0dc 49 4e 54 20 61 6e 64 20 50 32 3d 4f 45 5f 41 62 INT and P2=OE_Ab
1a0dd 6f 72 74 2e 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f ort..** * OP_
1a0de 44 65 73 74 72 6f 79 0a 2a 2a 20 20 20 2a 20 20 Destroy.** *
1a0df 4f 50 5f 56 55 70 64 61 74 65 0a 2a 2a 20 20 20 OP_VUpdate.**
1a0e0 2a 20 20 4f 50 5f 56 52 65 6e 61 6d 65 0a 2a 2a * OP_VRename.**
1a0e1 20 20 20 2a 20 20 4f 50 5f 46 6b 43 6f 75 6e 74 * OP_FkCount
1a0e2 65 72 20 77 69 74 68 20 50 32 3d 3d 30 20 28 69 er with P2==0 (i
1a0e3 6d 6d 65 64 69 61 74 65 20 66 6f 72 65 69 67 6e mmediate foreign
1a0e4 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 29 key constraint)
1a0e5 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 63 68 65 63 .**.** Then chec
1a0e6 6b 20 74 68 61 74 20 74 68 65 20 76 61 6c 75 65 k that the value
1a0e7 20 6f 66 20 50 61 72 73 65 2e 6d 61 79 41 62 6f of Parse.mayAbo
1a0e8 72 74 20 69 73 20 74 72 75 65 20 69 66 20 61 6e rt is true if an
1a0e9 0a 2a 2a 20 41 42 4f 52 54 20 6d 61 79 20 62 65 .** ABORT may be
1a0ea 20 74 68 72 6f 77 6e 2c 20 6f 72 20 66 61 6c 73 thrown, or fals
1a0eb 65 20 6f 74 68 65 72 77 69 73 65 2e 20 52 65 74 e otherwise. Ret
1a0ec 75 72 6e 20 74 72 75 65 20 69 66 20 69 74 20 64 urn true if it d
1a0ed 6f 65 73 0a 2a 2a 20 6d 61 74 63 68 2c 20 6f 72 oes.** match, or
1a0ee 20 66 61 6c 73 65 20 6f 74 68 65 72 77 69 73 65 false otherwise
1a0ef 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 . This function
1a0f0 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 is intended to b
1a0f1 65 20 75 73 65 64 20 61 73 0a 2a 2a 20 70 61 72 e used as.** par
1a0f2 74 20 6f 66 20 61 6e 20 61 73 73 65 72 74 20 73 t of an assert s
1a0f3 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 tatement in the
1a0f4 63 6f 6d 70 69 6c 65 72 2e 20 53 69 6d 69 6c 61 compiler. Simila
1a0f5 72 20 74 6f 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 73 r to:.**.** as
1a0f6 73 65 72 74 28 20 73 71 6c 69 74 65 33 56 64 62 sert( sqlite3Vdb
1a0f7 65 41 73 73 65 72 74 4d 61 79 41 62 6f 72 74 28 eAssertMayAbort(
1a0f8 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 70 pParse->pVdbe, p
1a0f9 50 61 72 73 65 2d 3e 6d 61 79 41 62 6f 72 74 29 Parse->mayAbort)
1a0fa 20 29 3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 );.*/.SQLITE_PR
1a0fb 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1a0fc 33 56 64 62 65 41 73 73 65 72 74 4d 61 79 41 62 3VdbeAssertMayAb
1a0fd 6f 72 74 28 56 64 62 65 20 2a 76 2c 20 69 6e 74 ort(Vdbe *v, int
1a0fe 20 6d 61 79 41 62 6f 72 74 29 7b 0a 20 20 69 6e mayAbort){. in
1a0ff 74 20 68 61 73 41 62 6f 72 74 20 3d 20 30 3b 0a t hasAbort = 0;.
1a100 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 56 64 62 Op *pOp;. Vdb
1a101 65 4f 70 49 74 65 72 20 73 49 74 65 72 3b 0a 20 eOpIter sIter;.
1a102 20 6d 65 6d 73 65 74 28 26 73 49 74 65 72 2c 20 memset(&sIter,
1a103 30 2c 20 73 69 7a 65 6f 66 28 73 49 74 65 72 29 0, sizeof(sIter)
1a104 29 3b 0a 20 20 73 49 74 65 72 2e 76 20 3d 20 76 );. sIter.v = v
1a105 3b 0a 0a 20 20 77 68 69 6c 65 28 20 28 70 4f 70 ;.. while( (pOp
1a106 20 3d 20 6f 70 49 74 65 72 4e 65 78 74 28 26 73 = opIterNext(&s
1a107 49 74 65 72 29 29 21 3d 30 20 29 7b 0a 20 20 20 Iter))!=0 ){.
1a108 20 69 6e 74 20 6f 70 63 6f 64 65 20 3d 20 70 4f int opcode = pO
1a109 70 2d 3e 6f 70 63 6f 64 65 3b 0a 20 20 20 20 69 p->opcode;. i
1a10a 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 44 65 f( opcode==OP_De
1a10b 73 74 72 6f 79 20 7c 7c 20 6f 70 63 6f 64 65 3d stroy || opcode=
1a10c 3d 4f 50 5f 56 55 70 64 61 74 65 20 7c 7c 20 6f =OP_VUpdate || o
1a10d 70 63 6f 64 65 3d 3d 4f 50 5f 56 52 65 6e 61 6d pcode==OP_VRenam
1a10e 65 20 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 e .#ifndef SQLIT
1a10f 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b E_OMIT_FOREIGN_K
1a110 45 59 0a 20 20 20 20 20 7c 7c 20 28 6f 70 63 6f EY. || (opco
1a111 64 65 3d 3d 4f 50 5f 46 6b 43 6f 75 6e 74 65 72 de==OP_FkCounter
1a112 20 26 26 20 70 4f 70 2d 3e 70 31 3d 3d 30 20 26 && pOp->p1==0 &
1a113 26 20 70 4f 70 2d 3e 70 32 3d 3d 31 29 20 0a 23 & pOp->p2==1) .#
1a114 65 6e 64 69 66 0a 20 20 20 20 20 7c 7c 20 28 28 endif. || ((
1a115 6f 70 63 6f 64 65 3d 3d 4f 50 5f 48 61 6c 74 20 opcode==OP_Halt
1a116 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 48 61 || opcode==OP_Ha
1a117 6c 74 49 66 4e 75 6c 6c 29 20 0a 20 20 20 20 20 ltIfNull) .
1a118 20 26 26 20 28 70 4f 70 2d 3e 70 31 3d 3d 53 51 && (pOp->p1==SQ
1a119 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 LITE_CONSTRAINT
1a11a 26 26 20 70 4f 70 2d 3e 70 32 3d 3d 4f 45 5f 41 && pOp->p2==OE_A
1a11b 62 6f 72 74 29 29 0a 20 20 20 20 29 7b 0a 20 20 bort)). ){.
1a11c 20 20 20 20 68 61 73 41 62 6f 72 74 20 3d 20 31 hasAbort = 1
1a11d 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
1a11e 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 }. }. sqlit
1a11f 65 33 44 62 46 72 65 65 28 76 2d 3e 64 62 2c 20 e3DbFree(v->db,
1a120 73 49 74 65 72 2e 61 70 53 75 62 29 3b 0a 0a 20 sIter.apSub);..
1a121 20 2f 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 /* Return true
1a122 69 66 20 68 61 73 41 62 6f 72 74 3d 3d 6d 61 79 if hasAbort==may
1a123 41 62 6f 72 74 2e 20 4f 72 20 69 66 20 61 20 6d Abort. Or if a m
1a124 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 6f 63 alloc failure oc
1a125 63 75 72 65 64 2e 0a 20 20 2a 2a 20 49 66 20 6d cured.. ** If m
1a126 61 6c 6c 6f 63 20 66 61 69 6c 65 64 2c 20 74 68 alloc failed, th
1a127 65 6e 20 74 68 65 20 77 68 69 6c 65 28 29 20 6c en the while() l
1a128 6f 6f 70 20 61 62 6f 76 65 20 6d 61 79 20 6e 6f oop above may no
1a129 74 20 68 61 76 65 20 69 74 65 72 61 74 65 64 0a t have iterated.
1a12a 20 20 2a 2a 20 74 68 72 6f 75 67 68 20 61 6c 6c ** through all
1a12b 20 6f 70 63 6f 64 65 73 20 61 6e 64 20 68 61 73 opcodes and has
1a12c 41 62 6f 72 74 20 6d 61 79 20 62 65 20 73 65 74 Abort may be set
1a12d 20 69 6e 63 6f 72 72 65 63 74 6c 79 2e 20 52 65 incorrectly. Re
1a12e 74 75 72 6e 0a 20 20 2a 2a 20 74 72 75 65 20 66 turn. ** true f
1a12f 6f 72 20 74 68 69 73 20 63 61 73 65 20 74 6f 20 or this case to
1a130 70 72 65 76 65 6e 74 20 74 68 65 20 61 73 73 65 prevent the asse
1a131 72 74 28 29 20 69 6e 20 74 68 65 20 63 61 6c 6c rt() in the call
1a132 65 72 73 20 66 72 61 6d 65 0a 20 20 2a 2a 20 66 ers frame. ** f
1a133 72 6f 6d 20 66 61 69 6c 69 6e 67 2e 20 20 2a 2f rom failing. */
1a134 0a 20 20 72 65 74 75 72 6e 20 28 20 76 2d 3e 64 . return ( v->d
1a135 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1a136 7c 7c 20 68 61 73 41 62 6f 72 74 3d 3d 6d 61 79 || hasAbort==may
1a137 41 62 6f 72 74 20 29 3b 0a 7d 0a 23 65 6e 64 69 Abort );.}.#endi
1a138 66 20 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42 55 f /* SQLITE_DEBU
1a139 47 20 2d 20 74 68 65 20 73 71 6c 69 74 65 33 41 G - the sqlite3A
1a13a 73 73 65 72 74 4d 61 79 41 62 6f 72 74 28 29 20 ssertMayAbort()
1a13b 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 2f 2a 0a function */../*.
1a13c 2a 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 ** Loop through
1a13d 74 68 65 20 70 72 6f 67 72 61 6d 20 6c 6f 6f 6b the program look
1a13e 69 6e 67 20 66 6f 72 20 50 32 20 76 61 6c 75 65 ing for P2 value
1a13f 73 20 74 68 61 74 20 61 72 65 20 6e 65 67 61 74 s that are negat
1a140 69 76 65 0a 2a 2a 20 6f 6e 20 6a 75 6d 70 20 69 ive.** on jump i
1a141 6e 73 74 72 75 63 74 69 6f 6e 73 2e 20 20 45 61 nstructions. Ea
1a142 63 68 20 73 75 63 68 20 76 61 6c 75 65 20 69 73 ch such value is
1a143 20 61 20 6c 61 62 65 6c 2e 20 20 52 65 73 6f 6c a label. Resol
1a144 76 65 20 74 68 65 0a 2a 2a 20 6c 61 62 65 6c 20 ve the.** label
1a145 62 79 20 73 65 74 74 69 6e 67 20 74 68 65 20 50 by setting the P
1a146 32 20 76 61 6c 75 65 20 74 6f 20 69 74 73 20 63 2 value to its c
1a147 6f 72 72 65 63 74 20 6e 6f 6e 2d 7a 65 72 6f 20 orrect non-zero
1a148 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 value..**.** Thi
1a149 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
1a14a 6c 65 64 20 6f 6e 63 65 20 61 66 74 65 72 20 61 led once after a
1a14b 6c 6c 20 6f 70 63 6f 64 65 73 20 68 61 76 65 20 ll opcodes have
1a14c 62 65 65 6e 20 69 6e 73 65 72 74 65 64 2e 0a 2a been inserted..*
1a14d 2a 0a 2a 2a 20 56 61 72 69 61 62 6c 65 20 2a 70 *.** Variable *p
1a14e 4d 61 78 46 75 6e 63 41 72 67 73 20 69 73 20 73 MaxFuncArgs is s
1a14f 65 74 20 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 et to the maximu
1a150 6d 20 76 61 6c 75 65 20 6f 66 20 61 6e 79 20 50 m value of any P
1a151 32 20 61 72 67 75 6d 65 6e 74 20 0a 2a 2a 20 74 2 argument .** t
1a152 6f 20 61 6e 20 4f 50 5f 46 75 6e 63 74 69 6f 6e o an OP_Function
1a153 2c 20 4f 50 5f 41 67 67 53 74 65 70 20 6f 72 20 , OP_AggStep or
1a154 4f 50 5f 56 46 69 6c 74 65 72 20 6f 70 63 6f 64 OP_VFilter opcod
1a155 65 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20 e. This is used
1a156 62 79 20 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 by .** sqlite3Vd
1a157 62 65 4d 61 6b 65 52 65 61 64 79 28 29 20 74 6f beMakeReady() to
1a158 20 73 69 7a 65 20 74 68 65 20 56 64 62 65 2e 61 size the Vdbe.a
1a159 70 41 72 67 5b 5d 20 61 72 72 61 79 2e 0a 2a 2f pArg[] array..*/
1a15a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 73 .static void res
1a15b 6f 6c 76 65 50 32 56 61 6c 75 65 73 28 56 64 62 olveP2Values(Vdb
1a15c 65 20 2a 70 2c 20 69 6e 74 20 2a 70 4d 61 78 46 e *p, int *pMaxF
1a15d 75 6e 63 41 72 67 73 29 7b 0a 20 20 69 6e 74 20 uncArgs){. int
1a15e 69 3b 0a 20 20 69 6e 74 20 6e 4d 61 78 41 72 67 i;. int nMaxArg
1a15f 73 20 3d 20 2a 70 4d 61 78 46 75 6e 63 41 72 67 s = *pMaxFuncArg
1a160 73 3b 0a 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 s;. Op *pOp;.
1a161 69 6e 74 20 2a 61 4c 61 62 65 6c 20 3d 20 70 2d int *aLabel = p-
1a162 3e 61 4c 61 62 65 6c 3b 0a 20 20 70 2d 3e 72 65 >aLabel;. p->re
1a163 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a 20 20 66 6f adOnly = 1;. fo
1a164 72 28 70 4f 70 3d 70 2d 3e 61 4f 70 2c 20 69 3d r(pOp=p->aOp, i=
1a165 70 2d 3e 6e 4f 70 2d 31 3b 20 69 3e 3d 30 3b 20 p->nOp-1; i>=0;
1a166 69 2d 2d 2c 20 70 4f 70 2b 2b 29 7b 0a 20 20 20 i--, pOp++){.
1a167 20 75 38 20 6f 70 63 6f 64 65 20 3d 20 70 4f 70 u8 opcode = pOp
1a168 2d 3e 6f 70 63 6f 64 65 3b 0a 0a 20 20 20 20 69 ->opcode;.. i
1a169 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46 75 f( opcode==OP_Fu
1a16a 6e 63 74 69 6f 6e 20 7c 7c 20 6f 70 63 6f 64 65 nction || opcode
1a16b 3d 3d 4f 50 5f 41 67 67 53 74 65 70 20 29 7b 0a ==OP_AggStep ){.
1a16c 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 if( pOp->p
1a16d 35 3e 6e 4d 61 78 41 72 67 73 20 29 20 6e 4d 61 5>nMaxArgs ) nMa
1a16e 78 41 72 67 73 20 3d 20 70 4f 70 2d 3e 70 35 3b xArgs = pOp->p5;
1a16f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1a170 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
1a171 45 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 E. }else if(
1a172 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 55 70 64 61 opcode==OP_VUpda
1a173 74 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 te ){. if(
1a174 70 4f 70 2d 3e 70 32 3e 6e 4d 61 78 41 72 67 73 pOp->p2>nMaxArgs
1a175 20 29 20 6e 4d 61 78 41 72 67 73 20 3d 20 70 4f ) nMaxArgs = pO
1a176 70 2d 3e 70 32 3b 0a 23 65 6e 64 69 66 0a 20 20 p->p2;.#endif.
1a177 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 63 6f }else if( opco
1a178 64 65 3d 3d 4f 50 5f 54 72 61 6e 73 61 63 74 69 de==OP_Transacti
1a179 6f 6e 20 26 26 20 70 4f 70 2d 3e 70 32 21 3d 30 on && pOp->p2!=0
1a17a 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 72 65 61 ){. p->rea
1a17b 64 4f 6e 6c 79 20 3d 20 30 3b 0a 23 69 66 6e 64 dOnly = 0;.#ifnd
1a17c 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ef SQLITE_OMIT_V
1a17d 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 IRTUALTABLE.
1a17e 7d 65 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65 }else if( opcode
1a17f 3d 3d 4f 50 5f 56 46 69 6c 74 65 72 20 29 7b 0a ==OP_VFilter ){.
1a180 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 int n;.
1a181 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f assert( p->nO
1a182 70 20 2d 20 69 20 3e 3d 20 33 20 29 3b 0a 20 20 p - i >= 3 );.
1a183 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b assert( pOp[
1a184 2d 31 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 -1].opcode==OP_I
1a185 6e 74 65 67 65 72 20 29 3b 0a 20 20 20 20 20 20 nteger );.
1a186 6e 20 3d 20 70 4f 70 5b 2d 31 5d 2e 70 31 3b 0a n = pOp[-1].p1;.
1a187 20 20 20 20 20 20 69 66 28 20 6e 3e 6e 4d 61 78 if( n>nMax
1a188 41 72 67 73 20 29 20 6e 4d 61 78 41 72 67 73 20 Args ) nMaxArgs
1a189 3d 20 6e 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 = n;.#endif.
1a18a 7d 0a 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 }.. if( sqlit
1a18b 65 33 56 64 62 65 4f 70 63 6f 64 65 48 61 73 50 e3VdbeOpcodeHasP
1a18c 72 6f 70 65 72 74 79 28 6f 70 63 6f 64 65 2c 20 roperty(opcode,
1a18d 4f 50 46 4c 47 5f 4a 55 4d 50 29 20 26 26 20 70 OPFLG_JUMP) && p
1a18e 4f 70 2d 3e 70 32 3c 30 20 29 7b 0a 20 20 20 20 Op->p2<0 ){.
1a18f 20 20 61 73 73 65 72 74 28 20 2d 31 2d 70 4f 70 assert( -1-pOp
1a190 2d 3e 70 32 3c 70 2d 3e 6e 4c 61 62 65 6c 20 29 ->p2<p->nLabel )
1a191 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 70 32 20 ;. pOp->p2
1a192 3d 20 61 4c 61 62 65 6c 5b 2d 31 2d 70 4f 70 2d = aLabel[-1-pOp-
1a193 3e 70 32 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a >p2];. }. }.
1a194 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1a195 70 2d 3e 64 62 2c 20 70 2d 3e 61 4c 61 62 65 6c p->db, p->aLabel
1a196 29 3b 0a 20 20 70 2d 3e 61 4c 61 62 65 6c 20 3d );. p->aLabel =
1a197 20 30 3b 0a 0a 20 20 2a 70 4d 61 78 46 75 6e 63 0;.. *pMaxFunc
1a198 41 72 67 73 20 3d 20 6e 4d 61 78 41 72 67 73 3b Args = nMaxArgs;
1a199 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
1a19a 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 the address of
1a19b 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 the next instruc
1a19c 74 69 6f 6e 20 74 6f 20 62 65 20 69 6e 73 65 72 tion to be inser
1a19d 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ted..*/.SQLITE_P
1a19e 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1a19f 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 e3VdbeCurrentAdd
1a1a0 72 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 61 73 r(Vdbe *p){. as
1a1a1 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d sert( p->magic==
1a1a2 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 VDBE_MAGIC_INIT
1a1a3 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e );. return p->n
1a1a4 4f 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 Op;.}../*.** Thi
1a1a5 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 s function retur
1a1a6 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ns a pointer to
1a1a7 74 68 65 20 61 72 72 61 79 20 6f 66 20 6f 70 63 the array of opc
1a1a8 6f 64 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 odes associated
1a1a9 77 69 74 68 0a 2a 2a 20 74 68 65 20 56 64 62 65 with.** the Vdbe
1a1aa 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 passed as the f
1a1ab 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 49 irst argument. I
1a1ac 74 20 69 73 20 74 68 65 20 63 61 6c 6c 65 72 73 t is the callers
1a1ad 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 0a responsibility.
1a1ae 2a 2a 20 74 6f 20 61 72 72 61 6e 67 65 20 66 6f ** to arrange fo
1a1af 72 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 61 r the returned a
1a1b0 72 72 61 79 20 74 6f 20 62 65 20 65 76 65 6e 74 rray to be event
1a1b1 75 61 6c 6c 79 20 66 72 65 65 64 20 75 73 69 6e ually freed usin
1a1b2 67 20 74 68 65 20 0a 2a 2a 20 76 64 62 65 46 72 g the .** vdbeFr
1a1b3 65 65 4f 70 41 72 72 61 79 28 29 20 66 75 6e 63 eeOpArray() func
1a1b4 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 42 65 66 6f tion..**.** Befo
1a1b5 72 65 20 72 65 74 75 72 6e 69 6e 67 2c 20 2a 70 re returning, *p
1a1b6 6e 4f 70 20 69 73 20 73 65 74 20 74 6f 20 74 68 nOp is set to th
1a1b7 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 e number of entr
1a1b8 69 65 73 20 69 6e 20 74 68 65 20 72 65 74 75 72 ies in the retur
1a1b9 6e 65 64 0a 2a 2a 20 61 72 72 61 79 2e 20 41 6c ned.** array. Al
1a1ba 73 6f 2c 20 2a 70 6e 4d 61 78 41 72 67 20 69 73 so, *pnMaxArg is
1a1bb 20 73 65 74 20 74 6f 20 74 68 65 20 6c 61 72 67 set to the larg
1a1bc 65 72 20 6f 66 20 69 74 73 20 63 75 72 72 65 6e er of its curren
1a1bd 74 20 76 61 6c 75 65 20 61 6e 64 20 0a 2a 2a 20 t value and .**
1a1be 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e the number of en
1a1bf 74 72 69 65 73 20 69 6e 20 74 68 65 20 56 64 62 tries in the Vdb
1a1c0 65 2e 61 70 41 72 67 5b 5d 20 61 72 72 61 79 20 e.apArg[] array
1a1c1 72 65 71 75 69 72 65 64 20 74 6f 20 65 78 65 63 required to exec
1a1c2 75 74 65 20 74 68 65 20 0a 2a 2a 20 72 65 74 75 ute the .** retu
1a1c3 72 6e 65 64 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f rned program..*/
1a1c4 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1a1c5 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 65 33 56 VdbeOp *sqlite3V
1a1c6 64 62 65 54 61 6b 65 4f 70 41 72 72 61 79 28 56 dbeTakeOpArray(V
1a1c7 64 62 65 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 4f dbe *p, int *pnO
1a1c8 70 2c 20 69 6e 74 20 2a 70 6e 4d 61 78 41 72 67 p, int *pnMaxArg
1a1c9 29 7b 0a 20 20 56 64 62 65 4f 70 20 2a 61 4f 70 ){. VdbeOp *aOp
1a1ca 20 3d 20 70 2d 3e 61 4f 70 3b 0a 20 20 61 73 73 = p->aOp;. ass
1a1cb 65 72 74 28 20 61 4f 70 20 26 26 20 21 70 2d 3e ert( aOp && !p->
1a1cc 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1a1cd 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 );.. /* Check
1a1ce 74 68 61 74 20 73 71 6c 69 74 65 33 56 64 62 65 that sqlite3Vdbe
1a1cf 55 73 65 73 42 74 72 65 65 28 29 20 77 61 73 20 UsesBtree() was
1a1d0 6e 6f 74 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68 not called on th
1a1d1 69 73 20 56 4d 20 2a 2f 0a 20 20 61 73 73 65 72 is VM */. asser
1a1d2 74 28 20 70 2d 3e 61 4d 75 74 65 78 2e 6e 4d 75 t( p->aMutex.nMu
1a1d3 74 65 78 3d 3d 30 20 29 3b 0a 0a 20 20 72 65 73 tex==0 );.. res
1a1d4 6f 6c 76 65 50 32 56 61 6c 75 65 73 28 70 2c 20 olveP2Values(p,
1a1d5 70 6e 4d 61 78 41 72 67 29 3b 0a 20 20 2a 70 6e pnMaxArg);. *pn
1a1d6 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 70 Op = p->nOp;. p
1a1d7 2d 3e 61 4f 70 20 3d 20 30 3b 0a 20 20 72 65 74 ->aOp = 0;. ret
1a1d8 75 72 6e 20 61 4f 70 3b 0a 7d 0a 0a 2f 2a 0a 2a urn aOp;.}../*.*
1a1d9 2a 20 41 64 64 20 61 20 77 68 6f 6c 65 20 6c 69 * Add a whole li
1a1da 73 74 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 73 st of operations
1a1db 20 74 6f 20 74 68 65 20 6f 70 65 72 61 74 69 6f to the operatio
1a1dc 6e 20 73 74 61 63 6b 2e 20 20 52 65 74 75 72 6e n stack. Return
1a1dd 20 74 68 65 0a 2a 2a 20 61 64 64 72 65 73 73 20 the.** address
1a1de 6f 66 20 74 68 65 20 66 69 72 73 74 20 6f 70 65 of the first ope
1a1df 72 61 74 69 6f 6e 20 61 64 64 65 64 2e 0a 2a 2f ration added..*/
1a1e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1a1e1 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 int sqlite3VdbeA
1a1e2 64 64 4f 70 4c 69 73 74 28 56 64 62 65 20 2a 70 ddOpList(Vdbe *p
1a1e3 2c 20 69 6e 74 20 6e 4f 70 2c 20 56 64 62 65 4f , int nOp, VdbeO
1a1e4 70 4c 69 73 74 20 63 6f 6e 73 74 20 2a 61 4f 70 pList const *aOp
1a1e5 29 7b 0a 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 ){. int addr;.
1a1e6 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 assert( p->magi
1a1e7 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e c==VDBE_MAGIC_IN
1a1e8 49 54 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e IT );. if( p->n
1a1e9 4f 70 20 2b 20 6e 4f 70 20 3e 20 70 2d 3e 6e 4f Op + nOp > p->nO
1a1ea 70 41 6c 6c 6f 63 20 26 26 20 67 72 6f 77 4f 70 pAlloc && growOp
1a1eb 41 72 72 61 79 28 70 29 20 29 7b 0a 20 20 20 20 Array(p) ){.
1a1ec 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
1a1ed 61 64 64 72 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 addr = p->nOp;.
1a1ee 20 69 66 28 20 41 4c 57 41 59 53 28 6e 4f 70 3e if( ALWAYS(nOp>
1a1ef 30 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0) ){. int i;
1a1f0 0a 20 20 20 20 56 64 62 65 4f 70 4c 69 73 74 20 . VdbeOpList
1a1f1 63 6f 6e 73 74 20 2a 70 49 6e 20 3d 20 61 4f 70 const *pIn = aOp
1a1f2 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 ;. for(i=0; i
1a1f3 3c 6e 4f 70 3b 20 69 2b 2b 2c 20 70 49 6e 2b 2b <nOp; i++, pIn++
1a1f4 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 32 20 ){. int p2
1a1f5 3d 20 70 49 6e 2d 3e 70 32 3b 0a 20 20 20 20 20 = pIn->p2;.
1a1f6 20 56 64 62 65 4f 70 20 2a 70 4f 75 74 20 3d 20 VdbeOp *pOut =
1a1f7 26 70 2d 3e 61 4f 70 5b 69 2b 61 64 64 72 5d 3b &p->aOp[i+addr];
1a1f8 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 6f 70 63 . pOut->opc
1a1f9 6f 64 65 20 3d 20 70 49 6e 2d 3e 6f 70 63 6f 64 ode = pIn->opcod
1a1fa 65 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 e;. pOut->p
1a1fb 31 20 3d 20 70 49 6e 2d 3e 70 31 3b 0a 20 20 20 1 = pIn->p1;.
1a1fc 20 20 20 69 66 28 20 70 32 3c 30 20 26 26 20 73 if( p2<0 && s
1a1fd 71 6c 69 74 65 33 56 64 62 65 4f 70 63 6f 64 65 qlite3VdbeOpcode
1a1fe 48 61 73 50 72 6f 70 65 72 74 79 28 70 4f 75 74 HasProperty(pOut
1a1ff 2d 3e 6f 70 63 6f 64 65 2c 20 4f 50 46 4c 47 5f ->opcode, OPFLG_
1a200 4a 55 4d 50 29 20 29 7b 0a 20 20 20 20 20 20 20 JUMP) ){.
1a201 20 70 4f 75 74 2d 3e 70 32 20 3d 20 61 64 64 72 pOut->p2 = addr
1a202 20 2b 20 41 44 44 52 28 70 32 29 3b 0a 20 20 20 + ADDR(p2);.
1a203 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1a204 20 20 70 4f 75 74 2d 3e 70 32 20 3d 20 70 32 3b pOut->p2 = p2;
1a205 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 . }. p
1a206 4f 75 74 2d 3e 70 33 20 3d 20 70 49 6e 2d 3e 70 Out->p3 = pIn->p
1a207 33 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 3;. pOut->p
1a208 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 4type = P4_NOTUS
1a209 45 44 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e ED;. pOut->
1a20a 70 34 2e 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 p4.p = 0;.
1a20b 70 4f 75 74 2d 3e 70 35 20 3d 20 30 3b 0a 23 69 pOut->p5 = 0;.#i
1a20c 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
1a20d 47 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 7a 43 G. pOut->zC
1a20e 6f 6d 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20 omment = 0;.
1a20f 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 if( sqlite3Vdb
1a210 65 41 64 64 6f 70 54 72 61 63 65 20 29 7b 0a 20 eAddopTrace ){.
1a211 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
1a212 62 65 50 72 69 6e 74 4f 70 28 30 2c 20 69 2b 61 bePrintOp(0, i+a
1a213 64 64 72 2c 20 26 70 2d 3e 61 4f 70 5b 69 2b 61 ddr, &p->aOp[i+a
1a214 64 64 72 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 23 ddr]);. }.#
1a215 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 20 20 endif. }.
1a216 70 2d 3e 6e 4f 70 20 2b 3d 20 6e 4f 70 3b 0a 20 p->nOp += nOp;.
1a217 20 7d 0a 20 20 72 65 74 75 72 6e 20 61 64 64 72 }. return addr
1a218 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 ;.}../*.** Chang
1a219 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 e the value of t
1a21a 68 65 20 50 31 20 6f 70 65 72 61 6e 64 20 66 6f he P1 operand fo
1a21b 72 20 61 20 73 70 65 63 69 66 69 63 20 69 6e 73 r a specific ins
1a21c 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 truction..** Thi
1a21d 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 s routine is use
1a21e 66 75 6c 20 77 68 65 6e 20 61 20 6c 61 72 67 65 ful when a large
1a21f 20 70 72 6f 67 72 61 6d 20 69 73 20 6c 6f 61 64 program is load
1a220 65 64 20 66 72 6f 6d 20 61 0a 2a 2a 20 73 74 61 ed from a.** sta
1a221 74 69 63 20 61 72 72 61 79 20 75 73 69 6e 67 20 tic array using
1a222 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1a223 4c 69 73 74 20 62 75 74 20 77 65 20 77 61 6e 74 List but we want
1a224 20 74 6f 20 6d 61 6b 65 20 61 0a 2a 2a 20 66 65 to make a.** fe
1a225 77 20 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 20 w minor changes
1a226 74 6f 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 0a to the program..
1a227 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1a228 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
1a229 62 65 43 68 61 6e 67 65 50 31 28 56 64 62 65 20 beChangeP1(Vdbe
1a22a 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e *p, int addr, in
1a22b 74 20 76 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 t val){. assert
1a22c 28 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 ( p!=0 );. asse
1a22d 72 74 28 20 61 64 64 72 3e 3d 30 20 29 3b 0a 20 rt( addr>=0 );.
1a22e 20 69 66 28 20 70 2d 3e 6e 4f 70 3e 61 64 64 72 if( p->nOp>addr
1a22f 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4f 70 5b 61 ){. p->aOp[a
1a230 64 64 72 5d 2e 70 31 20 3d 20 76 61 6c 3b 0a 20 ddr].p1 = val;.
1a231 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e }.}../*.** Chan
1a232 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 ge the value of
1a233 74 68 65 20 50 32 20 6f 70 65 72 61 6e 64 20 66 the P2 operand f
1a234 6f 72 20 61 20 73 70 65 63 69 66 69 63 20 69 6e or a specific in
1a235 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 struction..** Th
1a236 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 is routine is us
1a237 65 66 75 6c 20 66 6f 72 20 73 65 74 74 69 6e 67 eful for setting
1a238 20 61 20 6a 75 6d 70 20 64 65 73 74 69 6e 61 74 a jump destinat
1a239 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ion..*/.SQLITE_P
1a23a 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
1a23b 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 te3VdbeChangeP2(
1a23c 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 Vdbe *p, int add
1a23d 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20 61 r, int val){. a
1a23e 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 ssert( p!=0 );.
1a23f 20 61 73 73 65 72 74 28 20 61 64 64 72 3e 3d 30 assert( addr>=0
1a240 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 );. if( p->nOp
1a241 3e 61 64 64 72 20 29 7b 0a 20 20 20 20 70 2d 3e >addr ){. p->
1a242 61 4f 70 5b 61 64 64 72 5d 2e 70 32 20 3d 20 76 aOp[addr].p2 = v
1a243 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a al;. }.}../*.**
1a244 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 Change the valu
1a245 65 20 6f 66 20 74 68 65 20 50 33 20 6f 70 65 72 e of the P3 oper
1a246 61 6e 64 20 66 6f 72 20 61 20 73 70 65 63 69 66 and for a specif
1a247 69 63 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a ic instruction..
1a248 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1a249 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
1a24a 62 65 43 68 61 6e 67 65 50 33 28 56 64 62 65 20 beChangeP3(Vdbe
1a24b 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e *p, int addr, in
1a24c 74 20 76 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 t val){. assert
1a24d 28 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 ( p!=0 );. asse
1a24e 72 74 28 20 61 64 64 72 3e 3d 30 20 29 3b 0a 20 rt( addr>=0 );.
1a24f 20 69 66 28 20 70 2d 3e 6e 4f 70 3e 61 64 64 72 if( p->nOp>addr
1a250 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4f 70 5b 61 ){. p->aOp[a
1a251 64 64 72 5d 2e 70 33 20 3d 20 76 61 6c 3b 0a 20 ddr].p3 = val;.
1a252 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e }.}../*.** Chan
1a253 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 ge the value of
1a254 74 68 65 20 50 35 20 6f 70 65 72 61 6e 64 20 66 the P5 operand f
1a255 6f 72 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 or the most rece
1a256 6e 74 6c 79 0a 2a 2a 20 61 64 64 65 64 20 6f 70 ntly.** added op
1a257 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 eration..*/.SQLI
1a258 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1a259 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
1a25a 65 50 35 28 56 64 62 65 20 2a 70 2c 20 75 38 20 eP5(Vdbe *p, u8
1a25b 76 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 val){. assert(
1a25c 70 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d p!=0 );. if( p-
1a25d 3e 61 4f 70 20 29 7b 0a 20 20 20 20 61 73 73 65 >aOp ){. asse
1a25e 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 29 3b 0a rt( p->nOp>0 );.
1a25f 20 20 20 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f p->aOp[p->nO
1a260 70 2d 31 5d 2e 70 35 20 3d 20 76 61 6c 3b 0a 20 p-1].p5 = val;.
1a261 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e }.}../*.** Chan
1a262 67 65 20 74 68 65 20 50 32 20 6f 70 65 72 61 6e ge the P2 operan
1a263 64 20 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e d of instruction
1a264 20 61 64 64 72 20 73 6f 20 74 68 61 74 20 69 74 addr so that it
1a265 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 74 68 points to.** th
1a266 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 e address of the
1a267 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f next instructio
1a268 6e 20 74 6f 20 62 65 20 63 6f 64 65 64 2e 0a 2a n to be coded..*
1a269 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1a26a 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
1a26b 65 4a 75 6d 70 48 65 72 65 28 56 64 62 65 20 2a eJumpHere(Vdbe *
1a26c 70 2c 20 69 6e 74 20 61 64 64 72 29 7b 0a 20 20 p, int addr){.
1a26d 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
1a26e 65 50 32 28 70 2c 20 61 64 64 72 2c 20 70 2d 3e eP2(p, addr, p->
1a26f 6e 4f 70 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 nOp);.}.../*.**
1a270 49 66 20 74 68 65 20 69 6e 70 75 74 20 46 75 6e If the input Fun
1a271 63 44 65 66 20 73 74 72 75 63 74 75 72 65 20 69 cDef structure i
1a272 73 20 65 70 68 65 6d 65 72 61 6c 2c 20 74 68 65 s ephemeral, the
1a273 6e 20 66 72 65 65 20 69 74 2e 20 20 49 66 0a 2a n free it. If.*
1a274 2a 20 74 68 65 20 46 75 6e 63 44 65 66 20 69 73 * the FuncDef is
1a275 20 6e 6f 74 20 65 70 68 65 72 6d 61 6c 2c 20 74 not ephermal, t
1a276 68 65 6e 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 0a hen do nothing..
1a277 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 */.static void f
1a278 72 65 65 45 70 68 65 6d 65 72 61 6c 46 75 6e 63 reeEphemeralFunc
1a279 74 69 6f 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 tion(sqlite3 *db
1a27a 2c 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 29 , FuncDef *pDef)
1a27b 7b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 {. if( ALWAYS(p
1a27c 44 65 66 29 20 26 26 20 28 70 44 65 66 2d 3e 66 Def) && (pDef->f
1a27d 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55 lags & SQLITE_FU
1a27e 4e 43 5f 45 50 48 45 4d 29 21 3d 30 20 29 7b 0a NC_EPHEM)!=0 ){.
1a27f 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1a280 65 28 64 62 2c 20 70 44 65 66 29 3b 0a 20 20 7d e(db, pDef);. }
1a281 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 .}../*.** Delete
1a282 20 61 20 50 34 20 76 61 6c 75 65 20 69 66 20 6e a P4 value if n
1a283 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 73 74 61 ecessary..*/.sta
1a284 74 69 63 20 76 6f 69 64 20 66 72 65 65 50 34 28 tic void freeP4(
1a285 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 sqlite3 *db, int
1a286 20 70 34 74 79 70 65 2c 20 76 6f 69 64 20 2a 70 p4type, void *p
1a287 34 29 7b 0a 20 20 69 66 28 20 70 34 20 29 7b 0a 4){. if( p4 ){.
1a288 20 20 20 20 73 77 69 74 63 68 28 20 70 34 74 79 switch( p4ty
1a289 70 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 pe ){. case
1a28a 20 50 34 5f 52 45 41 4c 3a 0a 20 20 20 20 20 20 P4_REAL:.
1a28b 63 61 73 65 20 50 34 5f 49 4e 54 36 34 3a 0a 20 case P4_INT64:.
1a28c 20 20 20 20 20 63 61 73 65 20 50 34 5f 4d 50 52 case P4_MPR
1a28d 49 4e 54 46 3a 0a 20 20 20 20 20 20 63 61 73 65 INTF:. case
1a28e 20 50 34 5f 44 59 4e 41 4d 49 43 3a 0a 20 20 20 P4_DYNAMIC:.
1a28f 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e case P4_KEYIN
1a290 46 4f 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50 FO:. case P
1a291 34 5f 49 4e 54 41 52 52 41 59 3a 0a 20 20 20 20 4_INTARRAY:.
1a292 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e 46 case P4_KEYINF
1a293 4f 5f 48 41 4e 44 4f 46 46 3a 20 7b 0a 20 20 20 O_HANDOFF: {.
1a294 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
1a295 65 65 28 64 62 2c 20 70 34 29 3b 0a 20 20 20 20 ee(db, p4);.
1a296 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
1a297 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 }. case P4
1a298 5f 56 44 42 45 46 55 4e 43 3a 20 7b 0a 20 20 20 _VDBEFUNC: {.
1a299 20 20 20 20 20 56 64 62 65 46 75 6e 63 20 2a 70 VdbeFunc *p
1a29a 56 64 62 65 46 75 6e 63 20 3d 20 28 56 64 62 65 VdbeFunc = (Vdbe
1a29b 46 75 6e 63 20 2a 29 70 34 3b 0a 20 20 20 20 20 Func *)p4;.
1a29c 20 20 20 66 72 65 65 45 70 68 65 6d 65 72 61 6c freeEphemeral
1a29d 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70 56 64 Function(db, pVd
1a29e 62 65 46 75 6e 63 2d 3e 70 46 75 6e 63 29 3b 0a beFunc->pFunc);.
1a29f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1a2a0 64 62 65 44 65 6c 65 74 65 41 75 78 44 61 74 61 dbeDeleteAuxData
1a2a1 28 70 56 64 62 65 46 75 6e 63 2c 20 30 29 3b 0a (pVdbeFunc, 0);.
1a2a2 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 sqlite3D
1a2a3 62 46 72 65 65 28 64 62 2c 20 70 56 64 62 65 46 bFree(db, pVdbeF
1a2a4 75 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 62 72 unc);. br
1a2a5 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 eak;. }.
1a2a6 20 20 20 63 61 73 65 20 50 34 5f 46 55 4e 43 44 case P4_FUNCD
1a2a7 45 46 3a 20 7b 0a 20 20 20 20 20 20 20 20 66 72 EF: {. fr
1a2a8 65 65 45 70 68 65 6d 65 72 61 6c 46 75 6e 63 74 eeEphemeralFunct
1a2a9 69 6f 6e 28 64 62 2c 20 28 46 75 6e 63 44 65 66 ion(db, (FuncDef
1a2aa 2a 29 70 34 29 3b 0a 20 20 20 20 20 20 20 20 62 *)p4);. b
1a2ab 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
1a2ac 20 20 20 20 63 61 73 65 20 50 34 5f 4d 45 4d 3a case P4_MEM:
1a2ad 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 {. sqlit
1a2ae 65 33 56 61 6c 75 65 46 72 65 65 28 28 73 71 6c e3ValueFree((sql
1a2af 69 74 65 33 5f 76 61 6c 75 65 2a 29 70 34 29 3b ite3_value*)p4);
1a2b0 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
1a2b1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 }. ca
1a2b2 73 65 20 50 34 5f 56 54 41 42 20 3a 20 7b 0a 20 se P4_VTAB : {.
1a2b3 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 74 sqlite3Vt
1a2b4 61 62 55 6e 6c 6f 63 6b 28 28 56 54 61 62 6c 65 abUnlock((VTable
1a2b5 20 2a 29 70 34 29 3b 0a 20 20 20 20 20 20 20 20 *)p4);.
1a2b6 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 break;. }.
1a2b7 20 20 20 20 20 63 61 73 65 20 50 34 5f 53 55 42 case P4_SUB
1a2b8 50 52 4f 47 52 41 4d 20 3a 20 7b 0a 20 20 20 20 PROGRAM : {.
1a2b9 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 sqlite3VdbeP
1a2ba 72 6f 67 72 61 6d 44 65 6c 65 74 65 28 64 62 2c rogramDelete(db,
1a2bb 20 28 53 75 62 50 72 6f 67 72 61 6d 20 2a 29 70 (SubProgram *)p
1a2bc 34 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 62 4, 1);. b
1a2bd 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
1a2be 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
1a2bf 20 46 72 65 65 20 74 68 65 20 73 70 61 63 65 20 Free the space
1a2c0 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 4f allocated for aO
1a2c1 70 20 61 6e 64 20 61 6e 79 20 70 34 20 76 61 6c p and any p4 val
1a2c2 75 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f ues allocated fo
1a2c3 72 20 74 68 65 0a 2a 2a 20 6f 70 63 6f 64 65 73 r the.** opcodes
1a2c4 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 contained withi
1a2c5 6e 2e 20 49 66 20 61 4f 70 20 69 73 20 6e 6f 74 n. If aOp is not
1a2c6 20 4e 55 4c 4c 20 69 74 20 69 73 20 61 73 73 75 NULL it is assu
1a2c7 6d 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 0a med to contain .
1a2c8 2a 2a 20 6e 4f 70 20 65 6e 74 72 69 65 73 2e 20 ** nOp entries.
1a2c9 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
1a2ca 76 64 62 65 46 72 65 65 4f 70 41 72 72 61 79 28 vdbeFreeOpArray(
1a2cb 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 4f 70 20 sqlite3 *db, Op
1a2cc 2a 61 4f 70 2c 20 69 6e 74 20 6e 4f 70 29 7b 0a *aOp, int nOp){.
1a2cd 20 20 69 66 28 20 61 4f 70 20 29 7b 0a 20 20 20 if( aOp ){.
1a2ce 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20 66 6f Op *pOp;. fo
1a2cf 72 28 70 4f 70 3d 61 4f 70 3b 20 70 4f 70 3c 26 r(pOp=aOp; pOp<&
1a2d0 61 4f 70 5b 6e 4f 70 5d 3b 20 70 4f 70 2b 2b 29 aOp[nOp]; pOp++)
1a2d1 7b 0a 20 20 20 20 20 20 66 72 65 65 50 34 28 64 {. freeP4(d
1a2d2 62 2c 20 70 4f 70 2d 3e 70 34 74 79 70 65 2c 20 b, pOp->p4type,
1a2d3 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a 23 69 66 64 pOp->p4.p);.#ifd
1a2d4 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
1a2d5 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 sqlite3DbF
1a2d6 72 65 65 28 64 62 2c 20 70 4f 70 2d 3e 7a 43 6f ree(db, pOp->zCo
1a2d7 6d 6d 65 6e 74 29 3b 0a 23 65 6e 64 69 66 20 20 mment);.#endif
1a2d8 20 20 20 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 . }. }.
1a2d9 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1a2da 2c 20 61 4f 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a , aOp);.}../*.**
1a2db 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 72 Decrement the r
1a2dc 65 66 2d 63 6f 75 6e 74 20 6f 6e 20 74 68 65 20 ef-count on the
1a2dd 53 75 62 50 72 6f 67 72 61 6d 20 73 74 72 75 63 SubProgram struc
1a2de 74 75 72 65 20 70 61 73 73 65 64 20 61 73 20 74 ture passed as t
1a2df 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 61 72 67 he.** second arg
1a2e0 75 6d 65 6e 74 2e 20 49 66 20 74 68 65 20 72 65 ument. If the re
1a2e1 66 2d 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 f-count reaches
1a2e2 7a 65 72 6f 2c 20 66 72 65 65 20 74 68 65 20 73 zero, free the s
1a2e3 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 tructure..**.**
1a2e4 54 68 65 20 61 72 72 61 79 20 6f 66 20 56 44 42 The array of VDB
1a2e5 45 20 6f 70 63 6f 64 65 73 20 73 74 6f 72 65 64 E opcodes stored
1a2e6 20 61 73 20 53 75 62 50 72 6f 67 72 61 6d 2e 61 as SubProgram.a
1a2e7 4f 70 20 69 73 20 66 72 65 65 64 20 69 66 0a 2a Op is freed if.*
1a2e8 2a 20 65 69 74 68 65 72 20 74 68 65 20 72 65 66 * either the ref
1a2e9 2d 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 7a -count reaches z
1a2ea 65 72 6f 20 6f 72 20 70 61 72 61 6d 65 74 65 72 ero or parameter
1a2eb 20 66 72 65 65 6f 70 20 69 73 20 6e 6f 6e 2d 7a freeop is non-z
1a2ec 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 65 ero..**.** Since
1a2ed 20 74 68 65 20 61 72 72 61 79 20 6f 66 20 6f 70 the array of op
1a2ee 63 6f 64 65 73 20 70 6f 69 6e 74 65 64 20 74 6f codes pointed to
1a2ef 20 62 79 20 53 75 62 50 72 6f 67 72 61 6d 2e 61 by SubProgram.a
1a2f0 4f 70 20 6d 61 79 20 64 69 72 65 63 74 6c 79 0a Op may directly.
1a2f1 2a 2a 20 6f 72 20 69 6e 64 69 72 65 63 74 6c 79 ** or indirectly
1a2f2 20 63 6f 6e 74 61 69 6e 20 61 20 72 65 66 65 72 contain a refer
1a2f3 65 6e 63 65 20 74 6f 20 74 68 65 20 53 75 62 50 ence to the SubP
1a2f4 72 6f 67 72 61 6d 20 73 74 72 75 63 74 75 72 65 rogram structure
1a2f5 20 69 74 73 65 6c 66 2e 0a 2a 2a 20 42 79 20 70 itself..** By p
1a2f6 61 73 73 69 6e 67 20 61 20 6e 6f 6e 2d 7a 65 72 assing a non-zer
1a2f7 6f 20 66 72 65 65 6f 70 20 70 61 72 61 6d 65 74 o freeop paramet
1a2f8 65 72 2c 20 74 68 65 20 63 61 6c 6c 65 72 20 6d er, the caller m
1a2f9 61 79 20 65 6e 73 75 72 65 20 74 68 61 74 20 61 ay ensure that a
1a2fa 6c 6c 0a 2a 2a 20 53 75 62 50 72 6f 67 72 61 6d ll.** SubProgram
1a2fb 20 73 74 72 75 63 74 75 72 65 73 20 61 6e 64 20 structures and
1a2fc 74 68 65 69 72 20 61 4f 70 20 61 72 72 61 79 73 their aOp arrays
1a2fd 20 61 72 65 20 66 72 65 65 64 2c 20 65 76 65 6e are freed, even
1a2fe 20 77 68 65 6e 20 74 68 65 72 65 0a 2a 2a 20 61 when there.** a
1a2ff 72 65 20 73 75 63 68 20 63 69 72 63 75 6c 61 72 re such circular
1a300 20 72 65 66 65 72 65 6e 63 65 73 2e 0a 2a 2f 0a references..*/.
1a301 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1a302 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 50 oid sqlite3VdbeP
1a303 72 6f 67 72 61 6d 44 65 6c 65 74 65 28 73 71 6c rogramDelete(sql
1a304 69 74 65 33 20 2a 64 62 2c 20 53 75 62 50 72 6f ite3 *db, SubPro
1a305 67 72 61 6d 20 2a 70 2c 20 69 6e 74 20 66 72 65 gram *p, int fre
1a306 65 6f 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b eop){. if( p ){
1a307 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e . assert( p->
1a308 6e 52 65 66 3e 30 20 29 3b 0a 20 20 20 20 69 66 nRef>0 );. if
1a309 28 20 66 72 65 65 6f 70 20 7c 7c 20 70 2d 3e 6e ( freeop || p->n
1a30a 52 65 66 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 Ref==1 ){.
1a30b 4f 70 20 2a 61 4f 70 20 3d 20 70 2d 3e 61 4f 70 Op *aOp = p->aOp
1a30c 3b 0a 20 20 20 20 20 20 70 2d 3e 61 4f 70 20 3d ;. p->aOp =
1a30d 20 30 3b 0a 20 20 20 20 20 20 76 64 62 65 46 72 0;. vdbeFr
1a30e 65 65 4f 70 41 72 72 61 79 28 64 62 2c 20 61 4f eeOpArray(db, aO
1a30f 70 2c 20 70 2d 3e 6e 4f 70 29 3b 0a 20 20 20 20 p, p->nOp);.
1a310 20 20 70 2d 3e 6e 4f 70 20 3d 20 30 3b 0a 20 20 p->nOp = 0;.
1a311 20 20 7d 0a 20 20 20 20 70 2d 3e 6e 52 65 66 2d }. p->nRef-
1a312 2d 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 52 -;. if( p->nR
1a313 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 ef==0 ){. s
1a314 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1a315 20 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d p);. }. }.}
1a316 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 .../*.** Change
1a317 4e 20 6f 70 63 6f 64 65 73 20 73 74 61 72 74 69 N opcodes starti
1a318 6e 67 20 61 74 20 61 64 64 72 20 74 6f 20 4e 6f ng at addr to No
1a319 2d 6f 70 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f -ops..*/.SQLITE_
1a31a 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1a31b 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 54 6f ite3VdbeChangeTo
1a31c 4e 6f 6f 70 28 56 64 62 65 20 2a 70 2c 20 69 6e Noop(Vdbe *p, in
1a31d 74 20 61 64 64 72 2c 20 69 6e 74 20 4e 29 7b 0a t addr, int N){.
1a31e 20 20 69 66 28 20 70 2d 3e 61 4f 70 20 29 7b 0a if( p->aOp ){.
1a31f 20 20 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 20 VdbeOp *pOp
1a320 3d 20 26 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 3b = &p->aOp[addr];
1a321 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 . sqlite3 *db
1a322 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 77 68 = p->db;. wh
1a323 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a 20 20 20 20 ile( N-- ){.
1a324 20 20 66 72 65 65 50 34 28 64 62 2c 20 70 4f 70 freeP4(db, pOp
1a325 2d 3e 70 34 74 79 70 65 2c 20 70 4f 70 2d 3e 70 ->p4type, pOp->p
1a326 34 2e 70 29 3b 0a 20 20 20 20 20 20 6d 65 6d 73 4.p);. mems
1a327 65 74 28 70 4f 70 2c 20 30 2c 20 73 69 7a 65 6f et(pOp, 0, sizeo
1a328 66 28 70 4f 70 5b 30 5d 29 29 3b 0a 20 20 20 20 f(pOp[0]));.
1a329 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 pOp->opcode =
1a32a 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20 20 20 20 70 OP_Noop;. p
1a32b 4f 70 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a Op++;. }. }.
1a32c 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 }../*.** Change
1a32d 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 the value of the
1a32e 20 50 34 20 6f 70 65 72 61 6e 64 20 66 6f 72 20 P4 operand for
1a32f 61 20 73 70 65 63 69 66 69 63 20 69 6e 73 74 72 a specific instr
1a330 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 uction..** This
1a331 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 66 75 routine is usefu
1a332 6c 20 77 68 65 6e 20 61 20 6c 61 72 67 65 20 70 l when a large p
1a333 72 6f 67 72 61 6d 20 69 73 20 6c 6f 61 64 65 64 rogram is loaded
1a334 20 66 72 6f 6d 20 61 0a 2a 2a 20 73 74 61 74 69 from a.** stati
1a335 63 20 61 72 72 61 79 20 75 73 69 6e 67 20 73 71 c array using sq
1a336 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 lite3VdbeAddOpLi
1a337 73 74 20 62 75 74 20 77 65 20 77 61 6e 74 20 74 st but we want t
1a338 6f 20 6d 61 6b 65 20 61 0a 2a 2a 20 66 65 77 20 o make a.** few
1a339 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 20 74 6f minor changes to
1a33a 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a the program..**
1a33b 0a 2a 2a 20 49 66 20 6e 3e 3d 30 20 74 68 65 6e .** If n>=0 then
1a33c 20 74 68 65 20 50 34 20 6f 70 65 72 61 6e 64 20 the P4 operand
1a33d 69 73 20 64 79 6e 61 6d 69 63 2c 20 6d 65 61 6e is dynamic, mean
1a33e 69 6e 67 20 74 68 61 74 20 61 20 63 6f 70 79 20 ing that a copy
1a33f 6f 66 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 of.** the string
1a340 20 69 73 20 6d 61 64 65 20 69 6e 74 6f 20 6d 65 is made into me
1a341 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 mory obtained fr
1a342 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f om sqlite3_mallo
1a343 63 28 29 2e 0a 2a 2a 20 41 20 76 61 6c 75 65 20 c()..** A value
1a344 6f 66 20 6e 3d 3d 30 20 6d 65 61 6e 73 20 63 6f of n==0 means co
1a345 70 79 20 62 79 74 65 73 20 6f 66 20 7a 50 34 20 py bytes of zP4
1a346 75 70 20 74 6f 20 61 6e 64 20 69 6e 63 6c 75 64 up to and includ
1a347 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 ing the.** first
1a348 20 6e 75 6c 6c 20 62 79 74 65 2e 20 20 49 66 20 null byte. If
1a349 6e 3e 30 20 74 68 65 6e 20 63 6f 70 79 20 6e 2b n>0 then copy n+
1a34a 31 20 62 79 74 65 73 20 6f 66 20 7a 50 34 2e 0a 1 bytes of zP4..
1a34b 2a 2a 0a 2a 2a 20 49 66 20 6e 3d 3d 50 34 5f 4b **.** If n==P4_K
1a34c 45 59 49 4e 46 4f 20 69 74 20 6d 65 61 6e 73 20 EYINFO it means
1a34d 74 68 61 74 20 7a 50 34 20 69 73 20 61 20 70 6f that zP4 is a po
1a34e 69 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e inter to a KeyIn
1a34f 66 6f 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a fo structure..**
1a350 20 41 20 63 6f 70 79 20 69 73 20 6d 61 64 65 20 A copy is made
1a351 6f 66 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 of the KeyInfo s
1a352 74 72 75 63 74 75 72 65 20 69 6e 74 6f 20 6d 65 tructure into me
1a353 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 mory obtained fr
1a354 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 om.** sqlite3_ma
1a355 6c 6c 6f 63 2c 20 74 6f 20 62 65 20 66 72 65 65 lloc, to be free
1a356 64 20 77 68 65 6e 20 74 68 65 20 56 64 62 65 20 d when the Vdbe
1a357 69 73 20 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2a is finalized..**
1a358 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 n==P4_KEYINFO_H
1a359 41 4e 44 4f 46 46 20 69 6e 64 69 63 61 74 65 73 ANDOFF indicates
1a35a 20 74 68 61 74 20 7a 50 34 20 70 6f 69 6e 74 73 that zP4 points
1a35b 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 to a KeyInfo st
1a35c 72 75 63 74 75 72 65 0a 2a 2a 20 73 74 6f 72 65 ructure.** store
1a35d 64 20 69 6e 20 6d 65 6d 6f 72 79 20 74 68 61 74 d in memory that
1a35e 20 74 68 65 20 63 61 6c 6c 65 72 20 68 61 73 20 the caller has
1a35f 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 obtained from sq
1a360 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 2e 20 54 68 lite3_malloc. Th
1a361 65 20 0a 2a 2a 20 63 61 6c 6c 65 72 20 73 68 6f e .** caller sho
1a362 75 6c 64 20 6e 6f 74 20 66 72 65 65 20 74 68 65 uld not free the
1a363 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 74 20 allocation, it
1a364 77 69 6c 6c 20 62 65 20 66 72 65 65 64 20 77 68 will be freed wh
1a365 65 6e 20 74 68 65 20 56 64 62 65 20 69 73 0a 2a en the Vdbe is.*
1a366 2a 20 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2a 20 * finalized..**
1a367 0a 2a 2a 20 4f 74 68 65 72 20 76 61 6c 75 65 73 .** Other values
1a368 20 6f 66 20 6e 20 28 50 34 5f 53 54 41 54 49 43 of n (P4_STATIC
1a369 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 20 65 74 63 , P4_COLLSEQ etc
1a36a 2e 29 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 .) indicate that
1a36b 20 7a 50 34 20 70 6f 69 6e 74 73 0a 2a 2a 20 74 zP4 points.** t
1a36c 6f 20 61 20 73 74 72 69 6e 67 20 6f 72 20 73 74 o a string or st
1a36d 72 75 63 74 75 72 65 20 74 68 61 74 20 69 73 20 ructure that is
1a36e 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 65 78 guaranteed to ex
1a36f 69 73 74 20 66 6f 72 20 74 68 65 20 6c 69 66 65 ist for the life
1a370 74 69 6d 65 20 6f 66 0a 2a 2a 20 74 68 65 20 56 time of.** the V
1a371 64 62 65 2e 20 49 6e 20 74 68 65 73 65 20 63 61 dbe. In these ca
1a372 73 65 73 20 77 65 20 63 61 6e 20 6a 75 73 74 20 ses we can just
1a373 63 6f 70 79 20 74 68 65 20 70 6f 69 6e 74 65 72 copy the pointer
1a374 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 64 64 72 3c ..**.** If addr<
1a375 30 20 74 68 65 6e 20 63 68 61 6e 67 65 20 50 34 0 then change P4
1a376 20 6f 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 on the most rec
1a377 65 6e 74 6c 79 20 69 6e 73 65 72 74 65 64 20 69 ently inserted i
1a378 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 nstruction..*/.S
1a379 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1a37a 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 id sqlite3VdbeCh
1a37b 61 6e 67 65 50 34 28 56 64 62 65 20 2a 70 2c 20 angeP4(Vdbe *p,
1a37c 69 6e 74 20 61 64 64 72 2c 20 63 6f 6e 73 74 20 int addr, const
1a37d 63 68 61 72 20 2a 7a 50 34 2c 20 69 6e 74 20 6e char *zP4, int n
1a37e 29 7b 0a 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 ){. Op *pOp;.
1a37f 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 61 sqlite3 *db;. a
1a380 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 ssert( p!=0 );.
1a381 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 61 db = p->db;. a
1a382 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d ssert( p->magic=
1a383 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 =VDBE_MAGIC_INIT
1a384 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 4f 70 );. if( p->aOp
1a385 3d 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f ==0 || db->mallo
1a386 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 69 cFailed ){. i
1a387 66 20 28 20 6e 21 3d 50 34 5f 4b 45 59 49 4e 46 f ( n!=P4_KEYINF
1a388 4f 20 26 26 20 6e 21 3d 50 34 5f 56 54 41 42 20 O && n!=P4_VTAB
1a389 29 20 7b 0a 20 20 20 20 20 20 66 72 65 65 50 34 ) {. freeP4
1a38a 28 64 62 2c 20 6e 2c 20 28 76 6f 69 64 2a 29 2a (db, n, (void*)*
1a38b 28 63 68 61 72 2a 2a 29 26 7a 50 34 29 3b 0a 20 (char**)&zP4);.
1a38c 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 3b }. return;
1a38d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 . }. assert( p
1a38e 2d 3e 6e 4f 70 3e 30 20 29 3b 0a 20 20 61 73 73 ->nOp>0 );. ass
1a38f 65 72 74 28 20 61 64 64 72 3c 70 2d 3e 6e 4f 70 ert( addr<p->nOp
1a390 20 29 3b 0a 20 20 69 66 28 20 61 64 64 72 3c 30 );. if( addr<0
1a391 20 29 7b 0a 20 20 20 20 61 64 64 72 20 3d 20 70 ){. addr = p
1a392 2d 3e 6e 4f 70 20 2d 20 31 3b 0a 20 20 7d 0a 20 ->nOp - 1;. }.
1a393 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 61 pOp = &p->aOp[a
1a394 64 64 72 5d 3b 0a 20 20 66 72 65 65 50 34 28 64 ddr];. freeP4(d
1a395 62 2c 20 70 4f 70 2d 3e 70 34 74 79 70 65 2c 20 b, pOp->p4type,
1a396 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a 20 20 70 4f pOp->p4.p);. pO
1a397 70 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 20 69 p->p4.p = 0;. i
1a398 66 28 20 6e 3d 3d 50 34 5f 49 4e 54 33 32 20 29 f( n==P4_INT32 )
1a399 7b 0a 20 20 20 20 2f 2a 20 4e 6f 74 65 3a 20 74 {. /* Note: t
1a39a 68 69 73 20 63 61 73 74 20 69 73 20 73 61 66 65 his cast is safe
1a39b 2c 20 62 65 63 61 75 73 65 20 74 68 65 20 6f 72 , because the or
1a39c 69 67 69 6e 20 64 61 74 61 20 70 6f 69 6e 74 20 igin data point
1a39d 77 61 73 20 61 6e 20 69 6e 74 0a 20 20 20 20 2a was an int. *
1a39e 2a 20 74 68 61 74 20 77 61 73 20 63 61 73 74 20 * that was cast
1a39f 74 6f 20 61 20 28 63 6f 6e 73 74 20 63 68 61 72 to a (const char
1a3a0 20 2a 29 2e 20 2a 2f 0a 20 20 20 20 70 4f 70 2d *). */. pOp-
1a3a1 3e 70 34 2e 69 20 3d 20 53 51 4c 49 54 45 5f 50 >p4.i = SQLITE_P
1a3a2 54 52 5f 54 4f 5f 49 4e 54 28 7a 50 34 29 3b 0a TR_TO_INT(zP4);.
1a3a3 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 pOp->p4type
1a3a4 3d 20 50 34 5f 49 4e 54 33 32 3b 0a 20 20 7d 65 = P4_INT32;. }e
1a3a5 6c 73 65 20 69 66 28 20 7a 50 34 3d 3d 30 20 29 lse if( zP4==0 )
1a3a6 7b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 20 {. pOp->p4.p
1a3a7 3d 20 30 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 = 0;. pOp->p4
1a3a8 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 45 type = P4_NOTUSE
1a3a9 44 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e D;. }else if( n
1a3aa 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20 29 7b 0a ==P4_KEYINFO ){.
1a3ab 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 KeyInfo *pKe
1a3ac 79 49 6e 66 6f 3b 0a 20 20 20 20 69 6e 74 20 6e yInfo;. int n
1a3ad 46 69 65 6c 64 2c 20 6e 42 79 74 65 3b 0a 0a 20 Field, nByte;..
1a3ae 20 20 20 6e 46 69 65 6c 64 20 3d 20 28 28 4b 65 nField = ((Ke
1a3af 79 49 6e 66 6f 2a 29 7a 50 34 29 2d 3e 6e 46 69 yInfo*)zP4)->nFi
1a3b0 65 6c 64 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d eld;. nByte =
1a3b1 20 73 69 7a 65 6f 66 28 2a 70 4b 65 79 49 6e 66 sizeof(*pKeyInf
1a3b2 6f 29 20 2b 20 28 6e 46 69 65 6c 64 2d 31 29 2a o) + (nField-1)*
1a3b3 73 69 7a 65 6f 66 28 70 4b 65 79 49 6e 66 6f 2d sizeof(pKeyInfo-
1a3b4 3e 61 43 6f 6c 6c 5b 30 5d 29 20 2b 20 6e 46 69 >aColl[0]) + nFi
1a3b5 65 6c 64 3b 0a 20 20 20 20 70 4b 65 79 49 6e 66 eld;. pKeyInf
1a3b6 6f 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f o = sqlite3Mallo
1a3b7 63 28 20 6e 42 79 74 65 20 29 3b 0a 20 20 20 20 c( nByte );.
1a3b8 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f pOp->p4.pKeyInfo
1a3b9 20 3d 20 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 = pKeyInfo;.
1a3ba 20 69 66 28 20 70 4b 65 79 49 6e 66 6f 20 29 7b if( pKeyInfo ){
1a3bb 0a 20 20 20 20 20 20 75 38 20 2a 61 53 6f 72 74 . u8 *aSort
1a3bc 4f 72 64 65 72 3b 0a 20 20 20 20 20 20 6d 65 6d Order;. mem
1a3bd 63 70 79 28 70 4b 65 79 49 6e 66 6f 2c 20 7a 50 cpy(pKeyInfo, zP
1a3be 34 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 4, nByte);.
1a3bf 20 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 70 4b aSortOrder = pK
1a3c0 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 eyInfo->aSortOrd
1a3c1 65 72 3b 0a 20 20 20 20 20 20 69 66 28 20 61 53 er;. if( aS
1a3c2 6f 72 74 4f 72 64 65 72 20 29 7b 0a 20 20 20 20 ortOrder ){.
1a3c3 20 20 20 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 pKeyInfo->aS
1a3c4 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 6e 73 69 ortOrder = (unsi
1a3c5 67 6e 65 64 20 63 68 61 72 2a 29 26 70 4b 65 79 gned char*)&pKey
1a3c6 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 6e 46 69 65 Info->aColl[nFie
1a3c7 6c 64 5d 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d ld];. mem
1a3c8 63 70 79 28 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 cpy(pKeyInfo->aS
1a3c9 6f 72 74 4f 72 64 65 72 2c 20 61 53 6f 72 74 4f ortOrder, aSortO
1a3ca 72 64 65 72 2c 20 6e 46 69 65 6c 64 29 3b 0a 20 rder, nField);.
1a3cb 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4f 70 }. pOp
1a3cc 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 4b 45 ->p4type = P4_KE
1a3cd 59 49 4e 46 4f 3b 0a 20 20 20 20 7d 65 6c 73 65 YINFO;. }else
1a3ce 7b 0a 20 20 20 20 20 20 70 2d 3e 64 62 2d 3e 6d {. p->db->m
1a3cf 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b allocFailed = 1;
1a3d0 0a 20 20 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 . pOp->p4ty
1a3d1 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 45 44 3b pe = P4_NOTUSED;
1a3d2 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 . }. }else i
1a3d3 66 28 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f f( n==P4_KEYINFO
1a3d4 5f 48 41 4e 44 4f 46 46 20 29 7b 0a 20 20 20 20 _HANDOFF ){.
1a3d5 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 28 76 6f 69 pOp->p4.p = (voi
1a3d6 64 2a 29 7a 50 34 3b 0a 20 20 20 20 70 4f 70 2d d*)zP4;. pOp-
1a3d7 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 4b 45 59 >p4type = P4_KEY
1a3d8 49 4e 46 4f 3b 0a 20 20 7d 65 6c 73 65 20 69 66 INFO;. }else if
1a3d9 28 20 6e 3d 3d 50 34 5f 56 54 41 42 20 29 7b 0a ( n==P4_VTAB ){.
1a3da 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 pOp->p4.p =
1a3db 28 76 6f 69 64 2a 29 7a 50 34 3b 0a 20 20 20 20 (void*)zP4;.
1a3dc 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 pOp->p4type = P4
1a3dd 5f 56 54 41 42 3b 0a 20 20 20 20 73 71 6c 69 74 _VTAB;. sqlit
1a3de 65 33 56 74 61 62 4c 6f 63 6b 28 28 56 54 61 62 e3VtabLock((VTab
1a3df 6c 65 20 2a 29 7a 50 34 29 3b 0a 20 20 20 20 61 le *)zP4);. a
1a3e0 73 73 65 72 74 28 20 28 28 56 54 61 62 6c 65 20 ssert( ((VTable
1a3e1 2a 29 7a 50 34 29 2d 3e 64 62 3d 3d 70 2d 3e 64 *)zP4)->db==p->d
1a3e2 62 20 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 b );. }else if(
1a3e3 20 6e 3c 30 20 29 7b 0a 20 20 20 20 70 4f 70 2d n<0 ){. pOp-
1a3e4 3e 70 34 2e 70 20 3d 20 28 76 6f 69 64 2a 29 7a >p4.p = (void*)z
1a3e5 50 34 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 P4;. pOp->p4t
1a3e6 79 70 65 20 3d 20 28 73 69 67 6e 65 64 20 63 68 ype = (signed ch
1a3e7 61 72 29 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 ar)n;. }else{.
1a3e8 20 20 20 69 66 28 20 6e 3d 3d 30 20 29 20 6e 20 if( n==0 ) n
1a3e9 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 = sqlite3Strlen3
1a3ea 30 28 7a 50 34 29 3b 0a 20 20 20 20 70 4f 70 2d 0(zP4);. pOp-
1a3eb 3e 70 34 2e 7a 20 3d 20 73 71 6c 69 74 65 33 44 >p4.z = sqlite3D
1a3ec 62 53 74 72 4e 44 75 70 28 70 2d 3e 64 62 2c 20 bStrNDup(p->db,
1a3ed 7a 50 34 2c 20 6e 29 3b 0a 20 20 20 20 70 4f 70 zP4, n);. pOp
1a3ee 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 44 59 ->p4type = P4_DY
1a3ef 4e 41 4d 49 43 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 NAMIC;. }.}..#i
1a3f0 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a fndef NDEBUG./*.
1a3f1 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 63 6f ** Change the co
1a3f2 6d 6d 65 6e 74 20 6f 6e 20 74 68 65 20 74 68 65 mment on the the
1a3f3 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 63 most recently c
1a3f4 6f 64 65 64 20 69 6e 73 74 72 75 63 74 69 6f 6e oded instruction
1a3f5 2e 20 20 4f 72 0a 2a 2a 20 69 6e 73 65 72 74 20 . Or.** insert
1a3f6 61 20 4e 6f 2d 6f 70 20 61 6e 64 20 61 64 64 20 a No-op and add
1a3f7 74 68 65 20 63 6f 6d 6d 65 6e 74 20 74 6f 20 74 the comment to t
1a3f8 68 61 74 20 6e 65 77 20 69 6e 73 74 72 75 63 74 hat new instruct
1a3f9 69 6f 6e 2e 20 20 54 68 69 73 0a 2a 2a 20 6d 61 ion. This.** ma
1a3fa 6b 65 73 20 74 68 65 20 63 6f 64 65 20 65 61 73 kes the code eas
1a3fb 69 65 72 20 74 6f 20 72 65 61 64 20 64 75 72 69 ier to read duri
1a3fc 6e 67 20 64 65 62 75 67 67 69 6e 67 2e 20 20 4e ng debugging. N
1a3fd 6f 6e 65 20 6f 66 20 74 68 69 73 20 68 61 70 70 one of this happ
1a3fe 65 6e 73 0a 2a 2a 20 69 6e 20 61 20 70 72 6f 64 ens.** in a prod
1a3ff 75 63 74 69 6f 6e 20 62 75 69 6c 64 2e 0a 2a 2f uction build..*/
1a400 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1a401 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
1a402 43 6f 6d 6d 65 6e 74 28 56 64 62 65 20 2a 70 2c Comment(Vdbe *p,
1a403 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f const char *zFo
1a404 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 rmat, ...){. va
1a405 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69 66 28 20 _list ap;. if(
1a406 21 70 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 !p ) return;. a
1a407 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 ssert( p->nOp>0
1a408 7c 7c 20 70 2d 3e 61 4f 70 3d 3d 30 20 29 3b 0a || p->aOp==0 );.
1a409 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 4f 70 assert( p->aOp
1a40a 3d 3d 30 20 7c 7c 20 70 2d 3e 61 4f 70 5b 70 2d ==0 || p->aOp[p-
1a40b 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 >nOp-1].zComment
1a40c 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 2d 3e 6d 61 ==0 || p->db->ma
1a40d 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 llocFailed );.
1a40e 69 66 28 20 70 2d 3e 6e 4f 70 20 29 7b 0a 20 20 if( p->nOp ){.
1a40f 20 20 63 68 61 72 20 2a 2a 70 7a 20 3d 20 26 70 char **pz = &p
1a410 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e ->aOp[p->nOp-1].
1a411 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 20 20 20 76 61 zComment;. va
1a412 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d _start(ap, zForm
1a413 61 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 at);. sqlite3
1a414 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 2a 70 DbFree(p->db, *p
1a415 7a 29 3b 0a 20 20 20 20 2a 70 7a 20 3d 20 73 71 z);. *pz = sq
1a416 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 70 2d lite3VMPrintf(p-
1a417 3e 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 >db, zFormat, ap
1a418 29 3b 0a 20 20 20 20 76 61 5f 65 6e 64 28 61 70 );. va_end(ap
1a419 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f );. }.}.SQLITE_
1a41a 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1a41b 69 74 65 33 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d ite3VdbeNoopComm
1a41c 65 6e 74 28 56 64 62 65 20 2a 70 2c 20 63 6f 6e ent(Vdbe *p, con
1a41d 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 st char *zFormat
1a41e 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 , ...){. va_lis
1a41f 74 20 61 70 3b 0a 20 20 69 66 28 20 21 70 20 29 t ap;. if( !p )
1a420 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 return;. sqlit
1a421 65 33 56 64 62 65 41 64 64 4f 70 30 28 70 2c 20 e3VdbeAddOp0(p,
1a422 4f 50 5f 4e 6f 6f 70 29 3b 0a 20 20 61 73 73 65 OP_Noop);. asse
1a423 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 7c 7c 20 rt( p->nOp>0 ||
1a424 70 2d 3e 61 4f 70 3d 3d 30 20 29 3b 0a 20 20 61 p->aOp==0 );. a
1a425 73 73 65 72 74 28 20 70 2d 3e 61 4f 70 3d 3d 30 ssert( p->aOp==0
1a426 20 7c 7c 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f || p->aOp[p->nO
1a427 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3d 3d 30 p-1].zComment==0
1a428 20 7c 7c 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f || p->db->mallo
1a429 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 cFailed );. if(
1a42a 20 70 2d 3e 6e 4f 70 20 29 7b 0a 20 20 20 20 63 p->nOp ){. c
1a42b 68 61 72 20 2a 2a 70 7a 20 3d 20 26 70 2d 3e 61 har **pz = &p->a
1a42c 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f Op[p->nOp-1].zCo
1a42d 6d 6d 65 6e 74 3b 0a 20 20 20 20 76 61 5f 73 74 mment;. va_st
1a42e 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 art(ap, zFormat)
1a42f 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 ;. sqlite3DbF
1a430 72 65 65 28 70 2d 3e 64 62 2c 20 2a 70 7a 29 3b ree(p->db, *pz);
1a431 0a 20 20 20 20 2a 70 7a 20 3d 20 73 71 6c 69 74 . *pz = sqlit
1a432 65 33 56 4d 50 72 69 6e 74 66 28 70 2d 3e 64 62 e3VMPrintf(p->db
1a433 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a , zFormat, ap);.
1a434 20 20 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a va_end(ap);.
1a435 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 20 2f 2a }.}.#endif /*
1a436 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a NDEBUG */../*.*
1a437 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6f 70 63 * Return the opc
1a438 6f 64 65 20 66 6f 72 20 61 20 67 69 76 65 6e 20 ode for a given
1a439 61 64 64 72 65 73 73 2e 20 20 49 66 20 74 68 65 address. If the
1a43a 20 61 64 64 72 65 73 73 20 69 73 20 2d 31 2c 20 address is -1,
1a43b 74 68 65 6e 0a 2a 2a 20 72 65 74 75 72 6e 20 74 then.** return t
1a43c 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 he most recently
1a43d 20 69 6e 73 65 72 74 65 64 20 6f 70 63 6f 64 65 inserted opcode
1a43e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d ..**.** If a mem
1a43f 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 ory allocation e
1a440 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 rror has occurre
1a441 64 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 63 d prior to the c
1a442 61 6c 6c 69 6e 67 20 6f 66 20 74 68 69 73 0a 2a alling of this.*
1a443 2a 20 72 6f 75 74 69 6e 65 2c 20 74 68 65 6e 20 * routine, then
1a444 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 64 a pointer to a d
1a445 75 6d 6d 79 20 56 64 62 65 4f 70 20 77 69 6c 6c ummy VdbeOp will
1a446 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 20 54 be returned. T
1a447 68 61 74 20 6f 70 63 6f 64 65 0a 2a 2a 20 69 73 hat opcode.** is
1a448 20 72 65 61 64 61 62 6c 65 20 61 6e 64 20 77 72 readable and wr
1a449 69 74 61 62 6c 65 2c 20 62 75 74 20 69 74 20 68 itable, but it h
1a44a 61 73 20 6e 6f 20 65 66 66 65 63 74 2e 20 20 54 as no effect. T
1a44b 68 65 20 72 65 74 75 72 6e 20 6f 66 20 61 20 64 he return of a d
1a44c 75 6d 6d 79 0a 2a 2a 20 6f 70 63 6f 64 65 20 61 ummy.** opcode a
1a44d 6c 6c 6f 77 73 20 74 68 65 20 63 61 6c 6c 20 74 llows the call t
1a44e 6f 20 63 6f 6e 74 69 6e 75 65 20 66 75 6e 63 74 o continue funct
1a44f 69 6f 6e 69 6e 67 20 61 66 74 65 72 20 61 20 4f ioning after a O
1a450 4f 4d 20 66 61 75 6c 74 20 77 69 74 68 6f 75 74 OM fault without
1a451 0a 2a 2a 20 68 61 76 69 6e 67 20 74 6f 20 63 68 .** having to ch
1a452 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 eck to see if th
1a453 65 20 72 65 74 75 72 6e 20 66 72 6f 6d 20 74 68 e return from th
1a454 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 is routine is a
1a455 76 61 6c 69 64 20 70 6f 69 6e 74 65 72 2e 0a 2a valid pointer..*
1a456 2a 0a 2a 2a 20 41 62 6f 75 74 20 74 68 65 20 23 *.** About the #
1a457 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ifdef SQLITE_OMI
1a458 54 5f 54 52 41 43 45 3a 20 20 4e 6f 72 6d 61 6c T_TRACE: Normal
1a459 6c 79 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 ly, this routine
1a45a 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 is never called
1a45b 0a 2a 2a 20 75 6e 6c 65 73 73 20 70 2d 3e 6e 4f .** unless p->nO
1a45c 70 3e 30 2e 20 20 54 68 69 73 20 69 73 20 62 65 p>0. This is be
1a45d 63 61 75 73 65 20 69 6e 20 74 68 65 20 61 62 73 cause in the abs
1a45e 65 6e 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f ense of SQLITE_O
1a45f 4d 49 54 5f 54 52 41 43 45 2c 0a 2a 2a 20 61 6e MIT_TRACE,.** an
1a460 20 4f 50 5f 54 72 61 63 65 20 69 6e 73 74 72 75 OP_Trace instru
1a461 63 74 69 6f 6e 20 69 73 20 61 6c 77 61 79 73 20 ction is always
1a462 69 6e 73 65 72 74 65 64 20 62 79 20 73 71 6c 69 inserted by sqli
1a463 74 65 33 56 64 62 65 47 65 74 28 29 20 61 73 20 te3VdbeGet() as
1a464 73 6f 6f 6e 20 61 73 0a 2a 2a 20 61 20 6e 65 77 soon as.** a new
1a465 20 56 44 42 45 20 69 73 20 63 72 65 61 74 65 64 VDBE is created
1a466 2e 20 20 53 6f 20 77 65 20 61 72 65 20 66 72 65 . So we are fre
1a467 65 20 74 6f 20 73 65 74 20 61 64 64 72 20 74 6f e to set addr to
1a468 20 70 2d 3e 6e 4f 70 2d 31 20 77 69 74 68 6f 75 p->nOp-1 withou
1a469 74 0a 2a 2a 20 68 61 76 69 6e 67 20 74 6f 20 64 t.** having to d
1a46a 6f 75 62 6c 65 2d 63 68 65 63 6b 20 74 6f 20 6d ouble-check to m
1a46b 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68 ake sure that th
1a46c 65 20 72 65 73 75 6c 74 20 69 73 20 6e 6f 6e 2d e result is non-
1a46d 6e 65 67 61 74 69 76 65 2e 20 42 75 74 0a 2a 2a negative. But.**
1a46e 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f if SQLITE_OMIT_
1a46f 54 52 41 43 45 20 69 73 20 64 65 66 69 6e 65 64 TRACE is defined
1a470 2c 20 74 68 65 20 4f 50 5f 54 72 61 63 65 20 69 , the OP_Trace i
1a471 73 20 6f 6d 69 74 74 65 64 20 61 6e 64 20 77 65 s omitted and we
1a472 20 64 6f 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 63 do need to.** c
1a473 68 65 63 6b 20 74 68 65 20 76 61 6c 75 65 20 6f heck the value o
1a474 66 20 70 2d 3e 6e 4f 70 2d 31 20 62 65 66 6f 72 f p->nOp-1 befor
1a475 65 20 63 6f 6e 74 69 6e 75 69 6e 67 2e 0a 2a 2f e continuing..*/
1a476 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1a477 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 65 33 56 VdbeOp *sqlite3V
1a478 64 62 65 47 65 74 4f 70 28 56 64 62 65 20 2a 70 dbeGetOp(Vdbe *p
1a479 2c 20 69 6e 74 20 61 64 64 72 29 7b 0a 20 20 73 , int addr){. s
1a47a 74 61 74 69 63 20 56 64 62 65 4f 70 20 64 75 6d tatic VdbeOp dum
1a47b 6d 79 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d my;. assert( p-
1a47c 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 >magic==VDBE_MAG
1a47d 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 IC_INIT );. if(
1a47e 20 61 64 64 72 3c 30 20 29 7b 0a 23 69 66 64 65 addr<0 ){.#ifde
1a47f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 f SQLITE_OMIT_TR
1a480 41 43 45 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e ACE. if( p->n
1a481 4f 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 26 Op==0 ) return &
1a482 64 75 6d 6d 79 3b 0a 23 65 6e 64 69 66 0a 20 20 dummy;.#endif.
1a483 20 20 61 64 64 72 20 3d 20 70 2d 3e 6e 4f 70 20 addr = p->nOp
1a484 2d 20 31 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 - 1;. }. asser
1a485 74 28 20 28 61 64 64 72 3e 3d 30 20 26 26 20 61 t( (addr>=0 && a
1a486 64 64 72 3c 70 2d 3e 6e 4f 70 29 20 7c 7c 20 70 ddr<p->nOp) || p
1a487 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ->db->mallocFail
1a488 65 64 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 64 ed );. if( p->d
1a489 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1a48a 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 64 ){. return &d
1a48b 75 6d 6d 79 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 ummy;. }else{.
1a48c 20 20 20 72 65 74 75 72 6e 20 26 70 2d 3e 61 4f return &p->aO
1a48d 70 5b 61 64 64 72 5d 3b 0a 20 20 7d 0a 7d 0a 0a p[addr];. }.}..
1a48e 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c #if !defined(SQL
1a48f 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e ITE_OMIT_EXPLAIN
1a490 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 4e 44 ) || !defined(ND
1a491 45 42 55 47 29 20 5c 0a 20 20 20 20 20 7c 7c 20 EBUG) \. ||
1a492 64 65 66 69 6e 65 64 28 56 44 42 45 5f 50 52 4f defined(VDBE_PRO
1a493 46 49 4c 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 FILE) || defined
1a494 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f (SQLITE_DEBUG)./
1a495 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 61 20 73 *.** Compute a s
1a496 74 72 69 6e 67 20 74 68 61 74 20 64 65 73 63 72 tring that descr
1a497 69 62 65 73 20 74 68 65 20 50 34 20 70 61 72 61 ibes the P4 para
1a498 6d 65 74 65 72 20 66 6f 72 20 61 6e 20 6f 70 63 meter for an opc
1a499 6f 64 65 2e 0a 2a 2a 20 55 73 65 20 7a 54 65 6d ode..** Use zTem
1a49a 70 20 66 6f 72 20 61 6e 79 20 72 65 71 75 69 72 p for any requir
1a49b 65 64 20 74 65 6d 70 6f 72 61 72 79 20 62 75 66 ed temporary buf
1a49c 66 65 72 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 fer space..*/.st
1a49d 61 74 69 63 20 63 68 61 72 20 2a 64 69 73 70 6c atic char *displ
1a49e 61 79 50 34 28 4f 70 20 2a 70 4f 70 2c 20 63 68 ayP4(Op *pOp, ch
1a49f 61 72 20 2a 7a 54 65 6d 70 2c 20 69 6e 74 20 6e ar *zTemp, int n
1a4a0 54 65 6d 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a Temp){. char *z
1a4a1 50 34 20 3d 20 7a 54 65 6d 70 3b 0a 20 20 61 73 P4 = zTemp;. as
1a4a2 73 65 72 74 28 20 6e 54 65 6d 70 3e 3d 32 30 20 sert( nTemp>=20
1a4a3 29 3b 0a 20 20 73 77 69 74 63 68 28 20 70 4f 70 );. switch( pOp
1a4a4 2d 3e 70 34 74 79 70 65 20 29 7b 0a 20 20 20 20 ->p4type ){.
1a4a5 63 61 73 65 20 50 34 5f 4b 45 59 49 4e 46 4f 5f case P4_KEYINFO_
1a4a6 53 54 41 54 49 43 3a 0a 20 20 20 20 63 61 73 65 STATIC:. case
1a4a7 20 50 34 5f 4b 45 59 49 4e 46 4f 3a 20 7b 0a 20 P4_KEYINFO: {.
1a4a8 20 20 20 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 int i, j;.
1a4a9 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b KeyInfo *pK
1a4aa 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 eyInfo = pOp->p4
1a4ab 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 .pKeyInfo;.
1a4ac 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
1a4ad 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 f(nTemp, zTemp,
1a4ae 22 6b 65 79 69 6e 66 6f 28 25 64 22 2c 20 70 4b "keyinfo(%d", pK
1a4af 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 29 3b eyInfo->nField);
1a4b0 0a 20 20 20 20 20 20 69 20 3d 20 73 71 6c 69 74 . i = sqlit
1a4b1 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 65 6d 70 e3Strlen30(zTemp
1a4b2 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 );. for(j=0
1a4b3 3b 20 6a 3c 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 ; j<pKeyInfo->nF
1a4b4 69 65 6c 64 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 ield; j++){.
1a4b5 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f CollSeq *pCo
1a4b6 6c 6c 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 ll = pKeyInfo->a
1a4b7 43 6f 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 20 20 20 Coll[j];.
1a4b8 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 if( pColl ){.
1a4b9 20 20 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 int n =
1a4ba 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
1a4bb 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 pColl->zName);.
1a4bc 20 20 20 20 20 20 20 20 20 69 66 28 20 69 2b 6e if( i+n
1a4bd 3e 6e 54 65 6d 70 2d 36 20 29 7b 0a 20 20 20 20 >nTemp-6 ){.
1a4be 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 memcpy(&
1a4bf 7a 54 65 6d 70 5b 69 5d 2c 22 2c 2e 2e 2e 22 2c zTemp[i],",...",
1a4c0 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 4);.
1a4c1 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 break;.
1a4c2 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7a 54 65 }. zTe
1a4c3 6d 70 5b 69 2b 2b 5d 20 3d 20 27 2c 27 3b 0a 20 mp[i++] = ',';.
1a4c4 20 20 20 20 20 20 20 20 20 69 66 28 20 70 4b 65 if( pKe
1a4c5 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 yInfo->aSortOrde
1a4c6 72 20 26 26 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 r && pKeyInfo->a
1a4c7 53 6f 72 74 4f 72 64 65 72 5b 6a 5d 20 29 7b 0a SortOrder[j] ){.
1a4c8 20 20 20 20 20 20 20 20 20 20 20 20 7a 54 65 6d zTem
1a4c9 70 5b 69 2b 2b 5d 20 3d 20 27 2d 27 3b 0a 20 20 p[i++] = '-';.
1a4ca 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1a4cb 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 54 65 6d memcpy(&zTem
1a4cc 70 5b 69 5d 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 p[i], pColl->zNa
1a4cd 6d 65 2c 6e 2b 31 29 3b 0a 20 20 20 20 20 20 20 me,n+1);.
1a4ce 20 20 20 69 20 2b 3d 20 6e 3b 0a 20 20 20 20 20 i += n;.
1a4cf 20 20 20 7d 65 6c 73 65 20 69 66 28 20 69 2b 34 }else if( i+4
1a4d0 3c 6e 54 65 6d 70 2d 36 20 29 7b 0a 20 20 20 20 <nTemp-6 ){.
1a4d1 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 54 memcpy(&zT
1a4d2 65 6d 70 5b 69 5d 2c 22 2c 6e 69 6c 22 2c 34 29 emp[i],",nil",4)
1a4d3 3b 0a 20 20 20 20 20 20 20 20 20 20 69 20 2b 3d ;. i +=
1a4d4 20 34 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 4;. }.
1a4d5 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 54 65 6d }. zTem
1a4d6 70 5b 69 2b 2b 5d 20 3d 20 27 29 27 3b 0a 20 20 p[i++] = ')';.
1a4d7 20 20 20 20 7a 54 65 6d 70 5b 69 5d 20 3d 20 30 zTemp[i] = 0
1a4d8 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1a4d9 69 3c 6e 54 65 6d 70 20 29 3b 0a 20 20 20 20 20 i<nTemp );.
1a4da 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
1a4db 20 20 63 61 73 65 20 50 34 5f 43 4f 4c 4c 53 45 case P4_COLLSE
1a4dc 51 3a 20 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 Q: {. CollS
1a4dd 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 70 4f 70 2d eq *pColl = pOp-
1a4de 3e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 >p4.pColl;.
1a4df 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
1a4e0 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 f(nTemp, zTemp,
1a4e1 22 63 6f 6c 6c 73 65 71 28 25 2e 32 30 73 29 22 "collseq(%.20s)"
1a4e2 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b , pColl->zName);
1a4e3 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1a4e4 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f }. case P4_
1a4e5 46 55 4e 43 44 45 46 3a 20 7b 0a 20 20 20 20 20 FUNCDEF: {.
1a4e6 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 20 3d FuncDef *pDef =
1a4e7 20 70 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b 0a pOp->p4.pFunc;.
1a4e8 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e sqlite3_sn
1a4e9 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 printf(nTemp, zT
1a4ea 65 6d 70 2c 20 22 25 73 28 25 64 29 22 2c 20 70 emp, "%s(%d)", p
1a4eb 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20 70 44 65 66 Def->zName, pDef
1a4ec 2d 3e 6e 41 72 67 29 3b 0a 20 20 20 20 20 20 62 ->nArg);. b
1a4ed 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
1a4ee 63 61 73 65 20 50 34 5f 49 4e 54 36 34 3a 20 7b case P4_INT64: {
1a4ef 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 . sqlite3_s
1a4f0 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a nprintf(nTemp, z
1a4f1 54 65 6d 70 2c 20 22 25 6c 6c 64 22 2c 20 2a 70 Temp, "%lld", *p
1a4f2 4f 70 2d 3e 70 34 2e 70 49 36 34 29 3b 0a 20 20 Op->p4.pI64);.
1a4f3 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1a4f4 0a 20 20 20 20 63 61 73 65 20 50 34 5f 49 4e 54 . case P4_INT
1a4f5 33 32 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 32: {. sqli
1a4f6 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 te3_snprintf(nTe
1a4f7 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 64 22 2c mp, zTemp, "%d",
1a4f8 20 70 4f 70 2d 3e 70 34 2e 69 29 3b 0a 20 20 20 pOp->p4.i);.
1a4f9 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
1a4fa 20 20 20 20 63 61 73 65 20 50 34 5f 52 45 41 4c case P4_REAL
1a4fb 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 : {. sqlite
1a4fc 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 3_snprintf(nTemp
1a4fd 2c 20 7a 54 65 6d 70 2c 20 22 25 2e 31 36 67 22 , zTemp, "%.16g"
1a4fe 2c 20 2a 70 4f 70 2d 3e 70 34 2e 70 52 65 61 6c , *pOp->p4.pReal
1a4ff 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
1a500 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 }. case P
1a501 34 5f 4d 45 4d 3a 20 7b 0a 20 20 20 20 20 20 4d 4_MEM: {. M
1a502 65 6d 20 2a 70 4d 65 6d 20 3d 20 70 4f 70 2d 3e em *pMem = pOp->
1a503 70 34 2e 70 4d 65 6d 3b 0a 20 20 20 20 20 20 61 p4.pMem;. a
1a504 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c ssert( (pMem->fl
1a505 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d ags & MEM_Null)=
1a506 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 =0 );. if(
1a507 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pMem->flags & ME
1a508 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 20 20 20 M_Str ){.
1a509 20 7a 50 34 20 3d 20 70 4d 65 6d 2d 3e 7a 3b 0a zP4 = pMem->z;.
1a50a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
1a50b 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pMem->flags & ME
1a50c 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 M_Int ){.
1a50d 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
1a50e 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 f(nTemp, zTemp,
1a50f 22 25 6c 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75 2e "%lld", pMem->u.
1a510 69 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 i);. }else
1a511 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 if( pMem->flags
1a512 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 & MEM_Real ){.
1a513 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e sqlite3_sn
1a514 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 printf(nTemp, zT
1a515 65 6d 70 2c 20 22 25 2e 31 36 67 22 2c 20 70 4d emp, "%.16g", pM
1a516 65 6d 2d 3e 72 29 3b 0a 20 20 20 20 20 20 7d 65 em->r);. }e
1a517 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 lse{. ass
1a518 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 ert( pMem->flags
1a519 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 & MEM_Blob );.
1a51a 20 20 20 20 20 20 20 7a 50 34 20 3d 20 22 28 62 zP4 = "(b
1a51b 6c 6f 62 29 22 3b 0a 20 20 20 20 20 20 7d 0a 20 lob)";. }.
1a51c 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1a51d 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 }.#ifndef SQLITE
1a51e 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
1a51f 4c 45 0a 20 20 20 20 63 61 73 65 20 50 34 5f 56 LE. case P4_V
1a520 54 41 42 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c TAB: {. sql
1a521 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 ite3_vtab *pVtab
1a522 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 = pOp->p4.pVtab
1a523 2d 3e 70 56 74 61 62 3b 0a 20 20 20 20 20 20 73 ->pVtab;. s
1a524 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
1a525 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 76 nTemp, zTemp, "v
1a526 74 61 62 3a 25 70 3a 25 70 22 2c 20 70 56 74 61 tab:%p:%p", pVta
1a527 62 2c 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c b, pVtab->pModul
1a528 65 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b e);. break;
1a529 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 . }.#endif.
1a52a 20 20 63 61 73 65 20 50 34 5f 49 4e 54 41 52 52 case P4_INTARR
1a52b 41 59 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 AY: {. sqli
1a52c 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 te3_snprintf(nTe
1a52d 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 69 6e 74 61 mp, zTemp, "inta
1a52e 72 72 61 79 22 29 3b 0a 20 20 20 20 20 20 62 72 rray");. br
1a52f 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 eak;. }. c
1a530 61 73 65 20 50 34 5f 53 55 42 50 52 4f 47 52 41 ase P4_SUBPROGRA
1a531 4d 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 M: {. sqlit
1a532 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d e3_snprintf(nTem
1a533 70 2c 20 7a 54 65 6d 70 2c 20 22 70 72 6f 67 72 p, zTemp, "progr
1a534 61 6d 22 29 3b 0a 20 20 20 20 20 20 62 72 65 61 am");. brea
1a535 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 k;. }. def
1a536 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 7a 50 ault: {. zP
1a537 34 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 4 = pOp->p4.z;.
1a538 20 20 20 20 20 69 66 28 20 7a 50 34 3d 3d 30 20 if( zP4==0
1a539 29 7b 0a 20 20 20 20 20 20 20 20 7a 50 34 20 3d ){. zP4 =
1a53a 20 7a 54 65 6d 70 3b 0a 20 20 20 20 20 20 20 20 zTemp;.
1a53b 7a 54 65 6d 70 5b 30 5d 20 3d 20 30 3b 0a 20 20 zTemp[0] = 0;.
1a53c 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
1a53d 20 20 61 73 73 65 72 74 28 20 7a 50 34 21 3d 30 assert( zP4!=0
1a53e 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 50 34 );. return zP4
1a53f 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ;.}.#endif../*.*
1a540 2a 20 44 65 63 6c 61 72 65 20 74 6f 20 74 68 65 * Declare to the
1a541 20 56 64 62 65 20 74 68 61 74 20 74 68 65 20 42 Vdbe that the B
1a542 54 72 65 65 20 6f 62 6a 65 63 74 20 61 74 20 64 Tree object at d
1a543 62 2d 3e 61 44 62 5b 69 5d 20 69 73 20 75 73 65 b->aDb[i] is use
1a544 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
1a545 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
1a546 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28 56 3VdbeUsesBtree(V
1a547 64 62 65 20 2a 70 2c 20 69 6e 74 20 69 29 7b 0a dbe *p, int i){.
1a548 20 20 69 6e 74 20 6d 61 73 6b 3b 0a 20 20 61 73 int mask;. as
1a549 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c sert( i>=0 && i<
1a54a 70 2d 3e 64 62 2d 3e 6e 44 62 20 26 26 20 69 3c p->db->nDb && i<
1a54b 73 69 7a 65 6f 66 28 75 33 32 29 2a 38 20 29 3b sizeof(u32)*8 );
1a54c 0a 20 20 61 73 73 65 72 74 28 20 69 3c 28 69 6e . assert( i<(in
1a54d 74 29 73 69 7a 65 6f 66 28 70 2d 3e 62 74 72 65 t)sizeof(p->btre
1a54e 65 4d 61 73 6b 29 2a 38 20 29 3b 0a 20 20 6d 61 eMask)*8 );. ma
1a54f 73 6b 20 3d 20 28 28 75 33 32 29 31 29 3c 3c 69 sk = ((u32)1)<<i
1a550 3b 0a 20 20 69 66 28 20 28 70 2d 3e 62 74 72 65 ;. if( (p->btre
1a551 65 4d 61 73 6b 20 26 20 6d 61 73 6b 29 3d 3d 30 eMask & mask)==0
1a552 20 29 7b 0a 20 20 20 20 70 2d 3e 62 74 72 65 65 ){. p->btree
1a553 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b 3b 0a 20 20 Mask |= mask;.
1a554 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 sqlite3BtreeMu
1a555 74 65 78 41 72 72 61 79 49 6e 73 65 72 74 28 26 texArrayInsert(&
1a556 70 2d 3e 61 4d 75 74 65 78 2c 20 70 2d 3e 64 62 p->aMutex, p->db
1a557 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 3b 0a 20 ->aDb[i].pBt);.
1a558 20 7d 0a 7d 0a 0a 0a 23 69 66 20 64 65 66 69 6e }.}...#if defin
1a559 65 64 28 56 44 42 45 5f 50 52 4f 46 49 4c 45 29 ed(VDBE_PROFILE)
1a55a 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 || defined(SQLI
1a55b 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 TE_DEBUG)./*.**
1a55c 50 72 69 6e 74 20 61 20 73 69 6e 67 6c 65 20 6f Print a single o
1a55d 70 63 6f 64 65 2e 20 20 54 68 69 73 20 72 6f 75 pcode. This rou
1a55e 74 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f 72 tine is used for
1a55f 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e debugging only.
1a560 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1a561 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
1a562 64 62 65 50 72 69 6e 74 4f 70 28 46 49 4c 45 20 dbePrintOp(FILE
1a563 2a 70 4f 75 74 2c 20 69 6e 74 20 70 63 2c 20 4f *pOut, int pc, O
1a564 70 20 2a 70 4f 70 29 7b 0a 20 20 63 68 61 72 20 p *pOp){. char
1a565 2a 7a 50 34 3b 0a 20 20 63 68 61 72 20 7a 50 74 *zP4;. char zPt
1a566 72 5b 35 30 5d 3b 0a 20 20 73 74 61 74 69 63 20 r[50];. static
1a567 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 const char *zFor
1a568 6d 61 74 31 20 3d 20 22 25 34 64 20 25 2d 31 33 mat1 = "%4d %-13
1a569 73 20 25 34 64 20 25 34 64 20 25 34 64 20 25 2d s %4d %4d %4d %-
1a56a 34 73 20 25 2e 32 58 20 25 73 5c 6e 22 3b 0a 20 4s %.2X %s\n";.
1a56b 20 69 66 28 20 70 4f 75 74 3d 3d 30 20 29 20 70 if( pOut==0 ) p
1a56c 4f 75 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20 Out = stdout;.
1a56d 7a 50 34 20 3d 20 64 69 73 70 6c 61 79 50 34 28 zP4 = displayP4(
1a56e 70 4f 70 2c 20 7a 50 74 72 2c 20 73 69 7a 65 6f pOp, zPtr, sizeo
1a56f 66 28 7a 50 74 72 29 29 3b 0a 20 20 66 70 72 69 f(zPtr));. fpri
1a570 6e 74 66 28 70 4f 75 74 2c 20 7a 46 6f 72 6d 61 ntf(pOut, zForma
1a571 74 31 2c 20 70 63 2c 20 0a 20 20 20 20 20 20 73 t1, pc, . s
1a572 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65 qlite3OpcodeName
1a573 28 70 4f 70 2d 3e 6f 70 63 6f 64 65 29 2c 20 70 (pOp->opcode), p
1a574 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 32 2c Op->p1, pOp->p2,
1a575 20 70 4f 70 2d 3e 70 33 2c 20 7a 50 34 2c 20 70 pOp->p3, zP4, p
1a576 4f 70 2d 3e 70 35 2c 0a 23 69 66 64 65 66 20 53 Op->p5,.#ifdef S
1a577 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 QLITE_DEBUG.
1a578 20 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 pOp->zComment
1a579 3f 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 ? pOp->zComment
1a57a 3a 20 22 22 0a 23 65 6c 73 65 0a 20 20 20 20 20 : "".#else.
1a57b 20 22 22 0a 23 65 6e 64 69 66 0a 20 20 29 3b 0a "".#endif. );.
1a57c 20 20 66 66 6c 75 73 68 28 70 4f 75 74 29 3b 0a fflush(pOut);.
1a57d 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
1a57e 52 65 6c 65 61 73 65 20 61 6e 20 61 72 72 61 79 Release an array
1a57f 20 6f 66 20 4e 20 4d 65 6d 20 65 6c 65 6d 65 6e of N Mem elemen
1a580 74 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 ts.*/.static voi
1a581 64 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 d releaseMemArra
1a582 79 28 4d 65 6d 20 2a 70 2c 20 69 6e 74 20 4e 29 y(Mem *p, int N)
1a583 7b 0a 20 20 69 66 28 20 70 20 26 26 20 4e 20 29 {. if( p && N )
1a584 7b 0a 20 20 20 20 4d 65 6d 20 2a 70 45 6e 64 3b {. Mem *pEnd;
1a585 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 . sqlite3 *db
1a586 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 75 38 = p->db;. u8
1a587 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 20 3d malloc_failed =
1a588 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
1a589 64 3b 0a 20 20 20 20 66 6f 72 28 70 45 6e 64 3d d;. for(pEnd=
1a58a 26 70 5b 4e 5d 3b 20 70 3c 70 45 6e 64 3b 20 70 &p[N]; p<pEnd; p
1a58b 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 ++){. asser
1a58c 74 28 20 28 26 70 5b 31 5d 29 3d 3d 70 45 6e 64 t( (&p[1])==pEnd
1a58d 20 7c 7c 20 70 5b 30 5d 2e 64 62 3d 3d 70 5b 31 || p[0].db==p[1
1a58e 5d 2e 64 62 20 29 3b 0a 0a 20 20 20 20 20 20 2f ].db );.. /
1a58f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 69 73 20 * This block is
1a590 72 65 61 6c 6c 79 20 61 6e 20 69 6e 6c 69 6e 65 really an inline
1a591 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 73 71 6c d version of sql
1a592 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
1a593 73 65 28 29 0a 20 20 20 20 20 20 2a 2a 20 74 68 se(). ** th
1a594 61 74 20 74 61 6b 65 73 20 61 64 76 61 6e 74 61 at takes advanta
1a595 67 65 20 6f 66 20 74 68 65 20 66 61 63 74 20 74 ge of the fact t
1a596 68 61 74 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 hat the memory c
1a597 65 6c 6c 20 76 61 6c 75 65 20 69 73 20 0a 20 20 ell value is .
1a598 20 20 20 20 2a 2a 20 62 65 69 6e 67 20 73 65 74 ** being set
1a599 20 74 6f 20 4e 55 4c 4c 20 61 66 74 65 72 20 72 to NULL after r
1a59a 65 6c 65 61 73 69 6e 67 20 61 6e 79 20 64 79 6e eleasing any dyn
1a59b 61 6d 69 63 20 72 65 73 6f 75 72 63 65 73 2e 0a amic resources..
1a59c 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a **. *
1a59d 2a 20 54 68 65 20 6a 75 73 74 69 66 69 63 61 74 * The justificat
1a59e 69 6f 6e 20 66 6f 72 20 64 75 70 6c 69 63 61 74 ion for duplicat
1a59f 69 6e 67 20 63 6f 64 65 20 69 73 20 74 68 61 74 ing code is that
1a5a0 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 0a 20 according to .
1a5a1 20 20 20 20 20 2a 2a 20 63 61 6c 6c 67 72 69 6e ** callgrin
1a5a2 64 2c 20 74 68 69 73 20 63 61 75 73 65 73 20 61 d, this causes a
1a5a3 20 63 65 72 74 61 69 6e 20 74 65 73 74 20 63 61 certain test ca
1a5a4 73 65 20 74 6f 20 68 69 74 20 74 68 65 20 43 50 se to hit the CP
1a5a5 55 20 34 2e 37 20 0a 20 20 20 20 20 20 2a 2a 20 U 4.7 . **
1a5a6 70 65 72 63 65 6e 74 20 6c 65 73 73 20 28 78 38 percent less (x8
1a5a7 36 20 6c 69 6e 75 78 2c 20 67 63 63 20 76 65 72 6 linux, gcc ver
1a5a8 73 69 6f 6e 20 34 2e 31 2e 32 2c 20 2d 4f 36 29 sion 4.1.2, -O6)
1a5a9 20 74 68 61 6e 20 69 66 20 0a 20 20 20 20 20 20 than if .
1a5aa 2a 2a 20 73 71 6c 69 74 65 33 4d 65 6d 52 65 6c ** sqlite3MemRel
1a5ab 65 61 73 65 28 29 20 77 65 72 65 20 63 61 6c 6c ease() were call
1a5ac 65 64 20 66 72 6f 6d 20 68 65 72 65 2e 20 57 69 ed from here. Wi
1a5ad 74 68 20 2d 4f 32 2c 20 74 68 69 73 20 6a 75 6d th -O2, this jum
1a5ae 70 73 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 36 ps. ** to 6
1a5af 2e 36 20 70 65 72 63 65 6e 74 2e 20 54 68 65 20 .6 percent. The
1a5b0 74 65 73 74 20 63 61 73 65 20 69 73 20 69 6e 73 test case is ins
1a5b1 65 72 74 69 6e 67 20 31 30 30 30 20 72 6f 77 73 erting 1000 rows
1a5b2 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 20 0a 20 into a table .
1a5b3 20 20 20 20 20 2a 2a 20 77 69 74 68 20 6e 6f 20 ** with no
1a5b4 69 6e 64 65 78 65 73 20 75 73 69 6e 67 20 61 20 indexes using a
1a5b5 73 69 6e 67 6c 65 20 70 72 65 70 61 72 65 64 20 single prepared
1a5b6 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 INSERT statement
1a5b7 2c 20 62 69 6e 64 28 29 20 0a 20 20 20 20 20 20 , bind() .
1a5b8 2a 2a 20 61 6e 64 20 72 65 73 65 74 28 29 2e 20 ** and reset().
1a5b9 49 6e 73 65 72 74 73 20 61 72 65 20 67 72 6f 75 Inserts are grou
1a5ba 70 65 64 20 69 6e 74 6f 20 61 20 74 72 61 6e 73 ped into a trans
1a5bb 61 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f action.. */
1a5bc 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 66 6c . if( p->fl
1a5bd 61 67 73 26 28 4d 45 4d 5f 41 67 67 7c 4d 45 4d ags&(MEM_Agg|MEM
1a5be 5f 44 79 6e 7c 4d 45 4d 5f 46 72 61 6d 65 7c 4d _Dyn|MEM_Frame|M
1a5bf 45 4d 5f 52 6f 77 53 65 74 29 20 29 7b 0a 20 20 EM_RowSet) ){.
1a5c0 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1a5c1 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 29 3b 0a eMemRelease(p);.
1a5c2 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
1a5c3 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 p->zMalloc ){.
1a5c4 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 sqlite3DbF
1a5c5 72 65 65 28 64 62 2c 20 70 2d 3e 7a 4d 61 6c 6c ree(db, p->zMall
1a5c6 6f 63 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e oc);. p->
1a5c7 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 zMalloc = 0;.
1a5c8 20 20 20 7d 0a 0a 20 20 20 20 20 20 70 2d 3e 66 }.. p->f
1a5c9 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b lags = MEM_Null;
1a5ca 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e 6d . }. db->m
1a5cb 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 6d 61 allocFailed = ma
1a5cc 6c 6c 6f 63 5f 66 61 69 6c 65 64 3b 0a 20 20 7d lloc_failed;. }
1a5cd 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 .}../*.** Delete
1a5ce 20 61 20 56 64 62 65 46 72 61 6d 65 20 6f 62 6a a VdbeFrame obj
1a5cf 65 63 74 20 61 6e 64 20 69 74 73 20 63 6f 6e 74 ect and its cont
1a5d0 65 6e 74 73 2e 20 56 64 62 65 46 72 61 6d 65 20 ents. VdbeFrame
1a5d1 6f 62 6a 65 63 74 73 20 61 72 65 0a 2a 2a 20 61 objects are.** a
1a5d2 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 65 20 llocated by the
1a5d3 4f 50 5f 50 72 6f 67 72 61 6d 20 6f 70 63 6f 64 OP_Program opcod
1a5d4 65 20 69 6e 20 73 71 6c 69 74 65 33 56 64 62 65 e in sqlite3Vdbe
1a5d5 45 78 65 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 Exec()..*/.SQLIT
1a5d6 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1a5d7 71 6c 69 74 65 33 56 64 62 65 46 72 61 6d 65 44 qlite3VdbeFrameD
1a5d8 65 6c 65 74 65 28 56 64 62 65 46 72 61 6d 65 20 elete(VdbeFrame
1a5d9 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 *p){. int i;.
1a5da 4d 65 6d 20 2a 61 4d 65 6d 20 3d 20 56 64 62 65 Mem *aMem = Vdbe
1a5db 46 72 61 6d 65 4d 65 6d 28 70 29 3b 0a 20 20 56 FrameMem(p);. V
1a5dc 64 62 65 43 75 72 73 6f 72 20 2a 2a 61 70 43 73 dbeCursor **apCs
1a5dd 72 20 3d 20 28 56 64 62 65 43 75 72 73 6f 72 20 r = (VdbeCursor
1a5de 2a 2a 29 26 61 4d 65 6d 5b 70 2d 3e 6e 43 68 69 **)&aMem[p->nChi
1a5df 6c 64 4d 65 6d 5d 3b 0a 20 20 66 6f 72 28 69 3d ldMem];. for(i=
1a5e0 30 3b 20 69 3c 70 2d 3e 6e 43 68 69 6c 64 43 73 0; i<p->nChildCs
1a5e1 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c r; i++){. sql
1a5e2 69 74 65 33 56 64 62 65 46 72 65 65 43 75 72 73 ite3VdbeFreeCurs
1a5e3 6f 72 28 70 2d 3e 76 2c 20 61 70 43 73 72 5b 69 or(p->v, apCsr[i
1a5e4 5d 29 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 ]);. }. releas
1a5e5 65 4d 65 6d 41 72 72 61 79 28 61 4d 65 6d 2c 20 eMemArray(aMem,
1a5e6 70 2d 3e 6e 43 68 69 6c 64 4d 65 6d 29 3b 0a 20 p->nChildMem);.
1a5e7 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 sqlite3DbFree(p
1a5e8 2d 3e 76 2d 3e 64 62 2c 20 70 29 3b 0a 7d 0a 0a ->v->db, p);.}..
1a5e9 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1a5ea 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 2f 2a 0a 2a MIT_EXPLAIN./*.*
1a5eb 2a 20 47 69 76 65 20 61 20 6c 69 73 74 69 6e 67 * Give a listing
1a5ec 20 6f 66 20 74 68 65 20 70 72 6f 67 72 61 6d 20 of the program
1a5ed 69 6e 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d in the virtual m
1a5ee 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 achine..**.** Th
1a5ef 65 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 74 e interface is t
1a5f0 68 65 20 73 61 6d 65 20 61 73 20 73 71 6c 69 74 he same as sqlit
1a5f1 65 33 56 64 62 65 45 78 65 63 28 29 2e 20 20 42 e3VdbeExec(). B
1a5f2 75 74 20 69 6e 73 74 65 61 64 20 6f 66 0a 2a 2a ut instead of.**
1a5f3 20 72 75 6e 6e 69 6e 67 20 74 68 65 20 63 6f 64 running the cod
1a5f4 65 2c 20 69 74 20 69 6e 76 6f 6b 65 73 20 74 68 e, it invokes th
1a5f5 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 63 65 20 e callback once
1a5f6 66 6f 72 20 65 61 63 68 20 69 6e 73 74 72 75 63 for each instruc
1a5f7 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 66 65 tion..** This fe
1a5f8 61 74 75 72 65 20 69 73 20 75 73 65 64 20 74 6f ature is used to
1a5f9 20 69 6d 70 6c 65 6d 65 6e 74 20 22 45 58 50 4c implement "EXPL
1a5fa 41 49 4e 22 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e AIN"..**.** When
1a5fb 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 2c 20 p->explain==1,
1a5fc 65 61 63 68 20 69 6e 73 74 72 75 63 74 69 6f 6e each instruction
1a5fd 20 69 73 20 6c 69 73 74 65 64 2e 20 20 57 68 65 is listed. Whe
1a5fe 6e 0a 2a 2a 20 70 2d 3e 65 78 70 6c 61 69 6e 3d n.** p->explain=
1a5ff 3d 32 2c 20 6f 6e 6c 79 20 4f 50 5f 45 78 70 6c =2, only OP_Expl
1a600 61 69 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 ain instructions
1a601 20 61 72 65 20 6c 69 73 74 65 64 20 61 6e 64 20 are listed and
1a602 74 68 65 73 65 0a 2a 2a 20 61 72 65 20 73 68 6f these.** are sho
1a603 77 6e 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e wn in a differen
1a604 74 20 66 6f 72 6d 61 74 2e 20 20 70 2d 3e 65 78 t format. p->ex
1a605 70 6c 61 69 6e 3d 3d 32 20 69 73 20 75 73 65 64 plain==2 is used
1a606 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 0a 2a 2a to implement.**
1a607 20 45 58 50 4c 41 49 4e 20 51 55 45 52 59 20 50 EXPLAIN QUERY P
1a608 4c 41 4e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 LAN..*/.SQLITE_P
1a609 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1a60a 65 33 56 64 62 65 4c 69 73 74 28 0a 20 20 56 64 e3VdbeList(. Vd
1a60b 62 65 20 2a 70 20 20 20 20 20 20 20 20 20 20 20 be *p
1a60c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 /* The V
1a60d 44 42 45 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 DBE */.){. int
1a60e 6e 52 6f 77 3b 20 20 20 20 20 20 20 20 20 20 20 nRow;
1a60f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a610 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 /* Total number
1a611 20 6f 66 20 72 6f 77 73 20 74 6f 20 72 65 74 75 of rows to retu
1a612 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 75 62 rn */. int nSub
1a613 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
1a614 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1a615 4e 75 6d 62 65 72 20 6f 66 20 73 75 62 2d 76 64 Number of sub-vd
1a616 62 65 73 20 73 65 65 6e 20 73 6f 20 66 61 72 20 bes seen so far
1a617 2a 2f 0a 20 20 53 75 62 50 72 6f 67 72 61 6d 20 */. SubProgram
1a618 2a 2a 61 70 53 75 62 20 3d 20 30 3b 20 20 20 20 **apSub = 0;
1a619 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 /* Arr
1a61a 61 79 20 6f 66 20 73 75 62 2d 76 64 62 65 73 20 ay of sub-vdbes
1a61b 2a 2f 0a 20 20 4d 65 6d 20 2a 70 53 75 62 20 3d */. Mem *pSub =
1a61c 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 0;. sqlite3 *d
1a61d 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 6e 74 b = p->db;. int
1a61e 20 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 i;. int rc = S
1a61f 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 20 QLITE_OK;. Mem
1a620 2a 70 4d 65 6d 20 3d 20 70 2d 3e 70 52 65 73 75 *pMem = p->pResu
1a621 6c 74 53 65 74 20 3d 20 26 70 2d 3e 61 4d 65 6d ltSet = &p->aMem
1a622 5b 31 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 [1];.. assert(
1a623 70 2d 3e 65 78 70 6c 61 69 6e 20 29 3b 0a 20 20 p->explain );.
1a624 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 assert( p->magic
1a625 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e ==VDBE_MAGIC_RUN
1a626 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 );. assert( db
1a627 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f ->magic==SQLITE_
1a628 4d 41 47 49 43 5f 42 55 53 59 20 29 3b 0a 20 20 MAGIC_BUSY );.
1a629 61 73 73 65 72 74 28 20 70 2d 3e 72 63 3d 3d 53 assert( p->rc==S
1a62a 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 QLITE_OK || p->r
1a62b 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 7c c==SQLITE_BUSY |
1a62c 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f | p->rc==SQLITE_
1a62d 4e 4f 4d 45 4d 20 29 3b 0a 0a 20 20 2f 2a 20 45 NOMEM );.. /* E
1a62e 76 65 6e 20 74 68 6f 75 67 68 20 74 68 69 73 20 ven though this
1a62f 6f 70 63 6f 64 65 20 64 6f 65 73 20 6e 6f 74 20 opcode does not
1a630 75 73 65 20 64 79 6e 61 6d 69 63 20 73 74 72 69 use dynamic stri
1a631 6e 67 73 20 66 6f 72 0a 20 20 2a 2a 20 74 68 65 ngs for. ** the
1a632 20 72 65 73 75 6c 74 2c 20 72 65 73 75 6c 74 20 result, result
1a633 63 6f 6c 75 6d 6e 73 20 6d 61 79 20 62 65 63 6f columns may beco
1a634 6d 65 20 64 79 6e 61 6d 69 63 20 69 66 20 74 68 me dynamic if th
1a635 65 20 75 73 65 72 20 63 61 6c 6c 73 0a 20 20 2a e user calls. *
1a636 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e * sqlite3_column
1a637 5f 74 65 78 74 31 36 28 29 2c 20 63 61 75 73 69 _text16(), causi
1a638 6e 67 20 61 20 74 72 61 6e 73 6c 61 74 69 6f 6e ng a translation
1a639 20 74 6f 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 to UTF-16 encod
1a63a 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 72 65 6c 65 ing.. */. rele
1a63b 61 73 65 4d 65 6d 41 72 72 61 79 28 70 4d 65 6d aseMemArray(pMem
1a63c 2c 20 38 29 3b 0a 0a 20 20 69 66 28 20 70 2d 3e , 8);.. if( p->
1a63d 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d rc==SQLITE_NOMEM
1a63e 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 ){. /* This
1a63f 68 61 70 70 65 6e 73 20 69 66 20 61 20 6d 61 6c happens if a mal
1a640 6c 6f 63 28 29 20 69 6e 73 69 64 65 20 61 20 63 loc() inside a c
1a641 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 all to sqlite3_c
1a642 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0a olumn_text() or.
1a643 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 ** sqlite3_c
1a644 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 olumn_text16() f
1a645 61 69 6c 65 64 2e 20 20 2a 2f 0a 20 20 20 20 64 ailed. */. d
1a646 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1a647 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 = 1;. return
1a648 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 SQLITE_ERROR;.
1a649 7d 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f }.. /* Figure o
1a64a 75 74 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 ut total number
1a64b 6f 66 20 72 6f 77 73 20 74 68 61 74 20 77 69 6c of rows that wil
1a64c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 20 62 79 l be returned by
1a64d 20 74 68 69 73 20 0a 20 20 2a 2a 20 45 58 50 4c this . ** EXPL
1a64e 41 49 4e 20 70 72 6f 67 72 61 6d 2e 20 20 2a 2f AIN program. */
1a64f 0a 20 20 6e 52 6f 77 20 3d 20 70 2d 3e 6e 4f 70 . nRow = p->nOp
1a650 3b 0a 20 20 69 66 28 20 70 2d 3e 65 78 70 6c 61 ;. if( p->expla
1a651 69 6e 3d 3d 31 20 29 7b 0a 20 20 20 20 70 53 75 in==1 ){. pSu
1a652 62 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 39 5d 3b b = &p->aMem[9];
1a653 0a 20 20 20 20 69 66 28 20 70 53 75 62 2d 3e 66 . if( pSub->f
1a654 6c 61 67 73 26 4d 45 4d 5f 42 6c 6f 62 20 29 7b lags&MEM_Blob ){
1a655 0a 20 20 20 20 20 20 6e 53 75 62 20 3d 20 70 53 . nSub = pS
1a656 75 62 2d 3e 6e 2f 73 69 7a 65 6f 66 28 56 64 62 ub->n/sizeof(Vdb
1a657 65 2a 29 3b 0a 20 20 20 20 20 20 61 70 53 75 62 e*);. apSub
1a658 20 3d 20 28 53 75 62 50 72 6f 67 72 61 6d 20 2a = (SubProgram *
1a659 2a 29 70 53 75 62 2d 3e 7a 3b 0a 20 20 20 20 7d *)pSub->z;. }
1a65a 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
1a65b 6e 53 75 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 nSub; i++){.
1a65c 20 20 6e 52 6f 77 20 2b 3d 20 61 70 53 75 62 5b nRow += apSub[
1a65d 69 5d 2d 3e 6e 4f 70 3b 0a 20 20 20 20 7d 0a 20 i]->nOp;. }.
1a65e 20 7d 0a 0a 20 20 64 6f 7b 0a 20 20 20 20 69 20 }.. do{. i
1a65f 3d 20 70 2d 3e 70 63 2b 2b 3b 0a 20 20 7d 77 68 = p->pc++;. }wh
1a660 69 6c 65 28 20 69 3c 6e 52 6f 77 20 26 26 20 70 ile( i<nRow && p
1a661 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 26 26 20 ->explain==2 &&
1a662 70 2d 3e 61 4f 70 5b 69 5d 2e 6f 70 63 6f 64 65 p->aOp[i].opcode
1a663 21 3d 4f 50 5f 45 78 70 6c 61 69 6e 20 29 3b 0a !=OP_Explain );.
1a664 20 20 69 66 28 20 69 3e 3d 6e 52 6f 77 20 29 7b if( i>=nRow ){
1a665 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c . p->rc = SQL
1a666 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 72 63 20 3d ITE_OK;. rc =
1a667 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 SQLITE_DONE;.
1a668 7d 65 6c 73 65 20 69 66 28 20 64 62 2d 3e 75 31 }else if( db->u1
1a669 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20 29 .isInterrupted )
1a66a 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 {. p->rc = SQ
1a66b 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 3b 0a LITE_INTERRUPT;.
1a66c 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1a66d 45 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69 74 ERROR;. sqlit
1a66e 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e e3SetString(&p->
1a66f 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 zErrMsg, db, "%s
1a670 22 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 ", sqlite3ErrStr
1a671 28 70 2d 3e 72 63 29 29 3b 0a 20 20 7d 65 6c 73 (p->rc));. }els
1a672 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b 0a e{. char *z;.
1a673 20 20 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 Op *pOp;.
1a674 20 69 66 28 20 69 3c 70 2d 3e 6e 4f 70 20 29 7b if( i<p->nOp ){
1a675 0a 20 20 20 20 20 20 70 4f 70 20 3d 20 26 70 2d . pOp = &p-
1a676 3e 61 4f 70 5b 69 5d 3b 0a 20 20 20 20 7d 65 6c >aOp[i];. }el
1a677 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b se{. int j;
1a678 0a 20 20 20 20 20 20 69 20 2d 3d 20 70 2d 3e 6e . i -= p->n
1a679 4f 70 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d Op;. for(j=
1a67a 30 3b 20 69 3e 3d 61 70 53 75 62 5b 6a 5d 2d 3e 0; i>=apSub[j]->
1a67b 6e 4f 70 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 nOp; j++){.
1a67c 20 20 20 69 20 2d 3d 20 61 70 53 75 62 5b 6a 5d i -= apSub[j]
1a67d 2d 3e 6e 4f 70 3b 0a 20 20 20 20 20 20 7d 0a 20 ->nOp;. }.
1a67e 20 20 20 20 20 70 4f 70 20 3d 20 26 61 70 53 75 pOp = &apSu
1a67f 62 5b 6a 5d 2d 3e 61 4f 70 5b 69 5d 3b 0a 20 20 b[j]->aOp[i];.
1a680 20 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 65 }. if( p->e
1a681 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20 20 20 xplain==1 ){.
1a682 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d pMem->flags =
1a683 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 MEM_Int;.
1a684 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c pMem->type = SQL
1a685 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 ITE_INTEGER;.
1a686 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 69 pMem->u.i = i
1a687 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1a688 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a689 20 2f 2a 20 50 72 6f 67 72 61 6d 20 63 6f 75 6e /* Program coun
1a68a 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 ter */. pMe
1a68b 6d 2b 2b 3b 0a 20 20 0a 20 20 20 20 20 20 70 4d m++;. . pM
1a68c 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f em->flags = MEM_
1a68d 53 74 61 74 69 63 7c 4d 45 4d 5f 53 74 72 7c 4d Static|MEM_Str|M
1a68e 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 EM_Term;. p
1a68f 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 2a 29 Mem->z = (char*)
1a690 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d sqlite3OpcodeNam
1a691 65 28 70 4f 70 2d 3e 6f 70 63 6f 64 65 29 3b 20 e(pOp->opcode);
1a692 20 2f 2a 20 4f 70 63 6f 64 65 20 2a 2f 0a 20 20 /* Opcode */.
1a693 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d assert( pMem
1a694 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 ->z!=0 );.
1a695 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74 65 pMem->n = sqlite
1a696 33 53 74 72 6c 65 6e 33 30 28 70 4d 65 6d 2d 3e 3Strlen30(pMem->
1a697 7a 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e z);. pMem->
1a698 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 54 45 type = SQLITE_TE
1a699 58 54 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e XT;. pMem->
1a69a 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 enc = SQLITE_UTF
1a69b 38 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b 8;. pMem++;
1a69c 0a 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d .. if( pOp-
1a69d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 53 55 42 50 >p4type==P4_SUBP
1a69e 52 4f 47 52 41 4d 20 29 7b 0a 20 20 20 20 20 20 ROGRAM ){.
1a69f 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 28 6e int nByte = (n
1a6a0 53 75 62 2b 31 29 2a 73 69 7a 65 6f 66 28 53 75 Sub+1)*sizeof(Su
1a6a1 62 50 72 6f 67 72 61 6d 2a 29 3b 0a 20 20 20 20 bProgram*);.
1a6a2 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 int j;.
1a6a3 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 53 for(j=0; j<nS
1a6a4 75 62 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 ub; j++){.
1a6a5 20 20 20 20 69 66 28 20 61 70 53 75 62 5b 6a 5d if( apSub[j]
1a6a6 3d 3d 70 4f 70 2d 3e 70 34 2e 70 50 72 6f 67 72 ==pOp->p4.pProgr
1a6a7 61 6d 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 am ) break;.
1a6a8 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 }. if
1a6a9 28 20 6a 3d 3d 6e 53 75 62 20 26 26 20 53 51 4c ( j==nSub && SQL
1a6aa 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33 56 ITE_OK==sqlite3V
1a6ab 64 62 65 4d 65 6d 47 72 6f 77 28 70 53 75 62 2c dbeMemGrow(pSub,
1a6ac 20 6e 42 79 74 65 2c 20 31 29 20 29 7b 0a 20 20 nByte, 1) ){.
1a6ad 20 20 20 20 20 20 20 20 61 70 53 75 62 20 3d 20 apSub =
1a6ae 28 53 75 62 50 72 6f 67 72 61 6d 20 2a 2a 29 70 (SubProgram **)p
1a6af 53 75 62 2d 3e 7a 3b 0a 20 20 20 20 20 20 20 20 Sub->z;.
1a6b0 20 20 61 70 53 75 62 5b 6e 53 75 62 2b 2b 5d 20 apSub[nSub++]
1a6b1 3d 20 70 4f 70 2d 3e 70 34 2e 70 50 72 6f 67 72 = pOp->p4.pProgr
1a6b2 61 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 70 53 am;. pS
1a6b3 75 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d ub->flags |= MEM
1a6b4 5f 42 6c 6f 62 3b 0a 20 20 20 20 20 20 20 20 20 _Blob;.
1a6b5 20 70 53 75 62 2d 3e 6e 20 3d 20 6e 53 75 62 2a pSub->n = nSub*
1a6b6 73 69 7a 65 6f 66 28 53 75 62 50 72 6f 67 72 61 sizeof(SubProgra
1a6b7 6d 2a 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 m*);. }.
1a6b8 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 }. }..
1a6b9 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 pMem->flags =
1a6ba 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 70 4d 65 MEM_Int;. pMe
1a6bb 6d 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 31 m->u.i = pOp->p1
1a6bc 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1a6bd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 31 /* P1
1a6be 20 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 */. pMem->ty
1a6bf 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 pe = SQLITE_INTE
1a6c0 47 45 52 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b GER;. pMem++;
1a6c1 0a 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 .. pMem->flag
1a6c2 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 s = MEM_Int;.
1a6c3 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 70 4f 70 pMem->u.i = pOp
1a6c4 2d 3e 70 32 3b 20 20 20 20 20 20 20 20 20 20 20 ->p2;
1a6c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1a6c6 2a 20 50 32 20 2a 2f 0a 20 20 20 20 70 4d 65 6d * P2 */. pMem
1a6c7 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f ->type = SQLITE_
1a6c8 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 70 4d 65 INTEGER;. pMe
1a6c9 6d 2b 2b 3b 0a 0a 20 20 20 20 69 66 28 20 70 2d m++;.. if( p-
1a6ca 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20 >explain==1 ){.
1a6cb 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 pMem->flags
1a6cc 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 = MEM_Int;.
1a6cd 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 70 4f pMem->u.i = pO
1a6ce 70 2d 3e 70 33 3b 20 20 20 20 20 20 20 20 20 20 p->p3;
1a6cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a6d0 2f 2a 20 50 33 20 2a 2f 0a 20 20 20 20 20 20 70 /* P3 */. p
1a6d1 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 Mem->type = SQLI
1a6d2 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 TE_INTEGER;.
1a6d3 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20 20 20 7d 0a pMem++;. }.
1a6d4 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
1a6d5 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d VdbeMemGrow(pMem
1a6d6 2c 20 33 32 2c 20 30 29 20 29 7b 20 20 20 20 20 , 32, 0) ){
1a6d7 20 20 20 20 20 20 20 2f 2a 20 50 34 20 2a 2f 0a /* P4 */.
1a6d8 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d assert( p-
1a6d9 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 >db->mallocFaile
1a6da 64 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 d );. retur
1a6db 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a n SQLITE_ERROR;.
1a6dc 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e }. pMem->
1a6dd 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 44 79 6e 7c flags = MEM_Dyn|
1a6de 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d MEM_Str|MEM_Term
1a6df 3b 0a 20 20 20 20 7a 20 3d 20 64 69 73 70 6c 61 ;. z = displa
1a6e0 79 50 34 28 70 4f 70 2c 20 70 4d 65 6d 2d 3e 7a yP4(pOp, pMem->z
1a6e1 2c 20 33 32 29 3b 0a 20 20 20 20 69 66 28 20 7a , 32);. if( z
1a6e2 21 3d 70 4d 65 6d 2d 3e 7a 20 29 7b 0a 20 20 20 !=pMem->z ){.
1a6e3 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
1a6e4 6d 53 65 74 53 74 72 28 70 4d 65 6d 2c 20 7a 2c mSetStr(pMem, z,
1a6e5 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 -1, SQLITE_UTF8
1a6e6 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b , 0);. }else{
1a6e7 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1a6e8 4d 65 6d 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20 20 Mem->z!=0 );.
1a6e9 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71 6c pMem->n = sql
1a6ea 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4d 65 ite3Strlen30(pMe
1a6eb 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 70 4d 65 m->z);. pMe
1a6ec 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f m->enc = SQLITE_
1a6ed 55 54 46 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20 UTF8;. }.
1a6ee 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c pMem->type = SQL
1a6ef 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 70 4d ITE_TEXT;. pM
1a6f0 65 6d 2b 2b 3b 0a 0a 20 20 20 20 69 66 28 20 70 em++;.. if( p
1a6f1 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a ->explain==1 ){.
1a6f2 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
1a6f3 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 3VdbeMemGrow(pMe
1a6f4 6d 2c 20 34 2c 20 30 29 20 29 7b 0a 20 20 20 20 m, 4, 0) ){.
1a6f5 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 assert( p->d
1a6f6 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1a6f7 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 );. retur
1a6f8 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a n SQLITE_ERROR;.
1a6f9 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4d }. pM
1a6fa 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f em->flags = MEM_
1a6fb 44 79 6e 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f Dyn|MEM_Str|MEM_
1a6fc 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 4d 65 6d Term;. pMem
1a6fd 2d 3e 6e 20 3d 20 32 3b 0a 20 20 20 20 20 20 73 ->n = 2;. s
1a6fe 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
1a6ff 33 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 22 25 2e 32 3, pMem->z, "%.2
1a700 78 22 2c 20 70 4f 70 2d 3e 70 35 29 3b 20 20 20 x", pOp->p5);
1a701 2f 2a 20 50 35 20 2a 2f 0a 20 20 20 20 20 20 70 /* P5 */. p
1a702 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 Mem->type = SQLI
1a703 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 20 20 70 TE_TEXT;. p
1a704 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 Mem->enc = SQLIT
1a705 45 5f 55 54 46 38 3b 0a 20 20 20 20 20 20 70 4d E_UTF8;. pM
1a706 65 6d 2b 2b 3b 0a 20 20 0a 23 69 66 64 65 66 20 em++;. .#ifdef
1a707 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 SQLITE_DEBUG.
1a708 20 20 20 69 66 28 20 70 4f 70 2d 3e 7a 43 6f 6d if( pOp->zCom
1a709 6d 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 ment ){.
1a70a 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 pMem->flags = ME
1a70b 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a M_Str|MEM_Term;.
1a70c 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 pMem->z
1a70d 3d 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 3b = pOp->zComment;
1a70e 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e . pMem->n
1a70f 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e = sqlite3Strlen
1a710 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 30(pMem->z);.
1a711 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d pMem->enc =
1a712 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 SQLITE_UTF8;.
1a713 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 pMem->type
1a714 20 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a = SQLITE_TEXT;.
1a715 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 }else.#end
1a716 69 66 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 if. {.
1a717 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d pMem->flags =
1a718 20 4d 45 4d 5f 4e 75 6c 6c 3b 20 20 20 20 20 20 MEM_Null;
1a719 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a71a 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20 2a 2f 0a 20 /* Comment */.
1a71b 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 pMem->typ
1a71c 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b e = SQLITE_NULL;
1a71d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a . }. }..
1a71e 20 20 20 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d p->nResColum
1a71f 6e 20 3d 20 38 20 2d 20 35 2a 28 70 2d 3e 65 78 n = 8 - 5*(p->ex
1a720 70 6c 61 69 6e 2d 31 29 3b 0a 20 20 20 20 70 2d plain-1);. p-
1a721 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b >rc = SQLITE_OK;
1a722 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
1a723 5f 52 4f 57 3b 0a 20 20 7d 0a 20 20 72 65 74 75 _ROW;. }. retu
1a724 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 rn rc;.}.#endif
1a725 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 /* SQLITE_OMIT_E
1a726 58 50 4c 41 49 4e 20 2a 2f 0a 0a 23 69 66 64 65 XPLAIN */..#ifde
1a727 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f f SQLITE_DEBUG./
1a728 2a 0a 2a 2a 20 50 72 69 6e 74 20 74 68 65 20 53 *.** Print the S
1a729 51 4c 20 74 68 61 74 20 77 61 73 20 75 73 65 64 QL that was used
1a72a 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61 20 56 to generate a V
1a72b 44 42 45 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a DBE program..*/.
1a72c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1a72d 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 50 oid sqlite3VdbeP
1a72e 72 69 6e 74 53 71 6c 28 56 64 62 65 20 2a 70 29 rintSql(Vdbe *p)
1a72f 7b 0a 20 20 69 6e 74 20 6e 4f 70 20 3d 20 70 2d {. int nOp = p-
1a730 3e 6e 4f 70 3b 0a 20 20 56 64 62 65 4f 70 20 2a >nOp;. VdbeOp *
1a731 70 4f 70 3b 0a 20 20 69 66 28 20 6e 4f 70 3c 31 pOp;. if( nOp<1
1a732 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 4f 70 ) return;. pOp
1a733 20 3d 20 26 70 2d 3e 61 4f 70 5b 30 5d 3b 0a 20 = &p->aOp[0];.
1a734 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 if( pOp->opcode
1a735 3d 3d 4f 50 5f 54 72 61 63 65 20 26 26 20 70 4f ==OP_Trace && pO
1a736 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 7b 0a 20 20 p->p4.z!=0 ){.
1a737 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 const char *z
1a738 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 = pOp->p4.z;.
1a739 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 while( sqlite3I
1a73a 73 73 70 61 63 65 28 2a 7a 29 20 29 20 7a 2b 2b sspace(*z) ) z++
1a73b 3b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 53 51 ;. printf("SQ
1a73c 4c 3a 20 5b 25 73 5d 5c 6e 22 2c 20 7a 29 3b 0a L: [%s]\n", z);.
1a73d 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 }.}.#endif..#i
1a73e 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 f !defined(SQLIT
1a73f 45 5f 4f 4d 49 54 5f 54 52 41 43 45 29 20 26 26 E_OMIT_TRACE) &&
1a740 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
1a741 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 29 0a ENABLE_IOTRACE).
1a742 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 61 6e 20 49 /*.** Print an I
1a743 4f 54 52 41 43 45 20 6d 65 73 73 61 67 65 20 73 OTRACE message s
1a744 68 6f 77 69 6e 67 20 53 51 4c 20 63 6f 6e 74 65 howing SQL conte
1a745 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 nt..*/.SQLITE_PR
1a746 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1a747 65 33 56 64 62 65 49 4f 54 72 61 63 65 53 71 6c e3VdbeIOTraceSql
1a748 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 (Vdbe *p){. int
1a749 20 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 nOp = p->nOp;.
1a74a 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 VdbeOp *pOp;.
1a74b 69 66 28 20 73 71 6c 69 74 65 33 49 6f 54 72 61 if( sqlite3IoTra
1a74c 63 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a ce==0 ) return;.
1a74d 20 20 69 66 28 20 6e 4f 70 3c 31 20 29 20 72 65 if( nOp<1 ) re
1a74e 74 75 72 6e 3b 0a 20 20 70 4f 70 20 3d 20 26 70 turn;. pOp = &p
1a74f 2d 3e 61 4f 70 5b 30 5d 3b 0a 20 20 69 66 28 20 ->aOp[0];. if(
1a750 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f pOp->opcode==OP_
1a751 54 72 61 63 65 20 26 26 20 70 4f 70 2d 3e 70 34 Trace && pOp->p4
1a752 2e 7a 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 .z!=0 ){. int
1a753 20 69 2c 20 6a 3b 0a 20 20 20 20 63 68 61 72 20 i, j;. char
1a754 7a 5b 31 30 30 30 5d 3b 0a 20 20 20 20 73 71 6c z[1000];. sql
1a755 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 ite3_snprintf(si
1a756 7a 65 6f 66 28 7a 29 2c 20 7a 2c 20 22 25 73 22 zeof(z), z, "%s"
1a757 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 , pOp->p4.z);.
1a758 20 20 66 6f 72 28 69 3d 30 3b 20 73 71 6c 69 74 for(i=0; sqlit
1a759 65 33 49 73 73 70 61 63 65 28 7a 5b 69 5d 29 3b e3Isspace(z[i]);
1a75a 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 66 6f 72 28 i++){}. for(
1a75b 6a 3d 30 3b 20 7a 5b 69 5d 3b 20 69 2b 2b 29 7b j=0; z[i]; i++){
1a75c 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 . if( sqlit
1a75d 65 33 49 73 73 70 61 63 65 28 7a 5b 69 5d 29 20 e3Isspace(z[i])
1a75e 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a ){. if( z
1a75f 5b 69 2d 31 5d 21 3d 27 20 27 20 29 7b 0a 20 20 [i-1]!=' ' ){.
1a760 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d z[j++] =
1a761 20 27 20 27 3b 0a 20 20 20 20 20 20 20 20 7d 0a ' ';. }.
1a762 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1a763 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b z[j++] = z[
1a764 69 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 i];. }.
1a765 7d 0a 20 20 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a }. z[j] = 0;.
1a766 20 20 20 20 73 71 6c 69 74 65 33 49 6f 54 72 61 sqlite3IoTra
1a767 63 65 28 22 53 51 4c 20 25 73 5c 6e 22 2c 20 7a ce("SQL %s\n", z
1a768 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 );. }.}.#endif
1a769 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* !SQLITE_OMIT_
1a76a 54 52 41 43 45 20 26 26 20 53 51 4c 49 54 45 5f TRACE && SQLITE_
1a76b 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 20 2a ENABLE_IOTRACE *
1a76c 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 /../*.** Allocat
1a76d 65 20 73 70 61 63 65 20 66 72 6f 6d 20 61 20 66 e space from a f
1a76e 69 78 65 64 20 73 69 7a 65 20 62 75 66 66 65 72 ixed size buffer
1a76f 2e 20 20 4d 61 6b 65 20 2a 70 70 20 70 6f 69 6e . Make *pp poin
1a770 74 20 74 6f 20 74 68 65 0a 2a 2a 20 61 6c 6c 6f t to the.** allo
1a771 63 61 74 65 64 20 73 70 61 63 65 2e 20 20 28 4e cated space. (N
1a772 6f 74 65 3a 20 20 70 70 20 69 73 20 61 20 63 68 ote: pp is a ch
1a773 61 72 2a 20 72 61 74 68 65 72 20 74 68 61 6e 20 ar* rather than
1a774 61 20 76 6f 69 64 2a 2a 20 74 6f 0a 2a 2a 20 77 a void** to.** w
1a775 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 65 20 70 ork around the p
1a776 6f 69 6e 74 65 72 20 61 6c 69 61 73 69 6e 67 20 ointer aliasing
1a777 72 75 6c 65 73 20 6f 66 20 43 2e 29 20 20 2a 70 rules of C.) *p
1a778 70 20 73 68 6f 75 6c 64 20 69 6e 69 74 69 61 6c p should initial
1a779 6c 79 0a 2a 2a 20 62 65 20 7a 65 72 6f 2e 20 20 ly.** be zero.
1a77a 49 66 20 2a 70 70 20 69 73 20 6e 6f 74 20 7a 65 If *pp is not ze
1a77b 72 6f 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 ro, that means t
1a77c 68 61 74 20 74 68 65 20 73 70 61 63 65 20 68 61 hat the space ha
1a77d 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 62 65 65 s already.** bee
1a77e 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 n allocated and
1a77f 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
1a780 61 20 6e 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 6e 42 a noop..**.** nB
1a781 79 74 65 20 69 73 20 74 68 65 20 6e 75 6d 62 65 yte is the numbe
1a782 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70 r of bytes of sp
1a783 61 63 65 20 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a ace needed..**.*
1a784 2a 20 2a 70 70 46 72 6f 6d 20 70 6f 69 6e 74 20 * *ppFrom point
1a785 74 6f 20 61 76 61 69 6c 61 62 6c 65 20 73 70 61 to available spa
1a786 63 65 20 61 6e 64 20 70 45 6e 64 20 70 6f 69 6e ce and pEnd poin
1a787 74 73 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 ts to the end of
1a788 20 74 68 65 0a 2a 2a 20 61 76 61 69 6c 61 62 6c the.** availabl
1a789 65 20 73 70 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 2a e space..**.** *
1a78a 70 6e 42 79 74 65 20 69 73 20 61 20 63 6f 75 6e pnByte is a coun
1a78b 74 65 72 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 ter of the numbe
1a78c 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70 r of bytes of sp
1a78d 61 63 65 20 74 68 61 74 20 68 61 76 65 20 66 61 ace that have fa
1a78e 69 6c 65 64 0a 2a 2a 20 74 6f 20 61 6c 6c 6f 63 iled.** to alloc
1a78f 61 74 65 2e 20 20 49 66 20 74 68 65 72 65 20 69 ate. If there i
1a790 73 20 69 6e 73 75 66 66 69 63 69 65 6e 74 20 73 s insufficient s
1a791 70 61 63 65 20 69 6e 20 2a 70 70 46 72 6f 6d 20 pace in *ppFrom
1a792 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 0a 2a to satisfy the.*
1a793 2a 20 72 65 71 75 65 73 74 2c 20 74 68 65 6e 20 * request, then
1a794 69 6e 63 72 65 6d 65 6e 74 20 2a 70 6e 42 79 74 increment *pnByt
1a795 65 20 62 79 20 74 68 65 20 61 6d 6f 75 6e 74 20 e by the amount
1a796 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 2e 0a of the request..
1a797 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 */.static void a
1a798 6c 6c 6f 63 53 70 61 63 65 28 0a 20 20 63 68 61 llocSpace(. cha
1a799 72 20 2a 70 70 2c 20 20 20 20 20 20 20 20 20 20 r *pp,
1a79a 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 53 65 74 /* IN/OUT: Set
1a79b 20 2a 70 70 20 74 6f 20 70 6f 69 6e 74 20 74 6f *pp to point to
1a79c 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 allocated buffe
1a79d 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 r */. int nByte
1a79e 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e , /* N
1a79f 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 umber of bytes t
1a7a0 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 20 o allocate */.
1a7a1 75 38 20 2a 2a 70 70 46 72 6f 6d 2c 20 20 20 20 u8 **ppFrom,
1a7a2 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 /* IN/OUT:
1a7a3 41 6c 6c 6f 63 61 74 65 20 66 72 6f 6d 20 2a 70 Allocate from *p
1a7a4 70 46 72 6f 6d 20 2a 2f 0a 20 20 75 38 20 2a 70 pFrom */. u8 *p
1a7a5 45 6e 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 End,
1a7a6 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 31 20 /* Pointer to 1
1a7a7 62 79 74 65 20 70 61 73 74 20 74 68 65 20 65 6e byte past the en
1a7a8 64 20 6f 66 20 2a 70 70 46 72 6f 6d 20 62 75 66 d of *ppFrom buf
1a7a9 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e fer */. int *pn
1a7aa 42 79 74 65 20 20 20 20 20 20 20 20 20 20 2f 2a Byte /*
1a7ab 20 49 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 63 If allocation c
1a7ac 61 6e 6e 6f 74 20 62 65 20 6d 61 64 65 2c 20 69 annot be made, i
1a7ad 6e 63 72 65 6d 65 6e 74 20 2a 70 6e 42 79 74 65 ncrement *pnByte
1a7ae 20 2a 2f 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 */.){. assert(
1a7af 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 EIGHT_BYTE_ALIG
1a7b0 4e 4d 45 4e 54 28 2a 70 70 46 72 6f 6d 29 20 29 NMENT(*ppFrom) )
1a7b1 3b 0a 20 20 69 66 28 20 28 2a 28 76 6f 69 64 2a ;. if( (*(void*
1a7b2 2a 29 70 70 29 3d 3d 30 20 29 7b 0a 20 20 20 20 *)pp)==0 ){.
1a7b3 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44 38 28 6e nByte = ROUND8(n
1a7b4 42 79 74 65 29 3b 0a 20 20 20 20 69 66 28 20 26 Byte);. if( &
1a7b5 28 2a 70 70 46 72 6f 6d 29 5b 6e 42 79 74 65 5d (*ppFrom)[nByte]
1a7b6 20 3c 3d 20 70 45 6e 64 20 29 7b 0a 20 20 20 20 <= pEnd ){.
1a7b7 20 20 2a 28 76 6f 69 64 2a 2a 29 70 70 20 3d 20 *(void**)pp =
1a7b8 28 76 6f 69 64 20 2a 29 2a 70 70 46 72 6f 6d 3b (void *)*ppFrom;
1a7b9 0a 20 20 20 20 20 20 2a 70 70 46 72 6f 6d 20 2b . *ppFrom +
1a7ba 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 7d 65 6c = nByte;. }el
1a7bb 73 65 7b 0a 20 20 20 20 20 20 2a 70 6e 42 79 74 se{. *pnByt
1a7bc 65 20 2b 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 e += nByte;.
1a7bd 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 }. }.}../*.** P
1a7be 72 65 70 61 72 65 20 61 20 76 69 72 74 75 61 6c repare a virtual
1a7bf 20 6d 61 63 68 69 6e 65 20 66 6f 72 20 65 78 65 machine for exe
1a7c0 63 75 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 6e cution. This in
1a7c1 76 6f 6c 76 65 73 20 74 68 69 6e 67 73 20 73 75 volves things su
1a7c2 63 68 0a 2a 2a 20 61 73 20 61 6c 6c 6f 63 61 74 ch.** as allocat
1a7c3 69 6e 67 20 73 74 61 63 6b 20 73 70 61 63 65 20 ing stack space
1a7c4 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 and initializing
1a7c5 20 74 68 65 20 70 72 6f 67 72 61 6d 20 63 6f 75 the program cou
1a7c6 6e 74 65 72 2e 0a 2a 2a 20 41 66 74 65 72 20 74 nter..** After t
1a7c7 68 65 20 56 44 42 45 20 68 61 73 20 62 65 20 70 he VDBE has be p
1a7c8 72 65 70 70 65 64 2c 20 69 74 20 63 61 6e 20 62 repped, it can b
1a7c9 65 20 65 78 65 63 75 74 65 64 20 62 79 20 6f 6e e executed by on
1a7ca 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 63 61 6c e or more.** cal
1a7cb 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 56 64 62 ls to sqlite3Vdb
1a7cc 65 45 78 65 63 28 29 2e 20 20 0a 2a 2a 0a 2a 2a eExec(). .**.**
1a7cd 20 54 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c This is the onl
1a7ce 79 20 77 61 79 20 74 6f 20 6d 6f 76 65 20 61 20 y way to move a
1a7cf 56 44 42 45 20 66 72 6f 6d 20 56 44 42 45 5f 4d VDBE from VDBE_M
1a7d0 41 47 49 43 5f 49 4e 49 54 20 74 6f 0a 2a 2a 20 AGIC_INIT to.**
1a7d1 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 2e 0a VDBE_MAGIC_RUN..
1a7d2 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 **.** This funct
1a7d3 69 6f 6e 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 ion may be calle
1a7d4 64 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 d more than once
1a7d5 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 76 69 72 on a single vir
1a7d6 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a tual machine..**
1a7d7 20 54 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20 The first call
1a7d8 69 73 20 6d 61 64 65 20 77 68 69 6c 65 20 63 6f is made while co
1a7d9 6d 70 69 6c 69 6e 67 20 74 68 65 20 53 51 4c 20 mpiling the SQL
1a7da 73 74 61 74 65 6d 65 6e 74 2e 20 53 75 62 73 65 statement. Subse
1a7db 71 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c 73 20 61 quent.** calls a
1a7dc 72 65 20 6d 61 64 65 20 61 73 20 70 61 72 74 20 re made as part
1a7dd 6f 66 20 74 68 65 20 70 72 6f 63 65 73 73 20 6f of the process o
1a7de 66 20 72 65 73 65 74 74 69 6e 67 20 61 20 73 74 f resetting a st
1a7df 61 74 65 6d 65 6e 74 20 74 6f 20 62 65 0a 2a 2a atement to be.**
1a7e0 20 72 65 2d 65 78 65 63 75 74 65 64 20 28 66 72 re-executed (fr
1a7e1 6f 6d 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c om a call to sql
1a7e2 69 74 65 33 5f 72 65 73 65 74 28 29 29 2e 20 54 ite3_reset()). T
1a7e3 68 65 20 6e 56 61 72 2c 20 6e 4d 65 6d 2c 20 6e he nVar, nMem, n
1a7e4 43 75 72 73 6f 72 20 0a 2a 2a 20 61 6e 64 20 69 Cursor .** and i
1a7e5 73 45 78 70 6c 61 69 6e 20 70 61 72 61 6d 65 74 sExplain paramet
1a7e6 65 72 73 20 61 72 65 20 6f 6e 6c 79 20 70 61 73 ers are only pas
1a7e7 73 65 64 20 63 6f 72 72 65 63 74 20 76 61 6c 75 sed correct valu
1a7e8 65 73 20 74 68 65 20 66 69 72 73 74 20 74 69 6d es the first tim
1a7e9 65 0a 2a 2a 20 74 68 65 20 66 75 6e 63 74 69 6f e.** the functio
1a7ea 6e 20 69 73 20 63 61 6c 6c 65 64 2e 20 4f 6e 20 n is called. On
1a7eb 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 subsequent calls
1a7ec 2c 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 72 , from sqlite3_r
1a7ed 65 73 65 74 28 29 2c 20 6e 56 61 72 0a 2a 2a 20 eset(), nVar.**
1a7ee 69 73 20 70 61 73 73 65 64 20 2d 31 20 61 6e 64 is passed -1 and
1a7ef 20 6e 4d 65 6d 2c 20 6e 43 75 72 73 6f 72 20 61 nMem, nCursor a
1a7f0 6e 64 20 69 73 45 78 70 6c 61 69 6e 20 61 72 65 nd isExplain are
1a7f1 20 61 6c 6c 20 70 61 73 73 65 64 20 7a 65 72 6f all passed zero
1a7f2 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1a7f3 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1a7f4 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28 0a 20 VdbeMakeReady(.
1a7f5 20 56 64 62 65 20 2a 70 2c 20 20 20 20 20 20 20 Vdbe *p,
1a7f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a7f7 2f 2a 20 54 68 65 20 56 44 42 45 20 2a 2f 0a 20 /* The VDBE */.
1a7f8 20 69 6e 74 20 6e 56 61 72 2c 20 20 20 20 20 20 int nVar,
1a7f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a7fa 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 27 3f 27 /* Number of '?'
1a7fb 20 73 65 65 20 69 6e 20 74 68 65 20 53 51 4c 20 see in the SQL
1a7fc 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 statement */. i
1a7fd 6e 74 20 6e 4d 65 6d 2c 20 20 20 20 20 20 20 20 nt nMem,
1a7fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1a7ff 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 Number of memor
1a800 79 20 63 65 6c 6c 73 20 74 6f 20 61 6c 6c 6f 63 y cells to alloc
1a801 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 75 ate */. int nCu
1a802 72 73 6f 72 2c 20 20 20 20 20 20 20 20 20 20 20 rsor,
1a803 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
1a804 72 20 6f 66 20 63 75 72 73 6f 72 73 20 74 6f 20 r of cursors to
1a805 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 20 69 6e allocate */. in
1a806 74 20 6e 41 72 67 2c 20 20 20 20 20 20 20 20 20 t nArg,
1a807 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1a808 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f Maximum number o
1a809 66 20 61 72 67 73 20 69 6e 20 53 75 62 50 72 6f f args in SubPro
1a80a 67 72 61 6d 73 20 2a 2f 0a 20 20 69 6e 74 20 69 grams */. int i
1a80b 73 45 78 70 6c 61 69 6e 2c 20 20 20 20 20 20 20 sExplain,
1a80c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
1a80d 65 20 69 66 20 74 68 65 20 45 58 50 4c 41 49 4e e if the EXPLAIN
1a80e 20 6b 65 79 77 6f 72 64 73 20 69 73 20 70 72 65 keywords is pre
1a80f 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 75 73 sent */. int us
1a810 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 20 20 esStmtJournal
1a811 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
1a812 20 74 6f 20 73 65 74 20 56 64 62 65 2e 75 73 65 to set Vdbe.use
1a813 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 2a 2f 0a sStmtJournal */.
1a814 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 ){. int n;. sq
1a815 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 lite3 *db = p->d
1a816 62 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 21 b;.. assert( p!
1a817 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
1a818 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d p->magic==VDBE_M
1a819 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 0a 20 20 AGIC_INIT );..
1a81a 2f 2a 20 54 68 65 72 65 20 73 68 6f 75 6c 64 20 /* There should
1a81b 62 65 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 be at least one
1a81c 6f 70 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 61 opcode.. */. a
1a81d 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 ssert( p->nOp>0
1a81e 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 );.. /* Set the
1a81f 20 6d 61 67 69 63 20 74 6f 20 56 44 42 45 5f 4d magic to VDBE_M
1a820 41 47 49 43 5f 52 55 4e 20 73 6f 6f 6e 65 72 20 AGIC_RUN sooner
1a821 72 61 74 68 65 72 20 74 68 61 6e 20 6c 61 74 65 rather than late
1a822 72 2e 20 2a 2f 0a 20 20 70 2d 3e 6d 61 67 69 63 r. */. p->magic
1a823 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 = VDBE_MAGIC_RU
1a824 4e 3b 0a 0a 20 20 2f 2a 20 46 6f 72 20 65 61 63 N;.. /* For eac
1a825 68 20 63 75 72 73 6f 72 20 72 65 71 75 69 72 65 h cursor require
1a826 64 2c 20 61 6c 73 6f 20 61 6c 6c 6f 63 61 74 65 d, also allocate
1a827 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e 20 a memory cell.
1a828 4d 65 6d 6f 72 79 0a 20 20 2a 2a 20 63 65 6c 6c Memory. ** cell
1a829 73 20 28 6e 4d 65 6d 2b 31 2d 6e 43 75 72 73 6f s (nMem+1-nCurso
1a82a 72 29 2e 2e 6e 4d 65 6d 2c 20 69 6e 63 6c 75 73 r)..nMem, inclus
1a82b 69 76 65 2c 20 77 69 6c 6c 20 6e 65 76 65 72 20 ive, will never
1a82c 62 65 20 75 73 65 64 20 62 79 0a 20 20 2a 2a 20 be used by. **
1a82d 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d the vdbe program
1a82e 2e 20 49 6e 73 74 65 61 64 20 74 68 65 79 20 61 . Instead they a
1a82f 72 65 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63 re used to alloc
1a830 61 74 65 20 73 70 61 63 65 20 66 6f 72 0a 20 20 ate space for.
1a831 2a 2a 20 56 64 62 65 43 75 72 73 6f 72 2f 42 74 ** VdbeCursor/Bt
1a832 43 75 72 73 6f 72 20 73 74 72 75 63 74 75 72 65 Cursor structure
1a833 73 2e 20 54 68 65 20 62 6c 6f 62 20 6f 66 20 6d s. The blob of m
1a834 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64 emory associated
1a835 20 77 69 74 68 20 0a 20 20 2a 2a 20 63 75 72 73 with . ** curs
1a836 6f 72 20 30 20 69 73 20 73 74 6f 72 65 64 20 69 or 0 is stored i
1a837 6e 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 6e 4d n memory cell nM
1a838 65 6d 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 em. Memory cell
1a839 28 6e 4d 65 6d 2d 31 29 0a 20 20 2a 2a 20 73 74 (nMem-1). ** st
1a83a 6f 72 65 73 20 74 68 65 20 62 6c 6f 62 20 6f 66 ores the blob of
1a83b 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 memory associat
1a83c 65 64 20 77 69 74 68 20 63 75 72 73 6f 72 20 31 ed with cursor 1
1a83d 2c 20 65 74 63 2e 0a 20 20 2a 2a 0a 20 20 2a 2a , etc.. **. **
1a83e 20 53 65 65 20 61 6c 73 6f 3a 20 61 6c 6c 6f 63 See also: alloc
1a83f 61 74 65 43 75 72 73 6f 72 28 29 2e 0a 20 20 2a ateCursor().. *
1a840 2f 0a 20 20 6e 4d 65 6d 20 2b 3d 20 6e 43 75 72 /. nMem += nCur
1a841 73 6f 72 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 sor;.. /* Alloc
1a842 61 74 65 20 73 70 61 63 65 20 66 6f 72 20 6d 65 ate space for me
1a843 6d 6f 72 79 20 72 65 67 69 73 74 65 72 73 2c 20 mory registers,
1a844 53 51 4c 20 76 61 72 69 61 62 6c 65 73 2c 20 56 SQL variables, V
1a845 44 42 45 20 63 75 72 73 6f 72 73 20 61 6e 64 20 DBE cursors and
1a846 0a 20 20 2a 2a 20 61 6e 20 61 72 72 61 79 20 74 . ** an array t
1a847 6f 20 6d 61 72 73 68 61 6c 20 53 51 4c 20 66 75 o marshal SQL fu
1a848 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 nction arguments
1a849 20 69 6e 2e 20 54 68 69 73 20 69 73 20 6f 6e 6c in. This is onl
1a84a 79 20 64 6f 6e 65 20 74 68 65 0a 20 20 2a 2a 20 y done the. **
1a84b 66 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 first time this
1a84c 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
1a84d 65 64 20 66 6f 72 20 61 20 67 69 76 65 6e 20 56 ed for a given V
1a84e 44 42 45 2c 20 6e 6f 74 20 77 68 65 6e 20 69 74 DBE, not when it
1a84f 20 69 73 0a 20 20 2a 2a 20 62 65 69 6e 67 20 63 is. ** being c
1a850 61 6c 6c 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 alled from sqlit
1a851 65 33 5f 72 65 73 65 74 28 29 20 74 6f 20 72 65 e3_reset() to re
1a852 73 65 74 20 74 68 65 20 76 69 72 74 75 61 6c 20 set the virtual
1a853 6d 61 63 68 69 6e 65 2e 0a 20 20 2a 2f 0a 20 20 machine.. */.
1a854 69 66 28 20 6e 56 61 72 3e 3d 30 20 26 26 20 41 if( nVar>=0 && A
1a855 4c 57 41 59 53 28 64 62 2d 3e 6d 61 6c 6c 6f 63 LWAYS(db->malloc
1a856 46 61 69 6c 65 64 3d 3d 30 29 20 29 7b 0a 20 20 Failed==0) ){.
1a857 20 20 75 38 20 2a 7a 43 73 72 20 3d 20 28 75 38 u8 *zCsr = (u8
1a858 20 2a 29 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f *)&p->aOp[p->nO
1a859 70 5d 3b 0a 20 20 20 20 75 38 20 2a 7a 45 6e 64 p];. u8 *zEnd
1a85a 20 3d 20 28 75 38 20 2a 29 26 70 2d 3e 61 4f 70 = (u8 *)&p->aOp
1a85b 5b 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 5d 3b 0a 20 [p->nOpAlloc];.
1a85c 20 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 int nByte;.
1a85d 20 20 72 65 73 6f 6c 76 65 50 32 56 61 6c 75 65 resolveP2Value
1a85e 73 28 70 2c 20 26 6e 41 72 67 29 3b 0a 20 20 20 s(p, &nArg);.
1a85f 20 70 2d 3e 75 73 65 73 53 74 6d 74 4a 6f 75 72 p->usesStmtJour
1a860 6e 61 6c 20 3d 20 28 75 38 29 75 73 65 73 53 74 nal = (u8)usesSt
1a861 6d 74 4a 6f 75 72 6e 61 6c 3b 0a 20 20 20 20 69 mtJournal;. i
1a862 66 28 20 69 73 45 78 70 6c 61 69 6e 20 26 26 20 f( isExplain &&
1a863 6e 4d 65 6d 3c 31 30 20 29 7b 0a 20 20 20 20 20 nMem<10 ){.
1a864 20 6e 4d 65 6d 20 3d 20 31 30 3b 0a 20 20 20 20 nMem = 10;.
1a865 7d 0a 20 20 20 20 6d 65 6d 73 65 74 28 7a 43 73 }. memset(zCs
1a866 72 2c 20 30 2c 20 7a 45 6e 64 2d 7a 43 73 72 29 r, 0, zEnd-zCsr)
1a867 3b 0a 20 20 20 20 7a 43 73 72 20 2b 3d 20 28 7a ;. zCsr += (z
1a868 43 73 72 20 2d 20 28 75 38 2a 29 30 29 26 37 3b Csr - (u8*)0)&7;
1a869 0a 20 20 20 20 61 73 73 65 72 74 28 20 45 49 47 . assert( EIG
1a86a 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e HT_BYTE_ALIGNMEN
1a86b 54 28 7a 43 73 72 29 20 29 3b 0a 0a 20 20 20 20 T(zCsr) );..
1a86c 64 6f 20 7b 0a 20 20 20 20 20 20 6e 42 79 74 65 do {. nByte
1a86d 20 3d 20 30 3b 0a 20 20 20 20 20 20 61 6c 6c 6f = 0;. allo
1a86e 63 53 70 61 63 65 28 28 63 68 61 72 2a 29 26 70 cSpace((char*)&p
1a86f 2d 3e 61 4d 65 6d 2c 20 6e 4d 65 6d 2a 73 69 7a ->aMem, nMem*siz
1a870 65 6f 66 28 4d 65 6d 29 2c 20 26 7a 43 73 72 2c eof(Mem), &zCsr,
1a871 20 7a 45 6e 64 2c 20 26 6e 42 79 74 65 29 3b 0a zEnd, &nByte);.
1a872 20 20 20 20 20 20 61 6c 6c 6f 63 53 70 61 63 65 allocSpace
1a873 28 28 63 68 61 72 2a 29 26 70 2d 3e 61 56 61 72 ((char*)&p->aVar
1a874 2c 20 6e 56 61 72 2a 73 69 7a 65 6f 66 28 4d 65 , nVar*sizeof(Me
1a875 6d 29 2c 20 26 7a 43 73 72 2c 20 7a 45 6e 64 2c m), &zCsr, zEnd,
1a876 20 26 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 &nByte);.
1a877 61 6c 6c 6f 63 53 70 61 63 65 28 28 63 68 61 72 allocSpace((char
1a878 2a 29 26 70 2d 3e 61 70 41 72 67 2c 20 6e 41 72 *)&p->apArg, nAr
1a879 67 2a 73 69 7a 65 6f 66 28 4d 65 6d 2a 29 2c 20 g*sizeof(Mem*),
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 7a 56 61 72 2c 20 6e 56 61 72 2a 73 69 ->azVar, nVar*si
1a87e 7a 65 6f 66 28 63 68 61 72 2a 29 2c 20 26 7a 43 zeof(char*), &zC
1a87f 73 72 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 74 65 sr, zEnd, &nByte
1a880 29 3b 0a 20 20 20 20 20 20 61 6c 6c 6f 63 53 70 );. allocSp
1a881 61 63 65 28 28 63 68 61 72 2a 29 26 70 2d 3e 61 ace((char*)&p->a
1a882 70 43 73 72 2c 20 0a 20 20 20 20 20 20 20 20 20 pCsr, .
1a883 20 20 20 20 20 20 20 20 6e 43 75 72 73 6f 72 2a nCursor*
1a884 73 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f sizeof(VdbeCurso
1a885 72 2a 29 2c 20 26 7a 43 73 72 2c 20 7a 45 6e 64 r*), &zCsr, zEnd
1a886 2c 20 26 6e 42 79 74 65 0a 20 20 20 20 20 20 29 , &nByte. )
1a887 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 42 79 74 ;. if( nByt
1a888 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e e ){. p->
1a889 70 46 72 65 65 20 3d 20 73 71 6c 69 74 65 33 44 pFree = sqlite3D
1a88a 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 bMallocZero(db,
1a88b 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 7d 0a nByte);. }.
1a88c 20 20 20 20 20 20 7a 43 73 72 20 3d 20 70 2d 3e zCsr = p->
1a88d 70 46 72 65 65 3b 0a 20 20 20 20 20 20 7a 45 6e pFree;. zEn
1a88e 64 20 3d 20 26 7a 43 73 72 5b 6e 42 79 74 65 5d d = &zCsr[nByte]
1a88f 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 6e 42 ;. }while( nB
1a890 79 74 65 20 26 26 20 21 64 62 2d 3e 6d 61 6c 6c yte && !db->mall
1a891 6f 63 46 61 69 6c 65 64 20 29 3b 0a 0a 20 20 20 ocFailed );..
1a892 20 70 2d 3e 6e 43 75 72 73 6f 72 20 3d 20 28 75 p->nCursor = (u
1a893 31 36 29 6e 43 75 72 73 6f 72 3b 0a 20 20 20 20 16)nCursor;.
1a894 69 66 28 20 70 2d 3e 61 56 61 72 20 29 7b 0a 20 if( p->aVar ){.
1a895 20 20 20 20 20 70 2d 3e 6e 56 61 72 20 3d 20 28 p->nVar = (
1a896 79 6e 56 61 72 29 6e 56 61 72 3b 0a 20 20 20 20 ynVar)nVar;.
1a897 20 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c 6e 56 61 for(n=0; n<nVa
1a898 72 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 20 r; n++){.
1a899 20 70 2d 3e 61 56 61 72 5b 6e 5d 2e 66 6c 61 67 p->aVar[n].flag
1a89a 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 s = MEM_Null;.
1a89b 20 20 20 20 20 20 70 2d 3e 61 56 61 72 5b 6e 5d p->aVar[n]
1a89c 2e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 20 20 .db = db;.
1a89d 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 }. }. if(
1a89e 70 2d 3e 61 4d 65 6d 20 29 7b 0a 20 20 20 20 20 p->aMem ){.
1a89f 20 70 2d 3e 61 4d 65 6d 2d 2d 3b 20 20 20 20 20 p->aMem--;
1a8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a8a1 20 2f 2a 20 61 4d 65 6d 5b 5d 20 67 6f 65 73 20 /* aMem[] goes
1a8a2 66 72 6f 6d 20 31 2e 2e 6e 4d 65 6d 20 2a 2f 0a from 1..nMem */.
1a8a3 20 20 20 20 20 20 70 2d 3e 6e 4d 65 6d 20 3d 20 p->nMem =
1a8a4 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 nMem;
1a8a5 20 20 20 20 20 20 2f 2a 20 20 20 20 20 20 20 6e /* n
1a8a6 6f 74 20 66 72 6f 6d 20 30 2e 2e 6e 4d 65 6d 2d ot from 0..nMem-
1a8a7 31 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28 6e 1 */. for(n
1a8a8 3d 31 3b 20 6e 3c 3d 6e 4d 65 6d 3b 20 6e 2b 2b =1; n<=nMem; n++
1a8a9 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 4d ){. p->aM
1a8aa 65 6d 5b 6e 5d 2e 66 6c 61 67 73 20 3d 20 4d 45 em[n].flags = ME
1a8ab 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 M_Null;.
1a8ac 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e 64 62 20 3d 20 p->aMem[n].db =
1a8ad 64 62 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 db;. }.
1a8ae 7d 0a 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c }. }.#ifdef SQL
1a8af 49 54 45 5f 44 45 42 55 47 0a 20 20 66 6f 72 28 ITE_DEBUG. for(
1a8b0 6e 3d 31 3b 20 6e 3c 70 2d 3e 6e 4d 65 6d 3b 20 n=1; n<p->nMem;
1a8b1 6e 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 n++){. assert
1a8b2 28 20 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e 64 62 3d ( p->aMem[n].db=
1a8b3 3d 64 62 20 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 =db );. }.#endi
1a8b4 66 0a 0a 20 20 70 2d 3e 70 63 20 3d 20 2d 31 3b f.. p->pc = -1;
1a8b5 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 . p->rc = SQLIT
1a8b6 45 5f 4f 4b 3b 0a 20 20 70 2d 3e 65 72 72 6f 72 E_OK;. p->error
1a8b7 41 63 74 69 6f 6e 20 3d 20 4f 45 5f 41 62 6f 72 Action = OE_Abor
1a8b8 74 3b 0a 20 20 70 2d 3e 65 78 70 6c 61 69 6e 20 t;. p->explain
1a8b9 7c 3d 20 69 73 45 78 70 6c 61 69 6e 3b 0a 20 20 |= isExplain;.
1a8ba 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 5f p->magic = VDBE_
1a8bb 4d 41 47 49 43 5f 52 55 4e 3b 0a 20 20 70 2d 3e MAGIC_RUN;. p->
1a8bc 6e 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 70 nChange = 0;. p
1a8bd 2d 3e 63 61 63 68 65 43 74 72 20 3d 20 31 3b 0a ->cacheCtr = 1;.
1a8be 20 20 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c p->minWriteFil
1a8bf 65 46 6f 72 6d 61 74 20 3d 20 32 35 35 3b 0a 20 eFormat = 255;.
1a8c0 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20 3d p->iStatement =
1a8c1 20 30 3b 0a 23 69 66 64 65 66 20 56 44 42 45 5f 0;.#ifdef VDBE_
1a8c2 50 52 4f 46 49 4c 45 0a 20 20 7b 0a 20 20 20 20 PROFILE. {.
1a8c3 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 int i;. for(i
1a8c4 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b =0; i<p->nOp; i+
1a8c5 2b 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61 4f 70 +){. p->aOp
1a8c6 5b 69 5d 2e 63 6e 74 20 3d 20 30 3b 0a 20 20 20 [i].cnt = 0;.
1a8c7 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63 p->aOp[i].cyc
1a8c8 6c 65 73 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 les = 0;. }.
1a8c9 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a }.#endif.}../*.
1a8ca 2a 2a 20 43 6c 6f 73 65 20 61 20 56 44 42 45 20 ** Close a VDBE
1a8cb 63 75 72 73 6f 72 20 61 6e 64 20 72 65 6c 65 61 cursor and relea
1a8cc 73 65 20 61 6c 6c 20 74 68 65 20 72 65 73 6f 75 se all the resou
1a8cd 72 63 65 73 20 74 68 61 74 20 63 75 72 73 6f 72 rces that cursor
1a8ce 20 0a 2a 2a 20 68 61 70 70 65 6e 73 20 74 6f 20 .** happens to
1a8cf 68 6f 6c 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f hold..*/.SQLITE_
1a8d0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1a8d1 69 74 65 33 56 64 62 65 46 72 65 65 43 75 72 73 ite3VdbeFreeCurs
1a8d2 6f 72 28 56 64 62 65 20 2a 70 2c 20 56 64 62 65 or(Vdbe *p, Vdbe
1a8d3 43 75 72 73 6f 72 20 2a 70 43 78 29 7b 0a 20 20 Cursor *pCx){.
1a8d4 69 66 28 20 70 43 78 3d 3d 30 20 29 7b 0a 20 20 if( pCx==0 ){.
1a8d5 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 return;. }.
1a8d6 69 66 28 20 70 43 78 2d 3e 70 42 74 20 29 7b 0a if( pCx->pBt ){.
1a8d7 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
1a8d8 43 6c 6f 73 65 28 70 43 78 2d 3e 70 42 74 29 3b Close(pCx->pBt);
1a8d9 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 43 78 2d . /* The pCx-
1a8da 3e 70 43 75 72 73 6f 72 20 77 69 6c 6c 20 62 65 >pCursor will be
1a8db 20 63 6c 6f 73 65 20 61 75 74 6f 6d 61 74 69 63 close automatic
1a8dc 61 6c 6c 79 2c 20 69 66 20 69 74 20 65 78 69 73 ally, if it exis
1a8dd 74 73 2c 20 62 79 0a 20 20 20 20 2a 2a 20 74 68 ts, by. ** th
1a8de 65 20 63 61 6c 6c 20 61 62 6f 76 65 2e 20 2a 2f e call above. */
1a8df 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 43 78 . }else if( pCx
1a8e0 2d 3e 70 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 ->pCursor ){.
1a8e1 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f sqlite3BtreeClo
1a8e2 73 65 43 75 72 73 6f 72 28 70 43 78 2d 3e 70 43 seCursor(pCx->pC
1a8e3 75 72 73 6f 72 29 3b 0a 20 20 7d 0a 23 69 66 6e ursor);. }.#ifn
1a8e4 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1a8e5 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 VIRTUALTABLE. i
1a8e6 66 28 20 70 43 78 2d 3e 70 56 74 61 62 43 75 72 f( pCx->pVtabCur
1a8e7 73 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 sor ){. sqlit
1a8e8 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a e3_vtab_cursor *
1a8e9 70 56 74 61 62 43 75 72 73 6f 72 20 3d 20 70 43 pVtabCursor = pC
1a8ea 78 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 3b 0a x->pVtabCursor;.
1a8eb 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 const sqlite
1a8ec 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 3_module *pModul
1a8ed 65 20 3d 20 70 43 78 2d 3e 70 4d 6f 64 75 6c 65 e = pCx->pModule
1a8ee 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d ;. p->inVtabM
1a8ef 65 74 68 6f 64 20 3d 20 31 3b 0a 20 20 20 20 28 ethod = 1;. (
1a8f0 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 void)sqlite3Safe
1a8f1 74 79 4f 66 66 28 70 2d 3e 64 62 29 3b 0a 20 20 tyOff(p->db);.
1a8f2 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73 pModule->xClos
1a8f3 65 28 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a e(pVtabCursor);.
1a8f4 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 (void)sqlite
1a8f5 33 53 61 66 65 74 79 4f 6e 28 70 2d 3e 64 62 29 3SafetyOn(p->db)
1a8f6 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d ;. p->inVtabM
1a8f7 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 7d 0a 23 ethod = 0;. }.#
1a8f8 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 endif.}../*.** C
1a8f9 6f 70 79 20 74 68 65 20 76 61 6c 75 65 73 20 73 opy the values s
1a8fa 74 6f 72 65 64 20 69 6e 20 74 68 65 20 56 64 62 tored in the Vdb
1a8fb 65 46 72 61 6d 65 20 73 74 72 75 63 74 75 72 65 eFrame structure
1a8fc 20 74 6f 20 69 74 73 20 56 64 62 65 2e 20 54 68 to its Vdbe. Th
1a8fd 69 73 0a 2a 2a 20 69 73 20 75 73 65 64 2c 20 66 is.** is used, f
1a8fe 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e or example, when
1a8ff 20 61 20 74 72 69 67 67 65 72 20 73 75 62 2d 70 a trigger sub-p
1a900 72 6f 67 72 61 6d 20 69 73 20 68 61 6c 74 65 64 rogram is halted
1a901 20 74 6f 20 72 65 73 74 6f 72 65 0a 2a 2a 20 63 to restore.** c
1a902 6f 6e 74 72 6f 6c 20 74 6f 20 74 68 65 20 6d 61 ontrol to the ma
1a903 69 6e 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 53 in program..*/.S
1a904 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1a905 74 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 61 t sqlite3VdbeFra
1a906 6d 65 52 65 73 74 6f 72 65 28 56 64 62 65 46 72 meRestore(VdbeFr
1a907 61 6d 65 20 2a 70 46 72 61 6d 65 29 7b 0a 20 20 ame *pFrame){.
1a908 56 64 62 65 20 2a 76 20 3d 20 70 46 72 61 6d 65 Vdbe *v = pFrame
1a909 2d 3e 76 3b 0a 20 20 76 2d 3e 61 4f 70 20 3d 20 ->v;. v->aOp =
1a90a 70 46 72 61 6d 65 2d 3e 61 4f 70 3b 0a 20 20 76 pFrame->aOp;. v
1a90b 2d 3e 6e 4f 70 20 3d 20 70 46 72 61 6d 65 2d 3e ->nOp = pFrame->
1a90c 6e 4f 70 3b 0a 20 20 76 2d 3e 61 4d 65 6d 20 3d nOp;. v->aMem =
1a90d 20 70 46 72 61 6d 65 2d 3e 61 4d 65 6d 3b 0a 20 pFrame->aMem;.
1a90e 20 76 2d 3e 6e 4d 65 6d 20 3d 20 70 46 72 61 6d v->nMem = pFram
1a90f 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 76 2d 3e 61 70 e->nMem;. v->ap
1a910 43 73 72 20 3d 20 70 46 72 61 6d 65 2d 3e 61 70 Csr = pFrame->ap
1a911 43 73 72 3b 0a 20 20 76 2d 3e 6e 43 75 72 73 6f Csr;. v->nCurso
1a912 72 20 3d 20 70 46 72 61 6d 65 2d 3e 6e 43 75 72 r = pFrame->nCur
1a913 73 6f 72 3b 0a 20 20 76 2d 3e 64 62 2d 3e 6c 61 sor;. v->db->la
1a914 73 74 52 6f 77 69 64 20 3d 20 70 46 72 61 6d 65 stRowid = pFrame
1a915 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20 76 ->lastRowid;. v
1a916 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 70 46 72 61 ->nChange = pFra
1a917 6d 65 2d 3e 6e 43 68 61 6e 67 65 3b 0a 20 20 72 me->nChange;. r
1a918 65 74 75 72 6e 20 70 46 72 61 6d 65 2d 3e 70 63 eturn pFrame->pc
1a919 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 ;.}../*.** Close
1a91a 20 61 6c 6c 20 63 75 72 73 6f 72 73 2e 0a 2a 2a all cursors..**
1a91b 0a 2a 2a 20 41 6c 73 6f 20 72 65 6c 65 61 73 65 .** Also release
1a91c 20 61 6e 79 20 64 79 6e 61 6d 69 63 20 6d 65 6d any dynamic mem
1a91d 6f 72 79 20 68 65 6c 64 20 62 79 20 74 68 65 20 ory held by the
1a91e 56 4d 20 69 6e 20 74 68 65 20 56 64 62 65 2e 61 VM in the Vdbe.a
1a91f 4d 65 6d 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 63 Mem memory .** c
1a920 65 6c 6c 20 61 72 72 61 79 2e 20 54 68 69 73 20 ell array. This
1a921 69 73 20 6e 65 63 65 73 73 61 72 79 20 61 73 20 is necessary as
1a922 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 the memory cell
1a923 61 72 72 61 79 20 6d 61 79 20 63 6f 6e 74 61 69 array may contai
1a924 6e 0a 2a 2a 20 70 6f 69 6e 74 65 72 73 20 74 6f n.** pointers to
1a925 20 56 64 62 65 46 72 61 6d 65 20 6f 62 6a 65 63 VdbeFrame objec
1a926 74 73 2c 20 77 68 69 63 68 20 6d 61 79 20 69 6e ts, which may in
1a927 20 74 75 72 6e 20 63 6f 6e 74 61 69 6e 20 70 6f turn contain po
1a928 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20 6f 70 65 inters to.** ope
1a929 6e 20 63 75 72 73 6f 72 73 2e 0a 2a 2f 0a 73 74 n cursors..*/.st
1a92a 61 74 69 63 20 76 6f 69 64 20 63 6c 6f 73 65 41 atic void closeA
1a92b 6c 6c 43 75 72 73 6f 72 73 28 56 64 62 65 20 2a llCursors(Vdbe *
1a92c 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 70 46 72 p){. if( p->pFr
1a92d 61 6d 65 20 29 7b 0a 20 20 20 20 56 64 62 65 46 ame ){. VdbeF
1a92e 72 61 6d 65 20 2a 70 46 72 61 6d 65 20 3d 20 70 rame *pFrame = p
1a92f 2d 3e 70 46 72 61 6d 65 3b 0a 20 20 20 20 66 6f ->pFrame;. fo
1a930 72 28 70 46 72 61 6d 65 3d 70 2d 3e 70 46 72 61 r(pFrame=p->pFra
1a931 6d 65 3b 20 70 46 72 61 6d 65 2d 3e 70 50 61 72 me; pFrame->pPar
1a932 65 6e 74 3b 20 70 46 72 61 6d 65 3d 70 46 72 61 ent; pFrame=pFra
1a933 6d 65 2d 3e 70 50 61 72 65 6e 74 29 3b 0a 20 20 me->pParent);.
1a934 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 61 sqlite3VdbeFra
1a935 6d 65 52 65 73 74 6f 72 65 28 70 46 72 61 6d 65 meRestore(pFrame
1a936 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 46 72 61 );. }. p->pFra
1a937 6d 65 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 46 72 me = 0;. p->nFr
1a938 61 6d 65 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 ame = 0;.. if(
1a939 70 2d 3e 61 70 43 73 72 20 29 7b 0a 20 20 20 20 p->apCsr ){.
1a93a 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 int i;. for(i
1a93b 3d 30 3b 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 =0; i<p->nCursor
1a93c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 56 64 ; i++){. Vd
1a93d 62 65 43 75 72 73 6f 72 20 2a 70 43 20 3d 20 70 beCursor *pC = p
1a93e 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 20 20 ->apCsr[i];.
1a93f 20 20 69 66 28 20 70 43 20 29 7b 0a 20 20 20 20 if( pC ){.
1a940 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 sqlite3VdbeF
1a941 72 65 65 43 75 72 73 6f 72 28 70 2c 20 70 43 29 reeCursor(p, pC)
1a942 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 70 43 ;. p->apC
1a943 73 72 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 20 sr[i] = 0;.
1a944 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 }. }. }. i
1a945 66 28 20 70 2d 3e 61 4d 65 6d 20 29 7b 0a 20 20 f( p->aMem ){.
1a946 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 releaseMemArra
1a947 79 28 26 70 2d 3e 61 4d 65 6d 5b 31 5d 2c 20 70 y(&p->aMem[1], p
1a948 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 7d 0a 7d 0a 0a ->nMem);. }.}..
1a949 2f 2a 0a 2a 2a 20 43 6c 65 61 6e 20 75 70 20 74 /*.** Clean up t
1a94a 68 65 20 56 4d 20 61 66 74 65 72 20 65 78 65 63 he VM after exec
1a94b 75 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 ution..**.** Thi
1a94c 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 61 s routine will a
1a94d 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 6c 6f utomatically clo
1a94e 73 65 20 61 6e 79 20 63 75 72 73 6f 72 73 2c 20 se any cursors,
1a94f 6c 69 73 74 73 2c 20 61 6e 64 2f 6f 72 0a 2a 2a lists, and/or.**
1a950 20 73 6f 72 74 65 72 73 20 74 68 61 74 20 77 65 sorters that we
1a951 72 65 20 6c 65 66 74 20 6f 70 65 6e 2e 20 20 49 re left open. I
1a952 74 20 61 6c 73 6f 20 64 65 6c 65 74 65 73 20 74 t also deletes t
1a953 68 65 20 76 61 6c 75 65 73 20 6f 66 0a 2a 2a 20 he values of.**
1a954 76 61 72 69 61 62 6c 65 73 20 69 6e 20 74 68 65 variables in the
1a955 20 61 56 61 72 5b 5d 20 61 72 72 61 79 2e 0a 2a aVar[] array..*
1a956 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 43 6c /.static void Cl
1a957 65 61 6e 75 70 28 56 64 62 65 20 2a 70 29 7b 0a eanup(Vdbe *p){.
1a958 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
1a959 70 2d 3e 64 62 3b 0a 0a 23 69 66 64 65 66 20 53 p->db;..#ifdef S
1a95a 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 2f 2a QLITE_DEBUG. /*
1a95b 20 45 78 65 63 75 74 65 20 61 73 73 65 72 74 28 Execute assert(
1a95c 29 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 ) statements to
1a95d 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 20 ensure that the
1a95e 56 64 62 65 2e 61 70 43 73 72 5b 5d 20 61 6e 64 Vdbe.apCsr[] and
1a95f 20 0a 20 20 2a 2a 20 56 64 62 65 2e 61 4d 65 6d . ** Vdbe.aMem
1a960 5b 5d 20 61 72 72 61 79 73 20 68 61 76 65 20 61 [] arrays have a
1a961 6c 72 65 61 64 79 20 62 65 65 6e 20 63 6c 65 61 lready been clea
1a962 6e 65 64 20 75 70 2e 20 20 2a 2f 0a 20 20 69 6e ned up. */. in
1a963 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 t i;. for(i=0;
1a964 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 3b 20 69 2b i<p->nCursor; i+
1a965 2b 29 20 61 73 73 65 72 74 28 20 70 2d 3e 61 70 +) assert( p->ap
1a966 43 73 72 3d 3d 30 20 7c 7c 20 70 2d 3e 61 70 43 Csr==0 || p->apC
1a967 73 72 5b 69 5d 3d 3d 30 20 29 3b 0a 20 20 66 6f sr[i]==0 );. fo
1a968 72 28 69 3d 31 3b 20 69 3c 3d 70 2d 3e 6e 4d 65 r(i=1; i<=p->nMe
1a969 6d 3b 20 69 2b 2b 29 20 61 73 73 65 72 74 28 20 m; i++) assert(
1a96a 70 2d 3e 61 4d 65 6d 3d 3d 30 20 7c 7c 20 70 2d p->aMem==0 || p-
1a96b 3e 61 4d 65 6d 5b 69 5d 2e 66 6c 61 67 73 3d 3d >aMem[i].flags==
1a96c 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 23 65 6e 64 MEM_Null );.#end
1a96d 69 66 0a 0a 20 20 73 71 6c 69 74 65 33 44 62 46 if.. sqlite3DbF
1a96e 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d ree(db, p->zErrM
1a96f 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 sg);. p->zErrMs
1a970 67 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 52 65 73 g = 0;. p->pRes
1a971 75 6c 74 53 65 74 20 3d 20 30 3b 0a 7d 0a 0a 2f ultSet = 0;.}../
1a972 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 6e 75 6d *.** Set the num
1a973 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20 63 6f ber of result co
1a974 6c 75 6d 6e 73 20 74 68 61 74 20 77 69 6c 6c 20 lumns that will
1a975 62 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 be returned by t
1a976 68 69 73 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65 his SQL.** state
1a977 6d 65 6e 74 2e 20 54 68 69 73 20 69 73 20 6e 6f ment. This is no
1a978 77 20 73 65 74 20 61 74 20 63 6f 6d 70 69 6c 65 w set at compile
1a979 20 74 69 6d 65 2c 20 72 61 74 68 65 72 20 74 68 time, rather th
1a97a 61 6e 20 64 75 72 69 6e 67 0a 2a 2a 20 65 78 65 an during.** exe
1a97b 63 75 74 69 6f 6e 20 6f 66 20 74 68 65 20 76 64 cution of the vd
1a97c 62 65 20 70 72 6f 67 72 61 6d 20 73 6f 20 74 68 be program so th
1a97d 61 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d at sqlite3_colum
1a97e 6e 5f 63 6f 75 6e 74 28 29 20 63 61 6e 0a 2a 2a n_count() can.**
1a97f 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e 20 61 6e be called on an
1a980 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 62 SQL statement b
1a981 65 66 6f 72 65 20 73 71 6c 69 74 65 33 5f 73 74 efore sqlite3_st
1a982 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ep()..*/.SQLITE_
1a983 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1a984 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f ite3VdbeSetNumCo
1a985 6c 73 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 ls(Vdbe *p, int
1a986 6e 52 65 73 43 6f 6c 75 6d 6e 29 7b 0a 20 20 4d nResColumn){. M
1a987 65 6d 20 2a 70 43 6f 6c 4e 61 6d 65 3b 0a 20 20 em *pColName;.
1a988 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33 int n;. sqlite3
1a989 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 *db = p->db;..
1a98a 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 releaseMemArray
1a98b 28 70 2d 3e 61 43 6f 6c 4e 61 6d 65 2c 20 70 2d (p->aColName, p-
1a98c 3e 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e >nResColumn*COLN
1a98d 41 4d 45 5f 4e 29 3b 0a 20 20 73 71 6c 69 74 65 AME_N);. sqlite
1a98e 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61 3DbFree(db, p->a
1a98f 43 6f 6c 4e 61 6d 65 29 3b 0a 20 20 6e 20 3d 20 ColName);. n =
1a990 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 nResColumn*COLNA
1a991 4d 45 5f 4e 3b 0a 20 20 70 2d 3e 6e 52 65 73 43 ME_N;. p->nResC
1a992 6f 6c 75 6d 6e 20 3d 20 28 75 31 36 29 6e 52 65 olumn = (u16)nRe
1a993 73 43 6f 6c 75 6d 6e 3b 0a 20 20 70 2d 3e 61 43 sColumn;. p->aC
1a994 6f 6c 4e 61 6d 65 20 3d 20 70 43 6f 6c 4e 61 6d olName = pColNam
1a995 65 20 3d 20 28 4d 65 6d 2a 29 73 71 6c 69 74 65 e = (Mem*)sqlite
1a996 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 3DbMallocZero(db
1a997 2c 20 73 69 7a 65 6f 66 28 4d 65 6d 29 2a 6e 20 , sizeof(Mem)*n
1a998 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 43 6f 6c );. if( p->aCol
1a999 4e 61 6d 65 3d 3d 30 20 29 20 72 65 74 75 72 6e Name==0 ) return
1a99a 3b 0a 20 20 77 68 69 6c 65 28 20 6e 2d 2d 20 3e ;. while( n-- >
1a99b 20 30 20 29 7b 0a 20 20 20 20 70 43 6f 6c 4e 61 0 ){. pColNa
1a99c 6d 65 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f me->flags = MEM_
1a99d 4e 75 6c 6c 3b 0a 20 20 20 20 70 43 6f 6c 4e 61 Null;. pColNa
1a99e 6d 65 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a me->db = p->db;.
1a99f 20 20 20 20 70 43 6f 6c 4e 61 6d 65 2b 2b 3b 0a pColName++;.
1a9a0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 }.}../*.** Set
1a9a1 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 the name of the
1a9a2 20 69 64 78 27 74 68 20 63 6f 6c 75 6d 6e 20 74 idx'th column t
1a9a3 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20 62 79 o be returned by
1a9a4 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 the SQL stateme
1a9a5 6e 74 2e 0a 2a 2a 20 7a 4e 61 6d 65 20 6d 75 73 nt..** zName mus
1a9a6 74 20 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74 t be a pointer t
1a9a7 6f 20 61 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 o a nul terminat
1a9a8 65 64 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a ed string..**.**
1a9a9 20 54 68 69 73 20 63 61 6c 6c 20 6d 75 73 74 20 This call must
1a9aa 62 65 20 6d 61 64 65 20 61 66 74 65 72 20 61 20 be made after a
1a9ab 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 56 call to sqlite3V
1a9ac 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 29 2e dbeSetNumCols().
1a9ad 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6e 61 6c .**.** The final
1a9ae 20 70 61 72 61 6d 65 74 65 72 2c 20 78 44 65 6c parameter, xDel
1a9af 2c 20 6d 75 73 74 20 62 65 20 6f 6e 65 20 6f 66 , must be one of
1a9b0 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 2c SQLITE_DYNAMIC,
1a9b1 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 0a 2a SQLITE_STATIC.*
1a9b2 2a 20 6f 72 20 53 51 4c 49 54 45 5f 54 52 41 4e * or SQLITE_TRAN
1a9b3 53 49 45 4e 54 2e 20 49 66 20 69 74 20 69 73 20 SIENT. If it is
1a9b4 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 2c 20 SQLITE_DYNAMIC,
1a9b5 74 68 65 6e 20 74 68 65 20 62 75 66 66 65 72 20 then the buffer
1a9b6 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 62 79 pointed.** to by
1a9b7 20 7a 4e 61 6d 65 20 77 69 6c 6c 20 62 65 20 66 zName will be f
1a9b8 72 65 65 64 20 62 79 20 73 71 6c 69 74 65 33 44 reed by sqlite3D
1a9b9 62 46 72 65 65 28 29 20 77 68 65 6e 20 74 68 65 bFree() when the
1a9ba 20 76 64 62 65 20 69 73 20 64 65 73 74 72 6f 79 vdbe is destroy
1a9bb 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
1a9bc 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1a9bd 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 3VdbeSetColName(
1a9be 0a 20 20 56 64 62 65 20 2a 70 2c 20 20 20 20 20 . Vdbe *p,
1a9bf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a9c0 20 20 20 20 2f 2a 20 56 64 62 65 20 62 65 69 6e /* Vdbe bein
1a9c1 67 20 63 6f 6e 66 69 67 75 72 65 64 20 2a 2f 0a g configured */.
1a9c2 20 20 69 6e 74 20 69 64 78 2c 20 20 20 20 20 20 int idx,
1a9c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a9c4 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 /* Index of c
1a9c5 6f 6c 75 6d 6e 20 7a 4e 61 6d 65 20 61 70 70 6c olumn zName appl
1a9c6 69 65 73 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 ies to */. int
1a9c7 76 61 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 var,
1a9c8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1a9c9 4f 6e 65 20 6f 66 20 74 68 65 20 43 4f 4c 4e 41 One of the COLNA
1a9ca 4d 45 5f 2a 20 63 6f 6e 73 74 61 6e 74 73 20 2a ME_* constants *
1a9cb 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
1a9cc 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20 zName,
1a9cd 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
1a9ce 74 6f 20 62 75 66 66 65 72 20 63 6f 6e 74 61 69 to buffer contai
1a9cf 6e 69 6e 67 20 6e 61 6d 65 20 2a 2f 0a 20 20 76 ning name */. v
1a9d0 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 oid (*xDel)(void
1a9d1 2a 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *)
1a9d2 2f 2a 20 4d 65 6d 6f 72 79 20 6d 61 6e 61 67 65 /* Memory manage
1a9d3 6d 65 6e 74 20 73 74 72 61 74 65 67 79 20 66 6f ment strategy fo
1a9d4 72 20 7a 4e 61 6d 65 20 2a 2f 0a 29 7b 0a 20 20 r zName */.){.
1a9d5 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 20 2a 70 int rc;. Mem *p
1a9d6 43 6f 6c 4e 61 6d 65 3b 0a 20 20 61 73 73 65 72 ColName;. asser
1a9d7 74 28 20 69 64 78 3c 70 2d 3e 6e 52 65 73 43 6f t( idx<p->nResCo
1a9d8 6c 75 6d 6e 20 29 3b 0a 20 20 61 73 73 65 72 74 lumn );. assert
1a9d9 28 20 76 61 72 3c 43 4f 4c 4e 41 4d 45 5f 4e 20 ( var<COLNAME_N
1a9da 29 3b 0a 20 20 69 66 28 20 70 2d 3e 64 62 2d 3e );. if( p->db->
1a9db 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a mallocFailed ){.
1a9dc 20 20 20 20 61 73 73 65 72 74 28 20 21 7a 4e 61 assert( !zNa
1a9dd 6d 65 20 7c 7c 20 78 44 65 6c 21 3d 53 51 4c 49 me || xDel!=SQLI
1a9de 54 45 5f 44 59 4e 41 4d 49 43 20 29 3b 0a 20 20 TE_DYNAMIC );.
1a9df 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1a9e0 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 61 73 73 NOMEM;. }. ass
1a9e1 65 72 74 28 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 ert( p->aColName
1a9e2 21 3d 30 20 29 3b 0a 20 20 70 43 6f 6c 4e 61 6d !=0 );. pColNam
1a9e3 65 20 3d 20 26 28 70 2d 3e 61 43 6f 6c 4e 61 6d e = &(p->aColNam
1a9e4 65 5b 69 64 78 2b 76 61 72 2a 70 2d 3e 6e 52 65 e[idx+var*p->nRe
1a9e5 73 43 6f 6c 75 6d 6e 5d 29 3b 0a 20 20 72 63 20 sColumn]);. rc
1a9e6 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d = sqlite3VdbeMem
1a9e7 53 65 74 53 74 72 28 70 43 6f 6c 4e 61 6d 65 2c SetStr(pColName,
1a9e8 20 7a 4e 61 6d 65 2c 20 2d 31 2c 20 53 51 4c 49 zName, -1, SQLI
1a9e9 54 45 5f 55 54 46 38 2c 20 78 44 65 6c 29 3b 0a TE_UTF8, xDel);.
1a9ea 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 30 20 assert( rc!=0
1a9eb 7c 7c 20 21 7a 4e 61 6d 65 20 7c 7c 20 28 70 43 || !zName || (pC
1a9ec 6f 6c 4e 61 6d 65 2d 3e 66 6c 61 67 73 26 4d 45 olName->flags&ME
1a9ed 4d 5f 54 65 72 6d 29 21 3d 30 20 29 3b 0a 20 20 M_Term)!=0 );.
1a9ee 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
1a9ef 0a 2a 2a 20 41 20 72 65 61 64 20 6f 72 20 77 72 .** A read or wr
1a9f0 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ite transaction
1a9f1 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 may or may not b
1a9f2 65 20 61 63 74 69 76 65 20 6f 6e 20 64 61 74 61 e active on data
1a9f3 62 61 73 65 20 68 61 6e 64 6c 65 0a 2a 2a 20 64 base handle.** d
1a9f4 62 2e 20 49 66 20 61 20 74 72 61 6e 73 61 63 74 b. If a transact
1a9f5 69 6f 6e 20 69 73 20 61 63 74 69 76 65 2c 20 63 ion is active, c
1a9f6 6f 6d 6d 69 74 20 69 74 2e 20 49 66 20 74 68 65 ommit it. If the
1a9f7 72 65 20 69 73 20 61 0a 2a 2a 20 77 72 69 74 65 re is a.** write
1a9f8 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 70 61 -transaction spa
1a9f9 6e 6e 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 nning more than
1a9fa 6f 6e 65 20 64 61 74 61 62 61 73 65 20 66 69 6c one database fil
1a9fb 65 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a e, this routine.
1a9fc 2a 2a 20 74 61 6b 65 73 20 63 61 72 65 20 6f 66 ** takes care of
1a9fd 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
1a9fe 6e 61 6c 20 74 72 69 63 6b 65 72 79 2e 0a 2a 2f nal trickery..*/
1a9ff 0a 73 74 61 74 69 63 20 69 6e 74 20 76 64 62 65 .static int vdbe
1aa00 43 6f 6d 6d 69 74 28 73 71 6c 69 74 65 33 20 2a Commit(sqlite3 *
1aa01 64 62 2c 20 56 64 62 65 20 2a 70 29 7b 0a 20 20 db, Vdbe *p){.
1aa02 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 54 72 int i;. int nTr
1aa03 61 6e 73 20 3d 20 30 3b 20 20 2f 2a 20 4e 75 6d ans = 0; /* Num
1aa04 62 65 72 20 6f 66 20 64 61 74 61 62 61 73 65 73 ber of databases
1aa05 20 77 69 74 68 20 61 6e 20 61 63 74 69 76 65 20 with an active
1aa06 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f write-transactio
1aa07 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 n */. int rc =
1aa08 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 SQLITE_OK;. int
1aa09 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20 3d 20 30 needXcommit = 0
1aa0a 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 ;..#ifdef SQLITE
1aa0b 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
1aa0c 4c 45 0a 20 20 2f 2a 20 57 69 74 68 20 74 68 69 LE. /* With thi
1aa0d 73 20 6f 70 74 69 6f 6e 2c 20 73 71 6c 69 74 65 s option, sqlite
1aa0e 33 56 74 61 62 53 79 6e 63 28 29 20 69 73 20 64 3VtabSync() is d
1aa0f 65 66 69 6e 65 64 20 74 6f 20 62 65 20 73 69 6d efined to be sim
1aa10 70 6c 79 20 0a 20 20 2a 2a 20 53 51 4c 49 54 45 ply . ** SQLITE
1aa11 5f 4f 4b 20 73 6f 20 70 20 69 73 20 6e 6f 74 20 _OK so p is not
1aa12 75 73 65 64 2e 20 0a 20 20 2a 2f 0a 20 20 55 4e used. . */. UN
1aa13 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 USED_PARAMETER(p
1aa14 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 );.#endif.. /*
1aa15 42 65 66 6f 72 65 20 64 6f 69 6e 67 20 61 6e 79 Before doing any
1aa16 74 68 69 6e 67 20 65 6c 73 65 2c 20 63 61 6c 6c thing else, call
1aa17 20 74 68 65 20 78 53 79 6e 63 28 29 20 63 61 6c the xSync() cal
1aa18 6c 62 61 63 6b 20 66 6f 72 20 61 6e 79 0a 20 20 lback for any.
1aa19 2a 2a 20 76 69 72 74 75 61 6c 20 6d 6f 64 75 6c ** virtual modul
1aa1a 65 20 74 61 62 6c 65 73 20 77 72 69 74 74 65 6e e tables written
1aa1b 20 69 6e 20 74 68 69 73 20 74 72 61 6e 73 61 63 in this transac
1aa1c 74 69 6f 6e 2e 20 54 68 69 73 20 68 61 73 20 74 tion. This has t
1aa1d 6f 0a 20 20 2a 2a 20 62 65 20 64 6f 6e 65 20 62 o. ** be done b
1aa1e 65 66 6f 72 65 20 64 65 74 65 72 6d 69 6e 69 6e efore determinin
1aa1f 67 20 77 68 65 74 68 65 72 20 61 20 6d 61 73 74 g whether a mast
1aa20 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 er journal file
1aa21 69 73 20 0a 20 20 2a 2a 20 72 65 71 75 69 72 65 is . ** require
1aa22 64 2c 20 61 73 20 61 6e 20 78 53 79 6e 63 28 29 d, as an xSync()
1aa23 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20 61 64 callback may ad
1aa24 64 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 61 d an attached da
1aa25 74 61 62 61 73 65 0a 20 20 2a 2a 20 74 6f 20 74 tabase. ** to t
1aa26 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a he transaction..
1aa27 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 */. rc = sqli
1aa28 74 65 33 56 74 61 62 53 79 6e 63 28 64 62 2c 20 te3VtabSync(db,
1aa29 26 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 &p->zErrMsg);.
1aa2a 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
1aa2b 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 K ){. return
1aa2c 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 rc;. }.. /* Th
1aa2d 69 73 20 6c 6f 6f 70 20 64 65 74 65 72 6d 69 6e is loop determin
1aa2e 65 73 20 28 61 29 20 69 66 20 74 68 65 20 63 6f es (a) if the co
1aa2f 6d 6d 69 74 20 68 6f 6f 6b 20 73 68 6f 75 6c 64 mmit hook should
1aa30 20 62 65 20 69 6e 76 6f 6b 65 64 20 61 6e 64 0a be invoked and.
1aa31 20 20 2a 2a 20 28 62 29 20 68 6f 77 20 6d 61 6e ** (b) how man
1aa32 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 y database files
1aa33 20 68 61 76 65 20 6f 70 65 6e 20 77 72 69 74 65 have open write
1aa34 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 2c 20 6e transactions, n
1aa35 6f 74 20 0a 20 20 2a 2a 20 69 6e 63 6c 75 64 69 ot . ** includi
1aa36 6e 67 20 74 68 65 20 74 65 6d 70 20 64 61 74 61 ng the temp data
1aa37 62 61 73 65 2e 20 28 62 29 20 69 73 20 69 6d 70 base. (b) is imp
1aa38 6f 72 74 61 6e 74 20 62 65 63 61 75 73 65 20 69 ortant because i
1aa39 66 20 6d 6f 72 65 20 74 68 61 6e 20 0a 20 20 2a f more than . *
1aa3a 2a 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 66 * one database f
1aa3b 69 6c 65 20 68 61 73 20 61 6e 20 6f 70 65 6e 20 ile has an open
1aa3c 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f write transactio
1aa3d 6e 2c 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 n, a master jour
1aa3e 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 69 73 nal. ** file is
1aa3f 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 61 6e required for an
1aa40 20 61 74 6f 6d 69 63 20 63 6f 6d 6d 69 74 2e 0a atomic commit..
1aa41 20 20 2a 2f 20 0a 20 20 66 6f 72 28 69 3d 30 3b */ . for(i=0;
1aa42 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 i<db->nDb; i++)
1aa43 7b 20 0a 20 20 20 20 42 74 72 65 65 20 2a 70 42 { . Btree *pB
1aa44 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 t = db->aDb[i].p
1aa45 42 74 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 Bt;. if( sqli
1aa46 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e te3BtreeIsInTran
1aa47 73 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 s(pBt) ){.
1aa48 6e 65 65 64 58 63 6f 6d 6d 69 74 20 3d 20 31 3b needXcommit = 1;
1aa49 0a 20 20 20 20 20 20 69 66 28 20 69 21 3d 31 20 . if( i!=1
1aa4a 29 20 6e 54 72 61 6e 73 2b 2b 3b 0a 20 20 20 20 ) nTrans++;.
1aa4b 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 }. }.. /* If t
1aa4c 68 65 72 65 20 61 72 65 20 61 6e 79 20 77 72 69 here are any wri
1aa4d 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 te-transactions
1aa4e 61 74 20 61 6c 6c 2c 20 69 6e 76 6f 6b 65 20 74 at all, invoke t
1aa4f 68 65 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 2a he commit hook *
1aa50 2f 0a 20 20 69 66 28 20 6e 65 65 64 58 63 6f 6d /. if( needXcom
1aa51 6d 69 74 20 26 26 20 64 62 2d 3e 78 43 6f 6d 6d mit && db->xComm
1aa52 69 74 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 itCallback ){.
1aa53 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 (void)sqlite3S
1aa54 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 afetyOff(db);.
1aa55 20 20 72 63 20 3d 20 64 62 2d 3e 78 43 6f 6d 6d rc = db->xComm
1aa56 69 74 43 61 6c 6c 62 61 63 6b 28 64 62 2d 3e 70 itCallback(db->p
1aa57 43 6f 6d 6d 69 74 41 72 67 29 3b 0a 20 20 20 20 CommitArg);.
1aa58 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 (void)sqlite3Saf
1aa59 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20 69 etyOn(db);. i
1aa5a 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 f( rc ){. r
1aa5b 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 4e eturn SQLITE_CON
1aa5c 53 54 52 41 49 4e 54 3b 0a 20 20 20 20 7d 0a 20 STRAINT;. }.
1aa5d 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 73 69 6d }.. /* The sim
1aa5e 70 6c 65 20 63 61 73 65 20 2d 20 6e 6f 20 6d 6f ple case - no mo
1aa5f 72 65 20 74 68 61 6e 20 6f 6e 65 20 64 61 74 61 re than one data
1aa60 62 61 73 65 20 66 69 6c 65 20 28 6e 6f 74 20 63 base file (not c
1aa61 6f 75 6e 74 69 6e 67 20 74 68 65 0a 20 20 2a 2a ounting the. **
1aa62 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 29 20 TEMP database)
1aa63 68 61 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f has a transactio
1aa64 6e 20 61 63 74 69 76 65 2e 20 20 20 54 68 65 72 n active. Ther
1aa65 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 66 6f 72 e is no need for
1aa66 20 74 68 65 0a 20 20 2a 2a 20 6d 61 73 74 65 72 the. ** master
1aa67 2d 6a 6f 75 72 6e 61 6c 2e 0a 20 20 2a 2a 0a 20 -journal.. **.
1aa68 20 2a 2a 20 49 66 20 74 68 65 20 72 65 74 75 72 ** If the retur
1aa69 6e 20 76 61 6c 75 65 20 6f 66 20 73 71 6c 69 74 n value of sqlit
1aa6a 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 e3BtreeGetFilena
1aa6b 6d 65 28 29 20 69 73 20 61 20 7a 65 72 6f 20 6c me() is a zero l
1aa6c 65 6e 67 74 68 0a 20 20 2a 2a 20 73 74 72 69 6e ength. ** strin
1aa6d 67 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 65 20 g, it means the
1aa6e 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 69 73 main database is
1aa6f 20 3a 6d 65 6d 6f 72 79 3a 20 6f 72 20 61 20 74 :memory: or a t
1aa70 65 6d 70 20 66 69 6c 65 2e 20 20 49 6e 20 0a 20 emp file. In .
1aa71 20 2a 2a 20 74 68 61 74 20 63 61 73 65 20 77 65 ** that case we
1aa72 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 do not support
1aa73 61 74 6f 6d 69 63 20 6d 75 6c 74 69 2d 66 69 6c atomic multi-fil
1aa74 65 20 63 6f 6d 6d 69 74 73 2c 20 73 6f 20 75 73 e commits, so us
1aa75 65 20 74 68 65 20 0a 20 20 2a 2a 20 73 69 6d 70 e the . ** simp
1aa76 6c 65 20 63 61 73 65 20 74 68 65 6e 20 74 6f 6f le case then too
1aa77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 30 3d 3d .. */. if( 0==
1aa78 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
1aa79 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 46 sqlite3BtreeGetF
1aa7a 69 6c 65 6e 61 6d 65 28 64 62 2d 3e 61 44 62 5b ilename(db->aDb[
1aa7b 30 5d 2e 70 42 74 29 29 0a 20 20 20 7c 7c 20 6e 0].pBt)). || n
1aa7c 54 72 61 6e 73 3c 3d 31 0a 20 20 29 7b 0a 20 20 Trans<=1. ){.
1aa7d 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 for(i=0; rc==S
1aa7e 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62 QLITE_OK && i<db
1aa7f 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 ->nDb; i++){.
1aa80 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 Btree *pBt =
1aa81 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a db->aDb[i].pBt;.
1aa82 20 20 20 20 20 20 69 66 28 20 70 42 74 20 29 7b if( pBt ){
1aa83 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
1aa84 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 lite3BtreeCommit
1aa85 50 68 61 73 65 4f 6e 65 28 70 42 74 2c 20 30 29 PhaseOne(pBt, 0)
1aa86 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1aa87 0a 20 20 20 20 2f 2a 20 44 6f 20 74 68 65 20 63 . /* Do the c
1aa88 6f 6d 6d 69 74 20 6f 6e 6c 79 20 69 66 20 61 6c ommit only if al
1aa89 6c 20 64 61 74 61 62 61 73 65 73 20 73 75 63 63 l databases succ
1aa8a 65 73 73 66 75 6c 6c 79 20 63 6f 6d 70 6c 65 74 essfully complet
1aa8b 65 20 70 68 61 73 65 20 31 2e 20 0a 20 20 20 20 e phase 1. .
1aa8c 2a 2a 20 49 66 20 6f 6e 65 20 6f 66 20 74 68 65 ** If one of the
1aa8d 20 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 BtreeCommitPhas
1aa8e 65 4f 6e 65 28 29 20 63 61 6c 6c 73 20 66 61 69 eOne() calls fai
1aa8f 6c 73 2c 20 74 68 69 73 20 69 6e 64 69 63 61 74 ls, this indicat
1aa90 65 73 20 61 6e 0a 20 20 20 20 2a 2a 20 49 4f 20 es an. ** IO
1aa91 65 72 72 6f 72 20 77 68 69 6c 65 20 64 65 6c 65 error while dele
1aa92 74 69 6e 67 20 6f 72 20 74 72 75 6e 63 61 74 69 ting or truncati
1aa93 6e 67 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ng a journal fil
1aa94 65 2e 20 49 74 20 69 73 20 75 6e 6c 69 6b 65 6c e. It is unlikel
1aa95 79 2c 0a 20 20 20 20 2a 2a 20 62 75 74 20 63 6f y,. ** but co
1aa96 75 6c 64 20 68 61 70 70 65 6e 2e 20 49 6e 20 74 uld happen. In t
1aa97 68 69 73 20 63 61 73 65 20 61 62 61 6e 64 6f 6e his case abandon
1aa98 20 70 72 6f 63 65 73 73 69 6e 67 20 61 6e 64 20 processing and
1aa99 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 return the error
1aa9a 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 .. */. for
1aa9b 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 (i=0; rc==SQLITE
1aa9c 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 _OK && i<db->nDb
1aa9d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 42 74 ; i++){. Bt
1aa9e 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 ree *pBt = db->a
1aa9f 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 Db[i].pBt;.
1aaa0 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20 if( pBt ){.
1aaa1 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1aaa2 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 BtreeCommitPhase
1aaa3 54 77 6f 28 70 42 74 29 3b 0a 20 20 20 20 20 20 Two(pBt);.
1aaa4 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 }. }. if(
1aaa5 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
1aaa6 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 74 . sqlite3Vt
1aaa7 61 62 43 6f 6d 6d 69 74 28 64 62 29 3b 0a 20 20 abCommit(db);.
1aaa8 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 }. }.. /* Th
1aaa9 65 20 63 6f 6d 70 6c 65 78 20 63 61 73 65 20 2d e complex case -
1aaaa 20 54 68 65 72 65 20 69 73 20 61 20 6d 75 6c 74 There is a mult
1aaab 69 2d 66 69 6c 65 20 77 72 69 74 65 2d 74 72 61 i-file write-tra
1aaac 6e 73 61 63 74 69 6f 6e 20 61 63 74 69 76 65 2e nsaction active.
1aaad 0a 20 20 2a 2a 20 54 68 69 73 20 72 65 71 75 69 . ** This requi
1aaae 72 65 73 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 res a master jou
1aaaf 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 65 6e 73 rnal file to ens
1aab0 75 72 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 ure the transact
1aab1 69 6f 6e 20 69 73 0a 20 20 2a 2a 20 63 6f 6d 6d ion is. ** comm
1aab2 69 74 74 65 64 20 61 74 6f 6d 69 63 6c 79 2e 0a itted atomicly..
1aab3 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c */.#ifndef SQL
1aab4 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a ITE_OMIT_DISKIO.
1aab5 20 20 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 else{. sqli
1aab6 74 65 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 te3_vfs *pVfs =
1aab7 64 62 2d 3e 70 56 66 73 3b 0a 20 20 20 20 69 6e db->pVfs;. in
1aab8 74 20 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a t needSync = 0;.
1aab9 20 20 20 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 char *zMaste
1aaba 72 20 3d 20 30 3b 20 20 20 2f 2a 20 46 69 6c 65 r = 0; /* File
1aabb 2d 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 6d 61 -name for the ma
1aabc 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a ster journal */.
1aabd 20 20 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a char const *
1aabe 7a 4d 61 69 6e 46 69 6c 65 20 3d 20 73 71 6c 69 zMainFile = sqli
1aabf 74 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e te3BtreeGetFilen
1aac0 61 6d 65 28 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 ame(db->aDb[0].p
1aac1 42 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 Bt);. sqlite3
1aac2 5f 66 69 6c 65 20 2a 70 4d 61 73 74 65 72 20 3d _file *pMaster =
1aac3 20 30 3b 0a 20 20 20 20 69 36 34 20 6f 66 66 73 0;. i64 offs
1aac4 65 74 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 et = 0;. int
1aac5 72 65 73 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 6c res;.. /* Sel
1aac6 65 63 74 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 ect a master jou
1aac7 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 2a rnal file name *
1aac8 2f 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 /. do {.
1aac9 20 75 33 32 20 69 52 61 6e 64 6f 6d 3b 0a 20 20 u32 iRandom;.
1aaca 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1aacb 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a e(db, zMaster);.
1aacc 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 61 sqlite3_ra
1aacd 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 ndomness(sizeof(
1aace 69 52 61 6e 64 6f 6d 29 2c 20 26 69 52 61 6e 64 iRandom), &iRand
1aacf 6f 6d 29 3b 0a 20 20 20 20 20 20 7a 4d 61 73 74 om);. zMast
1aad0 65 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 er = sqlite3MPri
1aad1 6e 74 66 28 64 62 2c 20 22 25 73 2d 6d 6a 25 30 ntf(db, "%s-mj%0
1aad2 38 58 22 2c 20 7a 4d 61 69 6e 46 69 6c 65 2c 20 8X", zMainFile,
1aad3 69 52 61 6e 64 6f 6d 26 30 78 37 66 66 66 66 66 iRandom&0x7fffff
1aad4 66 66 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 ff);. if( !
1aad5 7a 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 zMaster ){.
1aad6 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1aad7 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a _NOMEM;. }.
1aad8 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1aad9 65 33 4f 73 41 63 63 65 73 73 28 70 56 66 73 2c e3OsAccess(pVfs,
1aada 20 7a 4d 61 73 74 65 72 2c 20 53 51 4c 49 54 45 zMaster, SQLITE
1aadb 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20 _ACCESS_EXISTS,
1aadc 26 72 65 73 29 3b 0a 20 20 20 20 7d 77 68 69 6c &res);. }whil
1aadd 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b e( rc==SQLITE_OK
1aade 20 26 26 20 72 65 73 20 29 3b 0a 20 20 20 20 69 && res );. i
1aadf 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1aae0 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4f 70 65 ){. /* Ope
1aae1 6e 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 n the master jou
1aae2 72 6e 61 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 72 rnal. */. r
1aae3 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 c = sqlite3OsOpe
1aae4 6e 4d 61 6c 6c 6f 63 28 70 56 66 73 2c 20 7a 4d nMalloc(pVfs, zM
1aae5 61 73 74 65 72 2c 20 26 70 4d 61 73 74 65 72 2c aster, &pMaster,
1aae6 20 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 . SQLI
1aae7 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 TE_OPEN_READWRIT
1aae8 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 E|SQLITE_OPEN_CR
1aae9 45 41 54 45 7c 0a 20 20 20 20 20 20 20 20 20 20 EATE|.
1aaea 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c SQLITE_OPEN_EXCL
1aaeb 55 53 49 56 45 7c 53 51 4c 49 54 45 5f 4f 50 45 USIVE|SQLITE_OPE
1aaec 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c N_MASTER_JOURNAL
1aaed 2c 20 30 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 , 0. );.
1aaee 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 }. if( rc!=S
1aaef 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1aaf0 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1aaf1 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 db, zMaster);.
1aaf2 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
1aaf3 20 20 20 7d 0a 20 0a 20 20 20 20 2f 2a 20 57 72 }. . /* Wr
1aaf4 69 74 65 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 ite the name of
1aaf5 65 61 63 68 20 64 61 74 61 62 61 73 65 20 66 69 each database fi
1aaf6 6c 65 20 69 6e 20 74 68 65 20 74 72 61 6e 73 61 le in the transa
1aaf7 63 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 6e ction into the n
1aaf8 65 77 0a 20 20 20 20 2a 2a 20 6d 61 73 74 65 72 ew. ** master
1aaf9 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 journal file. I
1aafa 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
1aafb 73 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 20 s at this point
1aafc 63 6c 6f 73 65 0a 20 20 20 20 2a 2a 20 61 6e 64 close. ** and
1aafd 20 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74 delete the mast
1aafe 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e er journal file.
1aaff 20 41 6c 6c 20 74 68 65 20 69 6e 64 69 76 69 64 All the individ
1ab00 75 61 6c 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ual journal file
1ab01 73 0a 20 20 20 20 2a 2a 20 73 74 69 6c 6c 20 68 s. ** still h
1ab02 61 76 65 20 27 6e 75 6c 6c 27 20 61 73 20 74 68 ave 'null' as th
1ab03 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
1ab04 20 70 6f 69 6e 74 65 72 2c 20 73 6f 20 74 68 65 pointer, so the
1ab05 79 20 77 69 6c 6c 20 72 6f 6c 6c 0a 20 20 20 20 y will roll.
1ab06 2a 2a 20 62 61 63 6b 20 69 6e 64 65 70 65 6e 64 ** back independ
1ab07 65 6e 74 6c 79 20 69 66 20 61 20 66 61 69 6c 75 ently if a failu
1ab08 72 65 20 6f 63 63 75 72 73 2e 0a 20 20 20 20 2a re occurs.. *
1ab09 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 /. for(i=0; i
1ab0a 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a <db->nDb; i++){.
1ab0b 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 Btree *pBt
1ab0c 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 = db->aDb[i].pB
1ab0d 74 3b 0a 20 20 20 20 20 20 69 66 28 20 69 3d 3d t;. if( i==
1ab0e 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b 20 20 20 1 ) continue;
1ab0f 2f 2a 20 49 67 6e 6f 72 65 20 74 68 65 20 54 45 /* Ignore the TE
1ab10 4d 50 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 MP database */.
1ab11 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
1ab12 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70 BtreeIsInTrans(p
1ab13 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 63 Bt) ){. c
1ab14 68 61 72 20 63 6f 6e 73 74 20 2a 7a 46 69 6c 65 har const *zFile
1ab15 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 = sqlite3BtreeG
1ab16 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 70 42 etJournalname(pB
1ab17 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 t);. if(
1ab18 7a 46 69 6c 65 5b 30 5d 3d 3d 30 20 29 20 63 6f zFile[0]==0 ) co
1ab19 6e 74 69 6e 75 65 3b 20 20 2f 2a 20 49 67 6e 6f ntinue; /* Igno
1ab1a 72 65 20 3a 6d 65 6d 6f 72 79 3a 20 64 61 74 61 re :memory: data
1ab1b 62 61 73 65 73 20 2a 2f 0a 20 20 20 20 20 20 20 bases */.
1ab1c 20 69 66 28 20 21 6e 65 65 64 53 79 6e 63 20 26 if( !needSync &
1ab1d 26 20 21 73 71 6c 69 74 65 33 42 74 72 65 65 53 & !sqlite3BtreeS
1ab1e 79 6e 63 44 69 73 61 62 6c 65 64 28 70 42 74 29 yncDisabled(pBt)
1ab1f 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 65 ){. ne
1ab20 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 edSync = 1;.
1ab21 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 63 }. rc
1ab22 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 = sqlite3OsWrit
1ab23 65 28 70 4d 61 73 74 65 72 2c 20 7a 46 69 6c 65 e(pMaster, zFile
1ab24 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 , sqlite3Strlen3
1ab25 30 28 7a 46 69 6c 65 29 2b 31 2c 20 6f 66 66 73 0(zFile)+1, offs
1ab26 65 74 29 3b 0a 20 20 20 20 20 20 20 20 6f 66 66 et);. off
1ab27 73 65 74 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 set += sqlite3St
1ab28 72 6c 65 6e 33 30 28 7a 46 69 6c 65 29 2b 31 3b rlen30(zFile)+1;
1ab29 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 . if( rc!
1ab2a 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1ab2b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f sqlite3O
1ab2c 73 43 6c 6f 73 65 46 72 65 65 28 70 4d 61 73 74 sCloseFree(pMast
1ab2d 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 er);. s
1ab2e 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 qlite3OsDelete(p
1ab2f 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29 Vfs, zMaster, 0)
1ab30 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 ;. sqli
1ab31 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4d te3DbFree(db, zM
1ab32 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 20 20 aster);.
1ab33 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
1ab34 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
1ab35 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 79 6e }.. /* Syn
1ab36 63 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 c the master jou
1ab37 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 74 68 rnal file. If th
1ab38 65 20 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 e IOCAP_SEQUENTI
1ab39 41 4c 20 64 65 76 69 63 65 0a 20 20 20 20 2a 2a AL device. **
1ab3a 20 66 6c 61 67 20 69 73 20 73 65 74 20 74 68 69 flag is set thi
1ab3b 73 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 s is not require
1ab3c 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 d.. */. if
1ab3d 28 20 6e 65 65 64 53 79 6e 63 20 0a 20 20 20 20 ( needSync .
1ab3e 20 26 26 20 30 3d 3d 28 73 71 6c 69 74 65 33 4f && 0==(sqlite3O
1ab3f 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 sDeviceCharacter
1ab40 69 73 74 69 63 73 28 70 4d 61 73 74 65 72 29 26 istics(pMaster)&
1ab41 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 SQLITE_IOCAP_SEQ
1ab42 55 45 4e 54 49 41 4c 29 0a 20 20 20 20 20 26 26 UENTIAL). &&
1ab43 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 SQLITE_OK!=(rc
1ab44 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 = sqlite3OsSync(
1ab45 70 4d 61 73 74 65 72 2c 20 53 51 4c 49 54 45 5f pMaster, SQLITE_
1ab46 53 59 4e 43 5f 4e 4f 52 4d 41 4c 29 29 0a 20 20 SYNC_NORMAL)).
1ab47 20 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
1ab48 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 70 4d e3OsCloseFree(pM
1ab49 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 73 71 aster);. sq
1ab4a 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 lite3OsDelete(pV
1ab4b 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b fs, zMaster, 0);
1ab4c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 . sqlite3Db
1ab4d 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 Free(db, zMaster
1ab4e 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
1ab4f 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f rc;. }.. /
1ab50 2a 20 53 79 6e 63 20 61 6c 6c 20 74 68 65 20 64 * Sync all the d
1ab51 62 20 66 69 6c 65 73 20 69 6e 76 6f 6c 76 65 64 b files involved
1ab52 20 69 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74 in the transact
1ab53 69 6f 6e 2e 20 54 68 65 20 73 61 6d 65 20 63 61 ion. The same ca
1ab54 6c 6c 0a 20 20 20 20 2a 2a 20 73 65 74 73 20 74 ll. ** sets t
1ab55 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 he master journa
1ab56 6c 20 70 6f 69 6e 74 65 72 20 69 6e 20 65 61 63 l pointer in eac
1ab57 68 20 69 6e 64 69 76 69 64 75 61 6c 20 6a 6f 75 h individual jou
1ab58 72 6e 61 6c 2e 20 49 66 0a 20 20 20 20 2a 2a 20 rnal. If. **
1ab59 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 an error occurs
1ab5a 68 65 72 65 2c 20 64 6f 20 6e 6f 74 20 64 65 6c here, do not del
1ab5b 65 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a ete the master j
1ab5c 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20 ournal file..
1ab5d 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 **. ** If th
1ab5e 65 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 e error occurs d
1ab5f 75 72 69 6e 67 20 74 68 65 20 66 69 72 73 74 20 uring the first
1ab60 63 61 6c 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 73 call to. ** s
1ab61 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 qlite3BtreeCommi
1ab62 74 50 68 61 73 65 4f 6e 65 28 29 2c 20 74 68 65 tPhaseOne(), the
1ab63 6e 20 74 68 65 72 65 20 69 73 20 61 20 63 68 61 n there is a cha
1ab64 6e 63 65 20 74 68 61 74 20 74 68 65 0a 20 20 20 nce that the.
1ab65 20 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e ** master journ
1ab66 61 6c 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 al file will be
1ab67 6f 72 70 68 61 6e 65 64 2e 20 42 75 74 20 77 65 orphaned. But we
1ab68 20 63 61 6e 6e 6f 74 20 64 65 6c 65 74 65 20 69 cannot delete i
1ab69 74 2c 0a 20 20 20 20 2a 2a 20 69 6e 20 63 61 73 t,. ** in cas
1ab6a 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 e the master jou
1ab6b 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 77 rnal file name w
1ab6c 61 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 as written into
1ab6d 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 the journal.
1ab6e 2a 2a 20 66 69 6c 65 20 62 65 66 6f 72 65 20 74 ** file before t
1ab6f 68 65 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 he failure occur
1ab70 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 red.. */.
1ab71 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c for(i=0; rc==SQL
1ab72 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e ITE_OK && i<db->
1ab73 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 20 20 nDb; i++){ .
1ab74 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 Btree *pBt = d
1ab75 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 b->aDb[i].pBt;.
1ab76 20 20 20 20 20 69 66 28 20 70 42 74 20 29 7b 0a if( pBt ){.
1ab77 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
1ab78 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 ite3BtreeCommitP
1ab79 68 61 73 65 4f 6e 65 28 70 42 74 2c 20 7a 4d 61 haseOne(pBt, zMa
1ab7a 73 74 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 ster);. }.
1ab7b 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
1ab7c 4f 73 43 6c 6f 73 65 46 72 65 65 28 70 4d 61 73 OsCloseFree(pMas
1ab7d 74 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 ter);. if( rc
1ab7e 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1ab7f 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
1ab80 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b ee(db, zMaster);
1ab81 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
1ab82 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
1ab83 44 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65 Delete the maste
1ab84 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 r journal file.
1ab85 54 68 69 73 20 63 6f 6d 6d 69 74 73 20 74 68 65 This commits the
1ab86 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 41 66 transaction. Af
1ab87 74 65 72 0a 20 20 20 20 2a 2a 20 64 6f 69 6e 67 ter. ** doing
1ab88 20 74 68 69 73 20 74 68 65 20 64 69 72 65 63 74 this the direct
1ab89 6f 72 79 20 69 73 20 73 79 6e 63 65 64 20 61 67 ory is synced ag
1ab8a 61 69 6e 20 62 65 66 6f 72 65 20 61 6e 79 20 69 ain before any i
1ab8b 6e 64 69 76 69 64 75 61 6c 0a 20 20 20 20 2a 2a ndividual. **
1ab8c 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 66 69 6c transaction fil
1ab8d 65 73 20 61 72 65 20 64 65 6c 65 74 65 64 2e 0a es are deleted..
1ab8e 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 */. rc =
1ab8f 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 sqlite3OsDelete(
1ab90 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 31 pVfs, zMaster, 1
1ab91 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 );. sqlite3Db
1ab92 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 Free(db, zMaster
1ab93 29 3b 0a 20 20 20 20 7a 4d 61 73 74 65 72 20 3d );. zMaster =
1ab94 20 30 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 0;. if( rc )
1ab95 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 {. return r
1ab96 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a c;. }.. /*
1ab97 20 41 6c 6c 20 66 69 6c 65 73 20 61 6e 64 20 64 All files and d
1ab98 69 72 65 63 74 6f 72 69 65 73 20 68 61 76 65 20 irectories have
1ab99 61 6c 72 65 61 64 79 20 62 65 65 6e 20 73 79 6e already been syn
1ab9a 63 65 64 2c 20 73 6f 20 74 68 65 20 66 6f 6c 6c ced, so the foll
1ab9b 6f 77 69 6e 67 0a 20 20 20 20 2a 2a 20 63 61 6c owing. ** cal
1ab9c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 ls to sqlite3Btr
1ab9d 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f eeCommitPhaseTwo
1ab9e 28 29 20 61 72 65 20 6f 6e 6c 79 20 63 6c 6f 73 () are only clos
1ab9f 69 6e 67 20 66 69 6c 65 73 20 61 6e 64 0a 20 20 ing files and.
1aba0 20 20 2a 2a 20 64 65 6c 65 74 69 6e 67 20 6f 72 ** deleting or
1aba1 20 74 72 75 6e 63 61 74 69 6e 67 20 6a 6f 75 72 truncating jour
1aba2 6e 61 6c 73 2e 20 49 66 20 73 6f 6d 65 74 68 69 nals. If somethi
1aba3 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 20 77 68 ng goes wrong wh
1aba4 69 6c 65 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 ile. ** this
1aba5 69 73 20 68 61 70 70 65 6e 69 6e 67 20 77 65 20 is happening we
1aba6 64 6f 6e 27 74 20 72 65 61 6c 6c 79 20 63 61 72 don't really car
1aba7 65 2e 20 54 68 65 20 69 6e 74 65 67 72 69 74 79 e. The integrity
1aba8 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 of the. ** t
1aba9 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 6c ransaction is al
1abaa 72 65 61 64 79 20 67 75 61 72 61 6e 74 65 65 64 ready guaranteed
1abab 2c 20 62 75 74 20 73 6f 6d 65 20 73 74 72 61 79 , but some stray
1abac 20 27 63 6f 6c 64 27 20 6a 6f 75 72 6e 61 6c 73 'cold' journals
1abad 0a 20 20 20 20 2a 2a 20 6d 61 79 20 62 65 20 6c . ** may be l
1abae 79 69 6e 67 20 61 72 6f 75 6e 64 2e 20 52 65 74 ying around. Ret
1abaf 75 72 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 urning an error
1abb0 63 6f 64 65 20 77 6f 6e 27 74 20 68 65 6c 70 20 code won't help
1abb1 6d 61 74 74 65 72 73 2e 0a 20 20 20 20 2a 2f 0a matters.. */.
1abb2 20 20 20 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 disable_simu
1abb3 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 lated_io_errors(
1abb4 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65 );. sqlite3Be
1abb5 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 ginBenignMalloc(
1abb6 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 );. for(i=0;
1abb7 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b i<db->nDb; i++){
1abb8 20 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 . Btree *p
1abb9 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e Bt = db->aDb[i].
1abba 70 42 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 pBt;. if( p
1abbb 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 Bt ){. sq
1abbc 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 lite3BtreeCommit
1abbd 50 68 61 73 65 54 77 6f 28 70 42 74 29 3b 0a 20 PhaseTwo(pBt);.
1abbe 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
1abbf 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 sqlite3EndBenig
1abc0 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 65 nMalloc();. e
1abc1 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f nable_simulated_
1abc2 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a 0a 20 20 io_errors();..
1abc3 20 20 73 71 6c 69 74 65 33 56 74 61 62 43 6f 6d sqlite3VtabCom
1abc4 6d 69 74 28 64 62 29 3b 0a 20 20 7d 0a 23 65 6e mit(db);. }.#en
1abc5 64 69 66 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 dif.. return rc
1abc6 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 ;.}../* .** This
1abc7 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 routine checks
1abc8 74 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 33 that the sqlite3
1abc9 2e 61 63 74 69 76 65 56 64 62 65 43 6e 74 20 63 .activeVdbeCnt c
1abca 6f 75 6e 74 20 76 61 72 69 61 62 6c 65 0a 2a 2a ount variable.**
1abcb 20 6d 61 74 63 68 65 73 20 74 68 65 20 6e 75 6d matches the num
1abcc 62 65 72 20 6f 66 20 76 64 62 65 27 73 20 69 6e ber of vdbe's in
1abcd 20 74 68 65 20 6c 69 73 74 20 73 71 6c 69 74 65 the list sqlite
1abce 33 2e 70 56 64 62 65 20 74 68 61 74 20 61 72 65 3.pVdbe that are
1abcf 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 61 63 .** currently ac
1abd0 74 69 76 65 2e 20 41 6e 20 61 73 73 65 72 74 69 tive. An asserti
1abd1 6f 6e 20 66 61 69 6c 73 20 69 66 20 74 68 65 20 on fails if the
1abd2 74 77 6f 20 63 6f 75 6e 74 73 20 64 6f 20 6e 6f two counts do no
1abd3 74 20 6d 61 74 63 68 2e 0a 2a 2a 20 54 68 69 73 t match..** This
1abd4 20 69 73 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 is an internal
1abd5 73 65 6c 66 2d 63 68 65 63 6b 20 6f 6e 6c 79 20 self-check only
1abd6 2d 20 69 74 20 69 73 20 6e 6f 74 20 61 6e 20 65 - it is not an e
1abd7 73 73 65 6e 74 69 61 6c 20 70 72 6f 63 65 73 73 ssential process
1abd8 69 6e 67 0a 2a 2a 20 73 74 65 70 2e 0a 2a 2a 0a ing.** step..**.
1abd9 2a 2a 20 54 68 69 73 20 69 73 20 61 20 6e 6f 2d ** This is a no-
1abda 6f 70 20 69 66 20 4e 44 45 42 55 47 20 69 73 20 op if NDEBUG is
1abdb 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66 6e defined..*/.#ifn
1abdc 64 65 66 20 4e 44 45 42 55 47 0a 73 74 61 74 69 def NDEBUG.stati
1abdd 63 20 76 6f 69 64 20 63 68 65 63 6b 41 63 74 69 c void checkActi
1abde 76 65 56 64 62 65 43 6e 74 28 73 71 6c 69 74 65 veVdbeCnt(sqlite
1abdf 33 20 2a 64 62 29 7b 0a 20 20 56 64 62 65 20 2a 3 *db){. Vdbe *
1abe0 70 3b 0a 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 p;. int cnt = 0
1abe1 3b 0a 20 20 69 6e 74 20 6e 57 72 69 74 65 20 3d ;. int nWrite =
1abe2 20 30 3b 0a 20 20 70 20 3d 20 64 62 2d 3e 70 56 0;. p = db->pV
1abe3 64 62 65 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 dbe;. while( p
1abe4 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6d 61 ){. if( p->ma
1abe5 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f gic==VDBE_MAGIC_
1abe6 52 55 4e 20 26 26 20 70 2d 3e 70 63 3e 3d 30 20 RUN && p->pc>=0
1abe7 29 7b 0a 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a ){. cnt++;.
1abe8 20 20 20 20 20 20 69 66 28 20 70 2d 3e 72 65 61 if( p->rea
1abe9 64 4f 6e 6c 79 3d 3d 30 20 29 20 6e 57 72 69 74 dOnly==0 ) nWrit
1abea 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 e++;. }. p
1abeb 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d = p->pNext;. }
1abec 0a 20 20 61 73 73 65 72 74 28 20 63 6e 74 3d 3d . assert( cnt==
1abed 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e db->activeVdbeCn
1abee 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e t );. assert( n
1abef 57 72 69 74 65 3d 3d 64 62 2d 3e 77 72 69 74 65 Write==db->write
1abf0 56 64 62 65 43 6e 74 20 29 3b 0a 7d 0a 23 65 6c VdbeCnt );.}.#el
1abf1 73 65 0a 23 64 65 66 69 6e 65 20 63 68 65 63 6b se.#define check
1abf2 41 63 74 69 76 65 56 64 62 65 43 6e 74 28 78 29 ActiveVdbeCnt(x)
1abf3 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 .#endif../*.** F
1abf4 6f 72 20 65 76 65 72 79 20 42 74 72 65 65 20 74 or every Btree t
1abf5 68 61 74 20 69 6e 20 64 61 74 61 62 61 73 65 20 hat in database
1abf6 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 20 77 68 connection db wh
1abf7 69 63 68 20 0a 2a 2a 20 68 61 73 20 62 65 65 6e ich .** has been
1abf8 20 6d 6f 64 69 66 69 65 64 2c 20 22 74 72 69 70 modified, "trip
1abf9 22 20 6f 72 20 69 6e 76 61 6c 69 64 61 74 65 20 " or invalidate
1abfa 65 61 63 68 20 63 75 72 73 6f 72 20 69 6e 0a 2a each cursor in.*
1abfb 2a 20 74 68 61 74 20 42 74 72 65 65 20 6d 69 67 * that Btree mig
1abfc 68 74 20 68 61 76 65 20 62 65 65 6e 20 6d 6f 64 ht have been mod
1abfd 69 66 69 65 64 20 73 6f 20 74 68 61 74 20 74 68 ified so that th
1abfe 65 20 63 75 72 73 6f 72 0a 2a 2a 20 63 61 6e 20 e cursor.** can
1abff 6e 65 76 65 72 20 62 65 20 75 73 65 64 20 61 67 never be used ag
1ac00 61 69 6e 2e 20 20 54 68 69 73 20 68 61 70 70 65 ain. This happe
1ac01 6e 73 20 77 68 65 6e 20 61 20 72 6f 6c 6c 62 61 ns when a rollba
1ac02 63 6b 0a 2a 2a 2a 20 6f 63 63 75 72 73 2e 20 20 ck.*** occurs.
1ac03 57 65 20 68 61 76 65 20 74 6f 20 74 72 69 70 20 We have to trip
1ac04 61 6c 6c 20 74 68 65 20 6f 74 68 65 72 20 63 75 all the other cu
1ac05 72 73 6f 72 73 2c 20 65 76 65 6e 0a 2a 2a 20 63 rsors, even.** c
1ac06 75 72 73 6f 72 20 66 72 6f 6d 20 6f 74 68 65 72 ursor from other
1ac07 20 56 4d 73 20 69 6e 20 64 69 66 66 65 72 65 6e VMs in differen
1ac08 74 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 t database conne
1ac09 63 74 69 6f 6e 73 2c 0a 2a 2a 20 73 6f 20 74 68 ctions,.** so th
1ac0a 61 74 20 6e 6f 6e 65 20 6f 66 20 74 68 65 6d 20 at none of them
1ac0b 74 72 79 20 74 6f 20 75 73 65 20 74 68 65 20 64 try to use the d
1ac0c 61 74 61 20 61 74 20 77 68 69 63 68 20 74 68 65 ata at which the
1ac0d 79 0a 2a 2a 20 77 65 72 65 20 70 6f 69 6e 74 69 y.** were pointi
1ac0e 6e 67 20 61 6e 64 20 77 68 69 63 68 20 6e 6f 77 ng and which now
1ac0f 20 6d 61 79 20 68 61 76 65 20 62 65 65 6e 20 63 may have been c
1ac10 68 61 6e 67 65 64 20 64 75 65 0a 2a 2a 20 74 6f hanged due.** to
1ac11 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a the rollback..*
1ac12 2a 0a 2a 2a 20 52 65 6d 65 6d 62 65 72 20 74 68 *.** Remember th
1ac13 61 74 20 61 20 72 6f 6c 6c 62 61 63 6b 20 63 61 at a rollback ca
1ac14 6e 20 64 65 6c 65 74 65 20 74 61 62 6c 65 73 20 n delete tables
1ac15 63 6f 6d 70 6c 65 74 65 20 61 6e 64 0a 2a 2a 20 complete and.**
1ac16 72 65 6f 72 64 65 72 20 72 6f 6f 74 70 61 67 65 reorder rootpage
1ac17 73 2e 20 20 53 6f 20 69 74 20 69 73 20 6e 6f 74 s. So it is not
1ac18 20 73 75 66 66 69 63 69 65 6e 74 20 6a 75 73 74 sufficient just
1ac19 20 74 6f 20 73 61 76 65 0a 2a 2a 20 74 68 65 20 to save.** the
1ac1a 73 74 61 74 65 20 6f 66 20 74 68 65 20 63 75 72 state of the cur
1ac1b 73 6f 72 2e 20 20 57 65 20 68 61 76 65 20 74 6f sor. We have to
1ac1c 20 69 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20 invalidate the
1ac1d 63 75 72 73 6f 72 0a 2a 2a 20 73 6f 20 74 68 61 cursor.** so tha
1ac1e 74 20 69 74 20 69 73 20 6e 65 76 65 72 20 75 73 t it is never us
1ac1f 65 64 20 61 67 61 69 6e 2e 0a 2a 2f 0a 73 74 61 ed again..*/.sta
1ac20 74 69 63 20 76 6f 69 64 20 69 6e 76 61 6c 69 64 tic void invalid
1ac21 61 74 65 43 75 72 73 6f 72 73 4f 6e 4d 6f 64 69 ateCursorsOnModi
1ac22 66 69 65 64 42 74 72 65 65 73 28 73 71 6c 69 74 fiedBtrees(sqlit
1ac23 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 e3 *db){. int i
1ac24 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 ;. for(i=0; i<d
1ac25 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 b->nDb; i++){.
1ac26 20 20 42 74 72 65 65 20 2a 70 20 3d 20 64 62 2d Btree *p = db-
1ac27 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 >aDb[i].pBt;.
1ac28 20 69 66 28 20 70 20 26 26 20 73 71 6c 69 74 65 if( p && sqlite
1ac29 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 3BtreeIsInTrans(
1ac2a 70 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 p) ){. sqli
1ac2b 74 65 33 42 74 72 65 65 54 72 69 70 41 6c 6c 43 te3BtreeTripAllC
1ac2c 75 72 73 6f 72 73 28 70 2c 20 53 51 4c 49 54 45 ursors(p, SQLITE
1ac2d 5f 41 42 4f 52 54 29 3b 0a 20 20 20 20 7d 0a 20 _ABORT);. }.
1ac2e 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 }.}../*.** If t
1ac2f 68 65 20 56 64 62 65 20 70 61 73 73 65 64 20 61 he Vdbe passed a
1ac30 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 s the first argu
1ac31 6d 65 6e 74 20 6f 70 65 6e 65 64 20 61 20 73 74 ment opened a st
1ac32 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74 atement-transact
1ac33 69 6f 6e 2c 0a 2a 2a 20 63 6c 6f 73 65 20 69 74 ion,.** close it
1ac34 20 6e 6f 77 2e 20 41 72 67 75 6d 65 6e 74 20 65 now. Argument e
1ac35 4f 70 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 Op must be eithe
1ac36 72 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c r SAVEPOINT_ROLL
1ac37 42 41 43 4b 20 6f 72 0a 2a 2a 20 53 41 56 45 50 BACK or.** SAVEP
1ac38 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2e 20 49 66 OINT_RELEASE. If
1ac39 20 69 74 20 69 73 20 53 41 56 45 50 4f 49 4e 54 it is SAVEPOINT
1ac3a 5f 52 4f 4c 4c 42 41 43 4b 2c 20 74 68 65 6e 20 _ROLLBACK, then
1ac3b 74 68 65 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a the statement.**
1ac3c 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
1ac3d 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 49 66 20 rolled back. If
1ac3e 65 4f 70 20 69 73 20 53 41 56 45 50 4f 49 4e 54 eOp is SAVEPOINT
1ac3f 5f 52 45 4c 45 41 53 45 2c 20 74 68 65 6e 20 74 _RELEASE, then t
1ac40 68 65 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 he .** statement
1ac41 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
1ac42 63 6f 6d 6d 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 commtted..**.**
1ac43 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f If an IO error o
1ac44 63 63 75 72 73 2c 20 61 6e 20 53 51 4c 49 54 45 ccurs, an SQLITE
1ac45 5f 49 4f 45 52 52 5f 58 58 58 20 65 72 72 6f 72 _IOERR_XXX error
1ac46 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 code is returne
1ac47 64 2e 20 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 d. .** Otherwise
1ac48 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 SQLITE_OK..*/.S
1ac49 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1ac4a 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 6c 6f t sqlite3VdbeClo
1ac4b 73 65 53 74 61 74 65 6d 65 6e 74 28 56 64 62 65 seStatement(Vdbe
1ac4c 20 2a 70 2c 20 69 6e 74 20 65 4f 70 29 7b 0a 20 *p, int eOp){.
1ac4d 20 73 71 6c 69 74 65 33 20 2a 63 6f 6e 73 74 20 sqlite3 *const
1ac4e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 6e db = p->db;. in
1ac4f 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
1ac50 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 2d 3e 69 53 ;.. /* If p->iS
1ac51 74 61 74 65 6d 65 6e 74 20 69 73 20 67 72 65 61 tatement is grea
1ac52 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 ter than zero, t
1ac53 68 65 6e 20 74 68 69 73 20 56 64 62 65 20 6f 70 hen this Vdbe op
1ac54 65 6e 65 64 20 61 20 0a 20 20 2a 2a 20 73 74 61 ened a . ** sta
1ac55 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 tement transacti
1ac56 6f 6e 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 on that should b
1ac57 65 20 63 6c 6f 73 65 64 20 68 65 72 65 2e 20 54 e closed here. T
1ac58 68 65 20 6f 6e 6c 79 20 65 78 63 65 70 74 69 6f he only exceptio
1ac59 6e 0a 20 20 2a 2a 20 69 73 20 74 68 61 74 20 61 n. ** is that a
1ac5a 6e 20 49 4f 20 65 72 72 6f 72 20 6d 61 79 20 68 n IO error may h
1ac5b 61 76 65 20 6f 63 63 75 72 65 64 2c 20 63 61 75 ave occured, cau
1ac5c 73 69 6e 67 20 61 6e 20 65 6d 65 72 67 65 6e 63 sing an emergenc
1ac5d 79 20 72 6f 6c 6c 62 61 63 6b 2e 0a 20 20 2a 2a y rollback.. **
1ac5e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 28 64 In this case (d
1ac5f 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3d 3d 30 b->nStatement==0
1ac60 29 2c 20 61 6e 64 20 74 68 65 72 65 20 69 73 20 ), and there is
1ac61 6e 6f 74 68 69 6e 67 20 74 6f 20 64 6f 2e 0a 20 nothing to do..
1ac62 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 6e 53 */. if( db->nS
1ac63 74 61 74 65 6d 65 6e 74 20 26 26 20 70 2d 3e 69 tatement && p->i
1ac64 53 74 61 74 65 6d 65 6e 74 20 29 7b 0a 20 20 20 Statement ){.
1ac65 20 69 6e 74 20 69 3b 0a 20 20 20 20 63 6f 6e 73 int i;. cons
1ac66 74 20 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 t int iSavepoint
1ac67 20 3d 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 = p->iStatement
1ac68 2d 31 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 -1;.. assert(
1ac69 20 65 4f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f eOp==SAVEPOINT_
1ac6a 52 4f 4c 4c 42 41 43 4b 20 7c 7c 20 65 4f 70 3d ROLLBACK || eOp=
1ac6b 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 =SAVEPOINT_RELEA
1ac6c 53 45 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 SE);. assert(
1ac6d 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3e db->nStatement>
1ac6e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 0 );. assert(
1ac6f 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 3d 3d p->iStatement==
1ac70 28 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 2b (db->nStatement+
1ac71 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 29 20 db->nSavepoint)
1ac72 29 3b 0a 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b );.. for(i=0;
1ac73 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 i<db->nDb; i++)
1ac74 7b 20 0a 20 20 20 20 20 20 69 6e 74 20 72 63 32 { . int rc2
1ac75 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
1ac76 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d Btree *pBt =
1ac77 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b db->aDb[i].pBt;
1ac78 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 20 29 . if( pBt )
1ac79 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 65 4f {. if( eO
1ac7a 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c p==SAVEPOINT_ROL
1ac7b 4c 42 41 43 4b 20 29 7b 0a 20 20 20 20 20 20 20 LBACK ){.
1ac7c 20 20 20 72 63 32 20 3d 20 73 71 6c 69 74 65 33 rc2 = sqlite3
1ac7d 42 74 72 65 65 53 61 76 65 70 6f 69 6e 74 28 70 BtreeSavepoint(p
1ac7e 42 74 2c 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f Bt, SAVEPOINT_RO
1ac7f 4c 4c 42 41 43 4b 2c 20 69 53 61 76 65 70 6f 69 LLBACK, iSavepoi
1ac80 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 nt);. }.
1ac81 20 20 20 20 20 20 20 69 66 28 20 72 63 32 3d 3d if( rc2==
1ac82 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1ac83 20 20 20 20 20 20 20 72 63 32 20 3d 20 73 71 6c rc2 = sql
1ac84 69 74 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 ite3BtreeSavepoi
1ac85 6e 74 28 70 42 74 2c 20 53 41 56 45 50 4f 49 4e nt(pBt, SAVEPOIN
1ac86 54 5f 52 45 4c 45 41 53 45 2c 20 69 53 61 76 65 T_RELEASE, iSave
1ac87 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 point);.
1ac88 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 }. if( rc
1ac89 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1ac8a 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 63 rc = rc
1ac8b 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 2;. }.
1ac8c 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 64 }. }. d
1ac8d 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 2d 2d 3b b->nStatement--;
1ac8e 0a 20 20 20 20 70 2d 3e 69 53 74 61 74 65 6d 65 . p->iStateme
1ac8f 6e 74 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 nt = 0;.. /*
1ac90 49 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 If the statement
1ac91 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
1ac92 62 65 69 6e 67 20 72 6f 6c 6c 65 64 20 62 61 63 being rolled bac
1ac93 6b 2c 20 61 6c 73 6f 20 72 65 73 74 6f 72 65 20 k, also restore
1ac94 74 68 65 20 0a 20 20 20 20 2a 2a 20 64 61 74 61 the . ** data
1ac95 62 61 73 65 20 68 61 6e 64 6c 65 73 20 64 65 66 base handles def
1ac96 65 72 72 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 erred constraint
1ac97 20 63 6f 75 6e 74 65 72 20 74 6f 20 74 68 65 20 counter to the
1ac98 76 61 6c 75 65 20 69 74 20 68 61 64 20 77 68 65 value it had whe
1ac99 6e 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 74 n . ** the st
1ac9a 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 atement transact
1ac9b 69 6f 6e 20 77 61 73 20 6f 70 65 6e 65 64 2e 20 ion was opened.
1ac9c 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 4f 70 3d */. if( eOp=
1ac9d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 =SAVEPOINT_ROLLB
1ac9e 41 43 4b 20 29 7b 0a 20 20 20 20 20 20 64 62 2d ACK ){. db-
1ac9f 3e 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 20 3d >nDeferredCons =
1aca0 20 70 2d 3e 6e 53 74 6d 74 44 65 66 43 6f 6e 73 p->nStmtDefCons
1aca1 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
1aca2 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
1aca3 2a 20 49 66 20 53 51 4c 69 74 65 20 69 73 20 63 * If SQLite is c
1aca4 6f 6d 70 69 6c 65 64 20 74 6f 20 73 75 70 70 6f ompiled to suppo
1aca5 72 74 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 rt shared-cache
1aca6 6d 6f 64 65 20 61 6e 64 20 74 6f 20 62 65 20 74 mode and to be t
1aca7 68 72 65 61 64 73 61 66 65 2c 0a 2a 2a 20 74 68 hreadsafe,.** th
1aca8 69 73 20 72 6f 75 74 69 6e 65 20 6f 62 74 61 69 is routine obtai
1aca9 6e 73 20 74 68 65 20 6d 75 74 65 78 20 61 73 73 ns the mutex ass
1acaa 6f 63 69 61 74 65 64 20 77 69 74 68 20 65 61 63 ociated with eac
1acab 68 20 42 74 53 68 61 72 65 64 20 73 74 72 75 63 h BtShared struc
1acac 74 75 72 65 0a 2a 2a 20 74 68 61 74 20 6d 61 79 ture.** that may
1acad 20 62 65 20 61 63 63 65 73 73 65 64 20 62 79 20 be accessed by
1acae 74 68 65 20 56 4d 20 70 61 73 73 65 64 20 61 73 the VM passed as
1acaf 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e 20 49 6e an argument. In
1acb0 20 64 6f 69 6e 67 20 73 6f 20 69 74 0a 2a 2a 20 doing so it.**
1acb1 73 65 74 73 20 74 68 65 20 42 74 53 68 61 72 65 sets the BtShare
1acb2 64 2e 64 62 20 6d 65 6d 62 65 72 20 6f 66 20 65 d.db member of e
1acb3 61 63 68 20 6f 66 20 74 68 65 20 42 74 53 68 61 ach of the BtSha
1acb4 72 65 64 20 73 74 72 75 63 74 75 72 65 73 2c 20 red structures,
1acb5 65 6e 73 75 72 69 6e 67 0a 2a 2a 20 74 68 61 74 ensuring.** that
1acb6 20 74 68 65 20 63 6f 72 72 65 63 74 20 62 75 73 the correct bus
1acb7 79 2d 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62 61 y-handler callba
1acb8 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 69 66 ck is invoked if
1acb9 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a required..**.**
1acba 20 49 66 20 53 51 4c 69 74 65 20 69 73 20 6e 6f If SQLite is no
1acbb 74 20 74 68 72 65 61 64 73 61 66 65 20 62 75 74 t threadsafe but
1acbc 20 64 6f 65 73 20 73 75 70 70 6f 72 74 20 73 68 does support sh
1acbd 61 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 2c ared-cache mode,
1acbe 20 74 68 65 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 then.** sqlite3
1acbf 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 29 20 BtreeEnterAll()
1acc0 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f 20 73 65 is invoked to se
1acc1 74 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 64 t the BtShared.d
1acc2 62 20 76 61 72 69 61 62 6c 65 73 0a 2a 2a 20 6f b variables.** o
1acc3 66 20 61 6c 6c 20 6f 66 20 42 74 53 68 61 72 65 f all of BtShare
1acc4 64 20 73 74 72 75 63 74 75 72 65 73 20 61 63 63 d structures acc
1acc5 65 73 73 69 62 6c 65 20 76 69 61 20 74 68 65 20 essible via the
1acc6 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 database handle
1acc7 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 .** associated w
1acc8 69 74 68 20 74 68 65 20 56 4d 2e 20 4f 66 20 63 ith the VM. Of c
1acc9 6f 75 72 73 65 20 6f 6e 6c 79 20 61 20 73 75 62 ourse only a sub
1acca 73 65 74 20 6f 66 20 74 68 65 73 65 20 73 74 72 set of these str
1accb 75 63 74 75 72 65 73 0a 2a 2a 20 77 69 6c 6c 20 uctures.** will
1accc 62 65 20 61 63 63 65 73 73 65 64 20 62 79 20 74 be accessed by t
1accd 68 65 20 56 4d 2c 20 61 6e 64 20 77 65 20 63 6f he VM, and we co
1acce 75 6c 64 20 75 73 65 20 56 64 62 65 2e 62 74 72 uld use Vdbe.btr
1accf 65 65 4d 61 73 6b 20 74 6f 20 66 69 67 75 72 65 eeMask to figure
1acd0 0a 2a 2a 20 74 68 61 74 20 73 75 62 73 65 74 20 .** that subset
1acd1 6f 75 74 2c 20 62 75 74 20 74 68 65 72 65 20 69 out, but there i
1acd2 73 20 6e 6f 20 61 64 76 61 6e 74 61 67 65 20 74 s no advantage t
1acd3 6f 20 64 6f 69 6e 67 20 73 6f 2e 0a 2a 2a 0a 2a o doing so..**.*
1acd4 2a 20 49 66 20 53 51 4c 69 74 65 20 69 73 20 6e * If SQLite is n
1acd5 6f 74 20 74 68 72 65 61 64 73 61 66 65 20 61 6e ot threadsafe an
1acd6 64 20 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f d does not suppo
1acd7 72 74 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 rt shared-cache
1acd8 6d 6f 64 65 2c 20 74 68 69 73 0a 2a 2a 20 66 75 mode, this.** fu
1acd9 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f nction is a no-o
1acda 70 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 p..*/.#ifndef SQ
1acdb 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
1acdc 5f 43 41 43 48 45 0a 53 51 4c 49 54 45 5f 50 52 _CACHE.SQLITE_PR
1acdd 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1acde 65 33 56 64 62 65 4d 75 74 65 78 41 72 72 61 79 e3VdbeMutexArray
1acdf 45 6e 74 65 72 28 56 64 62 65 20 2a 70 29 7b 0a Enter(Vdbe *p){.
1ace0 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 #if SQLITE_THREA
1ace1 44 53 41 46 45 0a 20 20 73 71 6c 69 74 65 33 42 DSAFE. sqlite3B
1ace2 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 45 6e treeMutexArrayEn
1ace3 74 65 72 28 26 70 2d 3e 61 4d 75 74 65 78 29 3b ter(&p->aMutex);
1ace4 0a 23 65 6c 73 65 0a 20 20 73 71 6c 69 74 65 33 .#else. sqlite3
1ace5 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 70 2d BtreeEnterAll(p-
1ace6 3e 64 62 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 23 >db);.#endif.}.#
1ace7 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 endif../*.** Thi
1ace8 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
1ace9 6c 6c 65 64 20 77 68 65 6e 20 61 20 74 72 61 6e lled when a tran
1acea 73 61 63 74 69 6f 6e 20 6f 70 65 6e 65 64 20 62 saction opened b
1aceb 79 20 74 68 65 20 64 61 74 61 62 61 73 65 20 0a y the database .
1acec 2a 2a 20 68 61 6e 64 6c 65 20 61 73 73 6f 63 69 ** handle associ
1aced 61 74 65 64 20 77 69 74 68 20 74 68 65 20 56 4d ated with the VM
1acee 20 70 61 73 73 65 64 20 61 73 20 61 6e 20 61 72 passed as an ar
1acef 67 75 6d 65 6e 74 20 69 73 20 61 62 6f 75 74 20 gument is about
1acf0 74 6f 20 62 65 20 0a 2a 2a 20 63 6f 6d 6d 69 74 to be .** commit
1acf1 74 65 64 2e 20 49 66 20 74 68 65 72 65 20 61 72 ted. If there ar
1acf2 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 64 65 e outstanding de
1acf3 66 65 72 72 65 64 20 66 6f 72 65 69 67 6e 20 6b ferred foreign k
1acf4 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a ey constraint.**
1acf5 20 76 69 6f 6c 61 74 69 6f 6e 73 2c 20 72 65 74 violations, ret
1acf6 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 urn SQLITE_ERROR
1acf7 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c . Otherwise, SQL
1acf8 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 ITE_OK..**.** If
1acf9 20 74 68 65 72 65 20 61 72 65 20 6f 75 74 73 74 there are outst
1acfa 61 6e 64 69 6e 67 20 46 4b 20 76 69 6f 6c 61 74 anding FK violat
1acfb 69 6f 6e 73 20 61 6e 64 20 74 68 69 73 20 66 75 ions and this fu
1acfc 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 0a nction returns .
1acfd 2a 2a 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c ** SQLITE_ERROR,
1acfe 20 73 65 74 20 74 68 65 20 72 65 73 75 6c 74 20 set the result
1acff 6f 66 20 74 68 65 20 56 4d 20 74 6f 20 53 51 4c of the VM to SQL
1ad00 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 61 ITE_CONSTRAINT a
1ad01 6e 64 20 77 72 69 74 65 0a 2a 2a 20 61 6e 20 65 nd write.** an e
1ad02 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 6f 20 rror message to
1ad03 69 74 2e 20 54 68 65 6e 20 72 65 74 75 72 6e 20 it. Then return
1ad04 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2f SQLITE_ERROR..*/
1ad05 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1ad06 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 OMIT_FOREIGN_KEY
1ad07 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1ad08 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 int sqlite3VdbeC
1ad09 68 65 63 6b 46 6b 28 56 64 62 65 20 2a 70 2c 20 heckFk(Vdbe *p,
1ad0a 69 6e 74 20 64 65 66 65 72 72 65 64 29 7b 0a 20 int deferred){.
1ad0b 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
1ad0c 2d 3e 64 62 3b 0a 20 20 69 66 28 20 28 64 65 66 ->db;. if( (def
1ad0d 65 72 72 65 64 20 26 26 20 64 62 2d 3e 6e 44 65 erred && db->nDe
1ad0e 66 65 72 72 65 64 43 6f 6e 73 3e 30 29 20 7c 7c ferredCons>0) ||
1ad0f 20 28 21 64 65 66 65 72 72 65 64 20 26 26 20 70 (!deferred && p
1ad10 2d 3e 6e 46 6b 43 6f 6e 73 74 72 61 69 6e 74 3e ->nFkConstraint>
1ad11 30 29 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 0) ){. p->rc
1ad12 3d 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 = SQLITE_CONSTRA
1ad13 49 4e 54 3b 0a 20 20 20 20 70 2d 3e 65 72 72 6f INT;. p->erro
1ad14 72 41 63 74 69 6f 6e 20 3d 20 4f 45 5f 41 62 6f rAction = OE_Abo
1ad15 72 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 rt;. sqlite3S
1ad16 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 etString(&p->zEr
1ad17 72 4d 73 67 2c 20 64 62 2c 20 22 66 6f 72 65 69 rMsg, db, "forei
1ad18 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e gn key constrain
1ad19 74 20 66 61 69 6c 65 64 22 29 3b 0a 20 20 20 20 t failed");.
1ad1a 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 return SQLITE_ER
1ad1b 52 4f 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ROR;. }. retur
1ad1c 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 n SQLITE_OK;.}.#
1ad1d 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 endif../*.** Thi
1ad1e 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
1ad1f 6c 65 64 20 74 68 65 20 77 68 65 6e 20 61 20 56 led the when a V
1ad20 44 42 45 20 74 72 69 65 73 20 74 6f 20 68 61 6c DBE tries to hal
1ad21 74 2e 20 20 49 66 20 74 68 65 20 56 44 42 45 0a t. If the VDBE.
1ad22 2a 2a 20 68 61 73 20 6d 61 64 65 20 63 68 61 6e ** has made chan
1ad23 67 65 73 20 61 6e 64 20 69 73 20 69 6e 20 61 75 ges and is in au
1ad24 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 2c 20 74 tocommit mode, t
1ad25 68 65 6e 20 63 6f 6d 6d 69 74 20 74 68 6f 73 65 hen commit those
1ad26 0a 2a 2a 20 63 68 61 6e 67 65 73 2e 20 20 49 66 .** changes. If
1ad27 20 61 20 72 6f 6c 6c 62 61 63 6b 20 69 73 20 6e a rollback is n
1ad28 65 65 64 65 64 2c 20 74 68 65 6e 20 64 6f 20 74 eeded, then do t
1ad29 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a he rollback..**.
1ad2a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
1ad2b 69 73 20 74 68 65 20 6f 6e 6c 79 20 77 61 79 20 is the only way
1ad2c 74 6f 20 6d 6f 76 65 20 74 68 65 20 73 74 61 74 to move the stat
1ad2d 65 20 6f 66 20 61 20 56 4d 20 66 72 6f 6d 0a 2a e of a VM from.*
1ad2e 2a 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 52 * SQLITE_MAGIC_R
1ad2f 55 4e 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 UN to SQLITE_MAG
1ad30 49 43 5f 48 41 4c 54 2e 20 20 49 74 20 69 73 20 IC_HALT. It is
1ad31 68 61 72 6d 6c 65 73 73 20 74 6f 0a 2a 2a 20 63 harmless to.** c
1ad32 61 6c 6c 20 74 68 69 73 20 6f 6e 20 61 20 56 4d all this on a VM
1ad33 20 74 68 61 74 20 69 73 20 69 6e 20 74 68 65 20 that is in the
1ad34 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 48 41 4c SQLITE_MAGIC_HAL
1ad35 54 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 T state..**.** R
1ad36 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 eturn an error c
1ad37 6f 64 65 2e 20 20 49 66 20 74 68 65 20 63 6f 6d ode. If the com
1ad38 6d 69 74 20 63 6f 75 6c 64 20 6e 6f 74 20 63 6f mit could not co
1ad39 6d 70 6c 65 74 65 20 62 65 63 61 75 73 65 20 6f mplete because o
1ad3a 66 0a 2a 2a 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e f.** lock conten
1ad3b 74 69 6f 6e 2c 20 72 65 74 75 72 6e 20 53 51 4c tion, return SQL
1ad3c 49 54 45 5f 42 55 53 59 2e 20 20 49 66 20 53 51 ITE_BUSY. If SQ
1ad3d 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72 65 74 LITE_BUSY is ret
1ad3e 75 72 6e 65 64 2c 20 69 74 0a 2a 2a 20 6d 65 61 urned, it.** mea
1ad3f 6e 73 20 74 68 65 20 63 6c 6f 73 65 20 64 69 64 ns the close did
1ad40 20 6e 6f 74 20 68 61 70 70 65 6e 20 61 6e 64 20 not happen and
1ad41 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 70 65 needs to be repe
1ad42 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ated..*/.SQLITE_
1ad43 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1ad44 74 65 33 56 64 62 65 48 61 6c 74 28 56 64 62 65 te3VdbeHalt(Vdbe
1ad45 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 *p){. int rc;
1ad46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ad47 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 /* Used
1ad48 74 6f 20 73 74 6f 72 65 20 74 72 61 6e 73 69 65 to store transie
1ad49 6e 74 20 72 65 74 75 72 6e 20 63 6f 64 65 73 20 nt return codes
1ad4a 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 */. sqlite3 *db
1ad4b 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 = p->db;.. /*
1ad4c 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f This function co
1ad4d 6e 74 61 69 6e 73 20 74 68 65 20 6c 6f 67 69 63 ntains the logic
1ad4e 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 73 that determines
1ad4f 20 69 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20 if a statement
1ad50 6f 72 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 or. ** transact
1ad51 69 6f 6e 20 77 69 6c 6c 20 62 65 20 63 6f 6d 6d ion will be comm
1ad52 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c 65 64 20 itted or rolled
1ad53 62 61 63 6b 20 61 73 20 61 20 72 65 73 75 6c 74 back as a result
1ad54 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 65 78 65 of the. ** exe
1ad55 63 75 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 76 cution of this v
1ad56 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20 irtual machine.
1ad57 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 61 6e . **. ** If an
1ad58 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 y of the followi
1ad59 6e 67 20 65 72 72 6f 72 73 20 6f 63 63 75 72 3a ng errors occur:
1ad5a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 53 . **. ** S
1ad5b 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 20 20 2a 2a QLITE_NOMEM. **
1ad5c 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4f 45 52 SQLITE_IOER
1ad5d 52 0a 20 20 2a 2a 20 20 20 20 20 53 51 4c 49 54 R. ** SQLIT
1ad5e 45 5f 46 55 4c 4c 0a 20 20 2a 2a 20 20 20 20 20 E_FULL. **
1ad5f 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 SQLITE_INTERRUPT
1ad60 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 6e 20 . **. ** Then
1ad61 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 63 61 63 the internal cac
1ad62 68 65 20 6d 69 67 68 74 20 68 61 76 65 20 62 65 he might have be
1ad63 65 6e 20 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e en left in an in
1ad64 63 6f 6e 73 69 73 74 65 6e 74 0a 20 20 2a 2a 20 consistent. **
1ad65 73 74 61 74 65 2e 20 20 57 65 20 6e 65 65 64 20 state. We need
1ad66 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 to rollback the
1ad67 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 statement transa
1ad68 63 74 69 6f 6e 2c 20 69 66 20 74 68 65 72 65 20 ction, if there
1ad69 69 73 0a 20 20 2a 2a 20 6f 6e 65 2c 20 6f 72 20 is. ** one, or
1ad6a 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 72 61 the complete tra
1ad6b 6e 73 61 63 74 69 6f 6e 20 69 66 20 74 68 65 72 nsaction if ther
1ad6c 65 20 69 73 20 6e 6f 20 73 74 61 74 65 6d 65 6e e is no statemen
1ad6d 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 t transaction..
1ad6e 20 2a 2f 0a 0a 20 20 69 66 28 20 70 2d 3e 64 62 */.. if( p->db
1ad6f 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
1ad70 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 {. p->rc = SQ
1ad71 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a LITE_NOMEM;. }.
1ad72 20 20 63 6c 6f 73 65 41 6c 6c 43 75 72 73 6f 72 closeAllCursor
1ad73 73 28 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6d s(p);. if( p->m
1ad74 61 67 69 63 21 3d 56 44 42 45 5f 4d 41 47 49 43 agic!=VDBE_MAGIC
1ad75 5f 52 55 4e 20 29 7b 0a 20 20 20 20 72 65 74 75 _RUN ){. retu
1ad76 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
1ad77 7d 0a 20 20 63 68 65 63 6b 41 63 74 69 76 65 56 }. checkActiveV
1ad78 64 62 65 43 6e 74 28 64 62 29 3b 0a 0a 20 20 2f dbeCnt(db);.. /
1ad79 2a 20 4e 6f 20 63 6f 6d 6d 69 74 20 6f 72 20 72 * No commit or r
1ad7a 6f 6c 6c 62 61 63 6b 20 6e 65 65 64 65 64 20 69 ollback needed i
1ad7b 66 20 74 68 65 20 70 72 6f 67 72 61 6d 20 6e 65 f the program ne
1ad7c 76 65 72 20 73 74 61 72 74 65 64 20 2a 2f 0a 20 ver started */.
1ad7d 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30 20 29 7b if( p->pc>=0 ){
1ad7e 0a 20 20 20 20 69 6e 74 20 6d 72 63 3b 20 20 20 . int mrc;
1ad7f 2f 2a 20 50 72 69 6d 61 72 79 20 65 72 72 6f 72 /* Primary error
1ad80 20 63 6f 64 65 20 66 72 6f 6d 20 70 2d 3e 72 63 code from p->rc
1ad81 20 2a 2f 0a 20 20 20 20 69 6e 74 20 65 53 74 61 */. int eSta
1ad82 74 65 6d 65 6e 74 4f 70 20 3d 20 30 3b 0a 20 20 tementOp = 0;.
1ad83 20 20 69 6e 74 20 69 73 53 70 65 63 69 61 6c 45 int isSpecialE
1ad84 72 72 6f 72 3b 20 20 20 20 20 20 20 20 20 20 20 rror;
1ad85 20 2f 2a 20 53 65 74 20 74 6f 20 74 72 75 65 20 /* Set to true
1ad86 69 66 20 61 20 27 73 70 65 63 69 61 6c 27 20 65 if a 'special' e
1ad87 72 72 6f 72 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 rror */.. /*
1ad88 4c 6f 63 6b 20 61 6c 6c 20 62 74 72 65 65 73 20 Lock all btrees
1ad89 75 73 65 64 20 62 79 20 74 68 65 20 73 74 61 74 used by the stat
1ad8a 65 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 73 71 6c ement */. sql
1ad8b 69 74 65 33 56 64 62 65 4d 75 74 65 78 41 72 72 ite3VdbeMutexArr
1ad8c 61 79 45 6e 74 65 72 28 70 29 3b 0a 0a 20 20 20 ayEnter(p);..
1ad8d 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 6f 6e /* Check for on
1ad8e 65 20 6f 66 20 74 68 65 20 73 70 65 63 69 61 6c e of the special
1ad8f 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 20 20 6d errors */. m
1ad90 72 63 20 3d 20 70 2d 3e 72 63 20 26 20 30 78 66 rc = p->rc & 0xf
1ad91 66 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 f;. assert( p
1ad92 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 49 4f 45 ->rc!=SQLITE_IOE
1ad93 52 52 5f 42 4c 4f 43 4b 45 44 20 29 3b 20 20 2f RR_BLOCKED ); /
1ad94 2a 20 54 68 69 73 20 65 72 72 6f 72 20 6e 6f 20 * This error no
1ad95 6c 6f 6e 67 65 72 20 65 78 69 73 74 73 20 2a 2f longer exists */
1ad96 0a 20 20 20 20 69 73 53 70 65 63 69 61 6c 45 72 . isSpecialEr
1ad97 72 6f 72 20 3d 20 6d 72 63 3d 3d 53 51 4c 49 54 ror = mrc==SQLIT
1ad98 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 6d 72 63 3d 3d E_NOMEM || mrc==
1ad99 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a 20 20 20 SQLITE_IOERR.
1ad9a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ad9b 20 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 45 || mrc==SQLITE
1ad9c 5f 49 4e 54 45 52 52 55 50 54 20 7c 7c 20 6d 72 _INTERRUPT || mr
1ad9d 63 3d 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a c==SQLITE_FULL;.
1ad9e 20 20 20 20 69 66 28 20 69 73 53 70 65 63 69 61 if( isSpecia
1ad9f 6c 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 20 20 lError ){.
1ada0 2f 2a 20 49 66 20 74 68 65 20 71 75 65 72 79 20 /* If the query
1ada1 77 61 73 20 72 65 61 64 2d 6f 6e 6c 79 2c 20 77 was read-only, w
1ada2 65 20 6e 65 65 64 20 64 6f 20 6e 6f 20 72 6f 6c e need do no rol
1ada3 6c 62 61 63 6b 20 61 74 20 61 6c 6c 2e 20 4f 74 lback at all. Ot
1ada4 68 65 72 77 69 73 65 2c 0a 20 20 20 20 20 20 2a herwise,. *
1ada5 2a 20 70 72 6f 63 65 65 64 20 77 69 74 68 20 74 * proceed with t
1ada6 68 65 20 73 70 65 63 69 61 6c 20 68 61 6e 64 6c he special handl
1ada7 69 6e 67 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ing.. */.
1ada8 20 20 20 20 69 66 28 20 21 70 2d 3e 72 65 61 64 if( !p->read
1ada9 4f 6e 6c 79 20 7c 7c 20 6d 72 63 21 3d 53 51 4c Only || mrc!=SQL
1adaa 49 54 45 5f 49 4e 54 45 52 52 55 50 54 20 29 7b ITE_INTERRUPT ){
1adab 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 6d 72 . if( (mr
1adac 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 c==SQLITE_NOMEM
1adad 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 46 || mrc==SQLITE_F
1adae 55 4c 4c 29 20 26 26 20 70 2d 3e 75 73 65 73 53 ULL) && p->usesS
1adaf 74 6d 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 tmtJournal ){.
1adb0 20 20 20 20 20 20 20 20 65 53 74 61 74 65 6d 65 eStateme
1adb1 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f 49 4e 54 ntOp = SAVEPOINT
1adb2 5f 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20 20 20 20 _ROLLBACK;.
1adb3 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1adb4 20 20 20 20 2f 2a 20 57 65 20 61 72 65 20 66 6f /* We are fo
1adb5 72 63 65 64 20 74 6f 20 72 6f 6c 6c 20 62 61 63 rced to roll bac
1adb6 6b 20 74 68 65 20 61 63 74 69 76 65 20 74 72 61 k the active tra
1adb7 6e 73 61 63 74 69 6f 6e 2e 20 42 65 66 6f 72 65 nsaction. Before
1adb8 20 64 6f 69 6e 67 0a 20 20 20 20 20 20 20 20 20 doing.
1adb9 20 2a 2a 20 73 6f 2c 20 61 62 6f 72 74 20 61 6e ** so, abort an
1adba 79 20 6f 74 68 65 72 20 73 74 61 74 65 6d 65 6e y other statemen
1adbb 74 73 20 74 68 69 73 20 68 61 6e 64 6c 65 20 63 ts this handle c
1adbc 75 72 72 65 6e 74 6c 79 20 68 61 73 20 61 63 74 urrently has act
1adbd 69 76 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a ive.. *
1adbe 2f 0a 20 20 20 20 20 20 20 20 20 20 69 6e 76 61 /. inva
1adbf 6c 69 64 61 74 65 43 75 72 73 6f 72 73 4f 6e 4d lidateCursorsOnM
1adc0 6f 64 69 66 69 65 64 42 74 72 65 65 73 28 64 62 odifiedBtrees(db
1adc1 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c );. sql
1adc2 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 ite3RollbackAll(
1adc3 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 db);. s
1adc4 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 76 65 70 qlite3CloseSavep
1adc5 6f 69 6e 74 73 28 64 62 29 3b 0a 20 20 20 20 20 oints(db);.
1adc6 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d db->autoCom
1adc7 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 mit = 1;.
1adc8 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d }. }. }
1adc9 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 66 .. /* Check f
1adca 6f 72 20 69 6d 6d 65 64 69 61 74 65 20 66 6f 72 or immediate for
1adcb 65 69 67 6e 20 6b 65 79 20 76 69 6f 6c 61 74 69 eign key violati
1adcc 6f 6e 73 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 ons. */. if(
1adcd 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b p->rc==SQLITE_OK
1adce 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1adcf 33 56 64 62 65 43 68 65 63 6b 46 6b 28 70 2c 20 3VdbeCheckFk(p,
1add0 30 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 0);. }. .
1add1 20 2f 2a 20 49 66 20 74 68 65 20 61 75 74 6f 2d /* If the auto-
1add2 63 6f 6d 6d 69 74 20 66 6c 61 67 20 69 73 20 73 commit flag is s
1add3 65 74 20 61 6e 64 20 74 68 69 73 20 69 73 20 74 et and this is t
1add4 68 65 20 6f 6e 6c 79 20 61 63 74 69 76 65 20 77 he only active w
1add5 72 69 74 65 72 20 0a 20 20 20 20 2a 2a 20 56 4d riter . ** VM
1add6 2c 20 74 68 65 6e 20 77 65 20 64 6f 20 65 69 74 , then we do eit
1add7 68 65 72 20 61 20 63 6f 6d 6d 69 74 20 6f 72 20 her a commit or
1add8 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 74 68 65 20 rollback of the
1add9 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 current transact
1adda 69 6f 6e 2e 20 0a 20 20 20 20 2a 2a 0a 20 20 20 ion. . **.
1addb 20 2a 2a 20 4e 6f 74 65 3a 20 54 68 69 73 20 62 ** Note: This b
1addc 6c 6f 63 6b 20 61 6c 73 6f 20 72 75 6e 73 20 69 lock also runs i
1addd 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 70 65 f one of the spe
1adde 63 69 61 6c 20 65 72 72 6f 72 73 20 68 61 6e 64 cial errors hand
1addf 6c 65 64 20 0a 20 20 20 20 2a 2a 20 61 62 6f 76 led . ** abov
1ade0 65 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20 e has occurred.
1ade1 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
1ade2 21 73 71 6c 69 74 65 33 56 74 61 62 49 6e 53 79 !sqlite3VtabInSy
1ade3 6e 63 28 64 62 29 20 0a 20 20 20 20 20 26 26 20 nc(db) . &&
1ade4 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 0a db->autoCommit .
1ade5 20 20 20 20 20 26 26 20 64 62 2d 3e 77 72 69 74 && db->writ
1ade6 65 56 64 62 65 43 6e 74 3d 3d 28 70 2d 3e 72 65 eVdbeCnt==(p->re
1ade7 61 64 4f 6e 6c 79 3d 3d 30 29 20 0a 20 20 20 20 adOnly==0) .
1ade8 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e ){. if( p->
1ade9 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c rc==SQLITE_OK ||
1adea 20 28 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e (p->errorAction
1adeb 3d 3d 4f 45 5f 46 61 69 6c 20 26 26 20 21 69 73 ==OE_Fail && !is
1adec 53 70 65 63 69 61 6c 45 72 72 6f 72 29 20 29 7b SpecialError) ){
1aded 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c . if( sql
1adee 69 74 65 33 56 64 62 65 43 68 65 63 6b 46 6b 28 ite3VdbeCheckFk(
1adef 70 2c 20 31 29 20 29 7b 0a 20 20 20 20 20 20 20 p, 1) ){.
1adf0 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d sqlite3BtreeM
1adf1 75 74 65 78 41 72 72 61 79 4c 65 61 76 65 28 26 utexArrayLeave(&
1adf2 70 2d 3e 61 4d 75 74 65 78 29 3b 0a 20 20 20 20 p->aMutex);.
1adf3 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
1adf4 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 ITE_ERROR;.
1adf5 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 }. /*
1adf6 54 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 The auto-commit
1adf7 66 6c 61 67 20 69 73 20 74 72 75 65 2c 20 74 68 flag is true, th
1adf8 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 77 e vdbe program w
1adf9 61 73 20 73 75 63 63 65 73 73 66 75 6c 20 0a 20 as successful .
1adfa 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 68 69 74 ** or hit
1adfb 20 61 6e 20 27 4f 52 20 46 41 49 4c 27 20 63 6f an 'OR FAIL' co
1adfc 6e 73 74 72 61 69 6e 74 20 61 6e 64 20 74 68 65 nstraint and the
1adfd 72 65 20 61 72 65 20 6e 6f 20 64 65 66 65 72 72 re are no deferr
1adfe 65 64 20 66 6f 72 65 69 67 6e 0a 20 20 20 20 20 ed foreign.
1adff 20 20 20 2a 2a 20 6b 65 79 20 63 6f 6e 73 74 72 ** key constr
1ae00 61 69 6e 74 73 20 74 6f 20 68 6f 6c 64 20 75 70 aints to hold up
1ae01 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
1ae02 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 61 20 63 . This means a c
1ae03 6f 6d 6d 69 74 20 0a 20 20 20 20 20 20 20 20 2a ommit . *
1ae04 2a 20 69 73 20 72 65 71 75 69 72 65 64 2e 20 20 * is required.
1ae05 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 */. rc =
1ae06 76 64 62 65 43 6f 6d 6d 69 74 28 64 62 2c 20 70 vdbeCommit(db, p
1ae07 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 );. if( r
1ae08 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 c==SQLITE_BUSY )
1ae09 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 {. sqli
1ae0a 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 te3BtreeMutexArr
1ae0b 61 79 4c 65 61 76 65 28 26 70 2d 3e 61 4d 75 74 ayLeave(&p->aMut
1ae0c 65 78 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 ex);. r
1ae0d 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 eturn SQLITE_BUS
1ae0e 59 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 Y;. }else
1ae0f 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1ae10 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
1ae11 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 p->rc = rc;.
1ae12 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6c sqlite3Rol
1ae13 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 lbackAll(db);.
1ae14 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1ae15 20 20 20 20 20 20 20 64 62 2d 3e 6e 44 65 66 65 db->nDefe
1ae16 72 72 65 64 43 6f 6e 73 20 3d 20 30 3b 0a 20 20 rredCons = 0;.
1ae17 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 sqlite3C
1ae18 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43 68 61 ommitInternalCha
1ae19 6e 67 65 73 28 64 62 29 3b 0a 20 20 20 20 20 20 nges(db);.
1ae1a 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b }. }else{
1ae1b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1ae1c 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b RollbackAll(db);
1ae1d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 . }. d
1ae1e 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 20 3d 20 b->nStatement =
1ae1f 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 0;. }else if(
1ae20 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 3d 3d 30 eStatementOp==0
1ae21 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 2d ){. if( p-
1ae22 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c >rc==SQLITE_OK |
1ae23 7c 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e | p->errorAction
1ae24 3d 3d 4f 45 5f 46 61 69 6c 20 29 7b 0a 20 20 20 ==OE_Fail ){.
1ae25 20 20 20 20 20 65 53 74 61 74 65 6d 65 6e 74 4f eStatementO
1ae26 70 20 3d 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 p = SAVEPOINT_RE
1ae27 4c 45 41 53 45 3b 0a 20 20 20 20 20 20 7d 65 6c LEASE;. }el
1ae28 73 65 20 69 66 28 20 70 2d 3e 65 72 72 6f 72 41 se if( p->errorA
1ae29 63 74 69 6f 6e 3d 3d 4f 45 5f 41 62 6f 72 74 20 ction==OE_Abort
1ae2a 29 7b 0a 20 20 20 20 20 20 20 20 65 53 74 61 74 ){. eStat
1ae2b 65 6d 65 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f ementOp = SAVEPO
1ae2c 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20 INT_ROLLBACK;.
1ae2d 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1ae2e 20 20 20 69 6e 76 61 6c 69 64 61 74 65 43 75 72 invalidateCur
1ae2f 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 65 64 42 74 sorsOnModifiedBt
1ae30 72 65 65 73 28 64 62 29 3b 0a 20 20 20 20 20 20 rees(db);.
1ae31 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 sqlite3Rollbac
1ae32 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 kAll(db);.
1ae33 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 sqlite3CloseSa
1ae34 76 65 70 6f 69 6e 74 73 28 64 62 29 3b 0a 20 20 vepoints(db);.
1ae35 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f db->autoCo
1ae36 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 mmit = 1;.
1ae37 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f }. }. . /
1ae38 2a 20 49 66 20 65 53 74 61 74 65 6d 65 6e 74 4f * If eStatementO
1ae39 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 p is non-zero, t
1ae3a 68 65 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 20 hen a statement
1ae3b 74 72 61 6e 73 61 63 74 69 6f 6e 20 6e 65 65 64 transaction need
1ae3c 73 20 74 6f 0a 20 20 20 20 2a 2a 20 62 65 20 63 s to. ** be c
1ae3d 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c ommitted or roll
1ae3e 65 64 20 62 61 63 6b 2e 20 43 61 6c 6c 20 73 71 ed back. Call sq
1ae3f 6c 69 74 65 33 56 64 62 65 43 6c 6f 73 65 53 74 lite3VdbeCloseSt
1ae40 61 74 65 6d 65 6e 74 28 29 20 74 6f 0a 20 20 20 atement() to.
1ae41 20 2a 2a 20 64 6f 20 73 6f 2e 20 49 66 20 74 68 ** do so. If th
1ae42 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 72 65 74 is operation ret
1ae43 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 2c 20 61 urns an error, a
1ae44 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 nd the current s
1ae45 74 61 74 65 6d 65 6e 74 0a 20 20 20 20 2a 2a 20 tatement. **
1ae46 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 53 51 error code is SQ
1ae47 4c 49 54 45 5f 4f 4b 20 6f 72 20 53 51 4c 49 54 LITE_OK or SQLIT
1ae48 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20 74 68 E_CONSTRAINT, th
1ae49 65 6e 20 73 65 74 20 74 68 65 20 65 72 72 6f 72 en set the error
1ae4a 0a 20 20 20 20 2a 2a 20 63 6f 64 65 20 74 6f 20 . ** code to
1ae4b 74 68 65 20 6e 65 77 20 76 61 6c 75 65 2e 0a 20 the new value..
1ae4c 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 53 */. if( eS
1ae4d 74 61 74 65 6d 65 6e 74 4f 70 20 29 7b 0a 20 20 tatementOp ){.
1ae4e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1ae4f 56 64 62 65 43 6c 6f 73 65 53 74 61 74 65 6d 65 VdbeCloseStateme
1ae50 6e 74 28 70 2c 20 65 53 74 61 74 65 6d 65 6e 74 nt(p, eStatement
1ae51 4f 70 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 Op);. if( r
1ae52 63 20 26 26 20 28 70 2d 3e 72 63 3d 3d 53 51 4c c && (p->rc==SQL
1ae53 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63 3d ITE_OK || p->rc=
1ae54 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 =SQLITE_CONSTRAI
1ae55 4e 54 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 NT) ){. p
1ae56 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 20 ->rc = rc;.
1ae57 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
1ae58 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 (db, p->zErrMsg)
1ae59 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 7a 45 72 ;. p->zEr
1ae5a 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 rMsg = 0;.
1ae5b 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f }. }. . /
1ae5c 2a 20 49 66 20 74 68 69 73 20 77 61 73 20 61 6e * If this was an
1ae5d 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 20 INSERT, UPDATE
1ae5e 6f 72 20 44 45 4c 45 54 45 20 61 6e 64 20 6e 6f or DELETE and no
1ae5f 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 statement trans
1ae60 61 63 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 68 61 action. ** ha
1ae61 73 20 62 65 65 6e 20 72 6f 6c 6c 65 64 20 62 61 s been rolled ba
1ae62 63 6b 2c 20 75 70 64 61 74 65 20 74 68 65 20 64 ck, update the d
1ae63 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
1ae64 6f 6e 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 on change-counte
1ae65 72 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 r. . */. i
1ae66 66 28 20 70 2d 3e 63 68 61 6e 67 65 43 6e 74 4f f( p->changeCntO
1ae67 6e 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 65 n ){. if( e
1ae68 53 74 61 74 65 6d 65 6e 74 4f 70 21 3d 53 41 56 StatementOp!=SAV
1ae69 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 EPOINT_ROLLBACK
1ae6a 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
1ae6b 65 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 e3VdbeSetChanges
1ae6c 28 64 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 65 29 (db, p->nChange)
1ae6d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
1ae6e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
1ae6f 62 65 53 65 74 43 68 61 6e 67 65 73 28 64 62 2c beSetChanges(db,
1ae70 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 0);. }.
1ae71 20 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 p->nChange =
1ae72 30 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 0;. }. .
1ae73 2f 2a 20 52 6f 6c 6c 62 61 63 6b 20 6f 72 20 63 /* Rollback or c
1ae74 6f 6d 6d 69 74 20 61 6e 79 20 73 63 68 65 6d 61 ommit any schema
1ae75 20 63 68 61 6e 67 65 73 20 74 68 61 74 20 6f 63 changes that oc
1ae76 63 75 72 72 65 64 2e 20 2a 2f 0a 20 20 20 20 69 curred. */. i
1ae77 66 28 20 70 2d 3e 72 63 21 3d 53 51 4c 49 54 45 f( p->rc!=SQLITE
1ae78 5f 4f 4b 20 26 26 20 64 62 2d 3e 66 6c 61 67 73 _OK && db->flags
1ae79 26 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 &SQLITE_InternCh
1ae7a 61 6e 67 65 73 20 29 7b 0a 20 20 20 20 20 20 73 anges ){. s
1ae7b 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 qlite3ResetInter
1ae7c 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 nalSchema(db, 0)
1ae7d 3b 0a 20 20 20 20 20 20 64 62 2d 3e 66 6c 61 67 ;. db->flag
1ae7e 73 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20 7c s = (db->flags |
1ae7f 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 SQLITE_InternCh
1ae80 61 6e 67 65 73 29 3b 0a 20 20 20 20 7d 0a 0a 20 anges);. }..
1ae81 20 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 /* Release th
1ae82 65 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20 20 20 73 e locks */. s
1ae83 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 qlite3BtreeMutex
1ae84 41 72 72 61 79 4c 65 61 76 65 28 26 70 2d 3e 61 ArrayLeave(&p->a
1ae85 4d 75 74 65 78 29 3b 0a 20 20 7d 0a 0a 20 20 2f Mutex);. }.. /
1ae86 2a 20 57 65 20 68 61 76 65 20 73 75 63 63 65 73 * We have succes
1ae87 73 66 75 6c 6c 79 20 68 61 6c 74 65 64 20 61 6e sfully halted an
1ae88 64 20 63 6c 6f 73 65 64 20 74 68 65 20 56 4d 2e d closed the VM.
1ae89 20 20 52 65 63 6f 72 64 20 74 68 69 73 20 66 61 Record this fa
1ae8a 63 74 2e 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e ct. */. if( p->
1ae8b 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20 64 62 2d pc>=0 ){. db-
1ae8c 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 2d 2d >activeVdbeCnt--
1ae8d 3b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 72 65 ;. if( !p->re
1ae8e 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20 20 adOnly ){.
1ae8f 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74 db->writeVdbeCnt
1ae90 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 --;. }. as
1ae91 73 65 72 74 28 20 64 62 2d 3e 61 63 74 69 76 65 sert( db->active
1ae92 56 64 62 65 43 6e 74 3e 3d 64 62 2d 3e 77 72 69 VdbeCnt>=db->wri
1ae93 74 65 56 64 62 65 43 6e 74 20 29 3b 0a 20 20 7d teVdbeCnt );. }
1ae94 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 . p->magic = VD
1ae95 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 3b 0a 20 BE_MAGIC_HALT;.
1ae96 20 63 68 65 63 6b 41 63 74 69 76 65 56 64 62 65 checkActiveVdbe
1ae97 43 6e 74 28 64 62 29 3b 0a 20 20 69 66 28 20 70 Cnt(db);. if( p
1ae98 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ->db->mallocFail
1ae99 65 64 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 ed ){. p->rc
1ae9a 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a = SQLITE_NOMEM;.
1ae9b 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 }.. /* If the
1ae9c 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 auto-commit fla
1ae9d 67 20 69 73 20 73 65 74 20 74 6f 20 74 72 75 65 g is set to true
1ae9e 2c 20 74 68 65 6e 20 61 6e 79 20 6c 6f 63 6b 73 , then any locks
1ae9f 20 74 68 61 74 20 77 65 72 65 20 68 65 6c 64 0a that were held.
1aea0 20 20 2a 2a 20 62 79 20 63 6f 6e 6e 65 63 74 69 ** by connecti
1aea1 6f 6e 20 64 62 20 68 61 76 65 20 6e 6f 77 20 62 on db have now b
1aea2 65 65 6e 20 72 65 6c 65 61 73 65 64 2e 20 43 61 een released. Ca
1aea3 6c 6c 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 ll sqlite3Connec
1aea4 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 29 20 0a tionUnlocked() .
1aea5 20 20 2a 2a 20 74 6f 20 69 6e 76 6f 6b 65 20 61 ** to invoke a
1aea6 6e 79 20 72 65 71 75 69 72 65 64 20 75 6e 6c 6f ny required unlo
1aea7 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 ck-notify callba
1aea8 63 6b 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 cks.. */. if(
1aea9 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 db->autoCommit )
1aeaa 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6e {. sqlite3Con
1aeab 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 nectionUnlocked(
1aeac 64 62 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 db);. }.. asse
1aead 72 74 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64 rt( db->activeVd
1aeae 62 65 43 6e 74 3e 30 20 7c 7c 20 64 62 2d 3e 61 beCnt>0 || db->a
1aeaf 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c 20 utoCommit==0 ||
1aeb0 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3d 3d db->nStatement==
1aeb1 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 0 );. return SQ
1aeb2 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a LITE_OK;.}.../*.
1aeb3 2a 2a 20 45 61 63 68 20 56 44 42 45 20 68 6f 6c ** Each VDBE hol
1aeb4 64 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 ds the result of
1aeb5 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 the most recent
1aeb6 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 sqlite3_step()
1aeb7 63 61 6c 6c 0a 2a 2a 20 69 6e 20 70 2d 3e 72 63 call.** in p->rc
1aeb8 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 . This routine
1aeb9 73 65 74 73 20 74 68 61 74 20 72 65 73 75 6c 74 sets that result
1aeba 20 62 61 63 6b 20 74 6f 20 53 51 4c 49 54 45 5f back to SQLITE_
1aebb 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 OK..*/.SQLITE_PR
1aebc 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1aebd 65 33 56 64 62 65 52 65 73 65 74 53 74 65 70 52 e3VdbeResetStepR
1aebe 65 73 75 6c 74 28 56 64 62 65 20 2a 70 29 7b 0a esult(Vdbe *p){.
1aebf 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 p->rc = SQLITE
1aec0 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c _OK;.}../*.** Cl
1aec1 65 61 6e 20 75 70 20 61 20 56 44 42 45 20 61 66 ean up a VDBE af
1aec2 74 65 72 20 65 78 65 63 75 74 69 6f 6e 20 62 75 ter execution bu
1aec3 74 20 64 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 t do not delete
1aec4 74 68 65 20 56 44 42 45 20 6a 75 73 74 20 79 65 the VDBE just ye
1aec5 74 2e 0a 2a 2a 20 57 72 69 74 65 20 61 6e 79 20 t..** Write any
1aec6 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 69 error messages i
1aec7 6e 74 6f 20 2a 70 7a 45 72 72 4d 73 67 2e 20 20 nto *pzErrMsg.
1aec8 52 65 74 75 72 6e 20 74 68 65 20 72 65 73 75 6c Return the resul
1aec9 74 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 66 t code..**.** Af
1aeca 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 ter this routine
1aecb 20 69 73 20 72 75 6e 2c 20 74 68 65 20 56 44 42 is run, the VDB
1aecc 45 20 73 68 6f 75 6c 64 20 62 65 20 72 65 61 64 E should be read
1aecd 79 20 74 6f 20 62 65 20 65 78 65 63 75 74 65 64 y to be executed
1aece 0a 2a 2a 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a .** again..**.**
1aecf 20 54 6f 20 6c 6f 6f 6b 20 61 74 20 69 74 20 61 To look at it a
1aed0 6e 6f 74 68 65 72 20 77 61 79 2c 20 74 68 69 73 nother way, this
1aed1 20 72 6f 75 74 69 6e 65 20 72 65 73 65 74 73 20 routine resets
1aed2 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 the state of the
1aed3 0a 2a 2a 20 76 69 72 74 75 61 6c 20 6d 61 63 68 .** virtual mach
1aed4 69 6e 65 20 66 72 6f 6d 20 56 44 42 45 5f 4d 41 ine from VDBE_MA
1aed5 47 49 43 5f 52 55 4e 20 6f 72 20 56 44 42 45 5f GIC_RUN or VDBE_
1aed6 4d 41 47 49 43 5f 48 41 4c 54 20 62 61 63 6b 20 MAGIC_HALT back
1aed7 74 6f 0a 2a 2a 20 56 44 42 45 5f 4d 41 47 49 43 to.** VDBE_MAGIC
1aed8 5f 49 4e 49 54 2e 0a 2a 2f 0a 53 51 4c 49 54 45 _INIT..*/.SQLITE
1aed9 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1aeda 69 74 65 33 56 64 62 65 52 65 73 65 74 28 56 64 ite3VdbeReset(Vd
1aedb 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 be *p){. sqlite
1aedc 33 20 2a 64 62 3b 0a 20 20 64 62 20 3d 20 70 2d 3 *db;. db = p-
1aedd 3e 64 62 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 >db;.. /* If th
1aede 65 20 56 4d 20 64 69 64 20 6e 6f 74 20 72 75 6e e VM did not run
1aedf 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f to completion o
1aee0 72 20 69 66 20 69 74 20 65 6e 63 6f 75 6e 74 65 r if it encounte
1aee1 72 65 64 20 61 6e 0a 20 20 2a 2a 20 65 72 72 6f red an. ** erro
1aee2 72 2c 20 74 68 65 6e 20 69 74 20 6d 69 67 68 74 r, then it might
1aee3 20 6e 6f 74 20 68 61 76 65 20 62 65 65 6e 20 68 not have been h
1aee4 61 6c 74 65 64 20 70 72 6f 70 65 72 6c 79 2e 20 alted properly.
1aee5 20 53 6f 20 68 61 6c 74 0a 20 20 2a 2a 20 69 74 So halt. ** it
1aee6 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 28 76 6f now.. */. (vo
1aee7 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 id)sqlite3Safety
1aee8 4f 6e 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 65 On(db);. sqlite
1aee9 33 56 64 62 65 48 61 6c 74 28 70 29 3b 0a 20 20 3VdbeHalt(p);.
1aeea 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 (void)sqlite3Saf
1aeeb 65 74 79 4f 66 66 28 64 62 29 3b 0a 0a 20 20 2f etyOff(db);.. /
1aeec 2a 20 49 66 20 74 68 65 20 56 44 42 45 20 68 61 * If the VDBE ha
1aeed 73 20 62 65 20 72 75 6e 20 65 76 65 6e 20 70 61 s be run even pa
1aeee 72 74 69 61 6c 6c 79 2c 20 74 68 65 6e 20 74 72 rtially, then tr
1aeef 61 6e 73 66 65 72 20 74 68 65 20 65 72 72 6f 72 ansfer the error
1aef0 20 63 6f 64 65 0a 20 20 2a 2a 20 61 6e 64 20 65 code. ** and e
1aef1 72 72 6f 72 20 6d 65 73 73 61 67 65 20 66 72 6f rror message fro
1aef2 6d 20 74 68 65 20 56 44 42 45 20 69 6e 74 6f 20 m the VDBE into
1aef3 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 the main databas
1aef4 65 20 73 74 72 75 63 74 75 72 65 2e 20 20 42 75 e structure. Bu
1aef5 74 0a 20 20 2a 2a 20 69 66 20 74 68 65 20 56 44 t. ** if the VD
1aef6 42 45 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e BE has just been
1aef7 20 73 65 74 20 74 6f 20 72 75 6e 20 62 75 74 20 set to run but
1aef8 68 61 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 has not actually
1aef9 20 65 78 65 63 75 74 65 64 20 61 6e 79 0a 20 20 executed any.
1aefa 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 ** instructions
1aefb 79 65 74 2c 20 6c 65 61 76 65 20 74 68 65 20 6d yet, leave the m
1aefc 61 69 6e 20 64 61 74 61 62 61 73 65 20 65 72 72 ain database err
1aefd 6f 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 or information u
1aefe 6e 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2f 0a 20 nchanged.. */.
1aeff 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30 20 29 7b if( p->pc>=0 ){
1af00 0a 20 20 20 20 69 66 28 20 70 2d 3e 7a 45 72 72 . if( p->zErr
1af01 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 73 71 6c Msg ){. sql
1af02 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d ite3BeginBenignM
1af03 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 73 alloc();. s
1af04 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 qlite3ValueSetSt
1af05 72 28 64 62 2d 3e 70 45 72 72 2c 2d 31 2c 70 2d r(db->pErr,-1,p-
1af06 3e 7a 45 72 72 4d 73 67 2c 53 51 4c 49 54 45 5f >zErrMsg,SQLITE_
1af07 55 54 46 38 2c 53 51 4c 49 54 45 5f 54 52 41 4e UTF8,SQLITE_TRAN
1af08 53 49 45 4e 54 29 3b 0a 20 20 20 20 20 20 73 71 SIENT);. sq
1af09 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 lite3EndBenignMa
1af0a 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 64 62 lloc();. db
1af0b 2d 3e 65 72 72 43 6f 64 65 20 3d 20 70 2d 3e 72 ->errCode = p->r
1af0c 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 c;. sqlite3
1af0d 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 DbFree(db, p->zE
1af0e 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 70 2d rrMsg);. p-
1af0f 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 >zErrMsg = 0;.
1af10 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 72 }else if( p->r
1af11 63 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 c ){. sqlit
1af12 65 33 45 72 72 6f 72 28 64 62 2c 20 70 2d 3e 72 e3Error(db, p->r
1af13 63 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 c, 0);. }else
1af14 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 {. sqlite3E
1af15 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f rror(db, SQLITE_
1af16 4f 4b 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 OK, 0);. }.
1af17 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 72 63 20 }else if( p->rc
1af18 26 26 20 70 2d 3e 65 78 70 69 72 65 64 20 29 7b && p->expired ){
1af19 0a 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 69 . /* The expi
1af1a 72 65 64 20 66 6c 61 67 20 77 61 73 20 73 65 74 red flag was set
1af1b 20 6f 6e 20 74 68 65 20 56 44 42 45 20 62 65 66 on the VDBE bef
1af1c 6f 72 65 20 74 68 65 20 66 69 72 73 74 20 63 61 ore the first ca
1af1d 6c 6c 0a 20 20 20 20 2a 2a 20 74 6f 20 73 71 6c ll. ** to sql
1af1e 69 74 65 33 5f 73 74 65 70 28 29 2e 20 46 6f 72 ite3_step(). For
1af1f 20 63 6f 6e 73 69 73 74 65 6e 63 79 20 28 73 69 consistency (si
1af20 6e 63 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70 nce sqlite3_step
1af21 28 29 20 77 61 73 0a 20 20 20 20 2a 2a 20 63 61 () was. ** ca
1af22 6c 6c 65 64 29 2c 20 73 65 74 20 74 68 65 20 64 lled), set the d
1af23 61 74 61 62 61 73 65 20 65 72 72 6f 72 20 69 6e atabase error in
1af24 20 74 68 69 73 20 63 61 73 65 20 61 73 20 77 65 this case as we
1af25 6c 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 ll.. */. s
1af26 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 qlite3Error(db,
1af27 70 2d 3e 72 63 2c 20 30 29 3b 0a 20 20 20 20 73 p->rc, 0);. s
1af28 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 qlite3ValueSetSt
1af29 72 28 64 62 2d 3e 70 45 72 72 2c 20 2d 31 2c 20 r(db->pErr, -1,
1af2a 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 53 51 4c 49 p->zErrMsg, SQLI
1af2b 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f TE_UTF8, SQLITE_
1af2c 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 TRANSIENT);.
1af2d 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1af2e 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 , p->zErrMsg);.
1af2f 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 p->zErrMsg =
1af30 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 63 0;. }.. /* Rec
1af31 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 laim all memory
1af32 75 73 65 64 20 62 79 20 74 68 65 20 56 44 42 45 used by the VDBE
1af33 0a 20 20 2a 2f 0a 20 20 43 6c 65 61 6e 75 70 28 . */. Cleanup(
1af34 70 29 3b 0a 0a 20 20 2f 2a 20 53 61 76 65 20 70 p);.. /* Save p
1af35 72 6f 66 69 6c 69 6e 67 20 69 6e 66 6f 72 6d 61 rofiling informa
1af36 74 69 6f 6e 20 66 72 6f 6d 20 74 68 69 73 20 56 tion from this V
1af37 44 42 45 20 72 75 6e 2e 0a 20 20 2a 2f 0a 23 69 DBE run.. */.#i
1af38 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c fdef VDBE_PROFIL
1af39 45 0a 20 20 7b 0a 20 20 20 20 46 49 4c 45 20 2a E. {. FILE *
1af3a 6f 75 74 20 3d 20 66 6f 70 65 6e 28 22 76 64 62 out = fopen("vdb
1af3b 65 5f 70 72 6f 66 69 6c 65 2e 6f 75 74 22 2c 20 e_profile.out",
1af3c 22 61 22 29 3b 0a 20 20 20 20 69 66 28 20 6f 75 "a");. if( ou
1af3d 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 t ){. int i
1af3e 3b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 ;. fprintf(
1af3f 6f 75 74 2c 20 22 2d 2d 2d 2d 20 22 29 3b 0a 20 out, "---- ");.
1af40 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c for(i=0; i<
1af41 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a 20 20 p->nOp; i++){.
1af42 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 fprintf(ou
1af43 74 2c 20 22 25 30 32 78 22 2c 20 70 2d 3e 61 4f t, "%02x", p->aO
1af44 70 5b 69 5d 2e 6f 70 63 6f 64 65 29 3b 0a 20 20 p[i].opcode);.
1af45 20 20 20 20 7d 0a 20 20 20 20 20 20 66 70 72 69 }. fpri
1af46 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 0a ntf(out, "\n");.
1af47 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 for(i=0; i
1af48 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a 20 <p->nOp; i++){.
1af49 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f fprintf(o
1af4a 75 74 2c 20 22 25 36 64 20 25 31 30 6c 6c 64 20 ut, "%6d %10lld
1af4b 25 38 6c 6c 64 20 22 2c 0a 20 20 20 20 20 20 20 %8lld ",.
1af4c 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e p->aOp[i].cn
1af4d 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 2d t,. p-
1af4e 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 2c 0a >aOp[i].cycles,.
1af4f 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 61 4f p->aO
1af50 70 5b 69 5d 2e 63 6e 74 3e 30 20 3f 20 70 2d 3e p[i].cnt>0 ? p->
1af51 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 2f 70 2d aOp[i].cycles/p-
1af52 3e 61 4f 70 5b 69 5d 2e 63 6e 74 20 3a 20 30 0a >aOp[i].cnt : 0.
1af53 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 );.
1af54 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 sqlite3VdbePr
1af55 69 6e 74 4f 70 28 6f 75 74 2c 20 69 2c 20 26 70 intOp(out, i, &p
1af56 2d 3e 61 4f 70 5b 69 5d 29 3b 0a 20 20 20 20 20 ->aOp[i]);.
1af57 20 7d 0a 20 20 20 20 20 20 66 63 6c 6f 73 65 28 }. fclose(
1af58 6f 75 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a out);. }. }.
1af59 23 65 6e 64 69 66 0a 20 20 70 2d 3e 6d 61 67 69 #endif. p->magi
1af5a 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 c = VDBE_MAGIC_I
1af5b 4e 49 54 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d NIT;. return p-
1af5c 3e 72 63 20 26 20 64 62 2d 3e 65 72 72 4d 61 73 >rc & db->errMas
1af5d 6b 3b 0a 7d 0a 20 0a 2f 2a 0a 2a 2a 20 43 6c 65 k;.}. ./*.** Cle
1af5e 61 6e 20 75 70 20 61 6e 64 20 64 65 6c 65 74 65 an up and delete
1af5f 20 61 20 56 44 42 45 20 61 66 74 65 72 20 65 78 a VDBE after ex
1af60 65 63 75 74 69 6f 6e 2e 20 20 52 65 74 75 72 6e ecution. Return
1af61 20 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 an integer whic
1af62 68 20 69 73 0a 2a 2a 20 74 68 65 20 72 65 73 75 h is.** the resu
1af63 6c 74 20 63 6f 64 65 2e 20 20 57 72 69 74 65 20 lt code. Write
1af64 61 6e 79 20 65 72 72 6f 72 20 6d 65 73 73 61 67 any error messag
1af65 65 20 74 65 78 74 20 69 6e 74 6f 20 2a 70 7a 45 e text into *pzE
1af66 72 72 4d 73 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 rrMsg..*/.SQLITE
1af67 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1af68 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 ite3VdbeFinalize
1af69 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 (Vdbe *p){. int
1af6a 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
1af6b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 3d . if( p->magic=
1af6c 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 =VDBE_MAGIC_RUN
1af6d 7c 7c 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 || p->magic==VDB
1af6e 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 29 7b 0a E_MAGIC_HALT ){.
1af6f 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1af70 56 64 62 65 52 65 73 65 74 28 70 29 3b 0a 20 20 VdbeReset(p);.
1af71 20 20 61 73 73 65 72 74 28 20 28 72 63 20 26 20 assert( (rc &
1af72 70 2d 3e 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3d p->db->errMask)=
1af73 3d 72 63 20 29 3b 0a 20 20 7d 0a 20 20 73 71 6c =rc );. }. sql
1af74 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 28 70 ite3VdbeDelete(p
1af75 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
1af76 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 74 68 }../*.** Call th
1af77 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 6f 72 e destructor for
1af78 20 65 61 63 68 20 61 75 78 64 61 74 61 20 65 6e each auxdata en
1af79 74 72 79 20 69 6e 20 70 56 64 62 65 46 75 6e 63 try in pVdbeFunc
1af7a 20 66 6f 72 20 77 68 69 63 68 0a 2a 2a 20 74 68 for which.** th
1af7b 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 e corresponding
1af7c 62 69 74 20 69 6e 20 6d 61 73 6b 20 69 73 20 63 bit in mask is c
1af7d 6c 65 61 72 2e 20 20 41 75 78 64 61 74 61 20 65 lear. Auxdata e
1af7e 6e 74 72 69 65 73 20 62 65 79 6f 6e 64 20 33 31 ntries beyond 31
1af7f 0a 2a 2a 20 61 72 65 20 61 6c 77 61 79 73 20 64 .** are always d
1af80 65 73 74 72 6f 79 65 64 2e 20 20 54 6f 20 64 65 estroyed. To de
1af81 73 74 72 6f 79 20 61 6c 6c 20 61 75 78 64 61 74 stroy all auxdat
1af82 61 20 65 6e 74 72 69 65 73 2c 20 63 61 6c 6c 20 a entries, call
1af83 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 this.** routine
1af84 77 69 74 68 20 6d 61 73 6b 3d 3d 30 2e 0a 2a 2f with mask==0..*/
1af85 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1af86 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
1af87 44 65 6c 65 74 65 41 75 78 44 61 74 61 28 56 64 DeleteAuxData(Vd
1af88 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75 6e beFunc *pVdbeFun
1af89 63 2c 20 69 6e 74 20 6d 61 73 6b 29 7b 0a 20 20 c, int mask){.
1af8a 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 int i;. for(i=0
1af8b 3b 20 69 3c 70 56 64 62 65 46 75 6e 63 2d 3e 6e ; i<pVdbeFunc->n
1af8c 41 75 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 Aux; i++){. s
1af8d 74 72 75 63 74 20 41 75 78 44 61 74 61 20 2a 70 truct AuxData *p
1af8e 41 75 78 20 3d 20 26 70 56 64 62 65 46 75 6e 63 Aux = &pVdbeFunc
1af8f 2d 3e 61 70 41 75 78 5b 69 5d 3b 0a 20 20 20 20 ->apAux[i];.
1af90 69 66 28 20 28 69 3e 33 31 20 7c 7c 20 21 28 6d if( (i>31 || !(m
1af91 61 73 6b 26 28 28 28 75 33 32 29 31 29 3c 3c 69 ask&(((u32)1)<<i
1af92 29 29 29 20 26 26 20 70 41 75 78 2d 3e 70 41 75 ))) && pAux->pAu
1af93 78 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 x ){. if( p
1af94 41 75 78 2d 3e 78 44 65 6c 65 74 65 20 29 7b 0a Aux->xDelete ){.
1af95 20 20 20 20 20 20 20 20 70 41 75 78 2d 3e 78 44 pAux->xD
1af96 65 6c 65 74 65 28 70 41 75 78 2d 3e 70 41 75 78 elete(pAux->pAux
1af97 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
1af98 20 70 41 75 78 2d 3e 70 41 75 78 20 3d 20 30 3b pAux->pAux = 0;
1af99 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a . }. }.}../*
1af9a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 65 6e .** Delete an en
1af9b 74 69 72 65 20 56 44 42 45 2e 0a 2a 2f 0a 53 51 tire VDBE..*/.SQ
1af9c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1af9d 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c d sqlite3VdbeDel
1af9e 65 74 65 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 ete(Vdbe *p){.
1af9f 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 sqlite3 *db;..
1afa0 69 66 28 20 4e 45 56 45 52 28 70 3d 3d 30 29 20 if( NEVER(p==0)
1afa1 29 20 72 65 74 75 72 6e 3b 0a 20 20 64 62 20 3d ) return;. db =
1afa2 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 2d p->db;. if( p-
1afa3 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20 70 2d >pPrev ){. p-
1afa4 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 >pPrev->pNext =
1afa5 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65 6c 73 p->pNext;. }els
1afa6 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 e{. assert( d
1afa7 62 2d 3e 70 56 64 62 65 3d 3d 70 20 29 3b 0a 20 b->pVdbe==p );.
1afa8 20 20 20 64 62 2d 3e 70 56 64 62 65 20 3d 20 70 db->pVdbe = p
1afa9 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 ->pNext;. }. i
1afaa 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 f( p->pNext ){.
1afab 20 20 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 p->pNext->pPr
1afac 65 76 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0a 20 ev = p->pPrev;.
1afad 20 7d 0a 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 }. releaseMemA
1afae 72 72 61 79 28 70 2d 3e 61 56 61 72 2c 20 70 2d rray(p->aVar, p-
1afaf 3e 6e 56 61 72 29 3b 0a 20 20 72 65 6c 65 61 73 >nVar);. releas
1afb0 65 4d 65 6d 41 72 72 61 79 28 70 2d 3e 61 43 6f eMemArray(p->aCo
1afb1 6c 4e 61 6d 65 2c 20 70 2d 3e 6e 52 65 73 43 6f lName, p->nResCo
1afb2 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f 4e 29 3b lumn*COLNAME_N);
1afb3 0a 20 20 76 64 62 65 46 72 65 65 4f 70 41 72 72 . vdbeFreeOpArr
1afb4 61 79 28 64 62 2c 20 70 2d 3e 61 4f 70 2c 20 70 ay(db, p->aOp, p
1afb5 2d 3e 6e 4f 70 29 3b 0a 20 20 73 71 6c 69 74 65 ->nOp);. sqlite
1afb6 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61 3DbFree(db, p->a
1afb7 4c 61 62 65 6c 29 3b 0a 20 20 73 71 6c 69 74 65 Label);. sqlite
1afb8 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61 3DbFree(db, p->a
1afb9 43 6f 6c 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 ColName);. sqli
1afba 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d te3DbFree(db, p-
1afbb 3e 7a 53 71 6c 29 3b 0a 20 20 70 2d 3e 6d 61 67 >zSql);. p->mag
1afbc 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f ic = VDBE_MAGIC_
1afbd 44 45 41 44 3b 0a 20 20 73 71 6c 69 74 65 33 44 DEAD;. sqlite3D
1afbe 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 70 46 72 bFree(db, p->pFr
1afbf 65 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 ee);. sqlite3Db
1afc0 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 7d 0a 0a Free(db, p);.}..
1afc1 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 /*.** Make sure
1afc2 74 68 65 20 63 75 72 73 6f 72 20 70 20 69 73 20 the cursor p is
1afc3 72 65 61 64 79 20 74 6f 20 72 65 61 64 20 6f 72 ready to read or
1afc4 20 77 72 69 74 65 20 74 68 65 20 72 6f 77 20 74 write the row t
1afc5 6f 20 77 68 69 63 68 20 69 74 0a 2a 2a 20 77 61 o which it.** wa
1afc6 73 20 6c 61 73 74 20 70 6f 73 69 74 69 6f 6e 65 s last positione
1afc7 64 2e 20 20 52 65 74 75 72 6e 20 61 6e 20 65 72 d. Return an er
1afc8 72 6f 72 20 63 6f 64 65 20 69 66 20 61 6e 20 4f ror code if an O
1afc9 4f 4d 20 66 61 75 6c 74 20 6f 72 20 49 2f 4f 20 OM fault or I/O
1afca 65 72 72 6f 72 0a 2a 2a 20 70 72 65 76 65 6e 74 error.** prevent
1afcb 73 20 75 73 20 66 72 6f 6d 20 70 6f 73 69 74 69 s us from positi
1afcc 6f 6e 69 6e 67 20 74 68 65 20 63 75 72 73 6f 72 oning the cursor
1afcd 20 74 6f 20 69 74 73 20 63 6f 72 72 65 63 74 20 to its correct
1afce 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 position..**.**
1afcf 49 66 20 61 20 4d 6f 76 65 54 6f 20 6f 70 65 72 If a MoveTo oper
1afd0 61 74 69 6f 6e 20 69 73 20 70 65 6e 64 69 6e 67 ation is pending
1afd1 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 63 75 on the given cu
1afd2 72 73 6f 72 2c 20 74 68 65 6e 20 64 6f 20 74 68 rsor, then do th
1afd3 61 74 0a 2a 2a 20 4d 6f 76 65 54 6f 20 6e 6f 77 at.** MoveTo now
1afd4 2e 20 20 49 66 20 6e 6f 20 6d 6f 76 65 20 69 73 . If no move is
1afd5 20 70 65 6e 64 69 6e 67 2c 20 63 68 65 63 6b 20 pending, check
1afd6 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 72 6f to see if the ro
1afd7 77 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 64 65 w has been.** de
1afd8 6c 65 74 65 64 20 6f 75 74 20 66 72 6f 6d 20 75 leted out from u
1afd9 6e 64 65 72 20 74 68 65 20 63 75 72 73 6f 72 20 nder the cursor
1afda 61 6e 64 20 69 66 20 69 74 20 68 61 73 2c 20 6d and if it has, m
1afdb 61 72 6b 20 74 68 65 20 72 6f 77 20 61 73 0a 2a ark the row as.*
1afdc 2a 20 61 20 4e 55 4c 4c 20 72 6f 77 2e 0a 2a 2a * a NULL row..**
1afdd 0a 2a 2a 20 49 66 20 74 68 65 20 63 75 72 73 6f .** If the curso
1afde 72 20 69 73 20 61 6c 72 65 61 64 79 20 70 6f 69 r is already poi
1afdf 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 63 6f 72 nting to the cor
1afe0 72 65 63 74 20 72 6f 77 20 61 6e 64 20 74 68 61 rect row and tha
1afe1 74 20 72 6f 77 20 68 61 73 0a 2a 2a 20 6e 6f 74 t row has.** not
1afe2 20 62 65 65 6e 20 64 65 6c 65 74 65 64 20 6f 75 been deleted ou
1afe3 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 74 68 65 t from under the
1afe4 20 63 75 72 73 6f 72 2c 20 74 68 65 6e 20 74 68 cursor, then th
1afe5 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 is routine is a
1afe6 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 no-op..*/.SQLITE
1afe7 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1afe8 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f ite3VdbeCursorMo
1afe9 76 65 74 6f 28 56 64 62 65 43 75 72 73 6f 72 20 veto(VdbeCursor
1afea 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 64 65 *p){. if( p->de
1afeb 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 29 7b 0a ferredMoveto ){.
1afec 20 20 20 20 69 6e 74 20 72 65 73 2c 20 72 63 3b int res, rc;
1afed 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
1afee 45 53 54 0a 20 20 20 20 65 78 74 65 72 6e 20 69 EST. extern i
1afef 6e 74 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63 nt sqlite3_searc
1aff0 68 5f 63 6f 75 6e 74 3b 0a 23 65 6e 64 69 66 0a h_count;.#endif.
1aff1 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 assert( p->i
1aff2 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 72 63 sTable );. rc
1aff3 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d = sqlite3BtreeM
1aff4 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 2d ovetoUnpacked(p-
1aff5 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 70 2d 3e >pCursor, 0, p->
1aff6 6d 6f 76 65 74 6f 54 61 72 67 65 74 2c 20 30 2c movetoTarget, 0,
1aff7 20 26 72 65 73 29 3b 0a 20 20 20 20 69 66 28 20 &res);. if(
1aff8 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
1aff9 20 20 20 20 70 2d 3e 6c 61 73 74 52 6f 77 69 64 p->lastRowid
1affa 20 3d 20 70 2d 3e 6d 6f 76 65 74 6f 54 61 72 67 = p->movetoTarg
1affb 65 74 3b 0a 20 20 20 20 70 2d 3e 72 6f 77 69 64 et;. p->rowid
1affc 49 73 56 61 6c 69 64 20 3d 20 41 4c 57 41 59 53 IsValid = ALWAYS
1affd 28 72 65 73 3d 3d 30 29 20 3f 31 3a 30 3b 0a 20 (res==0) ?1:0;.
1affe 20 20 20 69 66 28 20 4e 45 56 45 52 28 72 65 73 if( NEVER(res
1afff 3c 30 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 <0) ){. rc
1b000 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 = sqlite3BtreeNe
1b001 78 74 28 70 2d 3e 70 43 75 72 73 6f 72 2c 20 26 xt(p->pCursor, &
1b002 72 65 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 res);. if(
1b003 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
1b004 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c }.#ifdef SQL
1b005 49 54 45 5f 54 45 53 54 0a 20 20 20 20 73 71 6c ITE_TEST. sql
1b006 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e ite3_search_coun
1b007 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 t++;.#endif.
1b008 70 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 p->deferredMovet
1b009 6f 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 63 61 o = 0;. p->ca
1b00a 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 cheStatus = CACH
1b00b 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 65 6c 73 65 E_STALE;. }else
1b00c 20 69 66 28 20 41 4c 57 41 59 53 28 70 2d 3e 70 if( ALWAYS(p->p
1b00d 43 75 72 73 6f 72 29 20 29 7b 0a 20 20 20 20 69 Cursor) ){. i
1b00e 6e 74 20 68 61 73 4d 6f 76 65 64 3b 0a 20 20 20 nt hasMoved;.
1b00f 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 int rc = sqlite
1b010 33 42 74 72 65 65 43 75 72 73 6f 72 48 61 73 4d 3BtreeCursorHasM
1b011 6f 76 65 64 28 70 2d 3e 70 43 75 72 73 6f 72 2c oved(p->pCursor,
1b012 20 26 68 61 73 4d 6f 76 65 64 29 3b 0a 20 20 20 &hasMoved);.
1b013 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
1b014 20 72 63 3b 0a 20 20 20 20 69 66 28 20 68 61 73 rc;. if( has
1b015 4d 6f 76 65 64 20 29 7b 0a 20 20 20 20 20 20 70 Moved ){. p
1b016 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 ->cacheStatus =
1b017 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 20 CACHE_STALE;.
1b018 20 20 20 70 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 p->nullRow =
1b019 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 1;. }. }. r
1b01a 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
1b01b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f .}../*.** The fo
1b01c 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f 6e llowing function
1b01d 73 3a 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 s:.**.** sqlite3
1b01e 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 29 VdbeSerialType()
1b01f 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53 .** sqlite3VdbeS
1b020 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 29 0a 2a erialTypeLen().*
1b021 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 * sqlite3VdbeSer
1b022 69 61 6c 4c 65 6e 28 29 0a 2a 2a 20 73 71 6c 69 ialLen().** sqli
1b023 74 65 33 56 64 62 65 53 65 72 69 61 6c 50 75 74 te3VdbeSerialPut
1b024 28 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 ().** sqlite3Vdb
1b025 65 53 65 72 69 61 6c 47 65 74 28 29 0a 2a 2a 0a eSerialGet().**.
1b026 2a 2a 20 65 6e 63 61 70 73 75 6c 61 74 65 20 74 ** encapsulate t
1b027 68 65 20 63 6f 64 65 20 74 68 61 74 20 73 65 72 he code that ser
1b028 69 61 6c 69 7a 65 73 20 76 61 6c 75 65 73 20 66 ializes values f
1b029 6f 72 20 73 74 6f 72 61 67 65 20 69 6e 20 53 51 or storage in SQ
1b02a 4c 69 74 65 0a 2a 2a 20 64 61 74 61 20 61 6e 64 Lite.** data and
1b02b 20 69 6e 64 65 78 20 72 65 63 6f 72 64 73 2e 20 index records.
1b02c 45 61 63 68 20 73 65 72 69 61 6c 69 7a 65 64 20 Each serialized
1b02d 76 61 6c 75 65 20 63 6f 6e 73 69 73 74 73 20 6f value consists o
1b02e 66 20 61 0a 2a 2a 20 27 73 65 72 69 61 6c 2d 74 f a.** 'serial-t
1b02f 79 70 65 27 20 61 6e 64 20 61 20 62 6c 6f 62 20 ype' and a blob
1b030 6f 66 20 64 61 74 61 2e 20 54 68 65 20 73 65 72 of data. The ser
1b031 69 61 6c 20 74 79 70 65 20 69 73 20 61 6e 20 38 ial type is an 8
1b032 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 0a 2a -byte unsigned.*
1b033 2a 20 69 6e 74 65 67 65 72 2c 20 73 74 6f 72 65 * integer, store
1b034 64 20 61 73 20 61 20 76 61 72 69 6e 74 2e 0a 2a d as a varint..*
1b035 2a 0a 2a 2a 20 49 6e 20 61 6e 20 53 51 4c 69 74 *.** In an SQLit
1b036 65 20 69 6e 64 65 78 20 72 65 63 6f 72 64 2c 20 e index record,
1b037 74 68 65 20 73 65 72 69 61 6c 20 74 79 70 65 20 the serial type
1b038 69 73 20 73 74 6f 72 65 64 20 64 69 72 65 63 74 is stored direct
1b039 6c 79 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65 ly before.** the
1b03a 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 20 74 68 blob of data th
1b03b 61 74 20 69 74 20 63 6f 72 72 65 73 70 6f 6e 64 at it correspond
1b03c 73 20 74 6f 2e 20 49 6e 20 61 20 74 61 62 6c 65 s to. In a table
1b03d 20 72 65 63 6f 72 64 2c 20 61 6c 6c 20 73 65 72 record, all ser
1b03e 69 61 6c 0a 2a 2a 20 74 79 70 65 73 20 61 72 65 ial.** types are
1b03f 20 73 74 6f 72 65 64 20 61 74 20 74 68 65 20 73 stored at the s
1b040 74 61 72 74 20 6f 66 20 74 68 65 20 72 65 63 6f tart of the reco
1b041 72 64 2c 20 61 6e 64 20 74 68 65 20 62 6c 6f 62 rd, and the blob
1b042 73 20 6f 66 20 64 61 74 61 20 61 74 0a 2a 2a 20 s of data at.**
1b043 74 68 65 20 65 6e 64 2e 20 48 65 6e 63 65 20 74 the end. Hence t
1b044 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 hese functions a
1b045 6c 6c 6f 77 20 74 68 65 20 63 61 6c 6c 65 72 20 llow the caller
1b046 74 6f 20 68 61 6e 64 6c 65 20 74 68 65 0a 2a 2a to handle the.**
1b047 20 73 65 72 69 61 6c 2d 74 79 70 65 20 61 6e 64 serial-type and
1b048 20 64 61 74 61 20 62 6c 6f 62 20 73 65 70 65 72 data blob seper
1b049 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ately..**.** The
1b04a 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 following table
1b04b 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 76 describes the v
1b04c 61 72 69 6f 75 73 20 73 74 6f 72 61 67 65 20 63 arious storage c
1b04d 6c 61 73 73 65 73 20 66 6f 72 20 64 61 74 61 3a lasses for data:
1b04e 0a 2a 2a 0a 2a 2a 20 20 20 73 65 72 69 61 6c 20 .**.** serial
1b04f 74 79 70 65 20 20 20 20 20 20 20 20 62 79 74 65 type byte
1b050 73 20 6f 66 20 64 61 74 61 20 20 20 20 20 20 74 s of data t
1b051 79 70 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d ype.** -------
1b052 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20 2d 2d 2d 2d ------- ----
1b053 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 2d ----------- -
1b054 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a --------------.*
1b055 2a 20 20 20 20 20 20 30 20 20 20 20 20 20 20 20 * 0
1b056 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 20 0
1b057 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 0a 2a NULL.*
1b058 2a 20 20 20 20 20 20 31 20 20 20 20 20 20 20 20 * 1
1b059 20 20 20 20 20 20 20 20 20 20 20 20 20 31 20 20 1
1b05a 20 20 20 20 20 20 20 20 20 20 73 69 67 6e 65 64 signed
1b05b 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 integer.**
1b05c 20 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2
1b05d 20 20 20 20 20 20 20 32 20 20 20 20 20 20 20 20 2
1b05e 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65 67 signed integ
1b05f 65 72 0a 2a 2a 20 20 20 20 20 20 33 20 20 20 20 er.** 3
1b060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b061 20 33 20 20 20 20 20 20 20 20 20 20 20 20 73 69 3 si
1b062 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 gned integer.**
1b063 20 20 20 20 20 34 20 20 20 20 20 20 20 20 20 20 4
1b064 20 20 20 20 20 20 20 20 20 20 20 34 20 20 20 20 4
1b065 20 20 20 20 20 20 20 20 73 69 67 6e 65 64 20 69 signed i
1b066 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 35 nteger.** 5
1b067 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b068 20 20 20 20 20 36 20 20 20 20 20 20 20 20 20 20 6
1b069 20 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 signed integer
1b06a 0a 2a 2a 20 20 20 20 20 20 36 20 20 20 20 20 20 .** 6
1b06b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 8
1b06c 20 20 20 20 20 20 20 20 20 20 20 20 73 69 67 6e sign
1b06d 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 ed integer.**
1b06e 20 20 20 37 20 20 20 20 20 20 20 20 20 20 20 20 7
1b06f 20 20 20 20 20 20 20 20 20 38 20 20 20 20 20 20 8
1b070 20 20 20 20 20 20 49 45 45 45 20 66 6c 6f 61 74 IEEE float
1b071 0a 2a 2a 20 20 20 20 20 20 38 20 20 20 20 20 20 .** 8
1b072 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0
1b073 20 20 20 20 20 20 20 20 20 20 20 20 49 6e 74 65 Inte
1b074 67 65 72 20 63 6f 6e 73 74 61 6e 74 20 30 0a 2a ger constant 0.*
1b075 2a 20 20 20 20 20 20 39 20 20 20 20 20 20 20 20 * 9
1b076 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 20 0
1b077 20 20 20 20 20 20 20 20 20 20 49 6e 74 65 67 65 Intege
1b078 72 20 63 6f 6e 73 74 61 6e 74 20 31 0a 2a 2a 20 r constant 1.**
1b079 20 20 20 20 31 30 2c 31 31 20 20 20 20 20 20 20 10,11
1b07a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b07b 20 20 20 20 20 20 20 20 72 65 73 65 72 76 65 64 reserved
1b07c 20 66 6f 72 20 65 78 70 61 6e 73 69 6f 6e 0a 2a for expansion.*
1b07d 2a 20 20 20 20 4e 3e 3d 31 32 20 61 6e 64 20 65 * N>=12 and e
1b07e 76 65 6e 20 20 20 20 20 20 20 28 4e 2d 31 32 29 ven (N-12)
1b07f 2f 32 20 20 20 20 20 20 20 20 42 4c 4f 42 0a 2a /2 BLOB.*
1b080 2a 20 20 20 20 4e 3e 3d 31 33 20 61 6e 64 20 6f * N>=13 and o
1b081 64 64 20 20 20 20 20 20 20 20 28 4e 2d 31 33 29 dd (N-13)
1b082 2f 32 20 20 20 20 20 20 20 20 74 65 78 74 0a 2a /2 text.*
1b083 2a 0a 2a 2a 20 54 68 65 20 38 20 61 6e 64 20 39 *.** The 8 and 9
1b084 20 74 79 70 65 73 20 77 65 72 65 20 61 64 64 65 types were adde
1b085 64 20 69 6e 20 33 2e 33 2e 30 2c 20 66 69 6c 65 d in 3.3.0, file
1b086 20 66 6f 72 6d 61 74 20 34 2e 20 20 50 72 69 6f format 4. Prio
1b087 72 20 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 6f 66 r versions.** of
1b088 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f 74 SQLite will not
1b089 20 75 6e 64 65 72 73 74 61 6e 64 20 74 68 6f 73 understand thos
1b08a 65 20 73 65 72 69 61 6c 20 74 79 70 65 73 2e 0a e serial types..
1b08b 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e */../*.** Return
1b08c 20 74 68 65 20 73 65 72 69 61 6c 2d 74 79 70 65 the serial-type
1b08d 20 66 6f 72 20 74 68 65 20 76 61 6c 75 65 20 73 for the value s
1b08e 74 6f 72 65 64 20 69 6e 20 70 4d 65 6d 2e 0a 2a tored in pMem..*
1b08f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1b090 20 75 33 32 20 73 71 6c 69 74 65 33 56 64 62 65 u32 sqlite3Vdbe
1b091 53 65 72 69 61 6c 54 79 70 65 28 4d 65 6d 20 2a SerialType(Mem *
1b092 70 4d 65 6d 2c 20 69 6e 74 20 66 69 6c 65 5f 66 pMem, int file_f
1b093 6f 72 6d 61 74 29 7b 0a 20 20 69 6e 74 20 66 6c ormat){. int fl
1b094 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 ags = pMem->flag
1b095 73 3b 0a 20 20 69 6e 74 20 6e 3b 0a 0a 20 20 69 s;. int n;.. i
1b096 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c f( flags&MEM_Nul
1b097 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 l ){. return
1b098 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 6c 61 0;. }. if( fla
1b099 67 73 26 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 gs&MEM_Int ){.
1b09a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 /* Figure out
1b09b 77 68 65 74 68 65 72 20 74 6f 20 75 73 65 20 31 whether to use 1
1b09c 2c 20 32 2c 20 34 2c 20 36 20 6f 72 20 38 20 62 , 2, 4, 6 or 8 b
1b09d 79 74 65 73 2e 20 2a 2f 0a 23 20 20 20 64 65 66 ytes. */.# def
1b09e 69 6e 65 20 4d 41 58 5f 36 42 59 54 45 20 28 28 ine MAX_6BYTE ((
1b09f 28 28 69 36 34 29 30 78 30 30 30 30 38 30 30 30 ((i64)0x00008000
1b0a0 29 3c 3c 33 32 29 2d 31 29 0a 20 20 20 20 69 36 )<<32)-1). i6
1b0a1 34 20 69 20 3d 20 70 4d 65 6d 2d 3e 75 2e 69 3b 4 i = pMem->u.i;
1b0a2 0a 20 20 20 20 75 36 34 20 75 3b 0a 20 20 20 20 . u64 u;.
1b0a3 69 66 28 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3e if( file_format>
1b0a4 3d 34 20 26 26 20 28 69 26 31 29 3d 3d 69 20 29 =4 && (i&1)==i )
1b0a5 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 38 {. return 8
1b0a6 2b 28 75 33 32 29 69 3b 0a 20 20 20 20 7d 0a 20 +(u32)i;. }.
1b0a7 20 20 20 75 20 3d 20 69 3c 30 20 3f 20 2d 69 20 u = i<0 ? -i
1b0a8 3a 20 69 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d : i;. if( u<=
1b0a9 31 32 37 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 127 ) return 1;.
1b0aa 20 20 20 20 69 66 28 20 75 3c 3d 33 32 37 36 37 if( u<=32767
1b0ab 20 29 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 ) return 2;.
1b0ac 20 69 66 28 20 75 3c 3d 38 33 38 38 36 30 37 20 if( u<=8388607
1b0ad 29 20 72 65 74 75 72 6e 20 33 3b 0a 20 20 20 20 ) return 3;.
1b0ae 69 66 28 20 75 3c 3d 32 31 34 37 34 38 33 36 34 if( u<=214748364
1b0af 37 20 29 20 72 65 74 75 72 6e 20 34 3b 0a 20 20 7 ) return 4;.
1b0b0 20 20 69 66 28 20 75 3c 3d 4d 41 58 5f 36 42 59 if( u<=MAX_6BY
1b0b1 54 45 20 29 20 72 65 74 75 72 6e 20 35 3b 0a 20 TE ) return 5;.
1b0b2 20 20 20 72 65 74 75 72 6e 20 36 3b 0a 20 20 7d return 6;. }
1b0b3 0a 20 20 69 66 28 20 66 6c 61 67 73 26 4d 45 4d . if( flags&MEM
1b0b4 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 65 74 _Real ){. ret
1b0b5 75 72 6e 20 37 3b 0a 20 20 7d 0a 20 20 61 73 73 urn 7;. }. ass
1b0b6 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 2d 3e 6d ert( pMem->db->m
1b0b7 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 66 allocFailed || f
1b0b8 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 lags&(MEM_Str|ME
1b0b9 4d 5f 42 6c 6f 62 29 20 29 3b 0a 20 20 6e 20 3d M_Blob) );. n =
1b0ba 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 20 69 66 28 20 pMem->n;. if(
1b0bb 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f flags & MEM_Zero
1b0bc 20 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 70 4d 65 ){. n += pMe
1b0bd 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 7d 0a m->u.nZero;. }.
1b0be 20 20 61 73 73 65 72 74 28 20 6e 3e 3d 30 20 29 assert( n>=0 )
1b0bf 3b 0a 20 20 72 65 74 75 72 6e 20 28 28 6e 2a 32 ;. return ((n*2
1b0c0 29 20 2b 20 31 32 20 2b 20 28 28 66 6c 61 67 73 ) + 12 + ((flags
1b0c1 26 4d 45 4d 5f 53 74 72 29 21 3d 30 29 29 3b 0a &MEM_Str)!=0));.
1b0c2 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
1b0c3 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 the length of th
1b0c4 65 20 64 61 74 61 20 63 6f 72 72 65 73 70 6f 6e e data correspon
1b0c5 64 69 6e 67 20 74 6f 20 74 68 65 20 73 75 70 70 ding to the supp
1b0c6 6c 69 65 64 20 73 65 72 69 61 6c 2d 74 79 70 65 lied serial-type
1b0c7 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1b0c8 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 56 ATE u32 sqlite3V
1b0c9 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e dbeSerialTypeLen
1b0ca 28 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 (u32 serial_type
1b0cb 29 7b 0a 20 20 69 66 28 20 73 65 72 69 61 6c 5f ){. if( serial_
1b0cc 74 79 70 65 3e 3d 31 32 20 29 7b 0a 20 20 20 20 type>=12 ){.
1b0cd 72 65 74 75 72 6e 20 28 73 65 72 69 61 6c 5f 74 return (serial_t
1b0ce 79 70 65 2d 31 32 29 2f 32 3b 0a 20 20 7d 65 6c ype-12)/2;. }el
1b0cf 73 65 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63 se{. static c
1b0d0 6f 6e 73 74 20 75 38 20 61 53 69 7a 65 5b 5d 20 onst u8 aSize[]
1b0d1 3d 20 7b 20 30 2c 20 31 2c 20 32 2c 20 33 2c 20 = { 0, 1, 2, 3,
1b0d2 34 2c 20 36 2c 20 38 2c 20 38 2c 20 30 2c 20 30 4, 6, 8, 8, 0, 0
1b0d3 2c 20 30 2c 20 30 20 7d 3b 0a 20 20 20 20 72 65 , 0, 0 };. re
1b0d4 74 75 72 6e 20 61 53 69 7a 65 5b 73 65 72 69 61 turn aSize[seria
1b0d5 6c 5f 74 79 70 65 5d 3b 0a 20 20 7d 0a 7d 0a 0a l_type];. }.}..
1b0d6 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72 65 20 /*.** If we are
1b0d7 6f 6e 20 61 6e 20 61 72 63 68 69 74 65 63 74 75 on an architectu
1b0d8 72 65 20 77 69 74 68 20 6d 69 78 65 64 2d 65 6e re with mixed-en
1b0d9 64 69 61 6e 20 66 6c 6f 61 74 69 6e 67 20 0a 2a dian floating .*
1b0da 2a 20 70 6f 69 6e 74 73 20 28 65 78 3a 20 41 52 * points (ex: AR
1b0db 4d 37 29 20 74 68 65 6e 20 73 77 61 70 20 74 68 M7) then swap th
1b0dc 65 20 6c 6f 77 65 72 20 34 20 62 79 74 65 73 20 e lower 4 bytes
1b0dd 77 69 74 68 20 74 68 65 20 0a 2a 2a 20 75 70 70 with the .** upp
1b0de 65 72 20 34 20 62 79 74 65 73 2e 20 20 52 65 74 er 4 bytes. Ret
1b0df 75 72 6e 20 74 68 65 20 72 65 73 75 6c 74 2e 0a urn the result..
1b0e0 2a 2a 0a 2a 2a 20 46 6f 72 20 6d 6f 73 74 20 61 **.** For most a
1b0e1 72 63 68 69 74 65 63 74 75 72 65 73 2c 20 74 68 rchitectures, th
1b0e2 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a is is a no-op..*
1b0e3 2a 0a 2a 2a 20 28 6c 61 74 65 72 29 3a 20 20 49 *.** (later): I
1b0e4 74 20 69 73 20 72 65 70 6f 72 74 65 64 20 74 6f t is reported to
1b0e5 20 6d 65 20 74 68 61 74 20 74 68 65 20 6d 69 78 me that the mix
1b0e6 65 64 2d 65 6e 64 69 61 6e 20 70 72 6f 62 6c 65 ed-endian proble
1b0e7 6d 0a 2a 2a 20 6f 6e 20 41 52 4d 37 20 69 73 20 m.** on ARM7 is
1b0e8 61 6e 20 69 73 73 75 65 20 77 69 74 68 20 47 43 an issue with GC
1b0e9 43 2c 20 6e 6f 74 20 77 69 74 68 20 74 68 65 20 C, not with the
1b0ea 41 52 4d 37 20 63 68 69 70 2e 20 20 49 74 20 73 ARM7 chip. It s
1b0eb 65 65 6d 73 0a 2a 2a 20 74 68 61 74 20 65 61 72 eems.** that ear
1b0ec 6c 79 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 47 ly versions of G
1b0ed 43 43 20 73 74 6f 72 65 64 20 74 68 65 20 74 77 CC stored the tw
1b0ee 6f 20 77 6f 72 64 73 20 6f 66 20 61 20 36 34 2d o words of a 64-
1b0ef 62 69 74 0a 2a 2a 20 66 6c 6f 61 74 20 69 6e 20 bit.** float in
1b0f0 74 68 65 20 77 72 6f 6e 67 20 6f 72 64 65 72 2e the wrong order.
1b0f1 20 20 41 6e 64 20 74 68 61 74 20 65 72 72 6f 72 And that error
1b0f2 20 68 61 73 20 62 65 65 6e 20 70 72 6f 70 61 67 has been propag
1b0f3 61 74 65 64 0a 2a 2a 20 65 76 65 72 20 73 69 6e ated.** ever sin
1b0f4 63 65 2e 20 20 54 68 65 20 62 6c 61 6d 65 20 69 ce. The blame i
1b0f5 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c s not necessaril
1b0f6 79 20 77 69 74 68 20 47 43 43 2c 20 74 68 6f 75 y with GCC, thou
1b0f7 67 68 2e 0a 2a 2a 20 47 43 43 20 6d 69 67 68 74 gh..** GCC might
1b0f8 20 68 61 76 65 20 6a 75 73 74 20 63 6f 70 79 69 have just copyi
1b0f9 6e 67 20 74 68 65 20 70 72 6f 62 6c 65 6d 20 66 ng the problem f
1b0fa 72 6f 6d 20 61 20 70 72 69 6f 72 20 63 6f 6d 70 rom a prior comp
1b0fb 69 6c 65 72 2e 0a 2a 2a 20 49 20 61 6d 20 61 6c iler..** I am al
1b0fc 73 6f 20 74 6f 6c 64 20 74 68 61 74 20 6e 65 77 so told that new
1b0fd 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 47 er versions of G
1b0fe 43 43 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 61 CC that follow a
1b0ff 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 41 42 different.** AB
1b100 49 20 67 65 74 20 74 68 65 20 62 79 74 65 20 6f I get the byte o
1b101 72 64 65 72 20 72 69 67 68 74 2e 0a 2a 2a 0a 2a rder right..**.*
1b102 2a 20 44 65 76 65 6c 6f 70 65 72 73 20 75 73 69 * Developers usi
1b103 6e 67 20 53 51 4c 69 74 65 20 6f 6e 20 61 6e 20 ng SQLite on an
1b104 41 52 4d 37 20 73 68 6f 75 6c 64 20 63 6f 6d 70 ARM7 should comp
1b105 69 6c 65 20 61 6e 64 20 72 75 6e 20 74 68 65 69 ile and run thei
1b106 72 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e r.** application
1b107 20 75 73 69 6e 67 20 2d 44 53 51 4c 49 54 45 5f using -DSQLITE_
1b108 44 45 42 55 47 3d 31 20 61 74 20 6c 65 61 73 74 DEBUG=1 at least
1b109 20 6f 6e 63 65 2e 20 20 57 69 74 68 20 44 45 42 once. With DEB
1b10a 55 47 0a 2a 2a 20 65 6e 61 62 6c 65 64 2c 20 73 UG.** enabled, s
1b10b 6f 6d 65 20 61 73 73 65 72 74 73 20 62 65 6c 6f ome asserts belo
1b10c 77 20 77 69 6c 6c 20 65 6e 73 75 72 65 20 74 68 w will ensure th
1b10d 61 74 20 74 68 65 20 62 79 74 65 20 6f 72 64 65 at the byte orde
1b10e 72 20 6f 66 0a 2a 2a 20 66 6c 6f 61 74 69 6e 67 r of.** floating
1b10f 20 70 6f 69 6e 74 20 76 61 6c 75 65 73 20 69 73 point values is
1b110 20 63 6f 72 72 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 correct..**.**
1b111 28 32 30 30 37 2d 30 38 2d 33 30 29 20 20 46 72 (2007-08-30) Fr
1b112 61 6e 6b 20 76 61 6e 20 56 75 67 74 20 68 61 73 ank van Vugt has
1b113 20 73 74 75 64 69 65 64 20 74 68 69 73 20 70 72 studied this pr
1b114 6f 62 6c 65 6d 20 63 6c 6f 73 65 6c 79 0a 2a 2a oblem closely.**
1b115 20 61 6e 64 20 68 61 73 20 73 65 6e 64 20 68 69 and has send hi
1b116 73 20 66 69 6e 64 69 6e 67 73 20 74 6f 20 74 68 s findings to th
1b117 65 20 53 51 4c 69 74 65 20 64 65 76 65 6c 6f 70 e SQLite develop
1b118 65 72 73 2e 20 20 46 72 61 6e 6b 0a 2a 2a 20 77 ers. Frank.** w
1b119 72 69 74 65 73 20 74 68 61 74 20 73 6f 6d 65 20 rites that some
1b11a 4c 69 6e 75 78 20 6b 65 72 6e 65 6c 73 20 6f 66 Linux kernels of
1b11b 66 65 72 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 fer floating poi
1b11c 6e 74 20 68 61 72 64 77 61 72 65 0a 2a 2a 20 65 nt hardware.** e
1b11d 6d 75 6c 61 74 69 6f 6e 20 74 68 61 74 20 75 73 mulation that us
1b11e 65 73 20 6f 6e 6c 79 20 33 32 2d 62 69 74 20 6d es only 32-bit m
1b11f 61 6e 74 69 73 73 61 73 20 69 6e 73 74 65 61 64 antissas instead
1b120 20 6f 66 20 61 20 66 75 6c 6c 20 0a 2a 2a 20 34 of a full .** 4
1b121 38 2d 62 69 74 73 20 61 73 20 72 65 71 75 69 72 8-bits as requir
1b122 65 64 20 62 79 20 74 68 65 20 49 45 45 45 20 73 ed by the IEEE s
1b123 74 61 6e 64 61 72 64 2e 20 20 28 54 68 69 73 20 tandard. (This
1b124 69 73 20 74 68 65 0a 2a 2a 20 43 4f 4e 46 49 47 is the.** CONFIG
1b125 5f 46 50 45 5f 46 41 53 54 46 50 45 20 6f 70 74 _FPE_FASTFPE opt
1b126 69 6f 6e 2e 29 20 20 4f 6e 20 73 75 63 68 20 73 ion.) On such s
1b127 79 73 74 65 6d 73 2c 20 66 6c 6f 61 74 69 6e 67 ystems, floating
1b128 20 70 6f 69 6e 74 0a 2a 2a 20 62 79 74 65 20 73 point.** byte s
1b129 77 61 70 70 69 6e 67 20 62 65 63 6f 6d 65 73 20 wapping becomes
1b12a 76 65 72 79 20 63 6f 6d 70 6c 69 63 61 74 65 64 very complicated
1b12b 2e 20 20 54 6f 20 61 76 6f 69 64 20 70 72 6f 62 . To avoid prob
1b12c 6c 65 6d 73 2c 0a 2a 2a 20 74 68 65 20 6e 65 63 lems,.** the nec
1b12d 65 73 73 61 72 79 20 62 79 74 65 20 73 77 61 70 essary byte swap
1b12e 70 69 6e 67 20 69 73 20 63 61 72 72 69 65 64 20 ping is carried
1b12f 6f 75 74 20 75 73 69 6e 67 20 61 20 36 34 2d 62 out using a 64-b
1b130 69 74 20 69 6e 74 65 67 65 72 0a 2a 2a 20 72 61 it integer.** ra
1b131 74 68 65 72 20 74 68 61 6e 20 61 20 36 34 2d 62 ther than a 64-b
1b132 69 74 20 66 6c 6f 61 74 2e 20 20 46 72 61 6e 6b it float. Frank
1b133 20 61 73 73 75 72 65 73 20 75 73 20 74 68 61 74 assures us that
1b134 20 74 68 65 20 63 6f 64 65 20 68 65 72 65 0a 2a the code here.*
1b135 2a 20 77 6f 72 6b 73 20 66 6f 72 20 68 69 6d 2e * works for him.
1b136 20 20 57 65 2c 20 74 68 65 20 64 65 76 65 6c 6f We, the develo
1b137 70 65 72 73 2c 20 68 61 76 65 20 6e 6f 20 77 61 pers, have no wa
1b138 79 20 74 6f 20 69 6e 64 65 70 65 6e 64 65 6e 74 y to independent
1b139 6c 79 0a 2a 2a 20 76 65 72 69 66 79 20 74 68 69 ly.** verify thi
1b13a 73 2c 20 62 75 74 20 46 72 61 6e 6b 20 73 65 65 s, but Frank see
1b13b 6d 73 20 74 6f 20 6b 6e 6f 77 20 77 68 61 74 20 ms to know what
1b13c 68 65 20 69 73 20 74 61 6c 6b 69 6e 67 20 61 62 he is talking ab
1b13d 6f 75 74 0a 2a 2a 20 73 6f 20 77 65 20 74 72 75 out.** so we tru
1b13e 73 74 20 68 69 6d 2e 0a 2a 2f 0a 23 69 66 64 65 st him..*/.#ifde
1b13f 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 44 5f 45 f SQLITE_MIXED_E
1b140 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f 41 NDIAN_64BIT_FLOA
1b141 54 0a 73 74 61 74 69 63 20 75 36 34 20 66 6c 6f T.static u64 flo
1b142 61 74 53 77 61 70 28 75 36 34 20 69 6e 29 7b 0a atSwap(u64 in){.
1b143 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 75 36 union {. u6
1b144 34 20 72 3b 0a 20 20 20 20 75 33 32 20 69 5b 32 4 r;. u32 i[2
1b145 5d 3b 0a 20 20 7d 20 75 3b 0a 20 20 75 33 32 20 ];. } u;. u32
1b146 74 3b 0a 0a 20 20 75 2e 72 20 3d 20 69 6e 3b 0a t;.. u.r = in;.
1b147 20 20 74 20 3d 20 75 2e 69 5b 30 5d 3b 0a 20 20 t = u.i[0];.
1b148 75 2e 69 5b 30 5d 20 3d 20 75 2e 69 5b 31 5d 3b u.i[0] = u.i[1];
1b149 0a 20 20 75 2e 69 5b 31 5d 20 3d 20 74 3b 0a 20 . u.i[1] = t;.
1b14a 20 72 65 74 75 72 6e 20 75 2e 72 3b 0a 7d 0a 23 return u.r;.}.#
1b14b 20 64 65 66 69 6e 65 20 73 77 61 70 4d 69 78 65 define swapMixe
1b14c 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 58 29 20 dEndianFloat(X)
1b14d 20 58 20 3d 20 66 6c 6f 61 74 53 77 61 70 28 58 X = floatSwap(X
1b14e 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 ).#else.# define
1b14f 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69 61 6e swapMixedEndian
1b150 46 6c 6f 61 74 28 58 29 0a 23 65 6e 64 69 66 0a Float(X).#endif.
1b151 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 ./*.** Write the
1b152 20 73 65 72 69 61 6c 69 7a 65 64 20 64 61 74 61 serialized data
1b153 20 62 6c 6f 62 20 66 6f 72 20 74 68 65 20 76 61 blob for the va
1b154 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 70 4d lue stored in pM
1b155 65 6d 20 69 6e 74 6f 20 0a 2a 2a 20 62 75 66 2e em into .** buf.
1b156 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 It is assumed t
1b157 68 61 74 20 74 68 65 20 63 61 6c 6c 65 72 20 68 hat the caller h
1b158 61 73 20 61 6c 6c 6f 63 61 74 65 64 20 73 75 66 as allocated suf
1b159 66 69 63 69 65 6e 74 20 73 70 61 63 65 2e 0a 2a ficient space..*
1b15a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d * Return the num
1b15b 62 65 72 20 6f 66 20 62 79 74 65 73 20 77 72 69 ber of bytes wri
1b15c 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 6e 42 75 66 tten..**.** nBuf
1b15d 20 69 73 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f is the amount o
1b15e 66 20 73 70 61 63 65 20 6c 65 66 74 20 69 6e 20 f space left in
1b15f 62 75 66 5b 5d 2e 20 20 6e 42 75 66 20 6d 75 73 buf[]. nBuf mus
1b160 74 20 61 6c 77 61 79 73 20 62 65 0a 2a 2a 20 6c t always be.** l
1b161 61 72 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 68 arge enough to h
1b162 6f 6c 64 20 74 68 65 20 65 6e 74 69 72 65 20 66 old the entire f
1b163 69 65 6c 64 2e 20 20 45 78 63 65 70 74 2c 20 69 ield. Except, i
1b164 66 20 74 68 65 20 66 69 65 6c 64 20 69 73 0a 2a f the field is.*
1b165 2a 20 61 20 62 6c 6f 62 20 77 69 74 68 20 61 20 * a blob with a
1b166 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c zero-filled tail
1b167 2c 20 74 68 65 6e 20 62 75 66 5b 5d 20 6d 69 67 , then buf[] mig
1b168 68 74 20 62 65 20 6a 75 73 74 20 74 68 65 20 72 ht be just the r
1b169 69 67 68 74 0a 2a 2a 20 73 69 7a 65 20 74 6f 20 ight.** size to
1b16a 68 6f 6c 64 20 65 76 65 72 79 74 68 69 6e 67 20 hold everything
1b16b 65 78 63 65 70 74 20 66 6f 72 20 74 68 65 20 7a except for the z
1b16c 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c 2e ero-filled tail.
1b16d 20 20 49 66 20 62 75 66 5b 5d 0a 2a 2a 20 69 73 If buf[].** is
1b16e 20 6f 6e 6c 79 20 62 69 67 20 65 6e 6f 75 67 68 only big enough
1b16f 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6e 6f 6e to hold the non
1b170 2d 7a 65 72 6f 20 70 72 65 66 69 78 2c 20 74 68 -zero prefix, th
1b171 65 6e 20 6f 6e 6c 79 20 77 72 69 74 65 20 74 68 en only write th
1b172 61 74 0a 2a 2a 20 70 72 65 66 69 78 20 69 6e 74 at.** prefix int
1b173 6f 20 62 75 66 5b 5d 2e 20 20 42 75 74 20 69 66 o buf[]. But if
1b174 20 62 75 66 5b 5d 20 69 73 20 6c 61 72 67 65 20 buf[] is large
1b175 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 62 enough to hold b
1b176 6f 74 68 20 74 68 65 0a 2a 2a 20 70 72 65 66 69 oth the.** prefi
1b177 78 20 61 6e 64 20 74 68 65 20 74 61 69 6c 20 74 x and the tail t
1b178 68 65 6e 20 77 72 69 74 65 20 74 68 65 20 70 72 hen write the pr
1b179 65 66 69 78 20 61 6e 64 20 73 65 74 20 74 68 65 efix and set the
1b17a 20 74 61 69 6c 20 74 6f 20 61 6c 6c 0a 2a 2a 20 tail to all.**
1b17b 7a 65 72 6f 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 zeros..**.** Ret
1b17c 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
1b17d 66 20 62 79 74 65 73 20 61 63 74 75 61 6c 6c 79 f bytes actually
1b17e 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 62 75 written into bu
1b17f 66 5b 5d 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 f[]. The number
1b180 0a 2a 2a 20 6f 66 20 62 79 74 65 73 20 69 6e 20 .** of bytes in
1b181 74 68 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 the zero-filled
1b182 74 61 69 6c 20 69 73 20 69 6e 63 6c 75 64 65 64 tail is included
1b183 20 69 6e 20 74 68 65 20 72 65 74 75 72 6e 20 76 in the return v
1b184 61 6c 75 65 20 6f 6e 6c 79 0a 2a 2a 20 69 66 20 alue only.** if
1b185 74 68 6f 73 65 20 62 79 74 65 73 20 77 65 72 65 those bytes were
1b186 20 7a 65 72 6f 65 64 20 69 6e 20 62 75 66 5b 5d zeroed in buf[]
1b187 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49 ..*/ .SQLITE_PRI
1b188 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 VATE u32 sqlite3
1b189 56 64 62 65 53 65 72 69 61 6c 50 75 74 28 75 38 VdbeSerialPut(u8
1b18a 20 2a 62 75 66 2c 20 69 6e 74 20 6e 42 75 66 2c *buf, int nBuf,
1b18b 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 Mem *pMem, int
1b18c 66 69 6c 65 5f 66 6f 72 6d 61 74 29 7b 0a 20 20 file_format){.
1b18d 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 20 u32 serial_type
1b18e 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 = sqlite3VdbeSer
1b18f 69 61 6c 54 79 70 65 28 70 4d 65 6d 2c 20 66 69 ialType(pMem, fi
1b190 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 75 33 le_format);. u3
1b191 32 20 6c 65 6e 3b 0a 0a 20 20 2f 2a 20 49 6e 74 2 len;.. /* Int
1b192 65 67 65 72 20 61 6e 64 20 52 65 61 6c 20 2a 2f eger and Real */
1b193 0a 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79 . if( serial_ty
1b194 70 65 3c 3d 37 20 26 26 20 73 65 72 69 61 6c 5f pe<=7 && serial_
1b195 74 79 70 65 3e 30 20 29 7b 0a 20 20 20 20 75 36 type>0 ){. u6
1b196 34 20 76 3b 0a 20 20 20 20 75 33 32 20 69 3b 0a 4 v;. u32 i;.
1b197 20 20 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 if( serial_t
1b198 79 70 65 3d 3d 37 20 29 7b 0a 20 20 20 20 20 20 ype==7 ){.
1b199 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 76 assert( sizeof(v
1b19a 29 3d 3d 73 69 7a 65 6f 66 28 70 4d 65 6d 2d 3e )==sizeof(pMem->
1b19b 72 29 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 r) );. memc
1b19c 70 79 28 26 76 2c 20 26 70 4d 65 6d 2d 3e 72 2c py(&v, &pMem->r,
1b19d 20 73 69 7a 65 6f 66 28 76 29 29 3b 0a 20 20 20 sizeof(v));.
1b19e 20 20 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69 swapMixedEndi
1b19f 61 6e 46 6c 6f 61 74 28 76 29 3b 0a 20 20 20 20 anFloat(v);.
1b1a0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 76 20 3d }else{. v =
1b1a1 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 20 20 pMem->u.i;.
1b1a2 7d 0a 20 20 20 20 6c 65 6e 20 3d 20 69 20 3d 20 }. len = i =
1b1a3 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 sqlite3VdbeSeria
1b1a4 6c 54 79 70 65 4c 65 6e 28 73 65 72 69 61 6c 5f lTypeLen(serial_
1b1a5 74 79 70 65 29 3b 0a 20 20 20 20 61 73 73 65 72 type);. asser
1b1a6 74 28 20 6c 65 6e 3c 3d 28 75 33 32 29 6e 42 75 t( len<=(u32)nBu
1b1a7 66 20 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 f );. while(
1b1a8 69 2d 2d 20 29 7b 0a 20 20 20 20 20 20 62 75 66 i-- ){. buf
1b1a9 5b 69 5d 20 3d 20 28 75 38 29 28 76 26 30 78 46 [i] = (u8)(v&0xF
1b1aa 46 29 3b 0a 20 20 20 20 20 20 76 20 3e 3e 3d 20 F);. v >>=
1b1ab 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 8;. }. ret
1b1ac 75 72 6e 20 6c 65 6e 3b 0a 20 20 7d 0a 0a 20 20 urn len;. }..
1b1ad 2f 2a 20 53 74 72 69 6e 67 20 6f 72 20 62 6c 6f /* String or blo
1b1ae 62 20 2a 2f 0a 20 20 69 66 28 20 73 65 72 69 61 b */. if( seria
1b1af 6c 5f 74 79 70 65 3e 3d 31 32 20 29 7b 0a 20 20 l_type>=12 ){.
1b1b0 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e assert( pMem->
1b1b1 6e 20 2b 20 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 n + ((pMem->flag
1b1b2 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 29 3f 70 4d s & MEM_Zero)?pM
1b1b3 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3a 30 29 0a 20 em->u.nZero:0).
1b1b4 20 20 20 20 20 20 20 20 20 20 20 20 3d 3d 20 28 == (
1b1b5 69 6e 74 29 73 71 6c 69 74 65 33 56 64 62 65 53 int)sqlite3VdbeS
1b1b6 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 73 65 72 erialTypeLen(ser
1b1b7 69 61 6c 5f 74 79 70 65 29 20 29 3b 0a 20 20 20 ial_type) );.
1b1b8 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 6e assert( pMem->n
1b1b9 3c 3d 6e 42 75 66 20 29 3b 0a 20 20 20 20 6c 65 <=nBuf );. le
1b1ba 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 20 20 n = pMem->n;.
1b1bb 20 6d 65 6d 63 70 79 28 62 75 66 2c 20 70 4d 65 memcpy(buf, pMe
1b1bc 6d 2d 3e 7a 2c 20 6c 65 6e 29 3b 0a 20 20 20 20 m->z, len);.
1b1bd 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 if( pMem->flags
1b1be 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 & MEM_Zero ){.
1b1bf 20 20 20 20 6c 65 6e 20 2b 3d 20 70 4d 65 6d 2d len += pMem-
1b1c0 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 20 20 >u.nZero;.
1b1c1 61 73 73 65 72 74 28 20 6e 42 75 66 3e 3d 30 20 assert( nBuf>=0
1b1c2 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6c 65 6e );. if( len
1b1c3 20 3e 20 28 75 33 32 29 6e 42 75 66 20 29 7b 0a > (u32)nBuf ){.
1b1c4 20 20 20 20 20 20 20 20 6c 65 6e 20 3d 20 28 75 len = (u
1b1c5 33 32 29 6e 42 75 66 3b 0a 20 20 20 20 20 20 7d 32)nBuf;. }
1b1c6 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 62 . memset(&b
1b1c7 75 66 5b 70 4d 65 6d 2d 3e 6e 5d 2c 20 30 2c 20 uf[pMem->n], 0,
1b1c8 6c 65 6e 2d 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 len-pMem->n);.
1b1c9 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 6c }. return l
1b1ca 65 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 55 en;. }.. /* NU
1b1cb 4c 4c 20 6f 72 20 63 6f 6e 73 74 61 6e 74 73 20 LL or constants
1b1cc 30 20 6f 72 20 31 20 2a 2f 0a 20 20 72 65 74 75 0 or 1 */. retu
1b1cd 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 rn 0;.}../*.** D
1b1ce 65 73 65 72 69 61 6c 69 7a 65 20 74 68 65 20 64 eserialize the d
1b1cf 61 74 61 20 62 6c 6f 62 20 70 6f 69 6e 74 65 64 ata blob pointed
1b1d0 20 74 6f 20 62 79 20 62 75 66 20 61 73 20 73 65 to by buf as se
1b1d1 72 69 61 6c 20 74 79 70 65 20 73 65 72 69 61 6c rial type serial
1b1d2 5f 74 79 70 65 0a 2a 2a 20 61 6e 64 20 73 74 6f _type.** and sto
1b1d3 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e re the result in
1b1d4 20 70 4d 65 6d 2e 20 20 52 65 74 75 72 6e 20 74 pMem. Return t
1b1d5 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 he number of byt
1b1d6 65 73 20 72 65 61 64 2e 0a 2a 2f 20 0a 53 51 4c es read..*/ .SQL
1b1d7 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 ITE_PRIVATE u32
1b1d8 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 sqlite3VdbeSeria
1b1d9 6c 47 65 74 28 0a 20 20 63 6f 6e 73 74 20 75 6e lGet(. const un
1b1da 73 69 67 6e 65 64 20 63 68 61 72 20 2a 62 75 66 signed char *buf
1b1db 2c 20 20 20 20 20 2f 2a 20 42 75 66 66 65 72 20 , /* Buffer
1b1dc 74 6f 20 64 65 73 65 72 69 61 6c 69 7a 65 20 66 to deserialize f
1b1dd 72 6f 6d 20 2a 2f 0a 20 20 75 33 32 20 73 65 72 rom */. u32 ser
1b1de 69 61 6c 5f 74 79 70 65 2c 20 20 20 20 20 20 20 ial_type,
1b1df 20 20 20 20 20 20 20 2f 2a 20 53 65 72 69 61 6c /* Serial
1b1e0 20 74 79 70 65 20 74 6f 20 64 65 73 65 72 69 61 type to deseria
1b1e1 6c 69 7a 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 lize */. Mem *p
1b1e2 4d 65 6d 20 20 20 20 20 20 20 20 20 20 20 20 20 Mem
1b1e3 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 /* Memor
1b1e4 79 20 63 65 6c 6c 20 74 6f 20 77 72 69 74 65 20 y cell to write
1b1e5 76 61 6c 75 65 20 69 6e 74 6f 20 2a 2f 0a 29 7b value into */.){
1b1e6 0a 20 20 73 77 69 74 63 68 28 20 73 65 72 69 61 . switch( seria
1b1e7 6c 5f 74 79 70 65 20 29 7b 0a 20 20 20 20 63 61 l_type ){. ca
1b1e8 73 65 20 31 30 3a 20 20 20 2f 2a 20 52 65 73 65 se 10: /* Rese
1b1e9 72 76 65 64 20 66 6f 72 20 66 75 74 75 72 65 20 rved for future
1b1ea 75 73 65 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 use */. case
1b1eb 31 31 3a 20 20 20 2f 2a 20 52 65 73 65 72 76 65 11: /* Reserve
1b1ec 64 20 66 6f 72 20 66 75 74 75 72 65 20 75 73 65 d for future use
1b1ed 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 30 3a 20 */. case 0:
1b1ee 7b 20 20 2f 2a 20 4e 55 4c 4c 20 2a 2f 0a 20 20 { /* NULL */.
1b1ef 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 pMem->flags
1b1f0 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 = MEM_Null;.
1b1f1 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
1b1f2 20 20 20 63 61 73 65 20 31 3a 20 7b 20 2f 2a 20 case 1: { /*
1b1f3 31 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e 1-byte signed in
1b1f4 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 teger */. p
1b1f5 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 73 69 67 6e Mem->u.i = (sign
1b1f6 65 64 20 63 68 61 72 29 62 75 66 5b 30 5d 3b 0a ed char)buf[0];.
1b1f7 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 pMem->flag
1b1f8 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 s = MEM_Int;.
1b1f9 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 return 1;.
1b1fa 20 7d 0a 20 20 20 20 63 61 73 65 20 32 3a 20 7b }. case 2: {
1b1fb 20 2f 2a 20 32 2d 62 79 74 65 20 73 69 67 6e 65 /* 2-byte signe
1b1fc 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 d integer */.
1b1fd 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 pMem->u.i = (
1b1fe 28 28 73 69 67 6e 65 64 20 63 68 61 72 29 62 75 ((signed char)bu
1b1ff 66 5b 30 5d 29 3c 3c 38 29 20 7c 20 62 75 66 5b f[0])<<8) | buf[
1b200 31 5d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 1];. pMem->
1b201 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b flags = MEM_Int;
1b202 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b . return 2;
1b203 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
1b204 33 3a 20 7b 20 2f 2a 20 33 2d 62 79 74 65 20 73 3: { /* 3-byte s
1b205 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f igned integer */
1b206 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 . pMem->u.i
1b207 20 3d 20 28 28 28 73 69 67 6e 65 64 20 63 68 61 = (((signed cha
1b208 72 29 62 75 66 5b 30 5d 29 3c 3c 31 36 29 20 7c r)buf[0])<<16) |
1b209 20 28 62 75 66 5b 31 5d 3c 3c 38 29 20 7c 20 62 (buf[1]<<8) | b
1b20a 75 66 5b 32 5d 3b 0a 20 20 20 20 20 20 70 4d 65 uf[2];. pMe
1b20b 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 m->flags = MEM_I
1b20c 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e nt;. return
1b20d 20 33 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 3;. }. ca
1b20e 73 65 20 34 3a 20 7b 20 2f 2a 20 34 2d 62 79 74 se 4: { /* 4-byt
1b20f 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 e signed integer
1b210 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e */. pMem->
1b211 75 2e 69 20 3d 20 28 62 75 66 5b 30 5d 3c 3c 32 u.i = (buf[0]<<2
1b212 34 29 20 7c 20 28 62 75 66 5b 31 5d 3c 3c 31 36 4) | (buf[1]<<16
1b213 29 20 7c 20 28 62 75 66 5b 32 5d 3c 3c 38 29 20 ) | (buf[2]<<8)
1b214 7c 20 62 75 66 5b 33 5d 3b 0a 20 20 20 20 20 20 | buf[3];.
1b215 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 pMem->flags = ME
1b216 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 M_Int;. ret
1b217 75 72 6e 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20 urn 4;. }.
1b218 20 63 61 73 65 20 35 3a 20 7b 20 2f 2a 20 36 2d case 5: { /* 6-
1b219 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 byte signed inte
1b21a 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 75 36 34 ger */. u64
1b21b 20 78 20 3d 20 28 28 28 73 69 67 6e 65 64 20 63 x = (((signed c
1b21c 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c 38 29 20 har)buf[0])<<8)
1b21d 7c 20 62 75 66 5b 31 5d 3b 0a 20 20 20 20 20 20 | buf[1];.
1b21e 75 33 32 20 79 20 3d 20 28 62 75 66 5b 32 5d 3c u32 y = (buf[2]<
1b21f 3c 32 34 29 20 7c 20 28 62 75 66 5b 33 5d 3c 3c <24) | (buf[3]<<
1b220 31 36 29 20 7c 20 28 62 75 66 5b 34 5d 3c 3c 38 16) | (buf[4]<<8
1b221 29 20 7c 20 62 75 66 5b 35 5d 3b 0a 20 20 20 20 ) | buf[5];.
1b222 20 20 78 20 3d 20 28 78 3c 3c 33 32 29 20 7c 20 x = (x<<32) |
1b223 79 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 y;. pMem->u
1b224 2e 69 20 3d 20 2a 28 69 36 34 2a 29 26 78 3b 0a .i = *(i64*)&x;.
1b225 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 pMem->flag
1b226 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 s = MEM_Int;.
1b227 20 20 20 72 65 74 75 72 6e 20 36 3b 0a 20 20 20 return 6;.
1b228 20 7d 0a 20 20 20 20 63 61 73 65 20 36 3a 20 20 }. case 6:
1b229 20 2f 2a 20 38 2d 62 79 74 65 20 73 69 67 6e 65 /* 8-byte signe
1b22a 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 d integer */.
1b22b 20 63 61 73 65 20 37 3a 20 7b 20 2f 2a 20 49 45 case 7: { /* IE
1b22c 45 45 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e EE floating poin
1b22d 74 20 2a 2f 0a 20 20 20 20 20 20 75 36 34 20 78 t */. u64 x
1b22e 3b 0a 20 20 20 20 20 20 75 33 32 20 79 3b 0a 23 ;. u32 y;.#
1b22f 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 if !defined(NDEB
1b230 55 47 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 UG) && !defined(
1b231 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 SQLITE_OMIT_FLOA
1b232 54 49 4e 47 5f 50 4f 49 4e 54 29 0a 20 20 20 20 TING_POINT).
1b233 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74 /* Verify that
1b234 20 69 6e 74 65 67 65 72 73 20 61 6e 64 20 66 6c integers and fl
1b235 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c oating point val
1b236 75 65 73 20 75 73 65 20 74 68 65 20 73 61 6d 65 ues use the same
1b237 0a 20 20 20 20 20 20 2a 2a 20 62 79 74 65 20 6f . ** byte o
1b238 72 64 65 72 2e 20 20 4f 72 2c 20 74 68 61 74 20 rder. Or, that
1b239 69 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 44 5f if SQLITE_MIXED_
1b23a 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f ENDIAN_64BIT_FLO
1b23b 41 54 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 64 AT is. ** d
1b23c 65 66 69 6e 65 64 20 74 68 61 74 20 36 34 2d 62 efined that 64-b
1b23d 69 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e it floating poin
1b23e 74 20 76 61 6c 75 65 73 20 72 65 61 6c 6c 79 20 t values really
1b23f 61 72 65 20 6d 69 78 65 64 0a 20 20 20 20 20 20 are mixed.
1b240 2a 2a 20 65 6e 64 69 61 6e 2e 0a 20 20 20 20 20 ** endian..
1b241 20 2a 2f 0a 20 20 20 20 20 20 73 74 61 74 69 63 */. static
1b242 20 63 6f 6e 73 74 20 75 36 34 20 74 31 20 3d 20 const u64 t1 =
1b243 28 28 75 36 34 29 30 78 33 66 66 30 30 30 30 30 ((u64)0x3ff00000
1b244 29 3c 3c 33 32 3b 0a 20 20 20 20 20 20 73 74 61 )<<32;. sta
1b245 74 69 63 20 63 6f 6e 73 74 20 64 6f 75 62 6c 65 tic const double
1b246 20 72 31 20 3d 20 31 2e 30 3b 0a 20 20 20 20 20 r1 = 1.0;.
1b247 20 75 36 34 20 74 32 20 3d 20 74 31 3b 0a 20 20 u64 t2 = t1;.
1b248 20 20 20 20 73 77 61 70 4d 69 78 65 64 45 6e 64 swapMixedEnd
1b249 69 61 6e 46 6c 6f 61 74 28 74 32 29 3b 0a 20 20 ianFloat(t2);.
1b24a 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 assert( size
1b24b 6f 66 28 72 31 29 3d 3d 73 69 7a 65 6f 66 28 74 of(r1)==sizeof(t
1b24c 32 29 20 26 26 20 6d 65 6d 63 6d 70 28 26 72 31 2) && memcmp(&r1
1b24d 2c 20 26 74 32 2c 20 73 69 7a 65 6f 66 28 72 31 , &t2, sizeof(r1
1b24e 29 29 3d 3d 30 20 29 3b 0a 23 65 6e 64 69 66 0a ))==0 );.#endif.
1b24f 0a 20 20 20 20 20 20 78 20 3d 20 28 62 75 66 5b . x = (buf[
1b250 30 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 31 0]<<24) | (buf[1
1b251 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 32 5d ]<<16) | (buf[2]
1b252 3c 3c 38 29 20 7c 20 62 75 66 5b 33 5d 3b 0a 20 <<8) | buf[3];.
1b253 20 20 20 20 20 79 20 3d 20 28 62 75 66 5b 34 5d y = (buf[4]
1b254 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 35 5d 3c <<24) | (buf[5]<
1b255 3c 31 36 29 20 7c 20 28 62 75 66 5b 36 5d 3c 3c <16) | (buf[6]<<
1b256 38 29 20 7c 20 62 75 66 5b 37 5d 3b 0a 20 20 20 8) | buf[7];.
1b257 20 20 20 78 20 3d 20 28 78 3c 3c 33 32 29 20 7c x = (x<<32) |
1b258 20 79 3b 0a 20 20 20 20 20 20 69 66 28 20 73 65 y;. if( se
1b259 72 69 61 6c 5f 74 79 70 65 3d 3d 36 20 29 7b 0a rial_type==6 ){.
1b25a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e pMem->u.
1b25b 69 20 3d 20 2a 28 69 36 34 2a 29 26 78 3b 0a 20 i = *(i64*)&x;.
1b25c 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 pMem->fla
1b25d 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 gs = MEM_Int;.
1b25e 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1b25f 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f assert( sizeo
1b260 66 28 78 29 3d 3d 38 20 26 26 20 73 69 7a 65 6f f(x)==8 && sizeo
1b261 66 28 70 4d 65 6d 2d 3e 72 29 3d 3d 38 20 29 3b f(pMem->r)==8 );
1b262 0a 20 20 20 20 20 20 20 20 73 77 61 70 4d 69 78 . swapMix
1b263 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 78 29 edEndianFloat(x)
1b264 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 ;. memcpy
1b265 28 26 70 4d 65 6d 2d 3e 72 2c 20 26 78 2c 20 73 (&pMem->r, &x, s
1b266 69 7a 65 6f 66 28 78 29 29 3b 0a 20 20 20 20 20 izeof(x));.
1b267 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d pMem->flags =
1b268 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 70 4d sqlite3IsNaN(pM
1b269 65 6d 2d 3e 72 29 20 3f 20 4d 45 4d 5f 4e 75 6c em->r) ? MEM_Nul
1b26a 6c 20 3a 20 4d 45 4d 5f 52 65 61 6c 3b 0a 20 20 l : MEM_Real;.
1b26b 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 }. retu
1b26c 72 6e 20 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20 rn 8;. }.
1b26d 63 61 73 65 20 38 3a 20 20 20 20 2f 2a 20 49 6e case 8: /* In
1b26e 74 65 67 65 72 20 30 20 2a 2f 0a 20 20 20 20 63 teger 0 */. c
1b26f 61 73 65 20 39 3a 20 7b 20 20 2f 2a 20 49 6e 74 ase 9: { /* Int
1b270 65 67 65 72 20 31 20 2a 2f 0a 20 20 20 20 20 20 eger 1 */.
1b271 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 73 65 72 69 pMem->u.i = seri
1b272 61 6c 5f 74 79 70 65 2d 38 3b 0a 20 20 20 20 20 al_type-8;.
1b273 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d pMem->flags = M
1b274 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 EM_Int;. re
1b275 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 turn 0;. }.
1b276 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 default: {.
1b277 20 20 20 75 33 32 20 6c 65 6e 20 3d 20 28 73 65 u32 len = (se
1b278 72 69 61 6c 5f 74 79 70 65 2d 31 32 29 2f 32 3b rial_type-12)/2;
1b279 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d . pMem->z =
1b27a 20 28 63 68 61 72 20 2a 29 62 75 66 3b 0a 20 20 (char *)buf;.
1b27b 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6c 65 pMem->n = le
1b27c 6e 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 78 n;. pMem->x
1b27d 44 65 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 Del = 0;. i
1b27e 66 28 20 73 65 72 69 61 6c 5f 74 79 70 65 26 30 f( serial_type&0
1b27f 78 30 31 20 29 7b 0a 20 20 20 20 20 20 20 20 70 x01 ){. p
1b280 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Mem->flags = MEM
1b281 5f 53 74 72 20 7c 20 4d 45 4d 5f 45 70 68 65 6d _Str | MEM_Ephem
1b282 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
1b283 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 pMem->fla
1b284 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 20 7c 20 gs = MEM_Blob |
1b285 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20 20 MEM_Ephem;.
1b286 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 }. return
1b287 6c 65 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 len;. }. }.
1b288 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f return 0;.}.../
1b289 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 6e *.** Given the n
1b28a 4b 65 79 2d 62 79 74 65 20 65 6e 63 6f 64 69 6e Key-byte encodin
1b28b 67 20 6f 66 20 61 20 72 65 63 6f 72 64 20 69 6e g of a record in
1b28c 20 70 4b 65 79 5b 5d 2c 20 70 61 72 73 65 20 74 pKey[], parse t
1b28d 68 65 0a 2a 2a 20 72 65 63 6f 72 64 20 69 6e 74 he.** record int
1b28e 6f 20 61 20 55 6e 70 61 63 6b 65 64 52 65 63 6f o a UnpackedReco
1b28f 72 64 20 73 74 72 75 63 74 75 72 65 2e 20 20 52 rd structure. R
1b290 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 eturn a pointer
1b291 74 6f 0a 2a 2a 20 74 68 61 74 20 73 74 72 75 63 to.** that struc
1b292 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ture..**.** The
1b293 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e calling function
1b294 20 6d 69 67 68 74 20 70 72 6f 76 69 64 65 20 73 might provide s
1b295 7a 53 70 61 63 65 20 62 79 74 65 73 20 6f 66 20 zSpace bytes of
1b296 6d 65 6d 6f 72 79 0a 2a 2a 20 73 70 61 63 65 20 memory.** space
1b297 61 74 20 70 53 70 61 63 65 2e 20 20 54 68 69 73 at pSpace. This
1b298 20 73 70 61 63 65 20 63 61 6e 20 62 65 20 75 73 space can be us
1b299 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 ed to hold the r
1b29a 65 74 75 72 6e 65 64 0a 2a 2a 20 56 44 62 65 50 eturned.** VDbeP
1b29b 61 72 73 65 64 52 65 63 6f 72 64 20 73 74 72 75 arsedRecord stru
1b29c 63 74 75 72 65 20 69 66 20 69 74 20 69 73 20 6c cture if it is l
1b29d 61 72 67 65 20 65 6e 6f 75 67 68 2e 20 20 49 66 arge enough. If
1b29e 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 62 69 it is.** not bi
1b29f 67 20 65 6e 6f 75 67 68 2c 20 73 70 61 63 65 20 g enough, space
1b2a0 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d is obtained from
1b2a1 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
1b2a2 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 )..**.** The ret
1b2a3 75 72 6e 65 64 20 73 74 72 75 63 74 75 72 65 20 urned structure
1b2a4 73 68 6f 75 6c 64 20 62 65 20 63 6c 6f 73 65 64 should be closed
1b2a5 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a by a call to.**
1b2a6 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 sqlite3VdbeDele
1b2a7 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 teUnpackedRecord
1b2a8 28 29 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 ()..*/ .SQLITE_P
1b2a9 52 49 56 41 54 45 20 55 6e 70 61 63 6b 65 64 52 RIVATE UnpackedR
1b2aa 65 63 6f 72 64 20 2a 73 71 6c 69 74 65 33 56 64 ecord *sqlite3Vd
1b2ab 62 65 52 65 63 6f 72 64 55 6e 70 61 63 6b 28 0a beRecordUnpack(.
1b2ac 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 KeyInfo *pKeyI
1b2ad 6e 66 6f 2c 20 20 20 20 20 2f 2a 20 49 6e 66 6f nfo, /* Info
1b2ae 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 rmation about th
1b2af 65 20 72 65 63 6f 72 64 20 66 6f 72 6d 61 74 20 e record format
1b2b0 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 20 20 */. int nKey,
1b2b1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
1b2b2 69 7a 65 20 6f 66 20 74 68 65 20 62 69 6e 61 72 ize of the binar
1b2b3 79 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63 6f y record */. co
1b2b4 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 nst void *pKey,
1b2b5 20 20 20 20 20 2f 2a 20 54 68 65 20 62 69 6e 61 /* The bina
1b2b6 72 79 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63 ry record */. c
1b2b7 68 61 72 20 2a 70 53 70 61 63 65 2c 20 20 20 20 har *pSpace,
1b2b8 20 20 20 20 20 20 2f 2a 20 55 6e 61 6c 69 67 6e /* Unalign
1b2b9 65 64 20 73 70 61 63 65 20 61 76 61 69 6c 61 62 ed space availab
1b2ba 6c 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6f le to hold the o
1b2bb 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 73 bject */. int s
1b2bc 7a 53 70 61 63 65 20 20 20 20 20 20 20 20 20 20 zSpace
1b2bd 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70 53 70 /* Size of pSp
1b2be 61 63 65 5b 5d 20 69 6e 20 62 79 74 65 73 20 2a ace[] in bytes *
1b2bf 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 /.){. const uns
1b2c0 69 67 6e 65 64 20 63 68 61 72 20 2a 61 4b 65 79 igned char *aKey
1b2c1 20 3d 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e = (const unsign
1b2c2 65 64 20 63 68 61 72 20 2a 29 70 4b 65 79 3b 0a ed char *)pKey;.
1b2c3 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 UnpackedRecord
1b2c4 20 2a 70 3b 20 20 2f 2a 20 54 68 65 20 75 6e 70 *p; /* The unp
1b2c5 61 63 6b 65 64 20 72 65 63 6f 72 64 20 74 68 61 acked record tha
1b2c6 74 20 77 65 20 77 69 6c 6c 20 72 65 74 75 72 6e t we will return
1b2c7 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b */. int nByte;
1b2c8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d /* Mem
1b2c9 6f 72 79 20 73 70 61 63 65 20 6e 65 65 64 65 64 ory space needed
1b2ca 20 74 6f 20 68 6f 6c 64 20 70 2c 20 69 6e 20 62 to hold p, in b
1b2cb 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 64 3b ytes */. int d;
1b2cc 0a 20 20 75 33 32 20 69 64 78 3b 0a 20 20 75 31 . u32 idx;. u1
1b2cd 36 20 75 3b 20 20 20 20 20 20 20 20 20 20 20 20 6 u;
1b2ce 20 20 2f 2a 20 55 6e 73 69 67 6e 65 64 20 6c 6f /* Unsigned lo
1b2cf 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 op counter */.
1b2d0 75 33 32 20 73 7a 48 64 72 3b 0a 20 20 4d 65 6d u32 szHdr;. Mem
1b2d1 20 2a 70 4d 65 6d 3b 0a 20 20 69 6e 74 20 6e 4f *pMem;. int nO
1b2d2 66 66 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a ff; /*
1b2d3 20 49 6e 63 72 65 61 73 65 20 70 53 70 61 63 65 Increase pSpace
1b2d4 20 62 79 20 74 68 69 73 20 6d 75 63 68 20 74 6f by this much to
1b2d5 20 38 2d 62 79 74 65 20 61 6c 69 67 6e 20 69 74 8-byte align it
1b2d6 20 2a 2f 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a */. . /*. **
1b2d7 20 57 65 20 77 61 6e 74 20 74 6f 20 73 68 69 66 We want to shif
1b2d8 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 70 53 t the pointer pS
1b2d9 70 61 63 65 20 75 70 20 73 75 63 68 20 74 68 61 pace up such tha
1b2da 74 20 69 74 20 69 73 20 38 2d 62 79 74 65 20 61 t it is 8-byte a
1b2db 6c 69 67 6e 65 64 2e 0a 20 20 2a 2a 20 54 68 75 ligned.. ** Thu
1b2dc 73 2c 20 77 65 20 6e 65 65 64 20 74 6f 20 63 61 s, we need to ca
1b2dd 6c 63 75 6c 61 74 65 20 61 20 76 61 6c 75 65 2c lculate a value,
1b2de 20 6e 4f 66 66 2c 20 62 65 74 77 65 65 6e 20 30 nOff, between 0
1b2df 20 61 6e 64 20 37 2c 20 74 6f 20 73 68 69 66 74 and 7, to shift
1b2e0 20 0a 20 20 2a 2a 20 69 74 20 62 79 2e 20 20 49 . ** it by. I
1b2e1 66 20 70 53 70 61 63 65 20 69 73 20 61 6c 72 65 f pSpace is alre
1b2e2 61 64 79 20 38 2d 62 79 74 65 20 61 6c 69 67 6e ady 8-byte align
1b2e3 65 64 2c 20 6e 4f 66 66 20 73 68 6f 75 6c 64 20 ed, nOff should
1b2e4 62 65 20 7a 65 72 6f 2e 0a 20 20 2a 2f 0a 20 20 be zero.. */.
1b2e5 6e 4f 66 66 20 3d 20 28 38 20 2d 20 28 53 51 4c nOff = (8 - (SQL
1b2e6 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 70 ITE_PTR_TO_INT(p
1b2e7 53 70 61 63 65 29 20 26 20 37 29 29 20 26 20 37 Space) & 7)) & 7
1b2e8 3b 0a 20 20 70 53 70 61 63 65 20 2b 3d 20 6e 4f ;. pSpace += nO
1b2e9 66 66 3b 0a 20 20 73 7a 53 70 61 63 65 20 2d 3d ff;. szSpace -=
1b2ea 20 6e 4f 66 66 3b 0a 20 20 6e 42 79 74 65 20 3d nOff;. nByte =
1b2eb 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 55 ROUND8(sizeof(U
1b2ec 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 29 29 20 npackedRecord))
1b2ed 2b 20 73 69 7a 65 6f 66 28 4d 65 6d 29 2a 28 70 + sizeof(Mem)*(p
1b2ee 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 2b KeyInfo->nField+
1b2ef 31 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3e 1);. if( nByte>
1b2f0 73 7a 53 70 61 63 65 20 29 7b 0a 20 20 20 20 70 szSpace ){. p
1b2f1 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
1b2f2 6f 63 52 61 77 28 70 4b 65 79 49 6e 66 6f 2d 3e ocRaw(pKeyInfo->
1b2f3 64 62 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 db, nByte);.
1b2f4 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 if( p==0 ) retur
1b2f5 6e 20 30 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 n 0;. p->flag
1b2f6 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 s = UNPACKED_NEE
1b2f7 44 5f 46 52 45 45 20 7c 20 55 4e 50 41 43 4b 45 D_FREE | UNPACKE
1b2f8 44 5f 4e 45 45 44 5f 44 45 53 54 52 4f 59 3b 0a D_NEED_DESTROY;.
1b2f9 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d }else{. p =
1b2fa 20 28 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 (UnpackedRecord
1b2fb 2a 29 70 53 70 61 63 65 3b 0a 20 20 20 20 70 2d *)pSpace;. p-
1b2fc 3e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b 45 >flags = UNPACKE
1b2fd 44 5f 4e 45 45 44 5f 44 45 53 54 52 4f 59 3b 0a D_NEED_DESTROY;.
1b2fe 20 20 7d 0a 20 20 70 2d 3e 70 4b 65 79 49 6e 66 }. p->pKeyInf
1b2ff 6f 20 3d 20 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 o = pKeyInfo;.
1b300 70 2d 3e 6e 46 69 65 6c 64 20 3d 20 70 4b 65 79 p->nField = pKey
1b301 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 2b 20 31 Info->nField + 1
1b302 3b 0a 20 20 70 2d 3e 61 4d 65 6d 20 3d 20 70 4d ;. p->aMem = pM
1b303 65 6d 20 3d 20 28 4d 65 6d 2a 29 26 28 28 63 68 em = (Mem*)&((ch
1b304 61 72 2a 29 70 29 5b 52 4f 55 4e 44 38 28 73 69 ar*)p)[ROUND8(si
1b305 7a 65 6f 66 28 55 6e 70 61 63 6b 65 64 52 65 63 zeof(UnpackedRec
1b306 6f 72 64 29 29 5d 3b 0a 20 20 61 73 73 65 72 74 ord))];. assert
1b307 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 ( EIGHT_BYTE_ALI
1b308 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a GNMENT(pMem) );.
1b309 20 20 69 64 78 20 3d 20 67 65 74 56 61 72 69 6e idx = getVarin
1b30a 74 33 32 28 61 4b 65 79 2c 20 73 7a 48 64 72 29 t32(aKey, szHdr)
1b30b 3b 0a 20 20 64 20 3d 20 73 7a 48 64 72 3b 0a 20 ;. d = szHdr;.
1b30c 20 75 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 u = 0;. while(
1b30d 20 69 64 78 3c 73 7a 48 64 72 20 26 26 20 75 3c idx<szHdr && u<
1b30e 70 2d 3e 6e 46 69 65 6c 64 20 26 26 20 64 3c 3d p->nField && d<=
1b30f 6e 4b 65 79 20 29 7b 0a 20 20 20 20 75 33 32 20 nKey ){. u32
1b310 73 65 72 69 61 6c 5f 74 79 70 65 3b 0a 0a 20 20 serial_type;..
1b311 20 20 69 64 78 20 2b 3d 20 67 65 74 56 61 72 69 idx += getVari
1b312 6e 74 33 32 28 26 61 4b 65 79 5b 69 64 78 5d 2c nt32(&aKey[idx],
1b313 20 73 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20 serial_type);.
1b314 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 70 pMem->enc = p
1b315 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 3b 0a 20 20 KeyInfo->enc;.
1b316 20 20 70 4d 65 6d 2d 3e 64 62 20 3d 20 70 4b 65 pMem->db = pKe
1b317 79 49 6e 66 6f 2d 3e 64 62 3b 0a 20 20 20 20 70 yInfo->db;. p
1b318 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 30 3b 0a Mem->flags = 0;.
1b319 20 20 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f pMem->zMallo
1b31a 63 20 3d 20 30 3b 0a 20 20 20 20 64 20 2b 3d 20 c = 0;. d +=
1b31b 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 sqlite3VdbeSeria
1b31c 6c 47 65 74 28 26 61 4b 65 79 5b 64 5d 2c 20 73 lGet(&aKey[d], s
1b31d 65 72 69 61 6c 5f 74 79 70 65 2c 20 70 4d 65 6d erial_type, pMem
1b31e 29 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20 );. pMem++;.
1b31f 20 20 20 75 2b 2b 3b 0a 20 20 7d 0a 20 20 61 73 u++;. }. as
1b320 73 65 72 74 28 20 75 3c 3d 70 4b 65 79 49 6e 66 sert( u<=pKeyInf
1b321 6f 2d 3e 6e 46 69 65 6c 64 20 2b 20 31 20 29 3b o->nField + 1 );
1b322 0a 20 20 70 2d 3e 6e 46 69 65 6c 64 20 3d 20 75 . p->nField = u
1b323 3b 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 ;. return (void
1b324 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 *)p;.}../*.** Th
1b325 69 73 20 72 6f 75 74 69 6e 65 20 64 65 73 74 72 is routine destr
1b326 6f 79 73 20 61 20 55 6e 70 61 63 6b 65 64 52 65 oys a UnpackedRe
1b327 63 6f 72 64 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a cord object..*/.
1b328 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1b329 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 oid sqlite3VdbeD
1b32a 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 eleteUnpackedRec
1b32b 6f 72 64 28 55 6e 70 61 63 6b 65 64 52 65 63 6f ord(UnpackedReco
1b32c 72 64 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b rd *p){. int i;
1b32d 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 0a 20 . Mem *pMem;..
1b32e 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b assert( p!=0 );
1b32f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 66 6c . assert( p->fl
1b330 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 4e ags & UNPACKED_N
1b331 45 45 44 5f 44 45 53 54 52 4f 59 20 29 3b 0a 20 EED_DESTROY );.
1b332 20 66 6f 72 28 69 3d 30 2c 20 70 4d 65 6d 3d 70 for(i=0, pMem=p
1b333 2d 3e 61 4d 65 6d 3b 20 69 3c 70 2d 3e 6e 46 69 ->aMem; i<p->nFi
1b334 65 6c 64 3b 20 69 2b 2b 2c 20 70 4d 65 6d 2b 2b eld; i++, pMem++
1b335 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 75 6e ){. /* The un
1b336 70 61 63 6b 65 64 20 72 65 63 6f 72 64 20 69 73 packed record is
1b337 20 61 6c 77 61 79 73 20 63 6f 6e 73 74 72 75 63 always construc
1b338 74 65 64 20 62 79 20 74 68 65 0a 20 20 20 20 2a ted by the. *
1b339 2a 20 73 71 6c 69 74 65 33 56 64 62 65 55 6e 70 * sqlite3VdbeUnp
1b33a 61 63 6b 52 65 63 6f 72 64 28 29 20 66 75 6e 63 ackRecord() func
1b33b 74 69 6f 6e 20 61 62 6f 76 65 2c 20 77 68 69 63 tion above, whic
1b33c 68 20 6d 61 6b 65 73 20 61 6c 6c 0a 20 20 20 20 h makes all.
1b33d 2a 2a 20 73 74 72 69 6e 67 73 20 61 6e 64 20 62 ** strings and b
1b33e 6c 6f 62 73 20 73 74 61 74 69 63 2e 20 20 41 6e lobs static. An
1b33f 64 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 65 6c d none of the el
1b340 65 6d 65 6e 74 73 20 61 72 65 0a 20 20 20 20 2a ements are. *
1b341 2a 20 65 76 65 72 20 74 72 61 6e 73 66 6f 72 6d * ever transform
1b342 65 64 2c 20 73 6f 20 74 68 65 72 65 20 69 73 20 ed, so there is
1b343 6e 65 76 65 72 20 61 6e 79 74 68 69 6e 67 20 74 never anything t
1b344 6f 20 64 65 6c 65 74 65 2e 0a 20 20 20 20 2a 2f o delete.. */
1b345 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 70 . if( NEVER(p
1b346 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 20 29 20 Mem->zMalloc) )
1b347 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
1b348 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 7d lease(pMem);. }
1b349 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 . if( p->flags
1b34a 26 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f & UNPACKED_NEED_
1b34b 46 52 45 45 20 29 7b 0a 20 20 20 20 73 71 6c 69 FREE ){. sqli
1b34c 74 65 33 44 62 46 72 65 65 28 70 2d 3e 70 4b 65 te3DbFree(p->pKe
1b34d 79 49 6e 66 6f 2d 3e 64 62 2c 20 70 29 3b 0a 20 yInfo->db, p);.
1b34e 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 }.}../*.** This
1b34f 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6d 70 61 72 function compar
1b350 65 73 20 74 68 65 20 74 77 6f 20 74 61 62 6c 65 es the two table
1b351 20 72 6f 77 73 20 6f 72 20 69 6e 64 65 78 20 72 rows or index r
1b352 65 63 6f 72 64 73 0a 2a 2a 20 73 70 65 63 69 66 ecords.** specif
1b353 69 65 64 20 62 79 20 7b 6e 4b 65 79 31 2c 20 70 ied by {nKey1, p
1b354 4b 65 79 31 7d 20 61 6e 64 20 70 50 4b 65 79 32 Key1} and pPKey2
1b355 2e 20 20 49 74 20 72 65 74 75 72 6e 73 20 61 20 . It returns a
1b356 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f 0a 2a negative, zero.*
1b357 2a 20 6f 72 20 70 6f 73 69 74 69 76 65 20 69 6e * or positive in
1b358 74 65 67 65 72 20 69 66 20 6b 65 79 31 20 69 73 teger if key1 is
1b359 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75 61 less than, equa
1b35a 6c 20 74 6f 20 6f 72 20 0a 2a 2a 20 67 72 65 61 l to or .** grea
1b35b 74 65 72 20 74 68 61 6e 20 6b 65 79 32 2e 20 20 ter than key2.
1b35c 54 68 65 20 7b 6e 4b 65 79 31 2c 20 70 4b 65 79 The {nKey1, pKey
1b35d 31 7d 20 6b 65 79 20 6d 75 73 74 20 62 65 20 61 1} key must be a
1b35e 20 62 6c 6f 62 0a 2a 2a 20 63 72 65 61 74 65 64 blob.** created
1b35f 20 62 79 20 74 68 20 4f 50 5f 4d 61 6b 65 52 65 by th OP_MakeRe
1b360 63 6f 72 64 20 6f 70 63 6f 64 65 20 6f 66 20 74 cord opcode of t
1b361 68 65 20 56 44 42 45 2e 20 20 54 68 65 20 70 50 he VDBE. The pP
1b362 4b 65 79 32 0a 2a 2a 20 6b 65 79 20 6d 75 73 74 Key2.** key must
1b363 20 62 65 20 61 20 70 61 72 73 65 64 20 6b 65 79 be a parsed key
1b364 20 73 75 63 68 20 61 73 20 6f 62 74 61 69 6e 65 such as obtaine
1b365 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 d from.** sqlite
1b366 33 56 64 62 65 50 61 72 73 65 52 65 63 6f 72 64 3VdbeParseRecord
1b367 2e 0a 2a 2a 0a 2a 2a 20 4b 65 79 31 20 61 6e 64 ..**.** Key1 and
1b368 20 4b 65 79 32 20 64 6f 20 6e 6f 74 20 68 61 76 Key2 do not hav
1b369 65 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 65 e to contain the
1b36a 20 73 61 6d 65 20 6e 75 6d 62 65 72 20 6f 66 20 same number of
1b36b 66 69 65 6c 64 73 2e 0a 2a 2a 20 54 68 65 20 6b fields..** The k
1b36c 65 79 20 77 69 74 68 20 66 65 77 65 72 20 66 69 ey with fewer fi
1b36d 65 6c 64 73 20 69 73 20 75 73 75 61 6c 6c 79 20 elds is usually
1b36e 63 6f 6d 70 61 72 65 73 20 6c 65 73 73 20 74 68 compares less th
1b36f 61 6e 20 74 68 65 20 0a 2a 2a 20 6c 6f 6e 67 65 an the .** longe
1b370 72 20 6b 65 79 2e 20 20 48 6f 77 65 76 65 72 20 r key. However
1b371 69 66 20 74 68 65 20 55 4e 50 41 43 4b 45 44 5f if the UNPACKED_
1b372 49 4e 43 52 4b 45 59 20 66 6c 61 67 73 20 69 6e INCRKEY flags in
1b373 20 70 50 4b 65 79 32 20 69 73 20 73 65 74 0a 2a pPKey2 is set.*
1b374 2a 20 61 6e 64 20 74 68 65 20 63 6f 6d 6d 6f 6e * and the common
1b375 20 70 72 65 66 69 78 65 73 20 61 72 65 20 65 71 prefixes are eq
1b376 75 61 6c 2c 20 74 68 65 6e 20 6b 65 79 31 20 69 ual, then key1 i
1b377 73 20 6c 65 73 73 20 74 68 61 6e 20 6b 65 79 32 s less than key2
1b378 2e 0a 2a 2a 20 4f 72 20 69 66 20 74 68 65 20 55 ..** Or if the U
1b379 4e 50 41 43 4b 45 44 5f 4d 41 54 43 48 5f 50 52 NPACKED_MATCH_PR
1b37a 45 46 49 58 20 66 6c 61 67 20 69 73 20 73 65 74 EFIX flag is set
1b37b 20 61 6e 64 20 74 68 65 20 70 72 65 66 69 78 65 and the prefixe
1b37c 73 20 61 72 65 0a 2a 2a 20 65 71 75 61 6c 2c 20 s are.** equal,
1b37d 74 68 65 6e 20 74 68 65 20 6b 65 79 73 20 61 72 then the keys ar
1b37e 65 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 e considered to
1b37f 62 65 20 65 71 75 61 6c 20 61 6e 64 0a 2a 2a 20 be equal and.**
1b380 74 68 65 20 70 61 72 74 73 20 62 65 79 6f 6e 64 the parts beyond
1b381 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 70 72 65 66 the common pref
1b382 69 78 20 61 72 65 20 69 67 6e 6f 72 65 64 2e 0a ix are ignored..
1b383 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 55 4e 50 **.** If the UNP
1b384 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 ACKED_IGNORE_ROW
1b385 49 44 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 ID flag is set,
1b386 74 68 65 6e 20 74 68 65 20 6c 61 73 74 20 62 79 then the last by
1b387 74 65 20 6f 66 0a 2a 2a 20 74 68 65 20 68 65 61 te of.** the hea
1b388 64 65 72 20 6f 66 20 70 4b 65 79 31 20 69 73 20 der of pKey1 is
1b389 69 67 6e 6f 72 65 64 2e 20 20 49 74 20 69 73 20 ignored. It is
1b38a 61 73 73 75 6d 65 64 20 74 68 61 74 20 70 4b 65 assumed that pKe
1b38b 79 31 20 69 73 0a 2a 2a 20 61 6e 20 69 6e 64 65 y1 is.** an inde
1b38c 78 20 6b 65 79 2c 20 61 6e 64 20 74 68 75 73 20 x key, and thus
1b38d 65 6e 64 73 20 77 69 74 68 20 61 20 72 6f 77 69 ends with a rowi
1b38e 64 20 76 61 6c 75 65 2e 20 20 54 68 65 20 6c 61 d value. The la
1b38f 73 74 20 62 79 74 65 0a 2a 2a 20 6f 66 20 74 68 st byte.** of th
1b390 65 20 68 65 61 64 65 72 20 77 69 6c 6c 20 74 68 e header will th
1b391 65 72 65 66 6f 72 65 20 62 65 20 74 68 65 20 73 erefore be the s
1b392 65 72 69 61 6c 20 74 79 70 65 20 6f 66 20 74 68 erial type of th
1b393 65 20 72 6f 77 69 64 3a 0a 2a 2a 20 6f 6e 65 20 e rowid:.** one
1b394 6f 66 20 31 2c 20 32 2c 20 33 2c 20 34 2c 20 35 of 1, 2, 3, 4, 5
1b395 2c 20 36 2c 20 38 2c 20 6f 72 20 39 20 2d 20 74 , 6, 8, or 9 - t
1b396 68 65 20 69 6e 74 65 67 65 72 20 73 65 72 69 61 he integer seria
1b397 6c 20 74 79 70 65 73 2e 0a 2a 2a 20 54 68 65 20 l types..** The
1b398 73 65 72 69 61 6c 20 74 79 70 65 20 6f 66 20 74 serial type of t
1b399 68 65 20 66 69 6e 61 6c 20 72 6f 77 69 64 20 77 he final rowid w
1b39a 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 20 61 20 ill always be a
1b39b 73 69 6e 67 6c 65 20 62 79 74 65 2e 0a 2a 2a 20 single byte..**
1b39c 42 79 20 69 67 6e 6f 72 69 6e 67 20 74 68 69 73 By ignoring this
1b39d 20 6c 61 73 74 20 62 79 74 65 20 6f 66 20 74 68 last byte of th
1b39e 65 20 68 65 61 64 65 72 2c 20 77 65 20 66 6f 72 e header, we for
1b39f 63 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f ce the compariso
1b3a0 6e 0a 2a 2a 20 74 6f 20 69 67 6e 6f 72 65 20 74 n.** to ignore t
1b3a1 68 65 20 72 6f 77 69 64 20 61 74 20 74 68 65 20 he rowid at the
1b3a2 65 6e 64 20 6f 66 20 6b 65 79 31 2e 0a 2a 2f 0a end of key1..*/.
1b3a3 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1b3a4 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 nt sqlite3VdbeRe
1b3a5 63 6f 72 64 43 6f 6d 70 61 72 65 28 0a 20 20 69 cordCompare(. i
1b3a6 6e 74 20 6e 4b 65 79 31 2c 20 63 6f 6e 73 74 20 nt nKey1, const
1b3a7 76 6f 69 64 20 2a 70 4b 65 79 31 2c 20 2f 2a 20 void *pKey1, /*
1b3a8 4c 65 66 74 20 6b 65 79 20 2a 2f 0a 20 20 55 6e Left key */. Un
1b3a9 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 50 packedRecord *pP
1b3aa 4b 65 79 32 20 20 20 20 20 20 20 20 2f 2a 20 52 Key2 /* R
1b3ab 69 67 68 74 20 6b 65 79 20 2a 2f 0a 29 7b 0a 20 ight key */.){.
1b3ac 20 69 6e 74 20 64 31 3b 20 20 20 20 20 20 20 20 int d1;
1b3ad 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e /* Offset in
1b3ae 74 6f 20 61 4b 65 79 5b 5d 20 6f 66 20 6e 65 78 to aKey[] of nex
1b3af 74 20 64 61 74 61 20 65 6c 65 6d 65 6e 74 20 2a t data element *
1b3b0 2f 0a 20 20 75 33 32 20 69 64 78 31 3b 20 20 20 /. u32 idx1;
1b3b1 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 /* Offset
1b3b2 20 69 6e 74 6f 20 61 4b 65 79 5b 5d 20 6f 66 20 into aKey[] of
1b3b3 6e 65 78 74 20 68 65 61 64 65 72 20 65 6c 65 6d next header elem
1b3b4 65 6e 74 20 2a 2f 0a 20 20 75 33 32 20 73 7a 48 ent */. u32 szH
1b3b5 64 72 31 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e dr1; /* N
1b3b6 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 umber of bytes i
1b3b7 6e 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 6e n header */. in
1b3b8 74 20 69 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e t i = 0;. int n
1b3b9 46 69 65 6c 64 3b 0a 20 20 69 6e 74 20 72 63 20 Field;. int rc
1b3ba 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 = 0;. const uns
1b3bb 69 67 6e 65 64 20 63 68 61 72 20 2a 61 4b 65 79 igned char *aKey
1b3bc 31 20 3d 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 1 = (const unsig
1b3bd 6e 65 64 20 63 68 61 72 20 2a 29 70 4b 65 79 31 ned char *)pKey1
1b3be 3b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 ;. KeyInfo *pKe
1b3bf 79 49 6e 66 6f 3b 0a 20 20 4d 65 6d 20 6d 65 6d yInfo;. Mem mem
1b3c0 31 3b 0a 0a 20 20 70 4b 65 79 49 6e 66 6f 20 3d 1;.. pKeyInfo =
1b3c1 20 70 50 4b 65 79 32 2d 3e 70 4b 65 79 49 6e 66 pPKey2->pKeyInf
1b3c2 6f 3b 0a 20 20 6d 65 6d 31 2e 65 6e 63 20 3d 20 o;. mem1.enc =
1b3c3 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 3b 0a 20 pKeyInfo->enc;.
1b3c4 20 6d 65 6d 31 2e 64 62 20 3d 20 70 4b 65 79 49 mem1.db = pKeyI
1b3c5 6e 66 6f 2d 3e 64 62 3b 0a 20 20 6d 65 6d 31 2e nfo->db;. mem1.
1b3c6 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 6d 65 6d flags = 0;. mem
1b3c7 31 2e 75 2e 69 20 3d 20 30 3b 20 20 2f 2a 20 6e 1.u.i = 0; /* n
1b3c8 6f 74 20 6e 65 65 64 65 64 2c 20 68 65 72 65 20 ot needed, here
1b3c9 74 6f 20 73 69 6c 65 6e 63 65 20 63 6f 6d 70 69 to silence compi
1b3ca 6c 65 72 20 77 61 72 6e 69 6e 67 20 2a 2f 0a 20 ler warning */.
1b3cb 20 6d 65 6d 31 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 mem1.zMalloc =
1b3cc 30 3b 0a 20 20 0a 20 20 69 64 78 31 20 3d 20 67 0;. . idx1 = g
1b3cd 65 74 56 61 72 69 6e 74 33 32 28 61 4b 65 79 31 etVarint32(aKey1
1b3ce 2c 20 73 7a 48 64 72 31 29 3b 0a 20 20 64 31 20 , szHdr1);. d1
1b3cf 3d 20 73 7a 48 64 72 31 3b 0a 20 20 69 66 28 20 = szHdr1;. if(
1b3d0 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20 26 20 pPKey2->flags &
1b3d1 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f UNPACKED_IGNORE_
1b3d2 52 4f 57 49 44 20 29 7b 0a 20 20 20 20 73 7a 48 ROWID ){. szH
1b3d3 64 72 31 2d 2d 3b 0a 20 20 7d 0a 20 20 6e 46 69 dr1--;. }. nFi
1b3d4 65 6c 64 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e eld = pKeyInfo->
1b3d5 6e 46 69 65 6c 64 3b 0a 20 20 77 68 69 6c 65 28 nField;. while(
1b3d6 20 69 64 78 31 3c 73 7a 48 64 72 31 20 26 26 20 idx1<szHdr1 &&
1b3d7 69 3c 70 50 4b 65 79 32 2d 3e 6e 46 69 65 6c 64 i<pPKey2->nField
1b3d8 20 29 7b 0a 20 20 20 20 75 33 32 20 73 65 72 69 ){. u32 seri
1b3d9 61 6c 5f 74 79 70 65 31 3b 0a 0a 20 20 20 20 2f al_type1;.. /
1b3da 2a 20 52 65 61 64 20 74 68 65 20 73 65 72 69 61 * Read the seria
1b3db 6c 20 74 79 70 65 73 20 66 6f 72 20 74 68 65 20 l types for the
1b3dc 6e 65 78 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20 next element in
1b3dd 65 61 63 68 20 6b 65 79 2e 20 2a 2f 0a 20 20 20 each key. */.
1b3de 20 69 64 78 31 20 2b 3d 20 67 65 74 56 61 72 69 idx1 += getVari
1b3df 6e 74 33 32 28 20 61 4b 65 79 31 2b 69 64 78 31 nt32( aKey1+idx1
1b3e0 2c 20 73 65 72 69 61 6c 5f 74 79 70 65 31 20 29 , serial_type1 )
1b3e1 3b 0a 20 20 20 20 69 66 28 20 64 31 3e 3d 6e 4b ;. if( d1>=nK
1b3e2 65 79 31 20 26 26 20 73 71 6c 69 74 65 33 56 64 ey1 && sqlite3Vd
1b3e3 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 beSerialTypeLen(
1b3e4 73 65 72 69 61 6c 5f 74 79 70 65 31 29 3e 30 20 serial_type1)>0
1b3e5 29 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 2f 2a ) break;.. /*
1b3e6 20 45 78 74 72 61 63 74 20 74 68 65 20 76 61 6c Extract the val
1b3e7 75 65 73 20 74 6f 20 62 65 20 63 6f 6d 70 61 72 ues to be compar
1b3e8 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 64 ed.. */. d
1b3e9 31 20 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 1 += sqlite3Vdbe
1b3ea 53 65 72 69 61 6c 47 65 74 28 26 61 4b 65 79 31 SerialGet(&aKey1
1b3eb 5b 64 31 5d 2c 20 73 65 72 69 61 6c 5f 74 79 70 [d1], serial_typ
1b3ec 65 31 2c 20 26 6d 65 6d 31 29 3b 0a 0a 20 20 20 e1, &mem1);..
1b3ed 20 2f 2a 20 44 6f 20 74 68 65 20 63 6f 6d 70 61 /* Do the compa
1b3ee 72 69 73 6f 6e 0a 20 20 20 20 2a 2f 0a 20 20 20 rison. */.
1b3ef 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d rc = sqlite3Mem
1b3f0 43 6f 6d 70 61 72 65 28 26 6d 65 6d 31 2c 20 26 Compare(&mem1, &
1b3f1 70 50 4b 65 79 32 2d 3e 61 4d 65 6d 5b 69 5d 2c pPKey2->aMem[i],
1b3f2 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1b3f3 20 20 20 20 20 20 20 20 20 20 20 20 69 3c 6e 46 i<nF
1b3f4 69 65 6c 64 20 3f 20 70 4b 65 79 49 6e 66 6f 2d ield ? pKeyInfo-
1b3f5 3e 61 43 6f 6c 6c 5b 69 5d 20 3a 20 30 29 3b 0a >aColl[i] : 0);.
1b3f6 20 20 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b if( rc!=0 ){
1b3f7 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1b3f8 20 20 7d 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d }. i++;. }
1b3f9 0a 0a 20 20 2f 2a 20 4e 6f 20 6d 65 6d 6f 72 79 .. /* No memory
1b3fa 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 65 allocation is e
1b3fb 76 65 72 20 75 73 65 64 20 6f 6e 20 6d 65 6d 31 ver used on mem1
1b3fc 2e 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 . */. if( NEVER
1b3fd 28 6d 65 6d 31 2e 7a 4d 61 6c 6c 6f 63 29 20 29 (mem1.zMalloc) )
1b3fe 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
1b3ff 65 6c 65 61 73 65 28 26 6d 65 6d 31 29 3b 0a 0a elease(&mem1);..
1b400 20 20 2f 2a 20 49 66 20 74 68 65 20 50 52 45 46 /* If the PREF
1b401 49 58 5f 53 45 41 52 43 48 20 66 6c 61 67 20 69 IX_SEARCH flag i
1b402 73 20 73 65 74 20 61 6e 64 20 61 6c 6c 20 66 69 s set and all fi
1b403 65 6c 64 73 20 65 78 63 65 70 74 20 74 68 65 20 elds except the
1b404 66 69 6e 61 6c 0a 20 20 2a 2a 20 72 6f 77 69 64 final. ** rowid
1b405 20 66 69 65 6c 64 20 77 65 72 65 20 65 71 75 61 field were equa
1b406 6c 2c 20 74 68 65 6e 20 63 6c 65 61 72 20 74 68 l, then clear th
1b407 65 20 50 52 45 46 49 58 5f 53 45 41 52 43 48 20 e PREFIX_SEARCH
1b408 66 6c 61 67 20 61 6e 64 20 73 65 74 20 0a 20 20 flag and set .
1b409 2a 2a 20 70 50 4b 65 79 32 2d 3e 72 6f 77 69 64 ** pPKey2->rowid
1b40a 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 to the value of
1b40b 20 74 68 65 20 72 6f 77 69 64 20 66 69 65 6c 64 the rowid field
1b40c 20 69 6e 20 28 70 4b 65 79 31 2c 20 6e 4b 65 79 in (pKey1, nKey
1b40d 31 29 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 73 1).. ** This is
1b40e 20 75 73 65 64 20 62 79 20 74 68 65 20 4f 50 5f used by the OP_
1b40f 49 73 55 6e 69 71 75 65 20 6f 70 63 6f 64 65 2e IsUnique opcode.
1b410 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 50 4b . */. if( (pPK
1b411 65 79 32 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 ey2->flags & UNP
1b412 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41 ACKED_PREFIX_SEA
1b413 52 43 48 29 20 26 26 20 69 3d 3d 28 70 50 4b 65 RCH) && i==(pPKe
1b414 79 32 2d 3e 6e 46 69 65 6c 64 2d 31 29 20 29 7b y2->nField-1) ){
1b415 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 64 78 . assert( idx
1b416 31 3d 3d 73 7a 48 64 72 31 20 26 26 20 72 63 20 1==szHdr1 && rc
1b417 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d );. assert( m
1b418 65 6d 31 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f em1.flags & MEM_
1b419 49 6e 74 20 29 3b 0a 20 20 20 20 70 50 4b 65 79 Int );. pPKey
1b41a 32 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 55 4e 50 2->flags &= ~UNP
1b41b 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41 ACKED_PREFIX_SEA
1b41c 52 43 48 3b 0a 20 20 20 20 70 50 4b 65 79 32 2d RCH;. pPKey2-
1b41d 3e 72 6f 77 69 64 20 3d 20 6d 65 6d 31 2e 75 2e >rowid = mem1.u.
1b41e 69 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 i;. }.. if( rc
1b41f 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 72 63 ==0 ){. /* rc
1b420 3d 3d 30 20 68 65 72 65 20 6d 65 61 6e 73 20 74 ==0 here means t
1b421 68 61 74 20 6f 6e 65 20 6f 66 20 74 68 65 20 6b hat one of the k
1b422 65 79 73 20 72 61 6e 20 6f 75 74 20 6f 66 20 66 eys ran out of f
1b423 69 65 6c 64 73 20 61 6e 64 0a 20 20 20 20 2a 2a ields and. **
1b424 20 61 6c 6c 20 74 68 65 20 66 69 65 6c 64 73 20 all the fields
1b425 75 70 20 74 6f 20 74 68 61 74 20 70 6f 69 6e 74 up to that point
1b426 20 77 65 72 65 20 65 71 75 61 6c 2e 20 49 66 20 were equal. If
1b427 74 68 65 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 the UNPACKED_INC
1b428 52 4b 45 59 0a 20 20 20 20 2a 2a 20 66 6c 61 67 RKEY. ** flag
1b429 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 62 72 is set, then br
1b42a 65 61 6b 20 74 68 65 20 74 69 65 20 62 79 20 74 eak the tie by t
1b42b 72 65 61 74 69 6e 67 20 6b 65 79 32 20 61 73 20 reating key2 as
1b42c 6c 61 72 67 65 72 2e 0a 20 20 20 20 2a 2a 20 49 larger.. ** I
1b42d 66 20 74 68 65 20 55 50 41 43 4b 45 44 5f 50 52 f the UPACKED_PR
1b42e 45 46 49 58 5f 4d 41 54 43 48 20 66 6c 61 67 20 EFIX_MATCH flag
1b42f 69 73 20 73 65 74 2c 20 74 68 65 6e 20 6b 65 79 is set, then key
1b430 73 20 77 69 74 68 20 63 6f 6d 6d 6f 6e 20 70 72 s with common pr
1b431 65 66 69 78 65 73 0a 20 20 20 20 2a 2a 20 61 72 efixes. ** ar
1b432 65 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 e considered to
1b433 62 65 20 65 71 75 61 6c 2e 20 20 4f 74 68 65 72 be equal. Other
1b434 77 69 73 65 2c 20 74 68 65 20 6c 6f 6e 67 65 72 wise, the longer
1b435 20 6b 65 79 20 69 73 20 74 68 65 20 0a 20 20 20 key is the .
1b436 20 2a 2a 20 6c 61 72 67 65 72 2e 20 20 41 73 20 ** larger. As
1b437 69 74 20 68 61 70 70 65 6e 73 2c 20 74 68 65 20 it happens, the
1b438 70 50 4b 65 79 32 20 77 69 6c 6c 20 61 6c 77 61 pPKey2 will alwa
1b439 79 73 20 62 65 20 74 68 65 20 6c 6f 6e 67 65 72 ys be the longer
1b43a 0a 20 20 20 20 2a 2a 20 69 66 20 74 68 65 72 65 . ** if there
1b43b 20 69 73 20 61 20 64 69 66 66 65 72 65 6e 63 65 is a difference
1b43c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
1b43d 20 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20 26 pPKey2->flags &
1b43e 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 UNPACKED_INCRKE
1b43f 59 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 Y ){. rc =
1b440 2d 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 -1;. }else if
1b441 28 20 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20 ( pPKey2->flags
1b442 26 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 & UNPACKED_PREFI
1b443 58 5f 4d 41 54 43 48 20 29 7b 0a 20 20 20 20 20 X_MATCH ){.
1b444 20 2f 2a 20 4c 65 61 76 65 20 72 63 3d 3d 30 20 /* Leave rc==0
1b445 2a 2f 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 */. }else if(
1b446 20 69 64 78 31 3c 73 7a 48 64 72 31 20 29 7b 0a idx1<szHdr1 ){.
1b447 20 20 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 rc = 1;.
1b448 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 }. }else if(
1b449 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f pKeyInfo->aSortO
1b44a 72 64 65 72 20 26 26 20 69 3c 70 4b 65 79 49 6e rder && i<pKeyIn
1b44b 66 6f 2d 3e 6e 46 69 65 6c 64 0a 20 20 20 20 20 fo->nField.
1b44c 20 20 20 20 20 20 20 20 20 20 26 26 20 70 4b 65 && pKe
1b44d 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 yInfo->aSortOrde
1b44e 72 5b 69 5d 20 29 7b 0a 20 20 20 20 72 63 20 3d r[i] ){. rc =
1b44f 20 2d 72 63 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 -rc;. }.. ret
1b450 75 72 6e 20 72 63 3b 0a 7d 0a 20 0a 0a 2f 2a 0a urn rc;.}. ../*.
1b451 2a 2a 20 70 43 75 72 20 70 6f 69 6e 74 73 20 61 ** pCur points a
1b452 74 20 61 6e 20 69 6e 64 65 78 20 65 6e 74 72 79 t an index entry
1b453 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 74 created using t
1b454 68 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 he OP_MakeRecord
1b455 20 6f 70 63 6f 64 65 2e 0a 2a 2a 20 52 65 61 64 opcode..** Read
1b456 20 74 68 65 20 72 6f 77 69 64 20 28 74 68 65 20 the rowid (the
1b457 6c 61 73 74 20 66 69 65 6c 64 20 69 6e 20 74 68 last field in th
1b458 65 20 72 65 63 6f 72 64 29 20 61 6e 64 20 73 74 e record) and st
1b459 6f 72 65 20 69 74 20 69 6e 20 2a 72 6f 77 69 64 ore it in *rowid
1b45a 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 ..** Return SQLI
1b45b 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68 TE_OK if everyth
1b45c 69 6e 67 20 77 6f 72 6b 73 2c 20 6f 72 20 61 6e ing works, or an
1b45d 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 74 68 65 error code othe
1b45e 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 70 43 75 rwise..**.** pCu
1b45f 72 20 6d 69 67 68 74 20 62 65 20 70 6f 69 6e 74 r might be point
1b460 69 6e 67 20 74 6f 20 74 65 78 74 20 6f 62 74 61 ing to text obta
1b461 69 6e 65 64 20 66 72 6f 6d 20 61 20 63 6f 72 72 ined from a corr
1b462 75 70 74 20 64 61 74 61 62 61 73 65 20 66 69 6c upt database fil
1b463 65 2e 0a 2a 2a 20 53 6f 20 74 68 65 20 63 6f 6e e..** So the con
1b464 74 65 6e 74 20 63 61 6e 6e 6f 74 20 62 65 20 74 tent cannot be t
1b465 72 75 73 74 65 64 2e 20 20 44 6f 20 61 70 70 72 rusted. Do appr
1b466 6f 70 72 69 61 74 65 20 63 68 65 63 6b 73 20 6f opriate checks o
1b467 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 2e 0a 2a n the content..*
1b468 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1b469 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
1b46a 49 64 78 52 6f 77 69 64 28 73 71 6c 69 74 65 33 IdxRowid(sqlite3
1b46b 20 2a 64 62 2c 20 42 74 43 75 72 73 6f 72 20 2a *db, BtCursor *
1b46c 70 43 75 72 2c 20 69 36 34 20 2a 72 6f 77 69 64 pCur, i64 *rowid
1b46d 29 7b 0a 20 20 69 36 34 20 6e 43 65 6c 6c 4b 65 ){. i64 nCellKe
1b46e 79 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b y = 0;. int rc;
1b46f 0a 20 20 75 33 32 20 73 7a 48 64 72 3b 20 20 20 . u32 szHdr;
1b470 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
1b471 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 the header */.
1b472 75 33 32 20 74 79 70 65 52 6f 77 69 64 3b 20 20 u32 typeRowid;
1b473 20 20 2f 2a 20 53 65 72 69 61 6c 20 74 79 70 65 /* Serial type
1b474 20 6f 66 20 74 68 65 20 72 6f 77 69 64 20 2a 2f of the rowid */
1b475 0a 20 20 75 33 32 20 6c 65 6e 52 6f 77 69 64 3b . u32 lenRowid;
1b476 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
1b477 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20 20 4d the rowid */. M
1b478 65 6d 20 6d 2c 20 76 3b 0a 0a 20 20 55 4e 55 53 em m, v;.. UNUS
1b479 45 44 5f 50 41 52 41 4d 45 54 45 52 28 64 62 29 ED_PARAMETER(db)
1b47a 3b 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68 65 20 ;.. /* Get the
1b47b 73 69 7a 65 20 6f 66 20 74 68 65 20 69 6e 64 65 size of the inde
1b47c 78 20 65 6e 74 72 79 2e 20 20 4f 6e 6c 79 20 69 x entry. Only i
1b47d 6e 64 69 63 65 73 20 65 6e 74 72 69 65 73 20 6f ndices entries o
1b47e 66 20 6c 65 73 73 0a 20 20 2a 2a 20 74 68 61 6e f less. ** than
1b47f 20 32 47 69 42 20 61 72 65 20 73 75 70 70 6f 72 2GiB are suppor
1b480 74 20 2d 20 61 6e 79 74 68 69 6e 67 20 6c 61 72 t - anything lar
1b481 67 65 20 6d 75 73 74 20 62 65 20 64 61 74 61 62 ge must be datab
1b482 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a ase corruption..
1b483 20 20 2a 2a 20 41 6e 79 20 63 6f 72 72 75 70 74 ** Any corrupt
1b484 69 6f 6e 20 69 73 20 64 65 74 65 63 74 65 64 20 ion is detected
1b485 69 6e 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 in sqlite3BtreeP
1b486 61 72 73 65 43 65 6c 6c 50 74 72 28 29 2c 20 74 arseCellPtr(), t
1b487 68 6f 75 67 68 2c 20 73 6f 0a 20 20 2a 2a 20 74 hough, so. ** t
1b488 68 69 73 20 63 6f 64 65 20 63 61 6e 20 73 61 66 his code can saf
1b489 65 6c 79 20 61 73 73 75 6d 65 20 74 68 61 74 20 ely assume that
1b48a 6e 43 65 6c 6c 4b 65 79 20 69 73 20 33 32 2d 62 nCellKey is 32-b
1b48b 69 74 73 20 20 0a 20 20 2a 2f 0a 20 20 61 73 73 its . */. ass
1b48c 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 ert( sqlite3Btre
1b48d 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28 70 eCursorIsValid(p
1b48e 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 Cur) );. rc = s
1b48f 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 qlite3BtreeKeySi
1b490 7a 65 28 70 43 75 72 2c 20 26 6e 43 65 6c 6c 4b ze(pCur, &nCellK
1b491 65 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 ey);. assert( r
1b492 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 c==SQLITE_OK );
1b493 20 20 20 20 2f 2a 20 70 43 75 72 20 69 73 20 61 /* pCur is a
1b494 6c 77 61 79 73 20 76 61 6c 69 64 20 73 6f 20 4b lways valid so K
1b495 65 79 53 69 7a 65 20 63 61 6e 6e 6f 74 20 66 61 eySize cannot fa
1b496 69 6c 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 il */. assert(
1b497 28 6e 43 65 6c 6c 4b 65 79 20 26 20 53 51 4c 49 (nCellKey & SQLI
1b498 54 45 5f 4d 41 58 5f 55 33 32 29 3d 3d 28 75 36 TE_MAX_U32)==(u6
1b499 34 29 6e 43 65 6c 6c 4b 65 79 20 29 3b 0a 0a 20 4)nCellKey );..
1b49a 20 2f 2a 20 52 65 61 64 20 69 6e 20 74 68 65 20 /* Read in the
1b49b 63 6f 6d 70 6c 65 74 65 20 63 6f 6e 74 65 6e 74 complete content
1b49c 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 65 6e of the index en
1b49d 74 72 79 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 try */. memset(
1b49e 26 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d 29 &m, 0, sizeof(m)
1b49f 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 );. rc = sqlite
1b4a0 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 3VdbeMemFromBtre
1b4a1 65 28 70 43 75 72 2c 20 30 2c 20 28 69 6e 74 29 e(pCur, 0, (int)
1b4a2 6e 43 65 6c 6c 4b 65 79 2c 20 31 2c 20 26 6d 29 nCellKey, 1, &m)
1b4a3 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 ;. if( rc ){.
1b4a4 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d return rc;. }
1b4a5 0a 0a 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 .. /* The index
1b4a6 20 65 6e 74 72 79 20 6d 75 73 74 20 62 65 67 69 entry must begi
1b4a7 6e 20 77 69 74 68 20 61 20 68 65 61 64 65 72 20 n with a header
1b4a8 73 69 7a 65 20 2a 2f 0a 20 20 28 76 6f 69 64 29 size */. (void)
1b4a9 67 65 74 56 61 72 69 6e 74 33 32 28 28 75 38 2a getVarint32((u8*
1b4aa 29 6d 2e 7a 2c 20 73 7a 48 64 72 29 3b 0a 20 20 )m.z, szHdr);.
1b4ab 74 65 73 74 63 61 73 65 28 20 73 7a 48 64 72 3d testcase( szHdr=
1b4ac 3d 33 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 =3 );. testcase
1b4ad 28 20 73 7a 48 64 72 3d 3d 6d 2e 6e 20 29 3b 0a ( szHdr==m.n );.
1b4ae 20 20 69 66 28 20 75 6e 6c 69 6b 65 6c 79 28 73 if( unlikely(s
1b4af 7a 48 64 72 3c 33 20 7c 7c 20 28 69 6e 74 29 73 zHdr<3 || (int)s
1b4b0 7a 48 64 72 3e 6d 2e 6e 29 20 29 7b 0a 20 20 20 zHdr>m.n) ){.
1b4b1 20 67 6f 74 6f 20 69 64 78 5f 72 6f 77 69 64 5f goto idx_rowid_
1b4b2 63 6f 72 72 75 70 74 69 6f 6e 3b 0a 20 20 7d 0a corruption;. }.
1b4b3 0a 20 20 2f 2a 20 54 68 65 20 6c 61 73 74 20 66 . /* The last f
1b4b4 69 65 6c 64 20 6f 66 20 74 68 65 20 69 6e 64 65 ield of the inde
1b4b5 78 20 73 68 6f 75 6c 64 20 62 65 20 61 6e 20 69 x should be an i
1b4b6 6e 74 65 67 65 72 20 2d 20 74 68 65 20 52 4f 57 nteger - the ROW
1b4b7 49 44 2e 0a 20 20 2a 2a 20 56 65 72 69 66 79 20 ID.. ** Verify
1b4b8 74 68 61 74 20 74 68 65 20 6c 61 73 74 20 65 6e that the last en
1b4b9 74 72 79 20 72 65 61 6c 6c 79 20 69 73 20 61 6e try really is an
1b4ba 20 69 6e 74 65 67 65 72 2e 20 2a 2f 0a 20 20 28 integer. */. (
1b4bb 76 6f 69 64 29 67 65 74 56 61 72 69 6e 74 33 32 void)getVarint32
1b4bc 28 28 75 38 2a 29 26 6d 2e 7a 5b 73 7a 48 64 72 ((u8*)&m.z[szHdr
1b4bd 2d 31 5d 2c 20 74 79 70 65 52 6f 77 69 64 29 3b -1], typeRowid);
1b4be 0a 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70 . testcase( typ
1b4bf 65 52 6f 77 69 64 3d 3d 31 20 29 3b 0a 20 20 74 eRowid==1 );. t
1b4c0 65 73 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 estcase( typeRow
1b4c1 69 64 3d 3d 32 20 29 3b 0a 20 20 74 65 73 74 63 id==2 );. testc
1b4c2 61 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d ase( typeRowid==
1b4c3 33 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 3 );. testcase(
1b4c4 20 74 79 70 65 52 6f 77 69 64 3d 3d 34 20 29 3b typeRowid==4 );
1b4c5 0a 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70 . testcase( typ
1b4c6 65 52 6f 77 69 64 3d 3d 35 20 29 3b 0a 20 20 74 eRowid==5 );. t
1b4c7 65 73 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 estcase( typeRow
1b4c8 69 64 3d 3d 36 20 29 3b 0a 20 20 74 65 73 74 63 id==6 );. testc
1b4c9 61 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d ase( typeRowid==
1b4ca 38 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 8 );. testcase(
1b4cb 20 74 79 70 65 52 6f 77 69 64 3d 3d 39 20 29 3b typeRowid==9 );
1b4cc 0a 20 20 69 66 28 20 75 6e 6c 69 6b 65 6c 79 28 . if( unlikely(
1b4cd 74 79 70 65 52 6f 77 69 64 3c 31 20 7c 7c 20 74 typeRowid<1 || t
1b4ce 79 70 65 52 6f 77 69 64 3e 39 20 7c 7c 20 74 79 ypeRowid>9 || ty
1b4cf 70 65 52 6f 77 69 64 3d 3d 37 29 20 29 7b 0a 20 peRowid==7) ){.
1b4d0 20 20 20 67 6f 74 6f 20 69 64 78 5f 72 6f 77 69 goto idx_rowi
1b4d1 64 5f 63 6f 72 72 75 70 74 69 6f 6e 3b 0a 20 20 d_corruption;.
1b4d2 7d 0a 20 20 6c 65 6e 52 6f 77 69 64 20 3d 20 73 }. lenRowid = s
1b4d3 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c qlite3VdbeSerial
1b4d4 54 79 70 65 4c 65 6e 28 74 79 70 65 52 6f 77 69 TypeLen(typeRowi
1b4d5 64 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 d);. testcase(
1b4d6 28 75 33 32 29 6d 2e 6e 3d 3d 73 7a 48 64 72 2b (u32)m.n==szHdr+
1b4d7 6c 65 6e 52 6f 77 69 64 20 29 3b 0a 20 20 69 66 lenRowid );. if
1b4d8 28 20 75 6e 6c 69 6b 65 6c 79 28 28 75 33 32 29 ( unlikely((u32)
1b4d9 6d 2e 6e 3c 73 7a 48 64 72 2b 6c 65 6e 52 6f 77 m.n<szHdr+lenRow
1b4da 69 64 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 id) ){. goto
1b4db 69 64 78 5f 72 6f 77 69 64 5f 63 6f 72 72 75 70 idx_rowid_corrup
1b4dc 74 69 6f 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 tion;. }.. /*
1b4dd 46 65 74 63 68 20 74 68 65 20 69 6e 74 65 67 65 Fetch the intege
1b4de 72 20 6f 66 66 20 74 68 65 20 65 6e 64 20 6f 66 r off the end of
1b4df 20 74 68 65 20 69 6e 64 65 78 20 72 65 63 6f 72 the index recor
1b4e0 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 d */. sqlite3Vd
1b4e1 62 65 53 65 72 69 61 6c 47 65 74 28 28 75 38 2a beSerialGet((u8*
1b4e2 29 26 6d 2e 7a 5b 6d 2e 6e 2d 6c 65 6e 52 6f 77 )&m.z[m.n-lenRow
1b4e3 69 64 5d 2c 20 74 79 70 65 52 6f 77 69 64 2c 20 id], typeRowid,
1b4e4 26 76 29 3b 0a 20 20 2a 72 6f 77 69 64 20 3d 20 &v);. *rowid =
1b4e5 76 2e 75 2e 69 3b 0a 20 20 73 71 6c 69 74 65 33 v.u.i;. sqlite3
1b4e6 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 VdbeMemRelease(&
1b4e7 6d 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c m);. return SQL
1b4e8 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 4a 75 ITE_OK;.. /* Ju
1b4e9 6d 70 20 68 65 72 65 20 69 66 20 64 61 74 61 62 mp here if datab
1b4ea 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 ase corruption i
1b4eb 73 20 64 65 74 65 63 74 65 64 20 61 66 74 65 72 s detected after
1b4ec 20 6d 20 68 61 73 20 62 65 65 6e 0a 20 20 2a 2a m has been. **
1b4ed 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 46 72 65 allocated. Fre
1b4ee 65 20 74 68 65 20 6d 20 6f 62 6a 65 63 74 20 61 e the m object a
1b4ef 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 nd return SQLITE
1b4f0 5f 43 4f 52 52 55 50 54 2e 20 2a 2f 0a 69 64 78 _CORRUPT. */.idx
1b4f1 5f 72 6f 77 69 64 5f 63 6f 72 72 75 70 74 69 6f _rowid_corruptio
1b4f2 6e 3a 0a 20 20 74 65 73 74 63 61 73 65 28 20 6d n:. testcase( m
1b4f3 2e 7a 4d 61 6c 6c 6f 63 21 3d 30 20 29 3b 0a 20 .zMalloc!=0 );.
1b4f4 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
1b4f5 65 6c 65 61 73 65 28 26 6d 29 3b 0a 20 20 72 65 elease(&m);. re
1b4f6 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
1b4f7 55 50 54 5f 42 4b 50 54 3b 0a 7d 0a 0a 2f 2a 0a UPT_BKPT;.}../*.
1b4f8 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20 6b ** Compare the k
1b4f9 65 79 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 ey of the index
1b4fa 65 6e 74 72 79 20 74 68 61 74 20 63 75 72 73 6f entry that curso
1b4fb 72 20 70 43 20 69 73 20 70 6f 69 6e 74 69 6e 67 r pC is pointing
1b4fc 20 74 6f 20 61 67 61 69 6e 73 74 0a 2a 2a 20 74 to against.** t
1b4fd 68 65 20 6b 65 79 20 73 74 72 69 6e 67 20 69 6e he key string in
1b4fe 20 70 55 6e 70 61 63 6b 65 64 2e 20 20 57 72 69 pUnpacked. Wri
1b4ff 74 65 20 69 6e 74 6f 20 2a 70 52 65 73 20 61 20 te into *pRes a
1b500 6e 75 6d 62 65 72 0a 2a 2a 20 74 68 61 74 20 69 number.** that i
1b501 73 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f s negative, zero
1b502 2c 20 6f 72 20 70 6f 73 69 74 69 76 65 20 69 66 , or positive if
1b503 20 70 43 20 69 73 20 6c 65 73 73 20 74 68 61 6e pC is less than
1b504 2c 20 65 71 75 61 6c 20 74 6f 2c 0a 2a 2a 20 6f , equal to,.** o
1b505 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 70 r greater than p
1b506 55 6e 70 61 63 6b 65 64 2e 20 20 52 65 74 75 72 Unpacked. Retur
1b507 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 n SQLITE_OK on s
1b508 75 63 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 70 55 uccess..**.** pU
1b509 6e 70 61 63 6b 65 64 20 69 73 20 65 69 74 68 65 npacked is eithe
1b50a 72 20 63 72 65 61 74 65 64 20 77 69 74 68 6f 75 r created withou
1b50b 74 20 61 20 72 6f 77 69 64 20 6f 72 20 69 73 20 t a rowid or is
1b50c 74 72 75 6e 63 61 74 65 64 20 73 6f 20 74 68 61 truncated so tha
1b50d 74 20 69 74 0a 2a 2a 20 6f 6d 69 74 73 20 74 68 t it.** omits th
1b50e 65 20 72 6f 77 69 64 20 61 74 20 74 68 65 20 65 e rowid at the e
1b50f 6e 64 2e 20 20 54 68 65 20 72 6f 77 69 64 20 61 nd. The rowid a
1b510 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 t the end of the
1b511 20 69 6e 64 65 78 20 65 6e 74 72 79 0a 2a 2a 20 index entry.**
1b512 69 73 20 69 67 6e 6f 72 65 64 20 61 73 20 77 65 is ignored as we
1b513 6c 6c 2e 20 20 48 65 6e 63 65 2c 20 74 68 69 73 ll. Hence, this
1b514 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 63 6f routine only co
1b515 6d 70 61 72 65 73 20 74 68 65 20 70 72 65 66 69 mpares the prefi
1b516 78 65 73 20 0a 2a 2a 20 6f 66 20 74 68 65 20 6b xes .** of the k
1b517 65 79 73 20 70 72 69 6f 72 20 74 6f 20 74 68 65 eys prior to the
1b518 20 66 69 6e 61 6c 20 72 6f 77 69 64 2c 20 6e 6f final rowid, no
1b519 74 20 74 68 65 20 65 6e 74 69 72 65 20 6b 65 79 t the entire key
1b51a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1b51b 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
1b51c 64 62 65 49 64 78 4b 65 79 43 6f 6d 70 61 72 65 dbeIdxKeyCompare
1b51d 28 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a (. VdbeCursor *
1b51e 70 43 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 pC,
1b51f 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 74 6f /* The cursor to
1b520 20 63 6f 6d 70 61 72 65 20 61 67 61 69 6e 73 74 compare against
1b521 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 */. UnpackedRe
1b522 63 6f 72 64 20 2a 70 55 6e 70 61 63 6b 65 64 2c cord *pUnpacked,
1b523 20 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20 76 65 /* Unpacked ve
1b524 72 73 69 6f 6e 20 6f 66 20 6b 65 79 20 74 6f 20 rsion of key to
1b525 63 6f 6d 70 61 72 65 20 61 67 61 69 6e 73 74 20 compare against
1b526 2a 2f 0a 20 20 69 6e 74 20 2a 72 65 73 20 20 20 */. int *res
1b527 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b528 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f /* Write the co
1b529 6d 70 61 72 69 73 6f 6e 20 72 65 73 75 6c 74 20 mparison result
1b52a 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 36 34 here */.){. i64
1b52b 20 6e 43 65 6c 6c 4b 65 79 20 3d 20 30 3b 0a 20 nCellKey = 0;.
1b52c 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 43 75 72 int rc;. BtCur
1b52d 73 6f 72 20 2a 70 43 75 72 20 3d 20 70 43 2d 3e sor *pCur = pC->
1b52e 70 43 75 72 73 6f 72 3b 0a 20 20 4d 65 6d 20 6d pCursor;. Mem m
1b52f 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
1b530 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 49 ite3BtreeCursorI
1b531 73 56 61 6c 69 64 28 70 43 75 72 29 20 29 3b 0a sValid(pCur) );.
1b532 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1b533 72 65 65 4b 65 79 53 69 7a 65 28 70 43 75 72 2c reeKeySize(pCur,
1b534 20 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 61 &nCellKey);. a
1b535 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 ssert( rc==SQLIT
1b536 45 5f 4f 4b 20 29 3b 20 20 20 20 2f 2a 20 70 43 E_OK ); /* pC
1b537 75 72 20 69 73 20 61 6c 77 61 79 73 20 76 61 6c ur is always val
1b538 69 64 20 73 6f 20 4b 65 79 53 69 7a 65 20 63 61 id so KeySize ca
1b539 6e 6e 6f 74 20 66 61 69 6c 20 2a 2f 0a 20 20 2f nnot fail */. /
1b53a 2a 20 6e 43 65 6c 6c 4b 65 79 20 77 69 6c 6c 20 * nCellKey will
1b53b 61 6c 77 61 79 73 20 62 65 20 62 65 74 77 65 65 always be betwee
1b53c 6e 20 30 20 61 6e 64 20 30 78 66 66 66 66 66 66 n 0 and 0xffffff
1b53d 66 66 20 62 65 63 61 75 73 65 20 6f 66 20 74 68 ff because of th
1b53e 65 20 73 61 79 0a 20 20 2a 2a 20 74 68 61 74 20 e say. ** that
1b53f 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 btreeParseCellPt
1b540 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 47 r() and sqlite3G
1b541 65 74 56 61 72 69 6e 74 33 32 28 29 20 61 72 65 etVarint32() are
1b542 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 2a 2f 0a implemented */.
1b543 20 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3c 3d if( nCellKey<=
1b544 30 20 7c 7c 20 6e 43 65 6c 6c 4b 65 79 3e 30 78 0 || nCellKey>0x
1b545 37 66 66 66 66 66 66 66 20 29 7b 0a 20 20 20 20 7fffffff ){.
1b546 2a 72 65 73 20 3d 20 30 3b 0a 20 20 20 20 72 65 *res = 0;. re
1b547 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
1b548 55 50 54 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 UPT;. }. memse
1b549 74 28 26 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 t(&m, 0, sizeof(
1b54a 6d 29 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 m));. rc = sqli
1b54b 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 te3VdbeMemFromBt
1b54c 72 65 65 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c ree(pC->pCursor,
1b54d 20 30 2c 20 28 69 6e 74 29 6e 43 65 6c 6c 4b 65 0, (int)nCellKe
1b54e 79 2c 20 31 2c 20 26 6d 29 3b 0a 20 20 69 66 28 y, 1, &m);. if(
1b54f 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 rc ){. retur
1b550 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 n rc;. }. asse
1b551 72 74 28 20 70 55 6e 70 61 63 6b 65 64 2d 3e 66 rt( pUnpacked->f
1b552 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f lags & UNPACKED_
1b553 49 47 4e 4f 52 45 5f 52 4f 57 49 44 20 29 3b 0a IGNORE_ROWID );.
1b554 20 20 2a 72 65 73 20 3d 20 73 71 6c 69 74 65 33 *res = sqlite3
1b555 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 VdbeRecordCompar
1b556 65 28 6d 2e 6e 2c 20 6d 2e 7a 2c 20 70 55 6e 70 e(m.n, m.z, pUnp
1b557 61 63 6b 65 64 29 3b 0a 20 20 73 71 6c 69 74 65 acked);. sqlite
1b558 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 3VdbeMemRelease(
1b559 26 6d 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 &m);. return SQ
1b55a 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
1b55b 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 * This routine s
1b55c 65 74 73 20 74 68 65 20 76 61 6c 75 65 20 74 6f ets the value to
1b55d 20 62 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 be returned by
1b55e 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 subsequent calls
1b55f 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 to.** sqlite3_c
1b560 68 61 6e 67 65 73 28 29 20 6f 6e 20 74 68 65 20 hanges() on the
1b561 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 database handle
1b562 27 64 62 27 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 'db'. .*/.SQLITE
1b563 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1b564 6c 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e lite3VdbeSetChan
1b565 67 65 73 28 73 71 6c 69 74 65 33 20 2a 64 62 2c ges(sqlite3 *db,
1b566 20 69 6e 74 20 6e 43 68 61 6e 67 65 29 7b 0a 20 int nChange){.
1b567 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1b568 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e _mutex_held(db->
1b569 6d 75 74 65 78 29 20 29 3b 0a 20 20 64 62 2d 3e mutex) );. db->
1b56a 6e 43 68 61 6e 67 65 20 3d 20 6e 43 68 61 6e 67 nChange = nChang
1b56b 65 3b 0a 20 20 64 62 2d 3e 6e 54 6f 74 61 6c 43 e;. db->nTotalC
1b56c 68 61 6e 67 65 20 2b 3d 20 6e 43 68 61 6e 67 65 hange += nChange
1b56d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 61 ;.}../*.** Set a
1b56e 20 66 6c 61 67 20 69 6e 20 74 68 65 20 76 64 62 flag in the vdb
1b56f 65 20 74 6f 20 75 70 64 61 74 65 20 74 68 65 20 e to update the
1b570 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 77 change counter w
1b571 68 65 6e 20 69 74 20 69 73 20 66 69 6e 61 6c 69 hen it is finali
1b572 73 65 64 0a 2a 2a 20 6f 72 20 72 65 73 65 74 2e sed.** or reset.
1b573 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1b574 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
1b575 64 62 65 43 6f 75 6e 74 43 68 61 6e 67 65 73 28 dbeCountChanges(
1b576 56 64 62 65 20 2a 76 29 7b 0a 20 20 76 2d 3e 63 Vdbe *v){. v->c
1b577 68 61 6e 67 65 43 6e 74 4f 6e 20 3d 20 31 3b 0a hangeCntOn = 1;.
1b578 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 65 76 }../*.** Mark ev
1b579 65 72 79 20 70 72 65 70 61 72 65 64 20 73 74 61 ery prepared sta
1b57a 74 65 6d 65 6e 74 20 61 73 73 6f 63 69 61 74 65 tement associate
1b57b 64 20 77 69 74 68 20 61 20 64 61 74 61 62 61 73 d with a databas
1b57c 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 e connection.**
1b57d 61 73 20 65 78 70 69 72 65 64 2e 0a 2a 2a 0a 2a as expired..**.*
1b57e 2a 20 41 6e 20 65 78 70 69 72 65 64 20 73 74 61 * An expired sta
1b57f 74 65 6d 65 6e 74 20 6d 65 61 6e 73 20 74 68 61 tement means tha
1b580 74 20 72 65 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 t recompilation
1b581 6f 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 of the statement
1b582 20 69 73 0a 2a 2a 20 72 65 63 6f 6d 6d 65 6e 64 is.** recommend
1b583 2e 20 20 53 74 61 74 65 6d 65 6e 74 73 20 65 78 . Statements ex
1b584 70 69 72 65 20 77 68 65 6e 20 74 68 69 6e 67 73 pire when things
1b585 20 68 61 70 70 65 6e 20 74 68 61 74 20 6d 61 6b happen that mak
1b586 65 20 74 68 65 69 72 0a 2a 2a 20 70 72 6f 67 72 e their.** progr
1b587 61 6d 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20 52 ams obsolete. R
1b588 65 6d 6f 76 69 6e 67 20 75 73 65 72 2d 64 65 66 emoving user-def
1b589 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 20 6f ined functions o
1b58a 72 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73 r collating.** s
1b58b 65 71 75 65 6e 63 65 73 2c 20 6f 72 20 63 68 61 equences, or cha
1b58c 6e 67 69 6e 67 20 61 6e 20 61 75 74 68 6f 72 69 nging an authori
1b58d 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 zation function
1b58e 61 72 65 20 74 68 65 20 74 79 70 65 73 20 6f 66 are the types of
1b58f 0a 2a 2a 20 74 68 69 6e 67 73 20 74 68 61 74 20 .** things that
1b590 6d 61 6b 65 20 70 72 65 70 61 72 65 64 20 73 74 make prepared st
1b591 61 74 65 6d 65 6e 74 73 20 6f 62 73 6f 6c 65 74 atements obsolet
1b592 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
1b593 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
1b594 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64 53 3ExpirePreparedS
1b595 74 61 74 65 6d 65 6e 74 73 28 73 71 6c 69 74 65 tatements(sqlite
1b596 33 20 2a 64 62 29 7b 0a 20 20 56 64 62 65 20 2a 3 *db){. Vdbe *
1b597 70 3b 0a 20 20 66 6f 72 28 70 20 3d 20 64 62 2d p;. for(p = db-
1b598 3e 70 56 64 62 65 3b 20 70 3b 20 70 3d 70 2d 3e >pVdbe; p; p=p->
1b599 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d 3e 65 pNext){. p->e
1b59a 78 70 69 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a xpired = 1;. }.
1b59b 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
1b59c 74 68 65 20 64 61 74 61 62 61 73 65 20 61 73 73 the database ass
1b59d 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 ociated with the
1b59e 20 56 64 62 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 Vdbe..*/.SQLITE
1b59f 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 _PRIVATE sqlite3
1b5a0 20 2a 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 *sqlite3VdbeDb(
1b5a1 56 64 62 65 20 2a 76 29 7b 0a 20 20 72 65 74 75 Vdbe *v){. retu
1b5a2 72 6e 20 76 2d 3e 64 62 3b 0a 7d 0a 0a 2f 2a 0a rn v->db;.}../*.
1b5a3 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e ** Return a poin
1b5a4 74 65 72 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 ter to an sqlite
1b5a5 33 5f 76 61 6c 75 65 20 73 74 72 75 63 74 75 72 3_value structur
1b5a6 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 e containing the
1b5a7 20 76 61 6c 75 65 20 62 6f 75 6e 64 0a 2a 2a 20 value bound.**
1b5a8 70 61 72 61 6d 65 74 65 72 20 69 56 61 72 20 6f parameter iVar o
1b5a9 66 20 56 4d 20 76 2e 20 45 78 63 65 70 74 2c 20 f VM v. Except,
1b5aa 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 if the value is
1b5ab 61 6e 20 53 51 4c 20 4e 55 4c 4c 2c 20 72 65 74 an SQL NULL, ret
1b5ac 75 72 6e 20 0a 2a 2a 20 30 20 69 6e 73 74 65 61 urn .** 0 instea
1b5ad 64 2e 20 55 6e 6c 65 73 73 20 69 74 20 69 73 20 d. Unless it is
1b5ae 4e 55 4c 4c 2c 20 61 70 70 6c 79 20 61 66 66 69 NULL, apply affi
1b5af 6e 69 74 79 20 61 66 66 20 28 6f 6e 65 20 6f 66 nity aff (one of
1b5b0 20 74 68 65 20 53 51 4c 49 54 45 5f 41 46 46 5f the SQLITE_AFF_
1b5b1 2a 0a 2a 2a 20 63 6f 6e 73 74 61 6e 74 73 29 20 *.** constants)
1b5b2 74 6f 20 74 68 65 20 76 61 6c 75 65 20 62 65 66 to the value bef
1b5b3 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 20 69 74 ore returning it
1b5b4 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 ..**.** The retu
1b5b5 72 6e 65 64 20 76 61 6c 75 65 20 6d 75 73 74 20 rned value must
1b5b6 62 65 20 66 72 65 65 64 20 62 79 20 74 68 65 20 be freed by the
1b5b7 63 61 6c 6c 65 72 20 75 73 69 6e 67 20 73 71 6c caller using sql
1b5b8 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 29 2e ite3ValueFree().
1b5b9 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1b5ba 54 45 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 TE sqlite3_value
1b5bb 20 2a 73 71 6c 69 74 65 33 56 64 62 65 47 65 74 *sqlite3VdbeGet
1b5bc 56 61 6c 75 65 28 56 64 62 65 20 2a 76 2c 20 69 Value(Vdbe *v, i
1b5bd 6e 74 20 69 56 61 72 2c 20 75 38 20 61 66 66 29 nt iVar, u8 aff)
1b5be 7b 0a 20 20 61 73 73 65 72 74 28 20 69 56 61 72 {. assert( iVar
1b5bf 3e 30 20 29 3b 0a 20 20 69 66 28 20 76 20 29 7b >0 );. if( v ){
1b5c0 0a 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d . Mem *pMem =
1b5c1 20 26 76 2d 3e 61 56 61 72 5b 69 56 61 72 2d 31 &v->aVar[iVar-1
1b5c2 5d 3b 0a 20 20 20 20 69 66 28 20 30 3d 3d 28 70 ];. if( 0==(p
1b5c3 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d Mem->flags & MEM
1b5c4 5f 4e 75 6c 6c 29 20 29 7b 0a 20 20 20 20 20 20 _Null) ){.
1b5c5 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 sqlite3_value *p
1b5c6 52 65 74 20 3d 20 73 71 6c 69 74 65 33 56 61 6c Ret = sqlite3Val
1b5c7 75 65 4e 65 77 28 76 2d 3e 64 62 29 3b 0a 20 20 ueNew(v->db);.
1b5c8 20 20 20 20 69 66 28 20 70 52 65 74 20 29 7b 0a if( pRet ){.
1b5c9 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1b5ca 64 62 65 4d 65 6d 43 6f 70 79 28 28 4d 65 6d 20 dbeMemCopy((Mem
1b5cb 2a 29 70 52 65 74 2c 20 70 4d 65 6d 29 3b 0a 20 *)pRet, pMem);.
1b5cc 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 sqlite3Va
1b5cd 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 lueApplyAffinity
1b5ce 28 70 52 65 74 2c 20 61 66 66 2c 20 53 51 4c 49 (pRet, aff, SQLI
1b5cf 54 45 5f 55 54 46 38 29 3b 0a 20 20 20 20 20 20 TE_UTF8);.
1b5d0 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1b5d1 53 74 6f 72 65 54 79 70 65 28 28 4d 65 6d 20 2a StoreType((Mem *
1b5d2 29 70 52 65 74 29 3b 0a 20 20 20 20 20 20 7d 0a )pRet);. }.
1b5d3 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 52 65 return pRe
1b5d4 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 t;. }. }. r
1b5d5 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn 0;.}../*.*
1b5d6 2a 20 43 6f 6e 66 69 67 75 72 65 20 53 51 4c 20 * Configure SQL
1b5d7 76 61 72 69 61 62 6c 65 20 69 56 61 72 20 73 6f variable iVar so
1b5d8 20 74 68 61 74 20 62 69 6e 64 69 6e 67 20 61 20 that binding a
1b5d9 6e 65 77 20 76 61 6c 75 65 20 74 6f 20 69 74 20 new value to it
1b5da 73 69 67 6e 61 6c 73 0a 2a 2a 20 74 6f 20 73 71 signals.** to sq
1b5db 6c 69 74 65 33 5f 72 65 6f 70 74 69 6d 69 7a 65 lite3_reoptimize
1b5dc 28 29 20 74 68 61 74 20 72 65 2d 70 72 65 70 61 () that re-prepa
1b5dd 72 69 6e 67 20 74 68 65 20 73 74 61 74 65 6d 65 ring the stateme
1b5de 6e 74 20 6d 61 79 20 72 65 73 75 6c 74 0a 2a 2a nt may result.**
1b5df 20 69 6e 20 61 20 62 65 74 74 65 72 20 71 75 65 in a better que
1b5e0 72 79 20 70 6c 61 6e 2e 0a 2a 2f 0a 53 51 4c 49 ry plan..*/.SQLI
1b5e1 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1b5e2 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 56 61 sqlite3VdbeSetVa
1b5e3 72 6d 61 73 6b 28 56 64 62 65 20 2a 76 2c 20 69 rmask(Vdbe *v, i
1b5e4 6e 74 20 69 56 61 72 29 7b 0a 20 20 61 73 73 65 nt iVar){. asse
1b5e5 72 74 28 20 69 56 61 72 3e 30 20 29 3b 0a 20 20 rt( iVar>0 );.
1b5e6 69 66 28 20 69 56 61 72 3e 33 32 20 29 7b 0a 20 if( iVar>32 ){.
1b5e7 20 20 20 76 2d 3e 65 78 70 6d 61 73 6b 20 3d 20 v->expmask =
1b5e8 30 78 66 66 66 66 66 66 66 66 3b 0a 20 20 7d 65 0xffffffff;. }e
1b5e9 6c 73 65 7b 0a 20 20 20 20 76 2d 3e 65 78 70 6d lse{. v->expm
1b5ea 61 73 6b 20 7c 3d 20 28 28 75 33 32 29 31 20 3c ask |= ((u32)1 <
1b5eb 3c 20 28 69 56 61 72 2d 31 29 29 3b 0a 20 20 7d < (iVar-1));. }
1b5ec 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .}../***********
1b5ed 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 61 *** End of vdbea
1b5ee 75 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ux.c ***********
1b5ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b5f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b5f1 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
1b5f2 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 *** Begin file v
1b5f3 64 62 65 61 70 69 2e 63 20 2a 2a 2a 2a 2a 2a 2a dbeapi.c *******
1b5f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b5f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b5f6 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d **/./*.** 2004 M
1b5f7 61 79 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ay 26.**.** The
1b5f8 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
1b5f9 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
1b5fa 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
1b5fb 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
1b5fc 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
1b5fd 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
1b5fe 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
1b5ff 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
1b600 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
1b601 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
1b602 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
1b603 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
1b604 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
1b605 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
1b606 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
1b607 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
1b608 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
1b609 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b60a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b60b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b60c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b60d 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ****.**.** This
1b60e 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f file contains co
1b60f 64 65 20 75 73 65 20 74 6f 20 69 6d 70 6c 65 6d de use to implem
1b610 65 6e 74 20 41 50 49 73 20 74 68 61 74 20 61 72 ent APIs that ar
1b611 65 20 70 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a e part of the.**
1b612 20 56 44 42 45 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 VDBE..**.** $Id
1b613 3a 20 76 64 62 65 61 70 69 2e 63 2c 76 20 31 2e : vdbeapi.c,v 1.
1b614 31 36 37 20 32 30 30 39 2f 30 36 2f 32 35 20 30 167 2009/06/25 0
1b615 31 3a 34 37 3a 31 32 20 64 72 68 20 45 78 70 20 1:47:12 drh Exp
1b616 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 $.*/..#ifndef SQ
1b617 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 LITE_OMIT_DEPREC
1b618 41 54 45 44 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ATED./*.** Retur
1b619 6e 20 54 52 55 45 20 28 6e 6f 6e 2d 7a 65 72 6f n TRUE (non-zero
1b61a 29 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d 65 ) of the stateme
1b61b 6e 74 20 73 75 70 70 6c 69 65 64 20 61 73 20 61 nt supplied as a
1b61c 6e 20 61 72 67 75 6d 65 6e 74 20 6e 65 65 64 73 n argument needs
1b61d 0a 2a 2a 20 74 6f 20 62 65 20 72 65 63 6f 6d 70 .** to be recomp
1b61e 69 6c 65 64 2e 20 20 41 20 73 74 61 74 65 6d 65 iled. A stateme
1b61f 6e 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 nt needs to be r
1b620 65 63 6f 6d 70 69 6c 65 64 20 77 68 65 6e 65 76 ecompiled whenev
1b621 65 72 20 74 68 65 0a 2a 2a 20 65 78 65 63 75 74 er the.** execut
1b622 69 6f 6e 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 ion environment
1b623 63 68 61 6e 67 65 73 20 69 6e 20 61 20 77 61 79 changes in a way
1b624 20 74 68 61 74 20 77 6f 75 6c 64 20 61 6c 74 65 that would alte
1b625 72 20 74 68 65 20 70 72 6f 67 72 61 6d 0a 2a 2a r the program.**
1b626 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f 70 72 that sqlite3_pr
1b627 65 70 61 72 65 28 29 20 67 65 6e 65 72 61 74 65 epare() generate
1b628 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c s. For example,
1b629 20 69 66 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e if new function
1b62a 73 20 6f 72 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e s or.** collatin
1b62b 67 20 73 65 71 75 65 6e 63 65 73 20 61 72 65 20 g sequences are
1b62c 72 65 67 69 73 74 65 72 65 64 20 6f 72 20 69 66 registered or if
1b62d 20 61 6e 20 61 75 74 68 6f 72 69 7a 65 72 20 66 an authorizer f
1b62e 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 61 64 unction is.** ad
1b62f 64 65 64 20 6f 72 20 63 68 61 6e 67 65 64 2e 0a ded or changed..
1b630 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
1b631 74 20 73 71 6c 69 74 65 33 5f 65 78 70 69 72 65 t sqlite3_expire
1b632 64 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a d(sqlite3_stmt *
1b633 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a pStmt){. Vdbe *
1b634 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 p = (Vdbe*)pStmt
1b635 3b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 ;. return p==0
1b636 7c 7c 20 70 2d 3e 65 78 70 69 72 65 64 3b 0a 7d || p->expired;.}
1b637 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
1b638 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 he following rou
1b639 74 69 6e 65 20 64 65 73 74 72 6f 79 73 20 61 20 tine destroys a
1b63a 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 virtual machine
1b63b 74 68 61 74 20 69 73 20 63 72 65 61 74 65 64 20 that is created
1b63c 62 79 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 by.** the sqlite
1b63d 33 5f 63 6f 6d 70 69 6c 65 28 29 20 72 6f 75 74 3_compile() rout
1b63e 69 6e 65 2e 20 54 68 65 20 69 6e 74 65 67 65 72 ine. The integer
1b63f 20 72 65 74 75 72 6e 65 64 20 69 73 20 61 6e 20 returned is an
1b640 53 51 4c 49 54 45 5f 0a 2a 2a 20 73 75 63 63 65 SQLITE_.** succe
1b641 73 73 2f 66 61 69 6c 75 72 65 20 63 6f 64 65 20 ss/failure code
1b642 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20 74 that describes t
1b643 68 65 20 72 65 73 75 6c 74 20 6f 66 20 65 78 65 he result of exe
1b644 63 75 74 69 6e 67 20 74 68 65 20 76 69 72 74 75 cuting the virtu
1b645 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 2e 0a 2a al.** machine..*
1b646 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
1b647 65 20 73 65 74 73 20 74 68 65 20 65 72 72 6f 72 e sets the error
1b648 20 63 6f 64 65 20 61 6e 64 20 73 74 72 69 6e 67 code and string
1b649 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 returned by.**
1b64a 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 sqlite3_errcode(
1b64b 29 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 ), sqlite3_errms
1b64c 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f g() and sqlite3_
1b64d 65 72 72 6d 73 67 31 36 28 29 2e 0a 2a 2f 0a 53 errmsg16()..*/.S
1b64e 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1b64f 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 73 lite3_finalize(s
1b650 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1b651 6d 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 mt){. int rc;.
1b652 20 69 66 28 20 70 53 74 6d 74 3d 3d 30 20 29 7b if( pStmt==0 ){
1b653 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
1b654 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 _OK;. }else{.
1b655 20 20 56 64 62 65 20 2a 76 20 3d 20 28 56 64 62 Vdbe *v = (Vdb
1b656 65 2a 29 70 53 74 6d 74 3b 0a 20 20 20 20 73 71 e*)pStmt;. sq
1b657 6c 69 74 65 33 20 2a 64 62 20 3d 20 76 2d 3e 64 lite3 *db = v->d
1b658 62 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 b;.#if SQLITE_TH
1b659 52 45 41 44 53 41 46 45 0a 20 20 20 20 73 71 6c READSAFE. sql
1b65a 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 ite3_mutex *mute
1b65b 78 20 3d 20 76 2d 3e 64 62 2d 3e 6d 75 74 65 78 x = v->db->mutex
1b65c 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 73 71 6c ;.#endif. sql
1b65d 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
1b65e 28 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 63 20 (mutex);. rc
1b65f 3d 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e = sqlite3VdbeFin
1b660 61 6c 69 7a 65 28 76 29 3b 0a 20 20 20 20 72 63 alize(v);. rc
1b661 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 = sqlite3ApiExi
1b662 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 20 20 73 t(db, rc);. s
1b663 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
1b664 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 ve(mutex);. }.
1b665 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1b666 2a 0a 2a 2a 20 54 65 72 6d 69 6e 61 74 65 20 74 *.** Terminate t
1b667 68 65 20 63 75 72 72 65 6e 74 20 65 78 65 63 75 he current execu
1b668 74 69 6f 6e 20 6f 66 20 61 6e 20 53 51 4c 20 73 tion of an SQL s
1b669 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 72 65 73 tatement and res
1b66a 65 74 20 69 74 0a 2a 2a 20 62 61 63 6b 20 74 6f et it.** back to
1b66b 20 69 74 73 20 73 74 61 72 74 69 6e 67 20 73 74 its starting st
1b66c 61 74 65 20 73 6f 20 74 68 61 74 20 69 74 20 63 ate so that it c
1b66d 61 6e 20 62 65 20 72 65 75 73 65 64 2e 20 41 20 an be reused. A
1b66e 73 75 63 63 65 73 73 20 63 6f 64 65 20 66 72 6f success code fro
1b66f 6d 0a 2a 2a 20 74 68 65 20 70 72 69 6f 72 20 65 m.** the prior e
1b670 78 65 63 75 74 69 6f 6e 20 69 73 20 72 65 74 75 xecution is retu
1b671 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 rned..**.** This
1b672 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 routine sets th
1b673 65 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 e error code and
1b674 20 73 74 72 69 6e 67 20 72 65 74 75 72 6e 65 64 string returned
1b675 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 65 by.** sqlite3_e
1b676 72 72 63 6f 64 65 28 29 2c 20 73 71 6c 69 74 65 rrcode(), sqlite
1b677 33 5f 65 72 72 6d 73 67 28 29 20 61 6e 64 20 73 3_errmsg() and s
1b678 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 qlite3_errmsg16(
1b679 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 )..*/.SQLITE_API
1b67a 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72 65 73 int sqlite3_res
1b67b 65 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 et(sqlite3_stmt
1b67c 2a 70 53 74 6d 74 29 7b 0a 20 20 69 6e 74 20 72 *pStmt){. int r
1b67d 63 3b 0a 20 20 69 66 28 20 70 53 74 6d 74 3d 3d c;. if( pStmt==
1b67e 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 0 ){. rc = SQ
1b67f 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 LITE_OK;. }else
1b680 7b 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20 {. Vdbe *v =
1b681 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 (Vdbe*)pStmt;.
1b682 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
1b683 65 6e 74 65 72 28 76 2d 3e 64 62 2d 3e 6d 75 74 enter(v->db->mut
1b684 65 78 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 ex);. rc = sq
1b685 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 28 76 lite3VdbeReset(v
1b686 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 );. sqlite3Vd
1b687 62 65 4d 61 6b 65 52 65 61 64 79 28 76 2c 20 2d beMakeReady(v, -
1b688 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 1, 0, 0, 0, 0, 0
1b689 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 );. assert( (
1b68a 72 63 20 26 20 28 76 2d 3e 64 62 2d 3e 65 72 72 rc & (v->db->err
1b68b 4d 61 73 6b 29 29 3d 3d 72 63 20 29 3b 0a 20 20 Mask))==rc );.
1b68c 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 rc = sqlite3Ap
1b68d 69 45 78 69 74 28 76 2d 3e 64 62 2c 20 72 63 29 iExit(v->db, rc)
1b68e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 ;. sqlite3_mu
1b68f 74 65 78 5f 6c 65 61 76 65 28 76 2d 3e 64 62 2d tex_leave(v->db-
1b690 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 >mutex);. }. r
1b691 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
1b692 2a 2a 20 53 65 74 20 61 6c 6c 20 74 68 65 20 70 ** Set all the p
1b693 61 72 61 6d 65 74 65 72 73 20 69 6e 20 74 68 65 arameters in the
1b694 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73 74 compiled SQL st
1b695 61 74 65 6d 65 6e 74 20 74 6f 20 4e 55 4c 4c 2e atement to NULL.
1b696 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
1b697 6e 74 20 73 71 6c 69 74 65 33 5f 63 6c 65 61 72 nt sqlite3_clear
1b698 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 65 _bindings(sqlite
1b699 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 3_stmt *pStmt){.
1b69a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 72 int i;. int r
1b69b 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
1b69c 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 Vdbe *p = (Vdbe
1b69d 2a 29 70 53 74 6d 74 3b 0a 23 69 66 20 53 51 4c *)pStmt;.#if SQL
1b69e 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 ITE_THREADSAFE.
1b69f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a sqlite3_mutex *
1b6a0 6d 75 74 65 78 20 3d 20 28 28 56 64 62 65 2a 29 mutex = ((Vdbe*)
1b6a1 70 53 74 6d 74 29 2d 3e 64 62 2d 3e 6d 75 74 65 pStmt)->db->mute
1b6a2 78 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 x;.#endif. sqli
1b6a3 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
1b6a4 6d 75 74 65 78 29 3b 0a 20 20 66 6f 72 28 69 3d mutex);. for(i=
1b6a5 30 3b 20 69 3c 70 2d 3e 6e 56 61 72 3b 20 69 2b 0; i<p->nVar; i+
1b6a6 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 +){. sqlite3V
1b6a7 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 70 dbeMemRelease(&p
1b6a8 2d 3e 61 56 61 72 5b 69 5d 29 3b 0a 20 20 20 20 ->aVar[i]);.
1b6a9 70 2d 3e 61 56 61 72 5b 69 5d 2e 66 6c 61 67 73 p->aVar[i].flags
1b6aa 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d = MEM_Null;. }
1b6ab 0a 20 20 69 66 28 20 70 2d 3e 69 73 50 72 65 70 . if( p->isPrep
1b6ac 61 72 65 56 32 20 26 26 20 70 2d 3e 65 78 70 6d areV2 && p->expm
1b6ad 61 73 6b 20 29 7b 0a 20 20 20 20 70 2d 3e 65 78 ask ){. p->ex
1b6ae 70 69 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 pired = 1;. }.
1b6af 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
1b6b0 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 eave(mutex);. r
1b6b1 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a eturn rc;.}.../*
1b6b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b6b3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 *********** sqli
1b6b4 74 65 33 5f 76 61 6c 75 65 5f 20 20 2a 2a 2a 2a te3_value_ ****
1b6b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b6b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 ***********.** T
1b6b7 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 he following rou
1b6b8 74 69 6e 65 73 20 65 78 74 72 61 63 74 20 69 6e tines extract in
1b6b9 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 61 formation from a
1b6ba 20 4d 65 6d 20 6f 72 20 73 71 6c 69 74 65 33 5f Mem or sqlite3_
1b6bb 76 61 6c 75 65 0a 2a 2a 20 73 74 72 75 63 74 75 value.** structu
1b6bc 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 re..*/.SQLITE_AP
1b6bd 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 I const void *sq
1b6be 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 lite3_value_blob
1b6bf 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a (sqlite3_value *
1b6c0 70 56 61 6c 29 7b 0a 20 20 4d 65 6d 20 2a 70 20 pVal){. Mem *p
1b6c1 3d 20 28 4d 65 6d 2a 29 70 56 61 6c 3b 0a 20 20 = (Mem*)pVal;.
1b6c2 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 28 if( p->flags & (
1b6c3 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 MEM_Blob|MEM_Str
1b6c4 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ) ){. sqlite3
1b6c5 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f VdbeMemExpandBlo
1b6c6 62 28 70 29 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 b(p);. p->fla
1b6c7 67 73 20 26 3d 20 7e 4d 45 4d 5f 53 74 72 3b 0a gs &= ~MEM_Str;.
1b6c8 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 7c 3d 20 p->flags |=
1b6c9 4d 45 4d 5f 42 6c 6f 62 3b 0a 20 20 20 20 72 65 MEM_Blob;. re
1b6ca 74 75 72 6e 20 70 2d 3e 7a 3b 0a 20 20 7d 65 6c turn p->z;. }el
1b6cb 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 se{. return s
1b6cc 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
1b6cd 74 28 70 56 61 6c 29 3b 0a 20 20 7d 0a 7d 0a 53 t(pVal);. }.}.S
1b6ce 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1b6cf 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 lite3_value_byte
1b6d0 73 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 s(sqlite3_value
1b6d1 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e *pVal){. return
1b6d2 20 73 71 6c 69 74 65 33 56 61 6c 75 65 42 79 74 sqlite3ValueByt
1b6d3 65 73 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f es(pVal, SQLITE_
1b6d4 55 54 46 38 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f UTF8);.}.SQLITE_
1b6d5 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1b6d6 76 61 6c 75 65 5f 62 79 74 65 73 31 36 28 73 71 value_bytes16(sq
1b6d7 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 lite3_value *pVa
1b6d8 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c l){. return sql
1b6d9 69 74 65 33 56 61 6c 75 65 42 79 74 65 73 28 70 ite3ValueBytes(p
1b6da 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 Val, SQLITE_UTF1
1b6db 36 4e 41 54 49 56 45 29 3b 0a 7d 0a 53 51 4c 49 6NATIVE);.}.SQLI
1b6dc 54 45 5f 41 50 49 20 64 6f 75 62 6c 65 20 73 71 TE_API double sq
1b6dd 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 lite3_value_doub
1b6de 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 le(sqlite3_value
1b6df 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 *pVal){. retur
1b6e0 6e 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 61 n sqlite3VdbeRea
1b6e1 6c 56 61 6c 75 65 28 28 4d 65 6d 2a 29 70 56 61 lValue((Mem*)pVa
1b6e2 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 l);.}.SQLITE_API
1b6e3 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c int sqlite3_val
1b6e4 75 65 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 76 ue_int(sqlite3_v
1b6e5 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 alue *pVal){. r
1b6e6 65 74 75 72 6e 20 28 69 6e 74 29 73 71 6c 69 74 eturn (int)sqlit
1b6e7 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 28 e3VdbeIntValue((
1b6e8 4d 65 6d 2a 29 70 56 61 6c 29 3b 0a 7d 0a 53 51 Mem*)pVal);.}.SQ
1b6e9 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 5f LITE_API sqlite_
1b6ea 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 76 61 int64 sqlite3_va
1b6eb 6c 75 65 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 lue_int64(sqlite
1b6ec 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 3_value *pVal){.
1b6ed 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
1b6ee 56 64 62 65 49 6e 74 56 61 6c 75 65 28 28 4d 65 VdbeIntValue((Me
1b6ef 6d 2a 29 70 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 m*)pVal);.}.SQLI
1b6f0 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 75 6e 73 TE_API const uns
1b6f1 69 67 6e 65 64 20 63 68 61 72 20 2a 73 71 6c 69 igned char *sqli
1b6f2 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 73 te3_value_text(s
1b6f3 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 qlite3_value *pV
1b6f4 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 63 al){. return (c
1b6f5 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
1b6f6 61 72 20 2a 29 73 71 6c 69 74 65 33 56 61 6c 75 ar *)sqlite3Valu
1b6f7 65 54 65 78 74 28 70 56 61 6c 2c 20 53 51 4c 49 eText(pVal, SQLI
1b6f8 54 45 5f 55 54 46 38 29 3b 0a 7d 0a 23 69 66 6e TE_UTF8);.}.#ifn
1b6f9 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1b6fa 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 UTF16.SQLITE_API
1b6fb 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c const void *sql
1b6fc 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 ite3_value_text1
1b6fd 36 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 6(sqlite3_value*
1b6fe 20 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e pVal){. return
1b6ff 20 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 sqlite3ValueTex
1b700 74 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 t(pVal, SQLITE_U
1b701 54 46 31 36 4e 41 54 49 56 45 29 3b 0a 7d 0a 53 TF16NATIVE);.}.S
1b702 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
1b703 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 void *sqlite3_va
1b704 6c 75 65 5f 74 65 78 74 31 36 62 65 28 73 71 6c lue_text16be(sql
1b705 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c ite3_value *pVal
1b706 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 ){. return sqli
1b707 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 61 te3ValueText(pVa
1b708 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 l, SQLITE_UTF16B
1b709 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 E);.}.SQLITE_API
1b70a 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c const void *sql
1b70b 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 ite3_value_text1
1b70c 36 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 6le(sqlite3_valu
1b70d 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 e *pVal){. retu
1b70e 72 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65 54 rn sqlite3ValueT
1b70f 65 78 74 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 ext(pVal, SQLITE
1b710 5f 55 54 46 31 36 4c 45 29 3b 0a 7d 0a 23 65 6e _UTF16LE);.}.#en
1b711 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
1b712 49 54 5f 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49 IT_UTF16 */.SQLI
1b713 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
1b714 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 73 71 e3_value_type(sq
1b715 6c 69 74 65 33 5f 76 61 6c 75 65 2a 20 70 56 61 lite3_value* pVa
1b716 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 61 l){. return pVa
1b717 6c 2d 3e 74 79 70 65 3b 0a 7d 0a 0a 2f 2a 2a 2a l->type;.}../***
1b718 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b719 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 ********* sqlite
1b71a 33 5f 72 65 73 75 6c 74 5f 20 20 2a 2a 2a 2a 2a 3_result_ *****
1b71b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b71c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 **********.** Th
1b71d 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 e following rout
1b71e 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 62 79 ines are used by
1b71f 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20 66 75 user-defined fu
1b720 6e 63 74 69 6f 6e 73 20 74 6f 20 73 70 65 63 69 nctions to speci
1b721 66 79 0a 2a 2a 20 74 68 65 20 66 75 6e 63 74 69 fy.** the functi
1b722 6f 6e 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a on result..**.**
1b723 20 54 68 65 20 73 65 74 53 74 72 4f 72 45 72 72 The setStrOrErr
1b724 6f 72 28 29 20 66 75 6e 74 69 6f 6e 20 63 61 6c or() funtion cal
1b725 6c 73 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 ls sqlite3VdbeMe
1b726 6d 53 65 74 53 74 72 28 29 20 74 6f 20 73 74 6f mSetStr() to sto
1b727 72 65 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 re the.** result
1b728 20 61 73 20 61 20 73 74 72 69 6e 67 20 6f 72 20 as a string or
1b729 62 6c 6f 62 20 62 75 74 20 69 66 20 74 68 65 20 blob but if the
1b72a 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 69 string or blob i
1b72b 73 20 74 6f 6f 20 6c 61 72 67 65 2c 20 69 74 0a s too large, it.
1b72c 2a 2a 20 74 68 65 6e 20 73 65 74 73 20 74 68 65 ** then sets the
1b72d 20 65 72 72 6f 72 20 63 6f 64 65 20 74 6f 20 53 error code to S
1b72e 51 4c 49 54 45 5f 54 4f 4f 42 49 47 0a 2a 2f 0a QLITE_TOOBIG.*/.
1b72f 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 74 52 static void setR
1b730 65 73 75 6c 74 53 74 72 4f 72 45 72 72 6f 72 28 esultStrOrError(
1b731 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 . sqlite3_conte
1b732 78 74 20 2a 70 43 74 78 2c 20 20 2f 2a 20 46 75 xt *pCtx, /* Fu
1b733 6e 63 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a nction context *
1b734 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
1b735 7a 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 z, /* S
1b736 74 72 69 6e 67 20 70 6f 69 6e 74 65 72 20 2a 2f tring pointer */
1b737 0a 20 20 69 6e 74 20 6e 2c 20 20 20 20 20 20 20 . int n,
1b738 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 /* By
1b739 74 65 73 20 69 6e 20 73 74 72 69 6e 67 2c 20 6f tes in string, o
1b73a 72 20 6e 65 67 61 74 69 76 65 20 2a 2f 0a 20 20 r negative */.
1b73b 75 38 20 65 6e 63 2c 20 20 20 20 20 20 20 20 20 u8 enc,
1b73c 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 /* Encod
1b73d 69 6e 67 20 6f 66 20 7a 2e 20 20 30 20 66 6f 72 ing of z. 0 for
1b73e 20 42 4c 4f 42 73 20 2a 2f 0a 20 20 76 6f 69 64 BLOBs */. void
1b73f 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 20 (*xDel)(void*)
1b740 20 20 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f /* Destructo
1b741 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b r function */.){
1b742 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 . if( sqlite3Vd
1b743 62 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74 beMemSetStr(&pCt
1b744 78 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 65 6e 63 2c x->s, z, n, enc,
1b745 20 78 44 65 6c 29 3d 3d 53 51 4c 49 54 45 5f 54 xDel)==SQLITE_T
1b746 4f 4f 42 49 47 20 29 7b 0a 20 20 20 20 73 71 6c OOBIG ){. sql
1b747 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f ite3_result_erro
1b748 72 5f 74 6f 6f 62 69 67 28 70 43 74 78 29 3b 0a r_toobig(pCtx);.
1b749 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 }.}.SQLITE_API
1b74a 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 void sqlite3_re
1b74b 73 75 6c 74 5f 62 6c 6f 62 28 0a 20 20 73 71 6c sult_blob(. sql
1b74c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 ite3_context *pC
1b74d 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 tx, . const voi
1b74e 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 d *z, . int n,
1b74f 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 . void (*xDel)(
1b750 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 void *).){. ass
1b751 65 72 74 28 20 6e 3e 3d 30 20 29 3b 0a 20 20 61 ert( n>=0 );. a
1b752 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
1b753 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e utex_held(pCtx->
1b754 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a s.db->mutex) );.
1b755 20 20 73 65 74 52 65 73 75 6c 74 53 74 72 4f 72 setResultStrOr
1b756 45 72 72 6f 72 28 70 43 74 78 2c 20 7a 2c 20 6e Error(pCtx, z, n
1b757 2c 20 30 2c 20 78 44 65 6c 29 3b 0a 7d 0a 53 51 , 0, xDel);.}.SQ
1b758 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
1b759 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 lite3_result_dou
1b75a 62 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 ble(sqlite3_cont
1b75b 65 78 74 20 2a 70 43 74 78 2c 20 64 6f 75 62 6c ext *pCtx, doubl
1b75c 65 20 72 56 61 6c 29 7b 0a 20 20 61 73 73 65 72 e rVal){. asser
1b75d 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
1b75e 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 _held(pCtx->s.db
1b75f 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 ->mutex) );. sq
1b760 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 44 lite3VdbeMemSetD
1b761 6f 75 62 6c 65 28 26 70 43 74 78 2d 3e 73 2c 20 ouble(&pCtx->s,
1b762 72 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f rVal);.}.SQLITE_
1b763 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
1b764 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 73 71 _result_error(sq
1b765 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 lite3_context *p
1b766 43 74 78 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 Ctx, const char
1b767 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 61 73 *z, int n){. as
1b768 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1b769 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 tex_held(pCtx->s
1b76a 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 .db->mutex) );.
1b76b 20 70 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d pCtx->isError =
1b76c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 SQLITE_ERROR;.
1b76d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1b76e 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 etStr(&pCtx->s,
1b76f 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 z, n, SQLITE_UTF
1b770 38 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 8, SQLITE_TRANSI
1b771 45 4e 54 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 ENT);.}.#ifndef
1b772 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 SQLITE_OMIT_UTF1
1b773 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 6.SQLITE_API voi
1b774 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 d sqlite3_result
1b775 5f 65 72 72 6f 72 31 36 28 73 71 6c 69 74 65 33 _error16(sqlite3
1b776 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 _context *pCtx,
1b777 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 69 const void *z, i
1b778 6e 74 20 6e 29 7b 0a 20 20 61 73 73 65 72 74 28 nt n){. assert(
1b779 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
1b77a 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e eld(pCtx->s.db->
1b77b 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 43 74 78 mutex) );. pCtx
1b77c 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 4c 49 ->isError = SQLI
1b77d 54 45 5f 45 52 52 4f 52 3b 0a 20 20 73 71 6c 69 TE_ERROR;. sqli
1b77e 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 te3VdbeMemSetStr
1b77f 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e 2c (&pCtx->s, z, n,
1b780 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 SQLITE_UTF16NAT
1b781 49 56 45 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e IVE, SQLITE_TRAN
1b782 53 49 45 4e 54 29 3b 0a 7d 0a 23 65 6e 64 69 66 SIENT);.}.#endif
1b783 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
1b784 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
1b785 69 6e 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 int(sqlite3_cont
1b786 65 78 74 20 2a 70 43 74 78 2c 20 69 6e 74 20 69 ext *pCtx, int i
1b787 56 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 Val){. assert(
1b788 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
1b789 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d ld(pCtx->s.db->m
1b78a 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 utex) );. sqlit
1b78b 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 e3VdbeMemSetInt6
1b78c 34 28 26 70 43 74 78 2d 3e 73 2c 20 28 69 36 34 4(&pCtx->s, (i64
1b78d 29 69 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 )iVal);.}.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 69 6e 74 36 34 28 73 3_result_int64(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 69 36 34 20 69 56 61 6c 29 7b pCtx, i64 iVal){
1b792 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
1b793 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 e3_mutex_held(pC
1b794 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 tx->s.db->mutex)
1b795 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 );. sqlite3Vdb
1b796 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 26 70 43 eMemSetInt64(&pC
1b797 74 78 2d 3e 73 2c 20 69 56 61 6c 29 3b 0a 7d 0a tx->s, iVal);.}.
1b798 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
1b799 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e sqlite3_result_n
1b79a 75 6c 6c 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 ull(sqlite3_cont
1b79b 65 78 74 20 2a 70 43 74 78 29 7b 0a 20 20 61 73 ext *pCtx){. as
1b79c 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1b79d 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 tex_held(pCtx->s
1b79e 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 .db->mutex) );.
1b79f 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1b7a0 65 74 4e 75 6c 6c 28 26 70 43 74 78 2d 3e 73 29 etNull(&pCtx->s)
1b7a1 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 ;.}.SQLITE_API v
1b7a2 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 oid sqlite3_resu
1b7a3 6c 74 5f 74 65 78 74 28 0a 20 20 73 71 6c 69 74 lt_text(. sqlit
1b7a4 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 e3_context *pCtx
1b7a5 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 , . const char
1b7a6 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 0a 20 20 *z, . int n,.
1b7a7 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 void (*xDel)(voi
1b7a8 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72 74 d *).){. assert
1b7a9 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
1b7aa 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d held(pCtx->s.db-
1b7ab 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 65 74 >mutex) );. set
1b7ac 52 65 73 75 6c 74 53 74 72 4f 72 45 72 72 6f 72 ResultStrOrError
1b7ad 28 70 43 74 78 2c 20 7a 2c 20 6e 2c 20 53 51 4c (pCtx, z, n, SQL
1b7ae 49 54 45 5f 55 54 46 38 2c 20 78 44 65 6c 29 3b ITE_UTF8, xDel);
1b7af 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 .}.#ifndef SQLIT
1b7b0 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c E_OMIT_UTF16.SQL
1b7b1 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
1b7b2 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 ite3_result_text
1b7b3 31 36 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 16(. sqlite3_co
1b7b4 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 0a 20 20 ntext *pCtx, .
1b7b5 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 0a const void *z, .
1b7b6 20 20 69 6e 74 20 6e 2c 20 0a 20 20 76 6f 69 64 int n, . void
1b7b7 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a 29 (*xDel)(void *)
1b7b8 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 .){. assert( sq
1b7b9 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
1b7ba 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 (pCtx->s.db->mut
1b7bb 65 78 29 20 29 3b 0a 20 20 73 65 74 52 65 73 75 ex) );. setResu
1b7bc 6c 74 53 74 72 4f 72 45 72 72 6f 72 28 70 43 74 ltStrOrError(pCt
1b7bd 78 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f x, z, n, SQLITE_
1b7be 55 54 46 31 36 4e 41 54 49 56 45 2c 20 78 44 65 UTF16NATIVE, xDe
1b7bf 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 l);.}.SQLITE_API
1b7c0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 void sqlite3_re
1b7c1 73 75 6c 74 5f 74 65 78 74 31 36 62 65 28 0a 20 sult_text16be(.
1b7c2 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
1b7c3 20 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 *pCtx, . const
1b7c4 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 void *z, . int
1b7c5 20 6e 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 n, . void (*xD
1b7c6 65 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 el)(void *).){.
1b7c7 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1b7c8 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 _mutex_held(pCtx
1b7c9 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 ->s.db->mutex) )
1b7ca 3b 0a 20 20 73 65 74 52 65 73 75 6c 74 53 74 72 ;. setResultStr
1b7cb 4f 72 45 72 72 6f 72 28 70 43 74 78 2c 20 7a 2c OrError(pCtx, z,
1b7cc 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 n, SQLITE_UTF16
1b7cd 42 45 2c 20 78 44 65 6c 29 3b 0a 7d 0a 53 51 4c BE, xDel);.}.SQL
1b7ce 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
1b7cf 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 ite3_result_text
1b7d0 31 36 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 16le(. sqlite3_
1b7d1 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 0a context *pCtx, .
1b7d2 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c const void *z,
1b7d3 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20 76 6f . int n, . vo
1b7d4 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 20 id (*xDel)(void
1b7d5 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 *).){. assert(
1b7d6 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
1b7d7 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d ld(pCtx->s.db->m
1b7d8 75 74 65 78 29 20 29 3b 0a 20 20 73 65 74 52 65 utex) );. setRe
1b7d9 73 75 6c 74 53 74 72 4f 72 45 72 72 6f 72 28 70 sultStrOrError(p
1b7da 43 74 78 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 Ctx, z, n, SQLIT
1b7db 45 5f 55 54 46 31 36 4c 45 2c 20 78 44 65 6c 29 E_UTF16LE, xDel)
1b7dc 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
1b7dd 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 LITE_OMIT_UTF16
1b7de 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f */.SQLITE_API vo
1b7df 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c id sqlite3_resul
1b7e0 74 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f t_value(sqlite3_
1b7e1 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 73 context *pCtx, s
1b7e2 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 qlite3_value *pV
1b7e3 61 6c 75 65 29 7b 0a 20 20 61 73 73 65 72 74 28 alue){. assert(
1b7e4 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
1b7e5 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e eld(pCtx->s.db->
1b7e6 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 mutex) );. sqli
1b7e7 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 26 te3VdbeMemCopy(&
1b7e8 70 43 74 78 2d 3e 73 2c 20 70 56 61 6c 75 65 29 pCtx->s, pValue)
1b7e9 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 ;.}.SQLITE_API v
1b7ea 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 oid sqlite3_resu
1b7eb 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 73 71 6c 69 lt_zeroblob(sqli
1b7ec 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 te3_context *pCt
1b7ed 78 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 61 73 73 x, int n){. ass
1b7ee 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
1b7ef 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e ex_held(pCtx->s.
1b7f0 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
1b7f1 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
1b7f2 74 5a 65 72 6f 42 6c 6f 62 28 26 70 43 74 78 2d tZeroBlob(&pCtx-
1b7f3 3e 73 2c 20 6e 29 3b 0a 7d 0a 53 51 4c 49 54 45 >s, n);.}.SQLITE
1b7f4 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
1b7f5 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 3_result_error_c
1b7f6 6f 64 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 ode(sqlite3_cont
1b7f7 65 78 74 20 2a 70 43 74 78 2c 20 69 6e 74 20 65 ext *pCtx, int e
1b7f8 72 72 43 6f 64 65 29 7b 0a 20 20 70 43 74 78 2d rrCode){. pCtx-
1b7f9 3e 69 73 45 72 72 6f 72 20 3d 20 65 72 72 43 6f >isError = errCo
1b7fa 64 65 3b 0a 20 20 69 66 28 20 70 43 74 78 2d 3e de;. if( pCtx->
1b7fb 73 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 s.flags & MEM_Nu
1b7fc 6c 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ll ){. sqlite
1b7fd 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 3VdbeMemSetStr(&
1b7fe 70 43 74 78 2d 3e 73 2c 20 73 71 6c 69 74 65 33 pCtx->s, sqlite3
1b7ff 45 72 72 53 74 72 28 65 72 72 43 6f 64 65 29 2c ErrStr(errCode),
1b800 20 2d 31 2c 20 0a 20 20 20 20 20 20 20 20 20 20 -1, .
1b801 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 S
1b802 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 QLITE_UTF8, SQLI
1b803 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a TE_STATIC);. }.
1b804 7d 0a 0a 2f 2a 20 46 6f 72 63 65 20 61 6e 20 53 }../* Force an S
1b805 51 4c 49 54 45 5f 54 4f 4f 42 49 47 20 65 72 72 QLITE_TOOBIG err
1b806 6f 72 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 or. */.SQLITE_AP
1b807 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
1b808 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 esult_error_toob
1b809 69 67 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 ig(sqlite3_conte
1b80a 78 74 20 2a 70 43 74 78 29 7b 0a 20 20 61 73 73 xt *pCtx){. ass
1b80b 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
1b80c 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e ex_held(pCtx->s.
1b80d 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
1b80e 70 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 pCtx->isError =
1b80f 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 SQLITE_TOOBIG;.
1b810 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1b811 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 etStr(&pCtx->s,
1b812 22 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 "string or blob
1b813 74 6f 6f 20 62 69 67 22 2c 20 2d 31 2c 20 0a 20 too big", -1, .
1b814 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b815 20 20 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46 SQLITE_UTF
1b816 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 8, SQLITE_STATIC
1b817 29 3b 0a 7d 0a 0a 2f 2a 20 41 6e 20 53 51 4c 49 );.}../* An SQLI
1b818 54 45 5f 4e 4f 4d 45 4d 20 65 72 72 6f 72 2e 20 TE_NOMEM error.
1b819 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f */.SQLITE_API vo
1b81a 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c id sqlite3_resul
1b81b 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 73 71 t_error_nomem(sq
1b81c 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 lite3_context *p
1b81d 43 74 78 29 7b 0a 20 20 61 73 73 65 72 74 28 20 Ctx){. assert(
1b81e 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
1b81f 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d ld(pCtx->s.db->m
1b820 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 utex) );. sqlit
1b821 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c e3VdbeMemSetNull
1b822 28 26 70 43 74 78 2d 3e 73 29 3b 0a 20 20 70 43 (&pCtx->s);. pC
1b823 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 tx->isError = SQ
1b824 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 70 43 LITE_NOMEM;. pC
1b825 74 78 2d 3e 73 2e 64 62 2d 3e 6d 61 6c 6c 6f 63 tx->s.db->malloc
1b826 46 61 69 6c 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f Failed = 1;.}../
1b827 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 74 68 65 *.** Execute the
1b828 20 73 74 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 statement pStmt
1b829 2c 20 65 69 74 68 65 72 20 75 6e 74 69 6c 20 61 , either until a
1b82a 20 72 6f 77 20 6f 66 20 64 61 74 61 20 69 73 20 row of data is
1b82b 72 65 61 64 79 2c 20 74 68 65 0a 2a 2a 20 73 74 ready, the.** st
1b82c 61 74 65 6d 65 6e 74 20 69 73 20 63 6f 6d 70 6c atement is compl
1b82d 65 74 65 6c 79 20 65 78 65 63 75 74 65 64 20 6f etely executed o
1b82e 72 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 r an error occur
1b82f 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f s..**.** This ro
1b830 75 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73 utine implements
1b831 20 74 68 65 20 62 75 6c 6b 20 6f 66 20 74 68 65 the bulk of the
1b832 20 6c 6f 67 69 63 20 62 65 68 69 6e 64 20 74 68 logic behind th
1b833 65 20 73 71 6c 69 74 65 5f 73 74 65 70 28 29 0a e sqlite_step().
1b834 2a 2a 20 41 50 49 2e 20 20 54 68 65 20 6f 6e 6c ** API. The onl
1b835 79 20 74 68 69 6e 67 20 6f 6d 69 74 74 65 64 20 y thing omitted
1b836 69 73 20 74 68 65 20 61 75 74 6f 6d 61 74 69 63 is the automatic
1b837 20 72 65 63 6f 6d 70 69 6c 65 20 69 66 20 61 20 recompile if a
1b838 0a 2a 2a 20 73 63 68 65 6d 61 20 63 68 61 6e 67 .** schema chang
1b839 65 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20 e has occurred.
1b83a 20 54 68 61 74 20 64 65 74 61 69 6c 20 69 73 20 That detail is
1b83b 68 61 6e 64 6c 65 64 20 62 79 20 74 68 65 0a 2a handled by the.*
1b83c 2a 20 6f 75 74 65 72 20 73 71 6c 69 74 65 33 5f * outer sqlite3_
1b83d 73 74 65 70 28 29 20 77 72 61 70 70 65 72 20 70 step() wrapper p
1b83e 72 6f 63 65 64 75 72 65 2e 0a 2a 2f 0a 73 74 61 rocedure..*/.sta
1b83f 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 53 tic int sqlite3S
1b840 74 65 70 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 tep(Vdbe *p){.
1b841 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 69 sqlite3 *db;. i
1b842 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 nt rc;.. assert
1b843 28 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 (p);. if( p->ma
1b844 67 69 63 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f gic!=VDBE_MAGIC_
1b845 52 55 4e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 RUN ){. retur
1b846 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b n SQLITE_MISUSE;
1b847 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 73 73 65 72 . }.. /* Asser
1b848 74 20 74 68 61 74 20 6d 61 6c 6c 6f 63 28 29 20 t that malloc()
1b849 68 61 73 20 6e 6f 74 20 66 61 69 6c 65 64 20 2a has not failed *
1b84a 2f 0a 20 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a /. db = p->db;.
1b84b 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 if( db->malloc
1b84c 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 Failed ){. re
1b84d 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
1b84e 4d 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 2d M;. }.. if( p-
1b84f 3e 70 63 3c 3d 30 20 26 26 20 70 2d 3e 65 78 70 >pc<=0 && p->exp
1b850 69 72 65 64 20 29 7b 0a 20 20 20 20 69 66 28 20 ired ){. if(
1b851 41 4c 57 41 59 53 28 70 2d 3e 72 63 3d 3d 53 51 ALWAYS(p->rc==SQ
1b852 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63 LITE_OK || p->rc
1b853 3d 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 29 ==SQLITE_SCHEMA)
1b854 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 72 63 20 ){. p->rc
1b855 3d 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b = SQLITE_SCHEMA;
1b856 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 . }. rc =
1b857 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 SQLITE_ERROR;.
1b858 20 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 73 74 goto end_of_st
1b859 65 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 ep;. }. if( sq
1b85a 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 lite3SafetyOn(db
1b85b 29 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d ) ){. p->rc =
1b85c 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a SQLITE_MISUSE;.
1b85d 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1b85e 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 E_MISUSE;. }.
1b85f 69 66 28 20 70 2d 3e 70 63 3c 30 20 29 7b 0a 20 if( p->pc<0 ){.
1b860 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 /* If there a
1b861 72 65 20 6e 6f 20 6f 74 68 65 72 20 73 74 61 74 re no other stat
1b862 65 6d 65 6e 74 73 20 63 75 72 72 65 6e 74 6c 79 ements currently
1b863 20 72 75 6e 6e 69 6e 67 2c 20 74 68 65 6e 0a 20 running, then.
1b864 20 20 20 2a 2a 20 72 65 73 65 74 20 74 68 65 20 ** reset the
1b865 69 6e 74 65 72 72 75 70 74 20 66 6c 61 67 2e 20 interrupt flag.
1b866 20 54 68 69 73 20 70 72 65 76 65 6e 74 73 20 61 This prevents a
1b867 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
1b868 5f 69 6e 74 65 72 72 75 70 74 0a 20 20 20 20 2a _interrupt. *
1b869 2a 20 66 72 6f 6d 20 69 6e 74 65 72 72 75 70 74 * from interrupt
1b86a 69 6e 67 20 61 20 73 74 61 74 65 6d 65 6e 74 20 ing a statement
1b86b 74 68 61 74 20 68 61 73 20 6e 6f 74 20 79 65 74 that has not yet
1b86c 20 73 74 61 72 74 65 64 2e 0a 20 20 20 20 2a 2f started.. */
1b86d 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61 63 74 . if( db->act
1b86e 69 76 65 56 64 62 65 43 6e 74 3d 3d 30 20 29 7b iveVdbeCnt==0 ){
1b86f 0a 20 20 20 20 20 20 64 62 2d 3e 75 31 2e 69 73 . db->u1.is
1b870 49 6e 74 65 72 72 75 70 74 65 64 20 3d 20 30 3b Interrupted = 0;
1b871 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73 65 . }.. asse
1b872 72 74 28 20 64 62 2d 3e 77 72 69 74 65 56 64 62 rt( db->writeVdb
1b873 65 43 6e 74 3e 30 20 7c 7c 20 64 62 2d 3e 61 75 eCnt>0 || db->au
1b874 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c 20 64 toCommit==0 || d
1b875 62 2d 3e 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 b->nDeferredCons
1b876 3d 3d 30 20 29 3b 0a 0a 23 69 66 6e 64 65 66 20 ==0 );..#ifndef
1b877 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 SQLITE_OMIT_TRAC
1b878 45 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 78 50 E. if( db->xP
1b879 72 6f 66 69 6c 65 20 26 26 20 21 64 62 2d 3e 69 rofile && !db->i
1b87a 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 nit.busy ){.
1b87b 20 20 64 6f 75 62 6c 65 20 72 4e 6f 77 3b 0a 20 double rNow;.
1b87c 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 75 sqlite3OsCu
1b87d 72 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e 70 56 rrentTime(db->pV
1b87e 66 73 2c 20 26 72 4e 6f 77 29 3b 0a 20 20 20 20 fs, &rNow);.
1b87f 20 20 70 2d 3e 73 74 61 72 74 54 69 6d 65 20 3d p->startTime =
1b880 20 28 75 36 34 29 28 28 72 4e 6f 77 20 2d 20 28 (u64)((rNow - (
1b881 69 6e 74 29 72 4e 6f 77 29 2a 33 36 30 30 2e 30 int)rNow)*3600.0
1b882 2a 32 34 2e 30 2a 31 30 30 30 30 30 30 30 30 30 *24.0*1000000000
1b883 2e 30 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 .0);. }.#endi
1b884 66 0a 0a 20 20 20 20 64 62 2d 3e 61 63 74 69 76 f.. db->activ
1b885 65 56 64 62 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 eVdbeCnt++;.
1b886 69 66 28 20 70 2d 3e 72 65 61 64 4f 6e 6c 79 3d if( p->readOnly=
1b887 3d 30 20 29 20 64 62 2d 3e 77 72 69 74 65 56 64 =0 ) db->writeVd
1b888 62 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 70 2d 3e beCnt++;. p->
1b889 70 63 20 3d 20 30 3b 0a 20 20 7d 0a 23 69 66 6e pc = 0;. }.#ifn
1b88a 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1b88b 45 58 50 4c 41 49 4e 0a 20 20 69 66 28 20 70 2d EXPLAIN. if( p-
1b88c 3e 65 78 70 6c 61 69 6e 20 29 7b 0a 20 20 20 20 >explain ){.
1b88d 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 rc = sqlite3Vdbe
1b88e 4c 69 73 74 28 70 29 3b 0a 20 20 7d 65 6c 73 65 List(p);. }else
1b88f 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1b890 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 20 2a E_OMIT_EXPLAIN *
1b891 2f 0a 20 20 7b 0a 20 20 20 20 72 63 20 3d 20 73 /. {. rc = s
1b892 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 70 qlite3VdbeExec(p
1b893 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 73 71 );. }.. if( sq
1b894 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 lite3SafetyOff(d
1b895 62 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 b) ){. rc = S
1b896 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 QLITE_MISUSE;.
1b897 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }..#ifndef SQLIT
1b898 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 20 20 2f E_OMIT_TRACE. /
1b899 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 70 72 6f * Invoke the pro
1b89a 66 69 6c 65 20 63 61 6c 6c 62 61 63 6b 20 69 66 file callback if
1b89b 20 74 68 65 72 65 20 69 73 20 6f 6e 65 0a 20 20 there is one.
1b89c 2a 2f 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c */. if( rc!=SQL
1b89d 49 54 45 5f 52 4f 57 20 26 26 20 64 62 2d 3e 78 ITE_ROW && db->x
1b89e 50 72 6f 66 69 6c 65 20 26 26 20 21 64 62 2d 3e Profile && !db->
1b89f 69 6e 69 74 2e 62 75 73 79 20 26 26 20 70 2d 3e init.busy && p->
1b8a0 7a 53 71 6c 20 29 7b 0a 20 20 20 20 64 6f 75 62 zSql ){. doub
1b8a1 6c 65 20 72 4e 6f 77 3b 0a 20 20 20 20 75 36 34 le rNow;. u64
1b8a2 20 65 6c 61 70 73 65 54 69 6d 65 3b 0a 0a 20 20 elapseTime;..
1b8a3 20 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 sqlite3OsCurre
1b8a4 6e 74 54 69 6d 65 28 64 62 2d 3e 70 56 66 73 2c ntTime(db->pVfs,
1b8a5 20 26 72 4e 6f 77 29 3b 0a 20 20 20 20 65 6c 61 &rNow);. ela
1b8a6 70 73 65 54 69 6d 65 20 3d 20 28 75 36 34 29 28 pseTime = (u64)(
1b8a7 28 72 4e 6f 77 20 2d 20 28 69 6e 74 29 72 4e 6f (rNow - (int)rNo
1b8a8 77 29 2a 33 36 30 30 2e 30 2a 32 34 2e 30 2a 31 w)*3600.0*24.0*1
1b8a9 30 30 30 30 30 30 30 30 30 2e 30 29 3b 0a 20 20 000000000.0);.
1b8aa 20 20 65 6c 61 70 73 65 54 69 6d 65 20 2d 3d 20 elapseTime -=
1b8ab 70 2d 3e 73 74 61 72 74 54 69 6d 65 3b 0a 20 20 p->startTime;.
1b8ac 20 20 64 62 2d 3e 78 50 72 6f 66 69 6c 65 28 64 db->xProfile(d
1b8ad 62 2d 3e 70 50 72 6f 66 69 6c 65 41 72 67 2c 20 b->pProfileArg,
1b8ae 70 2d 3e 7a 53 71 6c 2c 20 65 6c 61 70 73 65 54 p->zSql, elapseT
1b8af 69 6d 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 ime);. }.#endif
1b8b0 0a 0a 20 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 .. db->errCode
1b8b1 3d 20 72 63 3b 0a 20 20 69 66 28 20 53 51 4c 49 = rc;. if( SQLI
1b8b2 54 45 5f 4e 4f 4d 45 4d 3d 3d 73 71 6c 69 74 65 TE_NOMEM==sqlite
1b8b3 33 41 70 69 45 78 69 74 28 70 2d 3e 64 62 2c 20 3ApiExit(p->db,
1b8b4 70 2d 3e 72 63 29 20 29 7b 0a 20 20 20 20 70 2d p->rc) ){. p-
1b8b5 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d >rc = SQLITE_NOM
1b8b6 45 4d 3b 0a 20 20 7d 0a 65 6e 64 5f 6f 66 5f 73 EM;. }.end_of_s
1b8b7 74 65 70 3a 0a 20 20 2f 2a 20 41 74 20 74 68 69 tep:. /* At thi
1b8b8 73 20 70 6f 69 6e 74 20 6c 6f 63 61 6c 20 76 61 s point local va
1b8b9 72 69 61 62 6c 65 20 72 63 20 68 6f 6c 64 73 20 riable rc holds
1b8ba 74 68 65 20 76 61 6c 75 65 20 74 68 61 74 20 73 the value that s
1b8bb 68 6f 75 6c 64 20 62 65 20 0a 20 20 2a 2a 20 72 hould be . ** r
1b8bc 65 74 75 72 6e 65 64 20 69 66 20 74 68 69 73 20 eturned if this
1b8bd 73 74 61 74 65 6d 65 6e 74 20 77 61 73 20 63 6f statement was co
1b8be 6d 70 69 6c 65 64 20 75 73 69 6e 67 20 74 68 65 mpiled using the
1b8bf 20 6c 65 67 61 63 79 20 0a 20 20 2a 2a 20 73 71 legacy . ** sq
1b8c0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 20 lite3_prepare()
1b8c1 69 6e 74 65 72 66 61 63 65 2e 20 41 63 63 6f 72 interface. Accor
1b8c2 64 69 6e 67 20 74 6f 20 74 68 65 20 64 6f 63 73 ding to the docs
1b8c3 2c 20 74 68 69 73 20 63 61 6e 20 6f 6e 6c 79 0a , this can only.
1b8c4 20 20 2a 2a 20 62 65 20 6f 6e 65 20 6f 66 20 74 ** be one of t
1b8c5 68 65 20 76 61 6c 75 65 73 20 69 6e 20 74 68 65 he values in the
1b8c6 20 66 69 72 73 74 20 61 73 73 65 72 74 28 29 20 first assert()
1b8c7 62 65 6c 6f 77 2e 20 56 61 72 69 61 62 6c 65 20 below. Variable
1b8c8 70 2d 3e 72 63 20 0a 20 20 2a 2a 20 63 6f 6e 74 p->rc . ** cont
1b8c9 61 69 6e 73 20 74 68 65 20 76 61 6c 75 65 20 74 ains the value t
1b8ca 68 61 74 20 77 6f 75 6c 64 20 62 65 20 72 65 74 hat would be ret
1b8cb 75 72 6e 65 64 20 69 66 20 73 71 6c 69 74 65 33 urned if sqlite3
1b8cc 5f 66 69 6e 61 6c 69 7a 65 28 29 20 0a 20 20 2a _finalize() . *
1b8cd 2a 20 77 65 72 65 20 63 61 6c 6c 65 64 20 6f 6e * were called on
1b8ce 20 73 74 61 74 65 6d 65 6e 74 20 70 2e 0a 20 20 statement p..
1b8cf 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d */. assert( rc=
1b8d0 3d 53 51 4c 49 54 45 5f 52 4f 57 20 20 7c 7c 20 =SQLITE_ROW ||
1b8d1 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 rc==SQLITE_DONE
1b8d2 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f || rc==SQLITE_
1b8d3 45 52 52 4f 52 20 0a 20 20 20 20 20 20 20 7c 7c ERROR . ||
1b8d4 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 rc==SQLITE_BUSY
1b8d5 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4d || rc==SQLITE_M
1b8d6 49 53 55 53 45 0a 20 20 29 3b 0a 20 20 61 73 73 ISUSE. );. ass
1b8d7 65 72 74 28 20 70 2d 3e 72 63 21 3d 53 51 4c 49 ert( p->rc!=SQLI
1b8d8 54 45 5f 52 4f 57 20 26 26 20 70 2d 3e 72 63 21 TE_ROW && p->rc!
1b8d9 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a =SQLITE_DONE );.
1b8da 20 20 69 66 28 20 70 2d 3e 69 73 50 72 65 70 61 if( p->isPrepa
1b8db 72 65 56 32 20 26 26 20 72 63 21 3d 53 51 4c 49 reV2 && rc!=SQLI
1b8dc 54 45 5f 52 4f 57 20 26 26 20 72 63 21 3d 53 51 TE_ROW && rc!=SQ
1b8dd 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 LITE_DONE ){.
1b8de 20 2f 2a 20 49 66 20 74 68 69 73 20 73 74 61 74 /* If this stat
1b8df 65 6d 65 6e 74 20 77 61 73 20 70 72 65 70 61 72 ement was prepar
1b8e0 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 ed using sqlite3
1b8e1 5f 70 72 65 70 61 72 65 5f 76 32 28 29 2c 20 61 _prepare_v2(), a
1b8e2 6e 64 20 61 6e 0a 20 20 20 20 2a 2a 20 65 72 72 nd an. ** err
1b8e3 6f 72 20 68 61 73 20 6f 63 63 75 72 65 64 2c 20 or has occured,
1b8e4 74 68 65 6e 20 72 65 74 75 72 6e 20 74 68 65 20 then return the
1b8e5 65 72 72 6f 72 20 63 6f 64 65 20 69 6e 20 70 2d error code in p-
1b8e6 3e 72 63 20 74 6f 20 74 68 65 0a 20 20 20 20 2a >rc to the. *
1b8e7 2a 20 63 61 6c 6c 65 72 2e 20 53 65 74 20 74 68 * caller. Set th
1b8e8 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 6e 20 e error code in
1b8e9 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e the database han
1b8ea 64 6c 65 20 74 6f 20 74 68 65 20 73 61 6d 65 20 dle to the same
1b8eb 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2f 20 0a 20 value.. */ .
1b8ec 20 20 20 72 63 20 3d 20 64 62 2d 3e 65 72 72 43 rc = db->errC
1b8ed 6f 64 65 20 3d 20 70 2d 3e 72 63 3b 0a 20 20 7d ode = p->rc;. }
1b8ee 0a 20 20 72 65 74 75 72 6e 20 28 72 63 26 64 62 . return (rc&db
1b8ef 2d 3e 65 72 72 4d 61 73 6b 29 3b 0a 7d 0a 0a 2f ->errMask);.}../
1b8f0 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 *.** This is the
1b8f1 20 74 6f 70 2d 6c 65 76 65 6c 20 69 6d 70 6c 65 top-level imple
1b8f2 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c mentation of sql
1b8f3 69 74 65 33 5f 73 74 65 70 28 29 2e 20 20 43 61 ite3_step(). Ca
1b8f4 6c 6c 0a 2a 2a 20 73 71 6c 69 74 65 33 53 74 65 ll.** sqlite3Ste
1b8f5 70 28 29 20 74 6f 20 64 6f 20 6d 6f 73 74 20 6f p() to do most o
1b8f6 66 20 74 68 65 20 77 6f 72 6b 2e 20 20 49 66 20 f the work. If
1b8f7 61 20 73 63 68 65 6d 61 20 65 72 72 6f 72 20 6f a schema error o
1b8f8 63 63 75 72 73 2c 0a 2a 2a 20 63 61 6c 6c 20 73 ccurs,.** call s
1b8f9 71 6c 69 74 65 33 52 65 70 72 65 70 61 72 65 28 qlite3Reprepare(
1b8fa 29 20 61 6e 64 20 74 72 79 20 61 67 61 69 6e 2e ) and try again.
1b8fb 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
1b8fc 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 nt sqlite3_step(
1b8fd 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
1b8fe 74 6d 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d tmt){. int rc =
1b8ff 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a SQLITE_MISUSE;.
1b900 20 20 69 66 28 20 70 53 74 6d 74 20 29 7b 0a 20 if( pStmt ){.
1b901 20 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a int cnt = 0;.
1b902 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20 28 56 Vdbe *v = (V
1b903 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 20 20 dbe*)pStmt;.
1b904 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 76 2d sqlite3 *db = v-
1b905 3e 64 62 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 >db;. sqlite3
1b906 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d _mutex_enter(db-
1b907 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 77 68 69 >mutex);. whi
1b908 6c 65 28 20 28 72 63 20 3d 20 73 71 6c 69 74 65 le( (rc = sqlite
1b909 33 53 74 65 70 28 76 29 29 3d 3d 53 51 4c 49 54 3Step(v))==SQLIT
1b90a 45 5f 53 43 48 45 4d 41 0a 20 20 20 20 20 20 20 E_SCHEMA.
1b90b 20 20 20 20 26 26 20 63 6e 74 2b 2b 20 3c 20 35 && cnt++ < 5
1b90c 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28 . && (
1b90d 72 63 20 3d 20 73 71 6c 69 74 65 33 52 65 70 72 rc = sqlite3Repr
1b90e 65 70 61 72 65 28 76 29 29 3d 3d 53 51 4c 49 54 epare(v))==SQLIT
1b90f 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 E_OK ){. sq
1b910 6c 69 74 65 33 5f 72 65 73 65 74 28 70 53 74 6d lite3_reset(pStm
1b911 74 29 3b 0a 20 20 20 20 20 20 76 2d 3e 65 78 70 t);. v->exp
1b912 69 72 65 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a ired = 0;. }.
1b913 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
1b914 54 45 5f 53 43 48 45 4d 41 20 26 26 20 41 4c 57 TE_SCHEMA && ALW
1b915 41 59 53 28 76 2d 3e 69 73 50 72 65 70 61 72 65 AYS(v->isPrepare
1b916 56 32 29 20 26 26 20 41 4c 57 41 59 53 28 64 62 V2) && ALWAYS(db
1b917 2d 3e 70 45 72 72 29 20 29 7b 0a 20 20 20 20 20 ->pErr) ){.
1b918 20 2f 2a 20 54 68 69 73 20 63 61 73 65 20 6f 63 /* This case oc
1b919 63 75 72 73 20 61 66 74 65 72 20 66 61 69 6c 69 curs after faili
1b91a 6e 67 20 74 6f 20 72 65 63 6f 6d 70 69 6c 65 20 ng to recompile
1b91b 61 6e 20 73 71 6c 20 73 74 61 74 65 6d 65 6e 74 an sql statement
1b91c 2e 20 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 20 . . ** The
1b91d 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 66 72 error message fr
1b91e 6f 6d 20 74 68 65 20 53 51 4c 20 63 6f 6d 70 69 om the SQL compi
1b91f 6c 65 72 20 68 61 73 20 61 6c 72 65 61 64 79 20 ler has already
1b920 62 65 65 6e 20 6c 6f 61 64 65 64 20 0a 20 20 20 been loaded .
1b921 20 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 64 ** into the d
1b922 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 atabase handle.
1b923 54 68 69 73 20 62 6c 6f 63 6b 20 63 6f 70 69 65 This block copie
1b924 73 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 s the error mess
1b925 61 67 65 20 0a 20 20 20 20 20 20 2a 2a 20 66 72 age . ** fr
1b926 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 om the database
1b927 68 61 6e 64 6c 65 20 69 6e 74 6f 20 74 68 65 20 handle into the
1b928 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 73 65 statement and se
1b929 74 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 ts the statement
1b92a 0a 20 20 20 20 20 20 2a 2a 20 70 72 6f 67 72 61 . ** progra
1b92b 6d 20 63 6f 75 6e 74 65 72 20 74 6f 20 30 20 74 m counter to 0 t
1b92c 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 77 68 o ensure that wh
1b92d 65 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 en the statement
1b92e 20 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 66 69 is . ** fi
1b92f 6e 61 6c 69 7a 65 64 20 6f 72 20 72 65 73 65 74 nalized or reset
1b930 20 74 68 65 20 70 61 72 73 65 72 20 65 72 72 6f the parser erro
1b931 72 20 6d 65 73 73 61 67 65 20 69 73 20 61 76 61 r message is ava
1b932 69 6c 61 62 6c 65 20 76 69 61 0a 20 20 20 20 20 ilable via.
1b933 20 2a 2a 20 73 71 6c 69 74 65 33 5f 65 72 72 6d ** sqlite3_errm
1b934 73 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 sg() and sqlite3
1b935 5f 65 72 72 63 6f 64 65 28 29 2e 0a 20 20 20 20 _errcode()..
1b936 20 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 */. const
1b937 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 28 63 char *zErr = (c
1b938 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 onst char *)sqli
1b939 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 64 te3_value_text(d
1b93a 62 2d 3e 70 45 72 72 29 3b 20 0a 20 20 20 20 20 b->pErr); .
1b93b 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1b93c 62 2c 20 76 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a b, v->zErrMsg);.
1b93d 20 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d if( !db->m
1b93e 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 allocFailed ){.
1b93f 20 20 20 20 20 20 20 76 2d 3e 7a 45 72 72 4d 73 v->zErrMs
1b940 67 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 g = sqlite3DbStr
1b941 44 75 70 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 Dup(db, zErr);.
1b942 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 } else {.
1b943 20 20 20 20 20 20 76 2d 3e 7a 45 72 72 4d 73 67 v->zErrMsg
1b944 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 76 2d = 0;. v-
1b945 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d >rc = SQLITE_NOM
1b946 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 EM;. }.
1b947 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 }. rc = sqlit
1b948 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 e3ApiExit(db, rc
1b949 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d );. sqlite3_m
1b94a 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d utex_leave(db->m
1b94b 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 utex);. }. ret
1b94c 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
1b94d 20 45 78 74 72 61 63 74 20 74 68 65 20 75 73 65 Extract the use
1b94e 72 20 64 61 74 61 20 66 72 6f 6d 20 61 20 73 71 r data from a sq
1b94f 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 73 74 lite3_context st
1b950 72 75 63 74 75 72 65 20 61 6e 64 20 72 65 74 75 ructure and retu
1b951 72 6e 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 rn a.** pointer
1b952 74 6f 20 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 to it..*/.SQLITE
1b953 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 _API void *sqlit
1b954 65 33 5f 75 73 65 72 5f 64 61 74 61 28 73 71 6c e3_user_data(sql
1b955 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 29 ite3_context *p)
1b956 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 26 26 {. assert( p &&
1b957 20 70 2d 3e 70 46 75 6e 63 20 29 3b 0a 20 20 72 p->pFunc );. r
1b958 65 74 75 72 6e 20 70 2d 3e 70 46 75 6e 63 2d 3e eturn p->pFunc->
1b959 70 55 73 65 72 44 61 74 61 3b 0a 7d 0a 0a 2f 2a pUserData;.}../*
1b95a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 .** Extract the
1b95b 75 73 65 72 20 64 61 74 61 20 66 72 6f 6d 20 61 user data from a
1b95c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
1b95d 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 72 structure and r
1b95e 65 74 75 72 6e 20 61 0a 2a 2a 20 70 6f 69 6e 74 eturn a.** point
1b95f 65 72 20 74 6f 20 69 74 2e 0a 2a 2f 0a 53 51 4c er to it..*/.SQL
1b960 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 20 ITE_API sqlite3
1b961 2a 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 *sqlite3_context
1b962 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 6c 69 74 _db_handle(sqlit
1b963 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 29 7b 0a e3_context *p){.
1b964 20 20 61 73 73 65 72 74 28 20 70 20 26 26 20 70 assert( p && p
1b965 2d 3e 70 46 75 6e 63 20 29 3b 0a 20 20 72 65 74 ->pFunc );. ret
1b966 75 72 6e 20 70 2d 3e 73 2e 64 62 3b 0a 7d 0a 0a urn p->s.db;.}..
1b967 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
1b968 69 6e 67 20 69 73 20 74 68 65 20 69 6d 70 6c 65 ing is the imple
1b969 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20 mentation of an
1b96a 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 SQL function tha
1b96b 74 20 61 6c 77 61 79 73 0a 2a 2a 20 66 61 69 6c t always.** fail
1b96c 73 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 s with an error
1b96d 6d 65 73 73 61 67 65 20 73 74 61 74 69 6e 67 20 message stating
1b96e 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 69 6f that the functio
1b96f 6e 20 69 73 20 75 73 65 64 20 69 6e 20 74 68 65 n is used in the
1b970 0a 2a 2a 20 77 72 6f 6e 67 20 63 6f 6e 74 65 78 .** wrong contex
1b971 74 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f t. The sqlite3_
1b972 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f overload_functio
1b973 6e 28 29 20 41 50 49 20 6d 69 67 68 74 20 63 6f n() API might co
1b974 6e 73 74 72 75 63 74 0a 2a 2a 20 53 51 4c 20 66 nstruct.** SQL f
1b975 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 75 73 65 unction that use
1b976 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73 6f this routine so
1b977 20 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 69 that the functi
1b978 6f 6e 73 20 77 69 6c 6c 20 65 78 69 73 74 0a 2a ons will exist.*
1b979 2a 20 66 6f 72 20 6e 61 6d 65 20 72 65 73 6f 6c * for name resol
1b97a 75 74 69 6f 6e 20 62 75 74 20 61 72 65 20 61 63 ution but are ac
1b97b 74 75 61 6c 6c 79 20 6f 76 65 72 6c 6f 61 64 65 tually overloade
1b97c 64 20 62 79 20 74 68 65 20 78 46 69 6e 64 46 75 d by the xFindFu
1b97d 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 65 74 68 6f 64 nction.** method
1b97e 20 6f 66 20 76 69 72 74 75 61 6c 20 74 61 62 6c of virtual tabl
1b97f 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 es..*/.SQLITE_PR
1b980 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1b981 65 33 49 6e 76 61 6c 69 64 46 75 6e 63 74 69 6f e3InvalidFunctio
1b982 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e n(. sqlite3_con
1b983 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 20 text *context,
1b984 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 /* The function
1b985 63 61 6c 6c 69 6e 67 20 63 6f 6e 74 65 78 74 20 calling context
1b986 2a 2f 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 */. int NotUsed
1b987 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
1b988 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67 /* Number of arg
1b989 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 66 75 uments to the fu
1b98a 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 nction */. sqli
1b98b 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 te3_value **NotU
1b98c 73 65 64 32 20 20 20 2f 2a 20 56 61 6c 75 65 20 sed2 /* Value
1b98d 6f 66 20 65 61 63 68 20 61 72 67 75 6d 65 6e 74 of each argument
1b98e 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 */.){. const c
1b98f 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 63 6f 6e har *zName = con
1b990 74 65 78 74 2d 3e 70 46 75 6e 63 2d 3e 7a 4e 61 text->pFunc->zNa
1b991 6d 65 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 me;. char *zErr
1b992 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d ;. UNUSED_PARAM
1b993 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e ETER2(NotUsed, N
1b994 6f 74 55 73 65 64 32 29 3b 0a 20 20 7a 45 72 72 otUsed2);. zErr
1b995 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e = sqlite3_mprin
1b996 74 66 28 0a 20 20 20 20 20 20 22 75 6e 61 62 6c tf(. "unabl
1b997 65 20 74 6f 20 75 73 65 20 66 75 6e 63 74 69 6f e to use functio
1b998 6e 20 25 73 20 69 6e 20 74 68 65 20 72 65 71 75 n %s in the requ
1b999 65 73 74 65 64 20 63 6f 6e 74 65 78 74 22 2c 20 ested context",
1b99a 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 zName);. sqlite
1b99b 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 3_result_error(c
1b99c 6f 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d 31 ontext, zErr, -1
1b99d 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 );. sqlite3_fre
1b99e 65 28 7a 45 72 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a e(zErr);.}../*.*
1b99f 2a 20 41 6c 6c 6f 63 61 74 65 20 6f 72 20 72 65 * Allocate or re
1b9a0 74 75 72 6e 20 74 68 65 20 61 67 67 72 65 67 61 turn the aggrega
1b9a1 74 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 61 te context for a
1b9a2 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 2e 20 user function.
1b9a3 20 41 20 6e 65 77 0a 2a 2a 20 63 6f 6e 74 65 78 A new.** contex
1b9a4 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 6f t is allocated o
1b9a5 6e 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c n the first call
1b9a6 2e 20 20 53 75 62 73 65 71 75 65 6e 74 20 63 61 . Subsequent ca
1b9a7 6c 6c 73 20 72 65 74 75 72 6e 20 74 68 65 0a 2a lls return the.*
1b9a8 2a 20 73 61 6d 65 20 63 6f 6e 74 65 78 74 20 74 * same context t
1b9a9 68 61 74 20 77 61 73 20 72 65 74 75 72 6e 65 64 hat was returned
1b9aa 20 6f 6e 20 70 72 69 6f 72 20 63 61 6c 6c 73 2e on prior calls.
1b9ab 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 .*/.SQLITE_API v
1b9ac 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 61 67 67 oid *sqlite3_agg
1b9ad 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 73 regate_context(s
1b9ae 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
1b9af 70 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 p, int nByte){.
1b9b0 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 61 73 Mem *pMem;. as
1b9b1 73 65 72 74 28 20 70 20 26 26 20 70 2d 3e 70 46 sert( p && p->pF
1b9b2 75 6e 63 20 26 26 20 70 2d 3e 70 46 75 6e 63 2d unc && p->pFunc-
1b9b3 3e 78 53 74 65 70 20 29 3b 0a 20 20 61 73 73 65 >xStep );. asse
1b9b4 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
1b9b5 78 5f 68 65 6c 64 28 70 2d 3e 73 2e 64 62 2d 3e x_held(p->s.db->
1b9b6 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 4d 65 6d mutex) );. pMem
1b9b7 20 3d 20 70 2d 3e 70 4d 65 6d 3b 0a 20 20 69 66 = p->pMem;. if
1b9b8 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 ( (pMem->flags &
1b9b9 20 4d 45 4d 5f 41 67 67 29 3d 3d 30 20 29 7b 0a MEM_Agg)==0 ){.
1b9ba 20 20 20 20 69 66 28 20 6e 42 79 74 65 3d 3d 30 if( nByte==0
1b9bb 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1b9bc 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 3VdbeMemReleaseE
1b9bd 78 74 65 72 6e 61 6c 28 70 4d 65 6d 29 3b 0a 20 xternal(pMem);.
1b9be 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 pMem->flags
1b9bf 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 = MEM_Null;.
1b9c0 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 30 3b 0a pMem->z = 0;.
1b9c1 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1b9c2 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 sqlite3VdbeMemG
1b9c3 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c row(pMem, nByte,
1b9c4 20 30 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 0);. pMem-
1b9c5 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 41 67 67 >flags = MEM_Agg
1b9c6 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e ;. pMem->u.
1b9c7 70 44 65 66 20 3d 20 70 2d 3e 70 46 75 6e 63 3b pDef = p->pFunc;
1b9c8 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d 2d . if( pMem-
1b9c9 3e 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 >z ){. me
1b9ca 6d 73 65 74 28 70 4d 65 6d 2d 3e 7a 2c 20 30 2c mset(pMem->z, 0,
1b9cb 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 7d nByte);. }
1b9cc 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
1b9cd 75 72 6e 20 28 76 6f 69 64 2a 29 70 4d 65 6d 2d urn (void*)pMem-
1b9ce 3e 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 >z;.}../*.** Ret
1b9cf 75 72 6e 20 74 68 65 20 61 75 78 69 6c 61 72 79 urn the auxilary
1b9d0 20 64 61 74 61 20 70 6f 69 6e 74 65 72 2c 20 69 data pointer, i
1b9d1 66 20 61 6e 79 2c 20 66 6f 72 20 74 68 65 20 69 f any, for the i
1b9d2 41 72 67 27 74 68 20 61 72 67 75 6d 65 6e 74 20 Arg'th argument
1b9d3 74 6f 0a 2a 2a 20 74 68 65 20 75 73 65 72 2d 66 to.** the user-f
1b9d4 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 unction defined
1b9d5 62 79 20 70 43 74 78 2e 0a 2a 2f 0a 53 51 4c 49 by pCtx..*/.SQLI
1b9d6 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c TE_API void *sql
1b9d7 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 ite3_get_auxdata
1b9d8 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
1b9d9 20 2a 70 43 74 78 2c 20 69 6e 74 20 69 41 72 67 *pCtx, int iArg
1b9da 29 7b 0a 20 20 56 64 62 65 46 75 6e 63 20 2a 70 ){. VdbeFunc *p
1b9db 56 64 62 65 46 75 6e 63 3b 0a 0a 20 20 61 73 73 VdbeFunc;.. ass
1b9dc 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
1b9dd 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e ex_held(pCtx->s.
1b9de 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
1b9df 70 56 64 62 65 46 75 6e 63 20 3d 20 70 43 74 78 pVdbeFunc = pCtx
1b9e0 2d 3e 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 69 ->pVdbeFunc;. i
1b9e1 66 28 20 21 70 56 64 62 65 46 75 6e 63 20 7c 7c f( !pVdbeFunc ||
1b9e2 20 69 41 72 67 3e 3d 70 56 64 62 65 46 75 6e 63 iArg>=pVdbeFunc
1b9e3 2d 3e 6e 41 75 78 20 7c 7c 20 69 41 72 67 3c 30 ->nAux || iArg<0
1b9e4 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 ){. return 0
1b9e5 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 ;. }. return p
1b9e6 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 75 78 5b VdbeFunc->apAux[
1b9e7 69 41 72 67 5d 2e 70 41 75 78 3b 0a 7d 0a 0a 2f iArg].pAux;.}../
1b9e8 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 61 75 78 *.** Set the aux
1b9e9 69 6c 61 72 79 20 64 61 74 61 20 70 6f 69 6e 74 ilary data point
1b9ea 65 72 20 61 6e 64 20 64 65 6c 65 74 65 20 66 75 er and delete fu
1b9eb 6e 63 74 69 6f 6e 2c 20 66 6f 72 20 74 68 65 20 nction, for the
1b9ec 69 41 72 67 27 74 68 0a 2a 2a 20 61 72 67 75 6d iArg'th.** argum
1b9ed 65 6e 74 20 74 6f 20 74 68 65 20 75 73 65 72 2d ent to the user-
1b9ee 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 65 64 function defined
1b9ef 20 62 79 20 70 43 74 78 2e 20 41 6e 79 20 70 72 by pCtx. Any pr
1b9f0 65 76 69 6f 75 73 20 76 61 6c 75 65 20 69 73 0a evious value is.
1b9f1 2a 2a 20 64 65 6c 65 74 65 64 20 62 79 20 63 61 ** deleted by ca
1b9f2 6c 6c 69 6e 67 20 74 68 65 20 64 65 6c 65 74 65 lling the delete
1b9f3 20 66 75 6e 63 74 69 6f 6e 20 73 70 65 63 69 66 function specif
1b9f4 69 65 64 20 77 68 65 6e 20 69 74 20 77 61 73 20 ied when it was
1b9f5 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 set..*/.SQLITE_A
1b9f6 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
1b9f7 73 65 74 5f 61 75 78 64 61 74 61 28 0a 20 20 73 set_auxdata(. s
1b9f8 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
1b9f9 70 43 74 78 2c 20 0a 20 20 69 6e 74 20 69 41 72 pCtx, . int iAr
1b9fa 67 2c 20 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 g, . void *pAux
1b9fb 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c , . void (*xDel
1b9fc 65 74 65 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20 ete)(void*).){.
1b9fd 20 73 74 72 75 63 74 20 41 75 78 44 61 74 61 20 struct AuxData
1b9fe 2a 70 41 75 78 44 61 74 61 3b 0a 20 20 56 64 62 *pAuxData;. Vdb
1b9ff 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 eFunc *pVdbeFunc
1ba00 3b 0a 20 20 69 66 28 20 69 41 72 67 3c 30 20 29 ;. if( iArg<0 )
1ba01 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 0a 20 goto failed;..
1ba02 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1ba03 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 _mutex_held(pCtx
1ba04 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 ->s.db->mutex) )
1ba05 3b 0a 20 20 70 56 64 62 65 46 75 6e 63 20 3d 20 ;. pVdbeFunc =
1ba06 70 43 74 78 2d 3e 70 56 64 62 65 46 75 6e 63 3b pCtx->pVdbeFunc;
1ba07 0a 20 20 69 66 28 20 21 70 56 64 62 65 46 75 6e . if( !pVdbeFun
1ba08 63 20 7c 7c 20 70 56 64 62 65 46 75 6e 63 2d 3e c || pVdbeFunc->
1ba09 6e 41 75 78 3c 3d 69 41 72 67 20 29 7b 0a 20 20 nAux<=iArg ){.
1ba0a 20 20 69 6e 74 20 6e 41 75 78 20 3d 20 28 70 56 int nAux = (pV
1ba0b 64 62 65 46 75 6e 63 20 3f 20 70 56 64 62 65 46 dbeFunc ? pVdbeF
1ba0c 75 6e 63 2d 3e 6e 41 75 78 20 3a 20 30 29 3b 0a unc->nAux : 0);.
1ba0d 20 20 20 20 69 6e 74 20 6e 4d 61 6c 6c 6f 63 20 int nMalloc
1ba0e 3d 20 73 69 7a 65 6f 66 28 56 64 62 65 46 75 6e = sizeof(VdbeFun
1ba0f 63 29 20 2b 20 73 69 7a 65 6f 66 28 73 74 72 75 c) + sizeof(stru
1ba10 63 74 20 41 75 78 44 61 74 61 29 2a 69 41 72 67 ct AuxData)*iArg
1ba11 3b 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63 20 ;. pVdbeFunc
1ba12 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c = sqlite3DbReall
1ba13 6f 63 28 70 43 74 78 2d 3e 73 2e 64 62 2c 20 70 oc(pCtx->s.db, p
1ba14 56 64 62 65 46 75 6e 63 2c 20 6e 4d 61 6c 6c 6f VdbeFunc, nMallo
1ba15 63 29 3b 0a 20 20 20 20 69 66 28 20 21 70 56 64 c);. if( !pVd
1ba16 62 65 46 75 6e 63 20 29 7b 0a 20 20 20 20 20 20 beFunc ){.
1ba17 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 goto failed;.
1ba18 20 7d 0a 20 20 20 20 70 43 74 78 2d 3e 70 56 64 }. pCtx->pVd
1ba19 62 65 46 75 6e 63 20 3d 20 70 56 64 62 65 46 75 beFunc = pVdbeFu
1ba1a 6e 63 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 nc;. memset(&
1ba1b 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 75 78 pVdbeFunc->apAux
1ba1c 5b 6e 41 75 78 5d 2c 20 30 2c 20 73 69 7a 65 6f [nAux], 0, sizeo
1ba1d 66 28 73 74 72 75 63 74 20 41 75 78 44 61 74 61 f(struct AuxData
1ba1e 29 2a 28 69 41 72 67 2b 31 2d 6e 41 75 78 29 29 )*(iArg+1-nAux))
1ba1f 3b 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63 2d ;. pVdbeFunc-
1ba20 3e 6e 41 75 78 20 3d 20 69 41 72 67 2b 31 3b 0a >nAux = iArg+1;.
1ba21 20 20 20 20 70 56 64 62 65 46 75 6e 63 2d 3e 70 pVdbeFunc->p
1ba22 46 75 6e 63 20 3d 20 70 43 74 78 2d 3e 70 46 75 Func = pCtx->pFu
1ba23 6e 63 3b 0a 20 20 7d 0a 0a 20 20 70 41 75 78 44 nc;. }.. pAuxD
1ba24 61 74 61 20 3d 20 26 70 56 64 62 65 46 75 6e 63 ata = &pVdbeFunc
1ba25 2d 3e 61 70 41 75 78 5b 69 41 72 67 5d 3b 0a 20 ->apAux[iArg];.
1ba26 20 69 66 28 20 70 41 75 78 44 61 74 61 2d 3e 70 if( pAuxData->p
1ba27 41 75 78 20 26 26 20 70 41 75 78 44 61 74 61 2d Aux && pAuxData-
1ba28 3e 78 44 65 6c 65 74 65 20 29 7b 0a 20 20 20 20 >xDelete ){.
1ba29 70 41 75 78 44 61 74 61 2d 3e 78 44 65 6c 65 74 pAuxData->xDelet
1ba2a 65 28 70 41 75 78 44 61 74 61 2d 3e 70 41 75 78 e(pAuxData->pAux
1ba2b 29 3b 0a 20 20 7d 0a 20 20 70 41 75 78 44 61 74 );. }. pAuxDat
1ba2c 61 2d 3e 70 41 75 78 20 3d 20 70 41 75 78 3b 0a a->pAux = pAux;.
1ba2d 20 20 70 41 75 78 44 61 74 61 2d 3e 78 44 65 6c pAuxData->xDel
1ba2e 65 74 65 20 3d 20 78 44 65 6c 65 74 65 3b 0a 20 ete = xDelete;.
1ba2f 20 72 65 74 75 72 6e 3b 0a 0a 66 61 69 6c 65 64 return;..failed
1ba30 3a 0a 20 20 69 66 28 20 78 44 65 6c 65 74 65 20 :. if( xDelete
1ba31 29 7b 0a 20 20 20 20 78 44 65 6c 65 74 65 28 70 ){. xDelete(p
1ba32 41 75 78 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 Aux);. }.}..#if
1ba33 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1ba34 5f 44 45 50 52 45 43 41 54 45 44 0a 2f 2a 0a 2a _DEPRECATED./*.*
1ba35 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d * Return the num
1ba36 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 65 ber of times the
1ba37 20 53 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 6f Step function o
1ba38 66 20 61 20 61 67 67 72 65 67 61 74 65 20 68 61 f a aggregate ha
1ba39 73 20 62 65 65 6e 20 0a 2a 2a 20 63 61 6c 6c 65 s been .** calle
1ba3a 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 d..**.** This fu
1ba3b 6e 63 74 69 6f 6e 20 69 73 20 64 65 70 72 65 63 nction is deprec
1ba3c 61 74 65 64 2e 20 20 44 6f 20 6e 6f 74 20 75 73 ated. Do not us
1ba3d 65 20 69 74 20 66 6f 72 20 6e 65 77 20 63 6f 64 e it for new cod
1ba3e 65 2e 20 20 49 74 20 69 73 0a 2a 2a 20 70 72 6f e. It is.** pro
1ba3f 76 69 64 65 20 6f 6e 6c 79 20 74 6f 20 61 76 6f vide only to avo
1ba40 69 64 20 62 72 65 61 6b 69 6e 67 20 6c 65 67 61 id breaking lega
1ba41 63 79 20 63 6f 64 65 2e 20 20 4e 65 77 20 61 67 cy code. New ag
1ba42 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e gregate function
1ba43 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 .** implementati
1ba44 6f 6e 73 20 73 68 6f 75 6c 64 20 6b 65 65 70 20 ons should keep
1ba45 74 68 65 69 72 20 6f 77 6e 20 63 6f 75 6e 74 73 their own counts
1ba46 20 77 69 74 68 69 6e 20 74 68 65 69 72 20 61 67 within their ag
1ba47 67 72 65 67 61 74 65 0a 2a 2a 20 63 6f 6e 74 65 gregate.** conte
1ba48 78 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 xt..*/.SQLITE_AP
1ba49 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 67 I int sqlite3_ag
1ba4a 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 28 73 71 gregate_count(sq
1ba4b 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 lite3_context *p
1ba4c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 26 ){. assert( p &
1ba4d 26 20 70 2d 3e 70 4d 65 6d 20 26 26 20 70 2d 3e & p->pMem && p->
1ba4e 70 46 75 6e 63 20 26 26 20 70 2d 3e 70 46 75 6e pFunc && p->pFun
1ba4f 63 2d 3e 78 53 74 65 70 20 29 3b 0a 20 20 72 65 c->xStep );. re
1ba50 74 75 72 6e 20 70 2d 3e 70 4d 65 6d 2d 3e 6e 3b turn p->pMem->n;
1ba51 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a .}.#endif../*.**
1ba52 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
1ba53 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e er of columns in
1ba54 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 the result set
1ba55 66 6f 72 20 74 68 65 20 73 74 61 74 65 6d 65 6e for the statemen
1ba56 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 t pStmt..*/.SQLI
1ba57 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
1ba58 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 e3_column_count(
1ba59 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
1ba5a 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 tmt){. Vdbe *pV
1ba5b 6d 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d m = (Vdbe *)pStm
1ba5c 74 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 6d 20 t;. return pVm
1ba5d 3f 20 70 56 6d 2d 3e 6e 52 65 73 43 6f 6c 75 6d ? pVm->nResColum
1ba5e 6e 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 n : 0;.}../*.**
1ba5f 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 Return the numbe
1ba60 72 20 6f 66 20 76 61 6c 75 65 73 20 61 76 61 69 r of values avai
1ba61 6c 61 62 6c 65 20 66 72 6f 6d 20 74 68 65 20 63 lable from the c
1ba62 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 urrent row of th
1ba63 65 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 65 e.** currently e
1ba64 78 65 63 75 74 69 6e 67 20 73 74 61 74 65 6d 65 xecuting stateme
1ba65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c nt pStmt..*/.SQL
1ba66 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1ba67 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74 28 73 te3_data_count(s
1ba68 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1ba69 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 6d mt){. Vdbe *pVm
1ba6a 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 = (Vdbe *)pStmt
1ba6b 3b 0a 20 20 69 66 28 20 70 56 6d 3d 3d 30 20 7c ;. if( pVm==0 |
1ba6c 7c 20 70 56 6d 2d 3e 70 52 65 73 75 6c 74 53 65 | pVm->pResultSe
1ba6d 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b t==0 ) return 0;
1ba6e 0a 20 20 72 65 74 75 72 6e 20 70 56 6d 2d 3e 6e . return pVm->n
1ba6f 52 65 73 43 6f 6c 75 6d 6e 3b 0a 7d 0a 0a 0a 2f ResColumn;.}.../
1ba70 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 *.** Check to se
1ba71 65 20 69 66 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c e if column iCol
1ba72 20 6f 66 20 74 68 65 20 67 69 76 65 6e 20 73 74 of the given st
1ba73 61 74 65 6d 65 6e 74 20 69 73 20 76 61 6c 69 64 atement is valid
1ba74 2e 20 20 49 66 0a 2a 2a 20 69 74 20 69 73 2c 20 . If.** it is,
1ba75 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 return a pointer
1ba76 20 74 6f 20 74 68 65 20 4d 65 6d 20 66 6f 72 20 to the Mem for
1ba77 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 61 the value of tha
1ba78 74 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 49 66 20 t column..** If
1ba79 69 43 6f 6c 20 69 73 20 6e 6f 74 20 76 61 6c 69 iCol is not vali
1ba7a 64 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e d, return a poin
1ba7b 74 65 72 20 74 6f 20 61 20 4d 65 6d 20 77 68 69 ter to a Mem whi
1ba7c 63 68 20 68 61 73 20 61 20 76 61 6c 75 65 0a 2a ch has a value.*
1ba7d 2a 20 6f 66 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 * of NULL..*/.st
1ba7e 61 74 69 63 20 4d 65 6d 20 2a 63 6f 6c 75 6d 6e atic Mem *column
1ba7f 4d 65 6d 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 Mem(sqlite3_stmt
1ba80 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b *pStmt, int i){
1ba81 0a 20 20 56 64 62 65 20 2a 70 56 6d 3b 0a 20 20 . Vdbe *pVm;.
1ba82 69 6e 74 20 76 61 6c 73 3b 0a 20 20 4d 65 6d 20 int vals;. Mem
1ba83 2a 70 4f 75 74 3b 0a 0a 20 20 70 56 6d 20 3d 20 *pOut;.. pVm =
1ba84 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 (Vdbe *)pStmt;.
1ba85 20 69 66 28 20 70 56 6d 20 26 26 20 70 56 6d 2d if( pVm && pVm-
1ba86 3e 70 52 65 73 75 6c 74 53 65 74 21 3d 30 20 26 >pResultSet!=0 &
1ba87 26 20 69 3c 70 56 6d 2d 3e 6e 52 65 73 43 6f 6c & i<pVm->nResCol
1ba88 75 6d 6e 20 26 26 20 69 3e 3d 30 20 29 7b 0a 20 umn && i>=0 ){.
1ba89 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
1ba8a 5f 65 6e 74 65 72 28 70 56 6d 2d 3e 64 62 2d 3e _enter(pVm->db->
1ba8b 6d 75 74 65 78 29 3b 0a 20 20 20 20 76 61 6c 73 mutex);. vals
1ba8c 20 3d 20 73 71 6c 69 74 65 33 5f 64 61 74 61 5f = sqlite3_data_
1ba8d 63 6f 75 6e 74 28 70 53 74 6d 74 29 3b 0a 20 20 count(pStmt);.
1ba8e 20 20 70 4f 75 74 20 3d 20 26 70 56 6d 2d 3e 70 pOut = &pVm->p
1ba8f 52 65 73 75 6c 74 53 65 74 5b 69 5d 3b 0a 20 20 ResultSet[i];.
1ba90 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 }else{. /* If
1ba91 20 74 68 65 20 76 61 6c 75 65 20 70 61 73 73 65 the value passe
1ba92 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 d as the second
1ba93 61 72 67 75 6d 65 6e 74 20 69 73 20 6f 75 74 20 argument is out
1ba94 6f 66 20 72 61 6e 67 65 2c 20 72 65 74 75 72 6e of range, return
1ba95 0a 20 20 20 20 2a 2a 20 61 20 70 6f 69 6e 74 65 . ** a pointe
1ba96 72 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 r to the followi
1ba97 6e 67 20 73 74 61 74 69 63 20 4d 65 6d 20 6f 62 ng static Mem ob
1ba98 6a 65 63 74 20 77 68 69 63 68 20 63 6f 6e 74 61 ject which conta
1ba99 69 6e 73 20 74 68 65 0a 20 20 20 20 2a 2a 20 76 ins the. ** v
1ba9a 61 6c 75 65 20 53 51 4c 20 4e 55 4c 4c 2e 20 45 alue SQL NULL. E
1ba9b 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65 20 4d ven though the M
1ba9c 65 6d 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e em structure con
1ba9d 74 61 69 6e 73 20 61 6e 20 65 6c 65 6d 65 6e 74 tains an element
1ba9e 0a 20 20 20 20 2a 2a 20 6f 66 20 74 79 70 65 20 . ** of type
1ba9f 69 36 34 2c 20 6f 6e 20 63 65 72 74 61 69 6e 20 i64, on certain
1baa0 61 72 63 68 69 74 65 63 74 75 72 65 20 28 78 38 architecture (x8
1baa1 36 29 20 77 69 74 68 20 63 65 72 74 61 69 6e 20 6) with certain
1baa2 63 6f 6d 70 69 6c 65 72 0a 20 20 20 20 2a 2a 20 compiler. **
1baa3 73 77 69 74 63 68 65 73 20 28 2d 4f 73 29 2c 20 switches (-Os),
1baa4 67 63 63 20 6d 61 79 20 61 6c 69 67 6e 20 74 68 gcc may align th
1baa5 69 73 20 4d 65 6d 20 6f 62 6a 65 63 74 20 6f 6e is Mem object on
1baa6 20 61 20 34 2d 62 79 74 65 20 62 6f 75 6e 64 61 a 4-byte bounda
1baa7 72 79 0a 20 20 20 20 2a 2a 20 69 6e 73 74 65 61 ry. ** instea
1baa8 64 20 6f 66 20 61 6e 20 38 2d 62 79 74 65 20 6f d of an 8-byte o
1baa9 6e 65 2e 20 54 68 69 73 20 61 6c 6c 20 77 6f 72 ne. This all wor
1baaa 6b 73 20 66 69 6e 65 2c 20 65 78 63 65 70 74 20 ks fine, except
1baab 74 68 61 74 20 77 68 65 6e 0a 20 20 20 20 2a 2a that when. **
1baac 20 72 75 6e 6e 69 6e 67 20 77 69 74 68 20 53 51 running with SQ
1baad 4c 49 54 45 5f 44 45 42 55 47 20 64 65 66 69 6e LITE_DEBUG defin
1baae 65 64 20 74 68 65 20 53 51 4c 69 74 65 20 63 6f ed the SQLite co
1baaf 64 65 20 73 6f 6d 65 74 69 6d 65 73 20 61 73 73 de sometimes ass
1bab0 65 72 74 28 29 73 0a 20 20 20 20 2a 2a 20 74 68 ert()s. ** th
1bab1 61 74 20 61 20 4d 65 6d 20 73 74 72 75 63 74 75 at a Mem structu
1bab2 72 65 20 69 73 20 6c 6f 63 61 74 65 64 20 6f 6e re is located on
1bab3 20 61 6e 20 38 2d 62 79 74 65 20 62 6f 75 6e 64 an 8-byte bound
1bab4 61 72 79 2e 20 54 6f 20 70 72 65 76 65 6e 74 0a ary. To prevent.
1bab5 20 20 20 20 2a 2a 20 74 68 69 73 20 61 73 73 65 ** this asse
1bab6 72 74 28 29 20 66 72 6f 6d 20 66 61 69 6c 69 6e rt() from failin
1bab7 67 2c 20 77 68 65 6e 20 62 75 69 6c 64 69 6e 67 g, when building
1bab8 20 77 69 74 68 20 53 51 4c 49 54 45 5f 44 45 42 with SQLITE_DEB
1bab9 55 47 20 64 65 66 69 6e 65 64 0a 20 20 20 20 2a UG defined. *
1baba 2a 20 75 73 69 6e 67 20 67 63 63 2c 20 66 6f 72 * using gcc, for
1babb 63 65 20 6e 75 6c 6c 4d 65 6d 20 74 6f 20 62 65 ce nullMem to be
1babc 20 38 2d 62 79 74 65 20 61 6c 69 67 6e 65 64 20 8-byte aligned
1babd 75 73 69 6e 67 20 74 68 65 20 6d 61 67 69 63 61 using the magica
1babe 6c 0a 20 20 20 20 2a 2a 20 5f 5f 61 74 74 72 69 l. ** __attri
1babf 62 75 74 65 5f 5f 28 28 61 6c 69 67 6e 65 64 28 bute__((aligned(
1bac0 38 29 29 29 20 6d 61 63 72 6f 2e 20 20 2a 2f 0a 8))) macro. */.
1bac1 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 static const
1bac2 20 4d 65 6d 20 6e 75 6c 6c 4d 65 6d 20 0a 23 69 Mem nullMem .#i
1bac3 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
1bac4 5f 44 45 42 55 47 29 20 26 26 20 64 65 66 69 6e _DEBUG) && defin
1bac5 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 20 20 20 ed(__GNUC__).
1bac6 20 20 20 5f 5f 61 74 74 72 69 62 75 74 65 5f 5f __attribute__
1bac7 28 28 61 6c 69 67 6e 65 64 28 38 29 29 29 20 0a ((aligned(8))) .
1bac8 23 65 6e 64 69 66 0a 20 20 20 20 20 20 3d 20 7b #endif. = {
1bac9 7b 30 7d 2c 20 28 64 6f 75 62 6c 65 29 30 2c 20 {0}, (double)0,
1baca 30 2c 20 22 22 2c 20 30 2c 20 4d 45 4d 5f 4e 75 0, "", 0, MEM_Nu
1bacb 6c 6c 2c 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c ll, SQLITE_NULL,
1bacc 20 30 2c 20 30 2c 20 30 20 7d 3b 0a 0a 20 20 20 0, 0, 0 };..
1bacd 20 69 66 28 20 70 56 6d 20 26 26 20 41 4c 57 41 if( pVm && ALWA
1bace 59 53 28 70 56 6d 2d 3e 64 62 29 20 29 7b 0a 20 YS(pVm->db) ){.
1bacf 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 sqlite3_mut
1bad0 65 78 5f 65 6e 74 65 72 28 70 56 6d 2d 3e 64 62 ex_enter(pVm->db
1bad1 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 ->mutex);.
1bad2 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 56 6d sqlite3Error(pVm
1bad3 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f 52 41 4e ->db, SQLITE_RAN
1bad4 47 45 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 GE, 0);. }.
1bad5 20 20 70 4f 75 74 20 3d 20 28 4d 65 6d 2a 29 26 pOut = (Mem*)&
1bad6 6e 75 6c 6c 4d 65 6d 3b 0a 20 20 7d 0a 20 20 72 nullMem;. }. r
1bad7 65 74 75 72 6e 20 70 4f 75 74 3b 0a 7d 0a 0a 2f eturn pOut;.}../
1bad8 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
1bad9 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 on is called aft
1bada 65 72 20 69 6e 76 6f 6b 69 6e 67 20 61 6e 20 73 er invoking an s
1badb 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 58 58 58 qlite3_value_XXX
1badc 20 66 75 6e 63 74 69 6f 6e 20 6f 6e 20 61 20 0a function on a .
1badd 2a 2a 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 ** column value
1bade 28 69 2e 65 2e 20 61 20 76 61 6c 75 65 20 72 65 (i.e. a value re
1badf 74 75 72 6e 65 64 20 62 79 20 65 76 61 6c 75 61 turned by evalua
1bae0 74 69 6e 67 20 61 6e 20 53 51 4c 20 65 78 70 72 ting an SQL expr
1bae1 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 0a 2a 2a ession in the.**
1bae2 20 73 65 6c 65 63 74 20 6c 69 73 74 20 6f 66 20 select list of
1bae3 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 a SELECT stateme
1bae4 6e 74 29 20 74 68 61 74 20 6d 61 79 20 63 61 75 nt) that may cau
1bae5 73 65 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 se a malloc() fa
1bae6 69 6c 75 72 65 2e 20 49 66 20 0a 2a 2a 20 6d 61 ilure. If .** ma
1bae7 6c 6c 6f 63 28 29 20 68 61 73 20 66 61 69 6c 65 lloc() has faile
1bae8 64 2c 20 74 68 65 20 74 68 72 65 61 64 73 20 6d d, the threads m
1bae9 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 allocFailed flag
1baea 20 69 73 20 63 6c 65 61 72 65 64 20 61 6e 64 20 is cleared and
1baeb 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 63 6f the result.** co
1baec 64 65 20 6f 66 20 73 74 61 74 65 6d 65 6e 74 20 de of statement
1baed 70 53 74 6d 74 20 73 65 74 20 74 6f 20 53 51 4c pStmt set to SQL
1baee 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2a 0a 2a 2a ITE_NOMEM..**.**
1baef 20 53 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 74 Specifically, t
1baf0 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 66 72 his is called fr
1baf1 6f 6d 20 77 69 74 68 69 6e 3a 0a 2a 2a 0a 2a 2a om within:.**.**
1baf2 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c sqlite3_col
1baf3 75 6d 6e 5f 69 6e 74 28 29 0a 2a 2a 20 20 20 20 umn_int().**
1baf4 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
1baf5 69 6e 74 36 34 28 29 0a 2a 2a 20 20 20 20 20 73 int64().** s
1baf6 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 qlite3_column_te
1baf7 78 74 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 xt().** sqli
1baf8 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 te3_column_text1
1baf9 36 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 6().** sqlit
1bafa 65 33 5f 63 6f 6c 75 6d 6e 5f 72 65 61 6c 28 29 e3_column_real()
1bafb 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f .** sqlite3_
1bafc 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 0a 2a column_bytes().*
1bafd 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f * sqlite3_co
1bafe 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 0a 2a lumn_bytes16().*
1baff 2a 0a 2a 2a 20 42 75 74 20 6e 6f 74 20 66 6f 72 *.** But not for
1bb00 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
1bb01 62 6c 6f 62 28 29 2c 20 77 68 69 63 68 20 6e 65 blob(), which ne
1bb02 76 65 72 20 63 61 6c 6c 73 20 6d 61 6c 6c 6f 63 ver calls malloc
1bb03 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ()..*/.static vo
1bb04 69 64 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 id columnMallocF
1bb05 61 69 6c 75 72 65 28 73 71 6c 69 74 65 33 5f 73 ailure(sqlite3_s
1bb06 74 6d 74 20 2a 70 53 74 6d 74 29 0a 7b 0a 20 20 tmt *pStmt).{.
1bb07 2f 2a 20 49 66 20 6d 61 6c 6c 6f 63 28 29 20 66 /* If malloc() f
1bb08 61 69 6c 65 64 20 64 75 72 69 6e 67 20 61 6e 20 ailed during an
1bb09 65 6e 63 6f 64 69 6e 67 20 63 6f 6e 76 65 72 73 encoding convers
1bb0a 69 6f 6e 20 77 69 74 68 69 6e 20 61 6e 0a 20 20 ion within an.
1bb0b 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d ** sqlite3_colum
1bb0c 6e 5f 58 58 58 20 41 50 49 2c 20 74 68 65 6e 20 n_XXX API, then
1bb0d 73 65 74 20 74 68 65 20 72 65 74 75 72 6e 20 63 set the return c
1bb0e 6f 64 65 20 6f 66 20 74 68 65 20 73 74 61 74 65 ode of the state
1bb0f 6d 65 6e 74 20 74 6f 0a 20 20 2a 2a 20 53 51 4c ment to. ** SQL
1bb10 49 54 45 5f 4e 4f 4d 45 4d 2e 20 54 68 65 20 6e ITE_NOMEM. The n
1bb11 65 78 74 20 63 61 6c 6c 20 74 6f 20 5f 73 74 65 ext call to _ste
1bb12 70 28 29 20 28 69 66 20 61 6e 79 29 20 77 69 6c p() (if any) wil
1bb13 6c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f l return SQLITE_
1bb14 45 52 52 4f 52 0a 20 20 2a 2a 20 61 6e 64 20 5f ERROR. ** and _
1bb15 66 69 6e 61 6c 69 7a 65 28 29 20 77 69 6c 6c 20 finalize() will
1bb16 72 65 74 75 72 6e 20 4e 4f 4d 45 4d 2e 0a 20 20 return NOMEM..
1bb17 2a 2f 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 */. Vdbe *p = (
1bb18 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 Vdbe *)pStmt;.
1bb19 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e if( p ){. p->
1bb1a 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 rc = sqlite3ApiE
1bb1b 78 69 74 28 70 2d 3e 64 62 2c 20 70 2d 3e 72 63 xit(p->db, p->rc
1bb1c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d );. sqlite3_m
1bb1d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 utex_leave(p->db
1bb1e 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 7d 0a ->mutex);. }.}.
1bb1f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
1bb20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 ************** s
1bb21 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 20 20 qlite3_column_
1bb22 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bb23 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
1bb24 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
1bb25 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 routines are us
1bb26 65 64 20 74 6f 20 61 63 63 65 73 73 20 65 6c 65 ed to access ele
1bb27 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 63 75 72 ments of the cur
1bb28 72 65 6e 74 20 72 6f 77 0a 2a 2a 20 69 6e 20 74 rent row.** in t
1bb29 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 0a 2a he result set..*
1bb2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e /.SQLITE_API con
1bb2b 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 st void *sqlite3
1bb2c 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 73 71 6c _column_blob(sql
1bb2d 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1bb2e 2c 20 69 6e 74 20 69 29 7b 0a 20 20 63 6f 6e 73 , int i){. cons
1bb2f 74 20 76 6f 69 64 20 2a 76 61 6c 3b 0a 20 20 76 t void *val;. v
1bb30 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c al = sqlite3_val
1bb31 75 65 5f 62 6c 6f 62 28 20 63 6f 6c 75 6d 6e 4d ue_blob( columnM
1bb32 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 em(pStmt,i) );.
1bb33 20 2f 2a 20 45 76 65 6e 20 74 68 6f 75 67 68 20 /* Even though
1bb34 74 68 65 72 65 20 69 73 20 6e 6f 20 65 6e 63 6f there is no enco
1bb35 64 69 6e 67 20 63 6f 6e 76 65 72 73 69 6f 6e 2c ding conversion,
1bb36 20 76 61 6c 75 65 5f 62 6c 6f 62 28 29 20 6d 69 value_blob() mi
1bb37 67 68 74 0a 20 20 2a 2a 20 6e 65 65 64 20 74 6f ght. ** need to
1bb38 20 63 61 6c 6c 20 6d 61 6c 6c 6f 63 28 29 20 74 call malloc() t
1bb39 6f 20 65 78 70 61 6e 64 20 74 68 65 20 72 65 73 o expand the res
1bb3a 75 6c 74 20 6f 66 20 61 20 7a 65 72 6f 62 6c 6f ult of a zeroblo
1bb3b 62 28 29 20 0a 20 20 2a 2a 20 65 78 70 72 65 73 b() . ** expres
1bb3c 73 69 6f 6e 2e 20 0a 20 20 2a 2f 0a 20 20 63 6f sion. . */. co
1bb3d 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 lumnMallocFailur
1bb3e 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 e(pStmt);. retu
1bb3f 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 rn val;.}.SQLITE
1bb40 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
1bb41 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 73 71 _column_bytes(sq
1bb42 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1bb43 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 t, int i){. int
1bb44 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 val = sqlite3_v
1bb45 61 6c 75 65 5f 62 79 74 65 73 28 20 63 6f 6c 75 alue_bytes( colu
1bb46 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 mnMem(pStmt,i) )
1bb47 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 ;. columnMalloc
1bb48 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a Failure(pStmt);.
1bb49 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a return val;.}.
1bb4a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
1bb4b 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 qlite3_column_by
1bb4c 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f 73 74 tes16(sqlite3_st
1bb4d 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 mt *pStmt, int i
1bb4e 29 7b 0a 20 20 69 6e 74 20 76 61 6c 20 3d 20 73 ){. int val = s
1bb4f 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 qlite3_value_byt
1bb50 65 73 31 36 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 es16( columnMem(
1bb51 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f pStmt,i) );. co
1bb52 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 lumnMallocFailur
1bb53 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 e(pStmt);. retu
1bb54 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 rn val;.}.SQLITE
1bb55 5f 41 50 49 20 64 6f 75 62 6c 65 20 73 71 6c 69 _API double sqli
1bb56 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c te3_column_doubl
1bb57 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a e(sqlite3_stmt *
1bb58 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 pStmt, int i){.
1bb59 20 64 6f 75 62 6c 65 20 76 61 6c 20 3d 20 73 71 double val = sq
1bb5a 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 lite3_value_doub
1bb5b 6c 65 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 le( columnMem(pS
1bb5c 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 tmt,i) );. colu
1bb5d 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 mnMallocFailure(
1bb5e 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e pStmt);. return
1bb5f 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 val;.}.SQLITE_A
1bb60 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 PI int sqlite3_c
1bb61 6f 6c 75 6d 6e 5f 69 6e 74 28 73 71 6c 69 74 65 olumn_int(sqlite
1bb62 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 3_stmt *pStmt, i
1bb63 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 76 61 6c nt i){. int val
1bb64 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 = sqlite3_value
1bb65 5f 69 6e 74 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 _int( columnMem(
1bb66 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f pStmt,i) );. co
1bb67 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 lumnMallocFailur
1bb68 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 e(pStmt);. retu
1bb69 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 rn val;.}.SQLITE
1bb6a 5f 41 50 49 20 73 71 6c 69 74 65 5f 69 6e 74 36 _API sqlite_int6
1bb6b 34 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 4 sqlite3_column
1bb6c 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 73 _int64(sqlite3_s
1bb6d 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 tmt *pStmt, int
1bb6e 69 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 i){. sqlite_int
1bb6f 36 34 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 64 val = sqlite3
1bb70 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 20 63 6f _value_int64( co
1bb71 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 lumnMem(pStmt,i)
1bb72 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c );. columnMall
1bb73 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 ocFailure(pStmt)
1bb74 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a ;. return val;.
1bb75 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e }.SQLITE_API con
1bb76 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
1bb77 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e *sqlite3_column
1bb78 5f 74 65 78 74 28 73 71 6c 69 74 65 33 5f 73 74 _text(sqlite3_st
1bb79 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 mt *pStmt, int i
1bb7a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 ){. const unsig
1bb7b 6e 65 64 20 63 68 61 72 20 2a 76 61 6c 20 3d 20 ned char *val =
1bb7c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
1bb7d 78 74 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 xt( columnMem(pS
1bb7e 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 tmt,i) );. colu
1bb7f 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 mnMallocFailure(
1bb80 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e pStmt);. return
1bb81 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 val;.}.SQLITE_A
1bb82 50 49 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 PI sqlite3_value
1bb83 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e *sqlite3_column
1bb84 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 73 _value(sqlite3_s
1bb85 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 tmt *pStmt, int
1bb86 69 29 7b 0a 20 20 4d 65 6d 20 2a 70 4f 75 74 20 i){. Mem *pOut
1bb87 3d 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d = columnMem(pStm
1bb88 74 2c 20 69 29 3b 0a 20 20 69 66 28 20 70 4f 75 t, i);. if( pOu
1bb89 74 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 61 t->flags&MEM_Sta
1bb8a 74 69 63 20 29 7b 0a 20 20 20 20 70 4f 75 74 2d tic ){. pOut-
1bb8b 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d 5f 53 >flags &= ~MEM_S
1bb8c 74 61 74 69 63 3b 0a 20 20 20 20 70 4f 75 74 2d tatic;. pOut-
1bb8d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 45 70 >flags |= MEM_Ep
1bb8e 68 65 6d 3b 0a 20 20 7d 0a 20 20 63 6f 6c 75 6d hem;. }. colum
1bb8f 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 nMallocFailure(p
1bb90 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 Stmt);. return
1bb91 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a (sqlite3_value *
1bb92 29 70 4f 75 74 3b 0a 7d 0a 23 69 66 6e 64 65 66 )pOut;.}.#ifndef
1bb93 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 SQLITE_OMIT_UTF
1bb94 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 16.SQLITE_API co
1bb95 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 nst void *sqlite
1bb96 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 3_column_text16(
1bb97 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
1bb98 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 63 tmt, int i){. c
1bb99 6f 6e 73 74 20 76 6f 69 64 20 2a 76 61 6c 20 3d onst void *val =
1bb9a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
1bb9b 65 78 74 31 36 28 20 63 6f 6c 75 6d 6e 4d 65 6d ext16( columnMem
1bb9c 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 (pStmt,i) );. c
1bb9d 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 olumnMallocFailu
1bb9e 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 re(pStmt);. ret
1bb9f 75 72 6e 20 76 61 6c 3b 0a 7d 0a 23 65 6e 64 69 urn val;.}.#endi
1bba0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
1bba1 5f 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49 54 45 _UTF16 */.SQLITE
1bba2 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
1bba3 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 73 71 6c _column_type(sql
1bba4 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1bba5 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 , int i){. int
1bba6 69 54 79 70 65 20 3d 20 73 71 6c 69 74 65 33 5f iType = sqlite3_
1bba7 76 61 6c 75 65 5f 74 79 70 65 28 20 63 6f 6c 75 value_type( colu
1bba8 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 mnMem(pStmt,i) )
1bba9 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 ;. columnMalloc
1bbaa 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a Failure(pStmt);.
1bbab 20 20 72 65 74 75 72 6e 20 69 54 79 70 65 3b 0a return iType;.
1bbac 7d 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 }../* The follow
1bbad 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 ing function is
1bbae 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 61 6e 64 experimental and
1bbaf 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e subject to chan
1bbb0 67 65 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c ge or.** removal
1bbb1 20 2a 2f 0a 2f 2a 69 6e 74 20 73 71 6c 69 74 65 */./*int sqlite
1bbb2 33 5f 63 6f 6c 75 6d 6e 5f 6e 75 6d 65 72 69 63 3_column_numeric
1bbb3 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f 73 74 _type(sqlite3_st
1bbb4 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 mt *pStmt, int i
1bbb5 29 7b 0a 2a 2a 20 20 72 65 74 75 72 6e 20 73 71 ){.** return sq
1bbb6 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 lite3_value_nume
1bbb7 72 69 63 5f 74 79 70 65 28 20 63 6f 6c 75 6d 6e ric_type( column
1bbb8 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a Mem(pStmt,i) );.
1bbb9 2a 2a 7d 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f **}.*/../*.** Co
1bbba 6e 76 65 72 74 20 74 68 65 20 4e 2d 74 68 20 65 nvert the N-th e
1bbbb 6c 65 6d 65 6e 74 20 6f 66 20 70 53 74 6d 74 2d lement of pStmt-
1bbbc 3e 70 43 6f 6c 4e 61 6d 65 5b 5d 20 69 6e 74 6f >pColName[] into
1bbbd 20 61 20 73 74 72 69 6e 67 20 75 73 69 6e 67 0a a string using.
1bbbe 2a 2a 20 78 46 75 6e 63 28 29 20 74 68 65 6e 20 ** xFunc() then
1bbbf 72 65 74 75 72 6e 20 74 68 61 74 20 73 74 72 69 return that stri
1bbc0 6e 67 2e 20 20 49 66 20 4e 20 69 73 20 6f 75 74 ng. If N is out
1bbc1 20 6f 66 20 72 61 6e 67 65 2c 20 72 65 74 75 72 of range, retur
1bbc2 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 n 0..**.** There
1bbc3 20 61 72 65 20 75 70 20 74 6f 20 35 20 6e 61 6d are up to 5 nam
1bbc4 65 73 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 es for each colu
1bbc5 6d 6e 2e 20 20 75 73 65 54 79 70 65 20 64 65 74 mn. useType det
1bbc6 65 72 6d 69 6e 65 73 20 77 68 69 63 68 0a 2a 2a ermines which.**
1bbc7 20 6e 61 6d 65 20 69 73 20 72 65 74 75 72 6e 65 name is returne
1bbc8 64 2e 20 20 48 65 72 65 20 61 72 65 20 74 68 65 d. Here are the
1bbc9 20 6e 61 6d 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 names:.**.**
1bbca 20 30 20 20 20 20 20 20 54 68 65 20 63 6f 6c 75 0 The colu
1bbcb 6d 6e 20 6e 61 6d 65 20 61 73 20 69 74 20 73 68 mn name as it sh
1bbcc 6f 75 6c 64 20 62 65 20 64 69 73 70 6c 61 79 65 ould be displaye
1bbcd 64 20 66 6f 72 20 6f 75 74 70 75 74 0a 2a 2a 20 d for output.**
1bbce 20 20 20 31 20 20 20 20 20 20 54 68 65 20 64 61 1 The da
1bbcf 74 61 74 79 70 65 20 6e 61 6d 65 20 66 6f 72 20 tatype name for
1bbd0 74 68 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 20 20 the column.**
1bbd1 20 32 20 20 20 20 20 20 54 68 65 20 6e 61 6d 65 2 The name
1bbd2 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
1bbd3 20 74 68 61 74 20 74 68 65 20 63 6f 6c 75 6d 6e that the column
1bbd4 20 64 65 72 69 76 65 73 20 66 72 6f 6d 0a 2a 2a derives from.**
1bbd5 20 20 20 20 33 20 20 20 20 20 20 54 68 65 20 6e 3 The n
1bbd6 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 ame of the table
1bbd7 20 74 68 61 74 20 74 68 65 20 63 6f 6c 75 6d 6e that the column
1bbd8 20 64 65 72 69 76 65 73 20 66 72 6f 6d 0a 2a 2a derives from.**
1bbd9 20 20 20 20 34 20 20 20 20 20 20 54 68 65 20 6e 4 The n
1bbda 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 ame of the table
1bbdb 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 74 68 65 column that the
1bbdc 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 64 result column d
1bbdd 65 72 69 76 65 73 20 66 72 6f 6d 0a 2a 2a 0a 2a erives from.**.*
1bbde 2a 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 * If the result
1bbdf 69 73 20 6e 6f 74 20 61 20 73 69 6d 70 6c 65 20 is not a simple
1bbe0 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 column reference
1bbe1 20 28 69 66 20 69 74 20 69 73 20 61 6e 20 65 78 (if it is an ex
1bbe2 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 6f 72 20 61 pression.** or a
1bbe3 20 63 6f 6e 73 74 61 6e 74 29 20 74 68 65 6e 20 constant) then
1bbe4 75 73 65 54 79 70 65 73 20 32 2c 20 33 2c 20 61 useTypes 2, 3, a
1bbe5 6e 64 20 34 20 72 65 74 75 72 6e 20 4e 55 4c 4c nd 4 return NULL
1bbe6 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 ..*/.static cons
1bbe7 74 20 76 6f 69 64 20 2a 63 6f 6c 75 6d 6e 4e 61 t void *columnNa
1bbe8 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 me(. sqlite3_st
1bbe9 6d 74 20 2a 70 53 74 6d 74 2c 0a 20 20 69 6e 74 mt *pStmt,. int
1bbea 20 4e 2c 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 N,. const void
1bbeb 20 2a 28 2a 78 46 75 6e 63 29 28 4d 65 6d 2a 29 *(*xFunc)(Mem*)
1bbec 2c 0a 20 20 69 6e 74 20 75 73 65 54 79 70 65 0a ,. int useType.
1bbed 29 7b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 ){. const void
1bbee 2a 72 65 74 20 3d 20 30 3b 0a 20 20 56 64 62 65 *ret = 0;. Vdbe
1bbef 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 *p = (Vdbe *)pS
1bbf0 74 6d 74 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 tmt;. int n;.
1bbf1 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d sqlite3 *db = p-
1bbf2 3e 64 62 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 >db;. . assert
1bbf3 28 20 64 62 21 3d 30 20 29 3b 0a 20 20 6e 20 3d ( db!=0 );. n =
1bbf4 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
1bbf5 63 6f 75 6e 74 28 70 53 74 6d 74 29 3b 0a 20 20 count(pStmt);.
1bbf6 69 66 28 20 4e 3c 6e 20 26 26 20 4e 3e 3d 30 20 if( N<n && N>=0
1bbf7 29 7b 0a 20 20 20 20 4e 20 2b 3d 20 75 73 65 54 ){. N += useT
1bbf8 79 70 65 2a 6e 3b 0a 20 20 20 20 73 71 6c 69 74 ype*n;. sqlit
1bbf9 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 e3_mutex_enter(d
1bbfa 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 61 b->mutex);. a
1bbfb 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f ssert( db->mallo
1bbfc 63 46 61 69 6c 65 64 3d 3d 30 20 29 3b 0a 20 20 cFailed==0 );.
1bbfd 20 20 72 65 74 20 3d 20 78 46 75 6e 63 28 26 70 ret = xFunc(&p
1bbfe 2d 3e 61 43 6f 6c 4e 61 6d 65 5b 4e 5d 29 3b 0a ->aColName[N]);.
1bbff 20 20 20 20 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63 /* A malloc
1bc00 20 6d 61 79 20 68 61 76 65 20 66 61 69 6c 65 64 may have failed
1bc01 20 69 6e 73 69 64 65 20 6f 66 20 74 68 65 20 78 inside of the x
1bc02 46 75 6e 63 28 29 20 63 61 6c 6c 2e 20 49 66 20 Func() call. If
1bc03 74 68 69 73 0a 20 20 20 20 2a 2a 20 69 73 20 74 this. ** is t
1bc04 68 65 20 63 61 73 65 2c 20 63 6c 65 61 72 20 74 he case, clear t
1bc05 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 he mallocFailed
1bc06 66 6c 61 67 20 61 6e 64 20 72 65 74 75 72 6e 20 flag and return
1bc07 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 NULL.. */.
1bc08 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 if( db->mallocF
1bc09 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 64 ailed ){. d
1bc0a 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1bc0b 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74 20 3d = 0;. ret =
1bc0c 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 0;. }. sq
1bc0d 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
1bc0e 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 e(db->mutex);.
1bc0f 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 74 3b 0a }. return ret;.
1bc10 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
1bc11 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 the name of the
1bc12 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 Nth column of th
1bc13 65 20 72 65 73 75 6c 74 20 73 65 74 20 72 65 74 e result set ret
1bc14 75 72 6e 65 64 20 62 79 20 53 51 4c 0a 2a 2a 20 urned by SQL.**
1bc15 73 74 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 2e statement pStmt.
1bc16 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 .*/.SQLITE_API c
1bc17 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 onst char *sqlit
1bc18 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 73 e3_column_name(s
1bc19 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1bc1a 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 mt, int N){. re
1bc1b 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 turn columnName(
1bc1c 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c . pStmt, N,
1bc1d 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 (const void*(*)
1bc1e 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 (Mem*))sqlite3_v
1bc1f 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 alue_text, COLNA
1bc20 4d 45 5f 4e 41 4d 45 29 3b 0a 7d 0a 23 69 66 6e ME_NAME);.}.#ifn
1bc21 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1bc22 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 UTF16.SQLITE_API
1bc23 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c const void *sql
1bc24 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 ite3_column_name
1bc25 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 16(sqlite3_stmt
1bc26 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a *pStmt, int N){.
1bc27 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e return columnN
1bc28 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 ame(. pStmt
1bc29 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 , N, (const void
1bc2a 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 *(*)(Mem*))sqlit
1bc2b 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 2c e3_value_text16,
1bc2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 29 3b 0a COLNAME_NAME);.
1bc2d 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
1bc2e 43 6f 6e 73 74 72 61 69 6e 74 3a 20 20 49 66 20 Constraint: If
1bc2f 79 6f 75 20 68 61 76 65 20 45 4e 41 42 4c 45 5f you have ENABLE_
1bc30 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 20 COLUMN_METADATA
1bc31 74 68 65 6e 20 79 6f 75 20 6d 75 73 74 0a 2a 2a then you must.**
1bc32 20 6e 6f 74 20 64 65 66 69 6e 65 20 4f 4d 49 54 not define OMIT
1bc33 5f 44 45 43 4c 54 59 50 45 2e 0a 2a 2f 0a 23 69 _DECLTYPE..*/.#i
1bc34 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
1bc35 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 29 20 _OMIT_DECLTYPE)
1bc36 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 && defined(SQLIT
1bc37 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f E_ENABLE_COLUMN_
1bc38 4d 45 54 41 44 41 54 41 29 0a 23 20 65 72 72 6f METADATA).# erro
1bc39 72 20 22 4d 75 73 74 20 6e 6f 74 20 64 65 66 69 r "Must not defi
1bc3a 6e 65 20 62 6f 74 68 20 53 51 4c 49 54 45 5f 4f ne both SQLITE_O
1bc3b 4d 49 54 5f 44 45 43 4c 54 59 50 45 20 5c 0a 20 MIT_DECLTYPE \.
1bc3c 20 20 20 20 20 20 20 20 61 6e 64 20 53 51 4c 49 and SQLI
1bc3d 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e TE_ENABLE_COLUMN
1bc3e 5f 4d 45 54 41 44 41 54 41 22 0a 23 65 6e 64 69 _METADATA".#endi
1bc3f 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 f..#ifndef SQLIT
1bc40 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 0a E_OMIT_DECLTYPE.
1bc41 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
1bc42 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74 column declarat
1bc43 69 6f 6e 20 74 79 70 65 20 28 69 66 20 61 70 70 ion type (if app
1bc44 6c 69 63 61 62 6c 65 29 20 6f 66 20 74 68 65 20 licable) of the
1bc45 27 69 27 74 68 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 'i'th column.**
1bc46 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 of the result se
1bc47 74 20 6f 66 20 53 51 4c 20 73 74 61 74 65 6d 65 t of SQL stateme
1bc48 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c nt pStmt..*/.SQL
1bc49 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 ITE_API const ch
1bc4a 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 ar *sqlite3_colu
1bc4b 6d 6e 5f 64 65 63 6c 74 79 70 65 28 73 71 6c 69 mn_decltype(sqli
1bc4c 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c te3_stmt *pStmt,
1bc4d 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 int N){. retur
1bc4e 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 n columnName(.
1bc4f 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 pStmt, N, (c
1bc50 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 onst void*(*)(Me
1bc51 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 m*))sqlite3_valu
1bc52 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f e_text, COLNAME_
1bc53 44 45 43 4c 54 59 50 45 29 3b 0a 7d 0a 23 69 66 DECLTYPE);.}.#if
1bc54 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1bc55 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 _UTF16.SQLITE_AP
1bc56 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 I const void *sq
1bc57 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 lite3_column_dec
1bc58 6c 74 79 70 65 31 36 28 73 71 6c 69 74 65 33 5f ltype16(sqlite3_
1bc59 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 stmt *pStmt, int
1bc5a 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f N){. return co
1bc5b 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 lumnName(.
1bc5c 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 pStmt, N, (const
1bc5d 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 void*(*)(Mem*))
1bc5e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
1bc5f 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 44 45 xt16, COLNAME_DE
1bc60 43 4c 54 59 50 45 29 3b 0a 7d 0a 23 65 6e 64 69 CLTYPE);.}.#endi
1bc61 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
1bc62 5f 55 54 46 31 36 20 2a 2f 0a 23 65 6e 64 69 66 _UTF16 */.#endif
1bc63 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
1bc64 44 45 43 4c 54 59 50 45 20 2a 2f 0a 0a 23 69 66 DECLTYPE */..#if
1bc65 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c def SQLITE_ENABL
1bc66 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 E_COLUMN_METADAT
1bc67 41 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 A./*.** Return t
1bc68 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 he name of the d
1bc69 61 74 61 62 61 73 65 20 66 72 6f 6d 20 77 68 69 atabase from whi
1bc6a 63 68 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 ch a result colu
1bc6b 6d 6e 20 64 65 72 69 76 65 73 2e 0a 2a 2a 20 4e mn derives..** N
1bc6c 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 ULL is returned
1bc6d 69 66 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f if the result co
1bc6e 6c 75 6d 6e 20 69 73 20 61 6e 20 65 78 70 72 65 lumn is an expre
1bc6f 73 73 69 6f 6e 20 6f 72 20 63 6f 6e 73 74 61 6e ssion or constan
1bc70 74 20 6f 72 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 t or.** anything
1bc71 20 65 6c 73 65 20 77 68 69 63 68 20 69 73 20 6e else which is n
1bc72 6f 74 20 61 6e 20 75 6e 61 62 69 67 75 6f 75 73 ot an unabiguous
1bc73 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 reference to a
1bc74 64 61 74 61 62 61 73 65 20 63 6f 6c 75 6d 6e 2e database column.
1bc75 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 .*/.SQLITE_API c
1bc76 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 onst char *sqlit
1bc77 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 e3_column_databa
1bc78 73 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f se_name(sqlite3_
1bc79 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 stmt *pStmt, int
1bc7a 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f N){. return co
1bc7b 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 lumnName(.
1bc7c 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 pStmt, N, (const
1bc7d 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 void*(*)(Mem*))
1bc7e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
1bc7f 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 44 41 54 41 xt, COLNAME_DATA
1bc80 42 41 53 45 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 BASE);.}.#ifndef
1bc81 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 SQLITE_OMIT_UTF
1bc82 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 16.SQLITE_API co
1bc83 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 nst void *sqlite
1bc84 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 3_column_databas
1bc85 65 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 e_name16(sqlite3
1bc86 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e _stmt *pStmt, in
1bc87 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 t N){. return c
1bc88 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 olumnName(.
1bc89 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 pStmt, N, (cons
1bc8a 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 t void*(*)(Mem*)
1bc8b 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 )sqlite3_value_t
1bc8c 65 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 44 ext16, COLNAME_D
1bc8d 41 54 41 42 41 53 45 29 3b 0a 7d 0a 23 65 6e 64 ATABASE);.}.#end
1bc8e 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
1bc8f 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a T_UTF16 */../*.*
1bc90 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 61 6d * Return the nam
1bc91 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 66 e of the table f
1bc92 72 6f 6d 20 77 68 69 63 68 20 61 20 72 65 73 75 rom which a resu
1bc93 6c 74 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 lt column derive
1bc94 73 2e 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 s..** NULL is re
1bc95 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 72 65 turned if the re
1bc96 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61 sult column is a
1bc97 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 n expression or
1bc98 63 6f 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a 20 61 constant or.** a
1bc99 6e 79 74 68 69 6e 67 20 65 6c 73 65 20 77 68 69 nything else whi
1bc9a 63 68 20 69 73 20 6e 6f 74 20 61 6e 20 75 6e 61 ch is not an una
1bc9b 62 69 67 75 6f 75 73 20 72 65 66 65 72 65 6e 63 biguous referenc
1bc9c 65 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 e to a database
1bc9d 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 column..*/.SQLIT
1bc9e 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 E_API const char
1bc9f 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e *sqlite3_column
1bca0 5f 74 61 62 6c 65 5f 6e 61 6d 65 28 73 71 6c 69 _table_name(sqli
1bca1 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c te3_stmt *pStmt,
1bca2 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 int N){. retur
1bca3 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 n columnName(.
1bca4 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 pStmt, N, (c
1bca5 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 onst void*(*)(Me
1bca6 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 m*))sqlite3_valu
1bca7 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f e_text, COLNAME_
1bca8 54 41 42 4c 45 29 3b 0a 7d 0a 23 69 66 6e 64 65 TABLE);.}.#ifnde
1bca9 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 f SQLITE_OMIT_UT
1bcaa 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 F16.SQLITE_API c
1bcab 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 onst void *sqlit
1bcac 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f e3_column_table_
1bcad 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73 name16(sqlite3_s
1bcae 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 tmt *pStmt, int
1bcaf 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c N){. return col
1bcb0 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 umnName(. p
1bcb1 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 Stmt, N, (const
1bcb2 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 void*(*)(Mem*))s
1bcb3 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
1bcb4 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 54 41 42 t16, COLNAME_TAB
1bcb5 4c 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a LE);.}.#endif /*
1bcb6 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 SQLITE_OMIT_UTF
1bcb7 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 16 */../*.** Ret
1bcb8 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 urn the name of
1bcb9 74 68 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e the table column
1bcba 20 66 72 6f 6d 20 77 68 69 63 68 20 61 20 72 65 from which a re
1bcbb 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 64 65 72 69 sult column deri
1bcbc 76 65 73 2e 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 ves..** NULL is
1bcbd 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 returned if the
1bcbe 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 result column is
1bcbf 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f an expression o
1bcc0 72 20 63 6f 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a r constant or.**
1bcc1 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 20 77 anything else w
1bcc2 68 69 63 68 20 69 73 20 6e 6f 74 20 61 6e 20 75 hich is not an u
1bcc3 6e 61 62 69 67 75 6f 75 73 20 72 65 66 65 72 65 nabiguous refere
1bcc4 6e 63 65 20 74 6f 20 61 20 64 61 74 61 62 61 73 nce to a databas
1bcc5 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c e column..*/.SQL
1bcc6 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 ITE_API const ch
1bcc7 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 ar *sqlite3_colu
1bcc8 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 28 73 mn_origin_name(s
1bcc9 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1bcca 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 mt, int N){. re
1bccb 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 turn columnName(
1bccc 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c . pStmt, N,
1bccd 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 (const void*(*)
1bcce 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 (Mem*))sqlite3_v
1bccf 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 alue_text, COLNA
1bcd0 4d 45 5f 43 4f 4c 55 4d 4e 29 3b 0a 7d 0a 23 69 ME_COLUMN);.}.#i
1bcd1 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1bcd2 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 T_UTF16.SQLITE_A
1bcd3 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 PI const void *s
1bcd4 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 qlite3_column_or
1bcd5 69 67 69 6e 5f 6e 61 6d 65 31 36 28 73 71 6c 69 igin_name16(sqli
1bcd6 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c te3_stmt *pStmt,
1bcd7 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 int N){. retur
1bcd8 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 n columnName(.
1bcd9 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 pStmt, N, (c
1bcda 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 onst void*(*)(Me
1bcdb 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 m*))sqlite3_valu
1bcdc 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41 4d e_text16, COLNAM
1bcdd 45 5f 43 4f 4c 55 4d 4e 29 3b 0a 7d 0a 23 65 6e E_COLUMN);.}.#en
1bcde 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
1bcdf 49 54 5f 55 54 46 31 36 20 2a 2f 0a 23 65 6e 64 IT_UTF16 */.#end
1bce0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 if /* SQLITE_ENA
1bce1 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 BLE_COLUMN_METAD
1bce2 41 54 41 20 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a ATA */.../******
1bce3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bce4 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 ********* sqlite
1bce5 33 5f 62 69 6e 64 5f 20 20 2a 2a 2a 2a 2a 2a 2a 3_bind_ *******
1bce6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bce7 2a 2a 2a 2a 0a 2a 2a 20 0a 2a 2a 20 52 6f 75 74 ****.** .** Rout
1bce8 69 6e 65 73 20 75 73 65 64 20 74 6f 20 61 74 74 ines used to att
1bce9 61 63 68 20 76 61 6c 75 65 73 20 74 6f 20 77 69 ach values to wi
1bcea 6c 64 63 61 72 64 73 20 69 6e 20 61 20 63 6f 6d ldcards in a com
1bceb 70 69 6c 65 64 20 53 51 4c 20 73 74 61 74 65 6d piled SQL statem
1bcec 65 6e 74 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 55 6e ent..*/./*.** Un
1bced 62 69 6e 64 20 74 68 65 20 76 61 6c 75 65 20 62 bind the value b
1bcee 6f 75 6e 64 20 74 6f 20 76 61 72 69 61 62 6c 65 ound to variable
1bcef 20 69 20 69 6e 20 76 69 72 74 75 61 6c 20 6d 61 i in virtual ma
1bcf0 63 68 69 6e 65 20 70 2e 20 54 68 69 73 20 69 73 chine p. This is
1bcf1 20 74 68 65 20 0a 2a 2a 20 74 68 65 20 73 61 6d the .** the sam
1bcf2 65 20 61 73 20 62 69 6e 64 69 6e 67 20 61 20 4e e as binding a N
1bcf3 55 4c 4c 20 76 61 6c 75 65 20 74 6f 20 74 68 65 ULL value to the
1bcf4 20 63 6f 6c 75 6d 6e 2e 20 49 66 20 74 68 65 20 column. If the
1bcf5 22 69 22 20 70 61 72 61 6d 65 74 65 72 20 69 73 "i" parameter is
1bcf6 0a 2a 2a 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 .** out of range
1bcf7 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 52 41 , then SQLITE_RA
1bcf8 4e 47 45 20 69 73 20 72 65 74 75 72 6e 65 64 2e NGE is returned.
1bcf9 20 4f 74 68 65 77 69 73 65 20 53 51 4c 49 54 45 Othewise SQLITE
1bcfa 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 75 63 _OK..**.** A suc
1bcfb 63 65 73 73 66 75 6c 20 65 76 61 6c 75 61 74 69 cessful evaluati
1bcfc 6f 6e 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 on of this routi
1bcfd 6e 65 20 61 63 71 75 69 72 65 73 20 74 68 65 20 ne acquires the
1bcfe 6d 75 74 65 78 20 6f 6e 20 70 2e 0a 2a 2a 20 74 mutex on p..** t
1bcff 68 65 20 6d 75 74 65 78 20 69 73 20 72 65 6c 65 he mutex is rele
1bd00 61 73 65 64 20 69 66 20 61 6e 79 20 6b 69 6e 64 ased if any kind
1bd01 20 6f 66 20 65 72 72 6f 72 20 6f 63 63 75 72 73 of error occurs
1bd02 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 72 72 6f ..**.** The erro
1bd03 72 20 63 6f 64 65 20 73 74 6f 72 65 64 20 69 6e r code stored in
1bd04 20 64 61 74 61 62 61 73 65 20 70 2d 3e 64 62 20 database p->db
1bd05 69 73 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 is overwritten w
1bd06 69 74 68 20 74 68 65 20 72 65 74 75 72 6e 0a 2a ith the return.*
1bd07 2a 20 76 61 6c 75 65 20 69 6e 20 61 6e 79 20 63 * value in any c
1bd08 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ase..*/.static i
1bd09 6e 74 20 76 64 62 65 55 6e 62 69 6e 64 28 56 64 nt vdbeUnbind(Vd
1bd0a 62 65 20 2a 70 2c 20 69 6e 74 20 69 29 7b 0a 20 be *p, int i){.
1bd0b 20 4d 65 6d 20 2a 70 56 61 72 3b 0a 20 20 69 66 Mem *pVar;. if
1bd0c 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 ( p==0 ) return
1bd0d 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 SQLITE_MISUSE;.
1bd0e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
1bd0f 6e 74 65 72 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 nter(p->db->mute
1bd10 78 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 x);. if( p->mag
1bd11 69 63 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 ic!=VDBE_MAGIC_R
1bd12 55 4e 20 7c 7c 20 70 2d 3e 70 63 3e 3d 30 20 29 UN || p->pc>=0 )
1bd13 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 {. sqlite3Err
1bd14 6f 72 28 70 2d 3e 64 62 2c 20 53 51 4c 49 54 45 or(p->db, SQLITE
1bd15 5f 4d 49 53 55 53 45 2c 20 30 29 3b 0a 20 20 20 _MISUSE, 0);.
1bd16 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
1bd17 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 eave(p->db->mute
1bd18 78 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 x);. return S
1bd19 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 QLITE_MISUSE;.
1bd1a 7d 0a 20 20 69 66 28 20 69 3c 31 20 7c 7c 20 69 }. if( i<1 || i
1bd1b 3e 70 2d 3e 6e 56 61 72 20 29 7b 0a 20 20 20 20 >p->nVar ){.
1bd1c 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 2d 3e sqlite3Error(p->
1bd1d 64 62 2c 20 53 51 4c 49 54 45 5f 52 41 4e 47 45 db, SQLITE_RANGE
1bd1e 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 , 0);. sqlite
1bd1f 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3_mutex_leave(p-
1bd20 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 >db->mutex);.
1bd21 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 return SQLITE_R
1bd22 41 4e 47 45 3b 0a 20 20 7d 0a 20 20 69 2d 2d 3b ANGE;. }. i--;
1bd23 0a 20 20 70 56 61 72 20 3d 20 26 70 2d 3e 61 56 . pVar = &p->aV
1bd24 61 72 5b 69 5d 3b 0a 20 20 73 71 6c 69 74 65 33 ar[i];. sqlite3
1bd25 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 VdbeMemRelease(p
1bd26 56 61 72 29 3b 0a 20 20 70 56 61 72 2d 3e 66 6c Var);. pVar->fl
1bd27 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a ags = MEM_Null;.
1bd28 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 sqlite3Error(p
1bd29 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c ->db, SQLITE_OK,
1bd2a 20 30 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 0);.. /* If th
1bd2b 65 20 62 69 74 20 63 6f 72 72 65 73 70 6f 6e 64 e bit correspond
1bd2c 69 6e 67 20 74 6f 20 74 68 69 73 20 76 61 72 69 ing to this vari
1bd2d 61 62 6c 65 20 69 6e 20 56 64 62 65 2e 65 78 70 able in Vdbe.exp
1bd2e 6d 61 73 6b 20 69 73 20 73 65 74 2c 20 74 68 65 mask is set, the
1bd2f 6e 20 0a 20 20 2a 2a 20 62 69 6e 64 69 6e 67 20 n . ** binding
1bd30 61 20 6e 65 77 20 76 61 6c 75 65 20 74 6f 20 74 a new value to t
1bd31 68 69 73 20 76 61 72 69 61 62 6c 65 20 69 6e 76 his variable inv
1bd32 61 6c 69 64 61 74 65 73 20 74 68 65 20 63 75 72 alidates the cur
1bd33 72 65 6e 74 20 71 75 65 72 79 20 70 6c 61 6e 2e rent query plan.
1bd34 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 . */. if( p->i
1bd35 73 50 72 65 70 61 72 65 56 32 20 26 26 0a 20 20 sPrepareV2 &&.
1bd36 20 20 20 28 28 69 3c 33 32 20 26 26 20 70 2d 3e ((i<32 && p->
1bd37 65 78 70 6d 61 73 6b 20 26 20 28 28 75 33 32 29 expmask & ((u32)
1bd38 31 20 3c 3c 20 69 29 29 20 7c 7c 20 70 2d 3e 65 1 << i)) || p->e
1bd39 78 70 6d 61 73 6b 3d 3d 30 78 66 66 66 66 66 66 xpmask==0xffffff
1bd3a 66 66 29 0a 20 20 29 7b 0a 20 20 20 20 70 2d 3e ff). ){. p->
1bd3b 65 78 70 69 72 65 64 20 3d 20 31 3b 0a 20 20 7d expired = 1;. }
1bd3c 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
1bd3d 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 69 _OK;.}../*.** Bi
1bd3e 6e 64 20 61 20 74 65 78 74 20 6f 72 20 42 4c 4f nd a text or BLO
1bd3f 42 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 B value..*/.stat
1bd40 69 63 20 69 6e 74 20 62 69 6e 64 54 65 78 74 28 ic int bindText(
1bd41 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 . sqlite3_stmt
1bd42 2a 70 53 74 6d 74 2c 20 20 20 2f 2a 20 54 68 65 *pStmt, /* The
1bd43 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 69 statement to bi
1bd44 6e 64 20 61 67 61 69 6e 73 74 20 2a 2f 0a 20 20 nd against */.
1bd45 69 6e 74 20 69 2c 20 20 20 20 20 20 20 20 20 20 int i,
1bd46 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
1bd47 6f 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 of the parameter
1bd48 20 74 6f 20 62 69 6e 64 20 2a 2f 0a 20 20 63 6f to bind */. co
1bd49 6e 73 74 20 76 6f 69 64 20 2a 7a 44 61 74 61 2c nst void *zData,
1bd4a 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
1bd4b 74 6f 20 74 68 65 20 64 61 74 61 20 74 6f 20 62 to the data to b
1bd4c 65 20 62 6f 75 6e 64 20 2a 2f 0a 20 20 69 6e 74 e bound */. int
1bd4d 20 6e 44 61 74 61 2c 20 20 20 20 20 20 20 20 20 nData,
1bd4e 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1bd4f 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 74 bytes of data t
1bd50 6f 20 62 65 20 62 6f 75 6e 64 20 2a 2f 0a 20 20 o be bound */.
1bd51 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 void (*xDel)(voi
1bd52 64 2a 29 2c 20 20 20 2f 2a 20 44 65 73 74 72 75 d*), /* Destru
1bd53 63 74 6f 72 20 66 6f 72 20 74 68 65 20 64 61 74 ctor for the dat
1bd54 61 20 2a 2f 0a 20 20 75 38 20 65 6e 63 6f 64 69 a */. u8 encodi
1bd55 6e 67 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ng /*
1bd56 20 45 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74 68 Encoding for th
1bd57 65 20 64 61 74 61 20 2a 2f 0a 29 7b 0a 20 20 56 e data */.){. V
1bd58 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a dbe *p = (Vdbe *
1bd59 29 70 53 74 6d 74 3b 0a 20 20 4d 65 6d 20 2a 70 )pStmt;. Mem *p
1bd5a 56 61 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a Var;. int rc;..
1bd5b 20 20 72 63 20 3d 20 76 64 62 65 55 6e 62 69 6e rc = vdbeUnbin
1bd5c 64 28 70 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 d(p, i);. if( r
1bd5d 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
1bd5e 20 20 20 20 69 66 28 20 7a 44 61 74 61 21 3d 30 if( zData!=0
1bd5f 20 29 7b 0a 20 20 20 20 20 20 70 56 61 72 20 3d ){. pVar =
1bd60 20 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 3b 0a &p->aVar[i-1];.
1bd61 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1bd62 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 e3VdbeMemSetStr(
1bd63 70 56 61 72 2c 20 7a 44 61 74 61 2c 20 6e 44 61 pVar, zData, nDa
1bd64 74 61 2c 20 65 6e 63 6f 64 69 6e 67 2c 20 78 44 ta, encoding, xD
1bd65 65 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 el);. if( r
1bd66 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
1bd67 65 6e 63 6f 64 69 6e 67 21 3d 30 20 29 7b 0a 20 encoding!=0 ){.
1bd68 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
1bd69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 te3VdbeChangeEnc
1bd6a 6f 64 69 6e 67 28 70 56 61 72 2c 20 45 4e 43 28 oding(pVar, ENC(
1bd6b 70 2d 3e 64 62 29 29 3b 0a 20 20 20 20 20 20 7d p->db));. }
1bd6c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 . sqlite3Er
1bd6d 72 6f 72 28 70 2d 3e 64 62 2c 20 72 63 2c 20 30 ror(p->db, rc, 0
1bd6e 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 );. rc = sq
1bd6f 6c 69 74 65 33 41 70 69 45 78 69 74 28 70 2d 3e lite3ApiExit(p->
1bd70 64 62 2c 20 72 63 29 3b 0a 20 20 20 20 7d 0a 20 db, rc);. }.
1bd71 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
1bd72 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 _leave(p->db->mu
1bd73 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 tex);. }. retu
1bd74 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a rn rc;.}.../*.**
1bd75 20 42 69 6e 64 20 61 20 62 6c 6f 62 20 76 61 6c Bind a blob val
1bd76 75 65 20 74 6f 20 61 6e 20 53 51 4c 20 73 74 61 ue to an SQL sta
1bd77 74 65 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 2e tement variable.
1bd78 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
1bd79 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f nt sqlite3_bind_
1bd7a 62 6c 6f 62 28 0a 20 20 73 71 6c 69 74 65 33 5f blob(. sqlite3_
1bd7b 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 20 20 stmt *pStmt, .
1bd7c 69 6e 74 20 69 2c 20 0a 20 20 63 6f 6e 73 74 20 int i, . const
1bd7d 76 6f 69 64 20 2a 7a 44 61 74 61 2c 20 0a 20 20 void *zData, .
1bd7e 69 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20 76 6f int nData, . vo
1bd7f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a id (*xDel)(void*
1bd80 29 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 69 ).){. return bi
1bd81 6e 64 54 65 78 74 28 70 53 74 6d 74 2c 20 69 2c ndText(pStmt, i,
1bd82 20 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 78 zData, nData, x
1bd83 44 65 6c 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 Del, 0);.}.SQLIT
1bd84 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1bd85 33 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 28 73 71 3_bind_double(sq
1bd86 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1bd87 74 2c 20 69 6e 74 20 69 2c 20 64 6f 75 62 6c 65 t, int i, double
1bd88 20 72 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 rValue){. int
1bd89 72 63 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 rc;. Vdbe *p =
1bd8a 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 (Vdbe *)pStmt;.
1bd8b 20 72 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 rc = vdbeUnbind
1bd8c 28 70 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63 (p, i);. if( rc
1bd8d 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1bd8e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
1bd8f 6d 53 65 74 44 6f 75 62 6c 65 28 26 70 2d 3e 61 mSetDouble(&p->a
1bd90 56 61 72 5b 69 2d 31 5d 2c 20 72 56 61 6c 75 65 Var[i-1], rValue
1bd91 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d );. sqlite3_m
1bd92 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 utex_leave(p->db
1bd93 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 ->mutex);. }.
1bd94 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c return rc;.}.SQL
1bd95 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1bd96 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 71 6c te3_bind_int(sql
1bd97 69 74 65 33 5f 73 74 6d 74 20 2a 70 2c 20 69 6e ite3_stmt *p, in
1bd98 74 20 69 2c 20 69 6e 74 20 69 56 61 6c 75 65 29 t i, int iValue)
1bd99 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 {. return sqlit
1bd9a 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 2c e3_bind_int64(p,
1bd9b 20 69 2c 20 28 69 36 34 29 69 56 61 6c 75 65 29 i, (i64)iValue)
1bd9c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 ;.}.SQLITE_API i
1bd9d 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f nt sqlite3_bind_
1bd9e 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 73 74 int64(sqlite3_st
1bd9f 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 mt *pStmt, int i
1bda0 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 , sqlite_int64 i
1bda1 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72 63 Value){. int rc
1bda2 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 ;. Vdbe *p = (V
1bda3 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72 dbe *)pStmt;. r
1bda4 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 c = vdbeUnbind(p
1bda5 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d , i);. if( rc==
1bda6 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1bda7 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1bda8 65 74 49 6e 74 36 34 28 26 70 2d 3e 61 56 61 72 etInt64(&p->aVar
1bda9 5b 69 2d 31 5d 2c 20 69 56 61 6c 75 65 29 3b 0a [i-1], iValue);.
1bdaa 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
1bdab 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d x_leave(p->db->m
1bdac 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 utex);. }. ret
1bdad 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 urn rc;.}.SQLITE
1bdae 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
1bdaf 5f 62 69 6e 64 5f 6e 75 6c 6c 28 73 71 6c 69 74 _bind_null(sqlit
1bdb0 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 e3_stmt *pStmt,
1bdb1 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 72 63 int i){. int rc
1bdb2 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 ;. Vdbe *p = (V
1bdb3 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63 dbe*)pStmt;. rc
1bdb4 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c = vdbeUnbind(p,
1bdb5 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 i);. if( rc==S
1bdb6 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1bdb7 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
1bdb8 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 ave(p->db->mutex
1bdb9 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
1bdba 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 rc;.}.SQLITE_API
1bdbb 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e int sqlite3_bin
1bdbc 64 5f 74 65 78 74 28 20 0a 20 20 73 71 6c 69 74 d_text( . sqlit
1bdbd 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 e3_stmt *pStmt,
1bdbe 0a 20 20 69 6e 74 20 69 2c 20 0a 20 20 63 6f 6e . int i, . con
1bdbf 73 74 20 63 68 61 72 20 2a 7a 44 61 74 61 2c 20 st char *zData,
1bdc0 0a 20 20 69 6e 74 20 6e 44 61 74 61 2c 20 0a 20 . int nData, .
1bdc1 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f void (*xDel)(vo
1bdc2 69 64 2a 29 0a 29 7b 0a 20 20 72 65 74 75 72 6e id*).){. return
1bdc3 20 62 69 6e 64 54 65 78 74 28 70 53 74 6d 74 2c bindText(pStmt,
1bdc4 20 69 2c 20 7a 44 61 74 61 2c 20 6e 44 61 74 61 i, zData, nData
1bdc5 2c 20 78 44 65 6c 2c 20 53 51 4c 49 54 45 5f 55 , xDel, SQLITE_U
1bdc6 54 46 38 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 TF8);.}.#ifndef
1bdc7 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 SQLITE_OMIT_UTF1
1bdc8 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 6.SQLITE_API int
1bdc9 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 sqlite3_bind_te
1bdca 78 74 31 36 28 0a 20 20 73 71 6c 69 74 65 33 5f xt16(. sqlite3_
1bdcb 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 20 20 stmt *pStmt, .
1bdcc 69 6e 74 20 69 2c 20 0a 20 20 63 6f 6e 73 74 20 int i, . const
1bdcd 76 6f 69 64 20 2a 7a 44 61 74 61 2c 20 0a 20 20 void *zData, .
1bdce 69 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20 76 6f int nData, . vo
1bdcf 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a id (*xDel)(void*
1bdd0 29 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 69 ).){. return bi
1bdd1 6e 64 54 65 78 74 28 70 53 74 6d 74 2c 20 69 2c ndText(pStmt, i,
1bdd2 20 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 78 zData, nData, x
1bdd3 44 65 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 Del, SQLITE_UTF1
1bdd4 36 4e 41 54 49 56 45 29 3b 0a 7d 0a 23 65 6e 64 6NATIVE);.}.#end
1bdd5 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
1bdd6 54 5f 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49 54 T_UTF16 */.SQLIT
1bdd7 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1bdd8 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 73 71 6c 3_bind_value(sql
1bdd9 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1bdda 2c 20 69 6e 74 20 69 2c 20 63 6f 6e 73 74 20 73 , int i, const s
1bddb 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 qlite3_value *pV
1bddc 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b alue){. int rc;
1bddd 0a 20 20 73 77 69 74 63 68 28 20 70 56 61 6c 75 . switch( pValu
1bdde 65 2d 3e 74 79 70 65 20 29 7b 0a 20 20 20 20 63 e->type ){. c
1bddf 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 ase SQLITE_INTEG
1bde0 45 52 3a 20 7b 0a 20 20 20 20 20 20 72 63 20 3d ER: {. rc =
1bde1 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e sqlite3_bind_in
1bde2 74 36 34 28 70 53 74 6d 74 2c 20 69 2c 20 70 56 t64(pStmt, i, pV
1bde3 61 6c 75 65 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 alue->u.i);.
1bde4 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
1bde5 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 case SQLITE_F
1bde6 4c 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20 72 63 LOAT: {. rc
1bde7 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f = sqlite3_bind_
1bde8 64 6f 75 62 6c 65 28 70 53 74 6d 74 2c 20 69 2c double(pStmt, i,
1bde9 20 70 56 61 6c 75 65 2d 3e 72 29 3b 0a 20 20 20 pValue->r);.
1bdea 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
1bdeb 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
1bdec 42 4c 4f 42 3a 20 7b 0a 20 20 20 20 20 20 69 66 BLOB: {. if
1bded 28 20 70 56 61 6c 75 65 2d 3e 66 6c 61 67 73 20 ( pValue->flags
1bdee 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 & MEM_Zero ){.
1bdef 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1bdf0 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 e3_bind_zeroblob
1bdf1 28 70 53 74 6d 74 2c 20 69 2c 20 70 56 61 6c 75 (pStmt, i, pValu
1bdf2 65 2d 3e 75 2e 6e 5a 65 72 6f 29 3b 0a 20 20 20 e->u.nZero);.
1bdf3 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1bdf4 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 rc = sqlite3_b
1bdf5 69 6e 64 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20 ind_blob(pStmt,
1bdf6 69 2c 20 70 56 61 6c 75 65 2d 3e 7a 2c 20 70 56 i, pValue->z, pV
1bdf7 61 6c 75 65 2d 3e 6e 2c 53 51 4c 49 54 45 5f 54 alue->n,SQLITE_T
1bdf8 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 20 RANSIENT);.
1bdf9 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a }. break;.
1bdfa 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 }. case S
1bdfb 51 4c 49 54 45 5f 54 45 58 54 3a 20 7b 0a 20 20 QLITE_TEXT: {.
1bdfc 20 20 20 20 72 63 20 3d 20 62 69 6e 64 54 65 78 rc = bindTex
1bdfd 74 28 70 53 74 6d 74 2c 69 2c 20 20 70 56 61 6c t(pStmt,i, pVal
1bdfe 75 65 2d 3e 7a 2c 20 70 56 61 6c 75 65 2d 3e 6e ue->z, pValue->n
1bdff 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 , SQLITE_TRANSIE
1be00 4e 54 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 NT,.
1be01 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1be02 20 20 70 56 61 6c 75 65 2d 3e 65 6e 63 29 3b 0a pValue->enc);.
1be03 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1be04 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 }. default:
1be05 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c {. rc = sql
1be06 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 70 ite3_bind_null(p
1be07 53 74 6d 74 2c 20 69 29 3b 0a 20 20 20 20 20 20 Stmt, i);.
1be08 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d break;. }. }
1be09 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1be0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
1be0b 71 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f qlite3_bind_zero
1be0c 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 73 74 6d blob(sqlite3_stm
1be0d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 2c t *pStmt, int i,
1be0e 20 69 6e 74 20 6e 29 7b 0a 20 20 69 6e 74 20 72 int n){. int r
1be0f 63 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 c;. Vdbe *p = (
1be10 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 Vdbe *)pStmt;.
1be11 72 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 rc = vdbeUnbind(
1be12 70 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d p, i);. if( rc=
1be13 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1be14 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1be15 53 65 74 5a 65 72 6f 42 6c 6f 62 28 26 70 2d 3e SetZeroBlob(&p->
1be16 61 56 61 72 5b 69 2d 31 5d 2c 20 6e 29 3b 0a 20 aVar[i-1], n);.
1be17 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
1be18 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 _leave(p->db->mu
1be19 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 tex);. }. retu
1be1a 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
1be1b 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 Return the numbe
1be1c 72 20 6f 66 20 77 69 6c 64 63 61 72 64 73 20 74 r of wildcards t
1be1d 68 61 74 20 63 61 6e 20 62 65 20 70 6f 74 65 6e hat can be poten
1be1e 74 69 61 6c 6c 79 20 62 6f 75 6e 64 20 74 6f 2e tially bound to.
1be1f 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
1be20 20 69 73 20 61 64 64 65 64 20 74 6f 20 73 75 70 is added to sup
1be21 70 6f 72 74 20 44 42 44 3a 3a 53 51 4c 69 74 65 port DBD::SQLite
1be22 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 . .*/.SQLITE_AP
1be23 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 I int sqlite3_bi
1be24 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 nd_parameter_cou
1be25 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 nt(sqlite3_stmt
1be26 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 *pStmt){. Vdbe
1be27 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d *p = (Vdbe*)pStm
1be28 74 3b 0a 20 20 72 65 74 75 72 6e 20 70 20 3f 20 t;. return p ?
1be29 70 2d 3e 6e 56 61 72 20 3a 20 30 3b 0a 7d 0a 0a p->nVar : 0;.}..
1be2a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6d /*.** Create a m
1be2b 61 70 70 69 6e 67 20 66 72 6f 6d 20 76 61 72 69 apping from vari
1be2c 61 62 6c 65 20 6e 75 6d 62 65 72 73 20 74 6f 20 able numbers to
1be2d 76 61 72 69 61 62 6c 65 20 6e 61 6d 65 73 0a 2a variable names.*
1be2e 2a 20 69 6e 20 74 68 65 20 56 64 62 65 2e 61 7a * in the Vdbe.az
1be2f 56 61 72 5b 5d 20 61 72 72 61 79 2c 20 69 66 20 Var[] array, if
1be30 73 75 63 68 20 61 20 6d 61 70 70 69 6e 67 20 64 such a mapping d
1be31 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 0a oes not already.
1be32 2a 2a 20 65 78 69 73 74 2e 0a 2a 2f 0a 73 74 61 ** exist..*/.sta
1be33 74 69 63 20 76 6f 69 64 20 63 72 65 61 74 65 56 tic void createV
1be34 61 72 4d 61 70 28 56 64 62 65 20 2a 70 29 7b 0a arMap(Vdbe *p){.
1be35 20 20 69 66 28 20 21 70 2d 3e 6f 6b 56 61 72 20 if( !p->okVar
1be36 29 7b 0a 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 ){. int j;.
1be37 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20 73 Op *pOp;. s
1be38 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
1be39 65 72 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 er(p->db->mutex)
1be3a 3b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 61 63 ;. /* The rac
1be3b 65 20 63 6f 6e 64 69 74 69 6f 6e 20 68 65 72 65 e condition here
1be3c 20 69 73 20 68 61 72 6d 6c 65 73 73 2e 20 20 49 is harmless. I
1be3d 66 20 74 77 6f 20 74 68 72 65 61 64 73 20 63 61 f two threads ca
1be3e 6c 6c 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 72 ll this. ** r
1be3f 6f 75 74 69 6e 65 20 6f 6e 20 74 68 65 20 73 61 outine on the sa
1be40 6d 65 20 56 64 62 65 20 61 74 20 74 68 65 20 73 me Vdbe at the s
1be41 61 6d 65 20 74 69 6d 65 2c 20 74 68 65 79 20 62 ame time, they b
1be42 6f 74 68 20 6d 69 67 68 74 20 65 6e 64 0a 20 20 oth might end.
1be43 20 20 2a 2a 20 75 70 20 69 6e 69 74 69 61 6c 69 ** up initiali
1be44 7a 69 6e 67 20 74 68 65 20 56 64 62 65 2e 61 7a zing the Vdbe.az
1be45 56 61 72 5b 5d 20 61 72 72 61 79 2e 20 20 54 68 Var[] array. Th
1be46 61 74 20 69 73 20 61 20 6c 69 74 74 6c 65 20 65 at is a little e
1be47 78 74 72 61 0a 20 20 20 20 2a 2a 20 77 6f 72 6b xtra. ** work
1be48 20 62 75 74 20 69 74 20 72 65 73 75 6c 74 73 20 but it results
1be49 69 6e 20 74 68 65 20 73 61 6d 65 20 61 6e 73 77 in the same answ
1be4a 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 er.. */. f
1be4b 6f 72 28 6a 3d 30 2c 20 70 4f 70 3d 70 2d 3e 61 or(j=0, pOp=p->a
1be4c 4f 70 3b 20 6a 3c 70 2d 3e 6e 4f 70 3b 20 6a 2b Op; j<p->nOp; j+
1be4d 2b 2c 20 70 4f 70 2b 2b 29 7b 0a 20 20 20 20 20 +, pOp++){.
1be4e 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 if( pOp->opcode
1be4f 3d 3d 4f 50 5f 56 61 72 69 61 62 6c 65 20 29 7b ==OP_Variable ){
1be50 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
1be51 20 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70 4f pOp->p1>0 && pO
1be52 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 56 61 72 20 29 p->p1<=p->nVar )
1be53 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 7a 56 ;. p->azV
1be54 61 72 5b 70 4f 70 2d 3e 70 31 2d 31 5d 20 3d 20 ar[pOp->p1-1] =
1be55 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 20 pOp->p4.z;.
1be56 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e }. }. p->
1be57 6f 6b 56 61 72 20 3d 20 31 3b 0a 20 20 20 20 73 okVar = 1;. s
1be58 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
1be59 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 ve(p->db->mutex)
1be5a 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 ;. }.}../*.** R
1be5b 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f eturn the name o
1be5c 66 20 61 20 77 69 6c 64 63 61 72 64 20 70 61 72 f a wildcard par
1be5d 61 6d 65 74 65 72 2e 20 20 52 65 74 75 72 6e 20 ameter. Return
1be5e 4e 55 4c 4c 20 69 66 20 74 68 65 20 69 6e 64 65 NULL if the inde
1be5f 78 0a 2a 2a 20 69 73 20 6f 75 74 20 6f 66 20 72 x.** is out of r
1be60 61 6e 67 65 20 6f 72 20 69 66 20 74 68 65 20 77 ange or if the w
1be61 69 6c 64 63 61 72 64 20 69 73 20 75 6e 6e 61 6d ildcard is unnam
1be62 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 ed..**.** The re
1be63 73 75 6c 74 20 69 73 20 61 6c 77 61 79 73 20 55 sult is always U
1be64 54 46 2d 38 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f TF-8..*/.SQLITE_
1be65 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a API const char *
1be66 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 sqlite3_bind_par
1be67 61 6d 65 74 65 72 5f 6e 61 6d 65 28 73 71 6c 69 ameter_name(sqli
1be68 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c te3_stmt *pStmt,
1be69 20 69 6e 74 20 69 29 7b 0a 20 20 56 64 62 65 20 int i){. Vdbe
1be6a 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d *p = (Vdbe*)pStm
1be6b 74 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c t;. if( p==0 ||
1be6c 20 69 3c 31 20 7c 7c 20 69 3e 70 2d 3e 6e 56 61 i<1 || i>p->nVa
1be6d 72 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 r ){. return
1be6e 30 3b 0a 20 20 7d 0a 20 20 63 72 65 61 74 65 56 0;. }. createV
1be6f 61 72 4d 61 70 28 70 29 3b 0a 20 20 72 65 74 75 arMap(p);. retu
1be70 72 6e 20 70 2d 3e 61 7a 56 61 72 5b 69 2d 31 5d rn p->azVar[i-1]
1be71 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e ;.}../*.** Given
1be72 20 61 20 77 69 6c 64 63 61 72 64 20 70 61 72 61 a wildcard para
1be73 6d 65 74 65 72 20 6e 61 6d 65 2c 20 72 65 74 75 meter name, retu
1be74 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 rn the index of
1be75 74 68 65 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 the variable.**
1be76 77 69 74 68 20 74 68 61 74 20 6e 61 6d 65 2e 20 with that name.
1be77 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 If there is no
1be78 76 61 72 69 61 62 6c 65 20 77 69 74 68 20 74 68 variable with th
1be79 65 20 67 69 76 65 6e 20 6e 61 6d 65 2c 0a 2a 2a e given name,.**
1be7a 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 53 51 return 0..*/.SQ
1be7b 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1be7c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 ite3_bind_parame
1be7d 74 65 72 5f 69 6e 64 65 78 28 73 71 6c 69 74 65 ter_index(sqlite
1be7e 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 63 3_stmt *pStmt, c
1be7f 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 onst char *zName
1be80 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 ){. Vdbe *p = (
1be81 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 69 Vdbe*)pStmt;. i
1be82 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 3d 3d 30 nt i;. if( p==0
1be83 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 ){. return 0
1be84 3b 0a 20 20 7d 0a 20 20 63 72 65 61 74 65 56 61 ;. }. createVa
1be85 72 4d 61 70 28 70 29 3b 20 0a 20 20 69 66 28 20 rMap(p); . if(
1be86 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 66 6f 72 zName ){. for
1be87 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 56 61 72 3b (i=0; i<p->nVar;
1be88 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 6f 6e i++){. con
1be89 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 2d 3e st char *z = p->
1be8a 61 7a 56 61 72 5b 69 5d 3b 0a 20 20 20 20 20 20 azVar[i];.
1be8b 69 66 28 20 7a 20 26 26 20 73 74 72 63 6d 70 28 if( z && strcmp(
1be8c 7a 2c 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 z,zName)==0 ){.
1be8d 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 2b return i+
1be8e 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 1;. }. }
1be8f 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b . }. return 0;
1be90 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 66 .}../*.** Transf
1be91 65 72 20 61 6c 6c 20 62 69 6e 64 69 6e 67 73 20 er all bindings
1be92 66 72 6f 6d 20 74 68 65 20 66 69 72 73 74 20 73 from the first s
1be93 74 61 74 65 6d 65 6e 74 20 6f 76 65 72 20 74 6f tatement over to
1be94 20 74 68 65 20 73 65 63 6f 6e 64 2e 0a 2a 2f 0a the second..*/.
1be95 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1be96 6e 74 20 73 71 6c 69 74 65 33 54 72 61 6e 73 66 nt sqlite3Transf
1be97 65 72 42 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 erBindings(sqlit
1be98 65 33 5f 73 74 6d 74 20 2a 70 46 72 6f 6d 53 74 e3_stmt *pFromSt
1be99 6d 74 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 mt, sqlite3_stmt
1be9a 20 2a 70 54 6f 53 74 6d 74 29 7b 0a 20 20 56 64 *pToStmt){. Vd
1be9b 62 65 20 2a 70 46 72 6f 6d 20 3d 20 28 56 64 62 be *pFrom = (Vdb
1be9c 65 2a 29 70 46 72 6f 6d 53 74 6d 74 3b 0a 20 20 e*)pFromStmt;.
1be9d 56 64 62 65 20 2a 70 54 6f 20 3d 20 28 56 64 62 Vdbe *pTo = (Vdb
1be9e 65 2a 29 70 54 6f 53 74 6d 74 3b 0a 20 20 69 6e e*)pToStmt;. in
1be9f 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 70 t i;. assert( p
1bea0 54 6f 2d 3e 64 62 3d 3d 70 46 72 6f 6d 2d 3e 64 To->db==pFrom->d
1bea1 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 b );. assert( p
1bea2 54 6f 2d 3e 6e 56 61 72 3d 3d 70 46 72 6f 6d 2d To->nVar==pFrom-
1bea3 3e 6e 56 61 72 20 29 3b 0a 20 20 73 71 6c 69 74 >nVar );. sqlit
1bea4 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 e3_mutex_enter(p
1bea5 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a To->db->mutex);.
1bea6 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 46 72 for(i=0; i<pFr
1bea7 6f 6d 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29 7b 0a om->nVar; i++){.
1bea8 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1bea9 65 6d 4d 6f 76 65 28 26 70 54 6f 2d 3e 61 56 61 emMove(&pTo->aVa
1beaa 72 5b 69 5d 2c 20 26 70 46 72 6f 6d 2d 3e 61 56 r[i], &pFrom->aV
1beab 61 72 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 73 71 ar[i]);. }. sq
1beac 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
1bead 65 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78 e(pTo->db->mutex
1beae 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
1beaf 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 TE_OK;.}..#ifnde
1beb0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 f SQLITE_OMIT_DE
1beb1 50 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 44 PRECATED./*.** D
1beb2 65 70 72 65 63 61 74 65 64 20 65 78 74 65 72 6e eprecated extern
1beb3 61 6c 20 69 6e 74 65 72 66 61 63 65 2e 20 20 49 al interface. I
1beb4 6e 74 65 72 6e 61 6c 2f 63 6f 72 65 20 53 51 4c nternal/core SQL
1beb5 69 74 65 20 63 6f 64 65 0a 2a 2a 20 73 68 6f 75 ite code.** shou
1beb6 6c 64 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 54 ld call sqlite3T
1beb7 72 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 73 2e ransferBindings.
1beb8 0a 2a 2a 0a 2a 2a 20 49 73 20 69 73 20 6d 69 73 .**.** Is is mis
1beb9 75 73 65 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 use to call this
1beba 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20 73 74 routine with st
1bebb 61 74 65 6d 65 6e 74 73 20 66 72 6f 6d 20 64 69 atements from di
1bebc 66 66 65 72 65 6e 74 0a 2a 2a 20 64 61 74 61 62 fferent.** datab
1bebd 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e ase connections.
1bebe 20 20 42 75 74 20 61 73 20 74 68 69 73 20 69 73 But as this is
1bebf 20 61 20 64 65 70 72 65 63 61 74 65 64 20 69 6e a deprecated in
1bec0 74 65 72 66 61 63 65 2c 20 77 65 0a 2a 2a 20 77 terface, we.** w
1bec1 69 6c 6c 20 6e 6f 74 20 62 6f 74 68 65 72 20 74 ill not bother t
1bec2 6f 20 63 68 65 63 6b 20 66 6f 72 20 74 68 61 74 o check for that
1bec3 20 63 6f 6e 64 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a condition..**.*
1bec4 2a 20 49 66 20 74 68 65 20 74 77 6f 20 73 74 61 * If the two sta
1bec5 74 65 6d 65 6e 74 73 20 63 6f 6e 74 61 69 6e 20 tements contain
1bec6 61 20 64 69 66 66 65 72 65 6e 74 20 6e 75 6d 62 a different numb
1bec7 65 72 20 6f 66 20 62 69 6e 64 69 6e 67 73 2c 20 er of bindings,
1bec8 74 68 65 6e 0a 2a 2a 20 61 6e 20 53 51 4c 49 54 then.** an SQLIT
1bec9 45 5f 45 52 52 4f 52 20 69 73 20 72 65 74 75 72 E_ERROR is retur
1beca 6e 65 64 2e 20 20 4e 6f 74 68 69 6e 67 20 65 6c ned. Nothing el
1becb 73 65 20 63 61 6e 20 67 6f 20 77 72 6f 6e 67 2c se can go wrong,
1becc 20 73 6f 20 6f 74 68 65 72 77 69 73 65 0a 2a 2a so otherwise.**
1becd 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
1bece 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 turned..*/.SQLIT
1becf 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1bed0 33 5f 74 72 61 6e 73 66 65 72 5f 62 69 6e 64 69 3_transfer_bindi
1bed1 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 ngs(sqlite3_stmt
1bed2 20 2a 70 46 72 6f 6d 53 74 6d 74 2c 20 73 71 6c *pFromStmt, sql
1bed3 69 74 65 33 5f 73 74 6d 74 20 2a 70 54 6f 53 74 ite3_stmt *pToSt
1bed4 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 46 72 mt){. Vdbe *pFr
1bed5 6f 6d 20 3d 20 28 56 64 62 65 2a 29 70 46 72 6f om = (Vdbe*)pFro
1bed6 6d 53 74 6d 74 3b 0a 20 20 56 64 62 65 20 2a 70 mStmt;. Vdbe *p
1bed7 54 6f 20 3d 20 28 56 64 62 65 2a 29 70 54 6f 53 To = (Vdbe*)pToS
1bed8 74 6d 74 3b 0a 20 20 69 66 28 20 70 46 72 6f 6d tmt;. if( pFrom
1bed9 2d 3e 6e 56 61 72 21 3d 70 54 6f 2d 3e 6e 56 61 ->nVar!=pTo->nVa
1beda 72 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 r ){. return
1bedb 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 SQLITE_ERROR;.
1bedc 7d 0a 20 20 69 66 28 20 70 54 6f 2d 3e 69 73 50 }. if( pTo->isP
1bedd 72 65 70 61 72 65 56 32 20 26 26 20 70 54 6f 2d repareV2 && pTo-
1bede 3e 65 78 70 6d 61 73 6b 20 29 7b 0a 20 20 20 20 >expmask ){.
1bedf 70 54 6f 2d 3e 65 78 70 69 72 65 64 20 3d 20 31 pTo->expired = 1
1bee0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 46 72 6f ;. }. if( pFro
1bee1 6d 2d 3e 69 73 50 72 65 70 61 72 65 56 32 20 26 m->isPrepareV2 &
1bee2 26 20 70 46 72 6f 6d 2d 3e 65 78 70 6d 61 73 6b & pFrom->expmask
1bee3 20 29 7b 0a 20 20 20 20 70 46 72 6f 6d 2d 3e 65 ){. pFrom->e
1bee4 78 70 69 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a xpired = 1;. }.
1bee5 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
1bee6 54 72 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 73 TransferBindings
1bee7 28 70 46 72 6f 6d 53 74 6d 74 2c 20 70 54 6f 53 (pFromStmt, pToS
1bee8 74 6d 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a tmt);.}.#endif..
1bee9 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
1beea 20 73 71 6c 69 74 65 33 2a 20 64 61 74 61 62 61 sqlite3* databa
1beeb 73 65 20 68 61 6e 64 6c 65 20 74 6f 20 77 68 69 se handle to whi
1beec 63 68 20 74 68 65 20 70 72 65 70 61 72 65 64 20 ch the prepared
1beed 73 74 61 74 65 6d 65 6e 74 20 67 69 76 65 6e 0a statement given.
1beee 2a 2a 20 69 6e 20 74 68 65 20 61 72 67 75 6d 65 ** in the argume
1beef 6e 74 20 62 65 6c 6f 6e 67 73 2e 20 20 54 68 69 nt belongs. Thi
1bef0 73 20 69 73 20 74 68 65 20 73 61 6d 65 20 64 61 s is the same da
1bef1 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 74 68 tabase handle th
1bef2 61 74 20 77 61 73 0a 2a 2a 20 74 68 65 20 66 69 at was.** the fi
1bef3 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 rst argument to
1bef4 74 68 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 the sqlite3_prep
1bef5 61 72 65 28 29 20 74 68 61 74 20 77 61 73 20 75 are() that was u
1bef6 73 65 64 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a sed to create.**
1bef7 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 the statement i
1bef8 6e 20 74 68 65 20 66 69 72 73 74 20 70 6c 61 63 n the first plac
1bef9 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 e..*/.SQLITE_API
1befa 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 sqlite3 *sqlite
1befb 33 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 6c 69 3_db_handle(sqli
1befc 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 te3_stmt *pStmt)
1befd 7b 0a 20 20 72 65 74 75 72 6e 20 70 53 74 6d 74 {. return pStmt
1befe 20 3f 20 28 28 56 64 62 65 2a 29 70 53 74 6d 74 ? ((Vdbe*)pStmt
1beff 29 2d 3e 64 62 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a )->db : 0;.}../*
1bf00 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 .** Return a poi
1bf01 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 78 74 nter to the next
1bf02 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d prepared statem
1bf03 65 6e 74 20 61 66 74 65 72 20 70 53 74 6d 74 20 ent after pStmt
1bf04 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 associated.** wi
1bf05 74 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e th database conn
1bf06 65 63 74 69 6f 6e 20 70 44 62 2e 20 20 49 66 20 ection pDb. If
1bf07 70 53 74 6d 74 20 69 73 20 4e 55 4c 4c 2c 20 72 pStmt is NULL, r
1bf08 65 74 75 72 6e 20 74 68 65 20 66 69 72 73 74 0a eturn the first.
1bf09 2a 2a 20 70 72 65 70 61 72 65 64 20 73 74 61 74 ** prepared stat
1bf0a 65 6d 65 6e 74 20 66 6f 72 20 74 68 65 20 64 61 ement for the da
1bf0b 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
1bf0c 6e 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 n. Return NULL
1bf0d 69 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 if there.** are
1bf0e 6e 6f 20 6d 6f 72 65 2e 0a 2a 2f 0a 53 51 4c 49 no more..*/.SQLI
1bf0f 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 73 TE_API sqlite3_s
1bf10 74 6d 74 20 2a 73 71 6c 69 74 65 33 5f 6e 65 78 tmt *sqlite3_nex
1bf11 74 5f 73 74 6d 74 28 73 71 6c 69 74 65 33 20 2a t_stmt(sqlite3 *
1bf12 70 44 62 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d pDb, sqlite3_stm
1bf13 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 73 71 6c t *pStmt){. sql
1bf14 69 74 65 33 5f 73 74 6d 74 20 2a 70 4e 65 78 74 ite3_stmt *pNext
1bf15 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ;. sqlite3_mute
1bf16 78 5f 65 6e 74 65 72 28 70 44 62 2d 3e 6d 75 74 x_enter(pDb->mut
1bf17 65 78 29 3b 0a 20 20 69 66 28 20 70 53 74 6d 74 ex);. if( pStmt
1bf18 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4e 65 78 74 ==0 ){. pNext
1bf19 20 3d 20 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 = (sqlite3_stmt
1bf1a 2a 29 70 44 62 2d 3e 70 56 64 62 65 3b 0a 20 20 *)pDb->pVdbe;.
1bf1b 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 78 74 }else{. pNext
1bf1c 20 3d 20 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 = (sqlite3_stmt
1bf1d 2a 29 28 28 56 64 62 65 2a 29 70 53 74 6d 74 29 *)((Vdbe*)pStmt)
1bf1e 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 73 ->pNext;. }. s
1bf1f 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
1bf20 76 65 28 70 44 62 2d 3e 6d 75 74 65 78 29 3b 0a ve(pDb->mutex);.
1bf21 20 20 72 65 74 75 72 6e 20 70 4e 65 78 74 3b 0a return pNext;.
1bf22 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
1bf23 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20 73 the value of a s
1bf24 74 61 74 75 73 20 63 6f 75 6e 74 65 72 20 66 6f tatus counter fo
1bf25 72 20 61 20 70 72 65 70 61 72 65 64 20 73 74 61 r a prepared sta
1bf26 74 65 6d 65 6e 74 0a 2a 2f 0a 53 51 4c 49 54 45 tement.*/.SQLITE
1bf27 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
1bf28 5f 73 74 6d 74 5f 73 74 61 74 75 73 28 73 71 6c _stmt_status(sql
1bf29 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1bf2a 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 72 65 , int op, int re
1bf2b 73 65 74 46 6c 61 67 29 7b 0a 20 20 56 64 62 65 setFlag){. Vdbe
1bf2c 20 2a 70 56 64 62 65 20 3d 20 28 56 64 62 65 2a *pVdbe = (Vdbe*
1bf2d 29 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20 76 20 )pStmt;. int v
1bf2e 3d 20 70 56 64 62 65 2d 3e 61 43 6f 75 6e 74 65 = pVdbe->aCounte
1bf2f 72 5b 6f 70 2d 31 5d 3b 0a 20 20 69 66 28 20 72 r[op-1];. if( r
1bf30 65 73 65 74 46 6c 61 67 20 29 20 70 56 64 62 65 esetFlag ) pVdbe
1bf31 2d 3e 61 43 6f 75 6e 74 65 72 5b 6f 70 2d 31 5d ->aCounter[op-1]
1bf32 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 76 = 0;. return v
1bf33 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ;.}../**********
1bf34 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 **** End of vdbe
1bf35 61 70 69 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a api.c **********
1bf36 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bf37 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bf38 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
1bf39 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
1bf3a 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a vdbe.c *********
1bf3b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bf3c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bf3d 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 ***/./*.** 2001
1bf3e 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a September 15.**.
1bf3f 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
1bf40 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
1bf41 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
1bf42 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
1bf43 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
1bf44 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
1bf45 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
1bf46 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
1bf47 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
1bf48 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
1bf49 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
1bf4a 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
1bf4b 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
1bf4c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
1bf4d 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
1bf4e 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
1bf4f 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
1bf50 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
1bf51 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bf52 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bf53 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bf54 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 ***********.** T
1bf55 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 he code in this
1bf56 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 file implements
1bf57 65 78 65 63 75 74 69 6f 6e 20 6d 65 74 68 6f 64 execution method
1bf58 20 6f 66 20 74 68 65 20 0a 2a 2a 20 56 69 72 74 of the .** Virt
1bf59 75 61 6c 20 44 61 74 61 62 61 73 65 20 45 6e 67 ual Database Eng
1bf5a 69 6e 65 20 28 56 44 42 45 29 2e 20 20 41 20 73 ine (VDBE). A s
1bf5b 65 70 61 72 61 74 65 20 66 69 6c 65 20 28 22 76 eparate file ("v
1bf5c 64 62 65 61 75 78 2e 63 22 29 0a 2a 2a 20 68 61 dbeaux.c").** ha
1bf5d 6e 64 6c 65 73 20 68 6f 75 73 65 6b 65 65 70 69 ndles housekeepi
1bf5e 6e 67 20 64 65 74 61 69 6c 73 20 73 75 63 68 20 ng details such
1bf5f 61 73 20 63 72 65 61 74 69 6e 67 20 61 6e 64 20 as creating and
1bf60 64 65 6c 65 74 69 6e 67 0a 2a 2a 20 56 44 42 45 deleting.** VDBE
1bf61 20 69 6e 73 74 61 6e 63 65 73 2e 20 20 54 68 69 instances. Thi
1bf62 73 20 66 69 6c 65 20 69 73 20 73 6f 6c 65 6c 79 s file is solely
1bf63 20 69 6e 74 65 72 65 73 74 65 64 20 69 6e 20 65 interested in e
1bf64 78 65 63 75 74 69 6e 67 0a 2a 2a 20 74 68 65 20 xecuting.** the
1bf65 56 44 42 45 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a VDBE program..**
1bf66 0a 2a 2a 20 49 6e 20 74 68 65 20 65 78 74 65 72 .** In the exter
1bf67 6e 61 6c 20 69 6e 74 65 72 66 61 63 65 2c 20 61 nal interface, a
1bf68 6e 20 22 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a n "sqlite3_stmt*
1bf69 22 20 69 73 20 61 6e 20 6f 70 61 71 75 65 20 70 " is an opaque p
1bf6a 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 20 56 ointer.** to a V
1bf6b 44 42 45 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 DBE..**.** The S
1bf6c 51 4c 20 70 61 72 73 65 72 20 67 65 6e 65 72 61 QL parser genera
1bf6d 74 65 73 20 61 20 70 72 6f 67 72 61 6d 20 77 68 tes a program wh
1bf6e 69 63 68 20 69 73 20 74 68 65 6e 20 65 78 65 63 ich is then exec
1bf6f 75 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 56 uted by.** the V
1bf70 44 42 45 20 74 6f 20 64 6f 20 74 68 65 20 77 6f DBE to do the wo
1bf71 72 6b 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74 rk of the SQL st
1bf72 61 74 65 6d 65 6e 74 2e 20 20 56 44 42 45 20 70 atement. VDBE p
1bf73 72 6f 67 72 61 6d 73 20 61 72 65 20 0a 2a 2a 20 rograms are .**
1bf74 73 69 6d 69 6c 61 72 20 69 6e 20 66 6f 72 6d 20 similar in form
1bf75 74 6f 20 61 73 73 65 6d 62 6c 79 20 6c 61 6e 67 to assembly lang
1bf76 75 61 67 65 2e 20 20 54 68 65 20 70 72 6f 67 72 uage. The progr
1bf77 61 6d 20 63 6f 6e 73 69 73 74 73 20 6f 66 0a 2a am consists of.*
1bf78 2a 20 61 20 6c 69 6e 65 61 72 20 73 65 71 75 65 * a linear seque
1bf79 6e 63 65 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e nce of operation
1bf7a 73 2e 20 20 45 61 63 68 20 6f 70 65 72 61 74 69 s. Each operati
1bf7b 6f 6e 20 68 61 73 20 61 6e 20 6f 70 63 6f 64 65 on has an opcode
1bf7c 20 0a 2a 2a 20 61 6e 64 20 35 20 6f 70 65 72 61 .** and 5 opera
1bf7d 6e 64 73 2e 20 20 4f 70 65 72 61 6e 64 73 20 50 nds. Operands P
1bf7e 31 2c 20 50 32 2c 20 61 6e 64 20 50 33 20 61 72 1, P2, and P3 ar
1bf7f 65 20 69 6e 74 65 67 65 72 73 2e 20 20 4f 70 65 e integers. Ope
1bf80 72 61 6e 64 20 50 34 20 0a 2a 2a 20 69 73 20 61 rand P4 .** is a
1bf81 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 64 null-terminated
1bf82 20 73 74 72 69 6e 67 2e 20 20 4f 70 65 72 61 6e string. Operan
1bf83 64 20 50 35 20 69 73 20 61 6e 20 75 6e 73 69 67 d P5 is an unsig
1bf84 6e 65 64 20 63 68 61 72 61 63 74 65 72 2e 0a 2a ned character..*
1bf85 2a 20 46 65 77 20 6f 70 63 6f 64 65 73 20 75 73 * Few opcodes us
1bf86 65 20 61 6c 6c 20 35 20 6f 70 65 72 61 6e 64 73 e all 5 operands
1bf87 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 75 74 61 74 ..**.** Computat
1bf88 69 6f 6e 20 72 65 73 75 6c 74 73 20 61 72 65 20 ion results are
1bf89 73 74 6f 72 65 64 20 6f 6e 20 61 20 73 65 74 20 stored on a set
1bf8a 6f 66 20 72 65 67 69 73 74 65 72 73 20 6e 75 6d of registers num
1bf8b 62 65 72 65 64 20 62 65 67 69 6e 6e 69 6e 67 0a bered beginning.
1bf8c 2a 2a 20 77 69 74 68 20 31 20 61 6e 64 20 67 6f ** with 1 and go
1bf8d 69 6e 67 20 75 70 20 74 6f 20 56 64 62 65 2e 6e ing up to Vdbe.n
1bf8e 4d 65 6d 2e 20 20 45 61 63 68 20 72 65 67 69 73 Mem. Each regis
1bf8f 74 65 72 20 63 61 6e 20 73 74 6f 72 65 0a 2a 2a ter can store.**
1bf90 20 65 69 74 68 65 72 20 61 6e 20 69 6e 74 65 67 either an integ
1bf91 65 72 2c 20 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 er, a null-termi
1bf92 6e 61 74 65 64 20 73 74 72 69 6e 67 2c 20 61 20 nated string, a
1bf93 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a floating point.*
1bf94 2a 20 6e 75 6d 62 65 72 2c 20 6f 72 20 74 68 65 * number, or the
1bf95 20 53 51 4c 20 22 4e 55 4c 4c 22 20 76 61 6c 75 SQL "NULL" valu
1bf96 65 2e 20 20 41 6e 20 69 6d 70 6c 69 63 69 74 20 e. An implicit
1bf97 63 6f 6e 76 65 72 73 69 6f 6e 20 66 72 6f 6d 20 conversion from
1bf98 6f 6e 65 0a 2a 2a 20 74 79 70 65 20 74 6f 20 74 one.** type to t
1bf99 68 65 20 6f 74 68 65 72 20 6f 63 63 75 72 73 20 he other occurs
1bf9a 61 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a as necessary..**
1bf9b 20 0a 2a 2a 20 4d 6f 73 74 20 6f 66 20 74 68 65 .** Most of the
1bf9c 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 code in this fi
1bf9d 6c 65 20 69 73 20 74 61 6b 65 6e 20 75 70 20 62 le is taken up b
1bf9e 79 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 62 y the sqlite3Vdb
1bf9f 65 45 78 65 63 28 29 0a 2a 2a 20 66 75 6e 63 74 eExec().** funct
1bfa0 69 6f 6e 20 77 68 69 63 68 20 64 6f 65 73 20 74 ion which does t
1bfa1 68 65 20 77 6f 72 6b 20 6f 66 20 69 6e 74 65 72 he work of inter
1bfa2 70 72 65 74 69 6e 67 20 61 20 56 44 42 45 20 70 preting a VDBE p
1bfa3 72 6f 67 72 61 6d 2e 0a 2a 2a 20 42 75 74 20 6f rogram..** But o
1bfa4 74 68 65 72 20 72 6f 75 74 69 6e 65 73 20 61 72 ther routines ar
1bfa5 65 20 61 6c 73 6f 20 70 72 6f 76 69 64 65 64 20 e also provided
1bfa6 74 6f 20 68 65 6c 70 20 69 6e 20 62 75 69 6c 64 to help in build
1bfa7 69 6e 67 20 75 70 0a 2a 2a 20 61 20 70 72 6f 67 ing up.** a prog
1bfa8 72 61 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 ram instruction
1bfa9 62 79 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a by instruction..
1bfaa 2a 2a 0a 2a 2a 20 56 61 72 69 6f 75 73 20 73 63 **.** Various sc
1bfab 72 69 70 74 73 20 73 63 61 6e 20 74 68 69 73 20 ripts scan this
1bfac 73 6f 75 72 63 65 20 66 69 6c 65 20 69 6e 20 6f source file in o
1bfad 72 64 65 72 20 74 6f 20 67 65 6e 65 72 61 74 65 rder to generate
1bfae 20 48 54 4d 4c 0a 2a 2a 20 64 6f 63 75 6d 65 6e HTML.** documen
1bfaf 74 61 74 69 6f 6e 2c 20 68 65 61 64 65 72 73 20 tation, headers
1bfb0 66 69 6c 65 73 2c 20 6f 72 20 6f 74 68 65 72 20 files, or other
1bfb1 64 65 72 69 76 65 64 20 66 69 6c 65 73 2e 20 20 derived files.
1bfb2 54 68 65 20 66 6f 72 6d 61 74 74 69 6e 67 0a 2a The formatting.*
1bfb3 2a 20 6f 66 20 74 68 65 20 63 6f 64 65 20 69 6e * of the code in
1bfb4 20 74 68 69 73 20 66 69 6c 65 20 69 73 2c 20 74 this file is, t
1bfb5 68 65 72 65 66 6f 72 65 2c 20 69 6d 70 6f 72 74 herefore, import
1bfb6 61 6e 74 2e 20 20 53 65 65 20 6f 74 68 65 72 20 ant. See other
1bfb7 63 6f 6d 6d 65 6e 74 73 0a 2a 2a 20 69 6e 20 74 comments.** in t
1bfb8 68 69 73 20 66 69 6c 65 20 66 6f 72 20 64 65 74 his file for det
1bfb9 61 69 6c 73 2e 20 20 49 66 20 69 6e 20 64 6f 75 ails. If in dou
1bfba 62 74 2c 20 64 6f 20 6e 6f 74 20 64 65 76 69 61 bt, do not devia
1bfbb 74 65 20 66 72 6f 6d 20 65 78 69 73 74 69 6e 67 te from existing
1bfbc 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 69 6e 67 20 61 .** commenting a
1bfbd 6e 64 20 69 6e 64 65 6e 74 61 74 69 6f 6e 20 70 nd indentation p
1bfbe 72 61 63 74 69 63 65 73 20 77 68 65 6e 20 63 68 ractices when ch
1bfbf 61 6e 67 69 6e 67 20 6f 72 20 61 64 64 69 6e 67 anging or adding
1bfc0 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 code..**.** $Id
1bfc1 3a 20 76 64 62 65 2e 63 2c 76 20 31 2e 38 37 34 : vdbe.c,v 1.874
1bfc2 20 32 30 30 39 2f 30 37 2f 32 34 20 31 37 3a 35 2009/07/24 17:5
1bfc3 38 3a 35 33 20 64 61 6e 69 65 6c 6b 31 39 37 37 8:53 danielk1977
1bfc4 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a Exp $.*/../*.**
1bfc5 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 The following g
1bfc6 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 69 lobal variable i
1bfc7 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 65 76 s incremented ev
1bfc8 65 72 79 20 74 69 6d 65 20 61 20 63 75 72 73 6f ery time a curso
1bfc9 72 0a 2a 2a 20 6d 6f 76 65 73 2c 20 65 69 74 68 r.** moves, eith
1bfca 65 72 20 62 79 20 74 68 65 20 4f 50 5f 53 65 65 er by the OP_See
1bfcb 6b 58 58 2c 20 4f 50 5f 4e 65 78 74 2c 20 6f 72 kXX, OP_Next, or
1bfcc 20 4f 50 5f 50 72 65 76 20 6f 70 63 6f 64 65 73 OP_Prev opcodes
1bfcd 2e 20 20 54 68 65 20 74 65 73 74 0a 2a 2a 20 70 . The test.** p
1bfce 72 6f 63 65 64 75 72 65 73 20 75 73 65 20 74 68 rocedures use th
1bfcf 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 is information t
1bfd0 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 o make sure that
1bfd1 20 69 6e 64 69 63 65 73 20 61 72 65 0a 2a 2a 20 indices are.**
1bfd2 77 6f 72 6b 69 6e 67 20 63 6f 72 72 65 63 74 6c working correctl
1bfd3 79 2e 20 20 54 68 69 73 20 76 61 72 69 61 62 6c y. This variabl
1bfd4 65 20 68 61 73 20 6e 6f 20 66 75 6e 63 74 69 6f e has no functio
1bfd5 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 74 6f 0a n other than to.
1bfd6 2a 2a 20 68 65 6c 70 20 76 65 72 69 66 79 20 74 ** help verify t
1bfd7 68 65 20 63 6f 72 72 65 63 74 20 6f 70 65 72 61 he correct opera
1bfd8 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 62 72 tion of the libr
1bfd9 61 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 ary..*/.#ifdef S
1bfda 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 QLITE_TEST.SQLIT
1bfdb 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1bfdc 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 20 3d 3_search_count =
1bfdd 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 0;.#endif../*.*
1bfde 2a 20 57 68 65 6e 20 74 68 69 73 20 67 6c 6f 62 * When this glob
1bfdf 61 6c 20 76 61 72 69 61 62 6c 65 20 69 73 20 70 al variable is p
1bfe0 6f 73 69 74 69 76 65 2c 20 69 74 20 67 65 74 73 ositive, it gets
1bfe1 20 64 65 63 72 65 6d 65 6e 74 65 64 20 6f 6e 63 decremented onc
1bfe2 65 20 62 65 66 6f 72 65 0a 2a 2a 20 65 61 63 68 e before.** each
1bfe3 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 20 instruction in
1bfe4 74 68 65 20 56 44 42 45 2e 20 20 57 68 65 6e 20 the VDBE. When
1bfe5 72 65 61 63 68 65 73 20 7a 65 72 6f 2c 20 74 68 reaches zero, th
1bfe6 65 20 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 e u1.isInterrupt
1bfe7 65 64 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 20 74 ed.** field of t
1bfe8 68 65 20 73 71 6c 69 74 65 33 20 73 74 72 75 63 he sqlite3 struc
1bfe9 74 75 72 65 20 69 73 20 73 65 74 20 69 6e 20 6f ture is set in o
1bfea 72 64 65 72 20 74 6f 20 73 69 6d 75 6c 61 74 65 rder to simulate
1bfeb 20 61 6e 64 20 69 6e 74 65 72 72 75 70 74 2e 0a and interrupt..
1bfec 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 61 63 69 6c **.** This facil
1bfed 69 74 79 20 69 73 20 75 73 65 64 20 66 6f 72 20 ity is used for
1bfee 74 65 73 74 69 6e 67 20 70 75 72 70 6f 73 65 73 testing purposes
1bfef 20 6f 6e 6c 79 2e 20 20 49 74 20 64 6f 65 73 20 only. It does
1bff0 6e 6f 74 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 not function.**
1bff1 69 6e 20 61 6e 20 6f 72 64 69 6e 61 72 79 20 62 in an ordinary b
1bff2 75 69 6c 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 uild..*/.#ifdef
1bff3 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 SQLITE_TEST.SQLI
1bff4 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
1bff5 65 33 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f 75 e3_interrupt_cou
1bff6 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a nt = 0;.#endif..
1bff7 2f 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 67 /*.** The next g
1bff8 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 69 lobal variable i
1bff9 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 65 61 s incremented ea
1bffa 63 68 20 74 79 70 65 20 74 68 65 20 4f 50 5f 53 ch type the OP_S
1bffb 6f 72 74 20 6f 70 63 6f 64 65 0a 2a 2a 20 69 73 ort opcode.** is
1bffc 20 65 78 65 63 75 74 65 64 2e 20 20 54 68 65 20 executed. The
1bffd 74 65 73 74 20 70 72 6f 63 65 64 75 72 65 73 20 test procedures
1bffe 75 73 65 20 74 68 69 73 20 69 6e 66 6f 72 6d 61 use this informa
1bfff 74 69 6f 6e 20 74 6f 20 6d 61 6b 65 20 73 75 72 tion to make sur
1c000 65 20 74 68 61 74 0a 2a 2a 20 73 6f 72 74 69 6e e that.** sortin
1c001 67 20 69 73 20 6f 63 63 75 72 72 69 6e 67 20 6f g is occurring o
1c002 72 20 6e 6f 74 20 6f 63 63 75 72 72 69 6e 67 20 r not occurring
1c003 61 74 20 61 70 70 72 6f 70 72 69 61 74 65 20 74 at appropriate t
1c004 69 6d 65 73 2e 20 20 20 54 68 69 73 20 76 61 72 imes. This var
1c005 69 61 62 6c 65 0a 2a 2a 20 68 61 73 20 6e 6f 20 iable.** has no
1c006 66 75 6e 63 74 69 6f 6e 20 6f 74 68 65 72 20 74 function other t
1c007 68 61 6e 20 74 6f 20 68 65 6c 70 20 76 65 72 69 han to help veri
1c008 66 79 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f fy the correct o
1c009 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a peration of the.
1c00a 2a 2a 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 ** library..*/.#
1c00b 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
1c00c 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 T.SQLITE_API int
1c00d 20 73 71 6c 69 74 65 33 5f 73 6f 72 74 5f 63 6f sqlite3_sort_co
1c00e 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a unt = 0;.#endif.
1c00f 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 ./*.** The next
1c010 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 global variable
1c011 72 65 63 6f 72 64 73 20 74 68 65 20 73 69 7a 65 records the size
1c012 20 6f 66 20 74 68 65 20 6c 61 72 67 65 73 74 20 of the largest
1c013 4d 45 4d 5f 42 6c 6f 62 0a 2a 2a 20 6f 72 20 4d MEM_Blob.** or M
1c014 45 4d 5f 53 74 72 20 74 68 61 74 20 68 61 73 20 EM_Str that has
1c015 62 65 65 6e 20 75 73 65 64 20 62 79 20 61 20 56 been used by a V
1c016 44 42 45 20 6f 70 63 6f 64 65 2e 20 20 54 68 65 DBE opcode. The
1c017 20 74 65 73 74 20 70 72 6f 63 65 64 75 72 65 73 test procedures
1c018 0a 2a 2a 20 75 73 65 20 74 68 69 73 20 69 6e 66 .** use this inf
1c019 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 6d 61 6b 65 ormation to make
1c01a 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20 7a sure that the z
1c01b 65 72 6f 2d 62 6c 6f 62 20 66 75 6e 63 74 69 6f ero-blob functio
1c01c 6e 61 6c 69 74 79 0a 2a 2a 20 69 73 20 77 6f 72 nality.** is wor
1c01d 6b 69 6e 67 20 63 6f 72 72 65 63 74 6c 79 2e 20 king correctly.
1c01e 20 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 20 This variable
1c01f 68 61 73 20 6e 6f 20 66 75 6e 63 74 69 6f 6e 20 has no function
1c020 6f 74 68 65 72 20 74 68 61 6e 20 74 6f 0a 2a 2a other than to.**
1c021 20 68 65 6c 70 20 76 65 72 69 66 79 20 74 68 65 help verify the
1c022 20 63 6f 72 72 65 63 74 20 6f 70 65 72 61 74 69 correct operati
1c023 6f 6e 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 on of the librar
1c024 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c y..*/.#ifdef SQL
1c025 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f ITE_TEST.SQLITE_
1c026 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1c027 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 20 3d 20 30 max_blobsize = 0
1c028 3b 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 70 ;.static void up
1c029 64 61 74 65 4d 61 78 42 6c 6f 62 73 69 7a 65 28 dateMaxBlobsize(
1c02a 4d 65 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 28 Mem *p){. if( (
1c02b 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f p->flags & (MEM_
1c02c 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 21 3d Str|MEM_Blob))!=
1c02d 30 20 26 26 20 70 2d 3e 6e 3e 73 71 6c 69 74 65 0 && p->n>sqlite
1c02e 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 20 29 3_max_blobsize )
1c02f 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 61 {. sqlite3_ma
1c030 78 5f 62 6c 6f 62 73 69 7a 65 20 3d 20 70 2d 3e x_blobsize = p->
1c031 6e 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a n;. }.}.#endif.
1c032 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 ./*.** The next
1c033 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 global variable
1c034 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 65 is incremented e
1c035 61 63 68 20 74 79 70 65 20 74 68 65 20 4f 50 5f ach type the OP_
1c036 46 6f 75 6e 64 20 6f 70 63 6f 64 65 0a 2a 2a 20 Found opcode.**
1c037 69 73 20 65 78 65 63 75 74 65 64 2e 20 54 68 69 is executed. Thi
1c038 73 20 69 73 20 75 73 65 64 20 74 6f 20 74 65 73 s is used to tes
1c039 74 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 t whether or not
1c03a 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 the foreign key
1c03b 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 69 6d .** operation im
1c03c 70 6c 65 6d 65 6e 74 65 64 20 75 73 69 6e 67 20 plemented using
1c03d 4f 50 5f 46 6b 49 73 5a 65 72 6f 20 69 73 20 77 OP_FkIsZero is w
1c03e 6f 72 6b 69 6e 67 2e 20 54 68 69 73 20 76 61 72 orking. This var
1c03f 69 61 62 6c 65 0a 2a 2a 20 68 61 73 20 6e 6f 20 iable.** has no
1c040 66 75 6e 63 74 69 6f 6e 20 6f 74 68 65 72 20 74 function other t
1c041 68 61 6e 20 74 6f 20 68 65 6c 70 20 76 65 72 69 han to help veri
1c042 66 79 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f fy the correct o
1c043 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a peration of the.
1c044 2a 2a 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 ** library..*/.#
1c045 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
1c046 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 T.SQLITE_API int
1c047 20 73 71 6c 69 74 65 33 5f 66 6f 75 6e 64 5f 63 sqlite3_found_c
1c048 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 ount = 0;.#endif
1c049 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20 61 20 72 ../*.** Test a r
1c04a 65 67 69 73 74 65 72 20 74 6f 20 73 65 65 20 69 egister to see i
1c04b 66 20 69 74 20 65 78 63 65 65 64 73 20 74 68 65 f it exceeds the
1c04c 20 63 75 72 72 65 6e 74 20 6d 61 78 69 6d 75 6d current maximum
1c04d 20 62 6c 6f 62 20 73 69 7a 65 2e 0a 2a 2a 20 49 blob size..** I
1c04e 66 20 69 74 20 64 6f 65 73 2c 20 72 65 63 6f 72 f it does, recor
1c04f 64 20 74 68 65 20 6e 65 77 20 6d 61 78 69 6d 75 d the new maximu
1c050 6d 20 62 6c 6f 62 20 73 69 7a 65 2e 0a 2a 2f 0a m blob size..*/.
1c051 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 #if defined(SQLI
1c052 54 45 5f 54 45 53 54 29 20 26 26 20 21 64 65 66 TE_TEST) && !def
1c053 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
1c054 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 29 0a 23 _BUILTIN_TEST).#
1c055 20 64 65 66 69 6e 65 20 55 50 44 41 54 45 5f 4d define UPDATE_M
1c056 41 58 5f 42 4c 4f 42 53 49 5a 45 28 50 29 20 20 AX_BLOBSIZE(P)
1c057 75 70 64 61 74 65 4d 61 78 42 6c 6f 62 73 69 7a updateMaxBlobsiz
1c058 65 28 50 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 e(P).#else.# def
1c059 69 6e 65 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 ine UPDATE_MAX_B
1c05a 4c 4f 42 53 49 5a 45 28 50 29 0a 23 65 6e 64 69 LOBSIZE(P).#endi
1c05b 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 f../*.** Convert
1c05c 20 74 68 65 20 67 69 76 65 6e 20 72 65 67 69 73 the given regis
1c05d 74 65 72 20 69 6e 74 6f 20 61 20 73 74 72 69 6e ter into a strin
1c05e 67 20 69 66 20 69 74 20 69 73 6e 27 74 20 6f 6e g if it isn't on
1c05f 65 0a 2a 2a 20 61 6c 72 65 61 64 79 2e 20 52 65 e.** already. Re
1c060 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 turn non-zero if
1c061 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c a malloc() fail
1c062 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 74 s..*/.#define St
1c063 72 69 6e 67 69 66 79 28 50 2c 20 65 6e 63 29 20 ringify(P, enc)
1c064 5c 0a 20 20 20 69 66 28 28 28 50 29 2d 3e 66 6c \. if(((P)->fl
1c065 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d ags&(MEM_Str|MEM
1c066 5f 42 6c 6f 62 29 29 3d 3d 30 20 26 26 20 73 71 _Blob))==0 && sq
1c067 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69 lite3VdbeMemStri
1c068 6e 67 69 66 79 28 50 2c 65 6e 63 29 29 20 5c 0a ngify(P,enc)) \.
1c069 20 20 20 20 20 7b 20 67 6f 74 6f 20 6e 6f 5f 6d { goto no_m
1c06a 65 6d 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 em; }../*.** An
1c06b 65 70 68 65 6d 65 72 61 6c 20 73 74 72 69 6e 67 ephemeral string
1c06c 20 76 61 6c 75 65 20 28 73 69 67 6e 69 66 69 65 value (signifie
1c06d 64 20 62 79 20 74 68 65 20 4d 45 4d 5f 45 70 68 d by the MEM_Eph
1c06e 65 6d 20 66 6c 61 67 29 20 63 6f 6e 74 61 69 6e em flag) contain
1c06f 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 s.** a pointer t
1c070 6f 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 o a dynamically
1c071 61 6c 6c 6f 63 61 74 65 64 20 73 74 72 69 6e 67 allocated string
1c072 20 77 68 65 72 65 20 73 6f 6d 65 20 6f 74 68 65 where some othe
1c073 72 20 65 6e 74 69 74 79 0a 2a 2a 20 69 73 20 72 r entity.** is r
1c074 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 64 esponsible for d
1c075 65 61 6c 6c 6f 63 61 74 69 6e 67 20 74 68 61 74 eallocating that
1c076 20 73 74 72 69 6e 67 2e 20 20 42 65 63 61 75 73 string. Becaus
1c077 65 20 74 68 65 20 72 65 67 69 73 74 65 72 0a 2a e the register.*
1c078 2a 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 72 * does not contr
1c079 6f 6c 20 74 68 65 20 73 74 72 69 6e 67 2c 20 69 ol the string, i
1c07a 74 20 6d 69 67 68 74 20 62 65 20 64 65 6c 65 74 t might be delet
1c07b 65 64 20 77 69 74 68 6f 75 74 20 74 68 65 20 72 ed without the r
1c07c 65 67 69 73 74 65 72 0a 2a 2a 20 6b 6e 6f 77 69 egister.** knowi
1c07d 6e 67 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 ng it..**.** Thi
1c07e 73 20 72 6f 75 74 69 6e 65 20 63 6f 6e 76 65 72 s routine conver
1c07f 74 73 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 20 ts an ephemeral
1c080 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20 64 79 string into a dy
1c081 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 namically alloca
1c082 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20 74 68 ted.** string th
1c083 61 74 20 74 68 65 20 72 65 67 69 73 74 65 72 20 at the register
1c084 69 74 73 65 6c 66 20 63 6f 6e 74 72 6f 6c 73 2e itself controls.
1c085 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 In other words
1c086 2c 20 69 74 0a 2a 2a 20 63 6f 6e 76 65 72 74 73 , it.** converts
1c087 20 61 6e 20 4d 45 4d 5f 45 70 68 65 6d 20 73 74 an MEM_Ephem st
1c088 72 69 6e 67 20 69 6e 74 6f 20 61 6e 20 4d 45 4d ring into an MEM
1c089 5f 44 79 6e 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a _Dyn string..*/.
1c08a 23 64 65 66 69 6e 65 20 44 65 65 70 68 65 6d 65 #define Deepheme
1c08b 72 61 6c 69 7a 65 28 50 29 20 5c 0a 20 20 20 69 ralize(P) \. i
1c08c 66 28 20 28 28 50 29 2d 3e 66 6c 61 67 73 26 4d f( ((P)->flags&M
1c08d 45 4d 5f 45 70 68 65 6d 29 21 3d 30 20 5c 0a 20 EM_Ephem)!=0 \.
1c08e 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 && sqlite3
1c08f 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 VdbeMemMakeWrite
1c090 61 62 6c 65 28 50 29 20 29 7b 20 67 6f 74 6f 20 able(P) ){ goto
1c091 6e 6f 5f 6d 65 6d 3b 7d 0a 0a 2f 2a 0a 2a 2a 20 no_mem;}../*.**
1c092 43 61 6c 6c 20 73 71 6c 69 74 65 33 56 64 62 65 Call sqlite3Vdbe
1c093 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 29 20 MemExpandBlob()
1c094 6f 6e 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 on the supplied
1c095 76 61 6c 75 65 20 28 74 79 70 65 20 4d 65 6d 2a value (type Mem*
1c096 29 0a 2a 2a 20 50 20 69 66 20 72 65 71 75 69 72 ).** P if requir
1c097 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 ed..*/.#define E
1c098 78 70 61 6e 64 42 6c 6f 62 28 50 29 20 28 28 28 xpandBlob(P) (((
1c099 50 29 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 5a 65 P)->flags&MEM_Ze
1c09a 72 6f 29 3f 73 71 6c 69 74 65 33 56 64 62 65 4d ro)?sqlite3VdbeM
1c09b 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 50 29 3a emExpandBlob(P):
1c09c 30 29 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 0)../*.** Argume
1c09d 6e 74 20 70 4d 65 6d 20 70 6f 69 6e 74 73 20 61 nt pMem points a
1c09e 74 20 61 20 72 65 67 69 73 74 65 72 20 74 68 61 t a register tha
1c09f 74 20 77 69 6c 6c 20 62 65 20 70 61 73 73 65 64 t will be passed
1c0a0 20 74 6f 20 61 0a 2a 2a 20 75 73 65 72 2d 64 65 to a.** user-de
1c0a1 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 6f fined function o
1c0a2 72 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 r returned to th
1c0a3 65 20 75 73 65 72 20 61 73 20 74 68 65 20 72 65 e user as the re
1c0a4 73 75 6c 74 20 6f 66 20 61 20 71 75 65 72 79 2e sult of a query.
1c0a5 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
1c0a6 20 73 65 74 73 20 74 68 65 20 70 4d 65 6d 2d 3e sets the pMem->
1c0a7 74 79 70 65 20 76 61 72 69 61 62 6c 65 20 75 73 type variable us
1c0a8 65 64 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 ed by the sqlite
1c0a9 33 5f 76 61 6c 75 65 5f 2a 28 29 20 0a 2a 2a 20 3_value_*() .**
1c0aa 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 53 51 4c routines..*/.SQL
1c0ab 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
1c0ac 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1c0ad 74 6f 72 65 54 79 70 65 28 4d 65 6d 20 2a 70 4d toreType(Mem *pM
1c0ae 65 6d 29 7b 0a 20 20 69 6e 74 20 66 6c 61 67 73 em){. int flags
1c0af 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a = pMem->flags;.
1c0b0 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45 if( flags & ME
1c0b1 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 70 4d M_Null ){. pM
1c0b2 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 em->type = SQLIT
1c0b3 45 5f 4e 55 4c 4c 3b 0a 20 20 7d 0a 20 20 65 6c E_NULL;. }. el
1c0b4 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d se if( flags & M
1c0b5 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 70 4d EM_Int ){. pM
1c0b6 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 em->type = SQLIT
1c0b7 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 7d 0a 20 E_INTEGER;. }.
1c0b8 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 else if( flags
1c0b9 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 & MEM_Real ){.
1c0ba 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 pMem->type = S
1c0bb 51 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 20 7d QLITE_FLOAT;. }
1c0bc 0a 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67 . else if( flag
1c0bd 73 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 s & MEM_Str ){.
1c0be 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 pMem->type =
1c0bf 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 7d SQLITE_TEXT;. }
1c0c0 65 6c 73 65 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e else{. pMem->
1c0c1 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 42 4c type = SQLITE_BL
1c0c2 4f 42 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a OB;. }.}../*.**
1c0c3 20 50 72 6f 70 65 72 74 69 65 73 20 6f 66 20 6f Properties of o
1c0c4 70 63 6f 64 65 73 2e 20 20 54 68 65 20 4f 50 46 pcodes. The OPF
1c0c5 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 6d LG_INITIALIZER m
1c0c6 61 63 72 6f 20 69 73 0a 2a 2a 20 63 72 65 61 74 acro is.** creat
1c0c7 65 64 20 62 79 20 6d 6b 6f 70 63 6f 64 65 68 2e ed by mkopcodeh.
1c0c8 61 77 6b 20 64 75 72 69 6e 67 20 63 6f 6d 70 69 awk during compi
1c0c9 6c 61 74 69 6f 6e 2e 20 20 44 61 74 61 20 69 73 lation. Data is
1c0ca 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f obtained.** fro
1c0cb 6d 20 74 68 65 20 63 6f 6d 6d 65 6e 74 73 20 66 m the comments f
1c0cc 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 22 63 61 ollowing the "ca
1c0cd 73 65 20 4f 50 5f 78 78 78 78 3a 22 20 73 74 61 se OP_xxxx:" sta
1c0ce 74 65 6d 65 6e 74 73 20 69 6e 0a 2a 2a 20 74 68 tements in.** th
1c0cf 69 73 20 66 69 6c 65 2e 20 20 0a 2a 2f 0a 73 74 is file. .*/.st
1c0d0 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 atic const unsig
1c0d1 6e 65 64 20 63 68 61 72 20 6f 70 63 6f 64 65 50 ned char opcodeP
1c0d2 72 6f 70 65 72 74 79 5b 5d 20 3d 20 4f 50 46 4c roperty[] = OPFL
1c0d3 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52 3b 0a 0a G_INITIALIZER;..
1c0d4 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 /*.** Return tru
1c0d5 65 20 69 66 20 61 6e 20 6f 70 63 6f 64 65 20 68 e if an opcode h
1c0d6 61 73 20 61 6e 79 20 6f 66 20 74 68 65 20 4f 50 as any of the OP
1c0d7 46 4c 47 5f 78 78 78 20 70 72 6f 70 65 72 74 69 FLG_xxx properti
1c0d8 65 73 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 es.** specified
1c0d9 62 79 20 6d 61 73 6b 2e 0a 2a 2f 0a 53 51 4c 49 by mask..*/.SQLI
1c0da 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1c0db 71 6c 69 74 65 33 56 64 62 65 4f 70 63 6f 64 65 qlite3VdbeOpcode
1c0dc 48 61 73 50 72 6f 70 65 72 74 79 28 69 6e 74 20 HasProperty(int
1c0dd 6f 70 63 6f 64 65 2c 20 69 6e 74 20 6d 61 73 6b opcode, int mask
1c0de 29 7b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 63 ){. assert( opc
1c0df 6f 64 65 3e 30 20 26 26 20 6f 70 63 6f 64 65 3c ode>0 && opcode<
1c0e0 28 69 6e 74 29 73 69 7a 65 6f 66 28 6f 70 63 6f (int)sizeof(opco
1c0e1 64 65 50 72 6f 70 65 72 74 79 29 20 29 3b 0a 20 deProperty) );.
1c0e2 20 72 65 74 75 72 6e 20 28 6f 70 63 6f 64 65 50 return (opcodeP
1c0e3 72 6f 70 65 72 74 79 5b 6f 70 63 6f 64 65 5d 26 roperty[opcode]&
1c0e4 6d 61 73 6b 29 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a mask)!=0;.}../*.
1c0e5 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 56 64 62 65 ** Allocate Vdbe
1c0e6 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 69 43 Cursor number iC
1c0e7 75 72 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f ur. Return a po
1c0e8 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 52 65 inter to it. Re
1c0e9 74 75 72 6e 20 4e 55 4c 4c 0a 2a 2a 20 69 66 20 turn NULL.** if
1c0ea 77 65 20 72 75 6e 20 6f 75 74 20 6f 66 20 6d 65 we run out of me
1c0eb 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 mory..*/.static
1c0ec 56 64 62 65 43 75 72 73 6f 72 20 2a 61 6c 6c 6f VdbeCursor *allo
1c0ed 63 61 74 65 43 75 72 73 6f 72 28 0a 20 20 56 64 cateCursor(. Vd
1c0ee 62 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 be *p,
1c0ef 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 /* The virtu
1c0f0 61 6c 20 6d 61 63 68 69 6e 65 20 2a 2f 0a 20 20 al machine */.
1c0f1 69 6e 74 20 69 43 75 72 2c 20 20 20 20 20 20 20 int iCur,
1c0f2 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f /* Index o
1c0f3 66 20 74 68 65 20 6e 65 77 20 56 64 62 65 43 75 f the new VdbeCu
1c0f4 72 73 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 rsor */. int nF
1c0f5 69 65 6c 64 2c 20 20 20 20 20 20 20 20 20 20 20 ield,
1c0f6 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 69 65 /* Number of fie
1c0f7 6c 64 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 lds in the table
1c0f8 20 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 or index */. i
1c0f9 6e 74 20 69 44 62 2c 20 20 20 20 20 20 20 20 20 nt iDb,
1c0fa 20 20 20 20 20 2f 2a 20 57 68 65 6e 20 64 61 74 /* When dat
1c0fb 61 62 61 73 65 20 74 68 65 20 63 75 72 73 6f 72 abase the cursor
1c0fc 20 62 65 6c 6f 6e 67 73 20 74 6f 2c 20 6f 72 20 belongs to, or
1c0fd 2d 31 20 2a 2f 0a 20 20 69 6e 74 20 69 73 42 74 -1 */. int isBt
1c0fe 72 65 65 43 75 72 73 6f 72 20 20 20 20 20 2f 2a reeCursor /*
1c0ff 20 54 72 75 65 20 66 6f 72 20 42 2d 54 72 65 65 True for B-Tree
1c100 2e 20 20 46 61 6c 73 65 20 66 6f 72 20 70 73 65 . False for pse
1c101 75 64 6f 2d 74 61 62 6c 65 20 6f 72 20 76 74 61 udo-table or vta
1c102 62 20 2a 2f 0a 29 7b 0a 20 20 2f 2a 20 46 69 6e b */.){. /* Fin
1c103 64 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c d the memory cel
1c104 6c 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 75 l that will be u
1c105 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 sed to store the
1c106 20 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f 72 79 0a blob of memory.
1c107 20 20 2a 2a 20 72 65 71 75 69 72 65 64 20 66 6f ** required fo
1c108 72 20 74 68 69 73 20 56 64 62 65 43 75 72 73 6f r this VdbeCurso
1c109 72 20 73 74 72 75 63 74 75 72 65 2e 20 49 74 20 r structure. It
1c10a 69 73 20 63 6f 6e 76 65 6e 69 65 6e 74 20 74 6f is convenient to
1c10b 20 75 73 65 20 61 20 0a 20 20 2a 2a 20 76 64 62 use a . ** vdb
1c10c 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 6f e memory cell to
1c10d 20 6d 61 6e 61 67 65 20 74 68 65 20 6d 65 6d 6f manage the memo
1c10e 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 ry allocation re
1c10f 71 75 69 72 65 64 20 66 6f 72 20 61 0a 20 20 2a quired for a. *
1c110 2a 20 56 64 62 65 43 75 72 73 6f 72 20 73 74 72 * VdbeCursor str
1c111 75 63 74 75 72 65 20 66 6f 72 20 74 68 65 20 66 ucture for the f
1c112 6f 6c 6c 6f 77 69 6e 67 20 72 65 61 73 6f 6e 73 ollowing reasons
1c113 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 :. **. ** *
1c114 53 6f 6d 65 74 69 6d 65 73 20 63 75 72 73 6f 72 Sometimes cursor
1c115 20 6e 75 6d 62 65 72 73 20 61 72 65 20 75 73 65 numbers are use
1c116 64 20 66 6f 72 20 61 20 63 6f 75 70 6c 65 20 6f d for a couple o
1c117 66 20 64 69 66 66 65 72 65 6e 74 0a 20 20 2a 2a f different. **
1c118 20 20 20 20 20 70 75 72 70 6f 73 65 73 20 69 6e purposes in
1c119 20 61 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2e a vdbe program.
1c11a 20 54 68 65 20 64 69 66 66 65 72 65 6e 74 20 75 The different u
1c11b 73 65 73 20 6d 69 67 68 74 20 72 65 71 75 69 72 ses might requir
1c11c 65 0a 20 20 2a 2a 20 20 20 20 20 64 69 66 66 65 e. ** diffe
1c11d 72 65 6e 74 20 73 69 7a 65 64 20 61 6c 6c 6f 63 rent sized alloc
1c11e 61 74 69 6f 6e 73 2e 20 4d 65 6d 6f 72 79 20 63 ations. Memory c
1c11f 65 6c 6c 73 20 70 72 6f 76 69 64 65 20 67 72 6f ells provide gro
1c120 77 61 62 6c 65 0a 20 20 2a 2a 20 20 20 20 20 61 wable. ** a
1c121 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20 20 2a 2a llocations.. **
1c122 0a 20 20 2a 2a 20 20 20 2a 20 57 68 65 6e 20 75 . ** * When u
1c123 73 69 6e 67 20 45 4e 41 42 4c 45 5f 4d 45 4d 4f sing ENABLE_MEMO
1c124 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 2c 20 6d RY_MANAGEMENT, m
1c125 65 6d 6f 72 79 20 63 65 6c 6c 20 62 75 66 66 65 emory cell buffe
1c126 72 73 20 63 61 6e 0a 20 20 2a 2a 20 20 20 20 20 rs can. **
1c127 62 65 20 66 72 65 65 64 20 6c 61 7a 69 6c 79 20 be freed lazily
1c128 76 69 61 20 74 68 65 20 73 71 6c 69 74 65 33 5f via the sqlite3_
1c129 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 release_memory()
1c12a 20 41 50 49 2e 20 54 68 69 73 0a 20 20 2a 2a 20 API. This. **
1c12b 20 20 20 20 6d 69 6e 69 6d 69 7a 65 73 20 74 68 minimizes th
1c12c 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 61 6c 6c e number of mall
1c12d 6f 63 20 63 61 6c 6c 73 20 6d 61 64 65 20 62 79 oc calls made by
1c12e 20 74 68 65 20 73 79 73 74 65 6d 2e 0a 20 20 2a the system.. *
1c12f 2a 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79 20 63 65 *. ** Memory ce
1c130 6c 6c 73 20 66 6f 72 20 63 75 72 73 6f 72 73 20 lls for cursors
1c131 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 61 74 are allocated at
1c132 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 the top of the
1c133 61 64 64 72 65 73 73 0a 20 20 2a 2a 20 73 70 61 address. ** spa
1c134 63 65 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 ce. Memory cell
1c135 28 70 2d 3e 6e 4d 65 6d 29 20 63 6f 72 72 65 73 (p->nMem) corres
1c136 70 6f 6e 64 73 20 74 6f 20 63 75 72 73 6f 72 20 ponds to cursor
1c137 30 2e 20 53 70 61 63 65 20 66 6f 72 0a 20 20 2a 0. Space for. *
1c138 2a 20 63 75 72 73 6f 72 20 31 20 69 73 20 6d 61 * cursor 1 is ma
1c139 6e 61 67 65 64 20 62 79 20 6d 65 6d 6f 72 79 20 naged by memory
1c13a 63 65 6c 6c 20 28 70 2d 3e 6e 4d 65 6d 2d 31 29 cell (p->nMem-1)
1c13b 2c 20 65 74 63 2e 0a 20 20 2a 2f 0a 20 20 4d 65 , etc.. */. Me
1c13c 6d 20 2a 70 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d m *pMem = &p->aM
1c13d 65 6d 5b 70 2d 3e 6e 4d 65 6d 2d 69 43 75 72 5d em[p->nMem-iCur]
1c13e 3b 0a 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a ;.. int nByte;.
1c13f 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1c140 78 20 3d 20 30 3b 0a 20 20 6e 42 79 74 65 20 3d x = 0;. nByte =
1c141 20 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 56 . sizeof(V
1c142 64 62 65 43 75 72 73 6f 72 29 20 2b 20 0a 20 20 dbeCursor) + .
1c143 20 20 20 20 28 69 73 42 74 72 65 65 43 75 72 73 (isBtreeCurs
1c144 6f 72 3f 73 71 6c 69 74 65 33 42 74 72 65 65 43 or?sqlite3BtreeC
1c145 75 72 73 6f 72 53 69 7a 65 28 29 3a 30 29 20 2b ursorSize():0) +
1c146 20 0a 20 20 20 20 20 20 32 2a 6e 46 69 65 6c 64 . 2*nField
1c147 2a 73 69 7a 65 6f 66 28 75 33 32 29 3b 0a 0a 20 *sizeof(u32);..
1c148 20 61 73 73 65 72 74 28 20 69 43 75 72 3c 70 2d assert( iCur<p-
1c149 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 69 66 >nCursor );. if
1c14a 28 20 70 2d 3e 61 70 43 73 72 5b 69 43 75 72 5d ( p->apCsr[iCur]
1c14b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
1c14c 64 62 65 46 72 65 65 43 75 72 73 6f 72 28 70 2c dbeFreeCursor(p,
1c14d 20 70 2d 3e 61 70 43 73 72 5b 69 43 75 72 5d 29 p->apCsr[iCur])
1c14e 3b 0a 20 20 20 20 70 2d 3e 61 70 43 73 72 5b 69 ;. p->apCsr[i
1c14f 43 75 72 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 Cur] = 0;. }.
1c150 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73 if( SQLITE_OK==s
1c151 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f qlite3VdbeMemGro
1c152 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 30 w(pMem, nByte, 0
1c153 29 20 29 7b 0a 20 20 20 20 70 2d 3e 61 70 43 73 ) ){. p->apCs
1c154 72 5b 69 43 75 72 5d 20 3d 20 70 43 78 20 3d 20 r[iCur] = pCx =
1c155 28 56 64 62 65 43 75 72 73 6f 72 2a 29 70 4d 65 (VdbeCursor*)pMe
1c156 6d 2d 3e 7a 3b 0a 20 20 20 20 6d 65 6d 73 65 74 m->z;. memset
1c157 28 70 4d 65 6d 2d 3e 7a 2c 20 30 2c 20 6e 42 79 (pMem->z, 0, nBy
1c158 74 65 29 3b 0a 20 20 20 20 70 43 78 2d 3e 69 44 te);. pCx->iD
1c159 62 20 3d 20 69 44 62 3b 0a 20 20 20 20 70 43 78 b = iDb;. pCx
1c15a 2d 3e 6e 46 69 65 6c 64 20 3d 20 6e 46 69 65 6c ->nField = nFiel
1c15b 64 3b 0a 20 20 20 20 69 66 28 20 6e 46 69 65 6c d;. if( nFiel
1c15c 64 20 29 7b 0a 20 20 20 20 20 20 70 43 78 2d 3e d ){. pCx->
1c15d 61 54 79 70 65 20 3d 20 28 75 33 32 20 2a 29 26 aType = (u32 *)&
1c15e 70 4d 65 6d 2d 3e 7a 5b 73 69 7a 65 6f 66 28 56 pMem->z[sizeof(V
1c15f 64 62 65 43 75 72 73 6f 72 29 5d 3b 0a 20 20 20 dbeCursor)];.
1c160 20 7d 0a 20 20 20 20 69 66 28 20 69 73 42 74 72 }. if( isBtr
1c161 65 65 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 eeCursor ){.
1c162 20 20 70 43 78 2d 3e 70 43 75 72 73 6f 72 20 3d pCx->pCursor =
1c163 20 28 42 74 43 75 72 73 6f 72 2a 29 0a 20 20 20 (BtCursor*).
1c164 20 20 20 20 20 20 20 26 70 4d 65 6d 2d 3e 7a 5b &pMem->z[
1c165 73 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f sizeof(VdbeCurso
1c166 72 29 2b 32 2a 6e 46 69 65 6c 64 2a 73 69 7a 65 r)+2*nField*size
1c167 6f 66 28 75 33 32 29 5d 3b 0a 20 20 20 20 7d 0a of(u32)];. }.
1c168 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 43 78 }. return pCx
1c169 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 ;.}../*.** Try t
1c16a 6f 20 63 6f 6e 76 65 72 74 20 61 20 76 61 6c 75 o convert a valu
1c16b 65 20 69 6e 74 6f 20 61 20 6e 75 6d 65 72 69 63 e into a numeric
1c16c 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 representation
1c16d 69 66 20 77 65 20 63 61 6e 0a 2a 2a 20 64 6f 20 if we can.** do
1c16e 73 6f 20 77 69 74 68 6f 75 74 20 6c 6f 73 73 20 so without loss
1c16f 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 of information.
1c170 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c In other words,
1c171 20 69 66 20 74 68 65 20 73 74 72 69 6e 67 0a 2a if the string.*
1c172 2a 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 6e * looks like a n
1c173 75 6d 62 65 72 2c 20 63 6f 6e 76 65 72 74 20 69 umber, convert i
1c174 74 20 69 6e 74 6f 20 61 20 6e 75 6d 62 65 72 2e t into a number.
1c175 20 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 If it does not
1c176 0a 2a 2a 20 6c 6f 6f 6b 20 6c 69 6b 65 20 61 20 .** look like a
1c177 6e 75 6d 62 65 72 2c 20 6c 65 61 76 65 20 69 74 number, leave it
1c178 20 61 6c 6f 6e 65 2e 0a 2a 2f 0a 73 74 61 74 69 alone..*/.stati
1c179 63 20 76 6f 69 64 20 61 70 70 6c 79 4e 75 6d 65 c void applyNume
1c17a 72 69 63 41 66 66 69 6e 69 74 79 28 4d 65 6d 20 ricAffinity(Mem
1c17b 2a 70 52 65 63 29 7b 0a 20 20 69 66 28 20 28 70 *pRec){. if( (p
1c17c 52 65 63 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 Rec->flags & (ME
1c17d 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29 29 M_Real|MEM_Int))
1c17e 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 ==0 ){. int r
1c17f 65 61 6c 6e 75 6d 3b 0a 20 20 20 20 73 71 6c 69 ealnum;. sqli
1c180 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 te3VdbeMemNulTer
1c181 6d 69 6e 61 74 65 28 70 52 65 63 29 3b 0a 20 20 minate(pRec);.
1c182 20 20 69 66 28 20 28 70 52 65 63 2d 3e 66 6c 61 if( (pRec->fla
1c183 67 73 26 4d 45 4d 5f 53 74 72 29 0a 20 20 20 20 gs&MEM_Str).
1c184 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 49 && sqlite3I
1c185 73 4e 75 6d 62 65 72 28 70 52 65 63 2d 3e 7a 2c sNumber(pRec->z,
1c186 20 26 72 65 61 6c 6e 75 6d 2c 20 70 52 65 63 2d &realnum, pRec-
1c187 3e 65 6e 63 29 20 29 7b 0a 20 20 20 20 20 20 69 >enc) ){. i
1c188 36 34 20 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 64 value;.
1c189 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
1c18a 65 45 6e 63 6f 64 69 6e 67 28 70 52 65 63 2c 20 eEncoding(pRec,
1c18b 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 SQLITE_UTF8);.
1c18c 20 20 20 20 69 66 28 20 21 72 65 61 6c 6e 75 6d if( !realnum
1c18d 20 26 26 20 73 71 6c 69 74 65 33 41 74 6f 69 36 && sqlite3Atoi6
1c18e 34 28 70 52 65 63 2d 3e 7a 2c 20 26 76 61 6c 75 4(pRec->z, &valu
1c18f 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 52 e) ){. pR
1c190 65 63 2d 3e 75 2e 69 20 3d 20 76 61 6c 75 65 3b ec->u.i = value;
1c191 0a 20 20 20 20 20 20 20 20 4d 65 6d 53 65 74 54 . MemSetT
1c192 79 70 65 46 6c 61 67 28 70 52 65 63 2c 20 4d 45 ypeFlag(pRec, ME
1c193 4d 5f 49 6e 74 29 3b 0a 20 20 20 20 20 20 7d 65 M_Int);. }e
1c194 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c lse{. sql
1c195 69 74 65 33 56 64 62 65 4d 65 6d 52 65 61 6c 69 ite3VdbeMemReali
1c196 66 79 28 70 52 65 63 29 3b 0a 20 20 20 20 20 20 fy(pRec);.
1c197 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f }. }. }.}../
1c198 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 69 6e 67 20 *.** Processing
1c199 69 73 20 64 65 74 65 72 6d 69 6e 65 20 62 79 20 is determine by
1c19a 74 68 65 20 61 66 66 69 6e 69 74 79 20 70 61 72 the affinity par
1c19b 61 6d 65 74 65 72 3a 0a 2a 2a 0a 2a 2a 20 53 51 ameter:.**.** SQ
1c19c 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 LITE_AFF_INTEGER
1c19d 3a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f :.** SQLITE_AFF_
1c19e 52 45 41 4c 3a 0a 2a 2a 20 53 51 4c 49 54 45 5f REAL:.** SQLITE_
1c19f 41 46 46 5f 4e 55 4d 45 52 49 43 3a 0a 2a 2a 20 AFF_NUMERIC:.**
1c1a0 20 20 20 54 72 79 20 74 6f 20 63 6f 6e 76 65 72 Try to conver
1c1a1 74 20 70 52 65 63 20 74 6f 20 61 6e 20 69 6e 74 t pRec to an int
1c1a2 65 67 65 72 20 72 65 70 72 65 73 65 6e 74 61 74 eger representat
1c1a3 69 6f 6e 20 6f 72 20 61 20 0a 2a 2a 20 20 20 20 ion or a .**
1c1a4 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20 72 floating-point r
1c1a5 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 66 epresentation if
1c1a6 20 61 6e 20 69 6e 74 65 67 65 72 20 72 65 70 72 an integer repr
1c1a7 65 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 20 20 esentation.**
1c1a8 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 is not possible
1c1a9 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 . Note that the
1c1aa 20 69 6e 74 65 67 65 72 20 72 65 70 72 65 73 65 integer represe
1c1ab 6e 74 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 20 20 ntation is.**
1c1ac 20 61 6c 77 61 79 73 20 70 72 65 66 65 72 72 65 always preferre
1c1ad 64 2c 20 65 76 65 6e 20 69 66 20 74 68 65 20 61 d, even if the a
1c1ae 66 66 69 6e 69 74 79 20 69 73 20 52 45 41 4c 2c ffinity is REAL,
1c1af 20 62 65 63 61 75 73 65 0a 2a 2a 20 20 20 20 61 because.** a
1c1b0 6e 20 69 6e 74 65 67 65 72 20 72 65 70 72 65 73 n integer repres
1c1b1 65 6e 74 61 74 69 6f 6e 20 69 73 20 6d 6f 72 65 entation is more
1c1b2 20 73 70 61 63 65 20 65 66 66 69 63 69 65 6e 74 space efficient
1c1b3 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 on disk..**.**
1c1b4 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3a SQLITE_AFF_TEXT:
1c1b5 0a 2a 2a 20 20 20 20 43 6f 6e 76 65 72 74 20 70 .** Convert p
1c1b6 52 65 63 20 74 6f 20 61 20 74 65 78 74 20 72 65 Rec to a text re
1c1b7 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a presentation..**
1c1b8 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e .** SQLITE_AFF_N
1c1b9 4f 4e 45 3a 0a 2a 2a 20 20 20 20 4e 6f 2d 6f 70 ONE:.** No-op
1c1ba 2e 20 20 70 52 65 63 20 69 73 20 75 6e 63 68 61 . pRec is uncha
1c1bb 6e 67 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 nged..*/.static
1c1bc 76 6f 69 64 20 61 70 70 6c 79 41 66 66 69 6e 69 void applyAffini
1c1bd 74 79 28 0a 20 20 4d 65 6d 20 2a 70 52 65 63 2c ty(. Mem *pRec,
1c1be 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
1c1bf 20 76 61 6c 75 65 20 74 6f 20 61 70 70 6c 79 20 value to apply
1c1c0 61 66 66 69 6e 69 74 79 20 74 6f 20 2a 2f 0a 20 affinity to */.
1c1c1 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 2c 20 char affinity,
1c1c2 20 20 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 /* The affi
1c1c3 6e 69 74 79 20 74 6f 20 62 65 20 61 70 70 6c 69 nity to be appli
1c1c4 65 64 20 2a 2f 0a 20 20 75 38 20 65 6e 63 20 20 ed */. u8 enc
1c1c5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 /* U
1c1c6 73 65 20 74 68 69 73 20 74 65 78 74 20 65 6e 63 se this text enc
1c1c7 6f 64 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 66 oding */.){. if
1c1c8 28 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 ( affinity==SQLI
1c1c9 54 45 5f 41 46 46 5f 54 45 58 54 20 29 7b 0a 20 TE_AFF_TEXT ){.
1c1ca 20 20 20 2f 2a 20 4f 6e 6c 79 20 61 74 74 65 6d /* Only attem
1c1cb 70 74 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f pt the conversio
1c1cc 6e 20 74 6f 20 54 45 58 54 20 69 66 20 74 68 65 n to TEXT if the
1c1cd 72 65 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 re is an integer
1c1ce 20 6f 72 20 72 65 61 6c 0a 20 20 20 20 2a 2a 20 or real. **
1c1cf 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 28 representation (
1c1d0 62 6c 6f 62 20 61 6e 64 20 4e 55 4c 4c 20 64 6f blob and NULL do
1c1d1 20 6e 6f 74 20 67 65 74 20 63 6f 6e 76 65 72 74 not get convert
1c1d2 65 64 29 20 62 75 74 20 6e 6f 20 73 74 72 69 6e ed) but no strin
1c1d3 67 0a 20 20 20 20 2a 2a 20 72 65 70 72 65 73 65 g. ** represe
1c1d4 6e 74 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a ntation.. */.
1c1d5 20 20 20 20 69 66 28 20 30 3d 3d 28 70 52 65 63 if( 0==(pRec
1c1d6 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 29 ->flags&MEM_Str)
1c1d7 20 26 26 20 28 70 52 65 63 2d 3e 66 6c 61 67 73 && (pRec->flags
1c1d8 26 28 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 49 &(MEM_Real|MEM_I
1c1d9 6e 74 29 29 20 29 7b 0a 20 20 20 20 20 20 73 71 nt)) ){. sq
1c1da 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69 lite3VdbeMemStri
1c1db 6e 67 69 66 79 28 70 52 65 63 2c 20 65 6e 63 29 ngify(pRec, enc)
1c1dc 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 52 65 63 ;. }. pRec
1c1dd 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d ->flags &= ~(MEM
1c1de 5f 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29 3b 0a _Real|MEM_Int);.
1c1df 20 20 7d 65 6c 73 65 20 69 66 28 20 61 66 66 69 }else if( affi
1c1e0 6e 69 74 79 21 3d 53 51 4c 49 54 45 5f 41 46 46 nity!=SQLITE_AFF
1c1e1 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 61 73 73 _NONE ){. ass
1c1e2 65 72 74 28 20 61 66 66 69 6e 69 74 79 3d 3d 53 ert( affinity==S
1c1e3 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 QLITE_AFF_INTEGE
1c1e4 52 20 7c 7c 20 61 66 66 69 6e 69 74 79 3d 3d 53 R || affinity==S
1c1e5 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a 20 QLITE_AFF_REAL.
1c1e6 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 61 || a
1c1e7 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f ffinity==SQLITE_
1c1e8 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 3b 0a 20 AFF_NUMERIC );.
1c1e9 20 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 41 applyNumericA
1c1ea 66 66 69 6e 69 74 79 28 70 52 65 63 29 3b 0a 20 ffinity(pRec);.
1c1eb 20 20 20 69 66 28 20 70 52 65 63 2d 3e 66 6c 61 if( pRec->fla
1c1ec 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b gs & MEM_Real ){
1c1ed 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1c1ee 62 65 49 6e 74 65 67 65 72 41 66 66 69 6e 69 74 beIntegerAffinit
1c1ef 79 28 70 52 65 63 29 3b 0a 20 20 20 20 7d 0a 20 y(pRec);. }.
1c1f0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 }.}../*.** Try
1c1f1 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 65 20 74 to convert the t
1c1f2 79 70 65 20 6f 66 20 61 20 66 75 6e 63 74 69 6f ype of a functio
1c1f3 6e 20 61 72 67 75 6d 65 6e 74 20 6f 72 20 61 20 n argument or a
1c1f4 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 0a 2a 2a result column.**
1c1f5 20 69 6e 74 6f 20 61 20 6e 75 6d 65 72 69 63 20 into a numeric
1c1f6 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 20 representation.
1c1f7 20 55 73 65 20 65 69 74 68 65 72 20 49 4e 54 45 Use either INTE
1c1f8 47 45 52 20 6f 72 20 52 45 41 4c 20 77 68 69 63 GER or REAL whic
1c1f9 68 65 76 65 72 0a 2a 2a 20 69 73 20 61 70 70 72 hever.** is appr
1c1fa 6f 70 72 69 61 74 65 2e 20 20 42 75 74 20 6f 6e opriate. But on
1c1fb 6c 79 20 64 6f 20 74 68 65 20 63 6f 6e 76 65 72 ly do the conver
1c1fc 73 69 6f 6e 20 69 66 20 69 74 20 69 73 20 70 6f sion if it is po
1c1fd 73 73 69 62 6c 65 20 77 69 74 68 6f 75 74 0a 2a ssible without.*
1c1fe 2a 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72 6d * loss of inform
1c1ff 61 74 69 6f 6e 20 61 6e 64 20 72 65 74 75 72 6e ation and return
1c200 20 74 68 65 20 72 65 76 69 73 65 64 20 74 79 70 the revised typ
1c201 65 20 6f 66 20 74 68 65 20 61 72 67 75 6d 65 6e e of the argumen
1c202 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 t..**.** This is
1c203 20 61 6e 20 45 58 50 45 52 49 4d 45 4e 54 41 4c an EXPERIMENTAL
1c204 20 61 70 69 20 61 6e 64 20 69 73 20 73 75 62 6a api and is subj
1c205 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 6f 72 ect to change or
1c206 20 72 65 6d 6f 76 61 6c 2e 0a 2a 2f 0a 53 51 4c removal..*/.SQL
1c207 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1c208 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 te3_value_numeri
1c209 63 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f 76 c_type(sqlite3_v
1c20a 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 4d alue *pVal){. M
1c20b 65 6d 20 2a 70 4d 65 6d 20 3d 20 28 4d 65 6d 2a em *pMem = (Mem*
1c20c 29 70 56 61 6c 3b 0a 20 20 61 70 70 6c 79 4e 75 )pVal;. applyNu
1c20d 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 70 4d mericAffinity(pM
1c20e 65 6d 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 em);. sqlite3Vd
1c20f 62 65 4d 65 6d 53 74 6f 72 65 54 79 70 65 28 70 beMemStoreType(p
1c210 4d 65 6d 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 Mem);. return p
1c211 4d 65 6d 2d 3e 74 79 70 65 3b 0a 7d 0a 0a 2f 2a Mem->type;.}../*
1c212 0a 2a 2a 20 45 78 70 6f 72 74 65 64 20 76 65 72 .** Exported ver
1c213 73 69 6f 6e 20 6f 66 20 61 70 70 6c 79 41 66 66 sion of applyAff
1c214 69 6e 69 74 79 28 29 2e 20 54 68 69 73 20 6f 6e inity(). This on
1c215 65 20 77 6f 72 6b 73 20 6f 6e 20 73 71 6c 69 74 e works on sqlit
1c216 65 33 5f 76 61 6c 75 65 2a 2c 20 0a 2a 2a 20 6e e3_value*, .** n
1c217 6f 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 ot the internal
1c218 4d 65 6d 2a 20 74 79 70 65 2e 0a 2a 2f 0a 53 51 Mem* type..*/.SQ
1c219 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1c21a 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65 41 70 d sqlite3ValueAp
1c21b 70 6c 79 41 66 66 69 6e 69 74 79 28 0a 20 20 73 plyAffinity(. s
1c21c 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 qlite3_value *pV
1c21d 61 6c 2c 20 0a 20 20 75 38 20 61 66 66 69 6e 69 al, . u8 affini
1c21e 74 79 2c 20 0a 20 20 75 38 20 65 6e 63 0a 29 7b ty, . u8 enc.){
1c21f 0a 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 . applyAffinity
1c220 28 28 4d 65 6d 20 2a 29 70 56 61 6c 2c 20 61 66 ((Mem *)pVal, af
1c221 66 69 6e 69 74 79 2c 20 65 6e 63 29 3b 0a 7d 0a finity, enc);.}.
1c222 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
1c223 45 42 55 47 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 EBUG./*.** Write
1c224 20 61 20 6e 69 63 65 20 73 74 72 69 6e 67 20 72 a nice string r
1c225 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 epresentation of
1c226 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 the contents of
1c227 20 63 65 6c 6c 20 70 4d 65 6d 0a 2a 2a 20 69 6e cell pMem.** in
1c228 74 6f 20 62 75 66 66 65 72 20 7a 42 75 66 2c 20 to buffer zBuf,
1c229 6c 65 6e 67 74 68 20 6e 42 75 66 2e 0a 2a 2f 0a length nBuf..*/.
1c22a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1c22b 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d oid sqlite3VdbeM
1c22c 65 6d 50 72 65 74 74 79 50 72 69 6e 74 28 4d 65 emPrettyPrint(Me
1c22d 6d 20 2a 70 4d 65 6d 2c 20 63 68 61 72 20 2a 7a m *pMem, char *z
1c22e 42 75 66 29 7b 0a 20 20 63 68 61 72 20 2a 7a 43 Buf){. char *zC
1c22f 73 72 20 3d 20 7a 42 75 66 3b 0a 20 20 69 6e 74 sr = zBuf;. int
1c230 20 66 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 f = pMem->flags
1c231 3b 0a 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 ;.. static cons
1c232 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 20 65 6e t char *const en
1c233 63 6e 61 6d 65 73 5b 5d 20 3d 20 7b 22 28 58 29 cnames[] = {"(X)
1c234 22 2c 20 22 28 38 29 22 2c 20 22 28 31 36 4c 45 ", "(8)", "(16LE
1c235 29 22 2c 20 22 28 31 36 42 45 29 22 7d 3b 0a 0a )", "(16BE)"};..
1c236 20 20 69 66 28 20 66 26 4d 45 4d 5f 42 6c 6f 62 if( f&MEM_Blob
1c237 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 ){. int i;.
1c238 20 20 20 63 68 61 72 20 63 3b 0a 20 20 20 20 69 char c;. i
1c239 66 28 20 66 20 26 20 4d 45 4d 5f 44 79 6e 20 29 f( f & MEM_Dyn )
1c23a 7b 0a 20 20 20 20 20 20 63 20 3d 20 27 7a 27 3b {. c = 'z';
1c23b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 . assert( (
1c23c 66 20 26 20 28 4d 45 4d 5f 53 74 61 74 69 63 7c f & (MEM_Static|
1c23d 4d 45 4d 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29 MEM_Ephem))==0 )
1c23e 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
1c23f 66 20 26 20 4d 45 4d 5f 53 74 61 74 69 63 20 29 f & MEM_Static )
1c240 7b 0a 20 20 20 20 20 20 63 20 3d 20 27 74 27 3b {. c = 't';
1c241 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 . assert( (
1c242 66 20 26 20 28 4d 45 4d 5f 44 79 6e 7c 4d 45 4d f & (MEM_Dyn|MEM
1c243 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 _Ephem))==0 );.
1c244 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26 }else if( f &
1c245 20 4d 45 4d 5f 45 70 68 65 6d 20 29 7b 0a 20 20 MEM_Ephem ){.
1c246 20 20 20 20 63 20 3d 20 27 65 27 3b 0a 20 20 20 c = 'e';.
1c247 20 20 20 61 73 73 65 72 74 28 20 28 66 20 26 20 assert( (f &
1c248 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f (MEM_Static|MEM_
1c249 44 79 6e 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 Dyn))==0 );.
1c24a 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 20 3d }else{. c =
1c24b 20 27 73 27 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 's';. }..
1c24c 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
1c24d 66 28 31 30 30 2c 20 7a 43 73 72 2c 20 22 25 63 f(100, zCsr, "%c
1c24e 22 2c 20 63 29 3b 0a 20 20 20 20 7a 43 73 72 20 ", c);. zCsr
1c24f 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e += sqlite3Strlen
1c250 33 30 28 7a 43 73 72 29 3b 0a 20 20 20 20 73 71 30(zCsr);. sq
1c251 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 lite3_snprintf(1
1c252 30 30 2c 20 7a 43 73 72 2c 20 22 25 64 5b 22 2c 00, zCsr, "%d[",
1c253 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 20 20 7a pMem->n);. z
1c254 43 73 72 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 Csr += sqlite3St
1c255 72 6c 65 6e 33 30 28 7a 43 73 72 29 3b 0a 20 20 rlen30(zCsr);.
1c256 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 31 36 20 for(i=0; i<16
1c257 26 26 20 69 3c 70 4d 65 6d 2d 3e 6e 3b 20 69 2b && i<pMem->n; i+
1c258 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 +){. sqlite
1c259 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 3_snprintf(100,
1c25a 7a 43 73 72 2c 20 22 25 30 32 58 22 2c 20 28 28 zCsr, "%02X", ((
1c25b 69 6e 74 29 70 4d 65 6d 2d 3e 7a 5b 69 5d 20 26 int)pMem->z[i] &
1c25c 20 30 78 46 46 29 29 3b 0a 20 20 20 20 20 20 7a 0xFF));. z
1c25d 43 73 72 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 Csr += sqlite3St
1c25e 72 6c 65 6e 33 30 28 7a 43 73 72 29 3b 0a 20 20 rlen30(zCsr);.
1c25f 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b }. for(i=0;
1c260 20 69 3c 31 36 20 26 26 20 69 3c 70 4d 65 6d 2d i<16 && i<pMem-
1c261 3e 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 >n; i++){.
1c262 63 68 61 72 20 7a 20 3d 20 70 4d 65 6d 2d 3e 7a char z = pMem->z
1c263 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 7a [i];. if( z
1c264 3c 33 32 20 7c 7c 20 7a 3e 31 32 36 20 29 20 2a <32 || z>126 ) *
1c265 7a 43 73 72 2b 2b 20 3d 20 27 2e 27 3b 0a 20 20 zCsr++ = '.';.
1c266 20 20 20 20 65 6c 73 65 20 2a 7a 43 73 72 2b 2b else *zCsr++
1c267 20 3d 20 7a 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 = z;. }..
1c268 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
1c269 66 28 31 30 30 2c 20 7a 43 73 72 2c 20 22 5d 25 f(100, zCsr, "]%
1c26a 73 22 2c 20 65 6e 63 6e 61 6d 65 73 5b 70 4d 65 s", encnames[pMe
1c26b 6d 2d 3e 65 6e 63 5d 29 3b 0a 20 20 20 20 7a 43 m->enc]);. zC
1c26c 73 72 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 sr += sqlite3Str
1c26d 6c 65 6e 33 30 28 7a 43 73 72 29 3b 0a 20 20 20 len30(zCsr);.
1c26e 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 5a 65 72 if( f & MEM_Zer
1c26f 6f 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 o ){. sqlit
1c270 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c e3_snprintf(100,
1c271 20 7a 43 73 72 2c 22 2b 25 64 7a 22 2c 70 4d 65 zCsr,"+%dz",pMe
1c272 6d 2d 3e 75 2e 6e 5a 65 72 6f 29 3b 0a 20 20 20 m->u.nZero);.
1c273 20 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c 69 74 zCsr += sqlit
1c274 65 33 53 74 72 6c 65 6e 33 30 28 7a 43 73 72 29 e3Strlen30(zCsr)
1c275 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 7a 43 73 ;. }. *zCs
1c276 72 20 3d 20 27 5c 30 27 3b 0a 20 20 7d 65 6c 73 r = '\0';. }els
1c277 65 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 53 74 e if( f & MEM_St
1c278 72 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 2c 20 r ){. int j,
1c279 6b 3b 0a 20 20 20 20 7a 42 75 66 5b 30 5d 20 3d k;. zBuf[0] =
1c27a 20 27 20 27 3b 0a 20 20 20 20 69 66 28 20 66 20 ' ';. if( f
1c27b 26 20 4d 45 4d 5f 44 79 6e 20 29 7b 0a 20 20 20 & MEM_Dyn ){.
1c27c 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 7a 27 zBuf[1] = 'z'
1c27d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1c27e 28 66 20 26 20 28 4d 45 4d 5f 53 74 61 74 69 63 (f & (MEM_Static
1c27f 7c 4d 45 4d 5f 45 70 68 65 6d 29 29 3d 3d 30 20 |MEM_Ephem))==0
1c280 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 );. }else if(
1c281 20 66 20 26 20 4d 45 4d 5f 53 74 61 74 69 63 20 f & MEM_Static
1c282 29 7b 0a 20 20 20 20 20 20 7a 42 75 66 5b 31 5d ){. zBuf[1]
1c283 20 3d 20 27 74 27 3b 0a 20 20 20 20 20 20 61 73 = 't';. as
1c284 73 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f sert( (f & (MEM_
1c285 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 29 3d Dyn|MEM_Ephem))=
1c286 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 =0 );. }else
1c287 69 66 28 20 66 20 26 20 4d 45 4d 5f 45 70 68 65 if( f & MEM_Ephe
1c288 6d 20 29 7b 0a 20 20 20 20 20 20 7a 42 75 66 5b m ){. zBuf[
1c289 31 5d 20 3d 20 27 65 27 3b 0a 20 20 20 20 20 20 1] = 'e';.
1c28a 61 73 73 65 72 74 28 20 28 66 20 26 20 28 4d 45 assert( (f & (ME
1c28b 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 44 79 6e M_Static|MEM_Dyn
1c28c 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c ))==0 );. }el
1c28d 73 65 7b 0a 20 20 20 20 20 20 7a 42 75 66 5b 31 se{. zBuf[1
1c28e 5d 20 3d 20 27 73 27 3b 0a 20 20 20 20 7d 0a 20 ] = 's';. }.
1c28f 20 20 20 6b 20 3d 20 32 3b 0a 20 20 20 20 73 71 k = 2;. sq
1c290 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 lite3_snprintf(1
1c291 30 30 2c 20 26 7a 42 75 66 5b 6b 5d 2c 20 22 25 00, &zBuf[k], "%
1c292 64 22 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 d", pMem->n);.
1c293 20 20 6b 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 k += sqlite3St
1c294 72 6c 65 6e 33 30 28 26 7a 42 75 66 5b 6b 5d 29 rlen30(&zBuf[k])
1c295 3b 0a 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 ;. zBuf[k++]
1c296 3d 20 27 5b 27 3b 0a 20 20 20 20 66 6f 72 28 6a = '[';. for(j
1c297 3d 30 3b 20 6a 3c 31 35 20 26 26 20 6a 3c 70 4d =0; j<15 && j<pM
1c298 65 6d 2d 3e 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 em->n; j++){.
1c299 20 20 20 75 38 20 63 20 3d 20 70 4d 65 6d 2d 3e u8 c = pMem->
1c29a 7a 5b 6a 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 z[j];. if(
1c29b 63 3e 3d 30 78 32 30 20 26 26 20 63 3c 30 78 37 c>=0x20 && c<0x7
1c29c 66 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 42 75 f ){. zBu
1c29d 66 5b 6b 2b 2b 5d 20 3d 20 63 3b 0a 20 20 20 20 f[k++] = c;.
1c29e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1c29f 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 27 2e 27 zBuf[k++] = '.'
1c2a0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1c2a1 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 zBuf[k++] =
1c2a2 27 5d 27 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 ']';. sqlite3
1c2a3 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 26 7a _snprintf(100,&z
1c2a4 42 75 66 5b 6b 5d 2c 20 65 6e 63 6e 61 6d 65 73 Buf[k], encnames
1c2a5 5b 70 4d 65 6d 2d 3e 65 6e 63 5d 29 3b 0a 20 20 [pMem->enc]);.
1c2a6 20 20 6b 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 k += sqlite3St
1c2a7 72 6c 65 6e 33 30 28 26 7a 42 75 66 5b 6b 5d 29 rlen30(&zBuf[k])
1c2a8 3b 0a 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 ;. zBuf[k++]
1c2a9 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 = 0;. }.}.#endi
1c2aa 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 f..#ifdef SQLITE
1c2ab 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 50 72 69 _DEBUG./*.** Pri
1c2ac 6e 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 nt the value of
1c2ad 61 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 74 a register for t
1c2ae 72 61 63 69 6e 67 20 70 75 72 70 6f 73 65 73 3a racing purposes:
1c2af 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
1c2b0 6d 65 6d 54 72 61 63 65 50 72 69 6e 74 28 46 49 memTracePrint(FI
1c2b1 4c 45 20 2a 6f 75 74 2c 20 4d 65 6d 20 2a 70 29 LE *out, Mem *p)
1c2b2 7b 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 {. if( p->flags
1c2b3 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 & MEM_Null ){.
1c2b4 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 fprintf(out,
1c2b5 22 20 4e 55 4c 4c 22 29 3b 0a 20 20 7d 65 6c 73 " NULL");. }els
1c2b6 65 20 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 e if( (p->flags
1c2b7 26 20 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 53 & (MEM_Int|MEM_S
1c2b8 74 72 29 29 3d 3d 28 4d 45 4d 5f 49 6e 74 7c 4d tr))==(MEM_Int|M
1c2b9 45 4d 5f 53 74 72 29 20 29 7b 0a 20 20 20 20 66 EM_Str) ){. f
1c2ba 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 73 69 printf(out, " si
1c2bb 3a 25 6c 6c 64 22 2c 20 70 2d 3e 75 2e 69 29 3b :%lld", p->u.i);
1c2bc 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e . }else if( p->
1c2bd 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 flags & MEM_Int
1c2be 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f ){. fprintf(o
1c2bf 75 74 2c 20 22 20 69 3a 25 6c 6c 64 22 2c 20 70 ut, " i:%lld", p
1c2c0 2d 3e 75 2e 69 29 3b 0a 23 69 66 6e 64 65 66 20 ->u.i);.#ifndef
1c2c1 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 SQLITE_OMIT_FLOA
1c2c2 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 7d 65 6c TING_POINT. }el
1c2c3 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 se if( p->flags
1c2c4 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 & MEM_Real ){.
1c2c5 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
1c2c6 20 72 3a 25 67 22 2c 20 70 2d 3e 72 29 3b 0a 23 r:%g", p->r);.#
1c2c7 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 20 69 66 endif. }else if
1c2c8 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d ( p->flags & MEM
1c2c9 5f 52 6f 77 53 65 74 20 29 7b 0a 20 20 20 20 66 _RowSet ){. f
1c2ca 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 28 72 printf(out, " (r
1c2cb 6f 77 73 65 74 29 22 29 3b 0a 20 20 7d 65 6c 73 owset)");. }els
1c2cc 65 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66 e{. char zBuf
1c2cd 5b 32 30 30 5d 3b 0a 20 20 20 20 73 71 6c 69 74 [200];. sqlit
1c2ce 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74 79 50 e3VdbeMemPrettyP
1c2cf 72 69 6e 74 28 70 2c 20 7a 42 75 66 29 3b 0a 20 rint(p, zBuf);.
1c2d0 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 fprintf(out,
1c2d1 22 20 22 29 3b 0a 20 20 20 20 66 70 72 69 6e 74 " ");. fprint
1c2d2 66 28 6f 75 74 2c 20 22 25 73 22 2c 20 7a 42 75 f(out, "%s", zBu
1c2d3 66 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 f);. }.}.static
1c2d4 20 76 6f 69 64 20 72 65 67 69 73 74 65 72 54 72 void registerTr
1c2d5 61 63 65 28 46 49 4c 45 20 2a 6f 75 74 2c 20 69 ace(FILE *out, i
1c2d6 6e 74 20 69 52 65 67 2c 20 4d 65 6d 20 2a 70 29 nt iReg, Mem *p)
1c2d7 7b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c {. fprintf(out,
1c2d8 20 22 52 45 47 5b 25 64 5d 20 3d 20 22 2c 20 69 "REG[%d] = ", i
1c2d9 52 65 67 29 3b 0a 20 20 6d 65 6d 54 72 61 63 65 Reg);. memTrace
1c2da 50 72 69 6e 74 28 6f 75 74 2c 20 70 29 3b 0a 20 Print(out, p);.
1c2db 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c fprintf(out, "\
1c2dc 6e 22 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 n");.}.#endif..#
1c2dd 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
1c2de 55 47 0a 23 20 20 64 65 66 69 6e 65 20 52 45 47 UG.# define REG
1c2df 49 53 54 45 52 5f 54 52 41 43 45 28 52 2c 4d 29 ISTER_TRACE(R,M)
1c2e0 20 69 66 28 70 2d 3e 74 72 61 63 65 29 72 65 67 if(p->trace)reg
1c2e1 69 73 74 65 72 54 72 61 63 65 28 70 2d 3e 74 72 isterTrace(p->tr
1c2e2 61 63 65 2c 52 2c 4d 29 0a 23 65 6c 73 65 0a 23 ace,R,M).#else.#
1c2e3 20 20 64 65 66 69 6e 65 20 52 45 47 49 53 54 45 define REGISTE
1c2e4 52 5f 54 52 41 43 45 28 52 2c 4d 29 0a 23 65 6e R_TRACE(R,M).#en
1c2e5 64 69 66 0a 0a 0a 23 69 66 64 65 66 20 56 44 42 dif...#ifdef VDB
1c2e6 45 5f 50 52 4f 46 49 4c 45 0a 0a 2f 2a 20 0a 2a E_PROFILE../* .*
1c2e7 2a 20 68 77 74 69 6d 65 2e 68 20 63 6f 6e 74 61 * hwtime.h conta
1c2e8 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 73 65 6d ins inline assem
1c2e9 62 6c 65 72 20 63 6f 64 65 20 66 6f 72 20 69 6d bler code for im
1c2ea 70 6c 65 6d 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 plementing .** h
1c2eb 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 20 igh-performance
1c2ec 74 69 6d 69 6e 67 20 72 6f 75 74 69 6e 65 73 2e timing routines.
1c2ed 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .*/./***********
1c2ee 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 77 74 69 *** Include hwti
1c2ef 6d 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 me.h in the midd
1c2f0 6c 65 20 6f 66 20 76 64 62 65 2e 63 20 2a 2a 2a le of vdbe.c ***
1c2f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c2f2 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
1c2f3 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68 *** Begin file h
1c2f4 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a wtime.h ********
1c2f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c2f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c2f7 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d **/./*.** 2008 M
1c2f8 61 79 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ay 27.**.** The
1c2f9 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
1c2fa 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
1c2fb 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
1c2fc 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
1c2fd 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
1c2fe 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
1c2ff 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
1c300 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
1c301 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
1c302 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
1c303 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
1c304 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
1c305 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
1c306 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
1c307 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
1c308 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
1c309 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
1c30a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c30b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c30c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c30d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c30e 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
1c30f 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
1c310 6e 73 20 69 6e 6c 69 6e 65 20 61 73 6d 20 63 6f ns inline asm co
1c311 64 65 20 66 6f 72 20 72 65 74 72 69 65 76 69 6e de for retrievin
1c312 67 20 22 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 g "high-performa
1c313 6e 63 65 22 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 nce".** counters
1c314 20 66 6f 72 20 78 38 36 20 63 6c 61 73 73 20 43 for x86 class C
1c315 50 55 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 PUs..**.** $Id:
1c316 68 77 74 69 6d 65 2e 68 2c 76 20 31 2e 33 20 32 hwtime.h,v 1.3 2
1c317 30 30 38 2f 30 38 2f 30 31 20 31 34 3a 33 33 3a 008/08/01 14:33:
1c318 31 35 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 15 shane Exp $.*
1c319 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57 54 49 4d /.#ifndef _HWTIM
1c31a 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 48 57 E_H_.#define _HW
1c31b 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 TIME_H_../*.** T
1c31c 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 he following rou
1c31d 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 tine only works
1c31e 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c 61 73 73 on pentium-class
1c31f 20 28 6f 72 20 6e 65 77 65 72 29 20 70 72 6f 63 (or newer) proc
1c320 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 73 essors..** It us
1c321 65 73 20 74 68 65 20 52 44 54 53 43 20 6f 70 63 es the RDTSC opc
1c322 6f 64 65 20 74 6f 20 72 65 61 64 20 74 68 65 20 ode to read the
1c323 63 79 63 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75 cycle count valu
1c324 65 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 e out of the.**
1c325 70 72 6f 63 65 73 73 6f 72 20 61 6e 64 20 72 65 processor and re
1c326 74 75 72 6e 73 20 74 68 61 74 20 76 61 6c 75 65 turns that value
1c327 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 . This can be u
1c328 73 65 64 20 66 6f 72 20 68 69 67 68 2d 72 65 73 sed for high-res
1c329 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a .** profiling..*
1c32a 2f 0a 23 69 66 20 28 64 65 66 69 6e 65 64 28 5f /.#if (defined(_
1c32b 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69 _GNUC__) || defi
1c32c 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 29 20 26 ned(_MSC_VER)) &
1c32d 26 20 5c 0a 20 20 20 20 20 20 28 64 65 66 69 6e & \. (defin
1c32e 65 64 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69 ed(i386) || defi
1c32f 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c ned(__i386__) ||
1c330 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 defined(_M_IX86
1c331 29 29 0a 0a 20 20 23 69 66 20 64 65 66 69 6e 65 )).. #if define
1c332 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f d(__GNUC__).. _
1c333 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 _inline__ sqlite
1c334 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 _uint64 sqlite3H
1c335 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 wtime(void){.
1c336 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c unsigned int l
1c337 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f 5f 61 73 o, hi;. __as
1c338 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f m__ __volatile__
1c339 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d 61 22 ("rdtsc" : "=a"
1c33a 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28 68 69 29 (lo), "=d" (hi)
1c33b 29 3b 0a 20 20 20 20 20 72 65 74 75 72 6e 20 28 );. return (
1c33c 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 68 69 sqlite_uint64)hi
1c33d 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d << 32 | lo;. }
1c33e 0a 0a 20 20 23 65 6c 69 66 20 64 65 66 69 6e 65 .. #elif define
1c33f 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 20 5f d(_MSC_VER).. _
1c340 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b 65 64 29 _declspec(naked)
1c341 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65 __inline sqlite
1c342 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20 _uint64 __cdecl
1c343 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f sqlite3Hwtime(vo
1c344 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61 73 6d 20 id){. __asm
1c345 7b 0a 20 20 20 20 20 20 20 20 72 64 74 73 63 0a {. rdtsc.
1c346 20 20 20 20 20 20 20 20 72 65 74 20 20 20 20 20 ret
1c347 20 20 3b 20 72 65 74 75 72 6e 20 76 61 6c 75 65 ; return value
1c348 20 61 74 20 45 44 58 3a 45 41 58 0a 20 20 20 20 at EDX:EAX.
1c349 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 69 66 }. }.. #endif
1c34a 0a 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64 ..#elif (defined
1c34b 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 (__GNUC__) && de
1c34c 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f fined(__x86_64__
1c34d 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f )).. __inline__
1c34e 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 sqlite_uint64 s
1c34f 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 qlite3Hwtime(voi
1c350 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e d){. unsign
1c351 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 ed long val;.
1c352 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c __asm__ __vol
1c353 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 atile__ ("rdtsc"
1c354 20 3a 20 22 3d 41 22 20 28 76 61 6c 29 29 3b 0a : "=A" (val));.
1c355 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c return val
1c356 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 20 28 64 ;. }. .#elif (d
1c357 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 efined(__GNUC__)
1c358 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 70 70 && defined(__pp
1c359 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e c__)).. __inlin
1c35a 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 e__ sqlite_uint6
1c35b 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 4 sqlite3Hwtime(
1c35c 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 void){. uns
1c35d 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 igned long long
1c35e 72 65 74 76 61 6c 3b 0a 20 20 20 20 20 20 75 6e retval;. un
1c35f 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b signed long junk
1c360 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 ;. __asm__
1c361 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c __volatile__ ("\
1c362 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 31 3a 20 n\. 1:
1c363 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 31 5c mftbu %1\
1c364 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 n\.
1c365 20 20 20 20 20 6d 66 74 62 20 20 20 20 25 4c 30 mftb %L0
1c366 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 \n\.
1c367 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 30 mftbu %0
1c368 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 \n\.
1c369 20 20 20 20 20 20 63 6d 70 77 20 20 20 20 25 30 cmpw %0
1c36a 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 ,%1\n\.
1c36b 20 20 20 20 20 20 20 20 20 62 6e 65 20 20 20 20 bne
1c36c 20 31 62 22 0a 20 20 20 20 20 20 20 20 20 20 20 1b".
1c36d 20 20 20 20 20 20 20 3a 20 22 3d 72 22 20 28 72 : "=r" (r
1c36e 65 74 76 61 6c 29 2c 20 22 3d 72 22 20 28 6a 75 etval), "=r" (ju
1c36f 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 nk));. retu
1c370 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a rn retval;. }..
1c371 23 65 6c 73 65 0a 0a 20 20 23 65 72 72 6f 72 20 #else.. #error
1c372 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74 Need implementat
1c373 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 48 77 ion of sqlite3Hw
1c374 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 time() for your
1c375 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a platform... /*.
1c376 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20 ** To compile
1c377 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e without implemen
1c378 74 69 6e 67 20 73 71 6c 69 74 65 33 48 77 74 69 ting sqlite3Hwti
1c379 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c me() for your pl
1c37a 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 atform,. ** you
1c37b 20 63 61 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 can remove the
1c37c 61 62 6f 76 65 20 23 65 72 72 6f 72 20 61 6e 64 above #error and
1c37d 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 use the followi
1c37e 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20 66 75 6e ng. ** stub fun
1c37f 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c 6c ction. You will
1c380 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20 73 75 70 lose timing sup
1c381 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79 0a 20 20 port for many.
1c382 2a 2a 20 6f 66 20 74 68 65 20 64 65 62 75 67 67 ** of the debugg
1c383 69 6e 67 20 61 6e 64 20 74 65 73 74 69 6e 67 20 ing and testing
1c384 75 74 69 6c 69 74 69 65 73 2c 20 62 75 74 20 69 utilities, but i
1c385 74 20 73 68 6f 75 6c 64 20 61 74 0a 20 20 2a 2a t should at. **
1c386 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c 65 20 61 least compile a
1c387 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c nd run.. */.SQL
1c388 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 ITE_PRIVATE sq
1c389 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 lite_uint64 sqli
1c38a 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b te3Hwtime(void){
1c38b 20 72 65 74 75 72 6e 20 28 28 73 71 6c 69 74 65 return ((sqlite
1c38c 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d 0a 0a 23 _uint64)0); }..#
1c38d 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a endif..#endif /*
1c38e 20 21 64 65 66 69 6e 65 64 28 5f 48 57 54 49 4d !defined(_HWTIM
1c38f 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a E_H_) */../*****
1c390 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
1c391 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a hwtime.h ******
1c392 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c393 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c394 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
1c395 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e ********* Contin
1c396 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 uing where we le
1c397 66 74 20 6f 66 66 20 69 6e 20 76 64 62 65 2e 63 ft off in vdbe.c
1c398 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
1c399 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 65 6e 64 69 ********/..#endi
1c39a 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 43 48 45 f../*.** The CHE
1c39b 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 CK_FOR_INTERRUPT
1c39c 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 64 20 68 macro defined h
1c39d 65 72 65 20 6c 6f 6f 6b 73 20 74 6f 20 73 65 65 ere looks to see
1c39e 20 69 66 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 if the.** sqlit
1c39f 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 72 e3_interrupt() r
1c3a0 6f 75 74 69 6e 65 20 68 61 73 20 62 65 65 6e 20 outine has been
1c3a1 63 61 6c 6c 65 64 2e 20 20 49 66 20 69 74 20 68 called. If it h
1c3a2 61 73 20 62 65 65 6e 2c 20 74 68 65 6e 0a 2a 2a as been, then.**
1c3a3 20 70 72 6f 63 65 73 73 69 6e 67 20 6f 66 20 74 processing of t
1c3a4 68 65 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 he VDBE program
1c3a5 69 73 20 69 6e 74 65 72 72 75 70 74 65 64 2e 0a is interrupted..
1c3a6 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 61 63 72 6f **.** This macro
1c3a7 20 61 64 64 65 64 20 74 6f 20 65 76 65 72 79 20 added to every
1c3a8 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 61 74 instruction that
1c3a9 20 64 6f 65 73 20 61 20 6a 75 6d 70 20 69 6e 20 does a jump in
1c3aa 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 69 6d 70 6c order to.** impl
1c3ab 65 6d 65 6e 74 20 61 20 6c 6f 6f 70 2e 20 20 54 ement a loop. T
1c3ac 68 69 73 20 74 65 73 74 20 75 73 65 64 20 74 6f his test used to
1c3ad 20 62 65 20 6f 6e 20 65 76 65 72 79 20 73 69 6e be on every sin
1c3ae 67 6c 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 2c gle instruction,
1c3af 0a 2a 2a 20 62 75 74 20 74 68 61 74 20 6d 65 61 .** but that mea
1c3b0 6e 74 20 77 65 20 6d 6f 72 65 20 74 65 73 74 69 nt we more testi
1c3b1 6e 67 20 74 68 61 74 20 77 65 20 6e 65 65 64 65 ng that we neede
1c3b2 64 2e 20 20 42 79 20 6f 6e 6c 79 20 74 65 73 74 d. By only test
1c3b3 69 6e 67 20 74 68 65 0a 2a 2a 20 66 6c 61 67 20 ing the.** flag
1c3b4 6f 6e 20 6a 75 6d 70 20 69 6e 73 74 72 75 63 74 on jump instruct
1c3b5 69 6f 6e 73 2c 20 77 65 20 67 65 74 20 61 20 28 ions, we get a (
1c3b6 73 6d 61 6c 6c 29 20 73 70 65 65 64 20 69 6d 70 small) speed imp
1c3b7 72 6f 76 65 6d 65 6e 74 2e 0a 2a 2f 0a 23 64 65 rovement..*/.#de
1c3b8 66 69 6e 65 20 43 48 45 43 4b 5f 46 4f 52 5f 49 fine CHECK_FOR_I
1c3b9 4e 54 45 52 52 55 50 54 20 5c 0a 20 20 20 69 66 NTERRUPT \. if
1c3ba 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 ( db->u1.isInter
1c3bb 72 75 70 74 65 64 20 29 20 67 6f 74 6f 20 61 62 rupted ) goto ab
1c3bc 6f 72 74 5f 64 75 65 5f 74 6f 5f 69 6e 74 65 72 ort_due_to_inter
1c3bd 72 75 70 74 3b 0a 0a 23 69 66 64 65 66 20 53 51 rupt;..#ifdef SQ
1c3be 4c 49 54 45 5f 44 45 42 55 47 0a 73 74 61 74 69 LITE_DEBUG.stati
1c3bf 63 20 69 6e 74 20 66 69 6c 65 45 78 69 73 74 73 c int fileExists
1c3c0 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f (sqlite3 *db, co
1c3c1 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 29 nst char *zFile)
1c3c2 7b 0a 20 20 69 6e 74 20 72 65 73 20 3d 20 30 3b {. int res = 0;
1c3c3 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
1c3c4 54 45 5f 4f 4b 3b 0a 23 69 66 64 65 66 20 53 51 TE_OK;.#ifdef SQ
1c3c5 4c 49 54 45 5f 54 45 53 54 0a 20 20 2f 2a 20 49 LITE_TEST. /* I
1c3c6 66 20 77 65 20 61 72 65 20 63 75 72 72 65 6e 74 f we are current
1c3c7 6c 79 20 74 65 73 74 69 6e 67 20 49 4f 20 65 72 ly testing IO er
1c3c8 72 6f 72 73 2c 20 74 68 65 6e 20 64 6f 20 6e 6f rors, then do no
1c3c9 74 20 63 61 6c 6c 20 4f 73 41 63 63 65 73 73 28 t call OsAccess(
1c3ca 29 20 74 6f 0a 20 20 2a 2a 20 74 65 73 74 20 66 ) to. ** test f
1c3cb 6f 72 20 74 68 65 20 70 72 65 73 65 6e 63 65 20 or the presence
1c3cc 6f 66 20 7a 46 69 6c 65 2e 20 54 68 69 73 20 69 of zFile. This i
1c3cd 73 20 62 65 63 61 75 73 65 20 61 6e 79 20 49 4f s because any IO
1c3ce 20 65 72 72 6f 72 20 74 68 61 74 0a 20 20 2a 2a error that. **
1c3cf 20 6f 63 63 75 72 73 20 68 65 72 65 20 77 69 6c occurs here wil
1c3d0 6c 20 6e 6f 74 20 62 65 20 72 65 70 6f 72 74 65 l not be reporte
1c3d1 64 2c 20 63 61 75 73 69 6e 67 20 74 68 65 20 74 d, causing the t
1c3d2 65 73 74 20 74 6f 20 66 61 69 6c 2e 0a 20 20 2a est to fail.. *
1c3d3 2f 0a 20 20 65 78 74 65 72 6e 20 69 6e 74 20 73 /. extern int s
1c3d4 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
1c3d5 70 65 6e 64 69 6e 67 3b 0a 20 20 69 66 28 20 73 pending;. if( s
1c3d6 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
1c3d7 70 65 6e 64 69 6e 67 3c 3d 30 20 29 0a 23 65 6e pending<=0 ).#en
1c3d8 64 69 66 0a 20 20 20 20 72 63 20 3d 20 73 71 6c dif. rc = sql
1c3d9 69 74 65 33 4f 73 41 63 63 65 73 73 28 64 62 2d ite3OsAccess(db-
1c3da 3e 70 56 66 73 2c 20 7a 46 69 6c 65 2c 20 53 51 >pVfs, zFile, SQ
1c3db 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 LITE_ACCESS_EXIS
1c3dc 54 53 2c 20 26 72 65 73 29 3b 0a 20 20 72 65 74 TS, &res);. ret
1c3dd 75 72 6e 20 28 72 65 73 20 26 26 20 72 63 3d 3d urn (res && rc==
1c3de 53 51 4c 49 54 45 5f 4f 4b 29 3b 0a 7d 0a 23 65 SQLITE_OK);.}.#e
1c3df 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 4e 44 ndif..#ifndef ND
1c3e0 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 EBUG./*.** This
1c3e1 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 function is only
1c3e2 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 called from wit
1c3e3 68 69 6e 20 61 6e 20 61 73 73 65 72 74 28 29 20 hin an assert()
1c3e4 65 78 70 72 65 73 73 69 6f 6e 2e 20 49 74 0a 2a expression. It.*
1c3e5 2a 20 63 68 65 63 6b 73 20 74 68 61 74 20 74 68 * checks that th
1c3e6 65 20 73 71 6c 69 74 65 33 2e 6e 54 72 61 6e 73 e sqlite3.nTrans
1c3e7 61 63 74 69 6f 6e 20 76 61 72 69 61 62 6c 65 20 action variable
1c3e8 69 73 20 63 6f 72 72 65 63 74 6c 79 20 73 65 74 is correctly set
1c3e9 20 74 6f 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 to.** the numbe
1c3ea 72 20 6f 66 20 6e 6f 6e 2d 74 72 61 6e 73 61 63 r of non-transac
1c3eb 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 73 20 tion savepoints
1c3ec 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 65 currently in the
1c3ed 20 0a 2a 2a 20 6c 69 6e 6b 65 64 20 6c 69 73 74 .** linked list
1c3ee 20 73 74 61 72 74 69 6e 67 20 61 74 20 73 71 6c starting at sql
1c3ef 69 74 65 33 2e 70 53 61 76 65 70 6f 69 6e 74 2e ite3.pSavepoint.
1c3f0 0a 2a 2a 20 0a 2a 2a 20 55 73 61 67 65 3a 0a 2a .** .** Usage:.*
1c3f1 2a 0a 2a 2a 20 20 20 20 20 61 73 73 65 72 74 28 *.** assert(
1c3f2 20 63 68 65 63 6b 53 61 76 65 70 6f 69 6e 74 43 checkSavepointC
1c3f3 6f 75 6e 74 28 64 62 29 20 29 3b 0a 2a 2f 0a 73 ount(db) );.*/.s
1c3f4 74 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b 53 tatic int checkS
1c3f5 61 76 65 70 6f 69 6e 74 43 6f 75 6e 74 28 73 71 avepointCount(sq
1c3f6 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e lite3 *db){. in
1c3f7 74 20 6e 20 3d 20 30 3b 0a 20 20 53 61 76 65 70 t n = 0;. Savep
1c3f8 6f 69 6e 74 20 2a 70 3b 0a 20 20 66 6f 72 28 70 oint *p;. for(p
1c3f9 3d 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b =db->pSavepoint;
1c3fa 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 20 p; p=p->pNext)
1c3fb 6e 2b 2b 3b 0a 20 20 61 73 73 65 72 74 28 20 6e n++;. assert( n
1c3fc 3d 3d 28 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e ==(db->nSavepoin
1c3fd 74 20 2b 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 t + db->isTransa
1c3fe 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74 29 20 ctionSavepoint)
1c3ff 29 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d );. return 1;.}
1c400 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 .#endif../*.** E
1c401 78 65 63 75 74 65 20 61 73 20 6d 75 63 68 20 6f xecute as much o
1c402 66 20 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d f a VDBE program
1c403 20 61 73 20 77 65 20 63 61 6e 20 74 68 65 6e 20 as we can then
1c404 72 65 74 75 72 6e 2e 0a 2a 2a 0a 2a 2a 20 73 71 return..**.** sq
1c405 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 lite3VdbeMakeRea
1c406 64 79 28 29 20 6d 75 73 74 20 62 65 20 63 61 6c dy() must be cal
1c407 6c 65 64 20 62 65 66 6f 72 65 20 74 68 69 73 20 led before this
1c408 72 6f 75 74 69 6e 65 20 69 6e 20 6f 72 64 65 72 routine in order
1c409 20 74 6f 0a 2a 2a 20 63 6c 6f 73 65 20 74 68 65 to.** close the
1c40a 20 70 72 6f 67 72 61 6d 20 77 69 74 68 20 61 20 program with a
1c40b 66 69 6e 61 6c 20 4f 50 5f 48 61 6c 74 20 61 6e final OP_Halt an
1c40c 64 20 74 6f 20 73 65 74 20 75 70 20 74 68 65 20 d to set up the
1c40d 63 61 6c 6c 62 61 63 6b 73 0a 2a 2a 20 61 6e 64 callbacks.** and
1c40e 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 the error messa
1c40f 67 65 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a ge pointer..**.*
1c410 2a 20 57 68 65 6e 65 76 65 72 20 61 20 72 6f 77 * Whenever a row
1c411 20 6f 72 20 72 65 73 75 6c 74 20 64 61 74 61 20 or result data
1c412 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 is available, th
1c413 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 is routine will
1c414 65 69 74 68 65 72 0a 2a 2a 20 69 6e 76 6f 6b 65 either.** invoke
1c415 20 74 68 65 20 72 65 73 75 6c 74 20 63 61 6c 6c the result call
1c416 62 61 63 6b 20 28 69 66 20 74 68 65 72 65 20 69 back (if there i
1c417 73 20 6f 6e 65 29 20 6f 72 20 72 65 74 75 72 6e s one) or return
1c418 20 77 69 74 68 0a 2a 2a 20 53 51 4c 49 54 45 5f with.** SQLITE_
1c419 52 4f 57 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e ROW..**.** If an
1c41a 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 attempt is made
1c41b 20 74 6f 20 6f 70 65 6e 20 61 20 6c 6f 63 6b 65 to open a locke
1c41c 64 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e d database, then
1c41d 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a this routine.**
1c41e 20 77 69 6c 6c 20 65 69 74 68 65 72 20 69 6e 76 will either inv
1c41f 6f 6b 65 20 74 68 65 20 62 75 73 79 20 63 61 6c oke the busy cal
1c420 6c 62 61 63 6b 20 28 69 66 20 74 68 65 72 65 20 lback (if there
1c421 69 73 20 6f 6e 65 29 20 6f 72 20 69 74 20 77 69 is one) or it wi
1c422 6c 6c 0a 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c ll.** return SQL
1c423 49 54 45 5f 42 55 53 59 2e 0a 2a 2a 0a 2a 2a 20 ITE_BUSY..**.**
1c424 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 If an error occu
1c425 72 73 2c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 rs, an error mes
1c426 73 61 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 sage is written
1c427 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e to memory obtain
1c428 65 64 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 ed.** from sqlit
1c429 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 e3_malloc() and
1c42a 70 2d 3e 7a 45 72 72 4d 73 67 20 69 73 20 6d 61 p->zErrMsg is ma
1c42b 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 de to point to t
1c42c 68 61 74 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 54 hat memory..** T
1c42d 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 he error code is
1c42e 20 73 74 6f 72 65 64 20 69 6e 20 70 2d 3e 72 63 stored in p->rc
1c42f 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e and this routin
1c430 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 e returns SQLITE
1c431 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 49 66 _ERROR..**.** If
1c432 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 65 76 the callback ev
1c433 65 72 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a er returns non-z
1c434 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 70 72 ero, then the pr
1c435 6f 67 72 61 6d 20 65 78 69 74 73 0a 2a 2a 20 69 ogram exits.** i
1c436 6d 6d 65 64 69 61 74 65 6c 79 2e 20 20 54 68 65 mmediately. The
1c437 72 65 20 77 69 6c 6c 20 62 65 20 6e 6f 20 65 72 re will be no er
1c438 72 6f 72 20 6d 65 73 73 61 67 65 20 62 75 74 20 ror message but
1c439 74 68 65 20 70 2d 3e 72 63 20 66 69 65 6c 64 20 the p->rc field
1c43a 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 53 51 4c is.** set to SQL
1c43b 49 54 45 5f 41 42 4f 52 54 20 61 6e 64 20 74 68 ITE_ABORT and th
1c43c 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 is routine will
1c43d 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 return SQLITE_ER
1c43e 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 41 20 6d 65 6d ROR..**.** A mem
1c43f 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 ory allocation e
1c440 72 72 6f 72 20 63 61 75 73 65 73 20 70 2d 3e 72 rror causes p->r
1c441 63 20 74 6f 20 62 65 20 73 65 74 20 74 6f 20 53 c to be set to S
1c442 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 61 6e 64 20 QLITE_NOMEM and
1c443 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 this.** routine
1c444 74 6f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 to return SQLITE
1c445 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 4f 74 _ERROR..**.** Ot
1c446 68 65 72 20 66 61 74 61 6c 20 65 72 72 6f 72 73 her fatal errors
1c447 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 return SQLITE_E
1c448 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 RROR..**.** Afte
1c449 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 68 r this routine h
1c44a 61 73 20 66 69 6e 69 73 68 65 64 2c 20 73 71 6c as finished, sql
1c44b 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 ite3VdbeFinalize
1c44c 28 29 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 () should be.**
1c44d 75 73 65 64 20 74 6f 20 63 6c 65 61 6e 20 75 70 used to clean up
1c44e 20 74 68 65 20 6d 65 73 73 20 74 68 61 74 20 77 the mess that w
1c44f 61 73 20 6c 65 66 74 20 62 65 68 69 6e 64 2e 0a as left behind..
1c450 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1c451 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
1c452 65 45 78 65 63 28 0a 20 20 56 64 62 65 20 2a 70 eExec(. Vdbe *p
1c453 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c454 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 /* The VDBE
1c455 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 70 63 3b 20 */.){. int pc;
1c456 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c457 20 20 20 2f 2a 20 54 68 65 20 70 72 6f 67 72 61 /* The progra
1c458 6d 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 4f m counter */. O
1c459 70 20 2a 70 4f 70 3b 20 20 20 20 20 20 20 20 20 p *pOp;
1c45a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 /* Cur
1c45b 72 65 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20 2a rent operation *
1c45c 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c /. int rc = SQL
1c45d 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 2f ITE_OK; /
1c45e 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72 * Value to retur
1c45f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a n */. sqlite3 *
1c460 64 62 20 3d 20 70 2d 3e 64 62 3b 20 20 20 20 20 db = p->db;
1c461 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 /* The databas
1c462 65 20 2a 2f 0a 20 20 75 38 20 65 6e 63 6f 64 69 e */. u8 encodi
1c463 6e 67 20 3d 20 45 4e 43 28 64 62 29 3b 20 20 20 ng = ENC(db);
1c464 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 /* The databas
1c465 65 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 e encoding */.
1c466 4d 65 6d 20 2a 70 49 6e 31 20 3d 20 30 3b 20 20 Mem *pIn1 = 0;
1c467 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 73 /* 1s
1c468 74 20 69 6e 70 75 74 20 6f 70 65 72 61 6e 64 20 t input operand
1c469 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 6e 32 20 3d */. Mem *pIn2 =
1c46a 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
1c46b 2f 2a 20 32 6e 64 20 69 6e 70 75 74 20 6f 70 65 /* 2nd input ope
1c46c 72 61 6e 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 rand */. Mem *p
1c46d 49 6e 33 20 3d 20 30 3b 20 20 20 20 20 20 20 20 In3 = 0;
1c46e 20 20 20 20 20 2f 2a 20 33 72 64 20 69 6e 70 75 /* 3rd inpu
1c46f 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 4d t operand */. M
1c470 65 6d 20 2a 70 4f 75 74 20 3d 20 30 3b 20 20 20 em *pOut = 0;
1c471 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 /* Out
1c472 70 75 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 put operand */.
1c473 20 75 38 20 6f 70 50 72 6f 70 65 72 74 79 3b 0a u8 opProperty;.
1c474 20 20 69 6e 74 20 69 43 6f 6d 70 61 72 65 20 3d int iCompare =
1c475 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 0; /*
1c476 52 65 73 75 6c 74 20 6f 66 20 6c 61 73 74 20 4f Result of last O
1c477 50 5f 43 6f 6d 70 61 72 65 20 6f 70 65 72 61 74 P_Compare operat
1c478 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 50 ion */. int *aP
1c479 65 72 6d 75 74 65 20 3d 20 30 3b 20 20 20 20 20 ermute = 0;
1c47a 20 20 20 20 2f 2a 20 50 65 72 6d 75 74 61 74 69 /* Permutati
1c47b 6f 6e 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 66 6f on of columns fo
1c47c 72 20 4f 50 5f 43 6f 6d 70 61 72 65 20 2a 2f 0a r OP_Compare */.
1c47d 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 #ifdef VDBE_PROF
1c47e 49 4c 45 0a 20 20 75 36 34 20 73 74 61 72 74 3b ILE. u64 start;
1c47f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c480 20 2f 2a 20 43 50 55 20 63 6c 6f 63 6b 20 63 6f /* CPU clock co
1c481 75 6e 74 20 61 74 20 73 74 61 72 74 20 6f 66 20 unt at start of
1c482 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 opcode */. int
1c483 6f 72 69 67 50 63 3b 20 20 20 20 20 20 20 20 20 origPc;
1c484 20 20 20 20 20 20 20 2f 2a 20 50 72 6f 67 72 61 /* Progra
1c485 6d 20 63 6f 75 6e 74 65 72 20 61 74 20 73 74 61 m counter at sta
1c486 72 74 20 6f 66 20 6f 70 63 6f 64 65 20 2a 2f 0a rt of opcode */.
1c487 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 #endif.#ifndef S
1c488 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 QLITE_OMIT_PROGR
1c489 45 53 53 5f 43 41 4c 4c 42 41 43 4b 0a 20 20 69 ESS_CALLBACK. i
1c48a 6e 74 20 6e 50 72 6f 67 72 65 73 73 4f 70 73 20 nt nProgressOps
1c48b 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 4f 70 63 = 0; /* Opc
1c48c 6f 64 65 73 20 65 78 65 63 75 74 65 64 20 73 69 odes executed si
1c48d 6e 63 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c nce progress cal
1c48e 6c 62 61 63 6b 2e 20 2a 2f 0a 23 65 6e 64 69 66 lback. */.#endif
1c48f 0a 20 20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a . /************
1c490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c491 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c492 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c493 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20 2a 2a 20 41 75 ********. ** Au
1c494 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e 65 tomatically gene
1c495 72 61 74 65 64 20 63 6f 64 65 0a 20 20 2a 2a 0a rated code. **.
1c496 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 ** The followi
1c497 6e 67 20 75 6e 69 6f 6e 20 69 73 20 61 75 74 6f ng union is auto
1c498 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 matically genera
1c499 74 65 64 20 62 79 20 74 68 65 0a 20 20 2a 2a 20 ted by the. **
1c49a 76 64 62 65 2d 63 6f 6d 70 72 65 73 73 2e 74 63 vdbe-compress.tc
1c49b 6c 20 73 63 72 69 70 74 2e 20 20 54 68 65 20 70 l script. The p
1c49c 75 72 70 6f 73 65 20 6f 66 20 74 68 69 73 20 75 urpose of this u
1c49d 6e 69 6f 6e 20 69 73 20 74 6f 0a 20 20 2a 2a 20 nion is to. **
1c49e 72 65 64 75 63 65 20 74 68 65 20 61 6d 6f 75 6e reduce the amoun
1c49f 74 20 6f 66 20 73 74 61 63 6b 20 73 70 61 63 65 t of stack space
1c4a0 20 72 65 71 75 69 72 65 64 20 62 79 20 74 68 69 required by thi
1c4a1 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2a s function.. **
1c4a2 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 69 6e See comments in
1c4a3 20 74 68 65 20 76 64 62 65 2d 63 6f 6d 70 72 65 the vdbe-compre
1c4a4 73 73 2e 74 63 6c 20 73 63 72 69 70 74 20 66 6f ss.tcl script fo
1c4a5 72 20 64 65 74 61 69 6c 73 2e 0a 20 20 2a 2f 0a r details.. */.
1c4a6 20 20 75 6e 69 6f 6e 20 76 64 62 65 45 78 65 63 union vdbeExec
1c4a7 55 6e 69 6f 6e 20 7b 0a 20 20 20 20 73 74 72 75 Union {. stru
1c4a8 63 74 20 4f 50 5f 59 69 65 6c 64 5f 73 74 61 63 ct OP_Yield_stac
1c4a9 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 k_vars {. i
1c4aa 6e 74 20 70 63 44 65 73 74 3b 0a 20 20 20 20 7d nt pcDest;. }
1c4ab 20 61 61 3b 0a 20 20 20 20 73 74 72 75 63 74 20 aa;. struct
1c4ac 4f 50 5f 56 61 72 69 61 62 6c 65 5f 73 74 61 63 OP_Variable_stac
1c4ad 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 k_vars {. i
1c4ae 6e 74 20 70 31 3b 20 20 20 20 20 20 20 20 20 20 nt p1;
1c4af 2f 2a 20 56 61 72 69 61 62 6c 65 20 74 6f 20 63 /* Variable to c
1c4b0 6f 70 79 20 66 72 6f 6d 20 2a 2f 0a 20 20 20 20 opy from */.
1c4b1 20 20 69 6e 74 20 70 32 3b 20 20 20 20 20 20 20 int p2;
1c4b2 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74 /* Register t
1c4b3 6f 20 63 6f 70 79 20 74 6f 20 2a 2f 0a 20 20 20 o copy to */.
1c4b4 20 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 int n;
1c4b5 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1c4b6 20 76 61 6c 75 65 73 20 6c 65 66 74 20 74 6f 20 values left to
1c4b7 63 6f 70 79 20 2a 2f 0a 20 20 20 20 20 20 4d 65 copy */. Me
1c4b8 6d 20 2a 70 56 61 72 3b 20 20 20 20 20 20 20 2f m *pVar; /
1c4b9 2a 20 56 61 6c 75 65 20 62 65 69 6e 67 20 74 72 * Value being tr
1c4ba 61 6e 73 66 65 72 72 65 64 20 2a 2f 0a 20 20 20 ansferred */.
1c4bb 20 7d 20 61 62 3b 0a 20 20 20 20 73 74 72 75 63 } ab;. struc
1c4bc 74 20 4f 50 5f 4d 6f 76 65 5f 73 74 61 63 6b 5f t OP_Move_stack_
1c4bd 76 61 72 73 20 7b 0a 20 20 20 20 20 20 63 68 61 vars {. cha
1c4be 72 20 2a 7a 4d 61 6c 6c 6f 63 3b 20 20 20 2f 2a r *zMalloc; /*
1c4bf 20 48 6f 6c 64 69 6e 67 20 76 61 72 69 61 62 6c Holding variabl
1c4c0 65 20 66 6f 72 20 61 6c 6c 6f 63 61 74 65 64 20 e for allocated
1c4c1 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 20 20 20 20 memory */.
1c4c2 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20 20 int n;
1c4c3 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 /* Number of re
1c4c4 67 69 73 74 65 72 73 20 6c 65 66 74 20 74 6f 20 gisters left to
1c4c5 63 6f 70 79 20 2a 2f 0a 20 20 20 20 20 20 69 6e copy */. in
1c4c6 74 20 70 31 3b 20 20 20 20 20 20 20 20 20 20 2f t p1; /
1c4c7 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20 63 6f * Register to co
1c4c8 70 79 20 66 72 6f 6d 20 2a 2f 0a 20 20 20 20 20 py from */.
1c4c9 20 69 6e 74 20 70 32 3b 20 20 20 20 20 20 20 20 int p2;
1c4ca 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74 6f /* Register to
1c4cb 20 63 6f 70 79 20 74 6f 20 2a 2f 0a 20 20 20 20 copy to */.
1c4cc 7d 20 61 63 3b 0a 20 20 20 20 73 74 72 75 63 74 } ac;. struct
1c4cd 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 5f 73 74 OP_ResultRow_st
1c4ce 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 ack_vars {.
1c4cf 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 20 20 Mem *pMem;.
1c4d0 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 7d 20 61 int i;. } a
1c4d1 64 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 d;. struct OP
1c4d2 5f 43 6f 6e 63 61 74 5f 73 74 61 63 6b 5f 76 61 _Concat_stack_va
1c4d3 72 73 20 7b 0a 20 20 20 20 20 20 69 36 34 20 6e rs {. i64 n
1c4d4 42 79 74 65 3b 0a 20 20 20 20 7d 20 61 65 3b 0a Byte;. } ae;.
1c4d5 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 52 65 struct OP_Re
1c4d6 6d 61 69 6e 64 65 72 5f 73 74 61 63 6b 5f 76 61 mainder_stack_va
1c4d7 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 66 rs {. int f
1c4d8 6c 61 67 73 3b 20 20 20 20 20 20 2f 2a 20 43 6f lags; /* Co
1c4d9 6d 62 69 6e 65 64 20 4d 45 4d 5f 2a 20 66 6c 61 mbined MEM_* fla
1c4da 67 73 20 66 72 6f 6d 20 62 6f 74 68 20 69 6e 70 gs from both inp
1c4db 75 74 73 20 2a 2f 0a 20 20 20 20 20 20 69 36 34 uts */. i64
1c4dc 20 69 41 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 iA; /*
1c4dd 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20 6f 66 Integer value of
1c4de 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 2a 2f left operand */
1c4df 0a 20 20 20 20 20 20 69 36 34 20 69 42 3b 20 20 . i64 iB;
1c4e0 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 /* Intege
1c4e1 72 20 76 61 6c 75 65 20 6f 66 20 72 69 67 68 74 r value of right
1c4e2 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 20 20 operand */.
1c4e3 20 20 64 6f 75 62 6c 65 20 72 41 3b 20 20 20 20 double rA;
1c4e4 20 20 2f 2a 20 52 65 61 6c 20 76 61 6c 75 65 20 /* Real value
1c4e5 6f 66 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 of left operand
1c4e6 2a 2f 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 */. double
1c4e7 72 42 3b 20 20 20 20 20 20 2f 2a 20 52 65 61 6c rB; /* Real
1c4e8 20 76 61 6c 75 65 20 6f 66 20 72 69 67 68 74 20 value of right
1c4e9 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 20 20 7d operand */. }
1c4ea 20 61 66 3b 0a 20 20 20 20 73 74 72 75 63 74 20 af;. struct
1c4eb 4f 50 5f 46 75 6e 63 74 69 6f 6e 5f 73 74 61 63 OP_Function_stac
1c4ec 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 k_vars {. i
1c4ed 6e 74 20 69 3b 0a 20 20 20 20 20 20 4d 65 6d 20 nt i;. Mem
1c4ee 2a 70 41 72 67 3b 0a 20 20 20 20 20 20 73 71 6c *pArg;. sql
1c4ef 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 ite3_context ctx
1c4f0 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ;. sqlite3_
1c4f1 76 61 6c 75 65 20 2a 2a 61 70 56 61 6c 3b 0a 20 value **apVal;.
1c4f2 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 int n;.
1c4f3 7d 20 61 67 3b 0a 20 20 20 20 73 74 72 75 63 74 } ag;. struct
1c4f4 20 4f 50 5f 53 68 69 66 74 52 69 67 68 74 5f 73 OP_ShiftRight_s
1c4f5 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c4f6 20 20 69 36 34 20 61 3b 0a 20 20 20 20 20 20 69 i64 a;. i
1c4f7 36 34 20 62 3b 0a 20 20 20 20 7d 20 61 68 3b 0a 64 b;. } ah;.
1c4f8 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 47 65 struct OP_Ge
1c4f9 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 _stack_vars {.
1c4fa 20 20 20 20 69 6e 74 20 72 65 73 3b 20 20 20 20 int res;
1c4fb 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c /* Resul
1c4fc 74 20 6f 66 20 74 68 65 20 63 6f 6d 70 61 72 69 t of the compari
1c4fd 73 6f 6e 20 6f 66 20 70 49 6e 31 20 61 67 61 69 son of pIn1 agai
1c4fe 6e 73 74 20 70 49 6e 33 20 2a 2f 0a 20 20 20 20 nst pIn3 */.
1c4ff 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 3b char affinity;
1c500 20 20 20 20 20 20 2f 2a 20 41 66 66 69 6e 69 74 /* Affinit
1c501 79 20 74 6f 20 75 73 65 20 66 6f 72 20 63 6f 6d y to use for com
1c502 70 61 72 69 73 6f 6e 20 2a 2f 0a 20 20 20 20 7d parison */. }
1c503 20 61 69 3b 0a 20 20 20 20 73 74 72 75 63 74 20 ai;. struct
1c504 4f 50 5f 43 6f 6d 70 61 72 65 5f 73 74 61 63 6b OP_Compare_stack
1c505 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e _vars {. in
1c506 74 20 6e 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 t n;. int i
1c507 3b 0a 20 20 20 20 20 20 69 6e 74 20 70 31 3b 0a ;. int p1;.
1c508 20 20 20 20 20 20 69 6e 74 20 70 32 3b 0a 20 20 int p2;.
1c509 20 20 20 20 63 6f 6e 73 74 20 4b 65 79 49 6e 66 const KeyInf
1c50a 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 o *pKeyInfo;.
1c50b 20 20 20 69 6e 74 20 69 64 78 3b 0a 20 20 20 20 int idx;.
1c50c 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c CollSeq *pColl
1c50d 3b 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e ; /* Collatin
1c50e 67 20 73 65 71 75 65 6e 63 65 20 74 6f 20 75 73 g sequence to us
1c50f 65 20 6f 6e 20 74 68 69 73 20 74 65 72 6d 20 2a e on this term *
1c510 2f 0a 20 20 20 20 20 20 69 6e 74 20 62 52 65 76 /. int bRev
1c511 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 ; /* Tr
1c512 75 65 20 66 6f 72 20 44 45 53 43 45 4e 44 49 4e ue for DESCENDIN
1c513 47 20 73 6f 72 74 20 6f 72 64 65 72 20 2a 2f 0a G sort order */.
1c514 20 20 20 20 7d 20 61 6a 3b 0a 20 20 20 20 73 74 } aj;. st
1c515 72 75 63 74 20 4f 50 5f 4f 72 5f 73 74 61 63 6b ruct OP_Or_stack
1c516 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e _vars {. in
1c517 74 20 76 31 3b 20 20 20 20 2f 2a 20 4c 65 66 74 t v1; /* Left
1c518 20 6f 70 65 72 61 6e 64 3a 20 20 30 3d 3d 46 41 operand: 0==FA
1c519 4c 53 45 2c 20 31 3d 3d 54 52 55 45 2c 20 32 3d LSE, 1==TRUE, 2=
1c51a 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 55 4c 4c =UNKNOWN or NULL
1c51b 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 76 32 */. int v2
1c51c 3b 20 20 20 20 2f 2a 20 52 69 67 68 74 20 6f 70 ; /* Right op
1c51d 65 72 61 6e 64 3a 20 30 3d 3d 46 41 4c 53 45 2c erand: 0==FALSE,
1c51e 20 31 3d 3d 54 52 55 45 2c 20 32 3d 3d 55 4e 4b 1==TRUE, 2==UNK
1c51f 4e 4f 57 4e 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a NOWN or NULL */.
1c520 20 20 20 20 7d 20 61 6b 3b 0a 20 20 20 20 73 74 } ak;. st
1c521 72 75 63 74 20 4f 50 5f 49 66 4e 6f 74 5f 73 74 ruct OP_IfNot_st
1c522 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 ack_vars {.
1c523 20 69 6e 74 20 63 3b 0a 20 20 20 20 7d 20 61 6c int c;. } al
1c524 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f ;. struct OP_
1c525 43 6f 6c 75 6d 6e 5f 73 74 61 63 6b 5f 76 61 72 Column_stack_var
1c526 73 20 7b 0a 20 20 20 20 20 20 75 33 32 20 70 61 s {. u32 pa
1c527 79 6c 6f 61 64 53 69 7a 65 3b 20 20 20 2f 2a 20 yloadSize; /*
1c528 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
1c529 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f in the record */
1c52a 0a 20 20 20 20 20 20 69 36 34 20 70 61 79 6c 6f . i64 paylo
1c52b 61 64 53 69 7a 65 36 34 3b 20 2f 2a 20 4e 75 6d adSize64; /* Num
1c52c 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 ber of bytes in
1c52d 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 the record */.
1c52e 20 20 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20 int p1;
1c52f 20 20 20 20 20 20 20 2f 2a 20 50 31 20 76 61 6c /* P1 val
1c530 75 65 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 ue of the opcode
1c531 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 70 32 */. int p2
1c532 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
1c533 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 74 6f column number to
1c534 20 72 65 74 72 69 65 76 65 20 2a 2f 0a 20 20 20 retrieve */.
1c535 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 VdbeCursor *p
1c536 43 3b 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 C; /* The VDB
1c537 45 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20 E cursor */.
1c538 20 20 63 68 61 72 20 2a 7a 52 65 63 3b 20 20 20 char *zRec;
1c539 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
1c53a 74 6f 20 63 6f 6d 70 6c 65 74 65 20 72 65 63 6f to complete reco
1c53b 72 64 2d 64 61 74 61 20 2a 2f 0a 20 20 20 20 20 rd-data */.
1c53c 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 BtCursor *pCrsr
1c53d 3b 20 20 20 2f 2a 20 54 68 65 20 42 54 72 65 65 ; /* The BTree
1c53e 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20 20 cursor */.
1c53f 20 75 33 32 20 2a 61 54 79 70 65 3b 20 20 20 20 u32 *aType;
1c540 20 20 20 20 2f 2a 20 61 54 79 70 65 5b 69 5d 20 /* aType[i]
1c541 68 6f 6c 64 73 20 74 68 65 20 6e 75 6d 65 72 69 holds the numeri
1c542 63 20 74 79 70 65 20 6f 66 20 74 68 65 20 69 2d c type of the i-
1c543 74 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 20 th column */.
1c544 20 20 20 75 33 32 20 2a 61 4f 66 66 73 65 74 3b u32 *aOffset;
1c545 20 20 20 20 20 20 2f 2a 20 61 4f 66 66 73 65 74 /* aOffset
1c546 5b 69 5d 20 69 73 20 6f 66 66 73 65 74 20 74 6f [i] is offset to
1c547 20 73 74 61 72 74 20 6f 66 20 64 61 74 61 20 66 start of data f
1c548 6f 72 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 2a or i-th column *
1c549 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 46 69 65 /. int nFie
1c54a 6c 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 6e 75 ld; /* nu
1c54b 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 73 20 69 mber of fields i
1c54c 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a n the record */.
1c54d 20 20 20 20 20 20 69 6e 74 20 6c 65 6e 3b 20 20 int len;
1c54e 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1c54f 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73 65 length of the se
1c550 72 69 61 6c 69 7a 65 64 20 64 61 74 61 20 66 6f rialized data fo
1c551 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a r the column */.
1c552 20 20 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 int i;
1c553 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 /* Loop
1c554 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 counter */.
1c555 20 20 63 68 61 72 20 2a 7a 44 61 74 61 3b 20 20 char *zData;
1c556 20 20 20 20 20 2f 2a 20 50 61 72 74 20 6f 66 20 /* Part of
1c557 74 68 65 20 72 65 63 6f 72 64 20 62 65 69 6e 67 the record being
1c558 20 64 65 63 6f 64 65 64 20 2a 2f 0a 20 20 20 20 decoded */.
1c559 20 20 4d 65 6d 20 2a 70 44 65 73 74 3b 20 20 20 Mem *pDest;
1c55a 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f /* Where to
1c55b 20 77 72 69 74 65 20 74 68 65 20 65 78 74 72 61 write the extra
1c55c 63 74 65 64 20 76 61 6c 75 65 20 2a 2f 0a 20 20 cted value */.
1c55d 20 20 20 20 4d 65 6d 20 73 4d 65 6d 3b 20 20 20 Mem sMem;
1c55e 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 73 74 /* For st
1c55f 6f 72 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64 oring the record
1c560 20 62 65 69 6e 67 20 64 65 63 6f 64 65 64 20 2a being decoded *
1c561 2f 0a 20 20 20 20 20 20 75 38 20 2a 7a 49 64 78 /. u8 *zIdx
1c562 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e ; /* In
1c563 64 65 78 20 69 6e 74 6f 20 68 65 61 64 65 72 20 dex into header
1c564 2a 2f 0a 20 20 20 20 20 20 75 38 20 2a 7a 45 6e */. u8 *zEn
1c565 64 48 64 72 3b 20 20 20 20 20 20 20 2f 2a 20 50 dHdr; /* P
1c566 6f 69 6e 74 65 72 20 74 6f 20 66 69 72 73 74 20 ointer to first
1c567 62 79 74 65 20 61 66 74 65 72 20 74 68 65 20 68 byte after the h
1c568 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 20 20 75 eader */. u
1c569 33 32 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20 32 offset;
1c56a 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f /* Offset into
1c56b 20 74 68 65 20 64 61 74 61 20 2a 2f 0a 20 20 20 the data */.
1c56c 20 20 20 75 36 34 20 6f 66 66 73 65 74 36 34 3b u64 offset64;
1c56d 20 20 20 20 20 20 2f 2a 20 36 34 2d 62 69 74 20 /* 64-bit
1c56e 6f 66 66 73 65 74 2e 20 20 36 34 20 62 69 74 73 offset. 64 bits
1c56f 20 6e 65 65 64 65 64 20 74 6f 20 63 61 74 63 68 needed to catch
1c570 20 6f 76 65 72 66 6c 6f 77 20 2a 2f 0a 20 20 20 overflow */.
1c571 20 20 20 69 6e 74 20 73 7a 48 64 72 3b 20 20 20 int szHdr;
1c572 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
1c573 20 74 68 65 20 68 65 61 64 65 72 20 73 69 7a 65 the header size
1c574 20 66 69 65 6c 64 20 61 74 20 73 74 61 72 74 20 field at start
1c575 6f 66 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 of record */.
1c576 20 20 20 69 6e 74 20 61 76 61 69 6c 3b 20 20 20 int avail;
1c577 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
1c578 6f 66 20 62 79 74 65 73 20 6f 66 20 61 76 61 69 of bytes of avai
1c579 6c 61 62 6c 65 20 64 61 74 61 20 2a 2f 0a 20 20 lable data */.
1c57a 20 20 20 20 4d 65 6d 20 2a 70 52 65 67 3b 20 20 Mem *pReg;
1c57b 20 20 20 20 20 20 20 2f 2a 20 50 73 65 75 64 6f /* Pseudo
1c57c 54 61 62 6c 65 20 69 6e 70 75 74 20 72 65 67 69 Table input regi
1c57d 73 74 65 72 20 2a 2f 0a 20 20 20 20 7d 20 61 6d ster */. } am
1c57e 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f ;. struct OP_
1c57f 41 66 66 69 6e 69 74 79 5f 73 74 61 63 6b 5f 76 Affinity_stack_v
1c580 61 72 73 20 7b 0a 20 20 20 20 20 20 63 68 61 72 ars {. char
1c581 20 2a 7a 41 66 66 69 6e 69 74 79 3b 20 20 20 2f *zAffinity; /
1c582 2a 20 54 68 65 20 61 66 66 69 6e 69 74 79 20 74 * The affinity t
1c583 6f 20 62 65 20 61 70 70 6c 69 65 64 20 2a 2f 0a o be applied */.
1c584 20 20 20 20 20 20 4d 65 6d 20 2a 70 44 61 74 61 Mem *pData
1c585 30 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 0; /* Firs
1c586 74 20 72 65 67 69 73 74 65 72 20 74 6f 20 77 68 t register to wh
1c587 69 63 68 20 74 6f 20 61 70 70 6c 79 20 61 66 66 ich to apply aff
1c588 69 6e 69 74 79 20 2a 2f 0a 20 20 20 20 20 20 4d inity */. M
1c589 65 6d 20 2a 70 4c 61 73 74 3b 20 20 20 20 20 20 em *pLast;
1c58a 20 20 2f 2a 20 4c 61 73 74 20 72 65 67 69 73 74 /* Last regist
1c58b 65 72 20 74 6f 20 77 68 69 63 68 20 74 6f 20 61 er to which to a
1c58c 70 70 6c 79 20 61 66 66 69 6e 69 74 79 20 2a 2f pply affinity */
1c58d 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 52 65 63 . Mem *pRec
1c58e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 ; /* Cur
1c58f 72 65 6e 74 20 72 65 67 69 73 74 65 72 20 2a 2f rent register */
1c590 0a 20 20 20 20 7d 20 61 6e 3b 0a 20 20 20 20 73 . } an;. s
1c591 74 72 75 63 74 20 4f 50 5f 4d 61 6b 65 52 65 63 truct OP_MakeRec
1c592 6f 72 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b ord_stack_vars {
1c593 0a 20 20 20 20 20 20 75 38 20 2a 7a 4e 65 77 52 . u8 *zNewR
1c594 65 63 6f 72 64 3b 20 20 20 20 20 20 20 20 2f 2a ecord; /*
1c595 20 41 20 62 75 66 66 65 72 20 74 6f 20 68 6f 6c A buffer to hol
1c596 64 20 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 d the data for t
1c597 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f he new record */
1c598 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 52 65 63 . Mem *pRec
1c599 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
1c59a 20 54 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20 The new record
1c59b 2a 2f 0a 20 20 20 20 20 20 75 36 34 20 6e 44 61 */. u64 nDa
1c59c 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ta;
1c59d 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 /* Number of byt
1c59e 65 73 20 6f 66 20 64 61 74 61 20 73 70 61 63 65 es of data space
1c59f 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 48 */. int nH
1c5a0 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 dr;
1c5a1 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
1c5a2 74 65 73 20 6f 66 20 68 65 61 64 65 72 20 73 70 tes of header sp
1c5a3 61 63 65 20 2a 2f 0a 20 20 20 20 20 20 69 36 34 ace */. i64
1c5a4 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 nByte;
1c5a5 20 20 20 20 2f 2a 20 44 61 74 61 20 73 70 61 63 /* Data spac
1c5a6 65 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 74 e required for t
1c5a7 68 69 73 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 his record */.
1c5a8 20 20 20 20 69 6e 74 20 6e 5a 65 72 6f 3b 20 20 int nZero;
1c5a9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
1c5aa 6d 62 65 72 20 6f 66 20 7a 65 72 6f 20 62 79 74 mber of zero byt
1c5ab 65 73 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 es at the end of
1c5ac 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 the record */.
1c5ad 20 20 20 20 20 69 6e 74 20 6e 56 61 72 69 6e 74 int nVarint
1c5ae 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e ; /* N
1c5af 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 umber of bytes i
1c5b0 6e 20 61 20 76 61 72 69 6e 74 20 2a 2f 0a 20 20 n a varint */.
1c5b1 20 20 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74 u32 serial_t
1c5b2 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 79 ype; /* Ty
1c5b3 70 65 20 66 69 65 6c 64 20 2a 2f 0a 20 20 20 20 pe field */.
1c5b4 20 20 4d 65 6d 20 2a 70 44 61 74 61 30 3b 20 20 Mem *pData0;
1c5b5 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 /* Firs
1c5b6 74 20 66 69 65 6c 64 20 74 6f 20 62 65 20 63 6f t field to be co
1c5b7 6d 62 69 6e 65 64 20 69 6e 74 6f 20 74 68 65 20 mbined into the
1c5b8 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 record */.
1c5b9 4d 65 6d 20 2a 70 4c 61 73 74 3b 20 20 20 20 20 Mem *pLast;
1c5ba 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 66 /* Last f
1c5bb 69 65 6c 64 20 6f 66 20 74 68 65 20 72 65 63 6f ield of the reco
1c5bc 72 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 rd */. int
1c5bd 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20 20 nField;
1c5be 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1c5bf 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 72 65 fields in the re
1c5c0 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 63 68 cord */. ch
1c5c1 61 72 20 2a 7a 41 66 66 69 6e 69 74 79 3b 20 20 ar *zAffinity;
1c5c2 20 20 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 /* The affi
1c5c3 6e 69 74 79 20 73 74 72 69 6e 67 20 66 6f 72 20 nity string for
1c5c4 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 the record */.
1c5c5 20 20 20 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72 int file_for
1c5c6 6d 61 74 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 mat; /* Fi
1c5c7 6c 65 20 66 6f 72 6d 61 74 20 74 6f 20 75 73 65 le format to use
1c5c8 20 66 6f 72 20 65 6e 63 6f 64 69 6e 67 20 2a 2f for encoding */
1c5c9 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 20 20 20 . int i;
1c5ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1c5cb 20 53 70 61 63 65 20 75 73 65 64 20 69 6e 20 7a Space used in z
1c5cc 4e 65 77 52 65 63 6f 72 64 5b 5d 20 2a 2f 0a 20 NewRecord[] */.
1c5cd 20 20 20 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20 int len;
1c5ce 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
1c5cf 65 6e 67 74 68 20 6f 66 20 61 20 66 69 65 6c 64 ength of a field
1c5d0 20 2a 2f 0a 20 20 20 20 7d 20 61 6f 3b 0a 20 20 */. } ao;.
1c5d1 20 20 73 74 72 75 63 74 20 4f 50 5f 43 6f 75 6e struct OP_Coun
1c5d2 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 t_stack_vars {.
1c5d3 20 20 20 20 20 69 36 34 20 6e 45 6e 74 72 79 3b i64 nEntry;
1c5d4 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 20 . BtCursor
1c5d5 2a 70 43 72 73 72 3b 0a 20 20 20 20 7d 20 61 70 *pCrsr;. } ap
1c5d6 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f ;. struct OP_
1c5d7 53 61 76 65 70 6f 69 6e 74 5f 73 74 61 63 6b 5f Savepoint_stack_
1c5d8 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 vars {. int
1c5d9 20 70 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 p1;
1c5da 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1c5db 56 61 6c 75 65 20 6f 66 20 50 31 20 6f 70 65 72 Value of P1 oper
1c5dc 61 6e 64 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 and */. cha
1c5dd 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 r *zName;
1c5de 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1c5df 4e 61 6d 65 20 6f 66 20 73 61 76 65 70 6f 69 6e Name of savepoin
1c5e0 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e t */. int n
1c5e1 4e 61 6d 65 3b 0a 20 20 20 20 20 20 53 61 76 65 Name;. Save
1c5e2 70 6f 69 6e 74 20 2a 70 4e 65 77 3b 0a 20 20 20 point *pNew;.
1c5e3 20 20 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 Savepoint *pS
1c5e4 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 20 20 20 avepoint;.
1c5e5 53 61 76 65 70 6f 69 6e 74 20 2a 70 54 6d 70 3b Savepoint *pTmp;
1c5e6 0a 20 20 20 20 20 20 69 6e 74 20 69 53 61 76 65 . int iSave
1c5e7 70 6f 69 6e 74 3b 0a 20 20 20 20 20 20 69 6e 74 point;. int
1c5e8 20 69 69 3b 0a 20 20 20 20 7d 20 61 71 3b 0a 20 ii;. } aq;.
1c5e9 20 20 20 73 74 72 75 63 74 20 4f 50 5f 41 75 74 struct OP_Aut
1c5ea 6f 43 6f 6d 6d 69 74 5f 73 74 61 63 6b 5f 76 61 oCommit_stack_va
1c5eb 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64 rs {. int d
1c5ec 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 esiredAutoCommit
1c5ed 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 52 6f 6c ;. int iRol
1c5ee 6c 62 61 63 6b 3b 0a 20 20 20 20 20 20 69 6e 74 lback;. int
1c5ef 20 74 75 72 6e 4f 6e 41 43 3b 0a 20 20 20 20 7d turnOnAC;. }
1c5f0 20 61 72 3b 0a 20 20 20 20 73 74 72 75 63 74 20 ar;. struct
1c5f1 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 5f 73 OP_Transaction_s
1c5f2 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c5f3 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 20 20 Btree *pBt;.
1c5f4 20 20 7d 20 61 73 3b 0a 20 20 20 20 73 74 72 75 } as;. stru
1c5f5 63 74 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 ct OP_ReadCookie
1c5f6 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 _stack_vars {.
1c5f7 20 20 20 20 69 6e 74 20 69 4d 65 74 61 3b 0a 20 int iMeta;.
1c5f8 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 int iDb;.
1c5f9 20 20 20 20 69 6e 74 20 69 43 6f 6f 6b 69 65 3b int iCookie;
1c5fa 0a 20 20 20 20 7d 20 61 74 3b 0a 20 20 20 20 73 . } at;. s
1c5fb 74 72 75 63 74 20 4f 50 5f 53 65 74 43 6f 6f 6b truct OP_SetCook
1c5fc 69 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a ie_stack_vars {.
1c5fd 20 20 20 20 20 20 44 62 20 2a 70 44 62 3b 0a 20 Db *pDb;.
1c5fe 20 20 20 7d 20 61 75 3b 0a 20 20 20 20 73 74 72 } au;. str
1c5ff 75 63 74 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f uct OP_VerifyCoo
1c600 6b 69 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b kie_stack_vars {
1c601 0a 20 20 20 20 20 20 69 6e 74 20 69 4d 65 74 61 . int iMeta
1c602 3b 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 ;. Btree *p
1c603 42 74 3b 0a 20 20 20 20 7d 20 61 76 3b 0a 20 20 Bt;. } av;.
1c604 20 20 73 74 72 75 63 74 20 4f 50 5f 4f 70 65 6e struct OP_Open
1c605 57 72 69 74 65 5f 73 74 61 63 6b 5f 76 61 72 73 Write_stack_vars
1c606 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 46 69 {. int nFi
1c607 65 6c 64 3b 0a 20 20 20 20 20 20 4b 65 79 49 6e eld;. KeyIn
1c608 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 fo *pKeyInfo;.
1c609 20 20 20 20 69 6e 74 20 70 32 3b 0a 20 20 20 20 int p2;.
1c60a 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 20 int iDb;.
1c60b 20 69 6e 74 20 77 72 46 6c 61 67 3b 0a 20 20 20 int wrFlag;.
1c60c 20 20 20 42 74 72 65 65 20 2a 70 58 3b 0a 20 20 Btree *pX;.
1c60d 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a VdbeCursor *
1c60e 70 43 75 72 3b 0a 20 20 20 20 20 20 44 62 20 2a pCur;. Db *
1c60f 70 44 62 3b 0a 20 20 20 20 7d 20 61 77 3b 0a 20 pDb;. } aw;.
1c610 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4f 70 65 struct OP_Ope
1c611 6e 45 70 68 65 6d 65 72 61 6c 5f 73 74 61 63 6b nEphemeral_stack
1c612 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 _vars {. Vd
1c613 62 65 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a 20 beCursor *pCx;.
1c614 20 20 20 7d 20 61 78 3b 0a 20 20 20 20 73 74 72 } ax;. str
1c615 75 63 74 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64 uct OP_OpenPseud
1c616 6f 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 o_stack_vars {.
1c617 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 VdbeCursor
1c618 2a 70 43 78 3b 0a 20 20 20 20 7d 20 61 79 3b 0a *pCx;. } ay;.
1c619 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 53 65 struct OP_Se
1c61a 65 6b 47 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 ekGt_stack_vars
1c61b 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b {. int res;
1c61c 0a 20 20 20 20 20 20 69 6e 74 20 6f 63 3b 0a 20 . int oc;.
1c61d 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 VdbeCursor
1c61e 2a 70 43 3b 0a 20 20 20 20 20 20 55 6e 70 61 63 *pC;. Unpac
1c61f 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a 20 20 20 kedRecord r;.
1c620 20 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 0a 20 int nField;.
1c621 20 20 20 20 20 69 36 34 20 69 4b 65 79 3b 20 20 i64 iKey;
1c622 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 69 64 /* The rowid
1c623 20 77 65 20 61 72 65 20 74 6f 20 73 65 65 6b 20 we are to seek
1c624 74 6f 20 2a 2f 0a 20 20 20 20 7d 20 61 7a 3b 0a to */. } az;.
1c625 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 53 65 struct OP_Se
1c626 65 6b 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a ek_stack_vars {.
1c627 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 VdbeCursor
1c628 20 2a 70 43 3b 0a 20 20 20 20 7d 20 62 61 3b 0a *pC;. } ba;.
1c629 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 46 6f struct OP_Fo
1c62a 75 6e 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b und_stack_vars {
1c62b 0a 20 20 20 20 20 20 69 6e 74 20 61 6c 72 65 61 . int alrea
1c62c 64 79 45 78 69 73 74 73 3b 0a 20 20 20 20 20 20 dyExists;.
1c62d 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a VdbeCursor *pC;.
1c62e 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 int res;.
1c62f 20 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 63 UnpackedRec
1c630 6f 72 64 20 2a 70 49 64 78 4b 65 79 3b 0a 20 20 ord *pIdxKey;.
1c631 20 20 20 20 63 68 61 72 20 61 54 65 6d 70 52 65 char aTempRe
1c632 63 5b 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 c[ROUND8(sizeof(
1c633 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 29 29 UnpackedRecord))
1c634 20 2b 20 73 69 7a 65 6f 66 28 4d 65 6d 29 2a 33 + sizeof(Mem)*3
1c635 20 2b 20 37 5d 3b 0a 20 20 20 20 7d 20 62 62 3b + 7];. } bb;
1c636 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49 . struct OP_I
1c637 73 55 6e 69 71 75 65 5f 73 74 61 63 6b 5f 76 61 sUnique_stack_va
1c638 72 73 20 7b 0a 20 20 20 20 20 20 75 31 36 20 69 rs {. u16 i
1c639 69 3b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 i;. VdbeCur
1c63a 73 6f 72 20 2a 70 43 78 3b 0a 20 20 20 20 20 20 sor *pCx;.
1c63b 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b BtCursor *pCrsr;
1c63c 0a 20 20 20 20 20 20 75 31 36 20 6e 46 69 65 6c . u16 nFiel
1c63d 64 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 61 4d d;. Mem *aM
1c63e 65 6d 3b 0a 20 20 20 20 20 20 55 6e 70 61 63 6b em;. Unpack
1c63f 65 64 52 65 63 6f 72 64 20 72 3b 20 20 20 20 20 edRecord r;
1c640 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1c641 42 2d 54 72 65 65 20 69 6e 64 65 78 20 73 65 61 B-Tree index sea
1c642 72 63 68 20 6b 65 79 20 2a 2f 0a 20 20 20 20 20 rch key */.
1c643 20 69 36 34 20 52 3b 20 20 20 20 20 20 20 20 20 i64 R;
1c644 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c645 20 20 20 20 2f 2a 20 52 6f 77 69 64 20 73 74 6f /* Rowid sto
1c646 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 red in register
1c647 50 33 20 2a 2f 0a 20 20 20 20 7d 20 62 63 3b 0a P3 */. } bc;.
1c648 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4e 6f struct OP_No
1c649 74 45 78 69 73 74 73 5f 73 74 61 63 6b 5f 76 61 tExists_stack_va
1c64a 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 rs {. VdbeC
1c64b 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 ursor *pC;.
1c64c 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 BtCursor *pCrsr
1c64d 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b ;. int res;
1c64e 0a 20 20 20 20 20 20 75 36 34 20 69 4b 65 79 3b . u64 iKey;
1c64f 0a 20 20 20 20 7d 20 62 64 3b 0a 20 20 20 20 73 . } bd;. s
1c650 74 72 75 63 74 20 4f 50 5f 4e 65 77 52 6f 77 69 truct OP_NewRowi
1c651 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 d_stack_vars {.
1c652 20 20 20 20 20 69 36 34 20 76 3b 20 20 20 20 20 i64 v;
1c653 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1c654 68 65 20 6e 65 77 20 72 6f 77 69 64 20 2a 2f 0a he new rowid */.
1c655 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 VdbeCursor
1c656 20 2a 70 43 3b 20 20 20 20 20 20 20 20 2f 2a 20 *pC; /*
1c657 43 75 72 73 6f 72 20 6f 66 20 74 61 62 6c 65 20 Cursor of table
1c658 74 6f 20 67 65 74 20 74 68 65 20 6e 65 77 20 72 to get the new r
1c659 6f 77 69 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e owid */. in
1c65a 74 20 72 65 73 3b 20 20 20 20 20 20 20 20 20 20 t res;
1c65b 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f /* Result o
1c65c 66 20 61 6e 20 73 71 6c 69 74 65 33 42 74 72 65 f an sqlite3Btre
1c65d 65 4c 61 73 74 28 29 20 2a 2f 0a 20 20 20 20 20 eLast() */.
1c65e 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 20 20 20 int cnt;
1c65f 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 /* Count
1c660 65 72 20 74 6f 20 6c 69 6d 69 74 20 74 68 65 20 er to limit the
1c661 6e 75 6d 62 65 72 20 6f 66 20 73 65 61 72 63 68 number of search
1c662 65 73 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 es */. Mem
1c663 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 *pMem;
1c664 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 /* Register h
1c665 6f 6c 64 69 6e 67 20 6c 61 72 67 65 73 74 20 72 olding largest r
1c666 6f 77 69 64 20 66 6f 72 20 41 55 54 4f 49 4e 43 owid for AUTOINC
1c667 52 45 4d 45 4e 54 20 2a 2f 0a 20 20 20 20 20 20 REMENT */.
1c668 56 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d VdbeFrame *pFram
1c669 65 3b 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 66 e; /* Root f
1c66a 72 61 6d 65 20 6f 66 20 56 44 42 45 20 2a 2f 0a rame of VDBE */.
1c66b 20 20 20 20 7d 20 62 65 3b 0a 20 20 20 20 73 74 } be;. st
1c66c 72 75 63 74 20 4f 50 5f 49 6e 73 65 72 74 49 6e ruct OP_InsertIn
1c66d 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 t_stack_vars {.
1c66e 20 20 20 20 20 4d 65 6d 20 2a 70 44 61 74 61 3b Mem *pData;
1c66f 20 20 20 20 20 20 20 2f 2a 20 4d 45 4d 20 63 65 /* MEM ce
1c670 6c 6c 20 68 6f 6c 64 69 6e 67 20 64 61 74 61 20 ll holding data
1c671 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 74 for the record t
1c672 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f o be inserted */
1c673 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 4b 65 79 . Mem *pKey
1c674 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 45 4d 20 ; /* MEM
1c675 63 65 6c 6c 20 68 6f 6c 64 69 6e 67 20 6b 65 79 cell holding key
1c676 20 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64 for the record
1c677 20 2a 2f 0a 20 20 20 20 20 20 69 36 34 20 69 4b */. i64 iK
1c678 65 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ey; /* T
1c679 68 65 20 69 6e 74 65 67 65 72 20 52 4f 57 49 44 he integer ROWID
1c67a 20 6f 72 20 6b 65 79 20 66 6f 72 20 74 68 65 20 or key for the
1c67b 72 65 63 6f 72 64 20 74 6f 20 62 65 20 69 6e 73 record to be ins
1c67c 65 72 74 65 64 20 2a 2f 0a 20 20 20 20 20 20 56 erted */. V
1c67d 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 20 20 dbeCursor *pC;
1c67e 20 2f 2a 20 43 75 72 73 6f 72 20 74 6f 20 74 61 /* Cursor to ta
1c67f 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 69 ble into which i
1c680 6e 73 65 72 74 20 69 73 20 77 72 69 74 74 65 6e nsert is written
1c681 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 5a */. int nZ
1c682 65 72 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e ero; /* N
1c683 75 6d 62 65 72 20 6f 66 20 7a 65 72 6f 2d 62 79 umber of zero-by
1c684 74 65 73 20 74 6f 20 61 70 70 65 6e 64 20 2a 2f tes to append */
1c685 0a 20 20 20 20 20 20 69 6e 74 20 73 65 65 6b 52 . int seekR
1c686 65 73 75 6c 74 3b 20 20 20 2f 2a 20 52 65 73 75 esult; /* Resu
1c687 6c 74 20 6f 66 20 70 72 69 6f 72 20 73 65 65 6b lt of prior seek
1c688 20 6f 72 20 30 20 69 66 20 6e 6f 20 55 53 45 53 or 0 if no USES
1c689 45 45 4b 52 45 53 55 4c 54 20 66 6c 61 67 20 2a EEKRESULT flag *
1c68a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 /. const ch
1c68b 61 72 20 2a 7a 44 62 3b 20 20 2f 2a 20 64 61 74 ar *zDb; /* dat
1c68c 61 62 61 73 65 20 6e 61 6d 65 20 2d 20 75 73 65 abase name - use
1c68d 64 20 62 79 20 74 68 65 20 75 70 64 61 74 65 20 d by the update
1c68e 68 6f 6f 6b 20 2a 2f 0a 20 20 20 20 20 20 63 6f hook */. co
1c68f 6e 73 74 20 63 68 61 72 20 2a 7a 54 62 6c 3b 20 nst char *zTbl;
1c690 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65 20 2d 20 /* Table name -
1c691 75 73 65 64 20 62 79 20 74 68 65 20 6f 70 64 61 used by the opda
1c692 74 65 20 68 6f 6f 6b 20 2a 2f 0a 20 20 20 20 20 te hook */.
1c693 20 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 20 int op;
1c694 20 20 20 2f 2a 20 4f 70 63 6f 64 65 20 66 6f 72 /* Opcode for
1c695 20 75 70 64 61 74 65 20 68 6f 6f 6b 3a 20 53 51 update hook: SQ
1c696 4c 49 54 45 5f 55 50 44 41 54 45 20 6f 72 20 53 LITE_UPDATE or S
1c697 51 4c 49 54 45 5f 49 4e 53 45 52 54 20 2a 2f 0a QLITE_INSERT */.
1c698 20 20 20 20 7d 20 62 66 3b 0a 20 20 20 20 73 74 } bf;. st
1c699 72 75 63 74 20 4f 50 5f 44 65 6c 65 74 65 5f 73 ruct OP_Delete_s
1c69a 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c69b 20 20 69 36 34 20 69 4b 65 79 3b 0a 20 20 20 20 i64 iKey;.
1c69c 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1c69d 3b 0a 20 20 20 20 7d 20 62 67 3b 0a 20 20 20 20 ;. } bg;.
1c69e 73 74 72 75 63 74 20 4f 50 5f 52 6f 77 44 61 74 struct OP_RowDat
1c69f 61 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 a_stack_vars {.
1c6a0 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 VdbeCursor
1c6a1 2a 70 43 3b 0a 20 20 20 20 20 20 42 74 43 75 72 *pC;. BtCur
1c6a2 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 20 20 sor *pCrsr;.
1c6a3 20 20 75 33 32 20 6e 3b 0a 20 20 20 20 20 20 69 u32 n;. i
1c6a4 36 34 20 6e 36 34 3b 0a 20 20 20 20 7d 20 62 68 64 n64;. } bh
1c6a5 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f ;. struct OP_
1c6a6 52 6f 77 69 64 5f 73 74 61 63 6b 5f 76 61 72 73 Rowid_stack_vars
1c6a7 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 {. VdbeCur
1c6a8 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 69 sor *pC;. i
1c6a9 36 34 20 76 3b 0a 20 20 20 20 20 20 73 71 6c 69 64 v;. sqli
1c6aa 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b te3_vtab *pVtab;
1c6ab 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 73 71 6c . const sql
1c6ac 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f ite3_module *pMo
1c6ad 64 75 6c 65 3b 0a 20 20 20 20 7d 20 62 69 3b 0a dule;. } bi;.
1c6ae 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4e 75 struct OP_Nu
1c6af 6c 6c 52 6f 77 5f 73 74 61 63 6b 5f 76 61 72 73 llRow_stack_vars
1c6b0 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 {. VdbeCur
1c6b1 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 7d 20 62 sor *pC;. } b
1c6b2 6a 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 j;. struct OP
1c6b3 5f 4c 61 73 74 5f 73 74 61 63 6b 5f 76 61 72 73 _Last_stack_vars
1c6b4 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 {. VdbeCur
1c6b5 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 42 sor *pC;. B
1c6b6 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a tCursor *pCrsr;.
1c6b7 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 int res;.
1c6b8 20 20 20 7d 20 62 6b 3b 0a 20 20 20 20 73 74 72 } bk;. str
1c6b9 75 63 74 20 4f 50 5f 52 65 77 69 6e 64 5f 73 74 uct OP_Rewind_st
1c6ba 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 ack_vars {.
1c6bb 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b VdbeCursor *pC;
1c6bc 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 20 . BtCursor
1c6bd 2a 70 43 72 73 72 3b 0a 20 20 20 20 20 20 69 6e *pCrsr;. in
1c6be 74 20 72 65 73 3b 0a 20 20 20 20 7d 20 62 6c 3b t res;. } bl;
1c6bf 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4e . struct OP_N
1c6c0 65 78 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b ext_stack_vars {
1c6c1 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f . VdbeCurso
1c6c2 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 42 74 43 r *pC;. BtC
1c6c3 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 ursor *pCrsr;.
1c6c4 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 int res;.
1c6c5 20 7d 20 62 6d 3b 0a 20 20 20 20 73 74 72 75 63 } bm;. struc
1c6c6 74 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 5f 73 t OP_IdxInsert_s
1c6c7 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c6c8 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1c6c9 3b 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 ;. BtCursor
1c6ca 20 2a 70 43 72 73 72 3b 0a 20 20 20 20 20 20 69 *pCrsr;. i
1c6cb 6e 74 20 6e 4b 65 79 3b 0a 20 20 20 20 20 20 63 nt nKey;. c
1c6cc 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4b 65 79 3b onst char *zKey;
1c6cd 0a 20 20 20 20 7d 20 62 6e 3b 0a 20 20 20 20 73 . } bn;. s
1c6ce 74 72 75 63 74 20 4f 50 5f 49 64 78 44 65 6c 65 truct OP_IdxDele
1c6cf 74 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a te_stack_vars {.
1c6d0 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 VdbeCursor
1c6d1 20 2a 70 43 3b 0a 20 20 20 20 20 20 42 74 43 75 *pC;. BtCu
1c6d2 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 20 rsor *pCrsr;.
1c6d3 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 int res;.
1c6d4 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 UnpackedRecord
1c6d5 20 72 3b 0a 20 20 20 20 7d 20 62 6f 3b 0a 20 20 r;. } bo;.
1c6d6 20 20 73 74 72 75 63 74 20 4f 50 5f 49 64 78 52 struct OP_IdxR
1c6d7 6f 77 69 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 owid_stack_vars
1c6d8 7b 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 {. BtCursor
1c6d9 20 2a 70 43 72 73 72 3b 0a 20 20 20 20 20 20 56 *pCrsr;. V
1c6da 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 dbeCursor *pC;.
1c6db 20 20 20 20 20 69 36 34 20 72 6f 77 69 64 3b 0a i64 rowid;.
1c6dc 20 20 20 20 7d 20 62 70 3b 0a 20 20 20 20 73 74 } bp;. st
1c6dd 72 75 63 74 20 4f 50 5f 49 64 78 47 45 5f 73 74 ruct OP_IdxGE_st
1c6de 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 ack_vars {.
1c6df 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b VdbeCursor *pC;
1c6e0 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a . int res;.
1c6e1 20 20 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 UnpackedRe
1c6e2 63 6f 72 64 20 72 3b 0a 20 20 20 20 7d 20 62 71 cord r;. } bq
1c6e3 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f ;. struct OP_
1c6e4 44 65 73 74 72 6f 79 5f 73 74 61 63 6b 5f 76 61 Destroy_stack_va
1c6e5 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 rs {. int i
1c6e6 4d 6f 76 65 64 3b 0a 20 20 20 20 20 20 69 6e 74 Moved;. int
1c6e7 20 69 43 6e 74 3b 0a 20 20 20 20 20 20 56 64 62 iCnt;. Vdb
1c6e8 65 20 2a 70 56 64 62 65 3b 0a 20 20 20 20 20 20 e *pVdbe;.
1c6e9 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 7d 20 62 int iDb;. } b
1c6ea 72 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 r;. struct OP
1c6eb 5f 43 6c 65 61 72 5f 73 74 61 63 6b 5f 76 61 72 _Clear_stack_var
1c6ec 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 43 s {. int nC
1c6ed 68 61 6e 67 65 3b 0a 20 20 20 20 7d 20 62 73 3b hange;. } bs;
1c6ee 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 43 . struct OP_C
1c6ef 72 65 61 74 65 54 61 62 6c 65 5f 73 74 61 63 6b reateTable_stack
1c6f0 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e _vars {. in
1c6f1 74 20 70 67 6e 6f 3b 0a 20 20 20 20 20 20 69 6e t pgno;. in
1c6f2 74 20 66 6c 61 67 73 3b 0a 20 20 20 20 20 20 44 t flags;. D
1c6f3 62 20 2a 70 44 62 3b 0a 20 20 20 20 7d 20 62 74 b *pDb;. } bt
1c6f4 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f ;. struct OP_
1c6f5 50 61 72 73 65 53 63 68 65 6d 61 5f 73 74 61 63 ParseSchema_stac
1c6f6 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 k_vars {. i
1c6f7 6e 74 20 69 44 62 3b 0a 20 20 20 20 20 20 63 6f nt iDb;. co
1c6f8 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 nst char *zMaste
1c6f9 72 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a r;. char *z
1c6fa 53 71 6c 3b 0a 20 20 20 20 20 20 49 6e 69 74 44 Sql;. InitD
1c6fb 61 74 61 20 69 6e 69 74 44 61 74 61 3b 0a 20 20 ata initData;.
1c6fc 20 20 7d 20 62 75 3b 0a 20 20 20 20 73 74 72 75 } bu;. stru
1c6fd 63 74 20 4f 50 5f 49 6e 74 65 67 72 69 74 79 43 ct OP_IntegrityC
1c6fe 6b 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 k_stack_vars {.
1c6ff 20 20 20 20 20 69 6e 74 20 6e 52 6f 6f 74 3b 20 int nRoot;
1c700 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1c701 66 20 74 61 62 6c 65 73 20 74 6f 20 63 68 65 63 f tables to chec
1c702 6b 2e 20 20 28 4e 75 6d 62 65 72 20 6f 66 20 72 k. (Number of r
1c703 6f 6f 74 20 70 61 67 65 73 2e 29 20 2a 2f 0a 20 oot pages.) */.
1c704 20 20 20 20 20 69 6e 74 20 2a 61 52 6f 6f 74 3b int *aRoot;
1c705 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 /* Array of
1c706 20 72 6f 6f 74 70 61 67 65 20 6e 75 6d 62 65 72 rootpage number
1c707 73 20 66 6f 72 20 74 61 62 6c 65 73 20 74 6f 20 s for tables to
1c708 62 65 20 63 68 65 63 6b 65 64 20 2a 2f 0a 20 20 be checked */.
1c709 20 20 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 20 int j;
1c70a 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e /* Loop coun
1c70b 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 ter */. int
1c70c 20 6e 45 72 72 3b 20 20 20 20 20 20 20 2f 2a 20 nErr; /*
1c70d 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 Number of errors
1c70e 20 72 65 70 6f 72 74 65 64 20 2a 2f 0a 20 20 20 reported */.
1c70f 20 20 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 char *z;
1c710 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 /* Text of th
1c711 65 20 65 72 72 6f 72 20 72 65 70 6f 72 74 20 2a e error report *
1c712 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 6e 45 /. Mem *pnE
1c713 72 72 3b 20 20 20 20 20 2f 2a 20 52 65 67 69 73 rr; /* Regis
1c714 74 65 72 20 6b 65 65 70 69 6e 67 20 74 72 61 63 ter keeping trac
1c715 6b 20 6f 66 20 65 72 72 6f 72 73 20 72 65 6d 61 k of errors rema
1c716 69 6e 69 6e 67 20 2a 2f 0a 20 20 20 20 7d 20 62 ining */. } b
1c717 76 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 v;. struct OP
1c718 5f 52 6f 77 53 65 74 41 64 64 5f 73 74 61 63 6b _RowSetAdd_stack
1c719 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 4d 65 _vars {. Me
1c71a 6d 20 2a 70 49 64 78 3b 0a 20 20 20 20 20 20 4d m *pIdx;. M
1c71b 65 6d 20 2a 70 56 61 6c 3b 0a 20 20 20 20 7d 20 em *pVal;. }
1c71c 62 77 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f bw;. struct O
1c71d 50 5f 52 6f 77 53 65 74 52 65 61 64 5f 73 74 61 P_RowSetRead_sta
1c71e 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c71f 4d 65 6d 20 2a 70 49 64 78 3b 0a 20 20 20 20 20 Mem *pIdx;.
1c720 20 69 36 34 20 76 61 6c 3b 0a 20 20 20 20 7d 20 i64 val;. }
1c721 62 78 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f bx;. struct O
1c722 50 5f 52 6f 77 53 65 74 54 65 73 74 5f 73 74 61 P_RowSetTest_sta
1c723 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c724 69 6e 74 20 69 53 65 74 3b 0a 20 20 20 20 20 20 int iSet;.
1c725 69 6e 74 20 65 78 69 73 74 73 3b 0a 20 20 20 20 int exists;.
1c726 7d 20 62 79 3b 0a 20 20 20 20 73 74 72 75 63 74 } by;. struct
1c727 20 4f 50 5f 50 72 6f 67 72 61 6d 5f 73 74 61 63 OP_Program_stac
1c728 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 k_vars {. i
1c729 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 nt nMem;
1c72a 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1c72b 20 6f 66 20 6d 65 6d 6f 72 79 20 72 65 67 69 73 of memory regis
1c72c 74 65 72 73 20 66 6f 72 20 73 75 62 2d 70 72 6f ters for sub-pro
1c72d 67 72 61 6d 20 2a 2f 0a 20 20 20 20 20 20 69 6e gram */. in
1c72e 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 t nByte;
1c72f 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f /* Bytes o
1c730 66 20 72 75 6e 74 69 6d 65 20 73 70 61 63 65 20 f runtime space
1c731 72 65 71 75 69 72 65 64 20 66 6f 72 20 73 75 62 required for sub
1c732 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 20 20 -program */.
1c733 20 20 4d 65 6d 20 2a 70 52 74 3b 20 20 20 20 20 Mem *pRt;
1c734 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 /* Reg
1c735 69 73 74 65 72 20 74 6f 20 61 6c 6c 6f 63 61 74 ister to allocat
1c736 65 20 72 75 6e 74 69 6d 65 20 73 70 61 63 65 20 e runtime space
1c737 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 4d */. Mem *pM
1c738 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 em;
1c739 20 2f 2a 20 55 73 65 64 20 74 6f 20 69 74 65 72 /* Used to iter
1c73a 61 74 65 20 74 68 72 6f 75 67 68 20 6d 65 6d 6f ate through memo
1c73b 72 79 20 63 65 6c 6c 73 20 2a 2f 0a 20 20 20 20 ry cells */.
1c73c 20 20 4d 65 6d 20 2a 70 45 6e 64 3b 20 20 20 20 Mem *pEnd;
1c73d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 /* Las
1c73e 74 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 6e t memory cell in
1c73f 20 6e 65 77 20 61 72 72 61 79 20 2a 2f 0a 20 20 new array */.
1c740 20 20 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 VdbeFrame *p
1c741 46 72 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20 4e Frame; /* N
1c742 65 77 20 76 64 62 65 20 66 72 61 6d 65 20 74 6f ew vdbe frame to
1c743 20 65 78 65 63 75 74 65 20 69 6e 20 2a 2f 0a 20 execute in */.
1c744 20 20 20 20 20 53 75 62 50 72 6f 67 72 61 6d 20 SubProgram
1c745 2a 70 50 72 6f 67 72 61 6d 3b 20 20 20 2f 2a 20 *pProgram; /*
1c746 53 75 62 2d 70 72 6f 67 72 61 6d 20 74 6f 20 65 Sub-program to e
1c747 78 65 63 75 74 65 20 2a 2f 0a 20 20 20 20 20 20 xecute */.
1c748 76 6f 69 64 20 2a 74 3b 20 20 20 20 20 20 20 20 void *t;
1c749 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e /* Token
1c74a 20 69 64 65 6e 74 69 66 79 69 6e 67 20 74 72 69 identifying tri
1c74b 67 67 65 72 20 2a 2f 0a 20 20 20 20 7d 20 62 7a gger */. } bz
1c74c 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f ;. struct OP_
1c74d 50 61 72 61 6d 5f 73 74 61 63 6b 5f 76 61 72 73 Param_stack_vars
1c74e 20 7b 0a 20 20 20 20 20 20 56 64 62 65 46 72 61 {. VdbeFra
1c74f 6d 65 20 2a 70 46 72 61 6d 65 3b 0a 20 20 20 20 me *pFrame;.
1c750 20 20 4d 65 6d 20 2a 70 49 6e 3b 0a 20 20 20 20 Mem *pIn;.
1c751 7d 20 63 61 3b 0a 20 20 20 20 73 74 72 75 63 74 } ca;. struct
1c752 20 4f 50 5f 4d 65 6d 4d 61 78 5f 73 74 61 63 6b OP_MemMax_stack
1c753 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 4d 65 _vars {. Me
1c754 6d 20 2a 70 49 6e 31 3b 0a 20 20 20 20 20 20 56 m *pIn1;. V
1c755 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 dbeFrame *pFrame
1c756 3b 0a 20 20 20 20 7d 20 63 62 3b 0a 20 20 20 20 ;. } cb;.
1c757 73 74 72 75 63 74 20 4f 50 5f 41 67 67 53 74 65 struct OP_AggSte
1c758 70 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 p_stack_vars {.
1c759 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 int n;.
1c75a 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 4d int i;. M
1c75b 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 20 20 20 20 em *pMem;.
1c75c 4d 65 6d 20 2a 70 52 65 63 3b 0a 20 20 20 20 20 Mem *pRec;.
1c75d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
1c75e 20 63 74 78 3b 0a 20 20 20 20 20 20 73 71 6c 69 ctx;. sqli
1c75f 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61 te3_value **apVa
1c760 6c 3b 0a 20 20 20 20 7d 20 63 63 3b 0a 20 20 20 l;. } cc;.
1c761 20 73 74 72 75 63 74 20 4f 50 5f 41 67 67 46 69 struct OP_AggFi
1c762 6e 61 6c 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b nal_stack_vars {
1c763 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d . Mem *pMem
1c764 3b 0a 20 20 20 20 7d 20 63 64 3b 0a 20 20 20 20 ;. } cd;.
1c765 73 74 72 75 63 74 20 4f 50 5f 49 6e 63 72 56 61 struct OP_IncrVa
1c766 63 75 75 6d 5f 73 74 61 63 6b 5f 76 61 72 73 20 cuum_stack_vars
1c767 7b 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 {. Btree *p
1c768 42 74 3b 0a 20 20 20 20 7d 20 63 65 3b 0a 20 20 Bt;. } ce;.
1c769 20 20 73 74 72 75 63 74 20 4f 50 5f 56 42 65 67 struct OP_VBeg
1c76a 69 6e 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a in_stack_vars {.
1c76b 20 20 20 20 20 20 56 54 61 62 6c 65 20 2a 70 56 VTable *pV
1c76c 54 61 62 3b 0a 20 20 20 20 7d 20 63 66 3b 0a 20 Tab;. } cf;.
1c76d 20 20 20 73 74 72 75 63 74 20 4f 50 5f 56 4f 70 struct OP_VOp
1c76e 65 6e 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a en_stack_vars {.
1c76f 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 VdbeCursor
1c770 20 2a 70 43 75 72 3b 0a 20 20 20 20 20 20 73 71 *pCur;. sq
1c771 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f lite3_vtab_curso
1c772 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b 0a r *pVtabCursor;.
1c773 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 sqlite3_vt
1c774 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 20 20 20 ab *pVtab;.
1c775 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 sqlite3_module
1c776 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 7d 20 *pModule;. }
1c777 63 67 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f cg;. struct O
1c778 50 5f 56 46 69 6c 74 65 72 5f 73 74 61 63 6b 5f P_VFilter_stack_
1c779 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 vars {. int
1c77a 20 6e 41 72 67 3b 0a 20 20 20 20 20 20 69 6e 74 nArg;. int
1c77b 20 69 51 75 65 72 79 3b 0a 20 20 20 20 20 20 63 iQuery;. c
1c77c 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 onst sqlite3_mod
1c77d 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 ule *pModule;.
1c77e 20 20 20 20 4d 65 6d 20 2a 70 51 75 65 72 79 3b Mem *pQuery;
1c77f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 41 72 67 . Mem *pArg
1c780 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 c;. sqlite3
1c781 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 _vtab_cursor *pV
1c782 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 tabCursor;.
1c783 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 sqlite3_vtab *p
1c784 56 74 61 62 3b 0a 20 20 20 20 20 20 56 64 62 65 Vtab;. Vdbe
1c785 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 Cursor *pCur;.
1c786 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 int res;.
1c787 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 int i;.
1c788 4d 65 6d 20 2a 2a 61 70 41 72 67 3b 0a 20 20 20 Mem **apArg;.
1c789 20 7d 20 63 68 3b 0a 20 20 20 20 73 74 72 75 63 } ch;. struc
1c78a 74 20 4f 50 5f 56 43 6f 6c 75 6d 6e 5f 73 74 61 t OP_VColumn_sta
1c78b 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c78c 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 sqlite3_vtab *pV
1c78d 74 61 62 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 tab;. const
1c78e 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 sqlite3_module
1c78f 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 20 20 *pModule;.
1c790 4d 65 6d 20 2a 70 44 65 73 74 3b 0a 20 20 20 20 Mem *pDest;.
1c791 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
1c792 74 20 73 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 t sContext;.
1c793 7d 20 63 69 3b 0a 20 20 20 20 73 74 72 75 63 74 } ci;. struct
1c794 20 4f 50 5f 56 4e 65 78 74 5f 73 74 61 63 6b 5f OP_VNext_stack_
1c795 76 61 72 73 20 7b 0a 20 20 20 20 20 20 73 71 6c vars {. sql
1c796 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 ite3_vtab *pVtab
1c797 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 73 71 ;. const sq
1c798 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d lite3_module *pM
1c799 6f 64 75 6c 65 3b 0a 20 20 20 20 20 20 69 6e 74 odule;. int
1c79a 20 72 65 73 3b 0a 20 20 20 20 20 20 56 64 62 65 res;. Vdbe
1c79b 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 Cursor *pCur;.
1c79c 20 20 7d 20 63 6a 3b 0a 20 20 20 20 73 74 72 75 } cj;. stru
1c79d 63 74 20 4f 50 5f 56 52 65 6e 61 6d 65 5f 73 74 ct OP_VRename_st
1c79e 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 ack_vars {.
1c79f 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 sqlite3_vtab *p
1c7a0 56 74 61 62 3b 0a 20 20 20 20 20 20 4d 65 6d 20 Vtab;. Mem
1c7a1 2a 70 4e 61 6d 65 3b 0a 20 20 20 20 7d 20 63 6b *pName;. } ck
1c7a2 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f ;. struct OP_
1c7a3 56 55 70 64 61 74 65 5f 73 74 61 63 6b 5f 76 61 VUpdate_stack_va
1c7a4 72 73 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 rs {. sqlit
1c7a5 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a e3_vtab *pVtab;.
1c7a6 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 6f sqlite3_mo
1c7a7 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 dule *pModule;.
1c7a8 20 20 20 20 20 69 6e 74 20 6e 41 72 67 3b 0a 20 int nArg;.
1c7a9 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 int i;.
1c7aa 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 72 sqlite_int64 r
1c7ab 6f 77 69 64 3b 0a 20 20 20 20 20 20 4d 65 6d 20 owid;. Mem
1c7ac 2a 2a 61 70 41 72 67 3b 0a 20 20 20 20 20 20 4d **apArg;. M
1c7ad 65 6d 20 2a 70 58 3b 0a 20 20 20 20 7d 20 63 6c em *pX;. } cl
1c7ae 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f ;. struct OP_
1c7af 50 61 67 65 63 6f 75 6e 74 5f 73 74 61 63 6b 5f Pagecount_stack_
1c7b0 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 vars {. int
1c7b1 20 70 31 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e p1;. int n
1c7b2 50 61 67 65 3b 0a 20 20 20 20 20 20 50 61 67 65 Page;. Page
1c7b3 72 20 2a 70 50 61 67 65 72 3b 0a 20 20 20 20 7d r *pPager;. }
1c7b4 20 63 6d 3b 0a 20 20 20 20 73 74 72 75 63 74 20 cm;. struct
1c7b5 4f 50 5f 54 72 61 63 65 5f 73 74 61 63 6b 5f 76 OP_Trace_stack_v
1c7b6 61 72 73 20 7b 0a 20 20 20 20 20 20 63 68 61 72 ars {. char
1c7b7 20 2a 7a 54 72 61 63 65 3b 0a 20 20 20 20 7d 20 *zTrace;. }
1c7b8 63 6e 3b 0a 20 20 7d 20 75 3b 0a 20 20 2f 2a 20 cn;. } u;. /*
1c7b9 45 6e 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c End automaticall
1c7ba 79 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65 y generated code
1c7bb 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a . *************
1c7bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c7bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c7be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c7bf 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 20 20 61 73 73 65 *******/.. asse
1c7c0 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 rt( p->magic==VD
1c7c1 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 29 3b 20 BE_MAGIC_RUN );
1c7c2 20 2f 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70 /* sqlite3_step
1c7c3 28 29 20 76 65 72 69 66 69 65 73 20 74 68 69 73 () verifies this
1c7c4 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 64 62 */. assert( db
1c7c5 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f ->magic==SQLITE_
1c7c6 4d 41 47 49 43 5f 42 55 53 59 20 29 3b 0a 20 20 MAGIC_BUSY );.
1c7c7 73 71 6c 69 74 65 33 56 64 62 65 4d 75 74 65 78 sqlite3VdbeMutex
1c7c8 41 72 72 61 79 45 6e 74 65 72 28 70 29 3b 0a 20 ArrayEnter(p);.
1c7c9 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 if( p->rc==SQLI
1c7ca 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 TE_NOMEM ){.
1c7cb 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 /* This happens
1c7cc 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 69 6e if a malloc() in
1c7cd 73 69 64 65 20 61 20 63 61 6c 6c 20 74 6f 20 73 side a call to s
1c7ce 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 qlite3_column_te
1c7cf 78 74 28 29 20 6f 72 0a 20 20 20 20 2a 2a 20 73 xt() or. ** s
1c7d0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 qlite3_column_te
1c7d1 78 74 31 36 28 29 20 66 61 69 6c 65 64 2e 20 20 xt16() failed.
1c7d2 2a 2f 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d */. goto no_m
1c7d3 65 6d 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 em;. }. assert
1c7d4 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f ( p->rc==SQLITE_
1c7d5 4f 4b 20 7c 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c OK || p->rc==SQL
1c7d6 49 54 45 5f 42 55 53 59 20 29 3b 0a 20 20 70 2d ITE_BUSY );. p-
1c7d7 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b >rc = SQLITE_OK;
1c7d8 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 65 78 . assert( p->ex
1c7d9 70 6c 61 69 6e 3d 3d 30 20 29 3b 0a 20 20 70 2d plain==0 );. p-
1c7da 3e 70 52 65 73 75 6c 74 53 65 74 20 3d 20 30 3b >pResultSet = 0;
1c7db 0a 20 20 64 62 2d 3e 62 75 73 79 48 61 6e 64 6c . db->busyHandl
1c7dc 65 72 2e 6e 42 75 73 79 20 3d 20 30 3b 0a 20 20 er.nBusy = 0;.
1c7dd 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 CHECK_FOR_INTERR
1c7de 55 50 54 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 UPT;. sqlite3Vd
1c7df 62 65 49 4f 54 72 61 63 65 53 71 6c 28 70 29 3b beIOTraceSql(p);
1c7e0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
1c7e1 45 42 55 47 0a 20 20 73 71 6c 69 74 65 33 42 65 EBUG. sqlite3Be
1c7e2 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 ginBenignMalloc(
1c7e3 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 63 3d 3d );. if( p->pc==
1c7e4 30 20 0a 20 20 20 26 26 20 28 28 70 2d 3e 64 62 0 . && ((p->db
1c7e5 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 ->flags & SQLITE
1c7e6 5f 56 64 62 65 4c 69 73 74 69 6e 67 29 20 7c 7c _VdbeListing) ||
1c7e7 20 66 69 6c 65 45 78 69 73 74 73 28 64 62 2c 20 fileExists(db,
1c7e8 22 76 64 62 65 5f 65 78 70 6c 61 69 6e 22 29 29 "vdbe_explain"))
1c7e9 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b . ){. int i;
1c7ea 0a 20 20 20 20 70 72 69 6e 74 66 28 22 56 44 42 . printf("VDB
1c7eb 45 20 50 72 6f 67 72 61 6d 20 4c 69 73 74 69 6e E Program Listin
1c7ec 67 3a 5c 6e 22 29 3b 0a 20 20 20 20 73 71 6c 69 g:\n");. sqli
1c7ed 74 65 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28 te3VdbePrintSql(
1c7ee 70 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b p);. for(i=0;
1c7ef 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b i<p->nOp; i++){
1c7f0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1c7f1 62 65 50 72 69 6e 74 4f 70 28 73 74 64 6f 75 74 bePrintOp(stdout
1c7f2 2c 20 69 2c 20 26 70 2d 3e 61 4f 70 5b 69 5d 29 , i, &p->aOp[i])
1c7f3 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 ;. }. }. if
1c7f4 28 20 66 69 6c 65 45 78 69 73 74 73 28 64 62 2c ( fileExists(db,
1c7f5 20 22 76 64 62 65 5f 74 72 61 63 65 22 29 20 29 "vdbe_trace") )
1c7f6 7b 0a 20 20 20 20 70 2d 3e 74 72 61 63 65 20 3d {. p->trace =
1c7f7 20 73 74 64 6f 75 74 3b 0a 20 20 7d 0a 20 20 73 stdout;. }. s
1c7f8 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d qlite3EndBenignM
1c7f9 61 6c 6c 6f 63 28 29 3b 0a 23 65 6e 64 69 66 0a alloc();.#endif.
1c7fa 20 20 66 6f 72 28 70 63 3d 70 2d 3e 70 63 3b 20 for(pc=p->pc;
1c7fb 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 70 rc==SQLITE_OK; p
1c7fc 63 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 c++){. assert
1c7fd 28 20 70 63 3e 3d 30 20 26 26 20 70 63 3c 70 2d ( pc>=0 && pc<p-
1c7fe 3e 6e 4f 70 20 29 3b 0a 20 20 20 20 69 66 28 20 >nOp );. if(
1c7ff 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1c800 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a ) goto no_mem;.
1c801 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 #ifdef VDBE_PROF
1c802 49 4c 45 0a 20 20 20 20 6f 72 69 67 50 63 20 3d ILE. origPc =
1c803 20 70 63 3b 0a 20 20 20 20 73 74 61 72 74 20 3d pc;. start =
1c804 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 sqlite3Hwtime()
1c805 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 70 4f 70 ;.#endif. pOp
1c806 20 3d 20 26 70 2d 3e 61 4f 70 5b 70 63 5d 3b 0a = &p->aOp[pc];.
1c807 0a 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 61 6c 6c . /* Only all
1c808 6f 77 20 74 72 61 63 69 6e 67 20 69 66 20 53 51 ow tracing if SQ
1c809 4c 49 54 45 5f 44 45 42 55 47 20 69 73 20 64 65 LITE_DEBUG is de
1c80a 66 69 6e 65 64 2e 0a 20 20 20 20 2a 2f 0a 23 69 fined.. */.#i
1c80b 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
1c80c 47 0a 20 20 20 20 69 66 28 20 70 2d 3e 74 72 61 G. if( p->tra
1c80d 63 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 ce ){. if(
1c80e 70 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 pc==0 ){.
1c80f 20 70 72 69 6e 74 66 28 22 56 44 42 45 20 45 78 printf("VDBE Ex
1c810 65 63 75 74 69 6f 6e 20 54 72 61 63 65 3a 5c 6e ecution Trace:\n
1c811 22 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ");. sqli
1c812 74 65 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28 te3VdbePrintSql(
1c813 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 p);. }.
1c814 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 sqlite3VdbePri
1c815 6e 74 4f 70 28 70 2d 3e 74 72 61 63 65 2c 20 70 ntOp(p->trace, p
1c816 63 2c 20 70 4f 70 29 3b 0a 20 20 20 20 7d 0a 20 c, pOp);. }.
1c817 20 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65 3d if( p->trace=
1c818 3d 30 20 26 26 20 70 63 3d 3d 30 20 29 7b 0a 20 =0 && pc==0 ){.
1c819 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 sqlite3Begi
1c81a 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b nBenignMalloc();
1c81b 0a 20 20 20 20 20 20 69 66 28 20 66 69 6c 65 45 . if( fileE
1c81c 78 69 73 74 73 28 64 62 2c 20 22 76 64 62 65 5f xists(db, "vdbe_
1c81d 73 71 6c 74 72 61 63 65 22 29 20 29 7b 0a 20 20 sqltrace") ){.
1c81e 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1c81f 65 50 72 69 6e 74 53 71 6c 28 70 29 3b 0a 20 20 ePrintSql(p);.
1c820 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 }. sqli
1c821 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c te3EndBenignMall
1c822 6f 63 28 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 oc();. }.#end
1c823 69 66 0a 20 20 20 20 20 20 0a 0a 20 20 20 20 2f if. .. /
1c824 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 * Check to see i
1c825 66 20 77 65 20 6e 65 65 64 20 74 6f 20 73 69 6d f we need to sim
1c826 75 6c 61 74 65 20 61 6e 20 69 6e 74 65 72 72 75 ulate an interru
1c827 70 74 2e 20 20 54 68 69 73 20 6f 6e 6c 79 20 68 pt. This only h
1c828 61 70 70 65 6e 73 0a 20 20 20 20 2a 2a 20 69 66 appens. ** if
1c829 20 77 65 20 68 61 76 65 20 61 20 73 70 65 63 69 we have a speci
1c82a 61 6c 20 74 65 73 74 20 62 75 69 6c 64 2e 0a 20 al test build..
1c82b 20 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c */.#ifdef SQL
1c82c 49 54 45 5f 54 45 53 54 0a 20 20 20 20 69 66 28 ITE_TEST. if(
1c82d 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 sqlite3_interru
1c82e 70 74 5f 63 6f 75 6e 74 3e 30 20 29 7b 0a 20 20 pt_count>0 ){.
1c82f 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 sqlite3_inte
1c830 72 72 75 70 74 5f 63 6f 75 6e 74 2d 2d 3b 0a 20 rrupt_count--;.
1c831 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
1c832 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f 75 6e 74 _interrupt_count
1c833 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 ==0 ){. s
1c834 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 qlite3_interrupt
1c835 28 64 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 (db);. }.
1c836 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e }.#endif..#ifn
1c837 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1c838 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 43 PROGRESS_CALLBAC
1c839 4b 0a 20 20 20 20 2f 2a 20 43 61 6c 6c 20 74 68 K. /* Call th
1c83a 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 e progress callb
1c83b 61 63 6b 20 69 66 20 69 74 20 69 73 20 63 6f 6e ack if it is con
1c83c 66 69 67 75 72 65 64 20 61 6e 64 20 74 68 65 20 figured and the
1c83d 72 65 71 75 69 72 65 64 20 6e 75 6d 62 65 72 0a required number.
1c83e 20 20 20 20 2a 2a 20 6f 66 20 56 44 42 45 20 6f ** of VDBE o
1c83f 70 73 20 68 61 76 65 20 62 65 65 6e 20 65 78 65 ps have been exe
1c840 63 75 74 65 64 20 28 65 69 74 68 65 72 20 73 69 cuted (either si
1c841 6e 63 65 20 74 68 69 73 20 69 6e 76 6f 63 61 74 nce this invocat
1c842 69 6f 6e 20 6f 66 0a 20 20 20 20 2a 2a 20 73 71 ion of. ** sq
1c843 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 29 20 lite3VdbeExec()
1c844 6f 72 20 73 69 6e 63 65 20 6c 61 73 74 20 74 69 or since last ti
1c845 6d 65 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 me the progress
1c846 63 61 6c 6c 62 61 63 6b 20 77 61 73 20 63 61 6c callback was cal
1c847 6c 65 64 29 2e 0a 20 20 20 20 2a 2a 20 49 66 20 led).. ** If
1c848 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c the progress cal
1c849 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f lback returns no
1c84a 6e 2d 7a 65 72 6f 2c 20 65 78 69 74 20 74 68 65 n-zero, exit the
1c84b 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 virtual machine
1c84c 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 61 20 72 with. ** a r
1c84d 65 74 75 72 6e 20 63 6f 64 65 20 53 51 4c 49 54 eturn code SQLIT
1c84e 45 5f 41 42 4f 52 54 2e 0a 20 20 20 20 2a 2f 0a E_ABORT.. */.
1c84f 20 20 20 20 69 66 28 20 64 62 2d 3e 78 50 72 6f if( db->xPro
1c850 67 72 65 73 73 20 29 7b 0a 20 20 20 20 20 20 69 gress ){. i
1c851 66 28 20 64 62 2d 3e 6e 50 72 6f 67 72 65 73 73 f( db->nProgress
1c852 4f 70 73 3d 3d 6e 50 72 6f 67 72 65 73 73 4f 70 Ops==nProgressOp
1c853 73 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 s ){. int
1c854 20 70 72 63 3b 0a 20 20 20 20 20 20 20 20 69 66 prc;. if
1c855 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f ( sqlite3SafetyO
1c856 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 ff(db) ) goto ab
1c857 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 ort_due_to_misus
1c858 65 3b 0a 20 20 20 20 20 20 20 20 70 72 63 20 3d e;. prc =
1c859 64 62 2d 3e 78 50 72 6f 67 72 65 73 73 28 64 62 db->xProgress(db
1c85a 2d 3e 70 50 72 6f 67 72 65 73 73 41 72 67 29 3b ->pProgressArg);
1c85b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c . if( sql
1c85c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 ite3SafetyOn(db)
1c85d 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 ) goto abort_du
1c85e 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 e_to_misuse;.
1c85f 20 20 20 20 20 69 66 28 20 70 72 63 21 3d 30 20 if( prc!=0
1c860 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 ){. rc
1c861 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 = SQLITE_INTERRU
1c862 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f PT;. go
1c863 74 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 to vdbe_error_ha
1c864 6c 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 lt;. }.
1c865 20 20 20 20 20 20 6e 50 72 6f 67 72 65 73 73 4f nProgressO
1c866 70 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a ps = 0;. }.
1c867 20 20 20 20 20 20 6e 50 72 6f 67 72 65 73 73 4f nProgressO
1c868 70 73 2b 2b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 ps++;. }.#end
1c869 69 66 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 63 6f if.. /* Do co
1c86a 6d 6d 6f 6e 20 73 65 74 75 70 20 70 72 6f 63 65 mmon setup proce
1c86b 73 73 69 6e 67 20 66 6f 72 20 61 6e 79 20 6f 70 ssing for any op
1c86c 63 6f 64 65 20 74 68 61 74 20 69 73 20 6d 61 72 code that is mar
1c86d 6b 65 64 0a 20 20 20 20 2a 2a 20 77 69 74 68 20 ked. ** with
1c86e 74 68 65 20 22 6f 75 74 32 2d 70 72 65 72 65 6c the "out2-prerel
1c86f 65 61 73 65 22 20 74 61 67 2e 20 20 53 75 63 68 ease" tag. Such
1c870 20 6f 70 63 6f 64 65 73 20 68 61 76 65 20 61 20 opcodes have a
1c871 73 69 6e 67 6c 65 0a 20 20 20 20 2a 2a 20 6f 75 single. ** ou
1c872 74 70 75 74 20 77 68 69 63 68 20 69 73 20 73 70 tput which is sp
1c873 65 63 69 66 69 65 64 20 62 79 20 74 68 65 20 50 ecified by the P
1c874 32 20 70 61 72 61 6d 65 74 65 72 2e 20 20 54 68 2 parameter. Th
1c875 65 20 50 32 20 72 65 67 69 73 74 65 72 0a 20 20 e P2 register.
1c876 20 20 2a 2a 20 69 73 20 69 6e 69 74 69 61 6c 69 ** is initiali
1c877 7a 65 64 20 74 6f 20 61 20 4e 55 4c 4c 2e 0a 20 zed to a NULL..
1c878 20 20 20 2a 2f 0a 20 20 20 20 6f 70 50 72 6f 70 */. opProp
1c879 65 72 74 79 20 3d 20 6f 70 63 6f 64 65 50 72 6f erty = opcodePro
1c87a 70 65 72 74 79 5b 70 4f 70 2d 3e 6f 70 63 6f 64 perty[pOp->opcod
1c87b 65 5d 3b 0a 20 20 20 20 69 66 28 20 28 6f 70 50 e];. if( (opP
1c87c 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47 5f roperty & OPFLG_
1c87d 4f 55 54 32 5f 50 52 45 52 45 4c 45 41 53 45 29 OUT2_PRERELEASE)
1c87e 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 !=0 ){. ass
1c87f 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 ert( pOp->p2>0 )
1c880 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1c881 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d pOp->p2<=p->nMem
1c882 20 29 3b 0a 20 20 20 20 20 20 70 4f 75 74 20 3d );. pOut =
1c883 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 &p->aMem[pOp->p
1c884 32 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 2];. sqlite
1c885 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 3VdbeMemReleaseE
1c886 78 74 65 72 6e 61 6c 28 70 4f 75 74 29 3b 0a 20 xternal(pOut);.
1c887 20 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 pOut->flags
1c888 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 = MEM_Null;.
1c889 20 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 30 3b 0a pOut->n = 0;.
1c88a 20 20 20 20 7d 65 6c 73 65 0a 20 0a 20 20 20 20 }else. .
1c88b 2f 2a 20 44 6f 20 63 6f 6d 6d 6f 6e 20 73 65 74 /* Do common set
1c88c 75 70 20 66 6f 72 20 6f 70 63 6f 64 65 73 20 6d up for opcodes m
1c88d 61 72 6b 65 64 20 77 69 74 68 20 6f 6e 65 20 6f arked with one o
1c88e 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a f the following.
1c88f 20 20 20 20 2a 2a 20 63 6f 6d 62 69 6e 61 74 69 ** combinati
1c890 6f 6e 73 20 6f 66 20 70 72 6f 70 65 72 74 69 65 ons of propertie
1c891 73 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a s.. **. **
1c892 20 20 20 20 20 20 20 20 20 20 20 69 6e 31 0a 20 in1.
1c893 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 **
1c894 69 6e 31 20 69 6e 32 0a 20 20 20 20 2a 2a 20 20 in1 in2. **
1c895 20 20 20 20 20 20 20 20 20 69 6e 31 20 69 6e 32 in1 in2
1c896 20 6f 75 74 33 0a 20 20 20 20 2a 2a 20 20 20 20 out3. **
1c897 20 20 20 20 20 20 20 69 6e 31 20 69 6e 33 0a 20 in1 in3.
1c898 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 56 61 72 **. ** Var
1c899 69 61 62 6c 65 73 20 70 49 6e 31 2c 20 70 49 6e iables pIn1, pIn
1c89a 32 2c 20 61 6e 64 20 70 49 6e 33 20 61 72 65 20 2, and pIn3 are
1c89b 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f made to point to
1c89c 20 61 70 70 72 6f 70 72 69 61 74 65 0a 20 20 20 appropriate.
1c89d 20 2a 2a 20 72 65 67 69 73 74 65 72 73 20 66 6f ** registers fo
1c89e 72 20 69 6e 70 75 74 73 2e 20 20 56 61 72 69 61 r inputs. Varia
1c89f 62 6c 65 20 70 4f 75 74 20 70 6f 69 6e 74 73 20 ble pOut points
1c8a0 74 6f 20 74 68 65 20 6f 75 74 70 75 74 20 72 65 to the output re
1c8a1 67 69 73 74 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 gister.. */.
1c8a2 20 20 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72 if( (opProper
1c8a3 74 79 20 26 20 4f 50 46 4c 47 5f 49 4e 31 29 21 ty & OPFLG_IN1)!
1c8a4 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 =0 ){. asse
1c8a5 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b rt( pOp->p1>0 );
1c8a6 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1c8a7 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 Op->p1<=p->nMem
1c8a8 29 3b 0a 20 20 20 20 20 20 70 49 6e 31 20 3d 20 );. pIn1 =
1c8a9 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 &p->aMem[pOp->p1
1c8aa 5d 3b 0a 20 20 20 20 20 20 52 45 47 49 53 54 45 ];. REGISTE
1c8ab 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c R_TRACE(pOp->p1,
1c8ac 20 70 49 6e 31 29 3b 0a 20 20 20 20 20 20 69 66 pIn1);. if
1c8ad 28 20 28 6f 70 50 72 6f 70 65 72 74 79 20 26 20 ( (opProperty &
1c8ae 4f 50 46 4c 47 5f 49 4e 32 29 21 3d 30 20 29 7b OPFLG_IN2)!=0 ){
1c8af 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
1c8b0 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 pOp->p2>0 );.
1c8b1 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f assert( pO
1c8b2 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 p->p2<=p->nMem )
1c8b3 3b 0a 20 20 20 20 20 20 20 20 70 49 6e 32 20 3d ;. pIn2 =
1c8b4 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 &p->aMem[pOp->p
1c8b5 32 5d 3b 0a 20 20 20 20 20 20 20 20 52 45 47 49 2];. REGI
1c8b6 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e STER_TRACE(pOp->
1c8b7 70 32 2c 20 70 49 6e 32 29 3b 0a 20 20 20 20 20 p2, pIn2);.
1c8b8 20 20 20 2f 2a 20 41 73 20 63 75 72 72 65 6e 74 /* As current
1c8b9 6c 79 20 69 6d 70 6c 65 6d 65 6e 74 65 64 2c 20 ly implemented,
1c8ba 69 6e 32 20 69 6d 70 6c 69 65 73 20 6f 75 74 33 in2 implies out3
1c8bb 2e 20 20 54 68 65 72 65 20 69 73 20 6e 6f 20 72 . There is no r
1c8bc 65 61 73 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a eason. **
1c8bd 20 77 68 79 20 74 68 69 73 20 68 61 73 20 74 6f why this has to
1c8be 20 62 65 2c 20 69 74 20 6a 75 73 74 20 77 6f 72 be, it just wor
1c8bf 6b 65 64 20 6f 75 74 20 74 68 61 74 20 77 61 79 ked out that way
1c8c0 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 . */. ass
1c8c1 65 72 74 28 20 28 6f 70 50 72 6f 70 65 72 74 79 ert( (opProperty
1c8c2 20 26 20 4f 50 46 4c 47 5f 4f 55 54 33 29 21 3d & OPFLG_OUT3)!=
1c8c3 30 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 0 );. ass
1c8c4 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 ert( pOp->p3>0 )
1c8c5 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 ;. assert
1c8c6 28 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d ( pOp->p3<=p->nM
1c8c7 65 6d 20 29 3b 0a 20 20 20 20 20 20 20 20 70 4f em );. pO
1c8c8 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f ut = &p->aMem[pO
1c8c9 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 20 7d 65 p->p3];. }e
1c8ca 6c 73 65 20 69 66 28 20 28 6f 70 50 72 6f 70 65 lse if( (opPrope
1c8cb 72 74 79 20 26 20 4f 50 46 4c 47 5f 49 4e 33 29 rty & OPFLG_IN3)
1c8cc 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 !=0 ){. a
1c8cd 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 ssert( pOp->p3>0
1c8ce 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 );. asse
1c8cf 72 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e rt( pOp->p3<=p->
1c8d0 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 20 20 nMem );.
1c8d1 70 49 6e 33 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b pIn3 = &p->aMem[
1c8d2 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 20 pOp->p3];.
1c8d3 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 REGISTER_TRACE
1c8d4 28 70 4f 70 2d 3e 70 33 2c 20 70 49 6e 33 29 3b (pOp->p3, pIn3);
1c8d5 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c . }. }el
1c8d6 73 65 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72 se if( (opProper
1c8d7 74 79 20 26 20 4f 50 46 4c 47 5f 49 4e 32 29 21 ty & OPFLG_IN2)!
1c8d8 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 =0 ){. asse
1c8d9 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b rt( pOp->p2>0 );
1c8da 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1c8db 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 Op->p2<=p->nMem
1c8dc 29 3b 0a 20 20 20 20 20 20 70 49 6e 32 20 3d 20 );. pIn2 =
1c8dd 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 &p->aMem[pOp->p2
1c8de 5d 3b 0a 20 20 20 20 20 20 52 45 47 49 53 54 45 ];. REGISTE
1c8df 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c R_TRACE(pOp->p2,
1c8e0 20 70 49 6e 32 29 3b 0a 20 20 20 20 7d 65 6c 73 pIn2);. }els
1c8e1 65 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72 74 e if( (opPropert
1c8e2 79 20 26 20 4f 50 46 4c 47 5f 49 4e 33 29 21 3d y & OPFLG_IN3)!=
1c8e3 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 0 ){. asser
1c8e4 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a t( pOp->p3>0 );.
1c8e5 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f assert( pO
1c8e6 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 p->p3<=p->nMem )
1c8e7 3b 0a 20 20 20 20 20 20 70 49 6e 33 20 3d 20 26 ;. pIn3 = &
1c8e8 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d p->aMem[pOp->p3]
1c8e9 3b 0a 20 20 20 20 20 20 52 45 47 49 53 54 45 52 ;. REGISTER
1c8ea 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 _TRACE(pOp->p3,
1c8eb 70 49 6e 33 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 pIn3);. }..
1c8ec 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f switch( pOp->o
1c8ed 70 63 6f 64 65 20 29 7b 0a 0a 2f 2a 2a 2a 2a 2a pcode ){../*****
1c8ee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c8ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c8f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c8f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c8f2 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 57 68 61 74 ********.** What
1c8f3 20 66 6f 6c 6c 6f 77 73 20 69 73 20 61 20 6d 61 follows is a ma
1c8f4 73 73 69 76 65 20 73 77 69 74 63 68 20 73 74 61 ssive switch sta
1c8f5 74 65 6d 65 6e 74 20 77 68 65 72 65 20 65 61 63 tement where eac
1c8f6 68 20 63 61 73 65 20 69 6d 70 6c 65 6d 65 6e 74 h case implement
1c8f7 73 20 61 0a 2a 2a 20 73 65 70 61 72 61 74 65 20 s a.** separate
1c8f8 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 20 74 instruction in t
1c8f9 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 he virtual machi
1c8fa 6e 65 2e 20 20 49 66 20 77 65 20 66 6f 6c 6c 6f ne. If we follo
1c8fb 77 20 74 68 65 20 75 73 75 61 6c 0a 2a 2a 20 69 w the usual.** i
1c8fc 6e 64 65 6e 74 61 74 69 6f 6e 20 63 6f 6e 76 65 ndentation conve
1c8fd 6e 74 69 6f 6e 73 2c 20 65 61 63 68 20 63 61 73 ntions, each cas
1c8fe 65 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 64 65 e should be inde
1c8ff 6e 74 65 64 20 62 79 20 36 20 73 70 61 63 65 73 nted by 6 spaces
1c900 2e 20 20 42 75 74 0a 2a 2a 20 74 68 61 74 20 69 . But.** that i
1c901 73 20 61 20 6c 6f 74 20 6f 66 20 77 61 73 74 65 s a lot of waste
1c902 64 20 73 70 61 63 65 20 6f 6e 20 74 68 65 20 6c d space on the l
1c903 65 66 74 20 6d 61 72 67 69 6e 2e 20 20 53 6f 20 eft margin. So
1c904 74 68 65 20 63 6f 64 65 20 77 69 74 68 69 6e 0a the code within.
1c905 2a 2a 20 74 68 65 20 73 77 69 74 63 68 20 73 74 ** the switch st
1c906 61 74 65 6d 65 6e 74 20 77 69 6c 6c 20 62 72 65 atement will bre
1c907 61 6b 20 77 69 74 68 20 63 6f 6e 76 65 6e 74 69 ak with conventi
1c908 6f 6e 20 61 6e 64 20 62 65 20 66 6c 75 73 68 2d on and be flush-
1c909 6c 65 66 74 2e 20 41 6e 6f 74 68 65 72 0a 2a 2a left. Another.**
1c90a 20 62 69 67 20 63 6f 6d 6d 65 6e 74 20 28 73 69 big comment (si
1c90b 6d 69 6c 61 72 20 74 6f 20 74 68 69 73 20 6f 6e milar to this on
1c90c 65 29 20 77 69 6c 6c 20 6d 61 72 6b 20 74 68 65 e) will mark the
1c90d 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 63 6f point in the co
1c90e 64 65 20 77 68 65 72 65 0a 2a 2a 20 77 65 20 74 de where.** we t
1c90f 72 61 6e 73 69 74 69 6f 6e 20 62 61 63 6b 20 74 ransition back t
1c910 6f 20 6e 6f 72 6d 61 6c 20 69 6e 64 65 6e 74 61 o normal indenta
1c911 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 tion..**.** The
1c912 66 6f 72 6d 61 74 74 69 6e 67 20 6f 66 20 65 61 formatting of ea
1c913 63 68 20 63 61 73 65 20 69 73 20 69 6d 70 6f 72 ch case is impor
1c914 74 61 6e 74 2e 20 20 54 68 65 20 6d 61 6b 65 66 tant. The makef
1c915 69 6c 65 20 66 6f 72 20 53 51 4c 69 74 65 0a 2a ile for SQLite.*
1c916 2a 20 67 65 6e 65 72 61 74 65 73 20 74 77 6f 20 * generates two
1c917 43 20 66 69 6c 65 73 20 22 6f 70 63 6f 64 65 73 C files "opcodes
1c918 2e 68 22 20 61 6e 64 20 22 6f 70 63 6f 64 65 73 .h" and "opcodes
1c919 2e 63 22 20 62 79 20 73 63 61 6e 6e 69 6e 67 20 .c" by scanning
1c91a 74 68 69 73 0a 2a 2a 20 66 69 6c 65 20 6c 6f 6f this.** file loo
1c91b 6b 69 6e 67 20 66 6f 72 20 6c 69 6e 65 73 20 74 king for lines t
1c91c 68 61 74 20 62 65 67 69 6e 20 77 69 74 68 20 22 hat begin with "
1c91d 63 61 73 65 20 4f 50 5f 22 2e 20 20 54 68 65 20 case OP_". The
1c91e 6f 70 63 6f 64 65 73 2e 68 20 66 69 6c 65 73 0a opcodes.h files.
1c91f 2a 2a 20 77 69 6c 6c 20 62 65 20 66 69 6c 6c 65 ** will be fille
1c920 64 20 77 69 74 68 20 23 64 65 66 69 6e 65 73 20 d with #defines
1c921 74 68 61 74 20 67 69 76 65 20 75 6e 69 71 75 65 that give unique
1c922 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73 20 integer values
1c923 74 6f 20 65 61 63 68 0a 2a 2a 20 6f 70 63 6f 64 to each.** opcod
1c924 65 20 61 6e 64 20 74 68 65 20 6f 70 63 6f 64 65 e and the opcode
1c925 73 2e 63 20 66 69 6c 65 20 69 73 20 66 69 6c 6c s.c file is fill
1c926 65 64 20 77 69 74 68 20 61 6e 20 61 72 72 61 79 ed with an array
1c927 20 6f 66 20 73 74 72 69 6e 67 73 20 77 68 65 72 of strings wher
1c928 65 0a 2a 2a 20 65 61 63 68 20 73 74 72 69 6e 67 e.** each string
1c929 20 69 73 20 74 68 65 20 73 79 6d 62 6f 6c 69 63 is the symbolic
1c92a 20 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 63 6f name for the co
1c92b 72 72 65 73 70 6f 6e 64 69 6e 67 20 6f 70 63 6f rresponding opco
1c92c 64 65 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 63 de. If the.** c
1c92d 61 73 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 ase statement is
1c92e 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 20 63 followed by a c
1c92f 6f 6d 6d 65 6e 74 20 6f 66 20 74 68 65 20 66 6f omment of the fo
1c930 72 6d 20 22 2f 23 20 73 61 6d 65 20 61 73 20 2e rm "/# same as .
1c931 2e 2e 20 23 2f 22 0a 2a 2a 20 74 68 61 74 20 63 .. #/".** that c
1c932 6f 6d 6d 65 6e 74 20 69 73 20 75 73 65 64 20 74 omment is used t
1c933 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 o determine the
1c934 70 61 72 74 69 63 75 6c 61 72 20 76 61 6c 75 65 particular value
1c935 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 2e 0a of the opcode..
1c936 2a 2a 0a 2a 2a 20 4f 74 68 65 72 20 6b 65 79 77 **.** Other keyw
1c937 6f 72 64 73 20 69 6e 20 74 68 65 20 63 6f 6d 6d ords in the comm
1c938 65 6e 74 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 ent that follows
1c939 20 65 61 63 68 20 63 61 73 65 20 61 72 65 20 75 each case are u
1c93a 73 65 64 20 74 6f 0a 2a 2a 20 63 6f 6e 73 74 72 sed to.** constr
1c93b 75 63 74 20 74 68 65 20 4f 50 46 4c 47 5f 49 4e uct the OPFLG_IN
1c93c 49 54 49 41 4c 49 5a 45 52 20 76 61 6c 75 65 20 ITIALIZER value
1c93d 74 68 61 74 20 69 6e 69 74 69 61 6c 69 7a 65 73 that initializes
1c93e 20 6f 70 63 6f 64 65 50 72 6f 70 65 72 74 79 5b opcodeProperty[
1c93f 5d 2e 0a 2a 2a 20 4b 65 79 77 6f 72 64 73 20 69 ]..** Keywords i
1c940 6e 63 6c 75 64 65 3a 20 69 6e 31 2c 20 69 6e 32 nclude: in1, in2
1c941 2c 20 69 6e 33 2c 20 6f 75 74 32 5f 70 72 65 72 , in3, out2_prer
1c942 65 6c 65 61 73 65 2c 20 6f 75 74 32 2c 20 6f 75 elease, out2, ou
1c943 74 33 2e 20 20 53 65 65 0a 2a 2a 20 74 68 65 20 t3. See.** the
1c944 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 6b 20 73 63 mkopcodeh.awk sc
1c945 72 69 70 74 20 66 6f 72 20 61 64 64 69 74 69 6f ript for additio
1c946 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e nal information.
1c947 0a 2a 2a 0a 2a 2a 20 44 6f 63 75 6d 65 6e 74 61 .**.** Documenta
1c948 74 69 6f 6e 20 61 62 6f 75 74 20 56 44 42 45 20 tion about VDBE
1c949 6f 70 63 6f 64 65 73 20 69 73 20 67 65 6e 65 72 opcodes is gener
1c94a 61 74 65 64 20 62 79 20 73 63 61 6e 6e 69 6e 67 ated by scanning
1c94b 20 74 68 69 73 20 66 69 6c 65 0a 2a 2a 20 66 6f this file.** fo
1c94c 72 20 6c 69 6e 65 73 20 6f 66 20 74 68 61 74 20 r lines of that
1c94d 63 6f 6e 74 61 69 6e 20 22 4f 70 63 6f 64 65 3a contain "Opcode:
1c94e 22 2e 20 20 54 68 61 74 20 6c 69 6e 65 20 61 6e ". That line an
1c94f 64 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 d all subsequent
1c950 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 20 6c 69 6e 65 .** comment line
1c951 73 20 61 72 65 20 75 73 65 64 20 69 6e 20 74 68 s are used in th
1c952 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f 66 20 e generation of
1c953 74 68 65 20 6f 70 63 6f 64 65 2e 68 74 6d 6c 20 the opcode.html
1c954 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a documentation.**
1c955 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 55 4d file..**.** SUM
1c956 4d 41 52 59 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 MARY:.**.**
1c957 46 6f 72 6d 61 74 74 69 6e 67 20 69 73 20 69 6d Formatting is im
1c958 70 6f 72 74 61 6e 74 20 74 6f 20 73 63 72 69 70 portant to scrip
1c959 74 73 20 74 68 61 74 20 73 63 61 6e 20 74 68 69 ts that scan thi
1c95a 73 20 66 69 6c 65 2e 0a 2a 2a 20 20 20 20 20 44 s file..** D
1c95b 6f 20 6e 6f 74 20 64 65 76 69 61 74 65 20 66 72 o not deviate fr
1c95c 6f 6d 20 74 68 65 20 66 6f 72 6d 61 74 74 69 6e om the formattin
1c95d 67 20 73 74 79 6c 65 20 63 75 72 72 65 6e 74 6c g style currentl
1c95e 79 20 69 6e 20 75 73 65 2e 0a 2a 2a 0a 2a 2a 2a y in use..**.***
1c95f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c961 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c962 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c963 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 20 **********/../*
1c964 4f 70 63 6f 64 65 3a 20 20 47 6f 74 6f 20 2a 20 Opcode: Goto *
1c965 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 P2 * * *.**.** A
1c966 6e 20 75 6e 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 n unconditional
1c967 6a 75 6d 70 20 74 6f 20 61 64 64 72 65 73 73 20 jump to address
1c968 50 32 2e 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 P2..** The next
1c969 69 6e 73 74 72 75 63 74 69 6f 6e 20 65 78 65 63 instruction exec
1c96a 75 74 65 64 20 77 69 6c 6c 20 62 65 20 0a 2a 2a uted will be .**
1c96b 20 74 68 65 20 6f 6e 65 20 61 74 20 69 6e 64 65 the one at inde
1c96c 78 20 50 32 20 66 72 6f 6d 20 74 68 65 20 62 65 x P2 from the be
1c96d 67 69 6e 6e 69 6e 67 20 6f 66 0a 2a 2a 20 74 68 ginning of.** th
1c96e 65 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 63 61 e program..*/.ca
1c96f 73 65 20 4f 50 5f 47 6f 74 6f 3a 20 7b 20 20 20 se OP_Goto: {
1c970 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d /* jum
1c971 70 20 2a 2f 0a 20 20 43 48 45 43 4b 5f 46 4f 52 p */. CHECK_FOR
1c972 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 70 63 _INTERRUPT;. pc
1c973 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a = pOp->p2 - 1;.
1c974 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1c975 70 63 6f 64 65 3a 20 20 47 6f 73 75 62 20 50 31 pcode: Gosub P1
1c976 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * * *.**.**
1c977 57 72 69 74 65 20 74 68 65 20 63 75 72 72 65 6e Write the curren
1c978 74 20 61 64 64 72 65 73 73 20 6f 6e 74 6f 20 72 t address onto r
1c979 65 67 69 73 74 65 72 20 50 31 0a 2a 2a 20 61 6e egister P1.** an
1c97a 64 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 61 d then jump to a
1c97b 64 64 72 65 73 73 20 50 32 2e 0a 2a 2f 0a 63 61 ddress P2..*/.ca
1c97c 73 65 20 4f 50 5f 47 6f 73 75 62 3a 20 7b 20 20 se OP_Gosub: {
1c97d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d /* jum
1c97e 70 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 p */. assert( p
1c97f 4f 70 2d 3e 70 31 3e 30 20 29 3b 0a 20 20 61 73 Op->p1>0 );. as
1c980 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3c 3d 70 sert( pOp->p1<=p
1c981 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 49 6e 31 ->nMem );. pIn1
1c982 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d = &p->aMem[pOp-
1c983 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 >p1];. assert(
1c984 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d (pIn1->flags & M
1c985 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a 20 20 EM_Dyn)==0 );.
1c986 70 49 6e 31 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 pIn1->flags = ME
1c987 4d 5f 49 6e 74 3b 0a 20 20 70 49 6e 31 2d 3e 75 M_Int;. pIn1->u
1c988 2e 69 20 3d 20 70 63 3b 0a 20 20 52 45 47 49 53 .i = pc;. REGIS
1c989 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 TER_TRACE(pOp->p
1c98a 31 2c 20 70 49 6e 31 29 3b 0a 20 20 70 63 20 3d 1, pIn1);. pc =
1c98b 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 pOp->p2 - 1;.
1c98c 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1c98d 6f 64 65 3a 20 20 52 65 74 75 72 6e 20 50 31 20 ode: Return P1
1c98e 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 * * * *.**.** Ju
1c98f 6d 70 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 mp to the next i
1c990 6e 73 74 72 75 63 74 69 6f 6e 20 61 66 74 65 72 nstruction after
1c991 20 74 68 65 20 61 64 64 72 65 73 73 20 69 6e 20 the address in
1c992 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2f 0a register P1..*/.
1c993 63 61 73 65 20 4f 50 5f 52 65 74 75 72 6e 3a 20 case OP_Return:
1c994 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 { /* i
1c995 6e 31 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 n1 */. assert(
1c996 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pIn1->flags & ME
1c997 4d 5f 49 6e 74 20 29 3b 0a 20 20 70 63 20 3d 20 M_Int );. pc =
1c998 28 69 6e 74 29 70 49 6e 31 2d 3e 75 2e 69 3b 0a (int)pIn1->u.i;.
1c999 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1c99a 70 63 6f 64 65 3a 20 20 59 69 65 6c 64 20 50 31 pcode: Yield P1
1c99b 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 * * * *.**.** S
1c99c 77 61 70 20 74 68 65 20 70 72 6f 67 72 61 6d 20 wap the program
1c99d 63 6f 75 6e 74 65 72 20 77 69 74 68 20 74 68 65 counter with the
1c99e 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 value in regist
1c99f 65 72 20 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f er P1..*/.case O
1c9a0 50 5f 59 69 65 6c 64 3a 20 7b 20 20 20 20 20 20 P_Yield: {
1c9a1 20 20 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a /* in1 */.
1c9a2 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 #if 0 /* local
1c9a3 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1c9a4 69 6e 74 6f 20 75 2e 61 61 20 2a 2f 0a 20 20 69 into u.aa */. i
1c9a5 6e 74 20 70 63 44 65 73 74 3b 0a 23 65 6e 64 69 nt pcDest;.#endi
1c9a6 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1c9a7 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1c9a8 75 2e 61 61 20 2a 2f 0a 20 20 61 73 73 65 72 74 u.aa */. assert
1c9a9 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 ( (pIn1->flags &
1c9aa 20 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a MEM_Dyn)==0 );.
1c9ab 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 3d 20 pIn1->flags =
1c9ac 4d 45 4d 5f 49 6e 74 3b 0a 20 20 75 2e 61 61 2e MEM_Int;. u.aa.
1c9ad 70 63 44 65 73 74 20 3d 20 28 69 6e 74 29 70 49 pcDest = (int)pI
1c9ae 6e 31 2d 3e 75 2e 69 3b 0a 20 20 70 49 6e 31 2d n1->u.i;. pIn1-
1c9af 3e 75 2e 69 20 3d 20 70 63 3b 0a 20 20 52 45 47 >u.i = pc;. REG
1c9b0 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d ISTER_TRACE(pOp-
1c9b1 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20 70 63 >p1, pIn1);. pc
1c9b2 20 3d 20 75 2e 61 61 2e 70 63 44 65 73 74 3b 0a = u.aa.pcDest;.
1c9b3 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1c9b4 70 63 6f 64 65 3a 20 20 48 61 6c 74 49 66 4e 75 pcode: HaltIfNu
1c9b5 6c 6c 20 20 50 31 20 50 32 20 50 33 20 50 34 20 ll P1 P2 P3 P4
1c9b6 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 *.**.** Check th
1c9b7 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 e value in regis
1c9b8 74 65 72 20 50 33 2e 20 20 49 66 20 69 73 20 69 ter P3. If is i
1c9b9 73 20 4e 55 4c 4c 20 74 68 65 6e 20 48 61 6c 74 s NULL then Halt
1c9ba 20 75 73 69 6e 67 0a 2a 2a 20 70 61 72 61 6d 65 using.** parame
1c9bb 74 65 72 20 50 31 2c 20 50 32 2c 20 61 6e 64 20 ter P1, P2, and
1c9bc 50 34 20 61 73 20 69 66 20 74 68 69 73 20 77 65 P4 as if this we
1c9bd 72 65 20 61 20 48 61 6c 74 20 69 6e 73 74 72 75 re a Halt instru
1c9be 63 74 69 6f 6e 2e 20 20 49 66 20 74 68 65 0a 2a ction. If the.*
1c9bf 2a 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 * value in regis
1c9c0 74 65 72 20 50 33 20 69 73 20 6e 6f 74 20 4e 55 ter P3 is not NU
1c9c1 4c 4c 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f LL, then this ro
1c9c2 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 utine is a no-op
1c9c3 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 48 61 6c ..*/.case OP_Hal
1c9c4 74 49 66 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20 tIfNull: {
1c9c5 2f 2a 20 69 6e 33 20 2a 2f 0a 20 20 69 66 28 20 /* in3 */. if(
1c9c6 28 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d (pIn3->flags & M
1c9c7 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 20 62 72 EM_Null)==0 ) br
1c9c8 65 61 6b 3b 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 eak;. /* Fall t
1c9c9 68 72 6f 75 67 68 20 69 6e 74 6f 20 4f 50 5f 48 hrough into OP_H
1c9ca 61 6c 74 20 2a 2f 0a 7d 0a 0a 2f 2a 20 4f 70 63 alt */.}../* Opc
1c9cb 6f 64 65 3a 20 20 48 61 6c 74 20 50 31 20 50 32 ode: Halt P1 P2
1c9cc 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 * P4 *.**.** Ex
1c9cd 69 74 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 it immediately.
1c9ce 20 41 6c 6c 20 6f 70 65 6e 20 63 75 72 73 6f 72 All open cursor
1c9cf 73 2c 20 65 74 63 20 61 72 65 20 63 6c 6f 73 65 s, etc are close
1c9d0 64 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c d.** automatical
1c9d1 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 ly..**.** P1 is
1c9d2 74 68 65 20 72 65 73 75 6c 74 20 63 6f 64 65 20 the result code
1c9d3 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 returned by sqli
1c9d4 74 65 33 5f 65 78 65 63 28 29 2c 20 73 71 6c 69 te3_exec(), sqli
1c9d5 74 65 33 5f 72 65 73 65 74 28 29 2c 0a 2a 2a 20 te3_reset(),.**
1c9d6 6f 72 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c or sqlite3_final
1c9d7 69 7a 65 28 29 2e 20 20 46 6f 72 20 61 20 6e 6f ize(). For a no
1c9d8 72 6d 61 6c 20 68 61 6c 74 2c 20 74 68 69 73 20 rmal halt, this
1c9d9 73 68 6f 75 6c 64 20 62 65 20 53 51 4c 49 54 45 should be SQLITE
1c9da 5f 4f 4b 20 28 30 29 2e 0a 2a 2a 20 46 6f 72 20 _OK (0)..** For
1c9db 65 72 72 6f 72 73 2c 20 69 74 20 63 61 6e 20 62 errors, it can b
1c9dc 65 20 73 6f 6d 65 20 6f 74 68 65 72 20 76 61 6c e some other val
1c9dd 75 65 2e 20 20 49 66 20 50 31 21 3d 30 20 74 68 ue. If P1!=0 th
1c9de 65 6e 20 50 32 20 77 69 6c 6c 20 64 65 74 65 72 en P2 will deter
1c9df 6d 69 6e 65 0a 2a 2a 20 77 68 65 74 68 65 72 20 mine.** whether
1c9e0 6f 72 20 6e 6f 74 20 74 6f 20 72 6f 6c 6c 62 61 or not to rollba
1c9e1 63 6b 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 ck the current t
1c9e2 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 44 6f 20 ransaction. Do
1c9e3 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 not rollback.**
1c9e4 69 66 20 50 32 3d 3d 4f 45 5f 46 61 69 6c 2e 20 if P2==OE_Fail.
1c9e5 44 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 Do the rollback
1c9e6 69 66 20 50 32 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 if P2==OE_Rollba
1c9e7 63 6b 2e 20 20 49 66 20 50 32 3d 3d 4f 45 5f 41 ck. If P2==OE_A
1c9e8 62 6f 72 74 2c 0a 2a 2a 20 74 68 65 6e 20 62 61 bort,.** then ba
1c9e9 63 6b 20 6f 75 74 20 61 6c 6c 20 63 68 61 6e 67 ck out all chang
1c9ea 65 73 20 74 68 61 74 20 68 61 76 65 20 6f 63 63 es that have occ
1c9eb 75 72 72 65 64 20 64 75 72 69 6e 67 20 74 68 69 urred during thi
1c9ec 73 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 s execution of t
1c9ed 68 65 0a 2a 2a 20 56 44 42 45 2c 20 62 75 74 20 he.** VDBE, but
1c9ee 64 6f 20 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 20 do not rollback
1c9ef 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e the transaction.
1c9f0 20 0a 2a 2a 0a 2a 2a 20 49 66 20 50 34 20 69 73 .**.** If P4 is
1c9f1 20 6e 6f 74 20 6e 75 6c 6c 20 74 68 65 6e 20 69 not null then i
1c9f2 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 6d 65 t is an error me
1c9f3 73 73 61 67 65 20 73 74 72 69 6e 67 2e 0a 2a 2a ssage string..**
1c9f4 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 61 6e 20 .** There is an
1c9f5 69 6d 70 6c 69 65 64 20 22 48 61 6c 74 20 30 20 implied "Halt 0
1c9f6 30 20 30 22 20 69 6e 73 74 72 75 63 74 69 6f 6e 0 0" instruction
1c9f7 20 69 6e 73 65 72 74 65 64 20 61 74 20 74 68 65 inserted at the
1c9f8 20 76 65 72 79 20 65 6e 64 20 6f 66 0a 2a 2a 20 very end of.**
1c9f9 65 76 65 72 79 20 70 72 6f 67 72 61 6d 2e 20 20 every program.
1c9fa 53 6f 20 61 20 6a 75 6d 70 20 70 61 73 74 20 74 So a jump past t
1c9fb 68 65 20 6c 61 73 74 20 69 6e 73 74 72 75 63 74 he last instruct
1c9fc 69 6f 6e 20 6f 66 20 74 68 65 20 70 72 6f 67 72 ion of the progr
1c9fd 61 6d 0a 2a 2a 20 69 73 20 74 68 65 20 73 61 6d am.** is the sam
1c9fe 65 20 61 73 20 65 78 65 63 75 74 69 6e 67 20 48 e as executing H
1c9ff 61 6c 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f alt..*/.case OP_
1ca00 48 61 6c 74 3a 20 7b 0a 20 20 69 66 28 20 70 4f Halt: {. if( pO
1ca01 70 2d 3e 70 31 3d 3d 53 51 4c 49 54 45 5f 4f 4b p->p1==SQLITE_OK
1ca02 20 26 26 20 70 2d 3e 70 46 72 61 6d 65 20 29 7b && p->pFrame ){
1ca03 0a 20 20 20 20 2f 2a 20 48 61 6c 74 20 74 68 65 . /* Halt the
1ca04 20 73 75 62 2d 70 72 6f 67 72 61 6d 2e 20 52 65 sub-program. Re
1ca05 74 75 72 6e 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 turn control to
1ca06 74 68 65 20 70 61 72 65 6e 74 20 66 72 61 6d 65 the parent frame
1ca07 2e 20 2a 2f 0a 20 20 20 20 56 64 62 65 46 72 61 . */. VdbeFra
1ca08 6d 65 20 2a 70 46 72 61 6d 65 20 3d 20 70 2d 3e me *pFrame = p->
1ca09 70 46 72 61 6d 65 3b 0a 20 20 20 20 70 2d 3e 70 pFrame;. p->p
1ca0a 46 72 61 6d 65 20 3d 20 70 46 72 61 6d 65 2d 3e Frame = pFrame->
1ca0b 70 50 61 72 65 6e 74 3b 0a 20 20 20 20 70 2d 3e pParent;. p->
1ca0c 6e 46 72 61 6d 65 2d 2d 3b 0a 20 20 20 20 73 71 nFrame--;. sq
1ca0d 6c 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e lite3VdbeSetChan
1ca0e 67 65 73 28 64 62 2c 20 70 2d 3e 6e 43 68 61 6e ges(db, p->nChan
1ca0f 67 65 29 3b 0a 20 20 20 20 70 63 20 3d 20 73 71 ge);. pc = sq
1ca10 6c 69 74 65 33 56 64 62 65 46 72 61 6d 65 52 65 lite3VdbeFrameRe
1ca11 73 74 6f 72 65 28 70 46 72 61 6d 65 29 3b 0a 20 store(pFrame);.
1ca12 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d if( pOp->p2==
1ca13 4f 45 5f 49 67 6e 6f 72 65 20 29 7b 0a 20 20 20 OE_Ignore ){.
1ca14 20 20 20 2f 2a 20 49 6e 73 74 72 75 63 74 69 6f /* Instructio
1ca15 6e 20 70 63 20 69 73 20 74 68 65 20 4f 50 5f 50 n pc is the OP_P
1ca16 72 6f 67 72 61 6d 20 74 68 61 74 20 69 6e 76 6f rogram that invo
1ca17 6b 65 64 20 74 68 65 20 73 75 62 2d 70 72 6f 67 ked the sub-prog
1ca18 72 61 6d 20 0a 20 20 20 20 20 20 2a 2a 20 63 75 ram . ** cu
1ca19 72 72 65 6e 74 6c 79 20 62 65 69 6e 67 20 68 61 rrently being ha
1ca1a 6c 74 65 64 2e 20 49 66 20 74 68 65 20 70 32 20 lted. If the p2
1ca1b 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66 20 74 instruction of t
1ca1c 68 69 73 20 4f 50 5f 48 61 6c 74 0a 20 20 20 20 his OP_Halt.
1ca1d 20 20 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e ** instruction
1ca1e 20 69 73 20 73 65 74 20 74 6f 20 4f 45 5f 49 67 is set to OE_Ig
1ca1f 6e 6f 72 65 2c 20 74 68 65 6e 20 74 68 65 20 73 nore, then the s
1ca20 75 62 2d 70 72 6f 67 72 61 6d 20 69 73 20 74 68 ub-program is th
1ca21 72 6f 77 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 rowing. **
1ca22 61 6e 20 49 47 4e 4f 52 45 20 65 78 63 65 70 74 an IGNORE except
1ca23 69 6f 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 73 ion. In this cas
1ca24 65 20 6a 75 6d 70 20 74 6f 20 74 68 65 20 61 64 e jump to the ad
1ca25 64 72 65 73 73 20 73 70 65 63 69 66 69 65 64 0a dress specified.
1ca26 20 20 20 20 20 20 2a 2a 20 61 73 20 74 68 65 20 ** as the
1ca27 70 32 20 6f 66 20 74 68 65 20 63 61 6c 6c 69 6e p2 of the callin
1ca28 67 20 4f 50 5f 50 72 6f 67 72 61 6d 2e 20 20 2a g OP_Program. *
1ca29 2f 0a 20 20 20 20 20 20 70 63 20 3d 20 70 2d 3e /. pc = p->
1ca2a 61 4f 70 5b 70 63 5d 2e 70 32 2d 31 3b 0a 20 20 aOp[pc].p2-1;.
1ca2b 20 20 7d 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 }. break;.
1ca2c 20 7d 0a 0a 20 20 70 2d 3e 72 63 20 3d 20 70 4f }.. p->rc = pO
1ca2d 70 2d 3e 70 31 3b 0a 20 20 70 2d 3e 65 72 72 6f p->p1;. p->erro
1ca2e 72 41 63 74 69 6f 6e 20 3d 20 28 75 38 29 70 4f rAction = (u8)pO
1ca2f 70 2d 3e 70 32 3b 0a 20 20 70 2d 3e 70 63 20 3d p->p2;. p->pc =
1ca30 20 70 63 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e pc;. if( pOp->
1ca31 70 34 2e 7a 20 29 7b 0a 20 20 20 20 73 71 6c 69 p4.z ){. sqli
1ca32 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d te3SetString(&p-
1ca33 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 >zErrMsg, db, "%
1ca34 73 22 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a s", pOp->p4.z);.
1ca35 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 }. rc = sqlit
1ca36 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b 0a 20 e3VdbeHalt(p);.
1ca37 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c assert( rc==SQL
1ca38 49 54 45 5f 42 55 53 59 20 7c 7c 20 72 63 3d 3d ITE_BUSY || rc==
1ca39 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d SQLITE_OK || rc=
1ca3a 3d 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 29 3b =SQLITE_ERROR );
1ca3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
1ca3c 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 70 2d E_BUSY ){. p-
1ca3d 3e 72 63 20 3d 20 72 63 20 3d 20 53 51 4c 49 54 >rc = rc = SQLIT
1ca3e 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73 65 7b E_BUSY;. }else{
1ca3f 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d . assert( rc=
1ca40 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d =SQLITE_OK || p-
1ca41 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 >rc==SQLITE_CONS
1ca42 54 52 41 49 4e 54 20 29 3b 0a 20 20 20 20 61 73 TRAINT );. as
1ca43 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 sert( rc==SQLITE
1ca44 5f 4f 4b 20 7c 7c 20 64 62 2d 3e 6e 44 65 66 65 _OK || db->nDefe
1ca45 72 72 65 64 43 6f 6e 73 3e 30 20 29 3b 0a 20 20 rredCons>0 );.
1ca46 20 20 72 63 20 3d 20 70 2d 3e 72 63 20 3f 20 53 rc = p->rc ? S
1ca47 51 4c 49 54 45 5f 45 52 52 4f 52 20 3a 20 53 51 QLITE_ERROR : SQ
1ca48 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 20 LITE_DONE;. }.
1ca49 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 goto vdbe_retur
1ca4a 6e 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a n;.}../* Opcode:
1ca4b 20 49 6e 74 65 67 65 72 20 50 31 20 50 32 20 2a Integer P1 P2 *
1ca4c 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 33 * *.**.** The 3
1ca4d 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 76 61 2-bit integer va
1ca4e 6c 75 65 20 50 31 20 69 73 20 77 72 69 74 74 65 lue P1 is writte
1ca4f 6e 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 n into register
1ca50 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 P2..*/.case OP_I
1ca51 6e 74 65 67 65 72 3a 20 7b 20 20 20 20 20 20 20 nteger: {
1ca52 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c /* out2-prerel
1ca53 65 61 73 65 20 2a 2f 0a 20 20 70 4f 75 74 2d 3e ease */. pOut->
1ca54 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b flags = MEM_Int;
1ca55 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 70 . pOut->u.i = p
1ca56 4f 70 2d 3e 70 31 3b 0a 20 20 62 72 65 61 6b 3b Op->p1;. break;
1ca57 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 .}../* Opcode: I
1ca58 6e 74 36 34 20 2a 20 50 32 20 2a 20 50 34 20 2a nt64 * P2 * P4 *
1ca59 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 .**.** P4 is a p
1ca5a 6f 69 6e 74 65 72 20 74 6f 20 61 20 36 34 2d 62 ointer to a 64-b
1ca5b 69 74 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 it integer value
1ca5c 2e 0a 2a 2a 20 57 72 69 74 65 20 74 68 61 74 20 ..** Write that
1ca5d 76 61 6c 75 65 20 69 6e 74 6f 20 72 65 67 69 73 value into regis
1ca5e 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 ter P2..*/.case
1ca5f 4f 50 5f 49 6e 74 36 34 3a 20 7b 20 20 20 20 20 OP_Int64: {
1ca60 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 /* out2-pr
1ca61 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73 erelease */. as
1ca62 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 70 49 sert( pOp->p4.pI
1ca63 36 34 21 3d 30 20 29 3b 0a 20 20 70 4f 75 74 2d 64!=0 );. pOut-
1ca64 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 >flags = MEM_Int
1ca65 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 ;. pOut->u.i =
1ca66 2a 70 4f 70 2d 3e 70 34 2e 70 49 36 34 3b 0a 20 *pOp->p4.pI64;.
1ca67 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1ca68 63 6f 64 65 3a 20 52 65 61 6c 20 2a 20 50 32 20 code: Real * P2
1ca69 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 * P4 *.**.** P4
1ca6a 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
1ca6b 61 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 69 6e a 64-bit floatin
1ca6c 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 2e 0a 2a g point value..*
1ca6d 2a 20 57 72 69 74 65 20 74 68 61 74 20 76 61 6c * Write that val
1ca6e 75 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 ue into register
1ca6f 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f P2..*/.case OP_
1ca70 52 65 61 6c 3a 20 7b 20 20 20 20 20 20 20 20 20 Real: {
1ca71 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
1ca72 5f 46 4c 4f 41 54 2c 20 6f 75 74 32 2d 70 72 65 _FLOAT, out2-pre
1ca73 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 70 4f 75 release */. pOu
1ca74 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 52 t->flags = MEM_R
1ca75 65 61 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 21 eal;. assert( !
1ca76 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 2a 70 4f sqlite3IsNaN(*pO
1ca77 70 2d 3e 70 34 2e 70 52 65 61 6c 29 20 29 3b 0a p->p4.pReal) );.
1ca78 20 20 70 4f 75 74 2d 3e 72 20 3d 20 2a 70 4f 70 pOut->r = *pOp
1ca79 2d 3e 70 34 2e 70 52 65 61 6c 3b 0a 20 20 62 72 ->p4.pReal;. br
1ca7a 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1ca7b 65 3a 20 53 74 72 69 6e 67 38 20 2a 20 50 32 20 e: String8 * P2
1ca7c 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 * P4 *.**.** P4
1ca7d 70 6f 69 6e 74 73 20 74 6f 20 61 20 6e 75 6c 20 points to a nul
1ca7e 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 terminated UTF-8
1ca7f 20 73 74 72 69 6e 67 2e 20 54 68 69 73 20 6f 70 string. This op
1ca80 63 6f 64 65 20 69 73 20 74 72 61 6e 73 66 6f 72 code is transfor
1ca81 6d 65 64 20 0a 2a 2a 20 69 6e 74 6f 20 61 6e 20 med .** into an
1ca82 4f 50 5f 53 74 72 69 6e 67 20 62 65 66 6f 72 65 OP_String before
1ca83 20 69 74 20 69 73 20 65 78 65 63 75 74 65 64 20 it is executed
1ca84 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 74 69 for the first ti
1ca85 6d 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 me..*/.case OP_S
1ca86 74 72 69 6e 67 38 3a 20 7b 20 20 20 20 20 20 20 tring8: {
1ca87 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
1ca88 53 54 52 49 4e 47 2c 20 6f 75 74 32 2d 70 72 65 STRING, out2-pre
1ca89 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73 73 release */. ass
1ca8a 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d ert( pOp->p4.z!=
1ca8b 30 20 29 3b 0a 20 20 70 4f 70 2d 3e 6f 70 63 6f 0 );. pOp->opco
1ca8c 64 65 20 3d 20 4f 50 5f 53 74 72 69 6e 67 3b 0a de = OP_String;.
1ca8d 20 20 70 4f 70 2d 3e 70 31 20 3d 20 73 71 6c 69 pOp->p1 = sqli
1ca8e 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4f 70 2d te3Strlen30(pOp-
1ca8f 3e 70 34 2e 7a 29 3b 0a 0a 23 69 66 6e 64 65 66 >p4.z);..#ifndef
1ca90 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 SQLITE_OMIT_UTF
1ca91 31 36 0a 20 20 69 66 28 20 65 6e 63 6f 64 69 6e 16. if( encodin
1ca92 67 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29 g!=SQLITE_UTF8 )
1ca93 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
1ca94 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 e3VdbeMemSetStr(
1ca95 70 4f 75 74 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c pOut, pOp->p4.z,
1ca96 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 -1, SQLITE_UTF8
1ca97 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 , SQLITE_STATIC)
1ca98 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 ;. if( rc==SQ
1ca99 4c 49 54 45 5f 54 4f 4f 42 49 47 20 29 20 67 6f LITE_TOOBIG ) go
1ca9a 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 to too_big;.
1ca9b 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 if( SQLITE_OK!=s
1ca9c 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
1ca9d 45 6e 63 6f 64 69 6e 67 28 70 4f 75 74 2c 20 65 Encoding(pOut, e
1ca9e 6e 63 6f 64 69 6e 67 29 20 29 20 67 6f 74 6f 20 ncoding) ) goto
1ca9f 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 61 73 73 65 no_mem;. asse
1caa0 72 74 28 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f rt( pOut->zMallo
1caa1 63 3d 3d 70 4f 75 74 2d 3e 7a 20 29 3b 0a 20 20 c==pOut->z );.
1caa2 20 20 61 73 73 65 72 74 28 20 70 4f 75 74 2d 3e assert( pOut->
1caa3 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 20 flags & MEM_Dyn
1caa4 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 4d 61 );. pOut->zMa
1caa5 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 70 4f lloc = 0;. pO
1caa6 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d ut->flags |= MEM
1caa7 5f 53 74 61 74 69 63 3b 0a 20 20 20 20 70 4f 75 _Static;. pOu
1caa8 74 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d t->flags &= ~MEM
1caa9 5f 44 79 6e 3b 0a 20 20 20 20 69 66 28 20 70 4f _Dyn;. if( pO
1caaa 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 44 59 p->p4type==P4_DY
1caab 4e 41 4d 49 43 20 29 7b 0a 20 20 20 20 20 20 73 NAMIC ){. s
1caac 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1caad 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20 pOp->p4.z);.
1caae 20 7d 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 }. pOp->p4ty
1caaf 70 65 20 3d 20 50 34 5f 44 59 4e 41 4d 49 43 3b pe = P4_DYNAMIC;
1cab0 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 7a 20 3d . pOp->p4.z =
1cab1 20 70 4f 75 74 2d 3e 7a 3b 0a 20 20 20 20 70 4f pOut->z;. pO
1cab2 70 2d 3e 70 31 20 3d 20 70 4f 75 74 2d 3e 6e 3b p->p1 = pOut->n;
1cab3 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 . }.#endif. if
1cab4 28 20 70 4f 70 2d 3e 70 31 3e 64 62 2d 3e 61 4c ( pOp->p1>db->aL
1cab5 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 imit[SQLITE_LIMI
1cab6 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 T_LENGTH] ){.
1cab7 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 goto too_big;.
1cab8 20 7d 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 }. /* Fall thr
1cab9 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 ough to the next
1caba 20 63 61 73 65 2c 20 4f 50 5f 53 74 72 69 6e 67 case, OP_String
1cabb 20 2a 2f 0a 7d 0a 20 20 0a 2f 2a 20 4f 70 63 6f */.}. ./* Opco
1cabc 64 65 3a 20 53 74 72 69 6e 67 20 50 31 20 50 32 de: String P1 P2
1cabd 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 * P4 *.**.** Th
1cabe 65 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 50 e string value P
1cabf 34 20 6f 66 20 6c 65 6e 67 74 68 20 50 31 20 28 4 of length P1 (
1cac0 62 79 74 65 73 29 20 69 73 20 73 74 6f 72 65 64 bytes) is stored
1cac1 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e in register P2.
1cac2 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 74 72 69 .*/.case OP_Stri
1cac3 6e 67 3a 20 7b 20 20 20 20 20 20 20 20 20 20 2f ng: { /
1cac4 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 * out2-prereleas
1cac5 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 e */. assert( p
1cac6 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 3b 0a 20 Op->p4.z!=0 );.
1cac7 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d pOut->flags = M
1cac8 45 4d 5f 53 74 72 7c 4d 45 4d 5f 53 74 61 74 69 EM_Str|MEM_Stati
1cac9 63 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 70 4f c|MEM_Term;. pO
1caca 75 74 2d 3e 7a 20 3d 20 70 4f 70 2d 3e 70 34 2e ut->z = pOp->p4.
1cacb 7a 3b 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 70 z;. pOut->n = p
1cacc 4f 70 2d 3e 70 31 3b 0a 20 20 70 4f 75 74 2d 3e Op->p1;. pOut->
1cacd 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a enc = encoding;.
1cace 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f UPDATE_MAX_BLO
1cacf 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 BSIZE(pOut);. b
1cad0 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1cad1 64 65 3a 20 4e 75 6c 6c 20 2a 20 50 32 20 2a 20 de: Null * P2 *
1cad2 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 * *.**.** Write
1cad3 61 20 4e 55 4c 4c 20 69 6e 74 6f 20 72 65 67 69 a NULL into regi
1cad4 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 ster P2..*/.case
1cad5 20 4f 50 5f 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 OP_Null: {
1cad6 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 /* out2-pr
1cad7 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 62 72 erelease */. br
1cad8 65 61 6b 3b 0a 7d 0a 0a 0a 2f 2a 20 4f 70 63 6f eak;.}.../* Opco
1cad9 64 65 3a 20 42 6c 6f 62 20 50 31 20 50 32 20 2a de: Blob P1 P2 *
1cada 20 50 34 0a 2a 2a 0a 2a 2a 20 50 34 20 70 6f 69 P4.**.** P4 poi
1cadb 6e 74 73 20 74 6f 20 61 20 62 6c 6f 62 20 6f 66 nts to a blob of
1cadc 20 64 61 74 61 20 50 31 20 62 79 74 65 73 20 6c data P1 bytes l
1cadd 6f 6e 67 2e 20 20 53 74 6f 72 65 20 74 68 69 73 ong. Store this
1cade 0a 2a 2a 20 62 6c 6f 62 20 69 6e 20 72 65 67 69 .** blob in regi
1cadf 73 74 65 72 20 50 32 2e 20 54 68 69 73 20 69 6e ster P2. This in
1cae0 73 74 72 75 63 74 69 6f 6e 20 69 73 20 6e 6f 74 struction is not
1cae1 20 63 6f 64 65 64 20 64 69 72 65 63 74 6c 79 0a coded directly.
1cae2 2a 2a 20 62 79 20 74 68 65 20 63 6f 6d 70 69 6c ** by the compil
1cae3 65 72 2e 20 49 6e 73 74 65 61 64 2c 20 74 68 65 er. Instead, the
1cae4 20 63 6f 6d 70 69 6c 65 72 20 6c 61 79 65 72 20 compiler layer
1cae5 73 70 65 63 69 66 69 65 73 0a 2a 2a 20 61 6e 20 specifies.** an
1cae6 4f 50 5f 48 65 78 42 6c 6f 62 20 6f 70 63 6f 64 OP_HexBlob opcod
1cae7 65 2c 20 77 69 74 68 20 74 68 65 20 68 65 78 20 e, with the hex
1cae8 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 string represent
1cae9 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68 65 20 ation of.** the
1caea 62 6c 6f 62 20 61 73 20 50 34 2e 20 54 68 69 73 blob as P4. This
1caeb 20 6f 70 63 6f 64 65 20 69 73 20 74 72 61 6e 73 opcode is trans
1caec 66 6f 72 6d 65 64 20 74 6f 20 61 6e 20 4f 50 5f formed to an OP_
1caed 42 6c 6f 62 0a 2a 2a 20 74 68 65 20 66 69 72 73 Blob.** the firs
1caee 74 20 74 69 6d 65 20 69 74 20 69 73 20 65 78 65 t time it is exe
1caef 63 75 74 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f cuted..*/.case O
1caf0 50 5f 42 6c 6f 62 3a 20 7b 20 20 20 20 20 20 20 P_Blob: {
1caf1 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 /* out2
1caf2 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 -prerelease */.
1caf3 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 assert( pOp->p1
1caf4 20 3c 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c <= SQLITE_MAX_L
1caf5 45 4e 47 54 48 20 29 3b 0a 20 20 73 71 6c 69 74 ENGTH );. sqlit
1caf6 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 e3VdbeMemSetStr(
1caf7 70 4f 75 74 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c pOut, pOp->p4.z,
1caf8 20 70 4f 70 2d 3e 70 31 2c 20 30 2c 20 30 29 3b pOp->p1, 0, 0);
1caf9 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 65 . pOut->enc = e
1cafa 6e 63 6f 64 69 6e 67 3b 0a 20 20 55 50 44 41 54 ncoding;. UPDAT
1cafb 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 E_MAX_BLOBSIZE(p
1cafc 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d Out);. break;.}
1cafd 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 61 72 ../* Opcode: Var
1cafe 69 61 62 6c 65 20 50 31 20 50 32 20 50 33 20 50 iable P1 P2 P3 P
1caff 34 20 2a 0a 2a 2a 0a 2a 2a 20 54 72 61 6e 73 66 4 *.**.** Transf
1cb00 65 72 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 er the values of
1cb01 20 62 6f 75 6e 64 20 70 61 72 61 6d 65 74 65 72 bound parameter
1cb02 73 20 50 31 2e 2e 50 31 2b 50 33 2d 31 20 69 6e s P1..P1+P3-1 in
1cb03 74 6f 20 72 65 67 69 73 74 65 72 73 0a 2a 2a 20 to registers.**
1cb04 50 32 2e 2e 50 32 2b 50 33 2d 31 2e 0a 2a 2a 0a P2..P2+P3-1..**.
1cb05 2a 2a 20 49 66 20 74 68 65 20 70 61 72 61 6d 65 ** If the parame
1cb06 74 65 72 20 69 73 20 6e 61 6d 65 64 2c 20 74 68 ter is named, th
1cb07 65 6e 20 69 74 73 20 6e 61 6d 65 20 61 70 70 65 en its name appe
1cb08 61 72 73 20 69 6e 20 50 34 20 61 6e 64 20 50 33 ars in P4 and P3
1cb09 3d 3d 31 2e 0a 2a 2a 20 54 68 65 20 50 34 20 76 ==1..** The P4 v
1cb0a 61 6c 75 65 20 69 73 20 75 73 65 64 20 62 79 20 alue is used by
1cb0b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 sqlite3_bind_par
1cb0c 61 6d 65 74 65 72 5f 6e 61 6d 65 28 29 2e 0a 2a ameter_name()..*
1cb0d 2f 0a 63 61 73 65 20 4f 50 5f 56 61 72 69 61 62 /.case OP_Variab
1cb0e 6c 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 le: {.#if 0 /*
1cb0f 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1cb10 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 62 20 moved into u.ab
1cb11 2a 2f 0a 20 20 69 6e 74 20 70 31 3b 20 20 20 20 */. int p1;
1cb12 20 20 20 20 20 20 2f 2a 20 56 61 72 69 61 62 6c /* Variabl
1cb13 65 20 74 6f 20 63 6f 70 79 20 66 72 6f 6d 20 2a e to copy from *
1cb14 2f 0a 20 20 69 6e 74 20 70 32 3b 20 20 20 20 20 /. int p2;
1cb15 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 /* Register
1cb16 20 74 6f 20 63 6f 70 79 20 74 6f 20 2a 2f 0a 20 to copy to */.
1cb17 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20 int n;
1cb18 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 76 /* Number of v
1cb19 61 6c 75 65 73 20 6c 65 66 74 20 74 6f 20 63 6f alues left to co
1cb1a 70 79 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 56 61 py */. Mem *pVa
1cb1b 72 3b 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 r; /* Valu
1cb1c 65 20 62 65 69 6e 67 20 74 72 61 6e 73 66 65 72 e being transfer
1cb1d 72 65 64 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a red */.#endif /*
1cb1e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1cb1f 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 62 moved into u.ab
1cb20 20 2a 2f 0a 0a 20 20 75 2e 61 62 2e 70 31 20 3d */.. u.ab.p1 =
1cb21 20 70 4f 70 2d 3e 70 31 20 2d 20 31 3b 0a 20 20 pOp->p1 - 1;.
1cb22 75 2e 61 62 2e 70 32 20 3d 20 70 4f 70 2d 3e 70 u.ab.p2 = pOp->p
1cb23 32 3b 0a 20 20 75 2e 61 62 2e 6e 20 3d 20 70 4f 2;. u.ab.n = pO
1cb24 70 2d 3e 70 33 3b 0a 20 20 61 73 73 65 72 74 28 p->p3;. assert(
1cb25 20 75 2e 61 62 2e 70 31 3e 3d 30 20 26 26 20 75 u.ab.p1>=0 && u
1cb26 2e 61 62 2e 70 31 2b 75 2e 61 62 2e 6e 3c 3d 70 .ab.p1+u.ab.n<=p
1cb27 2d 3e 6e 56 61 72 20 29 3b 0a 20 20 61 73 73 65 ->nVar );. asse
1cb28 72 74 28 20 75 2e 61 62 2e 70 32 3e 3d 31 20 26 rt( u.ab.p2>=1 &
1cb29 26 20 75 2e 61 62 2e 70 32 2b 75 2e 61 62 2e 6e & u.ab.p2+u.ab.n
1cb2a 2d 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 -1<=p->nMem );.
1cb2b 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 assert( pOp->p4
1cb2c 2e 7a 3d 3d 30 20 7c 7c 20 70 4f 70 2d 3e 70 33 .z==0 || pOp->p3
1cb2d 3d 3d 31 20 7c 7c 20 70 4f 70 2d 3e 70 33 3d 3d ==1 || pOp->p3==
1cb2e 30 20 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20 75 0 );.. while( u
1cb2f 2e 61 62 2e 6e 2d 2d 20 3e 20 30 20 29 7b 0a 20 .ab.n-- > 0 ){.
1cb30 20 20 20 75 2e 61 62 2e 70 56 61 72 20 3d 20 26 u.ab.pVar = &
1cb31 70 2d 3e 61 56 61 72 5b 75 2e 61 62 2e 70 31 2b p->aVar[u.ab.p1+
1cb32 2b 5d 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 +];. if( sqli
1cb33 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 te3VdbeMemTooBig
1cb34 28 75 2e 61 62 2e 70 56 61 72 29 20 29 7b 0a 20 (u.ab.pVar) ){.
1cb35 20 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 goto too_bi
1cb36 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4f 75 g;. }. pOu
1cb37 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e 61 t = &p->aMem[u.a
1cb38 62 2e 70 32 2b 2b 5d 3b 0a 20 20 20 20 73 71 6c b.p2++];. sql
1cb39 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
1cb3a 73 65 45 78 74 65 72 6e 61 6c 28 70 4f 75 74 29 seExternal(pOut)
1cb3b 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 ;. pOut->flag
1cb3c 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 s = MEM_Null;.
1cb3d 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1cb3e 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70 4f 75 74 ShallowCopy(pOut
1cb3f 2c 20 75 2e 61 62 2e 70 56 61 72 2c 20 4d 45 4d , u.ab.pVar, MEM
1cb40 5f 53 74 61 74 69 63 29 3b 0a 20 20 20 20 55 50 _Static);. UP
1cb41 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a DATE_MAX_BLOBSIZ
1cb42 45 28 70 4f 75 74 29 3b 0a 20 20 7d 0a 20 20 62 E(pOut);. }. b
1cb43 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1cb44 64 65 3a 20 4d 6f 76 65 20 50 31 20 50 32 20 50 de: Move P1 P2 P
1cb45 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 6f 76 65 3 * *.**.** Move
1cb46 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e 20 72 the values in r
1cb47 65 67 69 73 74 65 72 20 50 31 2e 2e 50 31 2b 50 egister P1..P1+P
1cb48 33 2d 31 20 6f 76 65 72 20 69 6e 74 6f 0a 2a 2a 3-1 over into.**
1cb49 20 72 65 67 69 73 74 65 72 73 20 50 32 2e 2e 50 registers P2..P
1cb4a 32 2b 50 33 2d 31 2e 20 20 52 65 67 69 73 74 65 2+P3-1. Registe
1cb4b 72 73 20 50 31 2e 2e 50 31 2b 50 31 2d 31 20 61 rs P1..P1+P1-1 a
1cb4c 72 65 0a 2a 2a 20 6c 65 66 74 20 68 6f 6c 64 69 re.** left holdi
1cb4d 6e 67 20 61 20 4e 55 4c 4c 2e 20 20 49 74 20 69 ng a NULL. It i
1cb4e 73 20 61 6e 20 65 72 72 6f 72 20 66 6f 72 20 72 s an error for r
1cb4f 65 67 69 73 74 65 72 20 72 61 6e 67 65 73 0a 2a egister ranges.*
1cb50 2a 20 50 31 2e 2e 50 31 2b 50 33 2d 31 20 61 6e * P1..P1+P3-1 an
1cb51 64 20 50 32 2e 2e 50 32 2b 50 33 2d 31 20 74 6f d P2..P2+P3-1 to
1cb52 20 6f 76 65 72 6c 61 70 2e 0a 2a 2f 0a 63 61 73 overlap..*/.cas
1cb53 65 20 4f 50 5f 4d 6f 76 65 3a 20 7b 0a 23 69 66 e OP_Move: {.#if
1cb54 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 0 /* local var
1cb55 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1cb56 6f 20 75 2e 61 63 20 2a 2f 0a 20 20 63 68 61 72 o u.ac */. char
1cb57 20 2a 7a 4d 61 6c 6c 6f 63 3b 20 20 20 2f 2a 20 *zMalloc; /*
1cb58 48 6f 6c 64 69 6e 67 20 76 61 72 69 61 62 6c 65 Holding variable
1cb59 20 66 6f 72 20 61 6c 6c 6f 63 61 74 65 64 20 6d for allocated m
1cb5a 65 6d 6f 72 79 20 2a 2f 0a 20 20 69 6e 74 20 6e emory */. int n
1cb5b 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e ; /* N
1cb5c 75 6d 62 65 72 20 6f 66 20 72 65 67 69 73 74 65 umber of registe
1cb5d 72 73 20 6c 65 66 74 20 74 6f 20 63 6f 70 79 20 rs left to copy
1cb5e 2a 2f 0a 20 20 69 6e 74 20 70 31 3b 20 20 20 20 */. int p1;
1cb5f 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 /* Registe
1cb60 72 20 74 6f 20 63 6f 70 79 20 66 72 6f 6d 20 2a r to copy from *
1cb61 2f 0a 20 20 69 6e 74 20 70 32 3b 20 20 20 20 20 /. int p2;
1cb62 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 /* Register
1cb63 20 74 6f 20 63 6f 70 79 20 74 6f 20 2a 2f 0a 23 to copy to */.#
1cb64 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 endif /* local v
1cb65 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1cb66 6e 74 6f 20 75 2e 61 63 20 2a 2f 0a 0a 20 20 75 nto u.ac */.. u
1cb67 2e 61 63 2e 6e 20 3d 20 70 4f 70 2d 3e 70 33 3b .ac.n = pOp->p3;
1cb68 0a 20 20 75 2e 61 63 2e 70 31 20 3d 20 70 4f 70 . u.ac.p1 = pOp
1cb69 2d 3e 70 31 3b 0a 20 20 75 2e 61 63 2e 70 32 20 ->p1;. u.ac.p2
1cb6a 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 61 73 73 = pOp->p2;. ass
1cb6b 65 72 74 28 20 75 2e 61 63 2e 6e 3e 30 20 26 26 ert( u.ac.n>0 &&
1cb6c 20 75 2e 61 63 2e 70 31 3e 30 20 26 26 20 75 2e u.ac.p1>0 && u.
1cb6d 61 63 2e 70 32 3e 30 20 29 3b 0a 20 20 61 73 73 ac.p2>0 );. ass
1cb6e 65 72 74 28 20 75 2e 61 63 2e 70 31 2b 75 2e 61 ert( u.ac.p1+u.a
1cb6f 63 2e 6e 3c 3d 75 2e 61 63 2e 70 32 20 7c 7c 20 c.n<=u.ac.p2 ||
1cb70 75 2e 61 63 2e 70 32 2b 75 2e 61 63 2e 6e 3c 3d u.ac.p2+u.ac.n<=
1cb71 75 2e 61 63 2e 70 31 20 29 3b 0a 0a 20 20 70 49 u.ac.p1 );.. pI
1cb72 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e n1 = &p->aMem[u.
1cb73 61 63 2e 70 31 5d 3b 0a 20 20 70 4f 75 74 20 3d ac.p1];. pOut =
1cb74 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e 61 63 2e 70 &p->aMem[u.ac.p
1cb75 32 5d 3b 0a 20 20 77 68 69 6c 65 28 20 75 2e 61 2];. while( u.a
1cb76 63 2e 6e 2d 2d 20 29 7b 0a 20 20 20 20 61 73 73 c.n-- ){. ass
1cb77 65 72 74 28 20 70 4f 75 74 3c 3d 26 70 2d 3e 61 ert( pOut<=&p->a
1cb78 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 5d 20 29 3b 0a Mem[p->nMem] );.
1cb79 20 20 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 assert( pIn1
1cb7a 3c 3d 26 70 2d 3e 61 4d 65 6d 5b 70 2d 3e 6e 4d <=&p->aMem[p->nM
1cb7b 65 6d 5d 20 29 3b 0a 20 20 20 20 75 2e 61 63 2e em] );. u.ac.
1cb7c 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4f 75 74 2d 3e zMalloc = pOut->
1cb7d 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 20 20 70 4f 75 zMalloc;. pOu
1cb7e 74 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a t->zMalloc = 0;.
1cb7f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1cb80 65 6d 4d 6f 76 65 28 70 4f 75 74 2c 20 70 49 6e emMove(pOut, pIn
1cb81 31 29 3b 0a 20 20 20 20 70 49 6e 31 2d 3e 7a 4d 1);. pIn1->zM
1cb82 61 6c 6c 6f 63 20 3d 20 75 2e 61 63 2e 7a 4d 61 alloc = u.ac.zMa
1cb83 6c 6c 6f 63 3b 0a 20 20 20 20 52 45 47 49 53 54 lloc;. REGIST
1cb84 45 52 5f 54 52 41 43 45 28 75 2e 61 63 2e 70 32 ER_TRACE(u.ac.p2
1cb85 2b 2b 2c 20 70 4f 75 74 29 3b 0a 20 20 20 20 70 ++, pOut);. p
1cb86 49 6e 31 2b 2b 3b 0a 20 20 20 20 70 4f 75 74 2b In1++;. pOut+
1cb87 2b 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a +;. }. break;.
1cb88 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f }../* Opcode: Co
1cb89 70 79 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a py P1 P2 * * *.*
1cb8a 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 63 6f 70 79 *.** Make a copy
1cb8b 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 20 of register P1
1cb8c 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 into register P2
1cb8d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 ..**.** This ins
1cb8e 74 72 75 63 74 69 6f 6e 20 6d 61 6b 65 73 20 61 truction makes a
1cb8f 20 64 65 65 70 20 63 6f 70 79 20 6f 66 20 74 68 deep copy of th
1cb90 65 20 76 61 6c 75 65 2e 20 20 41 20 64 75 70 6c e value. A dupl
1cb91 69 63 61 74 65 0a 2a 2a 20 69 73 20 6d 61 64 65 icate.** is made
1cb92 20 6f 66 20 61 6e 79 20 73 74 72 69 6e 67 20 6f of any string o
1cb93 72 20 62 6c 6f 62 20 63 6f 6e 73 74 61 6e 74 2e r blob constant.
1cb94 20 20 53 65 65 20 61 6c 73 6f 20 4f 50 5f 53 43 See also OP_SC
1cb95 6f 70 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f opy..*/.case OP_
1cb96 43 6f 70 79 3a 20 7b 20 20 20 20 20 20 20 20 20 Copy: {
1cb97 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 /* in1 */.
1cb98 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e assert( pOp->p2>
1cb99 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
1cb9a 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 Op->p2<=p->nMem
1cb9b 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e );. pOut = &p->
1cb9c 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 aMem[pOp->p2];.
1cb9d 20 61 73 73 65 72 74 28 20 70 4f 75 74 21 3d 70 assert( pOut!=p
1cb9e 49 6e 31 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 In1 );. sqlite3
1cb9f 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f VdbeMemShallowCo
1cba0 70 79 28 70 4f 75 74 2c 20 70 49 6e 31 2c 20 4d py(pOut, pIn1, M
1cba1 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 44 65 65 EM_Ephem);. Dee
1cba2 70 68 65 6d 65 72 61 6c 69 7a 65 28 70 4f 75 74 phemeralize(pOut
1cba3 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 );. REGISTER_TR
1cba4 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f 75 ACE(pOp->p2, pOu
1cba5 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a t);. break;.}..
1cba6 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 43 6f 70 79 /* Opcode: SCopy
1cba7 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a P1 P2 * * *.**.
1cba8 2a 2a 20 4d 61 6b 65 20 61 20 73 68 61 6c 6c 6f ** Make a shallo
1cba9 77 20 63 6f 70 79 20 6f 66 20 72 65 67 69 73 74 w copy of regist
1cbaa 65 72 20 50 31 20 69 6e 74 6f 20 72 65 67 69 73 er P1 into regis
1cbab 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 ter P2..**.** Th
1cbac 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6d is instruction m
1cbad 61 6b 65 73 20 61 20 73 68 61 6c 6c 6f 77 20 63 akes a shallow c
1cbae 6f 70 79 20 6f 66 20 74 68 65 20 76 61 6c 75 65 opy of the value
1cbaf 2e 20 20 49 66 20 74 68 65 20 76 61 6c 75 65 0a . If the value.
1cbb0 2a 2a 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f ** is a string o
1cbb1 72 20 62 6c 6f 62 2c 20 74 68 65 6e 20 74 68 65 r blob, then the
1cbb2 20 63 6f 70 79 20 69 73 20 6f 6e 6c 79 20 61 20 copy is only a
1cbb3 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 2a pointer to the.*
1cbb4 2a 20 6f 72 69 67 69 6e 61 6c 20 61 6e 64 20 68 * original and h
1cbb5 65 6e 63 65 20 69 66 20 74 68 65 20 6f 72 69 67 ence if the orig
1cbb6 69 6e 61 6c 20 63 68 61 6e 67 65 73 20 73 6f 20 inal changes so
1cbb7 77 69 6c 6c 20 74 68 65 20 63 6f 70 79 2e 0a 2a will the copy..*
1cbb8 2a 20 57 6f 72 73 65 2c 20 69 66 20 74 68 65 20 * Worse, if the
1cbb9 6f 72 69 67 69 6e 61 6c 20 69 73 20 64 65 61 6c original is deal
1cbba 6c 6f 63 61 74 65 64 2c 20 74 68 65 20 63 6f 70 located, the cop
1cbbb 79 20 62 65 63 6f 6d 65 73 20 69 6e 76 61 6c 69 y becomes invali
1cbbc 64 2e 0a 2a 2a 20 54 68 75 73 20 74 68 65 20 70 d..** Thus the p
1cbbd 72 6f 67 72 61 6d 20 6d 75 73 74 20 67 75 61 72 rogram must guar
1cbbe 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 6f antee that the o
1cbbf 72 69 67 69 6e 61 6c 20 77 69 6c 6c 20 6e 6f 74 riginal will not
1cbc0 20 63 68 61 6e 67 65 0a 2a 2a 20 64 75 72 69 6e change.** durin
1cbc1 67 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f g the lifetime o
1cbc2 66 20 74 68 65 20 63 6f 70 79 2e 20 20 55 73 65 f the copy. Use
1cbc3 20 4f 50 5f 43 6f 70 79 20 74 6f 20 6d 61 6b 65 OP_Copy to make
1cbc4 20 61 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20 63 a complete.** c
1cbc5 6f 70 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f opy..*/.case OP_
1cbc6 53 43 6f 70 79 3a 20 7b 20 20 20 20 20 20 20 20 SCopy: {
1cbc7 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 /* in1 */.
1cbc8 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 REGISTER_TRACE(p
1cbc9 4f 70 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20 Op->p1, pIn1);.
1cbca 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 assert( pOp->p2
1cbcb 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 >0 );. assert(
1cbcc 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d pOp->p2<=p->nMem
1cbcd 20 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d );. pOut = &p-
1cbce 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a >aMem[pOp->p2];.
1cbcf 20 20 61 73 73 65 72 74 28 20 70 4f 75 74 21 3d assert( pOut!=
1cbd0 70 49 6e 31 20 29 3b 0a 20 20 73 71 6c 69 74 65 pIn1 );. sqlite
1cbd1 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 3VdbeMemShallowC
1cbd2 6f 70 79 28 70 4f 75 74 2c 20 70 49 6e 31 2c 20 opy(pOut, pIn1,
1cbd3 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 52 45 MEM_Ephem);. RE
1cbd4 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 GISTER_TRACE(pOp
1cbd5 2d 3e 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20 62 ->p2, pOut);. b
1cbd6 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1cbd7 64 65 3a 20 52 65 73 75 6c 74 52 6f 77 20 50 31 de: ResultRow P1
1cbd8 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * * *.**.**
1cbd9 54 68 65 20 72 65 67 69 73 74 65 72 73 20 50 31 The registers P1
1cbda 20 74 68 72 6f 75 67 68 20 50 31 2b 50 32 2d 31 through P1+P2-1
1cbdb 20 63 6f 6e 74 61 69 6e 20 61 20 73 69 6e 67 6c contain a singl
1cbdc 65 20 72 6f 77 20 6f 66 0a 2a 2a 20 72 65 73 75 e row of.** resu
1cbdd 6c 74 73 2e 20 54 68 69 73 20 6f 70 63 6f 64 65 lts. This opcode
1cbde 20 63 61 75 73 65 73 20 74 68 65 20 73 71 6c 69 causes the sqli
1cbdf 74 65 33 5f 73 74 65 70 28 29 20 63 61 6c 6c 20 te3_step() call
1cbe0 74 6f 20 74 65 72 6d 69 6e 61 74 65 0a 2a 2a 20 to terminate.**
1cbe1 77 69 74 68 20 61 6e 20 53 51 4c 49 54 45 5f 52 with an SQLITE_R
1cbe2 4f 57 20 72 65 74 75 72 6e 20 63 6f 64 65 20 61 OW return code a
1cbe3 6e 64 20 69 74 20 73 65 74 73 20 75 70 20 74 68 nd it sets up th
1cbe4 65 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 0a 2a e sqlite3_stmt.*
1cbe5 2a 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 70 * structure to p
1cbe6 72 6f 76 69 64 65 20 61 63 63 65 73 73 20 74 6f rovide access to
1cbe7 20 74 68 65 20 74 6f 70 20 50 31 20 76 61 6c 75 the top P1 valu
1cbe8 65 73 20 61 73 20 74 68 65 20 72 65 73 75 6c 74 es as the result
1cbe9 0a 2a 2a 20 72 6f 77 2e 0a 2a 2f 0a 63 61 73 65 .** row..*/.case
1cbea 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 3a 20 7b OP_ResultRow: {
1cbeb 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1cbec 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1cbed 20 69 6e 74 6f 20 75 2e 61 64 20 2a 2f 0a 20 20 into u.ad */.
1cbee 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 69 6e 74 Mem *pMem;. int
1cbef 20 69 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f i;.#endif /* lo
1cbf0 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1cbf1 76 65 64 20 69 6e 74 6f 20 75 2e 61 64 20 2a 2f ved into u.ad */
1cbf2 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 . assert( p->nR
1cbf3 65 73 43 6f 6c 75 6d 6e 3d 3d 70 4f 70 2d 3e 70 esColumn==pOp->p
1cbf4 32 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2 );. assert( p
1cbf5 4f 70 2d 3e 70 31 3e 30 20 29 3b 0a 20 20 61 73 Op->p1>0 );. as
1cbf6 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 2b 70 4f sert( pOp->p1+pO
1cbf7 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 p->p2<=p->nMem+1
1cbf8 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 );.. /* If thi
1cbf9 73 20 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20 s statement has
1cbfa 76 69 6f 6c 61 74 65 64 20 69 6d 6d 65 64 69 61 violated immedia
1cbfb 74 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 te foreign key c
1cbfc 6f 6e 73 74 72 61 69 6e 74 73 2c 20 64 6f 0a 20 onstraints, do.
1cbfd 20 2a 2a 20 6e 6f 74 20 72 65 74 75 72 6e 20 74 ** not return t
1cbfe 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 he number of row
1cbff 73 20 6d 6f 64 69 66 69 65 64 2e 20 41 6e 64 20 s modified. And
1cc00 64 6f 20 6e 6f 74 20 52 45 4c 45 41 53 45 20 74 do not RELEASE t
1cc01 68 65 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 2a he statement. *
1cc02 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49 * transaction. I
1cc03 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 6f t needs to be ro
1cc04 6c 6c 65 64 20 62 61 63 6b 2e 20 20 2a 2f 0a 20 lled back. */.
1cc05 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d if( SQLITE_OK!=
1cc06 28 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 (rc = sqlite3Vdb
1cc07 65 43 68 65 63 6b 46 6b 28 70 2c 20 30 29 29 20 eCheckFk(p, 0))
1cc08 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 ){. assert( d
1cc09 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f b->flags&SQLITE_
1cc0a 43 6f 75 6e 74 52 6f 77 73 20 29 3b 0a 20 20 20 CountRows );.
1cc0b 20 61 73 73 65 72 74 28 20 70 2d 3e 75 73 65 73 assert( p->uses
1cc0c 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 StmtJournal );.
1cc0d 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 break;. }..
1cc0e 20 2f 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54 /* If the SQLIT
1cc0f 45 5f 43 6f 75 6e 74 52 6f 77 73 20 66 6c 61 67 E_CountRows flag
1cc10 20 69 73 20 73 65 74 20 69 6e 20 73 71 6c 69 74 is set in sqlit
1cc11 65 33 2e 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74 e3.flags mask, t
1cc12 68 65 6e 0a 20 20 2a 2a 20 44 4d 4c 20 73 74 61 hen. ** DML sta
1cc13 74 65 6d 65 6e 74 73 20 69 6e 76 6f 6b 65 20 74 tements invoke t
1cc14 68 69 73 20 6f 70 63 6f 64 65 20 74 6f 20 72 65 his opcode to re
1cc15 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 turn the number
1cc16 6f 66 20 72 6f 77 73 0a 20 20 2a 2a 20 6d 6f 64 of rows. ** mod
1cc17 69 66 69 65 64 20 74 6f 20 74 68 65 20 75 73 65 ified to the use
1cc18 72 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 6f r. This is the o
1cc19 6e 6c 79 20 77 61 79 20 74 68 61 74 20 61 20 56 nly way that a V
1cc1a 4d 20 74 68 61 74 0a 20 20 2a 2a 20 6f 70 65 6e M that. ** open
1cc1b 73 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72 s a statement tr
1cc1c 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 69 6e ansaction may in
1cc1d 76 6f 6b 65 20 74 68 69 73 20 6f 70 63 6f 64 65 voke this opcode
1cc1e 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20 63 .. **. ** In c
1cc1f 61 73 65 20 74 68 69 73 20 69 73 20 73 75 63 68 ase this is such
1cc20 20 61 20 73 74 61 74 65 6d 65 6e 74 2c 20 63 6c a statement, cl
1cc21 6f 73 65 20 61 6e 79 20 73 74 61 74 65 6d 65 6e ose any statemen
1cc22 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 t transaction.
1cc23 2a 2a 20 6f 70 65 6e 65 64 20 62 79 20 74 68 69 ** opened by thi
1cc24 73 20 56 4d 20 62 65 66 6f 72 65 20 72 65 74 75 s VM before retu
1cc25 72 6e 69 6e 67 20 63 6f 6e 74 72 6f 6c 20 74 6f rning control to
1cc26 20 74 68 65 20 75 73 65 72 2e 20 54 68 69 73 20 the user. This
1cc27 69 73 20 74 6f 0a 20 20 2a 2a 20 65 6e 73 75 72 is to. ** ensur
1cc28 65 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74 e that statement
1cc29 2d 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 61 72 -transactions ar
1cc2a 65 20 61 6c 77 61 79 73 20 6e 65 73 74 65 64 2c e always nested,
1cc2b 20 6e 6f 74 20 6f 76 65 72 6c 61 70 70 69 6e 67 not overlapping
1cc2c 2e 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 6f 70 .. ** If the op
1cc2d 65 6e 20 73 74 61 74 65 6d 65 6e 74 2d 74 72 61 en statement-tra
1cc2e 6e 73 61 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 nsaction is not
1cc2f 63 6c 6f 73 65 64 20 68 65 72 65 2c 20 74 68 65 closed here, the
1cc30 6e 20 74 68 65 20 75 73 65 72 0a 20 20 2a 2a 20 n the user. **
1cc31 6d 61 79 20 73 74 65 70 20 61 6e 6f 74 68 65 72 may step another
1cc32 20 56 4d 20 74 68 61 74 20 6f 70 65 6e 73 20 69 VM that opens i
1cc33 74 73 20 6f 77 6e 20 73 74 61 74 65 6d 65 6e 74 ts own statement
1cc34 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 transaction. Th
1cc35 69 73 0a 20 20 2a 2a 20 6d 61 79 20 6c 65 61 64 is. ** may lead
1cc36 20 74 6f 20 6f 76 65 72 6c 61 70 70 69 6e 67 20 to overlapping
1cc37 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 statement transa
1cc38 63 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a ctions.. **. *
1cc39 2a 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74 20 * The statement
1cc3a 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6e transaction is n
1cc3b 65 76 65 72 20 61 20 74 6f 70 2d 6c 65 76 65 6c ever a top-level
1cc3c 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 48 transaction. H
1cc3d 65 6e 63 65 0a 20 20 2a 2a 20 74 68 65 20 52 45 ence. ** the RE
1cc3e 4c 45 41 53 45 20 63 61 6c 6c 20 62 65 6c 6f 77 LEASE call below
1cc3f 20 63 61 6e 20 6e 65 76 65 72 20 66 61 69 6c 2e can never fail.
1cc40 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
1cc41 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 3d 3d 30 p->iStatement==0
1cc42 20 7c 7c 20 64 62 2d 3e 66 6c 61 67 73 26 53 51 || db->flags&SQ
1cc43 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 29 LITE_CountRows )
1cc44 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
1cc45 56 64 62 65 43 6c 6f 73 65 53 74 61 74 65 6d 65 VdbeCloseStateme
1cc46 6e 74 28 70 2c 20 53 41 56 45 50 4f 49 4e 54 5f nt(p, SAVEPOINT_
1cc47 52 45 4c 45 41 53 45 29 3b 0a 20 20 69 66 28 20 RELEASE);. if(
1cc48 4e 45 56 45 52 28 72 63 21 3d 53 51 4c 49 54 45 NEVER(rc!=SQLITE
1cc49 5f 4f 4b 29 20 29 7b 0a 20 20 20 20 62 72 65 61 _OK) ){. brea
1cc4a 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 76 k;. }.. /* Inv
1cc4b 61 6c 69 64 61 74 65 20 61 6c 6c 20 65 70 68 65 alidate all ephe
1cc4c 6d 65 72 61 6c 20 63 75 72 73 6f 72 20 72 6f 77 meral cursor row
1cc4d 20 63 61 63 68 65 73 20 2a 2f 0a 20 20 70 2d 3e caches */. p->
1cc4e 63 61 63 68 65 43 74 72 20 3d 20 28 70 2d 3e 63 cacheCtr = (p->c
1cc4f 61 63 68 65 43 74 72 20 2b 20 32 29 7c 31 3b 0a acheCtr + 2)|1;.
1cc50 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 . /* Make sure
1cc51 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74 the results of t
1cc52 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 61 he current row a
1cc53 72 65 20 5c 30 30 30 20 74 65 72 6d 69 6e 61 74 re \000 terminat
1cc54 65 64 0a 20 20 2a 2a 20 61 6e 64 20 68 61 76 65 ed. ** and have
1cc55 20 61 6e 20 61 73 73 69 67 6e 65 64 20 74 79 70 an assigned typ
1cc56 65 2e 20 20 54 68 65 20 72 65 73 75 6c 74 73 20 e. The results
1cc57 61 72 65 20 64 65 2d 65 70 68 65 6d 65 72 61 6c are de-ephemeral
1cc58 69 7a 65 64 20 61 73 0a 20 20 2a 2a 20 61 73 20 ized as. ** as
1cc59 73 69 64 65 20 65 66 66 65 63 74 2e 0a 20 20 2a side effect.. *
1cc5a 2f 0a 20 20 75 2e 61 64 2e 70 4d 65 6d 20 3d 20 /. u.ad.pMem =
1cc5b 70 2d 3e 70 52 65 73 75 6c 74 53 65 74 20 3d 20 p->pResultSet =
1cc5c 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 &p->aMem[pOp->p1
1cc5d 5d 3b 0a 20 20 66 6f 72 28 75 2e 61 64 2e 69 3d ];. for(u.ad.i=
1cc5e 30 3b 20 75 2e 61 64 2e 69 3c 70 4f 70 2d 3e 70 0; u.ad.i<pOp->p
1cc5f 32 3b 20 75 2e 61 64 2e 69 2b 2b 29 7b 0a 20 20 2; u.ad.i++){.
1cc60 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1cc61 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 26 75 2e NulTerminate(&u.
1cc62 61 64 2e 70 4d 65 6d 5b 75 2e 61 64 2e 69 5d 29 ad.pMem[u.ad.i])
1cc63 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
1cc64 65 4d 65 6d 53 74 6f 72 65 54 79 70 65 28 26 75 eMemStoreType(&u
1cc65 2e 61 64 2e 70 4d 65 6d 5b 75 2e 61 64 2e 69 5d .ad.pMem[u.ad.i]
1cc66 29 3b 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f );. REGISTER_
1cc67 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2b 75 2e TRACE(pOp->p1+u.
1cc68 61 64 2e 69 2c 20 26 75 2e 61 64 2e 70 4d 65 6d ad.i, &u.ad.pMem
1cc69 5b 75 2e 61 64 2e 69 5d 29 3b 0a 20 20 7d 0a 20 [u.ad.i]);. }.
1cc6a 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 if( db->mallocF
1cc6b 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 6e 6f 5f ailed ) goto no_
1cc6c 6d 65 6d 3b 0a 0a 20 20 2f 2a 20 52 65 74 75 72 mem;.. /* Retur
1cc6d 6e 20 53 51 4c 49 54 45 5f 52 4f 57 0a 20 20 2a n SQLITE_ROW. *
1cc6e 2f 0a 20 20 70 2d 3e 70 63 20 3d 20 70 63 20 2b /. p->pc = pc +
1cc6f 20 31 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 1;. rc = SQLIT
1cc70 45 5f 52 4f 57 3b 0a 20 20 67 6f 74 6f 20 76 64 E_ROW;. goto vd
1cc71 62 65 5f 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a be_return;.}../*
1cc72 20 4f 70 63 6f 64 65 3a 20 43 6f 6e 63 61 74 20 Opcode: Concat
1cc73 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a P1 P2 P3 * *.**.
1cc74 2a 2a 20 41 64 64 20 74 68 65 20 74 65 78 74 20 ** Add the text
1cc75 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 6f in register P1 o
1cc76 6e 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 nto the end of t
1cc77 68 65 20 74 65 78 74 20 69 6e 0a 2a 2a 20 72 65 he text in.** re
1cc78 67 69 73 74 65 72 20 50 32 20 61 6e 64 20 73 74 gister P2 and st
1cc79 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 ore the result i
1cc7a 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a n register P3..*
1cc7b 2a 20 49 66 20 65 69 74 68 65 72 20 74 68 65 20 * If either the
1cc7c 50 31 20 6f 72 20 50 32 20 74 65 78 74 20 61 72 P1 or P2 text ar
1cc7d 65 20 4e 55 4c 4c 20 74 68 65 6e 20 73 74 6f 72 e NULL then stor
1cc7e 65 20 4e 55 4c 4c 20 69 6e 20 50 33 2e 0a 2a 2a e NULL in P3..**
1cc7f 0a 2a 2a 20 20 20 50 33 20 3d 20 50 32 20 7c 7c .** P3 = P2 ||
1cc80 20 50 31 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 P1.**.** It is
1cc81 69 6c 6c 65 67 61 6c 20 66 6f 72 20 50 31 20 61 illegal for P1 a
1cc82 6e 64 20 50 33 20 74 6f 20 62 65 20 74 68 65 20 nd P3 to be the
1cc83 73 61 6d 65 20 72 65 67 69 73 74 65 72 2e 20 53 same register. S
1cc84 6f 6d 65 74 69 6d 65 73 2c 0a 2a 2a 20 69 66 20 ometimes,.** if
1cc85 50 33 20 69 73 20 74 68 65 20 73 61 6d 65 20 72 P3 is the same r
1cc86 65 67 69 73 74 65 72 20 61 73 20 50 32 2c 20 74 egister as P2, t
1cc87 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
1cc88 6e 20 69 73 20 61 62 6c 65 0a 2a 2a 20 74 6f 20 n is able.** to
1cc89 61 76 6f 69 64 20 61 20 6d 65 6d 63 70 79 28 29 avoid a memcpy()
1cc8a 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6e ..*/.case OP_Con
1cc8b 63 61 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 cat: {
1cc8c 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 43 /* same as TK_C
1cc8d 4f 4e 43 41 54 2c 20 69 6e 31 2c 20 69 6e 32 2c ONCAT, in1, in2,
1cc8e 20 6f 75 74 33 20 2a 2f 0a 23 69 66 20 30 20 20 out3 */.#if 0
1cc8f 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1cc90 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1cc91 61 65 20 2a 2f 0a 20 20 69 36 34 20 6e 42 79 74 ae */. i64 nByt
1cc92 65 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 e;.#endif /* loc
1cc93 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1cc94 65 64 20 69 6e 74 6f 20 75 2e 61 65 20 2a 2f 0a ed into u.ae */.
1cc95 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 21 . assert( pIn1!
1cc96 3d 70 4f 75 74 20 29 3b 0a 20 20 69 66 28 20 28 =pOut );. if( (
1cc97 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c 20 70 49 pIn1->flags | pI
1cc98 6e 32 2d 3e 66 6c 61 67 73 29 20 26 20 4d 45 4d n2->flags) & MEM
1cc99 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73 71 6c _Null ){. sql
1cc9a 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 ite3VdbeMemSetNu
1cc9b 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 20 20 62 72 ll(pOut);. br
1cc9c 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 45 eak;. }. if( E
1cc9d 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 31 29 20 xpandBlob(pIn1)
1cc9e 7c 7c 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 || ExpandBlob(pI
1cc9f 6e 32 29 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 n2) ) goto no_me
1cca0 6d 3b 0a 20 20 53 74 72 69 6e 67 69 66 79 28 70 m;. Stringify(p
1cca1 49 6e 31 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a In1, encoding);.
1cca2 20 20 53 74 72 69 6e 67 69 66 79 28 70 49 6e 32 Stringify(pIn2
1cca3 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 75 , encoding);. u
1cca4 2e 61 65 2e 6e 42 79 74 65 20 3d 20 70 49 6e 31 .ae.nByte = pIn1
1cca5 2d 3e 6e 20 2b 20 70 49 6e 32 2d 3e 6e 3b 0a 20 ->n + pIn2->n;.
1cca6 20 69 66 28 20 75 2e 61 65 2e 6e 42 79 74 65 3e if( u.ae.nByte>
1cca7 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 db->aLimit[SQLIT
1cca8 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 E_LIMIT_LENGTH]
1cca9 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f ){. goto too_
1ccaa 62 69 67 3b 0a 20 20 7d 0a 20 20 4d 65 6d 53 65 big;. }. MemSe
1ccab 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 tTypeFlag(pOut,
1ccac 4d 45 4d 5f 53 74 72 29 3b 0a 20 20 69 66 28 20 MEM_Str);. if(
1ccad 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 sqlite3VdbeMemGr
1ccae 6f 77 28 70 4f 75 74 2c 20 28 69 6e 74 29 75 2e ow(pOut, (int)u.
1ccaf 61 65 2e 6e 42 79 74 65 2b 32 2c 20 70 4f 75 74 ae.nByte+2, pOut
1ccb0 3d 3d 70 49 6e 32 29 20 29 7b 0a 20 20 20 20 67 ==pIn2) ){. g
1ccb1 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a oto no_mem;. }.
1ccb2 20 20 69 66 28 20 70 4f 75 74 21 3d 70 49 6e 32 if( pOut!=pIn2
1ccb3 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 ){. memcpy(p
1ccb4 4f 75 74 2d 3e 7a 2c 20 70 49 6e 32 2d 3e 7a 2c Out->z, pIn2->z,
1ccb5 20 70 49 6e 32 2d 3e 6e 29 3b 0a 20 20 7d 0a 20 pIn2->n);. }.
1ccb6 20 6d 65 6d 63 70 79 28 26 70 4f 75 74 2d 3e 7a memcpy(&pOut->z
1ccb7 5b 70 49 6e 32 2d 3e 6e 5d 2c 20 70 49 6e 31 2d [pIn2->n], pIn1-
1ccb8 3e 7a 2c 20 70 49 6e 31 2d 3e 6e 29 3b 0a 20 20 >z, pIn1->n);.
1ccb9 70 4f 75 74 2d 3e 7a 5b 75 2e 61 65 2e 6e 42 79 pOut->z[u.ae.nBy
1ccba 74 65 5d 20 3d 20 30 3b 0a 20 20 70 4f 75 74 2d te] = 0;. pOut-
1ccbb 3e 7a 5b 75 2e 61 65 2e 6e 42 79 74 65 2b 31 5d >z[u.ae.nByte+1]
1ccbc 20 3d 20 30 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c = 0;. pOut->fl
1ccbd 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b ags |= MEM_Term;
1ccbe 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 28 69 6e . pOut->n = (in
1ccbf 74 29 75 2e 61 65 2e 6e 42 79 74 65 3b 0a 20 20 t)u.ae.nByte;.
1ccc0 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f pOut->enc = enco
1ccc1 64 69 6e 67 3b 0a 20 20 55 50 44 41 54 45 5f 4d ding;. UPDATE_M
1ccc2 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 AX_BLOBSIZE(pOut
1ccc3 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f );. break;.}../
1ccc4 2a 20 4f 70 63 6f 64 65 3a 20 41 64 64 20 50 31 * Opcode: Add P1
1ccc5 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a P2 P3 * *.**.**
1ccc6 20 41 64 64 20 74 68 65 20 76 61 6c 75 65 20 69 Add the value i
1ccc7 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 74 6f n register P1 to
1ccc8 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 the value in re
1ccc9 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64 gister P2.** and
1ccca 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c store the resul
1cccb 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 t in register P3
1cccc 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69 ..** If either i
1cccd 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 nput is NULL, th
1ccce 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c e result is NULL
1cccf 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ..*/./* Opcode:
1ccd0 4d 75 6c 74 69 70 6c 79 20 50 31 20 50 32 20 50 Multiply P1 P2 P
1ccd1 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 4d 3 * *.**.**.** M
1ccd2 75 6c 74 69 70 6c 79 20 74 68 65 20 76 61 6c 75 ultiply the valu
1ccd3 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 e in register P1
1ccd4 20 62 79 20 74 68 65 20 76 61 6c 75 65 20 69 6e by the value in
1ccd5 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 register P2.**
1ccd6 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 and store the re
1ccd7 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 sult in register
1ccd8 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 P3..** If eithe
1ccd9 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c r input is NULL,
1ccda 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e the result is N
1ccdb 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 ULL..*/./* Opcod
1ccdc 65 3a 20 53 75 62 74 72 61 63 74 20 50 31 20 50 e: Subtract P1 P
1ccdd 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 2 P3 * *.**.** S
1ccde 75 62 74 72 61 63 74 20 74 68 65 20 76 61 6c 75 ubtract the valu
1ccdf 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 e in register P1
1cce0 20 66 72 6f 6d 20 74 68 65 20 76 61 6c 75 65 20 from the value
1cce1 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a in register P2.*
1cce2 2a 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 * and store the
1cce3 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 result in regist
1cce4 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 er P3..** If eit
1cce5 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c her input is NUL
1cce6 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 L, the result is
1cce7 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 NULL..*/./* Opc
1cce8 6f 64 65 3a 20 44 69 76 69 64 65 20 50 31 20 50 ode: Divide P1 P
1cce9 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 2 P3 * *.**.** D
1ccea 69 76 69 64 65 20 74 68 65 20 76 61 6c 75 65 20 ivide the value
1cceb 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 62 in register P1 b
1ccec 79 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 y the value in r
1cced 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 61 6e egister P2.** an
1ccee 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 d store the resu
1ccef 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 lt in register P
1ccf0 33 20 28 50 33 3d 50 32 2f 50 31 29 2e 20 49 66 3 (P3=P2/P1). If
1ccf1 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 0a 2a the value in .*
1ccf2 2a 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 * register P1 is
1ccf3 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 zero, then the
1ccf4 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 20 result is NULL.
1ccf5 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 If either input
1ccf6 69 73 20 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 is .** NULL, the
1ccf7 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e result is NULL.
1ccf8 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 .*/./* Opcode: R
1ccf9 65 6d 61 69 6e 64 65 72 20 50 31 20 50 32 20 50 emainder P1 P2 P
1ccfa 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 3 * *.**.** Comp
1ccfb 75 74 65 20 74 68 65 20 72 65 6d 61 69 6e 64 65 ute the remainde
1ccfc 72 20 61 66 74 65 72 20 69 6e 74 65 67 65 72 20 r after integer
1ccfd 64 69 76 69 73 69 6f 6e 20 6f 66 20 74 68 65 20 division of the
1ccfe 76 61 6c 75 65 20 69 6e 0a 2a 2a 20 72 65 67 69 value in.** regi
1ccff 73 74 65 72 20 50 31 20 62 79 20 74 68 65 20 76 ster P1 by the v
1cd00 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 alue in register
1cd01 20 50 32 20 61 6e 64 20 73 74 6f 72 65 20 74 68 P2 and store th
1cd02 65 20 72 65 73 75 6c 74 20 69 6e 20 50 33 2e 20 e result in P3.
1cd03 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 .** If the value
1cd04 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 20 in register P2
1cd05 69 73 20 7a 65 72 6f 20 74 68 65 20 72 65 73 75 is zero the resu
1cd06 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 20 49 lt is NULL..** I
1cd07 66 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64 f either operand
1cd08 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 is NULL, the re
1cd09 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f sult is NULL..*/
1cd0a 0a 63 61 73 65 20 4f 50 5f 41 64 64 3a 20 20 20 .case OP_Add:
1cd0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cd0c 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 50 4c /* same as TK_PL
1cd0d 55 53 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 US, in1, in2, ou
1cd0e 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 75 t3 */.case OP_Su
1cd0f 62 74 72 61 63 74 3a 20 20 20 20 20 20 20 20 20 btract:
1cd10 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
1cd11 54 4b 5f 4d 49 4e 55 53 2c 20 69 6e 31 2c 20 69 TK_MINUS, in1, i
1cd12 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 n2, out3 */.case
1cd13 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 3a 20 20 20 OP_Multiply:
1cd14 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 /* sa
1cd15 6d 65 20 61 73 20 54 4b 5f 53 54 41 52 2c 20 69 me as TK_STAR, i
1cd16 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f n1, in2, out3 */
1cd17 0a 63 61 73 65 20 4f 50 5f 44 69 76 69 64 65 3a .case OP_Divide:
1cd18 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cd19 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 4c /* same as TK_SL
1cd1a 41 53 48 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f ASH, in1, in2, o
1cd1b 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 ut3 */.case OP_R
1cd1c 65 6d 61 69 6e 64 65 72 3a 20 7b 20 20 20 20 20 emainder: {
1cd1d 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 /* same as
1cd1e 20 54 4b 5f 52 45 4d 2c 20 69 6e 31 2c 20 69 6e TK_REM, in1, in
1cd1f 32 2c 20 6f 75 74 33 20 2a 2f 0a 23 69 66 20 30 2, out3 */.#if 0
1cd20 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 /* local varia
1cd21 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1cd22 75 2e 61 66 20 2a 2f 0a 20 20 69 6e 74 20 66 6c u.af */. int fl
1cd23 61 67 73 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6d ags; /* Com
1cd24 62 69 6e 65 64 20 4d 45 4d 5f 2a 20 66 6c 61 67 bined MEM_* flag
1cd25 73 20 66 72 6f 6d 20 62 6f 74 68 20 69 6e 70 75 s from both inpu
1cd26 74 73 20 2a 2f 0a 20 20 69 36 34 20 69 41 3b 20 ts */. i64 iA;
1cd27 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 /* Integ
1cd28 65 72 20 76 61 6c 75 65 20 6f 66 20 6c 65 66 74 er value of left
1cd29 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 36 operand */. i6
1cd2a 34 20 69 42 3b 20 20 20 20 20 20 20 20 20 2f 2a 4 iB; /*
1cd2b 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20 6f Integer value o
1cd2c 66 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20 f right operand
1cd2d 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 41 3b 20 */. double rA;
1cd2e 20 20 20 20 20 2f 2a 20 52 65 61 6c 20 76 61 6c /* Real val
1cd2f 75 65 20 6f 66 20 6c 65 66 74 20 6f 70 65 72 61 ue of left opera
1cd30 6e 64 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 nd */. double r
1cd31 42 3b 20 20 20 20 20 20 2f 2a 20 52 65 61 6c 20 B; /* Real
1cd32 76 61 6c 75 65 20 6f 66 20 72 69 67 68 74 20 6f value of right o
1cd33 70 65 72 61 6e 64 20 2a 2f 0a 23 65 6e 64 69 66 perand */.#endif
1cd34 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1cd35 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1cd36 2e 61 66 20 2a 2f 0a 0a 20 20 61 70 70 6c 79 4e .af */.. applyN
1cd37 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 70 umericAffinity(p
1cd38 49 6e 31 29 3b 0a 20 20 61 70 70 6c 79 4e 75 6d In1);. applyNum
1cd39 65 72 69 63 41 66 66 69 6e 69 74 79 28 70 49 6e ericAffinity(pIn
1cd3a 32 29 3b 0a 20 20 75 2e 61 66 2e 66 6c 61 67 73 2);. u.af.flags
1cd3b 20 3d 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c = pIn1->flags |
1cd3c 20 70 49 6e 32 2d 3e 66 6c 61 67 73 3b 0a 20 20 pIn2->flags;.
1cd3d 69 66 28 20 28 75 2e 61 66 2e 66 6c 61 67 73 20 if( (u.af.flags
1cd3e 26 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30 20 29 & MEM_Null)!=0 )
1cd3f 20 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63 goto arithmetic
1cd40 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b _result_is_null;
1cd41 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c . if( (pIn1->fl
1cd42 61 67 73 20 26 20 70 49 6e 32 2d 3e 66 6c 61 67 ags & pIn2->flag
1cd43 73 20 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d 4d 45 s & MEM_Int)==ME
1cd44 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 75 2e 61 M_Int ){. u.a
1cd45 66 2e 69 41 20 3d 20 70 49 6e 31 2d 3e 75 2e 69 f.iA = pIn1->u.i
1cd46 3b 0a 20 20 20 20 75 2e 61 66 2e 69 42 20 3d 20 ;. u.af.iB =
1cd47 70 49 6e 32 2d 3e 75 2e 69 3b 0a 20 20 20 20 73 pIn2->u.i;. s
1cd48 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f witch( pOp->opco
1cd49 64 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 de ){. case
1cd4a 20 4f 50 5f 41 64 64 3a 20 20 20 20 20 20 20 20 OP_Add:
1cd4b 20 75 2e 61 66 2e 69 42 20 2b 3d 20 75 2e 61 66 u.af.iB += u.af
1cd4c 2e 69 41 3b 20 20 20 20 20 20 20 62 72 65 61 6b .iA; break
1cd4d 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f ;. case OP_
1cd4e 53 75 62 74 72 61 63 74 3a 20 20 20 20 75 2e 61 Subtract: u.a
1cd4f 66 2e 69 42 20 2d 3d 20 75 2e 61 66 2e 69 41 3b f.iB -= u.af.iA;
1cd50 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
1cd51 20 20 20 20 63 61 73 65 20 4f 50 5f 4d 75 6c 74 case OP_Mult
1cd52 69 70 6c 79 3a 20 20 20 20 75 2e 61 66 2e 69 42 iply: u.af.iB
1cd53 20 2a 3d 20 75 2e 61 66 2e 69 41 3b 20 20 20 20 *= u.af.iA;
1cd54 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
1cd55 63 61 73 65 20 4f 50 5f 44 69 76 69 64 65 3a 20 case OP_Divide:
1cd56 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e {. if( u.
1cd57 61 66 2e 69 41 3d 3d 30 20 29 20 67 6f 74 6f 20 af.iA==0 ) goto
1cd58 61 72 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c arithmetic_resul
1cd59 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 t_is_null;.
1cd5a 20 20 20 2f 2a 20 44 69 76 69 64 69 6e 67 20 74 /* Dividing t
1cd5b 68 65 20 6c 61 72 67 65 73 74 20 70 6f 73 73 69 he largest possi
1cd5c 62 6c 65 20 6e 65 67 61 74 69 76 65 20 36 34 2d ble negative 64-
1cd5d 62 69 74 20 69 6e 74 65 67 65 72 20 28 31 3c 3c bit integer (1<<
1cd5e 36 33 29 20 62 79 0a 20 20 20 20 20 20 20 20 2a 63) by. *
1cd5f 2a 20 2d 31 20 72 65 74 75 72 6e 73 20 61 6e 20 * -1 returns an
1cd60 69 6e 74 65 67 65 72 20 74 6f 6f 20 6c 61 72 67 integer too larg
1cd61 65 20 74 6f 20 73 74 6f 72 65 20 69 6e 20 61 20 e to store in a
1cd62 36 34 2d 62 69 74 20 64 61 74 61 2d 74 79 70 65 64-bit data-type
1cd63 2e 20 4f 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 . On. **
1cd64 73 6f 6d 65 20 61 72 63 68 69 74 65 63 74 75 72 some architectur
1cd65 65 73 2c 20 74 68 65 20 76 61 6c 75 65 20 6f 76 es, the value ov
1cd66 65 72 66 6c 6f 77 73 20 74 6f 20 28 31 3c 3c 36 erflows to (1<<6
1cd67 33 29 2e 20 4f 6e 20 6f 74 68 65 72 73 2c 0a 20 3). On others,.
1cd68 20 20 20 20 20 20 20 2a 2a 20 61 20 53 49 47 46 ** a SIGF
1cd69 50 45 20 69 73 20 69 73 73 75 65 64 2e 20 54 68 PE is issued. Th
1cd6a 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 e following stat
1cd6b 65 6d 65 6e 74 20 6e 6f 72 6d 61 6c 69 7a 65 73 ement normalizes
1cd6c 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 2a 2a this. **
1cd6d 20 62 65 68 61 76 69 6f 72 20 73 6f 20 74 68 61 behavior so tha
1cd6e 74 20 61 6c 6c 20 61 72 63 68 69 74 65 63 74 75 t all architectu
1cd6f 72 65 73 20 62 65 68 61 76 65 20 61 73 20 69 66 res behave as if
1cd70 20 69 6e 74 65 67 65 72 0a 20 20 20 20 20 20 20 integer.
1cd71 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 6f 63 63 ** overflow occ
1cd72 75 72 72 65 64 2e 0a 20 20 20 20 20 20 20 20 2a urred.. *
1cd73 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e /. if( u.
1cd74 61 66 2e 69 41 3d 3d 2d 31 20 26 26 20 75 2e 61 af.iA==-1 && u.a
1cd75 66 2e 69 42 3d 3d 53 4d 41 4c 4c 45 53 54 5f 49 f.iB==SMALLEST_I
1cd76 4e 54 36 34 20 29 20 75 2e 61 66 2e 69 41 20 3d NT64 ) u.af.iA =
1cd77 20 31 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 66 1;. u.af
1cd78 2e 69 42 20 2f 3d 20 75 2e 61 66 2e 69 41 3b 0a .iB /= u.af.iA;.
1cd79 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
1cd7a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 65 66 }. def
1cd7b 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 ault: {.
1cd7c 69 66 28 20 75 2e 61 66 2e 69 41 3d 3d 30 20 29 if( u.af.iA==0 )
1cd7d 20 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63 goto arithmetic
1cd7e 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b _result_is_null;
1cd7f 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 . if( u.a
1cd80 66 2e 69 41 3d 3d 2d 31 20 29 20 75 2e 61 66 2e f.iA==-1 ) u.af.
1cd81 69 41 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 iA = 1;.
1cd82 75 2e 61 66 2e 69 42 20 25 3d 20 75 2e 61 66 2e u.af.iB %= u.af.
1cd83 69 41 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 iA;. brea
1cd84 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d k;. }. }
1cd85 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d . pOut->u.i =
1cd86 20 75 2e 61 66 2e 69 42 3b 0a 20 20 20 20 4d 65 u.af.iB;. Me
1cd87 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 mSetTypeFlag(pOu
1cd88 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d t, MEM_Int);. }
1cd89 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 66 2e 72 else{. u.af.r
1cd8a 41 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 A = sqlite3VdbeR
1cd8b 65 61 6c 56 61 6c 75 65 28 70 49 6e 31 29 3b 0a ealValue(pIn1);.
1cd8c 20 20 20 20 75 2e 61 66 2e 72 42 20 3d 20 73 71 u.af.rB = sq
1cd8d 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c lite3VdbeRealVal
1cd8e 75 65 28 70 49 6e 32 29 3b 0a 20 20 20 20 73 77 ue(pIn2);. sw
1cd8f 69 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 itch( pOp->opcod
1cd90 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 e ){. case
1cd91 4f 50 5f 41 64 64 3a 20 20 20 20 20 20 20 20 20 OP_Add:
1cd92 75 2e 61 66 2e 72 42 20 2b 3d 20 75 2e 61 66 2e u.af.rB += u.af.
1cd93 72 41 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b rA; break;
1cd94 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f 53 . case OP_S
1cd95 75 62 74 72 61 63 74 3a 20 20 20 20 75 2e 61 66 ubtract: u.af
1cd96 2e 72 42 20 2d 3d 20 75 2e 61 66 2e 72 41 3b 20 .rB -= u.af.rA;
1cd97 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1cd98 20 20 20 63 61 73 65 20 4f 50 5f 4d 75 6c 74 69 case OP_Multi
1cd99 70 6c 79 3a 20 20 20 20 75 2e 61 66 2e 72 42 20 ply: u.af.rB
1cd9a 2a 3d 20 75 2e 61 66 2e 72 41 3b 20 20 20 20 20 *= u.af.rA;
1cd9b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 break;. c
1cd9c 61 73 65 20 4f 50 5f 44 69 76 69 64 65 3a 20 7b ase OP_Divide: {
1cd9d 0a 20 20 20 20 20 20 20 20 2f 2a 20 28 64 6f 75 . /* (dou
1cd9e 62 6c 65 29 30 20 49 6e 20 63 61 73 65 20 6f 66 ble)0 In case of
1cd9f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f SQLITE_OMIT_FLO
1cda0 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a ATING_POINT... *
1cda1 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e /. if( u.
1cda2 61 66 2e 72 41 3d 3d 28 64 6f 75 62 6c 65 29 30 af.rA==(double)0
1cda3 20 29 20 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 ) goto arithmet
1cda4 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c ic_result_is_nul
1cda5 6c 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 66 2e l;. u.af.
1cda6 72 42 20 2f 3d 20 75 2e 61 66 2e 72 41 3b 0a 20 rB /= u.af.rA;.
1cda7 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
1cda8 20 20 20 20 7d 0a 20 20 20 20 20 20 64 65 66 61 }. defa
1cda9 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 75 ult: {. u
1cdaa 2e 61 66 2e 69 41 20 3d 20 28 69 36 34 29 75 2e .af.iA = (i64)u.
1cdab 61 66 2e 72 41 3b 0a 20 20 20 20 20 20 20 20 75 af.rA;. u
1cdac 2e 61 66 2e 69 42 20 3d 20 28 69 36 34 29 75 2e .af.iB = (i64)u.
1cdad 61 66 2e 72 42 3b 0a 20 20 20 20 20 20 20 20 69 af.rB;. i
1cdae 66 28 20 75 2e 61 66 2e 69 41 3d 3d 30 20 29 20 f( u.af.iA==0 )
1cdaf 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f goto arithmetic_
1cdb0 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a result_is_null;.
1cdb1 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 66 if( u.af
1cdb2 2e 69 41 3d 3d 2d 31 20 29 20 75 2e 61 66 2e 69 .iA==-1 ) u.af.i
1cdb3 41 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 75 A = 1;. u
1cdb4 2e 61 66 2e 72 42 20 3d 20 28 64 6f 75 62 6c 65 .af.rB = (double
1cdb5 29 28 75 2e 61 66 2e 69 42 20 25 20 75 2e 61 66 )(u.af.iB % u.af
1cdb6 2e 69 41 29 3b 0a 20 20 20 20 20 20 20 20 62 72 .iA);. br
1cdb7 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 eak;. }.
1cdb8 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 }. if( sqlit
1cdb9 65 33 49 73 4e 61 4e 28 75 2e 61 66 2e 72 42 29 e3IsNaN(u.af.rB)
1cdba 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 ){. goto a
1cdbb 72 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 rithmetic_result
1cdbc 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 7d 0a _is_null;. }.
1cdbd 20 20 20 20 70 4f 75 74 2d 3e 72 20 3d 20 75 2e pOut->r = u.
1cdbe 61 66 2e 72 42 3b 0a 20 20 20 20 4d 65 6d 53 65 af.rB;. MemSe
1cdbf 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 tTypeFlag(pOut,
1cdc0 4d 45 4d 5f 52 65 61 6c 29 3b 0a 20 20 20 20 69 MEM_Real);. i
1cdc1 66 28 20 28 75 2e 61 66 2e 66 6c 61 67 73 20 26 f( (u.af.flags &
1cdc2 20 4d 45 4d 5f 52 65 61 6c 29 3d 3d 30 20 29 7b MEM_Real)==0 ){
1cdc3 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1cdc4 62 65 49 6e 74 65 67 65 72 41 66 66 69 6e 69 74 beIntegerAffinit
1cdc5 79 28 70 4f 75 74 29 3b 0a 20 20 20 20 7d 0a 20 y(pOut);. }.
1cdc6 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 0a 61 72 69 }. break;..ari
1cdc7 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 thmetic_result_i
1cdc8 73 5f 6e 75 6c 6c 3a 0a 20 20 73 71 6c 69 74 65 s_null:. sqlite
1cdc9 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 3VdbeMemSetNull(
1cdca 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a pOut);. break;.
1cdcb 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f }../* Opcode: Co
1cdcc 6c 6c 53 65 71 20 2a 20 2a 20 50 34 0a 2a 2a 0a llSeq * * P4.**.
1cdcd 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 ** P4 is a point
1cdce 65 72 20 74 6f 20 61 20 43 6f 6c 6c 53 65 71 20 er to a CollSeq
1cdcf 73 74 72 75 63 74 2e 20 49 66 20 74 68 65 20 6e struct. If the n
1cdd0 65 78 74 20 63 61 6c 6c 20 74 6f 20 61 20 75 73 ext call to a us
1cdd1 65 72 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6f er function.** o
1cdd2 72 20 61 67 67 72 65 67 61 74 65 20 63 61 6c 6c r aggregate call
1cdd3 73 20 73 71 6c 69 74 65 33 47 65 74 46 75 6e 63 s sqlite3GetFunc
1cdd4 43 6f 6c 6c 53 65 71 28 29 2c 20 74 68 69 73 20 CollSeq(), this
1cdd5 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e collation sequen
1cdd6 63 65 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 72 65 ce will.** be re
1cdd7 74 75 72 6e 65 64 2e 20 54 68 69 73 20 69 73 20 turned. This is
1cdd8 75 73 65 64 20 62 79 20 74 68 65 20 62 75 69 6c used by the buil
1cdd9 74 2d 69 6e 20 6d 69 6e 28 29 2c 20 6d 61 78 28 t-in min(), max(
1cdda 29 20 61 6e 64 20 6e 75 6c 6c 69 66 28 29 0a 2a ) and nullif().*
1cddb 2a 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a * functions..**.
1cddc 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 ** The interface
1cddd 20 75 73 65 64 20 62 79 20 74 68 65 20 69 6d 70 used by the imp
1cdde 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
1cddf 68 65 20 61 66 6f 72 65 6d 65 6e 74 69 6f 6e 65 he aforementione
1cde0 64 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 74 d functions.** t
1cde1 6f 20 72 65 74 72 69 65 76 65 20 74 68 65 20 63 o retrieve the c
1cde2 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 ollation sequenc
1cde3 65 20 73 65 74 20 62 79 20 74 68 69 73 20 6f 70 e set by this op
1cde4 63 6f 64 65 20 69 73 20 6e 6f 74 20 61 76 61 69 code is not avai
1cde5 6c 61 62 6c 65 0a 2a 2a 20 70 75 62 6c 69 63 6c lable.** publicl
1cde6 79 2c 20 6f 6e 6c 79 20 74 6f 20 75 73 65 72 20 y, only to user
1cde7 66 75 6e 63 74 69 6f 6e 73 20 64 65 66 69 6e 65 functions define
1cde8 64 20 69 6e 20 66 75 6e 63 2e 63 2e 0a 2a 2f 0a d in func.c..*/.
1cde9 63 61 73 65 20 4f 50 5f 43 6f 6c 6c 53 65 71 3a case OP_CollSeq:
1cdea 20 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 {. assert( pOp
1cdeb 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 43 4f 4c ->p4type==P4_COL
1cdec 4c 53 45 51 20 29 3b 0a 20 20 62 72 65 61 6b 3b LSEQ );. break;
1cded 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 46 .}../* Opcode: F
1cdee 75 6e 63 74 69 6f 6e 20 50 31 20 50 32 20 50 33 unction P1 P2 P3
1cdef 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 49 6e 76 P4 P5.**.** Inv
1cdf0 6f 6b 65 20 61 20 75 73 65 72 20 66 75 6e 63 74 oke a user funct
1cdf1 69 6f 6e 20 28 50 34 20 69 73 20 61 20 70 6f 69 ion (P4 is a poi
1cdf2 6e 74 65 72 20 74 6f 20 61 20 46 75 6e 63 74 69 nter to a Functi
1cdf3 6f 6e 20 73 74 72 75 63 74 75 72 65 20 74 68 61 on structure tha
1cdf4 74 0a 2a 2a 20 64 65 66 69 6e 65 73 20 74 68 65 t.** defines the
1cdf5 20 66 75 6e 63 74 69 6f 6e 29 20 77 69 74 68 20 function) with
1cdf6 50 35 20 61 72 67 75 6d 65 6e 74 73 20 74 61 6b P5 arguments tak
1cdf7 65 6e 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72 en from register
1cdf8 20 50 32 20 61 6e 64 0a 2a 2a 20 73 75 63 63 65 P2 and.** succe
1cdf9 73 73 6f 72 73 2e 20 20 54 68 65 20 72 65 73 75 ssors. The resu
1cdfa 6c 74 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 lt of the functi
1cdfb 6f 6e 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 on is stored in
1cdfc 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 register P3..**
1cdfd 52 65 67 69 73 74 65 72 20 50 33 20 6d 75 73 74 Register P3 must
1cdfe 20 6e 6f 74 20 62 65 20 6f 6e 65 20 6f 66 20 74 not be one of t
1cdff 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 6e 70 75 he function inpu
1ce00 74 73 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 ts..**.** P1 is
1ce01 61 20 33 32 2d 62 69 74 20 62 69 74 6d 61 73 6b a 32-bit bitmask
1ce02 20 69 6e 64 69 63 61 74 69 6e 67 20 77 68 65 74 indicating whet
1ce03 68 65 72 20 6f 72 20 6e 6f 74 20 65 61 63 68 20 her or not each
1ce04 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 argument to the
1ce05 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 .** function was
1ce06 20 64 65 74 65 72 6d 69 6e 65 64 20 74 6f 20 62 determined to b
1ce07 65 20 63 6f 6e 73 74 61 6e 74 20 61 74 20 63 6f e constant at co
1ce08 6d 70 69 6c 65 20 74 69 6d 65 2e 20 49 66 20 74 mpile time. If t
1ce09 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 he first.** argu
1ce0a 6d 65 6e 74 20 77 61 73 20 63 6f 6e 73 74 61 6e ment was constan
1ce0b 74 20 74 68 65 6e 20 62 69 74 20 30 20 6f 66 20 t then bit 0 of
1ce0c 50 31 20 69 73 20 73 65 74 2e 20 54 68 69 73 20 P1 is set. This
1ce0d 69 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 is used to deter
1ce0e 6d 69 6e 65 0a 2a 2a 20 77 68 65 74 68 65 72 20 mine.** whether
1ce0f 6d 65 74 61 20 64 61 74 61 20 61 73 73 6f 63 69 meta data associ
1ce10 61 74 65 64 20 77 69 74 68 20 61 20 75 73 65 72 ated with a user
1ce11 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 function argume
1ce12 6e 74 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 nt using the.**
1ce13 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 sqlite3_set_auxd
1ce14 61 74 61 28 29 20 41 50 49 20 6d 61 79 20 62 65 ata() API may be
1ce15 20 73 61 66 65 6c 79 20 72 65 74 61 69 6e 65 64 safely retained
1ce16 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 78 74 0a until the next.
1ce17 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 ** invocation of
1ce18 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 0a 2a 2a this opcode..**
1ce19 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 41 67 .** See also: Ag
1ce1a 67 53 74 65 70 20 61 6e 64 20 41 67 67 46 69 6e gStep and AggFin
1ce1b 61 6c 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 46 75 al.*/.case OP_Fu
1ce1c 6e 63 74 69 6f 6e 3a 20 7b 0a 23 69 66 20 30 20 nction: {.#if 0
1ce1d 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1ce1e 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1ce1f 2e 61 67 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 0a .ag */. int i;.
1ce20 20 20 4d 65 6d 20 2a 70 41 72 67 3b 0a 20 20 73 Mem *pArg;. s
1ce21 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 63 qlite3_context c
1ce22 74 78 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 tx;. sqlite3_va
1ce23 6c 75 65 20 2a 2a 61 70 56 61 6c 3b 0a 20 20 69 lue **apVal;. i
1ce24 6e 74 20 6e 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 nt n;.#endif /*
1ce25 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1ce26 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 67 20 moved into u.ag
1ce27 2a 2f 0a 0a 20 20 75 2e 61 67 2e 6e 20 3d 20 70 */.. u.ag.n = p
1ce28 4f 70 2d 3e 70 35 3b 0a 20 20 75 2e 61 67 2e 61 Op->p5;. u.ag.a
1ce29 70 56 61 6c 20 3d 20 70 2d 3e 61 70 41 72 67 3b pVal = p->apArg;
1ce2a 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 67 2e . assert( u.ag.
1ce2b 61 70 56 61 6c 20 7c 7c 20 75 2e 61 67 2e 6e 3d apVal || u.ag.n=
1ce2c 3d 30 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 =0 );.. assert(
1ce2d 20 75 2e 61 67 2e 6e 3d 3d 30 20 7c 7c 20 28 70 u.ag.n==0 || (p
1ce2e 4f 70 2d 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d Op->p2>0 && pOp-
1ce2f 3e 70 32 2b 75 2e 61 67 2e 6e 3c 3d 70 2d 3e 6e >p2+u.ag.n<=p->n
1ce30 4d 65 6d 2b 31 29 20 29 3b 0a 20 20 61 73 73 65 Mem+1) );. asse
1ce31 72 74 28 20 70 4f 70 2d 3e 70 33 3c 70 4f 70 2d rt( pOp->p3<pOp-
1ce32 3e 70 32 20 7c 7c 20 70 4f 70 2d 3e 70 33 3e 3d >p2 || pOp->p3>=
1ce33 70 4f 70 2d 3e 70 32 2b 75 2e 61 67 2e 6e 20 29 pOp->p2+u.ag.n )
1ce34 3b 0a 20 20 75 2e 61 67 2e 70 41 72 67 20 3d 20 ;. u.ag.pArg =
1ce35 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 &p->aMem[pOp->p2
1ce36 5d 3b 0a 20 20 66 6f 72 28 75 2e 61 67 2e 69 3d ];. for(u.ag.i=
1ce37 30 3b 20 75 2e 61 67 2e 69 3c 75 2e 61 67 2e 6e 0; u.ag.i<u.ag.n
1ce38 3b 20 75 2e 61 67 2e 69 2b 2b 2c 20 75 2e 61 67 ; u.ag.i++, u.ag
1ce39 2e 70 41 72 67 2b 2b 29 7b 0a 20 20 20 20 75 2e .pArg++){. u.
1ce3a 61 67 2e 61 70 56 61 6c 5b 75 2e 61 67 2e 69 5d ag.apVal[u.ag.i]
1ce3b 20 3d 20 75 2e 61 67 2e 70 41 72 67 3b 0a 20 20 = u.ag.pArg;.
1ce3c 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1ce3d 53 74 6f 72 65 54 79 70 65 28 75 2e 61 67 2e 70 StoreType(u.ag.p
1ce3e 41 72 67 29 3b 0a 20 20 20 20 52 45 47 49 53 54 Arg);. REGIST
1ce3f 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 ER_TRACE(pOp->p2
1ce40 2c 20 75 2e 61 67 2e 70 41 72 67 29 3b 0a 20 20 , u.ag.pArg);.
1ce41 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 }.. assert( pOp
1ce42 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 46 55 4e ->p4type==P4_FUN
1ce43 43 44 45 46 20 7c 7c 20 70 4f 70 2d 3e 70 34 74 CDEF || pOp->p4t
1ce44 79 70 65 3d 3d 50 34 5f 56 44 42 45 46 55 4e 43 ype==P4_VDBEFUNC
1ce45 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 );. if( pOp->p
1ce46 34 74 79 70 65 3d 3d 50 34 5f 46 55 4e 43 44 45 4type==P4_FUNCDE
1ce47 46 20 29 7b 0a 20 20 20 20 75 2e 61 67 2e 63 74 F ){. u.ag.ct
1ce48 78 2e 70 46 75 6e 63 20 3d 20 70 4f 70 2d 3e 70 x.pFunc = pOp->p
1ce49 34 2e 70 46 75 6e 63 3b 0a 20 20 20 20 75 2e 61 4.pFunc;. u.a
1ce4a 67 2e 63 74 78 2e 70 56 64 62 65 46 75 6e 63 20 g.ctx.pVdbeFunc
1ce4b 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 = 0;. }else{.
1ce4c 20 20 75 2e 61 67 2e 63 74 78 2e 70 56 64 62 65 u.ag.ctx.pVdbe
1ce4d 46 75 6e 63 20 3d 20 28 56 64 62 65 46 75 6e 63 Func = (VdbeFunc
1ce4e 2a 29 70 4f 70 2d 3e 70 34 2e 70 56 64 62 65 46 *)pOp->p4.pVdbeF
1ce4f 75 6e 63 3b 0a 20 20 20 20 75 2e 61 67 2e 63 74 unc;. u.ag.ct
1ce50 78 2e 70 46 75 6e 63 20 3d 20 75 2e 61 67 2e 63 x.pFunc = u.ag.c
1ce51 74 78 2e 70 56 64 62 65 46 75 6e 63 2d 3e 70 46 tx.pVdbeFunc->pF
1ce52 75 6e 63 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 unc;. }.. asse
1ce53 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 rt( pOp->p3>0 &&
1ce54 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 pOp->p3<=p->nMe
1ce55 6d 20 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 m );. pOut = &p
1ce56 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b ->aMem[pOp->p3];
1ce57 0a 20 20 75 2e 61 67 2e 63 74 78 2e 73 2e 66 6c . u.ag.ctx.s.fl
1ce58 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a ags = MEM_Null;.
1ce59 20 20 75 2e 61 67 2e 63 74 78 2e 73 2e 64 62 20 u.ag.ctx.s.db
1ce5a 3d 20 64 62 3b 0a 20 20 75 2e 61 67 2e 63 74 78 = db;. u.ag.ctx
1ce5b 2e 73 2e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 75 .s.xDel = 0;. u
1ce5c 2e 61 67 2e 63 74 78 2e 73 2e 7a 4d 61 6c 6c 6f .ag.ctx.s.zMallo
1ce5d 63 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 68 65 c = 0;.. /* The
1ce5e 20 6f 75 74 70 75 74 20 63 65 6c 6c 20 6d 61 79 output cell may
1ce5f 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 61 20 already have a
1ce60 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 buffer allocated
1ce61 2e 20 4d 6f 76 65 0a 20 20 2a 2a 20 74 68 65 20 . Move. ** the
1ce62 70 6f 69 6e 74 65 72 20 74 6f 20 75 2e 61 67 2e pointer to u.ag.
1ce63 63 74 78 2e 73 20 73 6f 20 69 6e 20 63 61 73 65 ctx.s so in case
1ce64 20 74 68 65 20 75 73 65 72 2d 66 75 6e 63 74 69 the user-functi
1ce65 6f 6e 20 63 61 6e 20 75 73 65 0a 20 20 2a 2a 20 on can use. **
1ce66 74 68 65 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f the already allo
1ce67 63 61 74 65 64 20 62 75 66 66 65 72 20 69 6e 73 cated buffer ins
1ce68 74 65 61 64 20 6f 66 20 61 6c 6c 6f 63 61 74 69 tead of allocati
1ce69 6e 67 20 61 20 6e 65 77 20 6f 6e 65 2e 0a 20 20 ng a new one..
1ce6a 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 */. sqlite3Vdbe
1ce6b 4d 65 6d 4d 6f 76 65 28 26 75 2e 61 67 2e 63 74 MemMove(&u.ag.ct
1ce6c 78 2e 73 2c 20 70 4f 75 74 29 3b 0a 20 20 4d 65 x.s, pOut);. Me
1ce6d 6d 53 65 74 54 79 70 65 46 6c 61 67 28 26 75 2e mSetTypeFlag(&u.
1ce6e 61 67 2e 63 74 78 2e 73 2c 20 4d 45 4d 5f 4e 75 ag.ctx.s, MEM_Nu
1ce6f 6c 6c 29 3b 0a 0a 20 20 75 2e 61 67 2e 63 74 78 ll);.. u.ag.ctx
1ce70 2e 69 73 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 .isError = 0;.
1ce71 69 66 28 20 75 2e 61 67 2e 63 74 78 2e 70 46 75 if( u.ag.ctx.pFu
1ce72 6e 63 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 nc->flags & SQLI
1ce73 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c TE_FUNC_NEEDCOLL
1ce74 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
1ce75 70 4f 70 3e 70 2d 3e 61 4f 70 20 29 3b 0a 20 20 pOp>p->aOp );.
1ce76 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d 31 assert( pOp[-1
1ce77 5d 2e 70 34 74 79 70 65 3d 3d 50 34 5f 43 4f 4c ].p4type==P4_COL
1ce78 4c 53 45 51 20 29 3b 0a 20 20 20 20 61 73 73 65 LSEQ );. asse
1ce79 72 74 28 20 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f rt( pOp[-1].opco
1ce7a 64 65 3d 3d 4f 50 5f 43 6f 6c 6c 53 65 71 20 29 de==OP_CollSeq )
1ce7b 3b 0a 20 20 20 20 75 2e 61 67 2e 63 74 78 2e 70 ;. u.ag.ctx.p
1ce7c 43 6f 6c 6c 20 3d 20 70 4f 70 5b 2d 31 5d 2e 70 Coll = pOp[-1].p
1ce7d 34 2e 70 43 6f 6c 6c 3b 0a 20 20 7d 0a 20 20 69 4.pColl;. }. i
1ce7e 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 f( sqlite3Safety
1ce7f 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 Off(db) ) goto a
1ce80 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 bort_due_to_misu
1ce81 73 65 3b 0a 20 20 28 2a 75 2e 61 67 2e 63 74 78 se;. (*u.ag.ctx
1ce82 2e 70 46 75 6e 63 2d 3e 78 46 75 6e 63 29 28 26 .pFunc->xFunc)(&
1ce83 75 2e 61 67 2e 63 74 78 2c 20 75 2e 61 67 2e 6e u.ag.ctx, u.ag.n
1ce84 2c 20 75 2e 61 67 2e 61 70 56 61 6c 29 3b 0a 20 , u.ag.apVal);.
1ce85 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 if( sqlite3Safe
1ce86 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 20 tyOn(db) ){.
1ce87 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
1ce88 6c 65 61 73 65 28 26 75 2e 61 67 2e 63 74 78 2e lease(&u.ag.ctx.
1ce89 73 29 3b 0a 20 20 20 20 67 6f 74 6f 20 61 62 6f s);. goto abo
1ce8a 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 rt_due_to_misuse
1ce8b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e ;. }. if( db->
1ce8c 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a mallocFailed ){.
1ce8d 20 20 20 20 2f 2a 20 45 76 65 6e 20 74 68 6f 75 /* Even thou
1ce8e 67 68 20 61 20 6d 61 6c 6c 6f 63 28 29 20 68 61 gh a malloc() ha
1ce8f 73 20 66 61 69 6c 65 64 2c 20 74 68 65 20 69 6d s failed, the im
1ce90 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
1ce91 74 68 65 0a 20 20 20 20 2a 2a 20 75 73 65 72 20 the. ** user
1ce92 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 68 61 76 function may hav
1ce93 65 20 63 61 6c 6c 65 64 20 61 6e 20 73 71 6c 69 e called an sqli
1ce94 74 65 33 5f 72 65 73 75 6c 74 5f 58 58 58 28 29 te3_result_XXX()
1ce95 20 66 75 6e 63 74 69 6f 6e 0a 20 20 20 20 2a 2a function. **
1ce96 20 74 6f 20 72 65 74 75 72 6e 20 61 20 76 61 6c to return a val
1ce97 75 65 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e ue. The followin
1ce98 67 20 63 61 6c 6c 20 72 65 6c 65 61 73 65 73 20 g call releases
1ce99 61 6e 79 20 72 65 73 6f 75 72 63 65 73 0a 20 20 any resources.
1ce9a 20 20 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 ** associated
1ce9b 77 69 74 68 20 73 75 63 68 20 61 20 76 61 6c 75 with such a valu
1ce9c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a e.. **. **
1ce9d 20 4e 6f 74 65 3a 20 4d 61 79 62 65 20 4d 65 6d Note: Maybe Mem
1ce9e 52 65 6c 65 61 73 65 28 29 20 73 68 6f 75 6c 64 Release() should
1ce9f 20 62 65 20 63 61 6c 6c 65 64 20 69 66 20 73 71 be called if sq
1cea0 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 29 0a lite3SafetyOn().
1cea1 20 20 20 20 2a 2a 20 66 61 69 6c 73 20 61 6c 73 ** fails als
1cea2 6f 20 28 74 68 65 20 69 66 28 2e 2e 2e 29 20 73 o (the if(...) s
1cea3 74 61 74 65 6d 65 6e 74 20 61 62 6f 76 65 29 2e tatement above).
1cea4 20 42 75 74 20 69 66 20 70 65 6f 70 6c 65 20 61 But if people a
1cea5 72 65 0a 20 20 20 20 2a 2a 20 6d 69 73 75 73 69 re. ** misusi
1cea6 6e 67 20 73 71 6c 69 74 65 2c 20 74 68 65 79 20 ng sqlite, they
1cea7 68 61 76 65 20 62 69 67 67 65 72 20 70 72 6f 62 have bigger prob
1cea8 6c 65 6d 73 20 74 68 61 6e 20 61 20 6c 65 61 6b lems than a leak
1cea9 65 64 20 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2f ed value.. */
1ceaa 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1ceab 4d 65 6d 52 65 6c 65 61 73 65 28 26 75 2e 61 67 MemRelease(&u.ag
1ceac 2e 63 74 78 2e 73 29 3b 0a 20 20 20 20 67 6f 74 .ctx.s);. got
1cead 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 0a 20 o no_mem;. }..
1ceae 20 2f 2a 20 49 66 20 61 6e 79 20 61 75 78 69 6c /* If any auxil
1ceaf 69 61 72 79 20 64 61 74 61 20 66 75 6e 63 74 69 iary data functi
1ceb0 6f 6e 73 20 68 61 76 65 20 62 65 65 6e 20 63 61 ons have been ca
1ceb1 6c 6c 65 64 20 62 79 20 74 68 69 73 20 75 73 65 lled by this use
1ceb2 72 20 66 75 6e 63 74 69 6f 6e 2c 0a 20 20 2a 2a r function,. **
1ceb3 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 63 61 6c immediately cal
1ceb4 6c 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 l the destructor
1ceb5 20 66 6f 72 20 61 6e 79 20 6e 6f 6e 2d 73 74 61 for any non-sta
1ceb6 74 69 63 20 76 61 6c 75 65 73 2e 0a 20 20 2a 2f tic values.. */
1ceb7 0a 20 20 69 66 28 20 75 2e 61 67 2e 63 74 78 2e . if( u.ag.ctx.
1ceb8 70 56 64 62 65 46 75 6e 63 20 29 7b 0a 20 20 20 pVdbeFunc ){.
1ceb9 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 sqlite3VdbeDele
1ceba 74 65 41 75 78 44 61 74 61 28 75 2e 61 67 2e 63 teAuxData(u.ag.c
1cebb 74 78 2e 70 56 64 62 65 46 75 6e 63 2c 20 70 4f tx.pVdbeFunc, pO
1cebc 70 2d 3e 70 31 29 3b 0a 20 20 20 20 70 4f 70 2d p->p1);. pOp-
1cebd 3e 70 34 2e 70 56 64 62 65 46 75 6e 63 20 3d 20 >p4.pVdbeFunc =
1cebe 75 2e 61 67 2e 63 74 78 2e 70 56 64 62 65 46 75 u.ag.ctx.pVdbeFu
1cebf 6e 63 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 nc;. pOp->p4t
1cec0 79 70 65 20 3d 20 50 34 5f 56 44 42 45 46 55 4e ype = P4_VDBEFUN
1cec1 43 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 C;. }.. /* If
1cec2 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 the function ret
1cec3 75 72 6e 65 64 20 61 6e 20 65 72 72 6f 72 2c 20 urned an error,
1cec4 74 68 72 6f 77 20 61 6e 20 65 78 63 65 70 74 69 throw an excepti
1cec5 6f 6e 20 2a 2f 0a 20 20 69 66 28 20 75 2e 61 67 on */. if( u.ag
1cec6 2e 63 74 78 2e 69 73 45 72 72 6f 72 20 29 7b 0a .ctx.isError ){.
1cec7 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 sqlite3SetSt
1cec8 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 ring(&p->zErrMsg
1cec9 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 , db, "%s", sqli
1ceca 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 26 te3_value_text(&
1cecb 75 2e 61 67 2e 63 74 78 2e 73 29 29 3b 0a 20 20 u.ag.ctx.s));.
1cecc 20 20 72 63 20 3d 20 75 2e 61 67 2e 63 74 78 2e rc = u.ag.ctx.
1cecd 69 73 45 72 72 6f 72 3b 0a 20 20 7d 0a 0a 20 20 isError;. }..
1cece 2f 2a 20 43 6f 70 79 20 74 68 65 20 72 65 73 75 /* Copy the resu
1cecf 6c 74 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 lt of the functi
1ced0 6f 6e 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 on into register
1ced1 20 50 33 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 P3 */. sqlite3
1ced2 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 VdbeChangeEncodi
1ced3 6e 67 28 26 75 2e 61 67 2e 63 74 78 2e 73 2c 20 ng(&u.ag.ctx.s,
1ced4 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 73 71 6c encoding);. sql
1ced5 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 ite3VdbeMemMove(
1ced6 70 4f 75 74 2c 20 26 75 2e 61 67 2e 63 74 78 2e pOut, &u.ag.ctx.
1ced7 73 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 s);. if( sqlite
1ced8 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 70 3VdbeMemTooBig(p
1ced9 4f 75 74 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f Out) ){. goto
1ceda 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 too_big;. }.
1cedb 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 REGISTER_TRACE(p
1cedc 4f 70 2d 3e 70 33 2c 20 70 4f 75 74 29 3b 0a 20 Op->p3, pOut);.
1cedd 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 UPDATE_MAX_BLOB
1cede 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 SIZE(pOut);. br
1cedf 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1cee0 65 3a 20 42 69 74 41 6e 64 20 50 31 20 50 32 20 e: BitAnd P1 P2
1cee1 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b P3 * *.**.** Tak
1cee2 65 20 74 68 65 20 62 69 74 2d 77 69 73 65 20 41 e the bit-wise A
1cee3 4e 44 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73 ND of the values
1cee4 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 in register P1
1cee5 61 6e 64 20 50 32 20 61 6e 64 0a 2a 2a 20 73 74 and P2 and.** st
1cee6 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 ore the result i
1cee7 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a n register P3..*
1cee8 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 * If either inpu
1cee9 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 t is NULL, the r
1ceea 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a esult is NULL..*
1ceeb 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 69 74 /./* Opcode: Bit
1ceec 4f 72 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a Or P1 P2 P3 * *.
1ceed 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20 62 **.** Take the b
1ceee 69 74 2d 77 69 73 65 20 4f 52 20 6f 66 20 74 68 it-wise OR of th
1ceef 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 e values in regi
1cef0 73 74 65 72 20 50 31 20 61 6e 64 20 50 32 20 61 ster P1 and P2 a
1cef1 6e 64 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 nd.** store the
1cef2 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 result in regist
1cef3 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 er P3..** If eit
1cef4 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c her input is NUL
1cef5 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 L, the result is
1cef6 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 NULL..*/./* Opc
1cef7 6f 64 65 3a 20 53 68 69 66 74 4c 65 66 74 20 50 ode: ShiftLeft P
1cef8 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 1 P2 P3 * *.**.*
1cef9 2a 20 53 68 69 66 74 20 74 68 65 20 69 6e 74 65 * Shift the inte
1cefa 67 65 72 20 76 61 6c 75 65 20 69 6e 20 72 65 67 ger value in reg
1cefb 69 73 74 65 72 20 50 32 20 74 6f 20 74 68 65 20 ister P2 to the
1cefc 6c 65 66 74 20 62 79 20 74 68 65 0a 2a 2a 20 6e left by the.** n
1cefd 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 73 70 umber of bits sp
1cefe 65 63 69 66 69 65 64 20 62 79 20 74 68 65 20 69 ecified by the i
1ceff 6e 74 65 67 65 72 20 69 6e 20 72 65 67 69 73 65 nteger in regise
1cf00 72 20 50 31 2e 0a 2a 2a 20 53 74 6f 72 65 20 74 r P1..** Store t
1cf01 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 he result in reg
1cf02 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 ister P3..** If
1cf03 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 20 either input is
1cf04 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 NULL, the result
1cf05 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 is NULL..*/./*
1cf06 4f 70 63 6f 64 65 3a 20 53 68 69 66 74 52 69 67 Opcode: ShiftRig
1cf07 68 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a ht P1 P2 P3 * *.
1cf08 2a 2a 0a 2a 2a 20 53 68 69 66 74 20 74 68 65 20 **.** Shift the
1cf09 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 69 6e integer value in
1cf0a 20 72 65 67 69 73 74 65 72 20 50 32 20 74 6f 20 register P2 to
1cf0b 74 68 65 20 72 69 67 68 74 20 62 79 20 74 68 65 the right by the
1cf0c 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 .** number of bi
1cf0d 74 73 20 73 70 65 63 69 66 69 65 64 20 62 79 20 ts specified by
1cf0e 74 68 65 20 69 6e 74 65 67 65 72 20 69 6e 20 72 the integer in r
1cf0f 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a 20 53 egister P1..** S
1cf10 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 tore the result
1cf11 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a in register P3..
1cf12 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 ** If either inp
1cf13 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 ut is NULL, the
1cf14 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a result is NULL..
1cf15 2a 2f 0a 63 61 73 65 20 4f 50 5f 42 69 74 41 6e */.case OP_BitAn
1cf16 64 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d:
1cf17 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
1cf18 5f 42 49 54 41 4e 44 2c 20 69 6e 31 2c 20 69 6e _BITAND, in1, in
1cf19 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 2, out3 */.case
1cf1a 4f 50 5f 42 69 74 4f 72 3a 20 20 20 20 20 20 20 OP_BitOr:
1cf1b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 /* sa
1cf1c 6d 65 20 61 73 20 54 4b 5f 42 49 54 4f 52 2c 20 me as TK_BITOR,
1cf1d 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a in1, in2, out3 *
1cf1e 2f 0a 63 61 73 65 20 4f 50 5f 53 68 69 66 74 4c /.case OP_ShiftL
1cf1f 65 66 74 3a 20 20 20 20 20 20 20 20 20 20 20 20 eft:
1cf20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
1cf21 4c 53 48 49 46 54 2c 20 69 6e 31 2c 20 69 6e 32 LSHIFT, in1, in2
1cf22 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f , out3 */.case O
1cf23 50 5f 53 68 69 66 74 52 69 67 68 74 3a 20 7b 20 P_ShiftRight: {
1cf24 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d /* sam
1cf25 65 20 61 73 20 54 4b 5f 52 53 48 49 46 54 2c 20 e as TK_RSHIFT,
1cf26 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a in1, in2, out3 *
1cf27 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 /.#if 0 /* loca
1cf28 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1cf29 64 20 69 6e 74 6f 20 75 2e 61 68 20 2a 2f 0a 20 d into u.ah */.
1cf2a 20 69 36 34 20 61 3b 0a 20 20 69 36 34 20 62 3b i64 a;. i64 b;
1cf2b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1cf2c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1cf2d 20 69 6e 74 6f 20 75 2e 61 68 20 2a 2f 0a 0a 20 into u.ah */..
1cf2e 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 if( (pIn1->flag
1cf2f 73 20 7c 20 70 49 6e 32 2d 3e 66 6c 61 67 73 29 s | pIn2->flags)
1cf30 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 & MEM_Null ){.
1cf31 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
1cf32 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a mSetNull(pOut);.
1cf33 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 break;. }.
1cf34 20 75 2e 61 68 2e 61 20 3d 20 73 71 6c 69 74 65 u.ah.a = sqlite
1cf35 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 3VdbeIntValue(pI
1cf36 6e 32 29 3b 0a 20 20 75 2e 61 68 2e 62 20 3d 20 n2);. u.ah.b =
1cf37 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 sqlite3VdbeIntVa
1cf38 6c 75 65 28 70 49 6e 31 29 3b 0a 20 20 73 77 69 lue(pIn1);. swi
1cf39 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 tch( pOp->opcode
1cf3a 20 29 7b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f ){. case OP_
1cf3b 42 69 74 41 6e 64 3a 20 20 20 20 20 20 75 2e 61 BitAnd: u.a
1cf3c 68 2e 61 20 26 3d 20 75 2e 61 68 2e 62 3b 20 20 h.a &= u.ah.b;
1cf3d 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 break;. ca
1cf3e 73 65 20 4f 50 5f 42 69 74 4f 72 3a 20 20 20 20 se OP_BitOr:
1cf3f 20 20 20 75 2e 61 68 2e 61 20 7c 3d 20 75 2e 61 u.ah.a |= u.a
1cf40 68 2e 62 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a h.b; break;.
1cf41 20 20 20 20 63 61 73 65 20 4f 50 5f 53 68 69 66 case OP_Shif
1cf42 74 4c 65 66 74 3a 20 20 20 75 2e 61 68 2e 61 20 tLeft: u.ah.a
1cf43 3c 3c 3d 20 75 2e 61 68 2e 62 3b 20 20 20 20 62 <<= u.ah.b; b
1cf44 72 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c reak;. defaul
1cf45 74 3a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d t: assert( pOp-
1cf46 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 53 68 69 66 >opcode==OP_Shif
1cf47 74 52 69 67 68 74 20 29 3b 0a 20 20 20 20 20 20 tRight );.
1cf48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cf49 20 20 20 75 2e 61 68 2e 61 20 3e 3e 3d 20 75 2e u.ah.a >>= u.
1cf4a 61 68 2e 62 3b 20 20 20 20 62 72 65 61 6b 3b 0a ah.b; break;.
1cf4b 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 }. pOut->u.i
1cf4c 3d 20 75 2e 61 68 2e 61 3b 0a 20 20 4d 65 6d 53 = u.ah.a;. MemS
1cf4d 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c etTypeFlag(pOut,
1cf4e 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65 MEM_Int);. bre
1cf4f 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1cf50 3a 20 41 64 64 49 6d 6d 20 20 50 31 20 50 32 20 : AddImm P1 P2
1cf51 2a 20 2a 20 2a 0a 2a 2a 20 0a 2a 2a 20 41 64 64 * * *.** .** Add
1cf52 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 50 32 the constant P2
1cf53 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 69 6e to the value in
1cf54 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a register P1..**
1cf55 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 The result is a
1cf56 6c 77 61 79 73 20 61 6e 20 69 6e 74 65 67 65 72 lways an integer
1cf57 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 66 6f 72 63 65 ..**.** To force
1cf58 20 61 6e 79 20 72 65 67 69 73 74 65 72 20 74 6f any register to
1cf59 20 62 65 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 be an integer,
1cf5a 6a 75 73 74 20 61 64 64 20 30 2e 0a 2a 2f 0a 63 just add 0..*/.c
1cf5b 61 73 65 20 4f 50 5f 41 64 64 49 6d 6d 3a 20 7b ase OP_AddImm: {
1cf5c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 /* i
1cf5d 6e 31 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 n1 */. sqlite3V
1cf5e 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 dbeMemIntegerify
1cf5f 28 70 49 6e 31 29 3b 0a 20 20 70 49 6e 31 2d 3e (pIn1);. pIn1->
1cf60 75 2e 69 20 2b 3d 20 70 4f 70 2d 3e 70 32 3b 0a u.i += pOp->p2;.
1cf61 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1cf62 70 63 6f 64 65 3a 20 4d 75 73 74 42 65 49 6e 74 pcode: MustBeInt
1cf63 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 20 P1 P2 * * *.**
1cf64 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 76 61 .** Force the va
1cf65 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 lue in register
1cf66 50 31 20 74 6f 20 62 65 20 61 6e 20 69 6e 74 65 P1 to be an inte
1cf67 67 65 72 2e 20 20 49 66 20 74 68 65 20 76 61 6c ger. If the val
1cf68 75 65 0a 2a 2a 20 69 6e 20 50 31 20 69 73 20 6e ue.** in P1 is n
1cf69 6f 74 20 61 6e 20 69 6e 74 65 67 65 72 20 61 6e ot an integer an
1cf6a 64 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 d cannot be conv
1cf6b 65 72 74 65 64 20 69 6e 74 6f 20 61 6e 20 69 6e erted into an in
1cf6c 74 65 67 65 72 0a 2a 2a 20 77 69 74 68 6f 75 74 teger.** without
1cf6d 20 64 61 74 61 20 6c 6f 73 73 2c 20 74 68 65 6e data loss, then
1cf6e 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c jump immediatel
1cf6f 79 20 74 6f 20 50 32 2c 20 6f 72 20 69 66 20 50 y to P2, or if P
1cf70 32 3d 3d 30 0a 2a 2a 20 72 61 69 73 65 20 61 6e 2==0.** raise an
1cf71 20 53 51 4c 49 54 45 5f 4d 49 53 4d 41 54 43 48 SQLITE_MISMATCH
1cf72 20 65 78 63 65 70 74 69 6f 6e 2e 0a 2a 2f 0a 63 exception..*/.c
1cf73 61 73 65 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 ase OP_MustBeInt
1cf74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f : { /
1cf75 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 * jump, in1 */.
1cf76 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 applyAffinity(p
1cf77 49 6e 31 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f In1, SQLITE_AFF_
1cf78 4e 55 4d 45 52 49 43 2c 20 65 6e 63 6f 64 69 6e NUMERIC, encodin
1cf79 67 29 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 2d g);. if( (pIn1-
1cf7a 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 >flags & MEM_Int
1cf7b 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 )==0 ){. if(
1cf7c 70 4f 70 2d 3e 70 32 3d 3d 30 20 29 7b 0a 20 20 pOp->p2==0 ){.
1cf7d 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1cf7e 4d 49 53 4d 41 54 43 48 3b 0a 20 20 20 20 20 20 MISMATCH;.
1cf7f 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1cf80 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 65 6c o_error;. }el
1cf81 73 65 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 se{. pc = p
1cf82 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 Op->p2 - 1;.
1cf83 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4d }. }else{. M
1cf84 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 49 emSetTypeFlag(pI
1cf85 6e 31 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 n1, MEM_Int);.
1cf86 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a }. break;.}../*
1cf87 20 4f 70 63 6f 64 65 3a 20 52 65 61 6c 41 66 66 Opcode: RealAff
1cf88 69 6e 69 74 79 20 50 31 20 2a 20 2a 20 2a 20 2a inity P1 * * * *
1cf89 0a 2a 2a 0a 2a 2a 20 49 66 20 72 65 67 69 73 74 .**.** If regist
1cf8a 65 72 20 50 31 20 68 6f 6c 64 73 20 61 6e 20 69 er P1 holds an i
1cf8b 6e 74 65 67 65 72 20 63 6f 6e 76 65 72 74 20 69 nteger convert i
1cf8c 74 20 74 6f 20 61 20 72 65 61 6c 20 76 61 6c 75 t to a real valu
1cf8d 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 e..**.** This op
1cf8e 63 6f 64 65 20 69 73 20 75 73 65 64 20 77 68 65 code is used whe
1cf8f 6e 20 65 78 74 72 61 63 74 69 6e 67 20 69 6e 66 n extracting inf
1cf90 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 ormation from a
1cf91 63 6f 6c 75 6d 6e 20 74 68 61 74 0a 2a 2a 20 68 column that.** h
1cf92 61 73 20 52 45 41 4c 20 61 66 66 69 6e 69 74 79 as REAL affinity
1cf93 2e 20 20 53 75 63 68 20 63 6f 6c 75 6d 6e 20 76 . Such column v
1cf94 61 6c 75 65 73 20 6d 61 79 20 73 74 69 6c 6c 20 alues may still
1cf95 62 65 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a 20 be stored as.**
1cf96 69 6e 74 65 67 65 72 73 2c 20 66 6f 72 20 73 70 integers, for sp
1cf97 61 63 65 20 65 66 66 69 63 69 65 6e 63 79 2c 20 ace efficiency,
1cf98 62 75 74 20 61 66 74 65 72 20 65 78 74 72 61 63 but after extrac
1cf99 74 69 6f 6e 20 77 65 20 77 61 6e 74 20 74 68 65 tion we want the
1cf9a 6d 0a 2a 2a 20 74 6f 20 68 61 76 65 20 6f 6e 6c m.** to have onl
1cf9b 79 20 61 20 72 65 61 6c 20 76 61 6c 75 65 2e 0a y a real value..
1cf9c 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 61 6c 41 */.case OP_RealA
1cf9d 66 66 69 6e 69 74 79 3a 20 7b 20 20 20 20 20 20 ffinity: {
1cf9e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 /* i
1cf9f 6e 31 20 2a 2f 0a 20 20 69 66 28 20 70 49 6e 31 n1 */. if( pIn1
1cfa0 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e ->flags & MEM_In
1cfa1 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 t ){. sqlite3
1cfa2 56 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 70 VdbeMemRealify(p
1cfa3 49 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 In1);. }. brea
1cfa4 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 k;.}..#ifndef SQ
1cfa5 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 2f LITE_OMIT_CAST./
1cfa6 2a 20 4f 70 63 6f 64 65 3a 20 54 6f 54 65 78 74 * Opcode: ToText
1cfa7 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a P1 * * * *.**.*
1cfa8 2a 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75 * Force the valu
1cfa9 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 e in register P1
1cfaa 20 74 6f 20 62 65 20 74 65 78 74 2e 0a 2a 2a 20 to be text..**
1cfab 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 If the value is
1cfac 6e 75 6d 65 72 69 63 2c 20 63 6f 6e 76 65 72 74 numeric, convert
1cfad 20 69 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20 it to a string
1cfae 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 65 71 75 using the.** equ
1cfaf 69 76 61 6c 65 6e 74 20 6f 66 20 70 72 69 6e 74 ivalent of print
1cfb0 66 28 29 2e 20 20 42 6c 6f 62 20 76 61 6c 75 65 f(). Blob value
1cfb1 73 20 61 72 65 20 75 6e 63 68 61 6e 67 65 64 20 s are unchanged
1cfb2 61 6e 64 0a 2a 2a 20 61 72 65 20 61 66 74 65 72 and.** are after
1cfb3 77 61 72 64 73 20 73 69 6d 70 6c 79 20 69 6e 74 wards simply int
1cfb4 65 72 70 72 65 74 65 64 20 61 73 20 74 65 78 74 erpreted as text
1cfb5 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 ..**.** A NULL v
1cfb6 61 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61 6e alue is not chan
1cfb7 67 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 ged by this rout
1cfb8 69 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 ine. It remains
1cfb9 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f NULL..*/.case O
1cfba 50 5f 54 6f 54 65 78 74 3a 20 7b 20 20 20 20 20 P_ToText: {
1cfbb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1cfbc 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 54 45 same as TK_TO_TE
1cfbd 58 54 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 XT, in1 */. if(
1cfbe 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d pIn1->flags & M
1cfbf 45 4d 5f 4e 75 6c 6c 20 29 20 62 72 65 61 6b 3b EM_Null ) break;
1cfc0 0a 20 20 61 73 73 65 72 74 28 20 4d 45 4d 5f 53 . assert( MEM_S
1cfc1 74 72 3d 3d 28 4d 45 4d 5f 42 6c 6f 62 3e 3e 33 tr==(MEM_Blob>>3
1cfc2 29 20 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 ) );. pIn1->fla
1cfc3 67 73 20 7c 3d 20 28 70 49 6e 31 2d 3e 66 6c 61 gs |= (pIn1->fla
1cfc4 67 73 26 4d 45 4d 5f 42 6c 6f 62 29 3e 3e 33 3b gs&MEM_Blob)>>3;
1cfc5 0a 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 . applyAffinity
1cfc6 28 70 49 6e 31 2c 20 53 51 4c 49 54 45 5f 41 46 (pIn1, SQLITE_AF
1cfc7 46 5f 54 45 58 54 2c 20 65 6e 63 6f 64 69 6e 67 F_TEXT, encoding
1cfc8 29 3b 0a 20 20 72 63 20 3d 20 45 78 70 61 6e 64 );. rc = Expand
1cfc9 42 6c 6f 62 28 70 49 6e 31 29 3b 0a 20 20 61 73 Blob(pIn1);. as
1cfca 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 sert( pIn1->flag
1cfcb 73 20 26 20 4d 45 4d 5f 53 74 72 20 7c 7c 20 64 s & MEM_Str || d
1cfcc 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1cfcd 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73 );. pIn1->flags
1cfce 20 26 3d 20 7e 28 4d 45 4d 5f 49 6e 74 7c 4d 45 &= ~(MEM_Int|ME
1cfcf 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 42 6c 6f 62 7c M_Real|MEM_Blob|
1cfd0 4d 45 4d 5f 5a 65 72 6f 29 3b 0a 20 20 55 50 44 MEM_Zero);. UPD
1cfd1 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 ATE_MAX_BLOBSIZE
1cfd2 28 70 49 6e 31 29 3b 0a 20 20 62 72 65 61 6b 3b (pIn1);. break;
1cfd3 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 .}../* Opcode: T
1cfd4 6f 42 6c 6f 62 20 50 31 20 2a 20 2a 20 2a 20 2a oBlob P1 * * * *
1cfd5 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 .**.** Force the
1cfd6 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 value in regist
1cfd7 65 72 20 50 31 20 74 6f 20 62 65 20 61 20 42 4c er P1 to be a BL
1cfd8 4f 42 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 OB..** If the va
1cfd9 6c 75 65 20 69 73 20 6e 75 6d 65 72 69 63 2c 20 lue is numeric,
1cfda 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 20 convert it to a
1cfdb 73 74 72 69 6e 67 20 66 69 72 73 74 2e 0a 2a 2a string first..**
1cfdc 20 53 74 72 69 6e 67 73 20 61 72 65 20 73 69 6d Strings are sim
1cfdd 70 6c 79 20 72 65 69 6e 74 65 72 70 72 65 74 65 ply reinterprete
1cfde 64 20 61 73 20 62 6c 6f 62 73 20 77 69 74 68 20 d as blobs with
1cfdf 6e 6f 20 63 68 61 6e 67 65 0a 2a 2a 20 74 6f 20 no change.** to
1cfe0 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 the underlying d
1cfe1 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c ata..**.** A NUL
1cfe2 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 63 L value is not c
1cfe3 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20 72 hanged by this r
1cfe4 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 65 6d 61 outine. It rema
1cfe5 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 ins NULL..*/.cas
1cfe6 65 20 4f 50 5f 54 6f 42 6c 6f 62 3a 20 7b 20 20 e OP_ToBlob: {
1cfe7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cfe8 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f /* same as TK_TO
1cfe9 5f 42 4c 4f 42 2c 20 69 6e 31 20 2a 2f 0a 20 20 _BLOB, in1 */.
1cfea 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 if( pIn1->flags
1cfeb 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 20 62 72 65 & MEM_Null ) bre
1cfec 61 6b 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 2d ak;. if( (pIn1-
1cfed 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f >flags & MEM_Blo
1cfee 62 29 3d 3d 30 20 29 7b 0a 20 20 20 20 61 70 70 b)==0 ){. app
1cfef 6c 79 41 66 66 69 6e 69 74 79 28 70 49 6e 31 2c lyAffinity(pIn1,
1cff0 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 SQLITE_AFF_TEXT
1cff1 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 20 , encoding);.
1cff2 20 61 73 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 assert( pIn1->f
1cff3 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 20 7c lags & MEM_Str |
1cff4 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c | db->mallocFail
1cff5 65 64 20 29 3b 0a 20 20 20 20 4d 65 6d 53 65 74 ed );. MemSet
1cff6 54 79 70 65 46 6c 61 67 28 70 49 6e 31 2c 20 4d TypeFlag(pIn1, M
1cff7 45 4d 5f 42 6c 6f 62 29 3b 0a 20 20 7d 65 6c 73 EM_Blob);. }els
1cff8 65 7b 0a 20 20 20 20 70 49 6e 31 2d 3e 66 6c 61 e{. pIn1->fla
1cff9 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 54 79 70 65 gs &= ~(MEM_Type
1cffa 4d 61 73 6b 26 7e 4d 45 4d 5f 42 6c 6f 62 29 3b Mask&~MEM_Blob);
1cffb 0a 20 20 7d 0a 20 20 55 50 44 41 54 45 5f 4d 41 . }. UPDATE_MA
1cffc 58 5f 42 4c 4f 42 53 49 5a 45 28 70 49 6e 31 29 X_BLOBSIZE(pIn1)
1cffd 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a ;. break;.}../*
1cffe 20 4f 70 63 6f 64 65 3a 20 54 6f 4e 75 6d 65 72 Opcode: ToNumer
1cfff 69 63 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a ic P1 * * * *.**
1d000 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 76 61 .** Force the va
1d001 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 lue in register
1d002 50 31 20 74 6f 20 62 65 20 6e 75 6d 65 72 69 63 P1 to be numeric
1d003 20 28 65 69 74 68 65 72 20 61 6e 0a 2a 2a 20 69 (either an.** i
1d004 6e 74 65 67 65 72 20 6f 72 20 61 20 66 6c 6f 61 nteger or a floa
1d005 74 69 6e 67 2d 70 6f 69 6e 74 20 6e 75 6d 62 65 ting-point numbe
1d006 72 2e 29 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 r.).** If the va
1d007 6c 75 65 20 69 73 20 74 65 78 74 20 6f 72 20 62 lue is text or b
1d008 6c 6f 62 2c 20 74 72 79 20 74 6f 20 63 6f 6e 76 lob, try to conv
1d009 65 72 74 20 69 74 20 74 6f 20 61 6e 20 75 73 69 ert it to an usi
1d00a 6e 67 20 74 68 65 0a 2a 2a 20 65 71 75 69 76 61 ng the.** equiva
1d00b 6c 65 6e 74 20 6f 66 20 61 74 6f 69 28 29 20 6f lent of atoi() o
1d00c 72 20 61 74 6f 66 28 29 20 61 6e 64 20 73 74 6f r atof() and sto
1d00d 72 65 20 30 20 69 66 20 6e 6f 20 73 75 63 68 20 re 0 if no such
1d00e 63 6f 6e 76 65 72 73 69 6f 6e 20 0a 2a 2a 20 69 conversion .** i
1d00f 73 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a s possible..**.*
1d010 2a 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 * A NULL value i
1d011 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79 s not changed by
1d012 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 this routine.
1d013 49 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e It remains NULL.
1d014 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 4e 75 .*/.case OP_ToNu
1d015 6d 65 72 69 63 3a 20 7b 20 20 20 20 20 20 20 20 meric: {
1d016 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d /* sam
1d017 65 20 61 73 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52 e as TK_TO_NUMER
1d018 49 43 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 IC, in1 */. if(
1d019 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 (pIn1->flags &
1d01a 28 4d 45 4d 5f 4e 75 6c 6c 7c 4d 45 4d 5f 49 6e (MEM_Null|MEM_In
1d01b 74 7c 4d 45 4d 5f 52 65 61 6c 29 29 3d 3d 30 20 t|MEM_Real))==0
1d01c 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
1d01d 62 65 4d 65 6d 4e 75 6d 65 72 69 66 79 28 70 49 beMemNumerify(pI
1d01e 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b n1);. }. break
1d01f 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
1d020 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 20 2a LITE_OMIT_CAST *
1d021 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f /../* Opcode: To
1d022 49 6e 74 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a Int P1 * * * *.*
1d023 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 76 *.** Force the v
1d024 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 alue in register
1d025 20 50 31 20 62 65 20 61 6e 20 69 6e 74 65 67 65 P1 be an intege
1d026 72 2e 20 20 49 66 0a 2a 2a 20 54 68 65 20 76 61 r. If.** The va
1d027 6c 75 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79 lue is currently
1d028 20 61 20 72 65 61 6c 20 6e 75 6d 62 65 72 2c 20 a real number,
1d029 64 72 6f 70 20 69 74 73 20 66 72 61 63 74 69 6f drop its fractio
1d02a 6e 61 6c 20 70 61 72 74 2e 0a 2a 2a 20 49 66 20 nal part..** If
1d02b 74 68 65 20 76 61 6c 75 65 20 69 73 20 74 65 78 the value is tex
1d02c 74 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 t or blob, try t
1d02d 6f 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 o convert it to
1d02e 61 6e 20 69 6e 74 65 67 65 72 20 75 73 69 6e 67 an integer using
1d02f 20 74 68 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65 the.** equivale
1d030 6e 74 20 6f 66 20 61 74 6f 69 28 29 20 61 6e 64 nt of atoi() and
1d031 20 73 74 6f 72 65 20 30 20 69 66 20 6e 6f 20 73 store 0 if no s
1d032 75 63 68 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 uch conversion i
1d033 73 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a s possible..**.*
1d034 2a 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 * A NULL value i
1d035 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79 s not changed by
1d036 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 this routine.
1d037 49 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e It remains NULL.
1d038 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 49 6e .*/.case OP_ToIn
1d039 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 t: {
1d03a 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 /* same as
1d03b 20 54 4b 5f 54 4f 5f 49 4e 54 2c 20 69 6e 31 20 TK_TO_INT, in1
1d03c 2a 2f 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e */. if( (pIn1->
1d03d 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c flags & MEM_Null
1d03e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 )==0 ){. sqli
1d03f 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 te3VdbeMemIntege
1d040 72 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 7d 0a rify(pIn1);. }.
1d041 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e break;.}..#ifn
1d042 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1d043 43 41 53 54 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 CAST./* Opcode:
1d044 54 6f 52 65 61 6c 20 50 31 20 2a 20 2a 20 2a 20 ToReal P1 * * *
1d045 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68 *.**.** Force th
1d046 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 e value in regis
1d047 74 65 72 20 50 31 20 74 6f 20 62 65 20 61 20 66 ter P1 to be a f
1d048 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75 loating point nu
1d049 6d 62 65 72 2e 0a 2a 2a 20 49 66 20 54 68 65 20 mber..** If The
1d04a 76 61 6c 75 65 20 69 73 20 63 75 72 72 65 6e 74 value is current
1d04b 6c 79 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 63 ly an integer, c
1d04c 6f 6e 76 65 72 74 20 69 74 2e 0a 2a 2a 20 49 66 onvert it..** If
1d04d 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 74 65 the value is te
1d04e 78 74 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 xt or blob, try
1d04f 74 6f 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f to convert it to
1d050 20 61 6e 20 69 6e 74 65 67 65 72 20 75 73 69 6e an integer usin
1d051 67 20 74 68 65 0a 2a 2a 20 65 71 75 69 76 61 6c g the.** equival
1d052 65 6e 74 20 6f 66 20 61 74 6f 69 28 29 20 61 6e ent of atoi() an
1d053 64 20 73 74 6f 72 65 20 30 2e 30 20 69 66 20 6e d store 0.0 if n
1d054 6f 20 73 75 63 68 20 63 6f 6e 76 65 72 73 69 6f o such conversio
1d055 6e 20 69 73 20 70 6f 73 73 69 62 6c 65 2e 0a 2a n is possible..*
1d056 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61 6c 75 *.** A NULL valu
1d057 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 e is not changed
1d058 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 by this routine
1d059 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20 4e 55 . It remains NU
1d05a 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 LL..*/.case OP_T
1d05b 6f 52 65 61 6c 3a 20 7b 20 20 20 20 20 20 20 20 oReal: {
1d05c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d /* sam
1d05d 65 20 61 73 20 54 4b 5f 54 4f 5f 52 45 41 4c 2c e as TK_TO_REAL,
1d05e 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 28 70 in1 */. if( (p
1d05f 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d In1->flags & MEM
1d060 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 _Null)==0 ){.
1d061 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
1d062 65 61 6c 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 ealify(pIn1);.
1d063 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e }. break;.}.#en
1d064 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
1d065 49 54 5f 43 41 53 54 20 2a 2f 0a 0a 2f 2a 20 4f IT_CAST */../* O
1d066 70 63 6f 64 65 3a 20 4c 74 20 50 31 20 50 32 20 pcode: Lt P1 P2
1d067 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 43 P3 P4 P5.**.** C
1d068 6f 6d 70 61 72 65 20 74 68 65 20 76 61 6c 75 65 ompare the value
1d069 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 s in register P1
1d06a 20 61 6e 64 20 50 33 2e 20 20 49 66 20 72 65 67 and P3. If reg
1d06b 28 50 33 29 3c 72 65 67 28 50 31 29 20 74 68 65 (P3)<reg(P1) the
1d06c 6e 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 61 64 64 n.** jump to add
1d06d 72 65 73 73 20 50 32 2e 20 20 0a 2a 2a 0a 2a 2a ress P2. .**.**
1d06e 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f 4a If the SQLITE_J
1d06f 55 4d 50 49 46 4e 55 4c 4c 20 62 69 74 20 6f 66 UMPIFNULL bit of
1d070 20 50 35 20 69 73 20 73 65 74 20 61 6e 64 20 65 P5 is set and e
1d071 69 74 68 65 72 20 72 65 67 28 50 31 29 20 6f 72 ither reg(P1) or
1d072 0a 2a 2a 20 72 65 67 28 50 33 29 20 69 73 20 4e .** reg(P3) is N
1d073 55 4c 4c 20 74 68 65 6e 20 74 61 6b 65 20 74 68 ULL then take th
1d074 65 20 6a 75 6d 70 2e 20 20 49 66 20 74 68 65 20 e jump. If the
1d075 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c SQLITE_JUMPIFNUL
1d076 4c 20 0a 2a 2a 20 62 69 74 20 69 73 20 63 6c 65 L .** bit is cle
1d077 61 72 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 ar then fall thr
1d078 75 20 69 66 20 65 69 74 68 65 72 20 6f 70 65 72 u if either oper
1d079 61 6e 64 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a and is NULL..**.
1d07a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 41 46 ** The SQLITE_AF
1d07b 46 5f 4d 41 53 4b 20 70 6f 72 74 69 6f 6e 20 6f F_MASK portion o
1d07c 66 20 50 35 20 6d 75 73 74 20 62 65 20 61 6e 20 f P5 must be an
1d07d 61 66 66 69 6e 69 74 79 20 63 68 61 72 61 63 74 affinity charact
1d07e 65 72 20 2d 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 er -.** SQLITE_A
1d07f 46 46 5f 54 45 58 54 2c 20 53 51 4c 49 54 45 5f FF_TEXT, SQLITE_
1d080 41 46 46 5f 49 4e 54 45 47 45 52 2c 20 61 6e 64 AFF_INTEGER, and
1d081 20 73 6f 20 66 6f 72 74 68 2e 20 41 6e 20 61 74 so forth. An at
1d082 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 0a 2a tempt is made .*
1d083 2a 20 74 6f 20 63 6f 65 72 63 65 20 62 6f 74 68 * to coerce both
1d084 20 69 6e 70 75 74 73 20 61 63 63 6f 72 64 69 6e inputs accordin
1d085 67 20 74 6f 20 74 68 69 73 20 61 66 66 69 6e 69 g to this affini
1d086 74 79 20 62 65 66 6f 72 65 20 74 68 65 0a 2a 2a ty before the.**
1d087 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 6d comparison is m
1d088 61 64 65 2e 20 49 66 20 74 68 65 20 53 51 4c 49 ade. If the SQLI
1d089 54 45 5f 41 46 46 5f 4d 41 53 4b 20 69 73 20 30 TE_AFF_MASK is 0
1d08a 78 30 30 2c 20 74 68 65 6e 20 6e 75 6d 65 72 69 x00, then numeri
1d08b 63 0a 2a 2a 20 61 66 66 69 6e 69 74 79 20 69 73 c.** affinity is
1d08c 20 75 73 65 64 2e 20 4e 6f 74 65 20 74 68 61 74 used. Note that
1d08d 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 63 6f the affinity co
1d08e 6e 76 65 72 73 69 6f 6e 73 20 61 72 65 20 73 74 nversions are st
1d08f 6f 72 65 64 0a 2a 2a 20 62 61 63 6b 20 69 6e 74 ored.** back int
1d090 6f 20 74 68 65 20 69 6e 70 75 74 20 72 65 67 69 o the input regi
1d091 73 74 65 72 73 20 50 31 20 61 6e 64 20 50 33 2e sters P1 and P3.
1d092 20 20 53 6f 20 74 68 69 73 20 6f 70 63 6f 64 65 So this opcode
1d093 20 63 61 6e 20 63 61 75 73 65 0a 2a 2a 20 70 65 can cause.** pe
1d094 72 73 69 73 74 65 6e 74 20 63 68 61 6e 67 65 73 rsistent changes
1d095 20 74 6f 20 72 65 67 69 73 74 65 72 73 20 50 31 to registers P1
1d096 20 61 6e 64 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 4f and P3..**.** O
1d097 6e 63 65 20 61 6e 79 20 63 6f 6e 76 65 72 73 69 nce any conversi
1d098 6f 6e 73 20 68 61 76 65 20 74 61 6b 65 6e 20 70 ons have taken p
1d099 6c 61 63 65 2c 20 61 6e 64 20 6e 65 69 74 68 65 lace, and neithe
1d09a 72 20 76 61 6c 75 65 20 69 73 20 4e 55 4c 4c 2c r value is NULL,
1d09b 20 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 73 20 .** the values
1d09c 61 72 65 20 63 6f 6d 70 61 72 65 64 2e 20 49 66 are compared. If
1d09d 20 62 6f 74 68 20 76 61 6c 75 65 73 20 61 72 65 both values are
1d09e 20 62 6c 6f 62 73 20 74 68 65 6e 20 6d 65 6d 63 blobs then memc
1d09f 6d 70 28 29 20 69 73 0a 2a 2a 20 75 73 65 64 20 mp() is.** used
1d0a0 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 to determine the
1d0a1 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 results of the
1d0a2 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20 49 66 20 comparison. If
1d0a3 62 6f 74 68 20 76 61 6c 75 65 73 0a 2a 2a 20 61 both values.** a
1d0a4 72 65 20 74 65 78 74 2c 20 74 68 65 6e 20 74 68 re text, then th
1d0a5 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 63 6f e appropriate co
1d0a6 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e llating function
1d0a7 20 73 70 65 63 69 66 69 65 64 20 69 6e 0a 2a 2a specified in.**
1d0a8 20 50 34 20 69 73 20 20 75 73 65 64 20 74 6f 20 P4 is used to
1d0a9 64 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f do the compariso
1d0aa 6e 2e 20 20 49 66 20 50 34 20 69 73 20 6e 6f 74 n. If P4 is not
1d0ab 20 73 70 65 63 69 66 69 65 64 20 74 68 65 6e 0a specified then.
1d0ac 2a 2a 20 6d 65 6d 63 6d 70 28 29 20 69 73 20 75 ** memcmp() is u
1d0ad 73 65 64 20 74 6f 20 63 6f 6d 70 61 72 65 20 74 sed to compare t
1d0ae 65 78 74 20 73 74 72 69 6e 67 2e 20 20 49 66 20 ext string. If
1d0af 62 6f 74 68 20 76 61 6c 75 65 73 20 61 72 65 0a both values are.
1d0b0 2a 2a 20 6e 75 6d 65 72 69 63 2c 20 74 68 65 6e ** numeric, then
1d0b1 20 61 20 6e 75 6d 65 72 69 63 20 63 6f 6d 70 61 a numeric compa
1d0b2 72 69 73 6f 6e 20 69 73 20 75 73 65 64 2e 20 49 rison is used. I
1d0b3 66 20 74 68 65 20 74 77 6f 20 76 61 6c 75 65 73 f the two values
1d0b4 0a 2a 2a 20 61 72 65 20 6f 66 20 64 69 66 66 65 .** are of diffe
1d0b5 72 65 6e 74 20 74 79 70 65 73 2c 20 74 68 65 6e rent types, then
1d0b6 20 6e 75 6d 62 65 72 73 20 61 72 65 20 63 6f 6e numbers are con
1d0b7 73 69 64 65 72 65 64 20 6c 65 73 73 20 74 68 61 sidered less tha
1d0b8 6e 0a 2a 2a 20 73 74 72 69 6e 67 73 20 61 6e 64 n.** strings and
1d0b9 20 73 74 72 69 6e 67 73 20 61 72 65 20 63 6f 6e strings are con
1d0ba 73 69 64 65 72 65 64 20 6c 65 73 73 20 74 68 61 sidered less tha
1d0bb 6e 20 62 6c 6f 62 73 2e 0a 2a 2a 0a 2a 2a 20 49 n blobs..**.** I
1d0bc 66 20 74 68 65 20 53 51 4c 49 54 45 5f 53 54 4f f the SQLITE_STO
1d0bd 52 45 50 32 20 62 69 74 20 6f 66 20 50 35 20 69 REP2 bit of P5 i
1d0be 73 20 73 65 74 2c 20 74 68 65 6e 20 64 6f 20 6e s set, then do n
1d0bf 6f 74 20 6a 75 6d 70 2e 20 20 49 6e 73 74 65 61 ot jump. Instea
1d0c0 64 2c 0a 2a 2a 20 73 74 6f 72 65 20 61 20 62 6f d,.** store a bo
1d0c1 6f 6c 65 61 6e 20 72 65 73 75 6c 74 20 28 65 69 olean result (ei
1d0c2 74 68 65 72 20 30 2c 20 6f 72 20 31 2c 20 6f 72 ther 0, or 1, or
1d0c3 20 4e 55 4c 4c 29 20 69 6e 20 72 65 67 69 73 74 NULL) in regist
1d0c4 65 72 20 50 32 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 er P2..*/./* Opc
1d0c5 6f 64 65 3a 20 4e 65 20 50 31 20 50 32 20 50 33 ode: Ne P1 P2 P3
1d0c6 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 P4 P5.**.** Thi
1d0c7 73 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b s works just lik
1d0c8 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 e the Lt opcode
1d0c9 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65 20 except that the
1d0ca 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66 jump is taken if
1d0cb 0a 2a 2a 20 74 68 65 20 6f 70 65 72 61 6e 64 73 .** the operands
1d0cc 20 69 6e 20 72 65 67 69 73 74 65 72 73 20 50 31 in registers P1
1d0cd 20 61 6e 64 20 50 33 20 61 72 65 20 6e 6f 74 20 and P3 are not
1d0ce 65 71 75 61 6c 2e 20 20 53 65 65 20 74 68 65 20 equal. See the
1d0cf 4c 74 20 6f 70 63 6f 64 65 20 66 6f 72 0a 2a 2a Lt opcode for.**
1d0d0 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f additional info
1d0d1 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 rmation..**.** I
1d0d2 66 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 20 f SQLITE_NULLEQ
1d0d3 69 73 20 73 65 74 20 69 6e 20 50 35 20 74 68 65 is set in P5 the
1d0d4 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 n the result of
1d0d5 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 61 6c comparison is al
1d0d6 77 61 79 73 20 65 69 74 68 65 72 0a 2a 2a 20 74 ways either.** t
1d0d7 72 75 65 20 6f 72 20 66 61 6c 73 65 20 61 6e 64 rue or false and
1d0d8 20 69 73 20 6e 65 76 65 72 20 4e 55 4c 4c 2e 20 is never NULL.
1d0d9 20 49 66 20 62 6f 74 68 20 6f 70 65 72 61 6e 64 If both operand
1d0da 73 20 61 72 65 20 4e 55 4c 4c 20 74 68 65 6e 20 s are NULL then
1d0db 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 6f 66 the result.** of
1d0dc 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 66 comparison is f
1d0dd 61 6c 73 65 2e 20 20 49 66 20 65 69 74 68 65 72 alse. If either
1d0de 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c operand is NULL
1d0df 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 then the result
1d0e0 20 69 73 20 74 72 75 65 2e 0a 2a 2a 20 49 66 20 is true..** If
1d0e1 6e 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 neither operand
1d0e2 69 73 20 4e 55 4c 4c 20 74 68 65 20 74 68 65 20 is NULL the the
1d0e3 72 65 73 75 6c 74 20 69 73 20 74 68 65 20 73 61 result is the sa
1d0e4 6d 65 20 61 73 20 69 74 20 77 6f 75 6c 64 20 62 me as it would b
1d0e5 65 20 69 66 0a 2a 2a 20 74 68 65 20 53 51 4c 49 e if.** the SQLI
1d0e6 54 45 5f 4e 55 4c 4c 45 51 20 66 6c 61 67 20 77 TE_NULLEQ flag w
1d0e7 65 72 65 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d ere omitted from
1d0e8 20 50 35 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 P5..*/./* Opcod
1d0e9 65 3a 20 45 71 20 50 31 20 50 32 20 50 33 20 50 e: Eq P1 P2 P3 P
1d0ea 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 4 P5.**.** This
1d0eb 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 works just like
1d0ec 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 the Lt opcode ex
1d0ed 63 65 70 74 20 74 68 61 74 20 74 68 65 20 6a 75 cept that the ju
1d0ee 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a mp is taken if.*
1d0ef 2a 20 74 68 65 20 6f 70 65 72 61 6e 64 73 20 69 * the operands i
1d0f0 6e 20 72 65 67 69 73 74 65 72 73 20 50 31 20 61 n registers P1 a
1d0f1 6e 64 20 50 33 20 61 72 65 20 65 71 75 61 6c 2e nd P3 are equal.
1d0f2 0a 2a 2a 20 53 65 65 20 74 68 65 20 4c 74 20 6f .** See the Lt o
1d0f3 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74 69 pcode for additi
1d0f4 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e onal information
1d0f5 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 ..**.** If SQLIT
1d0f6 45 5f 4e 55 4c 4c 45 51 20 69 73 20 73 65 74 20 E_NULLEQ is set
1d0f7 69 6e 20 50 35 20 74 68 65 6e 20 74 68 65 20 72 in P5 then the r
1d0f8 65 73 75 6c 74 20 6f 66 20 63 6f 6d 70 61 72 69 esult of compari
1d0f9 73 6f 6e 20 69 73 20 61 6c 77 61 79 73 20 65 69 son is always ei
1d0fa 74 68 65 72 0a 2a 2a 20 74 72 75 65 20 6f 72 20 ther.** true or
1d0fb 66 61 6c 73 65 20 61 6e 64 20 69 73 20 6e 65 76 false and is nev
1d0fc 65 72 20 4e 55 4c 4c 2e 20 20 49 66 20 62 6f 74 er NULL. If bot
1d0fd 68 20 6f 70 65 72 61 6e 64 73 20 61 72 65 20 4e h operands are N
1d0fe 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 72 65 73 ULL then the res
1d0ff 75 6c 74 0a 2a 2a 20 6f 66 20 63 6f 6d 70 61 72 ult.** of compar
1d100 69 73 6f 6e 20 69 73 20 74 72 75 65 2e 20 20 49 ison is true. I
1d101 66 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64 f either operand
1d102 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 is NULL then th
1d103 65 20 72 65 73 75 6c 74 20 69 73 20 66 61 6c 73 e result is fals
1d104 65 2e 0a 2a 2a 20 49 66 20 6e 65 69 74 68 65 72 e..** If neither
1d105 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c operand is NULL
1d106 20 74 68 65 20 74 68 65 20 72 65 73 75 6c 74 20 the the result
1d107 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 69 is the same as i
1d108 74 20 77 6f 75 6c 64 20 62 65 20 69 66 0a 2a 2a t would be if.**
1d109 20 74 68 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c the SQLITE_NULL
1d10a 45 51 20 66 6c 61 67 20 77 65 72 65 20 6f 6d 69 EQ flag were omi
1d10b 74 74 65 64 20 66 72 6f 6d 20 50 35 2e 0a 2a 2f tted from P5..*/
1d10c 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 65 20 50 ./* Opcode: Le P
1d10d 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 1 P2 P3 P4 P5.**
1d10e 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 6a .** This works j
1d10f 75 73 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 20 ust like the Lt
1d110 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20 74 68 opcode except th
1d111 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 at the jump is t
1d112 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 63 aken if.** the c
1d113 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74 ontent of regist
1d114 65 72 20 50 33 20 69 73 20 6c 65 73 73 20 74 68 er P3 is less th
1d115 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 an or equal to t
1d116 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a he content of.**
1d117 20 72 65 67 69 73 74 65 72 20 50 31 2e 20 20 53 register P1. S
1d118 65 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 ee the Lt opcode
1d119 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 for additional
1d11a 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a information..*/.
1d11b 2f 2a 20 4f 70 63 6f 64 65 3a 20 47 74 20 50 31 /* Opcode: Gt P1
1d11c 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a P2 P3 P4 P5.**.
1d11d 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 ** This works ju
1d11e 73 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f st like the Lt o
1d11f 70 63 6f 64 65 20 65 78 63 65 70 74 20 74 68 61 pcode except tha
1d120 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61 t the jump is ta
1d121 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 63 6f ken if.** the co
1d122 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65 ntent of registe
1d123 72 20 50 33 20 69 73 20 67 72 65 61 74 65 72 20 r P3 is greater
1d124 74 68 61 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 than the content
1d125 20 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 of.** register
1d126 50 31 2e 20 20 53 65 65 20 74 68 65 20 4c 74 20 P1. See the Lt
1d127 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74 opcode for addit
1d128 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f ional informatio
1d129 6e 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a n..*/./* Opcode:
1d12a 20 47 65 20 50 31 20 50 32 20 50 33 20 50 34 20 Ge P1 P2 P3 P4
1d12b 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f P5.**.** This wo
1d12c 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 rks just like th
1d12d 65 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65 e Lt opcode exce
1d12e 70 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70 pt that the jump
1d12f 20 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 is taken if.**
1d130 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 the content of r
1d131 65 67 69 73 74 65 72 20 50 33 20 69 73 20 67 72 egister P3 is gr
1d132 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 eater than or eq
1d133 75 61 6c 20 74 6f 20 74 68 65 20 63 6f 6e 74 65 ual to the conte
1d134 6e 74 20 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65 nt of.** registe
1d135 72 20 50 31 2e 20 20 53 65 65 20 74 68 65 20 4c r P1. See the L
1d136 74 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64 t opcode for add
1d137 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 itional informat
1d138 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ion..*/.case OP_
1d139 45 71 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 Eq:
1d13a 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
1d13b 45 51 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 EQ, jump, in1, i
1d13c 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 n3 */.case OP_Ne
1d13d 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 :
1d13e 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 45 /* same as TK_NE
1d13f 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 , jump, in1, in3
1d140 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 74 3a 20 */.case OP_Lt:
1d141 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1d142 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 54 2c 20 same as TK_LT,
1d143 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a jump, in1, in3 *
1d144 2f 0a 63 61 73 65 20 4f 50 5f 4c 65 3a 20 20 20 /.case OP_Le:
1d145 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 /* s
1d146 61 6d 65 20 61 73 20 54 4b 5f 4c 45 2c 20 6a 75 ame as TK_LE, ju
1d147 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a mp, in1, in3 */.
1d148 63 61 73 65 20 4f 50 5f 47 74 3a 20 20 20 20 20 case OP_Gt:
1d149 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d /* sam
1d14a 65 20 61 73 20 54 4b 5f 47 54 2c 20 6a 75 6d 70 e as TK_GT, jump
1d14b 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 61 , in1, in3 */.ca
1d14c 73 65 20 4f 50 5f 47 65 3a 20 7b 20 20 20 20 20 se OP_Ge: {
1d14d 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 /* same
1d14e 61 73 20 54 4b 5f 47 45 2c 20 6a 75 6d 70 2c 20 as TK_GE, jump,
1d14f 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 20 in1, in3 */.#if
1d150 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1d151 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1d152 20 75 2e 61 69 20 2a 2f 0a 20 20 69 6e 74 20 72 u.ai */. int r
1d153 65 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f es; /
1d154 2a 20 52 65 73 75 6c 74 20 6f 66 20 74 68 65 20 * Result of the
1d155 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20 70 49 comparison of pI
1d156 6e 31 20 61 67 61 69 6e 73 74 20 70 49 6e 33 20 n1 against pIn3
1d157 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 69 6e 69 */. char affini
1d158 74 79 3b 20 20 20 20 20 20 2f 2a 20 41 66 66 69 ty; /* Affi
1d159 6e 69 74 79 20 74 6f 20 75 73 65 20 66 6f 72 20 nity to use for
1d15a 63 6f 6d 70 61 72 69 73 6f 6e 20 2a 2f 0a 23 65 comparison */.#e
1d15b 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1d15c 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1d15d 74 6f 20 75 2e 61 69 20 2a 2f 0a 0a 20 20 69 66 to u.ai */.. if
1d15e 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c ( (pIn1->flags |
1d15f 20 70 49 6e 33 2d 3e 66 6c 61 67 73 29 26 4d 45 pIn3->flags)&ME
1d160 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 2f 2a M_Null ){. /*
1d161 20 4f 6e 65 20 6f 72 20 62 6f 74 68 20 6f 70 65 One or both ope
1d162 72 61 6e 64 73 20 61 72 65 20 4e 55 4c 4c 20 2a rands are NULL *
1d163 2f 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 /. if( pOp->p
1d164 35 20 26 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 5 & SQLITE_NULLE
1d165 51 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 Q ){. /* If
1d166 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 20 69 SQLITE_NULLEQ i
1d167 73 20 73 65 74 20 28 77 68 69 63 68 20 77 69 6c s set (which wil
1d168 6c 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 69 66 l only happen if
1d169 20 74 68 65 20 6f 70 65 72 61 74 6f 72 20 69 73 the operator is
1d16a 0a 20 20 20 20 20 20 2a 2a 20 4f 50 5f 45 71 20 . ** OP_Eq
1d16b 6f 72 20 4f 50 5f 4e 65 29 20 74 68 65 6e 20 74 or OP_Ne) then t
1d16c 61 6b 65 20 74 68 65 20 6a 75 6d 70 20 6f 72 20 ake the jump or
1d16d 6e 6f 74 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e not depending on
1d16e 20 77 68 65 74 68 65 72 0a 20 20 20 20 20 20 2a whether. *
1d16f 2a 20 6f 72 20 6e 6f 74 20 62 6f 74 68 20 6f 70 * or not both op
1d170 65 72 61 6e 64 73 20 61 72 65 20 6e 75 6c 6c 2e erands are null.
1d171 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
1d172 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 6f 70 63 assert( pOp->opc
1d173 6f 64 65 3d 3d 4f 50 5f 45 71 20 7c 7c 20 70 4f ode==OP_Eq || pO
1d174 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4e 65 p->opcode==OP_Ne
1d175 20 29 3b 0a 20 20 20 20 20 20 75 2e 61 69 2e 72 );. u.ai.r
1d176 65 73 20 3d 20 28 70 49 6e 31 2d 3e 66 6c 61 67 es = (pIn1->flag
1d177 73 20 26 20 70 49 6e 33 2d 3e 66 6c 61 67 73 20 s & pIn3->flags
1d178 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 3b 0a & MEM_Null)==0;.
1d179 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1d17a 20 2f 2a 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 /* SQLITE_NULLE
1d17b 51 20 69 73 20 63 6c 65 61 72 20 61 6e 64 20 61 Q is clear and a
1d17c 74 20 6c 65 61 73 74 20 6f 6e 65 20 6f 70 65 72 t least one oper
1d17d 61 6e 64 20 69 73 20 4e 55 4c 4c 2c 0a 20 20 20 and is NULL,.
1d17e 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 72 ** then the r
1d17f 65 73 75 6c 74 20 69 73 20 61 6c 77 61 79 73 20 esult is always
1d180 4e 55 4c 4c 2e 0a 20 20 20 20 20 20 2a 2a 20 54 NULL.. ** T
1d181 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e he jump is taken
1d182 20 69 66 20 74 68 65 20 53 51 4c 49 54 45 5f 4a if the SQLITE_J
1d183 55 4d 50 49 46 4e 55 4c 4c 20 62 69 74 20 69 73 UMPIFNULL bit is
1d184 20 73 65 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 set.. */.
1d185 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 if( pOp->p5
1d186 20 26 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 & SQLITE_STOREP
1d187 32 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4f 75 2 ){. pOu
1d188 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 t = &p->aMem[pOp
1d189 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 20 20 20 4d ->p2];. M
1d18a 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f emSetTypeFlag(pO
1d18b 75 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 20 ut, MEM_Null);.
1d18c 20 20 20 20 20 20 20 52 45 47 49 53 54 45 52 5f REGISTER_
1d18d 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 TRACE(pOp->p2, p
1d18e 4f 75 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 Out);. }els
1d18f 65 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 e if( pOp->p5 &
1d190 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c SQLITE_JUMPIFNUL
1d191 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 70 63 20 L ){. pc
1d192 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 20 = pOp->p2-1;.
1d193 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b }. break
1d194 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b ;. }. }else{
1d195 0a 20 20 20 20 2f 2a 20 4e 65 69 74 68 65 72 20 . /* Neither
1d196 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 2e operand is NULL.
1d197 20 20 44 6f 20 61 20 63 6f 6d 70 61 72 69 73 6f Do a compariso
1d198 6e 2e 20 2a 2f 0a 20 20 20 20 75 2e 61 69 2e 61 n. */. u.ai.a
1d199 66 66 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e 70 ffinity = pOp->p
1d19a 35 20 26 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 5 & SQLITE_AFF_M
1d19b 41 53 4b 3b 0a 20 20 20 20 69 66 28 20 75 2e 61 ASK;. if( u.a
1d19c 69 2e 61 66 66 69 6e 69 74 79 20 29 7b 0a 20 20 i.affinity ){.
1d19d 20 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 applyAffinit
1d19e 79 28 70 49 6e 31 2c 20 75 2e 61 69 2e 61 66 66 y(pIn1, u.ai.aff
1d19f 69 6e 69 74 79 2c 20 65 6e 63 6f 64 69 6e 67 29 inity, encoding)
1d1a0 3b 0a 20 20 20 20 20 20 61 70 70 6c 79 41 66 66 ;. applyAff
1d1a1 69 6e 69 74 79 28 70 49 6e 33 2c 20 75 2e 61 69 inity(pIn3, u.ai
1d1a2 2e 61 66 66 69 6e 69 74 79 2c 20 65 6e 63 6f 64 .affinity, encod
1d1a3 69 6e 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ing);. if(
1d1a4 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1d1a5 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a ) goto no_mem;.
1d1a6 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73 65 72 }.. asser
1d1a7 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d t( pOp->p4type==
1d1a8 50 34 5f 43 4f 4c 4c 53 45 51 20 7c 7c 20 70 4f P4_COLLSEQ || pO
1d1a9 70 2d 3e 70 34 2e 70 43 6f 6c 6c 3d 3d 30 20 29 p->p4.pColl==0 )
1d1aa 3b 0a 20 20 20 20 45 78 70 61 6e 64 42 6c 6f 62 ;. ExpandBlob
1d1ab 28 70 49 6e 31 29 3b 0a 20 20 20 20 45 78 70 61 (pIn1);. Expa
1d1ac 6e 64 42 6c 6f 62 28 70 49 6e 33 29 3b 0a 20 20 ndBlob(pIn3);.
1d1ad 20 20 75 2e 61 69 2e 72 65 73 20 3d 20 73 71 6c u.ai.res = sql
1d1ae 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 70 ite3MemCompare(p
1d1af 49 6e 33 2c 20 70 49 6e 31 2c 20 70 4f 70 2d 3e In3, pIn1, pOp->
1d1b0 70 34 2e 70 43 6f 6c 6c 29 3b 0a 20 20 7d 0a 20 p4.pColl);. }.
1d1b1 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f 70 switch( pOp->op
1d1b2 63 6f 64 65 20 29 7b 0a 20 20 20 20 63 61 73 65 code ){. case
1d1b3 20 4f 50 5f 45 71 3a 20 20 20 20 75 2e 61 69 2e OP_Eq: u.ai.
1d1b4 72 65 73 20 3d 20 75 2e 61 69 2e 72 65 73 3d 3d res = u.ai.res==
1d1b5 30 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 0; break;.
1d1b6 20 20 63 61 73 65 20 4f 50 5f 4e 65 3a 20 20 20 case OP_Ne:
1d1b7 20 75 2e 61 69 2e 72 65 73 20 3d 20 75 2e 61 69 u.ai.res = u.ai
1d1b8 2e 72 65 73 21 3d 30 3b 20 20 20 20 20 62 72 65 .res!=0; bre
1d1b9 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f ak;. case OP_
1d1ba 4c 74 3a 20 20 20 20 75 2e 61 69 2e 72 65 73 20 Lt: u.ai.res
1d1bb 3d 20 75 2e 61 69 2e 72 65 73 3c 30 3b 20 20 20 = u.ai.res<0;
1d1bc 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 break;. ca
1d1bd 73 65 20 4f 50 5f 4c 65 3a 20 20 20 20 75 2e 61 se OP_Le: u.a
1d1be 69 2e 72 65 73 20 3d 20 75 2e 61 69 2e 72 65 73 i.res = u.ai.res
1d1bf 3c 3d 30 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a <=0; break;.
1d1c0 20 20 20 20 63 61 73 65 20 4f 50 5f 47 74 3a 20 case OP_Gt:
1d1c1 20 20 20 75 2e 61 69 2e 72 65 73 20 3d 20 75 2e u.ai.res = u.
1d1c2 61 69 2e 72 65 73 3e 30 3b 20 20 20 20 20 20 62 ai.res>0; b
1d1c3 72 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c reak;. defaul
1d1c4 74 3a 20 20 20 20 20 20 20 75 2e 61 69 2e 72 65 t: u.ai.re
1d1c5 73 20 3d 20 75 2e 61 69 2e 72 65 73 3e 3d 30 3b s = u.ai.res>=0;
1d1c6 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a break;. }.
1d1c7 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 . if( pOp->p5 &
1d1c8 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 20 SQLITE_STOREP2
1d1c9 29 7b 0a 20 20 20 20 70 4f 75 74 20 3d 20 26 70 ){. pOut = &p
1d1ca 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b ->aMem[pOp->p2];
1d1cb 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 . MemSetTypeF
1d1cc 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e lag(pOut, MEM_In
1d1cd 74 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e t);. pOut->u.
1d1ce 69 20 3d 20 75 2e 61 69 2e 72 65 73 3b 0a 20 20 i = u.ai.res;.
1d1cf 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 REGISTER_TRACE
1d1d0 28 70 4f 70 2d 3e 70 32 2c 20 70 4f 75 74 29 3b (pOp->p2, pOut);
1d1d1 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 61 . }else if( u.a
1d1d2 69 2e 72 65 73 20 29 7b 0a 20 20 20 20 70 63 20 i.res ){. pc
1d1d3 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 7d = pOp->p2-1;. }
1d1d4 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1d1d5 4f 70 63 6f 64 65 3a 20 50 65 72 6d 75 74 61 74 Opcode: Permutat
1d1d6 69 6f 6e 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a ion * * * P4 *.*
1d1d7 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 70 65 72 *.** Set the per
1d1d8 6d 75 74 61 74 69 6f 6e 20 75 73 65 64 20 62 79 mutation used by
1d1d9 20 74 68 65 20 4f 50 5f 43 6f 6d 70 61 72 65 20 the OP_Compare
1d1da 6f 70 65 72 61 74 6f 72 20 74 6f 20 62 65 20 74 operator to be t
1d1db 68 65 20 61 72 72 61 79 0a 2a 2a 20 6f 66 20 69 he array.** of i
1d1dc 6e 74 65 67 65 72 73 20 69 6e 20 50 34 2e 0a 2a ntegers in P4..*
1d1dd 2a 0a 2a 2a 20 54 68 65 20 70 65 72 6d 75 74 61 *.** The permuta
1d1de 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 76 61 6c tion is only val
1d1df 69 64 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 78 id until the nex
1d1e0 74 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e t OP_Permutation
1d1e1 2c 20 4f 50 5f 43 6f 6d 70 61 72 65 2c 0a 2a 2a , OP_Compare,.**
1d1e2 20 4f 50 5f 48 61 6c 74 2c 20 6f 72 20 4f 50 5f OP_Halt, or OP_
1d1e3 52 65 73 75 6c 74 52 6f 77 2e 20 20 54 79 70 69 ResultRow. Typi
1d1e4 63 61 6c 6c 79 20 74 68 65 20 4f 50 5f 50 65 72 cally the OP_Per
1d1e5 6d 75 74 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 mutation should
1d1e6 6f 63 63 75 72 0a 2a 2a 20 69 6d 6d 65 64 69 61 occur.** immedia
1d1e7 74 65 6c 79 20 70 72 69 6f 72 20 74 6f 20 74 68 tely prior to th
1d1e8 65 20 4f 50 5f 43 6f 6d 70 61 72 65 2e 0a 2a 2f e OP_Compare..*/
1d1e9 0a 63 61 73 65 20 4f 50 5f 50 65 72 6d 75 74 61 .case OP_Permuta
1d1ea 74 69 6f 6e 3a 20 7b 0a 20 20 61 73 73 65 72 74 tion: {. assert
1d1eb 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 ( pOp->p4type==P
1d1ec 34 5f 49 4e 54 41 52 52 41 59 20 29 3b 0a 20 20 4_INTARRAY );.
1d1ed 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e assert( pOp->p4.
1d1ee 61 69 20 29 3b 0a 20 20 61 50 65 72 6d 75 74 65 ai );. aPermute
1d1ef 20 3d 20 70 4f 70 2d 3e 70 34 2e 61 69 3b 0a 20 = pOp->p4.ai;.
1d1f0 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1d1f1 63 6f 64 65 3a 20 43 6f 6d 70 61 72 65 20 50 31 code: Compare P1
1d1f2 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a P2 P3 P4 *.**.*
1d1f3 2a 20 43 6f 6d 70 61 72 65 20 74 6f 20 76 65 63 * Compare to vec
1d1f4 74 6f 72 73 20 6f 66 20 72 65 67 69 73 74 65 72 tors of register
1d1f5 73 20 69 6e 20 72 65 67 28 50 31 29 2e 2e 72 65 s in reg(P1)..re
1d1f6 67 28 50 31 2b 50 33 2d 31 29 20 28 61 6c 6c 20 g(P1+P3-1) (all
1d1f7 74 68 69 73 0a 2a 2a 20 6f 6e 65 20 22 41 22 29 this.** one "A")
1d1f8 20 61 6e 64 20 69 6e 20 72 65 67 28 50 32 29 2e and in reg(P2).
1d1f9 2e 72 65 67 28 50 32 2b 50 33 2d 31 29 20 28 22 .reg(P2+P3-1) ("
1d1fa 42 22 29 2e 20 20 53 61 76 65 20 74 68 65 20 72 B"). Save the r
1d1fb 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20 esult of.** the
1d1fc 63 6f 6d 70 61 72 69 73 6f 6e 20 66 6f 72 20 75 comparison for u
1d1fd 73 65 20 62 79 20 74 68 65 20 6e 65 78 74 20 4f se by the next O
1d1fe 50 5f 4a 75 6d 70 20 69 6e 73 74 72 75 63 74 2e P_Jump instruct.
1d1ff 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 4b .**.** P4 is a K
1d200 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 eyInfo structure
1d201 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 63 6f that defines co
1d202 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
1d203 73 20 61 6e 64 20 73 6f 72 74 0a 2a 2a 20 6f 72 s and sort.** or
1d204 64 65 72 73 20 66 6f 72 20 74 68 65 20 63 6f 6d ders for the com
1d205 70 61 72 69 73 6f 6e 2e 20 20 54 68 65 20 70 65 parison. The pe
1d206 72 6d 75 74 61 74 69 6f 6e 20 61 70 70 6c 69 65 rmutation applie
1d207 73 20 74 6f 20 72 65 67 69 73 74 65 72 73 0a 2a s to registers.*
1d208 2a 20 6f 6e 6c 79 2e 20 20 54 68 65 20 4b 65 79 * only. The Key
1d209 49 6e 66 6f 20 65 6c 65 6d 65 6e 74 73 20 61 72 Info elements ar
1d20a 65 20 75 73 65 64 20 73 65 71 75 65 6e 74 69 61 e used sequentia
1d20b 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 lly..**.** The c
1d20c 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 61 20 73 omparison is a s
1d20d 6f 72 74 20 63 6f 6d 70 61 72 69 73 6f 6e 2c 20 ort comparison,
1d20e 73 6f 20 4e 55 4c 4c 73 20 63 6f 6d 70 61 72 65 so NULLs compare
1d20f 20 65 71 75 61 6c 2c 0a 2a 2a 20 4e 55 4c 4c 73 equal,.** NULLs
1d210 20 61 72 65 20 6c 65 73 73 20 74 68 61 6e 20 6e are less than n
1d211 75 6d 62 65 72 73 2c 20 6e 75 6d 62 65 72 73 20 umbers, numbers
1d212 61 72 65 20 6c 65 73 73 20 74 68 61 6e 20 73 74 are less than st
1d213 72 69 6e 67 73 2c 0a 2a 2a 20 61 6e 64 20 73 74 rings,.** and st
1d214 72 69 6e 67 73 20 61 72 65 20 6c 65 73 73 20 74 rings are less t
1d215 68 61 6e 20 62 6c 6f 62 73 2e 0a 2a 2f 0a 63 61 han blobs..*/.ca
1d216 73 65 20 4f 50 5f 43 6f 6d 70 61 72 65 3a 20 7b se OP_Compare: {
1d217 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1d218 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1d219 20 69 6e 74 6f 20 75 2e 61 6a 20 2a 2f 0a 20 20 into u.aj */.
1d21a 69 6e 74 20 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a int n;. int i;.
1d21b 20 20 69 6e 74 20 70 31 3b 0a 20 20 69 6e 74 20 int p1;. int
1d21c 70 32 3b 0a 20 20 63 6f 6e 73 74 20 4b 65 79 49 p2;. const KeyI
1d21d 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 nfo *pKeyInfo;.
1d21e 20 69 6e 74 20 69 64 78 3b 0a 20 20 43 6f 6c 6c int idx;. Coll
1d21f 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 2f Seq *pColl; /
1d220 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 * Collating sequ
1d221 65 6e 63 65 20 74 6f 20 75 73 65 20 6f 6e 20 74 ence to use on t
1d222 68 69 73 20 74 65 72 6d 20 2a 2f 0a 20 20 69 6e his term */. in
1d223 74 20 62 52 65 76 3b 20 20 20 20 20 20 20 20 20 t bRev;
1d224 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 44 45 53 /* True for DES
1d225 43 45 4e 44 49 4e 47 20 73 6f 72 74 20 6f 72 64 CENDING sort ord
1d226 65 72 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 er */.#endif /*
1d227 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1d228 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6a 20 moved into u.aj
1d229 2a 2f 0a 0a 20 20 75 2e 61 6a 2e 6e 20 3d 20 70 */.. u.aj.n = p
1d22a 4f 70 2d 3e 70 33 3b 0a 20 20 75 2e 61 6a 2e 70 Op->p3;. u.aj.p
1d22b 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 KeyInfo = pOp->p
1d22c 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 61 73 4.pKeyInfo;. as
1d22d 73 65 72 74 28 20 75 2e 61 6a 2e 6e 3e 30 20 29 sert( u.aj.n>0 )
1d22e 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 6a ;. assert( u.aj
1d22f 2e 70 4b 65 79 49 6e 66 6f 21 3d 30 20 29 3b 0a .pKeyInfo!=0 );.
1d230 20 20 75 2e 61 6a 2e 70 31 20 3d 20 70 4f 70 2d u.aj.p1 = pOp-
1d231 3e 70 31 3b 0a 20 20 75 2e 61 6a 2e 70 32 20 3d >p1;. u.aj.p2 =
1d232 20 70 4f 70 2d 3e 70 32 3b 0a 23 69 66 20 53 51 pOp->p2;.#if SQ
1d233 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 69 66 28 LITE_DEBUG. if(
1d234 20 61 50 65 72 6d 75 74 65 20 29 7b 0a 20 20 20 aPermute ){.
1d235 20 69 6e 74 20 6b 2c 20 6d 78 20 3d 20 30 3b 0a int k, mx = 0;.
1d236 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 75 for(k=0; k<u
1d237 2e 61 6a 2e 6e 3b 20 6b 2b 2b 29 20 69 66 28 20 .aj.n; k++) if(
1d238 61 50 65 72 6d 75 74 65 5b 6b 5d 3e 6d 78 20 29 aPermute[k]>mx )
1d239 20 6d 78 20 3d 20 61 50 65 72 6d 75 74 65 5b 6b mx = aPermute[k
1d23a 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 ];. assert( u
1d23b 2e 61 6a 2e 70 31 3e 30 20 26 26 20 75 2e 61 6a .aj.p1>0 && u.aj
1d23c 2e 70 31 2b 6d 78 3c 3d 70 2d 3e 6e 4d 65 6d 2b .p1+mx<=p->nMem+
1d23d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 1 );. assert(
1d23e 20 75 2e 61 6a 2e 70 32 3e 30 20 26 26 20 75 2e u.aj.p2>0 && u.
1d23f 61 6a 2e 70 32 2b 6d 78 3c 3d 70 2d 3e 6e 4d 65 aj.p2+mx<=p->nMe
1d240 6d 2b 31 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a m+1 );. }else{.
1d241 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6a assert( u.aj
1d242 2e 70 31 3e 30 20 26 26 20 75 2e 61 6a 2e 70 31 .p1>0 && u.aj.p1
1d243 2b 75 2e 61 6a 2e 6e 3c 3d 70 2d 3e 6e 4d 65 6d +u.aj.n<=p->nMem
1d244 2b 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 +1 );. assert
1d245 28 20 75 2e 61 6a 2e 70 32 3e 30 20 26 26 20 75 ( u.aj.p2>0 && u
1d246 2e 61 6a 2e 70 32 2b 75 2e 61 6a 2e 6e 3c 3d 70 .aj.p2+u.aj.n<=p
1d247 2d 3e 6e 4d 65 6d 2b 31 20 29 3b 0a 20 20 7d 0a ->nMem+1 );. }.
1d248 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
1d249 5f 44 45 42 55 47 20 2a 2f 0a 20 20 66 6f 72 28 _DEBUG */. for(
1d24a 75 2e 61 6a 2e 69 3d 30 3b 20 75 2e 61 6a 2e 69 u.aj.i=0; u.aj.i
1d24b 3c 75 2e 61 6a 2e 6e 3b 20 75 2e 61 6a 2e 69 2b <u.aj.n; u.aj.i+
1d24c 2b 29 7b 0a 20 20 20 20 75 2e 61 6a 2e 69 64 78 +){. u.aj.idx
1d24d 20 3d 20 61 50 65 72 6d 75 74 65 20 3f 20 61 50 = aPermute ? aP
1d24e 65 72 6d 75 74 65 5b 75 2e 61 6a 2e 69 5d 20 3a ermute[u.aj.i] :
1d24f 20 75 2e 61 6a 2e 69 3b 0a 20 20 20 20 52 45 47 u.aj.i;. REG
1d250 49 53 54 45 52 5f 54 52 41 43 45 28 75 2e 61 6a ISTER_TRACE(u.aj
1d251 2e 70 31 2b 75 2e 61 6a 2e 69 64 78 2c 20 26 70 .p1+u.aj.idx, &p
1d252 2d 3e 61 4d 65 6d 5b 75 2e 61 6a 2e 70 31 2b 75 ->aMem[u.aj.p1+u
1d253 2e 61 6a 2e 69 64 78 5d 29 3b 0a 20 20 20 20 52 .aj.idx]);. R
1d254 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 75 2e EGISTER_TRACE(u.
1d255 61 6a 2e 70 32 2b 75 2e 61 6a 2e 69 64 78 2c 20 aj.p2+u.aj.idx,
1d256 26 70 2d 3e 61 4d 65 6d 5b 75 2e 61 6a 2e 70 32 &p->aMem[u.aj.p2
1d257 2b 75 2e 61 6a 2e 69 64 78 5d 29 3b 0a 20 20 20 +u.aj.idx]);.
1d258 20 61 73 73 65 72 74 28 20 75 2e 61 6a 2e 69 3c assert( u.aj.i<
1d259 75 2e 61 6a 2e 70 4b 65 79 49 6e 66 6f 2d 3e 6e u.aj.pKeyInfo->n
1d25a 46 69 65 6c 64 20 29 3b 0a 20 20 20 20 75 2e 61 Field );. u.a
1d25b 6a 2e 70 43 6f 6c 6c 20 3d 20 75 2e 61 6a 2e 70 j.pColl = u.aj.p
1d25c 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 75 KeyInfo->aColl[u
1d25d 2e 61 6a 2e 69 5d 3b 0a 20 20 20 20 75 2e 61 6a .aj.i];. u.aj
1d25e 2e 62 52 65 76 20 3d 20 75 2e 61 6a 2e 70 4b 65 .bRev = u.aj.pKe
1d25f 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 yInfo->aSortOrde
1d260 72 5b 75 2e 61 6a 2e 69 5d 3b 0a 20 20 20 20 69 r[u.aj.i];. i
1d261 43 6f 6d 70 61 72 65 20 3d 20 73 71 6c 69 74 65 Compare = sqlite
1d262 33 4d 65 6d 43 6f 6d 70 61 72 65 28 26 70 2d 3e 3MemCompare(&p->
1d263 61 4d 65 6d 5b 75 2e 61 6a 2e 70 31 2b 75 2e 61 aMem[u.aj.p1+u.a
1d264 6a 2e 69 64 78 5d 2c 20 26 70 2d 3e 61 4d 65 6d j.idx], &p->aMem
1d265 5b 75 2e 61 6a 2e 70 32 2b 75 2e 61 6a 2e 69 64 [u.aj.p2+u.aj.id
1d266 78 5d 2c 20 75 2e 61 6a 2e 70 43 6f 6c 6c 29 3b x], u.aj.pColl);
1d267 0a 20 20 20 20 69 66 28 20 69 43 6f 6d 70 61 72 . if( iCompar
1d268 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 75 e ){. if( u
1d269 2e 61 6a 2e 62 52 65 76 20 29 20 69 43 6f 6d 70 .aj.bRev ) iComp
1d26a 61 72 65 20 3d 20 2d 69 43 6f 6d 70 61 72 65 3b are = -iCompare;
1d26b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1d26c 20 20 7d 0a 20 20 7d 0a 20 20 61 50 65 72 6d 75 }. }. aPermu
1d26d 74 65 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b te = 0;. break;
1d26e 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4a .}../* Opcode: J
1d26f 75 6d 70 20 50 31 20 50 32 20 50 33 20 2a 20 2a ump P1 P2 P3 * *
1d270 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 74 .**.** Jump to t
1d271 68 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 he instruction a
1d272 74 20 61 64 64 72 65 73 73 20 50 31 2c 20 50 32 t address P1, P2
1d273 2c 20 6f 72 20 50 33 20 64 65 70 65 6e 64 69 6e , or P3 dependin
1d274 67 20 6f 6e 20 77 68 65 74 68 65 72 0a 2a 2a 20 g on whether.**
1d275 69 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 in the most rece
1d276 6e 74 20 4f 50 5f 43 6f 6d 70 61 72 65 20 69 6e nt OP_Compare in
1d277 73 74 72 75 63 74 69 6f 6e 20 74 68 65 20 50 31 struction the P1
1d278 20 76 65 63 74 6f 72 20 77 61 73 20 6c 65 73 73 vector was less
1d279 20 74 68 61 6e 0a 2a 2a 20 65 71 75 61 6c 20 74 than.** equal t
1d27a 6f 2c 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 o, or greater th
1d27b 61 6e 20 74 68 65 20 50 32 20 76 65 63 74 6f 72 an the P2 vector
1d27c 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a , respectively..
1d27d 2a 2f 0a 63 61 73 65 20 4f 50 5f 4a 75 6d 70 3a */.case OP_Jump:
1d27e 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f { /
1d27f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 69 66 28 20 * jump */. if(
1d280 69 43 6f 6d 70 61 72 65 3c 30 20 29 7b 0a 20 20 iCompare<0 ){.
1d281 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 31 20 2d pc = pOp->p1 -
1d282 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 1;. }else if(
1d283 69 43 6f 6d 70 61 72 65 3d 3d 30 20 29 7b 0a 20 iCompare==0 ){.
1d284 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 pc = pOp->p2
1d285 2d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 - 1;. }else{.
1d286 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 33 20 2d pc = pOp->p3 -
1d287 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 1;. }. break;
1d288 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 .}../* Opcode: A
1d289 6e 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a nd P1 P2 P3 * *.
1d28a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20 6c **.** Take the l
1d28b 6f 67 69 63 61 6c 20 41 4e 44 20 6f 66 20 74 68 ogical AND of th
1d28c 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 e values in regi
1d28d 73 74 65 72 73 20 50 31 20 61 6e 64 20 50 32 20 sters P1 and P2
1d28e 61 6e 64 0a 2a 2a 20 77 72 69 74 65 20 74 68 65 and.** write the
1d28f 20 72 65 73 75 6c 74 20 69 6e 74 6f 20 72 65 67 result into reg
1d290 69 73 74 65 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 ister P3..**.**
1d291 49 66 20 65 69 74 68 65 72 20 50 31 20 6f 72 20 If either P1 or
1d292 50 32 20 69 73 20 30 20 28 66 61 6c 73 65 29 20 P2 is 0 (false)
1d293 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 then the result
1d294 69 73 20 30 20 65 76 65 6e 20 69 66 0a 2a 2a 20 is 0 even if.**
1d295 74 68 65 20 6f 74 68 65 72 20 69 6e 70 75 74 20 the other input
1d296 69 73 20 4e 55 4c 4c 2e 20 20 41 20 4e 55 4c 4c is NULL. A NULL
1d297 20 61 6e 64 20 74 72 75 65 20 6f 72 20 74 77 6f and true or two
1d298 20 4e 55 4c 4c 73 20 67 69 76 65 0a 2a 2a 20 61 NULLs give.** a
1d299 20 4e 55 4c 4c 20 6f 75 74 70 75 74 2e 0a 2a 2f NULL output..*/
1d29a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 72 20 50 ./* Opcode: Or P
1d29b 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 1 P2 P3 * *.**.*
1d29c 2a 20 54 61 6b 65 20 74 68 65 20 6c 6f 67 69 63 * Take the logic
1d29d 61 6c 20 4f 52 20 6f 66 20 74 68 65 20 76 61 6c al OR of the val
1d29e 75 65 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 ues in register
1d29f 50 31 20 61 6e 64 20 50 32 20 61 6e 64 0a 2a 2a P1 and P2 and.**
1d2a0 20 73 74 6f 72 65 20 74 68 65 20 61 6e 73 77 65 store the answe
1d2a1 72 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 r in register P3
1d2a2 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 65 69 74 68 65 ..**.** If eithe
1d2a3 72 20 50 31 20 6f 72 20 50 32 20 69 73 20 6e 6f r P1 or P2 is no
1d2a4 6e 7a 65 72 6f 20 28 74 72 75 65 29 20 74 68 65 nzero (true) the
1d2a5 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 n the result is
1d2a6 31 20 28 74 72 75 65 29 0a 2a 2a 20 65 76 65 6e 1 (true).** even
1d2a7 20 69 66 20 74 68 65 20 6f 74 68 65 72 20 69 6e if the other in
1d2a8 70 75 74 20 69 73 20 4e 55 4c 4c 2e 20 20 41 20 put is NULL. A
1d2a9 4e 55 4c 4c 20 61 6e 64 20 66 61 6c 73 65 20 6f NULL and false o
1d2aa 72 20 74 77 6f 20 4e 55 4c 4c 73 0a 2a 2a 20 67 r two NULLs.** g
1d2ab 69 76 65 20 61 20 4e 55 4c 4c 20 6f 75 74 70 75 ive a NULL outpu
1d2ac 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 6e t..*/.case OP_An
1d2ad 64 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d:
1d2ae 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 41 4e /* same as TK_AN
1d2af 44 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 D, in1, in2, out
1d2b0 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4f 72 3a 3 */.case OP_Or:
1d2b1 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f { /
1d2b2 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4f 52 2c * same as TK_OR,
1d2b3 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 in1, in2, out3
1d2b4 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 */.#if 0 /* loc
1d2b5 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1d2b6 65 64 20 69 6e 74 6f 20 75 2e 61 6b 20 2a 2f 0a ed into u.ak */.
1d2b7 20 20 69 6e 74 20 76 31 3b 20 20 20 20 2f 2a 20 int v1; /*
1d2b8 4c 65 66 74 20 6f 70 65 72 61 6e 64 3a 20 20 30 Left operand: 0
1d2b9 3d 3d 46 41 4c 53 45 2c 20 31 3d 3d 54 52 55 45 ==FALSE, 1==TRUE
1d2ba 2c 20 32 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 , 2==UNKNOWN or
1d2bb 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 76 32 NULL */. int v2
1d2bc 3b 20 20 20 20 2f 2a 20 52 69 67 68 74 20 6f 70 ; /* Right op
1d2bd 65 72 61 6e 64 3a 20 30 3d 3d 46 41 4c 53 45 2c erand: 0==FALSE,
1d2be 20 31 3d 3d 54 52 55 45 2c 20 32 3d 3d 55 4e 4b 1==TRUE, 2==UNK
1d2bf 4e 4f 57 4e 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a NOWN or NULL */.
1d2c0 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 #endif /* local
1d2c1 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1d2c2 69 6e 74 6f 20 75 2e 61 6b 20 2a 2f 0a 0a 20 20 into u.ak */..
1d2c3 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 if( pIn1->flags
1d2c4 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 & MEM_Null ){.
1d2c5 20 20 75 2e 61 6b 2e 76 31 20 3d 20 32 3b 0a 20 u.ak.v1 = 2;.
1d2c6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 6b }else{. u.ak
1d2c7 2e 76 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 .v1 = sqlite3Vdb
1d2c8 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 21 eIntValue(pIn1)!
1d2c9 3d 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 =0;. }. if( pI
1d2ca 6e 32 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f n2->flags & MEM_
1d2cb 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 75 2e 61 6b Null ){. u.ak
1d2cc 2e 76 32 20 3d 20 32 3b 0a 20 20 7d 65 6c 73 65 .v2 = 2;. }else
1d2cd 7b 0a 20 20 20 20 75 2e 61 6b 2e 76 32 20 3d 20 {. u.ak.v2 =
1d2ce 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 sqlite3VdbeIntVa
1d2cf 6c 75 65 28 70 49 6e 32 29 21 3d 30 3b 0a 20 20 lue(pIn2)!=0;.
1d2d0 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 }. if( pOp->opc
1d2d1 6f 64 65 3d 3d 4f 50 5f 41 6e 64 20 29 7b 0a 20 ode==OP_And ){.
1d2d2 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 static const
1d2d3 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61 6e unsigned char an
1d2d4 64 5f 6c 6f 67 69 63 5b 5d 20 3d 20 7b 20 30 2c d_logic[] = { 0,
1d2d5 20 30 2c 20 30 2c 20 30 2c 20 31 2c 20 32 2c 20 0, 0, 0, 1, 2,
1d2d6 30 2c 20 32 2c 20 32 20 7d 3b 0a 20 20 20 20 75 0, 2, 2 };. u
1d2d7 2e 61 6b 2e 76 31 20 3d 20 61 6e 64 5f 6c 6f 67 .ak.v1 = and_log
1d2d8 69 63 5b 75 2e 61 6b 2e 76 31 2a 33 2b 75 2e 61 ic[u.ak.v1*3+u.a
1d2d9 6b 2e 76 32 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a k.v2];. }else{.
1d2da 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 static const
1d2db 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6f unsigned char o
1d2dc 72 5f 6c 6f 67 69 63 5b 5d 20 3d 20 7b 20 30 2c r_logic[] = { 0,
1d2dd 20 31 2c 20 32 2c 20 31 2c 20 31 2c 20 31 2c 20 1, 2, 1, 1, 1,
1d2de 32 2c 20 31 2c 20 32 20 7d 3b 0a 20 20 20 20 75 2, 1, 2 };. u
1d2df 2e 61 6b 2e 76 31 20 3d 20 6f 72 5f 6c 6f 67 69 .ak.v1 = or_logi
1d2e0 63 5b 75 2e 61 6b 2e 76 31 2a 33 2b 75 2e 61 6b c[u.ak.v1*3+u.ak
1d2e1 2e 76 32 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 .v2];. }. if(
1d2e2 75 2e 61 6b 2e 76 31 3d 3d 32 20 29 7b 0a 20 20 u.ak.v1==2 ){.
1d2e3 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 MemSetTypeFlag
1d2e4 28 70 4f 75 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 (pOut, MEM_Null)
1d2e5 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
1d2e6 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 6b 2e Out->u.i = u.ak.
1d2e7 76 31 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 v1;. MemSetTy
1d2e8 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d peFlag(pOut, MEM
1d2e9 5f 49 6e 74 29 3b 0a 20 20 7d 0a 20 20 62 72 65 _Int);. }. bre
1d2ea 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1d2eb 3a 20 4e 6f 74 20 50 31 20 50 32 20 2a 20 2a 20 : Not P1 P2 * *
1d2ec 2a 0a 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 *.**.** Interpre
1d2ed 74 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 t the value in r
1d2ee 65 67 69 73 74 65 72 20 50 31 20 61 73 20 61 20 egister P1 as a
1d2ef 62 6f 6f 6c 65 61 6e 20 76 61 6c 75 65 2e 20 20 boolean value.
1d2f0 53 74 6f 72 65 20 74 68 65 0a 2a 2a 20 62 6f 6f Store the.** boo
1d2f1 6c 65 61 6e 20 63 6f 6d 70 6c 65 6d 65 6e 74 20 lean complement
1d2f2 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e 20 in register P2.
1d2f3 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e If the value in
1d2f4 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20 register P1 is
1d2f5 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 .** NULL, then a
1d2f6 20 4e 55 4c 4c 20 69 73 20 73 74 6f 72 65 64 20 NULL is stored
1d2f7 69 6e 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f in P2..*/.case O
1d2f8 50 5f 4e 6f 74 3a 20 7b 20 20 20 20 20 20 20 20 P_Not: {
1d2f9 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 /* same
1d2fa 61 73 20 54 4b 5f 4e 4f 54 2c 20 69 6e 31 20 2a as TK_NOT, in1 *
1d2fb 2f 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 /. pOut = &p->a
1d2fc 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 Mem[pOp->p2];.
1d2fd 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 if( pIn1->flags
1d2fe 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 & MEM_Null ){.
1d2ff 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1d300 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 SetNull(pOut);.
1d301 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 }else{. sqli
1d302 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 te3VdbeMemSetInt
1d303 36 34 28 70 4f 75 74 2c 20 21 73 71 6c 69 74 65 64(pOut, !sqlite
1d304 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 3VdbeIntValue(pI
1d305 6e 31 29 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 n1));. }. brea
1d306 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1d307 20 42 69 74 4e 6f 74 20 50 31 20 50 32 20 2a 20 BitNot P1 P2 *
1d308 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70 * *.**.** Interp
1d309 72 65 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 ret the content
1d30a 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 20 61 of register P1 a
1d30b 73 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 53 s an integer. S
1d30c 74 6f 72 65 20 74 68 65 0a 2a 2a 20 6f 6e 65 73 tore the.** ones
1d30d 2d 63 6f 6d 70 6c 65 6d 65 6e 74 20 6f 66 20 74 -complement of t
1d30e 68 65 20 50 31 20 76 61 6c 75 65 20 69 6e 74 6f he P1 value into
1d30f 20 72 65 67 69 73 74 65 72 20 50 32 2e 20 20 49 register P2. I
1d310 66 20 50 31 20 68 6f 6c 64 73 0a 2a 2a 20 61 20 f P1 holds.** a
1d311 4e 55 4c 4c 20 74 68 65 6e 20 73 74 6f 72 65 20 NULL then store
1d312 61 20 4e 55 4c 4c 20 69 6e 20 50 32 2e 0a 2a 2f a NULL in P2..*/
1d313 0a 63 61 73 65 20 4f 50 5f 42 69 74 4e 6f 74 3a .case OP_BitNot:
1d314 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f { /
1d315 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 * same as TK_BIT
1d316 4e 4f 54 2c 20 69 6e 31 20 2a 2f 0a 20 20 70 4f NOT, in1 */. pO
1d317 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f ut = &p->aMem[pO
1d318 70 2d 3e 70 32 5d 3b 0a 20 20 69 66 28 20 70 49 p->p2];. if( pI
1d319 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f n1->flags & MEM_
1d31a 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 Null ){. sqli
1d31b 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c te3VdbeMemSetNul
1d31c 6c 28 70 4f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 l(pOut);. }else
1d31d 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
1d31e 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 70 4f 75 eMemSetInt64(pOu
1d31f 74 2c 20 7e 73 71 6c 69 74 65 33 56 64 62 65 49 t, ~sqlite3VdbeI
1d320 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 29 3b 0a ntValue(pIn1));.
1d321 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1d322 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66 20 50 31 /* Opcode: If P1
1d323 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a P2 P3 * *.**.**
1d324 20 4a 75 6d 70 20 74 6f 20 50 32 20 69 66 20 74 Jump to P2 if t
1d325 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 he value in regi
1d326 73 74 65 72 20 50 31 20 69 73 20 74 72 75 65 2e ster P1 is true.
1d327 20 20 54 68 65 20 76 61 6c 75 65 20 69 73 0a 2a The value is.*
1d328 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 * is considered
1d329 74 72 75 65 20 69 66 20 69 74 20 69 73 20 6e 75 true if it is nu
1d32a 6d 65 72 69 63 20 61 6e 64 20 6e 6f 6e 2d 7a 65 meric and non-ze
1d32b 72 6f 2e 20 20 49 66 20 74 68 65 20 76 61 6c 75 ro. If the valu
1d32c 65 0a 2a 2a 20 69 6e 20 50 31 20 69 73 20 4e 55 e.** in P1 is NU
1d32d 4c 4c 20 74 68 65 6e 20 74 61 6b 65 20 74 68 65 LL then take the
1d32e 20 6a 75 6d 70 20 69 66 20 50 33 20 69 73 20 74 jump if P3 is t
1d32f 72 75 65 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 rue..*/./* Opcod
1d330 65 3a 20 49 66 4e 6f 74 20 50 31 20 50 32 20 50 e: IfNot P1 P2 P
1d331 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 3 * *.**.** Jump
1d332 20 74 6f 20 50 32 20 69 66 20 74 68 65 20 76 61 to P2 if the va
1d333 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 lue in register
1d334 50 31 20 69 73 20 46 61 6c 73 65 2e 20 20 54 68 P1 is False. Th
1d335 65 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 69 73 e value is.** is
1d336 20 63 6f 6e 73 69 64 65 72 65 64 20 74 72 75 65 considered true
1d337 20 69 66 20 69 74 20 68 61 73 20 61 20 6e 75 6d if it has a num
1d338 65 72 69 63 20 76 61 6c 75 65 20 6f 66 20 7a 65 eric value of ze
1d339 72 6f 2e 20 20 49 66 20 74 68 65 20 76 61 6c 75 ro. If the valu
1d33a 65 0a 2a 2a 20 69 6e 20 50 31 20 69 73 20 4e 55 e.** in P1 is NU
1d33b 4c 4c 20 74 68 65 6e 20 74 61 6b 65 20 74 68 65 LL then take the
1d33c 20 6a 75 6d 70 20 69 66 20 50 33 20 69 73 20 74 jump if P3 is t
1d33d 72 75 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f rue..*/.case OP_
1d33e 49 66 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 If:
1d33f 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 /* jump, in1
1d340 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 66 4e 6f */.case OP_IfNo
1d341 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 t: {
1d342 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a /* jump, in1 */.
1d343 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 #if 0 /* local
1d344 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1d345 69 6e 74 6f 20 75 2e 61 6c 20 2a 2f 0a 20 20 69 into u.al */. i
1d346 6e 74 20 63 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 nt c;.#endif /*
1d347 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1d348 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6c 20 moved into u.al
1d349 2a 2f 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 */. if( pIn1->f
1d34a 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 lags & MEM_Null
1d34b 29 7b 0a 20 20 20 20 75 2e 61 6c 2e 63 20 3d 20 ){. u.al.c =
1d34c 70 4f 70 2d 3e 70 33 3b 0a 20 20 7d 65 6c 73 65 pOp->p3;. }else
1d34d 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f {.#ifdef SQLITE_
1d34e 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f OMIT_FLOATING_PO
1d34f 49 4e 54 0a 20 20 20 20 75 2e 61 6c 2e 63 20 3d INT. u.al.c =
1d350 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 sqlite3VdbeIntV
1d351 61 6c 75 65 28 70 49 6e 31 29 21 3d 30 3b 0a 23 alue(pIn1)!=0;.#
1d352 65 6c 73 65 0a 20 20 20 20 75 2e 61 6c 2e 63 20 else. u.al.c
1d353 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 61 = sqlite3VdbeRea
1d354 6c 56 61 6c 75 65 28 70 49 6e 31 29 21 3d 30 2e lValue(pIn1)!=0.
1d355 30 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 0;.#endif. if
1d356 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f ( pOp->opcode==O
1d357 50 5f 49 66 4e 6f 74 20 29 20 75 2e 61 6c 2e 63 P_IfNot ) u.al.c
1d358 20 3d 20 21 75 2e 61 6c 2e 63 3b 0a 20 20 7d 0a = !u.al.c;. }.
1d359 20 20 69 66 28 20 75 2e 61 6c 2e 63 20 29 7b 0a if( u.al.c ){.
1d35a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 pc = pOp->p2
1d35b 2d 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b -1;. }. break;
1d35c 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 .}../* Opcode: I
1d35d 73 4e 75 6c 6c 20 50 31 20 50 32 20 2a 20 2a 20 sNull P1 P2 * *
1d35e 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 *.**.** Jump to
1d35f 50 32 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 P2 if the value
1d360 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 69 in register P1 i
1d361 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 s NULL..*/.case
1d362 4f 50 5f 49 73 4e 75 6c 6c 3a 20 7b 20 20 20 20 OP_IsNull: {
1d363 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 /* same
1d364 61 73 20 54 4b 5f 49 53 4e 55 4c 4c 2c 20 6a 75 as TK_ISNULL, ju
1d365 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 mp, in1 */. if(
1d366 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 (pIn1->flags &
1d367 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30 20 29 7b 0a MEM_Null)!=0 ){.
1d368 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 pc = pOp->p2
1d369 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 - 1;. }. brea
1d36a 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1d36b 20 4e 6f 74 4e 75 6c 6c 20 50 31 20 50 32 20 2a NotNull P1 P2 *
1d36c 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 * *.**.** Jump
1d36d 74 6f 20 50 32 20 69 66 20 74 68 65 20 76 61 6c to P2 if the val
1d36e 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ue in register P
1d36f 31 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2e 20 20 1 is not NULL.
1d370 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f 74 4e .*/.case OP_NotN
1d371 75 6c 6c 3a 20 7b 20 20 20 20 20 20 20 20 20 20 ull: {
1d372 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
1d373 4e 4f 54 4e 55 4c 4c 2c 20 6a 75 6d 70 2c 20 69 NOTNULL, jump, i
1d374 6e 31 20 2a 2f 0a 20 20 69 66 28 20 28 70 49 6e n1 */. if( (pIn
1d375 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 1->flags & MEM_N
1d376 75 6c 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 ull)==0 ){. p
1d377 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b c = pOp->p2 - 1;
1d378 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1d379 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6c 75 ./* Opcode: Colu
1d37a 6d 6e 20 50 31 20 50 32 20 50 33 20 50 34 20 50 mn P1 P2 P3 P4 P
1d37b 35 0a 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 5.**.** Interpre
1d37c 74 20 74 68 65 20 64 61 74 61 20 74 68 61 74 20 t the data that
1d37d 63 75 72 73 6f 72 20 50 31 20 70 6f 69 6e 74 73 cursor P1 points
1d37e 20 74 6f 20 61 73 20 61 20 73 74 72 75 63 74 75 to as a structu
1d37f 72 65 20 62 75 69 6c 74 20 75 73 69 6e 67 0a 2a re built using.*
1d380 2a 20 74 68 65 20 4d 61 6b 65 52 65 63 6f 72 64 * the MakeRecord
1d381 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 28 instruction. (
1d382 53 65 65 20 74 68 65 20 4d 61 6b 65 52 65 63 6f See the MakeReco
1d383 72 64 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 rd opcode for ad
1d384 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f ditional.** info
1d385 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 rmation about th
1d386 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 68 65 20 e format of the
1d387 64 61 74 61 2e 29 20 20 45 78 74 72 61 63 74 20 data.) Extract
1d388 74 68 65 20 50 32 2d 74 68 20 63 6f 6c 75 6d 6e the P2-th column
1d389 0a 2a 2a 20 66 72 6f 6d 20 74 68 69 73 20 72 65 .** from this re
1d38a 63 6f 72 64 2e 20 20 49 66 20 74 68 65 72 65 20 cord. If there
1d38b 61 72 65 20 6c 65 73 73 20 74 68 61 74 20 28 50 are less that (P
1d38c 32 2b 31 29 20 0a 2a 2a 20 76 61 6c 75 65 73 20 2+1) .** values
1d38d 69 6e 20 74 68 65 20 72 65 63 6f 72 64 2c 20 65 in the record, e
1d38e 78 74 72 61 63 74 20 61 20 4e 55 4c 4c 2e 0a 2a xtract a NULL..*
1d38f 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 65 *.** The value e
1d390 78 74 72 61 63 74 65 64 20 69 73 20 73 74 6f 72 xtracted is stor
1d391 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ed in register P
1d392 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 3..**.** If the
1d393 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e 73 20 column contains
1d394 66 65 77 65 72 20 74 68 61 6e 20 50 32 20 66 69 fewer than P2 fi
1d395 65 6c 64 73 2c 20 74 68 65 6e 20 65 78 74 72 61 elds, then extra
1d396 63 74 20 61 20 4e 55 4c 4c 2e 20 20 4f 72 2c 0a ct a NULL. Or,.
1d397 2a 2a 20 69 66 20 74 68 65 20 50 34 20 61 72 67 ** if the P4 arg
1d398 75 6d 65 6e 74 20 69 73 20 61 20 50 34 5f 4d 45 ument is a P4_ME
1d399 4d 20 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 M use the value
1d39a 6f 66 20 74 68 65 20 50 34 20 61 72 67 75 6d 65 of the P4 argume
1d39b 6e 74 20 61 73 0a 2a 2a 20 74 68 65 20 72 65 73 nt as.** the res
1d39c 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ult..**.** If th
1d39d 65 20 4f 50 46 4c 41 47 5f 43 4c 45 41 52 43 41 e OPFLAG_CLEARCA
1d39e 43 48 45 20 62 69 74 20 69 73 20 73 65 74 20 6f CHE bit is set o
1d39f 6e 20 50 35 20 61 6e 64 20 50 31 20 69 73 20 61 n P5 and P1 is a
1d3a0 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 63 75 pseudo-table cu
1d3a1 72 73 6f 72 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 rsor,.** then th
1d3a2 65 20 63 61 63 68 65 20 6f 66 20 74 68 65 20 63 e cache of the c
1d3a3 75 72 73 6f 72 20 69 73 20 72 65 73 65 74 20 70 ursor is reset p
1d3a4 72 69 6f 72 20 74 6f 20 65 78 74 72 61 63 74 69 rior to extracti
1d3a5 6e 67 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 0a 2a ng the column..*
1d3a6 2a 20 54 68 65 20 66 69 72 73 74 20 4f 50 5f 43 * The first OP_C
1d3a7 6f 6c 75 6d 6e 20 61 67 61 69 6e 73 74 20 61 20 olumn against a
1d3a8 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 61 66 74 pseudo-table aft
1d3a9 65 72 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 er the value of
1d3aa 74 68 65 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 72 the content.** r
1d3ab 65 67 69 73 74 65 72 20 68 61 73 20 63 68 61 6e egister has chan
1d3ac 67 65 64 20 73 68 6f 75 6c 64 20 68 61 76 65 20 ged should have
1d3ad 74 68 69 73 20 62 69 74 20 73 65 74 2e 0a 2a 2f this bit set..*/
1d3ae 0a 63 61 73 65 20 4f 50 5f 43 6f 6c 75 6d 6e 3a .case OP_Column:
1d3af 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 {.#if 0 /* loc
1d3b0 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1d3b1 65 64 20 69 6e 74 6f 20 75 2e 61 6d 20 2a 2f 0a ed into u.am */.
1d3b2 20 20 75 33 32 20 70 61 79 6c 6f 61 64 53 69 7a u32 payloadSiz
1d3b3 65 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f e; /* Number o
1d3b4 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 72 f bytes in the r
1d3b5 65 63 6f 72 64 20 2a 2f 0a 20 20 69 36 34 20 70 ecord */. i64 p
1d3b6 61 79 6c 6f 61 64 53 69 7a 65 36 34 3b 20 2f 2a ayloadSize64; /*
1d3b7 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
1d3b8 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a in the record *
1d3b9 2f 0a 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20 /. int p1;
1d3ba 20 20 20 20 20 20 20 2f 2a 20 50 31 20 76 61 6c /* P1 val
1d3bb 75 65 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 ue of the opcode
1d3bc 20 2a 2f 0a 20 20 69 6e 74 20 70 32 3b 20 20 20 */. int p2;
1d3bd 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f 6c 75 /* colu
1d3be 6d 6e 20 6e 75 6d 62 65 72 20 74 6f 20 72 65 74 mn number to ret
1d3bf 72 69 65 76 65 20 2a 2f 0a 20 20 56 64 62 65 43 rieve */. VdbeC
1d3c0 75 72 73 6f 72 20 2a 70 43 3b 20 20 20 20 2f 2a ursor *pC; /*
1d3c1 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 The VDBE cursor
1d3c2 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 52 65 63 */. char *zRec
1d3c3 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e ; /* Poin
1d3c4 74 65 72 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 ter to complete
1d3c5 72 65 63 6f 72 64 2d 64 61 74 61 20 2a 2f 0a 20 record-data */.
1d3c6 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 BtCursor *pCrsr
1d3c7 3b 20 20 20 2f 2a 20 54 68 65 20 42 54 72 65 65 ; /* The BTree
1d3c8 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 75 33 32 cursor */. u32
1d3c9 20 2a 61 54 79 70 65 3b 20 20 20 20 20 20 20 20 *aType;
1d3ca 2f 2a 20 61 54 79 70 65 5b 69 5d 20 68 6f 6c 64 /* aType[i] hold
1d3cb 73 20 74 68 65 20 6e 75 6d 65 72 69 63 20 74 79 s the numeric ty
1d3cc 70 65 20 6f 66 20 74 68 65 20 69 2d 74 68 20 63 pe of the i-th c
1d3cd 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 75 33 32 20 2a olumn */. u32 *
1d3ce 61 4f 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a aOffset; /*
1d3cf 20 61 4f 66 66 73 65 74 5b 69 5d 20 69 73 20 6f aOffset[i] is o
1d3d0 66 66 73 65 74 20 74 6f 20 73 74 61 72 74 20 6f ffset to start o
1d3d1 66 20 64 61 74 61 20 66 6f 72 20 69 2d 74 68 20 f data for i-th
1d3d2 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 column */. int
1d3d3 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20 2f nField; /
1d3d4 2a 20 6e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c * number of fiel
1d3d5 64 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 ds in the record
1d3d6 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 3b 20 20 */. int len;
1d3d7 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1d3d8 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73 65 length of the se
1d3d9 72 69 61 6c 69 7a 65 64 20 64 61 74 61 20 66 6f rialized data fo
1d3da 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a r the column */.
1d3db 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 int i;
1d3dc 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 /* Loop cou
1d3dd 6e 74 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a nter */. char *
1d3de 7a 44 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 zData; /*
1d3df 50 61 72 74 20 6f 66 20 74 68 65 20 72 65 63 6f Part of the reco
1d3e0 72 64 20 62 65 69 6e 67 20 64 65 63 6f 64 65 64 rd being decoded
1d3e1 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 44 65 73 74 */. Mem *pDest
1d3e2 3b 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72 ; /* Wher
1d3e3 65 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 65 e to write the e
1d3e4 78 74 72 61 63 74 65 64 20 76 61 6c 75 65 20 2a xtracted value *
1d3e5 2f 0a 20 20 4d 65 6d 20 73 4d 65 6d 3b 20 20 20 /. Mem sMem;
1d3e6 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 73 74 /* For st
1d3e7 6f 72 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64 oring the record
1d3e8 20 62 65 69 6e 67 20 64 65 63 6f 64 65 64 20 2a being decoded *
1d3e9 2f 0a 20 20 75 38 20 2a 7a 49 64 78 3b 20 20 20 /. u8 *zIdx;
1d3ea 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
1d3eb 69 6e 74 6f 20 68 65 61 64 65 72 20 2a 2f 0a 20 into header */.
1d3ec 20 75 38 20 2a 7a 45 6e 64 48 64 72 3b 20 20 20 u8 *zEndHdr;
1d3ed 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
1d3ee 6f 20 66 69 72 73 74 20 62 79 74 65 20 61 66 74 o first byte aft
1d3ef 65 72 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f er the header */
1d3f0 0a 20 20 75 33 32 20 6f 66 66 73 65 74 3b 20 20 . u32 offset;
1d3f1 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 /* Offset
1d3f2 69 6e 74 6f 20 74 68 65 20 64 61 74 61 20 2a 2f into the data */
1d3f3 0a 20 20 75 36 34 20 6f 66 66 73 65 74 36 34 3b . u64 offset64;
1d3f4 20 20 20 20 20 20 2f 2a 20 36 34 2d 62 69 74 20 /* 64-bit
1d3f5 6f 66 66 73 65 74 2e 20 20 36 34 20 62 69 74 73 offset. 64 bits
1d3f6 20 6e 65 65 64 65 64 20 74 6f 20 63 61 74 63 68 needed to catch
1d3f7 20 6f 76 65 72 66 6c 6f 77 20 2a 2f 0a 20 20 69 overflow */. i
1d3f8 6e 74 20 73 7a 48 64 72 3b 20 20 20 20 20 20 20 nt szHdr;
1d3f9 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 /* Size of the
1d3fa 20 68 65 61 64 65 72 20 73 69 7a 65 20 66 69 65 header size fie
1d3fb 6c 64 20 61 74 20 73 74 61 72 74 20 6f 66 20 72 ld at start of r
1d3fc 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 61 ecord */. int a
1d3fd 76 61 69 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a vail; /*
1d3fe 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
1d3ff 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 64 61 of available da
1d400 74 61 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52 65 ta */. Mem *pRe
1d401 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 50 73 g; /* Ps
1d402 65 75 64 6f 54 61 62 6c 65 20 69 6e 70 75 74 20 eudoTable input
1d403 72 65 67 69 73 74 65 72 20 2a 2f 0a 23 65 6e 64 register */.#end
1d404 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1d405 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1d406 20 75 2e 61 6d 20 2a 2f 0a 0a 0a 20 20 75 2e 61 u.am */... u.a
1d407 6d 2e 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a m.p1 = pOp->p1;.
1d408 20 20 75 2e 61 6d 2e 70 32 20 3d 20 70 4f 70 2d u.am.p2 = pOp-
1d409 3e 70 32 3b 0a 20 20 75 2e 61 6d 2e 70 43 20 3d >p2;. u.am.pC =
1d40a 20 30 3b 0a 20 20 6d 65 6d 73 65 74 28 26 75 2e 0;. memset(&u.
1d40b 61 6d 2e 73 4d 65 6d 2c 20 30 2c 20 73 69 7a 65 am.sMem, 0, size
1d40c 6f 66 28 75 2e 61 6d 2e 73 4d 65 6d 29 29 3b 0a of(u.am.sMem));.
1d40d 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d 2e 70 assert( u.am.p
1d40e 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 1<p->nCursor );.
1d40f 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1d410 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 3>0 && pOp->p3<=
1d411 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e 61 p->nMem );. u.a
1d412 6d 2e 70 44 65 73 74 20 3d 20 26 70 2d 3e 61 4d m.pDest = &p->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 70 u.am.pReg = &p
1d48f 2d 3e 61 4d 65 6d 5b 75 2e 61 6d 2e 70 43 2d 3e ->aMem[u.am.pC->
1d490 70 73 65 75 64 6f 54 61 62 6c 65 52 65 67 5d 3b pseudoTableReg];
1d491 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 . assert( u.a
1d492 6d 2e 70 52 65 67 2d 3e 66 6c 61 67 73 20 26 20 m.pReg->flags &
1d493 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 MEM_Blob );.
1d494 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 u.am.payloadSize
1d495 20 3d 20 75 2e 61 6d 2e 70 52 65 67 2d 3e 6e 3b = u.am.pReg->n;
1d496 0a 20 20 20 20 75 2e 61 6d 2e 7a 52 65 63 20 3d . u.am.zRec =
1d497 20 75 2e 61 6d 2e 70 52 65 67 2d 3e 7a 3b 0a 20 u.am.pReg->z;.
1d498 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 63 61 63 68 u.am.pC->cach
1d499 65 53 74 61 74 75 73 20 3d 20 28 70 4f 70 2d 3e eStatus = (pOp->
1d49a 70 35 26 4f 50 46 4c 41 47 5f 43 4c 45 41 52 43 p5&OPFLAG_CLEARC
1d49b 41 43 48 45 29 20 3f 20 43 41 43 48 45 5f 53 54 ACHE) ? CACHE_ST
1d49c 41 4c 45 20 3a 20 70 2d 3e 63 61 63 68 65 43 74 ALE : p->cacheCt
1d49d 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 r;. assert( u
1d49e 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 3d .am.payloadSize=
1d49f 3d 30 20 7c 7c 20 75 2e 61 6d 2e 7a 52 65 63 21 =0 || u.am.zRec!
1d4a0 3d 30 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 =0 );. }else{.
1d4a1 20 20 20 2f 2a 20 43 6f 6e 73 69 64 65 72 20 74 /* Consider t
1d4a2 68 65 20 72 6f 77 20 74 6f 20 62 65 20 4e 55 4c he row to be NUL
1d4a3 4c 20 2a 2f 0a 20 20 20 20 75 2e 61 6d 2e 70 61 L */. u.am.pa
1d4a4 79 6c 6f 61 64 53 69 7a 65 20 3d 20 30 3b 0a 20 yloadSize = 0;.
1d4a5 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 75 2e 61 6d }.. /* If u.am
1d4a6 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20 69 73 20 .payloadSize is
1d4a7 30 2c 20 74 68 65 6e 20 6a 75 73 74 20 73 74 6f 0, then just sto
1d4a8 72 65 20 61 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 re a NULL */. i
1d4a9 66 28 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 f( u.am.payloadS
1d4aa 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 ize==0 ){. as
1d4ab 73 65 72 74 28 20 75 2e 61 6d 2e 70 44 65 73 74 sert( u.am.pDest
1d4ac 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c ->flags&MEM_Null
1d4ad 20 29 3b 0a 20 20 20 20 67 6f 74 6f 20 6f 70 5f );. goto op_
1d4ae 63 6f 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 20 7d 0a column_out;. }.
1d4af 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 4c assert( db->aL
1d4b0 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 imit[SQLITE_LIMI
1d4b1 54 5f 4c 45 4e 47 54 48 5d 3e 3d 30 20 29 3b 0a T_LENGTH]>=0 );.
1d4b2 20 20 69 66 28 20 75 2e 61 6d 2e 70 61 79 6c 6f if( u.am.paylo
1d4b3 61 64 53 69 7a 65 20 3e 20 28 75 33 32 29 64 62 adSize > (u32)db
1d4b4 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f ->aLimit[SQLITE_
1d4b5 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b LIMIT_LENGTH] ){
1d4b6 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 . goto too_bi
1d4b7 67 3b 0a 20 20 7d 0a 0a 20 20 75 2e 61 6d 2e 6e g;. }.. u.am.n
1d4b8 46 69 65 6c 64 20 3d 20 75 2e 61 6d 2e 70 43 2d Field = u.am.pC-
1d4b9 3e 6e 46 69 65 6c 64 3b 0a 20 20 61 73 73 65 72 >nField;. asser
1d4ba 74 28 20 75 2e 61 6d 2e 70 32 3c 75 2e 61 6d 2e t( u.am.p2<u.am.
1d4bb 6e 46 69 65 6c 64 20 29 3b 0a 0a 20 20 2f 2a 20 nField );.. /*
1d4bc 52 65 61 64 20 61 6e 64 20 70 61 72 73 65 20 74 Read and parse t
1d4bd 68 65 20 74 61 62 6c 65 20 68 65 61 64 65 72 2e he table header.
1d4be 20 20 53 74 6f 72 65 20 74 68 65 20 72 65 73 75 Store the resu
1d4bf 6c 74 73 20 6f 66 20 74 68 65 20 70 61 72 73 65 lts of the parse
1d4c0 0a 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 72 . ** into the r
1d4c1 65 63 6f 72 64 20 68 65 61 64 65 72 20 63 61 63 ecord header cac
1d4c2 68 65 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 he fields of the
1d4c3 20 63 75 72 73 6f 72 2e 0a 20 20 2a 2f 0a 20 20 cursor.. */.
1d4c4 75 2e 61 6d 2e 61 54 79 70 65 20 3d 20 75 2e 61 u.am.aType = u.a
1d4c5 6d 2e 70 43 2d 3e 61 54 79 70 65 3b 0a 20 20 69 m.pC->aType;. i
1d4c6 66 28 20 75 2e 61 6d 2e 70 43 2d 3e 63 61 63 68 f( u.am.pC->cach
1d4c7 65 53 74 61 74 75 73 3d 3d 70 2d 3e 63 61 63 68 eStatus==p->cach
1d4c8 65 43 74 72 20 29 7b 0a 20 20 20 20 75 2e 61 6d eCtr ){. u.am
1d4c9 2e 61 4f 66 66 73 65 74 20 3d 20 75 2e 61 6d 2e .aOffset = u.am.
1d4ca 70 43 2d 3e 61 4f 66 66 73 65 74 3b 0a 20 20 7d pC->aOffset;. }
1d4cb 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 else{. assert
1d4cc 28 75 2e 61 6d 2e 61 54 79 70 65 29 3b 0a 20 20 (u.am.aType);.
1d4cd 20 20 75 2e 61 6d 2e 61 76 61 69 6c 20 3d 20 30 u.am.avail = 0
1d4ce 3b 0a 20 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 61 ;. u.am.pC->a
1d4cf 4f 66 66 73 65 74 20 3d 20 75 2e 61 6d 2e 61 4f Offset = u.am.aO
1d4d0 66 66 73 65 74 20 3d 20 26 75 2e 61 6d 2e 61 54 ffset = &u.am.aT
1d4d1 79 70 65 5b 75 2e 61 6d 2e 6e 46 69 65 6c 64 5d ype[u.am.nField]
1d4d2 3b 0a 20 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 70 ;. u.am.pC->p
1d4d3 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20 75 2e 61 ayloadSize = u.a
1d4d4 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 3b 0a 20 m.payloadSize;.
1d4d5 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 63 61 63 68 u.am.pC->cach
1d4d6 65 53 74 61 74 75 73 20 3d 20 70 2d 3e 63 61 63 eStatus = p->cac
1d4d7 68 65 43 74 72 3b 0a 0a 20 20 20 20 2f 2a 20 46 heCtr;.. /* F
1d4d8 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 igure out how ma
1d4d9 6e 79 20 62 79 74 65 73 20 61 72 65 20 69 6e 20 ny bytes are in
1d4da 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 the header */.
1d4db 20 20 69 66 28 20 75 2e 61 6d 2e 7a 52 65 63 20 if( u.am.zRec
1d4dc 29 7b 0a 20 20 20 20 20 20 75 2e 61 6d 2e 7a 44 ){. u.am.zD
1d4dd 61 74 61 20 3d 20 75 2e 61 6d 2e 7a 52 65 63 3b ata = u.am.zRec;
1d4de 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
1d4df 20 20 69 66 28 20 75 2e 61 6d 2e 70 43 2d 3e 69 if( u.am.pC->i
1d4e0 73 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 20 20 sIndex ){.
1d4e1 20 20 75 2e 61 6d 2e 7a 44 61 74 61 20 3d 20 28 u.am.zData = (
1d4e2 63 68 61 72 2a 29 73 71 6c 69 74 65 33 42 74 72 char*)sqlite3Btr
1d4e3 65 65 4b 65 79 46 65 74 63 68 28 75 2e 61 6d 2e eeKeyFetch(u.am.
1d4e4 70 43 72 73 72 2c 20 26 75 2e 61 6d 2e 61 76 61 pCrsr, &u.am.ava
1d4e5 69 6c 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 il);. }else
1d4e6 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 6d 2e 7a {. u.am.z
1d4e7 44 61 74 61 20 3d 20 28 63 68 61 72 2a 29 73 71 Data = (char*)sq
1d4e8 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 46 65 lite3BtreeDataFe
1d4e9 74 63 68 28 75 2e 61 6d 2e 70 43 72 73 72 2c 20 tch(u.am.pCrsr,
1d4ea 26 75 2e 61 6d 2e 61 76 61 69 6c 29 3b 0a 20 20 &u.am.avail);.
1d4eb 20 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 49 }. /* I
1d4ec 66 20 4b 65 79 46 65 74 63 68 28 29 2f 44 61 74 f KeyFetch()/Dat
1d4ed 61 46 65 74 63 68 28 29 20 6d 61 6e 61 67 65 64 aFetch() managed
1d4ee 20 74 6f 20 67 65 74 20 74 68 65 20 65 6e 74 69 to get the enti
1d4ef 72 65 20 70 61 79 6c 6f 61 64 2c 0a 20 20 20 20 re payload,.
1d4f0 20 20 2a 2a 20 73 61 76 65 20 74 68 65 20 70 61 ** save the pa
1d4f1 79 6c 6f 61 64 20 69 6e 20 74 68 65 20 75 2e 61 yload in the u.a
1d4f2 6d 2e 70 43 2d 3e 61 52 6f 77 20 63 61 63 68 65 m.pC->aRow cache
1d4f3 2e 20 20 54 68 61 74 20 77 69 6c 6c 20 73 61 76 . That will sav
1d4f4 65 20 75 73 20 66 72 6f 6d 0a 20 20 20 20 20 20 e us from.
1d4f5 2a 2a 20 68 61 76 69 6e 67 20 74 6f 20 6d 61 6b ** having to mak
1d4f6 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 63 61 6c e additional cal
1d4f7 6c 73 20 74 6f 20 66 65 74 63 68 20 74 68 65 20 ls to fetch the
1d4f8 63 6f 6e 74 65 6e 74 20 70 6f 72 74 69 6f 6e 20 content portion
1d4f9 6f 66 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 of. ** the
1d4fa 72 65 63 6f 72 64 2e 0a 20 20 20 20 20 20 2a 2f record.. */
1d4fb 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 . assert( u
1d4fc 2e 61 6d 2e 61 76 61 69 6c 3e 3d 30 20 29 3b 0a .am.avail>=0 );.
1d4fd 20 20 20 20 20 20 69 66 28 20 75 2e 61 6d 2e 70 if( u.am.p
1d4fe 61 79 6c 6f 61 64 53 69 7a 65 20 3c 3d 20 28 75 ayloadSize <= (u
1d4ff 33 32 29 75 2e 61 6d 2e 61 76 61 69 6c 20 29 7b 32)u.am.avail ){
1d500 0a 20 20 20 20 20 20 20 20 75 2e 61 6d 2e 7a 52 . u.am.zR
1d501 65 63 20 3d 20 75 2e 61 6d 2e 7a 44 61 74 61 3b ec = u.am.zData;
1d502 0a 20 20 20 20 20 20 20 20 75 2e 61 6d 2e 70 43 . u.am.pC
1d503 2d 3e 61 52 6f 77 20 3d 20 28 75 38 2a 29 75 2e ->aRow = (u8*)u.
1d504 61 6d 2e 7a 44 61 74 61 3b 0a 20 20 20 20 20 20 am.zData;.
1d505 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 75 }else{. u
1d506 2e 61 6d 2e 70 43 2d 3e 61 52 6f 77 20 3d 20 30 .am.pC->aRow = 0
1d507 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1d508 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f /* The follo
1d509 77 69 6e 67 20 61 73 73 65 72 74 20 69 73 20 74 wing assert is t
1d50a 72 75 65 20 69 6e 20 61 6c 6c 20 63 61 73 65 73 rue in all cases
1d50b 20 61 63 63 65 70 74 20 77 68 65 6e 0a 20 20 20 accept when.
1d50c 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 ** the database
1d50d 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20 63 file has been c
1d50e 6f 72 72 75 70 74 65 64 20 65 78 74 65 72 6e 61 orrupted externa
1d50f 6c 6c 79 2e 0a 20 20 20 20 2a 2a 20 20 20 20 61 lly.. ** a
1d510 73 73 65 72 74 28 20 75 2e 61 6d 2e 7a 52 65 63 ssert( u.am.zRec
1d511 21 3d 30 20 7c 7c 20 75 2e 61 6d 2e 61 76 61 69 !=0 || u.am.avai
1d512 6c 3e 3d 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 l>=u.am.payloadS
1d513 69 7a 65 20 7c 7c 20 75 2e 61 6d 2e 61 76 61 69 ize || u.am.avai
1d514 6c 3e 3d 39 20 29 3b 20 2a 2f 0a 20 20 20 20 75 l>=9 ); */. u
1d515 2e 61 6d 2e 73 7a 48 64 72 20 3d 20 67 65 74 56 .am.szHdr = getV
1d516 61 72 69 6e 74 33 32 28 28 75 38 2a 29 75 2e 61 arint32((u8*)u.a
1d517 6d 2e 7a 44 61 74 61 2c 20 75 2e 61 6d 2e 6f 66 m.zData, u.am.of
1d518 66 73 65 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 4d fset);.. /* M
1d519 61 6b 65 20 73 75 72 65 20 61 20 63 6f 72 72 75 ake sure a corru
1d51a 70 74 20 64 61 74 61 62 61 73 65 20 68 61 73 20 pt database has
1d51b 6e 6f 74 20 67 69 76 65 6e 20 75 73 20 61 6e 20 not given us an
1d51c 6f 76 65 72 73 69 7a 65 20 68 65 61 64 65 72 2e oversize header.
1d51d 0a 20 20 20 20 2a 2a 20 44 6f 20 74 68 69 73 20 . ** Do this
1d51e 6e 6f 77 20 74 6f 20 61 76 6f 69 64 20 61 6e 20 now to avoid an
1d51f 6f 76 65 72 73 69 7a 65 20 6d 65 6d 6f 72 79 20 oversize memory
1d520 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 20 20 20 20 allocation..
1d521 2a 2a 0a 20 20 20 20 2a 2a 20 54 79 70 65 20 65 **. ** Type e
1d522 6e 74 72 69 65 73 20 63 61 6e 20 62 65 20 62 65 ntries can be be
1d523 74 77 65 65 6e 20 31 20 61 6e 64 20 35 20 62 79 tween 1 and 5 by
1d524 74 65 73 20 65 61 63 68 2e 20 20 42 75 74 20 34 tes each. But 4
1d525 20 61 6e 64 20 35 20 62 79 74 65 0a 20 20 20 20 and 5 byte.
1d526 2a 2a 20 74 79 70 65 73 20 75 73 65 20 73 6f 20 ** types use so
1d527 6d 75 63 68 20 64 61 74 61 20 73 70 61 63 65 20 much data space
1d528 74 68 61 74 20 74 68 65 72 65 20 63 61 6e 20 6f that there can o
1d529 6e 6c 79 20 62 65 20 34 30 39 36 20 61 6e 64 20 nly be 4096 and
1d52a 33 32 20 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65 32 of. ** the
1d52b 6d 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e m, respectively.
1d52c 20 20 53 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d So the maximum
1d52d 20 68 65 61 64 65 72 20 6c 65 6e 67 74 68 20 72 header length r
1d52e 65 73 75 6c 74 73 20 66 72 6f 6d 20 61 0a 20 20 esults from a.
1d52f 20 20 2a 2a 20 33 2d 62 79 74 65 20 74 79 70 65 ** 3-byte type
1d530 20 66 6f 72 20 65 61 63 68 20 6f 66 20 74 68 65 for each of the
1d531 20 6d 61 78 69 6d 75 6d 20 6f 66 20 33 32 37 36 maximum of 3276
1d532 38 20 63 6f 6c 75 6d 6e 73 20 70 6c 75 73 20 74 8 columns plus t
1d533 68 72 65 65 0a 20 20 20 20 2a 2a 20 65 78 74 72 hree. ** extr
1d534 61 20 62 79 74 65 73 20 66 6f 72 20 74 68 65 20 a bytes for the
1d535 68 65 61 64 65 72 20 6c 65 6e 67 74 68 20 69 74 header length it
1d536 73 65 6c 66 2e 20 20 33 32 37 36 38 2a 33 20 2b self. 32768*3 +
1d537 20 33 20 3d 20 39 38 33 30 37 2e 0a 20 20 20 20 3 = 98307..
1d538 2a 2f 0a 20 20 20 20 69 66 28 20 75 2e 61 6d 2e */. if( u.am.
1d539 6f 66 66 73 65 74 20 3e 20 39 38 33 30 37 20 29 offset > 98307 )
1d53a 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c {. rc = SQL
1d53b 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
1d53c 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f ;. goto op_
1d53d 63 6f 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 column_out;.
1d53e 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 75 74 }.. /* Comput
1d53f 65 20 69 6e 20 75 2e 61 6d 2e 6c 65 6e 20 74 68 e in u.am.len th
1d540 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 e number of byte
1d541 73 20 6f 66 20 64 61 74 61 20 77 65 20 6e 65 65 s of data we nee
1d542 64 20 74 6f 20 72 65 61 64 20 69 6e 20 6f 72 64 d to read in ord
1d543 65 72 0a 20 20 20 20 2a 2a 20 74 6f 20 67 65 74 er. ** to get
1d544 20 75 2e 61 6d 2e 6e 46 69 65 6c 64 20 74 79 70 u.am.nField typ
1d545 65 20 76 61 6c 75 65 73 2e 20 20 75 2e 61 6d 2e e values. u.am.
1d546 6f 66 66 73 65 74 20 69 73 20 61 6e 20 75 70 70 offset is an upp
1d547 65 72 20 62 6f 75 6e 64 20 6f 6e 20 74 68 69 73 er bound on this
1d548 2e 20 20 42 75 74 0a 20 20 20 20 2a 2a 20 75 2e . But. ** u.
1d549 61 6d 2e 6e 46 69 65 6c 64 20 6d 69 67 68 74 20 am.nField might
1d54a 62 65 20 73 69 67 6e 69 66 69 63 61 6e 74 6c 79 be significantly
1d54b 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 74 less than the t
1d54c 72 75 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f rue number of co
1d54d 6c 75 6d 6e 73 0a 20 20 20 20 2a 2a 20 69 6e 20 lumns. ** in
1d54e 74 68 65 20 74 61 62 6c 65 2c 20 61 6e 64 20 69 the table, and i
1d54f 6e 20 74 68 61 74 20 63 61 73 65 2c 20 35 2a 75 n that case, 5*u
1d550 2e 61 6d 2e 6e 46 69 65 6c 64 2b 33 20 6d 69 67 .am.nField+3 mig
1d551 68 74 20 62 65 20 73 6d 61 6c 6c 65 72 20 74 68 ht be smaller th
1d552 61 6e 20 75 2e 61 6d 2e 6f 66 66 73 65 74 2e 0a an u.am.offset..
1d553 20 20 20 20 2a 2a 20 57 65 20 77 61 6e 74 20 74 ** We want t
1d554 6f 20 6d 69 6e 69 6d 69 7a 65 20 75 2e 61 6d 2e o minimize u.am.
1d555 6c 65 6e 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 len in order to
1d556 6c 69 6d 69 74 20 74 68 65 20 73 69 7a 65 20 6f limit the size o
1d557 66 20 74 68 65 20 6d 65 6d 6f 72 79 0a 20 20 20 f the memory.
1d558 20 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 ** allocation,
1d559 65 73 70 65 63 69 61 6c 6c 79 20 69 66 20 61 20 especially if a
1d55a 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65 corrupt database
1d55b 20 66 69 6c 65 20 68 61 73 20 63 61 75 73 65 64 file has caused
1d55c 20 75 2e 61 6d 2e 6f 66 66 73 65 74 0a 20 20 20 u.am.offset.
1d55d 20 2a 2a 20 74 6f 20 62 65 20 6f 76 65 72 73 69 ** to be oversi
1d55e 7a 65 64 2e 20 4f 66 66 73 65 74 20 69 73 20 6c zed. Offset is l
1d55f 69 6d 69 74 65 64 20 74 6f 20 39 38 33 30 37 20 imited to 98307
1d560 61 62 6f 76 65 2e 20 20 42 75 74 20 39 38 33 30 above. But 9830
1d561 37 20 6d 69 67 68 74 0a 20 20 20 20 2a 2a 20 73 7 might. ** s
1d562 74 69 6c 6c 20 65 78 63 65 65 64 20 52 6f 62 73 till exceed Robs
1d563 6f 6e 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 on memory alloca
1d564 74 69 6f 6e 20 6c 69 6d 69 74 73 20 6f 6e 20 73 tion limits on s
1d565 6f 6d 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f ome configuratio
1d566 6e 73 2e 0a 20 20 20 20 2a 2a 20 4f 6e 20 73 79 ns.. ** On sy
1d567 73 74 65 6d 73 20 74 68 61 74 20 63 61 6e 6e 6f stems that canno
1d568 74 20 74 6f 6c 65 72 61 74 65 20 6c 61 72 67 65 t tolerate large
1d569 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
1d56a 6f 6e 73 2c 20 75 2e 61 6d 2e 6e 46 69 65 6c 64 ons, u.am.nField
1d56b 2a 35 2b 33 0a 20 20 20 20 2a 2a 20 77 69 6c 6c *5+3. ** will
1d56c 20 6c 69 6b 65 6c 79 20 62 65 20 6d 75 63 68 20 likely be much
1d56d 73 6d 61 6c 6c 65 72 20 73 69 6e 63 65 20 75 2e smaller since u.
1d56e 61 6d 2e 6e 46 69 65 6c 64 20 77 69 6c 6c 20 6c am.nField will l
1d56f 69 6b 65 6c 79 20 62 65 20 6c 65 73 73 20 74 68 ikely be less th
1d570 61 6e 0a 20 20 20 20 2a 2a 20 32 30 20 6f 72 20 an. ** 20 or
1d571 73 6f 2e 20 20 54 68 69 73 20 69 6e 73 75 72 65 so. This insure
1d572 73 20 74 68 61 74 20 52 6f 62 73 6f 6e 20 6d 65 s that Robson me
1d573 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
1d574 6c 69 6d 69 74 73 20 61 72 65 0a 20 20 20 20 2a limits are. *
1d575 2a 20 6e 6f 74 20 65 78 63 65 65 64 65 64 20 65 * not exceeded e
1d576 76 65 6e 20 66 6f 72 20 63 6f 72 72 75 70 74 20 ven for corrupt
1d577 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 2e 0a database files..
1d578 20 20 20 20 2a 2f 0a 20 20 20 20 75 2e 61 6d 2e */. u.am.
1d579 6c 65 6e 20 3d 20 75 2e 61 6d 2e 6e 46 69 65 6c len = u.am.nFiel
1d57a 64 2a 35 20 2b 20 33 3b 0a 20 20 20 20 69 66 28 d*5 + 3;. if(
1d57b 20 75 2e 61 6d 2e 6c 65 6e 20 3e 20 28 69 6e 74 u.am.len > (int
1d57c 29 75 2e 61 6d 2e 6f 66 66 73 65 74 20 29 20 75 )u.am.offset ) u
1d57d 2e 61 6d 2e 6c 65 6e 20 3d 20 28 69 6e 74 29 75 .am.len = (int)u
1d57e 2e 61 6d 2e 6f 66 66 73 65 74 3b 0a 0a 20 20 20 .am.offset;..
1d57f 20 2f 2a 20 54 68 65 20 4b 65 79 46 65 74 63 68 /* The KeyFetch
1d580 28 29 20 6f 72 20 44 61 74 61 46 65 74 63 68 28 () or DataFetch(
1d581 29 20 61 62 6f 76 65 20 61 72 65 20 66 61 73 74 ) above are fast
1d582 20 61 6e 64 20 77 69 6c 6c 20 67 65 74 20 74 68 and will get th
1d583 65 20 65 6e 74 69 72 65 0a 20 20 20 20 2a 2a 20 e entire. **
1d584 72 65 63 6f 72 64 20 68 65 61 64 65 72 20 69 6e record header in
1d585 20 6d 6f 73 74 20 63 61 73 65 73 2e 20 20 42 75 most cases. Bu
1d586 74 20 74 68 65 79 20 77 69 6c 6c 20 66 61 69 6c t they will fail
1d587 20 74 6f 20 67 65 74 20 74 68 65 20 63 6f 6d 70 to get the comp
1d588 6c 65 74 65 0a 20 20 20 20 2a 2a 20 72 65 63 6f lete. ** reco
1d589 72 64 20 68 65 61 64 65 72 20 69 66 20 74 68 65 rd header if the
1d58a 20 72 65 63 6f 72 64 20 68 65 61 64 65 72 20 64 record header d
1d58b 6f 65 73 20 6e 6f 74 20 66 69 74 20 6f 6e 20 61 oes not fit on a
1d58c 20 73 69 6e 67 6c 65 20 70 61 67 65 0a 20 20 20 single page.
1d58d 20 2a 2a 20 69 6e 20 74 68 65 20 42 2d 54 72 65 ** in the B-Tre
1d58e 65 2e 20 20 57 68 65 6e 20 74 68 61 74 20 68 61 e. When that ha
1d58f 70 70 65 6e 73 2c 20 75 73 65 20 73 71 6c 69 74 ppens, use sqlit
1d590 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 e3VdbeMemFromBtr
1d591 65 65 28 29 20 74 6f 0a 20 20 20 20 2a 2a 20 61 ee() to. ** a
1d592 63 71 75 69 72 65 20 74 68 65 20 63 6f 6d 70 6c cquire the compl
1d593 65 74 65 20 68 65 61 64 65 72 20 74 65 78 74 2e ete header text.
1d594 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
1d595 21 75 2e 61 6d 2e 7a 52 65 63 20 26 26 20 75 2e !u.am.zRec && u.
1d596 61 6d 2e 61 76 61 69 6c 3c 75 2e 61 6d 2e 6c 65 am.avail<u.am.le
1d597 6e 20 29 7b 0a 20 20 20 20 20 20 75 2e 61 6d 2e n ){. u.am.
1d598 73 4d 65 6d 2e 66 6c 61 67 73 20 3d 20 30 3b 0a sMem.flags = 0;.
1d599 20 20 20 20 20 20 75 2e 61 6d 2e 73 4d 65 6d 2e u.am.sMem.
1d59a 64 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 db = 0;. rc
1d59b 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 = sqlite3VdbeMe
1d59c 6d 46 72 6f 6d 42 74 72 65 65 28 75 2e 61 6d 2e mFromBtree(u.am.
1d59d 70 43 72 73 72 2c 20 30 2c 20 75 2e 61 6d 2e 6c pCrsr, 0, u.am.l
1d59e 65 6e 2c 20 75 2e 61 6d 2e 70 43 2d 3e 69 73 49 en, u.am.pC->isI
1d59f 6e 64 65 78 2c 20 26 75 2e 61 6d 2e 73 4d 65 6d ndex, &u.am.sMem
1d5a0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 );. if( rc!
1d5a1 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1d5a2 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f goto op_co
1d5a3 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 lumn_out;.
1d5a4 7d 0a 20 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 }. u.am.zDa
1d5a5 74 61 20 3d 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a ta = u.am.sMem.z
1d5a6 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75 2e 61 6d ;. }. u.am
1d5a7 2e 7a 45 6e 64 48 64 72 20 3d 20 28 75 38 20 2a .zEndHdr = (u8 *
1d5a8 29 26 75 2e 61 6d 2e 7a 44 61 74 61 5b 75 2e 61 )&u.am.zData[u.a
1d5a9 6d 2e 6c 65 6e 5d 3b 0a 20 20 20 20 75 2e 61 6d m.len];. u.am
1d5aa 2e 7a 49 64 78 20 3d 20 28 75 38 20 2a 29 26 75 .zIdx = (u8 *)&u
1d5ab 2e 61 6d 2e 7a 44 61 74 61 5b 75 2e 61 6d 2e 73 .am.zData[u.am.s
1d5ac 7a 48 64 72 5d 3b 0a 0a 20 20 20 20 2f 2a 20 53 zHdr];.. /* S
1d5ad 63 61 6e 20 74 68 65 20 68 65 61 64 65 72 20 61 can the header a
1d5ae 6e 64 20 75 73 65 20 69 74 20 74 6f 20 66 69 6c nd use it to fil
1d5af 6c 20 69 6e 20 74 68 65 20 75 2e 61 6d 2e 61 54 l in the u.am.aT
1d5b0 79 70 65 5b 5d 20 61 6e 64 20 75 2e 61 6d 2e 61 ype[] and u.am.a
1d5b1 4f 66 66 73 65 74 5b 5d 0a 20 20 20 20 2a 2a 20 Offset[]. **
1d5b2 61 72 72 61 79 73 2e 20 20 75 2e 61 6d 2e 61 54 arrays. u.am.aT
1d5b3 79 70 65 5b 75 2e 61 6d 2e 69 5d 20 77 69 6c 6c ype[u.am.i] will
1d5b4 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 74 79 70 contain the typ
1d5b5 65 20 69 6e 74 65 67 65 72 20 66 6f 72 20 74 68 e integer for th
1d5b6 65 20 75 2e 61 6d 2e 69 2d 74 68 0a 20 20 20 20 e u.am.i-th.
1d5b7 2a 2a 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 75 2e ** column and u.
1d5b8 61 6d 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e am.aOffset[u.am.
1d5b9 69 5d 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 i] will contain
1d5ba 74 68 65 20 75 2e 61 6d 2e 6f 66 66 73 65 74 20 the u.am.offset
1d5bb 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 from the beginni
1d5bc 6e 67 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 ng. ** of the
1d5bd 20 72 65 63 6f 72 64 20 74 6f 20 74 68 65 20 73 record to the s
1d5be 74 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61 tart of the data
1d5bf 20 66 6f 72 20 74 68 65 20 75 2e 61 6d 2e 69 2d for the u.am.i-
1d5c0 74 68 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2f th column. */
1d5c1 0a 20 20 20 20 75 2e 61 6d 2e 6f 66 66 73 65 74 . u.am.offset
1d5c2 36 34 20 3d 20 75 2e 61 6d 2e 6f 66 66 73 65 74 64 = u.am.offset
1d5c3 3b 0a 20 20 20 20 66 6f 72 28 75 2e 61 6d 2e 69 ;. for(u.am.i
1d5c4 3d 30 3b 20 75 2e 61 6d 2e 69 3c 75 2e 61 6d 2e =0; u.am.i<u.am.
1d5c5 6e 46 69 65 6c 64 3b 20 75 2e 61 6d 2e 69 2b 2b nField; u.am.i++
1d5c6 29 7b 0a 20 20 20 20 20 20 69 66 28 20 75 2e 61 ){. if( u.a
1d5c7 6d 2e 7a 49 64 78 3c 75 2e 61 6d 2e 7a 45 6e 64 m.zIdx<u.am.zEnd
1d5c8 48 64 72 20 29 7b 0a 20 20 20 20 20 20 20 20 75 Hdr ){. u
1d5c9 2e 61 6d 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d .am.aOffset[u.am
1d5ca 2e 69 5d 20 3d 20 28 75 33 32 29 75 2e 61 6d 2e .i] = (u32)u.am.
1d5cb 6f 66 66 73 65 74 36 34 3b 0a 20 20 20 20 20 20 offset64;.
1d5cc 20 20 75 2e 61 6d 2e 7a 49 64 78 20 2b 3d 20 67 u.am.zIdx += g
1d5cd 65 74 56 61 72 69 6e 74 33 32 28 75 2e 61 6d 2e etVarint32(u.am.
1d5ce 7a 49 64 78 2c 20 75 2e 61 6d 2e 61 54 79 70 65 zIdx, u.am.aType
1d5cf 5b 75 2e 61 6d 2e 69 5d 29 3b 0a 20 20 20 20 20 [u.am.i]);.
1d5d0 20 20 20 75 2e 61 6d 2e 6f 66 66 73 65 74 36 34 u.am.offset64
1d5d1 20 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 += sqlite3VdbeS
1d5d2 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 75 2e 61 erialTypeLen(u.a
1d5d3 6d 2e 61 54 79 70 65 5b 75 2e 61 6d 2e 69 5d 29 m.aType[u.am.i])
1d5d4 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
1d5d5 20 20 20 20 20 20 20 2f 2a 20 49 66 20 75 2e 61 /* If u.a
1d5d6 6d 2e 69 20 69 73 20 6c 65 73 73 20 74 68 61 74 m.i is less that
1d5d7 20 75 2e 61 6d 2e 6e 46 69 65 6c 64 2c 20 74 68 u.am.nField, th
1d5d8 65 6e 20 74 68 65 72 65 20 61 72 65 20 6c 65 73 en there are les
1d5d9 73 20 66 69 65 6c 64 73 20 69 6e 20 74 68 69 73 s fields in this
1d5da 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 65 63 6f . ** reco
1d5db 72 64 20 74 68 61 6e 20 53 65 74 4e 75 6d 43 6f rd than SetNumCo
1d5dc 6c 75 6d 6e 73 20 69 6e 64 69 63 61 74 65 64 20 lumns indicated
1d5dd 74 68 65 72 65 20 61 72 65 20 63 6f 6c 75 6d 6e there are column
1d5de 73 20 69 6e 20 74 68 65 0a 20 20 20 20 20 20 20 s in the.
1d5df 20 2a 2a 20 74 61 62 6c 65 2e 20 53 65 74 20 74 ** table. Set t
1d5e0 68 65 20 75 2e 61 6d 2e 6f 66 66 73 65 74 20 66 he u.am.offset f
1d5e1 6f 72 20 61 6e 79 20 65 78 74 72 61 20 63 6f 6c or any extra col
1d5e2 75 6d 6e 73 20 6e 6f 74 20 70 72 65 73 65 6e 74 umns not present
1d5e3 20 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 in. ** t
1d5e4 68 65 20 72 65 63 6f 72 64 20 74 6f 20 30 2e 20 he record to 0.
1d5e5 54 68 69 73 20 74 65 6c 6c 73 20 63 6f 64 65 20 This tells code
1d5e6 62 65 6c 6f 77 20 74 6f 20 73 74 6f 72 65 20 61 below to store a
1d5e7 20 4e 55 4c 4c 0a 20 20 20 20 20 20 20 20 2a 2a NULL. **
1d5e8 20 69 6e 73 74 65 61 64 20 6f 66 20 64 65 73 65 instead of dese
1d5e9 72 69 61 6c 69 7a 69 6e 67 20 61 20 76 61 6c 75 rializing a valu
1d5ea 65 20 66 72 6f 6d 20 74 68 65 20 72 65 63 6f 72 e from the recor
1d5eb 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 d.. */.
1d5ec 20 20 20 20 20 20 75 2e 61 6d 2e 61 4f 66 66 73 u.am.aOffs
1d5ed 65 74 5b 75 2e 61 6d 2e 69 5d 20 3d 20 30 3b 0a et[u.am.i] = 0;.
1d5ee 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1d5ef 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1d5f0 52 65 6c 65 61 73 65 28 26 75 2e 61 6d 2e 73 4d Release(&u.am.sM
1d5f1 65 6d 29 3b 0a 20 20 20 20 75 2e 61 6d 2e 73 4d em);. u.am.sM
1d5f2 65 6d 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e em.flags = MEM_N
1d5f3 75 6c 6c 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 ull;.. /* If
1d5f4 77 65 20 68 61 76 65 20 72 65 61 64 20 6d 6f 72 we have read mor
1d5f5 65 20 68 65 61 64 65 72 20 64 61 74 61 20 74 68 e header data th
1d5f6 61 6e 20 77 61 73 20 63 6f 6e 74 61 69 6e 65 64 an was contained
1d5f7 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 2c 0a in the header,.
1d5f8 20 20 20 20 2a 2a 20 6f 72 20 69 66 20 74 68 65 ** or if the
1d5f9 20 65 6e 64 20 6f 66 20 74 68 65 20 6c 61 73 74 end of the last
1d5fa 20 66 69 65 6c 64 20 61 70 70 65 61 72 73 20 74 field appears t
1d5fb 6f 20 62 65 20 70 61 73 74 20 74 68 65 20 65 6e o be past the en
1d5fc 64 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 d of the. **
1d5fd 72 65 63 6f 72 64 2c 20 6f 72 20 69 66 20 74 68 record, or if th
1d5fe 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6c 61 73 e end of the las
1d5ff 74 20 66 69 65 6c 64 20 61 70 70 65 61 72 73 20 t field appears
1d600 74 6f 20 62 65 20 62 65 66 6f 72 65 20 74 68 65 to be before the
1d601 20 65 6e 64 0a 20 20 20 20 2a 2a 20 6f 66 20 74 end. ** of t
1d602 68 65 20 72 65 63 6f 72 64 20 28 77 68 65 6e 20 he record (when
1d603 61 6c 6c 20 66 69 65 6c 64 73 20 70 72 65 73 65 all fields prese
1d604 6e 74 29 2c 20 74 68 65 6e 20 77 65 20 6d 75 73 nt), then we mus
1d605 74 20 62 65 20 64 65 61 6c 69 6e 67 0a 20 20 20 t be dealing.
1d606 20 2a 2a 20 77 69 74 68 20 61 20 63 6f 72 72 75 ** with a corru
1d607 70 74 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20 pt database..
1d608 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 75 2e 61 */. if( (u.a
1d609 6d 2e 7a 49 64 78 20 3e 20 75 2e 61 6d 2e 7a 45 m.zIdx > u.am.zE
1d60a 6e 64 48 64 72 29 7c 7c 20 28 75 2e 61 6d 2e 6f ndHdr)|| (u.am.o
1d60b 66 66 73 65 74 36 34 20 3e 20 75 2e 61 6d 2e 70 ffset64 > u.am.p
1d60c 61 79 6c 6f 61 64 53 69 7a 65 29 0a 20 20 20 20 ayloadSize).
1d60d 20 7c 7c 20 28 75 2e 61 6d 2e 7a 49 64 78 3d 3d || (u.am.zIdx==
1d60e 75 2e 61 6d 2e 7a 45 6e 64 48 64 72 20 26 26 20 u.am.zEndHdr &&
1d60f 75 2e 61 6d 2e 6f 66 66 73 65 74 36 34 21 3d 28 u.am.offset64!=(
1d610 75 36 34 29 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 u64)u.am.payload
1d611 53 69 7a 65 29 20 29 7b 0a 20 20 20 20 20 20 72 Size) ){. r
1d612 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 c = SQLITE_CORRU
1d613 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 PT_BKPT;. g
1d614 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 oto op_column_ou
1d615 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 t;. }. }..
1d616 2f 2a 20 47 65 74 20 74 68 65 20 63 6f 6c 75 6d /* Get the colum
1d617 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 49 n information. I
1d618 66 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 5b 75 f u.am.aOffset[u
1d619 2e 61 6d 2e 70 32 5d 20 69 73 20 6e 6f 6e 2d 7a .am.p2] is non-z
1d61a 65 72 6f 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 64 ero, then. ** d
1d61b 65 73 65 72 69 61 6c 69 7a 65 20 74 68 65 20 76 eserialize the v
1d61c 61 6c 75 65 20 66 72 6f 6d 20 74 68 65 20 72 65 alue from the re
1d61d 63 6f 72 64 2e 20 49 66 20 75 2e 61 6d 2e 61 4f cord. If u.am.aO
1d61e 66 66 73 65 74 5b 75 2e 61 6d 2e 70 32 5d 20 69 ffset[u.am.p2] i
1d61f 73 20 7a 65 72 6f 2c 0a 20 20 2a 2a 20 74 68 65 s zero,. ** the
1d620 6e 20 74 68 65 72 65 20 61 72 65 20 6e 6f 74 20 n there are not
1d621 65 6e 6f 75 67 68 20 66 69 65 6c 64 73 20 69 6e enough fields in
1d622 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 73 the record to s
1d623 61 74 69 73 66 79 20 74 68 65 0a 20 20 2a 2a 20 atisfy the. **
1d624 72 65 71 75 65 73 74 2e 20 20 49 6e 20 74 68 69 request. In thi
1d625 73 20 63 61 73 65 2c 20 73 65 74 20 74 68 65 20 s case, set the
1d626 76 61 6c 75 65 20 4e 55 4c 4c 20 6f 72 20 74 6f value NULL or to
1d627 20 50 34 20 69 66 20 50 34 20 69 73 0a 20 20 2a P4 if P4 is. *
1d628 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 * a pointer to a
1d629 20 4d 65 6d 20 6f 62 6a 65 63 74 2e 0a 20 20 2a Mem object.. *
1d62a 2f 0a 20 20 69 66 28 20 75 2e 61 6d 2e 61 4f 66 /. if( u.am.aOf
1d62b 66 73 65 74 5b 75 2e 61 6d 2e 70 32 5d 20 29 7b fset[u.am.p2] ){
1d62c 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d . assert( rc=
1d62d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 =SQLITE_OK );.
1d62e 20 20 69 66 28 20 75 2e 61 6d 2e 7a 52 65 63 20 if( u.am.zRec
1d62f 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1d630 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 VdbeMemReleaseEx
1d631 74 65 72 6e 61 6c 28 75 2e 61 6d 2e 70 44 65 73 ternal(u.am.pDes
1d632 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 t);. sqlite
1d633 33 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 28 3VdbeSerialGet((
1d634 75 38 20 2a 29 26 75 2e 61 6d 2e 7a 52 65 63 5b u8 *)&u.am.zRec[
1d635 75 2e 61 6d 2e 61 4f 66 66 73 65 74 5b 75 2e 61 u.am.aOffset[u.a
1d636 6d 2e 70 32 5d 5d 2c 20 75 2e 61 6d 2e 61 54 79 m.p2]], u.am.aTy
1d637 70 65 5b 75 2e 61 6d 2e 70 32 5d 2c 20 75 2e 61 pe[u.am.p2], u.a
1d638 6d 2e 70 44 65 73 74 29 3b 0a 20 20 20 20 7d 65 m.pDest);. }e
1d639 6c 73 65 7b 0a 20 20 20 20 20 20 75 2e 61 6d 2e lse{. u.am.
1d63a 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 56 64 62 len = sqlite3Vdb
1d63b 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 75 eSerialTypeLen(u
1d63c 2e 61 6d 2e 61 54 79 70 65 5b 75 2e 61 6d 2e 70 .am.aType[u.am.p
1d63d 32 5d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 2]);. sqlit
1d63e 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 26 75 e3VdbeMemMove(&u
1d63f 2e 61 6d 2e 73 4d 65 6d 2c 20 75 2e 61 6d 2e 70 .am.sMem, u.am.p
1d640 44 65 73 74 29 3b 0a 20 20 20 20 20 20 72 63 20 Dest);. rc
1d641 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d = sqlite3VdbeMem
1d642 46 72 6f 6d 42 74 72 65 65 28 75 2e 61 6d 2e 70 FromBtree(u.am.p
1d643 43 72 73 72 2c 20 75 2e 61 6d 2e 61 4f 66 66 73 Crsr, u.am.aOffs
1d644 65 74 5b 75 2e 61 6d 2e 70 32 5d 2c 20 75 2e 61 et[u.am.p2], u.a
1d645 6d 2e 6c 65 6e 2c 20 75 2e 61 6d 2e 70 43 2d 3e m.len, u.am.pC->
1d646 69 73 49 6e 64 65 78 2c 20 26 75 2e 61 6d 2e 73 isIndex, &u.am.s
1d647 4d 65 6d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 Mem);. if(
1d648 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1d649 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 . goto op
1d64a 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 _column_out;.
1d64b 20 20 20 7d 0a 20 20 20 20 20 20 75 2e 61 6d 2e }. u.am.
1d64c 7a 44 61 74 61 20 3d 20 75 2e 61 6d 2e 73 4d 65 zData = u.am.sMe
1d64d 6d 2e 7a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 m.z;. sqlit
1d64e 65 33 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 e3VdbeSerialGet(
1d64f 28 75 38 2a 29 75 2e 61 6d 2e 7a 44 61 74 61 2c (u8*)u.am.zData,
1d650 20 75 2e 61 6d 2e 61 54 79 70 65 5b 75 2e 61 6d u.am.aType[u.am
1d651 2e 70 32 5d 2c 20 75 2e 61 6d 2e 70 44 65 73 74 .p2], u.am.pDest
1d652 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75 2e 61 );. }. u.a
1d653 6d 2e 70 44 65 73 74 2d 3e 65 6e 63 20 3d 20 65 m.pDest->enc = e
1d654 6e 63 6f 64 69 6e 67 3b 0a 20 20 7d 65 6c 73 65 ncoding;. }else
1d655 7b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 {. if( pOp->p
1d656 34 74 79 70 65 3d 3d 50 34 5f 4d 45 4d 20 29 7b 4type==P4_MEM ){
1d657 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1d658 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 beMemShallowCopy
1d659 28 75 2e 61 6d 2e 70 44 65 73 74 2c 20 70 4f 70 (u.am.pDest, pOp
1d65a 2d 3e 70 34 2e 70 4d 65 6d 2c 20 4d 45 4d 5f 53 ->p4.pMem, MEM_S
1d65b 74 61 74 69 63 29 3b 0a 20 20 20 20 7d 65 6c 73 tatic);. }els
1d65c 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 e{. assert(
1d65d 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 u.am.pDest->fla
1d65e 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 20 gs&MEM_Null );.
1d65f 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 }. }.. /* I
1d660 66 20 77 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 f we dynamically
1d661 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 allocated space
1d662 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 64 61 74 to hold the dat
1d663 61 20 28 69 6e 20 74 68 65 0a 20 20 2a 2a 20 73 a (in the. ** s
1d664 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f qlite3VdbeMemFro
1d665 6d 42 74 72 65 65 28 29 20 63 61 6c 6c 20 61 62 mBtree() call ab
1d666 6f 76 65 29 20 74 68 65 6e 20 74 72 61 6e 73 66 ove) then transf
1d667 65 72 20 63 6f 6e 74 72 6f 6c 20 6f 66 20 74 68 er control of th
1d668 61 74 0a 20 20 2a 2a 20 64 79 6e 61 6d 69 63 61 at. ** dynamica
1d669 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 lly allocated sp
1d66a 61 63 65 20 6f 76 65 72 20 74 6f 20 74 68 65 20 ace over to the
1d66b 75 2e 61 6d 2e 70 44 65 73 74 20 73 74 72 75 63 u.am.pDest struc
1d66c 74 75 72 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 ture.. ** This
1d66d 70 72 65 76 65 6e 74 73 20 61 20 6d 65 6d 6f 72 prevents a memor
1d66e 79 20 63 6f 70 79 2e 0a 20 20 2a 2f 0a 20 20 69 y copy.. */. i
1d66f 66 28 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 4d 61 f( u.am.sMem.zMa
1d670 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 61 73 73 65 lloc ){. asse
1d671 72 74 28 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 3d rt( u.am.sMem.z=
1d672 3d 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 4d 61 6c 6c =u.am.sMem.zMall
1d673 6f 63 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 oc );. assert
1d674 28 20 21 28 75 2e 61 6d 2e 70 44 65 73 74 2d 3e ( !(u.am.pDest->
1d675 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 flags & MEM_Dyn)
1d676 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
1d677 21 28 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 66 6c !(u.am.pDest->fl
1d678 61 67 73 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c ags & (MEM_Blob|
1d679 4d 45 4d 5f 53 74 72 29 29 20 7c 7c 20 75 2e 61 MEM_Str)) || u.a
1d67a 6d 2e 70 44 65 73 74 2d 3e 7a 3d 3d 75 2e 61 6d m.pDest->z==u.am
1d67b 2e 73 4d 65 6d 2e 7a 20 29 3b 0a 20 20 20 20 75 .sMem.z );. u
1d67c 2e 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73 .am.pDest->flags
1d67d 20 26 3d 20 7e 28 4d 45 4d 5f 45 70 68 65 6d 7c &= ~(MEM_Ephem|
1d67e 4d 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20 20 MEM_Static);.
1d67f 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 u.am.pDest->fla
1d680 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a gs |= MEM_Term;.
1d681 20 20 20 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e u.am.pDest->
1d682 7a 20 3d 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 3b z = u.am.sMem.z;
1d683 0a 20 20 20 20 75 2e 61 6d 2e 70 44 65 73 74 2d . u.am.pDest-
1d684 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 75 2e 61 6d 2e >zMalloc = u.am.
1d685 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 sMem.zMalloc;.
1d686 7d 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 }.. rc = sqlite
1d687 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 3VdbeMemMakeWrit
1d688 65 61 62 6c 65 28 75 2e 61 6d 2e 70 44 65 73 74 eable(u.am.pDest
1d689 29 3b 0a 0a 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 );..op_column_ou
1d68a 74 3a 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f t:. UPDATE_MAX_
1d68b 42 4c 4f 42 53 49 5a 45 28 75 2e 61 6d 2e 70 44 BLOBSIZE(u.am.pD
1d68c 65 73 74 29 3b 0a 20 20 52 45 47 49 53 54 45 52 est);. REGISTER
1d68d 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 _TRACE(pOp->p3,
1d68e 75 2e 61 6d 2e 70 44 65 73 74 29 3b 0a 20 20 62 u.am.pDest);. b
1d68f 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1d690 64 65 3a 20 41 66 66 69 6e 69 74 79 20 50 31 20 de: Affinity P1
1d691 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * P4 *.**.**
1d692 41 70 70 6c 79 20 61 66 66 69 6e 69 74 69 65 73 Apply affinities
1d693 20 74 6f 20 61 20 72 61 6e 67 65 20 6f 66 20 50 to a range of P
1d694 32 20 72 65 67 69 73 74 65 72 73 20 73 74 61 72 2 registers star
1d695 74 69 6e 67 20 77 69 74 68 20 50 31 2e 0a 2a 2a ting with P1..**
1d696 0a 2a 2a 20 50 34 20 69 73 20 61 20 73 74 72 69 .** P4 is a stri
1d697 6e 67 20 74 68 61 74 20 69 73 20 50 32 20 63 68 ng that is P2 ch
1d698 61 72 61 63 74 65 72 73 20 6c 6f 6e 67 2e 20 54 aracters long. T
1d699 68 65 20 6e 74 68 20 63 68 61 72 61 63 74 65 72 he nth character
1d69a 20 6f 66 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e of the.** strin
1d69b 67 20 69 6e 64 69 63 61 74 65 73 20 74 68 65 20 g indicates the
1d69c 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 column affinity
1d69d 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 75 that should be u
1d69e 73 65 64 20 66 6f 72 20 74 68 65 20 6e 74 68 0a sed for the nth.
1d69f 2a 2a 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 ** memory cell i
1d6a0 6e 20 74 68 65 20 72 61 6e 67 65 2e 0a 2a 2f 0a n the range..*/.
1d6a1 63 61 73 65 20 4f 50 5f 41 66 66 69 6e 69 74 79 case OP_Affinity
1d6a2 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f : {.#if 0 /* lo
1d6a3 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1d6a4 76 65 64 20 69 6e 74 6f 20 75 2e 61 6e 20 2a 2f ved into u.an */
1d6a5 0a 20 20 63 68 61 72 20 2a 7a 41 66 66 69 6e 69 . char *zAffini
1d6a6 74 79 3b 20 20 20 2f 2a 20 54 68 65 20 61 66 66 ty; /* The aff
1d6a7 69 6e 69 74 79 20 74 6f 20 62 65 20 61 70 70 6c inity to be appl
1d6a8 69 65 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 44 ied */. Mem *pD
1d6a9 61 74 61 30 3b 20 20 20 20 20 20 20 2f 2a 20 46 ata0; /* F
1d6aa 69 72 73 74 20 72 65 67 69 73 74 65 72 20 74 6f irst register to
1d6ab 20 77 68 69 63 68 20 74 6f 20 61 70 70 6c 79 20 which to apply
1d6ac 61 66 66 69 6e 69 74 79 20 2a 2f 0a 20 20 4d 65 affinity */. Me
1d6ad 6d 20 2a 70 4c 61 73 74 3b 20 20 20 20 20 20 20 m *pLast;
1d6ae 20 2f 2a 20 4c 61 73 74 20 72 65 67 69 73 74 65 /* Last registe
1d6af 72 20 74 6f 20 77 68 69 63 68 20 74 6f 20 61 70 r to which to ap
1d6b0 70 6c 79 20 61 66 66 69 6e 69 74 79 20 2a 2f 0a ply affinity */.
1d6b1 20 20 4d 65 6d 20 2a 70 52 65 63 3b 20 20 20 20 Mem *pRec;
1d6b2 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 /* Current
1d6b3 72 65 67 69 73 74 65 72 20 2a 2f 0a 23 65 6e 64 register */.#end
1d6b4 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1d6b5 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1d6b6 20 75 2e 61 6e 20 2a 2f 0a 0a 20 20 75 2e 61 6e u.an */.. u.an
1d6b7 2e 7a 41 66 66 69 6e 69 74 79 20 3d 20 70 4f 70 .zAffinity = pOp
1d6b8 2d 3e 70 34 2e 7a 3b 0a 20 20 75 2e 61 6e 2e 70 ->p4.z;. u.an.p
1d6b9 44 61 74 61 30 20 3d 20 26 70 2d 3e 61 4d 65 6d Data0 = &p->aMem
1d6ba 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 75 2e 61 [pOp->p1];. u.a
1d6bb 6e 2e 70 4c 61 73 74 20 3d 20 26 75 2e 61 6e 2e n.pLast = &u.an.
1d6bc 70 44 61 74 61 30 5b 70 4f 70 2d 3e 70 32 2d 31 pData0[pOp->p2-1
1d6bd 5d 3b 0a 20 20 66 6f 72 28 75 2e 61 6e 2e 70 52 ];. for(u.an.pR
1d6be 65 63 3d 75 2e 61 6e 2e 70 44 61 74 61 30 3b 20 ec=u.an.pData0;
1d6bf 75 2e 61 6e 2e 70 52 65 63 3c 3d 75 2e 61 6e 2e u.an.pRec<=u.an.
1d6c0 70 4c 61 73 74 3b 20 75 2e 61 6e 2e 70 52 65 63 pLast; u.an.pRec
1d6c1 2b 2b 29 7b 0a 20 20 20 20 45 78 70 61 6e 64 42 ++){. ExpandB
1d6c2 6c 6f 62 28 75 2e 61 6e 2e 70 52 65 63 29 3b 0a lob(u.an.pRec);.
1d6c3 20 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 applyAffinit
1d6c4 79 28 75 2e 61 6e 2e 70 52 65 63 2c 20 75 2e 61 y(u.an.pRec, u.a
1d6c5 6e 2e 7a 41 66 66 69 6e 69 74 79 5b 75 2e 61 6e n.zAffinity[u.an
1d6c6 2e 70 52 65 63 2d 75 2e 61 6e 2e 70 44 61 74 61 .pRec-u.an.pData
1d6c7 30 5d 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 0], encoding);.
1d6c8 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f }. break;.}../
1d6c9 2a 20 4f 70 63 6f 64 65 3a 20 4d 61 6b 65 52 65 * Opcode: MakeRe
1d6ca 63 6f 72 64 20 50 31 20 50 32 20 50 33 20 50 34 cord P1 P2 P3 P4
1d6cb 20 2a 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 *.**.** Convert
1d6cc 20 50 32 20 72 65 67 69 73 74 65 72 73 20 62 65 P2 registers be
1d6cd 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 50 31 20 ginning with P1
1d6ce 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 65 6e into a single en
1d6cf 74 72 79 0a 2a 2a 20 73 75 69 74 61 62 6c 65 20 try.** suitable
1d6d0 66 6f 72 20 75 73 65 20 61 73 20 61 20 64 61 74 for use as a dat
1d6d1 61 20 72 65 63 6f 72 64 20 69 6e 20 61 20 64 61 a record in a da
1d6d2 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20 tabase table or
1d6d3 61 73 20 61 20 6b 65 79 0a 2a 2a 20 69 6e 20 61 as a key.** in a
1d6d4 6e 20 69 6e 64 65 78 2e 20 20 54 68 65 20 64 65 n index. The de
1d6d5 74 61 69 6c 73 20 6f 66 20 74 68 65 20 66 6f 72 tails of the for
1d6d6 6d 61 74 20 61 72 65 20 69 72 72 65 6c 65 76 61 mat are irreleva
1d6d7 6e 74 20 61 73 20 6c 6f 6e 67 20 61 73 0a 2a 2a nt as long as.**
1d6d8 20 74 68 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f the OP_Column o
1d6d9 70 63 6f 64 65 20 63 61 6e 20 64 65 63 6f 64 65 pcode can decode
1d6da 20 74 68 65 20 72 65 63 6f 72 64 20 6c 61 74 65 the record late
1d6db 72 2e 0a 2a 2a 20 52 65 66 65 72 20 74 6f 20 73 r..** Refer to s
1d6dc 6f 75 72 63 65 20 63 6f 64 65 20 63 6f 6d 6d 65 ource code comme
1d6dd 6e 74 73 20 66 6f 72 20 74 68 65 20 64 65 74 61 nts for the deta
1d6de 69 6c 73 20 6f 66 20 74 68 65 20 72 65 63 6f 72 ils of the recor
1d6df 64 0a 2a 2a 20 66 6f 72 6d 61 74 2e 0a 2a 2a 0a d.** format..**.
1d6e0 2a 2a 20 50 34 20 6d 61 79 20 62 65 20 61 20 73 ** P4 may be a s
1d6e1 74 72 69 6e 67 20 74 68 61 74 20 69 73 20 50 32 tring that is P2
1d6e2 20 63 68 61 72 61 63 74 65 72 73 20 6c 6f 6e 67 characters long
1d6e3 2e 20 20 54 68 65 20 6e 74 68 20 63 68 61 72 61 . The nth chara
1d6e4 63 74 65 72 20 6f 66 20 74 68 65 0a 2a 2a 20 73 cter of the.** s
1d6e5 74 72 69 6e 67 20 69 6e 64 69 63 61 74 65 73 20 tring indicates
1d6e6 74 68 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e the column affin
1d6e7 69 74 79 20 74 68 61 74 20 73 68 6f 75 6c 64 20 ity that should
1d6e8 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 be used for the
1d6e9 6e 74 68 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 20 nth.** field of
1d6ea 74 68 65 20 69 6e 64 65 78 20 6b 65 79 2e 0a 2a the index key..*
1d6eb 2a 0a 2a 2a 20 54 68 65 20 6d 61 70 70 69 6e 67 *.** The mapping
1d6ec 20 66 72 6f 6d 20 63 68 61 72 61 63 74 65 72 20 from character
1d6ed 74 6f 20 61 66 66 69 6e 69 74 79 20 69 73 20 67 to affinity is g
1d6ee 69 76 65 6e 20 62 79 20 74 68 65 20 53 51 4c 49 iven by the SQLI
1d6ef 54 45 5f 41 46 46 5f 0a 2a 2a 20 6d 61 63 72 6f TE_AFF_.** macro
1d6f0 73 20 64 65 66 69 6e 65 64 20 69 6e 20 73 71 6c s defined in sql
1d6f1 69 74 65 49 6e 74 2e 68 2e 0a 2a 2a 0a 2a 2a 20 iteInt.h..**.**
1d6f2 49 66 20 50 34 20 69 73 20 4e 55 4c 4c 20 74 68 If P4 is NULL th
1d6f3 65 6e 20 61 6c 6c 20 69 6e 64 65 78 20 66 69 65 en all index fie
1d6f4 6c 64 73 20 68 61 76 65 20 74 68 65 20 61 66 66 lds have the aff
1d6f5 69 6e 69 74 79 20 4e 4f 4e 45 2e 0a 2a 2f 0a 63 inity NONE..*/.c
1d6f6 61 73 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 ase OP_MakeRecor
1d6f7 64 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c d: {.#if 0 /* l
1d6f8 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1d6f9 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6f 20 2a oved into u.ao *
1d6fa 2f 0a 20 20 75 38 20 2a 7a 4e 65 77 52 65 63 6f /. u8 *zNewReco
1d6fb 72 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 20 rd; /* A
1d6fc 62 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 74 buffer to hold t
1d6fd 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 he data for the
1d6fe 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 new record */.
1d6ff 4d 65 6d 20 2a 70 52 65 63 3b 20 20 20 20 20 20 Mem *pRec;
1d700 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 /* The ne
1d701 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 36 w record */. u6
1d702 34 20 6e 44 61 74 61 3b 20 20 20 20 20 20 20 20 4 nData;
1d703 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1d704 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 f bytes of data
1d705 73 70 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e space */. int n
1d706 48 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 Hdr;
1d707 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
1d708 79 74 65 73 20 6f 66 20 68 65 61 64 65 72 20 73 ytes of header s
1d709 70 61 63 65 20 2a 2f 0a 20 20 69 36 34 20 6e 42 pace */. i64 nB
1d70a 79 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 yte;
1d70b 20 2f 2a 20 44 61 74 61 20 73 70 61 63 65 20 72 /* Data space r
1d70c 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 69 73 equired for this
1d70d 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 record */. int
1d70e 20 6e 5a 65 72 6f 3b 20 20 20 20 20 20 20 20 20 nZero;
1d70f 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1d710 20 7a 65 72 6f 20 62 79 74 65 73 20 61 74 20 74 zero bytes at t
1d711 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 72 65 he end of the re
1d712 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 cord */. int nV
1d713 61 72 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 arint;
1d714 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
1d715 74 65 73 20 69 6e 20 61 20 76 61 72 69 6e 74 20 tes in a varint
1d716 2a 2f 0a 20 20 75 33 32 20 73 65 72 69 61 6c 5f */. u32 serial_
1d717 74 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 type; /* T
1d718 79 70 65 20 66 69 65 6c 64 20 2a 2f 0a 20 20 4d ype field */. M
1d719 65 6d 20 2a 70 44 61 74 61 30 3b 20 20 20 20 20 em *pData0;
1d71a 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 66 /* First f
1d71b 69 65 6c 64 20 74 6f 20 62 65 20 63 6f 6d 62 69 ield to be combi
1d71c 6e 65 64 20 69 6e 74 6f 20 74 68 65 20 72 65 63 ned into the rec
1d71d 6f 72 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4c ord */. Mem *pL
1d71e 61 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 ast;
1d71f 2f 2a 20 4c 61 73 74 20 66 69 65 6c 64 20 6f 66 /* Last field of
1d720 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 the record */.
1d721 20 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 int nField;
1d722 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
1d723 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74 r of fields in t
1d724 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63 he record */. c
1d725 68 61 72 20 2a 7a 41 66 66 69 6e 69 74 79 3b 20 har *zAffinity;
1d726 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 66 66 /* The aff
1d727 69 6e 69 74 79 20 73 74 72 69 6e 67 20 66 6f 72 inity string for
1d728 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 the record */.
1d729 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72 6d 61 74 int file_format
1d72a 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 ; /* File
1d72b 66 6f 72 6d 61 74 20 74 6f 20 75 73 65 20 66 6f format to use fo
1d72c 72 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 r encoding */.
1d72d 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 int i;
1d72e 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 /* Space
1d72f 75 73 65 64 20 69 6e 20 7a 4e 65 77 52 65 63 6f used in zNewReco
1d730 72 64 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 rd[] */. int le
1d731 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n;
1d732 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 61 20 /* Length of a
1d733 66 69 65 6c 64 20 2a 2f 0a 23 65 6e 64 69 66 20 field */.#endif
1d734 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1d735 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1d736 61 6f 20 2a 2f 0a 0a 20 20 2f 2a 20 41 73 73 75 ao */.. /* Assu
1d737 6d 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64 20 ming the record
1d738 63 6f 6e 74 61 69 6e 73 20 4e 20 66 69 65 6c 64 contains N field
1d739 73 2c 20 74 68 65 20 72 65 63 6f 72 64 20 66 6f s, the record fo
1d73a 72 6d 61 74 20 6c 6f 6f 6b 73 0a 20 20 2a 2a 20 rmat looks. **
1d73b 6c 69 6b 65 20 74 68 69 73 3a 0a 20 20 2a 2a 0a like this:. **.
1d73c 20 20 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ** -----------
1d73d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d73e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d73f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d740 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 20 -------------.
1d741 2a 2a 20 7c 20 68 64 72 2d 73 69 7a 65 20 7c 20 ** | hdr-size |
1d742 74 79 70 65 20 30 20 7c 20 74 79 70 65 20 31 20 type 0 | type 1
1d743 7c 20 2e 2e 2e 20 7c 20 74 79 70 65 20 4e 2d 31 | ... | type N-1
1d744 20 7c 20 64 61 74 61 30 20 7c 20 2e 2e 2e 20 7c | data0 | ... |
1d745 20 64 61 74 61 20 4e 2d 31 20 7c 0a 20 20 2a 2a data N-1 |. **
1d746 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ---------------
1d747 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d748 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d749 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d74a 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 0a 20 ---------. **.
1d74b 20 2a 2a 20 44 61 74 61 28 30 29 20 69 73 20 74 ** Data(0) is t
1d74c 61 6b 65 6e 20 66 72 6f 6d 20 72 65 67 69 73 74 aken from regist
1d74d 65 72 20 50 31 2e 20 20 44 61 74 61 28 31 29 20 er P1. Data(1)
1d74e 63 6f 6d 65 73 20 66 72 6f 6d 20 72 65 67 69 73 comes from regis
1d74f 74 65 72 20 50 31 2b 31 0a 20 20 2a 2a 20 61 6e ter P1+1. ** an
1d750 64 20 73 6f 20 66 72 6f 74 68 2e 0a 20 20 2a 2a d so froth.. **
1d751 0a 20 20 2a 2a 20 45 61 63 68 20 74 79 70 65 20 . ** Each type
1d752 66 69 65 6c 64 20 69 73 20 61 20 76 61 72 69 6e field is a varin
1d753 74 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 t representing t
1d754 68 65 20 73 65 72 69 61 6c 20 74 79 70 65 20 6f he serial type o
1d755 66 20 74 68 65 0a 20 20 2a 2a 20 63 6f 72 72 65 f the. ** corre
1d756 73 70 6f 6e 64 69 6e 67 20 64 61 74 61 20 65 6c sponding data el
1d757 65 6d 65 6e 74 20 28 73 65 65 20 73 71 6c 69 74 ement (see sqlit
1d758 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 e3VdbeSerialType
1d759 28 29 29 2e 20 54 68 65 0a 20 20 2a 2a 20 68 64 ()). The. ** hd
1d75a 72 2d 73 69 7a 65 20 66 69 65 6c 64 20 69 73 20 r-size field is
1d75b 61 6c 73 6f 20 61 20 76 61 72 69 6e 74 20 77 68 also a varint wh
1d75c 69 63 68 20 69 73 20 74 68 65 20 6f 66 66 73 65 ich is the offse
1d75d 74 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e t from the begin
1d75e 6e 69 6e 67 0a 20 20 2a 2a 20 6f 66 20 74 68 65 ning. ** of the
1d75f 20 72 65 63 6f 72 64 20 74 6f 20 64 61 74 61 30 record to data0
1d760 2e 0a 20 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 44 .. */. u.ao.nD
1d761 61 74 61 20 3d 20 30 3b 20 20 20 20 20 20 20 20 ata = 0;
1d762 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
1d763 74 65 73 20 6f 66 20 64 61 74 61 20 73 70 61 63 tes of data spac
1d764 65 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 48 64 72 e */. u.ao.nHdr
1d765 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f = 0; /
1d766 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 * Number of byte
1d767 73 20 6f 66 20 68 65 61 64 65 72 20 73 70 61 63 s of header spac
1d768 65 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 42 79 74 e */. u.ao.nByt
1d769 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f e = 0; /
1d76a 2a 20 44 61 74 61 20 73 70 61 63 65 20 72 65 71 * Data space req
1d76b 75 69 72 65 64 20 66 6f 72 20 74 68 69 73 20 72 uired for this r
1d76c 65 63 6f 72 64 20 2a 2f 0a 20 20 75 2e 61 6f 2e ecord */. u.ao.
1d76d 6e 5a 65 72 6f 20 3d 20 30 3b 20 20 20 20 20 20 nZero = 0;
1d76e 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1d76f 7a 65 72 6f 20 62 79 74 65 73 20 61 74 20 74 68 zero bytes at th
1d770 65 20 65 6e 64 20 6f 66 20 74 68 65 20 72 65 63 e end of the rec
1d771 6f 72 64 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 46 ord */. u.ao.nF
1d772 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a ield = pOp->p1;.
1d773 20 20 75 2e 61 6f 2e 7a 41 66 66 69 6e 69 74 79 u.ao.zAffinity
1d774 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 = pOp->p4.z;.
1d775 61 73 73 65 72 74 28 20 75 2e 61 6f 2e 6e 46 69 assert( u.ao.nFi
1d776 65 6c 64 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 eld>0 && pOp->p2
1d777 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 2b 75 2e >0 && pOp->p2+u.
1d778 61 6f 2e 6e 46 69 65 6c 64 3c 3d 70 2d 3e 6e 4d ao.nField<=p->nM
1d779 65 6d 2b 31 20 29 3b 0a 20 20 75 2e 61 6f 2e 70 em+1 );. u.ao.p
1d77a 44 61 74 61 30 20 3d 20 26 70 2d 3e 61 4d 65 6d Data0 = &p->aMem
1d77b 5b 75 2e 61 6f 2e 6e 46 69 65 6c 64 5d 3b 0a 20 [u.ao.nField];.
1d77c 20 75 2e 61 6f 2e 6e 46 69 65 6c 64 20 3d 20 70 u.ao.nField = p
1d77d 4f 70 2d 3e 70 32 3b 0a 20 20 75 2e 61 6f 2e 70 Op->p2;. u.ao.p
1d77e 4c 61 73 74 20 3d 20 26 75 2e 61 6f 2e 70 44 61 Last = &u.ao.pDa
1d77f 74 61 30 5b 75 2e 61 6f 2e 6e 46 69 65 6c 64 2d ta0[u.ao.nField-
1d780 31 5d 3b 0a 20 20 75 2e 61 6f 2e 66 69 6c 65 5f 1];. u.ao.file_
1d781 66 6f 72 6d 61 74 20 3d 20 70 2d 3e 6d 69 6e 57 format = p->minW
1d782 72 69 74 65 46 69 6c 65 46 6f 72 6d 61 74 3b 0a riteFileFormat;.
1d783 0a 20 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 . /* Loop throu
1d784 67 68 20 74 68 65 20 65 6c 65 6d 65 6e 74 73 20 gh the elements
1d785 74 68 61 74 20 77 69 6c 6c 20 6d 61 6b 65 20 75 that will make u
1d786 70 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 p the record to
1d787 66 69 67 75 72 65 0a 20 20 2a 2a 20 6f 75 74 20 figure. ** out
1d788 68 6f 77 20 6d 75 63 68 20 73 70 61 63 65 20 69 how much space i
1d789 73 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 74 s required for t
1d78a 68 65 20 6e 65 77 20 72 65 63 6f 72 64 2e 0a 20 he new record..
1d78b 20 2a 2f 0a 20 20 66 6f 72 28 75 2e 61 6f 2e 70 */. for(u.ao.p
1d78c 52 65 63 3d 75 2e 61 6f 2e 70 44 61 74 61 30 3b Rec=u.ao.pData0;
1d78d 20 75 2e 61 6f 2e 70 52 65 63 3c 3d 75 2e 61 6f u.ao.pRec<=u.ao
1d78e 2e 70 4c 61 73 74 3b 20 75 2e 61 6f 2e 70 52 65 .pLast; u.ao.pRe
1d78f 63 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 75 2e c++){. if( u.
1d790 61 6f 2e 7a 41 66 66 69 6e 69 74 79 20 29 7b 0a ao.zAffinity ){.
1d791 20 20 20 20 20 20 61 70 70 6c 79 41 66 66 69 6e applyAffin
1d792 69 74 79 28 75 2e 61 6f 2e 70 52 65 63 2c 20 75 ity(u.ao.pRec, u
1d793 2e 61 6f 2e 7a 41 66 66 69 6e 69 74 79 5b 75 2e .ao.zAffinity[u.
1d794 61 6f 2e 70 52 65 63 2d 75 2e 61 6f 2e 70 44 61 ao.pRec-u.ao.pDa
1d795 74 61 30 5d 2c 20 65 6e 63 6f 64 69 6e 67 29 3b ta0], encoding);
1d796 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 75 . }. if( u
1d797 2e 61 6f 2e 70 52 65 63 2d 3e 66 6c 61 67 73 26 .ao.pRec->flags&
1d798 4d 45 4d 5f 5a 65 72 6f 20 26 26 20 75 2e 61 6f MEM_Zero && u.ao
1d799 2e 70 52 65 63 2d 3e 6e 3e 30 20 29 7b 0a 20 20 .pRec->n>0 ){.
1d79a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1d79b 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 75 2e 61 emExpandBlob(u.a
1d79c 6f 2e 70 52 65 63 29 3b 0a 20 20 20 20 7d 0a 20 o.pRec);. }.
1d79d 20 20 20 75 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 u.ao.serial_t
1d79e 79 70 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 ype = sqlite3Vdb
1d79f 65 53 65 72 69 61 6c 54 79 70 65 28 75 2e 61 6f eSerialType(u.ao
1d7a0 2e 70 52 65 63 2c 20 75 2e 61 6f 2e 66 69 6c 65 .pRec, u.ao.file
1d7a1 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 20 20 75 2e _format);. u.
1d7a2 61 6f 2e 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 ao.len = sqlite3
1d7a3 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 VdbeSerialTypeLe
1d7a4 6e 28 75 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 79 n(u.ao.serial_ty
1d7a5 70 65 29 3b 0a 20 20 20 20 75 2e 61 6f 2e 6e 44 pe);. u.ao.nD
1d7a6 61 74 61 20 2b 3d 20 75 2e 61 6f 2e 6c 65 6e 3b ata += u.ao.len;
1d7a7 0a 20 20 20 20 75 2e 61 6f 2e 6e 48 64 72 20 2b . u.ao.nHdr +
1d7a8 3d 20 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c = sqlite3VarintL
1d7a9 65 6e 28 75 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 en(u.ao.serial_t
1d7aa 79 70 65 29 3b 0a 20 20 20 20 69 66 28 20 75 2e ype);. if( u.
1d7ab 61 6f 2e 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 ao.pRec->flags &
1d7ac 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 MEM_Zero ){.
1d7ad 20 20 20 2f 2a 20 4f 6e 6c 79 20 70 75 72 65 20 /* Only pure
1d7ae 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 42 4c 4f 42 zero-filled BLOB
1d7af 73 20 63 61 6e 20 62 65 20 69 6e 70 75 74 20 74 s can be input t
1d7b0 6f 20 74 68 69 73 20 4f 70 63 6f 64 65 2e 0a 20 o this Opcode..
1d7b1 20 20 20 20 20 2a 2a 20 57 65 20 64 6f 20 6e 6f ** We do no
1d7b2 74 20 61 6c 6c 6f 77 20 62 6c 6f 62 73 20 77 69 t allow blobs wi
1d7b3 74 68 20 61 20 70 72 65 66 69 78 20 61 6e 64 20 th a prefix and
1d7b4 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 a zero-filled ta
1d7b5 69 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 75 2e 61 il. */. u.a
1d7b6 6f 2e 6e 5a 65 72 6f 20 2b 3d 20 75 2e 61 6f 2e o.nZero += u.ao.
1d7b7 70 52 65 63 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 pRec->u.nZero;.
1d7b8 20 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 61 }else if( u.a
1d7b9 6f 2e 6c 65 6e 20 29 7b 0a 20 20 20 20 20 20 75 o.len ){. u
1d7ba 2e 61 6f 2e 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20 .ao.nZero = 0;.
1d7bb 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 }. }.. /* A
1d7bc 64 64 20 74 68 65 20 69 6e 69 74 69 61 6c 20 68 dd the initial h
1d7bd 65 61 64 65 72 20 76 61 72 69 6e 74 20 61 6e 64 eader varint and
1d7be 20 74 6f 74 61 6c 20 74 68 65 20 73 69 7a 65 20 total the size
1d7bf 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 48 64 72 20 2b */. u.ao.nHdr +
1d7c0 3d 20 75 2e 61 6f 2e 6e 56 61 72 69 6e 74 20 3d = u.ao.nVarint =
1d7c1 20 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 sqlite3VarintLe
1d7c2 6e 28 75 2e 61 6f 2e 6e 48 64 72 29 3b 0a 20 20 n(u.ao.nHdr);.
1d7c3 69 66 28 20 75 2e 61 6f 2e 6e 56 61 72 69 6e 74 if( u.ao.nVarint
1d7c4 3c 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 <sqlite3VarintLe
1d7c5 6e 28 75 2e 61 6f 2e 6e 48 64 72 29 20 29 7b 0a n(u.ao.nHdr) ){.
1d7c6 20 20 20 20 75 2e 61 6f 2e 6e 48 64 72 2b 2b 3b u.ao.nHdr++;
1d7c7 0a 20 20 7d 0a 20 20 75 2e 61 6f 2e 6e 42 79 74 . }. u.ao.nByt
1d7c8 65 20 3d 20 75 2e 61 6f 2e 6e 48 64 72 2b 75 2e e = u.ao.nHdr+u.
1d7c9 61 6f 2e 6e 44 61 74 61 2d 75 2e 61 6f 2e 6e 5a ao.nData-u.ao.nZ
1d7ca 65 72 6f 3b 0a 20 20 69 66 28 20 75 2e 61 6f 2e ero;. if( u.ao.
1d7cb 6e 42 79 74 65 3e 64 62 2d 3e 61 4c 69 6d 69 74 nByte>db->aLimit
1d7cc 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 [SQLITE_LIMIT_LE
1d7cd 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 67 6f 74 NGTH] ){. got
1d7ce 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 0a o too_big;. }..
1d7cf 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 /* Make sure t
1d7d0 68 65 20 6f 75 74 70 75 74 20 72 65 67 69 73 74 he output regist
1d7d1 65 72 20 68 61 73 20 61 20 62 75 66 66 65 72 20 er has a buffer
1d7d2 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 large enough to
1d7d3 73 74 6f 72 65 0a 20 20 2a 2a 20 74 68 65 20 6e store. ** the n
1d7d4 65 77 20 72 65 63 6f 72 64 2e 20 54 68 65 20 6f ew record. The o
1d7d5 75 74 70 75 74 20 72 65 67 69 73 74 65 72 20 28 utput register (
1d7d6 70 4f 70 2d 3e 70 33 29 20 69 73 20 6e 6f 74 20 pOp->p3) is not
1d7d7 61 6c 6c 6f 77 65 64 20 74 6f 0a 20 20 2a 2a 20 allowed to. **
1d7d8 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 69 6e be one of the in
1d7d9 70 75 74 20 72 65 67 69 73 74 65 72 73 20 28 62 put registers (b
1d7da 65 63 61 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f ecause the follo
1d7db 77 69 6e 67 20 63 61 6c 6c 20 74 6f 0a 20 20 2a wing call to. *
1d7dc 2a 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d * sqlite3VdbeMem
1d7dd 47 72 6f 77 28 29 20 63 6f 75 6c 64 20 63 6c 6f Grow() could clo
1d7de 62 62 65 72 20 74 68 65 20 76 61 6c 75 65 20 62 bber the value b
1d7df 65 66 6f 72 65 20 69 74 20 69 73 20 75 73 65 64 efore it is used
1d7e0 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 ).. */. assert
1d7e1 28 20 70 4f 70 2d 3e 70 33 3c 70 4f 70 2d 3e 70 ( pOp->p3<pOp->p
1d7e2 31 20 7c 7c 20 70 4f 70 2d 3e 70 33 3e 3d 70 4f 1 || pOp->p3>=pO
1d7e3 70 2d 3e 70 31 2b 70 4f 70 2d 3e 70 32 20 29 3b p->p1+pOp->p2 );
1d7e4 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d . pOut = &p->aM
1d7e5 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 em[pOp->p3];. i
1d7e6 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 f( sqlite3VdbeMe
1d7e7 6d 47 72 6f 77 28 70 4f 75 74 2c 20 28 69 6e 74 mGrow(pOut, (int
1d7e8 29 75 2e 61 6f 2e 6e 42 79 74 65 2c 20 30 29 20 )u.ao.nByte, 0)
1d7e9 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d ){. goto no_m
1d7ea 65 6d 3b 0a 20 20 7d 0a 20 20 75 2e 61 6f 2e 7a em;. }. u.ao.z
1d7eb 4e 65 77 52 65 63 6f 72 64 20 3d 20 28 75 38 20 NewRecord = (u8
1d7ec 2a 29 70 4f 75 74 2d 3e 7a 3b 0a 0a 20 20 2f 2a *)pOut->z;.. /*
1d7ed 20 57 72 69 74 65 20 74 68 65 20 72 65 63 6f 72 Write the recor
1d7ee 64 20 2a 2f 0a 20 20 75 2e 61 6f 2e 69 20 3d 20 d */. u.ao.i =
1d7ef 70 75 74 56 61 72 69 6e 74 33 32 28 75 2e 61 6f putVarint32(u.ao
1d7f0 2e 7a 4e 65 77 52 65 63 6f 72 64 2c 20 75 2e 61 .zNewRecord, u.a
1d7f1 6f 2e 6e 48 64 72 29 3b 0a 20 20 66 6f 72 28 75 o.nHdr);. for(u
1d7f2 2e 61 6f 2e 70 52 65 63 3d 75 2e 61 6f 2e 70 44 .ao.pRec=u.ao.pD
1d7f3 61 74 61 30 3b 20 75 2e 61 6f 2e 70 52 65 63 3c ata0; u.ao.pRec<
1d7f4 3d 75 2e 61 6f 2e 70 4c 61 73 74 3b 20 75 2e 61 =u.ao.pLast; u.a
1d7f5 6f 2e 70 52 65 63 2b 2b 29 7b 0a 20 20 20 20 75 o.pRec++){. u
1d7f6 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 79 70 65 20 .ao.serial_type
1d7f7 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 = sqlite3VdbeSer
1d7f8 69 61 6c 54 79 70 65 28 75 2e 61 6f 2e 70 52 65 ialType(u.ao.pRe
1d7f9 63 2c 20 75 2e 61 6f 2e 66 69 6c 65 5f 66 6f 72 c, u.ao.file_for
1d7fa 6d 61 74 29 3b 0a 20 20 20 20 75 2e 61 6f 2e 69 mat);. u.ao.i
1d7fb 20 2b 3d 20 70 75 74 56 61 72 69 6e 74 33 32 28 += putVarint32(
1d7fc 26 75 2e 61 6f 2e 7a 4e 65 77 52 65 63 6f 72 64 &u.ao.zNewRecord
1d7fd 5b 75 2e 61 6f 2e 69 5d 2c 20 75 2e 61 6f 2e 73 [u.ao.i], u.ao.s
1d7fe 65 72 69 61 6c 5f 74 79 70 65 29 3b 20 20 20 20 erial_type);
1d7ff 20 20 2f 2a 20 73 65 72 69 61 6c 20 74 79 70 65 /* serial type
1d800 20 2a 2f 0a 20 20 7d 0a 20 20 66 6f 72 28 75 2e */. }. for(u.
1d801 61 6f 2e 70 52 65 63 3d 75 2e 61 6f 2e 70 44 61 ao.pRec=u.ao.pDa
1d802 74 61 30 3b 20 75 2e 61 6f 2e 70 52 65 63 3c 3d ta0; u.ao.pRec<=
1d803 75 2e 61 6f 2e 70 4c 61 73 74 3b 20 75 2e 61 6f u.ao.pLast; u.ao
1d804 2e 70 52 65 63 2b 2b 29 7b 20 20 2f 2a 20 73 65 .pRec++){ /* se
1d805 72 69 61 6c 20 64 61 74 61 20 2a 2f 0a 20 20 20 rial data */.
1d806 20 75 2e 61 6f 2e 69 20 2b 3d 20 73 71 6c 69 74 u.ao.i += sqlit
1d807 65 33 56 64 62 65 53 65 72 69 61 6c 50 75 74 28 e3VdbeSerialPut(
1d808 26 75 2e 61 6f 2e 7a 4e 65 77 52 65 63 6f 72 64 &u.ao.zNewRecord
1d809 5b 75 2e 61 6f 2e 69 5d 2c 20 28 69 6e 74 29 28 [u.ao.i], (int)(
1d80a 75 2e 61 6f 2e 6e 42 79 74 65 2d 75 2e 61 6f 2e u.ao.nByte-u.ao.
1d80b 69 29 2c 20 75 2e 61 6f 2e 70 52 65 63 2c 75 2e i), u.ao.pRec,u.
1d80c 61 6f 2e 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b ao.file_format);
1d80d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 75 . }. assert( u
1d80e 2e 61 6f 2e 69 3d 3d 75 2e 61 6f 2e 6e 42 79 74 .ao.i==u.ao.nByt
1d80f 65 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 e );.. assert(
1d810 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 pOp->p3>0 && pOp
1d811 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b ->p3<=p->nMem );
1d812 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 28 69 6e . pOut->n = (in
1d813 74 29 75 2e 61 6f 2e 6e 42 79 74 65 3b 0a 20 20 t)u.ao.nByte;.
1d814 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 pOut->flags = ME
1d815 4d 5f 42 6c 6f 62 20 7c 20 4d 45 4d 5f 44 79 6e M_Blob | MEM_Dyn
1d816 3b 0a 20 20 70 4f 75 74 2d 3e 78 44 65 6c 20 3d ;. pOut->xDel =
1d817 20 30 3b 0a 20 20 69 66 28 20 75 2e 61 6f 2e 6e 0;. if( u.ao.n
1d818 5a 65 72 6f 20 29 7b 0a 20 20 20 20 70 4f 75 74 Zero ){. pOut
1d819 2d 3e 75 2e 6e 5a 65 72 6f 20 3d 20 75 2e 61 6f ->u.nZero = u.ao
1d81a 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 70 4f 75 74 .nZero;. pOut
1d81b 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 5a ->flags |= MEM_Z
1d81c 65 72 6f 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 2d ero;. }. pOut-
1d81d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 >enc = SQLITE_UT
1d81e 46 38 3b 20 20 2f 2a 20 49 6e 20 63 61 73 65 20 F8; /* In case
1d81f 74 68 65 20 62 6c 6f 62 20 69 73 20 65 76 65 72 the blob is ever
1d820 20 63 6f 6e 76 65 72 74 65 64 20 74 6f 20 74 65 converted to te
1d821 78 74 20 2a 2f 0a 20 20 52 45 47 49 53 54 45 52 xt */. REGISTER
1d822 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 _TRACE(pOp->p3,
1d823 70 4f 75 74 29 3b 0a 20 20 55 50 44 41 54 45 5f pOut);. UPDATE_
1d824 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 MAX_BLOBSIZE(pOu
1d825 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a t);. break;.}..
1d826 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 75 6e 74 /* Opcode: Count
1d827 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a P1 P2 * * *.**.
1d828 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 6e 75 6d ** Store the num
1d829 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 28 ber of entries (
1d82a 61 6e 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 an integer value
1d82b 29 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 6f ) in the table o
1d82c 72 20 69 6e 64 65 78 20 0a 2a 2a 20 6f 70 65 6e r index .** open
1d82d 65 64 20 62 79 20 63 75 72 73 6f 72 20 50 31 20 ed by cursor P1
1d82e 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a in register P2.*
1d82f 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
1d830 5f 4f 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e 54 _OMIT_BTREECOUNT
1d831 0a 63 61 73 65 20 4f 50 5f 43 6f 75 6e 74 3a 20 .case OP_Count:
1d832 7b 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 { /* out
1d833 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 2-prerelease */.
1d834 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 #if 0 /* local
1d835 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1d836 69 6e 74 6f 20 75 2e 61 70 20 2a 2f 0a 20 20 69 into u.ap */. i
1d837 36 34 20 6e 45 6e 74 72 79 3b 0a 20 20 42 74 43 64 nEntry;. BtC
1d838 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 23 65 ursor *pCrsr;.#e
1d839 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1d83a 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1d83b 74 6f 20 75 2e 61 70 20 2a 2f 0a 0a 20 20 75 2e to u.ap */.. u.
1d83c 61 70 2e 70 43 72 73 72 20 3d 20 70 2d 3e 61 70 ap.pCrsr = p->ap
1d83d 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 2d 3e 70 43 Csr[pOp->p1]->pC
1d83e 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 75 2e 61 ursor;. if( u.a
1d83f 70 2e 70 43 72 73 72 20 29 7b 0a 20 20 20 20 72 p.pCrsr ){. r
1d840 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1d841 43 6f 75 6e 74 28 75 2e 61 70 2e 70 43 72 73 72 Count(u.ap.pCrsr
1d842 2c 20 26 75 2e 61 70 2e 6e 45 6e 74 72 79 29 3b , &u.ap.nEntry);
1d843 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e . }else{. u.
1d844 61 70 2e 6e 45 6e 74 72 79 20 3d 20 30 3b 0a 20 ap.nEntry = 0;.
1d845 20 7d 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 }. pOut->flags
1d846 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70 4f = MEM_Int;. pO
1d847 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 70 2e 6e ut->u.i = u.ap.n
1d848 45 6e 74 72 79 3b 0a 20 20 62 72 65 61 6b 3b 0a Entry;. break;.
1d849 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 4f 70 63 }.#endif../* Opc
1d84a 6f 64 65 3a 20 53 61 76 65 70 6f 69 6e 74 20 50 ode: Savepoint P
1d84b 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 1 * * P4 *.**.**
1d84c 20 4f 70 65 6e 2c 20 72 65 6c 65 61 73 65 20 6f Open, release o
1d84d 72 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 73 r rollback the s
1d84e 61 76 65 70 6f 69 6e 74 20 6e 61 6d 65 64 20 62 avepoint named b
1d84f 79 20 70 61 72 61 6d 65 74 65 72 20 50 34 2c 20 y parameter P4,
1d850 64 65 70 65 6e 64 69 6e 67 0a 2a 2a 20 6f 6e 20 depending.** on
1d851 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50 31 2e the value of P1.
1d852 20 54 6f 20 6f 70 65 6e 20 61 20 6e 65 77 20 73 To open a new s
1d853 61 76 65 70 6f 69 6e 74 2c 20 50 31 3d 3d 30 2e avepoint, P1==0.
1d854 20 54 6f 20 72 65 6c 65 61 73 65 20 28 63 6f 6d To release (com
1d855 6d 69 74 29 20 61 6e 0a 2a 2a 20 65 78 69 73 74 mit) an.** exist
1d856 69 6e 67 20 73 61 76 65 70 6f 69 6e 74 2c 20 50 ing savepoint, P
1d857 31 3d 3d 31 2c 20 6f 72 20 74 6f 20 72 6f 6c 6c 1==1, or to roll
1d858 62 61 63 6b 20 61 6e 20 65 78 69 73 74 69 6e 67 back an existing
1d859 20 73 61 76 65 70 6f 69 6e 74 20 50 31 3d 3d 32 savepoint P1==2
1d85a 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 61 76 ..*/.case OP_Sav
1d85b 65 70 6f 69 6e 74 3a 20 7b 0a 23 69 66 20 30 20 epoint: {.#if 0
1d85c 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1d85d 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1d85e 2e 61 71 20 2a 2f 0a 20 20 69 6e 74 20 70 31 3b .aq */. int p1;
1d85f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d860 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 /* Valu
1d861 65 20 6f 66 20 50 31 20 6f 70 65 72 61 6e 64 20 e of P1 operand
1d862 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 */. char *zName
1d863 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1d864 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 /* Name of
1d865 73 61 76 65 70 6f 69 6e 74 20 2a 2f 0a 20 20 69 savepoint */. i
1d866 6e 74 20 6e 4e 61 6d 65 3b 0a 20 20 53 61 76 65 nt nName;. Save
1d867 70 6f 69 6e 74 20 2a 70 4e 65 77 3b 0a 20 20 53 point *pNew;. S
1d868 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 70 avepoint *pSavep
1d869 6f 69 6e 74 3b 0a 20 20 53 61 76 65 70 6f 69 6e oint;. Savepoin
1d86a 74 20 2a 70 54 6d 70 3b 0a 20 20 69 6e 74 20 69 t *pTmp;. int i
1d86b 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 69 6e 74 Savepoint;. int
1d86c 20 69 69 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c ii;.#endif /* l
1d86d 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1d86e 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 71 20 2a oved into u.aq *
1d86f 2f 0a 0a 20 20 75 2e 61 71 2e 70 31 20 3d 20 70 /.. u.aq.p1 = p
1d870 4f 70 2d 3e 70 31 3b 0a 20 20 75 2e 61 71 2e 7a Op->p1;. u.aq.z
1d871 4e 61 6d 65 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a Name = pOp->p4.z
1d872 3b 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74 ;.. /* Assert t
1d873 68 61 74 20 74 68 65 20 75 2e 61 71 2e 70 31 20 hat the u.aq.p1
1d874 70 61 72 61 6d 65 74 65 72 20 69 73 20 76 61 6c parameter is val
1d875 69 64 2e 20 41 6c 73 6f 20 74 68 61 74 20 69 66 id. Also that if
1d876 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6f 70 65 there is no ope
1d877 6e 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 n. ** transacti
1d878 6f 6e 2c 20 74 68 65 6e 20 74 68 65 72 65 20 63 on, then there c
1d879 61 6e 6e 6f 74 20 62 65 20 61 6e 79 20 73 61 76 annot be any sav
1d87a 65 70 6f 69 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 epoints.. */.
1d87b 61 73 73 65 72 74 28 20 64 62 2d 3e 70 53 61 76 assert( db->pSav
1d87c 65 70 6f 69 6e 74 3d 3d 30 20 7c 7c 20 64 62 2d epoint==0 || db-
1d87d 3e 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 29 >autoCommit==0 )
1d87e 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 71 ;. assert( u.aq
1d87f 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 42 .p1==SAVEPOINT_B
1d880 45 47 49 4e 7c 7c 75 2e 61 71 2e 70 31 3d 3d 53 EGIN||u.aq.p1==S
1d881 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 AVEPOINT_RELEASE
1d882 7c 7c 75 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 ||u.aq.p1==SAVEP
1d883 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b OINT_ROLLBACK );
1d884 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 70 . assert( db->p
1d885 53 61 76 65 70 6f 69 6e 74 20 7c 7c 20 64 62 2d Savepoint || db-
1d886 3e 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 >isTransactionSa
1d887 76 65 70 6f 69 6e 74 3d 3d 30 20 29 3b 0a 20 20 vepoint==0 );.
1d888 61 73 73 65 72 74 28 20 63 68 65 63 6b 53 61 76 assert( checkSav
1d889 65 70 6f 69 6e 74 43 6f 75 6e 74 28 64 62 29 20 epointCount(db)
1d88a 29 3b 0a 0a 20 20 69 66 28 20 75 2e 61 71 2e 70 );.. if( u.aq.p
1d88b 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 42 45 47 1==SAVEPOINT_BEG
1d88c 49 4e 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62 IN ){. if( db
1d88d 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74 3e 30 ->writeVdbeCnt>0
1d88e 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 6e ){. /* A n
1d88f 65 77 20 73 61 76 65 70 6f 69 6e 74 20 63 61 6e ew savepoint can
1d890 6e 6f 74 20 62 65 20 63 72 65 61 74 65 64 20 69 not be created i
1d891 66 20 74 68 65 72 65 20 61 72 65 20 61 63 74 69 f there are acti
1d892 76 65 20 77 72 69 74 65 0a 20 20 20 20 20 20 2a ve write. *
1d893 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 28 69 2e * statements (i.
1d894 65 2e 20 6f 70 65 6e 20 72 65 61 64 2f 77 72 69 e. open read/wri
1d895 74 65 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 62 te incremental b
1d896 6c 6f 62 20 68 61 6e 64 6c 65 73 29 2e 0a 20 20 lob handles)..
1d897 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c */. sql
1d898 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 ite3SetString(&p
1d899 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 ->zErrMsg, db, "
1d89a 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 73 61 76 65 cannot open save
1d89b 70 6f 69 6e 74 20 2d 20 22 0a 20 20 20 20 20 20 point - ".
1d89c 20 20 22 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 "SQL statement
1d89d 73 20 69 6e 20 70 72 6f 67 72 65 73 73 22 29 3b s in progress");
1d89e 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
1d89f 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 65 6c TE_BUSY;. }el
1d8a0 73 65 7b 0a 20 20 20 20 20 20 75 2e 61 71 2e 6e se{. u.aq.n
1d8a1 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 Name = sqlite3St
1d8a2 72 6c 65 6e 33 30 28 75 2e 61 71 2e 7a 4e 61 6d rlen30(u.aq.zNam
1d8a3 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 72 e);.. /* Cr
1d8a4 65 61 74 65 20 61 20 6e 65 77 20 73 61 76 65 70 eate a new savep
1d8a5 6f 69 6e 74 20 73 74 72 75 63 74 75 72 65 2e 20 oint structure.
1d8a6 2a 2f 0a 20 20 20 20 20 20 75 2e 61 71 2e 70 4e */. u.aq.pN
1d8a7 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 ew = sqlite3DbMa
1d8a8 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 llocRaw(db, size
1d8a9 6f 66 28 53 61 76 65 70 6f 69 6e 74 29 2b 75 2e of(Savepoint)+u.
1d8aa 61 71 2e 6e 4e 61 6d 65 2b 31 29 3b 0a 20 20 20 aq.nName+1);.
1d8ab 20 20 20 69 66 28 20 75 2e 61 71 2e 70 4e 65 77 if( u.aq.pNew
1d8ac 20 29 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 71 ){. u.aq
1d8ad 2e 70 4e 65 77 2d 3e 7a 4e 61 6d 65 20 3d 20 28 .pNew->zName = (
1d8ae 63 68 61 72 20 2a 29 26 75 2e 61 71 2e 70 4e 65 char *)&u.aq.pNe
1d8af 77 5b 31 5d 3b 0a 20 20 20 20 20 20 20 20 6d 65 w[1];. me
1d8b0 6d 63 70 79 28 75 2e 61 71 2e 70 4e 65 77 2d 3e mcpy(u.aq.pNew->
1d8b1 7a 4e 61 6d 65 2c 20 75 2e 61 71 2e 7a 4e 61 6d zName, u.aq.zNam
1d8b2 65 2c 20 75 2e 61 71 2e 6e 4e 61 6d 65 2b 31 29 e, u.aq.nName+1)
1d8b3 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 ;.. /* If
1d8b4 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6f 70 65 there is no ope
1d8b5 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 n transaction, t
1d8b6 68 65 6e 20 6d 61 72 6b 20 74 68 69 73 20 61 73 hen mark this as
1d8b7 20 61 20 73 70 65 63 69 61 6c 0a 20 20 20 20 20 a special.
1d8b8 20 20 20 2a 2a 20 22 74 72 61 6e 73 61 63 74 69 ** "transacti
1d8b9 6f 6e 20 73 61 76 65 70 6f 69 6e 74 22 2e 20 2a on savepoint". *
1d8ba 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 64 62 /. if( db
1d8bb 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a ->autoCommit ){.
1d8bc 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 db->au
1d8bd 74 6f 43 6f 6d 6d 69 74 20 3d 20 30 3b 0a 20 20 toCommit = 0;.
1d8be 20 20 20 20 20 20 20 20 64 62 2d 3e 69 73 54 72 db->isTr
1d8bf 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 ansactionSavepoi
1d8c0 6e 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 nt = 1;.
1d8c1 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
1d8c2 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 2b db->nSavepoint+
1d8c3 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 +;. }..
1d8c4 20 20 20 20 20 20 2f 2a 20 4c 69 6e 6b 20 74 68 /* Link th
1d8c5 65 20 6e 65 77 20 73 61 76 65 70 6f 69 6e 74 20 e new savepoint
1d8c6 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 into the databas
1d8c7 65 20 68 61 6e 64 6c 65 27 73 20 6c 69 73 74 2e e handle's list.
1d8c8 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 2e 61 71 */. u.aq
1d8c9 2e 70 4e 65 77 2d 3e 70 4e 65 78 74 20 3d 20 64 .pNew->pNext = d
1d8ca 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b 0a 20 b->pSavepoint;.
1d8cb 20 20 20 20 20 20 20 64 62 2d 3e 70 53 61 76 65 db->pSave
1d8cc 70 6f 69 6e 74 20 3d 20 75 2e 61 71 2e 70 4e 65 point = u.aq.pNe
1d8cd 77 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 71 2e w;. u.aq.
1d8ce 70 4e 65 77 2d 3e 6e 44 65 66 65 72 72 65 64 43 pNew->nDeferredC
1d8cf 6f 6e 73 20 3d 20 64 62 2d 3e 6e 44 65 66 65 72 ons = db->nDefer
1d8d0 72 65 64 43 6f 6e 73 3b 0a 20 20 20 20 20 20 7d redCons;. }
1d8d1 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a . }. }else{.
1d8d2 20 20 20 20 75 2e 61 71 2e 69 53 61 76 65 70 6f u.aq.iSavepo
1d8d3 69 6e 74 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a int = 0;.. /*
1d8d4 20 46 69 6e 64 20 74 68 65 20 6e 61 6d 65 64 20 Find the named
1d8d5 73 61 76 65 70 6f 69 6e 74 2e 20 49 66 20 74 68 savepoint. If th
1d8d6 65 72 65 20 69 73 20 6e 6f 20 73 75 63 68 20 73 ere is no such s
1d8d7 61 76 65 70 6f 69 6e 74 2c 20 74 68 65 6e 20 61 avepoint, then a
1d8d8 6e 0a 20 20 20 20 2a 2a 20 61 6e 20 65 72 72 6f n. ** an erro
1d8d9 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f r is returned to
1d8da 20 74 68 65 20 75 73 65 72 2e 20 20 2a 2f 0a 20 the user. */.
1d8db 20 20 20 66 6f 72 28 0a 20 20 20 20 20 20 75 2e for(. u.
1d8dc 61 71 2e 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 aq.pSavepoint =
1d8dd 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b 0a db->pSavepoint;.
1d8de 20 20 20 20 20 20 75 2e 61 71 2e 70 53 61 76 65 u.aq.pSave
1d8df 70 6f 69 6e 74 20 26 26 20 73 71 6c 69 74 65 33 point && sqlite3
1d8e0 53 74 72 49 43 6d 70 28 75 2e 61 71 2e 70 53 61 StrICmp(u.aq.pSa
1d8e1 76 65 70 6f 69 6e 74 2d 3e 7a 4e 61 6d 65 2c 20 vepoint->zName,
1d8e2 75 2e 61 71 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 u.aq.zName);.
1d8e3 20 20 20 75 2e 61 71 2e 70 53 61 76 65 70 6f 69 u.aq.pSavepoi
1d8e4 6e 74 20 3d 20 75 2e 61 71 2e 70 53 61 76 65 70 nt = u.aq.pSavep
1d8e5 6f 69 6e 74 2d 3e 70 4e 65 78 74 0a 20 20 20 20 oint->pNext.
1d8e6 29 7b 0a 20 20 20 20 20 20 75 2e 61 71 2e 69 53 ){. u.aq.iS
1d8e7 61 76 65 70 6f 69 6e 74 2b 2b 3b 0a 20 20 20 20 avepoint++;.
1d8e8 7d 0a 20 20 20 20 69 66 28 20 21 75 2e 61 71 2e }. if( !u.aq.
1d8e9 70 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 pSavepoint ){.
1d8ea 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 sqlite3SetSt
1d8eb 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 ring(&p->zErrMsg
1d8ec 2c 20 64 62 2c 20 22 6e 6f 20 73 75 63 68 20 73 , db, "no such s
1d8ed 61 76 65 70 6f 69 6e 74 3a 20 25 73 22 2c 20 75 avepoint: %s", u
1d8ee 2e 61 71 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 .aq.zName);.
1d8ef 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 rc = SQLITE_ER
1d8f0 52 4f 52 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 ROR;. }else i
1d8f1 66 28 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 77 f(. db->w
1d8f2 72 69 74 65 56 64 62 65 43 6e 74 3e 30 20 7c 7c riteVdbeCnt>0 ||
1d8f3 20 28 75 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 (u.aq.p1==SAVEP
1d8f4 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 26 26 OINT_ROLLBACK &&
1d8f5 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 db->activeVdbeC
1d8f6 6e 74 3e 31 29 0a 20 20 20 20 29 7b 0a 20 20 20 nt>1). ){.
1d8f7 20 20 20 2f 2a 20 49 74 20 69 73 20 6e 6f 74 20 /* It is not
1d8f8 70 6f 73 73 69 62 6c 65 20 74 6f 20 72 65 6c 65 possible to rele
1d8f9 61 73 65 20 28 63 6f 6d 6d 69 74 29 20 61 20 73 ase (commit) a s
1d8fa 61 76 65 70 6f 69 6e 74 20 69 66 20 74 68 65 72 avepoint if ther
1d8fb 65 20 61 72 65 0a 20 20 20 20 20 20 2a 2a 20 61 e are. ** a
1d8fc 63 74 69 76 65 20 77 72 69 74 65 20 73 74 61 74 ctive write stat
1d8fd 65 6d 65 6e 74 73 2e 20 49 74 20 69 73 20 6e 6f ements. It is no
1d8fe 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 72 6f t possible to ro
1d8ff 6c 6c 62 61 63 6b 20 61 20 73 61 76 65 70 6f 69 llback a savepoi
1d900 6e 74 0a 20 20 20 20 20 20 2a 2a 20 69 66 20 74 nt. ** if t
1d901 68 65 72 65 20 61 72 65 20 61 6e 79 20 61 63 74 here are any act
1d902 69 76 65 20 73 74 61 74 65 6d 65 6e 74 73 20 61 ive statements a
1d903 74 20 61 6c 6c 2e 0a 20 20 20 20 20 20 2a 2f 0a t all.. */.
1d904 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 sqlite3Set
1d905 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d String(&p->zErrM
1d906 73 67 2c 20 64 62 2c 0a 20 20 20 20 20 20 20 20 sg, db,.
1d907 22 63 61 6e 6e 6f 74 20 25 73 20 73 61 76 65 70 "cannot %s savep
1d908 6f 69 6e 74 20 2d 20 53 51 4c 20 73 74 61 74 65 oint - SQL state
1d909 6d 65 6e 74 73 20 69 6e 20 70 72 6f 67 72 65 73 ments in progres
1d90a 73 22 2c 0a 20 20 20 20 20 20 20 20 28 75 2e 61 s",. (u.a
1d90b 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f q.p1==SAVEPOINT_
1d90c 52 4f 4c 4c 42 41 43 4b 20 3f 20 22 72 6f 6c 6c ROLLBACK ? "roll
1d90d 62 61 63 6b 22 3a 20 22 72 65 6c 65 61 73 65 22 back": "release"
1d90e 29 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 ). );.
1d90f 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 rc = SQLITE_BUS
1d910 59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 0a 20 Y;. }else{..
1d911 20 20 20 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e /* Determin
1d912 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 e whether or not
1d913 20 74 68 69 73 20 69 73 20 61 20 74 72 61 6e 73 this is a trans
1d914 61 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 action savepoint
1d915 2e 20 49 66 20 73 6f 2c 0a 20 20 20 20 20 20 2a . If so,. *
1d916 2a 20 61 6e 64 20 74 68 69 73 20 69 73 20 61 20 * and this is a
1d917 52 45 4c 45 41 53 45 20 63 6f 6d 6d 61 6e 64 2c RELEASE command,
1d918 20 74 68 65 6e 20 74 68 65 20 63 75 72 72 65 6e then the curren
1d919 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 t transaction.
1d91a 20 20 20 20 2a 2a 20 69 73 20 63 6f 6d 6d 69 74 ** is commit
1d91b 74 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ted.. */.
1d91c 20 20 20 20 69 6e 74 20 69 73 54 72 61 6e 73 61 int isTransa
1d91d 63 74 69 6f 6e 20 3d 20 75 2e 61 71 2e 70 53 61 ction = u.aq.pSa
1d91e 76 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74 3d 3d vepoint->pNext==
1d91f 30 20 26 26 20 64 62 2d 3e 69 73 54 72 61 6e 73 0 && db->isTrans
1d920 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74 3b actionSavepoint;
1d921 0a 20 20 20 20 20 20 69 66 28 20 69 73 54 72 61 . if( isTra
1d922 6e 73 61 63 74 69 6f 6e 20 26 26 20 75 2e 61 71 nsaction && u.aq
1d923 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 .p1==SAVEPOINT_R
1d924 45 4c 45 41 53 45 20 29 7b 0a 20 20 20 20 20 20 ELEASE ){.
1d925 20 20 69 66 28 20 28 72 63 20 3d 20 73 71 6c 69 if( (rc = sqli
1d926 74 65 33 56 64 62 65 43 68 65 63 6b 46 6b 28 70 te3VdbeCheckFk(p
1d927 2c 20 31 29 29 21 3d 53 51 4c 49 54 45 5f 4f 4b , 1))!=SQLITE_OK
1d928 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f ){. go
1d929 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a to vdbe_return;.
1d92a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1d92b 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 db->autoCommit
1d92c 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 69 66 = 1;. if
1d92d 28 20 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c ( sqlite3VdbeHal
1d92e 74 28 70 29 3d 3d 53 51 4c 49 54 45 5f 42 55 53 t(p)==SQLITE_BUS
1d92f 59 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 Y ){. p
1d930 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20 20 20 ->pc = pc;.
1d931 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d db->autoCom
1d932 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 mit = 0;.
1d933 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 p->rc = rc =
1d934 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 SQLITE_BUSY;.
1d935 20 20 20 20 20 20 20 67 6f 74 6f 20 76 64 62 65 goto vdbe
1d936 5f 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 _return;.
1d937 20 7d 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 69 }. db->i
1d938 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 sTransactionSave
1d939 70 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 point = 0;.
1d93a 20 20 20 72 63 20 3d 20 70 2d 3e 72 63 3b 0a 20 rc = p->rc;.
1d93b 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1d93c 20 20 20 20 75 2e 61 71 2e 69 53 61 76 65 70 6f u.aq.iSavepo
1d93d 69 6e 74 20 3d 20 64 62 2d 3e 6e 53 61 76 65 70 int = db->nSavep
1d93e 6f 69 6e 74 20 2d 20 75 2e 61 71 2e 69 53 61 76 oint - u.aq.iSav
1d93f 65 70 6f 69 6e 74 20 2d 20 31 3b 0a 20 20 20 20 epoint - 1;.
1d940 20 20 20 20 66 6f 72 28 75 2e 61 71 2e 69 69 3d for(u.aq.ii=
1d941 30 3b 20 75 2e 61 71 2e 69 69 3c 64 62 2d 3e 6e 0; u.aq.ii<db->n
1d942 44 62 3b 20 75 2e 61 71 2e 69 69 2b 2b 29 7b 0a Db; u.aq.ii++){.
1d943 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 rc = s
1d944 71 6c 69 74 65 33 42 74 72 65 65 53 61 76 65 70 qlite3BtreeSavep
1d945 6f 69 6e 74 28 64 62 2d 3e 61 44 62 5b 75 2e 61 oint(db->aDb[u.a
1d946 71 2e 69 69 5d 2e 70 42 74 2c 20 75 2e 61 71 2e q.ii].pBt, u.aq.
1d947 70 31 2c 20 75 2e 61 71 2e 69 53 61 76 65 70 6f p1, u.aq.iSavepo
1d948 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 int);.
1d949 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
1d94a 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 K ){.
1d94b 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f goto abort_due_
1d94c 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 to_error;.
1d94d 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a }. }.
1d94e 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 71 if( u.aq
1d94f 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 .p1==SAVEPOINT_R
1d950 4f 4c 4c 42 41 43 4b 20 26 26 20 28 64 62 2d 3e OLLBACK && (db->
1d951 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 49 6e 74 flags&SQLITE_Int
1d952 65 72 6e 43 68 61 6e 67 65 73 29 21 3d 30 20 29 ernChanges)!=0 )
1d953 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 {. sqli
1d954 74 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65 te3ExpirePrepare
1d955 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b dStatements(db);
1d956 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
1d957 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 e3ResetInternalS
1d958 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 chema(db, 0);.
1d959 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
1d95a 0a 20 20 20 20 20 20 2f 2a 20 52 65 67 61 72 64 . /* Regard
1d95b 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 less of whether
1d95c 74 68 69 73 20 69 73 20 61 20 52 45 4c 45 41 53 this is a RELEAS
1d95d 45 20 6f 72 20 52 4f 4c 4c 42 41 43 4b 2c 20 64 E or ROLLBACK, d
1d95e 65 73 74 72 6f 79 20 61 6c 6c 0a 20 20 20 20 20 estroy all.
1d95f 20 2a 2a 20 73 61 76 65 70 6f 69 6e 74 73 20 6e ** savepoints n
1d960 65 73 74 65 64 20 69 6e 73 69 64 65 20 6f 66 20 ested inside of
1d961 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20 62 65 the savepoint be
1d962 69 6e 67 20 6f 70 65 72 61 74 65 64 20 6f 6e 2e ing operated on.
1d963 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 */. while(
1d964 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 21 db->pSavepoint!
1d965 3d 75 2e 61 71 2e 70 53 61 76 65 70 6f 69 6e 74 =u.aq.pSavepoint
1d966 20 29 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 71 ){. u.aq
1d967 2e 70 54 6d 70 20 3d 20 64 62 2d 3e 70 53 61 76 .pTmp = db->pSav
1d968 65 70 6f 69 6e 74 3b 0a 20 20 20 20 20 20 20 20 epoint;.
1d969 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 3d db->pSavepoint =
1d96a 20 75 2e 61 71 2e 70 54 6d 70 2d 3e 70 4e 65 78 u.aq.pTmp->pNex
1d96b 74 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 t;. sqlit
1d96c 65 33 44 62 46 72 65 65 28 64 62 2c 20 75 2e 61 e3DbFree(db, u.a
1d96d 71 2e 70 54 6d 70 29 3b 0a 20 20 20 20 20 20 20 q.pTmp);.
1d96e 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 2d db->nSavepoint-
1d96f 2d 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 -;. }..
1d970 20 20 2f 2a 20 49 66 20 69 74 20 69 73 20 61 20 /* If it is a
1d971 52 45 4c 45 41 53 45 2c 20 74 68 65 6e 20 64 65 RELEASE, then de
1d972 73 74 72 6f 79 20 74 68 65 20 73 61 76 65 70 6f stroy the savepo
1d973 69 6e 74 20 62 65 69 6e 67 20 6f 70 65 72 61 74 int being operat
1d974 65 64 20 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 74 ed on. ** t
1d975 6f 6f 2e 20 49 66 20 69 74 20 69 73 20 61 20 52 oo. If it is a R
1d976 4f 4c 4c 42 41 43 4b 20 54 4f 2c 20 74 68 65 6e OLLBACK TO, then
1d977 20 73 65 74 20 74 68 65 20 6e 75 6d 62 65 72 20 set the number
1d978 6f 66 20 64 65 66 65 72 72 65 64 0a 20 20 20 20 of deferred.
1d979 20 20 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20 ** constraint
1d97a 76 69 6f 6c 61 74 69 6f 6e 73 20 70 72 65 73 65 violations prese
1d97b 6e 74 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 nt in the databa
1d97c 73 65 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 se to the value
1d97d 73 74 6f 72 65 64 0a 20 20 20 20 20 20 2a 2a 20 stored. **
1d97e 77 68 65 6e 20 74 68 65 20 73 61 76 65 70 6f 69 when the savepoi
1d97f 6e 74 20 77 61 73 20 63 72 65 61 74 65 64 2e 20 nt was created.
1d980 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 75 2e */. if( u.
1d981 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 aq.p1==SAVEPOINT
1d982 5f 52 45 4c 45 41 53 45 20 29 7b 0a 20 20 20 20 _RELEASE ){.
1d983 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 71 assert( u.aq
1d984 2e 70 53 61 76 65 70 6f 69 6e 74 3d 3d 64 62 2d .pSavepoint==db-
1d985 3e 70 53 61 76 65 70 6f 69 6e 74 20 29 3b 0a 20 >pSavepoint );.
1d986 20 20 20 20 20 20 20 64 62 2d 3e 70 53 61 76 65 db->pSave
1d987 70 6f 69 6e 74 20 3d 20 75 2e 61 71 2e 70 53 61 point = u.aq.pSa
1d988 76 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74 3b 0a vepoint->pNext;.
1d989 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 sqlite3D
1d98a 62 46 72 65 65 28 64 62 2c 20 75 2e 61 71 2e 70 bFree(db, u.aq.p
1d98b 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 Savepoint);.
1d98c 20 20 20 20 69 66 28 20 21 69 73 54 72 61 6e 73 if( !isTrans
1d98d 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 action ){.
1d98e 20 20 20 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69 db->nSavepoi
1d98f 6e 74 2d 2d 3b 0a 20 20 20 20 20 20 20 20 7d 0a nt--;. }.
1d990 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1d991 20 20 20 20 20 64 62 2d 3e 6e 44 65 66 65 72 72 db->nDeferr
1d992 65 64 43 6f 6e 73 20 3d 20 75 2e 61 71 2e 70 53 edCons = u.aq.pS
1d993 61 76 65 70 6f 69 6e 74 2d 3e 6e 44 65 66 65 72 avepoint->nDefer
1d994 72 65 64 43 6f 6e 73 3b 0a 20 20 20 20 20 20 7d redCons;. }
1d995 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 62 72 . }. }.. br
1d996 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1d997 65 3a 20 41 75 74 6f 43 6f 6d 6d 69 74 20 50 31 e: AutoCommit P1
1d998 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * * *.**.**
1d999 53 65 74 20 74 68 65 20 64 61 74 61 62 61 73 65 Set the database
1d99a 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 auto-commit fla
1d99b 67 20 74 6f 20 50 31 20 28 31 20 6f 72 20 30 29 g to P1 (1 or 0)
1d99c 2e 20 49 66 20 50 32 20 69 73 20 74 72 75 65 2c . If P2 is true,
1d99d 20 72 6f 6c 6c 0a 2a 2a 20 62 61 63 6b 20 61 6e roll.** back an
1d99e 79 20 63 75 72 72 65 6e 74 6c 79 20 61 63 74 69 y currently acti
1d99f 76 65 20 62 74 72 65 65 20 74 72 61 6e 73 61 63 ve btree transac
1d9a0 74 69 6f 6e 73 2e 20 49 66 20 74 68 65 72 65 20 tions. If there
1d9a1 61 72 65 20 61 6e 79 20 61 63 74 69 76 65 0a 2a are any active.*
1d9a2 2a 20 56 4d 73 20 28 61 70 61 72 74 20 66 72 6f * VMs (apart fro
1d9a3 6d 20 74 68 69 73 20 6f 6e 65 29 2c 20 74 68 65 m this one), the
1d9a4 6e 20 61 20 52 4f 4c 4c 42 41 43 4b 20 66 61 69 n a ROLLBACK fai
1d9a5 6c 73 2e 20 20 41 20 43 4f 4d 4d 49 54 20 66 61 ls. A COMMIT fa
1d9a6 69 6c 73 20 69 66 0a 2a 2a 20 74 68 65 72 65 20 ils if.** there
1d9a7 61 72 65 20 61 63 74 69 76 65 20 77 72 69 74 69 are active writi
1d9a8 6e 67 20 56 4d 73 20 6f 72 20 61 63 74 69 76 65 ng VMs or active
1d9a9 20 56 4d 73 20 74 68 61 74 20 75 73 65 20 73 68 VMs that use sh
1d9aa 61 72 65 64 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a ared cache..**.*
1d9ab 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 * This instructi
1d9ac 6f 6e 20 63 61 75 73 65 73 20 74 68 65 20 56 4d on causes the VM
1d9ad 20 74 6f 20 68 61 6c 74 2e 0a 2a 2f 0a 63 61 73 to halt..*/.cas
1d9ae 65 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 3a e OP_AutoCommit:
1d9af 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 {.#if 0 /* loc
1d9b0 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1d9b1 65 64 20 69 6e 74 6f 20 75 2e 61 72 20 2a 2f 0a ed into u.ar */.
1d9b2 20 20 69 6e 74 20 64 65 73 69 72 65 64 41 75 74 int desiredAut
1d9b3 6f 43 6f 6d 6d 69 74 3b 0a 20 20 69 6e 74 20 69 oCommit;. int i
1d9b4 52 6f 6c 6c 62 61 63 6b 3b 0a 20 20 69 6e 74 20 Rollback;. int
1d9b5 74 75 72 6e 4f 6e 41 43 3b 0a 23 65 6e 64 69 66 turnOnAC;.#endif
1d9b6 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1d9b7 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1d9b8 2e 61 72 20 2a 2f 0a 0a 20 20 75 2e 61 72 2e 64 .ar */.. u.ar.d
1d9b9 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 esiredAutoCommit
1d9ba 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 2e = pOp->p1;. u.
1d9bb 61 72 2e 69 52 6f 6c 6c 62 61 63 6b 20 3d 20 70 ar.iRollback = p
1d9bc 4f 70 2d 3e 70 32 3b 0a 20 20 75 2e 61 72 2e 74 Op->p2;. u.ar.t
1d9bd 75 72 6e 4f 6e 41 43 20 3d 20 75 2e 61 72 2e 64 urnOnAC = u.ar.d
1d9be 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 esiredAutoCommit
1d9bf 20 26 26 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d && !db->autoCom
1d9c0 6d 69 74 3b 0a 20 20 61 73 73 65 72 74 28 20 75 mit;. assert( u
1d9c1 2e 61 72 2e 64 65 73 69 72 65 64 41 75 74 6f 43 .ar.desiredAutoC
1d9c2 6f 6d 6d 69 74 3d 3d 31 20 7c 7c 20 75 2e 61 72 ommit==1 || u.ar
1d9c3 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d .desiredAutoComm
1d9c4 69 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 it==0 );. asser
1d9c5 74 28 20 75 2e 61 72 2e 64 65 73 69 72 65 64 41 t( u.ar.desiredA
1d9c6 75 74 6f 43 6f 6d 6d 69 74 3d 3d 31 20 7c 7c 20 utoCommit==1 ||
1d9c7 75 2e 61 72 2e 69 52 6f 6c 6c 62 61 63 6b 3d 3d u.ar.iRollback==
1d9c8 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 0 );. assert( d
1d9c9 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 b->activeVdbeCnt
1d9ca 3e 30 20 29 3b 20 20 2f 2a 20 41 74 20 6c 65 61 >0 ); /* At lea
1d9cb 73 74 20 74 68 69 73 20 6f 6e 65 20 56 4d 20 69 st this one VM i
1d9cc 73 20 61 63 74 69 76 65 20 2a 2f 0a 0a 20 20 69 s active */.. i
1d9cd 66 28 20 75 2e 61 72 2e 74 75 72 6e 4f 6e 41 43 f( u.ar.turnOnAC
1d9ce 20 26 26 20 75 2e 61 72 2e 69 52 6f 6c 6c 62 61 && u.ar.iRollba
1d9cf 63 6b 20 26 26 20 64 62 2d 3e 61 63 74 69 76 65 ck && db->active
1d9d0 56 64 62 65 43 6e 74 3e 31 20 29 7b 0a 20 20 20 VdbeCnt>1 ){.
1d9d1 20 2f 2a 20 49 66 20 74 68 69 73 20 69 6e 73 74 /* If this inst
1d9d2 72 75 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e ruction implemen
1d9d3 74 73 20 61 20 52 4f 4c 4c 42 41 43 4b 20 61 6e ts a ROLLBACK an
1d9d4 64 20 6f 74 68 65 72 20 56 4d 73 20 61 72 65 0a d other VMs are.
1d9d5 20 20 20 20 2a 2a 20 73 74 69 6c 6c 20 72 75 6e ** still run
1d9d6 6e 69 6e 67 2c 20 61 6e 64 20 61 20 74 72 61 6e ning, and a tran
1d9d7 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 saction is activ
1d9d8 65 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 e, return an err
1d9d9 6f 72 20 69 6e 64 69 63 61 74 69 6e 67 0a 20 20 or indicating.
1d9da 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 6f 74 ** that the ot
1d9db 68 65 72 20 56 4d 73 20 6d 75 73 74 20 63 6f 6d her VMs must com
1d9dc 70 6c 65 74 65 20 66 69 72 73 74 2e 0a 20 20 20 plete first..
1d9dd 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 53 */. sqlite3S
1d9de 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 etString(&p->zEr
1d9df 72 4d 73 67 2c 20 64 62 2c 20 22 63 61 6e 6e 6f rMsg, db, "canno
1d9e0 74 20 72 6f 6c 6c 62 61 63 6b 20 74 72 61 6e 73 t rollback trans
1d9e1 61 63 74 69 6f 6e 20 2d 20 22 0a 20 20 20 20 20 action - ".
1d9e2 20 20 20 22 53 51 4c 20 73 74 61 74 65 6d 65 6e "SQL statemen
1d9e3 74 73 20 69 6e 20 70 72 6f 67 72 65 73 73 22 29 ts in progress")
1d9e4 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 ;. rc = SQLIT
1d9e5 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73 65 20 E_BUSY;. }else
1d9e6 69 66 28 20 75 2e 61 72 2e 74 75 72 6e 4f 6e 41 if( u.ar.turnOnA
1d9e7 43 20 26 26 20 21 75 2e 61 72 2e 69 52 6f 6c 6c C && !u.ar.iRoll
1d9e8 62 61 63 6b 20 26 26 20 64 62 2d 3e 77 72 69 74 back && db->writ
1d9e9 65 56 64 62 65 43 6e 74 3e 30 20 29 7b 0a 20 20 eVdbeCnt>0 ){.
1d9ea 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 6e 73 /* If this ins
1d9eb 74 72 75 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 truction impleme
1d9ec 6e 74 73 20 61 20 43 4f 4d 4d 49 54 20 61 6e 64 nts a COMMIT and
1d9ed 20 6f 74 68 65 72 20 56 4d 73 20 61 72 65 20 77 other VMs are w
1d9ee 72 69 74 69 6e 67 0a 20 20 20 20 2a 2a 20 72 65 riting. ** re
1d9ef 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 69 6e turn an error in
1d9f0 64 69 63 61 74 69 6e 67 20 74 68 61 74 20 74 68 dicating that th
1d9f1 65 20 6f 74 68 65 72 20 56 4d 73 20 6d 75 73 74 e other VMs must
1d9f2 20 63 6f 6d 70 6c 65 74 65 20 66 69 72 73 74 2e complete first.
1d9f3 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 . */. sqli
1d9f4 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d te3SetString(&p-
1d9f5 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 63 >zErrMsg, db, "c
1d9f6 61 6e 6e 6f 74 20 63 6f 6d 6d 69 74 20 74 72 61 annot commit tra
1d9f7 6e 73 61 63 74 69 6f 6e 20 2d 20 22 0a 20 20 20 nsaction - ".
1d9f8 20 20 20 20 20 22 53 51 4c 20 73 74 61 74 65 6d "SQL statem
1d9f9 65 6e 74 73 20 69 6e 20 70 72 6f 67 72 65 73 73 ents in progress
1d9fa 22 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ");. rc = SQL
1d9fb 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73 ITE_BUSY;. }els
1d9fc 65 20 69 66 28 20 75 2e 61 72 2e 64 65 73 69 72 e if( u.ar.desir
1d9fd 65 64 41 75 74 6f 43 6f 6d 6d 69 74 21 3d 64 62 edAutoCommit!=db
1d9fe 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a ->autoCommit ){.
1d9ff 20 20 20 20 69 66 28 20 75 2e 61 72 2e 69 52 6f if( u.ar.iRo
1da00 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 20 20 llback ){.
1da01 61 73 73 65 72 74 28 20 75 2e 61 72 2e 64 65 73 assert( u.ar.des
1da02 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d iredAutoCommit==
1da03 31 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 1 );. sqlit
1da04 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 e3RollbackAll(db
1da05 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 61 75 74 );. db->aut
1da06 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 oCommit = 1;.
1da07 20 7d 65 6c 73 65 20 69 66 28 20 28 72 63 20 3d }else if( (rc =
1da08 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 65 63 sqlite3VdbeChec
1da09 6b 46 6b 28 70 2c 20 31 29 29 21 3d 53 51 4c 49 kFk(p, 1))!=SQLI
1da0a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 TE_OK ){. g
1da0b 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b oto vdbe_return;
1da0c 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
1da0d 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 db->autoCommit
1da0e 20 3d 20 28 75 38 29 75 2e 61 72 2e 64 65 73 69 = (u8)u.ar.desi
1da0f 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3b 0a 20 redAutoCommit;.
1da10 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
1da11 56 64 62 65 48 61 6c 74 28 70 29 3d 3d 53 51 4c VdbeHalt(p)==SQL
1da12 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 ITE_BUSY ){.
1da13 20 20 20 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a p->pc = pc;.
1da14 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f db->auto
1da15 43 6f 6d 6d 69 74 20 3d 20 28 75 38 29 28 31 2d Commit = (u8)(1-
1da16 75 2e 61 72 2e 64 65 73 69 72 65 64 41 75 74 6f u.ar.desiredAuto
1da17 43 6f 6d 6d 69 74 29 3b 0a 20 20 20 20 20 20 20 Commit);.
1da18 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53 51 p->rc = rc = SQ
1da19 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 LITE_BUSY;.
1da1a 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 goto vdbe_ret
1da1b 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 urn;. }.
1da1c 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 }. assert( d
1da1d 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3d 3d 30 b->nStatement==0
1da1e 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 );. sqlite3C
1da1f 6c 6f 73 65 53 61 76 65 70 6f 69 6e 74 73 28 64 loseSavepoints(d
1da20 62 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 72 b);. if( p->r
1da21 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
1da22 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
1da23 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 65 6c 73 E_DONE;. }els
1da24 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 e{. rc = SQ
1da25 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 LITE_ERROR;.
1da26 7d 0a 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f }. goto vdbe_
1da27 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b return;. }else{
1da28 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 . sqlite3SetS
1da29 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 tring(&p->zErrMs
1da2a 67 2c 20 64 62 2c 0a 20 20 20 20 20 20 20 20 28 g, db,. (
1da2b 21 75 2e 61 72 2e 64 65 73 69 72 65 64 41 75 74 !u.ar.desiredAut
1da2c 6f 43 6f 6d 6d 69 74 29 3f 22 63 61 6e 6e 6f 74 oCommit)?"cannot
1da2d 20 73 74 61 72 74 20 61 20 74 72 61 6e 73 61 63 start a transac
1da2e 74 69 6f 6e 20 77 69 74 68 69 6e 20 61 20 74 72 tion within a tr
1da2f 61 6e 73 61 63 74 69 6f 6e 22 3a 28 0a 20 20 20 ansaction":(.
1da30 20 20 20 20 20 28 75 2e 61 72 2e 69 52 6f 6c 6c (u.ar.iRoll
1da31 62 61 63 6b 29 3f 22 63 61 6e 6e 6f 74 20 72 6f back)?"cannot ro
1da32 6c 6c 62 61 63 6b 20 2d 20 6e 6f 20 74 72 61 6e llback - no tran
1da33 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 saction is activ
1da34 65 22 3a 0a 20 20 20 20 20 20 20 20 20 20 20 20 e":.
1da35 20 20 20 20 20 20 20 22 63 61 6e 6e 6f 74 20 63 "cannot c
1da36 6f 6d 6d 69 74 20 2d 20 6e 6f 20 74 72 61 6e 73 ommit - no trans
1da37 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 action is active
1da38 22 29 29 3b 0a 0a 20 20 20 20 72 63 20 3d 20 53 "));.. rc = S
1da39 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d QLITE_ERROR;. }
1da3a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1da3b 4f 70 63 6f 64 65 3a 20 54 72 61 6e 73 61 63 74 Opcode: Transact
1da3c 69 6f 6e 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a ion P1 P2 * * *.
1da3d 2a 2a 0a 2a 2a 20 42 65 67 69 6e 20 61 20 74 72 **.** Begin a tr
1da3e 61 6e 73 61 63 74 69 6f 6e 2e 20 20 54 68 65 20 ansaction. The
1da3f 74 72 61 6e 73 61 63 74 69 6f 6e 20 65 6e 64 73 transaction ends
1da40 20 77 68 65 6e 20 61 20 43 6f 6d 6d 69 74 20 6f when a Commit o
1da41 72 20 52 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6f 70 r Rollback.** op
1da42 63 6f 64 65 20 69 73 20 65 6e 63 6f 75 6e 74 65 code is encounte
1da43 72 65 64 2e 20 20 44 65 70 65 6e 64 69 6e 67 20 red. Depending
1da44 6f 6e 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 on the ON CONFLI
1da45 43 54 20 73 65 74 74 69 6e 67 2c 20 74 68 65 0a CT setting, the.
1da46 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d ** transaction m
1da47 69 67 68 74 20 61 6c 73 6f 20 62 65 20 72 6f 6c ight also be rol
1da48 6c 65 64 20 62 61 63 6b 20 69 66 20 61 6e 20 65 led back if an e
1da49 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 rror is encounte
1da4a 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 red..**.** P1 is
1da4b 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 the index of th
1da4c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
1da4d 6f 6e 20 77 68 69 63 68 20 74 68 65 20 74 72 61 on which the tra
1da4e 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 73 nsaction is.** s
1da4f 74 61 72 74 65 64 2e 20 20 49 6e 64 65 78 20 30 tarted. Index 0
1da50 20 69 73 20 74 68 65 20 6d 61 69 6e 20 64 61 74 is the main dat
1da51 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 69 abase file and i
1da52 6e 64 65 78 20 31 20 69 73 20 74 68 65 0a 2a 2a ndex 1 is the.**
1da53 20 66 69 6c 65 20 75 73 65 64 20 66 6f 72 20 74 file used for t
1da54 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e emporary tables.
1da55 20 20 49 6e 64 69 63 65 73 20 6f 66 20 32 20 6f Indices of 2 o
1da56 72 20 6d 6f 72 65 20 61 72 65 20 75 73 65 64 20 r more are used
1da57 66 6f 72 0a 2a 2a 20 61 74 74 61 63 68 65 64 20 for.** attached
1da58 64 61 74 61 62 61 73 65 73 2e 0a 2a 2a 0a 2a 2a databases..**.**
1da59 20 49 66 20 50 32 20 69 73 20 6e 6f 6e 2d 7a 65 If P2 is non-ze
1da5a 72 6f 2c 20 74 68 65 6e 20 61 20 77 72 69 74 65 ro, then a write
1da5b 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 -transaction is
1da5c 73 74 61 72 74 65 64 2e 20 20 41 20 52 45 53 45 started. A RESE
1da5d 52 56 45 44 20 6c 6f 63 6b 20 69 73 0a 2a 2a 20 RVED lock is.**
1da5e 6f 62 74 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 obtained on the
1da5f 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 68 database file wh
1da60 65 6e 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 en a write-trans
1da61 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74 65 action is starte
1da62 64 2e 20 20 4e 6f 0a 2a 2a 20 6f 74 68 65 72 20 d. No.** other
1da63 70 72 6f 63 65 73 73 20 63 61 6e 20 73 74 61 72 process can star
1da64 74 20 61 6e 6f 74 68 65 72 20 77 72 69 74 65 20 t another write
1da65 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 68 69 6c transaction whil
1da66 65 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 e this transacti
1da67 6f 6e 20 69 73 0a 2a 2a 20 75 6e 64 65 72 77 61 on is.** underwa
1da68 79 2e 20 20 53 74 61 72 74 69 6e 67 20 61 20 77 y. Starting a w
1da69 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e rite transaction
1da6a 20 61 6c 73 6f 20 63 72 65 61 74 65 73 20 61 20 also creates a
1da6b 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c rollback journal
1da6c 2e 20 41 0a 2a 2a 20 77 72 69 74 65 20 74 72 61 . A.** write tra
1da6d 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 nsaction must be
1da6e 20 73 74 61 72 74 65 64 20 62 65 66 6f 72 65 20 started before
1da6f 61 6e 79 20 63 68 61 6e 67 65 73 20 63 61 6e 20 any changes can
1da70 62 65 20 6d 61 64 65 20 74 6f 20 74 68 65 0a 2a be made to the.*
1da71 2a 20 64 61 74 61 62 61 73 65 2e 20 20 49 66 20 * database. If
1da72 50 32 20 69 73 20 32 20 6f 72 20 67 72 65 61 74 P2 is 2 or great
1da73 65 72 20 74 68 65 6e 20 61 6e 20 45 58 43 4c 55 er then an EXCLU
1da74 53 49 56 45 20 6c 6f 63 6b 20 69 73 20 61 6c 73 SIVE lock is als
1da75 6f 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 6f 6e o obtained.** on
1da76 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a the file..**.**
1da77 20 49 66 20 61 20 77 72 69 74 65 2d 74 72 61 6e If a write-tran
1da78 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74 saction is start
1da79 65 64 20 61 6e 64 20 74 68 65 20 56 64 62 65 2e ed and the Vdbe.
1da7a 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 usesStmtJournal
1da7b 66 6c 61 67 20 69 73 0a 2a 2a 20 74 72 75 65 20 flag is.** true
1da7c 28 74 68 69 73 20 66 6c 61 67 20 69 73 20 73 65 (this flag is se
1da7d 74 20 69 66 20 74 68 65 20 56 64 62 65 20 6d 61 t if the Vdbe ma
1da7e 79 20 6d 6f 64 69 66 79 20 6d 6f 72 65 20 74 68 y modify more th
1da7f 61 6e 20 6f 6e 65 20 72 6f 77 20 61 6e 64 20 6d an one row and m
1da80 61 79 0a 2a 2a 20 74 68 72 6f 77 20 61 6e 20 41 ay.** throw an A
1da81 42 4f 52 54 20 65 78 63 65 70 74 69 6f 6e 29 2c BORT exception),
1da82 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 a statement tra
1da83 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 61 6c 73 nsaction may als
1da84 6f 20 62 65 20 6f 70 65 6e 65 64 2e 0a 2a 2a 20 o be opened..**
1da85 4d 6f 72 65 20 73 70 65 63 69 66 69 63 61 6c 6c More specificall
1da86 79 2c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 y, a statement t
1da87 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 ransaction is op
1da88 65 6e 65 64 20 69 66 66 20 74 68 65 20 64 61 74 ened iff the dat
1da89 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 abase.** connect
1da8a 69 6f 6e 20 69 73 20 63 75 72 72 65 6e 74 6c 79 ion is currently
1da8b 20 6e 6f 74 20 69 6e 20 61 75 74 6f 63 6f 6d 6d not in autocomm
1da8c 69 74 20 6d 6f 64 65 2c 20 6f 72 20 69 66 20 74 it mode, or if t
1da8d 68 65 72 65 20 61 72 65 20 6f 74 68 65 72 0a 2a here are other.*
1da8e 2a 20 61 63 74 69 76 65 20 73 74 61 74 65 6d 65 * active stateme
1da8f 6e 74 73 2e 20 41 20 73 74 61 74 65 6d 65 6e 74 nts. A statement
1da90 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6c 6c transaction all
1da91 6f 77 73 20 74 68 65 20 61 66 66 65 63 74 73 20 ows the affects
1da92 6f 66 20 74 68 69 73 0a 2a 2a 20 56 44 42 45 20 of this.** VDBE
1da93 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 to be rolled bac
1da94 6b 20 61 66 74 65 72 20 61 6e 20 65 72 72 6f 72 k after an error
1da95 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 without having
1da96 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 74 68 65 to roll back the
1da97 0a 2a 2a 20 65 6e 74 69 72 65 20 74 72 61 6e 73 .** entire trans
1da98 61 63 74 69 6f 6e 2e 20 49 66 20 6e 6f 20 65 72 action. If no er
1da99 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 ror is encounter
1da9a 65 64 2c 20 74 68 65 20 73 74 61 74 65 6d 65 6e ed, the statemen
1da9b 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a t transaction.**
1da9c 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61 will automatica
1da9d 6c 6c 79 20 63 6f 6d 6d 69 74 20 77 68 65 6e 20 lly commit when
1da9e 74 68 65 20 56 44 42 45 20 68 61 6c 74 73 2e 0a the VDBE halts..
1da9f 2a 2a 0a 2a 2a 20 49 66 20 50 32 20 69 73 20 7a **.** If P2 is z
1daa0 65 72 6f 2c 20 74 68 65 6e 20 61 20 72 65 61 64 ero, then a read
1daa1 2d 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 -lock is obtaine
1daa2 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 d on the databas
1daa3 65 20 66 69 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 e file..*/.case
1daa4 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 3a 20 OP_Transaction:
1daa5 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 {.#if 0 /* loca
1daa6 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1daa7 64 20 69 6e 74 6f 20 75 2e 61 73 20 2a 2f 0a 20 d into u.as */.
1daa8 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 23 65 6e Btree *pBt;.#en
1daa9 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 dif /* local var
1daaa 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1daab 6f 20 75 2e 61 73 20 2a 2f 0a 0a 20 20 61 73 73 o u.as */.. ass
1daac 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 ert( pOp->p1>=0
1daad 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e && pOp->p1<db->n
1daae 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 Db );. assert(
1daaf 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 (p->btreeMask &
1dab0 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 (1<<pOp->p1))!=0
1dab1 20 29 3b 0a 20 20 75 2e 61 73 2e 70 42 74 20 3d );. u.as.pBt =
1dab2 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 db->aDb[pOp->p1
1dab3 5d 2e 70 42 74 3b 0a 0a 20 20 69 66 28 20 75 2e ].pBt;.. if( u.
1dab4 61 73 2e 70 42 74 20 29 7b 0a 20 20 20 20 72 63 as.pBt ){. rc
1dab5 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 = sqlite3BtreeB
1dab6 65 67 69 6e 54 72 61 6e 73 28 75 2e 61 73 2e 70 eginTrans(u.as.p
1dab7 42 74 2c 20 70 4f 70 2d 3e 70 32 29 3b 0a 20 20 Bt, pOp->p2);.
1dab8 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1dab9 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 20 20 70 _BUSY ){. p
1daba 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20 20 20 ->pc = pc;.
1dabb 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53 51 p->rc = rc = SQ
1dabc 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 LITE_BUSY;.
1dabd 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 goto vdbe_retur
1dabe 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 n;. }. if(
1dabf 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
1dac0 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f {. goto abo
1dac1 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b rt_due_to_error;
1dac2 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 . }.. if(
1dac3 70 4f 70 2d 3e 70 32 20 26 26 20 70 2d 3e 75 73 pOp->p2 && p->us
1dac4 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 0a 20 20 esStmtJournal.
1dac5 20 20 20 26 26 20 28 64 62 2d 3e 61 75 74 6f 43 && (db->autoC
1dac6 6f 6d 6d 69 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e ommit==0 || db->
1dac7 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e 31 29 activeVdbeCnt>1)
1dac8 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 61 73 . ){. as
1dac9 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 sert( sqlite3Btr
1daca 65 65 49 73 49 6e 54 72 61 6e 73 28 75 2e 61 73 eeIsInTrans(u.as
1dacb 2e 70 42 74 29 20 29 3b 0a 20 20 20 20 20 20 69 .pBt) );. i
1dacc 66 28 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 f( p->iStatement
1dacd 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 ==0 ){. a
1dace 73 73 65 72 74 28 20 64 62 2d 3e 6e 53 74 61 74 ssert( db->nStat
1dacf 65 6d 65 6e 74 3e 3d 30 20 26 26 20 64 62 2d 3e ement>=0 && db->
1dad0 6e 53 61 76 65 70 6f 69 6e 74 3e 3d 30 20 29 3b nSavepoint>=0 );
1dad1 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6e 53 74 . db->nSt
1dad2 61 74 65 6d 65 6e 74 2b 2b 3b 0a 20 20 20 20 20 atement++;.
1dad3 20 20 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 p->iStatement
1dad4 20 3d 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e = db->nSavepoin
1dad5 74 20 2b 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 t + db->nStateme
1dad6 6e 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 nt;. }.
1dad7 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1dad8 72 65 65 42 65 67 69 6e 53 74 6d 74 28 75 2e 61 reeBeginStmt(u.a
1dad9 73 2e 70 42 74 2c 20 70 2d 3e 69 53 74 61 74 65 s.pBt, p->iState
1dada 6d 65 6e 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a ment);.. /*
1dadb 20 53 74 6f 72 65 20 74 68 65 20 63 75 72 72 65 Store the curre
1dadc 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 nt value of the
1dadd 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 73 database handles
1dade 20 64 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72 deferred constr
1dadf 61 69 6e 74 0a 20 20 20 20 20 20 2a 2a 20 63 6f aint. ** co
1dae0 75 6e 74 65 72 2e 20 49 66 20 74 68 65 20 73 74 unter. If the st
1dae1 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 atement transact
1dae2 69 6f 6e 20 6e 65 65 64 73 20 74 6f 20 62 65 20 ion needs to be
1dae3 72 6f 6c 6c 65 64 20 62 61 63 6b 2c 0a 20 20 20 rolled back,.
1dae4 20 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 ** the value
1dae5 6f 66 20 74 68 69 73 20 63 6f 75 6e 74 65 72 20 of this counter
1dae6 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 73 74 needs to be rest
1dae7 6f 72 65 64 20 74 6f 6f 2e 20 20 2a 2f 0a 20 20 ored too. */.
1dae8 20 20 20 20 70 2d 3e 6e 53 74 6d 74 44 65 66 43 p->nStmtDefC
1dae9 6f 6e 73 20 3d 20 64 62 2d 3e 6e 44 65 66 65 72 ons = db->nDefer
1daea 72 65 64 43 6f 6e 73 3b 0a 20 20 20 20 7d 0a 20 redCons;. }.
1daeb 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f }. break;.}../
1daec 2a 20 4f 70 63 6f 64 65 3a 20 52 65 61 64 43 6f * Opcode: ReadCo
1daed 6f 6b 69 65 20 50 31 20 50 32 20 50 33 20 2a 20 okie P1 P2 P3 *
1daee 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 63 6f 6f *.**.** Read coo
1daef 6b 69 65 20 6e 75 6d 62 65 72 20 50 33 20 66 72 kie number P3 fr
1daf0 6f 6d 20 64 61 74 61 62 61 73 65 20 50 31 20 61 om database P1 a
1daf1 6e 64 20 77 72 69 74 65 20 69 74 20 69 6e 74 6f nd write it into
1daf2 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a register P2..**
1daf3 20 50 33 3d 3d 31 20 69 73 20 74 68 65 20 73 63 P3==1 is the sc
1daf4 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e 20 20 50 hema version. P
1daf5 33 3d 3d 32 20 69 73 20 74 68 65 20 64 61 74 61 3==2 is the data
1daf6 62 61 73 65 20 66 6f 72 6d 61 74 2e 0a 2a 2a 20 base format..**
1daf7 50 33 3d 3d 33 20 69 73 20 74 68 65 20 72 65 63 P3==3 is the rec
1daf8 6f 6d 6d 65 6e 64 65 64 20 70 61 67 65 72 20 63 ommended pager c
1daf9 61 63 68 65 20 73 69 7a 65 2c 20 61 6e 64 20 73 ache size, and s
1dafa 6f 20 66 6f 72 74 68 2e 20 20 50 31 3d 3d 30 20 o forth. P1==0
1dafb 69 73 0a 2a 2a 20 74 68 65 20 6d 61 69 6e 20 64 is.** the main d
1dafc 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 atabase file and
1dafd 20 50 31 3d 3d 31 20 69 73 20 74 68 65 20 64 61 P1==1 is the da
1dafe 74 61 62 61 73 65 20 66 69 6c 65 20 75 73 65 64 tabase file used
1daff 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 74 65 6d to store.** tem
1db00 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 0a 2a porary tables..*
1db01 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 74 20 *.** There must
1db02 62 65 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f be a read-lock o
1db03 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 28 n the database (
1db04 65 69 74 68 65 72 20 61 20 74 72 61 6e 73 61 63 either a transac
1db05 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 62 65 20 tion.** must be
1db06 73 74 61 72 74 65 64 20 6f 72 20 74 68 65 72 65 started or there
1db07 20 6d 75 73 74 20 62 65 20 61 6e 20 6f 70 65 6e must be an open
1db08 20 63 75 72 73 6f 72 29 20 62 65 66 6f 72 65 0a cursor) before.
1db09 2a 2a 20 65 78 65 63 75 74 69 6e 67 20 74 68 69 ** executing thi
1db0a 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a s instruction..*
1db0b 2f 0a 63 61 73 65 20 4f 50 5f 52 65 61 64 43 6f /.case OP_ReadCo
1db0c 6f 6b 69 65 3a 20 7b 20 20 20 20 20 20 20 20 20 okie: {
1db0d 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 /* out2-pr
1db0e 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 erelease */.#if
1db0f 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1db10 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1db11 20 75 2e 61 74 20 2a 2f 0a 20 20 69 6e 74 20 69 u.at */. int i
1db12 4d 65 74 61 3b 0a 20 20 69 6e 74 20 69 44 62 3b Meta;. int iDb;
1db13 0a 20 20 69 6e 74 20 69 43 6f 6f 6b 69 65 3b 0a . int iCookie;.
1db14 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 #endif /* local
1db15 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1db16 69 6e 74 6f 20 75 2e 61 74 20 2a 2f 0a 0a 20 20 into u.at */..
1db17 75 2e 61 74 2e 69 44 62 20 3d 20 70 4f 70 2d 3e u.at.iDb = pOp->
1db18 70 31 3b 0a 20 20 75 2e 61 74 2e 69 43 6f 6f 6b p1;. u.at.iCook
1db19 69 65 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 ie = pOp->p3;.
1db1a 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c assert( pOp->p3<
1db1b 53 51 4c 49 54 45 5f 4e 5f 42 54 52 45 45 5f 4d SQLITE_N_BTREE_M
1db1c 45 54 41 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ETA );. assert(
1db1d 20 75 2e 61 74 2e 69 44 62 3e 3d 30 20 26 26 20 u.at.iDb>=0 &&
1db1e 75 2e 61 74 2e 69 44 62 3c 64 62 2d 3e 6e 44 62 u.at.iDb<db->nDb
1db1f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 );. assert( db
1db20 2d 3e 61 44 62 5b 75 2e 61 74 2e 69 44 62 5d 2e ->aDb[u.at.iDb].
1db21 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 pBt!=0 );. asse
1db22 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 rt( (p->btreeMas
1db23 6b 20 26 20 28 31 3c 3c 75 2e 61 74 2e 69 44 62 k & (1<<u.at.iDb
1db24 29 29 21 3d 30 20 29 3b 0a 0a 20 20 73 71 6c 69 ))!=0 );.. sqli
1db25 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 te3BtreeGetMeta(
1db26 64 62 2d 3e 61 44 62 5b 75 2e 61 74 2e 69 44 62 db->aDb[u.at.iDb
1db27 5d 2e 70 42 74 2c 20 75 2e 61 74 2e 69 43 6f 6f ].pBt, u.at.iCoo
1db28 6b 69 65 2c 20 28 75 33 32 20 2a 29 26 75 2e 61 kie, (u32 *)&u.a
1db29 74 2e 69 4d 65 74 61 29 3b 0a 20 20 70 4f 75 74 t.iMeta);. pOut
1db2a 2d 3e 75 2e 69 20 3d 20 75 2e 61 74 2e 69 4d 65 ->u.i = u.at.iMe
1db2b 74 61 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 ta;. MemSetType
1db2c 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 Flag(pOut, MEM_I
1db2d 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a nt);. break;.}.
1db2e 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 74 43 ./* Opcode: SetC
1db2f 6f 6f 6b 69 65 20 50 31 20 50 32 20 50 33 20 2a ookie P1 P2 P3 *
1db30 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 *.**.** Write t
1db31 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 he content of re
1db32 67 69 73 74 65 72 20 50 33 20 28 69 6e 74 65 72 gister P3 (inter
1db33 70 72 65 74 65 64 20 61 73 20 61 6e 20 69 6e 74 preted as an int
1db34 65 67 65 72 29 0a 2a 2a 20 69 6e 74 6f 20 63 6f eger).** into co
1db35 6f 6b 69 65 20 6e 75 6d 62 65 72 20 50 32 20 6f okie number P2 o
1db36 66 20 64 61 74 61 62 61 73 65 20 50 31 2e 20 20 f database P1.
1db37 50 32 3d 3d 31 20 69 73 20 74 68 65 20 73 63 68 P2==1 is the sch
1db38 65 6d 61 20 76 65 72 73 69 6f 6e 2e 20 20 0a 2a ema version. .*
1db39 2a 20 50 32 3d 3d 32 20 69 73 20 74 68 65 20 64 * P2==2 is the d
1db3a 61 74 61 62 61 73 65 20 66 6f 72 6d 61 74 2e 20 atabase format.
1db3b 50 32 3d 3d 33 20 69 73 20 74 68 65 20 72 65 63 P2==3 is the rec
1db3c 6f 6d 6d 65 6e 64 65 64 20 70 61 67 65 72 20 63 ommended pager c
1db3d 61 63 68 65 20 0a 2a 2a 20 73 69 7a 65 2c 20 61 ache .** size, a
1db3e 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 50 31 nd so forth. P1
1db3f 3d 3d 30 20 69 73 20 74 68 65 20 6d 61 69 6e 20 ==0 is the main
1db40 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e database file an
1db41 64 20 50 31 3d 3d 31 20 69 73 20 74 68 65 20 0a d P1==1 is the .
1db42 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ** database file
1db43 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 used to store t
1db44 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e emporary tables.
1db45 0a 2a 2a 0a 2a 2a 20 41 20 74 72 61 6e 73 61 63 .**.** A transac
1db46 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 73 74 61 tion must be sta
1db47 72 74 65 64 20 62 65 66 6f 72 65 20 65 78 65 63 rted before exec
1db48 75 74 69 6e 67 20 74 68 69 73 20 6f 70 63 6f 64 uting this opcod
1db49 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 e..*/.case OP_Se
1db4a 74 43 6f 6f 6b 69 65 3a 20 7b 20 20 20 20 20 20 tCookie: {
1db4b 20 2f 2a 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 /* in3 */.#if 0
1db4c 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 /* local varia
1db4d 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1db4e 75 2e 61 75 20 2a 2f 0a 20 20 44 62 20 2a 70 44 u.au */. Db *pD
1db4f 62 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 b;.#endif /* loc
1db50 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1db51 65 64 20 69 6e 74 6f 20 75 2e 61 75 20 2a 2f 0a ed into u.au */.
1db52 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1db53 32 3c 53 51 4c 49 54 45 5f 4e 5f 42 54 52 45 45 2<SQLITE_N_BTREE
1db54 5f 4d 45 54 41 20 29 3b 0a 20 20 61 73 73 65 72 _META );. asser
1db55 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 t( pOp->p1>=0 &&
1db56 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 pOp->p1<db->nDb
1db57 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 );. assert( (p
1db58 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 ->btreeMask & (1
1db59 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 <<pOp->p1))!=0 )
1db5a 3b 0a 20 20 75 2e 61 75 2e 70 44 62 20 3d 20 26 ;. u.au.pDb = &
1db5b 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d db->aDb[pOp->p1]
1db5c 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 75 ;. assert( u.au
1db5d 2e 70 44 62 2d 3e 70 42 74 21 3d 30 20 29 3b 0a .pDb->pBt!=0 );.
1db5e 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1db5f 49 6e 74 65 67 65 72 69 66 79 28 70 49 6e 33 29 Integerify(pIn3)
1db60 3b 0a 20 20 2f 2a 20 53 65 65 20 6e 6f 74 65 20 ;. /* See note
1db61 61 62 6f 75 74 20 69 6e 64 65 78 20 73 68 69 66 about index shif
1db62 74 69 6e 67 20 6f 6e 20 4f 50 5f 52 65 61 64 43 ting on OP_ReadC
1db63 6f 6f 6b 69 65 20 2a 2f 0a 20 20 72 63 20 3d 20 ookie */. rc =
1db64 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 sqlite3BtreeUpda
1db65 74 65 4d 65 74 61 28 75 2e 61 75 2e 70 44 62 2d teMeta(u.au.pDb-
1db66 3e 70 42 74 2c 20 70 4f 70 2d 3e 70 32 2c 20 28 >pBt, pOp->p2, (
1db67 69 6e 74 29 70 49 6e 33 2d 3e 75 2e 69 29 3b 0a int)pIn3->u.i);.
1db68 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 42 if( pOp->p2==B
1db69 54 52 45 45 5f 53 43 48 45 4d 41 5f 56 45 52 53 TREE_SCHEMA_VERS
1db6a 49 4f 4e 20 29 7b 0a 20 20 20 20 2f 2a 20 57 68 ION ){. /* Wh
1db6b 65 6e 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f en the schema co
1db6c 6f 6b 69 65 20 63 68 61 6e 67 65 73 2c 20 72 65 okie changes, re
1db6d 63 6f 72 64 20 74 68 65 20 6e 65 77 20 63 6f 6f cord the new coo
1db6e 6b 69 65 20 69 6e 74 65 72 6e 61 6c 6c 79 20 2a kie internally *
1db6f 2f 0a 20 20 20 20 75 2e 61 75 2e 70 44 62 2d 3e /. u.au.pDb->
1db70 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f pSchema->schema_
1db71 63 6f 6f 6b 69 65 20 3d 20 28 69 6e 74 29 70 49 cookie = (int)pI
1db72 6e 33 2d 3e 75 2e 69 3b 0a 20 20 20 20 64 62 2d n3->u.i;. db-
1db73 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 >flags |= SQLITE
1db74 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a _InternChanges;.
1db75 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 70 2d }else if( pOp-
1db76 3e 70 32 3d 3d 42 54 52 45 45 5f 46 49 4c 45 5f >p2==BTREE_FILE_
1db77 46 4f 52 4d 41 54 20 29 7b 0a 20 20 20 20 2f 2a FORMAT ){. /*
1db78 20 52 65 63 6f 72 64 20 63 68 61 6e 67 65 73 20 Record changes
1db79 69 6e 20 74 68 65 20 66 69 6c 65 20 66 6f 72 6d in the file form
1db7a 61 74 20 2a 2f 0a 20 20 20 20 75 2e 61 75 2e 70 at */. u.au.p
1db7b 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c Db->pSchema->fil
1db7c 65 5f 66 6f 72 6d 61 74 20 3d 20 28 75 38 29 70 e_format = (u8)p
1db7d 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 7d 0a 20 20 In3->u.i;. }.
1db7e 69 66 28 20 70 4f 70 2d 3e 70 31 3d 3d 31 20 29 if( pOp->p1==1 )
1db7f 7b 0a 20 20 20 20 2f 2a 20 49 6e 76 61 6c 69 64 {. /* Invalid
1db80 61 74 65 20 61 6c 6c 20 70 72 65 70 61 72 65 64 ate all prepared
1db81 20 73 74 61 74 65 6d 65 6e 74 73 20 77 68 65 6e statements when
1db82 65 76 65 72 20 74 68 65 20 54 45 4d 50 20 64 61 ever the TEMP da
1db83 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 73 63 tabase. ** sc
1db84 68 65 6d 61 20 69 73 20 63 68 61 6e 67 65 64 2e hema is changed.
1db85 20 20 54 69 63 6b 65 74 20 23 31 36 34 34 20 2a Ticket #1644 *
1db86 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 /. sqlite3Exp
1db87 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74 65 irePreparedState
1db88 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 20 20 70 ments(db);. p
1db89 2d 3e 65 78 70 69 72 65 64 20 3d 20 30 3b 0a 20 ->expired = 0;.
1db8a 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f }. break;.}../
1db8b 2a 20 4f 70 63 6f 64 65 3a 20 56 65 72 69 66 79 * Opcode: Verify
1db8c 43 6f 6f 6b 69 65 20 50 31 20 50 32 20 2a 0a 2a Cookie P1 P2 *.*
1db8d 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 65 20 76 *.** Check the v
1db8e 61 6c 75 65 20 6f 66 20 67 6c 6f 62 61 6c 20 64 alue of global d
1db8f 61 74 61 62 61 73 65 20 70 61 72 61 6d 65 74 65 atabase paramete
1db90 72 20 6e 75 6d 62 65 72 20 30 20 28 74 68 65 0a r number 0 (the.
1db91 2a 2a 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f ** schema versio
1db92 6e 29 20 61 6e 64 20 6d 61 6b 65 20 73 75 72 65 n) and make sure
1db93 20 69 74 20 69 73 20 65 71 75 61 6c 20 74 6f 20 it is equal to
1db94 50 32 2e 20 20 0a 2a 2a 20 50 31 20 69 73 20 74 P2. .** P1 is t
1db95 68 65 20 64 61 74 61 62 61 73 65 20 6e 75 6d 62 he database numb
1db96 65 72 20 77 68 69 63 68 20 69 73 20 30 20 66 6f er which is 0 fo
1db97 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 r the main datab
1db98 61 73 65 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 ase file.** and
1db99 31 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20 68 1 for the file h
1db9a 6f 6c 64 69 6e 67 20 74 65 6d 70 6f 72 61 72 79 olding temporary
1db9b 20 74 61 62 6c 65 73 20 61 6e 64 20 73 6f 6d 65 tables and some
1db9c 20 68 69 67 68 65 72 20 6e 75 6d 62 65 72 0a 2a higher number.*
1db9d 2a 20 66 6f 72 20 61 75 78 69 6c 69 61 72 79 20 * for auxiliary
1db9e 64 61 74 61 62 61 73 65 73 2e 0a 2a 2a 0a 2a 2a databases..**.**
1db9f 20 54 68 65 20 63 6f 6f 6b 69 65 20 63 68 61 6e The cookie chan
1dba0 67 65 73 20 69 74 73 20 76 61 6c 75 65 20 77 68 ges its value wh
1dba1 65 6e 65 76 65 72 20 74 68 65 20 64 61 74 61 62 enever the datab
1dba2 61 73 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67 ase schema chang
1dba3 65 73 2e 0a 2a 2a 20 54 68 69 73 20 6f 70 65 72 es..** This oper
1dba4 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f ation is used to
1dba5 20 64 65 74 65 63 74 20 77 68 65 6e 20 74 68 61 detect when tha
1dba6 74 20 74 68 65 20 63 6f 6f 6b 69 65 20 68 61 73 t the cookie has
1dba7 20 63 68 61 6e 67 65 64 0a 2a 2a 20 61 6e 64 20 changed.** and
1dba8 74 68 61 74 20 74 68 65 20 63 75 72 72 65 6e 74 that the current
1dba9 20 70 72 6f 63 65 73 73 20 6e 65 65 64 73 20 74 process needs t
1dbaa 6f 20 72 65 72 65 61 64 20 74 68 65 20 73 63 68 o reread the sch
1dbab 65 6d 61 2e 0a 2a 2a 0a 2a 2a 20 45 69 74 68 65 ema..**.** Eithe
1dbac 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 r a transaction
1dbad 6e 65 65 64 73 20 74 6f 20 68 61 76 65 20 62 65 needs to have be
1dbae 65 6e 20 73 74 61 72 74 65 64 20 6f 72 20 61 6e en started or an
1dbaf 20 4f 50 5f 4f 70 65 6e 20 6e 65 65 64 73 0a 2a OP_Open needs.*
1dbb0 2a 20 74 6f 20 62 65 20 65 78 65 63 75 74 65 64 * to be executed
1dbb1 20 28 74 6f 20 65 73 74 61 62 6c 69 73 68 20 61 (to establish a
1dbb2 20 72 65 61 64 20 6c 6f 63 6b 29 20 62 65 66 6f read lock) befo
1dbb3 72 65 20 74 68 69 73 20 6f 70 63 6f 64 65 20 69 re this opcode i
1dbb4 73 0a 2a 2a 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2f s.** invoked..*/
1dbb5 0a 63 61 73 65 20 4f 50 5f 56 65 72 69 66 79 43 .case OP_VerifyC
1dbb6 6f 6f 6b 69 65 3a 20 7b 0a 23 69 66 20 30 20 20 ookie: {.#if 0
1dbb7 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1dbb8 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1dbb9 61 76 20 2a 2f 0a 20 20 69 6e 74 20 69 4d 65 74 av */. int iMet
1dbba 61 3b 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b a;. Btree *pBt;
1dbbb 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1dbbc 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1dbbd 20 69 6e 74 6f 20 75 2e 61 76 20 2a 2f 0a 20 20 into u.av */.
1dbbe 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e assert( pOp->p1>
1dbbf 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 =0 && pOp->p1<db
1dbc0 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 ->nDb );. asser
1dbc1 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b t( (p->btreeMask
1dbc2 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 & (1<<pOp->p1))
1dbc3 21 3d 30 20 29 3b 0a 20 20 75 2e 61 76 2e 70 42 !=0 );. u.av.pB
1dbc4 74 20 3d 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d t = db->aDb[pOp-
1dbc5 3e 70 31 5d 2e 70 42 74 3b 0a 20 20 69 66 28 20 >p1].pBt;. if(
1dbc6 75 2e 61 76 2e 70 42 74 20 29 7b 0a 20 20 20 20 u.av.pBt ){.
1dbc7 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d sqlite3BtreeGetM
1dbc8 65 74 61 28 75 2e 61 76 2e 70 42 74 2c 20 42 54 eta(u.av.pBt, BT
1dbc9 52 45 45 5f 53 43 48 45 4d 41 5f 56 45 52 53 49 REE_SCHEMA_VERSI
1dbca 4f 4e 2c 20 28 75 33 32 20 2a 29 26 75 2e 61 76 ON, (u32 *)&u.av
1dbcb 2e 69 4d 65 74 61 29 3b 0a 20 20 7d 65 6c 73 65 .iMeta);. }else
1dbcc 7b 0a 20 20 20 20 75 2e 61 76 2e 69 4d 65 74 61 {. u.av.iMeta
1dbcd 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 = 0;. }. if(
1dbce 75 2e 61 76 2e 69 4d 65 74 61 21 3d 70 4f 70 2d u.av.iMeta!=pOp-
1dbcf 3e 70 32 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 >p2 ){. sqlit
1dbd0 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e e3DbFree(db, p->
1dbd1 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d zErrMsg);. p-
1dbd2 3e 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 >zErrMsg = sqlit
1dbd3 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 22 e3DbStrDup(db, "
1dbd4 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 database schema
1dbd5 68 61 73 20 63 68 61 6e 67 65 64 22 29 3b 0a 20 has changed");.
1dbd6 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73 63 68 /* If the sch
1dbd7 65 6d 61 2d 63 6f 6f 6b 69 65 20 66 72 6f 6d 20 ema-cookie from
1dbd8 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
1dbd9 65 20 6d 61 74 63 68 65 73 20 74 68 65 20 63 6f e matches the co
1dbda 6f 6b 69 65 0a 20 20 20 20 2a 2a 20 73 74 6f 72 okie. ** stor
1dbdb 65 64 20 77 69 74 68 20 74 68 65 20 69 6e 2d 6d ed with the in-m
1dbdc 65 6d 6f 72 79 20 72 65 70 72 65 73 65 6e 74 61 emory representa
1dbdd 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 63 68 65 tion of the sche
1dbde 6d 61 2c 20 64 6f 0a 20 20 20 20 2a 2a 20 6e 6f ma, do. ** no
1dbdf 74 20 72 65 6c 6f 61 64 20 74 68 65 20 73 63 68 t reload the sch
1dbe0 65 6d 61 20 66 72 6f 6d 20 74 68 65 20 64 61 74 ema from the dat
1dbe1 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 abase file..
1dbe2 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 76 69 72 **. ** If vir
1dbe3 74 75 61 6c 2d 74 61 62 6c 65 73 20 61 72 65 20 tual-tables are
1dbe4 69 6e 20 75 73 65 2c 20 74 68 69 73 20 69 73 20 in use, this is
1dbe5 6e 6f 74 20 6a 75 73 74 20 61 6e 20 6f 70 74 69 not just an opti
1dbe6 6d 69 7a 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2a mization.. **
1dbe7 20 4f 66 74 65 6e 2c 20 76 2d 74 61 62 6c 65 73 Often, v-tables
1dbe8 20 73 74 6f 72 65 20 74 68 65 69 72 20 64 61 74 store their dat
1dbe9 61 20 69 6e 20 6f 74 68 65 72 20 53 51 4c 69 74 a in other SQLit
1dbea 65 20 74 61 62 6c 65 73 2c 20 77 68 69 63 68 0a e tables, which.
1dbeb 20 20 20 20 2a 2a 20 61 72 65 20 71 75 65 72 69 ** are queri
1dbec 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 78 ed from within x
1dbed 4e 65 78 74 28 29 20 61 6e 64 20 6f 74 68 65 72 Next() and other
1dbee 20 76 2d 74 61 62 6c 65 20 6d 65 74 68 6f 64 73 v-table methods
1dbef 20 75 73 69 6e 67 0a 20 20 20 20 2a 2a 20 70 72 using. ** pr
1dbf0 65 70 61 72 65 64 20 71 75 65 72 69 65 73 2e 20 epared queries.
1dbf1 49 66 20 73 75 63 68 20 61 20 71 75 65 72 79 20 If such a query
1dbf2 69 73 20 6f 75 74 2d 6f 66 2d 64 61 74 65 2c 20 is out-of-date,
1dbf3 77 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 we do not want t
1dbf4 6f 0a 20 20 20 20 2a 2a 20 64 69 73 63 61 72 64 o. ** discard
1dbf5 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 the database sc
1dbf6 68 65 6d 61 2c 20 61 73 20 74 68 65 20 75 73 65 hema, as the use
1dbf7 72 20 63 6f 64 65 20 69 6d 70 6c 65 6d 65 6e 74 r code implement
1dbf8 69 6e 67 20 74 68 65 0a 20 20 20 20 2a 2a 20 76 ing the. ** v
1dbf9 2d 74 61 62 6c 65 20 77 6f 75 6c 64 20 68 61 76 -table would hav
1dbfa 65 20 74 6f 20 62 65 20 72 65 61 64 79 20 66 6f e to be ready fo
1dbfb 72 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 74 r the sqlite3_vt
1dbfc 61 62 20 73 74 72 75 63 74 75 72 65 20 69 74 73 ab structure its
1dbfd 65 6c 66 0a 20 20 20 20 2a 2a 20 74 6f 20 62 65 elf. ** to be
1dbfe 20 69 6e 76 61 6c 69 64 61 74 65 64 20 77 68 65 invalidated whe
1dbff 6e 65 76 65 72 20 73 71 6c 69 74 65 33 5f 73 74 never sqlite3_st
1dc00 65 70 28 29 20 69 73 20 63 61 6c 6c 65 64 20 66 ep() is called f
1dc01 72 6f 6d 20 77 69 74 68 69 6e 0a 20 20 20 20 2a rom within. *
1dc02 2a 20 61 20 76 2d 74 61 62 6c 65 20 6d 65 74 68 * a v-table meth
1dc03 6f 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 od.. */. i
1dc04 66 28 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e f( db->aDb[pOp->
1dc05 70 31 5d 2e 70 53 63 68 65 6d 61 2d 3e 73 63 68 p1].pSchema->sch
1dc06 65 6d 61 5f 63 6f 6f 6b 69 65 21 3d 75 2e 61 76 ema_cookie!=u.av
1dc07 2e 69 4d 65 74 61 20 29 7b 0a 20 20 20 20 20 20 .iMeta ){.
1dc08 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 sqlite3ResetInte
1dc09 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 70 rnalSchema(db, p
1dc0a 4f 70 2d 3e 70 31 29 3b 0a 20 20 20 20 7d 0a 0a Op->p1);. }..
1dc0b 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 69 72 sqlite3Expir
1dc0c 65 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 ePreparedStateme
1dc0d 6e 74 73 28 64 62 29 3b 0a 20 20 20 20 72 63 20 nts(db);. rc
1dc0e 3d 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b = SQLITE_SCHEMA;
1dc0f 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1dc10 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 65 6e ./* Opcode: Open
1dc11 52 65 61 64 20 50 31 20 50 32 20 50 33 20 50 34 Read P1 P2 P3 P4
1dc12 20 50 35 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61 P5.**.** Open a
1dc13 20 72 65 61 64 2d 6f 6e 6c 79 20 63 75 72 73 6f read-only curso
1dc14 72 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 r for the databa
1dc15 73 65 20 74 61 62 6c 65 20 77 68 6f 73 65 20 72 se table whose r
1dc16 6f 6f 74 20 70 61 67 65 20 69 73 0a 2a 2a 20 50 oot page is.** P
1dc17 32 20 69 6e 20 61 20 64 61 74 61 62 61 73 65 20 2 in a database
1dc18 66 69 6c 65 2e 20 20 54 68 65 20 64 61 74 61 62 file. The datab
1dc19 61 73 65 20 66 69 6c 65 20 69 73 20 64 65 74 65 ase file is dete
1dc1a 72 6d 69 6e 65 64 20 62 79 20 50 33 2e 20 0a 2a rmined by P3. .*
1dc1b 2a 20 50 33 3d 3d 30 20 6d 65 61 6e 73 20 74 68 * P3==0 means th
1dc1c 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2c e main database,
1dc1d 20 50 33 3d 3d 31 20 6d 65 61 6e 73 20 74 68 65 P3==1 means the
1dc1e 20 64 61 74 61 62 61 73 65 20 75 73 65 64 20 66 database used f
1dc1f 6f 72 20 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 or .** temporary
1dc20 20 74 61 62 6c 65 73 2c 20 61 6e 64 20 50 33 3e tables, and P3>
1dc21 31 20 6d 65 61 6e 73 20 75 73 65 64 20 74 68 65 1 means used the
1dc22 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 61 corresponding a
1dc23 74 74 61 63 68 65 64 0a 2a 2a 20 64 61 74 61 62 ttached.** datab
1dc24 61 73 65 2e 20 20 47 69 76 65 20 74 68 65 20 6e ase. Give the n
1dc25 65 77 20 63 75 72 73 6f 72 20 61 6e 20 69 64 65 ew cursor an ide
1dc26 6e 74 69 66 69 65 72 20 6f 66 20 50 31 2e 20 20 ntifier of P1.
1dc27 54 68 65 20 50 31 0a 2a 2a 20 76 61 6c 75 65 73 The P1.** values
1dc28 20 6e 65 65 64 20 6e 6f 74 20 62 65 20 63 6f 6e need not be con
1dc29 74 69 67 75 6f 75 73 20 62 75 74 20 61 6c 6c 20 tiguous but all
1dc2a 50 31 20 76 61 6c 75 65 73 20 73 68 6f 75 6c 64 P1 values should
1dc2b 20 62 65 20 73 6d 61 6c 6c 20 69 6e 74 65 67 65 be small intege
1dc2c 72 73 2e 0a 2a 2a 20 49 74 20 69 73 20 61 6e 20 rs..** It is an
1dc2d 65 72 72 6f 72 20 66 6f 72 20 50 31 20 74 6f 20 error for P1 to
1dc2e 62 65 20 6e 65 67 61 74 69 76 65 2e 0a 2a 2a 0a be negative..**.
1dc2f 2a 2a 20 49 66 20 50 35 21 3d 30 20 74 68 65 6e ** If P5!=0 then
1dc30 20 75 73 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 use the content
1dc31 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 32 20 of register P2
1dc32 61 73 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 as the root page
1dc33 2c 20 6e 6f 74 0a 2a 2a 20 74 68 65 20 76 61 6c , not.** the val
1dc34 75 65 20 6f 66 20 50 32 20 69 74 73 65 6c 66 2e ue of P2 itself.
1dc35 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 77 69 6c .**.** There wil
1dc36 6c 20 62 65 20 61 20 72 65 61 64 20 6c 6f 63 6b l be a read lock
1dc37 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
1dc38 20 77 68 65 6e 65 76 65 72 20 74 68 65 72 65 20 whenever there
1dc39 69 73 20 61 6e 0a 2a 2a 20 6f 70 65 6e 20 63 75 is an.** open cu
1dc3a 72 73 6f 72 2e 20 20 49 66 20 74 68 65 20 64 61 rsor. If the da
1dc3b 74 61 62 61 73 65 20 77 61 73 20 75 6e 6c 6f 63 tabase was unloc
1dc3c 6b 65 64 20 70 72 69 6f 72 20 74 6f 20 74 68 69 ked prior to thi
1dc3d 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a s instruction.**
1dc3e 20 74 68 65 6e 20 61 20 72 65 61 64 20 6c 6f 63 then a read loc
1dc3f 6b 20 69 73 20 61 63 71 75 69 72 65 64 20 61 73 k is acquired as
1dc40 20 70 61 72 74 20 6f 66 20 74 68 69 73 20 69 6e part of this in
1dc41 73 74 72 75 63 74 69 6f 6e 2e 20 20 41 20 72 65 struction. A re
1dc42 61 64 0a 2a 2a 20 6c 6f 63 6b 20 61 6c 6c 6f 77 ad.** lock allow
1dc43 73 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 s other processe
1dc44 73 20 74 6f 20 72 65 61 64 20 74 68 65 20 64 61 s to read the da
1dc45 74 61 62 61 73 65 20 62 75 74 20 70 72 6f 68 69 tabase but prohi
1dc46 62 69 74 73 0a 2a 2a 20 61 6e 79 20 6f 74 68 65 bits.** any othe
1dc47 72 20 70 72 6f 63 65 73 73 20 66 72 6f 6d 20 6d r process from m
1dc48 6f 64 69 66 79 69 6e 67 20 74 68 65 20 64 61 74 odifying the dat
1dc49 61 62 61 73 65 2e 20 20 54 68 65 20 72 65 61 64 abase. The read
1dc4a 20 6c 6f 63 6b 20 69 73 0a 2a 2a 20 72 65 6c 65 lock is.** rele
1dc4b 61 73 65 64 20 77 68 65 6e 20 61 6c 6c 20 63 75 ased when all cu
1dc4c 72 73 6f 72 73 20 61 72 65 20 63 6c 6f 73 65 64 rsors are closed
1dc4d 2e 20 20 49 66 20 74 68 69 73 20 69 6e 73 74 72 . If this instr
1dc4e 75 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 0a uction attempts.
1dc4f 2a 2a 20 74 6f 20 67 65 74 20 61 20 72 65 61 64 ** to get a read
1dc50 20 6c 6f 63 6b 20 62 75 74 20 66 61 69 6c 73 2c lock but fails,
1dc51 20 74 68 65 20 73 63 72 69 70 74 20 74 65 72 6d the script term
1dc52 69 6e 61 74 65 73 20 77 69 74 68 20 61 6e 0a 2a inates with an.*
1dc53 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 20 65 72 * SQLITE_BUSY er
1dc54 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 ror code..**.**
1dc55 54 68 65 20 50 34 20 76 61 6c 75 65 20 6d 61 79 The P4 value may
1dc56 20 62 65 20 65 69 74 68 65 72 20 61 6e 20 69 6e be either an in
1dc57 74 65 67 65 72 20 28 50 34 5f 49 4e 54 33 32 29 teger (P4_INT32)
1dc58 20 6f 72 20 61 20 70 6f 69 6e 74 65 72 20 74 6f or a pointer to
1dc59 0a 2a 2a 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 .** a KeyInfo st
1dc5a 72 75 63 74 75 72 65 20 28 50 34 5f 4b 45 59 49 ructure (P4_KEYI
1dc5b 4e 46 4f 29 2e 20 49 66 20 69 74 20 69 73 20 61 NFO). If it is a
1dc5c 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4b 65 pointer to a Ke
1dc5d 79 49 6e 66 6f 20 0a 2a 2a 20 73 74 72 75 63 74 yInfo .** struct
1dc5e 75 72 65 2c 20 74 68 65 6e 20 73 61 69 64 20 73 ure, then said s
1dc5f 74 72 75 63 74 75 72 65 20 64 65 66 69 6e 65 73 tructure defines
1dc60 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 61 6e 64 the content and
1dc61 20 63 6f 6c 6c 61 74 69 6e 67 20 0a 2a 2a 20 73 collating .** s
1dc62 65 71 75 65 6e 63 65 20 6f 66 20 74 68 65 20 69 equence of the i
1dc63 6e 64 65 78 20 62 65 69 6e 67 20 6f 70 65 6e 65 ndex being opene
1dc64 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 d. Otherwise, if
1dc65 20 50 34 20 69 73 20 61 6e 20 69 6e 74 65 67 65 P4 is an intege
1dc66 72 20 0a 2a 2a 20 76 61 6c 75 65 2c 20 69 74 20 r .** value, it
1dc67 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75 is set to the nu
1dc68 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 mber of columns
1dc69 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a in the table..**
1dc6a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 4f 70 65 .** See also Ope
1dc6b 6e 57 72 69 74 65 2e 0a 2a 2f 0a 2f 2a 20 4f 70 nWrite..*/./* Op
1dc6c 63 6f 64 65 3a 20 4f 70 65 6e 57 72 69 74 65 20 code: OpenWrite
1dc6d 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a P1 P2 P3 P4 P5.*
1dc6e 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 72 65 61 64 *.** Open a read
1dc6f 2f 77 72 69 74 65 20 63 75 72 73 6f 72 20 6e 61 /write cursor na
1dc70 6d 65 64 20 50 31 20 6f 6e 20 74 68 65 20 74 61 med P1 on the ta
1dc71 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 77 68 6f ble or index who
1dc72 73 65 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 se root.** page
1dc73 69 73 20 50 32 2e 20 20 4f 72 20 69 66 20 50 35 is P2. Or if P5
1dc74 21 3d 30 20 75 73 65 20 74 68 65 20 63 6f 6e 74 !=0 use the cont
1dc75 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72 20 ent of register
1dc76 50 32 20 74 6f 20 66 69 6e 64 20 74 68 65 0a 2a P2 to find the.*
1dc77 2a 20 72 6f 6f 74 20 70 61 67 65 2e 0a 2a 2a 0a * root page..**.
1dc78 2a 2a 20 54 68 65 20 50 34 20 76 61 6c 75 65 20 ** The P4 value
1dc79 6d 61 79 20 62 65 20 65 69 74 68 65 72 20 61 6e may be either an
1dc7a 20 69 6e 74 65 67 65 72 20 28 50 34 5f 49 4e 54 integer (P4_INT
1dc7b 33 32 29 20 6f 72 20 61 20 70 6f 69 6e 74 65 72 32) or a pointer
1dc7c 20 74 6f 0a 2a 2a 20 61 20 4b 65 79 49 6e 66 6f to.** a KeyInfo
1dc7d 20 73 74 72 75 63 74 75 72 65 20 28 50 34 5f 4b structure (P4_K
1dc7e 45 59 49 4e 46 4f 29 2e 20 49 66 20 69 74 20 69 EYINFO). If it i
1dc7f 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
1dc80 20 4b 65 79 49 6e 66 6f 20 0a 2a 2a 20 73 74 72 KeyInfo .** str
1dc81 75 63 74 75 72 65 2c 20 74 68 65 6e 20 73 61 69 ucture, then sai
1dc82 64 20 73 74 72 75 63 74 75 72 65 20 64 65 66 69 d structure defi
1dc83 6e 65 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 nes the content
1dc84 61 6e 64 20 63 6f 6c 6c 61 74 69 6e 67 20 0a 2a and collating .*
1dc85 2a 20 73 65 71 75 65 6e 63 65 20 6f 66 20 74 68 * sequence of th
1dc86 65 20 69 6e 64 65 78 20 62 65 69 6e 67 20 6f 70 e index being op
1dc87 65 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c ened. Otherwise,
1dc88 20 69 66 20 50 34 20 69 73 20 61 6e 20 69 6e 74 if P4 is an int
1dc89 65 67 65 72 20 0a 2a 2a 20 76 61 6c 75 65 2c 20 eger .** value,
1dc8a 69 74 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 it is set to the
1dc8b 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d number of colum
1dc8c 6e 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2c ns in the table,
1dc8d 20 6f 72 20 74 6f 20 74 68 65 0a 2a 2a 20 6c 61 or to the.** la
1dc8e 72 67 65 73 74 20 69 6e 64 65 78 20 6f 66 20 61 rgest index of a
1dc8f 6e 79 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 ny column of the
1dc90 20 74 61 62 6c 65 20 74 68 61 74 20 69 73 20 61 table that is a
1dc91 63 74 75 61 6c 6c 79 20 75 73 65 64 2e 0a 2a 2a ctually used..**
1dc92 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 .** This instruc
1dc93 74 69 6f 6e 20 77 6f 72 6b 73 20 6a 75 73 74 20 tion works just
1dc94 6c 69 6b 65 20 4f 70 65 6e 52 65 61 64 20 65 78 like OpenRead ex
1dc95 63 65 70 74 20 74 68 61 74 20 69 74 20 6f 70 65 cept that it ope
1dc96 6e 73 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a ns the cursor.**
1dc97 20 69 6e 20 72 65 61 64 2f 77 72 69 74 65 20 6d in read/write m
1dc98 6f 64 65 2e 20 20 46 6f 72 20 61 20 67 69 76 65 ode. For a give
1dc99 6e 20 74 61 62 6c 65 2c 20 74 68 65 72 65 20 63 n table, there c
1dc9a 61 6e 20 62 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 an be one or mor
1dc9b 65 20 72 65 61 64 2d 6f 6e 6c 79 0a 2a 2a 20 63 e read-only.** c
1dc9c 75 72 73 6f 72 73 20 6f 72 20 61 20 73 69 6e 67 ursors or a sing
1dc9d 6c 65 20 72 65 61 64 2f 77 72 69 74 65 20 63 75 le read/write cu
1dc9e 72 73 6f 72 20 62 75 74 20 6e 6f 74 20 62 6f 74 rsor but not bot
1dc9f 68 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 h..**.** See als
1dca0 6f 20 4f 70 65 6e 52 65 61 64 2e 0a 2a 2f 0a 63 o OpenRead..*/.c
1dca1 61 73 65 20 4f 50 5f 4f 70 65 6e 52 65 61 64 3a ase OP_OpenRead:
1dca2 0a 63 61 73 65 20 4f 50 5f 4f 70 65 6e 57 72 69 .case OP_OpenWri
1dca3 74 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 te: {.#if 0 /*
1dca4 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1dca5 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 77 20 moved into u.aw
1dca6 2a 2f 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b */. int nField;
1dca7 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 . KeyInfo *pKey
1dca8 49 6e 66 6f 3b 0a 20 20 69 6e 74 20 70 32 3b 0a Info;. int p2;.
1dca9 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 69 6e 74 int iDb;. int
1dcaa 20 77 72 46 6c 61 67 3b 0a 20 20 42 74 72 65 65 wrFlag;. Btree
1dcab 20 2a 70 58 3b 0a 20 20 56 64 62 65 43 75 72 73 *pX;. VdbeCurs
1dcac 6f 72 20 2a 70 43 75 72 3b 0a 20 20 44 62 20 2a or *pCur;. Db *
1dcad 70 44 62 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c pDb;.#endif /* l
1dcae 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1dcaf 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 77 20 2a oved into u.aw *
1dcb0 2f 0a 0a 20 20 69 66 28 20 70 2d 3e 65 78 70 69 /.. if( p->expi
1dcb1 72 65 64 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 red ){. rc =
1dcb2 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20 20 SQLITE_ABORT;.
1dcb3 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 break;. }..
1dcb4 75 2e 61 77 2e 6e 46 69 65 6c 64 20 3d 20 30 3b u.aw.nField = 0;
1dcb5 0a 20 20 75 2e 61 77 2e 70 4b 65 79 49 6e 66 6f . u.aw.pKeyInfo
1dcb6 20 3d 20 30 3b 0a 20 20 75 2e 61 77 2e 70 32 20 = 0;. u.aw.p2
1dcb7 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 75 2e 61 = pOp->p2;. u.a
1dcb8 77 2e 69 44 62 20 3d 20 70 4f 70 2d 3e 70 33 3b w.iDb = pOp->p3;
1dcb9 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 77 2e . assert( u.aw.
1dcba 69 44 62 3e 3d 30 20 26 26 20 75 2e 61 77 2e 69 iDb>=0 && u.aw.i
1dcbb 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 Db<db->nDb );.
1dcbc 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 assert( (p->btre
1dcbd 65 4d 61 73 6b 20 26 20 28 31 3c 3c 75 2e 61 77 eMask & (1<<u.aw
1dcbe 2e 69 44 62 29 29 21 3d 30 20 29 3b 0a 20 20 75 .iDb))!=0 );. u
1dcbf 2e 61 77 2e 70 44 62 20 3d 20 26 64 62 2d 3e 61 .aw.pDb = &db->a
1dcc0 44 62 5b 75 2e 61 77 2e 69 44 62 5d 3b 0a 20 20 Db[u.aw.iDb];.
1dcc1 75 2e 61 77 2e 70 58 20 3d 20 75 2e 61 77 2e 70 u.aw.pX = u.aw.p
1dcc2 44 62 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 Db->pBt;. asser
1dcc3 74 28 20 75 2e 61 77 2e 70 58 21 3d 30 20 29 3b t( u.aw.pX!=0 );
1dcc4 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f . if( pOp->opco
1dcc5 64 65 3d 3d 4f 50 5f 4f 70 65 6e 57 72 69 74 65 de==OP_OpenWrite
1dcc6 20 29 7b 0a 20 20 20 20 75 2e 61 77 2e 77 72 46 ){. u.aw.wrF
1dcc7 6c 61 67 20 3d 20 31 3b 0a 20 20 20 20 69 66 28 lag = 1;. if(
1dcc8 20 75 2e 61 77 2e 70 44 62 2d 3e 70 53 63 68 65 u.aw.pDb->pSche
1dcc9 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 20 ma->file_format
1dcca 3c 20 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c < p->minWriteFil
1dccb 65 46 6f 72 6d 61 74 20 29 7b 0a 20 20 20 20 20 eFormat ){.
1dccc 20 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65 p->minWriteFile
1dccd 46 6f 72 6d 61 74 20 3d 20 75 2e 61 77 2e 70 44 Format = u.aw.pD
1dcce 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 b->pSchema->file
1dccf 5f 66 6f 72 6d 61 74 3b 0a 20 20 20 20 7d 0a 20 _format;. }.
1dcd0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 77 }else{. u.aw
1dcd1 2e 77 72 46 6c 61 67 20 3d 20 30 3b 0a 20 20 7d .wrFlag = 0;. }
1dcd2 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 29 . if( pOp->p5 )
1dcd3 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e {. assert( u.
1dcd4 61 77 2e 70 32 3e 30 20 29 3b 0a 20 20 20 20 61 aw.p2>0 );. a
1dcd5 73 73 65 72 74 28 20 75 2e 61 77 2e 70 32 3c 3d ssert( u.aw.p2<=
1dcd6 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 70 p->nMem );. p
1dcd7 49 6e 32 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 75 In2 = &p->aMem[u
1dcd8 2e 61 77 2e 70 32 5d 3b 0a 20 20 20 20 73 71 6c .aw.p2];. sql
1dcd9 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 ite3VdbeMemInteg
1dcda 65 72 69 66 79 28 70 49 6e 32 29 3b 0a 20 20 20 erify(pIn2);.
1dcdb 20 75 2e 61 77 2e 70 32 20 3d 20 28 69 6e 74 29 u.aw.p2 = (int)
1dcdc 70 49 6e 32 2d 3e 75 2e 69 3b 0a 20 20 20 20 2f pIn2->u.i;. /
1dcdd 2a 20 54 68 65 20 75 2e 61 77 2e 70 32 20 76 61 * The u.aw.p2 va
1dcde 6c 75 65 20 61 6c 77 61 79 73 20 63 6f 6d 65 73 lue always comes
1dcdf 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 20 4f 50 from a prior OP
1dce0 5f 43 72 65 61 74 65 54 61 62 6c 65 20 6f 70 63 _CreateTable opc
1dce1 6f 64 65 20 61 6e 64 0a 20 20 20 20 2a 2a 20 74 ode and. ** t
1dce2 68 61 74 20 6f 70 63 6f 64 65 20 77 69 6c 6c 20 hat opcode will
1dce3 61 6c 77 61 79 73 20 73 65 74 20 74 68 65 20 75 always set the u
1dce4 2e 61 77 2e 70 32 20 76 61 6c 75 65 20 74 6f 20 .aw.p2 value to
1dce5 32 20 6f 72 20 6d 6f 72 65 20 6f 72 20 65 6c 73 2 or more or els
1dce6 65 20 66 61 69 6c 2e 0a 20 20 20 20 2a 2a 20 49 e fail.. ** I
1dce7 66 20 74 68 65 72 65 20 77 65 72 65 20 61 20 66 f there were a f
1dce8 61 69 6c 75 72 65 2c 20 74 68 65 20 70 72 65 70 ailure, the prep
1dce9 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 77 ared statement w
1dcea 6f 75 6c 64 20 68 61 76 65 20 68 61 6c 74 65 64 ould have halted
1dceb 0a 20 20 20 20 2a 2a 20 62 65 66 6f 72 65 20 72 . ** before r
1dcec 65 61 63 68 69 6e 67 20 74 68 69 73 20 69 6e 73 eaching this ins
1dced 74 72 75 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20 20 truction. */.
1dcee 20 69 66 28 20 4e 45 56 45 52 28 75 2e 61 77 2e if( NEVER(u.aw.
1dcef 70 32 3c 32 29 20 29 20 7b 0a 20 20 20 20 20 20 p2<2) ) {.
1dcf0 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 rc = SQLITE_CORR
1dcf1 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 UPT_BKPT;.
1dcf2 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1dcf3 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 o_error;. }.
1dcf4 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 }. if( pOp->p4
1dcf5 74 79 70 65 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f type==P4_KEYINFO
1dcf6 20 29 7b 0a 20 20 20 20 75 2e 61 77 2e 70 4b 65 ){. u.aw.pKe
1dcf7 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e yInfo = pOp->p4.
1dcf8 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 75 2e pKeyInfo;. u.
1dcf9 61 77 2e 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 aw.pKeyInfo->enc
1dcfa 20 3d 20 45 4e 43 28 70 2d 3e 64 62 29 3b 0a 20 = ENC(p->db);.
1dcfb 20 20 20 75 2e 61 77 2e 6e 46 69 65 6c 64 20 3d u.aw.nField =
1dcfc 20 75 2e 61 77 2e 70 4b 65 79 49 6e 66 6f 2d 3e u.aw.pKeyInfo->
1dcfd 6e 46 69 65 6c 64 2b 31 3b 0a 20 20 7d 65 6c 73 nField+1;. }els
1dcfe 65 20 69 66 28 20 70 4f 70 2d 3e 70 34 74 79 70 e if( pOp->p4typ
1dcff 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29 7b 0a 20 e==P4_INT32 ){.
1dd00 20 20 20 75 2e 61 77 2e 6e 46 69 65 6c 64 20 3d u.aw.nField =
1dd01 20 70 4f 70 2d 3e 70 34 2e 69 3b 0a 20 20 7d 0a pOp->p4.i;. }.
1dd02 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1dd03 31 3e 3d 30 20 29 3b 0a 20 20 75 2e 61 77 2e 70 1>=0 );. u.aw.p
1dd04 43 75 72 20 3d 20 61 6c 6c 6f 63 61 74 65 43 75 Cur = allocateCu
1dd05 72 73 6f 72 28 70 2c 20 70 4f 70 2d 3e 70 31 2c rsor(p, pOp->p1,
1dd06 20 75 2e 61 77 2e 6e 46 69 65 6c 64 2c 20 75 2e u.aw.nField, u.
1dd07 61 77 2e 69 44 62 2c 20 31 29 3b 0a 20 20 69 66 aw.iDb, 1);. if
1dd08 28 20 75 2e 61 77 2e 70 43 75 72 3d 3d 30 20 29 ( u.aw.pCur==0 )
1dd09 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 goto no_mem;.
1dd0a 75 2e 61 77 2e 70 43 75 72 2d 3e 6e 75 6c 6c 52 u.aw.pCur->nullR
1dd0b 6f 77 20 3d 20 31 3b 0a 20 20 72 63 20 3d 20 73 ow = 1;. rc = s
1dd0c 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f qlite3BtreeCurso
1dd0d 72 28 75 2e 61 77 2e 70 58 2c 20 75 2e 61 77 2e r(u.aw.pX, u.aw.
1dd0e 70 32 2c 20 75 2e 61 77 2e 77 72 46 6c 61 67 2c p2, u.aw.wrFlag,
1dd0f 20 75 2e 61 77 2e 70 4b 65 79 49 6e 66 6f 2c 20 u.aw.pKeyInfo,
1dd10 75 2e 61 77 2e 70 43 75 72 2d 3e 70 43 75 72 73 u.aw.pCur->pCurs
1dd11 6f 72 29 3b 0a 20 20 75 2e 61 77 2e 70 43 75 72 or);. u.aw.pCur
1dd12 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 75 2e 61 ->pKeyInfo = u.a
1dd13 77 2e 70 4b 65 79 49 6e 66 6f 3b 0a 0a 20 20 2f w.pKeyInfo;.. /
1dd14 2a 20 53 69 6e 63 65 20 69 74 20 70 65 72 66 6f * Since it perfo
1dd15 72 6d 73 20 6e 6f 20 6d 65 6d 6f 72 79 20 61 6c rms no memory al
1dd16 6c 6f 63 61 74 69 6f 6e 20 6f 72 20 49 4f 2c 20 location or IO,
1dd17 74 68 65 20 6f 6e 6c 79 20 76 61 6c 75 65 73 20 the only values
1dd18 74 68 61 74 0a 20 20 2a 2a 20 73 71 6c 69 74 65 that. ** sqlite
1dd19 33 42 74 72 65 65 43 75 72 73 6f 72 28 29 20 6d 3BtreeCursor() m
1dd1a 61 79 20 72 65 74 75 72 6e 20 61 72 65 20 53 51 ay return are SQ
1dd1b 4c 49 54 45 5f 45 4d 50 54 59 20 61 6e 64 20 53 LITE_EMPTY and S
1dd1c 51 4c 49 54 45 5f 4f 4b 2e 0a 20 20 2a 2a 20 53 QLITE_OK.. ** S
1dd1d 51 4c 49 54 45 5f 45 4d 50 54 59 20 69 73 20 6f QLITE_EMPTY is o
1dd1e 6e 6c 79 20 72 65 74 75 72 6e 65 64 20 77 68 65 nly returned whe
1dd1f 6e 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 n attempting to
1dd20 6f 70 65 6e 20 74 68 65 20 74 61 62 6c 65 0a 20 open the table.
1dd21 20 2a 2a 20 72 6f 6f 74 65 64 20 61 74 20 70 61 ** rooted at pa
1dd22 67 65 20 31 20 6f 66 20 61 20 7a 65 72 6f 2d 62 ge 1 of a zero-b
1dd23 79 74 65 20 64 61 74 61 62 61 73 65 2e 20 20 2a yte database. *
1dd24 2f 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d /. assert( rc==
1dd25 53 51 4c 49 54 45 5f 45 4d 50 54 59 20 7c 7c 20 SQLITE_EMPTY ||
1dd26 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b rc==SQLITE_OK );
1dd27 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
1dd28 45 5f 45 4d 50 54 59 20 29 7b 0a 20 20 20 20 75 E_EMPTY ){. u
1dd29 2e 61 77 2e 70 43 75 72 2d 3e 70 43 75 72 73 6f .aw.pCur->pCurso
1dd2a 72 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 r = 0;. rc =
1dd2b 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a SQLITE_OK;. }..
1dd2c 20 20 2f 2a 20 53 65 74 20 74 68 65 20 56 64 62 /* Set the Vdb
1dd2d 65 43 75 72 73 6f 72 2e 69 73 54 61 62 6c 65 20 eCursor.isTable
1dd2e 61 6e 64 20 69 73 49 6e 64 65 78 20 76 61 72 69 and isIndex vari
1dd2f 61 62 6c 65 73 2e 20 50 72 65 76 69 6f 75 73 20 ables. Previous
1dd30 76 65 72 73 69 6f 6e 73 20 6f 66 0a 20 20 2a 2a versions of. **
1dd31 20 53 51 4c 69 74 65 20 75 73 65 64 20 74 6f 20 SQLite used to
1dd32 63 68 65 63 6b 20 69 66 20 74 68 65 20 72 6f 6f check if the roo
1dd33 74 2d 70 61 67 65 20 66 6c 61 67 73 20 77 65 72 t-page flags wer
1dd34 65 20 73 61 6e 65 20 61 74 20 74 68 69 73 20 70 e sane at this p
1dd35 6f 69 6e 74 0a 20 20 2a 2a 20 61 6e 64 20 72 65 oint. ** and re
1dd36 70 6f 72 74 20 64 61 74 61 62 61 73 65 20 63 6f port database co
1dd37 72 72 75 70 74 69 6f 6e 20 69 66 20 74 68 65 79 rruption if they
1dd38 20 77 65 72 65 20 6e 6f 74 2c 20 62 75 74 20 74 were not, but t
1dd39 68 69 73 20 63 68 65 63 6b 20 68 61 73 0a 20 20 his check has.
1dd3a 2a 2a 20 73 69 6e 63 65 20 6d 6f 76 65 64 20 69 ** since moved i
1dd3b 6e 74 6f 20 74 68 65 20 62 74 72 65 65 20 6c 61 nto the btree la
1dd3c 79 65 72 2e 20 20 2a 2f 0a 20 20 75 2e 61 77 2e yer. */. u.aw.
1dd3d 70 43 75 72 2d 3e 69 73 54 61 62 6c 65 20 3d 20 pCur->isTable =
1dd3e 70 4f 70 2d 3e 70 34 74 79 70 65 21 3d 50 34 5f pOp->p4type!=P4_
1dd3f 4b 45 59 49 4e 46 4f 3b 0a 20 20 75 2e 61 77 2e KEYINFO;. u.aw.
1dd40 70 43 75 72 2d 3e 69 73 49 6e 64 65 78 20 3d 20 pCur->isIndex =
1dd41 21 75 2e 61 77 2e 70 43 75 72 2d 3e 69 73 54 61 !u.aw.pCur->isTa
1dd42 62 6c 65 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a ble;. break;.}.
1dd43 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 65 6e ./* Opcode: Open
1dd44 45 70 68 65 6d 65 72 61 6c 20 50 31 20 50 32 20 Ephemeral P1 P2
1dd45 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f 70 65 * P4 *.**.** Ope
1dd46 6e 20 61 20 6e 65 77 20 63 75 72 73 6f 72 20 50 n a new cursor P
1dd47 31 20 74 6f 20 61 20 74 72 61 6e 73 69 65 6e 74 1 to a transient
1dd48 20 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 63 table..** The c
1dd49 75 72 73 6f 72 20 69 73 20 61 6c 77 61 79 73 20 ursor is always
1dd4a 6f 70 65 6e 65 64 20 72 65 61 64 2f 77 72 69 74 opened read/writ
1dd4b 65 20 65 76 65 6e 20 69 66 20 0a 2a 2a 20 74 68 e even if .** th
1dd4c 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 e main database
1dd4d 69 73 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 20 54 is read-only. T
1dd4e 68 65 20 74 72 61 6e 73 69 65 6e 74 20 6f 72 20 he transient or
1dd4f 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 virtual.** table
1dd50 20 69 73 20 64 65 6c 65 74 65 64 20 61 75 74 6f is deleted auto
1dd51 6d 61 74 69 63 61 6c 6c 79 20 77 68 65 6e 20 74 matically when t
1dd52 68 65 20 63 75 72 73 6f 72 20 69 73 20 63 6c 6f he cursor is clo
1dd53 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 69 73 sed..**.** P2 is
1dd54 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 the number of c
1dd55 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 76 69 olumns in the vi
1dd56 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 20 rtual table..**
1dd57 54 68 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 The cursor point
1dd58 73 20 74 6f 20 61 20 42 54 72 65 65 20 74 61 62 s to a BTree tab
1dd59 6c 65 20 69 66 20 50 34 3d 3d 30 20 61 6e 64 20 le if P4==0 and
1dd5a 74 6f 20 61 20 42 54 72 65 65 20 69 6e 64 65 78 to a BTree index
1dd5b 0a 2a 2a 20 69 66 20 50 34 20 69 73 20 6e 6f 74 .** if P4 is not
1dd5c 20 30 2e 20 20 49 66 20 50 34 20 69 73 20 6e 6f 0. If P4 is no
1dd5d 74 20 4e 55 4c 4c 2c 20 69 74 20 70 6f 69 6e 74 t NULL, it point
1dd5e 73 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 73 s to a KeyInfo s
1dd5f 74 72 75 63 74 75 72 65 0a 2a 2a 20 74 68 61 74 tructure.** that
1dd60 20 64 65 66 69 6e 65 73 20 74 68 65 20 66 6f 72 defines the for
1dd61 6d 61 74 20 6f 66 20 6b 65 79 73 20 69 6e 20 74 mat of keys in t
1dd62 68 65 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 he index..**.**
1dd63 54 68 69 73 20 6f 70 63 6f 64 65 20 77 61 73 20 This opcode was
1dd64 6f 6e 63 65 20 63 61 6c 6c 65 64 20 4f 70 65 6e once called Open
1dd65 54 65 6d 70 2e 20 20 42 75 74 20 74 68 61 74 20 Temp. But that
1dd66 63 72 65 61 74 65 64 0a 2a 2a 20 63 6f 6e 66 75 created.** confu
1dd67 73 69 6f 6e 20 62 65 63 61 75 73 65 20 74 68 65 sion because the
1dd68 20 74 65 72 6d 20 22 74 65 6d 70 20 74 61 62 6c term "temp tabl
1dd69 65 22 2c 20 6d 69 67 68 74 20 72 65 66 65 72 20 e", might refer
1dd6a 65 69 74 68 65 72 0a 2a 2a 20 74 6f 20 61 20 54 either.** to a T
1dd6b 45 4d 50 20 74 61 62 6c 65 20 61 74 20 74 68 65 EMP table at the
1dd6c 20 53 51 4c 20 6c 65 76 65 6c 2c 20 6f 72 20 74 SQL level, or t
1dd6d 6f 20 61 20 74 61 62 6c 65 20 6f 70 65 6e 65 64 o a table opened
1dd6e 20 62 79 0a 2a 2a 20 74 68 69 73 20 6f 70 63 6f by.** this opco
1dd6f 64 65 2e 20 20 54 68 65 6e 20 74 68 69 73 20 6f de. Then this o
1dd70 70 63 6f 64 65 20 77 61 73 20 63 61 6c 6c 20 4f pcode was call O
1dd71 70 65 6e 56 69 72 74 75 61 6c 2e 20 20 42 75 74 penVirtual. But
1dd72 0a 2a 2a 20 74 68 61 74 20 63 72 65 61 74 65 64 .** that created
1dd73 20 63 6f 6e 66 75 73 69 6f 6e 20 77 69 74 68 20 confusion with
1dd74 74 68 65 20 77 68 6f 6c 65 20 76 69 72 74 75 61 the whole virtua
1dd75 6c 2d 74 61 62 6c 65 20 69 64 65 61 2e 0a 2a 2f l-table idea..*/
1dd76 0a 63 61 73 65 20 4f 50 5f 4f 70 65 6e 45 70 68 .case OP_OpenEph
1dd77 65 6d 65 72 61 6c 3a 20 7b 0a 23 69 66 20 30 20 emeral: {.#if 0
1dd78 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1dd79 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1dd7a 2e 61 78 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 .ax */. VdbeCur
1dd7b 73 6f 72 20 2a 70 43 78 3b 0a 23 65 6e 64 69 66 sor *pCx;.#endif
1dd7c 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1dd7d 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1dd7e 2e 61 78 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 .ax */. static
1dd7f 63 6f 6e 73 74 20 69 6e 74 20 6f 70 65 6e 46 6c const int openFl
1dd80 61 67 73 20 3d 0a 20 20 20 20 20 20 53 51 4c 49 ags =. SQLI
1dd81 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 TE_OPEN_READWRIT
1dd82 45 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45 E |. SQLITE
1dd83 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 7c 0a 20 _OPEN_CREATE |.
1dd84 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e SQLITE_OPEN
1dd85 5f 45 58 43 4c 55 53 49 56 45 20 7c 0a 20 20 20 _EXCLUSIVE |.
1dd86 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 SQLITE_OPEN_D
1dd87 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 7c 0a 20 ELETEONCLOSE |.
1dd88 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e SQLITE_OPEN
1dd89 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 3b 0a 0a _TRANSIENT_DB;..
1dd8a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1dd8b 31 3e 3d 30 20 29 3b 0a 20 20 75 2e 61 78 2e 70 1>=0 );. u.ax.p
1dd8c 43 78 20 3d 20 61 6c 6c 6f 63 61 74 65 43 75 72 Cx = allocateCur
1dd8d 73 6f 72 28 70 2c 20 70 4f 70 2d 3e 70 31 2c 20 sor(p, pOp->p1,
1dd8e 70 4f 70 2d 3e 70 32 2c 20 2d 31 2c 20 31 29 3b pOp->p2, -1, 1);
1dd8f 0a 20 20 69 66 28 20 75 2e 61 78 2e 70 43 78 3d . if( u.ax.pCx=
1dd90 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d =0 ) goto no_mem
1dd91 3b 0a 20 20 75 2e 61 78 2e 70 43 78 2d 3e 6e 75 ;. u.ax.pCx->nu
1dd92 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 72 63 20 llRow = 1;. rc
1dd93 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 61 = sqlite3BtreeFa
1dd94 63 74 6f 72 79 28 64 62 2c 20 30 2c 20 31 2c 20 ctory(db, 0, 1,
1dd95 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 54 SQLITE_DEFAULT_T
1dd96 45 4d 50 5f 43 41 43 48 45 5f 53 49 5a 45 2c 20 EMP_CACHE_SIZE,
1dd97 6f 70 65 6e 46 6c 61 67 73 2c 0a 20 20 20 20 20 openFlags,.
1dd98 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dd99 20 20 20 20 20 20 26 75 2e 61 78 2e 70 43 78 2d &u.ax.pCx-
1dd9a 3e 70 42 74 29 3b 0a 20 20 69 66 28 20 72 63 3d >pBt);. if( rc=
1dd9b 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1dd9c 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1dd9d 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 75 2e reeBeginTrans(u.
1dd9e 61 78 2e 70 43 78 2d 3e 70 42 74 2c 20 31 29 3b ax.pCx->pBt, 1);
1dd9f 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 . }. if( rc==S
1dda0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1dda1 2f 2a 20 49 66 20 61 20 74 72 61 6e 73 69 65 6e /* If a transien
1dda2 74 20 69 6e 64 65 78 20 69 73 20 72 65 71 75 69 t index is requi
1dda3 72 65 64 2c 20 63 72 65 61 74 65 20 69 74 20 62 red, create it b
1dda4 79 20 63 61 6c 6c 69 6e 67 0a 20 20 20 20 2a 2a y calling. **
1dda5 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 sqlite3BtreeCre
1dda6 61 74 65 54 61 62 6c 65 28 29 20 77 69 74 68 20 ateTable() with
1dda7 74 68 65 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 the BTREE_ZERODA
1dda8 54 41 20 66 6c 61 67 20 62 65 66 6f 72 65 0a 20 TA flag before.
1dda9 20 20 20 2a 2a 20 6f 70 65 6e 69 6e 67 20 69 74 ** opening it
1ddaa 2e 20 49 66 20 61 20 74 72 61 6e 73 69 65 6e 74 . If a transient
1ddab 20 74 61 62 6c 65 20 69 73 20 72 65 71 75 69 72 table is requir
1ddac 65 64 2c 20 6a 75 73 74 20 75 73 65 20 74 68 65 ed, just use the
1ddad 0a 20 20 20 20 2a 2a 20 61 75 74 6f 6d 61 74 69 . ** automati
1ddae 63 61 6c 6c 79 20 63 72 65 61 74 65 64 20 74 61 cally created ta
1ddaf 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 ble with root-pa
1ddb0 67 65 20 31 20 28 61 6e 20 49 4e 54 4b 45 59 20 ge 1 (an INTKEY
1ddb1 74 61 62 6c 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 table).. */.
1ddb2 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 2e 70 if( pOp->p4.p
1ddb3 4b 65 79 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20 KeyInfo ){.
1ddb4 20 69 6e 74 20 70 67 6e 6f 3b 0a 20 20 20 20 20 int pgno;.
1ddb5 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 assert( pOp->p4
1ddb6 74 79 70 65 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f type==P4_KEYINFO
1ddb7 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 );. rc = s
1ddb8 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74 qlite3BtreeCreat
1ddb9 65 54 61 62 6c 65 28 75 2e 61 78 2e 70 43 78 2d eTable(u.ax.pCx-
1ddba 3e 70 42 74 2c 20 26 70 67 6e 6f 2c 20 42 54 52 >pBt, &pgno, BTR
1ddbb 45 45 5f 5a 45 52 4f 44 41 54 41 29 3b 0a 20 20 EE_ZERODATA);.
1ddbc 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
1ddbd 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
1ddbe 20 61 73 73 65 72 74 28 20 70 67 6e 6f 3d 3d 4d assert( pgno==M
1ddbf 41 53 54 45 52 5f 52 4f 4f 54 2b 31 20 29 3b 0a ASTER_ROOT+1 );.
1ddc0 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
1ddc1 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 28 ite3BtreeCursor(
1ddc2 75 2e 61 78 2e 70 43 78 2d 3e 70 42 74 2c 20 70 u.ax.pCx->pBt, p
1ddc3 67 6e 6f 2c 20 31 2c 0a 20 20 20 20 20 20 20 20 gno, 1,.
1ddc4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ddc5 20 20 20 20 20 20 20 20 28 4b 65 79 49 6e 66 6f (KeyInfo
1ddc6 2a 29 70 4f 70 2d 3e 70 34 2e 7a 2c 20 75 2e 61 *)pOp->p4.z, u.a
1ddc7 78 2e 70 43 78 2d 3e 70 43 75 72 73 6f 72 29 3b x.pCx->pCursor);
1ddc8 0a 20 20 20 20 20 20 20 20 75 2e 61 78 2e 70 43 . u.ax.pC
1ddc9 78 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4f x->pKeyInfo = pO
1ddca 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a p->p4.pKeyInfo;.
1ddcb 20 20 20 20 20 20 20 20 75 2e 61 78 2e 70 43 78 u.ax.pCx
1ddcc 2d 3e 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 20 ->pKeyInfo->enc
1ddcd 3d 20 45 4e 43 28 70 2d 3e 64 62 29 3b 0a 20 20 = ENC(p->db);.
1ddce 20 20 20 20 7d 0a 20 20 20 20 20 20 75 2e 61 78 }. u.ax
1ddcf 2e 70 43 78 2d 3e 69 73 54 61 62 6c 65 20 3d 20 .pCx->isTable =
1ddd0 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 0;. }else{.
1ddd1 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1ddd2 42 74 72 65 65 43 75 72 73 6f 72 28 75 2e 61 78 BtreeCursor(u.ax
1ddd3 2e 70 43 78 2d 3e 70 42 74 2c 20 4d 41 53 54 45 .pCx->pBt, MASTE
1ddd4 52 5f 52 4f 4f 54 2c 20 31 2c 20 30 2c 20 75 2e R_ROOT, 1, 0, u.
1ddd5 61 78 2e 70 43 78 2d 3e 70 43 75 72 73 6f 72 29 ax.pCx->pCursor)
1ddd6 3b 0a 20 20 20 20 20 20 75 2e 61 78 2e 70 43 78 ;. u.ax.pCx
1ddd7 2d 3e 69 73 54 61 62 6c 65 20 3d 20 31 3b 0a 20 ->isTable = 1;.
1ddd8 20 20 20 7d 0a 20 20 7d 0a 20 20 75 2e 61 78 2e }. }. u.ax.
1ddd9 70 43 78 2d 3e 69 73 49 6e 64 65 78 20 3d 20 21 pCx->isIndex = !
1ddda 75 2e 61 78 2e 70 43 78 2d 3e 69 73 54 61 62 6c u.ax.pCx->isTabl
1dddb 65 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f e;. break;.}../
1dddc 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 65 6e 50 73 * Opcode: OpenPs
1dddd 65 75 64 6f 20 50 31 20 50 32 20 50 33 20 2a 20 eudo P1 P2 P3 *
1ddde 2a 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 6e *.**.** Open a n
1dddf 65 77 20 63 75 72 73 6f 72 20 74 68 61 74 20 70 ew cursor that p
1dde0 6f 69 6e 74 73 20 74 6f 20 61 20 66 61 6b 65 20 oints to a fake
1dde1 74 61 62 6c 65 20 74 68 61 74 20 63 6f 6e 74 61 table that conta
1dde2 69 6e 73 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 ins a single.**
1dde3 72 6f 77 20 6f 66 20 64 61 74 61 2e 20 20 54 68 row of data. Th
1dde4 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 61 e content of tha
1dde5 74 20 6f 6e 65 20 72 6f 77 20 69 6e 20 74 68 65 t one row in the
1dde6 20 63 6f 6e 74 65 6e 74 20 6f 66 20 6d 65 6d 6f content of memo
1dde7 72 79 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 ry.** register P
1dde8 32 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 2. In other wor
1dde9 64 73 2c 20 63 75 72 73 6f 72 20 50 31 20 62 65 ds, cursor P1 be
1ddea 63 6f 6d 65 73 20 61 6e 20 61 6c 69 61 73 20 66 comes an alias f
1ddeb 6f 72 20 74 68 65 20 0a 2a 2a 20 4d 45 4d 5f 42 or the .** MEM_B
1ddec 6c 6f 62 20 63 6f 6e 74 65 6e 74 20 63 6f 6e 74 lob content cont
1dded 61 69 6e 65 64 20 69 6e 20 72 65 67 69 73 74 65 ained in registe
1ddee 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 73 r P2..**.** A ps
1ddef 65 75 64 6f 2d 74 61 62 6c 65 20 63 72 65 61 74 eudo-table creat
1ddf0 65 64 20 62 79 20 74 68 69 73 20 6f 70 63 6f 64 ed by this opcod
1ddf1 65 20 69 73 20 75 73 65 64 20 74 6f 20 68 6f 6c e is used to hol
1ddf2 64 20 74 68 65 20 61 20 73 69 6e 67 6c 65 0a 2a d the a single.*
1ddf3 2a 20 72 6f 77 20 6f 75 74 70 75 74 20 66 72 6f * row output fro
1ddf4 6d 20 74 68 65 20 73 6f 72 74 65 72 20 73 6f 20 m the sorter so
1ddf5 74 68 61 74 20 74 68 65 20 72 6f 77 20 63 61 6e that the row can
1ddf6 20 62 65 20 64 65 63 6f 6d 70 6f 73 65 64 20 69 be decomposed i
1ddf7 6e 74 6f 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 nto.** individua
1ddf8 6c 20 63 6f 6c 75 6d 6e 73 20 75 73 69 6e 67 20 l columns using
1ddf9 74 68 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 the OP_Column op
1ddfa 63 6f 64 65 2e 20 20 54 68 65 20 4f 50 5f 43 6f code. The OP_Co
1ddfb 6c 75 6d 6e 20 6f 70 63 6f 64 65 0a 2a 2a 20 69 lumn opcode.** i
1ddfc 73 20 74 68 65 20 6f 6e 6c 79 20 63 75 72 73 6f s the only curso
1ddfd 72 20 6f 70 63 6f 64 65 20 74 68 61 74 20 77 6f r opcode that wo
1ddfe 72 6b 73 20 77 69 74 68 20 61 20 70 73 65 75 64 rks with a pseud
1ddff 6f 2d 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 50 o-table..**.** P
1de00 33 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 3 is the number
1de01 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 of fields in the
1de02 20 72 65 63 6f 72 64 73 20 74 68 61 74 20 77 69 records that wi
1de03 6c 6c 20 62 65 20 73 74 6f 72 65 64 20 62 79 0a ll be stored by.
1de04 2a 2a 20 74 68 65 20 70 73 65 75 64 6f 2d 74 61 ** the pseudo-ta
1de05 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ble..*/.case OP_
1de06 4f 70 65 6e 50 73 65 75 64 6f 3a 20 7b 0a 23 69 OpenPseudo: {.#i
1de07 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1de08 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1de09 74 6f 20 75 2e 61 79 20 2a 2f 0a 20 20 56 64 62 to u.ay */. Vdb
1de0a 65 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a 23 65 eCursor *pCx;.#e
1de0b 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1de0c 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1de0d 74 6f 20 75 2e 61 79 20 2a 2f 0a 0a 20 20 61 73 to u.ay */.. as
1de0e 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 sert( pOp->p1>=0
1de0f 20 29 3b 0a 20 20 75 2e 61 79 2e 70 43 78 20 3d );. u.ay.pCx =
1de10 20 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 allocateCursor(
1de11 70 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d p, pOp->p1, pOp-
1de12 3e 70 33 2c 20 2d 31 2c 20 30 29 3b 0a 20 20 69 >p3, -1, 0);. i
1de13 66 28 20 75 2e 61 79 2e 70 43 78 3d 3d 30 20 29 f( u.ay.pCx==0 )
1de14 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 goto no_mem;.
1de15 75 2e 61 79 2e 70 43 78 2d 3e 6e 75 6c 6c 52 6f u.ay.pCx->nullRo
1de16 77 20 3d 20 31 3b 0a 20 20 75 2e 61 79 2e 70 43 w = 1;. u.ay.pC
1de17 78 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 52 65 x->pseudoTableRe
1de18 67 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 75 g = pOp->p2;. u
1de19 2e 61 79 2e 70 43 78 2d 3e 69 73 54 61 62 6c 65 .ay.pCx->isTable
1de1a 20 3d 20 31 3b 0a 20 20 75 2e 61 79 2e 70 43 78 = 1;. u.ay.pCx
1de1b 2d 3e 69 73 49 6e 64 65 78 20 3d 20 30 3b 0a 20 ->isIndex = 0;.
1de1c 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1de1d 63 6f 64 65 3a 20 43 6c 6f 73 65 20 50 31 20 2a code: Close P1 *
1de1e 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 6c 6f * * *.**.** Clo
1de1f 73 65 20 61 20 63 75 72 73 6f 72 20 70 72 65 76 se a cursor prev
1de20 69 6f 75 73 6c 79 20 6f 70 65 6e 65 64 20 61 73 iously opened as
1de21 20 50 31 2e 20 20 49 66 20 50 31 20 69 73 20 6e P1. If P1 is n
1de22 6f 74 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 ot.** currently
1de23 6f 70 65 6e 2c 20 74 68 69 73 20 69 6e 73 74 72 open, this instr
1de24 75 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f uction is a no-o
1de25 70 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6c p..*/.case OP_Cl
1de26 6f 73 65 3a 20 7b 0a 20 20 61 73 73 65 72 74 28 ose: {. assert(
1de27 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 pOp->p1>=0 && p
1de28 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f Op->p1<p->nCurso
1de29 72 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 r );. sqlite3Vd
1de2a 62 65 46 72 65 65 43 75 72 73 6f 72 28 70 2c 20 beFreeCursor(p,
1de2b 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 p->apCsr[pOp->p1
1de2c 5d 29 3b 0a 20 20 70 2d 3e 61 70 43 73 72 5b 70 ]);. p->apCsr[p
1de2d 4f 70 2d 3e 70 31 5d 20 3d 20 30 3b 0a 20 20 62 Op->p1] = 0;. b
1de2e 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1de2f 64 65 3a 20 53 65 65 6b 47 65 20 50 31 20 50 32 de: SeekGe P1 P2
1de30 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 49 P3 P4 *.**.** I
1de31 66 20 63 75 72 73 6f 72 20 50 31 20 72 65 66 65 f cursor P1 refe
1de32 72 73 20 74 6f 20 61 6e 20 53 51 4c 20 74 61 62 rs to an SQL tab
1de33 6c 65 20 28 42 2d 54 72 65 65 20 74 68 61 74 20 le (B-Tree that
1de34 75 73 65 73 20 69 6e 74 65 67 65 72 20 6b 65 79 uses integer key
1de35 73 29 2c 20 0a 2a 2a 20 75 73 65 20 74 68 65 20 s), .** use the
1de36 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 value in registe
1de37 72 20 50 33 20 61 73 20 74 68 65 20 6b 65 79 2e r P3 as the key.
1de38 20 20 49 66 20 63 75 72 73 6f 72 20 50 31 20 72 If cursor P1 r
1de39 65 66 65 72 73 20 0a 2a 2a 20 74 6f 20 61 6e 20 efers .** to an
1de3a 53 51 4c 20 69 6e 64 65 78 2c 20 74 68 65 6e 20 SQL index, then
1de3b 50 33 20 69 73 20 74 68 65 20 66 69 72 73 74 20 P3 is the first
1de3c 69 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20 50 in an array of P
1de3d 34 20 72 65 67 69 73 74 65 72 73 20 0a 2a 2a 20 4 registers .**
1de3e 74 68 61 74 20 61 72 65 20 75 73 65 64 20 61 73 that are used as
1de3f 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 an unpacked ind
1de40 65 78 20 6b 65 79 2e 20 0a 2a 2a 0a 2a 2a 20 52 ex key. .**.** R
1de41 65 70 6f 73 69 74 69 6f 6e 20 63 75 72 73 6f 72 eposition cursor
1de42 20 50 31 20 73 6f 20 74 68 61 74 20 20 69 74 20 P1 so that it
1de43 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 73 6d points to the sm
1de44 61 6c 6c 65 73 74 20 65 6e 74 72 79 20 74 68 61 allest entry tha
1de45 74 20 0a 2a 2a 20 69 73 20 67 72 65 61 74 65 72 t .** is greater
1de46 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 than or equal t
1de47 6f 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 2e o the key value.
1de48 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f If there are no
1de49 20 72 65 63 6f 72 64 73 20 0a 2a 2a 20 67 72 65 records .** gre
1de4a 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 ater than or equ
1de4b 61 6c 20 74 6f 20 74 68 65 20 6b 65 79 20 61 6e al to the key an
1de4c 64 20 50 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f d P2 is not zero
1de4d 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 , then jump to P
1de4e 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 2..**.** See als
1de4f 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 o: Found, NotFou
1de50 6e 64 2c 20 44 69 73 74 69 6e 63 74 2c 20 53 65 nd, Distinct, Se
1de51 65 6b 4c 74 2c 20 53 65 65 6b 47 74 2c 20 53 65 ekLt, SeekGt, Se
1de52 65 6b 4c 65 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 ekLe.*/./* Opcod
1de53 65 3a 20 53 65 65 6b 47 74 20 50 31 20 50 32 20 e: SeekGt P1 P2
1de54 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 P3 P4 *.**.** If
1de55 20 63 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 cursor P1 refer
1de56 73 20 74 6f 20 61 6e 20 53 51 4c 20 74 61 62 6c s to an SQL tabl
1de57 65 20 28 42 2d 54 72 65 65 20 74 68 61 74 20 75 e (B-Tree that u
1de58 73 65 73 20 69 6e 74 65 67 65 72 20 6b 65 79 73 ses integer keys
1de59 29 2c 20 0a 2a 2a 20 75 73 65 20 74 68 65 20 76 ), .** use the v
1de5a 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 alue in register
1de5b 20 50 33 20 61 73 20 61 20 6b 65 79 2e 20 49 66 P3 as a key. If
1de5c 20 63 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 cursor P1 refer
1de5d 73 20 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20 s .** to an SQL
1de5e 69 6e 64 65 78 2c 20 74 68 65 6e 20 50 33 20 69 index, then P3 i
1de5f 73 20 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 s the first in a
1de60 6e 20 61 72 72 61 79 20 6f 66 20 50 34 20 72 65 n array of P4 re
1de61 67 69 73 74 65 72 73 20 0a 2a 2a 20 74 68 61 74 gisters .** that
1de62 20 61 72 65 20 75 73 65 64 20 61 73 20 61 6e 20 are used as an
1de63 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b unpacked index k
1de64 65 79 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 ey. .**.** Repos
1de65 69 74 69 6f 6e 20 63 75 72 73 6f 72 20 50 31 20 ition cursor P1
1de66 73 6f 20 74 68 61 74 20 20 69 74 20 70 6f 69 6e so that it poin
1de67 74 73 20 74 6f 20 74 68 65 20 73 6d 61 6c 6c 65 ts to the smalle
1de68 73 74 20 65 6e 74 72 79 20 74 68 61 74 20 0a 2a st entry that .*
1de69 2a 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 * is greater tha
1de6a 6e 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 2e n the key value.
1de6b 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f If there are no
1de6c 20 72 65 63 6f 72 64 73 20 67 72 65 61 74 65 72 records greater
1de6d 20 74 68 61 6e 20 0a 2a 2a 20 74 68 65 20 6b 65 than .** the ke
1de6e 79 20 61 6e 64 20 50 32 20 69 73 20 6e 6f 74 20 y and P2 is not
1de6f 7a 65 72 6f 2c 20 74 68 65 6e 20 6a 75 6d 70 20 zero, then jump
1de70 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 to P2..**.** See
1de71 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f also: Found, No
1de72 74 46 6f 75 6e 64 2c 20 44 69 73 74 69 6e 63 74 tFound, Distinct
1de73 2c 20 53 65 65 6b 4c 74 2c 20 53 65 65 6b 47 65 , SeekLt, SeekGe
1de74 2c 20 53 65 65 6b 4c 65 0a 2a 2f 0a 2f 2a 20 4f , SeekLe.*/./* O
1de75 70 63 6f 64 65 3a 20 53 65 65 6b 4c 74 20 50 31 pcode: SeekLt P1
1de76 20 50 32 20 50 33 20 50 34 20 2a 20 0a 2a 2a 0a P2 P3 P4 * .**.
1de77 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 50 31 20 ** If cursor P1
1de78 72 65 66 65 72 73 20 74 6f 20 61 6e 20 53 51 4c refers to an SQL
1de79 20 74 61 62 6c 65 20 28 42 2d 54 72 65 65 20 74 table (B-Tree t
1de7a 68 61 74 20 75 73 65 73 20 69 6e 74 65 67 65 72 hat uses integer
1de7b 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73 65 20 keys), .** use
1de7c 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 the value in reg
1de7d 69 73 74 65 72 20 50 33 20 61 73 20 61 20 6b 65 ister P3 as a ke
1de7e 79 2e 20 49 66 20 63 75 72 73 6f 72 20 50 31 20 y. If cursor P1
1de7f 72 65 66 65 72 73 20 0a 2a 2a 20 74 6f 20 61 6e refers .** to an
1de80 20 53 51 4c 20 69 6e 64 65 78 2c 20 74 68 65 6e SQL index, then
1de81 20 50 33 20 69 73 20 74 68 65 20 66 69 72 73 74 P3 is the first
1de82 20 69 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20 in an array of
1de83 50 34 20 72 65 67 69 73 74 65 72 73 20 0a 2a 2a P4 registers .**
1de84 20 74 68 61 74 20 61 72 65 20 75 73 65 64 20 61 that are used a
1de85 73 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e s an unpacked in
1de86 64 65 78 20 6b 65 79 2e 20 0a 2a 2a 0a 2a 2a 20 dex key. .**.**
1de87 52 65 70 6f 73 69 74 69 6f 6e 20 63 75 72 73 6f Reposition curso
1de88 72 20 50 31 20 73 6f 20 74 68 61 74 20 20 69 74 r P1 so that it
1de89 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 6c points to the l
1de8a 61 72 67 65 73 74 20 65 6e 74 72 79 20 74 68 61 argest entry tha
1de8b 74 20 0a 2a 2a 20 69 73 20 6c 65 73 73 20 74 68 t .** is less th
1de8c 61 6e 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 an the key value
1de8d 2e 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e . If there are n
1de8e 6f 20 72 65 63 6f 72 64 73 20 6c 65 73 73 20 74 o records less t
1de8f 68 61 6e 20 0a 2a 2a 20 74 68 65 20 6b 65 79 20 han .** the key
1de90 61 6e 64 20 50 32 20 69 73 20 6e 6f 74 20 7a 65 and P2 is not ze
1de91 72 6f 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f ro, then jump to
1de92 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 P2..**.** See a
1de93 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 lso: Found, NotF
1de94 6f 75 6e 64 2c 20 44 69 73 74 69 6e 63 74 2c 20 ound, Distinct,
1de95 53 65 65 6b 47 74 2c 20 53 65 65 6b 47 65 2c 20 SeekGt, SeekGe,
1de96 53 65 65 6b 4c 65 0a 2a 2f 0a 2f 2a 20 4f 70 63 SeekLe.*/./* Opc
1de97 6f 64 65 3a 20 53 65 65 6b 4c 65 20 50 31 20 50 ode: SeekLe P1 P
1de98 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 2 P3 P4 *.**.**
1de99 49 66 20 63 75 72 73 6f 72 20 50 31 20 72 65 66 If cursor P1 ref
1de9a 65 72 73 20 74 6f 20 61 6e 20 53 51 4c 20 74 61 ers to an SQL ta
1de9b 62 6c 65 20 28 42 2d 54 72 65 65 20 74 68 61 74 ble (B-Tree that
1de9c 20 75 73 65 73 20 69 6e 74 65 67 65 72 20 6b 65 uses integer ke
1de9d 79 73 29 2c 20 0a 2a 2a 20 75 73 65 20 74 68 65 ys), .** use the
1de9e 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 value in regist
1de9f 65 72 20 50 33 20 61 73 20 61 20 6b 65 79 2e 20 er P3 as a key.
1dea0 49 66 20 63 75 72 73 6f 72 20 50 31 20 72 65 66 If cursor P1 ref
1dea1 65 72 73 20 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 ers .** to an SQ
1dea2 4c 20 69 6e 64 65 78 2c 20 74 68 65 6e 20 50 33 L index, then P3
1dea3 20 69 73 20 74 68 65 20 66 69 72 73 74 20 69 6e is the first in
1dea4 20 61 6e 20 61 72 72 61 79 20 6f 66 20 50 34 20 an array of P4
1dea5 72 65 67 69 73 74 65 72 73 20 0a 2a 2a 20 74 68 registers .** th
1dea6 61 74 20 61 72 65 20 75 73 65 64 20 61 73 20 61 at are used as a
1dea7 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 n unpacked index
1dea8 20 6b 65 79 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70 key. .**.** Rep
1dea9 6f 73 69 74 69 6f 6e 20 63 75 72 73 6f 72 20 50 osition cursor P
1deaa 31 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69 1 so that it poi
1deab 6e 74 73 20 74 6f 20 74 68 65 20 6c 61 72 67 65 nts to the large
1deac 73 74 20 65 6e 74 72 79 20 74 68 61 74 20 0a 2a st entry that .*
1dead 2a 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f * is less than o
1deae 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6b r equal to the k
1deaf 65 79 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65 ey value. If the
1deb0 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 re are no record
1deb1 73 20 0a 2a 2a 20 6c 65 73 73 20 74 68 61 6e 20 s .** less than
1deb2 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 or equal to the
1deb3 6b 65 79 20 61 6e 64 20 50 32 20 69 73 20 6e 6f key and P2 is no
1deb4 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 6a 75 6d t zero, then jum
1deb5 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 p to P2..**.** S
1deb6 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 ee also: Found,
1deb7 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73 74 69 6e NotFound, Distin
1deb8 63 74 2c 20 53 65 65 6b 47 74 2c 20 53 65 65 6b ct, SeekGt, Seek
1deb9 47 65 2c 20 53 65 65 6b 4c 74 0a 2a 2f 0a 63 61 Ge, SeekLt.*/.ca
1deba 73 65 20 4f 50 5f 53 65 65 6b 4c 74 3a 20 20 20 se OP_SeekLt:
1debb 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 /* jump, i
1debc 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 n3 */.case OP_Se
1debd 65 6b 4c 65 3a 20 20 20 20 20 20 20 20 20 2f 2a ekLe: /*
1debe 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 jump, in3 */.ca
1debf 73 65 20 4f 50 5f 53 65 65 6b 47 65 3a 20 20 20 se OP_SeekGe:
1dec0 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 /* jump, i
1dec1 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 n3 */.case OP_Se
1dec2 65 6b 47 74 3a 20 7b 20 20 20 20 20 20 20 2f 2a ekGt: { /*
1dec3 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 23 69 jump, in3 */.#i
1dec4 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1dec5 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1dec6 74 6f 20 75 2e 61 7a 20 2a 2f 0a 20 20 69 6e 74 to u.az */. int
1dec7 20 72 65 73 3b 0a 20 20 69 6e 74 20 6f 63 3b 0a res;. int oc;.
1dec8 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1dec9 3b 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f ;. UnpackedReco
1deca 72 64 20 72 3b 0a 20 20 69 6e 74 20 6e 46 69 65 rd r;. int nFie
1decb 6c 64 3b 0a 20 20 69 36 34 20 69 4b 65 79 3b 20 ld;. i64 iKey;
1decc 20 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 69 /* The rowi
1decd 64 20 77 65 20 61 72 65 20 74 6f 20 73 65 65 6b d we are to seek
1dece 20 74 6f 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a to */.#endif /*
1decf 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1ded0 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 7a moved into u.az
1ded1 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 */.. assert( p
1ded2 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 Op->p1>=0 && pOp
1ded3 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 ->p1<p->nCursor
1ded4 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 );. assert( pOp
1ded5 2d 3e 70 32 21 3d 30 20 29 3b 0a 20 20 75 2e 61 ->p2!=0 );. u.a
1ded6 7a 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b z.pC = p->apCsr[
1ded7 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 pOp->p1];. asse
1ded8 72 74 28 20 75 2e 61 7a 2e 70 43 21 3d 30 20 29 rt( u.az.pC!=0 )
1ded9 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 7a ;. assert( u.az
1deda 2e 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 .pC->pseudoTable
1dedb 52 65 67 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 Reg==0 );. if(
1dedc 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 72 73 6f 72 u.az.pC->pCursor
1dedd 21 3d 30 20 29 7b 0a 20 20 20 20 75 2e 61 7a 2e !=0 ){. u.az.
1dede 6f 63 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 oc = pOp->opcode
1dedf 3b 0a 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 6e ;. u.az.pC->n
1dee0 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 20 20 20 20 ullRow = 0;.
1dee1 69 66 28 20 75 2e 61 7a 2e 70 43 2d 3e 69 73 54 if( u.az.pC->isT
1dee2 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a able ){. /*
1dee3 20 54 68 65 20 69 6e 70 75 74 20 76 61 6c 75 65 The input value
1dee4 20 69 6e 20 50 33 20 6d 69 67 68 74 20 62 65 20 in P3 might be
1dee5 6f 66 20 61 6e 79 20 74 79 70 65 3a 20 69 6e 74 of any type: int
1dee6 65 67 65 72 2c 20 72 65 61 6c 2c 20 73 74 72 69 eger, real, stri
1dee7 6e 67 2c 0a 20 20 20 20 20 20 2a 2a 20 62 6c 6f ng,. ** blo
1dee8 62 2c 20 6f 72 20 4e 55 4c 4c 2e 20 20 42 75 74 b, or NULL. But
1dee9 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 it needs to be
1deea 61 6e 20 69 6e 74 65 67 65 72 20 62 65 66 6f 72 an integer befor
1deeb 65 20 77 65 20 63 61 6e 20 64 6f 0a 20 20 20 20 e we can do.
1deec 20 20 2a 2a 20 74 68 65 20 73 65 65 6b 2c 20 73 ** the seek, s
1deed 6f 20 63 6f 76 65 72 74 20 69 74 2e 20 2a 2f 0a o covert it. */.
1deee 20 20 20 20 20 20 61 70 70 6c 79 4e 75 6d 65 72 applyNumer
1deef 69 63 41 66 66 69 6e 69 74 79 28 70 49 6e 33 29 icAffinity(pIn3)
1def0 3b 0a 20 20 20 20 20 20 75 2e 61 7a 2e 69 4b 65 ;. u.az.iKe
1def1 79 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 y = sqlite3VdbeI
1def2 6e 74 56 61 6c 75 65 28 70 49 6e 33 29 3b 0a 20 ntValue(pIn3);.
1def3 20 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 72 6f u.az.pC->ro
1def4 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a widIsValid = 0;.
1def5 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 . /* If the
1def6 20 50 33 20 76 61 6c 75 65 20 63 6f 75 6c 64 20 P3 value could
1def7 6e 6f 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 not be converted
1def8 20 69 6e 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 into an integer
1def9 20 77 69 74 68 6f 75 74 0a 20 20 20 20 20 20 2a without. *
1defa 2a 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72 6d * loss of inform
1defb 61 74 69 6f 6e 2c 20 74 68 65 6e 20 73 70 65 63 ation, then spec
1defc 69 61 6c 20 70 72 6f 63 65 73 73 69 6e 67 20 69 ial processing i
1defd 73 20 72 65 71 75 69 72 65 64 2e 2e 2e 20 2a 2f s required... */
1defe 0a 20 20 20 20 20 20 69 66 28 20 28 70 49 6e 33 . if( (pIn3
1deff 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e ->flags & MEM_In
1df00 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 t)==0 ){.
1df01 20 69 66 28 20 28 70 49 6e 33 2d 3e 66 6c 61 67 if( (pIn3->flag
1df02 73 20 26 20 4d 45 4d 5f 52 65 61 6c 29 3d 3d 30 s & MEM_Real)==0
1df03 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a ){. /*
1df04 20 49 66 20 74 68 65 20 50 33 20 76 61 6c 75 65 If the P3 value
1df05 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 65 cannot be conve
1df06 72 74 65 64 20 69 6e 74 6f 20 61 6e 79 20 6b 69 rted into any ki
1df07 6e 64 20 6f 66 20 61 20 6e 75 6d 62 65 72 2c 0a nd of a number,.
1df08 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 ** the
1df09 6e 20 74 68 65 20 73 65 65 6b 20 69 73 20 6e 6f n the seek is no
1df0a 74 20 70 6f 73 73 69 62 6c 65 2c 20 73 6f 20 6a t possible, so j
1df0b 75 6d 70 20 74 6f 20 50 32 20 2a 2f 0a 20 20 20 ump to P2 */.
1df0c 20 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d pc = pOp-
1df0d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 >p2 - 1;.
1df0e 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
1df0f 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 }. /* I
1df10 66 20 77 65 20 72 65 61 63 68 20 74 68 69 73 20 f we reach this
1df11 70 6f 69 6e 74 2c 20 74 68 65 6e 20 74 68 65 20 point, then the
1df12 50 33 20 76 61 6c 75 65 20 6d 75 73 74 20 62 65 P3 value must be
1df13 20 61 20 66 6c 6f 61 74 69 6e 67 0a 20 20 20 20 a floating.
1df14 20 20 20 20 2a 2a 20 70 6f 69 6e 74 20 6e 75 6d ** point num
1df15 62 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 ber. */.
1df16 61 73 73 65 72 74 28 20 28 70 49 6e 33 2d 3e 66 assert( (pIn3->f
1df17 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 29 lags & MEM_Real)
1df18 21 3d 30 20 29 3b 0a 0a 20 20 20 20 20 20 20 20 !=0 );..
1df19 69 66 28 20 75 2e 61 7a 2e 69 4b 65 79 3d 3d 53 if( u.az.iKey==S
1df1a 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 34 20 26 26 MALLEST_INT64 &&
1df1b 20 28 70 49 6e 33 2d 3e 72 3c 28 64 6f 75 62 6c (pIn3->r<(doubl
1df1c 65 29 75 2e 61 7a 2e 69 4b 65 79 20 7c 7c 20 70 e)u.az.iKey || p
1df1d 49 6e 33 2d 3e 72 3e 30 29 20 29 7b 0a 20 20 20 In3->r>0) ){.
1df1e 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50 33 /* The P3
1df1f 20 76 61 6c 75 65 20 69 73 20 74 6f 6f 20 6c 61 value is too la
1df20 72 67 65 20 69 6e 20 6d 61 67 6e 69 74 75 64 65 rge in magnitude
1df21 20 74 6f 20 62 65 20 65 78 70 72 65 73 73 65 64 to be expressed
1df22 20 61 73 20 61 6e 0a 20 20 20 20 20 20 20 20 20 as an.
1df23 20 2a 2a 20 69 6e 74 65 67 65 72 2e 20 2a 2f 0a ** integer. */.
1df24 20 20 20 20 20 20 20 20 20 20 75 2e 61 7a 2e 72 u.az.r
1df25 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 es = 1;.
1df26 20 20 69 66 28 20 70 49 6e 33 2d 3e 72 3c 30 20 if( pIn3->r<0
1df27 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 ){. i
1df28 66 28 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 f( u.az.oc==OP_S
1df29 65 65 6b 47 74 20 7c 7c 20 75 2e 61 7a 2e 6f 63 eekGt || u.az.oc
1df2a 3d 3d 4f 50 5f 53 65 65 6b 47 65 20 29 7b 0a 20 ==OP_SeekGe ){.
1df2b 20 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 rc
1df2c 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 69 = sqlite3BtreeFi
1df2d 72 73 74 28 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 rst(u.az.pC->pCu
1df2e 72 73 6f 72 2c 20 26 75 2e 61 7a 2e 72 65 73 29 rsor, &u.az.res)
1df2f 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
1df30 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
1df31 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 K ) goto abort_d
1df32 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 ue_to_error;.
1df33 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
1df34 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1df35 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 7a if( u.az
1df36 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 74 20 7c .oc==OP_SeekLt |
1df37 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 | u.az.oc==OP_Se
1df38 65 6b 4c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 ekLe ){.
1df39 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1df3a 65 33 42 74 72 65 65 4c 61 73 74 28 75 2e 61 7a e3BtreeLast(u.az
1df3b 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 75 .pC->pCursor, &u
1df3c 2e 61 7a 2e 72 65 73 29 3b 0a 20 20 20 20 20 20 .az.res);.
1df3d 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
1df3e 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f SQLITE_OK ) goto
1df3f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 abort_due_to_er
1df40 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20 ror;.
1df41 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
1df42 20 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 if( u.a
1df43 7a 2e 72 65 73 20 29 7b 0a 20 20 20 20 20 20 20 z.res ){.
1df44 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 pc = pOp->p
1df45 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 2 - 1;.
1df46 20 7d 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 }. bre
1df47 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 ak;. }els
1df48 65 20 69 66 28 20 75 2e 61 7a 2e 6f 63 3d 3d 4f e if( u.az.oc==O
1df49 50 5f 53 65 65 6b 4c 74 20 7c 7c 20 75 2e 61 7a P_SeekLt || u.az
1df4a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 65 20 29 .oc==OP_SeekGe )
1df4b 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 {. /* U
1df4c 73 65 20 74 68 65 20 63 65 69 6c 69 6e 67 28 29 se the ceiling()
1df4d 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 6f 6e function to con
1df4e 76 65 72 74 20 72 65 61 6c 2d 3e 69 6e 74 20 2a vert real->int *
1df4f 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 /. if(
1df50 70 49 6e 33 2d 3e 72 20 3e 20 28 64 6f 75 62 6c pIn3->r > (doubl
1df51 65 29 75 2e 61 7a 2e 69 4b 65 79 20 29 20 75 2e e)u.az.iKey ) u.
1df52 61 7a 2e 69 4b 65 79 2b 2b 3b 0a 20 20 20 20 20 az.iKey++;.
1df53 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1df54 20 20 20 20 2f 2a 20 55 73 65 20 74 68 65 20 66 /* Use the f
1df55 6c 6f 6f 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 loor() function
1df56 74 6f 20 63 6f 6e 76 65 72 74 20 72 65 61 6c 2d to convert real-
1df57 3e 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 >int */.
1df58 20 20 61 73 73 65 72 74 28 20 75 2e 61 7a 2e 6f assert( u.az.o
1df59 63 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 7c 7c 20 c==OP_SeekLe ||
1df5a 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b u.az.oc==OP_Seek
1df5b 47 74 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 Gt );.
1df5c 69 66 28 20 70 49 6e 33 2d 3e 72 20 3c 20 28 64 if( pIn3->r < (d
1df5d 6f 75 62 6c 65 29 75 2e 61 7a 2e 69 4b 65 79 20 ouble)u.az.iKey
1df5e 29 20 75 2e 61 7a 2e 69 4b 65 79 2d 2d 3b 0a 20 ) u.az.iKey--;.
1df5f 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
1df60 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
1df61 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e te3BtreeMovetoUn
1df62 70 61 63 6b 65 64 28 75 2e 61 7a 2e 70 43 2d 3e packed(u.az.pC->
1df63 70 43 75 72 73 6f 72 2c 20 30 2c 20 28 75 36 34 pCursor, 0, (u64
1df64 29 75 2e 61 7a 2e 69 4b 65 79 2c 20 30 2c 20 26 )u.az.iKey, 0, &
1df65 75 2e 61 7a 2e 72 65 73 29 3b 0a 20 20 20 20 20 u.az.res);.
1df66 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1df67 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f OK ){. go
1df68 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1df69 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 error;. }.
1df6a 20 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 72 65 if( u.az.re
1df6b 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 s==0 ){.
1df6c 75 2e 61 7a 2e 70 43 2d 3e 72 6f 77 69 64 49 73 u.az.pC->rowidIs
1df6d 56 61 6c 69 64 20 3d 20 31 3b 0a 20 20 20 20 20 Valid = 1;.
1df6e 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 6c 61 73 74 u.az.pC->last
1df6f 52 6f 77 69 64 20 3d 20 75 2e 61 7a 2e 69 4b 65 Rowid = u.az.iKe
1df70 79 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d y;. }. }
1df71 65 6c 73 65 7b 0a 20 20 20 20 20 20 75 2e 61 7a else{. u.az
1df72 2e 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 .nField = pOp->p
1df73 34 2e 69 3b 0a 20 20 20 20 20 20 61 73 73 65 72 4.i;. asser
1df74 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d t( pOp->p4type==
1df75 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20 20 P4_INT32 );.
1df76 20 20 61 73 73 65 72 74 28 20 75 2e 61 7a 2e 6e assert( u.az.n
1df77 46 69 65 6c 64 3e 30 20 29 3b 0a 20 20 20 20 20 Field>0 );.
1df78 20 75 2e 61 7a 2e 72 2e 70 4b 65 79 49 6e 66 6f u.az.r.pKeyInfo
1df79 20 3d 20 75 2e 61 7a 2e 70 43 2d 3e 70 4b 65 79 = u.az.pC->pKey
1df7a 49 6e 66 6f 3b 0a 20 20 20 20 20 20 75 2e 61 7a Info;. u.az
1df7b 2e 72 2e 6e 46 69 65 6c 64 20 3d 20 28 75 31 36 .r.nField = (u16
1df7c 29 75 2e 61 7a 2e 6e 46 69 65 6c 64 3b 0a 20 20 )u.az.nField;.
1df7d 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 6f 63 3d if( u.az.oc=
1df7e 3d 4f 50 5f 53 65 65 6b 47 74 20 7c 7c 20 75 2e =OP_SeekGt || u.
1df7f 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 65 az.oc==OP_SeekLe
1df80 20 29 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 7a ){. u.az
1df81 2e 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 .r.flags = UNPAC
1df82 4b 45 44 5f 49 4e 43 52 4b 45 59 3b 0a 20 20 20 KED_INCRKEY;.
1df83 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1df84 20 20 75 2e 61 7a 2e 72 2e 66 6c 61 67 73 20 3d u.az.r.flags =
1df85 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0;. }.
1df86 20 20 75 2e 61 7a 2e 72 2e 61 4d 65 6d 20 3d 20 u.az.r.aMem =
1df87 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 &p->aMem[pOp->p3
1df88 5d 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 ];. rc = sq
1df89 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f lite3BtreeMoveto
1df8a 55 6e 70 61 63 6b 65 64 28 75 2e 61 7a 2e 70 43 Unpacked(u.az.pC
1df8b 2d 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e 61 7a ->pCursor, &u.az
1df8c 2e 72 2c 20 30 2c 20 30 2c 20 26 75 2e 61 7a 2e .r, 0, 0, &u.az.
1df8d 72 65 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 res);. if(
1df8e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1df8f 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 . goto ab
1df90 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 ort_due_to_error
1df91 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1df92 75 2e 61 7a 2e 70 43 2d 3e 72 6f 77 69 64 49 73 u.az.pC->rowidIs
1df93 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 7d Valid = 0;. }
1df94 0a 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 64 65 . u.az.pC->de
1df95 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 ferredMoveto = 0
1df96 3b 0a 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 63 ;. u.az.pC->c
1df97 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 acheStatus = CAC
1df98 48 45 5f 53 54 41 4c 45 3b 0a 23 69 66 64 65 66 HE_STALE;.#ifdef
1df99 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 SQLITE_TEST.
1df9a 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f sqlite3_search_
1df9b 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a count++;.#endif.
1df9c 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 6f 63 3d if( u.az.oc=
1df9d 3d 4f 50 5f 53 65 65 6b 47 65 20 7c 7c 20 75 2e =OP_SeekGe || u.
1df9e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 74 az.oc==OP_SeekGt
1df9f 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 75 2e ){. if( u.
1dfa0 61 7a 2e 72 65 73 3c 30 20 7c 7c 20 28 75 2e 61 az.res<0 || (u.a
1dfa1 7a 2e 72 65 73 3d 3d 30 20 26 26 20 75 2e 61 7a z.res==0 && u.az
1dfa2 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 74 29 20 .oc==OP_SeekGt)
1dfa3 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
1dfa4 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 sqlite3BtreeNext
1dfa5 28 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 72 73 6f (u.az.pC->pCurso
1dfa6 72 2c 20 26 75 2e 61 7a 2e 72 65 73 29 3b 0a 20 r, &u.az.res);.
1dfa7 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 if( rc!=S
1dfa8 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 QLITE_OK ) goto
1dfa9 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 abort_due_to_err
1dfaa 6f 72 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 7a or;. u.az
1dfab 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 .pC->rowidIsVali
1dfac 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c d = 0;. }el
1dfad 73 65 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 7a se{. u.az
1dfae 2e 72 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 .res = 0;.
1dfaf 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
1dfb0 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 7a 2e assert( u.az.
1dfb1 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 74 20 7c 7c oc==OP_SeekLt ||
1dfb2 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 u.az.oc==OP_See
1dfb3 6b 4c 65 20 29 3b 0a 20 20 20 20 20 20 69 66 28 kLe );. if(
1dfb4 20 75 2e 61 7a 2e 72 65 73 3e 30 20 7c 7c 20 28 u.az.res>0 || (
1dfb5 75 2e 61 7a 2e 72 65 73 3d 3d 30 20 26 26 20 75 u.az.res==0 && u
1dfb6 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c .az.oc==OP_SeekL
1dfb7 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 t) ){. rc
1dfb8 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 = sqlite3BtreeP
1dfb9 72 65 76 69 6f 75 73 28 75 2e 61 7a 2e 70 43 2d revious(u.az.pC-
1dfba 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e 61 7a 2e >pCursor, &u.az.
1dfbb 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66 res);. if
1dfbc 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1dfbd 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 ) goto abort_due
1dfbe 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 _to_error;.
1dfbf 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 72 6f 77 69 u.az.pC->rowi
1dfc0 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 dIsValid = 0;.
1dfc1 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1dfc2 20 20 20 2f 2a 20 75 2e 61 7a 2e 72 65 73 20 6d /* u.az.res m
1dfc3 69 67 68 74 20 62 65 20 6e 65 67 61 74 69 76 65 ight be negative
1dfc4 20 62 65 63 61 75 73 65 20 74 68 65 20 74 61 62 because the tab
1dfc5 6c 65 20 69 73 20 65 6d 70 74 79 2e 20 20 43 68 le is empty. Ch
1dfc6 65 63 6b 20 74 6f 0a 20 20 20 20 20 20 20 20 2a eck to. *
1dfc7 2a 20 73 65 65 20 69 66 20 74 68 69 73 20 69 73 * see if this is
1dfc8 20 74 68 65 20 63 61 73 65 2e 0a 20 20 20 20 20 the case..
1dfc9 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 2e */. u.
1dfca 61 7a 2e 72 65 73 20 3d 20 73 71 6c 69 74 65 33 az.res = sqlite3
1dfcb 42 74 72 65 65 45 6f 66 28 75 2e 61 7a 2e 70 43 BtreeEof(u.az.pC
1dfcc 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 ->pCursor);.
1dfcd 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 }. }. as
1dfce 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 sert( pOp->p2>0
1dfcf 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 61 7a 2e );. if( u.az.
1dfd0 72 65 73 20 29 7b 0a 20 20 20 20 20 20 70 63 20 res ){. pc
1dfd1 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 = pOp->p2 - 1;.
1dfd2 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
1dfd3 20 20 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e /* This happen
1dfd4 73 20 77 68 65 6e 20 61 74 74 65 6d 70 74 69 6e s when attemptin
1dfd5 67 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 73 71 g to open the sq
1dfd6 6c 69 74 65 33 5f 6d 61 73 74 65 72 20 74 61 62 lite3_master tab
1dfd7 6c 65 0a 20 20 20 20 2a 2a 20 66 6f 72 20 72 65 le. ** for re
1dfd8 61 64 20 61 63 63 65 73 73 20 72 65 74 75 72 6e ad access return
1dfd9 73 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 2e 20 s SQLITE_EMPTY.
1dfda 49 6e 20 74 68 69 73 20 63 61 73 65 20 61 6c 77 In this case alw
1dfdb 61 79 73 0a 20 20 20 20 2a 2a 20 74 61 6b 65 20 ays. ** take
1dfdc 74 68 65 20 6a 75 6d 70 20 28 73 69 6e 63 65 20 the jump (since
1dfdd 74 68 65 72 65 20 61 72 65 20 6e 6f 20 72 65 63 there are no rec
1dfde 6f 72 64 73 20 69 6e 20 74 68 65 20 74 61 62 6c ords in the tabl
1dfdf 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 e).. */. p
1dfe0 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b c = pOp->p2 - 1;
1dfe1 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1dfe2 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b ./* Opcode: Seek
1dfe3 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a P1 P2 * * *.**.
1dfe4 2a 2a 20 50 31 20 69 73 20 61 6e 20 6f 70 65 6e ** P1 is an open
1dfe5 20 74 61 62 6c 65 20 63 75 72 73 6f 72 20 61 6e table cursor an
1dfe6 64 20 50 32 20 69 73 20 61 20 72 6f 77 69 64 20 d P2 is a rowid
1dfe7 69 6e 74 65 67 65 72 2e 20 20 41 72 72 61 6e 67 integer. Arrang
1dfe8 65 0a 2a 2a 20 66 6f 72 20 50 31 20 74 6f 20 6d e.** for P1 to m
1dfe9 6f 76 65 20 73 6f 20 74 68 61 74 20 69 74 20 70 ove so that it p
1dfea 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 72 6f 77 oints to the row
1dfeb 69 64 20 67 69 76 65 6e 20 62 79 20 50 32 2e 0a id given by P2..
1dfec 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 63 **.** This is ac
1dfed 74 75 61 6c 6c 79 20 61 20 64 65 66 65 72 72 65 tually a deferre
1dfee 64 20 73 65 65 6b 2e 20 20 4e 6f 74 68 69 6e 67 d seek. Nothing
1dfef 20 61 63 74 75 61 6c 6c 79 20 68 61 70 70 65 6e actually happen
1dff0 73 20 75 6e 74 69 6c 0a 2a 2a 20 74 68 65 20 63 s until.** the c
1dff1 75 72 73 6f 72 20 69 73 20 75 73 65 64 20 74 6f ursor is used to
1dff2 20 72 65 61 64 20 61 20 72 65 63 6f 72 64 2e 20 read a record.
1dff3 20 54 68 61 74 20 77 61 79 2c 20 69 66 20 6e 6f That way, if no
1dff4 20 72 65 61 64 73 0a 2a 2a 20 6f 63 63 75 72 2c reads.** occur,
1dff5 20 6e 6f 20 75 6e 6e 65 63 65 73 73 61 72 79 20 no unnecessary
1dff6 49 2f 4f 20 68 61 70 70 65 6e 73 2e 0a 2a 2f 0a I/O happens..*/.
1dff7 63 61 73 65 20 4f 50 5f 53 65 65 6b 3a 20 7b 20 case OP_Seek: {
1dff8 20 20 20 2f 2a 20 69 6e 32 20 2a 2f 0a 23 69 66 /* in2 */.#if
1dff9 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 0 /* local var
1dffa 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1dffb 6f 20 75 2e 62 61 20 2a 2f 0a 20 20 56 64 62 65 o u.ba */. Vdbe
1dffc 43 75 72 73 6f 72 20 2a 70 43 3b 0a 23 65 6e 64 Cursor *pC;.#end
1dffd 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1dffe 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1dfff 20 75 2e 62 61 20 2a 2f 0a 0a 20 20 61 73 73 65 u.ba */.. asse
1e000 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 rt( pOp->p1>=0 &
1e001 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 & pOp->p1<p->nCu
1e002 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 61 2e 70 rsor );. u.ba.p
1e003 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 C = p->apCsr[pOp
1e004 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 ->p1];. assert(
1e005 20 75 2e 62 61 2e 70 43 21 3d 30 20 29 3b 0a 20 u.ba.pC!=0 );.
1e006 20 69 66 28 20 41 4c 57 41 59 53 28 75 2e 62 61 if( ALWAYS(u.ba
1e007 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 29 .pC->pCursor!=0)
1e008 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
1e009 75 2e 62 61 2e 70 43 2d 3e 69 73 54 61 62 6c 65 u.ba.pC->isTable
1e00a 20 29 3b 0a 20 20 20 20 75 2e 62 61 2e 70 43 2d );. u.ba.pC-
1e00b 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 20 20 >nullRow = 0;.
1e00c 20 20 75 2e 62 61 2e 70 43 2d 3e 6d 6f 76 65 74 u.ba.pC->movet
1e00d 6f 54 61 72 67 65 74 20 3d 20 73 71 6c 69 74 65 oTarget = sqlite
1e00e 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 3VdbeIntValue(pI
1e00f 6e 32 29 3b 0a 20 20 20 20 75 2e 62 61 2e 70 43 n2);. u.ba.pC
1e010 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d ->rowidIsValid =
1e011 20 30 3b 0a 20 20 20 20 75 2e 62 61 2e 70 43 2d 0;. u.ba.pC-
1e012 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 >deferredMoveto
1e013 3d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b = 1;. }. break
1e014 3b 0a 7d 0a 20 20 0a 0a 2f 2a 20 4f 70 63 6f 64 ;.}. ../* Opcod
1e015 65 3a 20 46 6f 75 6e 64 20 50 31 20 50 32 20 50 e: Found P1 P2 P
1e016 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 67 69 3 * *.**.** Regi
1e017 73 74 65 72 20 50 33 20 68 6f 6c 64 73 20 61 20 ster P3 holds a
1e018 62 6c 6f 62 20 63 6f 6e 73 74 72 75 63 74 65 64 blob constructed
1e019 20 62 79 20 4d 61 6b 65 52 65 63 6f 72 64 2e 20 by MakeRecord.
1e01a 20 50 31 20 69 73 20 61 6e 20 69 6e 64 65 78 2e P1 is an index.
1e01b 0a 2a 2a 20 49 66 20 61 6e 20 65 6e 74 72 79 20 .** If an entry
1e01c 74 68 61 74 20 6d 61 74 63 68 65 73 20 74 68 65 that matches the
1e01d 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 value in regist
1e01e 65 72 20 70 33 20 65 78 69 73 74 73 20 69 6e 20 er p3 exists in
1e01f 50 31 20 74 68 65 6e 0a 2a 2a 20 6a 75 6d 70 20 P1 then.** jump
1e020 74 6f 20 50 32 2e 20 20 49 66 20 74 68 65 20 50 to P2. If the P
1e021 33 20 76 61 6c 75 65 20 64 6f 65 73 20 6e 6f 74 3 value does not
1e022 20 6d 61 74 63 68 20 61 6e 79 20 65 6e 74 72 79 match any entry
1e023 20 69 6e 20 50 31 0a 2a 2a 20 74 68 65 6e 20 66 in P1.** then f
1e024 61 6c 6c 20 74 68 72 75 2e 20 20 54 68 65 20 50 all thru. The P
1e025 31 20 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74 1 cursor is left
1e026 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 74 68 65 pointing at the
1e027 20 6d 61 74 63 68 69 6e 67 20 65 6e 74 72 79 0a matching entry.
1e028 2a 2a 20 69 66 20 69 74 20 65 78 69 73 74 73 2e ** if it exists.
1e029 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 .**.** This inst
1e02a 72 75 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 ruction is used
1e02b 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 to implement the
1e02c 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 77 68 65 IN operator whe
1e02d 72 65 20 74 68 65 0a 2a 2a 20 6c 65 66 74 2d 68 re the.** left-h
1e02e 61 6e 64 20 73 69 64 65 20 69 73 20 61 20 53 45 and side is a SE
1e02f 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 LECT statement.
1e030 20 50 31 20 6d 61 79 20 62 65 20 61 20 74 72 75 P1 may be a tru
1e031 65 20 69 6e 64 65 78 2c 20 6f 72 20 69 74 0a 2a e index, or it.*
1e032 2a 20 6d 61 79 20 62 65 20 61 20 74 65 6d 70 6f * may be a tempo
1e033 72 61 72 79 20 69 6e 64 65 78 20 74 68 61 74 20 rary index that
1e034 68 6f 6c 64 73 20 74 68 65 20 72 65 73 75 6c 74 holds the result
1e035 73 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 0a s of the SELECT.
1e036 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 20 ** statement.
1e037 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e This instruction
1e038 20 69 73 20 61 6c 73 6f 20 75 73 65 64 20 74 6f is also used to
1e039 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 0a 2a implement the.*
1e03a 2a 20 44 49 53 54 49 4e 43 54 20 6b 65 79 77 6f * DISTINCT keywo
1e03b 72 64 20 69 6e 20 53 45 4c 45 43 54 20 73 74 61 rd in SELECT sta
1e03c 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 tements..**.** T
1e03d 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 his instruction
1e03e 63 68 65 63 6b 73 20 69 66 20 69 6e 64 65 78 20 checks if index
1e03f 50 31 20 63 6f 6e 74 61 69 6e 73 20 61 20 72 65 P1 contains a re
1e040 63 6f 72 64 20 66 6f 72 20 77 68 69 63 68 20 0a cord for which .
1e041 2a 2a 20 74 68 65 20 66 69 72 73 74 20 4e 20 73 ** the first N s
1e042 65 72 69 61 6c 69 7a 65 64 20 76 61 6c 75 65 73 erialized values
1e043 20 65 78 61 63 74 6c 79 20 6d 61 74 63 68 20 74 exactly match t
1e044 68 65 20 4e 20 73 65 72 69 61 6c 69 7a 65 64 20 he N serialized
1e045 76 61 6c 75 65 73 0a 2a 2a 20 69 6e 20 74 68 65 values.** in the
1e046 20 72 65 63 6f 72 64 20 69 6e 20 72 65 67 69 73 record in regis
1e047 74 65 72 20 50 33 2c 20 77 68 65 72 65 20 4e 20 ter P3, where N
1e048 69 73 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d is the total num
1e049 62 65 72 20 6f 66 20 76 61 6c 75 65 73 20 69 6e ber of values in
1e04a 0a 2a 2a 20 74 68 65 20 50 33 20 72 65 63 6f 72 .** the P3 recor
1e04b 64 20 28 74 68 65 20 50 33 20 72 65 63 6f 72 64 d (the P3 record
1e04c 20 69 73 20 61 20 70 72 65 66 69 78 20 6f 66 20 is a prefix of
1e04d 74 68 65 20 50 31 20 72 65 63 6f 72 64 29 2e 20 the P1 record).
1e04e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a .**.** See also:
1e04f 20 4e 6f 74 46 6f 75 6e 64 2c 20 49 73 55 6e 69 NotFound, IsUni
1e050 71 75 65 2c 20 4e 6f 74 45 78 69 73 74 73 0a 2a que, NotExists.*
1e051 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 74 /./* Opcode: Not
1e052 46 6f 75 6e 64 20 50 31 20 50 32 20 50 33 20 2a Found P1 P2 P3 *
1e053 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 *.**.** Registe
1e054 72 20 50 33 20 68 6f 6c 64 73 20 61 20 62 6c 6f r P3 holds a blo
1e055 62 20 63 6f 6e 73 74 72 75 63 74 65 64 20 62 79 b constructed by
1e056 20 4d 61 6b 65 52 65 63 6f 72 64 2e 20 20 50 31 MakeRecord. P1
1e057 20 69 73 0a 2a 2a 20 61 6e 20 69 6e 64 65 78 2e is.** an index.
1e058 20 20 49 66 20 6e 6f 20 65 6e 74 72 79 20 65 78 If no entry ex
1e059 69 73 74 73 20 69 6e 20 50 31 20 74 68 61 74 20 ists in P1 that
1e05a 6d 61 74 63 68 65 73 20 74 68 65 20 62 6c 6f 62 matches the blob
1e05b 20 74 68 65 6e 20 6a 75 6d 70 0a 2a 2a 20 74 6f then jump.** to
1e05c 20 50 32 2e 20 20 49 66 20 61 6e 20 65 6e 74 72 P2. If an entr
1e05d 79 20 64 6f 65 73 20 65 78 69 73 74 69 6e 67 2c y does existing,
1e05e 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 2e 20 20 fall through.
1e05f 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65 The cursor is le
1e060 66 74 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 20 74 ft.** pointing t
1e061 6f 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 o the entry that
1e062 20 6d 61 74 63 68 65 73 2e 0a 2a 2a 0a 2a 2a 20 matches..**.**
1e063 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c See also: Found,
1e064 20 4e 6f 74 45 78 69 73 74 73 2c 20 49 73 55 6e NotExists, IsUn
1e065 69 71 75 65 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ique.*/.case OP_
1e066 4e 6f 74 46 6f 75 6e 64 3a 20 20 20 20 20 20 20 NotFound:
1e067 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a /* jump, in3 */.
1e068 63 61 73 65 20 4f 50 5f 46 6f 75 6e 64 3a 20 7b case OP_Found: {
1e069 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c /* jump,
1e06a 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f in3 */.#if 0 /
1e06b 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1e06c 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 s moved into u.b
1e06d 62 20 2a 2f 0a 20 20 69 6e 74 20 61 6c 72 65 61 b */. int alrea
1e06e 64 79 45 78 69 73 74 73 3b 0a 20 20 56 64 62 65 dyExists;. Vdbe
1e06f 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 69 6e Cursor *pC;. in
1e070 74 20 72 65 73 3b 0a 20 20 55 6e 70 61 63 6b 65 t res;. Unpacke
1e071 64 52 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79 dRecord *pIdxKey
1e072 3b 0a 20 20 63 68 61 72 20 61 54 65 6d 70 52 65 ;. char aTempRe
1e073 63 5b 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 c[ROUND8(sizeof(
1e074 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 29 29 UnpackedRecord))
1e075 20 2b 20 73 69 7a 65 6f 66 28 4d 65 6d 29 2a 33 + sizeof(Mem)*3
1e076 20 2b 20 37 5d 3b 0a 23 65 6e 64 69 66 20 2f 2a + 7];.#endif /*
1e077 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1e078 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 62 moved into u.bb
1e079 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 */..#ifdef SQLI
1e07a 54 45 5f 54 45 53 54 0a 20 20 73 71 6c 69 74 65 TE_TEST. sqlite
1e07b 33 5f 66 6f 75 6e 64 5f 63 6f 75 6e 74 2b 2b 3b 3_found_count++;
1e07c 0a 23 65 6e 64 69 66 0a 0a 20 20 75 2e 62 62 2e .#endif.. u.bb.
1e07d 61 6c 72 65 61 64 79 45 78 69 73 74 73 20 3d 20 alreadyExists =
1e07e 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 0;. assert( pOp
1e07f 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e ->p1>=0 && pOp->
1e080 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b p1<p->nCursor );
1e081 0a 20 20 75 2e 62 62 2e 70 43 20 3d 20 70 2d 3e . u.bb.pC = p->
1e082 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a apCsr[pOp->p1];.
1e083 20 20 61 73 73 65 72 74 28 20 75 2e 62 62 2e 70 assert( u.bb.p
1e084 43 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 41 4c C!=0 );. if( AL
1e085 57 41 59 53 28 75 2e 62 62 2e 70 43 2d 3e 70 43 WAYS(u.bb.pC->pC
1e086 75 72 73 6f 72 21 3d 30 29 20 29 7b 0a 0a 20 20 ursor!=0) ){..
1e087 20 20 61 73 73 65 72 74 28 20 75 2e 62 62 2e 70 assert( u.bb.p
1e088 43 2d 3e 69 73 54 61 62 6c 65 3d 3d 30 20 29 3b C->isTable==0 );
1e089 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 49 6e . assert( pIn
1e08a 33 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 3->flags & MEM_B
1e08b 6c 6f 62 20 29 3b 0a 20 20 20 20 45 78 70 61 6e lob );. Expan
1e08c 64 42 6c 6f 62 28 70 49 6e 33 29 3b 0a 20 20 20 dBlob(pIn3);.
1e08d 20 75 2e 62 62 2e 70 49 64 78 4b 65 79 20 3d 20 u.bb.pIdxKey =
1e08e 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 sqlite3VdbeRecor
1e08f 64 55 6e 70 61 63 6b 28 75 2e 62 62 2e 70 43 2d dUnpack(u.bb.pC-
1e090 3e 70 4b 65 79 49 6e 66 6f 2c 20 70 49 6e 33 2d >pKeyInfo, pIn3-
1e091 3e 6e 2c 20 70 49 6e 33 2d 3e 7a 2c 0a 20 20 20 >n, pIn3->z,.
1e092 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e093 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e094 20 20 20 75 2e 62 62 2e 61 54 65 6d 70 52 65 63 u.bb.aTempRec
1e095 2c 20 73 69 7a 65 6f 66 28 75 2e 62 62 2e 61 54 , sizeof(u.bb.aT
1e096 65 6d 70 52 65 63 29 29 3b 0a 20 20 20 20 69 66 empRec));. if
1e097 28 20 75 2e 62 62 2e 70 49 64 78 4b 65 79 3d 3d ( u.bb.pIdxKey==
1e098 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 0 ){. goto
1e099 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 no_mem;. }.
1e09a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 if( pOp->opcod
1e09b 65 3d 3d 4f 50 5f 46 6f 75 6e 64 20 29 7b 0a 20 e==OP_Found ){.
1e09c 20 20 20 20 20 75 2e 62 62 2e 70 49 64 78 4b 65 u.bb.pIdxKe
1e09d 79 2d 3e 66 6c 61 67 73 20 7c 3d 20 55 4e 50 41 y->flags |= UNPA
1e09e 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 43 CKED_PREFIX_MATC
1e09f 48 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 H;. }. rc
1e0a0 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f = sqlite3BtreeMo
1e0a1 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e 62 vetoUnpacked(u.b
1e0a2 62 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 75 b.pC->pCursor, u
1e0a3 2e 62 62 2e 70 49 64 78 4b 65 79 2c 20 30 2c 20 .bb.pIdxKey, 0,
1e0a4 30 2c 20 26 75 2e 62 62 2e 72 65 73 29 3b 0a 20 0, &u.bb.res);.
1e0a5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 sqlite3VdbeDe
1e0a6 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f leteUnpackedReco
1e0a7 72 64 28 75 2e 62 62 2e 70 49 64 78 4b 65 79 29 rd(u.bb.pIdxKey)
1e0a8 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
1e0a9 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1e0aa 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
1e0ab 20 20 75 2e 62 62 2e 61 6c 72 65 61 64 79 45 78 u.bb.alreadyEx
1e0ac 69 73 74 73 20 3d 20 28 75 2e 62 62 2e 72 65 73 ists = (u.bb.res
1e0ad 3d 3d 30 29 3b 0a 20 20 20 20 75 2e 62 62 2e 70 ==0);. u.bb.p
1e0ae 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 C->deferredMovet
1e0af 6f 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 62 2e o = 0;. u.bb.
1e0b0 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 pC->cacheStatus
1e0b1 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 = CACHE_STALE;.
1e0b2 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 }. if( pOp->op
1e0b3 63 6f 64 65 3d 3d 4f 50 5f 46 6f 75 6e 64 20 29 code==OP_Found )
1e0b4 7b 0a 20 20 20 20 69 66 28 20 75 2e 62 62 2e 61 {. if( u.bb.a
1e0b5 6c 72 65 61 64 79 45 78 69 73 74 73 20 29 20 70 lreadyExists ) p
1e0b6 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b c = pOp->p2 - 1;
1e0b7 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 . }else{. if
1e0b8 28 20 21 75 2e 62 62 2e 61 6c 72 65 61 64 79 45 ( !u.bb.alreadyE
1e0b9 78 69 73 74 73 20 29 20 70 63 20 3d 20 70 4f 70 xists ) pc = pOp
1e0ba 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 ->p2 - 1;. }.
1e0bb 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1e0bc 6f 64 65 3a 20 49 73 55 6e 69 71 75 65 20 50 31 ode: IsUnique P1
1e0bd 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a P2 P3 P4 *.**.*
1e0be 2a 20 43 75 72 73 6f 72 20 50 31 20 69 73 20 6f * Cursor P1 is o
1e0bf 70 65 6e 20 6f 6e 20 61 6e 20 69 6e 64 65 78 2e pen on an index.
1e0c0 20 20 53 6f 20 69 74 20 68 61 73 20 6e 6f 20 64 So it has no d
1e0c1 61 74 61 20 61 6e 64 20 69 74 73 20 6b 65 79 20 ata and its key
1e0c2 63 6f 6e 73 69 73 74 73 20 0a 2a 2a 20 6f 66 20 consists .** of
1e0c3 61 20 72 65 63 6f 72 64 20 67 65 6e 65 72 61 74 a record generat
1e0c4 65 64 20 62 79 20 4f 50 5f 4d 61 6b 65 52 65 63 ed by OP_MakeRec
1e0c5 6f 72 64 20 77 68 65 72 65 20 74 68 65 20 6c 61 ord where the la
1e0c6 73 74 20 66 69 65 6c 64 20 69 73 20 74 68 65 20 st field is the
1e0c7 0a 2a 2a 20 72 6f 77 69 64 20 6f 66 20 74 68 65 .** rowid of the
1e0c8 20 65 6e 74 72 79 20 74 68 61 74 20 74 68 65 20 entry that the
1e0c9 69 6e 64 65 78 20 72 65 66 65 72 73 20 74 6f 2e index refers to.
1e0ca 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 33 20 72 65 .**.** The P3 re
1e0cb 67 69 73 74 65 72 20 63 6f 6e 74 61 69 6e 73 20 gister contains
1e0cc 61 6e 20 69 6e 74 65 67 65 72 20 72 65 63 6f 72 an integer recor
1e0cd 64 20 6e 75 6d 62 65 72 2e 20 43 61 6c 6c 20 74 d number. Call t
1e0ce 68 69 73 20 72 65 63 6f 72 64 20 0a 2a 2a 20 6e his record .** n
1e0cf 75 6d 62 65 72 20 52 2e 20 52 65 67 69 73 74 65 umber R. Registe
1e0d0 72 20 50 34 20 69 73 20 74 68 65 20 66 69 72 73 r P4 is the firs
1e0d1 74 20 69 6e 20 61 20 73 65 74 20 6f 66 20 4e 20 t in a set of N
1e0d2 63 6f 6e 74 69 67 75 6f 75 73 20 72 65 67 69 73 contiguous regis
1e0d3 74 65 72 73 0a 2a 2a 20 74 68 61 74 20 6d 61 6b ters.** that mak
1e0d4 65 20 75 70 20 61 6e 20 75 6e 70 61 63 6b 65 64 e up an unpacked
1e0d5 20 69 6e 64 65 78 20 6b 65 79 20 74 68 61 74 20 index key that
1e0d6 63 61 6e 20 62 65 20 75 73 65 64 20 77 69 74 68 can be used with
1e0d7 20 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2a 20 54 cursor P1..** T
1e0d8 68 65 20 76 61 6c 75 65 20 6f 66 20 4e 20 63 61 he value of N ca
1e0d9 6e 20 62 65 20 69 6e 66 65 72 72 65 64 20 66 72 n be inferred fr
1e0da 6f 6d 20 74 68 65 20 63 75 72 73 6f 72 2e 20 4e om the cursor. N
1e0db 20 69 6e 63 6c 75 64 65 73 20 74 68 65 20 72 6f includes the ro
1e0dc 77 69 64 0a 2a 2a 20 76 61 6c 75 65 20 61 70 70 wid.** value app
1e0dd 65 6e 64 65 64 20 74 6f 20 74 68 65 20 65 6e 64 ended to the end
1e0de 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 72 65 of the index re
1e0df 63 6f 72 64 2e 20 54 68 69 73 20 72 6f 77 69 64 cord. This rowid
1e0e0 20 76 61 6c 75 65 20 6d 61 79 0a 2a 2a 20 6f 72 value may.** or
1e0e1 20 6d 61 79 20 6e 6f 74 20 62 65 20 74 68 65 20 may not be the
1e0e2 73 61 6d 65 20 61 73 20 52 2e 0a 2a 2a 0a 2a 2a same as R..**.**
1e0e3 20 49 66 20 61 6e 79 20 6f 66 20 74 68 65 20 4e If any of the N
1e0e4 20 72 65 67 69 73 74 65 72 73 20 62 65 67 69 6e registers begin
1e0e5 6e 69 6e 67 20 77 69 74 68 20 72 65 67 69 73 74 ning with regist
1e0e6 65 72 20 50 34 20 63 6f 6e 74 61 69 6e 73 20 61 er P4 contains a
1e0e7 20 4e 55 4c 4c 0a 2a 2a 20 76 61 6c 75 65 2c 20 NULL.** value,
1e0e8 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 jump immediately
1e0e9 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 4f 74 to P2..**.** Ot
1e0ea 68 65 72 77 69 73 65 2c 20 74 68 69 73 20 69 6e herwise, this in
1e0eb 73 74 72 75 63 74 69 6f 6e 20 63 68 65 63 6b 73 struction checks
1e0ec 20 69 66 20 63 75 72 73 6f 72 20 50 31 20 63 6f if cursor P1 co
1e0ed 6e 74 61 69 6e 73 20 61 6e 20 65 6e 74 72 79 0a ntains an entry.
1e0ee 2a 2a 20 77 68 65 72 65 20 74 68 65 20 66 69 72 ** where the fir
1e0ef 73 74 20 28 4e 2d 31 29 20 66 69 65 6c 64 73 20 st (N-1) fields
1e0f0 6d 61 74 63 68 20 62 75 74 20 74 68 65 20 72 6f match but the ro
1e0f1 77 69 64 20 76 61 6c 75 65 20 61 74 20 74 68 65 wid value at the
1e0f2 20 65 6e 64 0a 2a 2a 20 6f 66 20 74 68 65 20 69 end.** of the i
1e0f3 6e 64 65 78 20 65 6e 74 72 79 20 69 73 20 6e 6f ndex entry is no
1e0f4 74 20 52 2e 20 49 66 20 74 68 65 72 65 20 69 73 t R. If there is
1e0f5 20 6e 6f 20 73 75 63 68 20 65 6e 74 72 79 2c 20 no such entry,
1e0f6 63 6f 6e 74 72 6f 6c 20 6a 75 6d 70 73 0a 2a 2a control jumps.**
1e0f7 20 74 6f 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 to instruction
1e0f8 50 32 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 74 P2. Otherwise, t
1e0f9 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 he rowid of the
1e0fa 63 6f 6e 66 6c 69 63 74 69 6e 67 20 69 6e 64 65 conflicting inde
1e0fb 78 0a 2a 2a 20 65 6e 74 72 79 20 69 73 20 63 6f x.** entry is co
1e0fc 70 69 65 64 20 74 6f 20 72 65 67 69 73 74 65 72 pied to register
1e0fd 20 50 33 20 61 6e 64 20 63 6f 6e 74 72 6f 6c 20 P3 and control
1e0fe 66 61 6c 6c 73 20 74 68 72 6f 75 67 68 20 74 6f falls through to
1e0ff 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 69 6e 73 the next.** ins
1e100 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 truction..**.**
1e101 53 65 65 20 61 6c 73 6f 3a 20 4e 6f 74 46 6f 75 See also: NotFou
1e102 6e 64 2c 20 4e 6f 74 45 78 69 73 74 73 2c 20 46 nd, NotExists, F
1e103 6f 75 6e 64 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ound.*/.case OP_
1e104 49 73 55 6e 69 71 75 65 3a 20 7b 20 20 20 20 20 IsUnique: {
1e105 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 /* jump, in3
1e106 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 */.#if 0 /* loc
1e107 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1e108 65 64 20 69 6e 74 6f 20 75 2e 62 63 20 2a 2f 0a ed into u.bc */.
1e109 20 20 75 31 36 20 69 69 3b 0a 20 20 56 64 62 65 u16 ii;. Vdbe
1e10a 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a 20 20 42 Cursor *pCx;. B
1e10b 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a tCursor *pCrsr;.
1e10c 20 20 75 31 36 20 6e 46 69 65 6c 64 3b 0a 20 20 u16 nField;.
1e10d 4d 65 6d 20 2a 61 4d 65 6d 3b 0a 20 20 55 6e 70 Mem *aMem;. Unp
1e10e 61 63 6b 65 64 52 65 63 6f 72 64 20 72 3b 20 20 ackedRecord r;
1e10f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e110 2f 2a 20 42 2d 54 72 65 65 20 69 6e 64 65 78 20 /* B-Tree index
1e111 73 65 61 72 63 68 20 6b 65 79 20 2a 2f 0a 20 20 search key */.
1e112 69 36 34 20 52 3b 20 20 20 20 20 20 20 20 20 20 i64 R;
1e113 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e114 20 20 20 2f 2a 20 52 6f 77 69 64 20 73 74 6f 72 /* Rowid stor
1e115 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ed in register P
1e116 33 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 3 */.#endif /* l
1e117 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1e118 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 63 20 2a oved into u.bc *
1e119 2f 0a 0a 20 20 75 2e 62 63 2e 61 4d 65 6d 20 3d /.. u.bc.aMem =
1e11a 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 &p->aMem[pOp->p
1e11b 34 2e 69 5d 3b 0a 20 20 2f 2a 20 41 73 73 65 72 4.i];. /* Asser
1e11c 74 20 74 68 61 74 20 74 68 65 20 76 61 6c 75 65 t that the value
1e11d 73 20 6f 66 20 70 61 72 61 6d 65 74 65 72 73 20 s of parameters
1e11e 50 31 20 61 6e 64 20 50 34 20 61 72 65 20 69 6e P1 and P4 are in
1e11f 20 72 61 6e 67 65 2e 20 2a 2f 0a 20 20 61 73 73 range. */. ass
1e120 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 ert( pOp->p4type
1e121 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 ==P4_INT32 );.
1e122 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e assert( pOp->p4.
1e123 69 3e 30 20 26 26 20 70 4f 70 2d 3e 70 34 2e 69 i>0 && pOp->p4.i
1e124 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 61 <=p->nMem );. a
1e125 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d ssert( pOp->p1>=
1e126 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 0 && pOp->p1<p->
1e127 6e 43 75 72 73 6f 72 20 29 3b 0a 0a 20 20 2f 2a nCursor );.. /*
1e128 20 46 69 6e 64 20 74 68 65 20 69 6e 64 65 78 20 Find the index
1e129 63 75 72 73 6f 72 2e 20 2a 2f 0a 20 20 75 2e 62 cursor. */. u.b
1e12a 63 2e 70 43 78 20 3d 20 70 2d 3e 61 70 43 73 72 c.pCx = p->apCsr
1e12b 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 [pOp->p1];. ass
1e12c 65 72 74 28 20 75 2e 62 63 2e 70 43 78 2d 3e 64 ert( u.bc.pCx->d
1e12d 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 eferredMoveto==0
1e12e 20 29 3b 0a 20 20 75 2e 62 63 2e 70 43 78 2d 3e );. u.bc.pCx->
1e12f 73 65 65 6b 52 65 73 75 6c 74 20 3d 20 30 3b 0a seekResult = 0;.
1e130 20 20 75 2e 62 63 2e 70 43 78 2d 3e 63 61 63 68 u.bc.pCx->cach
1e131 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f eStatus = CACHE_
1e132 53 54 41 4c 45 3b 0a 20 20 75 2e 62 63 2e 70 43 STALE;. u.bc.pC
1e133 72 73 72 20 3d 20 75 2e 62 63 2e 70 43 78 2d 3e rsr = u.bc.pCx->
1e134 70 43 75 72 73 6f 72 3b 0a 0a 20 20 2f 2a 20 49 pCursor;.. /* I
1e135 66 20 61 6e 79 20 6f 66 20 74 68 65 20 76 61 6c f any of the val
1e136 75 65 73 20 61 72 65 20 4e 55 4c 4c 2c 20 74 61 ues are NULL, ta
1e137 6b 65 20 74 68 65 20 6a 75 6d 70 2e 20 2a 2f 0a ke the jump. */.
1e138 20 20 75 2e 62 63 2e 6e 46 69 65 6c 64 20 3d 20 u.bc.nField =
1e139 75 2e 62 63 2e 70 43 78 2d 3e 70 4b 65 79 49 6e u.bc.pCx->pKeyIn
1e13a 66 6f 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 66 6f fo->nField;. fo
1e13b 72 28 75 2e 62 63 2e 69 69 3d 30 3b 20 75 2e 62 r(u.bc.ii=0; u.b
1e13c 63 2e 69 69 3c 75 2e 62 63 2e 6e 46 69 65 6c 64 c.ii<u.bc.nField
1e13d 3b 20 75 2e 62 63 2e 69 69 2b 2b 29 7b 0a 20 20 ; u.bc.ii++){.
1e13e 20 20 69 66 28 20 75 2e 62 63 2e 61 4d 65 6d 5b if( u.bc.aMem[
1e13f 75 2e 62 63 2e 69 69 5d 2e 66 6c 61 67 73 20 26 u.bc.ii].flags &
1e140 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 MEM_Null ){.
1e141 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 pc = pOp->p2
1e142 2d 20 31 3b 0a 20 20 20 20 20 20 75 2e 62 63 2e - 1;. u.bc.
1e143 70 43 72 73 72 20 3d 20 30 3b 0a 20 20 20 20 20 pCrsr = 0;.
1e144 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
1e145 7d 0a 20 20 61 73 73 65 72 74 28 20 28 75 2e 62 }. assert( (u.b
1e146 63 2e 61 4d 65 6d 5b 75 2e 62 63 2e 6e 46 69 65 c.aMem[u.bc.nFie
1e147 6c 64 5d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f ld].flags & MEM_
1e148 4e 75 6c 6c 29 3d 3d 30 20 29 3b 0a 0a 20 20 69 Null)==0 );.. i
1e149 66 28 20 75 2e 62 63 2e 70 43 72 73 72 21 3d 30 f( u.bc.pCrsr!=0
1e14a 20 29 7b 0a 20 20 20 20 2f 2a 20 50 6f 70 75 6c ){. /* Popul
1e14b 61 74 65 20 74 68 65 20 69 6e 64 65 78 20 73 65 ate the index se
1e14c 61 72 63 68 20 6b 65 79 2e 20 2a 2f 0a 20 20 20 arch key. */.
1e14d 20 75 2e 62 63 2e 72 2e 70 4b 65 79 49 6e 66 6f u.bc.r.pKeyInfo
1e14e 20 3d 20 75 2e 62 63 2e 70 43 78 2d 3e 70 4b 65 = u.bc.pCx->pKe
1e14f 79 49 6e 66 6f 3b 0a 20 20 20 20 75 2e 62 63 2e yInfo;. u.bc.
1e150 72 2e 6e 46 69 65 6c 64 20 3d 20 75 2e 62 63 2e r.nField = u.bc.
1e151 6e 46 69 65 6c 64 20 2b 20 31 3b 0a 20 20 20 20 nField + 1;.
1e152 75 2e 62 63 2e 72 2e 66 6c 61 67 73 20 3d 20 55 u.bc.r.flags = U
1e153 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 NPACKED_PREFIX_S
1e154 45 41 52 43 48 3b 0a 20 20 20 20 75 2e 62 63 2e EARCH;. u.bc.
1e155 72 2e 61 4d 65 6d 20 3d 20 75 2e 62 63 2e 61 4d r.aMem = u.bc.aM
1e156 65 6d 3b 0a 0a 20 20 20 20 2f 2a 20 45 78 74 72 em;.. /* Extr
1e157 61 63 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 act the value of
1e158 20 75 2e 62 63 2e 52 20 66 72 6f 6d 20 72 65 67 u.bc.R from reg
1e159 69 73 74 65 72 20 50 33 2e 20 2a 2f 0a 20 20 20 ister P3. */.
1e15a 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 sqlite3VdbeMemI
1e15b 6e 74 65 67 65 72 69 66 79 28 70 49 6e 33 29 3b ntegerify(pIn3);
1e15c 0a 20 20 20 20 75 2e 62 63 2e 52 20 3d 20 70 49 . u.bc.R = pI
1e15d 6e 33 2d 3e 75 2e 69 3b 0a 0a 20 20 20 20 2f 2a n3->u.i;.. /*
1e15e 20 53 65 61 72 63 68 20 74 68 65 20 42 2d 54 72 Search the B-Tr
1e15f 65 65 20 69 6e 64 65 78 2e 20 49 66 20 6e 6f 20 ee index. If no
1e160 63 6f 6e 66 6c 69 63 74 69 6e 67 20 72 65 63 6f conflicting reco
1e161 72 64 20 69 73 20 66 6f 75 6e 64 2c 20 6a 75 6d rd is found, jum
1e162 70 0a 20 20 20 20 2a 2a 20 74 6f 20 50 32 2e 20 p. ** to P2.
1e163 4f 74 68 65 72 77 69 73 65 2c 20 63 6f 70 79 20 Otherwise, copy
1e164 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 the rowid of the
1e165 20 63 6f 6e 66 6c 69 63 74 69 6e 67 20 72 65 63 conflicting rec
1e166 6f 72 64 20 74 6f 0a 20 20 20 20 2a 2a 20 72 65 ord to. ** re
1e167 67 69 73 74 65 72 20 50 33 20 61 6e 64 20 66 61 gister P3 and fa
1e168 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 ll through to th
1e169 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 e next instructi
1e16a 6f 6e 2e 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d on. */. rc =
1e16b 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 sqlite3BtreeMov
1e16c 65 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e 62 63 etoUnpacked(u.bc
1e16d 2e 70 43 72 73 72 2c 20 26 75 2e 62 63 2e 72 2c .pCrsr, &u.bc.r,
1e16e 20 30 2c 20 30 2c 20 26 75 2e 62 63 2e 70 43 78 0, 0, &u.bc.pCx
1e16f 2d 3e 73 65 65 6b 52 65 73 75 6c 74 29 3b 0a 20 ->seekResult);.
1e170 20 20 20 69 66 28 20 28 75 2e 62 63 2e 72 2e 66 if( (u.bc.r.f
1e171 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f lags & UNPACKED_
1e172 50 52 45 46 49 58 5f 53 45 41 52 43 48 29 20 7c PREFIX_SEARCH) |
1e173 7c 20 75 2e 62 63 2e 72 2e 72 6f 77 69 64 3d 3d | u.bc.r.rowid==
1e174 75 2e 62 63 2e 52 20 29 7b 0a 20 20 20 20 20 20 u.bc.R ){.
1e175 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 pc = pOp->p2 - 1
1e176 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
1e177 20 20 20 70 49 6e 33 2d 3e 75 2e 69 20 3d 20 75 pIn3->u.i = u
1e178 2e 62 63 2e 72 2e 72 6f 77 69 64 3b 0a 20 20 20 .bc.r.rowid;.
1e179 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a }. }. break;.
1e17a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f }../* Opcode: No
1e17b 74 45 78 69 73 74 73 20 50 31 20 50 32 20 50 33 tExists P1 P2 P3
1e17c 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 * *.**.** Use t
1e17d 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 he content of re
1e17e 67 69 73 74 65 72 20 50 33 20 61 73 20 61 20 69 gister P3 as a i
1e17f 6e 74 65 67 65 72 20 6b 65 79 2e 20 20 49 66 20 nteger key. If
1e180 61 20 72 65 63 6f 72 64 20 0a 2a 2a 20 77 69 74 a record .** wit
1e181 68 20 74 68 61 74 20 6b 65 79 20 64 6f 65 73 20 h that key does
1e182 6e 6f 74 20 65 78 69 73 74 20 69 6e 20 74 61 62 not exist in tab
1e183 6c 65 20 6f 66 20 50 31 2c 20 74 68 65 6e 20 6a le of P1, then j
1e184 75 6d 70 20 74 6f 20 50 32 2e 20 0a 2a 2a 20 49 ump to P2. .** I
1e185 66 20 74 68 65 20 72 65 63 6f 72 64 20 64 6f 65 f the record doe
1e186 73 20 65 78 69 73 74 2c 20 74 68 65 6e 20 66 61 s exist, then fa
1e187 6c 6c 20 74 68 72 75 2e 20 20 54 68 65 20 63 75 ll thru. The cu
1e188 72 73 6f 72 20 69 73 20 6c 65 66 74 20 0a 2a 2a rsor is left .**
1e189 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 pointing to the
1e18a 20 72 65 63 6f 72 64 20 69 66 20 69 74 20 65 78 record if it ex
1e18b 69 73 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ists..**.** The
1e18c 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 difference betwe
1e18d 65 6e 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f en this operatio
1e18e 6e 20 61 6e 64 20 4e 6f 74 46 6f 75 6e 64 20 69 n and NotFound i
1e18f 73 20 74 68 61 74 20 74 68 69 73 0a 2a 2a 20 6f s that this.** o
1e190 70 65 72 61 74 69 6f 6e 20 61 73 73 75 6d 65 73 peration assumes
1e191 20 74 68 65 20 6b 65 79 20 69 73 20 61 6e 20 69 the key is an i
1e192 6e 74 65 67 65 72 20 61 6e 64 20 74 68 61 74 20 nteger and that
1e193 50 31 20 69 73 20 61 20 74 61 62 6c 65 20 77 68 P1 is a table wh
1e194 65 72 65 61 73 0a 2a 2a 20 4e 6f 74 46 6f 75 6e ereas.** NotFoun
1e195 64 20 61 73 73 75 6d 65 73 20 6b 65 79 20 69 73 d assumes key is
1e196 20 61 20 62 6c 6f 62 20 63 6f 6e 73 74 72 75 63 a blob construc
1e197 74 65 64 20 66 72 6f 6d 20 4d 61 6b 65 52 65 63 ted from MakeRec
1e198 6f 72 64 20 61 6e 64 0a 2a 2a 20 50 31 20 69 73 ord and.** P1 is
1e199 20 61 6e 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a an index..**.**
1e19a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 See also: Found
1e19b 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 49 73 55 6e , NotFound, IsUn
1e19c 69 71 75 65 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ique.*/.case OP_
1e19d 4e 6f 74 45 78 69 73 74 73 3a 20 7b 20 20 20 20 NotExists: {
1e19e 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 /* jump, in3
1e19f 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f */.#if 0 /* lo
1e1a0 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1e1a1 76 65 64 20 69 6e 74 6f 20 75 2e 62 64 20 2a 2f ved into u.bd */
1e1a2 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 . VdbeCursor *p
1e1a3 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 C;. BtCursor *p
1e1a4 43 72 73 72 3b 0a 20 20 69 6e 74 20 72 65 73 3b Crsr;. int res;
1e1a5 0a 20 20 75 36 34 20 69 4b 65 79 3b 0a 23 65 6e . u64 iKey;.#en
1e1a6 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 dif /* local var
1e1a7 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1e1a8 6f 20 75 2e 62 64 20 2a 2f 0a 0a 20 20 61 73 73 o u.bd */.. ass
1e1a9 65 72 74 28 20 70 49 6e 33 2d 3e 66 6c 61 67 73 ert( pIn3->flags
1e1aa 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 & MEM_Int );.
1e1ab 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e assert( pOp->p1>
1e1ac 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d =0 && pOp->p1<p-
1e1ad 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e >nCursor );. u.
1e1ae 62 64 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 bd.pC = p->apCsr
1e1af 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 [pOp->p1];. ass
1e1b0 65 72 74 28 20 75 2e 62 64 2e 70 43 21 3d 30 20 ert( u.bd.pC!=0
1e1b1 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 );. assert( u.b
1e1b2 64 2e 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b d.pC->isTable );
1e1b3 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 64 2e . assert( u.bd.
1e1b4 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 52 pC->pseudoTableR
1e1b5 65 67 3d 3d 30 20 29 3b 0a 20 20 75 2e 62 64 2e eg==0 );. u.bd.
1e1b6 70 43 72 73 72 20 3d 20 75 2e 62 64 2e 70 43 2d pCrsr = u.bd.pC-
1e1b7 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 >pCursor;. if(
1e1b8 75 2e 62 64 2e 70 43 72 73 72 21 3d 30 20 29 7b u.bd.pCrsr!=0 ){
1e1b9 0a 20 20 20 20 75 2e 62 64 2e 72 65 73 20 3d 20 . u.bd.res =
1e1ba 30 3b 0a 20 20 20 20 75 2e 62 64 2e 69 4b 65 79 0;. u.bd.iKey
1e1bb 20 3d 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 = pIn3->u.i;.
1e1bc 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1e1bd 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 reeMovetoUnpacke
1e1be 64 28 75 2e 62 64 2e 70 43 72 73 72 2c 20 30 2c d(u.bd.pCrsr, 0,
1e1bf 20 75 2e 62 64 2e 69 4b 65 79 2c 20 30 2c 20 26 u.bd.iKey, 0, &
1e1c0 75 2e 62 64 2e 72 65 73 29 3b 0a 20 20 20 20 75 u.bd.res);. u
1e1c1 2e 62 64 2e 70 43 2d 3e 6c 61 73 74 52 6f 77 69 .bd.pC->lastRowi
1e1c2 64 20 3d 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 d = pIn3->u.i;.
1e1c3 20 20 20 75 2e 62 64 2e 70 43 2d 3e 72 6f 77 69 u.bd.pC->rowi
1e1c4 64 49 73 56 61 6c 69 64 20 3d 20 75 2e 62 64 2e dIsValid = u.bd.
1e1c5 72 65 73 3d 3d 30 20 3f 31 3a 30 3b 0a 20 20 20 res==0 ?1:0;.
1e1c6 20 75 2e 62 64 2e 70 43 2d 3e 6e 75 6c 6c 52 6f u.bd.pC->nullRo
1e1c7 77 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 64 2e w = 0;. u.bd.
1e1c8 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 pC->cacheStatus
1e1c9 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 = CACHE_STALE;.
1e1ca 20 20 20 75 2e 62 64 2e 70 43 2d 3e 64 65 66 65 u.bd.pC->defe
1e1cb 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a rredMoveto = 0;.
1e1cc 20 20 20 20 69 66 28 20 75 2e 62 64 2e 72 65 73 if( u.bd.res
1e1cd 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 63 20 !=0 ){. pc
1e1ce 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 = pOp->p2 - 1;.
1e1cf 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 assert( u.b
1e1d0 64 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c d.pC->rowidIsVal
1e1d1 69 64 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 0a 20 id==0 );. }.
1e1d2 20 20 20 75 2e 62 64 2e 70 43 2d 3e 73 65 65 6b u.bd.pC->seek
1e1d3 52 65 73 75 6c 74 20 3d 20 75 2e 62 64 2e 72 65 Result = u.bd.re
1e1d4 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 s;. }else{.
1e1d5 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 /* This happens
1e1d6 77 68 65 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 when an attempt
1e1d7 74 6f 20 6f 70 65 6e 20 61 20 72 65 61 64 20 63 to open a read c
1e1d8 75 72 73 6f 72 20 6f 6e 20 74 68 65 0a 20 20 20 ursor on the.
1e1d9 20 2a 2a 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 ** sqlite_maste
1e1da 72 20 74 61 62 6c 65 20 72 65 74 75 72 6e 73 20 r table returns
1e1db 53 51 4c 49 54 45 5f 45 4d 50 54 59 2e 0a 20 20 SQLITE_EMPTY..
1e1dc 20 20 2a 2f 0a 20 20 20 20 70 63 20 3d 20 70 4f */. pc = pO
1e1dd 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 61 p->p2 - 1;. a
1e1de 73 73 65 72 74 28 20 75 2e 62 64 2e 70 43 2d 3e ssert( u.bd.pC->
1e1df 72 6f 77 69 64 49 73 56 61 6c 69 64 3d 3d 30 20 rowidIsValid==0
1e1e0 29 3b 0a 20 20 20 20 75 2e 62 64 2e 70 43 2d 3e );. u.bd.pC->
1e1e1 73 65 65 6b 52 65 73 75 6c 74 20 3d 20 30 3b 0a seekResult = 0;.
1e1e2 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1e1e3 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 71 75 65 /* Opcode: Seque
1e1e4 6e 63 65 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a nce P1 P2 * * *.
1e1e5 2a 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 6e **.** Find the n
1e1e6 65 78 74 20 61 76 61 69 6c 61 62 6c 65 20 73 65 ext available se
1e1e7 71 75 65 6e 63 65 20 6e 75 6d 62 65 72 20 66 6f quence number fo
1e1e8 72 20 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2a 20 r cursor P1..**
1e1e9 57 72 69 74 65 20 74 68 65 20 73 65 71 75 65 6e Write the sequen
1e1ea 63 65 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 72 ce number into r
1e1eb 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 20 54 egister P2..** T
1e1ec 68 65 20 73 65 71 75 65 6e 63 65 20 6e 75 6d 62 he sequence numb
1e1ed 65 72 20 6f 6e 20 74 68 65 20 63 75 72 73 6f 72 er on the cursor
1e1ee 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 is incremented
1e1ef 61 66 74 65 72 20 74 68 69 73 0a 2a 2a 20 69 6e after this.** in
1e1f0 73 74 72 75 63 74 69 6f 6e 2e 20 20 0a 2a 2f 0a struction. .*/.
1e1f1 63 61 73 65 20 4f 50 5f 53 65 71 75 65 6e 63 65 case OP_Sequence
1e1f2 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a : { /*
1e1f3 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 out2-prerelease
1e1f4 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f */. assert( pO
1e1f5 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d p->p1>=0 && pOp-
1e1f6 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 >p1<p->nCursor )
1e1f7 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 ;. assert( p->a
1e1f8 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 21 3d 30 pCsr[pOp->p1]!=0
1e1f9 20 29 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 );. pOut->u.i
1e1fa 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e = p->apCsr[pOp->
1e1fb 70 31 5d 2d 3e 73 65 71 43 6f 75 6e 74 2b 2b 3b p1]->seqCount++;
1e1fc 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 . MemSetTypeFla
1e1fd 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 g(pOut, MEM_Int)
1e1fe 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f ;. break;.}.../
1e1ff 2a 20 4f 70 63 6f 64 65 3a 20 4e 65 77 52 6f 77 * Opcode: NewRow
1e200 69 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a id P1 P2 P3 * *.
1e201 2a 2a 0a 2a 2a 20 47 65 74 20 61 20 6e 65 77 20 **.** Get a new
1e202 69 6e 74 65 67 65 72 20 72 65 63 6f 72 64 20 6e integer record n
1e203 75 6d 62 65 72 20 28 61 2e 6b 2e 61 20 22 72 6f umber (a.k.a "ro
1e204 77 69 64 22 29 20 75 73 65 64 20 61 73 20 74 68 wid") used as th
1e205 65 20 6b 65 79 20 74 6f 20 61 20 74 61 62 6c 65 e key to a table
1e206 2e 0a 2a 2a 20 54 68 65 20 72 65 63 6f 72 64 20 ..** The record
1e207 6e 75 6d 62 65 72 20 69 73 20 6e 6f 74 20 70 72 number is not pr
1e208 65 76 69 6f 75 73 6c 79 20 75 73 65 64 20 61 73 eviously used as
1e209 20 61 20 6b 65 79 20 69 6e 20 74 68 65 20 64 61 a key in the da
1e20a 74 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 20 tabase.** table
1e20b 74 68 61 74 20 63 75 72 73 6f 72 20 50 31 20 70 that cursor P1 p
1e20c 6f 69 6e 74 73 20 74 6f 2e 20 20 54 68 65 20 6e oints to. The n
1e20d 65 77 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 ew record number
1e20e 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 77 is written.** w
1e20f 72 69 74 74 65 6e 20 74 6f 20 72 65 67 69 73 74 ritten to regist
1e210 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 er P2..**.** If
1e211 50 33 3e 30 20 74 68 65 6e 20 50 33 20 69 73 20 P3>0 then P3 is
1e212 61 20 72 65 67 69 73 74 65 72 20 69 6e 20 74 68 a register in th
1e213 65 20 72 6f 6f 74 20 66 72 61 6d 65 20 6f 66 20 e root frame of
1e214 74 68 69 73 20 56 44 42 45 20 74 68 61 74 20 68 this VDBE that h
1e215 6f 6c 64 73 20 0a 2a 2a 20 74 68 65 20 6c 61 72 olds .** the lar
1e216 67 65 73 74 20 70 72 65 76 69 6f 75 73 6c 79 20 gest previously
1e217 67 65 6e 65 72 61 74 65 64 20 72 65 63 6f 72 64 generated record
1e218 20 6e 75 6d 62 65 72 2e 20 4e 6f 20 6e 65 77 20 number. No new
1e219 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 73 20 61 record numbers a
1e21a 72 65 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20 74 6f re.** allowed to
1e21b 20 62 65 20 6c 65 73 73 20 74 68 61 6e 20 74 68 be less than th
1e21c 69 73 20 76 61 6c 75 65 2e 20 57 68 65 6e 20 74 is value. When t
1e21d 68 69 73 20 76 61 6c 75 65 20 72 65 61 63 68 65 his value reache
1e21e 73 20 69 74 73 20 6d 61 78 69 6d 75 6d 2c 20 0a s its maximum, .
1e21f 2a 2a 20 61 20 53 51 4c 49 54 45 5f 46 55 4c 4c ** a SQLITE_FULL
1e220 20 65 72 72 6f 72 20 69 73 20 67 65 6e 65 72 61 error is genera
1e221 74 65 64 2e 20 54 68 65 20 50 33 20 72 65 67 69 ted. The P3 regi
1e222 73 74 65 72 20 69 73 20 75 70 64 61 74 65 64 20 ster is updated
1e223 77 69 74 68 20 74 68 65 20 27 0a 2a 2a 20 67 65 with the '.** ge
1e224 6e 65 72 61 74 65 64 20 72 65 63 6f 72 64 20 6e nerated record n
1e225 75 6d 62 65 72 2e 20 54 68 69 73 20 50 33 20 6d umber. This P3 m
1e226 65 63 68 61 6e 69 73 6d 20 69 73 20 75 73 65 64 echanism is used
1e227 20 74 6f 20 68 65 6c 70 20 69 6d 70 6c 65 6d 65 to help impleme
1e228 6e 74 20 74 68 65 0a 2a 2a 20 41 55 54 4f 49 4e nt the.** AUTOIN
1e229 43 52 45 4d 45 4e 54 20 66 65 61 74 75 72 65 2e CREMENT feature.
1e22a 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 77 52 .*/.case OP_NewR
1e22b 6f 77 69 64 3a 20 7b 20 20 20 20 20 20 20 20 20 owid: {
1e22c 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c /* out2-prerel
1e22d 65 61 73 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f ease */.#if 0 /
1e22e 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1e22f 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 s moved into u.b
1e230 65 20 2a 2f 0a 20 20 69 36 34 20 76 3b 20 20 20 e */. i64 v;
1e231 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1e232 20 54 68 65 20 6e 65 77 20 72 6f 77 69 64 20 2a The new rowid *
1e233 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a /. VdbeCursor *
1e234 70 43 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 75 pC; /* Cu
1e235 72 73 6f 72 20 6f 66 20 74 61 62 6c 65 20 74 6f rsor of table to
1e236 20 67 65 74 20 74 68 65 20 6e 65 77 20 72 6f 77 get the new row
1e237 69 64 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 3b id */. int res;
1e238 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1e239 2a 20 52 65 73 75 6c 74 20 6f 66 20 61 6e 20 73 * Result of an s
1e23a 71 6c 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 qlite3BtreeLast(
1e23b 29 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20 ) */. int cnt;
1e23c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1e23d 20 43 6f 75 6e 74 65 72 20 74 6f 20 6c 69 6d 69 Counter to limi
1e23e 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 t the number of
1e23f 73 65 61 72 63 68 65 73 20 2a 2f 0a 20 20 4d 65 searches */. Me
1e240 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 20 m *pMem;
1e241 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 /* Register
1e242 20 68 6f 6c 64 69 6e 67 20 6c 61 72 67 65 73 74 holding largest
1e243 20 72 6f 77 69 64 20 66 6f 72 20 41 55 54 4f 49 rowid for AUTOI
1e244 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 20 20 56 64 NCREMENT */. Vd
1e245 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b beFrame *pFrame;
1e246 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 66 72 61 /* Root fra
1e247 6d 65 20 6f 66 20 56 44 42 45 20 2a 2f 0a 23 65 me of VDBE */.#e
1e248 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1e249 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1e24a 74 6f 20 75 2e 62 65 20 2a 2f 0a 0a 20 20 75 2e to u.be */.. u.
1e24b 62 65 2e 76 20 3d 20 30 3b 0a 20 20 75 2e 62 65 be.v = 0;. u.be
1e24c 2e 72 65 73 20 3d 20 30 3b 0a 20 20 61 73 73 65 .res = 0;. asse
1e24d 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 rt( pOp->p1>=0 &
1e24e 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 & pOp->p1<p->nCu
1e24f 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 65 2e 70 rsor );. u.be.p
1e250 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 C = p->apCsr[pOp
1e251 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 ->p1];. assert(
1e252 20 75 2e 62 65 2e 70 43 21 3d 30 20 29 3b 0a 20 u.be.pC!=0 );.
1e253 20 69 66 28 20 4e 45 56 45 52 28 75 2e 62 65 2e if( NEVER(u.be.
1e254 70 43 2d 3e 70 43 75 72 73 6f 72 3d 3d 30 29 20 pC->pCursor==0)
1e255 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 7a 65 ){. /* The ze
1e256 72 6f 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f ro initializatio
1e257 6e 20 61 62 6f 76 65 20 69 73 20 61 6c 6c 20 74 n above is all t
1e258 68 61 74 20 69 73 20 6e 65 65 64 65 64 20 2a 2f hat is needed */
1e259 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
1e25a 20 54 68 65 20 6e 65 78 74 20 72 6f 77 69 64 20 The next rowid
1e25b 6f 72 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 or record number
1e25c 20 28 64 69 66 66 65 72 65 6e 74 20 74 65 72 6d (different term
1e25d 73 20 66 6f 72 20 74 68 65 20 73 61 6d 65 0a 20 s for the same.
1e25e 20 20 20 2a 2a 20 74 68 69 6e 67 29 20 69 73 20 ** thing) is
1e25f 6f 62 74 61 69 6e 65 64 20 69 6e 20 61 20 74 77 obtained in a tw
1e260 6f 2d 73 74 65 70 20 61 6c 67 6f 72 69 74 68 6d o-step algorithm
1e261 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
1e262 46 69 72 73 74 20 77 65 20 61 74 74 65 6d 70 74 First we attempt
1e263 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6c 61 72 to find the lar
1e264 67 65 73 74 20 65 78 69 73 74 69 6e 67 20 72 6f gest existing ro
1e265 77 69 64 20 61 6e 64 20 61 64 64 20 6f 6e 65 0a wid and add one.
1e266 20 20 20 20 2a 2a 20 74 6f 20 74 68 61 74 2e 20 ** to that.
1e267 20 42 75 74 20 69 66 20 74 68 65 20 6c 61 72 67 But if the larg
1e268 65 73 74 20 65 78 69 73 74 69 6e 67 20 72 6f 77 est existing row
1e269 69 64 20 69 73 20 61 6c 72 65 61 64 79 20 74 68 id is already th
1e26a 65 20 6d 61 78 69 6d 75 6d 0a 20 20 20 20 2a 2a e maximum. **
1e26b 20 70 6f 73 69 74 69 76 65 20 69 6e 74 65 67 65 positive intege
1e26c 72 2c 20 77 65 20 68 61 76 65 20 74 6f 20 66 61 r, we have to fa
1e26d 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 ll through to th
1e26e 65 20 73 65 63 6f 6e 64 0a 20 20 20 20 2a 2a 20 e second. **
1e26f 70 72 6f 62 61 62 69 6c 69 73 74 69 63 20 61 6c probabilistic al
1e270 67 6f 72 69 74 68 6d 0a 20 20 20 20 2a 2a 0a 20 gorithm. **.
1e271 20 20 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 ** The second
1e272 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 20 74 6f algorithm is to
1e273 20 73 65 6c 65 63 74 20 61 20 72 6f 77 69 64 20 select a rowid
1e274 61 74 20 72 61 6e 64 6f 6d 20 61 6e 64 20 73 65 at random and se
1e275 65 20 69 66 0a 20 20 20 20 2a 2a 20 69 74 20 61 e if. ** it a
1e276 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 69 6e lready exists in
1e277 20 74 68 65 20 74 61 62 6c 65 2e 20 20 49 66 20 the table. If
1e278 69 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 it does not exis
1e279 74 2c 20 77 65 20 68 61 76 65 0a 20 20 20 20 2a t, we have. *
1e27a 2a 20 73 75 63 63 65 65 64 65 64 2e 20 20 49 66 * succeeded. If
1e27b 20 74 68 65 20 72 61 6e 64 6f 6d 20 72 6f 77 69 the random rowi
1e27c 64 20 64 6f 65 73 20 65 78 69 73 74 2c 20 77 65 d does exist, we
1e27d 20 73 65 6c 65 63 74 20 61 20 6e 65 77 20 6f 6e select a new on
1e27e 65 0a 20 20 20 20 2a 2a 20 61 6e 64 20 74 72 79 e. ** and try
1e27f 20 61 67 61 69 6e 2c 20 75 70 20 74 6f 20 31 30 again, up to 10
1e280 30 20 74 69 6d 65 73 2e 0a 20 20 20 20 2a 2f 0a 0 times.. */.
1e281 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 65 assert( u.be
1e282 2e 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a .pC->isTable );.
1e283 20 20 20 20 75 2e 62 65 2e 63 6e 74 20 3d 20 30 u.be.cnt = 0
1e284 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 ;..#ifdef SQLITE
1e285 5f 33 32 42 49 54 5f 52 4f 57 49 44 0a 23 20 20 _32BIT_ROWID.#
1e286 20 64 65 66 69 6e 65 20 4d 41 58 5f 52 4f 57 49 define MAX_ROWI
1e287 44 20 30 78 37 66 66 66 66 66 66 66 0a 23 65 6c D 0x7fffffff.#el
1e288 73 65 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 20 63 se. /* Some c
1e289 6f 6d 70 69 6c 65 72 73 20 63 6f 6d 70 6c 61 69 ompilers complai
1e28a 6e 20 61 62 6f 75 74 20 63 6f 6e 73 74 61 6e 74 n about constant
1e28b 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 30 78 s of the form 0x
1e28c 37 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 7fffffffffffffff
1e28d 2e 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72 73 20 .. ** Others
1e28e 63 6f 6d 70 6c 61 69 6e 20 61 62 6f 75 74 20 30 complain about 0
1e28f 78 37 66 66 66 66 66 66 66 66 66 66 66 66 66 66 x7ffffffffffffff
1e290 66 66 4c 4c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f ffLL. The follo
1e291 77 69 6e 67 20 6d 61 63 72 6f 20 73 65 65 6d 73 wing macro seems
1e292 0a 20 20 20 20 2a 2a 20 74 6f 20 70 72 6f 76 69 . ** to provi
1e293 64 65 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 de the constant
1e294 77 68 69 6c 65 20 6d 61 6b 69 6e 67 20 61 6c 6c while making all
1e295 20 63 6f 6d 70 69 6c 65 72 73 20 68 61 70 70 79 compilers happy
1e296 2e 0a 20 20 20 20 2a 2f 0a 23 20 20 20 64 65 66 .. */.# def
1e297 69 6e 65 20 4d 41 58 5f 52 4f 57 49 44 20 20 28 ine MAX_ROWID (
1e298 69 36 34 29 28 20 28 28 28 75 36 34 29 30 78 37 i64)( (((u64)0x7
1e299 66 66 66 66 66 66 66 29 3c 3c 33 32 29 20 7c 20 fffffff)<<32) |
1e29a 28 75 36 34 29 30 78 66 66 66 66 66 66 66 66 20 (u64)0xffffffff
1e29b 29 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 69 66 ).#endif.. if
1e29c 28 20 21 75 2e 62 65 2e 70 43 2d 3e 75 73 65 52 ( !u.be.pC->useR
1e29d 61 6e 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20 andomRowid ){.
1e29e 20 20 20 20 75 2e 62 65 2e 76 20 3d 20 73 71 6c u.be.v = sql
1e29f 69 74 65 33 42 74 72 65 65 47 65 74 43 61 63 68 ite3BtreeGetCach
1e2a0 65 64 52 6f 77 69 64 28 75 2e 62 65 2e 70 43 2d edRowid(u.be.pC-
1e2a1 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 >pCursor);.
1e2a2 20 69 66 28 20 75 2e 62 65 2e 76 3d 3d 30 20 29 if( u.be.v==0 )
1e2a3 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 {. rc = s
1e2a4 71 6c 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 qlite3BtreeLast(
1e2a5 75 2e 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f 72 u.be.pC->pCursor
1e2a6 2c 20 26 75 2e 62 65 2e 72 65 73 29 3b 0a 20 20 , &u.be.res);.
1e2a7 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
1e2a8 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1e2a9 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f goto abort_
1e2aa 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 due_to_error;.
1e2ab 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1e2ac 69 66 28 20 75 2e 62 65 2e 72 65 73 20 29 7b 0a if( u.be.res ){.
1e2ad 20 20 20 20 20 20 20 20 20 20 75 2e 62 65 2e 76 u.be.v
1e2ae 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 = 1;. }e
1e2af 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 61 lse{. a
1e2b0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 ssert( sqlite3Bt
1e2b1 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64 reeCursorIsValid
1e2b2 28 75 2e 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f (u.be.pC->pCurso
1e2b3 72 29 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 r) );.
1e2b4 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1e2b5 65 4b 65 79 53 69 7a 65 28 75 2e 62 65 2e 70 43 eKeySize(u.be.pC
1e2b6 2d 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e 62 65 ->pCursor, &u.be
1e2b7 2e 76 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 .v);. a
1e2b8 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 ssert( rc==SQLIT
1e2b9 45 5f 4f 4b 20 29 3b 20 20 20 2f 2a 20 43 61 6e E_OK ); /* Can
1e2ba 6e 6f 74 20 66 61 69 6c 20 66 6f 6c 6c 6f 77 69 not fail followi
1e2bb 6e 67 20 42 74 72 65 65 4c 61 73 74 28 29 20 2a ng BtreeLast() *
1e2bc 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 /. if(
1e2bd 75 2e 62 65 2e 76 3d 3d 4d 41 58 5f 52 4f 57 49 u.be.v==MAX_ROWI
1e2be 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 D ){.
1e2bf 20 75 2e 62 65 2e 70 43 2d 3e 75 73 65 52 61 6e u.be.pC->useRan
1e2c0 64 6f 6d 52 6f 77 69 64 20 3d 20 31 3b 0a 20 20 domRowid = 1;.
1e2c1 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
1e2c2 20 20 20 20 20 20 20 20 20 20 20 75 2e 62 65 2e u.be.
1e2c3 76 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d v++;. }
1e2c4 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1e2c5 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 }..#ifndef SQLI
1e2c6 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 TE_OMIT_AUTOINCR
1e2c7 45 4d 45 4e 54 0a 20 20 20 20 20 20 69 66 28 20 EMENT. if(
1e2c8 70 4f 70 2d 3e 70 33 20 29 7b 0a 20 20 20 20 20 pOp->p3 ){.
1e2c9 20 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 /* Assert tha
1e2ca 74 20 50 33 20 69 73 20 61 20 76 61 6c 69 64 20 t P3 is a valid
1e2cb 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e 20 2a 2f 0a memory cell. */.
1e2cc 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
1e2cd 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20 pOp->p3>0 );.
1e2ce 20 20 20 20 20 69 66 28 20 70 2d 3e 70 46 72 61 if( p->pFra
1e2cf 6d 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 me ){.
1e2d0 66 6f 72 28 75 2e 62 65 2e 70 46 72 61 6d 65 3d for(u.be.pFrame=
1e2d1 70 2d 3e 70 46 72 61 6d 65 3b 20 75 2e 62 65 2e p->pFrame; u.be.
1e2d2 70 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 3b pFrame->pParent;
1e2d3 20 75 2e 62 65 2e 70 46 72 61 6d 65 3d 75 2e 62 u.be.pFrame=u.b
1e2d4 65 2e 70 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e e.pFrame->pParen
1e2d5 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a t);. /*
1e2d6 20 41 73 73 65 72 74 20 74 68 61 74 20 50 33 20 Assert that P3
1e2d7 69 73 20 61 20 76 61 6c 69 64 20 6d 65 6d 6f 72 is a valid memor
1e2d8 79 20 63 65 6c 6c 2e 20 2a 2f 0a 20 20 20 20 20 y cell. */.
1e2d9 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 assert( pOp
1e2da 2d 3e 70 33 3c 3d 75 2e 62 65 2e 70 46 72 61 6d ->p3<=u.be.pFram
1e2db 65 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 e->nMem );.
1e2dc 20 20 20 20 20 75 2e 62 65 2e 70 4d 65 6d 20 3d u.be.pMem =
1e2dd 20 26 75 2e 62 65 2e 70 46 72 61 6d 65 2d 3e 61 &u.be.pFrame->a
1e2de 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 Mem[pOp->p3];.
1e2df 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1e2e0 20 20 20 20 20 20 20 2f 2a 20 41 73 73 65 72 74 /* Assert
1e2e1 20 74 68 61 74 20 50 33 20 69 73 20 61 20 76 61 that P3 is a va
1e2e2 6c 69 64 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e lid memory cell.
1e2e3 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 61 73 */. as
1e2e4 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 70 sert( pOp->p3<=p
1e2e5 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 ->nMem );.
1e2e6 20 20 20 20 75 2e 62 65 2e 70 4d 65 6d 20 3d 20 u.be.pMem =
1e2e7 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 &p->aMem[pOp->p3
1e2e8 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 ];. }..
1e2e9 20 20 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 REGISTER_T
1e2ea 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e RACE(pOp->p3, u.
1e2eb 62 65 2e 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20 be.pMem);.
1e2ec 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1e2ed 49 6e 74 65 67 65 72 69 66 79 28 75 2e 62 65 2e Integerify(u.be.
1e2ee 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 61 pMem);. a
1e2ef 73 73 65 72 74 28 20 28 75 2e 62 65 2e 70 4d 65 ssert( (u.be.pMe
1e2f0 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 m->flags & MEM_I
1e2f1 6e 74 29 21 3d 30 20 29 3b 20 20 2f 2a 20 6d 65 nt)!=0 ); /* me
1e2f2 6d 28 50 33 29 20 68 6f 6c 64 73 20 61 6e 20 69 m(P3) holds an i
1e2f3 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 nteger */.
1e2f4 20 20 69 66 28 20 75 2e 62 65 2e 70 4d 65 6d 2d if( u.be.pMem-
1e2f5 3e 75 2e 69 3d 3d 4d 41 58 5f 52 4f 57 49 44 20 >u.i==MAX_ROWID
1e2f6 7c 7c 20 75 2e 62 65 2e 70 43 2d 3e 75 73 65 52 || u.be.pC->useR
1e2f7 61 6e 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20 andomRowid ){.
1e2f8 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
1e2f9 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 20 20 ITE_FULL;.
1e2fa 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 goto abort_d
1e2fb 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 ue_to_error;.
1e2fc 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
1e2fd 66 28 20 75 2e 62 65 2e 76 3c 75 2e 62 65 2e 70 f( u.be.v<u.be.p
1e2fe 4d 65 6d 2d 3e 75 2e 69 2b 31 20 29 7b 0a 20 20 Mem->u.i+1 ){.
1e2ff 20 20 20 20 20 20 20 20 75 2e 62 65 2e 76 20 3d u.be.v =
1e300 20 75 2e 62 65 2e 70 4d 65 6d 2d 3e 75 2e 69 20 u.be.pMem->u.i
1e301 2b 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 + 1;. }.
1e302 20 20 20 20 20 20 20 75 2e 62 65 2e 70 4d 65 6d u.be.pMem
1e303 2d 3e 75 2e 69 20 3d 20 75 2e 62 65 2e 76 3b 0a ->u.i = u.be.v;.
1e304 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a }.#endif..
1e305 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 sqlite3Btr
1e306 65 65 53 65 74 43 61 63 68 65 64 52 6f 77 69 64 eeSetCachedRowid
1e307 28 75 2e 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f (u.be.pC->pCurso
1e308 72 2c 20 75 2e 62 65 2e 76 3c 4d 41 58 5f 52 4f r, u.be.v<MAX_RO
1e309 57 49 44 20 3f 20 75 2e 62 65 2e 76 2b 31 20 3a WID ? u.be.v+1 :
1e30a 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 0);. }. i
1e30b 66 28 20 75 2e 62 65 2e 70 43 2d 3e 75 73 65 52 f( u.be.pC->useR
1e30c 61 6e 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20 andomRowid ){.
1e30d 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d assert( pOp-
1e30e 3e 70 33 3d 3d 30 20 29 3b 20 20 2f 2a 20 57 65 >p3==0 ); /* We
1e30f 20 63 61 6e 6e 6f 74 20 62 65 20 69 6e 20 72 61 cannot be in ra
1e310 6e 64 6f 6d 20 72 6f 77 69 64 20 6d 6f 64 65 20 ndom rowid mode
1e311 69 66 20 74 68 69 73 20 69 73 0a 20 20 20 20 20 if this is.
1e312 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e313 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 20 41 55 ** an AU
1e314 54 4f 49 4e 43 52 45 4d 45 4e 54 20 74 61 62 6c TOINCREMENT tabl
1e315 65 2e 20 2a 2f 0a 20 20 20 20 20 20 75 2e 62 65 e. */. u.be
1e316 2e 76 20 3d 20 64 62 2d 3e 6c 61 73 74 52 6f 77 .v = db->lastRow
1e317 69 64 3b 0a 20 20 20 20 20 20 75 2e 62 65 2e 63 id;. u.be.c
1e318 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 64 6f nt = 0;. do
1e319 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e {. if( u.
1e31a 62 65 2e 63 6e 74 3d 3d 30 20 26 26 20 28 75 2e be.cnt==0 && (u.
1e31b 62 65 2e 76 26 30 78 66 66 66 66 66 66 29 3d 3d be.v&0xffffff)==
1e31c 75 2e 62 65 2e 76 20 29 7b 0a 20 20 20 20 20 20 u.be.v ){.
1e31d 20 20 20 20 75 2e 62 65 2e 76 2b 2b 3b 0a 20 20 u.be.v++;.
1e31e 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1e31f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 sqlite3_r
1e320 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 andomness(sizeof
1e321 28 75 2e 62 65 2e 76 29 2c 20 26 75 2e 62 65 2e (u.be.v), &u.be.
1e322 76 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 v);. if
1e323 28 20 75 2e 62 65 2e 63 6e 74 3c 35 20 29 20 75 ( u.be.cnt<5 ) u
1e324 2e 62 65 2e 76 20 26 3d 20 30 78 66 66 66 66 66 .be.v &= 0xfffff
1e325 66 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 f;. }.
1e326 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1e327 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 3BtreeMovetoUnpa
1e328 63 6b 65 64 28 75 2e 62 65 2e 70 43 2d 3e 70 43 cked(u.be.pC->pC
1e329 75 72 73 6f 72 2c 20 30 2c 20 28 75 36 34 29 75 ursor, 0, (u64)u
1e32a 2e 62 65 2e 76 2c 20 30 2c 20 26 75 2e 62 65 2e .be.v, 0, &u.be.
1e32b 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20 75 2e res);. u.
1e32c 62 65 2e 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 be.cnt++;.
1e32d 7d 77 68 69 6c 65 28 20 75 2e 62 65 2e 63 6e 74 }while( u.be.cnt
1e32e 3c 31 30 30 20 26 26 20 72 63 3d 3d 53 51 4c 49 <100 && rc==SQLI
1e32f 54 45 5f 4f 4b 20 26 26 20 75 2e 62 65 2e 72 65 TE_OK && u.be.re
1e330 73 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 s==0 );. if
1e331 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1e332 26 26 20 75 2e 62 65 2e 72 65 73 3d 3d 30 20 29 && u.be.res==0 )
1e333 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 {. rc = S
1e334 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 QLITE_FULL;.
1e335 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 goto abort_d
1e336 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 ue_to_error;.
1e337 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 75 }. }. u
1e338 2e 62 65 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 .be.pC->rowidIsV
1e339 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 75 2e alid = 0;. u.
1e33a 62 65 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d be.pC->deferredM
1e33b 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 75 oveto = 0;. u
1e33c 2e 62 65 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 .be.pC->cacheSta
1e33d 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c tus = CACHE_STAL
1e33e 45 3b 0a 20 20 7d 0a 20 20 4d 65 6d 53 65 74 54 E;. }. MemSetT
1e33f 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 ypeFlag(pOut, ME
1e340 4d 5f 49 6e 74 29 3b 0a 20 20 70 4f 75 74 2d 3e M_Int);. pOut->
1e341 75 2e 69 20 3d 20 75 2e 62 65 2e 76 3b 0a 20 20 u.i = u.be.v;.
1e342 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1e343 6f 64 65 3a 20 49 6e 73 65 72 74 20 50 31 20 50 ode: Insert P1 P
1e344 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 2 P3 P4 P5.**.**
1e345 20 57 72 69 74 65 20 61 6e 20 65 6e 74 72 79 20 Write an entry
1e346 69 6e 74 6f 20 74 68 65 20 74 61 62 6c 65 20 6f into the table o
1e347 66 20 63 75 72 73 6f 72 20 50 31 2e 20 20 41 20 f cursor P1. A
1e348 6e 65 77 20 65 6e 74 72 79 20 69 73 0a 2a 2a 20 new entry is.**
1e349 63 72 65 61 74 65 64 20 69 66 20 69 74 20 64 6f created if it do
1e34a 65 73 6e 27 74 20 61 6c 72 65 61 64 79 20 65 78 esn't already ex
1e34b 69 73 74 20 6f 72 20 74 68 65 20 64 61 74 61 20 ist or the data
1e34c 66 6f 72 20 61 6e 20 65 78 69 73 74 69 6e 67 0a for an existing.
1e34d 2a 2a 20 65 6e 74 72 79 20 69 73 20 6f 76 65 72 ** entry is over
1e34e 77 72 69 74 74 65 6e 2e 20 20 54 68 65 20 64 61 written. The da
1e34f 74 61 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 ta is the value
1e350 4d 45 4d 5f 42 6c 6f 62 20 73 74 6f 72 65 64 20 MEM_Blob stored
1e351 69 6e 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 6e in register.** n
1e352 75 6d 62 65 72 20 50 32 2e 20 54 68 65 20 6b 65 umber P2. The ke
1e353 79 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72 y is stored in r
1e354 65 67 69 73 74 65 72 20 50 33 2e 20 54 68 65 20 egister P3. The
1e355 6b 65 79 20 6d 75 73 74 0a 2a 2a 20 62 65 20 61 key must.** be a
1e356 20 4d 45 4d 5f 49 6e 74 2e 0a 2a 2a 0a 2a 2a 20 MEM_Int..**.**
1e357 49 66 20 74 68 65 20 4f 50 46 4c 41 47 5f 4e 43 If the OPFLAG_NC
1e358 48 41 4e 47 45 20 66 6c 61 67 20 6f 66 20 50 35 HANGE flag of P5
1e359 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68 is set, then th
1e35a 65 20 72 6f 77 20 63 68 61 6e 67 65 20 63 6f 75 e row change cou
1e35b 6e 74 20 69 73 0a 2a 2a 20 69 6e 63 72 65 6d 65 nt is.** increme
1e35c 6e 74 65 64 20 28 6f 74 68 65 72 77 69 73 65 20 nted (otherwise
1e35d 6e 6f 74 29 2e 20 20 49 66 20 74 68 65 20 4f 50 not). If the OP
1e35e 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49 44 20 66 FLAG_LASTROWID f
1e35f 6c 61 67 20 6f 66 20 50 35 20 69 73 20 73 65 74 lag of P5 is set
1e360 2c 0a 2a 2a 20 74 68 65 6e 20 72 6f 77 69 64 20 ,.** then rowid
1e361 69 73 20 73 74 6f 72 65 64 20 66 6f 72 20 73 75 is stored for su
1e362 62 73 65 71 75 65 6e 74 20 72 65 74 75 72 6e 20 bsequent return
1e363 62 79 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 by the.** sqlite
1e364 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 3_last_insert_ro
1e365 77 69 64 28 29 20 66 75 6e 63 74 69 6f 6e 20 28 wid() function (
1e366 6f 74 68 65 72 77 69 73 65 20 69 74 20 69 73 20 otherwise it is
1e367 75 6e 6d 6f 64 69 66 69 65 64 29 2e 0a 2a 2a 0a unmodified)..**.
1e368 2a 2a 20 49 66 20 74 68 65 20 4f 50 46 4c 41 47 ** If the OPFLAG
1e369 5f 55 53 45 53 45 45 4b 52 45 53 55 4c 54 20 66 _USESEEKRESULT f
1e36a 6c 61 67 20 6f 66 20 50 35 20 69 73 20 73 65 74 lag of P5 is set
1e36b 20 61 6e 64 20 69 66 20 74 68 65 20 72 65 73 75 and if the resu
1e36c 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20 6c 61 73 lt of.** the las
1e36d 74 20 73 65 65 6b 20 6f 70 65 72 61 74 69 6f 6e t seek operation
1e36e 20 28 4f 50 5f 4e 6f 74 45 78 69 73 74 73 29 20 (OP_NotExists)
1e36f 77 61 73 20 61 20 73 75 63 63 65 73 73 2c 20 74 was a success, t
1e370 68 65 6e 20 74 68 69 73 0a 2a 2a 20 6f 70 65 72 hen this.** oper
1e371 61 74 69 6f 6e 20 77 69 6c 6c 20 6e 6f 74 20 61 ation will not a
1e372 74 74 65 6d 70 74 20 74 6f 20 66 69 6e 64 20 74 ttempt to find t
1e373 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 72 he appropriate r
1e374 6f 77 20 62 65 66 6f 72 65 20 64 6f 69 6e 67 0a ow before doing.
1e375 2a 2a 20 74 68 65 20 69 6e 73 65 72 74 20 62 75 ** the insert bu
1e376 74 20 77 69 6c 6c 20 69 6e 73 74 65 61 64 20 6f t will instead o
1e377 76 65 72 77 72 69 74 65 20 74 68 65 20 72 6f 77 verwrite the row
1e378 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 that the cursor
1e379 20 69 73 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 is.** currently
1e37a 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 50 pointing to. P
1e37b 72 65 73 75 6d 61 62 6c 79 2c 20 74 68 65 20 70 resumably, the p
1e37c 72 69 6f 72 20 4f 50 5f 4e 6f 74 45 78 69 73 74 rior OP_NotExist
1e37d 73 20 6f 70 63 6f 64 65 0a 2a 2a 20 68 61 73 20 s opcode.** has
1e37e 61 6c 72 65 61 64 79 20 70 6f 73 69 74 69 6f 6e already position
1e37f 65 64 20 74 68 65 20 63 75 72 73 6f 72 20 63 6f ed the cursor co
1e380 72 72 65 63 74 6c 79 2e 20 20 54 68 69 73 20 69 rrectly. This i
1e381 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f s an optimizatio
1e382 6e 0a 2a 2a 20 74 68 61 74 20 62 6f 6f 73 74 73 n.** that boosts
1e383 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 62 79 20 performance by
1e384 61 76 6f 69 64 69 6e 67 20 72 65 64 75 6e 64 61 avoiding redunda
1e385 6e 74 20 73 65 65 6b 73 2e 0a 2a 2a 0a 2a 2a 20 nt seeks..**.**
1e386 49 66 20 74 68 65 20 4f 50 46 4c 41 47 5f 49 53 If the OPFLAG_IS
1e387 55 50 44 41 54 45 20 66 6c 61 67 20 69 73 20 73 UPDATE flag is s
1e388 65 74 2c 20 74 68 65 6e 20 74 68 69 73 20 6f 70 et, then this op
1e389 63 6f 64 65 20 69 73 20 70 61 72 74 20 6f 66 20 code is part of
1e38a 61 6e 0a 2a 2a 20 55 50 44 41 54 45 20 6f 70 65 an.** UPDATE ope
1e38b 72 61 74 69 6f 6e 2e 20 20 4f 74 68 65 72 77 69 ration. Otherwi
1e38c 73 65 20 28 69 66 20 74 68 65 20 66 6c 61 67 20 se (if the flag
1e38d 69 73 20 63 6c 65 61 72 29 20 74 68 65 6e 20 74 is clear) then t
1e38e 68 69 73 20 6f 70 63 6f 64 65 0a 2a 2a 20 69 73 his opcode.** is
1e38f 20 70 61 72 74 20 6f 66 20 61 6e 20 49 4e 53 45 part of an INSE
1e390 52 54 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 54 RT operation. T
1e391 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 69 73 he difference is
1e392 20 6f 6e 6c 79 20 69 6d 70 6f 72 74 61 6e 74 20 only important
1e393 74 6f 0a 2a 2a 20 74 68 65 20 75 70 64 61 74 65 to.** the update
1e394 20 68 6f 6f 6b 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 hook..**.** Par
1e395 61 6d 65 74 65 72 20 50 34 20 6d 61 79 20 70 6f ameter P4 may po
1e396 69 6e 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20 int to a string
1e397 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 containing the t
1e398 61 62 6c 65 2d 6e 61 6d 65 2c 20 6f 72 0a 2a 2a able-name, or.**
1e399 20 6d 61 79 20 62 65 20 4e 55 4c 4c 2e 20 49 66 may be NULL. If
1e39a 20 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c it is not NULL,
1e39b 20 74 68 65 6e 20 74 68 65 20 75 70 64 61 74 65 then the update
1e39c 2d 68 6f 6f 6b 20 0a 2a 2a 20 28 73 71 6c 69 74 -hook .** (sqlit
1e39d 65 33 2e 78 55 70 64 61 74 65 43 61 6c 6c 62 61 e3.xUpdateCallba
1e39e 63 6b 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 66 ck) is invoked f
1e39f 6f 6c 6c 6f 77 69 6e 67 20 61 20 73 75 63 63 65 ollowing a succe
1e3a0 73 73 66 75 6c 20 69 6e 73 65 72 74 2e 0a 2a 2a ssful insert..**
1e3a1 0a 2a 2a 20 28 57 41 52 4e 49 4e 47 2f 54 4f 44 .** (WARNING/TOD
1e3a2 4f 3a 20 49 66 20 50 31 20 69 73 20 61 20 70 73 O: If P1 is a ps
1e3a3 65 75 64 6f 2d 63 75 72 73 6f 72 20 61 6e 64 20 eudo-cursor and
1e3a4 50 32 20 69 73 20 64 79 6e 61 6d 69 63 61 6c 6c P2 is dynamicall
1e3a5 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20 y.** allocated,
1e3a6 74 68 65 6e 20 6f 77 6e 65 72 73 68 69 70 20 6f then ownership o
1e3a7 66 20 50 32 20 69 73 20 74 72 61 6e 73 66 65 72 f P2 is transfer
1e3a8 72 65 64 20 74 6f 20 74 68 65 20 70 73 65 75 64 red to the pseud
1e3a9 6f 2d 63 75 72 73 6f 72 0a 2a 2a 20 61 6e 64 20 o-cursor.** and
1e3aa 72 65 67 69 73 74 65 72 20 50 32 20 62 65 63 6f register P2 beco
1e3ab 6d 65 73 20 65 70 68 65 6d 65 72 61 6c 2e 20 20 mes ephemeral.
1e3ac 49 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 If the cursor is
1e3ad 20 63 68 61 6e 67 65 64 2c 20 74 68 65 0a 2a 2a changed, the.**
1e3ae 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 value of regist
1e3af 65 72 20 50 32 20 77 69 6c 6c 20 74 68 65 6e 20 er P2 will then
1e3b0 63 68 61 6e 67 65 2e 20 20 4d 61 6b 65 20 73 75 change. Make su
1e3b1 72 65 20 74 68 69 73 20 64 6f 65 73 20 6e 6f 74 re this does not
1e3b2 0a 2a 2a 20 63 61 75 73 65 20 61 6e 79 20 70 72 .** cause any pr
1e3b3 6f 62 6c 65 6d 73 2e 29 0a 2a 2a 0a 2a 2a 20 54 oblems.).**.** T
1e3b4 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 his instruction
1e3b5 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 74 61 only works on ta
1e3b6 62 6c 65 73 2e 20 20 54 68 65 20 65 71 75 69 76 bles. The equiv
1e3b7 61 6c 65 6e 74 20 69 6e 73 74 72 75 63 74 69 6f alent instructio
1e3b8 6e 0a 2a 2a 20 66 6f 72 20 69 6e 64 69 63 65 73 n.** for indices
1e3b9 20 69 73 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 is OP_IdxInsert
1e3ba 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ..*/./* Opcode:
1e3bb 49 6e 73 65 72 74 49 6e 74 20 50 31 20 50 32 20 InsertInt P1 P2
1e3bc 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 P3 P4 P5.**.** T
1e3bd 68 69 73 20 77 6f 72 6b 73 20 65 78 61 63 74 6c his works exactl
1e3be 79 20 6c 69 6b 65 20 4f 50 5f 49 6e 73 65 72 74 y like OP_Insert
1e3bf 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65 except that the
1e3c0 20 6b 65 79 20 69 73 20 74 68 65 0a 2a 2a 20 69 key is the.** i
1e3c1 6e 74 65 67 65 72 20 76 61 6c 75 65 20 50 33 2c nteger value P3,
1e3c2 20 6e 6f 74 20 74 68 65 20 76 61 6c 75 65 20 6f not the value o
1e3c3 66 20 74 68 65 20 69 6e 74 65 67 65 72 20 73 74 f the integer st
1e3c4 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 ored in register
1e3c5 20 50 33 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f P3..*/.case OP_
1e3c6 49 6e 73 65 72 74 3a 20 0a 63 61 73 65 20 4f 50 Insert: .case OP
1e3c7 5f 49 6e 73 65 72 74 49 6e 74 3a 20 7b 0a 23 69 _InsertInt: {.#i
1e3c8 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1e3c9 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1e3ca 74 6f 20 75 2e 62 66 20 2a 2f 0a 20 20 4d 65 6d to u.bf */. Mem
1e3cb 20 2a 70 44 61 74 61 3b 20 20 20 20 20 20 20 2f *pData; /
1e3cc 2a 20 4d 45 4d 20 63 65 6c 6c 20 68 6f 6c 64 69 * MEM cell holdi
1e3cd 6e 67 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 ng data for the
1e3ce 72 65 63 6f 72 64 20 74 6f 20 62 65 20 69 6e 73 record to be ins
1e3cf 65 72 74 65 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a erted */. Mem *
1e3d0 70 4b 65 79 3b 20 20 20 20 20 20 20 20 2f 2a 20 pKey; /*
1e3d1 4d 45 4d 20 63 65 6c 6c 20 68 6f 6c 64 69 6e 67 MEM cell holding
1e3d2 20 6b 65 79 20 20 66 6f 72 20 74 68 65 20 72 65 key for the re
1e3d3 63 6f 72 64 20 2a 2f 0a 20 20 69 36 34 20 69 4b cord */. i64 iK
1e3d4 65 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ey; /* T
1e3d5 68 65 20 69 6e 74 65 67 65 72 20 52 4f 57 49 44 he integer ROWID
1e3d6 20 6f 72 20 6b 65 79 20 66 6f 72 20 74 68 65 20 or key for the
1e3d7 72 65 63 6f 72 64 20 74 6f 20 62 65 20 69 6e 73 record to be ins
1e3d8 65 72 74 65 64 20 2a 2f 0a 20 20 56 64 62 65 43 erted */. VdbeC
1e3d9 75 72 73 6f 72 20 2a 70 43 3b 20 20 20 2f 2a 20 ursor *pC; /*
1e3da 43 75 72 73 6f 72 20 74 6f 20 74 61 62 6c 65 20 Cursor to table
1e3db 69 6e 74 6f 20 77 68 69 63 68 20 69 6e 73 65 72 into which inser
1e3dc 74 20 69 73 20 77 72 69 74 74 65 6e 20 2a 2f 0a t is written */.
1e3dd 20 20 69 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20 int nZero;
1e3de 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1e3df 20 7a 65 72 6f 2d 62 79 74 65 73 20 74 6f 20 61 zero-bytes to a
1e3e0 70 70 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 73 ppend */. int s
1e3e1 65 65 6b 52 65 73 75 6c 74 3b 20 20 20 2f 2a 20 eekResult; /*
1e3e2 52 65 73 75 6c 74 20 6f 66 20 70 72 69 6f 72 20 Result of prior
1e3e3 73 65 65 6b 20 6f 72 20 30 20 69 66 20 6e 6f 20 seek or 0 if no
1e3e4 55 53 45 53 45 45 4b 52 45 53 55 4c 54 20 66 6c USESEEKRESULT fl
1e3e5 61 67 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 ag */. const ch
1e3e6 61 72 20 2a 7a 44 62 3b 20 20 2f 2a 20 64 61 74 ar *zDb; /* dat
1e3e7 61 62 61 73 65 20 6e 61 6d 65 20 2d 20 75 73 65 abase name - use
1e3e8 64 20 62 79 20 74 68 65 20 75 70 64 61 74 65 20 d by the update
1e3e9 68 6f 6f 6b 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 hook */. const
1e3ea 63 68 61 72 20 2a 7a 54 62 6c 3b 20 2f 2a 20 54 char *zTbl; /* T
1e3eb 61 62 6c 65 20 6e 61 6d 65 20 2d 20 75 73 65 64 able name - used
1e3ec 20 62 79 20 74 68 65 20 6f 70 64 61 74 65 20 68 by the opdate h
1e3ed 6f 6f 6b 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 3b ook */. int op;
1e3ee 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 /* Op
1e3ef 63 6f 64 65 20 66 6f 72 20 75 70 64 61 74 65 20 code for update
1e3f0 68 6f 6f 6b 3a 20 53 51 4c 49 54 45 5f 55 50 44 hook: SQLITE_UPD
1e3f1 41 54 45 20 6f 72 20 53 51 4c 49 54 45 5f 49 4e ATE or SQLITE_IN
1e3f2 53 45 52 54 20 2a 2f 0a 23 65 6e 64 69 66 20 2f SERT */.#endif /
1e3f3 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1e3f4 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 s moved into u.b
1e3f5 66 20 2a 2f 0a 0a 20 20 75 2e 62 66 2e 70 44 61 f */.. u.bf.pDa
1e3f6 74 61 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f ta = &p->aMem[pO
1e3f7 70 2d 3e 70 32 5d 3b 0a 20 20 61 73 73 65 72 74 p->p2];. assert
1e3f8 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 ( pOp->p1>=0 &&
1e3f9 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 pOp->p1<p->nCurs
1e3fa 6f 72 20 29 3b 0a 20 20 75 2e 62 66 2e 70 43 20 or );. u.bf.pC
1e3fb 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e = p->apCsr[pOp->
1e3fc 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 p1];. assert( u
1e3fd 2e 62 66 2e 70 43 21 3d 30 20 29 3b 0a 20 20 61 .bf.pC!=0 );. a
1e3fe 73 73 65 72 74 28 20 75 2e 62 66 2e 70 43 2d 3e ssert( u.bf.pC->
1e3ff 70 43 75 72 73 6f 72 21 3d 30 20 29 3b 0a 20 20 pCursor!=0 );.
1e400 61 73 73 65 72 74 28 20 75 2e 62 66 2e 70 43 2d assert( u.bf.pC-
1e401 3e 70 73 65 75 64 6f 54 61 62 6c 65 52 65 67 3d >pseudoTableReg=
1e402 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
1e403 75 2e 62 66 2e 70 43 2d 3e 69 73 54 61 62 6c 65 u.bf.pC->isTable
1e404 20 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 );. REGISTER_T
1e405 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 75 2e RACE(pOp->p2, u.
1e406 62 66 2e 70 44 61 74 61 29 3b 0a 0a 20 20 69 66 bf.pData);.. if
1e407 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f ( pOp->opcode==O
1e408 50 5f 49 6e 73 65 72 74 20 29 7b 0a 20 20 20 20 P_Insert ){.
1e409 75 2e 62 66 2e 70 4b 65 79 20 3d 20 26 70 2d 3e u.bf.pKey = &p->
1e40a 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 aMem[pOp->p3];.
1e40b 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 66 2e assert( u.bf.
1e40c 70 4b 65 79 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pKey->flags & ME
1e40d 4d 5f 49 6e 74 20 29 3b 0a 20 20 20 20 52 45 47 M_Int );. REG
1e40e 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d ISTER_TRACE(pOp-
1e40f 3e 70 33 2c 20 75 2e 62 66 2e 70 4b 65 79 29 3b >p3, u.bf.pKey);
1e410 0a 20 20 20 20 75 2e 62 66 2e 69 4b 65 79 20 3d . u.bf.iKey =
1e411 20 75 2e 62 66 2e 70 4b 65 79 2d 3e 75 2e 69 3b u.bf.pKey->u.i;
1e412 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 . }else{. as
1e413 73 65 72 74 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 sert( pOp->opcod
1e414 65 3d 3d 4f 50 5f 49 6e 73 65 72 74 49 6e 74 20 e==OP_InsertInt
1e415 29 3b 0a 20 20 20 20 75 2e 62 66 2e 69 4b 65 79 );. u.bf.iKey
1e416 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 7d 0a = pOp->p3;. }.
1e417 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 . if( pOp->p5 &
1e418 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 OPFLAG_NCHANGE
1e419 29 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0a ) p->nChange++;.
1e41a 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 if( pOp->p5 &
1e41b 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49 44 OPFLAG_LASTROWID
1e41c 20 29 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 ) db->lastRowid
1e41d 20 3d 20 75 2e 62 66 2e 69 4b 65 79 3b 0a 20 20 = u.bf.iKey;.
1e41e 69 66 28 20 75 2e 62 66 2e 70 44 61 74 61 2d 3e if( u.bf.pData->
1e41f 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c flags & MEM_Null
1e420 20 29 7b 0a 20 20 20 20 75 2e 62 66 2e 70 44 61 ){. u.bf.pDa
1e421 74 61 2d 3e 7a 20 3d 20 30 3b 0a 20 20 20 20 75 ta->z = 0;. u
1e422 2e 62 66 2e 70 44 61 74 61 2d 3e 6e 20 3d 20 30 .bf.pData->n = 0
1e423 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 ;. }else{. a
1e424 73 73 65 72 74 28 20 75 2e 62 66 2e 70 44 61 74 ssert( u.bf.pDat
1e425 61 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f a->flags & (MEM_
1e426 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 20 29 3b Blob|MEM_Str) );
1e427 0a 20 20 7d 0a 20 20 75 2e 62 66 2e 73 65 65 6b . }. u.bf.seek
1e428 52 65 73 75 6c 74 20 3d 20 28 28 70 4f 70 2d 3e Result = ((pOp->
1e429 70 35 20 26 20 4f 50 46 4c 41 47 5f 55 53 45 53 p5 & OPFLAG_USES
1e42a 45 45 4b 52 45 53 55 4c 54 29 20 3f 20 75 2e 62 EEKRESULT) ? u.b
1e42b 66 2e 70 43 2d 3e 73 65 65 6b 52 65 73 75 6c 74 f.pC->seekResult
1e42c 20 3a 20 30 29 3b 0a 20 20 69 66 28 20 75 2e 62 : 0);. if( u.b
1e42d 66 2e 70 44 61 74 61 2d 3e 66 6c 61 67 73 20 26 f.pData->flags &
1e42e 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 MEM_Zero ){.
1e42f 20 75 2e 62 66 2e 6e 5a 65 72 6f 20 3d 20 75 2e u.bf.nZero = u.
1e430 62 66 2e 70 44 61 74 61 2d 3e 75 2e 6e 5a 65 72 bf.pData->u.nZer
1e431 6f 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 o;. }else{.
1e432 75 2e 62 66 2e 6e 5a 65 72 6f 20 3d 20 30 3b 0a u.bf.nZero = 0;.
1e433 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 }. sqlite3Btr
1e434 65 65 53 65 74 43 61 63 68 65 64 52 6f 77 69 64 eeSetCachedRowid
1e435 28 75 2e 62 66 2e 70 43 2d 3e 70 43 75 72 73 6f (u.bf.pC->pCurso
1e436 72 2c 20 30 29 3b 0a 20 20 72 63 20 3d 20 73 71 r, 0);. rc = sq
1e437 6c 69 74 65 33 42 74 72 65 65 49 6e 73 65 72 74 lite3BtreeInsert
1e438 28 75 2e 62 66 2e 70 43 2d 3e 70 43 75 72 73 6f (u.bf.pC->pCurso
1e439 72 2c 20 30 2c 20 75 2e 62 66 2e 69 4b 65 79 2c r, 0, u.bf.iKey,
1e43a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1e43b 20 20 20 20 20 20 20 20 20 20 20 75 2e 62 66 2e u.bf.
1e43c 70 44 61 74 61 2d 3e 7a 2c 20 75 2e 62 66 2e 70 pData->z, u.bf.p
1e43d 44 61 74 61 2d 3e 6e 2c 20 75 2e 62 66 2e 6e 5a Data->n, u.bf.nZ
1e43e 65 72 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ero,.
1e43f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
1e440 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f Op->p5 & OPFLAG_
1e441 41 50 50 45 4e 44 2c 20 75 2e 62 66 2e 73 65 65 APPEND, u.bf.see
1e442 6b 52 65 73 75 6c 74 0a 20 20 29 3b 0a 20 20 75 kResult. );. u
1e443 2e 62 66 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 .bf.pC->rowidIsV
1e444 61 6c 69 64 20 3d 20 30 3b 0a 20 20 75 2e 62 66 alid = 0;. u.bf
1e445 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 .pC->deferredMov
1e446 65 74 6f 20 3d 20 30 3b 0a 20 20 75 2e 62 66 2e eto = 0;. u.bf.
1e447 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 pC->cacheStatus
1e448 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 0a = CACHE_STALE;..
1e449 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 /* Invoke the
1e44a 75 70 64 61 74 65 2d 68 6f 6f 6b 20 69 66 20 72 update-hook if r
1e44b 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69 66 equired. */. if
1e44c 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1e44d 26 26 20 64 62 2d 3e 78 55 70 64 61 74 65 43 61 && db->xUpdateCa
1e44e 6c 6c 62 61 63 6b 20 26 26 20 70 4f 70 2d 3e 70 llback && pOp->p
1e44f 34 2e 7a 20 29 7b 0a 20 20 20 20 75 2e 62 66 2e 4.z ){. u.bf.
1e450 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 75 2e zDb = db->aDb[u.
1e451 62 66 2e 70 43 2d 3e 69 44 62 5d 2e 7a 4e 61 6d bf.pC->iDb].zNam
1e452 65 3b 0a 20 20 20 20 75 2e 62 66 2e 7a 54 62 6c e;. u.bf.zTbl
1e453 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 = pOp->p4.z;.
1e454 20 20 75 2e 62 66 2e 6f 70 20 3d 20 28 28 70 4f u.bf.op = ((pO
1e455 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f 49 p->p5 & OPFLAG_I
1e456 53 55 50 44 41 54 45 29 20 3f 20 53 51 4c 49 54 SUPDATE) ? SQLIT
1e457 45 5f 55 50 44 41 54 45 20 3a 20 53 51 4c 49 54 E_UPDATE : SQLIT
1e458 45 5f 49 4e 53 45 52 54 29 3b 0a 20 20 20 20 61 E_INSERT);. a
1e459 73 73 65 72 74 28 20 75 2e 62 66 2e 70 43 2d 3e ssert( u.bf.pC->
1e45a 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 64 isTable );. d
1e45b 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c 62 61 b->xUpdateCallba
1e45c 63 6b 28 64 62 2d 3e 70 55 70 64 61 74 65 41 72 ck(db->pUpdateAr
1e45d 67 2c 20 75 2e 62 66 2e 6f 70 2c 20 75 2e 62 66 g, u.bf.op, u.bf
1e45e 2e 7a 44 62 2c 20 75 2e 62 66 2e 7a 54 62 6c 2c .zDb, u.bf.zTbl,
1e45f 20 75 2e 62 66 2e 69 4b 65 79 29 3b 0a 20 20 20 u.bf.iKey);.
1e460 20 61 73 73 65 72 74 28 20 75 2e 62 66 2e 70 43 assert( u.bf.pC
1e461 2d 3e 69 44 62 3e 3d 30 20 29 3b 0a 20 20 7d 0a ->iDb>=0 );. }.
1e462 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1e463 70 63 6f 64 65 3a 20 44 65 6c 65 74 65 20 50 31 pcode: Delete P1
1e464 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a P2 * P4 *.**.**
1e465 20 44 65 6c 65 74 65 20 74 68 65 20 72 65 63 6f Delete the reco
1e466 72 64 20 61 74 20 77 68 69 63 68 20 74 68 65 20 rd at which the
1e467 50 31 20 63 75 72 73 6f 72 20 69 73 20 63 75 72 P1 cursor is cur
1e468 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 2e rently pointing.
1e469 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f .**.** The curso
1e46a 72 20 77 69 6c 6c 20 62 65 20 6c 65 66 74 20 70 r will be left p
1e46b 6f 69 6e 74 69 6e 67 20 61 74 20 65 69 74 68 65 ointing at eithe
1e46c 72 20 74 68 65 20 6e 65 78 74 20 6f 72 20 74 68 r the next or th
1e46d 65 20 70 72 65 76 69 6f 75 73 0a 2a 2a 20 72 65 e previous.** re
1e46e 63 6f 72 64 20 69 6e 20 74 68 65 20 74 61 62 6c cord in the tabl
1e46f 65 2e 20 49 66 20 69 74 20 69 73 20 6c 65 66 74 e. If it is left
1e470 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 74 68 65 pointing at the
1e471 20 6e 65 78 74 20 72 65 63 6f 72 64 2c 20 74 68 next record, th
1e472 65 6e 0a 2a 2a 20 74 68 65 20 6e 65 78 74 20 4e en.** the next N
1e473 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 ext instruction
1e474 77 69 6c 6c 20 62 65 20 61 20 6e 6f 2d 6f 70 2e will be a no-op.
1e475 20 20 48 65 6e 63 65 20 69 74 20 69 73 20 4f 4b Hence it is OK
1e476 20 74 6f 20 64 65 6c 65 74 65 0a 2a 2a 20 61 20 to delete.** a
1e477 72 65 63 6f 72 64 20 66 72 6f 6d 20 77 69 74 68 record from with
1e478 69 6e 20 61 6e 20 4e 65 78 74 20 6c 6f 6f 70 2e in an Next loop.
1e479 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f 50 .**.** If the OP
1e47a 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 66 6c 61 FLAG_NCHANGE fla
1e47b 67 20 6f 66 20 50 32 20 69 73 20 73 65 74 2c 20 g of P2 is set,
1e47c 74 68 65 6e 20 74 68 65 20 72 6f 77 20 63 68 61 then the row cha
1e47d 6e 67 65 20 63 6f 75 6e 74 20 69 73 0a 2a 2a 20 nge count is.**
1e47e 69 6e 63 72 65 6d 65 6e 74 65 64 20 28 6f 74 68 incremented (oth
1e47f 65 72 77 69 73 65 20 6e 6f 74 29 2e 0a 2a 2a 0a erwise not)..**.
1e480 2a 2a 20 50 31 20 6d 75 73 74 20 6e 6f 74 20 62 ** P1 must not b
1e481 65 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 20 e pseudo-table.
1e482 20 49 74 20 68 61 73 20 74 6f 20 62 65 20 61 20 It has to be a
1e483 72 65 61 6c 20 74 61 62 6c 65 20 77 69 74 68 0a real table with.
1e484 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 72 6f 77 73 ** multiple rows
1e485 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 34 20 69 73 ..**.** If P4 is
1e486 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 not NULL, then
1e487 69 74 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f it is the name o
1e488 66 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 f the table that
1e489 20 50 31 20 69 73 0a 2a 2a 20 70 6f 69 6e 74 69 P1 is.** pointi
1e48a 6e 67 20 74 6f 2e 20 20 54 68 65 20 75 70 64 61 ng to. The upda
1e48b 74 65 20 68 6f 6f 6b 20 77 69 6c 6c 20 62 65 20 te hook will be
1e48c 69 6e 76 6f 6b 65 64 2c 20 69 66 20 69 74 20 65 invoked, if it e
1e48d 78 69 73 74 73 2e 0a 2a 2a 20 49 66 20 50 34 20 xists..** If P4
1e48e 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 74 68 65 6e is not NULL then
1e48f 20 74 68 65 20 50 31 20 63 75 72 73 6f 72 20 6d the P1 cursor m
1e490 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 70 6f ust have been po
1e491 73 69 74 69 6f 6e 65 64 0a 2a 2a 20 75 73 69 6e sitioned.** usin
1e492 67 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 20 70 72 g OP_NotFound pr
1e493 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 ior to invoking
1e494 74 68 69 73 20 6f 70 63 6f 64 65 2e 0a 2a 2f 0a this opcode..*/.
1e495 63 61 73 65 20 4f 50 5f 44 65 6c 65 74 65 3a 20 case OP_Delete:
1e496 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 {.#if 0 /* loca
1e497 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1e498 64 20 69 6e 74 6f 20 75 2e 62 67 20 2a 2f 0a 20 d into u.bg */.
1e499 20 69 36 34 20 69 4b 65 79 3b 0a 20 20 56 64 62 i64 iKey;. Vdb
1e49a 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 23 65 6e eCursor *pC;.#en
1e49b 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 dif /* local var
1e49c 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1e49d 6f 20 75 2e 62 67 20 2a 2f 0a 0a 20 20 75 2e 62 o u.bg */.. u.b
1e49e 67 2e 69 4b 65 79 20 3d 20 30 3b 0a 20 20 61 73 g.iKey = 0;. as
1e49f 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 sert( pOp->p1>=0
1e4a0 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e && pOp->p1<p->n
1e4a1 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 67 Cursor );. u.bg
1e4a2 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 .pC = p->apCsr[p
1e4a3 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 Op->p1];. asser
1e4a4 74 28 20 75 2e 62 67 2e 70 43 21 3d 30 20 29 3b t( u.bg.pC!=0 );
1e4a5 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 67 2e . assert( u.bg.
1e4a6 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 pC->pCursor!=0 )
1e4a7 3b 20 20 2f 2a 20 4f 6e 6c 79 20 76 61 6c 69 64 ; /* Only valid
1e4a8 20 66 6f 72 20 72 65 61 6c 20 74 61 62 6c 65 73 for real tables
1e4a9 2c 20 6e 6f 20 70 73 65 75 64 6f 74 61 62 6c 65 , no pseudotable
1e4aa 73 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 s */.. /* If th
1e4ab 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20 77 69 e update-hook wi
1e4ac 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 2c 20 73 ll be invoked, s
1e4ad 65 74 20 75 2e 62 67 2e 69 4b 65 79 20 74 6f 20 et u.bg.iKey to
1e4ae 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 the rowid of the
1e4af 0a 20 20 2a 2a 20 72 6f 77 20 62 65 69 6e 67 20 . ** row being
1e4b0 64 65 6c 65 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 deleted.. */.
1e4b1 69 66 28 20 64 62 2d 3e 78 55 70 64 61 74 65 43 if( db->xUpdateC
1e4b2 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f 70 2d 3e allback && pOp->
1e4b3 70 34 2e 7a 20 29 7b 0a 20 20 20 20 61 73 73 65 p4.z ){. asse
1e4b4 72 74 28 20 75 2e 62 67 2e 70 43 2d 3e 69 73 54 rt( u.bg.pC->isT
1e4b5 61 62 6c 65 20 29 3b 0a 20 20 20 20 61 73 73 65 able );. asse
1e4b6 72 74 28 20 75 2e 62 67 2e 70 43 2d 3e 72 6f 77 rt( u.bg.pC->row
1e4b7 69 64 49 73 56 61 6c 69 64 20 29 3b 20 20 2f 2a idIsValid ); /*
1e4b8 20 6c 61 73 74 52 6f 77 69 64 20 73 65 74 20 62 lastRowid set b
1e4b9 79 20 70 72 65 76 69 6f 75 73 20 4f 50 5f 4e 6f y previous OP_No
1e4ba 74 46 6f 75 6e 64 20 2a 2f 0a 20 20 20 20 75 2e tFound */. u.
1e4bb 62 67 2e 69 4b 65 79 20 3d 20 75 2e 62 67 2e 70 bg.iKey = u.bg.p
1e4bc 43 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20 C->lastRowid;.
1e4bd 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 4f 50 5f 44 }.. /* The OP_D
1e4be 65 6c 65 74 65 20 6f 70 63 6f 64 65 20 61 6c 77 elete opcode alw
1e4bf 61 79 73 20 66 6f 6c 6c 6f 77 73 20 61 6e 20 4f ays follows an O
1e4c0 50 5f 4e 6f 74 45 78 69 73 74 73 20 6f 72 20 4f P_NotExists or O
1e4c1 50 5f 4c 61 73 74 20 6f 72 0a 20 20 2a 2a 20 4f P_Last or. ** O
1e4c2 50 5f 43 6f 6c 75 6d 6e 20 6f 6e 20 74 68 65 20 P_Column on the
1e4c3 73 61 6d 65 20 74 61 62 6c 65 20 77 69 74 68 6f same table witho
1e4c4 75 74 20 61 6e 79 20 69 6e 74 65 72 76 65 6e 69 ut any interveni
1e4c5 6e 67 20 6f 70 65 72 61 74 69 6f 6e 73 20 74 68 ng operations th
1e4c6 61 74 0a 20 20 2a 2a 20 6d 69 67 68 74 20 6d 6f at. ** might mo
1e4c7 76 65 20 6f 72 20 69 6e 76 61 6c 69 64 61 74 65 ve or invalidate
1e4c8 20 74 68 65 20 63 75 72 73 6f 72 2e 20 20 48 65 the cursor. He
1e4c9 6e 63 65 20 63 75 72 73 6f 72 20 75 2e 62 67 2e nce cursor u.bg.
1e4ca 70 43 20 69 73 20 61 6c 77 61 79 73 20 70 6f 69 pC is always poi
1e4cb 6e 74 69 6e 67 0a 20 20 2a 2a 20 74 6f 20 74 68 nting. ** to th
1e4cc 65 20 72 6f 77 20 74 6f 20 62 65 20 64 65 6c 65 e row to be dele
1e4cd 74 65 64 20 61 6e 64 20 74 68 65 20 73 71 6c 69 ted and the sqli
1e4ce 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 te3VdbeCursorMov
1e4cf 65 74 6f 28 29 20 6f 70 65 72 61 74 69 6f 6e 0a eto() operation.
1e4d0 20 20 2a 2a 20 62 65 6c 6f 77 20 69 73 20 61 6c ** below is al
1e4d1 77 61 79 73 20 61 20 6e 6f 2d 6f 70 20 61 6e 64 ways a no-op and
1e4d2 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 20 57 cannot fail. W
1e4d3 65 20 77 69 6c 6c 20 72 75 6e 20 69 74 20 61 6e e will run it an
1e4d4 79 68 6f 77 2c 20 74 68 6f 75 67 68 2c 0a 20 20 yhow, though,.
1e4d5 2a 2a 20 74 6f 20 67 75 61 72 64 20 61 67 61 69 ** to guard agai
1e4d6 6e 73 74 20 66 75 74 75 72 65 20 63 68 61 6e 67 nst future chang
1e4d7 65 73 20 74 6f 20 74 68 65 20 63 6f 64 65 20 67 es to the code g
1e4d8 65 6e 65 72 61 74 6f 72 2e 0a 20 20 2a 2a 2f 0a enerator.. **/.
1e4d9 20 20 61 73 73 65 72 74 28 20 75 2e 62 67 2e 70 assert( u.bg.p
1e4da 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 C->deferredMovet
1e4db 6f 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 o==0 );. rc = s
1e4dc 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 qlite3VdbeCursor
1e4dd 4d 6f 76 65 74 6f 28 75 2e 62 67 2e 70 43 29 3b Moveto(u.bg.pC);
1e4de 0a 20 20 69 66 28 20 4e 45 56 45 52 28 72 63 21 . if( NEVER(rc!
1e4df 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 20 67 6f =SQLITE_OK) ) go
1e4e0 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1e4e1 65 72 72 6f 72 3b 0a 0a 20 20 73 71 6c 69 74 65 error;.. sqlite
1e4e2 33 42 74 72 65 65 53 65 74 43 61 63 68 65 64 52 3BtreeSetCachedR
1e4e3 6f 77 69 64 28 75 2e 62 67 2e 70 43 2d 3e 70 43 owid(u.bg.pC->pC
1e4e4 75 72 73 6f 72 2c 20 30 29 3b 0a 20 20 72 63 20 ursor, 0);. rc
1e4e5 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 65 = sqlite3BtreeDe
1e4e6 6c 65 74 65 28 75 2e 62 67 2e 70 43 2d 3e 70 43 lete(u.bg.pC->pC
1e4e7 75 72 73 6f 72 29 3b 0a 20 20 75 2e 62 67 2e 70 ursor);. u.bg.p
1e4e8 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d C->cacheStatus =
1e4e9 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 0a 20 CACHE_STALE;..
1e4ea 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 75 /* Invoke the u
1e4eb 70 64 61 74 65 2d 68 6f 6f 6b 20 69 66 20 72 65 pdate-hook if re
1e4ec 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69 66 28 quired. */. if(
1e4ed 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
1e4ee 26 20 64 62 2d 3e 78 55 70 64 61 74 65 43 61 6c & db->xUpdateCal
1e4ef 6c 62 61 63 6b 20 26 26 20 70 4f 70 2d 3e 70 34 lback && pOp->p4
1e4f0 2e 7a 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 .z ){. const
1e4f1 63 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e char *zDb = db->
1e4f2 61 44 62 5b 75 2e 62 67 2e 70 43 2d 3e 69 44 62 aDb[u.bg.pC->iDb
1e4f3 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 63 6f 6e ].zName;. con
1e4f4 73 74 20 63 68 61 72 20 2a 7a 54 62 6c 20 3d 20 st char *zTbl =
1e4f5 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 64 pOp->p4.z;. d
1e4f6 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c 62 61 b->xUpdateCallba
1e4f7 63 6b 28 64 62 2d 3e 70 55 70 64 61 74 65 41 72 ck(db->pUpdateAr
1e4f8 67 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 g, SQLITE_DELETE
1e4f9 2c 20 7a 44 62 2c 20 7a 54 62 6c 2c 20 75 2e 62 , zDb, zTbl, u.b
1e4fa 67 2e 69 4b 65 79 29 3b 0a 20 20 20 20 61 73 73 g.iKey);. ass
1e4fb 65 72 74 28 20 75 2e 62 67 2e 70 43 2d 3e 69 44 ert( u.bg.pC->iD
1e4fc 62 3e 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 69 66 b>=0 );. }. if
1e4fd 28 20 70 4f 70 2d 3e 70 32 20 26 20 4f 50 46 4c ( pOp->p2 & OPFL
1e4fe 41 47 5f 4e 43 48 41 4e 47 45 20 29 20 70 2d 3e AG_NCHANGE ) p->
1e4ff 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20 20 62 72 65 nChange++;. bre
1e500 61 6b 3b 0a 7d 0a 2f 2a 20 4f 70 63 6f 64 65 3a ak;.}./* Opcode:
1e501 20 52 65 73 65 74 43 6f 75 6e 74 20 2a 20 2a 20 ResetCount * *
1e502 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 * * *.**.** The
1e503 76 61 6c 75 65 20 6f 66 20 74 68 65 20 63 68 61 value of the cha
1e504 6e 67 65 20 63 6f 75 6e 74 65 72 20 69 73 20 63 nge counter is c
1e505 6f 70 69 65 64 20 74 6f 20 74 68 65 20 64 61 74 opied to the dat
1e506 61 62 61 73 65 20 68 61 6e 64 6c 65 0a 2a 2a 20 abase handle.**
1e507 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 28 change counter (
1e508 72 65 74 75 72 6e 65 64 20 62 79 20 73 75 62 73 returned by subs
1e509 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 equent calls to
1e50a 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 sqlite3_changes(
1e50b 29 29 2e 0a 2a 2a 20 54 68 65 6e 20 74 68 65 20 ))..** Then the
1e50c 56 4d 73 20 69 6e 74 65 72 6e 61 6c 20 63 68 61 VMs internal cha
1e50d 6e 67 65 20 63 6f 75 6e 74 65 72 20 72 65 73 65 nge counter rese
1e50e 74 73 20 74 6f 20 30 2e 0a 2a 2a 20 54 68 69 73 ts to 0..** This
1e50f 20 69 73 20 75 73 65 64 20 62 79 20 74 72 69 67 is used by trig
1e510 67 65 72 20 70 72 6f 67 72 61 6d 73 2e 0a 2a 2f ger programs..*/
1e511 0a 63 61 73 65 20 4f 50 5f 52 65 73 65 74 43 6f .case OP_ResetCo
1e512 75 6e 74 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33 unt: {. sqlite3
1e513 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 64 VdbeSetChanges(d
1e514 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 65 29 3b 0a b, p->nChange);.
1e515 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 30 p->nChange = 0
1e516 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a ;. break;.}../*
1e517 20 4f 70 63 6f 64 65 3a 20 52 6f 77 44 61 74 61 Opcode: RowData
1e518 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a P1 P2 * * *.**.
1e519 2a 2a 20 57 72 69 74 65 20 69 6e 74 6f 20 72 65 ** Write into re
1e51a 67 69 73 74 65 72 20 50 32 20 74 68 65 20 63 6f gister P2 the co
1e51b 6d 70 6c 65 74 65 20 72 6f 77 20 64 61 74 61 20 mplete row data
1e51c 66 6f 72 20 63 75 72 73 6f 72 20 50 31 2e 0a 2a for cursor P1..*
1e51d 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20 69 6e * There is no in
1e51e 74 65 72 70 72 65 74 61 74 69 6f 6e 20 6f 66 20 terpretation of
1e51f 74 68 65 20 64 61 74 61 2e 20 20 0a 2a 2a 20 49 the data. .** I
1e520 74 20 69 73 20 6a 75 73 74 20 63 6f 70 69 65 64 t is just copied
1e521 20 6f 6e 74 6f 20 74 68 65 20 50 32 20 72 65 67 onto the P2 reg
1e522 69 73 74 65 72 20 65 78 61 63 74 6c 79 20 61 73 ister exactly as
1e523 20 0a 2a 2a 20 69 74 20 69 73 20 66 6f 75 6e 64 .** it is found
1e524 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
1e525 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 file..**.** If
1e526 74 68 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75 the P1 cursor mu
1e527 73 74 20 62 65 20 70 6f 69 6e 74 69 6e 67 20 74 st be pointing t
1e528 6f 20 61 20 76 61 6c 69 64 20 72 6f 77 20 28 6e o a valid row (n
1e529 6f 74 20 61 20 4e 55 4c 4c 20 72 6f 77 29 0a 2a ot a NULL row).*
1e52a 2a 20 6f 66 20 61 20 72 65 61 6c 20 74 61 62 6c * of a real tabl
1e52b 65 2c 20 6e 6f 74 20 61 20 70 73 65 75 64 6f 2d e, not a pseudo-
1e52c 74 61 62 6c 65 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 table..*/./* Opc
1e52d 6f 64 65 3a 20 52 6f 77 4b 65 79 20 50 31 20 50 ode: RowKey P1 P
1e52e 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 2 * * *.**.** Wr
1e52f 69 74 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 ite into registe
1e530 72 20 50 32 20 74 68 65 20 63 6f 6d 70 6c 65 74 r P2 the complet
1e531 65 20 72 6f 77 20 6b 65 79 20 66 6f 72 20 63 75 e row key for cu
1e532 72 73 6f 72 20 50 31 2e 0a 2a 2a 20 54 68 65 72 rsor P1..** Ther
1e533 65 20 69 73 20 6e 6f 20 69 6e 74 65 72 70 72 65 e is no interpre
1e534 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 61 tation of the da
1e535 74 61 2e 20 20 0a 2a 2a 20 54 68 65 20 6b 65 79 ta. .** The key
1e536 20 69 73 20 63 6f 70 69 65 64 20 6f 6e 74 6f 20 is copied onto
1e537 74 68 65 20 50 33 20 72 65 67 69 73 74 65 72 20 the P3 register
1e538 65 78 61 63 74 6c 79 20 61 73 20 0a 2a 2a 20 69 exactly as .** i
1e539 74 20 69 73 20 66 6f 75 6e 64 20 69 6e 20 74 68 t is found in th
1e53a 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
1e53b 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 31 .**.** If the P1
1e53c 20 63 75 72 73 6f 72 20 6d 75 73 74 20 62 65 20 cursor must be
1e53d 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 76 61 pointing to a va
1e53e 6c 69 64 20 72 6f 77 20 28 6e 6f 74 20 61 20 4e lid row (not a N
1e53f 55 4c 4c 20 72 6f 77 29 0a 2a 2a 20 6f 66 20 61 ULL row).** of a
1e540 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 6e 6f 74 real table, not
1e541 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e a pseudo-table.
1e542 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f 77 4b .*/.case OP_RowK
1e543 65 79 3a 0a 63 61 73 65 20 4f 50 5f 52 6f 77 44 ey:.case OP_RowD
1e544 61 74 61 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a ata: {.#if 0 /*
1e545 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1e546 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 68 moved into u.bh
1e547 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 */. VdbeCursor
1e548 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 *pC;. BtCursor
1e549 20 2a 70 43 72 73 72 3b 0a 20 20 75 33 32 20 6e *pCrsr;. u32 n
1e54a 3b 0a 20 20 69 36 34 20 6e 36 34 3b 0a 23 65 6e ;. i64 n64;.#en
1e54b 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 dif /* local var
1e54c 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1e54d 6f 20 75 2e 62 68 20 2a 2f 0a 0a 20 20 70 4f 75 o u.bh */.. pOu
1e54e 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 t = &p->aMem[pOp
1e54f 2d 3e 70 32 5d 3b 0a 0a 20 20 2f 2a 20 4e 6f 74 ->p2];.. /* Not
1e550 65 20 74 68 61 74 20 52 6f 77 4b 65 79 20 61 6e e that RowKey an
1e551 64 20 52 6f 77 44 61 74 61 20 61 72 65 20 72 65 d RowData are re
1e552 61 6c 6c 79 20 65 78 61 63 74 6c 79 20 74 68 65 ally exactly the
1e553 20 73 61 6d 65 20 69 6e 73 74 72 75 63 74 69 6f same instructio
1e554 6e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 n */. assert( p
1e555 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 Op->p1>=0 && pOp
1e556 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 ->p1<p->nCursor
1e557 29 3b 0a 20 20 75 2e 62 68 2e 70 43 20 3d 20 70 );. u.bh.pC = p
1e558 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d ->apCsr[pOp->p1]
1e559 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 68 ;. assert( u.bh
1e55a 2e 70 43 2d 3e 69 73 54 61 62 6c 65 20 7c 7c 20 .pC->isTable ||
1e55b 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f pOp->opcode==OP_
1e55c 52 6f 77 4b 65 79 20 29 3b 0a 20 20 61 73 73 65 RowKey );. asse
1e55d 72 74 28 20 75 2e 62 68 2e 70 43 2d 3e 69 73 49 rt( u.bh.pC->isI
1e55e 6e 64 65 78 20 7c 7c 20 70 4f 70 2d 3e 6f 70 63 ndex || pOp->opc
1e55f 6f 64 65 3d 3d 4f 50 5f 52 6f 77 44 61 74 61 20 ode==OP_RowData
1e560 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 );. assert( u.b
1e561 68 2e 70 43 21 3d 30 20 29 3b 0a 20 20 61 73 73 h.pC!=0 );. ass
1e562 65 72 74 28 20 75 2e 62 68 2e 70 43 2d 3e 6e 75 ert( u.bh.pC->nu
1e563 6c 6c 52 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73 llRow==0 );. as
1e564 73 65 72 74 28 20 75 2e 62 68 2e 70 43 2d 3e 70 sert( u.bh.pC->p
1e565 73 65 75 64 6f 54 61 62 6c 65 52 65 67 3d 3d 30 seudoTableReg==0
1e566 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e );. assert( u.
1e567 62 68 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d bh.pC->pCursor!=
1e568 30 20 29 3b 0a 20 20 75 2e 62 68 2e 70 43 72 73 0 );. u.bh.pCrs
1e569 72 20 3d 20 75 2e 62 68 2e 70 43 2d 3e 70 43 75 r = u.bh.pC->pCu
1e56a 72 73 6f 72 3b 0a 20 20 61 73 73 65 72 74 28 20 rsor;. assert(
1e56b 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 sqlite3BtreeCurs
1e56c 6f 72 49 73 56 61 6c 69 64 28 75 2e 62 68 2e 70 orIsValid(u.bh.p
1e56d 43 72 73 72 29 20 29 3b 0a 0a 20 20 2f 2a 20 54 Crsr) );.. /* T
1e56e 68 65 20 4f 50 5f 52 6f 77 4b 65 79 20 61 6e 64 he OP_RowKey and
1e56f 20 4f 50 5f 52 6f 77 44 61 74 61 20 6f 70 63 6f OP_RowData opco
1e570 64 65 73 20 61 6c 77 61 79 73 20 66 6f 6c 6c 6f des always follo
1e571 77 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20 6f w OP_NotExists o
1e572 72 0a 20 20 2a 2a 20 4f 50 5f 52 65 77 69 6e 64 r. ** OP_Rewind
1e573 2f 4f 70 5f 4e 65 78 74 20 77 69 74 68 20 6e 6f /Op_Next with no
1e574 20 69 6e 74 65 72 76 65 6e 69 6e 67 20 69 6e 73 intervening ins
1e575 74 72 75 63 74 69 6f 6e 73 20 74 68 61 74 20 6d tructions that m
1e576 69 67 68 74 20 69 6e 76 61 6c 69 64 61 74 65 0a ight invalidate.
1e577 20 20 2a 2a 20 74 68 65 20 63 75 72 73 6f 72 2e ** the cursor.
1e578 20 20 48 65 6e 63 65 20 74 68 65 20 66 6f 6c 6c Hence the foll
1e579 6f 77 69 6e 67 20 73 71 6c 69 74 65 33 56 64 62 owing sqlite3Vdb
1e57a 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 29 20 eCursorMoveto()
1e57b 63 61 6c 6c 20 69 73 20 61 6c 77 61 79 73 0a 20 call is always.
1e57c 20 2a 2a 20 61 20 6e 6f 2d 6f 70 20 61 6e 64 20 ** a no-op and
1e57d 63 61 6e 20 6e 65 76 65 72 20 66 61 69 6c 2e 20 can never fail.
1e57e 20 42 75 74 20 77 65 20 6c 65 61 76 65 20 69 74 But we leave it
1e57f 20 69 6e 20 70 6c 61 63 65 20 61 73 20 61 20 73 in place as a s
1e580 61 66 65 74 79 2e 0a 20 20 2a 2f 0a 20 20 61 73 afety.. */. as
1e581 73 65 72 74 28 20 75 2e 62 68 2e 70 43 2d 3e 64 sert( u.bh.pC->d
1e582 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 eferredMoveto==0
1e583 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 );. rc = sqlit
1e584 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 e3VdbeCursorMove
1e585 74 6f 28 75 2e 62 68 2e 70 43 29 3b 0a 20 20 69 to(u.bh.pC);. i
1e586 66 28 20 4e 45 56 45 52 28 72 63 21 3d 53 51 4c f( NEVER(rc!=SQL
1e587 49 54 45 5f 4f 4b 29 20 29 20 67 6f 74 6f 20 61 ITE_OK) ) goto a
1e588 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f bort_due_to_erro
1e589 72 3b 0a 0a 20 20 69 66 28 20 75 2e 62 68 2e 70 r;.. if( u.bh.p
1e58a 43 2d 3e 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 C->isIndex ){.
1e58b 20 20 61 73 73 65 72 74 28 20 21 75 2e 62 68 2e assert( !u.bh.
1e58c 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 pC->isTable );.
1e58d 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
1e58e 74 72 65 65 4b 65 79 53 69 7a 65 28 75 2e 62 68 treeKeySize(u.bh
1e58f 2e 70 43 72 73 72 2c 20 26 75 2e 62 68 2e 6e 36 .pCrsr, &u.bh.n6
1e590 34 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 4);. assert(
1e591 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b rc==SQLITE_OK );
1e592 20 20 20 20 2f 2a 20 54 72 75 65 20 62 65 63 61 /* True beca
1e593 75 73 65 20 6f 66 20 43 75 72 73 6f 72 4d 6f 76 use of CursorMov
1e594 65 74 6f 28 29 20 63 61 6c 6c 20 61 62 6f 76 65 eto() call above
1e595 20 2a 2f 0a 20 20 20 20 69 66 28 20 75 2e 62 68 */. if( u.bh
1e596 2e 6e 36 34 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b .n64>db->aLimit[
1e597 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e SQLITE_LIMIT_LEN
1e598 47 54 48 5d 20 29 7b 0a 20 20 20 20 20 20 67 6f GTH] ){. go
1e599 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 to too_big;.
1e59a 7d 0a 20 20 20 20 75 2e 62 68 2e 6e 20 3d 20 28 }. u.bh.n = (
1e59b 75 33 32 29 75 2e 62 68 2e 6e 36 34 3b 0a 20 20 u32)u.bh.n64;.
1e59c 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 }else{. rc =
1e59d 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 sqlite3BtreeData
1e59e 53 69 7a 65 28 75 2e 62 68 2e 70 43 72 73 72 2c Size(u.bh.pCrsr,
1e59f 20 26 75 2e 62 68 2e 6e 29 3b 0a 20 20 20 20 61 &u.bh.n);. a
1e5a0 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 ssert( rc==SQLIT
1e5a1 45 5f 4f 4b 20 29 3b 20 20 20 20 2f 2a 20 44 61 E_OK ); /* Da
1e5a2 74 61 53 69 7a 65 28 29 20 63 61 6e 6e 6f 74 20 taSize() cannot
1e5a3 66 61 69 6c 20 2a 2f 0a 20 20 20 20 69 66 28 20 fail */. if(
1e5a4 75 2e 62 68 2e 6e 3e 28 75 33 32 29 64 62 2d 3e u.bh.n>(u32)db->
1e5a5 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 aLimit[SQLITE_LI
1e5a6 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 MIT_LENGTH] ){.
1e5a7 20 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 goto too_bi
1e5a8 67 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 g;. }. }. i
1e5a9 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 f( sqlite3VdbeMe
1e5aa 6d 47 72 6f 77 28 70 4f 75 74 2c 20 75 2e 62 68 mGrow(pOut, u.bh
1e5ab 2e 6e 2c 20 30 29 20 29 7b 0a 20 20 20 20 67 6f .n, 0) ){. go
1e5ac 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 to no_mem;. }.
1e5ad 20 70 4f 75 74 2d 3e 6e 20 3d 20 75 2e 62 68 2e pOut->n = u.bh.
1e5ae 6e 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 n;. MemSetTypeF
1e5af 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 42 6c lag(pOut, MEM_Bl
1e5b0 6f 62 29 3b 0a 20 20 69 66 28 20 75 2e 62 68 2e ob);. if( u.bh.
1e5b1 70 43 2d 3e 69 73 49 6e 64 65 78 20 29 7b 0a 20 pC->isIndex ){.
1e5b2 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
1e5b3 74 72 65 65 4b 65 79 28 75 2e 62 68 2e 70 43 72 treeKey(u.bh.pCr
1e5b4 73 72 2c 20 30 2c 20 75 2e 62 68 2e 6e 2c 20 70 sr, 0, u.bh.n, p
1e5b5 4f 75 74 2d 3e 7a 29 3b 0a 20 20 7d 65 6c 73 65 Out->z);. }else
1e5b6 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
1e5b7 65 33 42 74 72 65 65 44 61 74 61 28 75 2e 62 68 e3BtreeData(u.bh
1e5b8 2e 70 43 72 73 72 2c 20 30 2c 20 75 2e 62 68 2e .pCrsr, 0, u.bh.
1e5b9 6e 2c 20 70 4f 75 74 2d 3e 7a 29 3b 0a 20 20 7d n, pOut->z);. }
1e5ba 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 53 . pOut->enc = S
1e5bb 51 4c 49 54 45 5f 55 54 46 38 3b 20 20 2f 2a 20 QLITE_UTF8; /*
1e5bc 49 6e 20 63 61 73 65 20 74 68 65 20 62 6c 6f 62 In case the blob
1e5bd 20 69 73 20 65 76 65 72 20 63 61 73 74 20 74 6f is ever cast to
1e5be 20 74 65 78 74 20 2a 2f 0a 20 20 55 50 44 41 54 text */. UPDAT
1e5bf 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 E_MAX_BLOBSIZE(p
1e5c0 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d Out);. break;.}
1e5c1 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 ../* Opcode: Row
1e5c2 69 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a id P1 P2 * * *.*
1e5c3 2a 0a 2a 2a 20 53 74 6f 72 65 20 69 6e 20 72 65 *.** Store in re
1e5c4 67 69 73 74 65 72 20 50 32 20 61 6e 20 69 6e 74 gister P2 an int
1e5c5 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74 68 eger which is th
1e5c6 65 20 6b 65 79 20 6f 66 20 74 68 65 20 74 61 62 e key of the tab
1e5c7 6c 65 20 65 6e 74 72 79 20 74 68 61 74 0a 2a 2a le entry that.**
1e5c8 20 50 31 20 69 73 20 63 75 72 72 65 6e 74 6c 79 P1 is currently
1e5c9 20 70 6f 69 6e 74 20 74 6f 2e 0a 2a 2a 0a 2a 2a point to..**.**
1e5ca 20 50 31 20 63 61 6e 20 62 65 20 65 69 74 68 65 P1 can be eithe
1e5cb 72 20 61 6e 20 6f 72 64 69 6e 61 72 79 20 74 61 r an ordinary ta
1e5cc 62 6c 65 20 6f 72 20 61 20 76 69 72 74 75 61 6c ble or a virtual
1e5cd 20 74 61 62 6c 65 2e 20 20 54 68 65 72 65 20 75 table. There u
1e5ce 73 65 64 20 74 6f 0a 2a 2a 20 62 65 20 61 20 73 sed to.** be a s
1e5cf 65 70 61 72 61 74 65 20 4f 50 5f 56 52 6f 77 69 eparate OP_VRowi
1e5d0 64 20 6f 70 63 6f 64 65 20 66 6f 72 20 75 73 65 d opcode for use
1e5d1 20 77 69 74 68 20 76 69 72 74 75 61 6c 20 74 61 with virtual ta
1e5d2 62 6c 65 73 2c 20 62 75 74 20 74 68 69 73 0a 2a bles, but this.*
1e5d3 2a 20 6f 6e 65 20 6f 70 63 6f 64 65 20 6e 6f 77 * one opcode now
1e5d4 20 77 6f 72 6b 73 20 66 6f 72 20 62 6f 74 68 20 works for both
1e5d5 74 61 62 6c 65 20 74 79 70 65 73 2e 0a 2a 2f 0a table types..*/.
1e5d6 63 61 73 65 20 4f 50 5f 52 6f 77 69 64 3a 20 7b case OP_Rowid: {
1e5d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e5d8 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 /* out2-prerele
1e5d9 61 73 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a ase */.#if 0 /*
1e5da 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1e5db 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 69 moved into u.bi
1e5dc 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 */. VdbeCursor
1e5dd 20 2a 70 43 3b 0a 20 20 69 36 34 20 76 3b 0a 20 *pC;. i64 v;.
1e5de 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 sqlite3_vtab *p
1e5df 56 74 61 62 3b 0a 20 20 63 6f 6e 73 74 20 73 71 Vtab;. const sq
1e5e0 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d lite3_module *pM
1e5e1 6f 64 75 6c 65 3b 0a 23 65 6e 64 69 66 20 2f 2a odule;.#endif /*
1e5e2 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1e5e3 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 69 moved into u.bi
1e5e4 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 */.. assert( p
1e5e5 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 Op->p1>=0 && pOp
1e5e6 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 ->p1<p->nCursor
1e5e7 29 3b 0a 20 20 75 2e 62 69 2e 70 43 20 3d 20 70 );. u.bi.pC = p
1e5e8 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d ->apCsr[pOp->p1]
1e5e9 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 69 ;. assert( u.bi
1e5ea 2e 70 43 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 .pC!=0 );. asse
1e5eb 72 74 28 20 75 2e 62 69 2e 70 43 2d 3e 70 73 65 rt( u.bi.pC->pse
1e5ec 75 64 6f 54 61 62 6c 65 52 65 67 3d 3d 30 20 29 udoTableReg==0 )
1e5ed 3b 0a 20 20 69 66 28 20 75 2e 62 69 2e 70 43 2d ;. if( u.bi.pC-
1e5ee 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 >nullRow ){.
1e5ef 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 20 73 6f /* Do nothing so
1e5f0 20 74 68 61 74 20 72 65 67 5b 50 32 5d 20 72 65 that reg[P2] re
1e5f1 6d 61 69 6e 73 20 4e 55 4c 4c 20 2a 2f 0a 20 20 mains NULL */.
1e5f2 20 20 62 72 65 61 6b 3b 0a 20 20 7d 65 6c 73 65 break;. }else
1e5f3 20 69 66 28 20 75 2e 62 69 2e 70 43 2d 3e 64 65 if( u.bi.pC->de
1e5f4 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 29 7b 0a ferredMoveto ){.
1e5f5 20 20 20 20 75 2e 62 69 2e 76 20 3d 20 75 2e 62 u.bi.v = u.b
1e5f6 69 2e 70 43 2d 3e 6d 6f 76 65 74 6f 54 61 72 67 i.pC->movetoTarg
1e5f7 65 74 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 et;.#ifndef SQLI
1e5f8 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
1e5f9 41 42 4c 45 0a 20 20 7d 65 6c 73 65 20 69 66 28 ABLE. }else if(
1e5fa 20 75 2e 62 69 2e 70 43 2d 3e 70 56 74 61 62 43 u.bi.pC->pVtabC
1e5fb 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 75 2e 62 ursor ){. u.b
1e5fc 69 2e 70 56 74 61 62 20 3d 20 75 2e 62 69 2e 70 i.pVtab = u.bi.p
1e5fd 43 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e C->pVtabCursor->
1e5fe 70 56 74 61 62 3b 0a 20 20 20 20 75 2e 62 69 2e pVtab;. u.bi.
1e5ff 70 4d 6f 64 75 6c 65 20 3d 20 75 2e 62 69 2e 70 pModule = u.bi.p
1e600 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 Vtab->pModule;.
1e601 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 69 2e assert( u.bi.
1e602 70 4d 6f 64 75 6c 65 2d 3e 78 52 6f 77 69 64 20 pModule->xRowid
1e603 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 );. if( sqlit
1e604 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 e3SafetyOff(db)
1e605 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 ) goto abort_due
1e606 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 _to_misuse;.
1e607 72 63 20 3d 20 75 2e 62 69 2e 70 4d 6f 64 75 6c rc = u.bi.pModul
1e608 65 2d 3e 78 52 6f 77 69 64 28 75 2e 62 69 2e 70 e->xRowid(u.bi.p
1e609 43 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2c 20 C->pVtabCursor,
1e60a 26 75 2e 62 69 2e 76 29 3b 0a 20 20 20 20 73 71 &u.bi.v);. sq
1e60b 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
1e60c 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 p->zErrMsg);.
1e60d 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e p->zErrMsg = u.
1e60e 62 69 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 bi.pVtab->zErrMs
1e60f 67 3b 0a 20 20 20 20 75 2e 62 69 2e 70 56 74 61 g;. u.bi.pVta
1e610 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a b->zErrMsg = 0;.
1e611 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 if( sqlite3S
1e612 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f afetyOn(db) ) go
1e613 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1e614 6d 69 73 75 73 65 3b 0a 23 65 6e 64 69 66 20 2f misuse;.#endif /
1e615 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 * SQLITE_OMIT_VI
1e616 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 20 20 RTUALTABLE */.
1e617 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 }else{. asser
1e618 74 28 20 75 2e 62 69 2e 70 43 2d 3e 70 43 75 72 t( u.bi.pC->pCur
1e619 73 6f 72 21 3d 30 20 29 3b 0a 20 20 20 20 72 63 sor!=0 );. rc
1e61a 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 = sqlite3VdbeCu
1e61b 72 73 6f 72 4d 6f 76 65 74 6f 28 75 2e 62 69 2e rsorMoveto(u.bi.
1e61c 70 43 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 pC);. if( rc
1e61d 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 ) goto abort_due
1e61e 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 69 _to_error;. i
1e61f 66 28 20 75 2e 62 69 2e 70 43 2d 3e 72 6f 77 69 f( u.bi.pC->rowi
1e620 64 49 73 56 61 6c 69 64 20 29 7b 0a 20 20 20 20 dIsValid ){.
1e621 20 20 75 2e 62 69 2e 76 20 3d 20 75 2e 62 69 2e u.bi.v = u.bi.
1e622 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20 pC->lastRowid;.
1e623 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1e624 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1e625 65 4b 65 79 53 69 7a 65 28 75 2e 62 69 2e 70 43 eKeySize(u.bi.pC
1e626 2d 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e 62 69 ->pCursor, &u.bi
1e627 2e 76 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 .v);. asser
1e628 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b t( rc==SQLITE_OK
1e629 20 29 3b 20 20 2f 2a 20 41 6c 77 61 79 73 20 73 ); /* Always s
1e62a 6f 20 62 65 63 61 75 73 65 20 6f 66 20 43 75 72 o because of Cur
1e62b 73 6f 72 4d 6f 76 65 74 6f 28 29 20 61 62 6f 76 sorMoveto() abov
1e62c 65 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 e */. }. }.
1e62d 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 62 pOut->u.i = u.b
1e62e 69 2e 76 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 i.v;. MemSetTyp
1e62f 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f eFlag(pOut, MEM_
1e630 49 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d Int);. break;.}
1e631 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 75 6c ../* Opcode: Nul
1e632 6c 52 6f 77 20 50 31 20 2a 20 2a 20 2a 20 2a 0a lRow P1 * * * *.
1e633 2a 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 **.** Move the c
1e634 75 72 73 6f 72 20 50 31 20 74 6f 20 61 20 6e 75 ursor P1 to a nu
1e635 6c 6c 20 72 6f 77 2e 20 20 41 6e 79 20 4f 50 5f ll row. Any OP_
1e636 43 6f 6c 75 6d 6e 20 6f 70 65 72 61 74 69 6f 6e Column operation
1e637 73 0a 2a 2a 20 74 68 61 74 20 6f 63 63 75 72 20 s.** that occur
1e638 77 68 69 6c 65 20 74 68 65 20 63 75 72 73 6f 72 while the cursor
1e639 20 69 73 20 6f 6e 20 74 68 65 20 6e 75 6c 6c 20 is on the null
1e63a 72 6f 77 20 77 69 6c 6c 20 61 6c 77 61 79 73 0a row will always.
1e63b 2a 2a 20 77 72 69 74 65 20 61 20 4e 55 4c 4c 2e ** write a NULL.
1e63c 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 75 6c 6c .*/.case OP_Null
1e63d 52 6f 77 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a Row: {.#if 0 /*
1e63e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1e63f 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6a moved into u.bj
1e640 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 */. VdbeCursor
1e641 20 2a 70 43 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 *pC;.#endif /*
1e642 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1e643 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6a 20 moved into u.bj
1e644 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f */.. assert( pO
1e645 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d p->p1>=0 && pOp-
1e646 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 >p1<p->nCursor )
1e647 3b 0a 20 20 75 2e 62 6a 2e 70 43 20 3d 20 70 2d ;. u.bj.pC = p-
1e648 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b >apCsr[pOp->p1];
1e649 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 6a 2e . assert( u.bj.
1e64a 70 43 21 3d 30 20 29 3b 0a 20 20 75 2e 62 6a 2e pC!=0 );. u.bj.
1e64b 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b pC->nullRow = 1;
1e64c 0a 20 20 75 2e 62 6a 2e 70 43 2d 3e 72 6f 77 69 . u.bj.pC->rowi
1e64d 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 dIsValid = 0;.
1e64e 69 66 28 20 75 2e 62 6a 2e 70 43 2d 3e 70 43 75 if( u.bj.pC->pCu
1e64f 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 rsor ){. sqli
1e650 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 te3BtreeClearCur
1e651 73 6f 72 28 75 2e 62 6a 2e 70 43 2d 3e 70 43 75 sor(u.bj.pC->pCu
1e652 72 73 6f 72 29 3b 0a 20 20 7d 0a 20 20 62 72 65 rsor);. }. bre
1e653 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1e654 3a 20 4c 61 73 74 20 50 31 20 50 32 20 2a 20 2a : Last P1 P2 * *
1e655 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 *.**.** The nex
1e656 74 20 75 73 65 20 6f 66 20 74 68 65 20 52 6f 77 t use of the Row
1e657 69 64 20 6f 72 20 43 6f 6c 75 6d 6e 20 6f 72 20 id or Column or
1e658 4e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e Next instruction
1e659 20 66 6f 72 20 50 31 20 0a 2a 2a 20 77 69 6c 6c for P1 .** will
1e65a 20 72 65 66 65 72 20 74 6f 20 74 68 65 20 6c 61 refer to the la
1e65b 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 st entry in the
1e65c 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f database table o
1e65d 72 20 69 6e 64 65 78 2e 0a 2a 2a 20 49 66 20 74 r index..** If t
1e65e 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 he table or inde
1e65f 78 20 69 73 20 65 6d 70 74 79 20 61 6e 64 20 50 x is empty and P
1e660 32 3e 30 2c 20 74 68 65 6e 20 6a 75 6d 70 20 69 2>0, then jump i
1e661 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 mmediately to P2
1e662 2e 0a 2a 2a 20 49 66 20 50 32 20 69 73 20 30 20 ..** If P2 is 0
1e663 6f 72 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 or if the table
1e664 6f 72 20 69 6e 64 65 78 20 69 73 20 6e 6f 74 20 or index is not
1e665 65 6d 70 74 79 2c 20 66 61 6c 6c 20 74 68 72 6f empty, fall thro
1e666 75 67 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f ugh.** to the fo
1e667 6c 6c 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 74 llowing instruct
1e668 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ion..*/.case OP_
1e669 4c 61 73 74 3a 20 7b 20 20 20 20 20 20 20 20 2f Last: { /
1e66a 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 66 20 30 20 * jump */.#if 0
1e66b 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1e66c 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1e66d 2e 62 6b 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 .bk */. VdbeCur
1e66e 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 sor *pC;. BtCur
1e66f 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e sor *pCrsr;. in
1e670 74 20 72 65 73 3b 0a 23 65 6e 64 69 66 20 2f 2a t res;.#endif /*
1e671 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1e672 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6b moved into u.bk
1e673 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 */.. assert( p
1e674 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 Op->p1>=0 && pOp
1e675 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 ->p1<p->nCursor
1e676 29 3b 0a 20 20 75 2e 62 6b 2e 70 43 20 3d 20 70 );. u.bk.pC = p
1e677 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d ->apCsr[pOp->p1]
1e678 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 6b ;. assert( u.bk
1e679 2e 70 43 21 3d 30 20 29 3b 0a 20 20 75 2e 62 6b .pC!=0 );. u.bk
1e67a 2e 70 43 72 73 72 20 3d 20 75 2e 62 6b 2e 70 43 .pCrsr = u.bk.pC
1e67b 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 ->pCursor;. if(
1e67c 20 75 2e 62 6b 2e 70 43 72 73 72 3d 3d 30 20 29 u.bk.pCrsr==0 )
1e67d 7b 0a 20 20 20 20 75 2e 62 6b 2e 72 65 73 20 3d {. u.bk.res =
1e67e 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 1;. }else{.
1e67f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1e680 65 65 4c 61 73 74 28 75 2e 62 6b 2e 70 43 72 73 eeLast(u.bk.pCrs
1e681 72 2c 20 26 75 2e 62 6b 2e 72 65 73 29 3b 0a 20 r, &u.bk.res);.
1e682 20 7d 0a 20 20 75 2e 62 6b 2e 70 43 2d 3e 6e 75 }. u.bk.pC->nu
1e683 6c 6c 52 6f 77 20 3d 20 28 75 38 29 75 2e 62 6b llRow = (u8)u.bk
1e684 2e 72 65 73 3b 0a 20 20 75 2e 62 6b 2e 70 43 2d .res;. u.bk.pC-
1e685 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 >deferredMoveto
1e686 3d 20 30 3b 0a 20 20 75 2e 62 6b 2e 70 43 2d 3e = 0;. u.bk.pC->
1e687 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 rowidIsValid = 0
1e688 3b 0a 20 20 75 2e 62 6b 2e 70 43 2d 3e 63 61 63 ;. u.bk.pC->cac
1e689 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 heStatus = CACHE
1e68a 5f 53 54 41 4c 45 3b 0a 20 20 69 66 28 20 70 4f _STALE;. if( pO
1e68b 70 2d 3e 70 32 3e 30 20 26 26 20 75 2e 62 6b 2e p->p2>0 && u.bk.
1e68c 72 65 73 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 res ){. pc =
1e68d 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d pOp->p2 - 1;. }
1e68e 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f 2a . break;.}.../*
1e68f 20 4f 70 63 6f 64 65 3a 20 53 6f 72 74 20 50 31 Opcode: Sort P1
1e690 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * * *.**.**
1e691 54 68 69 73 20 6f 70 63 6f 64 65 20 64 6f 65 73 This opcode does
1e692 20 65 78 61 63 74 6c 79 20 74 68 65 20 73 61 6d exactly the sam
1e693 65 20 74 68 69 6e 67 20 61 73 20 4f 50 5f 52 65 e thing as OP_Re
1e694 77 69 6e 64 20 65 78 63 65 70 74 20 74 68 61 74 wind except that
1e695 0a 2a 2a 20 69 74 20 69 6e 63 72 65 6d 65 6e 74 .** it increment
1e696 73 20 61 6e 20 75 6e 64 6f 63 75 6d 65 6e 74 65 s an undocumente
1e697 64 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c d global variabl
1e698 65 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 e used for testi
1e699 6e 67 2e 0a 2a 2a 0a 2a 2a 20 53 6f 72 74 69 6e ng..**.** Sortin
1e69a 67 20 69 73 20 61 63 63 6f 6d 70 6c 69 73 68 65 g is accomplishe
1e69b 64 20 62 79 20 77 72 69 74 69 6e 67 20 72 65 63 d by writing rec
1e69c 6f 72 64 73 20 69 6e 74 6f 20 61 20 73 6f 72 74 ords into a sort
1e69d 69 6e 67 20 69 6e 64 65 78 2c 0a 2a 2a 20 74 68 ing index,.** th
1e69e 65 6e 20 72 65 77 69 6e 64 69 6e 67 20 74 68 61 en rewinding tha
1e69f 74 20 69 6e 64 65 78 20 61 6e 64 20 70 6c 61 79 t index and play
1e6a0 69 6e 67 20 69 74 20 62 61 63 6b 20 66 72 6f 6d ing it back from
1e6a1 20 62 65 67 69 6e 6e 69 6e 67 20 74 6f 0a 2a 2a beginning to.**
1e6a2 20 65 6e 64 2e 20 20 57 65 20 75 73 65 20 74 68 end. We use th
1e6a3 65 20 4f 50 5f 53 6f 72 74 20 6f 70 63 6f 64 65 e OP_Sort opcode
1e6a4 20 69 6e 73 74 65 61 64 20 6f 66 20 4f 50 5f 52 instead of OP_R
1e6a5 65 77 69 6e 64 20 74 6f 20 64 6f 20 74 68 65 0a ewind to do the.
1e6a6 2a 2a 20 72 65 77 69 6e 64 69 6e 67 20 73 6f 20 ** rewinding so
1e6a7 74 68 61 74 20 74 68 65 20 67 6c 6f 62 61 6c 20 that the global
1e6a8 76 61 72 69 61 62 6c 65 20 77 69 6c 6c 20 62 65 variable will be
1e6a9 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 61 6e 64 incremented and
1e6aa 0a 2a 2a 20 72 65 67 72 65 73 73 69 6f 6e 20 74 .** regression t
1e6ab 65 73 74 73 20 63 61 6e 20 64 65 74 65 72 6d 69 ests can determi
1e6ac 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f ne whether or no
1e6ad 74 20 74 68 65 20 6f 70 74 69 6d 69 7a 65 72 20 t the optimizer
1e6ae 69 73 0a 2a 2a 20 63 6f 72 72 65 63 74 6c 79 20 is.** correctly
1e6af 6f 70 74 69 6d 69 7a 69 6e 67 20 6f 75 74 20 73 optimizing out s
1e6b0 6f 72 74 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 orts..*/.case OP
1e6b1 5f 53 6f 72 74 3a 20 7b 20 20 20 20 20 20 20 20 _Sort: {
1e6b2 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 66 64 65 /* jump */.#ifde
1e6b3 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 f SQLITE_TEST.
1e6b4 73 71 6c 69 74 65 33 5f 73 6f 72 74 5f 63 6f 75 sqlite3_sort_cou
1e6b5 6e 74 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33 5f nt++;. sqlite3_
1e6b6 73 65 61 72 63 68 5f 63 6f 75 6e 74 2d 2d 3b 0a search_count--;.
1e6b7 23 65 6e 64 69 66 0a 20 20 70 2d 3e 61 43 6f 75 #endif. p->aCou
1e6b8 6e 74 65 72 5b 53 51 4c 49 54 45 5f 53 54 4d 54 nter[SQLITE_STMT
1e6b9 53 54 41 54 55 53 5f 53 4f 52 54 2d 31 5d 2b 2b STATUS_SORT-1]++
1e6ba 3b 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f ;. /* Fall thro
1e6bb 75 67 68 20 69 6e 74 6f 20 4f 50 5f 52 65 77 69 ugh into OP_Rewi
1e6bc 6e 64 20 2a 2f 0a 7d 0a 2f 2a 20 4f 70 63 6f 64 nd */.}./* Opcod
1e6bd 65 3a 20 52 65 77 69 6e 64 20 50 31 20 50 32 20 e: Rewind P1 P2
1e6be 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 * * *.**.** The
1e6bf 6e 65 78 74 20 75 73 65 20 6f 66 20 74 68 65 20 next use of the
1e6c0 52 6f 77 69 64 20 6f 72 20 43 6f 6c 75 6d 6e 20 Rowid or Column
1e6c1 6f 72 20 4e 65 78 74 20 69 6e 73 74 72 75 63 74 or Next instruct
1e6c2 69 6f 6e 20 66 6f 72 20 50 31 20 0a 2a 2a 20 77 ion for P1 .** w
1e6c3 69 6c 6c 20 72 65 66 65 72 20 74 6f 20 74 68 65 ill refer to the
1e6c4 20 66 69 72 73 74 20 65 6e 74 72 79 20 69 6e 20 first entry in
1e6c5 74 68 65 20 64 61 74 61 62 61 73 65 20 74 61 62 the database tab
1e6c6 6c 65 20 6f 72 20 69 6e 64 65 78 2e 0a 2a 2a 20 le or index..**
1e6c7 49 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 If the table or
1e6c8 69 6e 64 65 78 20 69 73 20 65 6d 70 74 79 20 61 index is empty a
1e6c9 6e 64 20 50 32 3e 30 2c 20 74 68 65 6e 20 6a 75 nd P2>0, then ju
1e6ca 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 mp immediately t
1e6cb 6f 20 50 32 2e 0a 2a 2a 20 49 66 20 50 32 20 69 o P2..** If P2 i
1e6cc 73 20 30 20 6f 72 20 69 66 20 74 68 65 20 74 61 s 0 or if the ta
1e6cd 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 73 20 ble or index is
1e6ce 6e 6f 74 20 65 6d 70 74 79 2c 20 66 61 6c 6c 20 not empty, fall
1e6cf 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f 20 74 68 through.** to th
1e6d0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73 74 e following inst
1e6d1 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 ruction..*/.case
1e6d2 20 4f 50 5f 52 65 77 69 6e 64 3a 20 7b 20 20 20 OP_Rewind: {
1e6d3 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a /* jump */.
1e6d4 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 #if 0 /* local
1e6d5 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1e6d6 69 6e 74 6f 20 75 2e 62 6c 20 2a 2f 0a 20 20 56 into u.bl */. V
1e6d7 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 dbeCursor *pC;.
1e6d8 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 BtCursor *pCrsr
1e6d9 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 23 65 6e ;. int res;.#en
1e6da 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 dif /* local var
1e6db 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1e6dc 6f 20 75 2e 62 6c 20 2a 2f 0a 0a 20 20 61 73 73 o u.bl */.. ass
1e6dd 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 ert( pOp->p1>=0
1e6de 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 && pOp->p1<p->nC
1e6df 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 6c 2e ursor );. u.bl.
1e6e0 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f pC = p->apCsr[pO
1e6e1 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 p->p1];. assert
1e6e2 28 20 75 2e 62 6c 2e 70 43 21 3d 30 20 29 3b 0a ( u.bl.pC!=0 );.
1e6e3 20 20 69 66 28 20 28 75 2e 62 6c 2e 70 43 72 73 if( (u.bl.pCrs
1e6e4 72 20 3d 20 75 2e 62 6c 2e 70 43 2d 3e 70 43 75 r = u.bl.pC->pCu
1e6e5 72 73 6f 72 29 21 3d 30 20 29 7b 0a 20 20 20 20 rsor)!=0 ){.
1e6e6 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1e6e7 65 46 69 72 73 74 28 75 2e 62 6c 2e 70 43 72 73 eFirst(u.bl.pCrs
1e6e8 72 2c 20 26 75 2e 62 6c 2e 72 65 73 29 3b 0a 20 r, &u.bl.res);.
1e6e9 20 20 20 75 2e 62 6c 2e 70 43 2d 3e 61 74 46 69 u.bl.pC->atFi
1e6ea 72 73 74 20 3d 20 75 2e 62 6c 2e 72 65 73 3d 3d rst = u.bl.res==
1e6eb 30 20 3f 31 3a 30 3b 0a 20 20 20 20 75 2e 62 6c 0 ?1:0;. u.bl
1e6ec 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 .pC->deferredMov
1e6ed 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 eto = 0;. u.b
1e6ee 6c 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 l.pC->cacheStatu
1e6ef 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b s = CACHE_STALE;
1e6f0 0a 20 20 20 20 75 2e 62 6c 2e 70 43 2d 3e 72 6f . u.bl.pC->ro
1e6f1 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a widIsValid = 0;.
1e6f2 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 62 }else{. u.b
1e6f3 6c 2e 72 65 73 20 3d 20 31 3b 0a 20 20 7d 0a 20 l.res = 1;. }.
1e6f4 20 75 2e 62 6c 2e 70 43 2d 3e 6e 75 6c 6c 52 6f u.bl.pC->nullRo
1e6f5 77 20 3d 20 28 75 38 29 75 2e 62 6c 2e 72 65 73 w = (u8)u.bl.res
1e6f6 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d ;. assert( pOp-
1e6f7 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 >p2>0 && pOp->p2
1e6f8 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 20 20 69 66 28 <p->nOp );. if(
1e6f9 20 75 2e 62 6c 2e 72 65 73 20 29 7b 0a 20 20 20 u.bl.res ){.
1e6fa 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 pc = pOp->p2 -
1e6fb 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 1;. }. break;.
1e6fc 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 65 }../* Opcode: Ne
1e6fd 78 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a xt P1 P2 * * *.*
1e6fe 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 63 75 72 *.** Advance cur
1e6ff 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74 20 69 sor P1 so that i
1e700 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 t points to the
1e701 6e 65 78 74 20 6b 65 79 2f 64 61 74 61 20 70 61 next key/data pa
1e702 69 72 20 69 6e 20 69 74 73 0a 2a 2a 20 74 61 62 ir in its.** tab
1e703 6c 65 20 6f 72 20 69 6e 64 65 78 2e 20 20 49 66 le or index. If
1e704 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6d 6f there are no mo
1e705 72 65 20 6b 65 79 2f 76 61 6c 75 65 20 70 61 69 re key/value pai
1e706 72 73 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 rs then fall thr
1e707 6f 75 67 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 ough.** to the f
1e708 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 ollowing instruc
1e709 74 69 6f 6e 2e 20 20 42 75 74 20 69 66 20 74 68 tion. But if th
1e70a 65 20 63 75 72 73 6f 72 20 61 64 76 61 6e 63 65 e cursor advance
1e70b 20 77 61 73 20 73 75 63 63 65 73 73 66 75 6c 2c was successful,
1e70c 0a 2a 2a 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 .** jump immedia
1e70d 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a tely to P2..**.*
1e70e 2a 20 54 68 65 20 50 31 20 63 75 72 73 6f 72 20 * The P1 cursor
1e70f 6d 75 73 74 20 62 65 20 66 6f 72 20 61 20 72 65 must be for a re
1e710 61 6c 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20 al table, not a
1e711 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2a pseudo-table..**
1e712 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 50 72 .** See also: Pr
1e713 65 76 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a ev.*/./* Opcode:
1e714 20 50 72 65 76 20 50 31 20 50 32 20 2a 20 2a 20 Prev P1 P2 * *
1e715 2a 0a 2a 2a 0a 2a 2a 20 42 61 63 6b 20 75 70 20 *.**.** Back up
1e716 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61 cursor P1 so tha
1e717 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 t it points to t
1e718 68 65 20 70 72 65 76 69 6f 75 73 20 6b 65 79 2f he previous key/
1e719 64 61 74 61 20 70 61 69 72 20 69 6e 20 69 74 73 data pair in its
1e71a 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 .** table or ind
1e71b 65 78 2e 20 20 49 66 20 74 68 65 72 65 20 69 73 ex. If there is
1e71c 20 6e 6f 20 70 72 65 76 69 6f 75 73 20 6b 65 79 no previous key
1e71d 2f 76 61 6c 75 65 20 70 61 69 72 73 20 74 68 65 /value pairs the
1e71e 6e 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 0a 2a n fall through.*
1e71f 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 * to the followi
1e720 6e 67 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 ng instruction.
1e721 20 42 75 74 20 69 66 20 74 68 65 20 63 75 72 73 But if the curs
1e722 6f 72 20 62 61 63 6b 75 70 20 77 61 73 20 73 75 or backup was su
1e723 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 6a 75 6d ccessful,.** jum
1e724 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f p immediately to
1e725 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 P2..**.** The P
1e726 31 20 63 75 72 73 6f 72 20 6d 75 73 74 20 62 65 1 cursor must be
1e727 20 66 6f 72 20 61 20 72 65 61 6c 20 74 61 62 6c for a real tabl
1e728 65 2c 20 6e 6f 74 20 61 20 70 73 65 75 64 6f 2d e, not a pseudo-
1e729 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f table..*/.case O
1e72a 50 5f 50 72 65 76 3a 20 20 20 20 20 20 20 20 20 P_Prev:
1e72b 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 63 61 73 65 /* jump */.case
1e72c 20 4f 50 5f 4e 65 78 74 3a 20 7b 20 20 20 20 20 OP_Next: {
1e72d 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 /* jump */.#i
1e72e 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1e72f 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1e730 74 6f 20 75 2e 62 6d 20 2a 2f 0a 20 20 56 64 62 to u.bm */. Vdb
1e731 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 eCursor *pC;. B
1e732 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a tCursor *pCrsr;.
1e733 20 20 69 6e 74 20 72 65 73 3b 0a 23 65 6e 64 69 int res;.#endi
1e734 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1e735 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1e736 75 2e 62 6d 20 2a 2f 0a 0a 20 20 43 48 45 43 4b u.bm */.. CHECK
1e737 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 3b 0a _FOR_INTERRUPT;.
1e738 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1e739 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 1>=0 && pOp->p1<
1e73a 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 p->nCursor );.
1e73b 75 2e 62 6d 2e 70 43 20 3d 20 70 2d 3e 61 70 43 u.bm.pC = p->apC
1e73c 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 sr[pOp->p1];. i
1e73d 66 28 20 75 2e 62 6d 2e 70 43 3d 3d 30 20 29 7b f( u.bm.pC==0 ){
1e73e 0a 20 20 20 20 62 72 65 61 6b 3b 20 20 2f 2a 20 . break; /*
1e73f 53 65 65 20 74 69 63 6b 65 74 20 23 32 32 37 33 See ticket #2273
1e740 20 2a 2f 0a 20 20 7d 0a 20 20 75 2e 62 6d 2e 70 */. }. u.bm.p
1e741 43 72 73 72 20 3d 20 75 2e 62 6d 2e 70 43 2d 3e Crsr = u.bm.pC->
1e742 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 75 pCursor;. if( u
1e743 2e 62 6d 2e 70 43 72 73 72 3d 3d 30 20 29 7b 0a .bm.pCrsr==0 ){.
1e744 20 20 20 20 75 2e 62 6d 2e 70 43 2d 3e 6e 75 6c u.bm.pC->nul
1e745 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 20 20 62 72 lRow = 1;. br
1e746 65 61 6b 3b 0a 20 20 7d 0a 20 20 75 2e 62 6d 2e eak;. }. u.bm.
1e747 72 65 73 20 3d 20 31 3b 0a 20 20 61 73 73 65 72 res = 1;. asser
1e748 74 28 20 75 2e 62 6d 2e 70 43 2d 3e 64 65 66 65 t( u.bm.pC->defe
1e749 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b rredMoveto==0 );
1e74a 0a 20 20 72 63 20 3d 20 70 4f 70 2d 3e 6f 70 63 . rc = pOp->opc
1e74b 6f 64 65 3d 3d 4f 50 5f 4e 65 78 74 20 3f 20 73 ode==OP_Next ? s
1e74c 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 qlite3BtreeNext(
1e74d 75 2e 62 6d 2e 70 43 72 73 72 2c 20 26 75 2e 62 u.bm.pCrsr, &u.b
1e74e 6d 2e 72 65 73 29 20 3a 0a 20 20 20 20 20 20 20 m.res) :.
1e74f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e750 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 sqlite3Bt
1e751 72 65 65 50 72 65 76 69 6f 75 73 28 75 2e 62 6d reePrevious(u.bm
1e752 2e 70 43 72 73 72 2c 20 26 75 2e 62 6d 2e 72 65 .pCrsr, &u.bm.re
1e753 73 29 3b 0a 20 20 75 2e 62 6d 2e 70 43 2d 3e 6e s);. u.bm.pC->n
1e754 75 6c 6c 52 6f 77 20 3d 20 28 75 38 29 75 2e 62 ullRow = (u8)u.b
1e755 6d 2e 72 65 73 3b 0a 20 20 75 2e 62 6d 2e 70 43 m.res;. u.bm.pC
1e756 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 ->cacheStatus =
1e757 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 69 CACHE_STALE;. i
1e758 66 28 20 75 2e 62 6d 2e 72 65 73 3d 3d 30 20 29 f( u.bm.res==0 )
1e759 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e {. pc = pOp->
1e75a 70 32 20 2d 20 31 3b 0a 20 20 20 20 69 66 28 20 p2 - 1;. if(
1e75b 70 4f 70 2d 3e 70 35 20 29 20 70 2d 3e 61 43 6f pOp->p5 ) p->aCo
1e75c 75 6e 74 65 72 5b 70 4f 70 2d 3e 70 35 2d 31 5d unter[pOp->p5-1]
1e75d 2b 2b 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 ++;.#ifdef SQLIT
1e75e 45 5f 54 45 53 54 0a 20 20 20 20 73 71 6c 69 74 E_TEST. sqlit
1e75f 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 2b e3_search_count+
1e760 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 +;.#endif. }.
1e761 75 2e 62 6d 2e 70 43 2d 3e 72 6f 77 69 64 49 73 u.bm.pC->rowidIs
1e762 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 62 72 65 Valid = 0;. bre
1e763 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1e764 3a 20 49 64 78 49 6e 73 65 72 74 20 50 31 20 50 : IdxInsert P1 P
1e765 32 20 50 33 20 2a 20 50 35 0a 2a 2a 0a 2a 2a 20 2 P3 * P5.**.**
1e766 52 65 67 69 73 74 65 72 20 50 32 20 68 6f 6c 64 Register P2 hold
1e767 73 20 61 20 53 51 4c 20 69 6e 64 65 78 20 6b 65 s a SQL index ke
1e768 79 20 6d 61 64 65 20 75 73 69 6e 67 20 74 68 65 y made using the
1e769 0a 2a 2a 20 4d 61 6b 65 52 65 63 6f 72 64 20 69 .** MakeRecord i
1e76a 6e 73 74 72 75 63 74 69 6f 6e 73 2e 20 20 54 68 nstructions. Th
1e76b 69 73 20 6f 70 63 6f 64 65 20 77 72 69 74 65 73 is opcode writes
1e76c 20 74 68 61 74 20 6b 65 79 0a 2a 2a 20 69 6e 74 that key.** int
1e76d 6f 20 74 68 65 20 69 6e 64 65 78 20 50 31 2e 20 o the index P1.
1e76e 20 44 61 74 61 20 66 6f 72 20 74 68 65 20 65 6e Data for the en
1e76f 74 72 79 20 69 73 20 6e 69 6c 2e 0a 2a 2a 0a 2a try is nil..**.*
1e770 2a 20 50 33 20 69 73 20 61 20 66 6c 61 67 20 74 * P3 is a flag t
1e771 68 61 74 20 70 72 6f 76 69 64 65 73 20 61 20 68 hat provides a h
1e772 69 6e 74 20 74 6f 20 74 68 65 20 62 2d 74 72 65 int to the b-tre
1e773 65 20 6c 61 79 65 72 20 74 68 61 74 20 74 68 69 e layer that thi
1e774 73 0a 2a 2a 20 69 6e 73 65 72 74 20 69 73 20 6c s.** insert is l
1e775 69 6b 65 6c 79 20 74 6f 20 62 65 20 61 6e 20 61 ikely to be an a
1e776 70 70 65 6e 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 ppend..**.** Thi
1e777 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 6e s instruction on
1e778 6c 79 20 77 6f 72 6b 73 20 66 6f 72 20 69 6e 64 ly works for ind
1e779 69 63 65 73 2e 20 20 54 68 65 20 65 71 75 69 76 ices. The equiv
1e77a 61 6c 65 6e 74 20 69 6e 73 74 72 75 63 74 69 6f alent instructio
1e77b 6e 0a 2a 2a 20 66 6f 72 20 74 61 62 6c 65 73 20 n.** for tables
1e77c 69 73 20 4f 50 5f 49 6e 73 65 72 74 2e 0a 2a 2f is OP_Insert..*/
1e77d 0a 63 61 73 65 20 4f 50 5f 49 64 78 49 6e 73 65 .case OP_IdxInse
1e77e 72 74 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 rt: { /*
1e77f 69 6e 32 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a in2 */.#if 0 /*
1e780 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1e781 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6e moved into u.bn
1e782 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 */. VdbeCursor
1e783 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 *pC;. BtCursor
1e784 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e 74 20 6e *pCrsr;. int n
1e785 4b 65 79 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 Key;. const cha
1e786 72 20 2a 7a 4b 65 79 3b 0a 23 65 6e 64 69 66 20 r *zKey;.#endif
1e787 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1e788 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1e789 62 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 bn */.. assert(
1e78a 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 pOp->p1>=0 && p
1e78b 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f Op->p1<p->nCurso
1e78c 72 20 29 3b 0a 20 20 75 2e 62 6e 2e 70 43 20 3d r );. u.bn.pC =
1e78d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 p->apCsr[pOp->p
1e78e 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 1];. assert( u.
1e78f 62 6e 2e 70 43 21 3d 30 20 29 3b 0a 20 20 61 73 bn.pC!=0 );. as
1e790 73 65 72 74 28 20 70 49 6e 32 2d 3e 66 6c 61 67 sert( pIn2->flag
1e791 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a s & MEM_Blob );.
1e792 20 20 75 2e 62 6e 2e 70 43 72 73 72 20 3d 20 75 u.bn.pCrsr = u
1e793 2e 62 6e 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b .bn.pC->pCursor;
1e794 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 75 2e . if( ALWAYS(u.
1e795 62 6e 2e 70 43 72 73 72 21 3d 30 29 20 29 7b 0a bn.pCrsr!=0) ){.
1e796 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 6e assert( u.bn
1e797 2e 70 43 2d 3e 69 73 54 61 62 6c 65 3d 3d 30 20 .pC->isTable==0
1e798 29 3b 0a 20 20 20 20 72 63 20 3d 20 45 78 70 61 );. rc = Expa
1e799 6e 64 42 6c 6f 62 28 70 49 6e 32 29 3b 0a 20 20 ndBlob(pIn2);.
1e79a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1e79b 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 75 2e 62 _OK ){. u.b
1e79c 6e 2e 6e 4b 65 79 20 3d 20 70 49 6e 32 2d 3e 6e n.nKey = pIn2->n
1e79d 3b 0a 20 20 20 20 20 20 75 2e 62 6e 2e 7a 4b 65 ;. u.bn.zKe
1e79e 79 20 3d 20 70 49 6e 32 2d 3e 7a 3b 0a 20 20 20 y = pIn2->z;.
1e79f 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
1e7a0 74 72 65 65 49 6e 73 65 72 74 28 75 2e 62 6e 2e treeInsert(u.bn.
1e7a1 70 43 72 73 72 2c 20 75 2e 62 6e 2e 7a 4b 65 79 pCrsr, u.bn.zKey
1e7a2 2c 20 75 2e 62 6e 2e 6e 4b 65 79 2c 20 22 22 2c , u.bn.nKey, "",
1e7a3 20 30 2c 20 30 2c 20 70 4f 70 2d 3e 70 33 2c 0a 0, 0, pOp->p3,.
1e7a4 20 20 20 20 20 20 20 20 20 20 28 28 70 4f 70 2d ((pOp-
1e7a5 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f 55 53 45 >p5 & OPFLAG_USE
1e7a6 53 45 45 4b 52 45 53 55 4c 54 29 20 3f 20 75 2e SEEKRESULT) ? u.
1e7a7 62 6e 2e 70 43 2d 3e 73 65 65 6b 52 65 73 75 6c bn.pC->seekResul
1e7a8 74 20 3a 20 30 29 0a 20 20 20 20 20 20 29 3b 0a t : 0). );.
1e7a9 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e assert( u.
1e7aa 62 6e 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d bn.pC->deferredM
1e7ab 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 oveto==0 );.
1e7ac 20 20 75 2e 62 6e 2e 70 43 2d 3e 63 61 63 68 65 u.bn.pC->cache
1e7ad 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 Status = CACHE_S
1e7ae 54 41 4c 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a TALE;. }. }.
1e7af 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1e7b0 70 63 6f 64 65 3a 20 49 64 78 44 65 6c 65 74 65 pcode: IdxDelete
1e7b1 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a P1 P2 P3 * *.**
1e7b2 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 .** The content
1e7b3 6f 66 20 50 33 20 72 65 67 69 73 74 65 72 73 20 of P3 registers
1e7b4 73 74 61 72 74 69 6e 67 20 61 74 20 72 65 67 69 starting at regi
1e7b5 73 74 65 72 20 50 32 20 66 6f 72 6d 0a 2a 2a 20 ster P2 form.**
1e7b6 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 an unpacked inde
1e7b7 78 20 6b 65 79 2e 20 54 68 69 73 20 6f 70 63 6f x key. This opco
1e7b8 64 65 20 72 65 6d 6f 76 65 73 20 74 68 61 74 20 de removes that
1e7b9 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65 20 0a entry from the .
1e7ba 2a 2a 20 69 6e 64 65 78 20 6f 70 65 6e 65 64 20 ** index opened
1e7bb 62 79 20 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2f by cursor P1..*/
1e7bc 0a 63 61 73 65 20 4f 50 5f 49 64 78 44 65 6c 65 .case OP_IdxDele
1e7bd 74 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 te: {.#if 0 /*
1e7be 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1e7bf 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6f 20 moved into u.bo
1e7c0 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 */. VdbeCursor
1e7c1 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 *pC;. BtCursor
1e7c2 2a 70 43 72 73 72 3b 0a 20 20 69 6e 74 20 72 65 *pCrsr;. int re
1e7c3 73 3b 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 s;. UnpackedRec
1e7c4 6f 72 64 20 72 3b 0a 23 65 6e 64 69 66 20 2f 2a ord r;.#endif /*
1e7c5 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1e7c6 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6f moved into u.bo
1e7c7 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 */.. assert( p
1e7c8 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 61 73 Op->p3>0 );. as
1e7c9 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 sert( pOp->p2>0
1e7ca 26 26 20 70 4f 70 2d 3e 70 32 2b 70 4f 70 2d 3e && pOp->p2+pOp->
1e7cb 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 20 29 3b p3<=p->nMem+1 );
1e7cc 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1e7cd 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 p1>=0 && pOp->p1
1e7ce 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 <p->nCursor );.
1e7cf 20 75 2e 62 6f 2e 70 43 20 3d 20 70 2d 3e 61 70 u.bo.pC = p->ap
1e7d0 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Csr[pOp->p1];.
1e7d1 61 73 73 65 72 74 28 20 75 2e 62 6f 2e 70 43 21 assert( u.bo.pC!
1e7d2 3d 30 20 29 3b 0a 20 20 75 2e 62 6f 2e 70 43 72 =0 );. u.bo.pCr
1e7d3 73 72 20 3d 20 75 2e 62 6f 2e 70 43 2d 3e 70 43 sr = u.bo.pC->pC
1e7d4 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 41 4c 57 ursor;. if( ALW
1e7d5 41 59 53 28 75 2e 62 6f 2e 70 43 72 73 72 21 3d AYS(u.bo.pCrsr!=
1e7d6 30 29 20 29 7b 0a 20 20 20 20 75 2e 62 6f 2e 72 0) ){. u.bo.r
1e7d7 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 75 2e 62 6f .pKeyInfo = u.bo
1e7d8 2e 70 43 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 .pC->pKeyInfo;.
1e7d9 20 20 20 75 2e 62 6f 2e 72 2e 6e 46 69 65 6c 64 u.bo.r.nField
1e7da 20 3d 20 28 75 31 36 29 70 4f 70 2d 3e 70 33 3b = (u16)pOp->p3;
1e7db 0a 20 20 20 20 75 2e 62 6f 2e 72 2e 66 6c 61 67 . u.bo.r.flag
1e7dc 73 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 6f 2e s = 0;. u.bo.
1e7dd 72 2e 61 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 r.aMem = &p->aMe
1e7de 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 m[pOp->p2];.
1e7df 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1e7e0 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 eMovetoUnpacked(
1e7e1 75 2e 62 6f 2e 70 43 72 73 72 2c 20 26 75 2e 62 u.bo.pCrsr, &u.b
1e7e2 6f 2e 72 2c 20 30 2c 20 30 2c 20 26 75 2e 62 6f o.r, 0, 0, &u.bo
1e7e3 2e 72 65 73 29 3b 0a 20 20 20 20 69 66 28 20 72 .res);. if( r
1e7e4 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
1e7e5 75 2e 62 6f 2e 72 65 73 3d 3d 30 20 29 7b 0a 20 u.bo.res==0 ){.
1e7e6 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1e7e7 33 42 74 72 65 65 44 65 6c 65 74 65 28 75 2e 62 3BtreeDelete(u.b
1e7e8 6f 2e 70 43 72 73 72 29 3b 0a 20 20 20 20 7d 0a o.pCrsr);. }.
1e7e9 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 6f assert( u.bo
1e7ea 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 .pC->deferredMov
1e7eb 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 75 2e eto==0 );. u.
1e7ec 62 6f 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 bo.pC->cacheStat
1e7ed 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 us = CACHE_STALE
1e7ee 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1e7ef 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 ../* Opcode: Idx
1e7f0 52 6f 77 69 64 20 50 31 20 50 32 20 2a 20 2a 20 Rowid P1 P2 * *
1e7f1 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 69 6e *.**.** Write in
1e7f2 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 20 61 to register P2 a
1e7f3 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 n integer which
1e7f4 69 73 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 is the last entr
1e7f5 79 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 y in the record
1e7f6 61 74 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66 at.** the end of
1e7f7 20 74 68 65 20 69 6e 64 65 78 20 6b 65 79 20 70 the index key p
1e7f8 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 63 75 72 ointed to by cur
1e7f9 73 6f 72 20 50 31 2e 20 20 54 68 69 73 20 69 6e sor P1. This in
1e7fa 74 65 67 65 72 20 73 68 6f 75 6c 64 20 62 65 0a teger should be.
1e7fb 2a 2a 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 ** the rowid of
1e7fc 74 68 65 20 74 61 62 6c 65 20 65 6e 74 72 79 20 the table entry
1e7fd 74 6f 20 77 68 69 63 68 20 74 68 69 73 20 69 6e to which this in
1e7fe 64 65 78 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 dex entry points
1e7ff 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f ..**.** See also
1e800 3a 20 52 6f 77 69 64 2c 20 4d 61 6b 65 52 65 63 : Rowid, MakeRec
1e801 6f 72 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ord..*/.case OP_
1e802 49 64 78 52 6f 77 69 64 3a 20 7b 20 20 20 20 20 IdxRowid: {
1e803 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 /* out2
1e804 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 -prerelease */.#
1e805 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 if 0 /* local v
1e806 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1e807 6e 74 6f 20 75 2e 62 70 20 2a 2f 0a 20 20 42 74 nto u.bp */. Bt
1e808 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 Cursor *pCrsr;.
1e809 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b VdbeCursor *pC;
1e80a 0a 20 20 69 36 34 20 72 6f 77 69 64 3b 0a 23 65 . i64 rowid;.#e
1e80b 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1e80c 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1e80d 74 6f 20 75 2e 62 70 20 2a 2f 0a 0a 20 20 61 73 to u.bp */.. as
1e80e 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 sert( pOp->p1>=0
1e80f 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e && pOp->p1<p->n
1e810 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 70 Cursor );. u.bp
1e811 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 .pC = p->apCsr[p
1e812 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 Op->p1];. asser
1e813 74 28 20 75 2e 62 70 2e 70 43 21 3d 30 20 29 3b t( u.bp.pC!=0 );
1e814 0a 20 20 75 2e 62 70 2e 70 43 72 73 72 20 3d 20 . u.bp.pCrsr =
1e815 75 2e 62 70 2e 70 43 2d 3e 70 43 75 72 73 6f 72 u.bp.pC->pCursor
1e816 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 75 ;. if( ALWAYS(u
1e817 2e 62 70 2e 70 43 72 73 72 21 3d 30 29 20 29 7b .bp.pCrsr!=0) ){
1e818 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1e819 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 3VdbeCursorMovet
1e81a 6f 28 75 2e 62 70 2e 70 43 29 3b 0a 20 20 20 20 o(u.bp.pC);.
1e81b 69 66 28 20 4e 45 56 45 52 28 72 63 29 20 29 20 if( NEVER(rc) )
1e81c 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1e81d 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 61 73 73 o_error;. ass
1e81e 65 72 74 28 20 75 2e 62 70 2e 70 43 2d 3e 64 65 ert( u.bp.pC->de
1e81f 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 ferredMoveto==0
1e820 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 );. assert( u
1e821 2e 62 70 2e 70 43 2d 3e 69 73 54 61 62 6c 65 3d .bp.pC->isTable=
1e822 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 21 75 =0 );. if( !u
1e823 2e 62 70 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 .bp.pC->nullRow
1e824 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 ){. rc = sq
1e825 6c 69 74 65 33 56 64 62 65 49 64 78 52 6f 77 69 lite3VdbeIdxRowi
1e826 64 28 64 62 2c 20 75 2e 62 70 2e 70 43 72 73 72 d(db, u.bp.pCrsr
1e827 2c 20 26 75 2e 62 70 2e 72 6f 77 69 64 29 3b 0a , &u.bp.rowid);.
1e828 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
1e829 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1e82a 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 goto abort_du
1e82b 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 e_to_error;.
1e82c 20 20 7d 0a 20 20 20 20 20 20 4d 65 6d 53 65 74 }. MemSet
1e82d 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d TypeFlag(pOut, M
1e82e 45 4d 5f 49 6e 74 29 3b 0a 20 20 20 20 20 20 70 EM_Int);. p
1e82f 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 62 70 2e Out->u.i = u.bp.
1e830 72 6f 77 69 64 3b 0a 20 20 20 20 7d 0a 20 20 7d rowid;. }. }
1e831 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1e832 4f 70 63 6f 64 65 3a 20 49 64 78 47 45 20 50 31 Opcode: IdxGE P1
1e833 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a P2 P3 P4 P5.**.
1e834 2a 2a 20 54 68 65 20 50 34 20 72 65 67 69 73 74 ** The P4 regist
1e835 65 72 20 76 61 6c 75 65 73 20 62 65 67 69 6e 6e er values beginn
1e836 69 6e 67 20 77 69 74 68 20 50 33 20 66 6f 72 6d ing with P3 form
1e837 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 an unpacked ind
1e838 65 78 20 0a 2a 2a 20 6b 65 79 20 74 68 61 74 20 ex .** key that
1e839 6f 6d 69 74 73 20 74 68 65 20 52 4f 57 49 44 2e omits the ROWID.
1e83a 20 20 43 6f 6d 70 61 72 65 20 74 68 69 73 20 6b Compare this k
1e83b 65 79 20 76 61 6c 75 65 20 61 67 61 69 6e 73 74 ey value against
1e83c 20 74 68 65 20 69 6e 64 65 78 20 0a 2a 2a 20 74 the index .** t
1e83d 68 61 74 20 50 31 20 69 73 20 63 75 72 72 65 6e hat P1 is curren
1e83e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2c tly pointing to,
1e83f 20 69 67 6e 6f 72 69 6e 67 20 74 68 65 20 52 4f ignoring the RO
1e840 57 49 44 20 6f 6e 20 74 68 65 20 50 31 20 69 6e WID on the P1 in
1e841 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 dex..**.** If th
1e842 65 20 50 31 20 69 6e 64 65 78 20 65 6e 74 72 79 e P1 index entry
1e843 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e is greater than
1e844 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 or equal to the
1e845 20 6b 65 79 20 76 61 6c 75 65 0a 2a 2a 20 74 68 key value.** th
1e846 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 20 20 en jump to P2.
1e847 4f 74 68 65 72 77 69 73 65 20 66 61 6c 6c 20 74 Otherwise fall t
1e848 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 hrough to the ne
1e849 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a xt instruction..
1e84a 2a 2a 0a 2a 2a 20 49 66 20 50 35 20 69 73 20 6e **.** If P5 is n
1e84b 6f 6e 2d 7a 65 72 6f 20 74 68 65 6e 20 74 68 65 on-zero then the
1e84c 20 6b 65 79 20 76 61 6c 75 65 20 69 73 20 69 6e key value is in
1e84d 63 72 65 61 73 65 64 20 62 79 20 61 6e 20 65 70 creased by an ep
1e84e 73 69 6c 6f 6e 20 0a 2a 2a 20 70 72 69 6f 72 20 silon .** prior
1e84f 74 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f to the compariso
1e850 6e 2e 20 20 54 68 69 73 20 6d 61 6b 65 20 74 68 n. This make th
1e851 65 20 6f 70 63 6f 64 65 20 77 6f 72 6b 20 6c 69 e opcode work li
1e852 6b 65 20 49 64 78 47 54 20 65 78 63 65 70 74 0a ke IdxGT except.
1e853 2a 2a 20 74 68 61 74 20 69 66 20 74 68 65 20 6b ** that if the k
1e854 65 79 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72 ey from register
1e855 20 50 33 20 69 73 20 61 20 70 72 65 66 69 78 20 P3 is a prefix
1e856 6f 66 20 74 68 65 20 6b 65 79 20 69 6e 20 74 68 of the key in th
1e857 65 20 63 75 72 73 6f 72 2c 0a 2a 2a 20 74 68 65 e cursor,.** the
1e858 20 72 65 73 75 6c 74 20 69 73 20 66 61 6c 73 65 result is false
1e859 20 77 68 65 72 65 61 73 20 69 74 20 77 6f 75 6c whereas it woul
1e85a 64 20 62 65 20 74 72 75 65 20 77 69 74 68 20 49 d be true with I
1e85b 64 78 47 54 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f dxGT..*/./* Opco
1e85c 64 65 3a 20 49 64 78 4c 54 20 50 31 20 50 32 20 de: IdxLT P1 P2
1e85d 50 33 20 2a 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 P3 * P5.**.** Th
1e85e 65 20 50 34 20 72 65 67 69 73 74 65 72 20 76 61 e P4 register va
1e85f 6c 75 65 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 lues beginning w
1e860 69 74 68 20 50 33 20 66 6f 72 6d 20 61 6e 20 75 ith P3 form an u
1e861 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 0a 2a npacked index .*
1e862 2a 20 6b 65 79 20 74 68 61 74 20 6f 6d 69 74 73 * key that omits
1e863 20 74 68 65 20 52 4f 57 49 44 2e 20 20 43 6f 6d the ROWID. Com
1e864 70 61 72 65 20 74 68 69 73 20 6b 65 79 20 76 61 pare this key va
1e865 6c 75 65 20 61 67 61 69 6e 73 74 20 74 68 65 20 lue against the
1e866 69 6e 64 65 78 20 0a 2a 2a 20 74 68 61 74 20 50 index .** that P
1e867 31 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 1 is currently p
1e868 6f 69 6e 74 69 6e 67 20 74 6f 2c 20 69 67 6e 6f ointing to, igno
1e869 72 69 6e 67 20 74 68 65 20 52 4f 57 49 44 20 6f ring the ROWID o
1e86a 6e 20 74 68 65 20 50 31 20 69 6e 64 65 78 2e 0a n the P1 index..
1e86b 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 31 20 **.** If the P1
1e86c 69 6e 64 65 78 20 65 6e 74 72 79 20 69 73 20 6c index entry is l
1e86d 65 73 73 20 74 68 61 6e 20 74 68 65 20 6b 65 79 ess than the key
1e86e 20 76 61 6c 75 65 20 74 68 65 6e 20 6a 75 6d 70 value then jump
1e86f 20 74 6f 20 50 32 2e 0a 2a 2a 20 4f 74 68 65 72 to P2..** Other
1e870 77 69 73 65 20 66 61 6c 6c 20 74 68 72 6f 75 67 wise fall throug
1e871 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 6e h to the next in
1e872 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a struction..**.**
1e873 20 49 66 20 50 35 20 69 73 20 6e 6f 6e 2d 7a 65 If P5 is non-ze
1e874 72 6f 20 74 68 65 6e 20 74 68 65 20 6b 65 79 20 ro then the key
1e875 76 61 6c 75 65 20 69 73 20 69 6e 63 72 65 61 73 value is increas
1e876 65 64 20 62 79 20 61 6e 20 65 70 73 69 6c 6f 6e ed by an epsilon
1e877 20 70 72 69 6f 72 20 0a 2a 2a 20 74 6f 20 74 68 prior .** to th
1e878 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20 54 e comparison. T
1e879 68 69 73 20 6d 61 6b 65 73 20 74 68 65 20 6f 70 his makes the op
1e87a 63 6f 64 65 20 77 6f 72 6b 20 6c 69 6b 65 20 49 code work like I
1e87b 64 78 4c 45 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 dxLE..*/.case OP
1e87c 5f 49 64 78 4c 54 3a 20 20 20 20 20 20 20 20 20 _IdxLT:
1e87d 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f /* jump, in3 */
1e87e 0a 63 61 73 65 20 4f 50 5f 49 64 78 47 45 3a 20 .case OP_IdxGE:
1e87f 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 { /* jump
1e880 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20 20 , in3 */.#if 0
1e881 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1e882 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1e883 62 71 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 bq */. VdbeCurs
1e884 6f 72 20 2a 70 43 3b 0a 20 20 69 6e 74 20 72 65 or *pC;. int re
1e885 73 3b 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 s;. UnpackedRec
1e886 6f 72 64 20 72 3b 0a 23 65 6e 64 69 66 20 2f 2a ord r;.#endif /*
1e887 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1e888 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 71 moved into u.bq
1e889 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 */.. assert( p
1e88a 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 Op->p1>=0 && pOp
1e88b 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 ->p1<p->nCursor
1e88c 29 3b 0a 20 20 75 2e 62 71 2e 70 43 20 3d 20 70 );. u.bq.pC = p
1e88d 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d ->apCsr[pOp->p1]
1e88e 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 71 ;. assert( u.bq
1e88f 2e 70 43 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 .pC!=0 );. if(
1e890 41 4c 57 41 59 53 28 75 2e 62 71 2e 70 43 2d 3e ALWAYS(u.bq.pC->
1e891 70 43 75 72 73 6f 72 21 3d 30 29 20 29 7b 0a 20 pCursor!=0) ){.
1e892 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 71 2e assert( u.bq.
1e893 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 pC->deferredMove
1e894 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 to==0 );. ass
1e895 65 72 74 28 20 70 4f 70 2d 3e 70 35 3d 3d 30 20 ert( pOp->p5==0
1e896 7c 7c 20 70 4f 70 2d 3e 70 35 3d 3d 31 20 29 3b || pOp->p5==1 );
1e897 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 . assert( pOp
1e898 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 ->p4type==P4_INT
1e899 33 32 20 29 3b 0a 20 20 20 20 75 2e 62 71 2e 72 32 );. u.bq.r
1e89a 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 75 2e 62 71 .pKeyInfo = u.bq
1e89b 2e 70 43 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 .pC->pKeyInfo;.
1e89c 20 20 20 75 2e 62 71 2e 72 2e 6e 46 69 65 6c 64 u.bq.r.nField
1e89d 20 3d 20 28 75 31 36 29 70 4f 70 2d 3e 70 34 2e = (u16)pOp->p4.
1e89e 69 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e i;. if( pOp->
1e89f 70 35 20 29 7b 0a 20 20 20 20 20 20 75 2e 62 71 p5 ){. u.bq
1e8a0 2e 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 .r.flags = UNPAC
1e8a1 4b 45 44 5f 49 4e 43 52 4b 45 59 20 7c 20 55 4e KED_INCRKEY | UN
1e8a2 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f PACKED_IGNORE_RO
1e8a3 57 49 44 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a WID;. }else{.
1e8a4 20 20 20 20 20 20 75 2e 62 71 2e 72 2e 66 6c 61 u.bq.r.fla
1e8a5 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f 49 47 gs = UNPACKED_IG
1e8a6 4e 4f 52 45 5f 52 4f 57 49 44 3b 0a 20 20 20 20 NORE_ROWID;.
1e8a7 7d 0a 20 20 20 20 75 2e 62 71 2e 72 2e 61 4d 65 }. u.bq.r.aMe
1e8a8 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 m = &p->aMem[pOp
1e8a9 2d 3e 70 33 5d 3b 0a 20 20 20 20 72 63 20 3d 20 ->p3];. rc =
1e8aa 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 4b 65 sqlite3VdbeIdxKe
1e8ab 79 43 6f 6d 70 61 72 65 28 75 2e 62 71 2e 70 43 yCompare(u.bq.pC
1e8ac 2c 20 26 75 2e 62 71 2e 72 2c 20 26 75 2e 62 71 , &u.bq.r, &u.bq
1e8ad 2e 72 65 73 29 3b 0a 20 20 20 20 69 66 28 20 70 .res);. if( p
1e8ae 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 Op->opcode==OP_I
1e8af 64 78 4c 54 20 29 7b 0a 20 20 20 20 20 20 75 2e dxLT ){. u.
1e8b0 62 71 2e 72 65 73 20 3d 20 2d 75 2e 62 71 2e 72 bq.res = -u.bq.r
1e8b1 65 73 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 es;. }else{.
1e8b2 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 assert( pOp
1e8b3 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 64 78 ->opcode==OP_Idx
1e8b4 47 45 20 29 3b 0a 20 20 20 20 20 20 75 2e 62 71 GE );. u.bq
1e8b5 2e 72 65 73 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 .res++;. }.
1e8b6 20 20 69 66 28 20 75 2e 62 71 2e 72 65 73 3e 30 if( u.bq.res>0
1e8b7 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 ){. pc = p
1e8b8 4f 70 2d 3e 70 32 20 2d 20 31 20 3b 0a 20 20 20 Op->p2 - 1 ;.
1e8b9 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a }. }. break;.
1e8ba 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 65 }../* Opcode: De
1e8bb 73 74 72 6f 79 20 50 31 20 50 32 20 50 33 20 2a stroy P1 P2 P3 *
1e8bc 20 2a 0a 2a 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 *.**.** Delete
1e8bd 61 6e 20 65 6e 74 69 72 65 20 64 61 74 61 62 61 an entire databa
1e8be 73 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 se table or inde
1e8bf 78 20 77 68 6f 73 65 20 72 6f 6f 74 20 70 61 67 x whose root pag
1e8c0 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 e in the databas
1e8c1 65 0a 2a 2a 20 66 69 6c 65 20 69 73 20 67 69 76 e.** file is giv
1e8c2 65 6e 20 62 79 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 en by P1..**.**
1e8c3 54 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 The table being
1e8c4 64 65 73 74 72 6f 79 65 64 20 69 73 20 69 6e 20 destroyed is in
1e8c5 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 the main databas
1e8c6 65 20 66 69 6c 65 20 69 66 20 50 33 3d 3d 30 2e e file if P3==0.
1e8c7 20 20 49 66 0a 2a 2a 20 50 33 3d 3d 31 20 74 68 If.** P3==1 th
1e8c8 65 6e 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 en the table to
1e8c9 62 65 20 63 6c 65 61 72 20 69 73 20 69 6e 20 74 be clear is in t
1e8ca 68 65 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 he auxiliary dat
1e8cb 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 74 68 abase file.** th
1e8cc 61 74 20 69 73 20 75 73 65 64 20 74 6f 20 73 74 at is used to st
1e8cd 6f 72 65 20 74 61 62 6c 65 73 20 63 72 65 61 74 ore tables creat
1e8ce 65 20 75 73 69 6e 67 20 43 52 45 41 54 45 20 54 e using CREATE T
1e8cf 45 4d 50 4f 52 41 52 59 20 54 41 42 4c 45 2e 0a EMPORARY TABLE..
1e8d0 2a 2a 0a 2a 2a 20 49 66 20 41 55 54 4f 56 41 43 **.** If AUTOVAC
1e8d1 55 55 4d 20 69 73 20 65 6e 61 62 6c 65 64 20 74 UUM is enabled t
1e8d2 68 65 6e 20 69 74 20 69 73 20 70 6f 73 73 69 62 hen it is possib
1e8d3 6c 65 20 74 68 61 74 20 61 6e 6f 74 68 65 72 20 le that another
1e8d4 72 6f 6f 74 20 70 61 67 65 0a 2a 2a 20 6d 69 67 root page.** mig
1e8d5 68 74 20 62 65 20 6d 6f 76 65 64 20 69 6e 74 6f ht be moved into
1e8d6 20 74 68 65 20 6e 65 77 6c 79 20 64 65 6c 65 74 the newly delet
1e8d7 65 64 20 72 6f 6f 74 20 70 61 67 65 20 69 6e 20 ed root page in
1e8d8 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20 61 6c order to keep al
1e8d9 6c 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 73 20 l.** root pages
1e8da 63 6f 6e 74 69 67 75 6f 75 73 20 61 74 20 74 68 contiguous at th
1e8db 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 e beginning of t
1e8dc 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 he database. Th
1e8dd 65 20 66 6f 72 6d 65 72 0a 2a 2a 20 76 61 6c 75 e former.** valu
1e8de 65 20 6f 66 20 74 68 65 20 72 6f 6f 74 20 70 61 e of the root pa
1e8df 67 65 20 74 68 61 74 20 6d 6f 76 65 64 20 2d 20 ge that moved -
1e8e0 69 74 73 20 76 61 6c 75 65 20 62 65 66 6f 72 65 its value before
1e8e1 20 74 68 65 20 6d 6f 76 65 20 6f 63 63 75 72 72 the move occurr
1e8e2 65 64 20 2d 0a 2a 2a 20 69 73 20 73 74 6f 72 65 ed -.** is store
1e8e3 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 d in register P2
1e8e4 2e 20 20 49 66 20 6e 6f 20 70 61 67 65 20 0a 2a . If no page .*
1e8e5 2a 20 6d 6f 76 65 6d 65 6e 74 20 77 61 73 20 72 * movement was r
1e8e6 65 71 75 69 72 65 64 20 28 62 65 63 61 75 73 65 equired (because
1e8e7 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 the table being
1e8e8 20 64 72 6f 70 70 65 64 20 77 61 73 20 61 6c 72 dropped was alr
1e8e9 65 61 64 79 20 0a 2a 2a 20 74 68 65 20 6c 61 73 eady .** the las
1e8ea 74 20 6f 6e 65 20 69 6e 20 74 68 65 20 64 61 74 t one in the dat
1e8eb 61 62 61 73 65 29 20 74 68 65 6e 20 61 20 7a 65 abase) then a ze
1e8ec 72 6f 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 ro is stored in
1e8ed 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 20 register P2..**
1e8ee 49 66 20 41 55 54 4f 56 41 43 55 55 4d 20 69 73 If AUTOVACUUM is
1e8ef 20 64 69 73 61 62 6c 65 64 20 74 68 65 6e 20 61 disabled then a
1e8f0 20 7a 65 72 6f 20 69 73 20 73 74 6f 72 65 64 20 zero is stored
1e8f1 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a in register P2..
1e8f2 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 **.** See also:
1e8f3 43 6c 65 61 72 0a 2a 2f 0a 63 61 73 65 20 4f 50 Clear.*/.case OP
1e8f4 5f 44 65 73 74 72 6f 79 3a 20 7b 20 20 20 20 20 _Destroy: {
1e8f5 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 /* out2-prerelea
1e8f6 73 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 se */.#if 0 /*
1e8f7 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1e8f8 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 72 20 moved into u.br
1e8f9 2a 2f 0a 20 20 69 6e 74 20 69 4d 6f 76 65 64 3b */. int iMoved;
1e8fa 0a 20 20 69 6e 74 20 69 43 6e 74 3b 0a 20 20 56 . int iCnt;. V
1e8fb 64 62 65 20 2a 70 56 64 62 65 3b 0a 20 20 69 6e dbe *pVdbe;. in
1e8fc 74 20 69 44 62 3b 0a 23 65 6e 64 69 66 20 2f 2a t iDb;.#endif /*
1e8fd 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1e8fe 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 72 moved into u.br
1e8ff 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 */.#ifndef SQLI
1e900 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
1e901 41 42 4c 45 0a 20 20 75 2e 62 72 2e 69 43 6e 74 ABLE. u.br.iCnt
1e902 20 3d 20 30 3b 0a 20 20 66 6f 72 28 75 2e 62 72 = 0;. for(u.br
1e903 2e 70 56 64 62 65 3d 64 62 2d 3e 70 56 64 62 65 .pVdbe=db->pVdbe
1e904 3b 20 75 2e 62 72 2e 70 56 64 62 65 3b 20 75 2e ; u.br.pVdbe; u.
1e905 62 72 2e 70 56 64 62 65 20 3d 20 75 2e 62 72 2e br.pVdbe = u.br.
1e906 70 56 64 62 65 2d 3e 70 4e 65 78 74 29 7b 0a 20 pVdbe->pNext){.
1e907 20 20 20 69 66 28 20 75 2e 62 72 2e 70 56 64 62 if( u.br.pVdb
1e908 65 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d e->magic==VDBE_M
1e909 41 47 49 43 5f 52 55 4e 20 26 26 20 75 2e 62 72 AGIC_RUN && u.br
1e90a 2e 70 56 64 62 65 2d 3e 69 6e 56 74 61 62 4d 65 .pVdbe->inVtabMe
1e90b 74 68 6f 64 3c 32 20 26 26 20 75 2e 62 72 2e 70 thod<2 && u.br.p
1e90c 56 64 62 65 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 Vdbe->pc>=0 ){.
1e90d 20 20 20 20 20 75 2e 62 72 2e 69 43 6e 74 2b 2b u.br.iCnt++
1e90e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 ;. }. }.#els
1e90f 65 0a 20 20 75 2e 62 72 2e 69 43 6e 74 20 3d 20 e. u.br.iCnt =
1e910 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e db->activeVdbeCn
1e911 74 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 t;.#endif. if(
1e912 75 2e 62 72 2e 69 43 6e 74 3e 31 20 29 7b 0a 20 u.br.iCnt>1 ){.
1e913 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4c rc = SQLITE_L
1e914 4f 43 4b 45 44 3b 0a 20 20 20 20 70 2d 3e 65 72 OCKED;. p->er
1e915 72 6f 72 41 63 74 69 6f 6e 20 3d 20 4f 45 5f 41 rorAction = OE_A
1e916 62 6f 72 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 bort;. }else{.
1e917 20 20 20 75 2e 62 72 2e 69 44 62 20 3d 20 70 4f u.br.iDb = pO
1e918 70 2d 3e 70 33 3b 0a 20 20 20 20 61 73 73 65 72 p->p3;. asser
1e919 74 28 20 75 2e 62 72 2e 69 43 6e 74 3d 3d 31 20 t( u.br.iCnt==1
1e91a 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 );. assert( (
1e91b 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 p->btreeMask & (
1e91c 31 3c 3c 75 2e 62 72 2e 69 44 62 29 29 21 3d 30 1<<u.br.iDb))!=0
1e91d 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c );. rc = sql
1e91e 69 74 65 33 42 74 72 65 65 44 72 6f 70 54 61 62 ite3BtreeDropTab
1e91f 6c 65 28 64 62 2d 3e 61 44 62 5b 75 2e 62 72 2e le(db->aDb[u.br.
1e920 69 44 62 5d 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 iDb].pBt, pOp->p
1e921 31 2c 20 26 75 2e 62 72 2e 69 4d 6f 76 65 64 29 1, &u.br.iMoved)
1e922 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 ;. MemSetType
1e923 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 Flag(pOut, MEM_I
1e924 6e 74 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 75 nt);. pOut->u
1e925 2e 69 20 3d 20 75 2e 62 72 2e 69 4d 6f 76 65 64 .i = u.br.iMoved
1e926 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
1e927 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
1e928 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
1e929 49 54 45 5f 4f 4b 20 26 26 20 75 2e 62 72 2e 69 ITE_OK && u.br.i
1e92a 4d 6f 76 65 64 21 3d 30 20 29 7b 0a 20 20 20 20 Moved!=0 ){.
1e92b 20 20 73 71 6c 69 74 65 33 52 6f 6f 74 50 61 67 sqlite3RootPag
1e92c 65 4d 6f 76 65 64 28 26 64 62 2d 3e 61 44 62 5b eMoved(&db->aDb[
1e92d 75 2e 62 72 2e 69 44 62 5d 2c 20 75 2e 62 72 2e u.br.iDb], u.br.
1e92e 69 4d 6f 76 65 64 2c 20 70 4f 70 2d 3e 70 31 29 iMoved, pOp->p1)
1e92f 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 ;. }.#endif.
1e930 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f }. break;.}../
1e931 2a 20 4f 70 63 6f 64 65 3a 20 43 6c 65 61 72 20 * Opcode: Clear
1e932 50 31 20 50 32 20 50 33 0a 2a 2a 0a 2a 2a 20 44 P1 P2 P3.**.** D
1e933 65 6c 65 74 65 20 61 6c 6c 20 63 6f 6e 74 65 6e elete all conten
1e934 74 73 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ts of the databa
1e935 73 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 se table or inde
1e936 78 20 77 68 6f 73 65 20 72 6f 6f 74 20 70 61 67 x whose root pag
1e937 65 0a 2a 2a 20 69 6e 20 74 68 65 20 64 61 74 61 e.** in the data
1e938 62 61 73 65 20 66 69 6c 65 20 69 73 20 67 69 76 base file is giv
1e939 65 6e 20 62 79 20 50 31 2e 20 20 42 75 74 2c 20 en by P1. But,
1e93a 75 6e 6c 69 6b 65 20 44 65 73 74 72 6f 79 2c 20 unlike Destroy,
1e93b 64 6f 20 6e 6f 74 0a 2a 2a 20 72 65 6d 6f 76 65 do not.** remove
1e93c 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e the table or in
1e93d 64 65 78 20 66 72 6f 6d 20 74 68 65 20 64 61 74 dex from the dat
1e93e 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a abase file..**.*
1e93f 2a 20 54 68 65 20 74 61 62 6c 65 20 62 65 69 6e * The table bein
1e940 67 20 63 6c 65 61 72 20 69 73 20 69 6e 20 74 68 g clear is in th
1e941 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 e main database
1e942 66 69 6c 65 20 69 66 20 50 32 3d 3d 30 2e 20 20 file if P2==0.
1e943 49 66 0a 2a 2a 20 50 32 3d 3d 31 20 74 68 65 6e If.** P2==1 then
1e944 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 the table to be
1e945 20 63 6c 65 61 72 20 69 73 20 69 6e 20 74 68 65 clear is in the
1e946 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 auxiliary datab
1e947 61 73 65 20 66 69 6c 65 0a 2a 2a 20 74 68 61 74 ase file.** that
1e948 20 69 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72 is used to stor
1e949 65 20 74 61 62 6c 65 73 20 63 72 65 61 74 65 20 e tables create
1e94a 75 73 69 6e 67 20 43 52 45 41 54 45 20 54 45 4d using CREATE TEM
1e94b 50 4f 52 41 52 59 20 54 41 42 4c 45 2e 0a 2a 2a PORARY TABLE..**
1e94c 0a 2a 2a 20 49 66 20 74 68 65 20 50 33 20 76 61 .** If the P3 va
1e94d 6c 75 65 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c lue is non-zero,
1e94e 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20 then the table
1e94f 72 65 66 65 72 72 65 64 20 74 6f 20 6d 75 73 74 referred to must
1e950 20 62 65 20 61 6e 0a 2a 2a 20 69 6e 74 6b 65 79 be an.** intkey
1e951 20 74 61 62 6c 65 20 28 61 6e 20 53 51 4c 20 74 table (an SQL t
1e952 61 62 6c 65 2c 20 6e 6f 74 20 61 6e 20 69 6e 64 able, not an ind
1e953 65 78 29 2e 20 49 6e 20 74 68 69 73 20 63 61 73 ex). In this cas
1e954 65 20 74 68 65 20 72 6f 77 20 63 68 61 6e 67 65 e the row change
1e955 20 0a 2a 2a 20 63 6f 75 6e 74 20 69 73 20 69 6e .** count is in
1e956 63 72 65 6d 65 6e 74 65 64 20 62 79 20 74 68 65 cremented by the
1e957 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 number of rows
1e958 69 6e 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 in the table bei
1e959 6e 67 20 63 6c 65 61 72 65 64 2e 20 0a 2a 2a 20 ng cleared. .**
1e95a 49 66 20 50 33 20 69 73 20 67 72 65 61 74 65 72 If P3 is greater
1e95b 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e than zero, then
1e95c 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72 65 the value store
1e95d 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 d in register P3
1e95e 20 69 73 0a 2a 2a 20 61 6c 73 6f 20 69 6e 63 72 is.** also incr
1e95f 65 6d 65 6e 74 65 64 20 62 79 20 74 68 65 20 6e emented by the n
1e960 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e umber of rows in
1e961 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 the table being
1e962 20 63 6c 65 61 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 cleared..**.**
1e963 53 65 65 20 61 6c 73 6f 3a 20 44 65 73 74 72 6f See also: Destro
1e964 79 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6c 65 y.*/.case OP_Cle
1e965 61 72 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 ar: {.#if 0 /*
1e966 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1e967 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 73 20 moved into u.bs
1e968 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67 65 */. int nChange
1e969 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 ;.#endif /* loca
1e96a 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1e96b 64 20 69 6e 74 6f 20 75 2e 62 73 20 2a 2f 0a 0a d into u.bs */..
1e96c 20 20 75 2e 62 73 2e 6e 43 68 61 6e 67 65 20 3d u.bs.nChange =
1e96d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 0;. assert( (p
1e96e 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 ->btreeMask & (1
1e96f 3c 3c 70 4f 70 2d 3e 70 32 29 29 21 3d 30 20 29 <<pOp->p2))!=0 )
1e970 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
1e971 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65 28 BtreeClearTable(
1e972 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 70 . db->aDb[p
1e973 4f 70 2d 3e 70 32 5d 2e 70 42 74 2c 20 70 4f 70 Op->p2].pBt, pOp
1e974 2d 3e 70 31 2c 20 28 70 4f 70 2d 3e 70 33 20 3f ->p1, (pOp->p3 ?
1e975 20 26 75 2e 62 73 2e 6e 43 68 61 6e 67 65 20 3a &u.bs.nChange :
1e976 20 30 29 0a 20 20 29 3b 0a 20 20 69 66 28 20 70 0). );. if( p
1e977 4f 70 2d 3e 70 33 20 29 7b 0a 20 20 20 20 70 2d Op->p3 ){. p-
1e978 3e 6e 43 68 61 6e 67 65 20 2b 3d 20 75 2e 62 73 >nChange += u.bs
1e979 2e 6e 43 68 61 6e 67 65 3b 0a 20 20 20 20 69 66 .nChange;. if
1e97a 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 7b 0a 20 ( pOp->p3>0 ){.
1e97b 20 20 20 20 20 70 2d 3e 61 4d 65 6d 5b 70 4f 70 p->aMem[pOp
1e97c 2d 3e 70 33 5d 2e 75 2e 69 20 2b 3d 20 75 2e 62 ->p3].u.i += u.b
1e97d 73 2e 6e 43 68 61 6e 67 65 3b 0a 20 20 20 20 7d s.nChange;. }
1e97e 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1e97f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 72 65 61 ./* Opcode: Crea
1e980 74 65 54 61 62 6c 65 20 50 31 20 50 32 20 2a 20 teTable P1 P2 *
1e981 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 * *.**.** Alloca
1e982 74 65 20 61 20 6e 65 77 20 74 61 62 6c 65 20 69 te a new table i
1e983 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 n the main datab
1e984 61 73 65 20 66 69 6c 65 20 69 66 20 50 31 3d 3d ase file if P1==
1e985 30 20 6f 72 20 69 6e 20 74 68 65 0a 2a 2a 20 61 0 or in the.** a
1e986 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 uxiliary databas
1e987 65 20 66 69 6c 65 20 69 66 20 50 31 3d 3d 31 20 e file if P1==1
1e988 6f 72 20 69 6e 20 61 6e 20 61 74 74 61 63 68 65 or in an attache
1e989 64 20 64 61 74 61 62 61 73 65 20 69 66 0a 2a 2a d database if.**
1e98a 20 50 31 3e 31 2e 20 20 57 72 69 74 65 20 74 68 P1>1. Write th
1e98b 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 e root page numb
1e98c 65 72 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61 er of the new ta
1e98d 62 6c 65 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 ble into.** regi
1e98e 73 74 65 72 20 50 32 0a 2a 2a 0a 2a 2a 20 54 68 ster P2.**.** Th
1e98f 65 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 e difference bet
1e990 77 65 65 6e 20 61 20 74 61 62 6c 65 20 61 6e 64 ween a table and
1e991 20 61 6e 20 69 6e 64 65 78 20 69 73 20 74 68 69 an index is thi
1e992 73 3a 20 20 41 20 74 61 62 6c 65 20 6d 75 73 74 s: A table must
1e993 0a 2a 2a 20 68 61 76 65 20 61 20 34 2d 62 79 74 .** have a 4-byt
1e994 65 20 69 6e 74 65 67 65 72 20 6b 65 79 20 61 6e e integer key an
1e995 64 20 63 61 6e 20 68 61 76 65 20 61 72 62 69 74 d can have arbit
1e996 72 61 72 79 20 64 61 74 61 2e 20 20 41 6e 20 69 rary data. An i
1e997 6e 64 65 78 0a 2a 2a 20 68 61 73 20 61 6e 20 61 ndex.** has an a
1e998 72 62 69 74 72 61 72 79 20 6b 65 79 20 62 75 74 rbitrary key but
1e999 20 6e 6f 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 no data..**.**
1e99a 53 65 65 20 61 6c 73 6f 3a 20 43 72 65 61 74 65 See also: Create
1e99b 49 6e 64 65 78 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f Index.*/./* Opco
1e99c 64 65 3a 20 43 72 65 61 74 65 49 6e 64 65 78 20 de: CreateIndex
1e99d 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a P1 P2 * * *.**.*
1e99e 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 * Allocate a new
1e99f 20 69 6e 64 65 78 20 69 6e 20 74 68 65 20 6d 61 index in the ma
1e9a0 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 in database file
1e9a1 20 69 66 20 50 31 3d 3d 30 20 6f 72 20 69 6e 20 if P1==0 or in
1e9a2 74 68 65 0a 2a 2a 20 61 75 78 69 6c 69 61 72 79 the.** auxiliary
1e9a3 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 database file i
1e9a4 66 20 50 31 3d 3d 31 20 6f 72 20 69 6e 20 61 6e f P1==1 or in an
1e9a5 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 attached databa
1e9a6 73 65 20 69 66 0a 2a 2a 20 50 31 3e 31 2e 20 20 se if.** P1>1.
1e9a7 57 72 69 74 65 20 74 68 65 20 72 6f 6f 74 20 70 Write the root p
1e9a8 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 age number of th
1e9a9 65 20 6e 65 77 20 74 61 62 6c 65 20 69 6e 74 6f e new table into
1e9aa 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 32 2e .** register P2.
1e9ab 0a 2a 2a 0a 2a 2a 20 53 65 65 20 64 6f 63 75 6d .**.** See docum
1e9ac 65 6e 74 61 74 69 6f 6e 20 6f 6e 20 4f 50 5f 43 entation on OP_C
1e9ad 72 65 61 74 65 54 61 62 6c 65 20 66 6f 72 20 61 reateTable for a
1e9ae 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d dditional inform
1e9af 61 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f ation..*/.case O
1e9b0 50 5f 43 72 65 61 74 65 49 6e 64 65 78 3a 20 20 P_CreateIndex:
1e9b1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 /* out
1e9b2 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 2-prerelease */.
1e9b3 63 61 73 65 20 4f 50 5f 43 72 65 61 74 65 54 61 case OP_CreateTa
1e9b4 62 6c 65 3a 20 7b 20 20 20 20 20 20 20 20 20 20 ble: {
1e9b5 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 /* out2-prerelea
1e9b6 73 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 se */.#if 0 /*
1e9b7 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1e9b8 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 74 20 moved into u.bt
1e9b9 2a 2f 0a 20 20 69 6e 74 20 70 67 6e 6f 3b 0a 20 */. int pgno;.
1e9ba 20 69 6e 74 20 66 6c 61 67 73 3b 0a 20 20 44 62 int flags;. Db
1e9bb 20 2a 70 44 62 3b 0a 23 65 6e 64 69 66 20 2f 2a *pDb;.#endif /*
1e9bc 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1e9bd 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 74 moved into u.bt
1e9be 20 2a 2f 0a 0a 20 20 75 2e 62 74 2e 70 67 6e 6f */.. u.bt.pgno
1e9bf 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 = 0;. assert(
1e9c0 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f pOp->p1>=0 && pO
1e9c1 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b p->p1<db->nDb );
1e9c2 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 . assert( (p->b
1e9c3 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 treeMask & (1<<p
1e9c4 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20 Op->p1))!=0 );.
1e9c5 20 75 2e 62 74 2e 70 44 62 20 3d 20 26 64 62 2d u.bt.pDb = &db-
1e9c6 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 >aDb[pOp->p1];.
1e9c7 20 61 73 73 65 72 74 28 20 75 2e 62 74 2e 70 44 assert( u.bt.pD
1e9c8 62 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 69 b->pBt!=0 );. i
1e9c9 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d f( pOp->opcode==
1e9ca 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 20 29 OP_CreateTable )
1e9cb 7b 0a 20 20 20 20 2f 2a 20 75 2e 62 74 2e 66 6c {. /* u.bt.fl
1e9cc 61 67 73 20 3d 20 42 54 52 45 45 5f 49 4e 54 4b ags = BTREE_INTK
1e9cd 45 59 3b 20 2a 2f 0a 20 20 20 20 75 2e 62 74 2e EY; */. u.bt.
1e9ce 66 6c 61 67 73 20 3d 20 42 54 52 45 45 5f 4c 45 flags = BTREE_LE
1e9cf 41 46 44 41 54 41 7c 42 54 52 45 45 5f 49 4e 54 AFDATA|BTREE_INT
1e9d0 4b 45 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 KEY;. }else{.
1e9d1 20 20 75 2e 62 74 2e 66 6c 61 67 73 20 3d 20 42 u.bt.flags = B
1e9d2 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 3b 0a 20 TREE_ZERODATA;.
1e9d3 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 }. rc = sqlite
1e9d4 33 42 74 72 65 65 43 72 65 61 74 65 54 61 62 6c 3BtreeCreateTabl
1e9d5 65 28 75 2e 62 74 2e 70 44 62 2d 3e 70 42 74 2c e(u.bt.pDb->pBt,
1e9d6 20 26 75 2e 62 74 2e 70 67 6e 6f 2c 20 75 2e 62 &u.bt.pgno, u.b
1e9d7 74 2e 66 6c 61 67 73 29 3b 0a 20 20 70 4f 75 74 t.flags);. pOut
1e9d8 2d 3e 75 2e 69 20 3d 20 75 2e 62 74 2e 70 67 6e ->u.i = u.bt.pgn
1e9d9 6f 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 o;. MemSetTypeF
1e9da 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e lag(pOut, MEM_In
1e9db 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a t);. break;.}..
1e9dc 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 61 72 73 65 /* Opcode: Parse
1e9dd 53 63 68 65 6d 61 20 50 31 20 50 32 20 2a 20 50 Schema P1 P2 * P
1e9de 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 61 4 *.**.** Read a
1e9df 6e 64 20 70 61 72 73 65 20 61 6c 6c 20 65 6e 74 nd parse all ent
1e9e0 72 69 65 73 20 66 72 6f 6d 20 74 68 65 20 53 51 ries from the SQ
1e9e1 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c LITE_MASTER tabl
1e9e2 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 50 31 e of database P1
1e9e3 0a 2a 2a 20 74 68 61 74 20 6d 61 74 63 68 20 74 .** that match t
1e9e4 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 he WHERE clause
1e9e5 50 34 2e 20 20 50 32 20 69 73 20 74 68 65 20 22 P4. P2 is the "
1e9e6 66 6f 72 63 65 22 20 66 6c 61 67 2e 20 20 20 41 force" flag. A
1e9e7 6c 77 61 79 73 20 64 6f 0a 2a 2a 20 74 68 65 20 lways do.** the
1e9e8 70 61 72 73 69 6e 67 20 69 66 20 50 32 20 69 73 parsing if P2 is
1e9e9 20 74 72 75 65 2e 20 20 49 66 20 50 32 20 69 73 true. If P2 is
1e9ea 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 74 68 69 false, then thi
1e9eb 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 0a 2a s routine is a.*
1e9ec 2a 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 73 * no-op if the s
1e9ed 63 68 65 6d 61 20 69 73 20 6e 6f 74 20 63 75 72 chema is not cur
1e9ee 72 65 6e 74 6c 79 20 6c 6f 61 64 65 64 2e 20 20 rently loaded.
1e9ef 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 In other words,
1e9f0 69 66 20 50 32 0a 2a 2a 20 69 73 20 66 61 6c 73 if P2.** is fals
1e9f1 65 2c 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 e, the SQLITE_MA
1e9f2 53 54 45 52 20 74 61 62 6c 65 20 69 73 20 6f 6e STER table is on
1e9f3 6c 79 20 70 61 72 73 65 64 20 69 66 20 74 68 65 ly parsed if the
1e9f4 20 72 65 73 74 20 6f 66 20 74 68 65 0a 2a 2a 20 rest of the.**
1e9f5 73 63 68 65 6d 61 20 69 73 20 61 6c 72 65 61 64 schema is alread
1e9f6 79 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20 74 68 y loaded into th
1e9f7 65 20 73 79 6d 62 6f 6c 20 74 61 62 6c 65 2e 0a e symbol table..
1e9f8 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 **.** This opcod
1e9f9 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 70 61 e invokes the pa
1e9fa 72 73 65 72 20 74 6f 20 63 72 65 61 74 65 20 61 rser to create a
1e9fb 20 6e 65 77 20 76 69 72 74 75 61 6c 20 6d 61 63 new virtual mac
1e9fc 68 69 6e 65 2c 0a 2a 2a 20 74 68 65 6e 20 72 75 hine,.** then ru
1e9fd 6e 73 20 74 68 65 20 6e 65 77 20 76 69 72 74 75 ns the new virtu
1e9fe 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20 49 74 20 al machine. It
1e9ff 69 73 20 74 68 75 73 20 61 20 72 65 2d 65 6e 74 is thus a re-ent
1ea00 72 61 6e 74 20 6f 70 63 6f 64 65 2e 0a 2a 2f 0a rant opcode..*/.
1ea01 63 61 73 65 20 4f 50 5f 50 61 72 73 65 53 63 68 case OP_ParseSch
1ea02 65 6d 61 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a ema: {.#if 0 /*
1ea03 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1ea04 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 75 moved into u.bu
1ea05 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 */. int iDb;.
1ea06 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 const char *zMa
1ea07 73 74 65 72 3b 0a 20 20 63 68 61 72 20 2a 7a 53 ster;. char *zS
1ea08 71 6c 3b 0a 20 20 49 6e 69 74 44 61 74 61 20 69 ql;. InitData i
1ea09 6e 69 74 44 61 74 61 3b 0a 23 65 6e 64 69 66 20 nitData;.#endif
1ea0a 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1ea0b 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1ea0c 62 75 20 2a 2f 0a 0a 20 20 75 2e 62 75 2e 69 44 bu */.. u.bu.iD
1ea0d 62 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 61 b = pOp->p1;. a
1ea0e 73 73 65 72 74 28 20 75 2e 62 75 2e 69 44 62 3e ssert( u.bu.iDb>
1ea0f 3d 30 20 26 26 20 75 2e 62 75 2e 69 44 62 3c 64 =0 && u.bu.iDb<d
1ea10 62 2d 3e 6e 44 62 20 29 3b 0a 0a 20 20 2f 2a 20 b->nDb );.. /*
1ea11 49 66 20 70 4f 70 2d 3e 70 32 20 69 73 20 30 2c If pOp->p2 is 0,
1ea12 20 74 68 65 6e 20 74 68 69 73 20 6f 70 63 6f 64 then this opcod
1ea13 65 20 69 73 20 62 65 69 6e 67 20 65 78 65 63 75 e is being execu
1ea14 74 65 64 20 74 6f 20 72 65 61 64 20 61 0a 20 20 ted to read a.
1ea15 2a 2a 20 73 69 6e 67 6c 65 20 72 6f 77 2c 20 66 ** single row, f
1ea16 6f 72 20 65 78 61 6d 70 6c 65 20 74 68 65 20 72 or example the r
1ea17 6f 77 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 ow corresponding
1ea18 20 74 6f 20 61 20 6e 65 77 20 69 6e 64 65 78 0a to a new index.
1ea19 20 20 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20 ** created by
1ea1a 74 68 69 73 20 56 44 42 45 2c 20 66 72 6f 6d 20 this VDBE, from
1ea1b 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 the sqlite_maste
1ea1c 72 20 74 61 62 6c 65 2e 20 49 74 20 6f 6e 6c 79 r table. It only
1ea1d 0a 20 20 2a 2a 20 64 6f 65 73 20 74 68 69 73 20 . ** does this
1ea1e 69 66 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e if the correspon
1ea1f 64 69 6e 67 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 ding in-memory s
1ea20 63 68 65 6d 61 20 69 73 20 63 75 72 72 65 6e 74 chema is current
1ea21 6c 79 0a 20 20 2a 2a 20 6c 6f 61 64 65 64 2e 20 ly. ** loaded.
1ea22 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 6e Otherwise, the n
1ea23 65 77 20 69 6e 64 65 78 20 64 65 66 69 6e 69 74 ew index definit
1ea24 69 6f 6e 20 63 61 6e 20 62 65 20 6c 6f 61 64 65 ion can be loade
1ea25 64 20 61 6c 6f 6e 67 0a 20 20 2a 2a 20 77 69 74 d along. ** wit
1ea26 68 20 74 68 65 20 72 65 73 74 20 6f 66 20 74 68 h the rest of th
1ea27 65 20 73 63 68 65 6d 61 20 77 68 65 6e 20 69 74 e schema when it
1ea28 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a 20 20 is required..
1ea29 2a 2a 0a 20 20 2a 2a 20 41 6c 74 68 6f 75 67 68 **. ** Although
1ea2a 20 74 68 65 20 6d 75 74 65 78 20 6f 6e 20 74 68 the mutex on th
1ea2b 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 e BtShared objec
1ea2c 74 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f 6e t that correspon
1ea2d 64 73 20 74 6f 0a 20 20 2a 2a 20 64 61 74 61 62 ds to. ** datab
1ea2e 61 73 65 20 75 2e 62 75 2e 69 44 62 20 28 74 68 ase u.bu.iDb (th
1ea2f 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 e database conta
1ea30 69 6e 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 ining the sqlite
1ea31 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 0a 20 20 _master table.
1ea32 2a 2a 20 72 65 61 64 20 62 79 20 74 68 69 73 20 ** read by this
1ea33 69 6e 73 74 72 75 63 74 69 6f 6e 29 20 69 73 20 instruction) is
1ea34 63 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 2c 20 currently held,
1ea35 69 74 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 it is necessary
1ea36 74 6f 0a 20 20 2a 2a 20 6f 62 74 61 69 6e 20 74 to. ** obtain t
1ea37 68 65 20 6d 75 74 65 78 65 73 20 6f 6e 20 61 6c he mutexes on al
1ea38 6c 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 l attached datab
1ea39 61 73 65 73 20 62 65 66 6f 72 65 20 63 68 65 63 ases before chec
1ea3a 6b 69 6e 67 20 69 66 0a 20 20 2a 2a 20 74 68 65 king if. ** the
1ea3b 20 73 63 68 65 6d 61 20 6f 66 20 75 2e 62 75 2e schema of u.bu.
1ea3c 69 44 62 20 69 73 20 6c 6f 61 64 65 64 2e 20 54 iDb is loaded. T
1ea3d 68 69 73 20 69 73 20 62 65 63 61 75 73 65 2c 20 his is because,
1ea3e 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 0a at the start of.
1ea3f 20 20 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 ** the sqlite3
1ea40 5f 65 78 65 63 28 29 20 63 61 6c 6c 20 62 65 6c _exec() call bel
1ea41 6f 77 2c 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 ow, SQLite will
1ea42 69 6e 76 6f 6b 65 0a 20 20 2a 2a 20 73 71 6c 69 invoke. ** sqli
1ea43 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c te3BtreeEnterAll
1ea44 28 29 2e 20 49 66 20 61 6c 6c 20 6d 75 74 65 78 (). If all mutex
1ea45 65 73 20 61 72 65 20 6e 6f 74 20 61 6c 72 65 61 es are not alrea
1ea46 64 79 20 68 65 6c 64 2c 20 74 68 65 0a 20 20 2a dy held, the. *
1ea47 2a 20 75 2e 62 75 2e 69 44 62 20 6d 75 74 65 78 * u.bu.iDb mutex
1ea48 20 6d 61 79 20 62 65 20 74 65 6d 70 6f 72 61 72 may be temporar
1ea49 69 6c 79 20 72 65 6c 65 61 73 65 64 20 74 6f 20 ily released to
1ea4a 61 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b 2e 20 avoid deadlock.
1ea4b 49 66 0a 20 20 2a 2a 20 74 68 69 73 20 68 61 70 If. ** this hap
1ea4c 70 65 6e 73 2c 20 74 68 65 6e 20 73 6f 6d 65 20 pens, then some
1ea4d 6f 74 68 65 72 20 74 68 72 65 61 64 20 6d 61 79 other thread may
1ea4e 20 64 65 6c 65 74 65 20 74 68 65 20 69 6e 2d 6d delete the in-m
1ea4f 65 6d 6f 72 79 0a 20 20 2a 2a 20 73 63 68 65 6d emory. ** schem
1ea50 61 20 6f 66 20 64 61 74 61 62 61 73 65 20 75 2e a of database u.
1ea51 62 75 2e 69 44 62 20 62 65 66 6f 72 65 20 74 68 bu.iDb before th
1ea52 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 e SQL statement
1ea53 72 75 6e 73 2e 20 54 68 65 20 73 63 68 65 6d 61 runs. The schema
1ea54 0a 20 20 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 . ** will not b
1ea55 65 20 72 65 6c 6f 61 64 65 64 20 62 65 63 75 61 e reloaded becua
1ea56 73 65 20 74 68 65 20 64 62 2d 3e 69 6e 69 74 2e se the db->init.
1ea57 62 75 73 79 20 66 6c 61 67 20 69 73 20 73 65 74 busy flag is set
1ea58 2e 20 54 68 69 73 0a 20 20 2a 2a 20 63 61 6e 20 . This. ** can
1ea59 72 65 73 75 6c 74 20 69 6e 20 61 20 22 6e 6f 20 result in a "no
1ea5a 73 75 63 68 20 74 61 62 6c 65 3a 20 73 71 6c 69 such table: sqli
1ea5b 74 65 5f 6d 61 73 74 65 72 22 20 6f 72 20 22 6d te_master" or "m
1ea5c 61 6c 66 6f 72 6d 65 64 0a 20 20 2a 2a 20 64 61 alformed. ** da
1ea5d 74 61 62 61 73 65 20 73 63 68 65 6d 61 22 20 65 tabase schema" e
1ea5e 72 72 6f 72 20 62 65 69 6e 67 20 72 65 74 75 72 rror being retur
1ea5f 6e 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 2e ned to the user.
1ea60 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
1ea61 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 sqlite3BtreeHold
1ea62 73 4d 75 74 65 78 28 64 62 2d 3e 61 44 62 5b 75 sMutex(db->aDb[u
1ea63 2e 62 75 2e 69 44 62 5d 2e 70 42 74 29 20 29 3b .bu.iDb].pBt) );
1ea64 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
1ea65 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20 20 69 nterAll(db);. i
1ea66 66 28 20 70 4f 70 2d 3e 70 32 20 7c 7c 20 44 62 f( pOp->p2 || Db
1ea67 48 61 73 50 72 6f 70 65 72 74 79 28 64 62 2c 20 HasProperty(db,
1ea68 75 2e 62 75 2e 69 44 62 2c 20 44 42 5f 53 63 68 u.bu.iDb, DB_Sch
1ea69 65 6d 61 4c 6f 61 64 65 64 29 20 29 7b 0a 20 20 emaLoaded) ){.
1ea6a 20 20 75 2e 62 75 2e 7a 4d 61 73 74 65 72 20 3d u.bu.zMaster =
1ea6b 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 75 2e SCHEMA_TABLE(u.
1ea6c 62 75 2e 69 44 62 29 3b 0a 20 20 20 20 75 2e 62 bu.iDb);. u.b
1ea6d 75 2e 69 6e 69 74 44 61 74 61 2e 64 62 20 3d 20 u.initData.db =
1ea6e 64 62 3b 0a 20 20 20 20 75 2e 62 75 2e 69 6e 69 db;. u.bu.ini
1ea6f 74 44 61 74 61 2e 69 44 62 20 3d 20 70 4f 70 2d tData.iDb = pOp-
1ea70 3e 70 31 3b 0a 20 20 20 20 75 2e 62 75 2e 69 6e >p1;. u.bu.in
1ea71 69 74 44 61 74 61 2e 70 7a 45 72 72 4d 73 67 20 itData.pzErrMsg
1ea72 3d 20 26 70 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 = &p->zErrMsg;.
1ea73 20 20 20 75 2e 62 75 2e 7a 53 71 6c 20 3d 20 73 u.bu.zSql = s
1ea74 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 qlite3MPrintf(db
1ea75 2c 0a 20 20 20 20 20 20 20 22 53 45 4c 45 43 54 ,. "SELECT
1ea76 20 6e 61 6d 65 2c 20 72 6f 6f 74 70 61 67 65 2c name, rootpage,
1ea77 20 73 71 6c 20 46 52 4f 4d 20 27 25 71 27 2e 25 sql FROM '%q'.%
1ea78 73 20 57 48 45 52 45 20 25 73 22 2c 0a 20 20 20 s WHERE %s",.
1ea79 20 20 20 20 64 62 2d 3e 61 44 62 5b 75 2e 62 75 db->aDb[u.bu
1ea7a 2e 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 75 2e 62 .iDb].zName, u.b
1ea7b 75 2e 7a 4d 61 73 74 65 72 2c 20 70 4f 70 2d 3e u.zMaster, pOp->
1ea7c 70 34 2e 7a 29 3b 0a 20 20 20 20 69 66 28 20 75 p4.z);. if( u
1ea7d 2e 62 75 2e 7a 53 71 6c 3d 3d 30 20 29 7b 0a 20 .bu.zSql==0 ){.
1ea7e 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
1ea7f 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 _NOMEM;. }els
1ea80 65 7b 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73 e{. (void)s
1ea81 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 qlite3SafetyOff(
1ea82 64 62 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 db);. asser
1ea83 74 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 t( db->init.busy
1ea84 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 64 62 2d ==0 );. db-
1ea85 3e 69 6e 69 74 2e 62 75 73 79 20 3d 20 31 3b 0a >init.busy = 1;.
1ea86 20 20 20 20 20 20 75 2e 62 75 2e 69 6e 69 74 44 u.bu.initD
1ea87 61 74 61 2e 72 63 20 3d 20 53 51 4c 49 54 45 5f ata.rc = SQLITE_
1ea88 4f 4b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 OK;. assert
1ea89 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 ( !db->mallocFai
1ea8a 6c 65 64 20 29 3b 0a 20 20 20 20 20 20 72 63 20 led );. rc
1ea8b 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64 = sqlite3_exec(d
1ea8c 62 2c 20 75 2e 62 75 2e 7a 53 71 6c 2c 20 73 71 b, u.bu.zSql, sq
1ea8d 6c 69 74 65 33 49 6e 69 74 43 61 6c 6c 62 61 63 lite3InitCallbac
1ea8e 6b 2c 20 26 75 2e 62 75 2e 69 6e 69 74 44 61 74 k, &u.bu.initDat
1ea8f 61 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 a, 0);. if(
1ea90 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1ea91 20 72 63 20 3d 20 75 2e 62 75 2e 69 6e 69 74 44 rc = u.bu.initD
1ea92 61 74 61 2e 72 63 3b 0a 20 20 20 20 20 20 73 71 ata.rc;. sq
1ea93 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
1ea94 75 2e 62 75 2e 7a 53 71 6c 29 3b 0a 20 20 20 20 u.bu.zSql);.
1ea95 20 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 db->init.busy
1ea96 3d 20 30 3b 0a 20 20 20 20 20 20 28 76 6f 69 64 = 0;. (void
1ea97 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e )sqlite3SafetyOn
1ea98 28 64 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a (db);. }. }.
1ea99 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
1ea9a 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 69 66 aveAll(db);. if
1ea9b 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d ( rc==SQLITE_NOM
1ea9c 45 4d 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6e EM ){. goto n
1ea9d 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 62 72 65 o_mem;. }. bre
1ea9e 61 6b 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 ak;.}..#if !defi
1ea9f 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
1eaa0 41 4e 41 4c 59 5a 45 29 0a 2f 2a 20 4f 70 63 6f ANALYZE)./* Opco
1eaa1 64 65 3a 20 4c 6f 61 64 41 6e 61 6c 79 73 69 73 de: LoadAnalysis
1eaa2 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a P1 * * * *.**.*
1eaa3 2a 20 52 65 61 64 20 74 68 65 20 73 71 6c 69 74 * Read the sqlit
1eaa4 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20 66 6f e_stat1 table fo
1eaa5 72 20 64 61 74 61 62 61 73 65 20 50 31 20 61 6e r database P1 an
1eaa6 64 20 6c 6f 61 64 20 74 68 65 20 63 6f 6e 74 65 d load the conte
1eaa7 6e 74 0a 2a 2a 20 6f 66 20 74 68 61 74 20 74 61 nt.** of that ta
1eaa8 62 6c 65 20 69 6e 74 6f 20 74 68 65 20 69 6e 74 ble into the int
1eaa9 65 72 6e 61 6c 20 69 6e 64 65 78 20 68 61 73 68 ernal index hash
1eaaa 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 77 69 table. This wi
1eaab 6c 6c 20 63 61 75 73 65 0a 2a 2a 20 74 68 65 20 ll cause.** the
1eaac 61 6e 61 6c 79 73 69 73 20 74 6f 20 62 65 20 75 analysis to be u
1eaad 73 65 64 20 77 68 65 6e 20 70 72 65 70 61 72 69 sed when prepari
1eaae 6e 67 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e ng all subsequen
1eaaf 74 20 71 75 65 72 69 65 73 2e 0a 2a 2f 0a 63 61 t queries..*/.ca
1eab0 73 65 20 4f 50 5f 4c 6f 61 64 41 6e 61 6c 79 73 se OP_LoadAnalys
1eab1 69 73 3a 20 7b 0a 20 20 61 73 73 65 72 74 28 20 is: {. assert(
1eab2 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f pOp->p1>=0 && pO
1eab3 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b p->p1<db->nDb );
1eab4 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 . rc = sqlite3A
1eab5 6e 61 6c 79 73 69 73 4c 6f 61 64 28 64 62 2c 20 nalysisLoad(db,
1eab6 70 4f 70 2d 3e 70 31 29 3b 0a 20 20 62 72 65 61 pOp->p1);. brea
1eab7 6b 3b 20 20 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a k; .}.#endif /*
1eab8 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
1eab9 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 29 20 2a _OMIT_ANALYZE) *
1eaba 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 72 /../* Opcode: Dr
1eabb 6f 70 54 61 62 6c 65 20 50 31 20 2a 20 2a 20 50 opTable P1 * * P
1eabc 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 6d 6f 76 65 4 *.**.** Remove
1eabd 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 28 69 the internal (i
1eabe 6e 2d 6d 65 6d 6f 72 79 29 20 64 61 74 61 20 73 n-memory) data s
1eabf 74 72 75 63 74 75 72 65 73 20 74 68 61 74 20 64 tructures that d
1eac0 65 73 63 72 69 62 65 0a 2a 2a 20 74 68 65 20 74 escribe.** the t
1eac1 61 62 6c 65 20 6e 61 6d 65 64 20 50 34 20 69 6e able named P4 in
1eac2 20 64 61 74 61 62 61 73 65 20 50 31 2e 20 20 54 database P1. T
1eac3 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 61 66 his is called af
1eac4 74 65 72 20 61 20 74 61 62 6c 65 0a 2a 2a 20 69 ter a table.** i
1eac5 73 20 64 72 6f 70 70 65 64 20 69 6e 20 6f 72 64 s dropped in ord
1eac6 65 72 20 74 6f 20 6b 65 65 70 20 74 68 65 20 69 er to keep the i
1eac7 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e nternal represen
1eac8 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a tation of the.**
1eac9 20 73 63 68 65 6d 61 20 63 6f 6e 73 69 73 74 65 schema consiste
1eaca 6e 74 20 77 69 74 68 20 77 68 61 74 20 69 73 20 nt with what is
1eacb 6f 6e 20 64 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65 on disk..*/.case
1eacc 20 4f 50 5f 44 72 6f 70 54 61 62 6c 65 3a 20 7b OP_DropTable: {
1eacd 0a 20 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b . sqlite3Unlink
1eace 41 6e 64 44 65 6c 65 74 65 54 61 62 6c 65 28 64 AndDeleteTable(d
1eacf 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d b, pOp->p1, pOp-
1ead0 3e 70 34 2e 7a 29 3b 0a 20 20 62 72 65 61 6b 3b >p4.z);. break;
1ead1 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 .}../* Opcode: D
1ead2 72 6f 70 49 6e 64 65 78 20 50 31 20 2a 20 2a 20 ropIndex P1 * *
1ead3 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 6d 6f 76 P4 *.**.** Remov
1ead4 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 28 e the internal (
1ead5 69 6e 2d 6d 65 6d 6f 72 79 29 20 64 61 74 61 20 in-memory) data
1ead6 73 74 72 75 63 74 75 72 65 73 20 74 68 61 74 20 structures that
1ead7 64 65 73 63 72 69 62 65 0a 2a 2a 20 74 68 65 20 describe.** the
1ead8 69 6e 64 65 78 20 6e 61 6d 65 64 20 50 34 20 69 index named P4 i
1ead9 6e 20 64 61 74 61 62 61 73 65 20 50 31 2e 20 20 n database P1.
1eada 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 61 This is called a
1eadb 66 74 65 72 20 61 6e 20 69 6e 64 65 78 0a 2a 2a fter an index.**
1eadc 20 69 73 20 64 72 6f 70 70 65 64 20 69 6e 20 6f is dropped in o
1eadd 72 64 65 72 20 74 6f 20 6b 65 65 70 20 74 68 65 rder to keep the
1eade 20 69 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73 internal repres
1eadf 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a entation of the.
1eae0 2a 2a 20 73 63 68 65 6d 61 20 63 6f 6e 73 69 73 ** schema consis
1eae1 74 65 6e 74 20 77 69 74 68 20 77 68 61 74 20 69 tent with what i
1eae2 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2f 0a 63 61 s on disk..*/.ca
1eae3 73 65 20 4f 50 5f 44 72 6f 70 49 6e 64 65 78 3a se OP_DropIndex:
1eae4 20 7b 0a 20 20 73 71 6c 69 74 65 33 55 6e 6c 69 {. sqlite3Unli
1eae5 6e 6b 41 6e 64 44 65 6c 65 74 65 49 6e 64 65 78 nkAndDeleteIndex
1eae6 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f (db, pOp->p1, pO
1eae7 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 62 72 65 61 p->p4.z);. brea
1eae8 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1eae9 20 44 72 6f 70 54 72 69 67 67 65 72 20 50 31 20 DropTrigger P1
1eaea 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 * * P4 *.**.** R
1eaeb 65 6d 6f 76 65 20 74 68 65 20 69 6e 74 65 72 6e emove the intern
1eaec 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20 64 al (in-memory) d
1eaed 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 74 ata structures t
1eaee 68 61 74 20 64 65 73 63 72 69 62 65 0a 2a 2a 20 hat describe.**
1eaef 74 68 65 20 74 72 69 67 67 65 72 20 6e 61 6d 65 the trigger name
1eaf0 64 20 50 34 20 69 6e 20 64 61 74 61 62 61 73 65 d P4 in database
1eaf1 20 50 31 2e 20 20 54 68 69 73 20 69 73 20 63 61 P1. This is ca
1eaf2 6c 6c 65 64 20 61 66 74 65 72 20 61 20 74 72 69 lled after a tri
1eaf3 67 67 65 72 0a 2a 2a 20 69 73 20 64 72 6f 70 70 gger.** is dropp
1eaf4 65 64 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b ed in order to k
1eaf5 65 65 70 20 74 68 65 20 69 6e 74 65 72 6e 61 6c eep the internal
1eaf6 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 representation
1eaf7 6f 66 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 of the.** schema
1eaf8 20 63 6f 6e 73 69 73 74 65 6e 74 20 77 69 74 68 consistent with
1eaf9 20 77 68 61 74 20 69 73 20 6f 6e 20 64 69 73 6b what is on disk
1eafa 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 72 6f ..*/.case OP_Dro
1eafb 70 54 72 69 67 67 65 72 3a 20 7b 0a 20 20 73 71 pTrigger: {. sq
1eafc 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 lite3UnlinkAndDe
1eafd 6c 65 74 65 54 72 69 67 67 65 72 28 64 62 2c 20 leteTrigger(db,
1eafe 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 pOp->p1, pOp->p4
1eaff 2e 7a 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a .z);. break;.}.
1eb00 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1eb01 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f _OMIT_INTEGRITY_
1eb02 43 48 45 43 4b 0a 2f 2a 20 4f 70 63 6f 64 65 3a CHECK./* Opcode:
1eb03 20 49 6e 74 65 67 72 69 74 79 43 6b 20 50 31 20 IntegrityCk P1
1eb04 50 32 20 50 33 20 2a 20 50 35 0a 2a 2a 0a 2a 2a P2 P3 * P5.**.**
1eb05 20 44 6f 20 61 6e 20 61 6e 61 6c 79 73 69 73 20 Do an analysis
1eb06 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 6c 79 of the currently
1eb07 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 2e 20 open database.
1eb08 20 53 74 6f 72 65 20 69 6e 0a 2a 2a 20 72 65 67 Store in.** reg
1eb09 69 73 74 65 72 20 50 31 20 74 68 65 20 74 65 78 ister P1 the tex
1eb0a 74 20 6f 66 20 61 6e 20 65 72 72 6f 72 20 6d 65 t of an error me
1eb0b 73 73 61 67 65 20 64 65 73 63 72 69 62 69 6e 67 ssage describing
1eb0c 20 61 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 0a 2a any problems..*
1eb0d 2a 20 49 66 20 6e 6f 20 70 72 6f 62 6c 65 6d 73 * If no problems
1eb0e 20 61 72 65 20 66 6f 75 6e 64 2c 20 73 74 6f 72 are found, stor
1eb0f 65 20 61 20 4e 55 4c 4c 20 69 6e 20 72 65 67 69 e a NULL in regi
1eb10 73 74 65 72 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 54 ster P1..**.** T
1eb11 68 65 20 72 65 67 69 73 74 65 72 20 50 33 20 63 he register P3 c
1eb12 6f 6e 74 61 69 6e 73 20 74 68 65 20 6d 61 78 69 ontains the maxi
1eb13 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c mum number of al
1eb14 6c 6f 77 65 64 20 65 72 72 6f 72 73 2e 0a 2a 2a lowed errors..**
1eb15 20 41 74 20 6d 6f 73 74 20 72 65 67 28 50 33 29 At most reg(P3)
1eb16 20 65 72 72 6f 72 73 20 77 69 6c 6c 20 62 65 20 errors will be
1eb17 72 65 70 6f 72 74 65 64 2e 0a 2a 2a 20 49 6e 20 reported..** In
1eb18 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 other words, the
1eb19 20 61 6e 61 6c 79 73 69 73 20 73 74 6f 70 73 20 analysis stops
1eb1a 61 73 20 73 6f 6f 6e 20 61 73 20 72 65 67 28 50 as soon as reg(P
1eb1b 31 29 20 65 72 72 6f 72 73 20 61 72 65 20 0a 2a 1) errors are .*
1eb1c 2a 20 73 65 65 6e 2e 20 20 52 65 67 28 50 31 29 * seen. Reg(P1)
1eb1d 20 69 73 20 75 70 64 61 74 65 64 20 77 69 74 68 is updated with
1eb1e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 the number of e
1eb1f 72 72 6f 72 73 20 72 65 6d 61 69 6e 69 6e 67 2e rrors remaining.
1eb20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f 74 20 .**.** The root
1eb21 70 61 67 65 20 6e 75 6d 62 65 72 73 20 6f 66 20 page numbers of
1eb22 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e 20 74 68 all tables in th
1eb23 65 20 64 61 74 61 62 61 73 65 20 61 72 65 20 69 e database are i
1eb24 6e 74 65 67 65 72 0a 2a 2a 20 73 74 6f 72 65 64 nteger.** stored
1eb25 20 69 6e 20 72 65 67 28 50 31 29 2c 20 72 65 67 in reg(P1), reg
1eb26 28 50 31 2b 31 29 2c 20 72 65 67 28 50 31 2b 32 (P1+1), reg(P1+2
1eb27 29 2c 20 2e 2e 2e 2e 20 20 54 68 65 72 65 20 61 ), .... There a
1eb28 72 65 20 50 32 20 74 61 62 6c 65 73 0a 2a 2a 20 re P2 tables.**
1eb29 74 6f 74 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 total..**.** If
1eb2a 50 35 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 P5 is not zero,
1eb2b 74 68 65 20 63 68 65 63 6b 20 69 73 20 64 6f 6e the check is don
1eb2c 65 20 6f 6e 20 74 68 65 20 61 75 78 69 6c 69 61 e on the auxilia
1eb2d 72 79 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 ry database.** f
1eb2e 69 6c 65 2c 20 6e 6f 74 20 74 68 65 20 6d 61 69 ile, not the mai
1eb2f 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e n database file.
1eb30 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f .**.** This opco
1eb31 64 65 20 69 73 20 75 73 65 64 20 74 6f 20 69 6d de is used to im
1eb32 70 6c 65 6d 65 6e 74 20 74 68 65 20 69 6e 74 65 plement the inte
1eb33 67 72 69 74 79 5f 63 68 65 63 6b 20 70 72 61 67 grity_check prag
1eb34 6d 61 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 ma..*/.case OP_I
1eb35 6e 74 65 67 72 69 74 79 43 6b 3a 20 7b 0a 23 69 ntegrityCk: {.#i
1eb36 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1eb37 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1eb38 74 6f 20 75 2e 62 76 20 2a 2f 0a 20 20 69 6e 74 to u.bv */. int
1eb39 20 6e 52 6f 6f 74 3b 20 20 20 20 20 20 2f 2a 20 nRoot; /*
1eb3a 4e 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73 Number of tables
1eb3b 20 74 6f 20 63 68 65 63 6b 2e 20 20 28 4e 75 6d to check. (Num
1eb3c 62 65 72 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 ber of root page
1eb3d 73 2e 29 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 s.) */. int *aR
1eb3e 6f 6f 74 3b 20 20 20 20 20 2f 2a 20 41 72 72 61 oot; /* Arra
1eb3f 79 20 6f 66 20 72 6f 6f 74 70 61 67 65 20 6e 75 y of rootpage nu
1eb40 6d 62 65 72 73 20 66 6f 72 20 74 61 62 6c 65 73 mbers for tables
1eb41 20 74 6f 20 62 65 20 63 68 65 63 6b 65 64 20 2a to be checked *
1eb42 2f 0a 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 20 /. int j;
1eb43 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e /* Loop coun
1eb44 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 ter */. int nEr
1eb45 72 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 r; /* Numb
1eb46 65 72 20 6f 66 20 65 72 72 6f 72 73 20 72 65 70 er of errors rep
1eb47 6f 72 74 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 orted */. char
1eb48 2a 7a 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 65 *z; /* Te
1eb49 78 74 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20 xt of the error
1eb4a 72 65 70 6f 72 74 20 2a 2f 0a 20 20 4d 65 6d 20 report */. Mem
1eb4b 2a 70 6e 45 72 72 3b 20 20 20 20 20 2f 2a 20 52 *pnErr; /* R
1eb4c 65 67 69 73 74 65 72 20 6b 65 65 70 69 6e 67 20 egister keeping
1eb4d 74 72 61 63 6b 20 6f 66 20 65 72 72 6f 72 73 20 track of errors
1eb4e 72 65 6d 61 69 6e 69 6e 67 20 2a 2f 0a 23 65 6e remaining */.#en
1eb4f 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 dif /* local var
1eb50 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1eb51 6f 20 75 2e 62 76 20 2a 2f 0a 0a 20 20 75 2e 62 o u.bv */.. u.b
1eb52 76 2e 6e 52 6f 6f 74 20 3d 20 70 4f 70 2d 3e 70 v.nRoot = pOp->p
1eb53 32 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 2;. assert( u.b
1eb54 76 2e 6e 52 6f 6f 74 3e 30 20 29 3b 0a 20 20 75 v.nRoot>0 );. u
1eb55 2e 62 76 2e 61 52 6f 6f 74 20 3d 20 73 71 6c 69 .bv.aRoot = sqli
1eb56 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 te3DbMallocRaw(d
1eb57 62 2c 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 28 b, sizeof(int)*(
1eb58 75 2e 62 76 2e 6e 52 6f 6f 74 2b 31 29 20 29 3b u.bv.nRoot+1) );
1eb59 0a 20 20 69 66 28 20 75 2e 62 76 2e 61 52 6f 6f . if( u.bv.aRoo
1eb5a 74 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d t==0 ) goto no_m
1eb5b 65 6d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f em;. assert( pO
1eb5c 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e p->p3>0 && pOp->
1eb5d 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 p3<=p->nMem );.
1eb5e 20 75 2e 62 76 2e 70 6e 45 72 72 20 3d 20 26 70 u.bv.pnErr = &p
1eb5f 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b ->aMem[pOp->p3];
1eb60 0a 20 20 61 73 73 65 72 74 28 20 28 75 2e 62 76 . assert( (u.bv
1eb61 2e 70 6e 45 72 72 2d 3e 66 6c 61 67 73 20 26 20 .pnErr->flags &
1eb62 4d 45 4d 5f 49 6e 74 29 21 3d 30 20 29 3b 0a 20 MEM_Int)!=0 );.
1eb63 20 61 73 73 65 72 74 28 20 28 75 2e 62 76 2e 70 assert( (u.bv.p
1eb64 6e 45 72 72 2d 3e 66 6c 61 67 73 20 26 20 28 4d nErr->flags & (M
1eb65 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 EM_Str|MEM_Blob)
1eb66 29 3d 3d 30 20 29 3b 0a 20 20 70 49 6e 31 20 3d )==0 );. pIn1 =
1eb67 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 &p->aMem[pOp->p
1eb68 31 5d 3b 0a 20 20 66 6f 72 28 75 2e 62 76 2e 6a 1];. for(u.bv.j
1eb69 3d 30 3b 20 75 2e 62 76 2e 6a 3c 75 2e 62 76 2e =0; u.bv.j<u.bv.
1eb6a 6e 52 6f 6f 74 3b 20 75 2e 62 76 2e 6a 2b 2b 29 nRoot; u.bv.j++)
1eb6b 7b 0a 20 20 20 20 75 2e 62 76 2e 61 52 6f 6f 74 {. u.bv.aRoot
1eb6c 5b 75 2e 62 76 2e 6a 5d 20 3d 20 28 69 6e 74 29 [u.bv.j] = (int)
1eb6d 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 sqlite3VdbeIntVa
1eb6e 6c 75 65 28 26 70 49 6e 31 5b 75 2e 62 76 2e 6a lue(&pIn1[u.bv.j
1eb6f 5d 29 3b 0a 20 20 7d 0a 20 20 75 2e 62 76 2e 61 ]);. }. u.bv.a
1eb70 52 6f 6f 74 5b 75 2e 62 76 2e 6a 5d 20 3d 20 30 Root[u.bv.j] = 0
1eb71 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d ;. assert( pOp-
1eb72 3e 70 35 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 >p5<db->nDb );.
1eb73 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 assert( (p->btr
1eb74 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f 70 eeMask & (1<<pOp
1eb75 2d 3e 70 35 29 29 21 3d 30 20 29 3b 0a 20 20 75 ->p5))!=0 );. u
1eb76 2e 62 76 2e 7a 20 3d 20 73 71 6c 69 74 65 33 42 .bv.z = sqlite3B
1eb77 74 72 65 65 49 6e 74 65 67 72 69 74 79 43 68 65 treeIntegrityChe
1eb78 63 6b 28 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e ck(db->aDb[pOp->
1eb79 70 35 5d 2e 70 42 74 2c 20 75 2e 62 76 2e 61 52 p5].pBt, u.bv.aR
1eb7a 6f 6f 74 2c 20 75 2e 62 76 2e 6e 52 6f 6f 74 2c oot, u.bv.nRoot,
1eb7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1eb7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1eb7d 20 20 28 69 6e 74 29 75 2e 62 76 2e 70 6e 45 72 (int)u.bv.pnEr
1eb7e 72 2d 3e 75 2e 69 2c 20 26 75 2e 62 76 2e 6e 45 r->u.i, &u.bv.nE
1eb7f 72 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 rr);. sqlite3Db
1eb80 46 72 65 65 28 64 62 2c 20 75 2e 62 76 2e 61 52 Free(db, u.bv.aR
1eb81 6f 6f 74 29 3b 0a 20 20 75 2e 62 76 2e 70 6e 45 oot);. u.bv.pnE
1eb82 72 72 2d 3e 75 2e 69 20 2d 3d 20 75 2e 62 76 2e rr->u.i -= u.bv.
1eb83 6e 45 72 72 3b 0a 20 20 73 71 6c 69 74 65 33 56 nErr;. sqlite3V
1eb84 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 49 dbeMemSetNull(pI
1eb85 6e 31 29 3b 0a 20 20 69 66 28 20 75 2e 62 76 2e n1);. if( u.bv.
1eb86 6e 45 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20 61 nErr==0 ){. a
1eb87 73 73 65 72 74 28 20 75 2e 62 76 2e 7a 3d 3d 30 ssert( u.bv.z==0
1eb88 20 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 );. }else if(
1eb89 75 2e 62 76 2e 7a 3d 3d 30 20 29 7b 0a 20 20 20 u.bv.z==0 ){.
1eb8a 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 goto no_mem;.
1eb8b 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 }else{. sqlit
1eb8c 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 e3VdbeMemSetStr(
1eb8d 70 49 6e 31 2c 20 75 2e 62 76 2e 7a 2c 20 2d 31 pIn1, u.bv.z, -1
1eb8e 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 73 , SQLITE_UTF8, s
1eb8f 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 qlite3_free);.
1eb90 7d 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 }. UPDATE_MAX_B
1eb91 4c 4f 42 53 49 5a 45 28 70 49 6e 31 29 3b 0a 20 LOBSIZE(pIn1);.
1eb92 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
1eb93 67 65 45 6e 63 6f 64 69 6e 67 28 70 49 6e 31 2c geEncoding(pIn1,
1eb94 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 62 72 encoding);. br
1eb95 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a eak;.}.#endif /*
1eb96 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 SQLITE_OMIT_INT
1eb97 45 47 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a EGRITY_CHECK */.
1eb98 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 53 ./* Opcode: RowS
1eb99 65 74 41 64 64 20 50 31 20 50 32 20 2a 20 2a 20 etAdd P1 P2 * *
1eb9a 2a 0a 2a 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 74 *.**.** Insert t
1eb9b 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 he integer value
1eb9c 20 68 65 6c 64 20 62 79 20 72 65 67 69 73 74 65 held by registe
1eb9d 72 20 50 32 20 69 6e 74 6f 20 61 20 62 6f 6f 6c r P2 into a bool
1eb9e 65 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 68 65 6c ean index.** hel
1eb9f 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 d in register P1
1eba0 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 73 73 65 72 ..**.** An asser
1eba1 74 69 6f 6e 20 66 61 69 6c 73 20 69 66 20 50 32 tion fails if P2
1eba2 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 67 is not an integ
1eba3 65 72 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 er..*/.case OP_R
1eba4 6f 77 53 65 74 41 64 64 3a 20 7b 20 20 20 20 20 owSetAdd: {
1eba5 20 20 2f 2a 20 69 6e 32 20 2a 2f 0a 23 69 66 20 /* in2 */.#if
1eba6 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1eba7 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1eba8 20 75 2e 62 77 20 2a 2f 0a 20 20 4d 65 6d 20 2a u.bw */. Mem *
1eba9 70 49 64 78 3b 0a 20 20 4d 65 6d 20 2a 70 56 61 pIdx;. Mem *pVa
1ebaa 6c 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 l;.#endif /* loc
1ebab 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1ebac 65 64 20 69 6e 74 6f 20 75 2e 62 77 20 2a 2f 0a ed into u.bw */.
1ebad 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1ebae 31 3e 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 3d 1>0 && pOp->p1<=
1ebaf 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e 62 p->nMem );. u.b
1ebb0 77 2e 70 49 64 78 20 3d 20 26 70 2d 3e 61 4d 65 w.pIdx = &p->aMe
1ebb1 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 m[pOp->p1];. as
1ebb2 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 sert( pOp->p2>0
1ebb3 26 26 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e && pOp->p2<=p->n
1ebb4 4d 65 6d 20 29 3b 0a 20 20 75 2e 62 77 2e 70 56 Mem );. u.bw.pV
1ebb5 61 6c 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f al = &p->aMem[pO
1ebb6 70 2d 3e 70 32 5d 3b 0a 20 20 61 73 73 65 72 74 p->p2];. assert
1ebb7 28 20 28 75 2e 62 77 2e 70 56 61 6c 2d 3e 66 6c ( (u.bw.pVal->fl
1ebb8 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29 21 3d ags & MEM_Int)!=
1ebb9 30 20 29 3b 0a 20 20 69 66 28 20 28 75 2e 62 77 0 );. if( (u.bw
1ebba 2e 70 49 64 78 2d 3e 66 6c 61 67 73 20 26 20 4d .pIdx->flags & M
1ebbb 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 7b EM_RowSet)==0 ){
1ebbc 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1ebbd 4d 65 6d 53 65 74 52 6f 77 53 65 74 28 75 2e 62 MemSetRowSet(u.b
1ebbe 77 2e 70 49 64 78 29 3b 0a 20 20 20 20 69 66 28 w.pIdx);. if(
1ebbf 20 28 75 2e 62 77 2e 70 49 64 78 2d 3e 66 6c 61 (u.bw.pIdx->fla
1ebc0 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 gs & MEM_RowSet)
1ebc1 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 ==0 ) goto no_me
1ebc2 6d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 m;. }. sqlite3
1ebc3 52 6f 77 53 65 74 49 6e 73 65 72 74 28 75 2e 62 RowSetInsert(u.b
1ebc4 77 2e 70 49 64 78 2d 3e 75 2e 70 52 6f 77 53 65 w.pIdx->u.pRowSe
1ebc5 74 2c 20 75 2e 62 77 2e 70 56 61 6c 2d 3e 75 2e t, u.bw.pVal->u.
1ebc6 69 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a i);. break;.}..
1ebc7 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 53 65 /* Opcode: RowSe
1ebc8 74 52 65 61 64 20 50 31 20 50 32 20 50 33 20 2a tRead P1 P2 P3 *
1ebc9 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 74 72 61 63 74 *.**.** Extract
1ebca 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 76 61 the smallest va
1ebcb 6c 75 65 20 66 72 6f 6d 20 62 6f 6f 6c 65 61 6e lue from boolean
1ebcc 20 69 6e 64 65 78 20 50 31 20 61 6e 64 20 70 75 index P1 and pu
1ebcd 74 20 74 68 61 74 20 76 61 6c 75 65 20 69 6e 74 t that value int
1ebce 6f 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 33 o.** register P3
1ebcf 2e 20 20 4f 72 2c 20 69 66 20 62 6f 6f 6c 65 61 . Or, if boolea
1ebd0 6e 20 69 6e 64 65 78 20 50 31 20 69 73 20 69 6e n index P1 is in
1ebd1 69 74 69 61 6c 6c 79 20 65 6d 70 74 79 2c 20 6c itially empty, l
1ebd2 65 61 76 65 20 50 33 0a 2a 2a 20 75 6e 63 68 61 eave P3.** uncha
1ebd3 6e 67 65 64 20 61 6e 64 20 6a 75 6d 70 20 74 6f nged and jump to
1ebd4 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 50 32 2e instruction P2.
1ebd5 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f 77 53 .*/.case OP_RowS
1ebd6 65 74 52 65 61 64 3a 20 7b 20 20 20 20 20 20 20 etRead: {
1ebd7 2f 2a 20 6a 75 6d 70 2c 20 6f 75 74 33 20 2a 2f /* jump, out3 */
1ebd8 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1ebd9 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1ebda 20 69 6e 74 6f 20 75 2e 62 78 20 2a 2f 0a 20 20 into u.bx */.
1ebdb 4d 65 6d 20 2a 70 49 64 78 3b 0a 20 20 69 36 34 Mem *pIdx;. i64
1ebdc 20 76 61 6c 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 val;.#endif /*
1ebdd 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1ebde 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 78 20 moved into u.bx
1ebdf 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 */. assert( pOp
1ebe0 2d 3e 70 31 3e 30 20 26 26 20 70 4f 70 2d 3e 70 ->p1>0 && pOp->p
1ebe1 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 1<=p->nMem );.
1ebe2 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 CHECK_FOR_INTERR
1ebe3 55 50 54 3b 0a 20 20 75 2e 62 78 2e 70 49 64 78 UPT;. u.bx.pIdx
1ebe4 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d = &p->aMem[pOp-
1ebe5 3e 70 31 5d 3b 0a 20 20 70 4f 75 74 20 3d 20 26 >p1];. pOut = &
1ebe6 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d p->aMem[pOp->p3]
1ebe7 3b 0a 20 20 69 66 28 20 28 75 2e 62 78 2e 70 49 ;. if( (u.bx.pI
1ebe8 64 78 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f dx->flags & MEM_
1ebe9 52 6f 77 53 65 74 29 3d 3d 30 0a 20 20 20 7c 7c RowSet)==0. ||
1ebea 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 4e 65 sqlite3RowSetNe
1ebeb 78 74 28 75 2e 62 78 2e 70 49 64 78 2d 3e 75 2e xt(u.bx.pIdx->u.
1ebec 70 52 6f 77 53 65 74 2c 20 26 75 2e 62 78 2e 76 pRowSet, &u.bx.v
1ebed 61 6c 29 3d 3d 30 0a 20 20 29 7b 0a 20 20 20 20 al)==0. ){.
1ebee 2f 2a 20 54 68 65 20 62 6f 6f 6c 65 61 6e 20 69 /* The boolean i
1ebef 6e 64 65 78 20 69 73 20 65 6d 70 74 79 20 2a 2f ndex is empty */
1ebf0 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1ebf1 4d 65 6d 53 65 74 4e 75 6c 6c 28 75 2e 62 78 2e MemSetNull(u.bx.
1ebf2 70 49 64 78 29 3b 0a 20 20 20 20 70 63 20 3d 20 pIdx);. pc =
1ebf3 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d pOp->p2 - 1;. }
1ebf4 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 41 20 76 else{. /* A v
1ebf5 61 6c 75 65 20 77 61 73 20 70 75 6c 6c 65 64 20 alue was pulled
1ebf6 66 72 6f 6d 20 74 68 65 20 69 6e 64 65 78 20 2a from the index *
1ebf7 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f /. assert( pO
1ebf8 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e p->p3>0 && pOp->
1ebf9 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 p3<=p->nMem );.
1ebfa 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
1ebfb 6d 53 65 74 49 6e 74 36 34 28 70 4f 75 74 2c 20 mSetInt64(pOut,
1ebfc 75 2e 62 78 2e 76 61 6c 29 3b 0a 20 20 7d 0a 20 u.bx.val);. }.
1ebfd 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1ebfe 63 6f 64 65 3a 20 52 6f 77 53 65 74 54 65 73 74 code: RowSetTest
1ebff 20 50 31 20 50 32 20 50 33 20 50 34 0a 2a 2a 0a P1 P2 P3 P4.**.
1ec00 2a 2a 20 52 65 67 69 73 74 65 72 20 50 33 20 69 ** Register P3 i
1ec01 73 20 61 73 73 75 6d 65 64 20 74 6f 20 68 6f 6c s assumed to hol
1ec02 64 20 61 20 36 34 2d 62 69 74 20 69 6e 74 65 67 d a 64-bit integ
1ec03 65 72 20 76 61 6c 75 65 2e 20 49 66 20 72 65 67 er value. If reg
1ec04 69 73 74 65 72 20 50 31 0a 2a 2a 20 63 6f 6e 74 ister P1.** cont
1ec05 61 69 6e 73 20 61 20 52 6f 77 53 65 74 20 6f 62 ains a RowSet ob
1ec06 6a 65 63 74 20 61 6e 64 20 74 68 61 74 20 52 6f ject and that Ro
1ec07 77 53 65 74 20 6f 62 6a 65 63 74 20 63 6f 6e 74 wSet object cont
1ec08 61 69 6e 73 0a 2a 2a 20 74 68 65 20 76 61 6c 75 ains.** the valu
1ec09 65 20 68 65 6c 64 20 69 6e 20 50 33 2c 20 6a 75 e held in P3, ju
1ec0a 6d 70 20 74 6f 20 72 65 67 69 73 74 65 72 20 50 mp to register P
1ec0b 32 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 6e 2. Otherwise, in
1ec0c 73 65 72 74 20 74 68 65 0a 2a 2a 20 69 6e 74 65 sert the.** inte
1ec0d 67 65 72 20 69 6e 20 50 33 20 69 6e 74 6f 20 74 ger in P3 into t
1ec0e 68 65 20 52 6f 77 53 65 74 20 61 6e 64 20 63 6f he RowSet and co
1ec0f 6e 74 69 6e 75 65 20 6f 6e 20 74 6f 20 74 68 65 ntinue on to the
1ec10 0a 2a 2a 20 6e 65 78 74 20 6f 70 63 6f 64 65 2e .** next opcode.
1ec11 0a 2a 2a 0a 2a 2a 20 54 68 65 20 52 6f 77 53 65 .**.** The RowSe
1ec12 74 20 6f 62 6a 65 63 74 20 69 73 20 6f 70 74 69 t object is opti
1ec13 6d 69 7a 65 64 20 66 6f 72 20 74 68 65 20 63 61 mized for the ca
1ec14 73 65 20 77 68 65 72 65 20 73 75 63 63 65 73 73 se where success
1ec15 69 76 65 20 73 65 74 73 0a 2a 2a 20 6f 66 20 69 ive sets.** of i
1ec16 6e 74 65 67 65 72 73 2c 20 77 68 65 72 65 20 65 ntegers, where e
1ec17 61 63 68 20 73 65 74 20 63 6f 6e 74 61 69 6e 73 ach set contains
1ec18 20 6e 6f 20 64 75 70 6c 69 63 61 74 65 73 2e 20 no duplicates.
1ec19 45 61 63 68 20 73 65 74 0a 2a 2a 20 6f 66 20 76 Each set.** of v
1ec1a 61 6c 75 65 73 20 69 73 20 69 64 65 6e 74 69 66 alues is identif
1ec1b 69 65 64 20 62 79 20 61 20 75 6e 69 71 75 65 20 ied by a unique
1ec1c 50 34 20 76 61 6c 75 65 2e 20 54 68 65 20 66 69 P4 value. The fi
1ec1d 72 73 74 20 73 65 74 0a 2a 2a 20 6d 75 73 74 20 rst set.** must
1ec1e 68 61 76 65 20 50 34 3d 3d 30 2c 20 74 68 65 20 have P4==0, the
1ec1f 66 69 6e 61 6c 20 73 65 74 20 50 34 3d 2d 31 2e final set P4=-1.
1ec20 20 20 50 34 20 6d 75 73 74 20 62 65 20 65 69 74 P4 must be eit
1ec21 68 65 72 20 2d 31 20 6f 72 0a 2a 2a 20 6e 6f 6e her -1 or.** non
1ec22 2d 6e 65 67 61 74 69 76 65 2e 20 20 46 6f 72 20 -negative. For
1ec23 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 20 76 61 6c non-negative val
1ec24 75 65 73 20 6f 66 20 50 34 20 6f 6e 6c 79 20 74 ues of P4 only t
1ec25 68 65 20 6c 6f 77 65 72 20 34 0a 2a 2a 20 62 69 he lower 4.** bi
1ec26 74 73 20 61 72 65 20 73 69 67 6e 69 66 69 63 61 ts are significa
1ec27 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 61 nt..**.** This a
1ec28 6c 6c 6f 77 73 20 6f 70 74 69 6d 69 7a 61 74 69 llows optimizati
1ec29 6f 6e 73 3a 20 28 61 29 20 77 68 65 6e 20 50 34 ons: (a) when P4
1ec2a 3d 3d 30 20 74 68 65 72 65 20 69 73 20 6e 6f 20 ==0 there is no
1ec2b 6e 65 65 64 20 74 6f 20 74 65 73 74 0a 2a 2a 20 need to test.**
1ec2c 74 68 65 20 72 6f 77 73 65 74 20 6f 62 6a 65 63 the rowset objec
1ec2d 74 20 66 6f 72 20 50 33 2c 20 61 73 20 69 74 20 t for P3, as it
1ec2e 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 6e 6f is guaranteed no
1ec2f 74 20 74 6f 20 63 6f 6e 74 61 69 6e 20 69 74 2c t to contain it,
1ec30 0a 2a 2a 20 28 62 29 20 77 68 65 6e 20 50 34 3d .** (b) when P4=
1ec31 3d 2d 31 20 74 68 65 72 65 20 69 73 20 6e 6f 20 =-1 there is no
1ec32 6e 65 65 64 20 74 6f 20 69 6e 73 65 72 74 20 74 need to insert t
1ec33 68 65 20 76 61 6c 75 65 2c 20 61 73 20 69 74 20 he value, as it
1ec34 77 69 6c 6c 0a 2a 2a 20 6e 65 76 65 72 20 62 65 will.** never be
1ec35 20 74 65 73 74 65 64 20 66 6f 72 2c 20 61 6e 64 tested for, and
1ec36 20 28 63 29 20 77 68 65 6e 20 61 20 76 61 6c 75 (c) when a valu
1ec37 65 20 74 68 61 74 20 69 73 20 70 61 72 74 20 6f e that is part o
1ec38 66 20 73 65 74 20 58 20 69 73 0a 2a 2a 20 69 6e f set X is.** in
1ec39 73 65 72 74 65 64 2c 20 74 68 65 72 65 20 69 73 serted, there is
1ec3a 20 6e 6f 20 6e 65 65 64 20 74 6f 20 73 65 61 72 no need to sear
1ec3b 63 68 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 ch to see if the
1ec3c 20 73 61 6d 65 20 76 61 6c 75 65 20 77 61 73 0a same value was.
1ec3d 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 69 6e ** previously in
1ec3e 73 65 72 74 65 64 20 61 73 20 70 61 72 74 20 6f serted as part o
1ec3f 66 20 73 65 74 20 58 20 28 6f 6e 6c 79 20 69 66 f set X (only if
1ec40 20 69 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 it was previous
1ec41 6c 79 0a 2a 2a 20 69 6e 73 65 72 74 65 64 20 61 ly.** inserted a
1ec42 73 20 70 61 72 74 20 6f 66 20 73 6f 6d 65 20 6f s part of some o
1ec43 74 68 65 72 20 73 65 74 29 2e 0a 2a 2f 0a 63 61 ther set)..*/.ca
1ec44 73 65 20 4f 50 5f 52 6f 77 53 65 74 54 65 73 74 se OP_RowSetTest
1ec45 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 : {
1ec46 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c /* jump,
1ec47 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 in1, in3 */.#if
1ec48 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 0 /* local var
1ec49 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1ec4a 6f 20 75 2e 62 79 20 2a 2f 0a 20 20 69 6e 74 20 o u.by */. int
1ec4b 69 53 65 74 3b 0a 20 20 69 6e 74 20 65 78 69 73 iSet;. int exis
1ec4c 74 73 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f ts;.#endif /* lo
1ec4d 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1ec4e 76 65 64 20 69 6e 74 6f 20 75 2e 62 79 20 2a 2f ved into u.by */
1ec4f 0a 0a 20 20 75 2e 62 79 2e 69 53 65 74 20 3d 20 .. u.by.iSet =
1ec50 70 4f 70 2d 3e 70 34 2e 69 3b 0a 20 20 61 73 73 pOp->p4.i;. ass
1ec51 65 72 74 28 20 70 49 6e 33 2d 3e 66 6c 61 67 73 ert( pIn3->flags
1ec52 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 0a 20 20 2f &MEM_Int );.. /
1ec53 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6e * If there is an
1ec54 79 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 ything other tha
1ec55 6e 20 61 20 72 6f 77 73 65 74 20 6f 62 6a 65 63 n a rowset objec
1ec56 74 20 69 6e 20 6d 65 6d 6f 72 79 20 63 65 6c 6c t in memory cell
1ec57 20 50 31 2c 0a 20 20 2a 2a 20 64 65 6c 65 74 65 P1,. ** delete
1ec58 20 69 74 20 6e 6f 77 20 61 6e 64 20 69 6e 69 74 it now and init
1ec59 69 61 6c 69 7a 65 20 50 31 20 77 69 74 68 20 61 ialize P1 with a
1ec5a 6e 20 65 6d 70 74 79 20 72 6f 77 73 65 74 0a 20 n empty rowset.
1ec5b 20 2a 2f 0a 20 20 69 66 28 20 28 70 49 6e 31 2d */. if( (pIn1-
1ec5c 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 >flags & MEM_Row
1ec5d 53 65 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 Set)==0 ){. s
1ec5e 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
1ec5f 52 6f 77 53 65 74 28 70 49 6e 31 29 3b 0a 20 20 RowSet(pIn1);.
1ec60 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 if( (pIn1->fla
1ec61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 gs & MEM_RowSet)
1ec62 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 ==0 ) goto no_me
1ec63 6d 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 m;. }.. assert
1ec64 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 ( pOp->p4type==P
1ec65 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 61 73 73 4_INT32 );. ass
1ec66 65 72 74 28 20 75 2e 62 79 2e 69 53 65 74 3d 3d ert( u.by.iSet==
1ec67 2d 31 20 7c 7c 20 75 2e 62 79 2e 69 53 65 74 3e -1 || u.by.iSet>
1ec68 3d 30 20 29 3b 0a 20 20 69 66 28 20 75 2e 62 79 =0 );. if( u.by
1ec69 2e 69 53 65 74 20 29 7b 0a 20 20 20 20 75 2e 62 .iSet ){. u.b
1ec6a 79 2e 65 78 69 73 74 73 20 3d 20 73 71 6c 69 74 y.exists = sqlit
1ec6b 65 33 52 6f 77 53 65 74 54 65 73 74 28 70 49 6e e3RowSetTest(pIn
1ec6c 31 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 0a 20 20 1->u.pRowSet,.
1ec6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ec6e 20 20 20 20 20 20 20 20 20 20 20 20 20 28 75 38 (u8
1ec6f 29 28 75 2e 62 79 2e 69 53 65 74 3e 3d 30 20 3f )(u.by.iSet>=0 ?
1ec70 20 75 2e 62 79 2e 69 53 65 74 20 26 20 30 78 66 u.by.iSet & 0xf
1ec71 20 3a 20 30 78 66 66 29 2c 0a 20 20 20 20 20 20 : 0xff),.
1ec72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ec73 20 20 20 20 20 20 20 20 20 70 49 6e 33 2d 3e 75 pIn3->u
1ec74 2e 69 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 62 .i);. if( u.b
1ec75 79 2e 65 78 69 73 74 73 20 29 7b 0a 20 20 20 20 y.exists ){.
1ec76 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d pc = pOp->p2 -
1ec77 20 31 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 1;. break;
1ec78 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 . }. }. if(
1ec79 20 75 2e 62 79 2e 69 53 65 74 3e 3d 30 20 29 7b u.by.iSet>=0 ){
1ec7a 0a 20 20 20 20 73 71 6c 69 74 65 33 52 6f 77 53 . sqlite3RowS
1ec7b 65 74 49 6e 73 65 72 74 28 70 49 6e 31 2d 3e 75 etInsert(pIn1->u
1ec7c 2e 70 52 6f 77 53 65 74 2c 20 70 49 6e 33 2d 3e .pRowSet, pIn3->
1ec7d 75 2e 69 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 u.i);. }. brea
1ec7e 6b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 k;.}...#ifndef S
1ec7f 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 QLITE_OMIT_TRIGG
1ec80 45 52 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 ER../* Opcode: P
1ec81 72 6f 67 72 61 6d 20 50 31 20 50 32 20 50 33 20 rogram P1 P2 P3
1ec82 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 P4 *.**.** Execu
1ec83 74 65 20 74 68 65 20 74 72 69 67 67 65 72 20 70 te the trigger p
1ec84 72 6f 67 72 61 6d 20 70 61 73 73 65 64 20 61 73 rogram passed as
1ec85 20 50 34 20 28 74 79 70 65 20 50 34 5f 53 55 42 P4 (type P4_SUB
1ec86 50 52 4f 47 52 41 4d 29 2e 20 0a 2a 2a 0a 2a 2a PROGRAM). .**.**
1ec87 20 50 31 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 P1 contains the
1ec88 20 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 address of the
1ec89 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 68 61 74 memory cell that
1ec8a 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 66 69 contains the fi
1ec8b 72 73 74 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 63 rst memory .** c
1ec8c 65 6c 6c 20 69 6e 20 61 6e 20 61 72 72 61 79 20 ell in an array
1ec8d 6f 66 20 76 61 6c 75 65 73 20 75 73 65 64 20 61 of values used a
1ec8e 73 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 s arguments to t
1ec8f 68 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 2e 20 he sub-program.
1ec90 50 32 20 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 P2 .** contains
1ec91 74 68 65 20 61 64 64 72 65 73 73 20 74 6f 20 6a the address to j
1ec92 75 6d 70 20 74 6f 20 69 66 20 74 68 65 20 73 75 ump to if the su
1ec93 62 2d 70 72 6f 67 72 61 6d 20 74 68 72 6f 77 73 b-program throws
1ec94 20 61 6e 20 49 47 4e 4f 52 45 20 0a 2a 2a 20 65 an IGNORE .** e
1ec95 78 63 65 70 74 69 6f 6e 20 75 73 69 6e 67 20 74 xception using t
1ec96 68 65 20 52 41 49 53 45 28 29 20 66 75 6e 63 74 he RAISE() funct
1ec97 69 6f 6e 2e 20 52 65 67 69 73 74 65 72 20 50 33 ion. Register P3
1ec98 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 61 64 contains the ad
1ec99 64 72 65 73 73 20 0a 2a 2a 20 6f 66 20 61 20 6d dress .** of a m
1ec9a 65 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 74 68 emory cell in th
1ec9b 69 73 20 28 74 68 65 20 70 61 72 65 6e 74 29 20 is (the parent)
1ec9c 56 4d 20 74 68 61 74 20 69 73 20 75 73 65 64 20 VM that is used
1ec9d 74 6f 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 to allocate the
1ec9e 0a 2a 2a 20 6d 65 6d 6f 72 79 20 72 65 71 75 69 .** memory requi
1ec9f 72 65 64 20 62 79 20 74 68 65 20 73 75 62 2d 76 red by the sub-v
1eca0 64 62 65 20 61 74 20 72 75 6e 74 69 6d 65 2e 0a dbe at runtime..
1eca1 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f **.** P4 is a po
1eca2 69 6e 74 65 72 20 74 6f 20 74 68 65 20 56 4d 20 inter to the VM
1eca3 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 containing the t
1eca4 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2e 0a rigger program..
1eca5 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 72 6f 67 72 */.case OP_Progr
1eca6 61 6d 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 am: { /*
1eca7 6a 75 6d 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f jump */.#if 0 /
1eca8 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1eca9 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 s moved into u.b
1ecaa 7a 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b z */. int nMem;
1ecab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1ecac 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f * Number of memo
1ecad 72 79 20 72 65 67 69 73 74 65 72 73 20 66 6f 72 ry registers for
1ecae 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a sub-program */.
1ecaf 20 20 69 6e 74 20 6e 42 79 74 65 3b 20 20 20 20 int nByte;
1ecb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 /* Byt
1ecb1 65 73 20 6f 66 20 72 75 6e 74 69 6d 65 20 73 70 es of runtime sp
1ecb2 61 63 65 20 72 65 71 75 69 72 65 64 20 66 6f 72 ace required for
1ecb3 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a sub-program */.
1ecb4 20 20 4d 65 6d 20 2a 70 52 74 3b 20 20 20 20 20 Mem *pRt;
1ecb5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 /* Reg
1ecb6 69 73 74 65 72 20 74 6f 20 61 6c 6c 6f 63 61 74 ister to allocat
1ecb7 65 20 72 75 6e 74 69 6d 65 20 73 70 61 63 65 20 e runtime space
1ecb8 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 */. Mem *pMem;
1ecb9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1ecba 55 73 65 64 20 74 6f 20 69 74 65 72 61 74 65 20 Used to iterate
1ecbb 74 68 72 6f 75 67 68 20 6d 65 6d 6f 72 79 20 63 through memory c
1ecbc 65 6c 6c 73 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 ells */. Mem *p
1ecbd 45 6e 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 End;
1ecbe 20 20 2f 2a 20 4c 61 73 74 20 6d 65 6d 6f 72 79 /* Last memory
1ecbf 20 63 65 6c 6c 20 69 6e 20 6e 65 77 20 61 72 72 cell in new arr
1ecc0 61 79 20 2a 2f 0a 20 20 56 64 62 65 46 72 61 6d ay */. VdbeFram
1ecc1 65 20 2a 70 46 72 61 6d 65 3b 20 20 20 20 20 20 e *pFrame;
1ecc2 2f 2a 20 4e 65 77 20 76 64 62 65 20 66 72 61 6d /* New vdbe fram
1ecc3 65 20 74 6f 20 65 78 65 63 75 74 65 20 69 6e 20 e to execute in
1ecc4 2a 2f 0a 20 20 53 75 62 50 72 6f 67 72 61 6d 20 */. SubProgram
1ecc5 2a 70 50 72 6f 67 72 61 6d 3b 20 20 20 2f 2a 20 *pProgram; /*
1ecc6 53 75 62 2d 70 72 6f 67 72 61 6d 20 74 6f 20 65 Sub-program to e
1ecc7 78 65 63 75 74 65 20 2a 2f 0a 20 20 76 6f 69 64 xecute */. void
1ecc8 20 2a 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 *t;
1ecc9 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 69 64 65 /* Token ide
1ecca 6e 74 69 66 79 69 6e 67 20 74 72 69 67 67 65 72 ntifying trigger
1eccb 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f */.#endif /* lo
1eccc 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1eccd 76 65 64 20 69 6e 74 6f 20 75 2e 62 7a 20 2a 2f ved into u.bz */
1ecce 0a 0a 20 20 75 2e 62 7a 2e 70 50 72 6f 67 72 61 .. u.bz.pProgra
1eccf 6d 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 50 72 6f m = pOp->p4.pPro
1ecd0 67 72 61 6d 3b 0a 20 20 75 2e 62 7a 2e 70 52 74 gram;. u.bz.pRt
1ecd1 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d = &p->aMem[pOp-
1ecd2 3e 70 33 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 >p3];. assert(
1ecd3 75 2e 62 7a 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e u.bz.pProgram->n
1ecd4 4f 70 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 Op>0 );.. /* If
1ecd5 20 74 68 65 20 70 35 20 66 6c 61 67 20 69 73 20 the p5 flag is
1ecd6 63 6c 65 61 72 2c 20 74 68 65 6e 20 72 65 63 75 clear, then recu
1ecd7 72 73 69 76 65 20 69 6e 76 6f 63 61 74 69 6f 6e rsive invocation
1ecd8 20 6f 66 20 74 72 69 67 67 65 72 73 20 69 73 0a of triggers is.
1ecd9 20 20 2a 2a 20 64 69 73 61 62 6c 65 64 20 66 6f ** disabled fo
1ecda 72 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 r backwards comp
1ecdb 61 74 69 62 69 6c 69 74 79 20 28 70 35 20 69 73 atibility (p5 is
1ecdc 20 73 65 74 20 69 66 20 74 68 69 73 20 73 75 62 set if this sub
1ecdd 2d 70 72 6f 67 72 61 6d 0a 20 20 2a 2a 20 69 73 -program. ** is
1ecde 20 72 65 61 6c 6c 79 20 61 20 74 72 69 67 67 65 really a trigge
1ecdf 72 2c 20 6e 6f 74 20 61 20 66 6f 72 65 69 67 6e r, not a foreign
1ece0 20 6b 65 79 20 61 63 74 69 6f 6e 2c 20 61 6e 64 key action, and
1ece1 20 74 68 65 20 66 6c 61 67 20 73 65 74 0a 20 20 the flag set.
1ece2 2a 2a 20 61 6e 64 20 63 6c 65 61 72 65 64 20 62 ** and cleared b
1ece3 79 20 74 68 65 20 22 50 52 41 47 4d 41 20 72 65 y the "PRAGMA re
1ece4 63 75 72 73 69 76 65 5f 74 72 69 67 67 65 72 73 cursive_triggers
1ece5 22 20 63 6f 6d 6d 61 6e 64 20 69 73 20 63 6c 65 " command is cle
1ece6 61 72 29 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 ar).. **. ** I
1ece7 74 20 69 73 20 72 65 63 75 72 73 69 76 65 20 69 t is recursive i
1ece8 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74 72 69 nvocation of tri
1ece9 67 67 65 72 73 2c 20 61 74 20 74 68 65 20 53 51 ggers, at the SQ
1ecea 4c 20 6c 65 76 65 6c 2c 20 74 68 61 74 20 69 73 L level, that is
1eceb 0a 20 20 2a 2a 20 64 69 73 61 62 6c 65 64 2e 20 . ** disabled.
1ecec 49 6e 20 73 6f 6d 65 20 63 61 73 65 73 20 61 20 In some cases a
1eced 73 69 6e 67 6c 65 20 74 72 69 67 67 65 72 20 6d single trigger m
1ecee 61 79 20 67 65 6e 65 72 61 74 65 20 6d 6f 72 65 ay generate more
1ecef 20 74 68 61 6e 20 6f 6e 65 0a 20 20 2a 2a 20 53 than one. ** S
1ecf0 75 62 50 72 6f 67 72 61 6d 20 28 69 66 20 74 68 ubProgram (if th
1ecf1 65 20 74 72 69 67 67 65 72 20 6d 61 79 20 62 65 e trigger may be
1ecf2 20 65 78 65 63 75 74 65 64 20 77 69 74 68 20 6d executed with m
1ecf3 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 64 69 66 ore than one dif
1ecf4 66 65 72 65 6e 74 0a 20 20 2a 2a 20 4f 4e 20 43 ferent. ** ON C
1ecf5 4f 4e 46 4c 49 43 54 20 61 6c 67 6f 72 69 74 68 ONFLICT algorith
1ecf6 6d 29 2e 20 53 75 62 50 72 6f 67 72 61 6d 20 73 m). SubProgram s
1ecf7 74 72 75 63 74 75 72 65 73 20 61 73 73 6f 63 69 tructures associ
1ecf8 61 74 65 64 20 77 69 74 68 20 61 0a 20 20 2a 2a ated with a. **
1ecf9 20 73 69 6e 67 6c 65 20 74 72 69 67 67 65 72 20 single trigger
1ecfa 61 6c 6c 20 68 61 76 65 20 74 68 65 20 73 61 6d all have the sam
1ecfb 65 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 e value for the
1ecfc 53 75 62 50 72 6f 67 72 61 6d 2e 74 6f 6b 65 6e SubProgram.token
1ecfd 0a 20 20 2a 2a 20 76 61 72 69 61 62 6c 65 2e 20 . ** variable.
1ecfe 20 2a 2f 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 */. if( pOp->p
1ecff 35 20 29 7b 0a 20 20 20 20 75 2e 62 7a 2e 74 20 5 ){. u.bz.t
1ed00 3d 20 75 2e 62 7a 2e 70 50 72 6f 67 72 61 6d 2d = u.bz.pProgram-
1ed01 3e 74 6f 6b 65 6e 3b 0a 20 20 20 20 66 6f 72 28 >token;. for(
1ed02 75 2e 62 7a 2e 70 46 72 61 6d 65 3d 70 2d 3e 70 u.bz.pFrame=p->p
1ed03 46 72 61 6d 65 3b 20 75 2e 62 7a 2e 70 46 72 61 Frame; u.bz.pFra
1ed04 6d 65 20 26 26 20 75 2e 62 7a 2e 70 46 72 61 6d me && u.bz.pFram
1ed05 65 2d 3e 74 6f 6b 65 6e 21 3d 75 2e 62 7a 2e 74 e->token!=u.bz.t
1ed06 3b 20 75 2e 62 7a 2e 70 46 72 61 6d 65 3d 75 2e ; u.bz.pFrame=u.
1ed07 62 7a 2e 70 46 72 61 6d 65 2d 3e 70 50 61 72 65 bz.pFrame->pPare
1ed08 6e 74 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 62 nt);. if( u.b
1ed09 7a 2e 70 46 72 61 6d 65 20 29 20 62 72 65 61 6b z.pFrame ) break
1ed0a 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 2d 3e ;. }.. if( p->
1ed0b 6e 46 72 61 6d 65 3e 3d 64 62 2d 3e 61 4c 69 6d nFrame>=db->aLim
1ed0c 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f it[SQLITE_LIMIT_
1ed0d 54 52 49 47 47 45 52 5f 44 45 50 54 48 5d 20 29 TRIGGER_DEPTH] )
1ed0e 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 {. rc = SQLIT
1ed0f 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c E_ERROR;. sql
1ed10 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 ite3SetString(&p
1ed11 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 ->zErrMsg, db, "
1ed12 74 6f 6f 20 6d 61 6e 79 20 6c 65 76 65 6c 73 20 too many levels
1ed13 6f 66 20 74 72 69 67 67 65 72 20 72 65 63 75 72 of trigger recur
1ed14 73 69 6f 6e 22 29 3b 0a 20 20 20 20 62 72 65 61 sion");. brea
1ed15 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 67 k;. }.. /* Reg
1ed16 69 73 74 65 72 20 75 2e 62 7a 2e 70 52 74 20 69 ister u.bz.pRt i
1ed17 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 s used to store
1ed18 74 68 65 20 6d 65 6d 6f 72 79 20 72 65 71 75 69 the memory requi
1ed19 72 65 64 20 74 6f 20 73 61 76 65 20 74 68 65 20 red to save the
1ed1a 73 74 61 74 65 0a 20 20 2a 2a 20 6f 66 20 74 68 state. ** of th
1ed1b 65 20 63 75 72 72 65 6e 74 20 70 72 6f 67 72 61 e current progra
1ed1c 6d 2c 20 61 6e 64 20 74 68 65 20 6d 65 6d 6f 72 m, and the memor
1ed1d 79 20 72 65 71 75 69 72 65 64 20 61 74 20 72 75 y required at ru
1ed1e 6e 74 69 6d 65 20 74 6f 20 65 78 65 63 75 74 65 ntime to execute
1ed1f 0a 20 20 2a 2a 20 74 68 65 20 74 72 69 67 67 65 . ** the trigge
1ed20 72 20 70 72 6f 67 72 61 6d 2e 20 49 66 20 74 68 r program. If th
1ed21 69 73 20 74 72 69 67 67 65 72 20 68 61 73 20 62 is trigger has b
1ed22 65 65 6e 20 66 69 72 65 64 20 62 65 66 6f 72 65 een fired before
1ed23 2c 20 74 68 65 6e 20 75 2e 62 7a 2e 70 52 74 0a , then u.bz.pRt.
1ed24 20 20 2a 2a 20 69 73 20 61 6c 72 65 61 64 79 20 ** is already
1ed25 61 6c 6c 6f 63 61 74 65 64 2e 20 4f 74 68 65 72 allocated. Other
1ed26 77 69 73 65 2c 20 69 74 20 6d 75 73 74 20 62 65 wise, it must be
1ed27 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 20 2a initialized. *
1ed28 2f 0a 20 20 69 66 28 20 28 75 2e 62 7a 2e 70 52 /. if( (u.bz.pR
1ed29 74 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 46 72 61 t->flags&MEM_Fra
1ed2a 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a me)==0 ){. /*
1ed2b 20 53 75 62 50 72 6f 67 72 61 6d 2e 6e 4d 65 6d SubProgram.nMem
1ed2c 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e is set to the n
1ed2d 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 umber of memory
1ed2e 63 65 6c 6c 73 20 75 73 65 64 20 62 79 20 74 68 cells used by th
1ed2f 65 0a 20 20 20 20 2a 2a 20 70 72 6f 67 72 61 6d e. ** program
1ed30 20 73 74 6f 72 65 64 20 69 6e 20 53 75 62 50 72 stored in SubPr
1ed31 6f 67 72 61 6d 2e 61 4f 70 2e 20 41 73 20 77 65 ogram.aOp. As we
1ed32 6c 6c 20 61 73 20 74 68 65 73 65 2c 20 6f 6e 65 ll as these, one
1ed33 20 6d 65 6d 6f 72 79 0a 20 20 20 20 2a 2a 20 63 memory. ** c
1ed34 65 6c 6c 20 69 73 20 72 65 71 75 69 72 65 64 20 ell is required
1ed35 66 6f 72 20 65 61 63 68 20 63 75 72 73 6f 72 20 for each cursor
1ed36 75 73 65 64 20 62 79 20 74 68 65 20 70 72 6f 67 used by the prog
1ed37 72 61 6d 2e 20 53 65 74 20 6c 6f 63 61 6c 0a 20 ram. Set local.
1ed38 20 20 20 2a 2a 20 76 61 72 69 61 62 6c 65 20 75 ** variable u
1ed39 2e 62 7a 2e 6e 4d 65 6d 20 28 61 6e 64 20 6c 61 .bz.nMem (and la
1ed3a 74 65 72 2c 20 56 64 62 65 46 72 61 6d 65 2e 6e ter, VdbeFrame.n
1ed3b 43 68 69 6c 64 4d 65 6d 29 20 74 6f 20 74 68 69 ChildMem) to thi
1ed3c 73 20 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2f 0a s value.. */.
1ed3d 20 20 20 20 75 2e 62 7a 2e 6e 4d 65 6d 20 3d 20 u.bz.nMem =
1ed3e 75 2e 62 7a 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e u.bz.pProgram->n
1ed3f 4d 65 6d 20 2b 20 75 2e 62 7a 2e 70 50 72 6f 67 Mem + u.bz.pProg
1ed40 72 61 6d 2d 3e 6e 43 73 72 3b 0a 20 20 20 20 75 ram->nCsr;. u
1ed41 2e 62 7a 2e 6e 42 79 74 65 20 3d 20 52 4f 55 4e .bz.nByte = ROUN
1ed42 44 38 28 73 69 7a 65 6f 66 28 56 64 62 65 46 72 D8(sizeof(VdbeFr
1ed43 61 6d 65 29 29 0a 20 20 20 20 20 20 20 20 20 20 ame)).
1ed44 20 20 20 20 2b 20 75 2e 62 7a 2e 6e 4d 65 6d 20 + u.bz.nMem
1ed45 2a 20 73 69 7a 65 6f 66 28 4d 65 6d 29 0a 20 20 * sizeof(Mem).
1ed46 20 20 20 20 20 20 20 20 20 20 20 20 2b 20 75 2e + u.
1ed47 62 7a 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 43 73 bz.pProgram->nCs
1ed48 72 20 2a 20 73 69 7a 65 6f 66 28 56 64 62 65 43 r * sizeof(VdbeC
1ed49 75 72 73 6f 72 20 2a 29 3b 0a 20 20 20 20 75 2e ursor *);. u.
1ed4a 62 7a 2e 70 46 72 61 6d 65 20 3d 20 73 71 6c 69 bz.pFrame = sqli
1ed4b 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 te3DbMallocZero(
1ed4c 64 62 2c 20 75 2e 62 7a 2e 6e 42 79 74 65 29 3b db, u.bz.nByte);
1ed4d 0a 20 20 20 20 69 66 28 20 21 75 2e 62 7a 2e 70 . if( !u.bz.p
1ed4e 46 72 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 67 Frame ){. g
1ed4f 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 oto no_mem;.
1ed50 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 }. sqlite3Vdb
1ed51 65 4d 65 6d 52 65 6c 65 61 73 65 28 75 2e 62 7a eMemRelease(u.bz
1ed52 2e 70 52 74 29 3b 0a 20 20 20 20 75 2e 62 7a 2e .pRt);. u.bz.
1ed53 70 52 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d pRt->flags = MEM
1ed54 5f 46 72 61 6d 65 3b 0a 20 20 20 20 75 2e 62 7a _Frame;. u.bz
1ed55 2e 70 52 74 2d 3e 75 2e 70 46 72 61 6d 65 20 3d .pRt->u.pFrame =
1ed56 20 75 2e 62 7a 2e 70 46 72 61 6d 65 3b 0a 0a 20 u.bz.pFrame;..
1ed57 20 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e u.bz.pFrame->
1ed58 76 20 3d 20 70 3b 0a 20 20 20 20 75 2e 62 7a 2e v = p;. u.bz.
1ed59 70 46 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 4d 65 pFrame->nChildMe
1ed5a 6d 20 3d 20 75 2e 62 7a 2e 6e 4d 65 6d 3b 0a 20 m = u.bz.nMem;.
1ed5b 20 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e u.bz.pFrame->
1ed5c 6e 43 68 69 6c 64 43 73 72 20 3d 20 75 2e 62 7a nChildCsr = u.bz
1ed5d 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 43 73 72 3b .pProgram->nCsr;
1ed5e 0a 20 20 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 . u.bz.pFrame
1ed5f 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20 20 75 ->pc = pc;. u
1ed60 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 61 4d 65 6d .bz.pFrame->aMem
1ed61 20 3d 20 70 2d 3e 61 4d 65 6d 3b 0a 20 20 20 20 = p->aMem;.
1ed62 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e 4d 65 u.bz.pFrame->nMe
1ed63 6d 20 3d 20 70 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 m = p->nMem;.
1ed64 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 61 70 u.bz.pFrame->ap
1ed65 43 73 72 20 3d 20 70 2d 3e 61 70 43 73 72 3b 0a Csr = p->apCsr;.
1ed66 20 20 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d u.bz.pFrame-
1ed67 3e 6e 43 75 72 73 6f 72 20 3d 20 70 2d 3e 6e 43 >nCursor = p->nC
1ed68 75 72 73 6f 72 3b 0a 20 20 20 20 75 2e 62 7a 2e ursor;. u.bz.
1ed69 70 46 72 61 6d 65 2d 3e 61 4f 70 20 3d 20 70 2d pFrame->aOp = p-
1ed6a 3e 61 4f 70 3b 0a 20 20 20 20 75 2e 62 7a 2e 70 >aOp;. u.bz.p
1ed6b 46 72 61 6d 65 2d 3e 6e 4f 70 20 3d 20 70 2d 3e Frame->nOp = p->
1ed6c 6e 4f 70 3b 0a 20 20 20 20 75 2e 62 7a 2e 70 46 nOp;. u.bz.pF
1ed6d 72 61 6d 65 2d 3e 74 6f 6b 65 6e 20 3d 20 75 2e rame->token = u.
1ed6e 62 7a 2e 70 50 72 6f 67 72 61 6d 2d 3e 74 6f 6b bz.pProgram->tok
1ed6f 65 6e 3b 0a 0a 20 20 20 20 75 2e 62 7a 2e 70 45 en;.. u.bz.pE
1ed70 6e 64 20 3d 20 26 56 64 62 65 46 72 61 6d 65 4d nd = &VdbeFrameM
1ed71 65 6d 28 75 2e 62 7a 2e 70 46 72 61 6d 65 29 5b em(u.bz.pFrame)[
1ed72 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 u.bz.pFrame->nCh
1ed73 69 6c 64 4d 65 6d 5d 3b 0a 20 20 20 20 66 6f 72 ildMem];. for
1ed74 28 75 2e 62 7a 2e 70 4d 65 6d 3d 56 64 62 65 46 (u.bz.pMem=VdbeF
1ed75 72 61 6d 65 4d 65 6d 28 75 2e 62 7a 2e 70 46 72 rameMem(u.bz.pFr
1ed76 61 6d 65 29 3b 20 75 2e 62 7a 2e 70 4d 65 6d 21 ame); u.bz.pMem!
1ed77 3d 75 2e 62 7a 2e 70 45 6e 64 3b 20 75 2e 62 7a =u.bz.pEnd; u.bz
1ed78 2e 70 4d 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 .pMem++){.
1ed79 75 2e 62 7a 2e 70 4d 65 6d 2d 3e 66 6c 61 67 73 u.bz.pMem->flags
1ed7a 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 = MEM_Null;.
1ed7b 20 20 20 75 2e 62 7a 2e 70 4d 65 6d 2d 3e 64 62 u.bz.pMem->db
1ed7c 20 3d 20 64 62 3b 0a 20 20 20 20 7d 0a 20 20 7d = db;. }. }
1ed7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 62 7a 2e 70 else{. u.bz.p
1ed7e 46 72 61 6d 65 20 3d 20 75 2e 62 7a 2e 70 52 74 Frame = u.bz.pRt
1ed7f 2d 3e 75 2e 70 46 72 61 6d 65 3b 0a 20 20 20 20 ->u.pFrame;.
1ed80 61 73 73 65 72 74 28 20 75 2e 62 7a 2e 70 50 72 assert( u.bz.pPr
1ed81 6f 67 72 61 6d 2d 3e 6e 4d 65 6d 2b 75 2e 62 7a ogram->nMem+u.bz
1ed82 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 43 73 72 3d .pProgram->nCsr=
1ed83 3d 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e 43 =u.bz.pFrame->nC
1ed84 68 69 6c 64 4d 65 6d 20 29 3b 0a 20 20 20 20 61 hildMem );. a
1ed85 73 73 65 72 74 28 20 75 2e 62 7a 2e 70 50 72 6f ssert( u.bz.pPro
1ed86 67 72 61 6d 2d 3e 6e 43 73 72 3d 3d 75 2e 62 7a gram->nCsr==u.bz
1ed87 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 43 .pFrame->nChildC
1ed88 73 72 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 sr );. assert
1ed89 28 20 70 63 3d 3d 75 2e 62 7a 2e 70 46 72 61 6d ( pc==u.bz.pFram
1ed8a 65 2d 3e 70 63 20 29 3b 0a 20 20 7d 0a 0a 20 20 e->pc );. }..
1ed8b 70 2d 3e 6e 46 72 61 6d 65 2b 2b 3b 0a 20 20 75 p->nFrame++;. u
1ed8c 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 70 50 61 72 .bz.pFrame->pPar
1ed8d 65 6e 74 20 3d 20 70 2d 3e 70 46 72 61 6d 65 3b ent = p->pFrame;
1ed8e 0a 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e . u.bz.pFrame->
1ed8f 6c 61 73 74 52 6f 77 69 64 20 3d 20 64 62 2d 3e lastRowid = db->
1ed90 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20 75 2e 62 lastRowid;. u.b
1ed91 7a 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 61 6e 67 z.pFrame->nChang
1ed92 65 20 3d 20 70 2d 3e 6e 43 68 61 6e 67 65 3b 0a e = p->nChange;.
1ed93 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 30 p->nChange = 0
1ed94 3b 0a 20 20 70 2d 3e 70 46 72 61 6d 65 20 3d 20 ;. p->pFrame =
1ed95 75 2e 62 7a 2e 70 46 72 61 6d 65 3b 0a 20 20 70 u.bz.pFrame;. p
1ed96 2d 3e 61 4d 65 6d 20 3d 20 26 56 64 62 65 46 72 ->aMem = &VdbeFr
1ed97 61 6d 65 4d 65 6d 28 75 2e 62 7a 2e 70 46 72 61 ameMem(u.bz.pFra
1ed98 6d 65 29 5b 2d 31 5d 3b 0a 20 20 70 2d 3e 6e 4d me)[-1];. p->nM
1ed99 65 6d 20 3d 20 75 2e 62 7a 2e 70 46 72 61 6d 65 em = u.bz.pFrame
1ed9a 2d 3e 6e 43 68 69 6c 64 4d 65 6d 3b 0a 20 20 70 ->nChildMem;. p
1ed9b 2d 3e 6e 43 75 72 73 6f 72 20 3d 20 28 75 31 36 ->nCursor = (u16
1ed9c 29 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e 43 )u.bz.pFrame->nC
1ed9d 68 69 6c 64 43 73 72 3b 0a 20 20 70 2d 3e 61 70 hildCsr;. p->ap
1ed9e 43 73 72 20 3d 20 28 56 64 62 65 43 75 72 73 6f Csr = (VdbeCurso
1ed9f 72 20 2a 2a 29 26 70 2d 3e 61 4d 65 6d 5b 70 2d r **)&p->aMem[p-
1eda0 3e 6e 4d 65 6d 2b 31 5d 3b 0a 20 20 70 2d 3e 61 >nMem+1];. p->a
1eda1 4f 70 20 3d 20 75 2e 62 7a 2e 70 50 72 6f 67 72 Op = u.bz.pProgr
1eda2 61 6d 2d 3e 61 4f 70 3b 0a 20 20 70 2d 3e 6e 4f am->aOp;. p->nO
1eda3 70 20 3d 20 75 2e 62 7a 2e 70 50 72 6f 67 72 61 p = u.bz.pProgra
1eda4 6d 2d 3e 6e 4f 70 3b 0a 20 20 70 63 20 3d 20 2d m->nOp;. pc = -
1eda5 31 3b 0a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 1;.. break;.}..
1eda6 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 61 72 61 6d /* Opcode: Param
1eda7 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a P1 P2 * * *.**.
1eda8 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 ** This opcode i
1eda9 73 20 6f 6e 6c 79 20 65 76 65 72 20 70 72 65 73 s only ever pres
1edaa 65 6e 74 20 69 6e 20 73 75 62 2d 70 72 6f 67 72 ent in sub-progr
1edab 61 6d 73 20 63 61 6c 6c 65 64 20 76 69 61 20 74 ams called via t
1edac 68 65 20 0a 2a 2a 20 4f 50 5f 50 72 6f 67 72 61 he .** OP_Progra
1edad 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 43 m instruction. C
1edae 6f 70 79 20 61 20 76 61 6c 75 65 20 63 75 72 72 opy a value curr
1edaf 65 6e 74 6c 79 20 73 74 6f 72 65 64 20 69 6e 20 ently stored in
1edb0 61 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 63 65 6c a memory .** cel
1edb1 6c 20 6f 66 20 74 68 65 20 63 61 6c 6c 69 6e 67 l of the calling
1edb2 20 28 70 61 72 65 6e 74 29 20 66 72 61 6d 65 20 (parent) frame
1edb3 74 6f 20 63 65 6c 6c 20 50 32 20 69 6e 20 74 68 to cell P2 in th
1edb4 65 20 63 75 72 72 65 6e 74 20 66 72 61 6d 65 73 e current frames
1edb5 20 0a 2a 2a 20 61 64 64 72 65 73 73 20 73 70 61 .** address spa
1edb6 63 65 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 ce. This is used
1edb7 20 62 79 20 74 72 69 67 67 65 72 20 70 72 6f 67 by trigger prog
1edb8 72 61 6d 73 20 74 6f 20 61 63 63 65 73 73 20 74 rams to access t
1edb9 68 65 20 6e 65 77 2e 2a 20 0a 2a 2a 20 61 6e 64 he new.* .** and
1edba 20 6f 6c 64 2e 2a 20 76 61 6c 75 65 73 2e 0a 2a old.* values..*
1edbb 2a 0a 2a 2a 20 54 68 65 20 61 64 64 72 65 73 73 *.** The address
1edbc 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 69 6e 20 of the cell in
1edbd 74 68 65 20 70 61 72 65 6e 74 20 66 72 61 6d 65 the parent frame
1edbe 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 is determined b
1edbf 79 20 61 64 64 69 6e 67 0a 2a 2a 20 74 68 65 20 y adding.** the
1edc0 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 31 20 value of the P1
1edc1 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 argument to the
1edc2 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 31 20 value of the P1
1edc3 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 0a argument to the.
1edc4 2a 2a 20 63 61 6c 6c 69 6e 67 20 4f 50 5f 50 72 ** calling OP_Pr
1edc5 6f 67 72 61 6d 20 69 6e 73 74 72 75 63 74 69 6f ogram instructio
1edc6 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 61 n..*/.case OP_Pa
1edc7 72 61 6d 3a 20 7b 20 20 20 20 20 20 20 20 20 20 ram: {
1edc8 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 /* out2-prerele
1edc9 61 73 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a ase */.#if 0 /*
1edca 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1edcb 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 61 moved into u.ca
1edcc 20 2a 2f 0a 20 20 56 64 62 65 46 72 61 6d 65 20 */. VdbeFrame
1edcd 2a 70 46 72 61 6d 65 3b 0a 20 20 4d 65 6d 20 2a *pFrame;. Mem *
1edce 70 49 6e 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c pIn;.#endif /* l
1edcf 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1edd0 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 61 20 2a oved into u.ca *
1edd1 2f 0a 20 20 75 2e 63 61 2e 70 46 72 61 6d 65 20 /. u.ca.pFrame
1edd2 3d 20 70 2d 3e 70 46 72 61 6d 65 3b 0a 20 20 75 = p->pFrame;. u
1edd3 2e 63 61 2e 70 49 6e 20 3d 20 26 75 2e 63 61 2e .ca.pIn = &u.ca.
1edd4 70 46 72 61 6d 65 2d 3e 61 4d 65 6d 5b 70 4f 70 pFrame->aMem[pOp
1edd5 2d 3e 70 31 20 2b 20 75 2e 63 61 2e 70 46 72 61 ->p1 + u.ca.pFra
1edd6 6d 65 2d 3e 61 4f 70 5b 75 2e 63 61 2e 70 46 72 me->aOp[u.ca.pFr
1edd7 61 6d 65 2d 3e 70 63 5d 2e 70 31 5d 3b 0a 20 20 ame->pc].p1];.
1edd8 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 sqlite3VdbeMemSh
1edd9 61 6c 6c 6f 77 43 6f 70 79 28 70 4f 75 74 2c 20 allowCopy(pOut,
1edda 75 2e 63 61 2e 70 49 6e 2c 20 4d 45 4d 5f 45 70 u.ca.pIn, MEM_Ep
1eddb 68 65 6d 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d hem);. break;.}
1eddc 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e ..#endif /* #ifn
1eddd 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1edde 54 52 49 47 47 45 52 20 2a 2f 0a 0a 23 69 66 6e TRIGGER */..#ifn
1eddf 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1ede0 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 2f 2a 20 4f FOREIGN_KEY./* O
1ede1 70 63 6f 64 65 3a 20 46 6b 43 6f 75 6e 74 65 72 pcode: FkCounter
1ede2 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a P1 P2 * * *.**.
1ede3 2a 2a 20 49 6e 63 72 65 6d 65 6e 74 20 61 20 22 ** Increment a "
1ede4 63 6f 6e 73 74 72 61 69 6e 74 20 63 6f 75 6e 74 constraint count
1ede5 65 72 22 20 62 79 20 50 32 20 28 50 32 20 6d 61 er" by P2 (P2 ma
1ede6 79 20 62 65 20 6e 65 67 61 74 69 76 65 20 6f 72 y be negative or
1ede7 20 70 6f 73 69 74 69 76 65 29 2e 0a 2a 2a 20 49 positive)..** I
1ede8 66 20 50 31 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f f P1 is non-zero
1ede9 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 , the database c
1edea 6f 6e 73 74 72 61 69 6e 74 20 63 6f 75 6e 74 65 onstraint counte
1edeb 72 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 r is incremented
1edec 20 0a 2a 2a 20 28 64 65 66 65 72 72 65 64 20 66 .** (deferred f
1eded 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 oreign key const
1edee 72 61 69 6e 74 73 29 2e 20 4f 74 68 65 72 77 69 raints). Otherwi
1edef 73 65 2c 20 69 66 20 50 31 20 69 73 20 7a 65 72 se, if P1 is zer
1edf0 6f 2c 20 74 68 65 20 0a 2a 2a 20 73 74 61 74 65 o, the .** state
1edf1 6d 65 6e 74 20 63 6f 75 6e 74 65 72 20 69 73 20 ment counter is
1edf2 69 6e 63 72 65 6d 65 6e 74 65 64 20 28 69 6d 6d incremented (imm
1edf3 65 64 69 61 74 65 20 66 6f 72 65 69 67 6e 20 6b ediate foreign k
1edf4 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 29 2e ey constraints).
1edf5 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 46 6b 43 6f .*/.case OP_FkCo
1edf6 75 6e 74 65 72 3a 20 7b 0a 20 20 69 66 28 20 70 unter: {. if( p
1edf7 4f 70 2d 3e 70 31 20 29 7b 0a 20 20 20 20 64 62 Op->p1 ){. db
1edf8 2d 3e 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 20 ->nDeferredCons
1edf9 2b 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 7d 65 += pOp->p2;. }e
1edfa 6c 73 65 7b 0a 20 20 20 20 70 2d 3e 6e 46 6b 43 lse{. p->nFkC
1edfb 6f 6e 73 74 72 61 69 6e 74 20 2b 3d 20 70 4f 70 onstraint += pOp
1edfc 2d 3e 70 32 3b 0a 20 20 7d 0a 20 20 62 72 65 61 ->p2;. }. brea
1edfd 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1edfe 20 46 6b 49 66 5a 65 72 6f 20 50 31 20 50 32 20 FkIfZero P1 P2
1edff 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 * * *.**.** This
1ee00 20 6f 70 63 6f 64 65 20 74 65 73 74 73 20 69 66 opcode tests if
1ee01 20 61 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 a foreign key c
1ee02 6f 6e 73 74 72 61 69 6e 74 2d 63 6f 75 6e 74 65 onstraint-counte
1ee03 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 7a r is currently z
1ee04 65 72 6f 2e 0a 2a 2a 20 49 66 20 73 6f 2c 20 6a ero..** If so, j
1ee05 75 6d 70 20 74 6f 20 69 6e 73 74 72 75 63 74 69 ump to instructi
1ee06 6f 6e 20 50 32 2e 20 4f 74 68 65 72 77 69 73 65 on P2. Otherwise
1ee07 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 , fall through t
1ee08 6f 20 74 68 65 20 6e 65 78 74 20 0a 2a 2a 20 69 o the next .** i
1ee09 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a nstruction..**.*
1ee0a 2a 20 49 66 20 50 31 20 69 73 20 6e 6f 6e 2d 7a * If P1 is non-z
1ee0b 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 6a 75 ero, then the ju
1ee0c 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66 20 74 mp is taken if t
1ee0d 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 73 he database cons
1ee0e 74 72 61 69 6e 74 2d 63 6f 75 6e 74 65 72 0a 2a traint-counter.*
1ee0f 2a 20 69 73 20 7a 65 72 6f 20 28 74 68 65 20 6f * is zero (the o
1ee10 6e 65 20 74 68 61 74 20 63 6f 75 6e 74 73 20 64 ne that counts d
1ee11 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61 69 eferred constrai
1ee12 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 73 29 2e 20 nt violations).
1ee13 49 66 20 50 31 20 69 73 0a 2a 2a 20 7a 65 72 6f If P1 is.** zero
1ee14 2c 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61 , the jump is ta
1ee15 6b 65 6e 20 69 66 20 74 68 65 20 73 74 61 74 65 ken if the state
1ee16 6d 65 6e 74 20 63 6f 6e 73 74 72 61 69 6e 74 2d ment constraint-
1ee17 63 6f 75 6e 74 65 72 20 69 73 20 7a 65 72 6f 0a counter is zero.
1ee18 2a 2a 20 28 69 6d 6d 65 64 69 61 74 65 20 66 6f ** (immediate fo
1ee19 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 reign key constr
1ee1a 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 73 29 aint violations)
1ee1b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 46 6b 49 ..*/.case OP_FkI
1ee1c 66 5a 65 72 6f 3a 20 7b 20 20 20 20 20 20 20 20 fZero: {
1ee1d 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 69 66 /* jump */. if
1ee1e 28 20 70 4f 70 2d 3e 70 31 20 29 7b 0a 20 20 20 ( pOp->p1 ){.
1ee1f 20 69 66 28 20 64 62 2d 3e 6e 44 65 66 65 72 72 if( db->nDeferr
1ee20 65 64 43 6f 6e 73 3d 3d 30 20 29 20 70 63 20 3d edCons==0 ) pc =
1ee21 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 7d 65 pOp->p2-1;. }e
1ee22 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e lse{. if( p->
1ee23 6e 46 6b 43 6f 6e 73 74 72 61 69 6e 74 3d 3d 30 nFkConstraint==0
1ee24 20 29 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d ) pc = pOp->p2-
1ee25 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 1;. }. break;.
1ee26 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e }.#endif /* #ifn
1ee27 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1ee28 46 4f 52 45 49 47 4e 5f 4b 45 59 20 2a 2f 0a 0a FOREIGN_KEY */..
1ee29 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1ee2a 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e MIT_AUTOINCREMEN
1ee2b 54 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 65 6d T./* Opcode: Mem
1ee2c 4d 61 78 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a Max P1 P2 * * *.
1ee2d 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61 20 72 65 **.** P1 is a re
1ee2e 67 69 73 74 65 72 20 69 6e 20 74 68 65 20 72 6f gister in the ro
1ee2f 6f 74 20 66 72 61 6d 65 20 6f 66 20 74 68 69 73 ot frame of this
1ee30 20 56 4d 20 28 74 68 65 20 72 6f 6f 74 20 66 72 VM (the root fr
1ee31 61 6d 65 20 69 73 0a 2a 2a 20 64 69 66 66 65 72 ame is.** differ
1ee32 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 63 75 72 ent from the cur
1ee33 72 65 6e 74 20 66 72 61 6d 65 20 69 66 20 74 68 rent frame if th
1ee34 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 is instruction i
1ee35 73 20 62 65 69 6e 67 20 65 78 65 63 75 74 65 64 s being executed
1ee36 0a 2a 2a 20 77 69 74 68 69 6e 20 61 20 73 75 62 .** within a sub
1ee37 2d 70 72 6f 67 72 61 6d 29 2e 20 53 65 74 20 74 -program). Set t
1ee38 68 65 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69 he value of regi
1ee39 73 74 65 72 20 50 31 20 74 6f 20 74 68 65 20 6d ster P1 to the m
1ee3a 61 78 69 6d 75 6d 20 6f 66 20 0a 2a 2a 20 69 74 aximum of .** it
1ee3b 73 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 s current value
1ee3c 61 6e 64 20 74 68 65 20 76 61 6c 75 65 20 69 6e and the value in
1ee3d 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a register P2..**
1ee3e 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 .** This instruc
1ee3f 74 69 6f 6e 20 74 68 72 6f 77 73 20 61 6e 20 65 tion throws an e
1ee40 72 72 6f 72 20 69 66 20 74 68 65 20 6d 65 6d 6f rror if the memo
1ee41 72 79 20 63 65 6c 6c 20 69 73 20 6e 6f 74 20 69 ry cell is not i
1ee42 6e 69 74 69 61 6c 6c 79 0a 2a 2a 20 61 6e 20 69 nitially.** an i
1ee43 6e 74 65 67 65 72 2e 0a 2a 2f 0a 63 61 73 65 20 nteger..*/.case
1ee44 4f 50 5f 4d 65 6d 4d 61 78 3a 20 7b 20 20 20 20 OP_MemMax: {
1ee45 20 20 20 20 2f 2a 20 69 6e 32 20 2a 2f 0a 23 69 /* in2 */.#i
1ee46 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1ee47 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1ee48 74 6f 20 75 2e 63 62 20 2a 2f 0a 20 20 4d 65 6d to u.cb */. Mem
1ee49 20 2a 70 49 6e 31 3b 0a 20 20 56 64 62 65 46 72 *pIn1;. VdbeFr
1ee4a 61 6d 65 20 2a 70 46 72 61 6d 65 3b 0a 23 65 6e ame *pFrame;.#en
1ee4b 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 dif /* local var
1ee4c 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1ee4d 6f 20 75 2e 63 62 20 2a 2f 0a 20 20 69 66 28 20 o u.cb */. if(
1ee4e 70 2d 3e 70 46 72 61 6d 65 20 29 7b 0a 20 20 20 p->pFrame ){.
1ee4f 20 66 6f 72 28 75 2e 63 62 2e 70 46 72 61 6d 65 for(u.cb.pFrame
1ee50 3d 70 2d 3e 70 46 72 61 6d 65 3b 20 75 2e 63 62 =p->pFrame; u.cb
1ee51 2e 70 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 .pFrame->pParent
1ee52 3b 20 75 2e 63 62 2e 70 46 72 61 6d 65 3d 75 2e ; u.cb.pFrame=u.
1ee53 63 62 2e 70 46 72 61 6d 65 2d 3e 70 50 61 72 65 cb.pFrame->pPare
1ee54 6e 74 29 3b 0a 20 20 20 20 75 2e 63 62 2e 70 49 nt);. u.cb.pI
1ee55 6e 31 20 3d 20 26 75 2e 63 62 2e 70 46 72 61 6d n1 = &u.cb.pFram
1ee56 65 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d e->aMem[pOp->p1]
1ee57 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 ;. }else{. u
1ee58 2e 63 62 2e 70 49 6e 31 20 3d 20 26 70 2d 3e 61 .cb.pIn1 = &p->a
1ee59 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Mem[pOp->p1];.
1ee5a 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d }. sqlite3VdbeM
1ee5b 65 6d 49 6e 74 65 67 65 72 69 66 79 28 75 2e 63 emIntegerify(u.c
1ee5c 62 2e 70 49 6e 31 29 3b 0a 20 20 73 71 6c 69 74 b.pIn1);. sqlit
1ee5d 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 e3VdbeMemInteger
1ee5e 69 66 79 28 70 49 6e 32 29 3b 0a 20 20 69 66 28 ify(pIn2);. if(
1ee5f 20 75 2e 63 62 2e 70 49 6e 31 2d 3e 75 2e 69 3c u.cb.pIn1->u.i<
1ee60 70 49 6e 32 2d 3e 75 2e 69 29 7b 0a 20 20 20 20 pIn2->u.i){.
1ee61 75 2e 63 62 2e 70 49 6e 31 2d 3e 75 2e 69 20 3d u.cb.pIn1->u.i =
1ee62 20 70 49 6e 32 2d 3e 75 2e 69 3b 0a 20 20 7d 0a pIn2->u.i;. }.
1ee63 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 break;.}.#endi
1ee64 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
1ee65 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a _AUTOINCREMENT *
1ee66 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66 /../* Opcode: If
1ee67 50 6f 73 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a Pos P1 P2 * * *.
1ee68 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c **.** If the val
1ee69 75 65 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 ue of register P
1ee6a 31 20 69 73 20 31 20 6f 72 20 67 72 65 61 74 65 1 is 1 or greate
1ee6b 72 2c 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a r, jump to P2..*
1ee6c 2a 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 *.** It is illeg
1ee6d 61 6c 20 74 6f 20 75 73 65 20 74 68 69 73 20 69 al to use this i
1ee6e 6e 73 74 72 75 63 74 69 6f 6e 20 6f 6e 20 61 20 nstruction on a
1ee6f 72 65 67 69 73 74 65 72 20 74 68 61 74 20 64 6f register that do
1ee70 65 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 es.** not contai
1ee71 6e 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 41 n an integer. A
1ee72 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c n assertion faul
1ee73 74 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 66 t will result if
1ee74 20 79 6f 75 20 74 72 79 2e 0a 2a 2f 0a 63 61 73 you try..*/.cas
1ee75 65 20 4f 50 5f 49 66 50 6f 73 3a 20 7b 20 20 20 e OP_IfPos: {
1ee76 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e /* jump, in
1ee77 31 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 1 */. assert( p
1ee78 49 6e 31 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 In1->flags&MEM_I
1ee79 6e 74 20 29 3b 0a 20 20 69 66 28 20 70 49 6e 31 nt );. if( pIn1
1ee7a 2d 3e 75 2e 69 3e 30 20 29 7b 0a 20 20 20 20 20 ->u.i>0 ){.
1ee7b 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 pc = pOp->p2 - 1
1ee7c 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1ee7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66 4e ../* Opcode: IfN
1ee7e 65 67 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a eg P1 P2 * * *.*
1ee7f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 *.** If the valu
1ee80 65 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 e of register P1
1ee81 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 is less than ze
1ee82 72 6f 2c 20 6a 75 6d 70 20 74 6f 20 50 32 2e 20 ro, jump to P2.
1ee83 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c .**.** It is ill
1ee84 65 67 61 6c 20 74 6f 20 75 73 65 20 74 68 69 73 egal to use this
1ee85 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 6e 20 instruction on
1ee86 61 20 72 65 67 69 73 74 65 72 20 74 68 61 74 20 a register that
1ee87 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74 does.** not cont
1ee88 61 69 6e 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 ain an integer.
1ee89 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 An assertion fa
1ee8a 75 6c 74 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 ult will result
1ee8b 69 66 20 79 6f 75 20 74 72 79 2e 0a 2a 2f 0a 63 if you try..*/.c
1ee8c 61 73 65 20 4f 50 5f 49 66 4e 65 67 3a 20 7b 20 ase OP_IfNeg: {
1ee8d 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 /* jump,
1ee8e 69 6e 31 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 in1 */. assert(
1ee8f 20 70 49 6e 31 2d 3e 66 6c 61 67 73 26 4d 45 4d pIn1->flags&MEM
1ee90 5f 49 6e 74 20 29 3b 0a 20 20 69 66 28 20 70 49 _Int );. if( pI
1ee91 6e 31 2d 3e 75 2e 69 3c 30 20 29 7b 0a 20 20 20 n1->u.i<0 ){.
1ee92 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d pc = pOp->p2 -
1ee93 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 1;. }. break;
1ee94 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 .}../* Opcode: I
1ee95 66 5a 65 72 6f 20 50 31 20 50 32 20 2a 20 2a 20 fZero P1 P2 * *
1ee96 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 76 *.**.** If the v
1ee97 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 65 72 alue of register
1ee98 20 50 31 20 69 73 20 65 78 61 63 74 6c 79 20 30 P1 is exactly 0
1ee99 2c 20 6a 75 6d 70 20 74 6f 20 50 32 2e 20 0a 2a , jump to P2. .*
1ee9a 2a 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 *.** It is illeg
1ee9b 61 6c 20 74 6f 20 75 73 65 20 74 68 69 73 20 69 al to use this i
1ee9c 6e 73 74 72 75 63 74 69 6f 6e 20 6f 6e 20 61 20 nstruction on a
1ee9d 72 65 67 69 73 74 65 72 20 74 68 61 74 20 64 6f register that do
1ee9e 65 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 es.** not contai
1ee9f 6e 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 41 n an integer. A
1eea0 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c n assertion faul
1eea1 74 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 66 t will result if
1eea2 20 79 6f 75 20 74 72 79 2e 0a 2a 2f 0a 63 61 73 you try..*/.cas
1eea3 65 20 4f 50 5f 49 66 5a 65 72 6f 3a 20 7b 20 20 e OP_IfZero: {
1eea4 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 /* jump, i
1eea5 6e 31 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 n1 */. assert(
1eea6 70 49 6e 31 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f pIn1->flags&MEM_
1eea7 49 6e 74 20 29 3b 0a 20 20 69 66 28 20 70 49 6e Int );. if( pIn
1eea8 31 2d 3e 75 2e 69 3d 3d 30 20 29 7b 0a 20 20 20 1->u.i==0 ){.
1eea9 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d pc = pOp->p2 -
1eeaa 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 1;. }. break;
1eeab 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 .}../* Opcode: A
1eeac 67 67 53 74 65 70 20 2a 20 50 32 20 50 33 20 50 ggStep * P2 P3 P
1eead 34 20 50 35 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 4 P5.**.** Execu
1eeae 74 65 20 74 68 65 20 73 74 65 70 20 66 75 6e 63 te the step func
1eeaf 74 69 6f 6e 20 66 6f 72 20 61 6e 20 61 67 67 72 tion for an aggr
1eeb0 65 67 61 74 65 2e 20 20 54 68 65 0a 2a 2a 20 66 egate. The.** f
1eeb1 75 6e 63 74 69 6f 6e 20 68 61 73 20 50 35 20 61 unction has P5 a
1eeb2 72 67 75 6d 65 6e 74 73 2e 20 20 20 50 34 20 69 rguments. P4 i
1eeb3 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 s a pointer to t
1eeb4 68 65 20 46 75 6e 63 44 65 66 0a 2a 2a 20 73 74 he FuncDef.** st
1eeb5 72 75 63 74 75 72 65 20 74 68 61 74 20 73 70 65 ructure that spe
1eeb6 63 69 66 69 65 73 20 74 68 65 20 66 75 6e 63 74 cifies the funct
1eeb7 69 6f 6e 2e 20 20 55 73 65 20 72 65 67 69 73 74 ion. Use regist
1eeb8 65 72 0a 2a 2a 20 50 33 20 61 73 20 74 68 65 20 er.** P3 as the
1eeb9 61 63 63 75 6d 75 6c 61 74 6f 72 2e 0a 2a 2a 0a accumulator..**.
1eeba 2a 2a 20 54 68 65 20 50 35 20 61 72 67 75 6d 65 ** The P5 argume
1eebb 6e 74 73 20 61 72 65 20 74 61 6b 65 6e 20 66 72 nts are taken fr
1eebc 6f 6d 20 72 65 67 69 73 74 65 72 20 50 32 20 61 om register P2 a
1eebd 6e 64 20 69 74 73 0a 2a 2a 20 73 75 63 63 65 73 nd its.** succes
1eebe 73 6f 72 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 sors..*/.case OP
1eebf 5f 41 67 67 53 74 65 70 3a 20 7b 0a 23 69 66 20 _AggStep: {.#if
1eec0 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1eec1 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1eec2 20 75 2e 63 63 20 2a 2f 0a 20 20 69 6e 74 20 6e u.cc */. int n
1eec3 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4d 65 6d ;. int i;. Mem
1eec4 20 2a 70 4d 65 6d 3b 0a 20 20 4d 65 6d 20 2a 70 *pMem;. Mem *p
1eec5 52 65 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 Rec;. sqlite3_c
1eec6 6f 6e 74 65 78 74 20 63 74 78 3b 0a 20 20 73 71 ontext ctx;. sq
1eec7 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 lite3_value **ap
1eec8 56 61 6c 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c Val;.#endif /* l
1eec9 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1eeca 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 63 20 2a oved into u.cc *
1eecb 2f 0a 0a 20 20 75 2e 63 63 2e 6e 20 3d 20 70 4f /.. u.cc.n = pO
1eecc 70 2d 3e 70 35 3b 0a 20 20 61 73 73 65 72 74 28 p->p5;. assert(
1eecd 20 75 2e 63 63 2e 6e 3e 3d 30 20 29 3b 0a 20 20 u.cc.n>=0 );.
1eece 75 2e 63 63 2e 70 52 65 63 20 3d 20 26 70 2d 3e u.cc.pRec = &p->
1eecf 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 aMem[pOp->p2];.
1eed0 20 75 2e 63 63 2e 61 70 56 61 6c 20 3d 20 70 2d u.cc.apVal = p-
1eed1 3e 61 70 41 72 67 3b 0a 20 20 61 73 73 65 72 74 >apArg;. assert
1eed2 28 20 75 2e 63 63 2e 61 70 56 61 6c 20 7c 7c 20 ( u.cc.apVal ||
1eed3 75 2e 63 63 2e 6e 3d 3d 30 20 29 3b 0a 20 20 66 u.cc.n==0 );. f
1eed4 6f 72 28 75 2e 63 63 2e 69 3d 30 3b 20 75 2e 63 or(u.cc.i=0; u.c
1eed5 63 2e 69 3c 75 2e 63 63 2e 6e 3b 20 75 2e 63 63 c.i<u.cc.n; u.cc
1eed6 2e 69 2b 2b 2c 20 75 2e 63 63 2e 70 52 65 63 2b .i++, u.cc.pRec+
1eed7 2b 29 7b 0a 20 20 20 20 75 2e 63 63 2e 61 70 56 +){. u.cc.apV
1eed8 61 6c 5b 75 2e 63 63 2e 69 5d 20 3d 20 75 2e 63 al[u.cc.i] = u.c
1eed9 63 2e 70 52 65 63 3b 0a 20 20 20 20 73 71 6c 69 c.pRec;. sqli
1eeda 74 65 33 56 64 62 65 4d 65 6d 53 74 6f 72 65 54 te3VdbeMemStoreT
1eedb 79 70 65 28 75 2e 63 63 2e 70 52 65 63 29 3b 0a ype(u.cc.pRec);.
1eedc 20 20 7d 0a 20 20 75 2e 63 63 2e 63 74 78 2e 70 }. u.cc.ctx.p
1eedd 46 75 6e 63 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 Func = pOp->p4.p
1eede 46 75 6e 63 3b 0a 20 20 61 73 73 65 72 74 28 20 Func;. assert(
1eedf 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 pOp->p3>0 && pOp
1eee0 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b ->p3<=p->nMem );
1eee1 0a 20 20 75 2e 63 63 2e 63 74 78 2e 70 4d 65 6d . u.cc.ctx.pMem
1eee2 20 3d 20 75 2e 63 63 2e 70 4d 65 6d 20 3d 20 26 = u.cc.pMem = &
1eee3 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d p->aMem[pOp->p3]
1eee4 3b 0a 20 20 75 2e 63 63 2e 70 4d 65 6d 2d 3e 6e ;. u.cc.pMem->n
1eee5 2b 2b 3b 0a 20 20 75 2e 63 63 2e 63 74 78 2e 73 ++;. u.cc.ctx.s
1eee6 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c .flags = MEM_Nul
1eee7 6c 3b 0a 20 20 75 2e 63 63 2e 63 74 78 2e 73 2e l;. u.cc.ctx.s.
1eee8 7a 20 3d 20 30 3b 0a 20 20 75 2e 63 63 2e 63 74 z = 0;. u.cc.ct
1eee9 78 2e 73 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b x.s.zMalloc = 0;
1eeea 0a 20 20 75 2e 63 63 2e 63 74 78 2e 73 2e 78 44 . u.cc.ctx.s.xD
1eeeb 65 6c 20 3d 20 30 3b 0a 20 20 75 2e 63 63 2e 63 el = 0;. u.cc.c
1eeec 74 78 2e 73 2e 64 62 20 3d 20 64 62 3b 0a 20 20 tx.s.db = db;.
1eeed 75 2e 63 63 2e 63 74 78 2e 69 73 45 72 72 6f 72 u.cc.ctx.isError
1eeee 20 3d 20 30 3b 0a 20 20 75 2e 63 63 2e 63 74 78 = 0;. u.cc.ctx
1eeef 2e 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 69 66 .pColl = 0;. if
1eef0 28 20 75 2e 63 63 2e 63 74 78 2e 70 46 75 6e 63 ( u.cc.ctx.pFunc
1eef1 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 ->flags & SQLITE
1eef2 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 20 29 _FUNC_NEEDCOLL )
1eef3 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f {. assert( pO
1eef4 70 3e 70 2d 3e 61 4f 70 20 29 3b 0a 20 20 20 20 p>p->aOp );.
1eef5 61 73 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d 2e assert( pOp[-1].
1eef6 70 34 74 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 p4type==P4_COLLS
1eef7 45 51 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 EQ );. assert
1eef8 28 20 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65 ( pOp[-1].opcode
1eef9 3d 3d 4f 50 5f 43 6f 6c 6c 53 65 71 20 29 3b 0a ==OP_CollSeq );.
1eefa 20 20 20 20 75 2e 63 63 2e 63 74 78 2e 70 43 6f u.cc.ctx.pCo
1eefb 6c 6c 20 3d 20 70 4f 70 5b 2d 31 5d 2e 70 34 2e ll = pOp[-1].p4.
1eefc 70 43 6f 6c 6c 3b 0a 20 20 7d 0a 20 20 28 75 2e pColl;. }. (u.
1eefd 63 63 2e 63 74 78 2e 70 46 75 6e 63 2d 3e 78 53 cc.ctx.pFunc->xS
1eefe 74 65 70 29 28 26 75 2e 63 63 2e 63 74 78 2c 20 tep)(&u.cc.ctx,
1eeff 75 2e 63 63 2e 6e 2c 20 75 2e 63 63 2e 61 70 56 u.cc.n, u.cc.apV
1ef00 61 6c 29 3b 0a 20 20 69 66 28 20 75 2e 63 63 2e al);. if( u.cc.
1ef01 63 74 78 2e 69 73 45 72 72 6f 72 20 29 7b 0a 20 ctx.isError ){.
1ef02 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 sqlite3SetStr
1ef03 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c ing(&p->zErrMsg,
1ef04 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 db, "%s", sqlit
1ef05 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 26 75 e3_value_text(&u
1ef06 2e 63 63 2e 63 74 78 2e 73 29 29 3b 0a 20 20 20 .cc.ctx.s));.
1ef07 20 72 63 20 3d 20 75 2e 63 63 2e 63 74 78 2e 69 rc = u.cc.ctx.i
1ef08 73 45 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 73 71 sError;. }. sq
1ef09 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
1ef0a 61 73 65 28 26 75 2e 63 63 2e 63 74 78 2e 73 29 ase(&u.cc.ctx.s)
1ef0b 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a ;. break;.}../*
1ef0c 20 4f 70 63 6f 64 65 3a 20 41 67 67 46 69 6e 61 Opcode: AggFina
1ef0d 6c 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a l P1 P2 * P4 *.*
1ef0e 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 74 68 65 *.** Execute the
1ef0f 20 66 69 6e 61 6c 69 7a 65 72 20 66 75 6e 63 74 finalizer funct
1ef10 69 6f 6e 20 66 6f 72 20 61 6e 20 61 67 67 72 65 ion for an aggre
1ef11 67 61 74 65 2e 20 20 50 31 20 69 73 0a 2a 2a 20 gate. P1 is.**
1ef12 74 68 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 the memory locat
1ef13 69 6f 6e 20 74 68 61 74 20 69 73 20 74 68 65 20 ion that is the
1ef14 61 63 63 75 6d 75 6c 61 74 6f 72 20 66 6f 72 20 accumulator for
1ef15 74 68 65 20 61 67 67 72 65 67 61 74 65 2e 0a 2a the aggregate..*
1ef16 2a 0a 2a 2a 20 50 32 20 69 73 20 74 68 65 20 6e *.** P2 is the n
1ef17 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e umber of argumen
1ef18 74 73 20 74 68 61 74 20 74 68 65 20 73 74 65 70 ts that the step
1ef19 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b 65 73 20 function takes
1ef1a 61 6e 64 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 and.** P4 is a p
1ef1b 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 46 75 ointer to the Fu
1ef1c 6e 63 44 65 66 20 66 6f 72 20 74 68 69 73 20 66 ncDef for this f
1ef1d 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 50 32 unction. The P2
1ef1e 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 73 20 .** argument is
1ef1f 6e 6f 74 20 75 73 65 64 20 62 79 20 74 68 69 73 not used by this
1ef20 20 6f 70 63 6f 64 65 2e 20 20 49 74 20 69 73 20 opcode. It is
1ef21 6f 6e 6c 79 20 74 68 65 72 65 20 74 6f 20 64 69 only there to di
1ef22 73 61 6d 62 69 67 75 61 74 65 0a 2a 2a 20 66 75 sambiguate.** fu
1ef23 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 63 61 6e nctions that can
1ef24 20 74 61 6b 65 20 76 61 72 79 69 6e 67 20 6e 75 take varying nu
1ef25 6d 62 65 72 73 20 6f 66 20 61 72 67 75 6d 65 6e mbers of argumen
1ef26 74 73 2e 20 20 54 68 65 0a 2a 2a 20 50 34 20 61 ts. The.** P4 a
1ef27 72 67 75 6d 65 6e 74 20 69 73 20 6f 6e 6c 79 20 rgument is only
1ef28 6e 65 65 64 65 64 20 66 6f 72 20 74 68 65 20 64 needed for the d
1ef29 65 67 65 6e 65 72 61 74 65 20 63 61 73 65 20 77 egenerate case w
1ef2a 68 65 72 65 0a 2a 2a 20 74 68 65 20 73 74 65 70 here.** the step
1ef2b 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 6e 6f function was no
1ef2c 74 20 70 72 65 76 69 6f 75 73 6c 79 20 63 61 6c t previously cal
1ef2d 6c 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f led..*/.case OP_
1ef2e 41 67 67 46 69 6e 61 6c 3a 20 7b 0a 23 69 66 20 AggFinal: {.#if
1ef2f 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1ef30 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1ef31 20 75 2e 63 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a u.cd */. Mem *
1ef32 70 4d 65 6d 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 pMem;.#endif /*
1ef33 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1ef34 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 64 20 moved into u.cd
1ef35 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 */. assert( pOp
1ef36 2d 3e 70 31 3e 30 20 26 26 20 70 4f 70 2d 3e 70 ->p1>0 && pOp->p
1ef37 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 1<=p->nMem );.
1ef38 75 2e 63 64 2e 70 4d 65 6d 20 3d 20 26 70 2d 3e u.cd.pMem = &p->
1ef39 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 aMem[pOp->p1];.
1ef3a 20 61 73 73 65 72 74 28 20 28 75 2e 63 64 2e 70 assert( (u.cd.p
1ef3b 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 7e 28 4d Mem->flags & ~(M
1ef3c 45 4d 5f 4e 75 6c 6c 7c 4d 45 4d 5f 41 67 67 29 EM_Null|MEM_Agg)
1ef3d 29 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 )==0 );. rc = s
1ef3e 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 69 6e qlite3VdbeMemFin
1ef3f 61 6c 69 7a 65 28 75 2e 63 64 2e 70 4d 65 6d 2c alize(u.cd.pMem,
1ef40 20 70 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 29 3b pOp->p4.pFunc);
1ef41 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 . if( rc ){.
1ef42 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e sqlite3SetStrin
1ef43 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 g(&p->zErrMsg, d
1ef44 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33 b, "%s", sqlite3
1ef45 5f 76 61 6c 75 65 5f 74 65 78 74 28 75 2e 63 64 _value_text(u.cd
1ef46 2e 70 4d 65 6d 29 29 3b 0a 20 20 7d 0a 20 20 73 .pMem));. }. s
1ef47 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
1ef48 45 6e 63 6f 64 69 6e 67 28 75 2e 63 64 2e 70 4d Encoding(u.cd.pM
1ef49 65 6d 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 em, encoding);.
1ef4a 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 UPDATE_MAX_BLOB
1ef4b 53 49 5a 45 28 75 2e 63 64 2e 70 4d 65 6d 29 3b SIZE(u.cd.pMem);
1ef4c 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 . if( sqlite3Vd
1ef4d 62 65 4d 65 6d 54 6f 6f 42 69 67 28 75 2e 63 64 beMemTooBig(u.cd
1ef4e 2e 70 4d 65 6d 29 20 29 7b 0a 20 20 20 20 67 6f .pMem) ){. go
1ef4f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a to too_big;. }.
1ef50 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 23 69 66 break;.}...#if
1ef51 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
1ef52 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 29 20 26 26 _OMIT_VACUUM) &&
1ef53 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
1ef54 5f 4f 4d 49 54 5f 41 54 54 41 43 48 29 0a 2f 2a _OMIT_ATTACH)./*
1ef55 20 4f 70 63 6f 64 65 3a 20 56 61 63 75 75 6d 20 Opcode: Vacuum
1ef56 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 * * * * *.**.**
1ef57 56 61 63 75 75 6d 20 74 68 65 20 65 6e 74 69 72 Vacuum the entir
1ef58 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 69 e database. Thi
1ef59 73 20 6f 70 63 6f 64 65 20 77 69 6c 6c 20 63 61 s opcode will ca
1ef5a 75 73 65 20 6f 74 68 65 72 20 76 69 72 74 75 61 use other virtua
1ef5b 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 73 20 74 6f l.** machines to
1ef5c 20 62 65 20 63 72 65 61 74 65 64 20 61 6e 64 20 be created and
1ef5d 72 75 6e 2e 20 20 49 74 20 6d 61 79 20 6e 6f 74 run. It may not
1ef5e 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 be called from
1ef5f 77 69 74 68 69 6e 0a 2a 2a 20 61 20 74 72 61 6e within.** a tran
1ef60 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 saction..*/.case
1ef61 20 4f 50 5f 56 61 63 75 75 6d 3a 20 7b 0a 20 20 OP_Vacuum: {.
1ef62 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 if( sqlite3Safet
1ef63 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 yOff(db) ) goto
1ef64 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 abort_due_to_mis
1ef65 75 73 65 3b 20 0a 20 20 72 63 20 3d 20 73 71 6c use; . rc = sql
1ef66 69 74 65 33 52 75 6e 56 61 63 75 75 6d 28 26 70 ite3RunVacuum(&p
1ef67 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 29 3b 0a ->zErrMsg, db);.
1ef68 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 if( sqlite3Saf
1ef69 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f etyOn(db) ) goto
1ef6a 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 abort_due_to_mi
1ef6b 73 75 73 65 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d suse;. break;.}
1ef6c 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 21 64 65 .#endif..#if !de
1ef6d 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
1ef6e 54 5f 41 55 54 4f 56 41 43 55 55 4d 29 0a 2f 2a T_AUTOVACUUM)./*
1ef6f 20 4f 70 63 6f 64 65 3a 20 49 6e 63 72 56 61 63 Opcode: IncrVac
1ef70 75 75 6d 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a uum P1 P2 * * *.
1ef71 2a 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20 **.** Perform a
1ef72 73 69 6e 67 6c 65 20 73 74 65 70 20 6f 66 20 74 single step of t
1ef73 68 65 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 he incremental v
1ef74 61 63 75 75 6d 20 70 72 6f 63 65 64 75 72 65 20 acuum procedure
1ef75 6f 6e 0a 2a 2a 20 74 68 65 20 50 31 20 64 61 74 on.** the P1 dat
1ef76 61 62 61 73 65 2e 20 49 66 20 74 68 65 20 76 61 abase. If the va
1ef77 63 75 75 6d 20 68 61 73 20 66 69 6e 69 73 68 65 cuum has finishe
1ef78 64 2c 20 6a 75 6d 70 20 74 6f 20 69 6e 73 74 72 d, jump to instr
1ef79 75 63 74 69 6f 6e 0a 2a 2a 20 50 32 2e 20 4f 74 uction.** P2. Ot
1ef7a 68 65 72 77 69 73 65 2c 20 66 61 6c 6c 20 74 68 herwise, fall th
1ef7b 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 rough to the nex
1ef7c 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a t instruction..*
1ef7d 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 63 72 56 61 /.case OP_IncrVa
1ef7e 63 75 75 6d 3a 20 7b 20 20 20 20 20 20 20 20 2f cuum: { /
1ef7f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 66 20 30 20 * jump */.#if 0
1ef80 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1ef81 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1ef82 2e 63 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a .ce */. Btree *
1ef83 70 42 74 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c pBt;.#endif /* l
1ef84 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1ef85 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 65 20 2a oved into u.ce *
1ef86 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 /.. assert( pOp
1ef87 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e ->p1>=0 && pOp->
1ef88 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 p1<db->nDb );.
1ef89 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 assert( (p->btre
1ef8a 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d eMask & (1<<pOp-
1ef8b 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20 20 75 2e >p1))!=0 );. u.
1ef8c 63 65 2e 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 ce.pBt = db->aDb
1ef8d 5b 70 4f 70 2d 3e 70 31 5d 2e 70 42 74 3b 0a 20 [pOp->p1].pBt;.
1ef8e 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1ef8f 65 65 49 6e 63 72 56 61 63 75 75 6d 28 75 2e 63 eeIncrVacuum(u.c
1ef90 65 2e 70 42 74 29 3b 0a 20 20 69 66 28 20 72 63 e.pBt);. if( rc
1ef91 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b ==SQLITE_DONE ){
1ef92 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 . pc = pOp->p
1ef93 32 20 2d 20 31 3b 0a 20 20 20 20 72 63 20 3d 20 2 - 1;. rc =
1ef94 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 SQLITE_OK;. }.
1ef95 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 break;.}.#endif
1ef96 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 45 78 70 ../* Opcode: Exp
1ef97 69 72 65 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a ire P1 * * * *.*
1ef98 2a 0a 2a 2a 20 43 61 75 73 65 20 70 72 65 63 6f *.** Cause preco
1ef99 6d 70 69 6c 65 64 20 73 74 61 74 65 6d 65 6e 74 mpiled statement
1ef9a 73 20 74 6f 20 62 65 63 6f 6d 65 20 65 78 70 69 s to become expi
1ef9b 72 65 64 2e 20 41 6e 20 65 78 70 69 72 65 64 20 red. An expired
1ef9c 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 66 61 69 statement.** fai
1ef9d 6c 73 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 ls with an error
1ef9e 20 63 6f 64 65 20 6f 66 20 53 51 4c 49 54 45 5f code of SQLITE_
1ef9f 53 43 48 45 4d 41 20 69 66 20 69 74 20 69 73 20 SCHEMA if it is
1efa0 65 76 65 72 20 65 78 65 63 75 74 65 64 20 0a 2a ever executed .*
1efa1 2a 20 28 76 69 61 20 73 71 6c 69 74 65 33 5f 73 * (via sqlite3_s
1efa2 74 65 70 28 29 29 2e 0a 2a 2a 20 0a 2a 2a 20 49 tep())..** .** I
1efa3 66 20 50 31 20 69 73 20 30 2c 20 74 68 65 6e 20 f P1 is 0, then
1efa4 61 6c 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 6e all SQL statemen
1efa5 74 73 20 62 65 63 6f 6d 65 20 65 78 70 69 72 65 ts become expire
1efa6 64 2e 20 49 66 20 50 31 20 69 73 20 6e 6f 6e 2d d. If P1 is non-
1efa7 7a 65 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 6f 6e zero,.** then on
1efa8 6c 79 20 74 68 65 20 63 75 72 72 65 6e 74 6c 79 ly the currently
1efa9 20 65 78 65 63 75 74 69 6e 67 20 73 74 61 74 65 executing state
1efaa 6d 65 6e 74 20 69 73 20 61 66 66 65 63 74 65 64 ment is affected
1efab 2e 20 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 45 78 . .*/.case OP_Ex
1efac 70 69 72 65 3a 20 7b 0a 20 20 69 66 28 20 21 70 pire: {. if( !p
1efad 4f 70 2d 3e 70 31 20 29 7b 0a 20 20 20 20 73 71 Op->p1 ){. sq
1efae 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61 lite3ExpirePrepa
1efaf 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62 redStatements(db
1efb0 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
1efb1 70 2d 3e 65 78 70 69 72 65 64 20 3d 20 31 3b 0a p->expired = 1;.
1efb2 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1efb3 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1efb4 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 MIT_SHARED_CACHE
1efb5 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 61 62 6c ./* Opcode: Tabl
1efb6 65 4c 6f 63 6b 20 50 31 20 50 32 20 50 33 20 50 eLock P1 P2 P3 P
1efb7 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f 62 74 61 69 6e 4 *.**.** Obtain
1efb8 20 61 20 6c 6f 63 6b 20 6f 6e 20 61 20 70 61 72 a lock on a par
1efb9 74 69 63 75 6c 61 72 20 74 61 62 6c 65 2e 20 54 ticular table. T
1efba 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 his instruction
1efbb 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 77 68 65 is only used whe
1efbc 6e 0a 2a 2a 20 74 68 65 20 73 68 61 72 65 64 2d n.** the shared-
1efbd 63 61 63 68 65 20 66 65 61 74 75 72 65 20 69 73 cache feature is
1efbe 20 65 6e 61 62 6c 65 64 2e 20 0a 2a 2a 0a 2a 2a enabled. .**.**
1efbf 20 50 31 20 69 73 20 74 68 65 20 69 6e 64 65 78 P1 is the index
1efc0 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
1efc1 20 69 6e 20 73 71 6c 69 74 65 33 2e 61 44 62 5b in sqlite3.aDb[
1efc2 5d 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 ] of the databas
1efc3 65 0a 2a 2a 20 6f 6e 20 77 68 69 63 68 20 74 68 e.** on which th
1efc4 65 20 6c 6f 63 6b 20 69 73 20 61 63 71 75 69 72 e lock is acquir
1efc5 65 64 2e 20 20 41 20 72 65 61 64 6c 6f 63 6b 20 ed. A readlock
1efc6 69 73 20 6f 62 74 61 69 6e 65 64 20 69 66 20 50 is obtained if P
1efc7 33 3d 3d 30 20 6f 72 0a 2a 2a 20 61 20 77 72 69 3==0 or.** a wri
1efc8 74 65 20 6c 6f 63 6b 20 69 66 20 50 33 3d 3d 31 te lock if P3==1
1efc9 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 63 6f 6e 74 61 ..**.** P2 conta
1efca 69 6e 73 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 ins the root-pag
1efcb 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 e of the table t
1efcc 6f 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 50 34 o lock..**.** P4
1efcd 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e contains a poin
1efce 74 65 72 20 74 6f 20 74 68 65 20 6e 61 6d 65 20 ter to the name
1efcf 6f 66 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 of the table bei
1efd0 6e 67 20 6c 6f 63 6b 65 64 2e 20 54 68 69 73 20 ng locked. This
1efd1 69 73 20 6f 6e 6c 79 0a 2a 2a 20 75 73 65 64 20 is only.** used
1efd2 74 6f 20 67 65 6e 65 72 61 74 65 20 61 6e 20 65 to generate an e
1efd3 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 66 20 rror message if
1efd4 74 68 65 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 20 the lock cannot
1efd5 62 65 20 6f 62 74 61 69 6e 65 64 2e 0a 2a 2f 0a be obtained..*/.
1efd6 63 61 73 65 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 case OP_TableLoc
1efd7 6b 3a 20 7b 0a 20 20 75 38 20 69 73 57 72 69 74 k: {. u8 isWrit
1efd8 65 4c 6f 63 6b 20 3d 20 28 75 38 29 70 4f 70 2d eLock = (u8)pOp-
1efd9 3e 70 33 3b 0a 20 20 69 66 28 20 69 73 57 72 69 >p3;. if( isWri
1efda 74 65 4c 6f 63 6b 20 7c 7c 20 30 3d 3d 28 64 62 teLock || 0==(db
1efdb 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 52 ->flags&SQLITE_R
1efdc 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29 20 eadUncommitted)
1efdd 29 7b 0a 20 20 20 20 69 6e 74 20 70 31 20 3d 20 ){. int p1 =
1efde 70 4f 70 2d 3e 70 31 3b 20 0a 20 20 20 20 61 73 pOp->p1; . as
1efdf 73 65 72 74 28 20 70 31 3e 3d 30 20 26 26 20 70 sert( p1>=0 && p
1efe0 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 20 1<db->nDb );.
1efe1 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 assert( (p->btr
1efe2 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 31 29 eeMask & (1<<p1)
1efe3 29 21 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 )!=0 );. asse
1efe4 72 74 28 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d rt( isWriteLock=
1efe5 3d 30 20 7c 7c 20 69 73 57 72 69 74 65 4c 6f 63 =0 || isWriteLoc
1efe6 6b 3d 3d 31 20 29 3b 0a 20 20 20 20 72 63 20 3d k==1 );. rc =
1efe7 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 6f 63 sqlite3BtreeLoc
1efe8 6b 54 61 62 6c 65 28 64 62 2d 3e 61 44 62 5b 70 kTable(db->aDb[p
1efe9 31 5d 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 32 2c 1].pBt, pOp->p2,
1efea 20 69 73 57 72 69 74 65 4c 6f 63 6b 29 3b 0a 20 isWriteLock);.
1efeb 20 20 20 69 66 28 20 28 72 63 26 30 78 46 46 29 if( (rc&0xFF)
1efec 3d 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 ==SQLITE_LOCKED
1efed 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 ){. const c
1efee 68 61 72 20 2a 7a 20 3d 20 70 4f 70 2d 3e 70 34 har *z = pOp->p4
1efef 2e 7a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 .z;. sqlite
1eff0 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 3SetString(&p->z
1eff1 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 64 61 74 ErrMsg, db, "dat
1eff2 61 62 61 73 65 20 74 61 62 6c 65 20 69 73 20 6c abase table is l
1eff3 6f 63 6b 65 64 3a 20 25 73 22 2c 20 7a 29 3b 0a ocked: %s", z);.
1eff4 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 }. }. brea
1eff5 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 k;.}.#endif /* S
1eff6 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
1eff7 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e D_CACHE */..#ifn
1eff8 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1eff9 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 VIRTUALTABLE./*
1effa 4f 70 63 6f 64 65 3a 20 56 42 65 67 69 6e 20 2a Opcode: VBegin *
1effb 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 * * P4 *.**.**
1effc 50 34 20 6d 61 79 20 62 65 20 61 20 70 6f 69 6e P4 may be a poin
1effd 74 65 72 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 ter to an sqlite
1effe 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 72 65 3_vtab structure
1efff 2e 20 49 66 20 73 6f 2c 20 63 61 6c 6c 20 74 68 . If so, call th
1f000 65 20 0a 2a 2a 20 78 42 65 67 69 6e 20 6d 65 74 e .** xBegin met
1f001 68 6f 64 20 66 6f 72 20 74 68 61 74 20 74 61 62 hod for that tab
1f002 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f 2c 20 le..**.** Also,
1f003 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 50 whether or not P
1f004 34 20 69 73 20 73 65 74 2c 20 63 68 65 63 6b 20 4 is set, check
1f005 74 68 61 74 20 74 68 69 73 20 69 73 20 6e 6f 74 that this is not
1f006 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 20 66 72 being called fr
1f007 6f 6d 0a 2a 2a 20 77 69 74 68 69 6e 20 61 20 63 om.** within a c
1f008 61 6c 6c 62 61 63 6b 20 74 6f 20 61 20 76 69 72 allback to a vir
1f009 74 75 61 6c 20 74 61 62 6c 65 20 78 53 79 6e 63 tual table xSync
1f00a 28 29 20 6d 65 74 68 6f 64 2e 20 49 66 20 69 74 () method. If it
1f00b 20 69 73 2c 20 74 68 65 20 65 72 72 6f 72 0a 2a is, the error.*
1f00c 2a 20 63 6f 64 65 20 77 69 6c 6c 20 62 65 20 73 * code will be s
1f00d 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4c 4f 43 et to SQLITE_LOC
1f00e 4b 45 44 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f KED..*/.case OP_
1f00f 56 42 65 67 69 6e 3a 20 7b 0a 23 69 66 20 30 20 VBegin: {.#if 0
1f010 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1f011 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1f012 2e 63 66 20 2a 2f 0a 20 20 56 54 61 62 6c 65 20 .cf */. VTable
1f013 2a 70 56 54 61 62 3b 0a 23 65 6e 64 69 66 20 2f *pVTab;.#endif /
1f014 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1f015 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 s moved into u.c
1f016 66 20 2a 2f 0a 20 20 75 2e 63 66 2e 70 56 54 61 f */. u.cf.pVTa
1f017 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 b = pOp->p4.pVta
1f018 62 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 b;. rc = sqlite
1f019 33 56 74 61 62 42 65 67 69 6e 28 64 62 2c 20 75 3VtabBegin(db, u
1f01a 2e 63 66 2e 70 56 54 61 62 29 3b 0a 20 20 69 66 .cf.pVTab);. if
1f01b 28 20 75 2e 63 66 2e 70 56 54 61 62 20 29 7b 0a ( u.cf.pVTab ){.
1f01c 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1f01d 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 e(db, p->zErrMsg
1f01e 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 );. p->zErrMs
1f01f 67 20 3d 20 75 2e 63 66 2e 70 56 54 61 62 2d 3e g = u.cf.pVTab->
1f020 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a pVtab->zErrMsg;.
1f021 20 20 20 20 75 2e 63 66 2e 70 56 54 61 62 2d 3e u.cf.pVTab->
1f022 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d pVtab->zErrMsg =
1f023 20 30 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0;. }. break;
1f024 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
1f025 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
1f026 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 TABLE */..#ifnde
1f027 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 f SQLITE_OMIT_VI
1f028 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 RTUALTABLE./* Op
1f029 63 6f 64 65 3a 20 56 43 72 65 61 74 65 20 50 31 code: VCreate P1
1f02a 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 * * P4 *.**.**
1f02b 50 34 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f P4 is the name o
1f02c 66 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c f a virtual tabl
1f02d 65 20 69 6e 20 64 61 74 61 62 61 73 65 20 50 31 e in database P1
1f02e 2e 20 43 61 6c 6c 20 74 68 65 20 78 43 72 65 61 . Call the xCrea
1f02f 74 65 20 6d 65 74 68 6f 64 0a 2a 2a 20 66 6f 72 te method.** for
1f030 20 74 68 61 74 20 74 61 62 6c 65 2e 0a 2a 2f 0a that table..*/.
1f031 63 61 73 65 20 4f 50 5f 56 43 72 65 61 74 65 3a case OP_VCreate:
1f032 20 7b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 {. rc = sqlite
1f033 33 56 74 61 62 43 61 6c 6c 43 72 65 61 74 65 28 3VtabCallCreate(
1f034 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 db, pOp->p1, pOp
1f035 2d 3e 70 34 2e 7a 2c 20 26 70 2d 3e 7a 45 72 72 ->p4.z, &p->zErr
1f036 4d 73 67 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d Msg);. break;.}
1f037 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1f038 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
1f039 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 BLE */..#ifndef
1f03a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
1f03b 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f UALTABLE./* Opco
1f03c 64 65 3a 20 56 44 65 73 74 72 6f 79 20 50 31 20 de: VDestroy P1
1f03d 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 * * P4 *.**.** P
1f03e 34 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 4 is the name of
1f03f 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 a virtual table
1f040 20 69 6e 20 64 61 74 61 62 61 73 65 20 50 31 2e in database P1.
1f041 20 20 43 61 6c 6c 20 74 68 65 20 78 44 65 73 74 Call the xDest
1f042 72 6f 79 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 roy method.** of
1f043 20 74 68 61 74 20 74 61 62 6c 65 2e 0a 2a 2f 0a that table..*/.
1f044 63 61 73 65 20 4f 50 5f 56 44 65 73 74 72 6f 79 case OP_VDestroy
1f045 3a 20 7b 0a 20 20 70 2d 3e 69 6e 56 74 61 62 4d : {. p->inVtabM
1f046 65 74 68 6f 64 20 3d 20 32 3b 0a 20 20 72 63 20 ethod = 2;. rc
1f047 3d 20 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c = sqlite3VtabCal
1f048 6c 44 65 73 74 72 6f 79 28 64 62 2c 20 70 4f 70 lDestroy(db, pOp
1f049 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 ->p1, pOp->p4.z)
1f04a 3b 0a 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 ;. p->inVtabMet
1f04b 68 6f 64 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b hod = 0;. break
1f04c 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
1f04d 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
1f04e 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 LTABLE */..#ifnd
1f04f 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ef SQLITE_OMIT_V
1f050 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f IRTUALTABLE./* O
1f051 70 63 6f 64 65 3a 20 56 4f 70 65 6e 20 50 31 20 pcode: VOpen P1
1f052 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 * * P4 *.**.** P
1f053 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 4 is a pointer t
1f054 6f 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c o a virtual tabl
1f055 65 20 6f 62 6a 65 63 74 2c 20 61 6e 20 73 71 6c e object, an sql
1f056 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 ite3_vtab struct
1f057 75 72 65 2e 0a 2a 2a 20 50 31 20 69 73 20 61 20 ure..** P1 is a
1f058 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 2e 20 20 cursor number.
1f059 54 68 69 73 20 6f 70 63 6f 64 65 20 6f 70 65 6e This opcode open
1f05a 73 20 61 20 63 75 72 73 6f 72 20 74 6f 20 74 68 s a cursor to th
1f05b 65 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62 e virtual.** tab
1f05c 6c 65 20 61 6e 64 20 73 74 6f 72 65 73 20 74 68 le and stores th
1f05d 61 74 20 63 75 72 73 6f 72 20 69 6e 20 50 31 2e at cursor in P1.
1f05e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 4f 70 65 .*/.case OP_VOpe
1f05f 6e 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c n: {.#if 0 /* l
1f060 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1f061 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 67 20 2a oved into u.cg *
1f062 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a /. VdbeCursor *
1f063 70 43 75 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f pCur;. sqlite3_
1f064 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 vtab_cursor *pVt
1f065 61 62 43 75 72 73 6f 72 3b 0a 20 20 73 71 6c 69 abCursor;. sqli
1f066 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b te3_vtab *pVtab;
1f067 0a 20 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c . sqlite3_modul
1f068 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 23 65 6e 64 e *pModule;.#end
1f069 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1f06a 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1f06b 20 75 2e 63 67 20 2a 2f 0a 0a 20 20 75 2e 63 67 u.cg */.. u.cg
1f06c 2e 70 43 75 72 20 3d 20 30 3b 0a 20 20 75 2e 63 .pCur = 0;. u.c
1f06d 67 2e 70 56 74 61 62 43 75 72 73 6f 72 20 3d 20 g.pVtabCursor =
1f06e 30 3b 0a 20 20 75 2e 63 67 2e 70 56 74 61 62 20 0;. u.cg.pVtab
1f06f 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 2d = pOp->p4.pVtab-
1f070 3e 70 56 74 61 62 3b 0a 20 20 75 2e 63 67 2e 70 >pVtab;. u.cg.p
1f071 4d 6f 64 75 6c 65 20 3d 20 28 73 71 6c 69 74 65 Module = (sqlite
1f072 33 5f 6d 6f 64 75 6c 65 20 2a 29 75 2e 63 67 2e 3_module *)u.cg.
1f073 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a pVtab->pModule;.
1f074 20 20 61 73 73 65 72 74 28 75 2e 63 67 2e 70 56 assert(u.cg.pV
1f075 74 61 62 20 26 26 20 75 2e 63 67 2e 70 4d 6f 64 tab && u.cg.pMod
1f076 75 6c 65 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 ule);. if( sqli
1f077 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 te3SafetyOff(db)
1f078 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 ) goto abort_du
1f079 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 72 e_to_misuse;. r
1f07a 63 20 3d 20 75 2e 63 67 2e 70 4d 6f 64 75 6c 65 c = u.cg.pModule
1f07b 2d 3e 78 4f 70 65 6e 28 75 2e 63 67 2e 70 56 74 ->xOpen(u.cg.pVt
1f07c 61 62 2c 20 26 75 2e 63 67 2e 70 56 74 61 62 43 ab, &u.cg.pVtabC
1f07d 75 72 73 6f 72 29 3b 0a 20 20 73 71 6c 69 74 65 ursor);. sqlite
1f07e 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 3DbFree(db, p->z
1f07f 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 ErrMsg);. p->zE
1f080 72 72 4d 73 67 20 3d 20 75 2e 63 67 2e 70 56 74 rrMsg = u.cg.pVt
1f081 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 75 ab->zErrMsg;. u
1f082 2e 63 67 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d .cg.pVtab->zErrM
1f083 73 67 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 71 sg = 0;. if( sq
1f084 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 lite3SafetyOn(db
1f085 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 ) ) goto abort_d
1f086 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 ue_to_misuse;.
1f087 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 if( SQLITE_OK==r
1f088 63 20 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 69 74 c ){. /* Init
1f089 69 61 6c 69 7a 65 20 73 71 6c 69 74 65 33 5f 76 ialize sqlite3_v
1f08a 74 61 62 5f 63 75 72 73 6f 72 20 62 61 73 65 20 tab_cursor base
1f08b 63 6c 61 73 73 20 2a 2f 0a 20 20 20 20 75 2e 63 class */. u.c
1f08c 67 2e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 g.pVtabCursor->p
1f08d 56 74 61 62 20 3d 20 75 2e 63 67 2e 70 56 74 61 Vtab = u.cg.pVta
1f08e 62 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e 69 74 69 b;.. /* Initi
1f08f 61 6c 69 73 65 20 76 64 62 65 20 63 75 72 73 6f alise vdbe curso
1f090 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 r object */.
1f091 75 2e 63 67 2e 70 43 75 72 20 3d 20 61 6c 6c 6f u.cg.pCur = allo
1f092 63 61 74 65 43 75 72 73 6f 72 28 70 2c 20 70 4f cateCursor(p, pO
1f093 70 2d 3e 70 31 2c 20 30 2c 20 2d 31 2c 20 30 29 p->p1, 0, -1, 0)
1f094 3b 0a 20 20 20 20 69 66 28 20 75 2e 63 67 2e 70 ;. if( u.cg.p
1f095 43 75 72 20 29 7b 0a 20 20 20 20 20 20 75 2e 63 Cur ){. u.c
1f096 67 2e 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 g.pCur->pVtabCur
1f097 73 6f 72 20 3d 20 75 2e 63 67 2e 70 56 74 61 62 sor = u.cg.pVtab
1f098 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 75 2e Cursor;. u.
1f099 63 67 2e 70 43 75 72 2d 3e 70 4d 6f 64 75 6c 65 cg.pCur->pModule
1f09a 20 3d 20 75 2e 63 67 2e 70 56 74 61 62 43 75 72 = u.cg.pVtabCur
1f09b 73 6f 72 2d 3e 70 56 74 61 62 2d 3e 70 4d 6f 64 sor->pVtab->pMod
1f09c 75 6c 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ule;. }else{.
1f09d 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 db->malloc
1f09e 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 Failed = 1;.
1f09f 20 20 75 2e 63 67 2e 70 4d 6f 64 75 6c 65 2d 3e u.cg.pModule->
1f0a0 78 43 6c 6f 73 65 28 75 2e 63 67 2e 70 56 74 61 xClose(u.cg.pVta
1f0a1 62 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 7d 0a bCursor);. }.
1f0a2 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 }. break;.}.#
1f0a3 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
1f0a4 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
1f0a5 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 E */..#ifndef SQ
1f0a6 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
1f0a7 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 LTABLE./* Opcode
1f0a8 3a 20 56 46 69 6c 74 65 72 20 50 31 20 50 32 20 : VFilter P1 P2
1f0a9 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 31 P3 P4 *.**.** P1
1f0aa 20 69 73 20 61 20 63 75 72 73 6f 72 20 6f 70 65 is a cursor ope
1f0ab 6e 65 64 20 75 73 69 6e 67 20 56 4f 70 65 6e 2e ned using VOpen.
1f0ac 20 20 50 32 20 69 73 20 61 6e 20 61 64 64 72 65 P2 is an addre
1f0ad 73 73 20 74 6f 20 6a 75 6d 70 20 74 6f 20 69 66 ss to jump to if
1f0ae 0a 2a 2a 20 74 68 65 20 66 69 6c 74 65 72 65 64 .** the filtered
1f0af 20 72 65 73 75 6c 74 20 73 65 74 20 69 73 20 65 result set is e
1f0b0 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 mpty..**.** P4 i
1f0b1 73 20 65 69 74 68 65 72 20 4e 55 4c 4c 20 6f 72 s either NULL or
1f0b2 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 77 a string that w
1f0b3 61 73 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 as generated by
1f0b4 74 68 65 20 78 42 65 73 74 49 6e 64 65 78 0a 2a the xBestIndex.*
1f0b5 2a 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 * method of the
1f0b6 6d 6f 64 75 6c 65 2e 20 20 54 68 65 20 69 6e 74 module. The int
1f0b7 65 72 70 72 65 74 61 74 69 6f 6e 20 6f 66 20 74 erpretation of t
1f0b8 68 65 20 50 34 20 73 74 72 69 6e 67 20 69 73 20 he P4 string is
1f0b9 6c 65 66 74 0a 2a 2a 20 74 6f 20 74 68 65 20 6d left.** to the m
1f0ba 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 odule implementa
1f0bb 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 tion..**.** This
1f0bc 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65 73 20 opcode invokes
1f0bd 74 68 65 20 78 46 69 6c 74 65 72 20 6d 65 74 68 the xFilter meth
1f0be 6f 64 20 6f 6e 20 74 68 65 20 76 69 72 74 75 61 od on the virtua
1f0bf 6c 20 74 61 62 6c 65 20 73 70 65 63 69 66 69 65 l table specifie
1f0c0 64 0a 2a 2a 20 62 79 20 50 31 2e 20 20 54 68 65 d.** by P1. The
1f0c1 20 69 6e 74 65 67 65 72 20 71 75 65 72 79 20 70 integer query p
1f0c2 6c 61 6e 20 70 61 72 61 6d 65 74 65 72 20 74 6f lan parameter to
1f0c3 20 78 46 69 6c 74 65 72 20 69 73 20 73 74 6f 72 xFilter is stor
1f0c4 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 0a 2a ed in register.*
1f0c5 2a 20 50 33 2e 20 52 65 67 69 73 74 65 72 20 50 * P3. Register P
1f0c6 33 2b 31 20 73 74 6f 72 65 73 20 74 68 65 20 61 3+1 stores the a
1f0c7 72 67 63 20 70 61 72 61 6d 65 74 65 72 20 74 6f rgc parameter to
1f0c8 20 62 65 20 70 61 73 73 65 64 20 74 6f 20 74 68 be passed to th
1f0c9 65 0a 2a 2a 20 78 46 69 6c 74 65 72 20 6d 65 74 e.** xFilter met
1f0ca 68 6f 64 2e 20 52 65 67 69 73 74 65 72 73 20 50 hod. Registers P
1f0cb 33 2b 32 2e 2e 50 33 2b 31 2b 61 72 67 63 20 61 3+2..P3+1+argc a
1f0cc 72 65 20 74 68 65 20 61 72 67 63 0a 2a 2a 20 61 re the argc.** a
1f0cd 64 64 69 74 69 6f 6e 61 6c 20 70 61 72 61 6d 65 dditional parame
1f0ce 74 65 72 73 20 77 68 69 63 68 20 61 72 65 20 70 ters which are p
1f0cf 61 73 73 65 64 20 74 6f 0a 2a 2a 20 78 46 69 6c assed to.** xFil
1f0d0 74 65 72 20 61 73 20 61 72 67 76 2e 20 52 65 67 ter as argv. Reg
1f0d1 69 73 74 65 72 20 50 33 2b 32 20 62 65 63 6f 6d ister P3+2 becom
1f0d2 65 73 20 61 72 67 76 5b 30 5d 20 77 68 65 6e 20 es argv[0] when
1f0d3 70 61 73 73 65 64 20 74 6f 20 78 46 69 6c 74 65 passed to xFilte
1f0d4 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 6a 75 6d 70 20 r..**.** A jump
1f0d5 69 73 20 6d 61 64 65 20 74 6f 20 50 32 20 69 66 is made to P2 if
1f0d6 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 the result set
1f0d7 61 66 74 65 72 20 66 69 6c 74 65 72 69 6e 67 20 after filtering
1f0d8 77 6f 75 6c 64 20 62 65 20 65 6d 70 74 79 2e 0a would be empty..
1f0d9 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 46 69 6c 74 */.case OP_VFilt
1f0da 65 72 3a 20 7b 20 20 20 2f 2a 20 6a 75 6d 70 20 er: { /* jump
1f0db 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 */.#if 0 /* loc
1f0dc 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1f0dd 65 64 20 69 6e 74 6f 20 75 2e 63 68 20 2a 2f 0a ed into u.ch */.
1f0de 20 20 69 6e 74 20 6e 41 72 67 3b 0a 20 20 69 6e int nArg;. in
1f0df 74 20 69 51 75 65 72 79 3b 0a 20 20 63 6f 6e 73 t iQuery;. cons
1f0e0 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 t sqlite3_module
1f0e1 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 4d 65 6d *pModule;. Mem
1f0e2 20 2a 70 51 75 65 72 79 3b 0a 20 20 4d 65 6d 20 *pQuery;. Mem
1f0e3 2a 70 41 72 67 63 3b 0a 20 20 73 71 6c 69 74 65 *pArgc;. sqlite
1f0e4 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 3_vtab_cursor *p
1f0e5 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 73 71 VtabCursor;. sq
1f0e6 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 lite3_vtab *pVta
1f0e7 62 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 b;. VdbeCursor
1f0e8 2a 70 43 75 72 3b 0a 20 20 69 6e 74 20 72 65 73 *pCur;. int res
1f0e9 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4d 65 6d ;. int i;. Mem
1f0ea 20 2a 2a 61 70 41 72 67 3b 0a 23 65 6e 64 69 66 **apArg;.#endif
1f0eb 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1f0ec 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1f0ed 2e 63 68 20 2a 2f 0a 0a 20 20 75 2e 63 68 2e 70 .ch */.. u.ch.p
1f0ee 51 75 65 72 79 20 3d 20 26 70 2d 3e 61 4d 65 6d Query = &p->aMem
1f0ef 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 75 2e 63 [pOp->p3];. u.c
1f0f0 68 2e 70 41 72 67 63 20 3d 20 26 75 2e 63 68 2e h.pArgc = &u.ch.
1f0f1 70 51 75 65 72 79 5b 31 5d 3b 0a 20 20 75 2e 63 pQuery[1];. u.c
1f0f2 68 2e 70 43 75 72 20 3d 20 70 2d 3e 61 70 43 73 h.pCur = p->apCs
1f0f3 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 52 45 r[pOp->p1];. RE
1f0f4 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 GISTER_TRACE(pOp
1f0f5 2d 3e 70 33 2c 20 75 2e 63 68 2e 70 51 75 65 72 ->p3, u.ch.pQuer
1f0f6 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e y);. assert( u.
1f0f7 63 68 2e 70 43 75 72 2d 3e 70 56 74 61 62 43 75 ch.pCur->pVtabCu
1f0f8 72 73 6f 72 20 29 3b 0a 20 20 75 2e 63 68 2e 70 rsor );. u.ch.p
1f0f9 56 74 61 62 43 75 72 73 6f 72 20 3d 20 75 2e 63 VtabCursor = u.c
1f0fa 68 2e 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 h.pCur->pVtabCur
1f0fb 73 6f 72 3b 0a 20 20 75 2e 63 68 2e 70 56 74 61 sor;. u.ch.pVta
1f0fc 62 20 3d 20 75 2e 63 68 2e 70 56 74 61 62 43 75 b = u.ch.pVtabCu
1f0fd 72 73 6f 72 2d 3e 70 56 74 61 62 3b 0a 20 20 75 rsor->pVtab;. u
1f0fe 2e 63 68 2e 70 4d 6f 64 75 6c 65 20 3d 20 75 2e .ch.pModule = u.
1f0ff 63 68 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c ch.pVtab->pModul
1f100 65 3b 0a 0a 20 20 2f 2a 20 47 72 61 62 20 74 68 e;.. /* Grab th
1f101 65 20 69 6e 64 65 78 20 6e 75 6d 62 65 72 20 61 e index number a
1f102 6e 64 20 61 72 67 63 20 70 61 72 61 6d 65 74 65 nd argc paramete
1f103 72 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 rs */. assert(
1f104 28 75 2e 63 68 2e 70 51 75 65 72 79 2d 3e 66 6c (u.ch.pQuery->fl
1f105 61 67 73 26 4d 45 4d 5f 49 6e 74 29 21 3d 30 20 ags&MEM_Int)!=0
1f106 26 26 20 75 2e 63 68 2e 70 41 72 67 63 2d 3e 66 && u.ch.pArgc->f
1f107 6c 61 67 73 3d 3d 4d 45 4d 5f 49 6e 74 20 29 3b lags==MEM_Int );
1f108 0a 20 20 75 2e 63 68 2e 6e 41 72 67 20 3d 20 28 . u.ch.nArg = (
1f109 69 6e 74 29 75 2e 63 68 2e 70 41 72 67 63 2d 3e int)u.ch.pArgc->
1f10a 75 2e 69 3b 0a 20 20 75 2e 63 68 2e 69 51 75 65 u.i;. u.ch.iQue
1f10b 72 79 20 3d 20 28 69 6e 74 29 75 2e 63 68 2e 70 ry = (int)u.ch.p
1f10c 51 75 65 72 79 2d 3e 75 2e 69 3b 0a 0a 20 20 2f Query->u.i;.. /
1f10d 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 78 46 69 * Invoke the xFi
1f10e 6c 74 65 72 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 lter method */.
1f10f 20 7b 0a 20 20 20 20 75 2e 63 68 2e 72 65 73 20 {. u.ch.res
1f110 3d 20 30 3b 0a 20 20 20 20 75 2e 63 68 2e 61 70 = 0;. u.ch.ap
1f111 41 72 67 20 3d 20 70 2d 3e 61 70 41 72 67 3b 0a Arg = p->apArg;.
1f112 20 20 20 20 66 6f 72 28 75 2e 63 68 2e 69 20 3d for(u.ch.i =
1f113 20 30 3b 20 75 2e 63 68 2e 69 3c 75 2e 63 68 2e 0; u.ch.i<u.ch.
1f114 6e 41 72 67 3b 20 75 2e 63 68 2e 69 2b 2b 29 7b nArg; u.ch.i++){
1f115 0a 20 20 20 20 20 20 75 2e 63 68 2e 61 70 41 72 . u.ch.apAr
1f116 67 5b 75 2e 63 68 2e 69 5d 20 3d 20 26 75 2e 63 g[u.ch.i] = &u.c
1f117 68 2e 70 41 72 67 63 5b 75 2e 63 68 2e 69 2b 31 h.pArgc[u.ch.i+1
1f118 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ];. sqlite3
1f119 56 64 62 65 4d 65 6d 53 74 6f 72 65 54 79 70 65 VdbeMemStoreType
1f11a 28 75 2e 63 68 2e 61 70 41 72 67 5b 75 2e 63 68 (u.ch.apArg[u.ch
1f11b 2e 69 5d 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 .i]);. }..
1f11c 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 if( sqlite3Safe
1f11d 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f tyOff(db) ) goto
1f11e 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 abort_due_to_mi
1f11f 73 75 73 65 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 suse;. p->inV
1f120 74 61 62 4d 65 74 68 6f 64 20 3d 20 31 3b 0a 20 tabMethod = 1;.
1f121 20 20 20 72 63 20 3d 20 75 2e 63 68 2e 70 4d 6f rc = u.ch.pMo
1f122 64 75 6c 65 2d 3e 78 46 69 6c 74 65 72 28 75 2e dule->xFilter(u.
1f123 63 68 2e 70 56 74 61 62 43 75 72 73 6f 72 2c 20 ch.pVtabCursor,
1f124 75 2e 63 68 2e 69 51 75 65 72 79 2c 20 70 4f 70 u.ch.iQuery, pOp
1f125 2d 3e 70 34 2e 7a 2c 20 75 2e 63 68 2e 6e 41 72 ->p4.z, u.ch.nAr
1f126 67 2c 20 75 2e 63 68 2e 61 70 41 72 67 29 3b 0a g, u.ch.apArg);.
1f127 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 p->inVtabMet
1f128 68 6f 64 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c hod = 0;. sql
1f129 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
1f12a 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 ->zErrMsg);.
1f12b 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63 p->zErrMsg = u.c
1f12c 68 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 h.pVtab->zErrMsg
1f12d 3b 0a 20 20 20 20 75 2e 63 68 2e 70 56 74 61 62 ;. u.ch.pVtab
1f12e 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 ->zErrMsg = 0;.
1f12f 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
1f130 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 75 2e E_OK ){. u.
1f131 63 68 2e 72 65 73 20 3d 20 75 2e 63 68 2e 70 4d ch.res = u.ch.pM
1f132 6f 64 75 6c 65 2d 3e 78 45 6f 66 28 75 2e 63 68 odule->xEof(u.ch
1f133 2e 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 .pVtabCursor);.
1f134 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c }. if( sql
1f135 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 ite3SafetyOn(db)
1f136 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 ) goto abort_du
1f137 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 e_to_misuse;..
1f138 20 20 69 66 28 20 75 2e 63 68 2e 72 65 73 20 29 if( u.ch.res )
1f139 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 {. pc = pOp
1f13a 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d 0a ->p2 - 1;. }.
1f13b 20 20 7d 0a 20 20 75 2e 63 68 2e 70 43 75 72 2d }. u.ch.pCur-
1f13c 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 0a 20 >nullRow = 0;..
1f13d 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 break;.}.#endif
1f13e 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
1f13f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a VIRTUALTABLE */.
1f140 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1f141 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
1f142 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 43 6f E./* Opcode: VCo
1f143 6c 75 6d 6e 20 50 31 20 50 32 20 50 33 20 2a 20 lumn P1 P2 P3 *
1f144 2a 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65 20 74 68 *.**.** Store th
1f145 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 e value of the P
1f146 32 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 0a 2a 2-th column of.*
1f147 2a 20 74 68 65 20 72 6f 77 20 6f 66 20 74 68 65 * the row of the
1f148 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 74 virtual-table t
1f149 68 61 74 20 74 68 65 20 0a 2a 2a 20 50 31 20 63 hat the .** P1 c
1f14a 75 72 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e ursor is pointin
1f14b 67 20 74 6f 20 69 6e 74 6f 20 72 65 67 69 73 74 g to into regist
1f14c 65 72 20 50 33 2e 0a 2a 2f 0a 63 61 73 65 20 4f er P3..*/.case O
1f14d 50 5f 56 43 6f 6c 75 6d 6e 3a 20 7b 0a 23 69 66 P_VColumn: {.#if
1f14e 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 0 /* local var
1f14f 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1f150 6f 20 75 2e 63 69 20 2a 2f 0a 20 20 73 71 6c 69 o u.ci */. sqli
1f151 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b te3_vtab *pVtab;
1f152 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 . const sqlite3
1f153 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 _module *pModule
1f154 3b 0a 20 20 4d 65 6d 20 2a 70 44 65 73 74 3b 0a ;. Mem *pDest;.
1f155 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
1f156 74 20 73 43 6f 6e 74 65 78 74 3b 0a 23 65 6e 64 t sContext;.#end
1f157 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1f158 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1f159 20 75 2e 63 69 20 2a 2f 0a 0a 20 20 56 64 62 65 u.ci */.. Vdbe
1f15a 43 75 72 73 6f 72 20 2a 70 43 75 72 20 3d 20 70 Cursor *pCur = p
1f15b 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d ->apCsr[pOp->p1]
1f15c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
1f15d 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 20 29 3b ->pVtabCursor );
1f15e 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1f15f 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c p3>0 && pOp->p3<
1f160 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e =p->nMem );. u.
1f161 63 69 2e 70 44 65 73 74 20 3d 20 26 70 2d 3e 61 ci.pDest = &p->a
1f162 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 Mem[pOp->p3];.
1f163 69 66 28 20 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f if( pCur->nullRo
1f164 77 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 w ){. sqlite3
1f165 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 75 VdbeMemSetNull(u
1f166 2e 63 69 2e 70 44 65 73 74 29 3b 0a 20 20 20 20 .ci.pDest);.
1f167 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 75 2e 63 break;. }. u.c
1f168 69 2e 70 56 74 61 62 20 3d 20 70 43 75 72 2d 3e i.pVtab = pCur->
1f169 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 pVtabCursor->pVt
1f16a 61 62 3b 0a 20 20 75 2e 63 69 2e 70 4d 6f 64 75 ab;. u.ci.pModu
1f16b 6c 65 20 3d 20 75 2e 63 69 2e 70 56 74 61 62 2d le = u.ci.pVtab-
1f16c 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 61 73 73 65 >pModule;. asse
1f16d 72 74 28 20 75 2e 63 69 2e 70 4d 6f 64 75 6c 65 rt( u.ci.pModule
1f16e 2d 3e 78 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 6d ->xColumn );. m
1f16f 65 6d 73 65 74 28 26 75 2e 63 69 2e 73 43 6f 6e emset(&u.ci.sCon
1f170 74 65 78 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 text, 0, sizeof(
1f171 75 2e 63 69 2e 73 43 6f 6e 74 65 78 74 29 29 3b u.ci.sContext));
1f172 0a 0a 20 20 2f 2a 20 54 68 65 20 6f 75 74 70 75 .. /* The outpu
1f173 74 20 63 65 6c 6c 20 6d 61 79 20 61 6c 72 65 61 t cell may alrea
1f174 64 79 20 68 61 76 65 20 61 20 62 75 66 66 65 72 dy have a buffer
1f175 20 61 6c 6c 6f 63 61 74 65 64 2e 20 4d 6f 76 65 allocated. Move
1f176 0a 20 20 2a 2a 20 74 68 65 20 63 75 72 72 65 6e . ** the curren
1f177 74 20 63 6f 6e 74 65 6e 74 73 20 74 6f 20 75 2e t contents to u.
1f178 63 69 2e 73 43 6f 6e 74 65 78 74 2e 73 20 73 6f ci.sContext.s so
1f179 20 69 6e 20 63 61 73 65 20 74 68 65 20 75 73 65 in case the use
1f17a 72 2d 66 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 r-function. **
1f17b 63 61 6e 20 75 73 65 20 74 68 65 20 61 6c 72 65 can use the alre
1f17c 61 64 79 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 ady allocated bu
1f17d 66 66 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 ffer instead of
1f17e 61 6c 6c 6f 63 61 74 69 6e 67 20 61 0a 20 20 2a allocating a. *
1f17f 2a 20 6e 65 77 20 6f 6e 65 2e 0a 20 20 2a 2f 0a * new one.. */.
1f180 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1f181 4d 6f 76 65 28 26 75 2e 63 69 2e 73 43 6f 6e 74 Move(&u.ci.sCont
1f182 65 78 74 2e 73 2c 20 75 2e 63 69 2e 70 44 65 73 ext.s, u.ci.pDes
1f183 74 29 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 t);. MemSetType
1f184 46 6c 61 67 28 26 75 2e 63 69 2e 73 43 6f 6e 74 Flag(&u.ci.sCont
1f185 65 78 74 2e 73 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 ext.s, MEM_Null)
1f186 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 ;.. if( sqlite3
1f187 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 SafetyOff(db) )
1f188 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1f189 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 72 63 20 3d o_misuse;. rc =
1f18a 20 75 2e 63 69 2e 70 4d 6f 64 75 6c 65 2d 3e 78 u.ci.pModule->x
1f18b 43 6f 6c 75 6d 6e 28 70 43 75 72 2d 3e 70 56 74 Column(pCur->pVt
1f18c 61 62 43 75 72 73 6f 72 2c 20 26 75 2e 63 69 2e abCursor, &u.ci.
1f18d 73 43 6f 6e 74 65 78 74 2c 20 70 4f 70 2d 3e 70 sContext, pOp->p
1f18e 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 2);. sqlite3DbF
1f18f 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d ree(db, p->zErrM
1f190 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 sg);. p->zErrMs
1f191 67 20 3d 20 75 2e 63 69 2e 70 56 74 61 62 2d 3e g = u.ci.pVtab->
1f192 7a 45 72 72 4d 73 67 3b 0a 20 20 75 2e 63 69 2e zErrMsg;. u.ci.
1f193 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d pVtab->zErrMsg =
1f194 20 30 3b 0a 20 20 69 66 28 20 75 2e 63 69 2e 73 0;. if( u.ci.s
1f195 43 6f 6e 74 65 78 74 2e 69 73 45 72 72 6f 72 20 Context.isError
1f196 29 7b 0a 20 20 20 20 72 63 20 3d 20 75 2e 63 69 ){. rc = u.ci
1f197 2e 73 43 6f 6e 74 65 78 74 2e 69 73 45 72 72 6f .sContext.isErro
1f198 72 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 70 r;. }.. /* Cop
1f199 79 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 y the result of
1f19a 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 the function to
1f19b 74 68 65 20 50 33 20 72 65 67 69 73 74 65 72 2e the P3 register.
1f19c 20 57 65 0a 20 20 2a 2a 20 64 6f 20 74 68 69 73 We. ** do this
1f19d 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 regardless of w
1f19e 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 6e hether or not an
1f19f 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 20 error occurred
1f1a0 74 6f 20 65 6e 73 75 72 65 20 61 6e 79 0a 20 20 to ensure any.
1f1a1 2a 2a 20 64 79 6e 61 6d 69 63 20 61 6c 6c 6f 63 ** dynamic alloc
1f1a2 61 74 69 6f 6e 20 69 6e 20 75 2e 63 69 2e 73 43 ation in u.ci.sC
1f1a3 6f 6e 74 65 78 74 2e 73 20 28 61 20 4d 65 6d 20 ontext.s (a Mem
1f1a4 73 74 72 75 63 74 29 20 69 73 20 20 72 65 6c 65 struct) is rele
1f1a5 61 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c ased.. */. sql
1f1a6 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e ite3VdbeChangeEn
1f1a7 63 6f 64 69 6e 67 28 26 75 2e 63 69 2e 73 43 6f coding(&u.ci.sCo
1f1a8 6e 74 65 78 74 2e 73 2c 20 65 6e 63 6f 64 69 6e ntext.s, encodin
1f1a9 67 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 g);. REGISTER_T
1f1aa 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e RACE(pOp->p3, u.
1f1ab 63 69 2e 70 44 65 73 74 29 3b 0a 20 20 73 71 6c ci.pDest);. sql
1f1ac 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 ite3VdbeMemMove(
1f1ad 75 2e 63 69 2e 70 44 65 73 74 2c 20 26 75 2e 63 u.ci.pDest, &u.c
1f1ae 69 2e 73 43 6f 6e 74 65 78 74 2e 73 29 3b 0a 20 i.sContext.s);.
1f1af 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 UPDATE_MAX_BLOB
1f1b0 53 49 5a 45 28 75 2e 63 69 2e 70 44 65 73 74 29 SIZE(u.ci.pDest)
1f1b1 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 ;.. if( sqlite3
1f1b2 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a SafetyOn(db) ){.
1f1b3 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 goto abort_d
1f1b4 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 ue_to_misuse;.
1f1b5 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 }. if( sqlite3V
1f1b6 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 75 2e 63 dbeMemTooBig(u.c
1f1b7 69 2e 70 44 65 73 74 29 20 29 7b 0a 20 20 20 20 i.pDest) ){.
1f1b8 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 goto too_big;.
1f1b9 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e }. break;.}.#en
1f1ba 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
1f1bb 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 IT_VIRTUALTABLE
1f1bc 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 */..#ifndef SQLI
1f1bd 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
1f1be 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ABLE./* Opcode:
1f1bf 56 4e 65 78 74 20 50 31 20 50 32 20 2a 20 2a 20 VNext P1 P2 * *
1f1c0 2a 0a 2a 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 *.**.** Advance
1f1c1 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 50 31 virtual table P1
1f1c2 20 74 6f 20 74 68 65 20 6e 65 78 74 20 72 6f 77 to the next row
1f1c3 20 69 6e 20 69 74 73 20 72 65 73 75 6c 74 20 73 in its result s
1f1c4 65 74 20 61 6e 64 0a 2a 2a 20 6a 75 6d 70 20 74 et and.** jump t
1f1c5 6f 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 50 32 o instruction P2
1f1c6 2e 20 20 4f 72 2c 20 69 66 20 74 68 65 20 76 69 . Or, if the vi
1f1c7 72 74 75 61 6c 20 74 61 62 6c 65 20 68 61 73 20 rtual table has
1f1c8 72 65 61 63 68 65 64 0a 2a 2a 20 74 68 65 20 65 reached.** the e
1f1c9 6e 64 20 6f 66 20 69 74 73 20 72 65 73 75 6c 74 nd of its result
1f1ca 20 73 65 74 2c 20 74 68 65 6e 20 66 61 6c 6c 20 set, then fall
1f1cb 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e through to the n
1f1cc 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e ext instruction.
1f1cd 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 4e 65 78 .*/.case OP_VNex
1f1ce 74 3a 20 7b 20 20 20 2f 2a 20 6a 75 6d 70 20 2a t: { /* jump *
1f1cf 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 /.#if 0 /* loca
1f1d0 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1f1d1 64 20 69 6e 74 6f 20 75 2e 63 6a 20 2a 2f 0a 20 d into u.cj */.
1f1d2 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 sqlite3_vtab *p
1f1d3 56 74 61 62 3b 0a 20 20 63 6f 6e 73 74 20 73 71 Vtab;. const sq
1f1d4 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d lite3_module *pM
1f1d5 6f 64 75 6c 65 3b 0a 20 20 69 6e 74 20 72 65 73 odule;. int res
1f1d6 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a ;. VdbeCursor *
1f1d7 70 43 75 72 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 pCur;.#endif /*
1f1d8 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1f1d9 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6a 20 moved into u.cj
1f1da 2a 2f 0a 0a 20 20 75 2e 63 6a 2e 72 65 73 20 3d */.. u.cj.res =
1f1db 20 30 3b 0a 20 20 75 2e 63 6a 2e 70 43 75 72 20 0;. u.cj.pCur
1f1dc 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e = p->apCsr[pOp->
1f1dd 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 p1];. assert( u
1f1de 2e 63 6a 2e 70 43 75 72 2d 3e 70 56 74 61 62 43 .cj.pCur->pVtabC
1f1df 75 72 73 6f 72 20 29 3b 0a 20 20 69 66 28 20 75 ursor );. if( u
1f1e0 2e 63 6a 2e 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f .cj.pCur->nullRo
1f1e1 77 20 29 7b 0a 20 20 20 20 62 72 65 61 6b 3b 0a w ){. break;.
1f1e2 20 20 7d 0a 20 20 75 2e 63 6a 2e 70 56 74 61 62 }. u.cj.pVtab
1f1e3 20 3d 20 75 2e 63 6a 2e 70 43 75 72 2d 3e 70 56 = u.cj.pCur->pV
1f1e4 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 tabCursor->pVtab
1f1e5 3b 0a 20 20 75 2e 63 6a 2e 70 4d 6f 64 75 6c 65 ;. u.cj.pModule
1f1e6 20 3d 20 75 2e 63 6a 2e 70 56 74 61 62 2d 3e 70 = u.cj.pVtab->p
1f1e7 4d 6f 64 75 6c 65 3b 0a 20 20 61 73 73 65 72 74 Module;. assert
1f1e8 28 20 75 2e 63 6a 2e 70 4d 6f 64 75 6c 65 2d 3e ( u.cj.pModule->
1f1e9 78 4e 65 78 74 20 29 3b 0a 0a 20 20 2f 2a 20 49 xNext );.. /* I
1f1ea 6e 76 6f 6b 65 20 74 68 65 20 78 4e 65 78 74 28 nvoke the xNext(
1f1eb 29 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 ) method of the
1f1ec 6d 6f 64 75 6c 65 2e 20 54 68 65 72 65 20 69 73 module. There is
1f1ed 20 6e 6f 20 77 61 79 20 66 6f 72 20 74 68 65 0a no way for the.
1f1ee 20 20 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20 ** underlying
1f1ef 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 implementation t
1f1f0 6f 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f o return an erro
1f1f1 72 20 69 66 20 6f 6e 65 20 6f 63 63 75 72 73 20 r if one occurs
1f1f2 64 75 72 69 6e 67 0a 20 20 2a 2a 20 78 4e 65 78 during. ** xNex
1f1f3 74 28 29 2e 20 49 6e 73 74 65 61 64 2c 20 69 66 t(). Instead, if
1f1f4 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
1f1f5 2c 20 74 72 75 65 20 69 73 20 72 65 74 75 72 6e , true is return
1f1f6 65 64 20 28 69 6e 64 69 63 61 74 69 6e 67 20 74 ed (indicating t
1f1f7 68 61 74 0a 20 20 2a 2a 20 64 61 74 61 20 69 73 hat. ** data is
1f1f8 20 61 76 61 69 6c 61 62 6c 65 29 20 61 6e 64 20 available) and
1f1f9 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 72 the error code r
1f1fa 65 74 75 72 6e 65 64 20 77 68 65 6e 20 78 43 6f eturned when xCo
1f1fb 6c 75 6d 6e 20 6f 72 0a 20 20 2a 2a 20 73 6f 6d lumn or. ** som
1f1fc 65 20 6f 74 68 65 72 20 6d 65 74 68 6f 64 20 69 e other method i
1f1fd 73 20 6e 65 78 74 20 69 6e 76 6f 6b 65 64 20 6f s next invoked o
1f1fe 6e 20 74 68 65 20 73 61 76 65 20 76 69 72 74 75 n the save virtu
1f1ff 61 6c 20 74 61 62 6c 65 20 63 75 72 73 6f 72 2e al table cursor.
1f200 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 . */. if( sqli
1f201 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 te3SafetyOff(db)
1f202 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 ) goto abort_du
1f203 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 70 e_to_misuse;. p
1f204 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d ->inVtabMethod =
1f205 20 31 3b 0a 20 20 72 63 20 3d 20 75 2e 63 6a 2e 1;. rc = u.cj.
1f206 70 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 28 75 pModule->xNext(u
1f207 2e 63 6a 2e 70 43 75 72 2d 3e 70 56 74 61 62 43 .cj.pCur->pVtabC
1f208 75 72 73 6f 72 29 3b 0a 20 20 70 2d 3e 69 6e 56 ursor);. p->inV
1f209 74 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 tabMethod = 0;.
1f20a 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1f20b 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a b, p->zErrMsg);.
1f20c 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 p->zErrMsg = u
1f20d 2e 63 6a 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d .cj.pVtab->zErrM
1f20e 73 67 3b 0a 20 20 75 2e 63 6a 2e 70 56 74 61 62 sg;. u.cj.pVtab
1f20f 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 ->zErrMsg = 0;.
1f210 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1f211 4f 4b 20 29 7b 0a 20 20 20 20 75 2e 63 6a 2e 72 OK ){. u.cj.r
1f212 65 73 20 3d 20 75 2e 63 6a 2e 70 4d 6f 64 75 6c es = u.cj.pModul
1f213 65 2d 3e 78 45 6f 66 28 75 2e 63 6a 2e 70 43 75 e->xEof(u.cj.pCu
1f214 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 29 3b r->pVtabCursor);
1f215 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 . }. if( sqlit
1f216 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 e3SafetyOn(db) )
1f217 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f goto abort_due_
1f218 74 6f 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 69 66 to_misuse;.. if
1f219 28 20 21 75 2e 63 6a 2e 72 65 73 20 29 7b 0a 20 ( !u.cj.res ){.
1f21a 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 /* If there i
1f21b 73 20 64 61 74 61 2c 20 6a 75 6d 70 20 74 6f 20 s data, jump to
1f21c 50 32 20 2a 2f 0a 20 20 20 20 70 63 20 3d 20 70 P2 */. pc = p
1f21d 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a Op->p2 - 1;. }.
1f21e 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 break;.}.#endi
1f21f 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
1f220 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f _VIRTUALTABLE */
1f221 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1f222 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
1f223 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 52 LE./* Opcode: VR
1f224 65 6e 61 6d 65 20 50 31 20 2a 20 2a 20 50 34 20 ename P1 * * P4
1f225 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 *.**.** P4 is a
1f226 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 76 69 72 pointer to a vir
1f227 74 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63 tual table objec
1f228 74 2c 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 t, an sqlite3_vt
1f229 61 62 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a ab structure..**
1f22a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 6e 76 This opcode inv
1f22b 6f 6b 65 73 20 74 68 65 20 63 6f 72 72 65 73 70 okes the corresp
1f22c 6f 6e 64 69 6e 67 20 78 52 65 6e 61 6d 65 20 6d onding xRename m
1f22d 65 74 68 6f 64 2e 20 54 68 65 20 76 61 6c 75 65 ethod. The value
1f22e 0a 2a 2a 20 69 6e 20 72 65 67 69 73 74 65 72 20 .** in register
1f22f 50 31 20 69 73 20 70 61 73 73 65 64 20 61 73 20 P1 is passed as
1f230 74 68 65 20 7a 4e 61 6d 65 20 61 72 67 75 6d 65 the zName argume
1f231 6e 74 20 74 6f 20 74 68 65 20 78 52 65 6e 61 6d nt to the xRenam
1f232 65 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 63 61 73 e method..*/.cas
1f233 65 20 4f 50 5f 56 52 65 6e 61 6d 65 3a 20 7b 0a e OP_VRename: {.
1f234 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 #if 0 /* local
1f235 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1f236 69 6e 74 6f 20 75 2e 63 6b 20 2a 2f 0a 20 20 73 into u.ck */. s
1f237 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 qlite3_vtab *pVt
1f238 61 62 3b 0a 20 20 4d 65 6d 20 2a 70 4e 61 6d 65 ab;. Mem *pName
1f239 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 ;.#endif /* loca
1f23a 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1f23b 64 20 69 6e 74 6f 20 75 2e 63 6b 20 2a 2f 0a 0a d into u.ck */..
1f23c 20 20 75 2e 63 6b 2e 70 56 74 61 62 20 3d 20 70 u.ck.pVtab = p
1f23d 4f 70 2d 3e 70 34 2e 70 56 74 61 62 2d 3e 70 56 Op->p4.pVtab->pV
1f23e 74 61 62 3b 0a 20 20 75 2e 63 6b 2e 70 4e 61 6d tab;. u.ck.pNam
1f23f 65 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 e = &p->aMem[pOp
1f240 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 ->p1];. assert(
1f241 20 75 2e 63 6b 2e 70 56 74 61 62 2d 3e 70 4d 6f u.ck.pVtab->pMo
1f242 64 75 6c 65 2d 3e 78 52 65 6e 61 6d 65 20 29 3b dule->xRename );
1f243 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 . REGISTER_TRAC
1f244 45 28 70 4f 70 2d 3e 70 31 2c 20 75 2e 63 6b 2e E(pOp->p1, u.ck.
1f245 70 4e 61 6d 65 29 3b 0a 20 20 61 73 73 65 72 74 pName);. assert
1f246 28 20 75 2e 63 6b 2e 70 4e 61 6d 65 2d 3e 66 6c ( u.ck.pName->fl
1f247 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 20 29 3b ags & MEM_Str );
1f248 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 . if( sqlite3Sa
1f249 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f fetyOff(db) ) go
1f24a 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1f24b 6d 69 73 75 73 65 3b 0a 20 20 72 63 20 3d 20 75 misuse;. rc = u
1f24c 2e 63 6b 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 .ck.pVtab->pModu
1f24d 6c 65 2d 3e 78 52 65 6e 61 6d 65 28 75 2e 63 6b le->xRename(u.ck
1f24e 2e 70 56 74 61 62 2c 20 75 2e 63 6b 2e 70 4e 61 .pVtab, u.ck.pNa
1f24f 6d 65 2d 3e 7a 29 3b 0a 20 20 73 71 6c 69 74 65 me->z);. sqlite
1f250 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 3DbFree(db, p->z
1f251 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 ErrMsg);. p->zE
1f252 72 72 4d 73 67 20 3d 20 75 2e 63 6b 2e 70 56 74 rrMsg = u.ck.pVt
1f253 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 75 ab->zErrMsg;. u
1f254 2e 63 6b 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d .ck.pVtab->zErrM
1f255 73 67 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 71 sg = 0;. if( sq
1f256 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 lite3SafetyOn(db
1f257 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 ) ) goto abort_d
1f258 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 0a 20 ue_to_misuse;..
1f259 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 break;.}.#endif
1f25a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1f25b 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
1f25c 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 55 LE./* Opcode: VU
1f25d 70 64 61 74 65 20 50 31 20 50 32 20 50 33 20 50 pdate P1 P2 P3 P
1f25e 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 4 *.**.** P4 is
1f25f 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 76 a pointer to a v
1f260 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a irtual table obj
1f261 65 63 74 2c 20 61 6e 20 73 71 6c 69 74 65 33 5f ect, an sqlite3_
1f262 76 74 61 62 20 73 74 72 75 63 74 75 72 65 2e 0a vtab structure..
1f263 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 ** This opcode i
1f264 6e 76 6f 6b 65 73 20 74 68 65 20 63 6f 72 72 65 nvokes the corre
1f265 73 70 6f 6e 64 69 6e 67 20 78 55 70 64 61 74 65 sponding xUpdate
1f266 20 6d 65 74 68 6f 64 2e 20 50 32 20 76 61 6c 75 method. P2 valu
1f267 65 73 0a 2a 2a 20 61 72 65 20 63 6f 6e 74 69 67 es.** are contig
1f268 75 6f 75 73 20 6d 65 6d 6f 72 79 20 63 65 6c 6c uous memory cell
1f269 73 20 73 74 61 72 74 69 6e 67 20 61 74 20 50 33 s starting at P3
1f26a 20 74 6f 20 70 61 73 73 20 74 6f 20 74 68 65 20 to pass to the
1f26b 78 55 70 64 61 74 65 20 0a 2a 2a 20 69 6e 76 6f xUpdate .** invo
1f26c 63 61 74 69 6f 6e 2e 20 54 68 65 20 76 61 6c 75 cation. The valu
1f26d 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 28 50 e in register (P
1f26e 33 2b 50 32 2d 31 29 20 63 6f 72 72 65 73 70 6f 3+P2-1) correspo
1f26f 6e 64 73 20 74 6f 20 74 68 65 20 0a 2a 2a 20 70 nds to the .** p
1f270 32 74 68 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 2th element of t
1f271 68 65 20 61 72 67 76 20 61 72 72 61 79 20 70 61 he argv array pa
1f272 73 73 65 64 20 74 6f 20 78 55 70 64 61 74 65 2e ssed to xUpdate.
1f273 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 55 70 64 61 .**.** The xUpda
1f274 74 65 20 6d 65 74 68 6f 64 20 77 69 6c 6c 20 64 te method will d
1f275 6f 20 61 20 44 45 4c 45 54 45 20 6f 72 20 61 6e o a DELETE or an
1f276 20 49 4e 53 45 52 54 20 6f 72 20 62 6f 74 68 2e INSERT or both.
1f277 0a 2a 2a 20 54 68 65 20 61 72 67 76 5b 30 5d 20 .** The argv[0]
1f278 65 6c 65 6d 65 6e 74 20 28 77 68 69 63 68 20 63 element (which c
1f279 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 6d 65 orresponds to me
1f27a 6d 6f 72 79 20 63 65 6c 6c 20 50 33 29 0a 2a 2a mory cell P3).**
1f27b 20 69 73 20 74 68 65 20 72 6f 77 69 64 20 6f 66 is the rowid of
1f27c 20 61 20 72 6f 77 20 74 6f 20 64 65 6c 65 74 65 a row to delete
1f27d 2e 20 20 49 66 20 61 72 67 76 5b 30 5d 20 69 73 . If argv[0] is
1f27e 20 4e 55 4c 4c 20 74 68 65 6e 20 6e 6f 20 0a 2a NULL then no .*
1f27f 2a 20 64 65 6c 65 74 69 6f 6e 20 6f 63 63 75 72 * deletion occur
1f280 73 2e 20 20 54 68 65 20 61 72 67 76 5b 31 5d 20 s. The argv[1]
1f281 65 6c 65 6d 65 6e 74 20 69 73 20 74 68 65 20 72 element is the r
1f282 6f 77 69 64 20 6f 66 20 74 68 65 20 6e 65 77 20 owid of the new
1f283 0a 2a 2a 20 72 6f 77 2e 20 20 54 68 69 73 20 63 .** row. This c
1f284 61 6e 20 62 65 20 4e 55 4c 4c 20 74 6f 20 68 61 an be NULL to ha
1f285 76 65 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 ve the virtual t
1f286 61 62 6c 65 20 73 65 6c 65 63 74 20 74 68 65 20 able select the
1f287 6e 65 77 20 0a 2a 2a 20 72 6f 77 69 64 20 66 6f new .** rowid fo
1f288 72 20 69 74 73 65 6c 66 2e 20 20 54 68 65 20 73 r itself. The s
1f289 75 62 73 65 71 75 65 6e 74 20 65 6c 65 6d 65 6e ubsequent elemen
1f28a 74 73 20 69 6e 20 74 68 65 20 61 72 72 61 79 20 ts in the array
1f28b 61 72 65 20 0a 2a 2a 20 74 68 65 20 76 61 6c 75 are .** the valu
1f28c 65 73 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e es of columns in
1f28d 20 74 68 65 20 6e 65 77 20 72 6f 77 2e 0a 2a 2a the new row..**
1f28e 0a 2a 2a 20 49 66 20 50 32 3d 3d 31 20 74 68 65 .** If P2==1 the
1f28f 6e 20 6e 6f 20 69 6e 73 65 72 74 20 69 73 20 70 n no insert is p
1f290 65 72 66 6f 72 6d 65 64 2e 20 20 61 72 67 76 5b erformed. argv[
1f291 30 5d 20 69 73 20 74 68 65 20 72 6f 77 69 64 20 0] is the rowid
1f292 6f 66 0a 2a 2a 20 61 20 72 6f 77 20 74 6f 20 64 of.** a row to d
1f293 65 6c 65 74 65 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 elete..**.** P1
1f294 69 73 20 61 20 62 6f 6f 6c 65 61 6e 20 66 6c 61 is a boolean fla
1f295 67 2e 20 49 66 20 69 74 20 69 73 20 73 65 74 20 g. If it is set
1f296 74 6f 20 74 72 75 65 20 61 6e 64 20 74 68 65 20 to true and the
1f297 78 55 70 64 61 74 65 20 63 61 6c 6c 0a 2a 2a 20 xUpdate call.**
1f298 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74 is successful, t
1f299 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 hen the value re
1f29a 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 turned by sqlite
1f29b 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 3_last_insert_ro
1f29c 77 69 64 28 29 20 0a 2a 2a 20 69 73 20 73 65 74 wid() .** is set
1f29d 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 to the value of
1f29e 20 74 68 65 20 72 6f 77 69 64 20 66 6f 72 20 74 the rowid for t
1f29f 68 65 20 72 6f 77 20 6a 75 73 74 20 69 6e 73 65 he row just inse
1f2a0 72 74 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 rted..*/.case OP
1f2a1 5f 56 55 70 64 61 74 65 3a 20 7b 0a 23 69 66 20 _VUpdate: {.#if
1f2a2 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1f2a3 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1f2a4 20 75 2e 63 6c 20 2a 2f 0a 20 20 73 71 6c 69 74 u.cl */. sqlit
1f2a5 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a e3_vtab *pVtab;.
1f2a6 20 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 sqlite3_module
1f2a7 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e 74 *pModule;. int
1f2a8 20 6e 41 72 67 3b 0a 20 20 69 6e 74 20 69 3b 0a nArg;. int i;.
1f2a9 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 72 sqlite_int64 r
1f2aa 6f 77 69 64 3b 0a 20 20 4d 65 6d 20 2a 2a 61 70 owid;. Mem **ap
1f2ab 41 72 67 3b 0a 20 20 4d 65 6d 20 2a 70 58 3b 0a Arg;. Mem *pX;.
1f2ac 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 #endif /* local
1f2ad 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1f2ae 69 6e 74 6f 20 75 2e 63 6c 20 2a 2f 0a 0a 20 20 into u.cl */..
1f2af 75 2e 63 6c 2e 70 56 74 61 62 20 3d 20 70 4f 70 u.cl.pVtab = pOp
1f2b0 2d 3e 70 34 2e 70 56 74 61 62 2d 3e 70 56 74 61 ->p4.pVtab->pVta
1f2b1 62 3b 0a 20 20 75 2e 63 6c 2e 70 4d 6f 64 75 6c b;. u.cl.pModul
1f2b2 65 20 3d 20 28 73 71 6c 69 74 65 33 5f 6d 6f 64 e = (sqlite3_mod
1f2b3 75 6c 65 20 2a 29 75 2e 63 6c 2e 70 56 74 61 62 ule *)u.cl.pVtab
1f2b4 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 75 2e 63 ->pModule;. u.c
1f2b5 6c 2e 6e 41 72 67 20 3d 20 70 4f 70 2d 3e 70 32 l.nArg = pOp->p2
1f2b6 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d ;. assert( pOp-
1f2b7 3e 70 34 74 79 70 65 3d 3d 50 34 5f 56 54 41 42 >p4type==P4_VTAB
1f2b8 20 29 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 );. if( ALWAYS
1f2b9 28 75 2e 63 6c 2e 70 4d 6f 64 75 6c 65 2d 3e 78 (u.cl.pModule->x
1f2ba 55 70 64 61 74 65 29 20 29 7b 0a 20 20 20 20 75 Update) ){. u
1f2bb 2e 63 6c 2e 61 70 41 72 67 20 3d 20 70 2d 3e 61 .cl.apArg = p->a
1f2bc 70 41 72 67 3b 0a 20 20 20 20 75 2e 63 6c 2e 70 pArg;. u.cl.p
1f2bd 58 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 X = &p->aMem[pOp
1f2be 2d 3e 70 33 5d 3b 0a 20 20 20 20 66 6f 72 28 75 ->p3];. for(u
1f2bf 2e 63 6c 2e 69 3d 30 3b 20 75 2e 63 6c 2e 69 3c .cl.i=0; u.cl.i<
1f2c0 75 2e 63 6c 2e 6e 41 72 67 3b 20 75 2e 63 6c 2e u.cl.nArg; u.cl.
1f2c1 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 i++){. sqli
1f2c2 74 65 33 56 64 62 65 4d 65 6d 53 74 6f 72 65 54 te3VdbeMemStoreT
1f2c3 79 70 65 28 75 2e 63 6c 2e 70 58 29 3b 0a 20 20 ype(u.cl.pX);.
1f2c4 20 20 20 20 75 2e 63 6c 2e 61 70 41 72 67 5b 75 u.cl.apArg[u
1f2c5 2e 63 6c 2e 69 5d 20 3d 20 75 2e 63 6c 2e 70 58 .cl.i] = u.cl.pX
1f2c6 3b 0a 20 20 20 20 20 20 75 2e 63 6c 2e 70 58 2b ;. u.cl.pX+
1f2c7 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 +;. }. if(
1f2c8 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 sqlite3SafetyOf
1f2c9 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f f(db) ) goto abo
1f2ca 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 rt_due_to_misuse
1f2cb 3b 0a 20 20 20 20 72 63 20 3d 20 75 2e 63 6c 2e ;. rc = u.cl.
1f2cc 70 4d 6f 64 75 6c 65 2d 3e 78 55 70 64 61 74 65 pModule->xUpdate
1f2cd 28 75 2e 63 6c 2e 70 56 74 61 62 2c 20 75 2e 63 (u.cl.pVtab, u.c
1f2ce 6c 2e 6e 41 72 67 2c 20 75 2e 63 6c 2e 61 70 41 l.nArg, u.cl.apA
1f2cf 72 67 2c 20 26 75 2e 63 6c 2e 72 6f 77 69 64 29 rg, &u.cl.rowid)
1f2d0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 ;. sqlite3DbF
1f2d1 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d ree(db, p->zErrM
1f2d2 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 sg);. p->zErr
1f2d3 4d 73 67 20 3d 20 75 2e 63 6c 2e 70 56 74 61 62 Msg = u.cl.pVtab
1f2d4 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 75 ->zErrMsg;. u
1f2d5 2e 63 6c 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d .cl.pVtab->zErrM
1f2d6 73 67 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 sg = 0;. if(
1f2d7 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 sqlite3SafetyOn(
1f2d8 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 db) ) goto abort
1f2d9 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a _due_to_misuse;.
1f2da 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
1f2db 54 45 5f 4f 4b 20 26 26 20 70 4f 70 2d 3e 70 31 TE_OK && pOp->p1
1f2dc 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ){. assert
1f2dd 28 20 75 2e 63 6c 2e 6e 41 72 67 3e 31 20 26 26 ( u.cl.nArg>1 &&
1f2de 20 75 2e 63 6c 2e 61 70 41 72 67 5b 30 5d 20 26 u.cl.apArg[0] &
1f2df 26 20 28 75 2e 63 6c 2e 61 70 41 72 67 5b 30 5d & (u.cl.apArg[0]
1f2e0 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c ->flags&MEM_Null
1f2e1 29 20 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 6c ) );. db->l
1f2e2 61 73 74 52 6f 77 69 64 20 3d 20 75 2e 63 6c 2e astRowid = u.cl.
1f2e3 72 6f 77 69 64 3b 0a 20 20 20 20 7d 0a 20 20 20 rowid;. }.
1f2e4 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20 p->nChange++;.
1f2e5 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 }. break;.}.#e
1f2e6 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
1f2e7 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
1f2e8 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 20 53 51 */..#ifndef SQ
1f2e9 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f LITE_OMIT_PAGER_
1f2ea 50 52 41 47 4d 41 53 0a 2f 2a 20 4f 70 63 6f 64 PRAGMAS./* Opcod
1f2eb 65 3a 20 50 61 67 65 63 6f 75 6e 74 20 50 31 20 e: Pagecount P1
1f2ec 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 P2 * * *.**.** W
1f2ed 72 69 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 rite the current
1f2ee 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
1f2ef 20 69 6e 20 64 61 74 61 62 61 73 65 20 50 31 20 in database P1
1f2f0 74 6f 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 50 to memory cell P
1f2f1 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 61 2..*/.case OP_Pa
1f2f2 67 65 63 6f 75 6e 74 3a 20 7b 20 20 20 20 20 20 gecount: {
1f2f3 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 /* out2-pr
1f2f4 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 erelease */.#if
1f2f5 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1f2f6 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1f2f7 20 75 2e 63 6d 20 2a 2f 0a 20 20 69 6e 74 20 70 u.cm */. int p
1f2f8 31 3b 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b 0a 1;. int nPage;.
1f2f9 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b Pager *pPager;
1f2fa 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1f2fb 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1f2fc 20 69 6e 74 6f 20 75 2e 63 6d 20 2a 2f 0a 0a 20 into u.cm */..
1f2fd 20 75 2e 63 6d 2e 70 31 20 3d 20 70 4f 70 2d 3e u.cm.p1 = pOp->
1f2fe 70 31 3b 0a 20 20 75 2e 63 6d 2e 70 50 61 67 65 p1;. u.cm.pPage
1f2ff 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 r = sqlite3Btree
1f300 50 61 67 65 72 28 64 62 2d 3e 61 44 62 5b 75 2e Pager(db->aDb[u.
1f301 63 6d 2e 70 31 5d 2e 70 42 74 29 3b 0a 20 20 72 cm.p1].pBt);. r
1f302 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
1f303 50 61 67 65 63 6f 75 6e 74 28 75 2e 63 6d 2e 70 Pagecount(u.cm.p
1f304 50 61 67 65 72 2c 20 26 75 2e 63 6d 2e 6e 50 61 Pager, &u.cm.nPa
1f305 67 65 29 3b 0a 20 20 2f 2a 20 4f 50 5f 50 61 67 ge);. /* OP_Pag
1f306 65 63 6f 75 6e 74 20 69 73 20 61 6c 77 61 79 73 ecount is always
1f307 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 called from wit
1f308 68 69 6e 20 61 20 72 65 61 64 20 74 72 61 6e 73 hin a read trans
1f309 61 63 74 69 6f 6e 2e 20 20 54 68 65 0a 20 20 2a action. The. *
1f30a 2a 20 70 61 67 65 20 63 6f 75 6e 74 20 68 61 73 * page count has
1f30b 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 73 75 already been su
1f30c 63 63 65 73 73 66 75 6c 6c 79 20 72 65 61 64 20 ccessfully read
1f30d 61 6e 64 20 63 61 63 68 65 64 2e 20 20 53 6f 20 and cached. So
1f30e 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 the. ** sqlite3
1f30f 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 29 PagerPagecount()
1f310 20 63 61 6c 6c 20 61 62 6f 76 65 20 63 61 6e 6e call above cann
1f311 6f 74 20 66 61 69 6c 2e 20 2a 2f 0a 20 20 69 66 ot fail. */. if
1f312 28 20 41 4c 57 41 59 53 28 72 63 3d 3d 53 51 4c ( ALWAYS(rc==SQL
1f313 49 54 45 5f 4f 4b 29 20 29 7b 0a 20 20 20 20 70 ITE_OK) ){. p
1f314 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Out->flags = MEM
1f315 5f 49 6e 74 3b 0a 20 20 20 20 70 4f 75 74 2d 3e _Int;. pOut->
1f316 75 2e 69 20 3d 20 75 2e 63 6d 2e 6e 50 61 67 65 u.i = u.cm.nPage
1f317 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1f318 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 .#endif..#ifndef
1f319 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 SQLITE_OMIT_TRA
1f31a 43 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 72 CE./* Opcode: Tr
1f31b 61 63 65 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a ace * * * P4 *.*
1f31c 2a 0a 2a 2a 20 49 66 20 74 72 61 63 69 6e 67 20 *.** If tracing
1f31d 69 73 20 65 6e 61 62 6c 65 64 20 28 62 79 20 74 is enabled (by t
1f31e 68 65 20 73 71 6c 69 74 65 33 5f 74 72 61 63 65 he sqlite3_trace
1f31f 28 29 29 20 69 6e 74 65 72 66 61 63 65 2c 20 74 ()) interface, t
1f320 68 65 6e 0a 2a 2a 20 74 68 65 20 55 54 46 2d 38 hen.** the UTF-8
1f321 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 65 string containe
1f322 64 20 69 6e 20 50 34 20 69 73 20 65 6d 69 74 74 d in P4 is emitt
1f323 65 64 20 6f 6e 20 74 68 65 20 74 72 61 63 65 20 ed on the trace
1f324 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 63 61 73 callback..*/.cas
1f325 65 20 4f 50 5f 54 72 61 63 65 3a 20 7b 0a 23 69 e OP_Trace: {.#i
1f326 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1f327 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1f328 74 6f 20 75 2e 63 6e 20 2a 2f 0a 20 20 63 68 61 to u.cn */. cha
1f329 72 20 2a 7a 54 72 61 63 65 3b 0a 23 65 6e 64 69 r *zTrace;.#endi
1f32a 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1f32b 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1f32c 75 2e 63 6e 20 2a 2f 0a 0a 20 20 75 2e 63 6e 2e u.cn */.. u.cn.
1f32d 7a 54 72 61 63 65 20 3d 20 28 70 4f 70 2d 3e 70 zTrace = (pOp->p
1f32e 34 2e 7a 20 3f 20 70 4f 70 2d 3e 70 34 2e 7a 20 4.z ? pOp->p4.z
1f32f 3a 20 70 2d 3e 7a 53 71 6c 29 3b 0a 20 20 69 66 : p->zSql);. if
1f330 28 20 75 2e 63 6e 2e 7a 54 72 61 63 65 20 29 7b ( u.cn.zTrace ){
1f331 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 78 54 72 . if( db->xTr
1f332 61 63 65 20 29 7b 0a 20 20 20 20 20 20 64 62 2d ace ){. db-
1f333 3e 78 54 72 61 63 65 28 64 62 2d 3e 70 54 72 61 >xTrace(db->pTra
1f334 63 65 41 72 67 2c 20 75 2e 63 6e 2e 7a 54 72 61 ceArg, u.cn.zTra
1f335 63 65 29 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65 ce);. }.#ifde
1f336 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 f SQLITE_DEBUG.
1f337 20 20 20 69 66 28 20 28 64 62 2d 3e 66 6c 61 67 if( (db->flag
1f338 73 20 26 20 53 51 4c 49 54 45 5f 53 71 6c 54 72 s & SQLITE_SqlTr
1f339 61 63 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 ace)!=0 ){.
1f33a 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 sqlite3DebugPri
1f33b 6e 74 66 28 22 53 51 4c 2d 74 72 61 63 65 3a 20 ntf("SQL-trace:
1f33c 25 73 5c 6e 22 2c 20 75 2e 63 6e 2e 7a 54 72 61 %s\n", u.cn.zTra
1f33d 63 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 ce);. }.#endi
1f33e 66 20 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42 55 f /* SQLITE_DEBU
1f33f 47 20 2a 2f 0a 20 20 7d 0a 20 20 62 72 65 61 6b G */. }. break
1f340 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 20 ;.}.#endif.../*
1f341 4f 70 63 6f 64 65 3a 20 4e 6f 6f 70 20 2a 20 2a Opcode: Noop * *
1f342 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 6f 20 * * *.**.** Do
1f343 6e 6f 74 68 69 6e 67 2e 20 20 54 68 69 73 20 69 nothing. This i
1f344 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 6f 66 nstruction is of
1f345 74 65 6e 20 75 73 65 66 75 6c 20 61 73 20 61 20 ten useful as a
1f346 6a 75 6d 70 0a 2a 2a 20 64 65 73 74 69 6e 61 74 jump.** destinat
1f347 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 ion..*/./*.** Th
1f348 65 20 6d 61 67 69 63 20 45 78 70 6c 61 69 6e 20 e magic Explain
1f349 6f 70 63 6f 64 65 20 61 72 65 20 6f 6e 6c 79 20 opcode are only
1f34a 69 6e 73 65 72 74 65 64 20 77 68 65 6e 20 65 78 inserted when ex
1f34b 70 6c 61 69 6e 3d 3d 32 20 28 77 68 69 63 68 0a plain==2 (which.
1f34c 2a 2a 20 69 73 20 74 6f 20 73 61 79 20 77 68 65 ** is to say whe
1f34d 6e 20 74 68 65 20 45 58 50 4c 41 49 4e 20 51 55 n the EXPLAIN QU
1f34e 45 52 59 20 50 4c 41 4e 20 73 79 6e 74 61 78 20 ERY PLAN syntax
1f34f 69 73 20 75 73 65 64 2e 29 0a 2a 2a 20 54 68 69 is used.).** Thi
1f350 73 20 6f 70 63 6f 64 65 20 72 65 63 6f 72 64 73 s opcode records
1f351 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f information fro
1f352 6d 20 74 68 65 20 6f 70 74 69 6d 69 7a 65 72 2e m the optimizer.
1f353 20 20 49 74 20 69 73 20 74 68 65 0a 2a 2a 20 74 It is the.** t
1f354 68 65 20 73 61 6d 65 20 61 73 20 61 20 6e 6f 2d he same as a no-
1f355 6f 70 2e 20 20 54 68 69 73 20 6f 70 63 6f 64 65 op. This opcode
1f356 73 6e 65 76 65 72 20 61 70 70 65 61 72 73 20 69 snever appears i
1f357 6e 20 61 20 72 65 61 6c 20 56 4d 20 70 72 6f 67 n a real VM prog
1f358 72 61 6d 2e 0a 2a 2f 0a 64 65 66 61 75 6c 74 3a ram..*/.default:
1f359 20 7b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 { /* T
1f35a 68 69 73 20 69 73 20 72 65 61 6c 6c 79 20 4f 50 his is really OP
1f35b 5f 4e 6f 6f 70 20 61 6e 64 20 4f 50 5f 45 78 70 _Noop and OP_Exp
1f35c 6c 61 69 6e 20 2a 2f 0a 20 20 62 72 65 61 6b 3b lain */. break;
1f35d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .}../***********
1f35e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f35f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f361 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f362 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 73 65 73 20 **.** The cases
1f363 6f 66 20 74 68 65 20 73 77 69 74 63 68 20 73 74 of the switch st
1f364 61 74 65 6d 65 6e 74 20 61 62 6f 76 65 20 74 68 atement above th
1f365 69 73 20 6c 69 6e 65 20 73 68 6f 75 6c 64 20 61 is line should a
1f366 6c 6c 20 62 65 20 69 6e 64 65 6e 74 65 64 0a 2a ll be indented.*
1f367 2a 20 62 79 20 36 20 73 70 61 63 65 73 2e 20 20 * by 6 spaces.
1f368 42 75 74 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 But the left-mos
1f369 74 20 36 20 73 70 61 63 65 73 20 68 61 76 65 20 t 6 spaces have
1f36a 62 65 65 6e 20 72 65 6d 6f 76 65 64 20 74 6f 20 been removed to
1f36b 69 6d 70 72 6f 76 65 20 74 68 65 0a 2a 2a 20 72 improve the.** r
1f36c 65 61 64 61 62 69 6c 69 74 79 2e 20 20 46 72 6f eadability. Fro
1f36d 6d 20 74 68 69 73 20 70 6f 69 6e 74 20 6f 6e 20 m this point on
1f36e 64 6f 77 6e 2c 20 74 68 65 20 6e 6f 72 6d 61 6c down, the normal
1f36f 20 69 6e 64 65 6e 74 61 74 69 6f 6e 20 72 75 6c indentation rul
1f370 65 73 20 61 72 65 0a 2a 2a 20 72 65 73 74 6f 72 es are.** restor
1f371 65 64 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ed..************
1f372 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
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 2f 0a 20 20 20 20 7d 0a 0a 23 69 66 64 65 66 */. }..#ifdef
1f377 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 VDBE_PROFILE.
1f378 20 20 7b 0a 20 20 20 20 20 20 75 36 34 20 65 6c {. u64 el
1f379 61 70 73 65 64 20 3d 20 73 71 6c 69 74 65 33 48 apsed = sqlite3H
1f37a 77 74 69 6d 65 28 29 20 2d 20 73 74 61 72 74 3b wtime() - start;
1f37b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 63 79 63 6c . pOp->cycl
1f37c 65 73 20 2b 3d 20 65 6c 61 70 73 65 64 3b 0a 20 es += elapsed;.
1f37d 20 20 20 20 20 70 4f 70 2d 3e 63 6e 74 2b 2b 3b pOp->cnt++;
1f37e 0a 23 69 66 20 30 0a 20 20 20 20 20 20 20 20 66 .#if 0. f
1f37f 70 72 69 6e 74 66 28 73 74 64 6f 75 74 2c 20 22 printf(stdout, "
1f380 25 31 30 6c 6c 75 20 22 2c 20 65 6c 61 70 73 65 %10llu ", elapse
1f381 64 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 d);. sqli
1f382 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 73 te3VdbePrintOp(s
1f383 74 64 6f 75 74 2c 20 6f 72 69 67 50 63 2c 20 26 tdout, origPc, &
1f384 70 2d 3e 61 4f 70 5b 6f 72 69 67 50 63 5d 29 3b p->aOp[origPc]);
1f385 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 23 65 .#endif. }.#e
1f386 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 54 68 65 ndif.. /* The
1f387 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 following code
1f388 61 64 64 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 adds nothing to
1f389 74 68 65 20 61 63 74 75 61 6c 20 66 75 6e 63 74 the actual funct
1f38a 69 6f 6e 61 6c 69 74 79 0a 20 20 20 20 2a 2a 20 ionality. **
1f38b 6f 66 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 20 of the program.
1f38c 20 49 74 20 69 73 20 6f 6e 6c 79 20 68 65 72 65 It is only here
1f38d 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64 for testing and
1f38e 20 64 65 62 75 67 67 69 6e 67 2e 0a 20 20 20 20 debugging..
1f38f 2a 2a 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 ** On the other
1f390 68 61 6e 64 2c 20 69 74 20 64 6f 65 73 20 62 75 hand, it does bu
1f391 72 6e 20 43 50 55 20 63 79 63 6c 65 73 20 65 76 rn CPU cycles ev
1f392 65 72 79 20 74 69 6d 65 20 74 68 72 6f 75 67 68 ery time through
1f393 0a 20 20 20 20 2a 2a 20 74 68 65 20 65 76 61 6c . ** the eval
1f394 75 61 74 6f 72 20 6c 6f 6f 70 2e 20 20 53 6f 20 uator loop. So
1f395 77 65 20 63 61 6e 20 6c 65 61 76 65 20 69 74 20 we can leave it
1f396 6f 75 74 20 77 68 65 6e 20 4e 44 45 42 55 47 20 out when NDEBUG
1f397 69 73 20 64 65 66 69 6e 65 64 2e 0a 20 20 20 20 is defined..
1f398 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 */.#ifndef NDEBU
1f399 47 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 63 G. assert( pc
1f39a 3e 3d 2d 31 20 26 26 20 70 63 3c 70 2d 3e 6e 4f >=-1 && pc<p->nO
1f39b 70 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c p );..#ifdef SQL
1f39c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 69 66 ITE_DEBUG. if
1f39d 28 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 ( p->trace ){.
1f39e 20 20 20 20 69 66 28 20 72 63 21 3d 30 20 29 20 if( rc!=0 )
1f39f 66 70 72 69 6e 74 66 28 70 2d 3e 74 72 61 63 65 fprintf(p->trace
1f3a0 2c 22 72 63 3d 25 64 5c 6e 22 2c 72 63 29 3b 0a ,"rc=%d\n",rc);.
1f3a1 20 20 20 20 20 20 69 66 28 20 6f 70 50 72 6f 70 if( opProp
1f3a2 65 72 74 79 20 26 20 4f 50 46 4c 47 5f 4f 55 54 erty & OPFLG_OUT
1f3a3 32 5f 50 52 45 52 45 4c 45 41 53 45 20 29 7b 0a 2_PRERELEASE ){.
1f3a4 20 20 20 20 20 20 20 20 72 65 67 69 73 74 65 72 register
1f3a5 54 72 61 63 65 28 70 2d 3e 74 72 61 63 65 2c 20 Trace(p->trace,
1f3a6 70 4f 70 2d 3e 70 32 2c 20 70 4f 75 74 29 3b 0a pOp->p2, pOut);.
1f3a7 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
1f3a8 28 20 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f ( opProperty & O
1f3a9 50 46 4c 47 5f 4f 55 54 33 20 29 7b 0a 20 20 20 PFLG_OUT3 ){.
1f3aa 20 20 20 20 20 72 65 67 69 73 74 65 72 54 72 61 registerTra
1f3ab 63 65 28 70 2d 3e 74 72 61 63 65 2c 20 70 4f 70 ce(p->trace, pOp
1f3ac 2d 3e 70 33 2c 20 70 4f 75 74 29 3b 0a 20 20 20 ->p3, pOut);.
1f3ad 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 }. }.#endi
1f3ae 66 20 20 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42 f /* SQLITE_DEB
1f3af 55 47 20 2a 2f 0a 23 65 6e 64 69 66 20 20 2f 2a UG */.#endif /*
1f3b0 20 4e 44 45 42 55 47 20 2a 2f 0a 20 20 7d 20 20 NDEBUG */. }
1f3b1 2f 2a 20 54 68 65 20 65 6e 64 20 6f 66 20 74 68 /* The end of th
1f3b2 65 20 66 6f 72 28 3b 3b 29 20 6c 6f 6f 70 20 74 e for(;;) loop t
1f3b3 68 65 20 6c 6f 6f 70 73 20 74 68 72 6f 75 67 68 he loops through
1f3b4 20 6f 70 63 6f 64 65 73 20 2a 2f 0a 0a 20 20 2f opcodes */.. /
1f3b5 2a 20 49 66 20 77 65 20 72 65 61 63 68 20 74 68 * If we reach th
1f3b6 69 73 20 70 6f 69 6e 74 2c 20 69 74 20 6d 65 61 is point, it mea
1f3b7 6e 73 20 74 68 61 74 20 65 78 65 63 75 74 69 6f ns that executio
1f3b8 6e 20 69 73 20 66 69 6e 69 73 68 65 64 20 77 69 n is finished wi
1f3b9 74 68 0a 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72 th. ** an error
1f3ba 20 6f 66 20 73 6f 6d 65 20 6b 69 6e 64 2e 0a 20 of some kind..
1f3bb 20 2a 2f 0a 76 64 62 65 5f 65 72 72 6f 72 5f 68 */.vdbe_error_h
1f3bc 61 6c 74 3a 0a 20 20 61 73 73 65 72 74 28 20 72 alt:. assert( r
1f3bd 63 20 29 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 72 c );. p->rc = r
1f3be 63 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 c;. sqlite3Vdbe
1f3bf 48 61 6c 74 28 70 29 3b 0a 20 20 69 66 28 20 72 Halt(p);. if( r
1f3c0 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f c==SQLITE_IOERR_
1f3c1 4e 4f 4d 45 4d 20 29 20 64 62 2d 3e 6d 61 6c 6c NOMEM ) db->mall
1f3c2 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 ocFailed = 1;.
1f3c3 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f rc = SQLITE_ERRO
1f3c4 52 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 69 73 R;.. /* This is
1f3c5 20 74 68 65 20 6f 6e 6c 79 20 77 61 79 20 6f 75 the only way ou
1f3c6 74 20 6f 66 20 74 68 69 73 20 70 72 6f 63 65 64 t of this proced
1f3c7 75 72 65 2e 20 20 57 65 20 68 61 76 65 20 74 6f ure. We have to
1f3c8 0a 20 20 2a 2a 20 72 65 6c 65 61 73 65 20 74 68 . ** release th
1f3c9 65 20 6d 75 74 65 78 65 73 20 6f 6e 20 62 74 72 e mutexes on btr
1f3ca 65 65 73 20 74 68 61 74 20 77 65 72 65 20 61 63 ees that were ac
1f3cb 71 75 69 72 65 64 20 61 74 20 74 68 65 0a 20 20 quired at the.
1f3cc 2a 2a 20 74 6f 70 2e 20 2a 2f 0a 76 64 62 65 5f ** top. */.vdbe_
1f3cd 72 65 74 75 72 6e 3a 0a 20 20 73 71 6c 69 74 65 return:. sqlite
1f3ce 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 3BtreeMutexArray
1f3cf 4c 65 61 76 65 28 26 70 2d 3e 61 4d 75 74 65 78 Leave(&p->aMutex
1f3d0 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
1f3d1 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65 . /* Jump to he
1f3d2 72 65 20 69 66 20 61 20 73 74 72 69 6e 67 20 6f re if a string o
1f3d3 72 20 62 6c 6f 62 20 6c 61 72 67 65 72 20 74 68 r blob larger th
1f3d4 61 6e 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 an SQLITE_MAX_LE
1f3d5 4e 47 54 48 0a 20 20 2a 2a 20 69 73 20 65 6e 63 NGTH. ** is enc
1f3d6 6f 75 6e 74 65 72 65 64 2e 0a 20 20 2a 2f 0a 74 ountered.. */.t
1f3d7 6f 6f 5f 62 69 67 3a 0a 20 20 73 71 6c 69 74 65 oo_big:. sqlite
1f3d8 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 3SetString(&p->z
1f3d9 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 73 74 72 ErrMsg, db, "str
1f3da 69 6e 67 20 6f 72 20 62 6c 6f 62 20 74 6f 6f 20 ing or blob too
1f3db 62 69 67 22 29 3b 0a 20 20 72 63 20 3d 20 53 51 big");. rc = SQ
1f3dc 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 67 LITE_TOOBIG;. g
1f3dd 6f 74 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 oto vdbe_error_h
1f3de 61 6c 74 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 alt;.. /* Jump
1f3df 74 6f 20 68 65 72 65 20 69 66 20 61 20 6d 61 6c to here if a mal
1f3e0 6c 6f 63 28 29 20 66 61 69 6c 73 2e 0a 20 20 2a loc() fails.. *
1f3e1 2f 0a 6e 6f 5f 6d 65 6d 3a 0a 20 20 64 62 2d 3e /.no_mem:. db->
1f3e2 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 mallocFailed = 1
1f3e3 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 ;. sqlite3SetSt
1f3e4 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 ring(&p->zErrMsg
1f3e5 2c 20 64 62 2c 20 22 6f 75 74 20 6f 66 20 6d 65 , db, "out of me
1f3e6 6d 6f 72 79 22 29 3b 0a 20 20 72 63 20 3d 20 53 mory");. rc = S
1f3e7 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 67 QLITE_NOMEM;. g
1f3e8 6f 74 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 oto vdbe_error_h
1f3e9 61 6c 74 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 alt;.. /* Jump
1f3ea 74 6f 20 68 65 72 65 20 66 6f 72 20 61 6e 20 53 to here for an S
1f3eb 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 65 72 72 QLITE_MISUSE err
1f3ec 6f 72 2e 0a 20 20 2a 2f 0a 61 62 6f 72 74 5f 64 or.. */.abort_d
1f3ed 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3a 0a 20 20 ue_to_misuse:.
1f3ee 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 rc = SQLITE_MISU
1f3ef 53 45 3b 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 68 SE;. /* Fall th
1f3f0 72 75 20 69 6e 74 6f 20 61 62 6f 72 74 5f 64 75 ru into abort_du
1f3f1 65 5f 74 6f 5f 65 72 72 6f 72 20 2a 2f 0a 0a 20 e_to_error */..
1f3f2 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 /* Jump to here
1f3f3 20 66 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 6b for any other k
1f3f4 69 6e 64 20 6f 66 20 66 61 74 61 6c 20 65 72 72 ind of fatal err
1f3f5 6f 72 2e 20 20 54 68 65 20 22 72 63 22 20 76 61 or. The "rc" va
1f3f6 72 69 61 62 6c 65 0a 20 20 2a 2a 20 73 68 6f 75 riable. ** shou
1f3f7 6c 64 20 68 6f 6c 64 20 74 68 65 20 65 72 72 6f ld hold the erro
1f3f8 72 20 6e 75 6d 62 65 72 2e 0a 20 20 2a 2f 0a 61 r number.. */.a
1f3f9 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f bort_due_to_erro
1f3fa 72 3a 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e r:. assert( p->
1f3fb 7a 45 72 72 4d 73 67 3d 3d 30 20 29 3b 0a 20 20 zErrMsg==0 );.
1f3fc 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 if( db->mallocFa
1f3fd 69 6c 65 64 20 29 20 72 63 20 3d 20 53 51 4c 49 iled ) rc = SQLI
1f3fe 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 69 66 28 20 TE_NOMEM;. if(
1f3ff 72 63 21 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 rc!=SQLITE_IOERR
1f400 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 73 71 _NOMEM ){. sq
1f401 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 lite3SetString(&
1f402 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 p->zErrMsg, db,
1f403 22 25 73 22 2c 20 73 71 6c 69 74 65 33 45 72 72 "%s", sqlite3Err
1f404 53 74 72 28 72 63 29 29 3b 0a 20 20 7d 0a 20 20 Str(rc));. }.
1f405 67 6f 74 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f goto vdbe_error_
1f406 68 61 6c 74 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 halt;.. /* Jump
1f407 20 74 6f 20 68 65 72 65 20 69 66 20 74 68 65 20 to here if the
1f408 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 sqlite3_interrup
1f409 74 28 29 20 41 50 49 20 73 65 74 73 20 74 68 65 t() API sets the
1f40a 20 69 6e 74 65 72 72 75 70 74 0a 20 20 2a 2a 20 interrupt. **
1f40b 66 6c 61 67 2e 0a 20 20 2a 2f 0a 61 62 6f 72 74 flag.. */.abort
1f40c 5f 64 75 65 5f 74 6f 5f 69 6e 74 65 72 72 75 70 _due_to_interrup
1f40d 74 3a 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d t:. assert( db-
1f40e 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 >u1.isInterrupte
1f40f 64 20 29 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 d );. rc = SQLI
1f410 54 45 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 TE_INTERRUPT;.
1f411 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 73 71 p->rc = rc;. sq
1f412 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 lite3SetString(&
1f413 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 p->zErrMsg, db,
1f414 22 25 73 22 2c 20 73 71 6c 69 74 65 33 45 72 72 "%s", sqlite3Err
1f415 53 74 72 28 72 63 29 29 3b 0a 20 20 67 6f 74 6f Str(rc));. goto
1f416 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74 vdbe_error_halt
1f417 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ;.}../**********
1f418 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 **** End of vdbe
1f419 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
1f41a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f41b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f41c 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
1f41d 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
1f41e 76 64 62 65 62 6c 6f 62 2e 63 20 2a 2a 2a 2a 2a vdbeblob.c *****
1f41f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f421 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 ***/./*.** 2007
1f422 4d 61 79 20 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20 May 1.**.** The
1f423 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
1f424 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
1f425 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
1f426 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
1f427 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
1f428 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
1f429 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
1f42a 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
1f42b 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
1f42c 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
1f42d 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
1f42e 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
1f42f 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
1f430 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
1f431 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
1f432 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
1f433 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
1f434 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
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 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f438 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ****.**.** This
1f439 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f file contains co
1f43a 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 de used to imple
1f43b 6d 65 6e 74 20 69 6e 63 72 65 6d 65 6e 74 61 6c ment incremental
1f43c 20 42 4c 4f 42 20 49 2f 4f 2e 0a 2a 2a 0a 2a 2a BLOB I/O..**.**
1f43d 20 24 49 64 3a 20 76 64 62 65 62 6c 6f 62 2e 63 $Id: vdbeblob.c
1f43e 2c 76 20 31 2e 33 35 20 32 30 30 39 2f 30 37 2f ,v 1.35 2009/07/
1f43f 30 32 20 30 37 3a 34 37 3a 33 33 20 64 61 6e 69 02 07:47:33 dani
1f440 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f elk1977 Exp $.*/
1f441 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ...#ifndef SQLIT
1f442 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a E_OMIT_INCRBLOB.
1f443 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20 73 71 6c ./*.** Valid sql
1f444 69 74 65 33 5f 62 6c 6f 62 2a 20 68 61 6e 64 6c ite3_blob* handl
1f445 65 73 20 70 6f 69 6e 74 20 74 6f 20 49 6e 63 72 es point to Incr
1f446 62 6c 6f 62 20 73 74 72 75 63 74 75 72 65 73 2e blob structures.
1f447 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
1f448 63 74 20 49 6e 63 72 62 6c 6f 62 20 49 6e 63 72 ct Incrblob Incr
1f449 62 6c 6f 62 3b 0a 73 74 72 75 63 74 20 49 6e 63 blob;.struct Inc
1f44a 72 62 6c 6f 62 20 7b 0a 20 20 69 6e 74 20 66 6c rblob {. int fl
1f44b 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 ags;
1f44c 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 22 66 6c /* Copy of "fl
1f44d 61 67 73 22 20 70 61 73 73 65 64 20 74 6f 20 73 ags" passed to s
1f44e 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e qlite3_blob_open
1f44f 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 () */. int nByt
1f450 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
1f451 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 70 65 6e 20 /* Size of open
1f452 62 6c 6f 62 2c 20 69 6e 20 62 79 74 65 73 20 2a blob, in bytes *
1f453 2f 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74 3b /. int iOffset;
1f454 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 /* B
1f455 79 74 65 20 6f 66 66 73 65 74 20 6f 66 20 62 6c yte offset of bl
1f456 6f 62 20 69 6e 20 63 75 72 73 6f 72 20 64 61 74 ob in cursor dat
1f457 61 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 a */. BtCursor
1f458 2a 70 43 73 72 3b 20 20 20 20 20 20 20 20 20 2f *pCsr; /
1f459 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e * Cursor pointin
1f45a 67 20 61 74 20 62 6c 6f 62 20 72 6f 77 20 2a 2f g at blob row */
1f45b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 . sqlite3_stmt
1f45c 2a 70 53 74 6d 74 3b 20 20 20 20 2f 2a 20 53 74 *pStmt; /* St
1f45d 61 74 65 6d 65 6e 74 20 68 6f 6c 64 69 6e 67 20 atement holding
1f45e 63 75 72 73 6f 72 20 6f 70 65 6e 20 2a 2f 0a 20 cursor open */.
1f45f 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 sqlite3 *db;
1f460 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1f461 61 73 73 6f 63 69 61 74 65 64 20 64 61 74 61 62 associated datab
1f462 61 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ase */.};../*.**
1f463 20 4f 70 65 6e 20 61 20 62 6c 6f 62 20 68 61 6e Open a blob han
1f464 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 dle..*/.SQLITE_A
1f465 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 PI int sqlite3_b
1f466 6c 6f 62 5f 6f 70 65 6e 28 0a 20 20 73 71 6c 69 lob_open(. sqli
1f467 74 65 33 2a 20 64 62 2c 20 20 20 20 20 20 20 20 te3* db,
1f468 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 /* The datab
1f469 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a ase connection *
1f46a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
1f46b 7a 44 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 zDb, /* T
1f46c 68 65 20 61 74 74 61 63 68 65 64 20 64 61 74 61 he attached data
1f46d 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 base containing
1f46e 74 68 65 20 62 6c 6f 62 20 2a 2f 0a 20 20 63 6f the blob */. co
1f46f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 nst char *zTable
1f470 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 , /* The tab
1f471 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 le containing th
1f472 65 20 62 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73 e blob */. cons
1f473 74 20 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e 2c t char *zColumn,
1f474 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d /* The colum
1f475 6e 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 n containing the
1f476 20 62 6c 6f 62 20 2a 2f 0a 20 20 73 71 6c 69 74 blob */. sqlit
1f477 65 5f 69 6e 74 36 34 20 69 52 6f 77 2c 20 20 20 e_int64 iRow,
1f478 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 20 63 6f /* The row co
1f479 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 67 6c 6f ntaining the glo
1f47a 62 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 b */. int flags
1f47b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
1f47c 2a 20 54 72 75 65 20 2d 3e 20 72 65 61 64 2f 77 * True -> read/w
1f47d 72 69 74 65 20 61 63 63 65 73 73 2c 20 66 61 6c rite access, fal
1f47e 73 65 20 2d 3e 20 72 65 61 64 2d 6f 6e 6c 79 20 se -> read-only
1f47f 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f */. sqlite3_blo
1f480 62 20 2a 2a 70 70 42 6c 6f 62 20 20 20 2f 2a 20 b **ppBlob /*
1f481 48 61 6e 64 6c 65 20 66 6f 72 20 61 63 63 65 73 Handle for acces
1f482 73 69 6e 67 20 74 68 65 20 62 6c 6f 62 20 72 65 sing the blob re
1f483 74 75 72 6e 65 64 20 68 65 72 65 20 2a 2f 0a 29 turned here */.)
1f484 7b 0a 20 20 69 6e 74 20 6e 41 74 74 65 6d 70 74 {. int nAttempt
1f485 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 43 6f 6c = 0;. int iCol
1f486 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1f487 2f 2a 20 49 6e 64 65 78 20 6f 66 20 7a 43 6f 6c /* Index of zCol
1f488 75 6d 6e 20 69 6e 20 72 6f 77 2d 72 65 63 6f 72 umn in row-recor
1f489 64 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 d */.. /* This
1f48a 56 44 42 45 20 70 72 6f 67 72 61 6d 20 73 65 65 VDBE program see
1f48b 6b 73 20 61 20 62 74 72 65 65 20 63 75 72 73 6f ks a btree curso
1f48c 72 20 74 6f 20 74 68 65 20 69 64 65 6e 74 69 66 r to the identif
1f48d 69 65 64 20 0a 20 20 2a 2a 20 64 62 2f 74 61 62 ied . ** db/tab
1f48e 6c 65 2f 72 6f 77 20 65 6e 74 72 79 2e 20 54 68 le/row entry. Th
1f48f 65 20 72 65 61 73 6f 6e 20 66 6f 72 20 75 73 69 e reason for usi
1f490 6e 67 20 61 20 76 64 62 65 20 70 72 6f 67 72 61 ng a vdbe progra
1f491 6d 20 69 6e 73 74 65 61 64 0a 20 20 2a 2a 20 6f m instead. ** o
1f492 66 20 77 72 69 74 69 6e 67 20 63 6f 64 65 20 74 f writing code t
1f493 6f 20 75 73 65 20 74 68 65 20 62 2d 74 72 65 65 o use the b-tree
1f494 20 6c 61 79 65 72 20 64 69 72 65 63 74 6c 79 20 layer directly
1f495 69 73 20 74 68 61 74 20 74 68 65 0a 20 20 2a 2a is that the. **
1f496 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 77 69 vdbe program wi
1f497 6c 6c 20 74 61 6b 65 20 61 64 76 61 6e 74 61 67 ll take advantag
1f498 65 20 6f 66 20 74 68 65 20 76 61 72 69 6f 75 73 e of the various
1f499 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a 20 20 transaction,.
1f49a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 61 6e 64 20 65 ** locking and e
1f49b 72 72 6f 72 20 68 61 6e 64 6c 69 6e 67 20 69 6e rror handling in
1f49c 66 72 61 73 74 72 75 63 74 75 72 65 20 62 75 69 frastructure bui
1f49d 6c 74 20 69 6e 74 6f 20 74 68 65 20 76 64 62 65 lt into the vdbe
1f49e 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 66 74 65 .. **. ** Afte
1f49f 72 20 73 65 65 6b 69 6e 67 20 74 68 65 20 63 75 r seeking the cu
1f4a0 72 73 6f 72 2c 20 74 68 65 20 76 64 62 65 20 65 rsor, the vdbe e
1f4a1 78 65 63 75 74 65 73 20 61 6e 20 4f 50 5f 52 65 xecutes an OP_Re
1f4a2 73 75 6c 74 52 6f 77 2e 0a 20 20 2a 2a 20 43 6f sultRow.. ** Co
1f4a3 64 65 20 65 78 74 65 72 6e 61 6c 20 74 6f 20 74 de external to t
1f4a4 68 65 20 56 64 62 65 20 74 68 65 6e 20 22 62 6f he Vdbe then "bo
1f4a5 72 72 6f 77 73 22 20 74 68 65 20 62 2d 74 72 65 rrows" the b-tre
1f4a6 65 20 63 75 72 73 6f 72 20 61 6e 64 0a 20 20 2a e cursor and. *
1f4a7 2a 20 75 73 65 73 20 69 74 20 74 6f 20 69 6d 70 * uses it to imp
1f4a8 6c 65 6d 65 6e 74 20 74 68 65 20 62 6c 6f 62 5f lement the blob_
1f4a9 72 65 61 64 28 29 2c 20 62 6c 6f 62 5f 77 72 69 read(), blob_wri
1f4aa 74 65 28 29 20 61 6e 64 20 0a 20 20 2a 2a 20 62 te() and . ** b
1f4ab 6c 6f 62 5f 62 79 74 65 73 28 29 20 66 75 6e 63 lob_bytes() func
1f4ac 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a tions.. **. **
1f4ad 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f The sqlite3_blo
1f4ae 62 5f 63 6c 6f 73 65 28 29 20 66 75 6e 63 74 69 b_close() functi
1f4af 6f 6e 20 66 69 6e 61 6c 69 7a 65 73 20 74 68 65 on finalizes the
1f4b0 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2c 0a 20 vdbe program,.
1f4b1 20 2a 2a 20 77 68 69 63 68 20 63 6c 6f 73 65 73 ** which closes
1f4b2 20 74 68 65 20 62 2d 74 72 65 65 20 63 75 72 73 the b-tree curs
1f4b3 6f 72 20 61 6e 64 20 28 70 6f 73 73 69 62 6c 79 or and (possibly
1f4b4 29 20 63 6f 6d 6d 69 74 73 20 74 68 65 20 0a 20 ) commits the .
1f4b5 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e ** transaction.
1f4b6 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 . */. static c
1f4b7 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20 onst VdbeOpList
1f4b8 6f 70 65 6e 42 6c 6f 62 5b 5d 20 3d 20 7b 0a 20 openBlob[] = {.
1f4b9 20 20 20 7b 4f 50 5f 54 72 61 6e 73 61 63 74 69 {OP_Transacti
1f4ba 6f 6e 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 on, 0, 0, 0},
1f4bb 20 20 2f 2a 20 30 3a 20 53 74 61 72 74 20 61 20 /* 0: Start a
1f4bc 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 transaction */.
1f4bd 20 20 20 7b 4f 50 5f 56 65 72 69 66 79 43 6f 6f {OP_VerifyCoo
1f4be 6b 69 65 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 kie, 0, 0, 0},
1f4bf 20 20 2f 2a 20 31 3a 20 43 68 65 63 6b 20 74 68 /* 1: Check th
1f4c0 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 e schema cookie
1f4c1 2a 2f 0a 20 20 20 20 7b 4f 50 5f 54 61 62 6c 65 */. {OP_Table
1f4c2 4c 6f 63 6b 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 Lock, 0, 0, 0},
1f4c3 20 20 20 20 20 20 2f 2a 20 32 3a 20 41 63 71 75 /* 2: Acqu
1f4c4 69 72 65 20 61 20 72 65 61 64 20 6f 72 20 77 72 ire a read or wr
1f4c5 69 74 65 20 6c 6f 63 6b 20 2a 2f 0a 0a 20 20 20 ite lock */..
1f4c6 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 66 /* One of the f
1f4c7 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 69 6e 73 ollowing two ins
1f4c8 74 72 75 63 74 69 6f 6e 73 20 69 73 20 72 65 70 tructions is rep
1f4c9 6c 61 63 65 64 20 62 79 20 61 6e 20 4f 50 5f 4e laced by an OP_N
1f4ca 6f 6f 70 2e 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f oop. */. {OP_
1f4cb 4f 70 65 6e 52 65 61 64 2c 20 30 2c 20 30 2c 20 OpenRead, 0, 0,
1f4cc 30 7d 2c 20 20 20 20 20 20 20 20 2f 2a 20 33 3a 0}, /* 3:
1f4cd 20 4f 70 65 6e 20 63 75 72 73 6f 72 20 30 20 66 Open cursor 0 f
1f4ce 6f 72 20 72 65 61 64 69 6e 67 20 2a 2f 0a 20 20 or reading */.
1f4cf 20 20 7b 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c {OP_OpenWrite,
1f4d0 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 0, 0, 0},
1f4d1 20 2f 2a 20 34 3a 20 4f 70 65 6e 20 63 75 72 73 /* 4: Open curs
1f4d2 6f 72 20 30 20 66 6f 72 20 72 65 61 64 2f 77 72 or 0 for read/wr
1f4d3 69 74 65 20 2a 2f 0a 0a 20 20 20 20 7b 4f 50 5f ite */.. {OP_
1f4d4 56 61 72 69 61 62 6c 65 2c 20 31 2c 20 31 2c 20 Variable, 1, 1,
1f4d5 31 7d 2c 20 20 20 20 20 20 20 20 2f 2a 20 35 3a 1}, /* 5:
1f4d6 20 50 75 73 68 20 74 68 65 20 72 6f 77 69 64 20 Push the rowid
1f4d7 74 6f 20 74 68 65 20 73 74 61 63 6b 20 2a 2f 0a to the stack */.
1f4d8 20 20 20 20 7b 4f 50 5f 4e 6f 74 45 78 69 73 74 {OP_NotExist
1f4d9 73 2c 20 30 2c 20 39 2c 20 31 7d 2c 20 20 20 20 s, 0, 9, 1},
1f4da 20 20 20 2f 2a 20 36 3a 20 53 65 65 6b 20 74 68 /* 6: Seek th
1f4db 65 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20 e cursor */.
1f4dc 7b 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 30 2c 20 30 {OP_Column, 0, 0
1f4dd 2c 20 31 7d 2c 20 20 20 20 20 20 20 20 20 20 2f , 1}, /
1f4de 2a 20 37 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f * 7 */. {OP_
1f4df 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 30 2c ResultRow, 1, 0,
1f4e0 20 30 7d 2c 20 20 20 20 20 20 20 2f 2a 20 38 20 0}, /* 8
1f4e1 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 43 6c 6f 73 */. {OP_Clos
1f4e2 65 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 e, 0, 0, 0},
1f4e3 20 20 20 20 20 20 20 2f 2a 20 39 20 20 2a 2f 0a /* 9 */.
1f4e4 20 20 20 20 7b 4f 50 5f 48 61 6c 74 2c 20 30 2c {OP_Halt, 0,
1f4e5 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 20 20 0, 0},
1f4e6 20 20 20 2f 2a 20 31 30 20 2a 2f 0a 20 20 7d 3b /* 10 */. };
1f4e7 0a 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 30 3b .. Vdbe *v = 0;
1f4e8 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
1f4e9 54 45 5f 4f 4b 3b 0a 20 20 63 68 61 72 20 2a 7a TE_OK;. char *z
1f4ea 45 72 72 20 3d 20 30 3b 0a 20 20 54 61 62 6c 65 Err = 0;. Table
1f4eb 20 2a 70 54 61 62 3b 0a 20 20 50 61 72 73 65 20 *pTab;. Parse
1f4ec 2a 70 50 61 72 73 65 3b 0a 0a 20 20 2a 70 70 42 *pParse;.. *ppB
1f4ed 6c 6f 62 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 lob = 0;. sqlit
1f4ee 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 e3_mutex_enter(d
1f4ef 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 50 61 b->mutex);. pPa
1f4f0 72 73 65 20 3d 20 73 71 6c 69 74 65 33 53 74 61 rse = sqlite3Sta
1f4f1 63 6b 41 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 ckAllocRaw(db, s
1f4f2 69 7a 65 6f 66 28 2a 70 50 61 72 73 65 29 29 3b izeof(*pParse));
1f4f3 0a 20 20 69 66 28 20 70 50 61 72 73 65 3d 3d 30 . if( pParse==0
1f4f4 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ){. rc = SQL
1f4f5 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 ITE_NOMEM;. g
1f4f6 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 oto blob_open_ou
1f4f7 74 3b 0a 20 20 7d 0a 20 20 64 6f 20 7b 0a 20 20 t;. }. do {.
1f4f8 20 20 6d 65 6d 73 65 74 28 70 50 61 72 73 65 2c memset(pParse,
1f4f9 20 30 2c 20 73 69 7a 65 6f 66 28 50 61 72 73 65 0, sizeof(Parse
1f4fa 29 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e ));. pParse->
1f4fb 64 62 20 3d 20 64 62 3b 0a 0a 20 20 20 20 69 66 db = db;.. if
1f4fc 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f ( sqlite3SafetyO
1f4fd 6e 28 64 62 29 20 29 7b 0a 20 20 20 20 20 20 73 n(db) ){. s
1f4fe 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1f4ff 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 73 71 zErr);. sq
1f500 6c 69 74 65 33 53 74 61 63 6b 46 72 65 65 28 64 lite3StackFree(d
1f501 62 2c 20 70 50 61 72 73 65 29 3b 0a 20 20 20 20 b, pParse);.
1f502 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
1f503 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 leave(db->mutex)
1f504 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 ;. return S
1f505 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 QLITE_MISUSE;.
1f506 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 }.. sqlite3
1f507 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 BtreeEnterAll(db
1f508 29 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 73 71 );. pTab = sq
1f509 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 lite3LocateTable
1f50a 28 70 50 61 72 73 65 2c 20 30 2c 20 7a 54 61 62 (pParse, 0, zTab
1f50b 6c 65 2c 20 7a 44 62 29 3b 0a 20 20 20 20 69 66 le, zDb);. if
1f50c 28 20 70 54 61 62 20 26 26 20 49 73 56 69 72 74 ( pTab && IsVirt
1f50d 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 ual(pTab) ){.
1f50e 20 20 20 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 pTab = 0;.
1f50f 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
1f510 73 67 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e sg(pParse, "cann
1f511 6f 74 20 6f 70 65 6e 20 76 69 72 74 75 61 6c 20 ot open virtual
1f512 74 61 62 6c 65 3a 20 25 73 22 2c 20 7a 54 61 62 table: %s", zTab
1f513 6c 65 29 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 le);. }.#ifnd
1f514 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ef SQLITE_OMIT_V
1f515 49 45 57 0a 20 20 20 20 69 66 28 20 70 54 61 62 IEW. if( pTab
1f516 20 26 26 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 && pTab->pSelec
1f517 74 20 29 7b 0a 20 20 20 20 20 20 70 54 61 62 20 t ){. pTab
1f518 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 = 0;. sqlit
1f519 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
1f51a 65 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 e, "cannot open
1f51b 76 69 65 77 3a 20 25 73 22 2c 20 7a 54 61 62 6c view: %s", zTabl
1f51c 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 e);. }.#endif
1f51d 0a 20 20 20 20 69 66 28 20 21 70 54 61 62 20 29 . if( !pTab )
1f51e 7b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 72 {. if( pPar
1f51f 73 65 2d 3e 7a 45 72 72 4d 73 67 20 29 7b 0a 20 se->zErrMsg ){.
1f520 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 sqlite3Db
1f521 46 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a Free(db, zErr);.
1f522 20 20 20 20 20 20 20 20 7a 45 72 72 20 3d 20 70 zErr = p
1f523 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 3b 0a Parse->zErrMsg;.
1f524 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e pParse->
1f525 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 zErrMsg = 0;.
1f526 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 }. rc =
1f527 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 SQLITE_ERROR;.
1f528 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 (void)sqlite
1f529 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 3SafetyOff(db);.
1f52a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 sqlite3Btr
1f52b 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a eeLeaveAll(db);.
1f52c 20 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f goto blob_
1f52d 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a open_out;. }.
1f52e 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 73 65 61 72 . /* Now sear
1f52f 63 68 20 70 54 61 62 20 66 6f 72 20 74 68 65 20 ch pTab for the
1f530 65 78 61 63 74 20 63 6f 6c 75 6d 6e 2e 20 2a 2f exact column. */
1f531 0a 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b . for(iCol=0;
1f532 20 69 43 6f 6c 20 3c 20 70 54 61 62 2d 3e 6e 43 iCol < pTab->nC
1f533 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 20 7b 0a 20 20 ol; iCol++) {.
1f534 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 if( sqlite3S
1f535 74 72 49 43 6d 70 28 70 54 61 62 2d 3e 61 43 6f trICmp(pTab->aCo
1f536 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 2c 20 7a l[iCol].zName, z
1f537 43 6f 6c 75 6d 6e 29 3d 3d 30 20 29 7b 0a 20 20 Column)==0 ){.
1f538 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1f539 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 }. }. i
1f53a 66 28 20 69 43 6f 6c 3d 3d 70 54 61 62 2d 3e 6e f( iCol==pTab->n
1f53b 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c Col ){. sql
1f53c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a ite3DbFree(db, z
1f53d 45 72 72 29 3b 0a 20 20 20 20 20 20 7a 45 72 72 Err);. zErr
1f53e 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 = sqlite3MPrint
1f53f 66 28 64 62 2c 20 22 6e 6f 20 73 75 63 68 20 63 f(db, "no such c
1f540 6f 6c 75 6d 6e 3a 20 5c 22 25 73 5c 22 22 2c 20 olumn: \"%s\"",
1f541 7a 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 zColumn);.
1f542 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f rc = SQLITE_ERRO
1f543 52 3b 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73 R;. (void)s
1f544 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 qlite3SafetyOff(
1f545 64 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 db);. sqlit
1f546 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 e3BtreeLeaveAll(
1f547 64 62 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 db);. goto
1f548 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 blob_open_out;.
1f549 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 }.. /* If
1f54a 74 68 65 20 76 61 6c 75 65 20 69 73 20 62 65 69 the value is bei
1f54b 6e 67 20 6f 70 65 6e 65 64 20 66 6f 72 20 77 72 ng opened for wr
1f54c 69 74 69 6e 67 2c 20 63 68 65 63 6b 20 74 68 61 iting, check tha
1f54d 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f 6c t the. ** col
1f54e 75 6d 6e 20 69 73 20 6e 6f 74 20 69 6e 64 65 78 umn is not index
1f54f 65 64 2c 20 61 6e 64 20 74 68 61 74 20 69 74 20 ed, and that it
1f550 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 61 is not part of a
1f551 20 66 6f 72 65 69 67 6e 20 6b 65 79 2e 20 0a 20 foreign key. .
1f552 20 20 20 2a 2a 20 49 74 20 69 73 20 61 67 61 69 ** It is agai
1f553 6e 73 74 20 74 68 65 20 72 75 6c 65 73 20 74 6f nst the rules to
1f554 20 6f 70 65 6e 20 61 20 63 6f 6c 75 6d 6e 20 74 open a column t
1f555 6f 20 77 68 69 63 68 20 65 69 74 68 65 72 20 6f o which either o
1f556 66 20 74 68 65 73 65 0a 20 20 20 20 2a 2a 20 64 f these. ** d
1f557 65 73 63 72 69 70 74 69 6f 6e 73 20 61 70 70 6c escriptions appl
1f558 69 65 73 20 66 6f 72 20 77 72 69 74 69 6e 67 2e ies for writing.
1f559 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 66 6c 61 */. if( fla
1f55a 67 73 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 gs ){. cons
1f55b 74 20 63 68 61 72 20 2a 7a 46 61 75 6c 74 20 3d t char *zFault =
1f55c 20 30 3b 0a 20 20 20 20 20 20 49 6e 64 65 78 20 0;. Index
1f55d 2a 70 49 64 78 3b 0a 23 69 66 6e 64 65 66 20 53 *pIdx;.#ifndef S
1f55e 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 QLITE_OMIT_FOREI
1f55f 47 4e 5f 4b 45 59 0a 20 20 20 20 20 20 69 66 28 GN_KEY. if(
1f560 20 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 db->flags&SQLIT
1f561 45 5f 46 6f 72 65 69 67 6e 4b 65 79 73 20 29 7b E_ForeignKeys ){
1f562 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63 . /* Chec
1f563 6b 20 74 68 61 74 20 74 68 65 20 63 6f 6c 75 6d k that the colum
1f564 6e 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 n is not part of
1f565 20 61 6e 20 46 4b 20 63 68 69 6c 64 20 6b 65 79 an FK child key
1f566 20 64 65 66 69 6e 69 74 69 6f 6e 2e 20 49 74 0a definition. It.
1f567 20 20 20 20 20 20 20 20 2a 2a 20 69 73 20 6e 6f ** is no
1f568 74 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 63 t necessary to c
1f569 68 65 63 6b 20 69 66 20 69 74 20 69 73 20 70 61 heck if it is pa
1f56a 72 74 20 6f 66 20 61 20 70 61 72 65 6e 74 20 6b rt of a parent k
1f56b 65 79 2c 20 61 73 20 70 61 72 65 6e 74 0a 20 20 ey, as parent.
1f56c 20 20 20 20 20 20 2a 2a 20 6b 65 79 20 63 6f 6c ** key col
1f56d 75 6d 6e 73 20 6d 75 73 74 20 62 65 20 69 6e 64 umns must be ind
1f56e 65 78 65 64 2e 20 54 68 65 20 63 68 65 63 6b 20 exed. The check
1f56f 62 65 6c 6f 77 20 77 69 6c 6c 20 70 69 63 6b 20 below will pick
1f570 75 70 20 74 68 69 73 20 0a 20 20 20 20 20 20 20 up this .
1f571 20 2a 2a 20 63 61 73 65 2e 20 20 2a 2f 0a 20 20 ** case. */.
1f572 20 20 20 20 20 20 46 4b 65 79 20 2a 70 46 4b 65 FKey *pFKe
1f573 79 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 70 y;. for(p
1f574 46 4b 65 79 3d 70 54 61 62 2d 3e 70 46 4b 65 79 FKey=pTab->pFKey
1f575 3b 20 70 46 4b 65 79 3b 20 70 46 4b 65 79 3d 70 ; pFKey; pFKey=p
1f576 46 4b 65 79 2d 3e 70 4e 65 78 74 46 72 6f 6d 29 FKey->pNextFrom)
1f577 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 {. int
1f578 6a 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 j;. for
1f579 28 6a 3d 30 3b 20 6a 3c 70 46 4b 65 79 2d 3e 6e (j=0; j<pFKey->n
1f57a 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 Col; j++){.
1f57b 20 20 20 20 20 20 20 69 66 28 20 70 46 4b 65 79 if( pFKey
1f57c 2d 3e 61 43 6f 6c 5b 6a 5d 2e 69 46 72 6f 6d 3d ->aCol[j].iFrom=
1f57d 3d 69 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 =iCol ){.
1f57e 20 20 20 20 20 20 20 7a 46 61 75 6c 74 20 3d 20 zFault =
1f57f 22 66 6f 72 65 69 67 6e 20 6b 65 79 22 3b 0a 20 "foreign key";.
1f580 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
1f581 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1f582 20 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 }. }.#endi
1f583 66 0a 20 20 20 20 20 20 66 6f 72 28 70 49 64 78 f. for(pIdx
1f584 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 =pTab->pIndex; p
1f585 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e Idx; pIdx=pIdx->
1f586 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20 pNext){.
1f587 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20 20 66 int j;. f
1f588 6f 72 28 6a 3d 30 3b 20 6a 3c 70 49 64 78 2d 3e or(j=0; j<pIdx->
1f589 6e 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20 nColumn; j++){.
1f58a 20 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64 if( pId
1f58b 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 3d 3d x->aiColumn[j]==
1f58c 69 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 iCol ){.
1f58d 20 20 20 20 7a 46 61 75 6c 74 20 3d 20 22 69 6e zFault = "in
1f58e 64 65 78 65 64 22 3b 0a 20 20 20 20 20 20 20 20 dexed";.
1f58f 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
1f590 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
1f591 7a 46 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20 zFault ){.
1f592 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1f593 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 db, zErr);.
1f594 20 20 20 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 zErr = sqlite
1f595 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 63 61 3MPrintf(db, "ca
1f596 6e 6e 6f 74 20 6f 70 65 6e 20 25 73 20 63 6f 6c nnot open %s col
1f597 75 6d 6e 20 66 6f 72 20 77 72 69 74 69 6e 67 22 umn for writing"
1f598 2c 20 7a 46 61 75 6c 74 29 3b 0a 20 20 20 20 20 , zFault);.
1f599 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 rc = SQLITE_E
1f59a 52 52 4f 52 3b 0a 20 20 20 20 20 20 20 20 28 76 RROR;. (v
1f59b 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 oid)sqlite3Safet
1f59c 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20 20 yOff(db);.
1f59d 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
1f59e 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 aveAll(db);.
1f59f 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 goto blob_op
1f5a0 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a en_out;. }.
1f5a1 20 20 20 20 7d 0a 0a 20 20 20 20 76 20 3d 20 73 }.. v = s
1f5a2 71 6c 69 74 65 33 56 64 62 65 43 72 65 61 74 65 qlite3VdbeCreate
1f5a3 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 76 20 (db);. if( v
1f5a4 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62 ){. int iDb
1f5a5 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 = sqlite3Schema
1f5a6 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 ToIndex(db, pTab
1f5a7 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 ->pSchema);.
1f5a8 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1f5a9 4f 70 4c 69 73 74 28 76 2c 20 73 69 7a 65 6f 66 OpList(v, sizeof
1f5aa 28 6f 70 65 6e 42 6c 6f 62 29 2f 73 69 7a 65 6f (openBlob)/sizeo
1f5ab 66 28 56 64 62 65 4f 70 4c 69 73 74 29 2c 20 6f f(VdbeOpList), o
1f5ac 70 65 6e 42 6c 6f 62 29 3b 0a 20 20 20 20 20 20 penBlob);.
1f5ad 66 6c 61 67 73 20 3d 20 21 21 66 6c 61 67 73 3b flags = !!flags;
1f5ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f5af 20 2f 2a 20 66 6c 61 67 73 20 3d 20 28 66 6c 61 /* flags = (fla
1f5b0 67 73 20 3f 20 31 20 3a 20 30 29 3b 20 2a 2f 0a gs ? 1 : 0); */.
1f5b1 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 . /* Config
1f5b2 75 72 65 20 74 68 65 20 4f 50 5f 54 72 61 6e 73 ure the OP_Trans
1f5b3 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 action */.
1f5b4 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
1f5b5 65 50 31 28 76 2c 20 30 2c 20 69 44 62 29 3b 0a eP1(v, 0, iDb);.
1f5b6 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1f5b7 65 43 68 61 6e 67 65 50 32 28 76 2c 20 30 2c 20 eChangeP2(v, 0,
1f5b8 66 6c 61 67 73 29 3b 0a 0a 20 20 20 20 20 20 2f flags);.. /
1f5b9 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 * Configure the
1f5ba 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 OP_VerifyCookie
1f5bb 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 */. sqlite3
1f5bc 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 VdbeChangeP1(v,
1f5bd 31 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 1, iDb);. s
1f5be 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
1f5bf 50 32 28 76 2c 20 31 2c 20 70 54 61 62 2d 3e 70 P2(v, 1, pTab->p
1f5c0 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 Schema->schema_c
1f5c1 6f 6f 6b 69 65 29 3b 0a 0a 20 20 20 20 20 20 2f ookie);.. /
1f5c2 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 20 6d 75 * Make sure a mu
1f5c3 74 65 78 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 tex is held on t
1f5c4 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 61 he table to be a
1f5c5 63 63 65 73 73 65 64 20 2a 2f 0a 20 20 20 20 20 ccessed */.
1f5c6 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 sqlite3VdbeUses
1f5c7 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b 20 0a Btree(v, iDb); .
1f5c8 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 . /* Config
1f5c9 75 72 65 20 74 68 65 20 4f 50 5f 54 61 62 6c 65 ure the OP_Table
1f5ca 4c 6f 63 6b 20 69 6e 73 74 72 75 63 74 69 6f 6e Lock instruction
1f5cb 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 */. sqlite
1f5cc 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 3VdbeChangeP1(v,
1f5cd 20 32 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 2, iDb);.
1f5ce 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
1f5cf 65 50 32 28 76 2c 20 32 2c 20 70 54 61 62 2d 3e eP2(v, 2, pTab->
1f5d0 74 6e 75 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c tnum);. sql
1f5d1 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 ite3VdbeChangeP3
1f5d2 28 76 2c 20 32 2c 20 66 6c 61 67 73 29 3b 0a 20 (v, 2, flags);.
1f5d3 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1f5d4 43 68 61 6e 67 65 50 34 28 76 2c 20 32 2c 20 70 ChangeP4(v, 2, p
1f5d5 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 54 Tab->zName, P4_T
1f5d6 52 41 4e 53 49 45 4e 54 29 3b 0a 0a 20 20 20 20 RANSIENT);..
1f5d7 20 20 2f 2a 20 52 65 6d 6f 76 65 20 65 69 74 68 /* Remove eith
1f5d8 65 72 20 74 68 65 20 4f 50 5f 4f 70 65 6e 57 72 er the OP_OpenWr
1f5d9 69 74 65 20 6f 72 20 4f 70 65 6e 52 65 61 64 2e ite or OpenRead.
1f5da 20 53 65 74 20 74 68 65 20 50 32 20 0a 20 20 20 Set the P2 .
1f5db 20 20 20 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 ** parameter
1f5dc 6f 66 20 74 68 65 20 6f 74 68 65 72 20 74 6f 20 of the other to
1f5dd 70 54 61 62 2d 3e 74 6e 75 6d 2e 20 20 2a 2f 0a pTab->tnum. */.
1f5de 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1f5df 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c eChangeToNoop(v,
1f5e0 20 34 20 2d 20 66 6c 61 67 73 2c 20 31 29 3b 0a 4 - flags, 1);.
1f5e1 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1f5e2 65 43 68 61 6e 67 65 50 32 28 76 2c 20 33 20 2b eChangeP2(v, 3 +
1f5e3 20 66 6c 61 67 73 2c 20 70 54 61 62 2d 3e 74 6e flags, pTab->tn
1f5e4 75 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 um);. sqlit
1f5e5 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 76 e3VdbeChangeP3(v
1f5e6 2c 20 33 20 2b 20 66 6c 61 67 73 2c 20 69 44 62 , 3 + flags, iDb
1f5e7 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e );.. /* Con
1f5e8 66 69 67 75 72 65 20 74 68 65 20 6e 75 6d 62 65 figure the numbe
1f5e9 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 2e 20 43 6f r of columns. Co
1f5ea 6e 66 69 67 75 72 65 20 74 68 65 20 63 75 72 73 nfigure the curs
1f5eb 6f 72 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 or to. ** t
1f5ec 68 69 6e 6b 20 74 68 61 74 20 74 68 65 20 74 61 hink that the ta
1f5ed 62 6c 65 20 68 61 73 20 6f 6e 65 20 6d 6f 72 65 ble has one more
1f5ee 20 63 6f 6c 75 6d 6e 20 74 68 61 6e 20 69 74 20 column than it
1f5ef 72 65 61 6c 6c 79 0a 20 20 20 20 20 20 2a 2a 20 really. **
1f5f0 64 6f 65 73 2e 20 41 6e 20 4f 50 5f 43 6f 6c 75 does. An OP_Colu
1f5f1 6d 6e 20 74 6f 20 72 65 74 72 69 65 76 65 20 74 mn to retrieve t
1f5f2 68 69 73 20 69 6d 61 67 69 6e 61 72 79 20 63 6f his imaginary co
1f5f3 6c 75 6d 6e 20 77 69 6c 6c 0a 20 20 20 20 20 20 lumn will.
1f5f4 2a 2a 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e ** always return
1f5f5 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e 20 54 68 an SQL NULL. Th
1f5f6 69 73 20 69 73 20 75 73 65 66 75 6c 20 62 65 63 is is useful bec
1f5f7 61 75 73 65 20 69 74 20 6d 65 61 6e 73 0a 20 20 ause it means.
1f5f8 20 20 20 20 2a 2a 20 77 65 20 63 61 6e 20 69 6e ** we can in
1f5f9 76 6f 6b 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 74 voke OP_Column t
1f5fa 6f 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 76 64 o fill in the vd
1f5fb 62 65 20 63 75 72 73 6f 72 73 20 74 79 70 65 20 be cursors type
1f5fc 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 6f 66 . ** and of
1f5fd 66 73 65 74 20 63 61 63 68 65 20 77 69 74 68 6f fset cache witho
1f5fe 75 74 20 63 61 75 73 69 6e 67 20 61 6e 79 20 49 ut causing any I
1f5ff 4f 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 O.. */.
1f600 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 sqlite3VdbeCha
1f601 6e 67 65 50 34 28 76 2c 20 33 2b 66 6c 61 67 73 ngeP4(v, 3+flags
1f602 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f , SQLITE_INT_TO_
1f603 50 54 52 28 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31 PTR(pTab->nCol+1
1f604 29 2c 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20 20 ),P4_INT32);.
1f605 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 sqlite3VdbeCh
1f606 61 6e 67 65 50 32 28 76 2c 20 37 2c 20 70 54 61 angeP2(v, 7, pTa
1f607 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 20 20 b->nCol);.
1f608 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 if( !db->mallocF
1f609 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20 ailed ){.
1f60a 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 sqlite3VdbeMake
1f60b 52 65 61 64 79 28 76 2c 20 31 2c 20 31 2c 20 31 Ready(v, 1, 1, 1
1f60c 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 , 0, 0, 0);.
1f60d 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 0a 20 20 }. }. .
1f60e 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
1f60f 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 aveAll(db);.
1f610 72 63 20 3d 20 73 71 6c 69 74 65 33 53 61 66 65 rc = sqlite3Safe
1f611 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 69 tyOff(db);. i
1f612 66 28 20 4e 45 56 45 52 28 72 63 21 3d 53 51 4c f( NEVER(rc!=SQL
1f613 49 54 45 5f 4f 4b 29 20 7c 7c 20 64 62 2d 3e 6d ITE_OK) || db->m
1f614 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 allocFailed ){.
1f615 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f goto blob_o
1f616 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a pen_out;. }..
1f617 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 sqlite3_bind
1f618 5f 69 6e 74 36 34 28 28 73 71 6c 69 74 65 33 5f _int64((sqlite3_
1f619 73 74 6d 74 20 2a 29 76 2c 20 31 2c 20 69 52 6f stmt *)v, 1, iRo
1f61a 77 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c w);. rc = sql
1f61b 69 74 65 33 5f 73 74 65 70 28 28 73 71 6c 69 74 ite3_step((sqlit
1f61c 65 33 5f 73 74 6d 74 20 2a 29 76 29 3b 0a 20 20 e3_stmt *)v);.
1f61d 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1f61e 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 6e 41 _ROW ){. nA
1f61f 74 74 65 6d 70 74 2b 2b 3b 0a 20 20 20 20 20 20 ttempt++;.
1f620 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 66 69 6e rc = sqlite3_fin
1f621 61 6c 69 7a 65 28 28 73 71 6c 69 74 65 33 5f 73 alize((sqlite3_s
1f622 74 6d 74 20 2a 29 76 29 3b 0a 20 20 20 20 20 20 tmt *)v);.
1f623 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1f624 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 7a , zErr);. z
1f625 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 Err = sqlite3MPr
1f626 69 6e 74 66 28 64 62 2c 20 73 71 6c 69 74 65 33 intf(db, sqlite3
1f627 5f 65 72 72 6d 73 67 28 64 62 29 29 3b 0a 20 20 _errmsg(db));.
1f628 20 20 20 20 76 20 3d 20 30 3b 0a 20 20 20 20 7d v = 0;. }
1f629 0a 20 20 7d 20 77 68 69 6c 65 28 20 6e 41 74 74 . } while( nAtt
1f62a 65 6d 70 74 3c 35 20 26 26 20 72 63 3d 3d 53 51 empt<5 && rc==SQ
1f62b 4c 49 54 45 5f 53 43 48 45 4d 41 20 29 3b 0a 0a LITE_SCHEMA );..
1f62c 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1f62d 5f 52 4f 57 20 29 7b 0a 20 20 20 20 2f 2a 20 54 _ROW ){. /* T
1f62e 68 65 20 72 6f 77 2d 72 65 63 6f 72 64 20 68 61 he row-record ha
1f62f 73 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 73 75 s been opened su
1f630 63 63 65 73 73 66 75 6c 6c 79 2e 20 43 68 65 63 ccessfully. Chec
1f631 6b 20 74 68 61 74 20 74 68 65 0a 20 20 20 20 2a k that the. *
1f632 2a 20 63 6f 6c 75 6d 6e 20 69 6e 20 71 75 65 73 * column in ques
1f633 74 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 74 65 tion contains te
1f634 78 74 20 6f 72 20 61 20 62 6c 6f 62 2e 20 49 66 xt or a blob. If
1f635 20 69 74 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20 it contains.
1f636 20 2a 2a 20 74 65 78 74 2c 20 69 74 20 69 73 20 ** text, it is
1f637 75 70 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 up to the caller
1f638 20 74 6f 20 67 65 74 20 74 68 65 20 65 6e 63 6f to get the enco
1f639 64 69 6e 67 20 72 69 67 68 74 2e 0a 20 20 20 20 ding right..
1f63a 2a 2f 0a 20 20 20 20 49 6e 63 72 62 6c 6f 62 20 */. Incrblob
1f63b 2a 70 42 6c 6f 62 3b 0a 20 20 20 20 75 33 32 20 *pBlob;. u32
1f63c 74 79 70 65 20 3d 20 76 2d 3e 61 70 43 73 72 5b type = v->apCsr[
1f63d 30 5d 2d 3e 61 54 79 70 65 5b 69 43 6f 6c 5d 3b 0]->aType[iCol];
1f63e 0a 0a 20 20 20 20 69 66 28 20 74 79 70 65 3c 31 .. if( type<1
1f63f 32 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 2 ){. sqlit
1f640 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 e3DbFree(db, zEr
1f641 72 29 3b 0a 20 20 20 20 20 20 7a 45 72 72 20 3d r);. zErr =
1f642 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 sqlite3MPrintf(
1f643 64 62 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e db, "cannot open
1f644 20 76 61 6c 75 65 20 6f 66 20 74 79 70 65 20 25 value of type %
1f645 73 22 2c 0a 20 20 20 20 20 20 20 20 20 20 74 79 s",. ty
1f646 70 65 3d 3d 30 3f 22 6e 75 6c 6c 22 3a 20 74 79 pe==0?"null": ty
1f647 70 65 3d 3d 37 3f 22 72 65 61 6c 22 3a 20 22 69 pe==7?"real": "i
1f648 6e 74 65 67 65 72 22 0a 20 20 20 20 20 20 29 3b nteger". );
1f649 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
1f64a 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 TE_ERROR;.
1f64b 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f goto blob_open_o
1f64c 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 ut;. }. pB
1f64d 6c 6f 62 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20 lob = (Incrblob
1f64e 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f *)sqlite3DbMallo
1f64f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 cZero(db, sizeof
1f650 28 49 6e 63 72 62 6c 6f 62 29 29 3b 0a 20 20 20 (Incrblob));.
1f651 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 if( db->mallocF
1f652 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 73 ailed ){. s
1f653 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1f654 20 70 42 6c 6f 62 29 3b 0a 20 20 20 20 20 20 67 pBlob);. g
1f655 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 oto blob_open_ou
1f656 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 6c t;. }. pBl
1f657 6f 62 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61 67 ob->flags = flag
1f658 73 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 70 43 s;. pBlob->pC
1f659 73 72 20 3d 20 20 76 2d 3e 61 70 43 73 72 5b 30 sr = v->apCsr[0
1f65a 5d 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 20 20 ]->pCursor;.
1f65b 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
1f65c 72 43 75 72 73 6f 72 28 70 42 6c 6f 62 2d 3e 70 rCursor(pBlob->p
1f65d 43 73 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 Csr);. sqlite
1f65e 33 42 74 72 65 65 43 61 63 68 65 4f 76 65 72 66 3BtreeCacheOverf
1f65f 6c 6f 77 28 70 42 6c 6f 62 2d 3e 70 43 73 72 29 low(pBlob->pCsr)
1f660 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 ;. sqlite3Btr
1f661 65 65 4c 65 61 76 65 43 75 72 73 6f 72 28 70 42 eeLeaveCursor(pB
1f662 6c 6f 62 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20 lob->pCsr);.
1f663 70 42 6c 6f 62 2d 3e 70 53 74 6d 74 20 3d 20 28 pBlob->pStmt = (
1f664 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 76 sqlite3_stmt *)v
1f665 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 69 4f 66 ;. pBlob->iOf
1f666 66 73 65 74 20 3d 20 76 2d 3e 61 70 43 73 72 5b fset = v->apCsr[
1f667 30 5d 2d 3e 61 4f 66 66 73 65 74 5b 69 43 6f 6c 0]->aOffset[iCol
1f668 5d 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 6e 42 ];. pBlob->nB
1f669 79 74 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 yte = sqlite3Vdb
1f66a 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 74 eSerialTypeLen(t
1f66b 79 70 65 29 3b 0a 20 20 20 20 70 42 6c 6f 62 2d ype);. pBlob-
1f66c 3e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 2a 70 >db = db;. *p
1f66d 70 42 6c 6f 62 20 3d 20 28 73 71 6c 69 74 65 33 pBlob = (sqlite3
1f66e 5f 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 _blob *)pBlob;.
1f66f 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
1f670 4b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 72 K;. }else if( r
1f671 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
1f672 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1f673 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 e(db, zErr);.
1f674 20 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d zErr = sqlite3M
1f675 50 72 69 6e 74 66 28 64 62 2c 20 22 6e 6f 20 73 Printf(db, "no s
1f676 75 63 68 20 72 6f 77 69 64 3a 20 25 6c 6c 64 22 uch rowid: %lld"
1f677 2c 20 69 52 6f 77 29 3b 0a 20 20 20 20 72 63 20 , iRow);. rc
1f678 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a = SQLITE_ERROR;.
1f679 20 20 7d 0a 0a 62 6c 6f 62 5f 6f 70 65 6e 5f 6f }..blob_open_o
1f67a 75 74 3a 0a 20 20 69 66 28 20 76 20 26 26 20 28 ut:. if( v && (
1f67b 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c rc!=SQLITE_OK ||
1f67c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
1f67d 64 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 d) ){. sqlite
1f67e 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 76 29 3VdbeFinalize(v)
1f67f 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 ;. }. sqlite3E
1f680 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 7a 45 72 rror(db, rc, zEr
1f681 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 r);. sqlite3DbF
1f682 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 ree(db, zErr);.
1f683 20 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65 sqlite3StackFre
1f684 65 28 64 62 2c 20 70 50 61 72 73 65 29 3b 0a 20 e(db, pParse);.
1f685 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 rc = sqlite3Api
1f686 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 Exit(db, rc);.
1f687 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
1f688 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a ave(db->mutex);.
1f689 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1f68a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 62 6c /*.** Close a bl
1f68b 6f 62 20 68 61 6e 64 6c 65 20 74 68 61 74 20 77 ob handle that w
1f68c 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 63 72 as previously cr
1f68d 65 61 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 73 eated using.** s
1f68e 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e qlite3_blob_open
1f68f 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ()..*/.SQLITE_AP
1f690 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c I int sqlite3_bl
1f691 6f 62 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33 ob_close(sqlite3
1f692 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 20 _blob *pBlob){.
1f693 20 49 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20 28 Incrblob *p = (
1f694 49 6e 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 Incrblob *)pBlob
1f695 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 ;. int rc;. sq
1f696 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66 lite3 *db;.. if
1f697 28 20 70 20 29 7b 0a 20 20 20 20 64 62 20 3d 20 ( p ){. db =
1f698 70 2d 3e 64 62 3b 0a 20 20 20 20 73 71 6c 69 74 p->db;. sqlit
1f699 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 e3_mutex_enter(d
1f69a 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 b->mutex);. r
1f69b 63 20 3d 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 c = sqlite3_fina
1f69c 6c 69 7a 65 28 70 2d 3e 70 53 74 6d 74 29 3b 0a lize(p->pStmt);.
1f69d 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1f69e 65 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 73 71 e(db, p);. sq
1f69f 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
1f6a0 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 e(db->mutex);.
1f6a1 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 }else{. rc =
1f6a2 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 SQLITE_OK;. }.
1f6a3 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1f6a4 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20 72 *.** Perform a r
1f6a5 65 61 64 20 6f 72 20 77 72 69 74 65 20 6f 70 65 ead or write ope
1f6a6 72 61 74 69 6f 6e 20 6f 6e 20 61 20 62 6c 6f 62 ration on a blob
1f6a7 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 .*/.static int b
1f6a8 6c 6f 62 52 65 61 64 57 72 69 74 65 28 0a 20 20 lobReadWrite(.
1f6a9 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 70 42 sqlite3_blob *pB
1f6aa 6c 6f 62 2c 20 0a 20 20 76 6f 69 64 20 2a 7a 2c lob, . void *z,
1f6ab 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20 69 6e . int n, . in
1f6ac 74 20 69 4f 66 66 73 65 74 2c 20 0a 20 20 69 6e t iOffset, . in
1f6ad 74 20 28 2a 78 43 61 6c 6c 29 28 42 74 43 75 72 t (*xCall)(BtCur
1f6ae 73 6f 72 2a 2c 20 75 33 32 2c 20 75 33 32 2c 20 sor*, u32, u32,
1f6af 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 69 6e 74 20 void*).){. int
1f6b0 72 63 3b 0a 20 20 49 6e 63 72 62 6c 6f 62 20 2a rc;. Incrblob *
1f6b1 70 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20 2a 29 p = (Incrblob *)
1f6b2 70 42 6c 6f 62 3b 0a 20 20 56 64 62 65 20 2a 76 pBlob;. Vdbe *v
1f6b3 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b ;. sqlite3 *db;
1f6b4 0a 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 .. if( p==0 ) r
1f6b5 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 eturn SQLITE_MIS
1f6b6 55 53 45 3b 0a 20 20 64 62 20 3d 20 70 2d 3e 64 USE;. db = p->d
1f6b7 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 b;. sqlite3_mut
1f6b8 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 ex_enter(db->mut
1f6b9 65 78 29 3b 0a 20 20 76 20 3d 20 28 56 64 62 65 ex);. v = (Vdbe
1f6ba 2a 29 70 2d 3e 70 53 74 6d 74 3b 0a 0a 20 20 69 *)p->pStmt;.. i
1f6bb 66 28 20 6e 3c 30 20 7c 7c 20 69 4f 66 66 73 65 f( n<0 || iOffse
1f6bc 74 3c 30 20 7c 7c 20 28 69 4f 66 66 73 65 74 2b t<0 || (iOffset+
1f6bd 6e 29 3e 70 2d 3e 6e 42 79 74 65 20 29 7b 0a 20 n)>p->nByte ){.
1f6be 20 20 20 2f 2a 20 52 65 71 75 65 73 74 20 69 73 /* Request is
1f6bf 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2e 20 52 out of range. R
1f6c0 65 74 75 72 6e 20 61 20 74 72 61 6e 73 69 65 6e eturn a transien
1f6c1 74 20 65 72 72 6f 72 2e 20 2a 2f 0a 20 20 20 20 t error. */.
1f6c2 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f rc = SQLITE_ERRO
1f6c3 52 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 R;. sqlite3Er
1f6c4 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 45 ror(db, SQLITE_E
1f6c5 52 52 4f 52 2c 20 30 29 3b 0a 20 20 7d 20 65 6c RROR, 0);. } el
1f6c6 73 65 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a 20 se if( v==0 ){.
1f6c7 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 /* If there i
1f6c8 73 20 6e 6f 20 73 74 61 74 65 6d 65 6e 74 20 68 s no statement h
1f6c9 61 6e 64 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 andle, then the
1f6ca 62 6c 6f 62 2d 68 61 6e 64 6c 65 20 68 61 73 0a blob-handle has.
1f6cb 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 62 ** already b
1f6cc 65 65 6e 20 69 6e 76 61 6c 69 64 61 74 65 64 2e een invalidated.
1f6cd 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 Return SQLITE_A
1f6ce 42 4f 52 54 20 69 6e 20 74 68 69 73 20 63 61 73 BORT in this cas
1f6cf 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 e.. */. rc
1f6d0 20 3d 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b = SQLITE_ABORT;
1f6d1 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
1f6d2 20 43 61 6c 6c 20 65 69 74 68 65 72 20 42 74 72 Call either Btr
1f6d3 65 65 44 61 74 61 28 29 20 6f 72 20 42 74 72 65 eeData() or Btre
1f6d4 65 50 75 74 44 61 74 61 28 29 2e 20 49 66 20 53 ePutData(). If S
1f6d5 51 4c 49 54 45 5f 41 42 4f 52 54 20 69 73 0a 20 QLITE_ABORT is.
1f6d6 20 20 20 2a 2a 20 72 65 74 75 72 6e 65 64 2c 20 ** returned,
1f6d7 63 6c 65 61 6e 2d 75 70 20 74 68 65 20 73 74 61 clean-up the sta
1f6d8 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 2e 0a 20 tement handle..
1f6d9 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 */. assert
1f6da 28 20 64 62 20 3d 3d 20 76 2d 3e 64 62 20 29 3b ( db == v->db );
1f6db 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
1f6dc 65 45 6e 74 65 72 43 75 72 73 6f 72 28 70 2d 3e eEnterCursor(p->
1f6dd 70 43 73 72 29 3b 0a 20 20 20 20 72 63 20 3d 20 pCsr);. rc =
1f6de 78 43 61 6c 6c 28 70 2d 3e 70 43 73 72 2c 20 69 xCall(p->pCsr, i
1f6df 4f 66 66 73 65 74 2b 70 2d 3e 69 4f 66 66 73 65 Offset+p->iOffse
1f6e0 74 2c 20 6e 2c 20 7a 29 3b 0a 20 20 20 20 73 71 t, n, z);. sq
1f6e1 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 43 lite3BtreeLeaveC
1f6e2 75 72 73 6f 72 28 70 2d 3e 70 43 73 72 29 3b 0a ursor(p->pCsr);.
1f6e3 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
1f6e4 54 45 5f 41 42 4f 52 54 20 29 7b 0a 20 20 20 20 TE_ABORT ){.
1f6e5 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e sqlite3VdbeFin
1f6e6 61 6c 69 7a 65 28 76 29 3b 0a 20 20 20 20 20 20 alize(v);.
1f6e7 70 2d 3e 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20 p->pStmt = 0;.
1f6e8 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 64 }else{. d
1f6e9 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 63 3b b->errCode = rc;
1f6ea 0a 20 20 20 20 20 20 76 2d 3e 72 63 20 3d 20 72 . v->rc = r
1f6eb 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 c;. }. }. r
1f6ec 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 c = sqlite3ApiEx
1f6ed 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 it(db, rc);. sq
1f6ee 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
1f6ef 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 e(db->mutex);.
1f6f0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
1f6f1 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20 66 72 .** Read data fr
1f6f2 6f 6d 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 om a blob handle
1f6f3 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
1f6f4 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 int sqlite3_blob
1f6f5 5f 72 65 61 64 28 73 71 6c 69 74 65 33 5f 62 6c _read(sqlite3_bl
1f6f6 6f 62 20 2a 70 42 6c 6f 62 2c 20 76 6f 69 64 20 ob *pBlob, void
1f6f7 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 69 *z, int n, int i
1f6f8 4f 66 66 73 65 74 29 7b 0a 20 20 72 65 74 75 72 Offset){. retur
1f6f9 6e 20 62 6c 6f 62 52 65 61 64 57 72 69 74 65 28 n blobReadWrite(
1f6fa 70 42 6c 6f 62 2c 20 7a 2c 20 6e 2c 20 69 4f 66 pBlob, z, n, iOf
1f6fb 66 73 65 74 2c 20 73 71 6c 69 74 65 33 42 74 72 fset, sqlite3Btr
1f6fc 65 65 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a eeData);.}../*.*
1f6fd 2a 20 57 72 69 74 65 20 64 61 74 61 20 74 6f 20 * Write data to
1f6fe 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 2e 0a 2a a blob handle..*
1f6ff 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
1f700 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 sqlite3_blob_wr
1f701 69 74 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 ite(sqlite3_blob
1f702 20 2a 70 42 6c 6f 62 2c 20 63 6f 6e 73 74 20 76 *pBlob, const v
1f703 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 oid *z, int n, i
1f704 6e 74 20 69 4f 66 66 73 65 74 29 7b 0a 20 20 72 nt iOffset){. r
1f705 65 74 75 72 6e 20 62 6c 6f 62 52 65 61 64 57 72 eturn blobReadWr
1f706 69 74 65 28 70 42 6c 6f 62 2c 20 28 76 6f 69 64 ite(pBlob, (void
1f707 20 2a 29 7a 2c 20 6e 2c 20 69 4f 66 66 73 65 74 *)z, n, iOffset
1f708 2c 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 75 , sqlite3BtreePu
1f709 74 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a tData);.}../*.**
1f70a 20 51 75 65 72 79 20 61 20 62 6c 6f 62 20 68 61 Query a blob ha
1f70b 6e 64 6c 65 20 66 6f 72 20 74 68 65 20 73 69 7a ndle for the siz
1f70c 65 20 6f 66 20 74 68 65 20 64 61 74 61 2e 0a 2a e of the data..*
1f70d 2a 0a 2a 2a 20 54 68 65 20 49 6e 63 72 62 6c 6f *.** The Incrblo
1f70e 62 2e 6e 42 79 74 65 20 66 69 65 6c 64 20 69 73 b.nByte field is
1f70f 20 66 69 78 65 64 20 66 6f 72 20 74 68 65 20 6c fixed for the l
1f710 69 66 65 74 69 6d 65 20 6f 66 20 74 68 65 20 49 ifetime of the I
1f711 6e 63 72 62 6c 6f 62 0a 2a 2a 20 73 6f 20 6e 6f ncrblob.** so no
1f712 20 6d 75 74 65 78 20 69 73 20 72 65 71 75 69 72 mutex is requir
1f713 65 64 20 66 6f 72 20 61 63 63 65 73 73 2e 0a 2a ed for access..*
1f714 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
1f715 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 sqlite3_blob_by
1f716 74 65 73 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 tes(sqlite3_blob
1f717 20 2a 70 42 6c 6f 62 29 7b 0a 20 20 49 6e 63 72 *pBlob){. Incr
1f718 62 6c 6f 62 20 2a 70 20 3d 20 28 49 6e 63 72 62 blob *p = (Incrb
1f719 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 20 72 lob *)pBlob;. r
1f71a 65 74 75 72 6e 20 70 20 3f 20 70 2d 3e 6e 42 79 eturn p ? p->nBy
1f71b 74 65 20 3a 20 30 3b 0a 7d 0a 0a 23 65 6e 64 69 te : 0;.}..#endi
1f71c 66 20 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c f /* #ifndef SQL
1f71d 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f ITE_OMIT_INCRBLO
1f71e 42 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a B */../*********
1f71f 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 ***** End of vdb
1f720 65 62 6c 6f 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a eblob.c ********
1f721 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f722 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f723 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
1f724 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
1f725 20 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a journal.c *****
1f726 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f727 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f728 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 ****/./*.** 2007
1f729 20 41 75 67 75 73 74 20 32 32 0a 2a 2a 0a 2a 2a August 22.**.**
1f72a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
1f72b 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
1f72c 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
1f72d 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
1f72e 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
1f72f 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
1f730 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
1f731 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
1f732 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
1f733 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
1f734 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
1f735 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
1f736 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
1f737 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
1f738 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
1f739 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
1f73a 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
1f73b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f73c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
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 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
1f740 40 28 23 29 20 24 49 64 3a 20 6a 6f 75 72 6e 61 @(#) $Id: journa
1f741 6c 2e 63 2c 76 20 31 2e 39 20 32 30 30 39 2f 30 l.c,v 1.9 2009/0
1f742 31 2f 32 30 20 31 37 3a 30 36 3a 32 37 20 64 61 1/20 17:06:27 da
1f743 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a nielk1977 Exp $.
1f744 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 */..#ifdef SQLIT
1f745 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f E_ENABLE_ATOMIC_
1f746 57 52 49 54 45 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 WRITE../*.** Thi
1f747 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 s file implement
1f748 73 20 61 20 73 70 65 63 69 61 6c 20 6b 69 6e 64 s a special kind
1f749 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 of sqlite3_file
1f74a 20 6f 62 6a 65 63 74 20 75 73 65 64 0a 2a 2a 20 object used.**
1f74b 62 79 20 53 51 4c 69 74 65 20 74 6f 20 63 72 65 by SQLite to cre
1f74c 61 74 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ate journal file
1f74d 73 20 69 66 20 74 68 65 20 61 74 6f 6d 69 63 2d s if the atomic-
1f74e 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 write optimizati
1f74f 6f 6e 0a 2a 2a 20 69 73 20 65 6e 61 62 6c 65 64 on.** is enabled
1f750 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 73 74 ..**.** The dist
1f751 69 6e 63 74 69 76 65 20 63 68 61 72 61 63 74 65 inctive characte
1f752 72 69 73 74 69 63 20 6f 66 20 74 68 69 73 20 73 ristic of this s
1f753 71 6c 69 74 65 33 5f 66 69 6c 65 20 69 73 20 74 qlite3_file is t
1f754 68 61 74 20 74 68 65 0a 2a 2a 20 61 63 74 75 61 hat the.** actua
1f755 6c 20 6f 6e 20 64 69 73 6b 20 66 69 6c 65 20 69 l on disk file i
1f756 73 20 63 72 65 61 74 65 64 20 6c 61 7a 69 6c 79 s created lazily
1f757 2e 20 57 68 65 6e 20 74 68 65 20 66 69 6c 65 20 . When the file
1f758 69 73 20 63 72 65 61 74 65 64 2c 0a 2a 2a 20 74 is created,.** t
1f759 68 65 20 63 61 6c 6c 65 72 20 73 70 65 63 69 66 he caller specif
1f75a 69 65 73 20 61 20 62 75 66 66 65 72 20 73 69 7a ies a buffer siz
1f75b 65 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f e for an in-memo
1f75c 72 79 20 62 75 66 66 65 72 20 74 6f 0a 2a 2a 20 ry buffer to.**
1f75d 62 65 20 75 73 65 64 20 74 6f 20 73 65 72 76 69 be used to servi
1f75e 63 65 20 72 65 61 64 28 29 20 61 6e 64 20 77 72 ce read() and wr
1f75f 69 74 65 28 29 20 72 65 71 75 65 73 74 73 2e 20 ite() requests.
1f760 54 68 65 20 61 63 74 75 61 6c 20 66 69 6c 65 0a The actual file.
1f761 2a 2a 20 6f 6e 20 64 69 73 6b 20 69 73 20 6e 6f ** on disk is no
1f762 74 20 63 72 65 61 74 65 64 20 6f 72 20 70 6f 70 t created or pop
1f763 75 6c 61 74 65 64 20 75 6e 74 69 6c 20 65 69 74 ulated until eit
1f764 68 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 her:.**.** 1)
1f765 54 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 The in-memory re
1f766 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 67 72 6f presentation gro
1f767 77 73 20 74 6f 6f 20 6c 61 72 67 65 20 66 6f 72 ws too large for
1f768 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 0a the allocated .
1f769 2a 2a 20 20 20 20 20 20 62 75 66 66 65 72 2c 20 ** buffer,
1f76a 6f 72 0a 2a 2a 20 20 20 32 29 20 54 68 65 20 73 or.** 2) The s
1f76b 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 qlite3JournalCre
1f76c 61 74 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 ate() function i
1f76d 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 0a 0a 0a s called..*/....
1f76e 2f 2a 0a 2a 2a 20 41 20 4a 6f 75 72 6e 61 6c 46 /*.** A JournalF
1f76f 69 6c 65 20 6f 62 6a 65 63 74 20 69 73 20 61 20 ile object is a
1f770 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 6c 69 subclass of sqli
1f771 74 65 33 5f 66 69 6c 65 20 75 73 65 64 20 62 79 te3_file used by
1f772 0a 2a 2a 20 61 73 20 61 6e 20 6f 70 65 6e 20 66 .** as an open f
1f773 69 6c 65 20 68 61 6e 64 6c 65 20 66 6f 72 20 6a ile handle for j
1f774 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2f ournal files..*/
1f775 0a 73 74 72 75 63 74 20 4a 6f 75 72 6e 61 6c 46 .struct JournalF
1f776 69 6c 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f ile {. sqlite3_
1f777 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74 io_methods *pMet
1f778 68 6f 64 3b 20 20 20 20 2f 2a 20 49 2f 4f 20 6d hod; /* I/O m
1f779 65 74 68 6f 64 73 20 6f 6e 20 6a 6f 75 72 6e 61 ethods on journa
1f77a 6c 20 66 69 6c 65 73 20 2a 2f 0a 20 20 69 6e 74 l files */. int
1f77b 20 6e 42 75 66 3b 20 20 20 20 20 20 20 20 20 20 nBuf;
1f77c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1f77d 53 69 7a 65 20 6f 66 20 7a 42 75 66 5b 5d 20 69 Size of zBuf[] i
1f77e 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 63 68 61 n bytes */. cha
1f77f 72 20 2a 7a 42 75 66 3b 20 20 20 20 20 20 20 20 r *zBuf;
1f780 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1f781 53 70 61 63 65 20 74 6f 20 62 75 66 66 65 72 20 Space to buffer
1f782 6a 6f 75 72 6e 61 6c 20 77 72 69 74 65 73 20 2a journal writes *
1f783 2f 0a 20 20 69 6e 74 20 69 53 69 7a 65 3b 20 20 /. int iSize;
1f784 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f785 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 /* Amount of
1f786 20 7a 42 75 66 5b 5d 20 63 75 72 72 65 6e 74 6c zBuf[] currentl
1f787 79 20 75 73 65 64 20 2a 2f 0a 20 20 69 6e 74 20 y used */. int
1f788 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 flags;
1f789 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
1f78a 4f 70 65 6e 20 66 6c 61 67 73 20 2a 2f 0a 20 20 Open flags */.
1f78b 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
1f78c 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s;
1f78d 2f 2a 20 54 68 65 20 22 72 65 61 6c 22 20 75 6e /* The "real" un
1f78e 64 65 72 6c 79 69 6e 67 20 56 46 53 20 2a 2f 0a derlying VFS */.
1f78f 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a sqlite3_file *
1f790 70 52 65 61 6c 3b 20 20 20 20 20 20 20 20 20 20 pReal;
1f791 20 20 2f 2a 20 54 68 65 20 22 72 65 61 6c 22 20 /* The "real"
1f792 75 6e 64 65 72 6c 79 69 6e 67 20 66 69 6c 65 20 underlying file
1f793 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 descriptor */.
1f794 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4a 6f 75 const char *zJou
1f795 72 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20 rnal;
1f796 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6a /* Name of the j
1f797 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 7d ournal file */.}
1f798 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
1f799 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 4a 6f 75 JournalFile Jou
1f79a 72 6e 61 6c 46 69 6c 65 3b 0a 0a 2f 2a 0a 2a 2a rnalFile;../*.**
1f79b 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 If it does not
1f79c 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 2c 20 already exists,
1f79d 63 72 65 61 74 65 20 61 6e 64 20 70 6f 70 75 6c create and popul
1f79e 61 74 65 20 74 68 65 20 6f 6e 2d 64 69 73 6b 20 ate the on-disk
1f79f 66 69 6c 65 20 0a 2a 2a 20 66 6f 72 20 4a 6f 75 file .** for Jou
1f7a0 72 6e 61 6c 46 69 6c 65 20 70 2e 0a 2a 2f 0a 73 rnalFile p..*/.s
1f7a1 74 61 74 69 63 20 69 6e 74 20 63 72 65 61 74 65 tatic int create
1f7a2 46 69 6c 65 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 File(JournalFile
1f7a3 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d *p){. int rc =
1f7a4 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 SQLITE_OK;. if
1f7a5 28 20 21 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 ( !p->pReal ){.
1f7a6 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 sqlite3_file
1f7a7 2a 70 52 65 61 6c 20 3d 20 28 73 71 6c 69 74 65 *pReal = (sqlite
1f7a8 33 5f 66 69 6c 65 20 2a 29 26 70 5b 31 5d 3b 0a 3_file *)&p[1];.
1f7a9 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1f7aa 4f 73 4f 70 65 6e 28 70 2d 3e 70 56 66 73 2c 20 OsOpen(p->pVfs,
1f7ab 70 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 52 65 p->zJournal, pRe
1f7ac 61 6c 2c 20 70 2d 3e 66 6c 61 67 73 2c 20 30 29 al, p->flags, 0)
1f7ad 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 ;. if( rc==SQ
1f7ae 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1f7af 20 70 2d 3e 70 52 65 61 6c 20 3d 20 70 52 65 61 p->pReal = pRea
1f7b0 6c 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e l;. if( p->
1f7b1 69 53 69 7a 65 3e 30 20 29 7b 0a 20 20 20 20 20 iSize>0 ){.
1f7b2 20 20 20 61 73 73 65 72 74 28 70 2d 3e 69 53 69 assert(p->iSi
1f7b3 7a 65 3c 3d 70 2d 3e 6e 42 75 66 29 3b 0a 20 20 ze<=p->nBuf);.
1f7b4 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1f7b5 65 33 4f 73 57 72 69 74 65 28 70 2d 3e 70 52 65 e3OsWrite(p->pRe
1f7b6 61 6c 2c 20 70 2d 3e 7a 42 75 66 2c 20 70 2d 3e al, p->zBuf, p->
1f7b7 69 53 69 7a 65 2c 20 30 29 3b 0a 20 20 20 20 20 iSize, 0);.
1f7b8 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 }. }. }. r
1f7b9 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
1f7ba 2a 2a 20 43 6c 6f 73 65 20 74 68 65 20 66 69 6c ** Close the fil
1f7bb 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
1f7bc 20 6a 72 6e 6c 43 6c 6f 73 65 28 73 71 6c 69 74 jrnlClose(sqlit
1f7bd 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 29 7b 0a e3_file *pJfd){.
1f7be 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 JournalFile *p
1f7bf 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 = (JournalFile
1f7c0 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d *)pJfd;. if( p-
1f7c1 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 73 71 >pReal ){. sq
1f7c2 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 2d 3e lite3OsClose(p->
1f7c3 70 52 65 61 6c 29 3b 0a 20 20 7d 0a 20 20 73 71 pReal);. }. sq
1f7c4 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 7a 42 lite3_free(p->zB
1f7c5 75 66 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 uf);. return SQ
1f7c6 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
1f7c7 2a 20 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d * Read data from
1f7c8 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 the file..*/.st
1f7c9 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 52 65 61 atic int jrnlRea
1f7ca 64 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c d(. sqlite3_fil
1f7cb 65 20 2a 70 4a 66 64 2c 20 20 20 20 2f 2a 20 54 e *pJfd, /* T
1f7cc 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
1f7cd 66 72 6f 6d 20 77 68 69 63 68 20 74 6f 20 72 65 from which to re
1f7ce 61 64 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 7a 42 ad */. void *zB
1f7cf 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f uf, /
1f7d0 2a 20 50 75 74 20 74 68 65 20 72 65 73 75 6c 74 * Put the result
1f7d1 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 s here */. int
1f7d2 69 41 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 iAmt,
1f7d3 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1f7d4 62 79 74 65 73 20 74 6f 20 72 65 61 64 20 2a 2f bytes to read */
1f7d5 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 . sqlite_int64
1f7d6 69 4f 66 73 74 20 20 20 20 20 2f 2a 20 42 65 67 iOfst /* Beg
1f7d7 69 6e 20 72 65 61 64 69 6e 67 20 61 74 20 74 68 in reading at th
1f7d8 69 73 20 6f 66 66 73 65 74 20 2a 2f 0a 29 7b 0a is offset */.){.
1f7d9 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
1f7da 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46 E_OK;. JournalF
1f7db 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 ile *p = (Journa
1f7dc 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 lFile *)pJfd;.
1f7dd 69 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a if( p->pReal ){.
1f7de 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1f7df 4f 73 52 65 61 64 28 70 2d 3e 70 52 65 61 6c 2c OsRead(p->pReal,
1f7e0 20 7a 42 75 66 2c 20 69 41 6d 74 2c 20 69 4f 66 zBuf, iAmt, iOf
1f7e1 73 74 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 st);. }else if(
1f7e2 20 28 69 41 6d 74 2b 69 4f 66 73 74 29 3e 70 2d (iAmt+iOfst)>p-
1f7e3 3e 69 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 63 >iSize ){. rc
1f7e4 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f = SQLITE_IOERR_
1f7e5 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 7d 65 SHORT_READ;. }e
1f7e6 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 lse{. memcpy(
1f7e7 7a 42 75 66 2c 20 26 70 2d 3e 7a 42 75 66 5b 69 zBuf, &p->zBuf[i
1f7e8 4f 66 73 74 5d 2c 20 69 41 6d 74 29 3b 0a 20 20 Ofst], iAmt);.
1f7e9 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
1f7ea 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 ../*.** Write da
1f7eb 74 61 20 74 6f 20 74 68 65 20 66 69 6c 65 2e 0a ta to the file..
1f7ec 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 */.static int jr
1f7ed 6e 6c 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74 nlWrite(. sqlit
1f7ee 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 e3_file *pJfd,
1f7ef 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c /* The journal
1f7f0 20 66 69 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 file into which
1f7f1 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 63 to write */. c
1f7f2 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 42 75 66 2c onst void *zBuf,
1f7f3 20 20 20 20 20 20 2f 2a 20 54 61 6b 65 20 64 61 /* Take da
1f7f4 74 61 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e ta to be written
1f7f5 20 66 72 6f 6d 20 68 65 72 65 20 2a 2f 0a 20 20 from here */.
1f7f6 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20 int iAmt,
1f7f7 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1f7f8 20 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72 69 of bytes to wri
1f7f9 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 te */. sqlite_i
1f7fa 6e 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f nt64 iOfst /
1f7fb 2a 20 42 65 67 69 6e 20 77 72 69 74 69 6e 67 20 * Begin writing
1f7fc 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 69 at this offset i
1f7fd 6e 74 6f 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a nto the file */.
1f7fe 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
1f7ff 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e LITE_OK;. Journ
1f800 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 alFile *p = (Jou
1f801 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b rnalFile *)pJfd;
1f802 0a 20 20 69 66 28 20 21 70 2d 3e 70 52 65 61 6c . if( !p->pReal
1f803 20 26 26 20 28 69 4f 66 73 74 2b 69 41 6d 74 29 && (iOfst+iAmt)
1f804 3e 70 2d 3e 6e 42 75 66 20 29 7b 0a 20 20 20 20 >p->nBuf ){.
1f805 72 63 20 3d 20 63 72 65 61 74 65 46 69 6c 65 28 rc = createFile(
1f806 70 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 p);. }. if( rc
1f807 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1f808 20 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c 20 if( p->pReal
1f809 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 ){. rc = sq
1f80a 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 2d 3e lite3OsWrite(p->
1f80b 70 52 65 61 6c 2c 20 7a 42 75 66 2c 20 69 41 6d pReal, zBuf, iAm
1f80c 74 2c 20 69 4f 66 73 74 29 3b 0a 20 20 20 20 7d t, iOfst);. }
1f80d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 63 else{. memc
1f80e 70 79 28 26 70 2d 3e 7a 42 75 66 5b 69 4f 66 73 py(&p->zBuf[iOfs
1f80f 74 5d 2c 20 7a 42 75 66 2c 20 69 41 6d 74 29 3b t], zBuf, iAmt);
1f810 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69 53 . if( p->iS
1f811 69 7a 65 3c 28 69 4f 66 73 74 2b 69 41 6d 74 29 ize<(iOfst+iAmt)
1f812 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 ){. p->i
1f813 53 69 7a 65 20 3d 20 28 69 4f 66 73 74 2b 69 41 Size = (iOfst+iA
1f814 6d 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 mt);. }.
1f815 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
1f816 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 rc;.}../*.** Tru
1f817 6e 63 61 74 65 20 74 68 65 20 66 69 6c 65 2e 0a ncate the file..
1f818 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 */.static int jr
1f819 6e 6c 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74 nlTruncate(sqlit
1f81a 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 73 e3_file *pJfd, s
1f81b 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 69 7a 65 qlite_int64 size
1f81c 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
1f81d 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e LITE_OK;. Journ
1f81e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 alFile *p = (Jou
1f81f 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b rnalFile *)pJfd;
1f820 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c 20 . if( p->pReal
1f821 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 ){. rc = sqli
1f822 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 2d te3OsTruncate(p-
1f823 3e 70 52 65 61 6c 2c 20 73 69 7a 65 29 3b 0a 20 >pReal, size);.
1f824 20 7d 65 6c 73 65 20 69 66 28 20 73 69 7a 65 3c }else if( size<
1f825 70 2d 3e 69 53 69 7a 65 20 29 7b 0a 20 20 20 20 p->iSize ){.
1f826 70 2d 3e 69 53 69 7a 65 20 3d 20 73 69 7a 65 3b p->iSize = size;
1f827 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
1f828 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 ;.}../*.** Sync
1f829 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 the file..*/.sta
1f82a 74 69 63 20 69 6e 74 20 6a 72 6e 6c 53 79 6e 63 tic int jrnlSync
1f82b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 (sqlite3_file *p
1f82c 4a 66 64 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b Jfd, int flags){
1f82d 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4a 6f 75 . int rc;. Jou
1f82e 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a rnalFile *p = (J
1f82f 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 ournalFile *)pJf
1f830 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61 d;. if( p->pRea
1f831 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 l ){. rc = sq
1f832 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 2d 3e 70 lite3OsSync(p->p
1f833 52 65 61 6c 2c 20 66 6c 61 67 73 29 3b 0a 20 20 Real, flags);.
1f834 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 }else{. rc =
1f835 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 SQLITE_OK;. }.
1f836 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1f837 2a 0a 2a 2a 20 51 75 65 72 79 20 74 68 65 20 73 *.** Query the s
1f838 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 ize of the file
1f839 69 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 in bytes..*/.sta
1f83a 74 69 63 20 69 6e 74 20 6a 72 6e 6c 46 69 6c 65 tic int jrnlFile
1f83b 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c Size(sqlite3_fil
1f83c 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74 65 5f e *pJfd, sqlite_
1f83d 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 int64 *pSize){.
1f83e 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
1f83f 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 _OK;. JournalFi
1f840 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c le *p = (Journal
1f841 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69 File *)pJfd;. i
1f842 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 f( p->pReal ){.
1f843 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
1f844 73 46 69 6c 65 53 69 7a 65 28 70 2d 3e 70 52 65 sFileSize(p->pRe
1f845 61 6c 2c 20 70 53 69 7a 65 29 3b 0a 20 20 7d 65 al, pSize);. }e
1f846 6c 73 65 7b 0a 20 20 20 20 2a 70 53 69 7a 65 20 lse{. *pSize
1f847 3d 20 28 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 = (sqlite_int64)
1f848 20 70 2d 3e 69 53 69 7a 65 3b 0a 20 20 7d 0a 20 p->iSize;. }.
1f849 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1f84a 2a 0a 2a 2a 20 54 61 62 6c 65 20 6f 66 20 6d 65 *.** Table of me
1f84b 74 68 6f 64 73 20 66 6f 72 20 4a 6f 75 72 6e 61 thods for Journa
1f84c 6c 46 69 6c 65 20 73 71 6c 69 74 65 33 5f 66 69 lFile sqlite3_fi
1f84d 6c 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 le object..*/.st
1f84e 61 74 69 63 20 73 74 72 75 63 74 20 73 71 6c 69 atic struct sqli
1f84f 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 4a te3_io_methods J
1f850 6f 75 72 6e 61 6c 46 69 6c 65 4d 65 74 68 6f 64 ournalFileMethod
1f851 73 20 3d 20 7b 0a 20 20 31 2c 20 20 20 20 20 20 s = {. 1,
1f852 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 /* iVersi
1f853 6f 6e 20 2a 2f 0a 20 20 6a 72 6e 6c 43 6c 6f 73 on */. jrnlClos
1f854 65 2c 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 e, /* xClose
1f855 20 2a 2f 0a 20 20 6a 72 6e 6c 52 65 61 64 2c 20 */. jrnlRead,
1f856 20 20 20 20 20 2f 2a 20 78 52 65 61 64 20 2a 2f /* xRead */
1f857 0a 20 20 6a 72 6e 6c 57 72 69 74 65 2c 20 20 20 . jrnlWrite,
1f858 20 20 2f 2a 20 78 57 72 69 74 65 20 2a 2f 0a 20 /* xWrite */.
1f859 20 6a 72 6e 6c 54 72 75 6e 63 61 74 65 2c 20 20 jrnlTruncate,
1f85a 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 2a 2f 0a /* xTruncate */.
1f85b 20 20 6a 72 6e 6c 53 79 6e 63 2c 20 20 20 20 20 jrnlSync,
1f85c 20 2f 2a 20 78 53 79 6e 63 20 2a 2f 0a 20 20 6a /* xSync */. j
1f85d 72 6e 6c 46 69 6c 65 53 69 7a 65 2c 20 20 2f 2a rnlFileSize, /*
1f85e 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f 0a 20 20 xFileSize */.
1f85f 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 0, /
1f860 2a 20 78 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 * xLock */. 0,
1f861 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
1f862 55 6e 6c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 Unlock */. 0,
1f863 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 /* xC
1f864 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b heckReservedLock
1f865 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 */. 0,
1f866 20 20 20 20 20 2f 2a 20 78 46 69 6c 65 43 6f 6e /* xFileCon
1f867 74 72 6f 6c 20 2a 2f 0a 20 20 30 2c 20 20 20 20 trol */. 0,
1f868 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 65 63 /* xSec
1f869 74 6f 72 53 69 7a 65 20 2a 2f 0a 20 20 30 20 20 torSize */. 0
1f86a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
1f86b 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 DeviceCharacteri
1f86c 73 74 69 63 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 stics */.};../*
1f86d 0a 2a 2a 20 4f 70 65 6e 20 61 20 6a 6f 75 72 6e .** Open a journ
1f86e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 al file..*/.SQLI
1f86f 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1f870 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4f 70 65 qlite3JournalOpe
1f871 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 n(. sqlite3_vfs
1f872 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 *pVfs,
1f873 2f 2a 20 54 68 65 20 56 46 53 20 74 6f 20 75 73 /* The VFS to us
1f874 65 20 66 6f 72 20 61 63 74 75 61 6c 20 66 69 6c e for actual fil
1f875 65 20 49 2f 4f 20 2a 2f 0a 20 20 63 6f 6e 73 74 e I/O */. const
1f876 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 char *zName,
1f877 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
1f878 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
1f879 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 e */. sqlite3_f
1f87a 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20 20 20 ile *pJfd,
1f87b 20 20 2f 2a 20 50 72 65 61 6c 6c 6f 63 61 74 65 /* Preallocate
1f87c 64 2c 20 62 6c 61 6e 6b 20 66 69 6c 65 20 68 61 d, blank file ha
1f87d 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c ndle */. int fl
1f87e 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 ags,
1f87f 20 20 20 20 20 2f 2a 20 4f 70 65 6e 69 6e 67 20 /* Opening
1f880 66 6c 61 67 73 20 2a 2f 0a 20 20 69 6e 74 20 6e flags */. int n
1f881 42 75 66 20 20 20 20 20 20 20 20 20 20 20 20 20 Buf
1f882 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 62 /* Bytes b
1f883 75 66 66 65 72 65 64 20 62 65 66 6f 72 65 20 6f uffered before o
1f884 70 65 6e 69 6e 67 20 74 68 65 20 66 69 6c 65 20 pening the file
1f885 2a 2f 0a 29 7b 0a 20 20 4a 6f 75 72 6e 61 6c 46 */.){. JournalF
1f886 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 ile *p = (Journa
1f887 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 lFile *)pJfd;.
1f888 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 71 6c memset(p, 0, sql
1f889 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 ite3JournalSize(
1f88a 70 56 66 73 29 29 3b 0a 20 20 69 66 28 20 6e 42 pVfs));. if( nB
1f88b 75 66 3e 30 20 29 7b 0a 20 20 20 20 70 2d 3e 7a uf>0 ){. p->z
1f88c 42 75 66 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c Buf = sqlite3Mal
1f88d 6c 6f 63 5a 65 72 6f 28 6e 42 75 66 29 3b 0a 20 locZero(nBuf);.
1f88e 20 20 20 69 66 28 20 21 70 2d 3e 7a 42 75 66 20 if( !p->zBuf
1f88f 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
1f890 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
1f891 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
1f892 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4f return sqlite3O
1f893 73 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4e 61 6d sOpen(pVfs, zNam
1f894 65 2c 20 70 4a 66 64 2c 20 66 6c 61 67 73 2c 20 e, pJfd, flags,
1f895 30 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4d 65 0);. }. p->pMe
1f896 74 68 6f 64 20 3d 20 26 4a 6f 75 72 6e 61 6c 46 thod = &JournalF
1f897 69 6c 65 4d 65 74 68 6f 64 73 3b 0a 20 20 70 2d ileMethods;. p-
1f898 3e 6e 42 75 66 20 3d 20 6e 42 75 66 3b 0a 20 20 >nBuf = nBuf;.
1f899 70 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61 67 73 p->flags = flags
1f89a 3b 0a 20 20 70 2d 3e 7a 4a 6f 75 72 6e 61 6c 20 ;. p->zJournal
1f89b 3d 20 7a 4e 61 6d 65 3b 0a 20 20 70 2d 3e 70 56 = zName;. p->pV
1f89c 66 73 20 3d 20 70 56 66 73 3b 0a 20 20 72 65 74 fs = pVfs;. ret
1f89d 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1f89e 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 61 ../*.** If the a
1f89f 72 67 75 6d 65 6e 74 20 70 20 70 6f 69 6e 74 73 rgument p points
1f8a0 20 74 6f 20 61 20 4a 6f 75 72 6e 61 6c 46 69 6c to a JournalFil
1f8a1 65 20 73 74 72 75 63 74 75 72 65 2c 20 61 6e 64 e structure, and
1f8a2 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 0a the underlying.
1f8a3 2a 2a 20 66 69 6c 65 20 68 61 73 20 6e 6f 74 20 ** file has not
1f8a4 79 65 74 20 62 65 65 6e 20 63 72 65 61 74 65 64 yet been created
1f8a5 2c 20 63 72 65 61 74 65 20 69 74 20 6e 6f 77 2e , create it now.
1f8a6 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1f8a7 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f TE int sqlite3Jo
1f8a8 75 72 6e 61 6c 43 72 65 61 74 65 28 73 71 6c 69 urnalCreate(sqli
1f8a9 74 65 33 5f 66 69 6c 65 20 2a 70 29 7b 0a 20 20 te3_file *p){.
1f8aa 69 66 28 20 70 2d 3e 70 4d 65 74 68 6f 64 73 21 if( p->pMethods!
1f8ab 3d 26 4a 6f 75 72 6e 61 6c 46 69 6c 65 4d 65 74 =&JournalFileMet
1f8ac 68 6f 64 73 20 29 7b 0a 20 20 20 20 72 65 74 75 hods ){. retu
1f8ad 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
1f8ae 7d 0a 20 20 72 65 74 75 72 6e 20 63 72 65 61 74 }. return creat
1f8af 65 46 69 6c 65 28 28 4a 6f 75 72 6e 61 6c 46 69 eFile((JournalFi
1f8b0 6c 65 20 2a 29 70 29 3b 0a 7d 0a 0a 2f 2a 20 0a le *)p);.}../* .
1f8b1 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
1f8b2 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 mber of bytes re
1f8b3 71 75 69 72 65 64 20 74 6f 20 73 74 6f 72 65 20 quired to store
1f8b4 61 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 74 68 a JournalFile th
1f8b5 61 74 20 75 73 65 73 20 76 66 73 0a 2a 2a 20 70 at uses vfs.** p
1f8b6 56 66 73 20 74 6f 20 63 72 65 61 74 65 20 74 68 Vfs to create th
1f8b7 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 6e 2d e underlying on-
1f8b8 64 69 73 6b 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 disk files..*/.S
1f8b9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1f8ba 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c t sqlite3Journal
1f8bb 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 76 66 73 Size(sqlite3_vfs
1f8bc 20 2a 70 56 66 73 29 7b 0a 20 20 72 65 74 75 72 *pVfs){. retur
1f8bd 6e 20 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c n (pVfs->szOsFil
1f8be 65 2b 73 69 7a 65 6f 66 28 4a 6f 75 72 6e 61 6c e+sizeof(Journal
1f8bf 46 69 6c 65 29 29 3b 0a 7d 0a 23 65 6e 64 69 66 File));.}.#endif
1f8c0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
1f8c1 2a 20 45 6e 64 20 6f 66 20 6a 6f 75 72 6e 61 6c * End of journal
1f8c2 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
1f8c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f8c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f8c5 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
1f8c6 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d * Begin file mem
1f8c7 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a journal.c ******
1f8c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f8c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f8ca 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4f 63 74 /./*.** 2008 Oct
1f8cb 6f 62 65 72 20 37 0a 2a 2a 0a 2a 2a 20 54 68 65 ober 7.**.** The
1f8cc 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
1f8cd 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
1f8ce 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
1f8cf 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
1f8d0 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
1f8d1 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
1f8d2 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
1f8d3 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
1f8d4 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
1f8d5 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
1f8d6 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
1f8d7 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
1f8d8 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
1f8d9 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
1f8da 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
1f8db 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
1f8dc 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
1f8dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f8de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f8df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f8e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f8e1 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 *****.**.** This
1f8e2 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 file contains c
1f8e3 6f 64 65 20 75 73 65 20 74 6f 20 69 6d 70 6c 65 ode use to imple
1f8e4 6d 65 6e 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 ment an in-memor
1f8e5 79 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e y rollback journ
1f8e6 61 6c 2e 0a 2a 2a 20 54 68 65 20 69 6e 2d 6d 65 al..** The in-me
1f8e7 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f mory rollback jo
1f8e8 75 72 6e 61 6c 20 69 73 20 75 73 65 64 20 74 6f urnal is used to
1f8e9 20 6a 6f 75 72 6e 61 6c 20 74 72 61 6e 73 61 63 journal transac
1f8ea 74 69 6f 6e 73 20 66 6f 72 0a 2a 2a 20 22 3a 6d tions for.** ":m
1f8eb 65 6d 6f 72 79 3a 22 20 64 61 74 61 62 61 73 65 emory:" database
1f8ec 73 20 61 6e 64 20 77 68 65 6e 20 74 68 65 20 6a s and when the j
1f8ed 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 4d 45 4d 4f ournal_mode=MEMO
1f8ee 52 59 20 70 72 61 67 6d 61 20 69 73 20 75 73 65 RY pragma is use
1f8ef 64 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 d..**.** @(#) $I
1f8f0 64 3a 20 6d 65 6d 6a 6f 75 72 6e 61 6c 2e 63 2c d: memjournal.c,
1f8f1 76 20 31 2e 31 32 20 32 30 30 39 2f 30 35 2f 30 v 1.12 2009/05/0
1f8f2 34 20 31 31 3a 34 32 3a 33 30 20 64 61 6e 69 65 4 11:42:30 danie
1f8f3 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a lk1977 Exp $.*/.
1f8f4 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65 ./* Forward refe
1f8f5 72 65 6e 63 65 73 20 74 6f 20 69 6e 74 65 72 6e rences to intern
1f8f6 61 6c 20 73 74 72 75 63 74 75 72 65 73 20 2a 2f al structures */
1f8f7 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
1f8f8 4d 65 6d 4a 6f 75 72 6e 61 6c 20 4d 65 6d 4a 6f MemJournal MemJo
1f8f9 75 72 6e 61 6c 3b 0a 74 79 70 65 64 65 66 20 73 urnal;.typedef s
1f8fa 74 72 75 63 74 20 46 69 6c 65 50 6f 69 6e 74 20 truct FilePoint
1f8fb 46 69 6c 65 50 6f 69 6e 74 3b 0a 74 79 70 65 64 FilePoint;.typed
1f8fc 65 66 20 73 74 72 75 63 74 20 46 69 6c 65 43 68 ef struct FileCh
1f8fd 75 6e 6b 20 46 69 6c 65 43 68 75 6e 6b 3b 0a 0a unk FileChunk;..
1f8fe 2f 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 /* Space to hold
1f8ff 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f the rollback jo
1f900 75 72 6e 61 6c 20 69 73 20 61 6c 6c 6f 63 61 74 urnal is allocat
1f901 65 64 20 69 6e 20 69 6e 63 72 65 6d 65 6e 74 73 ed in increments
1f902 20 6f 66 0a 2a 2a 20 74 68 69 73 20 6d 61 6e 79 of.** this many
1f903 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 bytes..**.** Th
1f904 65 20 73 69 7a 65 20 63 68 6f 73 65 6e 20 69 73 e size chosen is
1f905 20 61 20 6c 69 74 74 6c 65 20 6c 65 73 73 20 74 a little less t
1f906 68 61 6e 20 61 20 70 6f 77 65 72 20 6f 66 20 74 han a power of t
1f907 77 6f 2e 20 20 54 68 61 74 20 77 61 79 2c 0a 2a wo. That way,.*
1f908 2a 20 74 68 65 20 46 69 6c 65 43 68 75 6e 6b 20 * the FileChunk
1f909 6f 62 6a 65 63 74 20 77 69 6c 6c 20 68 61 76 65 object will have
1f90a 20 61 20 73 69 7a 65 20 74 68 61 74 20 61 6c 6d a size that alm
1f90b 6f 73 74 20 65 78 61 63 74 6c 79 20 66 69 6c 6c ost exactly fill
1f90c 73 0a 2a 2a 20 61 20 70 6f 77 65 72 2d 6f 66 2d s.** a power-of-
1f90d 74 77 6f 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 two allocation.
1f90e 20 54 68 69 73 20 6d 69 6d 69 6d 69 7a 65 73 20 This mimimizes
1f90f 77 61 73 74 65 64 20 73 70 61 63 65 20 69 6e 20 wasted space in
1f910 70 6f 77 65 72 2d 6f 66 2d 74 77 6f 0a 2a 2a 20 power-of-two.**
1f911 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 memory allocator
1f912 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f s..*/.#define JO
1f913 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20 URNAL_CHUNKSIZE
1f914 28 28 69 6e 74 29 28 31 30 32 34 2d 73 69 7a 65 ((int)(1024-size
1f915 6f 66 28 46 69 6c 65 43 68 75 6e 6b 2a 29 29 29 of(FileChunk*)))
1f916 0a 0a 2f 2a 20 4d 61 63 72 6f 20 74 6f 20 66 69 ../* Macro to fi
1f917 6e 64 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 6f nd the minimum o
1f918 66 20 74 77 6f 20 6e 75 6d 65 72 69 63 20 76 61 f two numeric va
1f919 6c 75 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 lues..*/.#ifndef
1f91a 20 4d 49 4e 0a 23 20 64 65 66 69 6e 65 20 4d 49 MIN.# define MI
1f91b 4e 28 78 2c 79 29 20 28 28 78 29 3c 28 79 29 3f N(x,y) ((x)<(y)?
1f91c 28 78 29 3a 28 79 29 29 0a 23 65 6e 64 69 66 0a (x):(y)).#endif.
1f91d 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c 62 ./*.** The rollb
1f91e 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 69 73 20 63 ack journal is c
1f91f 6f 6d 70 6f 73 65 64 20 6f 66 20 61 20 6c 69 6e omposed of a lin
1f920 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 68 65 73 ked list of thes
1f921 65 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f e structures..*/
1f922 0a 73 74 72 75 63 74 20 46 69 6c 65 43 68 75 6e .struct FileChun
1f923 6b 20 7b 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 k {. FileChunk
1f924 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 *pNext;
1f925 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 63 68 /* Next ch
1f926 75 6e 6b 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e unk in the journ
1f927 61 6c 20 2a 2f 0a 20 20 75 38 20 7a 43 68 75 6e al */. u8 zChun
1f928 6b 5b 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 k[JOURNAL_CHUNKS
1f929 49 5a 45 5d 3b 20 20 20 2f 2a 20 43 6f 6e 74 65 IZE]; /* Conte
1f92a 6e 74 20 6f 66 20 74 68 69 73 20 63 68 75 6e 6b nt of this chunk
1f92b 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e */.};../*.** An
1f92c 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 instance of thi
1f92d 73 20 6f 62 6a 65 63 74 20 73 65 72 76 65 73 20 s object serves
1f92e 61 73 20 61 20 63 75 72 73 6f 72 20 69 6e 74 6f as a cursor into
1f92f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f the rollback jo
1f930 75 72 6e 61 6c 2e 0a 2a 2a 20 54 68 65 20 63 75 urnal..** The cu
1f931 72 73 6f 72 20 63 61 6e 20 62 65 20 65 69 74 68 rsor can be eith
1f932 65 72 20 66 6f 72 20 72 65 61 64 69 6e 67 20 6f er for reading o
1f933 72 20 77 72 69 74 69 6e 67 2e 0a 2a 2f 0a 73 74 r writing..*/.st
1f934 72 75 63 74 20 46 69 6c 65 50 6f 69 6e 74 20 7b ruct FilePoint {
1f935 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 . sqlite3_int64
1f936 20 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 iOffset;
1f937 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 66 72 6f /* Offset fro
1f938 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 m the beginning
1f939 6f 66 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 of the file */.
1f93a 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 43 68 75 FileChunk *pChu
1f93b 6e 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 nk;
1f93c 20 2f 2a 20 53 70 65 63 69 66 69 63 20 63 68 75 /* Specific chu
1f93d 6e 6b 20 69 6e 74 6f 20 77 68 69 63 68 20 63 75 nk into which cu
1f93e 72 73 6f 72 20 70 6f 69 6e 74 73 20 2a 2f 0a 7d rsor points */.}
1f93f 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 75 ;../*.** This su
1f940 62 63 6c 61 73 73 20 69 73 20 61 20 73 75 62 63 bclass is a subc
1f941 6c 61 73 73 20 6f 66 20 73 71 6c 69 74 65 33 5f lass of sqlite3_
1f942 66 69 6c 65 2e 20 20 45 61 63 68 20 6f 70 65 6e file. Each open
1f943 20 6d 65 6d 6f 72 79 2d 6a 6f 75 72 6e 61 6c 0a memory-journal.
1f944 2a 2a 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 ** is an instanc
1f945 65 20 6f 66 20 74 68 69 73 20 63 6c 61 73 73 2e e of this class.
1f946 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d 4a 6f .*/.struct MemJo
1f947 75 72 6e 61 6c 20 7b 0a 20 20 73 71 6c 69 74 65 urnal {. sqlite
1f948 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 3_io_methods *pM
1f949 65 74 68 6f 64 3b 20 20 20 20 2f 2a 20 50 61 72 ethod; /* Par
1f94a 65 6e 74 20 63 6c 61 73 73 2e 20 4d 55 53 54 20 ent class. MUST
1f94b 42 45 20 46 49 52 53 54 20 2a 2f 0a 20 20 46 69 BE FIRST */. Fi
1f94c 6c 65 43 68 75 6e 6b 20 2a 70 46 69 72 73 74 3b leChunk *pFirst;
1f94d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1f94e 20 48 65 61 64 20 6f 66 20 69 6e 2d 6d 65 6d 6f Head of in-memo
1f94f 72 79 20 63 68 75 6e 6b 2d 6c 69 73 74 20 2a 2f ry chunk-list */
1f950 0a 20 20 46 69 6c 65 50 6f 69 6e 74 20 65 6e 64 . FilePoint end
1f951 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 point;
1f952 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
1f953 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 the end of the
1f954 66 69 6c 65 20 2a 2f 0a 20 20 46 69 6c 65 50 6f file */. FilePo
1f955 69 6e 74 20 72 65 61 64 70 6f 69 6e 74 3b 20 20 int readpoint;
1f956 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 /* Poi
1f957 6e 74 65 72 20 74 6f 20 74 68 65 20 65 6e 64 20 nter to the end
1f958 6f 66 20 74 68 65 20 6c 61 73 74 20 78 52 65 61 of the last xRea
1f959 64 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a d() */.};../*.**
1f95a 20 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 Read data from
1f95b 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f the in-memory jo
1f95c 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20 54 68 69 urnal file. Thi
1f95d 73 20 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 s is the impleme
1f95e 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 ntation.** of th
1f95f 65 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 52 e sqlite3_vfs.xR
1f960 65 61 64 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 ead method..*/.s
1f961 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e tatic int memjrn
1f962 6c 52 65 61 64 28 0a 20 20 73 71 6c 69 74 65 33 lRead(. sqlite3
1f963 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20 _file *pJfd,
1f964 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 /* The journal f
1f965 69 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20 74 ile from which t
1f966 6f 20 72 65 61 64 20 2a 2f 0a 20 20 76 6f 69 64 o read */. void
1f967 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 20 20 20 *zBuf,
1f968 20 20 20 2f 2a 20 50 75 74 20 74 68 65 20 72 65 /* Put the re
1f969 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 sults here */.
1f96a 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20 int iAmt,
1f96b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1f96c 20 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 61 of bytes to rea
1f96d 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e d */. sqlite_in
1f96e 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f 2a t64 iOfst /*
1f96f 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20 61 Begin reading a
1f970 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 2a 2f t this offset */
1f971 0a 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c .){. MemJournal
1f972 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 *p = (MemJourna
1f973 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 75 38 20 2a l *)pJfd;. u8 *
1f974 7a 4f 75 74 20 3d 20 7a 42 75 66 3b 0a 20 20 69 zOut = zBuf;. i
1f975 6e 74 20 6e 52 65 61 64 20 3d 20 69 41 6d 74 3b nt nRead = iAmt;
1f976 0a 20 20 69 6e 74 20 69 43 68 75 6e 6b 4f 66 66 . int iChunkOff
1f977 73 65 74 3b 0a 20 20 46 69 6c 65 43 68 75 6e 6b set;. FileChunk
1f978 20 2a 70 43 68 75 6e 6b 3b 0a 0a 20 20 2f 2a 20 *pChunk;.. /*
1f979 53 51 4c 69 74 65 20 6e 65 76 65 72 20 74 72 69 SQLite never tri
1f97a 65 73 20 74 6f 20 72 65 61 64 20 70 61 73 74 20 es to read past
1f97b 74 68 65 20 65 6e 64 20 6f 66 20 61 20 72 6f 6c the end of a rol
1f97c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 66 69 lback journal fi
1f97d 6c 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 le */. assert(
1f97e 69 4f 66 73 74 2b 69 41 6d 74 3c 3d 70 2d 3e 65 iOfst+iAmt<=p->e
1f97f 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 ndpoint.iOffset
1f980 29 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 72 65 61 );.. if( p->rea
1f981 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 21 3d dpoint.iOffset!=
1f982 69 4f 66 73 74 20 7c 7c 20 69 4f 66 73 74 3d 3d iOfst || iOfst==
1f983 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 0 ){. sqlite3
1f984 5f 69 6e 74 36 34 20 69 4f 66 66 20 3d 20 30 3b _int64 iOff = 0;
1f985 0a 20 20 20 20 66 6f 72 28 70 43 68 75 6e 6b 3d . for(pChunk=
1f986 70 2d 3e 70 46 69 72 73 74 3b 20 0a 20 20 20 20 p->pFirst; .
1f987 20 20 20 20 41 4c 57 41 59 53 28 70 43 68 75 6e ALWAYS(pChun
1f988 6b 29 20 26 26 20 28 69 4f 66 66 2b 4a 4f 55 52 k) && (iOff+JOUR
1f989 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 29 3c 3d NAL_CHUNKSIZE)<=
1f98a 69 4f 66 73 74 3b 0a 20 20 20 20 20 20 20 20 70 iOfst;. p
1f98b 43 68 75 6e 6b 3d 70 43 68 75 6e 6b 2d 3e 70 4e Chunk=pChunk->pN
1f98c 65 78 74 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 ext. ){.
1f98d 20 69 4f 66 66 20 2b 3d 20 4a 4f 55 52 4e 41 4c iOff += JOURNAL
1f98e 5f 43 48 55 4e 4b 53 49 5a 45 3b 0a 20 20 20 20 _CHUNKSIZE;.
1f98f 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 }. }else{. p
1f990 43 68 75 6e 6b 20 3d 20 70 2d 3e 72 65 61 64 70 Chunk = p->readp
1f991 6f 69 6e 74 2e 70 43 68 75 6e 6b 3b 0a 20 20 7d oint.pChunk;. }
1f992 0a 0a 20 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 .. iChunkOffset
1f993 20 3d 20 28 69 6e 74 29 28 69 4f 66 73 74 25 4a = (int)(iOfst%J
1f994 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 OURNAL_CHUNKSIZE
1f995 29 3b 0a 20 20 64 6f 20 7b 0a 20 20 20 20 69 6e );. do {. in
1f996 74 20 69 53 70 61 63 65 20 3d 20 4a 4f 55 52 4e t iSpace = JOURN
1f997 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20 2d 20 69 AL_CHUNKSIZE - i
1f998 43 68 75 6e 6b 4f 66 66 73 65 74 3b 0a 20 20 20 ChunkOffset;.
1f999 20 69 6e 74 20 6e 43 6f 70 79 20 3d 20 4d 49 4e int nCopy = MIN
1f99a 28 6e 52 65 61 64 2c 20 28 4a 4f 55 52 4e 41 4c (nRead, (JOURNAL
1f99b 5f 43 48 55 4e 4b 53 49 5a 45 20 2d 20 69 43 68 _CHUNKSIZE - iCh
1f99c 75 6e 6b 4f 66 66 73 65 74 29 29 3b 0a 20 20 20 unkOffset));.
1f99d 20 6d 65 6d 63 70 79 28 7a 4f 75 74 2c 20 26 70 memcpy(zOut, &p
1f99e 43 68 75 6e 6b 2d 3e 7a 43 68 75 6e 6b 5b 69 43 Chunk->zChunk[iC
1f99f 68 75 6e 6b 4f 66 66 73 65 74 5d 2c 20 6e 43 6f hunkOffset], nCo
1f9a0 70 79 29 3b 0a 20 20 20 20 7a 4f 75 74 20 2b 3d py);. zOut +=
1f9a1 20 6e 43 6f 70 79 3b 0a 20 20 20 20 6e 52 65 61 nCopy;. nRea
1f9a2 64 20 2d 3d 20 69 53 70 61 63 65 3b 0a 20 20 20 d -= iSpace;.
1f9a3 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 20 3d 20 iChunkOffset =
1f9a4 30 3b 0a 20 20 7d 20 77 68 69 6c 65 28 20 6e 52 0;. } while( nR
1f9a5 65 61 64 3e 3d 30 20 26 26 20 28 70 43 68 75 6e ead>=0 && (pChun
1f9a6 6b 3d 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 29 k=pChunk->pNext)
1f9a7 21 3d 30 20 26 26 20 6e 52 65 61 64 3e 30 20 29 !=0 && nRead>0 )
1f9a8 3b 0a 20 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74 ;. p->readpoint
1f9a9 2e 69 4f 66 66 73 65 74 20 3d 20 69 4f 66 73 74 .iOffset = iOfst
1f9aa 2b 69 41 6d 74 3b 0a 20 20 70 2d 3e 72 65 61 64 +iAmt;. p->read
1f9ab 70 6f 69 6e 74 2e 70 43 68 75 6e 6b 20 3d 20 70 point.pChunk = p
1f9ac 43 68 75 6e 6b 3b 0a 0a 20 20 72 65 74 75 72 6e Chunk;.. return
1f9ad 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
1f9ae 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20 *.** Write data
1f9af 74 6f 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a to the file..*/.
1f9b0 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72 static int memjr
1f9b1 6e 6c 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74 nlWrite(. sqlit
1f9b2 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 e3_file *pJfd,
1f9b3 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c /* The journal
1f9b4 20 66 69 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 file into which
1f9b5 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 63 to write */. c
1f9b6 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 42 75 66 2c onst void *zBuf,
1f9b7 20 20 20 20 20 20 2f 2a 20 54 61 6b 65 20 64 61 /* Take da
1f9b8 74 61 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e ta to be written
1f9b9 20 66 72 6f 6d 20 68 65 72 65 20 2a 2f 0a 20 20 from here */.
1f9ba 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20 int iAmt,
1f9bb 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1f9bc 20 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72 69 of bytes to wri
1f9bd 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 te */. sqlite_i
1f9be 6e 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f nt64 iOfst /
1f9bf 2a 20 42 65 67 69 6e 20 77 72 69 74 69 6e 67 20 * Begin writing
1f9c0 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 69 at this offset i
1f9c1 6e 74 6f 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a nto the file */.
1f9c2 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 ){. MemJournal
1f9c3 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c *p = (MemJournal
1f9c4 20 2a 29 70 4a 66 64 3b 0a 20 20 69 6e 74 20 6e *)pJfd;. int n
1f9c5 57 72 69 74 65 20 3d 20 69 41 6d 74 3b 0a 20 20 Write = iAmt;.
1f9c6 75 38 20 2a 7a 57 72 69 74 65 20 3d 20 28 75 38 u8 *zWrite = (u8
1f9c7 20 2a 29 7a 42 75 66 3b 0a 0a 20 20 2f 2a 20 41 *)zBuf;.. /* A
1f9c8 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 n in-memory jour
1f9c9 6e 61 6c 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 nal file should
1f9ca 6f 6e 6c 79 20 65 76 65 72 20 62 65 20 61 70 70 only ever be app
1f9cb 65 6e 64 65 64 20 74 6f 2e 20 52 61 6e 64 6f 6d ended to. Random
1f9cc 0a 20 20 2a 2a 20 61 63 63 65 73 73 20 77 72 69 . ** access wri
1f9cd 74 65 73 20 61 72 65 20 6e 6f 74 20 72 65 71 75 tes are not requ
1f9ce 69 72 65 64 20 62 79 20 73 71 6c 69 74 65 2e 0a ired by sqlite..
1f9cf 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 */. assert( i
1f9d0 4f 66 73 74 3d 3d 70 2d 3e 65 6e 64 70 6f 69 6e Ofst==p->endpoin
1f9d1 74 2e 69 4f 66 66 73 65 74 20 29 3b 0a 20 20 55 t.iOffset );. U
1f9d2 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
1f9d3 69 4f 66 73 74 29 3b 0a 0a 20 20 77 68 69 6c 65 iOfst);.. while
1f9d4 28 20 6e 57 72 69 74 65 3e 30 20 29 7b 0a 20 20 ( nWrite>0 ){.
1f9d5 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 43 68 FileChunk *pCh
1f9d6 75 6e 6b 20 3d 20 70 2d 3e 65 6e 64 70 6f 69 6e unk = p->endpoin
1f9d7 74 2e 70 43 68 75 6e 6b 3b 0a 20 20 20 20 69 6e t.pChunk;. in
1f9d8 74 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 20 3d t iChunkOffset =
1f9d9 20 28 69 6e 74 29 28 70 2d 3e 65 6e 64 70 6f 69 (int)(p->endpoi
1f9da 6e 74 2e 69 4f 66 66 73 65 74 25 4a 4f 55 52 4e nt.iOffset%JOURN
1f9db 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 29 3b 0a 20 AL_CHUNKSIZE);.
1f9dc 20 20 20 69 6e 74 20 69 53 70 61 63 65 20 3d 20 int iSpace =
1f9dd 4d 49 4e 28 6e 57 72 69 74 65 2c 20 4a 4f 55 52 MIN(nWrite, JOUR
1f9de 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20 2d 20 NAL_CHUNKSIZE -
1f9df 69 43 68 75 6e 6b 4f 66 66 73 65 74 29 3b 0a 0a iChunkOffset);..
1f9e0 20 20 20 20 69 66 28 20 69 43 68 75 6e 6b 4f 66 if( iChunkOf
1f9e1 66 73 65 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 fset==0 ){.
1f9e2 20 2f 2a 20 4e 65 77 20 63 68 75 6e 6b 20 69 73 /* New chunk is
1f9e3 20 72 65 71 75 69 72 65 64 20 74 6f 20 65 78 74 required to ext
1f9e4 65 6e 64 20 74 68 65 20 66 69 6c 65 2e 20 2a 2f end the file. */
1f9e5 0a 20 20 20 20 20 20 46 69 6c 65 43 68 75 6e 6b . FileChunk
1f9e6 20 2a 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 *pNew = sqlite3
1f9e7 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 46 _malloc(sizeof(F
1f9e8 69 6c 65 43 68 75 6e 6b 29 29 3b 0a 20 20 20 20 ileChunk));.
1f9e9 20 20 69 66 28 20 21 70 4e 65 77 20 29 7b 0a 20 if( !pNew ){.
1f9ea 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 return SQ
1f9eb 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d LITE_IOERR_NOMEM
1f9ec 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1f9ed 70 4e 65 77 2d 3e 70 4e 65 78 74 20 3d 20 30 3b pNew->pNext = 0;
1f9ee 0a 20 20 20 20 20 20 69 66 28 20 70 43 68 75 6e . if( pChun
1f9ef 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 k ){. ass
1f9f0 65 72 74 28 20 70 2d 3e 70 46 69 72 73 74 20 29 ert( p->pFirst )
1f9f1 3b 0a 20 20 20 20 20 20 20 20 70 43 68 75 6e 6b ;. pChunk
1f9f2 2d 3e 70 4e 65 78 74 20 3d 20 70 4e 65 77 3b 0a ->pNext = pNew;.
1f9f3 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1f9f4 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70 2d assert( !p-
1f9f5 3e 70 46 69 72 73 74 20 29 3b 0a 20 20 20 20 20 >pFirst );.
1f9f6 20 20 20 70 2d 3e 70 46 69 72 73 74 20 3d 20 70 p->pFirst = p
1f9f7 4e 65 77 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 New;. }.
1f9f8 20 20 20 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 70 p->endpoint.p
1f9f9 43 68 75 6e 6b 20 3d 20 70 4e 65 77 3b 0a 20 20 Chunk = pNew;.
1f9fa 20 20 7d 0a 0a 20 20 20 20 6d 65 6d 63 70 79 28 }.. memcpy(
1f9fb 26 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 70 43 68 &p->endpoint.pCh
1f9fc 75 6e 6b 2d 3e 7a 43 68 75 6e 6b 5b 69 43 68 75 unk->zChunk[iChu
1f9fd 6e 6b 4f 66 66 73 65 74 5d 2c 20 7a 57 72 69 74 nkOffset], zWrit
1f9fe 65 2c 20 69 53 70 61 63 65 29 3b 0a 20 20 20 20 e, iSpace);.
1f9ff 7a 57 72 69 74 65 20 2b 3d 20 69 53 70 61 63 65 zWrite += iSpace
1fa00 3b 0a 20 20 20 20 6e 57 72 69 74 65 20 2d 3d 20 ;. nWrite -=
1fa01 69 53 70 61 63 65 3b 0a 20 20 20 20 70 2d 3e 65 iSpace;. p->e
1fa02 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 ndpoint.iOffset
1fa03 2b 3d 20 69 53 70 61 63 65 3b 0a 20 20 7d 0a 0a += iSpace;. }..
1fa04 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1fa05 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 OK;.}../*.** Tru
1fa06 6e 63 61 74 65 20 74 68 65 20 66 69 6c 65 2e 0a ncate the file..
1fa07 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 */.static int me
1fa08 6d 6a 72 6e 6c 54 72 75 6e 63 61 74 65 28 73 71 mjrnlTruncate(sq
1fa09 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 lite3_file *pJfd
1fa0a 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 , sqlite_int64 s
1fa0b 69 7a 65 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e ize){. MemJourn
1fa0c 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 al *p = (MemJour
1fa0d 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 46 69 nal *)pJfd;. Fi
1fa0e 6c 65 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b leChunk *pChunk;
1fa0f 0a 20 20 61 73 73 65 72 74 28 73 69 7a 65 3d 3d . assert(size==
1fa10 30 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 0);. UNUSED_PAR
1fa11 41 4d 45 54 45 52 28 73 69 7a 65 29 3b 0a 20 20 AMETER(size);.
1fa12 70 43 68 75 6e 6b 20 3d 20 70 2d 3e 70 46 69 72 pChunk = p->pFir
1fa13 73 74 3b 0a 20 20 77 68 69 6c 65 28 20 70 43 68 st;. while( pCh
1fa14 75 6e 6b 20 29 7b 0a 20 20 20 20 46 69 6c 65 43 unk ){. FileC
1fa15 68 75 6e 6b 20 2a 70 54 6d 70 20 3d 20 70 43 68 hunk *pTmp = pCh
1fa16 75 6e 6b 3b 0a 20 20 20 20 70 43 68 75 6e 6b 20 unk;. pChunk
1fa17 3d 20 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 3b = pChunk->pNext;
1fa18 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 . sqlite3_fre
1fa19 65 28 70 54 6d 70 29 3b 0a 20 20 7d 0a 20 20 73 e(pTmp);. }. s
1fa1a 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c qlite3MemJournal
1fa1b 4f 70 65 6e 28 70 4a 66 64 29 3b 0a 20 20 72 65 Open(pJfd);. re
1fa1c 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1fa1d 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 }../*.** Close t
1fa1e 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 he file..*/.stat
1fa1f 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 43 6c ic int memjrnlCl
1fa20 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ose(sqlite3_file
1fa21 20 2a 70 4a 66 64 29 7b 0a 20 20 6d 65 6d 6a 72 *pJfd){. memjr
1fa22 6e 6c 54 72 75 6e 63 61 74 65 28 70 4a 66 64 2c nlTruncate(pJfd,
1fa23 20 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 0);. return SQ
1fa24 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a LITE_OK;.}.../*.
1fa25 2a 2a 20 53 79 6e 63 20 74 68 65 20 66 69 6c 65 ** Sync the file
1fa26 2e 0a 2a 2a 0a 2a 2a 20 53 79 6e 63 69 6e 67 20 ..**.** Syncing
1fa27 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 an in-memory jou
1fa28 72 6e 61 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e rnal is a no-op.
1fa29 20 20 41 6e 64 2c 20 69 6e 20 66 61 63 74 2c 20 And, in fact,
1fa2a 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 this routine.**
1fa2b 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 20 is never called
1fa2c 69 6e 20 61 20 77 6f 72 6b 69 6e 67 20 69 6d 70 in a working imp
1fa2d 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68 lementation. Th
1fa2e 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f is implementatio
1fa2f 6e 0a 2a 2a 20 65 78 69 73 74 73 20 70 75 72 65 n.** exists pure
1fa30 6c 79 20 61 73 20 61 20 63 6f 6e 74 69 6e 67 65 ly as a continge
1fa31 6e 63 79 2c 20 69 6e 20 63 61 73 65 20 73 6f 6d ncy, in case som
1fa32 65 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 20 69 6e e malfunction in
1fa33 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 70 some other.** p
1fa34 61 72 74 20 6f 66 20 53 51 4c 69 74 65 20 63 61 art of SQLite ca
1fa35 75 73 65 73 20 53 79 6e 63 20 74 6f 20 62 65 20 uses Sync to be
1fa36 63 61 6c 6c 65 64 20 62 79 20 6d 69 73 74 61 6b called by mistak
1fa37 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
1fa38 20 6d 65 6d 6a 72 6e 6c 53 79 6e 63 28 73 71 6c memjrnlSync(sql
1fa39 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 ite3_file *NotUs
1fa3a 65 64 2c 20 69 6e 74 20 4e 6f 74 55 73 65 64 32 ed, int NotUsed2
1fa3b 29 7b 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a 2f ){ /*NO_TEST*/
1fa3c 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
1fa3d 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f TER2(NotUsed, No
1fa3e 74 55 73 65 64 32 29 3b 20 20 20 20 20 20 20 20 tUsed2);
1fa3f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fa40 2f 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 20 20 61 73 /*NO_TEST*/. as
1fa41 73 65 72 74 28 20 30 20 29 3b 20 20 20 20 20 20 sert( 0 );
1fa42 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fa43 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fa44 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f 5f /*NO_
1fa45 54 45 53 54 2a 2f 0a 20 20 72 65 74 75 72 6e 20 TEST*/. return
1fa46 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 SQLITE_OK;
1fa47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fa48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fa49 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a /*NO_TEST*
1fa4a 2f 0a 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 /.}
1fa4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fa4c 20 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 20 20 20 20 20 20 20
1fa4e 20 2f 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 0a 2f 2a /*NO_TEST*/../*
1fa4f 0a 2a 2a 20 51 75 65 72 79 20 74 68 65 20 73 69 .** Query the si
1fa50 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 ze of the file i
1fa51 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 n bytes..*/.stat
1fa52 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 46 69 ic int memjrnlFi
1fa53 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 leSize(sqlite3_f
1fa54 69 6c 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74 ile *pJfd, sqlit
1fa55 65 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 7b e_int64 *pSize){
1fa56 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 . MemJournal *p
1fa57 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a = (MemJournal *
1fa58 29 70 4a 66 64 3b 0a 20 20 2a 70 53 69 7a 65 20 )pJfd;. *pSize
1fa59 3d 20 28 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 = (sqlite_int64)
1fa5a 20 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66 p->endpoint.iOf
1fa5b 66 73 65 74 3b 0a 20 20 72 65 74 75 72 6e 20 53 fset;. return S
1fa5c 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
1fa5d 2a 2a 20 54 61 62 6c 65 20 6f 66 20 6d 65 74 68 ** Table of meth
1fa5e 6f 64 73 20 66 6f 72 20 4d 65 6d 4a 6f 75 72 6e ods for MemJourn
1fa5f 61 6c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 al sqlite3_file
1fa60 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 object..*/.stati
1fa61 63 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 c struct sqlite3
1fa62 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 4d 65 6d 4a _io_methods MemJ
1fa63 6f 75 72 6e 61 6c 4d 65 74 68 6f 64 73 20 3d 20 ournalMethods =
1fa64 7b 0a 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 {. 1,
1fa65 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f /* iVersio
1fa66 6e 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 43 6c n */. memjrnlCl
1fa67 6f 73 65 2c 20 20 20 20 20 2f 2a 20 78 43 6c 6f ose, /* xClo
1fa68 73 65 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 52 se */. memjrnlR
1fa69 65 61 64 2c 20 20 20 20 20 20 2f 2a 20 78 52 65 ead, /* xRe
1fa6a 61 64 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 57 ad */. memjrnlW
1fa6b 72 69 74 65 2c 20 20 20 20 20 2f 2a 20 78 57 72 rite, /* xWr
1fa6c 69 74 65 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c ite */. memjrnl
1fa6d 54 72 75 6e 63 61 74 65 2c 20 20 2f 2a 20 78 54 Truncate, /* xT
1fa6e 72 75 6e 63 61 74 65 20 2a 2f 0a 20 20 6d 65 6d runcate */. mem
1fa6f 6a 72 6e 6c 53 79 6e 63 2c 20 20 20 20 20 20 2f jrnlSync, /
1fa70 2a 20 78 53 79 6e 63 20 2a 2f 0a 20 20 6d 65 6d * xSync */. mem
1fa71 6a 72 6e 6c 46 69 6c 65 53 69 7a 65 2c 20 20 2f jrnlFileSize, /
1fa72 2a 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f 0a 20 * xFileSize */.
1fa73 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
1fa74 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 0a 20 /* xLock */.
1fa75 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
1fa76 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f /* xUnlock */
1fa77 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 . 0,
1fa78 20 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 /* xCheckRe
1fa79 73 65 72 76 65 64 4c 6f 63 6b 20 2a 2f 0a 20 20 servedLock */.
1fa7a 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
1fa7b 20 20 2f 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f /* xFileContro
1fa7c 6c 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 l */. 0,
1fa7d 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 65 63 /* xSec
1fa7e 74 6f 72 53 69 7a 65 20 2a 2f 0a 20 20 30 20 20 torSize */. 0
1fa7f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1fa80 2a 20 78 44 65 76 69 63 65 43 68 61 72 61 63 74 * xDeviceCharact
1fa81 65 72 69 73 74 69 63 73 20 2a 2f 0a 7d 3b 0a 0a eristics */.};..
1fa82 2f 2a 20 0a 2a 2a 20 4f 70 65 6e 20 61 20 6a 6f /* .** Open a jo
1fa83 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53 urnal file..*/.S
1fa84 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1fa85 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 id sqlite3MemJou
1fa86 72 6e 61 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 rnalOpen(sqlite3
1fa87 5f 66 69 6c 65 20 2a 70 4a 66 64 29 7b 0a 20 20 _file *pJfd){.
1fa88 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 MemJournal *p =
1fa89 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a (MemJournal *)pJ
1fa8a 66 64 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 fd;. assert( EI
1fa8b 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 GHT_BYTE_ALIGNME
1fa8c 4e 54 28 70 29 20 29 3b 0a 20 20 6d 65 6d 73 65 NT(p) );. memse
1fa8d 74 28 70 2c 20 30 2c 20 73 71 6c 69 74 65 33 4d t(p, 0, sqlite3M
1fa8e 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 29 29 emJournalSize())
1fa8f 3b 0a 20 20 70 2d 3e 70 4d 65 74 68 6f 64 20 3d ;. p->pMethod =
1fa90 20 26 4d 65 6d 4a 6f 75 72 6e 61 6c 4d 65 74 68 &MemJournalMeth
1fa91 6f 64 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 ods;.}../*.** Re
1fa92 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 turn true if the
1fa93 20 66 69 6c 65 2d 68 61 6e 64 6c 65 20 70 61 73 file-handle pas
1fa94 73 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 sed as an argume
1fa95 6e 74 20 69 73 20 0a 2a 2a 20 61 6e 20 69 6e 2d nt is .** an in-
1fa96 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 0a memory journal .
1fa97 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1fa98 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4d E int sqlite3IsM
1fa99 65 6d 4a 6f 75 72 6e 61 6c 28 73 71 6c 69 74 65 emJournal(sqlite
1fa9a 33 5f 66 69 6c 65 20 2a 70 4a 66 64 29 7b 0a 20 3_file *pJfd){.
1fa9b 20 72 65 74 75 72 6e 20 70 4a 66 64 2d 3e 70 4d return pJfd->pM
1fa9c 65 74 68 6f 64 73 3d 3d 26 4d 65 6d 4a 6f 75 72 ethods==&MemJour
1fa9d 6e 61 6c 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 2f nalMethods;.}../
1fa9e 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 * .** Return the
1fa9f 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
1faa0 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f required to sto
1faa1 72 65 20 61 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 re a MemJournal
1faa2 74 68 61 74 20 75 73 65 73 20 76 66 73 0a 2a 2a that uses vfs.**
1faa3 20 70 56 66 73 20 74 6f 20 63 72 65 61 74 65 20 pVfs to create
1faa4 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f the underlying o
1faa5 6e 2d 64 69 73 6b 20 66 69 6c 65 73 2e 0a 2a 2f n-disk files..*/
1faa6 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1faa7 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f int sqlite3MemJo
1faa8 75 72 6e 61 6c 53 69 7a 65 28 76 6f 69 64 29 7b urnalSize(void){
1faa9 0a 20 20 72 65 74 75 72 6e 20 73 69 7a 65 6f 66 . return sizeof
1faaa 28 4d 65 6d 4a 6f 75 72 6e 61 6c 29 3b 0a 7d 0a (MemJournal);.}.
1faab 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
1faac 20 45 6e 64 20 6f 66 20 6d 65 6d 6a 6f 75 72 6e End of memjourn
1faad 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a al.c ***********
1faae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1faaf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
1fab0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
1fab1 20 42 65 67 69 6e 20 66 69 6c 65 20 77 61 6c 6b Begin file walk
1fab2 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a er.c ***********
1fab3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fab4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
1fab5 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 41 75 67 75 ./*.** 2008 Augu
1fab6 73 74 20 31 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 st 16.**.** The
1fab7 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
1fab8 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
1fab9 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
1faba 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
1fabb 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
1fabc 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
1fabd 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
1fabe 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
1fabf 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
1fac0 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
1fac1 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
1fac2 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
1fac3 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
1fac4 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
1fac5 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
1fac6 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
1fac7 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
1fac8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fac9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1faca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1facb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1facc 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c ****.** This fil
1facd 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75 74 69 e contains routi
1face 6e 65 73 20 75 73 65 64 20 66 6f 72 20 77 61 6c nes used for wal
1facf 6b 69 6e 67 20 74 68 65 20 70 61 72 73 65 72 20 king the parser
1fad0 74 72 65 65 20 66 6f 72 0a 2a 2a 20 61 6e 20 53 tree for.** an S
1fad1 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a QL statement..**
1fad2 0a 2a 2a 20 24 49 64 3a 20 77 61 6c 6b 65 72 2e .** $Id: walker.
1fad3 63 2c 76 20 31 2e 37 20 32 30 30 39 2f 30 36 2f c,v 1.7 2009/06/
1fad4 31 35 20 32 33 3a 31 35 3a 35 39 20 64 72 68 20 15 23:15:59 drh
1fad5 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a Exp $.*/.../*.**
1fad6 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 73 Walk an express
1fad7 69 6f 6e 20 74 72 65 65 2e 20 20 49 6e 76 6f 6b ion tree. Invok
1fad8 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f e the callback o
1fad9 6e 63 65 20 66 6f 72 20 65 61 63 68 20 6e 6f 64 nce for each nod
1fada 65 0a 2a 2a 20 6f 66 20 74 68 65 20 65 78 70 72 e.** of the expr
1fadb 65 73 73 69 6f 6e 2c 20 77 68 69 6c 65 20 64 65 ession, while de
1fadc 63 65 6e 64 69 6e 67 2e 20 20 28 49 6e 20 6f 74 cending. (In ot
1fadd 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 63 her words, the c
1fade 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73 20 69 6e allback.** is in
1fadf 76 6f 6b 65 64 20 62 65 66 6f 72 65 20 76 69 73 voked before vis
1fae0 69 74 69 6e 67 20 63 68 69 6c 64 72 65 6e 2e 29 iting children.)
1fae1 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 .**.** The retur
1fae2 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 65 n value from the
1fae3 20 63 61 6c 6c 62 61 63 6b 20 73 68 6f 75 6c 64 callback should
1fae4 20 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 57 be one of the W
1fae5 52 43 5f 2a 0a 2a 2a 20 63 6f 6e 73 74 61 6e 74 RC_*.** constant
1fae6 73 20 74 6f 20 73 70 65 63 69 66 79 20 68 6f 77 s to specify how
1fae7 20 74 6f 20 70 72 6f 63 65 65 64 20 77 69 74 68 to proceed with
1fae8 20 74 68 65 20 77 61 6c 6b 2e 0a 2a 2a 0a 2a 2a the walk..**.**
1fae9 20 20 20 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 WRC_Continue
1faea 20 20 20 20 20 20 43 6f 6e 74 69 6e 75 65 20 64 Continue d
1faeb 65 73 63 65 6e 64 69 6e 67 20 64 6f 77 6e 20 74 escending down t
1faec 68 65 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 20 he tree..**.**
1faed 20 20 57 52 43 5f 50 72 75 6e 65 20 20 20 20 20 WRC_Prune
1faee 20 20 20 20 44 6f 20 6e 6f 74 20 64 65 73 63 65 Do not desce
1faef 6e 64 20 69 6e 74 6f 20 63 68 69 6c 64 20 6e 6f nd into child no
1faf0 64 65 73 2e 20 20 42 75 74 20 61 6c 6c 6f 77 0a des. But allow.
1faf1 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
1faf2 20 20 20 20 20 20 20 20 74 68 65 20 77 61 6c 6b the walk
1faf3 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 to continue wit
1faf4 68 20 73 69 62 6c 69 6e 67 20 6e 6f 64 65 73 2e h sibling nodes.
1faf5 0a 2a 2a 0a 2a 2a 20 20 20 20 57 52 43 5f 41 62 .**.** WRC_Ab
1faf6 6f 72 74 20 20 20 20 20 20 20 20 20 44 6f 20 6e ort Do n
1faf7 6f 20 6d 6f 72 65 20 63 61 6c 6c 62 61 63 6b 73 o more callbacks
1faf8 2e 20 20 55 6e 77 69 6e 64 20 74 68 65 20 73 74 . Unwind the st
1faf9 61 63 6b 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 ack and.**
1fafa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fafb 72 65 74 75 72 6e 20 74 68 65 20 74 6f 70 2d 6c return the top-l
1fafc 65 76 65 6c 20 77 61 6c 6b 20 63 61 6c 6c 2e 0a evel walk call..
1fafd 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e **.** The return
1fafe 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 69 73 value from this
1faff 20 72 6f 75 74 69 6e 65 20 69 73 20 57 52 43 5f routine is WRC_
1fb00 41 62 6f 72 74 20 74 6f 20 61 62 61 6e 64 6f 6e Abort to abandon
1fb01 20 74 68 65 20 74 72 65 65 20 77 61 6c 6b 0a 2a the tree walk.*
1fb02 2a 20 61 6e 64 20 57 52 43 5f 43 6f 6e 74 69 6e * and WRC_Contin
1fb03 75 65 20 74 6f 20 63 6f 6e 74 69 6e 75 65 2e 0a ue to continue..
1fb04 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1fb05 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c E int sqlite3Wal
1fb06 6b 45 78 70 72 28 57 61 6c 6b 65 72 20 2a 70 57 kExpr(Walker *pW
1fb07 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78 alker, Expr *pEx
1fb08 70 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 pr){. int rc;.
1fb09 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29 20 if( pExpr==0 )
1fb0a 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 return WRC_Conti
1fb0b 6e 75 65 3b 0a 20 20 74 65 73 74 63 61 73 65 28 nue;. testcase(
1fb0c 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 ExprHasProperty
1fb0d 28 70 45 78 70 72 2c 20 45 50 5f 54 6f 6b 65 6e (pExpr, EP_Token
1fb0e 4f 6e 6c 79 29 20 29 3b 0a 20 20 74 65 73 74 63 Only) );. testc
1fb0f 61 73 65 28 20 45 78 70 72 48 61 73 50 72 6f 70 ase( ExprHasProp
1fb10 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 52 erty(pExpr, EP_R
1fb11 65 64 75 63 65 64 29 20 29 3b 0a 20 20 72 63 20 educed) );. rc
1fb12 3d 20 70 57 61 6c 6b 65 72 2d 3e 78 45 78 70 72 = pWalker->xExpr
1fb13 43 61 6c 6c 62 61 63 6b 28 70 57 61 6c 6b 65 72 Callback(pWalker
1fb14 2c 20 70 45 78 70 72 29 3b 0a 20 20 69 66 28 20 , pExpr);. if(
1fb15 72 63 3d 3d 57 52 43 5f 43 6f 6e 74 69 6e 75 65 rc==WRC_Continue
1fb16 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 . &
1fb17 26 20 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f & !ExprHasAnyPro
1fb18 70 65 72 74 79 28 70 45 78 70 72 2c 45 50 5f 54 perty(pExpr,EP_T
1fb19 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 okenOnly) ){.
1fb1a 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b if( sqlite3Walk
1fb1b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 45 Expr(pWalker, pE
1fb1c 78 70 72 2d 3e 70 4c 65 66 74 29 20 29 20 72 65 xpr->pLeft) ) re
1fb1d 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a turn WRC_Abort;.
1fb1e 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 if( sqlite3W
1fb1f 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c alkExpr(pWalker,
1fb20 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29 20 pExpr->pRight)
1fb21 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f ) return WRC_Abo
1fb22 72 74 3b 0a 20 20 20 20 69 66 28 20 45 78 70 72 rt;. if( Expr
1fb23 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 HasProperty(pExp
1fb24 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 r, EP_xIsSelect)
1fb25 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 ){. if( sq
1fb26 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 lite3WalkSelect(
1fb27 70 57 61 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e pWalker, pExpr->
1fb28 78 2e 70 53 65 6c 65 63 74 29 20 29 20 72 65 74 x.pSelect) ) ret
1fb29 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 urn WRC_Abort;.
1fb2a 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1fb2b 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 if( sqlite3WalkE
1fb2c 78 70 72 4c 69 73 74 28 70 57 61 6c 6b 65 72 2c xprList(pWalker,
1fb2d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 29 pExpr->x.pList)
1fb2e 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 ) return WRC_Ab
1fb2f 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 ort;. }. }.
1fb30 20 72 65 74 75 72 6e 20 72 63 20 26 20 57 52 43 return rc & WRC
1fb31 5f 41 62 6f 72 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a _Abort;.}../*.**
1fb32 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 57 61 6c Call sqlite3Wal
1fb33 6b 45 78 70 72 28 29 20 66 6f 72 20 65 76 65 72 kExpr() for ever
1fb34 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 y expression in
1fb35 6c 69 73 74 20 70 20 6f 72 20 75 6e 74 69 6c 0a list p or until.
1fb36 2a 2a 20 61 6e 20 61 62 6f 72 74 20 72 65 71 75 ** an abort requ
1fb37 65 73 74 20 69 73 20 73 65 65 6e 2e 0a 2a 2f 0a est is seen..*/.
1fb38 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1fb39 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 nt sqlite3WalkEx
1fb3a 70 72 4c 69 73 74 28 57 61 6c 6b 65 72 20 2a 70 prList(Walker *p
1fb3b 57 61 6c 6b 65 72 2c 20 45 78 70 72 4c 69 73 74 Walker, ExprList
1fb3c 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 *p){. int i;.
1fb3d 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 struct ExprList
1fb3e 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 _item *pItem;.
1fb3f 69 66 28 20 70 20 29 7b 0a 20 20 20 20 66 6f 72 if( p ){. for
1fb40 28 69 3d 70 2d 3e 6e 45 78 70 72 2c 20 70 49 74 (i=p->nExpr, pIt
1fb41 65 6d 3d 70 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d em=p->a; i>0; i-
1fb42 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 -, pItem++){.
1fb43 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 if( sqlite3Wa
1fb44 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 lkExpr(pWalker,
1fb45 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 20 29 20 pItem->pExpr) )
1fb46 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 return WRC_Abort
1fb47 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
1fb48 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 turn WRC_Continu
1fb49 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b e;.}../*.** Walk
1fb4a 20 61 6c 6c 20 65 78 70 72 65 73 73 69 6f 6e 73 all expressions
1fb4b 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
1fb4c 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
1fb4d 74 20 70 2e 20 20 44 6f 0a 2a 2a 20 6e 6f 74 20 t p. Do.** not
1fb4e 69 6e 76 6f 6b 65 20 74 68 65 20 53 45 4c 45 43 invoke the SELEC
1fb4f 54 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 20 70 2c T callback on p,
1fb50 20 62 75 74 20 64 6f 20 28 6f 66 20 63 6f 75 72 but do (of cour
1fb51 73 65 29 20 69 6e 76 6f 6b 65 0a 2a 2a 20 61 6e se) invoke.** an
1fb52 79 20 65 78 70 72 20 63 61 6c 6c 62 61 63 6b 73 y expr callbacks
1fb53 20 61 6e 64 20 53 45 4c 45 43 54 20 63 61 6c 6c and SELECT call
1fb54 62 61 63 6b 73 20 74 68 61 74 20 63 6f 6d 65 20 backs that come
1fb55 66 72 6f 6d 20 73 75 62 71 75 65 72 69 65 73 2e from subqueries.
1fb56 0a 2a 2a 20 52 65 74 75 72 6e 20 57 52 43 5f 41 .** Return WRC_A
1fb57 62 6f 72 74 20 6f 72 20 57 52 43 5f 43 6f 6e 74 bort or WRC_Cont
1fb58 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f inue..*/.SQLITE_
1fb59 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1fb5a 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 45 78 70 te3WalkSelectExp
1fb5b 72 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 r(Walker *pWalke
1fb5c 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 r, Select *p){.
1fb5d 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b if( sqlite3Walk
1fb5e 45 78 70 72 4c 69 73 74 28 70 57 61 6c 6b 65 72 ExprList(pWalker
1fb5f 2c 20 70 2d 3e 70 45 4c 69 73 74 29 20 29 20 72 , p->pEList) ) r
1fb60 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b eturn WRC_Abort;
1fb61 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 . if( sqlite3Wa
1fb62 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 lkExpr(pWalker,
1fb63 70 2d 3e 70 57 68 65 72 65 29 20 29 20 72 65 74 p->pWhere) ) ret
1fb64 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 urn WRC_Abort;.
1fb65 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b if( sqlite3Walk
1fb66 45 78 70 72 4c 69 73 74 28 70 57 61 6c 6b 65 72 ExprList(pWalker
1fb67 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79 29 20 29 , p->pGroupBy) )
1fb68 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 return WRC_Abor
1fb69 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 t;. if( sqlite3
1fb6a 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 WalkExpr(pWalker
1fb6b 2c 20 70 2d 3e 70 48 61 76 69 6e 67 29 20 29 20 , p->pHaving) )
1fb6c 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 return WRC_Abort
1fb6d 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 ;. if( sqlite3W
1fb6e 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c alkExprList(pWal
1fb6f 6b 65 72 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 ker, p->pOrderBy
1fb70 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 ) ) return WRC_A
1fb71 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 bort;. if( sqli
1fb72 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c te3WalkExpr(pWal
1fb73 6b 65 72 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 20 ker, p->pLimit)
1fb74 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f ) return WRC_Abo
1fb75 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 rt;. if( sqlite
1fb76 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 3WalkExpr(pWalke
1fb77 72 2c 20 70 2d 3e 70 4f 66 66 73 65 74 29 20 29 r, p->pOffset) )
1fb78 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 return WRC_Abor
1fb79 74 3b 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f t;. return WRC_
1fb7a 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a Continue;.}../*.
1fb7b 2a 2a 20 57 61 6c 6b 20 74 68 65 20 70 61 72 73 ** Walk the pars
1fb7c 65 20 74 72 65 65 73 20 61 73 73 6f 63 69 61 74 e trees associat
1fb7d 65 64 20 77 69 74 68 20 61 6c 6c 20 73 75 62 71 ed with all subq
1fb7e 75 65 72 69 65 73 20 69 6e 20 74 68 65 0a 2a 2a ueries in the.**
1fb7f 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 FROM clause of
1fb80 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 SELECT statement
1fb81 20 70 2e 20 20 44 6f 20 6e 6f 74 20 69 6e 76 6f p. Do not invo
1fb82 6b 65 20 74 68 65 20 73 65 6c 65 63 74 0a 2a 2a ke the select.**
1fb83 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 20 70 2c 20 callback on p,
1fb84 62 75 74 20 64 6f 20 69 6e 76 6f 6b 65 20 69 74 but do invoke it
1fb85 20 6f 6e 20 65 61 63 68 20 46 52 4f 4d 20 63 6c on each FROM cl
1fb86 61 75 73 65 20 73 75 62 71 75 65 72 79 0a 2a 2a ause subquery.**
1fb87 20 61 6e 64 20 6f 6e 20 61 6e 79 20 73 75 62 71 and on any subq
1fb88 75 65 72 69 65 73 20 66 75 72 74 68 65 72 20 64 ueries further d
1fb89 6f 77 6e 20 69 6e 20 74 68 65 20 74 72 65 65 2e own in the tree.
1fb8a 20 20 52 65 74 75 72 6e 20 0a 2a 2a 20 57 52 43 Return .** WRC
1fb8b 5f 41 62 6f 72 74 20 6f 72 20 57 52 43 5f 43 6f _Abort or WRC_Co
1fb8c 6e 74 69 6e 75 65 3b 0a 2a 2f 0a 53 51 4c 49 54 ntinue;.*/.SQLIT
1fb8d 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1fb8e 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 46 lite3WalkSelectF
1fb8f 72 6f 6d 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c rom(Walker *pWal
1fb90 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b ker, Select *p){
1fb91 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 . SrcList *pSrc
1fb92 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 ;. int i;. str
1fb93 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d uct SrcList_item
1fb94 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 70 53 72 63 *pItem;.. pSrc
1fb95 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 69 66 = p->pSrc;. if
1fb96 28 20 41 4c 57 41 59 53 28 70 53 72 63 29 20 29 ( ALWAYS(pSrc) )
1fb97 7b 0a 20 20 20 20 66 6f 72 28 69 3d 70 53 72 63 {. for(i=pSrc
1fb98 2d 3e 6e 53 72 63 2c 20 70 49 74 65 6d 3d 70 53 ->nSrc, pItem=pS
1fb99 72 63 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d 2d 2c rc->a; i>0; i--,
1fb9a 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 pItem++){.
1fb9b 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b if( sqlite3Walk
1fb9c 53 65 6c 65 63 74 28 70 57 61 6c 6b 65 72 2c 20 Select(pWalker,
1fb9d 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 29 20 pItem->pSelect)
1fb9e 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 ){. retur
1fb9f 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 n WRC_Abort;.
1fba0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
1fba1 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 return WRC_Cont
1fba2 69 6e 75 65 3b 0a 7d 20 0a 0a 2f 2a 0a 2a 2a 20 inue;.} ../*.**
1fba3 43 61 6c 6c 20 73 71 6c 69 74 65 33 57 61 6c 6b Call sqlite3Walk
1fba4 45 78 70 72 28 29 20 66 6f 72 20 65 76 65 72 79 Expr() for every
1fba5 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 53 expression in S
1fba6 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20 elect statement
1fba7 70 2e 0a 2a 2a 20 49 6e 76 6f 6b 65 20 73 71 6c p..** Invoke sql
1fba8 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 29 ite3WalkSelect()
1fba9 20 66 6f 72 20 73 75 62 71 75 65 72 69 65 73 20 for subqueries
1fbaa 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 in the FROM clau
1fbab 73 65 20 61 6e 64 0a 2a 2a 20 6f 6e 20 74 68 65 se and.** on the
1fbac 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 compound select
1fbad 20 63 68 61 69 6e 2c 20 70 2d 3e 70 50 72 69 6f chain, p->pPrio
1fbae 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 r..**.** Return
1fbaf 57 52 43 5f 43 6f 6e 74 69 6e 75 65 20 75 6e 64 WRC_Continue und
1fbb0 65 72 20 6e 6f 72 6d 61 6c 20 63 6f 6e 64 69 74 er normal condit
1fbb1 69 6f 6e 73 2e 20 20 52 65 74 75 72 6e 20 57 52 ions. Return WR
1fbb2 43 5f 41 62 6f 72 74 20 69 66 0a 2a 2a 20 74 68 C_Abort if.** th
1fbb3 65 72 65 20 69 73 20 61 6e 20 61 62 6f 72 74 20 ere is an abort
1fbb4 72 65 71 75 65 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 request..**.** I
1fbb5 66 20 74 68 65 20 57 61 6c 6b 65 72 20 64 6f 65 f the Walker doe
1fbb6 73 20 6e 6f 74 20 68 61 76 65 20 61 6e 20 78 53 s not have an xS
1fbb7 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 28 29 20 electCallback()
1fbb8 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e then this routin
1fbb9 65 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 20 e.** is a no-op
1fbba 72 65 74 75 72 6e 69 6e 67 20 57 52 43 5f 43 6f returning WRC_Co
1fbbb 6e 74 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 ntinue..*/.SQLIT
1fbbc 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1fbbd 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 lite3WalkSelect(
1fbbe 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c Walker *pWalker,
1fbbf 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 69 Select *p){. i
1fbc0 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 70 3d 3d nt rc;. if( p==
1fbc1 30 20 7c 7c 20 70 57 61 6c 6b 65 72 2d 3e 78 53 0 || pWalker->xS
1fbc2 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 3d 3d 30 electCallback==0
1fbc3 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f ) return WRC_Co
1fbc4 6e 74 69 6e 75 65 3b 0a 20 20 72 63 20 3d 20 57 ntinue;. rc = W
1fbc5 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 77 RC_Continue;. w
1fbc6 68 69 6c 65 28 20 70 20 20 29 7b 0a 20 20 20 20 hile( p ){.
1fbc7 72 63 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 78 53 rc = pWalker->xS
1fbc8 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 28 70 57 electCallback(pW
1fbc9 61 6c 6b 65 72 2c 20 70 29 3b 0a 20 20 20 20 69 alker, p);. i
1fbca 66 28 20 72 63 20 29 20 62 72 65 61 6b 3b 0a 20 f( rc ) break;.
1fbcb 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 if( sqlite3Wa
1fbcc 6c 6b 53 65 6c 65 63 74 45 78 70 72 28 70 57 61 lkSelectExpr(pWa
1fbcd 6c 6b 65 72 2c 20 70 29 20 29 20 72 65 74 75 72 lker, p) ) retur
1fbce 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 n WRC_Abort;.
1fbcf 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b if( sqlite3Walk
1fbd0 53 65 6c 65 63 74 46 72 6f 6d 28 70 57 61 6c 6b SelectFrom(pWalk
1fbd1 65 72 2c 20 70 29 20 29 20 72 65 74 75 72 6e 20 er, p) ) return
1fbd2 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 70 WRC_Abort;. p
1fbd3 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 = p->pPrior;.
1fbd4 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20 26 20 }. return rc &
1fbd5 57 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a 0a 2f 2a WRC_Abort;.}../*
1fbd6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
1fbd7 64 20 6f 66 20 77 61 6c 6b 65 72 2e 63 20 2a 2a d of walker.c **
1fbd8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fbd9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fbda 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
1fbdb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
1fbdc 67 69 6e 20 66 69 6c 65 20 72 65 73 6f 6c 76 65 gin file resolve
1fbdd 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
1fbde 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fbdf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
1fbe0 0a 2a 2a 20 32 30 30 38 20 41 75 67 75 73 74 20 .** 2008 August
1fbe1 31 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 18.**.** The aut
1fbe2 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
1fbe3 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
1fbe4 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
1fbe5 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
1fbe6 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
1fbe7 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
1fbe8 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
1fbe9 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
1fbea 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
1fbeb 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
1fbec 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
1fbed 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
1fbee 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
1fbef 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
1fbf0 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
1fbf1 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
1fbf2 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
1fbf3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fbf4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fbf5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fbf6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fbf7 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c *.**.** This fil
1fbf8 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75 74 69 e contains routi
1fbf9 6e 65 73 20 75 73 65 64 20 66 6f 72 20 77 61 6c nes used for wal
1fbfa 6b 69 6e 67 20 74 68 65 20 70 61 72 73 65 72 20 king the parser
1fbfb 74 72 65 65 20 61 6e 64 0a 2a 2a 20 72 65 73 6f tree and.** reso
1fbfc 6c 76 65 20 61 6c 6c 20 69 64 65 6e 74 69 66 69 lve all identifi
1fbfd 65 72 73 20 62 79 20 61 73 73 6f 63 69 61 74 69 ers by associati
1fbfe 6e 67 20 74 68 65 6d 20 77 69 74 68 20 61 20 70 ng them with a p
1fbff 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 74 61 62 articular.** tab
1fc00 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 2e 0a 2a le and column..*
1fc01 2a 0a 2a 2a 20 24 49 64 3a 20 72 65 73 6f 6c 76 *.** $Id: resolv
1fc02 65 2e 63 2c 76 20 31 2e 33 30 20 32 30 30 39 2f e.c,v 1.30 2009/
1fc03 30 36 2f 31 35 20 32 33 3a 31 35 3a 35 39 20 64 06/15 23:15:59 d
1fc04 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a rh Exp $.*/../*.
1fc05 2a 2a 20 54 75 72 6e 20 74 68 65 20 70 45 78 70 ** Turn the pExp
1fc06 72 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 74 r expression int
1fc07 6f 20 61 6e 20 61 6c 69 61 73 20 66 6f 72 20 74 o an alias for t
1fc08 68 65 20 69 43 6f 6c 2d 74 68 20 63 6f 6c 75 6d he iCol-th colum
1fc09 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65 73 75 n of the.** resu
1fc0a 6c 74 20 73 65 74 20 69 6e 20 70 45 4c 69 73 74 lt set in pEList
1fc0b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 ..**.** If the r
1fc0c 65 73 75 6c 74 20 73 65 74 20 63 6f 6c 75 6d 6e esult set column
1fc0d 20 69 73 20 61 20 73 69 6d 70 6c 65 20 63 6f 6c is a simple col
1fc0e 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 2c 20 74 umn reference, t
1fc0f 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 hen this routine
1fc10 0a 2a 2a 20 6d 61 6b 65 73 20 61 6e 20 65 78 61 .** makes an exa
1fc11 63 74 20 63 6f 70 79 2e 20 20 42 75 74 20 66 6f ct copy. But fo
1fc12 72 20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 r any other kind
1fc13 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 of expression,
1fc14 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 this.** routine
1fc15 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 make a copy of t
1fc16 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 63 6f he result set co
1fc17 6c 75 6d 6e 20 61 73 20 74 68 65 20 61 72 67 75 lumn as the argu
1fc18 6d 65 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20 54 ment to the.** T
1fc19 4b 5f 41 53 20 6f 70 65 72 61 74 6f 72 2e 20 20 K_AS operator.
1fc1a 54 68 65 20 54 4b 5f 41 53 20 6f 70 65 72 61 74 The TK_AS operat
1fc1b 6f 72 20 63 61 75 73 65 73 20 74 68 65 20 65 78 or causes the ex
1fc1c 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 0a 2a pression to be.*
1fc1d 2a 20 65 76 61 6c 75 61 74 65 64 20 6a 75 73 74 * evaluated just
1fc1e 20 6f 6e 63 65 20 61 6e 64 20 74 68 65 6e 20 72 once and then r
1fc1f 65 75 73 65 64 20 66 6f 72 20 65 61 63 68 20 61 eused for each a
1fc20 6c 69 61 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 lias..**.** The
1fc21 72 65 61 73 6f 6e 20 66 6f 72 20 73 75 70 70 72 reason for suppr
1fc22 65 73 73 69 6e 67 20 74 68 65 20 54 4b 5f 41 53 essing the TK_AS
1fc23 20 74 65 72 6d 20 77 68 65 6e 20 74 68 65 20 65 term when the e
1fc24 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 73 xpression is a s
1fc25 69 6d 70 6c 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 imple.** column
1fc26 72 65 66 65 72 65 6e 63 65 20 69 73 20 73 6f 20 reference is so
1fc27 74 68 61 74 20 74 68 65 20 63 6f 6c 75 6d 6e 20 that the column
1fc28 72 65 66 65 72 65 6e 63 65 20 77 69 6c 6c 20 62 reference will b
1fc29 65 20 72 65 63 6f 67 6e 69 7a 65 64 20 61 73 0a e recognized as.
1fc2a 2a 2a 20 75 73 61 62 6c 65 20 62 79 20 69 6e 64 ** usable by ind
1fc2b 69 63 65 73 20 77 69 74 68 69 6e 20 74 68 65 20 ices within the
1fc2c 57 48 45 52 45 20 63 6c 61 75 73 65 20 70 72 6f WHERE clause pro
1fc2d 63 65 73 73 69 6e 67 20 6c 6f 67 69 63 2e 20 0a cessing logic. .
1fc2e 2a 2a 0a 2a 2a 20 48 61 63 6b 3a 20 20 54 68 65 **.** Hack: The
1fc2f 20 54 4b 5f 41 53 20 6f 70 65 72 61 74 6f 72 20 TK_AS operator
1fc30 69 73 20 69 6e 68 69 62 69 74 65 64 20 69 66 20 is inhibited if
1fc31 7a 54 79 70 65 5b 30 5d 3d 3d 27 47 27 2e 20 20 zType[0]=='G'.
1fc32 54 68 69 73 20 6d 65 61 6e 73 0a 2a 2a 20 74 68 This means.** th
1fc33 61 74 20 69 6e 20 61 20 47 52 4f 55 50 20 42 59 at in a GROUP BY
1fc34 20 63 6c 61 75 73 65 2c 20 74 68 65 20 65 78 70 clause, the exp
1fc35 72 65 73 73 69 6f 6e 20 69 73 20 65 76 61 6c 75 ression is evalu
1fc36 61 74 65 64 20 74 77 69 63 65 2e 20 20 48 65 6e ated twice. Hen
1fc37 63 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 ce:.**.** SE
1fc38 4c 45 43 54 20 72 61 6e 64 6f 6d 28 29 25 35 20 LECT random()%5
1fc39 41 53 20 78 2c 20 63 6f 75 6e 74 28 2a 29 20 46 AS x, count(*) F
1fc3a 52 4f 4d 20 74 61 62 20 47 52 4f 55 50 20 42 59 ROM tab GROUP BY
1fc3b 20 78 0a 2a 2a 0a 2a 2a 20 49 73 20 65 71 75 69 x.**.** Is equi
1fc3c 76 61 6c 65 6e 74 20 74 6f 3a 0a 2a 2a 0a 2a 2a valent to:.**.**
1fc3d 20 20 20 20 20 53 45 4c 45 43 54 20 72 61 6e 64 SELECT rand
1fc3e 6f 6d 28 29 25 35 20 41 53 20 78 2c 20 63 6f 75 om()%5 AS x, cou
1fc3f 6e 74 28 2a 29 20 46 52 4f 4d 20 74 61 62 20 47 nt(*) FROM tab G
1fc40 52 4f 55 50 20 42 59 20 72 61 6e 64 6f 6d 28 29 ROUP BY random()
1fc41 25 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 73 %5.**.** The res
1fc42 75 6c 74 20 6f 66 20 72 61 6e 64 6f 6d 28 29 25 ult of random()%
1fc43 35 20 69 6e 20 74 68 65 20 47 52 4f 55 50 20 42 5 in the GROUP B
1fc44 59 20 63 6c 61 75 73 65 20 69 73 20 70 72 6f 62 Y clause is prob
1fc45 61 62 6c 79 20 64 69 66 66 65 72 65 6e 74 0a 2a ably different.*
1fc46 2a 20 66 72 6f 6d 20 74 68 65 20 72 65 73 75 6c * from the resul
1fc47 74 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 2d t in the result-
1fc48 73 65 74 2e 20 20 57 65 20 6d 69 67 68 74 20 66 set. We might f
1fc49 69 78 20 74 68 69 73 20 73 6f 6d 65 64 61 79 2e ix this someday.
1fc4a 20 20 4f 72 0a 2a 2a 20 74 68 65 6e 20 61 67 61 Or.** then aga
1fc4b 69 6e 2c 20 77 65 20 6d 69 67 68 74 20 6e 6f 74 in, we might not
1fc4c 2e 2e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ....*/.static vo
1fc4d 69 64 20 72 65 73 6f 6c 76 65 41 6c 69 61 73 28 id resolveAlias(
1fc4e 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
1fc4f 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 , /* Par
1fc50 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a sing context */.
1fc51 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 ExprList *pELi
1fc52 73 74 2c 20 20 20 20 20 20 2f 2a 20 41 20 72 65 st, /* A re
1fc53 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 69 6e sult set */. in
1fc54 74 20 69 43 6f 6c 2c 20 20 20 20 20 20 20 20 20 t iCol,
1fc55 20 20 20 20 20 2f 2a 20 41 20 63 6f 6c 75 6d 6e /* A column
1fc56 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 in the result s
1fc57 65 74 2e 20 20 30 2e 2e 70 45 4c 69 73 74 2d 3e et. 0..pEList->
1fc58 6e 45 78 70 72 2d 31 20 2a 2f 0a 20 20 45 78 70 nExpr-1 */. Exp
1fc59 72 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20 r *pExpr,
1fc5a 20 20 20 20 2f 2a 20 54 72 61 6e 73 66 6f 72 6d /* Transform
1fc5b 20 74 68 69 73 20 69 6e 74 6f 20 61 6e 20 61 6c this into an al
1fc5c 69 61 73 20 74 6f 20 74 68 65 20 72 65 73 75 6c ias to the resul
1fc5d 74 20 73 65 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 t set */. const
1fc5e 20 63 68 61 72 20 2a 7a 54 79 70 65 20 20 20 20 char *zType
1fc5f 20 20 2f 2a 20 22 47 52 4f 55 50 22 20 6f 72 20 /* "GROUP" or
1fc60 22 4f 52 44 45 52 22 20 6f 72 20 22 22 20 2a 2f "ORDER" or "" */
1fc61 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70 4f 72 69 .){. Expr *pOri
1fc62 67 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 g; /*
1fc63 54 68 65 20 69 43 6f 6c 2d 74 68 20 63 6f 6c 75 The iCol-th colu
1fc64 6d 6e 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 mn of the result
1fc65 20 73 65 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a set */. Expr *
1fc66 70 44 75 70 3b 20 20 20 20 20 20 20 20 20 20 20 pDup;
1fc67 20 2f 2a 20 43 6f 70 79 20 6f 66 20 70 4f 72 69 /* Copy of pOri
1fc68 67 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a g */. sqlite3 *
1fc69 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a db; /*
1fc6a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f The database co
1fc6b 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 61 nnection */.. a
1fc6c 73 73 65 72 74 28 20 69 43 6f 6c 3e 3d 30 20 26 ssert( iCol>=0 &
1fc6d 26 20 69 43 6f 6c 3c 70 45 4c 69 73 74 2d 3e 6e & iCol<pEList->n
1fc6e 45 78 70 72 20 29 3b 0a 20 20 70 4f 72 69 67 20 Expr );. pOrig
1fc6f 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c = pEList->a[iCol
1fc70 5d 2e 70 45 78 70 72 3b 0a 20 20 61 73 73 65 72 ].pExpr;. asser
1fc71 74 28 20 70 4f 72 69 67 21 3d 30 20 29 3b 0a 20 t( pOrig!=0 );.
1fc72 20 61 73 73 65 72 74 28 20 70 4f 72 69 67 2d 3e assert( pOrig->
1fc73 66 6c 61 67 73 20 26 20 45 50 5f 52 65 73 6f 6c flags & EP_Resol
1fc74 76 65 64 20 29 3b 0a 20 20 64 62 20 3d 20 70 50 ved );. db = pP
1fc75 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 arse->db;. if(
1fc76 70 4f 72 69 67 2d 3e 6f 70 21 3d 54 4b 5f 43 4f pOrig->op!=TK_CO
1fc77 4c 55 4d 4e 20 26 26 20 7a 54 79 70 65 5b 30 5d LUMN && zType[0]
1fc78 21 3d 27 47 27 20 29 7b 0a 20 20 20 20 70 44 75 !='G' ){. pDu
1fc79 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 p = sqlite3ExprD
1fc7a 75 70 28 64 62 2c 20 70 4f 72 69 67 2c 20 30 29 up(db, pOrig, 0)
1fc7b 3b 0a 20 20 20 20 70 44 75 70 20 3d 20 73 71 6c ;. pDup = sql
1fc7c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 ite3PExpr(pParse
1fc7d 2c 20 54 4b 5f 41 53 2c 20 70 44 75 70 2c 20 30 , TK_AS, pDup, 0
1fc7e 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 44 , 0);. if( pD
1fc7f 75 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a up==0 ) return;.
1fc80 20 20 20 20 69 66 28 20 70 45 4c 69 73 74 2d 3e if( pEList->
1fc81 61 5b 69 43 6f 6c 5d 2e 69 41 6c 69 61 73 3d 3d a[iCol].iAlias==
1fc82 30 20 29 7b 0a 20 20 20 20 20 20 70 45 4c 69 73 0 ){. pELis
1fc83 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 69 41 6c 69 61 t->a[iCol].iAlia
1fc84 73 20 3d 20 28 75 31 36 29 28 2b 2b 70 50 61 72 s = (u16)(++pPar
1fc85 73 65 2d 3e 6e 41 6c 69 61 73 29 3b 0a 20 20 20 se->nAlias);.
1fc86 20 7d 0a 20 20 20 20 70 44 75 70 2d 3e 69 54 61 }. pDup->iTa
1fc87 62 6c 65 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b ble = pEList->a[
1fc88 69 43 6f 6c 5d 2e 69 41 6c 69 61 73 3b 0a 20 20 iCol].iAlias;.
1fc89 7d 65 6c 73 65 20 69 66 28 20 45 78 70 72 48 61 }else if( ExprHa
1fc8a 73 50 72 6f 70 65 72 74 79 28 70 4f 72 69 67 2c sProperty(pOrig,
1fc8b 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 7c 7c EP_IntValue) ||
1fc8c 20 70 4f 72 69 67 2d 3e 75 2e 7a 54 6f 6b 65 6e pOrig->u.zToken
1fc8d 3d 3d 30 20 29 7b 0a 20 20 20 20 70 44 75 70 20 ==0 ){. pDup
1fc8e 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 = sqlite3ExprDup
1fc8f 28 64 62 2c 20 70 4f 72 69 67 2c 20 30 29 3b 0a (db, pOrig, 0);.
1fc90 20 20 20 20 69 66 28 20 70 44 75 70 3d 3d 30 20 if( pDup==0
1fc91 29 20 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73 ) return;. }els
1fc92 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 6f e{. char *zTo
1fc93 6b 65 6e 20 3d 20 70 4f 72 69 67 2d 3e 75 2e 7a ken = pOrig->u.z
1fc94 54 6f 6b 65 6e 3b 0a 20 20 20 20 61 73 73 65 72 Token;. asser
1fc95 74 28 20 7a 54 6f 6b 65 6e 21 3d 30 20 29 3b 0a t( zToken!=0 );.
1fc96 20 20 20 20 70 4f 72 69 67 2d 3e 75 2e 7a 54 6f pOrig->u.zTo
1fc97 6b 65 6e 20 3d 20 30 3b 0a 20 20 20 20 70 44 75 ken = 0;. pDu
1fc98 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 p = sqlite3ExprD
1fc99 75 70 28 64 62 2c 20 70 4f 72 69 67 2c 20 30 29 up(db, pOrig, 0)
1fc9a 3b 0a 20 20 20 20 70 4f 72 69 67 2d 3e 75 2e 7a ;. pOrig->u.z
1fc9b 54 6f 6b 65 6e 20 3d 20 7a 54 6f 6b 65 6e 3b 0a Token = zToken;.
1fc9c 20 20 20 20 69 66 28 20 70 44 75 70 3d 3d 30 20 if( pDup==0
1fc9d 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 61 73 ) return;. as
1fc9e 73 65 72 74 28 20 28 70 44 75 70 2d 3e 66 6c 61 sert( (pDup->fla
1fc9f 67 73 20 26 20 28 45 50 5f 52 65 64 75 63 65 64 gs & (EP_Reduced
1fca0 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 29 3d |EP_TokenOnly))=
1fca1 3d 30 20 29 3b 0a 20 20 20 20 70 44 75 70 2d 3e =0 );. pDup->
1fca2 66 6c 61 67 73 32 20 7c 3d 20 45 50 32 5f 4d 61 flags2 |= EP2_Ma
1fca3 6c 6c 6f 63 65 64 54 6f 6b 65 6e 3b 0a 20 20 20 llocedToken;.
1fca4 20 70 44 75 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 pDup->u.zToken
1fca5 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 = sqlite3DbStrDu
1fca6 70 28 64 62 2c 20 7a 54 6f 6b 65 6e 29 3b 0a 20 p(db, zToken);.
1fca7 20 7d 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e }. if( pExpr->
1fca8 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f flags & EP_ExpCo
1fca9 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 70 44 75 llate ){. pDu
1fcaa 70 2d 3e 70 43 6f 6c 6c 20 3d 20 70 45 78 70 72 p->pColl = pExpr
1fcab 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 70 44 75 ->pColl;. pDu
1fcac 70 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 45 p->flags |= EP_E
1fcad 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 7d 0a 20 xpCollate;. }.
1fcae 20 73 71 6c 69 74 65 33 45 78 70 72 43 6c 65 61 sqlite3ExprClea
1fcaf 72 28 64 62 2c 20 70 45 78 70 72 29 3b 0a 20 20 r(db, pExpr);.
1fcb0 6d 65 6d 63 70 79 28 70 45 78 70 72 2c 20 70 44 memcpy(pExpr, pD
1fcb1 75 70 2c 20 73 69 7a 65 6f 66 28 2a 70 45 78 70 up, sizeof(*pExp
1fcb2 72 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 r));. sqlite3Db
1fcb3 46 72 65 65 28 64 62 2c 20 70 44 75 70 29 3b 0a Free(db, pDup);.
1fcb4 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 }../*.** Given t
1fcb5 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 63 6f 6c he name of a col
1fcb6 75 6d 6e 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 umn of the form
1fcb7 58 2e 59 2e 5a 20 6f 72 20 59 2e 5a 20 6f 72 20 X.Y.Z or Y.Z or
1fcb8 6a 75 73 74 20 5a 2c 20 6c 6f 6f 6b 20 75 70 0a just Z, look up.
1fcb9 2a 2a 20 74 68 61 74 20 6e 61 6d 65 20 69 6e 20 ** that name in
1fcba 74 68 65 20 73 65 74 20 6f 66 20 73 6f 75 72 63 the set of sourc
1fcbb 65 20 74 61 62 6c 65 73 20 69 6e 20 70 53 72 63 e tables in pSrc
1fcbc 4c 69 73 74 20 61 6e 64 20 6d 61 6b 65 20 74 68 List and make th
1fcbd 65 20 70 45 78 70 72 20 0a 2a 2a 20 65 78 70 72 e pExpr .** expr
1fcbe 65 73 73 69 6f 6e 20 6e 6f 64 65 20 72 65 66 65 ession node refe
1fcbf 72 20 62 61 63 6b 20 74 6f 20 74 68 61 74 20 73 r back to that s
1fcc0 6f 75 72 63 65 20 63 6f 6c 75 6d 6e 2e 20 20 54 ource column. T
1fcc1 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 he following cha
1fcc2 6e 67 65 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65 nges.** are made
1fcc3 20 74 6f 20 70 45 78 70 72 3a 0a 2a 2a 0a 2a 2a to pExpr:.**.**
1fcc4 20 20 20 20 70 45 78 70 72 2d 3e 69 44 62 20 20 pExpr->iDb
1fcc5 20 20 20 20 20 20 20 20 20 53 65 74 20 74 68 65 Set the
1fcc6 20 69 6e 64 65 78 20 69 6e 20 64 62 2d 3e 61 44 index in db->aD
1fcc7 62 5b 5d 20 6f 66 20 74 68 65 20 64 61 74 61 62 b[] of the datab
1fcc8 61 73 65 20 58 0a 2a 2a 20 20 20 20 20 20 20 20 ase X.**
1fcc9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fcca 20 28 65 76 65 6e 20 69 66 20 58 20 69 73 20 69 (even if X is i
1fccb 6d 70 6c 69 65 64 29 2e 0a 2a 2a 20 20 20 20 70 mplied)..** p
1fccc 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 20 20 20 Expr->iTable
1fccd 20 20 20 20 53 65 74 20 74 6f 20 74 68 65 20 63 Set to the c
1fcce 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 ursor number for
1fccf 20 74 68 65 20 74 61 62 6c 65 20 6f 62 74 61 69 the table obtai
1fcd0 6e 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ned.**
1fcd1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 f
1fcd2 72 6f 6d 20 70 53 72 63 4c 69 73 74 2e 0a 2a 2a rom pSrcList..**
1fcd3 20 20 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20 pExpr->pTab
1fcd4 20 20 20 20 20 20 20 20 20 50 6f 69 6e 74 73 20 Points
1fcd5 74 6f 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 to the Table str
1fcd6 75 63 74 75 72 65 20 6f 66 20 58 2e 59 20 28 65 ucture of X.Y (e
1fcd7 76 65 6e 20 69 66 0a 2a 2a 20 20 20 20 20 20 20 ven if.**
1fcd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fcd9 20 20 58 20 61 6e 64 2f 6f 72 20 59 20 61 72 65 X and/or Y are
1fcda 20 69 6d 70 6c 69 65 64 2e 29 0a 2a 2a 20 20 20 implied.).**
1fcdb 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 pExpr->iColumn
1fcdc 20 20 20 20 20 20 53 65 74 20 74 6f 20 74 68 65 Set to the
1fcdd 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 77 column number w
1fcde 69 74 68 69 6e 20 74 68 65 20 74 61 62 6c 65 2e ithin the table.
1fcdf 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 .** pExpr->op
1fce0 20 20 20 20 20 20 20 20 20 20 20 20 53 65 74 20 Set
1fce1 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 2e 0a 2a 2a to TK_COLUMN..**
1fce2 20 20 20 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 pExpr->pLeft
1fce3 20 20 20 20 20 20 20 20 20 41 6e 79 20 65 78 70 Any exp
1fce4 72 65 73 73 69 6f 6e 20 74 68 69 73 20 70 6f 69 ression this poi
1fce5 6e 74 73 20 74 6f 20 69 73 20 64 65 6c 65 74 65 nts to is delete
1fce6 64 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 70 d.** pExpr->p
1fce7 52 69 67 68 74 20 20 20 20 20 20 20 20 41 6e 79 Right Any
1fce8 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 69 73 expression this
1fce9 20 70 6f 69 6e 74 73 20 74 6f 20 69 73 20 64 65 points to is de
1fcea 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 leted..**.** The
1fceb 20 7a 44 62 20 76 61 72 69 61 62 6c 65 20 69 73 zDb variable is
1fcec 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 the name of the
1fced 20 64 61 74 61 62 61 73 65 20 28 74 68 65 20 22 database (the "
1fcee 58 22 29 2e 20 20 54 68 69 73 20 76 61 6c 75 65 X"). This value
1fcef 20 6d 61 79 20 62 65 0a 2a 2a 20 4e 55 4c 4c 20 may be.** NULL
1fcf0 6d 65 61 6e 69 6e 67 20 74 68 61 74 20 6e 61 6d meaning that nam
1fcf1 65 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d e is of the form
1fcf2 20 59 2e 5a 20 6f 72 20 5a 2e 20 20 41 6e 79 20 Y.Z or Z. Any
1fcf3 61 76 61 69 6c 61 62 6c 65 20 64 61 74 61 62 61 available databa
1fcf4 73 65 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 se.** can be use
1fcf5 64 2e 20 20 54 68 65 20 7a 54 61 62 6c 65 20 76 d. The zTable v
1fcf6 61 72 69 61 62 6c 65 20 69 73 20 74 68 65 20 6e ariable is the n
1fcf7 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 ame of the table
1fcf8 20 28 74 68 65 20 22 59 22 29 2e 20 20 54 68 69 (the "Y"). Thi
1fcf9 73 0a 2a 2a 20 76 61 6c 75 65 20 63 61 6e 20 62 s.** value can b
1fcfa 65 20 4e 55 4c 4c 20 69 66 20 7a 44 62 20 69 73 e NULL if zDb is
1fcfb 20 61 6c 73 6f 20 4e 55 4c 4c 2e 20 20 49 66 20 also NULL. If
1fcfc 7a 54 61 62 6c 65 20 69 73 20 4e 55 4c 4c 20 69 zTable is NULL i
1fcfd 74 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74 20 t.** means that
1fcfe 74 68 65 20 66 6f 72 6d 20 6f 66 20 74 68 65 20 the form of the
1fcff 6e 61 6d 65 20 69 73 20 5a 20 61 6e 64 20 74 68 name is Z and th
1fd00 61 74 20 63 6f 6c 75 6d 6e 73 20 66 72 6f 6d 20 at columns from
1fd01 61 6e 79 20 74 61 62 6c 65 0a 2a 2a 20 63 61 6e any table.** can
1fd02 20 62 65 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 be used..**.**
1fd03 49 66 20 74 68 65 20 6e 61 6d 65 20 63 61 6e 6e If the name cann
1fd04 6f 74 20 62 65 20 72 65 73 6f 6c 76 65 64 20 75 ot be resolved u
1fd05 6e 61 6d 62 69 67 75 6f 75 73 6c 79 2c 20 6c 65 nambiguously, le
1fd06 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 ave an error mes
1fd07 73 61 67 65 0a 2a 2a 20 69 6e 20 70 50 61 72 73 sage.** in pPars
1fd08 65 20 61 6e 64 20 72 65 74 75 72 6e 20 57 52 43 e and return WRC
1fd09 5f 41 62 6f 72 74 2e 20 20 52 65 74 75 72 6e 20 _Abort. Return
1fd0a 57 52 43 5f 50 72 75 6e 65 20 6f 6e 20 73 75 63 WRC_Prune on suc
1fd0b 63 65 73 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 cess..*/.static
1fd0c 69 6e 74 20 6c 6f 6f 6b 75 70 4e 61 6d 65 28 0a int lookupName(.
1fd0d 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c Parse *pParse,
1fd0e 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 /* The pa
1fd0f 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f rsing context */
1fd10 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
1fd11 44 62 2c 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 Db, /* Name
1fd12 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
1fd13 63 6f 6e 74 61 69 6e 69 6e 67 20 74 61 62 6c 65 containing table
1fd14 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 , or NULL */. c
1fd15 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 2c onst char *zTab,
1fd16 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 /* Name of t
1fd17 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 able containing
1fd18 63 6f 6c 75 6d 6e 2c 20 6f 72 20 4e 55 4c 4c 20 column, or NULL
1fd19 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
1fd1a 2a 7a 43 6f 6c 2c 20 20 20 20 2f 2a 20 4e 61 6d *zCol, /* Nam
1fd1b 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 2e e of the column.
1fd1c 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 */. NameContex
1fd1d 74 20 2a 70 4e 43 2c 20 20 20 20 2f 2a 20 54 68 t *pNC, /* Th
1fd1e 65 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 20 75 e name context u
1fd1f 73 65 64 20 74 6f 20 72 65 73 6f 6c 76 65 20 74 sed to resolve t
1fd20 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 45 78 70 he name */. Exp
1fd21 72 20 2a 70 45 78 70 72 20 20 20 20 20 20 20 20 r *pExpr
1fd22 20 20 2f 2a 20 4d 61 6b 65 20 74 68 69 73 20 45 /* Make this E
1fd23 58 50 52 20 6e 6f 64 65 20 70 6f 69 6e 74 20 74 XPR node point t
1fd24 6f 20 74 68 65 20 73 65 6c 65 63 74 65 64 20 63 o the selected c
1fd25 6f 6c 75 6d 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e olumn */.){. in
1fd26 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20 t i, j;
1fd27 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 /* Loop count
1fd28 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 ers */. int cnt
1fd29 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
1fd2a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
1fd2b 6d 62 65 72 20 6f 66 20 6d 61 74 63 68 69 6e 67 mber of matching
1fd2c 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 2a 2f column names */
1fd2d 0a 20 20 69 6e 74 20 63 6e 74 54 61 62 20 3d 20 . int cntTab =
1fd2e 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
1fd2f 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1fd30 66 20 6d 61 74 63 68 69 6e 67 20 74 61 62 6c 65 f matching table
1fd31 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 73 71 6c 69 names */. sqli
1fd32 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 te3 *db = pParse
1fd33 2d 3e 64 62 3b 20 20 20 20 20 20 20 20 20 2f 2a ->db; /*
1fd34 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f The database co
1fd35 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 nnection */. st
1fd36 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 ruct SrcList_ite
1fd37 6d 20 2a 70 49 74 65 6d 3b 20 20 20 20 20 20 20 m *pItem;
1fd38 2f 2a 20 55 73 65 20 66 6f 72 20 6c 6f 6f 70 69 /* Use for loopi
1fd39 6e 67 20 6f 76 65 72 20 70 53 72 63 4c 69 73 74 ng over pSrcList
1fd3a 20 69 74 65 6d 73 20 2a 2f 0a 20 20 73 74 72 75 items */. stru
1fd3b 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 ct SrcList_item
1fd3c 2a 70 4d 61 74 63 68 20 3d 20 30 3b 20 20 2f 2a *pMatch = 0; /*
1fd3d 20 54 68 65 20 6d 61 74 63 68 69 6e 67 20 70 53 The matching pS
1fd3e 72 63 4c 69 73 74 20 69 74 65 6d 20 2a 2f 0a 20 rcList item */.
1fd3f 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 54 NameContext *pT
1fd40 6f 70 4e 43 20 3d 20 70 4e 43 3b 20 20 20 20 20 opNC = pNC;
1fd41 20 20 20 2f 2a 20 46 69 72 73 74 20 6e 61 6d 65 /* First name
1fd42 63 6f 6e 74 65 78 74 20 69 6e 20 74 68 65 20 6c context in the l
1fd43 69 73 74 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 ist */. Schema
1fd44 2a 70 53 63 68 65 6d 61 20 3d 20 30 3b 20 20 20 *pSchema = 0;
1fd45 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 63 /* Sc
1fd46 68 65 6d 61 20 6f 66 20 74 68 65 20 65 78 70 72 hema of the expr
1fd47 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 ession */. int
1fd48 69 73 54 72 69 67 67 65 72 20 3d 20 30 3b 0a 0a isTrigger = 0;..
1fd49 20 20 61 73 73 65 72 74 28 20 70 4e 43 20 29 3b assert( pNC );
1fd4a 20 20 20 20 20 2f 2a 20 74 68 65 20 6e 61 6d 65 /* the name
1fd4b 20 63 6f 6e 74 65 78 74 20 63 61 6e 6e 6f 74 20 context cannot
1fd4c 62 65 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 61 73 be NULL. */. as
1fd4d 73 65 72 74 28 20 7a 43 6f 6c 20 29 3b 20 20 20 sert( zCol );
1fd4e 20 2f 2a 20 54 68 65 20 5a 20 69 6e 20 58 2e 59 /* The Z in X.Y
1fd4f 2e 5a 20 63 61 6e 6e 6f 74 20 62 65 20 4e 55 4c .Z cannot be NUL
1fd50 4c 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 7e L */. assert( ~
1fd51 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 ExprHasAnyProper
1fd52 74 79 28 70 45 78 70 72 2c 20 45 50 5f 54 6f 6b ty(pExpr, EP_Tok
1fd53 65 6e 4f 6e 6c 79 7c 45 50 5f 52 65 64 75 63 65 enOnly|EP_Reduce
1fd54 64 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 d) );.. /* Init
1fd55 69 61 6c 69 7a 65 20 74 68 65 20 6e 6f 64 65 20 ialize the node
1fd56 74 6f 20 6e 6f 2d 6d 61 74 63 68 20 2a 2f 0a 20 to no-match */.
1fd57 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d pExpr->iTable =
1fd58 20 2d 31 3b 0a 20 20 70 45 78 70 72 2d 3e 70 54 -1;. pExpr->pT
1fd59 61 62 20 3d 20 30 3b 0a 20 20 45 78 70 72 53 65 ab = 0;. ExprSe
1fd5a 74 49 72 72 65 64 75 63 69 62 6c 65 28 70 45 78 tIrreducible(pEx
1fd5b 70 72 29 3b 0a 0a 20 20 2f 2a 20 53 74 61 72 74 pr);.. /* Start
1fd5c 20 61 74 20 74 68 65 20 69 6e 6e 65 72 2d 6d 6f at the inner-mo
1fd5d 73 74 20 63 6f 6e 74 65 78 74 20 61 6e 64 20 6d st context and m
1fd5e 6f 76 65 20 6f 75 74 77 61 72 64 20 75 6e 74 69 ove outward unti
1fd5f 6c 20 61 20 6d 61 74 63 68 20 69 73 20 66 6f 75 l a match is fou
1fd60 6e 64 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 70 nd */. while( p
1fd61 4e 43 20 26 26 20 63 6e 74 3d 3d 30 20 29 7b 0a NC && cnt==0 ){.
1fd62 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 ExprList *pE
1fd63 4c 69 73 74 3b 0a 20 20 20 20 53 72 63 4c 69 73 List;. SrcLis
1fd64 74 20 2a 70 53 72 63 4c 69 73 74 20 3d 20 70 4e t *pSrcList = pN
1fd65 43 2d 3e 70 53 72 63 4c 69 73 74 3b 0a 0a 20 20 C->pSrcList;..
1fd66 20 20 69 66 28 20 70 53 72 63 4c 69 73 74 20 29 if( pSrcList )
1fd67 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c {. for(i=0,
1fd68 20 70 49 74 65 6d 3d 70 53 72 63 4c 69 73 74 2d pItem=pSrcList-
1fd69 3e 61 3b 20 69 3c 70 53 72 63 4c 69 73 74 2d 3e >a; i<pSrcList->
1fd6a 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d nSrc; i++, pItem
1fd6b 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 54 61 62 ++){. Tab
1fd6c 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 20 20 le *pTab;.
1fd6d 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 20 int iDb;.
1fd6e 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b Column *pCol;
1fd6f 0a 20 20 0a 20 20 20 20 20 20 20 20 70 54 61 62 . . pTab
1fd70 20 3d 20 70 49 74 65 6d 2d 3e 70 54 61 62 3b 0a = pItem->pTab;.
1fd71 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
1fd72 70 54 61 62 21 3d 30 20 26 26 20 70 54 61 62 2d pTab!=0 && pTab-
1fd73 3e 7a 4e 61 6d 65 21 3d 30 20 29 3b 0a 20 20 20 >zName!=0 );.
1fd74 20 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 iDb = sqlit
1fd75 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 e3SchemaToIndex(
1fd76 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d db, pTab->pSchem
1fd77 61 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 a);. asse
1fd78 72 74 28 20 70 54 61 62 2d 3e 6e 43 6f 6c 3e 30 rt( pTab->nCol>0
1fd79 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 );. if(
1fd7a 7a 54 61 62 20 29 7b 0a 20 20 20 20 20 20 20 20 zTab ){.
1fd7b 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 41 6c if( pItem->zAl
1fd7c 69 61 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ias ){.
1fd7d 20 20 20 63 68 61 72 20 2a 7a 54 61 62 4e 61 6d char *zTabNam
1fd7e 65 20 3d 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 e = pItem->zAlia
1fd7f 73 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 s;. i
1fd80 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d f( sqlite3StrICm
1fd81 70 28 7a 54 61 62 4e 61 6d 65 2c 20 7a 54 61 62 p(zTabName, zTab
1fd82 29 21 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b )!=0 ) continue;
1fd83 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 . }else
1fd84 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 68 {. ch
1fd85 61 72 20 2a 7a 54 61 62 4e 61 6d 65 20 3d 20 70 ar *zTabName = p
1fd86 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 Tab->zName;.
1fd87 20 20 20 20 20 20 20 20 69 66 28 20 4e 45 56 45 if( NEVE
1fd88 52 28 7a 54 61 62 4e 61 6d 65 3d 3d 30 29 20 7c R(zTabName==0) |
1fd89 7c 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 | sqlite3StrICmp
1fd8a 28 7a 54 61 62 4e 61 6d 65 2c 20 7a 54 61 62 29 (zTabName, zTab)
1fd8b 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 !=0 ){.
1fd8c 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 continue;.
1fd8d 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
1fd8e 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 44 62 if( zDb
1fd8f 21 3d 30 20 26 26 20 73 71 6c 69 74 65 33 53 74 !=0 && sqlite3St
1fd90 72 49 43 6d 70 28 64 62 2d 3e 61 44 62 5b 69 44 rICmp(db->aDb[iD
1fd91 62 5d 2e 7a 4e 61 6d 65 2c 20 7a 44 62 29 21 3d b].zName, zDb)!=
1fd92 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 0 ){.
1fd93 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 continue;.
1fd94 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
1fd95 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
1fd96 0a 20 20 20 20 20 20 20 20 69 66 28 20 30 3d 3d . if( 0==
1fd97 28 63 6e 74 54 61 62 2b 2b 29 20 29 7b 0a 20 20 (cntTab++) ){.
1fd98 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 pExpr->i
1fd99 54 61 62 6c 65 20 3d 20 70 49 74 65 6d 2d 3e 69 Table = pItem->i
1fd9a 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 20 20 Cursor;.
1fd9b 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 pExpr->pTab =
1fd9c 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 20 20 pTab;.
1fd9d 70 53 63 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e pSchema = pTab->
1fd9e 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 20 20 20 pSchema;.
1fd9f 20 20 20 70 4d 61 74 63 68 20 3d 20 70 49 74 65 pMatch = pIte
1fda0 6d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 m;. }.
1fda1 20 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 43 for(j=0, pC
1fda2 6f 6c 3d 70 54 61 62 2d 3e 61 43 6f 6c 3b 20 6a ol=pTab->aCol; j
1fda3 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b <pTab->nCol; j++
1fda4 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 , pCol++){.
1fda5 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
1fda6 53 74 72 49 43 6d 70 28 70 43 6f 6c 2d 3e 7a 4e StrICmp(pCol->zN
1fda7 61 6d 65 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b ame, zCol)==0 ){
1fda8 0a 20 20 20 20 20 20 20 20 20 20 20 20 49 64 4c . IdL
1fda9 69 73 74 20 2a 70 55 73 69 6e 67 3b 0a 20 20 20 ist *pUsing;.
1fdaa 20 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a cnt++;.
1fdab 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 pExp
1fdac 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 49 74 65 r->iTable = pIte
1fdad 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 m->iCursor;.
1fdae 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 pExpr->p
1fdaf 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20 20 20 Tab = pTab;.
1fdb0 20 20 20 20 20 20 20 20 70 4d 61 74 63 68 20 3d pMatch =
1fdb1 20 70 49 74 65 6d 3b 0a 20 20 20 20 20 20 20 20 pItem;.
1fdb2 20 20 20 20 70 53 63 68 65 6d 61 20 3d 20 70 54 pSchema = pT
1fdb3 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 20 20 ab->pSchema;.
1fdb4 20 20 20 20 20 20 20 20 20 2f 2a 20 53 75 62 73 /* Subs
1fdb5 74 69 74 75 74 65 20 74 68 65 20 72 6f 77 69 64 titute the rowid
1fdb6 20 28 63 6f 6c 75 6d 6e 20 2d 31 29 20 66 6f 72 (column -1) for
1fdb7 20 74 68 65 20 49 4e 54 45 47 45 52 20 50 52 49 the INTEGER PRI
1fdb8 4d 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20 20 20 MARY KEY */.
1fdb9 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 pExpr->i
1fdba 43 6f 6c 75 6d 6e 20 3d 20 6a 3d 3d 70 54 61 62 Column = j==pTab
1fdbb 2d 3e 69 50 4b 65 79 20 3f 20 2d 31 20 3a 20 28 ->iPKey ? -1 : (
1fdbc 69 31 36 29 6a 3b 0a 20 20 20 20 20 20 20 20 20 i16)j;.
1fdbd 20 20 20 69 66 28 20 69 3c 70 53 72 63 4c 69 73 if( i<pSrcLis
1fdbe 74 2d 3e 6e 53 72 63 2d 31 20 29 7b 0a 20 20 20 t->nSrc-1 ){.
1fdbf 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 if( p
1fdc0 49 74 65 6d 5b 31 5d 2e 6a 6f 69 6e 74 79 70 65 Item[1].jointype
1fdc1 20 26 20 4a 54 5f 4e 41 54 55 52 41 4c 20 29 7b & JT_NATURAL ){
1fdc2 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1fdc3 20 2f 2a 20 49 66 20 74 68 69 73 20 6d 61 74 63 /* If this matc
1fdc4 68 20 6f 63 63 75 72 72 65 64 20 69 6e 20 74 68 h occurred in th
1fdc5 65 20 6c 65 66 74 20 74 61 62 6c 65 20 6f 66 20 e left table of
1fdc6 61 20 6e 61 74 75 72 61 6c 20 6a 6f 69 6e 2c 0a a natural join,.
1fdc7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fdc8 2a 2a 20 74 68 65 6e 20 73 6b 69 70 20 74 68 65 ** then skip the
1fdc9 20 72 69 67 68 74 20 74 61 62 6c 65 20 74 6f 20 right table to
1fdca 61 76 6f 69 64 20 61 20 64 75 70 6c 69 63 61 74 avoid a duplicat
1fdcb 65 20 6d 61 74 63 68 20 2a 2f 0a 20 20 20 20 20 e match */.
1fdcc 20 20 20 20 20 20 20 20 20 20 20 70 49 74 65 6d pItem
1fdcd 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ++;.
1fdce 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20 i++;.
1fdcf 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 }else if(
1fdd0 20 28 70 55 73 69 6e 67 20 3d 20 70 49 74 65 6d (pUsing = pItem
1fdd1 5b 31 5d 2e 70 55 73 69 6e 67 29 21 3d 30 20 29 [1].pUsing)!=0 )
1fdd2 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
1fdd3 20 20 2f 2a 20 49 66 20 74 68 69 73 20 6d 61 74 /* If this mat
1fdd4 63 68 20 6f 63 63 75 72 73 20 6f 6e 20 61 20 63 ch occurs on a c
1fdd5 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73 20 69 6e olumn that is in
1fdd6 20 74 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73 the USING claus
1fdd7 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e.
1fdd8 20 20 2a 2a 20 6f 66 20 61 20 6a 6f 69 6e 2c 20 ** of a join,
1fdd9 73 6b 69 70 20 74 68 65 20 73 65 61 72 63 68 20 skip the search
1fdda 6f 66 20 74 68 65 20 72 69 67 68 74 20 74 61 62 of the right tab
1fddb 6c 65 20 6f 66 20 74 68 65 20 6a 6f 69 6e 0a 20 le of the join.
1fddc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a *
1fddd 2a 20 74 6f 20 61 76 6f 69 64 20 61 20 64 75 70 * to avoid a dup
1fdde 6c 69 63 61 74 65 20 6d 61 74 63 68 20 74 68 65 licate match the
1fddf 72 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 re. */.
1fde0 20 20 20 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20 int k;.
1fde1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 6f fo
1fde2 72 28 6b 3d 30 3b 20 6b 3c 70 55 73 69 6e 67 2d r(k=0; k<pUsing-
1fde3 3e 6e 49 64 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20 >nId; k++){.
1fde4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 if
1fde5 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 ( sqlite3StrICmp
1fde6 28 70 55 73 69 6e 67 2d 3e 61 5b 6b 5d 2e 7a 4e (pUsing->a[k].zN
1fde7 61 6d 65 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b ame, zCol)==0 ){
1fde8 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1fde9 20 20 20 20 20 70 49 74 65 6d 2b 2b 3b 0a 20 20 pItem++;.
1fdea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fdeb 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 i++;.
1fdec 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b break
1fded 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
1fdee 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
1fdef 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1fdf0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1fdf1 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
1fdf2 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
1fdf3 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
1fdf4 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 23 69 }. }..#i
1fdf5 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1fdf6 54 5f 54 52 49 47 47 45 52 0a 20 20 20 20 2f 2a T_TRIGGER. /*
1fdf7 20 49 66 20 77 65 20 68 61 76 65 20 6e 6f 74 20 If we have not
1fdf8 61 6c 72 65 61 64 79 20 72 65 73 6f 6c 76 65 64 already resolved
1fdf9 20 74 68 65 20 6e 61 6d 65 2c 20 74 68 65 6e 20 the name, then
1fdfa 6d 61 79 62 65 20 0a 20 20 20 20 2a 2a 20 69 74 maybe . ** it
1fdfb 20 69 73 20 61 20 6e 65 77 2e 2a 20 6f 72 20 6f is a new.* or o
1fdfc 6c 64 2e 2a 20 74 72 69 67 67 65 72 20 61 72 67 ld.* trigger arg
1fdfd 75 6d 65 6e 74 20 72 65 66 65 72 65 6e 63 65 0a ument reference.
1fdfe 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 7a */. if( z
1fdff 44 62 3d 3d 30 20 26 26 20 7a 54 61 62 21 3d 30 Db==0 && zTab!=0
1fe00 20 26 26 20 63 6e 74 3d 3d 30 20 26 26 20 70 50 && cnt==0 && pP
1fe01 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 arse->pTriggerTa
1fe02 62 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e b!=0 ){. in
1fe03 74 20 6f 70 20 3d 20 70 50 61 72 73 65 2d 3e 65 t op = pParse->e
1fe04 54 72 69 67 67 65 72 4f 70 3b 0a 20 20 20 20 20 TriggerOp;.
1fe05 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 30 Table *pTab = 0
1fe06 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1fe07 6f 70 3d 3d 54 4b 5f 44 45 4c 45 54 45 20 7c 7c op==TK_DELETE ||
1fe08 20 6f 70 3d 3d 54 4b 5f 55 50 44 41 54 45 20 7c op==TK_UPDATE |
1fe09 7c 20 6f 70 3d 3d 54 4b 5f 49 4e 53 45 52 54 20 | op==TK_INSERT
1fe0a 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6f 70 21 );. if( op!
1fe0b 3d 54 4b 5f 44 45 4c 45 54 45 20 26 26 20 73 71 =TK_DELETE && sq
1fe0c 6c 69 74 65 33 53 74 72 49 43 6d 70 28 22 6e 65 lite3StrICmp("ne
1fe0d 77 22 2c 7a 54 61 62 29 20 3d 3d 20 30 20 29 7b w",zTab) == 0 ){
1fe0e 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e . pExpr->
1fe0f 69 54 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20 iTable = 1;.
1fe10 20 20 20 20 70 54 61 62 20 3d 20 70 50 61 72 73 pTab = pPars
1fe11 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62 3b 0a e->pTriggerTab;.
1fe12 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
1fe13 6f 70 21 3d 54 4b 5f 49 4e 53 45 52 54 20 26 26 op!=TK_INSERT &&
1fe14 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 sqlite3StrICmp(
1fe15 22 6f 6c 64 22 2c 7a 54 61 62 29 3d 3d 30 20 29 "old",zTab)==0 )
1fe16 7b 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d {. pExpr-
1fe17 3e 69 54 61 62 6c 65 20 3d 20 30 3b 0a 20 20 20 >iTable = 0;.
1fe18 20 20 20 20 20 70 54 61 62 20 3d 20 70 50 61 72 pTab = pPar
1fe19 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62 3b se->pTriggerTab;
1fe1a 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 . }..
1fe1b 69 66 28 20 70 54 61 62 20 29 7b 20 0a 20 20 20 if( pTab ){ .
1fe1c 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 3b 0a 20 int iCol;.
1fe1d 20 20 20 20 20 20 20 70 53 63 68 65 6d 61 20 3d pSchema =
1fe1e 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a pTab->pSchema;.
1fe1f 20 20 20 20 20 20 20 20 63 6e 74 54 61 62 2b 2b cntTab++
1fe20 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 ;. if( sq
1fe21 6c 69 74 65 33 49 73 52 6f 77 69 64 28 7a 43 6f lite3IsRowid(zCo
1fe22 6c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 l) ){.
1fe23 69 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20 20 20 20 iCol = -1;.
1fe24 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1fe25 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 for(iCol=0;
1fe26 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b iCol<pTab->nCol;
1fe27 20 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 iCol++){.
1fe28 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 Column *pC
1fe29 6f 6c 20 3d 20 26 70 54 61 62 2d 3e 61 43 6f 6c ol = &pTab->aCol
1fe2a 5b 69 43 6f 6c 5d 3b 0a 20 20 20 20 20 20 20 20 [iCol];.
1fe2b 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 if( sqlite3S
1fe2c 74 72 49 43 6d 70 28 70 43 6f 6c 2d 3e 7a 4e 61 trICmp(pCol->zNa
1fe2d 6d 65 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0a me, zCol)==0 ){.
1fe2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 if
1fe2f 28 20 69 43 6f 6c 3d 3d 70 54 61 62 2d 3e 69 50 ( iCol==pTab->iP
1fe30 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 Key ){.
1fe31 20 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 2d 31 iCol = -1
1fe32 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
1fe33 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 }.
1fe34 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 break;.
1fe35 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d }. }
1fe36 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1fe37 20 20 20 69 66 28 20 69 43 6f 6c 3c 70 54 61 62 if( iCol<pTab
1fe38 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 ->nCol ){.
1fe39 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 cnt++;.
1fe3a 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 if( iCol<0
1fe3b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 ){. p
1fe3c 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 20 3d Expr->affinity =
1fe3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 SQLITE_AFF_INTE
1fe3e 47 45 52 3b 0a 20 20 20 20 20 20 20 20 20 20 7d GER;. }
1fe3f 65 6c 73 65 20 69 66 28 20 70 45 78 70 72 2d 3e else if( pExpr->
1fe40 69 54 61 62 6c 65 3d 3d 30 20 29 7b 0a 20 20 20 iTable==0 ){.
1fe41 20 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 testcas
1fe42 65 28 20 69 43 6f 6c 3d 3d 33 31 20 29 3b 0a 20 e( iCol==31 );.
1fe43 20 20 20 20 20 20 20 20 20 20 20 74 65 73 74 63 testc
1fe44 61 73 65 28 20 69 43 6f 6c 3d 3d 33 32 20 29 3b ase( iCol==32 );
1fe45 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61 . pPa
1fe46 72 73 65 2d 3e 6f 6c 64 6d 61 73 6b 20 7c 3d 20 rse->oldmask |=
1fe47 28 69 43 6f 6c 3e 3d 33 32 20 3f 20 30 78 66 66 (iCol>=32 ? 0xff
1fe48 66 66 66 66 66 66 20 3a 20 28 28 28 75 33 32 29 ffffff : (((u32)
1fe49 31 29 3c 3c 69 43 6f 6c 29 29 3b 0a 20 20 20 20 1)<<iCol));.
1fe4a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1fe4b 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e pExpr->iColumn
1fe4c 20 3d 20 28 69 31 36 29 69 43 6f 6c 3b 0a 20 20 = (i16)iCol;.
1fe4d 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 pExpr->p
1fe4e 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20 20 20 Tab = pTab;.
1fe4f 20 20 20 20 20 20 69 73 54 72 69 67 67 65 72 20 isTrigger
1fe50 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 = 1;. }.
1fe51 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e }. }.#en
1fe52 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 dif /* !defined(
1fe53 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 SQLITE_OMIT_TRIG
1fe54 47 45 52 29 20 2a 2f 0a 0a 20 20 20 20 2f 2a 0a GER) */.. /*.
1fe55 20 20 20 20 2a 2a 20 50 65 72 68 61 70 73 20 74 ** Perhaps t
1fe56 68 65 20 6e 61 6d 65 20 69 73 20 61 20 72 65 66 he name is a ref
1fe57 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 52 4f erence to the RO
1fe58 57 49 44 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 WID. */. i
1fe59 66 28 20 63 6e 74 3d 3d 30 20 26 26 20 63 6e 74 f( cnt==0 && cnt
1fe5a 54 61 62 3d 3d 31 20 26 26 20 73 71 6c 69 74 65 Tab==1 && sqlite
1fe5b 33 49 73 52 6f 77 69 64 28 7a 43 6f 6c 29 20 29 3IsRowid(zCol) )
1fe5c 7b 0a 20 20 20 20 20 20 63 6e 74 20 3d 20 31 3b {. cnt = 1;
1fe5d 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 . pExpr->iC
1fe5e 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 olumn = -1;.
1fe5f 20 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 pExpr->affinit
1fe60 79 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 y = SQLITE_AFF_I
1fe61 4e 54 45 47 45 52 3b 0a 20 20 20 20 7d 0a 0a 20 NTEGER;. }..
1fe62 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 49 66 20 /*. ** If
1fe63 74 68 65 20 69 6e 70 75 74 20 69 73 20 6f 66 20 the input is of
1fe64 74 68 65 20 66 6f 72 6d 20 5a 20 28 6e 6f 74 20 the form Z (not
1fe65 59 2e 5a 20 6f 72 20 58 2e 59 2e 5a 29 20 74 68 Y.Z or X.Y.Z) th
1fe66 65 6e 20 74 68 65 20 6e 61 6d 65 20 5a 0a 20 20 en the name Z.
1fe67 20 20 2a 2a 20 6d 69 67 68 74 20 72 65 66 65 72 ** might refer
1fe68 20 74 6f 20 61 6e 20 72 65 73 75 6c 74 2d 73 65 to an result-se
1fe69 74 20 61 6c 69 61 73 2e 20 20 54 68 69 73 20 68 t alias. This h
1fe6a 61 70 70 65 6e 73 2c 20 66 6f 72 20 65 78 61 6d appens, for exam
1fe6b 70 6c 65 2c 20 77 68 65 6e 0a 20 20 20 20 2a 2a ple, when. **
1fe6c 20 77 65 20 61 72 65 20 72 65 73 6f 6c 76 69 6e we are resolvin
1fe6d 67 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 57 g names in the W
1fe6e 48 45 52 45 20 63 6c 61 75 73 65 20 6f 66 20 74 HERE clause of t
1fe6f 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6d he following com
1fe70 6d 61 6e 64 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 mand:. **.
1fe71 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 61 ** SELECT a
1fe72 2b 62 20 41 53 20 78 20 46 52 4f 4d 20 74 61 62 +b AS x FROM tab
1fe73 6c 65 20 57 48 45 52 45 20 78 3c 31 30 3b 0a 20 le WHERE x<10;.
1fe74 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 6e 20 **. ** In
1fe75 63 61 73 65 73 20 6c 69 6b 65 20 74 68 69 73 2c cases like this,
1fe76 20 72 65 70 6c 61 63 65 20 70 45 78 70 72 20 77 replace pExpr w
1fe77 69 74 68 20 61 20 63 6f 70 79 20 6f 66 20 74 68 ith a copy of th
1fe78 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 e expression tha
1fe79 74 0a 20 20 20 20 2a 2a 20 66 6f 72 6d 73 20 74 t. ** forms t
1fe7a 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 65 6e he result set en
1fe7b 74 72 79 20 28 22 61 2b 62 22 20 69 6e 20 74 68 try ("a+b" in th
1fe7c 65 20 65 78 61 6d 70 6c 65 29 20 61 6e 64 20 72 e example) and r
1fe7d 65 74 75 72 6e 20 69 6d 6d 65 64 69 61 74 65 6c eturn immediatel
1fe7e 79 2e 0a 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 y.. ** Note t
1fe7f 68 61 74 20 74 68 65 20 65 78 70 72 65 73 73 69 hat the expressi
1fe80 6f 6e 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 on in the result
1fe81 20 73 65 74 20 73 68 6f 75 6c 64 20 68 61 76 65 set should have
1fe82 20 61 6c 72 65 61 64 79 20 62 65 65 6e 0a 20 20 already been.
1fe83 20 20 2a 2a 20 72 65 73 6f 6c 76 65 64 20 62 79 ** resolved by
1fe84 20 74 68 65 20 74 69 6d 65 20 74 68 65 20 57 48 the time the WH
1fe85 45 52 45 20 63 6c 61 75 73 65 20 69 73 20 72 65 ERE clause is re
1fe86 73 6f 6c 76 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 solved.. */.
1fe87 20 20 20 69 66 28 20 63 6e 74 3d 3d 30 20 26 26 if( cnt==0 &&
1fe88 20 28 70 45 4c 69 73 74 20 3d 20 70 4e 43 2d 3e (pEList = pNC->
1fe89 70 45 4c 69 73 74 29 21 3d 30 20 26 26 20 7a 54 pEList)!=0 && zT
1fe8a 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 ab==0 ){. f
1fe8b 6f 72 28 6a 3d 30 3b 20 6a 3c 70 45 4c 69 73 74 or(j=0; j<pEList
1fe8c 2d 3e 6e 45 78 70 72 3b 20 6a 2b 2b 29 7b 0a 20 ->nExpr; j++){.
1fe8d 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 41 73 char *zAs
1fe8e 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 6a 5d 2e = pEList->a[j].
1fe8f 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 69 zName;. i
1fe90 66 28 20 7a 41 73 21 3d 30 20 26 26 20 73 71 6c f( zAs!=0 && sql
1fe91 69 74 65 33 53 74 72 49 43 6d 70 28 7a 41 73 2c ite3StrICmp(zAs,
1fe92 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 zCol)==0 ){.
1fe93 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 4f 72 Expr *pOr
1fe94 69 67 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 ig;. as
1fe95 73 65 72 74 28 20 70 45 78 70 72 2d 3e 70 4c 65 sert( pExpr->pLe
1fe96 66 74 3d 3d 30 20 26 26 20 70 45 78 70 72 2d 3e ft==0 && pExpr->
1fe97 70 52 69 67 68 74 3d 3d 30 20 29 3b 0a 20 20 20 pRight==0 );.
1fe98 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
1fe99 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3d 3d 30 Expr->x.pList==0
1fe9a 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 );. as
1fe9b 73 65 72 74 28 20 70 45 78 70 72 2d 3e 78 2e 70 sert( pExpr->x.p
1fe9c 53 65 6c 65 63 74 3d 3d 30 20 29 3b 0a 20 20 20 Select==0 );.
1fe9d 20 20 20 20 20 20 20 70 4f 72 69 67 20 3d 20 70 pOrig = p
1fe9e 45 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70 45 78 70 EList->a[j].pExp
1fe9f 72 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 r;. if(
1fea0 20 21 70 4e 43 2d 3e 61 6c 6c 6f 77 41 67 67 20 !pNC->allowAgg
1fea1 26 26 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 && ExprHasProper
1fea2 74 79 28 70 4f 72 69 67 2c 20 45 50 5f 41 67 67 ty(pOrig, EP_Agg
1fea3 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 ) ){.
1fea4 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
1fea5 28 70 50 61 72 73 65 2c 20 22 6d 69 73 75 73 65 (pParse, "misuse
1fea6 20 6f 66 20 61 6c 69 61 73 65 64 20 61 67 67 72 of aliased aggr
1fea7 65 67 61 74 65 20 25 73 22 2c 20 7a 41 73 29 3b egate %s", zAs);
1fea8 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74 . ret
1fea9 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 urn WRC_Abort;.
1feaa 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
1feab 20 20 20 20 20 72 65 73 6f 6c 76 65 41 6c 69 61 resolveAlia
1feac 73 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74 s(pParse, pEList
1fead 2c 20 6a 2c 20 70 45 78 70 72 2c 20 22 22 29 3b , j, pExpr, "");
1feae 0a 20 20 20 20 20 20 20 20 20 20 63 6e 74 20 3d . cnt =
1feaf 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4d 1;. pM
1feb0 61 74 63 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 atch = 0;.
1feb1 20 20 20 20 61 73 73 65 72 74 28 20 7a 54 61 62 assert( zTab
1feb2 3d 3d 30 20 26 26 20 7a 44 62 3d 3d 30 20 29 3b ==0 && zDb==0 );
1feb3 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 . goto
1feb4 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 3b 0a lookupname_end;.
1feb5 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1feb6 7d 20 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a } . }.. /*
1feb7 20 41 64 76 61 6e 63 65 20 74 6f 20 74 68 65 20 Advance to the
1feb8 6e 65 78 74 20 6e 61 6d 65 20 63 6f 6e 74 65 78 next name contex
1feb9 74 2e 20 20 54 68 65 20 6c 6f 6f 70 20 77 69 6c t. The loop wil
1feba 6c 20 65 78 69 74 20 77 68 65 6e 20 65 69 74 68 l exit when eith
1febb 65 72 0a 20 20 20 20 2a 2a 20 77 65 20 68 61 76 er. ** we hav
1febc 65 20 61 20 6d 61 74 63 68 20 28 63 6e 74 3e 30 e a match (cnt>0
1febd 29 20 6f 72 20 77 68 65 6e 20 77 65 20 72 75 6e ) or when we run
1febe 20 6f 75 74 20 6f 66 20 6e 61 6d 65 20 63 6f 6e out of name con
1febf 74 65 78 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 texts.. */.
1fec0 20 20 69 66 28 20 63 6e 74 3d 3d 30 20 29 7b 0a if( cnt==0 ){.
1fec1 20 20 20 20 20 20 70 4e 43 20 3d 20 70 4e 43 2d pNC = pNC-
1fec2 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 >pNext;. }.
1fec3 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 49 66 20 }.. /*. ** If
1fec4 58 20 61 6e 64 20 59 20 61 72 65 20 4e 55 4c 4c X and Y are NULL
1fec5 20 28 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 (in other words
1fec6 20 69 66 20 6f 6e 6c 79 20 74 68 65 20 63 6f 6c if only the col
1fec7 75 6d 6e 20 6e 61 6d 65 20 5a 20 69 73 0a 20 20 umn name Z is.
1fec8 2a 2a 20 73 75 70 70 6c 69 65 64 29 20 61 6e 64 ** supplied) and
1fec9 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 5a 20 the value of Z
1feca 69 73 20 65 6e 63 6c 6f 73 65 64 20 69 6e 20 64 is enclosed in d
1fecb 6f 75 62 6c 65 2d 71 75 6f 74 65 73 2c 20 74 68 ouble-quotes, th
1fecc 65 6e 0a 20 20 2a 2a 20 5a 20 69 73 20 61 20 73 en. ** Z is a s
1fecd 74 72 69 6e 67 20 6c 69 74 65 72 61 6c 20 69 66 tring literal if
1fece 20 69 74 20 64 6f 65 73 6e 27 74 20 6d 61 74 63 it doesn't matc
1fecf 68 20 61 6e 79 20 63 6f 6c 75 6d 6e 20 6e 61 6d h any column nam
1fed0 65 73 2e 20 20 49 6e 20 74 68 61 74 0a 20 20 2a es. In that. *
1fed1 2a 20 63 61 73 65 2c 20 77 65 20 6e 65 65 64 20 * case, we need
1fed2 74 6f 20 72 65 74 75 72 6e 20 72 69 67 68 74 20 to return right
1fed3 61 77 61 79 20 61 6e 64 20 6e 6f 74 20 6d 61 6b away and not mak
1fed4 65 20 61 6e 79 20 63 68 61 6e 67 65 73 20 74 6f e any changes to
1fed5 0a 20 20 2a 2a 20 70 45 78 70 72 2e 0a 20 20 2a . ** pExpr.. *
1fed6 2a 0a 20 20 2a 2a 20 42 65 63 61 75 73 65 20 6e *. ** Because n
1fed7 6f 20 72 65 66 65 72 65 6e 63 65 20 77 61 73 20 o reference was
1fed8 6d 61 64 65 20 74 6f 20 6f 75 74 65 72 20 63 6f made to outer co
1fed9 6e 74 65 78 74 73 2c 20 74 68 65 20 70 4e 43 2d ntexts, the pNC-
1feda 3e 6e 52 65 66 0a 20 20 2a 2a 20 66 69 65 6c 64 >nRef. ** field
1fedb 73 20 61 72 65 20 6e 6f 74 20 63 68 61 6e 67 65 s are not change
1fedc 64 20 69 6e 20 61 6e 79 20 63 6f 6e 74 65 78 74 d in any context
1fedd 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6e 74 .. */. if( cnt
1fede 3d 3d 30 20 26 26 20 7a 54 61 62 3d 3d 30 20 26 ==0 && zTab==0 &
1fedf 26 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 & ExprHasPropert
1fee0 79 28 70 45 78 70 72 2c 45 50 5f 44 62 6c 51 75 y(pExpr,EP_DblQu
1fee1 6f 74 65 64 29 20 29 7b 0a 20 20 20 20 70 45 78 oted) ){. pEx
1fee2 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 53 54 52 49 pr->op = TK_STRI
1fee3 4e 47 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 70 NG;. pExpr->p
1fee4 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 Tab = 0;. ret
1fee5 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 urn WRC_Prune;.
1fee6 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 63 6e }.. /*. ** cn
1fee7 74 3d 3d 30 20 6d 65 61 6e 73 20 74 68 65 72 65 t==0 means there
1fee8 20 77 61 73 20 6e 6f 74 20 6d 61 74 63 68 2e 20 was not match.
1fee9 20 63 6e 74 3e 31 20 6d 65 61 6e 73 20 74 68 65 cnt>1 means the
1feea 72 65 20 77 65 72 65 20 74 77 6f 20 6f 72 0a 20 re were two or.
1feeb 20 2a 2a 20 6d 6f 72 65 20 6d 61 74 63 68 65 73 ** more matches
1feec 2e 20 20 45 69 74 68 65 72 20 77 61 79 2c 20 77 . Either way, w
1feed 65 20 68 61 76 65 20 61 6e 20 65 72 72 6f 72 2e e have an error.
1feee 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6e 74 21 . */. if( cnt!
1feef 3d 31 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 =1 ){. const
1fef0 63 68 61 72 20 2a 7a 45 72 72 3b 0a 20 20 20 20 char *zErr;.
1fef1 7a 45 72 72 20 3d 20 63 6e 74 3d 3d 30 20 3f 20 zErr = cnt==0 ?
1fef2 22 6e 6f 20 73 75 63 68 20 63 6f 6c 75 6d 6e 22 "no such column"
1fef3 20 3a 20 22 61 6d 62 69 67 75 6f 75 73 20 63 6f : "ambiguous co
1fef4 6c 75 6d 6e 20 6e 61 6d 65 22 3b 0a 20 20 20 20 lumn name";.
1fef5 69 66 28 20 7a 44 62 20 29 7b 0a 20 20 20 20 20 if( zDb ){.
1fef6 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
1fef7 28 70 50 61 72 73 65 2c 20 22 25 73 3a 20 25 73 (pParse, "%s: %s
1fef8 2e 25 73 2e 25 73 22 2c 20 7a 45 72 72 2c 20 7a .%s.%s", zErr, z
1fef9 44 62 2c 20 7a 54 61 62 2c 20 7a 43 6f 6c 29 3b Db, zTab, zCol);
1fefa 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 7a . }else if( z
1fefb 54 61 62 20 29 7b 0a 20 20 20 20 20 20 73 71 6c Tab ){. sql
1fefc 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
1fefd 72 73 65 2c 20 22 25 73 3a 20 25 73 2e 25 73 22 rse, "%s: %s.%s"
1fefe 2c 20 7a 45 72 72 2c 20 7a 54 61 62 2c 20 7a 43 , zErr, zTab, zC
1feff 6f 6c 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ol);. }else{.
1ff00 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
1ff01 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 orMsg(pParse, "%
1ff02 73 3a 20 25 73 22 2c 20 7a 45 72 72 2c 20 7a 43 s: %s", zErr, zC
1ff03 6f 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 ol);. }. p
1ff04 54 6f 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 TopNC->nErr++;.
1ff05 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 20 63 6f }.. /* If a co
1ff06 6c 75 6d 6e 20 66 72 6f 6d 20 61 20 74 61 62 6c lumn from a tabl
1ff07 65 20 69 6e 20 70 53 72 63 4c 69 73 74 20 69 73 e in pSrcList is
1ff08 20 72 65 66 65 72 65 6e 63 65 64 2c 20 74 68 65 referenced, the
1ff09 6e 20 72 65 63 6f 72 64 0a 20 20 2a 2a 20 74 68 n record. ** th
1ff0a 69 73 20 66 61 63 74 20 69 6e 20 74 68 65 20 70 is fact in the p
1ff0b 53 72 63 4c 69 73 74 2e 61 5b 5d 2e 63 6f 6c 55 SrcList.a[].colU
1ff0c 73 65 64 20 62 69 74 6d 61 73 6b 2e 20 20 43 6f sed bitmask. Co
1ff0d 6c 75 6d 6e 20 30 20 63 61 75 73 65 73 0a 20 20 lumn 0 causes.
1ff0e 2a 2a 20 62 69 74 20 30 20 74 6f 20 62 65 20 73 ** bit 0 to be s
1ff0f 65 74 2e 20 20 43 6f 6c 75 6d 6e 20 31 20 73 65 et. Column 1 se
1ff10 74 73 20 62 69 74 20 31 2e 20 20 41 6e 64 20 73 ts bit 1. And s
1ff11 6f 20 66 6f 72 74 68 2e 20 20 49 66 20 74 68 65 o forth. If the
1ff12 0a 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d . ** column num
1ff13 62 65 72 20 69 73 20 67 72 65 61 74 65 72 20 74 ber is greater t
1ff14 68 61 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f han the number o
1ff15 66 20 62 69 74 73 20 69 6e 20 74 68 65 20 62 69 f bits in the bi
1ff16 74 6d 61 73 6b 0a 20 20 2a 2a 20 74 68 65 6e 20 tmask. ** then
1ff17 73 65 74 20 74 68 65 20 68 69 67 68 2d 6f 72 64 set the high-ord
1ff18 65 72 20 62 69 74 20 6f 66 20 74 68 65 20 62 69 er bit of the bi
1ff19 74 6d 61 73 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 tmask.. */. if
1ff1a 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e ( pExpr->iColumn
1ff1b 3e 3d 30 20 26 26 20 70 4d 61 74 63 68 21 3d 30 >=0 && pMatch!=0
1ff1c 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 ){. int n =
1ff1d 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a pExpr->iColumn;.
1ff1e 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6e 3d testcase( n=
1ff1f 3d 42 4d 53 2d 31 20 29 3b 0a 20 20 20 20 69 66 =BMS-1 );. if
1ff20 28 20 6e 3e 3d 42 4d 53 20 29 7b 0a 20 20 20 20 ( n>=BMS ){.
1ff21 20 20 6e 20 3d 20 42 4d 53 2d 31 3b 0a 20 20 20 n = BMS-1;.
1ff22 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 }. assert( p
1ff23 4d 61 74 63 68 2d 3e 69 43 75 72 73 6f 72 3d 3d Match->iCursor==
1ff24 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 29 3b pExpr->iTable );
1ff25 0a 20 20 20 20 70 4d 61 74 63 68 2d 3e 63 6f 6c . pMatch->col
1ff26 55 73 65 64 20 7c 3d 20 28 28 42 69 74 6d 61 73 Used |= ((Bitmas
1ff27 6b 29 31 29 3c 3c 6e 3b 0a 20 20 7d 0a 0a 20 20 k)1)<<n;. }..
1ff28 2f 2a 20 43 6c 65 61 6e 20 75 70 20 61 6e 64 20 /* Clean up and
1ff29 72 65 74 75 72 6e 0a 20 20 2a 2f 0a 20 20 73 71 return. */. sq
1ff2a 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 lite3ExprDelete(
1ff2b 64 62 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 db, pExpr->pLeft
1ff2c 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70 4c 65 66 );. pExpr->pLef
1ff2d 74 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 t = 0;. sqlite3
1ff2e 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 ExprDelete(db, p
1ff2f 45 78 70 72 2d 3e 70 52 69 67 68 74 29 3b 0a 20 Expr->pRight);.
1ff30 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 20 3d pExpr->pRight =
1ff31 20 30 3b 0a 20 20 70 45 78 70 72 2d 3e 6f 70 20 0;. pExpr->op
1ff32 3d 20 28 69 73 54 72 69 67 67 65 72 20 3f 20 54 = (isTrigger ? T
1ff33 4b 5f 54 52 49 47 47 45 52 20 3a 20 54 4b 5f 43 K_TRIGGER : TK_C
1ff34 4f 4c 55 4d 4e 29 3b 0a 6c 6f 6f 6b 75 70 6e 61 OLUMN);.lookupna
1ff35 6d 65 5f 65 6e 64 3a 0a 20 20 69 66 28 20 63 6e me_end:. if( cn
1ff36 74 3d 3d 31 20 29 7b 0a 20 20 20 20 61 73 73 65 t==1 ){. asse
1ff37 72 74 28 20 70 4e 43 21 3d 30 20 29 3b 0a 20 20 rt( pNC!=0 );.
1ff38 20 20 73 71 6c 69 74 65 33 41 75 74 68 52 65 61 sqlite3AuthRea
1ff39 64 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c d(pParse, pExpr,
1ff3a 20 70 53 63 68 65 6d 61 2c 20 70 4e 43 2d 3e 70 pSchema, pNC->p
1ff3b 53 72 63 4c 69 73 74 29 3b 0a 20 20 20 20 2f 2a SrcList);. /*
1ff3c 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 6e Increment the n
1ff3d 52 65 66 20 76 61 6c 75 65 20 6f 6e 20 61 6c 6c Ref value on all
1ff3e 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 73 20 66 name contexts f
1ff3f 72 6f 6d 20 54 6f 70 4e 43 20 75 70 20 74 6f 0a rom TopNC up to.
1ff40 20 20 20 20 2a 2a 20 74 68 65 20 70 6f 69 6e 74 ** the point
1ff41 20 77 68 65 72 65 20 74 68 65 20 6e 61 6d 65 20 where the name
1ff42 6d 61 74 63 68 65 64 2e 20 2a 2f 0a 20 20 20 20 matched. */.
1ff43 66 6f 72 28 3b 3b 29 7b 0a 20 20 20 20 20 20 61 for(;;){. a
1ff44 73 73 65 72 74 28 20 70 54 6f 70 4e 43 21 3d 30 ssert( pTopNC!=0
1ff45 20 29 3b 0a 20 20 20 20 20 20 70 54 6f 70 4e 43 );. pTopNC
1ff46 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 20 20 ->nRef++;.
1ff47 69 66 28 20 70 54 6f 70 4e 43 3d 3d 70 4e 43 20 if( pTopNC==pNC
1ff48 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 70 ) break;. p
1ff49 54 6f 70 4e 43 20 3d 20 70 54 6f 70 4e 43 2d 3e TopNC = pTopNC->
1ff4a 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 pNext;. }.
1ff4b 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e return WRC_Prun
1ff4c 65 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 e;. } else {.
1ff4d 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f return WRC_Abo
1ff4e 72 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a rt;. }.}../*.**
1ff4f 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 72 65 Allocate and re
1ff50 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 turn a pointer t
1ff51 6f 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 o an expression
1ff52 74 6f 20 6c 6f 61 64 20 74 68 65 20 63 6f 6c 75 to load the colu
1ff53 6d 6e 20 69 43 6f 6c 0a 2a 2a 20 66 72 6f 6d 20 mn iCol.** from
1ff54 64 61 74 61 73 6f 75 72 63 65 20 69 53 72 63 20 datasource iSrc
1ff55 64 61 74 61 73 6f 75 72 63 65 20 69 6e 20 53 72 datasource in Sr
1ff56 63 4c 69 73 74 20 70 53 72 63 2e 0a 2a 2f 0a 53 cList pSrc..*/.S
1ff57 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 QLITE_PRIVATE Ex
1ff58 70 72 20 2a 73 71 6c 69 74 65 33 43 72 65 61 74 pr *sqlite3Creat
1ff59 65 43 6f 6c 75 6d 6e 45 78 70 72 28 73 71 6c 69 eColumnExpr(sqli
1ff5a 74 65 33 20 2a 64 62 2c 20 53 72 63 4c 69 73 74 te3 *db, SrcList
1ff5b 20 2a 70 53 72 63 2c 20 69 6e 74 20 69 53 72 63 *pSrc, int iSrc
1ff5c 2c 20 69 6e 74 20 69 43 6f 6c 29 7b 0a 20 20 45 , int iCol){. E
1ff5d 78 70 72 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 xpr *p = sqlite3
1ff5e 45 78 70 72 41 6c 6c 6f 63 28 64 62 2c 20 54 4b ExprAlloc(db, TK
1ff5f 5f 43 4f 4c 55 4d 4e 2c 20 30 2c 20 30 29 3b 0a _COLUMN, 0, 0);.
1ff60 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 73 if( p ){. s
1ff61 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 truct SrcList_it
1ff62 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 53 72 em *pItem = &pSr
1ff63 63 2d 3e 61 5b 69 53 72 63 5d 3b 0a 20 20 20 20 c->a[iSrc];.
1ff64 70 2d 3e 70 54 61 62 20 3d 20 70 49 74 65 6d 2d p->pTab = pItem-
1ff65 3e 70 54 61 62 3b 0a 20 20 20 20 70 2d 3e 69 54 >pTab;. p->iT
1ff66 61 62 6c 65 20 3d 20 70 49 74 65 6d 2d 3e 69 43 able = pItem->iC
1ff67 75 72 73 6f 72 3b 0a 20 20 20 20 69 66 28 20 70 ursor;. if( p
1ff68 2d 3e 70 54 61 62 2d 3e 69 50 4b 65 79 3d 3d 69 ->pTab->iPKey==i
1ff69 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e Col ){. p->
1ff6a 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 iColumn = -1;.
1ff6b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 }else{. p
1ff6c 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 28 79 6e 56 ->iColumn = (ynV
1ff6d 61 72 29 69 43 6f 6c 3b 0a 20 20 20 20 20 20 70 ar)iCol;. p
1ff6e 49 74 65 6d 2d 3e 63 6f 6c 55 73 65 64 20 7c 3d Item->colUsed |=
1ff6f 20 28 28 42 69 74 6d 61 73 6b 29 31 29 3c 3c 28 ((Bitmask)1)<<(
1ff70 69 43 6f 6c 3e 3d 42 4d 53 20 3f 20 42 4d 53 2d iCol>=BMS ? BMS-
1ff71 31 20 3a 20 69 43 6f 6c 29 3b 0a 20 20 20 20 7d 1 : iCol);. }
1ff72 0a 20 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 . ExprSetProp
1ff73 65 72 74 79 28 70 2c 20 45 50 5f 52 65 73 6f 6c erty(p, EP_Resol
1ff74 76 65 64 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 ved);. }. retu
1ff75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 rn p;.}../*.** T
1ff76 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 his routine is c
1ff77 61 6c 6c 62 61 63 6b 20 66 6f 72 20 73 71 6c 69 allback for sqli
1ff78 74 65 33 57 61 6c 6b 45 78 70 72 28 29 2e 0a 2a te3WalkExpr()..*
1ff79 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 73 79 6d *.** Resolve sym
1ff7a 62 6f 6c 69 63 20 6e 61 6d 65 73 20 69 6e 74 6f bolic names into
1ff7b 20 54 4b 5f 43 4f 4c 55 4d 4e 20 6f 70 65 72 61 TK_COLUMN opera
1ff7c 74 6f 72 73 20 66 6f 72 20 74 68 65 20 63 75 72 tors for the cur
1ff7d 72 65 6e 74 0a 2a 2a 20 6e 6f 64 65 20 69 6e 20 rent.** node in
1ff7e 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 the expression t
1ff7f 72 65 65 2e 20 20 52 65 74 75 72 6e 20 30 20 74 ree. Return 0 t
1ff80 6f 20 63 6f 6e 74 69 6e 75 65 20 74 68 65 20 73 o continue the s
1ff81 65 61 72 63 68 20 64 6f 77 6e 0a 2a 2a 20 74 68 earch down.** th
1ff82 65 20 74 72 65 65 20 6f 72 20 32 20 74 6f 20 61 e tree or 2 to a
1ff83 62 6f 72 74 20 74 68 65 20 74 72 65 65 20 77 61 bort the tree wa
1ff84 6c 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 lk..**.** This r
1ff85 6f 75 74 69 6e 65 20 61 6c 73 6f 20 64 6f 65 73 outine also does
1ff86 20 65 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 20 error checking
1ff87 61 6e 64 20 6e 61 6d 65 20 72 65 73 6f 6c 75 74 and name resolut
1ff88 69 6f 6e 20 66 6f 72 0a 2a 2a 20 66 75 6e 63 74 ion for.** funct
1ff89 69 6f 6e 20 6e 61 6d 65 73 2e 20 20 54 68 65 20 ion names. The
1ff8a 6f 70 65 72 61 74 6f 72 20 66 6f 72 20 61 67 67 operator for agg
1ff8b 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 regate functions
1ff8c 20 69 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 74 is changed.** t
1ff8d 6f 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f o TK_AGG_FUNCTIO
1ff8e 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 N..*/.static int
1ff8f 20 72 65 73 6f 6c 76 65 45 78 70 72 53 74 65 70 resolveExprStep
1ff90 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 (Walker *pWalker
1ff91 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a , Expr *pExpr){.
1ff92 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 NameContext *p
1ff93 4e 43 3b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 NC;. Parse *pPa
1ff94 72 73 65 3b 0a 0a 20 20 70 4e 43 20 3d 20 70 57 rse;.. pNC = pW
1ff95 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43 3b 0a 20 20 alker->u.pNC;.
1ff96 61 73 73 65 72 74 28 20 70 4e 43 21 3d 30 20 29 assert( pNC!=0 )
1ff97 3b 0a 20 20 70 50 61 72 73 65 20 3d 20 70 4e 43 ;. pParse = pNC
1ff98 2d 3e 70 50 61 72 73 65 3b 0a 20 20 61 73 73 65 ->pParse;. asse
1ff99 72 74 28 20 70 50 61 72 73 65 3d 3d 70 57 61 6c rt( pParse==pWal
1ff9a 6b 65 72 2d 3e 70 50 61 72 73 65 20 29 3b 0a 0a ker->pParse );..
1ff9b 20 20 69 66 28 20 45 78 70 72 48 61 73 41 6e 79 if( ExprHasAny
1ff9c 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 Property(pExpr,
1ff9d 45 50 5f 52 65 73 6f 6c 76 65 64 29 20 29 20 72 EP_Resolved) ) r
1ff9e 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b eturn WRC_Prune;
1ff9f 0a 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 . ExprSetProper
1ffa0 74 79 28 70 45 78 70 72 2c 20 45 50 5f 52 65 73 ty(pExpr, EP_Res
1ffa1 6f 6c 76 65 64 29 3b 0a 23 69 66 6e 64 65 66 20 olved);.#ifndef
1ffa2 4e 44 45 42 55 47 0a 20 20 69 66 28 20 70 4e 43 NDEBUG. if( pNC
1ffa3 2d 3e 70 53 72 63 4c 69 73 74 20 26 26 20 70 4e ->pSrcList && pN
1ffa4 43 2d 3e 70 53 72 63 4c 69 73 74 2d 3e 6e 41 6c C->pSrcList->nAl
1ffa5 6c 6f 63 3e 30 20 29 7b 0a 20 20 20 20 53 72 63 loc>0 ){. Src
1ffa6 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 74 20 3d List *pSrcList =
1ffa7 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 3b 0a pNC->pSrcList;.
1ffa8 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 int i;. f
1ffa9 6f 72 28 69 3d 30 3b 20 69 3c 70 4e 43 2d 3e 70 or(i=0; i<pNC->p
1ffaa 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 SrcList->nSrc; i
1ffab 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 ++){. asser
1ffac 74 28 20 70 53 72 63 4c 69 73 74 2d 3e 61 5b 69 t( pSrcList->a[i
1ffad 5d 2e 69 43 75 72 73 6f 72 3e 3d 30 20 26 26 20 ].iCursor>=0 &&
1ffae 70 53 72 63 4c 69 73 74 2d 3e 61 5b 69 5d 2e 69 pSrcList->a[i].i
1ffaf 43 75 72 73 6f 72 3c 70 50 61 72 73 65 2d 3e 6e Cursor<pParse->n
1ffb0 54 61 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a Tab);. }. }.
1ffb1 23 65 6e 64 69 66 0a 20 20 73 77 69 74 63 68 28 #endif. switch(
1ffb2 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 0a 23 pExpr->op ){..#
1ffb3 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 if defined(SQLIT
1ffb4 45 5f 45 4e 41 42 4c 45 5f 55 50 44 41 54 45 5f E_ENABLE_UPDATE_
1ffb5 44 45 4c 45 54 45 5f 4c 49 4d 49 54 29 20 26 26 DELETE_LIMIT) &&
1ffb6 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
1ffb7 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 0a _OMIT_SUBQUERY).
1ffb8 20 20 20 20 2f 2a 20 54 68 65 20 73 70 65 63 69 /* The speci
1ffb9 61 6c 20 6f 70 65 72 61 74 6f 72 20 54 4b 5f 52 al operator TK_R
1ffba 4f 57 20 6d 65 61 6e 73 20 75 73 65 20 74 68 65 OW means use the
1ffbb 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20 66 rowid for the f
1ffbc 69 72 73 74 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 irst. ** colu
1ffbd 6d 6e 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 mn in the FROM c
1ffbe 6c 61 75 73 65 2e 20 20 54 68 69 73 20 69 73 20 lause. This is
1ffbf 75 73 65 64 20 62 79 20 74 68 65 20 4c 49 4d 49 used by the LIMI
1ffc0 54 20 61 6e 64 20 4f 52 44 45 52 20 42 59 0a 20 T and ORDER BY.
1ffc1 20 20 20 2a 2a 20 63 6c 61 75 73 65 20 70 72 6f ** clause pro
1ffc2 63 65 73 73 69 6e 67 20 6f 6e 20 55 50 44 41 54 cessing on UPDAT
1ffc3 45 20 61 6e 64 20 44 45 4c 45 54 45 20 73 74 61 E and DELETE sta
1ffc4 74 65 6d 65 6e 74 73 2e 0a 20 20 20 20 2a 2f 0a tements.. */.
1ffc5 20 20 20 20 63 61 73 65 20 54 4b 5f 52 4f 57 3a case TK_ROW:
1ffc6 20 7b 0a 20 20 20 20 20 20 53 72 63 4c 69 73 74 {. SrcList
1ffc7 20 2a 70 53 72 63 4c 69 73 74 20 3d 20 70 4e 43 *pSrcList = pNC
1ffc8 2d 3e 70 53 72 63 4c 69 73 74 3b 0a 20 20 20 20 ->pSrcList;.
1ffc9 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 struct SrcList
1ffca 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 _item *pItem;.
1ffcb 20 20 20 20 61 73 73 65 72 74 28 20 70 53 72 63 assert( pSrc
1ffcc 4c 69 73 74 20 26 26 20 70 53 72 63 4c 69 73 74 List && pSrcList
1ffcd 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 20 ->nSrc==1 );.
1ffce 20 20 20 70 49 74 65 6d 20 3d 20 70 53 72 63 4c pItem = pSrcL
1ffcf 69 73 74 2d 3e 61 3b 20 0a 20 20 20 20 20 20 70 ist->a; . p
1ffd0 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 43 4f Expr->op = TK_CO
1ffd1 4c 55 4d 4e 3b 0a 20 20 20 20 20 20 70 45 78 70 LUMN;. pExp
1ffd2 72 2d 3e 70 54 61 62 20 3d 20 70 49 74 65 6d 2d r->pTab = pItem-
1ffd3 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 70 45 78 >pTab;. pEx
1ffd4 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 49 74 pr->iTable = pIt
1ffd5 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 em->iCursor;.
1ffd6 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d pExpr->iColum
1ffd7 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 70 45 n = -1;. pE
1ffd8 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 xpr->affinity =
1ffd9 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 SQLITE_AFF_INTEG
1ffda 45 52 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b ER;. break;
1ffdb 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a . }.#endif /*
1ffdc 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
1ffdd 45 4e 41 42 4c 45 5f 55 50 44 41 54 45 5f 44 45 ENABLE_UPDATE_DE
1ffde 4c 45 54 45 5f 4c 49 4d 49 54 29 20 26 26 20 21 LETE_LIMIT) && !
1ffdf 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
1ffe0 4d 49 54 5f 53 55 42 51 55 45 52 59 29 20 2a 2f MIT_SUBQUERY) */
1ffe1 0a 0a 20 20 20 20 2f 2a 20 41 20 6c 6f 6e 65 20 .. /* A lone
1ffe2 69 64 65 6e 74 69 66 69 65 72 20 69 73 20 74 68 identifier is th
1ffe3 65 20 6e 61 6d 65 20 6f 66 20 61 20 63 6f 6c 75 e name of a colu
1ffe4 6d 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 mn.. */. c
1ffe5 61 73 65 20 54 4b 5f 49 44 3a 20 7b 0a 20 20 20 ase TK_ID: {.
1ffe6 20 20 20 72 65 74 75 72 6e 20 6c 6f 6f 6b 75 70 return lookup
1ffe7 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 30 2c 20 Name(pParse, 0,
1ffe8 30 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 0, pExpr->u.zTok
1ffe9 65 6e 2c 20 70 4e 43 2c 20 70 45 78 70 72 29 3b en, pNC, pExpr);
1ffea 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a . }. . /*
1ffeb 20 41 20 74 61 62 6c 65 20 6e 61 6d 65 20 61 6e A table name an
1ffec 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 3a 20 20 d column name:
1ffed 20 20 20 49 44 2e 49 44 0a 20 20 20 20 2a 2a 20 ID.ID. **
1ffee 4f 72 20 61 20 64 61 74 61 62 61 73 65 2c 20 74 Or a database, t
1ffef 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 3a able and column:
1fff0 20 20 49 44 2e 49 44 2e 49 44 0a 20 20 20 20 2a ID.ID.ID. *
1fff1 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 44 4f /. case TK_DO
1fff2 54 3a 20 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 T: {. const
1fff3 20 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e 3b 0a char *zColumn;.
1fff4 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 const char
1fff5 20 2a 7a 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 *zTable;.
1fff6 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b const char *zDb;
1fff7 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 52 69 . Expr *pRi
1fff8 67 68 74 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 69 ght;.. /* i
1fff9 66 28 20 70 53 72 63 4c 69 73 74 3d 3d 30 20 29 f( pSrcList==0 )
1fffa 20 62 72 65 61 6b 3b 20 2a 2f 0a 20 20 20 20 20 break; */.
1fffb 20 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d pRight = pExpr-
1fffc 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 69 >pRight;. i
1fffd 66 28 20 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 f( pRight->op==T
1fffe 4b 5f 49 44 20 29 7b 0a 20 20 20 20 20 20 20 20 K_ID ){.
1ffff 7a 44 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 zDb = 0;.
20000 20 7a 54 61 62 6c 65 20 3d 20 70 45 78 70 72 2d zTable = pExpr-
20001 3e 70 4c 65 66 74 2d 3e 75 2e 7a 54 6f 6b 65 6e >pLeft->u.zToken
20002 3b 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c 75 6d ;. zColum
20003 6e 20 3d 20 70 52 69 67 68 74 2d 3e 75 2e 7a 54 n = pRight->u.zT
20004 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 7d 65 6c 73 oken;. }els
20005 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 e{. asser
20006 74 28 20 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 t( pRight->op==T
20007 4b 5f 44 4f 54 20 29 3b 0a 20 20 20 20 20 20 20 K_DOT );.
20008 20 7a 44 62 20 3d 20 70 45 78 70 72 2d 3e 70 4c zDb = pExpr->pL
20009 65 66 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 eft->u.zToken;.
2000a 20 20 20 20 20 20 20 7a 54 61 62 6c 65 20 3d 20 zTable =
2000b 70 52 69 67 68 74 2d 3e 70 4c 65 66 74 2d 3e 75 pRight->pLeft->u
2000c 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 .zToken;.
2000d 20 7a 43 6f 6c 75 6d 6e 20 3d 20 70 52 69 67 68 zColumn = pRigh
2000e 74 2d 3e 70 52 69 67 68 74 2d 3e 75 2e 7a 54 6f t->pRight->u.zTo
2000f 6b 65 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ken;. }.
20010 20 20 20 72 65 74 75 72 6e 20 6c 6f 6f 6b 75 70 return lookup
20011 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 7a 44 62 Name(pParse, zDb
20012 2c 20 7a 54 61 62 6c 65 2c 20 7a 43 6f 6c 75 6d , zTable, zColum
20013 6e 2c 20 70 4e 43 2c 20 70 45 78 70 72 29 3b 0a n, pNC, pExpr);.
20014 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 }.. /* Re
20015 73 6f 6c 76 65 20 66 75 6e 63 74 69 6f 6e 20 6e solve function n
20016 61 6d 65 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ames. */.
20017 63 61 73 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 case TK_CONST_FU
20018 4e 43 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f NC:. case TK_
20019 46 55 4e 43 54 49 4f 4e 3a 20 7b 0a 20 20 20 20 FUNCTION: {.
2001a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 ExprList *pLis
2001b 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 t = pExpr->x.pLi
2001c 73 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 61 72 st; /* The ar
2001d 67 75 6d 65 6e 74 20 6c 69 73 74 20 2a 2f 0a 20 gument list */.
2001e 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 4c 69 int n = pLi
2001f 73 74 20 3f 20 70 4c 69 73 74 2d 3e 6e 45 78 70 st ? pList->nExp
20020 72 20 3a 20 30 3b 20 20 20 20 2f 2a 20 4e 75 6d r : 0; /* Num
20021 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 ber of arguments
20022 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 6f */. int no
20023 5f 73 75 63 68 5f 66 75 6e 63 20 3d 20 30 3b 20 _such_func = 0;
20024 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
20025 20 6e 6f 20 73 75 63 68 20 66 75 6e 63 74 69 6f no such functio
20026 6e 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 20 20 n exists */.
20027 20 20 69 6e 74 20 77 72 6f 6e 67 5f 6e 75 6d 5f int wrong_num_
20028 61 72 67 73 20 3d 20 30 3b 20 20 20 20 20 2f 2a args = 0; /*
20029 20 54 72 75 65 20 69 66 20 77 72 6f 6e 67 20 6e True if wrong n
2002a 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e umber of argumen
2002b 74 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 ts */. int
2002c 69 73 5f 61 67 67 20 3d 20 30 3b 20 20 20 20 20 is_agg = 0;
2002d 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
2002e 69 66 20 69 73 20 61 6e 20 61 67 67 72 65 67 61 if is an aggrega
2002f 74 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 te function */.
20030 20 20 20 20 20 69 6e 74 20 61 75 74 68 3b 20 20 int auth;
20031 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20032 20 2f 2a 20 41 75 74 68 6f 72 69 7a 61 74 69 6f /* Authorizatio
20033 6e 20 74 6f 20 75 73 65 20 74 68 65 20 66 75 6e n to use the fun
20034 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69 ction */. i
20035 6e 74 20 6e 49 64 3b 20 20 20 20 20 20 20 20 20 nt nId;
20036 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
20037 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 mber of characte
20038 72 73 20 69 6e 20 66 75 6e 63 74 69 6f 6e 20 6e rs in function n
20039 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e ame */. con
2003a 73 74 20 63 68 61 72 20 2a 7a 49 64 3b 20 20 20 st char *zId;
2003b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
2003c 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 2e 20 2a function name. *
2003d 2f 0a 20 20 20 20 20 20 46 75 6e 63 44 65 66 20 /. FuncDef
2003e 2a 70 44 65 66 3b 20 20 20 20 20 20 20 20 20 20 *pDef;
2003f 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 /* Informati
20040 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 66 75 6e on about the fun
20041 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 75 ction */. u
20042 38 20 65 6e 63 20 3d 20 45 4e 43 28 70 50 61 72 8 enc = ENC(pPar
20043 73 65 2d 3e 64 62 29 3b 20 20 20 2f 2a 20 54 68 se->db); /* Th
20044 65 20 64 61 74 61 62 61 73 65 20 65 6e 63 6f 64 e database encod
20045 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 20 20 74 65 ing */.. te
20046 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f stcase( pExpr->o
20047 70 3d 3d 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 p==TK_CONST_FUNC
20048 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
20049 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 ( !ExprHasProper
2004a 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 ty(pExpr, EP_xIs
2004b 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20 Select) );.
2004c 20 7a 49 64 20 3d 20 70 45 78 70 72 2d 3e 75 2e zId = pExpr->u.
2004d 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 6e 49 zToken;. nI
2004e 64 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 d = sqlite3Strle
2004f 6e 33 30 28 7a 49 64 29 3b 0a 20 20 20 20 20 20 n30(zId);.
20050 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33 46 69 pDef = sqlite3Fi
20051 6e 64 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73 ndFunction(pPars
20052 65 2d 3e 64 62 2c 20 7a 49 64 2c 20 6e 49 64 2c e->db, zId, nId,
20053 20 6e 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 20 20 n, enc, 0);.
20054 20 20 20 69 66 28 20 70 44 65 66 3d 3d 30 20 29 if( pDef==0 )
20055 7b 0a 20 20 20 20 20 20 20 20 70 44 65 66 20 3d {. pDef =
20056 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 sqlite3FindFunc
20057 74 69 6f 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c tion(pParse->db,
20058 20 7a 49 64 2c 20 6e 49 64 2c 20 2d 31 2c 20 65 zId, nId, -1, e
20059 6e 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 nc, 0);.
2005a 69 66 28 20 70 44 65 66 3d 3d 30 20 29 7b 0a 20 if( pDef==0 ){.
2005b 20 20 20 20 20 20 20 20 20 6e 6f 5f 73 75 63 68 no_such
2005c 5f 66 75 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 _func = 1;.
2005d 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
2005e 20 20 20 20 77 72 6f 6e 67 5f 6e 75 6d 5f 61 72 wrong_num_ar
2005f 67 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 gs = 1;.
20060 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
20061 20 20 20 20 20 20 20 69 73 5f 61 67 67 20 3d 20 is_agg =
20062 70 44 65 66 2d 3e 78 46 75 6e 63 3d 3d 30 3b 0a pDef->xFunc==0;.
20063 20 20 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 }.#ifndef
20064 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 SQLITE_OMIT_AUTH
20065 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20 20 20 ORIZATION.
20066 69 66 28 20 70 44 65 66 20 29 7b 0a 20 20 20 20 if( pDef ){.
20067 20 20 20 20 61 75 74 68 20 3d 20 73 71 6c 69 74 auth = sqlit
20068 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 e3AuthCheck(pPar
20069 73 65 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43 54 se, SQLITE_FUNCT
2006a 49 4f 4e 2c 20 30 2c 20 70 44 65 66 2d 3e 7a 4e ION, 0, pDef->zN
2006b 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 ame, 0);.
2006c 20 69 66 28 20 61 75 74 68 21 3d 53 51 4c 49 54 if( auth!=SQLIT
2006d 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
2006e 20 20 69 66 28 20 61 75 74 68 3d 3d 53 51 4c 49 if( auth==SQLI
2006f 54 45 5f 44 45 4e 59 20 29 7b 0a 20 20 20 20 20 TE_DENY ){.
20070 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 sqlite3Er
20071 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 rorMsg(pParse, "
20072 6e 6f 74 20 61 75 74 68 6f 72 69 7a 65 64 20 74 not authorized t
20073 6f 20 75 73 65 20 66 75 6e 63 74 69 6f 6e 3a 20 o use function:
20074 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 %s",.
20075 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20076 20 20 20 20 20 20 20 20 20 70 44 65 66 2d 3e 7a pDef->z
20077 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 Name);.
20078 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a pNC->nErr++;.
20079 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
2007a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 pExpr->op
2007b 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a 20 20 20 20 20 = TK_NULL;.
2007c 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f return WRC_
2007d 50 72 75 6e 65 3b 0a 20 20 20 20 20 20 20 20 7d Prune;. }
2007e 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a . }.#endif.
2007f 20 20 20 20 20 20 69 66 28 20 69 73 5f 61 67 67 if( is_agg
20080 20 26 26 20 21 70 4e 43 2d 3e 61 6c 6c 6f 77 41 && !pNC->allowA
20081 67 67 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 gg ){. sq
20082 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
20083 61 72 73 65 2c 20 22 6d 69 73 75 73 65 20 6f 66 arse, "misuse of
20084 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 aggregate funct
20085 69 6f 6e 20 25 2e 2a 73 28 29 22 2c 20 6e 49 64 ion %.*s()", nId
20086 2c 7a 49 64 29 3b 0a 20 20 20 20 20 20 20 20 70 ,zId);. p
20087 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 NC->nErr++;.
20088 20 20 20 20 69 73 5f 61 67 67 20 3d 20 30 3b 0a is_agg = 0;.
20089 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
2008a 6e 6f 5f 73 75 63 68 5f 66 75 6e 63 20 29 7b 0a no_such_func ){.
2008b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 sqlite3E
2008c 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
2008d 22 6e 6f 20 73 75 63 68 20 66 75 6e 63 74 69 6f "no such functio
2008e 6e 3a 20 25 2e 2a 73 22 2c 20 6e 49 64 2c 20 7a n: %.*s", nId, z
2008f 49 64 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 43 Id);. pNC
20090 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 ->nErr++;.
20091 7d 65 6c 73 65 20 69 66 28 20 77 72 6f 6e 67 5f }else if( wrong_
20092 6e 75 6d 5f 61 72 67 73 20 29 7b 0a 20 20 20 20 num_args ){.
20093 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
20094 4d 73 67 28 70 50 61 72 73 65 2c 22 77 72 6f 6e Msg(pParse,"wron
20095 67 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 g number of argu
20096 6d 65 6e 74 73 20 74 6f 20 66 75 6e 63 74 69 6f ments to functio
20097 6e 20 25 2e 2a 73 28 29 22 2c 0a 20 20 20 20 20 n %.*s()",.
20098 20 20 20 20 20 20 20 20 6e 49 64 2c 20 7a 49 64 nId, zId
20099 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 43 2d 3e );. pNC->
2009a 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a nErr++;. }.
2009b 20 20 20 20 20 20 69 66 28 20 69 73 5f 61 67 67 if( is_agg
2009c 20 29 7b 0a 20 20 20 20 20 20 20 20 70 45 78 70 ){. pExp
2009d 72 2d 3e 6f 70 20 3d 20 54 4b 5f 41 47 47 5f 46 r->op = TK_AGG_F
2009e 55 4e 43 54 49 4f 4e 3b 0a 20 20 20 20 20 20 20 UNCTION;.
2009f 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 3d 20 31 pNC->hasAgg = 1
200a0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
200a1 69 66 28 20 69 73 5f 61 67 67 20 29 20 70 4e 43 if( is_agg ) pNC
200a2 2d 3e 61 6c 6c 6f 77 41 67 67 20 3d 20 30 3b 0a ->allowAgg = 0;.
200a3 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 61 6c sqlite3Wal
200a4 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c 6b 65 kExprList(pWalke
200a5 72 2c 20 70 4c 69 73 74 29 3b 0a 20 20 20 20 20 r, pList);.
200a6 20 69 66 28 20 69 73 5f 61 67 67 20 29 20 70 4e if( is_agg ) pN
200a7 43 2d 3e 61 6c 6c 6f 77 41 67 67 20 3d 20 31 3b C->allowAgg = 1;
200a8 0a 20 20 20 20 20 20 2f 2a 20 46 49 58 20 4d 45 . /* FIX ME
200a9 3a 20 20 43 6f 6d 70 75 74 65 20 70 45 78 70 72 : Compute pExpr
200aa 2d 3e 61 66 66 69 6e 69 74 79 20 62 61 73 65 64 ->affinity based
200ab 20 6f 6e 20 74 68 65 20 65 78 70 65 63 74 65 64 on the expected
200ac 20 72 65 74 75 72 6e 0a 20 20 20 20 20 20 2a 2a return. **
200ad 20 74 79 70 65 20 6f 66 20 74 68 65 20 66 75 6e type of the fun
200ae 63 74 69 6f 6e 20 0a 20 20 20 20 20 20 2a 2f 0a ction . */.
200af 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 return WRC
200b0 5f 50 72 75 6e 65 3b 0a 20 20 20 20 7d 0a 23 69 _Prune;. }.#i
200b1 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
200b2 54 5f 53 55 42 51 55 45 52 59 0a 20 20 20 20 63 T_SUBQUERY. c
200b3 61 73 65 20 54 4b 5f 53 45 4c 45 43 54 3a 0a 20 ase TK_SELECT:.
200b4 20 20 20 63 61 73 65 20 54 4b 5f 45 58 49 53 54 case TK_EXIST
200b5 53 3a 20 20 74 65 73 74 63 61 73 65 28 20 70 45 S: testcase( pE
200b6 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 49 53 xpr->op==TK_EXIS
200b7 54 53 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 TS );.#endif.
200b8 20 63 61 73 65 20 54 4b 5f 49 4e 3a 20 7b 0a 20 case TK_IN: {.
200b9 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 testcase( p
200ba 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 20 Expr->op==TK_IN
200bb 29 3b 0a 20 20 20 20 20 20 69 66 28 20 45 78 70 );. if( Exp
200bc 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 rHasProperty(pEx
200bd 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 pr, EP_xIsSelect
200be 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 ) ){. int
200bf 20 6e 52 65 66 20 3d 20 70 4e 43 2d 3e 6e 52 65 nRef = pNC->nRe
200c0 66 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 f;.#ifndef SQLIT
200c1 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 20 E_OMIT_CHECK.
200c2 20 20 20 20 20 69 66 28 20 70 4e 43 2d 3e 69 73 if( pNC->is
200c3 43 68 65 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 Check ){.
200c4 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
200c5 73 67 28 70 50 61 72 73 65 2c 22 73 75 62 71 75 sg(pParse,"subqu
200c6 65 72 69 65 73 20 70 72 6f 68 69 62 69 74 65 64 eries prohibited
200c7 20 69 6e 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 in CHECK constr
200c8 61 69 6e 74 73 22 29 3b 0a 20 20 20 20 20 20 20 aints");.
200c9 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 }.#endif.
200ca 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c sqlite3WalkSel
200cb 65 63 74 28 70 57 61 6c 6b 65 72 2c 20 70 45 78 ect(pWalker, pEx
200cc 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 29 3b 0a pr->x.pSelect);.
200cd 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
200ce 70 4e 43 2d 3e 6e 52 65 66 3e 3d 6e 52 65 66 20 pNC->nRef>=nRef
200cf 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e );. if( n
200d0 52 65 66 21 3d 70 4e 43 2d 3e 6e 52 65 66 20 29 Ref!=pNC->nRef )
200d1 7b 0a 20 20 20 20 20 20 20 20 20 20 45 78 70 72 {. Expr
200d2 53 65 74 50 72 6f 70 65 72 74 79 28 70 45 78 70 SetProperty(pExp
200d3 72 2c 20 45 50 5f 56 61 72 53 65 6c 65 63 74 29 r, EP_VarSelect)
200d4 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
200d5 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b }. break;
200d6 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 . }.#ifndef S
200d7 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b QLITE_OMIT_CHECK
200d8 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 56 41 52 . case TK_VAR
200d9 49 41 42 4c 45 3a 20 7b 0a 20 20 20 20 20 20 69 IABLE: {. i
200da 66 28 20 70 4e 43 2d 3e 69 73 43 68 65 63 6b 20 f( pNC->isCheck
200db 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
200dc 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
200dd 65 2c 22 70 61 72 61 6d 65 74 65 72 73 20 70 72 e,"parameters pr
200de 6f 68 69 62 69 74 65 64 20 69 6e 20 43 48 45 43 ohibited in CHEC
200df 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 22 29 3b K constraints");
200e0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 . }. b
200e1 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 reak;. }.#end
200e2 69 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 if. }. return
200e3 28 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c (pParse->nErr ||
200e4 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c pParse->db->mal
200e5 6c 6f 63 46 61 69 6c 65 64 29 20 3f 20 57 52 43 locFailed) ? WRC
200e6 5f 41 62 6f 72 74 20 3a 20 57 52 43 5f 43 6f 6e _Abort : WRC_Con
200e7 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 tinue;.}../*.**
200e8 70 45 4c 69 73 74 20 69 73 20 61 20 6c 69 73 74 pEList is a list
200e9 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 of expressions
200ea 77 68 69 63 68 20 61 72 65 20 72 65 61 6c 6c 79 which are really
200eb 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 the result set
200ec 6f 66 20 74 68 65 0a 2a 2a 20 61 20 53 45 4c 45 of the.** a SELE
200ed 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 70 CT statement. p
200ee 45 20 69 73 20 61 20 74 65 72 6d 20 69 6e 20 61 E is a term in a
200ef 6e 20 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 n ORDER BY or GR
200f0 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2e 0a 2a OUP BY clause..*
200f1 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 * This routine c
200f2 68 65 63 6b 73 20 74 6f 20 73 65 65 20 69 66 20 hecks to see if
200f3 70 45 20 69 73 20 61 20 73 69 6d 70 6c 65 20 69 pE is a simple i
200f4 64 65 6e 74 69 66 69 65 72 20 77 68 69 63 68 20 dentifier which
200f5 63 6f 72 72 65 73 70 6f 6e 64 73 0a 2a 2a 20 74 corresponds.** t
200f6 6f 20 74 68 65 20 41 53 2d 6e 61 6d 65 20 6f 66 o the AS-name of
200f7 20 6f 6e 65 20 6f 66 20 74 68 65 20 74 65 72 6d one of the term
200f8 73 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 s of the express
200f9 69 6f 6e 20 6c 69 73 74 2e 20 20 49 66 20 69 74 ion list. If it
200fa 20 69 73 2c 0a 2a 2a 20 74 68 69 73 20 72 6f 75 is,.** this rou
200fb 74 69 6e 65 20 72 65 74 75 72 6e 20 61 6e 20 69 tine return an i
200fc 6e 74 65 67 65 72 20 62 65 74 77 65 65 6e 20 31 nteger between 1
200fd 20 61 6e 64 20 4e 20 77 68 65 72 65 20 4e 20 69 and N where N i
200fe 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a s the number of.
200ff 2a 2a 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 70 ** elements in p
20100 45 4c 69 73 74 2c 20 63 6f 72 72 65 73 70 6f 6e EList, correspon
20101 64 69 6e 67 20 74 6f 20 74 68 65 20 6d 61 74 63 ding to the matc
20102 68 69 6e 67 20 65 6e 74 72 79 2e 20 20 49 66 20 hing entry. If
20103 74 68 65 72 65 20 69 73 0a 2a 2a 20 6e 6f 20 6d there is.** no m
20104 61 74 63 68 2c 20 6f 72 20 69 66 20 70 45 20 69 atch, or if pE i
20105 73 20 6e 6f 74 20 61 20 73 69 6d 70 6c 65 20 69 s not a simple i
20106 64 65 6e 74 69 66 69 65 72 2c 20 74 68 65 6e 20 dentifier, then
20107 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 this routine.**
20108 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 return 0..**.**
20109 70 45 4c 69 73 74 20 68 61 73 20 62 65 65 6e 20 pEList has been
2010a 72 65 73 6f 6c 76 65 64 2e 20 20 70 45 20 68 61 resolved. pE ha
2010b 73 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 s not..*/.static
2010c 20 69 6e 74 20 72 65 73 6f 6c 76 65 41 73 4e 61 int resolveAsNa
2010d 6d 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 me(. Parse *pPa
2010e 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 rse, /* Pars
2010f 69 6e 67 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 ing context for
20110 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a error messages *
20111 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 /. ExprList *pE
20112 4c 69 73 74 2c 20 20 2f 2a 20 4c 69 73 74 20 6f List, /* List o
20113 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 6f f expressions to
20114 20 73 63 61 6e 20 2a 2f 0a 20 20 45 78 70 72 20 scan */. Expr
20115 2a 70 45 20 20 20 20 20 20 20 20 20 20 20 2f 2a *pE /*
20116 20 45 78 70 72 65 73 73 69 6f 6e 20 77 65 20 61 Expression we a
20117 72 65 20 74 72 79 69 6e 67 20 74 6f 20 6d 61 74 re trying to mat
20118 63 68 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 ch */.){. int i
20119 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
2011a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f Loop counter */
2011b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d .. UNUSED_PARAM
2011c 45 54 45 52 28 70 50 61 72 73 65 29 3b 0a 0a 20 ETER(pParse);..
2011d 20 69 66 28 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f if( pE->op==TK_
2011e 49 44 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a ID ){. char *
2011f 7a 43 6f 6c 20 3d 20 70 45 2d 3e 75 2e 7a 54 6f zCol = pE->u.zTo
20120 6b 65 6e 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 ken;. for(i=0
20121 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 ; i<pEList->nExp
20122 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 r; i++){. c
20123 68 61 72 20 2a 7a 41 73 20 3d 20 70 45 4c 69 73 har *zAs = pELis
20124 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 t->a[i].zName;.
20125 20 20 20 20 20 69 66 28 20 7a 41 73 21 3d 30 20 if( zAs!=0
20126 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d && sqlite3StrICm
20127 70 28 7a 41 73 2c 20 7a 43 6f 6c 29 3d 3d 30 20 p(zAs, zCol)==0
20128 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 ){. retur
20129 6e 20 69 2b 31 3b 0a 20 20 20 20 20 20 7d 0a 20 n i+1;. }.
2012a 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
2012b 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 n 0;.}../*.** pE
2012c 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
2012d 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 77 an expression w
2012e 68 69 63 68 20 69 73 20 61 20 73 69 6e 67 6c 65 hich is a single
2012f 20 74 65 72 6d 20 69 6e 20 74 68 65 0a 2a 2a 20 term in the.**
20130 4f 52 44 45 52 20 42 59 20 6f 66 20 61 20 63 6f ORDER BY of a co
20131 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 2e 20 20 mpound SELECT.
20132 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 68 The expression h
20133 61 73 20 6e 6f 74 20 62 65 65 6e 0a 2a 2a 20 6e as not been.** n
20134 61 6d 65 20 72 65 73 6f 6c 76 65 64 2e 0a 2a 2a ame resolved..**
20135 0a 2a 2a 20 41 74 20 74 68 65 20 70 6f 69 6e 74 .** At the point
20136 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
20137 20 63 61 6c 6c 65 64 2c 20 77 65 20 61 6c 72 65 called, we alre
20138 61 64 79 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 ady know that th
20139 65 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20 74 65 e.** ORDER BY te
2013a 72 6d 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 rm is not an int
2013b 65 67 65 72 20 69 6e 64 65 78 20 69 6e 74 6f 20 eger index into
2013c 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20 the result set.
2013d 20 54 68 61 74 0a 2a 2a 20 63 61 73 65 20 69 73 That.** case is
2013e 20 68 61 6e 64 6c 65 64 20 62 79 20 74 68 65 20 handled by the
2013f 63 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 2e calling routine.
20140 0a 2a 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 .**.** Attempt t
20141 6f 20 6d 61 74 63 68 20 70 45 20 61 67 61 69 6e o match pE again
20142 73 74 20 72 65 73 75 6c 74 20 73 65 74 20 63 6f st result set co
20143 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 6c 65 66 lumns in the lef
20144 74 2d 6d 6f 73 74 0a 2a 2a 20 53 45 4c 45 43 54 t-most.** SELECT
20145 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 52 65 74 statement. Ret
20146 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20 69 20 urn the index i
20147 6f 66 20 74 68 65 20 6d 61 74 63 68 69 6e 67 20 of the matching
20148 63 6f 6c 75 6d 6e 2c 0a 2a 2a 20 61 73 20 61 6e column,.** as an
20149 20 69 6e 64 69 63 61 74 69 6f 6e 20 74 6f 20 74 indication to t
2014a 68 65 20 63 61 6c 6c 65 72 20 74 68 61 74 20 69 he caller that i
2014b 74 20 73 68 6f 75 6c 64 20 73 6f 72 74 20 62 79 t should sort by
2014c 20 74 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e the i-th column
2014d 2e 0a 2a 2a 20 54 68 65 20 6c 65 66 74 2d 6d 6f ..** The left-mo
2014e 73 74 20 63 6f 6c 75 6d 6e 20 69 73 20 31 2e 20 st column is 1.
2014f 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c In other words,
20150 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 the value retur
20151 6e 65 64 20 69 73 20 74 68 65 0a 2a 2a 20 73 61 ned is the.** sa
20152 6d 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 me integer value
20153 20 74 68 61 74 20 77 6f 75 6c 64 20 62 65 20 75 that would be u
20154 73 65 64 20 69 6e 20 74 68 65 20 53 51 4c 20 73 sed in the SQL s
20155 74 61 74 65 6d 65 6e 74 20 74 6f 20 69 6e 64 69 tatement to indi
20156 63 61 74 65 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 cate.** the colu
20157 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 mn..**.** If the
20158 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 2c 20 re is no match,
20159 72 65 74 75 72 6e 20 30 2e 20 20 52 65 74 75 72 return 0. Retur
2015a 6e 20 2d 31 20 69 66 20 61 6e 20 65 72 72 6f 72 n -1 if an error
2015b 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 occurs..*/.stat
2015c 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 4f 72 ic int resolveOr
2015d 64 65 72 42 79 54 65 72 6d 54 6f 45 78 70 72 4c derByTermToExprL
2015e 69 73 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 ist(. Parse *pP
2015f 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 arse, /* Par
20160 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 66 6f 72 sing context for
20161 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 error messages
20162 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 */. Select *pSe
20163 6c 65 63 74 2c 20 20 20 2f 2a 20 54 68 65 20 53 lect, /* The S
20164 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 ELECT statement
20165 77 69 74 68 20 74 68 65 20 4f 52 44 45 52 20 42 with the ORDER B
20166 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 Y clause */. Ex
20167 70 72 20 2a 70 45 20 20 20 20 20 20 20 20 20 20 pr *pE
20168 20 2f 2a 20 54 68 65 20 73 70 65 63 69 66 69 63 /* The specific
20169 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 2a ORDER BY term *
2016a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 /.){. int i;
2016b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f /* Loo
2016c 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 45 p counter */. E
2016d 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b xprList *pEList;
2016e 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 73 /* The columns
2016f 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 of the result s
20170 65 74 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 et */. NameCont
20171 65 78 74 20 6e 63 3b 20 20 20 20 2f 2a 20 4e 61 ext nc; /* Na
20172 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 72 me context for r
20173 65 73 6f 6c 76 69 6e 67 20 70 45 20 2a 2f 0a 0a esolving pE */..
20174 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
20175 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 70 3ExprIsInteger(p
20176 45 2c 20 26 69 29 3d 3d 30 20 29 3b 0a 20 20 70 E, &i)==0 );. p
20177 45 4c 69 73 74 20 3d 20 70 53 65 6c 65 63 74 2d EList = pSelect-
20178 3e 70 45 4c 69 73 74 3b 0a 0a 20 20 2f 2a 20 52 >pEList;.. /* R
20179 65 73 6f 6c 76 65 20 61 6c 6c 20 6e 61 6d 65 73 esolve all names
2017a 20 69 6e 20 74 68 65 20 4f 52 44 45 52 20 42 59 in the ORDER BY
2017b 20 74 65 72 6d 20 65 78 70 72 65 73 73 69 6f 6e term expression
2017c 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 . */. memset(&
2017d 6e 63 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6e 63 nc, 0, sizeof(nc
2017e 29 29 3b 0a 20 20 6e 63 2e 70 50 61 72 73 65 20 ));. nc.pParse
2017f 3d 20 70 50 61 72 73 65 3b 0a 20 20 6e 63 2e 70 = pParse;. nc.p
20180 53 72 63 4c 69 73 74 20 3d 20 70 53 65 6c 65 63 SrcList = pSelec
20181 74 2d 3e 70 53 72 63 3b 0a 20 20 6e 63 2e 70 45 t->pSrc;. nc.pE
20182 4c 69 73 74 20 3d 20 70 45 4c 69 73 74 3b 0a 20 List = pEList;.
20183 20 6e 63 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 31 nc.allowAgg = 1
20184 3b 0a 20 20 6e 63 2e 6e 45 72 72 20 3d 20 30 3b ;. nc.nErr = 0;
20185 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 . if( sqlite3Re
20186 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 solveExprNames(&
20187 6e 63 2c 20 70 45 29 20 29 7b 0a 20 20 20 20 73 nc, pE) ){. s
20188 71 6c 69 74 65 33 45 72 72 6f 72 43 6c 65 61 72 qlite3ErrorClear
20189 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 72 65 (pParse);. re
2018a 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f turn 0;. }.. /
2018b 2a 20 54 72 79 20 74 6f 20 6d 61 74 63 68 20 74 * Try to match t
2018c 68 65 20 4f 52 44 45 52 20 42 59 20 65 78 70 72 he ORDER BY expr
2018d 65 73 73 69 6f 6e 20 61 67 61 69 6e 73 74 20 61 ession against a
2018e 6e 20 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 2a n expression. *
2018f 2a 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 * in the result
20190 73 65 74 2e 20 20 52 65 74 75 72 6e 20 61 6e 20 set. Return an
20191 31 2d 62 61 73 65 64 20 69 6e 64 65 78 20 6f 66 1-based index of
20192 20 74 68 65 20 6d 61 74 63 68 69 6e 67 0a 20 20 the matching.
20193 2a 2a 20 72 65 73 75 6c 74 2d 73 65 74 20 65 6e ** result-set en
20194 74 72 79 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 try.. */. for(
20195 69 3d 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e i=0; i<pEList->n
20196 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 Expr; i++){.
20197 69 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 43 if( sqlite3ExprC
20198 6f 6d 70 61 72 65 28 70 45 4c 69 73 74 2d 3e 61 ompare(pEList->a
20199 5b 69 5d 2e 70 45 78 70 72 2c 20 70 45 29 20 29 [i].pExpr, pE) )
2019a 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 {. return i
2019b 2b 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 +1;. }. }..
2019c 20 2f 2a 20 49 66 20 6e 6f 20 6d 61 74 63 68 2c /* If no match,
2019d 20 72 65 74 75 72 6e 20 30 2e 20 2a 2f 0a 20 20 return 0. */.
2019e 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a return 0;.}../*.
2019f 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 6e 20 4f ** Generate an O
201a0 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50 RDER BY or GROUP
201a1 20 42 59 20 74 65 72 6d 20 6f 75 74 2d 6f 66 2d BY term out-of-
201a2 72 61 6e 67 65 20 65 72 72 6f 72 2e 0a 2a 2f 0a range error..*/.
201a3 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 73 6f static void reso
201a4 6c 76 65 4f 75 74 4f 66 52 61 6e 67 65 45 72 72 lveOutOfRangeErr
201a5 6f 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 or(. Parse *pPa
201a6 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 rse, /*
201a7 54 68 65 20 65 72 72 6f 72 20 63 6f 6e 74 65 78 The error contex
201a8 74 20 69 6e 74 6f 20 77 68 69 63 68 20 74 6f 20 t into which to
201a9 77 72 69 74 65 20 74 68 65 20 65 72 72 6f 72 20 write the error
201aa 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
201ab 2a 7a 54 79 70 65 2c 20 20 20 20 20 2f 2a 20 22 *zType, /* "
201ac 4f 52 44 45 52 22 20 6f 72 20 22 47 52 4f 55 50 ORDER" or "GROUP
201ad 22 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 20 20 " */. int i,
201ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
201af 20 54 68 65 20 69 6e 64 65 78 20 28 31 2d 62 61 The index (1-ba
201b0 73 65 64 29 20 6f 66 20 74 68 65 20 74 65 72 6d sed) of the term
201b1 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 2a 2f out of range */
201b2 0a 20 20 69 6e 74 20 6d 78 20 20 20 20 20 20 20 . int mx
201b3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 72 /* Lar
201b4 67 65 73 74 20 70 65 72 6d 69 73 73 69 62 6c 65 gest permissible
201b5 20 76 61 6c 75 65 20 6f 66 20 69 20 2a 2f 0a 29 value of i */.)
201b6 7b 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 {. sqlite3Error
201b7 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 Msg(pParse, .
201b8 20 22 25 72 20 25 73 20 42 59 20 74 65 72 6d 20 "%r %s BY term
201b9 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 2d 20 73 out of range - s
201ba 68 6f 75 6c 64 20 62 65 20 22 0a 20 20 20 20 22 hould be ". "
201bb 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 25 64 between 1 and %d
201bc 22 2c 20 69 2c 20 7a 54 79 70 65 2c 20 6d 78 29 ", i, zType, mx)
201bd 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 61 6c 79 ;.}../*.** Analy
201be 7a 65 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 ze the ORDER BY
201bf 63 6c 61 75 73 65 20 69 6e 20 61 20 63 6f 6d 70 clause in a comp
201c0 6f 75 6e 64 20 53 45 4c 45 43 54 20 73 74 61 74 ound SELECT stat
201c1 65 6d 65 6e 74 2e 20 20 20 4d 6f 64 69 66 79 0a ement. Modify.
201c2 2a 2a 20 65 61 63 68 20 74 65 72 6d 20 6f 66 20 ** each term of
201c3 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 the ORDER BY cla
201c4 75 73 65 20 69 73 20 61 20 63 6f 6e 73 74 61 6e use is a constan
201c5 74 20 69 6e 74 65 67 65 72 20 62 65 74 77 65 65 t integer betwee
201c6 6e 20 31 0a 2a 2a 20 61 6e 64 20 4e 20 77 68 65 n 1.** and N whe
201c7 72 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 re N is the numb
201c8 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e er of columns in
201c9 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 53 45 the compound SE
201ca 4c 45 43 54 2e 0a 2a 2a 0a 2a 2a 20 4f 52 44 45 LECT..**.** ORDE
201cb 52 20 42 59 20 74 65 72 6d 73 20 74 68 61 74 20 R BY terms that
201cc 61 72 65 20 61 6c 72 65 61 64 79 20 61 6e 20 69 are already an i
201cd 6e 74 65 67 65 72 20 62 65 74 77 65 65 6e 20 31 nteger between 1
201ce 20 61 6e 64 20 4e 20 61 72 65 0a 2a 2a 20 75 6e and N are.** un
201cf 6d 6f 64 69 66 69 65 64 2e 20 20 4f 52 44 45 52 modified. ORDER
201d0 20 42 59 20 74 65 72 6d 73 20 74 68 61 74 20 61 BY terms that a
201d1 72 65 20 69 6e 74 65 67 65 72 73 20 6f 75 74 73 re integers outs
201d2 69 64 65 20 74 68 65 20 72 61 6e 67 65 20 6f 66 ide the range of
201d3 0a 2a 2a 20 31 20 74 68 72 6f 75 67 68 20 4e 20 .** 1 through N
201d4 67 65 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f generate an erro
201d5 72 2e 20 20 4f 52 44 45 52 20 42 59 20 74 65 72 r. ORDER BY ter
201d6 6d 73 20 74 68 61 74 20 61 72 65 20 65 78 70 72 ms that are expr
201d7 65 73 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 6d essions.** are m
201d8 61 74 63 68 65 64 20 61 67 61 69 6e 73 74 20 72 atched against r
201d9 65 73 75 6c 74 20 73 65 74 20 65 78 70 72 65 73 esult set expres
201da 73 69 6f 6e 73 20 6f 66 20 63 6f 6d 70 6f 75 6e sions of compoun
201db 64 20 53 45 4c 45 43 54 0a 2a 2a 20 62 65 67 69 d SELECT.** begi
201dc 6e 6e 69 6e 67 20 77 69 74 68 20 74 68 65 20 6c nning with the l
201dd 65 66 74 2d 6d 6f 73 74 20 53 45 4c 45 43 54 20 eft-most SELECT
201de 61 6e 64 20 77 6f 72 6b 69 6e 67 20 74 6f 77 61 and working towa
201df 72 64 20 74 68 65 20 72 69 67 68 74 2e 0a 2a 2a rd the right..**
201e0 20 41 74 20 74 68 65 20 66 69 72 73 74 20 6d 61 At the first ma
201e1 74 63 68 2c 20 74 68 65 20 4f 52 44 45 52 20 42 tch, the ORDER B
201e2 59 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 Y expression is
201e3 74 72 61 6e 73 66 6f 72 6d 65 64 20 69 6e 74 6f transformed into
201e4 0a 2a 2a 20 74 68 65 20 69 6e 74 65 67 65 72 20 .** the integer
201e5 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e 0a 2a column number..*
201e6 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
201e7 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 number of errors
201e8 20 73 65 65 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 seen..*/.static
201e9 20 69 6e 74 20 72 65 73 6f 6c 76 65 43 6f 6d 70 int resolveComp
201ea 6f 75 6e 64 4f 72 64 65 72 42 79 28 0a 20 20 50 oundOrderBy(. P
201eb 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 arse *pParse,
201ec 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 /* Parsing
201ed 63 6f 6e 74 65 78 74 2e 20 20 4c 65 61 76 65 20 context. Leave
201ee 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 68 error messages h
201ef 65 72 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 ere */. Select
201f0 2a 70 53 65 6c 65 63 74 20 20 20 20 20 20 20 2f *pSelect /
201f1 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 * The SELECT sta
201f2 74 65 6d 65 6e 74 20 63 6f 6e 74 61 69 6e 69 6e tement containin
201f3 67 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 2a g the ORDER BY *
201f4 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 /.){. int i;.
201f5 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 ExprList *pOrder
201f6 42 79 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a By;. ExprList *
201f7 70 45 4c 69 73 74 3b 0a 20 20 73 71 6c 69 74 65 pEList;. sqlite
201f8 33 20 2a 64 62 3b 0a 20 20 69 6e 74 20 6d 6f 72 3 *db;. int mor
201f9 65 54 6f 44 6f 20 3d 20 31 3b 0a 0a 20 20 70 4f eToDo = 1;.. pO
201fa 72 64 65 72 42 79 20 3d 20 70 53 65 6c 65 63 74 rderBy = pSelect
201fb 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20 69 66 ->pOrderBy;. if
201fc 28 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 20 ( pOrderBy==0 )
201fd 72 65 74 75 72 6e 20 30 3b 0a 20 20 64 62 20 3d return 0;. db =
201fe 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 23 69 66 pParse->db;.#if
201ff 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 SQLITE_MAX_COLU
20200 4d 4e 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 MN. if( pOrderB
20201 79 2d 3e 6e 45 78 70 72 3e 64 62 2d 3e 61 4c 69 y->nExpr>db->aLi
20202 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 mit[SQLITE_LIMIT
20203 5f 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20 20 _COLUMN] ){.
20204 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
20205 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e pParse, "too man
20206 79 20 74 65 72 6d 73 20 69 6e 20 4f 52 44 45 52 y terms in ORDER
20207 20 42 59 20 63 6c 61 75 73 65 22 29 3b 0a 20 20 BY clause");.
20208 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a return 1;. }.
20209 23 65 6e 64 69 66 0a 20 20 66 6f 72 28 69 3d 30 #endif. for(i=0
2020a 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 ; i<pOrderBy->nE
2020b 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 xpr; i++){. p
2020c 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 64 6f OrderBy->a[i].do
2020d 6e 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 53 ne = 0;. }. pS
2020e 65 6c 65 63 74 2d 3e 70 4e 65 78 74 20 3d 20 30 elect->pNext = 0
2020f 3b 0a 20 20 77 68 69 6c 65 28 20 70 53 65 6c 65 ;. while( pSele
20210 63 74 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20 ct->pPrior ){.
20211 20 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f pSelect->pPrio
20212 72 2d 3e 70 4e 65 78 74 20 3d 20 70 53 65 6c 65 r->pNext = pSele
20213 63 74 3b 0a 20 20 20 20 70 53 65 6c 65 63 74 20 ct;. pSelect
20214 3d 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f = pSelect->pPrio
20215 72 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 r;. }. while(
20216 70 53 65 6c 65 63 74 20 26 26 20 6d 6f 72 65 54 pSelect && moreT
20217 6f 44 6f 20 29 7b 0a 20 20 20 20 73 74 72 75 63 oDo ){. struc
20218 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 t ExprList_item
20219 2a 70 49 74 65 6d 3b 0a 20 20 20 20 6d 6f 72 65 *pItem;. more
2021a 54 6f 44 6f 20 3d 20 30 3b 0a 20 20 20 20 70 45 ToDo = 0;. pE
2021b 4c 69 73 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e List = pSelect->
2021c 70 45 4c 69 73 74 3b 0a 20 20 20 20 61 73 73 65 pEList;. asse
2021d 72 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b rt( pEList!=0 );
2021e 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 . for(i=0, pI
2021f 74 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b tem=pOrderBy->a;
20220 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 i<pOrderBy->nEx
20221 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b pr; i++, pItem++
20222 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f ){. int iCo
20223 6c 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 45 78 l = -1;. Ex
20224 70 72 20 2a 70 45 2c 20 2a 70 44 75 70 3b 0a 20 pr *pE, *pDup;.
20225 20 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e if( pItem->
20226 64 6f 6e 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b done ) continue;
20227 0a 20 20 20 20 20 20 70 45 20 3d 20 70 49 74 65 . pE = pIte
20228 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 m->pExpr;.
20229 69 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 if( sqlite3ExprI
2022a 73 49 6e 74 65 67 65 72 28 70 45 2c 20 26 69 43 sInteger(pE, &iC
2022b 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 ol) ){. i
2022c 66 28 20 69 43 6f 6c 3c 3d 30 20 7c 7c 20 69 43 f( iCol<=0 || iC
2022d 6f 6c 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 ol>pEList->nExpr
2022e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 ){. re
2022f 73 6f 6c 76 65 4f 75 74 4f 66 52 61 6e 67 65 45 solveOutOfRangeE
20230 72 72 6f 72 28 70 50 61 72 73 65 2c 20 22 4f 52 rror(pParse, "OR
20231 44 45 52 22 2c 20 69 2b 31 2c 20 70 45 4c 69 73 DER", i+1, pELis
20232 74 2d 3e 6e 45 78 70 72 29 3b 0a 20 20 20 20 20 t->nExpr);.
20233 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 return 1;.
20234 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
20235 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 43 else{. iC
20236 6f 6c 20 3d 20 72 65 73 6f 6c 76 65 41 73 4e 61 ol = resolveAsNa
20237 6d 65 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73 me(pParse, pELis
20238 74 2c 20 70 45 29 3b 0a 20 20 20 20 20 20 20 20 t, pE);.
20239 69 66 28 20 69 43 6f 6c 3d 3d 30 20 29 7b 0a 20 if( iCol==0 ){.
2023a 20 20 20 20 20 20 20 20 20 70 44 75 70 20 3d 20 pDup =
2023b 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 sqlite3ExprDup(d
2023c 62 2c 20 70 45 2c 20 30 29 3b 0a 20 20 20 20 20 b, pE, 0);.
2023d 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 if( !db->ma
2023e 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 llocFailed ){.
2023f 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 assert
20240 28 70 44 75 70 29 3b 0a 20 20 20 20 20 20 20 20 (pDup);.
20241 20 20 20 20 69 43 6f 6c 20 3d 20 72 65 73 6f 6c iCol = resol
20242 76 65 4f 72 64 65 72 42 79 54 65 72 6d 54 6f 45 veOrderByTermToE
20243 78 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 xprList(pParse,
20244 70 53 65 6c 65 63 74 2c 20 70 44 75 70 29 3b 0a pSelect, pDup);.
20245 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
20246 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 sqlite3Exp
20247 72 44 65 6c 65 74 65 28 64 62 2c 20 70 44 75 70 rDelete(db, pDup
20248 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
20249 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69 }. if( i
2024a 43 6f 6c 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 Col>0 ){.
2024b 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 CollSeq *pColl
2024c 3d 20 70 45 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 = pE->pColl;.
2024d 20 20 20 20 20 69 6e 74 20 66 6c 61 67 73 20 3d int flags =
2024e 20 70 45 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f pE->flags & EP_
2024f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 20 20 ExpCollate;.
20250 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 sqlite3ExprD
20251 65 6c 65 74 65 28 64 62 2c 20 70 45 29 3b 0a 20 elete(db, pE);.
20252 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 70 45 pItem->pE
20253 78 70 72 20 3d 20 70 45 20 3d 20 73 71 6c 69 74 xpr = pE = sqlit
20254 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f 49 4e e3Expr(db, TK_IN
20255 54 45 47 45 52 2c 20 30 29 3b 0a 20 20 20 20 20 TEGER, 0);.
20256 20 20 20 69 66 28 20 70 45 3d 3d 30 20 29 20 72 if( pE==0 ) r
20257 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 eturn 1;.
20258 20 70 45 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f pE->pColl = pCo
20259 6c 6c 3b 0a 20 20 20 20 20 20 20 20 70 45 2d 3e ll;. pE->
2025a 66 6c 61 67 73 20 7c 3d 20 45 50 5f 49 6e 74 56 flags |= EP_IntV
2025b 61 6c 75 65 20 7c 20 66 6c 61 67 73 3b 0a 20 20 alue | flags;.
2025c 20 20 20 20 20 20 70 45 2d 3e 75 2e 69 56 61 6c pE->u.iVal
2025d 75 65 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 20 ue = iCol;.
2025e 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d pItem->iCol =
2025f 20 28 75 31 36 29 69 43 6f 6c 3b 0a 20 20 20 20 (u16)iCol;.
20260 20 20 20 20 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 pItem->done
20261 3d 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 = 1;. }else
20262 7b 0a 20 20 20 20 20 20 20 20 6d 6f 72 65 54 6f {. moreTo
20263 44 6f 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a Do = 1;. }.
20264 20 20 20 20 7d 0a 20 20 20 20 70 53 65 6c 65 63 }. pSelec
20265 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 4e 65 t = pSelect->pNe
20266 78 74 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d xt;. }. for(i=
20267 30 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 0; i<pOrderBy->n
20268 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 Expr; i++){.
20269 69 66 28 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b if( pOrderBy->a[
2026a 69 5d 2e 64 6f 6e 65 3d 3d 30 20 29 7b 0a 20 20 i].done==0 ){.
2026b 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
2026c 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 72 20 Msg(pParse, "%r
2026d 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 64 6f ORDER BY term do
2026e 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 61 6e 79 es not match any
2026f 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 ". "
20270 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72 65 column in the re
20271 73 75 6c 74 20 73 65 74 22 2c 20 69 2b 31 29 3b sult set", i+1);
20272 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b . return 1;
20273 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
20274 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn 0;.}../*.**
20275 43 68 65 63 6b 20 65 76 65 72 79 20 74 65 72 6d Check every term
20276 20 69 6e 20 74 68 65 20 4f 52 44 45 52 20 42 59 in the ORDER BY
20277 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 or GROUP BY cla
20278 75 73 65 20 70 4f 72 64 65 72 42 79 20 6f 66 0a use pOrderBy of.
20279 2a 2a 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 ** the SELECT st
2027a 61 74 65 6d 65 6e 74 20 70 53 65 6c 65 63 74 2e atement pSelect.
2027b 20 20 49 66 20 61 6e 79 20 74 65 72 6d 20 69 73 If any term is
2027c 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 0a reference to a.
2027d 2a 2a 20 72 65 73 75 6c 74 20 73 65 74 20 65 78 ** result set ex
2027e 70 72 65 73 73 69 6f 6e 20 28 61 73 20 64 65 74 pression (as det
2027f 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 45 ermined by the E
20280 78 70 72 4c 69 73 74 2e 61 2e 69 43 6f 6c 20 66 xprList.a.iCol f
20281 69 65 6c 64 29 0a 2a 2a 20 74 68 65 6e 20 63 6f ield).** then co
20282 6e 76 65 72 74 20 74 68 61 74 20 74 65 72 6d 20 nvert that term
20283 69 6e 74 6f 20 61 20 63 6f 70 79 20 6f 66 20 74 into a copy of t
20284 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 he corresponding
20285 20 72 65 73 75 6c 74 20 73 65 74 0a 2a 2a 20 63 result set.** c
20286 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 olumn..**.** If
20287 61 6e 79 20 65 72 72 6f 72 73 20 61 72 65 20 64 any errors are d
20288 65 74 65 63 74 65 64 2c 20 61 64 64 20 61 6e 20 etected, add an
20289 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 6f error message to
2028a 20 70 50 61 72 73 65 20 61 6e 64 0a 2a 2a 20 72 pParse and.** r
2028b 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 eturn non-zero.
2028c 20 52 65 74 75 72 6e 20 7a 65 72 6f 20 69 66 20 Return zero if
2028d 6e 6f 20 65 72 72 6f 72 73 20 61 72 65 20 73 65 no errors are se
2028e 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 en..*/.SQLITE_PR
2028f 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
20290 33 52 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 3ResolveOrderGro
20291 75 70 42 79 28 0a 20 20 50 61 72 73 65 20 2a 70 upBy(. Parse *p
20292 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a Parse, /*
20293 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 Parsing context
20294 2e 20 20 4c 65 61 76 65 20 65 72 72 6f 72 20 6d . Leave error m
20295 65 73 73 61 67 65 73 20 68 65 72 65 20 2a 2f 0a essages here */.
20296 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 Select *pSelec
20297 74 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 t, /* The S
20298 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 ELECT statement
20299 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 63 containing the c
2029a 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c lause */. ExprL
2029b 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 ist *pOrderBy,
2029c 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 /* The ORDER BY
2029d 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 or GROUP BY cla
2029e 75 73 65 20 74 6f 20 62 65 20 70 72 6f 63 65 73 use to be proces
2029f 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 sed */. const c
202a0 68 61 72 20 2a 7a 54 79 70 65 20 20 20 20 20 2f har *zType /
202a1 2a 20 22 4f 52 44 45 52 22 20 6f 72 20 22 47 52 * "ORDER" or "GR
202a2 4f 55 50 22 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 OUP" */.){. int
202a3 20 69 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 i;. sqlite3 *d
202a4 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a b = pParse->db;.
202a5 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 ExprList *pELi
202a6 73 74 3b 0a 20 20 73 74 72 75 63 74 20 45 78 70 st;. struct Exp
202a7 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 rList_item *pIte
202a8 6d 3b 0a 0a 20 20 69 66 28 20 70 4f 72 64 65 72 m;.. if( pOrder
202a9 42 79 3d 3d 30 20 7c 7c 20 70 50 61 72 73 65 2d By==0 || pParse-
202aa 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 >db->mallocFaile
202ab 64 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 69 d ) return 0;.#i
202ac 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c f SQLITE_MAX_COL
202ad 55 4d 4e 0a 20 20 69 66 28 20 70 4f 72 64 65 72 UMN. if( pOrder
202ae 42 79 2d 3e 6e 45 78 70 72 3e 64 62 2d 3e 61 4c By->nExpr>db->aL
202af 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 imit[SQLITE_LIMI
202b0 54 5f 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20 T_COLUMN] ){.
202b1 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
202b2 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 (pParse, "too ma
202b3 6e 79 20 74 65 72 6d 73 20 69 6e 20 25 73 20 42 ny terms in %s B
202b4 59 20 63 6c 61 75 73 65 22 2c 20 7a 54 79 70 65 Y clause", zType
202b5 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b );. return 1;
202b6 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 70 45 . }.#endif. pE
202b7 4c 69 73 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e List = pSelect->
202b8 70 45 4c 69 73 74 3b 0a 20 20 61 73 73 65 72 74 pEList;. assert
202b9 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b 20 20 ( pEList!=0 );
202ba 2f 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 /* sqlite3Select
202bb 4e 65 77 28 29 20 67 75 61 72 61 6e 74 65 65 73 New() guarantees
202bc 20 74 68 69 73 20 2a 2f 0a 20 20 66 6f 72 28 69 this */. for(i
202bd 3d 30 2c 20 70 49 74 65 6d 3d 70 4f 72 64 65 72 =0, pItem=pOrder
202be 42 79 2d 3e 61 3b 20 69 3c 70 4f 72 64 65 72 42 By->a; i<pOrderB
202bf 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 y->nExpr; i++, p
202c0 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 Item++){. if(
202c1 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 29 7b 0a pItem->iCol ){.
202c2 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d if( pItem-
202c3 3e 69 43 6f 6c 3e 70 45 4c 69 73 74 2d 3e 6e 45 >iCol>pEList->nE
202c4 78 70 72 20 29 7b 0a 20 20 20 20 20 20 20 20 72 xpr ){. r
202c5 65 73 6f 6c 76 65 4f 75 74 4f 66 52 61 6e 67 65 esolveOutOfRange
202c6 45 72 72 6f 72 28 70 50 61 72 73 65 2c 20 7a 54 Error(pParse, zT
202c7 79 70 65 2c 20 69 2b 31 2c 20 70 45 4c 69 73 74 ype, i+1, pEList
202c8 2d 3e 6e 45 78 70 72 29 3b 0a 20 20 20 20 20 20 ->nExpr);.
202c9 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 return 1;.
202ca 20 20 7d 0a 20 20 20 20 20 20 72 65 73 6f 6c 76 }. resolv
202cb 65 41 6c 69 61 73 28 70 50 61 72 73 65 2c 20 70 eAlias(pParse, p
202cc 45 4c 69 73 74 2c 20 70 49 74 65 6d 2d 3e 69 43 EList, pItem->iC
202cd 6f 6c 2d 31 2c 20 70 49 74 65 6d 2d 3e 70 45 78 ol-1, pItem->pEx
202ce 70 72 2c 20 7a 54 79 70 65 29 3b 0a 20 20 20 20 pr, zType);.
202cf 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 }. }. return 0
202d0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 4f 72 64 65 ;.}../*.** pOrde
202d1 72 42 79 20 69 73 20 61 6e 20 4f 52 44 45 52 20 rBy is an ORDER
202d2 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 BY or GROUP BY c
202d3 6c 61 75 73 65 20 69 6e 20 53 45 4c 45 43 54 20 lause in SELECT
202d4 73 74 61 74 65 6d 65 6e 74 20 70 53 65 6c 65 63 statement pSelec
202d5 74 2e 0a 2a 2a 20 54 68 65 20 4e 61 6d 65 20 63 t..** The Name c
202d6 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20 53 45 ontext of the SE
202d7 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 69 LECT statement i
202d8 73 20 70 4e 43 2e 20 20 7a 54 79 70 65 20 69 73 s pNC. zType is
202d9 20 65 69 74 68 65 72 0a 2a 2a 20 22 4f 52 44 45 either.** "ORDE
202da 52 22 20 6f 72 20 22 47 52 4f 55 50 22 20 64 65 R" or "GROUP" de
202db 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 69 63 68 pending on which
202dc 20 74 79 70 65 20 6f 66 20 63 6c 61 75 73 65 20 type of clause
202dd 70 4f 72 64 65 72 42 79 20 69 73 2e 0a 2a 2a 0a pOrderBy is..**.
202de 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
202df 72 65 73 6f 6c 76 65 73 20 65 61 63 68 20 74 65 resolves each te
202e0 72 6d 20 6f 66 20 74 68 65 20 63 6c 61 75 73 65 rm of the clause
202e1 20 69 6e 74 6f 20 61 6e 20 65 78 70 72 65 73 73 into an express
202e2 69 6f 6e 2e 0a 2a 2a 20 49 66 20 74 68 65 20 6f ion..** If the o
202e3 72 64 65 72 2d 62 79 20 74 65 72 6d 20 69 73 20 rder-by term is
202e4 61 6e 20 69 6e 74 65 67 65 72 20 49 20 62 65 74 an integer I bet
202e5 77 65 65 6e 20 31 20 61 6e 64 20 4e 20 28 77 68 ween 1 and N (wh
202e6 65 72 65 20 4e 20 69 73 20 74 68 65 0a 2a 2a 20 ere N is the.**
202e7 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e number of column
202e8 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 s in the result
202e9 73 65 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 set of the SELEC
202ea 54 29 20 74 68 65 6e 20 74 68 65 20 65 78 70 72 T) then the expr
202eb 65 73 73 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 65 ession.** in the
202ec 20 72 65 73 6f 6c 75 74 69 6f 6e 20 69 73 20 61 resolution is a
202ed 20 63 6f 70 79 20 6f 66 20 74 68 65 20 49 2d 74 copy of the I-t
202ee 68 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70 h result-set exp
202ef 72 65 73 73 69 6f 6e 2e 20 20 49 66 0a 2a 2a 20 ression. If.**
202f0 74 68 65 20 6f 72 64 65 72 2d 62 79 20 74 65 72 the order-by ter
202f1 6d 20 69 73 20 61 6e 20 69 64 65 6e 74 69 66 79 m is an identify
202f2 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 that correspond
202f3 73 20 74 6f 20 74 68 65 20 41 53 2d 6e 61 6d 65 s to the AS-name
202f4 20 6f 66 0a 2a 2a 20 61 20 72 65 73 75 6c 74 2d of.** a result-
202f5 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 set expression,
202f6 74 68 65 6e 20 74 68 65 20 74 65 72 6d 20 72 65 then the term re
202f7 73 6f 6c 76 65 73 20 74 6f 20 61 20 63 6f 70 79 solves to a copy
202f8 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c of the.** resul
202f9 74 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e t-set expression
202fa 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 . Otherwise, th
202fb 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 e expression is
202fc 72 65 73 6f 6c 76 65 64 20 69 6e 0a 2a 2a 20 74 resolved in.** t
202fd 68 65 20 75 73 75 61 6c 20 77 61 79 20 2d 20 75 he usual way - u
202fe 73 69 6e 67 20 73 71 6c 69 74 65 33 52 65 73 6f sing sqlite3Reso
202ff 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 29 2e 0a lveExprNames()..
20300 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
20301 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e ne returns the n
20302 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 2e umber of errors.
20303 20 20 49 66 20 65 72 72 6f 72 73 20 6f 63 63 75 If errors occu
20304 72 2c 20 74 68 65 6e 0a 2a 2a 20 61 6e 20 61 70 r, then.** an ap
20305 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20 propriate error
20306 6d 65 73 73 61 67 65 20 6d 69 67 68 74 20 62 65 message might be
20307 20 6c 65 66 74 20 69 6e 20 70 50 61 72 73 65 2e left in pParse.
20308 20 20 28 4f 4f 4d 20 65 72 72 6f 72 73 0a 2a 2a (OOM errors.**
20309 20 65 78 63 65 70 74 65 64 2e 29 0a 2a 2f 0a 73 excepted.).*/.s
2030a 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 tatic int resolv
2030b 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28 0a 20 eOrderGroupBy(.
2030c 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e NameContext *pN
2030d 43 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 61 C, /* The na
2030e 6d 65 20 63 6f 6e 74 65 78 74 20 6f 66 20 74 68 me context of th
2030f 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 e SELECT stateme
20310 6e 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a nt */. Select *
20311 70 53 65 6c 65 63 74 2c 20 20 20 20 20 20 2f 2a pSelect, /*
20312 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 The SELECT stat
20313 65 6d 65 6e 74 20 68 6f 6c 64 69 6e 67 20 70 4f ement holding pO
20314 72 64 65 72 42 79 20 2a 2f 0a 20 20 45 78 70 72 rderBy */. Expr
20315 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 List *pOrderBy,
20316 20 20 2f 2a 20 41 6e 20 4f 52 44 45 52 20 42 59 /* An ORDER BY
20317 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 or GROUP BY cla
20318 75 73 65 20 74 6f 20 72 65 73 6f 6c 76 65 20 2a use to resolve *
20319 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
2031a 7a 54 79 70 65 20 20 20 20 20 2f 2a 20 45 69 74 zType /* Eit
2031b 68 65 72 20 22 4f 52 44 45 52 22 20 6f 72 20 22 her "ORDER" or "
2031c 47 52 4f 55 50 22 2c 20 61 73 20 61 70 70 72 6f GROUP", as appro
2031d 70 72 69 61 74 65 20 2a 2f 0a 29 7b 0a 20 20 69 priate */.){. i
2031e 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 nt i;
2031f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
20320 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f Loop counter */
20321 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 20 20 20 20 . int iCol;
20322 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20323 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 /* Column numb
20324 65 72 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 45 er */. struct E
20325 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 xprList_item *pI
20326 74 65 6d 3b 20 20 20 2f 2a 20 41 20 74 65 72 6d tem; /* A term
20327 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 of the ORDER BY
20328 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 50 61 72 clause */. Par
20329 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 se *pParse;
2032a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
2032b 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a arsing context *
2032c 2f 0a 20 20 69 6e 74 20 6e 52 65 73 75 6c 74 3b /. int nResult;
2032d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2032e 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
2032f 74 65 72 6d 73 20 69 6e 20 74 68 65 20 72 65 73 terms in the res
20330 75 6c 74 20 73 65 74 20 2a 2f 0a 0a 20 20 69 66 ult set */.. if
20331 28 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 20 ( pOrderBy==0 )
20332 72 65 74 75 72 6e 20 30 3b 0a 20 20 6e 52 65 73 return 0;. nRes
20333 75 6c 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 ult = pSelect->p
20334 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 EList->nExpr;.
20335 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50 pParse = pNC->pP
20336 61 72 73 65 3b 0a 20 20 66 6f 72 28 69 3d 30 2c arse;. for(i=0,
20337 20 70 49 74 65 6d 3d 70 4f 72 64 65 72 42 79 2d pItem=pOrderBy-
20338 3e 61 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e >a; i<pOrderBy->
20339 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 nExpr; i++, pIte
2033a 6d 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a m++){. Expr *
2033b 70 45 20 3d 20 70 49 74 65 6d 2d 3e 70 45 78 70 pE = pItem->pExp
2033c 72 3b 0a 20 20 20 20 69 43 6f 6c 20 3d 20 72 65 r;. iCol = re
2033d 73 6f 6c 76 65 41 73 4e 61 6d 65 28 70 50 61 72 solveAsName(pPar
2033e 73 65 2c 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c se, pSelect->pEL
2033f 69 73 74 2c 20 70 45 29 3b 0a 20 20 20 20 69 66 ist, pE);. if
20340 28 20 69 43 6f 6c 3e 30 20 29 7b 0a 20 20 20 20 ( iCol>0 ){.
20341 20 20 2f 2a 20 49 66 20 61 6e 20 41 53 2d 6e 61 /* If an AS-na
20342 6d 65 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e me match is foun
20343 64 2c 20 6d 61 72 6b 20 74 68 69 73 20 4f 52 44 d, mark this ORD
20344 45 52 20 42 59 20 63 6f 6c 75 6d 6e 20 61 73 20 ER BY column as
20345 62 65 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 61 being. ** a
20346 20 63 6f 70 79 20 6f 66 20 74 68 65 20 69 43 6f copy of the iCo
20347 6c 2d 74 68 20 72 65 73 75 6c 74 2d 73 65 74 20 l-th result-set
20348 63 6f 6c 75 6d 6e 2e 20 20 54 68 65 20 73 75 62 column. The sub
20349 73 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 6f 0a sequent call to.
2034a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 ** sqlite3
2034b 52 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 ResolveOrderGrou
2034c 70 42 79 28 29 20 77 69 6c 6c 20 63 6f 6e 76 65 pBy() will conve
2034d 72 74 20 74 68 65 20 65 78 70 72 65 73 73 69 6f rt the expressio
2034e 6e 20 74 6f 20 61 0a 20 20 20 20 20 20 2a 2a 20 n to a. **
2034f 63 6f 70 79 20 6f 66 20 74 68 65 20 69 43 6f 6c copy of the iCol
20350 2d 74 68 20 72 65 73 75 6c 74 2d 73 65 74 20 65 -th result-set e
20351 78 70 72 65 73 73 69 6f 6e 2e 20 2a 2f 0a 20 20 xpression. */.
20352 20 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 pItem->iCol
20353 3d 20 28 75 31 36 29 69 43 6f 6c 3b 0a 20 20 20 = (u16)iCol;.
20354 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 continue;.
20355 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 }. if( sqlit
20356 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 e3ExprIsInteger(
20357 70 45 2c 20 26 69 43 6f 6c 29 20 29 7b 0a 20 20 pE, &iCol) ){.
20358 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 /* The ORDER
20359 20 42 59 20 74 65 72 6d 20 69 73 20 61 6e 20 69 BY term is an i
2035a 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 2e nteger constant.
2035b 20 20 41 67 61 69 6e 2c 20 73 65 74 20 74 68 65 Again, set the
2035c 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 2a 2a column. **
2035d 20 6e 75 6d 62 65 72 20 73 6f 20 74 68 61 74 20 number so that
2035e 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 sqlite3ResolveOr
2035f 64 65 72 47 72 6f 75 70 42 79 28 29 20 77 69 6c derGroupBy() wil
20360 6c 20 63 6f 6e 76 65 72 74 20 74 68 65 0a 20 20 l convert the.
20361 20 20 20 20 2a 2a 20 6f 72 64 65 72 2d 62 79 20 ** order-by
20362 74 65 72 6d 20 74 6f 20 61 20 63 6f 70 79 20 6f term to a copy o
20363 66 20 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74 f the result-set
20364 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 expression */.
20365 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 31 20 if( iCol<1
20366 29 7b 0a 20 20 20 20 20 20 20 20 72 65 73 6f 6c ){. resol
20367 76 65 4f 75 74 4f 66 52 61 6e 67 65 45 72 72 6f veOutOfRangeErro
20368 72 28 70 50 61 72 73 65 2c 20 7a 54 79 70 65 2c r(pParse, zType,
20369 20 69 2b 31 2c 20 6e 52 65 73 75 6c 74 29 3b 0a i+1, nResult);.
2036a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 return 1
2036b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
2036c 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 28 75 pItem->iCol = (u
2036d 31 36 29 69 43 6f 6c 3b 0a 20 20 20 20 20 20 63 16)iCol;. c
2036e 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 0a ontinue;. }..
2036f 20 20 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 /* Otherwise
20370 2c 20 74 72 65 61 74 20 74 68 65 20 4f 52 44 45 , treat the ORDE
20371 52 20 42 59 20 74 65 72 6d 20 61 73 20 61 6e 20 R BY term as an
20372 6f 72 64 69 6e 61 72 79 20 65 78 70 72 65 73 73 ordinary express
20373 69 6f 6e 20 2a 2f 0a 20 20 20 20 70 49 74 65 6d ion */. pItem
20374 2d 3e 69 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 ->iCol = 0;.
20375 69 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c if( sqlite3Resol
20376 76 65 45 78 70 72 4e 61 6d 65 73 28 70 4e 43 2c veExprNames(pNC,
20377 20 70 45 29 20 29 7b 0a 20 20 20 20 20 20 72 65 pE) ){. re
20378 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 turn 1;. }.
20379 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 }. return sqlit
2037a 65 33 52 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 e3ResolveOrderGr
2037b 6f 75 70 42 79 28 70 50 61 72 73 65 2c 20 70 53 oupBy(pParse, pS
2037c 65 6c 65 63 74 2c 20 70 4f 72 64 65 72 42 79 2c elect, pOrderBy,
2037d 20 7a 54 79 70 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a zType);.}../*.*
2037e 2a 20 52 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 * Resolve names
2037f 69 6e 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 in the SELECT st
20380 61 74 65 6d 65 6e 74 20 70 20 61 6e 64 20 61 6c atement p and al
20381 6c 20 6f 66 20 69 74 73 20 64 65 73 63 65 6e 64 l of its descend
20382 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ents..*/.static
20383 69 6e 74 20 72 65 73 6f 6c 76 65 53 65 6c 65 63 int resolveSelec
20384 74 53 74 65 70 28 57 61 6c 6b 65 72 20 2a 70 57 tStep(Walker *pW
20385 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 alker, Select *p
20386 29 7b 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 ){. NameContext
20387 20 2a 70 4f 75 74 65 72 4e 43 3b 20 20 2f 2a 20 *pOuterNC; /*
20388 43 6f 6e 74 65 78 74 20 74 68 61 74 20 63 6f 6e Context that con
20389 74 61 69 6e 73 20 74 68 69 73 20 53 45 4c 45 43 tains this SELEC
2038a 54 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 T */. NameConte
2038b 78 74 20 73 4e 43 3b 20 20 20 20 20 20 20 20 2f xt sNC; /
2038c 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 6f * Name context o
2038d 66 20 74 68 69 73 20 53 45 4c 45 43 54 20 2a 2f f this SELECT */
2038e 0a 20 20 69 6e 74 20 69 73 43 6f 6d 70 6f 75 6e . int isCompoun
2038f 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 d; /* Tr
20390 75 65 20 69 66 20 70 20 69 73 20 61 20 63 6f 6d ue if p is a com
20391 70 6f 75 6e 64 20 73 65 6c 65 63 74 20 2a 2f 0a pound select */.
20392 20 20 69 6e 74 20 6e 43 6f 6d 70 6f 75 6e 64 3b int nCompound;
20393 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
20394 62 65 72 20 6f 66 20 63 6f 6d 70 6f 75 6e 64 20 ber of compound
20395 74 65 72 6d 73 20 70 72 6f 63 65 73 73 65 64 20 terms processed
20396 73 6f 20 66 61 72 20 2a 2f 0a 20 20 50 61 72 73 so far */. Pars
20397 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 e *pParse;
20398 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 /* Parsing c
20399 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 ontext */. Expr
2039a 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 20 List *pEList;
2039b 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 73 65 /* Result se
2039c 74 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 t expression lis
2039d 74 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 t */. int i;
2039e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
2039f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a * Loop counter *
203a0 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 47 /. ExprList *pG
203a1 72 6f 75 70 42 79 3b 20 20 20 20 20 2f 2a 20 54 roupBy; /* T
203a2 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 he GROUP BY clau
203a3 73 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a se */. Select *
203a4 70 4c 65 66 74 6d 6f 73 74 3b 20 20 20 20 20 20 pLeftmost;
203a5 2f 2a 20 4c 65 66 74 2d 6d 6f 73 74 20 6f 66 20 /* Left-most of
203a6 53 45 4c 45 43 54 20 6f 66 20 61 20 63 6f 6d 70 SELECT of a comp
203a7 6f 75 6e 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 ound */. sqlite
203a8 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 3 *db;
203a9 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f /* Database co
203aa 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 0a 0a nnection */. ..
203ab 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 assert( p!=0 )
203ac 3b 0a 20 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c ;. if( p->selFl
203ad 61 67 73 20 26 20 53 46 5f 52 65 73 6f 6c 76 65 ags & SF_Resolve
203ae 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 d ){. return
203af 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a 20 WRC_Prune;. }.
203b0 20 70 4f 75 74 65 72 4e 43 20 3d 20 70 57 61 6c pOuterNC = pWal
203b1 6b 65 72 2d 3e 75 2e 70 4e 43 3b 0a 20 20 70 50 ker->u.pNC;. pP
203b2 61 72 73 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e arse = pWalker->
203b3 70 50 61 72 73 65 3b 0a 20 20 64 62 20 3d 20 70 pParse;. db = p
203b4 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 2f 2a Parse->db;.. /*
203b5 20 4e 6f 72 6d 61 6c 6c 79 20 73 71 6c 69 74 65 Normally sqlite
203b6 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28 29 20 3SelectExpand()
203b7 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 66 will be called f
203b8 69 72 73 74 20 61 6e 64 20 77 69 6c 6c 20 68 61 irst and will ha
203b9 76 65 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 ve. ** already
203ba 65 78 70 61 6e 64 65 64 20 74 68 69 73 20 53 45 expanded this SE
203bb 4c 45 43 54 2e 20 20 48 6f 77 65 76 65 72 2c 20 LECT. However,
203bc 69 66 20 74 68 69 73 20 69 73 20 61 20 73 75 62 if this is a sub
203bd 71 75 65 72 79 20 77 69 74 68 69 6e 0a 20 20 2a query within. *
203be 2a 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 2c * an expression,
203bf 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 sqlite3ResolveE
203c0 78 70 72 4e 61 6d 65 73 28 29 20 77 69 6c 6c 20 xprNames() will
203c1 62 65 20 63 61 6c 6c 65 64 20 77 69 74 68 6f 75 be called withou
203c2 74 20 61 0a 20 20 2a 2a 20 70 72 69 6f 72 20 63 t a. ** prior c
203c3 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 53 65 all to sqlite3Se
203c4 6c 65 63 74 45 78 70 61 6e 64 28 29 2e 20 20 57 lectExpand(). W
203c5 68 65 6e 20 74 68 61 74 20 68 61 70 70 65 6e 73 hen that happens
203c6 2c 20 6c 65 74 0a 20 20 2a 2a 20 73 71 6c 69 74 , let. ** sqlit
203c7 65 33 53 65 6c 65 63 74 50 72 65 70 28 29 20 64 e3SelectPrep() d
203c8 6f 20 61 6c 6c 20 6f 66 20 74 68 65 20 70 72 6f o all of the pro
203c9 63 65 73 73 69 6e 67 20 66 6f 72 20 74 68 69 73 cessing for this
203ca 20 53 45 4c 45 43 54 2e 0a 20 20 2a 2a 20 73 71 SELECT.. ** sq
203cb 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65 70 28 lite3SelectPrep(
203cc 29 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 62 6f ) will invoke bo
203cd 74 68 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 th sqlite3Select
203ce 45 78 70 61 6e 64 28 29 20 61 6e 64 0a 20 20 2a Expand() and. *
203cf 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * this routine i
203d0 6e 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f 72 n the correct or
203d1 64 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 der.. */. if(
203d2 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 (p->selFlags & S
203d3 46 5f 45 78 70 61 6e 64 65 64 29 3d 3d 30 20 29 F_Expanded)==0 )
203d4 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c {. sqlite3Sel
203d5 65 63 74 50 72 65 70 28 70 50 61 72 73 65 2c 20 ectPrep(pParse,
203d6 70 2c 20 70 4f 75 74 65 72 4e 43 29 3b 0a 20 20 p, pOuterNC);.
203d7 20 20 72 65 74 75 72 6e 20 28 70 50 61 72 73 65 return (pParse
203d8 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 ->nErr || db->ma
203d9 6c 6c 6f 63 46 61 69 6c 65 64 29 20 3f 20 57 52 llocFailed) ? WR
203da 43 5f 41 62 6f 72 74 20 3a 20 57 52 43 5f 50 72 C_Abort : WRC_Pr
203db 75 6e 65 3b 0a 20 20 7d 0a 0a 20 20 69 73 43 6f une;. }.. isCo
203dc 6d 70 6f 75 6e 64 20 3d 20 70 2d 3e 70 50 72 69 mpound = p->pPri
203dd 6f 72 21 3d 30 3b 0a 20 20 6e 43 6f 6d 70 6f 75 or!=0;. nCompou
203de 6e 64 20 3d 20 30 3b 0a 20 20 70 4c 65 66 74 6d nd = 0;. pLeftm
203df 6f 73 74 20 3d 20 70 3b 0a 20 20 77 68 69 6c 65 ost = p;. while
203e0 28 20 70 20 29 7b 0a 20 20 20 20 61 73 73 65 72 ( p ){. asser
203e1 74 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 t( (p->selFlags
203e2 26 20 53 46 5f 45 78 70 61 6e 64 65 64 29 21 3d & SF_Expanded)!=
203e3 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 0 );. assert(
203e4 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 (p->selFlags &
203e5 53 46 5f 52 65 73 6f 6c 76 65 64 29 3d 3d 30 20 SF_Resolved)==0
203e6 29 3b 0a 20 20 20 20 70 2d 3e 73 65 6c 46 6c 61 );. p->selFla
203e7 67 73 20 7c 3d 20 53 46 5f 52 65 73 6f 6c 76 65 gs |= SF_Resolve
203e8 64 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c d;.. /* Resol
203e9 76 65 20 74 68 65 20 65 78 70 72 65 73 73 69 6f ve the expressio
203ea 6e 73 20 69 6e 20 74 68 65 20 4c 49 4d 49 54 20 ns in the LIMIT
203eb 61 6e 64 20 4f 46 46 53 45 54 20 63 6c 61 75 73 and OFFSET claus
203ec 65 73 2e 20 54 68 65 73 65 0a 20 20 20 20 2a 2a es. These. **
203ed 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 are not allowed
203ee 20 74 6f 20 72 65 66 65 72 20 74 6f 20 61 6e 79 to refer to any
203ef 20 6e 61 6d 65 73 2c 20 73 6f 20 70 61 73 73 20 names, so pass
203f0 61 6e 20 65 6d 70 74 79 20 4e 61 6d 65 43 6f 6e an empty NameCon
203f1 74 65 78 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 text.. */.
203f2 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c memset(&sNC, 0,
203f3 20 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20 sizeof(sNC));.
203f4 20 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 sNC.pParse =
203f5 70 50 61 72 73 65 3b 0a 20 20 20 20 69 66 28 20 pParse;. if(
203f6 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 sqlite3ResolveEx
203f7 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d prNames(&sNC, p-
203f8 3e 70 4c 69 6d 69 74 29 20 7c 7c 0a 20 20 20 20 >pLimit) ||.
203f9 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 6f 6c sqlite3Resol
203fa 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 veExprNames(&sNC
203fb 2c 20 70 2d 3e 70 4f 66 66 73 65 74 29 20 29 7b , p->pOffset) ){
203fc 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 . return WR
203fd 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 C_Abort;. }.
203fe 20 0a 20 20 20 20 2f 2a 20 53 65 74 20 75 70 20 . /* Set up
203ff 74 68 65 20 6c 6f 63 61 6c 20 6e 61 6d 65 2d 63 the local name-c
20400 6f 6e 74 65 78 74 20 74 6f 20 70 61 73 73 20 74 ontext to pass t
20401 6f 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 o sqlite3Resolve
20402 45 78 70 72 4e 61 6d 65 73 28 29 20 74 6f 0a 20 ExprNames() to.
20403 20 20 20 2a 2a 20 72 65 73 6f 6c 76 65 20 74 68 ** resolve th
20404 65 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70 e result-set exp
20405 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 20 20 ression list..
20406 20 20 2a 2f 0a 20 20 20 20 73 4e 43 2e 61 6c 6c */. sNC.all
20407 6f 77 41 67 67 20 3d 20 31 3b 0a 20 20 20 20 73 owAgg = 1;. s
20408 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 2d NC.pSrcList = p-
20409 3e 70 53 72 63 3b 0a 20 20 20 20 73 4e 43 2e 70 >pSrc;. sNC.p
2040a 4e 65 78 74 20 3d 20 70 4f 75 74 65 72 4e 43 3b Next = pOuterNC;
2040b 0a 20 20 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c . . /* Resol
2040c 76 65 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 ve names in the
2040d 72 65 73 75 6c 74 20 73 65 74 2e 20 2a 2f 0a 20 result set. */.
2040e 20 20 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70 pEList = p->p
2040f 45 4c 69 73 74 3b 0a 20 20 20 20 61 73 73 65 72 EList;. asser
20410 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b 0a t( pEList!=0 );.
20411 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 for(i=0; i<p
20412 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b EList->nExpr; i+
20413 2b 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a +){. Expr *
20414 70 58 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 pX = pEList->a[i
20415 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 69 ].pExpr;. i
20416 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 f( sqlite3Resolv
20417 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c eExprNames(&sNC,
20418 20 70 58 29 20 29 7b 0a 20 20 20 20 20 20 20 20 pX) ){.
20419 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 return WRC_Abort
2041a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
2041b 20 20 0a 20 20 20 20 2f 2a 20 52 65 63 75 72 73 . /* Recurs
2041c 69 76 65 6c 79 20 72 65 73 6f 6c 76 65 20 6e 61 ively resolve na
2041d 6d 65 73 20 69 6e 20 61 6c 6c 20 73 75 62 71 75 mes in all subqu
2041e 65 72 69 65 73 0a 20 20 20 20 2a 2f 0a 20 20 20 eries. */.
2041f 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 70 for(i=0; i<p->p
20420 53 72 63 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b Src->nSrc; i++){
20421 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 53 72 . struct Sr
20422 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 cList_item *pIte
20423 6d 20 3d 20 26 70 2d 3e 70 53 72 63 2d 3e 61 5b m = &p->pSrc->a[
20424 69 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 i];. if( pI
20425 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a tem->pSelect ){.
20426 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 const ch
20427 61 72 20 2a 7a 53 61 76 65 64 43 6f 6e 74 65 78 ar *zSavedContex
20428 74 20 3d 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 t = pParse->zAut
20429 68 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 20 20 hContext;.
2042a 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 4e 61 if( pItem->zNa
2042b 6d 65 20 29 20 70 50 61 72 73 65 2d 3e 7a 41 75 me ) pParse->zAu
2042c 74 68 43 6f 6e 74 65 78 74 20 3d 20 70 49 74 65 thContext = pIte
2042d 6d 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 m->zName;.
2042e 20 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 sqlite3Resolve
2042f 53 65 6c 65 63 74 4e 61 6d 65 73 28 70 50 61 72 SelectNames(pPar
20430 73 65 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 se, pItem->pSele
20431 63 74 2c 20 70 4f 75 74 65 72 4e 43 29 3b 0a 20 ct, pOuterNC);.
20432 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 7a pParse->z
20433 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 7a 53 AuthContext = zS
20434 61 76 65 64 43 6f 6e 74 65 78 74 3b 0a 20 20 20 avedContext;.
20435 20 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d if( pParse-
20436 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c >nErr || db->mal
20437 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 locFailed ) retu
20438 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 rn WRC_Abort;.
20439 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 }. }. .
2043a 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 /* If there a
2043b 72 65 20 6e 6f 20 61 67 67 72 65 67 61 74 65 20 re no aggregate
2043c 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 74 68 65 functions in the
2043d 20 72 65 73 75 6c 74 2d 73 65 74 2c 20 61 6e 64 result-set, and
2043e 20 6e 6f 20 47 52 4f 55 50 20 42 59 20 0a 20 20 no GROUP BY .
2043f 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 2c ** expression,
20440 20 64 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 61 67 do not allow ag
20441 67 72 65 67 61 74 65 73 20 69 6e 20 61 6e 79 20 gregates in any
20442 6f 66 20 74 68 65 20 6f 74 68 65 72 20 65 78 70 of the other exp
20443 72 65 73 73 69 6f 6e 73 2e 0a 20 20 20 20 2a 2f ressions.. */
20444 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d . assert( (p-
20445 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 41 >selFlags & SF_A
20446 67 67 72 65 67 61 74 65 29 3d 3d 30 20 29 3b 0a ggregate)==0 );.
20447 20 20 20 20 70 47 72 6f 75 70 42 79 20 3d 20 70 pGroupBy = p
20448 2d 3e 70 47 72 6f 75 70 42 79 3b 0a 20 20 20 20 ->pGroupBy;.
20449 69 66 28 20 70 47 72 6f 75 70 42 79 20 7c 7c 20 if( pGroupBy ||
2044a 73 4e 43 2e 68 61 73 41 67 67 20 29 7b 0a 20 20 sNC.hasAgg ){.
2044b 20 20 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 p->selFlags
2044c 7c 3d 20 53 46 5f 41 67 67 72 65 67 61 74 65 3b |= SF_Aggregate;
2044d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
2044e 20 20 73 4e 43 2e 61 6c 6c 6f 77 41 67 67 20 3d sNC.allowAgg =
2044f 20 30 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 0;. }. .
20450 20 2f 2a 20 49 66 20 61 20 48 41 56 49 4e 47 20 /* If a HAVING
20451 63 6c 61 75 73 65 20 69 73 20 70 72 65 73 65 6e clause is presen
20452 74 2c 20 74 68 65 6e 20 74 68 65 72 65 20 6d 75 t, then there mu
20453 73 74 20 62 65 20 61 20 47 52 4f 55 50 20 42 59 st be a GROUP BY
20454 20 63 6c 61 75 73 65 2e 0a 20 20 20 20 2a 2f 0a clause.. */.
20455 20 20 20 20 69 66 28 20 70 2d 3e 70 48 61 76 69 if( p->pHavi
20456 6e 67 20 26 26 20 21 70 47 72 6f 75 70 42 79 20 ng && !pGroupBy
20457 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
20458 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
20459 20 22 61 20 47 52 4f 55 50 20 42 59 20 63 6c 61 "a GROUP BY cla
2045a 75 73 65 20 69 73 20 72 65 71 75 69 72 65 64 20 use is required
2045b 62 65 66 6f 72 65 20 48 41 56 49 4e 47 22 29 3b before HAVING");
2045c 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 . return WR
2045d 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 C_Abort;. }.
2045e 20 0a 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 . /* Add the
2045f 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 expression list
20460 20 74 6f 20 74 68 65 20 6e 61 6d 65 2d 63 6f 6e to the name-con
20461 74 65 78 74 20 62 65 66 6f 72 65 20 70 61 72 73 text before pars
20462 69 6e 67 20 74 68 65 0a 20 20 20 20 2a 2a 20 6f ing the. ** o
20463 74 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e 73 ther expressions
20464 20 69 6e 20 74 68 65 20 53 45 4c 45 43 54 20 73 in the SELECT s
20465 74 61 74 65 6d 65 6e 74 2e 20 54 68 69 73 20 69 tatement. This i
20466 73 20 73 6f 20 74 68 61 74 0a 20 20 20 20 2a 2a s so that. **
20467 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 expressions in
20468 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 the WHERE clause
20469 20 28 65 74 63 2e 29 20 63 61 6e 20 72 65 66 65 (etc.) can refe
2046a 72 20 74 6f 20 65 78 70 72 65 73 73 69 6f 6e 73 r to expressions
2046b 20 62 79 0a 20 20 20 20 2a 2a 20 61 6c 69 61 73 by. ** alias
2046c 65 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 es in the result
2046d 20 73 65 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 set.. **.
2046e 20 2a 2a 20 4d 69 6e 6f 72 20 70 6f 69 6e 74 3a ** Minor point:
2046f 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 If this is the
20470 63 61 73 65 2c 20 74 68 65 6e 20 74 68 65 20 65 case, then the e
20471 78 70 72 65 73 73 69 6f 6e 20 77 69 6c 6c 20 62 xpression will b
20472 65 0a 20 20 20 20 2a 2a 20 72 65 2d 65 76 61 6c e. ** re-eval
20473 75 61 74 65 64 20 66 6f 72 20 65 61 63 68 20 72 uated for each r
20474 65 66 65 72 65 6e 63 65 20 74 6f 20 69 74 2e 0a eference to it..
20475 20 20 20 20 2a 2f 0a 20 20 20 20 73 4e 43 2e 70 */. sNC.p
20476 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73 EList = p->pELis
20477 74 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 t;. if( sqlit
20478 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d e3ResolveExprNam
20479 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70 57 68 65 es(&sNC, p->pWhe
2047a 72 65 29 20 7c 7c 0a 20 20 20 20 20 20 20 73 71 re) ||. sq
2047b 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 lite3ResolveExpr
2047c 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70 Names(&sNC, p->p
2047d 48 61 76 69 6e 67 29 0a 20 20 20 20 29 7b 0a 20 Having). ){.
2047e 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f return WRC_
2047f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 Abort;. }..
20480 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 /* The ORDER B
20481 59 20 61 6e 64 20 47 52 4f 55 50 20 42 59 20 63 Y and GROUP BY c
20482 6c 61 75 73 65 73 20 6d 61 79 20 6e 6f 74 20 72 lauses may not r
20483 65 66 65 72 20 74 6f 20 74 65 72 6d 73 20 69 6e efer to terms in
20484 0a 20 20 20 20 2a 2a 20 6f 75 74 65 72 20 71 75 . ** outer qu
20485 65 72 69 65 73 20 0a 20 20 20 20 2a 2f 0a 20 20 eries . */.
20486 20 20 73 4e 43 2e 70 4e 65 78 74 20 3d 20 30 3b sNC.pNext = 0;
20487 0a 20 20 20 20 73 4e 43 2e 61 6c 6c 6f 77 41 67 . sNC.allowAg
20488 67 20 3d 20 31 3b 0a 0a 20 20 20 20 2f 2a 20 50 g = 1;.. /* P
20489 72 6f 63 65 73 73 20 74 68 65 20 4f 52 44 45 52 rocess the ORDER
2048a 20 42 59 20 63 6c 61 75 73 65 20 66 6f 72 20 73 BY clause for s
2048b 69 6e 67 6c 65 74 6f 6e 20 53 45 4c 45 43 54 20 ingleton SELECT
2048c 73 74 61 74 65 6d 65 6e 74 73 2e 0a 20 20 20 20 statements..
2048d 2a 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 ** The ORDER BY
2048e 63 6c 61 75 73 65 20 66 6f 72 20 63 6f 6d 70 6f clause for compo
2048f 75 6e 64 73 20 53 45 4c 45 43 54 20 73 74 61 74 unds SELECT stat
20490 65 6d 65 6e 74 73 20 69 73 20 68 61 6e 64 6c 65 ements is handle
20491 64 0a 20 20 20 20 2a 2a 20 62 65 6c 6f 77 2c 20 d. ** below,
20492 61 66 74 65 72 20 61 6c 6c 20 6f 66 20 74 68 65 after all of the
20493 20 72 65 73 75 6c 74 2d 73 65 74 73 20 66 6f 72 result-sets for
20494 20 61 6c 6c 20 6f 66 20 74 68 65 20 65 6c 65 6d all of the elem
20495 65 6e 74 73 20 6f 66 0a 20 20 20 20 2a 2a 20 74 ents of. ** t
20496 68 65 20 63 6f 6d 70 6f 75 6e 64 20 68 61 76 65 he compound have
20497 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 2e 0a been resolved..
20498 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 */. if( !
20499 69 73 43 6f 6d 70 6f 75 6e 64 20 26 26 20 72 65 isCompound && re
2049a 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 solveOrderGroupB
2049b 79 28 26 73 4e 43 2c 20 70 2c 20 70 2d 3e 70 4f y(&sNC, p, p->pO
2049c 72 64 65 72 42 79 2c 20 22 4f 52 44 45 52 22 29 rderBy, "ORDER")
2049d 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
2049e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 WRC_Abort;.
2049f 7d 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 }. if( db->ma
204a0 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 llocFailed ){.
204a1 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 return WRC_A
204a2 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 bort;. }. .
204a3 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 /* Resolve th
204a4 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 e GROUP BY claus
204a5 65 2e 20 20 41 74 20 74 68 65 20 73 61 6d 65 20 e. At the same
204a6 74 69 6d 65 2c 20 6d 61 6b 65 20 73 75 72 65 20 time, make sure
204a7 0a 20 20 20 20 2a 2a 20 74 68 65 20 47 52 4f 55 . ** the GROU
204a8 50 20 42 59 20 63 6c 61 75 73 65 20 64 6f 65 73 P BY clause does
204a9 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 67 67 not contain agg
204aa 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 regate functions
204ab 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
204ac 20 70 47 72 6f 75 70 42 79 20 29 7b 0a 20 20 20 pGroupBy ){.
204ad 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 struct ExprLi
204ae 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a st_item *pItem;.
204af 20 20 20 20 0a 20 20 20 20 20 20 69 66 28 20 72 . if( r
204b0 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 esolveOrderGroup
204b1 42 79 28 26 73 4e 43 2c 20 70 2c 20 70 47 72 6f By(&sNC, p, pGro
204b2 75 70 42 79 2c 20 22 47 52 4f 55 50 22 29 20 7c upBy, "GROUP") |
204b3 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c | db->mallocFail
204b4 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 ed ){. re
204b5 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a turn WRC_Abort;.
204b6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f }. fo
204b7 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 47 72 r(i=0, pItem=pGr
204b8 6f 75 70 42 79 2d 3e 61 3b 20 69 3c 70 47 72 6f oupBy->a; i<pGro
204b9 75 70 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b upBy->nExpr; i++
204ba 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 , pItem++){.
204bb 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73 50 if( ExprHasP
204bc 72 6f 70 65 72 74 79 28 70 49 74 65 6d 2d 3e 70 roperty(pItem->p
204bd 45 78 70 72 2c 20 45 50 5f 41 67 67 29 20 29 7b Expr, EP_Agg) ){
204be 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
204bf 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
204c0 65 2c 20 22 61 67 67 72 65 67 61 74 65 20 66 75 e, "aggregate fu
204c1 6e 63 74 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20 nctions are not
204c2 61 6c 6c 6f 77 65 64 20 69 6e 20 22 0a 20 20 20 allowed in ".
204c3 20 20 20 20 20 20 20 20 20 20 20 22 74 68 65 20 "the
204c4 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 22 GROUP BY clause"
204c5 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 );. ret
204c6 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 urn WRC_Abort;.
204c7 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
204c8 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 . }.. /* A
204c9 64 76 61 6e 63 65 20 74 6f 20 74 68 65 20 6e 65 dvance to the ne
204ca 78 74 20 74 65 72 6d 20 6f 66 20 74 68 65 20 63 xt term of the c
204cb 6f 6d 70 6f 75 6e 64 0a 20 20 20 20 2a 2f 0a 20 ompound. */.
204cc 20 20 20 70 20 3d 20 70 2d 3e 70 50 72 69 6f 72 p = p->pPrior
204cd 3b 0a 20 20 20 20 6e 43 6f 6d 70 6f 75 6e 64 2b ;. nCompound+
204ce 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 73 +;. }.. /* Res
204cf 6f 6c 76 65 20 74 68 65 20 4f 52 44 45 52 20 42 olve the ORDER B
204d0 59 20 6f 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 Y on a compound
204d1 53 45 4c 45 43 54 20 61 66 74 65 72 20 61 6c 6c SELECT after all
204d2 20 74 65 72 6d 73 20 6f 66 0a 20 20 2a 2a 20 74 terms of. ** t
204d3 68 65 20 63 6f 6d 70 6f 75 6e 64 20 68 61 76 65 he compound have
204d4 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 2e 0a been resolved..
204d5 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 43 6f 6d */. if( isCom
204d6 70 6f 75 6e 64 20 26 26 20 72 65 73 6f 6c 76 65 pound && resolve
204d7 43 6f 6d 70 6f 75 6e 64 4f 72 64 65 72 42 79 28 CompoundOrderBy(
204d8 70 50 61 72 73 65 2c 20 70 4c 65 66 74 6d 6f 73 pParse, pLeftmos
204d9 74 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e t) ){. return
204da 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a WRC_Abort;. }.
204db 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 . return WRC_Pr
204dc 75 6e 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 une;.}../*.** Th
204dd 69 73 20 72 6f 75 74 69 6e 65 20 77 61 6c 6b 73 is routine walks
204de 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 an expression t
204df 72 65 65 20 61 6e 64 20 72 65 73 6f 6c 76 65 73 ree and resolves
204e0 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 0a 2a references to.*
204e1 2a 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 73 20 * table columns
204e2 61 6e 64 20 72 65 73 75 6c 74 2d 73 65 74 20 63 and result-set c
204e3 6f 6c 75 6d 6e 73 2e 20 20 41 74 20 74 68 65 20 olumns. At the
204e4 73 61 6d 65 20 74 69 6d 65 2c 20 64 6f 20 65 72 same time, do er
204e5 72 6f 72 0a 2a 2a 20 63 68 65 63 6b 69 6e 67 20 ror.** checking
204e6 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 75 73 61 67 on function usag
204e7 65 20 61 6e 64 20 73 65 74 20 61 20 66 6c 61 67 e and set a flag
204e8 20 69 66 20 61 6e 79 20 61 67 67 72 65 67 61 74 if any aggregat
204e9 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 61 e functions.** a
204ea 72 65 20 73 65 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54 re seen..**.** T
204eb 6f 20 72 65 73 6f 6c 76 65 20 74 61 62 6c 65 20 o resolve table
204ec 63 6f 6c 75 6d 6e 73 20 72 65 66 65 72 65 6e 63 columns referenc
204ed 65 73 20 77 65 20 6c 6f 6f 6b 20 66 6f 72 20 6e es we look for n
204ee 6f 64 65 73 20 28 6f 72 20 73 75 62 74 72 65 65 odes (or subtree
204ef 73 29 20 6f 66 20 74 68 65 20 0a 2a 2a 20 66 6f s) of the .** fo
204f0 72 6d 20 58 2e 59 2e 5a 20 6f 72 20 59 2e 5a 20 rm X.Y.Z or Y.Z
204f1 6f 72 20 6a 75 73 74 20 5a 20 77 68 65 72 65 0a or just Z where.
204f2 2a 2a 0a 2a 2a 20 20 20 20 20 20 58 3a 20 20 20 **.** X:
204f3 54 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 64 61 The name of a da
204f4 74 61 62 61 73 65 2e 20 20 45 78 3a 20 20 22 6d tabase. Ex: "m
204f5 61 69 6e 22 20 6f 72 20 22 74 65 6d 70 22 20 6f ain" or "temp" o
204f6 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 r.** t
204f7 68 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 he symbolic name
204f8 20 61 73 73 69 67 6e 65 64 20 74 6f 20 61 6e 20 assigned to an
204f9 41 54 54 41 43 48 2d 65 64 20 64 61 74 61 62 61 ATTACH-ed databa
204fa 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 59 se..**.** Y
204fb 3a 20 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 : The name of
204fc 61 20 74 61 62 6c 65 20 69 6e 20 61 20 46 52 4f a table in a FRO
204fd 4d 20 63 6c 61 75 73 65 2e 20 20 4f 72 20 69 6e M clause. Or in
204fe 20 61 20 74 72 69 67 67 65 72 0a 2a 2a 20 20 20 a trigger.**
204ff 20 20 20 20 20 20 20 20 6f 6e 65 20 6f 66 20 74 one of t
20500 68 65 20 73 70 65 63 69 61 6c 20 6e 61 6d 65 73 he special names
20501 20 22 6f 6c 64 22 20 6f 72 20 22 6e 65 77 22 2e "old" or "new".
20502 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 5a 3a 20 20 .**.** Z:
20503 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 63 The name of a c
20504 6f 6c 75 6d 6e 20 69 6e 20 74 61 62 6c 65 20 59 olumn in table Y
20505 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 6f 64 65 ..**.** The node
20506 20 61 74 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 at the root of
20507 74 68 65 20 73 75 62 74 72 65 65 20 69 73 20 6d the subtree is m
20508 6f 64 69 66 69 65 64 20 61 73 20 66 6f 6c 6c 6f odified as follo
20509 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 45 78 70 ws:.**.** Exp
2050a 72 2e 6f 70 20 20 20 20 20 20 20 20 43 68 61 6e r.op Chan
2050b 67 65 64 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e ged to TK_COLUMN
2050c 0a 2a 2a 20 20 20 20 45 78 70 72 2e 70 54 61 62 .** Expr.pTab
2050d 20 20 20 20 20 20 50 6f 69 6e 74 73 20 74 6f 20 Points to
2050e 74 68 65 20 54 61 62 6c 65 20 6f 62 6a 65 63 74 the Table object
2050f 20 66 6f 72 20 58 2e 59 0a 2a 2a 20 20 20 20 45 for X.Y.** E
20510 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 20 20 54 68 xpr.iColumn Th
20511 65 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 20 69 e column index i
20512 6e 20 58 2e 59 2e 20 20 2d 31 20 66 6f 72 20 74 n X.Y. -1 for t
20513 68 65 20 72 6f 77 69 64 2e 0a 2a 2a 20 20 20 20 he rowid..**
20514 45 78 70 72 2e 69 54 61 62 6c 65 20 20 20 20 54 Expr.iTable T
20515 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e he VDBE cursor n
20516 75 6d 62 65 72 20 66 6f 72 20 58 2e 59 0a 2a 2a umber for X.Y.**
20517 0a 2a 2a 0a 2a 2a 20 54 6f 20 72 65 73 6f 6c 76 .**.** To resolv
20518 65 20 72 65 73 75 6c 74 2d 73 65 74 20 72 65 66 e result-set ref
20519 65 72 65 6e 63 65 73 2c 20 6c 6f 6f 6b 20 66 6f erences, look fo
2051a 72 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 r expression nod
2051b 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 72 es of the.** for
2051c 6d 20 5a 20 28 77 69 74 68 20 6e 6f 20 58 20 61 m Z (with no X a
2051d 6e 64 20 59 20 70 72 65 66 69 78 29 20 77 68 65 nd Y prefix) whe
2051e 72 65 20 74 68 65 20 5a 20 6d 61 74 63 68 65 73 re the Z matches
2051f 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64 0a the right-hand.
20520 2a 2a 20 73 69 7a 65 20 6f 66 20 61 6e 20 41 53 ** size of an AS
20521 20 63 6c 61 75 73 65 20 69 6e 20 74 68 65 20 72 clause in the r
20522 65 73 75 6c 74 2d 73 65 74 20 6f 66 20 61 20 53 esult-set of a S
20523 45 4c 45 43 54 2e 20 20 54 68 65 20 5a 20 65 78 ELECT. The Z ex
20524 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 73 20 72 pression.** is r
20525 65 70 6c 61 63 65 64 20 62 79 20 61 20 63 6f 70 eplaced by a cop
20526 79 20 6f 66 20 74 68 65 20 6c 65 66 74 2d 68 61 y of the left-ha
20527 6e 64 20 73 69 64 65 20 6f 66 20 74 68 65 20 72 nd side of the r
20528 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 esult-set expres
20529 73 69 6f 6e 2e 0a 2a 2a 20 54 61 62 6c 65 2d 6e sion..** Table-n
2052a 61 6d 65 20 61 6e 64 20 66 75 6e 63 74 69 6f 6e ame and function
2052b 20 72 65 73 6f 6c 75 74 69 6f 6e 20 6f 63 63 75 resolution occu
2052c 72 73 20 6f 6e 20 74 68 65 20 73 75 62 73 74 69 rs on the substi
2052d 74 75 74 65 64 20 65 78 70 72 65 73 73 69 6f 6e tuted expression
2052e 0a 2a 2a 20 74 72 65 65 2e 20 20 46 6f 72 20 65 .** tree. For e
2052f 78 61 6d 70 6c 65 2c 20 69 6e 3a 0a 2a 2a 0a 2a xample, in:.**.*
20530 2a 20 20 20 20 20 20 53 45 4c 45 43 54 20 61 2b * SELECT a+
20531 62 20 41 53 20 78 2c 20 63 2b 64 20 41 53 20 79 b AS x, c+d AS y
20532 20 46 52 4f 4d 20 74 31 20 4f 52 44 45 52 20 42 FROM t1 ORDER B
20533 59 20 78 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 Y x;.**.** The "
20534 78 22 20 74 65 72 6d 20 6f 66 20 74 68 65 20 6f x" term of the o
20535 72 64 65 72 20 62 79 20 69 73 20 72 65 70 6c 61 rder by is repla
20536 63 65 64 20 62 79 20 22 61 2b 62 22 20 74 6f 20 ced by "a+b" to
20537 72 65 6e 64 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 render:.**.**
20538 20 20 20 53 45 4c 45 43 54 20 61 2b 62 20 41 53 SELECT a+b AS
20539 20 78 2c 20 63 2b 64 20 41 53 20 79 20 46 52 4f x, c+d AS y FRO
2053a 4d 20 74 31 20 4f 52 44 45 52 20 42 59 20 61 2b M t1 ORDER BY a+
2053b 62 3b 0a 2a 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f b;.**.** Functio
2053c 6e 20 63 61 6c 6c 73 20 61 72 65 20 63 68 65 63 n calls are chec
2053d 6b 65 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 ked to make sure
2053e 20 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 69 that the functi
2053f 6f 6e 20 69 73 20 0a 2a 2a 20 64 65 66 69 6e 65 on is .** define
20540 64 20 61 6e 64 20 74 68 61 74 20 74 68 65 20 63 d and that the c
20541 6f 72 72 65 63 74 20 6e 75 6d 62 65 72 20 6f 66 orrect number of
20542 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20 73 arguments are s
20543 70 65 63 69 66 69 65 64 2e 0a 2a 2a 20 49 66 20 pecified..** If
20544 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 the function is
20545 61 6e 20 61 67 67 72 65 67 61 74 65 20 66 75 6e an aggregate fun
20546 63 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 ction, then the
20547 70 4e 43 2d 3e 68 61 73 41 67 67 20 69 73 0a 2a pNC->hasAgg is.*
20548 2a 20 73 65 74 20 61 6e 64 20 74 68 65 20 6f 70 * set and the op
20549 63 6f 64 65 20 69 73 20 63 68 61 6e 67 65 64 20 code is changed
2054a 66 72 6f 6d 20 54 4b 5f 46 55 4e 43 54 49 4f 4e from TK_FUNCTION
2054b 20 74 6f 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 to TK_AGG_FUNCT
2054c 49 4f 4e 2e 0a 2a 2a 20 49 66 20 61 6e 20 65 78 ION..** If an ex
2054d 70 72 65 73 73 69 6f 6e 20 63 6f 6e 74 61 69 6e pression contain
2054e 73 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 s aggregate func
2054f 74 69 6f 6e 73 20 74 68 65 6e 20 74 68 65 20 45 tions then the E
20550 50 5f 41 67 67 0a 2a 2a 20 70 72 6f 70 65 72 74 P_Agg.** propert
20551 79 20 6f 6e 20 74 68 65 20 65 78 70 72 65 73 73 y on the express
20552 69 6f 6e 20 69 73 20 73 65 74 2e 0a 2a 2a 0a 2a ion is set..**.*
20553 2a 20 41 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 * An error messa
20554 67 65 20 69 73 20 6c 65 66 74 20 69 6e 20 70 50 ge is left in pP
20555 61 72 73 65 20 69 66 20 61 6e 79 74 68 69 6e 67 arse if anything
20556 20 69 73 20 61 6d 69 73 73 2e 20 20 54 68 65 20 is amiss. The
20557 6e 75 6d 62 65 72 0a 2a 2a 20 69 66 20 65 72 72 number.** if err
20558 6f 72 73 20 69 73 20 72 65 74 75 72 6e 65 64 2e ors is returned.
20559 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
2055a 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 TE int sqlite3Re
2055b 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 20 solveExprNames(
2055c 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a . NameContext *
2055d 70 4e 43 2c 20 20 20 20 20 20 20 2f 2a 20 4e 61 pNC, /* Na
2055e 6d 65 73 70 61 63 65 20 74 6f 20 72 65 73 6f 6c mespace to resol
2055f 76 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 ve expressions i
20560 6e 2e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 n. */. Expr *pE
20561 78 70 72 20 20 20 20 20 20 20 20 20 20 20 20 20 xpr
20562 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f /* The expressio
20563 6e 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 n to be analyzed
20564 2e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 73 61 . */.){. int sa
20565 76 65 64 48 61 73 41 67 67 3b 0a 20 20 57 61 6c vedHasAgg;. Wal
20566 6b 65 72 20 77 3b 0a 0a 20 20 69 66 28 20 70 45 ker w;.. if( pE
20567 78 70 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 xpr==0 ) return
20568 30 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 0;.#if SQLITE_MA
20569 58 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 20 X_EXPR_DEPTH>0.
2056a 20 7b 0a 20 20 20 20 50 61 72 73 65 20 2a 70 50 {. Parse *pP
2056b 61 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 arse = pNC->pPar
2056c 73 65 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 se;. if( sqli
2056d 74 65 33 45 78 70 72 43 68 65 63 6b 48 65 69 67 te3ExprCheckHeig
2056e 68 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 ht(pParse, pExpr
2056f 2d 3e 6e 48 65 69 67 68 74 2b 70 4e 43 2d 3e 70 ->nHeight+pNC->p
20570 50 61 72 73 65 2d 3e 6e 48 65 69 67 68 74 29 20 Parse->nHeight)
20571 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
20572 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 1;. }. pPa
20573 72 73 65 2d 3e 6e 48 65 69 67 68 74 20 2b 3d 20 rse->nHeight +=
20574 70 45 78 70 72 2d 3e 6e 48 65 69 67 68 74 3b 0a pExpr->nHeight;.
20575 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 73 61 76 }.#endif. sav
20576 65 64 48 61 73 41 67 67 20 3d 20 70 4e 43 2d 3e edHasAgg = pNC->
20577 68 61 73 41 67 67 3b 0a 20 20 70 4e 43 2d 3e 68 hasAgg;. pNC->h
20578 61 73 41 67 67 20 3d 20 30 3b 0a 20 20 77 2e 78 asAgg = 0;. w.x
20579 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 72 ExprCallback = r
2057a 65 73 6f 6c 76 65 45 78 70 72 53 74 65 70 3b 0a esolveExprStep;.
2057b 20 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 w.xSelectCallb
2057c 61 63 6b 20 3d 20 72 65 73 6f 6c 76 65 53 65 6c ack = resolveSel
2057d 65 63 74 53 74 65 70 3b 0a 20 20 77 2e 70 50 61 ectStep;. w.pPa
2057e 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 rse = pNC->pPars
2057f 65 3b 0a 20 20 77 2e 75 2e 70 4e 43 20 3d 20 70 e;. w.u.pNC = p
20580 4e 43 3b 0a 20 20 73 71 6c 69 74 65 33 57 61 6c NC;. sqlite3Wal
20581 6b 45 78 70 72 28 26 77 2c 20 70 45 78 70 72 29 kExpr(&w, pExpr)
20582 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 ;.#if SQLITE_MAX
20583 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 20 20 _EXPR_DEPTH>0.
20584 70 4e 43 2d 3e 70 50 61 72 73 65 2d 3e 6e 48 65 pNC->pParse->nHe
20585 69 67 68 74 20 2d 3d 20 70 45 78 70 72 2d 3e 6e ight -= pExpr->n
20586 48 65 69 67 68 74 3b 0a 23 65 6e 64 69 66 0a 20 Height;.#endif.
20587 20 69 66 28 20 70 4e 43 2d 3e 6e 45 72 72 3e 30 if( pNC->nErr>0
20588 20 7c 7c 20 77 2e 70 50 61 72 73 65 2d 3e 6e 45 || w.pParse->nE
20589 72 72 3e 30 20 29 7b 0a 20 20 20 20 45 78 70 72 rr>0 ){. Expr
2058a 53 65 74 50 72 6f 70 65 72 74 79 28 70 45 78 70 SetProperty(pExp
2058b 72 2c 20 45 50 5f 45 72 72 6f 72 29 3b 0a 20 20 r, EP_Error);.
2058c 7d 0a 20 20 69 66 28 20 70 4e 43 2d 3e 68 61 73 }. if( pNC->has
2058d 41 67 67 20 29 7b 0a 20 20 20 20 45 78 70 72 53 Agg ){. ExprS
2058e 65 74 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 etProperty(pExpr
2058f 2c 20 45 50 5f 41 67 67 29 3b 0a 20 20 7d 65 6c , EP_Agg);. }el
20590 73 65 20 69 66 28 20 73 61 76 65 64 48 61 73 41 se if( savedHasA
20591 67 67 20 29 7b 0a 20 20 20 20 70 4e 43 2d 3e 68 gg ){. pNC->h
20592 61 73 41 67 67 20 3d 20 31 3b 0a 20 20 7d 0a 20 asAgg = 1;. }.
20593 20 72 65 74 75 72 6e 20 45 78 70 72 48 61 73 50 return ExprHasP
20594 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 roperty(pExpr, E
20595 50 5f 45 72 72 6f 72 29 3b 0a 7d 0a 0a 0a 2f 2a P_Error);.}.../*
20596 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 61 6c 6c 20 .** Resolve all
20597 6e 61 6d 65 73 20 69 6e 20 61 6c 6c 20 65 78 70 names in all exp
20598 72 65 73 73 69 6f 6e 73 20 6f 66 20 61 20 53 45 ressions of a SE
20599 4c 45 43 54 20 61 6e 64 20 69 6e 20 61 6c 6c 0a LECT and in all.
2059a 2a 2a 20 64 65 63 65 6e 64 65 6e 74 73 20 6f 66 ** decendents of
2059b 20 74 68 65 20 53 45 4c 45 43 54 2c 20 69 6e 63 the SELECT, inc
2059c 6c 75 64 69 6e 67 20 63 6f 6d 70 6f 75 6e 64 73 luding compounds
2059d 20 6f 66 66 20 6f 66 20 70 2d 3e 70 50 72 69 6f off of p->pPrio
2059e 72 2c 0a 2a 2a 20 73 75 62 71 75 65 72 69 65 73 r,.** subqueries
2059f 20 69 6e 20 65 78 70 72 65 73 73 69 6f 6e 73 2c in expressions,
205a0 20 61 6e 64 20 73 75 62 71 75 65 72 69 65 73 20 and subqueries
205a1 75 73 65 64 20 61 73 20 46 52 4f 4d 20 63 6c 61 used as FROM cla
205a2 75 73 65 0a 2a 2a 20 74 65 72 6d 73 2e 0a 2a 2a use.** terms..**
205a3 0a 2a 2a 20 53 65 65 20 73 71 6c 69 74 65 33 52 .** See sqlite3R
205a4 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 esolveExprNames(
205a5 29 20 66 6f 72 20 61 20 64 65 73 63 72 69 70 74 ) for a descript
205a6 69 6f 6e 20 6f 66 20 74 68 65 20 6b 69 6e 64 73 ion of the kinds
205a7 20 6f 66 0a 2a 2a 20 74 72 61 6e 73 66 6f 72 6d of.** transform
205a8 61 74 69 6f 6e 73 20 74 68 61 74 20 6f 63 63 75 ations that occu
205a9 72 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 53 45 4c r..**.** All SEL
205aa 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 73 ECT statements s
205ab 68 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e 20 hould have been
205ac 65 78 70 61 6e 64 65 64 20 75 73 69 6e 67 0a 2a expanded using.*
205ad 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 * sqlite3SelectE
205ae 78 70 61 6e 64 28 29 20 70 72 69 6f 72 20 74 6f xpand() prior to
205af 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69 73 20 72 invoking this r
205b0 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 outine..*/.SQLIT
205b1 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
205b2 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 53 65 6c qlite3ResolveSel
205b3 65 63 74 4e 61 6d 65 73 28 0a 20 20 50 61 72 73 ectNames(. Pars
205b4 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 e *pParse,
205b5 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 /* The parser
205b6 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 context */. Se
205b7 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20 lect *p,
205b8 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 /* The SELE
205b9 43 54 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69 CT statement bei
205ba 6e 67 20 63 6f 64 65 64 2e 20 2a 2f 0a 20 20 4e ng coded. */. N
205bb 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4f 75 74 ameContext *pOut
205bc 65 72 4e 43 20 20 2f 2a 20 4e 61 6d 65 20 63 6f erNC /* Name co
205bd 6e 74 65 78 74 20 66 6f 72 20 70 61 72 65 6e 74 ntext for parent
205be 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
205bf 74 20 2a 2f 0a 29 7b 0a 20 20 57 61 6c 6b 65 72 t */.){. Walker
205c0 20 77 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 w;.. assert( p
205c1 21 3d 30 20 29 3b 0a 20 20 77 2e 78 45 78 70 72 !=0 );. w.xExpr
205c2 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 73 6f 6c Callback = resol
205c3 76 65 45 78 70 72 53 74 65 70 3b 0a 20 20 77 2e veExprStep;. w.
205c4 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 xSelectCallback
205c5 3d 20 72 65 73 6f 6c 76 65 53 65 6c 65 63 74 53 = resolveSelectS
205c6 74 65 70 3b 0a 20 20 77 2e 70 50 61 72 73 65 20 tep;. w.pParse
205c7 3d 20 70 50 61 72 73 65 3b 0a 20 20 77 2e 75 2e = pParse;. w.u.
205c8 70 4e 43 20 3d 20 70 4f 75 74 65 72 4e 43 3b 0a pNC = pOuterNC;.
205c9 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c sqlite3WalkSel
205ca 65 63 74 28 26 77 2c 20 70 29 3b 0a 7d 0a 0a 2f ect(&w, p);.}../
205cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
205cc 6e 64 20 6f 66 20 72 65 73 6f 6c 76 65 2e 63 20 nd of resolve.c
205cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
205ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
205cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
205d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
205d1 65 67 69 6e 20 66 69 6c 65 20 65 78 70 72 2e 63 egin file expr.c
205d2 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
205d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
205d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
205d5 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d *.** 2001 Septem
205d6 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 ber 15.**.** The
205d7 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
205d8 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
205d9 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
205da 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
205db 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
205dc 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
205dd 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
205de 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
205df 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
205e0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
205e1 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
205e2 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
205e3 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
205e4 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
205e5 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
205e6 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
205e7 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
205e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
205e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
205ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
205eb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
205ec 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 *****.** This fi
205ed 6c 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75 74 le contains rout
205ee 69 6e 65 73 20 75 73 65 64 20 66 6f 72 20 61 6e ines used for an
205ef 61 6c 79 7a 69 6e 67 20 65 78 70 72 65 73 73 69 alyzing expressi
205f0 6f 6e 73 20 61 6e 64 0a 2a 2a 20 66 6f 72 20 67 ons and.** for g
205f1 65 6e 65 72 61 74 69 6e 67 20 56 44 42 45 20 63 enerating VDBE c
205f2 6f 64 65 20 74 68 61 74 20 65 76 61 6c 75 61 74 ode that evaluat
205f3 65 73 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 es expressions i
205f4 6e 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 0a 2f 2a n SQLite..*/../*
205f5 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 27 .** Return the '
205f6 61 66 66 69 6e 69 74 79 27 20 6f 66 20 74 68 65 affinity' of the
205f7 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 expression pExp
205f8 72 20 69 66 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 r if any..**.**
205f9 49 66 20 70 45 78 70 72 20 69 73 20 61 20 63 6f If pExpr is a co
205fa 6c 75 6d 6e 2c 20 61 20 72 65 66 65 72 65 6e 63 lumn, a referenc
205fb 65 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 76 69 e to a column vi
205fc 61 20 61 6e 20 27 41 53 27 20 61 6c 69 61 73 2c a an 'AS' alias,
205fd 0a 2a 2a 20 6f 72 20 61 20 73 75 62 2d 73 65 6c .** or a sub-sel
205fe 65 63 74 20 77 69 74 68 20 61 20 63 6f 6c 75 6d ect with a colum
205ff 6e 20 61 73 20 74 68 65 20 72 65 74 75 72 6e 20 n as the return
20600 76 61 6c 75 65 2c 20 74 68 65 6e 20 74 68 65 20 value, then the
20601 0a 2a 2a 20 61 66 66 69 6e 69 74 79 20 6f 66 20 .** affinity of
20602 74 68 61 74 20 63 6f 6c 75 6d 6e 20 69 73 20 72 that column is r
20603 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 eturned. Otherwi
20604 73 65 2c 20 30 78 30 30 20 69 73 20 72 65 74 75 se, 0x00 is retu
20605 72 6e 65 64 2c 0a 2a 2a 20 69 6e 64 69 63 61 74 rned,.** indicat
20606 69 6e 67 20 6e 6f 20 61 66 66 69 6e 69 74 79 20 ing no affinity
20607 66 6f 72 20 74 68 65 20 65 78 70 72 65 73 73 69 for the expressi
20608 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 69 2e 65 2e 20 74 on..**.** i.e. t
20609 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 he WHERE clause
2060a 65 78 70 72 65 73 73 73 69 6f 6e 73 20 69 6e 20 expresssions in
2060b 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
2060c 61 74 65 6d 65 6e 74 73 20 61 6c 6c 0a 2a 2a 20 atements all.**
2060d 68 61 76 65 20 61 6e 20 61 66 66 69 6e 69 74 79 have an affinity
2060e 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41 54 45 20 54 :.**.** CREATE T
2060f 41 42 4c 45 20 74 31 28 61 29 3b 0a 2a 2a 20 53 ABLE t1(a);.** S
20610 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 ELECT * FROM t1
20611 57 48 45 52 45 20 61 3b 0a 2a 2a 20 53 45 4c 45 WHERE a;.** SELE
20612 43 54 20 61 20 41 53 20 62 20 46 52 4f 4d 20 74 CT a AS b FROM t
20613 31 20 57 48 45 52 45 20 62 3b 0a 2a 2a 20 53 45 1 WHERE b;.** SE
20614 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 57 LECT * FROM t1 W
20615 48 45 52 45 20 28 73 65 6c 65 63 74 20 61 20 66 HERE (select a f
20616 72 6f 6d 20 74 31 29 3b 0a 2a 2f 0a 53 51 4c 49 rom t1);.*/.SQLI
20617 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 TE_PRIVATE char
20618 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e sqlite3ExprAffin
20619 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 72 29 ity(Expr *pExpr)
2061a 7b 0a 20 20 69 6e 74 20 6f 70 20 3d 20 70 45 78 {. int op = pEx
2061b 70 72 2d 3e 6f 70 3b 0a 20 20 69 66 28 20 6f 70 pr->op;. if( op
2061c 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 29 7b 0a 20 ==TK_SELECT ){.
2061d 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 assert( pExpr
2061e 2d 3e 66 6c 61 67 73 26 45 50 5f 78 49 73 53 65 ->flags&EP_xIsSe
2061f 6c 65 63 74 20 29 3b 0a 20 20 20 20 72 65 74 75 lect );. retu
20620 72 6e 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 rn sqlite3ExprAf
20621 66 69 6e 69 74 79 28 70 45 78 70 72 2d 3e 78 2e finity(pExpr->x.
20622 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d pSelect->pEList-
20623 3e 61 5b 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 >a[0].pExpr);.
20624 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 }.#ifndef SQLITE
20625 5f 4f 4d 49 54 5f 43 41 53 54 0a 20 20 69 66 28 _OMIT_CAST. if(
20626 20 6f 70 3d 3d 54 4b 5f 43 41 53 54 20 29 7b 0a op==TK_CAST ){.
20627 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 assert( !Exp
20628 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 rHasProperty(pEx
20629 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 pr, EP_IntValue)
2062a 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 );. return s
2062b 71 6c 69 74 65 33 41 66 66 69 6e 69 74 79 54 79 qlite3AffinityTy
2062c 70 65 28 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b pe(pExpr->u.zTok
2062d 65 6e 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a en);. }.#endif.
2062e 20 20 69 66 28 20 28 6f 70 3d 3d 54 4b 5f 41 47 if( (op==TK_AG
2062f 47 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d G_COLUMN || op==
20630 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d TK_COLUMN || op=
20631 3d 54 4b 5f 52 45 47 49 53 54 45 52 29 20 0a 20 =TK_REGISTER) .
20632 20 20 26 26 20 70 45 78 70 72 2d 3e 70 54 61 62 && pExpr->pTab
20633 21 3d 30 0a 20 20 29 7b 0a 20 20 20 20 2f 2a 20 !=0. ){. /*
20634 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 op==TK_REGISTER
20635 26 26 20 70 45 78 70 72 2d 3e 70 54 61 62 21 3d && pExpr->pTab!=
20636 30 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 70 0 happens when p
20637 45 78 70 72 20 77 61 73 20 6f 72 69 67 69 6e 61 Expr was origina
20638 6c 6c 79 0a 20 20 20 20 2a 2a 20 61 20 54 4b 5f lly. ** a TK_
20639 43 4f 4c 55 4d 4e 20 62 75 74 20 77 61 73 20 70 COLUMN but was p
2063a 72 65 76 69 6f 75 73 6c 79 20 65 76 61 6c 75 61 reviously evalua
2063b 74 65 64 20 61 6e 64 20 63 61 63 68 65 64 20 69 ted and cached i
2063c 6e 20 61 20 72 65 67 69 73 74 65 72 20 2a 2f 0a n a register */.
2063d 20 20 20 20 69 6e 74 20 6a 20 3d 20 70 45 78 70 int j = pExp
2063e 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 r->iColumn;.
2063f 69 66 28 20 6a 3c 30 20 29 20 72 65 74 75 72 6e if( j<0 ) return
20640 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 SQLITE_AFF_INTE
20641 47 45 52 3b 0a 20 20 20 20 61 73 73 65 72 74 28 GER;. assert(
20642 20 70 45 78 70 72 2d 3e 70 54 61 62 20 26 26 20 pExpr->pTab &&
20643 6a 3c 70 45 78 70 72 2d 3e 70 54 61 62 2d 3e 6e j<pExpr->pTab->n
20644 43 6f 6c 20 29 3b 0a 20 20 20 20 72 65 74 75 72 Col );. retur
20645 6e 20 70 45 78 70 72 2d 3e 70 54 61 62 2d 3e 61 n pExpr->pTab->a
20646 43 6f 6c 5b 6a 5d 2e 61 66 66 69 6e 69 74 79 3b Col[j].affinity;
20647 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 45 . }. return pE
20648 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 3b 0a 7d xpr->affinity;.}
20649 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 ../*.** Set the
2064a 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e collating sequen
2064b 63 65 20 66 6f 72 20 65 78 70 72 65 73 73 69 6f ce for expressio
2064c 6e 20 70 45 78 70 72 20 74 6f 20 62 65 20 74 68 n pExpr to be th
2064d 65 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73 e collating.** s
2064e 65 71 75 65 6e 63 65 20 6e 61 6d 65 64 20 62 79 equence named by
2064f 20 70 54 6f 6b 65 6e 2e 20 20 20 52 65 74 75 72 pToken. Retur
20650 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 n a pointer to t
20651 68 65 20 72 65 76 69 73 65 64 20 65 78 70 72 65 he revised expre
20652 73 73 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 63 6f ssion..** The co
20653 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
20654 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 22 65 is marked as "e
20655 78 70 6c 69 63 69 74 22 20 75 73 69 6e 67 20 74 xplicit" using t
20656 68 65 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 he EP_ExpCollate
20657 0a 2a 2a 20 66 6c 61 67 2e 20 20 41 6e 20 65 78 .** flag. An ex
20658 70 6c 69 63 69 74 20 63 6f 6c 6c 61 74 69 6e 67 plicit collating
20659 20 73 65 71 75 65 6e 63 65 20 77 69 6c 6c 20 6f sequence will o
2065a 76 65 72 72 69 64 65 20 69 6d 70 6c 69 63 69 74 verride implicit
2065b 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 .** collating se
2065c 71 75 65 6e 63 65 73 2e 0a 2a 2f 0a 53 51 4c 49 quences..*/.SQLI
2065d 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 TE_PRIVATE Expr
2065e 2a 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 43 *sqlite3ExprSetC
2065f 6f 6c 6c 28 50 61 72 73 65 20 2a 70 50 61 72 73 oll(Parse *pPars
20660 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 e, Expr *pExpr,
20661 54 6f 6b 65 6e 20 2a 70 43 6f 6c 6c 4e 61 6d 65 Token *pCollName
20662 29 7b 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c ){. char *zColl
20663 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
20664 20 2f 2a 20 44 65 71 75 6f 74 65 64 20 6e 61 6d /* Dequoted nam
20665 65 20 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 e of collation s
20666 65 71 75 65 6e 63 65 20 2a 2f 0a 20 20 43 6f 6c equence */. Col
20667 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 73 lSeq *pColl;. s
20668 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 qlite3 *db = pPa
20669 72 73 65 2d 3e 64 62 3b 0a 20 20 7a 43 6f 6c 6c rse->db;. zColl
2066a 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 = sqlite3NameFr
2066b 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 43 6f 6c omToken(db, pCol
2066c 6c 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 70 45 lName);. if( pE
2066d 78 70 72 20 26 26 20 7a 43 6f 6c 6c 20 29 7b 0a xpr && zColl ){.
2066e 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 pColl = sqli
2066f 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 te3LocateCollSeq
20670 28 70 50 61 72 73 65 2c 20 7a 43 6f 6c 6c 29 3b (pParse, zColl);
20671 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 . if( pColl )
20672 7b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 {. pExpr->p
20673 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 Coll = pColl;.
20674 20 20 20 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 pExpr->flags
20675 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 |= EP_ExpCollat
20676 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 e;. }. }. s
20677 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
20678 20 7a 43 6f 6c 6c 29 3b 0a 20 20 72 65 74 75 72 zColl);. retur
20679 6e 20 70 45 78 70 72 3b 0a 7d 0a 0a 2f 2a 0a 2a n pExpr;.}../*.*
2067a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 64 65 66 * Return the def
2067b 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 ault collation s
2067c 65 71 75 65 6e 63 65 20 66 6f 72 20 74 68 65 20 equence for the
2067d 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 expression pExpr
2067e 2e 20 49 66 0a 2a 2a 20 74 68 65 72 65 20 69 73 . If.** there is
2067f 20 6e 6f 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c no default coll
20680 61 74 69 6f 6e 20 74 79 70 65 2c 20 72 65 74 75 ation type, retu
20681 72 6e 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f rn 0..*/.SQLITE_
20682 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 PRIVATE CollSeq
20683 2a 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c *sqlite3ExprColl
20684 53 65 71 28 50 61 72 73 65 20 2a 70 50 61 72 73 Seq(Parse *pPars
20685 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b e, Expr *pExpr){
20686 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c . CollSeq *pCol
20687 6c 20 3d 20 30 3b 0a 20 20 45 78 70 72 20 2a 70 l = 0;. Expr *p
20688 20 3d 20 70 45 78 70 72 3b 0a 20 20 77 68 69 6c = pExpr;. whil
20689 65 28 20 41 4c 57 41 59 53 28 70 29 20 29 7b 0a e( ALWAYS(p) ){.
2068a 20 20 20 20 69 6e 74 20 6f 70 3b 0a 20 20 20 20 int op;.
2068b 70 43 6f 6c 6c 20 3d 20 70 2d 3e 70 43 6f 6c 6c pColl = p->pColl
2068c 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 ;. if( pColl
2068d 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 6f 70 20 ) break;. op
2068e 3d 20 70 2d 3e 6f 70 3b 0a 20 20 20 20 69 66 28 = p->op;. if(
2068f 20 70 2d 3e 70 54 61 62 21 3d 30 20 26 26 20 28 p->pTab!=0 && (
20690 0a 20 20 20 20 20 20 20 20 6f 70 3d 3d 54 4b 5f . op==TK_
20691 41 47 47 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 AGG_COLUMN || op
20692 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f ==TK_COLUMN || o
20693 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 7c p==TK_REGISTER |
20694 7c 20 6f 70 3d 3d 54 4b 5f 54 52 49 47 47 45 52 | op==TK_TRIGGER
20695 0a 20 20 20 20 29 29 7b 0a 20 20 20 20 20 20 2f . )){. /
20696 2a 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 * op==TK_REGISTE
20697 52 20 26 26 20 70 2d 3e 70 54 61 62 21 3d 30 20 R && p->pTab!=0
20698 68 61 70 70 65 6e 73 20 77 68 65 6e 20 70 45 78 happens when pEx
20699 70 72 20 77 61 73 20 6f 72 69 67 69 6e 61 6c 6c pr was originall
2069a 79 0a 20 20 20 20 20 20 2a 2a 20 61 20 54 4b 5f y. ** a TK_
2069b 43 4f 4c 55 4d 4e 20 62 75 74 20 77 61 73 20 70 COLUMN but was p
2069c 72 65 76 69 6f 75 73 6c 79 20 65 76 61 6c 75 61 reviously evalua
2069d 74 65 64 20 61 6e 64 20 63 61 63 68 65 64 20 69 ted and cached i
2069e 6e 20 61 20 72 65 67 69 73 74 65 72 20 2a 2f 0a n a register */.
2069f 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 const char
206a0 20 2a 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 69 *zColl;. i
206a1 6e 74 20 6a 20 3d 20 70 2d 3e 69 43 6f 6c 75 6d nt j = p->iColum
206a2 6e 3b 0a 20 20 20 20 20 20 69 66 28 20 6a 3e 3d n;. if( j>=
206a3 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 0 ){. sql
206a4 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 ite3 *db = pPars
206a5 65 2d 3e 64 62 3b 0a 20 20 20 20 20 20 20 20 7a e->db;. z
206a6 43 6f 6c 6c 20 3d 20 70 2d 3e 70 54 61 62 2d 3e Coll = p->pTab->
206a7 61 43 6f 6c 5b 6a 5d 2e 7a 43 6f 6c 6c 3b 0a 20 aCol[j].zColl;.
206a8 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 pColl = s
206a9 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 qlite3FindCollSe
206aa 71 28 64 62 2c 20 45 4e 43 28 64 62 29 2c 20 7a q(db, ENC(db), z
206ab 43 6f 6c 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 Coll, 0);.
206ac 20 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 20 3d pExpr->pColl =
206ad 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a pColl;. }.
206ae 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
206af 20 7d 0a 20 20 20 20 69 66 28 20 6f 70 21 3d 54 }. if( op!=T
206b0 4b 5f 43 41 53 54 20 26 26 20 6f 70 21 3d 54 4b K_CAST && op!=TK
206b1 5f 55 50 4c 55 53 20 29 7b 0a 20 20 20 20 20 20 _UPLUS ){.
206b2 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
206b3 20 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a 20 p = p->pLeft;.
206b4 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 }. if( sqlite3
206b5 43 68 65 63 6b 43 6f 6c 6c 53 65 71 28 70 50 61 CheckCollSeq(pPa
206b6 72 73 65 2c 20 70 43 6f 6c 6c 29 20 29 7b 20 0a rse, pColl) ){ .
206b7 20 20 20 20 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 pColl = 0;.
206b8 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 43 6f 6c }. return pCol
206b9 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 78 70 l;.}../*.** pExp
206ba 72 20 69 73 20 61 6e 20 6f 70 65 72 61 6e 64 20 r is an operand
206bb 6f 66 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 of a comparison
206bc 6f 70 65 72 61 74 6f 72 2e 20 20 61 66 66 32 20 operator. aff2
206bd 69 73 20 74 68 65 0a 2a 2a 20 74 79 70 65 20 61 is the.** type a
206be 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65 20 6f ffinity of the o
206bf 74 68 65 72 20 6f 70 65 72 61 6e 64 2e 20 20 54 ther operand. T
206c0 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 his routine retu
206c1 72 6e 73 20 74 68 65 0a 2a 2a 20 74 79 70 65 20 rns the.** type
206c2 61 66 66 69 6e 69 74 79 20 74 68 61 74 20 73 68 affinity that sh
206c3 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f 72 ould be used for
206c4 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 the comparison
206c5 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 53 51 4c operator..*/.SQL
206c6 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 ITE_PRIVATE char
206c7 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 sqlite3CompareA
206c8 66 66 69 6e 69 74 79 28 45 78 70 72 20 2a 70 45 ffinity(Expr *pE
206c9 78 70 72 2c 20 63 68 61 72 20 61 66 66 32 29 7b xpr, char aff2){
206ca 0a 20 20 63 68 61 72 20 61 66 66 31 20 3d 20 73 . char aff1 = s
206cb 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 qlite3ExprAffini
206cc 74 79 28 70 45 78 70 72 29 3b 0a 20 20 69 66 28 ty(pExpr);. if(
206cd 20 61 66 66 31 20 26 26 20 61 66 66 32 20 29 7b aff1 && aff2 ){
206ce 0a 20 20 20 20 2f 2a 20 42 6f 74 68 20 73 69 64 . /* Both sid
206cf 65 73 20 6f 66 20 74 68 65 20 63 6f 6d 70 61 72 es of the compar
206d0 69 73 6f 6e 20 61 72 65 20 63 6f 6c 75 6d 6e 73 ison are columns
206d1 2e 20 49 66 20 6f 6e 65 20 68 61 73 20 6e 75 6d . If one has num
206d2 65 72 69 63 0a 20 20 20 20 2a 2a 20 61 66 66 69 eric. ** affi
206d3 6e 69 74 79 2c 20 75 73 65 20 74 68 61 74 2e 20 nity, use that.
206d4 4f 74 68 65 72 77 69 73 65 20 75 73 65 20 6e 6f Otherwise use no
206d5 20 61 66 66 69 6e 69 74 79 2e 0a 20 20 20 20 2a affinity.. *
206d6 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 /. if( sqlite
206d7 33 49 73 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 3IsNumericAffini
206d8 74 79 28 61 66 66 31 29 20 7c 7c 20 73 71 6c 69 ty(aff1) || sqli
206d9 74 65 33 49 73 4e 75 6d 65 72 69 63 41 66 66 69 te3IsNumericAffi
206da 6e 69 74 79 28 61 66 66 32 29 20 29 7b 0a 20 20 nity(aff2) ){.
206db 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
206dc 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 3b 0a 20 E_AFF_NUMERIC;.
206dd 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
206de 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 46 return SQLITE_AF
206df 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 F_NONE;. }.
206e0 7d 65 6c 73 65 20 69 66 28 20 21 61 66 66 31 20 }else if( !aff1
206e1 26 26 20 21 61 66 66 32 20 29 7b 0a 20 20 20 20 && !aff2 ){.
206e2 2f 2a 20 4e 65 69 74 68 65 72 20 73 69 64 65 20 /* Neither side
206e3 6f 66 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f of the compariso
206e4 6e 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2e 20 20 n is a column.
206e5 43 6f 6d 70 61 72 65 20 74 68 65 0a 20 20 20 20 Compare the.
206e6 2a 2a 20 72 65 73 75 6c 74 73 20 64 69 72 65 63 ** results direc
206e7 74 6c 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 tly.. */.
206e8 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 46 return SQLITE_AF
206e9 46 5f 4e 4f 4e 45 3b 0a 20 20 7d 65 6c 73 65 7b F_NONE;. }else{
206ea 0a 20 20 20 20 2f 2a 20 4f 6e 65 20 73 69 64 65 . /* One side
206eb 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2c 20 74 68 is a column, th
206ec 65 20 6f 74 68 65 72 20 69 73 20 6e 6f 74 2e 20 e other is not.
206ed 55 73 65 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 Use the columns
206ee 61 66 66 69 6e 69 74 79 2e 20 2a 2f 0a 20 20 20 affinity. */.
206ef 20 61 73 73 65 72 74 28 20 61 66 66 31 3d 3d 30 assert( aff1==0
206f0 20 7c 7c 20 61 66 66 32 3d 3d 30 20 29 3b 0a 20 || aff2==0 );.
206f1 20 20 20 72 65 74 75 72 6e 20 28 61 66 66 31 20 return (aff1
206f2 2b 20 61 66 66 32 29 3b 0a 20 20 7d 0a 7d 0a 0a + aff2);. }.}..
206f3 2f 2a 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61 /*.** pExpr is a
206f4 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 comparison oper
206f5 61 74 6f 72 2e 20 20 52 65 74 75 72 6e 20 74 68 ator. Return th
206f6 65 20 74 79 70 65 20 61 66 66 69 6e 69 74 79 20 e type affinity
206f7 74 68 61 74 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 that should.** b
206f8 65 20 61 70 70 6c 69 65 64 20 74 6f 20 62 6f 74 e applied to bot
206f9 68 20 6f 70 65 72 61 6e 64 73 20 70 72 69 6f 72 h operands prior
206fa 20 74 6f 20 64 6f 69 6e 67 20 74 68 65 20 63 6f to doing the co
206fb 6d 70 61 72 69 73 6f 6e 2e 0a 2a 2f 0a 73 74 61 mparison..*/.sta
206fc 74 69 63 20 63 68 61 72 20 63 6f 6d 70 61 72 69 tic char compari
206fd 73 6f 6e 41 66 66 69 6e 69 74 79 28 45 78 70 72 sonAffinity(Expr
206fe 20 2a 70 45 78 70 72 29 7b 0a 20 20 63 68 61 72 *pExpr){. char
206ff 20 61 66 66 3b 0a 20 20 61 73 73 65 72 74 28 20 aff;. assert(
20700 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 51 pExpr->op==TK_EQ
20701 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 || pExpr->op==T
20702 4b 5f 49 4e 20 7c 7c 20 70 45 78 70 72 2d 3e 6f K_IN || pExpr->o
20703 70 3d 3d 54 4b 5f 4c 54 20 7c 7c 0a 20 20 20 20 p==TK_LT ||.
20704 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 3d pExpr->op=
20705 3d 54 4b 5f 47 54 20 7c 7c 20 70 45 78 70 72 2d =TK_GT || pExpr-
20706 3e 6f 70 3d 3d 54 4b 5f 47 45 20 7c 7c 20 70 45 >op==TK_GE || pE
20707 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4c 45 20 7c xpr->op==TK_LE |
20708 7c 0a 20 20 20 20 20 20 20 20 20 20 70 45 78 70 |. pExp
20709 72 2d 3e 6f 70 3d 3d 54 4b 5f 4e 45 20 7c 7c 20 r->op==TK_NE ||
2070a 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 53 pExpr->op==TK_IS
2070b 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 || pExpr->op==T
2070c 4b 5f 49 53 4e 4f 54 20 29 3b 0a 20 20 61 73 73 K_ISNOT );. ass
2070d 65 72 74 28 20 70 45 78 70 72 2d 3e 70 4c 65 66 ert( pExpr->pLef
2070e 74 20 29 3b 0a 20 20 61 66 66 20 3d 20 73 71 6c t );. aff = sql
2070f 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 ite3ExprAffinity
20710 28 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a (pExpr->pLeft);.
20711 20 20 69 66 28 20 70 45 78 70 72 2d 3e 70 52 69 if( pExpr->pRi
20712 67 68 74 20 29 7b 0a 20 20 20 20 61 66 66 20 3d ght ){. aff =
20713 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 sqlite3CompareA
20714 66 66 69 6e 69 74 79 28 70 45 78 70 72 2d 3e 70 ffinity(pExpr->p
20715 52 69 67 68 74 2c 20 61 66 66 29 3b 0a 20 20 7d Right, aff);. }
20716 65 6c 73 65 20 69 66 28 20 45 78 70 72 48 61 73 else if( ExprHas
20717 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 Property(pExpr,
20718 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b EP_xIsSelect) ){
20719 0a 20 20 20 20 61 66 66 20 3d 20 73 71 6c 69 74 . aff = sqlit
2071a 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 e3CompareAffinit
2071b 79 28 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 y(pExpr->x.pSele
2071c 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d ct->pEList->a[0]
2071d 2e 70 45 78 70 72 2c 20 61 66 66 29 3b 0a 20 20 .pExpr, aff);.
2071e 7d 65 6c 73 65 20 69 66 28 20 21 61 66 66 20 29 }else if( !aff )
2071f 7b 0a 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 {. aff = SQLI
20720 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 7d TE_AFF_NONE;. }
20721 0a 20 20 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d . return aff;.}
20722 0a 0a 2f 2a 0a 2a 2a 20 70 45 78 70 72 20 69 73 ../*.** pExpr is
20723 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 65 78 a comparison ex
20724 70 72 65 73 73 69 6f 6e 2c 20 65 67 2e 20 27 3d pression, eg. '=
20725 27 2c 20 27 3c 27 2c 20 49 4e 28 2e 2e 2e 29 20 ', '<', IN(...)
20726 65 74 63 2e 0a 2a 2a 20 69 64 78 5f 61 66 66 69 etc..** idx_affi
20727 6e 69 74 79 20 69 73 20 74 68 65 20 61 66 66 69 nity is the affi
20728 6e 69 74 79 20 6f 66 20 61 6e 20 69 6e 64 65 78 nity of an index
20729 65 64 20 63 6f 6c 75 6d 6e 2e 20 52 65 74 75 72 ed column. Retur
2072a 6e 20 74 72 75 65 0a 2a 2a 20 69 66 20 74 68 65 n true.** if the
2072b 20 69 6e 64 65 78 20 77 69 74 68 20 61 66 66 69 index with affi
2072c 6e 69 74 79 20 69 64 78 5f 61 66 66 69 6e 69 74 nity idx_affinit
2072d 79 20 6d 61 79 20 62 65 20 75 73 65 64 20 74 6f y may be used to
2072e 20 69 6d 70 6c 65 6d 65 6e 74 0a 2a 2a 20 74 68 implement.** th
2072f 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 6e 20 e comparison in
20730 70 45 78 70 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 pExpr..*/.SQLITE
20731 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
20732 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74 ite3IndexAffinit
20733 79 4f 6b 28 45 78 70 72 20 2a 70 45 78 70 72 2c yOk(Expr *pExpr,
20734 20 63 68 61 72 20 69 64 78 5f 61 66 66 69 6e 69 char idx_affini
20735 74 79 29 7b 0a 20 20 63 68 61 72 20 61 66 66 20 ty){. char aff
20736 3d 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 = comparisonAffi
20737 6e 69 74 79 28 70 45 78 70 72 29 3b 0a 20 20 73 nity(pExpr);. s
20738 77 69 74 63 68 28 20 61 66 66 20 29 7b 0a 20 20 witch( aff ){.
20739 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 46 case SQLITE_AF
2073a 46 5f 4e 4f 4e 45 3a 0a 20 20 20 20 20 20 72 65 F_NONE:. re
2073b 74 75 72 6e 20 31 3b 0a 20 20 20 20 63 61 73 65 turn 1;. case
2073c 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 SQLITE_AFF_TEXT
2073d 3a 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 :. return i
2073e 64 78 5f 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c dx_affinity==SQL
2073f 49 54 45 5f 41 46 46 5f 54 45 58 54 3b 0a 20 20 ITE_AFF_TEXT;.
20740 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 default:.
20741 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 49 return sqlite3I
20742 73 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 sNumericAffinity
20743 28 69 64 78 5f 61 66 66 69 6e 69 74 79 29 3b 0a (idx_affinity);.
20744 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 }.}../*.** Ret
20745 75 72 6e 20 74 68 65 20 50 35 20 76 61 6c 75 65 urn the P5 value
20746 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 that should be
20747 75 73 65 64 20 66 6f 72 20 61 20 62 69 6e 61 72 used for a binar
20748 79 20 63 6f 6d 70 61 72 69 73 6f 6e 0a 2a 2a 20 y comparison.**
20749 6f 70 63 6f 64 65 20 28 4f 50 5f 45 71 2c 20 4f opcode (OP_Eq, O
2074a 50 5f 47 65 20 65 74 63 2e 29 20 75 73 65 64 20 P_Ge etc.) used
2074b 74 6f 20 63 6f 6d 70 61 72 65 20 70 45 78 70 72 to compare pExpr
2074c 31 20 61 6e 64 20 70 45 78 70 72 32 2e 0a 2a 2f 1 and pExpr2..*/
2074d 0a 73 74 61 74 69 63 20 75 38 20 62 69 6e 61 72 .static u8 binar
2074e 79 43 6f 6d 70 61 72 65 50 35 28 45 78 70 72 20 yCompareP5(Expr
2074f 2a 70 45 78 70 72 31 2c 20 45 78 70 72 20 2a 70 *pExpr1, Expr *p
20750 45 78 70 72 32 2c 20 69 6e 74 20 6a 75 6d 70 49 Expr2, int jumpI
20751 66 4e 75 6c 6c 29 7b 0a 20 20 75 38 20 61 66 66 fNull){. u8 aff
20752 20 3d 20 28 63 68 61 72 29 73 71 6c 69 74 65 33 = (char)sqlite3
20753 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 45 78 ExprAffinity(pEx
20754 70 72 32 29 3b 0a 20 20 61 66 66 20 3d 20 28 75 pr2);. aff = (u
20755 38 29 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 8)sqlite3Compare
20756 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 31 2c Affinity(pExpr1,
20757 20 61 66 66 29 20 7c 20 28 75 38 29 6a 75 6d 70 aff) | (u8)jump
20758 49 66 4e 75 6c 6c 3b 0a 20 20 72 65 74 75 72 6e IfNull;. return
20759 20 61 66 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 aff;.}../*.** R
2075a 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 eturn a pointer
2075b 74 6f 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e to the collation
2075c 20 73 65 71 75 65 6e 63 65 20 74 68 61 74 20 73 sequence that s
2075d 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 62 79 hould be used by
2075e 0a 2a 2a 20 61 20 62 69 6e 61 72 79 20 63 6f 6d .** a binary com
2075f 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 parison operator
20760 20 63 6f 6d 70 61 72 69 6e 67 20 70 4c 65 66 74 comparing pLeft
20761 20 61 6e 64 20 70 52 69 67 68 74 2e 0a 2a 2a 0a and pRight..**.
20762 2a 2a 20 49 66 20 74 68 65 20 6c 65 66 74 20 68 ** If the left h
20763 61 6e 64 20 65 78 70 72 65 73 73 69 6f 6e 20 68 and expression h
20764 61 73 20 61 20 63 6f 6c 6c 61 74 69 6e 67 20 73 as a collating s
20765 65 71 75 65 6e 63 65 20 74 79 70 65 2c 20 74 68 equence type, th
20766 65 6e 20 69 74 20 69 73 0a 2a 2a 20 75 73 65 64 en it is.** used
20767 2e 20 4f 74 68 65 72 77 69 73 65 20 74 68 65 20 . Otherwise the
20768 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e collation sequen
20769 63 65 20 66 6f 72 20 74 68 65 20 72 69 67 68 74 ce for the right
2076a 20 68 61 6e 64 20 65 78 70 72 65 73 73 69 6f 6e hand expression
2076b 0a 2a 2a 20 69 73 20 75 73 65 64 2c 20 6f 72 20 .** is used, or
2076c 74 68 65 20 64 65 66 61 75 6c 74 20 28 42 49 4e the default (BIN
2076d 41 52 59 29 20 69 66 20 6e 65 69 74 68 65 72 20 ARY) if neither
2076e 65 78 70 72 65 73 73 69 6f 6e 20 68 61 73 20 61 expression has a
2076f 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 74 79 collating.** ty
20770 70 65 2e 0a 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65 pe..**.** Argume
20771 6e 74 20 70 52 69 67 68 74 20 28 62 75 74 20 6e nt pRight (but n
20772 6f 74 20 70 4c 65 66 74 29 20 6d 61 79 20 62 65 ot pLeft) may be
20773 20 61 20 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 2e a null pointer.
20774 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 0a 2a In this case,.*
20775 2a 20 69 74 20 69 73 20 6e 6f 74 20 63 6f 6e 73 * it is not cons
20776 69 64 65 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 idered..*/.SQLIT
20777 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 E_PRIVATE CollSe
20778 71 20 2a 73 71 6c 69 74 65 33 42 69 6e 61 72 79 q *sqlite3Binary
20779 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 0a CompareCollSeq(.
2077a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c Parse *pParse,
2077b 20 0a 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c . Expr *pLeft,
2077c 20 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 . Expr *pRight
2077d 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 .){. CollSeq *p
2077e 43 6f 6c 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 Coll;. assert(
2077f 70 4c 65 66 74 20 29 3b 0a 20 20 69 66 28 20 70 pLeft );. if( p
20780 4c 65 66 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 Left->flags & EP
20781 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 _ExpCollate ){.
20782 20 20 20 61 73 73 65 72 74 28 20 70 4c 65 66 74 assert( pLeft
20783 2d 3e 70 43 6f 6c 6c 20 29 3b 0a 20 20 20 20 70 ->pColl );. p
20784 43 6f 6c 6c 20 3d 20 70 4c 65 66 74 2d 3e 70 43 Coll = pLeft->pC
20785 6f 6c 6c 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 oll;. }else if(
20786 20 70 52 69 67 68 74 20 26 26 20 70 52 69 67 68 pRight && pRigh
20787 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 t->flags & EP_Ex
20788 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 pCollate ){.
20789 61 73 73 65 72 74 28 20 70 52 69 67 68 74 2d 3e assert( pRight->
2078a 70 43 6f 6c 6c 20 29 3b 0a 20 20 20 20 70 43 6f pColl );. pCo
2078b 6c 6c 20 3d 20 70 52 69 67 68 74 2d 3e 70 43 6f ll = pRight->pCo
2078c 6c 6c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ll;. }else{.
2078d 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 pColl = sqlite3
2078e 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 ExprCollSeq(pPar
2078f 73 65 2c 20 70 4c 65 66 74 29 3b 0a 20 20 20 20 se, pLeft);.
20790 69 66 28 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 if( !pColl ){.
20791 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 pColl = sqli
20792 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 te3ExprCollSeq(p
20793 50 61 72 73 65 2c 20 70 52 69 67 68 74 29 3b 0a Parse, pRight);.
20794 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
20795 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a rn pColl;.}../*.
20796 2a 2a 20 47 65 6e 65 72 61 74 65 20 74 68 65 20 ** Generate the
20797 6f 70 65 72 61 6e 64 73 20 66 6f 72 20 61 20 63 operands for a c
20798 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 omparison operat
20799 69 6f 6e 2e 20 20 42 65 66 6f 72 65 0a 2a 2a 20 ion. Before.**
2079a 67 65 6e 65 72 61 74 69 6e 67 20 74 68 65 20 63 generating the c
2079b 6f 64 65 20 66 6f 72 20 65 61 63 68 20 6f 70 65 ode for each ope
2079c 72 61 6e 64 2c 20 73 65 74 20 74 68 65 20 45 50 rand, set the EP
2079d 5f 41 6e 79 41 66 66 0a 2a 2a 20 66 6c 61 67 20 _AnyAff.** flag
2079e 6f 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f on the expressio
2079f 6e 20 73 6f 20 74 68 61 74 20 69 74 20 77 69 6c n so that it wil
207a0 6c 20 62 65 20 61 62 6c 65 20 74 6f 20 75 73 65 l be able to use
207a1 64 20 61 0a 2a 2a 20 63 61 63 68 65 64 20 63 6f d a.** cached co
207a2 6c 75 6d 6e 20 76 61 6c 75 65 20 74 68 61 74 20 lumn value that
207a3 68 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 75 has previously u
207a4 6e 64 65 72 67 6f 6e 65 20 61 6e 0a 2a 2a 20 61 ndergone an.** a
207a5 66 66 69 6e 69 74 79 20 63 68 61 6e 67 65 2e 0a ffinity change..
207a6 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 */.static void c
207a7 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e odeCompareOperan
207a8 64 73 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 ds(. Parse *pPa
207a9 72 73 65 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 rse, /* Parsi
207aa 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 ng and code gene
207ab 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a rating context *
207ac 2f 0a 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c /. Expr *pLeft,
207ad 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 65 66 /* The lef
207ae 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 t operand */. i
207af 6e 74 20 2a 70 52 65 67 4c 65 66 74 2c 20 20 20 nt *pRegLeft,
207b0 20 2f 2a 20 52 65 67 69 73 74 65 72 20 77 68 65 /* Register whe
207b1 72 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 re left operand
207b2 69 73 20 73 74 6f 72 65 64 20 2a 2f 0a 20 20 69 is stored */. i
207b3 6e 74 20 2a 70 46 72 65 65 4c 65 66 74 2c 20 20 nt *pFreeLeft,
207b4 20 2f 2a 20 46 72 65 65 20 74 68 69 73 20 72 65 /* Free this re
207b5 67 69 73 74 65 72 20 77 68 65 6e 20 64 6f 6e 65 gister when done
207b6 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 67 */. Expr *pRig
207b7 68 74 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 72 ht, /* The r
207b8 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a ight operand */.
207b9 20 20 69 6e 74 20 2a 70 52 65 67 52 69 67 68 74 int *pRegRight
207ba 2c 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 , /* Register
207bb 77 68 65 72 65 20 72 69 67 68 74 20 6f 70 65 72 where right oper
207bc 61 6e 64 20 69 73 20 73 74 6f 72 65 64 20 2a 2f and is stored */
207bd 0a 20 20 69 6e 74 20 2a 70 46 72 65 65 52 69 67 . int *pFreeRig
207be 68 74 20 20 20 2f 2a 20 57 72 69 74 65 20 74 65 ht /* Write te
207bf 6d 70 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 mp register for
207c0 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20 74 68 right operand th
207c1 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 77 68 69 6c ere */.){. whil
207c2 65 28 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b e( pLeft->op==TK
207c3 5f 55 50 4c 55 53 20 29 20 70 4c 65 66 74 20 3d _UPLUS ) pLeft =
207c4 20 70 4c 65 66 74 2d 3e 70 4c 65 66 74 3b 0a 20 pLeft->pLeft;.
207c5 20 70 4c 65 66 74 2d 3e 66 6c 61 67 73 20 7c 3d pLeft->flags |=
207c6 20 45 50 5f 41 6e 79 41 66 66 3b 0a 20 20 2a 70 EP_AnyAff;. *p
207c7 52 65 67 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 RegLeft = sqlite
207c8 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 3ExprCodeTemp(pP
207c9 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 70 46 72 arse, pLeft, pFr
207ca 65 65 4c 65 66 74 29 3b 0a 20 20 77 68 69 6c 65 eeLeft);. while
207cb 28 20 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b ( pRight->op==TK
207cc 5f 55 50 4c 55 53 20 29 20 70 52 69 67 68 74 20 _UPLUS ) pRight
207cd 3d 20 70 52 69 67 68 74 2d 3e 70 4c 65 66 74 3b = pRight->pLeft;
207ce 0a 20 20 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 . pRight->flags
207cf 20 7c 3d 20 45 50 5f 41 6e 79 41 66 66 3b 0a 20 |= EP_AnyAff;.
207d0 20 2a 70 52 65 67 52 69 67 68 74 20 3d 20 73 71 *pRegRight = sq
207d1 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d lite3ExprCodeTem
207d2 70 28 70 50 61 72 73 65 2c 20 70 52 69 67 68 74 p(pParse, pRight
207d3 2c 20 70 46 72 65 65 52 69 67 68 74 29 3b 0a 7d , pFreeRight);.}
207d4 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 ../*.** Generate
207d5 20 63 6f 64 65 20 66 6f 72 20 61 20 63 6f 6d 70 code for a comp
207d6 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e arison operator.
207d7 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 .*/.static int c
207d8 6f 64 65 43 6f 6d 70 61 72 65 28 0a 20 20 50 61 odeCompare(. Pa
207d9 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 rse *pParse,
207da 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 28 /* The parsing (
207db 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74 and code generat
207dc 69 6e 67 29 20 63 6f 6e 74 65 78 74 20 2a 2f 0a ing) context */.
207dd 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 20 Expr *pLeft,
207de 20 20 20 20 2f 2a 20 54 68 65 20 6c 65 66 74 20 /* The left
207df 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 45 78 70 operand */. Exp
207e0 72 20 2a 70 52 69 67 68 74 2c 20 20 20 20 20 2f r *pRight, /
207e1 2a 20 54 68 65 20 72 69 67 68 74 20 6f 70 65 72 * The right oper
207e2 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 63 and */. int opc
207e3 6f 64 65 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 ode, /* Th
207e4 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 63 e comparison opc
207e5 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 31 ode */. int in1
207e6 2c 20 69 6e 74 20 69 6e 32 2c 20 2f 2a 20 52 65 , int in2, /* Re
207e7 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 6f gister holding o
207e8 70 65 72 61 6e 64 73 20 2a 2f 0a 20 20 69 6e 74 perands */. int
207e9 20 64 65 73 74 2c 20 20 20 20 20 20 20 20 20 2f dest, /
207ea 2a 20 4a 75 6d 70 20 68 65 72 65 20 69 66 20 74 * Jump here if t
207eb 72 75 65 2e 20 20 2a 2f 0a 20 20 69 6e 74 20 6a rue. */. int j
207ec 75 6d 70 49 66 4e 75 6c 6c 20 20 20 20 2f 2a 20 umpIfNull /*
207ed 49 66 20 74 72 75 65 2c 20 6a 75 6d 70 20 69 66 If true, jump if
207ee 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 either operand
207ef 69 73 20 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 is NULL */.){.
207f0 69 6e 74 20 70 35 3b 0a 20 20 69 6e 74 20 61 64 int p5;. int ad
207f1 64 72 3b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 dr;. CollSeq *p
207f2 34 3b 0a 0a 20 20 70 34 20 3d 20 73 71 6c 69 74 4;.. p4 = sqlit
207f3 65 33 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 e3BinaryCompareC
207f4 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 ollSeq(pParse, p
207f5 4c 65 66 74 2c 20 70 52 69 67 68 74 29 3b 0a 20 Left, pRight);.
207f6 20 70 35 20 3d 20 62 69 6e 61 72 79 43 6f 6d 70 p5 = binaryComp
207f7 61 72 65 50 35 28 70 4c 65 66 74 2c 20 70 52 69 areP5(pLeft, pRi
207f8 67 68 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 ght, jumpIfNull)
207f9 3b 0a 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 ;. addr = sqlit
207fa 65 33 56 64 62 65 41 64 64 4f 70 34 28 70 50 61 e3VdbeAddOp4(pPa
207fb 72 73 65 2d 3e 70 56 64 62 65 2c 20 6f 70 63 6f rse->pVdbe, opco
207fc 64 65 2c 20 69 6e 32 2c 20 64 65 73 74 2c 20 69 de, in2, dest, i
207fd 6e 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 n1,.
207fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 (
207ff 76 6f 69 64 2a 29 70 34 2c 20 50 34 5f 43 4f 4c void*)p4, P4_COL
20800 4c 53 45 51 29 3b 0a 20 20 73 71 6c 69 74 65 33 LSEQ);. sqlite3
20801 56 64 62 65 43 68 61 6e 67 65 50 35 28 70 50 61 VdbeChangeP5(pPa
20802 72 73 65 2d 3e 70 56 64 62 65 2c 20 28 75 38 29 rse->pVdbe, (u8)
20803 70 35 29 3b 0a 20 20 69 66 28 20 28 70 35 20 26 p5);. if( (p5 &
20804 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b SQLITE_AFF_MASK
20805 29 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f )!=SQLITE_AFF_NO
20806 4e 45 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 NE ){. sqlite
20807 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 3ExprCacheAffini
20808 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c tyChange(pParse,
20809 20 69 6e 31 2c 20 31 29 3b 0a 20 20 20 20 73 71 in1, 1);. sq
2080a 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66 lite3ExprCacheAf
2080b 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 finityChange(pPa
2080c 72 73 65 2c 20 69 6e 32 2c 20 31 29 3b 0a 20 20 rse, in2, 1);.
2080d 7d 0a 20 20 72 65 74 75 72 6e 20 61 64 64 72 3b }. return addr;
2080e 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d .}..#if SQLITE_M
2080f 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 0a AX_EXPR_DEPTH>0.
20810 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 /*.** Check that
20811 20 61 72 67 75 6d 65 6e 74 20 6e 48 65 69 67 68 argument nHeigh
20812 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f t is less than o
20813 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6d r equal to the m
20814 61 78 69 6d 75 6d 0a 2a 2a 20 65 78 70 72 65 73 aximum.** expres
20815 73 69 6f 6e 20 64 65 70 74 68 20 61 6c 6c 6f 77 sion depth allow
20816 65 64 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 74 ed. If it is not
20817 2c 20 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 , leave an error
20818 20 6d 65 73 73 61 67 65 20 69 6e 0a 2a 2a 20 70 message in.** p
20819 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 Parse..*/.SQLITE
2081a 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
2081b 69 74 65 33 45 78 70 72 43 68 65 63 6b 48 65 69 ite3ExprCheckHei
2081c 67 68 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 ght(Parse *pPars
2081d 65 2c 20 69 6e 74 20 6e 48 65 69 67 68 74 29 7b e, int nHeight){
2081e 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
2081f 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6d 78 48 TE_OK;. int mxH
20820 65 69 67 68 74 20 3d 20 70 50 61 72 73 65 2d 3e eight = pParse->
20821 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 db->aLimit[SQLIT
20822 45 5f 4c 49 4d 49 54 5f 45 58 50 52 5f 44 45 50 E_LIMIT_EXPR_DEP
20823 54 48 5d 3b 0a 20 20 69 66 28 20 6e 48 65 69 67 TH];. if( nHeig
20824 68 74 3e 6d 78 48 65 69 67 68 74 20 29 7b 0a 20 ht>mxHeight ){.
20825 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
20826 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 sg(pParse, .
20827 20 20 20 22 45 78 70 72 65 73 73 69 6f 6e 20 74 "Expression t
20828 72 65 65 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 ree is too large
20829 20 28 6d 61 78 69 6d 75 6d 20 64 65 70 74 68 20 (maximum depth
2082a 25 64 29 22 2c 20 6d 78 48 65 69 67 68 74 0a 20 %d)", mxHeight.
2082b 20 20 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 );. rc = S
2082c 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d QLITE_ERROR;. }
2082d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
2082e 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e ./* The followin
2082f 67 20 74 68 72 65 65 20 66 75 6e 63 74 69 6f 6e g three function
20830 73 2c 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 s, heightOfExpr(
20831 29 2c 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c ), heightOfExprL
20832 69 73 74 28 29 0a 2a 2a 20 61 6e 64 20 68 65 69 ist().** and hei
20833 67 68 74 4f 66 53 65 6c 65 63 74 28 29 2c 20 61 ghtOfSelect(), a
20834 72 65 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 re used to deter
20835 6d 69 6e 65 20 74 68 65 20 6d 61 78 69 6d 75 6d mine the maximum
20836 20 68 65 69 67 68 74 0a 2a 2a 20 6f 66 20 61 6e height.** of an
20837 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 y expression tre
20838 65 20 72 65 66 65 72 65 6e 63 65 64 20 62 79 20 e referenced by
20839 74 68 65 20 73 74 72 75 63 74 75 72 65 20 70 61 the structure pa
2083a 73 73 65 64 20 61 73 20 74 68 65 0a 2a 2a 20 66 ssed as the.** f
2083b 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a irst argument..*
2083c 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 6d 61 78 *.** If this max
2083d 69 6d 75 6d 20 68 65 69 67 68 74 20 69 73 20 67 imum height is g
2083e 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 reater than the
2083f 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 70 6f current value po
20840 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 62 79 20 70 inted.** to by p
20841 6e 48 65 69 67 68 74 2c 20 74 68 65 20 73 65 63 nHeight, the sec
20842 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 2c 20 74 ond parameter, t
20843 68 65 6e 20 73 65 74 20 2a 70 6e 48 65 69 67 68 hen set *pnHeigh
20844 74 20 74 6f 20 74 68 61 74 0a 2a 2a 20 76 61 6c t to that.** val
20845 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ue..*/.static vo
20846 69 64 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 id heightOfExpr(
20847 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 2a 70 6e Expr *p, int *pn
20848 48 65 69 67 68 74 29 7b 0a 20 20 69 66 28 20 70 Height){. if( p
20849 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e ){. if( p->n
2084a 48 65 69 67 68 74 3e 2a 70 6e 48 65 69 67 68 74 Height>*pnHeight
2084b 20 29 7b 0a 20 20 20 20 20 20 2a 70 6e 48 65 69 ){. *pnHei
2084c 67 68 74 20 3d 20 70 2d 3e 6e 48 65 69 67 68 74 ght = p->nHeight
2084d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 ;. }. }.}.st
2084e 61 74 69 63 20 76 6f 69 64 20 68 65 69 67 68 74 atic void height
2084f 4f 66 45 78 70 72 4c 69 73 74 28 45 78 70 72 4c OfExprList(ExprL
20850 69 73 74 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 ist *p, int *pnH
20851 65 69 67 68 74 29 7b 0a 20 20 69 66 28 20 70 20 eight){. if( p
20852 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 ){. int i;.
20853 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e for(i=0; i<p->
20854 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 nExpr; i++){.
20855 20 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 heightOfExpr(
20856 70 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 2c 20 70 p->a[i].pExpr, p
20857 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a nHeight);. }.
20858 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 }.}.static voi
20859 64 20 68 65 69 67 68 74 4f 66 53 65 6c 65 63 74 d heightOfSelect
2085a 28 53 65 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20 (Select *p, int
2085b 2a 70 6e 48 65 69 67 68 74 29 7b 0a 20 20 69 66 *pnHeight){. if
2085c 28 20 70 20 29 7b 0a 20 20 20 20 68 65 69 67 68 ( p ){. heigh
2085d 74 4f 66 45 78 70 72 28 70 2d 3e 70 57 68 65 72 tOfExpr(p->pWher
2085e 65 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 e, pnHeight);.
2085f 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 70 heightOfExpr(p
20860 2d 3e 70 48 61 76 69 6e 67 2c 20 70 6e 48 65 69 ->pHaving, pnHei
20861 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 ght);. height
20862 4f 66 45 78 70 72 28 70 2d 3e 70 4c 69 6d 69 74 OfExpr(p->pLimit
20863 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 , pnHeight);.
20864 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d heightOfExpr(p-
20865 3e 70 4f 66 66 73 65 74 2c 20 70 6e 48 65 69 67 >pOffset, pnHeig
20866 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f ht);. heightO
20867 66 45 78 70 72 4c 69 73 74 28 70 2d 3e 70 45 4c fExprList(p->pEL
20868 69 73 74 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a ist, pnHeight);.
20869 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 heightOfExpr
2086a 4c 69 73 74 28 70 2d 3e 70 47 72 6f 75 70 42 79 List(p->pGroupBy
2086b 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 , pnHeight);.
2086c 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 heightOfExprLis
2086d 74 28 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20 70 t(p->pOrderBy, p
2086e 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 nHeight);. he
2086f 69 67 68 74 4f 66 53 65 6c 65 63 74 28 70 2d 3e ightOfSelect(p->
20870 70 50 72 69 6f 72 2c 20 70 6e 48 65 69 67 68 74 pPrior, pnHeight
20871 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
20872 53 65 74 20 74 68 65 20 45 78 70 72 2e 6e 48 65 Set the Expr.nHe
20873 69 67 68 74 20 76 61 72 69 61 62 6c 65 20 69 6e ight variable in
20874 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20 70 the structure p
20875 61 73 73 65 64 20 61 73 20 61 6e 20 0a 2a 2a 20 assed as an .**
20876 61 72 67 75 6d 65 6e 74 2e 20 41 6e 20 65 78 70 argument. An exp
20877 72 65 73 73 69 6f 6e 20 77 69 74 68 20 6e 6f 20 ression with no
20878 63 68 69 6c 64 72 65 6e 2c 20 45 78 70 72 2e 70 children, Expr.p
20879 4c 69 73 74 20 6f 72 20 0a 2a 2a 20 45 78 70 72 List or .** Expr
2087a 2e 70 53 65 6c 65 63 74 20 6d 65 6d 62 65 72 20 .pSelect member
2087b 68 61 73 20 61 20 68 65 69 67 68 74 20 6f 66 20 has a height of
2087c 31 2e 20 41 6e 79 20 6f 74 68 65 72 20 65 78 70 1. Any other exp
2087d 72 65 73 73 69 6f 6e 0a 2a 2a 20 68 61 73 20 61 ression.** has a
2087e 20 68 65 69 67 68 74 20 65 71 75 61 6c 20 74 6f height equal to
2087f 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 69 the maximum hei
20880 67 68 74 20 6f 66 20 61 6e 79 20 6f 74 68 65 72 ght of any other
20881 20 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 64 20 .** referenced
20882 45 78 70 72 20 70 6c 75 73 20 6f 6e 65 2e 0a 2a Expr plus one..*
20883 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 65 78 /.static void ex
20884 70 72 53 65 74 48 65 69 67 68 74 28 45 78 70 72 prSetHeight(Expr
20885 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 48 65 69 *p){. int nHei
20886 67 68 74 20 3d 20 30 3b 0a 20 20 68 65 69 67 68 ght = 0;. heigh
20887 74 4f 66 45 78 70 72 28 70 2d 3e 70 4c 65 66 74 tOfExpr(p->pLeft
20888 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 68 , &nHeight);. h
20889 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 eightOfExpr(p->p
2088a 52 69 67 68 74 2c 20 26 6e 48 65 69 67 68 74 29 Right, &nHeight)
2088b 3b 0a 20 20 69 66 28 20 45 78 70 72 48 61 73 50 ;. if( ExprHasP
2088c 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 78 49 roperty(p, EP_xI
2088d 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 sSelect) ){.
2088e 68 65 69 67 68 74 4f 66 53 65 6c 65 63 74 28 70 heightOfSelect(p
2088f 2d 3e 78 2e 70 53 65 6c 65 63 74 2c 20 26 6e 48 ->x.pSelect, &nH
20890 65 69 67 68 74 29 3b 0a 20 20 7d 65 6c 73 65 7b eight);. }else{
20891 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 70 . heightOfExp
20892 72 4c 69 73 74 28 70 2d 3e 78 2e 70 4c 69 73 74 rList(p->x.pList
20893 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 7d , &nHeight);. }
20894 0a 20 20 70 2d 3e 6e 48 65 69 67 68 74 20 3d 20 . p->nHeight =
20895 6e 48 65 69 67 68 74 20 2b 20 31 3b 0a 7d 0a 0a nHeight + 1;.}..
20896 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 45 78 /*.** Set the Ex
20897 70 72 2e 6e 48 65 69 67 68 74 20 76 61 72 69 61 pr.nHeight varia
20898 62 6c 65 20 75 73 69 6e 67 20 74 68 65 20 65 78 ble using the ex
20899 70 72 53 65 74 48 65 69 67 68 74 28 29 20 66 75 prSetHeight() fu
2089a 6e 63 74 69 6f 6e 2e 20 49 66 0a 2a 2a 20 74 68 nction. If.** th
2089b 65 20 68 65 69 67 68 74 20 69 73 20 67 72 65 61 e height is grea
2089c 74 65 72 20 74 68 61 6e 20 74 68 65 20 6d 61 78 ter than the max
2089d 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 65 78 70 imum allowed exp
2089e 72 65 73 73 69 6f 6e 20 64 65 70 74 68 2c 0a 2a ression depth,.*
2089f 2a 20 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 * leave an error
208a0 20 69 6e 20 70 50 61 72 73 65 2e 0a 2a 2f 0a 53 in pParse..*/.S
208a1 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
208a2 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 id sqlite3ExprSe
208a3 74 48 65 69 67 68 74 28 50 61 72 73 65 20 2a 70 tHeight(Parse *p
208a4 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 29 7b Parse, Expr *p){
208a5 0a 20 20 65 78 70 72 53 65 74 48 65 69 67 68 74 . exprSetHeight
208a6 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 (p);. sqlite3Ex
208a7 70 72 43 68 65 63 6b 48 65 69 67 68 74 28 70 50 prCheckHeight(pP
208a8 61 72 73 65 2c 20 70 2d 3e 6e 48 65 69 67 68 74 arse, p->nHeight
208a9 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 );.}../*.** Retu
208aa 72 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 rn the maximum h
208ab 65 69 67 68 74 20 6f 66 20 61 6e 79 20 65 78 70 eight of any exp
208ac 72 65 73 73 69 6f 6e 20 74 72 65 65 20 72 65 66 ression tree ref
208ad 65 72 65 6e 63 65 64 0a 2a 2a 20 62 79 20 74 68 erenced.** by th
208ae 65 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65 e select stateme
208af 6e 74 20 70 61 73 73 65 64 20 61 73 20 61 6e 20 nt passed as an
208b0 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c argument..*/.SQL
208b1 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
208b2 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 sqlite3SelectExp
208b3 72 48 65 69 67 68 74 28 53 65 6c 65 63 74 20 2a rHeight(Select *
208b4 70 29 7b 0a 20 20 69 6e 74 20 6e 48 65 69 67 68 p){. int nHeigh
208b5 74 20 3d 20 30 3b 0a 20 20 68 65 69 67 68 74 4f t = 0;. heightO
208b6 66 53 65 6c 65 63 74 28 70 2c 20 26 6e 48 65 69 fSelect(p, &nHei
208b7 67 68 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e ght);. return n
208b8 48 65 69 67 68 74 3b 0a 7d 0a 23 65 6c 73 65 0a Height;.}.#else.
208b9 20 20 23 64 65 66 69 6e 65 20 65 78 70 72 53 65 #define exprSe
208ba 74 48 65 69 67 68 74 28 79 29 0a 23 65 6e 64 69 tHeight(y).#endi
208bb 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 41 58 5f f /* SQLITE_MAX_
208bc 45 58 50 52 5f 44 45 50 54 48 3e 30 20 2a 2f 0a EXPR_DEPTH>0 */.
208bd 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
208be 69 6e 65 20 69 73 20 74 68 65 20 63 6f 72 65 20 ine is the core
208bf 61 6c 6c 6f 63 61 74 6f 72 20 66 6f 72 20 45 78 allocator for Ex
208c0 70 72 20 6e 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 pr nodes..**.**
208c1 43 6f 6e 73 74 72 75 63 74 20 61 20 6e 65 77 20 Construct a new
208c2 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20 expression node
208c3 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69 and return a poi
208c4 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 4d 65 6d nter to it. Mem
208c5 6f 72 79 0a 2a 2a 20 66 6f 72 20 74 68 69 73 20 ory.** for this
208c6 6e 6f 64 65 20 61 6e 64 20 66 6f 72 20 74 68 65 node and for the
208c7 20 70 54 6f 6b 65 6e 20 61 72 67 75 6d 65 6e 74 pToken argument
208c8 20 69 73 20 61 20 73 69 6e 67 6c 65 20 61 6c 6c is a single all
208c9 6f 63 61 74 69 6f 6e 0a 2a 2a 20 6f 62 74 61 69 ocation.** obtai
208ca 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 ned from sqlite3
208cb 44 62 4d 61 6c 6c 6f 63 28 29 2e 20 20 54 68 65 DbMalloc(). The
208cc 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f calling functio
208cd 6e 0a 2a 2a 20 69 73 20 72 65 73 70 6f 6e 73 69 n.** is responsi
208ce 62 6c 65 20 66 6f 72 20 6d 61 6b 69 6e 67 20 73 ble for making s
208cf 75 72 65 20 74 68 65 20 6e 6f 64 65 20 65 76 65 ure the node eve
208d0 6e 74 75 61 6c 6c 79 20 67 65 74 73 20 66 72 65 ntually gets fre
208d1 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 64 65 71 ed..**.** If deq
208d2 75 6f 74 65 20 69 73 20 74 72 75 65 2c 20 74 68 uote is true, th
208d3 65 6e 20 74 68 65 20 74 6f 6b 65 6e 20 28 69 66 en the token (if
208d4 20 69 74 20 65 78 69 73 74 73 29 20 69 73 20 64 it exists) is d
208d5 65 71 75 6f 74 65 64 2e 0a 2a 2a 20 49 66 20 64 equoted..** If d
208d6 65 71 75 6f 74 65 20 69 73 20 66 61 6c 73 65 2c equote is false,
208d7 20 6e 6f 20 64 65 71 75 6f 74 69 6e 67 20 69 73 no dequoting is
208d8 20 70 65 72 66 6f 72 6d 61 6e 63 65 2e 20 20 54 performance. T
208d9 68 65 20 64 65 51 75 6f 74 65 0a 2a 2a 20 70 61 he deQuote.** pa
208da 72 61 6d 65 74 65 72 20 69 73 20 69 67 6e 6f 72 rameter is ignor
208db 65 64 20 69 66 20 70 54 6f 6b 65 6e 20 69 73 20 ed if pToken is
208dc 4e 55 4c 4c 20 6f 72 20 69 66 20 74 68 65 20 74 NULL or if the t
208dd 6f 6b 65 6e 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a oken does not.**
208de 20 61 70 70 65 61 72 20 74 6f 20 62 65 20 71 75 appear to be qu
208df 6f 74 65 64 2e 20 20 49 66 20 74 68 65 20 71 75 oted. If the qu
208e0 6f 74 65 73 20 77 65 72 65 20 6f 66 20 74 68 65 otes were of the
208e1 20 66 6f 72 6d 20 22 2e 2e 2e 22 20 28 64 6f 75 form "..." (dou
208e2 62 6c 65 2d 71 75 6f 74 65 73 29 0a 2a 2a 20 74 ble-quotes).** t
208e3 68 65 6e 20 74 68 65 20 45 50 5f 44 62 6c 51 75 hen the EP_DblQu
208e4 6f 74 65 64 20 66 6c 61 67 20 69 73 20 73 65 74 oted flag is set
208e5 20 6f 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 on the expressi
208e6 6f 6e 20 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53 on node..**.** S
208e7 70 65 63 69 61 6c 20 63 61 73 65 3a 20 20 49 66 pecial case: If
208e8 20 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 op==TK_INTEGER
208e9 61 6e 64 20 70 54 6f 6b 65 6e 20 70 6f 69 6e 74 and pToken point
208ea 73 20 74 6f 20 61 20 73 74 72 69 6e 67 20 74 68 s to a string th
208eb 61 74 0a 2a 2a 20 63 61 6e 20 62 65 20 74 72 61 at.** can be tra
208ec 6e 73 6c 61 74 65 64 20 69 6e 74 6f 20 61 20 33 nslated into a 3
208ed 32 2d 62 69 74 20 69 6e 74 65 67 65 72 2c 20 74 2-bit integer, t
208ee 68 65 6e 20 74 68 65 20 74 6f 6b 65 6e 20 69 73 hen the token is
208ef 20 6e 6f 74 0a 2a 2a 20 73 74 6f 72 65 64 20 69 not.** stored i
208f0 6e 20 75 2e 7a 54 6f 6b 65 6e 2e 20 20 49 6e 73 n u.zToken. Ins
208f1 74 65 61 64 2c 20 74 68 65 20 69 6e 74 65 67 65 tead, the intege
208f2 72 20 76 61 6c 75 65 73 20 69 73 20 77 72 69 74 r values is writ
208f3 74 65 6e 0a 2a 2a 20 69 6e 74 6f 20 75 2e 69 56 ten.** into u.iV
208f4 61 6c 75 65 20 61 6e 64 20 74 68 65 20 45 50 5f alue and the EP_
208f5 49 6e 74 56 61 6c 75 65 20 66 6c 61 67 20 69 73 IntValue flag is
208f6 20 73 65 74 2e 20 20 4e 6f 20 65 78 74 72 61 20 set. No extra
208f7 73 74 6f 72 61 67 65 0a 2a 2a 20 69 73 20 61 6c storage.** is al
208f8 6c 6f 63 61 74 65 64 20 74 6f 20 68 6f 6c 64 20 located to hold
208f9 74 68 65 20 69 6e 74 65 67 65 72 20 74 65 78 74 the integer text
208fa 20 61 6e 64 20 74 68 65 20 64 65 71 75 6f 74 65 and the dequote
208fb 20 66 6c 61 67 20 69 73 20 69 67 6e 6f 72 65 64 flag is ignored
208fc 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
208fd 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 ATE Expr *sqlite
208fe 33 45 78 70 72 41 6c 6c 6f 63 28 0a 20 20 73 71 3ExprAlloc(. sq
208ff 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 lite3 *db,
20900 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 /* Handle
20901 66 6f 72 20 73 71 6c 69 74 65 33 44 62 4d 61 6c for sqlite3DbMal
20902 6c 6f 63 5a 65 72 6f 28 29 20 28 6d 61 79 20 62 locZero() (may b
20903 65 20 6e 75 6c 6c 29 20 2a 2f 0a 20 20 69 6e 74 e null) */. int
20904 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 op,
20905 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 /* Expressi
20906 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 63 on opcode */. c
20907 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b onst Token *pTok
20908 65 6e 2c 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 en, /* Token
20909 61 72 67 75 6d 65 6e 74 2e 20 20 4d 69 67 68 74 argument. Might
2090a 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e be NULL */. in
2090b 74 20 64 65 71 75 6f 74 65 20 20 20 20 20 20 20 t dequote
2090c 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f /* True to
2090d 20 64 65 71 75 6f 74 65 20 2a 2f 0a 29 7b 0a 20 dequote */.){.
2090e 20 45 78 70 72 20 2a 70 4e 65 77 3b 0a 20 20 69 Expr *pNew;. i
2090f 6e 74 20 6e 45 78 74 72 61 20 3d 20 30 3b 0a 20 nt nExtra = 0;.
20910 20 69 6e 74 20 69 56 61 6c 75 65 20 3d 20 30 3b int iValue = 0;
20911 0a 0a 20 20 69 66 28 20 70 54 6f 6b 65 6e 20 29 .. if( pToken )
20912 7b 0a 20 20 20 20 69 66 28 20 6f 70 21 3d 54 4b {. if( op!=TK
20913 5f 49 4e 54 45 47 45 52 20 7c 7c 20 70 54 6f 6b _INTEGER || pTok
20914 65 6e 2d 3e 7a 3d 3d 30 0a 20 20 20 20 20 20 20 en->z==0.
20915 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 47 65 74 || sqlite3Get
20916 49 6e 74 33 32 28 70 54 6f 6b 65 6e 2d 3e 7a 2c Int32(pToken->z,
20917 20 26 69 56 61 6c 75 65 29 3d 3d 30 20 29 7b 0a &iValue)==0 ){.
20918 20 20 20 20 20 20 6e 45 78 74 72 61 20 3d 20 70 nExtra = p
20919 54 6f 6b 65 6e 2d 3e 6e 2b 31 3b 0a 20 20 20 20 Token->n+1;.
2091a 7d 0a 20 20 7d 0a 20 20 70 4e 65 77 20 3d 20 73 }. }. pNew = s
2091b 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 qlite3DbMallocZe
2091c 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 45 78 ro(db, sizeof(Ex
2091d 70 72 29 2b 6e 45 78 74 72 61 29 3b 0a 20 20 69 pr)+nExtra);. i
2091e 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 70 f( pNew ){. p
2091f 4e 65 77 2d 3e 6f 70 20 3d 20 28 75 38 29 6f 70 New->op = (u8)op
20920 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 69 41 67 67 ;. pNew->iAgg
20921 20 3d 20 2d 31 3b 0a 20 20 20 20 69 66 28 20 70 = -1;. if( p
20922 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 20 20 69 Token ){. i
20923 66 28 20 6e 45 78 74 72 61 3d 3d 30 20 29 7b 0a f( nExtra==0 ){.
20924 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c pNew->fl
20925 61 67 73 20 7c 3d 20 45 50 5f 49 6e 74 56 61 6c ags |= EP_IntVal
20926 75 65 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 ue;. pNew
20927 2d 3e 75 2e 69 56 61 6c 75 65 20 3d 20 69 56 61 ->u.iValue = iVa
20928 6c 75 65 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 lue;. }else
20929 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 63 3b {. int c;
2092a 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 75 . pNew->u
2092b 2e 7a 54 6f 6b 65 6e 20 3d 20 28 63 68 61 72 2a .zToken = (char*
2092c 29 26 70 4e 65 77 5b 31 5d 3b 0a 20 20 20 20 20 )&pNew[1];.
2092d 20 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 2d 3e memcpy(pNew->
2092e 75 2e 7a 54 6f 6b 65 6e 2c 20 70 54 6f 6b 65 6e u.zToken, pToken
2092f 2d 3e 7a 2c 20 70 54 6f 6b 65 6e 2d 3e 6e 29 3b ->z, pToken->n);
20930 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 75 . pNew->u
20931 2e 7a 54 6f 6b 65 6e 5b 70 54 6f 6b 65 6e 2d 3e .zToken[pToken->
20932 6e 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 n] = 0;.
20933 69 66 28 20 64 65 71 75 6f 74 65 20 26 26 20 6e if( dequote && n
20934 45 78 74 72 61 3e 3d 33 20 0a 20 20 20 20 20 20 Extra>=3 .
20935 20 20 20 20 20 20 20 26 26 20 28 28 63 20 3d 20 && ((c =
20936 70 54 6f 6b 65 6e 2d 3e 7a 5b 30 5d 29 3d 3d 27 pToken->z[0])=='
20937 5c 27 27 20 7c 7c 20 63 3d 3d 27 22 27 20 7c 7c \'' || c=='"' ||
20938 20 63 3d 3d 27 5b 27 20 7c 7c 20 63 3d 3d 27 60 c=='[' || c=='`
20939 27 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ') ){.
2093a 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28 70 sqlite3Dequote(p
2093b 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a New->u.zToken);.
2093c 20 20 20 20 20 20 20 20 20 20 69 66 28 20 63 3d if( c=
2093d 3d 27 22 27 20 29 20 70 4e 65 77 2d 3e 66 6c 61 ='"' ) pNew->fla
2093e 67 73 20 7c 3d 20 45 50 5f 44 62 6c 51 75 6f 74 gs |= EP_DblQuot
2093f 65 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ed;. }.
20940 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 20 }. }.#if
20941 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f SQLITE_MAX_EXPR_
20942 44 45 50 54 48 3e 30 0a 20 20 20 20 70 4e 65 77 DEPTH>0. pNew
20943 2d 3e 6e 48 65 69 67 68 74 20 3d 20 31 3b 0a 23 ->nHeight = 1;.#
20944 65 6e 64 69 66 20 20 0a 20 20 7d 0a 20 20 72 65 endif . }. re
20945 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a turn pNew;.}../*
20946 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e .** Allocate a n
20947 65 77 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f ew expression no
20948 64 65 20 66 72 6f 6d 20 61 20 7a 65 72 6f 2d 74 de from a zero-t
20949 65 72 6d 69 6e 61 74 65 64 20 74 6f 6b 65 6e 20 erminated token
2094a 74 68 61 74 20 68 61 73 0a 2a 2a 20 61 6c 72 65 that has.** alre
2094b 61 64 79 20 62 65 65 6e 20 64 65 71 75 6f 74 65 ady been dequote
2094c 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
2094d 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 VATE Expr *sqlit
2094e 65 33 45 78 70 72 28 0a 20 20 73 71 6c 69 74 65 e3Expr(. sqlite
2094f 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 3 *db,
20950 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72 20 /* Handle for
20951 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a sqlite3DbMallocZ
20952 65 72 6f 28 29 20 28 6d 61 79 20 62 65 20 6e 75 ero() (may be nu
20953 6c 6c 29 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c ll) */. int op,
20954 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20955 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 6f /* Expression o
20956 70 63 6f 64 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 pcode */. const
20957 20 63 68 61 72 20 2a 7a 54 6f 6b 65 6e 20 20 20 char *zToken
20958 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 61 72 67 75 /* Token argu
20959 6d 65 6e 74 2e 20 20 4d 69 67 68 74 20 62 65 20 ment. Might be
2095a 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 54 6f 6b NULL */.){. Tok
2095b 65 6e 20 78 3b 0a 20 20 78 2e 7a 20 3d 20 7a 54 en x;. x.z = zT
2095c 6f 6b 65 6e 3b 0a 20 20 78 2e 6e 20 3d 20 7a 54 oken;. x.n = zT
2095d 6f 6b 65 6e 20 3f 20 73 71 6c 69 74 65 33 53 74 oken ? sqlite3St
2095e 72 6c 65 6e 33 30 28 7a 54 6f 6b 65 6e 29 20 3a rlen30(zToken) :
2095f 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 0;. return sql
20960 69 74 65 33 45 78 70 72 41 6c 6c 6f 63 28 64 62 ite3ExprAlloc(db
20961 2c 20 6f 70 2c 20 26 78 2c 20 30 29 3b 0a 7d 0a , op, &x, 0);.}.
20962 0a 2f 2a 0a 2a 2a 20 41 74 74 61 63 68 20 73 75 ./*.** Attach su
20963 62 74 72 65 65 73 20 70 4c 65 66 74 20 61 6e 64 btrees pLeft and
20964 20 70 52 69 67 68 74 20 74 6f 20 74 68 65 20 45 pRight to the E
20965 78 70 72 20 6e 6f 64 65 20 70 52 6f 6f 74 2e 0a xpr node pRoot..
20966 2a 2a 0a 2a 2a 20 49 66 20 70 52 6f 6f 74 3d 3d **.** If pRoot==
20967 4e 55 4c 4c 20 74 68 61 74 20 6d 65 61 6e 73 20 NULL that means
20968 74 68 61 74 20 61 20 6d 65 6d 6f 72 79 20 61 6c that a memory al
20969 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 68 location error h
2096a 61 73 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a 20 as occurred..**
2096b 49 6e 20 74 68 61 74 20 63 61 73 65 2c 20 64 65 In that case, de
2096c 6c 65 74 65 20 74 68 65 20 73 75 62 74 72 65 65 lete the subtree
2096d 73 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67 s pLeft and pRig
2096e 68 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ht..*/.SQLITE_PR
2096f 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
20970 65 33 45 78 70 72 41 74 74 61 63 68 53 75 62 74 e3ExprAttachSubt
20971 72 65 65 73 28 0a 20 20 73 71 6c 69 74 65 33 20 rees(. sqlite3
20972 2a 64 62 2c 0a 20 20 45 78 70 72 20 2a 70 52 6f *db,. Expr *pRo
20973 6f 74 2c 0a 20 20 45 78 70 72 20 2a 70 4c 65 66 ot,. Expr *pLef
20974 74 2c 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68 t,. Expr *pRigh
20975 74 0a 29 7b 0a 20 20 69 66 28 20 70 52 6f 6f 74 t.){. if( pRoot
20976 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 ==0 ){. asser
20977 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 t( db->mallocFai
20978 6c 65 64 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 led );. sqlit
20979 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c e3ExprDelete(db,
2097a 20 70 4c 65 66 74 29 3b 0a 20 20 20 20 73 71 6c pLeft);. sql
2097b 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 ite3ExprDelete(d
2097c 62 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 7d 65 b, pRight);. }e
2097d 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 70 52 69 lse{. if( pRi
2097e 67 68 74 20 29 7b 0a 20 20 20 20 20 20 70 52 6f ght ){. pRo
2097f 6f 74 2d 3e 70 52 69 67 68 74 20 3d 20 70 52 69 ot->pRight = pRi
20980 67 68 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 ght;. if( p
20981 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 26 20 45 Right->flags & E
20982 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a P_ExpCollate ){.
20983 20 20 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e 66 pRoot->f
20984 6c 61 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f lags |= EP_ExpCo
20985 6c 6c 61 74 65 3b 0a 20 20 20 20 20 20 20 20 70 llate;. p
20986 52 6f 6f 74 2d 3e 70 43 6f 6c 6c 20 3d 20 70 52 Root->pColl = pR
20987 69 67 68 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 ight->pColl;.
20988 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 }. }. i
20989 66 28 20 70 4c 65 66 74 20 29 7b 0a 20 20 20 20 f( pLeft ){.
2098a 20 20 70 52 6f 6f 74 2d 3e 70 4c 65 66 74 20 3d pRoot->pLeft =
2098b 20 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 69 66 pLeft;. if
2098c 28 20 70 4c 65 66 74 2d 3e 66 6c 61 67 73 20 26 ( pLeft->flags &
2098d 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29 EP_ExpCollate )
2098e 7b 0a 20 20 20 20 20 20 20 20 70 52 6f 6f 74 2d {. pRoot-
2098f 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 45 78 70 >flags |= EP_Exp
20990 43 6f 6c 6c 61 74 65 3b 0a 20 20 20 20 20 20 20 Collate;.
20991 20 70 52 6f 6f 74 2d 3e 70 43 6f 6c 6c 20 3d 20 pRoot->pColl =
20992 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 pLeft->pColl;.
20993 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
20994 65 78 70 72 53 65 74 48 65 69 67 68 74 28 70 52 exprSetHeight(pR
20995 6f 6f 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a oot);. }.}../*.
20996 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 45 78 ** Allocate a Ex
20997 70 72 20 6e 6f 64 65 20 77 68 69 63 68 20 6a 6f pr node which jo
20998 69 6e 73 20 61 73 20 6d 61 6e 79 20 61 73 20 74 ins as many as t
20999 77 6f 20 73 75 62 74 72 65 65 73 2e 0a 2a 2a 0a wo subtrees..**.
2099a 2a 2a 20 4f 6e 65 20 6f 72 20 62 6f 74 68 20 6f ** One or both o
2099b 66 20 74 68 65 20 73 75 62 74 72 65 65 73 20 63 f the subtrees c
2099c 61 6e 20 62 65 20 4e 55 4c 4c 2e 20 20 52 65 74 an be NULL. Ret
2099d 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f urn a pointer to
2099e 20 74 68 65 20 6e 65 77 0a 2a 2a 20 45 78 70 72 the new.** Expr
2099f 20 6e 6f 64 65 2e 20 20 4f 72 2c 20 69 66 20 61 node. Or, if a
209a0 6e 20 4f 4f 4d 20 65 72 72 6f 72 20 6f 63 63 75 n OOM error occu
209a1 72 73 2c 20 73 65 74 20 70 50 61 72 73 65 2d 3e rs, set pParse->
209a2 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
209a3 2c 0a 2a 2a 20 66 72 65 65 20 74 68 65 20 73 75 ,.** free the su
209a4 62 74 72 65 65 73 20 61 6e 64 20 72 65 74 75 72 btrees and retur
209a5 6e 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 n NULL..*/.SQLIT
209a6 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a E_PRIVATE Expr *
209a7 73 71 6c 69 74 65 33 50 45 78 70 72 28 0a 20 20 sqlite3PExpr(.
209a8 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 Parse *pParse,
209a9 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 /* Parsi
209aa 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 ng context */.
209ab 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20 int op,
209ac 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 /* Expre
209ad 73 73 69 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a ssion opcode */.
209ae 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 20 Expr *pLeft,
209af 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 66 /* Lef
209b0 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 45 t operand */. E
209b1 78 70 72 20 2a 70 52 69 67 68 74 2c 20 20 20 20 xpr *pRight,
209b2 20 20 20 20 20 20 20 2f 2a 20 52 69 67 68 74 20 /* Right
209b3 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 63 6f 6e operand */. con
209b4 73 74 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e st Token *pToken
209b5 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 /* Argument
209b6 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 45 token */.){. E
209b7 78 70 72 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 xpr *p = sqlite3
209b8 45 78 70 72 41 6c 6c 6f 63 28 70 50 61 72 73 65 ExprAlloc(pParse
209b9 2d 3e 64 62 2c 20 6f 70 2c 20 70 54 6f 6b 65 6e ->db, op, pToken
209ba 2c 20 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 , 1);. sqlite3E
209bb 78 70 72 41 74 74 61 63 68 53 75 62 74 72 65 65 xprAttachSubtree
209bc 73 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 2c s(pParse->db, p,
209bd 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74 29 3b pLeft, pRight);
209be 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a . return p;.}..
209bf 2f 2a 0a 2a 2a 20 4a 6f 69 6e 20 74 77 6f 20 65 /*.** Join two e
209c0 78 70 72 65 73 73 69 6f 6e 73 20 75 73 69 6e 67 xpressions using
209c1 20 61 6e 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 an AND operator
209c2 2e 20 20 49 66 20 65 69 74 68 65 72 20 65 78 70 . If either exp
209c3 72 65 73 73 69 6f 6e 20 69 73 0a 2a 2a 20 4e 55 ression is.** NU
209c4 4c 4c 2c 20 74 68 65 6e 20 6a 75 73 74 20 72 65 LL, then just re
209c5 74 75 72 6e 20 74 68 65 20 6f 74 68 65 72 20 65 turn the other e
209c6 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2f 0a 53 51 xpression..*/.SQ
209c7 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 LITE_PRIVATE Exp
209c8 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 41 6e r *sqlite3ExprAn
209c9 64 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 d(sqlite3 *db, E
209ca 78 70 72 20 2a 70 4c 65 66 74 2c 20 45 78 70 72 xpr *pLeft, Expr
209cb 20 2a 70 52 69 67 68 74 29 7b 0a 20 20 69 66 28 *pRight){. if(
209cc 20 70 4c 65 66 74 3d 3d 30 20 29 7b 0a 20 20 20 pLeft==0 ){.
209cd 20 72 65 74 75 72 6e 20 70 52 69 67 68 74 3b 0a return pRight;.
209ce 20 20 7d 65 6c 73 65 20 69 66 28 20 70 52 69 67 }else if( pRig
209cf 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 ht==0 ){. ret
209d0 75 72 6e 20 70 4c 65 66 74 3b 0a 20 20 7d 65 6c urn pLeft;. }el
209d1 73 65 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 4e se{. Expr *pN
209d2 65 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 ew = sqlite3Expr
209d3 41 6c 6c 6f 63 28 64 62 2c 20 54 4b 5f 41 4e 44 Alloc(db, TK_AND
209d4 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c , 0, 0);. sql
209d5 69 74 65 33 45 78 70 72 41 74 74 61 63 68 53 75 ite3ExprAttachSu
209d6 62 74 72 65 65 73 28 64 62 2c 20 70 4e 65 77 2c btrees(db, pNew,
209d7 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74 29 3b pLeft, pRight);
209d8 0a 20 20 20 20 72 65 74 75 72 6e 20 70 4e 65 77 . return pNew
209d9 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 ;. }.}../*.** C
209da 6f 6e 73 74 72 75 63 74 20 61 20 6e 65 77 20 65 onstruct a new e
209db 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20 66 xpression node f
209dc 6f 72 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 or a function wi
209dd 74 68 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 61 th multiple.** a
209de 72 67 75 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c rguments..*/.SQL
209df 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 ITE_PRIVATE Expr
209e0 20 2a 73 71 6c 69 74 65 33 45 78 70 72 46 75 6e *sqlite3ExprFun
209e1 63 74 69 6f 6e 28 50 61 72 73 65 20 2a 70 50 61 ction(Parse *pPa
209e2 72 73 65 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 rse, ExprList *p
209e3 4c 69 73 74 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f List, Token *pTo
209e4 6b 65 6e 29 7b 0a 20 20 45 78 70 72 20 2a 70 4e ken){. Expr *pN
209e5 65 77 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 ew;. sqlite3 *d
209e6 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a b = pParse->db;.
209e7 20 20 61 73 73 65 72 74 28 20 70 54 6f 6b 65 6e assert( pToken
209e8 20 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c );. pNew = sql
209e9 69 74 65 33 45 78 70 72 41 6c 6c 6f 63 28 64 62 ite3ExprAlloc(db
209ea 2c 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 2c 20 70 , TK_FUNCTION, p
209eb 54 6f 6b 65 6e 2c 20 31 29 3b 0a 20 20 69 66 28 Token, 1);. if(
209ec 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 pNew==0 ){.
209ed 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 sqlite3ExprListD
209ee 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 73 74 29 elete(db, pList)
209ef 3b 20 2f 2a 20 41 76 6f 69 64 20 6d 65 6d 6f 72 ; /* Avoid memor
209f0 79 20 6c 65 61 6b 20 77 68 65 6e 20 6d 61 6c 6c y leak when mall
209f1 6f 63 20 66 61 69 6c 73 20 2a 2f 0a 20 20 20 20 oc fails */.
209f2 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
209f3 70 4e 65 77 2d 3e 78 2e 70 4c 69 73 74 20 3d 20 pNew->x.pList =
209f4 70 4c 69 73 74 3b 0a 20 20 61 73 73 65 72 74 28 pList;. assert(
209f5 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 !ExprHasPropert
209f6 79 28 70 4e 65 77 2c 20 45 50 5f 78 49 73 53 65 y(pNew, EP_xIsSe
209f7 6c 65 63 74 29 20 29 3b 0a 20 20 73 71 6c 69 74 lect) );. sqlit
209f8 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74 28 e3ExprSetHeight(
209f9 70 50 61 72 73 65 2c 20 70 4e 65 77 29 3b 0a 20 pParse, pNew);.
209fa 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a return pNew;.}.
209fb 0a 2f 2a 0a 2a 2a 20 41 73 73 69 67 6e 20 61 20 ./*.** Assign a
209fc 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 variable number
209fd 74 6f 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e to an expression
209fe 20 74 68 61 74 20 65 6e 63 6f 64 65 73 20 61 20 that encodes a
209ff 77 69 6c 64 63 61 72 64 0a 2a 2a 20 69 6e 20 74 wildcard.** in t
20a00 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 he original SQL
20a01 73 74 61 74 65 6d 65 6e 74 2e 20 20 0a 2a 2a 0a statement. .**.
20a02 2a 2a 20 57 69 6c 64 63 61 72 64 73 20 63 6f 6e ** Wildcards con
20a03 73 69 73 74 69 6e 67 20 6f 66 20 61 20 73 69 6e sisting of a sin
20a04 67 6c 65 20 22 3f 22 20 61 72 65 20 61 73 73 69 gle "?" are assi
20a05 67 6e 65 64 20 74 68 65 20 6e 65 78 74 20 73 65 gned the next se
20a06 71 75 65 6e 74 69 61 6c 0a 2a 2a 20 76 61 72 69 quential.** vari
20a07 61 62 6c 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a able number..**.
20a08 2a 2a 20 57 69 6c 64 63 61 72 64 73 20 6f 66 20 ** Wildcards of
20a09 74 68 65 20 66 6f 72 6d 20 22 3f 6e 6e 6e 22 20 the form "?nnn"
20a0a 61 72 65 20 61 73 73 69 67 6e 65 64 20 74 68 65 are assigned the
20a0b 20 6e 75 6d 62 65 72 20 22 6e 6e 6e 22 2e 20 20 number "nnn".
20a0c 57 65 20 6d 61 6b 65 0a 2a 2a 20 73 75 72 65 20 We make.** sure
20a0d 22 6e 6e 6e 22 20 69 73 20 6e 6f 74 20 74 6f 6f "nnn" is not too
20a0e 20 62 65 20 74 6f 20 61 76 6f 69 64 20 61 20 64 be to avoid a d
20a0f 65 6e 69 61 6c 20 6f 66 20 73 65 72 76 69 63 65 enial of service
20a10 20 61 74 74 61 63 6b 20 77 68 65 6e 0a 2a 2a 20 attack when.**
20a11 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e the SQL statemen
20a12 74 20 63 6f 6d 65 73 20 66 72 6f 6d 20 61 6e 20 t comes from an
20a13 65 78 74 65 72 6e 61 6c 20 73 6f 75 72 63 65 2e external source.
20a14 0a 2a 2a 0a 2a 2a 20 57 69 6c 64 63 61 72 64 73 .**.** Wildcards
20a15 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3a 61 of the form ":a
20a16 61 61 22 2c 20 22 40 61 61 61 22 2c 20 6f 72 20 aa", "@aaa", or
20a17 22 24 61 61 61 22 20 61 72 65 20 61 73 73 69 67 "$aaa" are assig
20a18 6e 65 64 20 74 68 65 20 73 61 6d 65 20 6e 75 6d ned the same num
20a19 62 65 72 0a 2a 2a 20 61 73 20 74 68 65 20 70 72 ber.** as the pr
20a1a 65 76 69 6f 75 73 20 69 6e 73 74 61 6e 63 65 20 evious instance
20a1b 6f 66 20 74 68 65 20 73 61 6d 65 20 77 69 6c 64 of the same wild
20a1c 63 61 72 64 2e 20 20 4f 72 20 69 66 20 74 68 69 card. Or if thi
20a1d 73 20 69 73 20 74 68 65 20 66 69 72 73 74 0a 2a s is the first.*
20a1e 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 * instance of th
20a1f 65 20 77 69 6c 64 63 61 72 64 2c 20 74 68 65 20 e wildcard, the
20a20 6e 65 78 74 20 73 65 71 75 65 6e 69 61 6c 20 76 next sequenial v
20a21 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 69 ariable number i
20a22 73 0a 2a 2a 20 61 73 73 69 67 6e 65 64 2e 0a 2a s.** assigned..*
20a23 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
20a24 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
20a25 72 41 73 73 69 67 6e 56 61 72 4e 75 6d 62 65 72 rAssignVarNumber
20a26 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
20a27 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 Expr *pExpr){.
20a28 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 sqlite3 *db = pP
20a29 61 72 73 65 2d 3e 64 62 3b 0a 20 20 63 6f 6e 73 arse->db;. cons
20a2a 74 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 69 66 t char *z;.. if
20a2b 28 20 70 45 78 70 72 3d 3d 30 20 29 20 72 65 74 ( pExpr==0 ) ret
20a2c 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 21 urn;. assert( !
20a2d 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 ExprHasAnyProper
20a2e 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 ty(pExpr, EP_Int
20a2f 56 61 6c 75 65 7c 45 50 5f 52 65 64 75 63 65 64 Value|EP_Reduced
20a30 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 |EP_TokenOnly) )
20a31 3b 0a 20 20 7a 20 3d 20 70 45 78 70 72 2d 3e 75 ;. z = pExpr->u
20a32 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 61 73 73 65 72 .zToken;. asser
20a33 74 28 20 7a 21 3d 30 20 29 3b 0a 20 20 61 73 73 t( z!=0 );. ass
20a34 65 72 74 28 20 7a 5b 30 5d 21 3d 30 20 29 3b 0a ert( z[0]!=0 );.
20a35 20 20 69 66 28 20 7a 5b 31 5d 3d 3d 30 20 29 7b if( z[1]==0 ){
20a36 0a 20 20 20 20 2f 2a 20 57 69 6c 64 63 61 72 64 . /* Wildcard
20a37 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 22 of the form "?"
20a38 2e 20 20 41 73 73 69 67 6e 20 74 68 65 20 6e 65 . Assign the ne
20a39 78 74 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 xt variable numb
20a3a 65 72 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 er */. assert
20a3b 28 20 7a 5b 30 5d 3d 3d 27 3f 27 20 29 3b 0a 20 ( z[0]=='?' );.
20a3c 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d pExpr->iColum
20a3d 6e 20 3d 20 28 79 6e 56 61 72 29 28 2b 2b 70 50 n = (ynVar)(++pP
20a3e 61 72 73 65 2d 3e 6e 56 61 72 29 3b 0a 20 20 7d arse->nVar);. }
20a3f 65 6c 73 65 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 else if( z[0]=='
20a40 3f 27 20 29 7b 0a 20 20 20 20 2f 2a 20 57 69 6c ?' ){. /* Wil
20a41 64 63 61 72 64 20 6f 66 20 74 68 65 20 66 6f 72 dcard of the for
20a42 6d 20 22 3f 6e 6e 6e 22 2e 20 20 43 6f 6e 76 65 m "?nnn". Conve
20a43 72 74 20 22 6e 6e 6e 22 20 74 6f 20 61 6e 20 69 rt "nnn" to an i
20a44 6e 74 65 67 65 72 20 61 6e 64 0a 20 20 20 20 2a nteger and. *
20a45 2a 20 75 73 65 20 69 74 20 61 73 20 74 68 65 20 * use it as the
20a46 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 variable number
20a47 2a 2f 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 61 */. int i = a
20a48 74 6f 69 28 28 63 68 61 72 2a 29 26 7a 5b 31 5d toi((char*)&z[1]
20a49 29 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 69 43 );. pExpr->iC
20a4a 6f 6c 75 6d 6e 20 3d 20 28 79 6e 56 61 72 29 69 olumn = (ynVar)i
20a4b 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 ;. testcase(
20a4c 69 3d 3d 30 20 29 3b 0a 20 20 20 20 74 65 73 74 i==0 );. test
20a4d 63 61 73 65 28 20 69 3d 3d 31 20 29 3b 0a 20 20 case( i==1 );.
20a4e 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 64 testcase( i==d
20a4f 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 b->aLimit[SQLITE
20a50 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f _LIMIT_VARIABLE_
20a51 4e 55 4d 42 45 52 5d 2d 31 20 29 3b 0a 20 20 20 NUMBER]-1 );.
20a52 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 64 62 testcase( i==db
20a53 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f ->aLimit[SQLITE_
20a54 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e LIMIT_VARIABLE_N
20a55 55 4d 42 45 52 5d 20 29 3b 0a 20 20 20 20 69 66 UMBER] );. if
20a56 28 20 69 3c 31 20 7c 7c 20 69 3e 64 62 2d 3e 61 ( i<1 || i>db->a
20a57 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d Limit[SQLITE_LIM
20a58 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 IT_VARIABLE_NUMB
20a59 45 52 5d 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ER] ){. sql
20a5a 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
20a5b 72 73 65 2c 20 22 76 61 72 69 61 62 6c 65 20 6e rse, "variable n
20a5c 75 6d 62 65 72 20 6d 75 73 74 20 62 65 20 62 65 umber must be be
20a5d 74 77 65 65 6e 20 3f 31 20 61 6e 64 20 3f 25 64 tween ?1 and ?%d
20a5e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d ",. db-
20a5f 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c >aLimit[SQLITE_L
20a60 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 IMIT_VARIABLE_NU
20a61 4d 42 45 52 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 MBER]);. }.
20a62 20 20 69 66 28 20 69 3e 70 50 61 72 73 65 2d 3e if( i>pParse->
20a63 6e 56 61 72 20 29 7b 0a 20 20 20 20 20 20 70 50 nVar ){. pP
20a64 61 72 73 65 2d 3e 6e 56 61 72 20 3d 20 69 3b 0a arse->nVar = i;.
20a65 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
20a66 20 20 20 2f 2a 20 57 69 6c 64 63 61 72 64 73 20 /* Wildcards
20a67 6c 69 6b 65 20 22 3a 61 61 61 22 2c 20 22 24 61 like ":aaa", "$a
20a68 61 61 22 20 6f 72 20 22 40 61 61 61 22 2e 20 20 aa" or "@aaa".
20a69 52 65 75 73 65 20 74 68 65 20 73 61 6d 65 20 76 Reuse the same v
20a6a 61 72 69 61 62 6c 65 0a 20 20 20 20 2a 2a 20 6e ariable. ** n
20a6b 75 6d 62 65 72 20 61 73 20 74 68 65 20 70 72 69 umber as the pri
20a6c 6f 72 20 61 70 70 65 61 72 61 6e 63 65 20 6f 66 or appearance of
20a6d 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 2c 20 the same name,
20a6e 6f 72 20 69 66 20 74 68 65 20 6e 61 6d 65 0a 20 or if the name.
20a6f 20 20 20 2a 2a 20 68 61 73 20 6e 65 76 65 72 20 ** has never
20a70 61 70 70 65 61 72 65 64 20 62 65 66 6f 72 65 2c appeared before,
20a71 20 72 65 75 73 65 20 74 68 65 20 73 61 6d 65 20 reuse the same
20a72 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 0a variable number.
20a73 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 */. int i
20a74 3b 0a 20 20 20 20 75 33 32 20 6e 3b 0a 20 20 20 ;. u32 n;.
20a75 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c n = sqlite3Strl
20a76 65 6e 33 30 28 7a 29 3b 0a 20 20 20 20 66 6f 72 en30(z);. for
20a77 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e (i=0; i<pParse->
20a78 6e 56 61 72 45 78 70 72 3b 20 69 2b 2b 29 7b 0a nVarExpr; i++){.
20a79 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d Expr *pE =
20a7a 20 70 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78 pParse->apVarEx
20a7b 70 72 5b 69 5d 3b 0a 20 20 20 20 20 20 61 73 73 pr[i];. ass
20a7c 65 72 74 28 20 70 45 21 3d 30 20 29 3b 0a 20 20 ert( pE!=0 );.
20a7d 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 70 if( memcmp(p
20a7e 45 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 7a 2c 20 E->u.zToken, z,
20a7f 6e 29 3d 3d 30 20 26 26 20 70 45 2d 3e 75 2e 7a n)==0 && pE->u.z
20a80 54 6f 6b 65 6e 5b 6e 5d 3d 3d 30 20 29 7b 0a 20 Token[n]==0 ){.
20a81 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 pExpr->iC
20a82 6f 6c 75 6d 6e 20 3d 20 70 45 2d 3e 69 43 6f 6c olumn = pE->iCol
20a83 75 6d 6e 3b 0a 20 20 20 20 20 20 20 20 62 72 65 umn;. bre
20a84 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ak;. }.
20a85 7d 0a 20 20 20 20 69 66 28 20 69 3e 3d 70 50 61 }. if( i>=pPa
20a86 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 20 29 7b rse->nVarExpr ){
20a87 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 . pExpr->iC
20a88 6f 6c 75 6d 6e 20 3d 20 28 79 6e 56 61 72 29 28 olumn = (ynVar)(
20a89 2b 2b 70 50 61 72 73 65 2d 3e 6e 56 61 72 29 3b ++pParse->nVar);
20a8a 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 72 73 . if( pPars
20a8b 65 2d 3e 6e 56 61 72 45 78 70 72 3e 3d 70 50 61 e->nVarExpr>=pPa
20a8c 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 41 6c 6c rse->nVarExprAll
20a8d 6f 63 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 oc-1 ){.
20a8e 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 pParse->nVarExpr
20a8f 41 6c 6c 6f 63 20 2b 3d 20 70 50 61 72 73 65 2d Alloc += pParse-
20a90 3e 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 20 2b >nVarExprAlloc +
20a91 20 31 30 3b 0a 20 20 20 20 20 20 20 20 70 50 61 10;. pPa
20a92 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 20 3d rse->apVarExpr =
20a93 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c . sql
20a94 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 ite3DbReallocOrF
20a95 72 65 65 28 0a 20 20 20 20 20 20 20 20 20 20 20 ree(.
20a96 20 20 20 64 62 2c 0a 20 20 20 20 20 20 20 20 20 db,.
20a97 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61 70 56 pParse->apV
20a98 61 72 45 78 70 72 2c 0a 20 20 20 20 20 20 20 20 arExpr,.
20a99 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 56 pParse->nV
20a9a 61 72 45 78 70 72 41 6c 6c 6f 63 2a 73 69 7a 65 arExprAlloc*size
20a9b 6f 66 28 70 50 61 72 73 65 2d 3e 61 70 56 61 72 of(pParse->apVar
20a9c 45 78 70 72 5b 30 5d 29 0a 20 20 20 20 20 20 20 Expr[0]).
20a9d 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 7d 0a );. }.
20a9e 20 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d if( !db->m
20a9f 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 allocFailed ){.
20aa0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
20aa1 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 Parse->apVarExpr
20aa2 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 70 !=0 );. p
20aa3 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 Parse->apVarExpr
20aa4 5b 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 [pParse->nVarExp
20aa5 72 2b 2b 5d 20 3d 20 70 45 78 70 72 3b 0a 20 20 r++] = pExpr;.
20aa6 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 20 }. }. }
20aa7 0a 20 20 69 66 28 20 21 70 50 61 72 73 65 2d 3e . if( !pParse->
20aa8 6e 45 72 72 20 26 26 20 70 50 61 72 73 65 2d 3e nErr && pParse->
20aa9 6e 56 61 72 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b nVar>db->aLimit[
20aaa 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 SQLITE_LIMIT_VAR
20aab 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 29 7b IABLE_NUMBER] ){
20aac 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f . sqlite3Erro
20aad 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f rMsg(pParse, "to
20aae 6f 20 6d 61 6e 79 20 53 51 4c 20 76 61 72 69 61 o many SQL varia
20aaf 62 6c 65 73 22 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f bles");. }.}../
20ab0 2a 0a 2a 2a 20 43 6c 65 61 72 20 61 6e 20 65 78 *.** Clear an ex
20ab1 70 72 65 73 73 69 6f 6e 20 73 74 72 75 63 74 75 pression structu
20ab2 72 65 20 77 69 74 68 6f 75 74 20 64 65 6c 65 74 re without delet
20ab3 69 6e 67 20 74 68 65 20 73 74 72 75 63 74 75 72 ing the structur
20ab4 65 20 69 74 73 65 6c 66 2e 0a 2a 2a 20 53 75 62 e itself..** Sub
20ab5 73 74 72 75 63 74 75 72 65 20 69 73 20 64 65 6c structure is del
20ab6 65 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f eted..*/.SQLITE_
20ab7 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
20ab8 69 74 65 33 45 78 70 72 43 6c 65 61 72 28 73 71 ite3ExprClear(sq
20ab9 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20 lite3 *db, Expr
20aba 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 *p){. assert( p
20abb 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 21 45 78 !=0 );. if( !Ex
20abc 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 prHasAnyProperty
20abd 28 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 (p, EP_TokenOnly
20abe 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ) ){. sqlite3
20abf 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 ExprDelete(db, p
20ac0 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 20 20 73 71 ->pLeft);. sq
20ac1 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 lite3ExprDelete(
20ac2 64 62 2c 20 70 2d 3e 70 52 69 67 68 74 29 3b 0a db, p->pRight);.
20ac3 20 20 20 20 69 66 28 20 21 45 78 70 72 48 61 73 if( !ExprHas
20ac4 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 52 Property(p, EP_R
20ac5 65 64 75 63 65 64 29 20 26 26 20 28 70 2d 3e 66 educed) && (p->f
20ac6 6c 61 67 73 32 20 26 20 45 50 32 5f 4d 61 6c 6c lags2 & EP2_Mall
20ac7 6f 63 65 64 54 6f 6b 65 6e 29 21 3d 30 20 29 7b ocedToken)!=0 ){
20ac8 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 . sqlite3Db
20ac9 46 72 65 65 28 64 62 2c 20 70 2d 3e 75 2e 7a 54 Free(db, p->u.zT
20aca 6f 6b 65 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 oken);. }.
20acb 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 if( ExprHasProp
20acc 65 72 74 79 28 70 2c 20 45 50 5f 78 49 73 53 65 erty(p, EP_xIsSe
20acd 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 73 lect) ){. s
20ace 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 qlite3SelectDele
20acf 74 65 28 64 62 2c 20 70 2d 3e 78 2e 70 53 65 6c te(db, p->x.pSel
20ad0 65 63 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b ect);. }else{
20ad1 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 . sqlite3Ex
20ad2 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c prListDelete(db,
20ad3 20 70 2d 3e 78 2e 70 4c 69 73 74 29 3b 0a 20 20 p->x.pList);.
20ad4 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
20ad5 20 52 65 63 75 72 73 69 76 65 6c 79 20 64 65 6c Recursively del
20ad6 65 74 65 20 61 6e 20 65 78 70 72 65 73 73 69 6f ete an expressio
20ad7 6e 20 74 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54 n tree..*/.SQLIT
20ad8 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
20ad9 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 qlite3ExprDelete
20ada 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 (sqlite3 *db, Ex
20adb 70 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d pr *p){. if( p=
20adc 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 =0 ) return;. s
20add 71 6c 69 74 65 33 45 78 70 72 43 6c 65 61 72 28 qlite3ExprClear(
20ade 64 62 2c 20 70 29 3b 0a 20 20 69 66 28 20 21 45 db, p);. if( !E
20adf 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 xprHasProperty(p
20ae0 2c 20 45 50 5f 53 74 61 74 69 63 29 20 29 7b 0a , EP_Static) ){.
20ae1 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
20ae2 65 28 64 62 2c 20 70 29 3b 0a 20 20 7d 0a 7d 0a e(db, p);. }.}.
20ae3 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
20ae4 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 e number of byte
20ae5 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 s allocated for
20ae6 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 73 the expression s
20ae7 74 72 75 63 74 75 72 65 20 0a 2a 2a 20 70 61 73 tructure .** pas
20ae8 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74 sed as the first
20ae9 20 61 72 67 75 6d 65 6e 74 2e 20 54 68 69 73 20 argument. This
20aea 69 73 20 61 6c 77 61 79 73 20 6f 6e 65 20 6f 66 is always one of
20aeb 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45 2c 0a EXPR_FULLSIZE,.
20aec 2a 2a 20 45 58 50 52 5f 52 45 44 55 43 45 44 53 ** EXPR_REDUCEDS
20aed 49 5a 45 20 6f 72 20 45 58 50 52 5f 54 4f 4b 45 IZE or EXPR_TOKE
20aee 4e 4f 4e 4c 59 53 49 5a 45 2e 0a 2a 2f 0a 73 74 NONLYSIZE..*/.st
20aef 61 74 69 63 20 69 6e 74 20 65 78 70 72 53 74 72 atic int exprStr
20af0 75 63 74 53 69 7a 65 28 45 78 70 72 20 2a 70 29 uctSize(Expr *p)
20af1 7b 0a 20 20 69 66 28 20 45 78 70 72 48 61 73 50 {. if( ExprHasP
20af2 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 54 6f roperty(p, EP_To
20af3 6b 65 6e 4f 6e 6c 79 29 20 29 20 72 65 74 75 72 kenOnly) ) retur
20af4 6e 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 n EXPR_TOKENONLY
20af5 53 49 5a 45 3b 0a 20 20 69 66 28 20 45 78 70 72 SIZE;. if( Expr
20af6 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45 HasProperty(p, E
20af7 50 5f 52 65 64 75 63 65 64 29 20 29 20 72 65 74 P_Reduced) ) ret
20af8 75 72 6e 20 45 58 50 52 5f 52 45 44 55 43 45 44 urn EXPR_REDUCED
20af9 53 49 5a 45 3b 0a 20 20 72 65 74 75 72 6e 20 45 SIZE;. return E
20afa 58 50 52 5f 46 55 4c 4c 53 49 5a 45 3b 0a 7d 0a XPR_FULLSIZE;.}.
20afb 0a 2f 2a 0a 2a 2a 20 54 68 65 20 64 75 70 65 64 ./*.** The duped
20afc 45 78 70 72 2a 53 69 7a 65 28 29 20 72 6f 75 74 Expr*Size() rout
20afd 69 6e 65 73 20 65 61 63 68 20 72 65 74 75 72 6e ines each return
20afe 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
20aff 79 74 65 73 20 72 65 71 75 69 72 65 64 0a 2a 2a ytes required.**
20b00 20 74 6f 20 73 74 6f 72 65 20 61 20 63 6f 70 79 to store a copy
20b01 20 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f of an expressio
20b02 6e 20 6f 72 20 65 78 70 72 65 73 73 69 6f 6e 20 n or expression
20b03 74 72 65 65 2e 20 20 54 68 65 79 20 64 69 66 66 tree. They diff
20b04 65 72 20 69 6e 0a 2a 2a 20 68 6f 77 20 6d 75 63 er in.** how muc
20b05 68 20 6f 66 20 74 68 65 20 74 72 65 65 20 69 73 h of the tree is
20b06 20 6d 65 61 73 75 72 65 64 2e 0a 2a 2a 0a 2a 2a measured..**.**
20b07 20 20 20 20 20 64 75 70 65 64 45 78 70 72 53 74 dupedExprSt
20b08 72 75 63 74 53 69 7a 65 28 29 20 20 20 20 20 53 ructSize() S
20b09 69 7a 65 20 6f 66 20 6f 6e 6c 79 20 74 68 65 20 ize of only the
20b0a 45 78 70 72 20 73 74 72 75 63 74 75 72 65 20 0a Expr structure .
20b0b 2a 2a 20 20 20 20 20 64 75 70 65 64 45 78 70 72 ** dupedExpr
20b0c 4e 6f 64 65 53 69 7a 65 28 29 20 20 20 20 20 20 NodeSize()
20b0d 20 53 69 7a 65 20 6f 66 20 45 78 70 72 20 2b 20 Size of Expr +
20b0e 73 70 61 63 65 20 66 6f 72 20 74 6f 6b 65 6e 0a space for token.
20b0f 2a 2a 20 20 20 20 20 64 75 70 65 64 45 78 70 72 ** dupedExpr
20b10 53 69 7a 65 28 29 20 20 20 20 20 20 20 20 20 20 Size()
20b11 20 45 78 70 72 20 2b 20 74 6f 6b 65 6e 20 2b 20 Expr + token +
20b12 73 75 62 74 72 65 65 20 63 6f 6d 70 6f 6e 65 6e subtree componen
20b13 74 73 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ts.**.**********
20b14 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20b15 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20b16 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20b17 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20b18 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 75 70 65 *.**.** The dupe
20b19 64 45 78 70 72 53 74 72 75 63 74 53 69 7a 65 28 dExprStructSize(
20b1a 29 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 ) function retur
20b1b 6e 73 20 74 77 6f 20 76 61 6c 75 65 73 20 4f 52 ns two values OR
20b1c 2d 65 64 20 74 6f 67 65 74 68 65 72 3a 20 20 0a -ed together: .
20b1d 2a 2a 20 28 31 29 20 74 68 65 20 73 70 61 63 65 ** (1) the space
20b1e 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 61 20 required for a
20b1f 63 6f 70 79 20 6f 66 20 74 68 65 20 45 78 70 72 copy of the Expr
20b20 20 73 74 72 75 63 74 75 72 65 20 6f 6e 6c 79 20 structure only
20b21 61 6e 64 20 0a 2a 2a 20 28 32 29 20 74 68 65 20 and .** (2) the
20b22 45 50 5f 78 78 78 20 66 6c 61 67 73 20 74 68 61 EP_xxx flags tha
20b23 74 20 69 6e 64 69 63 61 74 65 20 77 68 61 74 20 t indicate what
20b24 74 68 65 20 73 74 72 75 63 74 75 72 65 20 73 69 the structure si
20b25 7a 65 20 73 68 6f 75 6c 64 20 62 65 2e 0a 2a 2a ze should be..**
20b26 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 The return valu
20b27 65 73 20 69 73 20 61 6c 77 61 79 73 20 6f 6e 65 es is always one
20b28 20 6f 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 of:.**.**
20b29 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45 0a 2a 2a EXPR_FULLSIZE.**
20b2a 20 20 20 20 20 20 45 58 50 52 5f 52 45 44 55 43 EXPR_REDUC
20b2b 45 44 53 49 5a 45 20 20 20 7c 20 45 50 5f 52 65 EDSIZE | EP_Re
20b2c 64 75 63 65 64 0a 2a 2a 20 20 20 20 20 20 45 58 duced.** EX
20b2d 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 PR_TOKENONLYSIZE
20b2e 20 7c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 0a | EP_TokenOnly.
20b2f 2a 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f **.** The size o
20b30 66 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20 f the structure
20b31 63 61 6e 20 62 65 20 66 6f 75 6e 64 20 62 79 20 can be found by
20b32 6d 61 73 6b 69 6e 67 20 74 68 65 20 72 65 74 75 masking the retu
20b33 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 rn value.** of t
20b34 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 74 68 his routine with
20b35 20 30 78 66 66 66 2e 20 20 54 68 65 20 66 6c 61 0xfff. The fla
20b36 67 73 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 20 gs can be found
20b37 62 79 20 6d 61 73 6b 69 6e 67 20 74 68 65 0a 2a by masking the.*
20b38 2a 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 77 * return value w
20b39 69 74 68 20 45 50 5f 52 65 64 75 63 65 64 7c 45 ith EP_Reduced|E
20b3a 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 2e 0a 2a 2a 0a P_TokenOnly..**.
20b3b 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 77 69 74 ** Note that wit
20b3c 68 20 66 6c 61 67 73 3d 3d 45 58 50 52 44 55 50 h flags==EXPRDUP
20b3d 5f 52 45 44 55 43 45 2c 20 74 68 69 73 20 72 6f _REDUCE, this ro
20b3e 75 74 69 6e 65 73 20 77 6f 72 6b 73 20 6f 6e 20 utines works on
20b3f 66 75 6c 6c 2d 73 69 7a 65 0a 2a 2a 20 28 75 6e full-size.** (un
20b40 72 65 64 75 63 65 64 29 20 45 78 70 72 20 6f 62 reduced) Expr ob
20b41 6a 65 63 74 73 20 61 73 20 74 68 65 79 20 6f 72 jects as they or
20b42 20 6f 72 69 67 69 6e 61 6c 6c 79 20 63 6f 6e 73 originally cons
20b43 74 72 75 63 74 65 64 20 62 79 20 74 68 65 20 70 tructed by the p
20b44 61 72 73 65 72 2e 0a 2a 2a 20 44 75 72 69 6e 67 arser..** During
20b45 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 61 6c expression anal
20b46 79 73 69 73 2c 20 65 78 74 72 61 20 69 6e 66 6f ysis, extra info
20b47 72 6d 61 74 69 6f 6e 20 69 73 20 63 6f 6d 70 75 rmation is compu
20b48 74 65 64 20 61 6e 64 20 6d 6f 76 65 64 20 69 6e ted and moved in
20b49 74 6f 0a 2a 2a 20 6c 61 74 65 72 20 70 61 72 74 to.** later part
20b4a 73 20 6f 66 20 74 65 68 20 45 78 70 72 20 6f 62 s of teh Expr ob
20b4b 6a 65 63 74 20 61 6e 64 20 74 68 61 74 20 65 78 ject and that ex
20b4c 74 72 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 tra information
20b4d 6d 69 67 68 74 20 67 65 74 20 63 68 6f 70 70 65 might get choppe
20b4e 64 0a 2a 2a 20 6f 66 66 20 69 66 20 74 68 65 20 d.** off if the
20b4f 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 72 65 expression is re
20b50 64 75 63 65 64 2e 20 20 4e 6f 74 65 20 61 6c 73 duced. Note als
20b51 6f 20 74 68 61 74 20 69 74 20 64 6f 65 73 20 6e o that it does n
20b52 6f 74 20 77 6f 72 6b 20 74 6f 0a 2a 2a 20 6d 61 ot work to.** ma
20b53 6b 65 20 61 20 45 58 50 52 44 55 50 5f 52 45 44 ke a EXPRDUP_RED
20b54 55 43 45 20 63 6f 70 79 20 6f 66 20 61 20 72 65 UCE copy of a re
20b55 64 75 63 65 64 20 65 78 70 72 65 73 73 69 6f 6e duced expression
20b56 2e 20 20 49 74 20 69 73 20 6f 6e 6c 79 20 6c 65 . It is only le
20b57 67 61 6c 0a 2a 2a 20 74 6f 20 72 65 64 75 63 65 gal.** to reduce
20b58 20 61 20 70 72 69 73 74 69 6e 65 20 65 78 70 72 a pristine expr
20b59 65 73 73 69 6f 6e 20 74 72 65 65 20 66 72 6f 6d ession tree from
20b5a 20 74 68 65 20 70 61 72 73 65 72 2e 20 20 54 68 the parser. Th
20b5b 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
20b5c 0a 2a 2a 20 6f 66 20 64 75 70 65 64 45 78 70 72 .** of dupedExpr
20b5d 53 74 72 75 63 74 53 69 7a 65 28 29 20 63 6f 6e StructSize() con
20b5e 74 61 69 6e 20 6d 75 6c 74 69 70 6c 65 20 61 73 tain multiple as
20b5f 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 sert() statement
20b60 73 20 74 68 61 74 20 61 74 74 65 6d 70 74 0a 2a s that attempt.*
20b61 2a 20 74 6f 20 65 6e 66 6f 72 63 65 20 74 68 69 * to enforce thi
20b62 73 20 63 6f 6e 73 74 72 61 69 6e 74 2e 0a 2a 2f s constraint..*/
20b63 0a 73 74 61 74 69 63 20 69 6e 74 20 64 75 70 65 .static int dupe
20b64 64 45 78 70 72 53 74 72 75 63 74 53 69 7a 65 28 dExprStructSize(
20b65 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61 Expr *p, int fla
20b66 67 73 29 7b 0a 20 20 69 6e 74 20 6e 53 69 7a 65 gs){. int nSize
20b67 3b 0a 20 20 61 73 73 65 72 74 28 20 66 6c 61 67 ;. assert( flag
20b68 73 3d 3d 45 58 50 52 44 55 50 5f 52 45 44 55 43 s==EXPRDUP_REDUC
20b69 45 20 7c 7c 20 66 6c 61 67 73 3d 3d 30 20 29 3b E || flags==0 );
20b6a 20 2f 2a 20 4f 6e 6c 79 20 6f 6e 65 20 66 6c 61 /* Only one fla
20b6b 67 20 76 61 6c 75 65 20 61 6c 6c 6f 77 65 64 20 g value allowed
20b6c 2a 2f 0a 20 20 69 66 28 20 30 3d 3d 28 66 6c 61 */. if( 0==(fla
20b6d 67 73 26 45 58 50 52 44 55 50 5f 52 45 44 55 43 gs&EXPRDUP_REDUC
20b6e 45 29 20 29 7b 0a 20 20 20 20 6e 53 69 7a 65 20 E) ){. nSize
20b6f 3d 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45 3b = EXPR_FULLSIZE;
20b70 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 . }else{. as
20b71 73 65 72 74 28 20 21 45 78 70 72 48 61 73 41 6e sert( !ExprHasAn
20b72 79 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f yProperty(p, EP_
20b73 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 52 65 64 TokenOnly|EP_Red
20b74 75 63 65 64 29 20 29 3b 0a 20 20 20 20 61 73 73 uced) );. ass
20b75 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f ert( !ExprHasPro
20b76 70 65 72 74 79 28 70 2c 20 45 50 5f 46 72 6f 6d perty(p, EP_From
20b77 4a 6f 69 6e 29 20 29 3b 20 0a 20 20 20 20 61 73 Join) ); . as
20b78 73 65 72 74 28 20 28 70 2d 3e 66 6c 61 67 73 32 sert( (p->flags2
20b79 20 26 20 45 50 32 5f 4d 61 6c 6c 6f 63 65 64 54 & EP2_MallocedT
20b7a 6f 6b 65 6e 29 3d 3d 30 20 29 3b 0a 20 20 20 20 oken)==0 );.
20b7b 61 73 73 65 72 74 28 20 28 70 2d 3e 66 6c 61 67 assert( (p->flag
20b7c 73 32 20 26 20 45 50 32 5f 49 72 72 65 64 75 63 s2 & EP2_Irreduc
20b7d 69 62 6c 65 29 3d 3d 30 20 29 3b 0a 20 20 20 20 ible)==0 );.
20b7e 69 66 28 20 70 2d 3e 70 4c 65 66 74 20 7c 7c 20 if( p->pLeft ||
20b7f 70 2d 3e 70 52 69 67 68 74 20 7c 7c 20 70 2d 3e p->pRight || p->
20b80 70 43 6f 6c 6c 20 7c 7c 20 70 2d 3e 78 2e 70 4c pColl || p->x.pL
20b81 69 73 74 20 29 7b 0a 20 20 20 20 20 20 6e 53 69 ist ){. nSi
20b82 7a 65 20 3d 20 45 58 50 52 5f 52 45 44 55 43 45 ze = EXPR_REDUCE
20b83 44 53 49 5a 45 20 7c 20 45 50 5f 52 65 64 75 63 DSIZE | EP_Reduc
20b84 65 64 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ed;. }else{.
20b85 20 20 20 20 20 6e 53 69 7a 65 20 3d 20 45 58 50 nSize = EXP
20b86 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 20 R_TOKENONLYSIZE
20b87 7c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 3b 0a | EP_TokenOnly;.
20b88 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
20b89 72 6e 20 6e 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a rn nSize;.}../*.
20b8a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
20b8b 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 70 61 returns the spa
20b8c 63 65 20 69 6e 20 62 79 74 65 73 20 72 65 71 75 ce in bytes requ
20b8d 69 72 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 ired to store th
20b8e 65 20 63 6f 70 79 20 0a 2a 2a 20 6f 66 20 74 68 e copy .** of th
20b8f 65 20 45 78 70 72 20 73 74 72 75 63 74 75 72 65 e Expr structure
20b90 20 61 6e 64 20 61 20 63 6f 70 79 20 6f 66 20 74 and a copy of t
20b91 68 65 20 45 78 70 72 2e 75 2e 7a 54 6f 6b 65 6e he Expr.u.zToken
20b92 20 73 74 72 69 6e 67 20 28 69 66 20 74 68 61 74 string (if that
20b93 0a 2a 2a 20 73 74 72 69 6e 67 20 69 73 20 64 65 .** string is de
20b94 66 69 6e 65 64 2e 29 0a 2a 2f 0a 73 74 61 74 69 fined.).*/.stati
20b95 63 20 69 6e 74 20 64 75 70 65 64 45 78 70 72 4e c int dupedExprN
20b96 6f 64 65 53 69 7a 65 28 45 78 70 72 20 2a 70 2c odeSize(Expr *p,
20b97 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 69 int flags){. i
20b98 6e 74 20 6e 42 79 74 65 20 3d 20 64 75 70 65 64 nt nByte = duped
20b99 45 78 70 72 53 74 72 75 63 74 53 69 7a 65 28 70 ExprStructSize(p
20b9a 2c 20 66 6c 61 67 73 29 20 26 20 30 78 66 66 66 , flags) & 0xfff
20b9b 3b 0a 20 20 69 66 28 20 21 45 78 70 72 48 61 73 ;. if( !ExprHas
20b9c 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 49 Property(p, EP_I
20b9d 6e 74 56 61 6c 75 65 29 20 26 26 20 70 2d 3e 75 ntValue) && p->u
20b9e 2e 7a 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 6e .zToken ){. n
20b9f 42 79 74 65 20 2b 3d 20 73 71 6c 69 74 65 33 53 Byte += sqlite3S
20ba0 74 72 6c 65 6e 33 30 28 70 2d 3e 75 2e 7a 54 6f trlen30(p->u.zTo
20ba1 6b 65 6e 29 2b 31 3b 0a 20 20 7d 0a 20 20 72 65 ken)+1;. }. re
20ba2 74 75 72 6e 20 52 4f 55 4e 44 38 28 6e 42 79 74 turn ROUND8(nByt
20ba3 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 e);.}../*.** Ret
20ba4 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
20ba5 66 20 62 79 74 65 73 20 72 65 71 75 69 72 65 64 f bytes required
20ba6 20 74 6f 20 63 72 65 61 74 65 20 61 20 64 75 70 to create a dup
20ba7 6c 69 63 61 74 65 20 6f 66 20 74 68 65 20 0a 2a licate of the .*
20ba8 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 70 61 73 * expression pas
20ba9 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74 sed as the first
20baa 20 61 72 67 75 6d 65 6e 74 2e 20 54 68 65 20 73 argument. The s
20bab 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 econd argument i
20bac 73 20 61 0a 2a 2a 20 6d 61 73 6b 20 63 6f 6e 74 s a.** mask cont
20bad 61 69 6e 69 6e 67 20 45 58 50 52 44 55 50 5f 58 aining EXPRDUP_X
20bae 58 58 20 66 6c 61 67 73 2e 0a 2a 2a 0a 2a 2a 20 XX flags..**.**
20baf 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e The value return
20bb0 65 64 20 69 6e 63 6c 75 64 65 73 20 73 70 61 63 ed includes spac
20bb1 65 20 74 6f 20 63 72 65 61 74 65 20 61 20 63 6f e to create a co
20bb2 70 79 20 6f 66 20 74 68 65 20 45 78 70 72 20 73 py of the Expr s
20bb3 74 72 75 63 74 0a 2a 2a 20 69 74 73 65 6c 66 20 truct.** itself
20bb4 61 6e 64 20 74 68 65 20 62 75 66 66 65 72 20 72 and the buffer r
20bb5 65 66 65 72 72 65 64 20 74 6f 20 62 79 20 45 78 eferred to by Ex
20bb6 70 72 2e 75 2e 7a 54 6f 6b 65 6e 2c 20 69 66 20 pr.u.zToken, if
20bb7 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 any..**.** If th
20bb8 65 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 e EXPRDUP_REDUCE
20bb9 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 flag is set, th
20bba 65 6e 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 en the return va
20bbb 6c 75 65 20 69 6e 63 6c 75 64 65 73 20 0a 2a 2a lue includes .**
20bbc 20 73 70 61 63 65 20 74 6f 20 64 75 70 6c 69 63 space to duplic
20bbd 61 74 65 20 61 6c 6c 20 45 78 70 72 20 6e 6f 64 ate all Expr nod
20bbe 65 73 20 69 6e 20 74 68 65 20 74 72 65 65 20 66 es in the tree f
20bbf 6f 72 6d 65 64 20 62 79 20 45 78 70 72 2e 70 4c ormed by Expr.pL
20bc0 65 66 74 20 0a 2a 2a 20 61 6e 64 20 45 78 70 72 eft .** and Expr
20bc1 2e 70 52 69 67 68 74 20 76 61 72 69 61 62 6c 65 .pRight variable
20bc2 73 20 28 62 75 74 20 6e 6f 74 20 66 6f 72 20 61 s (but not for a
20bc3 6e 79 20 73 74 72 75 63 74 75 72 65 73 20 70 6f ny structures po
20bc4 69 6e 74 65 64 20 74 6f 20 6f 72 20 0a 2a 2a 20 inted to or .**
20bc5 64 65 73 63 65 6e 64 65 64 20 66 72 6f 6d 20 74 descended from t
20bc6 68 65 20 45 78 70 72 2e 78 2e 70 4c 69 73 74 20 he Expr.x.pList
20bc7 6f 72 20 45 78 70 72 2e 78 2e 70 53 65 6c 65 63 or Expr.x.pSelec
20bc8 74 20 76 61 72 69 61 62 6c 65 73 29 2e 0a 2a 2f t variables)..*/
20bc9 0a 73 74 61 74 69 63 20 69 6e 74 20 64 75 70 65 .static int dupe
20bca 64 45 78 70 72 53 69 7a 65 28 45 78 70 72 20 2a dExprSize(Expr *
20bcb 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 p, int flags){.
20bcc 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 30 3b 0a int nByte = 0;.
20bcd 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 6e if( p ){. n
20bce 42 79 74 65 20 3d 20 64 75 70 65 64 45 78 70 72 Byte = dupedExpr
20bcf 4e 6f 64 65 53 69 7a 65 28 70 2c 20 66 6c 61 67 NodeSize(p, flag
20bd0 73 29 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 s);. if( flag
20bd1 73 26 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 s&EXPRDUP_REDUCE
20bd2 20 29 7b 0a 20 20 20 20 20 20 6e 42 79 74 65 20 ){. nByte
20bd3 2b 3d 20 64 75 70 65 64 45 78 70 72 53 69 7a 65 += dupedExprSize
20bd4 28 70 2d 3e 70 4c 65 66 74 2c 20 66 6c 61 67 73 (p->pLeft, flags
20bd5 29 20 2b 20 64 75 70 65 64 45 78 70 72 53 69 7a ) + dupedExprSiz
20bd6 65 28 70 2d 3e 70 52 69 67 68 74 2c 20 66 6c 61 e(p->pRight, fla
20bd7 67 73 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 gs);. }. }.
20bd8 20 72 65 74 75 72 6e 20 6e 42 79 74 65 3b 0a 7d return nByte;.}
20bd9 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
20bda 63 74 69 6f 6e 20 69 73 20 73 69 6d 69 6c 61 72 ction is similar
20bdb 20 74 6f 20 73 71 6c 69 74 65 33 45 78 70 72 44 to sqlite3ExprD
20bdc 75 70 28 29 2c 20 65 78 63 65 70 74 20 74 68 61 up(), except tha
20bdd 74 20 69 66 20 70 7a 42 75 66 66 65 72 20 0a 2a t if pzBuffer .*
20bde 2a 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 74 68 * is not NULL th
20bdf 65 6e 20 2a 70 7a 42 75 66 66 65 72 20 69 73 20 en *pzBuffer is
20be0 61 73 73 75 6d 65 64 20 74 6f 20 70 6f 69 6e 74 assumed to point
20be1 20 74 6f 20 61 20 62 75 66 66 65 72 20 6c 61 72 to a buffer lar
20be2 67 65 20 65 6e 6f 75 67 68 20 0a 2a 2a 20 74 6f ge enough .** to
20be3 20 73 74 6f 72 65 20 74 68 65 20 63 6f 70 79 20 store the copy
20be4 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 20 70 2c of expression p,
20be5 20 74 68 65 20 63 6f 70 69 65 73 20 6f 66 20 70 the copies of p
20be6 2d 3e 75 2e 7a 54 6f 6b 65 6e 0a 2a 2a 20 28 69 ->u.zToken.** (i
20be7 66 20 61 70 70 6c 69 63 61 62 6c 65 29 2c 20 61 f applicable), a
20be8 6e 64 20 74 68 65 20 63 6f 70 69 65 73 20 6f 66 nd the copies of
20be9 20 74 68 65 20 70 2d 3e 70 4c 65 66 74 20 61 6e the p->pLeft an
20bea 64 20 70 2d 3e 70 52 69 67 68 74 20 65 78 70 72 d p->pRight expr
20beb 65 73 73 69 6f 6e 73 2c 0a 2a 2a 20 69 66 20 61 essions,.** if a
20bec 6e 79 2e 20 42 65 66 6f 72 65 20 72 65 74 75 72 ny. Before retur
20bed 6e 69 6e 67 2c 20 2a 70 7a 42 75 66 66 65 72 20 ning, *pzBuffer
20bee 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 66 69 is set to the fi
20bef 72 73 74 20 62 79 74 65 20 70 61 73 73 65 64 20 rst byte passed
20bf0 74 68 65 0a 2a 2a 20 70 6f 72 74 69 6f 6e 20 6f the.** portion o
20bf1 66 20 74 68 65 20 62 75 66 66 65 72 20 63 6f 70 f the buffer cop
20bf2 69 65 64 20 69 6e 74 6f 20 62 79 20 74 68 69 73 ied into by this
20bf3 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 function..*/.st
20bf4 61 74 69 63 20 45 78 70 72 20 2a 65 78 70 72 44 atic Expr *exprD
20bf5 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 up(sqlite3 *db,
20bf6 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61 Expr *p, int fla
20bf7 67 73 2c 20 75 38 20 2a 2a 70 7a 42 75 66 66 65 gs, u8 **pzBuffe
20bf8 72 29 7b 0a 20 20 45 78 70 72 20 2a 70 4e 65 77 r){. Expr *pNew
20bf9 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
20bfa 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 /* Va
20bfb 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f lue to return */
20bfc 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 . if( p ){.
20bfd 63 6f 6e 73 74 20 69 6e 74 20 69 73 52 65 64 75 const int isRedu
20bfe 63 65 64 20 3d 20 28 66 6c 61 67 73 26 45 58 50 ced = (flags&EXP
20bff 52 44 55 50 5f 52 45 44 55 43 45 29 3b 0a 20 20 RDUP_REDUCE);.
20c00 20 20 75 38 20 2a 7a 41 6c 6c 6f 63 3b 0a 20 20 u8 *zAlloc;.
20c01 20 20 75 33 32 20 73 74 61 74 69 63 46 6c 61 67 u32 staticFlag
20c02 20 3d 20 30 3b 0a 0a 20 20 20 20 61 73 73 65 72 = 0;.. asser
20c03 74 28 20 70 7a 42 75 66 66 65 72 3d 3d 30 20 7c t( pzBuffer==0 |
20c04 7c 20 69 73 52 65 64 75 63 65 64 20 29 3b 0a 0a | isReduced );..
20c05 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 /* Figure ou
20c06 74 20 77 68 65 72 65 20 74 6f 20 77 72 69 74 65 t where to write
20c07 20 74 68 65 20 6e 65 77 20 45 78 70 72 20 73 74 the new Expr st
20c08 72 75 63 74 75 72 65 2e 20 2a 2f 0a 20 20 20 20 ructure. */.
20c09 69 66 28 20 70 7a 42 75 66 66 65 72 20 29 7b 0a if( pzBuffer ){.
20c0a 20 20 20 20 20 20 7a 41 6c 6c 6f 63 20 3d 20 2a zAlloc = *
20c0b 70 7a 42 75 66 66 65 72 3b 0a 20 20 20 20 20 20 pzBuffer;.
20c0c 73 74 61 74 69 63 46 6c 61 67 20 3d 20 45 50 5f staticFlag = EP_
20c0d 53 74 61 74 69 63 3b 0a 20 20 20 20 7d 65 6c 73 Static;. }els
20c0e 65 7b 0a 20 20 20 20 20 20 7a 41 6c 6c 6f 63 20 e{. zAlloc
20c0f 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f = sqlite3DbMallo
20c10 63 52 61 77 28 64 62 2c 20 64 75 70 65 64 45 78 cRaw(db, dupedEx
20c11 70 72 53 69 7a 65 28 70 2c 20 66 6c 61 67 73 29 prSize(p, flags)
20c12 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65 );. }. pNe
20c13 77 20 3d 20 28 45 78 70 72 20 2a 29 7a 41 6c 6c w = (Expr *)zAll
20c14 6f 63 3b 0a 0a 20 20 20 20 69 66 28 20 70 4e 65 oc;.. if( pNe
20c15 77 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 53 65 w ){. /* Se
20c16 74 20 6e 4e 65 77 53 69 7a 65 20 74 6f 20 74 68 t nNewSize to th
20c17 65 20 73 69 7a 65 20 61 6c 6c 6f 63 61 74 65 64 e size allocated
20c18 20 66 6f 72 20 74 68 65 20 73 74 72 75 63 74 75 for the structu
20c19 72 65 20 70 6f 69 6e 74 65 64 20 74 6f 0a 20 20 re pointed to.
20c1a 20 20 20 20 2a 2a 20 62 79 20 70 4e 65 77 2e 20 ** by pNew.
20c1b 54 68 69 73 20 69 73 20 65 69 74 68 65 72 20 45 This is either E
20c1c 58 50 52 5f 46 55 4c 4c 53 49 5a 45 2c 20 45 58 XPR_FULLSIZE, EX
20c1d 50 52 5f 52 45 44 55 43 45 44 53 49 5a 45 20 6f PR_REDUCEDSIZE o
20c1e 72 0a 20 20 20 20 20 20 2a 2a 20 45 58 50 52 5f r. ** EXPR_
20c1f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 2e 20 6e TOKENONLYSIZE. n
20c20 54 6f 6b 65 6e 20 69 73 20 73 65 74 20 74 6f 20 Token is set to
20c21 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
20c22 74 65 73 20 63 6f 6e 73 75 6d 65 64 0a 20 20 20 tes consumed.
20c23 20 20 20 2a 2a 20 62 79 20 74 68 65 20 63 6f 70 ** by the cop
20c24 79 20 6f 66 20 74 68 65 20 70 2d 3e 75 2e 7a 54 y of the p->u.zT
20c25 6f 6b 65 6e 20 73 74 72 69 6e 67 20 28 69 66 20 oken string (if
20c26 61 6e 79 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 any).. */.
20c27 20 20 20 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 const unsig
20c28 6e 65 64 20 6e 53 74 72 75 63 74 53 69 7a 65 20 ned nStructSize
20c29 3d 20 64 75 70 65 64 45 78 70 72 53 74 72 75 63 = dupedExprStruc
20c2a 74 53 69 7a 65 28 70 2c 20 66 6c 61 67 73 29 3b tSize(p, flags);
20c2b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 . const int
20c2c 20 6e 4e 65 77 53 69 7a 65 20 3d 20 6e 53 74 72 nNewSize = nStr
20c2d 75 63 74 53 69 7a 65 20 26 20 30 78 66 66 66 3b uctSize & 0xfff;
20c2e 0a 20 20 20 20 20 20 69 6e 74 20 6e 54 6f 6b 65 . int nToke
20c2f 6e 3b 0a 20 20 20 20 20 20 69 66 28 20 21 45 78 n;. if( !Ex
20c30 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c prHasProperty(p,
20c31 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 26 26 EP_IntValue) &&
20c32 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 29 7b 0a p->u.zToken ){.
20c33 20 20 20 20 20 20 20 20 6e 54 6f 6b 65 6e 20 3d nToken =
20c34 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
20c35 28 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 20 2b 20 (p->u.zToken) +
20c36 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 1;. }else{.
20c37 20 20 20 20 20 20 20 20 6e 54 6f 6b 65 6e 20 3d nToken =
20c38 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0;. }.
20c39 20 20 69 66 28 20 69 73 52 65 64 75 63 65 64 20 if( isReduced
20c3a 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 ){. asser
20c3b 74 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 t( ExprHasProper
20c3c 74 79 28 70 2c 20 45 50 5f 52 65 64 75 63 65 64 ty(p, EP_Reduced
20c3d 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 )==0 );.
20c3e 6d 65 6d 63 70 79 28 7a 41 6c 6c 6f 63 2c 20 70 memcpy(zAlloc, p
20c3f 2c 20 6e 4e 65 77 53 69 7a 65 29 3b 0a 20 20 20 , nNewSize);.
20c40 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
20c41 20 20 69 6e 74 20 6e 53 69 7a 65 20 3d 20 65 78 int nSize = ex
20c42 70 72 53 74 72 75 63 74 53 69 7a 65 28 70 29 3b prStructSize(p);
20c43 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 . memcpy(
20c44 7a 41 6c 6c 6f 63 2c 20 70 2c 20 6e 53 69 7a 65 zAlloc, p, nSize
20c45 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 );. memse
20c46 74 28 26 7a 41 6c 6c 6f 63 5b 6e 53 69 7a 65 5d t(&zAlloc[nSize]
20c47 2c 20 30 2c 20 45 58 50 52 5f 46 55 4c 4c 53 49 , 0, EXPR_FULLSI
20c48 5a 45 2d 6e 53 69 7a 65 29 3b 0a 20 20 20 20 20 ZE-nSize);.
20c49 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 53 65 74 }.. /* Set
20c4a 20 74 68 65 20 45 50 5f 52 65 64 75 63 65 64 2c the EP_Reduced,
20c4b 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 2c 20 61 EP_TokenOnly, a
20c4c 6e 64 20 45 50 5f 53 74 61 74 69 63 20 66 6c 61 nd EP_Static fla
20c4d 67 73 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 gs appropriately
20c4e 2e 20 2a 2f 0a 20 20 20 20 20 20 70 4e 65 77 2d . */. pNew-
20c4f 3e 66 6c 61 67 73 20 26 3d 20 7e 28 45 50 5f 52 >flags &= ~(EP_R
20c50 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f educed|EP_TokenO
20c51 6e 6c 79 7c 45 50 5f 53 74 61 74 69 63 29 3b 0a nly|EP_Static);.
20c52 20 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 pNew->flag
20c53 73 20 7c 3d 20 6e 53 74 72 75 63 74 53 69 7a 65 s |= nStructSize
20c54 20 26 20 28 45 50 5f 52 65 64 75 63 65 64 7c 45 & (EP_Reduced|E
20c55 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 3b 0a 20 20 P_TokenOnly);.
20c56 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 pNew->flags
20c57 7c 3d 20 73 74 61 74 69 63 46 6c 61 67 3b 0a 0a |= staticFlag;..
20c58 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 74 68 /* Copy th
20c59 65 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 73 74 e p->u.zToken st
20c5a 72 69 6e 67 2c 20 69 66 20 61 6e 79 2e 20 2a 2f ring, if any. */
20c5b 0a 20 20 20 20 20 20 69 66 28 20 6e 54 6f 6b 65 . if( nToke
20c5c 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 n ){. cha
20c5d 72 20 2a 7a 54 6f 6b 65 6e 20 3d 20 70 4e 65 77 r *zToken = pNew
20c5e 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 3d 20 28 63 68 ->u.zToken = (ch
20c5f 61 72 2a 29 26 7a 41 6c 6c 6f 63 5b 6e 4e 65 77 ar*)&zAlloc[nNew
20c60 53 69 7a 65 5d 3b 0a 20 20 20 20 20 20 20 20 6d Size];. m
20c61 65 6d 63 70 79 28 7a 54 6f 6b 65 6e 2c 20 70 2d emcpy(zToken, p-
20c62 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 >u.zToken, nToke
20c63 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 n);. }..
20c64 20 20 20 69 66 28 20 30 3d 3d 28 28 70 2d 3e 66 if( 0==((p->f
20c65 6c 61 67 73 7c 70 4e 65 77 2d 3e 66 6c 61 67 73 lags|pNew->flags
20c66 29 20 26 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 ) & EP_TokenOnly
20c67 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 ) ){. /*
20c68 46 69 6c 6c 20 69 6e 20 74 68 65 20 70 4e 65 77 Fill in the pNew
20c69 2d 3e 78 2e 70 53 65 6c 65 63 74 20 6f 72 20 70 ->x.pSelect or p
20c6a 4e 65 77 2d 3e 78 2e 70 4c 69 73 74 20 6d 65 6d New->x.pList mem
20c6b 62 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 ber. */.
20c6c 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 if( ExprHasPrope
20c6d 72 74 79 28 70 2c 20 45 50 5f 78 49 73 53 65 6c rty(p, EP_xIsSel
20c6e 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 ect) ){.
20c6f 20 20 70 4e 65 77 2d 3e 78 2e 70 53 65 6c 65 63 pNew->x.pSelec
20c70 74 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 t = sqlite3Selec
20c71 74 44 75 70 28 64 62 2c 20 70 2d 3e 78 2e 70 53 tDup(db, p->x.pS
20c72 65 6c 65 63 74 2c 20 69 73 52 65 64 75 63 65 64 elect, isReduced
20c73 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 );. }else
20c74 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 {. pNew
20c75 2d 3e 78 2e 70 4c 69 73 74 20 3d 20 73 71 6c 69 ->x.pList = sqli
20c76 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 te3ExprListDup(d
20c77 62 2c 20 70 2d 3e 78 2e 70 4c 69 73 74 2c 20 69 b, p->x.pList, i
20c78 73 52 65 64 75 63 65 64 29 3b 0a 20 20 20 20 20 sReduced);.
20c79 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 }. }..
20c7a 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 70 /* Fill in p
20c7b 4e 65 77 2d 3e 70 4c 65 66 74 20 61 6e 64 20 70 New->pLeft and p
20c7c 4e 65 77 2d 3e 70 52 69 67 68 74 2e 20 2a 2f 0a New->pRight. */.
20c7d 20 20 20 20 20 20 69 66 28 20 45 78 70 72 48 61 if( ExprHa
20c7e 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 4e 65 sAnyProperty(pNe
20c7f 77 2c 20 45 50 5f 52 65 64 75 63 65 64 7c 45 50 w, EP_Reduced|EP
20c80 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 _TokenOnly) ){.
20c81 20 20 20 20 20 20 20 7a 41 6c 6c 6f 63 20 2b 3d zAlloc +=
20c82 20 64 75 70 65 64 45 78 70 72 4e 6f 64 65 53 69 dupedExprNodeSi
20c83 7a 65 28 70 2c 20 66 6c 61 67 73 29 3b 0a 20 20 ze(p, flags);.
20c84 20 20 20 20 20 20 69 66 28 20 45 78 70 72 48 61 if( ExprHa
20c85 73 50 72 6f 70 65 72 74 79 28 70 4e 65 77 2c 20 sProperty(pNew,
20c86 45 50 5f 52 65 64 75 63 65 64 29 20 29 7b 0a 20 EP_Reduced) ){.
20c87 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 pNew->p
20c88 4c 65 66 74 20 3d 20 65 78 70 72 44 75 70 28 64 Left = exprDup(d
20c89 62 2c 20 70 2d 3e 70 4c 65 66 74 2c 20 45 58 50 b, p->pLeft, EXP
20c8a 52 44 55 50 5f 52 45 44 55 43 45 2c 20 26 7a 41 RDUP_REDUCE, &zA
20c8b 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20 20 20 20 lloc);.
20c8c 20 70 4e 65 77 2d 3e 70 52 69 67 68 74 20 3d 20 pNew->pRight =
20c8d 65 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 exprDup(db, p->p
20c8e 52 69 67 68 74 2c 20 45 58 50 52 44 55 50 5f 52 Right, EXPRDUP_R
20c8f 45 44 55 43 45 2c 20 26 7a 41 6c 6c 6f 63 29 3b EDUCE, &zAlloc);
20c90 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
20c91 20 20 20 69 66 28 20 70 7a 42 75 66 66 65 72 20 if( pzBuffer
20c92 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 70 7a ){. *pz
20c93 42 75 66 66 65 72 20 3d 20 7a 41 6c 6c 6f 63 3b Buffer = zAlloc;
20c94 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
20c95 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
20c96 70 4e 65 77 2d 3e 66 6c 61 67 73 32 20 3d 20 30 pNew->flags2 = 0
20c97 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 45 ;. if( !E
20c98 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 xprHasAnyPropert
20c99 79 28 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c y(p, EP_TokenOnl
20c9a 79 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 y) ){.
20c9b 70 4e 65 77 2d 3e 70 4c 65 66 74 20 3d 20 73 71 pNew->pLeft = sq
20c9c 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c lite3ExprDup(db,
20c9d 20 70 2d 3e 70 4c 65 66 74 2c 20 30 29 3b 0a 20 p->pLeft, 0);.
20c9e 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 pNew->p
20c9f 52 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33 45 Right = sqlite3E
20ca0 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 52 xprDup(db, p->pR
20ca1 69 67 68 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 ight, 0);.
20ca2 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 }. }..
20ca3 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
20ca4 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 pNew;.}../*.** T
20ca5 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 72 6f he following gro
20ca6 75 70 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 6d up of routines m
20ca7 61 6b 65 20 64 65 65 70 20 63 6f 70 69 65 73 20 ake deep copies
20ca8 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 2c 0a of expressions,.
20ca9 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 ** expression li
20caa 73 74 73 2c 20 49 44 20 6c 69 73 74 73 2c 20 61 sts, ID lists, a
20cab 6e 64 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d nd select statem
20cac 65 6e 74 73 2e 20 20 54 68 65 20 63 6f 70 69 65 ents. The copie
20cad 73 20 63 61 6e 0a 2a 2a 20 62 65 20 64 65 6c 65 s can.** be dele
20cae 74 65 64 20 28 62 79 20 62 65 69 6e 67 20 70 61 ted (by being pa
20caf 73 73 65 64 20 74 6f 20 74 68 65 69 72 20 72 65 ssed to their re
20cb0 73 70 65 63 74 69 76 65 20 2e 2e 2e 44 65 6c 65 spective ...Dele
20cb1 74 65 28 29 20 72 6f 75 74 69 6e 65 73 29 0a 2a te() routines).*
20cb2 2a 20 77 69 74 68 6f 75 74 20 65 66 66 65 63 74 * without effect
20cb3 69 6e 67 20 74 68 65 20 6f 72 69 67 69 6e 61 6c ing the original
20cb4 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 78 70 s..**.** The exp
20cb5 72 65 73 73 69 6f 6e 20 6c 69 73 74 2c 20 49 44 ression list, ID
20cb6 2c 20 61 6e 64 20 73 6f 75 72 63 65 20 6c 69 73 , and source lis
20cb7 74 73 20 72 65 74 75 72 6e 20 62 79 20 73 71 6c ts return by sql
20cb8 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 ite3ExprListDup(
20cb9 29 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 49 64 4c ),.** sqlite3IdL
20cba 69 73 74 44 75 70 28 29 2c 20 61 6e 64 20 73 71 istDup(), and sq
20cbb 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75 70 28 lite3SrcListDup(
20cbc 29 20 63 61 6e 20 6e 6f 74 20 62 65 20 66 75 72 ) can not be fur
20cbd 74 68 65 72 20 65 78 70 61 6e 64 65 64 20 0a 2a ther expanded .*
20cbe 2a 20 62 79 20 73 75 62 73 65 71 75 65 6e 74 20 * by subsequent
20cbf 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 2a calls to sqlite*
20cc0 4c 69 73 74 41 70 70 65 6e 64 28 29 20 72 6f 75 ListAppend() rou
20cc1 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 tines..**.** Any
20cc2 20 74 61 62 6c 65 73 20 74 68 61 74 20 74 68 65 tables that the
20cc3 20 53 72 63 4c 69 73 74 20 6d 69 67 68 74 20 70 SrcList might p
20cc4 6f 69 6e 74 20 74 6f 20 61 72 65 20 6e 6f 74 20 oint to are not
20cc5 64 75 70 6c 69 63 61 74 65 64 2e 0a 2a 2a 0a 2a duplicated..**.*
20cc6 2a 20 54 68 65 20 66 6c 61 67 73 20 70 61 72 61 * The flags para
20cc7 6d 65 74 65 72 20 63 6f 6e 74 61 69 6e 73 20 61 meter contains a
20cc8 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 combination of
20cc9 74 68 65 20 45 58 50 52 44 55 50 5f 58 58 58 20 the EXPRDUP_XXX
20cca 66 6c 61 67 73 2e 0a 2a 2a 20 49 66 20 74 68 65 flags..** If the
20ccb 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 20 EXPRDUP_REDUCE
20ccc 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 flag is set, the
20ccd 6e 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20 n the structure
20cce 72 65 74 75 72 6e 65 64 20 69 73 20 61 0a 2a 2a returned is a.**
20ccf 20 74 72 75 6e 63 61 74 65 64 20 76 65 72 73 69 truncated versi
20cd0 6f 6e 20 6f 66 20 74 68 65 20 75 73 75 61 6c 20 on of the usual
20cd1 45 78 70 72 20 73 74 72 75 63 74 75 72 65 20 74 Expr structure t
20cd2 68 61 74 20 77 69 6c 6c 20 62 65 20 73 74 6f 72 hat will be stor
20cd3 65 64 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 ed as.** part of
20cd4 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 the in-memory r
20cd5 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 epresentation of
20cd6 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 the database sc
20cd7 68 65 6d 61 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f hema..*/.SQLITE_
20cd8 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 PRIVATE Expr *sq
20cd9 6c 69 74 65 33 45 78 70 72 44 75 70 28 73 71 6c lite3ExprDup(sql
20cda 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20 2a ite3 *db, Expr *
20cdb 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 p, int flags){.
20cdc 20 72 65 74 75 72 6e 20 65 78 70 72 44 75 70 28 return exprDup(
20cdd 64 62 2c 20 70 2c 20 66 6c 61 67 73 2c 20 30 29 db, p, flags, 0)
20cde 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
20cdf 54 45 20 45 78 70 72 4c 69 73 74 20 2a 73 71 6c TE ExprList *sql
20ce0 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 ite3ExprListDup(
20ce1 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 sqlite3 *db, Exp
20ce2 72 4c 69 73 74 20 2a 70 2c 20 69 6e 74 20 66 6c rList *p, int fl
20ce3 61 67 73 29 7b 0a 20 20 45 78 70 72 4c 69 73 74 ags){. ExprList
20ce4 20 2a 70 4e 65 77 3b 0a 20 20 73 74 72 75 63 74 *pNew;. struct
20ce5 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a ExprList_item *
20ce6 70 49 74 65 6d 2c 20 2a 70 4f 6c 64 49 74 65 6d pItem, *pOldItem
20ce7 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 ;. int i;. if(
20ce8 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 p==0 ) return 0
20ce9 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 ;. pNew = sqlit
20cea 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 e3DbMallocRaw(db
20ceb 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 20 , sizeof(*pNew)
20cec 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 );. if( pNew==0
20ced 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 ) return 0;. p
20cee 4e 65 77 2d 3e 69 45 43 75 72 73 6f 72 20 3d 20 New->iECursor =
20cef 30 3b 0a 20 20 70 4e 65 77 2d 3e 6e 45 78 70 72 0;. pNew->nExpr
20cf0 20 3d 20 70 4e 65 77 2d 3e 6e 41 6c 6c 6f 63 20 = pNew->nAlloc
20cf1 3d 20 70 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 4e = p->nExpr;. pN
20cf2 65 77 2d 3e 61 20 3d 20 70 49 74 65 6d 20 3d 20 ew->a = pItem =
20cf3 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 sqlite3DbMallocR
20cf4 61 77 28 64 62 2c 20 20 70 2d 3e 6e 45 78 70 72 aw(db, p->nExpr
20cf5 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 5b 30 5d 29 *sizeof(p->a[0])
20cf6 20 29 3b 0a 20 20 69 66 28 20 70 49 74 65 6d 3d );. if( pItem=
20cf7 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 =0 ){. sqlite
20cf8 33 44 62 46 72 65 65 28 64 62 2c 20 70 4e 65 77 3DbFree(db, pNew
20cf9 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b );. return 0;
20cfa 0a 20 20 7d 20 0a 20 20 70 4f 6c 64 49 74 65 6d . } . pOldItem
20cfb 20 3d 20 70 2d 3e 61 3b 0a 20 20 66 6f 72 28 69 = p->a;. for(i
20cfc 3d 30 3b 20 69 3c 70 2d 3e 6e 45 78 70 72 3b 20 =0; i<p->nExpr;
20cfd 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 2c 20 70 4f i++, pItem++, pO
20cfe 6c 64 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 45 ldItem++){. E
20cff 78 70 72 20 2a 70 4f 6c 64 45 78 70 72 20 3d 20 xpr *pOldExpr =
20d00 70 4f 6c 64 49 74 65 6d 2d 3e 70 45 78 70 72 3b pOldItem->pExpr;
20d01 0a 20 20 20 20 70 49 74 65 6d 2d 3e 70 45 78 70 . pItem->pExp
20d02 72 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 r = sqlite3ExprD
20d03 75 70 28 64 62 2c 20 70 4f 6c 64 45 78 70 72 2c up(db, pOldExpr,
20d04 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 70 49 74 flags);. pIt
20d05 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 em->zName = sqli
20d06 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 te3DbStrDup(db,
20d07 70 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 pOldItem->zName)
20d08 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 7a 53 70 ;. pItem->zSp
20d09 61 6e 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 an = sqlite3DbSt
20d0a 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 rDup(db, pOldIte
20d0b 6d 2d 3e 7a 53 70 61 6e 29 3b 0a 20 20 20 20 70 m->zSpan);. p
20d0c 49 74 65 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 20 Item->sortOrder
20d0d 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 73 6f 72 74 = pOldItem->sort
20d0e 4f 72 64 65 72 3b 0a 20 20 20 20 70 49 74 65 6d Order;. pItem
20d0f 2d 3e 64 6f 6e 65 20 3d 20 30 3b 0a 20 20 20 20 ->done = 0;.
20d10 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 70 4f pItem->iCol = pO
20d11 6c 64 49 74 65 6d 2d 3e 69 43 6f 6c 3b 0a 20 20 ldItem->iCol;.
20d12 20 20 70 49 74 65 6d 2d 3e 69 41 6c 69 61 73 20 pItem->iAlias
20d13 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 41 6c 69 = pOldItem->iAli
20d14 61 73 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e as;. }. return
20d15 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 pNew;.}../*.**
20d16 49 66 20 63 75 72 73 6f 72 73 2c 20 74 72 69 67 If cursors, trig
20d17 67 65 72 73 2c 20 76 69 65 77 73 20 61 6e 64 20 gers, views and
20d18 73 75 62 71 75 65 72 69 65 73 20 61 72 65 20 61 subqueries are a
20d19 6c 6c 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 0a ll omitted from.
20d1a 2a 2a 20 74 68 65 20 62 75 69 6c 64 2c 20 74 68 ** the build, th
20d1b 65 6e 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 66 en none of the f
20d1c 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 ollowing routine
20d1d 73 2c 20 65 78 63 65 70 74 20 66 6f 72 20 0a 2a s, except for .*
20d1e 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 * sqlite3SelectD
20d1f 75 70 28 29 2c 20 63 61 6e 20 62 65 20 63 61 6c up(), can be cal
20d20 6c 65 64 2e 20 73 71 6c 69 74 65 33 53 65 6c 65 led. sqlite3Sele
20d21 63 74 44 75 70 28 29 20 69 73 20 73 6f 6d 65 74 ctDup() is somet
20d22 69 6d 65 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 77 imes.** called w
20d23 69 74 68 20 61 20 4e 55 4c 4c 20 61 72 67 75 6d ith a NULL argum
20d24 65 6e 74 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 ent..*/.#if !def
20d25 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
20d26 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e _VIEW) || !defin
20d27 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 ed(SQLITE_OMIT_T
20d28 52 49 47 47 45 52 29 20 5c 0a 20 7c 7c 20 21 64 RIGGER) \. || !d
20d29 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
20d2a 49 54 5f 53 55 42 51 55 45 52 59 29 0a 53 51 4c IT_SUBQUERY).SQL
20d2b 49 54 45 5f 50 52 49 56 41 54 45 20 53 72 63 4c ITE_PRIVATE SrcL
20d2c 69 73 74 20 2a 73 71 6c 69 74 65 33 53 72 63 4c ist *sqlite3SrcL
20d2d 69 73 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a istDup(sqlite3 *
20d2e 64 62 2c 20 53 72 63 4c 69 73 74 20 2a 70 2c 20 db, SrcList *p,
20d2f 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 53 72 int flags){. Sr
20d30 63 4c 69 73 74 20 2a 70 4e 65 77 3b 0a 20 20 69 cList *pNew;. i
20d31 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 42 79 74 nt i;. int nByt
20d32 65 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 e;. if( p==0 )
20d33 72 65 74 75 72 6e 20 30 3b 0a 20 20 6e 42 79 74 return 0;. nByt
20d34 65 20 3d 20 73 69 7a 65 6f 66 28 2a 70 29 20 2b e = sizeof(*p) +
20d35 20 28 70 2d 3e 6e 53 72 63 3e 30 20 3f 20 73 69 (p->nSrc>0 ? si
20d36 7a 65 6f 66 28 70 2d 3e 61 5b 30 5d 29 20 2a 20 zeof(p->a[0]) *
20d37 28 70 2d 3e 6e 53 72 63 2d 31 29 20 3a 20 30 29 (p->nSrc-1) : 0)
20d38 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 ;. pNew = sqlit
20d39 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 e3DbMallocRaw(db
20d3a 2c 20 6e 42 79 74 65 20 29 3b 0a 20 20 69 66 28 , nByte );. if(
20d3b 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 pNew==0 ) retur
20d3c 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 6e 53 72 n 0;. pNew->nSr
20d3d 63 20 3d 20 70 4e 65 77 2d 3e 6e 41 6c 6c 6f 63 c = pNew->nAlloc
20d3e 20 3d 20 70 2d 3e 6e 53 72 63 3b 0a 20 20 66 6f = p->nSrc;. fo
20d3f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 53 72 63 r(i=0; i<p->nSrc
20d40 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75 ; i++){. stru
20d41 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 ct SrcList_item
20d42 2a 70 4e 65 77 49 74 65 6d 20 3d 20 26 70 4e 65 *pNewItem = &pNe
20d43 77 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 73 74 72 w->a[i];. str
20d44 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d uct SrcList_item
20d45 20 2a 70 4f 6c 64 49 74 65 6d 20 3d 20 26 70 2d *pOldItem = &p-
20d46 3e 61 5b 69 5d 3b 0a 20 20 20 20 54 61 62 6c 65 >a[i];. Table
20d47 20 2a 70 54 61 62 3b 0a 20 20 20 20 70 4e 65 77 *pTab;. pNew
20d48 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 20 Item->zDatabase
20d49 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 = sqlite3DbStrDu
20d4a 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e p(db, pOldItem->
20d4b 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20 zDatabase);.
20d4c 70 4e 65 77 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 pNewItem->zName
20d4d 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 = sqlite3DbStrDu
20d4e 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e p(db, pOldItem->
20d4f 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70 4e 65 77 zName);. pNew
20d50 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 3d 20 73 Item->zAlias = s
20d51 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 qlite3DbStrDup(d
20d52 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 41 6c b, pOldItem->zAl
20d53 69 61 73 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 ias);. pNewIt
20d54 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65 20 3d 20 70 em->jointype = p
20d55 4f 6c 64 49 74 65 6d 2d 3e 6a 6f 69 6e 74 79 70 OldItem->jointyp
20d56 65 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d e;. pNewItem-
20d57 3e 69 43 75 72 73 6f 72 20 3d 20 70 4f 6c 64 49 >iCursor = pOldI
20d58 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 tem->iCursor;.
20d59 20 20 70 4e 65 77 49 74 65 6d 2d 3e 69 73 50 6f pNewItem->isPo
20d5a 70 75 6c 61 74 65 64 20 3d 20 70 4f 6c 64 49 74 pulated = pOldIt
20d5b 65 6d 2d 3e 69 73 50 6f 70 75 6c 61 74 65 64 3b em->isPopulated;
20d5c 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a . pNewItem->z
20d5d 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33 44 Index = sqlite3D
20d5e 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 bStrDup(db, pOld
20d5f 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 29 3b 0a 20 Item->zIndex);.
20d60 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 6e 6f 74 pNewItem->not
20d61 49 6e 64 65 78 65 64 20 3d 20 70 4f 6c 64 49 74 Indexed = pOldIt
20d62 65 6d 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 3b 0a em->notIndexed;.
20d63 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70 49 pNewItem->pI
20d64 6e 64 65 78 20 3d 20 70 4f 6c 64 49 74 65 6d 2d ndex = pOldItem-
20d65 3e 70 49 6e 64 65 78 3b 0a 20 20 20 20 70 54 61 >pIndex;. pTa
20d66 62 20 3d 20 70 4e 65 77 49 74 65 6d 2d 3e 70 54 b = pNewItem->pT
20d67 61 62 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 ab = pOldItem->p
20d68 54 61 62 3b 0a 20 20 20 20 69 66 28 20 70 54 61 Tab;. if( pTa
20d69 62 20 29 7b 0a 20 20 20 20 20 20 70 54 61 62 2d b ){. pTab-
20d6a 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20 >nRef++;. }.
20d6b 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70 53 65 pNewItem->pSe
20d6c 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65 lect = sqlite3Se
20d6d 6c 65 63 74 44 75 70 28 64 62 2c 20 70 4f 6c 64 lectDup(db, pOld
20d6e 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 2c 20 66 Item->pSelect, f
20d6f 6c 61 67 73 29 3b 0a 20 20 20 20 70 4e 65 77 49 lags);. pNewI
20d70 74 65 6d 2d 3e 70 4f 6e 20 3d 20 73 71 6c 69 74 tem->pOn = sqlit
20d71 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f e3ExprDup(db, pO
20d72 6c 64 49 74 65 6d 2d 3e 70 4f 6e 2c 20 66 6c 61 ldItem->pOn, fla
20d73 67 73 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 gs);. pNewIte
20d74 6d 2d 3e 70 55 73 69 6e 67 20 3d 20 73 71 6c 69 m->pUsing = sqli
20d75 74 65 33 49 64 4c 69 73 74 44 75 70 28 64 62 2c te3IdListDup(db,
20d76 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 55 73 69 6e pOldItem->pUsin
20d77 67 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d g);. pNewItem
20d78 2d 3e 63 6f 6c 55 73 65 64 20 3d 20 70 4f 6c 64 ->colUsed = pOld
20d79 49 74 65 6d 2d 3e 63 6f 6c 55 73 65 64 3b 0a 20 Item->colUsed;.
20d7a 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 }. return pNew
20d7b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
20d7c 54 45 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 TE IdList *sqlit
20d7d 65 33 49 64 4c 69 73 74 44 75 70 28 73 71 6c 69 e3IdListDup(sqli
20d7e 74 65 33 20 2a 64 62 2c 20 49 64 4c 69 73 74 20 te3 *db, IdList
20d7f 2a 70 29 7b 0a 20 20 49 64 4c 69 73 74 20 2a 70 *p){. IdList *p
20d80 4e 65 77 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 New;. int i;.
20d81 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 if( p==0 ) retur
20d82 6e 20 30 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 n 0;. pNew = sq
20d83 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 lite3DbMallocRaw
20d84 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 (db, sizeof(*pNe
20d85 77 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 w) );. if( pNew
20d86 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a ==0 ) return 0;.
20d87 20 20 70 4e 65 77 2d 3e 6e 49 64 20 3d 20 70 4e pNew->nId = pN
20d88 65 77 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e ew->nAlloc = p->
20d89 6e 49 64 3b 0a 20 20 70 4e 65 77 2d 3e 61 20 3d nId;. pNew->a =
20d8a 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
20d8b 52 61 77 28 64 62 2c 20 70 2d 3e 6e 49 64 2a 73 Raw(db, p->nId*s
20d8c 69 7a 65 6f 66 28 70 2d 3e 61 5b 30 5d 29 20 29 izeof(p->a[0]) )
20d8d 3b 0a 20 20 69 66 28 20 70 4e 65 77 2d 3e 61 3d ;. if( pNew->a=
20d8e 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 =0 ){. sqlite
20d8f 33 44 62 46 72 65 65 28 64 62 2c 20 70 4e 65 77 3DbFree(db, pNew
20d90 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b );. return 0;
20d91 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 . }. for(i=0;
20d92 69 3c 70 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a i<p->nId; i++){.
20d93 20 20 20 20 73 74 72 75 63 74 20 49 64 4c 69 73 struct IdLis
20d94 74 5f 69 74 65 6d 20 2a 70 4e 65 77 49 74 65 6d t_item *pNewItem
20d95 20 3d 20 26 70 4e 65 77 2d 3e 61 5b 69 5d 3b 0a = &pNew->a[i];.
20d96 20 20 20 20 73 74 72 75 63 74 20 49 64 4c 69 73 struct IdLis
20d97 74 5f 69 74 65 6d 20 2a 70 4f 6c 64 49 74 65 6d t_item *pOldItem
20d98 20 3d 20 26 70 2d 3e 61 5b 69 5d 3b 0a 20 20 20 = &p->a[i];.
20d99 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 4e 61 6d 65 pNewItem->zName
20d9a 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 = sqlite3DbStrD
20d9b 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d up(db, pOldItem-
20d9c 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70 4e 65 >zName);. pNe
20d9d 77 49 74 65 6d 2d 3e 69 64 78 20 3d 20 70 4f 6c wItem->idx = pOl
20d9e 64 49 74 65 6d 2d 3e 69 64 78 3b 0a 20 20 7d 0a dItem->idx;. }.
20d9f 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d return pNew;.}
20da0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
20da1 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 65 33 53 Select *sqlite3S
20da2 65 6c 65 63 74 44 75 70 28 73 71 6c 69 74 65 33 electDup(sqlite3
20da3 20 2a 64 62 2c 20 53 65 6c 65 63 74 20 2a 70 2c *db, Select *p,
20da4 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 53 int flags){. S
20da5 65 6c 65 63 74 20 2a 70 4e 65 77 3b 0a 20 20 69 elect *pNew;. i
20da6 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e f( p==0 ) return
20da7 20 30 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 0;. pNew = sql
20da8 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 ite3DbMallocRaw(
20da9 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 db, sizeof(*p) )
20daa 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 ;. if( pNew==0
20dab 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e ) return 0;. pN
20dac 65 77 2d 3e 70 45 4c 69 73 74 20 3d 20 73 71 6c ew->pEList = sql
20dad 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 ite3ExprListDup(
20dae 64 62 2c 20 70 2d 3e 70 45 4c 69 73 74 2c 20 66 db, p->pEList, f
20daf 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 lags);. pNew->p
20db0 53 72 63 20 3d 20 73 71 6c 69 74 65 33 53 72 63 Src = sqlite3Src
20db1 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 ListDup(db, p->p
20db2 53 72 63 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 Src, flags);. p
20db3 4e 65 77 2d 3e 70 57 68 65 72 65 20 3d 20 73 71 New->pWhere = sq
20db4 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c lite3ExprDup(db,
20db5 20 70 2d 3e 70 57 68 65 72 65 2c 20 66 6c 61 67 p->pWhere, flag
20db6 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 47 72 6f s);. pNew->pGro
20db7 75 70 42 79 20 3d 20 73 71 6c 69 74 65 33 45 78 upBy = sqlite3Ex
20db8 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d prListDup(db, p-
20db9 3e 70 47 72 6f 75 70 42 79 2c 20 66 6c 61 67 73 >pGroupBy, flags
20dba 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 48 61 76 69 );. pNew->pHavi
20dbb 6e 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 ng = sqlite3Expr
20dbc 44 75 70 28 64 62 2c 20 70 2d 3e 70 48 61 76 69 Dup(db, p->pHavi
20dbd 6e 67 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e ng, flags);. pN
20dbe 65 77 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 73 ew->pOrderBy = s
20dbf 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 qlite3ExprListDu
20dc0 70 28 64 62 2c 20 70 2d 3e 70 4f 72 64 65 72 42 p(db, p->pOrderB
20dc1 79 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 y, flags);. pNe
20dc2 77 2d 3e 6f 70 20 3d 20 70 2d 3e 6f 70 3b 0a 20 w->op = p->op;.
20dc3 20 70 4e 65 77 2d 3e 70 50 72 69 6f 72 20 3d 20 pNew->pPrior =
20dc4 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 sqlite3SelectDup
20dc5 28 64 62 2c 20 70 2d 3e 70 50 72 69 6f 72 2c 20 (db, p->pPrior,
20dc6 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e flags);. pNew->
20dc7 70 4c 69 6d 69 74 20 3d 20 73 71 6c 69 74 65 33 pLimit = sqlite3
20dc8 45 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 ExprDup(db, p->p
20dc9 4c 69 6d 69 74 2c 20 66 6c 61 67 73 29 3b 0a 20 Limit, flags);.
20dca 20 70 4e 65 77 2d 3e 70 4f 66 66 73 65 74 20 3d pNew->pOffset =
20dcb 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 sqlite3ExprDup(
20dcc 64 62 2c 20 70 2d 3e 70 4f 66 66 73 65 74 2c 20 db, p->pOffset,
20dcd 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e flags);. pNew->
20dce 69 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 70 4e iLimit = 0;. pN
20dcf 65 77 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30 3b ew->iOffset = 0;
20dd0 0a 20 20 70 4e 65 77 2d 3e 73 65 6c 46 6c 61 67 . pNew->selFlag
20dd1 73 20 3d 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 s = p->selFlags
20dd2 26 20 7e 53 46 5f 55 73 65 73 45 70 68 65 6d 65 & ~SF_UsesEpheme
20dd3 72 61 6c 3b 0a 20 20 70 4e 65 77 2d 3e 70 52 69 ral;. pNew->pRi
20dd4 67 68 74 6d 6f 73 74 20 3d 20 30 3b 0a 20 20 70 ghtmost = 0;. p
20dd5 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 New->addrOpenEph
20dd6 6d 5b 30 5d 20 3d 20 2d 31 3b 0a 20 20 70 4e 65 m[0] = -1;. pNe
20dd7 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b w->addrOpenEphm[
20dd8 31 5d 20 3d 20 2d 31 3b 0a 20 20 70 4e 65 77 2d 1] = -1;. pNew-
20dd9 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 32 5d >addrOpenEphm[2]
20dda 20 3d 20 2d 31 3b 0a 20 20 72 65 74 75 72 6e 20 = -1;. return
20ddb 70 4e 65 77 3b 0a 7d 0a 23 65 6c 73 65 0a 53 51 pNew;.}.#else.SQ
20ddc 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 65 6c LITE_PRIVATE Sel
20ddd 65 63 74 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 ect *sqlite3Sele
20dde 63 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 ctDup(sqlite3 *d
20ddf 62 2c 20 53 65 6c 65 63 74 20 2a 70 2c 20 69 6e b, Select *p, in
20de0 74 20 66 6c 61 67 73 29 7b 0a 20 20 61 73 73 65 t flags){. asse
20de1 72 74 28 20 70 3d 3d 30 20 29 3b 0a 20 20 72 65 rt( p==0 );. re
20de2 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 turn 0;.}.#endif
20de3 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e .../*.** Add a n
20de4 65 77 20 65 6c 65 6d 65 6e 74 20 74 6f 20 74 68 ew element to th
20de5 65 20 65 6e 64 20 6f 66 20 61 6e 20 65 78 70 72 e end of an expr
20de6 65 73 73 69 6f 6e 20 6c 69 73 74 2e 20 20 49 66 ession list. If
20de7 20 70 4c 69 73 74 20 69 73 0a 2a 2a 20 69 6e 69 pList is.** ini
20de8 74 69 61 6c 6c 79 20 4e 55 4c 4c 2c 20 74 68 65 tially NULL, the
20de9 6e 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 65 n create a new e
20dea 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a xpression list..
20deb 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72 **.** If a memor
20dec 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 y allocation err
20ded 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65 20 65 or occurs, the e
20dee 6e 74 69 72 65 20 6c 69 73 74 20 69 73 20 66 72 ntire list is fr
20def 65 65 64 20 61 6e 64 0a 2a 2a 20 4e 55 4c 4c 20 eed and.** NULL
20df0 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66 is returned. If
20df1 20 6e 6f 6e 2d 4e 55 4c 4c 20 69 73 20 72 65 74 non-NULL is ret
20df2 75 72 6e 65 64 2c 20 74 68 65 6e 20 69 74 20 69 urned, then it i
20df3 73 20 67 75 61 72 61 6e 74 65 65 64 0a 2a 2a 20 s guaranteed.**
20df4 74 68 61 74 20 74 68 65 20 6e 65 77 20 65 6e 74 that the new ent
20df5 72 79 20 77 61 73 20 73 75 63 63 65 73 73 66 75 ry was successfu
20df6 6c 6c 79 20 61 70 70 65 6e 64 65 64 2e 0a 2a 2f lly appended..*/
20df7 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
20df8 45 78 70 72 4c 69 73 74 20 2a 73 71 6c 69 74 65 ExprList *sqlite
20df9 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 3ExprListAppend(
20dfa 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
20dfb 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 , /* Pa
20dfc 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f rsing context */
20dfd 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 . ExprList *pLi
20dfe 73 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 st, /* Li
20dff 73 74 20 74 6f 20 77 68 69 63 68 20 74 6f 20 61 st to which to a
20e00 70 70 65 6e 64 2e 20 4d 69 67 68 74 20 62 65 20 ppend. Might be
20e01 4e 55 4c 4c 20 2a 2f 0a 20 20 45 78 70 72 20 2a NULL */. Expr *
20e02 70 45 78 70 72 20 20 20 20 20 20 20 20 20 20 20 pExpr
20e03 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 /* Expression
20e04 74 6f 20 62 65 20 61 70 70 65 6e 64 65 64 2e 20 to be appended.
20e05 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f Might be NULL */
20e06 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 .){. sqlite3 *d
20e07 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a b = pParse->db;.
20e08 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 if( pList==0 )
20e09 7b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 73 71 {. pList = sq
20e0a 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 lite3DbMallocZer
20e0b 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 45 78 70 o(db, sizeof(Exp
20e0c 72 4c 69 73 74 29 20 29 3b 0a 20 20 20 20 69 66 rList) );. if
20e0d 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 ( pList==0 ){.
20e0e 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b goto no_mem;
20e0f 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 . }. asser
20e10 74 28 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 t( pList->nAlloc
20e11 3d 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 ==0 );. }. if(
20e12 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3c 3d pList->nAlloc<=
20e13 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a pList->nExpr ){.
20e14 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c struct ExprL
20e15 69 73 74 5f 69 74 65 6d 20 2a 61 3b 0a 20 20 20 ist_item *a;.
20e16 20 69 6e 74 20 6e 20 3d 20 70 4c 69 73 74 2d 3e int n = pList->
20e17 6e 41 6c 6c 6f 63 2a 32 20 2b 20 34 3b 0a 20 20 nAlloc*2 + 4;.
20e18 20 20 61 20 3d 20 73 71 6c 69 74 65 33 44 62 52 a = sqlite3DbR
20e19 65 61 6c 6c 6f 63 28 64 62 2c 20 70 4c 69 73 74 ealloc(db, pList
20e1a 2d 3e 61 2c 20 6e 2a 73 69 7a 65 6f 66 28 70 4c ->a, n*sizeof(pL
20e1b 69 73 74 2d 3e 61 5b 30 5d 29 29 3b 0a 20 20 20 ist->a[0]));.
20e1c 20 69 66 28 20 61 3d 3d 30 20 29 7b 0a 20 20 20 if( a==0 ){.
20e1d 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a goto no_mem;.
20e1e 20 20 20 20 7d 0a 20 20 20 20 70 4c 69 73 74 2d }. pList-
20e1f 3e 61 20 3d 20 61 3b 0a 20 20 20 20 70 4c 69 73 >a = a;. pLis
20e20 74 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 t->nAlloc = sqli
20e21 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 te3DbMallocSize(
20e22 64 62 2c 20 61 29 2f 73 69 7a 65 6f 66 28 61 5b db, a)/sizeof(a[
20e23 30 5d 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 0]);. }. asser
20e24 74 28 20 70 4c 69 73 74 2d 3e 61 21 3d 30 20 29 t( pList->a!=0 )
20e25 3b 0a 20 20 69 66 28 20 31 20 29 7b 0a 20 20 20 ;. if( 1 ){.
20e26 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 struct ExprList
20e27 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 _item *pItem = &
20e28 70 4c 69 73 74 2d 3e 61 5b 70 4c 69 73 74 2d 3e pList->a[pList->
20e29 6e 45 78 70 72 2b 2b 5d 3b 0a 20 20 20 20 6d 65 nExpr++];. me
20e2a 6d 73 65 74 28 70 49 74 65 6d 2c 20 30 2c 20 73 mset(pItem, 0, s
20e2b 69 7a 65 6f 66 28 2a 70 49 74 65 6d 29 29 3b 0a izeof(*pItem));.
20e2c 20 20 20 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 pItem->pExpr
20e2d 20 3d 20 70 45 78 70 72 3b 0a 20 20 7d 0a 20 20 = pExpr;. }.
20e2e 72 65 74 75 72 6e 20 70 4c 69 73 74 3b 0a 0a 6e return pList;..n
20e2f 6f 5f 6d 65 6d 3a 20 20 20 20 20 0a 20 20 2f 2a o_mem: . /*
20e30 20 41 76 6f 69 64 20 6c 65 61 6b 69 6e 67 20 6d Avoid leaking m
20e31 65 6d 6f 72 79 20 69 66 20 6d 61 6c 6c 6f 63 20 emory if malloc
20e32 68 61 73 20 66 61 69 6c 65 64 2e 20 2a 2f 0a 20 has failed. */.
20e33 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 sqlite3ExprDele
20e34 74 65 28 64 62 2c 20 70 45 78 70 72 29 3b 0a 20 te(db, pExpr);.
20e35 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 sqlite3ExprList
20e36 44 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 73 74 Delete(db, pList
20e37 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d );. return 0;.}
20e38 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 ../*.** Set the
20e39 45 78 70 72 4c 69 73 74 2e 61 5b 5d 2e 7a 4e 61 ExprList.a[].zNa
20e3a 6d 65 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 me element of th
20e3b 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 e most recently
20e3c 61 64 64 65 64 20 69 74 65 6d 0a 2a 2a 20 6f 6e added item.** on
20e3d 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
20e3e 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 70 4c 69 73 list..**.** pLis
20e3f 74 20 6d 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 t might be NULL
20e40 66 6f 6c 6c 6f 77 69 6e 67 20 61 6e 20 4f 4f 4d following an OOM
20e41 20 65 72 72 6f 72 2e 20 20 42 75 74 20 70 4e 61 error. But pNa
20e42 6d 65 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 me should never
20e43 62 65 0a 2a 2a 20 4e 55 4c 4c 2e 20 20 49 66 20 be.** NULL. If
20e44 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 a memory allocat
20e45 69 6f 6e 20 66 61 69 6c 73 2c 20 74 68 65 20 70 ion fails, the p
20e46 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f Parse->db->mallo
20e47 63 46 61 69 6c 65 64 20 66 6c 61 67 0a 2a 2a 20 cFailed flag.**
20e48 69 73 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 is set..*/.SQLIT
20e49 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
20e4a 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 53 65 qlite3ExprListSe
20e4b 74 4e 61 6d 65 28 0a 20 20 50 61 72 73 65 20 2a tName(. Parse *
20e4c 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 pParse,
20e4d 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 /* Parsing cont
20e4e 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 ext */. ExprLis
20e4f 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 20 20 t *pList,
20e50 20 2f 2a 20 4c 69 73 74 20 74 6f 20 77 68 69 63 /* List to whic
20e51 68 20 74 6f 20 61 64 64 20 74 68 65 20 73 70 61 h to add the spa
20e52 6e 2e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 n. */. Token *p
20e53 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 Name,
20e54 2f 2a 20 4e 61 6d 65 20 74 6f 20 62 65 20 61 64 /* Name to be ad
20e55 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 64 65 71 ded */. int deq
20e56 75 6f 74 65 20 20 20 20 20 20 20 20 20 20 20 20 uote
20e57 20 2f 2a 20 54 72 75 65 20 74 6f 20 63 61 75 73 /* True to caus
20e58 65 20 74 68 65 20 6e 61 6d 65 20 74 6f 20 62 65 e the name to be
20e59 20 64 65 71 75 6f 74 65 64 20 2a 2f 0a 29 7b 0a dequoted */.){.
20e5a 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 21 assert( pList!
20e5b 3d 30 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62 =0 || pParse->db
20e5c 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 21 3d ->mallocFailed!=
20e5d 30 20 29 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 0 );. if( pList
20e5e 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 45 ){. struct E
20e5f 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 xprList_item *pI
20e60 74 65 6d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 tem;. assert(
20e61 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 30 20 pList->nExpr>0
20e62 29 3b 0a 20 20 20 20 70 49 74 65 6d 20 3d 20 26 );. pItem = &
20e63 70 4c 69 73 74 2d 3e 61 5b 70 4c 69 73 74 2d 3e pList->a[pList->
20e64 6e 45 78 70 72 2d 31 5d 3b 0a 20 20 20 20 61 73 nExpr-1];. as
20e65 73 65 72 74 28 20 70 49 74 65 6d 2d 3e 7a 4e 61 sert( pItem->zNa
20e66 6d 65 3d 3d 30 20 29 3b 0a 20 20 20 20 70 49 74 me==0 );. pIt
20e67 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 em->zName = sqli
20e68 74 65 33 44 62 53 74 72 4e 44 75 70 28 70 50 61 te3DbStrNDup(pPa
20e69 72 73 65 2d 3e 64 62 2c 20 70 4e 61 6d 65 2d 3e rse->db, pName->
20e6a 7a 2c 20 70 4e 61 6d 65 2d 3e 6e 29 3b 0a 20 20 z, pName->n);.
20e6b 20 20 69 66 28 20 64 65 71 75 6f 74 65 20 26 26 if( dequote &&
20e6c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 29 20 pItem->zName )
20e6d 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28 70 sqlite3Dequote(p
20e6e 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 Item->zName);.
20e6f 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 }.}../*.** Set t
20e70 68 65 20 45 78 70 72 4c 69 73 74 2e 61 5b 5d 2e he ExprList.a[].
20e71 7a 53 70 61 6e 20 65 6c 65 6d 65 6e 74 20 6f 66 zSpan element of
20e72 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 the most recent
20e73 6c 79 20 61 64 64 65 64 20 69 74 65 6d 0a 2a 2a ly added item.**
20e74 20 6f 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 on the expressi
20e75 6f 6e 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 70 on list..**.** p
20e76 4c 69 73 74 20 6d 69 67 68 74 20 62 65 20 4e 55 List might be NU
20e77 4c 4c 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 6e 20 LL following an
20e78 4f 4f 4d 20 65 72 72 6f 72 2e 20 20 42 75 74 20 OOM error. But
20e79 70 53 70 61 6e 20 73 68 6f 75 6c 64 20 6e 65 76 pSpan should nev
20e7a 65 72 20 62 65 0a 2a 2a 20 4e 55 4c 4c 2e 20 20 er be.** NULL.
20e7b 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f If a memory allo
20e7c 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 74 68 cation fails, th
20e7d 65 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 e pParse->db->ma
20e7e 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 0a llocFailed flag.
20e7f 2a 2a 20 69 73 20 73 65 74 2e 0a 2a 2f 0a 53 51 ** is set..*/.SQ
20e80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
20e81 64 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 d sqlite3ExprLis
20e82 74 53 65 74 53 70 61 6e 28 0a 20 20 50 61 72 73 tSetSpan(. Pars
20e83 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 e *pParse,
20e84 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 /* Parsing c
20e85 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 ontext */. Expr
20e86 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 List *pList,
20e87 20 20 20 20 2f 2a 20 4c 69 73 74 20 74 6f 20 77 /* List to w
20e88 68 69 63 68 20 74 6f 20 61 64 64 20 74 68 65 20 hich to add the
20e89 73 70 61 6e 2e 20 2a 2f 0a 20 20 45 78 70 72 53 span. */. ExprS
20e8a 70 61 6e 20 2a 70 53 70 61 6e 20 20 20 20 20 20 pan *pSpan
20e8b 20 20 20 2f 2a 20 54 68 65 20 73 70 61 6e 20 74 /* The span t
20e8c 6f 20 62 65 20 61 64 64 65 64 20 2a 2f 0a 29 7b o be added */.){
20e8d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
20e8e 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 pParse->db;. a
20e8f 73 73 65 72 74 28 20 70 4c 69 73 74 21 3d 30 20 ssert( pList!=0
20e90 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 || db->mallocFai
20e91 6c 65 64 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 led!=0 );. if(
20e92 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 73 74 72 pList ){. str
20e93 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 uct ExprList_ite
20e94 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 4c 69 73 m *pItem = &pLis
20e95 74 2d 3e 61 5b 70 4c 69 73 74 2d 3e 6e 45 78 70 t->a[pList->nExp
20e96 72 2d 31 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 r-1];. assert
20e97 28 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 30 ( pList->nExpr>0
20e98 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
20e99 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
20e9a 20 7c 7c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 || pItem->pExpr
20e9b 3d 3d 70 53 70 61 6e 2d 3e 70 45 78 70 72 20 29 ==pSpan->pExpr )
20e9c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 ;. sqlite3DbF
20e9d 72 65 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a ree(db, pItem->z
20e9e 53 70 61 6e 29 3b 0a 20 20 20 20 70 49 74 65 6d Span);. pItem
20e9f 2d 3e 7a 53 70 61 6e 20 3d 20 73 71 6c 69 74 65 ->zSpan = sqlite
20ea0 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28 3DbStrNDup(db, (
20ea1 63 68 61 72 2a 29 70 53 70 61 6e 2d 3e 7a 53 74 char*)pSpan->zSt
20ea2 61 72 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 art,.
20ea3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20ea4 20 20 20 20 20 20 20 20 20 28 69 6e 74 29 28 70 (int)(p
20ea5 53 70 61 6e 2d 3e 7a 45 6e 64 20 2d 20 70 53 70 Span->zEnd - pSp
20ea6 61 6e 2d 3e 7a 53 74 61 72 74 29 29 3b 0a 20 20 an->zStart));.
20ea7 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 }.}../*.** If th
20ea8 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 e expression lis
20ea9 74 20 70 45 4c 69 73 74 20 63 6f 6e 74 61 69 6e t pEList contain
20eaa 73 20 6d 6f 72 65 20 74 68 61 6e 20 69 4c 69 6d s more than iLim
20eab 69 74 20 65 6c 65 6d 65 6e 74 73 2c 0a 2a 2a 20 it elements,.**
20eac 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d leave an error m
20ead 65 73 73 61 67 65 20 69 6e 20 70 50 61 72 73 65 essage in pParse
20eae 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
20eaf 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
20eb0 45 78 70 72 4c 69 73 74 43 68 65 63 6b 4c 65 6e ExprListCheckLen
20eb1 67 74 68 28 0a 20 20 50 61 72 73 65 20 2a 70 50 gth(. Parse *pP
20eb2 61 72 73 65 2c 0a 20 20 45 78 70 72 4c 69 73 74 arse,. ExprList
20eb3 20 2a 70 45 4c 69 73 74 2c 0a 20 20 63 6f 6e 73 *pEList,. cons
20eb4 74 20 63 68 61 72 20 2a 7a 4f 62 6a 65 63 74 0a t char *zObject.
20eb5 29 7b 0a 20 20 69 6e 74 20 6d 78 20 3d 20 70 50 ){. int mx = pP
20eb6 61 72 73 65 2d 3e 64 62 2d 3e 61 4c 69 6d 69 74 arse->db->aLimit
20eb7 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f [SQLITE_LIMIT_CO
20eb8 4c 55 4d 4e 5d 3b 0a 20 20 74 65 73 74 63 61 73 LUMN];. testcas
20eb9 65 28 20 70 45 4c 69 73 74 20 26 26 20 70 45 4c e( pEList && pEL
20eba 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 6d 78 20 29 ist->nExpr==mx )
20ebb 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 45 ;. testcase( pE
20ebc 4c 69 73 74 20 26 26 20 70 45 4c 69 73 74 2d 3e List && pEList->
20ebd 6e 45 78 70 72 3d 3d 6d 78 2b 31 20 29 3b 0a 20 nExpr==mx+1 );.
20ebe 20 69 66 28 20 70 45 4c 69 73 74 20 26 26 20 70 if( pEList && p
20ebf 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 6d 78 20 EList->nExpr>mx
20ec0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 ){. sqlite3Er
20ec1 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 rorMsg(pParse, "
20ec2 74 6f 6f 20 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73 too many columns
20ec3 20 69 6e 20 25 73 22 2c 20 7a 4f 62 6a 65 63 74 in %s", zObject
20ec4 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
20ec5 44 65 6c 65 74 65 20 61 6e 20 65 6e 74 69 72 65 Delete an entire
20ec6 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 expression list
20ec7 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
20ec8 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
20ec9 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 73 ExprListDelete(s
20eca 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 qlite3 *db, Expr
20ecb 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 List *pList){.
20ecc 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 int i;. struct
20ecd 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 ExprList_item *p
20ece 49 74 65 6d 3b 0a 20 20 69 66 28 20 70 4c 69 73 Item;. if( pLis
20ecf 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 t==0 ) return;.
20ed0 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e assert( pList->
20ed1 61 21 3d 30 20 7c 7c 20 28 70 4c 69 73 74 2d 3e a!=0 || (pList->
20ed2 6e 45 78 70 72 3d 3d 30 20 26 26 20 70 4c 69 73 nExpr==0 && pLis
20ed3 74 2d 3e 6e 41 6c 6c 6f 63 3d 3d 30 29 20 29 3b t->nAlloc==0) );
20ed4 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 . assert( pList
20ed5 2d 3e 6e 45 78 70 72 3c 3d 70 4c 69 73 74 2d 3e ->nExpr<=pList->
20ed6 6e 41 6c 6c 6f 63 20 29 3b 0a 20 20 66 6f 72 28 nAlloc );. for(
20ed7 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20 pItem=pList->a,
20ed8 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 i=0; i<pList->nE
20ed9 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b xpr; i++, pItem+
20eda 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 +){. sqlite3E
20edb 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 49 xprDelete(db, pI
20edc 74 65 6d 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20 tem->pExpr);.
20edd 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
20ede 62 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 b, pItem->zName)
20edf 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 ;. sqlite3DbF
20ee0 72 65 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a ree(db, pItem->z
20ee1 53 70 61 6e 29 3b 0a 20 20 7d 0a 20 20 73 71 6c Span);. }. sql
20ee2 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
20ee3 4c 69 73 74 2d 3e 61 29 3b 0a 20 20 73 71 6c 69 List->a);. sqli
20ee4 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c te3DbFree(db, pL
20ee5 69 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 ist);.}../*.** T
20ee6 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 hese routines ar
20ee7 65 20 57 61 6c 6b 65 72 20 63 61 6c 6c 62 61 63 e Walker callbac
20ee8 6b 73 2e 20 20 57 61 6c 6b 65 72 2e 75 2e 70 69 ks. Walker.u.pi
20ee9 20 69 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a is a pointer.**
20eea 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 to an integer.
20eeb 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 These routines
20eec 61 72 65 20 63 68 65 63 6b 69 6e 67 20 61 6e 20 are checking an
20eed 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 73 65 expression to se
20eee 65 0a 2a 2a 20 69 66 20 69 74 20 69 73 20 61 20 e.** if it is a
20eef 63 6f 6e 73 74 61 6e 74 2e 20 20 53 65 74 20 2a constant. Set *
20ef0 57 61 6c 6b 65 72 2e 75 2e 70 69 20 74 6f 20 30 Walker.u.pi to 0
20ef1 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69 if the expressi
20ef2 6f 6e 20 69 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e on is.** not con
20ef3 73 74 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 stant..**.** The
20ef4 73 65 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 se callback rout
20ef5 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 74 6f ines are used to
20ef6 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 66 implement the f
20ef7 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 ollowing:.**.**
20ef8 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 sqlite3ExprI
20ef9 73 43 6f 6e 73 74 61 6e 74 28 29 0a 2a 2a 20 20 sConstant().**
20efa 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 sqlite3ExprIs
20efb 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28 ConstantNotJoin(
20efc 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 ).** sqlite3
20efd 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4f 72 ExprIsConstantOr
20efe 46 75 6e 63 74 69 6f 6e 28 29 0a 2a 2a 0a 2a 2f Function().**.*/
20eff 0a 73 74 61 74 69 63 20 69 6e 74 20 65 78 70 72 .static int expr
20f00 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 28 57 NodeIsConstant(W
20f01 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 alker *pWalker,
20f02 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 0a 20 Expr *pExpr){..
20f03 20 2f 2a 20 49 66 20 70 57 61 6c 6b 65 72 2d 3e /* If pWalker->
20f04 75 2e 69 20 69 73 20 33 20 74 68 65 6e 20 61 6e u.i is 3 then an
20f05 79 20 74 65 72 6d 20 6f 66 20 74 68 65 20 65 78 y term of the ex
20f06 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 63 6f pression that co
20f07 6d 65 73 20 66 72 6f 6d 0a 20 20 2a 2a 20 74 68 mes from. ** th
20f08 65 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c e ON or USING cl
20f09 61 75 73 65 73 20 6f 66 20 61 20 6a 6f 69 6e 20 auses of a join
20f0a 64 69 73 71 75 61 6c 69 66 69 65 73 20 74 68 65 disqualifies the
20f0b 20 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2a expression. **
20f0c 20 66 72 6f 6d 20 62 65 69 6e 67 20 63 6f 6e 73 from being cons
20f0d 69 64 65 72 65 64 20 63 6f 6e 73 74 61 6e 74 2e idered constant.
20f0e 20 2a 2f 0a 20 20 69 66 28 20 70 57 61 6c 6b 65 */. if( pWalke
20f0f 72 2d 3e 75 2e 69 3d 3d 33 20 26 26 20 45 78 70 r->u.i==3 && Exp
20f10 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 rHasAnyProperty(
20f11 70 45 78 70 72 2c 20 45 50 5f 46 72 6f 6d 4a 6f pExpr, EP_FromJo
20f12 69 6e 29 20 29 7b 0a 20 20 20 20 70 57 61 6c 6b in) ){. pWalk
20f13 65 72 2d 3e 75 2e 69 20 3d 20 30 3b 0a 20 20 20 er->u.i = 0;.
20f14 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 return WRC_Abor
20f15 74 3b 0a 20 20 7d 0a 0a 20 20 73 77 69 74 63 68 t;. }.. switch
20f16 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 ( pExpr->op ){.
20f17 20 20 20 2f 2a 20 43 6f 6e 73 69 64 65 72 20 66 /* Consider f
20f18 75 6e 63 74 69 6f 6e 73 20 74 6f 20 62 65 20 63 unctions to be c
20f19 6f 6e 73 74 61 6e 74 20 69 66 20 61 6c 6c 20 74 onstant if all t
20f1a 68 65 69 72 20 61 72 67 75 6d 65 6e 74 73 20 61 heir arguments a
20f1b 72 65 20 63 6f 6e 73 74 61 6e 74 0a 20 20 20 20 re constant.
20f1c 2a 2a 20 61 6e 64 20 70 57 61 6c 6b 65 72 2d 3e ** and pWalker->
20f1d 75 2e 69 3d 3d 32 20 2a 2f 0a 20 20 20 20 63 61 u.i==2 */. ca
20f1e 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a 0a se TK_FUNCTION:.
20f1f 20 20 20 20 20 20 69 66 28 20 70 57 61 6c 6b 65 if( pWalke
20f20 72 2d 3e 75 2e 69 3d 3d 32 20 29 20 72 65 74 75 r->u.i==2 ) retu
20f21 72 6e 20 30 3b 0a 20 20 20 20 20 20 2f 2a 20 46 rn 0;. /* F
20f22 61 6c 6c 20 74 68 72 6f 75 67 68 20 2a 2f 0a 20 all through */.
20f23 20 20 20 63 61 73 65 20 54 4b 5f 49 44 3a 0a 20 case TK_ID:.
20f24 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d case TK_COLUM
20f25 4e 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 N:. case TK_A
20f26 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 0a 20 20 20 GG_FUNCTION:.
20f27 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 43 4f 4c case TK_AGG_COL
20f28 55 4d 4e 3a 0a 20 20 20 20 20 20 74 65 73 74 63 UMN:. testc
20f29 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d ase( pExpr->op==
20f2a 54 4b 5f 49 44 20 29 3b 0a 20 20 20 20 20 20 74 TK_ID );. t
20f2b 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e estcase( pExpr->
20f2c 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b op==TK_COLUMN );
20f2d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
20f2e 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 41 pExpr->op==TK_A
20f2f 47 47 5f 46 55 4e 43 54 49 4f 4e 20 29 3b 0a 20 GG_FUNCTION );.
20f30 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 testcase( p
20f31 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 41 47 47 Expr->op==TK_AGG
20f32 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 _COLUMN );.
20f33 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20 3d 20 pWalker->u.i =
20f34 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 0;. return
20f35 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 64 WRC_Abort;. d
20f36 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 74 65 efault:. te
20f37 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f stcase( pExpr->o
20f38 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 29 3b 20 p==TK_SELECT );
20f39 2f 2a 20 73 65 6c 65 63 74 4e 6f 64 65 49 73 43 /* selectNodeIsC
20f3a 6f 6e 73 74 61 6e 74 20 77 69 6c 6c 20 64 69 73 onstant will dis
20f3b 61 6c 6c 6f 77 20 2a 2f 0a 20 20 20 20 20 20 74 allow */. t
20f3c 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e estcase( pExpr->
20f3d 6f 70 3d 3d 54 4b 5f 45 58 49 53 54 53 20 29 3b op==TK_EXISTS );
20f3e 20 2f 2a 20 73 65 6c 65 63 74 4e 6f 64 65 49 73 /* selectNodeIs
20f3f 43 6f 6e 73 74 61 6e 74 20 77 69 6c 6c 20 64 69 Constant will di
20f40 73 61 6c 6c 6f 77 20 2a 2f 0a 20 20 20 20 20 20 sallow */.
20f41 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 return WRC_Conti
20f42 6e 75 65 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 nue;. }.}.stati
20f43 63 20 69 6e 74 20 73 65 6c 65 63 74 4e 6f 64 65 c int selectNode
20f44 49 73 43 6f 6e 73 74 61 6e 74 28 57 61 6c 6b 65 IsConstant(Walke
20f45 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 r *pWalker, Sele
20f46 63 74 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 ct *NotUsed){.
20f47 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
20f48 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 70 57 61 (NotUsed);. pWa
20f49 6c 6b 65 72 2d 3e 75 2e 69 20 3d 20 30 3b 0a 20 lker->u.i = 0;.
20f4a 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 return WRC_Abor
20f4b 74 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 t;.}.static int
20f4c 65 78 70 72 49 73 43 6f 6e 73 74 28 45 78 70 72 exprIsConst(Expr
20f4d 20 2a 70 2c 20 69 6e 74 20 69 6e 69 74 46 6c 61 *p, int initFla
20f4e 67 29 7b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a g){. Walker w;.
20f4f 20 20 77 2e 75 2e 69 20 3d 20 69 6e 69 74 46 6c w.u.i = initFl
20f50 61 67 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c ag;. w.xExprCal
20f51 6c 62 61 63 6b 20 3d 20 65 78 70 72 4e 6f 64 65 lback = exprNode
20f52 49 73 43 6f 6e 73 74 61 6e 74 3b 0a 20 20 77 2e IsConstant;. w.
20f53 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 xSelectCallback
20f54 3d 20 73 65 6c 65 63 74 4e 6f 64 65 49 73 43 6f = selectNodeIsCo
20f55 6e 73 74 61 6e 74 3b 0a 20 20 73 71 6c 69 74 65 nstant;. sqlite
20f56 33 57 61 6c 6b 45 78 70 72 28 26 77 2c 20 70 29 3WalkExpr(&w, p)
20f57 3b 0a 20 20 72 65 74 75 72 6e 20 77 2e 75 2e 69 ;. return w.u.i
20f58 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 ;.}../*.** Walk
20f59 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 an expression tr
20f5a 65 65 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66 ee. Return 1 if
20f5b 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
20f5c 69 73 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 61 is constant.** a
20f5d 6e 64 20 30 20 69 66 20 69 74 20 69 6e 76 6f 6c nd 0 if it invol
20f5e 76 65 73 20 76 61 72 69 61 62 6c 65 73 20 6f 72 ves variables or
20f5f 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 2e function calls.
20f60 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70 .**.** For the p
20f61 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69 73 20 urposes of this
20f62 66 75 6e 63 74 69 6f 6e 2c 20 61 20 64 6f 75 62 function, a doub
20f63 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 69 6e 67 le-quoted string
20f64 20 28 65 78 3a 20 22 61 62 63 22 29 0a 2a 2a 20 (ex: "abc").**
20f65 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 61 20 is considered a
20f66 76 61 72 69 61 62 6c 65 20 62 75 74 20 61 20 73 variable but a s
20f67 69 6e 67 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 ingle-quoted str
20f68 69 6e 67 20 28 65 78 3a 20 27 61 62 63 27 29 20 ing (ex: 'abc')
20f69 69 73 0a 2a 2a 20 61 20 63 6f 6e 73 74 61 6e 74 is.** a constant
20f6a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
20f6b 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 ATE int sqlite3E
20f6c 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 45 78 xprIsConstant(Ex
20f6d 70 72 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e pr *p){. return
20f6e 20 65 78 70 72 49 73 43 6f 6e 73 74 28 70 2c 20 exprIsConst(p,
20f6f 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 1);.}../*.** Wal
20f70 6b 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 k an expression
20f71 74 72 65 65 2e 20 20 52 65 74 75 72 6e 20 31 20 tree. Return 1
20f72 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f if the expressio
20f73 6e 20 69 73 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a n is constant.**
20f74 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 20 6f 72 that does no or
20f75 69 67 69 6e 61 74 65 20 66 72 6f 6d 20 74 68 65 iginate from the
20f76 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 ON or USING cla
20f77 75 73 65 73 20 6f 66 20 61 20 6a 6f 69 6e 2e 0a uses of a join..
20f78 2a 2a 20 52 65 74 75 72 6e 20 30 20 69 66 20 69 ** Return 0 if i
20f79 74 20 69 6e 76 6f 6c 76 65 73 20 76 61 72 69 61 t involves varia
20f7a 62 6c 65 73 20 6f 72 20 66 75 6e 63 74 69 6f 6e bles or function
20f7b 20 63 61 6c 6c 73 20 6f 72 20 74 65 72 6d 73 20 calls or terms
20f7c 66 72 6f 6d 0a 2a 2a 20 61 6e 20 4f 4e 20 6f 72 from.** an ON or
20f7d 20 55 53 49 4e 47 20 63 6c 61 75 73 65 2e 0a 2a USING clause..*
20f7e 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
20f7f 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 int sqlite3Expr
20f80 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f 69 IsConstantNotJoi
20f81 6e 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 72 65 n(Expr *p){. re
20f82 74 75 72 6e 20 65 78 70 72 49 73 43 6f 6e 73 74 turn exprIsConst
20f83 28 70 2c 20 33 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a (p, 3);.}../*.**
20f84 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 73 Walk an express
20f85 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 75 72 ion tree. Retur
20f86 6e 20 31 20 69 66 20 74 68 65 20 65 78 70 72 65 n 1 if the expre
20f87 73 73 69 6f 6e 20 69 73 20 63 6f 6e 73 74 61 6e ssion is constan
20f88 74 0a 2a 2a 20 6f 72 20 61 20 66 75 6e 63 74 69 t.** or a functi
20f89 6f 6e 20 63 61 6c 6c 20 77 69 74 68 20 63 6f 6e on call with con
20f8a 73 74 61 6e 74 20 61 72 67 75 6d 65 6e 74 73 2e stant arguments.
20f8b 20 20 52 65 74 75 72 6e 20 61 6e 64 20 30 20 69 Return and 0 i
20f8c 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 61 f there.** are a
20f8d 6e 79 20 76 61 72 69 61 62 6c 65 73 2e 0a 2a 2a ny variables..**
20f8e 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70 75 72 70 .** For the purp
20f8f 6f 73 65 73 20 6f 66 20 74 68 69 73 20 66 75 6e oses of this fun
20f90 63 74 69 6f 6e 2c 20 61 20 64 6f 75 62 6c 65 2d ction, a double-
20f91 71 75 6f 74 65 64 20 73 74 72 69 6e 67 20 28 65 quoted string (e
20f92 78 3a 20 22 61 62 63 22 29 0a 2a 2a 20 69 73 20 x: "abc").** is
20f93 63 6f 6e 73 69 64 65 72 65 64 20 61 20 76 61 72 considered a var
20f94 69 61 62 6c 65 20 62 75 74 20 61 20 73 69 6e 67 iable but a sing
20f95 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 69 6e 67 le-quoted string
20f96 20 28 65 78 3a 20 27 61 62 63 27 29 20 69 73 0a (ex: 'abc') is.
20f97 2a 2a 20 61 20 63 6f 6e 73 74 61 6e 74 2e 0a 2a ** a constant..*
20f98 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
20f99 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 int sqlite3Expr
20f9a 49 73 43 6f 6e 73 74 61 6e 74 4f 72 46 75 6e 63 IsConstantOrFunc
20f9b 74 69 6f 6e 28 45 78 70 72 20 2a 70 29 7b 0a 20 tion(Expr *p){.
20f9c 20 72 65 74 75 72 6e 20 65 78 70 72 49 73 43 6f return exprIsCo
20f9d 6e 73 74 28 70 2c 20 32 29 3b 0a 7d 0a 0a 2f 2a nst(p, 2);.}../*
20f9e 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 .** If the expre
20f9f 73 73 69 6f 6e 20 70 20 63 6f 64 65 73 20 61 20 ssion p codes a
20fa0 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65 67 65 72 constant integer
20fa1 20 74 68 61 74 20 69 73 20 73 6d 61 6c 6c 20 65 that is small e
20fa2 6e 6f 75 67 68 0a 2a 2a 20 74 6f 20 66 69 74 20 nough.** to fit
20fa3 69 6e 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 in a 32-bit inte
20fa4 67 65 72 2c 20 72 65 74 75 72 6e 20 31 20 61 6e ger, return 1 an
20fa5 64 20 70 75 74 20 74 68 65 20 76 61 6c 75 65 20 d put the value
20fa6 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a of the integer.*
20fa7 2a 20 69 6e 20 2a 70 56 61 6c 75 65 2e 20 20 49 * in *pValue. I
20fa8 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e f the expression
20fa9 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 67 is not an integ
20faa 65 72 20 6f 72 20 69 66 20 69 74 20 69 73 20 74 er or if it is t
20fab 6f 6f 20 62 69 67 0a 2a 2a 20 74 6f 20 66 69 74 oo big.** to fit
20fac 20 69 6e 20 61 20 73 69 67 6e 65 64 20 33 32 2d in a signed 32-
20fad 62 69 74 20 69 6e 74 65 67 65 72 2c 20 72 65 74 bit integer, ret
20fae 75 72 6e 20 30 20 61 6e 64 20 6c 65 61 76 65 20 urn 0 and leave
20faf 2a 70 56 61 6c 75 65 20 75 6e 63 68 61 6e 67 65 *pValue unchange
20fb0 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
20fb1 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
20fb2 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 45 78 ExprIsInteger(Ex
20fb3 70 72 20 2a 70 2c 20 69 6e 74 20 2a 70 56 61 6c pr *p, int *pVal
20fb4 75 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 ue){. int rc =
20fb5 30 3b 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 0;. if( p->flag
20fb6 73 20 26 20 45 50 5f 49 6e 74 56 61 6c 75 65 20 s & EP_IntValue
20fb7 29 7b 0a 20 20 20 20 2a 70 56 61 6c 75 65 20 3d ){. *pValue =
20fb8 20 70 2d 3e 75 2e 69 56 61 6c 75 65 3b 0a 20 20 p->u.iValue;.
20fb9 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a return 1;. }.
20fba 20 20 73 77 69 74 63 68 28 20 70 2d 3e 6f 70 20 switch( p->op
20fbb 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 ){. case TK_I
20fbc 4e 54 45 47 45 52 3a 20 7b 0a 20 20 20 20 20 20 NTEGER: {.
20fbd 72 63 20 3d 20 73 71 6c 69 74 65 33 47 65 74 49 rc = sqlite3GetI
20fbe 6e 74 33 32 28 70 2d 3e 75 2e 7a 54 6f 6b 65 6e nt32(p->u.zToken
20fbf 2c 20 70 56 61 6c 75 65 29 3b 0a 20 20 20 20 20 , pValue);.
20fc0 20 61 73 73 65 72 74 28 20 72 63 3d 3d 30 20 29 assert( rc==0 )
20fc1 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
20fc2 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b }. case TK
20fc3 5f 55 50 4c 55 53 3a 20 7b 0a 20 20 20 20 20 20 _UPLUS: {.
20fc4 72 63 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 rc = sqlite3Expr
20fc5 49 73 49 6e 74 65 67 65 72 28 70 2d 3e 70 4c 65 IsInteger(p->pLe
20fc6 66 74 2c 20 70 56 61 6c 75 65 29 3b 0a 20 20 20 ft, pValue);.
20fc7 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
20fc8 20 20 20 20 63 61 73 65 20 54 4b 5f 55 4d 49 4e case TK_UMIN
20fc9 55 53 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 US: {. int
20fca 76 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c v;. if( sql
20fcb 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 ite3ExprIsIntege
20fcc 72 28 70 2d 3e 70 4c 65 66 74 2c 20 26 76 29 20 r(p->pLeft, &v)
20fcd 29 7b 0a 20 20 20 20 20 20 20 20 2a 70 56 61 6c ){. *pVal
20fce 75 65 20 3d 20 2d 76 3b 0a 20 20 20 20 20 20 20 ue = -v;.
20fcf 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d rc = 1;. }
20fd0 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
20fd1 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a }. default:
20fd2 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 break;. }. if
20fd3 28 20 72 63 20 29 7b 0a 20 20 20 20 61 73 73 65 ( rc ){. asse
20fd4 72 74 28 20 45 78 70 72 48 61 73 41 6e 79 50 72 rt( ExprHasAnyPr
20fd5 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 52 65 64 operty(p, EP_Red
20fd6 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c uced|EP_TokenOnl
20fd7 79 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 y).
20fd8 20 20 7c 7c 20 28 70 2d 3e 66 6c 61 67 73 32 20 || (p->flags2
20fd9 26 20 45 50 32 5f 4d 61 6c 6c 6f 63 65 64 54 6f & EP2_MallocedTo
20fda 6b 65 6e 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70 ken)==0 );. p
20fdb 2d 3e 6f 70 20 3d 20 54 4b 5f 49 4e 54 45 47 45 ->op = TK_INTEGE
20fdc 52 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 R;. p->flags
20fdd 7c 3d 20 45 50 5f 49 6e 74 56 61 6c 75 65 3b 0a |= EP_IntValue;.
20fde 20 20 20 20 70 2d 3e 75 2e 69 56 61 6c 75 65 20 p->u.iValue
20fdf 3d 20 2a 70 56 61 6c 75 65 3b 0a 20 20 7d 0a 20 = *pValue;. }.
20fe0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
20fe1 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 *.** Return TRUE
20fe2 20 69 66 20 74 68 65 20 67 69 76 65 6e 20 73 74 if the given st
20fe3 72 69 6e 67 20 69 73 20 61 20 72 6f 77 2d 69 64 ring is a row-id
20fe4 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 2e 0a 2a 2f column name..*/
20fe5 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
20fe6 69 6e 74 20 73 71 6c 69 74 65 33 49 73 52 6f 77 int sqlite3IsRow
20fe7 69 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a id(const char *z
20fe8 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 ){. if( sqlite3
20fe9 53 74 72 49 43 6d 70 28 7a 2c 20 22 5f 52 4f 57 StrICmp(z, "_ROW
20fea 49 44 5f 22 29 3d 3d 30 20 29 20 72 65 74 75 72 ID_")==0 ) retur
20feb 6e 20 31 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 n 1;. if( sqlit
20fec 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 52 4f e3StrICmp(z, "RO
20fed 57 49 44 22 29 3d 3d 30 20 29 20 72 65 74 75 72 WID")==0 ) retur
20fee 6e 20 31 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 n 1;. if( sqlit
20fef 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 4f 49 e3StrICmp(z, "OI
20ff0 44 22 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 D")==0 ) return
20ff1 31 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 1;. return 0;.}
20ff2 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
20ff3 72 75 65 20 69 66 20 77 65 20 61 72 65 20 61 62 rue if we are ab
20ff4 6c 65 20 74 6f 20 74 68 65 20 49 4e 20 6f 70 65 le to the IN ope
20ff5 72 61 74 6f 72 20 6f 70 74 69 6d 69 7a 61 74 69 rator optimizati
20ff6 6f 6e 20 6f 6e 20 61 0a 2a 2a 20 71 75 65 72 79 on on a.** query
20ff7 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a of the form.**.
20ff8 2a 2a 20 20 20 20 20 20 20 78 20 49 4e 20 28 53 ** x IN (S
20ff9 45 4c 45 43 54 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a ELECT ...).**.**
20ffa 20 57 68 65 72 65 20 74 68 65 20 53 45 4c 45 43 Where the SELEC
20ffb 54 2e 2e 2e 20 63 6c 61 75 73 65 20 69 73 20 61 T... clause is a
20ffc 73 20 73 70 65 63 69 66 69 65 64 20 62 79 20 74 s specified by t
20ffd 68 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 he parameter to
20ffe 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 2e this.** routine.
20fff 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 65 6c 65 63 .**.** The Selec
21000 74 20 6f 62 6a 65 63 74 20 70 61 73 73 65 64 20 t object passed
21001 69 6e 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 in has already b
21002 65 65 6e 20 70 72 65 70 72 6f 63 65 73 73 65 64 een preprocessed
21003 20 61 6e 64 20 6e 6f 0a 2a 2a 20 65 72 72 6f 72 and no.** error
21004 73 20 68 61 76 65 20 62 65 65 6e 20 66 6f 75 6e s have been foun
21005 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 d..*/.#ifndef SQ
21006 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 LITE_OMIT_SUBQUE
21007 52 59 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73 RY.static int is
21008 43 61 6e 64 69 64 61 74 65 46 6f 72 49 6e 4f 70 CandidateForInOp
21009 74 28 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 t(Select *p){.
2100a 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 0a 20 SrcList *pSrc;.
2100b 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 ExprList *pELis
2100c 74 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 t;. Table *pTab
2100d 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 ;. if( p==0 ) r
2100e 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 eturn 0;
2100f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 72 69 /* ri
21010 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 ght-hand side of
21011 20 49 4e 20 69 73 20 53 45 4c 45 43 54 20 2a 2f IN is SELECT */
21012 0a 20 20 69 66 28 20 70 2d 3e 70 50 72 69 6f 72 . if( p->pPrior
21013 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 ) return 0;
21014 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 /* Not
21015 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 a compound SELE
21016 43 54 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 73 CT */. if( p->s
21017 65 6c 46 6c 61 67 73 20 26 20 28 53 46 5f 44 69 elFlags & (SF_Di
21018 73 74 69 6e 63 74 7c 53 46 5f 41 67 67 72 65 67 stinct|SF_Aggreg
21019 61 74 65 29 20 29 7b 0a 20 20 20 20 74 65 73 74 ate) ){. test
2101a 63 61 73 65 28 20 28 70 2d 3e 73 65 6c 46 6c 61 case( (p->selFla
2101b 67 73 20 26 20 28 53 46 5f 44 69 73 74 69 6e 63 gs & (SF_Distinc
2101c 74 7c 53 46 5f 41 67 67 72 65 67 61 74 65 29 29 t|SF_Aggregate))
2101d 3d 3d 53 46 5f 44 69 73 74 69 6e 63 74 20 29 3b ==SF_Distinct );
2101e 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 28 . testcase( (
2101f 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 28 53 p->selFlags & (S
21020 46 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f 41 67 F_Distinct|SF_Ag
21021 67 72 65 67 61 74 65 29 29 3d 3d 53 46 5f 41 67 gregate))==SF_Ag
21022 67 72 65 67 61 74 65 20 29 3b 0a 20 20 20 20 72 gregate );. r
21023 65 74 75 72 6e 20 30 3b 20 2f 2a 20 4e 6f 20 44 eturn 0; /* No D
21024 49 53 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 ISTINCT keyword
21025 61 6e 64 20 6e 6f 20 61 67 67 72 65 67 61 74 65 and no aggregate
21026 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 functions */.
21027 7d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 }. assert( p->p
21028 47 72 6f 75 70 42 79 3d 3d 30 20 29 3b 20 20 20 GroupBy==0 );
21029 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 /* Ha
2102a 73 20 6e 6f 20 47 52 4f 55 50 20 42 59 20 63 6c s no GROUP BY cl
2102b 61 75 73 65 20 2a 2f 0a 20 20 69 66 28 20 70 2d ause */. if( p-
2102c 3e 70 4c 69 6d 69 74 20 29 20 72 65 74 75 72 6e >pLimit ) return
2102d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
2102e 20 2f 2a 20 48 61 73 20 6e 6f 20 4c 49 4d 49 54 /* Has no LIMIT
2102f 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 61 73 73 clause */. ass
21030 65 72 74 28 20 70 2d 3e 70 4f 66 66 73 65 74 3d ert( p->pOffset=
21031 3d 30 20 29 3b 20 20 20 20 20 20 20 20 20 20 20 =0 );
21032 20 20 20 20 2f 2a 20 4e 6f 20 4c 49 4d 49 54 20 /* No LIMIT
21033 6d 65 61 6e 73 20 6e 6f 20 4f 46 46 53 45 54 20 means no OFFSET
21034 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 57 68 65 */. if( p->pWhe
21035 72 65 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 re ) return 0;
21036 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 /* H
21037 61 73 20 6e 6f 20 57 48 45 52 45 20 63 6c 61 75 as no WHERE clau
21038 73 65 20 2a 2f 0a 20 20 70 53 72 63 20 3d 20 70 se */. pSrc = p
21039 2d 3e 70 53 72 63 3b 0a 20 20 61 73 73 65 72 74 ->pSrc;. assert
2103a 28 20 70 53 72 63 21 3d 30 20 29 3b 0a 20 20 69 ( pSrc!=0 );. i
2103b 66 28 20 70 53 72 63 2d 3e 6e 53 72 63 21 3d 31 f( pSrc->nSrc!=1
2103c 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 ) return 0;
2103d 20 20 20 20 20 20 2f 2a 20 53 69 6e 67 6c 65 20 /* Single
2103e 74 65 72 6d 20 69 6e 20 46 52 4f 4d 20 63 6c 61 term in FROM cla
2103f 75 73 65 20 2a 2f 0a 20 20 69 66 28 20 70 53 72 use */. if( pSr
21040 63 2d 3e 61 5b 30 5d 2e 70 53 65 6c 65 63 74 20 c->a[0].pSelect
21041 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 ) return 0;
21042 2f 2a 20 46 52 4f 4d 20 69 73 20 6e 6f 74 20 61 /* FROM is not a
21043 20 73 75 62 71 75 65 72 79 20 6f 72 20 76 69 65 subquery or vie
21044 77 20 2a 2f 0a 20 20 70 54 61 62 20 3d 20 70 53 w */. pTab = pS
21045 72 63 2d 3e 61 5b 30 5d 2e 70 54 61 62 3b 0a 20 rc->a[0].pTab;.
21046 20 69 66 28 20 4e 45 56 45 52 28 70 54 61 62 3d if( NEVER(pTab=
21047 3d 30 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a =0) ) return 0;.
21048 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e assert( pTab->
21049 70 53 65 6c 65 63 74 3d 3d 30 20 29 3b 20 20 20 pSelect==0 );
2104a 20 20 20 20 20 20 20 20 20 2f 2a 20 46 52 4f 4d /* FROM
2104b 20 63 6c 61 75 73 65 20 69 73 20 6e 6f 74 20 61 clause is not a
2104c 20 76 69 65 77 20 2a 2f 0a 20 20 69 66 28 20 49 view */. if( I
2104d 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 sVirtual(pTab) )
2104e 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 return 0;
2104f 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 /* FROM clause
21050 20 6e 6f 74 20 61 20 76 69 72 74 75 61 6c 20 74 not a virtual t
21051 61 62 6c 65 20 2a 2f 0a 20 20 70 45 4c 69 73 74 able */. pEList
21052 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 = p->pEList;.
21053 69 66 28 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 if( pEList->nExp
21054 72 21 3d 31 20 29 20 72 65 74 75 72 6e 20 30 3b r!=1 ) return 0;
21055 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 63 6f /* One co
21056 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72 65 73 75 lumn in the resu
21057 6c 74 20 73 65 74 20 2a 2f 0a 20 20 69 66 28 20 lt set */. if(
21058 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 pEList->a[0].pEx
21059 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d pr->op!=TK_COLUM
2105a 4e 20 29 20 72 65 74 75 72 6e 20 30 3b 20 2f 2a N ) return 0; /*
2105b 20 52 65 73 75 6c 74 20 69 73 20 61 20 63 6f 6c Result is a col
2105c 75 6d 6e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 umn */. return
2105d 31 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 1;.}.#endif /* S
2105e 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 QLITE_OMIT_SUBQU
2105f 45 52 59 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 ERY */../*.** Th
21060 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 is function is u
21061 73 65 64 20 62 79 20 74 68 65 20 69 6d 70 6c 65 sed by the imple
21062 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 mentation of the
21063 20 49 4e 20 28 2e 2e 2e 29 20 6f 70 65 72 61 74 IN (...) operat
21064 6f 72 2e 0a 2a 2a 20 49 74 27 73 20 6a 6f 62 20 or..** It's job
21065 69 73 20 74 6f 20 66 69 6e 64 20 6f 72 20 63 72 is to find or cr
21066 65 61 74 65 20 61 20 62 2d 74 72 65 65 20 73 74 eate a b-tree st
21067 72 75 63 74 75 72 65 20 74 68 61 74 20 6d 61 79 ructure that may
21068 20 62 65 20 75 73 65 64 0a 2a 2a 20 65 69 74 68 be used.** eith
21069 65 72 20 74 6f 20 74 65 73 74 20 66 6f 72 20 6d er to test for m
2106a 65 6d 62 65 72 73 68 69 70 20 6f 66 20 74 68 65 embership of the
2106b 20 28 2e 2e 2e 29 20 73 65 74 20 6f 72 20 74 6f (...) set or to
2106c 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 iterate through
2106d 0a 2a 2a 20 69 74 73 20 6d 65 6d 62 65 72 73 2c .** its members,
2106e 20 73 6b 69 70 70 69 6e 67 20 64 75 70 6c 69 63 skipping duplic
2106f 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ates..**.** The
21070 69 6e 64 65 78 20 6f 66 20 74 68 65 20 63 75 72 index of the cur
21071 73 6f 72 20 6f 70 65 6e 65 64 20 6f 6e 20 74 68 sor opened on th
21072 65 20 62 2d 74 72 65 65 20 28 64 61 74 61 62 61 e b-tree (databa
21073 73 65 20 74 61 62 6c 65 2c 20 64 61 74 61 62 61 se table, databa
21074 73 65 20 69 6e 64 65 78 20 0a 2a 2a 20 6f 72 20 se index .** or
21075 65 70 68 65 72 6d 61 6c 20 74 61 62 6c 65 29 20 ephermal table)
21076 69 73 20 73 74 6f 72 65 64 20 69 6e 20 70 58 2d is stored in pX-
21077 3e 69 54 61 62 6c 65 20 62 65 66 6f 72 65 20 74 >iTable before t
21078 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 his function ret
21079 75 72 6e 73 2e 0a 2a 2a 20 54 68 65 20 72 65 74 urns..** The ret
2107a 75 72 6e 65 64 20 76 61 6c 75 65 20 6f 66 20 74 urned value of t
2107b 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e 64 his function ind
2107c 69 63 61 74 65 73 20 74 68 65 20 62 2d 74 72 65 icates the b-tre
2107d 65 20 74 79 70 65 2c 20 61 73 20 66 6f 6c 6c 6f e type, as follo
2107e 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 49 4e 5f 49 ws:.**.** IN_I
2107f 4e 44 45 58 5f 52 4f 57 49 44 20 2d 20 54 68 65 NDEX_ROWID - The
21080 20 63 75 72 73 6f 72 20 77 61 73 20 6f 70 65 6e cursor was open
21081 65 64 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65 ed on a database
21082 20 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 49 4e 5f table..** IN_
21083 49 4e 44 45 58 5f 49 4e 44 45 58 20 2d 20 54 68 INDEX_INDEX - Th
21084 65 20 63 75 72 73 6f 72 20 77 61 73 20 6f 70 65 e cursor was ope
21085 6e 65 64 20 6f 6e 20 61 20 64 61 74 61 62 61 73 ned on a databas
21086 65 20 69 6e 64 65 78 2e 0a 2a 2a 20 20 20 49 4e e index..** IN
21087 5f 49 4e 44 45 58 5f 45 50 48 20 2d 20 20 20 54 _INDEX_EPH - T
21088 68 65 20 63 75 72 73 6f 72 20 77 61 73 20 6f 70 he cursor was op
21089 65 6e 65 64 20 6f 6e 20 61 20 73 70 65 63 69 61 ened on a specia
2108a 6c 6c 79 20 63 72 65 61 74 65 64 20 61 6e 64 0a lly created and.
2108b 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
2108c 20 20 20 20 20 20 70 6f 70 75 6c 61 74 65 64 20 populated
2108d 65 70 68 65 72 65 6d 61 6c 20 74 61 62 6c 65 2e epheremal table.
2108e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 69 73 74 69 .**.** An existi
2108f 6e 67 20 62 2d 74 72 65 65 20 6d 61 79 20 6f 6e ng b-tree may on
21090 6c 79 20 62 65 20 75 73 65 64 20 69 66 20 74 68 ly be used if th
21091 65 20 53 45 4c 45 43 54 20 69 73 20 6f 66 20 74 e SELECT is of t
21092 68 65 20 73 69 6d 70 6c 65 0a 2a 2a 20 66 6f 72 he simple.** for
21093 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c m:.**.** SEL
21094 45 43 54 20 3c 63 6f 6c 75 6d 6e 3e 20 46 52 4f ECT <column> FRO
21095 4d 20 3c 74 61 62 6c 65 3e 0a 2a 2a 0a 2a 2a 20 M <table>.**.**
21096 49 66 20 74 68 65 20 70 72 4e 6f 74 46 6f 75 6e If the prNotFoun
21097 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 30 d parameter is 0
21098 2c 20 74 68 65 6e 20 74 68 65 20 62 2d 74 72 65 , then the b-tre
21099 65 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 74 e will be used t
2109a 6f 20 69 74 65 72 61 74 65 0a 2a 2a 20 74 68 72 o iterate.** thr
2109b 6f 75 67 68 20 74 68 65 20 73 65 74 20 6d 65 6d ough the set mem
2109c 62 65 72 73 2c 20 73 6b 69 70 70 69 6e 67 20 61 bers, skipping a
2109d 6e 79 20 64 75 70 6c 69 63 61 74 65 73 2e 20 49 ny duplicates. I
2109e 6e 20 74 68 69 73 20 63 61 73 65 20 61 6e 0a 2a n this case an.*
2109f 2a 20 65 70 68 65 72 65 6d 61 6c 20 74 61 62 6c * epheremal tabl
210a0 65 20 6d 75 73 74 20 62 65 20 75 73 65 64 20 75 e must be used u
210a1 6e 6c 65 73 73 20 74 68 65 20 73 65 6c 65 63 74 nless the select
210a2 65 64 20 3c 63 6f 6c 75 6d 6e 3e 20 69 73 20 67 ed <column> is g
210a3 75 61 72 61 6e 74 65 65 64 0a 2a 2a 20 74 6f 20 uaranteed.** to
210a4 62 65 20 75 6e 69 71 75 65 20 2d 20 65 69 74 68 be unique - eith
210a5 65 72 20 62 65 63 61 75 73 65 20 69 74 20 69 73 er because it is
210a6 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d an INTEGER PRIM
210a7 41 52 59 20 4b 45 59 20 6f 72 20 69 74 0a 2a 2a ARY KEY or it.**
210a8 20 68 61 73 20 61 20 55 4e 49 51 55 45 20 63 6f has a UNIQUE co
210a9 6e 73 74 72 61 69 6e 74 20 6f 72 20 55 4e 49 51 nstraint or UNIQ
210aa 55 45 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 UE index..**.**
210ab 49 66 20 74 68 65 20 70 72 4e 6f 74 46 6f 75 6e If the prNotFoun
210ac 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e d parameter is n
210ad 6f 74 20 30 2c 20 74 68 65 6e 20 74 68 65 20 62 ot 0, then the b
210ae 2d 74 72 65 65 20 77 69 6c 6c 20 62 65 20 75 73 -tree will be us
210af 65 64 20 0a 2a 2a 20 66 6f 72 20 66 61 73 74 20 ed .** for fast
210b0 73 65 74 20 6d 65 6d 62 65 72 73 68 69 70 20 74 set membership t
210b1 65 73 74 73 2e 20 49 6e 20 74 68 69 73 20 63 61 ests. In this ca
210b2 73 65 20 61 6e 20 65 70 68 65 72 65 6d 61 6c 20 se an epheremal
210b3 74 61 62 6c 65 20 6d 75 73 74 20 0a 2a 2a 20 62 table must .** b
210b4 65 20 75 73 65 64 20 75 6e 6c 65 73 73 20 3c 63 e used unless <c
210b5 6f 6c 75 6d 6e 3e 20 69 73 20 61 6e 20 49 4e 54 olumn> is an INT
210b6 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY
210b7 20 6f 72 20 61 6e 20 69 6e 64 65 78 20 63 61 6e or an index can
210b8 20 0a 2a 2a 20 62 65 20 66 6f 75 6e 64 20 77 69 .** be found wi
210b9 74 68 20 3c 63 6f 6c 75 6d 6e 3e 20 61 73 20 69 th <column> as i
210ba 74 73 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c ts left-most col
210bb 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 umn..**.** When
210bc 74 68 65 20 62 2d 74 72 65 65 20 69 73 20 62 65 the b-tree is be
210bd 69 6e 67 20 75 73 65 64 20 66 6f 72 20 6d 65 6d ing used for mem
210be 62 65 72 73 68 69 70 20 74 65 73 74 73 2c 20 74 bership tests, t
210bf 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 he calling funct
210c0 69 6f 6e 0a 2a 2a 20 6e 65 65 64 73 20 74 6f 20 ion.** needs to
210c1 6b 6e 6f 77 20 77 68 65 74 68 65 72 20 6f 72 20 know whether or
210c2 6e 6f 74 20 74 68 65 20 73 74 72 75 63 74 75 72 not the structur
210c3 65 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 53 51 e contains an SQ
210c4 4c 20 4e 55 4c 4c 20 0a 2a 2a 20 76 61 6c 75 65 L NULL .** value
210c5 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 6f 72 in order to cor
210c6 72 65 63 74 6c 79 20 65 76 61 6c 75 61 74 65 20 rectly evaluate
210c7 65 78 70 72 65 73 73 69 6f 6e 73 20 6c 69 6b 65 expressions like
210c8 20 22 58 20 49 4e 20 28 59 2c 20 5a 29 22 2e 0a "X IN (Y, Z)"..
210c9 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61 ** If there is a
210ca 20 63 68 61 6e 63 65 20 74 68 61 74 20 74 68 65 chance that the
210cb 20 62 2d 74 72 65 65 20 6d 69 67 68 74 20 63 6f b-tree might co
210cc 6e 74 61 69 6e 20 61 20 4e 55 4c 4c 20 76 61 6c ntain a NULL val
210cd 75 65 20 61 74 0a 2a 2a 20 72 75 6e 74 69 6d 65 ue at.** runtime
210ce 2c 20 74 68 65 6e 20 61 20 72 65 67 69 73 74 65 , then a registe
210cf 72 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 r is allocated a
210d0 6e 64 20 74 68 65 20 72 65 67 69 73 74 65 72 20 nd the register
210d1 6e 75 6d 62 65 72 20 77 72 69 74 74 65 6e 0a 2a number written.*
210d2 2a 20 74 6f 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 * to *prNotFound
210d3 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f . If there is no
210d4 20 63 68 61 6e 63 65 20 74 68 61 74 20 74 68 65 chance that the
210d5 20 62 2d 74 72 65 65 20 63 6f 6e 74 61 69 6e 73 b-tree contains
210d6 20 61 0a 2a 2a 20 4e 55 4c 4c 20 76 61 6c 75 65 a.** NULL value
210d7 2c 20 74 68 65 6e 20 2a 70 72 4e 6f 74 46 6f 75 , then *prNotFou
210d8 6e 64 20 69 73 20 6c 65 66 74 20 75 6e 63 68 61 nd is left uncha
210d9 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 nged..**.** If a
210da 20 72 65 67 69 73 74 65 72 20 69 73 20 61 6c 6c register is all
210db 6f 63 61 74 65 64 20 61 6e 64 20 69 74 73 20 6c ocated and its l
210dc 6f 63 61 74 69 6f 6e 20 73 74 6f 72 65 64 20 69 ocation stored i
210dd 6e 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 2c 20 74 n *prNotFound, t
210de 68 65 6e 0a 2a 2a 20 69 74 73 20 69 6e 69 74 69 hen.** its initi
210df 61 6c 20 76 61 6c 75 65 20 69 73 20 4e 55 4c 4c al value is NULL
210e0 2e 20 49 66 20 74 68 65 20 62 2d 74 72 65 65 20 . If the b-tree
210e1 64 6f 65 73 20 6e 6f 74 20 72 65 6d 61 69 6e 20 does not remain
210e2 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 66 6f 72 20 constant.** for
210e3 74 68 65 20 64 75 72 61 74 69 6f 6e 20 6f 66 20 the duration of
210e4 74 68 65 20 71 75 65 72 79 20 28 69 2e 65 2e 20 the query (i.e.
210e5 74 68 65 20 53 45 4c 45 43 54 20 74 68 61 74 20 the SELECT that
210e6 67 65 6e 65 72 61 74 65 73 20 74 68 65 20 62 2d generates the b-
210e7 74 72 65 65 0a 2a 2a 20 69 73 20 61 20 63 6f 72 tree.** is a cor
210e8 72 65 6c 61 74 65 64 20 73 75 62 71 75 65 72 79 related subquery
210e9 29 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 ) then the value
210ea 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 of the allocate
210eb 64 20 72 65 67 69 73 74 65 72 20 69 73 0a 2a 2a d register is.**
210ec 20 72 65 73 65 74 20 74 6f 20 4e 55 4c 4c 20 65 reset to NULL e
210ed 61 63 68 20 74 69 6d 65 20 74 68 65 20 62 2d 74 ach time the b-t
210ee 72 65 65 20 69 73 20 72 65 70 6f 70 75 6c 61 74 ree is repopulat
210ef 65 64 2e 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 ed. This allows
210f0 74 68 65 0a 2a 2a 20 63 61 6c 6c 65 72 20 74 6f the.** caller to
210f1 20 75 73 65 20 76 64 62 65 20 63 6f 64 65 20 65 use vdbe code e
210f2 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 74 68 65 quivalent to the
210f3 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a following:.**.*
210f4 2a 20 20 20 69 66 28 20 72 65 67 69 73 74 65 72 * if( register
210f5 3d 3d 4e 55 4c 4c 20 29 7b 0a 2a 2a 20 20 20 20 ==NULL ){.**
210f6 20 68 61 73 5f 6e 75 6c 6c 20 3d 20 3c 74 65 73 has_null = <tes
210f7 74 20 69 66 20 64 61 74 61 20 73 74 72 75 63 74 t if data struct
210f8 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 6e 75 6c ure contains nul
210f9 6c 3e 0a 2a 2a 20 20 20 20 20 72 65 67 69 73 74 l>.** regist
210fa 65 72 20 3d 20 31 0a 2a 2a 20 20 20 7d 0a 2a 2a er = 1.** }.**
210fb 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 .** in order to
210fc 61 76 6f 69 64 20 72 75 6e 6e 69 6e 67 20 74 68 avoid running th
210fd 65 20 3c 74 65 73 74 20 69 66 20 64 61 74 61 20 e <test if data
210fe 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 structure contai
210ff 6e 73 20 6e 75 6c 6c 3e 0a 2a 2a 20 74 65 73 74 ns null>.** test
21100 20 6d 6f 72 65 20 6f 66 74 65 6e 20 74 68 61 6e more often than
21101 20 69 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a is necessary..*
21102 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
21103 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 53 _OMIT_SUBQUERY.S
21104 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
21105 74 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 49 t sqlite3FindInI
21106 6e 64 65 78 28 50 61 72 73 65 20 2a 70 50 61 72 ndex(Parse *pPar
21107 73 65 2c 20 45 78 70 72 20 2a 70 58 2c 20 69 6e se, Expr *pX, in
21108 74 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 29 7b 0a t *prNotFound){.
21109 20 20 53 65 6c 65 63 74 20 2a 70 3b 20 20 20 20 Select *p;
2110a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2110b 20 20 20 20 20 20 20 20 2f 2a 20 53 45 4c 45 43 /* SELEC
2110c 54 20 74 6f 20 74 68 65 20 72 69 67 68 74 20 6f T to the right o
2110d 66 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f f IN operator */
2110e 0a 20 20 69 6e 74 20 65 54 79 70 65 20 3d 20 30 . int eType = 0
2110f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
21110 20 20 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65 /* Type
21111 20 6f 66 20 52 48 53 20 74 61 62 6c 65 2e 20 49 of RHS table. I
21112 4e 5f 49 4e 44 45 58 5f 2a 20 2a 2f 0a 20 20 69 N_INDEX_* */. i
21113 6e 74 20 69 54 61 62 20 3d 20 70 50 61 72 73 65 nt iTab = pParse
21114 2d 3e 6e 54 61 62 2b 2b 3b 20 20 20 20 20 20 20 ->nTab++;
21115 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6f /* Cursor o
21116 66 20 74 68 65 20 52 48 53 20 74 61 62 6c 65 20 f the RHS table
21117 2a 2f 0a 20 20 69 6e 74 20 6d 75 73 74 42 65 55 */. int mustBeU
21118 6e 69 71 75 65 20 3d 20 28 70 72 4e 6f 74 46 6f nique = (prNotFo
21119 75 6e 64 3d 3d 30 29 3b 20 20 20 2f 2a 20 54 72 und==0); /* Tr
2111a 75 65 20 69 66 20 52 48 53 20 6d 75 73 74 20 62 ue if RHS must b
2111b 65 20 75 6e 69 71 75 65 20 2a 2f 0a 0a 20 20 61 e unique */.. a
2111c 73 73 65 72 74 28 20 70 58 2d 3e 6f 70 3d 3d 54 ssert( pX->op==T
2111d 4b 5f 49 4e 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 K_IN );.. /* Ch
2111e 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 61 6e eck to see if an
2111f 20 65 78 69 73 74 69 6e 67 20 74 61 62 6c 65 20 existing table
21120 6f 72 20 69 6e 64 65 78 20 63 61 6e 20 62 65 20 or index can be
21121 75 73 65 64 20 74 6f 0a 20 20 2a 2a 20 73 61 74 used to. ** sat
21122 69 73 66 79 20 74 68 65 20 71 75 65 72 79 2e 20 isfy the query.
21123 20 54 68 69 73 20 69 73 20 70 72 65 66 65 72 61 This is prefera
21124 62 6c 65 20 74 6f 20 67 65 6e 65 72 61 74 69 6e ble to generatin
21125 67 20 61 20 6e 65 77 20 0a 20 20 2a 2a 20 65 70 g a new . ** ep
21126 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 2e 0a 20 hemeral table..
21127 20 2a 2f 0a 20 20 70 20 3d 20 28 45 78 70 72 48 */. p = (ExprH
21128 61 73 50 72 6f 70 65 72 74 79 28 70 58 2c 20 45 asProperty(pX, E
21129 50 5f 78 49 73 53 65 6c 65 63 74 29 20 3f 20 70 P_xIsSelect) ? p
2112a 58 2d 3e 78 2e 70 53 65 6c 65 63 74 20 3a 20 30 X->x.pSelect : 0
2112b 29 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 );. if( ALWAYS(
2112c 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 29 pParse->nErr==0)
2112d 20 26 26 20 69 73 43 61 6e 64 69 64 61 74 65 46 && isCandidateF
2112e 6f 72 49 6e 4f 70 74 28 70 29 20 29 7b 0a 20 20 orInOpt(p) ){.
2112f 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
21130 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 pParse->db;
21131 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 /* Data
21132 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
21133 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78 */. Expr *pEx
21134 70 72 20 3d 20 70 2d 3e 70 45 4c 69 73 74 2d 3e pr = p->pEList->
21135 61 5b 30 5d 2e 70 45 78 70 72 3b 20 20 20 2f 2a a[0].pExpr; /*
21136 20 45 78 70 72 65 73 73 69 6f 6e 20 3c 63 6f 6c Expression <col
21137 75 6d 6e 3e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 umn> */. int
21138 69 43 6f 6c 20 3d 20 70 45 78 70 72 2d 3e 69 43 iCol = pExpr->iC
21139 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 olumn;
2113a 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 /* Index of c
2113b 6f 6c 75 6d 6e 20 3c 63 6f 6c 75 6d 6e 3e 20 2a olumn <column> *
2113c 2f 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20 /. Vdbe *v =
2113d 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 sqlite3GetVdbe(p
2113e 50 61 72 73 65 29 3b 20 20 20 20 20 20 2f 2a 20 Parse); /*
2113f 56 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 Virtual machine
21140 62 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0a 20 being coded */.
21141 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d Table *pTab =
21142 20 70 2d 3e 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 p->pSrc->a[0].p
21143 54 61 62 3b 20 20 20 20 20 20 2f 2a 20 54 61 62 Tab; /* Tab
21144 6c 65 20 3c 74 61 62 6c 65 3e 2e 20 2a 2f 0a 20 le <table>. */.
21145 20 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 int iDb;
21146 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21147 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 /* Dat
21148 61 62 61 73 65 20 69 64 78 20 66 6f 72 20 70 54 abase idx for pT
21149 61 62 20 2a 2f 0a 20 20 20 0a 20 20 20 20 2f 2a ab */. . /*
2114a 20 43 6f 64 65 20 61 6e 20 4f 50 5f 56 65 72 69 Code an OP_Veri
2114b 66 79 43 6f 6f 6b 69 65 20 61 6e 64 20 4f 50 5f fyCookie and OP_
2114c 54 61 62 6c 65 4c 6f 63 6b 20 66 6f 72 20 3c 74 TableLock for <t
2114d 61 62 6c 65 3e 2e 20 2a 2f 0a 20 20 20 20 69 44 able>. */. iD
2114e 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d b = sqlite3Schem
2114f 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 aToIndex(db, pTa
21150 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 b->pSchema);.
21151 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 sqlite3CodeVeri
21152 66 79 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c fySchema(pParse,
21153 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 iDb);. sqlit
21154 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72 e3TableLock(pPar
21155 73 65 2c 20 69 44 62 2c 20 70 54 61 62 2d 3e 74 se, iDb, pTab->t
21156 6e 75 6d 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e num, 0, pTab->zN
21157 61 6d 65 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 ame);.. /* Th
21158 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f is function is o
21159 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 nly called from
2115a 74 77 6f 20 70 6c 61 63 65 73 2e 20 49 6e 20 62 two places. In b
2115b 6f 74 68 20 63 61 73 65 73 20 74 68 65 20 76 64 oth cases the vd
2115c 62 65 0a 20 20 20 20 2a 2a 20 68 61 73 20 61 6c be. ** has al
2115d 72 65 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 ready been alloc
2115e 61 74 65 64 2e 20 53 6f 20 61 73 73 75 6d 65 20 ated. So assume
2115f 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 29 sqlite3GetVdbe()
21160 20 69 73 20 61 6c 77 61 79 73 0a 20 20 20 20 2a is always. *
21161 2a 20 73 75 63 63 65 73 73 66 75 6c 20 68 65 72 * successful her
21162 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 e.. */. as
21163 73 65 72 74 28 76 29 3b 0a 20 20 20 20 69 66 28 sert(v);. if(
21164 20 69 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 20 iCol<0 ){.
21165 20 69 6e 74 20 69 4d 65 6d 20 3d 20 2b 2b 70 50 int iMem = ++pP
21166 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 arse->nMem;.
21167 20 20 69 6e 74 20 69 41 64 64 72 3b 0a 0a 20 20 int iAddr;..
21168 20 20 20 20 69 41 64 64 72 20 3d 20 73 71 6c 69 iAddr = sqli
21169 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c te3VdbeAddOp1(v,
2116a 20 4f 50 5f 49 66 2c 20 69 4d 65 6d 29 3b 0a 20 OP_If, iMem);.
2116b 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
2116c 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 AddOp2(v, OP_Int
2116d 65 67 65 72 2c 20 31 2c 20 69 4d 65 6d 29 3b 0a eger, 1, iMem);.
2116e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 70 . sqlite3Op
2116f 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 enTable(pParse,
21170 69 54 61 62 2c 20 69 44 62 2c 20 70 54 61 62 2c iTab, iDb, pTab,
21171 20 4f 50 5f 4f 70 65 6e 52 65 61 64 29 3b 0a 20 OP_OpenRead);.
21172 20 20 20 20 20 65 54 79 70 65 20 3d 20 49 4e 5f eType = IN_
21173 49 4e 44 45 58 5f 52 4f 57 49 44 3b 0a 0a 20 20 INDEX_ROWID;..
21174 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a sqlite3VdbeJ
21175 75 6d 70 48 65 72 65 28 76 2c 20 69 41 64 64 72 umpHere(v, iAddr
21176 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
21177 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b Index *pIdx;
21178 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21179 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 /* Iter
2117a 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f ator variable */
2117b 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 .. /* The c
2117c 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 ollation sequenc
2117d 65 20 75 73 65 64 20 62 79 20 74 68 65 20 63 6f e used by the co
2117e 6d 70 61 72 69 73 6f 6e 2e 20 49 66 20 61 6e 20 mparison. If an
2117f 69 6e 64 65 78 20 69 73 20 74 6f 0a 20 20 20 20 index is to.
21180 20 20 2a 2a 20 62 65 20 75 73 65 64 20 69 6e 20 ** be used in
21181 70 6c 61 63 65 20 6f 66 20 61 20 74 65 6d 70 2d place of a temp-
21182 74 61 62 6c 65 2c 20 69 74 20 6d 75 73 74 20 62 table, it must b
21183 65 20 6f 72 64 65 72 65 64 20 61 63 63 6f 72 64 e ordered accord
21184 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 ing. ** to
21185 74 68 69 73 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 this collation s
21186 65 71 75 65 6e 63 65 2e 20 20 2a 2f 0a 20 20 20 equence. */.
21187 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 52 65 71 CollSeq *pReq
21188 20 3d 20 73 71 6c 69 74 65 33 42 69 6e 61 72 79 = sqlite3Binary
21189 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 70 CompareCollSeq(p
2118a 50 61 72 73 65 2c 20 70 58 2d 3e 70 4c 65 66 74 Parse, pX->pLeft
2118b 2c 20 70 45 78 70 72 29 3b 0a 0a 20 20 20 20 20 , pExpr);..
2118c 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 /* Check that t
2118d 68 65 20 61 66 66 69 6e 69 74 79 20 74 68 61 74 he affinity that
2118e 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 74 6f will be used to
2118f 20 70 65 72 66 6f 72 6d 20 74 68 65 20 0a 20 20 perform the .
21190 20 20 20 20 2a 2a 20 63 6f 6d 70 61 72 69 73 6f ** compariso
21191 6e 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 n is the same as
21192 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 the affinity of
21193 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 49 66 0a the column. If.
21194 20 20 20 20 20 20 2a 2a 20 69 74 20 69 73 20 6e ** it is n
21195 6f 74 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 6f ot, it is not po
21196 73 73 69 62 6c 65 20 74 6f 20 75 73 65 20 61 6e ssible to use an
21197 79 20 69 6e 64 65 78 2e 0a 20 20 20 20 20 20 2a y index.. *
21198 2f 0a 20 20 20 20 20 20 63 68 61 72 20 61 66 66 /. char aff
21199 20 3d 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 = comparisonAff
2119a 69 6e 69 74 79 28 70 58 29 3b 0a 20 20 20 20 20 inity(pX);.
2119b 20 69 6e 74 20 61 66 66 69 6e 69 74 79 5f 6f 6b int affinity_ok
2119c 20 3d 20 28 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 = (pTab->aCol[i
2119d 43 6f 6c 5d 2e 61 66 66 69 6e 69 74 79 3d 3d 61 Col].affinity==a
2119e 66 66 7c 7c 61 66 66 3d 3d 53 51 4c 49 54 45 5f ff||aff==SQLITE_
2119f 41 46 46 5f 4e 4f 4e 45 29 3b 0a 0a 20 20 20 20 AFF_NONE);..
211a0 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d for(pIdx=pTab-
211a1 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 20 26 26 >pIndex; pIdx &&
211a2 20 65 54 79 70 65 3d 3d 30 20 26 26 20 61 66 66 eType==0 && aff
211a3 69 6e 69 74 79 5f 6f 6b 3b 20 70 49 64 78 3d 70 inity_ok; pIdx=p
211a4 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 Idx->pNext){.
211a5 20 20 20 20 20 69 66 28 20 28 70 49 64 78 2d 3e if( (pIdx->
211a6 61 69 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d 69 43 6f aiColumn[0]==iCo
211a7 6c 29 0a 20 20 20 20 20 20 20 20 20 26 26 20 73 l). && s
211a8 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 qlite3FindCollSe
211a9 71 28 64 62 2c 20 45 4e 43 28 64 62 29 2c 20 70 q(db, ENC(db), p
211aa 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 30 5d 2c 20 Idx->azColl[0],
211ab 30 29 3d 3d 70 52 65 71 0a 20 20 20 20 20 20 20 0)==pReq.
211ac 20 20 26 26 20 28 21 6d 75 73 74 42 65 55 6e 69 && (!mustBeUni
211ad 71 75 65 20 7c 7c 20 28 70 49 64 78 2d 3e 6e 43 que || (pIdx->nC
211ae 6f 6c 75 6d 6e 3d 3d 31 20 26 26 20 70 49 64 78 olumn==1 && pIdx
211af 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f ->onError!=OE_No
211b0 6e 65 29 29 0a 20 20 20 20 20 20 20 20 29 7b 0a ne)). ){.
211b1 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 4d int iM
211b2 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e em = ++pParse->n
211b3 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 Mem;. i
211b4 6e 74 20 69 41 64 64 72 3b 0a 20 20 20 20 20 20 nt iAddr;.
211b5 20 20 20 20 63 68 61 72 20 2a 70 4b 65 79 3b 0a char *pKey;.
211b6 20 20 0a 20 20 20 20 20 20 20 20 20 20 70 4b 65 . pKe
211b7 79 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 y = (char *)sqli
211b8 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 te3IndexKeyinfo(
211b9 70 50 61 72 73 65 2c 20 70 49 64 78 29 3b 0a 20 pParse, pIdx);.
211ba 20 20 20 20 20 20 20 20 20 69 41 64 64 72 20 3d iAddr =
211bb 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
211bc 70 31 28 76 2c 20 4f 50 5f 49 66 2c 20 69 4d 65 p1(v, OP_If, iMe
211bd 6d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 m);. sq
211be 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
211bf 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 v, OP_Integer, 1
211c0 2c 20 69 4d 65 6d 29 3b 0a 20 20 0a 20 20 20 20 , iMem);. .
211c1 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
211c2 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 eAddOp4(v, OP_Op
211c3 65 6e 52 65 61 64 2c 20 69 54 61 62 2c 20 70 49 enRead, iTab, pI
211c4 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 0a 20 dx->tnum, iDb,.
211c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
211c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4b pK
211c7 65 79 2c 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 ey,P4_KEYINFO_HA
211c8 4e 44 4f 46 46 29 3b 0a 20 20 20 20 20 20 20 20 NDOFF);.
211c9 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 VdbeComment((v
211ca 2c 20 22 25 73 22 2c 20 70 49 64 78 2d 3e 7a 4e , "%s", pIdx->zN
211cb 61 6d 65 29 29 3b 0a 20 20 20 20 20 20 20 20 20 ame));.
211cc 20 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45 eType = IN_INDE
211cd 58 5f 49 4e 44 45 58 3b 0a 0a 20 20 20 20 20 20 X_INDEX;..
211ce 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a sqlite3VdbeJ
211cf 75 6d 70 48 65 72 65 28 76 2c 20 69 41 64 64 72 umpHere(v, iAddr
211d0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
211d1 20 70 72 4e 6f 74 46 6f 75 6e 64 20 26 26 20 21 prNotFound && !
211d2 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d pTab->aCol[iCol]
211d3 2e 6e 6f 74 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 .notNull ){.
211d4 20 20 20 20 20 20 20 20 2a 70 72 4e 6f 74 46 6f *prNotFo
211d5 75 6e 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e und = ++pParse->
211d6 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 nMem;.
211d7 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
211d8 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 }. }. }..
211d9 20 69 66 28 20 65 54 79 70 65 3d 3d 30 20 29 7b if( eType==0 ){
211da 0a 20 20 20 20 2f 2a 20 43 6f 75 6c 64 20 6e 6f . /* Could no
211db 74 20 66 6f 75 6e 64 20 61 6e 20 65 78 69 73 74 t found an exist
211dc 69 6e 67 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 ing table or ind
211dd 65 78 20 74 6f 20 75 73 65 20 61 73 20 74 68 65 ex to use as the
211de 20 52 48 53 20 62 2d 74 72 65 65 2e 0a 20 20 20 RHS b-tree..
211df 20 2a 2a 20 57 65 20 77 69 6c 6c 20 68 61 76 65 ** We will have
211e0 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61 6e 20 to generate an
211e1 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 ephemeral table
211e2 74 6f 20 64 6f 20 74 68 65 20 6a 6f 62 2e 0a 20 to do the job..
211e3 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 4d */. int rM
211e4 61 79 48 61 76 65 4e 75 6c 6c 20 3d 20 30 3b 0a ayHaveNull = 0;.
211e5 20 20 20 20 65 54 79 70 65 20 3d 20 49 4e 5f 49 eType = IN_I
211e6 4e 44 45 58 5f 45 50 48 3b 0a 20 20 20 20 69 66 NDEX_EPH;. if
211e7 28 20 70 72 4e 6f 74 46 6f 75 6e 64 20 29 7b 0a ( prNotFound ){.
211e8 20 20 20 20 20 20 2a 70 72 4e 6f 74 46 6f 75 6e *prNotFoun
211e9 64 20 3d 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c d = rMayHaveNull
211ea 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 = ++pParse->nMe
211eb 6d 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 m;. }else if(
211ec 20 70 58 2d 3e 70 4c 65 66 74 2d 3e 69 43 6f 6c pX->pLeft->iCol
211ed 75 6d 6e 3c 30 20 26 26 20 21 45 78 70 72 48 61 umn<0 && !ExprHa
211ee 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 58 2c sAnyProperty(pX,
211ef 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 EP_xIsSelect) )
211f0 7b 0a 20 20 20 20 20 20 65 54 79 70 65 20 3d 20 {. eType =
211f1 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 3b 0a IN_INDEX_ROWID;.
211f2 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
211f3 33 43 6f 64 65 53 75 62 73 65 6c 65 63 74 28 70 3CodeSubselect(p
211f4 50 61 72 73 65 2c 20 70 58 2c 20 72 4d 61 79 48 Parse, pX, rMayH
211f5 61 76 65 4e 75 6c 6c 2c 20 65 54 79 70 65 3d 3d aveNull, eType==
211f6 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 29 3b IN_INDEX_ROWID);
211f7 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 58 . }else{. pX
211f8 2d 3e 69 54 61 62 6c 65 20 3d 20 69 54 61 62 3b ->iTable = iTab;
211f9 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 65 54 . }. return eT
211fa 79 70 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f ype;.}.#endif../
211fb 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f *.** Generate co
211fc 64 65 20 66 6f 72 20 73 63 61 6c 61 72 20 73 75 de for scalar su
211fd 62 71 75 65 72 69 65 73 20 75 73 65 64 20 61 73 bqueries used as
211fe 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a an expression.*
211ff 2a 20 61 6e 64 20 49 4e 20 6f 70 65 72 61 74 6f * and IN operato
21200 72 73 2e 20 20 45 78 61 6d 70 6c 65 73 3a 0a 2a rs. Examples:.*
21201 2a 0a 2a 2a 20 20 20 20 20 28 53 45 4c 45 43 54 *.** (SELECT
21202 20 61 20 46 52 4f 4d 20 62 29 20 20 20 20 20 20 a FROM b)
21203 20 20 20 20 2d 2d 20 73 75 62 71 75 65 72 79 0a -- subquery.
21204 2a 2a 20 20 20 20 20 45 58 49 53 54 53 20 28 53 ** EXISTS (S
21205 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 62 29 20 ELECT a FROM b)
21206 20 20 2d 2d 20 45 58 49 53 54 53 20 73 75 62 71 -- EXISTS subq
21207 75 65 72 79 0a 2a 2a 20 20 20 20 20 78 20 49 4e uery.** x IN
21208 20 28 34 2c 35 2c 31 31 29 20 20 20 20 20 20 20 (4,5,11)
21209 20 20 20 20 20 20 20 2d 2d 20 49 4e 20 6f 70 65 -- IN ope
2120a 72 61 74 6f 72 20 77 69 74 68 20 6c 69 73 74 20 rator with list
2120b 6f 6e 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 on right-hand si
2120c 64 65 0a 2a 2a 20 20 20 20 20 78 20 49 4e 20 28 de.** x IN (
2120d 53 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 62 29 SELECT a FROM b)
2120e 20 20 20 20 20 2d 2d 20 49 4e 20 6f 70 65 72 61 -- IN opera
2120f 74 6f 72 20 77 69 74 68 20 73 75 62 71 75 65 72 tor with subquer
21210 79 20 6f 6e 20 74 68 65 20 72 69 67 68 74 0a 2a y on the right.*
21211 2a 0a 2a 2a 20 54 68 65 20 70 45 78 70 72 20 70 *.** The pExpr p
21212 61 72 61 6d 65 74 65 72 20 64 65 73 63 72 69 62 arameter describ
21213 65 73 20 74 68 65 20 65 78 70 72 65 73 73 69 6f es the expressio
21214 6e 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 n that contains
21215 74 68 65 20 49 4e 0a 2a 2a 20 6f 70 65 72 61 74 the IN.** operat
21216 6f 72 20 6f 72 20 73 75 62 71 75 65 72 79 2e 0a or or subquery..
21217 2a 2a 0a 2a 2a 20 49 66 20 70 61 72 61 6d 65 74 **.** If paramet
21218 65 72 20 69 73 52 6f 77 69 64 20 69 73 20 6e 6f er isRowid is no
21219 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 65 78 70 n-zero, then exp
2121a 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 69 73 ression pExpr is
2121b 20 67 75 61 72 61 6e 74 65 65 64 0a 2a 2a 20 74 guaranteed.** t
2121c 6f 20 62 65 20 6f 66 20 74 68 65 20 66 6f 72 6d o be of the form
2121d 20 22 3c 72 6f 77 69 64 3e 20 49 4e 20 28 3f 2c "<rowid> IN (?,
2121e 20 3f 2c 20 3f 29 22 2c 20 77 68 65 72 65 20 3c ?, ?)", where <
2121f 72 6f 77 69 64 3e 20 69 73 20 61 20 72 65 66 65 rowid> is a refe
21220 72 65 6e 63 65 0a 2a 2a 20 74 6f 20 73 6f 6d 65 rence.** to some
21221 20 69 6e 74 65 67 65 72 20 6b 65 79 20 63 6f 6c integer key col
21222 75 6d 6e 20 6f 66 20 61 20 74 61 62 6c 65 20 42 umn of a table B
21223 2d 54 72 65 65 2e 20 49 6e 20 74 68 69 73 20 63 -Tree. In this c
21224 61 73 65 2c 20 75 73 65 20 61 6e 0a 2a 2a 20 69 ase, use an.** i
21225 6e 74 6b 65 79 20 42 2d 54 72 65 65 20 74 6f 20 ntkey B-Tree to
21226 73 74 6f 72 65 20 74 68 65 20 73 65 74 20 6f 66 store the set of
21227 20 49 4e 28 2e 2e 2e 29 20 76 61 6c 75 65 73 20 IN(...) values
21228 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 75 instead of the u
21229 73 75 61 6c 0a 2a 2a 20 28 73 6c 6f 77 65 72 29 sual.** (slower)
2122a 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 variable length
2122b 20 6b 65 79 73 20 42 2d 54 72 65 65 2e 0a 2a 2a keys B-Tree..**
2122c 0a 2a 2a 20 49 66 20 72 4d 61 79 48 61 76 65 4e .** If rMayHaveN
2122d 75 6c 6c 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c ull is non-zero,
2122e 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 that means that
2122f 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 the operation i
21230 73 20 61 6e 20 49 4e 0a 2a 2a 20 28 6e 6f 74 20 s an IN.** (not
21231 61 20 53 45 4c 45 43 54 20 6f 72 20 45 58 49 53 a SELECT or EXIS
21232 54 53 29 20 61 6e 64 20 74 68 61 74 20 74 68 65 TS) and that the
21233 20 52 48 53 20 6d 69 67 68 74 20 63 6f 6e 74 61 RHS might conta
21234 69 6e 73 20 4e 55 4c 4c 73 2e 0a 2a 2a 20 46 75 ins NULLs..** Fu
21235 72 74 68 65 72 6d 6f 72 65 2c 20 74 68 65 20 49 rthermore, the I
21236 4e 20 69 73 20 69 6e 20 61 20 57 48 45 52 45 20 N is in a WHERE
21237 63 6c 61 75 73 65 20 61 6e 64 20 74 68 61 74 20 clause and that
21238 77 65 20 72 65 61 6c 6c 79 20 77 61 6e 74 0a 2a we really want.*
21239 2a 20 74 6f 20 69 74 65 72 61 74 65 20 6f 76 65 * to iterate ove
2123a 72 20 74 68 65 20 52 48 53 20 6f 66 20 74 68 65 r the RHS of the
2123b 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 69 6e 20 IN operator in
2123c 6f 72 64 65 72 20 74 6f 20 71 75 69 63 6b 6c 79 order to quickly
2123d 20 6c 6f 63 61 74 65 0a 2a 2a 20 61 6c 6c 20 63 locate.** all c
2123e 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 4c 48 53 orresponding LHS
2123f 20 65 6c 65 6d 65 6e 74 73 2e 20 20 41 6c 6c 20 elements. All
21240 74 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 this routine doe
21241 73 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 0a s is initialize.
21242 2a 2a 20 74 68 65 20 72 65 67 69 73 74 65 72 20 ** the register
21243 67 69 76 65 6e 20 62 79 20 72 4d 61 79 48 61 76 given by rMayHav
21244 65 4e 75 6c 6c 20 74 6f 20 4e 55 4c 4c 2e 20 20 eNull to NULL.
21245 43 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 73 Calling routines
21246 20 77 69 6c 6c 20 74 61 6b 65 0a 2a 2a 20 63 61 will take.** ca
21247 72 65 20 6f 66 20 63 68 61 6e 67 69 6e 67 20 74 re of changing t
21248 68 69 73 20 72 65 67 69 73 74 65 72 20 76 61 6c his register val
21249 75 65 20 74 6f 20 6e 6f 6e 2d 4e 55 4c 4c 20 69 ue to non-NULL i
2124a 66 20 74 68 65 20 52 48 53 20 69 73 20 4e 55 4c f the RHS is NUL
2124b 4c 2d 66 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 L-free..**.** If
2124c 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 69 73 rMayHaveNull is
2124d 20 7a 65 72 6f 2c 20 74 68 61 74 20 6d 65 61 6e zero, that mean
2124e 73 20 74 68 61 74 20 74 68 65 20 73 75 62 71 75 s that the subqu
2124f 65 72 79 20 69 73 20 62 65 69 6e 67 20 75 73 65 ery is being use
21250 64 0a 2a 2a 20 66 6f 72 20 6d 65 6d 62 65 72 73 d.** for members
21251 68 69 70 20 74 65 73 74 69 6e 67 20 6f 6e 6c 79 hip testing only
21252 2e 20 20 54 68 65 72 65 20 69 73 20 6e 6f 20 6e . There is no n
21253 65 65 64 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a eed to initializ
21254 65 20 61 6e 79 0a 2a 2a 20 72 65 67 69 73 74 65 e any.** registe
21255 72 73 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 rs to indicate t
21256 68 65 20 70 72 65 73 65 6e 73 65 20 6f 72 20 61 he presense or a
21257 62 73 65 6e 63 65 20 6f 66 20 4e 55 4c 4c 73 20 bsence of NULLs
21258 6f 6e 20 74 68 65 20 52 48 53 2e 0a 2a 2a 0a 2a on the RHS..**.*
21259 2a 20 46 6f 72 20 61 20 53 45 4c 45 43 54 20 6f * For a SELECT o
2125a 72 20 45 58 49 53 54 53 20 6f 70 65 72 61 74 6f r EXISTS operato
2125b 72 2c 20 72 65 74 75 72 6e 20 74 68 65 20 72 65 r, return the re
2125c 67 69 73 74 65 72 20 74 68 61 74 20 68 6f 6c 64 gister that hold
2125d 73 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 2e s the.** result.
2125e 20 20 46 6f 72 20 49 4e 20 6f 70 65 72 61 74 6f For IN operato
2125f 72 73 20 6f 72 20 69 66 20 61 6e 20 65 72 72 6f rs or if an erro
21260 72 20 6f 63 63 75 72 73 2c 20 74 68 65 20 72 65 r occurs, the re
21261 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 30 2e turn value is 0.
21262 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
21263 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 TE_OMIT_SUBQUERY
21264 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
21265 69 6e 74 20 73 71 6c 69 74 65 33 43 6f 64 65 53 int sqlite3CodeS
21266 75 62 73 65 6c 65 63 74 28 0a 20 20 50 61 72 73 ubselect(. Pars
21267 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 e *pParse,
21268 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 /* Parsing c
21269 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 ontext */. Expr
2126a 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20 20 *pExpr,
2126b 20 20 20 20 2f 2a 20 54 68 65 20 49 4e 2c 20 53 /* The IN, S
2126c 45 4c 45 43 54 2c 20 6f 72 20 45 58 49 53 54 53 ELECT, or EXISTS
2126d 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 69 operator */. i
2126e 6e 74 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 2c nt rMayHaveNull,
2126f 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 /* Regist
21270 65 72 20 74 68 61 74 20 72 65 63 6f 72 64 73 20 er that records
21271 77 68 65 74 68 65 72 20 4e 55 4c 4c 73 20 65 78 whether NULLs ex
21272 69 73 74 20 69 6e 20 52 48 53 20 2a 2f 0a 20 20 ist in RHS */.
21273 69 6e 74 20 69 73 52 6f 77 69 64 20 20 20 20 20 int isRowid
21274 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 /* If tr
21275 75 65 2c 20 4c 48 53 20 6f 66 20 49 4e 20 6f 70 ue, LHS of IN op
21276 65 72 61 74 6f 72 20 69 73 20 61 20 72 6f 77 69 erator is a rowi
21277 64 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 74 65 d */.){. int te
21278 73 74 41 64 64 72 20 3d 20 30 3b 20 20 20 20 20 stAddr = 0;
21279 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2127a 20 20 2f 2a 20 4f 6e 65 2d 74 69 6d 65 20 74 65 /* One-time te
2127b 73 74 20 61 64 64 72 65 73 73 20 2a 2f 0a 20 20 st address */.
2127c 69 6e 74 20 72 52 65 67 20 3d 20 30 3b 20 20 20 int rReg = 0;
2127d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2127e 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 /* Regis
2127f 74 65 72 20 73 74 6f 72 69 6e 67 20 72 65 73 75 ter storing resu
21280 6c 74 69 6e 67 20 2a 2f 0a 20 20 56 64 62 65 20 lting */. Vdbe
21281 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 *v = sqlite3GetV
21282 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 dbe(pParse);. i
21283 66 28 20 4e 45 56 45 52 28 76 3d 3d 30 29 20 29 f( NEVER(v==0) )
21284 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 73 71 6c return 0;. sql
21285 69 74 65 33 45 78 70 72 43 61 63 68 65 50 75 73 ite3ExprCachePus
21286 68 28 70 50 61 72 73 65 29 3b 0a 0a 20 20 2f 2a h(pParse);.. /*
21287 20 54 68 69 73 20 63 6f 64 65 20 6d 75 73 74 20 This code must
21288 62 65 20 72 75 6e 20 69 6e 20 69 74 73 20 65 6e be run in its en
21289 74 69 72 65 74 79 20 65 76 65 72 79 20 74 69 6d tirety every tim
2128a 65 20 69 74 20 69 73 20 65 6e 63 6f 75 6e 74 65 e it is encounte
2128b 72 65 64 0a 20 20 2a 2a 20 69 66 20 61 6e 79 20 red. ** if any
2128c 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
2128d 20 69 73 20 74 72 75 65 3a 0a 20 20 2a 2a 0a 20 is true:. **.
2128e 20 2a 2a 20 20 20 20 2a 20 20 54 68 65 20 72 69 ** * The ri
2128f 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 69 73 ght-hand side is
21290 20 61 20 63 6f 72 72 65 6c 61 74 65 64 20 73 75 a correlated su
21291 62 71 75 65 72 79 0a 20 20 2a 2a 20 20 20 20 2a bquery. ** *
21292 20 20 54 68 65 20 72 69 67 68 74 2d 68 61 6e 64 The right-hand
21293 20 73 69 64 65 20 69 73 20 61 6e 20 65 78 70 72 side is an expr
21294 65 73 73 69 6f 6e 20 6c 69 73 74 20 63 6f 6e 74 ession list cont
21295 61 69 6e 69 6e 67 20 76 61 72 69 61 62 6c 65 73 aining variables
21296 0a 20 20 2a 2a 20 20 20 20 2a 20 20 57 65 20 61 . ** * We a
21297 72 65 20 69 6e 73 69 64 65 20 61 20 74 72 69 67 re inside a trig
21298 67 65 72 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 ger. **. ** If
21299 20 61 6c 6c 20 6f 66 20 74 68 65 20 61 62 6f 76 all of the abov
2129a 65 20 61 72 65 20 66 61 6c 73 65 2c 20 74 68 65 e are false, the
2129b 6e 20 77 65 20 63 61 6e 20 72 75 6e 20 74 68 69 n we can run thi
2129c 73 20 63 6f 64 65 20 6a 75 73 74 20 6f 6e 63 65 s code just once
2129d 0a 20 20 2a 2a 20 73 61 76 65 20 74 68 65 20 72 . ** save the r
2129e 65 73 75 6c 74 73 2c 20 61 6e 64 20 72 65 75 73 esults, and reus
2129f 65 20 74 68 65 20 73 61 6d 65 20 72 65 73 75 6c e the same resul
212a0 74 20 6f 6e 20 73 75 62 73 65 71 75 65 6e 74 20 t on subsequent
212a1 69 6e 76 6f 63 61 74 69 6f 6e 73 2e 0a 20 20 2a invocations.. *
212a2 2f 0a 20 20 69 66 28 20 21 45 78 70 72 48 61 73 /. if( !ExprHas
212a3 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 70 AnyProperty(pExp
212a4 72 2c 20 45 50 5f 56 61 72 53 65 6c 65 63 74 29 r, EP_VarSelect)
212a5 20 26 26 20 21 70 50 61 72 73 65 2d 3e 70 54 72 && !pParse->pTr
212a6 69 67 67 65 72 54 61 62 20 29 7b 0a 20 20 20 20 iggerTab ){.
212a7 69 6e 74 20 6d 65 6d 20 3d 20 2b 2b 70 50 61 72 int mem = ++pPar
212a8 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 se->nMem;. sq
212a9 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 lite3VdbeAddOp1(
212aa 76 2c 20 4f 50 5f 49 66 2c 20 6d 65 6d 29 3b 0a v, OP_If, mem);.
212ab 20 20 20 20 74 65 73 74 41 64 64 72 20 3d 20 73 testAddr = s
212ac 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
212ad 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 (v, OP_Integer,
212ae 31 2c 20 6d 65 6d 29 3b 0a 20 20 20 20 61 73 73 1, mem);. ass
212af 65 72 74 28 20 74 65 73 74 41 64 64 72 3e 30 20 ert( testAddr>0
212b0 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d || pParse->db->m
212b1 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 allocFailed );.
212b2 20 7d 0a 0a 20 20 73 77 69 74 63 68 28 20 70 45 }.. switch( pE
212b3 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63 xpr->op ){. c
212b4 61 73 65 20 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20 ase TK_IN: {.
212b5 20 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 char affinity
212b6 3b 0a 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 ;. KeyInfo
212b7 6b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 69 keyInfo;. i
212b8 6e 74 20 61 64 64 72 3b 20 20 20 20 20 20 20 20 nt addr;
212b9 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 4f 50 /* Address of OP
212ba 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 69 _OpenEphemeral i
212bb 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 nstruction */.
212bc 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74 20 Expr *pLeft
212bd 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a = pExpr->pLeft;.
212be 0a 20 20 20 20 20 20 69 66 28 20 72 4d 61 79 48 . if( rMayH
212bf 61 76 65 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 aveNull ){.
212c0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
212c1 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c dOp2(v, OP_Null,
212c2 20 30 2c 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 0, rMayHaveNull
212c3 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 );. }..
212c4 20 20 61 66 66 69 6e 69 74 79 20 3d 20 73 71 6c affinity = sql
212c5 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 ite3ExprAffinity
212c6 28 70 4c 65 66 74 29 3b 0a 0a 20 20 20 20 20 20 (pLeft);..
212c7 2f 2a 20 57 68 65 74 68 65 72 20 74 68 69 73 20 /* Whether this
212c8 69 73 20 61 6e 20 27 78 20 49 4e 28 53 45 4c 45 is an 'x IN(SELE
212c9 43 54 2e 2e 2e 29 27 20 6f 72 20 61 6e 20 27 78 CT...)' or an 'x
212ca 20 49 4e 28 3c 65 78 70 72 6c 69 73 74 3e 29 27 IN(<exprlist>)'
212cb 0a 20 20 20 20 20 20 2a 2a 20 65 78 70 72 65 73 . ** expres
212cc 73 69 6f 6e 20 69 74 20 69 73 20 68 61 6e 64 6c sion it is handl
212cd 65 64 20 74 68 65 20 73 61 6d 65 20 77 61 79 2e ed the same way.
212ce 20 41 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 A virtual table
212cf 20 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 66 69 is . ** fi
212d0 6c 6c 65 64 20 77 69 74 68 20 73 69 6e 67 6c 65 lled with single
212d1 2d 66 69 65 6c 64 20 69 6e 64 65 78 20 6b 65 79 -field index key
212d2 73 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 s representing t
212d3 68 65 20 72 65 73 75 6c 74 73 0a 20 20 20 20 20 he results.
212d4 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 53 45 4c ** from the SEL
212d5 45 43 54 20 6f 72 20 74 68 65 20 3c 65 78 70 72 ECT or the <expr
212d6 6c 69 73 74 3e 2e 0a 20 20 20 20 20 20 2a 2a 0a list>.. **.
212d7 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 ** If the
212d8 27 78 27 20 65 78 70 72 65 73 73 69 6f 6e 20 69 'x' expression i
212d9 73 20 61 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 s a column value
212da 2c 20 6f 72 20 74 68 65 20 53 45 4c 45 43 54 2e , or the SELECT.
212db 2e 2e 0a 20 20 20 20 20 20 2a 2a 20 73 74 61 74 ... ** stat
212dc 65 6d 65 6e 74 20 72 65 74 75 72 6e 73 20 61 20 ement returns a
212dd 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 2c 20 74 68 column value, th
212de 65 6e 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 en the affinity
212df 6f 66 20 74 68 61 74 0a 20 20 20 20 20 20 2a 2a of that. **
212e0 20 63 6f 6c 75 6d 6e 20 69 73 20 75 73 65 64 20 column is used
212e1 74 6f 20 62 75 69 6c 64 20 74 68 65 20 69 6e 64 to build the ind
212e2 65 78 20 6b 65 79 73 2e 20 49 66 20 62 6f 74 68 ex keys. If both
212e3 20 27 78 27 20 61 6e 64 20 74 68 65 0a 20 20 20 'x' and the.
212e4 20 20 20 2a 2a 20 53 45 4c 45 43 54 2e 2e 2e 20 ** SELECT...
212e5 73 74 61 74 65 6d 65 6e 74 20 61 72 65 20 63 6f statement are co
212e6 6c 75 6d 6e 73 2c 20 74 68 65 6e 20 6e 75 6d 65 lumns, then nume
212e7 72 69 63 20 61 66 66 69 6e 69 74 79 20 69 73 20 ric affinity is
212e8 75 73 65 64 0a 20 20 20 20 20 20 2a 2a 20 69 66 used. ** if
212e9 20 65 69 74 68 65 72 20 63 6f 6c 75 6d 6e 20 68 either column h
212ea 61 73 20 4e 55 4d 45 52 49 43 20 6f 72 20 49 4e as NUMERIC or IN
212eb 54 45 47 45 52 20 61 66 66 69 6e 69 74 79 2e 20 TEGER affinity.
212ec 49 66 20 6e 65 69 74 68 65 72 0a 20 20 20 20 20 If neither.
212ed 20 2a 2a 20 27 78 27 20 6e 6f 72 20 74 68 65 20 ** 'x' nor the
212ee 53 45 4c 45 43 54 2e 2e 2e 20 73 74 61 74 65 6d SELECT... statem
212ef 65 6e 74 20 61 72 65 20 63 6f 6c 75 6d 6e 73 2c ent are columns,
212f0 20 74 68 65 6e 20 6e 75 6d 65 72 69 63 20 61 66 then numeric af
212f1 66 69 6e 69 74 79 0a 20 20 20 20 20 20 2a 2a 20 finity. **
212f2 69 73 20 75 73 65 64 2e 0a 20 20 20 20 20 20 2a is used.. *
212f3 2f 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 /. pExpr->i
212f4 54 61 62 6c 65 20 3d 20 70 50 61 72 73 65 2d 3e Table = pParse->
212f5 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 61 64 nTab++;. ad
212f6 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 dr = sqlite3Vdbe
212f7 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 AddOp2(v, OP_Ope
212f8 6e 45 70 68 65 6d 65 72 61 6c 2c 20 70 45 78 70 nEphemeral, pExp
212f9 72 2d 3e 69 54 61 62 6c 65 2c 20 21 69 73 52 6f r->iTable, !isRo
212fa 77 69 64 29 3b 0a 20 20 20 20 20 20 6d 65 6d 73 wid);. mems
212fb 65 74 28 26 6b 65 79 49 6e 66 6f 2c 20 30 2c 20 et(&keyInfo, 0,
212fc 73 69 7a 65 6f 66 28 6b 65 79 49 6e 66 6f 29 29 sizeof(keyInfo))
212fd 3b 0a 20 20 20 20 20 20 6b 65 79 49 6e 66 6f 2e ;. keyInfo.
212fe 6e 46 69 65 6c 64 20 3d 20 31 3b 0a 0a 20 20 20 nField = 1;..
212ff 20 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 if( ExprHasPr
21300 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 operty(pExpr, EP
21301 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 _xIsSelect) ){.
21302 20 20 20 20 20 20 20 2f 2a 20 43 61 73 65 20 31 /* Case 1
21303 3a 20 20 20 20 20 65 78 70 72 20 49 4e 20 28 53 : expr IN (S
21304 45 4c 45 43 54 20 2e 2e 2e 29 0a 20 20 20 20 20 ELECT ...).
21305 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a **. **
21306 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 Generate code t
21307 6f 20 77 72 69 74 65 20 74 68 65 20 72 65 73 75 o write the resu
21308 6c 74 73 20 6f 66 20 74 68 65 20 73 65 6c 65 63 lts of the selec
21309 74 20 69 6e 74 6f 20 74 68 65 20 74 65 6d 70 6f t into the tempo
2130a 72 61 72 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 rary. **
2130b 74 61 62 6c 65 20 61 6c 6c 6f 63 61 74 65 64 20 table allocated
2130c 61 6e 64 20 6f 70 65 6e 65 64 20 61 62 6f 76 65 and opened above
2130d 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 .. */.
2130e 20 20 20 20 20 53 65 6c 65 63 74 44 65 73 74 20 SelectDest
2130f 64 65 73 74 3b 0a 20 20 20 20 20 20 20 20 45 78 dest;. Ex
21310 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a prList *pEList;.
21311 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
21312 20 21 69 73 52 6f 77 69 64 20 29 3b 0a 20 20 20 !isRowid );.
21313 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 sqlite3Sele
21314 63 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 74 ctDestInit(&dest
21315 2c 20 53 52 54 5f 53 65 74 2c 20 70 45 78 70 72 , SRT_Set, pExpr
21316 2d 3e 69 54 61 62 6c 65 29 3b 0a 20 20 20 20 20 ->iTable);.
21317 20 20 20 64 65 73 74 2e 61 66 66 69 6e 69 74 79 dest.affinity
21318 20 3d 20 28 75 38 29 61 66 66 69 6e 69 74 79 3b = (u8)affinity;
21319 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
2131a 20 28 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 26 (pExpr->iTable&
2131b 30 78 30 30 30 30 46 46 46 46 29 3d 3d 70 45 78 0x0000FFFF)==pEx
2131c 70 72 2d 3e 69 54 61 62 6c 65 20 29 3b 0a 20 20 pr->iTable );.
2131d 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
2131e 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 3Select(pParse,
2131f 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 pExpr->x.pSelect
21320 2c 20 26 64 65 73 74 29 20 29 7b 0a 20 20 20 20 , &dest) ){.
21321 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a return 0;.
21322 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
21323 20 20 70 45 4c 69 73 74 20 3d 20 70 45 78 70 72 pEList = pExpr
21324 2d 3e 78 2e 70 53 65 6c 65 63 74 2d 3e 70 45 4c ->x.pSelect->pEL
21325 69 73 74 3b 0a 20 20 20 20 20 20 20 20 69 66 28 ist;. if(
21326 20 41 4c 57 41 59 53 28 70 45 4c 69 73 74 21 3d ALWAYS(pEList!=
21327 30 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78 0 && pEList->nEx
21328 70 72 3e 30 29 20 29 7b 20 0a 20 20 20 20 20 20 pr>0) ){ .
21329 20 20 20 20 6b 65 79 49 6e 66 6f 2e 61 43 6f 6c keyInfo.aCol
2132a 6c 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33 42 69 l[0] = sqlite3Bi
2132b 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 naryCompareCollS
2132c 65 71 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 eq(pParse, pExpr
2132d 2d 3e 70 4c 65 66 74 2c 0a 20 20 20 20 20 20 20 ->pLeft,.
2132e 20 20 20 20 20 20 20 70 45 4c 69 73 74 2d 3e 61 pEList->a
2132f 5b 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 [0].pExpr);.
21330 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 }. }els
21331 65 20 69 66 28 20 70 45 78 70 72 2d 3e 78 2e 70 e if( pExpr->x.p
21332 4c 69 73 74 21 3d 30 20 29 7b 0a 20 20 20 20 20 List!=0 ){.
21333 20 20 20 2f 2a 20 43 61 73 65 20 32 3a 20 20 20 /* Case 2:
21334 20 20 65 78 70 72 20 49 4e 20 28 65 78 70 72 6c expr IN (exprl
21335 69 73 74 29 0a 20 20 20 20 20 20 20 20 2a 2a 0a ist). **.
21336 20 20 20 20 20 20 20 20 2a 2a 20 46 6f 72 20 65 ** For e
21337 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 ach expression,
21338 62 75 69 6c 64 20 61 6e 20 69 6e 64 65 78 20 6b build an index k
21339 65 79 20 66 72 6f 6d 20 74 68 65 20 65 76 61 6c ey from the eval
2133a 75 61 74 69 6f 6e 20 61 6e 64 0a 20 20 20 20 20 uation and.
2133b 20 20 20 2a 2a 20 73 74 6f 72 65 20 69 74 20 69 ** store it i
2133c 6e 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 n the temporary
2133d 74 61 62 6c 65 2e 20 49 66 20 3c 65 78 70 72 3e table. If <expr>
2133e 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2c 20 74 68 is a column, th
2133f 65 6e 20 75 73 65 0a 20 20 20 20 20 20 20 20 2a en use. *
21340 2a 20 74 68 61 74 20 63 6f 6c 75 6d 6e 73 20 61 * that columns a
21341 66 66 69 6e 69 74 79 20 77 68 65 6e 20 62 75 69 ffinity when bui
21342 6c 64 69 6e 67 20 69 6e 64 65 78 20 6b 65 79 73 lding index keys
21343 2e 20 49 66 20 3c 65 78 70 72 3e 20 69 73 20 6e . If <expr> is n
21344 6f 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 20 ot. ** a
21345 63 6f 6c 75 6d 6e 2c 20 75 73 65 20 6e 75 6d 65 column, use nume
21346 72 69 63 20 61 66 66 69 6e 69 74 79 2e 0a 20 20 ric affinity..
21347 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
21348 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 20 int i;.
21349 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 ExprList *pList
2134a 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 = pExpr->x.pList
2134b 3b 0a 20 20 20 20 20 20 20 20 73 74 72 75 63 74 ;. struct
2134c 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a ExprList_item *
2134d 70 49 74 65 6d 3b 0a 20 20 20 20 20 20 20 20 69 pItem;. i
2134e 6e 74 20 72 31 2c 20 72 32 2c 20 72 33 3b 0a 0a nt r1, r2, r3;..
2134f 20 20 20 20 20 20 20 20 69 66 28 20 21 61 66 66 if( !aff
21350 69 6e 69 74 79 20 29 7b 0a 20 20 20 20 20 20 20 inity ){.
21351 20 20 20 61 66 66 69 6e 69 74 79 20 3d 20 53 51 affinity = SQ
21352 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 LITE_AFF_NONE;.
21353 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
21354 20 6b 65 79 49 6e 66 6f 2e 61 43 6f 6c 6c 5b 30 keyInfo.aColl[0
21355 5d 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 ] = sqlite3ExprC
21356 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 ollSeq(pParse, p
21357 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 0a 20 Expr->pLeft);..
21358 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 74 /* Loop t
21359 68 72 6f 75 67 68 20 65 61 63 68 20 65 78 70 72 hrough each expr
2135a 65 73 73 69 6f 6e 20 69 6e 20 3c 65 78 70 72 6c ession in <exprl
2135b 69 73 74 3e 2e 20 2a 2f 0a 20 20 20 20 20 20 20 ist>. */.
2135c 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 r1 = sqlite3Get
2135d 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b TempReg(pParse);
2135e 0a 20 20 20 20 20 20 20 20 72 32 20 3d 20 73 71 . r2 = sq
2135f 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 lite3GetTempReg(
21360 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 pParse);.
21361 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21362 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 p2(v, OP_Null, 0
21363 2c 20 72 32 29 3b 0a 20 20 20 20 20 20 20 20 66 , r2);. f
21364 6f 72 28 69 3d 70 4c 69 73 74 2d 3e 6e 45 78 70 or(i=pList->nExp
21365 72 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e r, pItem=pList->
21366 61 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 a; i>0; i--, pIt
21367 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 em++){.
21368 20 45 78 70 72 20 2a 70 45 32 20 3d 20 70 49 74 Expr *pE2 = pIt
21369 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 em->pExpr;.
2136a 20 20 20 20 20 69 6e 74 20 69 56 61 6c 54 6f 49 int iValToI
2136b 6e 73 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 2f ns;.. /
2136c 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 * If the express
2136d 69 6f 6e 20 69 73 20 6e 6f 74 20 63 6f 6e 73 74 ion is not const
2136e 61 6e 74 20 74 68 65 6e 20 77 65 20 77 69 6c 6c ant then we will
2136f 20 6e 65 65 64 20 74 6f 0a 20 20 20 20 20 20 20 need to.
21370 20 20 20 2a 2a 20 64 69 73 61 62 6c 65 20 74 68 ** disable th
21371 65 20 74 65 73 74 20 74 68 61 74 20 77 61 73 20 e test that was
21372 67 65 6e 65 72 61 74 65 64 20 61 62 6f 76 65 20 generated above
21373 74 68 61 74 20 6d 61 6b 65 73 20 73 75 72 65 0a that makes sure.
21374 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 69 ** thi
21375 73 20 63 6f 64 65 20 6f 6e 6c 79 20 65 78 65 63 s code only exec
21376 75 74 65 73 20 6f 6e 63 65 2e 20 20 42 65 63 61 utes once. Beca
21377 75 73 65 20 66 6f 72 20 61 20 6e 6f 6e 2d 63 6f use for a non-co
21378 6e 73 74 61 6e 74 0a 20 20 20 20 20 20 20 20 20 nstant.
21379 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 77 ** expression w
2137a 65 20 6e 65 65 64 20 74 6f 20 72 65 72 75 6e 20 e need to rerun
2137b 74 68 69 73 20 63 6f 64 65 20 65 61 63 68 20 74 this code each t
2137c 69 6d 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a ime.. *
2137d 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 /. if(
2137e 74 65 73 74 41 64 64 72 20 26 26 20 21 73 71 6c testAddr && !sql
2137f 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 ite3ExprIsConsta
21380 6e 74 28 70 45 32 29 20 29 7b 0a 20 20 20 20 20 nt(pE2) ){.
21381 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
21382 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 beChangeToNoop(v
21383 2c 20 74 65 73 74 41 64 64 72 2d 31 2c 20 32 29 , testAddr-1, 2)
21384 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 74 65 ;. te
21385 73 74 41 64 64 72 20 3d 20 30 3b 0a 20 20 20 20 stAddr = 0;.
21386 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 }..
21387 20 20 20 2f 2a 20 45 76 61 6c 75 61 74 65 20 74 /* Evaluate t
21388 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e he expression an
21389 64 20 69 6e 73 65 72 74 20 69 74 20 69 6e 74 6f d insert it into
2138a 20 74 68 65 20 74 65 6d 70 20 74 61 62 6c 65 20 the temp table
2138b 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 */. if(
2138c 20 69 73 52 6f 77 69 64 20 26 26 20 73 71 6c 69 isRowid && sqli
2138d 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 te3ExprIsInteger
2138e 28 70 45 32 2c 20 26 69 56 61 6c 54 6f 49 6e 73 (pE2, &iValToIns
2138f 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 ) ){.
21390 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21391 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 49 p3(v, OP_InsertI
21392 6e 74 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c nt, pExpr->iTabl
21393 65 2c 20 72 32 2c 20 69 56 61 6c 54 6f 49 6e 73 e, r2, iValToIns
21394 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c );. }el
21395 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 se{.
21396 72 33 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 r3 = sqlite3Expr
21397 43 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 73 CodeTarget(pPars
21398 65 2c 20 70 45 32 2c 20 72 31 29 3b 0a 20 20 20 e, pE2, r1);.
21399 20 20 20 20 20 20 20 20 20 69 66 28 20 69 73 52 if( isR
2139a 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20 owid ){.
2139b 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
2139c 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4d 75 eAddOp2(v, OP_Mu
2139d 73 74 42 65 49 6e 74 2c 20 72 33 2c 0a 20 20 20 stBeInt, r3,.
2139e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2139f 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c sql
213a0 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 ite3VdbeCurrentA
213a1 64 64 72 28 76 29 2b 32 29 3b 0a 20 20 20 20 20 ddr(v)+2);.
213a2 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
213a3 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 VdbeAddOp3(v, OP
213a4 5f 49 6e 73 65 72 74 2c 20 70 45 78 70 72 2d 3e _Insert, pExpr->
213a5 69 54 61 62 6c 65 2c 20 72 32 2c 20 72 33 29 3b iTable, r2, r3);
213a6 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c . }el
213a7 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 se{.
213a8 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
213a9 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 Op4(v, OP_MakeRe
213aa 63 6f 72 64 2c 20 72 33 2c 20 31 2c 20 72 32 2c cord, r3, 1, r2,
213ab 20 26 61 66 66 69 6e 69 74 79 2c 20 31 29 3b 0a &affinity, 1);.
213ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 sq
213ad 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66 lite3ExprCacheAf
213ae 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 finityChange(pPa
213af 72 73 65 2c 20 72 33 2c 20 31 29 3b 0a 20 20 20 rse, r3, 1);.
213b0 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
213b1 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
213b2 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 70 45 OP_IdxInsert, pE
213b3 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 72 32 29 xpr->iTable, r2)
213b4 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a ;. }.
213b5 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
213b6 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 }. sq
213b7 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 lite3ReleaseTemp
213b8 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b Reg(pParse, r1);
213b9 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
213ba 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 ReleaseTempReg(p
213bb 50 61 72 73 65 2c 20 72 32 29 3b 0a 20 20 20 20 Parse, r2);.
213bc 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21 69 }. if( !i
213bd 73 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 sRowid ){.
213be 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 sqlite3VdbeCha
213bf 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2c 20 28 ngeP4(v, addr, (
213c0 76 6f 69 64 20 2a 29 26 6b 65 79 49 6e 66 6f 2c void *)&keyInfo,
213c1 20 50 34 5f 4b 45 59 49 4e 46 4f 29 3b 0a 20 20 P4_KEYINFO);.
213c2 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 }. brea
213c3 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63 61 k;. }.. ca
213c4 73 65 20 54 4b 5f 45 58 49 53 54 53 3a 0a 20 20 se TK_EXISTS:.
213c5 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43 54 case TK_SELECT
213c6 3a 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b :. default: {
213c7 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69 . /* If thi
213c8 73 20 68 61 73 20 74 6f 20 62 65 20 61 20 73 63 s has to be a sc
213c9 61 6c 61 72 20 53 45 4c 45 43 54 2e 20 20 47 65 alar SELECT. Ge
213ca 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 70 nerate code to p
213cb 75 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 ut the. **
213cc 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 73 65 value of this se
213cd 6c 65 63 74 20 69 6e 20 61 20 6d 65 6d 6f 72 79 lect in a memory
213ce 20 63 65 6c 6c 20 61 6e 64 20 72 65 63 6f 72 64 cell and record
213cf 20 74 68 65 20 6e 75 6d 62 65 72 0a 20 20 20 20 the number.
213d0 20 20 2a 2a 20 6f 66 20 74 68 65 20 6d 65 6d 6f ** of the memo
213d1 72 79 20 63 65 6c 6c 20 69 6e 20 69 43 6f 6c 75 ry cell in iColu
213d2 6d 6e 2e 20 20 49 66 20 74 68 69 73 20 69 73 20 mn. If this is
213d3 61 6e 20 45 58 49 53 54 53 2c 20 77 72 69 74 65 an EXISTS, write
213d4 0a 20 20 20 20 20 20 2a 2a 20 61 6e 20 69 6e 74 . ** an int
213d5 65 67 65 72 20 30 20 28 6e 6f 74 20 65 78 69 73 eger 0 (not exis
213d6 74 73 29 20 6f 72 20 31 20 28 65 78 69 73 74 73 ts) or 1 (exists
213d7 29 20 69 6e 74 6f 20 61 20 6d 65 6d 6f 72 79 20 ) into a memory
213d8 63 65 6c 6c 0a 20 20 20 20 20 20 2a 2a 20 61 6e cell. ** an
213d9 64 20 72 65 63 6f 72 64 20 74 68 61 74 20 6d 65 d record that me
213da 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 69 43 6f mory cell in iCo
213db 6c 75 6d 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 lumn.. */.
213dc 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 static cons
213dd 74 20 54 6f 6b 65 6e 20 6f 6e 65 20 3d 20 7b 20 t Token one = {
213de 22 31 22 2c 20 31 20 7d 3b 20 20 2f 2a 20 54 6f "1", 1 }; /* To
213df 6b 65 6e 20 66 6f 72 20 6c 69 74 65 72 61 6c 20 ken for literal
213e0 76 61 6c 75 65 20 31 20 2a 2f 0a 20 20 20 20 20 value 1 */.
213e1 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 3b 20 20 Select *pSel;
213e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
213e3 20 20 20 20 20 20 20 2f 2a 20 53 45 4c 45 43 54 /* SELECT
213e4 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 65 6e statement to en
213e5 63 6f 64 65 20 2a 2f 0a 20 20 20 20 20 20 53 65 code */. Se
213e6 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b 20 20 lectDest dest;
213e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
213e8 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 64 65 /* How to de
213e9 61 6c 20 77 69 74 68 20 53 45 4c 45 43 74 20 72 al with SELECt r
213ea 65 73 75 6c 74 20 2a 2f 0a 0a 20 20 20 20 20 20 esult */..
213eb 74 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d testcase( pExpr-
213ec 3e 6f 70 3d 3d 54 4b 5f 45 58 49 53 54 53 20 29 >op==TK_EXISTS )
213ed 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
213ee 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f ( pExpr->op==TK_
213ef 53 45 4c 45 43 54 20 29 3b 0a 20 20 20 20 20 20 SELECT );.
213f0 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f assert( pExpr->o
213f1 70 3d 3d 54 4b 5f 45 58 49 53 54 53 20 7c 7c 20 p==TK_EXISTS ||
213f2 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 pExpr->op==TK_SE
213f3 4c 45 43 54 20 29 3b 0a 0a 20 20 20 20 20 20 61 LECT );.. a
213f4 73 73 65 72 74 28 20 45 78 70 72 48 61 73 50 72 ssert( ExprHasPr
213f5 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 operty(pExpr, EP
213f6 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 _xIsSelect) );.
213f7 20 20 20 20 20 70 53 65 6c 20 3d 20 70 45 78 70 pSel = pExp
213f8 72 2d 3e 78 2e 70 53 65 6c 65 63 74 3b 0a 20 20 r->x.pSelect;.
213f9 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 sqlite3Selec
213fa 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 74 2c tDestInit(&dest,
213fb 20 30 2c 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 0, ++pParse->nM
213fc 65 6d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 em);. if( p
213fd 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 4c Expr->op==TK_SEL
213fe 45 43 54 20 29 7b 0a 20 20 20 20 20 20 20 20 64 ECT ){. d
213ff 65 73 74 2e 65 44 65 73 74 20 3d 20 53 52 54 5f est.eDest = SRT_
21400 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 73 71 6c Mem;. sql
21401 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
21402 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 64 65 , OP_Null, 0, de
21403 73 74 2e 69 50 61 72 6d 29 3b 0a 20 20 20 20 20 st.iParm);.
21404 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 VdbeComment((
21405 76 2c 20 22 49 6e 69 74 20 73 75 62 71 75 65 72 v, "Init subquer
21406 79 20 72 65 73 75 6c 74 22 29 29 3b 0a 20 20 20 y result"));.
21407 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
21408 20 20 64 65 73 74 2e 65 44 65 73 74 20 3d 20 53 dest.eDest = S
21409 52 54 5f 45 78 69 73 74 73 3b 0a 20 20 20 20 20 RT_Exists;.
2140a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
2140b 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 dOp2(v, OP_Integ
2140c 65 72 2c 20 30 2c 20 64 65 73 74 2e 69 50 61 72 er, 0, dest.iPar
2140d 6d 29 3b 0a 20 20 20 20 20 20 20 20 56 64 62 65 m);. Vdbe
2140e 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 49 6e 69 Comment((v, "Ini
2140f 74 20 45 58 49 53 54 53 20 72 65 73 75 6c 74 22 t EXISTS result"
21410 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ));. }.
21411 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c sqlite3ExprDel
21412 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 ete(pParse->db,
21413 70 53 65 6c 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20 pSel->pLimit);.
21414 20 20 20 20 20 70 53 65 6c 2d 3e 70 4c 69 6d 69 pSel->pLimi
21415 74 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 t = sqlite3PExpr
21416 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e 54 45 (pParse, TK_INTE
21417 47 45 52 2c 20 30 2c 20 30 2c 20 26 6f 6e 65 29 GER, 0, 0, &one)
21418 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 ;. if( sqli
21419 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 te3Select(pParse
2141a 2c 20 70 53 65 6c 2c 20 26 64 65 73 74 29 20 29 , pSel, &dest) )
2141b 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e {. return
2141c 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0;. }.
2141d 20 20 72 52 65 67 20 3d 20 64 65 73 74 2e 69 50 rReg = dest.iP
2141e 61 72 6d 3b 0a 20 20 20 20 20 20 45 78 70 72 53 arm;. ExprS
2141f 65 74 49 72 72 65 64 75 63 69 62 6c 65 28 70 45 etIrreducible(pE
21420 78 70 72 29 3b 0a 20 20 20 20 20 20 62 72 65 61 xpr);. brea
21421 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 k;. }. }..
21422 69 66 28 20 74 65 73 74 41 64 64 72 20 29 7b 0a if( testAddr ){.
21423 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a sqlite3VdbeJ
21424 75 6d 70 48 65 72 65 28 76 2c 20 74 65 73 74 41 umpHere(v, testA
21425 64 64 72 2d 31 29 3b 0a 20 20 7d 0a 20 20 73 71 ddr-1);. }. sq
21426 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 6f lite3ExprCachePo
21427 70 28 70 50 61 72 73 65 2c 20 31 29 3b 0a 0a 20 p(pParse, 1);..
21428 20 72 65 74 75 72 6e 20 72 52 65 67 3b 0a 7d 0a return rReg;.}.
21429 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
2142a 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 2a _OMIT_SUBQUERY *
2142b 2f 0a 0a 2f 2a 0a 2a 2a 20 44 75 70 6c 69 63 61 /../*.** Duplica
2142c 74 65 20 61 6e 20 38 2d 62 79 74 65 20 76 61 6c te an 8-byte val
2142d 75 65 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 ue.*/.static cha
2142e 72 20 2a 64 75 70 38 62 79 74 65 73 28 56 64 62 r *dup8bytes(Vdb
2142f 65 20 2a 76 2c 20 63 6f 6e 73 74 20 63 68 61 72 e *v, const char
21430 20 2a 69 6e 29 7b 0a 20 20 63 68 61 72 20 2a 6f *in){. char *o
21431 75 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 ut = sqlite3DbMa
21432 6c 6c 6f 63 52 61 77 28 73 71 6c 69 74 65 33 56 llocRaw(sqlite3V
21433 64 62 65 44 62 28 76 29 2c 20 38 29 3b 0a 20 20 dbeDb(v), 8);.
21434 69 66 28 20 6f 75 74 20 29 7b 0a 20 20 20 20 6d if( out ){. m
21435 65 6d 63 70 79 28 6f 75 74 2c 20 69 6e 2c 20 38 emcpy(out, in, 8
21436 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
21437 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 out;.}../*.** Ge
21438 6e 65 72 61 74 65 20 61 6e 20 69 6e 73 74 72 75 nerate an instru
21439 63 74 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 ction that will
2143a 70 75 74 20 74 68 65 20 66 6c 6f 61 74 69 6e 67 put the floating
2143b 20 70 6f 69 6e 74 0a 2a 2a 20 76 61 6c 75 65 20 point.** value
2143c 64 65 73 63 72 69 62 65 64 20 62 79 20 7a 5b 30 described by z[0
2143d 2e 2e 6e 2d 31 5d 20 69 6e 74 6f 20 72 65 67 69 ..n-1] into regi
2143e 73 74 65 72 20 69 4d 65 6d 2e 0a 2a 2a 0a 2a 2a ster iMem..**.**
2143f 20 54 68 65 20 7a 5b 5d 20 73 74 72 69 6e 67 20 The z[] string
21440 77 69 6c 6c 20 70 72 6f 62 61 62 6c 79 20 6e 6f will probably no
21441 74 20 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e t be zero-termin
21442 61 74 65 64 2e 20 20 42 75 74 20 74 68 65 20 0a ated. But the .
21443 2a 2a 20 7a 5b 6e 5d 20 63 68 61 72 61 63 74 65 ** z[n] characte
21444 72 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 r is guaranteed
21445 74 6f 20 62 65 20 73 6f 6d 65 74 68 69 6e 67 20 to be something
21446 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6c 6f that does not lo
21447 6f 6b 0a 2a 2a 20 6c 69 6b 65 20 74 68 65 20 63 ok.** like the c
21448 6f 6e 74 69 6e 75 61 74 69 6f 6e 20 6f 66 20 74 ontinuation of t
21449 68 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 he number..*/.st
2144a 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65 52 65 atic void codeRe
2144b 61 6c 28 56 64 62 65 20 2a 76 2c 20 63 6f 6e 73 al(Vdbe *v, cons
2144c 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e t char *z, int n
2144d 65 67 61 74 65 46 6c 61 67 2c 20 69 6e 74 20 69 egateFlag, int i
2144e 4d 65 6d 29 7b 0a 20 20 69 66 28 20 41 4c 57 41 Mem){. if( ALWA
2144f 59 53 28 7a 21 3d 30 29 20 29 7b 0a 20 20 20 20 YS(z!=0) ){.
21450 64 6f 75 62 6c 65 20 76 61 6c 75 65 3b 0a 20 20 double value;.
21451 20 20 63 68 61 72 20 2a 7a 56 3b 0a 20 20 20 20 char *zV;.
21452 73 71 6c 69 74 65 33 41 74 6f 46 28 7a 2c 20 26 sqlite3AtoF(z, &
21453 76 61 6c 75 65 29 3b 0a 20 20 20 20 61 73 73 65 value);. asse
21454 72 74 28 20 21 73 71 6c 69 74 65 33 49 73 4e 61 rt( !sqlite3IsNa
21455 4e 28 76 61 6c 75 65 29 20 29 3b 20 2f 2a 20 54 N(value) ); /* T
21456 68 65 20 6e 65 77 20 41 74 6f 46 20 6e 65 76 65 he new AtoF neve
21457 72 20 72 65 74 75 72 6e 73 20 4e 61 4e 20 2a 2f r returns NaN */
21458 0a 20 20 20 20 69 66 28 20 6e 65 67 61 74 65 46 . if( negateF
21459 6c 61 67 20 29 20 76 61 6c 75 65 20 3d 20 2d 76 lag ) value = -v
2145a 61 6c 75 65 3b 0a 20 20 20 20 7a 56 20 3d 20 64 alue;. zV = d
2145b 75 70 38 62 79 74 65 73 28 76 2c 20 28 63 68 61 up8bytes(v, (cha
2145c 72 2a 29 26 76 61 6c 75 65 29 3b 0a 20 20 20 20 r*)&value);.
2145d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
2145e 34 28 76 2c 20 4f 50 5f 52 65 61 6c 2c 20 30 2c 4(v, OP_Real, 0,
2145f 20 69 4d 65 6d 2c 20 30 2c 20 7a 56 2c 20 50 34 iMem, 0, zV, P4
21460 5f 52 45 41 4c 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a _REAL);. }.}...
21461 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 /*.** Generate a
21462 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 n instruction th
21463 61 74 20 77 69 6c 6c 20 70 75 74 20 74 68 65 20 at will put the
21464 69 6e 74 65 67 65 72 20 64 65 73 63 72 69 62 65 integer describe
21465 20 62 79 0a 2a 2a 20 74 65 78 74 20 7a 5b 30 2e by.** text z[0.
21466 2e 6e 2d 31 5d 20 69 6e 74 6f 20 72 65 67 69 73 .n-1] into regis
21467 74 65 72 20 69 4d 65 6d 2e 0a 2a 2a 0a 2a 2a 20 ter iMem..**.**
21468 54 68 65 20 7a 5b 5d 20 73 74 72 69 6e 67 20 77 The z[] string w
21469 69 6c 6c 20 70 72 6f 62 61 62 6c 79 20 6e 6f 74 ill probably not
2146a 20 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 be zero-termina
2146b 74 65 64 2e 20 20 42 75 74 20 74 68 65 20 0a 2a ted. But the .*
2146c 2a 20 7a 5b 6e 5d 20 63 68 61 72 61 63 74 65 72 * z[n] character
2146d 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 is guaranteed t
2146e 6f 20 62 65 20 73 6f 6d 65 74 68 69 6e 67 20 74 o be something t
2146f 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6c 6f 6f hat does not loo
21470 6b 0a 2a 2a 20 6c 69 6b 65 20 74 68 65 20 63 6f k.** like the co
21471 6e 74 69 6e 75 61 74 69 6f 6e 20 6f 66 20 74 68 ntinuation of th
21472 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 e number..*/.sta
21473 74 69 63 20 76 6f 69 64 20 63 6f 64 65 49 6e 74 tic void codeInt
21474 65 67 65 72 28 56 64 62 65 20 2a 76 2c 20 45 78 eger(Vdbe *v, Ex
21475 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 6e pr *pExpr, int n
21476 65 67 46 6c 61 67 2c 20 69 6e 74 20 69 4d 65 6d egFlag, int iMem
21477 29 7b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e ){. if( pExpr->
21478 66 6c 61 67 73 20 26 20 45 50 5f 49 6e 74 56 61 flags & EP_IntVa
21479 6c 75 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 lue ){. int i
2147a 20 3d 20 70 45 78 70 72 2d 3e 75 2e 69 56 61 6c = pExpr->u.iVal
2147b 75 65 3b 0a 20 20 20 20 69 66 28 20 6e 65 67 46 ue;. if( negF
2147c 6c 61 67 20 29 20 69 20 3d 20 2d 69 3b 0a 20 20 lag ) i = -i;.
2147d 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
2147e 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 Op2(v, OP_Intege
2147f 72 2c 20 69 2c 20 69 4d 65 6d 29 3b 0a 20 20 7d r, i, iMem);. }
21480 65 6c 73 65 7b 0a 20 20 20 20 63 6f 6e 73 74 20 else{. const
21481 63 68 61 72 20 2a 7a 20 3d 20 70 45 78 70 72 2d char *z = pExpr-
21482 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 61 >u.zToken;. a
21483 73 73 65 72 74 28 20 7a 21 3d 30 20 29 3b 0a 20 ssert( z!=0 );.
21484 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 if( sqlite3Fi
21485 74 73 49 6e 36 34 42 69 74 73 28 7a 2c 20 6e 65 tsIn64Bits(z, ne
21486 67 46 6c 61 67 29 20 29 7b 0a 20 20 20 20 20 20 gFlag) ){.
21487 69 36 34 20 76 61 6c 75 65 3b 0a 20 20 20 20 20 i64 value;.
21488 20 63 68 61 72 20 2a 7a 56 3b 0a 20 20 20 20 20 char *zV;.
21489 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 7a sqlite3Atoi64(z
2148a 2c 20 26 76 61 6c 75 65 29 3b 0a 20 20 20 20 20 , &value);.
2148b 20 69 66 28 20 6e 65 67 46 6c 61 67 20 29 20 76 if( negFlag ) v
2148c 61 6c 75 65 20 3d 20 2d 76 61 6c 75 65 3b 0a 20 alue = -value;.
2148d 20 20 20 20 20 7a 56 20 3d 20 64 75 70 38 62 79 zV = dup8by
2148e 74 65 73 28 76 2c 20 28 63 68 61 72 2a 29 26 76 tes(v, (char*)&v
2148f 61 6c 75 65 29 3b 0a 20 20 20 20 20 20 73 71 6c alue);. sql
21490 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 ite3VdbeAddOp4(v
21491 2c 20 4f 50 5f 49 6e 74 36 34 2c 20 30 2c 20 69 , OP_Int64, 0, i
21492 4d 65 6d 2c 20 30 2c 20 7a 56 2c 20 50 34 5f 49 Mem, 0, zV, P4_I
21493 4e 54 36 34 29 3b 0a 20 20 20 20 7d 65 6c 73 65 NT64);. }else
21494 7b 0a 20 20 20 20 20 20 63 6f 64 65 52 65 61 6c {. codeReal
21495 28 76 2c 20 7a 2c 20 6e 65 67 46 6c 61 67 2c 20 (v, z, negFlag,
21496 69 4d 65 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d iMem);. }. }
21497 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 .}../*.** Clear
21498 61 20 63 61 63 68 65 20 65 6e 74 72 79 2e 0a 2a a cache entry..*
21499 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 61 /.static void ca
2149a 63 68 65 45 6e 74 72 79 43 6c 65 61 72 28 50 61 cheEntryClear(Pa
2149b 72 73 65 20 2a 70 50 61 72 73 65 2c 20 73 74 72 rse *pParse, str
2149c 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 uct yColCache *p
2149d 29 7b 0a 20 20 69 66 28 20 70 2d 3e 74 65 6d 70 ){. if( p->temp
2149e 52 65 67 20 29 7b 0a 20 20 20 20 69 66 28 20 70 Reg ){. if( p
2149f 50 61 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 3c Parse->nTempReg<
214a0 41 72 72 61 79 53 69 7a 65 28 70 50 61 72 73 65 ArraySize(pParse
214a1 2d 3e 61 54 65 6d 70 52 65 67 29 20 29 7b 0a 20 ->aTempReg) ){.
214a2 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61 54 65 pParse->aTe
214a3 6d 70 52 65 67 5b 70 50 61 72 73 65 2d 3e 6e 54 mpReg[pParse->nT
214a4 65 6d 70 52 65 67 2b 2b 5d 20 3d 20 70 2d 3e 69 empReg++] = p->i
214a5 52 65 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 Reg;. }. p
214a6 2d 3e 74 65 6d 70 52 65 67 20 3d 20 30 3b 0a 20 ->tempReg = 0;.
214a7 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 }.}.../*.** Rec
214a8 6f 72 64 20 69 6e 20 74 68 65 20 63 6f 6c 75 6d ord in the colum
214a9 6e 20 63 61 63 68 65 20 74 68 61 74 20 61 20 70 n cache that a p
214aa 61 72 74 69 63 75 6c 61 72 20 63 6f 6c 75 6d 6e articular column
214ab 20 66 72 6f 6d 20 61 0a 2a 2a 20 70 61 72 74 69 from a.** parti
214ac 63 75 6c 61 72 20 74 61 62 6c 65 20 69 73 20 73 cular table is s
214ad 74 6f 72 65 64 20 69 6e 20 61 20 70 61 72 74 69 tored in a parti
214ae 63 75 6c 61 72 20 72 65 67 69 73 74 65 72 2e 0a cular register..
214af 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
214b0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 E void sqlite3Ex
214b1 70 72 43 61 63 68 65 53 74 6f 72 65 28 50 61 72 prCacheStore(Par
214b2 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 se *pParse, int
214b3 69 54 61 62 2c 20 69 6e 74 20 69 43 6f 6c 2c 20 iTab, int iCol,
214b4 69 6e 74 20 69 52 65 67 29 7b 0a 20 20 69 6e 74 int iReg){. int
214b5 20 69 3b 0a 20 20 69 6e 74 20 6d 69 6e 4c 72 75 i;. int minLru
214b6 3b 0a 20 20 69 6e 74 20 69 64 78 4c 72 75 3b 0a ;. int idxLru;.
214b7 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 struct yColCac
214b8 68 65 20 2a 70 3b 0a 0a 20 20 61 73 73 65 72 74 he *p;.. assert
214b9 28 20 69 52 65 67 3e 30 20 29 3b 20 20 2f 2a 20 ( iReg>0 ); /*
214ba 52 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 73 Register numbers
214bb 20 61 72 65 20 61 6c 77 61 79 73 20 70 6f 73 69 are always posi
214bc 74 69 76 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 tive */. assert
214bd 28 20 69 43 6f 6c 3e 3d 2d 31 20 26 26 20 69 43 ( iCol>=-1 && iC
214be 6f 6c 3c 33 32 37 36 38 20 29 3b 20 20 2f 2a 20 ol<32768 ); /*
214bf 46 69 6e 69 74 65 20 63 6f 6c 75 6d 6e 20 6e 75 Finite column nu
214c0 6d 62 65 72 73 20 2a 2f 0a 0a 20 20 2f 2a 20 46 mbers */.. /* F
214c1 69 72 73 74 20 72 65 70 6c 61 63 65 20 61 6e 79 irst replace any
214c2 20 65 78 69 73 74 69 6e 67 20 65 6e 74 72 79 20 existing entry
214c3 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d */. for(i=0, p=
214c4 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 pParse->aColCach
214c5 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f e; i<SQLITE_N_CO
214c6 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b LCACHE; i++, p++
214c7 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 52 ){. if( p->iR
214c8 65 67 20 26 26 20 70 2d 3e 69 54 61 62 6c 65 3d eg && p->iTable=
214c9 3d 69 54 61 62 20 26 26 20 70 2d 3e 69 43 6f 6c =iTab && p->iCol
214ca 75 6d 6e 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20 20 umn==iCol ){.
214cb 20 20 20 63 61 63 68 65 45 6e 74 72 79 43 6c 65 cacheEntryCle
214cc 61 72 28 70 50 61 72 73 65 2c 20 70 29 3b 0a 20 ar(pParse, p);.
214cd 20 20 20 20 20 70 2d 3e 69 4c 65 76 65 6c 20 3d p->iLevel =
214ce 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c pParse->iCacheL
214cf 65 76 65 6c 3b 0a 20 20 20 20 20 20 70 2d 3e 69 evel;. p->i
214d0 52 65 67 20 3d 20 69 52 65 67 3b 0a 20 20 20 20 Reg = iReg;.
214d1 20 20 70 2d 3e 61 66 66 43 68 61 6e 67 65 20 3d p->affChange =
214d2 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 6c 72 75 0;. p->lru
214d3 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 = pParse->iCach
214d4 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 72 65 eCnt++;. re
214d5 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a turn;. }. }.
214d6 0a 20 20 2f 2a 20 46 69 6e 64 20 61 6e 20 65 6d . /* Find an em
214d7 70 74 79 20 73 6c 6f 74 20 61 6e 64 20 72 65 70 pty slot and rep
214d8 6c 61 63 65 20 69 74 20 2a 2f 0a 20 20 66 6f 72 lace it */. for
214d9 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e (i=0, p=pParse->
214da 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c aColCache; i<SQL
214db 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 ITE_N_COLCACHE;
214dc 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 i++, p++){. i
214dd 66 28 20 70 2d 3e 69 52 65 67 3d 3d 30 20 29 7b f( p->iReg==0 ){
214de 0a 20 20 20 20 20 20 70 2d 3e 69 4c 65 76 65 6c . p->iLevel
214df 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 = pParse->iCach
214e0 65 4c 65 76 65 6c 3b 0a 20 20 20 20 20 20 70 2d eLevel;. p-
214e1 3e 69 54 61 62 6c 65 20 3d 20 69 54 61 62 3b 0a >iTable = iTab;.
214e2 20 20 20 20 20 20 70 2d 3e 69 43 6f 6c 75 6d 6e p->iColumn
214e3 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20 70 = iCol;. p
214e4 2d 3e 69 52 65 67 20 3d 20 69 52 65 67 3b 0a 20 ->iReg = iReg;.
214e5 20 20 20 20 20 70 2d 3e 61 66 66 43 68 61 6e 67 p->affChang
214e6 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e e = 0;. p->
214e7 74 65 6d 70 52 65 67 20 3d 20 30 3b 0a 20 20 20 tempReg = 0;.
214e8 20 20 20 70 2d 3e 6c 72 75 20 3d 20 70 50 61 72 p->lru = pPar
214e9 73 65 2d 3e 69 43 61 63 68 65 43 6e 74 2b 2b 3b se->iCacheCnt++;
214ea 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 . return;.
214eb 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 }. }.. /* R
214ec 65 70 6c 61 63 65 20 74 68 65 20 6c 61 73 74 20 eplace the last
214ed 72 65 63 65 6e 74 6c 79 20 75 73 65 64 20 2a 2f recently used */
214ee 0a 20 20 6d 69 6e 4c 72 75 20 3d 20 30 78 37 66 . minLru = 0x7f
214ef 66 66 66 66 66 66 3b 0a 20 20 69 64 78 4c 72 75 ffffff;. idxLru
214f0 20 3d 20 2d 31 3b 0a 20 20 66 6f 72 28 69 3d 30 = -1;. for(i=0
214f1 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c , p=pParse->aCol
214f2 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f Cache; i<SQLITE_
214f3 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c N_COLCACHE; i++,
214f4 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 p++){. if( p
214f5 2d 3e 6c 72 75 3c 6d 69 6e 4c 72 75 20 29 7b 0a ->lru<minLru ){.
214f6 20 20 20 20 20 20 69 64 78 4c 72 75 20 3d 20 69 idxLru = i
214f7 3b 0a 20 20 20 20 20 20 6d 69 6e 4c 72 75 20 3d ;. minLru =
214f8 20 70 2d 3e 6c 72 75 3b 0a 20 20 20 20 7d 0a 20 p->lru;. }.
214f9 20 7d 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 }. if( ALWAYS(
214fa 69 64 78 4c 72 75 3e 3d 30 29 20 29 7b 0a 20 20 idxLru>=0) ){.
214fb 20 20 70 20 3d 20 26 70 50 61 72 73 65 2d 3e 61 p = &pParse->a
214fc 43 6f 6c 43 61 63 68 65 5b 69 64 78 4c 72 75 5d ColCache[idxLru]
214fd 3b 0a 20 20 20 20 70 2d 3e 69 4c 65 76 65 6c 20 ;. p->iLevel
214fe 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 = pParse->iCache
214ff 4c 65 76 65 6c 3b 0a 20 20 20 20 70 2d 3e 69 54 Level;. p->iT
21500 61 62 6c 65 20 3d 20 69 54 61 62 3b 0a 20 20 20 able = iTab;.
21501 20 70 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 69 43 p->iColumn = iC
21502 6f 6c 3b 0a 20 20 20 20 70 2d 3e 69 52 65 67 20 ol;. p->iReg
21503 3d 20 69 52 65 67 3b 0a 20 20 20 20 70 2d 3e 61 = iReg;. p->a
21504 66 66 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 ffChange = 0;.
21505 20 20 70 2d 3e 74 65 6d 70 52 65 67 20 3d 20 30 p->tempReg = 0
21506 3b 0a 20 20 20 20 70 2d 3e 6c 72 75 20 3d 20 70 ;. p->lru = p
21507 50 61 72 73 65 2d 3e 69 43 61 63 68 65 43 6e 74 Parse->iCacheCnt
21508 2b 2b 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a ++;. return;.
21509 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 64 }.}../*.** Ind
2150a 69 63 61 74 65 20 74 68 61 74 20 61 20 72 65 67 icate that a reg
2150b 69 73 74 65 72 20 69 73 20 62 65 69 6e 67 20 6f ister is being o
2150c 76 65 72 77 72 69 74 74 65 6e 2e 20 20 50 75 72 verwritten. Pur
2150d 67 65 20 74 68 65 20 72 65 67 69 73 74 65 72 0a ge the register.
2150e 2a 2a 20 66 72 6f 6d 20 74 68 65 20 63 6f 6c 75 ** from the colu
2150f 6d 6e 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c mn cache..*/.SQL
21510 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
21511 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 sqlite3ExprCach
21512 65 52 65 6d 6f 76 65 28 50 61 72 73 65 20 2a 70 eRemove(Parse *p
21513 50 61 72 73 65 2c 20 69 6e 74 20 69 52 65 67 29 Parse, int iReg)
21514 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 {. int i;. str
21515 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 uct yColCache *p
21516 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 ;. for(i=0, p=p
21517 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 Parse->aColCache
21518 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c ; i<SQLITE_N_COL
21519 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 CACHE; i++, p++)
2151a 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 52 65 {. if( p->iRe
2151b 67 3d 3d 69 52 65 67 20 29 7b 0a 20 20 20 20 20 g==iReg ){.
2151c 20 63 61 63 68 65 45 6e 74 72 79 43 6c 65 61 72 cacheEntryClear
2151d 28 70 50 61 72 73 65 2c 20 70 29 3b 0a 20 20 20 (pParse, p);.
2151e 20 20 20 70 2d 3e 69 52 65 67 20 3d 20 30 3b 0a p->iReg = 0;.
2151f 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
21520 2a 2a 20 52 65 6d 65 6d 62 65 72 20 74 68 65 20 ** Remember the
21521 63 75 72 72 65 6e 74 20 63 6f 6c 75 6d 6e 20 63 current column c
21522 61 63 68 65 20 63 6f 6e 74 65 78 74 2e 20 20 41 ache context. A
21523 6e 79 20 6e 65 77 20 65 6e 74 72 69 65 73 20 61 ny new entries a
21524 64 64 65 64 0a 2a 2a 20 61 64 64 65 64 20 74 6f dded.** added to
21525 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 the column cach
21526 65 20 61 66 74 65 72 20 74 68 69 73 20 63 61 6c e after this cal
21527 6c 20 61 72 65 20 72 65 6d 6f 76 65 64 20 77 68 l are removed wh
21528 65 6e 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73 en the.** corres
21529 70 6f 6e 64 69 6e 67 20 70 6f 70 20 6f 63 63 75 ponding pop occu
2152a 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 rs..*/.SQLITE_PR
2152b 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
2152c 65 33 45 78 70 72 43 61 63 68 65 50 75 73 68 28 e3ExprCachePush(
2152d 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a Parse *pParse){.
2152e 20 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 pParse->iCache
2152f 4c 65 76 65 6c 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a Level++;.}../*.*
21530 2a 20 52 65 6d 6f 76 65 20 66 72 6f 6d 20 74 68 * Remove from th
21531 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 61 e column cache a
21532 6e 79 20 65 6e 74 72 69 65 73 20 74 68 61 74 20 ny entries that
21533 77 65 72 65 20 61 64 64 65 64 20 73 69 6e 63 65 were added since
21534 20 74 68 65 0a 2a 2a 20 74 68 65 20 70 72 65 76 the.** the prev
21535 69 6f 75 73 20 4e 20 50 75 73 68 20 6f 70 65 72 ious N Push oper
21536 61 74 69 6f 6e 73 2e 20 20 49 6e 20 6f 74 68 65 ations. In othe
21537 72 20 77 6f 72 64 73 2c 20 72 65 73 74 6f 72 65 r words, restore
21538 20 74 68 65 20 63 61 63 68 65 0a 2a 2a 20 74 6f the cache.** to
21539 20 74 68 65 20 73 74 61 74 65 20 69 74 20 77 61 the state it wa
2153a 73 20 69 6e 20 4e 20 50 75 73 68 65 73 20 61 67 s in N Pushes ag
2153b 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 o..*/.SQLITE_PRI
2153c 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
2153d 33 45 78 70 72 43 61 63 68 65 50 6f 70 28 50 61 3ExprCachePop(Pa
2153e 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 rse *pParse, int
2153f 20 4e 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 N){. int i;.
21540 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 struct yColCache
21541 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 4e *p;. assert( N
21542 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 >0 );. assert(
21543 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 pParse->iCacheLe
21544 76 65 6c 3e 3d 4e 20 29 3b 0a 20 20 70 50 61 72 vel>=N );. pPar
21545 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 20 se->iCacheLevel
21546 2d 3d 20 4e 3b 0a 20 20 66 6f 72 28 69 3d 30 2c -= N;. for(i=0,
21547 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 p=pParse->aColC
21548 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e ache; i<SQLITE_N
21549 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 _COLCACHE; i++,
2154a 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 2d p++){. if( p-
2154b 3e 69 52 65 67 20 26 26 20 70 2d 3e 69 4c 65 76 >iReg && p->iLev
2154c 65 6c 3e 70 50 61 72 73 65 2d 3e 69 43 61 63 68 el>pParse->iCach
2154d 65 4c 65 76 65 6c 20 29 7b 0a 20 20 20 20 20 20 eLevel ){.
2154e 63 61 63 68 65 45 6e 74 72 79 43 6c 65 61 72 28 cacheEntryClear(
2154f 70 50 61 72 73 65 2c 20 70 29 3b 0a 20 20 20 20 pParse, p);.
21550 20 20 70 2d 3e 69 52 65 67 20 3d 20 30 3b 0a 20 p->iReg = 0;.
21551 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a }. }.}../*.*
21552 2a 20 57 68 65 6e 20 61 20 63 61 63 68 65 64 20 * When a cached
21553 63 6f 6c 75 6d 6e 20 69 73 20 72 65 75 73 65 64 column is reused
21554 2c 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 , make sure that
21555 20 69 74 73 20 72 65 67 69 73 74 65 72 20 69 73 its register is
21556 0a 2a 2a 20 6e 6f 20 6c 6f 6e 67 65 72 20 61 76 .** no longer av
21557 61 69 6c 61 62 6c 65 20 61 73 20 61 20 74 65 6d ailable as a tem
21558 70 20 72 65 67 69 73 74 65 72 2e 20 20 74 69 63 p register. tic
21559 6b 65 74 20 23 33 38 37 39 3a 20 20 74 68 61 74 ket #3879: that
2155a 20 73 61 6d 65 0a 2a 2a 20 72 65 67 69 73 74 65 same.** registe
2155b 72 20 6d 69 67 68 74 20 62 65 20 69 6e 20 74 68 r might be in th
2155c 65 20 63 61 63 68 65 20 69 6e 20 6d 75 6c 74 69 e cache in multi
2155d 70 6c 65 20 70 6c 61 63 65 73 2c 20 73 6f 20 62 ple places, so b
2155e 65 20 73 75 72 65 20 74 6f 0a 2a 2a 20 67 65 74 e sure to.** get
2155f 20 74 68 65 6d 20 61 6c 6c 2e 0a 2a 2f 0a 73 74 them all..*/.st
21560 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 atic void sqlite
21561 33 45 78 70 72 43 61 63 68 65 50 69 6e 52 65 67 3ExprCachePinReg
21562 69 73 74 65 72 28 50 61 72 73 65 20 2a 70 50 61 ister(Parse *pPa
21563 72 73 65 2c 20 69 6e 74 20 69 52 65 67 29 7b 0a rse, int iReg){.
21564 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 int i;. struc
21565 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a t yColCache *p;.
21566 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 for(i=0, p=pPa
21567 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 rse->aColCache;
21568 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 i<SQLITE_N_COLCA
21569 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a CHE; i++, p++){.
2156a 20 20 20 20 69 66 28 20 70 2d 3e 69 52 65 67 3d if( p->iReg=
2156b 3d 69 52 65 67 20 29 7b 0a 20 20 20 20 20 20 70 =iReg ){. p
2156c 2d 3e 74 65 6d 70 52 65 67 20 3d 20 30 3b 0a 20 ->tempReg = 0;.
2156d 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a }. }.}../*.*
2156e 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 * Generate code
2156f 74 68 61 74 20 77 69 6c 6c 20 65 78 74 72 61 63 that will extrac
21570 74 20 74 68 65 20 69 43 6f 6c 75 6d 6e 2d 74 68 t the iColumn-th
21571 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 0a 2a 2a 20 column from.**
21572 74 61 62 6c 65 20 70 54 61 62 20 61 6e 64 20 73 table pTab and s
21573 74 6f 72 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 tore the column
21574 76 61 6c 75 65 20 69 6e 20 61 20 72 65 67 69 73 value in a regis
21575 74 65 72 2e 20 20 41 6e 20 65 66 66 6f 72 74 0a ter. An effort.
21576 2a 2a 20 69 73 20 6d 61 64 65 20 74 6f 20 73 74 ** is made to st
21577 6f 72 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 76 ore the column v
21578 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 alue in register
21579 20 69 52 65 67 2c 20 62 75 74 20 74 68 69 73 20 iReg, but this
2157a 69 73 0a 2a 2a 20 6e 6f 74 20 67 75 61 72 61 6e is.** not guaran
2157b 74 65 65 64 2e 20 20 54 68 65 20 6c 6f 63 61 74 teed. The locat
2157c 69 6f 6e 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d ion of the colum
2157d 6e 20 76 61 6c 75 65 20 69 73 20 72 65 74 75 72 n value is retur
2157e 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 ned..**.** There
2157f 20 6d 75 73 74 20 62 65 20 61 6e 20 6f 70 65 6e must be an open
21580 20 63 75 72 73 6f 72 20 74 6f 20 70 54 61 62 20 cursor to pTab
21581 69 6e 20 69 54 61 62 6c 65 20 77 68 65 6e 20 74 in iTable when t
21582 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 his routine.** i
21583 73 20 63 61 6c 6c 65 64 2e 20 20 49 66 20 69 43 s called. If iC
21584 6f 6c 75 6d 6e 3c 30 20 74 68 65 6e 20 63 6f 64 olumn<0 then cod
21585 65 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 74 e is generated t
21586 68 61 74 20 65 78 74 72 61 63 74 73 20 74 68 65 hat extracts the
21587 20 72 6f 77 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 rowid..**.** Th
21588 69 73 20 72 6f 75 74 69 6e 65 20 6d 69 67 68 74 is routine might
21589 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 75 73 attempt to reus
2158a 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 e the value of t
2158b 68 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 0a 2a he column that.*
2158c 2a 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 * has already be
2158d 65 6e 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20 61 en loaded into a
2158e 20 72 65 67 69 73 74 65 72 2e 20 20 54 68 65 20 register. The
2158f 76 61 6c 75 65 20 77 69 6c 6c 20 61 6c 77 61 79 value will alway
21590 73 0a 2a 2a 20 62 65 20 75 73 65 64 20 69 66 20 s.** be used if
21591 69 74 20 68 61 73 20 6e 6f 74 20 75 6e 64 65 72 it has not under
21592 67 6f 6e 65 20 61 6e 79 20 61 66 66 69 6e 69 74 gone any affinit
21593 79 20 63 68 61 6e 67 65 73 2e 20 20 42 75 74 20 y changes. But
21594 69 66 0a 2a 2a 20 61 6e 20 61 66 66 69 6e 69 74 if.** an affinit
21595 79 20 63 68 61 6e 67 65 20 68 61 73 20 6f 63 63 y change has occ
21596 75 72 72 65 64 2c 20 74 68 65 6e 20 74 68 65 20 urred, then the
21597 63 61 63 68 65 64 20 76 61 6c 75 65 20 77 69 6c cached value wil
21598 6c 20 6f 6e 6c 79 20 62 65 0a 2a 2a 20 75 73 65 l only be.** use
21599 64 20 69 66 20 61 6c 6c 6f 77 41 66 66 43 68 6e d if allowAffChn
2159a 67 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 53 51 g is true..*/.SQ
2159b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
2159c 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
2159d 47 65 74 43 6f 6c 75 6d 6e 28 0a 20 20 50 61 72 GetColumn(. Par
2159e 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 2f 2a se *pParse, /*
2159f 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 Parsing and cod
215a0 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e e generating con
215a1 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 text */. Table
215a2 2a 70 54 61 62 2c 20 20 20 20 20 2f 2a 20 44 65 *pTab, /* De
215a3 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 65 scription of the
215a4 20 74 61 62 6c 65 20 77 65 20 61 72 65 20 72 65 table we are re
215a5 61 64 69 6e 67 20 66 72 6f 6d 20 2a 2f 0a 20 20 ading from */.
215a6 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 20 20 20 int iColumn,
215a7 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65 /* Index of the
215a8 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 2a 2f table column */
215a9 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 . int iTable,
215aa 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f /* The curso
215ab 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 r pointing to th
215ac 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 e table */. int
215ad 20 69 52 65 67 2c 20 20 20 20 20 20 20 20 2f 2a iReg, /*
215ae 20 53 74 6f 72 65 20 72 65 73 75 6c 74 73 20 68 Store results h
215af 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 61 6c 6c ere */. int all
215b0 6f 77 41 66 66 43 68 6e 67 20 2f 2a 20 54 72 75 owAffChng /* Tru
215b1 65 20 69 66 20 70 72 69 6f 72 20 61 66 66 69 6e e if prior affin
215b2 69 74 79 20 63 68 61 6e 67 65 73 20 61 72 65 20 ity changes are
215b3 4f 4b 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 OK */.){. Vdbe
215b4 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 *v = pParse->pVd
215b5 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 be;. int i;. s
215b6 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20 truct yColCache
215b7 2a 70 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 2c 20 *p;.. for(i=0,
215b8 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 p=pParse->aColCa
215b9 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f che; i<SQLITE_N_
215ba 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 COLCACHE; i++, p
215bb 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e ++){. if( p->
215bc 69 52 65 67 3e 30 20 26 26 20 70 2d 3e 69 54 61 iReg>0 && p->iTa
215bd 62 6c 65 3d 3d 69 54 61 62 6c 65 20 26 26 20 70 ble==iTable && p
215be 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 69 43 6f 6c 75 ->iColumn==iColu
215bf 6d 6e 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 mn. &&
215c0 20 28 21 70 2d 3e 61 66 66 43 68 61 6e 67 65 20 (!p->affChange
215c1 7c 7c 20 61 6c 6c 6f 77 41 66 66 43 68 6e 67 29 || allowAffChng)
215c2 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6c 72 75 ){. p->lru
215c3 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 = pParse->iCach
215c4 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 73 71 eCnt++;. sq
215c5 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 69 lite3ExprCachePi
215c6 6e 52 65 67 69 73 74 65 72 28 70 50 61 72 73 65 nRegister(pParse
215c7 2c 20 70 2d 3e 69 52 65 67 29 3b 0a 20 20 20 20 , p->iReg);.
215c8 20 20 72 65 74 75 72 6e 20 70 2d 3e 69 52 65 67 return p->iReg
215c9 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 20 0a 20 20 ;. }. } .
215ca 61 73 73 65 72 74 28 20 76 21 3d 30 20 29 3b 0a assert( v!=0 );.
215cb 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e 3c 30 20 if( iColumn<0
215cc 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
215cd 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 beAddOp2(v, OP_R
215ce 6f 77 69 64 2c 20 69 54 61 62 6c 65 2c 20 69 52 owid, iTable, iR
215cf 65 67 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 eg);. }else if(
215d0 20 41 4c 57 41 59 53 28 70 54 61 62 21 3d 30 29 ALWAYS(pTab!=0)
215d1 20 29 7b 0a 20 20 20 20 69 6e 74 20 6f 70 20 3d ){. int op =
215d2 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 IsVirtual(pTab)
215d3 20 3f 20 4f 50 5f 56 43 6f 6c 75 6d 6e 20 3a 20 ? OP_VColumn :
215d4 4f 50 5f 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 73 OP_Column;. s
215d5 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
215d6 28 76 2c 20 6f 70 2c 20 69 54 61 62 6c 65 2c 20 (v, op, iTable,
215d7 69 43 6f 6c 75 6d 6e 2c 20 69 52 65 67 29 3b 0a iColumn, iReg);.
215d8 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d sqlite3Colum
215d9 6e 44 65 66 61 75 6c 74 28 76 2c 20 70 54 61 62 nDefault(v, pTab
215da 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 52 65 67 29 , iColumn, iReg)
215db 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 ;. }. sqlite3E
215dc 78 70 72 43 61 63 68 65 53 74 6f 72 65 28 70 50 xprCacheStore(pP
215dd 61 72 73 65 2c 20 69 54 61 62 6c 65 2c 20 69 43 arse, iTable, iC
215de 6f 6c 75 6d 6e 2c 20 69 52 65 67 29 3b 0a 20 20 olumn, iReg);.
215df 72 65 74 75 72 6e 20 69 52 65 67 3b 0a 7d 0a 0a return iReg;.}..
215e0 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 61 6c 6c 20 /*.** Clear all
215e1 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 65 6e 74 column cache ent
215e2 72 69 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ries..*/.SQLITE_
215e3 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
215e4 69 74 65 33 45 78 70 72 43 61 63 68 65 43 6c 65 ite3ExprCacheCle
215e5 61 72 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 ar(Parse *pParse
215e6 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 ){. int i;. st
215e7 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20 2a ruct yColCache *
215e8 70 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 p;.. for(i=0, p
215e9 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 =pParse->aColCac
215ea 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 he; i<SQLITE_N_C
215eb 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b OLCACHE; i++, p+
215ec 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 +){. if( p->i
215ed 52 65 67 20 29 7b 0a 20 20 20 20 20 20 63 61 63 Reg ){. cac
215ee 68 65 45 6e 74 72 79 43 6c 65 61 72 28 70 50 61 heEntryClear(pPa
215ef 72 73 65 2c 20 70 29 3b 0a 20 20 20 20 20 20 70 rse, p);. p
215f0 2d 3e 69 52 65 67 20 3d 20 30 3b 0a 20 20 20 20 ->iReg = 0;.
215f1 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 }. }.}../*.** R
215f2 65 63 6f 72 64 20 74 68 65 20 66 61 63 74 20 74 ecord the fact t
215f3 68 61 74 20 61 6e 20 61 66 66 69 6e 69 74 79 20 hat an affinity
215f4 63 68 61 6e 67 65 20 68 61 73 20 6f 63 63 75 72 change has occur
215f5 72 65 64 20 6f 6e 20 69 43 6f 75 6e 74 0a 2a 2a red on iCount.**
215f6 20 72 65 67 69 73 74 65 72 73 20 73 74 61 72 74 registers start
215f7 69 6e 67 20 77 69 74 68 20 69 53 74 61 72 74 2e ing with iStart.
215f8 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
215f9 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
215fa 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 xprCacheAffinity
215fb 43 68 61 6e 67 65 28 50 61 72 73 65 20 2a 70 50 Change(Parse *pP
215fc 61 72 73 65 2c 20 69 6e 74 20 69 53 74 61 72 74 arse, int iStart
215fd 2c 20 69 6e 74 20 69 43 6f 75 6e 74 29 7b 0a 20 , int iCount){.
215fe 20 69 6e 74 20 69 45 6e 64 20 3d 20 69 53 74 61 int iEnd = iSta
215ff 72 74 20 2b 20 69 43 6f 75 6e 74 20 2d 20 31 3b rt + iCount - 1;
21600 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 . int i;. stru
21601 63 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b ct yColCache *p;
21602 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 . for(i=0, p=pP
21603 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b arse->aColCache;
21604 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 i<SQLITE_N_COLC
21605 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b ACHE; i++, p++){
21606 0a 20 20 20 20 69 6e 74 20 72 20 3d 20 70 2d 3e . int r = p->
21607 69 52 65 67 3b 0a 20 20 20 20 69 66 28 20 72 3e iReg;. if( r>
21608 3d 69 53 74 61 72 74 20 26 26 20 72 3c 3d 69 45 =iStart && r<=iE
21609 6e 64 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61 nd ){. p->a
2160a 66 66 43 68 61 6e 67 65 20 3d 20 31 3b 0a 20 20 ffChange = 1;.
2160b 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
2160c 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 Generate code t
2160d 6f 20 6d 6f 76 65 20 63 6f 6e 74 65 6e 74 20 66 o move content f
2160e 72 6f 6d 20 72 65 67 69 73 74 65 72 73 20 69 46 rom registers iF
2160f 72 6f 6d 2e 2e 2e 69 46 72 6f 6d 2b 6e 52 65 67 rom...iFrom+nReg
21610 2d 31 0a 2a 2a 20 6f 76 65 72 20 74 6f 20 69 54 -1.** over to iT
21611 6f 2e 2e 69 54 6f 2b 6e 52 65 67 2d 31 2e 20 4b o..iTo+nReg-1. K
21612 65 65 70 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 eep the column c
21613 61 63 68 65 20 75 70 2d 74 6f 2d 64 61 74 65 2e ache up-to-date.
21614 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
21615 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
21616 78 70 72 43 6f 64 65 4d 6f 76 65 28 50 61 72 73 xprCodeMove(Pars
21617 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 e *pParse, int i
21618 46 72 6f 6d 2c 20 69 6e 74 20 69 54 6f 2c 20 69 From, int iTo, i
21619 6e 74 20 6e 52 65 67 29 7b 0a 20 20 69 6e 74 20 nt nReg){. int
2161a 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c i;. struct yCol
2161b 43 61 63 68 65 20 2a 70 3b 0a 20 20 69 66 28 20 Cache *p;. if(
2161c 4e 45 56 45 52 28 69 46 72 6f 6d 3d 3d 69 54 6f NEVER(iFrom==iTo
2161d 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 ) ) return;. sq
2161e 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
2161f 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 4f pParse->pVdbe, O
21620 50 5f 4d 6f 76 65 2c 20 69 46 72 6f 6d 2c 20 69 P_Move, iFrom, i
21621 54 6f 2c 20 6e 52 65 67 29 3b 0a 20 20 66 6f 72 To, nReg);. for
21622 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e (i=0, p=pParse->
21623 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c aColCache; i<SQL
21624 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 ITE_N_COLCACHE;
21625 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 i++, p++){. i
21626 6e 74 20 78 20 3d 20 70 2d 3e 69 52 65 67 3b 0a nt x = p->iReg;.
21627 20 20 20 20 69 66 28 20 78 3e 3d 69 46 72 6f 6d if( x>=iFrom
21628 20 26 26 20 78 3c 69 46 72 6f 6d 2b 6e 52 65 67 && x<iFrom+nReg
21629 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 52 65 ){. p->iRe
2162a 67 20 2b 3d 20 69 54 6f 2d 69 46 72 6f 6d 3b 0a g += iTo-iFrom;.
2162b 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
2162c 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 ** Generate code
2162d 20 74 6f 20 63 6f 70 79 20 63 6f 6e 74 65 6e 74 to copy content
2162e 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72 73 20 from registers
2162f 69 46 72 6f 6d 2e 2e 2e 69 46 72 6f 6d 2b 6e 52 iFrom...iFrom+nR
21630 65 67 2d 31 0a 2a 2a 20 6f 76 65 72 20 74 6f 20 eg-1.** over to
21631 69 54 6f 2e 2e 69 54 6f 2b 6e 52 65 67 2d 31 2e iTo..iTo+nReg-1.
21632 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
21633 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
21634 78 70 72 43 6f 64 65 43 6f 70 79 28 50 61 72 73 xprCodeCopy(Pars
21635 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 e *pParse, int i
21636 46 72 6f 6d 2c 20 69 6e 74 20 69 54 6f 2c 20 69 From, int iTo, i
21637 6e 74 20 6e 52 65 67 29 7b 0a 20 20 69 6e 74 20 nt nReg){. int
21638 69 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 69 i;. if( NEVER(i
21639 46 72 6f 6d 3d 3d 69 54 6f 29 20 29 20 72 65 74 From==iTo) ) ret
2163a 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 urn;. for(i=0;
2163b 69 3c 6e 52 65 67 3b 20 69 2b 2b 29 7b 0a 20 20 i<nReg; i++){.
2163c 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
2163d 4f 70 32 28 70 50 61 72 73 65 2d 3e 70 56 64 62 Op2(pParse->pVdb
2163e 65 2c 20 4f 50 5f 43 6f 70 79 2c 20 69 46 72 6f e, OP_Copy, iFro
2163f 6d 2b 69 2c 20 69 54 6f 2b 69 29 3b 0a 20 20 7d m+i, iTo+i);. }
21640 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
21641 20 74 72 75 65 20 69 66 20 61 6e 79 20 72 65 67 true if any reg
21642 69 73 74 65 72 20 69 6e 20 74 68 65 20 72 61 6e ister in the ran
21643 67 65 20 69 46 72 6f 6d 2e 2e 69 54 6f 20 28 69 ge iFrom..iTo (i
21644 6e 63 6c 75 73 69 76 65 29 0a 2a 2a 20 69 73 20 nclusive).** is
21645 75 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 used as part of
21646 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 the column cache
21647 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
21648 75 73 65 64 41 73 43 6f 6c 75 6d 6e 43 61 63 68 usedAsColumnCach
21649 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c e(Parse *pParse,
2164a 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74 20 int iFrom, int
2164b 69 54 6f 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 iTo){. int i;.
2164c 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 struct yColCach
2164d 65 20 2a 70 3b 0a 20 20 66 6f 72 28 69 3d 30 2c e *p;. for(i=0,
2164e 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 p=pParse->aColC
2164f 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e ache; i<SQLITE_N
21650 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 _COLCACHE; i++,
21651 70 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 72 20 p++){. int r
21652 3d 20 70 2d 3e 69 52 65 67 3b 0a 20 20 20 20 69 = p->iReg;. i
21653 66 28 20 72 3e 3d 69 46 72 6f 6d 20 26 26 20 72 f( r>=iFrom && r
21654 3c 3d 69 54 6f 20 29 20 72 65 74 75 72 6e 20 31 <=iTo ) return 1
21655 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 ;. }. return 0
21656 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 ;.}../*.** If th
21657 65 20 6c 61 73 74 20 69 6e 73 74 72 75 63 74 69 e last instructi
21658 6f 6e 20 63 6f 64 65 64 20 69 73 20 61 6e 20 65 on coded is an e
21659 70 68 65 6d 65 72 61 6c 20 63 6f 70 79 20 6f 66 phemeral copy of
2165a 20 61 6e 79 20 6f 66 0a 2a 2a 20 74 68 65 20 72 any of.** the r
2165b 65 67 69 73 74 65 72 73 20 69 6e 20 74 68 65 20 egisters in the
2165c 6e 52 65 67 20 72 65 67 69 73 74 65 72 73 20 62 nReg registers b
2165d 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 69 52 eginning with iR
2165e 65 67 2c 20 74 68 65 6e 0a 2a 2a 20 63 6f 6e 76 eg, then.** conv
2165f 65 72 74 20 74 68 65 20 6c 61 73 74 20 69 6e 73 ert the last ins
21660 74 72 75 63 74 69 6f 6e 20 66 72 6f 6d 20 4f 50 truction from OP
21661 5f 53 43 6f 70 79 20 74 6f 20 4f 50 5f 43 6f 70 _SCopy to OP_Cop
21662 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 y..*/.SQLITE_PRI
21663 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
21664 33 45 78 70 72 48 61 72 64 43 6f 70 79 28 50 61 3ExprHardCopy(Pa
21665 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 rse *pParse, int
21666 20 69 52 65 67 2c 20 69 6e 74 20 6e 52 65 67 29 iReg, int nReg)
21667 7b 0a 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b {. VdbeOp *pOp;
21668 0a 20 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20 61 . Vdbe *v;.. a
21669 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 64 ssert( pParse->d
2166a 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d b->mallocFailed=
2166b 3d 30 20 29 3b 0a 20 20 76 20 3d 20 70 50 61 72 =0 );. v = pPar
2166c 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 61 73 73 se->pVdbe;. ass
2166d 65 72 74 28 20 76 21 3d 30 20 29 3b 0a 20 20 70 ert( v!=0 );. p
2166e 4f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 Op = sqlite3Vdbe
2166f 47 65 74 4f 70 28 76 2c 20 2d 31 29 3b 0a 20 20 GetOp(v, -1);.
21670 61 73 73 65 72 74 28 20 70 4f 70 21 3d 30 20 29 assert( pOp!=0 )
21671 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 ;. if( pOp->opc
21672 6f 64 65 3d 3d 4f 50 5f 53 43 6f 70 79 20 26 26 ode==OP_SCopy &&
21673 20 70 4f 70 2d 3e 70 31 3e 3d 69 52 65 67 20 26 pOp->p1>=iReg &
21674 26 20 70 4f 70 2d 3e 70 31 3c 69 52 65 67 2b 6e & pOp->p1<iReg+n
21675 52 65 67 20 29 7b 0a 20 20 20 20 70 4f 70 2d 3e Reg ){. pOp->
21676 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 43 6f 70 79 opcode = OP_Copy
21677 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 ;. }.}../*.** G
21678 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 enerate code to
21679 73 74 6f 72 65 20 74 68 65 20 76 61 6c 75 65 20 store the value
2167a 6f 66 20 74 68 65 20 69 41 6c 69 61 73 2d 74 68 of the iAlias-th
2167b 20 61 6c 69 61 73 20 69 6e 20 72 65 67 69 73 74 alias in regist
2167c 65 72 0a 2a 2a 20 74 61 72 67 65 74 2e 20 20 54 er.** target. T
2167d 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 he first time th
2167e 69 73 20 69 73 20 63 61 6c 6c 65 64 2c 20 70 45 is is called, pE
2167f 78 70 72 20 69 73 20 65 76 61 6c 75 61 74 65 64 xpr is evaluated
21680 20 74 6f 20 63 6f 6d 70 75 74 65 0a 2a 2a 20 74 to compute.** t
21681 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 he value of the
21682 61 6c 69 61 73 2e 20 20 54 68 65 20 76 61 6c 75 alias. The valu
21683 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 e is stored in a
21684 6e 20 61 75 78 69 6c 69 61 72 79 20 72 65 67 69 n auxiliary regi
21685 73 74 65 72 0a 2a 2a 20 61 6e 64 20 74 68 65 20 ster.** and the
21686 6e 75 6d 62 65 72 20 6f 66 20 74 68 61 74 20 72 number of that r
21687 65 67 69 73 74 65 72 20 69 73 20 72 65 74 75 72 egister is retur
21688 6e 65 64 2e 20 20 4f 6e 20 73 75 62 73 65 71 75 ned. On subsequ
21689 65 6e 74 20 63 61 6c 6c 73 2c 0a 2a 2a 20 74 68 ent calls,.** th
2168a 65 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65 e register numbe
2168b 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 77 69 r is returned wi
2168c 74 68 6f 75 74 20 67 65 6e 65 72 61 74 69 6e 67 thout generating
2168d 20 61 6e 79 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a any code..**.**
2168e 20 4e 6f 74 65 20 74 68 61 74 20 69 6e 20 6f 72 Note that in or
2168f 64 65 72 20 66 6f 72 20 74 68 69 73 20 74 6f 20 der for this to
21690 77 6f 72 6b 2c 20 63 6f 64 65 20 6d 75 73 74 20 work, code must
21691 62 65 20 67 65 6e 65 72 61 74 65 64 20 69 6e 20 be generated in
21692 74 68 65 0a 2a 2a 20 73 61 6d 65 20 6f 72 64 65 the.** same orde
21693 72 20 74 68 61 74 20 69 74 20 69 73 20 65 78 65 r that it is exe
21694 63 75 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6c 69 cuted..**.** Ali
21695 61 73 65 73 20 61 72 65 20 6e 75 6d 62 65 72 65 ases are numbere
21696 64 20 73 74 61 72 74 69 6e 67 20 77 69 74 68 20 d starting with
21697 31 2e 20 20 53 6f 20 69 41 6c 69 61 73 20 69 73 1. So iAlias is
21698 20 69 6e 20 74 68 65 20 72 61 6e 67 65 0a 2a 2a in the range.**
21699 20 6f 66 20 31 20 74 6f 20 70 50 61 72 73 65 2d of 1 to pParse-
2169a 3e 6e 41 6c 69 61 73 20 69 6e 63 6c 75 73 69 76 >nAlias inclusiv
2169b 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 70 50 61 72 73 e. .**.** pPars
2169c 65 2d 3e 61 41 6c 69 61 73 5b 69 41 6c 69 61 73 e->aAlias[iAlias
2169d 2d 31 5d 20 72 65 63 6f 72 64 73 20 74 68 65 20 -1] records the
2169e 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 20 register number
2169f 77 68 65 72 65 20 74 68 65 20 76 61 6c 75 65 0a where the value.
216a0 2a 2a 20 6f 66 20 74 68 65 20 69 41 6c 69 61 73 ** of the iAlias
216a1 2d 74 68 20 61 6c 69 61 73 20 69 73 20 73 74 6f -th alias is sto
216a2 72 65 64 2e 20 20 49 66 20 7a 65 72 6f 2c 20 74 red. If zero, t
216a3 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 hat means that t
216a4 68 65 0a 2a 2a 20 61 6c 69 61 73 20 68 61 73 20 he.** alias has
216a5 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 63 6f 6d not yet been com
216a6 70 75 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 puted..*/.static
216a7 20 69 6e 74 20 63 6f 64 65 41 6c 69 61 73 28 50 int codeAlias(P
216a8 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e arse *pParse, in
216a9 74 20 69 41 6c 69 61 73 2c 20 45 78 70 72 20 2a t iAlias, Expr *
216aa 70 45 78 70 72 2c 20 69 6e 74 20 74 61 72 67 65 pExpr, int targe
216ab 74 29 7b 0a 23 69 66 20 30 0a 20 20 73 71 6c 69 t){.#if 0. sqli
216ac 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 te3 *db = pParse
216ad 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 52 65 67 ->db;. int iReg
216ae 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e ;. if( pParse->
216af 6e 41 6c 69 61 73 41 6c 6c 6f 63 3c 70 50 61 72 nAliasAlloc<pPar
216b0 73 65 2d 3e 6e 41 6c 69 61 73 20 29 7b 0a 20 20 se->nAlias ){.
216b1 20 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 pParse->aAlias
216b2 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c = sqlite3DbReal
216b3 6c 6f 63 4f 72 46 72 65 65 28 64 62 2c 20 70 50 locOrFree(db, pP
216b4 61 72 73 65 2d 3e 61 41 6c 69 61 73 2c 0a 20 20 arse->aAlias,.
216b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
216b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 s
216b7 69 7a 65 6f 66 28 70 50 61 72 73 65 2d 3e 61 41 izeof(pParse->aA
216b8 6c 69 61 73 5b 30 5d 29 2a 70 50 61 72 73 65 2d lias[0])*pParse-
216b9 3e 6e 41 6c 69 61 73 20 29 3b 0a 20 20 20 20 74 >nAlias );. t
216ba 65 73 74 63 61 73 65 28 20 64 62 2d 3e 6d 61 6c estcase( db->mal
216bb 6c 6f 63 46 61 69 6c 65 64 20 26 26 20 70 50 61 locFailed && pPa
216bc 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63 rse->nAliasAlloc
216bd 3e 30 20 29 3b 0a 20 20 20 20 69 66 28 20 64 62 >0 );. if( db
216be 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
216bf 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 6d return 0;. m
216c0 65 6d 73 65 74 28 26 70 50 61 72 73 65 2d 3e 61 emset(&pParse->a
216c1 41 6c 69 61 73 5b 70 50 61 72 73 65 2d 3e 6e 41 Alias[pParse->nA
216c2 6c 69 61 73 41 6c 6c 6f 63 5d 2c 20 30 2c 0a 20 liasAlloc], 0,.
216c3 20 20 20 20 20 20 20 20 20 20 28 70 50 61 72 73 (pPars
216c4 65 2d 3e 6e 41 6c 69 61 73 2d 70 50 61 72 73 65 e->nAlias-pParse
216c5 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63 29 2a 73 ->nAliasAlloc)*s
216c6 69 7a 65 6f 66 28 70 50 61 72 73 65 2d 3e 61 41 izeof(pParse->aA
216c7 6c 69 61 73 5b 30 5d 29 29 3b 0a 20 20 20 20 70 lias[0]));. p
216c8 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c Parse->nAliasAll
216c9 6f 63 20 3d 20 70 50 61 72 73 65 2d 3e 6e 41 6c oc = pParse->nAl
216ca 69 61 73 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 ias;. }. asser
216cb 74 28 20 69 41 6c 69 61 73 3e 30 20 26 26 20 69 t( iAlias>0 && i
216cc 41 6c 69 61 73 3c 3d 70 50 61 72 73 65 2d 3e 6e Alias<=pParse->n
216cd 41 6c 69 61 73 20 29 3b 0a 20 20 69 52 65 67 20 Alias );. iReg
216ce 3d 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 = pParse->aAlias
216cf 5b 69 41 6c 69 61 73 2d 31 5d 3b 0a 20 20 69 66 [iAlias-1];. if
216d0 28 20 69 52 65 67 3d 3d 30 20 29 7b 0a 20 20 20 ( iReg==0 ){.
216d1 20 69 66 28 20 70 50 61 72 73 65 2d 3e 69 43 61 if( pParse->iCa
216d2 63 68 65 4c 65 76 65 6c 3e 30 20 29 7b 0a 20 20 cheLevel>0 ){.
216d3 20 20 20 20 69 52 65 67 20 3d 20 73 71 6c 69 74 iReg = sqlit
216d4 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 e3ExprCodeTarget
216d5 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 (pParse, pExpr,
216d6 74 61 72 67 65 74 29 3b 0a 20 20 20 20 7d 65 6c target);. }el
216d7 73 65 7b 0a 20 20 20 20 20 20 69 52 65 67 20 3d se{. iReg =
216d8 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b ++pParse->nMem;
216d9 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 . sqlite3Ex
216da 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 prCode(pParse, p
216db 45 78 70 72 2c 20 69 52 65 67 29 3b 0a 20 20 20 Expr, iReg);.
216dc 20 20 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 pParse->aAlia
216dd 73 5b 69 41 6c 69 61 73 2d 31 5d 20 3d 20 69 52 s[iAlias-1] = iR
216de 65 67 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 eg;. }. }.
216df 72 65 74 75 72 6e 20 69 52 65 67 3b 0a 23 65 6c return iReg;.#el
216e0 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 se. UNUSED_PARA
216e1 4d 45 54 45 52 28 69 41 6c 69 61 73 29 3b 0a 20 METER(iAlias);.
216e2 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 45 return sqlite3E
216e3 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 xprCodeTarget(pP
216e4 61 72 73 65 2c 20 70 45 78 70 72 2c 20 74 61 72 arse, pExpr, tar
216e5 67 65 74 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a get);.#endif.}..
216e6 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 /*.** Generate c
216e7 6f 64 65 20 69 6e 74 6f 20 74 68 65 20 63 75 72 ode into the cur
216e8 72 65 6e 74 20 56 64 62 65 20 74 6f 20 65 76 61 rent Vdbe to eva
216e9 6c 75 61 74 65 20 74 68 65 20 67 69 76 65 6e 0a luate the given.
216ea 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 ** expression.
216eb 41 74 74 65 6d 70 74 20 74 6f 20 73 74 6f 72 65 Attempt to store
216ec 20 74 68 65 20 72 65 73 75 6c 74 73 20 69 6e 20 the results in
216ed 72 65 67 69 73 74 65 72 20 22 74 61 72 67 65 74 register "target
216ee 22 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 "..** Return the
216ef 20 72 65 67 69 73 74 65 72 20 77 68 65 72 65 20 register where
216f0 72 65 73 75 6c 74 73 20 61 72 65 20 73 74 6f 72 results are stor
216f1 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 20 74 ed..**.** With t
216f2 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 74 68 65 his routine, the
216f3 72 65 20 69 73 20 6e 6f 20 67 75 61 72 61 6e 74 re is no guarant
216f4 65 65 20 74 68 61 74 20 72 65 73 75 6c 74 73 20 ee that results
216f5 77 69 6c 6c 0a 2a 2a 20 62 65 20 73 74 6f 72 65 will.** be store
216f6 64 20 69 6e 20 74 61 72 67 65 74 2e 20 20 54 68 d in target. Th
216f7 65 20 72 65 73 75 6c 74 20 6d 69 67 68 74 20 62 e result might b
216f8 65 20 73 74 6f 72 65 64 20 69 6e 20 73 6f 6d 65 e stored in some
216f9 20 6f 74 68 65 72 0a 2a 2a 20 72 65 67 69 73 74 other.** regist
216fa 65 72 20 69 66 20 69 74 20 69 73 20 63 6f 6e 76 er if it is conv
216fb 65 6e 69 65 6e 74 20 74 6f 20 64 6f 20 73 6f 2e enient to do so.
216fc 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 The calling fu
216fd 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 63 nction.** must c
216fe 68 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20 heck the return
216ff 63 6f 64 65 20 61 6e 64 20 6d 6f 76 65 20 74 68 code and move th
21700 65 20 72 65 73 75 6c 74 73 20 74 6f 20 74 68 65 e results to the
21701 20 64 65 73 69 72 65 64 0a 2a 2a 20 72 65 67 69 desired.** regi
21702 73 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ster..*/.SQLITE_
21703 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
21704 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 te3ExprCodeTarge
21705 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c t(Parse *pParse,
21706 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e Expr *pExpr, in
21707 74 20 74 61 72 67 65 74 29 7b 0a 20 20 56 64 62 t target){. Vdb
21708 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 e *v = pParse->p
21709 56 64 62 65 3b 20 20 2f 2a 20 54 68 65 20 56 4d Vdbe; /* The VM
2170a 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 under construct
2170b 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 3b ion */. int op;
2170c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2170d 20 20 20 2f 2a 20 54 68 65 20 6f 70 63 6f 64 65 /* The opcode
2170e 20 62 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0a being coded */.
2170f 20 20 69 6e 74 20 69 6e 52 65 67 20 3d 20 74 61 int inReg = ta
21710 72 67 65 74 3b 20 20 20 20 20 20 20 2f 2a 20 52 rget; /* R
21711 65 73 75 6c 74 73 20 73 74 6f 72 65 64 20 69 6e esults stored in
21712 20 72 65 67 69 73 74 65 72 20 69 6e 52 65 67 20 register inReg
21713 2a 2f 0a 20 20 69 6e 74 20 72 65 67 46 72 65 65 */. int regFree
21714 31 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 1 = 0; /
21715 2a 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f 20 66 72 * If non-zero fr
21716 65 65 20 74 68 69 73 20 74 65 6d 70 6f 72 61 72 ee this temporar
21717 79 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 y register */.
21718 69 6e 74 20 72 65 67 46 72 65 65 32 20 3d 20 30 int regFree2 = 0
21719 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 ; /* If
2171a 6e 6f 6e 2d 7a 65 72 6f 20 66 72 65 65 20 74 68 non-zero free th
2171b 69 73 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 is temporary reg
2171c 69 73 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 ister */. int r
2171d 31 2c 20 72 32 2c 20 72 33 2c 20 72 34 3b 20 20 1, r2, r3, r4;
2171e 20 20 20 20 20 2f 2a 20 56 61 72 69 6f 75 73 20 /* Various
2171f 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 73 register numbers
21720 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 */. sqlite3 *d
21721 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 b = pParse->db;
21722 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 /* The database
21723 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20 connection */..
21724 20 61 73 73 65 72 74 28 20 74 61 72 67 65 74 3e assert( target>
21725 30 20 26 26 20 74 61 72 67 65 74 3c 3d 70 50 61 0 && target<=pPa
21726 72 73 65 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 69 rse->nMem );. i
21727 66 28 20 76 3d 3d 30 20 29 7b 0a 20 20 20 20 61 f( v==0 ){. a
21728 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 64 ssert( pParse->d
21729 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
2172a 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b );. return 0;
2172b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 45 78 70 . }.. if( pExp
2172c 72 3d 3d 30 20 29 7b 0a 20 20 20 20 6f 70 20 3d r==0 ){. op =
2172d 20 54 4b 5f 4e 55 4c 4c 3b 0a 20 20 7d 65 6c 73 TK_NULL;. }els
2172e 65 7b 0a 20 20 20 20 6f 70 20 3d 20 70 45 78 70 e{. op = pExp
2172f 72 2d 3e 6f 70 3b 0a 20 20 7d 0a 20 20 73 77 69 r->op;. }. swi
21730 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 tch( op ){. c
21731 61 73 65 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d ase TK_AGG_COLUM
21732 4e 3a 20 7b 0a 20 20 20 20 20 20 41 67 67 49 6e N: {. AggIn
21733 66 6f 20 2a 70 41 67 67 49 6e 66 6f 20 3d 20 70 fo *pAggInfo = p
21734 45 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f 3b 0a Expr->pAggInfo;.
21735 20 20 20 20 20 20 73 74 72 75 63 74 20 41 67 67 struct Agg
21736 49 6e 66 6f 5f 63 6f 6c 20 2a 70 43 6f 6c 20 3d Info_col *pCol =
21737 20 26 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c &pAggInfo->aCol
21738 5b 70 45 78 70 72 2d 3e 69 41 67 67 5d 3b 0a 20 [pExpr->iAgg];.
21739 20 20 20 20 20 69 66 28 20 21 70 41 67 67 49 6e if( !pAggIn
2173a 66 6f 2d 3e 64 69 72 65 63 74 4d 6f 64 65 20 29 fo->directMode )
2173b 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 {. assert
2173c 28 20 70 43 6f 6c 2d 3e 69 4d 65 6d 3e 30 20 29 ( pCol->iMem>0 )
2173d 3b 0a 20 20 20 20 20 20 20 20 69 6e 52 65 67 20 ;. inReg
2173e 3d 20 70 43 6f 6c 2d 3e 69 4d 65 6d 3b 0a 20 20 = pCol->iMem;.
2173f 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
21740 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 41 67 }else if( pAg
21741 67 49 6e 66 6f 2d 3e 75 73 65 53 6f 72 74 69 6e gInfo->useSortin
21742 67 49 64 78 20 29 7b 0a 20 20 20 20 20 20 20 20 gIdx ){.
21743 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
21744 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 3(v, OP_Column,
21745 70 41 67 67 49 6e 66 6f 2d 3e 73 6f 72 74 69 6e pAggInfo->sortin
21746 67 49 64 78 2c 0a 20 20 20 20 20 20 20 20 20 20 gIdx,.
21747 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21748 20 20 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 pCol->iSorte
21749 72 43 6f 6c 75 6d 6e 2c 20 74 61 72 67 65 74 29 rColumn, target)
2174a 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b ;. break;
2174b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2f . }. /
2174c 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 66 61 6c * Otherwise, fal
2174d 6c 20 74 68 72 75 20 69 6e 74 6f 20 74 68 65 20 l thru into the
2174e 54 4b 5f 43 4f 4c 55 4d 4e 20 63 61 73 65 20 2a TK_COLUMN case *
2174f 2f 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 /. }. case
21750 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 TK_COLUMN: {.
21751 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 if( pExpr->i
21752 54 61 62 6c 65 3c 30 20 29 7b 0a 20 20 20 20 20 Table<0 ){.
21753 20 20 20 2f 2a 20 54 68 69 73 20 6f 6e 6c 79 20 /* This only
21754 68 61 70 70 65 6e 73 20 77 68 65 6e 20 63 6f 64 happens when cod
21755 69 6e 67 20 63 68 65 63 6b 20 63 6f 6e 73 74 72 ing check constr
21756 61 69 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 20 aints */.
21757 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d assert( pParse-
21758 3e 63 6b 42 61 73 65 3e 30 20 29 3b 0a 20 20 20 >ckBase>0 );.
21759 20 20 20 20 20 69 6e 52 65 67 20 3d 20 70 45 78 inReg = pEx
2175a 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 2b 20 70 50 pr->iColumn + pP
2175b 61 72 73 65 2d 3e 63 6b 42 61 73 65 3b 0a 20 20 arse->ckBase;.
2175c 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
2175d 20 20 20 74 65 73 74 63 61 73 65 28 20 28 70 45 testcase( (pE
2175e 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f xpr->flags & EP_
2175f 41 6e 79 41 66 66 29 21 3d 30 20 29 3b 0a 20 20 AnyAff)!=0 );.
21760 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 73 71 inReg = sq
21761 6c 69 74 65 33 45 78 70 72 43 6f 64 65 47 65 74 lite3ExprCodeGet
21762 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c 20 70 Column(pParse, p
21763 45 78 70 72 2d 3e 70 54 61 62 2c 0a 20 20 20 20 Expr->pTab,.
21764 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21765 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 pEx
21766 70 72 2d 3e 69 43 6f 6c 75 6d 6e 2c 20 70 45 78 pr->iColumn, pEx
21767 70 72 2d 3e 69 54 61 62 6c 65 2c 20 74 61 72 67 pr->iTable, targ
21768 65 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 et,.
21769 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2176a 20 20 20 20 20 70 45 78 70 72 2d 3e 66 6c 61 67 pExpr->flag
2176b 73 20 26 20 45 50 5f 41 6e 79 41 66 66 29 3b 0a s & EP_AnyAff);.
2176c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 }. br
2176d 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 eak;. }. c
2176e 61 73 65 20 54 4b 5f 49 4e 54 45 47 45 52 3a 20 ase TK_INTEGER:
2176f 7b 0a 20 20 20 20 20 20 63 6f 64 65 49 6e 74 65 {. codeInte
21770 67 65 72 28 76 2c 20 70 45 78 70 72 2c 20 30 2c ger(v, pExpr, 0,
21771 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 target);.
21772 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
21773 20 63 61 73 65 20 54 4b 5f 46 4c 4f 41 54 3a 20 case TK_FLOAT:
21774 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
21775 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 !ExprHasProperty
21776 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 (pExpr, EP_IntVa
21777 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20 63 6f lue) );. co
21778 64 65 52 65 61 6c 28 76 2c 20 70 45 78 70 72 2d deReal(v, pExpr-
21779 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 30 2c 20 74 61 >u.zToken, 0, ta
2177a 72 67 65 74 29 3b 0a 20 20 20 20 20 20 62 72 65 rget);. bre
2177b 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 ak;. }. ca
2177c 73 65 20 54 4b 5f 53 54 52 49 4e 47 3a 20 7b 0a se TK_STRING: {.
2177d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 assert( !E
2177e 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 xprHasProperty(p
2177f 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 Expr, EP_IntValu
21780 65 29 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 e) );. sqli
21781 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c te3VdbeAddOp4(v,
21782 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 OP_String8, 0,
21783 74 61 72 67 65 74 2c 20 30 2c 20 70 45 78 70 72 target, 0, pExpr
21784 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 30 29 3b 0a ->u.zToken, 0);.
21785 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
21786 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e }. case TK_N
21787 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c ULL: {. sql
21788 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
21789 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 74 61 , OP_Null, 0, ta
2178a 72 67 65 74 29 3b 0a 20 20 20 20 20 20 62 72 65 rget);. bre
2178b 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 ak;. }.#ifnde
2178c 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c f SQLITE_OMIT_BL
2178d 4f 42 5f 4c 49 54 45 52 41 4c 0a 20 20 20 20 63 OB_LITERAL. c
2178e 61 73 65 20 54 4b 5f 42 4c 4f 42 3a 20 7b 0a 20 ase TK_BLOB: {.
2178f 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 int n;.
21790 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b const char *z;
21791 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 42 6c . char *zBl
21792 6f 62 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ob;. assert
21793 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 ( !ExprHasProper
21794 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 ty(pExpr, EP_Int
21795 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20 Value) );.
21796 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 75 assert( pExpr->u
21797 2e 7a 54 6f 6b 65 6e 5b 30 5d 3d 3d 27 78 27 20 .zToken[0]=='x'
21798 7c 7c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b || pExpr->u.zTok
21799 65 6e 5b 30 5d 3d 3d 27 58 27 20 29 3b 0a 20 20 en[0]=='X' );.
2179a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 assert( pExp
2179b 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 31 5d 3d 3d r->u.zToken[1]==
2179c 27 5c 27 27 20 29 3b 0a 20 20 20 20 20 20 7a 20 '\'' );. z
2179d 3d 20 26 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b = &pExpr->u.zTok
2179e 65 6e 5b 32 5d 3b 0a 20 20 20 20 20 20 6e 20 3d en[2];. n =
2179f 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
217a0 28 7a 29 20 2d 20 31 3b 0a 20 20 20 20 20 20 61 (z) - 1;. a
217a1 73 73 65 72 74 28 20 7a 5b 6e 5d 3d 3d 27 5c 27 ssert( z[n]=='\'
217a2 27 20 29 3b 0a 20 20 20 20 20 20 7a 42 6c 6f 62 ' );. zBlob
217a3 20 3d 20 73 71 6c 69 74 65 33 48 65 78 54 6f 42 = sqlite3HexToB
217a4 6c 6f 62 28 73 71 6c 69 74 65 33 56 64 62 65 44 lob(sqlite3VdbeD
217a5 62 28 76 29 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20 b(v), z, n);.
217a6 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
217a7 64 4f 70 34 28 76 2c 20 4f 50 5f 42 6c 6f 62 2c dOp4(v, OP_Blob,
217a8 20 6e 2f 32 2c 20 74 61 72 67 65 74 2c 20 30 2c n/2, target, 0,
217a9 20 7a 42 6c 6f 62 2c 20 50 34 5f 44 59 4e 41 4d zBlob, P4_DYNAM
217aa 49 43 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b IC);. break
217ab 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 ;. }.#endif.
217ac 20 20 20 63 61 73 65 20 54 4b 5f 56 41 52 49 41 case TK_VARIA
217ad 42 4c 45 3a 20 7b 0a 20 20 20 20 20 20 56 64 62 BLE: {. Vdb
217ae 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20 20 20 eOp *pOp;.
217af 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 assert( !ExprHas
217b0 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 Property(pExpr,
217b1 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a EP_IntValue) );.
217b2 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 assert( pE
217b3 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 21 3d 30 xpr->u.zToken!=0
217b4 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
217b5 28 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 ( pExpr->u.zToke
217b6 6e 5b 30 5d 21 3d 30 20 29 3b 0a 20 20 20 20 20 n[0]!=0 );.
217b7 20 69 66 28 20 70 45 78 70 72 2d 3e 75 2e 7a 54 if( pExpr->u.zT
217b8 6f 6b 65 6e 5b 31 5d 3d 3d 30 0a 20 20 20 20 20 oken[1]==0.
217b9 20 20 20 20 26 26 20 28 70 4f 70 20 3d 20 73 71 && (pOp = sq
217ba 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 76 lite3VdbeGetOp(v
217bb 2c 20 2d 31 29 29 2d 3e 6f 70 63 6f 64 65 3d 3d , -1))->opcode==
217bc 4f 50 5f 56 61 72 69 61 62 6c 65 0a 20 20 20 20 OP_Variable.
217bd 20 20 20 20 20 26 26 20 70 4f 70 2d 3e 70 31 2b && pOp->p1+
217be 70 4f 70 2d 3e 70 33 3d 3d 70 45 78 70 72 2d 3e pOp->p3==pExpr->
217bf 69 43 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 20 20 iColumn.
217c0 20 26 26 20 70 4f 70 2d 3e 70 32 2b 70 4f 70 2d && pOp->p2+pOp-
217c1 3e 70 33 3d 3d 74 61 72 67 65 74 0a 20 20 20 20 >p3==target.
217c2 20 20 20 20 20 26 26 20 70 4f 70 2d 3e 70 34 2e && pOp->p4.
217c3 7a 3d 3d 30 0a 20 20 20 20 20 20 29 7b 0a 20 20 z==0. ){.
217c4 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 /* If the
217c5 70 72 65 76 69 6f 75 73 20 69 6e 73 74 72 75 63 previous instruc
217c6 74 69 6f 6e 20 77 61 73 20 61 20 63 6f 70 79 20 tion was a copy
217c7 6f 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 of the previous
217c8 75 6e 6e 61 6d 65 64 0a 20 20 20 20 20 20 20 20 unnamed.
217c9 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69 6e 74 ** parameter int
217ca 6f 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 72 o the previous r
217cb 65 67 69 73 74 65 72 2c 20 74 68 65 6e 20 73 69 egister, then si
217cc 6d 70 6c 79 20 69 6e 63 72 65 6d 65 6e 74 20 74 mply increment t
217cd 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 65 he. ** re
217ce 70 65 61 74 20 63 6f 75 6e 74 20 6f 6e 20 74 68 peat count on th
217cf 65 20 70 72 69 6f 72 20 69 6e 73 74 72 75 63 74 e prior instruct
217d0 69 6f 6e 20 72 61 74 68 65 72 20 74 68 61 6e 20 ion rather than
217d1 6d 61 6b 69 6e 67 20 61 20 6e 65 77 0a 20 20 20 making a new.
217d2 20 20 20 20 20 2a 2a 20 69 6e 73 74 72 75 63 74 ** instruct
217d3 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a ion.. */.
217d4 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 70 33 2b pOp->p3+
217d5 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a +;. }else{.
217d6 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
217d7 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f dbeAddOp3(v, OP_
217d8 56 61 72 69 61 62 6c 65 2c 20 70 45 78 70 72 2d Variable, pExpr-
217d9 3e 69 43 6f 6c 75 6d 6e 2c 20 74 61 72 67 65 74 >iColumn, target
217da 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 69 66 , 1);. if
217db 28 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 ( pExpr->u.zToke
217dc 6e 5b 31 5d 21 3d 30 20 29 7b 0a 20 20 20 20 20 n[1]!=0 ){.
217dd 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
217de 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 ChangeP4(v, -1,
217df 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c pExpr->u.zToken,
217e0 20 30 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 0);. }.
217e1 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 }. bre
217e2 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 ak;. }. ca
217e3 73 65 20 54 4b 5f 52 45 47 49 53 54 45 52 3a 20 se TK_REGISTER:
217e4 7b 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 {. inReg =
217e5 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3b 0a 20 pExpr->iTable;.
217e6 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
217e7 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 53 }. case TK_AS
217e8 3a 20 7b 0a 20 20 20 20 20 20 69 6e 52 65 67 20 : {. inReg
217e9 3d 20 63 6f 64 65 41 6c 69 61 73 28 70 50 61 72 = codeAlias(pPar
217ea 73 65 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c se, pExpr->iTabl
217eb 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c e, pExpr->pLeft,
217ec 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 target);.
217ed 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 break;. }.#if
217ee 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
217ef 5f 43 41 53 54 0a 20 20 20 20 63 61 73 65 20 54 _CAST. case T
217f0 4b 5f 43 41 53 54 3a 20 7b 0a 20 20 20 20 20 20 K_CAST: {.
217f1 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 73 20 6f /* Expressions o
217f2 66 20 74 68 65 20 66 6f 72 6d 3a 20 20 20 43 41 f the form: CA
217f3 53 54 28 70 4c 65 66 74 20 41 53 20 74 6f 6b 65 ST(pLeft AS toke
217f4 6e 29 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 n) */. int
217f5 61 66 66 2c 20 74 6f 5f 6f 70 3b 0a 20 20 20 20 aff, to_op;.
217f6 20 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 inReg = sqlite
217f7 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 3ExprCodeTarget(
217f8 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 pParse, pExpr->p
217f9 4c 65 66 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 Left, target);.
217fa 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 assert( !Ex
217fb 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 prHasProperty(pE
217fc 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 xpr, EP_IntValue
217fd 29 20 29 3b 0a 20 20 20 20 20 20 61 66 66 20 3d ) );. aff =
217fe 20 73 71 6c 69 74 65 33 41 66 66 69 6e 69 74 79 sqlite3Affinity
217ff 54 79 70 65 28 70 45 78 70 72 2d 3e 75 2e 7a 54 Type(pExpr->u.zT
21800 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 74 6f 5f oken);. to_
21801 6f 70 20 3d 20 61 66 66 20 2d 20 53 51 4c 49 54 op = aff - SQLIT
21802 45 5f 41 46 46 5f 54 45 58 54 20 2b 20 4f 50 5f E_AFF_TEXT + OP_
21803 54 6f 54 65 78 74 3b 0a 20 20 20 20 20 20 61 73 ToText;. as
21804 73 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f sert( to_op==OP_
21805 54 6f 54 65 78 74 20 20 20 20 7c 7c 20 61 66 66 ToText || aff
21806 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 !=SQLITE_AFF_TEX
21807 54 20 20 20 20 29 3b 0a 20 20 20 20 20 20 61 73 T );. as
21808 73 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f sert( to_op==OP_
21809 54 6f 42 6c 6f 62 20 20 20 20 7c 7c 20 61 66 66 ToBlob || aff
2180a 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e !=SQLITE_AFF_NON
2180b 45 20 20 20 20 29 3b 0a 20 20 20 20 20 20 61 73 E );. as
2180c 73 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f sert( to_op==OP_
2180d 54 6f 4e 75 6d 65 72 69 63 20 7c 7c 20 61 66 66 ToNumeric || aff
2180e 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d !=SQLITE_AFF_NUM
2180f 45 52 49 43 20 29 3b 0a 20 20 20 20 20 20 61 73 ERIC );. as
21810 73 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f sert( to_op==OP_
21811 54 6f 49 6e 74 20 20 20 20 20 7c 7c 20 61 66 66 ToInt || aff
21812 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 !=SQLITE_AFF_INT
21813 45 47 45 52 20 29 3b 0a 20 20 20 20 20 20 61 73 EGER );. as
21814 73 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f sert( to_op==OP_
21815 54 6f 52 65 61 6c 20 20 20 20 7c 7c 20 61 66 66 ToReal || aff
21816 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 !=SQLITE_AFF_REA
21817 4c 20 20 20 20 29 3b 0a 20 20 20 20 20 20 74 65 L );. te
21818 73 74 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f stcase( to_op==O
21819 50 5f 54 6f 54 65 78 74 20 29 3b 0a 20 20 20 20 P_ToText );.
2181a 20 20 74 65 73 74 63 61 73 65 28 20 74 6f 5f 6f testcase( to_o
2181b 70 3d 3d 4f 50 5f 54 6f 42 6c 6f 62 20 29 3b 0a p==OP_ToBlob );.
2181c 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
2181d 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 4e 75 6d 65 to_op==OP_ToNume
2181e 72 69 63 20 29 3b 0a 20 20 20 20 20 20 74 65 73 ric );. tes
2181f 74 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 tcase( to_op==OP
21820 5f 54 6f 49 6e 74 20 29 3b 0a 20 20 20 20 20 20 _ToInt );.
21821 74 65 73 74 63 61 73 65 28 20 74 6f 5f 6f 70 3d testcase( to_op=
21822 3d 4f 50 5f 54 6f 52 65 61 6c 20 29 3b 0a 20 20 =OP_ToReal );.
21823 20 20 20 20 69 66 28 20 69 6e 52 65 67 21 3d 74 if( inReg!=t
21824 61 72 67 65 74 20 29 7b 0a 20 20 20 20 20 20 20 arget ){.
21825 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21826 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 p2(v, OP_SCopy,
21827 69 6e 52 65 67 2c 20 74 61 72 67 65 74 29 3b 0a inReg, target);.
21828 20 20 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 inReg =
21829 74 61 72 67 65 74 3b 0a 20 20 20 20 20 20 7d 0a target;. }.
2182a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
2182b 65 41 64 64 4f 70 31 28 76 2c 20 74 6f 5f 6f 70 eAddOp1(v, to_op
2182c 2c 20 69 6e 52 65 67 29 3b 0a 20 20 20 20 20 20 , inReg);.
2182d 74 65 73 74 63 61 73 65 28 20 75 73 65 64 41 73 testcase( usedAs
2182e 43 6f 6c 75 6d 6e 43 61 63 68 65 28 70 50 61 72 ColumnCache(pPar
2182f 73 65 2c 20 69 6e 52 65 67 2c 20 69 6e 52 65 67 se, inReg, inReg
21830 29 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 ) );. sqlit
21831 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e e3ExprCacheAffin
21832 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 ityChange(pParse
21833 2c 20 69 6e 52 65 67 2c 20 31 29 3b 0a 20 20 20 , inReg, 1);.
21834 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
21835 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
21836 5f 4f 4d 49 54 5f 43 41 53 54 20 2a 2f 0a 20 20 _OMIT_CAST */.
21837 20 20 63 61 73 65 20 54 4b 5f 4c 54 3a 0a 20 20 case TK_LT:.
21838 20 20 63 61 73 65 20 54 4b 5f 4c 45 3a 0a 20 20 case TK_LE:.
21839 20 20 63 61 73 65 20 54 4b 5f 47 54 3a 0a 20 20 case TK_GT:.
2183a 20 20 63 61 73 65 20 54 4b 5f 47 45 3a 0a 20 20 case TK_GE:.
2183b 20 20 63 61 73 65 20 54 4b 5f 4e 45 3a 0a 20 20 case TK_NE:.
2183c 20 20 63 61 73 65 20 54 4b 5f 45 51 3a 20 7b 0a case TK_EQ: {.
2183d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b assert( TK
2183e 5f 4c 54 3d 3d 4f 50 5f 4c 74 20 29 3b 0a 20 20 _LT==OP_Lt );.
2183f 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c assert( TK_L
21840 45 3d 3d 4f 50 5f 4c 65 20 29 3b 0a 20 20 20 20 E==OP_Le );.
21841 20 20 61 73 73 65 72 74 28 20 54 4b 5f 47 54 3d assert( TK_GT=
21842 3d 4f 50 5f 47 74 20 29 3b 0a 20 20 20 20 20 20 =OP_Gt );.
21843 61 73 73 65 72 74 28 20 54 4b 5f 47 45 3d 3d 4f assert( TK_GE==O
21844 50 5f 47 65 20 29 3b 0a 20 20 20 20 20 20 61 73 P_Ge );. as
21845 73 65 72 74 28 20 54 4b 5f 45 51 3d 3d 4f 50 5f sert( TK_EQ==OP_
21846 45 71 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 Eq );. asse
21847 72 74 28 20 54 4b 5f 4e 45 3d 3d 4f 50 5f 4e 65 rt( TK_NE==OP_Ne
21848 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
21849 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 54 20 29 3b se( op==TK_LT );
2184a 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
2184b 20 6f 70 3d 3d 54 4b 5f 4c 45 20 29 3b 0a 20 20 op==TK_LE );.
2184c 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 testcase( op
2184d 3d 3d 54 4b 5f 47 54 20 29 3b 0a 20 20 20 20 20 ==TK_GT );.
2184e 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
2184f 4b 5f 47 45 20 29 3b 0a 20 20 20 20 20 20 74 65 K_GE );. te
21850 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 45 stcase( op==TK_E
21851 51 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 Q );. testc
21852 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 45 20 29 ase( op==TK_NE )
21853 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 ;. codeComp
21854 61 72 65 4f 70 65 72 61 6e 64 73 28 70 50 61 72 areOperands(pPar
21855 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 se, pExpr->pLeft
21856 2c 20 26 72 31 2c 20 26 72 65 67 46 72 65 65 31 , &r1, ®Free1
21857 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
21858 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21859 20 20 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68 pExpr->pRigh
2185a 74 2c 20 26 72 32 2c 20 26 72 65 67 46 72 65 65 t, &r2, ®Free
2185b 32 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 2);. codeCo
2185c 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20 70 45 mpare(pParse, pE
2185d 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70 xpr->pLeft, pExp
2185e 72 2d 3e 70 52 69 67 68 74 2c 20 6f 70 2c 0a 20 r->pRight, op,.
2185f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21860 20 72 31 2c 20 72 32 2c 20 69 6e 52 65 67 2c 20 r1, r2, inReg,
21861 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 29 3b SQLITE_STOREP2);
21862 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
21863 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a regFree1==0 );.
21864 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
21865 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 regFree2==0 );.
21866 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
21867 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 53 }. case TK_IS
21868 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 53 :. case TK_IS
21869 4e 4f 54 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 NOT: {. tes
2186a 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 49 53 tcase( op==TK_IS
2186b 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
2186c 73 65 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 4f 54 se( op==TK_ISNOT
2186d 20 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f );. codeCo
2186e 6d 70 61 72 65 4f 70 65 72 61 6e 64 73 28 70 50 mpareOperands(pP
2186f 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 arse, pExpr->pLe
21870 66 74 2c 20 26 72 31 2c 20 26 72 65 67 46 72 65 ft, &r1, ®Fre
21871 65 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 e1,.
21872 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21873 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 52 69 pExpr->pRi
21874 67 68 74 2c 20 26 72 32 2c 20 26 72 65 67 46 72 ght, &r2, ®Fr
21875 65 65 32 29 3b 0a 20 20 20 20 20 20 6f 70 20 3d ee2);. op =
21876 20 28 6f 70 3d 3d 54 4b 5f 49 53 29 20 3f 20 54 (op==TK_IS) ? T
21877 4b 5f 45 51 20 3a 20 54 4b 5f 4e 45 3b 0a 20 20 K_EQ : TK_NE;.
21878 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 codeCompare(
21879 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 pParse, pExpr->p
2187a 4c 65 66 74 2c 20 70 45 78 70 72 2d 3e 70 52 69 Left, pExpr->pRi
2187b 67 68 74 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20 ght, op,.
2187c 20 20 20 20 20 20 20 20 20 20 20 72 31 2c 20 72 r1, r
2187d 32 2c 20 69 6e 52 65 67 2c 20 53 51 4c 49 54 45 2, inReg, SQLITE
2187e 5f 53 54 4f 52 45 50 32 20 7c 20 53 51 4c 49 54 _STOREP2 | SQLIT
2187f 45 5f 4e 55 4c 4c 45 51 29 3b 0a 20 20 20 20 20 E_NULLEQ);.
21880 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 testcase( regFr
21881 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 ee1==0 );.
21882 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 testcase( regFre
21883 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 e2==0 );. b
21884 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
21885 63 61 73 65 20 54 4b 5f 41 4e 44 3a 0a 20 20 20 case TK_AND:.
21886 20 63 61 73 65 20 54 4b 5f 4f 52 3a 0a 20 20 20 case TK_OR:.
21887 20 63 61 73 65 20 54 4b 5f 50 4c 55 53 3a 0a 20 case TK_PLUS:.
21888 20 20 20 63 61 73 65 20 54 4b 5f 53 54 41 52 3a case TK_STAR:
21889 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4d 49 4e . case TK_MIN
2188a 55 53 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f US:. case TK_
2188b 52 45 4d 3a 0a 20 20 20 20 63 61 73 65 20 54 4b REM:. case TK
2188c 5f 42 49 54 41 4e 44 3a 0a 20 20 20 20 63 61 73 _BITAND:. cas
2188d 65 20 54 4b 5f 42 49 54 4f 52 3a 0a 20 20 20 20 e TK_BITOR:.
2188e 63 61 73 65 20 54 4b 5f 53 4c 41 53 48 3a 0a 20 case TK_SLASH:.
2188f 20 20 20 63 61 73 65 20 54 4b 5f 4c 53 48 49 46 case TK_LSHIF
21890 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 T:. case TK_R
21891 53 48 49 46 54 3a 20 0a 20 20 20 20 63 61 73 65 SHIFT: . case
21892 20 54 4b 5f 43 4f 4e 43 41 54 3a 20 7b 0a 20 20 TK_CONCAT: {.
21893 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 41 assert( TK_A
21894 4e 44 3d 3d 4f 50 5f 41 6e 64 20 29 3b 0a 20 20 ND==OP_And );.
21895 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4f assert( TK_O
21896 52 3d 3d 4f 50 5f 4f 72 20 29 3b 0a 20 20 20 20 R==OP_Or );.
21897 20 20 61 73 73 65 72 74 28 20 54 4b 5f 50 4c 55 assert( TK_PLU
21898 53 3d 3d 4f 50 5f 41 64 64 20 29 3b 0a 20 20 20 S==OP_Add );.
21899 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4d 49 assert( TK_MI
2189a 4e 55 53 3d 3d 4f 50 5f 53 75 62 74 72 61 63 74 NUS==OP_Subtract
2189b 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
2189c 28 20 54 4b 5f 52 45 4d 3d 3d 4f 50 5f 52 65 6d ( TK_REM==OP_Rem
2189d 61 69 6e 64 65 72 20 29 3b 0a 20 20 20 20 20 20 ainder );.
2189e 61 73 73 65 72 74 28 20 54 4b 5f 42 49 54 41 4e assert( TK_BITAN
2189f 44 3d 3d 4f 50 5f 42 69 74 41 6e 64 20 29 3b 0a D==OP_BitAnd );.
218a0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b assert( TK
218a1 5f 42 49 54 4f 52 3d 3d 4f 50 5f 42 69 74 4f 72 _BITOR==OP_BitOr
218a2 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
218a3 28 20 54 4b 5f 53 4c 41 53 48 3d 3d 4f 50 5f 44 ( TK_SLASH==OP_D
218a4 69 76 69 64 65 20 29 3b 0a 20 20 20 20 20 20 61 ivide );. a
218a5 73 73 65 72 74 28 20 54 4b 5f 4c 53 48 49 46 54 ssert( TK_LSHIFT
218a6 3d 3d 4f 50 5f 53 68 69 66 74 4c 65 66 74 20 29 ==OP_ShiftLeft )
218a7 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
218a8 54 4b 5f 52 53 48 49 46 54 3d 3d 4f 50 5f 53 68 TK_RSHIFT==OP_Sh
218a9 69 66 74 52 69 67 68 74 20 29 3b 0a 20 20 20 20 iftRight );.
218aa 20 20 61 73 73 65 72 74 28 20 54 4b 5f 43 4f 4e assert( TK_CON
218ab 43 41 54 3d 3d 4f 50 5f 43 6f 6e 63 61 74 20 29 CAT==OP_Concat )
218ac 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
218ad 28 20 6f 70 3d 3d 54 4b 5f 41 4e 44 20 29 3b 0a ( op==TK_AND );.
218ae 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
218af 6f 70 3d 3d 54 4b 5f 4f 52 20 29 3b 0a 20 20 20 op==TK_OR );.
218b0 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d testcase( op=
218b1 3d 54 4b 5f 50 4c 55 53 20 29 3b 0a 20 20 20 20 =TK_PLUS );.
218b2 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
218b3 54 4b 5f 4d 49 4e 55 53 20 29 3b 0a 20 20 20 20 TK_MINUS );.
218b4 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
218b5 54 4b 5f 52 45 4d 20 29 3b 0a 20 20 20 20 20 20 TK_REM );.
218b6 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b testcase( op==TK
218b7 5f 42 49 54 41 4e 44 20 29 3b 0a 20 20 20 20 20 _BITAND );.
218b8 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
218b9 4b 5f 42 49 54 4f 52 20 29 3b 0a 20 20 20 20 20 K_BITOR );.
218ba 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
218bb 4b 5f 53 4c 41 53 48 20 29 3b 0a 20 20 20 20 20 K_SLASH );.
218bc 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
218bd 4b 5f 4c 53 48 49 46 54 20 29 3b 0a 20 20 20 20 K_LSHIFT );.
218be 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
218bf 54 4b 5f 52 53 48 49 46 54 20 29 3b 0a 20 20 20 TK_RSHIFT );.
218c0 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d testcase( op=
218c1 3d 54 4b 5f 43 4f 4e 43 41 54 20 29 3b 0a 20 20 =TK_CONCAT );.
218c2 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 r1 = sqlite3
218c3 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 ExprCodeTemp(pPa
218c4 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 rse, pExpr->pLef
218c5 74 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 t, ®Free1);.
218c6 20 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74 65 r2 = sqlite
218c7 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 3ExprCodeTemp(pP
218c8 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 arse, pExpr->pRi
218c9 67 68 74 2c 20 26 72 65 67 46 72 65 65 32 29 3b ght, ®Free2);
218ca 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
218cb 62 65 41 64 64 4f 70 33 28 76 2c 20 6f 70 2c 20 beAddOp3(v, op,
218cc 72 32 2c 20 72 31 2c 20 74 61 72 67 65 74 29 3b r2, r1, target);
218cd 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
218ce 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a regFree1==0 );.
218cf 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
218d0 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 regFree2==0 );.
218d1 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
218d2 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55 4d }. case TK_UM
218d3 49 4e 55 53 3a 20 7b 0a 20 20 20 20 20 20 45 78 INUS: {. Ex
218d4 70 72 20 2a 70 4c 65 66 74 20 3d 20 70 45 78 70 pr *pLeft = pExp
218d5 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 r->pLeft;.
218d6 61 73 73 65 72 74 28 20 70 4c 65 66 74 20 29 3b assert( pLeft );
218d7 0a 20 20 20 20 20 20 69 66 28 20 70 4c 65 66 74 . if( pLeft
218d8 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 ->op==TK_FLOAT )
218d9 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 {. assert
218da 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 ( !ExprHasProper
218db 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 ty(pExpr, EP_Int
218dc 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20 Value) );.
218dd 20 20 63 6f 64 65 52 65 61 6c 28 76 2c 20 70 4c codeReal(v, pL
218de 65 66 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 31 eft->u.zToken, 1
218df 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 , target);.
218e0 20 7d 65 6c 73 65 20 69 66 28 20 70 4c 65 66 74 }else if( pLeft
218e1 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 ->op==TK_INTEGER
218e2 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 ){. code
218e3 49 6e 74 65 67 65 72 28 76 2c 20 70 4c 65 66 74 Integer(v, pLeft
218e4 2c 20 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 , 1, target);.
218e5 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
218e6 20 20 20 72 65 67 46 72 65 65 31 20 3d 20 72 31 regFree1 = r1
218e7 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d = sqlite3GetTem
218e8 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 pReg(pParse);.
218e9 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
218ea 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e eAddOp2(v, OP_In
218eb 74 65 67 65 72 2c 20 30 2c 20 72 31 29 3b 0a 20 teger, 0, r1);.
218ec 20 20 20 20 20 20 20 72 32 20 3d 20 73 71 6c 69 r2 = sqli
218ed 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 te3ExprCodeTemp(
218ee 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 pParse, pExpr->p
218ef 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65 32 29 Left, ®Free2)
218f0 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
218f1 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 3VdbeAddOp3(v, O
218f2 50 5f 53 75 62 74 72 61 63 74 2c 20 72 32 2c 20 P_Subtract, r2,
218f3 72 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 r1, target);.
218f4 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 testcase( r
218f5 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 egFree2==0 );.
218f6 20 20 20 20 7d 0a 20 20 20 20 20 20 69 6e 52 65 }. inRe
218f7 67 20 3d 20 74 61 72 67 65 74 3b 0a 20 20 20 20 g = target;.
218f8 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
218f9 20 20 20 63 61 73 65 20 54 4b 5f 42 49 54 4e 4f case TK_BITNO
218fa 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e T:. case TK_N
218fb 4f 54 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 OT: {. asse
218fc 72 74 28 20 54 4b 5f 42 49 54 4e 4f 54 3d 3d 4f rt( TK_BITNOT==O
218fd 50 5f 42 69 74 4e 6f 74 20 29 3b 0a 20 20 20 20 P_BitNot );.
218fe 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e 4f 54 assert( TK_NOT
218ff 3d 3d 4f 50 5f 4e 6f 74 20 29 3b 0a 20 20 20 20 ==OP_Not );.
21900 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
21901 54 4b 5f 42 49 54 4e 4f 54 20 29 3b 0a 20 20 20 TK_BITNOT );.
21902 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d testcase( op=
21903 3d 54 4b 5f 4e 4f 54 20 29 3b 0a 20 20 20 20 20 =TK_NOT );.
21904 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70 r1 = sqlite3Exp
21905 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 rCodeTemp(pParse
21906 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 , pExpr->pLeft,
21907 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 ®Free1);.
21908 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 testcase( regF
21909 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 ree1==0 );.
2190a 20 69 6e 52 65 67 20 3d 20 74 61 72 67 65 74 3b inReg = target;
2190b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
2190c 62 65 41 64 64 4f 70 32 28 76 2c 20 6f 70 2c 20 beAddOp2(v, op,
2190d 72 31 2c 20 69 6e 52 65 67 29 3b 0a 20 20 20 20 r1, inReg);.
2190e 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
2190f 20 20 20 63 61 73 65 20 54 4b 5f 49 53 4e 55 4c case TK_ISNUL
21910 4c 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e L:. case TK_N
21911 4f 54 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 OTNULL: {.
21912 69 6e 74 20 61 64 64 72 3b 0a 20 20 20 20 20 20 int addr;.
21913 61 73 73 65 72 74 28 20 54 4b 5f 49 53 4e 55 4c assert( TK_ISNUL
21914 4c 3d 3d 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a L==OP_IsNull );.
21915 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b assert( TK
21916 5f 4e 4f 54 4e 55 4c 4c 3d 3d 4f 50 5f 4e 6f 74 _NOTNULL==OP_Not
21917 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 20 20 74 65 Null );. te
21918 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 49 stcase( op==TK_I
21919 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 74 SNULL );. t
2191a 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f estcase( op==TK_
2191b 4e 4f 54 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 NOTNULL );.
2191c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
2191d 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 p2(v, OP_Integer
2191e 2c 20 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 , 1, target);.
2191f 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 r1 = sqlite3
21920 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 ExprCodeTemp(pPa
21921 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 rse, pExpr->pLef
21922 74 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 t, ®Free1);.
21923 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 testcase( r
21924 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 egFree1==0 );.
21925 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 addr = sqlit
21926 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 e3VdbeAddOp1(v,
21927 6f 70 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73 op, r1);. s
21928 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
21929 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 74 (v, OP_AddImm, t
2192a 61 72 67 65 74 2c 20 2d 31 29 3b 0a 20 20 20 20 arget, -1);.
2192b 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d sqlite3VdbeJum
2192c 70 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a pHere(v, addr);.
2192d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
2192e 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 }. case TK_A
2192f 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 20 7b 0a 20 GG_FUNCTION: {.
21930 20 20 20 20 20 41 67 67 49 6e 66 6f 20 2a 70 49 AggInfo *pI
21931 6e 66 6f 20 3d 20 70 45 78 70 72 2d 3e 70 41 67 nfo = pExpr->pAg
21932 67 49 6e 66 6f 3b 0a 20 20 20 20 20 20 69 66 28 gInfo;. if(
21933 20 70 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20 20 pInfo==0 ){.
21934 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 assert( !Ex
21935 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 prHasProperty(pE
21936 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 xpr, EP_IntValue
21937 29 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c ) );. sql
21938 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
21939 72 73 65 2c 20 22 6d 69 73 75 73 65 20 6f 66 20 rse, "misuse of
2193a 61 67 67 72 65 67 61 74 65 3a 20 25 73 28 29 22 aggregate: %s()"
2193b 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 , pExpr->u.zToke
2193c 6e 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b n);. }else{
2193d 0a 20 20 20 20 20 20 20 20 69 6e 52 65 67 20 3d . inReg =
2193e 20 70 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 70 45 pInfo->aFunc[pE
2193f 78 70 72 2d 3e 69 41 67 67 5d 2e 69 4d 65 6d 3b xpr->iAgg].iMem;
21940 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 . }. b
21941 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
21942 63 61 73 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 case TK_CONST_FU
21943 4e 43 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f NC:. case TK_
21944 46 55 4e 43 54 49 4f 4e 3a 20 7b 0a 20 20 20 20 FUNCTION: {.
21945 20 20 45 78 70 72 4c 69 73 74 20 2a 70 46 61 72 ExprList *pFar
21946 67 3b 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 g; /* List
21947 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 of function arg
21948 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 uments */.
21949 69 6e 74 20 6e 46 61 72 67 3b 20 20 20 20 20 20 int nFarg;
2194a 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
2194b 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 of function arg
2194c 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 uments */.
2194d 46 75 6e 63 44 65 66 20 2a 70 44 65 66 3b 20 20 FuncDef *pDef;
2194e 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 75 /* The fu
2194f 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f nction definitio
21950 6e 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 n object */.
21951 20 20 69 6e 74 20 6e 49 64 3b 20 20 20 20 20 20 int nId;
21952 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 /* Leng
21953 74 68 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 th of the functi
21954 6f 6e 20 6e 61 6d 65 20 69 6e 20 62 79 74 65 73 on name in bytes
21955 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 */. const
21956 63 68 61 72 20 2a 7a 49 64 3b 20 20 20 20 20 20 char *zId;
21957 20 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e /* The function
21958 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20 69 name */. i
21959 6e 74 20 63 6f 6e 73 74 4d 61 73 6b 20 3d 20 30 nt constMask = 0
2195a 3b 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 ; /* Mask of
2195b 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 function argume
2195c 6e 74 73 20 74 68 61 74 20 61 72 65 20 63 6f 6e nts that are con
2195d 73 74 61 6e 74 20 2a 2f 0a 20 20 20 20 20 20 69 stant */. i
2195e 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 nt i;
2195f 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f /* Loop co
21960 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 75 unter */. u
21961 38 20 65 6e 63 20 3d 20 45 4e 43 28 64 62 29 3b 8 enc = ENC(db);
21962 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 65 78 /* The tex
21963 74 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 t encoding used
21964 62 79 20 74 68 69 73 20 64 61 74 61 62 61 73 65 by this database
21965 20 2a 2f 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 */. CollSe
21966 71 20 2a 70 43 6f 6c 6c 20 3d 20 30 3b 20 20 20 q *pColl = 0;
21967 20 2f 2a 20 41 20 63 6f 6c 6c 61 74 69 6e 67 20 /* A collating
21968 73 65 71 75 65 6e 63 65 20 2a 2f 0a 0a 20 20 20 sequence */..
21969 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 assert( !Expr
2196a 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 HasProperty(pExp
2196b 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 r, EP_xIsSelect)
2196c 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
2196d 73 65 28 20 6f 70 3d 3d 54 4b 5f 43 4f 4e 53 54 se( op==TK_CONST
2196e 5f 46 55 4e 43 20 29 3b 0a 20 20 20 20 20 20 74 _FUNC );. t
2196f 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f estcase( op==TK_
21970 46 55 4e 43 54 49 4f 4e 20 29 3b 0a 20 20 20 20 FUNCTION );.
21971 20 20 69 66 28 20 45 78 70 72 48 61 73 41 6e 79 if( ExprHasAny
21972 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 Property(pExpr,
21973 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b EP_TokenOnly) ){
21974 0a 20 20 20 20 20 20 20 20 70 46 61 72 67 20 3d . pFarg =
21975 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0;. }else{
21976 0a 20 20 20 20 20 20 20 20 70 46 61 72 67 20 3d . pFarg =
21977 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b pExpr->x.pList;
21978 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6e . }. n
21979 46 61 72 67 20 3d 20 70 46 61 72 67 20 3f 20 70 Farg = pFarg ? p
2197a 46 61 72 67 2d 3e 6e 45 78 70 72 20 3a 20 30 3b Farg->nExpr : 0;
2197b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 . assert( !
2197c 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 ExprHasProperty(
2197d 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c pExpr, EP_IntVal
2197e 75 65 29 20 29 3b 0a 20 20 20 20 20 20 7a 49 64 ue) );. zId
2197f 20 3d 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b = pExpr->u.zTok
21980 65 6e 3b 0a 20 20 20 20 20 20 6e 49 64 20 3d 20 en;. nId =
21981 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
21982 7a 49 64 29 3b 0a 20 20 20 20 20 20 70 44 65 66 zId);. pDef
21983 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75 = sqlite3FindFu
21984 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 49 64 2c 20 nction(db, zId,
21985 6e 49 64 2c 20 6e 46 61 72 67 2c 20 65 6e 63 2c nId, nFarg, enc,
21986 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 0);. if( p
21987 44 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 Def==0 ){.
21988 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
21989 67 28 70 50 61 72 73 65 2c 20 22 75 6e 6b 6e 6f g(pParse, "unkno
2198a 77 6e 20 66 75 6e 63 74 69 6f 6e 3a 20 25 2e 2a wn function: %.*
2198b 73 28 29 22 2c 20 6e 49 64 2c 20 7a 49 64 29 3b s()", nId, zId);
2198c 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
2198d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
2198e 28 20 70 46 61 72 67 20 29 7b 0a 20 20 20 20 20 ( pFarg ){.
2198f 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 r1 = sqlite3G
21990 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 etTempRange(pPar
21991 73 65 2c 20 6e 46 61 72 67 29 3b 0a 20 20 20 20 se, nFarg);.
21992 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 sqlite3ExprC
21993 61 63 68 65 50 75 73 68 28 70 50 61 72 73 65 29 achePush(pParse)
21994 3b 20 20 20 20 20 2f 2a 20 54 69 63 6b 65 74 20 ; /* Ticket
21995 32 65 61 32 34 32 35 64 33 34 62 65 20 2a 2f 0a 2ea2425d34be */.
21996 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 sqlite3E
21997 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73 74 28 xprCodeExprList(
21998 70 50 61 72 73 65 2c 20 70 46 61 72 67 2c 20 72 pParse, pFarg, r
21999 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73 1, 1);. s
2199a 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 qlite3ExprCacheP
2199b 6f 70 28 70 50 61 72 73 65 2c 20 31 29 3b 20 20 op(pParse, 1);
2199c 20 2f 2a 20 54 69 63 6b 65 74 20 32 65 61 32 34 /* Ticket 2ea24
2199d 32 35 64 33 34 62 65 20 2a 2f 0a 20 20 20 20 20 25d34be */.
2199e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
2199f 72 31 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a r1 = 0;. }.
219a0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
219a1 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
219a2 0a 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 69 62 . /* Possib
219a3 6c 79 20 6f 76 65 72 6c 6f 61 64 20 74 68 65 20 ly overload the
219a4 66 75 6e 63 74 69 6f 6e 20 69 66 20 74 68 65 20 function if the
219a5 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 first argument i
219a6 73 0a 20 20 20 20 20 20 2a 2a 20 61 20 76 69 72 s. ** a vir
219a7 74 75 61 6c 20 74 61 62 6c 65 20 63 6f 6c 75 6d tual table colum
219a8 6e 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 n.. **.
219a9 20 20 2a 2a 20 46 6f 72 20 69 6e 66 69 78 20 66 ** For infix f
219aa 75 6e 63 74 69 6f 6e 73 20 28 4c 49 4b 45 2c 20 unctions (LIKE,
219ab 47 4c 4f 42 2c 20 52 45 47 45 58 50 2c 20 61 6e GLOB, REGEXP, an
219ac 64 20 4d 41 54 43 48 29 20 75 73 65 20 74 68 65 d MATCH) use the
219ad 0a 20 20 20 20 20 20 2a 2a 20 73 65 63 6f 6e 64 . ** second
219ae 20 61 72 67 75 6d 65 6e 74 2c 20 6e 6f 74 20 74 argument, not t
219af 68 65 20 66 69 72 73 74 2c 20 61 73 20 74 68 65 he first, as the
219b0 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 65 73 argument to tes
219b1 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 73 65 t to. ** se
219b2 65 20 69 66 20 69 74 20 69 73 20 61 20 63 6f 6c e if it is a col
219b3 75 6d 6e 20 69 6e 20 61 20 76 69 72 74 75 61 6c umn in a virtual
219b4 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 69 73 table. This is
219b5 20 64 6f 6e 65 20 62 65 63 61 75 73 65 0a 20 20 done because.
219b6 20 20 20 20 2a 2a 20 74 68 65 20 6c 65 66 74 20 ** the left
219b7 6f 70 65 72 61 6e 64 20 6f 66 20 69 6e 66 69 78 operand of infix
219b8 20 66 75 6e 63 74 69 6f 6e 73 20 28 74 68 65 20 functions (the
219b9 6f 70 65 72 61 6e 64 20 77 65 20 77 61 6e 74 20 operand we want
219ba 74 6f 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 to. ** cont
219bb 72 6f 6c 20 6f 76 65 72 6c 6f 61 64 69 6e 67 29 rol overloading)
219bc 20 65 6e 64 73 20 75 70 20 61 73 20 74 68 65 20 ends up as the
219bd 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
219be 74 6f 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 to the. **
219bf 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 65 function. The e
219c0 78 70 72 65 73 73 69 6f 6e 20 22 41 20 67 6c 6f xpression "A glo
219c1 62 20 42 22 20 69 73 20 65 71 75 69 76 61 6c 65 b B" is equivale
219c2 6e 74 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20 nt to . **
219c3 22 67 6c 6f 62 28 42 2c 41 29 2e 20 20 57 65 20 "glob(B,A). We
219c4 77 61 6e 74 20 74 6f 20 75 73 65 20 74 68 65 20 want to use the
219c5 41 20 69 6e 20 22 41 20 67 6c 6f 62 20 42 22 20 A in "A glob B"
219c6 74 6f 20 74 65 73 74 0a 20 20 20 20 20 20 2a 2a to test. **
219c7 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e 20 6f 76 for function ov
219c8 65 72 6c 6f 61 64 69 6e 67 2e 20 20 42 75 74 20 erloading. But
219c9 77 65 20 75 73 65 20 74 68 65 20 42 20 74 65 72 we use the B ter
219ca 6d 20 69 6e 20 22 67 6c 6f 62 28 42 2c 41 29 22 m in "glob(B,A)"
219cb 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
219cc 20 69 66 28 20 6e 46 61 72 67 3e 3d 32 20 26 26 if( nFarg>=2 &&
219cd 20 28 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 26 (pExpr->flags &
219ce 20 45 50 5f 49 6e 66 69 78 46 75 6e 63 29 20 29 EP_InfixFunc) )
219cf 7b 0a 20 20 20 20 20 20 20 20 70 44 65 66 20 3d {. pDef =
219d0 20 73 71 6c 69 74 65 33 56 74 61 62 4f 76 65 72 sqlite3VtabOver
219d1 6c 6f 61 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c loadFunction(db,
219d2 20 70 44 65 66 2c 20 6e 46 61 72 67 2c 20 70 46 pDef, nFarg, pF
219d3 61 72 67 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 29 arg->a[1].pExpr)
219d4 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 ;. }else if
219d5 28 20 6e 46 61 72 67 3e 30 20 29 7b 0a 20 20 20 ( nFarg>0 ){.
219d6 20 20 20 20 20 70 44 65 66 20 3d 20 73 71 6c 69 pDef = sqli
219d7 74 65 33 56 74 61 62 4f 76 65 72 6c 6f 61 64 46 te3VtabOverloadF
219d8 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70 44 65 66 unction(db, pDef
219d9 2c 20 6e 46 61 72 67 2c 20 70 46 61 72 67 2d 3e , nFarg, pFarg->
219da 61 5b 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 a[0].pExpr);.
219db 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 }.#endif.
219dc 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 46 61 for(i=0; i<nFa
219dd 72 67 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 rg; i++){.
219de 20 20 69 66 28 20 69 3c 33 32 20 26 26 20 73 71 if( i<32 && sq
219df 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 lite3ExprIsConst
219e0 61 6e 74 28 70 46 61 72 67 2d 3e 61 5b 69 5d 2e ant(pFarg->a[i].
219e1 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20 pExpr) ){.
219e2 20 20 20 20 63 6f 6e 73 74 4d 61 73 6b 20 7c 3d constMask |=
219e3 20 28 31 3c 3c 69 29 3b 0a 20 20 20 20 20 20 20 (1<<i);.
219e4 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 }. if( (
219e5 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53 51 pDef->flags & SQ
219e6 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f LITE_FUNC_NEEDCO
219e7 4c 4c 29 21 3d 30 20 26 26 20 21 70 43 6f 6c 6c LL)!=0 && !pColl
219e8 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 43 ){. pC
219e9 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 oll = sqlite3Exp
219ea 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c rCollSeq(pParse,
219eb 20 70 46 61 72 67 2d 3e 61 5b 69 5d 2e 70 45 78 pFarg->a[i].pEx
219ec 70 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 pr);. }.
219ed 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
219ee 20 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53 pDef->flags & S
219ef 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 QLITE_FUNC_NEEDC
219f0 4f 4c 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 69 OLL ){. i
219f1 66 28 20 21 70 43 6f 6c 6c 20 29 20 70 43 6f 6c f( !pColl ) pCol
219f2 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c l = db->pDfltCol
219f3 6c 3b 20 0a 20 20 20 20 20 20 20 20 73 71 6c 69 l; . sqli
219f4 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c te3VdbeAddOp4(v,
219f5 20 4f 50 5f 43 6f 6c 6c 53 65 71 2c 20 30 2c 20 OP_CollSeq, 0,
219f6 30 2c 20 30 2c 20 28 63 68 61 72 20 2a 29 70 43 0, 0, (char *)pC
219f7 6f 6c 6c 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 29 oll, P4_COLLSEQ)
219f8 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
219f9 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
219fa 34 28 76 2c 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 4(v, OP_Function
219fb 2c 20 63 6f 6e 73 74 4d 61 73 6b 2c 20 72 31 2c , constMask, r1,
219fc 20 74 61 72 67 65 74 2c 0a 20 20 20 20 20 20 20 target,.
219fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
219fe 20 28 63 68 61 72 2a 29 70 44 65 66 2c 20 50 34 (char*)pDef, P4
219ff 5f 46 55 4e 43 44 45 46 29 3b 0a 20 20 20 20 20 _FUNCDEF);.
21a00 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
21a01 67 65 50 35 28 76 2c 20 28 75 38 29 6e 46 61 72 geP5(v, (u8)nFar
21a02 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 46 g);. if( nF
21a03 61 72 67 20 29 7b 0a 20 20 20 20 20 20 20 20 73 arg ){. s
21a04 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d qlite3ReleaseTem
21a05 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 pRange(pParse, r
21a06 31 2c 20 6e 46 61 72 67 29 3b 0a 20 20 20 20 20 1, nFarg);.
21a07 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
21a08 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 ExprCacheAffinit
21a09 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 yChange(pParse,
21a0a 72 31 2c 20 6e 46 61 72 67 29 3b 0a 20 20 20 20 r1, nFarg);.
21a0b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 break;. }.#
21a0c 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
21a0d 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20 20 20 IT_SUBQUERY.
21a0e 63 61 73 65 20 54 4b 5f 45 58 49 53 54 53 3a 0a case TK_EXISTS:.
21a0f 20 20 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 case TK_SELE
21a10 43 54 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 CT: {. test
21a11 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 45 58 49 case( op==TK_EXI
21a12 53 54 53 20 29 3b 0a 20 20 20 20 20 20 74 65 73 STS );. tes
21a13 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 53 45 tcase( op==TK_SE
21a14 4c 45 43 54 20 29 3b 0a 20 20 20 20 20 20 69 6e LECT );. in
21a15 52 65 67 20 3d 20 73 71 6c 69 74 65 33 43 6f 64 Reg = sqlite3Cod
21a16 65 53 75 62 73 65 6c 65 63 74 28 70 50 61 72 73 eSubselect(pPars
21a17 65 2c 20 70 45 78 70 72 2c 20 30 2c 20 30 29 3b e, pExpr, 0, 0);
21a18 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
21a19 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f }. case TK_
21a1a 49 4e 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 IN: {. int
21a1b 72 4e 6f 74 46 6f 75 6e 64 20 3d 20 30 3b 0a 20 rNotFound = 0;.
21a1c 20 20 20 20 20 69 6e 74 20 72 4d 61 79 48 61 76 int rMayHav
21a1d 65 4e 75 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 20 eNull = 0;.
21a1e 20 69 6e 74 20 6a 32 2c 20 6a 33 2c 20 6a 34 2c int j2, j3, j4,
21a1f 20 6a 35 3b 0a 20 20 20 20 20 20 63 68 61 72 20 j5;. char
21a20 61 66 66 69 6e 69 74 79 3b 0a 20 20 20 20 20 20 affinity;.
21a21 69 6e 74 20 65 54 79 70 65 3b 0a 0a 20 20 20 20 int eType;..
21a22 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e VdbeNoopCommen
21a23 74 28 28 76 2c 20 22 62 65 67 69 6e 20 49 4e 20 t((v, "begin IN
21a24 65 78 70 72 20 72 25 64 22 2c 20 74 61 72 67 65 expr r%d", targe
21a25 74 29 29 3b 0a 20 20 20 20 20 20 65 54 79 70 65 t));. eType
21a26 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e = sqlite3FindIn
21a27 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70 45 Index(pParse, pE
21a28 78 70 72 2c 20 26 72 4d 61 79 48 61 76 65 4e 75 xpr, &rMayHaveNu
21a29 6c 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 ll);. if( r
21a2a 4d 61 79 48 61 76 65 4e 75 6c 6c 20 29 7b 0a 20 MayHaveNull ){.
21a2b 20 20 20 20 20 20 20 72 4e 6f 74 46 6f 75 6e 64 rNotFound
21a2c 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 = ++pParse->nMe
21a2d 6d 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 m;. }..
21a2e 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 /* Figure out
21a2f 74 68 65 20 61 66 66 69 6e 69 74 79 20 74 6f 20 the affinity to
21a30 75 73 65 20 74 6f 20 63 72 65 61 74 65 20 61 20 use to create a
21a31 6b 65 79 20 66 72 6f 6d 20 74 68 65 20 72 65 73 key from the res
21a32 75 6c 74 73 0a 20 20 20 20 20 20 2a 2a 20 6f 66 ults. ** of
21a33 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 2e the expression.
21a34 20 61 66 66 69 6e 69 74 79 53 74 72 20 73 74 6f affinityStr sto
21a35 72 65 73 20 61 20 73 74 61 74 69 63 20 73 74 72 res a static str
21a36 69 6e 67 20 73 75 69 74 61 62 6c 65 20 66 6f 72 ing suitable for
21a37 0a 20 20 20 20 20 20 2a 2a 20 50 34 20 6f 66 20 . ** P4 of
21a38 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2e 0a 20 OP_MakeRecord..
21a39 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 66 */. af
21a3a 66 69 6e 69 74 79 20 3d 20 63 6f 6d 70 61 72 69 finity = compari
21a3b 73 6f 6e 41 66 66 69 6e 69 74 79 28 70 45 78 70 sonAffinity(pExp
21a3c 72 29 3b 0a 0a 0a 20 20 20 20 20 20 2f 2a 20 43 r);... /* C
21a3d 6f 64 65 20 74 68 65 20 3c 65 78 70 72 3e 20 66 ode the <expr> f
21a3e 72 6f 6d 20 22 3c 65 78 70 72 3e 20 49 4e 20 28 rom "<expr> IN (
21a3f 2e 2e 2e 29 22 2e 20 54 68 65 20 74 65 6d 70 6f ...)". The tempo
21a40 72 61 72 79 20 74 61 62 6c 65 0a 20 20 20 20 20 rary table.
21a41 20 2a 2a 20 70 45 78 70 72 2d 3e 69 54 61 62 6c ** pExpr->iTabl
21a42 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 76 e contains the v
21a43 61 6c 75 65 73 20 74 68 61 74 20 6d 61 6b 65 20 alues that make
21a44 75 70 20 74 68 65 20 28 2e 2e 2e 29 20 73 65 74 up the (...) set
21a45 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
21a46 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 sqlite3ExprCach
21a47 65 50 75 73 68 28 70 50 61 72 73 65 29 3b 0a 20 ePush(pParse);.
21a48 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 sqlite3Expr
21a49 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45 78 Code(pParse, pEx
21a4a 70 72 2d 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 pr->pLeft, targe
21a4b 74 29 3b 0a 20 20 20 20 20 20 6a 32 20 3d 20 73 t);. j2 = s
21a4c 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 qlite3VdbeAddOp1
21a4d 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 74 (v, OP_IsNull, t
21a4e 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 69 66 arget);. if
21a4f 28 20 65 54 79 70 65 3d 3d 49 4e 5f 49 4e 44 45 ( eType==IN_INDE
21a50 58 5f 52 4f 57 49 44 20 29 7b 0a 20 20 20 20 20 X_ROWID ){.
21a51 20 20 20 6a 33 20 3d 20 73 71 6c 69 74 65 33 56 j3 = sqlite3V
21a52 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f dbeAddOp1(v, OP_
21a53 4d 75 73 74 42 65 49 6e 74 2c 20 74 61 72 67 65 MustBeInt, targe
21a54 74 29 3b 0a 20 20 20 20 20 20 20 20 6a 34 20 3d t);. j4 =
21a55 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21a56 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 p3(v, OP_NotExis
21a57 74 73 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c ts, pExpr->iTabl
21a58 65 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 e, 0, target);.
21a59 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
21a5a 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 beAddOp2(v, OP_I
21a5b 6e 74 65 67 65 72 2c 20 31 2c 20 74 61 72 67 65 nteger, 1, targe
21a5c 74 29 3b 0a 20 20 20 20 20 20 20 20 6a 35 20 3d t);. j5 =
21a5d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21a5e 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b 0a p0(v, OP_Goto);.
21a5f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
21a60 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a dbeJumpHere(v, j
21a61 33 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 3);. sqli
21a62 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 te3VdbeJumpHere(
21a63 76 2c 20 6a 34 29 3b 0a 20 20 20 20 20 20 20 20 v, j4);.
21a64 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
21a65 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 2(v, OP_Integer,
21a66 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 0, target);.
21a67 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
21a68 20 20 72 32 20 3d 20 72 65 67 46 72 65 65 32 20 r2 = regFree2
21a69 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 = sqlite3GetTemp
21a6a 52 65 67 28 70 50 61 72 73 65 29 3b 0a 0a 20 20 Reg(pParse);..
21a6b 20 20 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 /* Create
21a6c 61 20 72 65 63 6f 72 64 20 61 6e 64 20 74 65 73 a record and tes
21a6d 74 20 66 6f 72 20 73 65 74 20 6d 65 6d 62 65 72 t for set member
21a6e 73 68 69 70 2e 20 49 66 20 74 68 65 20 73 65 74 ship. If the set
21a6f 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20 20 20 20 contains.
21a70 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 2c 20 ** the value,
21a71 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 74 68 65 then jump to the
21a72 20 65 6e 64 20 6f 66 20 74 68 65 20 74 65 73 74 end of the test
21a73 20 63 6f 64 65 2e 20 54 68 65 20 74 61 72 67 65 code. The targe
21a74 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 65 67 t. ** reg
21a75 69 73 74 65 72 20 73 74 69 6c 6c 20 63 6f 6e 74 ister still cont
21a76 61 69 6e 73 20 74 68 65 20 74 72 75 65 20 28 31 ains the true (1
21a77 29 20 76 61 6c 75 65 20 77 72 69 74 74 65 6e 20 ) value written
21a78 74 6f 20 69 74 20 65 61 72 6c 69 65 72 2e 0a 20 to it earlier..
21a79 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
21a7a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
21a7b 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 Op4(v, OP_MakeRe
21a7c 63 6f 72 64 2c 20 74 61 72 67 65 74 2c 20 31 2c cord, target, 1,
21a7d 20 72 32 2c 20 26 61 66 66 69 6e 69 74 79 2c 20 r2, &affinity,
21a7e 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 1);. sqli
21a7f 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
21a80 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 OP_Integer, 1,
21a81 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 target);.
21a82 20 6a 35 20 3d 20 73 71 6c 69 74 65 33 56 64 62 j5 = sqlite3Vdb
21a83 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 46 6f eAddOp3(v, OP_Fo
21a84 75 6e 64 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 und, pExpr->iTab
21a85 6c 65 2c 20 30 2c 20 72 32 29 3b 0a 0a 20 20 20 le, 0, r2);..
21a86 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73 /* If the s
21a87 65 74 20 6d 65 6d 62 65 72 73 68 69 70 20 74 65 et membership te
21a88 73 74 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 74 st fails, then t
21a89 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 he result of the
21a8a 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 22 78 20 . ** "x
21a8b 49 4e 20 28 2e 2e 2e 29 22 20 65 78 70 72 65 73 IN (...)" expres
21a8c 73 69 6f 6e 20 6d 75 73 74 20 62 65 20 65 69 74 sion must be eit
21a8d 68 65 72 20 30 20 6f 72 20 4e 55 4c 4c 2e 20 49 her 0 or NULL. I
21a8e 66 20 74 68 65 20 73 65 74 0a 20 20 20 20 20 20 f the set.
21a8f 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 6e 6f ** contains no
21a90 20 4e 55 4c 4c 20 76 61 6c 75 65 73 2c 20 74 68 NULL values, th
21a91 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 en the result is
21a92 20 30 2e 20 49 66 20 74 68 65 20 73 65 74 20 0a 0. If the set .
21a93 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61 ** conta
21a94 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 ins one or more
21a95 4e 55 4c 4c 20 76 61 6c 75 65 73 2c 20 74 68 65 NULL values, the
21a96 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 n the result of
21a97 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 65 the. ** e
21a98 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 6c 73 xpression is als
21a99 6f 20 4e 55 4c 4c 2e 0a 20 20 20 20 20 20 20 20 o NULL..
21a9a 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 */. if( r
21a9b 4e 6f 74 46 6f 75 6e 64 3d 3d 30 20 29 7b 0a 20 NotFound==0 ){.
21a9c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 /* This
21a9d 20 62 72 61 6e 63 68 20 72 75 6e 73 20 69 66 20 branch runs if
21a9e 69 74 20 69 73 20 6b 6e 6f 77 6e 20 61 74 20 63 it is known at c
21a9f 6f 6d 70 69 6c 65 20 74 69 6d 65 20 28 6e 6f 77 ompile time (now
21aa0 29 20 74 68 61 74 20 0a 20 20 20 20 20 20 20 20 ) that .
21aa1 20 20 2a 2a 20 74 68 65 20 73 65 74 20 63 6f 6e ** the set con
21aa2 74 61 69 6e 73 20 6e 6f 20 4e 55 4c 4c 20 76 61 tains no NULL va
21aa3 6c 75 65 73 2e 20 54 68 69 73 20 68 61 70 70 65 lues. This happe
21aa4 6e 73 20 61 73 20 74 68 65 20 72 65 73 75 6c 74 ns as the result
21aa5 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 . ** of
21aa6 20 61 20 22 4e 4f 54 20 4e 55 4c 4c 22 20 63 6f a "NOT NULL" co
21aa7 6e 73 74 72 61 69 6e 74 20 69 6e 20 74 68 65 20 nstraint in the
21aa8 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e database schema.
21aa9 20 4e 6f 20 6e 65 65 64 0a 20 20 20 20 20 20 20 No need.
21aaa 20 20 20 2a 2a 20 74 6f 20 74 65 73 74 20 74 68 ** to test th
21aab 65 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 e data structure
21aac 20 61 74 20 72 75 6e 74 69 6d 65 20 69 6e 20 74 at runtime in t
21aad 68 69 73 20 63 61 73 65 2e 0a 20 20 20 20 20 20 his case..
21aae 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 */.
21aaf 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21ab0 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 p2(v, OP_Integer
21ab1 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 , 0, target);.
21ab2 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
21ab3 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 /* This b
21ab4 6c 6f 63 6b 20 70 6f 70 75 6c 61 74 65 73 20 74 lock populates t
21ab5 68 65 20 72 4e 6f 74 46 6f 75 6e 64 20 72 65 67 he rNotFound reg
21ab6 69 73 74 65 72 20 77 69 74 68 20 65 69 74 68 65 ister with eithe
21ab7 72 20 4e 55 4c 4c 0a 20 20 20 20 20 20 20 20 20 r NULL.
21ab8 20 2a 2a 20 6f 72 20 30 20 28 61 6e 20 69 6e 74 ** or 0 (an int
21ab9 65 67 65 72 20 76 61 6c 75 65 29 2e 20 49 66 20 eger value). If
21aba 74 68 65 20 64 61 74 61 20 73 74 72 75 63 74 75 the data structu
21abb 72 65 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 0a re contains one.
21abc 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 ** or
21abd 6d 6f 72 65 20 4e 55 4c 4c 73 2c 20 74 68 65 6e more NULLs, then
21abe 20 73 65 74 20 72 4e 6f 74 46 6f 75 6e 64 20 74 set rNotFound t
21abf 6f 20 4e 55 4c 4c 2e 20 4f 74 68 65 72 77 69 73 o NULL. Otherwis
21ac0 65 2c 20 73 65 74 20 69 74 0a 20 20 20 20 20 20 e, set it.
21ac1 20 20 20 20 2a 2a 20 74 6f 20 30 2e 20 49 66 20 ** to 0. If
21ac2 72 65 67 69 73 74 65 72 20 72 4d 61 79 48 61 76 register rMayHav
21ac3 65 4e 75 6c 6c 20 69 73 20 61 6c 72 65 61 64 79 eNull is already
21ac4 20 73 65 74 20 74 6f 20 73 6f 6d 65 20 76 61 6c set to some val
21ac5 75 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 ue. **
21ac6 6f 74 68 65 72 20 74 68 61 6e 20 4e 55 4c 4c 2c other than NULL,
21ac7 20 74 68 65 6e 20 74 68 65 20 74 65 73 74 20 68 then the test h
21ac8 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 as already been
21ac9 72 75 6e 20 61 6e 64 20 0a 20 20 20 20 20 20 20 run and .
21aca 20 20 20 2a 2a 20 72 4e 6f 74 46 6f 75 6e 64 20 ** rNotFound
21acb 69 73 20 61 6c 72 65 61 64 79 20 70 6f 70 75 6c is already popul
21acc 61 74 65 64 2e 0a 20 20 20 20 20 20 20 20 20 20 ated..
21acd 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 73 74 61 */. sta
21ace 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 6e tic const char n
21acf 75 6c 6c 52 65 63 6f 72 64 5b 5d 20 3d 20 7b 20 ullRecord[] = {
21ad0 30 78 30 32 2c 20 30 78 30 30 20 7d 3b 0a 20 20 0x02, 0x00 };.
21ad1 20 20 20 20 20 20 20 20 6a 33 20 3d 20 73 71 6c j3 = sql
21ad2 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 ite3VdbeAddOp1(v
21ad3 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 4d , OP_NotNull, rM
21ad4 61 79 48 61 76 65 4e 75 6c 6c 29 3b 0a 20 20 20 ayHaveNull);.
21ad5 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
21ad6 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e beAddOp2(v, OP_N
21ad7 75 6c 6c 2c 20 30 2c 20 72 4e 6f 74 46 6f 75 6e ull, 0, rNotFoun
21ad8 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 d);. sq
21ad9 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 lite3VdbeAddOp4(
21ada 76 2c 20 4f 50 5f 42 6c 6f 62 2c 20 32 2c 20 72 v, OP_Blob, 2, r
21adb 4d 61 79 48 61 76 65 4e 75 6c 6c 2c 20 30 2c 20 MayHaveNull, 0,
21adc 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
21add 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 75 nu
21ade 6c 6c 52 65 63 6f 72 64 2c 20 50 34 5f 53 54 41 llRecord, P4_STA
21adf 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 20 20 TIC);.
21ae0 6a 34 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 j4 = sqlite3Vdbe
21ae1 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 46 6f 75 AddOp3(v, OP_Fou
21ae2 6e 64 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c nd, pExpr->iTabl
21ae3 65 2c 20 30 2c 20 72 4d 61 79 48 61 76 65 4e 75 e, 0, rMayHaveNu
21ae4 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 ll);. s
21ae5 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
21ae6 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 (v, OP_Integer,
21ae7 30 2c 20 72 4e 6f 74 46 6f 75 6e 64 29 3b 0a 20 0, rNotFound);.
21ae8 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
21ae9 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 VdbeJumpHere(v,
21aea 6a 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 j4);. s
21aeb 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 qlite3VdbeJumpHe
21aec 72 65 28 76 2c 20 6a 33 29 3b 0a 0a 20 20 20 20 re(v, j3);..
21aed 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 74 68 /* Copy th
21aee 65 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 e value of regis
21aef 74 65 72 20 72 4e 6f 74 46 6f 75 6e 64 20 28 77 ter rNotFound (w
21af0 68 69 63 68 20 69 73 20 65 69 74 68 65 72 20 4e hich is either N
21af1 55 4c 4c 20 6f 72 20 30 29 0a 20 20 20 20 20 20 ULL or 0).
21af2 20 20 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 ** into the
21af3 74 61 72 67 65 74 20 72 65 67 69 73 74 65 72 2e target register.
21af4 20 54 68 69 73 20 77 69 6c 6c 20 62 65 20 74 68 This will be th
21af5 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 0a e result of the.
21af6 20 20 20 20 20 20 20 20 20 20 2a 2a 20 65 78 70 ** exp
21af7 72 65 73 73 69 6f 6e 2e 0a 20 20 20 20 20 20 20 ression..
21af8 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 */.
21af9 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
21afa 32 28 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 72 4e 2(v, OP_Copy, rN
21afb 6f 74 46 6f 75 6e 64 2c 20 74 61 72 67 65 74 29 otFound, target)
21afc 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
21afd 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 }. sqlite
21afe 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 3VdbeJumpHere(v,
21aff 20 6a 32 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 j2);. sqli
21b00 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 te3VdbeJumpHere(
21b01 76 2c 20 6a 35 29 3b 0a 20 20 20 20 20 20 73 71 v, j5);. sq
21b02 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 6f lite3ExprCachePo
21b03 70 28 70 50 61 72 73 65 2c 20 31 29 3b 0a 20 20 p(pParse, 1);.
21b04 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 VdbeComment(
21b05 28 76 2c 20 22 65 6e 64 20 49 4e 20 65 78 70 72 (v, "end IN expr
21b06 20 72 25 64 22 2c 20 74 61 72 67 65 74 29 29 3b r%d", target));
21b07 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
21b08 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 2f }.#endif. /
21b09 2a 0a 20 20 20 20 2a 2a 20 20 20 20 78 20 42 45 *. ** x BE
21b0a 54 57 45 45 4e 20 79 20 41 4e 44 20 7a 0a 20 20 TWEEN y AND z.
21b0b 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 69 73 **. ** This
21b0c 20 69 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 is equivalent t
21b0d 6f 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 o. **. **
21b0e 20 20 20 78 3e 3d 79 20 41 4e 44 20 78 3c 3d 7a x>=y AND x<=z
21b0f 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 58 . **. ** X
21b10 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 70 45 is stored in pE
21b11 78 70 72 2d 3e 70 4c 65 66 74 2e 0a 20 20 20 20 xpr->pLeft..
21b12 2a 2a 20 59 20 69 73 20 73 74 6f 72 65 64 20 69 ** Y is stored i
21b13 6e 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e n pExpr->pList->
21b14 61 5b 30 5d 2e 70 45 78 70 72 2e 0a 20 20 20 20 a[0].pExpr..
21b15 2a 2a 20 5a 20 69 73 20 73 74 6f 72 65 64 20 69 ** Z is stored i
21b16 6e 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e n pExpr->pList->
21b17 61 5b 31 5d 2e 70 45 78 70 72 2e 0a 20 20 20 20 a[1].pExpr..
21b18 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 */. case TK_B
21b19 45 54 57 45 45 4e 3a 20 7b 0a 20 20 20 20 20 20 ETWEEN: {.
21b1a 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 70 45 Expr *pLeft = pE
21b1b 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 xpr->pLeft;.
21b1c 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 struct ExprLis
21b1d 74 5f 69 74 65 6d 20 2a 70 4c 49 74 65 6d 20 3d t_item *pLItem =
21b1e 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d pExpr->x.pList-
21b1f 3e 61 3b 0a 20 20 20 20 20 20 45 78 70 72 20 2a >a;. Expr *
21b20 70 52 69 67 68 74 20 3d 20 70 4c 49 74 65 6d 2d pRight = pLItem-
21b21 3e 70 45 78 70 72 3b 0a 0a 20 20 20 20 20 20 63 >pExpr;.. c
21b22 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e odeCompareOperan
21b23 64 73 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74 ds(pParse, pLeft
21b24 2c 20 26 72 31 2c 20 26 72 65 67 46 72 65 65 31 , &r1, ®Free1
21b25 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
21b26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21b27 20 20 20 20 70 52 69 67 68 74 2c 20 26 72 32 2c pRight, &r2,
21b28 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 ®Free2);.
21b29 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 testcase( reg
21b2a 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 Free1==0 );.
21b2b 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 testcase( regF
21b2c 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 ree2==0 );.
21b2d 20 72 33 20 3d 20 73 71 6c 69 74 65 33 47 65 74 r3 = sqlite3Get
21b2e 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b TempReg(pParse);
21b2f 0a 20 20 20 20 20 20 72 34 20 3d 20 73 71 6c 69 . r4 = sqli
21b30 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 te3GetTempReg(pP
21b31 61 72 73 65 29 3b 0a 20 20 20 20 20 20 63 6f 64 arse);. cod
21b32 65 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c eCompare(pParse,
21b33 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20 pLeft, pRight,
21b34 4f 50 5f 47 65 2c 0a 20 20 20 20 20 20 20 20 20 OP_Ge,.
21b35 20 20 20 20 20 20 20 20 20 72 31 2c 20 72 32 2c r1, r2,
21b36 20 72 33 2c 20 53 51 4c 49 54 45 5f 53 54 4f 52 r3, SQLITE_STOR
21b37 45 50 32 29 3b 0a 20 20 20 20 20 20 70 4c 49 74 EP2);. pLIt
21b38 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 70 52 69 67 em++;. pRig
21b39 68 74 20 3d 20 70 4c 49 74 65 6d 2d 3e 70 45 78 ht = pLItem->pEx
21b3a 70 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 pr;. sqlite
21b3b 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 3ReleaseTempReg(
21b3c 70 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 32 pParse, regFree2
21b3d 29 3b 0a 20 20 20 20 20 20 72 32 20 3d 20 73 71 );. r2 = sq
21b3e 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d lite3ExprCodeTem
21b3f 70 28 70 50 61 72 73 65 2c 20 70 52 69 67 68 74 p(pParse, pRight
21b40 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 , ®Free2);.
21b41 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 testcase( re
21b42 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 gFree2==0 );.
21b43 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 codeCompare(p
21b44 50 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 70 52 Parse, pLeft, pR
21b45 69 67 68 74 2c 20 4f 50 5f 4c 65 2c 20 72 31 2c ight, OP_Le, r1,
21b46 20 72 32 2c 20 72 34 2c 20 53 51 4c 49 54 45 5f r2, r4, SQLITE_
21b47 53 54 4f 52 45 50 32 29 3b 0a 20 20 20 20 20 20 STOREP2);.
21b48 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
21b49 33 28 76 2c 20 4f 50 5f 41 6e 64 2c 20 72 33 2c 3(v, OP_And, r3,
21b4a 20 72 34 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 r4, target);.
21b4b 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 sqlite3Relea
21b4c 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 seTempReg(pParse
21b4d 2c 20 72 33 29 3b 0a 20 20 20 20 20 20 73 71 6c , r3);. sql
21b4e 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 ite3ReleaseTempR
21b4f 65 67 28 70 50 61 72 73 65 2c 20 72 34 29 3b 0a eg(pParse, r4);.
21b50 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
21b51 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55 }. case TK_U
21b52 50 4c 55 53 3a 20 7b 0a 20 20 20 20 20 20 69 6e PLUS: {. in
21b53 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70 Reg = sqlite3Exp
21b54 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 rCodeTarget(pPar
21b55 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 se, pExpr->pLeft
21b56 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 , target);.
21b57 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 break;. }..
21b58 20 20 20 63 61 73 65 20 54 4b 5f 54 52 49 47 47 case TK_TRIGG
21b59 45 52 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 49 ER: {. /* I
21b5a 66 20 74 68 65 20 6f 70 63 6f 64 65 20 69 73 20 f the opcode is
21b5b 54 4b 5f 54 52 49 47 47 45 52 2c 20 74 68 65 6e TK_TRIGGER, then
21b5c 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
21b5d 69 73 20 61 20 72 65 66 65 72 65 6e 63 65 0a 20 is a reference.
21b5e 20 20 20 20 20 2a 2a 20 74 6f 20 61 20 63 6f 6c ** to a col
21b5f 75 6d 6e 20 69 6e 20 74 68 65 20 6e 65 77 2e 2a umn in the new.*
21b60 20 6f 72 20 6f 6c 64 2e 2a 20 70 73 65 75 64 6f or old.* pseudo
21b61 2d 74 61 62 6c 65 73 20 61 76 61 69 6c 61 62 6c -tables availabl
21b62 65 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 72 e to. ** tr
21b63 69 67 67 65 72 20 70 72 6f 67 72 61 6d 73 2e 20 igger programs.
21b64 49 6e 20 74 68 69 73 20 63 61 73 65 20 45 78 70 In this case Exp
21b65 72 2e 69 54 61 62 6c 65 20 69 73 20 73 65 74 20 r.iTable is set
21b66 74 6f 20 31 20 66 6f 72 20 74 68 65 0a 20 20 20 to 1 for the.
21b67 20 20 20 2a 2a 20 6e 65 77 2e 2a 20 70 73 65 75 ** new.* pseu
21b68 64 6f 2d 74 61 62 6c 65 2c 20 6f 72 20 30 20 66 do-table, or 0 f
21b69 6f 72 20 74 68 65 20 6f 6c 64 2e 2a 20 70 73 65 or the old.* pse
21b6a 75 64 6f 2d 74 61 62 6c 65 2e 20 45 78 70 72 2e udo-table. Expr.
21b6b 69 43 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 2a 2a iColumn. **
21b6c 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 63 is set to the c
21b6d 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 70 73 65 olumn of the pse
21b6e 75 64 6f 2d 74 61 62 6c 65 20 74 6f 20 72 65 61 udo-table to rea
21b6f 64 2c 20 6f 72 20 74 6f 20 2d 31 20 74 6f 0a 20 d, or to -1 to.
21b70 20 20 20 20 20 2a 2a 20 72 65 61 64 20 74 68 65 ** read the
21b71 20 72 6f 77 69 64 20 66 69 65 6c 64 2e 0a 20 20 rowid field..
21b72 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 **. **
21b73 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 The expression i
21b74 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 75 73 s implemented us
21b75 69 6e 67 20 61 6e 20 4f 50 5f 50 61 72 61 6d 20 ing an OP_Param
21b76 6f 70 63 6f 64 65 2e 20 54 68 65 20 70 31 0a 20 opcode. The p1.
21b77 20 20 20 20 20 2a 2a 20 70 61 72 61 6d 65 74 65 ** paramete
21b78 72 20 69 73 20 73 65 74 20 74 6f 20 30 20 66 6f r is set to 0 fo
21b79 72 20 61 6e 20 6f 6c 64 2e 72 6f 77 69 64 20 72 r an old.rowid r
21b7a 65 66 65 72 65 6e 63 65 2c 20 6f 72 20 74 6f 20 eference, or to
21b7b 28 69 2b 31 29 0a 20 20 20 20 20 20 2a 2a 20 74 (i+1). ** t
21b7c 6f 20 72 65 66 65 72 65 6e 63 65 20 61 6e 6f 74 o reference anot
21b7d 68 65 72 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 her column of th
21b7e 65 20 6f 6c 64 2e 2a 20 70 73 65 75 64 6f 2d 74 e old.* pseudo-t
21b7f 61 62 6c 65 2c 20 77 68 65 72 65 20 0a 20 20 20 able, where .
21b80 20 20 20 2a 2a 20 69 20 69 73 20 74 68 65 20 69 ** i is the i
21b81 6e 64 65 78 20 6f 66 20 74 68 65 20 63 6f 6c 75 ndex of the colu
21b82 6d 6e 2e 20 46 6f 72 20 61 20 6e 65 77 2e 72 6f mn. For a new.ro
21b83 77 69 64 20 72 65 66 65 72 65 6e 63 65 2c 20 70 wid reference, p
21b84 31 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 73 65 1 is. ** se
21b85 74 20 74 6f 20 28 6e 2b 31 29 2c 20 77 68 65 72 t to (n+1), wher
21b86 65 20 6e 20 69 73 20 74 68 65 20 6e 75 6d 62 65 e n is the numbe
21b87 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 r of columns in
21b88 65 61 63 68 20 70 73 65 75 64 6f 2d 74 61 62 6c each pseudo-tabl
21b89 65 2e 0a 20 20 20 20 20 20 2a 2a 20 46 6f 72 20 e.. ** For
21b8a 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 a reference to a
21b8b 6e 79 20 6f 74 68 65 72 20 63 6f 6c 75 6d 6e 20 ny other column
21b8c 69 6e 20 74 68 65 20 6e 65 77 2e 2a 20 70 73 65 in the new.* pse
21b8d 75 64 6f 2d 74 61 62 6c 65 2c 20 70 31 0a 20 20 udo-table, p1.
21b8e 20 20 20 20 2a 2a 20 69 73 20 73 65 74 20 74 6f ** is set to
21b8f 20 28 6e 2b 32 2b 69 29 2c 20 77 68 65 72 65 20 (n+2+i), where
21b90 6e 20 61 6e 64 20 69 20 61 72 65 20 61 73 20 64 n and i are as d
21b91 65 66 69 6e 65 64 20 70 72 65 76 69 6f 75 73 6c efined previousl
21b92 79 2e 20 46 6f 72 0a 20 20 20 20 20 20 2a 2a 20 y. For. **
21b93 65 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65 20 example, if the
21b94 74 61 62 6c 65 20 6f 6e 20 77 68 69 63 68 20 74 table on which t
21b95 72 69 67 67 65 72 73 20 61 72 65 20 62 65 69 6e riggers are bein
21b96 67 20 66 69 72 65 64 20 69 73 0a 20 20 20 20 20 g fired is.
21b97 20 2a 2a 20 64 65 63 6c 61 72 65 64 20 61 73 3a ** declared as:
21b98 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 . **.
21b99 2a 2a 20 20 20 43 52 45 41 54 45 20 54 41 42 4c ** CREATE TABL
21b9a 45 20 74 31 28 61 2c 20 62 29 3b 0a 20 20 20 20 E t1(a, b);.
21b9b 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68 **. ** Th
21b9c 65 6e 20 70 31 20 69 73 20 69 6e 74 65 72 70 72 en p1 is interpr
21b9d 65 74 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a eted as follows:
21b9e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 . **.
21b9f 2a 2a 20 20 20 70 31 3d 3d 30 20 20 20 2d 3e 20 ** p1==0 ->
21ba0 20 20 20 6f 6c 64 2e 72 6f 77 69 64 20 20 20 20 old.rowid
21ba1 20 70 31 3d 3d 33 20 20 20 2d 3e 20 20 20 20 6e p1==3 -> n
21ba2 65 77 2e 72 6f 77 69 64 0a 20 20 20 20 20 20 2a ew.rowid. *
21ba3 2a 20 20 20 70 31 3d 3d 31 20 20 20 2d 3e 20 20 * p1==1 ->
21ba4 20 20 6f 6c 64 2e 61 20 20 20 20 20 20 20 20 20 old.a
21ba5 70 31 3d 3d 34 20 20 20 2d 3e 20 20 20 20 6e 65 p1==4 -> ne
21ba6 77 2e 61 0a 20 20 20 20 20 20 2a 2a 20 20 20 70 w.a. ** p
21ba7 31 3d 3d 32 20 20 20 2d 3e 20 20 20 20 6f 6c 64 1==2 -> old
21ba8 2e 62 20 20 20 20 20 20 20 20 20 70 31 3d 3d 35 .b p1==5
21ba9 20 20 20 2d 3e 20 20 20 20 6e 65 77 2e 62 20 20 -> new.b
21baa 20 20 20 20 20 0a 20 20 20 20 20 20 2a 2f 0a 20 . */.
21bab 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 Table *pTab
21bac 20 3d 20 70 45 78 70 72 2d 3e 70 54 61 62 3b 0a = pExpr->pTab;.
21bad 20 20 20 20 20 20 69 6e 74 20 70 31 20 3d 20 70 int p1 = p
21bae 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 2a 20 28 Expr->iTable * (
21baf 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31 29 20 2b 20 pTab->nCol+1) +
21bb0 31 20 2b 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 1 + pExpr->iColu
21bb1 6d 6e 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 mn;.. asser
21bb2 74 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 t( pExpr->iTable
21bb3 3d 3d 30 20 7c 7c 20 70 45 78 70 72 2d 3e 69 54 ==0 || pExpr->iT
21bb4 61 62 6c 65 3d 3d 31 20 29 3b 0a 20 20 20 20 20 able==1 );.
21bb5 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e assert( pExpr->
21bb6 69 43 6f 6c 75 6d 6e 3e 3d 2d 31 20 26 26 20 70 iColumn>=-1 && p
21bb7 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3c 70 54 Expr->iColumn<pT
21bb8 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 ab->nCol );.
21bb9 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e assert( pTab->
21bba 69 50 4b 65 79 3c 30 20 7c 7c 20 70 45 78 70 72 iPKey<0 || pExpr
21bbb 2d 3e 69 43 6f 6c 75 6d 6e 21 3d 70 54 61 62 2d ->iColumn!=pTab-
21bbc 3e 69 50 4b 65 79 20 29 3b 0a 20 20 20 20 20 20 >iPKey );.
21bbd 61 73 73 65 72 74 28 20 70 31 3e 3d 30 20 26 26 assert( p1>=0 &&
21bbe 20 70 31 3c 28 70 54 61 62 2d 3e 6e 43 6f 6c 2a p1<(pTab->nCol*
21bbf 32 2b 32 29 20 29 3b 0a 0a 20 20 20 20 20 20 73 2+2) );.. s
21bc0 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
21bc1 28 76 2c 20 4f 50 5f 50 61 72 61 6d 2c 20 70 31 (v, OP_Param, p1
21bc2 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 , target);.
21bc3 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c VdbeComment((v,
21bc4 20 22 25 73 2e 25 73 20 2d 3e 20 24 25 64 22 2c "%s.%s -> $%d",
21bc5 0a 20 20 20 20 20 20 20 20 28 70 45 78 70 72 2d . (pExpr-
21bc6 3e 69 54 61 62 6c 65 20 3f 20 22 6e 65 77 22 20 >iTable ? "new"
21bc7 3a 20 22 6f 6c 64 22 29 2c 0a 20 20 20 20 20 20 : "old"),.
21bc8 20 20 28 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d (pExpr->iColum
21bc9 6e 3c 30 20 3f 20 22 72 6f 77 69 64 22 20 3a 20 n<0 ? "rowid" :
21bca 70 45 78 70 72 2d 3e 70 54 61 62 2d 3e 61 43 6f pExpr->pTab->aCo
21bcb 6c 5b 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e l[pExpr->iColumn
21bcc 5d 2e 7a 4e 61 6d 65 29 2c 0a 20 20 20 20 20 20 ].zName),.
21bcd 20 20 74 61 72 67 65 74 0a 20 20 20 20 20 20 29 target. )
21bce 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 );.. /* If
21bcf 74 68 65 20 63 6f 6c 75 6d 6e 20 68 61 73 20 52 the column has R
21bd0 45 41 4c 20 61 66 66 69 6e 69 74 79 2c 20 69 74 EAL affinity, it
21bd1 20 6d 61 79 20 63 75 72 72 65 6e 74 6c 79 20 62 may currently b
21bd2 65 20 73 74 6f 72 65 64 20 61 73 20 61 6e 0a 20 e stored as an.
21bd3 20 20 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 2e ** integer.
21bd4 20 55 73 65 20 4f 50 5f 52 65 61 6c 41 66 66 69 Use OP_RealAffi
21bd5 6e 69 74 79 20 74 6f 20 6d 61 6b 65 20 73 75 72 nity to make sur
21bd6 65 20 69 74 20 69 73 20 72 65 61 6c 6c 79 20 72 e it is really r
21bd7 65 61 6c 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69 eal. */. i
21bd8 66 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d f( pExpr->iColum
21bd9 6e 3e 3d 30 20 0a 20 20 20 20 20 20 20 26 26 20 n>=0 . &&
21bda 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 45 78 70 72 pTab->aCol[pExpr
21bdb 2d 3e 69 43 6f 6c 75 6d 6e 5d 2e 61 66 66 69 6e ->iColumn].affin
21bdc 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f ity==SQLITE_AFF_
21bdd 52 45 41 4c 0a 20 20 20 20 20 20 29 7b 0a 20 20 REAL. ){.
21bde 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
21bdf 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 eAddOp1(v, OP_Re
21be0 61 6c 41 66 66 69 6e 69 74 79 2c 20 74 61 72 67 alAffinity, targ
21be1 65 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 et);. }.
21be2 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
21be3 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 .. /*. **
21be4 46 6f 72 6d 20 41 3a 0a 20 20 20 20 2a 2a 20 20 Form A:. **
21be5 20 43 41 53 45 20 78 20 57 48 45 4e 20 65 31 20 CASE x WHEN e1
21be6 54 48 45 4e 20 72 31 20 57 48 45 4e 20 65 32 20 THEN r1 WHEN e2
21be7 54 48 45 4e 20 72 32 20 2e 2e 2e 20 57 48 45 4e THEN r2 ... WHEN
21be8 20 65 4e 20 54 48 45 4e 20 72 4e 20 45 4c 53 45 eN THEN rN ELSE
21be9 20 79 20 45 4e 44 0a 20 20 20 20 2a 2a 0a 20 20 y END. **.
21bea 20 20 2a 2a 20 46 6f 72 6d 20 42 3a 0a 20 20 20 ** Form B:.
21beb 20 2a 2a 20 20 20 43 41 53 45 20 57 48 45 4e 20 ** CASE WHEN
21bec 65 31 20 54 48 45 4e 20 72 31 20 57 48 45 4e 20 e1 THEN r1 WHEN
21bed 65 32 20 54 48 45 4e 20 72 32 20 2e 2e 2e 20 57 e2 THEN r2 ... W
21bee 48 45 4e 20 65 4e 20 54 48 45 4e 20 72 4e 20 45 HEN eN THEN rN E
21bef 4c 53 45 20 79 20 45 4e 44 0a 20 20 20 20 2a 2a LSE y END. **
21bf0 0a 20 20 20 20 2a 2a 20 46 6f 72 6d 20 41 20 69 . ** Form A i
21bf1 73 20 63 61 6e 20 62 65 20 74 72 61 6e 73 66 6f s can be transfo
21bf2 72 6d 65 64 20 69 6e 74 6f 20 74 68 65 20 65 71 rmed into the eq
21bf3 75 69 76 61 6c 65 6e 74 20 66 6f 72 6d 20 42 20 uivalent form B
21bf4 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 20 20 as follows:.
21bf5 2a 2a 20 20 20 43 41 53 45 20 57 48 45 4e 20 78 ** CASE WHEN x
21bf6 3d 65 31 20 54 48 45 4e 20 72 31 20 57 48 45 4e =e1 THEN r1 WHEN
21bf7 20 78 3d 65 32 20 54 48 45 4e 20 72 32 20 2e 2e x=e2 THEN r2 ..
21bf8 2e 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 .. **
21bf9 57 48 45 4e 20 78 3d 65 4e 20 54 48 45 4e 20 72 WHEN x=eN THEN r
21bfa 4e 20 45 4c 53 45 20 79 20 45 4e 44 0a 20 20 20 N ELSE y END.
21bfb 20 2a 2a 0a 20 20 20 20 2a 2a 20 58 20 28 69 66 **. ** X (if
21bfc 20 69 74 20 65 78 69 73 74 73 29 20 69 73 20 69 it exists) is i
21bfd 6e 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2e 0a n pExpr->pLeft..
21bfe 20 20 20 20 2a 2a 20 59 20 69 73 20 69 6e 20 70 ** Y is in p
21bff 45 78 70 72 2d 3e 70 52 69 67 68 74 2e 20 20 54 Expr->pRight. T
21c00 68 65 20 59 20 69 73 20 61 6c 73 6f 20 6f 70 74 he Y is also opt
21c01 69 6f 6e 61 6c 2e 20 20 49 66 20 74 68 65 72 65 ional. If there
21c02 20 69 73 20 6e 6f 0a 20 20 20 20 2a 2a 20 45 4c is no. ** EL
21c03 53 45 20 63 6c 61 75 73 65 20 61 6e 64 20 6e 6f SE clause and no
21c04 20 6f 74 68 65 72 20 74 65 72 6d 20 6d 61 74 63 other term matc
21c05 68 65 73 2c 20 74 68 65 6e 20 74 68 65 20 72 65 hes, then the re
21c06 73 75 6c 74 20 6f 66 20 74 68 65 0a 20 20 20 20 sult of the.
21c07 2a 2a 20 65 78 70 72 73 73 69 6f 6e 20 69 73 20 ** exprssion is
21c08 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2a 20 45 69 20 NULL.. ** Ei
21c09 69 73 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c 69 is in pExpr->pLi
21c0a 73 74 2d 3e 61 5b 69 2a 32 5d 20 61 6e 64 20 52 st->a[i*2] and R
21c0b 69 20 69 73 20 70 45 78 70 72 2d 3e 70 4c 69 73 i is pExpr->pLis
21c0c 74 2d 3e 61 5b 69 2a 32 2b 31 5d 2e 0a 20 20 20 t->a[i*2+1]..
21c0d 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 **. ** The r
21c0e 65 73 75 6c 74 20 6f 66 20 74 68 65 20 65 78 70 esult of the exp
21c0f 72 65 73 73 69 6f 6e 20 69 73 20 74 68 65 20 52 ression is the R
21c10 69 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 i for the first
21c11 6d 61 74 63 68 69 6e 67 20 45 69 2c 0a 20 20 20 matching Ei,.
21c12 20 2a 2a 20 6f 72 20 69 66 20 74 68 65 72 65 20 ** or if there
21c13 69 73 20 6e 6f 20 6d 61 74 63 68 69 6e 67 20 45 is no matching E
21c14 69 2c 20 74 68 65 20 45 4c 53 45 20 74 65 72 6d i, the ELSE term
21c15 20 59 2c 20 6f 72 20 69 66 20 74 68 65 72 65 20 Y, or if there
21c16 69 73 0a 20 20 20 20 2a 2a 20 6e 6f 20 45 4c 53 is. ** no ELS
21c17 45 20 74 65 72 6d 2c 20 4e 55 4c 4c 2e 0a 20 20 E term, NULL..
21c18 20 20 2a 2f 0a 20 20 20 20 64 65 66 61 75 6c 74 */. default
21c19 3a 20 61 73 73 65 72 74 28 20 6f 70 3d 3d 54 4b : assert( op==TK
21c1a 5f 43 41 53 45 20 29 3b 20 7b 0a 20 20 20 20 20 _CASE ); {.
21c1b 20 69 6e 74 20 65 6e 64 4c 61 62 65 6c 3b 20 20 int endLabel;
21c1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21c1d 20 20 20 2f 2a 20 47 4f 54 4f 20 6c 61 62 65 6c /* GOTO label
21c1e 20 66 6f 72 20 65 6e 64 20 6f 66 20 43 41 53 45 for end of CASE
21c1f 20 73 74 6d 74 20 2a 2f 0a 20 20 20 20 20 20 69 stmt */. i
21c20 6e 74 20 6e 65 78 74 43 61 73 65 3b 20 20 20 20 nt nextCase;
21c21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21c22 20 2f 2a 20 47 4f 54 4f 20 6c 61 62 65 6c 20 66 /* GOTO label f
21c23 6f 72 20 6e 65 78 74 20 57 48 45 4e 20 63 6c 61 or next WHEN cla
21c24 75 73 65 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 use */. int
21c25 20 6e 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 nExpr;
21c26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
21c27 2a 20 32 78 20 6e 75 6d 62 65 72 20 6f 66 20 57 * 2x number of W
21c28 48 45 4e 20 74 65 72 6d 73 20 2a 2f 0a 20 20 20 HEN terms */.
21c29 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 int i;
21c2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21c2b 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 /* Loop cou
21c2c 6e 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 45 78 nter */. Ex
21c2d 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 prList *pEList;
21c2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21c2f 2f 2a 20 4c 69 73 74 20 6f 66 20 57 48 45 4e 20 /* List of WHEN
21c30 74 65 72 6d 73 20 2a 2f 0a 20 20 20 20 20 20 73 terms */. s
21c31 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 truct ExprList_i
21c32 74 65 6d 20 2a 61 4c 69 73 74 65 6c 65 6d 3b 20 tem *aListelem;
21c33 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 57 48 45 /* Array of WHE
21c34 4e 20 74 65 72 6d 73 20 2a 2f 0a 20 20 20 20 20 N terms */.
21c35 20 45 78 70 72 20 6f 70 43 6f 6d 70 61 72 65 3b Expr opCompare;
21c36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21c37 20 20 20 2f 2a 20 54 68 65 20 58 3d 3d 45 69 20 /* The X==Ei
21c38 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 expression */.
21c39 20 20 20 20 45 78 70 72 20 63 61 63 68 65 58 3b Expr cacheX;
21c3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21c3b 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65 64 20 /* Cached
21c3c 65 78 70 72 65 73 73 69 6f 6e 20 58 20 2a 2f 0a expression X */.
21c3d 20 20 20 20 20 20 45 78 70 72 20 2a 70 58 3b 20 Expr *pX;
21c3e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21c3f 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 58 /* The X
21c40 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 expression */.
21c41 20 20 20 20 20 45 78 70 72 20 2a 70 54 65 73 74 Expr *pTest
21c42 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
21c43 20 20 20 20 20 20 20 2f 2a 20 58 3d 3d 45 69 20 /* X==Ei
21c44 28 66 6f 72 6d 20 41 29 20 6f 72 20 6a 75 73 74 (form A) or just
21c45 20 45 69 20 28 66 6f 72 6d 20 42 29 20 2a 2f 0a Ei (form B) */.
21c46 20 20 20 20 20 20 56 56 41 5f 4f 4e 4c 59 28 20 VVA_ONLY(
21c47 69 6e 74 20 69 43 61 63 68 65 4c 65 76 65 6c 20 int iCacheLevel
21c48 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 = pParse->iCache
21c49 4c 65 76 65 6c 3b 20 29 0a 0a 20 20 20 20 20 20 Level; )..
21c4a 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 assert( !ExprHas
21c4b 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 Property(pExpr,
21c4c 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 26 26 EP_xIsSelect) &&
21c4d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 20 pExpr->x.pList
21c4e 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
21c4f 28 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d (pExpr->x.pList-
21c50 3e 6e 45 78 70 72 20 25 20 32 29 20 3d 3d 20 30 >nExpr % 2) == 0
21c51 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
21c52 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e pExpr->x.pList->
21c53 6e 45 78 70 72 20 3e 20 30 29 3b 0a 20 20 20 20 nExpr > 0);.
21c54 20 20 70 45 4c 69 73 74 20 3d 20 70 45 78 70 72 pEList = pExpr
21c55 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 20 20 20 ->x.pList;.
21c56 20 61 4c 69 73 74 65 6c 65 6d 20 3d 20 70 45 4c aListelem = pEL
21c57 69 73 74 2d 3e 61 3b 0a 20 20 20 20 20 20 6e 45 ist->a;. nE
21c58 78 70 72 20 3d 20 70 45 4c 69 73 74 2d 3e 6e 45 xpr = pEList->nE
21c59 78 70 72 3b 0a 20 20 20 20 20 20 65 6e 64 4c 61 xpr;. endLa
21c5a 62 65 6c 20 3d 20 73 71 6c 69 74 65 33 56 64 62 bel = sqlite3Vdb
21c5b 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 eMakeLabel(v);.
21c5c 20 20 20 20 20 69 66 28 20 28 70 58 20 3d 20 70 if( (pX = p
21c5d 45 78 70 72 2d 3e 70 4c 65 66 74 29 21 3d 30 20 Expr->pLeft)!=0
21c5e 29 7b 0a 20 20 20 20 20 20 20 20 63 61 63 68 65 ){. cache
21c5f 58 20 3d 20 2a 70 58 3b 0a 20 20 20 20 20 20 20 X = *pX;.
21c60 20 74 65 73 74 63 61 73 65 28 20 70 58 2d 3e 6f testcase( pX->o
21c61 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a p==TK_COLUMN );.
21c62 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 testcase
21c63 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 52 45 47 ( pX->op==TK_REG
21c64 49 53 54 45 52 20 29 3b 0a 20 20 20 20 20 20 20 ISTER );.
21c65 20 63 61 63 68 65 58 2e 69 54 61 62 6c 65 20 3d cacheX.iTable =
21c66 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
21c67 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 58 2c Temp(pParse, pX,
21c68 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20 ®Free1);.
21c69 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 testcase( r
21c6a 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 egFree1==0 );.
21c6b 20 20 20 20 20 20 63 61 63 68 65 58 2e 6f 70 20 cacheX.op
21c6c 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20 = TK_REGISTER;.
21c6d 20 20 20 20 20 20 20 6f 70 43 6f 6d 70 61 72 65 opCompare
21c6e 2e 6f 70 20 3d 20 54 4b 5f 45 51 3b 0a 20 20 20 .op = TK_EQ;.
21c6f 20 20 20 20 20 6f 70 43 6f 6d 70 61 72 65 2e 70 opCompare.p
21c70 4c 65 66 74 20 3d 20 26 63 61 63 68 65 58 3b 0a Left = &cacheX;.
21c71 20 20 20 20 20 20 20 20 70 54 65 73 74 20 3d 20 pTest =
21c72 26 6f 70 43 6f 6d 70 61 72 65 3b 0a 20 20 20 20 &opCompare;.
21c73 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 69 3d }. for(i=
21c74 30 3b 20 69 3c 6e 45 78 70 72 3b 20 69 3d 69 2b 0; i<nExpr; i=i+
21c75 32 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 2){. sqli
21c76 74 65 33 45 78 70 72 43 61 63 68 65 50 75 73 68 te3ExprCachePush
21c77 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 (pParse);.
21c78 20 20 69 66 28 20 70 58 20 29 7b 0a 20 20 20 20 if( pX ){.
21c79 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54 assert( pT
21c7a 65 73 74 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 est!=0 );.
21c7b 20 20 20 20 6f 70 43 6f 6d 70 61 72 65 2e 70 52 opCompare.pR
21c7c 69 67 68 74 20 3d 20 61 4c 69 73 74 65 6c 65 6d ight = aListelem
21c7d 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 [i].pExpr;.
21c7e 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
21c7f 20 20 20 20 70 54 65 73 74 20 3d 20 61 4c 69 73 pTest = aLis
21c80 74 65 6c 65 6d 5b 69 5d 2e 70 45 78 70 72 3b 0a telem[i].pExpr;.
21c81 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
21c82 20 20 6e 65 78 74 43 61 73 65 20 3d 20 73 71 6c nextCase = sql
21c83 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 ite3VdbeMakeLabe
21c84 6c 28 76 29 3b 0a 20 20 20 20 20 20 20 20 74 65 l(v);. te
21c85 73 74 63 61 73 65 28 20 70 54 65 73 74 2d 3e 6f stcase( pTest->o
21c86 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a p==TK_COLUMN );.
21c87 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 sqlite3E
21c88 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 xprIfFalse(pPars
21c89 65 2c 20 70 54 65 73 74 2c 20 6e 65 78 74 43 61 e, pTest, nextCa
21c8a 73 65 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 se, SQLITE_JUMPI
21c8b 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 20 20 FNULL);.
21c8c 74 65 73 74 63 61 73 65 28 20 61 4c 69 73 74 65 testcase( aListe
21c8d 6c 65 6d 5b 69 2b 31 5d 2e 70 45 78 70 72 2d 3e lem[i+1].pExpr->
21c8e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b op==TK_COLUMN );
21c8f 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 . testcas
21c90 65 28 20 61 4c 69 73 74 65 6c 65 6d 5b 69 2b 31 e( aListelem[i+1
21c91 5d 2e 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f ].pExpr->op==TK_
21c92 52 45 47 49 53 54 45 52 20 29 3b 0a 20 20 20 20 REGISTER );.
21c93 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 sqlite3ExprC
21c94 6f 64 65 28 70 50 61 72 73 65 2c 20 61 4c 69 73 ode(pParse, aLis
21c95 74 65 6c 65 6d 5b 69 2b 31 5d 2e 70 45 78 70 72 telem[i+1].pExpr
21c96 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 , target);.
21c97 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
21c98 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c dOp2(v, OP_Goto,
21c99 20 30 2c 20 65 6e 64 4c 61 62 65 6c 29 3b 0a 20 0, endLabel);.
21c9a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 sqlite3Ex
21c9b 70 72 43 61 63 68 65 50 6f 70 28 70 50 61 72 73 prCachePop(pPars
21c9c 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73 e, 1);. s
21c9d 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 qlite3VdbeResolv
21c9e 65 4c 61 62 65 6c 28 76 2c 20 6e 65 78 74 43 61 eLabel(v, nextCa
21c9f 73 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 se);. }.
21ca0 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 70 52 if( pExpr->pR
21ca1 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 20 20 ight ){.
21ca2 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 sqlite3ExprCache
21ca3 50 75 73 68 28 70 50 61 72 73 65 29 3b 0a 20 20 Push(pParse);.
21ca4 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 sqlite3Exp
21ca5 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45 rCode(pParse, pE
21ca6 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 74 61 72 xpr->pRight, tar
21ca7 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 get);. sq
21ca8 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 6f lite3ExprCachePo
21ca9 70 28 70 50 61 72 73 65 2c 20 31 29 3b 0a 20 20 p(pParse, 1);.
21caa 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
21cab 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
21cac 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c dOp2(v, OP_Null,
21cad 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 0, target);.
21cae 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 }. asser
21caf 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 t( db->mallocFai
21cb0 6c 65 64 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e led || pParse->n
21cb1 45 72 72 3e 30 20 0a 20 20 20 20 20 20 20 20 20 Err>0 .
21cb2 20 20 7c 7c 20 70 50 61 72 73 65 2d 3e 69 43 61 || pParse->iCa
21cb3 63 68 65 4c 65 76 65 6c 3d 3d 69 43 61 63 68 65 cheLevel==iCache
21cb4 4c 65 76 65 6c 20 29 3b 0a 20 20 20 20 20 20 73 Level );. s
21cb5 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 qlite3VdbeResolv
21cb6 65 4c 61 62 65 6c 28 76 2c 20 65 6e 64 4c 61 62 eLabel(v, endLab
21cb7 65 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b el);. break
21cb8 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 ;. }.#ifndef
21cb9 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 SQLITE_OMIT_TRIG
21cba 47 45 52 0a 20 20 20 20 63 61 73 65 20 54 4b 5f GER. case TK_
21cbb 52 41 49 53 45 3a 20 7b 0a 20 20 20 20 20 20 61 RAISE: {. a
21cbc 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 61 66 ssert( pExpr->af
21cbd 66 69 6e 69 74 79 3d 3d 4f 45 5f 52 6f 6c 6c 62 finity==OE_Rollb
21cbe 61 63 6b 20 0a 20 20 20 20 20 20 20 20 20 20 20 ack .
21cbf 7c 7c 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 || pExpr->affini
21cc0 74 79 3d 3d 4f 45 5f 41 62 6f 72 74 0a 20 20 20 ty==OE_Abort.
21cc1 20 20 20 20 20 20 20 20 7c 7c 20 70 45 78 70 72 || pExpr
21cc2 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 4f 45 5f 46 ->affinity==OE_F
21cc3 61 69 6c 0a 20 20 20 20 20 20 20 20 20 20 20 7c ail. |
21cc4 7c 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 | pExpr->affinit
21cc5 79 3d 3d 4f 45 5f 49 67 6e 6f 72 65 0a 20 20 20 y==OE_Ignore.
21cc6 20 20 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 );. if(
21cc7 21 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 !pParse->pTrigge
21cc8 72 54 61 62 20 29 7b 0a 20 20 20 20 20 20 20 20 rTab ){.
21cc9 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
21cca 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 pParse,.
21ccb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 "
21ccc 52 41 49 53 45 28 29 20 6d 61 79 20 6f 6e 6c 79 RAISE() may only
21ccd 20 62 65 20 75 73 65 64 20 77 69 74 68 69 6e 20 be used within
21cce 61 20 74 72 69 67 67 65 72 2d 70 72 6f 67 72 61 a trigger-progra
21ccf 6d 22 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 m");. ret
21cd0 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 urn 0;. }.
21cd1 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e if( pExpr->
21cd2 61 66 66 69 6e 69 74 79 3d 3d 4f 45 5f 41 62 6f affinity==OE_Abo
21cd3 72 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 rt ){. sq
21cd4 6c 69 74 65 33 4d 61 79 41 62 6f 72 74 28 70 50 lite3MayAbort(pP
21cd5 61 72 73 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 arse);. }.
21cd6 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 assert( !Ex
21cd7 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 prHasProperty(pE
21cd8 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 xpr, EP_IntValue
21cd9 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 ) );. if( p
21cda 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 3d 3d Expr->affinity==
21cdb 4f 45 5f 49 67 6e 6f 72 65 20 29 7b 0a 20 20 20 OE_Ignore ){.
21cdc 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
21cdd 41 64 64 4f 70 34 28 0a 20 20 20 20 20 20 20 20 AddOp4(.
21cde 20 20 20 20 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 v, OP_Halt,
21cdf 53 51 4c 49 54 45 5f 4f 4b 2c 20 4f 45 5f 49 67 SQLITE_OK, OE_Ig
21ce0 6e 6f 72 65 2c 20 30 2c 20 70 45 78 70 72 2d 3e nore, 0, pExpr->
21ce1 75 2e 7a 54 6f 6b 65 6e 2c 30 29 3b 0a 20 20 20 u.zToken,0);.
21ce2 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
21ce3 20 20 73 71 6c 69 74 65 33 48 61 6c 74 43 6f 6e sqlite3HaltCon
21ce4 73 74 72 61 69 6e 74 28 70 50 61 72 73 65 2c 20 straint(pParse,
21ce5 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 2c pExpr->affinity,
21ce6 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e pExpr->u.zToken
21ce7 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 , 0);. }..
21ce8 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
21ce9 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 73 }.#endif. }. s
21cea 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d qlite3ReleaseTem
21ceb 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 pReg(pParse, reg
21cec 46 72 65 65 31 29 3b 0a 20 20 73 71 6c 69 74 65 Free1);. sqlite
21ced 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 3ReleaseTempReg(
21cee 70 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 32 pParse, regFree2
21cef 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 6e 52 65 );. return inRe
21cf0 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 g;.}../*.** Gene
21cf1 72 61 74 65 20 63 6f 64 65 20 74 6f 20 65 76 61 rate code to eva
21cf2 6c 75 61 74 65 20 61 6e 20 65 78 70 72 65 73 73 luate an express
21cf3 69 6f 6e 20 61 6e 64 20 73 74 6f 72 65 20 74 68 ion and store th
21cf4 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20 69 6e 74 e results.** int
21cf5 6f 20 61 20 72 65 67 69 73 74 65 72 2e 20 20 52 o a register. R
21cf6 65 74 75 72 6e 20 74 68 65 20 72 65 67 69 73 74 eturn the regist
21cf7 65 72 20 6e 75 6d 62 65 72 20 77 68 65 72 65 20 er number where
21cf8 74 68 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20 61 the results.** a
21cf9 72 65 20 73 74 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a re stored..**.**
21cfa 20 49 66 20 74 68 65 20 72 65 67 69 73 74 65 72 If the register
21cfb 20 69 73 20 61 20 74 65 6d 70 6f 72 61 72 79 20 is a temporary
21cfc 72 65 67 69 73 74 65 72 20 74 68 61 74 20 63 61 register that ca
21cfd 6e 20 62 65 20 64 65 61 6c 6c 6f 63 61 74 65 64 n be deallocated
21cfe 2c 0a 2a 2a 20 74 68 65 6e 20 77 72 69 74 65 20 ,.** then write
21cff 69 74 73 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 its number into
21d00 2a 70 52 65 67 2e 20 20 49 66 20 74 68 65 20 72 *pReg. If the r
21d01 65 73 75 6c 74 20 72 65 67 69 73 74 65 72 20 69 esult register i
21d02 73 20 6e 6f 74 0a 2a 2a 20 61 20 74 65 6d 70 6f s not.** a tempo
21d03 72 61 72 79 2c 20 74 68 65 6e 20 73 65 74 20 2a rary, then set *
21d04 70 52 65 67 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2f pReg to zero..*/
21d05 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
21d06 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 int sqlite3ExprC
21d07 6f 64 65 54 65 6d 70 28 50 61 72 73 65 20 2a 70 odeTemp(Parse *p
21d08 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 Parse, Expr *pEx
21d09 70 72 2c 20 69 6e 74 20 2a 70 52 65 67 29 7b 0a pr, int *pReg){.
21d0a 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74 int r1 = sqlit
21d0b 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 e3GetTempReg(pPa
21d0c 72 73 65 29 3b 0a 20 20 69 6e 74 20 72 32 20 3d rse);. int r2 =
21d0d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
21d0e 54 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 Target(pParse, p
21d0f 45 78 70 72 2c 20 72 31 29 3b 0a 20 20 69 66 28 Expr, r1);. if(
21d10 20 72 32 3d 3d 72 31 20 29 7b 0a 20 20 20 20 2a r2==r1 ){. *
21d11 70 52 65 67 20 3d 20 72 31 3b 0a 20 20 7d 65 6c pReg = r1;. }el
21d12 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 se{. sqlite3R
21d13 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 eleaseTempReg(pP
21d14 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 2a arse, r1);. *
21d15 70 52 65 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 pReg = 0;. }.
21d16 72 65 74 75 72 6e 20 72 32 3b 0a 7d 0a 0a 2f 2a return r2;.}../*
21d17 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 .** Generate cod
21d18 65 20 74 68 61 74 20 77 69 6c 6c 20 65 76 61 6c e that will eval
21d19 75 61 74 65 20 65 78 70 72 65 73 73 69 6f 6e 20 uate expression
21d1a 70 45 78 70 72 20 61 6e 64 20 73 74 6f 72 65 20 pExpr and store
21d1b 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 73 20 69 the.** results i
21d1c 6e 20 72 65 67 69 73 74 65 72 20 74 61 72 67 65 n register targe
21d1d 74 2e 20 20 54 68 65 20 72 65 73 75 6c 74 73 20 t. The results
21d1e 61 72 65 20 67 75 61 72 61 6e 74 65 65 64 20 74 are guaranteed t
21d1f 6f 20 61 70 70 65 61 72 0a 2a 2a 20 69 6e 20 72 o appear.** in r
21d20 65 67 69 73 74 65 72 20 74 61 72 67 65 74 2e 0a egister target..
21d21 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
21d22 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 E int sqlite3Exp
21d23 72 43 6f 64 65 28 50 61 72 73 65 20 2a 70 50 61 rCode(Parse *pPa
21d24 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 rse, Expr *pExpr
21d25 2c 20 69 6e 74 20 74 61 72 67 65 74 29 7b 0a 20 , int target){.
21d26 20 69 6e 74 20 69 6e 52 65 67 3b 0a 0a 20 20 61 int inReg;.. a
21d27 73 73 65 72 74 28 20 74 61 72 67 65 74 3e 30 20 ssert( target>0
21d28 26 26 20 74 61 72 67 65 74 3c 3d 70 50 61 72 73 && target<=pPars
21d29 65 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 69 6e 52 e->nMem );. inR
21d2a 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 eg = sqlite3Expr
21d2b 43 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 73 CodeTarget(pPars
21d2c 65 2c 20 70 45 78 70 72 2c 20 74 61 72 67 65 74 e, pExpr, target
21d2d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 );. assert( pPa
21d2e 72 73 65 2d 3e 70 56 64 62 65 20 7c 7c 20 70 50 rse->pVdbe || pP
21d2f 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 arse->db->malloc
21d30 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 Failed );. if(
21d31 69 6e 52 65 67 21 3d 74 61 72 67 65 74 20 26 26 inReg!=target &&
21d32 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 20 29 pParse->pVdbe )
21d33 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
21d34 65 41 64 64 4f 70 32 28 70 50 61 72 73 65 2d 3e eAddOp2(pParse->
21d35 70 56 64 62 65 2c 20 4f 50 5f 53 43 6f 70 79 2c pVdbe, OP_SCopy,
21d36 20 69 6e 52 65 67 2c 20 74 61 72 67 65 74 29 3b inReg, target);
21d37 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 74 61 . }. return ta
21d38 72 67 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 rget;.}../*.** G
21d39 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 enerate code tha
21d3a 74 20 65 76 61 6c 75 74 65 73 20 74 68 65 20 67 t evalutes the g
21d3b 69 76 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 iven expression
21d3c 61 6e 64 20 70 75 74 73 20 74 68 65 20 72 65 73 and puts the res
21d3d 75 6c 74 0a 2a 2a 20 69 6e 20 72 65 67 69 73 74 ult.** in regist
21d3e 65 72 20 74 61 72 67 65 74 2e 0a 2a 2a 0a 2a 2a er target..**.**
21d3f 20 41 6c 73 6f 20 6d 61 6b 65 20 61 20 63 6f 70 Also make a cop
21d40 79 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 y of the express
21d41 69 6f 6e 20 72 65 73 75 6c 74 73 20 69 6e 74 6f ion results into
21d42 20 61 6e 6f 74 68 65 72 20 22 63 61 63 68 65 22 another "cache"
21d43 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 61 6e 64 register.** and
21d44 20 6d 6f 64 69 66 79 20 74 68 65 20 65 78 70 72 modify the expr
21d45 65 73 73 69 6f 6e 20 73 6f 20 74 68 61 74 20 74 ession so that t
21d46 68 65 20 6e 65 78 74 20 74 69 6d 65 20 69 74 20 he next time it
21d47 69 73 20 65 76 61 6c 75 61 74 65 64 2c 0a 2a 2a is evaluated,.**
21d48 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 the result is a
21d49 20 63 6f 70 79 20 6f 66 20 74 68 65 20 63 61 63 copy of the cac
21d4a 68 65 20 72 65 67 69 73 74 65 72 2e 0a 2a 2a 0a he register..**.
21d4b 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
21d4c 69 73 20 75 73 65 64 20 66 6f 72 20 65 78 70 72 is used for expr
21d4d 65 73 73 69 6f 6e 73 20 74 68 61 74 20 61 72 65 essions that are
21d4e 20 75 73 65 64 20 6d 75 6c 74 69 70 6c 65 20 0a used multiple .
21d4f 2a 2a 20 74 69 6d 65 73 2e 20 20 54 68 65 79 20 ** times. They
21d50 61 72 65 20 65 76 61 6c 75 61 74 65 64 20 6f 6e are evaluated on
21d51 63 65 20 61 6e 64 20 74 68 65 20 72 65 73 75 6c ce and the resul
21d52 74 73 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 ts of the expres
21d53 73 69 6f 6e 0a 2a 2a 20 61 72 65 20 72 65 75 73 sion.** are reus
21d54 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
21d55 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
21d56 33 45 78 70 72 43 6f 64 65 41 6e 64 43 61 63 68 3ExprCodeAndCach
21d57 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c e(Parse *pParse,
21d58 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e Expr *pExpr, in
21d59 74 20 74 61 72 67 65 74 29 7b 0a 20 20 56 64 62 t target){. Vdb
21d5a 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 e *v = pParse->p
21d5b 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 6e 52 65 Vdbe;. int inRe
21d5c 67 3b 0a 20 20 69 6e 52 65 67 20 3d 20 73 71 6c g;. inReg = sql
21d5d 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 ite3ExprCode(pPa
21d5e 72 73 65 2c 20 70 45 78 70 72 2c 20 74 61 72 67 rse, pExpr, targ
21d5f 65 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 74 et);. assert( t
21d60 61 72 67 65 74 3e 30 20 29 3b 0a 20 20 2f 2a 20 arget>0 );. /*
21d61 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
21d62 63 61 6c 6c 65 64 20 66 6f 72 20 74 65 72 6d 73 called for terms
21d63 20 74 6f 20 49 4e 53 45 52 54 20 6f 72 20 55 50 to INSERT or UP
21d64 44 41 54 45 2e 20 20 41 6e 64 20 74 68 65 20 6f DATE. And the o
21d65 6e 6c 79 0a 20 20 2a 2a 20 6f 74 68 65 72 20 70 nly. ** other p
21d66 6c 61 63 65 20 77 68 65 72 65 20 65 78 70 72 65 lace where expre
21d67 73 73 69 6f 6e 73 20 63 61 6e 20 62 65 20 63 6f ssions can be co
21d68 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 54 4b 5f nverted into TK_
21d69 52 45 47 49 53 54 45 52 20 69 73 0a 20 20 2a 2a REGISTER is. **
21d6a 20 69 6e 20 57 48 45 52 45 20 63 6c 61 75 73 65 in WHERE clause
21d6b 20 70 72 6f 63 65 73 73 69 6e 67 2e 20 20 53 6f processing. So
21d6c 20 61 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6d as currently im
21d6d 70 6c 65 6d 65 6e 74 65 64 2c 20 74 68 65 72 65 plemented, there
21d6e 20 69 73 0a 20 20 2a 2a 20 6e 6f 20 77 61 79 20 is. ** no way
21d6f 66 6f 72 20 61 20 54 4b 5f 52 45 47 49 53 54 45 for a TK_REGISTE
21d70 52 20 74 6f 20 65 78 69 73 74 20 68 65 72 65 2e R to exist here.
21d71 20 20 42 75 74 20 69 74 20 73 65 65 6d 73 20 70 But it seems p
21d72 72 75 64 65 6e 74 20 74 6f 0a 20 20 2a 2a 20 6b rudent to. ** k
21d73 65 65 70 20 74 68 65 20 41 4c 57 41 59 53 28 29 eep the ALWAYS()
21d74 20 69 6e 20 63 61 73 65 20 74 68 65 20 63 6f 6e in case the con
21d75 64 69 74 69 6f 6e 73 20 61 62 6f 76 65 20 63 68 ditions above ch
21d76 61 6e 67 65 20 77 69 74 68 20 66 75 74 75 72 65 ange with future
21d77 0a 20 20 2a 2a 20 6d 6f 64 69 66 69 63 61 74 69 . ** modificati
21d78 6f 6e 73 20 6f 72 20 65 6e 68 61 6e 63 65 6d 65 ons or enhanceme
21d79 6e 74 73 2e 20 2a 2f 0a 20 20 69 66 28 20 41 4c nts. */. if( AL
21d7a 57 41 59 53 28 70 45 78 70 72 2d 3e 6f 70 21 3d WAYS(pExpr->op!=
21d7b 54 4b 5f 52 45 47 49 53 54 45 52 29 20 29 7b 20 TK_REGISTER) ){
21d7c 20 0a 20 20 20 20 69 6e 74 20 69 4d 65 6d 3b 0a . int iMem;.
21d7d 20 20 20 20 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 iMem = ++pPa
21d7e 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 rse->nMem;. s
21d7f 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
21d80 28 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 69 6e 52 (v, OP_Copy, inR
21d81 65 67 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 70 eg, iMem);. p
21d82 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 69 Expr->iTable = i
21d83 4d 65 6d 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e Mem;. pExpr->
21d84 6f 70 32 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b op2 = pExpr->op;
21d85 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d . pExpr->op =
21d86 20 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20 20 TK_REGISTER;.
21d87 7d 0a 20 20 72 65 74 75 72 6e 20 69 6e 52 65 67 }. return inReg
21d88 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
21d89 6e 20 54 52 55 45 20 69 66 20 70 45 78 70 72 20 n TRUE if pExpr
21d8a 69 73 20 61 6e 20 63 6f 6e 73 74 61 6e 74 20 65 is an constant e
21d8b 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 69 xpression that i
21d8c 73 20 61 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a s appropriate.**
21d8d 20 66 6f 72 20 66 61 63 74 6f 72 69 6e 67 20 6f for factoring o
21d8e 75 74 20 6f 66 20 61 20 6c 6f 6f 70 2e 20 20 41 ut of a loop. A
21d8f 70 70 72 6f 70 72 69 61 74 65 20 65 78 70 72 65 ppropriate expre
21d90 73 73 69 6f 6e 73 20 61 72 65 3a 0a 2a 2a 0a 2a ssions are:.**.*
21d91 2a 20 20 20 20 2a 20 20 41 6e 79 20 65 78 70 72 * * Any expr
21d92 65 73 73 69 6f 6e 20 74 68 61 74 20 65 76 61 6c ession that eval
21d93 75 61 74 65 73 20 74 6f 20 74 77 6f 20 6f 72 20 uates to two or
21d94 6d 6f 72 65 20 6f 70 63 6f 64 65 73 2e 0a 2a 2a more opcodes..**
21d95 0a 2a 2a 20 20 20 20 2a 20 20 41 6e 79 20 4f 50 .** * Any OP
21d96 5f 49 6e 74 65 67 65 72 2c 20 4f 50 5f 52 65 61 _Integer, OP_Rea
21d97 6c 2c 20 4f 50 5f 53 74 72 69 6e 67 2c 20 4f 50 l, OP_String, OP
21d98 5f 42 6c 6f 62 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 _Blob, OP_Null,
21d99 0a 2a 2a 20 20 20 20 20 20 20 6f 72 20 4f 50 5f .** or OP_
21d9a 56 61 72 69 61 62 6c 65 20 74 68 61 74 20 64 6f Variable that do
21d9b 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 es not need to b
21d9c 65 20 70 6c 61 63 65 64 20 69 6e 20 61 20 0a 2a e placed in a .*
21d9d 2a 20 20 20 20 20 20 20 73 70 65 63 69 66 69 63 * specific
21d9e 20 72 65 67 69 73 74 65 72 2e 0a 2a 2a 0a 2a 2a register..**.**
21d9f 20 54 68 65 72 65 20 69 73 20 6e 6f 20 70 6f 69 There is no poi
21da0 6e 74 20 69 6e 20 66 61 63 74 6f 72 69 6e 67 20 nt in factoring
21da1 6f 75 74 20 73 69 6e 67 6c 65 2d 69 6e 73 74 72 out single-instr
21da2 75 63 74 69 6f 6e 20 63 6f 6e 73 74 61 6e 74 0a uction constant.
21da3 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 ** expressions t
21da4 68 61 74 20 6e 65 65 64 20 74 6f 20 62 65 20 70 hat need to be p
21da5 6c 61 63 65 64 20 69 6e 20 61 20 70 61 72 74 69 laced in a parti
21da6 63 75 6c 61 72 20 72 65 67 69 73 74 65 72 2e 20 cular register.
21da7 20 0a 2a 2a 20 57 65 20 63 6f 75 6c 64 20 66 61 .** We could fa
21da8 63 74 6f 72 20 74 68 65 6d 20 6f 75 74 2c 20 62 ctor them out, b
21da9 75 74 20 74 68 65 6e 20 77 65 20 77 6f 75 6c 64 ut then we would
21daa 20 65 6e 64 20 75 70 20 61 64 64 69 6e 67 20 61 end up adding a
21dab 6e 0a 2a 2a 20 4f 50 5f 53 43 6f 70 79 20 69 6e n.** OP_SCopy in
21dac 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 6d 6f 76 struction to mov
21dad 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 74 6f e the value into
21dae 20 74 68 65 20 63 6f 72 72 65 63 74 20 72 65 67 the correct reg
21daf 69 73 74 65 72 0a 2a 2a 20 6c 61 74 65 72 2e 20 ister.** later.
21db0 20 57 65 20 6d 69 67 68 74 20 61 73 20 77 65 6c We might as wel
21db1 6c 20 6a 75 73 74 20 75 73 65 20 74 68 65 20 6f l just use the o
21db2 72 69 67 69 6e 61 6c 20 69 6e 73 74 72 75 63 74 riginal instruct
21db3 69 6f 6e 20 61 6e 64 0a 2a 2a 20 61 76 6f 69 64 ion and.** avoid
21db4 20 74 68 65 20 4f 50 5f 53 43 6f 70 79 2e 0a 2a the OP_SCopy..*
21db5 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73 41 /.static int isA
21db6 70 70 72 6f 70 72 69 61 74 65 46 6f 72 46 61 63 ppropriateForFac
21db7 74 6f 72 69 6e 67 28 45 78 70 72 20 2a 70 29 7b toring(Expr *p){
21db8 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 45 . if( !sqlite3E
21db9 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 xprIsConstantNot
21dba 4a 6f 69 6e 28 70 29 20 29 7b 0a 20 20 20 20 72 Join(p) ){. r
21dbb 65 74 75 72 6e 20 30 3b 20 20 2f 2a 20 4f 6e 6c eturn 0; /* Onl
21dbc 79 20 63 6f 6e 73 74 61 6e 74 20 65 78 70 72 65 y constant expre
21dbd 73 73 69 6f 6e 73 20 61 72 65 20 61 70 70 72 6f ssions are appro
21dbe 70 72 69 61 74 65 20 66 6f 72 20 66 61 63 74 6f priate for facto
21dbf 72 69 6e 67 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 ring */. }. if
21dc0 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 45 50 ( (p->flags & EP
21dc1 5f 46 69 78 65 64 44 65 73 74 29 3d 3d 30 20 29 _FixedDest)==0 )
21dc2 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 20 {. return 1;
21dc3 20 2f 2a 20 41 6e 79 20 63 6f 6e 73 74 61 6e 74 /* Any constant
21dc4 20 77 69 74 68 6f 75 74 20 61 20 66 69 78 65 64 without a fixed
21dc5 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 73 20 destination is
21dc6 61 70 70 72 6f 70 72 69 61 74 65 20 2a 2f 0a 20 appropriate */.
21dc7 20 7d 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e 6f }. while( p->o
21dc8 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 29 20 70 20 p==TK_UPLUS ) p
21dc9 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 73 77 = p->pLeft;. sw
21dca 69 74 63 68 28 20 70 2d 3e 6f 70 20 29 7b 0a 23 itch( p->op ){.#
21dcb 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
21dcc 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 0a IT_BLOB_LITERAL.
21dcd 20 20 20 20 63 61 73 65 20 54 4b 5f 42 4c 4f 42 case TK_BLOB
21dce 3a 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 61 73 :.#endif. cas
21dcf 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a 0a 20 e TK_VARIABLE:.
21dd0 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 54 45 47 case TK_INTEG
21dd1 45 52 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f ER:. case TK_
21dd2 46 4c 4f 41 54 3a 0a 20 20 20 20 63 61 73 65 20 FLOAT:. case
21dd3 54 4b 5f 4e 55 4c 4c 3a 0a 20 20 20 20 63 61 73 TK_NULL:. cas
21dd4 65 20 54 4b 5f 53 54 52 49 4e 47 3a 20 7b 0a 20 e TK_STRING: {.
21dd5 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 testcase( p
21dd6 2d 3e 6f 70 3d 3d 54 4b 5f 42 4c 4f 42 20 29 3b ->op==TK_BLOB );
21dd7 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
21dd8 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 56 41 52 49 41 p->op==TK_VARIA
21dd9 42 4c 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 BLE );. tes
21dda 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b tcase( p->op==TK
21ddb 5f 49 4e 54 45 47 45 52 20 29 3b 0a 20 20 20 20 _INTEGER );.
21ddc 20 20 74 65 73 74 63 61 73 65 28 20 70 2d 3e 6f testcase( p->o
21ddd 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 3b 0a 20 p==TK_FLOAT );.
21dde 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 testcase( p
21ddf 2d 3e 6f 70 3d 3d 54 4b 5f 4e 55 4c 4c 20 29 3b ->op==TK_NULL );
21de0 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
21de1 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 53 54 52 49 4e p->op==TK_STRIN
21de2 47 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 53 69 G );. /* Si
21de3 6e 67 6c 65 2d 69 6e 73 74 72 75 63 74 69 6f 6e ngle-instruction
21de4 20 63 6f 6e 73 74 61 6e 74 73 20 77 69 74 68 20 constants with
21de5 61 20 66 69 78 65 64 20 64 65 73 74 69 6e 61 74 a fixed destinat
21de6 69 6f 6e 20 61 72 65 0a 20 20 20 20 20 20 2a 2a ion are. **
21de7 20 62 65 74 74 65 72 20 64 6f 6e 65 20 69 6e 2d better done in-
21de8 6c 69 6e 65 2e 20 20 49 66 20 77 65 20 66 61 63 line. If we fac
21de9 74 6f 72 20 74 68 65 6d 2c 20 74 68 65 79 20 77 tor them, they w
21dea 69 6c 6c 20 6a 75 73 74 20 65 6e 64 0a 20 20 20 ill just end.
21deb 20 20 20 2a 2a 20 75 70 20 67 65 6e 65 72 61 74 ** up generat
21dec 69 6e 67 20 61 6e 20 4f 50 5f 53 43 6f 70 79 20 ing an OP_SCopy
21ded 74 6f 20 6d 6f 76 65 20 74 68 65 20 76 61 6c 75 to move the valu
21dee 65 20 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61 e to the destina
21def 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 72 65 tion. ** re
21df0 67 69 73 74 65 72 2e 20 2a 2f 0a 20 20 20 20 20 gister. */.
21df1 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d return 0;. }
21df2 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55 4d 49 . case TK_UMI
21df3 4e 55 53 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 NUS: {. if(
21df4 20 70 2d 3e 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 p->pLeft->op==T
21df5 4b 5f 46 4c 4f 41 54 20 7c 7c 20 70 2d 3e 70 4c K_FLOAT || p->pL
21df6 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 54 45 eft->op==TK_INTE
21df7 47 45 52 20 29 7b 0a 20 20 20 20 20 20 20 20 72 GER ){. r
21df8 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d eturn 0;. }
21df9 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
21dfa 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a }. default:
21dfb 20 7b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a {. break;.
21dfc 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
21dfd 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 rn 1;.}../*.** I
21dfe 66 20 70 45 78 70 72 20 69 73 20 61 20 63 6f 6e f pExpr is a con
21dff 73 74 61 6e 74 20 65 78 70 72 65 73 73 69 6f 6e stant expression
21e00 20 74 68 61 74 20 69 73 20 61 70 70 72 6f 70 72 that is appropr
21e01 69 61 74 65 20 66 6f 72 0a 2a 2a 20 66 61 63 74 iate for.** fact
21e02 6f 72 69 6e 67 20 6f 75 74 20 6f 66 20 61 20 6c oring out of a l
21e03 6f 6f 70 2c 20 74 68 65 6e 20 65 76 61 6c 75 61 oop, then evalua
21e04 74 65 20 74 68 65 20 65 78 70 72 65 73 73 69 6f te the expressio
21e05 6e 0a 2a 2a 20 69 6e 74 6f 20 61 20 72 65 67 69 n.** into a regi
21e06 73 74 65 72 20 61 6e 64 20 63 6f 6e 76 65 72 74 ster and convert
21e07 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
21e08 69 6e 74 6f 20 61 20 54 4b 5f 52 45 47 49 53 54 into a TK_REGIST
21e09 45 52 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e ER.** expression
21e0a 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
21e0b 65 76 61 6c 43 6f 6e 73 74 45 78 70 72 28 57 61 evalConstExpr(Wa
21e0c 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 lker *pWalker, E
21e0d 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 50 xpr *pExpr){. P
21e0e 61 72 73 65 20 2a 70 50 61 72 73 65 20 3d 20 70 arse *pParse = p
21e0f 57 61 6c 6b 65 72 2d 3e 70 50 61 72 73 65 3b 0a Walker->pParse;.
21e10 20 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d switch( pExpr-
21e11 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 >op ){. case
21e12 54 4b 5f 49 4e 3a 0a 20 20 20 20 63 61 73 65 20 TK_IN:. case
21e13 54 4b 5f 52 45 47 49 53 54 45 52 3a 20 7b 0a 20 TK_REGISTER: {.
21e14 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f return WRC_
21e15 50 72 75 6e 65 3b 0a 20 20 20 20 7d 0a 20 20 20 Prune;. }.
21e16 20 63 61 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f case TK_FUNCTIO
21e17 4e 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 N:. case TK_A
21e18 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 0a 20 20 20 GG_FUNCTION:.
21e19 20 63 61 73 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 case TK_CONST_F
21e1a 55 4e 43 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 UNC: {. /*
21e1b 54 68 65 20 61 72 67 75 6d 65 6e 74 73 20 74 6f The arguments to
21e1c 20 61 20 66 75 6e 63 74 69 6f 6e 20 68 61 76 65 a function have
21e1d 20 61 20 66 69 78 65 64 20 64 65 73 74 69 6e 61 a fixed destina
21e1e 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2a 20 4d tion.. ** M
21e1f 61 72 6b 20 74 68 65 6d 20 74 68 69 73 20 77 61 ark them this wa
21e20 79 20 74 6f 20 61 76 6f 69 64 20 67 65 6e 65 72 y to avoid gener
21e21 61 74 65 64 20 75 6e 6e 65 65 64 65 64 20 4f 50 ated unneeded OP
21e22 5f 53 43 6f 70 79 0a 20 20 20 20 20 20 2a 2a 20 _SCopy. **
21e23 69 6e 73 74 72 75 63 74 69 6f 6e 73 2e 20 0a 20 instructions. .
21e24 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 45 78 */. Ex
21e25 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 prList *pList =
21e26 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a pExpr->x.pList;.
21e27 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 assert( !E
21e28 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 xprHasProperty(p
21e29 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 Expr, EP_xIsSele
21e2a 63 74 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 ct) );. if(
21e2b 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 pList ){.
21e2c 20 20 69 6e 74 20 69 20 3d 20 70 4c 69 73 74 2d int i = pList-
21e2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 >nExpr;.
21e2e 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f struct ExprList_
21e2f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 70 4c item *pItem = pL
21e30 69 73 74 2d 3e 61 3b 0a 20 20 20 20 20 20 20 20 ist->a;.
21e31 66 6f 72 28 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 for(; i>0; i--,
21e32 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 pItem++){.
21e33 20 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 if( ALWAYS(p
21e34 49 74 65 6d 2d 3e 70 45 78 70 72 29 20 29 20 70 Item->pExpr) ) p
21e35 49 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 66 6c 61 Item->pExpr->fla
21e36 67 73 20 7c 3d 20 45 50 5f 46 69 78 65 64 44 65 gs |= EP_FixedDe
21e37 73 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 st;. }.
21e38 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 }. brea
21e39 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 k;. }. }. i
21e3a 66 28 20 69 73 41 70 70 72 6f 70 72 69 61 74 65 f( isAppropriate
21e3b 46 6f 72 46 61 63 74 6f 72 69 6e 67 28 70 45 78 ForFactoring(pEx
21e3c 70 72 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 pr) ){. int r
21e3d 31 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 1 = ++pParse->nM
21e3e 65 6d 3b 0a 20 20 20 20 69 6e 74 20 72 32 3b 0a em;. int r2;.
21e3f 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74 65 33 r2 = sqlite3
21e40 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 ExprCodeTarget(p
21e41 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 72 31 Parse, pExpr, r1
21e42 29 3b 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 );. if( NEVER
21e43 28 72 31 21 3d 72 32 29 20 29 20 73 71 6c 69 74 (r1!=r2) ) sqlit
21e44 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 e3ReleaseTempReg
21e45 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 (pParse, r1);.
21e46 20 20 70 45 78 70 72 2d 3e 6f 70 32 20 3d 20 70 pExpr->op2 = p
21e47 45 78 70 72 2d 3e 6f 70 3b 0a 20 20 20 20 70 45 Expr->op;. pE
21e48 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 52 45 47 xpr->op = TK_REG
21e49 49 53 54 45 52 3b 0a 20 20 20 20 70 45 78 70 72 ISTER;. pExpr
21e4a 2d 3e 69 54 61 62 6c 65 20 3d 20 72 32 3b 0a 20 ->iTable = r2;.
21e4b 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 return WRC_Pr
21e4c 75 6e 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 une;. }. retur
21e4d 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a n WRC_Continue;.
21e4e 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 65 76 61 6c }../*.** Preeval
21e4f 75 61 74 65 20 63 6f 6e 73 74 61 6e 74 20 73 75 uate constant su
21e50 62 65 78 70 72 65 73 73 69 6f 6e 73 20 77 69 74 bexpressions wit
21e51 68 69 6e 20 70 45 78 70 72 20 61 6e 64 20 73 74 hin pExpr and st
21e52 6f 72 65 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c ore the.** resul
21e53 74 73 20 69 6e 20 72 65 67 69 73 74 65 72 73 2e ts in registers.
21e54 20 20 4d 6f 64 69 66 79 20 70 45 78 70 72 20 73 Modify pExpr s
21e55 6f 20 74 68 61 74 20 74 68 65 20 63 6f 6e 73 74 o that the const
21e56 61 6e 74 20 73 75 62 65 78 70 72 65 73 69 6f 6e ant subexpresion
21e57 73 0a 2a 2a 20 61 72 65 20 54 4b 5f 52 45 47 49 s.** are TK_REGI
21e58 53 54 45 52 20 6f 70 63 6f 64 65 73 20 74 68 61 STER opcodes tha
21e59 74 20 72 65 66 65 72 20 74 6f 20 74 68 65 20 70 t refer to the p
21e5a 72 65 63 6f 6d 70 75 74 65 64 20 76 61 6c 75 65 recomputed value
21e5b 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
21e5c 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
21e5d 33 45 78 70 72 43 6f 64 65 43 6f 6e 73 74 61 6e 3ExprCodeConstan
21e5e 74 73 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 ts(Parse *pParse
21e5f 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a , Expr *pExpr){.
21e60 20 20 57 61 6c 6b 65 72 20 77 3b 0a 20 20 77 2e Walker w;. w.
21e61 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 xExprCallback =
21e62 65 76 61 6c 43 6f 6e 73 74 45 78 70 72 3b 0a 20 evalConstExpr;.
21e63 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 w.xSelectCallba
21e64 63 6b 20 3d 20 30 3b 0a 20 20 77 2e 70 50 61 72 ck = 0;. w.pPar
21e65 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 73 se = pParse;. s
21e66 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 26 qlite3WalkExpr(&
21e67 77 2c 20 70 45 78 70 72 29 3b 0a 7d 0a 0a 0a 2f w, pExpr);.}.../
21e68 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f *.** Generate co
21e69 64 65 20 74 68 61 74 20 70 75 73 68 65 73 20 74 de that pushes t
21e6a 68 65 20 76 61 6c 75 65 20 6f 66 20 65 76 65 72 he value of ever
21e6b 79 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 y element of the
21e6c 20 67 69 76 65 6e 0a 2a 2a 20 65 78 70 72 65 73 given.** expres
21e6d 73 69 6f 6e 20 6c 69 73 74 20 69 6e 74 6f 20 61 sion list into a
21e6e 20 73 65 71 75 65 6e 63 65 20 6f 66 20 72 65 67 sequence of reg
21e6f 69 73 74 65 72 73 20 62 65 67 69 6e 6e 69 6e 67 isters beginning
21e70 20 61 74 20 74 61 72 67 65 74 2e 0a 2a 2a 0a 2a at target..**.*
21e71 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d * Return the num
21e72 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 ber of elements
21e73 65 76 61 6c 75 61 74 65 64 2e 0a 2a 2f 0a 53 51 evaluated..*/.SQ
21e74 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
21e75 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
21e76 45 78 70 72 4c 69 73 74 28 0a 20 20 50 61 72 73 ExprList(. Pars
21e77 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f e *pParse, /
21e78 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 * Parsing contex
21e79 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 t */. ExprList
21e7a 2a 70 4c 69 73 74 2c 20 20 20 2f 2a 20 54 68 65 *pList, /* The
21e7b 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 expression list
21e7c 20 74 6f 20 62 65 20 63 6f 64 65 64 20 2a 2f 0a to be coded */.
21e7d 20 20 69 6e 74 20 74 61 72 67 65 74 2c 20 20 20 int target,
21e7e 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f /* Where to
21e7f 20 77 72 69 74 65 20 72 65 73 75 6c 74 73 20 2a write results *
21e80 2f 0a 20 20 69 6e 74 20 64 6f 48 61 72 64 43 6f /. int doHardCo
21e81 70 79 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 61 py /* Make a
21e82 20 68 61 72 64 20 63 6f 70 79 20 6f 66 20 65 76